Skip to content

feat(compaction): Add post-compaction recovery turn #3452

Description

@jarvis-sam

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:

  1. ✅ Pre-compaction: memoryFlush saves durable memories
  2. ✅ Compaction: summary generated, old messages removed
  3. ❌ 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:

  1. After compaction completes, check if postCompaction.enabled
  2. If enabled, inject a system turn with the configured prompt
  3. Agent reads memory files and recovers context
  4. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions