Loops and Graphs for Cursor

By | November 27, 2026

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

  1. A shell command decides success. The exit code is the check; the model never grades its own work.
  2. Tasks run concurrently. Two tasks editing the same tree can collide, so give them separate working directories.
  3. The default run never executes final_cmd. Running with --approve re-runs every check with no agent, and only then executes final_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:

  1. Run check_cmd. Exit 0 means green and the task stops immediately (0 iterations).
  2. Otherwise, send prompt to the selected agent.
  3. Run check_cmd again.
  4. 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 runner support (claude or cursor) and CURSOR_AGENT_BIN (defaulting to agent).
  • Added parse_agent_json() to normalize session IDs, answers, and costs across both CLIs.
  • Updated ask() to accept runner and construct the Cursor CLI argument list (--force --sandbox disabled --workspace).
  • Set Cursor’s default model to composer-2.5 while keeping Claude’s default at opus.
  • Added runner, model, and per-iteration session ID logging.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.