diff --git a/src/image-restore.ts b/src/image-restore.ts index fdac387535..cf6fbd8b0f 100644 --- a/src/image-restore.ts +++ b/src/image-restore.ts @@ -17,7 +17,7 @@ // sends no such bytes to the proxy, so those belong to the agent's in-process // recovery (cf. billion-context-pi#594), not here. -import { existsSync, mkdirSync, readdirSync, rmdirSync, statSync, unlinkSync, writeFileSync } from "node:fs"; +import { mkdirSync, readdirSync, rmdirSync, statSync, unlinkSync, writeFileSync } from "node:fs"; import { createHash } from "node:crypto"; import { join } from "node:path"; import { @@ -438,7 +438,8 @@ export function writeRestoredImage(ref: string, idx: number, img: RestorableImag const digest = createHash("sha256").update(body).digest("hex").slice(0, 8); const path = join(dir, `${safeRef}${idx > 0 ? `-${idx}` : ""}-${digest}.${extFor(img.mediaType)}`); try { - if (existsSync(path)) return path; + const existing = statSync(path, { throwIfNoEntry: false }); + if (existing?.isFile() && existing.size === body.length) return path; mkdirSync(dir, { recursive: true }); writeFileSync(path, body, { mode: 0o600 }); return path; diff --git a/tests/image-restore.test.ts b/tests/image-restore.test.ts index d0611aea23..c846a5c146 100644 --- a/tests/image-restore.test.ts +++ b/tests/image-restore.test.ts @@ -1,6 +1,6 @@ import test from "node:test"; import assert from "node:assert/strict"; -import { existsSync, mkdtempSync, readFileSync, statSync } from "node:fs"; +import { existsSync, mkdtempSync, readFileSync, statSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { anthropicToCore, googleToCore, openaiToCore, responsesToCore } from "acp-kernel/wire"; @@ -191,6 +191,22 @@ test("writeRestoredImage: a rebase that restarts refs never restores the previou assert.equal(writeRestoredImage("m00001", 0, pngImg, "sess-rebase"), gen1); }); +for (const length of [0, 8]) { + test(`image index repairs an incomplete ${length}-byte spill when the original is resent`, () => { + const body = { model: "gpt", messages: [{ role: "user", content: [{ type: "image_url", image_url: { url: dataUrl } }] }] }; + const sessionId = `sess-incomplete-${length}`; + const original = buildIndex("openai", body, false, sessionId); + const file = [...original.values()][0][0].path; + writeFileSync(file, Buffer.from(PNG, "base64").subarray(0, length)); + const repaired = buildIndex("openai", body, false, sessionId); + assert.equal([...repaired.values()][0][0].path, file, "stable refs retain the same spill path"); + assert.deepEqual(readFileSync(file), Buffer.from(PNG, "base64"), "resending the original must recover a partial write"); + const mtime = statSync(file).mtimeMs; + buildIndex("openai", body, false, sessionId); + assert.equal(statSync(file).mtimeMs, mtime, "complete files retain the skip-write fast path"); + }); +} + test("resolveDecompress({ imageRef }): lists, restores to file, and reports misses", () => { const p = writeRestoredImage("m00042", 0, pngImg, "sess-A")!; const idx = new Map([["m00042", [{ mediaType: "image/png", bytes: Buffer.byteLength(PNG, "base64"), width: 1, height: 1, path: p }]]]);