#6108: Fix compaction producing empty summary when ctx.model is undefined (#6083)
agents
Cluster:
Compaction Safeguards and Summaries
## Summary
When `createAgentSession()` is used, `extensionRunner.initialize()` is not called, leaving `ctx.model` undefined. This causes compaction to return only a fallback placeholder instead of a proper summary.
## Changes
- Add `extractModelFromMessages()` to extract model info from assistant messages as fallback
- Look up the full model object from `ctx.modelRegistry.getAvailable()`
## Test plan
- [x] Build passes (`npm run build`)
- [ ] Manual test with compaction when ctx.model is undefined
Fixes #6083
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
This PR fixes a compaction edge case where `ctx.model` can be unset (e.g., sessions created via `createAgentSession()` without `extensionRunner.initialize()`), causing the safeguard to fall back to the placeholder summary. It adds a fallback that scans recent assistant messages for provider/model identifiers and then resolves the full model entry via `ctx.modelRegistry.getAvailable()` so staged summarization can still run.
<h3>Confidence Score: 4/5</h3>
- This PR is likely safe to merge; it adds a narrow fallback path with limited surface area.
- Changes are localized to compaction safeguard logic and preserve existing behavior when `ctx.model` is present. Main risk is assuming assistant messages consistently carry `provider`/`model` fields and that `modelRegistry.getAvailable()` contains a matching entry, but failures still degrade to the existing fallback summary.
- src/agents/pi-extensions/compaction-safeguard.ts
<!-- greptile_other_comments_section -->
<sub>(2/5) Greptile learns from your feedback when you react with thumbs up/down!</sub>
**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
#4223: fix: compaction safeguard falls through when ctx.model is unavailable
by hanxiao · 2026-01-29
90.6%
#12046: fix(compaction): add fallback for undefined ctx.model (#12016)
by anandsuraj · 2026-02-08
88.8%
#17864: fix(compaction): pass model through runtime + reduce chunk ratio to...
by battman21 · 2026-02-16
82.6%
#10711: fix: cancel compaction instead of truncating history when summariza...
by DukeDeSouth · 2026-02-06
82.3%
#5380: fix(compaction): include recent messages as fallback when summary u...
by Glucksberg · 2026-01-31
81.7%
#8903: fix: improve compaction summary instructions to preserve active work
by joetomasone · 2026-02-04
81.6%
#11970: feat: add model.compact config for dedicated compaction model
by meaadore1221-afk · 2026-02-08
81.5%
#11089: feat(compaction): support customInstructions and model override for...
by p697 · 2026-02-07
80.4%
#10505: feat(compaction): add timeout, model override, and diagnostic logging
by thebtf · 2026-02-06
80.1%
#10273: fix(agents): detect and auto-compact mid-run context overflow
by terryops · 2026-02-06
80.0%