{"draft":null,"from":3,"to":318,"files":[{"path":"src/engine/experimental/projection-reader.ts","added":275,"removed":0,"diff":"--- /dev/null\n+++ b/src/engine/experimental/projection-reader.ts\n@@ -0,0 +1,275 @@\n+import { startNode } from \"../keys.ts\";\n+import { StableSet } from \"../state-source.ts\";\n+import type { FileAttrs, ReadView, SeqLine, VisibleLine } from \"../view.ts\";\n+import type { ExperimentalMarkerProjection, MarkerState } from \"./projection-marker.ts\";\n+\n+/** Private reader for a host-verified immutable projection. Not a production View replacement. */\n+export class ProjectionReader implements ReadView {\n+\treadonly #adapter: ExperimentalMarkerProjection;\n+\treadonly #state: MarkerState;\n+\treadonly #assertLive: () => void;\n+\tconstructor(adapter: ExperimentalMarkerProjection, state: MarkerState, assertLive: () => void) {\n+\t\tassertLive();\n+\t\tthis.#assertLive = assertLive;\n+\t\tadapter.assertContext(state);\n+\t\tthis.#adapter = adapter;\n+\t\tthis.#state = structuredClone(state);\n+\t}\n+\tget graph() {\n+\t\tthis.#assertLive();\n+\t\treturn this.#adapter.graph;\n+\t}\n+\tget set(): ReadonlySet<string> {\n+\t\tthis.#assertLive();\n+\t\tconst has = (id: string) =>\n+\t\t\tthis.graph.has(id) && (this.#adapter.members.get(this.#state.membership, Number(id.slice(1)))?.present ?? false);\n+\t\tconst assertLive = this.#assertLive;\n+\t\tconst members = this.#adapter.members;\n+\t\tconst root = this.#state.membership;\n+\t\tconst graph = this.graph;\n+\t\treturn new (class extends StableSet<string> {\n+\t\t\treadonly retainedBytes = 64;\n+\t\t\thas(id: string): boolean {\n+\t\t\t\tassertLive();\n+\t\t\t\treturn has(id);\n+\t\t\t}\n+\t\t\t*values(): SetIterator<string> {\n+\t\t\t\tassertLive();\n+\t\t\t\tconst iterator = members.entries(root);\n+\t\t\t\ttry {\n+\t\t\t\t\tfor (;;) {\n+\t\t\t\t\t\tassertLive();\n+\t\t\t\t\t\tconst item = iterator.next();\n+\t\t\t\t\t\tif (item.done) return;\n+\t\t\t\t\t\tconst [seq, member] = item.value;\n+\t\t\t\t\t\tif (member.present) yield graph.idOfSeq(seq);\n+\t\t\t\t\t}\n+\t\t\t\t} finally {\n+\t\t\t\t\titerator.return?.();\n+\t\t\t\t}\n+\t\t\t}\n+\t\t})();\n+\t}\n+\tpaths(): string[] {\n+\t\tthis.#assertLive();\n+\t\treturn this.knownPaths().filter((path) => this.hasFile(path));\n+\t}\n+\tfiles(): Record<string, string> {\n+\t\tthis.#assertLive();\n+\t\treturn Object.fromEntries(\n+\t\t\tthis.paths()\n+\t\t\t\t.filter((path) => this.attrs(path).blob === null)\n+\t\t\t\t.map((path) => [path, this.text(path)]),\n+\t\t);\n+\t}\n+\tblobs(): Record<string, string> {\n+\t\tthis.#assertLive();\n+\t\tconst result: Record<string, string> = {};\n+\t\tfor (const path of this.paths()) {\n+\t\t\tconst blob = this.attrs(path).blob;\n+\t\t\tif (blob !== null) result[path] = blob;\n+\t\t}\n+\t\treturn result;\n+\t}\n+\tlineNumber(path: string, line: string): number {\n+\t\tthis.#assertLive();\n+\t\tlet index = 0;\n+\t\tfor (const id of this.#adapter.lineIds(this.#state, path)) {\n+\t\t\tthis.#assertLive();\n+\t\t\tif (id === line) return index;\n+\t\t\tindex++;\n+\t\t}\n+\t\tthis.#assertLive();\n+\t\treturn -1;\n+\t}\n+\tsameText(path: string, other: ReadView): boolean {\n+\t\tthis.#assertLive();\n+\t\tconst a = this.lines(path),\n+\t\t\tb = other.lines(path);\n+\t\treturn a.length === b.length && a.every((line, index) => line.text === b[index]!.text);\n+\t}\n+\tvisibleNeighbours(_path: string, _line: string): string[] {\n+\t\tthis.#assertLive();\n+\t\tthrow Error(\"unsupported projected visibleNeighbours\");\n+\t}\n+\tweight(): number {\n+\t\tthis.#assertLive();\n+\t\tthrow Error(\"transaction projections cannot enter historical View cache\");\n+\t}\n+\n+\tattrs(path: string): FileAttrs {\n+\t\tthis.#assertLive();\n+\t\treturn this.#adapter.attrs(this.#state, path);\n+\t}\n+\tknownPaths(): string[] {\n+\t\tthis.#assertLive();\n+\t\treturn this.#adapter.knownPaths(this.#state);\n+\t}\n+\thasMoves(): boolean {\n+\t\tthis.#assertLive();\n+\t\tfor (const id of this.graph.movers()) if (this.set.has(id)) return true;\n+\t\treturn false;\n+\t}\n+\t*#textChunks(n: number): IterableIterator<string> {\n+\t\tthis.#assertLive();\n+\t\tconst iterator = this.graph.textChunks(n);\n+\t\twhile (true) {\n+\t\t\tthis.#assertLive();\n+\t\t\tconst next = iterator.next();\n+\t\t\tthis.#assertLive();\n+\t\t\tif (next.done) return;\n+\t\t\tyield next.value;\n+\t\t}\n+\t}\n+\t*lineChunks(path: string): IterableIterator<() => Iterable<string>> {\n+\t\tthis.#assertLive();\n+\t\tconst iterator = this.#adapter.lineIds(this.#state, path);\n+\t\twhile (true) {\n+\t\t\tthis.#assertLive();\n+\t\t\tconst next = iterator.next();\n+\t\t\tthis.#assertLive();\n+\t\t\tif (next.done) return;\n+\t\t\tconst n = this.graph.lineN(next.value);\n+\t\t\tyield () => this.#textChunks(n);\n+\t\t}\n+\t}\n+\n+\t*visibleLines(path: string): IterableIterator<VisibleLine> {\n+\t\tthis.#assertLive();\n+\t\tconst graph = this.#adapter.graph;\n+\t\tconst iterator = this.#adapter.lineIds(this.#state, path);\n+\t\twhile (true) {\n+\t\t\tthis.#assertLive();\n+\t\t\tconst next = iterator.next();\n+\t\t\tthis.#assertLive();\n+\t\t\tif (next.done) return;\n+\t\t\tconst id = next.value;\n+\t\t\tconst n = graph.lineN(id);\n+\t\t\tyield { id, text: graph.textOf(n), atom: graph.idOfSeq(graph.writerSeqOf(n)) };\n+\t\t}\n+\t}\n+\tlines(path: string): readonly VisibleLine[] {\n+\t\tthis.#assertLive();\n+\t\treturn [...this.visibleLines(path)];\n+\t}\n+\t*#buildingLines(path: string): IterableIterator<VisibleLine> {\n+\t\tthis.#assertLive();\n+\t\tconst graph = this.#adapter.graph;\n+\t\tconst iterator = this.#adapter.lineIds(this.#state, path);\n+\t\twhile (true) {\n+\t\t\tthis.#assertLive();\n+\t\t\tconst next = iterator.next();\n+\t\t\tthis.#assertLive();\n+\t\t\tif (next.done) return;\n+\t\t\tconst id = next.value;\n+\t\t\tconst n = graph.lineN(id);\n+\t\t\tconst assertLive = () => this.#assertLive();\n+\t\t\tyield {\n+\t\t\t\tid,\n+\t\t\t\ttextChunks: () => this.#textChunks(n),\n+\t\t\t\tget text() {\n+\t\t\t\t\tassertLive();\n+\t\t\t\t\treturn graph.textOf(n);\n+\t\t\t\t},\n+\t\t\t\tatom: graph.idOfSeq(graph.writerSeqOf(n)),\n+\t\t\t};\n+\t\t}\n+\t}\n+\tbuildLines(path: string): readonly VisibleLine[] {\n+\t\tthis.#assertLive();\n+\t\treturn [...this.#buildingLines(path)];\n+\t}\n+\n+\tsequence(path: string): readonly SeqLine[] {\n+\t\tthis.#assertLive();\n+\t\tconst adapter = this.#adapter,\n+\t\t\tstate = this.#state,\n+\t\t\tgraph = adapter.graph;\n+\t\tconst key = adapter.fileKey(path);\n+\t\tconst file = key === undefined ? undefined : adapter.markers.get(state.locator, key);\n+\t\tif (!file) return [];\n+\t\tconst result: SeqLine[] = [];\n+\t\tfor (const node of adapter.sequence.range(\n+\t\t\tstate.sequence,\n+\t\t\tadapter.sequence.rank(state.sequence, file.enter) + 1,\n+\t\t\tadapter.sequence.rank(state.sequence, file.exit),\n+\t\t\tfalse,\n+\t\t)) {\n+\t\t\tif (node.text.endsWith(\":end\")) continue;\n+\t\t\tconst n = graph.lineN(node.text),\n+\t\t\t\twriter = graph.writerSeqOf(n);\n+\t\t\tif (!adapter.members.get(state.membership, writer)?.present) continue;\n+\t\t\tconst deletedBy = [...graph.eachDeleterSeqOf(n)]\n+\t\t\t\t.filter((seq) => adapter.members.get(state.membership, seq)?.present)\n+\t\t\t\t.sort((a, b) => graph.lamportOfSeq(a) - graph.lamportOfSeq(b) || a - b)\n+\t\t\t\t.map((seq) => graph.idOfSeq(seq));\n+\t\t\tresult.push({\n+\t\t\t\tid: node.text,\n+\t\t\t\ttext: graph.textOf(n),\n+\t\t\t\tatom: graph.idOfSeq(writer),\n+\t\t\t\tdeletedBy,\n+\t\t\t\tvisible: deletedBy.length === 0,\n+\t\t\t});\n+\t\t}\n+\t\treturn result;\n+\t}\n+\tsequenceIndex(path: string, line: string): number {\n+\t\tthis.#assertLive();\n+\t\treturn this.sequence(path).findIndex((item) => item.id === line);\n+\t}\n+\tparentOf(line: string): string {\n+\t\tthis.#assertLive();\n+\t\tconst adapter = this.#adapter,\n+\t\t\tstate = this.#state;\n+\t\tconst marker = adapter.markers.get(state.locator, adapter.graph.lineN(line) + 1);\n+\t\tif (marker?.kind !== \"line\") throw Error(\"missing projected line\");\n+\t\tif (marker.parentKey > 0 && marker.parentKey < 0x80000000) return adapter.graph.lineIdOf(marker.parentKey - 1);\n+\t\tconst parent = adapter.markers.get(state.locator, marker.parentKey);\n+\t\tif (parent?.kind !== \"file\") throw Error(\"missing projected file parent\");\n+\t\tconst text = adapter.sequence.get(state.sequence.map, parent.enter).text;\n+\t\tif (!text.startsWith(\"root:\")) throw Error(\"invalid projected file parent\");\n+\t\treturn startNode(text.slice(5));\n+\t}\n+\tfileOf(line: string): string {\n+\t\tthis.#assertLive();\n+\t\tif (line.startsWith(\"^\")) return line.slice(1);\n+\t\tlet current = line;\n+\t\twhile (!current.startsWith(\"^\")) {\n+\t\t\tconst n = this.graph.lineN(current);\n+\t\t\tif (n < 0) throw Error(\"unknown projected line\");\n+\t\t\tconst marker = this.#adapter.markers.get(this.#state.locator, n + 1);\n+\t\t\tif (marker?.kind === \"line\") return this.#adapter.sequence.fileAt(this.#state.sequence, marker.enter);\n+\t\t\t// A line written outside this view has no marker. Follow canonical ancestry\n+\t\t\t// until it reaches a line whose placement this view actually contains.\n+\t\t\tif (marker || this.#adapter.members.get(this.#state.membership, this.graph.writerSeqOf(n))?.present)\n+\t\t\t\tthrow Error(\"missing projected line\");\n+\t\t\tconst parent = this.graph.lineParent(current);\n+\t\t\tif (parent === undefined) throw Error(\"missing projected line parent\");\n+\t\t\tcurrent = parent;\n+\t\t}\n+\t\treturn current.slice(1);\n+\t}\n+\ttext(path: string): string {\n+\t\tthis.#assertLive();\n+\t\tlet text = \"\",\n+\t\t\tpart = \"\";\n+\t\tfor (const line of this.visibleLines(path)) {\n+\t\t\tpart += `${line.text}\\n`;\n+\t\t\tif (part.length >= 16384) {\n+\t\t\t\ttext += part;\n+\t\t\t\tpart = \"\";\n+\t\t\t}\n+\t\t}\n+\t\treturn text + part;\n+\t}\n+\thasFile(path: string): boolean {\n+\t\tthis.#assertLive();\n+\t\tconst attrs = this.attrs(path);\n+\t\treturn (\n+\t\t\tattrs.exists ||\n+\t\t\tattrs.link !== null ||\n+\t\t\tattrs.blob !== null ||\n+\t\t\t!this.#adapter.lineIds(this.#state, path).next().done\n+\t\t);\n+\t}\n+}"}],"unmatched":[]}