Jakob Christensen / Cases#001#002#003#004
6 Rules + 3 Habits to finish Agent Sessions cleanly
Relevant skills: /strays, /am-i-done, /case-closed, /tidy-worktrees, /lanes, /fresh, /tidy-sessions, /attic
Archiving a chat does not mean its work has landed. I learned this from an agent that told me its work was merged and then deleted its work folder. The check was still running, and the commit it had reported did not exist. The branch was still there, so nothing was lost. After that I counted what else was lying around in my repositories.
By landed I mean merged into main, the branch of the project that everything else builds on. Until then the work sits on a side branch, and closing the chat is the easiest way to forget it. The rules below make an agent finish all of it before it says "done". The habits after them are the part only you can do.
Rule of thumbDone means committed, merged, pushed and nothing waiting.All four count. An agent that says "done" after the first one leaves the rest for you to find later.
Four questions to ask before you close a chat
You can ask your agent each of them in plain words. For each there is also a skill, a ready-made instruction file your agent follows when you type its name, that does the asking for you.
1Committed?
A commit is a saved snapshot of your changes in git, the tool that keeps the project's history. Until then, the changes are only files on disk.
Ask: "Is anything still not committed?"
Ask in the chat that did the work, because a chat in another folder cannot see its changes.
2Merged into main?
Agents usually work on a branch, a separate line of changes, so they do not get in each other's way. Merging brings that branch back into main, the project's main line (also called the default branch).
Ask: "Is all of this work in main?"
If not, have the agent merge it before it deletes its extra work folder (a worktree).
3Pushed?
Pushing sends your commits to the remote, the copy on a server such as GitHub. Until then they exist on your computer only.
Ask: "Is anything saved only on my computer?"
4Nothing waiting?
No open question, no command still running in the background, no folder left behind.
Ask: "Are you still waiting for me or for a running job?"
In a list of chats, one that waits for your answer looks just like one that is finished.
Example
When I counted my 14 repositories on 1 December, this was still open:
- 61branches had not been merged into main.
- 132extra work folders (worktrees) were still there, 20 of them with changes nobody had committed.
- 112commits existed only on my computer.
Not all of this was forgotten. Some of it was work in progress, some was on purpose. But a list of branches does not tell you which is which, and that is why the agent should finish its part before it says done.
Rules for your agent (paste into CLAUDE.md or AGENTS.md)
These rules are written for your agent, not for you, which is why they read like orders. Paste them into CLAUDE.md or AGENTS.md, the file of standing instructions your agent reads at the start of every session, or download them.
-
Merge before you say done
Commit your own finished work and merge it into the default branch before you report a session as done. Where your team reviews pull requests (proposed changes that someone approves before they are merged), open one instead and do not merge it yourself.
Why
Once the chat is closed, nobody remembers the branch. An unmerged branch then looks the same whether it is still in progress or simply forgotten.
-
Never say done while something is open
Never report "done" while a check is red, a question is open, work is uncommitted, or a background job still runs. A job counts as finished only when its own output shows it ended. If something is still open, end with what it waits for.
A check here means the project's tests, lint and build. A background job is a command that keeps running after the agent has replied, such as a long test run.
Why
"Done" reads like permission to close the chat. If something is still running at that moment, nobody is watching it anymore.
-
Check the branch before you remove its worktree
In short: before the agent deletes a work folder, it proves that the folder's branch is already in main. The commands are for the agent. The second one is needed after a squash merge, which turns a whole branch into one new commit, so git no longer recognises the branch as merged.
Remove a worktree only after its branch is in main:
git merge-base --is-ancestor <branch> mainsucceeds. After a squash merge that check fails. Then, with Git 2.38 or later, the branch is in main whengit merge-tree --write-tree main <branch>prints the same id asgit rev-parse main^{tree}. Delete the branch withgit branch -d. Use-Donly after that second check.Why
The work folder is the easiest place to finish a branch from, because everything the agent was working on is still there. Once the folder is gone, an unmerged branch is only a name in a list.
-
Close with a report that can be checked
End with a short report: what is not pushed, what is not deployed (not live yet), and for each finished step the command you ran and its result, one line each.
Why
With a report like that you see what is left without scrolling through the whole chat, and you can rerun any command to check what the agent claims.
-
Name every open branch in the handoff note
A handoff note names every open branch and what it is missing from main.
A handoff note is what a session leaves for the next one, so the next one can continue without reading the old chat.
Why
The next session has not seen this conversation. Whatever the note leaves out, it has to dig up again on its own.
-
Write down what you put off
Write every step you put off into a to-do file in the repository, in the same turn (one reply of the agent).
Why
A step that exists only in a chat disappears when the chat is closed. A file in the repository is still there when the next session starts.
Habits for you (an agent cannot do these)
-
Ask before you close
Before you close a chat, run /am-i-done.
Skill: /am-i-done checks whether the whole task is finished and lists what is still open.
Why
Closing a chat takes its open points out of sight, and asking before you close is cheaper than finding them later.
-
Push regularly
Push your local commits regularly, for example with /strays.
Skill: /strays finds work that is only on your computer and pushes it safely.
Why
A commit that was never pushed exists on one machine only. If that machine breaks, the work is gone with it.
-
Let a hook catch a false "done"
Ask your agent to add a Stop hook that sends the agent back when it says "done" while work is uncommitted or a job still runs.
A Stop hook is a small script Claude Code runs every time the agent finishes a reply. "Why a hook" further down lists what to watch out for when you set one up.
Why
A line in CLAUDE.md is a request, and the agent can forget it. A hook is a script that runs every time.
The details
Why a hook
A line in CLAUDE.md is a request: Claude Code's docs say there is no guarantee it is followed. A Stop hook is a script Claude Code runs every time the agent finishes responding. If it exits with code 2, the agent gets the script's message and keeps working.
Three things to get right when your agent sets it up:
- The hook runs after every answer, not only at the end of a session. So let it check only when the last message ends with your closing word, for example "Done." as its own paragraph (the hook gets the message as
last_assistant_message). Otherwise every question to you gets blocked too. - Let it pass when
stop_hook_activeis true. Claude Code sets this field when the agent is already continuing because of an earlier block. Otherwise the hook can loop. - Each time the hook blocks the agent, it costs one more model call. A
SessionEndhook cannot block at all.
How the numbers were measured
Summary
- On 2026-12-01 my 14 repositories held 132 extra worktrees, 61 local branches not in the default branch, and 112 commits on no remote (in 3 of the 9 repositories with a remote). 1,051 of 2,659 main sessions in October and November ended with my closing word, "Fertig." (German for "Done.") or "Done.". For the branches those sessions created, I cannot say how many were never merged.
Measurements
| Repositories | 14 of my local repositories, five of them without a remote |
|---|---|
| Worktrees | 132 besides each main checkout, 20 with uncommitted changes (in 3 repositories) |
| Branches not in the default branch | 61 (34 with a worktree) |
| Commits on no remote | 112 in 3 of the 9 repositories with a remote, oldest 26 days, and 1,135 in the 5 repositories without a remote, oldest 51 days |
| Main sessions, October and November 2026 | 2,659, of which 1,051 (40 %) whose last agent message has "Fertig." or "Done." as its own paragraph |
| Of those 1,051, sessions that created a branch | 350 (33 %): 21 had every branch merged into the default branch, 1 had a branch that git still shows as unmerged although its change is in the default branch, 328 had a branch that no longer exists, so I cannot tell |
Commands
| Count | Command |
|---|---|
| Worktrees | git worktree list --porcelain |
| Branches not merged | git branch --no-merged <default> |
| Commits on no remote | git log --branches --not --remotes --oneline |
| Uncommitted changes | git -C <worktree> status --porcelain |
| Branch merged? | git merge-base --is-ancestor <branch> <default> |
The counts are a snapshot of 2026-12-01, taken read-only. The session numbers come from my local transcripts (the .jsonl files Claude Code stores under ~/.claude/projects).
Method and limitations
- No
git fetchwas run, so "on no remote" is relative to the remote state at my last fetch. Some of the 112 commits may already be pushed. - 79 of the 112 commits are in one repository I deliberately do not push.
- A branch counts as unmerged against the local default branch only. A branch landed by squash or cherry-pick can still show as unmerged.
- The 61 branches and 132 worktrees include work in progress that is not meant to be finished yet. They show how much is open at once, not how much was forgotten.
- A session counts as "done" when "Fertig." or "Done." stands as its own paragraph in its last message. That is my own closing word, not something Claude Code records.
- A session's branches are found from the
git worktree add -b,git switch -candgit checkout -bcommands in its transcript. This is a loose match. I delete a branch after it is merged, so most branches were gone and I could not check them. The result says nothing about how many sessions ended unfinished. - Only my work was measured: one person, one machine, my own repositories.
- I did not measure how much any rule helps. The rules come from experience.