#10273: fix(agents): detect and auto-compact mid-run context overflow
app: web-ui
gateway
agents
stale
Cluster:
Context Management Enhancements
## Summary
- **Mid-run context overflow detection**: When context overflow occurs *after* the initial prompt succeeds (during the agent's tool-call loop), it is now detected via `lastAssistantErrorMessage` from subscription events and triggers auto-compaction with retry. Previously only the `promptError` path caught overflow on the initial prompt.
- **Staged summarization fallback**: When the SDK's built-in `session.compact()` fails on oversized contexts, falls back to `summarizeInStages` which chunks messages into smaller pieces that fit within the context window.
- **Empty WS chat final fix**: When the agent uses only `tool_use` blocks or errors before producing text, synthetic assistant events are emitted so webchat clients don't receive `state=final` with no content. Gateway also sends corrective final/error broadcasts as a safety net.
## Changes
| Area | Files | What |
|------|-------|------|
| Runner | `run.ts`, `run/attempt.ts`, `run/types.ts` | Mid-run overflow detection + compaction retry loop |
| Compaction | `compact.ts` | Staged summarization fallback for oversized contexts |
| Subscription | `subscribe.ts`, `handlers.messages.ts`, `handlers.types.ts`, `handlers.lifecycle.ts` | Track `lastAssistantErrorMessage`, emit synthetic assistant events |
| Gateway | `server-chat.ts`, `server-methods/chat.ts`, `server-methods/types.ts`, `server.impl.ts` | `emptyFinalRuns` tracking, corrective final/error broadcasts |
## Test plan
- [x] `pnpm build` — no type errors
- [x] `pnpm check` — lint/format pass
- [x] `pnpm test` — 213 tests pass (35 files)
- [ ] Manual: trigger compaction in a long conversation and verify recovery
🤖 Generated with [Claude Code](https://claude.com/claude-code)
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
- Adds mid-run context overflow detection by propagating the last assistant error message from subscription events into the runner and triggering auto-compaction + retry.
- Extends session compaction with a staged summarization fallback when the SDK’s `session.compact()` fails on oversized contexts.
- Improves WS/webchat behavior for tool_use-only or error-only runs by emitting synthetic assistant events and adding a gateway “corrective final/error” broadcast path.
- Introduces `emptyFinalRuns` tracking in the gateway to detect empty finals and attempt post-hoc correction.
<h3>Confidence Score: 3/5</h3>
- This PR is likely safe to merge after addressing a few correctness issues in the gateway empty-final tracking and staged compaction credentials.
- Core overflow-detection/compaction retry logic is straightforward, but the new gateway correction mechanism can leak state and potentially emit multiple finals for a single run, which can break clients. The staged compaction fallback also has a plausible credential-resolution mismatch that can cause compaction to fail even when an API key is configured.
- src/gateway/server-chat.ts, src/gateway/server-methods/chat.ts, src/agents/pi-embedded-runner/compact.ts
<!-- greptile_other_comments_section -->
<sub>(2/5) Greptile learns from your feedback when you react with thumbs up/down!</sub>
<!-- /greptile_comment -->
Most Similar PRs
#19923: feat: track held messages during compaction gate and split verifica...
by PrivacySmurf · 2026-02-18
84.2%
#18997: fix: improve context overflow error messages and docs
by realhoratiobot · 2026-02-17
83.9%
#20081: feat: post-compaction triage UX — fuzzy ok + stage-2 gate + Discord...
by PrivacySmurf · 2026-02-18
81.7%
#15749: fix: improve context overflow error with diagnostic details
by superlowburn · 2026-02-13
80.1%
#6108: Fix compaction producing empty summary when ctx.model is undefined ...
by GangEunzzang · 2026-02-01
80.0%
#18663: feat: progressive compaction escalation and mechanical flush fallback
by Adamya05 · 2026-02-16
79.5%
#9085: fix: improve stability for terminated responses and telegram retries
by vladdick88 · 2026-02-04
79.3%
#10915: fix: prevent session bloat from oversized tool results and improve ...
by DukeDeSouth · 2026-02-07
79.3%
#8713: feat: gateway memory monitor, install linger, docs and failover
by quratus · 2026-02-04
79.1%
#11999: fix: add session-growth guard to prevent unbounded session store gr...
by reverendrewind · 2026-02-08
78.9%