Skip to content

refactor(vscode): harden context-usage display with trusted token limits#2875

Open
yiliang114 wants to merge 1 commit intofix/2847-vscode-new-session-minimalfrom
fix/2847-vscode-new-session-reset-context
Open

refactor(vscode): harden context-usage display with trusted token limits#2875
yiliang114 wants to merge 1 commit intofix/2847-vscode-new-session-minimalfrom
fix/2847-vscode-new-session-reset-context

Conversation

@yiliang114
Copy link
Copy Markdown
Collaborator

Summary

Follow-up to #2874. Adds defensive hardening so the context-usage bar behaves correctly even when the ACP server omits tokenLimit from its responses.

  • knownTokenLimit() in core/tokenLimits.ts: a new export that returns undefined for unrecognized models (unlike tokenLimit() which always falls back to a default). This lets callers distinguish "known limit" from "guessed default".
  • acpModelInfo.ts: derives _meta.contextLimit from 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 in App.tsx into a standalone tested module. Returns null when no trusted numeric limit is available — the UI hides the context bar instead of showing a misleading default.
  • Removes the last @qwen-code/qwen-code-core runtime import from App.tsx, keeping the webview bundle free of Node-only dependencies.

Files changed

File Change
core/tokenLimits.ts Add findTokenLimit() + knownTokenLimit() export, refactor tokenLimit() to reuse
acpModelInfo.ts Derive contextLimit from known model table when ACP omits it
contextUsage.ts (new) Pure function extracted from App.tsx, no default fallback
App.tsx Replace 45-line inline block with computeContextUsage() call
+ 3 test files Cover all new behaviors

Test plan

  • tokenLimits.test.tsknownTokenLimit returns values for known models, undefined for unknown
  • acpModelInfo.test.ts — derives contextLimit when payload omits it, preserves explicit null
  • contextUsage.test.ts — null without trusted limit, prefers usageStats over meta, inputTokens/promptTokens compat
  • imageSupport.bundle.test.ts — App webview bundle has no core runtime imports

Depends on #2874

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
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 4, 2026

📋 Review Summary

This PR adds defensive hardening for the context-usage display in the VS Code extension, ensuring correct behavior when the ACP server omits tokenLimit from responses. The implementation introduces a new knownTokenLimit() function that distinguishes between "known limit" and "guessed default," extracts context usage computation into a testable module, and removes the last runtime dependency on @qwen-code/qwen-code-core from the webview bundle. Overall, this is a well-structured refactoring with comprehensive test coverage.

🔍 General Feedback

  • Strong separation of concerns: The extraction of computeContextUsage() from the 45-line inline block in App.tsx significantly improves testability and maintainability.
  • Clear priority chain: The context limit resolution order (legacy numeric > meta numeric > derived > explicit null > undefined) is well-documented and logically sound.
  • Excellent test coverage: All new functions have corresponding tests covering edge cases, including unknown models, null handling, and fallback behaviors.
  • Clean dependency management: Removing the @qwen-code/qwen-code-core runtime import from App.tsx is a positive architectural improvement for the webview bundle.
  • Consistent patterns: The refactoring of tokenLimit() to reuse knownTokenLimit() follows DRY principles without sacrificing clarity.

🎯 Specific Feedback

🔵 Low

  • File: packages/vscode-ide-companion/src/utils/acpModelInfo.ts:66-73 - The priority chain comment is helpful, but consider extracting the context limit derivation logic into a named helper function (e.g., resolveContextLimit()) to improve readability and make the priority chain self-documenting:

    const contextLimit = resolveContextLimit(legacyLimit, metaLimit, derivedLimit);
  • File: packages/vscode-ide-companion/src/webview/utils/contextUsage.ts:24 - The limit assignment could benefit from an explicit comment about why DEFAULT_TOKEN_LIMIT is intentionally NOT used as a fallback here (unlike in tokenLimit()), since this distinction is the core purpose of this PR.

  • File: packages/core/src/core/tokenLimits.ts:190-200 - The findTokenLimit() function is declared but not exported. Consider adding a JSDoc comment explaining its internal-only status to prevent future accidental exports.

✅ Highlights

  • packages/core/src/core/tokenLimits.ts - The refactoring of tokenLimit() to compose over knownTokenLimit() is elegant and maintains backward compatibility while enabling the new "unknown model" detection pattern.

  • packages/vscode-ide-companion/src/webview/utils/contextUsage.ts - The pure function design with no default fallback is exactly what's needed for the webview context. Returning null when no trusted limit is available prevents misleading UI states.

  • Test files - All three test files (tokenLimits.test.ts, acpModelInfo.test.ts, contextUsage.test.ts) demonstrate thorough coverage of edge cases, including the critical "unknown model returns undefined" behavior and the inputTokens/promptTokens compatibility handling.

  • packages/vscode-ide-companion/src/utils/acpModelInfo.ts - The explicit handling of null vs undefined for context limits ("limit intentionally unknown" vs "not provided") shows thoughtful API design that respects server intent.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant