Install any skill in seconds. Free to start, no credit card required.
Get Started Free →How Cavekit's autonomous execution loop works — state machine, stop hook, completion sentinel, lock, budgets, iteration cap. Read this skill any time you are about to run /ck:make, debug a stuck session, or write a new command that needs the loop. Trigger phrases: "how does the loop work", "stop hook", "autonomous", "completion sentinel", "why is the session looping".
| Test case | Without → With | Effect | Δ tokens | Δ turns |
|---|---|---|---|---|
| case-10 | ✗→✓ | ▲ Improved | -17% | 0% |
| case-11 | ✗→✓ | ▲ Improved | 55% | 0% |
| case-01 | ✗→✓ | ▲ Improved | 14% | 0% |
| case-02 | ✗→✓ | ▲ Improved | -16% | 0% |
| case-03 | ✗→✓ | ▲ Improved | 14% | 0% |
The loop lets a single /ck:make invocation run dozens of agent iterations without user intervention, while still respecting per-task budgets, session budgets, and explicit user-approval gates.
┌─────────────────┐ Stop event ┌────────────────────┐
│ Claude Code │──────────────▶│ stop-hook.sh │
│ session │ │ (.cavekit/loop │
└─────────────────┘ │ active?) │
▲ └─────────┬──────────┘
│ │ route()
│ next prompt injected ▼
│ ┌────────────────────┐
│ │ cavekit-tools.cjs │
│ │ routeDecision() │
│ │ + status-block │
│ │ + backprop- │
│ │ directive │
│ └─────────┬──────────┘
│ │
└───────── {decision:"block", │
reason:<next prompt>} ◀─┘All under <project>/.cavekit/:
| File | Writer | Purpose | |---------------------------------|-----------------------|----------------------------------------------| | .loop.json | setup-loop | Sentinel; stop-hook no-ops without it. | | .loop.lock | stop-hook (heartbeat) | Single-writer lock. PID + hostname + ts. | | state.md | cavekit-tools | phase, current_task, iteration. | | token-ledger.json | token-monitor hook | Session + per-task token tallies. | | task-status.json | commands | Authoritative task registry. | | .progress.json | progress-tracker | Zero-context UI snapshot. | | .auto-backprop-pending.json | auto-backprop hook | Flag file from failed tests. | | history/backprop-log.md | backprop skill | Append-only trace log. | | capabilities.json | discover command | MCP + CLI tool detection. |
/ck:make calls: node "${CLAUDE_PLUGIN_ROOT}/scripts/cavekit-tools.cjs" setup-loop This writes .loop.json (activating the stop hook) and resets state.md.
stop-hook.sh. The hook:session_id and transcript_path<promise>CAVEKIT COMPLETE</promise>routeDecision() for the next prompt{"decision":"block","reason":<next prompt>}decision:block + reason:... as a newuser message, so the session continues.
CAVEKIT_MAX_ITERATIONS)CAVEKIT_BUDGET_EXHAUSTED)CAVEKIT_LOCK_CONFLICT)To end the loop cleanly, emit exactly:
<promise>CAVEKIT COMPLETE</promise>The hook searches for this literal in the last 20 transcript lines. Put it on its own line at the very end of the final message. Do not wrap it in code fences or paraphrase it — the search is a literal substring match.
When routeDecision() cannot safely continue, it returns one of these strings instead of a prompt:
| Sentinel | Meaning | |------------------------------|---------------------------------------| | CAVEKIT_LOOP_DONE | All tasks complete. Hook exits silent.| | CAVEKIT_MAX_ITERATIONS | Iteration cap hit. Loop halted. | | CAVEKIT_BUDGET_EXHAUSTED | Session budget exhausted. | | CAVEKIT_LOCK_CONFLICT | Another session owns the lock. |
The hook translates each into a short user-facing message before returning.
The lock is a JSON file (.loop.lock) with {owner, pid, host, heartbeat_at}.
session:<session_id>.heartbeat_at.lock.
CAVEKIT_LOCK_CONFLICT.Never delete .loop.lock while a session might be active. Use cavekit-tools release-lock --owner <tag> instead.
bash# Inspect current state node "${CLAUDE_PLUGIN_ROOT}/scripts/cavekit-tools.cjs" status # Who holds the lock? cat .cavekit/.loop.lock # What would the router do right now? node "${CLAUDE_PLUGIN_ROOT}/scripts/cavekit-tools.cjs" route # Drop the loop entirely (safe after a crash) node "${CLAUDE_PLUGIN_ROOT}/scripts/cavekit-tools.cjs" teardown-loop
Turn on debug logging by exporting CAVEKIT_DEBUG=1. The hook will write to .cavekit/.debug.log.
state.md's phase field while a loop is active. Thehook assumes single-writer semantics and will overwrite you.
race, and Claude Code picks one arbitrarily.
truly complete. The hook trusts the sentinel and tears down immediately.
Other measured skills in the registry, with their headline benchmark lift.