perf(usage): cut warm usage scans from seconds to milliseconds on large histories (#15149)
Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
This commit is contained in:
1 parent
39efcd8558
commit
71dbaf1f94
9 files changed
+579
-131
No files matched your search
@@ -85,6 +85,8 @@ export const GUARD_LENGTH = 64;
|
||||
// readers; it never discards a record because of its size.
|
||||
const STREAMING_THRESHOLD_BYTES = 8 * 1024 * 1024;
|
||||
const NEWLINE = 0x0a;
|
||||
/** `stat` calls one transcript walk keeps in flight. The libuv pool has 4 threads. */
|
||||
const STAT_CONCURRENCY = 32;
|
||||
const CARRIAGE_RETURN = 0x0d;
|
||||
|
||||
type SelectedFields = { readonly [key: string]: true | SelectedFields };
|
||||
@@ -156,15 +158,19 @@ function fnv1a(buffer: Buffer): number {
|
||||
* `fileName` restricts the walk to a single basename (Grok's `updates.jsonl`).
|
||||
* Grok sessions also ship multi-megabyte `chat_history` and `events` logs that
|
||||
* never carry usage, so the basename filter keeps a cold scan off those files.
|
||||
*
|
||||
* Directories are listed depth-first, one at a time, then the candidates are
|
||||
* stat'd by a fixed pool of workers: a warm scan stats thousands of files, and
|
||||
* one at a time each waits its own trip through the thread pool. Results keep
|
||||
* `readdir` order, which the aggregator's first-seen dedupe relies on.
|
||||
*/
|
||||
export async function listTranscriptFiles(
|
||||
root: string,
|
||||
sinceMs: number,
|
||||
options?: { readonly fileName?: string },
|
||||
): Promise<readonly TranscriptFile[]> {
|
||||
const found: TranscriptFile[] = [];
|
||||
const fileName = options?.fileName;
|
||||
|
||||
const candidates: string[] = [];
|
||||
const walk = async (dir: string): Promise<void> => {
|
||||
let entries;
|
||||
try {
|
||||
@@ -174,28 +180,33 @@ export async function listTranscriptFiles(
|
||||
}
|
||||
for (const entry of entries) {
|
||||
const child = NodePath.join(dir, entry.name);
|
||||
if (entry.isDirectory()) {
|
||||
await walk(child);
|
||||
continue;
|
||||
}
|
||||
if (fileName !== undefined) {
|
||||
if (entry.name !== fileName) continue;
|
||||
} else if (!entry.name.endsWith(".jsonl")) {
|
||||
continue;
|
||||
if (entry.isDirectory()) await walk(child);
|
||||
else if (fileName !== undefined ? entry.name === fileName : entry.name.endsWith(".jsonl")) {
|
||||
candidates.push(child);
|
||||
}
|
||||
}
|
||||
};
|
||||
await walk(root);
|
||||
|
||||
const found: Array<TranscriptFile | undefined> = Array.from({ length: candidates.length });
|
||||
// Each worker pulls the next candidate from one shared iterator.
|
||||
const queue = candidates.entries();
|
||||
const statQueued = async (): Promise<void> => {
|
||||
for (const [index, path] of queue) {
|
||||
try {
|
||||
const stats = await NodeFSP.stat(child);
|
||||
const stats = await NodeFSP.stat(path);
|
||||
if (stats.mtimeMs >= sinceMs) {
|
||||
found.push({ path: child, size: stats.size, mtimeMs: stats.mtimeMs });
|
||||
found[index] = { path, size: stats.size, mtimeMs: stats.mtimeMs };
|
||||
}
|
||||
} catch {
|
||||
// Vanished between readdir and stat.
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
await walk(root);
|
||||
return found;
|
||||
await Promise.all(
|
||||
Array.from({ length: Math.min(STAT_CONCURRENCY, candidates.length) }, statQueued),
|
||||
);
|
||||
return found.filter((file) => file !== undefined);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user