Skip to content

UsageSummary omits cache-token fields the aggregator returns, and JsonSchema.type is a scalar where the format uses an array #608

Description

@serge-ivo

Two declared types are narrower than the values they describe — found by the first typecheck of worker code

6d44ac7 (#599) put worker sources and tests under tsc for the first time. Two production type defects surfaced and were deliberately left for their own ticket.

1. UsageSummary["totals"] omits fields aggregateUsage returns

workers/api/src/lib/usage.ts:319 — the declared totals shape lacks cacheReadTokens and cacheWriteTokens, which aggregateUsage computes and returns.

The console already works around it: store/console/src/pages/Usage.tsx:35 declares its own parallel shape with those fields optional. So the data crosses the wire, the UI reads it, and the type says it does not exist — which means the compiler cannot help anyone who touches either side.

This is the same drop-on-the-way-out shape #547 fixed for daily. Second instance, same file.

2. JsonSchema.properties[].type is a scalar where the format uses an array

workers/api/src/lib/connectors/types.ts:111 declares type as string. JSON Schema draft-07 permits string[], and behaviourToolSchema uses the array form. So a legal schema this codebase itself produces does not satisfy the type meant to describe it.

Why they belong together

Both are the same defect: a declared type narrower than the value it describes, surviving because nothing compiled the code that would have caught it. Both were invisible for the same reason and became visible on the same day, which is the argument for one guard rather than two fixes.

It is also the type-system instance of the pattern this batch has been removing everywhere else — a stored or declared fact that the runtime contradicts (#587 runtime.status, #588 RunHealth, #592 attempts, #596 waiting_until). Here the contradiction is between a type and its own producer.

Acceptance criteria

  1. UsageSummary["totals"] carries every field aggregateUsage returns, and Usage.tsx consumes that type instead of a parallel declaration. The parallel shape is the tell — remove it, do not sync it.
  2. JsonSchema.properties[].type accepts string | string[], and behaviourToolSchema typechecks against it without a cast.
  3. A test asserts the declared return type matches what the function actually returns for aggregateUsage — a compile-time assertion is now genuinely enforced after Three tsconfigs exclude test files and CI typechecks none of them, so a type-level guard in any worker test is inert #599, so this is the first issue that can rely on one.
  4. Check whether other API response types have a parallel console-side declaration. Two instances of one shape (The Usage chart shows 4.2M tokens for the day the 250M ceiling was tripped — the daily series drops the cache columns, which are 98.2% of what the ceiling counts #547, this) suggests a habit; report the count with its denominator per ADR 0002 rather than fixing only the two known.

Verified vs inferred

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2: correctnessReal defect, no live harm today — inert fields, miscounts, missing guardsbackendBackend / Worker / API workbugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions