DEV Community

Mike W
Mike W

Posted on

Claude Code just added lifecycle hooks. Here is how to use them to anchor your AI memory automatically.

Claude Code v2.1.83-85 shipped three new lifecycle hook events: FileChanged, CwdChanged, and TaskCreated. Most people will use these for linting or formatting. But there is a more interesting use case: automatic memory anchoring.

Here is the problem they solve for AI agents.

The memory gap

When an AI agent finishes a session, its working context disappears. Most tools try to solve this by storing conversation history. That is the wrong layer.

What actually matters is: what did the agent know, and when did it know it?

If an agent refactors your auth module on Tuesday and introduces a bug on Thursday, you want to answer: did the agent's understanding of the system change between those two sessions? Conversation logs do not tell you that. A timestamped, hash-verified memory snapshot does.

What Cathedral does

Cathedral is an open-source persistent memory layer for AI agents. It stores identity memories across sessions and exposes a /snapshot endpoint that freezes the current memory state into an immutable, BCH-anchored record.

Each snapshot produces a SHA256 hash of the full memory corpus. You can recompute it later and verify nothing was silently edited. The /drift endpoint shows how far the current state has moved from baseline.

Until now, snapshots were manual. You had to remember to call them.

Wiring Claude Code hooks to Cathedral

With the new lifecycle hooks, snapshots happen automatically. Add this to your ~/.claude/settings.json:

{
  "hooks": {
    "Stop": [
      {
        "hooks": [{
          "type": "command",
          "command": "curl -s -X POST https://cathedral-ai.com/snapshot -H \"Authorization: Bearer YOUR_API_KEY\" -H \"Content-Type: application/json\" -d \"{\\\"label\\\":\\\"session-end\\\"}\""
        }]
      }
    ],
    "TaskCreated": [
      {
        "hooks": [{
          "type": "command",
          "command": "curl -s -X POST https://cathedral-ai.com/snapshot -H \"Authorization: Bearer YOUR_API_KEY\" -H \"Content-Type: application/json\" -d \"{\\\"label\\\":\\\"task-created\\\"}\""
        }]
      }
    ],
    "CwdChanged": [
      {
        "hooks": [{
          "type": "command",
          "command": "curl -s -X POST https://cathedral-ai.com/snapshot -H \"Authorization: Bearer YOUR_API_KEY\" -H \"Content-Type: application/json\" -d \"{\\\"label\\\":\\\"project-switch\\\"}\""
        }]
      }
    ]
  }
}
Enter fullscreen mode Exit fullscreen mode

Get your API key at cathedral-ai.com.

What each hook captures

Stop fires when Claude returns control to you. This is the most important anchor point. Over time, /drift/history shows you the full timeline of how the agent's understanding evolved.

TaskCreated fires when the agent creates a task via TaskCreate. Snapshot at task initiation means you have a provenance record. If something goes wrong during a long task, you can check whether the agent's memory state was already drifted before it started.

CwdChanged fires when the working directory changes. This anchors the memory state at each project boundary — useful if you're debugging why the agent seemed to carry assumptions from one codebase into another.

The result

After a week of normal Claude Code usage with these hooks, your /drift/history timeline looks like this:

session-end      2026-04-02  hash: a5e814fe  divergence: 0.02
task-created     2026-04-02  hash: 9d3c21ab  divergence: 0.02
project-switch   2026-04-01  hash: 7f1a88cd  divergence: 0.08
session-end      2026-04-01  hash: 3b9e55f2  divergence: 0.11
Enter fullscreen mode Exit fullscreen mode

Each row is a moment in time where you can verify: this is exactly what the agent knew. Not what it said it knew. Not what the conversation log implies it knew. A hash-verified, blockchain-anchored record.

Advanced: FileChanged

v2.1.83 also added FileChanged, which fires when a file in your project changes. You can hook this to Cathedral for per-edit anchoring. Most people will find Stop plus TaskCreated sufficient for normal use, but FileChanged is there if you want a denser trail.

Why this matters

Anthropic's internal KAIROS system (recently referenced in leaked documentation) converges on the same primitives: snapshot, drift detection, provenance tagging. Cathedral has been shipping these as open infrastructure for 96 days.

The difference: Cathedral is model-portable and self-hosted. Your memory layer should not be owned by the company that also owns the weights.

Claude Code's new hooks close the last friction point. Wire them once, and every session is automatically anchored.


Links:

Top comments (0)