Skip to content

Profiler Source Attribution (v2) — file:line for every span, with editor handoff #302

Closed

Description

@nockawa

Summary

Surface the C# file path, line number, and method name of every span's emission site in the Workbench detail panel, with one-click handoff to the user's editor (VS Code / Cursor / Rider / Visual Studio / custom) and an inline react-syntax-highlighter preview.

Design doc: claude/design/Profiler/10-profiler-source-attribution.md

Why a v2

Closes the loop on #293 (closed). The v1 attempt was logically correct end-to-end but predated the #294 typed-event source-generator refactor. Pre-refactor, source attribution required 98 hand-written BeginXxxWithSiteId overload pairs, 98 ref-struct edits, and 98 codec method changes (mostly mechanical, but with enough non-uniform shapes to defeat regex scripts and stall the work).

After #294 landed, the producer side collapses to:

  • One field on TraceSpanHeader (ushort SourceLocationId).

  • One signature change on MakeHeader to thread the literal siteId.

  • One block change in TraceEventGenerator.Emit to emit a sibling BeginXxx_WithSiteId(ushort siteId, args) factory uniformly across all [TraceEvent] structs.

  • One block in TraceEventGenerator.EmitEncoderMethods to write the optional flag-bit + 2 bytes when Header.SourceLocationId != 0.

  • A small finite set of hand-written codecs (EmitEncoder = false events) get the same flag/bytes addition.

Wire format, manifest, Workbench Options, EditorLauncher, inline preview, repo-relative paths — unchanged from v1 and largely lift-and-shift from feature/293-profiler-source-attribution.

