DEV Community

Cover image for Nobody updates the notes file. So I made a Stop hook do it.
Fred 🍓
Fred 🍓

Posted on

Nobody updates the notes file. So I made a Stop hook do it.

My CLAUDE.md described last week's project. Claude reads that file fine. The problem was that I never updated it.

Every Claude Code session starts from zero. The usual fix is a notes file (CLAUDE.md, NOTES.md, a docs/ folder) that tells the next session what the last one learned. Reading it isn't the hard part. Writing it is: a long session ends with a new module, two decisions and one nasty pitfall, and the notes file still describes last week.

You can ask at the end of every task ("update the notes, please"), but you have to remember every single time. I wanted it to happen without asking.

So I let a hook ask instead.

Why a Stop hook

Claude Code fires Stop when Claude is about to end its turn. A Stop hook can answer:

{ "decision": "block", "reason": "Before you finish: update NOTES.md ..." }
Enter fullscreen mode Exit fullscreen mode

and Claude doesn't stop. It reads the reason and keeps going in the same turn.

That timing is the whole point. At that moment Claude still has the full context of what it just did: which files, why, what went wrong on the way. A background job afterwards would have to reconstruct all of that from a diff. A prompt you send automatically afterwards would collide with whatever you type next.

Only ask when it makes sense

Blocking every Stop would be awful. The rules I ended up with:

  1. Only after a turn that changed files. A turn that only answered a question has nothing to note.
  2. Not if the notes were already updated in this turn.
  3. At most once per turn.
  4. Never when stop_hook_active is true. That flag means Claude is already continuing because a Stop hook blocked it. Block again and you have a loop.

Rule 1 needs to know what happened during the turn, so the same script also listens to UserPromptSubmit (a turn starts) and PostToolUse (a file was edited), and keeps a tiny state file per session.

The script

Save it as ~/.claude/notes-keeper.js:

// notes-keeper.js - asks Claude Code to update NOTES.md before it ends a turn that changed code.
// One script for three hooks: UserPromptSubmit, PostToolUse, Stop. Always exits 0.
'use strict'
const fs = require('fs')
const os = require('os')
const path = require('path')

const NOTES = process.argv[2] || 'NOTES.md' // relative to the project folder
const EDIT_TOOLS = new Set(['Edit', 'Write', 'MultiEdit', 'NotebookEdit'])

let input = ''
process.stdin.setEncoding('utf8')
process.stdin.on('data', (c) => (input += c))
process.stdin.on('end', () => {
  try {
    main(JSON.parse(input))
  } catch {
    // a broken hook must never break Claude Code
  }
  process.exit(0)
})

function main(e) {
  const id = String(e.session_id || '').replace(/[^A-Za-z0-9-]/g, '')
  if (!id) return
  const stateFile = path.join(os.tmpdir(), `notes-keeper-${id}.json`)
  const load = () => { try { return JSON.parse(fs.readFileSync(stateFile, 'utf8')) } catch { return null } }
  const save = (s) => fs.writeFileSync(stateFile, JSON.stringify(s))

  if (e.hook_event_name === 'UserPromptSubmit') {
    save({ edits: 0, tended: false }) // a new turn starts
    return
  }

  if (e.hook_event_name === 'PostToolUse' && EDIT_TOOLS.has(e.tool_name)) {
    const s = load() || { edits: 0, tended: false }
    const file = e.tool_input && (e.tool_input.file_path || e.tool_input.notebook_path)
    if (!file) return
    const notes = path.resolve(e.cwd || '.', NOTES)
    if (path.resolve(e.cwd || '.', file).toLowerCase() === notes.toLowerCase()) s.tended = true
    else s.edits++
    save(s)
    return
  }

  if (e.hook_event_name === 'Stop') {
    if (e.stop_hook_active) return // Claude is already continuing because of us: let it stop
    const s = load()
    if (!s || s.tended || s.edits === 0) return
    s.tended = true // ask once per turn, never twice
    save(s)
    process.stdout.write(JSON.stringify({
      decision: 'block',
      reason: `Before you finish: update ${NOTES} with what you learned or decided in this turn ` +
        '(new areas, decisions and why, pitfalls), and fix notes that are now out of date. ' +
        'If there is nothing new, just answer "Notes are up to date." Keep it short and do not repeat your previous answer.'
    }))
  }
}
Enter fullscreen mode Exit fullscreen mode

Register it for all three events in .claude/settings.json (or your user settings):

{
  "hooks": {
    "UserPromptSubmit": [{ "hooks": [{ "type": "command", "command": "node ~/.claude/notes-keeper.js" }] }],
    "PostToolUse": [{ "matcher": "Edit|Write|MultiEdit|NotebookEdit",
                      "hooks": [{ "type": "command", "command": "node ~/.claude/notes-keeper.js" }] }],
    "Stop": [{ "hooks": [{ "type": "command", "command": "node ~/.claude/notes-keeper.js" }] }]
  }
}
Enter fullscreen mode Exit fullscreen mode

To use a different file, pass it as an argument: node ~/.claude/notes-keeper.js CLAUDE.md. On Windows, write the full path to the script instead of ~.

What the reason text needs

The reason is a prompt, and a vague one gets vague results. Three things in it make a big difference:

  • Say what's worth keeping: new areas, decisions with the reason, pitfalls. Otherwise you get a changelog ("edited src/a.ts"), which git already has.
  • Give it a way out: "If there is nothing new, just answer 'Notes are up to date.'" Without that, Claude invents something to write.
  • Say "don't repeat your previous answer". The continuation is part of the same turn, and Claude likes to summarize again.

What changed for me

I've had this running in VibeDesk since mid-October, where it writes to a folder of linked Markdown notes through an MCP tool instead of one file. The notes now grow with the project without me asking. A new session reads them first and can start from there, instead of rediscovering things by reading half the codebase.

What this doesn't prove

  • Claude decides what's worth noting. Sometimes it writes something obvious, sometimes it misses the one pitfall that mattered. I still read the notes now and then and delete the noise.
  • It adds a short extra step to every turn that changed files. That costs some tokens.
  • Notes can go stale in a different way: the code changes in a later turn that never touched the note. The hook doesn't catch that. In VibeDesk I check for notes whose files changed since, before a commit. That's a different mechanism.

Question

Where does the memory of your project live, and when did you last update it?

Or pick one:

A) CLAUDE.md / AGENTS.md, updated by hand
B) some notes folder, updated by Claude
C) nowhere, every session starts fresh
D) a tool or MCP server for it

(This is how the "brain" in VibeDesk stays up to date, a free Windows desktop for Claude Code. The real version is in src/main/brainTend.ts.)

Top comments (0)