perf(server): scan only appended transcript bytes for usage summaries #9024
MergedStiensWout(GitHub profile, opens in a new tab)merged 3 commits into
main fromrefs/pull/9024/headmerged by t3dotgg(GitHub profile, opens in a new tab) View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XL
- apps/server/src/usage/UsageService.test.ts line 1OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +1 @@1+// @effect-diagnostics nodeBuiltinImport:offapps/server/src/usage/usageTranscriptReader.test.ts line 1OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +1 @@1+// @effect-diagnostics nodeBuiltinImport:offapps/server/src/usage/UsageService.test.ts line 16OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +1,16 @@1+// @effect-diagnostics nodeBuiltinImport:off2+import * as NodeFSP from "node:fs/promises";3+import * as NodeOS from "node:os";4+import * as NodePath from "node:path";5+6+import { assert, describe, it } from "@effect/vitest";7+import * as NodeServices from "@effect/platform-node/NodeServices";8+import { HostProcessEnvironment } from "@t3tools/shared/hostProcess";9+import { UsageDay, type UsageSummaryInput } from "@t3tools/contracts";10+import * as Effect from "effect/Effect";11+import * as Layer from "effect/Layer";12+import { HttpClient, HttpClientResponse } from "effect/unstable/http";13+14+import * as ServerConfig from "../config.ts";15+import * as ServerSettings from "../serverSettings.ts";16+import { make } from "./UsageService.ts";
- apps/server/src/usage/usageTranscriptReader.ts line 170Resolvedby StiensWout(GitHub profile, opens in a new tab)@@ -100,3 +152,17 @@100152 }101153 }102154155+async function guardMatches(156+ handle: NodeFSP.FileHandle,157+ position: TranscriptParsePosition,158+): Promise<boolean> {159+ if (position.guardLength <= 0) return false;160+ const window = Buffer.alloc(position.guardLength);161+ try {162+ const { bytesRead } = await handle.read(163+ window,164+ 0,165+ position.guardLength,166+ position.resumeOffset - position.guardLength,167+ );168+ return bytesRead === position.guardLength && fnv1a(window) === position.guardHash;apps/server/src/usage/usageScanCache.ts line 232Resolvedby StiensWout(GitHub profile, opens in a new tab)@@ -195,6 +212,17 @@195212 dedupeKey: typeof dedupeKey === "string" ? dedupeKey : null,196213 });197214 }215+ return records;216+ };198217199- if (corrupt) continue;200- cache.set(path, { size: entry.s, mtimeMs: entry.m, provider, records });218+ for (const [path, raw] of Object.entries(root.files)) {219+ if (typeof raw !== "object" || raw === null) continue;220+ const entry = raw as Partial<SerializedFile>;221+ if (typeof entry.s !== "number" || typeof entry.m !== "number") continue;222+ if (entry.p !== "claude" && entry.p !== "codex" && entry.p !== "grok") continue;223+ if (!isRecordArray(entry.r) || !isRecordArray(entry.t)) continue;224+ if (225+ typeof entry.o !== "number" ||226+ !Number.isFinite(entry.o) ||227+ entry.o < 0 ||228+ typeof entry.gl !== "number" ||apps/server/src/usage/usageTranscriptReader.ts line 252OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -147,5 +235,18 @@147235 }148-149- if (!mightCarryUsage(line, provider)) continue;150236 const record = parseClaudeLine(line);151- if (record !== null) records.push(record);237+ if (record !== null) out.push(record);238+ };239+240+ const toLineString = (lineBuffer: Buffer): string => {241+ const content =242+ lineBuffer.length > 0 && lineBuffer[lineBuffer.length - 1] === CARRIAGE_RETURN243+ ? lineBuffer.subarray(0, -1)244+ : lineBuffer;245+ return content.toString("utf8");246+ };247+248+ const records: UsageRecord[] = [];249+ // Buffer-level line splitting rather than `readline`, because resuming250+ // needs byte-exact offsets and decoded strings cannot provide them.251+ let resumeOffset = start;252+ let pending: Buffer | null = null;
t3dotgg(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.
macroscopeapp[bot](GitHub profile, opens in a new tab) reviewed