← Back to PRs

#10457: fix(compaction): abort compaction when summarization fails instead of truncating history (#10332)

by lailoo open 2026-02-06 14:44 View on GitHub →
scripts agents stale
## 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