← Back to PRs

#18663: feat: progressive compaction escalation and mechanical flush fallback

by Adamya05 open 2026-02-16 23:25 View on GitHub →
agents stale size: M
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