Total estimated effort: ~5.5 dev-days (vs. v1's 11.5), with about half being direct port from the v1 branch.

Approach

Capture (build-time): Roslyn IIncrementalGenerator + C# 14 stable interceptors. SourceLocationGenerator discovers every TyphonEvent.Begin* invocation in the Typhon.Engine compilation, sorts by (file, line, column), assigns deterministic ushort site IDs (1-based; 0 = unknown source), emits per-call-site [InterceptsLocation] wrappers that forward to the matching Begin*_WithSiteId overload (also generator-emitted). Zero hot-path cost — literal ushort move; no runtime registry, no shared state, no allocation.

Validated end-to-end on Typhon's exact toolchain (.NET 10 SDK 10.0.203 + Microsoft.CodeAnalysis.CSharp 5.3.0) during v1 — confirms no dotnet/roslyn#82780 regression for our setup.

Wire format: reserve SpanFlags bit 1 (SpanFlagsHasSourceLocation = 0x02); 2-byte u16 SourceLocationId after the optional trace context. Backward-compatible — old readers ignore unknown flags.

Manifest:

  • File: trailer section in .typhon-trace, two new offsets in TraceFileHeader (file format version 5 → 6)

  • Cache: new CacheSectionId.SourceLocationTable, bump ChunkerVersion

  • Live TCP: single FileTable + SourceLocationManifest frame in the init handshake — no delta frames (table is fixed at compile time)

UI:

  • ProfilerDetail.tsx "Source" row — file:line · method

  • Open in editor — POST /api/profiler/open-in-editor → server Process.Start per OS:

    • VS Code: vscode://file/{abs}:{line} (Windows: registry handler; macOS: via open)

    • Cursor: cursor://file/{abs}:{line}

    • Rider: rider*.exe / rider --line N {abs} (no Toolbox / project-name dependency)

    • Visual Studio: devenv /Edit "{abs}" /Command "Edit.GoTo {line}" (Windows-only)

    • Custom: argv template, {file} / {line} / {column} substituted as discrete argv elements (no shell)

  • Show inline — lazy-loaded react-syntax-highlighter (Prism, ~50 KB) renders ±20 lines with the emission line highlighted

Workbench Options system: schema-driven (WorkbenchOptions, EditorOptions, ProfilerOptions), file-backed JSON at OS user-data folder, atomic write + FileSystemWatcher hot-reload (200 ms debounce), GET/PUT/PATCH /api/options, Zustand client store with optimistic-PATCH mutations.

Workspace root: auto-detected by walking up from CWD looking for .git (file or directory — accommodates submodules and worktrees). User can override via Options → Profiler.

Roadmap

7 phases, ~5.5 dev-days end-to-end. Phase 1+2 (engine work) can run in parallel with Phase 5+6+7 (Workbench work) once Phase 3+4 is settled.

  • Phase 1 — Producer side (~1d): TraceSpanHeader.SourceLocationId field; MakeHeader signature; TraceEventGenerator factory pair emission + optional 2-byte field in EmitEncoderMethods; hand-written codec audit + updates; TraceRecordHeader flag/helpers; round-trip tests

  • Phase 2 — Source generator + interceptors (~1d): port SourceLocationGenerator from v1 branch; Directory.Build.props (<TyphonRepoRoot>, <DeterministicSourcePaths>, <PathMap>); <InterceptorsNamespaces> opt-in in Typhon.Engine.csproj; determinism + interceptor sanity tests

  • Phase 3 — File / cache manifest (~1d): port SourceLocationManifestEntry, TraceFileWriter.WriteSourceLocationManifest + RewriteHeader, TraceFileReader.TryReadSourceLocationManifest, TraceFileHeader offsets + version bump (5 → 6); cache section + ChunkerVersion bump; wire FileExporter to emit manifest at session close

  • Phase 4 — Live stream (~0.5d): port LiveFrameType.FileTable + SourceLocationManifest; wire TcpExporter init handshake; Workbench AttachSessionRuntime + useProfilerSourceLocations hook; GET /api/sessions/{id}/profiler/source-locations endpoint

  • Phase 5 — Workbench Options system (~1d, lift-and-shift): WorkbenchOptions, OptionsStore, OptionsController, SystemController; useOptionsStore, OptionsPanel, EditorForm, ProfilerForm; "Open Options" command-palette entry

  • Phase 6 — Source row + editor handoff (~0.5d, lift-and-shift): port EditorLauncher (Windows + macOS adapters, LaunchResult); ProfilerSourceController (workspace-root, open-in-editor, source endpoints with path-traversal guard); useSourceLocationStore; ProfilerDetail.tsx Source row

  • Phase 7 — Inline preview (~0.5d, lift-and-shift): port SourcePreviewPanel.tsx; lazy-loaded react-syntax-highlighter; bundle-split verification

Acceptance

  • Selecting a span in the Workbench shows its emission file:line · method (across all kinds, since the centralized TraceSpanHeader field gives uniform coverage)

  • Open in editor opens the corresponding file at the right line in VS Code, Cursor, and Rider on Windows + macOS (Linux deferred — explicit non-goal in v2)

  • Show inline preview renders syntax-highlighted ±20 lines with the emission line highlighted

  • Site IDs are byte-identical across two builds of the same source (golden-file test)

  • Loading a pre-feature .typhon-trace file (v5 or older) works unchanged via re-record (the v6 bump intentionally invalidates older files; documented behavior)

  • Workbench launched from inside the working tree resolves relative paths automatically (no manual workspace-root config required)

  • Options edited via the Options panel persist across Workbench restarts; out-of-band JSON edits are picked up via hot-reload

  • Per-emission overhead vs current baseline is unmeasurable (literal ushort move only)

Cleanup plan (post-merge)

After this issue is closed and the implementation is on main:

  1. Old design doc — claude/design/observability/09-profiler-source-attribution.md was committed only on the v1 branch (feature/293-profiler-source-attribution) and was never merged to main of claude/. There is therefore nothing to delete from main; the doc disappears when the v1 branch is deleted (next bullet). If the v1 design seems worth preserving on main for historical reference rather than relying on the branch, the claude/CLAUDE.md convention is to copy it to claude/archive/observability/ with a leading note pointing at the v2 doc. Optional.

  2. Old branch feature/293-profiler-source-attribution (Typhon root + claude/) — delete from origin once Phases 5/6/7 have lifted the reusable Workbench code across:

    git -C <typhon-root> push origin --delete feature/293-profiler-source-attribution
    
    git -C claude        push origin --delete feature/293-profiler-source-attribution
    
  3. Obsolete tools — the tools/scripts/convert_typhon_factories.py and ~10 sibling Python scripts on the old branch should not be ported. The post-Refactor TyphonEvent emission: header struct + source generator + codec consolidation #294 architecture eliminates the bulk conversion entirely.

Activity

  1. self-assigned this
    on May 5, 2026
  2. moved this to In Progress in Typhon devon May 5, 2026
  3. added a commit that references this issue on May 5, 2026
  4. moved this from In Progress to Done in Typhon devon May 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Fields

Priority

None yet

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions