* Read current session usage. Returns undefined if file missing/corrupted * and no messages to rebuild from.
(workspaceId: string)
| 339 | * and no messages to rebuild from. |
| 340 | */ |
| 341 | async getSessionUsage(workspaceId: string): Promise<SessionUsageFile | undefined> { |
| 342 | return this.fileLocks.withLock(workspaceId, async () => { |
| 343 | try { |
| 344 | const filePath = this.getFilePath(workspaceId); |
| 345 | const data = await fs.readFile(filePath, "utf-8"); |
| 346 | return JSON.parse(data) as SessionUsageFile; |
| 347 | } catch (error) { |
| 348 | // File missing or corrupted - try to rebuild from messages |
| 349 | if (error && typeof error === "object" && "code" in error && error.code === "ENOENT") { |
| 350 | const messages = await this.collectFullHistory(workspaceId); |
| 351 | if (messages.length > 0) { |
| 352 | await this.rebuildFromMessagesInternal(workspaceId, messages); |
| 353 | return this.readFile(workspaceId); |
| 354 | } |
| 355 | return undefined; // Truly empty session |
| 356 | } |
| 357 | // Parse error - try rebuild |
| 358 | log.warn(`session-usage.json corrupted for ${workspaceId}, rebuilding`); |
| 359 | const messages = await this.collectFullHistory(workspaceId); |
| 360 | if (messages.length > 0) { |
| 361 | await this.rebuildFromMessagesInternal(workspaceId, messages); |
| 362 | return this.readFile(workspaceId); |
| 363 | } |
| 364 | return undefined; |
| 365 | } |
| 366 | }); |
| 367 | } |
| 368 | |
| 369 | /** |
| 370 | * Reset a workspace's persisted cost ledger while keeping copied chat history intact. |
no test coverage detected