Repository navigation
perf(sessions): strip unused per-message fields from the session context (fixes #307) #454
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔥 The Roast: 🩹 The Fix: Either extend the strip to walk 📏 Severity: warning |
||
| if (message.role !== "assistant") return message; | ||
| const record = message as unknown as Record<string, unknown>; | ||
| 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<string, unknown>; | ||
| 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<string, unknown>; | ||
| 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 | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔥 The Roast: This test asserts the four message-level fields vanish — which is exactly what the implementation does, so it's a tautology wearing a lab coat. It proves the code deletes what the code deletes. Nobody here is surprised, including the code.
🩹 The Fix: Add a fixture line carrying a
thinkingSignatureon a content block and assert it drops (or survives, per the real contract). A test that cannot fail against the spec it claims to encode is just a comment with extra steps.📏 Severity: nitpick