/long-haul:haul-loopfrom long-haul
Phase 3 of long-haul implementing the per-turn loop: decide explore vs exploit, implement in a git worktree, run the goal's check, and keep the attempt only if it beats the incumbent — a never-regressing ratchet. Supports detaching into a background process for overnight runs.
Ships in long-haul. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
Phase 3 of long-haul. The work you do on each /goal turn: pick a mode,
make one attempt, keep it only if it measurably beats the incumbent. One
round per turn. ROUND in STATE.md is the counter.
Two ways to run: foreground (the default — one round per /goal turn, a
human watching) and --background (detach the whole haul so it survives the
terminal closing — see Running in the background).
INCUMBENT (a branch tip + its recorded SCORE on the goal's check) holds the best result so far. Nothing replaces it unless it measures strictly better. The haul therefore never regresses — a bad attempt is simply discarded..longhaul/attempt/) off the right base, you implement there, and only a measured win is merged. A failed attempt costs nothing. (Where the acceptance gate consumes a pushed branch/CI artifact, the kept incumbent is the real working branch rather than an ephemeral longhaul/incumbent — see the CI-artifact-gated exception.)/goal evaluator reads to decide whether to continue.SPEC.md declared. A long autonomous run that reaches for undeclared tools is the drift the spec exists to prevent.REPO. BASE, the worktrees, the incumbent branch, and the check all live in the target repo (REPO in STATE.md, where .longhaul/ sits) — which may not be the cwd the haul was invoked from.A long haul is a bandit problem: keep pulling the arm that's paying off, but
switch arms when it stops. Each round, choose one mode and write the choice
and its reason into R<N>.md before you implement:
BASE and try a materially different approach per the spec — not a tweak of the incumbent. A fresh arm.The stall rule (checkable, mechanical): STALL counts consecutive rounds
that did not improve the incumbent's score. When STALL reaches STALL_CAP
(default 2), the next round must EXPLORE. Any round that improves the
incumbent resets STALL to 0 and you return to exploiting.
Judgment escape hatch: even before the cap, if the incumbent's approach is
structurally incapable of reaching the goal (e.g. the algorithm is O(n²) and the
goal needs O(n log n)), explore now — and say why in R<N>.md.
Don't re-explore a dead end. STATE.md's ## Tried list records each
explored approach and its score. Before exploring, read it; pick an approach not
already tried and rejected.
The ratchet needs a durable home for the best-so-far. On the first round only
(when longhaul/incumbent doesn't yet exist), create it at BASE and check the
main tree onto it — from here on the main tree always carries the incumbent:
git branch longhaul/incumbent <BASE> && git checkout longhaul/incumbent
Record INCUMBENT: longhaul/incumbent@<BASE> and SCORE: none (no measured win
yet). The incumbent branch only ever moves in step 6, and only on a measured
win — so a losing attempt can never touch it.
Exception — CI-artifact-gated hauls. When GOAL.md's acceptance gate consumes
a pushed branch or a CI-built artifact (e.g. a prod job that runs
image:<branch> built by CI on push), an ephemeral local-only incumbent can't feed
the gate — the gate needs a real, pushed branch. In that mode, make the incumbent
the existing working branch (don't invent longhaul/incumbent); record it as
INCUMBENT: <branch>@<sha>. The ratchet's worktree isolation (steps 3–6) still
governs each local code attempt — you still attempt in a throwaway worktree and only
fast-forward a measured win onto the working branch — but the kept incumbent is
pushable, and the gate runs on the pushed incumbent. Decide this once, at setup,
from GOAL.md's gate; don't silently drift into committing on the working branch
mid-haul.
.longhaul/bg.pid names a live process, a background driver owns this haul; do not run a round, report haul_bg_status and stop. Otherwise: apply the stall rule + escape hatch, read ## Tried if exploring, record MODE and the reason in R<N>.md and STATE.md, set STATUS: HAULING: R<N>.R<N>.md's brief: for exploit, what the incumbent (R<N-1>.md + its check) still falls short on; for explore, the different approach being tried and why it might clear what exploitation couldn't.git worktree add -b longhaul/attempt-r<N> .longhaul/attempt <base>
<base> is longhaul/incumbent (exploit) or BASE (explore). Before the first win, both are the same commit, so round 1 works either way./implement is user-invoked, so inside this turn you execute its contract yourself, in the worktree (cd .longhaul/attempt): build the work per the brief; use /tdd at the pre-agreed seams; typecheck regularly; run targeted tests often and the full suite at the end; commit continuously as you go (the progress must accrete in git, not live in the working tree). Stay within the toolbox.SCORE. (The acceptance gate, if GOAL.md has one, is not run here — see The acceptance gate.)SCORE: none (no incumbent yet), any attempt that passes the check is the first win; otherwise it must beat the incumbent's score. From the main tree (which holds longhaul/incumbent):
git merge --ff-only longhaul/attempt-r<N>.BASE → different lineage): git reset --hard longhaul/attempt-r<N> — the incumbent now points at the explored lineage.INCUMBENT/SCORE, set STALL: 0.STALL. If it was an explore, append the approach + its score to ## Tried.GOAL.md's proof line there and print its output verbatim. This is the transcript the /goal evaluator judges; the same command every round is what lets it decide. Note in R<N>.md what's still short of the goal.git worktree remove .longhaul/attempt --force, git branch -D longhaul/attempt-r<N>, git worktree prune. Bump ROUND; append a one-line R<N> entry to STATE.md's round log. Then check the proof from step 7: if the goal condition now holds, set PHASE: wrap, STATUS: GOAL-MET, and proceed to haul-ship — don't rely on /goal's evaluator alone to route you. But if GOAL.md has an acceptance gate, the condition is not met on a green ratchet alone — the gate's pasted evidence must also be in the transcript (see below). Otherwise end the turn; /goal decides whether to start another.When GOAL.md carries an acceptance gate (an expensive/external/one-shot
tier — a prod job, a deploy, a sign-off), the per-round loop above optimizes the
ratchet only; the gate is a separate, terminal phase, not a round — don't
tick ROUND for it, and don't fold it into the same turn as the ratchet work.
Don't run it every round. It costs hours and real money — running it each turn is the waste the two-tier split exists to prevent. Fire it once, after the ratchet is green and the incumbent is locked (the score holds and further rounds aren't improving it).
Pre-register the expected result before you spend the money. A bare "job SUCCEEDED" proves nothing about correctness. Before firing, with the toolbox's read-only tools (e.g. the DB MCP), capture and paste:
Run it across turns — never block one turn for hours. A multi-hour gate
(CI image build → multi-job prod run → polling) is launched, not awaited: submit
the job, print what's pending, and end the turn. Each later /goal turn checks
status and advances a stage — never churn synchronously holding a turn open. Use
ScheduleWakeup for external state the harness can't notify you about.
Paste evidence verbatim; only then is the goal met. When the gate completes,
paste the job-SUCCEEDED line and the after-query against the pre-registered baseline
(prediction matched, invariants held). GOAL-MET requires both the ratchet output
(re-printed) and this gate evidence present — a green ratchet with no gate evidence
is not done.
If the gate fails, it's a finding, not a win: the ratchet may be green while the
real artifact is wrong. Treat the failure as the next round's brief (a fix to
exploit), and don't mark GOAL-MET.
A goal with no acceptance gate skips all of this — the ratchet is the whole signal.
Foreground hauling rides /goal, which lives in the interactive session — close
the terminal and it dies. --background instead hands the whole haul to a
detached driver (reference/haul-bg.sh) that survives the terminal, and on
Linux the whole login session — so you can submit it on a remote box and let
it grind overnight. The driver runs one headless claude -p round at a time,
runs the goal's check between rounds, and stops when the goal holds or the round
cap trips.
Three things differ from foreground, and you must set them up before detaching:
/goal's evaluator is gone, so the driver
decides "goal met" by running .longhaul/check.sh (exit 0 = met). If it
doesn't exist, write it from GOAL.md's ratchet check — the runnable form,
exit 0 when the condition holds, printing the measured value (template in
../long-haul/reference/file-formats.md).
Confirm it with the user. check.sh can only express the ratchet — an
acceptance gate (a prod job, a deploy, a sign-off) can't be reduced to an
exit-0 between-rounds script, so a goal that requires a gate is not fully
background-able: the detached driver can ratchet to green unattended, but the
gate stays a foreground/manual terminal step. Say this — don't let the user
detach expecting the gate to fire itself.claude with
--permission-mode bypassPermissions; there's no one there overnight. For pure
code rounds the blast radius is bounded by the per-round worktree — but that
bound is false the moment a round's check or toolbox reaches external or paid
infra (a prod job, a deploy, an MCP write): with no human in the loop it can
spend real money and mutate production. If the toolbox includes anything like
that, say so plainly and get explicit go-ahead before detaching — or keep the
gate out of the background run entirely..longhaul/PROGRESS.log (the [LoopN] template — see
file-formats). That file is how the user checks in on a detached run.To launch:
source ~/.claude/skills/long-haul/reference/haul-bg.sh # or the plugin path
haul_bg_start # detaches; prints the PID + log paths. Safe to close the terminal.
haul_bg_status # is it alive? + tail PROGRESS.log
haul_bg_stop # kill the run (PID-scoped; never killall claude)
The single-driver lock. Exactly one thing may drive .longhaul/ at a time —
a foreground /goal loop and a live background driver both mutating the same
incumbent corrupts it. haul_bg_start refuses if a driver is already alive
(bg.pid). And a foreground round must refuse to run while bg.pid is
alive — check it at the top of step 1; if a driver owns the haul, this session
is read-only, so report haul_bg_status and stop.
Mid-haul, to free the terminal: first ensure .longhaul/check.sh exists — a
foreground run never created one, and haul_bg_start refuses without it, so
write it from GOAL.md's stated check and confirm with the user. Then
haul_bg_start (it resumes from STATE.md's ROUND, picking up where the
foreground loop was), then /goal clear to retire the interactive loop, then
close the terminal. The lock makes this safe — once the driver holds bg.pid,
the cleared foreground loop wouldn't double-drive anyway. To come back:
haul_bg_stop and resume foreground with a fresh /goal, or just watch with
haul_bg_status until it finishes and pick up at haul-ship.
You don't poll the goal — /goal does. It clears automatically when the
evaluator sees the condition met in the transcript, and the next thing you do is
haul-ship. But hold and surface to the user (don't burn turns) when:
ROUND reaches ROUNDS (cap) without the goal clearing → set STATUS: WAITING-USER: cap-decision, report partial against the incumbent, and ask whether to raise the cap, accept, or stop. (Setting the status means a resume re-presents the question instead of running another round.)STATUS: WAITING-USER: cap-decision, surface rather than grind.STATUS: BLOCKED, surface.After the round, print a 5–10 line digest before ending the turn so the user
(and the /goal evaluator) see progress and the explore/exploit path:
**R4 — exploit** (stall 0, incumbent improving)
- attempt: tightened the inner loop, memoized the lookup; 3 commits
- check: bench/price.py p95 = 41ms (goal <=40ms); was 48ms
- verdict: KEPT — new incumbent @ a1b2c3d, score 41ms
- main-tree check: p95 41ms — still 1ms short of goal
**R6 — explore** (stall hit cap; exploit plateaued at 41ms)
- approach: replace the sort-based grouping with a hash bucket (off BASE)
- check: p95 = 36ms (goal <=40ms)
- verdict: KEPT — explore beat incumbent, reset incumbent @ e5f6a7b
- main-tree check: p95 36ms — goal condition now holds
The steps above already encode the ratchet, the worktree isolation, the stall rule, and the toolbox bound — follow them and those traps don't arise. The non-obvious ones worth calling out:
| Hazard | Rule |
|---|---|
| Goal proof never in the transcript | /goal's evaluator runs no tools — re-run the check on the main tree and print it every round (step 7), or the loop never clears. |
| Leaving the attempt worktree behind | Some abort paths skip step 8 — git worktree remove --force + git worktree prune on every exit, not just the happy path. |
killall claude to stop a background run | Kills the user's own session too. Always haul_bg_stop (PID-scoped). |
If re-invoked while STATUS: HAULING: R<n>: inspect .longhaul/attempt/. If the
worktree exists with commits, the attempt is in flight — continue implementing
or, if the check already ran, judge it (step 6). Don't spin a second worktree for
the same round. If STATUS: GOAL-MET or cap-decision, the loop is over — go to
haul-ship, don't start a round.
If invoked standalone (not by the orchestrator), once the goal clears end by
suggesting: "Next: haul-ship to report and open a PR."
What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
npx claudepluginhub ccomkhj/skills --plugin long-haul5 skills in this plugin
/haul-goalPhase 2 of long-haul. Turn .longhaul/SPEC.md into a /goal-ready completion condition — one measurable end state, a stated transcript-demonstrable check, the constraints that must hold, and a round cap — written to .longhaul/GOAL.md plus the literal /goal line for the user to run. If the end state isn't clear or measurable, iterate with the user one question at a time until it is. Use after haul-spec, or standalone to craft a good /goal condition from a clear spec./haul-shipPhase 4 of long-haul — the finishing move. Fires once the /goal condition is met (the goal auto-clears) or the round cap trips. Reports goal-vs-achieved with the final check output, the explore/exploit path each round took, and the net kept diff; then commits the incumbent on a feature branch and opens a PR. Always confirms the branch and PR body with the user before pushing. Use as the last phase of a long-haul run./haul-specPhase 1 of long-haul. Turn the user's ask into a well-defined spec — a located deliverable, a transcript-demonstrable success signal, the toolbox of skills + MCP the haul may use, constraints, and out-of-scope. If the ask is vague, grill it first. Produces .longhaul/SPEC.md. Use as the first phase of a long-haul run, or standalone to harden a fuzzy ask into something buildable before setting a goal./long-haulAuto-chaining orchestrator for a solo long-horizon goal loop. Sharpen a spec (grill it if vague), forge a /goal completion condition, then haul toward it turn after turn in a git worktree — each turn deciding explore (a fresh approach off the base) vs exploit (refine the incumbent), keeping only measured wins, until the goal holds. Use when the user wants one agent driven relentlessly toward a verifiable target over many turns — not a one-shot edit. Reach for it when the user says "keep working until", "loop until done", "long haul", or hands a spec to be built to completion.Other plugins' skills for the same job