{"version":131,"agent":"codex-capacity-chair","message":"Bound persisted no-move layout with ordered successor traversal","time":1791841517646,"timeIso":"2026-10-12T21:45:17.646Z","changes":10,"change":1284,"files":["src/engine/graph.ts","src/engine/ordered-index.ts","src/engine/test/ordered-index.test.ts","src/engine/test/state-source.test.ts","src/engine/view.ts"],"draft":null,"diff":[{"path":"src/engine/graph.ts","added":58,"removed":0,"diff":"--- a/src/engine/graph.ts\n+++ b/src/engine/graph.ts\n@@ -15,6 +15,7 @@\n \tseqOf,\n \tstartNode,\n } from \"./keys.ts\";\n+import { OrderedIndex, orderedChildKey } from \"./ordered-index.ts\";\n import {\n \ttype GraphStateStorage,\n \ttype StateMap,\n@@ -631,6 +632,46 @@\n \t\telse yield* listOf(this.#runStarts.get(code));\n \t}\n \n+\tget hasOrderedChildren(): boolean {\n+\t\treturn this.#storage !== undefined;\n+\t}\n+\t#childPriority(n: number): string {\n+\t\tconst seq = this.writerSeqOf(n);\n+\t\treturn orderedChildKey({\n+\t\t\tn,\n+\t\t\treplaces: this.replacesN(n) ? 1 : 0,\n+\t\t\tlamport: this.lamportOfSeq(seq),\n+\t\t\tseq,\n+\t\t\tk: this.kOf(n),\n+\t\t});\n+\t}\n+\t/** Persistent layout order with a strict successor cursor; moves are separate. */\n+\t*orderedChildrenN(code: number, after?: number): IterableIterator<number> {\n+\t\tif (!this.#storage) throw new Error(\"ordered children require indexed storage\");\n+\t\tconst index = new OrderedIndex(this.#storage, \"ordered-children\", String(code));\n+\t\tif (this.#storage.read(\"ordered-children\", \"format\") !== 1)\n+\t\t\tthrow new Error(\"ordered children require fresh adoption\");\n+\t\tif (index.size !== this.#runStartPages!.length(String(code))) throw new Error(\"ordered child index count mismatch\");\n+\t\tconst cursor = after === undefined ? undefined : this.#childPriority(after);\n+\t\tlet implicit: number | undefined;\n+\t\tif (\n+\t\t\tcode >= 0 &&\n+\t\t\tcode + 1 < this.#lineTotal &&\n+\t\t\tthis.#lineAtom.at(code + 1) === this.#lineAtom.at(code) &&\n+\t\t\tthis.#lineParent.at(code + 1) === code\n+\t\t)\n+\t\t\timplicit = code + 1;\n+\t\tif (implicit !== undefined && cursor !== undefined && this.#childPriority(implicit) <= cursor) implicit = undefined;\n+\t\tfor (const n of index.values(cursor)) {\n+\t\t\tif (implicit !== undefined && this.#childPriority(implicit) < this.#childPriority(n)) {\n+\t\t\t\tyield implicit;\n+\t\t\t\timplicit = undefined;\n+\t\t\t}\n+\t\t\tyield n;\n+\t\t}\n+\t\tif (implicit !== undefined) yield implicit;\n+\t}\n+\n \treplacesN(n: number): boolean {\n \t\treturn this.#replacing.at(n) === 1;\n \t}\n@@ -904,6 +945,11 @@\n \t}\n \n \t#add(atom: Atom): void {\n+\t\tif (this.#storage && this.#storage.read(\"ordered-children\", \"format\") !== 1) {\n+\t\t\tif (this.#atoms.length !== 0) throw new Error(\"ordered children require fresh adoption\");\n+\t\t\tthis.#storage.write(\"ordered-children\", \"format\", 1);\n+\t\t}\n+\n \t\tvalidateShape(atom);\n \t\tconst seq = this.#atoms.length;\n \t\tconst bad = (why: string): never => {\n@@ -1001,6 +1047,18 @@\n \t\t\tconst file = this.#pathOf(run.file);\n \t\t\tlet parent = isStartNode(run.parent) ? -1 - file : this.#lineN(run.parent);\n \t\t\tconst first = this.#lineTotal;\n+\t\t\tif (this.#storage)\n+\t\t\t\tnew OrderedIndex(this.#storage, \"ordered-children\", String(parent)).insert(\n+\t\t\t\t\torderedChildKey({\n+\t\t\t\t\t\tn: first,\n+\t\t\t\t\t\treplaces: deletes?.has(run.parent) ? 1 : 0,\n+\t\t\t\t\t\tlamport: lam,\n+\t\t\t\t\t\tseq,\n+\t\t\t\t\t\tk: first - this.#lineBase[seq]!,\n+\t\t\t\t\t}),\n+\t\t\t\t\tfirst,\n+\t\t\t\t);\n+\n \t\t\tif (this.#runStartPages) this.#runStartPages.append(String(parent), first);\n \t\t\telse pushOne(this.#runStarts, parent, first);\n \t\t\tfor (let i = 0; i < run.lines.length; i++) {"},{"path":"src/engine/ordered-index.ts","added":152,"removed":0,"diff":"--- /dev/null\n+++ b/src/engine/ordered-index.ts\n@@ -0,0 +1,152 @@\n+import type { GraphStateStorage } from \"./state-source.ts\";\n+\n+const WIDTH = 57;\n+type Leaf = { prefix: string; value: number };\n+type Branch = { prefix: string; children: (number | null)[] };\n+type Node = Leaf | Branch;\n+const safe = (value: unknown): value is number => Number.isSafeInteger(value) && Number(value) >= 0;\n+\n+/** Exact existing layout priority, encoded for ascending lexical traversal. */\n+export function orderedChildKey(child: {\n+\treplaces: number;\n+\tlamport: number;\n+\tseq: number;\n+\tk: number;\n+\tn: number;\n+}): string {\n+\tif (![child.lamport, child.seq, child.k, child.n].every(safe) || ![0, 1].includes(child.replaces))\n+\t\tthrow new Error(\"invalid ordered child priority\");\n+\tconst hex = (n: number) => n.toString(16).padStart(14, \"0\");\n+\treturn `${1 - child.replaces}${hex(Number.MAX_SAFE_INTEGER - child.lamport)}${hex(Number.MAX_SAFE_INTEGER - child.seq)}${hex(child.k)}${hex(child.n)}`;\n+}\n+\n+/** Compressed radix tree with at most 16 references per node and 57 levels.\n+ * No collection scan or history-sized sibling array is needed. Values are\n+ * immutable numeric identities; the caller owns namespace isolation. */\n+export class OrderedIndex {\n+\treadonly #storage: GraphStateStorage;\n+\treadonly #collection: string;\n+\treadonly #group: string;\n+\tconstructor(storage: GraphStateStorage, collection: string, group: string) {\n+\t\tthis.#storage = storage;\n+\t\tthis.#collection = collection;\n+\t\tthis.#group = group;\n+\t}\n+\t#key(id: number | \"meta\"): string {\n+\t\treturn JSON.stringify([this.#group, id]);\n+\t}\n+\t#next(): number | undefined {\n+\t\tconst meta = this.#storage.read(this.#collection, this.#key(\"meta\")) as\n+\t\t\t| { format?: unknown; next?: unknown; count?: unknown }\n+\t\t\t| undefined;\n+\t\tif (meta === undefined) {\n+\t\t\tif (this.#storage.read(this.#collection, this.#key(0)) !== undefined)\n+\t\t\t\tthrow new Error(\"missing ordered index metadata\");\n+\t\t\treturn undefined;\n+\t\t}\n+\t\tif (\n+\t\t\tmeta?.format !== 1 ||\n+\t\t\t!safe(meta.next) ||\n+\t\t\tmeta.next < 1 ||\n+\t\t\t!safe(meta.count) ||\n+\t\t\tmeta.count < 1 ||\n+\t\t\tmeta.count > meta.next\n+\t\t)\n+\t\t\tthrow new Error(\"invalid ordered index metadata\");\n+\t\treturn meta.next;\n+\t}\n+\tget size(): number {\n+\t\tif (this.#next() === undefined) return 0;\n+\t\treturn (this.#storage.read(this.#collection, this.#key(\"meta\")) as { count: number }).count;\n+\t}\n+\t#write(id: number, node: Node): void {\n+\t\tthis.#storage.write(this.#collection, this.#key(id), node);\n+\t}\n+\t#read(id: number, expected: string): Node {\n+\t\tconst node = this.#storage.read(this.#collection, this.#key(id)) as Node | undefined;\n+\t\tif (\n+\t\t\t!node ||\n+\t\t\ttypeof node.prefix !== \"string\" ||\n+\t\t\t!/^[0-9a-f]*$/.test(node.prefix) ||\n+\t\t\tnode.prefix.length > WIDTH ||\n+\t\t\t!node.prefix.startsWith(expected)\n+\t\t)\n+\t\t\tthrow new Error(\"invalid ordered index node\");\n+\t\tif (\"value\" in node) {\n+\t\t\tif (node.prefix.length !== WIDTH || !safe(node.value) || \"children\" in node)\n+\t\t\t\tthrow new Error(\"invalid ordered index leaf\");\n+\t\t} else if (\n+\t\t\tnode.prefix.length >= WIDTH ||\n+\t\t\t!Array.isArray(node.children) ||\n+\t\t\tnode.children.length !== 16 ||\n+\t\t\t!node.children.every((id) => id === null || safe(id)) ||\n+\t\t\tnode.children.filter((id) => id !== null).length < 2\n+\t\t) {\n+\t\t\tthrow new Error(\"invalid ordered index branch\");\n+\t\t}\n+\t\treturn node;\n+\t}\n+\t/** Nested host transactions must roll back all allocation, split and link writes. */\n+\tinsert(key: string, value: number): void {\n+\t\tif (key.length !== WIDTH || !/^[0-9a-f]+$/.test(key) || !safe(value))\n+\t\t\tthrow new Error(\"invalid ordered index entry\");\n+\t\tthis.#storage.transaction(() => {\n+\t\t\tlet next = this.#next();\n+\t\t\tlet count = this.size;\n+\t\t\tif (next === undefined) {\n+\t\t\t\tthis.#write(0, { prefix: key, value });\n+\t\t\t\tthis.#storage.write(this.#collection, this.#key(\"meta\"), { format: 1, next: 1, count: 1 });\n+\t\t\t\treturn;\n+\t\t\t}\n+\t\t\tconst allocate = (node: Node): number => {\n+\t\t\t\tif (next! >= Number.MAX_SAFE_INTEGER) throw new Error(\"ordered index identity exhausted\");\n+\t\t\t\tconst id = next!++;\n+\t\t\t\tthis.#write(id, node);\n+\t\t\t\treturn id;\n+\t\t\t};\n+\t\t\tconst add = (id: number, expected: string): void => {\n+\t\t\t\tconst node = this.#read(id, expected);\n+\t\t\t\tlet common = 0;\n+\t\t\t\twhile (common < node.prefix.length && node.prefix[common] === key[common]) common++;\n+\t\t\t\tif (common < node.prefix.length) {\n+\t\t\t\t\tcount++;\n+\t\t\t\t\tconst children: (number | null)[] = Array(16).fill(null);\n+\t\t\t\t\tchildren[parseInt(node.prefix[common]!, 16)] = allocate(node);\n+\t\t\t\t\tchildren[parseInt(key[common]!, 16)] = allocate({ prefix: key, value });\n+\t\t\t\t\tthis.#write(id, { prefix: key.slice(0, common), children });\n+\t\t\t\t} else if (\"value\" in node) {\n+\t\t\t\t\tif (node.value !== value) throw new Error(\"ordered index key reused\");\n+\t\t\t\t} else {\n+\t\t\t\t\tconst digit = parseInt(key[common]!, 16);\n+\t\t\t\t\tconst child = node.children[digit]!;\n+\t\t\t\t\tif (child === null) {\n+\t\t\t\t\t\tcount++;\n+\t\t\t\t\t\tnode.children[digit] = allocate({ prefix: key, value });\n+\t\t\t\t\t\tthis.#write(id, node);\n+\t\t\t\t\t} else add(child, node.prefix + key[common]);\n+\t\t\t\t}\n+\t\t\t};\n+\t\t\tadd(0, \"\");\n+\t\t\tthis.#storage.write(this.#collection, this.#key(\"meta\"), { format: 1, next, count });\n+\t\t});\n+\t}\n+\t*values(after?: string): IterableIterator<number> {\n+\t\tif (after !== undefined && (after.length !== WIDTH || !/^[0-9a-f]+$/.test(after)))\n+\t\t\tthrow new Error(\"invalid ordered index cursor\");\n+\t\tif (this.#next() === undefined) return;\n+\t\tconst walk = function* (this: OrderedIndex, id: number, expected: string): IterableIterator<number> {\n+\t\t\tif (after !== undefined && expected < after.slice(0, expected.length)) return;\n+\t\t\tconst node = this.#read(id, expected);\n+\t\t\tif (after !== undefined && node.prefix < after.slice(0, node.prefix.length)) return;\n+\t\t\tif (\"value\" in node) {\n+\t\t\t\tif (after === undefined || node.prefix > after) yield node.value;\n+\t\t\t\treturn;\n+\t\t\t}\n+\t\t\tfor (let digit = 0; digit < 16; digit++) {\n+\t\t\t\tconst child = node.children[digit]!;\n+\t\t\t\tif (child !== null) yield* walk.call(this, child, node.prefix + digit.toString(16));\n+\t\t\t}\n+\t\t};\n+\t\tyield* walk.call(this, 0, \"\");\n+\t}\n+}"},{"path":"src/engine/test/ordered-index.test.ts","added":101,"removed":0,"diff":"--- /dev/null\n+++ b/src/engine/test/ordered-index.test.ts\n@@ -0,0 +1,101 @@\n+import assert from \"node:assert/strict\";\n+import { test } from \"node:test\";\n+import { OrderedIndex, orderedChildKey } from \"../ordered-index.ts\";\n+import { SqlState } from \"./storage-helper.ts\";\n+\n+test(\"ordered children retain priority across storage reopen and transactional split failures\", () => {\n+\tconst storage = new SqlState();\n+\ttry {\n+\t\tconst children = Array.from({ length: 513 }, (_, n) => ({\n+\t\t\tn,\n+\t\t\treplaces: n % 2,\n+\t\t\tlamport: n % 7,\n+\t\t\tseq: Math.floor(n / 8),\n+\t\t\tk: n % 8,\n+\t\t}));\n+\t\tconst expected = [...children]\n+\t\t\t.sort((a, b) => b.replaces - a.replaces || b.lamport - a.lamport || b.seq - a.seq || a.k - b.k || a.n - b.n)\n+\t\t\t.map((c) => c.n);\n+\t\tconst index = new OrderedIndex(storage, \"ordered\", \"parent\");\n+\t\tfor (const child of [...children].reverse()) index.insert(orderedChildKey(child), child.n);\n+\t\tassert.deepEqual([...new OrderedIndex(storage, \"ordered\", \"parent\").values()], expected);\n+\t\tstorage.reads = 0;\n+\t\tassert.equal(new OrderedIndex(storage, \"ordered\", \"parent\").values().next().value, expected[0]);\n+\t\tassert.ok(storage.reads <= 59);\n+\t\tconst before = storage.db.prepare(\"SELECT * FROM state ORDER BY collection,key\").all();\n+\t\tconst child = { n: 514, replaces: 1, lamport: Number.MAX_SAFE_INTEGER, seq: Number.MAX_SAFE_INTEGER, k: 0 };\n+\t\tlet failed = 0,\n+\t\t\tcomplete = false;\n+\t\tfor (let offset = 1; offset < 12; offset++) {\n+\t\t\tstorage.failWrite = storage.writes + offset;\n+\t\t\ttry {\n+\t\t\t\tindex.insert(orderedChildKey(child), child.n);\n+\t\t\t\tcomplete = true;\n+\t\t\t\tbreak;\n+\t\t\t} catch (error) {\n+\t\t\t\tassert.match(String(error), /injected/);\n+\t\t\t\tfailed++;\n+\t\t\t\tassert.deepEqual(storage.db.prepare(\"SELECT * FROM state ORDER BY collection,key\").all(), before);\n+\t\t\t}\n+\t\t}\n+\t\tassert.ok(complete && failed >= 3);\n+\t\tstorage.failWrite = Infinity;\n+\t\tassert.deepEqual([...new OrderedIndex(storage, \"ordered\", \"parent\").values()], [514, ...expected]);\n+\t\tindex.insert(orderedChildKey(child), child.n);\n+\t\tassert.deepEqual([...index.values()], [514, ...expected]);\n+\t\tassert.throws(() => index.insert(orderedChildKey(child), 999), /reused/);\n+\t\tassert.deepEqual([...new OrderedIndex(storage, \"ordered\", \"other\").values()], []);\n+\t} finally {\n+\t\tstorage.db.close();\n+\t}\n+});\n+\n+test(\"ordered index refuses malformed state and cycles instead of following them\", () => {\n+\tconst storage = new SqlState();\n+\ttry {\n+\t\tconst index = new OrderedIndex(storage, \"ordered\", \"parent\");\n+\t\tassert.throws(() => orderedChildKey({ n: 0, replaces: 2, lamport: 0, seq: 0, k: 0 }), /priority/);\n+\t\tassert.throws(() => index.insert(\"bad\", 0), /entry/);\n+\t\tindex.insert(\"0\".repeat(57), 0);\n+\t\tindex.insert(\"1\".repeat(57), 1);\n+\t\tconst node = storage.read(\"ordered\", JSON.stringify([\"parent\", 0])) as {\n+\t\t\tprefix: string;\n+\t\t\tchildren: (number | null)[];\n+\t\t};\n+\t\tnode.children[0] = 0;\n+\t\tstorage.write(\"ordered\", JSON.stringify([\"parent\", 0]), node);\n+\t\tassert.throws(() => [...index.values()], /node/);\n+\t} finally {\n+\t\tstorage.db.close();\n+\t}\n+});\n+\n+test(\"ordered successor cursors skip earlier subtrees and retain tied-priority order\", () => {\n+\tconst storage = new SqlState();\n+\ttry {\n+\t\tconst index = new OrderedIndex(storage, \"ordered\", \"parent\");\n+\t\tconst children = Array.from({ length: 1025 }, (_, n) => ({\n+\t\t\tn,\n+\t\t\treplaces: 0,\n+\t\t\tlamport: 4,\n+\t\t\tseq: Math.floor(n / 8),\n+\t\t\tk: n % 8,\n+\t\t}));\n+\t\tfor (const child of children) index.insert(orderedChildKey(child), child.n);\n+\t\tconst ordered = [...children].sort((a, b) => orderedChildKey(a).localeCompare(orderedChildKey(b)));\n+\t\tfor (const position of [0, 1, 255, 511, 1023, 1024]) {\n+\t\t\tconst cursor = orderedChildKey(ordered[position]!);\n+\t\t\tstorage.reads = 0;\n+\t\t\tconst first = new OrderedIndex(storage, \"ordered\", \"parent\").values(cursor).next().value;\n+\t\t\tassert.equal(first, ordered[position + 1]?.n);\n+\t\t\tassert.ok(storage.reads < 60, \"first successor must skip earlier history\");\n+\t\t\tassert.deepEqual(\n+\t\t\t\t[...index.values(cursor)],\n+\t\t\t\tordered.slice(position + 1).map((child) => child.n),\n+\t\t\t);\n+\t\t}\n+\t\tassert.throws(() => [...index.values(\"bad\")], /cursor/);\n+\t} finally {\n+\t\tstorage.db.close();\n+\t}\n+});"},{"path":"src/engine/test/state-source.test.ts","added":70,"removed":0,"diff":"--- a/src/engine/test/state-source.test.ts\n+++ b/src/engine/test/state-source.test.ts\n@@ -419,3 +419,73 @@\n \t\tstorage.db.close();\n \t}\n });\n+\n+test(\"persistent layout uses ordered successors without retaining deep or wide history frontiers\", () => {\n+\tfor (const shape of [\"deep\", \"wide\"] as const) {\n+\t\tconst storage = new SqlState();\n+\t\ttry {\n+\t\t\tconst oracle = Repo.init({\n+\t\t\t\t\"a.txt\": shape === \"deep\" ? Array.from({ length: 1024 }, (_, n) => `line ${n}\\n`).join(\"\") : \"base\\n\",\n+\t\t\t});\n+\t\t\tif (shape === \"wide\") {\n+\t\t\t\tconst draft = oracle.draft({ edits: [{ file: \"a.txt\", old: \"base\", new: \"branch\" }] });\n+\t\t\t\tfor (let seq = 1; seq <= 256; seq++) {\n+\t\t\t\t\tconst atom = { ...draft, id: `a${seq}`, seq };\n+\t\t\t\t\toracle.graph.add(atom);\n+\t\t\t\t\toracle.shared.add(atom.id);\n+\t\t\t\t}\n+\t\t\t}\n+\t\t\tconst before = oracle.view().text(\"a.txt\");\n+\t\t\toracle.edit({ edits: [{ file: \"a.txt\", old: before, new: \"live\\n\" }] });\n+\t\t\tconst graph = new Graph({ storage });\n+\t\t\tfor (const id of oracle.shared) graph.add(oracle.graph.atom(id));\n+\t\t\tconst reopened = new Graph({ storage });\n+\t\t\treopened.eachChildN = () => {\n+\t\t\t\tthrow Error(\"unordered child collection forbidden\");\n+\t\t\t};\n+\t\t\treopened.childrenN = () => {\n+\t\t\t\tthrow Error(\"materialized child collection forbidden\");\n+\t\t\t};\n+\t\t\tconst original = reopened.orderedChildrenN.bind(reopened);\n+\t\t\tlet active = 0,\n+\t\t\t\tmaximum = 0;\n+\t\t\treopened.orderedChildrenN = function* (parent: number, after?: number) {\n+\t\t\t\tactive++;\n+\t\t\t\tmaximum = Math.max(maximum, active);\n+\t\t\t\ttry {\n+\t\t\t\t\tyield* original(parent, after);\n+\t\t\t\t} finally {\n+\t\t\t\t\tactive--;\n+\t\t\t\t}\n+\t\t\t};\n+\t\t\tconst view = new View(reopened, new StableOverlaySet(oracle.shared));\n+\t\t\tassert.equal(view.text(\"a.txt\"), \"live\\n\");\n+\t\t\tassert.equal(active, 0);\n+\t\t\tassert.equal(maximum, 1, \"no ordered iterator retained per historical ancestor\");\n+\t\t\tassert.deepEqual(view.sequence(\"a.txt\"), oracle.view().sequence(\"a.txt\"));\n+\t\t} finally {\n+\t\t\tstorage.db.close();\n+\t\t}\n+\t}\n+});\n+\n+test(\"ordered layout refuses missing format or child metadata instead of dropping history\", () => {\n+\tconst storage = new SqlState();\n+\ttry {\n+\t\tconst oracle = Repo.init({ \"a.txt\": \"one\\n\" });\n+\t\tconst graph = new Graph({ storage });\n+\t\tgraph.add(oracle.graph.atom(\"a0\"));\n+\t\tstorage.remove(\"ordered-children\", \"format\");\n+\t\tassert.throws(() => new View(new Graph({ storage }), new StableOverlaySet([\"a0\"])).text(\"a.txt\"), /fresh adoption/);\n+\t\tassert.throws(\n+\t\t\t() => new Graph({ storage }).add(oracle.draft({ edits: [{ file: \"a.txt\", old: \"one\", new: \"two\" }] })),\n+\t\t\t/fresh adoption/,\n+\t\t);\n+\t\tstorage.write(\"ordered-children\", \"format\", 1);\n+\t\tconst parent = graph.startCodeOf(\"a.txt\")!;\n+\t\tstorage.remove(\"ordered-children\", JSON.stringify([String(parent), \"meta\"]));\n+\t\tassert.throws(() => [...graph.orderedChildrenN(parent)], /missing ordered index metadata/);\n+\t} finally {\n+\t\tstorage.db.close();\n+\t}\n+});"},{"path":"src/engine/view.ts","added":49,"removed":10,"diff":"--- a/src/engine/view.ts\n+++ b/src/engine/view.ts\n@@ -346,6 +346,53 @@\n \t\treturn out;\n \t}\n \n+\t/** Parent/successor traversal keeps no history-sized ancestry or sibling frontier. */\n+\t*#layoutNodes(start: number): IterableIterator<number> {\n+\t\tconst graph = this.graph;\n+\t\tif (graph.hasOrderedChildren && !this.#hasMoves) {\n+\t\t\tconst first = (parent: number, after?: number): number | undefined => {\n+\t\t\t\tfor (const n of graph.orderedChildrenN(parent, after)) if (this.#has(graph.writerSeqOf(n))) return n;\n+\t\t\t\treturn undefined;\n+\t\t\t};\n+\t\t\tlet n = first(start);\n+\t\t\twhile (n !== undefined) {\n+\t\t\t\tyield n;\n+\t\t\t\tconst child = first(n);\n+\t\t\t\tif (child !== undefined) {\n+\t\t\t\t\tn = child;\n+\t\t\t\t\tcontinue;\n+\t\t\t\t}\n+\t\t\t\tlet previous = n;\n+\t\t\t\tfor (;;) {\n+\t\t\t\t\tconst parent = graph.parentCodeOf(previous);\n+\t\t\t\t\tconst sibling = first(parent, previous);\n+\t\t\t\t\tif (sibling !== undefined) {\n+\t\t\t\t\t\tn = sibling;\n+\t\t\t\t\t\tbreak;\n+\t\t\t\t\t}\n+\t\t\t\t\tif (parent === start) {\n+\t\t\t\t\t\tn = undefined;\n+\t\t\t\t\t\tbreak;\n+\t\t\t\t\t}\n+\t\t\t\t\tif (parent < 0 || parent >= previous) throw new Error(\"invalid persistent layout ancestry\");\n+\t\t\t\t\tprevious = parent;\n+\t\t\t\t}\n+\t\t\t}\n+\t\t\treturn;\n+\t\t}\n+\t\tconst stack: number[] = [];\n+\t\tconst push = (parent: number): void => {\n+\t\t\tconst children = this.#children(parent);\n+\t\t\tfor (let i = children.length - 1; i >= 0; i--) stack.push(children[i]!.n);\n+\t\t};\n+\t\tpush(start);\n+\t\twhile (stack.length) {\n+\t\t\tconst n = stack.pop()!;\n+\t\t\tyield n;\n+\t\t\tpush(n);\n+\t\t}\n+\t}\n+\n \t/** Lay out one file: its line tree from the set's atoms, linearized. */\n \t#layout(path: string, complete: boolean): FileState {\n \t\tconst graph = this.graph;\n@@ -363,17 +410,10 @@\n \t\tconst seq: number[] = [];\n \t\tconst vis: number[] = [];\n \t\tconst deletedBy = new Map<number, readonly number[]>();\n-\t\t// Depth-first, iteratively (a file of n lines can be a chain n deep).\n-\t\tconst stack: number[] = [];\n-\t\tconst pushChildren = (code: number): void => {\n-\t\t\tconst list = this.#children(code);\n-\t\t\tfor (let i = list.length - 1; i >= 0; i--) stack.push(list[i]!.n);\n-\t\t};\n+\n \t\tconst start = graph.startCodeOf(path);\n-\t\tif (start !== undefined) pushChildren(start);\n \t\tconst priority = (a: number, b: number) => graph.lamportOfSeq(a) - graph.lamportOfSeq(b) || a - b;\n-\t\twhile (stack.length > 0) {\n-\t\t\tconst n = stack.pop()!;\n+\t\tfor (const n of start === undefined ? [] : this.#layoutNodes(start)) {\n \t\t\tlet deleted = false;\n \t\t\tconst by: number[] = [];\n \t\t\tfor (const deleter of graph.eachDeleterSeqOf(n)) {\n@@ -386,7 +426,6 @@\n \t\t\t\tif (complete) deletedBy.set(seq.length, by.length > 1 ? by.sort(priority) : by);\n \t\t\t} else vis.push(n);\n \t\t\tif (complete) seq.push(n);\n-\t\t\tpushChildren(n);\n \t\t}\n \t\tconst attrs: FileAttrs = {\n \t\t\texists: setters.get(\"exists\")?.value === \"1\","}],"ci":null}