fix: use provider-qualified key in MODEL_CACHE for context window lookup#15632
fix: use provider-qualified key in MODEL_CACHE for context window lookup#15632linwebs wants to merge 2 commits intoopenclaw:mainfrom
Conversation
Additional Comments (1)
Prompt To Fix With AIThis is a comment left during a code review.
Path: src/auto-reply/reply/followup-runner.ts
Line: 198:216
Comment:
**Persisted provider mismatch**
`providerUsed` is computed from `runResult.meta.agentMeta?.provider ?? fallbackProvider` and used to compute `contextTokensUsed`, but `persistRunSessionUsage` is called with `providerUsed: fallbackProvider` (not the `providerUsed` variable). If the agent meta provider differs from the fallback provider, the session store will persist `modelProvider` for one provider while `contextTokensUsed` came from another, which can reintroduce incorrect context-window resolution on subsequent `/status`/session views.
```suggestion
await persistRunSessionUsage({
storePath,
sessionKey,
usage,
lastCallUsage: runResult.meta.agentMeta?.lastCallUsage,
promptTokens,
modelUsed,
providerUsed,
contextTokensUsed,
logLabel: "followup",
});
```
How can I resolve this? If you propose a fix, please make it concise. |
…kup (cherry-pick PR openclaw#15632)
bfc1ccb to
f92900f
Compare
|
This pull request has been automatically marked as stale due to inactivity. |
|
This pull request has been automatically marked as stale due to inactivity. |
…ll sites applyConfiguredContextWindows now stores provider-qualified keys (e.g. "rdsec/claude-4.6-sonnet") in MODEL_CACHE alongside the bare model id fallback, using normalizeProviderId so the key format is consistent with resolveContextTokensForModel's lookup logic. All call sites that have provider info now prefer provider-qualified lookups: - agent-runner, followup-runner, directive-handling.persist: switched to resolveContextTokensForModel (direct config scan + qualified cache) - cron/isolated-agent/run, gateway/session-utils, commands/sessions: same migration to resolveContextTokensForModel - resolveContextTokens (model-selection), resolveMemoryFlushContextWindowTokens (memory-flush): added optional provider param with normalizeProviderId-based qualified lookup before bare id fallback (no cfg available at these sites) - get-reply-directives, agent-runner-memory: pass provider through to the above utilities Matches the provider-aware approach already applied to session-store.ts and status.ts in upstream. Fixes incorrect context window display and compaction timing when the same model id is configured across multiple providers with different context limits.
9f4f541 to
f5dcd24
Compare
applyConfiguredContextWindows now writes only bare model-id keys to MODEL_CACHE, reserving the provider-qualified key space exclusively for raw discovery entries (e.g. OpenRouter slash-paths). Call sites that need per-provider disambiguation (get-reply-directives, memory-flush, agent-runner-memory) now call resolveContextTokensForModel with the full cfg so they scan models.providers directly, rather than relying on a qualified cache key that was never reliably populated. resolveContextTokens in model-selection.ts reverted to remove the now- unused provider param; resolveMemoryFlushContextWindowTokens accepts cfg and delegates to resolveContextTokensForModel.
f5dcd24 to
80b8160
Compare
|
Revised approach in 80b8160: instead of writing provider-qualified keys into MODEL_CACHE (which corrupted discovery entries for OpenRouter-style slash-path model IDs), call sites that need per-provider disambiguation now call resolveContextTokensForModel with the full cfg and scan models.providers directly. Both previous Greptile review threads are resolved — the issues they flagged no longer exist in the new code. CI failure in src/cron/isolated-agent.delivers-response-has-heartbeat-ok-but-includes.test.ts is pre-existing in upstream main and unrelated to this PR. @greptileai review |
Greptile SummaryThis PR fixes a multi-provider context-window collision bug where two providers sharing the same model ID (e.g.
Confidence Score: 4/5
|
|
This pull request has been automatically marked as stale due to inactivity. |
|
Closing due to inactivity. |
Summary
/statusand compaction logic show/use the wrong context window for the non-last-loaded provider.provider-1/claude-4.6-sonnet(200k) andprovider-2/claude-4.6-sonnet(1M) would see both show1.0m, causing premature compaction on the 1M provider and incorrect status display on the 200k one.applyConfiguredContextWindowsnow writes only bare model-id keys toMODEL_CACHE. Call sites with provider info (get-reply-directives,memory-flush,agent-runner-memory) now callresolveContextTokensForModelwith the fullcfg, which scansmodels.providersdirectly to disambiguate same-named models across providers.resolveContextTokensinmodel-selection.tsreverted to remove the now-unusedproviderparam.Change Type (select all)
Scope (select all touched areas)
Linked Issue/PR
User-visible / Behavior Changes
/statusnow displays the correct context window when the same model ID exists across multiple providers with different limits.Security Impact (required)
Repro + Verification
Environment
provider-1/claude-4.6-sonnet(200k),provider-2/claude-4.6-sonnet(1M)claude-4.6-sonnetandclaude-4.6-opuswith differentcontextWindowvaluesSteps
contextWindowinopenclaw.json/model provider-1/claude-4.6-sonnet→/status/model provider-2/claude-4.6-sonnet→/statusExpected
provider-1/claude-4.6-sonnet→tokens xx/200kprovider-2/claude-4.6-sonnet→tokens xx/1.0mActual (after fix)
provider-1/claude-4.6-sonnet→tokens 19k/200k (9%)✅provider-2/claude-4.6-sonnet→tokens 18k/1.0m (2%)✅Evidence
Human Verification (required)
claude-4.6-sonnetandclaude-4.6-opustested across two providers via TUI. Context window displayed correctly in status bar for each provider. Messages sent to both providers successfully.MODEL_CACHEbare-key entries for discovery (OpenRouter slash-path IDs) are not affected by the change;resolveContextTokensForModelfalls through to bare cache key when config scan finds no match.contextTokensglobal override; OpenRouter provider with slash-containing model IDs in live environment.Review Conversations
(Both Greptile review threads from the previous commit are resolved. The issues they identified — unguarded
undefined/modellookup and provider mismatch inpersistRunSessionUsage— no longer exist in the current code because the qualified-key-in-cache approach has been replaced entirely.)Compatibility / Migration
Failure Recovery (if this breaks)
git revert f5dcd2409Risks and Mitigations
resolveContextTokensForModelscanscfg.models.providerson every call instead of reading from cache.