Skip to content

Commit 6fde2ef

Browse files
committed
test(bedrock): kill the cacheDetails reader mutants the gate found surviving
What: - Add a 3-row `it.each` covering the malformed split shapes the reader must absorb: a 5m entry with no 1h entry, a negative 1h count, and a zero 1h count. - Add a split ordered 5m-first so the reader is proven to match on the ttl value rather than trusting the entry's position. Why: CI's mutation-diff run reported four survivors inside `readCacheWrite1hTokens`. Re-run of Stryker on the same span confirmed all four, and three are real gaps rather than equivalent mutants: the optional chain before `.inputTokens` was never exercised without a 1h entry, the `ttl === ONE_HOUR` predicate was only ever fed a 1h-first array (so "return the first entry" passed), and the `<= 0` guard was never fed a negative count. The fourth (`<= 0` -> `< 0`) is equivalent because `!oneHour` already rejects 0. Impact: Test-only. `readCacheWrite1hTokens` is unchanged and still clamps to the reported aggregate. Stryker on the same span now kills 14/15, with the single survivor being the equivalent `<= 0` -> `< 0` mutant. No production line, price row, or suppression count changes.
1 parent 7a06eac commit 6fde2ef

1 file changed

Lines changed: 60 additions & 0 deletions

File tree

‎src/api/providers/__tests__/bedrock-cache-ttl.spec.ts‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,66 @@ describe("Bedrock cache-point TTL emission", () => {
379379
expect((usage as { cacheWrite1hTokens?: number }).cacheWrite1hTokens).toBeUndefined()
380380
})
381381

382+
it.each([
383+
["a 5m entry and no 1h entry", [{ ttl: "5m", inputTokens: 1000 }]],
384+
["a negative 1h count", [{ ttl: "1h", inputTokens: -5 }]],
385+
["a zero 1h count", [{ ttl: "1h", inputTokens: 0 }]],
386+
])("omits the 1h share when the split carries %s", async (_label, cacheDetails) => {
387+
// Each row is a distinct malformed shape the reader must absorb rather than
388+
// bill: an absent 1h entry (optional chain), a negative count (the `<= 0`
389+
// guard, which `!oneHour` alone does not cover), and a zero count.
390+
const handler = createHandler({ apiModelId: "anthropic.claude-sonnet-4-5-20250929-v1:0" })
391+
handler["client"].send = vi.fn().mockResolvedValue({
392+
stream: asyncStreamFrom([
393+
{
394+
metadata: {
395+
usage: {
396+
inputTokens: 10,
397+
outputTokens: 5,
398+
cacheWriteInputTokens: 3000,
399+
cacheDetails,
400+
},
401+
},
402+
},
403+
]),
404+
})
405+
406+
const chunks = await collectStream(handler.createMessage(longSystemPrompt, messages))
407+
const usage = chunks.find((chunk) => chunk.type === "usage")
408+
409+
expect(usage).toMatchObject({ type: "usage", cacheWriteTokens: 3000 })
410+
expect((usage as { cacheWrite1hTokens?: number }).cacheWrite1hTokens).toBeUndefined()
411+
})
412+
413+
it("reads the 1h entry wherever it sits in the split, not just the first entry", async () => {
414+
// Converse orders the split longest-TTL-first today, but the reader must match
415+
// on the ttl value rather than trusting position: a 5m entry ahead of the 1h
416+
// entry would otherwise report the 5m count as the 1h share.
417+
const handler = createHandler({ apiModelId: "anthropic.claude-sonnet-4-5-20250929-v1:0" })
418+
handler["client"].send = vi.fn().mockResolvedValue({
419+
stream: asyncStreamFrom([
420+
{
421+
metadata: {
422+
usage: {
423+
inputTokens: 10,
424+
outputTokens: 5,
425+
cacheWriteInputTokens: 3000,
426+
cacheDetails: [
427+
{ ttl: "5m", inputTokens: 1000 },
428+
{ ttl: "1h", inputTokens: 2000 },
429+
],
430+
},
431+
},
432+
},
433+
]),
434+
})
435+
436+
const chunks = await collectStream(handler.createMessage(longSystemPrompt, messages))
437+
const usage = chunks.find((chunk) => chunk.type === "usage")
438+
439+
expect(usage).toMatchObject({ type: "usage", cacheWriteTokens: 3000, cacheWrite1hTokens: 2000 })
440+
})
441+
382442
it("clamps the 1h share to the reported aggregate so a malformed split cannot over-bill", async () => {
383443
const handler = createHandler({ apiModelId: "anthropic.claude-sonnet-4-5-20250929-v1:0" })
384444
handler["client"].send = vi.fn().mockResolvedValue({

0 commit comments

Comments
 (0)