Skip to content

sim: port the .bctrace TraceWriter (NDJSON trace subsystem)#67

Merged
ch4r10t33r merged 1 commit into
mainfrom
feat/sim-trace-writer-59
Jul 3, 2026
Merged

sim: port the .bctrace TraceWriter (NDJSON trace subsystem)#67
ch4r10t33r merged 1 commit into
mainfrom
feat/sim-trace-writer-59

Conversation

@ch4r10t33r

Copy link
Copy Markdown
Collaborator

Closes #59.

Ports the Go sim's .bctrace trace subsystemsim/trace_writer.go TraceWriter — to src/sim/trace_writer.zig. There was no Zig counterpart (trace matched nothing under src/).

What it does

A compact NDJSON writer:

  • header line (v, t0, nodes, topology, config, optional decoderName/peer_ids)
  • one event tuple per record: [usec, node, ev, args…]
  • footer with total duration and a 100 ms time index ({ts, byte-offset} pairs)

Byte layout, field order, event-row shape, and the index cadence match the Go reference, locked down by golden-byte tests captured from the Go writer (same inputs as Go's TestTraceWriter_RoundTrip), including the JSON string escaping (", \, control chars, and the </>/& HTML-escape that encoding/json applies by default).

Adaptations (documented in the module header)

  • Output accumulates into an owned byte buffer (bytes()) rather than Go's io.Writer + bufio sink — observable bytes are identical.
  • t0 / topology / config enter as caller-provided raw values (RFC3339 string + raw JSON), since the Zig sim has no time.Time or reflective json.Marshal. The event-code table for the TracingObserver wrapper is exported for a follow-up once the broadcast Observer interface lands (broadcast: implement Observer interface callbacks + SessionRole enum #58).

Verification

zig fmt --check and zig build test pass on stock Zig 0.16.0 (7 golden/behaviour tests in the module).

Ports `sim/trace_writer.go`'s `TraceWriter` to `src/sim/trace_writer.zig`:
a compact NDJSON `.bctrace` writer — header line, one JSON-array tuple
per event, and a footer carrying a 100 ms time index. Byte layout,
field order, event-row shape, and index cadence match the Go reference,
locked down by golden-byte tests captured from the Go writer.

Adaptations (documented in the module): output accumulates into an owned
byte buffer rather than Go's `io.Writer`+`bufio` sink, and `t0` /
`topology` / `config` enter as caller-provided raw values (the Zig sim
has no `time.Time` or reflective `json.Marshal`). The event-code table
for the `TracingObserver` wrapper is exported for a follow-up once the
broadcast `Observer` interface lands (#58).

Refs #59.
@ch4r10t33r ch4r10t33r merged commit 1bf8fb9 into main Jul 3, 2026
7 checks passed
@ch4r10t33r ch4r10t33r deleted the feat/sim-trace-writer-59 branch July 3, 2026 07:31
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.

sim: port the .bctrace trace subsystem (TraceWriter + TracingObserver)

1 participant