DEV Community

Darkø Tasevski
Darkø Tasevski

Posted on Originally published at darkotasevski.dev

Giving coding-agent session notes one place to live

#ai

My coding agents write their notes (handoffs, review findings, PR bodies, the evidence behind a plan) into a private git repository, so that a later session, or I a week later, can read what happened and why. That folder slowly turned into a scratchpad, and what follows is why that happened, the small script that now decides where every note goes, and the part that took the most thought, which was deciding what counts as a "session" in the first place. The script and its tools are done, but the files that were already in the folder haven't been moved into the new layout yet, so the numbers below describe the folder as it was.

How the folder got messy

The idea was simple enough, one folder per project and one folder per session inside it, named after the issue number or a short slug. By the time the folder got a proper look, it held about 6.4 GB in roughly 200k files, of which the Markdown, the part that was supposed to be there, was about 250 MB. One project (an iOS reading app I'm working on) was 6.2 GB on its own, with 3,022 loose files next to 402 improvised directories, and it was spread over about a dozen top-level folders named after git worktrees instead of after the project.

None of that was one bad decision. The path rule lived as prose in four skills (the instructions an agent loads when it runs a step like "close the session" or "open the PR"), each one building a flat path like <root>/<repo>/<date>-<slug>-pre-pr-findings.md, and none of them had a per-session folder. Two of them took the project name from the working tree, a script in a third took it from the main checkout, and the fourth never said, and the first two give different answers as soon as we're in a linked worktree:

# in ~/Code/my-app-worktrees/4821-ipad-layout, on branch dev/4821-ipad-layout
git rev-parse --show-toplevel                            # .../my-app-worktrees/4821-ipad-layout
git rev-parse --path-format=absolute --git-common-dir    # .../my-app/.git
Enter fullscreen mode Exit fullscreen mode

The first one names the worktree and the second one leads back to the main checkout, so the same project ended up under a new top-level folder for every worktree an agent created. On top of that, nothing had a home for heavy output, so agents put it in the notes folder too. A few saved memories told them to write "run logs, mutation folders and driver copies" under a session folder that was never defined, and a test-evidence script in the app repository pointed its output root there, which is where 1.7 GB of .xcresult bundles and app-data backups came from.

One resolver instead of four paragraphs

The fix is one shell script that every writing skill calls instead of computing the path itself. It answers a few questions (which project, which session folder, where scratch goes) and does some housekeeping (an audit that reports anything outside the layout, and a prune for old scratch). Running it in the worktree above gives:

session-path.sh project                                # my-app
session-path.sh resolve --slug ipad-layout --create    # <archive>/my-app/4821-ipad-layout/
session-path.sh scratch --create                       # ~/.local/state/agent-scratch/my-app/4821-ipad-layout/
Enter fullscreen mode Exit fullscreen mode

Inside a repository, the project name comes from --git-common-dir, so a main checkout and all its worktrees share one folder. Scratch gets its own root with the same <project>/<session>/ shape, outside the notes repository and outside /tmp (a restart emptied /tmp once and took test evidence I wanted to keep with it). Each skill links the script into its own folder and calls it as "${CLAUDE_SKILL_DIR}/scripts/session-path.sh", and the skills repository's check script fails when a skill that uses the resolver has its own copy of it instead of the link, so the skills can't drift apart into slightly different versions of the rule the way the prose did.

What counts as a session

The session folder is named after a key when there is one (the issue the branch is for) and after the slug otherwise, and the key comes from the branch name. If the project already has a folder for that key, the resolver reuses it whatever slug was passed, which is what keeps one issue in one folder across sessions (two matching folders is an error that lists both), and an explicit --key or --session overrides the branch.

Reading a key out of a branch name means choosing which mistake we'd rather make. Two unrelated sessions merged into one folder means their notes are interleaved, and a later session can pick up the wrong handoff, while a session that misses its key only gets a folder named after its slug, which is a bit untidy. So I went with a strict rule, where anything it doesn't recognize gives no key at all:

TRACKER_KEY_PATTERN='^([A-Z][A-Z0-9]+-[0-9]+)(-|$)'        # ABC-1234-x -> abc-1234
ISSUE_WORD_PATTERN='^(issue|issues|fix|epic)-([0-9]+)(-|$)' # dev/issue-4821-x -> 4821
BARE_NUMBER_PATTERN='^([0-9]{2,6})(-|$)'                    # dev/4821-x -> 4821
DATE_PATTERN='^[0-9]{4}-[0-9]{2}-[0-9]{2}'                  # 2026-11-23-notes -> no key
Enter fullscreen mode Exit fullscreen mode

The function splits the branch on / and checks the start of each segment, leftmost first. A tracker key has to be uppercase, which keeps names like swift-6-migration and pr-4-… out, and a bare number has to be 2 to 6 digits and not the start of a date. The date check is a separate test because bash's regular expressions have no lookahead, and the whole thing has to run on the /bin/bash 3.2 that macOS ships, so the lower-casing goes through tr instead of ${x,,}.

The first version, from the first draft of the design doc, was one looser expression, (^|/)([A-Za-z][A-Za-z0-9]+-[0-9]+|[0-9]+)-, and it took a review that ran it against 923 real branch names to show how badly it did. pr-4-…, swift-6-migration and readium-4-update all read as tracker keys, so unrelated sessions would have shared a folder, while dev/issue-4821-x gave the key issue-4821 and dev/4821-x gave 4821, so one issue got two folders.

When in doubt, the rule should miss rather than merge. The strict version follows that as far as a name allows, but it isn't a guarantee. A branch like ios/18-support still reads as issue 18, dev/UTF-8-cleanup as utf-8, and a slug folder that happens to start with a number can be picked up by a key with that number. A one-digit issue branch like dev/7-x gets no key at all and lands in its slug folder, which is the cheap side of the trade.

Rewriting paths without touching other sessions' work

Moving files around breaks every path that cites them, and the notes cite each other a lot, as do saved memories and docs in the project repositories. So the layout comes with a map of old paths to new ones that stays around after the move, a locate command that answers "where did this go", and a rewrite command that updates citations in place.

Which files rewrite looks at matters as much as what it changes. Inside a git work tree it asks git, and lists tracked and untracked files but leaves ignored ones out:

my $listed = git_output('-C', $base, 'ls-files', '-z', '--cached', '--others', '--exclude-standard')
  // fail(2, "git ls-files failed in $base");
Enter fullscreen mode Exit fullscreen mode

Git lists a nested repository or linked worktree as one dir/ entry instead of descending into it, and the regular-file test after it drops that entry, so other sessions' worktrees stay out. Outside a work tree it falls back to walking the directory, and that walk skips any folder that holds a .git.

The first version walked the directory with Perl's File::Find and skipped only .git, so pointed at the app repository it would have gone through 37 GB of nested agent worktrees under .claude/worktrees/ and reached 440 files in other sessions' checkouts that cite the notes folder. The second version listed tracked files only, which looked like the safe choice until a re-review pointed out that the notes repository itself is mostly untracked files (one commit tracks under 3,000 of them), so rewrite would have skipped almost every note and its --check mode would have reported them clean.

Checking that the tests can fail

A test only proves something if we've seen it fail on the bug it's meant to catch, and a test suite that looks thorough is easy to take on trust, which is how a pre-PR review of this one still found 75 test claims across its three test scripts with no evidence they had ever failed, some of which could not fail as written. The "long slug" test used a 31-word slug, which the word-count rule rejected before the 50-character limit it was meant to check was ever looked at, and the loops that fed bad arguments to each command asserted only exit code 2, which the script also returns for an unknown command, so the loop for claims would have passed with no claims command at all. The fix is the method I wrote about in TDD and mutation testing when coding agents write the tests, breaking the line each claim is supposed to observe and watching the test go red. Every one of the 103 claims at the end (more than 75, since the fixes added and split tests) was seen to fail under a mutant of the line it checks.

Summary

When several agent skills each describe a shared path in prose, they drift, and agents fill the gaps with whatever seems reasonable at the time, so the path belongs in one script that every skill links to, with a check that keeps them on the same copy. For a rule that guesses identity from names, like which branches belong to one issue, it helps to decide first which mistake is cheaper, make the rule strict in that direction, and check it against the real names and not only the examples you had in mind while writing it. Anything that rewrites files in bulk needs its file listing tested for what it leaves out as well as for what it includes, because here both obvious listings were wrong, in opposite directions, and every one of those tests is only worth something once you've watched it fail.

Top comments (0)