#10567: feat(agents): add configurable startup session pruning
agents
stale
Cluster:
Session Pruning Enhancements
## Summary
Adds configurable startup session pruning to automatically trim bloated sessions when loading from disk, preventing context limit issues before the session becomes operational.
- Adds `AgentCompactionStartupPruningConfig` type with `enabled`, `targetTokens`, `strategy`, and `minRecentMessages` options
- Adds Zod schema validation for the new config
- Implements `applyStartupPruning()` using pi-coding-agent's `findCutPoint()` API
- Integrates into session load path in `attempt.ts` (non-blocking, with try/catch)
- Includes unit tests with 8 test cases covering all code paths
## Why This Is Needed
The `/compact` command requires a session to be loaded and operational. Bloated sessions (160k+ tokens) can fail or hit context limits *during initial load*, before `/compact` can be issued. Startup pruning solves this by trimming at load time.
## Configuration
```json
{
"agents": {
"defaults": {
"compaction": {
"startupPruning": {
"enabled": true,
"targetTokens": 160000,
"strategy": "keep-recent",
"minRecentMessages": 10
}
}
}
}
}
```
## Test plan
- [x] Unit tests pass (8 tests covering disabled, empty, within-limit, pruning, defaults, strategy fallback, error handling, and minRecentMessages warning)
- [ ] Build passes
- [ ] Lint passes
- [ ] Manual test with bloated session (170k+ tokens)
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
- Introduces a new `startupPruning` compaction defaults config (types + Zod validation) intended to trim oversized sessions during load.
- Adds `applyStartupPruning()` which estimates session token usage, picks a cut point via `findCutPoint()`, and attempts to create a branched session file.
- Hooks startup pruning into the embedded attempt session-load path (`runEmbeddedAttempt`) behind a config gate and try/catch.
- Adds unit tests exercising enabled/disabled behavior, default targets, strategy fallback, and failure paths.
<h3>Confidence Score: 2/5</h3>
- This PR should not be merged until pruning semantics are corrected and the pruned session state is actually used after load.
- The current implementation appears to prune the wrong portion of history due to how `branch()`/`createBranchedSession()` are combined, and even when pruning succeeds it likely doesn’t affect the in-memory session context used to create the agent session. These issues undermine the feature’s core goal (avoiding context-limit failures at startup).
- src/agents/startup-pruning.ts, src/agents/pi-embedded-runner/run/attempt.ts
<!-- greptile_other_comments_section -->
<sub>(3/5) Reply to the agent's comments like "Can you suggest a fix for this @greptileai?" or ask follow-up questions!</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
#14879: fix: persist session metadata to sessions.json after context pruning
by skylarkoo7 · 2026-02-12
81.5%
#11999: fix: add session-growth guard to prevent unbounded session store gr...
by reverendrewind · 2026-02-08
80.5%
#14913: fix: update context pruning to notify session metadata after prunin...
by ScreenTechnicals · 2026-02-12
79.1%
#19329: feat: add per-agent compaction and context pruning overrides
by curtismercier · 2026-02-17
78.9%
#5360: fix(compaction): add emergency pruning for context overflow
by sgwannabe · 2026-01-31
78.6%
#8313: feat: auto-compaction support for spawned subagent sessions
by vishaltandale00 · 2026-02-03
78.3%
#13388: feat(session): Auto-prune consecutive NO_REPLY messages from context
by Masha-L · 2026-02-10
77.9%
#4042: agents: add proactive compaction before request
by freedomzt · 2026-01-29
77.9%
#19878: fix: Handle compaction when fallback model has smaller context window
by gaurav10gg · 2026-02-18
76.9%
#11089: feat(compaction): support customInstructions and model override for...
by p697 · 2026-02-07
75.7%