{"draft":null,"from":4,"to":318,"files":[{"path":"src/engine/experimental/projection-sequence.ts","added":375,"removed":0,"diff":"--- /dev/null\n+++ b/src/engine/experimental/projection-sequence.ts\n@@ -0,0 +1,375 @@\n+import type { GraphStateStorage } from \"../state-source.ts\";\n+import { ProjectionLocator } from \"./projection-locator.ts\";\n+\n+type Page = number | null;\n+interface SequenceNode {\n+\tid: number;\n+\tparent: Page;\n+\tleft: Page;\n+\tright: Page;\n+\ttext: string;\n+\tdeletions: number;\n+\theight: number;\n+\tcount: number;\n+\tvisible: number;\n+\tfileStarts: number;\n+}\n+export interface SequenceState {\n+\treadonly map: Page;\n+\treadonly root: Page;\n+\treadonly next: number;\n+}\n+export type SequenceOperation =\n+\t| { kind: \"insert\"; rank: number; text: string; hidden?: boolean }\n+\t| { kind: \"visibility\"; id: number; delta: number }\n+\t| { kind: \"move\"; from: number; length: number; to: number };\n+const safe = (value: unknown): value is number => Number.isSafeInteger(value) && Number(value) >= 0;\n+function decode(value: unknown): SequenceNode {\n+\tif (!value || typeof value !== \"object\" || Array.isArray(value)) throw Error(\"invalid sequence node\");\n+\tconst row = value as Record<string, unknown>;\n+\tif (Object.keys(row).length !== 10 || !safe(row.id) || row.id >= 2 ** 32 || typeof row.text !== \"string\")\n+\t\tthrow Error(\"invalid sequence node fields\");\n+\tfor (const name of [\"parent\", \"left\", \"right\"])\n+\t\tif (row[name] !== null && (!safe(row[name]) || Number(row[name]) >= 2 ** 32 || row[name] === row.id))\n+\t\t\tthrow Error(\"invalid sequence edge\");\n+\tfor (const name of [\"deletions\", \"height\", \"count\", \"visible\", \"fileStarts\"])\n+\t\tif (!safe(row[name])) throw Error(\"invalid sequence aggregate\");\n+\tif (\n+\t\tNumber(row.height) < 1 ||\n+\t\tNumber(row.height) > 64 ||\n+\t\tNumber(row.count) < 1 ||\n+\t\tNumber(row.visible) > Number(row.count) ||\n+\t\tNumber(row.fileStarts) > Number(row.count)\n+\t)\n+\t\tthrow Error(\"invalid sequence aggregate bounds\");\n+\treturn row as unknown as SequenceNode;\n+}\n+export class ProjectionSequence {\n+\treadonly storage: GraphStateStorage;\n+\treadonly #locator: ProjectionLocator<SequenceNode>;\n+\tconstructor(storage: GraphStateStorage, namespace: string) {\n+\t\tthis.storage = storage;\n+\t\tthis.#locator = new ProjectionLocator(storage, namespace, decode);\n+\t}\n+\tempty(): SequenceState {\n+\t\treturn { map: null, root: null, next: 0 };\n+\t}\n+\tget(map: Page, id: number): SequenceNode {\n+\t\tconst node = this.#locator.get(map, id);\n+\t\tif (!node || node.id !== id) throw Error(\"missing sequence node\");\n+\t\tconst child = (key: Page): SequenceNode | undefined => {\n+\t\t\tif (key === null) return undefined;\n+\t\t\tconst value = this.#locator.get(map, key);\n+\t\t\tif (!value || value.id !== key || value.parent !== id) throw Error(\"invalid sequence child identity\");\n+\t\t\treturn value;\n+\t\t};\n+\t\tconst left = child(node.left),\n+\t\t\tright = child(node.right);\n+\t\tif (\n+\t\t\tnode.height !== 1 + Math.max(left?.height ?? 0, right?.height ?? 0) ||\n+\t\t\tMath.abs((left?.height ?? 0) - (right?.height ?? 0)) > 1 ||\n+\t\t\tnode.count !== 1 + (left?.count ?? 0) + (right?.count ?? 0) ||\n+\t\t\tnode.visible !== (node.deletions === 0 ? 1 : 0) + (left?.visible ?? 0) + (right?.visible ?? 0) ||\n+\t\t\tnode.fileStarts !== (node.text.startsWith(\"root:\") ? 1 : 0) + (left?.fileStarts ?? 0) + (right?.fileStarts ?? 0)\n+\t\t)\n+\t\t\tthrow Error(\"inconsistent sequence aggregate\");\n+\t\treturn node;\n+\t}\n+\t#state(state: SequenceState): void {\n+\t\tif (\n+\t\t\t!state ||\n+\t\t\t!safe(state.next) ||\n+\t\t\tstate.next > 2 ** 32 ||\n+\t\t\t(state.map !== null && !safe(state.map)) ||\n+\t\t\t(state.root !== null && (!safe(state.root) || state.root >= state.next)) ||\n+\t\t\t(state.root === null) !== (state.next === 0) ||\n+\t\t\t(state.map === null) !== (state.root === null)\n+\t\t)\n+\t\t\tthrow Error(\"invalid sequence state\");\n+\t\tif (state.root !== null && this.get(state.map, state.root).parent !== null)\n+\t\t\tthrow Error(\"invalid sequence root parent\");\n+\t}\n+\tset(map: Page, id: number, node: SequenceNode): number {\n+\t\tif (node.id !== id) throw Error(\"sequence node identity mismatch\");\n+\t\treturn this.#locator.set(map, id, node);\n+\t}\n+\trank(state: SequenceState, id: number): number {\n+\t\tthis.#state(state);\n+\t\tlet node = this.get(state.map, id);\n+\t\tif (!node) throw Error(\"unknown marker\");\n+\t\tconst count = (id: Page): number => (id === null ? 0 : this.get(state.map, id).count);\n+\t\tlet rank = count(node.left),\n+\t\t\tsteps = 0;\n+\t\twhile (node.parent !== null) {\n+\t\t\tif (++steps > 64) throw Error(\"invalid parent chain\");\n+\t\t\tconst parent = this.get(state.map, node.parent);\n+\t\t\tif (parent.right === node.id) rank += count(parent.left) + 1;\n+\t\t\telse if (parent.left !== node.id) throw Error(\"broken parent edge\");\n+\t\t\tnode = parent;\n+\t\t}\n+\t\tif (node.id !== state.root) throw Error(\"wrong root\");\n+\t\treturn rank;\n+\t}\n+\t/** Nearest file start before a marker, using subtree counts instead of line ancestry. */\n+\tfileAt(state: SequenceState, id: number): string {\n+\t\tconst target = this.rank(state, id);\n+\t\tconst seek = (id: Page, offset: number, depth: number): string | undefined => {\n+\t\t\tif (id === null || offset > target) return undefined;\n+\t\t\tif (depth > 64) throw Error(\"invalid file lookup depth\");\n+\t\t\tconst node = this.get(state.map, id);\n+\t\t\tif (!node.fileStarts) return undefined;\n+\t\t\tconst leftCount = node.left === null ? 0 : this.get(state.map, node.left).count;\n+\t\t\tconst at = offset + leftCount;\n+\t\t\tif (at <= target) {\n+\t\t\t\tconst right = seek(node.right, at + 1, depth + 1);\n+\t\t\t\tif (right !== undefined) return right;\n+\t\t\t\tif (node.text.startsWith(\"root:\")) return node.text.slice(5);\n+\t\t\t}\n+\t\t\treturn seek(node.left, offset, depth + 1);\n+\t\t};\n+\t\tconst file = seek(state.root, 0, 0);\n+\t\tif (file === undefined) throw Error(\"missing projected file start\");\n+\t\treturn file;\n+\t}\n+\tupdate(state: SequenceState, operation: SequenceOperation): SequenceState {\n+\t\treturn this.updateMany(state, [operation]);\n+\t}\n+\t/** A small bounded group shares changed AVL nodes and one locator flush. */\n+\tupdateMany(state: SequenceState, operations: readonly SequenceOperation[]): SequenceState {\n+\t\tif (operations.length < 1 || operations.length > 16) throw Error(\"invalid sequence operation batch\");\n+\t\tthis.#state(state);\n+\t\treturn this.storage.transaction(() => {\n+\t\t\tlet map = state.map;\n+\t\t\t// One operation changes an AVL path plus rotation neighbors, not history.\n+\t\t\t// Keep only those decoded records and persist each final record once.\n+\t\t\tconst changed = new Map<number, SequenceNode>();\n+\t\t\t// The immutable input map is fixed for this batch. Cache only its bounded reads;\n+\t\t\t// changed nodes always take precedence and the cache never survives a transaction.\n+\t\t\tconst original = new Map<number, SequenceNode>();\n+\t\t\tlet originalBytes = 0;\n+\t\t\tconst get = (id: number): SequenceNode => {\n+\t\t\t\tconst found = changed.get(id) ?? original.get(id);\n+\t\t\t\tif (found) return found;\n+\t\t\t\tconst node = this.get(map, id);\n+\t\t\t\tconst bytes = 192 + node.text.length * 2;\n+\t\t\t\tif (original.size < 256 && originalBytes + bytes <= 65536) {\n+\t\t\t\t\toriginal.set(id, node);\n+\t\t\t\t\toriginalBytes += bytes;\n+\t\t\t\t}\n+\t\t\t\treturn node;\n+\t\t\t};\n+\t\t\tconst put = (node: SequenceNode): number => {\n+\t\t\t\tchanged.set(node.id, node);\n+\t\t\t\treturn node.id;\n+\t\t\t};\n+\t\t\tconst height = (id: Page): number => (id === null ? 0 : get(id).height);\n+\t\t\tconst count = (id: Page): number => (id === null ? 0 : get(id).count);\n+\t\t\tconst visible = (id: Page): number => (id === null ? 0 : get(id).visible);\n+\t\t\tconst fileStarts = (id: Page): number => (id === null ? 0 : get(id).fileStarts);\n+\t\t\tconst parent = (id: Page, p: Page): void => {\n+\t\t\t\tif (id !== null) {\n+\t\t\t\t\tconst node = get(id);\n+\t\t\t\t\tif (node.parent !== p) put({ ...node, parent: p });\n+\t\t\t\t}\n+\t\t\t};\n+\t\t\tconst save = (node: SequenceNode): number => {\n+\t\t\t\tparent(node.left, node.id);\n+\t\t\t\tparent(node.right, node.id);\n+\t\t\t\treturn put({\n+\t\t\t\t\t...node,\n+\t\t\t\t\theight: 1 + Math.max(height(node.left), height(node.right)),\n+\t\t\t\t\tcount: 1 + count(node.left) + count(node.right),\n+\t\t\t\t\tvisible: (node.deletions === 0 ? 1 : 0) + visible(node.left) + visible(node.right),\n+\t\t\t\t\tfileStarts: (node.text.startsWith(\"root:\") ? 1 : 0) + fileStarts(node.left) + fileStarts(node.right),\n+\t\t\t\t});\n+\t\t\t};\n+\t\t\tconst rotateLeft = (id: number): number => {\n+\t\t\t\tconst a = get(id),\n+\t\t\t\t\tb = get(a.right!);\n+\t\t\t\tsave({ ...a, right: b.left, parent: b.id });\n+\t\t\t\treturn save({ ...b, left: a.id, parent: a.parent });\n+\t\t\t};\n+\t\t\tconst rotateRight = (id: number): number => {\n+\t\t\t\tconst a = get(id),\n+\t\t\t\t\tb = get(a.left!);\n+\t\t\t\tsave({ ...a, left: b.right, parent: b.id });\n+\t\t\t\treturn save({ ...b, right: a.id, parent: a.parent });\n+\t\t\t};\n+\t\t\tconst balance = (id: number): number => {\n+\t\t\t\tconst node = get(id);\n+\t\t\t\tif (height(node.left) - height(node.right) > 1) {\n+\t\t\t\t\tconst left = get(node.left!);\n+\t\t\t\t\tif (height(left.left) < height(left.right)) save({ ...node, left: rotateLeft(left.id) });\n+\t\t\t\t\treturn rotateRight(id);\n+\t\t\t\t}\n+\t\t\t\tif (height(node.right) - height(node.left) > 1) {\n+\t\t\t\t\tconst right = get(node.right!);\n+\t\t\t\t\tif (height(right.right) < height(right.left)) save({ ...node, right: rotateRight(right.id) });\n+\t\t\t\t\treturn rotateLeft(id);\n+\t\t\t\t}\n+\t\t\t\treturn id;\n+\t\t\t};\n+\t\t\tlet root = state.root,\n+\t\t\t\tnext = state.next;\n+\t\t\tfor (const operation of operations) {\n+\t\t\t\tif (operation.kind === \"insert\") {\n+\t\t\t\t\tconst { rank, text } = operation;\n+\t\t\t\t\tif (!Number.isInteger(rank) || rank < 0 || rank > count(root) || next >= 2 ** 32)\n+\t\t\t\t\t\tthrow Error(\"invalid insert\");\n+\t\t\t\t\tconst added = next++;\n+\t\t\t\t\tconst insert = (id: Page, at: number, p: Page): number => {\n+\t\t\t\t\t\tif (id === null)\n+\t\t\t\t\t\t\treturn put({\n+\t\t\t\t\t\t\t\tid: added,\n+\t\t\t\t\t\t\t\tparent: p,\n+\t\t\t\t\t\t\t\tleft: null,\n+\t\t\t\t\t\t\t\tright: null,\n+\t\t\t\t\t\t\t\ttext,\n+\t\t\t\t\t\t\t\tdeletions: operation.hidden ? 1 : 0,\n+\t\t\t\t\t\t\t\theight: 1,\n+\t\t\t\t\t\t\t\tcount: 1,\n+\t\t\t\t\t\t\t\tvisible: operation.hidden ? 0 : 1,\n+\t\t\t\t\t\t\t\tfileStarts: text.startsWith(\"root:\") ? 1 : 0,\n+\t\t\t\t\t\t\t});\n+\t\t\t\t\t\tconst node = get(id),\n+\t\t\t\t\t\t\tleftCount = count(node.left);\n+\t\t\t\t\t\tif (at <= leftCount) save({ ...node, left: insert(node.left, at, id) });\n+\t\t\t\t\t\telse save({ ...node, right: insert(node.right, at - leftCount - 1, id) });\n+\t\t\t\t\t\treturn balance(id);\n+\t\t\t\t\t};\n+\t\t\t\t\troot = insert(root, rank, null);\n+\t\t\t\t\tparent(root, null);\n+\t\t\t\t} else if (operation.kind === \"move\") {\n+\t\t\t\t\tconst { from, length, to } = operation;\n+\t\t\t\t\tconst total = count(root);\n+\t\t\t\t\tif (\n+\t\t\t\t\t\t![from, length, to].every(Number.isSafeInteger) ||\n+\t\t\t\t\t\tfrom < 0 ||\n+\t\t\t\t\t\tlength < 0 ||\n+\t\t\t\t\t\tfrom + length > total ||\n+\t\t\t\t\t\tto < 0 ||\n+\t\t\t\t\t\tto > total - length\n+\t\t\t\t\t)\n+\t\t\t\t\t\tthrow Error(\"invalid move range\");\n+\t\t\t\t\t// Join two AVL trees around one existing stable marker.\n+\t\t\t\t\tconst join = (left: Page, pivot: number, right: Page): number => {\n+\t\t\t\t\t\tif (height(left) > height(right) + 1) {\n+\t\t\t\t\t\t\tconst node = get(left!);\n+\t\t\t\t\t\t\tsave({ ...node, right: join(node.right, pivot, right) });\n+\t\t\t\t\t\t\tconst result = balance(left!);\n+\t\t\t\t\t\t\tparent(result, null);\n+\t\t\t\t\t\t\treturn result;\n+\t\t\t\t\t\t}\n+\t\t\t\t\t\tif (height(right) > height(left) + 1) {\n+\t\t\t\t\t\t\tconst node = get(right!);\n+\t\t\t\t\t\t\tsave({ ...node, left: join(left, pivot, node.left) });\n+\t\t\t\t\t\t\tconst result = balance(right!);\n+\t\t\t\t\t\t\tparent(result, null);\n+\t\t\t\t\t\t\treturn result;\n+\t\t\t\t\t\t}\n+\t\t\t\t\t\treturn save({ ...get(pivot), left, right, parent: null });\n+\t\t\t\t\t};\n+\t\t\t\t\tconst split = (id: Page, at: number): [Page, Page] => {\n+\t\t\t\t\t\tif (id === null) return [null, null];\n+\t\t\t\t\t\tconst node = get(id),\n+\t\t\t\t\t\t\tsize = count(node.left);\n+\t\t\t\t\t\tif (at <= size) {\n+\t\t\t\t\t\t\tconst [left, middle] = split(node.left, at);\n+\t\t\t\t\t\t\tconst right = join(middle, id, node.right);\n+\t\t\t\t\t\t\tparent(left, null);\n+\t\t\t\t\t\t\tparent(right, null);\n+\t\t\t\t\t\t\treturn [left, right];\n+\t\t\t\t\t\t}\n+\t\t\t\t\t\tconst [middle, right] = split(node.right, at - size - 1);\n+\t\t\t\t\t\tconst left = join(node.left, id, middle);\n+\t\t\t\t\t\tparent(left, null);\n+\t\t\t\t\t\tparent(right, null);\n+\t\t\t\t\t\treturn [left, right];\n+\t\t\t\t\t};\n+\t\t\t\t\tconst merge = (left: Page, right: Page): Page => {\n+\t\t\t\t\t\tif (left === null) {\n+\t\t\t\t\t\t\tparent(right, null);\n+\t\t\t\t\t\t\treturn right;\n+\t\t\t\t\t\t}\n+\t\t\t\t\t\tif (right === null) {\n+\t\t\t\t\t\t\tparent(left, null);\n+\t\t\t\t\t\t\treturn left;\n+\t\t\t\t\t\t}\n+\t\t\t\t\t\tconst [pivot, rest] = split(right, 1);\n+\t\t\t\t\t\treturn join(left, pivot!, rest);\n+\t\t\t\t\t};\n+\t\t\t\t\tif (length && from !== to) {\n+\t\t\t\t\t\tconst [left, tail] = split(root, from);\n+\t\t\t\t\t\tconst [moved, right] = split(tail, length);\n+\t\t\t\t\t\tconst remaining = merge(left, right);\n+\t\t\t\t\t\tconst [before, after] = split(remaining, to);\n+\t\t\t\t\t\troot = merge(merge(before, moved), after);\n+\t\t\t\t\t\tparent(root, null);\n+\t\t\t\t\t}\n+\t\t\t\t} else if (operation.kind === \"visibility\") {\n+\t\t\t\t\tlet node = get(operation.id);\n+\t\t\t\t\tif (\n+\t\t\t\t\t\t!node ||\n+\t\t\t\t\t\t!Number.isInteger(operation.delta) ||\n+\t\t\t\t\t\tMath.abs(operation.delta) !== 1 ||\n+\t\t\t\t\t\tnode.deletions + operation.delta < 0\n+\t\t\t\t\t)\n+\t\t\t\t\t\tthrow Error(\"invalid visibility\");\n+\t\t\t\t\tsave({ ...node, deletions: node.deletions + operation.delta });\n+\t\t\t\t\twhile (node.parent !== null) {\n+\t\t\t\t\t\tnode = get(node.parent);\n+\t\t\t\t\t\tsave(node);\n+\t\t\t\t\t}\n+\t\t\t\t} else throw Error(\"unknown operation\");\n+\t\t\t}\n+\t\t\t// Keep locator staging bounded even if a valid split/merge changes more\n+\t\t\t// than one batch. The enclosing transaction commits all batches together.\n+\t\t\tlet batch: Array<readonly [number, SequenceNode]> = [];\n+\t\t\tfor (const entry of changed) {\n+\t\t\t\tbatch.push(entry);\n+\t\t\t\tif (batch.length === 256) {\n+\t\t\t\t\tmap = this.#locator.setMany(map, batch);\n+\t\t\t\t\tbatch = [];\n+\t\t\t\t}\n+\t\t\t}\n+\t\t\tmap = this.#locator.setMany(map, batch);\n+\t\t\treturn { map, root, next };\n+\t\t});\n+\t}\n+\t*range(\n+\t\tstate: SequenceState,\n+\t\tfrom: number,\n+\t\tto: number,\n+\t\tvisibleOnly = true,\n+\t): IterableIterator<{ id: number; text: string }> {\n+\t\tthis.#state(state);\n+\t\tif (!safe(from) || !safe(to) || from > to) throw Error(\"invalid sequence range\");\n+\t\tconst visit = function* (\n+\t\t\tseq: ProjectionSequence,\n+\t\t\tid: Page,\n+\t\t\toffset: number,\n+\t\t): IterableIterator<{ id: number; text: string }> {\n+\t\t\tif (id === null) return;\n+\t\t\tconst node = seq.get(state.map, id);\n+\t\t\tif ((visibleOnly && !node.visible) || offset >= to || offset + node.count <= from) return;\n+\t\t\tconst leftCount = node.left === null ? 0 : seq.get(state.map, node.left).count;\n+\t\t\tyield* visit(seq, node.left, offset);\n+\t\t\tconst at = offset + leftCount;\n+\t\t\tif (at >= from && at < to && (!visibleOnly || node.deletions === 0)) yield { id: node.id, text: node.text };\n+\t\t\tyield* visit(seq, node.right, at + 1);\n+\t\t};\n+\t\tyield* visit(this, state.root, 0);\n+\t}\n+\t*values(state: SequenceState, visibleOnly = true): IterableIterator<{ id: number; text: string }> {\n+\t\tthis.#state(state);\n+\t\tconst visit = function* (seq: ProjectionSequence, id: Page): IterableIterator<{ id: number; text: string }> {\n+\t\t\tif (id === null) return;\n+\t\t\tconst node = seq.get(state.map, id);\n+\t\t\tif (visibleOnly && node.visible === 0) return;\n+\t\t\tyield* visit(seq, node.left);\n+\t\t\tif (!visibleOnly || node.deletions === 0) yield { id: node.id, text: node.text };\n+\t\t\tyield* visit(seq, node.right);\n+\t\t};\n+\t\tyield* visit(this, state.root);\n+\t}\n+}"}],"unmatched":[]}