Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions lib/session-reader.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Copy link
Copy Markdown

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 thinkingSignature on 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

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"),
Expand Down
55 changes: 54 additions & 1 deletion lib/session-reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥 The Roast: stripUnusedAssistantFields only deletes keys off the assistant message object itself. Real omp payloads nest thinkingSignature/textSignature inside each content block — the repo's own fixture tests/fixtures/omp-delta-stream.ndjson shows {"type":"thinking","thinking":"...","thinkingSignature":"..."} as a block, not a message-level field. So the claimed 53%-of-payload inputTransformations savings is real, but the signature bytes sail straight through into the UI because nobody walks message.content. You measured half the field and called it done.

🩹 The Fix: Either extend the strip to walk message.content and drop thinkingSignature/textSignature from thinking/text blocks, or confirm via grep that no renderer reads them and say so explicitly in the comment. If they're genuinely unread, add them to the allowlist in transcriptToMarkdown's assistantThinking/assistantText so the omission is visible rather than silent.

📏 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);
Expand Down Expand Up @@ -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
Expand Down
Loading