fix(server): ignore malformed Claude model usage
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 typecheckvp 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.tsvp fmt --check apps/server/src/provider/Layers/ClaudeAdapter.ts apps/server/src/provider/Layers/ClaudeAdapter.test.tsgit diff --check
Model: OpenAI Codex (GPT-5.6); harness: Codex desktop.
[!NOTE]
Fix
maxClaudeContextWindowFromModelUsageto ignore malformed model usage entriesThe
maxClaudeContextWindowFromModelUsageutil in ClaudeAdapter.ts now acceptsunknowninput and defensively skips invalidcontextWindowvalues (non-numeric, non-finite, zero, or negative) instead of propagatingNaN. Returnsundefinedwhen no valid entries are found, somaxTokensis omitted from thethread.token-usage.updatedevent 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
maxClaudeContextWindowFromModelUsageinClaudeAdapterso runtimemodelUsagefrom Claude result messages cannot crash the session or yield badmaxTokensonthread.token-usage.updated.The helper now takes
unknown, validates the map and each entry withPredicate, and only counts finite positive numericcontextWindowvalues. Invalid shapes (null entries, missingcontextWindow, strings, non-finite numbers, zero, or negatives) are skipped. When nothing valid remains, the function returnsundefined, somaxTokensis omitted from the usage payload while normal token totals still apply.A test replaces the happy-path context-window case with malformed
modelUsagefixtures and asserts usage events withoutmaxTokens.Reviewed by Cursor Bugbot for commit d64f3321e3dae01843cfd3f520c459dc384472cf. Bugbot is set up for automated code reviews on this repo. Configure here.
[!IMPORTANT]
Review skipped
Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
.coderabbit.yamlfile in this repository. To trigger a single review, invoke the@coderabbitai reviewcommand.⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID:
061b43ae-5f9a-4a54-9251-9b154edcab83You can disable this status message by setting the
reviews.review_statustofalsein 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.
Comment @coderabbitai help to get the list of available commands.
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.