diff --git a/lib/session-reader.test.mjs b/lib/session-reader.test.mjs index e711c1d67..b7bbe43ee 100644 --- a/lib/session-reader.test.mjs +++ b/lib/session-reader.test.mjs @@ -270,6 +270,49 @@ test("defers historical thinking without changing live-session content", () => { assert.equal(full.messages[1].content[0].thinking, "large reasoning"); }); +test("strips unused per-message fields from assistant messages in the display context (#307)", () => { + const entries = [ + userEntry("u1", null, "start"), + { + ...assistantEntry("a1", "u1", "answer"), + message: { + role: "assistant", + provider: "test", + model: "test-model", + content: [ + // Real payloads nest the signatures inside content blocks too + // (thinkingSignature on thinking blocks, textSignature on text + // blocks — see tests/fixtures/omp-delta-stream.ndjson); the strip + // must drop those as well, not just the message-level fields. + { type: "thinking", thinking: "large reasoning", thinkingSignature: "c2lnbmF0dXJlLWJsb2Nr" }, + { type: "text", text: "answer", textSignature: "c2lnbmF0dXJlLXRleHQ" }, + ], + inputTransformations: [{ type: "thinking_dropped", path: "messages.0.content.0", reason: "prefix_binding_mismatch" }], + thinkingSignature: "c2lnbmF0dXJl", + requestControls: { maxTokens: 100 }, + contextSnapshot: { tokens: 123 }, + }, + }, + ]; + + // Stripped with and without deferThinking: the UI never reads these fields. + for (const options of [undefined, { deferThinking: true }]) { + const context = buildSessionContext(entries, undefined, options); + const assistant = context.messages[1]; + assert.equal("inputTransformations" in assistant, false); + assert.equal("thinkingSignature" in assistant, false); + assert.equal("requestControls" in assistant, false); + assert.equal("contextSnapshot" in assistant, false); + assert.equal("thinkingSignature" in assistant.content[0], false); + assert.equal("textSignature" in assistant.content[1], false); + assert.equal(assistant.content[1].text, "answer"); + assert.equal(assistant.model, "test-model"); + } + // Without deferThinking the thinking text survives the strip. + const full = buildSessionContext(entries); + assert.equal(full.messages[1].content[0].thinking, "large reasoning"); +}); + test("does not defer empty historical thinking blocks", () => { const entries = [ userEntry("u1", null, "start"), diff --git a/lib/session-reader.ts b/lib/session-reader.ts index e2bbe5c16..ae4846918 100644 --- a/lib/session-reader.ts +++ b/lib/session-reader.ts @@ -1176,6 +1176,59 @@ function stripToolResultDetails(message: AgentMessage): AgentMessage { return rest; } +/** + * Per-message fields the history UI never reads dominate the session + * response (measured 2.1 MB of a 3.7 MB context on a 597-message session; + * inputTransformations alone is 53% — #307). They are provider-internal + * (opaque thinking signatures, request controls, context snapshots) and no + * reader in components/, hooks/, lib/ or app/ touches them, so the display + * context drops them. The on-disk session file keeps the full message. + * + * Real payloads also nest the signatures inside content blocks — a thinking + * block carries thinkingSignature and a text block textSignature (see + * tests/fixtures/omp-delta-stream.ndjson) — so the strip walks the blocks + * too. transcriptToMarkdown reads only block.thinking / block.text, never + * the signatures, so dropping them changes no rendered output. + */ +const UNUSED_ASSISTANT_MESSAGE_FIELDS = [ + "inputTransformations", + "thinkingSignature", + "requestControls", + "contextSnapshot", +] as const; + +// Signature fields nested inside assistant content blocks (thinking/text). +const UNUSED_ASSISTANT_BLOCK_FIELDS = ["thinkingSignature", "textSignature"] as const; + +function stripUnusedAssistantFields(message: AgentMessage): AgentMessage { + if (message.role !== "assistant") return message; + const record = message as unknown as Record; + const hasMessageFields = UNUSED_ASSISTANT_MESSAGE_FIELDS.some((field) => record[field] !== undefined); + const content = Array.isArray(message.content) ? message.content : null; + const hasBlockFields = content?.some((block) => { + const b = block as unknown as Record; + return ( + b !== null && + typeof b === "object" && + UNUSED_ASSISTANT_BLOCK_FIELDS.some((field) => b[field] !== undefined) + ); + }); + if (!hasMessageFields && !hasBlockFields) return message; + const stripped = { ...record }; + for (const field of UNUSED_ASSISTANT_MESSAGE_FIELDS) delete stripped[field]; + if (content && hasBlockFields) { + stripped.content = content.map((block) => { + const b = block as unknown as Record; + if (b === null || typeof b !== "object") return block; + if (UNUSED_ASSISTANT_BLOCK_FIELDS.every((field) => b[field] === undefined)) return block; + const copy = { ...b }; + for (const field of UNUSED_ASSISTANT_BLOCK_FIELDS) delete copy[field]; + return copy; + }); + } + return stripped as unknown as AgentMessage; +} + function toolResultImagesAsUrls(message: AgentMessage): AgentMessage { if (message.role !== "toolResult") return message; const content = toolResultContentAsUrls(message.content); @@ -1259,7 +1312,7 @@ export function entryToUiMessage( const normalized = options.deferToolResultImages ? toolResultImagesAsUrls(normalizeToolCalls(raw)) : normalizeToolCalls(raw); - const message = stripToolResultDetails(normalized); + const message = stripUnusedAssistantFields(stripToolResultDetails(normalized)); const displayMessage = options.origin && message.role === "user" ? { ...message, origin: options.origin } : message; if (!options.deferThinking || displayMessage.role !== "assistant") return withNormalizedTimestamp(displayMessage); // Guard like the loader does for bad lines: normalizeToolCalls passes