Skip to content

fix: rebuild stream context after sanitizing retry history#36

Merged
sichengchen merged 2 commits intomainfrom
fix/rebuild-stream-context-after-retry-sanitize
Mar 1, 2026
Merged

fix: rebuild stream context after sanitizing retry history#36
sichengchen merged 2 commits intomainfrom
fix/rebuild-stream-context-after-retry-sanitize

Conversation

@sichengchen
Copy link
Owner

Summary

  • When a retryable provider error occurs in Agent.chat(), sanitizeHistoryForRetry() assigns a new array to this.messages, but the context object passed to stream() still held a reference to the old array. Retries resent the unsanitized history, so errors like Gemini thought_signature failures repeated on every attempt instead of recovering.
  • Added context.messages = this.messages after sanitization so retries use the cleaned history.
  • Added regression test verifying the retry receives sanitized messages.

Test plan

  • bun test src/engine/agent/agent.test.ts — 4/4 pass (including new regression test)
  • bun run typecheck — no new errors

🤖 Generated with Claude Code

When a retryable provider error occurs, sanitizeHistoryForRetry assigns
a new array to this.messages, but the retry loop kept reusing the context
object created before the loop. This meant context.messages still pointed
to the old unsanitized array, so retries resent the same malformed history.

Update context.messages after sanitization so retries use the cleaned
history. Adds regression test.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4cecc98250

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

stream: async function* (_model: any, context: any) {
callCount++;
// Capture a snapshot of context.messages for each call
capturedMessages.push([...context.messages]);

Choose a reason for hiding this comment

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

P2 Badge Assert retry uses updated context array

capturedMessages.push([...context.messages]) snapshots message contents instead of preserving the array reference, and the test later only checks for errorMessage fields. In this code path the retryable provider error is not appended to this.messages before sanitization (agent.ts keeps only the user message), so sanitized and unsanitized histories are content-identical; this test will pass even if context.messages = this.messages is removed, leaving the stale-reference bug unguarded.

Useful? React with 👍 / 👎.

The previous test snapshot content with spread, which was identical
before and after sanitization (the error is never pushed to history).
Now captures the raw array reference and asserts the retry receives a
different reference, which only passes when context.messages is
reassigned after sanitizeHistoryForRetry.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@sichengchen
Copy link
Owner Author

@codex

@chatgpt-codex-connector
Copy link

Codex Review: Didn't find any major issues. Another round soon, please!

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@sichengchen sichengchen merged commit 2aafde6 into main Mar 1, 2026
2 checks passed
@sichengchen sichengchen deleted the fix/rebuild-stream-context-after-retry-sanitize branch March 1, 2026 06:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant