Context compaction race condition with multiple parallel async agents

Resolved 💬 3 comments Opened Jan 1, 2026 by shanemmattner Closed Jan 5, 2026

Bug Report: Context Compaction Race with Multiple Async Agents

Product: Claude Code CLI
Version: Current (Jan 2025)

Description

When multiple background Task agents complete around the same time, context can become critically low without auto-compaction triggering properly. Critically, the /compact command also fails when context is too low, leaving no recovery path except escaping and resuming a previous turn.

Steps to Reproduce

  1. Launch 4+ parallel Task agents with run_in_background: true (e.g., all using Opus model)
  2. Wait for agents to complete (they may complete within seconds of each other)
  3. Observe context warnings when agent completion notifications arrive
  4. Attempt to run /compact command
  5. Observe that /compact also errors out due to low context
  6. Only recovery: press Escape twice to go back to a previous turn where context was still available

Expected Behavior

  • Context should auto-compact when it gets low
  • Each agent completion should check context before adding output
  • /compact command should always work, even with critically low context (it should be the escape hatch)
  • Compaction should happen gracefully without user intervention

Actual Behavior

  • "Context too low" warnings appear
  • Subsequent agent completions also show low context
  • No automatic compaction between rapid completions
  • /compact command fails/errors when context is too low
  • Only workaround is Esc+Esc to return to earlier turn with sufficient context

Interesting Observation (Partial Recovery Works)

The sub-agents completed their work successfully - all git worktrees were created, code was committed, branches exist. When we escaped back a few conversation steps to where context was still available, Claude was able to:

  • See that the agents had completed
  • Check the git worktrees and verify the work
  • Continue as if no error had occurred

So the work was preserved, just the main thread's context management broke. This suggests the agent isolation is working correctly - the bug is specifically in how the main thread handles multiple simultaneous agent completion notifications.

Impact

  • Medium-High: No programmatic recovery when context exhausted by parallel agent returns
  • Users must manually escape and lose recent conversation context
  • However, actual agent work (file changes, commits) is preserved
  • Parallel agent workflows are risky but recoverable

Workaround

  • Press Escape twice to go back to a previous turn where bot still had enough context
  • Check agent outputs manually (git worktrees, commits exist)
  • Continue from earlier turn - work is preserved
  • Stagger agent launches to avoid simultaneous completion
  • Use smaller agent tasks that return less output

Environment

  • macOS Darwin 25.1.0
  • Claude Opus 4.5 (claude-opus-4-5-20251101)
  • 4 parallel Task agents launched simultaneously
  • All agents completed within ~1 minute of each other

View original on GitHub ↗

This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