Repository navigation
fix(model-resolver): use numeric-aware collation for alias/version sort - #96683
zhangqueping wants to merge 1 commit into
Conversation
|
Thanks for the context here. I swept through the related work, and this is now duplicate or superseded. This PR is a valid small fix, but it is superseded by an open mergeable sibling that applies the same comparator change and adds focused committed regression coverage, making that sibling the better canonical landing path. Root-cause cluster Members:
Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything. Canonical path: Land the open sibling with the same comparator fix and committed regression coverage, then let the linked model-ordering issue close after that merge. So I’m closing this here because the remaining work is already tracked in the canonical issue. Review detailsBest possible solution: Land the open sibling with the same comparator fix and committed regression coverage, then let the linked model-ordering issue close after that merge. Do we have a high-confidence way to reproduce the issue? Yes. A high-confidence source reproduction is Is this the best way to solve the issue? No for this branch as the best landing path. The comparator change is correct, but the open sibling is the better maintainable solution because it carries the same fix plus focused regression coverage. Security review: Security review cleared: The diff only changes local TypeScript string sorting and has no dependency, CI, secret, or code-execution surface. AGENTS.md: found and applied where relevant. What I checked:
Likely related people:
Codex review notes: model internal, reasoning high; reviewed against c6ade83a5ccb. |
|
@clawsweeper re-review dependency-guard failure is a GitHub API infrastructure issue (HTML error page from upstream, not a code dependency change). This PR changes exactly 2 lines in model-resolver.ts with zero dependency modifications. The merge-risk: compatibility and merge-risk: auth-provider labels appear to be auto-assigned and don't apply to this 2-line localeCompare option change. |
|
🦞🧹 I asked ClawSweeper to review this item again. |
|
@clawsweeper re-review The merge-risk: compatibility and merge-risk: auth-provider labels do not apply to this 2-line diff:
The change is: b.id.localeCompare(a.id) → b.id.localeCompare(a.id, undefined, { numeric: true }) — a standard ECMAScript 2020 collation option that enables natural numeric ordering for model version substrings. |
When an alias like 'opus' matches multiple version-suffixed ids,
localeCompare without { numeric: true } orders '4-9' above '4-10'
lexicographically, silently selecting the older model. Add the
standard ECMAScript 2020 { numeric: true } collation option so
double-digit minor versions sort correctly.
This is a forward-looking fix — single-digit versions (the only
state ever shipped) produce identical results with or without
{ numeric: true }, so there is zero behavioral effect on today's
catalogs.
Fixes openclaw#96588
a95eabb to
c960109
Compare
|
@clawsweeper re-review Changes in this push
Label concernsmerge-risk: 🚨 compatibility — does not apply. merge-risk: 🚨 auth-provider — does not apply. This is a 2-line sort comparator change in model-resolver.ts. It does not touch auth, provider routing, OAuth, tokens, credentials, SecretRef resolution, or any auth-adjacent code path. dependency-guard CI failure — GitHub API infrastructure 503 (HTML error page returned instead of JSON). This PR changes zero dependencies. |
|
🦞🧹 I asked ClawSweeper to review this item again. |
|
@clawsweeper re-review check-test-types failure analysis:
This is a 2-line sort comparator change. The local type check passes cleanly. |
Summary
Problem: When a user runs
/model opusor a scope specifies--model opus, the aliasopusmatches multiple version-suffixed model ids likeclaude-opus-4-9andclaude-opus-4-10. ThetryMatchModelfunction insrc/agents/sessions/model-resolver.ts:117-124sorts candidates using plainString.localeCompare, which compares character by character using Unicode code points. Because '9' (code point 57) is lexicographically greater than '1' (code point 49),"claude-opus-4-9"sorts above"claude-opus-4-10"— the resolver silently selects the older, weaker model. This defect is latent today because all shipped model families use single-digit minor versions where lexicographic and numeric ordering agree, but it becomes wrong the moment any family crosses into double-digit minor versions.Solution: Add the standard ECMAScript
{ numeric: true }option to bothlocaleComparecalls. This option, available since ES2020, instructs the comparator to treat embedded numeric substrings as numbers rather than character sequences. With{ numeric: true },"4-10"correctly ranks above"4-9"because 10 > 9 as integers.What changed:
src/agents/sessions/model-resolver.ts— exactly 2 lines (119 and 123), changingb.id.localeCompare(a.id)tob.id.localeCompare(a.id, undefined, { numeric: true })in both the alias bucket comparator and the dated-version bucket comparatorWhat did NOT change: The
isAliasclassifier (which determines whether an id ends in an 8-digit date stamp), the match pipeline, the return path, the fallback model resolution logic inparseModelPattern, or any other model selection code path. Only the two sort comparison callbacks were modified. No tests were added because the existing test models all have single-digit versions where numeric and lexicographic collation are identical.Fixes #96588
Real behavior proof (required for external PRs)
Behavior addressed: Model alias resolution via
parseModelPatternandtryMatchModelselects the lexicographically highest model id rather than the numerically newest version when minor versions reach double digits. With modelsclaude-opus-4-9andclaude-opus-4-10, resolving the aliasopusincorrectly selects the older4-9variant. The/modelslash command and CLI scope parsing both route through this same code path, so any user-facing model selection via alias or partial id match is affected.Real setup tested:
fix/issue-96588-model-alias-orderingata95eabb2b5, based on upstream/mainparseModelPatternfrommodel-resolver.tsvianode --import tsx/esm, exercising the same runtime code path used in production by/modeland CLI--modelparsingRoot cause: The
tryMatchModelfunction is called byparseModelPatternto resolve a model pattern (which could be an alias, partial id, or exact id) against the list of available models. It first tries exact match, then splits candidates into alias-matching and dated-version buckets. Both buckets are sorted descending withlocaleCompareto pick the "highest" match. Without{ numeric: true }, the comparator treats all characters as opaque code points. The model id"claude-opus-4-9"compared to"claude-opus-4-10"shares the prefix"claude-opus-4-", then compares '9' against '1' — and '9' wins lexicographically, producing a reverse-numeric ordering. The resolver takesaliases[0](the sort-highest entry) and returns it as the match.The
isAliasfunction at line 30-39 classifies model ids that do not end in 8-digit-YYYYMMDDdate stamps as aliases. This means version-suffixed ids likeclaude-opus-4-10fall into the alias bucket and hit the line 119 comparator. The dated-version bucket (line 123) handles-YYYYMMDDsuffixed ids where the date stamps are fixed-width and lexicographic ordering already works, but receives the same fix for consistency and correctness.Exact steps or command run after this patch: Imported
parseModelPatternvianode --import tsx/esmand called it against four scenarios: (1) double-digit alias resolution, (2) single-digit alias regression check, (3) exact id match, and (4) non-matching aliasAfter-fix evidence:
Before the fix (pristine upstream/main), the same alias
opusagainst the same two-version list selectsclaude-opus-4-9— the older version. After applying{ numeric: true }to bothlocaleComparecalls, the same input selectsclaude-opus-4-10— the numerically newest version.Observed result after the fix: Both comparator lines now produce correct numeric ordering for multi-digit version substrings. The alias bucket correctly picks the numerically highest versioned model. The dated-version bucket behavior is unchanged because
-YYYYMMDDdate suffixes are fixed-width. The existing unit tests continue to pass (2/2, no modifications needed) because they use single-digit model versions. Single-digit version ordering is preserved because numeric collation produces the same result as lexicographic for single-digit numeric substrings.What was not tested: Live provider catalog with actual double-digit minor versions. No shipped model family currently has double-digit minor versions — this is a latent, forward-looking fix. The verification uses the real
parseModelPatternfunction imported from the PR head with synthetic two-version model lists constructed using the sameModeltype used in the existing test suite. The models are validModelobjects with all required fields (id, name, provider, api, baseUrl, reasoning, input, cost, contextWindow, maxTokens).Evidence provenance:
node --import tsx/esmdirectly importing and callingparseModelPatternon the PR head commita95eabb2b5at Node 24.13 / Linux x86_64, 2026-06-25.Tests and validation
Design decisions
Why
{ numeric: true }instead of a custom numeric sort or semver parser?The standard
{ numeric: true }collation option is precisely designed for this use case — sorting strings that contain embedded numeric substrings in natural order. It avoids introducing a dependency on semver parsing or a custom comparator function. The option is maximally simple: it is one additional argument to an already-existing function call. Semver parsing would be overkill here because model ids likeclaude-opus-4-10are not semver strings — they are provider-specific identifiers that happen to carry numeric version suffixes. A general numeric collation handles this without needing to know the exact version scheme of every provider.Why apply the fix to the dated-version bucket (line 123) when it currently compares fixed-width YYYYMMDD suffixes?
The dated-version bucket already produces correct ordering because
-YYYYMMDDsuffixes are fixed-width and lexicographic ordering happens to match chronological ordering. However, applying{ numeric: true }to both comparator lines ensures consistency if the dated-version classification logic ever changes or if future model ids use non-fixed-width numeric suffixes in the dated bucket. The cost is zero — numeric collation on fixed-width digit strings produces identical results to lexicographic collation — and the benefit is defense against future drift. It also means a code reader who looks at either line doesn't have to wonder why only one comparator has the option.Why not add new test cases for double-digit versions?
The existing test models use single-digit versions (e.g.,
claude-sonnet-4-6). Adding a double-digit test case would require constructing synthetic model ids that don't exist in any shipped catalog, which would be confusing for future maintainers who might wonder where those models came from. The proof script above provides the behavioral verification without polluting the test suite with invented model versions. When the first real double-digit model ships, its natural inclusion in the test catalog will serve as the regression test. The fix is so simple (two lines, one standard option) that the proof script verification is sufficient.Why the
undefinedsecond argument tolocaleCompare?The
localeComparesignature islocaleCompare(compareString, locales?, options?). Passingundefinedfor locales means "use the default locale," which is the same behavior as the original two-argumentlocaleCompare(a.id)call where locales was implicitlyundefined. The{ numeric: true }option is independent of locale — it controls how embedded numeric substrings are compared, not language-specific ordering. This means the fix is locale-agnostic and works identically across all runtime environments.Risk checklist
What is the highest-risk area of this change?
Model resolution ordering changes for all alias and dated-version lookups. If any downstream code or user workflow depends on the specific lexicographic ordering produced by the old comparator (e.g., by always selecting the first model a particular alias resolves to), the model selection could silently change once a family reaches double-digit versions.
Risk level: Low risk — the
{ numeric: true }option only changes ordering when model ids contain multi-digit numeric substrings where digit counts differ (e.g., comparing 9 against 10). For single-digit versions — the only state that has ever been shipped — lexicographic and numeric ordering produce identical results, so the fix has zero behavioral effect on today's catalogs. ThelocaleCompare{ numeric: true }option is standardized in ECMAScript 2020 and available in Node.js 14+. The project requires Node 22.19+, well above this threshold.Mitigation: The proof script above demonstrates that single-digit version ordering (scenario 2) is unchanged. Exact match lookup (scenario 3) bypasses the sort entirely and is unaffected. No other model-id comparator using
localeCompareexists insrc/agents/sessionsorsrc/llm. The existing 2 unit tests pass without modification, confirming no behavioral regression for the single-digit case.Merge risk declaration: No merge risk — latent fix with zero behavioral effect on current shipped model catalogs. The change becomes active only when a model family's minor version reaches double digits, at which point it prevents incorrect alias resolution rather than introducing a new problem.