{"version":163,"agent":"codex-capacity-chair","message":"Integrate reviewed disk-backed operations, queue and bounded read caches","time":1791949753900,"timeIso":"2026-10-14T03:49:13.900Z","changes":23,"change":2408,"files":["server/src/decoded-frames.ts","server/src/small-frame-json.ts","server/test/decoded-frames-small.test.ts","server/test/project-object-disk-first.test.ts","server/test/projected-compare.test.ts","server/test/projected-diff.test.ts","server/test/projected-move-sync.test.ts","server/test/projected-move.test.ts","server/test/projected-queue.test.ts","server/test/projected-restore.test.ts","server/test/projected-sync.test.ts","server/test/small-frame-json.test.ts","src/engine/experimental/projection-reader.ts","src/engine/experimental/projection-sequence.ts","src/engine/test/projection-sequence.test.ts","worker/project/draft-diff.ts","worker/project/merge-identical.ts","worker/project/move.ts","worker/project/project.ts","worker/project/queue.ts","worker/project/restore.ts","worker/project/sync.ts","worker/project/version-log.ts"],"draft":null,"diff":[{"path":"server/src/decoded-frames.ts","added":8,"removed":3,"diff":"--- a/server/src/decoded-frames.ts\n+++ b/server/src/decoded-frames.ts\n@@ -2,6 +2,7 @@\n import type { WriterAdoptionFrame } from \"../../worker/project/project.ts\";\n import type { Entry } from \"../../worker/project/project-types.ts\";\n import type { SqliteLog, SqliteStorage } from \"./engine-project.ts\";\n+import { smallFrameJson } from \"./small-frame-json.ts\";\n import { StoredJson, storeJson } from \"./stored-json.ts\";\n import { storedJsonView } from \"./stored-json-view.ts\";\n import { SqliteWriterState, writerFrameChunksDigest } from \"./writer-host.ts\";\n@@ -26,7 +27,7 @@\n \treturn storage.transactionSync(() => {\n \t\tSqliteWriterState.initialize(storage);\n \t\tconst state = new SqliteWriterState(storage, namespace);\n-\t\t// Temporary nodes are immutable after parsing; cap retained reads for the whole page.\n+\t\t// Bound cached reads across all disk-backed frames in this page.\n \t\tconst reads = new CachedStateStorage(state, 64 * 1024, 256);\n \t\tif (\n \t\t\tstorage.sql.exec(\"SELECT 1 AS present FROM writer_state WHERE namespace = ? LIMIT 1\", namespace).toArray().length\n@@ -44,9 +45,13 @@\n \t\t\t\t\tthrow Error(\"decoded frame exceeds page byte bound\");\n \t\t\t\t}\n \t\t\t\tdigest = writerFrameChunksDigest(digest, seq, log.frameChunks(seq));\n-\t\t\t\tstoreJson(state, String(seq), log.frameChunks(seq));\n \t\t\t\tbytes += size;\n-\t\t\t\tyield { seq, bytes: size, entry: storedJsonView(new StoredJson(reads, String(seq))) as Entry };\n+\t\t\t\tif (size <= 4096) {\n+\t\t\t\t\tyield { seq, bytes: size, entry: smallFrameJson(log.frameChunks(seq)) as Entry };\n+\t\t\t\t} else {\n+\t\t\t\t\tstoreJson(state, String(seq), log.frameChunks(seq));\n+\t\t\t\t\tyield { seq, bytes: size, entry: storedJsonView(new StoredJson(reads, String(seq))) as Entry };\n+\t\t\t\t}\n \t\t\t}\n \t\t};\n \t\ttry {"},{"path":"server/src/small-frame-json.ts","added":15,"removed":0,"diff":"--- /dev/null\n+++ b/server/src/small-frame-json.ts\n@@ -0,0 +1,15 @@\n+import { jsonEvents } from \"./json-events.ts\";\n+\n+/** Decode a small canonical frame with the same validation as the streamed path. */\n+export function smallFrameJson(chunks: Iterable<string>): unknown {\n+\tlet text = \"\";\n+\tfor (const chunk of chunks) {\n+\t\tif (text.length + chunk.length > 4096) throw Error(\"small canonical frame exceeds bound\");\n+\t\ttext += chunk;\n+\t}\n+\t// Count UTF-8 after joining so a surrogate pair split across chunks counts once.\n+\tif (new TextEncoder().encode(text).byteLength > 4096) throw Error(\"small canonical frame exceeds bound\");\n+\t// Preserve depth, key and scalar limits; the fast path must not change acceptance.\n+\tfor (const event of jsonEvents([text])) void event;\n+\treturn JSON.parse(text);\n+}"},{"path":"server/test/decoded-frames-small.test.ts","added":72,"removed":0,"diff":"--- /dev/null\n+++ b/server/test/decoded-frames-small.test.ts\n@@ -0,0 +1,72 @@\n+import assert from \"node:assert/strict\";\n+import { test } from \"node:test\";\n+import { withDecodedFrames } from \"../src/decoded-frames.ts\";\n+import { logParts, SqliteLog } from \"../src/engine-project.ts\";\n+import { SqliteWriterState, writerFrameDigest } from \"../src/writer-host.ts\";\n+import { memoryStorage } from \"./helpers.ts\";\n+\n+test(\"decoded frame pages retain views through consumption and remove scratch on all exits\", (t) => {\n+\tconst storage = memoryStorage();\n+\tt.after(() => storage.db.close());\n+\tconst log = new SqliteLog(storage);\n+\tconst json = JSON.stringify({ type: \"join\", agent: \"alice\", version: 0 });\n+\tfor (const [part, text] of logParts(json).entries())\n+\t\tstorage.sql.exec(\"INSERT INTO log(seq,part,text) VALUES(?,?,?)\", 0, part, text);\n+\tSqliteWriterState.initialize(storage);\n+\tconst before = storage.db.prepare(\"SELECT * FROM writer_state\").all();\n+\tconst input = { from: 0, to: 1, maxEntries: 1, maxBytes: 1000, digest: \"\" };\n+\tconst result = withDecodedFrames(storage, \"decoded-page\", log, input, (frames) => {\n+\t\tconst frame = [...frames][0]!;\n+\t\tassert.ok(\"entry\" in frame);\n+\t\tassert.equal(frame.entry.type, \"join\");\n+\t\tassert.equal(\n+\t\t\tstorage.db.prepare(\"SELECT COUNT(*) AS n FROM writer_state WHERE namespace = ?\").get(\"decoded-page\")!.n,\n+\t\t\t0,\n+\t\t);\n+\t\treturn frame.entry.type;\n+\t});\n+\tassert.equal(result.result, \"join\");\n+\tassert.equal(result.digest, writerFrameDigest(\"\", 0, json));\n+\tassert.deepEqual(storage.db.prepare(\"SELECT * FROM writer_state\").all(), before);\n+\tassert.throws(\n+\t\t() =>\n+\t\t\twithDecodedFrames(storage, \"decoded-page\", log, input, (frames) => {\n+\t\t\t\t[...frames];\n+\t\t\t\tthrow Error(\"consumer failure\");\n+\t\t\t}),\n+\t\t/consumer failure/,\n+\t);\n+\tassert.deepEqual(storage.db.prepare(\"SELECT * FROM writer_state\").all(), before);\n+});\n+\n+test(\"frames above the small bound still use temporary disk storage\", (t) => {\n+\tconst storage = memoryStorage();\n+\tt.after(() => storage.db.close());\n+\tconst log = new SqliteLog(storage);\n+\tconst agent = \"a\".repeat(5000);\n+\tconst json = JSON.stringify({ type: \"join\", agent, version: 0 });\n+\tfor (const [part, text] of logParts(json).entries())\n+\t\tstorage.sql.exec(\"INSERT INTO log(seq,part,text) VALUES(?,?,?)\", 0, part, text);\n+\tconst result = withDecodedFrames(\n+\t\tstorage,\n+\t\t\"large-decoded-page\",\n+\t\tlog,\n+\t\t{ from: 0, to: 1, maxEntries: 1, maxBytes: 10000, digest: \"\" },\n+\t\t(frames) => {\n+\t\t\tconst frame = [...frames][0]!;\n+\t\t\tassert.ok(\"entry\" in frame);\n+\t\t\tassert.ok(\n+\t\t\t\tNumber(\n+\t\t\t\t\tstorage.db.prepare(\"SELECT COUNT(*) AS n FROM writer_state WHERE namespace = ?\").get(\"large-decoded-page\")!.n,\n+\t\t\t\t) > 0,\n+\t\t\t);\n+\t\t\tassert.equal((frame.entry as { agent: string }).agent, agent);\n+\t\t\treturn frame.entry.type;\n+\t\t},\n+\t);\n+\tassert.equal(result.result, \"join\");\n+\tassert.equal(\n+\t\tstorage.db.prepare(\"SELECT COUNT(*) AS n FROM writer_state WHERE namespace = ?\").get(\"large-decoded-page\")!.n,\n+\t\t0,\n+\t);\n+});"},{"path":"server/test/project-object-disk-first.test.ts","added":14,"removed":0,"diff":"--- a/server/test/project-object-disk-first.test.ts\n+++ b/server/test/project-object-disk-first.test.ts\n@@ -285,8 +285,13 @@\n \t\tthrow Error(\"empty test repository\");\n \t});\n \tlet pushed = 0;\n+\tlet failPush = true;\n \tlet remoteFiles: MemoryFS | undefined;\n \tt.mock.method(git, \"push\", async (options: Parameters<typeof git.push>[0]) => {\n+\t\tif (failPush) {\n+\t\t\tfailPush = false;\n+\t\t\tthrow Error(\"injected Artifacts transport failure\");\n+\t\t}\n \t\tconst oid = await git.resolveRef({ fs: options.fs, dir: options.dir, ref: \"main\" });\n \t\tconst commit = await git.readCommit({ fs: options.fs, dir: options.dir, oid });\n \t\tassert.match(commit.commit.message, /abso-version: 2/);\n@@ -304,8 +309,17 @@\n \t\t}),\n \t};\n \tconst bound = () => new ProjectObject(context as never, { ABSO_DISK_FIRST: \"on\", ARTIFACTS: artifacts } as never);\n+\t// Deliver the pending alarm: the fake storage must clear it as the host does.\n+\talarm = null;\n \tawait bound().alarm();\n+\tassert.equal(pushed, 0);\n+\tassert.equal(values.get(\"pushed\"), undefined, \"failed transport must not acknowledge a push\");\n+\tassert.match(JSON.stringify(values.get(\"pushError\")), /injected Artifacts transport failure/);\n+\tassert.deepEqual(rows(), stable, \"failed remote transport leaves canonical state unchanged\");\n+\tassert.ok(alarm !== null && alarm > Date.now(), \"failed transport schedules a retry\");\n+\tawait bound().alarm();\n \tassert.equal(pushed, 1);\n+\tassert.equal(values.get(\"pushError\"), undefined, \"cold retry clears the transport error\");\n \tassert.equal((await bound().out()).pushed?.version, 2);\n \tassert.deepEqual(rows(), stable, \"pushing shared does not mutate canonical project state\");\n \tawait bound().alarm();"},{"path":"server/test/projected-compare.test.ts","added":36,"removed":0,"diff":"--- /dev/null\n+++ b/server/test/projected-compare.test.ts\n@@ -0,0 +1,36 @@\n+import assert from \"node:assert/strict\";\n+import { test } from \"node:test\";\n+import { answer } from \"../../worker/project/http.ts\";\n+import { DiskFirstRuntime } from \"../src/disk-first-runtime.ts\";\n+import { memoryStorage } from \"./helpers.ts\";\n+\n+test(\"disk runtime compares published versions through indexed views\", (t) => {\n+\tconst storage = memoryStorage();\n+\tt.after(() => storage.db.close());\n+\tconst runtime = () => new DiskFirstRuntime(storage, \"compare\", \"compare\");\n+\truntime().initialize(\n+\t\t(c) => answer(c, \"init\", undefined, { files: { \"a.txt\": \"before\\n\" } }),\n+\t\t() => {},\n+\t);\n+\twhile (!runtime().preparePage(() => {}).ready) {}\n+\tassert.equal(\n+\t\truntime().write(\n+\t\t\t(c) => answer(c, \"record\", \"alice\", { files: { \"a.txt\": \"after\\n\" } }),\n+\t\t\t() => {},\n+\t\t).status,\n+\t\t200,\n+\t);\n+\tassert.equal(\n+\t\truntime().write(\n+\t\t\t(c) => answer(c, \"publish\", \"alice\", { message: \"change\" }),\n+\t\t\t() => {},\n+\t\t).status,\n+\t\t200,\n+\t);\n+\tconst result = runtime().read(\n+\t\t(c) => answer(c, \"compare\", undefined, { from: 0, to: 1 }),\n+\t\t() => {},\n+\t);\n+\tassert.equal(result.status, 200, JSON.stringify(result));\n+\tassert.equal((result.body as { files: unknown[] }).files.length, 1);\n+});"},{"path":"server/test/projected-diff.test.ts","added":31,"removed":0,"diff":"--- /dev/null\n+++ b/server/test/projected-diff.test.ts\n@@ -0,0 +1,31 @@\n+import assert from \"node:assert/strict\";\n+import { test } from \"node:test\";\n+import { answer } from \"../../worker/project/http.ts\";\n+import { DiskFirstRuntime } from \"../src/disk-first-runtime.ts\";\n+import { memoryStorage } from \"./helpers.ts\";\n+\n+test(\"disk runtime reads the unpublished diff from indexed membership\", (t) => {\n+\tconst storage = memoryStorage();\n+\tt.after(() => storage.db.close());\n+\tconst runtime = () => new DiskFirstRuntime(storage, \"diff\", \"diff\");\n+\truntime().initialize(\n+\t\t(c) => answer(c, \"init\", undefined, { files: { \"a.txt\": \"before\\n\" } }),\n+\t\t() => {},\n+\t);\n+\twhile (!runtime().preparePage(() => {}).ready) {}\n+\tassert.equal(\n+\t\truntime().write(\n+\t\t\t(c) => answer(c, \"record\", \"alice\", { files: { \"a.txt\": \"after\\n\" } }),\n+\t\t\t() => {},\n+\t\t).status,\n+\t\t200,\n+\t);\n+\tconst result = runtime().write(\n+\t\t(c) => answer(c, \"diff\", \"alice\", {}),\n+\t\t() => {},\n+\t);\n+\tassert.equal(result.status, 200, JSON.stringify(result));\n+\tassert.deepEqual((result.body as { files: unknown }).files, [\n+\t\t{ path: \"a.txt\", before: \"before\\n\", after: \"after\\n\" },\n+\t]);\n+});"},{"path":"server/test/projected-move-sync.test.ts","added":51,"removed":0,"diff":"--- /dev/null\n+++ b/server/test/projected-move-sync.test.ts\n@@ -0,0 +1,51 @@\n+import assert from \"node:assert/strict\";\n+import { test } from \"node:test\";\n+import { answer } from \"../../worker/project/http.ts\";\n+import { DiskFirstRuntime } from \"../src/disk-first-runtime.ts\";\n+import { memoryStorage } from \"./helpers.ts\";\n+\n+test(\"disk runtime syncs overlapping edits after a published move\", (t) => {\n+\tconst storage = memoryStorage();\n+\tt.after(() => storage.db.close());\n+\tconst runtime = () => new DiskFirstRuntime(storage, \"sync\", \"sync\");\n+\truntime().initialize(\n+\t\t(c) => answer(c, \"init\", undefined, { files: { \"a.txt\": \"one\\ntwo\\nthree\\n\" } }),\n+\t\t() => {},\n+\t);\n+\twhile (!runtime().preparePage(() => {}).ready) {}\n+\tconst call = (op: string, agent: string, body: unknown) =>\n+\t\truntime().write(\n+\t\t\t(c) => answer(c, op, agent, body),\n+\t\t\t() => {},\n+\t\t);\n+\tassert.equal(call(\"move\", \"alice\", { from: \"a.txt\", to: \"b.txt\" }).status, 200);\n+\tassert.equal(call(\"publish\", \"alice\", { message: \"Move\" }).status, 200);\n+\tfor (const agent of [\"alice\", \"bob\"]) assert.equal(call(\"sync\", agent, {}).status, 200);\n+\tassert.equal(call(\"record\", \"alice\", { files: { \"b.txt\": \"one\\nalice\\nthree\\n\" } }).status, 200);\n+\tassert.equal(call(\"record\", \"bob\", { files: { \"b.txt\": \"one\\nbob\\nthree\\n\" } }).status, 200);\n+\tassert.equal(call(\"publish\", \"alice\", { message: \"alice\" }).status, 200);\n+\tconst result = {\n+\t\tstatus: 200,\n+\t\tbody: runtime().write(\n+\t\t\t(c) => c.sync(\"bob\"),\n+\t\t\t() => {},\n+\t\t),\n+\t};\n+\tassert.equal(result.status, 200, JSON.stringify(result));\n+\tconst body = result.body as {\n+\t\tversion: number;\n+\t\tfiles: Record<string, string>;\n+\t\tcollisions: { kind: string; marked?: boolean }[];\n+\t};\n+\tassert.equal(body.version, 2);\n+\tassert.equal(body.collisions.length, 1);\n+\tassert.equal(body.collisions[0]!.kind, \"lines\");\n+\tassert.equal(body.collisions[0]!.marked, true);\n+\tassert.match(body.files[\"b.txt\"]!, /<<<<<<< yours/);\n+\tassert.match(body.files[\"b.txt\"]!, /bob/);\n+\tassert.match(body.files[\"b.txt\"]!, /alice/);\n+\tconst repeated = call(\"sync\", \"bob\", {});\n+\tassert.equal(repeated.status, 200);\n+\tassert.equal((repeated.body as typeof body).collisions.length, 1);\n+\tassert.equal((repeated.body as typeof body).collisions[0]!.marked, true);\n+});"},{"path":"server/test/projected-move.test.ts","added":35,"removed":0,"diff":"--- /dev/null\n+++ b/server/test/projected-move.test.ts\n@@ -0,0 +1,35 @@\n+import assert from \"node:assert/strict\";\n+import { test } from \"node:test\";\n+import { answer } from \"../../worker/project/http.ts\";\n+import { DiskFirstRuntime } from \"../src/disk-first-runtime.ts\";\n+import { memoryStorage } from \"./helpers.ts\";\n+\n+test(\"disk runtime moves a text file without using a stale reader\", (t) => {\n+\tconst storage = memoryStorage();\n+\tt.after(() => storage.db.close());\n+\tconst runtime = () => new DiskFirstRuntime(storage, \"move\", \"move\");\n+\truntime().initialize(\n+\t\t(c) => answer(c, \"init\", undefined, { files: { \"a.txt\": \"one\\ntwo\\n\" } }),\n+\t\t() => {},\n+\t);\n+\twhile (!runtime().preparePage(() => {}).ready) {}\n+\tconst result = runtime().write(\n+\t\t(c) => answer(c, \"move\", \"alice\", { from: \"a.txt\", to: \"b.txt\" }),\n+\t\t() => {},\n+\t);\n+\tassert.equal(result.status, 200, JSON.stringify(result));\n+\tassert.equal(\n+\t\truntime().write(\n+\t\t\t(c) => answer(c, \"publish\", \"alice\", { message: \"move\" }),\n+\t\t\t() => {},\n+\t\t).status,\n+\t\t200,\n+\t);\n+\tassert.deepEqual(\n+\t\truntime().read(\n+\t\t\t(c) => c.shared().files,\n+\t\t\t() => {},\n+\t\t),\n+\t\t{ \"b.txt\": \"one\\ntwo\\n\" },\n+\t);\n+});"},{"path":"server/test/projected-queue.test.ts","added":182,"removed":0,"diff":"--- /dev/null\n+++ b/server/test/projected-queue.test.ts\n@@ -0,0 +1,182 @@\n+import assert from \"node:assert/strict\";\n+import { test } from \"node:test\";\n+import { answer } from \"../../worker/project/http.ts\";\n+import type { Project } from \"../../worker/project/project.ts\";\n+import { DiskFirstRuntime } from \"../src/disk-first-runtime.ts\";\n+import { memoryStorage } from \"./helpers.ts\";\n+\n+test(\"disk queue preserves a dropped base without restoring dropped changes\", (t) => {\n+\tconst storage = memoryStorage();\n+\tt.after(() => storage.db.close());\n+\tconst runtime = () => new DiskFirstRuntime(storage, \"queue-drop\", \"queue-drop\");\n+\truntime().initialize(\n+\t\t(c) => answer(c, \"init\", undefined, { files: { \"avcs.json\": '{\"queue\":true}', \"a.txt\": \"before\\n\" } }),\n+\t\t() => {},\n+\t);\n+\twhile (!runtime().preparePage(() => {}).ready) {}\n+\truntime().write(\n+\t\t(c) => {\n+\t\t\tassert.equal(answer(c, \"record\", \"alice\", { files: { \"a.txt\": \"temporary\\n\" } }).status, 200);\n+\t\t\tassert.equal(answer(c, \"publish\", \"alice\", { message: \"Temporary\" }).status, 200);\n+\t\t\tc.sync(\"old\");\n+\t\t\tassert.equal(c.drop(\"alice\", [1], \"Undo temporary\").ok, true);\n+\t\t},\n+\t\t() => {},\n+\t);\n+\tfor (const [agent, file] of [\n+\t\t[\"ahead\", \"ahead.txt\"],\n+\t\t[\"old\", \"old.txt\"],\n+\t] as const) {\n+\t\truntime().write(\n+\t\t\t(c) => {\n+\t\t\t\tc.setCheckerReady(true);\n+\t\t\t\tassert.equal(answer(c, \"record\", agent, { files: { [file]: `${agent}\\n` } }).status, 200);\n+\t\t\t\tconst result = answer(c, \"publish\", agent, { message: agent });\n+\t\t\t\tassert.equal(result.status, 200, JSON.stringify(result));\n+\t\t\t\tassert.equal((result.body as { queued: number }).queued, agent === \"ahead\" ? 1 : 2);\n+\t\t\t},\n+\t\t\t() => {},\n+\t\t);\n+\t}\n+\tconst candidate = runtime().read(\n+\t\t(c) => (c as unknown as { project: Project }).project.queueCandidate(2),\n+\t\t() => {},\n+\t);\n+\tassert.equal(candidate.files[\"a.txt\"], \"before\\n\");\n+\tassert.equal(candidate.files[\"ahead.txt\"], \"ahead\\n\");\n+\tassert.equal(candidate.files[\"old.txt\"], \"old\\n\");\n+});\n+\n+test(\"disk runtime builds and lands a queued candidate\", (t) => {\n+\tconst storage = memoryStorage();\n+\tt.after(() => storage.db.close());\n+\tconst runtime = () => new DiskFirstRuntime(storage, \"queue\", \"queue\");\n+\truntime().initialize(\n+\t\t(c) => answer(c, \"init\", undefined, { files: { \"avcs.json\": '{\"queue\":true}', \"a.txt\": \"before\\n\" } }),\n+\t\t() => {},\n+\t);\n+\twhile (!runtime().preparePage(() => {}).ready) {}\n+\truntime().write(\n+\t\t(c) => {\n+\t\t\tc.setCheckerReady(true);\n+\t\t\tassert.equal(answer(c, \"record\", \"alice\", { files: { \"a.txt\": \"after\\n\" } }).status, 200);\n+\t\t\tconst result = answer(c, \"publish\", \"alice\", { message: \"Queued\" });\n+\t\t\tassert.equal((result.body as { queued: number }).queued, 1);\n+\t\t},\n+\t\t() => {},\n+\t);\n+\t// Queue-driver methods are internal to the Project adapter, not public HTTP commands.\n+\tconst candidate = runtime().read(\n+\t\t(c) => (c as unknown as { project: Project }).project.queueCandidate(1),\n+\t\t() => {},\n+\t);\n+\tassert.equal(candidate.files[\"a.txt\"], \"after\\n\");\n+\tconst landed = runtime().write(\n+\t\t(c) => (c as unknown as { project: Project }).project.queueLand(1, 10),\n+\t\t() => {},\n+\t);\n+\tassert.deepEqual(landed.versions, [{ id: 1, version: 1 }]);\n+\tassert.equal(\n+\t\truntime().read(\n+\t\t\t(c) => c.shared().files[\"a.txt\"],\n+\t\t\t() => {},\n+\t\t),\n+\t\t\"after\\n\",\n+\t);\n+});\n+\n+test(\"disk runtime builds and lands two queued candidates in order\", (t) => {\n+\tconst storage = memoryStorage();\n+\tt.after(() => storage.db.close());\n+\tconst runtime = () => new DiskFirstRuntime(storage, \"queue\", \"queue\");\n+\truntime().initialize(\n+\t\t(c) => answer(c, \"init\", undefined, { files: { \"avcs.json\": '{\"queue\":true}', \"a.txt\": \"before\\n\" } }),\n+\t\t() => {},\n+\t);\n+\twhile (!runtime().preparePage(() => {}).ready) {}\n+\truntime().write(\n+\t\t(c) => {\n+\t\t\tc.setCheckerReady(true);\n+\t\t\tassert.equal(answer(c, \"record\", \"alice\", { files: { \"a.txt\": \"after\\n\" } }).status, 200);\n+\t\t\tconst result = answer(c, \"publish\", \"alice\", { message: \"Queued\" });\n+\t\t\tassert.equal((result.body as { queued: number }).queued, 1);\n+\t\t},\n+\t\t() => {},\n+\t);\n+\truntime().write(\n+\t\t(c) => {\n+\t\t\tc.setCheckerReady(true);\n+\t\t\tassert.equal(answer(c, \"record\", \"bob\", { files: { \"b.txt\": \"second\\n\" } }).status, 200);\n+\t\t\tconst result = answer(c, \"publish\", \"bob\", { message: \"Second\" });\n+\t\t\tassert.equal(result.status, 200, JSON.stringify(result));\n+\t\t\tassert.equal((result.body as { queued: number }).queued, 2);\n+\t\t},\n+\t\t() => {},\n+\t);\n+\t// Queue-driver methods are internal to the Project adapter, not public HTTP commands.\n+\tconst candidate = runtime().read(\n+\t\t(c) => (c as unknown as { project: Project }).project.queueCandidate(2),\n+\t\t() => {},\n+\t);\n+\tassert.equal(candidate.files[\"a.txt\"], \"after\\n\");\n+\tassert.equal(candidate.files[\"b.txt\"], \"second\\n\");\n+\tconst landed = runtime().write(\n+\t\t(c) => (c as unknown as { project: Project }).project.queueLand(2, 10),\n+\t\t() => {},\n+\t);\n+\tassert.deepEqual(landed.versions, [\n+\t\t{ id: 1, version: 1 },\n+\t\t{ id: 2, version: 2 },\n+\t]);\n+\tassert.equal(\n+\t\truntime().read(\n+\t\t\t(c) => c.shared().files[\"a.txt\"],\n+\t\t\t() => {},\n+\t\t),\n+\t\t\"after\\n\",\n+\t);\n+});\n+\n+test(\"disk queue refuses overlapping edits and accepts an older nonconflicting base\", (t) => {\n+\tconst storage = memoryStorage();\n+\tt.after(() => storage.db.close());\n+\tconst runtime = () => new DiskFirstRuntime(storage, \"queue-edge\", \"queue-edge\");\n+\truntime().initialize(\n+\t\t(c) => answer(c, \"init\", undefined, { files: { \"avcs.json\": '{\"queue\":true}', \"a.txt\": \"before\\n\" } }),\n+\t\t() => {},\n+\t);\n+\twhile (!runtime().preparePage(() => {}).ready) {}\n+\tconst call = (op: string, agent: string, body: unknown) =>\n+\t\truntime().write(\n+\t\t\t(c) => {\n+\t\t\t\tc.setCheckerReady(true);\n+\t\t\t\treturn answer(c, op, agent, body);\n+\t\t\t},\n+\t\t\t() => {},\n+\t\t);\n+\tfor (const agent of [\"alice\", \"bob\", \"old\"]) assert.equal(call(\"sync\", agent, {}).status, 200);\n+\tassert.equal(call(\"record\", \"alice\", { files: { \"a.txt\": \"alice\\n\" } }).status, 200);\n+\tassert.equal((call(\"publish\", \"alice\", { message: \"Alice\" }).body as { queued: number }).queued, 1);\n+\tassert.equal(call(\"record\", \"bob\", { files: { \"a.txt\": \"bob\\n\" } }).status, 200);\n+\tconst refused = call(\"publish\", \"bob\", { message: \"Bob conflict\" });\n+\tassert.equal(refused.status, 200, JSON.stringify(refused));\n+\tassert.equal((refused.body as { ok: boolean }).ok, false);\n+\tassert.ok((refused.body as { collisions: unknown[] }).collisions.length > 0);\n+\truntime().write(\n+\t\t(c) => (c as unknown as { project: Project }).project.queueLand(1, 20),\n+\t\t() => {},\n+\t);\n+\tassert.equal(call(\"record\", \"alice\", { files: { \"next.txt\": \"next\\n\" } }).status, 200);\n+\tassert.equal((call(\"publish\", \"alice\", { message: \"Next\" }).body as { queued: number }).queued, 2);\n+\tassert.equal(call(\"record\", \"old\", { files: { \"old.txt\": \"old base\\n\" } }).status, 200);\n+\tconst queued = call(\"publish\", \"old\", { message: \"Old base\" });\n+\tassert.equal(queued.status, 200, JSON.stringify(queued));\n+\tassert.equal((queued.body as { queued: number }).queued, 3);\n+\tconst candidate = runtime().read(\n+\t\t(c) => (c as unknown as { project: Project }).project.queueCandidate(2),\n+\t\t() => {},\n+\t);\n+\tassert.equal(candidate.files[\"a.txt\"], \"alice\\n\");\n+\tassert.equal(candidate.files[\"next.txt\"], \"next\\n\");\n+\tassert.equal(candidate.files[\"old.txt\"], \"old base\\n\");\n+});"},{"path":"server/test/projected-restore.test.ts","added":69,"removed":0,"diff":"--- /dev/null\n+++ b/server/test/projected-restore.test.ts\n@@ -0,0 +1,69 @@\n+import assert from \"node:assert/strict\";\n+import { test } from \"node:test\";\n+import { answer } from \"../../worker/project/http.ts\";\n+import { DiskFirstRuntime } from \"../src/disk-first-runtime.ts\";\n+import { memoryStorage } from \"./helpers.ts\";\n+\n+test(\"disk runtime restores unpublished files and preserves published history\", (t) => {\n+\tconst storage = memoryStorage();\n+\tt.after(() => storage.db.close());\n+\tconst runtime = () => new DiskFirstRuntime(storage, \"restore\", \"restore\");\n+\truntime().initialize(\n+\t\t(c) => answer(c, \"init\", undefined, { files: { \"a.txt\": \"original\\n\", \"b.txt\": \"kept\\n\" } }),\n+\t\t() => {},\n+\t);\n+\twhile (!runtime().preparePage(() => {}).ready) {}\n+\tconst call = (op: string, body: unknown) =>\n+\t\truntime().write(\n+\t\t\t(c) => answer(c, op, \"alice\", body),\n+\t\t\t() => {},\n+\t\t);\n+\tassert.equal(call(\"record\", { files: { \"a.txt\": \"changed\\n\", \"b.txt\": \"different\\n\" } }).status, 200);\n+\tconst result = call(\"restore\", { all: true });\n+\tassert.equal(result.status, 200, JSON.stringify(result));\n+\tassert.deepEqual((result.body as { files: unknown }).files, { \"a.txt\": \"original\\n\", \"b.txt\": \"kept\\n\" });\n+\tassert.deepEqual(\n+\t\truntime().read(\n+\t\t\t(c) => c.shared().files,\n+\t\t\t() => {},\n+\t\t),\n+\t\t{ \"a.txt\": \"original\\n\", \"b.txt\": \"kept\\n\" },\n+\t);\n+});\n+\n+for (const queued of [false, true]) {\n+\ttest(`disk restore preserves ${queued ? \"queued\" : \"untargeted\"} edits`, (t) => {\n+\t\tconst storage = memoryStorage();\n+\t\tt.after(() => storage.db.close());\n+\t\tconst runtime = () => new DiskFirstRuntime(storage, \"partial\", \"partial\");\n+\t\truntime().initialize(\n+\t\t\t(c) =>\n+\t\t\t\tanswer(c, \"init\", undefined, {\n+\t\t\t\t\tfiles: { \"avcs.json\": JSON.stringify({ queue: queued }), \"a.txt\": \"original\\n\", \"b.txt\": \"original b\\n\" },\n+\t\t\t\t}),\n+\t\t\t() => {},\n+\t\t);\n+\t\twhile (!runtime().preparePage(() => {}).ready) {}\n+\t\tconst call = (op: string, body: unknown) =>\n+\t\t\truntime().write(\n+\t\t\t\t(c) => {\n+\t\t\t\t\tc.setCheckerReady(true);\n+\t\t\t\t\treturn answer(c, op, \"alice\", body);\n+\t\t\t\t},\n+\t\t\t\t() => {},\n+\t\t\t);\n+\t\tassert.equal(call(\"record\", { files: { \"b.txt\": \"preserved\\n\" } }).status, 200);\n+\t\tif (queued) {\n+\t\t\tconst published = call(\"publish\", { message: \"queued work\" });\n+\t\t\tassert.equal(published.status, 200, JSON.stringify(published));\n+\t\t\tassert.equal((published.body as { queued?: number }).queued, 1);\n+\t\t}\n+\t\tassert.equal(call(\"record\", { files: { \"a.txt\": \"discard me\\n\" } }).status, 200);\n+\t\tconst restored = call(\"restore\", queued ? { all: true } : { files: [\"a.txt\"] });\n+\t\tassert.equal(restored.status, 200, JSON.stringify(restored));\n+\t\tassert.deepEqual((restored.body as { files: unknown }).files, { \"a.txt\": \"original\\n\" });\n+\t\tconst synced = call(\"sync\", {});\n+\t\tassert.equal(synced.status, 200, JSON.stringify(synced));\n+\t\tassert.equal((synced.body as { files: Record<string, string> }).files[\"b.txt\"], \"preserved\\n\");\n+\t});\n+}"},{"path":"server/test/projected-sync.test.ts","added":43,"removed":0,"diff":"--- /dev/null\n+++ b/server/test/projected-sync.test.ts\n@@ -0,0 +1,43 @@\n+import assert from \"node:assert/strict\";\n+import { test } from \"node:test\";\n+import { answer } from \"../../worker/project/http.ts\";\n+import { DiskFirstRuntime } from \"../src/disk-first-runtime.ts\";\n+import { memoryStorage } from \"./helpers.ts\";\n+\n+test(\"disk runtime syncs overlapping edits without unkeyed membership\", (t) => {\n+\tconst storage = memoryStorage();\n+\tt.after(() => storage.db.close());\n+\tconst runtime = () => new DiskFirstRuntime(storage, \"sync\", \"sync\");\n+\truntime().initialize(\n+\t\t(c) => answer(c, \"init\", undefined, { files: { \"a.txt\": \"one\\ntwo\\nthree\\n\" } }),\n+\t\t() => {},\n+\t);\n+\twhile (!runtime().preparePage(() => {}).ready) {}\n+\tconst call = (op: string, agent: string, body: unknown) =>\n+\t\truntime().write(\n+\t\t\t(c) => answer(c, op, agent, body),\n+\t\t\t() => {},\n+\t\t);\n+\tfor (const agent of [\"alice\", \"bob\"]) assert.equal(call(\"sync\", agent, {}).status, 200);\n+\tassert.equal(call(\"record\", \"alice\", { files: { \"a.txt\": \"one\\nalice\\nthree\\n\" } }).status, 200);\n+\tassert.equal(call(\"record\", \"bob\", { files: { \"a.txt\": \"one\\nbob\\nthree\\n\" } }).status, 200);\n+\tassert.equal(call(\"publish\", \"alice\", { message: \"alice\" }).status, 200);\n+\tconst result = call(\"sync\", \"bob\", {});\n+\tassert.equal(result.status, 200, JSON.stringify(result));\n+\tconst body = result.body as {\n+\t\tversion: number;\n+\t\tfiles: Record<string, string>;\n+\t\tcollisions: { kind: string; marked?: boolean }[];\n+\t};\n+\tassert.equal(body.version, 1);\n+\tassert.equal(body.collisions.length, 1);\n+\tassert.equal(body.collisions[0]!.kind, \"lines\");\n+\tassert.equal(body.collisions[0]!.marked, true);\n+\tassert.match(body.files[\"a.txt\"]!, /<<<<<<< yours/);\n+\tassert.match(body.files[\"a.txt\"]!, /bob/);\n+\tassert.match(body.files[\"a.txt\"]!, /alice/);\n+\tconst repeated = call(\"sync\", \"bob\", {});\n+\tassert.equal(repeated.status, 200);\n+\tassert.equal((repeated.body as typeof body).collisions.length, 1);\n+\tassert.equal((repeated.body as typeof body).collisions[0]!.marked, true);\n+});"},{"path":"server/test/small-frame-json.test.ts","added":21,"removed":0,"diff":"--- /dev/null\n+++ b/server/test/small-frame-json.test.ts\n@@ -0,0 +1,21 @@\n+import assert from \"node:assert/strict\";\n+import { test } from \"node:test\";\n+import { smallFrameJson } from \"../src/small-frame-json.ts\";\n+\n+test(\"small frame decoding enforces a byte bound and streamed parser rules\", () => {\n+\tassert.deepEqual(smallFrameJson(['{\"type\":\"jo', 'in\",\"agent\":\"alice\"}']), { type: \"join\", agent: \"alice\" });\n+\tassert.deepEqual(smallFrameJson(['{\"x\":1,\"x\":2}']), { x: 2 });\n+\tassert.throws(() => smallFrameJson(['\"' + \"\\u00e9\".repeat(2048) + '\"']), /exceeds bound/);\n+\tassert.throws(() => smallFrameJson([\"[\".repeat(130) + \"0\" + \"]\".repeat(130)]), /nesting/);\n+\tassert.throws(() => smallFrameJson([\"1\" + \"0\".repeat(256)]), /scalar/);\n+\tassert.throws(() => smallFrameJson([\"{} trailing\"]), /trailing/);\n+});\n+test(\"small frame byte accounting handles a split surrogate pair at the limit\", () => {\n+\tconst pair = String.fromCodePoint(0x1f600);\n+\tconst body = \"x\".repeat(4090) + pair;\n+\tconst json = JSON.stringify(body);\n+\tassert.equal(new TextEncoder().encode(json).byteLength, 4096);\n+\tconst split = json.indexOf(pair) + 1;\n+\tassert.equal(smallFrameJson([json.slice(0, split), json.slice(split)]), body);\n+\tassert.throws(() => smallFrameJson([JSON.stringify(body + \"x\")]), /exceeds bound/);\n+});"},{"path":"src/engine/experimental/projection-reader.ts","added":15,"removed":3,"diff":"--- a/src/engine/experimental/projection-reader.ts\n+++ b/src/engine/experimental/projection-reader.ts\n@@ -175,9 +175,21 @@\n \tfileOf(line: string): string {\n \t\tthis.#assertLive();\n \t\tif (line.startsWith(\"^\")) return line.slice(1);\n-\t\tconst marker = this.#adapter.markers.get(this.#state.locator, this.graph.lineN(line) + 1);\n-\t\tif (marker?.kind !== \"line\") throw Error(\"missing projected line\");\n-\t\treturn this.#adapter.sequence.fileAt(this.#state.sequence, marker.enter);\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();"},{"path":"src/engine/experimental/projection-sequence.ts","added":15,"removed":1,"diff":"--- a/src/engine/experimental/projection-sequence.ts\n+++ b/src/engine/experimental/projection-sequence.ts\n@@ -143,7 +143,21 @@\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;"},{"path":"src/engine/test/projection-sequence.test.ts","added":25,"removed":0,"diff":"--- a/src/engine/test/projection-sequence.test.ts\n+++ b/src/engine/test/projection-sequence.test.ts\n@@ -47,3 +47,28 @@\n \t\tstorage.db.close();\n \t}\n });\n+\n+test(\"sequence batches match individual updates and retry after rollback\", () => {\n+\tconst storage = new SqlState();\n+\ttry {\n+\t\tconst sequence = new ProjectionSequence(storage, \"batch-cache\");\n+\t\tlet state = sequence.empty();\n+\t\tfor (let i = 0; i < 48; i++) state = sequence.update(state, { kind: \"insert\", rank: i, text: String(i) });\n+\t\tconst before = state;\n+\t\tconst operations = Array.from({ length: 16 }, (_, i) => ({ kind: \"visibility\" as const, id: i, delta: 1 }));\n+\t\tconst rows = () => JSON.stringify(storage.db.prepare(\"SELECT * FROM state ORDER BY collection,key\").all());\n+\t\tconst original = rows();\n+\t\tstorage.failWrite = storage.writes + 3;\n+\t\tassert.throws(() => sequence.updateMany(before, operations), /injected/);\n+\t\tassert.equal(rows(), original);\n+\t\tstorage.failWrite = Infinity;\n+\t\tconst batch = sequence.updateMany(before, operations);\n+\t\tlet individual = before;\n+\t\tfor (const operation of operations) individual = sequence.update(individual, operation);\n+\t\tassert.deepEqual([...sequence.values(batch)], [...sequence.values(individual)]);\n+\t\tassert.equal([...sequence.values(before)].length, 48);\n+\t\tassert.equal([...sequence.values(batch)].length, 32);\n+\t} finally {\n+\t\tstorage.db.close();\n+\t}\n+});"},{"path":"worker/project/draft-diff.ts","added":4,"removed":1,"diff":"--- a/worker/project/draft-diff.ts\n+++ b/worker/project/draft-diff.ts\n@@ -18,6 +18,7 @@\n \tneedInit(): void;\n \tdraft(agent: Agent): AtomId[];\n \tagentView(agent: Agent): ReadView;\n+\tagentViewWithout?(agent: Agent, excluded: ReadonlySet<AtomId>): ReadView;\n \tview(set: ReadonlySet<AtomId>): ReadView;\n \ttouchedFiles(ids: Iterable<AtomId>, views: readonly ReadView[]): Set<string>;\n }\n@@ -35,7 +36,9 @@\n \tconst draft = host.draft(agent);\n \tconst after = host.agentView(agent);\n \tconst without = new Set(draft);\n-\tconst before = host.view(new Set([...after.set].filter((id) => !without.has(id))));\n+\tconst before = host.agentViewWithout\n+\t\t? host.agentViewWithout(agent, without)\n+\t\t: host.view(new Set([...after.set].filter((id) => !without.has(id))));\n \tconst files = host.touchedFiles(draft, [before, after]);\n \treturn {\n \t\tdraft: agent.space === SHARED ? null : agent.space,"},{"path":"worker/project/merge-identical.ts","added":1,"removed":1,"diff":"--- a/worker/project/merge-identical.ts\n+++ b/worker/project/merge-identical.ts\n@@ -89,7 +89,7 @@\n \t\t}\n \t}\n \tif ([...involved].some((f) => shown.has(f))) return [];\n-\tconst baseView = host.view(oldBase);\n+\tconst baseView = host.viewAt(host.spaceOf(agent), oldVersion);\n \tconst theirs = host.viewAt(host.spaceOf(agent), agent.base);\n \tconst textOf = (v: ReadView, path: string): string | null => (v.hasFile(path) ? v.text(path) : null);\n \tconst merged = new Map<string, { text: string | null; mode: Mode; hunks?: Hunk[] }>();"},{"path":"worker/project/move.ts","added":2,"removed":1,"diff":"--- a/worker/project/move.ts\n+++ b/worker/project/move.ts\n@@ -73,8 +73,9 @@\n \tconst lines = view.lines(from).length;\n \tconst mode: Mode = { exec: attrs.exec, link: attrs.link };\n \tif (lines > 0 && attrs.blob === null) {\n+\t\tconst text = view.text(from);\n \t\thost.addDraft(agent, view, { edits: [], move: { file: from, start: 0, end: lines, to, at: 0 } }, time, {}, entries);\n-\t\thost.recordFile(agent, host.agentView(agent), to, view.text(from), time, {}, entries, mode);\n+\t\thost.recordFile(agent, host.agentView(agent), to, text, time, {}, entries, mode);\n \t} else {\n \t\t// A binary or empty file has no lines to carry: the new name takes its blob and modes.\n \t\thost.recordFile(agent, host.agentView(agent), to, \"\", time, {}, entries, mode, attrs.blob ?? undefined);"},{"path":"worker/project/project.ts","added":24,"removed":0,"diff":"--- a/worker/project/project.ts\n+++ b/worker/project/project.ts\n@@ -1837,6 +1837,10 @@\n \t\t\tneedInit: () => this.#needInit(),\n \t\t\tdraft: (agent) => this.#draft(agent as AgentState),\n \t\t\tagentView: (agent) => this.#agentView(agent as AgentState),\n+\t\t\tagentViewWithout: (agent, excluded) => {\n+\t\t\t\tconst state = agent as AgentState;\n+\t\t\t\treturn this.#agentView({ ...state, mine: state.mine.filter((id) => !excluded.has(id)) });\n+\t\t\t},\n \t\t\tview: (set) => this.#view(set),\n \t\t\ttouchedFiles: (ids, views) => this.#touchedFiles(ids, views),\n \t\t};\n@@ -2093,6 +2097,18 @@\n \t\t\tgraph: this.#graph,\n \t\t\tcheckerReady: () => this.checkerReady,\n \t\t\tsharedView: () => this.#sharedView(),\n+\t\t\tsharedOverlay: (atoms) =>\n+\t\t\t\tthis.#viewKeyed(`a @${last(this.#shared)}+${atoms.join(\",\")}`, () =>\n+\t\t\t\t\tunionOverlay(this.#setOf(this.#shared), atoms),\n+\t\t\t\t),\n+\t\t\tsharedAgentOverlay: (agent, atoms) => {\n+\t\t\t\tconst current = this.#setOf(this.#shared);\n+\t\t\t\tconst added = new Set([...atoms, ...agent.mine]);\n+\t\t\t\t// Retain old-base atoms absent from current shared, without copying shared history.\n+\t\t\t\tif (agent.base !== last(this.#shared))\n+\t\t\t\t\tfor (const id of this.#at(this.#spaceOf(agent), agent.base)) if (!current.has(id)) added.add(id);\n+\t\t\t\treturn this.#viewKeyed(`a @${last(this.#shared)}+${[...added].join(\",\")}`, () => unionOverlay(current, added));\n+\t\t\t},\n \t\t\tqueued: () => this.#queue,\n \t\t\tqueuedId: () => this.#queuedId,\n \t\t\tcheckingHas: (id) => this.#checking.has(id),\n@@ -2248,6 +2264,8 @@\n \t\t\tview: (set) => this.#view(set),\n \t\t\tagentSet: (agent) => this.#agentSet(agent),\n \t\t\tagentView: (agent) => this.#agentView(agent),\n+\t\t\tagentViewWithout: (agent, excluded) =>\n+\t\t\t\tthis.#agentView({ ...agent, mine: agent.mine.filter((id) => !excluded.has(id)) }),\n \t\t\trecordFile: (agent, view, path, text, time, meta, entries, mode, blob, hunks) =>\n \t\t\t\tthis.#recordFile(agent, view, path, text, time, meta, entries, mode, blob, hunks),\n \t\t\tflush: (entries) => this.#flush(entries),\n@@ -2278,6 +2296,12 @@\n \t\t\tagent: (name, entries) => this.#agent(name, entries),\n \t\t\tspaceOf: (agent) => this.#spaceOf(agent),\n \t\t\tagentView: (agent) => this.#agentView(agent),\n+\t\t\tcaptureAgentView: (agent) => {\n+\t\t\t\tconst key = this.#agentViewKey(agent);\n+\t\t\t\tconst original = this.#agentView(agent);\n+\t\t\t\t// Projected readers reopen the immutable key; only the in-memory fallback uses the original set.\n+\t\t\t\treturn () => this.#viewKeyed(key, () => original.set);\n+\t\t\t},\n \t\t\tat: (space, version) => this.#at(space, version),\n \t\t\tdraft: (agent) => this.#draft(agent),\n \t\t\tstrandedFiles: (draft, set) => this.#strandedFiles(draft, set),"},{"path":"worker/project/queue.ts","added":16,"removed":7,"diff":"--- a/worker/project/queue.ts\n+++ b/worker/project/queue.ts\n@@ -2,6 +2,7 @@\n // Writes go through QueueHost; today the host is Project.\n \n import { type AtomId, buildAtom, type Graph, type ReadView } from \"../../src/engine/index.ts\";\n+import { unionOverlay } from \"../../src/engine/state-source.ts\";\n import type { Pair } from \"./collisions.ts\";\n import { definedTwice } from \"./definitions.ts\";\n import { heldMessage, keptOf, lostLines } from \"./markers.ts\";\n@@ -28,6 +29,8 @@\n \treadonly graph: Graph;\n \tcheckerReady(): boolean;\n \tsharedView(): ReadView;\n+\tsharedOverlay?(atoms: readonly AtomId[]): ReadView;\n+\tsharedAgentOverlay?(agent: AgentState, atoms: readonly AtomId[]): ReadView;\n \tqueued(): readonly QueuedPublish[];\n \tqueuedId(): number;\n \tcheckingHas(id: number): boolean;\n@@ -117,11 +120,13 @@\n \tconst ahead = queuedAtoms(host, agent.name);\n \tconst now = host.setOf(space);\n \tif (ahead.size > 0) {\n-\t\tconst landed = new Set([...now, ...ahead]);\n-\t\tconst theirsView = host.view(landed);\n-\t\tconst yoursView = host.view(new Set([...landed, ...host.agentSet(agent)]));\n+\t\tconst landed = unionOverlay(now, ahead);\n+\t\tconst theirsView = host.sharedOverlay ? host.sharedOverlay([...ahead]) : host.view(landed);\n+\t\tconst yoursView = host.sharedAgentOverlay\n+\t\t\t? host.sharedAgentOverlay(agent, [...ahead])\n+\t\t\t: host.view(unionOverlay(landed, host.agentSet(agent)));\n \t\tconst queuedBy = (id: AtomId) => host.queued().find((q) => q.changes.includes(id));\n-\t\tconst collisions: Collision[] = host.pairs(changes, ahead, new Set([...landed, ...changes])).map((p) => {\n+\t\tconst collisions: Collision[] = host.pairs(changes, ahead, unionOverlay(landed, changes)).map((p) => {\n \t\t\tconst c = host.describe(p, host.agentView(agent), theirsView, agent.space);\n \t\t\tconst q = queuedBy(p.theirs);\n \t\t\treturn q ? { ...c, theirs: { ...c.theirs, agent: q.agent, message: q.message, queued: q.id } } : c;\n@@ -231,7 +236,10 @@\n } {\n \thost.needInit();\n \tconst items = host.queued().slice(0, Math.max(0, n));\n-\tconst view = host.view(new Set([...host.setOf(host.shared()), ...items.flatMap((q) => q.changes)]));\n+\tconst changes = items.flatMap((q) => q.changes);\n+\tconst view = host.sharedOverlay\n+\t\t? host.sharedOverlay(changes)\n+\t\t: host.view(unionOverlay(host.setOf(host.shared()), changes));\n \tconst files = view.files();\n \tconst modes = modesOf(view);\n \tconst blobs = view.blobs();\n@@ -250,8 +258,9 @@\n \t\tfor (const q of host.queued().slice(0, Math.max(0, n))) {\n \t\t\tconst agent = host.agentOf(q.agent);\n \t\t\tconst space = host.shared();\n-\t\t\tconst union = new Set([...host.setOf(space), ...q.changes]);\n-\t\t\tconst tie = buildAtom(host.view(union), {\n+\t\t\tconst union = unionOverlay(host.setOf(space), q.changes);\n+\t\t\tconst view = host.sharedOverlay ? host.sharedOverlay(q.changes) : host.view(union);\n+\t\t\tconst tie = buildAtom(view, {\n \t\t\t\tauthor: q.agent,\n \t\t\t\tmessage: q.message,\n \t\t\t\ttime,"},{"path":"worker/project/restore.ts","added":8,"removed":1,"diff":"--- a/worker/project/restore.ts\n+++ b/worker/project/restore.ts\n@@ -16,6 +16,7 @@\n \tview(set: ReadonlySet<AtomId>): ReadView;\n \tagentSet(agent: AgentState): Set<AtomId>;\n \tagentView(agent: AgentState): ReadView;\n+\tagentViewWithout?(agent: AgentState, excluded: ReadonlySet<AtomId>): ReadView;\n \trecordFile(\n \t\tagent: AgentState,\n \t\tview: ReadView,\n@@ -64,9 +65,15 @@\n \t// Whatever is still changed in a target goes back with a new change.\n \tconst now = host.draft(agent);\n \tconst without = new Set(now);\n-\tconst original = host.view(new Set([...host.agentSet(agent)].filter((id) => !without.has(id))));\n+\tconst originalAgent = { ...agent, mine: [...agent.mine] };\n+\tconst originalView = () =>\n+\t\thost.agentViewWithout\n+\t\t\t? host.agentViewWithout(originalAgent, without)\n+\t\t\t: host.view(new Set([...host.agentSet(originalAgent)].filter((id) => !without.has(id))));\n \tfor (const path of [...targets].toSorted()) {\n \t\tif (!touched.has(path)) continue;\n+\t\t// A previous file may have mutated the graph. Reopen the same base view.\n+\t\tconst original = originalView();\n \t\tconst was = original.attrs(path);\n \t\thost.recordFile(\n \t\t\tagent,"},{"path":"worker/project/sync.ts","added":7,"removed":3,"diff":"--- a/worker/project/sync.ts\n+++ b/worker/project/sync.ts\n@@ -3,7 +3,6 @@\n // SyncHost; today the host is Project.\n \n import type { AtomId, Graph, ReadView } from \"../../src/engine/index.ts\";\n-import { outlastsSync } from \"./news.ts\";\n import { ProjectError } from \"./project-error.ts\";\n import type { AgentState, Entry, MergedLine, SyncAnswer } from \"./project-types.ts\";\n import { last, SHARED, type Space } from \"./spaces.ts\";\n@@ -15,6 +14,7 @@\n \tagent(name: string, entries: Entry[]): AgentState;\n \tspaceOf(agent: AgentState): Space;\n \tagentView(agent: AgentState): ReadView;\n+\tcaptureAgentView?(agent: AgentState): () => ReadView;\n \tat(space: Space, version: number): ReadonlySet<AtomId>;\n \tdraft(agent: AgentState): AtomId[];\n \tstrandedFiles(draft: readonly AtomId[], set: ReadonlySet<AtomId>): string[];\n@@ -51,6 +51,7 @@\n \t\t\tthrow new ProjectError(\"bad-request\", `no version of shared has the label ${JSON.stringify(options.label)}`);\n \t\ttarget = versions[versions.length - 1]!;\n \t}\n+\tconst reopenBefore = agent.synced ? host.captureAgentView?.(agent) : undefined;\n \tconst before = agent.synced ? host.agentView(agent) : new View(host.graph, []);\n \tconst set = host.at(space, target);\n \tconst stranded = host.strandedFiles(host.draft(agent), set);\n@@ -80,14 +81,17 @@\n \tentries.push({ type: \"sync\", agent: agent.name, version: target });\n \thost.apply(entries[entries.length - 1]!);\n \t// News about versions the agent has now synced past is moot: the sync answer lists what's still open (#37).\n-\tif (agent.news.some((t) => !outlastsSync(t))) {\n+\tif (agent.news.some((t) => !t.startsWith(\"Announcement from \"))) {\n \t\tconst heard: Entry = { type: \"heard\", agent: agent.name, stale: true };\n \t\tentries.push(heard);\n \t\thost.apply(heard);\n \t}\n \tconst merged = host.mergeIdentical(agent, before, oldBase, oldVersion, Date.now(), entries);\n \t// full (#37, a partial join): every file in the view, as for a first sync, whatever the folder had.\n-\tconst answer = host.changesSince(agent, options.full ? new View(host.graph, []) : before);\n+\tconst answer = host.changesSince(\n+\t\tagent,\n+\t\toptions.full ? new View(host.graph, []) : reopenBefore ? reopenBefore() : before,\n+\t);\n \thost.flush(entries);\n \treturn merged.length > 0 ? { ...answer, merged } : answer;\n }"},{"path":"worker/project/version-log.ts","added":7,"removed":3,"diff":"--- a/worker/project/version-log.ts\n+++ b/worker/project/version-log.ts\n@@ -251,18 +251,22 @@\n \thost.checkVersion(space, to);\n \tconst a = host.at(space, from);\n \tconst b = host.at(space, to);\n+\tconst before = host.viewAt(space, from);\n+\tconst after = host.viewAt(space, to);\n \tconst files = new Set<string>();\n \tfor (const [x, y] of [\n \t\t[a, b],\n \t\t[b, a],\n \t] as const) {\n \t\tfor (const file of host.touchedFiles(\n-\t\t\t[...x].filter((id) => !y.has(id)),\n-\t\t\t[host.view(a), host.view(b)],\n+\t\t\t(function* () {\n+\t\t\t\tfor (const id of x) if (!y.has(id)) yield id;\n+\t\t\t})(),\n+\t\t\t[before, after],\n \t\t))\n \t\t\tfiles.add(file);\n \t}\n-\treturn { from, to, files: changesBetween(host.view(a), host.view(b), files) };\n+\treturn { from, to, files: changesBetween(before, after, files) };\n }\n \n /** A version of shared (or of a named draft) as a ReadView, for reading lines (blame at a version). */"}],"ci":null}