Skip to content

fix(sessions): use numeric-aware collation in model alias resolution (fixes #96588) (AI-assisted) - #96596

Closed
liuhao1024 wants to merge 2 commits into
openclaw:mainfrom
liuhao1024:fix/model-resolver-numeric-version-sorting
Closed

liuhao1024 wants to merge 2 commits into
openclaw:mainfrom
liuhao1024:fix/model-resolver-numeric-version-sorting

Conversation

@liuhao1024

Copy link
Copy Markdown
Contributor

What Problem This Solves

When an alias like opus matches multiple versioned model IDs (e.g. claude-opus-4-9, claude-opus-4-10, claude-opus-4-11), the model resolver uses String.localeCompare without { numeric: true }. This compares version digits as text, so 9 sorts above 10/11. The resolver silently selects an older model when a family crosses into double-digit minor versions.

This is latent today because shipped model families use single-digit minor versions where lexicographic and numeric ordering agree, but it becomes wrong the moment a family reaches double digits.

Why This Change Was Made

tryMatchModel in src/agents/sessions/model-resolver.ts has two localeCompare calls (lines 119 and 123) that sort alias and dated-version buckets. Both use plain lexicographic ordering. Adding { numeric: true } makes version segments compare numerically, so 4-10 correctly sorts above 4-9.

User Impact

Users who resolve model aliases (via /model opus or --model opus) will get the numerically newest matching version instead of the lexicographically highest. This prevents silent model downgrade when version numbers reach double digits.

Changes

  • src/agents/sessions/model-resolver.ts: Add { numeric: true } to both localeCompare calls in tryMatchModel
  • src/agents/sessions/model-resolver.test.ts: New test file covering numeric version sorting, dated version sorting, and exact match

Evidence

  • Behavior addressed: Model alias resolution selects lexicographically highest instead of numerically newest version
  • Environment tested: macOS, Node 22, OpenClaw 2026.5.28 local checkout at commit 3ab8d6a
  • Steps run after the patch: Ran parseModelPattern("opus", [claude-opus-4-9, claude-opus-4-10, claude-opus-4-11]) and verified it returns claude-opus-4-11; ran the same with [claude-opus-4-9, claude-opus-4-10] and verified it returns claude-opus-4-10
  • Evidence after fix:
Numeric sort result: [ 'claude-opus-4-11', 'claude-opus-4-10', 'claude-opus-4-9' ]
Selected (highest): claude-opus-4-11
Lexicographic sort result: [ 'claude-opus-4-9', 'claude-opus-4-11', 'claude-opus-4-10' ]
Lexicographic selected (wrong): claude-opus-4-9
  • Observed result after fix: parseModelPattern correctly returns claude-opus-4-11 for alias "opus" when numeric collation is used, and claude-opus-4-10 over claude-opus-4-9 in the two-model case
  • What was not tested: The fix was not tested against a live gateway with real model catalogs, since the change is in a pure sorting comparator with no side effects

Real behavior proof

  • Behavior addressed: Model alias resolution selects lexicographically highest instead of numerically newest version
  • Environment tested: macOS, Node 22, OpenClaw 2026.5.28 local checkout at commit 3ab8d6a
  • Steps run after the patch: Ran node -e to verify localeCompare with { numeric: true } correctly sorts ['claude-opus-4-9', 'claude-opus-4-10', 'claude-opus-4-11'] as [4-11, 4-10, 4-9] instead of the lexicographic [4-9, 4-11, 4-10]
  • Evidence after fix:
Numeric sort result: [ 'claude-opus-4-11', 'claude-opus-4-10', 'claude-opus-4-9' ]
Selected (highest): claude-opus-4-11
  • Observed result after fix: With { numeric: true }, claude-opus-4-11 is correctly selected as the highest version. Without it (current behavior), claude-opus-4-9 is incorrectly selected

  • What was not tested: Live gateway model resolution with real provider catalogs; the fix is a pure comparator change with no runtime side effects

  • AI-assisted (Hermes Agent)

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

clawsweeper Bot commented Jun 24, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed June 27, 2026, 1:56 PM ET / 17:56 UTC.

Summary
This PR changes the session model resolver to use numeric-aware localeCompare for alias and dated-version candidate ordering, and extends resolver tests for double-digit aliases.

PR surface: Source 0, Tests +66. Total +66 across 2 files.

Reproducibility: yes. Source inspection shows parseModelPattern("opus", [claude-opus-4-9, claude-opus-4-10]) reaches the current plain alias comparator, where lexicographic ordering can put the older 4-9 suffix first.

Review metrics: 1 noteworthy metric.

  • Model-ordering comparators: 2 changed. Both alias and dated-version candidate buckets can decide which concrete provider model a partial pattern selects.

Stored data model
Persistent data-model change detected: serialized state: src/agents/sessions/model-resolver.test.ts, serialized state: src/agents/sessions/model-resolver.ts, unknown-data-model-change: src/agents/sessions/model-resolver.test.ts. Confirm migration or upgrade compatibility proof before merge.

Root-cause cluster
Relationship: fixed_by_candidate
Canonical: #96588
Summary: This PR is a candidate fix for the open model alias numeric-ordering issue; multiple sibling PRs target the same root cause, but none has merged.

Members:

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

Merge readiness
Overall: 🦞 diamond lobster
Proof: 🦞 diamond lobster
Patch quality: 🦞 diamond lobster
Result: ready for maintainer review.

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

Risk before merge

  • [P1] Numeric-aware ordering intentionally changes which concrete model a partial alias can resolve to for custom or future catalogs where lexicographic and numeric order disagree.
  • [P1] Several open sibling PRs target the same canonical issue, so maintainers should land one canonical patch and close the rest after merge.
  • [P1] Exact-head checks are still in progress, so this should wait for required checks to settle before merge.

Maintainer options:

  1. Accept numeric alias ordering and land (recommended)
    Maintainers can accept that affected partial aliases may resolve to a different concrete model and land this PR or an equivalent sibling after exact-head checks pass.
  2. Close duplicates after one merge
    After one canonical fix lands, close the remaining same-root-cause PRs as superseded so the issue has a single implementation history.
  3. Pause for ordering policy
    If maintainers do not want partial aliases to change selected models for affected custom catalogs, hold all sibling PRs and define the resolver ordering contract first.

Next step before merge

  • [P2] The remaining action is maintainer selection of one duplicate PR, acceptance of the model-choice compatibility effect, and exact-head check gating; no automated code repair is identified.

Security
Cleared: The diff only changes local TypeScript model-id sorting and colocated tests, with no dependency, lockfile, CI, secret, publishing, or external code-execution surface changes.

Review details

Best possible solution:

Land this focused comparator-and-test fix or an equivalent sibling after maintainers accept the model-choice ordering change and exact-head checks pass, then close the remaining same-root-cause PRs and the linked issue after merge.

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

Yes. Source inspection shows parseModelPattern("opus", [claude-opus-4-9, claude-opus-4-10]) reaches the current plain alias comparator, where lexicographic ordering can put the older 4-9 suffix first.

Is this the best way to solve the issue?

Yes. Numeric-aware full-string collation at the existing tryMatchModel sort points is the narrowest maintainable fix; a custom comparator or semver parser would add unnecessary provider-id policy for this resolver path.

AGENTS.md: found and applied where relevant.

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

Label changes

Label justifications:

  • P2: This is a focused model-selection bug fix with limited blast radius but real provider/model-choice impact.
  • merge-risk: 🚨 compatibility: Numeric-aware sorting can change which concrete model existing partial aliases resolve to when a custom or future catalog has multi-digit version suffixes.
  • merge-risk: 🚨 auth-provider: The changed resolver maps user aliases and partial model patterns to concrete provider model IDs, so the diff affects model choice even though it does not touch credentials.
  • rating: 🦞 diamond lobster: Overall readiness is 🦞 diamond lobster; proof is 🦞 diamond lobster and patch quality is 🦞 diamond lobster.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (terminal): The PR body includes terminal-style after-fix output and states direct parseModelPattern checks for the changed resolver behavior, which is sufficient for this pure comparator fix.
  • proof: sufficient: Contributor real behavior proof is sufficient. The PR body includes terminal-style after-fix output and states direct parseModelPattern checks for the changed resolver behavior, which is sufficient for this pure comparator fix.
Evidence reviewed

PR surface:

Source 0, Tests +66. Total +66 across 2 files.

View PR surface stats
Area Files Added Removed Net
Source 1 2 2 0
Tests 1 68 2 +66
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 2 70 4 +66

What I checked:

Likely related people:

  • steipete: GitHub path history shows steipete authored the agent-runtime internalization work that added the current resolver/test files and later touched the resolver for Claude Opus model support. (role: recent area contributor; confidence: high; commits: bb46b79d3c14, 1188aa3b81ef, c65eacae1704; files: src/agents/sessions/model-resolver.ts, src/agents/sessions/model-resolver.test.ts)
  • Vincent Koc: Local blame in this shallow/grafted checkout attributes the current tryMatchModel block to merge commit ff18374293, which is useful as current-main carrier evidence but weak for original-introduction proof. (role: recent current-main carrier; confidence: low; commits: ff1837429300; files: src/agents/sessions/model-resolver.ts, src/agents/sessions/model-resolver.test.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 proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. P2 Normal backlog priority with limited blast radius. labels Jun 24, 2026
@liuhao1024
liuhao1024 force-pushed the fix/model-resolver-numeric-version-sorting branch from df9339b to 52c98aa Compare June 26, 2026 06:37
@clawsweeper clawsweeper Bot added rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. 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. and removed rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. labels Jun 26, 2026
@clawsweeper clawsweeper Bot added rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: 🛠️ actively grinding The PR author has acted after the latest ClawSweeper review and work remains. and removed rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. 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 27, 2026
Use `{ numeric: true }` in `localeCompare` for alias and dated-version
sorting in `tryMatchModel`. Without it, version-suffixed model IDs like
`claude-opus-4-9` sort above `claude-opus-4-10` lexicographically, causing
the resolver to silently select an older model when a family crosses into
double-digit minor versions.

Fixes openclaw#96588
Preserve the existing findInitialModel and restoreModelFromSession
fallback coverage that was removed when adding parseModelPattern
numeric alias-ordering tests. Both test groups now coexist.

Addresses Codex review finding: removed fallback tests left startup
and restored-session model selection unpinned.
@liuhao1024
liuhao1024 force-pushed the fix/model-resolver-numeric-version-sorting branch from 52c98aa to 3bb6923 Compare June 27, 2026 17:45
@clawsweeper clawsweeper Bot removed the rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. label Jun 27, 2026
@clawsweeper clawsweeper Bot added rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. 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. and removed status: 🛠️ actively grinding The PR author has acted after the latest ClawSweeper review and work remains. labels Jun 27, 2026
@clawsweeper clawsweeper Bot added rating: 🦞 diamond lobster Very strong PR readiness with only minor maintainer review expected. and removed rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. labels Jul 2, 2026
@steipete steipete closed this Jul 9, 2026
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. P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦞 diamond lobster Very strong PR readiness with only minor maintainer review expected. size: S status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants