#5380: fix(compaction): include recent messages as fallback when summary unavailable
agents
size: S
experienced-contributor
Cluster:
Memory Compaction Improvements
### Summary
When compaction summarization fails, include the last 10 messages as fallback context instead of leaving the agent with zero history.
### Problem
When auto-compaction triggers but summarization fails (no model available, no API key, or summarization error), the agent receives:
```
Summary unavailable due to context limits. Older messages were truncated.
```
This is effectively a **complete memory wipe** — the agent loses all context from before compaction and has no idea what was being discussed.
### Solution
Add a fallback that includes the last 10 messages from the conversation being compacted:
```
Summary unavailable due to context limits. Older messages were truncated.
## Recent Messages (fallback context)
**User**: Hey, can you help me with the billing system refactor?
**Assistant**: Of course! I see you want to refactor the billing module...
**User**: Yes, specifically the invoice generation part...
```
This ensures the model **never loses ALL context** — at minimum it has the most recent exchanges to continue the conversation coherently.
### Implementation Details
- Added `FALLBACK_RECENT_MESSAGES = 10` constant (configurable)
- Added `MAX_MESSAGE_PREVIEW_CHARS = 500` to keep fallback compact
- Added `extractMessageText()` to handle both string and block content
- Added `formatRecentMessagesSection()` to format the fallback
- Fallback includes only user/assistant messages (skips tool results)
- Newlines in messages are collapsed to spaces for readability
### Changes
```
src/agents/pi-extensions/compaction-safeguard.ts | +52/-1
```
Fixes #5224
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
This PR improves the compaction safeguard so that when summarization can’t run (e.g., no model/API key or summarization errors), the fallback summary includes a small “Recent Messages” section (last ~10 user/assistant messages) in addition to existing tool-failure and file-ops context. This avoids a hard context wipe when compaction occurs but summarization is unavailable, helping the agent continue the conversation coherently.
<h3>Confidence Score: 4/5</h3>
- This PR is likely safe to merge and improves degraded-mode behavior, with a small correctness concern around message content shapes.
- Changes are localized to fallback-summary formatting and don’t affect the normal summarization path. The main risk is that the fallback may omit messages if `AgentMessage.content` can be a single object block (not string/array), and the preview truncation slightly exceeds the configured cap.
- src/agents/pi-extensions/compaction-safeguard.ts
<!-- greptile_other_comments_section -->
**Context used:**
- Context from `dashboard` - CLAUDE.md ([source](https://app.greptile.com/review/custom-context?memory=fd949e91-5c3a-4ab5-90a1-cbe184fd6ce8))
- Context from `dashboard` - AGENTS.md ([source](https://app.greptile.com/review/custom-context?memory=0d0c8278-ef8e-4d6c-ab21-f5527e322f13))
<!-- /greptile_comment -->
Most Similar PRs
#8896: fix: improve compaction fallback message with recovery hints
by joetomasone · 2026-02-04
85.9%
#8903: fix: improve compaction summary instructions to preserve active work
by joetomasone · 2026-02-04
82.8%
#6108: Fix compaction producing empty summary when ctx.model is undefined ...
by GangEunzzang · 2026-02-01
81.7%
#5360: fix(compaction): add emergency pruning for context overflow
by sgwannabe · 2026-01-31
80.0%
#4223: fix: compaction safeguard falls through when ctx.model is unavailable
by hanxiao · 2026-01-29
79.2%
#19878: fix: Handle compaction when fallback model has smaller context window
by gaurav10gg · 2026-02-18
78.9%
#12046: fix(compaction): add fallback for undefined ctx.model (#12016)
by anandsuraj · 2026-02-08
78.2%
#9620: fix: increase auto-compaction reserve buffer to 40k tokens
by Arlo83963 · 2026-02-05
77.0%
#5817: fix: strip old images during compaction to prevent 413 session bloat
by jduartedj · 2026-02-01
76.9%
#20038: (fix): Compaction: preserve recent context and sync session memory ...
by rodrigouroz · 2026-02-18
76.9%