Skip to content

Commit 94df9bc

Browse files
anandgupta42claude
andcommitted
fix(session): omit tokens_cache_read when accounting rejected the detail count
- A cache-read count present only in `inputTokenDetails` can be rejected as incomplete by `Session.getUsage`; telemetry then reported a zero that looked measured. `cacheReadReported` emits the field only when the count was kept. - Make the resolver failure test fail clearly if the resolver stops throwing. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 5249f8a commit 94df9bc

3 files changed

Lines changed: 55 additions & 8 deletions

File tree

‎packages/opencode/src/session/processor.ts‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,20 @@ import type { SessionID, MessageID } from "./schema"
2222
// altimate_change start — import Telemetry for per-generation token tracking
2323
import { Telemetry } from "@/altimate/telemetry"
2424
// altimate_change end
25+
// altimate_change start — upstream_fix: report a cache-read count only when accounting accepted it
26+
/**
27+
* Whether the provider supplied a cache-read count that `Session.getUsage` kept. A count present only in
28+
* `inputTokenDetails` can be rejected as incomplete; reporting the resulting zero would look measured.
29+
*/
30+
export function cacheReadReported(
31+
raw: { cachedInputTokens?: number; inputTokenDetails?: { cacheReadTokens?: number } },
32+
accepted: number,
33+
): boolean {
34+
if (raw.cachedInputTokens !== undefined) return true
35+
const detail = raw.inputTokenDetails?.cacheReadTokens
36+
return detail !== undefined && detail === accepted
37+
}
38+
// altimate_change end
2539
// altimate_change start — write-starvation breaker + loop detection (fork-only
2640
// modules) and the run-mode flag that gates armed behavior.
2741
import { SessionStarvation } from "./starvation"
@@ -945,9 +959,9 @@ export namespace SessionProcessor {
945959
tokens_input_total: usage.tokens.inputTotal,
946960
// altimate_change end
947961
...(value.usage.reasoningTokens !== undefined && { tokens_reasoning: usage.tokens.reasoning }),
948-
...((value.usage.cachedInputTokens !== undefined ||
949-
(value.usage as { inputTokenDetails?: { cacheReadTokens?: number } }).inputTokenDetails
950-
?.cacheReadTokens !== undefined) && { tokens_cache_read: usage.tokens.cache.read }),
962+
...(cacheReadReported(value.usage, usage.tokens.cache.read) && {
963+
tokens_cache_read: usage.tokens.cache.read,
964+
}),
951965
...(usage.tokens.cache.write > 0 && { tokens_cache_write: usage.tokens.cache.write }),
952966
})
953967
// altimate_change end

‎packages/opencode/test/script/bedrock-eventstream-build.test.ts‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,18 @@ describe("smithyNodeSerdePlugin scope", () => {
8181
;(Bun as any).resolveSync = () => {
8282
throw boom
8383
}
84+
let caught: any
8485
try {
8586
resolve({ importer: "/x/node_modules/@smithy/util-buffer-from/dist-es/index.js" })
86-
throw new Error("expected resolver to throw")
87-
} catch (err: any) {
88-
expect(err.message).toContain("cannot resolve @smithy/core/serde")
89-
expect(err.message).toContain("/x/node_modules/@smithy/util-buffer-from/dist-es/index.js")
90-
expect(err.cause).toBe(boom)
87+
} catch (err) {
88+
caught = err
9189
} finally {
9290
;(Bun as any).resolveSync = original
9391
}
92+
expect(caught).toBeDefined()
93+
expect(caught.message).toContain("cannot resolve @smithy/core/serde")
94+
expect(caught.message).toContain("/x/node_modules/@smithy/util-buffer-from/dist-es/index.js")
95+
expect(caught.cause).toBe(boom)
9496
})
9597

9698
test("leaves every other importer untouched", () => {

‎packages/opencode/test/session/session-getusage-inclusive.test.ts‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { describe, test, expect } from "bun:test"
22
import { Session } from "../../src/session"
33
import { accountUsage } from "../../src/altimate/learn/usage"
4+
import { cacheReadReported } from "../../src/session/processor"
45

56
/**
67
* Cost accounting for providers whose AI SDK adapter reports an INCLUSIVE `inputTokens`.
@@ -202,6 +203,36 @@ describe("Session.getUsage - details-only cache reads and the long-context tier"
202203
expect(r.tokens.inputTotal).toBe(2011)
203204
})
204205

206+
test("learn accountUsage keeps an explicit top-level cachedInputTokens", async () => {
207+
const accounted = await accountUsage(
208+
model("@ai-sdk/amazon-bedrock"),
209+
{ inputTokens: 1011, outputTokens: 10, cachedInputTokens: 1000 },
210+
{ bedrock: {} } as any,
211+
)
212+
expect(accounted.inputTokens).toBe(2011)
213+
})
214+
215+
test("learn accountUsage still counts detail-only cache reads for inclusive providers", async () => {
216+
const accounted = await accountUsage(
217+
model("@ai-sdk/openai"),
218+
{ inputTokens: 5000, outputTokens: 10, inputTokenDetails: { cacheReadTokens: 2000 } },
219+
{} as any,
220+
)
221+
expect(accounted.inputTokens).toBe(5000)
222+
expect(accounted.estimatedCost).toBeCloseTo((3000 * 3 + 2000 * 0.3 + 10 * 15) / 1e6, 12)
223+
})
224+
225+
test("telemetry reports a cache-read count only when accounting kept it", () => {
226+
// Rejected incomplete detail: accounting holds 0, the provider said 1000.
227+
expect(cacheReadReported({ inputTokenDetails: { cacheReadTokens: 1000 } }, 0)).toBe(false)
228+
// Accepted detail, including a genuine zero.
229+
expect(cacheReadReported({ inputTokenDetails: { cacheReadTokens: 1000 } }, 1000)).toBe(true)
230+
expect(cacheReadReported({ inputTokenDetails: { cacheReadTokens: 0 } }, 0)).toBe(true)
231+
// An explicit top-level count is always reported; no count at all never is.
232+
expect(cacheReadReported({ cachedInputTokens: 0 }, 0)).toBe(true)
233+
expect(cacheReadReported({}, 0)).toBe(false)
234+
})
235+
205236
test("inclusive providers keep the details-only cache-read fallback without noCacheTokens", () => {
206237
const r = Session.getUsage({
207238
model: model("@ai-sdk/openai"),

0 commit comments

Comments
 (0)