* Record usage from a completed stream. Accumulates with existing usage * AND updates lastRequest in a single atomic write. * Model should already be normalized via normalizeToCanonical().
(workspaceId: string, model: string, usage: ChatUsageDisplay)
| 144 | * Model should already be normalized via normalizeToCanonical(). |
| 145 | */ |
| 146 | async recordUsage(workspaceId: string, model: string, usage: ChatUsageDisplay): Promise<void> { |
| 147 | return this.fileLocks.withLock(workspaceId, async () => { |
| 148 | const current = await this.readFile(workspaceId); |
| 149 | const existing = current.byModel[model]; |
| 150 | // CRITICAL: Accumulate, don't overwrite |
| 151 | current.byModel[model] = existing ? sumUsageHistory([existing, usage])! : usage; |
| 152 | current.lastRequest = { model, usage, timestamp: Date.now() }; |
| 153 | await this.writeFile(workspaceId, current); |
| 154 | }); |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Persist derived token stats (consumer + file breakdown) as a cache. |
nothing calls this directly
no test coverage detected