#18663: feat: progressive compaction escalation and mechanical flush fallback
agents
stale
size: M
Cluster:
Memory Management Enhancements
Improves compaction and memory flush robustness for sessions near context limits:
- **Progressive escalation:** Overflow compaction retries get increasingly aggressive (maxHistoryShare: 0.4 → 0.25 → 0.1) instead of repeating the same params
- **Headroom pre-check:** Estimates token usage before flush; skips LLM call if insufficient headroom
- **Mechanical fallback:** When LLM flush fails (context overflow), falls back to non-LLM flush that preserves tracking metadata
- **Force compaction:** Flush now triggers immediate compaction to prevent messages sneaking in between flush and compaction
- **Structural fallback summary:** Failed compaction summarization produces a useful structural overview (role counts, tools used) instead of a generic message
- **Oversized chunk handling:** Compaction pre-filters chunks that would exceed model context, preventing summarization failures
All changes are additive — no existing behavior is modified, only new fallback/escalation paths added.
<!-- greptile_comment -->
<h3>Greptile Summary</h3>
This PR adds progressive compaction escalation and mechanical flush fallback paths for sessions near context limits. The changes span 7 files and introduce several resilience mechanisms: headroom pre-checks before LLM flush, mechanical (non-LLM) flush fallback, force compaction after flush, structural fallback summaries for failed compaction, and oversized chunk handling.
- **Critical: Force compaction in `agent-runner.ts` doesn't update `followupRun.run` references** — the new session ID is persisted to the store but `followupRun.run.sessionId` and `followupRun.run.sessionFile` remain stale, causing the subsequent agent turn to target the wrong session. Compare with the existing `resetSession` helper which correctly updates these fields.
- The prior review flagged several issues (escalation share computed but not applied in the first branch, `as any` casts for `forceCompaction`, `setCompactionSafeguardRuntime` replacing rather than merging runtime state, unused imports, misleading JSDoc) — these remain unaddressed in the current revision.
- New `mechanical-flush.ts` is an intentional no-op stub that relies on the `forceCompaction` flag to trigger actual compaction downstream.
- Structural fallback in `compaction-safeguard.ts` and oversized chunk handling in `compaction.ts` are clean additions that improve resilience.
<h3>Confidence Score: 2/5</h3>
- The force compaction session reset bug in agent-runner.ts could cause agents to write to stale sessions, leading to data loss or orphaned sessions in production.
- Score of 2 reflects a critical logic bug where forceCompaction creates a new sessionId in the store but doesn't update the followupRun references used by the subsequent agent turn, plus multiple previously-flagged issues that remain unaddressed (escalation share not applied, runtime clobbering, type safety bypasses).
- Pay close attention to `src/auto-reply/reply/agent-runner.ts` (stale session references after force compaction) and `src/agents/pi-embedded-runner/run.ts` (escalation share computed but not applied in first branch).
<sub>Last reviewed commit: 90b76b1</sub>
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#15322: feat: post-compaction target token trimming + fallback strategy
by echoVic · 2026-02-13
81.9%
#19923: feat: track held messages during compaction gate and split verifica...
by PrivacySmurf · 2026-02-18
81.5%
#9012: fix(memory): resilient flush for large sessions [AI-assisted]
by cheenu1092-oss · 2026-02-04
81.3%
#8919: Pr/memory flush improvements
by shortbus · 2026-02-04
81.0%
#20038: (fix): Compaction: preserve recent context and sync session memory ...
by rodrigouroz · 2026-02-18
80.8%
#15945: fix(memory-flush): only write memoryFlushCompactionCount when compa...
by aldoeliacim · 2026-02-14
80.1%
#14021: feat(compaction): optional memory flush before manual /compact
by phenomenoner · 2026-02-11
79.9%
#14887: feat(compaction): configurable auto-compaction notifications with o...
by seilk · 2026-02-12
79.9%
#9620: fix: increase auto-compaction reserve buffer to 40k tokens
by Arlo83963 · 2026-02-05
79.8%
#8961: feat: smarter compaction tool truncation + token count in system pr...
by SocialNerd42069 · 2026-02-04
79.5%