Skip to content

fix(session): classify context overflow from generic errors before unknown fallback#33667

Open
Haohao-end wants to merge 1 commit into
anomalyco:devfrom
Haohao-end:overflow-error-classify
Open

fix(session): classify context overflow from generic errors before unknown fallback#33667
Haohao-end wants to merge 1 commit into
anomalyco:devfrom
Haohao-end:overflow-error-classify

Conversation

@Haohao-end

Copy link
Copy Markdown

Issue for this PR

Closes #33376

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

When a provider returns a context-length error wrapped in a generic Error (not an APICallError), fromError() in message-v2.ts classifies it as UnknownError instead of ContextOverflowError. This means the processor never triggers auto-compaction — the session dies with "Network connection lost." and no recovery is possible.

The root cause is the case e instanceof Error: branch in fromError() — it returns NamedError.Unknown without checking whether the error message indicates context overflow. The default branch does call ProviderError.parseStreamError() which can detect overflow, but generic Error instances never reach it.

This PR adds an isContextOverflow(msg) check before the unknown fallback. isContextOverflow already exists in @opencode-ai/llm with 18+ patterns covering messages from OpenAI, Anthropic, Bedrock, and others — so any provider whose overflow error lands as a plain Error now gets correctly classified, and the processor sets needsCompaction = true instead of killing the session.

How did you verify your code works?

  • bun typecheck from packages/opencode — passes
  • bun test test/session/message-v2.test.ts --test-name-pattern "fromError" — 12/12 pass
  • Added two new tests:
    • "detects context overflow from generic Error messages" — 5 real-world overflow strings all classify as ContextOverflowError
    • "does not classify generic non-overflow Error as context overflow" — "Network connection lost." still classifies as UnknownError

Screenshots / recordings

N/A — logic change only.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

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.

[Network connection lost] due to Context overflow at model limit misclassified — no compaction, no retry, session dies!!

1 participant