Skip to content

Commit 7ed2e89

Browse files
anandgupta42claude
andcommitted
fix: [#836] address re-review — rename misleading test + document negative-cost follow-up
Reviewer caught that the test 'tokens.input is never negative even with inconsistent provider counts' is the OPPOSITE of what it actually verifies. The test pins that the algebraic invariant input + cache.read + cache.write === inputTotal holds when input IS negative — not that input avoids being negative. Renamed to 'invariant holds even when tokens.input goes negative on inconsistent provider counts' which matches the test body. Expanded the comment to note the downstream cost-calculation implication (negative input × positive cost rate produces a negative cost contribution) as a pre-existing follow-up concern. No code behavior change. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 80d7e8a commit 7ed2e89

1 file changed

Lines changed: 15 additions & 5 deletions

File tree

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,13 +164,23 @@ describe("Session.getUsage — provider edge cases", () => {
164164
expect(result.tokens.inputTotal).toBe(800 + 2000 + 600)
165165
})
166166

167-
test("tokens.input is never negative even with inconsistent provider counts", () => {
167+
test("invariant holds even when tokens.input goes negative on inconsistent provider counts", () => {
168+
// Renamed from "tokens.input is never negative" — the previous name was
169+
// the OPPOSITE of what this test verifies. The test pins that the
170+
// algebraic invariant `input + cache.read + cache.write === inputTotal`
171+
// holds even when input is negative.
172+
//
168173
// Hypothetical: OpenAI returns inputTokens=1000 but cachedInputTokens=2000
169174
// (inconsistent — should never happen but providers occasionally surface
170-
// weird numbers). Verify the subtraction doesn't underflow into negative
171-
// territory; safe() clamps via Number.isFinite but does NOT clamp
172-
// negatives. Document the current behavior so a future refactor that
173-
// changes it is forced through this test.
175+
// weird numbers). Verify the invariant holds even though `safe()`
176+
// clamps non-finite values but does NOT clamp negatives. Documents the
177+
// current behavior so a future refactor that changes it is forced
178+
// through this test.
179+
//
180+
// Note: negative tokens.input flows into cost calculation as a
181+
// negative contribution. Pre-existing concern — not introduced by
182+
// this PR. Tracked as a follow-up to either clamp at zero or accept
183+
// the offset.
174184
const result = Session.getUsage({
175185
model: fakeModel("@ai-sdk/openai"),
176186
usage: {

0 commit comments

Comments
 (0)