feat(sessions): a timing log for session switches (logging only)

Merged
GR
4d ago
+3004-14

What

A timing log for session switches. Each switch started from the Sessions tab (Enter or click) appends one JSON line to ~/.config/codev/switch-timing.jsonl. Logging only: no switch script, matching order or return value changes.

Why

Switching to a session is slow at times. Before any part is rewritten for speed, this measures where the time goes. The hypothesis to test: the external command (osascript, or the cmux CLI) dominates and grows with machine load, and only time spent in CodeV's own JavaScript could be helped by a rewrite.

What one line holds

  • Step times (ms): click → IPC received, settings read, terminal detection, script built, command spawned, command run, and the total.
  • How the tab was found: tty / title / cwd / none for a switch, read from what the iTerm2, Terminal.app and Ghostty scripts already return on stdout; cmux sets it from its JS matching; a resume records new-tab / new-window / new-workspace (what was asked for — launch scripts return nothing).
  • Machine state: load average, system CPU % over the span, the main process's CPU time, memory-pressure level and swap, the number of running claude processes (from the last live-sessions report, with its age), the session-list length. For a switch over 1 s only: the three busiest processes, by executable name.
  • Which session it was: the full session id, its title, the account label, the project path, the pid switched to and its tty — so per-session patterns show up (a crowded terminal window, one account, a session that always falls back to cwd). The title is the session's /rename name or, when it has none, the summary Claude Code generated for it; the prompts and the transcript text themselves are not recorded. The file is local, and it and its rotated copy are kept owner-only (0600) on every append. Titles and paths can be private, so yarn switch-timing --redact replaces ids, titles, account labels and paths with stable placeholders before a result is quoted anywhere public.

Example (timings from one real switch on a loaded machine; the identifying fields are invented):

{"v":2,"sessionId":"0f1e2d3c-aaaa-4bbb-8ccc-123456789abc","title":"an example title","account":null,"project":"/Users/someone/code/example","pid":4242,"tty":"ttys007","ttySrc":"live-report","via":"click","kind":"switch","terminal":"ghostty","tool":"osascript","branch":"tty","order":"tty-first","ok":true,"timedOut":false,"ms":{"clickToIpc":0,"settings":1.2,"detect":183.8,"build":0.6,"spawn":0.3,"run":462.2,"main":648.1,"total":648.1},"load":[20.9,14.64,11.47],"cpuPct":86,"ncpu":10,"claudeProcs":26,"rows":127}

How

  • src/switch-timing.ts (new, pure): the collector, step deltas, CPU % from two os.cpus() samples, record building, rotation decision.
  • src/switch-timing-log.ts (new, no Electron imports): starts a collector when the IPC arrives; after the switch, samples the machine and appends the line.
  • The collector is an optional last argument on openSession, runCommandInTerminal and the four openSessionIn* functions. Every use is timing?.…, and each collector method swallows its own errors. Callers that pass nothing (saved-list bulk open, new-session launch) are untouched and not timed.
  • On the switch's path: when the IPC arrives, one os.cpus(), one process.cpuUsage() and a clock read; when the record closes, one os.cpus(), one os.loadavg() and one process.cpuUsage(); in between, a few clock reads. No process spawn, no disk.
  • After the switch: sysctl for memory, ps for the busiest processes only when the total exceeds 1 s or the command timed out, the tty (from the last live-sessions report, else one ps -o tty= with a 1 s timeout), and the append. Appends are serialized; the file rotates to .1 at about 1 MiB (one old file kept) and is created owner-only (0600).
  • The active-session paths start osascript and return without waiting, so a record closes when the command exits, or at a 15 s deadline with timedOut: true.
  • yarn switch-timing (scripts/switch-timing-summary.js, plain Node, no dependencies) prints p50/p90 per step by kind, terminal, branch and account, a per-session table (top 20; --all, --sort=p90), the share of the total spent inside the external command, and the total against load. A line that is not a plain object, or whose fields have the wrong type, counts as malformed and is skipped; a missing file is fine; an input that exists but cannot be read is reported and the report is marked incomplete (exit 1); older version-1 lines (which carry only an id prefix) are still read. Percentiles for run, main and total cover completed switches only — the first line counts the timed-out, killed, failed and not-timed ones, each record in one count only.

Verification

  • npx tsc --noEmit: 0 errors. yarn test: 382 tests pass on the branch with main merged in (62 of them are this change's). Rules mutation-checked: rotation boundary, close-on-exit, slow-only ps, owner-only mode on an existing file, control-character stripping in the summary, sampling on a timeout, the ~ path boundary.
  • One real switch in the dev app (yarn start), by clicking a row of a session running in Ghostty: the tab was focused as before, the app log showed found-by-tty, and a line was written with branch: "tty". yarn switch-timing summarized it. That run was made on the first commit (record version 1).
  • A second real switch on the final tree (with main merged in), again a click on a session running in Ghostty: the tab was focused, and the line written was version 2 with the session id, title, account, project, pid and tty filled in (ttySrc: "live-report"), killed and untimed false, and the log file mode -rw-------. yarn switch-timing --redact listed it as session-1 with placeholders for the title and path. Step times: detect 73 ms, command 320 ms, total 395 ms.
  • Not exercised for real: iTerm2, Terminal.app and cmux paths, the resume paths, the Enter key (only the click path was driven), rotation on a real 1 MiB file, and the over-1-s ps sample. The stdout-to-branch mapping for iTerm2 and Terminal.app is tested against the script text, not a live run.

Known limits

  • A timed-out switch (the command never reported its exit within 15 s) has no run, main or total; the summary counts it and keeps it out of the percentiles.

  • account is the account a resume actually runs under (decided from history) on the terminal paths that build a resume command (iTerm2, Ghostty, Terminal.app, cmux); for a switch to a running session, and for VS Code, Claude Desktop and embedded-terminal resumes, it is the row's validated label. Null means no account was resolved; it does not mean the anchor account.

  • A command killed by its 5 s exec timeout is recorded with killed: true and ok: false. Only the osascript paths that run with a timeout set it (the iTerm2, Ghostty and Terminal.app resumes, and the Ghostty and Terminal.app switches). The iTerm2 switch has no exec timeout (a hang there ends as timedOut), and cmux never sets it, so killed: false on a cmux record does not mean the command was not killed.

  • A record is marked untimed: true when no command was started and awaited: switches through VS Code, Claude Desktop or the embedded terminal, and a launch that failed before its command started. Such records have no run, main or total.

  • With no click stamp, total equals main and clickToIpc is null.

  • If the existing log cannot be set to owner-only, the record is dropped rather than written.

  • Not handled: a rotation that happens between the summary's two file reads (the report would miss one generation; running it again fixes it).

  • The first commit's message says "Timings and counts only: no title, prompt or path". That was true of that commit and is no longer true of the branch; the later commit and this description are current.

  • Tab counts are not recorded: only the terminal knows, and asking would cost a second script.

  • cpuPct can be null on a very fast switch, because the counters tick at 10 ms.

  • An active session with no pid already falls into the resume path today; such a record reads kind: "switch", branch: "new-tab".

Version 1.0.97, with a CHANGELOG entry and a "Measuring a switch: the timing log" section in docs/claude-session-integration-design.md. main (1.0.95 and 1.0.96) is merged in, with every CHANGELOG entry kept. The resume command now comes from the shared formatter that 1.0.96 introduced; the log's account is still set where the resume account is decided.

🤖 Generated with Claude Code