By kunghim (#663). Squashed at the reviewed head 42b01b3 (the PR's f2a1233, origin/main merged in, and the review's 0.2.19 bump: #657 had already published 0.2.18). workbuddy 0.2.17 -> 0.2.19: - The China build's token window is no longer called "Free model tokens": there deepseek-v4.1-flash is charged x0.11 credits (testdata/config-561.json, copilot.tencent.com's /v3/config; WorkBuddy AI's is x0.00). Its card is named "deepseek-v4.1-flash tokens (est.)", its text reads "151.5M / ~200M", and its unit is empty (the name says tokens). - The China windows are a table, CN_ESTIMATED_WINDOWS, one entry per measured model ({provider, model, aliases, cap, name, short, unit}). freeWindowOf, ofAccount and freeWindowRows take the model from the entry; the parse-cache key carries the model too. - WorkBuddy AI's card is unchanged (Free model tokens, 1e8, "tokens"). - Both stay set aside (shown, not routed on); a refusal still pins the card full. Routing in magpie reads the card's models/aside/refused, not its name. - README: the China windows section; the review fixed one line that said the numbers carry "tokens (est.)" (only the name does). Run at 42b01b3 on mote (macOS, bun 1.4.2), HOME=$(mktemp -d): - bun test (whole repo): 4900 pass, 14 skip, 0 fail (4914 tests, 374 files). - bun scripts/check.mjs: exit 0, 41 packages + registry ok. - packages/workbuddy bun test: 318 pass, 0 fail (node --test n/a: the package's tests use bun:test). - magpie's real host.js (origin/main) under bun, sandbox HOME, the package loaded from this tree, a usage.jsonl with one row per build and a preloaded fake for WorkBuddy's meter (network otherwise blocked): init provides workbuddy and workbuddy-ai; usage for workbuddy gives "deepseek-v4.1-flash tokens (est.)" 151,500,000 / 2e8, unit "", display "151.5M / ~200M", aside, models [deepseek-v4.1-flash, -sg]; workbuddy-ai gives "Free model tokens" 30,000,000 / 1e8, unit "tokens", "30.0M / 100M". Broken by hand: the card's name back to "Free model tokens": "the China build counts its own rows against 200M" and the ledger usage test go red. Not tried: a real China-build account; another CN model's window (none is measured). With several CN windows the one-entry parse cache would re-read the ledger per window each poll (one entry today).
788 lines
40 KiB
JavaScript
788 lines
40 KiB
JavaScript
// The free model's daily token window: what WorkBuddy AI refuses a chat on
|
|
// long before a free plan's credits are spent, and which the credit meter
|
|
// does not show. Every figure here is one measured on a real account; see
|
|
// freeWindowOf for the readings and how each was checked.
|
|
import { test, expect, beforeAll } from "bun:test"
|
|
import { tmpdir } from "node:os"
|
|
import { mkdtempSync, mkdirSync, realpathSync, rmSync, statSync, writeFileSync } from "node:fs"
|
|
import { homedir } from "node:os"
|
|
import { join } from "node:path"
|
|
|
|
let _internal
|
|
beforeAll(async () => {
|
|
// as usage.test.mjs: run with HOME=$(mktemp -d) bun test
|
|
if (![tmpdir(), realpathSync(tmpdir())].some((t) => homedir().startsWith(t))) throw new Error("run with HOME=$(mktemp -d) bun test")
|
|
;({ _internal } = await import("./index.mjs"))
|
|
})
|
|
|
|
// a row of magpie's usage ledger (usage.Record), as the plugin reads it
|
|
// pad stands in for a real row's request fields (conversation, client, price,
|
|
// error text), so a row can be made as fat as a ledger's own and a handful of
|
|
// them can outgrow one 64 KiB chunk, as they do in a live ledger
|
|
const row = (t, { status = 200, in: inp = 0, cache = 0, out = 0, err = "", model = "deepseek-v4.1-flash", account = "me@example.com", provider = "workbuddy-ai", pad = "" } = {}) => ({
|
|
provider,
|
|
providerAccount: account,
|
|
model,
|
|
t,
|
|
status,
|
|
in: inp,
|
|
cache_read: cache,
|
|
out,
|
|
err,
|
|
...(pad ? { pad } : {}),
|
|
})
|
|
const iso = (s) => new Date(s).toISOString()
|
|
const at = (s) => Date.parse(s)
|
|
|
|
// enough padding to make a row a live ledger's size
|
|
const FAT = "x".repeat(1400)
|
|
|
|
// the vendor's words before the reset it names
|
|
const TIMEOUT_BEFORE_RESET = "WorkBuddy AI: usage exceeds frequency limit, but don't worry, your usage will reset at "
|
|
|
|
// the refusal WorkBuddy AI sends when the window is spent
|
|
const refusal = (reset, account = "me@example.com") =>
|
|
row(`2026-10-20T19:47:41+08:00`, {
|
|
status: 429,
|
|
account,
|
|
err: `WorkBuddy AI: usage exceeds frequency limit, but don't worry, your usage will reset at ${reset} UTC+8, alternatively, you can switch to the other models to continue using it.`,
|
|
})
|
|
|
|
test("a window counts in + cache_read + out", () => {
|
|
const rows = [row("2026-10-20T19:53:48+08:00", { in: 1_000_000, cache: 4_000_000, out: 100_000 }), row("2026-10-20T20:00:00+08:00", { in: 500_000, cache: 2_000_000 })]
|
|
const w = _internal.freeWindowOf(rows, "me@example.com", at("2026-10-20T21:00:00+08:00"))
|
|
expect(w.amount).toBe(7_600_000)
|
|
expect(w.used).toBeCloseTo(7.6, 6)
|
|
expect(w.resetsAt).toBe(iso("2026-10-21T19:53:48+08:00"))
|
|
expect(w.span).toBe(86400)
|
|
expect(w.limit).toBe(1e8)
|
|
expect(w.unit).toBe("tokens")
|
|
expect(w.display).toBe("7.6M / 100M")
|
|
expect(w.models).toEqual(["deepseek-v4.1-flash", "deepseek-v4.1-flash-sg"])
|
|
})
|
|
|
|
// reading in + out alone is 20x too small and never reaches a cap, so a
|
|
// window read that way looks empty while it is full
|
|
test("cache_read alone can fill a window", () => {
|
|
const w = _internal.freeWindowOf([row("2026-10-20T19:53:48+08:00", { in: 200, cache: 99_000_000, out: 300 })], "me@example.com", at("2026-10-20T21:00:00+08:00"))
|
|
expect(w.used).toBeGreaterThan(98)
|
|
expect(w.used).toBeLessThanOrEqual(100)
|
|
})
|
|
|
|
// the reset a refusal names is the old window's end; the new one starts at
|
|
// the next request, which a real account made 68 minutes later
|
|
test("a new window opens at the next request, not at the reset instant", () => {
|
|
const rows = [
|
|
row("2026-10-16T15:40:03+08:00", { in: 60_000_000 }),
|
|
row("2026-10-17T08:00:00+08:00", { in: 40_000_000 }),
|
|
refusal("2026-10-17 08:00:13"),
|
|
row("2026-10-17T09:08:00+08:00", { in: 1_000 }),
|
|
]
|
|
const w = _internal.freeWindowOf(rows, "me@example.com", at("2026-10-17T10:00:00+08:00"))
|
|
expect(w.resetsAt).toBe(iso("2026-10-18T09:08:00+08:00"))
|
|
expect(w.amount).toBe(1_000)
|
|
})
|
|
|
|
// a window never refused still ends 24h after its first request
|
|
test("an unrefused window lapses 24h after its first request", () => {
|
|
const rows = [row("2026-10-19T19:52:24+08:00", { in: 90_000_000 }), row("2026-10-19T23:00:00+08:00", { in: 2_000_000 })]
|
|
expect(_internal.freeWindowOf(rows, "me@example.com", at("2026-10-20T19:00:00+08:00")).amount).toBe(92_000_000)
|
|
// lapsed: nothing is exhausted, so no window is reported
|
|
expect(_internal.freeWindowOf(rows, "me@example.com", at("2026-10-20T20:00:00+08:00"))).toBeNull()
|
|
})
|
|
|
|
// the paid id draws on the same window, so its rows are in this one
|
|
test("-sg counts in the same window as the free id", () => {
|
|
const rows = [row("2026-10-20T19:53:48+08:00", { in: 50_000_000 }), row("2026-10-20T20:00:00+08:00", { in: 10_000, model: "deepseek-v4.1-flash-sg" })]
|
|
const w = _internal.freeWindowOf(rows, "me@example.com", at("2026-10-20T21:00:00+08:00"))
|
|
expect(w.amount).toBe(50_010_000)
|
|
expect(w.models).toEqual(["deepseek-v4.1-flash", "deepseek-v4.1-flash-sg"])
|
|
})
|
|
|
|
test("other accounts, other models and the other build are left out", () => {
|
|
const rows = [
|
|
row("2026-10-20T19:53:48+08:00", { in: 1_000 }),
|
|
row("2026-10-20T19:54:00+08:00", { in: 9_000_000, account: "other@example.com" }),
|
|
row("2026-10-20T19:55:00+08:00", { in: 9_000_000, model: "glm-5.3-flash" }),
|
|
row("2026-10-20T19:56:00+08:00", { in: 9_000_000, model: "kimi-k2.6" }),
|
|
row("2026-10-20T19:57:00+08:00", { in: 9_000_000, provider: "workbuddy" }),
|
|
]
|
|
expect(_internal.freeWindowOf(rows, "me@example.com", at("2026-10-20T21:00:00+08:00")).amount).toBe(1_000)
|
|
})
|
|
|
|
// The China build (copilot.tencent.com) has the same window under its own
|
|
// words: measured 2026-11-02, two Free-plan accounts refused at 200,389,043
|
|
// and 200,485,923 tokens counted the same way. Its card reads against 2e8
|
|
// (a soft 1.9e8 in 0.2.12 and 0.2.13, plugins#641). A refusal in force
|
|
// still reads full.
|
|
const cnRow = (t, opts = {}) => row(t, { ...opts, provider: "workbuddy" })
|
|
// the refusal the China build sends, in its own words
|
|
const cnRefusal = (reset, account = "me@example.com") =>
|
|
row(`2026-11-02T11:30:00+08:00`, {
|
|
provider: "workbuddy",
|
|
status: 429,
|
|
account,
|
|
err: `WorkBuddy: 您的使用量已超出频率限制,将在 ${reset} UTC+8 重置,您也可以切换其他模型继续使用。`,
|
|
})
|
|
|
|
test("the China build counts its own rows against 200M", () => {
|
|
const rows = [cnRow("2026-11-02T02:32:00+08:00", { in: 1_000_000, cache: 188_000_000, out: 500_000 }), cnRow("2026-11-02T03:00:00+08:00", { in: 200 })]
|
|
const w = _internal.freeWindowOf(rows, "me@example.com", at("2026-11-02T12:00:00+08:00"), _internal.CN_ESTIMATED_WINDOWS[0])
|
|
expect(w.amount).toBe(189_500_200)
|
|
expect(w.limit).toBe(2e8)
|
|
expect(w.used).toBeCloseTo(94.75, 3)
|
|
expect(w.display).toBe("189.5M / ~200M")
|
|
expect(w.resetsAt).toBe(iso("2026-11-03T02:32:00+08:00"))
|
|
// the card says the cap is an estimate, and names its own model
|
|
expect(w.name).toBe("deepseek-v4.1-flash tokens (est.)")
|
|
expect(w.unit).toBe("")
|
|
expect(w.models).toEqual(["deepseek-v4.1-flash", "deepseek-v4.1-flash-sg"])
|
|
})
|
|
|
|
// one phone number can be signed in to both builds, and both write to one
|
|
// ledger: each build's read must see its own rows alone, or one build's
|
|
// spending fills the other's window
|
|
test("the two builds' rows stay apart over one account", () => {
|
|
const rows = [
|
|
cnRow("2026-11-02T02:00:00+08:00", { in: 90_000_000 }),
|
|
row("2026-11-02T02:10:00+08:00", { in: 40_000_000 }),
|
|
cnRow("2026-11-02T02:20:00+08:00", { in: 8_000_000, model: "deepseek-v4.1-flash-sg" }),
|
|
]
|
|
const ai = _internal.freeWindowOf(rows, "me@example.com", at("2026-11-02T12:00:00+08:00"))
|
|
expect(ai.amount).toBe(40_000_000)
|
|
expect(ai.limit).toBe(1e8)
|
|
const cn = _internal.freeWindowOf(rows, "me@example.com", at("2026-11-02T12:00:00+08:00"), _internal.CN_ESTIMATED_WINDOWS[0])
|
|
expect(cn.amount).toBe(98_000_000)
|
|
expect(cn.limit).toBe(2e8)
|
|
})
|
|
|
|
// the China build names its reset in Chinese, the time in the same shape and
|
|
// the offset spelled out the same way (UTC+8)
|
|
test("windowReset reads the China refusal", () => {
|
|
expect(_internal.windowReset("将在 2026-11-02 19:17:02 UTC+8 重置")).toBe(Date.parse("2026-11-02T19:17:02+08:00"))
|
|
expect(_internal.windowReset("您的使用量已超出频率限制,将在 2026-11-03 01:14:57 UTC+8 重置,您也可以切换其他模型继续使用。")).toBe(Date.parse("2026-11-03T01:14:57+08:00"))
|
|
// and a time it cannot place is still no edge
|
|
expect(Number.isNaN(_internal.windowReset("将在明天重置"))).toBe(true)
|
|
})
|
|
|
|
// same as the AI probe the review asked for: a refusal at 100M of a 200M
|
|
// window (the account spent elsewhere) reads full, refused, on the Chinese
|
|
// words alone
|
|
test("a China account refused under the cap reads full", () => {
|
|
const rows = [
|
|
cnRow("2026-11-02T02:32:00+08:00", { in: 80_000_000 }),
|
|
cnRefusal("2026-11-02 19:17:02"),
|
|
]
|
|
const w = _internal.freeWindowOf(rows, "me@example.com", at("2026-11-02T12:00:00+08:00"), _internal.CN_ESTIMATED_WINDOWS[0])
|
|
expect(w.amount).toBe(2e8) // the rows sum to 80M
|
|
expect(w.used).toBe(100)
|
|
expect(w.refused).toBe(true)
|
|
expect(w.aside).toBeUndefined() // the vendor's word stops the model
|
|
expect(w.resetsAt).toBe(iso("2026-11-02T19:17:02+08:00"))
|
|
expect(w.display).toBe("200.0M / ~200M")
|
|
})
|
|
|
|
// readLedgerTail stops the chain on both languages' edges: a Chinese refusal
|
|
// whose reset can be read pins where the read is taken from, and the read
|
|
// keeps its own build's rows alone
|
|
test("the China read stops on a Chinese refusal's edge", () => {
|
|
const now = at("2026-11-02T12:00:00+08:00")
|
|
const spent = cnRow("2026-10-29T10:00:00+08:00", { in: 50_000_000 })
|
|
// the refusal's row time sits where it was written: after the spent
|
|
// window's rows, before the reset it names
|
|
const denied = { ...cnRefusal("2026-10-31 08:00:13"), t: "2026-10-30T11:30:00+08:00" }
|
|
const fresh = cnRow("2026-11-02T02:32:00+08:00", { in: 300_000 })
|
|
const root = writeLedger([JSON.stringify(spent), JSON.stringify(denied), JSON.stringify(fresh)])
|
|
try {
|
|
const rows = _internal.freeWindowRows("me@example.com", now, root, {}, join(root, "nohome"), _internal.CN_ESTIMATED_WINDOWS[0])
|
|
// the row that reveals the edge is kept — the read goes back to it and
|
|
// stops — while the window the refusal closed reads nothing: the chain
|
|
// opens at the next answered row, 300k of 200M, resetting 24h later
|
|
const w = _internal.freeWindowOf(rows, "me@example.com", now, _internal.CN_ESTIMATED_WINDOWS[0])
|
|
expect(w.amount).toBe(300_000)
|
|
expect(w.resetsAt).toBe(iso("2026-11-03T02:32:00+08:00"))
|
|
expect(rows.every((r) => r.provider === "workbuddy")).toBe(true)
|
|
// the AI read over the same ledger has nothing answered inside 24h and
|
|
// stops at the day's bound: no window for the other build
|
|
expect(_internal.freeWindowRows("me@example.com", now, root, {}, join(root, "nohome"))).toEqual([])
|
|
} finally {
|
|
rmSync(root, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
// two reads of one ledger, one per build, must not answer for each other
|
|
// through the parse cache: the key carries the build
|
|
test("the parse cache keys the build", () => {
|
|
const now = at("2026-11-02T12:00:00+08:00")
|
|
const lines = [cnRow("2026-11-02T02:00:00+08:00", { in: 90_000_000 }), row("2026-11-02T02:10:00+08:00", { in: 40_000_000 })].map((r) => JSON.stringify(r))
|
|
const root = writeLedger(lines)
|
|
try {
|
|
const cn = _internal.freeWindowRows("me@example.com", now, root, {}, join(root, "nohome"), _internal.CN_ESTIMATED_WINDOWS[0])
|
|
expect(cn.map((r) => r.provider)).toEqual(["workbuddy"])
|
|
const ai = _internal.freeWindowRows("me@example.com", now, root, {}, join(root, "nohome"))
|
|
expect(ai.map((r) => r.provider)).toEqual(["workbuddy-ai"])
|
|
} finally {
|
|
rmSync(root, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
|
|
// a refusal carries no tokens: it neither counts nor opens a window
|
|
test("a 429 neither counts nor opens a window", () => {
|
|
const rows = [row("2026-10-20T19:53:48+08:00", { in: 100 }), row("2026-10-20T22:00:00+08:00", { status: 429, in: 5_000, err: "429 usage exceeds frequency limit" })]
|
|
const w = _internal.freeWindowOf(rows, "me@example.com", at("2026-10-20T23:00:00+08:00"))
|
|
expect(w.amount).toBe(100)
|
|
expect(w.resetsAt).toBe(iso("2026-10-21T19:53:48+08:00"))
|
|
})
|
|
|
|
// a refusal with no readable reset time is no edge; the 24h span still ends
|
|
// the window
|
|
test("a refusal that names no reset falls back to the 24h span", () => {
|
|
const rows = [
|
|
row("2026-10-20T10:00:00+08:00", { in: 99_000_000 }),
|
|
row("2026-10-20T12:00:00+08:00", { status: 429, err: "WorkBuddy AI: usage exceeds frequency limit" }),
|
|
row("2026-10-21T11:00:00+08:00", { in: 1_000 }),
|
|
]
|
|
const w = _internal.freeWindowOf(rows, "me@example.com", at("2026-10-21T12:00:00+08:00"))
|
|
expect(w.resetsAt).toBe(iso("2026-10-22T11:00:00+08:00"))
|
|
expect(w.amount).toBe(1_000)
|
|
})
|
|
|
|
test("nothing to read yields no window, not a made-up one", () => {
|
|
expect(_internal.freeWindowOf([], "me@example.com")).toBeNull()
|
|
expect(_internal.freeWindowOf(null, "me@example.com")).toBeNull()
|
|
expect(_internal.freeWindowOf([row("2026-10-20T19:53:48+08:00")], "nobody@example.com")).toBeNull()
|
|
expect(_internal.freeWindowOf([row("2026-10-20T19:53:48+08:00", { status: 429, err: "429 usage exceeds frequency limit" })], "me@example.com")).toBeNull()
|
|
})
|
|
|
|
test("the card reads a token count, not a credits count", () => {
|
|
expect(_internal.tokensShort(99_971_831)).toBe("100.0M")
|
|
expect(_internal.tokensShort(11_950_000)).toBe("11.9M")
|
|
expect(_internal.tokensShort(812_400)).toBe("812k")
|
|
expect(_internal.tokensShort(6)).toBe("6")
|
|
})
|
|
|
|
test("no ledger means no window rather than a guess", () => {
|
|
// HOME is a temp dir in tests and no folder it reads holds a ledger
|
|
expect(_internal.freeWindowRows("me@example.com", Date.now(), join(homedir(), "magpie-test-nowhere"), {}, homedir())).toBeNull()
|
|
})
|
|
|
|
// --- the review's points, each red on 33ca87f8 -------------------------
|
|
|
|
// point 1: an account refused under the cap read as 93% because the rest of
|
|
// its spending was on another machine. The refusal is the one exact signal
|
|
// there is: a 429 whose named reset is still ahead makes the window full.
|
|
test("an account refused under the cap reads full, not 93%", () => {
|
|
const rows = [
|
|
row("2026-10-18T19:47:00+08:00", { in: 62_000_000, cache: 30_000_000, out: 700_000 }),
|
|
row("2026-10-18T19:52:00+08:00", { in: 92_962, model: "deepseek-v4.1-flash-sg" }),
|
|
row("2026-10-18T19:52:04+08:00", { status: 429, err: `${TIMEOUT_BEFORE_RESET}2026-10-19 19:52:24 UTC+8, alternatively, you can switch to the other models to continue using it.` }),
|
|
]
|
|
const w = _internal.freeWindowOf(rows, "me@example.com", at("2026-10-18T23:00:00+08:00"))
|
|
expect(w.amount).toBe(1e8) // the rows sum to 92,792,962
|
|
expect(w.used).toBe(100)
|
|
expect(w.display).toBe("100.0M / 100M")
|
|
expect(w.resetsAt).toBe(iso("2026-10-19T19:52:24+08:00"))
|
|
expect(w.refused).toBe(true)
|
|
})
|
|
|
|
// the refusal belongs to its window: once its reset passes, the sum reads
|
|
test("a refusal whose reset has passed does not hold the next window full", () => {
|
|
const rows = [
|
|
row("2026-10-18T19:47:00+08:00", { in: 92_000_000 }),
|
|
row("2026-10-18T19:52:04+08:00", { status: 429, err: `${TIMEOUT_BEFORE_RESET}2026-10-19 19:52:24 UTC+8, alternatively, you can switch to the other models to continue using it.` }),
|
|
row("2026-10-19T20:00:00+08:00", { in: 1_000 }),
|
|
]
|
|
const w = _internal.freeWindowOf(rows, "me@example.com", at("2026-10-19T21:00:00+08:00"))
|
|
expect(w.amount).toBe(1_000)
|
|
expect(w.refused).toBeUndefined()
|
|
})
|
|
|
|
// point 6: the offset is read from the refusal, and one that cannot be read
|
|
// is no edge at all rather than +08:00
|
|
test("the reset's offset is read from the text", () => {
|
|
expect(_internal.windowOffset("reset at 2026-10-20 19:47:41 UTC+8")).toBe(480)
|
|
expect(_internal.windowOffset("reset at 2026-10-20 19:47:41 UTC+08:00")).toBe(480)
|
|
expect(_internal.windowOffset("reset at 2026-10-20 19:47:41 GMT-5")).toBe(-300)
|
|
expect(_internal.windowOffset("reset at 2026-10-20 19:47:41 UTC+05:30")).toBe(330)
|
|
expect(_internal.windowOffset("reset at 2026-10-20 19:47:41")).toBeNull()
|
|
expect(_internal.windowReset("reset at 2026-10-20 19:47:41 UTC+8")).toBe(Date.parse("2026-10-20T19:47:41+08:00"))
|
|
expect(Number.isNaN(_internal.windowReset("reset at 2026-10-20 19:47:41"))).toBe(true)
|
|
})
|
|
|
|
// point 3: magpie keeps usage.jsonl in appdir.Config(), which is the folder
|
|
// the plugin host runs in — portable, or $XDG_CONFIG_HOME/magpie — and only
|
|
// ~/.config/magpie otherwise
|
|
test("the ledger is found through $XDG_CONFIG_HOME", () => {
|
|
const root = mkdtempSync(join(tmpdir(), "wb-xdg-"))
|
|
try {
|
|
const home = join(root, "home")
|
|
const xdg = join(root, "xdg")
|
|
mkdirSync(join(home, ".config", "magpie"), { recursive: true })
|
|
mkdirSync(join(xdg, "magpie"), { recursive: true })
|
|
writeFileSync(join(home, ".config", "magpie", "usage.jsonl"), JSON.stringify(row("2026-10-01T10:00:00+08:00", { in: 1 })) + "\n")
|
|
writeFileSync(join(xdg, "magpie", "usage.jsonl"), JSON.stringify(row("2026-10-20T10:00:00+08:00", { in: 5_000_000 })) + "\n")
|
|
const rows = _internal.freeWindowRows("me@example.com", at("2026-10-20T12:00:00+08:00"), join(root, "elsewhere"), { XDG_CONFIG_HOME: xdg }, home)
|
|
expect(rows.map((r) => r.in)).toEqual([5_000_000])
|
|
} finally {
|
|
rmSync(root, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
test("the ledger is found through the folder the host runs in", () => {
|
|
const root = mkdtempSync(join(tmpdir(), "wb-cwd-"))
|
|
try {
|
|
writeFileSync(join(root, "usage.jsonl"), JSON.stringify(row("2026-10-20T10:00:00+08:00", { in: 7_000_000 })) + "\n")
|
|
const rows = _internal.freeWindowRows("me@example.com", at("2026-10-20T12:00:00+08:00"), root, {}, join(root, "nohome"))
|
|
expect(rows.map((r) => r.in)).toEqual([7_000_000])
|
|
} finally {
|
|
rmSync(root, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
// point 4: a heavy ledger runs to hundreds of MB, so the read starts at its
|
|
// end. Where it stops is the chain's business (see the probes below); with no
|
|
// edge in reach it goes back to an edge it can stand on, or to the file's end.
|
|
test("the read starts at the file's end", () => {
|
|
const root = mkdtempSync(join(tmpdir(), "wb-tail-"))
|
|
try {
|
|
const now = at("2026-10-20T20:00:00+08:00")
|
|
const lines = []
|
|
for (let i = 0; i < 3000; i++) lines.push(JSON.stringify(row(new Date(now - (2999 - i) * 3600_000).toISOString(), { in: i === 2999 ? 9_000_000 : 1 })))
|
|
lines.push(JSON.stringify({ ...row(new Date(now).toISOString(), { in: 4 }), provider: "zcode" }))
|
|
writeFileSync(join(root, "usage.jsonl"), lines.join("\n") + "\n" + '{"provider":"workbuddy-ai","t":"2026-10-20T')
|
|
const rows = _internal.freeWindowRows("me@example.com", now, root, {}, join(root, "nohome"))
|
|
expect(rows.every((r) => r.provider === "workbuddy-ai")).toBe(true)
|
|
expect(rows.at(-1).in).toBe(9_000_000) // the newest row, and never the torn one
|
|
} finally {
|
|
rmSync(root, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
// --- the next round: the read must stop on a pinned edge, not on an age --
|
|
|
|
// writeLedger puts rows into a fresh folder's usage.jsonl, one JSON line
|
|
// each, the last one whole — unless torn asks for a call still being written
|
|
function writeLedger(lines, torn = false) {
|
|
const root = mkdtempSync(join(tmpdir(), "wb-window-"))
|
|
writeFileSync(join(root, "usage.jsonl"), lines.join("\n") + "\n" + (torn ? '{"provider":"workbuddy-ai","t":"2026-10-' : ""))
|
|
return root
|
|
}
|
|
|
|
// The window is a chain: it starts at the first request answered after the
|
|
// previous one ended, so where the window now began depends on every window
|
|
// before it. A read that stops after 48 hours hands freeWindowOf a first row
|
|
// that is usually in the middle of a window, and every window after it is
|
|
// shifted — the review's probe read 4% two hours before the account is
|
|
// refused. Two things stand at an edge exactly, and the read stops for both:
|
|
// a refusal whose reset can be read, and a gap of a day between two of this
|
|
// account's answered rows.
|
|
const AN_HOUR = 3600_000
|
|
const A_DAY = 86_400_000
|
|
// spread is rows served every hour from start to now, 4M each
|
|
function spread(start, now) {
|
|
const rows = []
|
|
for (let t = start; t <= now; t += AN_HOUR) rows.push(row(new Date(t).toISOString(), { cache: 4_000_000 }))
|
|
return rows
|
|
}
|
|
// a ledger holding those rows, padded to a live ledger's row size, then
|
|
// another provider's 2000 rows after them, so this account's rows sit
|
|
// further back than one 64 KiB chunk and the 48h cutoff falls between them
|
|
function spreadLedger(rows, now) {
|
|
const lines = rows.map((r) => JSON.stringify({ ...r, pad: FAT }))
|
|
for (let i = 0; i < 2000; i++) lines.push(JSON.stringify(row(new Date(now - 3599_000 + i).toISOString(), { in: 1, provider: "zcode" })))
|
|
return writeLedger(lines)
|
|
}
|
|
|
|
// a day of rows, and the account served every hour after them: the window is
|
|
// whatever the ledger's own chain makes of it, and the tail alone makes
|
|
// something else
|
|
test("a day of rows pins the chain where the whole ledger does", () => {
|
|
const now = at("2026-10-20T20:00:00+08:00")
|
|
// served hourly for a week before, then a gap of 5h, then hourly
|
|
const rows = spread(now - 7 * A_DAY, now - 5 * AN_HOUR).concat(spread(now - 1 * AN_HOUR, now))
|
|
const root = spreadLedger(rows, now)
|
|
try {
|
|
const whole = _internal.freeWindowOf(rows, "me@example.com", now)
|
|
const read = _internal.freeWindowRows("me@example.com", now, root, {}, join(root, "nohome"))
|
|
// the read goes past the 48h tail to the day before where the gap lets
|
|
// the chain be read: the tail alone holds 41 of these rows
|
|
expect(read.length).toBeGreaterThan(41)
|
|
const w = _internal.freeWindowOf(read, "me@example.com", now)
|
|
expect(w.amount).toBe(whole.amount)
|
|
expect(w.resetsAt).toBe(whole.resetsAt)
|
|
} finally {
|
|
rmSync(root, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
// with a refusal to stand at, the chain is anchored exactly: the window
|
|
// opens at the first request answered after the reset it names
|
|
test("a refusal the read reaches pins the window exactly", () => {
|
|
const now = at("2026-10-22T12:00:00+08:00")
|
|
const rows = spread(now - 70 * AN_HOUR, now)
|
|
// the window the 70h row opened was refused once its day ran out
|
|
const reset = now - 70 * AN_HOUR + A_DAY
|
|
const atReset = `${new Date(reset).toISOString().slice(0, 19).replace("T", " ")} UTC+0`
|
|
const refused = [
|
|
...rows,
|
|
row(new Date(now - 46 * AN_HOUR + 60_000).toISOString(), { status: 429, err: `${TIMEOUT_BEFORE_RESET}${atReset}, alternatively, you can switch to the other models to continue using it.` }),
|
|
].sort((a, b) => Date.parse(a.t) - Date.parse(b.t))
|
|
const root = spreadLedger(refused, now)
|
|
try {
|
|
const w = _internal.freeWindowOf(_internal.freeWindowRows("me@example.com", now, root, {}, join(root, "nohome")), "me@example.com", now)
|
|
// the refusal names the window's end; the window holding now opened on
|
|
// the span after it, and the rows read say the same as all of them
|
|
const whole = _internal.freeWindowOf(refused, "me@example.com", now)
|
|
expect(whole.amount).toBe(23 * 4_000_000)
|
|
expect(w.amount).toBe(whole.amount)
|
|
expect(w.resetsAt).toBe(whole.resetsAt)
|
|
} finally {
|
|
rmSync(root, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
// a gap of a day or more ends every window before it, so the row after the
|
|
// gap opens one: the second thing that pins the chain without a refusal
|
|
test("a gap of a day or more pins the chain too", () => {
|
|
const now = at("2026-10-22T12:00:00+08:00")
|
|
// served hourly until 50h ago, then nothing for 30h, then hourly since
|
|
const rows = spread(now - 100 * AN_HOUR, now - 50 * AN_HOUR).concat(spread(now - 20 * AN_HOUR, now))
|
|
const root = spreadLedger(rows, now)
|
|
try {
|
|
const whole = _internal.freeWindowOf(rows, "me@example.com", now)
|
|
// the idle ended every window before it, so the window now is the one the
|
|
// first row after it opened, 20h ago
|
|
expect(whole.amount).toBe(21 * 4_000_000)
|
|
expect(whole.resetsAt).toBe(iso(new Date(now - 20 * AN_HOUR + A_DAY)))
|
|
|
|
const read = _internal.freeWindowRows("me@example.com", now, root, {}, join(root, "nohome"))
|
|
const w = _internal.freeWindowOf(read, "me@example.com", now)
|
|
// the read stops a day before the row after the idle: the 21 rows since
|
|
// it, and the 25 of the day before the gap — not the 100 before them
|
|
expect(read.length).toBe(46)
|
|
expect(w.amount).toBe(whole.amount)
|
|
expect(w.resetsAt).toBe(whole.resetsAt)
|
|
} finally {
|
|
rmSync(root, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
// The review's probe, unchanged: hourly rows for 70h, 4M of cache_read each,
|
|
// six days after the last of them is 2000 rows of another provider, and no
|
|
// refusal anywhere. With no edge to stand at there is no window chain to be
|
|
// read, so the whole ledger and the read agree — and a run of use with no gap
|
|
// and no refusal is exactly that: there is no window edge in it to find.
|
|
test("the review's probe: a run with no edge reads as the whole ledger", () => {
|
|
const now = at("2026-10-22T12:00:00+08:00")
|
|
// rows from two hours before the file's other rows, so the tail read alone
|
|
// would still open the window mid-run
|
|
const rows = spread(now - 70 * AN_HOUR, now - 1 * AN_HOUR)
|
|
const root = spreadLedger(rows, now)
|
|
try {
|
|
const whole = _internal.freeWindowOf(rows, "me@example.com", now)
|
|
const read = _internal.freeWindowRows("me@example.com", now, root, {}, join(root, "nohome"))
|
|
const w = _internal.freeWindowOf(read, "me@example.com", now)
|
|
expect(read.length).toBe(rows.length) // every row: no edge, so the file's start
|
|
expect(w.amount).toBe(whole.amount)
|
|
expect(w.resetsAt).toBe(whole.resetsAt)
|
|
} finally {
|
|
rmSync(root, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
// magpie appends the calls a client reports through POST
|
|
// /v1/magpie/usage/requests to the same ledger (#1874), dated where the
|
|
// client's call happened — a backfill reaching back on purpose
|
|
// (docs/integrating.md). Such a line is written at the file's end, so its
|
|
// time says nothing about where the read has got to: magpie's own index
|
|
// leaves it out of firstOwn for the same reason. It never carries
|
|
// providerAccount, so it is never a row of this account's either.
|
|
test("a backfilled row of a client's does not end the read", () => {
|
|
const now = at("2026-10-22T12:00:00+08:00")
|
|
const rows = spread(now - 70 * AN_HOUR, now - AN_HOUR)
|
|
const reported = (t) => ({ t: new Date(t).toISOString(), agent: "myapp", provider: "openai", model: "gpt-5", in: 10, out: 5, status: 200, rid: "r1", source: "reported" })
|
|
// one, dated a day and an hour ago, and a batch of 50 behind the same run
|
|
for (const backfilled of [[reported(now - 25 * AN_HOUR)], Array.from({ length: 50 }, (_, i) => reported(now - (60 + i) * AN_HOUR))]) {
|
|
const root = writeLedger([...rows, ...backfilled].map((r) => JSON.stringify(r)))
|
|
try {
|
|
const whole = _internal.freeWindowOf(rows, "me@example.com", now)
|
|
const read = _internal.freeWindowRows("me@example.com", now, root, {}, join(root, "nohome"))
|
|
const w = read && _internal.freeWindowOf(read, "me@example.com", now)
|
|
// the card is there, and reads as the whole ledger does
|
|
expect(w).not.toBeNull()
|
|
expect(w.amount).toBe(whole.amount)
|
|
expect(w.resetsAt).toBe(whole.resetsAt)
|
|
// and none of them is this account's row
|
|
expect(read.every((r) => r.providerAccount === "me@example.com")).toBe(true)
|
|
} finally {
|
|
rmSync(root, { recursive: true, force: true })
|
|
}
|
|
}
|
|
})
|
|
|
|
// The chain is this account's own: a refusal of another account's, or of a
|
|
// paid model, says nothing about where this account's window opened
|
|
test("another account's refusal does not pin this account", () => {
|
|
const now = at("2026-10-22T12:00:00+08:00")
|
|
const rows = spread(now - 70 * AN_HOUR, now - 1 * AN_HOUR)
|
|
const root = spreadLedger(
|
|
[...rows, row(new Date(now - 30 * AN_HOUR).toISOString(), { status: 429, account: "b@example.com", err: `${TIMEOUT_BEFORE_RESET}2026-10-22 09:00:00 UTC+8, alternatively, you can switch to the other models to continue using it.` })].sort((a, b) => Date.parse(a.t) - Date.parse(b.t)),
|
|
now,
|
|
)
|
|
try {
|
|
const whole = _internal.freeWindowOf(rows, "me@example.com", now)
|
|
const read = _internal.freeWindowRows("me@example.com", now, root, {}, join(root, "nohome"))
|
|
// the other account's row is not this account's, and is not kept either
|
|
expect(read.every((r) => r.providerAccount === "me@example.com")).toBe(true)
|
|
const w = _internal.freeWindowOf(read, "me@example.com", now)
|
|
expect(w.amount).toBe(whole.amount)
|
|
expect(w.resetsAt).toBe(whole.resetsAt)
|
|
} finally {
|
|
rmSync(root, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
// A ledger under one chunk used to line the chain up by luck: the file's
|
|
// first line was parsed and kept even after the loop had stopped on an older
|
|
// row, so a stray row far back opened the window and the answer was right by
|
|
// accident. It is kept only when the read runs to the file's start.
|
|
test("the file's first line is kept only when the read runs to the start", () => {
|
|
const now = at("2026-10-20T20:00:00+08:00")
|
|
const lines = [
|
|
JSON.stringify(row(new Date(now - 20 * 3600_000).toISOString(), { in: 1 })), // the file's first line
|
|
JSON.stringify(row(new Date(now - 10 * 3600_000).toISOString(), { in: 2 })),
|
|
JSON.stringify(row(new Date(now - 60 * 60_000).toISOString(), { in: 3 })),
|
|
]
|
|
const root = writeLedger(lines)
|
|
try {
|
|
const rows = _internal.freeWindowRows("me@example.com", now, root, {}, join(root, "nohome"))
|
|
// with no edge in reach the read goes back to the start, and then the
|
|
// first line is a row of the ledger like any other
|
|
expect(rows.map((r) => r.in)).toEqual([1, 2, 3])
|
|
} finally {
|
|
rmSync(root, { recursive: true, force: true })
|
|
}
|
|
// a day's gap stops the read with rows under it: the first line is under
|
|
// that stop and is not kept
|
|
const gap = at("2026-10-20T20:00:00+08:00")
|
|
const stopped = writeLedger([
|
|
JSON.stringify(row(new Date(gap - 90 * 3600_000).toISOString(), { in: 1 })), // under the stop
|
|
JSON.stringify(row(new Date(gap - 80 * 3600_000).toISOString(), { in: 2 })), // the gap's earlier side
|
|
JSON.stringify(row(new Date(gap - 50 * 3600_000).toISOString(), { in: 3 })), // 30h later: a window opens
|
|
JSON.stringify(row(new Date(gap - 3600_000).toISOString(), { in: 4 })),
|
|
])
|
|
try {
|
|
const rows = _internal.freeWindowRows("me@example.com", gap, stopped, {}, join(stopped, "nohome"))
|
|
// 3 and 4: the day before the gap's later row stopped the read, and the
|
|
// first line, 10h under it, is not kept
|
|
expect(rows.map((r) => r.in)).toEqual([3, 4])
|
|
} finally {
|
|
rmSync(stopped, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
// The read must be bounded by what it can report, not by how busy the account
|
|
// is. A ledger of another provider's rows, with this account's only row 60
|
|
// days back, is read from its end and stops a day in: a lapsed window is
|
|
// nothing to report, and no row above the day can open one.
|
|
test("a ledger of another provider's rows is not read whole", () => {
|
|
const now = at("2026-10-20T20:00:00+08:00")
|
|
const lines = [JSON.stringify(row(new Date(now - 60 * 24 * 3600_000).toISOString(), { in: 5 }))]
|
|
// a fortnight of another provider's rows, a minute apart: nine days of them
|
|
// are past the day the read stops at
|
|
for (let i = 0; i < 20000; i++) lines.push(JSON.stringify(row(new Date(now - (20000 - i) * 60_000).toISOString(), { in: 1, provider: "zcode" })))
|
|
const root = writeLedger(lines)
|
|
try {
|
|
let chunks = 0
|
|
const path = join(root, "usage.jsonl")
|
|
const got = _internal.readLedgerTail(path, statSync(path), now, () => chunks++, "me@example.com")
|
|
const all = Math.ceil(statSync(path).size / 65536)
|
|
// the read stops a day into the file, so a tenth of it or so, never all
|
|
expect(chunks).toBeLessThan(all / 4)
|
|
expect(chunks).toBeGreaterThan(0)
|
|
expect(got.rows.filter((r) => r.provider === "workbuddy-ai").length).toBeLessThan(3)
|
|
expect(_internal.freeWindowOf(got.rows, "me@example.com", now)).toBeNull()
|
|
} finally {
|
|
rmSync(root, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
// an account used would otherwise be read whole when that account has no
|
|
// refusal and only its own rows to go on: those rows stop the read, at the
|
|
// tail or where the chain is pinned
|
|
test("the read of an account's own rows still stops", () => {
|
|
const now = at("2026-10-20T20:00:00+08:00")
|
|
const lines = []
|
|
for (let i = 6000; i >= 1; i--) lines.push(JSON.stringify(row(new Date(now - i * 60_000).toISOString(), { in: 1 })))
|
|
const root = writeLedger(lines)
|
|
try {
|
|
let chunks = 0
|
|
const path = join(root, "usage.jsonl")
|
|
_internal.readLedgerTail(path, statSync(path), now, () => chunks++)
|
|
// 6000 rows is ~2300 hours of 1-minute rows; only the last 48h + a day
|
|
// of one-hour steps bear on the chain
|
|
expect(chunks).toBeLessThan(800)
|
|
} finally {
|
|
rmSync(root, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
// Two refusals in one chain: the oldest names a window that ended long ago and
|
|
// whose reset is behind us, the newest names one still running. pinned walks
|
|
// the rows oldest to newest and overwrites, so the newest refusal wins — it is
|
|
// nearer to now and its reset is the tighter bound on where the chain can be
|
|
// read from. The old `reset < newest` guard is gone; this pins what replaced it.
|
|
test("of two refusals in the chain, the newest one pins the read", () => {
|
|
const now = at("2026-10-22T12:00:00+08:00")
|
|
const at8 = (h) => `${new Date(now - h * AN_HOUR).toISOString().slice(0, 19).replace("T", " ")} UTC+8`
|
|
const refusalAt = (h, reset) => row(new Date(now - h * AN_HOUR).toISOString(), { status: 429, err: `${TIMEOUT_BEFORE_RESET}${reset}, alternatively, you can switch to the other models to continue using it.` })
|
|
// an old refusal whose reset lapsed 2 days ago, then a live one 6h back
|
|
const old = refusalAt(54, at8(30))
|
|
const live = refusalAt(6, at8(-18))
|
|
const rows = [old, ...spread(now - 54 * AN_HOUR + AN_HOUR, now - 7 * AN_HOUR), live].sort((a, b) => Date.parse(a.t) - Date.parse(b.t))
|
|
expect(rows.at(-1)).toBe(live)
|
|
// pinned's contract: latest first
|
|
const got = _internal.pinned([...rows].reverse(), "me@example.com")
|
|
const liveReset = at(at8(-18).replace(" UTC+8", "+08:00"))
|
|
const oldReset = at(at8(30).replace(" UTC+8", "+08:00"))
|
|
expect(got).toBe(liveReset - 86400_000)
|
|
expect(got).not.toBe(oldReset - 86400_000)
|
|
})
|
|
|
|
// One unreadable row in the middle of the chain: pinned passes over it rather
|
|
// than ending the chain at it, so the bound is the same as the whole chain's.
|
|
// Swap `if (!Number.isFinite(t)) continue` back to `return Infinity` and this
|
|
// fails: the corrupt row becomes the bound, and the read would go back to the
|
|
// file's start instead of stopping where the clean chain stops.
|
|
test("a row whose time cannot be read is passed over", () => {
|
|
const now = at("2026-10-22T12:00:00+08:00")
|
|
// hourly for 100h, then nothing until 20h ago, then hourly: two runs a day
|
|
// or more apart, so the row after the second gap pins the chain
|
|
const clean = spread(now - 100 * AN_HOUR, now - 50 * AN_HOUR).concat(spread(now - 20 * AN_HOUR, now))
|
|
const latestFirst = [...clean].sort((a, b) => Date.parse(b.t) - Date.parse(a.t))
|
|
const bound = _internal.pinned(latestFirst, "me@example.com")
|
|
// a real bound, not the "no edge, read from the start" 0: walked oldest to
|
|
// newest, the first day-wide gap is the one before the 50h row, and that is
|
|
// where the chain can first be read from
|
|
expect(bound).not.toBe(0)
|
|
expect(bound).toBe(now - 50 * AN_HOUR - A_DAY)
|
|
// the same chain with one row in its middle carrying no readable t
|
|
const holey = latestFirst.map((r) => (Date.parse(r.t) === now - 60 * AN_HOUR ? { ...r, t: "not-a-time" } : r))
|
|
expect(holey.some((r) => r.t === "not-a-time")).toBe(true)
|
|
// the same bound, not Infinity and not the 0 of a chain with no edge
|
|
expect(_internal.pinned(holey, "me@example.com")).toBe(bound)
|
|
})
|
|
|
|
// pinning answers each row as pinned would answer the rows handed so far, for
|
|
// as long as readLedgerTail asks (pinned said 0, and the row is not a
|
|
// refusal). Random chains of answered rows, failures, refusals with and
|
|
// without a readable reset, unreadable times and day-wide gaps.
|
|
test("pinning answers as pinned does, row by row", () => {
|
|
let seed = 7
|
|
const rand = () => ((seed = (seed * 1103515245 + 12345) % 2147483648) / 2147483648)
|
|
const now = at("2026-10-22T12:00:00+08:00")
|
|
const at8 = (ms) => `${new Date(ms + 8 * AN_HOUR).toISOString().slice(0, 19).replace("T", " ")} UTC+8`
|
|
let edged = 0
|
|
for (let k = 0; k < 400; k++) {
|
|
let t = now
|
|
const chain = []
|
|
const edge = _internal.pinning()
|
|
let reach = 0
|
|
for (let i = 0; i < 60 && !reach; i++) {
|
|
t -= rand() < 0.08 ? A_DAY + rand() * A_DAY : rand() * 3 * AN_HOUR
|
|
const p = rand()
|
|
const r =
|
|
p < 0.05
|
|
? row(new Date(t).toISOString(), { status: 429, err: `${TIMEOUT_BEFORE_RESET}${at8(t + rand() * A_DAY)}, alternatively, you can switch.` })
|
|
: p < 0.08
|
|
? row(new Date(t).toISOString(), { status: 429, err: "too many requests" })
|
|
: p < 0.12
|
|
? row(new Date(t).toISOString(), { status: 500 })
|
|
: p < 0.14
|
|
? row("not-a-time")
|
|
: row(new Date(t).toISOString())
|
|
chain.push(r)
|
|
const got = edge(r)
|
|
if (Number(r.status) === 429) {
|
|
expect(got).toBe(0)
|
|
continue
|
|
}
|
|
expect(got).toBe(_internal.pinned(chain, "me@example.com"))
|
|
reach = got
|
|
}
|
|
if (reach) edged++
|
|
}
|
|
// most chains met an edge, so both of pinned's answers were compared
|
|
expect(edged).toBeGreaterThan(200)
|
|
})
|
|
|
|
// xmoon76 (Discord, 2026-11-02): with WorkBuddy on, the plugin host sat at
|
|
// 99% CPU and every session through it stalled. readLedgerTail asked pinned
|
|
// about its whole chain after every row, and a chain with no edge — an account
|
|
// a server spends on the free model all day, no refusal and no day's gap —
|
|
// cost the square of its rows: 10.5 s for 16,000 on mote, on the host's one
|
|
// thread, at every usage read. The work is counted here, not timed: every
|
|
// time is read with Date.parse, which a quadratic read calls millions of
|
|
// times for these 6,000 rows and a linear one a few times per line.
|
|
test("a busy account's ledger is read in time linear in its rows", () => {
|
|
const now = at("2026-11-02T12:00:00+08:00")
|
|
const lines = []
|
|
for (let i = 6000; i >= 1; i--) {
|
|
const t = new Date(now - i * 60_000).toISOString()
|
|
lines.push(JSON.stringify(row(t, { in: 1000, out: 100, provider: "workbuddy" })))
|
|
lines.push(JSON.stringify(row(t, { in: 1, provider: "zcode", account: "other" })))
|
|
}
|
|
const root = writeLedger(lines)
|
|
const parse = Date.parse
|
|
let calls = 0
|
|
Date.parse = (s) => (calls++, parse(s))
|
|
try {
|
|
const path = join(root, "usage.jsonl")
|
|
const got = _internal.readLedgerTail(path, statSync(path), now, undefined, "me@example.com", _internal.CN_ESTIMATED_WINDOWS[0])
|
|
expect(got.rows.length).toBe(6000)
|
|
expect(calls).toBeLessThan(lines.length * 4)
|
|
} finally {
|
|
Date.parse = parse
|
|
rmSync(root, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
// plugins#641 (XiaoFeiyu9): a paid China account, 937 answered calls and no
|
|
// 429 on deepseek-v4.1-flash, read as 99% of the 190M soft cap while its
|
|
// credits had 2,457 left. The ledger's sum is an estimate: it is shown, set
|
|
// aside, and never stops the model; only the vendor's refusal does, and
|
|
// magpie's gateway rests the model on it until the reset it names. Both
|
|
// builds, as both read the sum the same way.
|
|
test("a window the vendor hasn't refused is set aside, on both builds (plugins#641)", () => {
|
|
const now = at("2026-11-02T14:00:00+08:00")
|
|
// the reporter's sum, 187,701,367, in a window opened at 11:08:53
|
|
const cn = _internal.freeWindowOf(
|
|
[cnRow("2026-11-02T11:08:53+08:00", { in: 7_701_367, cache: 179_000_000, out: 1_000_000 }), cnRow("2026-11-02T12:00:00+08:00", { in: 0, cache: 0, out: 0, status: 499 })],
|
|
"me@example.com",
|
|
now,
|
|
_internal.CN_ESTIMATED_WINDOWS[0],
|
|
)
|
|
expect(cn.amount).toBe(187_701_367)
|
|
expect(cn.limit).toBe(2e8)
|
|
expect(cn.display).toBe("187.7M / ~200M")
|
|
expect(cn.aside).toBe(true)
|
|
expect(cn.refused).toBeUndefined()
|
|
// a sum over the cap is still only the sum
|
|
const over = _internal.freeWindowOf([row("2026-11-02T11:00:00+08:00", { in: 120_000_000 })], "me@example.com", now)
|
|
expect(over.used).toBe(100)
|
|
expect(over.aside).toBe(true)
|
|
// the same rows with the vendor's refusal stop the model, on both builds
|
|
const cnDenied = _internal.freeWindowOf(
|
|
[cnRow("2026-11-02T11:08:53+08:00", { in: 200_000_000 }), cnRefusal("2026-11-03 11:08:53")],
|
|
"me@example.com",
|
|
now,
|
|
_internal.CN_ESTIMATED_WINDOWS[0],
|
|
)
|
|
expect(cnDenied.refused).toBe(true)
|
|
expect(cnDenied.aside).toBeUndefined()
|
|
const aiDenied = _internal.freeWindowOf([row("2026-11-02T11:00:00+08:00", { in: 100_000_000 }), refusal("2026-11-03 11:00:00")], "me@example.com", now)
|
|
expect(aiDenied.refused).toBe(true)
|
|
expect(aiDenied.aside).toBeUndefined()
|
|
})
|