- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/src/engine/experimental/projection-reader.ts?format=json
275 linescodex-capacity-chair 100%Last change codex-capacity-chair in version 175, 5 versions changed it
1import { startNode } from "../keys.ts";
2import { StableSet } from "../state-source.ts";
3import type { FileAttrs, ReadView, SeqLine, VisibleLine } from "../view.ts";
4import type { ExperimentalMarkerProjection, MarkerState } from "./projection-marker.ts";
6/** Private reader for a host-verified immutable projection. Not a production View replacement. */
7export class ProjectionReader implements ReadView {
8 readonly #adapter: ExperimentalMarkerProjection;
9 readonly #state: MarkerState;
10 readonly #assertLive: () => void;
11 constructor(adapter: ExperimentalMarkerProjection, state: MarkerState, assertLive: () => void) {
12 assertLive();
13 this.#assertLive = assertLive;
14 adapter.assertContext(state);
15 this.#adapter = adapter;
16 this.#state = structuredClone(state);
17 }
18 get graph() {
19 this.#assertLive();
20 return this.#adapter.graph;
21 }
22 get set(): ReadonlySet<string> {
23 this.#assertLive();
24 const has = (id: string) =>
25 this.graph.has(id) && (this.#adapter.members.get(this.#state.membership, Number(id.slice(1)))?.present ?? false);
26 const assertLive = this.#assertLive;
27 const members = this.#adapter.members;
28 const root = this.#state.membership;
29 const graph = this.graph;
30 return new (class extends StableSet<string> {
31 readonly retainedBytes = 64;
32 has(id: string): boolean {
33 assertLive();
34 return has(id);
35 }
36 *values(): SetIterator<string> {
37 assertLive();
38 const iterator = members.entries(root);
39 try {
40 for (;;) {
41 assertLive();
42 const item = iterator.next();
43 if (item.done) return;
44 const [seq, member] = item.value;
45 if (member.present) yield graph.idOfSeq(seq);
46 }
47 } finally {
48 iterator.return?.();
49 }
50 }
51 })();
52 }
53 paths(): string[] {
54 this.#assertLive();
55 return this.knownPaths().filter((path) => this.hasFile(path));
56 }
57 files(): Record<string, string> {
58 this.#assertLive();
59 return Object.fromEntries(
60 this.paths()
61 .filter((path) => this.attrs(path).blob === null)
62 .map((path) => [path, this.text(path)]),
63 );
64 }
65 blobs(): Record<string, string> {
66 this.#assertLive();
67 const result: Record<string, string> = {};
68 for (const path of this.paths()) {
69 const blob = this.attrs(path).blob;
70 if (blob !== null) result[path] = blob;
71 }
72 return result;
73 }
74 lineNumber(path: string, line: string): number {
75 this.#assertLive();
76 let index = 0;
77 for (const id of this.#adapter.lineIds(this.#state, path)) {
78 this.#assertLive();
79 if (id === line) return index;
80 index++;
81 }
82 this.#assertLive();
83 return -1;
84 }
85 sameText(path: string, other: ReadView): boolean {
86 this.#assertLive();
87 const a = this.lines(path),
88 b = other.lines(path);
89 return a.length === b.length && a.every((line, index) => line.text === b[index]!.text);
90 }
91 visibleNeighbours(_path: string, _line: string): string[] {
92 this.#assertLive();
93 throw Error("unsupported projected visibleNeighbours");
94 }
95 weight(): number {
96 this.#assertLive();
97 throw Error("transaction projections cannot enter historical View cache");
98 }
100 attrs(path: string): FileAttrs {
101 this.#assertLive();
102 return this.#adapter.attrs(this.#state, path);
103 }
104 knownPaths(): string[] {
105 this.#assertLive();
106 return this.#adapter.knownPaths(this.#state);
107 }
108 hasMoves(): boolean {
109 this.#assertLive();
110 for (const id of this.graph.movers()) if (this.set.has(id)) return true;
111 return false;
112 }
113 *#textChunks(n: number): IterableIterator<string> {
114 this.#assertLive();
115 const iterator = this.graph.textChunks(n);
116 while (true) {
117 this.#assertLive();
118 const next = iterator.next();
119 this.#assertLive();
120 if (next.done) return;
121 yield next.value;
122 }
123 }
124 *lineChunks(path: string): IterableIterator<() => Iterable<string>> {
125 this.#assertLive();
126 const iterator = this.#adapter.lineIds(this.#state, path);
127 while (true) {
128 this.#assertLive();
129 const next = iterator.next();
130 this.#assertLive();
131 if (next.done) return;
132 const n = this.graph.lineN(next.value);
133 yield () => this.#textChunks(n);
134 }
135 }
137 *visibleLines(path: string): IterableIterator<VisibleLine> {
138 this.#assertLive();
139 const graph = this.#adapter.graph;
140 const iterator = this.#adapter.lineIds(this.#state, path);
141 while (true) {
142 this.#assertLive();
143 const next = iterator.next();
144 this.#assertLive();
145 if (next.done) return;
146 const id = next.value;
147 const n = graph.lineN(id);
148 yield { id, text: graph.textOf(n), atom: graph.idOfSeq(graph.writerSeqOf(n)) };
149 }
150 }
151 lines(path: string): readonly VisibleLine[] {
152 this.#assertLive();
153 return [...this.visibleLines(path)];
154 }
155 *#buildingLines(path: string): IterableIterator<VisibleLine> {
156 this.#assertLive();
157 const graph = this.#adapter.graph;
158 const iterator = this.#adapter.lineIds(this.#state, path);
159 while (true) {
160 this.#assertLive();
161 const next = iterator.next();
162 this.#assertLive();
163 if (next.done) return;
164 const id = next.value;
165 const n = graph.lineN(id);
166 const assertLive = () => this.#assertLive();
167 yield {
168 id,
169 textChunks: () => this.#textChunks(n),
170 get text() {
171 assertLive();
172 return graph.textOf(n);
173 },
174 atom: graph.idOfSeq(graph.writerSeqOf(n)),
175 };
176 }
177 }
178 buildLines(path: string): readonly VisibleLine[] {
179 this.#assertLive();
180 return [...this.#buildingLines(path)];
181 }
183 sequence(path: string): readonly SeqLine[] {
184 this.#assertLive();
185 const adapter = this.#adapter,
186 state = this.#state,
187 graph = adapter.graph;
188 const key = adapter.fileKey(path);
189 const file = key === undefined ? undefined : adapter.markers.get(state.locator, key);
190 if (!file) return [];
191 const result: SeqLine[] = [];
192 for (const node of adapter.sequence.range(
193 state.sequence,
194 adapter.sequence.rank(state.sequence, file.enter) + 1,
195 adapter.sequence.rank(state.sequence, file.exit),
196 false,
197 )) {
198 if (node.text.endsWith(":end")) continue;
199 const n = graph.lineN(node.text),
200 writer = graph.writerSeqOf(n);
201 if (!adapter.members.get(state.membership, writer)?.present) continue;
202 const deletedBy = [...graph.eachDeleterSeqOf(n)]
203 .filter((seq) => adapter.members.get(state.membership, seq)?.present)
204 .sort((a, b) => graph.lamportOfSeq(a) - graph.lamportOfSeq(b) || a - b)
205 .map((seq) => graph.idOfSeq(seq));
206 result.push({
207 id: node.text,
208 text: graph.textOf(n),
209 atom: graph.idOfSeq(writer),
210 deletedBy,
211 visible: deletedBy.length === 0,
212 });
213 }
214 return result;
215 }
216 sequenceIndex(path: string, line: string): number {
217 this.#assertLive();
218 return this.sequence(path).findIndex((item) => item.id === line);
219 }
220 parentOf(line: string): string {
221 this.#assertLive();
222 const adapter = this.#adapter,
223 state = this.#state;
224 const marker = adapter.markers.get(state.locator, adapter.graph.lineN(line) + 1);
225 if (marker?.kind !== "line") throw Error("missing projected line");
226 if (marker.parentKey > 0 && marker.parentKey < 0x80000000) return adapter.graph.lineIdOf(marker.parentKey - 1);
227 const parent = adapter.markers.get(state.locator, marker.parentKey);
228 if (parent?.kind !== "file") throw Error("missing projected file parent");
229 const text = adapter.sequence.get(state.sequence.map, parent.enter).text;
230 if (!text.startsWith("root:")) throw Error("invalid projected file parent");
231 return startNode(text.slice(5));
232 }
233 fileOf(line: string): string {
234 this.#assertLive();
235 if (line.startsWith("^")) return line.slice(1);
236 let current = line;
237 while (!current.startsWith("^")) {
238 const n = this.graph.lineN(current);
239 if (n < 0) throw Error("unknown projected line");
240 const marker = this.#adapter.markers.get(this.#state.locator, n + 1);
241 if (marker?.kind === "line") return this.#adapter.sequence.fileAt(this.#state.sequence, marker.enter);
242 // A line written outside this view has no marker. Follow canonical ancestry
243 // until it reaches a line whose placement this view actually contains.
244 if (marker || this.#adapter.members.get(this.#state.membership, this.graph.writerSeqOf(n))?.present)
245 throw Error("missing projected line");
246 const parent = this.graph.lineParent(current);
247 if (parent === undefined) throw Error("missing projected line parent");
248 current = parent;
249 }
250 return current.slice(1);
251 }
252 text(path: string): string {
253 this.#assertLive();
254 let text = "",
255 part = "";
256 for (const line of this.visibleLines(path)) {
257 part += `${line.text}\n`;
258 if (part.length >= 16384) {
259 text += part;
260 part = "";
261 }
262 }
263 return text + part;
264 }
265 hasFile(path: string): boolean {
266 this.#assertLive();
267 const attrs = this.attrs(path);
268 return (
269 attrs.exists ||
270 attrs.link !== null ||
271 attrs.blob !== null ||
272 !this.#adapter.lineIds(this.#state, path).next().done
273 );
274 }
275}