#10457: fix(compaction): abort compaction when summarization fails instead of truncating history (#10332)
scripts
agents
stale
Cluster:
Compaction Safeguards and Summaries
## Summary
Fixes #10332 — Compaction truncates history even when summary generation fails, leaving only the useless fallback text "Summary unavailable due to context limits."
## Root Cause
In `compaction-safeguard.ts`, three failure paths (no model, no API key, summarization exception) all returned a compaction result with `firstKeptEntryId`, which told the framework to proceed with truncation despite having no meaningful summary. This caused catastrophic context loss ("amnesia").
## Fix
Return `{ cancel: true }` in all three failure paths instead of a fallback compaction result. The pi-coding-agent framework already supports `cancel` in `SessionBeforeCompactResult` — when set, it throws `"Compaction cancelled"` and preserves the full session history.
### Changed paths:
- **No model** → `{ cancel: true }` + warning log
- **No API key** → `{ cancel: true }` + warning log
- **Summarization throws** → `{ cancel: true }` + warning log (was: "truncating history")
## Tests
### Unit tests (3 regression tests)
1. Cancels compaction when model is unavailable
2. Cancels compaction when API key is unavailable
3. Cancels compaction when summarization throws (via `vi.spyOn` mock)
### Live test (LIVE=1)
- Added a live integration test in `compaction-safeguard.test.ts` that calls a real LLM to verify compaction produces a meaningful summary (not cancel) when model + API key are available.
- Skipped automatically when `LIVE=1` is not set or API key is missing.
- Verified with `claude-haiku-4-5-20251001` via custom Anthropic endpoint.
### Standalone script
- `scripts/test-compaction-live.ts` — standalone verification script that tests both `summarizeInStages` and the full extension handler with a real LLM.
All 5032 tests pass (5031 unit + 1 live).
## Files Changed
- `src/agents/pi-extensions/compaction-safeguard.ts` — 3 failure paths now return `{ cancel: true }`
- `src/agents/pi-extensions/compaction-safeguard.test.ts` — 3 regression tests + 1 live test
- `scripts/test-compaction-live.ts` — standalone live verification script
- `CHANGELOG.md` — entry added
Most Similar PRs
#10711: fix: cancel compaction instead of truncating history when summariza...
by DukeDeSouth · 2026-02-06
86.5%
#17864: fix(compaction): pass model through runtime + reduce chunk ratio to...
by battman21 · 2026-02-16
76.6%
#20038: (fix): Compaction: preserve recent context and sync session memory ...
by rodrigouroz · 2026-02-18
76.1%
#10456: fix: preserve persona and language continuity in compaction summaries
by keepitmello · 2026-02-06
75.1%
#10505: feat(compaction): add timeout, model override, and diagnostic logging
by thebtf · 2026-02-06
73.9%
#6108: Fix compaction producing empty summary when ctx.model is undefined ...
by GangEunzzang · 2026-02-01
73.1%
#12046: fix(compaction): add fallback for undefined ctx.model (#12016)
by anandsuraj · 2026-02-08
72.9%
#21117: feat(compaction): notify on start + idle-triggered proactive compac...
by irchelper · 2026-02-19
72.0%
#19878: fix: Handle compaction when fallback model has smaller context window
by gaurav10gg · 2026-02-18
71.3%
#5380: fix(compaction): include recent messages as fallback when summary u...
by Glucksberg · 2026-01-31
71.2%