Repository navigation
fix(anthropic): report the installed claude-cli version on the OAuth path - #150790
KrasimirKralev wants to merge 11 commits into
Conversation
…path On the subscription (OAuth) path the Anthropic transport presents itself as `claude-cli/<version>` with the version pinned in the bundle (2.1.75). Anthropic gates its newest models on that number: a Fable 5.1 request is refused with HTTP 400 "version 2.1.251 or newer is required" — while the same login works through the Anthropic CLI that is installed beside OpenClaw, because that one is newer. The pinned value stays as the fallback. The host now asks the installed CLI what it is (`claude --version`, once per process, best effort) and reports that version in the user-agent and the billing header when it is newer than the fallback. Nothing is reported that the machine does not print itself: no CLI, a slow one, or an unexpected output leaves the fallback; an older install is never adopted. `@openclaw/ai` stays free of process spawning — it gains `setAnthropicClaudeCodeVersion` / `getAnthropicClaudeCodeVersion` and computes the billing block from the current value; the probe lives with the other host wiring in `src/llm/ai-transport-host.ts`.
|
🦞👀 Pull request received. I will update this pull request when review starts. ClawSweeper review completeClawSweeper finished reviewing this revision. The review result is being finalized. |
|
Codex review: needs real behavior proof before merge. Reviewed September 17, 2026, 8:53 AM ET / 12:53 UTC (Revision 8). ClawSweeper reviewWhat this changesDetects the installed Claude CLI version at startup and uses one consistent version in Anthropic OAuth headers and billing metadata, retaining the existing fallback. Merge readiness⛔ Blocked before merge - 5 items remain This remains a useful fix for an unfixed OAuth failure, but the probe bypasses its advertised output bound. The updated Linux live evidence is valid; the previously requested Windows evidence remains outstanding. Priority: P1 Review scores
Verification
How this fits togetherOpenClaw’s Anthropic adapters turn model requests and credentials into API calls. The host discovers the installed CLI version, and OAuth requests use that value when presenting their client identity to Anthropic. flowchart LR
A[Installed Claude CLI] --> B[Host version probe]
B --> C[Shared version or fallback]
D[Model request and credential] --> E{OAuth route?}
C --> F[Consistent request identity]
E -->|Yes| F
E -->|No| G[Existing request construction]
F --> H[Anthropic API]
G --> H
Before merge
Findings
Agent review detailsSecurityNone. PR surfaceSource +380, Tests +590, Other +4. Total +974 across 14 files. View PR surface stats
Review metrics
Root-cause clusterRelationship: Members:
Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything. Merge-risk optionsMaintainer options:
Technical reviewBest possible solution: Keep one bounded host probe and one consistent per-request OAuth identity, preserving the existing fallback and unaffected authentication routes. Do we have a high-confidence way to reproduce the issue? Yes: current main retains the stale identity, and supplied live A/B traces demonstrate rejection and recovery. The newly identified output-bound defect is source-proven; no target code was executed during this review. Is this the best way to solve the issue? No, not quite yet: the shared identity and host-owned discovery are appropriate, but the observer must enforce the advertised output bound. Full review comments:
Overall correctness: patch is incorrect AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning medium; reviewed against 3c0fb2025a1d. LabelsLabel justifications:
EvidenceWhat I checked:
Likely related people:
Rank-up movesOptional improvements that raise the rating; they are not merge blockers.
Rating scale
Overall follows the weaker of proof and patch quality. Workflow
HistoryReview history (7 earlier review cycles)
|
…requests The host probe that adopts the installed CLI version ran unawaited, so the first direct OAuth request of a fresh process could still go out as the pinned 2.1.75 while a newer install was being detected, and the managed transport read the version twice per request (user-agent at client construction, billing block after message conversion), so a probe finishing in between produced two versions in one request. The probe now registers itself as identity startup work in @openclaw/ai (deferAnthropicClaudeCodeIdentityUntil, bounded at 7 s); both transports await resolveAnthropicClaudeCodeIdentity() once, before constructing the client, and thread that single snapshot into the user-agent header and the billing system block. A probe that fails or never settles leaves the pinned fallback and never fails a request. Tests: the identity gate (waits, one snapshot, bounded, failure-safe); both transports through the request boundary with a delayed probe (fetch not called until it settles; user-agent and billing block carry the same adopted version); the host probe making resolveAnthropicClaudeCodeIdentity wait.
|
Addressed the P2 finding in
PR body now carries the after-fix production-path evidence: one Request-boundary tests in both adapters cover the wait: no request is sent until the probe settles, then one version appears in both fields. |
Both Anthropic adapters awaited the installed-CLI discovery gate as the first step of a request, before auth routing, so while a fresh process was still probing, an API-key request, a Copilot or Foundry request and an injected SDK client all waited up to 7 s for an identity none of them ever sends. The wait now happens inside the OAuth branch of each client builder, after the auth route is decided. That branch returns the snapshot it resolved, the callers thread it into the billing system block, and every other route builds its client and its params without touching the gate. Tests: with a probe still pending, an API-key request reaches the client and the wire immediately and carries neither a claude-cli user-agent nor a billing block, in the provider and in the native transport; the existing OAuth tests still prove the wait and one version in both fields.
The version probe spawned `claude --version` with a bare execFile. On Windows npm installs the CLI as a `claude.cmd` shim, which that spawn cannot launch — windowsHide adds no batch-file handling — so both candidate commands failed and the OAuth path silently kept reporting the pinned 2.1.75. It now goes through runCommandWithTimeout, the runner the rest of src/ uses for short buffered commands and the one whose trusted cmd.exe wrapping is covered by src/process/exec.windows.test.ts. The contract is unchanged: 3 s timeout, 16 KiB of captured output, hidden window, first line of stdout, null on any failure, never throwing, and the probe injection point stays for tests. Tests: the default probe calls the runner with the expected argv and bounds, and reports nothing when the launch rejects or the command exits nonzero.
…line The probe waited for the CLI to exit, but an install can keep running after printing its version, so the runner hit the 3 s timeout and the probe reported nothing although the version had already arrived: the OAuth request went out with the pinned fallback again. The probe now stops the command as soon as the first stdout line is complete and reports that line; the exit code says nothing about a line that already arrived, and a launch failure still rejects into the fallback. Test: a runner that prints the version and lingers is stopped and its version adopted.
Waiting for the runner to stop a lingering CLI held the identity gate past its bound: on this machine the installed CLI printed its version at once but took eight seconds to stop, so the first OAuth request still went out with the pinned fallback. The probe now resolves as soon as the first stdout line is complete and leaves the runner to stop the command in the background, bounded by its timeout.
…g files Both big Anthropic test files are over the 1000-line cap and grandfathered by the fast-baseline ratchet, so the identity request-boundary tests added here made them grow and the ratchet rejected the branch. The two pairs move into focused siblings with their own minimal harness (the SDK constructor mock for the provider, a host fetch mock for the transport): the OAuth request that waits for a pending installed-CLI probe and carries one version in both the user-agent and the billing block, and the API-key request that must not wait and carries neither. No assertion changed, and the two big files are back at or under their ratchet counts.
The native Messages transport module is over its 700-line cap and grew again here, so the fast-baseline ratchet rejected the branch. Client construction moves to a sibling module: base-URL resolution, the SSE-backed HTTP client, and the auth routing that decides which credential and which identity headers one request carries — subscription OAuth, Copilot and Foundry bearer, gateway and plain API key. The stream module keeps payload building, the response contract and the stream function, and imports the client back; the Messages endpoint URL stays on its public surface, where callers outside the package already find it. Behavior is unchanged. The moved JSON.parse assertion now carries the SAFETY invariant the assertion ratchet asks of a new file, so the stream module's baseline shrinks by one.
…st work Both client builders reached the OAuth branch only after running the work above it — most of all the host's guarded model fetch, which loads plugin metadata and takes seconds on a cold process. The identity gate is bounded from the probe's start, so that work spent the budget and the first subscription request of a fresh process still went out as the pinned 2.1.75, which is what the live proof on this branch showed. The auth route is now decided from the credential first, and the identity is awaited as the very first await when that route is OAuth; the branch itself consumes the snapshot, so one request still carries one version in the user-agent and the billing block. Every other route — gateway, Copilot, Foundry, plain API key, injected client — still never touches the gate. Tests: the request-boundary tests now assert the order, so a slow fetch builder cannot change which version an OAuth request carries.
The production scan flags the version parser and the once-per-process reset seam as unused exports: only the focused probe tests consume them, while production reaches the probe through adoptInstalledClaudeCodeVersion. Record that in the ignore map next to the other test-only seams.
|
Both P2 findings from the third review are addressed; head is
Housekeeping the CI lanes asked for: the transport's client construction moved to the sibling Gates on this head: The live OAuth proof in the body was re-run at this head, both arms: the installed CLI's version is detected and awaited before the request is built ( |
Direct Anthropic OAuth requests could fail with claude_code_version_too_old because they advertised Claude Code 2.1.75. Maintain a 2.1.278 client-version floor and prefer a newer installed stable CLI through the Anthropic plugin's shared, bounded probe. Both transports use one selected version for request headers and billing metadata; failed or slow probes fall back to the floor. Preserve API-key and other provider routes without configuration, credential, dependency, or state migrations. Cover request identity and probe lifecycle, and repair the cold metadata fixture and OS-watch race encountered during validation. Focused tests and exact-head CI passed; candidate live OAuth acceptance remains unverified. Thanks to @Cyb3rb1ade, @raghidtawil-lab, and @davidcittadini for the reports and controlled comparisons. The single-snapshot and header/billing boundary-test approach builds on @KrasimirKralev's prior work. Fixes #94716. Related: #154016. Supersedes the approach in #150790 with credit; that PR remains open for maintainer follow-up. The separate model-allowlist issue #144903 is outside this repair. Co-authored-by: Krasimir Kralev <[email protected]>
|
Thank you for this, @KrasimirKralev — your diagnosis was right and your header/billing-consistency tests shaped the fix. The same defect is now fixed on |
…law#154098) Direct Anthropic OAuth requests could fail with claude_code_version_too_old because they advertised Claude Code 2.1.75. Maintain a 2.1.278 client-version floor and prefer a newer installed stable CLI through the Anthropic plugin's shared, bounded probe. Both transports use one selected version for request headers and billing metadata; failed or slow probes fall back to the floor. Preserve API-key and other provider routes without configuration, credential, dependency, or state migrations. Cover request identity and probe lifecycle, and repair the cold metadata fixture and OS-watch race encountered during validation. Focused tests and exact-head CI passed; candidate live OAuth acceptance remains unverified. Thanks to @Cyb3rb1ade, @raghidtawil-lab, and @davidcittadini for the reports and controlled comparisons. The single-snapshot and header/billing boundary-test approach builds on @KrasimirKralev's prior work. Fixes openclaw#94716. Related: openclaw#154016. Supersedes the approach in openclaw#150790 with credit; that PR remains open for maintainer follow-up. The separate model-allowlist issue openclaw#144903 is outside this repair. Co-authored-by: Krasimir Kralev <[email protected]>
What Problem This Solves
On the subscription (OAuth) path the Anthropic transport presents itself as
claude-cli/<version>with the version pinned in the bundle (ANTHROPIC_CLAUDE_CODE_VERSION = "2.1.75"). Anthropic gates its newest models on that number, so every Fable 5.1 turn on a subscription login fails:The same login works from a Hermes agent on the same machine, because that adapter reports the version of the CLI actually installed there.
mainand the 2026.9.4 release still pin 2.1.75 (canonical issue #94716).Change
@openclaw/aikeeps the pinned value as the fallback and gainssetAnthropicClaudeCodeVersion/getAnthropicClaudeCodeVersion; the billing system block is computed from the current value. The package still spawns nothing.src/llm/ai-transport-host.ts, beside the redactor wiring) asks the installed CLI once per process —claude --version, thenclaude-code --version— through the repository's command runner (runCommandWithTimeout, the same onesrc/agents/workspace.tsuses forgit --version), so npm'sclaude.cmdshim on Windows launches through its trustedcmd.exewrapping. 3 s timeout, 16 KiB output bound, hidden window. The probe settles on the first line the CLI prints and lets the runner stop the command in the background: some installs keep running after printing their version, and waiting for the exit held the identity gate past its bound. It adopts the printed version only when it is newer than the fallback. No CLI, a slow one, an unexpected line, or an older install leaves the fallback exactly as it is today.@openclaw/ai(deferAnthropicClaudeCodeIdentityUntil, bounded at 7 s). In both client builders (providers/anthropic.tscreateClient,transports/anthropic-transport-client.tscreateAnthropicTransportClient) the auth route is decided from the key first; only an OAuth request then doesawait resolveAnthropicClaudeCodeIdentity()as its very first await — before the guarded fetch, the headers or the SDK client are built — and that single snapshot feeds both theuser-agentheader and the billing system block (buildAnthropicSystemBlockstakes the block as a parameter). API-key, Copilot, Foundry, Cloudflare-gateway and injected-SDK-client routes never touch the gate. A probe that fails or never settles leaves the fallback and never fails a request.anthropic-transport-stream.tsinto the siblinganthropic-transport-client.ts(behavior-preserving; the stream module was over the repository's line cap and may not grow). The new request-boundary tests live in sibling*.claude-code-identity.test.tsfiles for the same reason; the two existing test files are byte-identical tomain.@openclaw/aipackage still performs no process spawning; the pinned constant is unchanged.Evidence
Real environment: Ubuntu 24.04 x64, Node 24.21, this branch at
b26ed3f03f(the current head), a real Anthropic subscription OAuth profile (sk-ant-oat…, redacted), the real host wiring (src/llm/ai-transport-host.ts— its probe and identity registration, the realbuildModelFetch), the production Anthropic Messages transport from@openclaw/ai/transports, livehttps://api.anthropic.com/v1/messages. The only variable between the two arms is whether the installed CLI is reachable onPATH. Requests targetanthropic/claude-fable-5-1, the gated model.Exact command run at this head (each arm), from the repo root:
Before (installed CLI hidden from
PATH→ the pinned fallback, i.e. whatmainsends):After (installed CLI on
PATH→ detected, awaited and reported; identity settled before the request was built):Observed result after fix: running
nodeagainst the live API through the direct OAuth transport, the request carries the detected version in both theuser-agentheader and the billing block (same number, one snapshot), and the gated model answers with HTTP 200 where the pinned identity was refused withclaude_code_version_too_old. The identity was resolved before client construction in a fresh process whose probe was started at import time.Why the proof was re-run on this head: an earlier head's probe waited for the CLI to exit and silently kept the fallback on this machine (the CLI lingered after printing), and the identity await sat behind the host's first-use fetch-builder work, which outlasted the gate. Both were caught by re-running this proof, not by the unit tests, and are fixed here (first-line settle; identity awaited before any other request work).
What was not tested: an upgrade in place (installing a newer CLI while a Gateway is already running — by design the version is probed once per process, so that takes a Gateway restart); the Windows
.cmdlaunch itself (Linux host; the runner'scmd.exewrapping is covered bysrc/process/exec.windows.test.ts, and the probe's use of the runner bysrc/llm/anthropic-claude-code-version.test.ts); a probe that never prints anything for the full 7 s bound (covered by the timeout path in the identity gate rather than a live run).Proof limitations: the proof script imports the same transport and host modules the Gateway loads, but drives them directly rather than through a full Gateway boot; the token is read from a 0600 file and never printed.
Tests
packages/ai/src/providers/anthropic-claude-code-identity.test.ts(new): the identity gate — resolves immediately with the fallback when nothing is registered; waits for registered work and reports the adopted version; proceeds with what is known when the bound elapses; a failed probe means nothing adopted, never a failed request; user-agent and billing block derive from one read.packages/ai/src/providers/anthropic.claude-code-identity.test.tsandpackages/ai/src/transports/anthropic-transport-stream.claude-code-identity.test.ts(new, request boundary): with a probe that has not answered, an OAuth request is not sent until it settles and then carries the adopted version in theuser-agentheader and the billing block; an API-key request is sent immediately and carries neither; the identity is resolved before the fetch builder runs.src/llm/anthropic-claude-code-version.test.ts: adopt newer, refuse older/equal/malformed, keep the newest seen, parse the CLI's2.1.273 (Claude Code)line, fall through to the second command name, survive a throwing probe, probe once per process, makeresolveAnthropicClaudeCodeIdentitywait for the probe, route the default probe through the shared runner, settle on the first printed line without waiting for a lingering CLI, report nothing when the launch fails.anthropic.test.ts,anthropic-transport-stream.test.ts,anthropic-payload-policy.test.tsunchanged and green: 284 tests across the seven files above.tsgo:core,oxlint,oxfmt --check,git diff --check, the line-cap / max-lines / assertion-safety ratchets againstmain, and the production knip scan pass on the changed files.