Repository navigation
fix(gateway): preserve billed costs and actual routes - #15366
Merged
Merged
Conversation
Contributor
|
➖ v2 migration progress: 0.9% → 0.9% (+2 done, 0 total, 0 dropped; 92/9829 cells) Placement: 51 new marked lines in shared files (51 patch), 0 new Ratchets: 9 core-internal imports (base 9); shared patches by rung 20 seam, 16 patch (base 20 seam, 7 patch).
2 cells closed
|
Contributor
Code Review SummaryStatus: No Issues Found | Recommendation: Merge Files Reviewed (3 files)
Verification notes
Previous Review Summary (commit 78db244)Current summary above is authoritative. Previous snapshots are kept for context only. Previous review (commit 78db244)Status: 2 Issues Found | Recommendation: Address before merge Overview
Issue Details (click to expand)SUGGESTION
Files Reviewed (21 files)
Verification notes
Reviewed by deepseek-flash-latest · Input: 88.5K · Output: 8.6K · Cached: 287.9K Review guidance: REVIEW.md from base branch |
chrarnoldus
approved these changes
Oct 22, 2026
This was referenced Oct 23, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What Problem This Solves
Gateway responses lost reported billing and actual-model metadata before v2 settled a step. Auto could therefore settle at its catalog price, and earlier responses could not retain different routes after restart.
This change carries successful Gateway metadata through the existing usage path, settles reported costs, and keeps the requested model separate from the actual route. It does not add an event type, table, projection, ledger, or accounting service.
Why This Change Was Made
Behavior was compared against freshly fetched
main9e4e3a43c80, fromv2981607bf544. No main code was merged into v2.Step.Ended/Step.Failed.nativenow carry onlynative.kilo.{routedModelID,vercelID,generationID,responseID}when supplied. Existing cost and token fields remain authoritative. Raw Gateway envelopes and duplicated token counts are not persisted.User Impact
session.metadataRPC is unchanged and is not durable history. No Protocol or HttpApi surface changed, so client regeneration was not needed.Privacy-safe crops from the real production TUI after reopening the same isolated session:
Evidence
All final checks used the repository-pinned Bun 1.4.2. Tests ran from their package directories with fresh
OPENCODE_TEST_HOME,XDG_DATA_HOME,XDG_CONFIG_HOME,XDG_STATE_HOME, andXDG_CACHE_HOME.bun test test/kilocode/gateway-usage.test.ts test/kilocode/session-stats.test.ts test/session-runner.test.ts test/session-runner-tool-events.test.ts test/session-title.test.ts test/session-stats.test.tsbun testbun test test/gateway-usage.test.ts test/rpc.test.tsbun test test/kilocode/routed-model.test.ts test/kilocode/routed-model.view.test.tsx test/util/session.test.ts test/feature-plugins/prompt-footer.test.tsxbun testbun typecheckbun run checkThe initial nested-worktree install selected an ancestor Node 22 type package. A local generated-only
bun install --linker hoisted --frozen-lockfileselected locked Node 24 types. The changed branch's root check and final tests then passed. No manifest, lockfile, global install, or source workaround was changed.The first CI run found one introduced tooling-test regression: the real-tree placement test still expected no compaction patch. The follow-up keeps its plugin-hook/service assertions and verifies the new registered billing patch instead. The complete tooling suite and root check pass locally after that correction; no failed workflow was manually rerun.
Review follow-up: partial or malformed normalized OpenRouter records no longer hide a valid raw OpenRouter cost. Regression tests reproduced the lost nonzero and zero charges before the fix, and also verify that a valid normalized zero still takes precedence. The stats test checks exact row membership/count without relying on the order of tied token totals. The two addressed review threads were resolved after the fix was pushed, without bot replies. The focused core/real-host suites and root check passed again.
Real-host tests cover Chat, Anthropic Messages, OpenAI Responses, five different routes within one session, real reasoning/read-tool continuation, provider-cost precedence, zero and malformed costs, token fallback, exact message association, title and compaction charges, parent/child totals, and full transcript equality after closing and reopening file-backed SQLite.
Real self-tests, separate from helper/renderer tests:
bun dev run --standalone --format jsonagainst a local controlled Gateway with a freshKILO_V2_PROFILE. First response settled upstream cost$0.125despite a zero routing fee. A new process using the same profile settled a second route at market cost$0. SQLite retained both routes and distinct request/generation/response IDs while both requested models stayed Auto.bun dev stats --standalone --models --cost: alpha and beta appeared as separate model rows at$0.125and$0.vscode-self-testsession running the real TUI through the isolatedbun deventry with--standalone. Both earlier and later reasoning/footer labels were correct. The TUI's Compact session command settled$0.0625; the session total became$0.2075, including the existing$0.02title charge. A cleanenv -irestart with the same explicit isolated database retained both routes and the completed compaction. The harness and fixture processes were stopped afterward.kilo/deepseek/deepseek-v4.1-flashsmoke test returnedACCOUNTING_OK. Its isolated stored assistant had cost$0.0017056884, the actual public model ID, and request/generation IDs. Credentials and private request IDs were not printed or published.Limits: controlled fixtures prove precedence and durable association, not a reconciliation with a deployed billing invoice. JSON native-compaction usage is covered at the success transformer; the exercised public native-model Session path selected streamed compaction, which was tested end to end. No historical rows without response metadata are reconstructed.
Placement
kilo-gateway-core/src/stream.ts; no adapter index changescore/src/session/{usage,title,compaction}.tsandrunner/{publish-llm-event,step}.ts; policy incore/src/kilocode/session/usage.tsroutes/session/{index,group-view}.tsxandutil/session.ts; Kilo-owned componentsAll nine shared files have
shared-patches.jsonentries. Shared-file limits change from 27 to 36 files and 204 to 255 marked lines; unmarked lines stay at 39. No new core-internal adapter import or mirrored file was added. Logic-package import checks, annotation checks, and shared-patch checks pass.Progress map
gateway: 59/75 to 61/75, with 12 existing drops unchanged. Module 30/35 to 31/35; test 17/22 to 18/22; patch 12/18 unchanged.gateway-metadata.tsand its regression tests. The disposition pointers identify the success transformer, actual settlement, native-dialect host tests, and restart tests.Manual Test
bun dev stats --standalone --models --cost. Verify actual-route rows and zero-cost responses. A regular non-Kilo provider must retain its existing token-price accounting.