#14487: feat(config): support per-agent compaction overrides (#14446)
agents
stale
size: S
trusted-contributor
Cluster:
Compaction Enhancements and Features
## Summary
Fixes #14446
## Problem
`agents.defaults.compaction.maxHistoryShare` applies globally to all agents. In multi-agent systems, coordinator agents benefit from higher retention while execution agents benefit from earlier compaction. There is no way to set per-agent compaction overrides.
## Fix
Add optional `compaction` field to per-agent config (`agents.list[].compaction`), following the same pattern as existing per-agent overrides (`humanDelay`, `heartbeat`, `memorySearch`).
### Config example
```yaml
agents:
defaults:
compaction:
mode: safeguard
maxHistoryShare: 0.5
list:
- id: coordinator
compaction:
maxHistoryShare: 0.8 # retain more history
- id: worker
# inherits defaults (0.5)
```
### Resolution order
```
agents.list[].compaction.{field} ?? agents.defaults.compaction.{field}
```
### Changes
1. **Type definition** (`src/config/types.agents.ts`): add `compaction?: AgentCompactionConfig` to `AgentConfig`
2. **Zod schema** (`src/config/zod-schema.agent-runtime.ts`): add compaction validation to `AgentEntrySchema`
3. **Agent resolution** (`src/agents/agent-scope.ts`): add `compaction` to `ResolvedAgentConfig` and `resolveAgentConfig`
4. **Runtime** (`src/agents/pi-embedded-runner/extensions.ts`): new `resolveCompactionConfig()` merges defaults with per-agent overrides; `buildEmbeddedExtensionPaths` accepts optional `agentId`
5. **Callers** (`attempt.ts`, `compact.ts`): pass `agentId` to `buildEmbeddedExtensionPaths`
## Reproduction & Verification
### Before fix (main branch):
- `agents.list[].compaction` is not a valid config field (Zod rejects it).
- `buildEmbeddedExtensionPaths` reads only `cfg.agents.defaults.compaction` — no per-agent override path.
- `resolveAgentConfig` does not return `compaction`.
### After fix — All verified:
```
✓ should return undefined when no agents config exists
✓ should return undefined when agent id does not exist
✓ should return basic agent config
✓ returns per-agent compaction overrides (#14446)
✓ supports per-agent model primary+fallbacks
✓ should return agent-specific sandbox config
✓ should return agent-specific tools config
✓ should return both sandbox and tools config
✓ should normalize agent id
✓ uses OPENCLAW_HOME for default agent workspace
✓ uses OPENCLAW_HOME for default agentDir
11 tests pass (pnpm vitest run src/agents/agent-scope.test.ts)
```
## Testing
- ✅ 11 tests pass (`pnpm vitest run src/agents/agent-scope.test.ts`)
- ✅ 7 config validation tests pass
- ✅ Lint passes
Most Similar PRs
#19329: feat: add per-agent compaction and context pruning overrides
by curtismercier · 2026-02-17
82.1%
#8313: feat: auto-compaction support for spawned subagent sessions
by vishaltandale00 · 2026-02-03
75.2%
#21547: feat: add compaction.announce config to notify users of compaction ...
by jlwestsr · 2026-02-20
74.0%
#15927: feat: add compaction.model override config option
by Shuai-DaiDai · 2026-02-14
73.6%
#19232: fix(compaction): preserve exact identifiers in summarization
by brandonwise · 2026-02-17
73.3%
#10505: feat(compaction): add timeout, model override, and diagnostic logging
by thebtf · 2026-02-06
72.3%
#20493: feat(agents): add compaction model override for compaction runs
by PastaPastaPasta · 2026-02-19
72.1%
#11089: feat(compaction): support customInstructions and model override for...
by p697 · 2026-02-07
72.1%
#14640: feat(agents): support per-agent temperature and maxTokens in agents...
by lailoo · 2026-02-12
71.8%
#10197: fix: add missing allowAgents to agent defaults subagents schema
by Yida-Dev · 2026-02-06
71.5%