← Back to PRs

#8313: feat: auto-compaction support for spawned subagent sessions

by vishaltandale00 open 2026-02-03 21:40 View on GitHub →
agents stale
## Summary This PR implements optional auto-compaction for subagent sessions created via `sessions_spawn`, allowing long-running tasks to compact and continue rather than hitting context limits and dying. ## Problem Currently, subagent sessions run until they hit the context window limit and then stop. They have no access to the compaction safeguard that the main session uses. Complex research and investigation tasks regularly fill the 128k context window. ## Solution Added a `compaction` option to the `subagents` configuration that can be: - **Boolean**: `true` enables safeguard mode with default settings - **Config object**: Full compaction configuration with custom `mode`, `maxHistoryShare`, `reserveTokensFloor`, and `memoryFlush` settings ### Configuration Examples Enable with defaults: ```json { "agents": { "defaults": { "subagents": { "compaction": true } } } } ``` Custom configuration: ```json { "agents": { "defaults": { "subagents": { "compaction": { "mode": "safeguard", "maxHistoryShare": 0.4, "reserveTokensFloor": 4000 } } } } } ``` ## Changes 1. **TypeScript types**: Added `compaction` field to `subagents` config (boolean or full config object) 2. **Zod schema**: Added validation for the new `subagents.compaction` field 3. **Extension initialization**: Modified `buildEmbeddedExtensionPaths` to: - Accept `sessionKey` parameter - Detect subagent sessions via `:subagent:` in session key - Use subagent-specific compaction config when present 4. **Call sites**: Updated both `attempt.ts` and `compact.ts` to pass `sessionKey` to extension initialization ## Technical Details The implementation reuses the existing `compaction-safeguard.js` logic. When a subagent session is detected, the configuration resolver checks `cfg?.agents?.defaults?.subagents?.compaction` first, falling back to the main `compaction` config if not present. ## Testing Tested by: - Configuration validation passes with new schema - No breaking changes to existing sessions (falls back to main config) - Subagent sessions will now respect their own compaction config ## Related Issue Fixes #8308 --- 🤖 Generated with Claude Code <!-- greptile_comment --> <h2>Greptile Overview</h2> <h3>Greptile Summary</h3> This PR adds an optional `agents.defaults.subagents.compaction` setting (boolean or full config object) and wires the embedded Pi runner so spawned subagent sessions can enable the existing compaction safeguard extension. Call sites in the embedded runner now pass `sessionKey` into `buildEmbeddedExtensionPaths`, which uses the session key to decide whether to read the main `compaction` config or the new `subagents.compaction` override. <h3>Confidence Score: 3/5</h3> - Mostly safe, but there is a correctness issue in how subagent sessions are detected for applying the new config. - Core change is config plumbing + extension runtime wiring, but `extensions.ts` reimplements subagent session-key detection in a way that likely misses valid subagent keys used elsewhere in the repo, so the feature may silently not apply in real usage. No other high-confidence issues found in the diff. - src/agents/pi-embedded-runner/extensions.ts <!-- greptile_other_comments_section --> <!-- /greptile_comment -->

Most Similar PRs