feat(telemetry): record per-agent usage for mission turns
The command centre's SPEND, ACTIVITY and THROUGHPUT cards read `usage_events`, and nothing on the mission path ever wrote a row: `cm_billing::charge` was called only from the agent-run path. Measured mid-mission with 14 agents live, `usage_events` was 0 while a crew had just burned 15k tokens — so an agent that had done real work reported zero cost and zero activity. The worker already knew everything needed: it logs node, role and token count per step, and the node's `attrs.agent` carries the `claw_<uuid>` binding the runtime dispatches on. This routes that to the ledger. `charge`'s run_id is now Option. `usage_events.run_id` references `agent_runs`, and a topology turn has no row there — passing its `topology_runs` id was a foreign-key violation, which is exactly what the first attempt hit. NULL is the honest value; the agent-run caller still passes its real id. The executor reports one total rather than an in/out split, so the cost is right (credits price the sum) and the columns record it as output rather than inventing a split. Verified end to end on a real mission: 4 agents, 1046-8670 tokens each, credits attributed per agent, and the SPEND/ACTIVITY queries now return real numbers. Co-Authored-By: Claude Opus 5 <[email protected]>
This commit is contained in:
co-authored by
Claude Opus 5
parent
8ef7067467
commit
8be7b3c9b2
@@ -62,7 +62,7 @@ async fn charges_span_lots_oldest_first_and_record_usage() {
|
||||
.unwrap();
|
||||
|
||||
// 2500 tokens → 3 credits: drains the first lot (2) then one more.
|
||||
let deducted = charge(&pool, ws.id, agent.id, run_id, 1500, 1000)
|
||||
let deducted = charge(&pool, ws.id, agent.id, Some(run_id), 1500, 1000)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(deducted, 3);
|
||||
@@ -96,7 +96,7 @@ async fn an_empty_workspace_records_usage_but_clamps_at_zero() {
|
||||
.unwrap();
|
||||
|
||||
// Owes 5, only 1 available: deducts 1, balance hits zero, never negative.
|
||||
let deducted = charge(&pool, ws.id, agent.id, run_id, 4000, 500)
|
||||
let deducted = charge(&pool, ws.id, agent.id, Some(run_id), 4000, 500)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(deducted, 1);
|
||||
|
||||
Reference in New Issue
Block a user