{"draft":null,"from":161,"to":162,"files":[{"path":"server/test/project-object-disk-first.test.ts","added":285,"removed":4,"diff":"--- a/server/test/project-object-disk-first.test.ts\n+++ b/server/test/project-object-disk-first.test.ts\n@@ -1,8 +1,10 @@\n import assert from \"node:assert/strict\";\n import { registerHooks } from \"node:module\";\n import { test } from \"node:test\";\n+import git from \"isomorphic-git\";\n import { Project } from \"../../worker/project/project.ts\";\n import { EngineProject } from \"../src/engine-project.ts\";\n+import { MemoryFS } from \"../src/memory-fs.ts\";\n import { memoryStorage } from \"./helpers.ts\";\n \n const hooks = registerHooks({\n@@ -45,6 +47,8 @@\n \tconst values = new Map<string, unknown>();\n \tlet alarm: number | null = null;\n \tlet onReaders: (() => Promise<void>) | undefined;\n+\tlet failGitAcknowledgement = false;\n+\tlet failNamePersistence = false;\n \tconst context = {\n \t\tid: { toString: () => \"project-1\" },\n \t\tstorage: {\n@@ -58,6 +62,14 @@\n \t\t\t\treturn values.get(key);\n \t\t\t},\n \t\t\tput: async (key: string | Record<string, unknown>, value?: unknown) => {\n+\t\t\t\tif (key === \"name\" && failNamePersistence) {\n+\t\t\t\t\tfailNamePersistence = false;\n+\t\t\t\t\tthrow Error(\"injected name persistence failure\");\n+\t\t\t\t}\n+\t\t\t\tif (failGitAcknowledgement && typeof key === \"object\" && \"git:head\" in key) {\n+\t\t\t\t\tfailGitAcknowledgement = false;\n+\t\t\t\t\tthrow Error(\"injected Git cursor persistence failure\");\n+\t\t\t\t}\n \t\t\t\tif (typeof key === \"string\") values.set(key, value);\n \t\t\t\telse for (const [name, v] of Object.entries(key)) values.set(name, v);\n \t\t\t},\n@@ -76,9 +88,9 @@\n \tt.mock.method(Project, \"load\", () => {\n \t\tthrow Error(\"full history replay forbidden\");\n \t});\n-\tconst call = async (op: string, agent: string | undefined, body: unknown) => {\n+\tconst call = async (op: string, agent: string | undefined, body: unknown, requestId?: string) => {\n \t\tfor (let page = 0; page < 100; page++) {\n-\t\t\tconst result = await object().call(\"project\", op, agent, body);\n+\t\t\tconst result = await object().call(\"project\", op, agent, body, false, undefined, undefined, requestId);\n \t\t\tif (result.status !== 503) {\n \t\t\t\tassert.notEqual(result.status, 500, JSON.stringify({ op, result }));\n \t\t\t\treturn result;\n@@ -88,12 +100,114 @@\n \t\t}\n \t\tthrow Error(\"adoption did not finish\");\n \t};\n-\tassert.equal((await call(\"init\", undefined, { files: { \"a.txt\": \"one\\ntwo\\n\" } })).status, 200);\n+\tconst initId = \"00000000-0000-4000-8000-000000000008\";\n+\tconst initBody = { files: { \"a.txt\": \"one\\ntwo\\n\" } };\n+\tfailNamePersistence = true;\n+\tawait assert.rejects(\n+\t\t() => object().call(\"project\", \"init\", undefined, initBody, false, undefined, undefined, initId),\n+\t\t/name persistence failure/,\n+\t);\n+\tconst initializedLog = storage.db.prepare(\"SELECT * FROM log ORDER BY seq,part\").all();\n+\tassert.equal((await call(\"init\", undefined, initBody, initId)).status, 200);\n+\tassert.deepEqual(storage.db.prepare(\"SELECT * FROM log ORDER BY seq,part\").all(), initializedLog);\n \tassert.equal((await call(\"sync\", \"alice\", {})).status, 200);\n-\tassert.equal((await call(\"record\", \"alice\", { files: { \"a.txt\": \"changed\\n\" } })).status, 200);\n+\tconst requestId = \"00000000-0000-4000-8000-000000000007\";\n+\tconst recordBody = { files: { \"a.txt\": \"changed\\n\" } };\n+\tfailNamePersistence = true;\n+\tawait assert.rejects(\n+\t\t() => object().call(\"project\", \"record\", \"alice\", recordBody, false, undefined, undefined, requestId),\n+\t\t/name persistence failure/,\n+\t);\n+\tconst recordedLog = storage.db.prepare(\"SELECT * FROM log ORDER BY seq,part\").all();\n+\tassert.equal(\n+\t\t(await object().call(\"project\", \"record\", \"alice\", recordBody, false, undefined, undefined, requestId)).status,\n+\t\t200,\n+\t);\n+\tassert.deepEqual(storage.db.prepare(\"SELECT * FROM log ORDER BY seq,part\").all(), recordedLog);\n+\tassert.equal(\n+\t\t(\n+\t\t\tawait object().call(\n+\t\t\t\t\"project\",\n+\t\t\t\t\"record\",\n+\t\t\t\t\"alice\",\n+\t\t\t\t{ files: { \"a.txt\": \"different\\n\" } },\n+\t\t\t\tfalse,\n+\t\t\t\tundefined,\n+\t\t\t\tundefined,\n+\t\t\t\trequestId,\n+\t\t\t)\n+\t\t).status,\n+\t\t409,\n+\t);\n \tconst published = await call(\"publish\", \"alice\", { message: \"Disk first\" });\n \tassert.equal(published.status, 200);\n \tassert.equal((published.body as { ok: boolean }).ok, true);\n+\tconst issued = await object().issueAgentKey(\"alice\", \"test-address\", null, \"writer\");\n+\tassert.ok(\"key\" in issued);\n+\tconst keyCall = { presented: issued.key, address: \"test-address\", write: true, route: \"record\" };\n+\tconst crossPrincipal = await object().call(\n+\t\t\"project\",\n+\t\t\"record\",\n+\t\t\"alice\",\n+\t\trecordBody,\n+\t\tfalse,\n+\t\tundefined,\n+\t\tkeyCall,\n+\t\trequestId,\n+\t);\n+\tassert.equal(crossPrincipal.status, 200, \"each checked principal owns its own request ID namespace\");\n+\tconst principalReceipts = storage.db\n+\t\t.prepare(\"SELECT DISTINCT key FROM writer_state WHERE collection = 'runtime:receipts' AND key LIKE ?\")\n+\t\t.all(`http:%:${requestId}`);\n+\tassert.equal(principalReceipts.length, 2);\n+\tconst beforeSharedReplay = storage.db.prepare(\"SELECT * FROM log ORDER BY seq,part\").all();\n+\tassert.equal((await call(\"record\", \"alice\", recordBody, requestId)).status, 200);\n+\tassert.deepEqual(storage.db.prepare(\"SELECT * FROM log ORDER BY seq,part\").all(), beforeSharedReplay);\n+\tconst keyRequestId = \"00000000-0000-4000-8000-000000000010\";\n+\tassert.equal(\n+\t\t(await object().call(\"project\", \"record\", \"alice\", recordBody, false, undefined, keyCall, keyRequestId)).status,\n+\t\t200,\n+\t);\n+\tawait object().revokeAgentKey(issued.id, \"test-address\");\n+\tconst beforeRevokedRetry = storage.db.prepare(\"SELECT * FROM log ORDER BY seq,part\").all();\n+\tconst revokedRetry = await object().call(\n+\t\t\"project\",\n+\t\t\"record\",\n+\t\t\"alice\",\n+\t\trecordBody,\n+\t\tfalse,\n+\t\tundefined,\n+\t\tkeyCall,\n+\t\tkeyRequestId,\n+\t);\n+\tassert.equal(revokedRetry.status, 403);\n+\tassert.deepEqual(storage.db.prepare(\"SELECT * FROM log ORDER BY seq,part\").all(), beforeRevokedRetry);\n+\tconst readerId = \"00000000-0000-4000-8000-000000000009\";\n+\tfailNamePersistence = true;\n+\tawait assert.rejects(\n+\t\t() => object().call(\"project\", \"reader\", \"alice\", { reader: true }, false, undefined, undefined, readerId),\n+\t\t/name persistence failure/,\n+\t);\n+\tconst nowRefused = await object().call(\n+\t\t\"project\",\n+\t\t\"record\",\n+\t\t\"alice\",\n+\t\trecordBody,\n+\t\tfalse,\n+\t\tundefined,\n+\t\tundefined,\n+\t\trequestId,\n+\t);\n+\tassert.equal(\n+\t\t(nowRefused.body as { ok: boolean }).ok,\n+\t\tfalse,\n+\t\t\"current reader authority overrides an earlier successful receipt\",\n+\t);\n+\tassert.equal(\n+\t\t(await object().call(\"project\", \"reader\", \"alice\", { reader: true }, false, undefined, undefined, readerId)).status,\n+\t\t200,\n+\t);\n+\tassert.equal((await call(\"reader\", \"alice\", { reader: false })).status, 200);\n \tconst file = await call(\"file\", undefined, { path: \"a.txt\", version: \"1\" });\n \tassert.equal(file.status, 200);\n \tassert.match(JSON.stringify(file.body), /changed/);\n@@ -165,4 +279,171 @@\n \t\t/injected after record/,\n \t);\n \tassert.deepEqual(rows(), stable);\n+\t// Exercise the real alarm and commit builder; only remote transport is mocked.\n+\tt.mock.method(git, \"listServerRefs\", async () => []);\n+\tt.mock.method(git, \"clone\", async () => {\n+\t\tthrow Error(\"empty test repository\");\n+\t});\n+\tlet pushed = 0;\n+\tlet remoteFiles: MemoryFS | undefined;\n+\tt.mock.method(git, \"push\", async (options: Parameters<typeof git.push>[0]) => {\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+\t\tconst blob = await git.readBlob({ fs: options.fs, dir: options.dir, oid, filepath: \"a.txt\" });\n+\t\tassert.equal(new TextDecoder().decode(blob.blob), \"Alice's change\\n\");\n+\t\tpushed++;\n+\t\tremoteFiles = options.fs as MemoryFS;\n+\t\treturn { ok: true };\n+\t});\n+\tconst artifacts = {\n+\t\tget: async () => ({\n+\t\t\tinfo: async () => ({ remote: \"https://artifacts.invalid/test\" }),\n+\t\t\tcreateToken: async () => ({ plaintext: \"invented-test-token\" }),\n+\t\t\t[Symbol.dispose]() {},\n+\t\t}),\n+\t};\n+\tconst bound = () => new ProjectObject(context as never, { ABSO_DISK_FIRST: \"on\", ARTIFACTS: artifacts } as never);\n+\tawait bound().alarm();\n+\tassert.equal(pushed, 1);\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();\n+\tassert.equal(pushed, 1, \"a cold alarm does not push the same version again\");\n+\tassert.ok(remoteFiles);\n+\tconst remote = remoteFiles;\n+\tconst parent = await git.resolveRef({ fs: remote, dir: \"/repo\", ref: \"main\" });\n+\tconst blob = await git.writeBlob({ fs: remote, dir: \"/repo\", blob: new TextEncoder().encode(\"From Git\\n\") });\n+\tconst tree = await git.writeTree({\n+\t\tfs: remote,\n+\t\tdir: \"/repo\",\n+\t\ttree: [{ mode: \"100644\", path: \"a.txt\", oid: blob, type: \"blob\" }],\n+\t});\n+\tconst incoming = await git.commit({\n+\t\tfs: remote,\n+\t\tdir: \"/repo\",\n+\t\ttree,\n+\t\tparent: [parent],\n+\t\tmessage: \"Plain Git edit\",\n+\t\tauthor: { name: \"Git writer\", email: \"test@example.invalid\" },\n+\t});\n+\tawait git.writeRef({ fs: remote, dir: \"/repo\", ref: \"main\", value: incoming, force: true });\n+\tt.mock.method(git, \"listServerRefs\", async () => [{ ref: \"refs/heads/main\", oid: incoming }]);\n+\tt.mock.method(git, \"clone\", async (options: Parameters<typeof git.clone>[0]) => {\n+\t\t(options.fs as MemoryFS).entries = structuredClone(remote.entries);\n+\t});\n+\tconst beforeIntake = rows();\n+\tconst originalIngest = EngineProject.prototype.ingest;\n+\tconst failedIngest = t.mock.method(\n+\t\tEngineProject.prototype,\n+\t\t\"ingest\",\n+\t\tfunction (this: EngineProject, ...args: Parameters<EngineProject[\"ingest\"]>) {\n+\t\t\toriginalIngest.apply(this, args);\n+\t\t\tthrow Error(\"injected after Git intake\");\n+\t\t},\n+\t);\n+\tawait assert.rejects(() => bound().gitSync(), /injected after Git intake/);\n+\tassert.deepEqual(rows(), beforeIntake);\n+\tassert.equal((await bound().out()).git.head, parent, \"failed intake must not acknowledge the remote head\");\n+\tfailedIngest.mock.restore();\n+\tfailGitAcknowledgement = true;\n+\tawait assert.rejects(() => bound().gitSync(), /Git cursor persistence failure/);\n+\tconst committedIntake = rows();\n+\tassert.equal((await bound().out()).version, 3);\n+\tassert.equal((await bound().out()).git.head, parent);\n+\tconst intake = await bound().gitSync();\n+\tassert.deepEqual(rows(), committedIntake, \"retry after cursor failure returns the atomic intake receipt\");\n+\tassert.ok(intake);\n+\tassert.equal(intake?.head, incoming);\n+\tassert.equal(intake?.commits, 1);\n+\tassert.equal((intake.answer as { landed: boolean }).landed, true, JSON.stringify(intake));\n+\tconst imported = await call(\"file\", undefined, { path: \"a.txt\", version: \"3\" });\n+\tassert.equal(imported.status, 200);\n+\tassert.match(JSON.stringify(imported.body), /From Git/);\n+\tconst afterIntake = rows();\n+\tassert.equal(await bound().gitSync(), null);\n+\tassert.deepEqual(rows(), afterIntake, \"cold intake does not apply the same commit twice\");\n });\n+\n+for (const disk of [false, true]) {\n+\ttest(`Git refusal permits a later fixing commit with disk mode ${disk}`, async (t) => {\n+\t\tconst storage = memoryStorage();\n+\t\tt.after(() => storage.db.close());\n+\t\tconst values = new Map<string, unknown>();\n+\t\tconst context = {\n+\t\t\tid: { toString: () => \"refusal\" },\n+\t\t\tstorage: {\n+\t\t\t\t...storage,\n+\t\t\t\tget: async (key: string) => values.get(key),\n+\t\t\t\tput: async (key: string | Record<string, unknown>, value?: unknown) => {\n+\t\t\t\t\tif (typeof key === \"string\") values.set(key, value);\n+\t\t\t\t\telse for (const [k, v] of Object.entries(key)) values.set(k, v);\n+\t\t\t\t},\n+\t\t\t\tgetAlarm: async () => null,\n+\t\t\t\tsetAlarm: async () => {},\n+\t\t\t},\n+\t\t};\n+\t\tconst artifacts = {\n+\t\t\tget: async () => ({\n+\t\t\t\tinfo: async () => ({ remote: \"https://artifacts.invalid/refusal\" }),\n+\t\t\t\tcreateToken: async () => ({ plaintext: \"invented-test-token\" }),\n+\t\t\t\t[Symbol.dispose]() {},\n+\t\t\t}),\n+\t\t};\n+\t\tconst object = () =>\n+\t\t\tnew ProjectObject(context as never, { ABSO_DISK_FIRST: disk ? \"on\" : \"off\", ARTIFACTS: artifacts } as never);\n+\t\tconst call = async (op: string, body: unknown) => {\n+\t\t\tfor (let page = 0; page < 100; page++) {\n+\t\t\t\tconst result = await object().call(\"refusal\", op, undefined, body);\n+\t\t\t\tif (result.status !== 503) return result;\n+\t\t\t}\n+\t\t\tthrow Error(\"preparation did not finish\");\n+\t\t};\n+\t\tassert.equal((await call(\"init\", { files: { \"a.txt\": \"original\\n\" } })).status, 200);\n+\t\tassert.equal((await call(\"status\", {})).status, 200);\n+\t\tconst remote = new MemoryFS();\n+\t\tawait git.init({ fs: remote, dir: \"/repo\", defaultBranch: \"main\" });\n+\t\tconst commit = async (files: Record<string, string>, message: string, parent: string[]) => {\n+\t\t\tconst entries = [];\n+\t\t\tfor (const [path, text] of Object.entries(files))\n+\t\t\t\tentries.push({\n+\t\t\t\t\tmode: \"100644\",\n+\t\t\t\t\tpath,\n+\t\t\t\t\ttype: \"blob\" as const,\n+\t\t\t\t\toid: await git.writeBlob({ fs: remote, dir: \"/repo\", blob: new TextEncoder().encode(text) }),\n+\t\t\t\t});\n+\t\t\tconst tree = await git.writeTree({ fs: remote, dir: \"/repo\", tree: entries });\n+\t\t\tconst oid = await git.commit({\n+\t\t\t\tfs: remote,\n+\t\t\t\tdir: \"/repo\",\n+\t\t\t\ttree,\n+\t\t\t\tparent,\n+\t\t\t\tmessage,\n+\t\t\t\tauthor: { name: \"Git writer\", email: \"test@example.invalid\" },\n+\t\t\t});\n+\t\t\tawait git.writeRef({ fs: remote, dir: \"/repo\", ref: \"main\", value: oid, force: true });\n+\t\t\treturn oid;\n+\t\t};\n+\t\tconst base = await commit({ \"a.txt\": \"original\\n\" }, \"Initial\\n\\nabso-version: 0\", []);\n+\t\tvalues.set(\"git:head\", base);\n+\t\tlet head = await commit({ \"A.TXT\": \"clash\\n\", \"a.txt\": \"original\\n\" }, \"Refused\", [base]);\n+\t\tt.mock.method(git, \"listServerRefs\", async () => [{ ref: \"refs/heads/main\", oid: head }]);\n+\t\tt.mock.method(git, \"clone\", async (options: Parameters<typeof git.clone>[0]) => {\n+\t\t\t(options.fs as MemoryFS).entries = structuredClone(remote.entries);\n+\t\t});\n+\t\tconst refused = await object().gitSync();\n+\t\tassert.ok(refused);\n+\t\tassert.match(JSON.stringify(refused.answer), /one file|clash/i);\n+\t\tassert.equal(values.get(\"git:head\"), head);\n+\t\tassert.equal(await object().gitSync(), null);\n+\t\thead = await commit({ \"a.txt\": \"fixed\\n\" }, \"Fixed\", [head]);\n+\t\tconst fixed = await object().gitSync();\n+\t\tassert.ok(fixed);\n+\t\tassert.equal((fixed.answer as { landed: boolean }).landed, true, JSON.stringify(fixed));\n+\t\tassert.equal(values.get(\"git:head\"), head);\n+\t\tassert.match(\n+\t\t\tJSON.stringify((await call(\"file\", { path: \"a.txt\", version: String((await object().out()).version) })).body),\n+\t\t\t/fixed/,\n+\t\t);\n+\t});\n+}"}],"unmatched":[]}