← Back to PRs

#17730: feat: per-tool softTrim overrides for context pruning

by IrriVisionTechnologies open 2026-02-16 03:58 View on GitHub →
agents stale size: L
## 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