|
| 1 | +// Regression: a stored step-finish part with a negative token count made the |
| 2 | +// messages endpoint 400. Some providers reported `outputTokens` excluding |
| 3 | +// reasoning while also reporting `reasoningTokens` separately, so the |
| 4 | +// `outputTokens - reasoningTokens` math in Session.getUsage underflowed to |
| 5 | +// negative. The pre-fix `safe()` clamp only guarded against non-finite. The |
| 6 | +// strict `NonNegativeInt` schema then made every load of the message list |
| 7 | +// fail to encode, killing Desktop boot for every user with such a row. |
| 8 | +import { afterEach, describe, expect } from "bun:test" |
| 9 | +import { Effect } from "effect" |
| 10 | +import { eq } from "drizzle-orm" |
| 11 | +import { ModelID, ProviderID } from "../../src/provider/schema" |
| 12 | +import { WithInstance } from "../../src/project/with-instance" |
| 13 | +import { Server } from "../../src/server/server" |
| 14 | +import { SessionPaths } from "../../src/server/routes/instance/httpapi/groups/session" |
| 15 | +import { Session } from "@/session/session" |
| 16 | +import { MessageID, PartID } from "../../src/session/schema" |
| 17 | +import * as Database from "@/storage/db" |
| 18 | +import { PartTable } from "@/session/session.sql" |
| 19 | +import { resetDatabase } from "../fixture/db" |
| 20 | +import { disposeAllInstances, tmpdir } from "../fixture/fixture" |
| 21 | +import { it } from "../lib/effect" |
| 22 | + |
| 23 | +afterEach(async () => { |
| 24 | + await disposeAllInstances() |
| 25 | + await resetDatabase() |
| 26 | +}) |
| 27 | + |
| 28 | +function seedNegativeTokenSession(directory: string) { |
| 29 | + return Effect.promise(async () => |
| 30 | + WithInstance.provide({ |
| 31 | + directory, |
| 32 | + fn: () => |
| 33 | + Effect.runPromise( |
| 34 | + Effect.gen(function* () { |
| 35 | + const session = yield* Session.Service |
| 36 | + const info = yield* session.create({}) |
| 37 | + const message = yield* session.updateMessage({ |
| 38 | + id: MessageID.ascending(), |
| 39 | + role: "user", |
| 40 | + sessionID: info.id, |
| 41 | + agent: "build", |
| 42 | + model: { providerID: ProviderID.make("test"), modelID: ModelID.make("test") }, |
| 43 | + time: { created: Date.now() }, |
| 44 | + }) |
| 45 | + const partID = PartID.ascending() |
| 46 | + yield* session.updatePart({ |
| 47 | + id: partID, |
| 48 | + sessionID: info.id, |
| 49 | + messageID: message.id, |
| 50 | + type: "step-finish", |
| 51 | + reason: "stop", |
| 52 | + cost: 0, |
| 53 | + tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } }, |
| 54 | + }) |
| 55 | + |
| 56 | + // Bypass the schema with a direct SQL update to install the |
| 57 | + // negative `output` value we want to test loading. |
| 58 | + Database.use((db) => |
| 59 | + db |
| 60 | + .update(PartTable) |
| 61 | + .set({ |
| 62 | + data: { |
| 63 | + type: "step-finish", |
| 64 | + reason: "stop", |
| 65 | + cost: 0, |
| 66 | + tokens: { input: 0, output: -42, reasoning: 0, cache: { read: 0, write: 0 } }, |
| 67 | + } as never, |
| 68 | + }) |
| 69 | + .where(eq(PartTable.id, partID)) |
| 70 | + .run(), |
| 71 | + ) |
| 72 | + |
| 73 | + return info.id |
| 74 | + }).pipe(Effect.provide(Session.defaultLayer)), |
| 75 | + ), |
| 76 | + }), |
| 77 | + ) |
| 78 | +} |
| 79 | + |
| 80 | +describe("messages endpoint tolerates legacy negative token counts", () => { |
| 81 | + it.live( |
| 82 | + "returns 200 even when a step-finish part has tokens.output < 0", |
| 83 | + Effect.acquireRelease( |
| 84 | + Effect.promise(() => tmpdir({ config: { formatter: false, lsp: false } })), |
| 85 | + (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]()), |
| 86 | + ).pipe( |
| 87 | + Effect.flatMap((tmp) => |
| 88 | + Effect.gen(function* () { |
| 89 | + const sessionID = yield* seedNegativeTokenSession(tmp.path) |
| 90 | + const url = `${SessionPaths.messages.replace(":sessionID", sessionID)}?limit=80&directory=${encodeURIComponent(tmp.path)}` |
| 91 | + const res = yield* Effect.promise(async () => Server.Default().app.request(url)) |
| 92 | + expect(res.status, "messages endpoint 400'd on legacy negative tokens").not.toBe(400) |
| 93 | + }), |
| 94 | + ), |
| 95 | + ), |
| 96 | + ) |
| 97 | +}) |
0 commit comments