Skip to content
mentoor

Topic 1 of 5 · 4 min read

Harness structure for multi-session agents

Goal: Design a harness whose first session sets up the project and whose later sessions each complete one feature and end in a clean, committed state.

A long-running agent job works when the first session builds the scaffolding and every later session finishes exactly one piece of work and leaves the repository clean. The agent has no memory between sessions. The harness, meaning the scripts and files around the model, is what carries the job forward.

Check yourself · 1 of 3

What should the first session of a multi-session agent job do?

Choose one answer

Why it matters

A model that tries to build a whole app in one go runs out of context half way through a feature. The next session then finds half-written code and no idea what was meant. Anthropic's write-up on long-running agents describes this failure and the two-part fix below. Claude Code, Codex and Cursor agents all behave the same way: each new session starts blank.

How to design it

  1. The initializer session runs once. It reads the goal, writes the feature list, creates an init.sh that installs and starts the project, writes an empty progress file and makes the first commit. It builds no features.
  2. Every later session follows the same short routine. Confirm the working directory, read the progress file and git log, run init.sh, run the existing tests, pick the one failing feature with the highest priority, build it, test it, commit and update the progress file.
  3. One feature per session. The limit is a rule in the prompt and a check in the harness, not a hope.
  4. End in a clean state. The tree builds, the tests that passed before still pass, everything is committed, and the notes say what comes next. A reviewer could merge the branch at that moment.
  5. Test before building. The first step of each session is a smoke test of what already works. Without it, a broken earlier feature gets buried under new work.

Before and after

Weak harness prompt:

Build the invoicing app. Work until it is done.

Better harness prompt for sessions after the first:

Read PROGRESS.md and the last 10 commits. Run ./init.sh and npm test.
If anything fails, fix that first. Then take the first feature in
features.json with "passes": false, implement only that feature, run
its test, commit, and add a dated entry to PROGRESS.md. Stop.

The first version hands the agent an open-ended goal and no memory. The second names the memory (progress file, git log), the start check, the single unit of work and the stopping point.

What goes wrong

  • One-shotting. The agent starts many features, finishes none, and the session ends mid-edit.
  • Declaring victory early. With no feature list, a later session sees a working demo and announces the job done.
  • A dirty hand-off. Uncommitted edits and failing tests mean the next session spends its context untangling them.
  • No start check. The new session builds on a broken base and cannot tell which change broke it.

Examples

A session routine as a numbered list

A team runs an agent overnight on a billing service. Its session prompt lists seven steps: pwd, read PROGRESS.md, read the last 10 commits, run ./init.sh, run the test suite, implement the first feature with "passes": false, commit. At 02:10 a session finds the suite red at step five. It fixes the failing test first, as the routine says, and only then touches the next feature.

Initializer versus coding session

The initializer session produces features.json with 40 entries, init.sh, PROGRESS.md and one commit named init: scaffold and feature list. Its prompt forbids writing application code. Session two starts from that commit and builds feature one. Splitting the roles keeps planning from eating into building.

Key points

  • An initializer session sets up the repository, the feature list, the start script and the first commit, and builds no features.
  • Every later session reads the notes and git log, runs a smoke test, finishes one feature and stops.
  • The agent has no memory between sessions, so the harness files and the git history are the only continuity.
  • A clean end state means the code builds, earlier tests still pass and everything is committed.
  • Run the existing tests at the start of every session so a broken base is found before new work lands on it.

Common mistakes

  • Letting the first session also build features, so the plan and the setup are rushed.
  • Giving an open goal such as "keep going until done" with no unit of work and no stopping rule.
  • Ending a session with uncommitted or failing code because the context ran out.
  • Skipping the start-of-session smoke test and discovering the breakage three features later.

Check yourself · 2 of 3

Why do later sessions each finish only one feature?

Choose one answer

Check yourself · 3 of 3

A new session starts and the test suite from the last session is failing. According to a good harness routine, what comes next?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic