In Loops and Graphs, part 2, I introduced loop_graph.py to run parallel Claude Code workers, each looped on a deterministic shell check, behind a single human approval gate.
Now Cursor uses that exact same script. A runner field selects the agent (claude or cursor), while the deterministic check, the parallel graph, and the final approval gate stay identical.
The Three Rules
- A shell command decides success. The exit code is the check; the model never grades its own work.
- Tasks run concurrently. Two tasks editing the same tree can collide, so give them separate working directories.
- The default run never executes
final_cmd. Running with--approvere-runs every check with no agent, and only then executesfinal_cmd.
Note: A check that is already green before any work starts does not launch an agent, and the log flags that the check may be meaningless.
How to Run It
python3 ~/scripts/loop_graph.py spec.json
python3 ~/scripts/loop_graph.py spec.json --approve
Logs are written to ~/.cache/loop-graph/<timestamp>-<pid>/<task>.log.
Example Cursor spec:
{
"runner": "cursor",
"model": "composer-2.5",
"max_iters": 3,
"tasks": [
{
"name": "api",
"prompt": "Fix X in api/routes.py",
"check_cmd": "pytest -q tests/test_routes.py",
"cwd": "/path/to/repo"
}
],
"final_cmd": "git push",
"final_cwd": "/path/to/repo"
}
runner defaults to claude, so existing specs continue to call claude -p without modification.
Claude vs. Cursor Runner
| Setting | Claude | Cursor |
|---|---|---|
| Binary | claude (or CLAUDE_BIN) |
agent (or CURSOR_AGENT_BIN) |
| One-shot CLI | claude -p --output-format json --model <model> --permission-mode <mode> |
agent -p --output-format json --model <model> --force --sandbox disabled --workspace <cwd> |
| Same session | --resume <session_id> |
--resume <session_id> |
| Prompt | stdin |
Last positional argument |
| Default model | opus |
composer-2.5 |
Both runners parse a single JSON object from stdout, extracting the session ID (session_id, sessionId, chat_id, or chatId), the answer (result or text), and the cost (total_cost_usd or total_cost).
The Loop & The Gate
For each task:
- Run
check_cmd. Exit0means green and the task stops immediately (0 iterations). - Otherwise, send
promptto the selected agent. - Run
check_cmdagain. - On failure, feed the last 4,000 characters of stdout/stderr back into the same session and repeat up to
max_iters(default: 3).
When every task is green and final_cmd is set, the script prints the command and exits 0 without running it. Passing --approve re-runs every check_cmd (with no agent) and executes final_cmd only if all checks still pass.
What Changed in loop_graph.py
- Added
runnersupport (claudeorcursor) andCURSOR_AGENT_BIN(defaulting toagent). - Added
parse_agent_json()to normalize session IDs, answers, and costs across both CLIs. - Updated
ask()to acceptrunnerand construct the Cursor CLI argument list (--force --sandbox disabled --workspace). - Set Cursor’s default model to
composer-2.5while keeping Claude’s default atopus. - Added runner, model, and per-iteration session ID logging.