#17730: feat: per-tool softTrim overrides for context pruning
agents
stale
size: L
Cluster:
Context Management Enhancements
## Problem
Context pruning currently applies the same `softTrim` settings to all prunable tools. Browser snapshots (often 50KB+) get trimmed to the same `maxChars: 4000` as small exec outputs. This means either browser results stay too large, or exec results get trimmed too aggressively.
## Solution
Add a `toolOverrides` config section to `contextPruning` that allows per-tool `softTrim` settings:
```yaml
contextPruning:
mode: cache-ttl
ttl: 5m
softTrim:
maxChars: 4000 # default for all tools
toolOverrides:
browser:
softTrim:
maxChars: 1000 # browser results trimmed aggressively
web_fetch:
softTrim:
maxChars: 2000
read:
softTrim:
maxChars: 8000 # file reads kept longer
```
### How it works
- Tool names are matched case-insensitively
- Glob patterns supported (e.g., `browser*`)
- Unspecified fields inherit from global `softTrim`
- No overrides = current behavior (fully backward-compatible)
## Changes
- `settings.ts`: New `EffectiveSoftTrimSettings` type, `toolOverrides` Map on effective settings
- `pruner.ts`: Uses `resolveToolSoftTrim()` to get per-tool settings before trimming
- `tools.ts`: New `resolveToolSoftTrim()` with direct + glob pattern matching
- `zod-schema.agent-defaults.ts`: Schema validation for `toolOverrides`
- `types.agent-defaults.ts`: TypeScript types for `toolOverrides`
- New test: `tool-overrides.test.ts` (5 tests, all passing)
## Motivation
We run an AI assistant with browser automation, and browser snapshot results were the #1 cause of transcript bloat. Being able to set `browser.softTrim.maxChars: 1000` while keeping `exec` at `4000` solved the issue cleanly.
<!-- greptile_comment -->
<h3>Greptile Summary</h3>
This PR bundles two distinct features: (1) per-tool `softTrim` overrides for context pruning, and (2) subagent abort-on-timeout, wait retry with exponential backoff, and stale run detection.
- **Per-tool softTrim overrides**: Adds a `toolOverrides` config section to `contextPruning` that allows different `softTrim` settings per tool name (or glob pattern). Settings resolution, Zod validation, TypeScript types, and tests are all included. The implementation is clean and backward-compatible.
- **Subagent abort-on-timeout**: When `agent.wait` returns a timeout, the child run is now actively aborted via `abortEmbeddedPiRun`. Wait RPC failures trigger exponential backoff retries (up to 3 attempts). A stale run detector in the sweeper catches orphaned runs older than 2 hours.
- **Test fixes**: Existing subagent tests updated with mocks for newly imported modules (`config/sessions.js`, `runtime.js`, `pi-embedded.js`, `STATE_DIR`).
<h3>Confidence Score: 3/5</h3>
- Generally safe but contains a minor logic bug and an inconsistency that should be addressed before merge.
- The per-tool override feature is well-implemented and tested. However, there are two issues: (1) the `?` glob wildcard check in `resolveToolSoftTrim` references functionality that `compileGlobPatterns` doesn't support, which would silently fail to match patterns using `?`; and (2) the sweeper-start logic in `replaceSubagentRunAfterSteer` is inconsistent with `registerSubagentRun`, potentially missing stale run detection for replaced runs.
- `src/agents/pi-extensions/context-pruning/tools.ts` (unsupported `?` glob check) and `src/agents/subagent-registry.ts` (inconsistent sweeper start in `replaceSubagentRunAfterSteer`)
<sub>Last reviewed commit: 6f49161</sub>
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#5057: fix: add per-tool-result hard cap to prevent context overflow
by hanxiao · 2026-01-31
81.1%
#10465: Context pruning: strip image blocks instead of skipping
by quentintou · 2026-02-06
80.0%
#16261: feat(agents): add two-tier tool output truncation and excludeFromCo...
by ProgramCaiCai · 2026-02-14
78.2%
#8332: fix: add per-tool-call timeout to prevent agent hangs (v2 - fixes m...
by vishaltandale00 · 2026-02-03
77.4%
#4852: fix(agents): sanitize tool pairing after compaction and history tru...
by lailoo · 2026-01-30
75.6%
#9861: fix(agents): re-run tool_use/tool_result repair after limitHistoryT...
by CyberSinister · 2026-02-05
75.5%
#19329: feat: add per-agent compaction and context pruning overrides
by curtismercier · 2026-02-17
75.2%
#19932: feat(agents): suppressPreToolText config + onBlockReply buffering
by Milofax · 2026-02-18
74.2%
#11854: fix: resolve per-agent tools.exec config in pi-tools
by Yida-Dev · 2026-02-08
74.0%
#19094: Fix empty tool_call_id and function names in provider transcript pa...
by yxshee · 2026-02-17
73.9%