Feature Request: Post-Compaction Recovery Turn
Problem
When auto-compaction completes, the agent loses context about what it was doing. The existing memoryFlush (pre-compaction) helps save state, but there's no mechanism to recover that context after compaction.
Current flow:
- ✅ Pre-compaction:
memoryFlush saves durable memories
- ✅ Compaction: summary generated, old messages removed
- ❌ Post-compaction: agent continues with reduced context, doesn't know to read saved memories
User impact: After compaction, users must re-explain context or the agent "forgets" ongoing tasks.
Development Process
Attempt 1: Hook into existing compaction flow
Approach: Add recovery logic directly in agent-runner-memory.ts after compaction detection
Problem: The compaction flag is checked but there was no hook point to inject a new turn
Outcome: ❌ Needed a cleaner abstraction
Attempt 2: Create dedicated post-compaction module
Approach: New post-compaction.ts module with:
buildPostCompactionTurn() - constructs the recovery message
shouldRunPostCompaction() - checks config and compaction state
- Integration in
agent-runner.ts after compaction completes
Outcome: ✅ Clean separation, testable, works
Attempt 3: Config schema design
Approach: Mirror the existing memoryFlush pattern
Options considered:
compaction.postCompaction - too nested
memory.postCompaction - consistent with memory features
recoveryPrompt - less discoverable
Outcome: ✅ Chose memory.postCompaction.enabled + memory.postCompaction.prompt
Testing
Test 1: Basic trigger
- Filled context to 151k tokens to force compaction
- ✅ Post-compaction turn injected after compaction completed
Test 2: Memory recovery with marker
- Added
BANANA_TEST_7X9K2 to memory/2026-01-28.md
- Triggered compaction (151k → 105k tokens)
- ✅ Agent read memory file and reported the marker correctly
Test 3: Multiple compaction cycles
- Continued session, triggered second compaction
- New marker:
BANANA_KEY_8472
- ✅ Recovery worked again across cycles
Known limitation found during testing
contextTokens config doesn't override model's native context window
- This is a separate issue, not related to this feature
Solution
New memory.postCompaction config:
{
"memory": {
"postCompaction": {
"enabled": true,
"prompt": "Compaction completed. Read memory/YYYY-MM-DD.md to recover context..."
}
}
}
How it works:
- After compaction completes, check if
postCompaction.enabled
- If enabled, inject a system turn with the configured prompt
- Agent reads memory files and recovers context
- User continues seamlessly without re-explaining
Files Changed
| File |
Purpose |
src/auto-reply/reply/post-compaction.ts |
Core logic |
src/auto-reply/reply/post-compaction.test.ts |
Unit tests (13 passing) |
src/auto-reply/reply/agent-runner.ts |
Integration point |
src/auto-reply/reply/agent-runner-memory.ts |
Compaction detection hook |
src/config/schema.ts |
Config schema |
src/config/zod-schema.agent-defaults.ts |
Zod validation |
src/config/types.agent-defaults.ts |
TypeScript types |
src/config/sessions/types.ts |
Session types |
Note on Contribution Guidelines
Per CONTRIBUTING.md, new features should start with a Discussion. This issue was created alongside the implementation as a proof-of-concept. Happy to move to Discussion format if preferred by maintainers.
Feature Request: Post-Compaction Recovery Turn
Problem
When auto-compaction completes, the agent loses context about what it was doing. The existing
memoryFlush(pre-compaction) helps save state, but there's no mechanism to recover that context after compaction.Current flow:
memoryFlushsaves durable memoriesUser impact: After compaction, users must re-explain context or the agent "forgets" ongoing tasks.
Development Process
Attempt 1: Hook into existing compaction flow
Approach: Add recovery logic directly in
agent-runner-memory.tsafter compaction detectionProblem: The compaction flag is checked but there was no hook point to inject a new turn
Outcome: ❌ Needed a cleaner abstraction
Attempt 2: Create dedicated post-compaction module
Approach: New
post-compaction.tsmodule with:buildPostCompactionTurn()- constructs the recovery messageshouldRunPostCompaction()- checks config and compaction stateagent-runner.tsafter compaction completesOutcome: ✅ Clean separation, testable, works
Attempt 3: Config schema design
Approach: Mirror the existing
memoryFlushpatternOptions considered:
compaction.postCompaction- too nestedmemory.postCompaction- consistent with memory featuresrecoveryPrompt- less discoverableOutcome: ✅ Chose
memory.postCompaction.enabled+memory.postCompaction.promptTesting
Test 1: Basic trigger
Test 2: Memory recovery with marker
BANANA_TEST_7X9K2tomemory/2026-01-28.mdTest 3: Multiple compaction cycles
BANANA_KEY_8472Known limitation found during testing
contextTokensconfig doesn't override model's native context windowSolution
New
memory.postCompactionconfig:{ "memory": { "postCompaction": { "enabled": true, "prompt": "Compaction completed. Read memory/YYYY-MM-DD.md to recover context..." } } }How it works:
postCompaction.enabledFiles Changed
src/auto-reply/reply/post-compaction.tssrc/auto-reply/reply/post-compaction.test.tssrc/auto-reply/reply/agent-runner.tssrc/auto-reply/reply/agent-runner-memory.tssrc/config/schema.tssrc/config/zod-schema.agent-defaults.tssrc/config/types.agent-defaults.tssrc/config/sessions/types.tsNote on Contribution Guidelines
Per CONTRIBUTING.md, new features should start with a Discussion. This issue was created alongside the implementation as a proof-of-concept. Happy to move to Discussion format if preferred by maintainers.