Skip to content

MEMORY: loop — lifecycle hooks + background auto-extraction #538

@Hmbown

Description

@Hmbown

Part of #534 (Phase 3 EPIC). Builds on #535 (substrate). Pairs with #538 (hygiene).

Thesis

Two patterns from reference-cc that close the memory loop:

  1. Lifecycle hooks (SessionStart, PreCompact, PostCompact, InstructionsLoaded) — backends and skills react to session boundaries without forking the engine.
  2. Background auto-extraction — a low-cost subagent runs after each turn (or on idle), reads the last N messages, and writes high-signal facts to the memory store automatically.

Together: memory grows without user effort, and external integrations get clean event points.

Current behavior

  • crates/tui/src/hooks.rs supports general hooks but has no memory-specific hook types.
  • Memory writes are entirely user-driven (# add quick-capture or /memory editor).
  • No background extraction.

Proposed change

Hooks — extend crates/tui/src/hooks.rs with four memory-relevant types:

  • SessionStart — fired before the first turn; payload includes loaded memory summary.
  • PreCompact — fired before context compaction; payload includes turn count and token budget.
  • PostCompact — fired after compaction; payload includes what was preserved/summarized.
  • InstructionsLoaded — fired after CLAUDE.md/AGENTS.md/memory hierarchy resolves; payload includes the merged context with provenance.

Configured via settings.json like existing hooks. Documented payloads.

Auto-extraction — opt-in subagent triggered post-turn or on idle:

  • Reads the last N messages.
  • Calls cheapest-available model with an extraction prompt.
  • Each extracted fact gets an auto-classified type (MEMORY: engine — typed memory model + multi-signal recall #536) and is written via MemoryBackend::remember.
  • Dedups against existing memory before writing (FTS5 lookup).
  • Counts toward session cost; shown in UI.

Open questions

  • Default trigger: post-turn, on-idle (e.g., 30s after last user input), or both?
  • Default off vs default on once shown safe? Probably default off until MEMORY: loop — lifecycle hooks + background auto-extraction #538 hygiene proves it doesn't accumulate noise.
  • Should extraction use a smaller model than the main turn (cost) or the same model (quality)?

Acceptance signals

  • Hooks fire with documented payloads.
  • Auto-extraction doesn't block the main turn (truly async).
  • Extraction count visible in UI; per-extraction cost folded into session total.
  • With auto-extraction off, behavior identical to pre-issue.
  • PreCompact hook can be used to trigger a memory consolidation pass (MEMORY: loop — lifecycle hooks + background auto-extraction #538).

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    Status
    In progress

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions