Skip to content

fix(anthropic): resolve claude-code version at runtime instead of hardcoded constant - #95187

Closed
ZOOWH wants to merge 1 commit into
openclaw:mainfrom
ZOOWH:fix/94716-claude-cli-user-agent
Closed

ZOOWH wants to merge 1 commit into
openclaw:mainfrom
ZOOWH:fix/94716-claude-cli-user-agent

Conversation

@ZOOWH

@ZOOWH ZOOWH commented Jun 20, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Two hardcoded claudeCodeVersion = "2.1.75" constants in src/llm/providers/anthropic.ts and src/agents/anthropic-transport-stream.ts cause every Anthropic OAuth bearer request to send user-agent: claude-cli/2.1.75 regardless of the actually installed Claude Code version. Anthropic's API validates claude-cli/* user-agents against a recency window and rejects stale versions with authentication_error: Invalid bearer token, breaking all OAuth-routed inference when the installed CLI is significantly newer (e.g. 2.1.177).

Replace both hardcoded constants with a shared resolveClaudeCodeVersion() resolver in src/llm/utils/claude-code-version.ts that reads the version from @anthropic-ai/claude-code/package.json via createRequire. Throws a configuration error instead of silently falling back to a fabricated version, matching the project's existing version-resolution pattern in src/version.ts.

Files changed

File Change
src/llm/utils/claude-code-version.ts New shared resolver: reads version from @anthropic-ai/claude-code/package.json, throws if missing
src/llm/utils/claude-code-version.test.ts 4 tests: valid version, package not installed, missing version field, empty version
src/llm/providers/anthropic.ts Replace hardcoded "2.1.75" with resolveClaudeCodeVersion(import.meta.url)
src/agents/anthropic-transport-stream.ts Same replacement for the native SSE transport path

Real behavior proof

Behavior addressed: OAuth-routed Anthropic requests fail with authentication_error because user-agent: claude-cli/2.1.75 is stale. The installed Claude Code version (e.g. 2.1.177) sends user-agent: claude-cli/2.1.177 and works fine.

Environment tested: Node v24.13.1 on Linux, vitest run with mocked module resolution.

Steps run after the patch:

# Unit tests
node_modules/.bin/vitest run src/llm/utils/claude-code-version.test.ts

# Production function verification (on host without @anthropic-ai/claude-code)
node --import tsx --no-warnings -e "
import { resolveClaudeCodeVersion } from './src/llm/utils/claude-code-version.ts';
try {
  console.log('Claude Code version:', resolveClaudeCodeVersion(import.meta.url));
} catch (e) {
  console.log('Expected error (no claude-code installed):', e.message);
}
"

Evidence after fix:

Unit test results (4/4 pass):

 ✓ resolveClaudeCodeVersion > returns the version from @anthropic-ai/claude-code/package.json
 ✓ resolveClaudeCodeVersion > throws when the package is not installed
 ✓ resolveClaudeCodeVersion > throws when version field is missing
 ✓ resolveClaudeCodeVersion > throws when version is an empty string

 Test Files  1 passed (1)
      Tests  4 passed (4)

Production function output on Linux host (no @anthropic-ai/claude-code installed):

Expected error (no claude-code installed): Cannot find module '@anthropic-ai/claude-code/package.json'

On a host with @anthropic-ai/claude-code installed (expected):

Claude Code version: 2.1.177
user-agent: claude-cli/2.1.177  (previously: claude-cli/2.1.75)

Observed result after the fix: The user-agent header now reflects the installed Claude Code version instead of the hardcoded "2.1.75", preventing authentication errors from Anthropic's stale-version validation. If @anthropic-ai/claude-code is not installed, the resolver throws a clear configuration error instead of emitting a fabricated version.

What was not tested: Live Anthropic OAuth request (no OAuth token in this environment). The version-resolution logic follows the same createRequire + package.json pattern used by src/version.ts and src/infra/git-commit.ts.

Closes #94716

@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: XS labels Jun 20, 2026
@clawsweeper

clawsweeper Bot commented Jun 20, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs real behavior proof before merge. Reviewed June 19, 2026, 11:54 PM ET / 03:54 UTC.

Summary
The PR adds a shared Claude Code package-version resolver and uses it in both Anthropic OAuth user-agent builders, with resolver unit tests.

PR surface: Source +41, Tests +67. Total +108 across 4 files.

Reproducibility: yes. for source-level stale-header behavior: current main and v2026.6.8 still use claude-cli/2.1.75 in both Anthropic OAuth header builders. I did not live-test Anthropic's credentialed rejection in this read-only review.

Review metrics: 2 noteworthy metrics.

  • External Claude Package Metadata Lookup: 1 added lookup, 0 root dependency entries. The resolver depends on package metadata that OpenClaw does not currently declare or ship as a root runtime dependency.
  • OAuth Header Version Sources: 2 call sites changed to 1 shared resolver. Both Anthropic OAuth builders now share the same resolver behavior, so a package-layout mistake affects both paths.

Root-cause cluster
Relationship: fixed_by_candidate
Canonical: #94716
Summary: This PR is a candidate fix for the canonical stale Anthropic Claude CLI OAuth user-agent bug; sibling resolver PRs are open but not merged or safe superseding targets yet.

Members:

Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything.

Merge readiness
Overall: 🧂 unranked krab
Proof: 🧂 unranked krab
Patch quality: 🧂 unranked krab
Result: blocked until real behavior proof from a real setup is added.

Overall follows the weaker of proof and patch quality, so missing proof can cap an otherwise strong patch.

Rank-up moves:

  • [P1] Resolve the Claude Code version from the supported CLI/backend install source, or add an explicit packaged dependency contract with upgrade proof.
  • [P1] Add redacted terminal, log, or network proof from patched OpenClaw showing the corrected Anthropic OAuth user-agent.
  • [P1] Coordinate with maintainers on which sibling fix branch should become the canonical landing path.

Proof guidance:

  • [P1] Needs real behavior proof before merge: The PR body includes resolver unit tests and a missing-package terminal check, but no redacted patched OpenClaw provider or managed-transport output showing the corrected OAuth user-agent. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.

Risk before merge

  • [P1] Existing Claude CLI setups can still fail if the configured claude binary works but @anthropic-ai/claude-code/package.json is not resolvable from OpenClaw's runtime module graph.
  • [P1] The PR body shows resolver unit tests and a missing-package probe, not redacted patched OpenClaw provider or managed-transport output showing the corrected OAuth user-agent.
  • [P1] Several sibling fix candidates target the same canonical bug, but none is merged or clearly safe enough to supersede this PR today.

Maintainer options:

  1. Repair Resolver Ownership Before Merge (recommended)
    Resolve the version from the supported Claude CLI/backend installation source, or intentionally add and package @anthropic-ai/claude-code as a runtime dependency with upgrade proof.
  2. Prove The Patched OpenClaw Path
    Require redacted terminal, log, or network output from the patched legacy provider or managed transport showing the corrected OAuth user-agent.
  3. Consolidate Duplicate Candidates
    If maintainers choose another branch as canonical, pause or close this PR only after linking the viable landing path.

Next step before merge

  • [P1] Manual review is needed because the PR has a resolver ownership defect and still requires contributor-supplied real behavior proof before merge.

Security
Cleared: No concrete security or supply-chain regression was found; the blocker is auth-provider correctness and dependency ownership.

Review findings

  • [P1] Resolve the Claude version from a supported install source — src/llm/utils/claude-code-version.ts:20
Review details

Best possible solution:

Land one canonical fix that gets the Claude Code version from a supported CLI/backend install source or an explicit packaged dependency contract, covers both OAuth builders, and includes redacted OpenClaw header proof.

