Skip to content

Workbench profiler — unify live and replay pipelines at the cache layer #289

Closed

Description

@nockawa

Summary

The live (Attach) and replay (Trace) profiler pipelines diverge at the server-ingestion stage and re-converge only at the renderer, with the live path doing all aggregation client-side. This causes a maintenance tax (two rendering paths, duplicated salvage/filter logic, mode-specific eviction policies) and produces concrete user-visible bugs (live p95 always 0, "all bars over P95", a per-batch ring buffer that strands long captures, missing scrollbar/wheel because of accumulated client-side state hacks).

Refactor TraceFileCacheBuilder from a static one-shot file-scanner into an incremental instance, drive it from both file (replay) and TCP-stream (live) sources, and move all aggregation to the server in both modes. The pipeline downstream of the builder becomes mode-agnostic.

Goal

After this refactor the two modes share one server-side cache representation (tickSummaries[], chunkManifest[], folded chunk data) and one client-side rendering path. Mode-specific code is confined to the source side of the builder (file vs TCP) and the growth-vs-static nature of the cache (live grows; replay sealed at session open).

Design doc

Full pipeline analysis + phase-by-phase plan: claude/design/Profiler/08-profiler-live-replay-unification.md.

Non-goals

  • Engine wire-protocol change (Init / Block / Shutdown frames stay).

  • Persisting live sessions across workbench restarts (live cache is ephemeral).

  • Multi-client live sharing.

  • Backwards compatibility with the existing live SSE protocol — the workbench is the sole consumer.

Phases

  • Phase 1 — Refactor TraceFileCacheBuilder into an instance class. Lift method-locals to fields, expose FeedRawRecords / FlushCurrentChunk / FlushTrailingTick / Dispose and TickFinalized / ChunkFlushed events. Introduce ICacheChunkSink with FileCacheSink (replay) and AppendOnlyChunkSink (live) implementations. Static Build(sourcePath, cachePath) preserved as a thin wrapper for zero-risk replay parity.

  • Phase 2 — Live cache temp file lifecycle. LiveCacheTempFile owning %TEMP%/typhon-workbench/{sessionId}.cache. Stale-file cleanup at SessionManager startup. Configurable disk-quota cap.

  • Phase 3 — Rewrite AttachSessionRuntime. Init frame: parse metadata + initialize builder. Block frame: decompress + feed raw records to builder. Shutdown: flush+dispose. Drop LiveTickBatch / LiveTraceEvent / EmitTickBatch / pendingTicksRef. Pre-tick events flow through builder's existing preTickBuffer.

  • Phase 4 — SSE delta protocol. Replace tick payload with tickSummaryAdded (coalesced 100 ms), chunkAdded (on builder flush), globalMetricsUpdated (1 Hz). Heartbeat unchanged. Reconnect = full metadata snapshot replay.

  • Phase 5 — Server chunk endpoint. Introduce IChunkProvider; both TraceSessionRuntime and AttachSessionRuntime implement. Controller becomes one-liner. Replay reads from sidecar via existing TraceFileCacheReader; live reads from temp file using in-memory manifest entry.

  • Phase 6 — Client refactor. Delete liveAssembled useMemo, recentTicks ring + LIVE_TICK_BUFFER_CAP, liveTickCacheRef + LIVE_TICK_CACHE_CAP, liveThreadNamesRef, LiveTickBatch / LiveTraceEvent. useProfilerCache collapses to one branch. TickOverview.tickRows always reads metadata.tickSummaries. Store gains appendTickSummary / appendChunkEntry actions for SSE deltas. chunkCache.ts / OPFS / ensureRangeLoaded untouched.

  • Phase 7 — Edge cases. In-flight chunk visibility (200 ms force-flush timer). Tick-finalization lag (250 ms fallback timer using event timestamps). Reconnect handling (engine resends Init; session continues if tables match). Long-session disk cap. PreTickBuffer cap. Multi-session coexistence by sessionId. Dropped TickEnd salvage (already in builder; client-side salvage code deleted). ThreadInfo migration to metadata.threadNames.

  • Phase 8 — Test plan (11 tests across unit/integration/e2e tiers — see design doc).

  • Phase 9 — Smoke test: /wb-dev start → AntHill --live 9100 --live-wait 30000 → workbench attach → wheel pans, scrollbar appears, p95 has a real value, no "all bars over P95".

Bugs this fixes for free

  • "All live bars over P95" — server now computes p95 in both modes.

  • The "147 visible bars" eviction we patched at runtime — the per-tick cap on the client cache disappears (chunk cache evicts by viewRange, same as replay).

  • ThreadInfo replay on reconnect / SSE late-join — already in metadata.

  • Tick-summary aggregator (max system duration, active-systems bitmask) becomes correct in live (impossible client-side without seeing all events).

Risks / things to watch

  • In-flight chunk visibility — only place without precedent in the existing codebase. Mitigation: builder exposes FlushCurrentChunk() for a 200 ms timer in AttachSessionRuntime. Chunk only emitted to client AFTER flush to disk.

  • Tick finalization lag at low tick rates — builder finalizes tick N when TickStart of N+1 arrives. 250 ms fallback timer uses event timestamps. Re-finalization on late records is rare; logged.

  • Long captures hitting disk quota — surface SSE warning + stop accepting new blocks. Workbench is a developer tool; multi-day live captures are a different problem.

  • Memory & disk lifecycle — temp file on session dispose; SessionManager startup sweep for crash-orphans.

Net code movement

Roughly −250 LOC client-side aggregation deleted, −150 LOC AttachSessionRuntime tick-batching deleted, +200 LOC builder refactor + sink interface + temp file lifecycle. Net ≈ −200 LOC plus one less rendering path.

Out of scope (follow-ups)

  • "Save live capture" — write trailer sections on dispose so live temp files become reopenable as .typhon-trace-cache.

  • Server-side LRU on chunks (not justified for a single-user dev tool).

  • Multi-hour live monitoring with disk rotation.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    refactoring

    Type

    No type

    Fields

    Priority

    None yet

    Area

    Workbench

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions