@hyperfixation/db 0.1.6 → 0.1.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/schema/ledger.js
CHANGED
|
@@ -5,7 +5,10 @@ export const actionLogStatuses = ["started", "ok", "failed", "uncertain"];
|
|
|
5
5
|
export const hfBudgetPeriod = pgTable("hf_budget_period", {
|
|
6
6
|
period: text("period").primaryKey(),
|
|
7
7
|
budgetUsd: numeric("budget_usd", { precision: 12, scale: 4 }).notNull(),
|
|
8
|
-
|
|
8
|
+
// Scale 6, matching `hf_llm_call.cost_usd`: every settle is `spent_usd = spent_usd + cost`,
|
|
9
|
+
// so a narrower column would round the running total once per call and the rounding would
|
|
10
|
+
// accumulate — drift the design promises is zero, and a cap compared against a wrong total.
|
|
11
|
+
spentUsd: numeric("spent_usd", { precision: 12, scale: 6 }).notNull().default("0"),
|
|
9
12
|
createdAt: timestamp("created_at", { withTimezone: true, mode: "date" })
|
|
10
13
|
.notNull()
|
|
11
14
|
.defaultNow(),
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
ALTER TABLE "hf_budget_period" ALTER COLUMN "spent_usd" SET DATA TYPE numeric(12, 6);--> statement-breakpoint
|
|
2
|
+
ALTER TABLE "hf_budget_period" ALTER COLUMN "spent_usd" SET DEFAULT '0';--> statement-breakpoint
|
|
3
|
+
-- Hand-written: the repair of what the narrow column already rounded away. Every settle is
|
|
4
|
+
-- `spent_usd = spent_usd + cost_usd` in the same transaction that writes the `ok` row, and
|
|
5
|
+
-- nothing else ever writes the column, so a period's `spent_usd` is by design exactly the sum
|
|
6
|
+
-- of its `ok` rows' `cost_usd`. At scale 4 each of those N additions rounded the running total,
|
|
7
|
+
-- so the stored value can sit up to N × 0.00005 away from that sum.
|
|
8
|
+
--
|
|
9
|
+
-- Only rows inside that bound are repaired. A row further out is drift this migration cannot
|
|
10
|
+
-- attribute to rounding, and `reconcile()` reports drift and never corrects it — a counter this
|
|
11
|
+
-- migration silently rewrote would hide exactly what that rule exists to surface.
|
|
12
|
+
UPDATE "hf_budget_period" b
|
|
13
|
+
SET "spent_usd" = l.ledger
|
|
14
|
+
FROM (
|
|
15
|
+
SELECT "period", COALESCE(SUM("cost_usd"), 0) AS ledger, count(*) AS settled
|
|
16
|
+
FROM "hf_llm_call" WHERE "status" = 'ok' GROUP BY "period"
|
|
17
|
+
) l
|
|
18
|
+
WHERE l."period" = b."period"
|
|
19
|
+
AND b."spent_usd" <> l.ledger
|
|
20
|
+
AND abs(b."spent_usd" - l.ledger) <= 0.00005 * l.settled;
|