fix(session): harden usage accounting and memory flush recovery

This commit is contained in:
Peter Steinberger
2026-03-02 00:06:52 +00:00
parent ee96e1751e
commit d729ab2150
9 changed files with 285 additions and 33 deletions

View File

@@ -93,8 +93,11 @@ export async function persistSessionUsageUpdate(params: {
if (hasUsage) {
patch.inputTokens = params.usage?.input ?? 0;
patch.outputTokens = params.usage?.output ?? 0;
patch.cacheRead = params.usage?.cacheRead ?? 0;
patch.cacheWrite = params.usage?.cacheWrite ?? 0;
// Cache counters should reflect the latest context snapshot when
// available, not accumulated per-call totals across a whole run.
const cacheUsage = params.lastCallUsage ?? params.usage;
patch.cacheRead = cacheUsage?.cacheRead ?? 0;
patch.cacheWrite = cacheUsage?.cacheWrite ?? 0;
}
// Missing a last-call snapshot (and promptTokens fallback) means
// context utilization is stale/unknown.