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(claude): 并行工具调用的结果不再漏编
chipfighterad024fbbeef1
diff --git a/plugin/src/claude-index.ts b/plugin/src/claude-index.tsindex 0453724..5b0eeed 100644--- a/plugin/src/claude-index.ts+++ b/plugin/src/claude-index.ts@@ -95,6 +95,8 @@ export function readTranscript(file: string): ClaudeRecord[] {const isMessage = (record: ClaudeRecord) => (record.type === "user" || record.type === "assistant") && typeof record.uuid === "string";const isBoundary = (record: ClaudeRecord) => record.type === "system" && (record.subtype === "compact_boundary" || !!record.compactMetadata);+const blocksOf = (record: ClaudeRecord): Json[] => (Array.isArray(record.message?.content) ? record.message.content : []);+// The conversation as it stands: from the newest message (not a subagent's sidechain) back to the start.export function activeChain(records: ClaudeRecord[]): ClaudeRecord[] {const byUuid = new Map<string, ClaudeRecord>();@@ -109,7 +111,28 @@ export function activeChain(records: ClaudeRecord[]): ClaudeRecord[] {const parent = record.parentUuid ?? (isBoundary(record) ? record.logicalParentUuid : null);record = parent ? byUuid.get(parent) : undefined;}- return chain.reverse();+ chain.reverse();+ // Parallel tool calls (Claude Code 2.1.293): each result hangs off its own call's record and the conversation goes+ // on from the last result, so the others sit off this chain though Claude Code sends them with it. Each result whose+ // call is on the chain and has no result there joins it right after its call; a result on an abandoned branch+ // answers a call that is not on the chain and stays out.+ const calls = new Set<string>();+ const answered = new Set<string>();+ for (const record of chain) {+ for (const block of blocksOf(record)) {+ if (block?.type === "tool_use") calls.add(String(block.id));+ if (block?.type === "tool_result") answered.add(String(block.tool_use_id));+ }+ }+ const after = new Map<string, ClaudeRecord[]>();+ for (const record of records) {+ if (record.type !== "user" || record.isSidechain || seen.has(record.uuid!) || !record.parentUuid || !seen.has(record.parentUuid)) continue;+ const results = blocksOf(record).filter((block) => block?.type === "tool_result");+ if (results.length === 0 || !results.every((block) => calls.has(String(block.tool_use_id)) && !answered.has(String(block.tool_use_id)))) continue;+ for (const block of results) answered.add(String(block.tool_use_id));+ after.set(record.parentUuid, [...(after.get(record.parentUuid) ?? []), record]);+ }+ return after.size === 0 ? chain : chain.flatMap((record) => [record, ...(after.get(record.uuid!) ?? [])]);}// A prompt the user sent (or a delivery queued as one): not a tool result, not Claude Code's own meta message or adiff --git a/spike/src/claude-fixtures.ts b/spike/src/claude-fixtures.tsindex c1be382..3e20baa 100644--- a/spike/src/claude-fixtures.ts+++ b/spike/src/claude-fixtures.ts@@ -29,7 +29,7 @@ export function transcript() {records,prompt: (content: Json[] | string, extra: Json = {}, parent?: string | null) => add({ type: "user", message: { role: "user", content }, ...extra }, parent),assistant: (block: Json, extra: Json = {}, parent?: string | null) => add({ type: "assistant", message: { role: "assistant", content: [block] }, ...extra }, parent),- toolResult: (toolUseId: string, content: Json[], extra: Json = {}) => add({ type: "user", message: { role: "user", content: [{ type: "tool_result", tool_use_id: toolUseId, content }] }, toolUseResult: {}, ...extra }),+ toolResult: (toolUseId: string, content: Json[], extra: Json = {}, parent?: string | null) => add({ type: "user", message: { role: "user", content: [{ type: "tool_result", tool_use_id: toolUseId, content }] }, toolUseResult: {}, ...extra }, parent),attachment: () => add({ type: "attachment", attachment: { type: "x" } }),boundary: () => add({ type: "system", subtype: "compact_boundary", compactMetadata: {}, logicalParentUuid: last }, null),summary: () => add({ type: "user", isCompactSummary: true, message: { role: "user", content: "summary" } }),diff --git a/spike/src/claude-index.test.ts b/spike/src/claude-index.test.tsindex ff757bd..54432e5 100644--- a/spike/src/claude-index.test.ts+++ b/spike/src/claude-index.test.ts@@ -42,6 +42,38 @@ test("the conversation is the branch the newest message is on; sidechains and atassert.deepEqual(index.images.map((entry) => entry.key), [`${p1}#0`, `${p2}#0`]);});+test("parallel tool calls: a result hanging off its own call is part of the conversation, in call order", () => {+ const t = transcript();+ t.prompt("Read a.png and b.png together, then c.png");+ const useA = t.assistant({ type: "tool_use", id: "toolu_a", name: "Read", input: { file_path: "C:\\work\\a.png" } });+ const useB = t.assistant({ type: "tool_use", id: "toolu_b", name: "Read", input: { file_path: "C:\\work\\b.png" } });+ // Claude Code 2.1.293: each result hangs off its own call, and the conversation goes on from the last one.+ t.toolResult("toolu_a", [image(pngA)], {}, useA);+ t.toolResult("toolu_b", [image(pngB)], {}, useB);+ t.assistant({ type: "tool_use", id: "toolu_c", name: "Read", input: { file_path: "C:\\work\\c.png" } });+ t.toolResult("toolu_c", [image(pngC)]);+ t.assistant(text("Three images."));+ assert.deepEqual(buildClaudeIndex("s", t.records).images.map((entry) => [entry.id, entry.key, entry.name]), [+ ["IMG-001", "tool:toolu_a#0", "a.png"],+ ["IMG-002", "tool:toolu_b#0", "b.png"],+ ["IMG-003", "tool:toolu_c#0", "c.png"],+ ]);+});++test("a tool result left on an abandoned branch stays out", () => {+ const t = transcript();+ t.prompt("first");+ const a1 = t.assistant(text("ok"));+ t.prompt("read d.png");+ const use = t.assistant({ type: "tool_use", id: "toolu_d", name: "Read", input: { file_path: "C:\\work\\d.png" } });+ t.toolResult("toolu_d", [image(pngA)], {}, use);+ t.assistant(text("old answer"));+ // A rewind to the first answer.+ t.prompt("edited", {}, a1);+ t.assistant(text("new answer"));+ assert.deepEqual(buildClaudeIndex("s", t.records).images, []);+});+test("compaction: the chain goes on across the boundary, and images before it are marked compacted", () => {const t = transcript();const p1 = t.prompt([image(pngA), text("first")]);