Repository navigation
feat: support per-agent compaction overrides in agents.list[] - #14598
curtismercier wants to merge 8 commits into
Conversation
f995375 to
770e7ff
Compare
| return { | ||
| ...defaults, | ||
| ...perAgent, | ||
| memoryFlush: | ||
| perAgent.memoryFlush || defaults.memoryFlush | ||
| ? { ...defaults.memoryFlush, ...perAgent.memoryFlush } | ||
| : undefined, | ||
| }; |
There was a problem hiding this comment.
MemoryFlush merge bug
resolveAgentCompaction does { ...defaults.memoryFlush, ...perAgent.memoryFlush } when either side is present, but defaults.memoryFlush can be undefined. Spreading undefined throws at runtime when an agent defines compaction.memoryFlush but agents.defaults.compaction.memoryFlush is not set. This makes per-agent memoryFlush unusable unless defaults also define memoryFlush.
A safe merge needs to spread {} instead of undefined (or branch on each side) before spreading.
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/agents/agent-scope.ts
Line: 151:158
Comment:
**MemoryFlush merge bug**
`resolveAgentCompaction` does `{ ...defaults.memoryFlush, ...perAgent.memoryFlush }` when either side is present, but `defaults.memoryFlush` can be `undefined`. Spreading `undefined` throws at runtime when an agent defines `compaction.memoryFlush` but `agents.defaults.compaction.memoryFlush` is not set. This makes per-agent `memoryFlush` unusable unless defaults also define `memoryFlush`.
A safe merge needs to spread `{}` instead of `undefined` (or branch on each side) before spreading.
How can I resolve this? If you propose a fix, please make it concise.|
Good catch by @greptile-apps — fixed in cb33279. The { ...(defaults.memoryFlush ?? {}), ...(perAgent.memoryFlush ?? {}) }Added a test case for the exact scenario: per-agent defines |
|
Thanks for the quick fix! I verified the commit and the new test case looks good. The nullish coalescing guards on both spreads correctly handle all three scenarios:
The new test case One minor observation: the test expects |
cb33279 to
8d008b7
Compare
affda17 to
b1581ff
Compare
Extract the compaction Zod schema from AgentDefaultsSchema into a reusable CompactionSchema constant in zod-schema.agent-runtime.ts. Add it as an optional field on AgentEntrySchema, enabling per-agent compaction overrides alongside the existing defaults. AgentDefaultsSchema now imports and reuses CompactionSchema, keeping the shape in sync and eliminating duplication.
…n helper Add compaction field to the ResolvedAgentConfig type and include it in resolveAgentConfig() output. Introduce resolveAgentCompaction(cfg, agentId?) which merges per-agent compaction overrides with agents.defaults.compaction. Top-level fields use per-agent-wins semantics; memoryFlush is shallow-merged one level deeper so partial overrides (e.g., enabled: false) preserve unrelated default fields like prompt and systemPrompt.
…sites Update resolveCompactionMode(), resolveCompactionReserveTokensFloor(), and resolveMemoryFlushSettings() to accept an optional agentId and use resolveAgentCompaction() for merged config resolution instead of reading agents.defaults.compaction directly. Thread agentId (from sessionAgentId) through buildEmbeddedExtensionPaths() and ensurePiCompactionReserveTokens() callsites in both run/attempt.ts and compact.ts. Extract agentId from sessionKey in agent-runner-memory.ts for the memory flush path. All functions remain backward compatible — omitting agentId falls back to global defaults.
…rging Cover per-agent override precedence, partial override merging with defaults, memoryFlush sub-object shallow merge (partial override preserves unrelated default fields), per-agent without defaults, unknown agent fallback, and undefined memoryFlush propagation. 12 test cases focused on the merge edge cases most likely to regress.
Add AgentCompactionConfig to the AgentConfig type in types.agents.ts, fixing TS2339 errors where agent-scope.ts references entry.compaction.
Use nullish coalescing (defaults.memoryFlush ?? {}) to prevent
spreading undefined when per-agent sets memoryFlush but global
defaults omit it. Adds a test case for this exact scenario.
- Add 'off' to AgentCompactionMode union (schema + types) - Skip ensurePiCompactionReserveTokens when mode is 'off' (compact.ts, attempt.ts) - Skip safeguard extension when mode is 'off' (extensions.ts) - Return null from resolveMemoryFlushSettings when mode is 'off' - Add contextPruning.mode to AgentEntrySchema for per-agent override - Add resolveAgentContextPruningMode helper with per-agent precedence - Wire per-agent contextPruning mode into buildContextPruningExtension - Add tests for mode 'off' and resolveAgentContextPruningMode
- Add contextPruning field to AgentConfig type definition - Fix TypeScript narrowing issue in extensions.ts where cfg was narrowed to 'never' in else branch of ternary
bfc1ccb to
f92900f
Compare
b1581ff to
4554d26
Compare
|
Superseded by #19329 — cleaner implementation with review feedback addressed, integration tested, and unrelated changes removed. |
Summary
Adds optional
compactionconfiguration to individual agent entries inagents.list[], allowing per-agent override ofagents.defaults.compactionsettings. When a per-agent compaction config is present, it takes precedence over the global defaults for that agent. When absent, the agent inherits the global defaults — no behavioral change for existing configurations.Motivation
Multi-agent deployments often need different context management strategies for different agents. A long-running research agent benefits from aggressive memory flushing and a high
reserveTokensFloor, while a quick-turnaround chat agent may need earlier compaction via a lowermaxHistoryShare. An orchestration agent might usesafeguardmode while execution agents stay ondefault.Today,
compactionis only configurable globally inagents.defaults.compaction, which forces all agents into the same strategy. Other agent-level settings likeheartbeat,humanDelay,memorySearch, andtoolsalready support per-agent overrides — compaction is the notable gap.Example:
Changes
CompactionSchemaconstant, add toAgentEntrySchema(zod-schema.agent-runtime.ts,zod-schema.agent-defaults.ts)compactiontoResolvedAgentConfigandresolveAgentConfig()output (agent-scope.ts)resolveAgentCompaction(cfg, agentId?)helper — merges per-agent overrides with global defaults:memoryFlushsub-object: shallow-merged one level deeper so partial overrides (e.g.,enabled: false) don't wipe the defaultprompt/systemPromptundefinedwhen neither per-agent nor defaults are setresolveCompactionMode(),resolveCompactionReserveTokensFloor(),resolveMemoryFlushSettings()to accept optionalagentIdand use merged config (extensions.ts,pi-settings.ts,memory-flush.ts)agentId(viasessionAgentId) through all callsites incompact.ts,run/attempt.ts,agent-runner-memory.tsHow Resolution Works
For
memoryFlush, the merge is one level deeper:Testing
12 test cases in
agent-scope.test.tscovering:maxHistoryShareset, inheritsmodeandmemoryFlush)memoryFlushsub-object shallow merge — partial override preserves unrelated default fieldsundefined(hardcoded fallbacks apply)reserveTokensFloorBackward Compatibility
Fully backward compatible. The
compactionfield on agent entries is optional. Existing configs with no per-agent compaction work exactly as before — all three resolution functions fall back toagents.defaults.compaction. No new required fields.Monkey Patch (for immediate use)
A standalone patch script for current OpenClaw releases is available at curtismercier/openclaw-mods — apply/revert/status with version tracking and pre-flight safety checks.
Closes #13736
Closes #14446
Greptile Overview
Greptile Summary
This PR adds per-agent
compactionoverrides underagents.list[], reusing a sharedCompactionSchema, and threads anagentIdthrough compaction/memory-flush resolution so individual agents can overrideagents.defaults.compaction.The main behavioral change is centralized in
resolveAgentCompaction(cfg, agentId?)(src/agents/agent-scope.ts), which merges per-agent and default compaction settings (including a one-level-deeper merge formemoryFlush). Call sites in embedded runner setup and memory flush now pass the session’s effective agent id so the right compaction settings are applied per agent.Confidence Score: 2/5
resolveAgentCompactionmerge logic can throw a TypeError by spreadingundefinedwhen an agent definescompaction.memoryFlushbut defaults omit it. This is a straightforward, high-impact runtime failure in a newly introduced codepath. Other changes look consistent and well-covered by tests, but this edge case isn’t exercised by the current test suite.(2/5) Greptile learns from your feedback when you react with thumbs up/down!