t3code icon indicating copy to clipboard operation
t3code copied to clipboard

fix(server): ignore malformed Claude model usage

Open Lucenx9 opened this issue 4 months ago • 2 comments

What changed

Claude result messages are typed by the SDK, but modelUsage is runtime provider telemetry. A malformed entry could throw while reading contextWindow or produce NaN, failing the Claude provider session or degrading token normalization.

The adapter now validates the top-level value and each model entry at runtime, accepting only finite positive numeric context windows. Malformed entries are ignored, while valid token accounting remains unchanged.

Validation

  • vp test run apps/server/src/provider/Layers/ClaudeAdapter.test.ts (62 passed)
  • vp run --filter t3 typecheck
  • vp lint --report-unused-disable-directives --type-aware --type-check --deny-warnings apps/server/src/provider/Layers/ClaudeAdapter.ts apps/server/src/provider/Layers/ClaudeAdapter.test.ts
  • vp fmt --check apps/server/src/provider/Layers/ClaudeAdapter.ts apps/server/src/provider/Layers/ClaudeAdapter.test.ts
  • git diff --check

Model: OpenAI Codex (GPT-5.6); harness: Codex desktop.

[!NOTE]

Fix maxClaudeContextWindowFromModelUsage to ignore malformed model usage entries

The maxClaudeContextWindowFromModelUsage util in ClaudeAdapter.ts now accepts unknown input and defensively skips invalid contextWindow values (non-numeric, non-finite, zero, or negative) instead of propagating NaN. Returns undefined when no valid entries are found, so maxTokens is omitted from the thread.token-usage.updated event payload rather than set to a bad value.

Macroscope summarized d64f332.


[!NOTE] Low Risk Localized defensive parsing in the Claude adapter’s token-usage path; no auth or persistence changes, with test coverage for malformed telemetry.

Overview Hardens maxClaudeContextWindowFromModelUsage in ClaudeAdapter so runtime modelUsage from Claude result messages cannot crash the session or yield bad maxTokens on thread.token-usage.updated.

The helper now takes unknown, validates the map and each entry with Predicate, and only counts finite positive numeric contextWindow values. Invalid shapes (null entries, missing contextWindow, strings, non-finite numbers, zero, or negatives) are skipped. When nothing valid remains, the function returns undefined, so maxTokens is omitted from the usage payload while normal token totals still apply.

A test replaces the happy-path context-window case with malformed modelUsage fixtures and asserts usage events without maxTokens.

Reviewed by Cursor Bugbot for commit d64f3321e3dae01843cfd3f520c459dc384472cf. Bugbot is set up for automated code reviews on this repo. Configure here.

Lucenx9 avatar Aug 01 '26 11:08 Lucenx9

[!IMPORTANT]

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 061b43ae-5f9a-4a54-9251-9b154edcab83

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • [ ] 🔍 Trigger review

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

coderabbitai[bot] avatar Aug 01 '26 11:08 coderabbitai[bot]

Approvability

Verdict: Approved d64f332

This is a defensive bug fix that adds input validation to gracefully skip malformed Claude model usage entries. The change is limited in scope, has clear intent, and includes appropriate test coverage for the new validation logic.

You can customize Macroscope's approvability policy. Learn more.

macroscopeapp[bot] avatar Aug 01 '26 11:08 macroscopeapp[bot]