The code survives a Claude Code session. The reasons behind it rarely do. Open the next session and the branch state, the approaches already discarded and the one command that proved the fix are gone. The new session either guesses or rebuilds them. A one-page handoff of about a dozen checkable claims prevents both.

A session stops being a reliable record

A Claude Code session can keep producing good work long after it stops being easy to follow. The model may still hold a useful thread. But the repository, the terminal output, the uncommitted edits and a decision from twenty prompts ago no longer agree with each other. It is not clear which one shows the current state, sometimes not even to you.

A handoff is a short working record, far shorter than a transcript. It lets the next session answer three questions without guessing: what result you want, what state the repository is in, and what evidence supports the next step. Treat compaction or closing a session like a release. The task is not finished, but its state must be easy to inspect, just as a build must be before it ships.

Skip it and a confident new session acts on an old assumption. It edits the wrong file, retries a rejected approach, treats a test that never ran as proof, or deletes a deliberate workaround. A handoff will not stop all of this. But it gives the next session a way to check before it touches the code.

Write down four things and they survive a context reset. Each protects against a different failure: the goal, the repository state, the decisions made and what has been proved.

The goal names the outcome for the user and, just as important, what is out of scope. "Finish authentication" is too broad to be safe. "Add a callback route that exchanges the provider code, and leave account linking out" tells the next session where to stop. Then an unfinished idea next door does not look like a bug to fix.

Repository state is what is actually on disk: branch, commit, changed paths, untracked files, generated files, and the exact command that reproduces the current behaviour. Read it from the working tree, because memory is exactly what the next session lacks. A local diff may hold the only copy of a fix. A failure may depend on an environment variable another terminal never set. Name the condition and leave the secret out.

Decision history explains code that looks wrong. Write down the option you chose and the ones you rejected, and the constraint that decided it. For example, a parser may keep an awkward compatibility branch only because another system still sends an older data format. Without that note, the next session "simplifies" it and brings back the bug you already fixed.

Proof status separates what you saw from what you hope. Give the exact command that passed or the check you ran, what it covered and what is still unchecked. "Tests pass" hides which suite, on which machine, and what could still fail. "pnpm test --filter billing passed after the schema update; the browser checkout has not been tried" is something a reviewer can check.

Resume, compact, and fresh sessions manage different risks

Resume when the goal has not changed, the reasoning still holds together and there is room for the next real piece of work. Resuming keeps details a summary would drop: the search results you threw out, the command that first showed the bug, the user's exact words for success. The risk is inertia. A long transcript can keep an abandoned idea alive just because it feels familiar.

Compact when the work continues in the same direction but the conversation has become long or noisy. Compaction is a lossy summary, not a reset. Record the repository state and open decisions first. A compacted context tends to keep the direction and drop the evidence you would need to question it. A good compact prompt restates the goal, points to HANDOFF.md and names the next check.

Start fresh when the task changes type, when the session has started going in circles, or when an independent review matters more than continuity. Typical cases: a redesign after a failed prototype, a security review, or a request from a different stakeholder. A fresh context questions assumptions much more easily. But it also loses the limits the earlier session discovered. The handoff turns that loss into a controlled reset.

One thing to avoid: do not start fresh just because you are unsure what is in the working tree. A new conversation cannot know whether a generated file was intended or whether a test ran with the right settings. Record and inspect the state first. Resume, compact and fresh are choices about reasoning. Checking what is really in the repository comes before all three.

Plausible assumptions cause the worst restarts

The worst handoff is rarely the empty one. It is the polished one that presents guesses as facts. "Migration complete" can hide a migration that was never applied, one tested only locally, or a schema change with no rollback. The next session then builds on something nobody checked. Research has a name for the receiving side of this. In a 2010 review in the journal Human Factors, Parasuraman and Manzey described automation bias: people who work with an automated aid tend to accept its output and stop looking for evidence against it. Experts do it as well as beginners. A fresh session has the same blind spot, because nothing in it marks a confident sentence as unchecked.

A list of changed files without their roles is almost as weak. A bare list of paths does not say which edit was the point, which file was generated and which diff was a throwaway test. If the work touched src/auth/callback.ts, schema.sql and a fixture, say how they relate. Then the receiver opens the right diff before it starts patching.

Do not hand over raw tool output instead of a decision either. A log shows what happened and leaves the meaning to the reader. Keep the relevant command, the error excerpt and the condition, then write your current guess separately. If the guess is wrong, the evidence is still useful. Some handoffs turn out to describe a connection problem, with nothing wrong in the context at all. For those, use the guide on Claude Code MCP failed-to-connect status troubleshooting rather than the handoff.

Keep one firm security rule: never put tokens, cookies, private endpoints, customer data or a full environment file in HANDOFF.md. Instead, say where credentials are supplied through the approved local process. Handoffs often outlive the session that wrote them and travel further than you expect.

Capture the state before you compact it

Write the handoff at a natural break: after a discovery, before a risky edit or a compaction, or before someone else takes over. Do not wait until the work feels done and your memory is vague. Write it while the terminal, the diff and the rejected paths are still in front of you.

The template below keeps facts, interpretation and the next action apart. If you do not know something, write "unknown" instead of inventing a plausible sentence.

# Handoff

**Captured:** YYYY-MM-DD HH:MM TZ
**Repository / branch:** `repo-name` / `branch-name`
**Base commit:** `abc1234`
**Task boundary:** One sentence describing the requested outcome.

## Outcome and scope
- Desired result:
- Explicit non-goals:
- Acceptance condition:

## Repository state
- Changed paths and the role of each:
- Untracked or generated files:
- State snapshot: `.handoff-state.txt`
- Environment assumptions without secrets:

## Decisions already made
- Chosen path and why:
- Rejected path and why:
- Assumption that still needs verification:

## Evidence
- Commands run:
- Result observed:
- Tests not run, with reason:
- Relevant error excerpt or log location:

## Next smallest action
Describe one action that can confirm or falsify the current hypothesis.

## Receiving-session prompt
Read this file, inspect the named diff and state snapshot, then verify the evidence before editing.

Keep the file short enough to read before the first reply. It is a briefing, not the project plan. If a decision really needs a design document, link its path in the repository and state the one point the next session must remember from it.

Add a small script that records the branch and diff details without any file contents. Run it from the repository root, read what it wrote, and keep the output file out of git if it should not be versioned.

#!/usr/bin/env bash
set -euo pipefail

out="${1:-.handoff-state.txt}"

{
  printf '# Repository state captured %s\n\n' "$(date -u +%FT%TZ)"
  printf '## Location\n'
  printf 'branch: '; git branch --show-current
  printf 'head: '; git rev-parse --short HEAD
  printf '\n## Working tree\n'
  git status --short
  printf '\n## Diff summary\n'
  git diff --stat
  printf '\n## Changed tracked paths\n'
  git diff --name-only
  printf '\n## Whitespace check\n'
  if git diff --check; then
    printf 'no whitespace errors reported\n'
  else
    printf 'git diff --check reported issues\n'
  fi
  printf '\n## Latest commit\n'
  git log -1 --oneline
} > "$out"

printf 'Wrote %s\n' "$out"

The script cannot tell you whether a diff is correct, whether a test hit the right service, or why a line changed. It only records part of the repository state, and the receiver still has to compare it with the current checkout. Add project-specific checks only where they are safe and help. A slow integration suite is better named as a handoff command than run automatically before every handoff.

Let the decision tree choose the context shape

Walk through this tree after you have created the state file and HANDOFF.md. It separates continuity from trust. A crowded session on the same task usually needs compaction. A session that contradicts itself should not be compacted just because the goal has the same name.

START
|
+- Is the working tree and test status captured and inspectable?
|  +- No: create the state snapshot and HANDOFF.md first.
|  +- Yes:
|      |
|      +- Has the requested outcome changed, or is independent review needed?
|      |  +- Yes: start FRESH with HANDOFF.md as the brief.
|      |  +- No:
|      |      |
|      |      +- Is the current transcript focused and still within useful context?
|      |      |  +- Yes: RESUME the session.
|      |      |  +- No:
|      |      |      |
|      |      |      +- Is the active hypothesis and next check clearly recorded?
|      |      |          +- Yes: COMPACT, then continue from the recorded check.
|      |      |          +- No: write the missing decision record, then start FRESH.

A short session can be just as unreliable as a long one. Repeated failed patches, conflicting terminal results or a misread instruction are all good reasons to start fresh. Context length is only one signal. The better test: does the conversation still help someone see the problem, or does it push the model to defend its earlier direction?

A compaction prompt should keep the task boundary: the outcome, the named files, the open decision, the proof so far and the next command. "Summarise everything" keeps the story and loses the details you need to choose between options.

Make the receiving session earn trust

Every line in a handoff is a claim. The receiving session should test the important ones before acting on them. Its first reply can state the goal as it understands it and a plan to check things. But the real work starts with looking at the repository. This short pass catches the gap between a polished note and the files on disk:

  • Confirm the branch, commit, and working-tree status match the snapshot.
  • Inspect the named diff before editing, including untracked files and generated artifacts.
  • Restate the outcome and non-goals in the receiver's own words.
  • Run or inspect the proof command, and label any result that cannot be reproduced.
  • Compare the current code against the recorded decision and its rejected alternative.
  • Do the documented next smallest action before widening scope.
  • Correct HANDOFF.md wherever it disagrees with the repository.

Measure this rather than treating it as taste. Time from session start to the first checked action shows how clear the handoff was. Work reopened because of wrong or missing claims shows the damage later. A team can track Handoff verification coverage = verified claims / claims requiring verification × 100. Agree in review what counts: checkable claims about state, tests, environment and decisions, not harmless descriptive notes.

Take a handoff with 12 claims. Three only describe things (why the branch exists, who asked, which ticket), so 9 need checking. The receiving session confirms the branch and commit, the changed paths, the passing billing test and four decisions. It cannot reproduce the browser checkout. And "migration applied" turns out true only locally. That is 7 of 9, or 78% coverage, and the two failures show exactly where the next work should start. Track time to first checked action next to it. Say sessions that start from a handoff reach it in 4 minutes on average and the rest in 25. Then the file pays for itself. If the two numbers are close, the handoffs record the wrong things.

High coverage alone does not make a handoff good if it records nothing that was hard to check. It also has to be easy to read. The receiver should find the next command, the expected result and the condition that would change the plan, without reopening the old conversation.

Skip the file when git already tells the story, then start with proof

All of this takes time, and on small jobs it is hard to justify. Say you made a 30-minute fix on your own branch, committed everything and put the test command in the commit message. Then git log -1 and git diff HEAD~1 already answer the three questions a handoff is for. Writing HANDOFF.md on top is ceremony, and ceremony nobody reads soon gets written badly.

Skipping is fine more often than process-minded teams admit. It stops being fine as soon as the working tree holds something git does not explain: uncommitted edits, a rejected approach that left no trace, a test skipped for a reason, or a second person taking over. A simple rule: if any one of these four is true, write the file. Otherwise let a clear commit message be the handoff, and name the commit to start from in the next prompt.

Either way, start the next session with proof. Its opening prompt is where the handoff gets tested. You have taken the snapshot, filled in the template and named one check. The new session's first job is to run that check and report what it saw. If the result matches, work continues in whichever mode the tree picked. A mismatch is even more useful. It shows the wrong assumption while the only thing to fix is HANDOFF.md, before any code is built on it.