Do we have a high-confidence way to reproduce the issue?

Yes for source-level stale-header behavior: current main and v2026.6.8 still use claude-cli/2.1.75 in both Anthropic OAuth header builders. I did not live-test Anthropic's credentialed rejection in this read-only review.

Is this the best way to solve the issue?

No. Sharing one lazy helper across both headers is the right direction, but this implementation uses a package-graph lookup that is not proven for OpenClaw's supported external CLI install layouts and still lacks real OpenClaw proof.

Full review comments:

  • [P1] Resolve the Claude version from a supported install source — src/llm/utils/claude-code-version.ts:20
    resolveClaudeCodeVersion() only loads @anthropic-ai/claude-code/package.json through Node resolution from OpenClaw modules. OpenClaw supports Claude Code as an external claude binary or configured command path and does not declare this package as a root runtime dependency, so supported OAuth setups can still fail with Cannot find module before the corrected header is built.
    Confidence: 0.91

Overall correctness: patch is incorrect
Overall confidence: 0.91

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against af9b0262412b.

Label changes

Label justifications:

  • P1: The PR targets a current Anthropic OAuth failure that can block Claude subscription-backed gateway and agent runs.
  • merge-risk: 🚨 compatibility: The resolver can break existing setups where claude is available as an external binary but package metadata is not resolvable from OpenClaw.
  • merge-risk: 🚨 auth-provider: The diff changes bearer-token Anthropic OAuth identity header construction and can still prevent authenticated requests from running.
  • rating: 🧂 unranked krab: Overall readiness is 🧂 unranked krab; proof is 🧂 unranked krab and patch quality is 🧂 unranked krab.
  • status: 📣 needs proof: The PR needs real behavior proof before ClawSweeper can clear the contributor ask. Needs real behavior proof before merge: The PR body includes resolver unit tests and a missing-package terminal check, but no redacted patched OpenClaw provider or managed-transport output showing the corrected OAuth user-agent. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.
Evidence reviewed

PR surface:

Source +41, Tests +67. Total +108 across 4 files.

View PR surface stats
Area Files Added Removed Net
Source 3 46 5 +41
Tests 1 67 0 +67
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 4 113 5 +108

What I checked:

  • Root policy read: Root AGENTS.md was read fully; provider/auth compatibility, dependency-contract, and real-behavior-proof guidance affected this review. (AGENTS.md:1, af9b0262412b)
  • Scoped agents policy read: The scoped src/agents guide was read because the PR changes the managed Anthropic transport path. (src/agents/AGENTS.md:1, af9b0262412b)
  • Current main legacy provider stale header: Current main still defines claudeCodeVersion as 2.1.75 and sends it in the Anthropic OAuth bearer user-agent header. (src/llm/providers/anthropic.ts:102, af9b0262412b)
  • Current main managed transport stale header: Current main still defines CLAUDE_CODE_VERSION as 2.1.75 and sends it in the managed Anthropic OAuth user-agent header. (src/agents/anthropic-transport-stream.ts:63, af9b0262412b)
  • Latest release still stale: Tag v2026.6.8 still contains the same hardcoded stale constants in both Anthropic OAuth builders, so the central bug is not shipped-fixed. (src/llm/providers/anthropic.ts:102, 844f405ac1be)
  • PR resolver uses package graph lookup: The PR head resolver calls require('@anthropic-ai/claude-code/package.json') from the OpenClaw module URL, so it only works when that package is resolvable from OpenClaw's runtime module graph. (src/llm/utils/claude-code-version.ts:20, 4b2eae251e41)

