Skip to content

Sub-agents hang indefinitely on context overflow — no compaction triggered #25187

Closed as not planned

Description

@Krypto-Whitehat

Bug Description

Sub-agents (spawned via the task tool) can hang indefinitely when their context window overflows. The main agent recovers from overflow via auto-compaction, but sub-agents do not — they silently fail or retry endlessly, causing the parent agent to wait for days.

Reproduction Steps

  1. Configure a provider that doesn't report accurate token counts or context limits (e.g., z.ai / GLM models)
  2. Delegate a task to a sub-agent (e.g., WHITEHAT-IMPLEMENTOR) that requires many tool calls (reads, searches, etc.)
  3. The sub-agent's context grows until it exceeds the model's context window
  4. The provider either silently accepts the overflow or returns a non-standard error
  5. The sub-agent hangs — the main agent waits indefinitely

Expected Behavior

The sub-agent should auto-compact its context (same as the main agent) and continue working, or return an error to the parent agent so it can retry or adjust.

Actual Behavior

The sub-agent hangs for hours/days because:

  • Overflow detection (isOverflow()) relies on API-reported token counts which are inaccurate for some providers
  • No proactive check exists before the API call — overflow is only detected AFTER the stream finishes
  • Some providers (z.ai) accept overflows silently without returning errors or accurate usage stats
  • Unrecognized overflow errors get classified as generic API errors → retry loop instead of compaction

Environment

  • Provider: z.ai (OpenAI-compatible, GLM-5V-Turbo model)
  • Model context: ~200k tokens
  • OpenCode: latest dev branch
  • OS: Windows 11

Root Cause Analysis

Three gaps in the current implementation:

  1. session/processor.ts: Overflow detection is purely reactive — only checks token counts from the API response AFTER the stream finishes. Some providers accept overflows silently, so the processor never detects the overflow.

  2. provider/error.ts: Not all provider-specific overflow error messages match the existing patterns. z.ai/GLM errors are partially handled (model_context_window_exceeded exists) but variants like token limit exceeded are missed.

  3. session/overflow.ts: When a model doesn't report context limits (context === 0), overflow detection is disabled entirely. This is by design but compounds the problem for providers with incomplete model metadata.

Activity

  1. added
    coreAnything pertaining to core functionality of the application (opencode server stuff)
    on May 1, 2026
  2. github-actions commented on May 1, 2026

    @github-actions
    Contributor

    This issue might be a duplicate of existing issues. Please check:

    If your issue is distinct from all of the above (e.g. specifically about sub-agents not inheriting compaction behavior, rather than the provider-level overflow classification), please clarify how it differs so maintainers can track it separately.

  3. vteran93 commented on May 2, 2026

    @vteran93

    I am experiencing something similar, if you are using z.ai coding plan alone without chat gpt or github copilot subscription the spawn agents die in the very beginning and they do not perform their tasks.

  4. removed
    coreAnything pertaining to core functionality of the application (opencode server stuff)
    on May 3, 2026
  5. Krypto-Whitehat commented on May 3, 2026

    @Krypto-Whitehat
    Author

    I am experiencing something similar, if you are using z.ai coding plan alone without chat gpt or github copilot subscription the spawn agents die in the very beginning and they do not perform their tasks.

    It has not really to do with ZAI GLM Modells, it has likely more to do how OpenCode handle some AI Model calls...
    I have the same issue with ChatGPT OpenAI API Calls through Sub Agents, Sub Agents don t get compress/Compact automatically how they should do normally like the main agent do, its likely more that issue.

  6. github-actions commented on Jul 4, 2026

    @github-actions
    Contributor

    To stay organized issues are automatically closed after 60 days of no activity. If the issue is still relevant please open a new one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions