MCPcopy
hub / github.com/coder/mux / getSessionUsage

Method getSessionUsage

src/node/services/sessionUsageService.ts:341–367  ·  view source on GitHub ↗

* Read current session usage. Returns undefined if file missing/corrupted * and no messages to rebuild from.

(workspaceId: string)

Source from the content-addressed store, hash-verified

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.

Callers 7

refreshSessionUsageMethod · 0.80
removeMethod · 0.80
streamMessageMethod · 0.80
routerFunction · 0.80

Calls 7

getFilePathMethod · 0.95
collectFullHistoryMethod · 0.95
readFileMethod · 0.95
withLockMethod · 0.80
readFileMethod · 0.65
parseMethod · 0.45

Tested by

no test coverage detected