Repository navigation
Keep running session totals out of the token columns - #3015
Conversation
Droid and Mastra Code write lifetime usage snapshots that are re-sent as they grow. Locally each replaces the last, but every revision is exported, so summing the token columns on the server counted a session once per revision. Their counts now stay in attributes.usage only, so the columns hold only counts that can be summed.
The droid_capture smoke still read the snapshot's output_tokens column, which is NULL now, so it failed. It reads attributes.usage and checks the columns are NULL. Add a Droid test for the columns and a backfill test that leaves session_usage rows NULL while it fills an ordinary row.
|
Final review at head 11708ca: LOW, clean. Pushed (tests only, normal push 0004979..11708ca). The droid_capture smoke failed at 0004979. Its usage step still expected the snapshot's totals in the token columns. It now asserts NULL columns and reads the totals from Writers. Every path that fills the columns goes through Readers.
Already-written 1.53.0 rows (author's choice, not blocking). A local row keeps its filled columns until a newer revision of the session replaces it. Revisions already exported keep theirs. A column sum over 1.53.0 data counts such a session once per revision. Leaving them costs nothing and the overcount ages out. Nulling them once would need migration code on both client and server. LLPs. 0590, 0598 and 0603 are all Draft. Release note (next release). Droid and Mastra Code session totals now stay in Privacy: LOW. The counts are still kept and exported in CPU and memory: one string compare per row on the write path and in the backfill. No allocation or growth. No concern. Checks: full Server-suite gate: hypaware-server master 3a01876 against this client head 11708ca: typecheck clean, vitest 329 files and 704 tests, all pass. |
|
Landing note (HypForge orchestrator): enqueuing at 11708ca. Kenny handed this PR over with
Two points left as they are unless the author says otherwise:
To do before the next client release, not in this PR:
|
|
Merged (HypForge orchestrator) as 19f3cb0 at 10:02:42Z on 2026-11-17, on the first pass through the merge queue. CI on the merge commit is green.
|
|
Correction (HypForge orchestrator) to point 1 of the landing note above. I wrote that leaving the rows already written with filled token columns "costs nothing and ages out". That is wrong for a server once hypaware-server #1724 deploys. What a server holds:
What #1724 does with them (read at its head aec6ad7a): What is not known: whether production holds any such rows. No HypForge seat can read production. This read-only query answers it; any row where SELECT org, date, conversation_source, COUNT(*) AS revisions, COUNT(DISTINCT message_id) AS sessions
FROM ai_gateway_messages
WHERE provider_subtype = 'session_usage' AND output_tokens IS NOT NULL
GROUP BY org, date, conversation_sourceOptions, the author's choice:
Nothing in this PR needs to change for any of these. |
Problem
The token columns from LLP 0590 should be safe to sum:
sum(output_tokens)over any rows should count each token once. Two adapters break that. Droid and Mastra Code write asession_usagerow holding a session's lifetime totals, and re-send it as the totals grow. Locally each revision replaces the last, but every revision is exported, so a server-side sum counts the session once per revision. The server usage rollup works around this with Droid-specific code that picks the newest revision fromattributes, and it ignores Mastra Code entirely.Fix
withUsageColumnsand the column backfill leave the token columns NULL on anyprovider_subtype = 'session_usage'row. The counts stay inattributes.usage, unchanged.This lets readers sum the columns with no role filters or per-client code. A follow-up server PR rewrites the usage rollup as a single
GROUP BY.Turning these totals into increments was considered. It needs durable per-session state the adapters do not keep (Droid's live cursors are in memory, capped at 64 files), so the totals stay out of the columns instead. Droid and Mastra Code usage drop out of column sums.
Changes
ai-gateway/src/dataset.js:rowUsagereturns nothing forsession_usagerows.#additive, which states the rule. The Draft specs LLP 0598 (Droid) and LLP 0603 (Mastra Code) and the two integration docs are updated to match.attributes.usageand assert that the columns are NULL.Not changed
hypoverview still readsattributes.usage(LLP 0590 rollout step 3).Checks
npm test: 9,731 pass, 0 fail.npm run typecheck: clean.