Dev.to Security πŸ” Cybersecurity πŸ‘ 0 πŸ“– 7 min read

Building a second brain for my AI agents

I use Claude Code as one of the tools on my Linux machine. The problem is that every session starts from zero. Whatever it worked out yesterday, whether that is a fix, a workaround or how I like things done, is gone the

I use Claude Code as one of the tools on my Linux machine. The problem is that every session starts from zero. Whatever it worked out yesterday, whether that is a fix, a workaround or how I like things done, is gone the next time I open a terminal. I was explaining the same things again and again.

So I gave it a long-term memory, and I built the whole thing in one day. It started as an empty vault. By the evening it had replayed a month of my old session transcripts into about 110 notes: 32 sessions, 11 projects, 13 desktop themes, 15 system notes and 22 "lessons". Lessons are gotchas it learned the hard way, so it does not have to learn them twice.

The idea

The memory is an Obsidian vault. It is plain Markdown files, so the model can read and edit them with the same tools it uses for code, and I can browse it in a nice graph view. I picked Obsidian because it is just a folder of Markdown on my own disk: no database, no account and no cloud sync, so I can version it with git and keep it off every server, and its [[links]] give me backlinks and a graph without any extra work. Each kind of thing gets a folder and a hub note:

  • Sessions: one note per conversation, covering what I asked, what was done, what it touched and what was learned
  • Projects, Systems, Rices: one note per thing, linked from every session that touched it
  • Lessons: one note per gotcha, linked from where it bit
  • Agenda: my open tasks

The rule that makes it work is simple: every note links to what it touched. With a month of history in it, the links are the knowledge. Open the note for my status bar and you see every session that broke it, every lesson it caused and the theme that depends on it.

The pipeline

Writing the notes by hand would have lasted about three days, so the whole thing is automated. There are four stages.

1. Capture. Claude Code supports hooks: scripts that run on events like a new prompt, a file edit or the end of a session. One small Python hook appends a timestamped line to a daily log for each of these. Commands are logged by their description ("Mount USB drive and list its contents"), never the raw command, so a password passed on a command line never ends up in the vault. I also raised the transcript retention from 30 days to effectively forever and archive the raw transcripts every day.

2. Memory. When a session ends, a background job turns its transcript into a session note. Transcripts can be hundreds of megabytes, so a digest script first strips them down to prompts, replies and one-line summaries of each tool call. Then a separate, headless model run reads the digest and edits the vault: it writes the session note, links it to existing notes, adds lessons and updates the hubs. The same job can loop over every archived transcript, oldest first, which is how a vault built in one day already knew about the past month.

3. Recall. When a new session starts, another hook injects a short briefing: open tasks, the last three sessions, and notes relevant to the folder I started in. "Relevant" is partly link-based and partly semantic. A local embedding model (served by ollama on 127.0.0.1) indexes every note, so "internet broke after I plugged something in" finds the Wi-Fi adapter and VPN notes even though they share no words with it.

This is also where it saves the most tokens, and the reason I find it far more efficient. Without the memory, a new session spends a lot of its budget rediscovering things: reading the same config files, re-running the same checks, and me pasting in the same background. The briefing is a few thousand characters, and it points straight at the one or two notes that matter, so the session can open those instead of searching the whole machine.

4. Upkeep. Two systemd user timers do the boring parts. A daily one archives transcripts, syncs the git status of my project repos into their notes, refreshes the "similar notes" boxes, draws activity charts, rebuilds the map, commits the vault and makes a backup. A weekly one writes a digest of the week.

The security part

A tool that records everything I do on my machine is also a very good target. The vault knows my projects, my habits, my hardware and every problem I have had. I designed around that from the start.

  • The summariser can only read and edit files. The headless run that writes notes has no shell and no web access. Transcripts contain web pages, READMEs and command output, and any of those could carry a prompt injection. If one does, the worst it can do is write a bad note, which git can roll back. It cannot run anything.
  • A recursion guard. The summariser is itself a Claude Code session, so without a guard it would log and summarise itself forever. An environment variable makes the hook exit when it is set.
  • Secret scanning on every commit. A git pre-commit hook scans every added line for API keys, tokens, private keys, password lines and email addresses, plus a personal blocklist of words that must never appear (my city, for one). If it finds anything, the commit fails and I get a desktop notification.
  • No remote, ever. The vault is a local-only git repo. It is never pushed anywhere, so the full history stays on my machine.
  • Local embeddings. The semantic search runs on my own GPU. My notes are never sent to an embedding API.
  • Encrypted backups. A daily tar | zstd | gpg archive (AES-256) of the vault, transcripts and scripts goes to a separate physical drive, and the newest 14 are kept.

It is not perfect. The raw transcripts are still a complete record of every session, sitting unencrypted in my home folder. Anyone who can read my home folder can read them, but anyone who can read my home folder already has everything else too.

What came out of the backfill

The most useful part turned out to be the lessons, not the session notes. Replaying the month pulled out gotchas I had already forgotten about. Some examples:

  • The out-of-tree DKMS driver for my USB Wi-Fi adapter does not build on the mainline kernel, and only builds on LTS after renaming one function that clashes with a kernel symbol. Stay on LTS, and check dkms status after every update.
  • Flatpak apps need an NVIDIA GL add-on that matches the host driver version exactly. After a driver update, the game just logs couldn't initialize OpenGL and exits.
  • pgrep -f something in a script also matches the shell running the script.
  • Hot-reloading my status bar with a signal after big CSS changes crashed it, so the theme switcher now kills and restarts it instead.

Each of these cost an hour the first time. Now, when one of them comes up again, the session starts with the briefing, sees the lesson and can go straight to the fix.

The second surprise was how much it pieced together about me. From a month of conversations, it wrote a note with my hard rules (all traffic goes through the VPN, explain before touching DNS or routes, never overwrite an old theme) and my taste, including things I had rejected. Every session now starts with that instead of me repeating it.

Then I copied it to my laptop

At the end of the day, I copied the vault to my laptop and opened it there. All 110 notes were there, the graph looked the same, and everything was linked. Then I checked the machinery, and none of it was there. No hooks, no scripts, no timers, no backup drive. The git pre-commit hook was still in the repo, pointing at a secret scanner that did not exist, so every commit would have failed.

Which makes sense. The vault is only the output. The thing that makes it a memory is the set of hooks and jobs around it, and they lived in ~/.claude and ~/.config on the other machine. What I had copied was a very well-organised diary.

The fix is boring: keep the scripts and unit files in their own repo, with an install script that sets up the hooks and timers on a new machine and refuses to commit to the vault until the scanner is in place. I should have done that from the start, because the same thing would have happened after a reinstall.

Would I recommend it

Yes, with one condition: treat it as sensitive data from day one, not as an afterthought. The useful part is cheap. A hook, a summariser and a briefing are a few hundred lines in total. Making it safe to keep is most of the work, and it is the part worth thinking about before you start logging your whole life into Markdown.

πŸ“° Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.