Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ const usage = (env) => env.USAGE.get(env.USAGE.idFromName("usage"));

// Hourly usage per seat, model and surface (setkernel-live#66), and each seat's newest limit readings
// (setkernel-live#88). Only the Worker calls it, over RPC, with what cleanUsage or cleanLimits has
// already checked. A bucket pushed again replaces the stored one, so a pusher can resend an hour's
// running totals; a limit reading replaces the stored one only if it is as new or newer.
// already checked. A push replaces whole hours (see push), so a pusher can resend an hour's running
// totals, even under another seat; a limit reading replaces the stored one only if it is as new or newer.
export class Usage extends DurableObject {
constructor(ctx, env) {
super(ctx, env);
Expand Down Expand Up @@ -192,16 +192,20 @@ export class Usage extends DurableObject {
return this.sql.exec(`SELECT hour, seat, ${sums} FROM buckets WHERE ${where} GROUP BY hour, seat ORDER BY hour, seat`, ...params).toArray();
}

// Stores the buckets, deletes hours older than KEEP_DAYS, and answers with the stored totals of
// the hours pushed, per Halifax day and seat, for the pusher to compare.
// A push replaces whole hours: it deletes every stored bucket of each hour it names, whatever the
// seat, model or surface, then stores the buckets, so a bucket a later push leaves out (an hour first
// pushed as seat `other`, then as `polaris`) does not linger. Hours it does not name are untouched.
// Also deletes hours older than KEEP_DAYS, and answers with the stored totals of the hours pushed,
// per Halifax day and seat, for the pusher to compare.
push(buckets, now) {
const cols = ["hour", "seat", "model", "surface", ...COUNTS];
const insert = `INSERT OR REPLACE INTO buckets (${cols.join(", ")}) VALUES (${cols.map(() => "?").join(", ")})`;
const hours = JSON.stringify([...new Set(buckets.map((b) => b.hour))]);
this.ctx.storage.transactionSync(() => {
this.sql.exec("DELETE FROM buckets WHERE hour IN (SELECT value FROM json_each(?))", hours);
for (const b of buckets) this.sql.exec(insert, ...cols.map((c) => b[c]));
this.sql.exec("DELETE FROM buckets WHERE hour < ?", keepFrom(now));
});
const hours = JSON.stringify([...new Set(buckets.map((b) => b.hour))]);
return { stored: buckets.length, days: sumByDay(this.#rows("hour IN (SELECT value FROM json_each(?))", hours)).days };
}

Expand Down
11 changes: 11 additions & 0 deletions test/worker/usage.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,17 @@ describe("usage", () => {
]);
});

it("replaces every stored bucket of an hour pushed again, even under another seat", async () => {
const H = "2026-11-01T14";
await push({ buckets: [bucket(H, "other", 4), bucket("2026-11-01T15", "other", 1)] });
const again = await push({ buckets: [bucket(H, "polaris", 6)] });
expect(again.json).toEqual({ stored: 1, days: { "2026-11-01": { polaris: counts(6) } } });
expect(await stored()).toEqual([
{ hour: H, seat: "polaris", model: "claude-opus-5-5", input: 6, requests: 36 },
{ hour: "2026-11-01T15", seat: "other", model: "claude-opus-5-5", input: 1, requests: 6 },
]);
});

it("puts 03:00 UTC on the day the clocks go forward on the previous Halifax day", async () => {
const pushed = await push({ buckets: [bucket("2026-03-08T03", "lyra", 1), bucket("2026-03-08T04", "lyra", 2), bucket("2026-03-01T03", "lyra", 4)] });
expect(pushed.json.days).toEqual({ "2026-02-28": { lyra: counts(4) }, "2026-03-07": { lyra: counts(1) }, "2026-03-08": { lyra: counts(2) } });
Expand Down
Loading