{"draft":null,"from":73,"to":318,"files":[{"path":"src/engine/relocation-scratch.ts","added":144,"removed":0,"diff":"--- /dev/null\n+++ b/src/engine/relocation-scratch.ts\n@@ -0,0 +1,144 @@\n+import type { Graph, RelocationInfo } from \"./graph.ts\";\n+import { isStartNode } from \"./keys.ts\";\n+import { OrderedIndex, orderedChildKey } from \"./ordered-index.ts\";\n+import type { GraphStateStorage } from \"./state-source.ts\";\n+import type { AtomId, LineId, NodeId } from \"./types.ts\";\n+\n+/** Scope-local relocation state. The caller must consume this handle inside\n+ * withProjectionScratch; selected moves and cycle bookkeeping live on disk. */\n+export function resolveScratchMoves(\n+\tgraph: Graph,\n+\thas: (id: AtomId) => boolean,\n+\tscratch: GraphStateStorage,\n+): { selected(line: LineId): RelocationInfo | undefined; parent(line: LineId): NodeId } {\n+\tconst selected = (line: LineId) => scratch.read(\"selected\", line) as RelocationInfo | undefined;\n+\tconst identity = (move: RelocationInfo) => JSON.stringify([move.atom, move.k]);\n+\tconst stronger = (a: RelocationInfo, b: RelocationInfo) => graph.comparePriority(a.atom, b.atom) > 0;\n+\tconst pick = (line: LineId): RelocationInfo | undefined => {\n+\t\tlet best: RelocationInfo | undefined;\n+\t\tfor (const move of graph.eachRelocationOf(line)) {\n+\t\t\tif (has(move.atom) && !scratch.read(\"ignored\", identity(move)) && (!best || stronger(move, best))) best = move;\n+\t\t}\n+\t\treturn best;\n+\t};\n+\tlet work = 0;\n+\tfor (const id of graph.movers()) {\n+\t\tif (!has(id)) continue;\n+\t\tfor (const move of graph.atom(id).moves!) {\n+\t\t\tif (scratch.read(\"seen\", move.line)) continue;\n+\t\t\tscratch.write(\"seen\", move.line, true);\n+\t\t\tscratch.write(\"work\", String(work++), move.line);\n+\t\t\tconst best = pick(move.line);\n+\t\t\tif (best) scratch.write(\"selected\", move.line, best);\n+\t\t}\n+\t}\n+\tconst parent = (line: LineId): NodeId => {\n+\t\tconst node = selected(line)?.parent ?? graph.lineParent(line);\n+\t\tif (node === undefined) throw new Error(`missing relocation ancestry for ${line}`);\n+\t\treturn node;\n+\t};\n+\tconst terminal = (node: NodeId | null): boolean =>\n+\t\tnode === null || isStartNode(node) || !!scratch.read(\"reached\", node);\n+\tconst step = (node: NodeId | null): NodeId | null => (terminal(node) ? null : parent(node!));\n+\tfor (let item = 0; item < work; item++) {\n+\t\tconst line = scratch.read(\"work\", String(item)) as LineId;\n+\t\tfor (;;) {\n+\t\t\t// Floyd's walk finds the exact cycle entry without a retained path.\n+\t\t\tlet slow: NodeId | null = line;\n+\t\t\tlet fast: NodeId | null = line;\n+\t\t\tlet meeting: NodeId | null = null;\n+\t\t\tfor (;;) {\n+\t\t\t\tslow = step(slow);\n+\t\t\t\tfast = step(step(fast));\n+\t\t\t\tif (terminal(slow) || terminal(fast)) break;\n+\t\t\t\tif (slow === fast) {\n+\t\t\t\t\tmeeting = slow;\n+\t\t\t\t\tbreak;\n+\t\t\t\t}\n+\t\t\t}\n+\t\t\tif (meeting === null) {\n+\t\t\t\tlet current: NodeId = line;\n+\t\t\t\twhile (!terminal(current)) {\n+\t\t\t\t\tconst next = parent(current);\n+\t\t\t\t\tscratch.write(\"reached\", current, true);\n+\t\t\t\t\tcurrent = next;\n+\t\t\t\t}\n+\t\t\t\tbreak;\n+\t\t\t}\n+\t\t\tlet entry: NodeId | null = line;\n+\t\t\twhile (entry !== meeting) {\n+\t\t\t\tentry = step(entry);\n+\t\t\t\tmeeting = step(meeting);\n+\t\t\t}\n+\t\t\tlet current = entry!;\n+\t\t\tlet weakest: RelocationInfo | undefined;\n+\t\t\tdo {\n+\t\t\t\tconst move = selected(current);\n+\t\t\t\tif (move && (!weakest || stronger(weakest, move))) weakest = move;\n+\t\t\t\tcurrent = parent(current);\n+\t\t\t} while (current !== entry);\n+\t\t\tif (!weakest) throw new Error(\"canonical ancestry unexpectedly cycles\");\n+\t\t\tscratch.write(\"ignored\", identity(weakest), true);\n+\t\t\tconst next = pick(weakest.line);\n+\t\t\tif (next) scratch.write(\"selected\", weakest.line, next);\n+\t\t\telse scratch.remove(\"selected\", weakest.line);\n+\t\t}\n+\t}\n+\treturn { selected, parent };\n+}\n+\n+/** Consume entirely within the projection scope. Traversal retains one line;\n+ * both priority ordering and parent links are held in rollback-only storage. */\n+export function* scratchLayoutNodes(\n+\tgraph: Graph,\n+\thas: (id: AtomId) => boolean,\n+\tscratch: GraphStateStorage,\n+\tstart: NodeId,\n+): IterableIterator<number> {\n+\tconst moves = resolveScratchMoves(graph, has, scratch);\n+\tfor (let n = 0; n < graph.lineCount; n++) {\n+\t\tif (!has(graph.idOfSeq(graph.writerSeqOf(n)))) continue;\n+\t\tconst line = graph.nodeOf(n);\n+\t\tconst move = moves.selected(line);\n+\t\tconst seq = move ? graph.atom(move.atom).seq : graph.writerSeqOf(n);\n+\t\tconst key = orderedChildKey({\n+\t\t\tn,\n+\t\t\treplaces: move ? 0 : Number(graph.replacesN(n)),\n+\t\t\tlamport: graph.lamportOfSeq(seq),\n+\t\t\tseq,\n+\t\t\tk: move ? move.k : graph.kOf(n),\n+\t\t});\n+\t\tconst parent = moves.parent(line);\n+\t\tscratch.write(\"layout-parent\", line, { parent, key });\n+\t\tnew OrderedIndex(scratch, \"layout-children\", parent).insert(key, n);\n+\t}\n+\tconst first = (parent: NodeId, after?: string): number | undefined => {\n+\t\tfor (const n of new OrderedIndex(scratch, \"layout-children\", parent).values(after)) return n;\n+\t\treturn undefined;\n+\t};\n+\tlet n = first(start);\n+\twhile (n !== undefined) {\n+\t\tyield n;\n+\t\tconst line = graph.nodeOf(n);\n+\t\tconst child = first(line);\n+\t\tif (child !== undefined) {\n+\t\t\tn = child;\n+\t\t\tcontinue;\n+\t\t}\n+\t\tlet previous = line;\n+\t\tfor (;;) {\n+\t\t\tconst row = scratch.read(\"layout-parent\", previous) as { parent: NodeId; key: string } | undefined;\n+\t\t\tif (!row) throw new Error(\"missing scratch layout ancestry\");\n+\t\t\tconst sibling = first(row.parent, row.key);\n+\t\t\tif (sibling !== undefined) {\n+\t\t\t\tn = sibling;\n+\t\t\t\tbreak;\n+\t\t\t}\n+\t\t\tif (row.parent === start) {\n+\t\t\t\tn = undefined;\n+\t\t\t\tbreak;\n+\t\t\t}\n+\t\t\tprevious = row.parent;\n+\t\t}\n+\t}\n+}"}],"unmatched":[]}