diff options
| author | Adam Malczewski <[email protected]> | 2026-06-02 13:08:36 +0900 |
|---|---|---|
| committer | Adam Malczewski <[email protected]> | 2026-06-02 13:08:36 +0900 |
| commit | b734eb96bf0af267fdfbef85df51940ca0b4e8c7 (patch) | |
| tree | 3815dc9e569bb57384f53d9042288ff831f02e74 /packages/api/src/routes | |
| parent | 3f629a8469fe483243671e1ca15582a111e96541 (diff) | |
| download | dispatch-b734eb96bf0af267fdfbef85df51940ca0b4e8c7.tar.gz dispatch-b734eb96bf0af267fdfbef85df51940ca0b4e8c7.zip | |
feat: persist per-tab token/cache usage across reload
Persist usage as invisible type:"usage" chunk rows (side channel):
- core: add "usage" ChunkType + UsageData; exclude usage rows from
getChunksForTab/getTotalChunkCount; add getUsageStatsForTab aggregate
(exported from barrel); defensive skip in groupRowsToMessages.
- api: agent-manager accumulates per-attempt usageRows and flushes them in
the same atomic appendChunks call as the turn's content (discarded on a
superseded fallback attempt). GET /tabs enriches rows with usageStats.
- frontend: hydrateFromBackend seeds cacheStats from usageStats (reload only;
no re-seed on statuses reconnect, so no double-count with live events).
Tests: core DB-backed usage persistence/aggregate; api usage-row-per-event +
fallback discard; routes GET /tabs usageStats; frontend hydrate seed +
no-double-count + live-accumulation-after-seed. 495 -> 509 passing.
Diffstat (limited to 'packages/api/src/routes')
| -rw-r--r-- | packages/api/src/routes/tabs.ts | 6 |
1 files changed, 5 insertions, 1 deletions
diff --git a/packages/api/src/routes/tabs.ts b/packages/api/src/routes/tabs.ts index b1e9659..f52ee99 100644 --- a/packages/api/src/routes/tabs.ts +++ b/packages/api/src/routes/tabs.ts @@ -6,6 +6,7 @@ import { getSetting, getTab, getTotalChunkCount, + getUsageStatsForTab, groupRowsToMessages, listOpenTabs, setSetting, @@ -27,7 +28,10 @@ export function setTabsAgentManager( } tabsRoutes.get("/", (c) => { - const tabs = listOpenTabs(); + // Enrich each tab with its persisted usage aggregate so the frontend can + // seed `cacheStats` on reload without an extra round-trip. N small indexed + // queries — fine for tab counts. + const tabs = listOpenTabs().map((t) => ({ ...t, usageStats: getUsageStatsForTab(t.id) })); return c.json({ tabs }); }); |
