Skip to content

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

Description

@AdityaPagare619

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

  1. Long session with a model that has a defined context limit (mine was mimo-v2.5-free @ 1,048,576 tokens)
  2. Heavy tool-calling workflow that grows context incrementally
  3. Continue until you're right at the limit
  4. 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

  1. 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)

  2. Use a heavy tool-calling workflow — sequential thinking, file operations, web searches — anything that incrementally adds tokens to the context each turn

  3. 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)

  4. Send one more message that would push the total over the limit

  5. Expected: OpenCode triggers auto-compaction, reduces context, retries the request

  6. 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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions