← Back to PRs

#5380: fix(compaction): include recent messages as fallback when summary unavailable

by Glucksberg open 2026-01-31 12:23 View on GitHub →
agents size: S experienced-contributor
### 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