claude-code - 💡(How to fix) Fix [PROPOSAL] Expose compact/session lifecycle hooks for external memory layers [3 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
anthropics/claude-code#47023Fetched 2026-04-13 05:43:32
View on GitHub
Comments
3
Participants
2
Timeline
12
Reactions
0
Author
Timeline (top)
cross-referenced ×7commented ×3labeled ×2

Root Cause

Five open issues request persistent memory (#14227, #32627, #34192, #34556, #46138). The community is already building solutions — 3-tier markdown architectures, knowledge graphs, structured memory layers. Each re-invents transcript access and compact interception because Claude Code doesn't expose the lifecycle events external memory needs.

Fix Action

Fix / Workaround

  • Claude Code's existing UserPromptSubmit hook is used by multiple memory projects as a workaround for SessionStart, but it fires on every message, not just session init.
  • The PreCompact hook is specifically requested in #31845.
  • Bella is an open-source belief hypergraph memory layer that would immediately use all four hooks. Currently works around the missing lifecycle by intercepting transcripts post-hoc.
  • The theoretical basis is Recursive Emergence — a framework for how beliefs should emerge, reinforce, and decay in bounded-memory systems.

Code Example

// settings.json
{
  "hooks": {
    "PreCompact": [{
      "type": "command",
      "command": "bella save --auto"
    }]
  }
}
RAW_BUFFERClick to expand / collapse

Problem

Five open issues request persistent memory (#14227, #32627, #34192, #34556, #46138). The community is already building solutions — 3-tier markdown architectures, knowledge graphs, structured memory layers. Each re-invents transcript access and compact interception because Claude Code doesn't expose the lifecycle events external memory needs.

The platform doesn't need to build persistent memory. It needs to expose the seams so the community can build it composably.

Proposed hooks

1. PreCompact — fires before context compaction

// settings.json
{
  "hooks": {
    "PreCompact": [{
      "type": "command",
      "command": "bella save --auto"
    }]
  }
}

What it enables: External memory layers save structured state before compaction flattens the context. Currently, if compaction happens between user messages, unfiled knowledge is lost (as documented in #34556 — 59 compactions, each a potential knowledge-loss event).

What the hook receives: Current session ID, token count, transcript path.

What the hook can return: Optional additionalContext to inject into the post-compaction context (e.g., a structured summary from the memory layer, replacing the lossy default compaction).

2. PostCompact — fires after compaction completes

What it enables: Memory layers can inject recalled context into the fresh post-compaction window. Different from PreCompact — this is the retrieval side, not the save side.

What the hook receives: Session ID, new token budget available, compaction summary.

3. SessionEnd — fires when a session terminates

What it enables: Final save opportunity. Currently, if the user closes the terminal or the process exits, no save happens.

What the hook receives: Session ID, transcript path, exit reason.

4. SessionStart — fires on session initialization (before CLAUDE.md)

What it enables: Memory layers inject recalled context before the first user message. Currently achievable via UserPromptSubmit hooks but semantically wrong — session start is the right lifecycle event.

What the hook receives: Session ID, project root, cwd.

Prior art

  • Claude Code's existing UserPromptSubmit hook is used by multiple memory projects as a workaround for SessionStart, but it fires on every message, not just session init.
  • The PreCompact hook is specifically requested in #31845.
  • Bella is an open-source belief hypergraph memory layer that would immediately use all four hooks. Currently works around the missing lifecycle by intercepting transcripts post-hoc.
  • The theoretical basis is Recursive Emergence — a framework for how beliefs should emerge, reinforce, and decay in bounded-memory systems.

What this does NOT propose

  • No opinion on what memory should look like (flat files, knowledge graphs, hypergraphs — that's the community's job)
  • No changes to /compact behavior itself
  • No new built-in memory system

This is purely about exposing lifecycle seams so the five existing community efforts can compose with Claude Code instead of fighting it.

extent analysis

TL;DR

Exposing lifecycle hooks (PreCompact, PostCompact, SessionEnd, SessionStart) in Claude Code allows the community to build persistent memory solutions composably.

Guidance

  • Implement the proposed hooks (PreCompact, PostCompact, SessionEnd, SessionStart) to expose lifecycle events, enabling external memory layers to save and retrieve structured state.
  • Define the hook interfaces to receive and return relevant data, such as session ID, token count, and transcript path.
  • Document the usage and behavior of each hook, including examples and use cases, to facilitate community adoption.
  • Collaborate with the community to ensure the hooks meet the needs of existing memory projects, such as Bella.

Example

// settings.json
{
  "hooks": {
    "PreCompact": [{
      "type": "command",
      "command": "bella save --auto"
    }]
  }
}

This example demonstrates how the PreCompact hook can be used to save structured state before compaction.

Notes

The proposed solution focuses on exposing lifecycle seams, allowing the community to build persistent memory solutions without modifying Claude Code's core behavior. This approach enables composability and flexibility, while avoiding opinions on the implementation of memory systems.

Recommendation

Apply the proposed workaround by implementing the lifecycle hooks, as this will enable the community to build persistent memory solutions that compose with Claude Code.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

claude-code - 💡(How to fix) Fix [PROPOSAL] Expose compact/session lifecycle hooks for external memory layers [3 comments, 2 participants]