Skip to content

Latest commit

 

History

History
153 lines (119 loc) · 9.75 KB

captain-without-capy.md

File metadata and controls

153 lines (119 loc) · 9.75 KB

Running parallel agent workers locally without Capy

crew/bin/gstack-crew lets one local controller run multiple coding-agent workers in parallel on a single machine using plain Claude Code (claude -p) or Codex CLI (codex exec) inside isolated git worktrees. You do not need Capy, cloud threads, or external orchestration services.

This is Phase 1 (local-only). Cloud worker backends such as Claude Code on the web and Codex cloud are intentionally out of scope until their task APIs stabilize; passing a cloud harness name is refused explicitly.

Prerequisites

  • Git with worktree support (Git 2.20+).
  • Bun >= 1.4.2 on PATH (runs crew/bin/gstack-crew and bin/gstack-ship-queue).
  • At least one authenticated local coding CLI:
    • Claude Code (claude on PATH, or set GSTACK_CLAUDE_BIN), or
    • Codex CLI (codex on PATH, or set GSTACK_CODEX_BIN).
  • GitHub CLI (gh) authenticated for the target repository if you hand finished branches off to gstack-ship-queue.

Run gstack-crew directly from your gstack checkout (for example ~/.claude/skills/gstack/crew/bin/gstack-crew) or add crew/bin to your PATH.

Safety bounds

