{"draft":null,"from":161,"to":318,"files":[{"path":"src/engine/experimental/projection-sequence.ts","added":127,"removed":105,"diff":"--- a/src/engine/experimental/projection-sequence.ts\n+++ b/src/engine/experimental/projection-sequence.ts\n@@ -20,7 +20,7 @@\n \treadonly next: number;\n }\n export type SequenceOperation =\n-\t| { kind: \"insert\"; rank: number; text: string }\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@@ -132,13 +132,32 @@\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\tconst get = (id: number): SequenceNode => changed.get(id) ?? this.get(map, id);\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@@ -192,114 +211,117 @@\n \t\t\t};\n \t\t\tlet root = state.root,\n \t\t\t\tnext = state.next;\n-\t\t\tif (operation.kind === \"insert\") {\n-\t\t\t\tconst { rank, text } = operation;\n-\t\t\t\tif (!Number.isInteger(rank) || rank < 0 || rank > count(root) || next >= 2 ** 32) throw Error(\"invalid insert\");\n-\t\t\t\tconst added = next++;\n-\t\t\t\tconst insert = (id: Page, at: number, p: Page): number => {\n-\t\t\t\t\tif (id === null)\n-\t\t\t\t\t\treturn put({\n-\t\t\t\t\t\t\tid: added,\n-\t\t\t\t\t\t\tparent: p,\n-\t\t\t\t\t\t\tleft: null,\n-\t\t\t\t\t\t\tright: null,\n-\t\t\t\t\t\t\ttext,\n-\t\t\t\t\t\t\tdeletions: 0,\n-\t\t\t\t\t\t\theight: 1,\n-\t\t\t\t\t\t\tcount: 1,\n-\t\t\t\t\t\t\tvisible: 1,\n-\t\t\t\t\t\t\tfileStarts: text.startsWith(\"root:\") ? 1 : 0,\n-\t\t\t\t\t\t});\n-\t\t\t\t\tconst node = get(id),\n-\t\t\t\t\t\tleftCount = count(node.left);\n-\t\t\t\t\tif (at <= leftCount) save({ ...node, left: insert(node.left, at, id) });\n-\t\t\t\t\telse save({ ...node, right: insert(node.right, at - leftCount - 1, id) });\n-\t\t\t\t\treturn balance(id);\n-\t\t\t\t};\n-\t\t\t\troot = insert(root, rank, null);\n-\t\t\t\tparent(root, null);\n-\t\t\t} else if (operation.kind === \"move\") {\n-\t\t\t\tconst { from, length, to } = operation;\n-\t\t\t\tconst total = count(root);\n-\t\t\t\tif (\n-\t\t\t\t\t![from, length, to].every(Number.isSafeInteger) ||\n-\t\t\t\t\tfrom < 0 ||\n-\t\t\t\t\tlength < 0 ||\n-\t\t\t\t\tfrom + length > total ||\n-\t\t\t\t\tto < 0 ||\n-\t\t\t\t\tto > total - length\n-\t\t\t\t)\n-\t\t\t\t\tthrow Error(\"invalid move range\");\n-\t\t\t\t// Join two AVL trees around one existing stable marker.\n-\t\t\t\tconst join = (left: Page, pivot: number, right: Page): number => {\n-\t\t\t\t\tif (height(left) > height(right) + 1) {\n-\t\t\t\t\t\tconst node = get(left!);\n-\t\t\t\t\t\tsave({ ...node, right: join(node.right, pivot, right) });\n-\t\t\t\t\t\tconst result = balance(left!);\n-\t\t\t\t\t\tparent(result, null);\n-\t\t\t\t\t\treturn result;\n-\t\t\t\t\t}\n-\t\t\t\t\tif (height(right) > height(left) + 1) {\n-\t\t\t\t\t\tconst node = get(right!);\n-\t\t\t\t\t\tsave({ ...node, left: join(left, pivot, node.left) });\n-\t\t\t\t\t\tconst result = balance(right!);\n-\t\t\t\t\t\tparent(result, null);\n-\t\t\t\t\t\treturn result;\n-\t\t\t\t\t}\n-\t\t\t\t\treturn save({ ...get(pivot), left, right, parent: null });\n-\t\t\t\t};\n-\t\t\t\tconst split = (id: Page, at: number): [Page, Page] => {\n-\t\t\t\t\tif (id === null) return [null, null];\n-\t\t\t\t\tconst node = get(id),\n-\t\t\t\t\t\tsize = count(node.left);\n-\t\t\t\t\tif (at <= size) {\n-\t\t\t\t\t\tconst [left, middle] = split(node.left, at);\n-\t\t\t\t\t\tconst right = join(middle, id, node.right);\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\tconst [middle, right] = split(node.right, at - size - 1);\n-\t\t\t\t\tconst left = join(node.left, id, middle);\n-\t\t\t\t\tparent(left, null);\n-\t\t\t\t\tparent(right, null);\n-\t\t\t\t\treturn [left, right];\n-\t\t\t\t};\n-\t\t\t\tconst merge = (left: Page, right: Page): Page => {\n-\t\t\t\t\tif (left === null) {\n-\t\t\t\t\t\tparent(right, null);\n-\t\t\t\t\t\treturn right;\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\tif (right === null) {\n-\t\t\t\t\t\tparent(left, null);\n-\t\t\t\t\t\treturn left;\n-\t\t\t\t\t}\n-\t\t\t\t\tconst [pivot, rest] = split(right, 1);\n-\t\t\t\t\treturn join(left, pivot!, rest);\n-\t\t\t\t};\n-\t\t\t\tif (length && from !== to) {\n-\t\t\t\t\tconst [left, tail] = split(root, from);\n-\t\t\t\t\tconst [moved, right] = split(tail, length);\n-\t\t\t\t\tconst remaining = merge(left, right);\n-\t\t\t\t\tconst [before, after] = split(remaining, to);\n-\t\t\t\t\troot = merge(merge(before, moved), after);\n-\t\t\t\t\tparent(root, null);\n-\t\t\t\t}\n-\t\t\t} else if (operation.kind === \"visibility\") {\n-\t\t\t\tlet node = get(operation.id);\n-\t\t\t\tif (\n-\t\t\t\t\t!node ||\n-\t\t\t\t\t!Number.isInteger(operation.delta) ||\n-\t\t\t\t\tMath.abs(operation.delta) !== 1 ||\n-\t\t\t\t\tnode.deletions + operation.delta < 0\n-\t\t\t\t)\n-\t\t\t\t\tthrow Error(\"invalid visibility\");\n-\t\t\t\tsave({ ...node, deletions: node.deletions + operation.delta });\n-\t\t\t\twhile (node.parent !== null) {\n-\t\t\t\t\tnode = get(node.parent);\n-\t\t\t\t\tsave(node);\n-\t\t\t\t}\n-\t\t\t} else throw Error(\"unknown operation\");\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]> = [];"}],"unmatched":[]}