#8961: feat: smarter compaction tool truncation + token count in system prompt report
docs
gateway
cli
docker
agents
size: M
Cluster:
Memory Management Enhancements
## Changes
### 1. Compaction tool output truncation (`compaction-safeguard.ts`)
When sessions get compacted, large tool outputs (e.g. massive file reads, search results) were being kept in full, bloating the compacted history. This adds intelligent truncation:
- Caps tool result tokens at 2% of total context (min 400 tokens / 2000 chars)
- Replaces excess with `[Tool output truncated for compaction]`
- Keeps compacted sessions leaner without losing critical information
### 2. Token estimation in system prompt report (`system-prompt-report.ts` + `types.ts`)
Adds actual token estimation to the system prompt report alongside the existing character count. Useful for understanding real token budget usage of the system prompt.
---
Small, self-contained patches. Been running these in production for weeks with no issues.
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
This PR updates the compaction safeguard and system prompt reporting.
- `src/agents/pi-extensions/compaction-safeguard.ts` introduces constants/imports intended to truncate large tool outputs during session compaction, but the truncation behavior is not actually implemented in the current code.
- `src/agents/system-prompt-report.ts` extends the system prompt report to include an estimated token count (now reflected in `src/config/sessions/types.ts`).
Overall, the direction fits the existing compaction/reporting utilities, but a couple of changes appear incomplete or may not function as intended without additional wiring/validation.
<h3>Confidence Score: 3/5</h3>
- This PR has a couple of correctness gaps that should be addressed before merging.
- The compaction truncation described in the PR does not appear to be implemented (unused constants/import), and the token estimation call may be using an incorrect input shape and can silently produce no token count due to broad error swallowing.
- src/agents/pi-extensions/compaction-safeguard.ts, src/agents/system-prompt-report.ts
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#10915: fix: prevent session bloat from oversized tool results and improve ...
by DukeDeSouth · 2026-02-07
84.2%
#15322: feat: post-compaction target token trimming + fallback strategy
by echoVic · 2026-02-13
82.6%
#15196: fix: clear stale token totals after compaction
by bufordtjustice2918 · 2026-02-13
81.2%
#4852: fix(agents): sanitize tool pairing after compaction and history tru...
by lailoo · 2026-01-30
80.2%
#18663: feat: progressive compaction escalation and mechanical flush fallback
by Adamya05 · 2026-02-16
79.5%
#9012: fix(memory): resilient flush for large sessions [AI-assisted]
by cheenu1092-oss · 2026-02-04
78.9%
#20038: (fix): Compaction: preserve recent context and sync session memory ...
by rodrigouroz · 2026-02-18
78.9%
#13792: Claude/openclaw token efficiency q ni2s
by imecostalima · 2026-02-11
78.8%
#9620: fix: increase auto-compaction reserve buffer to 40k tokens
by Arlo83963 · 2026-02-05
78.6%
#14887: feat(compaction): configurable auto-compaction notifications with o...
by seilk · 2026-02-12
78.5%