Unattended local workers operate on your own machine, so gstack-crew enforces four hard limits on every worker:

  1. Scoped permissions — explicit worktree allowlist.
    • Claude Code (claude -p): runs with --permission-mode dontAsk, --setting-sources "" (isolating user/project settings), explicit --tools Read,Edit,Write,Glob,Grep,Bash, --strict-mcp-config, hooks disabled, and a per-worktree --allowedTools allowlist (Read, Edit, Write, Glob, Grep restricted strictly to the worker's worktree path plus a small allowlist of local git and test commands). --disallowedTools explicitly blocks mcp__*, WebFetch, WebSearch, git merge, gh pr merge, force-push, git -c/--output/--no-index/commit -F, sudo, and network fetch commands. Passing --allow-bash <prefix> adds one safe command prefix (for example vitest); unrestricted shells, bare roots, path-qualified binaries, and merge/push/flag bypasses are refused. Note: allowlisted test runners (bun test, npm test, bun run, npm run) execute repo test scripts under your local user permissions unless you wrap the run in an OS sandbox.
    • Codex CLI (codex exec): always runs with --sandbox workspace-write, -C <worktree>, approval_policy="never", sandbox_workspace_write.network_access=false, and mcp_servers={} (clearing inherited ~/.codex/config.toml MCP servers so external tools cannot bypass the sandbox). Full-access and bypass flags are never emitted. Note: Codex's workspace-write sandbox keeps linked worktree .git metadata outside -C <worktree> read-only, so if a Codex worker leaves clean verified edits uncommitted, commit them in its worktree before running ready or land.
  2. Per-worker spend cap and wall-clock timeout.
    • Every worker requires positive --spend-cap <usd> (default $10) and --timeout <seconds> (default 1800s).
    • For Claude Code, --max-budget-usd is passed to the CLI and the controller parses streaming JSONL output in real time, terminating the process group as soon as accumulated spend reaches --spend-cap.
    • For Codex CLI, codex exec --json reports token usage on turn.completed (at the end of each turn), so --timeout is the live mid-turn bound and --spend-cap is checked immediately on turn.completed (marking an over-cap run stopped with stop_reason=spend_cap and blocking further resume until --spend-cap is raised). When --model is omitted or absent from lib/pricing.ts, Codex token usage is priced conservatively at gpt-6.2-astra rates ($8/$40 per MTok) so caps do not undercount pricier models. Keep --timeout tight on unattended Codex workers.
    • Whenever elapsed time reaches --timeout or live spend reaches --spend-cap, the controller terminates the worker's entire process group (SIGTERM, grace period, then SIGKILL) and records the exact stop reason (spend_cap or timeout) in the state file.
  3. Merges only through gstack-ship-queue.
    • Workers commit to their own worktree branch and never merge.
    • gstack-crew land validates that the worker is stopped and its worktree is clean, then delegates strictly to bin/gstack-ship-queue land --pr <n> --pin <sha>. gstack-crew never runs raw git merge or gh pr merge.
  4. Durable state and crash recovery.
    • State lives under gstack's state root ($GSTACK_STATE_ROOT/crew-local/<repo>-<hash>/state.json, mode 0600, written atomically under a file lock; override with --state-dir), alongside a shared spend file (spend.json), per-run stdout JSONL logs (logs/<id>-<run_id>.jsonl), and separate stderr logs (logs/<id>-<run_id>.stderr.log).
    • gstack-crew status checks OS process liveness for every running worker. If a worker was killed or crashed (OOM, SIGKILL, laptop sleep/reboot), status transitions it cleanly to crashed, recovers spend and session ID from its JSONL log, settles the open spend reservation, and leaves its worktree and branch intact.
    • gstack-crew resume --lane <id> picks up in that exact worktree and branch with the remaining budget (and passes --resume <session_id> for Claude Code unless --fresh-session is set).

Quick start

1. Start one local worker in its own worktree

crew/bin/gstack-crew start \
  --lane auth-retry \
  --harness claude \
  --planned "src/auth/**" --planned "test/auth.test.ts" \
  --spend-cap 5 \
  --timeout 900 \
  --prompt "Add exponential backoff to token refresh and cover it with unit tests."

To launch in the background so your shell returns immediately, add --detach:

crew/bin/gstack-crew start \
  --lane docs-api \
  --harness codex \
  --planned "docs/api.md" \
  --spend-cap 3 \
  --timeout 600 \
  --detach \
  --prompt "Document the v2 pagination query parameters in docs/api.md."

2. Run multiple disjoint workers in parallel from a manifest

Create workers.json:

{
  "lanes": [
    {
      "lane": "auth-retry",
      "harness": "claude",
      "planned": ["src/auth/**", "test/auth.test.ts"],
      "spend_cap": 5,
      "timeout": 900,
      "prompt": "Add exponential backoff to token refresh and add tests."
    },
    {
      "lane": "docs-api",
      "harness": "codex",
      "planned": ["docs/api.md"],
      "spend_cap": 3,
      "timeout": 600,
      "prompt": "Document the v2 pagination query parameters in docs/api.md."
    }
  ]
}

Run them together:

crew/bin/gstack-crew run --manifest workers.json --concurrency 2

Before creating any worktree, gstack-crew checks every worker's planned paths against sibling workers and existing branch diffs (using crew/lib/lane-ownership.ts) and refuses to start if two workers claim overlapping files.

3. Check status and recover a crashed or stopped worker

crew/bin/gstack-crew status
crew/bin/gstack-crew status --json

If a worker was killed (status=crashed) or hit a cap (status=stopped, stop_reason=spend_cap or stop_reason=timeout), resume it in the same worktree and branch:

# Recover a crashed worker with its remaining budget
crew/bin/gstack-crew resume --lane auth-retry

# Raise the cap to continue a worker that stopped at its spend limit
crew/bin/gstack-crew resume --lane auth-retry --spend-cap 10 --timeout 1200

4. Stop, hand off to gstack-ship-queue, and clean up

# Stop a running worker cleanly (keeps its worktree and branch)
crew/bin/gstack-crew stop --lane auth-retry

# Once the worker's branch has a PR with /gstack-ship verification:
crew/bin/gstack-crew ready --lane auth-retry --pr 42 --no-screen

# Land exclusively through gstack-ship-queue (dry-run first to inspect)
crew/bin/gstack-crew land --lane auth-retry --pr 42 --dry-run
crew/bin/gstack-crew land --lane auth-retry --pr 42

# Remove finished worktrees
crew/bin/gstack-crew clean --finished

Command reference

Command Purpose
start Create one git worktree and branch, persist state, and run a local claude or codex worker (foreground or --detach).
run Validate pairwise file ownership across a JSON manifest and run workers in parallel up to --concurrency.
status Reconcile process liveness, recover crashed workers, and show worktree SHA, dirty state, spend vs cap, and stop reason.
resume Restart a crashed, stopped, failed, or completed (non-landed) worker in its existing worktree and branch (reattaching the worktree from the branch if needed).
stop Send SIGTERM then SIGKILL to one worker's process group (or --all), settle the current attempt's spend, and preserve its worktree and branch.
ready Verify the worker is stopped and clean, run gstack-ship-queue ready from its worktree, and record the PR number and HEAD SHA on success.
land Verify the worker is stopped and clean, then run gstack-ship-queue land --pr <n> --pin <sha>.
clean Remove worktrees for --lane <id> or --finished (completed and landed only, preserving crashed/stopped/failed lanes); refuses live workers and dirty worktrees unless --force is passed (optional --delete-branch).

Common flags supported on every command: --repo <path>, --state-dir <dir>, and --json.

Phase 1 limits

  • Local machine only: every worker runs as a local process in a local git worktree. Cloud execution backends are planned for Phase 2.
  • No automatic PR creation: workers commit to their branch; open the PR and run /gstack-ship before calling gstack-crew ready and gstack-crew land.
  • Token-based spend estimation for Codex CLI: when codex exec --json emits token counts without a direct dollar figure, spend is estimated from gstack's model price table (lib/pricing.ts).