xonecas.io
64875
Mc

mini-coder / commit be9a977

fix(llm): strip runtime tool fields from history

xonecas2026-03-15 be9a9778209fa8056eb452cfb25fe6f3956d3032 parent e1e7f48

4 files changed

src/llm-api/history-transforms.ts+1−0
@@ -11,3 +11,4 @@ export {
1111 stripOpenAIItemIdsFromHistory,
1212 } from "./history/openai.ts";
1313 export { getReasoningDeltaFromStreamChunk } from "./history/reasoning.ts";
14+export { stripToolRuntimeInputFields } from "./history/shared.ts";
src/llm-api/history/shared.ts+58−0
@@ -42,3 +42,61 @@ export function getPartProviderOptions(
4242 export function isToolCallPart(part: unknown): part is Record<string, unknown> {
4343 return isRecord(part) && part.type === "tool-call";
4444 }
45+
46+function hasObjectToolCallInput(
47+ part: unknown,
48+): part is Record<string, unknown> & { input: Record<string, unknown> } {
49+ return (
50+ isToolCallPart(part) &&
51+ "input" in part &&
52+ isRecord(part.input) &&
53+ !Array.isArray(part.input)
54+ );
55+}
56+
57+const TOOL_RUNTIME_INPUT_KEYS = new Set([
58+ "cwd",
59+ "snapshotCallback",
60+ "onOutput",
61+]);
62+
63+export function stripToolRuntimeInputFields(
64+ messages: CoreMessage[],
65+): CoreMessage[] {
66+ let mutated = false;
67+ const result = messages.map((message) => {
68+ if (message.role !== "assistant" || !Array.isArray(message.content)) {
69+ return message;
70+ }
71+
72+ let contentMutated = false;
73+ const nextContent = message.content.map((part) => {
74+ if (!hasObjectToolCallInput(part)) {
75+ return part;
76+ }
77+
78+ let inputMutated = false;
79+ const nextInput = { ...part.input };
80+ for (const key of TOOL_RUNTIME_INPUT_KEYS) {
81+ if (!(key in nextInput)) continue;
82+ delete nextInput[key];
83+ inputMutated = true;
84+ }
85+ if (!inputMutated) return part;
86+ contentMutated = true;
87+ return {
88+ ...part,
89+ input: nextInput,
90+ };
91+ });
92+
93+ if (!contentMutated) return message;
94+ mutated = true;
95+ return {
96+ ...message,
97+ content: nextContent as CoreMessage["content"],
98+ } as CoreMessage;
99+ });
100+
101+ return mutated ? result : messages;
102+}
src/llm-api/turn-prepare-messages.ts+14−6
@@ -3,6 +3,7 @@ import {
33 normalizeOpenAICompatibleToolCallInputs,
44 sanitizeGeminiToolMessagesWithMetadata,
55 stripOpenAIHistoryTransforms,
6+ stripToolRuntimeInputFields,
67 } from "./history-transforms.ts";
78 import { getCacheFamily } from "./provider-options.ts";
89 import type { CoreMessage } from "./turn.ts";
@@ -51,9 +52,15 @@ export function prepareTurnMessages(input: {
5152
5253 const apiLogOn = isApiLogEnabled();
5354
54- // 1. Provider-specific sanitisation
55+ // 1. Strip runtime-only tool input fields before provider-specific sanitisation.
56+ const strippedRuntimeToolFields = stripToolRuntimeInputFields(messages);
57+ if (strippedRuntimeToolFields !== messages && apiLogOn) {
58+ logApiEvent("runtime tool input fields stripped", { modelString });
59+ }
60+
61+ // 2. Provider-specific sanitisation
5562 const geminiResult = sanitizeGeminiToolMessagesWithMetadata(
56- messages,
63+ strippedRuntimeToolFields,
5764 modelString,
5865 toolCount > 0,
5966 );
@@ -83,7 +90,7 @@ export function prepareTurnMessages(input: {
8390 logApiEvent("openai-compatible tool input normalized", { modelString });
8491 }
8592
86- // 2. Context pruning
93+ // 3. Context pruning
8794 const preStats = apiLogOn
8895 ? getMessageDiagnostics(normalised)
8996 : getMessageStats(normalised);
@@ -96,7 +103,7 @@ export function prepareTurnMessages(input: {
96103 : getMessageStats(pruned);
97104 if (apiLogOn) logApiEvent("turn context post-prune", postStats);
98105
99- // 3. Payload compaction
106+ // 4. Payload compaction
100107 const compacted = compactToolResultPayloads(
101108 pruned,
102109 toolResultPayloadCapBytes,
@@ -108,7 +115,7 @@ export function prepareTurnMessages(input: {
108115 });
109116 }
110117
111- // 4. Anthropic prompt caching breakpoints
118+ // 5. Anthropic prompt caching breakpoints
112119 let finalMessages = compacted;
113120 let finalSystemPrompt = systemPrompt;
114121
@@ -120,8 +127,9 @@ export function prepareTurnMessages(input: {
120127 );
121128 finalMessages = annotated.messages;
122129 finalSystemPrompt = annotated.systemPrompt;
123- if (apiLogOn)
130+ if (apiLogOn) {
124131 logApiEvent("Anthropic prompt caching", annotated.diagnostics);
132+ }
125133 }
126134
127135 const wasPruned =
src/llm-api/turn.test.ts+49−0
@@ -6,6 +6,7 @@ import {
66 sanitizeGeminiToolMessages,
77 stripGPTCommentaryFromHistory,
88 stripOpenAIItemIdsFromHistory,
9+ stripToolRuntimeInputFields,
910 } from "./history-transforms.ts";
1011 import type { CoreMessage } from "./turn.ts";
1112 import {
@@ -88,6 +89,54 @@ describe("normalizeOpenAICompatibleToolCallInputs", () => {
8889 });
8990 });
9091
92+describe("stripToolRuntimeInputFields", () => {
93+ test("strips runtime-only tool fields from assistant tool-call inputs", () => {
94+ const messages: CoreMessage[] = [
95+ {
96+ role: "assistant",
97+ content: [
98+ {
99+ type: "tool-call",
100+ toolCallId: "call_1",
101+ toolName: "read",
102+ input: {
103+ path: "TODO.md",
104+ line: 1,
105+ cwd: "/tmp/project",
106+ snapshotCallback: "ignore-me",
107+ onOutput: "ignore-me-too",
108+ },
109+ },
110+ ],
111+ } as unknown as CoreMessage,
112+ ];
113+
114+ const stripped = stripToolRuntimeInputFields(messages);
115+ const part = (stripped[0] as { content: Array<Record<string, unknown>> })
116+ .content[0];
117+ expect(part?.input).toEqual({ path: "TODO.md", line: 1 });
118+ });
119+
120+ test("leaves non-assistant messages and clean tool calls untouched", () => {
121+ const messages: CoreMessage[] = [
122+ { role: "user", content: "hi" },
123+ {
124+ role: "assistant",
125+ content: [
126+ {
127+ type: "tool-call",
128+ toolCallId: "call_2",
129+ toolName: "read",
130+ input: { path: "TODO.md" },
131+ },
132+ ],
133+ } as unknown as CoreMessage,
134+ ];
135+
136+ expect(stripToolRuntimeInputFields(messages)).toBe(messages);
137+ });
138+});
139+
91140 describe("getReasoningDeltaFromStreamChunk", () => {
92141 test("reads SDK reasoning-delta text field", () => {
93142 expect(