ductor icon indicating copy to clipboard operation
ductor copied to clipboard

feat: Pre-compaction memory flush (silent turn before context compaction)

Open alexeymorozua opened this issue 8 months ago • 1 comments

Problem

When Claude Code compacts the conversation context, the agent loses information from recent messages that hasn't been written to MAINMEMORY.md yet. Currently, CompactBoundaryEvent is only logged — no flush happens.

This causes silent memory loss in long sessions: decisions made, intermediate results, temporary facts — all gone at the compaction boundary.

Proposed Solution

Before context compaction, run a "silent turn" that instructs the agent to write durable facts to MAINMEMORY.md (append-only). Then proceed with compaction.

Reference implementation: OpenClaw (src/auto-reply/reply/memory-flush.ts) uses exactly this approach:

  • Configurable soft threshold (e.g. 4000 tokens before limit)
  • Separate flush prompt: "Store durable memories only. APPEND, do not overwrite."
  • hasAlreadyFlushedForCurrentCompaction() guard prevents double-flush per cycle

Implementation Sketch

In ductor_bot/cli/service.py, CompactBoundaryEvent is already surfaced. The change needed:

  1. Intercept CompactBoundaryEvent before it fires
  2. Inject a pre-flush turn with a focused memory-write prompt
  3. Track flush state per compaction cycle to avoid duplicates

Suggested Configuration

compaction:
  memory_flush:
    enabled: true
    soft_threshold_tokens: 4000

Impact

  • Long-running sessions retain context across compaction boundaries
  • No behavioral change for short sessions (threshold not reached)
  • Backward compatible

Priority: High — affects all agents in long conversations.

alexeymorozua avatar Mar 18 '26 08:03 alexeymorozua

Shipped in v0.16.0.

Hooked into the existing CompactBoundaryEvent stream event. When Claude signals it's about to compact, ductor fires a silent flush turn that captures key state into MAINMEMORY.md before the drop. On by default; disable with memory_flush.enabled: false. Infrastructure lives in a new orchestrator/memory_flush.py module so the reflection hook (#65) and LLM compaction (#80) can share the same dedup window and lock pool.

PleasePrompto avatar Apr 21 '26 10:04 PleasePrompto