Skip to content

Commit 438c55c

Browse files
authored
fix(ai): skip malformed thinking signatures during replay (#123228)
Responses replay parsed persisted thinking signatures without validating their JSON shape, so corrupt or unrelated values could prevent every later turn in a session. Parse signatures in the shared replay owner and accept only reasoning records. Cover malformed syntax, null, arrays, and wrong item types through both provider- and transport-style converters.
1 parent d975d9b commit 438c55c

2 files changed

Lines changed: 36 additions & 6 deletions

File tree

‎packages/ai/src/transports/openai-responses-compaction-replay.test.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,30 @@ function responseMessage(id: string, text: string) {
137137
}
138138

139139
describe("OpenAI Responses compaction replay", () => {
140+
it.each(responseConverters)("$name skips invalid reasoning signatures", ({ convert }) => {
141+
const invalidSignatures = [
142+
["truncated JSON", '{"type":"reasoning"'],
143+
["null", "null"],
144+
["array", "[]"],
145+
["wrong item type", '{"type":"message"}'],
146+
] as const;
147+
148+
for (const [caseName, thinkingSignature] of invalidSignatures) {
149+
const input = convert({
150+
messages: [
151+
createAssistant([
152+
{ type: "thinking", thinking: "invalid", thinkingSignature },
153+
{ type: "text", text: "session continues" },
154+
]),
155+
],
156+
});
157+
158+
expect(input, caseName).toEqual([
159+
expect.objectContaining({ type: "message", role: "assistant" }),
160+
]);
161+
}
162+
});
163+
140164
it("strips only exact compaction checkpoints with structural sharing", () => {
141165
const unchanged = createOutput();
142166
expect(stripCompactionReplayCheckpoint(unchanged)).toBe(unchanged);

‎packages/ai/src/transports/openai-responses-replay-messages-internal.ts‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -362,13 +362,19 @@ function convertResponsesMessagesWithStyle(
362362
block.thinkingSignature &&
363363
(providerStyle || block.thinkingSignature.startsWith("{"))
364364
) {
365-
// Transport conversion skips openai-completions provenance tags; the provider
366-
// conversion retains its shipped parse behavior for every non-empty signature.
367-
const reasoningItem = JSON.parse(
368-
block.thinkingSignature,
369-
) as ReplayableResponseReasoningItem;
365+
// Persisted signatures are provider-owned data. Skip malformed or unrelated
366+
// shapes so one corrupt history item cannot prevent the next request.
367+
let reasoningItem: unknown;
368+
try {
369+
reasoningItem = JSON.parse(block.thinkingSignature);
370+
} catch {
371+
continue;
372+
}
373+
if (!isRecord(reasoningItem) || reasoningItem.type !== "reasoning") {
374+
continue;
375+
}
370376
const replayableReasoningItem = prepareOpenAIResponsesReasoningItemForReplay(
371-
reasoningItem,
377+
reasoningItem as ReplayableResponseReasoningItem,
372378
replayContext,
373379
readOpenAIResponsesReasoningReplayBlockMetadata(isRecord(block) ? block : {}),
374380
providerStyle ? { preserveUnattributedEncryptedContent: true } : undefined,

0 commit comments

Comments
 (0)