refactor(vscode): harden context-usage display with trusted token limits#2875
Conversation
The webview context-usage bar did not clear when the user started a new session because the old code always fell back to DEFAULT_TOKEN_LIMIT, producing a stale percentage even after usageStats and modelInfo were both cleared. Key changes: - Extract `knownTokenLimit()` in core/tokenLimits.ts that returns `undefined` for unrecognized models instead of a default, keeping `tokenLimit()` behavior unchanged. - In acpModelInfo.ts, derive `_meta.contextLimit` from the known-model table when the ACP payload omits a numeric limit. - Extract `computeContextUsage()` into its own module, which returns `null` when no trusted numeric limit is available — the UI then correctly hides the context bar. - Remove the `@qwen-code/qwen-code-core` runtime import from App.tsx so the webview bundle stays free of Node-only dependencies. Closes #2847
📋 Review SummaryThis PR adds defensive hardening for the context-usage display in the VS Code extension, ensuring correct behavior when the ACP server omits 🔍 General Feedback
🎯 Specific Feedback🔵 Low
✅ Highlights
|
Summary
Follow-up to #2874. Adds defensive hardening so the context-usage bar behaves correctly even when the ACP server omits
tokenLimitfrom its responses.knownTokenLimit()incore/tokenLimits.ts: a new export that returnsundefinedfor unrecognized models (unliketokenLimit()which always falls back to a default). This lets callers distinguish "known limit" from "guessed default".acpModelInfo.ts: derives_meta.contextLimitfrom the known-model table when the ACP payload omits a numeric limit, with a clear priority chain (legacy numeric > meta numeric > derived > explicit null > undefined).computeContextUsage(): extracted from the 45-line inline block inApp.tsxinto a standalone tested module. Returnsnullwhen no trusted numeric limit is available — the UI hides the context bar instead of showing a misleading default.@qwen-code/qwen-code-coreruntime import fromApp.tsx, keeping the webview bundle free of Node-only dependencies.Files changed
core/tokenLimits.tsfindTokenLimit()+knownTokenLimit()export, refactortokenLimit()to reuseacpModelInfo.tscontextLimitfrom known model table when ACP omits itcontextUsage.ts(new)App.tsx, no default fallbackApp.tsxcomputeContextUsage()callTest plan
tokenLimits.test.ts—knownTokenLimitreturns values for known models,undefinedfor unknownacpModelInfo.test.ts— derives contextLimit when payload omits it, preserves explicit nullcontextUsage.test.ts— null without trusted limit, prefers usageStats over meta, inputTokens/promptTokens compatimageSupport.bundle.test.ts— App webview bundle has no core runtime importsDepends on #2874