Skip to content

perf: single-pass parse for dashboard ranges + correlation scan hoists - #1108

Merged
iamtoruk merged 1 commit into
mainfrom
perf/1106-single-pass
Aug 23, 2026
Merged

iamtoruk merged 1 commit into
mainfrom
perf/1106-single-pass

Conversation

@iamtoruk

Copy link
Copy Markdown
Member

Fixes #1106. Stacked on #1105 (base branch = perf/1104-discovery-sweep; retarget to master after #1105 merges).

What

renderDashboard issued three parseAllSessions calls (scan, plan usages, durable overview) whose ranges differ only in their ends (~1s apart) — the exact-key memo never hit.

  • withSinglePassParse(range, fn): the dashboard declares its widest range; any request inside the callback that is a pure narrowing — same start, end ≤, same month-shard scope — is served by slicing the one parse. month: 2 parses → 1. today: 3 → 2 (plan window starts on the 1st, scan at midnight — different starts cannot merge under the provable rule).
  • correlateCrossProviderPrSessions: per-child sessions.filter → one-shot agentId index; per-candidate launches.filter → sorted array + windowed scan. Match sets identical.
  • vitest.config.ts: exclude .claude/worktrees/** (stale agent worktrees were swept into npm test).

Why not full containment reuse (the sub-second lever)

Deliberately NOT shipped: a parse's file set depends on its range via two mechanisms — the fp.mtimeMs < dateRange.start skip and monthScopeForRange shard selection. A wider parse reads files a narrower one never sees, and seenKeys/seenMsgIds seed before turnSlicedToRange — so a wider parse can dedup-drop an in-range turn a narrower parse counts. That's a loss, not an attribution shift; it's the same reason burstReuse pins startMs. (Empirically: 6mo-parse-then-slice was byte-identical to a direct today-parse on this corpus — promising but unproven; left as a candidate on the issue.)

Parity (frozen APFS-clone corpus, isolated caches, vs #1105 base)

Byte-identical except /generated: warm today/month/models/all/sessions/status JSON; cold same set + per-provider; rendered TUI stdout for today and month; and a 16-case harness over the three dashboard-derived numbers (period × provider × scrollable-history) — 16/16 identical incl. carriedCostUSD and per-session attribution. (Run under TZ=Etc/GMT+12 to keep 'today' non-empty against the frozen corpus.)

Perf (isolated, median of 5 interleaved)

command #1105 base this PR
today (TUI) 5.12s 3.81s (1.34x)
month (TUI) 5.46s 3.41s (1.60x)
today --format json 4.11s 3.49s (1.18x)

Live-corpus spot check: warm today ~3.97s (was 9.3s on main before #1105+#1106).

Tests

New tests/single-pass-parse.test.ts (5, spy-counted parses — non-vacuous): slice-vs-direct equality, boundary straddling, start mismatch re-parses, month-scope mismatch re-parses, no scope leak. Full suite green (232 files / 3182 tests). No cache version, payload, or dep changes.

…lation loops

renderDashboard issued three parseAllSessions calls (scan range, plan window,
durable headline). They often share a START and differ only in where they end —
end-of-day vs each caller's own `new Date()` — which the exact-key memo cannot
match, so a warm render paid for the discovery sweep, the cache read and the
parse two or three times over.

withSinglePassParse lets a caller declare the widest range it will ask for. A
later request that is a PURE NARROWING of it is served by slicing that parse
instead of running the pipeline again. Pure narrowing is deliberately strict:
same start, an end that is inside, and the same month shard scope. Both extra
conditions are load-bearing, because a parse's file set is a function of its
range:
  - a changed file with `mtimeMs < range.start` is skipped without being parsed,
    so an earlier start reads files a later start never sees;
  - loadCache reads only the shards monthScopeForRange selects;
  - either way the extra files seed seenKeys/seenMsgIds BEFORE the range slice,
    and a seeded key suppresses the matching in-range turn in a provider parsed
    later — usage the narrower parse would have counted.
Holding start and month scope equal makes both parses see an identical file set,
leaving the range slice as the only difference — the same trade burstReuse
already makes for the mirror-image case (same start, later end).

Independently, correlateCrossProviderPrSessions ran two O(n*m) scans: a
`sessions.filter` per subagent child (now a one-shot agentId index) and a full
`launches.filter` per candidate (now a sorted array plus a windowed scan). Both
preserve the exact match set; launch order was never observable because the
matches collapse into a Map keyed by the sorted ref list.

Warm, 21k-file corpus, isolated cache, median of 5 interleaved:
  today  5.12s -> 3.81s (1.34x)   3 parses -> 2
  month  5.46s -> 3.41s (1.60x)   2 parses -> 1
  today --format json  4.11s -> 3.49s (1.18x, correlation hoist only)

vitest.config gains an explicit exclude so a .claude worktree's stale tests/
copy stops being swept into the run.

Refs #1106
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf: renderDashboard runs the full parse pipeline 3x per command — collapse to one pass

1 participant