Skip to content
This repository was archived by the owner on Jul 13, 2026. It is now read-only.

Commit 529c497

Browse files
fix(cache): remove truncateErroredTools — watermark-gated first-truncation busted prompt cache
Root-caused a two-bust execute turn from wire dumps (MC session ses_331acff9): bust cortexkit#1 was the legitimate m[0] fold; bust cortexkit#2, 11s later on the NEXT pass, was truncateErroredTools first-truncating message[352] — a `read: file not found` tool_result (same tool_use_id both passes, so a genuine in-place mutation): req 555: "...packages/plugin/src/plugin/event-handler.ts" (248B) req 557: "...packages/plugin/src/p... [truncated]" (241B) The function ran every postprocess pass (watermark>0), replacing an errored tool's state.error with `slice(0,100) + "... [truncated]"`. When the age watermark advanced and a tail message first crossed below it, it mutated that already-cached tail message on an uncontrolled pass — busting the whole prefix after it (~570K tokens re-billed) to save 7 bytes. Same class we already killed in stripProcessedImages (efe7cd2), dropStaleReduceCalls (frozen watermark), and the placeholder unification (2b17a59/0cbe847d): a watermark-gated in-place strip whose FIRST application lands on an arbitrary pass. Rather than add a fourth frozen-id guard, removed it entirely — the general drop machinery (ctx_reduce, 2-pass auto-drop, emergency) already reclaims aged errored tool outputs, and it deletes one more stray in-place "[truncated]" wire vocabulary. Removed: the function (strip-content.ts), its postprocess call site + import, the transform-operations re-export, and all its tests. Updated two staleness regression tests that asserted the old truncation to assert the error text is now left byte-identical across passes. Pi unaffected (no caller). Gate: plugin 2091/0, tsc + biome clean. Co-authored-by: Alfonso [Magic Context] <[email protected]>
1 parent dd1c461 commit 529c497

6 files changed

Lines changed: 7 additions & 170 deletions

File tree

‎packages/plugin/src/hooks/magic-context/strip-content.test.ts‎

Lines changed: 0 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
stripProcessedImages,
1010
stripReasoningFromMergedAssistants,
1111
stripSystemInjectedMessages,
12-
truncateErroredTools,
1312
} from "./strip-content";
1413
import type { MessageLike, ThinkingLikePart } from "./tag-messages";
1514

@@ -309,105 +308,6 @@ describe("strip-content", () => {
309308
});
310309
});
311310

