Description
Hey, spent some time digging into a session crash and traced it to what I think is a gap in the error classification path. Wanted to share findings in case it helps.
What happened
Long-running session (heavy tool-calling, sequential thinking) hit the model's context window limit. The next request exceeded it, provider returned a context-length error. Instead of triggering auto-compaction, OpenCode classified the error as "Network connection lost." (an UnknownError) and killed the session immediately.
The error is not retryable and doesn't trigger compaction — so the session is just dead with no way to recover.
Why it happens
The issue is in fromError() in packages/opencode/src/session/message-v2.ts. There's a catch-all at the end:
case e instanceof Error:
return new NamedError.Unknown({ message: errorMessage(e) }, { cause: e }).toObject()
When the provider returns a context-length error wrapped in a generic Error (not an APICallError), it hits this case instead of being recognized as a context overflow. The function never reaches ProviderError.parseStreamError() because the error is instanceof Error.
Then in retry.ts, retryable() only handles APIError — so NamedError.Unknown gets no retry, and the processor calls halt().
Repro
- Long session with a model that has a defined context limit (mine was mimo-v2.5-free @ 1,048,576 tokens)
- Heavy tool-calling workflow that grows context incrementally
- Continue until you're right at the limit
- Send one more message — session dies with
"Network connection lost."
Suggested fix
Before the case e instanceof Error: catch-all, detect context overflow patterns:
case e instanceof Error: {
const msg = errorMessage(e)
if (/context.*(length|window|limit|exceed)|token.*limit|input.*too.*long|maximum.*context/i.test(msg)) {
return new ContextOverflowError(
{ message: msg },
{ cause: e },
).toObject()
}
return new NamedError.Unknown({ message: msg }, { cause: e }).toObject()
}
This way context overflow from any provider gets classified properly and triggers auto-compaction.
Related issues I found while digging
Some of these might be the same root cause showing up differently:
Environment
- OpenCode v1.15.7 (crash), v1.15.11 (retry), v1.17.9 (current — bug still present)
- mimo-v2.5-free via OpenCode Zen (1,048,576 token context)
- Windows 11
Not sure if this is the right place for all of this or if some of these should be separate issues. Happy to split or combine however makes sense for the team.
Plugins
custom framework
OpenCode version
1.17.9
Steps to reproduce
-
Start a long-running session with a model that has a defined context window limit (e.g., mimo-v2.5-free with 1,048,576 token limit)
-
Use a heavy tool-calling workflow — sequential thinking, file operations, web searches — anything that incrementally adds tokens to the context each turn
-
Keep going until the session's total tokens are right at the model's limit. In my case, the session reached 1,048,572 tokens (4 below the 1,048,576 ceiling)
-
Send one more message that would push the total over the limit
-
Expected: OpenCode triggers auto-compaction, reduces context, retries the request
-
Actual: Session dies immediately with error "Network connection lost." — no compaction, no retry, session unrecoverable
The error shows up in the session data as:
{
"name": "UnknownError",
"data": {
"message": "\"Network connection lost.\""
}
}
This is a context_length_exceeded error from the provider that got misclassified by fromError() in message-v2.ts.
Screenshot and/or share link
No response
Operating System
Windows 11
Terminal
Windows Terminal
Description
Hey, spent some time digging into a session crash and traced it to what I think is a gap in the error classification path. Wanted to share findings in case it helps.
What happened
Long-running session (heavy tool-calling, sequential thinking) hit the model's context window limit. The next request exceeded it, provider returned a context-length error. Instead of triggering auto-compaction, OpenCode classified the error as
"Network connection lost."(anUnknownError) and killed the session immediately.The error is not retryable and doesn't trigger compaction — so the session is just dead with no way to recover.
Why it happens
The issue is in
fromError()inpackages/opencode/src/session/message-v2.ts. There's a catch-all at the end:When the provider returns a context-length error wrapped in a generic
Error(not anAPICallError), it hits this case instead of being recognized as a context overflow. The function never reachesProviderError.parseStreamError()because the error isinstanceof Error.Then in
retry.ts,retryable()only handlesAPIError— soNamedError.Unknowngets no retry, and the processor callshalt().Repro
"Network connection lost."Suggested fix
Before the
case e instanceof Error:catch-all, detect context overflow patterns:This way context overflow from any provider gets classified properly and triggers auto-compaction.
Related issues I found while digging
Some of these might be the same root cause showing up differently:
provider_unavailableerrors from OpenRouter are not retried, causing subagent/session aborts #22448 — 502provider_unavailableerrors not retried. Same error classification gap but for a different error type. The numericcodecheck inretry.ts(typeof json.code === "string") doesn't handle numeric codes from OpenRouter.TypeError: terminatedon Windows. Also falls through toNamedError.UnknowninfromError(). Same catch-all gap.bug+core). Probably the same thing.Environment
Not sure if this is the right place for all of this or if some of these should be separate issues. Happy to split or combine however makes sense for the team.
Plugins
custom framework
OpenCode version
1.17.9
Steps to reproduce
Start a long-running session with a model that has a defined context window limit (e.g., mimo-v2.5-free with 1,048,576 token limit)
Use a heavy tool-calling workflow — sequential thinking, file operations, web searches — anything that incrementally adds tokens to the context each turn
Keep going until the session's total tokens are right at the model's limit. In my case, the session reached 1,048,572 tokens (4 below the 1,048,576 ceiling)
Send one more message that would push the total over the limit
Expected: OpenCode triggers auto-compaction, reduces context, retries the request
Actual: Session dies immediately with error "Network connection lost." — no compaction, no retry, session unrecoverable
The error shows up in the session data as:
This is a context_length_exceeded error from the provider that got misclassified by fromError() in message-v2.ts.
Screenshot and/or share link
No response
Operating System
Windows 11
Terminal
Windows Terminal