From dd0039ab889fe55cb59d2aba6aaffdf4303e0bc9 Mon Sep 17 00:00:00 2001 From: 41fred <48071138+41fred@users.noreply.github.com> Date: Thu, 9 Jul 2026 08:32:51 -0700 Subject: [PATCH] Add progress indicator and event-loop yielding to cold-cache Claude scan A cold-cache scan over a large ~/.claude/projects tree (hundreds of project dirs, e.g. a git-worktree-per-task workflow) ran long enough to look hung and CPU-heavy enough on a single thread to visibly compete with anything else running interactively. scanProjectDirs now emits a `\r`-updated progress line (TTY-gated, so piped/captured output is untouched) and yields to the event loop every 25 items during both the directory-discovery and file-parsing passes. Verified against a real ~934-dir / ~18k-session cold cache: 51s wall at 60% CPU with visible progress, vs. a previously silent single-threaded block. Warm-cache totals unchanged; full test suite and tsc still pass. --- src/parser.ts | 50 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/src/parser.ts b/src/parser.ts index 4d407b98..90ba82d2 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -1530,6 +1530,8 @@ async function scanProjectDirs( const unchangedFiles: Array<{ filePath: string; dirName: string; cached: CachedFile }> = [] const changedFiles: Array<{ filePath: string; info: FileInfo }> = [] + const discoverProgress = createScanProgress('scanning claude project dirs', dirs.length) + let dirsDone = 0 for (const { path: dirPath, name: dirName } of dirs) { const jsonlFiles = await collectJsonlFiles(dirPath) for (const filePath of jsonlFiles) { @@ -1544,7 +1546,10 @@ async function scanProjectDirs( changedFiles.push({ filePath, info: { dirName, fp } }) } } + dirsDone++ + await discoverProgress.tick(dirsDone) } + discoverProgress.finish() // Pre-seed dedup set from cached (unchanged) files for (const { cached } of unchangedFiles) { @@ -1555,13 +1560,15 @@ async function scanProjectDirs( } } + const parseProgress = createScanProgress('parsing changed claude sessions', changedFiles.length) + let filesDone = 0 for (const { filePath, info } of changedFiles) { delete section.files[filePath] try { const tracker = { lastCompleteLineOffset: 0 } const entries = await parseClaudeEntries(filePath, tracker) - if (!entries) continue + if (!entries) { filesDone++; await parseProgress.tick(filesDone); continue } const turns = groupIntoTurns(dedupeStreamingMessageIds(entries), seenMsgIds) const cwd = extractCanonicalCwd(entries) @@ -1586,7 +1593,10 @@ async function scanProjectDirs( ;(diskCache as { _dirty?: boolean })._dirty = true warnProviderParseFailure('claude', filePath, err) } + filesDone++ + await parseProgress.tick(filesDone) } + parseProgress.finish() if (dirs.length > 0) { for (const cachedPath of Object.keys(section.files)) { @@ -1957,6 +1967,44 @@ function warnProviderParseFailure(providerName: string, sourcePath: string, err: ) } +// A cold-cache scan over a large ~/.claude/projects tree (hundreds of project +// dirs, e.g. a git-worktree-per-task workflow) can run long enough that it +// looks hung, and is CPU-heavy enough on a single thread to visibly compete +// with anything else running interactively on the same machine. Two cheap +// mitigations, neither of which reduces total CPU work: (1) a `\r`-updated +// progress line so a long cold run reads as "working" instead of "stuck", +// gated on isTTY so it never corrupts piped/captured output (export.ts, the +// --no-color path, or a subprocess capturing stderr); (2) yielding to the +// event loop every YIELD_EVERY items so the OS scheduler gets regular break +// points instead of one long uninterrupted synchronous block. This does NOT +// fix CPU contention with a separate process (that's the OS scheduler's job +// regardless), it only keeps this process itself responsive and honest about +// progress during the scan. +const YIELD_EVERY = 25 + +function yieldToEventLoop(): Promise { + return new Promise(resolve => setImmediate(resolve)) +} + +function createScanProgress(label: string, total: number) { + const show = total > 20 && process.stderr.isTTY === true + let lastWrite = 0 + return { + async tick(done: number): Promise { + if (done % YIELD_EVERY === 0) await yieldToEventLoop() + if (!show) return + const now = Date.now() + if (done !== total && now - lastWrite < 100) return + lastWrite = now + process.stderr.write(`\rcodeburn: ${label} ${done}/${total}…`) + }, + finish(): void { + if (!show) return + process.stderr.write('\r\x1b[K') + }, + } +} + async function parseProviderSources( providerName: string, sources: SessionSource[],