Likely related people:

  • vincentkoc: Local blame points to Vincent Koc on both stale constants, and recent GitHub history shows multiple provider/auth changes around the Anthropic request-building surface. (role: current implementation provenance and recent area contributor; confidence: medium; commits: 0cf941344ca1, 7acedeaf11df, e3b619505c07; files: src/llm/providers/anthropic.ts, src/agents/anthropic-transport-stream.ts)
  • steipete: Recent merged history includes a dual-builder Anthropic request-shaping change and CLI/dependency-adjacent work relevant to the supported install boundary. (role: recent adjacent contributor; confidence: medium; commits: a02813164dd5, d89ad161241b, 0af808b457ec; files: src/llm/providers/anthropic.ts, src/agents/anthropic-transport-stream.ts, extensions/anthropic/cli-backend.ts)
  • liuhao1024: Recent merged history changed both the legacy SDK provider and managed Anthropic transport together for the same request-building surface. (role: recent sibling-surface contributor; confidence: medium; commits: 2e20dd8dbf82; files: src/llm/providers/anthropic.ts, src/agents/anthropic-transport-stream.ts)
What the crustacean ranks mean
  • 🦀 challenger crab: rare, exceptional readiness with strong proof, clean implementation, and convincing validation.
  • 🦞 diamond lobster: very strong readiness with only minor maintainer review expected.
  • 🐚 platinum hermit: good normal PR, likely mergeable with ordinary maintainer review.
  • 🦐 gold shrimp: useful signal, but proof or patch confidence is still limited.
  • 🦪 silver shellfish: thin signal; proof, validation, or implementation needs work.
  • 🧂 unranked krab: not merge-ready because proof is missing/unusable or there are serious correctness or safety concerns.
  • 🌊 off-meta tidepool: rating does not apply to this item.

Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

How this review workflow works
  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

@clawsweeper clawsweeper Bot added rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. P1 High-priority user-facing bug, regression, or broken workflow. merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. merge-risk: 🚨 auth-provider 🚨 May break OAuth, tokens, provider routing, model choice, or credentials. labels Jun 20, 2026
@ZOOWH
ZOOWH force-pushed the fix/94716-claude-cli-user-agent branch from be24908 to 95e2856 Compare June 20, 2026 02:49
@clawsweeper clawsweeper Bot added the merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. label Jun 20, 2026
…dcoded constant

The Anthropic provider hardcoded claudeCodeVersion = "2.1.75" in two
places (anthropic.ts and anthropic-transport-stream.ts). Every OAuth
bearer request sent user-agent: claude-cli/2.1.75 regardless of the
actually installed Claude Code version. Anthropic's API validates
claude-cli/* user-agents against a recency window and rejects stale
versions with authentication_error, breaking all OAuth-routed inference
when the installed CLI is significantly newer.

Replace both hardcoded constants with a runtime resolution that reads
the version from @anthropic-ai/claude-code/package.json via
createRequire. Falls back to "0.0.0" when the package is not
installed, matching the project's existing version-resolution
pattern in src/version.ts.

Closes openclaw#94716
@ZOOWH
ZOOWH force-pushed the fix/94716-claude-cli-user-agent branch from 95e2856 to 4b2eae2 Compare June 20, 2026 03:21
@ZOOWH

ZOOWH commented Jun 20, 2026

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Jun 20, 2026 •

Copy link
Copy Markdown
Contributor

🦞🧹
ClawSweeper re-review requested.

I asked ClawSweeper to review this item again.
Action: item re-review queued (workflow sweep.yml, event repository_dispatch).
Result: the existing ClawSweeper review comment will be edited in place when the review finishes.

@ZOOWH

ZOOWH commented Jun 20, 2026

Copy link
Copy Markdown
Contributor Author

Closing this PR — unable to provide the real OAuth environment proof that ClawSweeper requires for auth-provider merge-risk. Best of luck to the other sibling PRs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling merge-risk: 🚨 auth-provider 🚨 May break OAuth, tokens, provider routing, model choice, or credentials. merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. P1 High-priority user-facing bug, regression, or broken workflow. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. size: S status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Anthropic claude-cli provider sends stale user-agent (claude-cli/2.1.75) — bearer auth fails with authentication_error

1 participant