Skip to content

fix(agent): recover from provider context overflows - #1838

Merged
wsp1911 merged 1 commit into
GCWing:mainfrom
wsp1911:dev2
Jul 28, 2026
Merged

fix(agent): recover from provider context overflows#1838
wsp1911 merged 1 commit into
GCWing:mainfrom
wsp1911:dev2

Conversation

@wsp1911

@wsp1911 wsp1911 commented Jul 28, 2026

Copy link
Copy Markdown
Collaborator

Summary

Improve context overflow handling with structured provider error detection, bounded recovery compression, and recent-context preservation.

Overflow recovery now remains within the same logical model round while exposing each recovery attempt through lifecycle events and traces.

Type and Areas

Type:

Bug fix

Areas:

Rust core, AI adapters, agent stream, core contracts

Motivation / Impact

Some providers omit token usage, making pre-request context estimates unreliable. An underestimated request could exceed the provider context window and fail without recovery.

This change:

  • detects context overflow using structured HTTP and provider error metadata, with conservative message matching as a fallback
  • retries failed requests after progressively trimming and compressing older context
  • preserves recent exact context on atomic message and tool-call boundaries
  • retains user and Todo anchors when the latest turn is only partially included
  • keeps recovery attempts under one logical model round
  • distinguishes logical round attempts, local retries, and transport attempts in events, logs, and traces
  • preserves the existing local fallback compression budget

Recovery is bounded to avoid unbounded retry loops. Plain HTTP 400 responses without overflow evidence remain classified as invalid requests, and output-token limit errors are excluded from context overflow recovery.

Verification

Passed:

  • pnpm run fmt:rs
  • cargo test -p bitfun-core-types
  • cargo test -p bitfun-ai-adapters
  • cargo test -p bitfun-agent-stream
  • cargo test -p bitfun-core round_executor::tests
  • cargo test -p bitfun-core execution_engine::tests
  • focused compressor tests
  • cargo check --workspace
  • node scripts/check-core-boundaries.mjs
  • git diff HEAD --check

cargo check --workspace reported only pre-existing CLI unused-import and dead-code warnings.

Reviewer Notes

The main behavioral changes are:

  • Main-request overflow recovery is limited to two recovery cycles.
  • Compression overflow handling uses up to four progressively smaller attempts.
  • Recent exact context targets approximately 10K tokens by default.
  • Structured provider status, code, and error type take precedence over message-based classification.
  • Adapter-local transport retries remain separate from logical model-round attempts.

No persistence migration is required.

Checklist

  • This PR is focused and does not include secrets, temporary prompts, generated scratch files, or unrelated artifacts.
  • Relevant verification is recorded above, or skipped checks are explained.
  • User-facing strings, docs, and locales are updated where applicable.

- preserve recent exact context across automatic compression
- retry overflow recovery within the same logical model round
- propagate structured provider errors through adapters and stream processing
- distinguish round attempts from transport retries in traces
@wsp1911
wsp1911 marked this pull request as ready for review July 28, 2026 16:58
@wsp1911
wsp1911 merged commit e37c5f5 into GCWing:main Jul 28, 2026
7 checks passed
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