A local plugin for image-heavy chats in Codex, Claude Code and other agents: earlier images stop going out with every message, and the model fetches the one it needs by its ID, also after compaction.
TypeScript ai-agentsclaude-codeclaude-code-plugincodexcodex-plugincontext-managementdeveloper-toolsimage-generationmcpmultimodal
fix(spike): 分叉任务读不到父任务里的历史;变量传路径的查看图也能认出
chipfighterf305a375bbfe
diff --git a/spike/src/images.test.ts b/spike/src/images.test.tsindex 20df0a9..40a6c89 100644--- a/spike/src/images.test.ts+++ b/spike/src/images.test.ts@@ -73,3 +73,13 @@ test("image sizes come from PNG, GIF and JPEG headers", () => {const jpeg = Buffer.from("ffd8" + "ffe00004" + "0000" + "ffc0000b08012c0190030100", "hex");assert.deepEqual(imageSize(jpeg), { width: 400, height: 300 });});++test("view_image through a variable path is still a viewed image; single-quoted inline paths are read", () => {+ const items = [+ { type: "custom_tool_call", call_id: "c1", name: "exec", input: "const paths = ['C:\\\\a.png'];\nconst calls = paths.map(path => tools.view_image({path, detail: 'original'}));" },+ { type: "custom_tool_call_output", id: "ctco_1", call_id: "c1", output: [{ type: "input_image", image_url: url(red) }] },+ { type: "custom_tool_call", call_id: "c2", name: "exec", input: "await tools.view_image({ path: 'D:\\\\x\\\\e.png' })" },+ { type: "custom_tool_call_output", id: "ctco_2", call_id: "c2", output: [{ type: "input_image", image_url: url(blue) }] },+ ];+ assert.deepEqual(findImages(items).map((r) => [r.kind, r.name]), [["view", null], ["view", "e.png"]]);+});diff --git a/spike/src/images.ts b/spike/src/images.tsindex e7c910f..5445b34 100644--- a/spike/src/images.ts+++ b/spike/src/images.ts@@ -89,10 +89,9 @@ function toolContext(call: Json | undefined, output: Json[]): { kind: ImageKind;return { kind: /imagegen|image_gen/.test(call.name ?? "") ? "generated" : "tool", names: [] };}const source = String(call.input ?? "");- const views = [...source.matchAll(/view_image\(\s*\{\s*path\s*:\s*"((?:[^"\\]|\\.)*)"/g)].map((m) => {- try { return baseName(JSON.parse(`"${m[1]}"`)); } catch { return baseName(m[1]); }- });- if (views.length) return { kind: "view", names: views };+ // Paths written inline are read; paths passed through variables leave the names unknown.+ const views = [...source.matchAll(/view_image\(\s*\{\s*path\s*:\s*(["'])((?:(?!\1)[^\\]|\\.)*)\1/g)].map((m) => baseName(m[2].replace(/\\(.)/g, "$1")));+ if (views.length || source.includes("view_image")) return { kind: "view", names: views };return { kind: /image_gen|imagegen|generatedImage\(/.test(source) ? "generated" : "tool", names: [] };}diff --git a/spike/src/thread-index.test.ts b/spike/src/thread-index.test.tsindex be0c88d..4f97ee4 100644--- a/spike/src/thread-index.test.ts+++ b/spike/src/thread-index.test.ts@@ -61,3 +61,21 @@ test("a later segment inherits its base segment only up to history_base.end_byteline("session_meta", { id: THREAD, history_base: { thread_id: THREAD, end_byte_offset: Buffer.byteLength(first) } }) + turn("t2") + upload("msg_2", "t2", "b.png", blue));assert.deepEqual(loadThreadIndex(dir, THREAD).images.map((i) => [i.id, i.name]), [["IMG-001", "a.png"], ["IMG-002", "b.png"]]);});++test("a fork inherits its parent's history up to the fork point, read from the parent's own file", () => {+ const { dir, day } = sessions();+ const PARENT = "01a0d301-0000-7000-8000-0000000000aa";+ const parentHead = line("session_meta", { id: PARENT }) + turn("t1") + upload("msg_1", "t1", "a.png", red);+ writeFileSync(join(day, `rollout-2026-09-24T09-00-00-${PARENT}.jsonl`), parentHead + turn("t2") + upload("msg_p", "t2", "parent-only.png", blue));+ writeFileSync(join(day, `rollout-2026-09-24T10-00-00-${THREAD}.jsonl`),+ line("session_meta", { id: THREAD, history_base: { thread_id: PARENT, end_byte_offset: Buffer.byteLength(parentHead) } }) + turn("t3") + upload("msg_2", "t3", "b.png", blue));+ assert.deepEqual(loadThreadIndex(dir, THREAD).images.map((i) => [i.id, i.name, i.turn]), [["IMG-001", "a.png", 1], ["IMG-002", "b.png", 2]]);+ assert.deepEqual(loadThreadIndex(dir, PARENT).images.map((i) => i.name), ["a.png", "parent-only.png"], "the parent is unaffected");+});++test("an unnamed image takes the name of an identical named one", () => {+ const { dir, day } = sessions();+ const viewed = line("response_item", { type: "custom_tool_call_output", id: "ctco_1", call_id: "c1", output: [{ type: "input_image", image_url: url(red) }] });+ writeFileSync(join(day, `rollout-2026-09-24T10-00-00-${THREAD}.jsonl`), line("session_meta", { id: THREAD }) + turn("t1") + upload("msg_1", "t1", "a.png", red) + viewed);+ assert.deepEqual(loadThreadIndex(dir, THREAD).images.map((i) => [i.id, i.kind, i.name]), [["IMG-001", "upload", "a.png"], ["IMG-002", "tool", "a.png"]]);+});diff --git a/spike/src/thread-index.ts b/spike/src/thread-index.tsindex 42818d7..72d04a7 100644--- a/spike/src/thread-index.ts+++ b/spike/src/thread-index.ts@@ -52,14 +52,18 @@ function records(file: string): Parsed[] {return cache.records;}-// The thread's segments: the first uses the thread id itself, later pages are named <thread>_<segment>.-function segmentsOf(sessionsDir: string, threadId: string): Map<string, string> {+// Every rollout segment by id: a thread's first segment uses the thread id itself, later pages are named+// <thread>_<segment>. A fork's history_base points into its parent's segments, so bases are looked up globally.+function allSegments(sessionsDir: string): { segments: Map<string, string>; threadOf: Map<string, string> } {const segments = new Map<string, string>();+ const threadOf = new Map<string, string>();for (const file of walk(sessionsDir)) {const match = SEGMENT_NAME.exec(basename(file));- if (match && match[1] === threadId) segments.set(match[2] ?? match[1], file);+ if (!match) continue;+ segments.set(match[2] ?? match[1], file);+ threadOf.set(match[2] ?? match[1], match[1]);}- return segments;+ return { segments, threadOf };}const baseOf = (file: string): Json | null => (records(file)[0]?.type === "session_meta" ? records(file)[0].payload.history_base ?? null : null);@@ -119,14 +123,26 @@ export function buildIndex(threadId: string, history: Parsed[]): ThreadIndex {images.push(entry);byKey.set(ref.key, entry);}+ // An image whose own name is unknown (e.g. viewed through a variable path) takes the name of an identical one.+ for (const entry of images) {+ if (entry.name) continue;+ const twin = images.find((other) => other.name && (other.contentId === entry.contentId || (entry.pixelSha256 !== null && other.pixelSha256 === entry.pixelSha256)));+ if (twin) entry.name = twin.name;+ }return { threadId, images, byKey, turns: turnNumbers.size };}-export function loadThreadIndex(sessionsDir: string, threadId: string): ThreadIndex {- const segments = segmentsOf(sessionsDir, threadId);- if (!segments.size) throw new Error(`no rollout found for thread ${threadId}`);- const bases = new Set([...segments.values()].map((file) => baseOf(file)?.thread_id).filter(Boolean));- const heads = [...segments.keys()].filter((id) => !bases.has(id));+// The thread's model-visible history records, following paginated segments back through history_base.+export function readThreadHistory(sessionsDir: string, threadId: string): Parsed[] {+ const { segments, threadOf } = allSegments(sessionsDir);+ const own = [...segments.keys()].filter((id) => threadOf.get(id) === threadId);+ if (!own.length) throw new Error(`no rollout found for thread ${threadId}`);+ const bases = new Set(own.map((id) => baseOf(segments.get(id)!)?.thread_id).filter(Boolean));+ const heads = own.filter((id) => !bases.has(id));if (heads.length !== 1) throw new Error(`expected one latest rollout segment for ${threadId}, found ${heads.length}`);- return buildIndex(threadId, effective(segments, heads[0]));+ return effective(segments, heads[0]);+}++export function loadThreadIndex(sessionsDir: string, threadId: string): ThreadIndex {+ return buildIndex(threadId, readThreadHistory(sessionsDir, threadId));}