← Back to PRs

#10567: feat(agents): add configurable startup session pruning

by abutlabs open 2026-02-06 17:36 View on GitHub →
agents stale
## 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