312-
describe("truncateErroredTools", () => {
313-
describe("#given tool error parts above and below a watermark", () => {
314-
describe("#when truncating errored tools", () => {
315-
it("#then it truncates only long errors at or below the watermark", () => {
316-
const below = message("m-1", "assistant", [
317-
{
318-
type: "tool",
319-
state: {
320-
status: "error",
321-
error: "e".repeat(200),
322-
},
323-
},
324-
]);
325-
const above = message("m-2", "assistant", [
326-
{
327-
type: "tool",
328-
state: {
329-
status: "error",
330-
error: "f".repeat(200),
331-
},
332-
},
333-
]);
334-
const shortError = message("m-3", "assistant", [
335-
{
336-
type: "tool",
337-
state: {
338-
status: "error",
339-
error: "short",
340-
},
341-
},
342-
]);
343-
const success = message("m-4", "assistant", [
344-
{
345-
type: "tool",
346-
state: {
347-
status: "completed",
348-
output: "done",
349-
},
350-
},
351-
]);
352-
const tags = new Map<MessageLike, number>([
353-
[below, 1],
354-
[above, 10],
355-
[shortError, 2],
356-
[success, 3],
357-
]);
358-
359-
const truncated = truncateErroredTools(
360-
[below, above, shortError, success],
361-
5,
362-
tags,
363-
);
364-
365-
expect(truncated).toBe(1);
366-
const belowPart = below.parts[0] as {
367-
state: { error: string };
368-
};
369-
const abovePart = above.parts[0] as {
370-
state: { error: string };
371-
};
372-
expect(belowPart.state.error.endsWith("[truncated]")).toBe(true);
373-
expect(abovePart.state.error.length).toBe(200);
374-
});
375-
});
376-
});
377-
378-
describe("#given an already truncated long error", () => {
379-
describe("#when truncating errored tools again", () => {
380-
it("#then it is byte-identical after the second pass", () => {
381-
const tool = message("m-1", "assistant", [
382-
{
383-
type: "tool",
384-
state: {
385-
status: "error",
386-
error: "e".repeat(200),
387-
},
388-
},
389-
]);
390-
const tags = new Map<MessageLike, number>([[tool, 1]]);
391-
392-
truncateErroredTools([tool], 5, tags);
393-
const firstPass = JSON.stringify(tool);
394-
truncateErroredTools([tool], 5, tags);
395-
396-
expect(JSON.stringify(tool)).toBe(firstPass);
397-
});
398-
});
399-
});
400-
401-
describe("#given empty messages", () => {
402-
describe("#when truncating errored tools", () => {
403-
it("#then it returns zero", () => {
404-
const tags = new Map<MessageLike, number>();
405-
expect(truncateErroredTools([], 5, tags)).toBe(0);
406-
});
407-
});
408-
});
409-
});
410-
411311
describe("stripProcessedImages (sentinel-based)", () => {
412312
describe("#given user image uploads around assistant responses and watermark boundaries", () => {
413313
describe("#when stripping processed images", () => {

‎packages/plugin/src/hooks/magic-context/strip-content.ts‎

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -420,34 +420,6 @@ export function stripInlineThinking(
420420
return stripped;
421421
}
422422

423-
export function truncateErroredTools(
424-
messages: MessageLike[],
425-
watermark: number,
426-
messageTagNumbers: Map<MessageLike, number>,
427-
): number {
428-
let truncated = 0;
429-
for (let i = 0; i < messages.length; i++) {
430-
const maxTag = messageTagNumbers.get(messages[i]) ?? 0;
431-
if (maxTag > watermark) {
432-
continue;
433-
}
434-
435-
for (const part of messages[i].parts) {
436-
if (!isRecord(part) || part.type !== "tool" || !isRecord(part.state)) {
437-
continue;
438-
}
439-
if (part.state.status !== "error") {
440-
continue;
441-
}
442-
if (typeof part.state.error === "string" && part.state.error.length > 100) {
443-
part.state.error = `${part.state.error.slice(0, 100)}... [truncated]`;
444-
truncated++;
445-
}
446-
}
447-
}
448-
return truncated;
449-
}
450-
451423
// Parts that the AI SDK ignores when converting OpenCode messages to the
452424
// Anthropic request body. Treating them as invisible when deciding whether
453425
// a reasoning part lands at the start of the eventual assistant block.

‎packages/plugin/src/hooks/magic-context/tool-input-preservation.test.ts‎

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { tmpdir } from "node:os";
66
import { join } from "node:path";
77
import { closeDatabase, openDatabase } from "../../features/magic-context/storage";
88
import { createTagger } from "../../features/magic-context/tagger";
9-
import { type MessageLike, tagMessages, truncateErroredTools } from "./transform-operations";
9+
import { type MessageLike, tagMessages } from "./transform-operations";
1010

1111
const tempDirs: string[] = [];
1212
const originalXdgDataHome = process.env.XDG_DATA_HOME;
@@ -65,34 +65,4 @@ describe("tool input preservation", () => {
6565
batch.finalize();
6666
expect(messages).toHaveLength(0);
6767
});
68-
69-
it("preserves errored tool input while still truncating long error text", () => {
70-
const originalInput = { command: "very long payload" };
71-
const messages: MessageLike[] = [
72-
{
73-
info: { id: "m-1", role: "tool", sessionID: "ses-1" },
74-
parts: [
75-
{
76-
type: "tool",
77-
state: {
78-
status: "error",
79-
input: originalInput,
80-
error: "x".repeat(150),
81-
},
82-
},
83-
],
84-
},
85-
];
86-
87-
const messageTagNumbers = new Map<MessageLike, number>([[messages[0], 1]]);
88-
const mutatedCount = truncateErroredTools(messages, 1, messageTagNumbers);
89-
90-
const toolPart = messages[0].parts[0] as {
91-
state: { input?: Record<string, unknown>; error?: string };
92-
};
93-
expect(mutatedCount).toBe(1);
94-
expect(toolPart.state.input).toEqual(originalInput);
95-
expect(toolPart.state.error).toBeDefined();
96-
expect(toolPart.state.error).toContain("... [truncated]");
97-
});
9868
});

‎packages/plugin/src/hooks/magic-context/transform-index-staleness.test.ts‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ function createTestTransform(sessionId: string) {
8080
return { transform, shouldExecute };
8181
}
8282
describe("createTransform index staleness regressions", () => {
83-
it("does not truncate errored tools above watermark after tool-drop pruning", async () => {
83+
it("never truncates errored tool output (truncation removed — it busted prompt cache)", async () => {
8484
useTempDataHome("context-transform-stale-truncate-");
8585
const sessionId = "ses-stale-truncate";
8686
const { transform } = createTestTransform(sessionId);
@@ -242,7 +242,7 @@ describe("createTransform index staleness regressions", () => {
242242
expect(secondPass[2].parts.some((part) => part.type === "file")).toBe(true);
243243
});
244244

245-
it("replays errored-tool truncation and processed-image stripping on defer passes", async () => {
245+
it("replays processed-image stripping on defer passes (errored-tool truncation removed)", async () => {
246246
useTempDataHome("context-transform-defer-watermark-replay-");
247247
const sessionId = "ses-defer-watermark-replay";
248248
const { transform, shouldExecute } = createTestTransform(sessionId);
@@ -286,7 +286,6 @@ describe("createTransform index staleness regressions", () => {
286286

287287
// The processed-image strip first-fires only on a cache-busting (execute)
288288
// pass, which freezes its id; afterwards it replays on every defer pass.
289-
// Errored-tool truncation already fires every pass.
290289
shouldExecute.mockImplementationOnce(() => "execute");
291290
const bustPass = buildMessages();
292291
await transform({}, { messages: bustPass });
@@ -299,7 +298,10 @@ describe("createTransform index staleness regressions", () => {
299298
const filePartAfterReplay = firstDeferPass[0].parts[1];
300299
const toolPartAfterReplay = firstDeferPass[2].parts[0] as ToolPart;
301300
expect(filePartAfterReplay).toEqual({ type: "text", text: "" });
302-
expect(toolPartAfterReplay.state.error).toBe(`${longError.slice(0, 100)}... [truncated]`);
301+
// Errored-tool truncation was REMOVED — the error text must be left
302+
// untouched and byte-identical across passes (its watermark-gated
303+
// first-truncation on an arbitrary pass was a cache-bust source).
304+
expect(toolPartAfterReplay.state.error).toBe(longError);
303305

304306
const secondDeferPass = buildMessages();
305307
await transform({}, { messages: secondDeferPass });

‎packages/plugin/src/hooks/magic-context/transform-operations.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ export {
44
stripClearedReasoning,
55
stripInlineThinking,
66
stripProcessedImages,
7-
truncateErroredTools,
87
} from "./strip-content";
98
export { stripStructuralNoise } from "./strip-structural-noise";
109
export { type MessageLike, type TagTarget, tagMessages } from "./tag-messages";

‎packages/plugin/src/hooks/magic-context/transform-postprocess-phase.ts‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ import {
6767
type MessageLike,
6868
stripProcessedImages,
6969
type TagTarget,
70-
truncateErroredTools,
7170
} from "./transform-operations";
7271
import { logTransformTiming } from "./transform-stage-logger";
7372

@@ -589,11 +588,6 @@ export async function runPostTransformPhase(
589588
if (deferredMaterialize) deferredMaterializedSuccessfully = true;
590589
heuristicsRanSuccessfully = true;
591590
}
592-
if (args.watermark > 0) {
593-
const tWatermarkCleanup = performance.now();
594-
truncateErroredTools(args.messages, args.watermark, args.messageTagNumbers);
595-
logTransformTiming(args.sessionId, "watermarkCleanup", tWatermarkCleanup);
596-
}
597591
if (shouldApplyPendingOps) {
598592
pendingOpsRanSuccessfully = true;
599593
}

0 commit comments

Comments
 (0)