Open History to see the last commit
1// @effect-diagnostics nodeBuiltinImport:off
2/**
3 * Raw filesystem access for transcript scanning.
4 *
5 * Isolated here so the rest of the usage code stays on Effect's `FileSystem`.
6 * The direct `node:fs` streaming is deliberate: a cold 30-day window is ~1.4 GB
7 * across ~1,500 files, and buffer-level streaming is roughly an order of
8 * magnitude cheaper than materialising each file. The equivalent Effect stream
9 * pipeline is idiomatic but not fast enough to sit behind a page load.
10 *
11 * Transcripts are append-only, so a parse also reports the byte position it
12 * stopped at. A later scan of the same file resumes from that position and
13 * parses only the appended bytes, which is what keeps a warm scan cheap while a
14 * session is actively writing a multi-hundred-megabyte rollout.
15 *
16 * @module usageTranscriptReader
17 */
18import * as NodeFSP from "node:fs/promises";
19import * as NodePath from "node:path";
20
21import type { UsageProviderKind } from "@t3tools/contracts";
22
23import {
24 initialCodexScanState,
25 mightCarryUsage,
26 parseClaudeLine,
27 parseCodexLine,
28 parseGrokLine,
29 type CodexScanState,
30 type UsageRecord,
31} from "./usageTranscripts.ts";
32
33export interface TranscriptFile {
34 readonly path: string;
35 readonly size: number;
36 readonly mtimeMs: number;
37}
38
39/**
40 * Where a parse stopped, with enough state to continue from there.
41 *
42 * The guard hash fingerprints the bytes immediately before `resumeOffset`. A
43 * resume only proceeds when those bytes still match: transcripts are
44 * append-only by design, but a rotated or rewritten file silently mis-parsed
45 * from the middle would corrupt usage totals. The window is a cheap tripwire
46 * for those realistic failure shapes, all of which disturb the file's tail at
47 * that exact offset; it deliberately does not hash the whole prefix, which
48 * would cost the full re-read the resume exists to avoid.
49 */
50export interface TranscriptParsePosition {
51 /** Byte offset just past the last newline-terminated line consumed. */
52 readonly resumeOffset: number;
53 /** Length of the fingerprinted window ending at `resumeOffset`. */
54 readonly guardLength: number;
55 /** FNV-1a hash of that window. */
56 readonly guardHash: number;
57 /** Codex reducer state as of `resumeOffset`; `null` for stateless providers. */
58 readonly codexState: CodexScanState | null;
59}
60
61export interface TranscriptParseResult {
62 /** Records from newline-terminated lines at or after the parse start. */
63 readonly records: readonly UsageRecord[];
64 /**
65 * Records from a trailing segment the writer has not newline-terminated yet.
66 * Kept out of `records` because `position` deliberately excludes that
67 * segment: the next scan re-reads it once the writer finishes the line.
68 */
69 readonly tailRecords: readonly UsageRecord[];
70 readonly position: TranscriptParsePosition;
71 /** Whether the parse continued from `resumeFrom` rather than byte 0. */
72 readonly resumed: boolean;
73}
74
75/** 64 bytes of JSONL tail is ample to distinguish a replaced file. */
76export const GUARD_LENGTH = 64;
77const NEWLINE = 0x0a;
78const CARRIAGE_RETURN = 0x0d;
79
80function fnv1a(buffer: Buffer): number {
81 let hash = 0x811c9dc5;
82 for (let index = 0; index < buffer.length; index += 1) {
83 hash ^= buffer[index]!;
84 hash = Math.imul(hash, 0x01000193);
85 }
86 return hash >>> 0;
87}
88
89/**
90 * Lists `.jsonl` transcripts under `root` last modified at or after `sinceMs`.
91 *
92 * Errors on individual entries are swallowed: session files rotate and get
93 * removed while the walk is in flight, and a partial listing is far better than
94 * failing the page.
95 *
96 * `fileName` restricts the walk to a single basename (Grok's `updates.jsonl`).
97 * Grok sessions also ship multi-megabyte `chat_history` and `events` logs that
98 * never carry usage, so the basename filter keeps a cold scan off those files.
99 */
100export async function listTranscriptFiles(
101 root: string,
102 sinceMs: number,
103 options?: { readonly fileName?: string },
104): Promise<readonly TranscriptFile[]> {
105 const found: TranscriptFile[] = [];
106 const fileName = options?.fileName;
107
108 const walk = async (dir: string): Promise<void> => {
109 let entries;
110 try {
111 entries = await NodeFSP.readdir(dir, { withFileTypes: true });
112 } catch {
113 return;
114 }
115 for (const entry of entries) {
116 const child = NodePath.join(dir, entry.name);
117 if (entry.isDirectory()) {
118 await walk(child);
119 continue;
120 }
121 if (fileName !== undefined) {
122 if (entry.name !== fileName) continue;
123 } else if (!entry.name.endsWith(".jsonl")) {
124 continue;
125 }
126 try {
127 const stats = await NodeFSP.stat(child);
128 if (stats.mtimeMs >= sinceMs) {
129 found.push({ path: child, size: stats.size, mtimeMs: stats.mtimeMs });
130 }
131 } catch {
132 // Vanished between readdir and stat.
133 }
134 }
135 };
136
137 await walk(root);
138 return found;
139}
140
141/**
142 * Filesystem identity of a directory, as `device:inode`.
143 *
144 * Used to tell "two servers reading the same transcript directory" apart from
145 * "two machines whose hostname and home path happen to match". Returns an empty
146 * string when the directory cannot be stat'd.
147 */
148export async function readDirectoryVolumeId(path: string): Promise<string> {
149 try {
150 const stats = await NodeFSP.stat(path);
151 return `${stats.dev}:${stats.ino}`;
152 } catch {
153 return "";
154 }
155}
156
157async function guardMatches(
158 handle: NodeFSP.FileHandle,
159 position: TranscriptParsePosition,
160): Promise<boolean> {
161 if (position.guardLength <= 0 || position.guardLength > GUARD_LENGTH) return false;
162 try {
163 const window = Buffer.alloc(position.guardLength);
164 const { bytesRead } = await handle.read(
165 window,
166 0,
167 position.guardLength,
168 position.resumeOffset - position.guardLength,
169 );
170 return bytesRead === position.guardLength && fnv1a(window) === position.guardHash;
171 } catch {
172 return false;
173 }
174}
175
176/**
177 * Streams one transcript and returns the usage records it contains, or `null`
178 * when the file could not be read.
179 *
180 * The distinction matters to the caller's cache: a genuinely empty transcript
181 * is a stable fact worth memoising, while a transient read failure memoised
182 * under the same `(size, mtime)` key would silently drop that file's usage
183 * until the file next changes.
184 *
185 * With `resumeFrom`, parsing continues from that position when its guard bytes
186 * still match, so only appended lines are read; otherwise the whole file is
187 * re-parsed from the start and `resumed` reports `false`.
188 *
189 * Codex carries the active model on `turn_context` lines that hold no usage of
190 * their own, so those still have to pass through the reducer to keep model
191 * attribution correct.
192 */
193export async function readTranscriptRecords(
194 filePath: string,
195 provider: UsageProviderKind,
196 resumeFrom?: TranscriptParsePosition,
197): Promise<TranscriptParseResult | null> {
198 let handle: NodeFSP.FileHandle;
199 try {
200 handle = await NodeFSP.open(filePath, "r");
201 } catch {
202 return null;
203 }
204
205 try {
206 let codexState = initialCodexScanState();
207 let resumed = false;
208 let start = 0;
209 if (
210 resumeFrom !== undefined &&
211 resumeFrom.resumeOffset > 0 &&
212 (provider !== "codex" || resumeFrom.codexState !== null) &&
213 (await guardMatches(handle, resumeFrom))
214 ) {
215 if (resumeFrom.codexState !== null) codexState = { ...resumeFrom.codexState };
216 start = resumeFrom.resumeOffset;
217 resumed = true;
218 }
219
220 const parseLine = (line: string, state: CodexScanState, out: UsageRecord[]): void => {
221 if (provider === "codex") {
222 if (
223 !mightCarryUsage(line, provider) &&
224 !line.includes('"turn_context"') &&
225 !line.includes('"session_meta"')
226 ) {
227 return;
228 }
229 const record = parseCodexLine(line, state);
230 if (record !== null) out.push(record);
231 return;
232 }
233 if (!mightCarryUsage(line, provider)) return;
234 if (provider === "grok") {
235 for (const grokRecord of parseGrokLine(line)) out.push(grokRecord);
236 return;
237 }
238 const record = parseClaudeLine(line);
239 if (record !== null) out.push(record);
240 };
241
242 const toLineString = (lineBuffer: Buffer): string => {
243 const content =
244 lineBuffer.length > 0 && lineBuffer[lineBuffer.length - 1] === CARRIAGE_RETURN
245 ? lineBuffer.subarray(0, -1)
246 : lineBuffer;
247 return content.toString("utf8");
248 };
249
250 const records: UsageRecord[] = [];
251 // Buffer-level line splitting rather than `readline`, because resuming
252 // needs byte-exact offsets and decoded strings cannot provide them.
253 // Newline-free chunks are collected rather than concatenated as they
254 // arrive, so a single huge line costs one copy instead of one per chunk.
255 let resumeOffset = start;
256 let pendingChunks: Buffer[] = [];
257 const stream = handle.createReadStream({
258 start,
259 autoClose: false,
260 }) as AsyncIterable<Buffer>;
261 for await (const chunk of stream) {
262 if (!chunk.includes(NEWLINE)) {
263 pendingChunks.push(chunk);
264 continue;
265 }
266 const buffer: Buffer =
267 pendingChunks.length === 0 ? chunk : Buffer.concat([...pendingChunks, chunk]);
268 pendingChunks = [];
269 let lineStart = 0;
270 for (;;) {
271 const newlineIndex = buffer.indexOf(NEWLINE, lineStart);
272 if (newlineIndex === -1) break;
273 parseLine(toLineString(buffer.subarray(lineStart, newlineIndex)), codexState, records);
274 lineStart = newlineIndex + 1;
275 }
276 resumeOffset += lineStart;
277 if (lineStart < buffer.length) pendingChunks.push(buffer.subarray(lineStart));
278 }
279
280 // A trailing segment without its newline is parsed for this result but not
281 // consumed: a writer may still be appending to it, and counting a half
282 // record now and its full form later would double count.
283 const tailRecords: UsageRecord[] = [];
284 if (pendingChunks.length > 0) {
285 const pending = pendingChunks.length === 1 ? pendingChunks[0]! : Buffer.concat(pendingChunks);
286 if (pending.length > 0) parseLine(toLineString(pending), { ...codexState }, tailRecords);
287 }
288
289 const guardLength = Math.min(GUARD_LENGTH, resumeOffset);
290 let guardHash = 0;
291 if (guardLength > 0) {
292 const window = Buffer.alloc(guardLength);
293 await handle.read(window, 0, guardLength, resumeOffset - guardLength);
294 guardHash = fnv1a(window);
295 }
296
297 return {
298 records,
299 tailRecords,
300 position: {
301 resumeOffset,
302 guardLength,
303 guardHash,
304 codexState: provider === "codex" ? codexState : null,
305 },
306 resumed,
307 };
308 } catch {
309 return null;
310 } finally {
311 await handle.close().catch(() => undefined);
312 }
313}