diff --git a/.claude/skills/consult/SKILL.md b/.claude/skills/consult/SKILL.md index 71b068335..a5e82dae2 100644 --- a/.claude/skills/consult/SKILL.md +++ b/.claude/skills/consult/SKILL.md @@ -28,7 +28,7 @@ The `-m` / `--model` flag is **always required** except for `consult stats`. `consult` loads `/.env` and sets every key the shell does not already define, **credentials included**. A key exported in the shell wins over `.env`. -- claude: the lane runs your installed `claude` on its own login, which for most people is the subscription. `ANTHROPIC_API_KEY` and `ANTHROPIC_AUTH_TOKEN` are withheld from it, from the shell or `.env`, unless config sets `"consult": { "claudeAuth": "api-key" }` (CI and key-only machines). `CLAUDE_CODE_OAUTH_TOKEN` is a login, so it passes through and beats the keychain login. A missing `claude`, one older than 2.1.141, or a logged-out one fails the lane with the command to run. +- claude: the lane runs your installed `claude` on its own login, which for most people is the subscription. `ANTHROPIC_API_KEY` and `ANTHROPIC_AUTH_TOKEN` are withheld from it, from the shell or `.env`, unless config sets `"consult": { "claudeAuth": "api-key" }` (CI and key-only machines). That is a billing guarantee, not secrecy: the reviewer can still read the workspace `.env` with its Read tool. `CLAUDE_CODE_OAUTH_TOKEN` is a login, so it passes through and beats the keychain login. A missing `claude`, one older than 2.1.141, or a logged-out one fails the lane with the command to run. - codex: `CODEX_API_KEY` beats the ChatGPT login. `OPENAI_API_KEY` is ignored. The claude and codex lanes print what they authenticate with, e.g. `[CLAUDE] auth: claude.ai (max) — stored claude login; ignoring ANTHROPIC_API_KEY (from .env) — set consult.claudeAuth to "api-key" to bill the API instead`. If a review fails with a usage-limit or auth error while your own login works, check that line first. diff --git a/.codex/skills/consult/SKILL.md b/.codex/skills/consult/SKILL.md index 71b068335..a5e82dae2 100644 --- a/.codex/skills/consult/SKILL.md +++ b/.codex/skills/consult/SKILL.md @@ -28,7 +28,7 @@ The `-m` / `--model` flag is **always required** except for `consult stats`. `consult` loads `/.env` and sets every key the shell does not already define, **credentials included**. A key exported in the shell wins over `.env`. -- claude: the lane runs your installed `claude` on its own login, which for most people is the subscription. `ANTHROPIC_API_KEY` and `ANTHROPIC_AUTH_TOKEN` are withheld from it, from the shell or `.env`, unless config sets `"consult": { "claudeAuth": "api-key" }` (CI and key-only machines). `CLAUDE_CODE_OAUTH_TOKEN` is a login, so it passes through and beats the keychain login. A missing `claude`, one older than 2.1.141, or a logged-out one fails the lane with the command to run. +- claude: the lane runs your installed `claude` on its own login, which for most people is the subscription. `ANTHROPIC_API_KEY` and `ANTHROPIC_AUTH_TOKEN` are withheld from it, from the shell or `.env`, unless config sets `"consult": { "claudeAuth": "api-key" }` (CI and key-only machines). That is a billing guarantee, not secrecy: the reviewer can still read the workspace `.env` with its Read tool. `CLAUDE_CODE_OAUTH_TOKEN` is a login, so it passes through and beats the keychain login. A missing `claude`, one older than 2.1.141, or a logged-out one fails the lane with the command to run. - codex: `CODEX_API_KEY` beats the ChatGPT login. `OPENAI_API_KEY` is ignored. The claude and codex lanes print what they authenticate with, e.g. `[CLAUDE] auth: claude.ai (max) — stored claude login; ignoring ANTHROPIC_API_KEY (from .env) — set consult.claudeAuth to "api-key" to bill the API instead`. If a review fails with a usage-limit or auth error while your own login works, check that line first. diff --git a/INSTALL.md b/INSTALL.md index bc0491f5f..6d7a3a7e0 100644 --- a/INSTALL.md +++ b/INSTALL.md @@ -88,7 +88,7 @@ When a user requests Codev installation, check if the consultation tools are ava command -v consult >/dev/null && echo "✓ consult CLI available" || echo "✗ consult CLI not found" ``` -**Note**: The `consult` CLI is installed with `npm install -g @cluesmith/codev`. Multi-agent consultation requires at least one of: `claude`, `gemini-cli`, or `codex`. +**Note**: The `consult` CLI is installed with `npm install -g @cluesmith/codev`. Multi-agent consultation requires at least one of: `claude`, `gemini-cli`, or `codex`. The `claude` lane runs your installed `claude` CLI, which must be version 2.1.141 or newer (`claude --version`). ### Step 2: Create and Populate the Codev Directory diff --git a/README.md b/README.md index 2755b1e19..09f9916fd 100644 --- a/README.md +++ b/README.md @@ -123,7 +123,7 @@ On Linux and WSL, also install `build-essential` and `python3` (node-pty compile **Windows:** two paths. **Native** (preview): Node.js 22 or 24 (x64 or ARM64), [Git for Windows](https://git-scm.com/download/win) and `jq`, no admin rights or Developer Mode needed. **WSL 2**: follow the Linux instructions inside your distribution. See [DEPENDENCIES.md](codev-skeleton/DEPENDENCIES.md#windows) for setup and limitations, including why native support is a preview (#1911). **AI CLIs** (install all three for multi-model consultation): -- Claude Code: `npm install -g @anthropic-ai/claude-code` +- Claude Code: `curl -fsSL https://claude.ai/install.sh | bash`, or `npm install -g @anthropic-ai/claude-code`. The `claude` consult lane runs this CLI and needs version 2.1.141 or newer. - Antigravity CLI (`agy`, the `gemini` consult lane): `curl -fsSL https://antigravity.google/cli/install.sh | bash`, then run `agy` once to sign in (OAuth — replaces the retired Gemini CLI) - Codex CLI: `npm install -g @openai/codex` diff --git a/codev-skeleton/.claude/skills/consult/SKILL.md b/codev-skeleton/.claude/skills/consult/SKILL.md index 71b068335..a5e82dae2 100644 --- a/codev-skeleton/.claude/skills/consult/SKILL.md +++ b/codev-skeleton/.claude/skills/consult/SKILL.md @@ -28,7 +28,7 @@ The `-m` / `--model` flag is **always required** except for `consult stats`. `consult` loads `/.env` and sets every key the shell does not already define, **credentials included**. A key exported in the shell wins over `.env`. -- claude: the lane runs your installed `claude` on its own login, which for most people is the subscription. `ANTHROPIC_API_KEY` and `ANTHROPIC_AUTH_TOKEN` are withheld from it, from the shell or `.env`, unless config sets `"consult": { "claudeAuth": "api-key" }` (CI and key-only machines). `CLAUDE_CODE_OAUTH_TOKEN` is a login, so it passes through and beats the keychain login. A missing `claude`, one older than 2.1.141, or a logged-out one fails the lane with the command to run. +- claude: the lane runs your installed `claude` on its own login, which for most people is the subscription. `ANTHROPIC_API_KEY` and `ANTHROPIC_AUTH_TOKEN` are withheld from it, from the shell or `.env`, unless config sets `"consult": { "claudeAuth": "api-key" }` (CI and key-only machines). That is a billing guarantee, not secrecy: the reviewer can still read the workspace `.env` with its Read tool. `CLAUDE_CODE_OAUTH_TOKEN` is a login, so it passes through and beats the keychain login. A missing `claude`, one older than 2.1.141, or a logged-out one fails the lane with the command to run. - codex: `CODEX_API_KEY` beats the ChatGPT login. `OPENAI_API_KEY` is ignored. The claude and codex lanes print what they authenticate with, e.g. `[CLAUDE] auth: claude.ai (max) — stored claude login; ignoring ANTHROPIC_API_KEY (from .env) — set consult.claudeAuth to "api-key" to bill the API instead`. If a review fails with a usage-limit or auth error while your own login works, check that line first. diff --git a/codev-skeleton/.codex/skills/consult/SKILL.md b/codev-skeleton/.codex/skills/consult/SKILL.md index 71b068335..a5e82dae2 100644 --- a/codev-skeleton/.codex/skills/consult/SKILL.md +++ b/codev-skeleton/.codex/skills/consult/SKILL.md @@ -28,7 +28,7 @@ The `-m` / `--model` flag is **always required** except for `consult stats`. `consult` loads `/.env` and sets every key the shell does not already define, **credentials included**. A key exported in the shell wins over `.env`. -- claude: the lane runs your installed `claude` on its own login, which for most people is the subscription. `ANTHROPIC_API_KEY` and `ANTHROPIC_AUTH_TOKEN` are withheld from it, from the shell or `.env`, unless config sets `"consult": { "claudeAuth": "api-key" }` (CI and key-only machines). `CLAUDE_CODE_OAUTH_TOKEN` is a login, so it passes through and beats the keychain login. A missing `claude`, one older than 2.1.141, or a logged-out one fails the lane with the command to run. +- claude: the lane runs your installed `claude` on its own login, which for most people is the subscription. `ANTHROPIC_API_KEY` and `ANTHROPIC_AUTH_TOKEN` are withheld from it, from the shell or `.env`, unless config sets `"consult": { "claudeAuth": "api-key" }` (CI and key-only machines). That is a billing guarantee, not secrecy: the reviewer can still read the workspace `.env` with its Read tool. `CLAUDE_CODE_OAUTH_TOKEN` is a login, so it passes through and beats the keychain login. A missing `claude`, one older than 2.1.141, or a logged-out one fails the lane with the command to run. - codex: `CODEX_API_KEY` beats the ChatGPT login. `OPENAI_API_KEY` is ignored. The claude and codex lanes print what they authenticate with, e.g. `[CLAUDE] auth: claude.ai (max) — stored claude login; ignoring ANTHROPIC_API_KEY (from .env) — set consult.claudeAuth to "api-key" to bill the API instead`. If a review fails with a usage-limit or auth error while your own login works, check that line first. diff --git a/codev-skeleton/resources/commands/consult.md b/codev-skeleton/resources/commands/consult.md index f3622998f..66e073c59 100644 --- a/codev-skeleton/resources/commands/consult.md +++ b/codev-skeleton/resources/commands/consult.md @@ -147,6 +147,21 @@ picks what the lane bills: - What passes through in `login` mode, what the auth line says, and how the lane fails are in [Claude auth: subscription vs. metered API](#claude-auth-subscription-vs-metered-api). +### `consult.claudeMaxTurns` and `consult.claudeMaxBudgetUsd` + +The claude lane caps each review at 200 turns (`--max-turns`) and $25 (`--max-budget-usd`). On a +subscription login the dollars are notional: the CLI still counts them, so a large review can +reach the budget cap without any real spend. Raise either cap here: + +```json +{ "consult": { "claudeMaxTurns": 400, "claudeMaxBudgetUsd": 100 } } +``` + +- `claudeMaxTurns` must be a positive whole number; `claudeMaxBudgetUsd` a positive number (it may + be fractional). Any other value is an error at config load. +- A review that reaches a cap fails, and the message names the cap and the key to raise: + `The review reached the $25 budget cap. To allow more, raise consult.claudeMaxBudgetUsd in .codev/config.json.` + ### `consult.pricing.codex` Per-1M-token rates for the codex lane. Set this when Codev has no rates for the model you run @@ -250,6 +265,7 @@ offending key and the valid alternatives. Nothing falls back to a default on err | Model ids | **The provider** | When the lane runs | | `codexPath` | **Codev**: absolute-path syntax, then ` --version` | Syntax at config load; a binary that doesn't run, when the codex lane starts | | `claudeAuth` | **Codev**, against a closed enum (`login`, `api-key`) | Config load, before anything runs | +| `claudeMaxTurns`, `claudeMaxBudgetUsd` | **Codev**: a positive whole number, a positive number | Config load, before anything runs | Codev checks a model id's *syntax* only (ASCII alphanumerics plus `. _ : / @ + -`, 1–200 characters, no leading punctuation) — never its existence. **There is no allowlist of model ids anywhere in @@ -483,6 +499,9 @@ your Claude subscription, for most people. That is `consult.claudeAuth: "login"` `claude` child in `login` mode, whether they come from the shell or from `/.env`. Before #1912, an `ANTHROPIC_API_KEY` in `.env` (there for app code, say) billed review traffic to the metered API unless an OAuth token was also set (#985 covered only that case). + **This is a billing guarantee, not secrecy.** The reviewer runs in the workspace with a Read + tool, so it can still read the workspace `.env`, key included, as the Agent SDK lane before it + could. - **What passes through:** `CLAUDE_CODE_OAUTH_TOKEN` (a `claude setup-token` subscription token, which is a login rather than API billing), and explicit provider and routing configuration (`CLAUDE_CODE_USE_BEDROCK`, `CLAUDE_CODE_USE_VERTEX`, …, `ANTHROPIC_BASE_URL`, proxies). @@ -523,33 +542,35 @@ That one config line is the fix. | Condition | What to do | |---|---| -| `claude` is not on `PATH` | Install it (`curl -fsSL https://claude.ai/install.sh \| bash`), or set `CODEV_CLAUDE_BIN` | +| `claude` is not on `PATH` | Install it (`curl -fsSL https://claude.ai/install.sh \| bash`, or `npm install -g @anthropic-ai/claude-code` where the native installer cannot run), or set `CODEV_CLAUDE_BIN` | | `CODEV_CLAUDE_BIN` is not an executable file | Fix the path. Codev does not fall back to `PATH` | | Older than 2.1.141, or a version it cannot parse | `claude update`, or `npm install -g @anthropic-ai/claude-code@latest` for an npm install | -| `claude auth status` reports no login | `claude auth login` | +| `claude auth status` reports no login | `claude auth login`; in `api-key` mode, check the key, or remove `consult.claudeAuth` | | `api-key` mode with neither key set | Set one, or remove `consult.claudeAuth` | It never falls back to another credential or to another `claude`. A run that fails before the auth status is known prints no auth line; its message is the explanation. Once the review runs, an error result (a usage limit, an invalid or expired credential) fails the -run with no output file, and the message names the credential in use. A reviewer that fills its +run with no output file, and the message names the credential in use. When the result says the +session is not logged in, the message adds the remedy for the auth mode, as the preflight does. A reviewer that fills its context window ("Prompt is too long") fails with a message naming the model and #1641; consult does not retry. A successful result with no review text fails too, since it has no VERDICT. Every failure after the preflight also names the binary that ran, so an old or unexpected `claude` is visible: ``` -Claude CLI error (error_max_budget_usd). The review reached the $25 budget cap. +Claude CLI error (error_max_budget_usd). The review reached the $25 budget cap. To allow more, raise consult.claudeMaxBudgetUsd in .codev/config.json. The claude lane ran /Users/you/.local/bin/claude, 2.1.326 (from PATH). ``` **Read-only, isolated, and safe to nest.** The reviewer session has only Read, Glob and Grep: no edit, shell, web or MCP tools. Consult checks the tool list the session reports when it starts, -and stops the review unless it is exactly those three. If a `claude` release changes the session's -tools, every review stops; pin a known-good `claude` with `CODEV_CLAUDE_BIN` until Codev catches up. -It has a 200-turn cap and a $25 budget cap. It loads none of your +and stops the review unless it is exactly those three (a tool listed twice is fine). If a `claude` +release changes the session's tools, every review stops; pin a known-good `claude` with +`CODEV_CLAUDE_BIN` until Codev catches up. It has a 200-turn cap and a $25 budget cap, both +configurable ([`consult.claudeMaxTurns` and `consult.claudeMaxBudgetUsd`](#consultclaudemaxturns-and-consultclaudemaxbudgetusd)). It loads none of your settings files, hooks, plugins, MCP servers or CLAUDE.md, and its sessions are not saved, so reviews do not fill `claude --resume`. Claude Code's session markers (`CLAUDECODE` and related variables) are removed from the child's environment, so consult works from inside a Claude Code diff --git a/codev/projects/bugfix-1941-consult-claude-lane-five-follo/status.yaml b/codev/projects/bugfix-1941-consult-claude-lane-five-follo/status.yaml new file mode 100644 index 000000000..c6b386f78 --- /dev/null +++ b/codev/projects/bugfix-1941-consult-claude-lane-five-follo/status.yaml @@ -0,0 +1,19 @@ +id: bugfix-1941 +title: consult-claude-lane-five-follo +protocol: bugfix +phase: fix +plan_phases: [] +current_plan_phase: null +gates: + pr: + status: pending +iteration: 1 +build_complete: false +history: [] +started_at: '2026-11-21T01:36:42.606Z' +updated_at: '2026-11-21T02:19:57.084Z' +pr_history: + - phase: fix + pr_number: 1942 + branch: builder/bugfix-1941 + created_at: '2026-11-21T02:19:57.083Z' diff --git a/codev/resources/arch.md b/codev/resources/arch.md index 0014c128b..adb687cae 100644 --- a/codev/resources/arch.md +++ b/codev/resources/arch.md @@ -2122,7 +2122,7 @@ consult -m claude spec 42 |-------|-----------|----------------|-------------| | gemini | `agy` (Antigravity CLI; resolved real bin, not the IDE symlink) | Folded into the prompt (role + query) | OAuth / subscription (no API key) | | codex | `codex` | Temp file via `-c experimental_instructions_file=` flag | `OPENAI_API_KEY` | -| claude | the installed `claude` CLI (`CODEV_CLAUDE_BIN` pin, else `PATH`; floor 2.1.141, Spec 1912) | Role + claude-lane capability note via `--system-prompt-file` | The CLI's own login (subscription for most). `ANTHROPIC_API_KEY`/`ANTHROPIC_AUTH_TOKEN` are withheld unless `consult.claudeAuth: "api-key"` | +| claude | the installed `claude` CLI (`CODEV_CLAUDE_BIN` pin, else `PATH`; floor 2.1.141, Spec 1912) | Role + claude-lane capability note via `--system-prompt-file` | The CLI's own login (subscription for most). `ANTHROPIC_API_KEY`/`ANTHROPIC_AUTH_TOKEN` are withheld unless `consult.claudeAuth: "api-key"`: a billing guarantee, not secrecy (the reviewer can still Read the workspace `.env`). Caps: `consult.claudeMaxTurns` (200) and `consult.claudeMaxBudgetUsd` ($25) | **Query building**: Five subcommands (`pr`, `spec`, `plan`, `impl`, `general`) each build a prompt that includes the spec/plan/diff content plus a verdict template (`VERDICT: [APPROVE | REQUEST_CHANGES | COMMENT]`). PR diffs truncated to 50k chars, impl diffs to 80k chars. diff --git a/codev/resources/commands/consult.md b/codev/resources/commands/consult.md index f3622998f..66e073c59 100644 --- a/codev/resources/commands/consult.md +++ b/codev/resources/commands/consult.md @@ -147,6 +147,21 @@ picks what the lane bills: - What passes through in `login` mode, what the auth line says, and how the lane fails are in [Claude auth: subscription vs. metered API](#claude-auth-subscription-vs-metered-api). +### `consult.claudeMaxTurns` and `consult.claudeMaxBudgetUsd` + +The claude lane caps each review at 200 turns (`--max-turns`) and $25 (`--max-budget-usd`). On a +subscription login the dollars are notional: the CLI still counts them, so a large review can +reach the budget cap without any real spend. Raise either cap here: + +```json +{ "consult": { "claudeMaxTurns": 400, "claudeMaxBudgetUsd": 100 } } +``` + +- `claudeMaxTurns` must be a positive whole number; `claudeMaxBudgetUsd` a positive number (it may + be fractional). Any other value is an error at config load. +- A review that reaches a cap fails, and the message names the cap and the key to raise: + `The review reached the $25 budget cap. To allow more, raise consult.claudeMaxBudgetUsd in .codev/config.json.` + ### `consult.pricing.codex` Per-1M-token rates for the codex lane. Set this when Codev has no rates for the model you run @@ -250,6 +265,7 @@ offending key and the valid alternatives. Nothing falls back to a default on err | Model ids | **The provider** | When the lane runs | | `codexPath` | **Codev**: absolute-path syntax, then ` --version` | Syntax at config load; a binary that doesn't run, when the codex lane starts | | `claudeAuth` | **Codev**, against a closed enum (`login`, `api-key`) | Config load, before anything runs | +| `claudeMaxTurns`, `claudeMaxBudgetUsd` | **Codev**: a positive whole number, a positive number | Config load, before anything runs | Codev checks a model id's *syntax* only (ASCII alphanumerics plus `. _ : / @ + -`, 1–200 characters, no leading punctuation) — never its existence. **There is no allowlist of model ids anywhere in @@ -483,6 +499,9 @@ your Claude subscription, for most people. That is `consult.claudeAuth: "login"` `claude` child in `login` mode, whether they come from the shell or from `/.env`. Before #1912, an `ANTHROPIC_API_KEY` in `.env` (there for app code, say) billed review traffic to the metered API unless an OAuth token was also set (#985 covered only that case). + **This is a billing guarantee, not secrecy.** The reviewer runs in the workspace with a Read + tool, so it can still read the workspace `.env`, key included, as the Agent SDK lane before it + could. - **What passes through:** `CLAUDE_CODE_OAUTH_TOKEN` (a `claude setup-token` subscription token, which is a login rather than API billing), and explicit provider and routing configuration (`CLAUDE_CODE_USE_BEDROCK`, `CLAUDE_CODE_USE_VERTEX`, …, `ANTHROPIC_BASE_URL`, proxies). @@ -523,33 +542,35 @@ That one config line is the fix. | Condition | What to do | |---|---| -| `claude` is not on `PATH` | Install it (`curl -fsSL https://claude.ai/install.sh \| bash`), or set `CODEV_CLAUDE_BIN` | +| `claude` is not on `PATH` | Install it (`curl -fsSL https://claude.ai/install.sh \| bash`, or `npm install -g @anthropic-ai/claude-code` where the native installer cannot run), or set `CODEV_CLAUDE_BIN` | | `CODEV_CLAUDE_BIN` is not an executable file | Fix the path. Codev does not fall back to `PATH` | | Older than 2.1.141, or a version it cannot parse | `claude update`, or `npm install -g @anthropic-ai/claude-code@latest` for an npm install | -| `claude auth status` reports no login | `claude auth login` | +| `claude auth status` reports no login | `claude auth login`; in `api-key` mode, check the key, or remove `consult.claudeAuth` | | `api-key` mode with neither key set | Set one, or remove `consult.claudeAuth` | It never falls back to another credential or to another `claude`. A run that fails before the auth status is known prints no auth line; its message is the explanation. Once the review runs, an error result (a usage limit, an invalid or expired credential) fails the -run with no output file, and the message names the credential in use. A reviewer that fills its +run with no output file, and the message names the credential in use. When the result says the +session is not logged in, the message adds the remedy for the auth mode, as the preflight does. A reviewer that fills its context window ("Prompt is too long") fails with a message naming the model and #1641; consult does not retry. A successful result with no review text fails too, since it has no VERDICT. Every failure after the preflight also names the binary that ran, so an old or unexpected `claude` is visible: ``` -Claude CLI error (error_max_budget_usd). The review reached the $25 budget cap. +Claude CLI error (error_max_budget_usd). The review reached the $25 budget cap. To allow more, raise consult.claudeMaxBudgetUsd in .codev/config.json. The claude lane ran /Users/you/.local/bin/claude, 2.1.326 (from PATH). ``` **Read-only, isolated, and safe to nest.** The reviewer session has only Read, Glob and Grep: no edit, shell, web or MCP tools. Consult checks the tool list the session reports when it starts, -and stops the review unless it is exactly those three. If a `claude` release changes the session's -tools, every review stops; pin a known-good `claude` with `CODEV_CLAUDE_BIN` until Codev catches up. -It has a 200-turn cap and a $25 budget cap. It loads none of your +and stops the review unless it is exactly those three (a tool listed twice is fine). If a `claude` +release changes the session's tools, every review stops; pin a known-good `claude` with +`CODEV_CLAUDE_BIN` until Codev catches up. It has a 200-turn cap and a $25 budget cap, both +configurable ([`consult.claudeMaxTurns` and `consult.claudeMaxBudgetUsd`](#consultclaudemaxturns-and-consultclaudemaxbudgetusd)). It loads none of your settings files, hooks, plugins, MCP servers or CLAUDE.md, and its sessions are not saved, so reviews do not fill `claude --resume`. Claude Code's session markers (`CLAUDECODE` and related variables) are removed from the child's environment, so consult works from inside a Claude Code diff --git a/codev/state/bugfix-1941_thread.md b/codev/state/bugfix-1941_thread.md new file mode 100644 index 000000000..e57aa89b5 --- /dev/null +++ b/codev/state/bugfix-1941_thread.md @@ -0,0 +1,41 @@ +# bugfix-1941 thread + +## Investigate (2026-11-20) + +All five points from #1941 located and reproduced against the source (and, for point 1, the real `claude` 2.1.330): + +1. **api-key logged-out advice.** `assertLoggedIn` (`lib/claude-cli.ts`) always says ``Run `claude auth login` `` whatever the mode. Probed the real CLI: with a key set, `auth status --json` reports `loggedIn: true` (even for a bogus key), so the preflight branch is defensive today; a print-mode run with no login ends with result `"Not logged in · Please run /login"` (`is_error: true`, subtype `success`), and `classifyClaudeRun` quotes that CLI text with no mode-aware remedy. A bogus key ends with `"Invalid API key · Fix external API key"` (401). Fix: one `claudeLoginRemedy(mode, source)` used by both the preflight and the review-result path, when the result says logged out. +2. **Tool-guard duplicates.** `startClaudeReview`'s init guard also checks `tools.length !== CLAUDE_REVIEW_TOOLS.length`, so `['Glob','Grep','Read','Read']` aborts. An existing test codifies this. Fix: compare as sets; extra and missing still abort. +3. **Install hint.** `CLAUDE_INSTALL_HINT` is curl only. Fix: a fragment naming both routes, like `CLAUDE_UPDATE_HINT`. +4. **Caps.** `CLAUDE_MAX_TURNS`/`CLAUDE_MAX_BUDGET_USD` are module constants. Fix: `consult.claudeMaxTurns` (positive integer) and `consult.claudeMaxBudgetUsd` (positive number), validated at config load like `claudeAuth`, defaults 200 / 25, cap messages name the key. +5. **Docs.** README/INSTALL lack the 2.1.141 floor; consult.md (both twins) and the consult SKILL.md copies describe withholding the key without saying it is a billing guarantee, not secrecy. + +Scope: small, fits BUGFIX. + +## Fix (2026-11-20) + +Two commits: Fix (0555ab16d) and Test (5f2249cac). + +- `claudeLoginRemedy(mode, source)` in `lib/claude-cli.ts`: login → ``Run `claude auth login`.``; api-key → names `consult.claudeAuth` and its file, says to check the key or remove the setting. Used by `assertLoggedIn` (now takes `{ mode, modeSource, withheld }`; preflight and doctor pass the mode) and by `classifyClaudeRun` when an error result matches `not logged in|run /login`. `ClaudePreflight` gained `modeSource`. +- Tool guard: dropped the length check, so the comparison is set-based. Extra and missing still abort. The old test that codified the duplicate abort became "duplicate + extra aborts on the extra", plus a success test for the duplicate alone. +- `CLAUDE_INSTALL_HINT` is now a fragment naming curl and npm, like `CLAUDE_UPDATE_HINT`. +- `consult.claudeMaxTurns` (positive integer) / `consult.claudeMaxBudgetUsd` (positive number): validated at config load in `consult-lanes.ts`; `resolveClaudeReviewCaps` feeds `buildClaudeReviewArgs` and the cap-reached messages, which name the value and the key. `null` is the config system's "delete this key", so it means default and is not asserted as an error. +- Docs: README/INSTALL got the 2.1.141 floor; consult.md (both twins), consult SKILL.md (all four copies) and the arch.md lane row say withholding is a billing guarantee, not secrecy; consult.md documents the new keys. + +Regression check: with the five source files reverted to pre-fix, 25 of the new/updated tests fail across all four code points; with the fix, the three targeted files pass (209 passed, 1 skipped). + +Noted, not changed: a lane-level bad config throws in `resolveLaneModelChoice` before the lane's try block, so a direct `runClaudeConsultation` call leaves a stale `--output` file. The same is true for a bad `claudeAuth` today; real `consult` loads config before any lane runs. + +## Blocked on porch `tests` under machine load (2026-11-20) + +First full run: `skill-refresh` failed for a real reason (my SKILL.md edit needs `generate-skill-hashes`), fixed in 4877866f5. After that, three porch `tests` runs at load average 35–54 (the load is from other workspaces, chiefly a `shannon` builder's python process) each failed a different, unrelated test, and each passes alone: + +- `src/__tests__/windows-console-spawn-guard.test.ts` (static spawn scan; 3.4s alone) +- `src/agent-farm/__tests__/ide-command.test.ts` (11.6s in the full run; 21/21 alone) +- `src/agent-farm/__tests__/bugfix-1267-launch-loop.test.ts` (21.1s in the full run; 7/7 alone) + +The build check passes. Asked the architect whether to retry once load drops. + +Architect ruling: retry once when the 1-minute load average is below 18, run the three failed tests alone as evidence, and stop on another unrelated failure. +- Alone, once each (load 29–31): windows-console-spawn-guard 7/7, ide-command 21/21, bugfix-1267-launch-loop 7/7. +- One porch run at 18:15, load 14.02 at start and 38.61 at end (the suite raises it): build ✓, tests ✗ on `src/commands/consult/__tests__/agy-auth-cache.test.ts` (27.2s). It references nothing this change touched and passes alone (27/27). Stopped and reported, per the ruling. diff --git a/packages/codev/src/__tests__/claude-cli.test.ts b/packages/codev/src/__tests__/claude-cli.test.ts index c0f9bb709..a8c4d1e78 100644 --- a/packages/codev/src/__tests__/claude-cli.test.ts +++ b/packages/codev/src/__tests__/claude-cli.test.ts @@ -234,6 +234,8 @@ describe('resolveClaudeBin', () => { process.env.CODEV_ALLOW_REAL_CLAUDE = '1'; process.env.PATH = path.join(tmpDir, 'nothing-here'); expect(() => resolveClaudeBin()).toThrow(/curl -fsSL https:\/\/claude\.ai\/install\.sh \| bash/); + // A locked-down CI image needs the npm route (#1941). + expect(() => resolveClaudeBin()).toThrow('`npm install -g @anthropic-ai/claude-code` for an npm install'); expect(() => resolveClaudeBin()).toThrow(/CODEV_CLAUDE_BIN/); }); @@ -439,12 +441,29 @@ describe('readClaudeAuthStatus (scenario 16)', () => { }); describe('assertLoggedIn', () => { + const loggedOut: ClaudeAuthStatus = { loggedIn: false, authMethod: 'none' }; + it('names a withheld key and the one-line opt-in, for key-only machines', () => { - const status: ClaudeAuthStatus = { loggedIn: false, authMethod: 'none' }; - expect(() => assertLoggedIn(status, ['ANTHROPIC_API_KEY'])).toThrow( + expect(() => assertLoggedIn(loggedOut, { withheld: ['ANTHROPIC_API_KEY'] })).toThrow( 'ANTHROPIC_API_KEY is set but not used: to bill the API with it instead, set consult.claudeAuth to "api-key"', ); }); + + it('in login mode, says to run `claude auth login`', () => { + expect(() => assertLoggedIn(loggedOut, { mode: 'login' })).toThrow( + 'claude reports no login (authMethod: none). Run `claude auth login`.', + ); + }); + + it('in api-key mode, points at the key and the config file, not `claude auth login` (#1941)', () => { + const { message } = thrown(() => assertLoggedIn(loggedOut, { mode: 'api-key', modeSource: '/repo/.codev/config.json' })); + expect(message).toBe( + 'claude reports no login (authMethod: none). consult.claudeAuth is "api-key" (set in /repo/.codev/config.json), ' + + 'so the lane authenticates with ANTHROPIC_API_KEY / ANTHROPIC_AUTH_TOKEN, not a claude login: check that the ' + + 'key is valid, or remove consult.claudeAuth to use your claude login.', + ); + expect(message).not.toContain('claude auth login'); + }); }); describe('describeClaudeAuth', () => { @@ -548,6 +567,27 @@ describe('consult.claudeAuth config', () => { }); }); +describe('consult.claudeMaxTurns and consult.claudeMaxBudgetUsd config (#1941)', () => { + it('accepts a positive whole number of turns and a positive, fractional budget', () => { + writeConfig(projectConfig(), { consult: { claudeMaxTurns: 400, claudeMaxBudgetUsd: 0.5 } }); + expect(loadConfig(workspace).consult).toMatchObject({ claudeMaxTurns: 400, claudeMaxBudgetUsd: 0.5 }); + }); + + it.each([0, -1, 2.5, '200', true])('rejects claudeMaxTurns %j at config load', (value) => { + writeConfig(projectConfig(), { consult: { claudeMaxTurns: value } }); + expect(() => loadConfig(workspace)).toThrow( + `Invalid consult.claudeMaxTurns in Codev config: expected a positive whole number of turns, got ${JSON.stringify(value)}.`, + ); + }); + + it.each([0, -5, '25', false])('rejects claudeMaxBudgetUsd %j at config load', (value) => { + writeConfig(projectConfig(), { consult: { claudeMaxBudgetUsd: value } }); + expect(() => loadConfig(workspace)).toThrow( + `Invalid consult.claudeMaxBudgetUsd in Codev config: expected a positive number of US dollars, got ${JSON.stringify(value)}.`, + ); + }); +}); + describe('preflightClaude', () => { it('returns the binary, version, child env, status and both report lines', () => { const healthy = pinnedStub(); @@ -646,6 +686,18 @@ describe('preflightClaude', () => { .toContain(`consult.claudeAuth="api-key" in ${global}`); }); + it('in api-key mode, a logged-out status fails with advice about the key, not `claude auth login` (#1941)', () => { + writeConfig(projectConfig(), { consult: { claudeAuth: 'api-key' } }); + pinnedStub({ authStatus: { stdout: JSON.stringify(STUB_LOGGED_OUT_STATUS), exitCode: 1 } }); + const { processEnv, fromDotenv } = consultEnv({ ANTHROPIC_API_KEY: 'sk-ci' }); + const err = preflightError(() => preflightClaude({ workspaceRoot: workspace, processEnv, fromDotenv })); + expect(err.message).toContain('claude reports no login (authMethod: none).'); + expect(err.message).toContain(`consult.claudeAuth is "api-key" (set in ${projectConfig()})`); + expect(err.message).toContain('check that the key is valid'); + expect(err.message).not.toContain('claude auth login'); + expect(err.authLine).toBe(`none — not logged in, consult.claudeAuth="api-key" in ${projectConfig()}`); + }); + it('in api-key mode with no key, fails before auth status and prints no auth line (scenario 9)', () => { writeConfig(projectConfig(), { consult: { claudeAuth: 'api-key' } }); const recorder = pinnedStub(); diff --git a/packages/codev/src/__tests__/doctor.test.ts b/packages/codev/src/__tests__/doctor.test.ts index a6ccefd34..a2d2fba9e 100644 --- a/packages/codev/src/__tests__/doctor.test.ts +++ b/packages/codev/src/__tests__/doctor.test.ts @@ -1426,6 +1426,17 @@ describe('doctor command', () => { expect(stub.invocations('authStatus')[0].env.ANTHROPIC_API_KEY).toBe('sk-ant-doctor-api-key-mode'); }); + it('in api-key mode, a logged-out status warns about the key, not `claude auth login` (#1941)', async () => { + fs.mkdirSync(path.join(workspace, '.codev')); + fs.writeFileSync(path.join(workspace, '.codev', 'config.json'), JSON.stringify({ consult: { claudeAuth: 'api-key' } })); + fs.writeFileSync(path.join(workspace, '.env'), 'ANTHROPIC_API_KEY=sk-ant-doctor-api-key-mode\n'); + pinStub({ authStatus: { stdout: `${JSON.stringify(STUB_LOGGED_OUT_STATUS)}\n`, exitCode: 1 } }); + const { claudeRows } = await runDoctor(); + expect(claudeRows[1]).toContain(`⚠ ${CLAUDE} not logged in`); + expect(claudeRows[1]).toContain('consult.claudeAuth is "api-key"'); + expect(claudeRows[1]).not.toContain('claude auth login'); + }); + it('honours a CODEV_CLAUDE_BIN set only in the workspace .env, as consult does, without touching process.env', async () => { const stub = createStubClaude(); stubs.push(stub); diff --git a/packages/codev/src/commands/consult/__tests__/claude-cli-lane.test.ts b/packages/codev/src/commands/consult/__tests__/claude-cli-lane.test.ts index e68d6107b..373b7db12 100644 --- a/packages/codev/src/commands/consult/__tests__/claude-cli-lane.test.ts +++ b/packages/codev/src/commands/consult/__tests__/claude-cli-lane.test.ts @@ -45,6 +45,8 @@ const { CLAUDE_CAPABILITY_NOTE, CLAUDE_REVIEW_TOOLS, DEFAULT_CLAUDE_MODEL, + DEFAULT_CLAUDE_MAX_TURNS, + DEFAULT_CLAUDE_MAX_BUDGET_USD, } = await import('../index.js'); const here = path.dirname(fileURLToPath(import.meta.url)); @@ -309,7 +311,10 @@ describe('a default successful review', () => { '--system-prompt-file', systemPromptFile, '--add-dir', sandboxDir, ]); - expect(review.argv).toEqual(buildClaudeReviewArgs({ model: DEFAULT_CLAUDE_MODEL, systemPromptFile, sandboxDir })); + expect(review.argv).toEqual(buildClaudeReviewArgs({ + model: DEFAULT_CLAUDE_MODEL, systemPromptFile, sandboxDir, + caps: { maxTurns: DEFAULT_CLAUDE_MAX_TURNS, maxBudgetUsd: DEFAULT_CLAUDE_MAX_BUDGET_USD }, + })); expect(review.argv).not.toContain('--mcp-config'); expect(review.argv).not.toContain('--dangerously-skip-permissions'); expect(CLAUDE_REVIEW_TOOLS).toEqual(['Read', 'Glob', 'Grep']); @@ -602,7 +607,7 @@ describe('a review run that fails is reported by how it failed', () => { { name: 'error_max_turns, naming the subtype (scenario 21)', print: { stdout: readClaudeFixture('claude-stream-error-max-turns.jsonl'), exitCode: 1 }, - message: [/Claude CLI error \(error_max_turns\)/, /used all 200 turns/], + message: [/Claude CLI error \(error_max_turns\)/, /used all 200 turns\. To allow more, raise consult\.claudeMaxTurns in \.codev\/config\.json\./], usage: { inputTokens: 2, cachedInputTokens: 0, outputTokens: 4, costUsd: 0.03259 }, }, { @@ -611,7 +616,7 @@ describe('a review run that fails is reported by how it failed', () => { stdout: withResult('claude-stream-error-max-turns.jsonl', (r) => ({ ...r, subtype: 'error_max_budget_usd' })), exitCode: 1, }, - message: [/Claude CLI error \(error_max_budget_usd\)/, /\$25 budget cap/], + message: [/Claude CLI error \(error_max_budget_usd\)/, /\$25 budget cap\. To allow more, raise consult\.claudeMaxBudgetUsd in \.codev\/config\.json\./], }, { name: '"Prompt is too long", as context exhaustion (scenario 22)', @@ -718,9 +723,9 @@ describe('a protocol failure mid-stream stops the review child before the lane f message: /claude did not honour --tools; refusing a review without Grep\./, }, { - name: 'an init event repeating a tool on top of all three', - stdout: jsonl({ ...INIT, tools: ['Glob', 'Grep', 'Read', 'Read'] }), - message: /claude did not honour --tools; refusing a review without each tool exactly once\./, + name: 'an init event with a duplicate and an extra tool', + stdout: jsonl({ ...INIT, tools: ['Glob', 'Grep', 'Read', 'Read', 'Bash'] }), + message: /claude did not honour --tools; refusing a review with tools consult did not grant: "Bash"\./, }, { name: 'an assistant event before any init event', @@ -752,6 +757,122 @@ describe('a protocol failure mid-stream stops the review child before the lane f }); }); +// --- #1941: the tool guard compares sets ---------------------------------------------------------- + +describe('the init guard compares the session tools as sets (#1941)', () => { + const ctx = freshContextPerTest(); + + it('runs the review when a tool is listed twice', async () => { + const run = await runLane(ctx(), { + stub: { print: { stdout: jsonl({ ...INIT, tools: ['Glob', 'Grep', 'Read', 'Read'] }, assistant(text('VERDICT: APPROVE')), RESULT_OK) } }, + }); + + expect(run.error).toBeUndefined(); + expect(fs.readFileSync(run.outputPath, 'utf-8')).toBe('VERDICT: APPROVE'); + }); +}); + +// --- #1941: a logged-out answer gets the remedy for the auth mode --------------------------------- + +describe('a logged-out answer names the remedy for the auth mode (#1941)', () => { + const ctx = freshContextPerTest(); + const NOT_LOGGED_IN_RESULT = 'Not logged in · Please run /login'; + const notLoggedIn = withResult('claude-stream-error-result.jsonl', (r) => ({ ...r, result: NOT_LOGGED_IN_RESULT })); + const API_KEY_STATUS = { loggedIn: true, authMethod: 'api_key', apiProvider: 'firstParty', apiKeySource: 'ANTHROPIC_API_KEY' }; + + function apiKeyMode(c: Ctx): string { + process.env.ANTHROPIC_API_KEY = 'sk-ci'; + return writeProjectConfig(c, { consult: { claudeAuth: 'api-key' } }); + } + + it('login mode: a logged-out review result says to run `claude auth login`', async () => { + const run = await runLane(ctx(), { stub: { print: { stdout: notLoggedIn, exitCode: 1 } } }); + + expect(run.error!.message).toContain(`Claude returned an error instead of a review: ${NOT_LOGGED_IN_RESULT}`); + expect(run.error!.message).toContain('Run `claude auth login`.'); + expect(fs.existsSync(run.outputPath)).toBe(false); + }); + + it('api-key mode: a logged-out review result points at the key, not `claude auth login`', async () => { + const configFile = apiKeyMode(ctx()); + const run = await runLane(ctx(), { + stub: { authStatus: { stdout: JSON.stringify(API_KEY_STATUS) }, print: { stdout: notLoggedIn, exitCode: 1 } }, + }); + + expect(run.error!.message).toContain(`Claude returned an error instead of a review: ${NOT_LOGGED_IN_RESULT}`); + expect(run.error!.message).toContain(`consult.claudeAuth is "api-key" (set in ${configFile})`); + expect(run.error!.message).toContain('check that the key is valid'); + expect(run.error!.message).not.toContain('claude auth login'); + expect(fs.existsSync(run.outputPath)).toBe(false); + }); + + it('api-key mode: a logged-out auth status points at the key, not `claude auth login`', async () => { + const configFile = apiKeyMode(ctx()); + const run = await runLane(ctx(), { stub: { authStatus: { stdout: JSON.stringify(STUB_LOGGED_OUT_STATUS), exitCode: 1 } } }); + + expect(run.error!.message).toContain('claude reports no login (authMethod: none).'); + expect(run.error!.message).toContain(`consult.claudeAuth is "api-key" (set in ${configFile})`); + expect(run.error!.message).not.toContain('claude auth login'); + expect(run.review).toBeUndefined(); + }); + + it('adds no login remedy to an error result that is not about the login', async () => { + const run = await runLane(ctx(), { stub: { print: { stdout: readClaudeFixture('claude-stream-error-result.jsonl'), exitCode: 1 } } }); + + expect(run.error!.message).toContain('You have hit your org monthly usage limit'); + expect(run.error!.message).not.toContain('claude auth login'); + }); +}); + +// --- #1941: configurable caps --------------------------------------------------------------------- + +describe('consult.claudeMaxTurns and consult.claudeMaxBudgetUsd set the review caps (#1941)', () => { + const ctx = freshContextPerTest(); + + it('pass the configured caps to the claude argv', async () => { + writeProjectConfig(ctx(), { consult: { claudeMaxTurns: 350, claudeMaxBudgetUsd: 7.5 } }); + const run = await runLane(ctx()); + + expect(run.error).toBeUndefined(); + expect(flagValue(run.review!.argv, '--max-turns')).toBe('350'); + expect(flagValue(run.review!.argv, '--max-budget-usd')).toBe('7.5'); + }); + + it('name the configured turn cap and its setting when the review reaches it', async () => { + writeProjectConfig(ctx(), { consult: { claudeMaxTurns: 350 } }); + const run = await runLane(ctx(), { stub: { print: { stdout: readClaudeFixture('claude-stream-error-max-turns.jsonl'), exitCode: 1 } } }); + + expect(run.error!.message).toContain('The review used all 350 turns. To allow more, raise consult.claudeMaxTurns in .codev/config.json.'); + }); + + it('name the configured budget cap and its setting when the review reaches it', async () => { + writeProjectConfig(ctx(), { consult: { claudeMaxBudgetUsd: 7.5 } }); + const run = await runLane(ctx(), { + stub: { + print: { + stdout: withResult('claude-stream-error-max-turns.jsonl', (r) => ({ ...r, subtype: 'error_max_budget_usd' })), + exitCode: 1, + }, + }, + }); + + expect(run.error!.message).toContain( + 'The review reached the $7.5 budget cap. To allow more, raise consult.claudeMaxBudgetUsd in .codev/config.json.', + ); + }); + + it.each([ + [{ claudeMaxTurns: 0 }, /Invalid consult\.claudeMaxTurns in Codev config: .* got 0\./], + [{ claudeMaxBudgetUsd: 'lots' }, /Invalid consult\.claudeMaxBudgetUsd in Codev config: .* got "lots"\./], + ])('fail the lane before any review on a bad value: %j', async (consult, message) => { + writeProjectConfig(ctx(), { consult }); + const run = await runLane(ctx()); + + expect(run.error!.message).toMatch(message); + expect(run.review).toBeUndefined(); + }); +}); + // --- Scenario 23a: a signal mid-review ------------------------------------------------------------ describe('SIGTERM while the review runs (scenario 23a)', () => { diff --git a/packages/codev/src/commands/consult/index.ts b/packages/codev/src/commands/consult/index.ts index 50f74fc5b..35036461d 100644 --- a/packages/codev/src/commands/consult/index.ts +++ b/packages/codev/src/commands/consult/index.ts @@ -37,6 +37,7 @@ import { CLAUDE_UPDATE_HINT, ClaudePreflightError, claudeLaunch, + claudeLoginRemedy, credentialOrigin, excerpt, preflightClaude, @@ -815,13 +816,32 @@ export async function runCodexConsultation( export const CLAUDE_REVIEW_TOOLS = ['Read', 'Glob', 'Grep'] as const; /** - * Turn cap for a review. Claude explores the codebase with Read/Glob/Grep before producing its - * verdict, so it needs a generous turn budget. + * Default turn cap for a review; `consult.claudeMaxTurns` overrides it. Claude explores the codebase + * with Read/Glob/Grep before producing its verdict, so it needs a generous turn budget. */ -const CLAUDE_MAX_TURNS = 200; +export const DEFAULT_CLAUDE_MAX_TURNS = 200; -/** Spend cap for a review, in USD. `--max-budget-usd` works in print mode only, which is how the lane runs. */ -const CLAUDE_MAX_BUDGET_USD = 25; +/** + * Default spend cap for a review, in USD; `consult.claudeMaxBudgetUsd` overrides it. + * `--max-budget-usd` works in print mode only, which is how the lane runs. On a subscription login + * the dollars are notional, which is why the cap is configurable. + */ +export const DEFAULT_CLAUDE_MAX_BUDGET_USD = 25; + +/** The caps one review runs under. */ +export interface ClaudeReviewCaps { + maxTurns: number; + maxBudgetUsd: number; +} + +/** `consult.claudeMaxTurns` and `consult.claudeMaxBudgetUsd`, or their defaults. Config load validates both. */ +export function resolveClaudeReviewCaps(workspaceRoot: string): ClaudeReviewCaps { + const consult = loadConfig(workspaceRoot).consult; + return { + maxTurns: consult?.claudeMaxTurns ?? DEFAULT_CLAUDE_MAX_TURNS, + maxBudgetUsd: consult?.claudeMaxBudgetUsd ?? DEFAULT_CLAUDE_MAX_BUDGET_USD, + }; +} /** The stderr tail a failure message quotes, bounded like `AGY_FAILURE_TAIL_MAX_CHARS`. */ const CLAUDE_STDERR_TAIL_MAX_CHARS = 2000; @@ -870,7 +890,12 @@ export const CLAUDE_CAPABILITY_NOTE = [ * was run live only on 2.1.326. A flag added later must be verified at the floor, or the floor * raised in the same commit. */ -export function buildClaudeReviewArgs(opts: { model: string; systemPromptFile: string; sandboxDir: string }): string[] { +export function buildClaudeReviewArgs(opts: { + model: string; + systemPromptFile: string; + sandboxDir: string; + caps: ClaudeReviewCaps; +}): string[] { const tools = CLAUDE_REVIEW_TOOLS.join(','); return [ '-p', @@ -884,8 +909,8 @@ export function buildClaudeReviewArgs(opts: { model: string; systemPromptFile: s '--strict-mcp-config', '--disable-slash-commands', '--no-session-persistence', - '--max-turns', String(CLAUDE_MAX_TURNS), - '--max-budget-usd', String(CLAUDE_MAX_BUDGET_USD), + '--max-turns', String(opts.caps.maxTurns), + '--max-budget-usd', String(opts.caps.maxBudgetUsd), '--model', opts.model, '--system-prompt-file', opts.systemPromptFile, '--add-dir', opts.sandboxDir, @@ -1017,8 +1042,10 @@ function startClaudeReview(opts: { } if (event.type === 'system' && event.subtype === 'init') { - // The session must have exactly the review tools: an extra one could write or run a shell, - // and a missing one means the reviewer reads less than the review assumes. + // The session must have exactly the review tools, compared as sets: an extra one could + // write or run a shell, and a missing one means the reviewer reads less than the review + // assumes. A tool listed twice is still the same tool, so a cosmetic duplicate in a claude + // release must not stop every review (#1941). const tools = event.tools; const allowed: readonly unknown[] = CLAUDE_REVIEW_TOOLS; const reported = `The session reported tools: ${excerpt(JSON.stringify(tools ?? null))}; expected exactly ${CLAUDE_REVIEW_TOOLS.join(', ')}.`; @@ -1032,8 +1059,8 @@ function startClaudeReview(opts: { return; } const missing = CLAUDE_REVIEW_TOOLS.filter((tool) => !tools.includes(tool)); - if (missing.length > 0 || tools.length !== CLAUDE_REVIEW_TOOLS.length) { - abort(toolsFailure(`without ${missing.length > 0 ? missing.join(', ') : 'each tool exactly once'}`, reported)); + if (missing.length > 0) { + abort(toolsFailure(`without ${missing.join(', ')}`, reported)); return; } sawInit = true; @@ -1123,6 +1150,9 @@ function startClaudeReview(opts: { const PROMPT_TOO_LONG = /prompt is too long/i; +/** A result saying the session has no login, e.g. `Not logged in · Please run /login` (2.1.330). */ +const NOT_LOGGED_IN = /not logged in|run \/login/i; + /** * Whether a finished review child produced a review, or why not. Null means success. * @@ -1133,7 +1163,12 @@ const PROMPT_TOO_LONG = /prompt is too long/i; * `is_error` is checked before a `success` subtype is trusted: the CLI reports an API error (a usage * limit, a bad credential) as subtype `success` with `is_error: true`. */ -function classifyClaudeRun(run: ClaudeReviewRun, modelId: string, authLine: string): Error | null { +function classifyClaudeRun( + run: ClaudeReviewRun, + modelId: string, + preflight: Pick, + caps: ClaudeReviewCaps, +): Error | null { const exited = run.signal ? `was killed by ${run.signal}` : `exited with code ${run.exitCode}`; const stderr = run.stderrTail.trim() ? `\n\nclaude stderr (last ${CLAUDE_STDERR_TAIL_MAX_CHARS} chars):\n${run.stderrTail.trim()}` @@ -1162,15 +1197,20 @@ function classifyClaudeRun(run: ClaudeReviewRun, modelId: string, authLine: stri if (result.subtype !== 'success') { const cap = result.subtype === 'error_max_turns' - ? ` The review used all ${CLAUDE_MAX_TURNS} turns.` + ? ` The review used all ${caps.maxTurns} turns. To allow more, raise consult.claudeMaxTurns in .codev/config.json.` : result.subtype === 'error_max_budget_usd' - ? ` The review reached the $${CLAUDE_MAX_BUDGET_USD} budget cap.` + ? ` The review reached the $${caps.maxBudgetUsd} budget cap. To allow more, raise consult.claudeMaxBudgetUsd in .codev/config.json.` : ''; return new Error(`Claude CLI error (${result.subtype})${errors.length ? `: ${errors.join(', ')}` : ''}.${cap}`); } if (result.is_error) { - return new Error(`Claude returned an error instead of a review: ${resultText}\nCredential in use: ${authLine}`); + // The CLI's own advice for a missing login is its interactive `/login`, which is wrong in + // `api-key` mode, so the lane adds the remedy for the mode it ran in (#1941). + const remedy = NOT_LOGGED_IN.test(resultText) ? `\n${claudeLoginRemedy(preflight.mode, preflight.modeSource)}` : ''; + return new Error( + `Claude returned an error instead of a review: ${resultText}\nCredential in use: ${preflight.authLine}${remedy}` + ); } if (run.exitCode !== 0) { @@ -1261,9 +1301,10 @@ export async function runClaudeConsultation( systemPromptFile = path.join(sandboxDir, `claude-system-prompt-${process.pid}-${Date.now()}.md`); fs.writeFileSync(systemPromptFile, `${role}\n\n${CLAUDE_CAPABILITY_NOTE}\n`, { mode: 0o600, flag: 'wx' }); + const caps = resolveClaudeReviewCaps(workspaceRoot); const review = startClaudeReview({ bin: preflight.bin, - args: buildClaudeReviewArgs({ model: choice.id, systemPromptFile, sandboxDir }), + args: buildClaudeReviewArgs({ model: choice.id, systemPromptFile, sandboxDir, caps }), prompt: queryText, cwd: workspaceRoot, env: preflight.env, @@ -1279,7 +1320,7 @@ export async function runClaudeConsultation( const denials = describeClaudePermissionDenials(result?.permission_denials); if (denials) console.error(denials); - const failure = classifyClaudeRun(run, choice.id, preflight.authLine); + const failure = classifyClaudeRun(run, choice.id, preflight, caps); if (failure) throw failure; // A successful result with no assistant text has no VERDICT block to save. if (chunks.join('').trim() === '') { diff --git a/packages/codev/src/commands/doctor.ts b/packages/codev/src/commands/doctor.ts index e7a9e1516..94931c0a3 100644 --- a/packages/codev/src/commands/doctor.ts +++ b/packages/codev/src/commands/doctor.ts @@ -41,6 +41,7 @@ import { readClaudeVersion, resolveClaudeAuthMode, resolveClaudeBin, + type ClaudeAuthMode, type ClaudeAuthStatus, } from '../lib/claude-cli.js'; import { readDotenvFile } from '../lib/dotenv.js'; @@ -946,7 +947,8 @@ function envWithWorkspaceDotenv(workspaceRoot: string | null): { env: NodeJS.Pro * key the lane would withhold is reported as withheld, naming where it came from, and a login the * review would not see is not reported as working. * - * Any problem is a warning carrying the lane's own message: `claude auth login` when logged out, + * Any problem is a warning carrying the lane's own message: the mode's remedy when logged out + * (`claude auth login`, or the key in `api-key` mode), * the output excerpt when `auth status` is unparseable, the missing key in `api-key` mode, and a * config error for an unknown `consult.claudeAuth`. */ @@ -957,18 +959,20 @@ function verifyClaudeViaCli( ): CheckResult { let status: ClaudeAuthStatus; let withheld: string[]; + let mode: ClaudeAuthMode; + let modeSource: string | null; try { - const { mode, source } = resolveClaudeAuthMode(workspaceRoot ?? process.cwd()); + ({ mode, source: modeSource } = resolveClaudeAuthMode(workspaceRoot ?? process.cwd())); const child = buildClaudeChildEnv(processEnv, mode); withheld = child.withheld; - assertApiKeyPresent(child.env, mode, source); + assertApiKeyPresent(child.env, mode, modeSource); status = readClaudeAuthStatus(bin, child.env); } catch (err) { return { status: 'warn', version: 'auth unknown', note: errorMessage(err) }; } try { - assertLoggedIn(status, withheld); + assertLoggedIn(status, { mode, modeSource, withheld }); } catch (err) { return { status: 'warn', version: 'not logged in', note: errorMessage(err) }; } diff --git a/packages/codev/src/lib/claude-cli.ts b/packages/codev/src/lib/claude-cli.ts index f1aa47b84..3c071980e 100644 --- a/packages/codev/src/lib/claude-cli.ts +++ b/packages/codev/src/lib/claude-cli.ts @@ -40,7 +40,13 @@ export const CLAUDE_MIN_VERSION = '2.1.141'; /** Pins the binary the lane runs. Mirrors `CODEV_AGY_BIN`. */ export const CLAUDE_BIN_ENV = 'CODEV_CLAUDE_BIN'; -export const CLAUDE_INSTALL_HINT = 'curl -fsSL https://claude.ai/install.sh | bash'; +/** + * How to install `claude`, for both install routes: the native installer, and npm for a locked-down + * CI image that cannot run it. A sentence fragment, so callers end it. + */ +export const CLAUDE_INSTALL_HINT = + 'Install claude: `curl -fsSL https://claude.ai/install.sh | bash` for the native install, or ' + + '`npm install -g @anthropic-ai/claude-code` for an npm install'; /** * How to update `claude`, for both install routes: `claude update` serves the native installer, and an @@ -152,7 +158,7 @@ export function resolveClaudeBin(env: NodeJS.ProcessEnv = process.env): string { } throw new Error( `The claude CLI was not found on PATH. Codev runs your installed claude (the claude review lane, and the merge session of \`codev update\` and \`codev adopt\`). ` + - `Install it with \`${CLAUDE_INSTALL_HINT}\`, or set ${CLAUDE_BIN_ENV} to its absolute path.` + `${CLAUDE_INSTALL_HINT}. Or set ${CLAUDE_BIN_ENV} to its absolute path.` ); } @@ -351,13 +357,33 @@ export function readClaudeAuthStatus(bin: string, env: Record): return parseClaudeAuthStatus(bin, stdout); } -export function assertLoggedIn(status: ClaudeAuthStatus, withheld: readonly string[] = []): void { +/** + * What to do when claude says it is not logged in, for the mode the lane ran in. In `login` mode + * that is a claude login. In `api-key` mode it is the key: the lane authenticates with it, never + * with a stored login, so `claude auth login` would not change the outcome. + */ +export function claudeLoginRemedy(mode: ClaudeAuthMode, modeSource: string | null = null): string { + if (mode === 'login') return 'Run `claude auth login`.'; + return ( + `consult.claudeAuth is "api-key"${modeSource ? ` (set in ${modeSource})` : ''}, so the lane ` + + `authenticates with ANTHROPIC_API_KEY / ANTHROPIC_AUTH_TOKEN, not a claude login: check that ` + + `the key is valid, or remove consult.claudeAuth to use your claude login.` + ); +} + +export function assertLoggedIn( + status: ClaudeAuthStatus, + opts: { mode?: ClaudeAuthMode; modeSource?: string | null; withheld?: readonly string[] } = {}, +): void { if (status.loggedIn) return; + const { mode = 'login', modeSource = null, withheld = [] } = opts; const keyHint = withheld.length > 0 ? ` ${withheld.join(' and ')} ${withheld.length > 1 ? 'are' : 'is'} set but not used: to bill ` + `the API with it instead, set consult.claudeAuth to "api-key" in .codev/config.json.` : ''; - throw new Error(`claude reports no login (authMethod: ${status.authMethod}). Run \`claude auth login\`.${keyHint}`); + throw new Error( + `claude reports no login (authMethod: ${status.authMethod}). ${claudeLoginRemedy(mode, modeSource)}${keyHint}` + ); } // --------------------------------------------------------------------------- @@ -462,6 +488,8 @@ export interface ClaudePreflight { bin: string; version: string; mode: ClaudeAuthMode; + /** The config file that set `consult.claudeAuth`, or null for the default. */ + modeSource: string | null; env: Record; status: ClaudeAuthStatus; /** Content of the `[CLAUDE] binary:` line. */ @@ -499,9 +527,9 @@ export function preflightClaude(opts: { const status = readClaudeAuthStatus(bin, env); authLine = describeClaudeAuth({ status, env, mode, modeSource: source, withheld, fromDotenv: opts.fromDotenv }); - assertLoggedIn(status, withheld); + assertLoggedIn(status, { mode, modeSource: source, withheld }); - return { bin, version, mode, env, status, binaryLine, authLine }; + return { bin, version, mode, modeSource: source, env, status, binaryLine, authLine }; } catch (err) { throw new ClaudePreflightError(errorText(err), binaryLine, authLine, { cause: err }); } diff --git a/packages/codev/src/lib/config.ts b/packages/codev/src/lib/config.ts index 41841fa8d..c9ae70c9a 100644 --- a/packages/codev/src/lib/config.ts +++ b/packages/codev/src/lib/config.ts @@ -24,6 +24,8 @@ import { validatePricing, validateCodexPath, validateClaudeAuth, + validateClaudeMaxTurns, + validateClaudeMaxBudgetUsd, validateConsultationConfig, type CodexPricing, type ClaudeAuthMode, @@ -104,6 +106,14 @@ export interface CodevConfig { * Honoured from every config layer; the lane's auth line names the file that set it. */ claudeAuth?: ClaudeAuthMode; + /** The claude lane's turn cap per review (`--max-turns`, #1941). A positive integer; default 200. */ + claudeMaxTurns?: number; + /** + * The claude lane's spend cap per review in USD (`--max-budget-usd`, #1941). A positive number; + * default 25. On a subscription login the dollars are notional, so a large review can reach it + * without any spend. + */ + claudeMaxBudgetUsd?: number; }; forge?: Record & { provider?: string }; templates?: { @@ -386,6 +396,8 @@ export function loadConfig(workspaceRoot: string): CodevConfig { validatePricing(merged.consult?.pricing); validateCodexPath(merged.consult?.codexPath); validateClaudeAuth(merged.consult?.claudeAuth); + validateClaudeMaxTurns(merged.consult?.claudeMaxTurns); + validateClaudeMaxBudgetUsd(merged.consult?.claudeMaxBudgetUsd); validateConsultationConfig(merged.porch?.consultation, workspaceRoot); return merged; diff --git a/packages/codev/src/lib/consult-lanes.ts b/packages/codev/src/lib/consult-lanes.ts index a49ed8c48..53d2d0201 100644 --- a/packages/codev/src/lib/consult-lanes.ts +++ b/packages/codev/src/lib/consult-lanes.ts @@ -248,6 +248,34 @@ export function validateClaudeAuth(claudeAuth: unknown): void { } } +/** + * `consult.claudeMaxTurns` — the claude lane's turn cap, passed to `--max-turns` (#1941). A + * positive integer; unset → the lane's default. + */ +export function validateClaudeMaxTurns(maxTurns: unknown): void { + if (maxTurns === undefined) return; + if (typeof maxTurns !== 'number' || !Number.isInteger(maxTurns) || maxTurns < 1) { + fail( + `Invalid consult.claudeMaxTurns in Codev config: expected a positive whole number of turns, ` + + `got ${JSON.stringify(maxTurns)}.` + ); + } +} + +/** + * `consult.claudeMaxBudgetUsd` — the claude lane's spend cap in USD, passed to `--max-budget-usd` + * (#1941). A positive number; unset → the lane's default. + */ +export function validateClaudeMaxBudgetUsd(maxBudgetUsd: unknown): void { + if (maxBudgetUsd === undefined) return; + if (typeof maxBudgetUsd !== 'number' || !Number.isFinite(maxBudgetUsd) || maxBudgetUsd <= 0) { + fail( + `Invalid consult.claudeMaxBudgetUsd in Codev config: expected a positive number of US ` + + `dollars, got ${JSON.stringify(maxBudgetUsd)}.` + ); + } +} + export function validatePricing(pricing: unknown): void { if (pricing === undefined) return; if (typeof pricing !== 'object' || pricing === null || Array.isArray(pricing)) { diff --git a/packages/codev/src/lib/shipped-skill-hashes.ts b/packages/codev/src/lib/shipped-skill-hashes.ts index 990975da8..46340bb9c 100644 --- a/packages/codev/src/lib/shipped-skill-hashes.ts +++ b/packages/codev/src/lib/shipped-skill-hashes.ts @@ -93,6 +93,7 @@ export const SHIPPED_SKILL_HASHES: Record = { '9d75d339dc41ecec2912f522f8937ea41f13d34cbae5668a7a72cf57f9b4e886', 'aaf54b22ca4c6d39497934b29407fe97f3580f9c866e4706a7827fa812e44e28', 'c2697868ed6a0f25d4e116877bd7699aef9259e11f3e27131e2c01af4c4a5f61', + 'dd036518b2ae7fc82d483a467577d22941cf9ec4742d14943a9f2c1f0b60d924', 'dfa095714c460375c900d7491ba6e045136594d211381cf78cb15b8356c90041', 'f7007e62436ad1c048ea9044b49d25ab5d21b438f844dc1170885d6d531c2156', ], @@ -189,6 +190,7 @@ export const SHIPPED_SKILL_HASHES: Record = { '.codex/skills/consult': [ '1ca473f62885088632ec05a6c14a7defc5e7ffad59fafd86f1afd58d6e419ab5', '9d75d339dc41ecec2912f522f8937ea41f13d34cbae5668a7a72cf57f9b4e886', + 'dd036518b2ae7fc82d483a467577d22941cf9ec4742d14943a9f2c1f0b60d924', 'dfa095714c460375c900d7491ba6e045136594d211381cf78cb15b8356c90041', 'f7007e62436ad1c048ea9044b49d25ab5d21b438f844dc1170885d6d531c2156', ],