refactor(cost): single writer for cost_entries, finishRun reads SUM
Cost was written through three independent paths: worker-runner shipped `finalUsage.costUsd` in run-finished -> finishRun wrote `runs.cost_usd`, `tasks.cost_usd += costUsd`, `agents.spent_monthly_usd += costUsd`, and inserted a per-run aggregate row in cost_entries. CostWriter additionally inserted per-message rows in cost_entries from main. Any CostWriter failure created silent drift between `SUM(cost_entries)` (read by budget enforcement) and `runs.cost_usd` (read by the UI). Make CostWriter the single writer, finishRun the single reader. - Add `db.recordCostEntry(...)` as the first-class insert path for per-message cost rows. CostWriter calls it; tests call it directly. - Remove the aggregate INSERT from finishRun. finishRun now reads `SELECT COALESCE(SUM(amount_usd), 0) FROM cost_entries WHERE run_id=? AND unattributed=0` and writes that as the authoritative run total to runs.cost_usd, tasks.cost_usd, and agents.spent_monthly_usd. - Drop `costUsd` from `finishRun` input, the worker `run-finished` ChildMessage shape, the automation `RunFinishedEvent` type, and every caller + test fixture. Cost is owned by the per-message path. - worker-runner stops extracting `usage.cost.total` — token counts stay for session accumulation, cost comes from pi-ai via CostWriter. - run-finished-helpers now gates budget incident checks on `event.status === "succeeded"` instead of a cost field that no longer exists on the event. - automation-run-completion fires `budget_threshold` on every succeeded run (post-run spent_monthly_usd already reflects the SUM path). - Update budget tests to write a `recordCostEntry` before `finishRun` to simulate the per-message cost path; drop `costUsd` from cross- subsystem, edge-case-budget, database, and related fixtures. One writer, one reader, one source of truth. No drift.
This commit is contained in:
1 parent
77f81a7c5a
commit
14dc9a4897
26 files changed
+102
-93
No files matched your search
@@ -270,7 +270,6 @@ export function processRunCompletion(
|
||||
taskId: run.taskId,
|
||||
agentId: run.agentId,
|
||||
runId: run.id,
|
||||
costUsd: event.costUsd ?? undefined,
|
||||
};
|
||||
try {
|
||||
const agent = db.getAgent(run.agentId);
|
||||
@@ -306,7 +305,10 @@ export function processRunCompletion(
|
||||
dispatchAutomation("run_failed", automationContext);
|
||||
}
|
||||
|
||||
if (event.costUsd && event.costUsd > 0) {
|
||||
// Budget threshold check runs after every succeeded run; finishRun
|
||||
// has already summed cost_entries and updated spentMonthlyUsd, so we
|
||||
// just read the post-run spend and fire if it crosses 80%.
|
||||
if (isSuccess) {
|
||||
try {
|
||||
const agent = db.getAgent(run.agentId);
|
||||
if (agent.budgetMonthlyUsd > 0) {
|
||||
|
||||
@@ -72,5 +72,4 @@ export interface RunFinishedEvent {
|
||||
status: RunStatus;
|
||||
summary: string | null;
|
||||
errorMessage: string | null;
|
||||
costUsd: number | null;
|
||||
}
|
||||
Reference in new issue
Block a user