Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/image-restore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down
18 changes: 17 additions & 1 deletion tests/image-restore.test.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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<string, IndexedImage[]>([["m00042", [{ mediaType: "image/png", bytes: Buffer.byteLength(PNG, "base64"), width: 1, height: 1, path: p }]]]);
Expand Down
Loading