Skip to content

fix(copilot): stop billing cached tokens twice on OTel spans - #1830

Merged
iamtoruk merged 2 commits into
mainfrom
fix/copilot-otel-cache-inclusive-input
Nov 2, 2026
Merged

iamtoruk merged 2 commits into
mainfrom
fix/copilot-otel-cache-inclusive-input

Conversation

@iamtoruk

@iamtoruk iamtoruk commented Nov 2, 2026

Copy link
Copy Markdown
Member

VS Code Copilot Chat writes gen_ai.usage.input_tokens on agent-traces.db spans as the full prompt, cache reads and writes included. The OTel parser passed that number to calculateCost as uncached input, so every cached token was billed once at the input rate and again at its cache rate. The chatSessions path already subtracts the cache counts; the OTel path now does the same, and keeps an input that is already below the cache counts as written.

The copilot parse version goes up so cached OTel calls re-parse with the corrected input. Calls whose spans the extension has already pruned keep the old input, since there is nothing left to re-read.

Test: tests/providers/copilot.test.ts gets a span with cache reads and writes and checks the input tokens and the cost after parsing, plus the unchanged case of an input already below the cache counts.

VS Code Copilot Chat writes gen_ai.usage.input_tokens on agent-traces.db
spans as the full prompt, cache reads and writes included. The OTel parser
passed it to calculateCost as uncached input, so every cached token was
billed at the input rate on top of its cache rate. The parser now subtracts
the cache counts the way the chatSessions path does, and keeps an input
below them as written.

Bumps the copilot parse version so cached OTel calls re-parse. Calls whose
spans the extension already pruned keep the old input.
…the bump

The parse-version bump re-parses the surviving agent-traces.db spans with
the cache counts taken out of input, so input and cost drop by design while
the pruned calls keep the 0.9.20 figure. Calls, output and cache reads stay
exact; input must drop by exactly the cache counts of the surviving
cache-inclusive spans, and cost must drop.
@iamtoruk
iamtoruk merged commit f846b63 into main Nov 2, 2026
30 of 31 checks passed
@iamtoruk
iamtoruk deleted the fix/copilot-otel-cache-inclusive-input branch November 2, 2026 19:26
iamtoruk added a commit that referenced this pull request Nov 7, 2026
#1830 f846b63: OTel input_tokens is cache-inclusive, cache reads carved out of inputTokens. inputTokens and cost may only drop; calls, output, reasoning, cache read and cache write stay exact. Tests: the corpus-shaped drop passes, a cost rise, input rise or cache-read change fails.
iamtoruk added a commit that referenced this pull request Nov 7, 2026
…readable cache entries

Seeded corpus is deterministic, so copilot must land on inputTokens 627176 and cost 3.015976 with every other field exact; anything else fails naming #1830. treeStats skips an unreadable entry or dangling symlink with a note. Index/envelope exclusion stays: a real re-parse rewrites piece files, which are hashed.
iamtoruk added a commit that referenced this pull request Nov 7, 2026
* fix(upgrade-path): build the Copilot fixture under the corpus home on every platform

On win32 the fixture path came from the real APPDATA and the generator rmSync'd the user's real agent-traces.db. It now derives from the throwaway home and throws if the path resolves outside it. Checked by loading the generator with process.platform mocked to darwin, win32 and linux and APPDATA pointing elsewhere: all three wrote under the corpus home.

* fix(upgrade-path): fail parity on unclassified provider differences

A provider outside the exact and changed lists only added a note, so parity could never fail for it. Differences now push a failure; identical stays a note. Checked with a two-dir fixture: differing zzz tokens print FAIL and parity reports 1 difference; identical dirs report parity ok.

* fix(upgrade-path): a warm run with no pending-parse line passes

The warm check was skipped when no 'parse workers=' line printed, but that line only prints when work is pending. No line is now a pass; a line with pending work still fails.

* fix(gnome): budget alert converted currency twice

Cost was multiplied by the FX rate and then _fmt multiplied again. Before, EUR at 0.9 with 100 USD spent over a 50 USD limit: cost 90 -> _fmt 81.00 EUR / thresholdConverted 45 -> 40.50. After: 90.00 EUR / 45.00 EUR.

* fix(mac): cost per edit honours the selected currency

Both cost-per-edit labels printed a literal dollar sign with no FX rate; they now use the currency symbol and rate like the neighbouring values.

* fix(mac): menubar placeholder uses the currency symbol

The no-data title showed a dollar sign under other currencies.

* test(upgrade-path): containment via path.relative, warm check proves no cache file changed, add vitest coverage

Helpers moved to scripts/upgrade-path/lib.mjs so they can be imported. The guard no longer breaks on drive or UNC roots. The warm check now compares mtime and size of every cache file before and after the warm run; the parse-workers line is secondary.

* fix(upgrade-path): anchor the pending-files match, hash cache contents, allow dot-prefixed child names

Adds tests for each, plus a wiring test that runs gen-corpus under a mocked win32 platform and checks run.mjs uses the shared warm helpers.

* test(upgrade-path): run the gen-corpus wiring test on the host platform

* fix(mac): route the cost-per-edit suffix through the localization catalog

The localization coverage test flagged the new interpolated literal. Adds the %@/edit key to every locale.

* fix(upgrade-path): warm snapshot ignores the envelope and index files

A warm run rewrites envelope.json and republishes the codex month index under a new name with identical content (known #1032 churn), so those two cannot be part of a no-change snapshot. Their content is already covered by the existing shard check (piece, key file and index bodies unchanged); every piece and key file is still content-hashed here.

* fix(upgrade-path): classify the copilot input carve-out from #1830

#1830 f846b63: OTel input_tokens is cache-inclusive, cache reads carved out of inputTokens. inputTokens and cost may only drop; calls, output, reasoning, cache read and cache write stay exact. Tests: the corpus-shaped drop passes, a cost rise, input rise or cache-read change fails.

* fix(upgrade-path): pin the copilot #1830 figures exactly, tolerate unreadable cache entries

Seeded corpus is deterministic, so copilot must land on inputTokens 627176 and cost 3.015976 with every other field exact; anything else fails naming #1830. treeStats skips an unreadable entry or dangling symlink with a note. Index/envelope exclusion stays: a real re-parse rewrites piece files, which are hashed.

* test(upgrade-path): import gen-corpus by file URL so the wiring test runs on Windows

A bare drive-letter path is not a valid ESM specifier (ERR_UNSUPPORTED_ESM_URL_SCHEME), so the child died at startup on Windows. The test now uses pathToFileURL and reports status, stderr and the fixture path on failure.

* test(upgrade-path): skip the corpus-generating wiring test on Windows

The Windows upgrade-path job is the end-to-end check there; the child that builds the whole corpus exceeds the test timeout on that runner.
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