negative-tokens-regression.test.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 { 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 { Server } from "../../src/server/server"
  13. import { SessionPaths } from "../../src/server/routes/instance/httpapi/groups/session"
  14. import { Session } from "@/session/session"
  15. import { MessageID, PartID } from "../../src/session/schema"
  16. import * as Database from "@/storage/db"
  17. import { PartTable } from "@/session/session.sql"
  18. import { resetDatabase } from "../fixture/db"
  19. import { TestInstance } from "../fixture/fixture"
  20. import { testEffect } from "../lib/effect"
  21. const it = testEffect(Session.defaultLayer)
  22. function seedNegativeTokenSession() {
  23. return Effect.gen(function* () {
  24. const session = yield* Session.Service
  25. const info = yield* session.create({})
  26. const message = yield* session.updateMessage({
  27. id: MessageID.ascending(),
  28. role: "user",
  29. sessionID: info.id,
  30. agent: "build",
  31. model: { providerID: ProviderID.make("test"), modelID: ModelID.make("test") },
  32. time: { created: Date.now() },
  33. })
  34. const partID = PartID.ascending()
  35. yield* session.updatePart({
  36. id: partID,
  37. sessionID: info.id,
  38. messageID: message.id,
  39. type: "step-finish",
  40. reason: "stop",
  41. cost: 0,
  42. tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } },
  43. })
  44. // Bypass the schema with a direct SQL update to install the
  45. // negative `output` value we want to test loading.
  46. Database.use((db) =>
  47. db
  48. .update(PartTable)
  49. .set({
  50. data: {
  51. type: "step-finish",
  52. reason: "stop",
  53. cost: 0,
  54. tokens: { input: 0, output: -42, reasoning: 0, cache: { read: 0, write: 0 } },
  55. } as never,
  56. })
  57. .where(eq(PartTable.id, partID))
  58. .run(),
  59. )
  60. return info.id
  61. })
  62. }
  63. describe("messages endpoint tolerates legacy negative token counts", () => {
  64. it.instance(
  65. "returns 200 even when a step-finish part has tokens.output < 0",
  66. Effect.gen(function* () {
  67. yield* Effect.addFinalizer(() => Effect.promise(() => resetDatabase()))
  68. const test = yield* TestInstance
  69. const sessionID = yield* seedNegativeTokenSession()
  70. const url = `${SessionPaths.messages.replace(":sessionID", sessionID)}?limit=80&directory=${encodeURIComponent(test.directory)}`
  71. const res = yield* Effect.promise(async () => Server.Default().app.request(url))
  72. expect(res.status, "messages endpoint 400'd on legacy negative tokens").not.toBe(400)
  73. }),
  74. { git: true, config: { formatter: false, lsp: false } },
  75. )
  76. })