{"draft":null,"from":175,"to":318,"files":[{"path":"server/test/project-object-disk-first.test.ts","added":211,"removed":4,"diff":"--- a/server/test/project-object-disk-first.test.ts\n+++ b/server/test/project-object-disk-first.test.ts\n@@ -1,9 +1,11 @@\n import assert from \"node:assert/strict\";\n-import { registerHooks } from \"node:module\";\n+import { createRequire, 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 { GitDiskIntake } from \"../src/git-disk-intake.ts\";\n+import { GitIntakeLimitError } from \"../src/git-intake-limit.ts\";\n import { MemoryFS } from \"../src/memory-fs.ts\";\n import { memoryStorage } from \"./helpers.ts\";\n \n@@ -20,6 +22,194 @@\n const { ProjectObject } = await import(\"../src/project-object.ts\");\n hooks.deregister();\n \n+for (const disk of [false, true])\n+\ttest(`failed intake retries across cold objects 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\tlet alarm: number | null = null;\n+\t\tconst context = {\n+\t\t\tid: { toString: () => \"retry\" },\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 [name, item] of Object.entries(key)) values.set(name, item);\n+\t\t\t\t},\n+\t\t\t\tdelete: async (key: string) => values.delete(key),\n+\t\t\t\tgetAlarm: async () => alarm,\n+\t\t\t\tsetAlarm: async (value: number) => {\n+\t\t\t\t\talarm = value;\n+\t\t\t\t},\n+\t\t\t},\n+\t\t};\n+\t\tconst object = (enabled: boolean) =>\n+\t\t\tnew ProjectObject(\n+\t\t\t\tcontext as never,\n+\t\t\t\t{\n+\t\t\t\t\tABSO_DISK_FIRST: disk ? \"on\" : \"off\",\n+\t\t\t\t\tABSO_ARTIFACTS: enabled ? \"on\" : \"off\",\n+\t\t\t\t\tARTIFACTS: {},\n+\t\t\t\t} as never,\n+\t\t\t);\n+\t\tassert.equal((await object(false).call(\"retry\", \"init\", undefined, { files: { \"a.txt\": \"first\\n\" } })).status, 200);\n+\t\tlet ready = false;\n+\t\tfor (let page = 0; page < 100; page++) {\n+\t\t\tif ((await object(false).call(\"retry\", \"status\", undefined, {})).status === 200) {\n+\t\t\t\tready = true;\n+\t\t\t\tbreak;\n+\t\t\t}\n+\t\t}\n+\t\tassert.equal(ready, true);\n+\t\tlet fails = true;\n+\t\tt.mock.method(ProjectObject.prototype, \"gitSync\", async () => {\n+\t\t\tif (fails) throw Error(\"transient intake failure\");\n+\t\t\treturn null;\n+\t\t});\n+\t\tfor (const [index, minutes] of [1, 5, 15, 15].entries()) {\n+\t\t\talarm = null;\n+\t\t\tconst before = Date.now();\n+\t\t\tawait object(true).alarm();\n+\t\t\tassert.equal(values.get(\"git:retry-attempts\"), index + 1);\n+\t\t\tassert.ok(alarm !== null && alarm >= before + minutes * 60000 && alarm <= Date.now() + minutes * 60000);\n+\t\t\tassert.equal(values.get(\"pushed\"), undefined);\n+\t\t}\n+\t\tfails = false;\n+\t\tvalues.set(\"pushed\", { version: Number.MAX_SAFE_INTEGER });\n+\t\talarm = null;\n+\t\tawait object(true).alarm();\n+\t\tassert.equal(values.has(\"git:retry-attempts\"), false);\n+\t\tassert.equal(values.has(\"git:error\"), false);\n+\t});\n+\n+test(\"size refusals survive cold objects, retry changed heads or policy, and prevent outgoing pushes\", async (t) => {\n+\tlet scheduled = 0;\n+\tconst storage = memoryStorage();\n+\tt.after(() => storage.db.close());\n+\tconst values = new Map<string, unknown>();\n+\tconst context = {\n+\t\tid: { toString: () => \"size-refusal\" },\n+\t\tstorage: {\n+\t\t\t...storage,\n+\t\t\tget: async (key: string) => values.get(key),\n+\t\t\tput: async (key: string | Record<string, unknown>, value?: unknown) => {\n+\t\t\t\tif (typeof key === \"string\") values.set(key, value);\n+\t\t\t\telse for (const [name, item] of Object.entries(key)) values.set(name, item);\n+\t\t\t},\n+\t\t\tdelete: async (key: string) => values.delete(key),\n+\t\t\tgetAlarm: async () => null,\n+\t\t\tsetAlarm: async (value: number) => {\n+\t\t\t\tscheduled = value;\n+\t\t\t},\n+\t\t},\n+\t};\n+\tconst artifacts = {\n+\t\tget: async () => ({\n+\t\t\tinfo: async () => ({ remote: \"https://artifacts.invalid/size\" }),\n+\t\t\tcreateToken: async () => ({ plaintext: \"invented-test-token\" }),\n+\t\t\t[Symbol.dispose]() {},\n+\t\t}),\n+\t};\n+\tconst object = (enabled = true) =>\n+\t\tnew ProjectObject(\n+\t\t\tcontext as never,\n+\t\t\t{\n+\t\t\t\tABSO_DISK_FIRST: \"on\",\n+\t\t\t\tABSO_ARTIFACTS: enabled ? \"on\" : \"off\",\n+\t\t\t\tARTIFACTS: artifacts,\n+\t\t\t} as never,\n+\t\t);\n+\tassert.equal(\n+\t\t(await object(false).call(\"size-refusal\", \"init\", undefined, { files: { \"a.txt\": \"original\\n\" } })).status,\n+\t\t200,\n+\t);\n+\tlet ready = false;\n+\tfor (let page = 0; page < 100; page++) {\n+\t\tconst result = await object(false).call(\"size-refusal\", \"status\", undefined, {});\n+\t\tif (result.status === 200) {\n+\t\t\tready = true;\n+\t\t\tbreak;\n+\t\t}\n+\t\tassert.equal(result.status, 503);\n+\t}\n+\tassert.equal(ready, true, \"project indexing must finish before testing intake refusals\");\n+\tlet head = \"a\".repeat(40),\n+\t\tdownloads = 0,\n+\t\tpushes = 0,\n+\t\ttransient = false;\n+\tt.mock.method(git, \"listServerRefs\", async () => [{ ref: \"refs/heads/main\", oid: head }]);\n+\tt.mock.method(git, \"clone\", async () => {\n+\t\tdownloads++;\n+\t\tif (transient) throw Error(\"temporary network failure\");\n+\t\tthrow new GitIntakeLimitError(\"decoded\", \"Git pack exceeds total decoded byte limit\");\n+\t});\n+\tt.mock.method(git, \"push\", async () => {\n+\t\tpushes++;\n+\t\treturn { ok: true };\n+\t});\n+\tawait assert.rejects(() => object().gitSync(), /decoded byte limit/);\n+\tawait assert.rejects(() => object().gitSync(), /decoded byte limit/);\n+\tassert.equal(downloads, 1);\n+\tassert.equal(values.get(\"git:head\"), undefined);\n+\tassert.equal((await object().out()).version, 0);\n+\tassert.match(JSON.stringify((await object().out()).git.error), /decoded byte limit/);\n+\tawait object().alarm();\n+\tassert.ok(scheduled >= Date.now() + 59_000);\n+\tassert.equal(values.get(\"git:retry-attempts\"), 1);\n+\tassert.equal(downloads, 1);\n+\tassert.equal(pushes, 0);\n+\thead = \"b\".repeat(40);\n+\tawait assert.rejects(() => object().gitSync(), /decoded byte limit/);\n+\tassert.equal(downloads, 2);\n+\tvalues.set(\"git:size-refusal\", { ...(values.get(\"git:size-refusal\") as object), policy: \"old-policy\" });\n+\tawait assert.rejects(() => object().gitSync(), /decoded byte limit/);\n+\tassert.equal(downloads, 3);\n+\thead = \"c\".repeat(40);\n+\ttransient = true;\n+\tawait assert.rejects(() => object().gitSync(), /temporary network failure/);\n+\tawait assert.rejects(() => object().gitSync(), /temporary network failure/);\n+\tassert.equal(downloads, 5);\n+\tassert.equal(values.has(\"git:size-refusal\"), false);\n+\tawait object().alarm();\n+\tassert.equal(pushes, 0);\n+\tassertGitScratchEmpty(storage);\n+});\n+\n+function assertGitScratchEmpty(storage: ReturnType<typeof memoryStorage>) {\n+\tfor (const table of [\n+\t\t\"git_pack_spool\",\n+\t\t\"git_pack_resolve\",\n+\t\t\"git_object_meta\",\n+\t\t\"git_object_bytes\",\n+\t\t\"git_object_budget\",\n+\t])\n+\t\tassert.equal(storage.db.prepare(`SELECT COUNT(*) AS n FROM ${table}`).get()!.n, 0, table);\n+}\n+\n+/** Keep transport synthetic while exercising the real disk pack decoder and cache. */\n+async function cloneFixture(remote: MemoryFS, options: Parameters<typeof git.clone>[0]) {\n+\tconst intake = (options.cache as Record<symbol, GitDiskIntake> | undefined)?.[Symbol.for(\"abso.git.disk-pack\")];\n+\tif (!intake) {\n+\t\t(options.fs as MemoryFS).entries = structuredClone(remote.entries);\n+\t\treturn;\n+\t}\n+\tconst oids = [...remote.entries.keys()].flatMap((path) => {\n+\t\tconst match = /\\/objects\\/([0-9a-f]{2})\\/([0-9a-f]{38})$/.exec(path);\n+\t\treturn match ? [match[1]! + match[2]!] : [];\n+\t});\n+\tconst pack = (await git.packObjects({ fs: remote, dir: \"/repo\", oids, write: false })).packfile!;\n+\tconst { Inflate } = createRequire(import.meta.url)(\"pako\");\n+\tawait intake.ingest([pack], () => new Inflate({ chunkSize: 16384 }));\n+\tawait git.init({ fs: options.fs, dir: options.dir, defaultBranch: \"main\" });\n+\tawait git.writeRef({\n+\t\tfs: options.fs,\n+\t\tdir: options.dir,\n+\t\tref: \"main\",\n+\t\tvalue: await git.resolveRef({ fs: remote, dir: \"/repo\", ref: \"main\" }),\n+\t});\n+}\n+\n test(\"empty disk projects and unloadable legacy projects preserve lookup fallbacks\", async (t) => {\n \tconst storage = memoryStorage();\n \tt.after(() => storage.db.close());\n@@ -298,7 +488,16 @@\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\t// The remote owns its copy; the export's scratch files expire when push returns.\n+\t\tremoteFiles = new MemoryFS();\n+\t\tfor (const [path, entry] of (options.fs as MemoryFS).entries) {\n+\t\t\tremoteFiles.entries.set(\n+\t\t\t\tpath,\n+\t\t\t\tentry.kind === \"file\"\n+\t\t\t\t\t? { ...entry, data: new Uint8Array((await (options.fs as MemoryFS).readFile(path)) as Uint8Array) }\n+\t\t\t\t\t: { ...entry },\n+\t\t\t);\n+\t\t}\n \t\treturn { ok: true };\n \t});\n \tconst artifacts = {\n@@ -344,9 +543,14 @@\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\tawait cloneFixture(remote, options);\n \t});\n \tconst beforeIntake = rows();\n+\tconst originalRead = GitDiskIntake.prototype.read;\n+\tt.mock.method(GitDiskIntake.prototype, \"read\", function (this: GitDiskIntake, oid: string) {\n+\t\tif (oid === blob) throw Error(\"whole intake text blob forbidden\");\n+\t\treturn originalRead.call(this, oid);\n+\t});\n \tconst originalIngest = EngineProject.prototype.ingest;\n \tconst failedIngest = t.mock.method(\n \t\tEngineProject.prototype,\n@@ -357,15 +561,18 @@\n \t\t},\n \t);\n \tawait assert.rejects(() => bound().gitSync(), /injected after Git intake/);\n+\tassertGitScratchEmpty(storage);\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+\tassertGitScratchEmpty(storage);\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+\tassertGitScratchEmpty(storage);\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@@ -443,7 +650,7 @@\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\tawait cloneFixture(remote, options);\n \t\t});\n \t\tconst refused = await object().gitSync();\n \t\tassert.ok(refused);"}],"unmatched":[]}