Skip to content

fix(trace): pair each command.end with its own start by sequence number - #51

Merged
gshost1 merged 5 commits into
mainfrom
fix/trace-command-pairing
Nov 8, 2026
Merged

gshost1 merged 5 commits into
mainfrom
fix/trace-command-pairing

Conversation

@gshost1

@gshost1 gshost1 commented Nov 8, 2026 •

Copy link
Copy Markdown
Owner

The trace reader paired command.start and command.end by command name in one slot per segment and writer pid. FFI host threads in one process can run the same command on one session at overlapping times, so start A, start B, end A, end B reported a false "Unpaired command.end", and start A, start B, end B with A lost hid A's missing end. CLI and MCP are not affected (one pid per command). This pairs each end to its own start. Ledger row STATE-7.

State before and after

before: open[segment:pid:command] = one slot
  start A -> slot = A
  start B -> slot = B          (A overwritten)
  end A   -> slot = empty
  end B   -> slot empty -> false "Unpaired command.end"
  (start A, start B, end B, A lost -> slot empty -> A's lost end hidden)

after: CommandPairing = BySeq(command_seq) | LegacyCount, read from each end
  first pass: collect the starts some end names by command_seq
  start named by an end -> by_seq[segment, pid, seq]   (only that end closes it)
  start not named       -> by_count[segment, pid, command]
  end BySeq(s)          -> remove by_seq[segment, pid, s], else warn "unpaired end"
  end LegacyCount       -> close the oldest start in by_count, else warn "unpaired end"
  left open (by seq)    -> "Unpaired command.start ... (seq N)"
  left open (by count)  -> "... its ends carry no command_seq; paired by count" (a seq is named only when every pairing in that bucket was forced)

Keeping the two kinds apart means an old-format end never closes a start that a new-format end will claim.

What changed

  • Trace writer: returns the seq of the line it wrote. CommandScope keeps its start's seq and writes it on command.end as command_seq (left out if the start was not written). Start and end both go through CommandContext (trace_recorded for the start).
  • crates/core/src/context/command_scope.rs: CommandScope moved out of context/mod.rs (the edit took mod.rs past 400 lines; also one type per file); context declares it pub(crate) mod, and lib.rs re-exports CommandScope (AGENTS.md: only lib.rs re-exports). src/dispatch/scope_end.rs imports it from the crate root.
  • crates/core/src/trace/trace_read/: the pairing above (open_commands.rs, count_bucket.rs); two passes, because one pass would let an old-format end close a start a later end claims by seq (see OpenCommands docs).
  • HTML trace viewer: grouping moved to trace_read/viewer_group.js, which applies the same claimed pre-pass and oldest-first rule as the reader; html.rs loads it before viewer.js.
  • apps/docs/content/docs/guides/traces.mdx: command_seq in the event table and the example line.
  • Tests: TempTrace guard replaces eight copies of temp-trace boilerplate in context_scope_tests.rs.

Trace format: one additive field, command_seq on command.end.

Prior art: none upstream; item 16 of the open upstream issue #602 register.

How tested

Test On main Break that fails it
overlapping_same_command_scopes_pair_without_warning (audit sketch: start, start, end, end; old and new format) fails pairing back to one slot
lost_end_of_an_overlapped_scope_is_reported_against_its_start (both orders) fails reader ignoring claims
a_legacy_end_leaves_alone_a_start_that_a_later_end_claims_by_seq (mixed formats; also the unwritten-start variant) fails reader ignoring claims
legacy_ends_pair_by_count_and_name_a_start_only_when_forced fails bucket never records a forced choice
sequenced_end_does_not_pair_with_a_different_start fails reader ignoring command_seq
overlapping_scopes_name_their_own_start_in_command_end fails emitter dropping the field
viewer_group.test.js (node --test, 5 cases: by seq, lost end, mixed formats, oldest-first, another writer) – viewer dropping the claimed rule; viewer closing the newest start

Gates on head e00bec80 (main 42b97e61 merged in): fmt, clippy -D warnings, source-rule and architecture scripts, core Windows check, Win32 leakage check, cargo tree, host-set tests 4832 passed, 0 failed. Re-cycled onto main 0a5ff966 (fork PRs #47 and #49 changed crates/core/src/lib.rs) as 19bc64c7: gates green (4892 passed, 0 failed), CI 13/13 green. The viewer test passes locally (node --test: 5 pass); CI does not run it yet (see What's left).

Performance

One extra integer per command.end line and one extra pass over the events when reading a trace.

Risks

  • An end whose start was never written (artifact budget hit or a tolerated write failure) has no command_seq and falls back to counting; it is reported as unpaired, as before.
  • Pairing relies on segment plus writer pid being unique; a reused pid inside one segment is not handled (nor was it before).
  • The viewer still nests other events under the most recent open command, so with overlapping commands child events can land under the wrong group; that needs a command id on every event.

What's left

  • CI wiring for the viewer test needs a .github/workflows/ci.yml change, which this machine's token cannot push (no workflow scope). The "Embedded visual viewer tests" step should run node --test src/visual_debug/viewer_filter.test.js crates/core/src/trace/trace_read/viewer_group.test.js.
  • The viewer's event nesting (above).

Merge rule: rule 3. main moved to d921b52 (PR #53: src/tests/cli_process.rs, src/tests/cli_process_support.rs) after this head's CI; neither file overlaps this PR, and neither is an always-overlap file.

The trace reader paired command.start and command.end by
segment:writer_pid:command in one slot. FFI host threads can run the same
command on one session at overlapping times, so start A, start B, end A,
end B reported a false "Unpaired command.end", and start A, start B,
end B with A crashed hid A's lost end.

command.end now carries command_seq, the seq of the scope's own
command.start, and the reader pairs a sequenced end only with that start.
Ends from older traces have no command_seq; they close one open start of
the same segment, writer pid and command, so overlapping legacy scopes pair
by count instead of overwriting each other. The two modes are the explicit
CommandPairing state, held apart in OpenCommands so a legacy end never
closes a start that a sequenced end claims.

The HTML viewer grouped ends by a blind stack pop across writers; it now
closes the frame the same rules pick. The unpaired-start warning names the
start's seq. CommandScope moves to its own file to keep context/mod.rs
under the size cap. The field is additive and documented in the traces
guide. Upstream register item 16 in `vercel-labs#602`.
gshost1 added a commit that referenced this pull request Nov 8, 2026
Review of the command_seq pairing found the viewer and reader disagreeing
and the reader naming a start it could not know.

The viewer's legacy fallback closed any open frame with the same name,
including a start that a later end claims by command_seq. Its grouping
now lives in viewer_group.js with the reader's rules: a claimed pre-pass,
claimed starts close only by seq, legacy ends close the oldest unclaimed
start of the same writer and command. viewer_group.test.js covers it
with node --test, in the same form as viewer_filter.test.js.

Legacy ends closed the newest start, and the unpaired warning then named
that bucket's leftover seq as if it were known. Legacy ends now close the
oldest start, and a bucket latches "ambiguous" once an end had to choose
among several. Its leftovers are reported as paired by count with no seq.
A seq is named only when every pairing was forced.

The claimed set stays a separate pass: a legacy end can come before the
sequenced end that claims a start of the same command, so one pass could
not leave that start alone. The mixed-format test covers that case.

CommandScope is re-exported from lib.rs, as AGENTS.md requires, instead
of from context. Its start goes through a CommandContext method like its
end. The scope tests share a temp-trace guard that removes the file on
drop.
@gshost1
gshost1 merged commit 3fce1eb into main Nov 8, 2026
13 checks passed
@gshost1
gshost1 deleted the fix/trace-command-pairing branch November 8, 2026 13:29
gshost1 added a commit that referenced this pull request Nov 8, 2026
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.

1 participant