The SHULIB BLACKBOX on-disk format, v1 — the binary record SdSink writes and BlackboxReader reads.
This header declares 6 types (55 members), 12 free functions, and 8 constants.
Extracted from include/shulib/diag/blackbox_format.hpp — this page is that header's documentation, reformatted, so it cannot disagree with the code. Prose about how to think about the API lives in How it works; worked recipes live in Recipes; this page is the complete, mechanical list of what exists.
kMagic— constantkFormatVersion— constantkHeaderBytes— constantkFrameHeaderBytes— constantkTickPayloadBytes— constantkSummaryPayloadBytes— constantkTriagePayloadBytes— constantkEndPayloadBytes— constantenum class FrameTypestruct TriageInfostruct EndInfostruct BlackboxHeaderclass ByteWriterclass ByteReaderencodeHeader— free functiondecodeHeader— free functionencodeTick— free functionsafeAngle— free functiondecodeTick— free functionencodeSummary— free functiondecodeSummary— free functionencodeTriage— free functiondecodeTriage— free functionencodeEnd— free functiondecodeEnd— free functionencodeFrameHeader— free function
inline constexpr char kMagic[4] = {'S', 'H', 'B', 'B'}The four magic bytes every blackbox file starts with ("SHulib BlackBox").
constant, declared at include/shulib/diag/blackbox_format.hpp:77.
inline constexpr std::uint16_t kFormatVersion = 1On-disk format version. BUMP THIS whenever any layout below changes — a reader refuses a version it was not built for rather than misreading it (header note). Giving a meaning to a tick-flags bit every earlier writer wrote as 0 is not a layout change (bit 2, TANK1: see the tick record's flags note), so it did not bump.
constant, declared at include/shulib/diag/blackbox_format.hpp:83.
inline constexpr std::size_t kHeaderBytes = 256Size of the fixed file header, in bytes (v1). Fixed width so a reader can seek past it without parsing, and generous enough to hold full provenance.
constant, declared at include/shulib/diag/blackbox_format.hpp:87.
inline constexpr std::size_t kFrameHeaderBytes = 4Size of the per-frame prefix: {u8 type, u8 reserved, u16 payloadBytes}.
constant, declared at include/shulib/diag/blackbox_format.hpp:90.
inline constexpr std::size_t kTickPayloadBytes = 428Payload size of one Tick frame (v1). Pinned by the golden test; the encoder asserts it wrote exactly this many bytes.
constant, declared at include/shulib/diag/blackbox_format.hpp:94.
inline constexpr std::size_t kSummaryPayloadBytes = 168Payload size of one Summary frame (v1).
constant, declared at include/shulib/diag/blackbox_format.hpp:97.
inline constexpr std::size_t kTriagePayloadBytes = 24 + kTickPayloadBytesPayload size of one Triage frame (v1): the D-7 triage fields PLUS the complete record of the tick the fault fired on (header note on dump ordering in sd_sink.hpp).
constant, declared at include/shulib/diag/blackbox_format.hpp:101.
inline constexpr std::size_t kEndPayloadBytes = 28Payload size of one End frame (v1) — the graceful-end stamp.
constant, declared at include/shulib/diag/blackbox_format.hpp:104.
enum class FrameType : std::uint8_tWhat a frame carries. WIRE-STABLE: explicit values, append-only — an unknown type is skipped by length, never guessed at.
enum class, declared at include/shulib/diag/blackbox_format.hpp:108.
Tick = 1one DebugRecord (kTickPayloadBytes)
enumerator, declared at include/shulib/diag/blackbox_format.hpp:109.
Summary = 2one RunSummary (kSummaryPayloadBytes)
enumerator, declared at include/shulib/diag/blackbox_format.hpp:110.
Triage = 3the D-7 fault triage block + the fault tick's own record
enumerator, declared at include/shulib/diag/blackbox_format.hpp:111.
End = 4the graceful-end stamp: counts, brownout latch, end time
enumerator, declared at include/shulib/diag/blackbox_format.hpp:112.
struct TriageInfoThe D-7 triage block, as data: which fault, when, on which tick, and how many preceding ticks follow it in the file. The record of the fault tick itself travels in the same frame (see sd_sink.hpp's dump-ordering rule).
struct, declared at include/shulib/diag/blackbox_format.hpp:118.
FaultCode fault = FaultCode::Nonethe fault that triggered the dump
field, declared at include/shulib/diag/blackbox_format.hpp:119.
bool brownout = falsethe latched brownout marker at dump time
field, declared at include/shulib/diag/blackbox_format.hpp:120.
std::uint32_t tickIndex = 0how many records the sink had seen when it fired
field, declared at include/shulib/diag/blackbox_format.hpp:121.
double faultTime = 0.0the fault tick's t, seconds since the run epoch
field, declared at include/shulib/diag/blackbox_format.hpp:122.
std::uint32_t precedingTicks = 0Tick frames that follow, oldest first (0 when streaming)
field, declared at include/shulib/diag/blackbox_format.hpp:123.
struct EndInfoThe end frame: what the sink knows about its own run when it closes cleanly. A file WITHOUT this frame ended abruptly — that absence is the truncation signal a reader can act on.
struct, declared at include/shulib/diag/blackbox_format.hpp:129.
std::uint32_t tickFrames = 0Tick frames staged over the run
field, declared at include/shulib/diag/blackbox_format.hpp:130.
std::uint32_t droppedFrames = 0frames dropped for want of buffer (byte budget)
field, declared at include/shulib/diag/blackbox_format.hpp:131.
std::uint32_t bytesBefore = 0Bytes of this file that PRECEDE this frame — i.e. the frame's own offset. A reader can verify it against where it actually found the frame, which is how a file that was appended to, interleaved, or spliced gives itself away. (It is NOT "bytes the device confirmed": at close() the bulk of a caller-paced run is still staged and goes out in the same write as this frame, so that figure would read 0 for the most common run of all.)
field, declared at include/shulib/diag/blackbox_format.hpp:138.
std::uint32_t messagesSeen = 0log() lines handed to the sink and NOT carried (header note)
field, declared at include/shulib/diag/blackbox_format.hpp:139.
bool brownout = falsethe latched brownout marker
field, declared at include/shulib/diag/blackbox_format.hpp:140.
bool deviceFailed = falsea write() or flush() reported failure during the run
field, declared at include/shulib/diag/blackbox_format.hpp:141.
double endTime = 0.0clock time at close, seconds since the run epoch
field, declared at include/shulib/diag/blackbox_format.hpp:142.
struct BlackboxHeaderA decoded file header. Value type with bounded storage, like RunSummary: a decoded header must never hold views into a buffer the caller may free.
struct, declared at include/shulib/diag/blackbox_format.hpp:147.
std::uint16_t formatVersion = 0as read from the file
field, declared at include/shulib/diag/blackbox_format.hpp:148.
std::uint16_t headerBytes = 0self-declared header size (lets a reader seek)
field, declared at include/shulib/diag/blackbox_format.hpp:149.
std::uint16_t tickRecordBytes = 0self-declared Tick payload size (cross-checked)
field, declared at include/shulib/diag/blackbox_format.hpp:150.
std::uint16_t flags = 0reserved, 0 in v1
field, declared at include/shulib/diag/blackbox_format.hpp:151.
double epochSeconds = 0.0the injected clock's reading when the file opened
field, declared at include/shulib/diag/blackbox_format.hpp:152.
std::uint32_t ringCapacity = 0flight-recorder ring size the writer was configured with
field, declared at include/shulib/diag/blackbox_format.hpp:153.
std::uint32_t byteBudget = 0RAM byte budget the writer was configured with
field, declared at include/shulib/diag/blackbox_format.hpp:154.
[[nodiscard]] std::string_view buildHash() const noexceptThe git build hash the run was built from. EMPTY means MISSING — render it loudly and never invent a plausible value (§18.5, build_info.hpp).
function, declared at include/shulib/diag/blackbox_format.hpp:158.
[[nodiscard]] std::string_view routineId() const noexceptThe routine id the run was started with (may be empty).
function, declared at include/shulib/diag/blackbox_format.hpp:160.
[[nodiscard]] std::string_view alliance() const noexceptAlliance as free text ("red"/"blue"/"skills"); may be empty.
function, declared at include/shulib/diag/blackbox_format.hpp:162.
[[nodiscard]] std::string_view side() const noexceptSide as free text ("left"/"right"); may be empty.
function, declared at include/shulib/diag/blackbox_format.hpp:164.
[[nodiscard]] std::string_view portMap() const noexceptThe caller-authored port map; may be empty.
function, declared at include/shulib/diag/blackbox_format.hpp:166.
char buildHash_[48] = ""Storage for buildHash() — written by the decoder, NUL-terminated.
field, declared at include/shulib/diag/blackbox_format.hpp:169.
char routineId_[32] = ""Storage for routineId().
field, declared at include/shulib/diag/blackbox_format.hpp:171.
char alliance_[16] = ""Storage for alliance().
field, declared at include/shulib/diag/blackbox_format.hpp:173.
char side_[16] = ""Storage for side().
field, declared at include/shulib/diag/blackbox_format.hpp:175.
char portMap_[96] = ""Storage for portMap().
field, declared at include/shulib/diag/blackbox_format.hpp:177.
class ByteWriterLittle-endian byte writer with a hard end: a write that would not fit writes NOTHING and latches overflow, so an undersized buffer can never corrupt neighbouring memory and can never half-write a field. Callers check ok().
class, declared at include/shulib/diag/blackbox_format.hpp:183.
explicit ByteWriter(std::span<std::byte> out) noexceptWrite into out, starting at offset 0.
function, declared at include/shulib/diag/blackbox_format.hpp:186.
void u8(std::uint8_t v) noexceptAppend one unsigned byte.
function, declared at include/shulib/diag/blackbox_format.hpp:189.
void boolean(bool v) noexceptAppend a bool as 0x00 / 0x01.
function, declared at include/shulib/diag/blackbox_format.hpp:196.
void u16(std::uint16_t v) noexceptAppend a 16-bit unsigned value, little-endian.
function, declared at include/shulib/diag/blackbox_format.hpp:198.
void u32(std::uint32_t v) noexceptAppend a 32-bit unsigned value, little-endian.
function, declared at include/shulib/diag/blackbox_format.hpp:206.
void i32(std::int32_t v) noexceptAppend a 32-bit signed value as two's complement, little-endian.
function, declared at include/shulib/diag/blackbox_format.hpp:215.
void f64(double v) noexceptAppend an IEEE-754 binary64 value, little-endian (bit pattern preserved, so a NaN or an infinity survives the trip exactly as it was recorded).
function, declared at include/shulib/diag/blackbox_format.hpp:218.
void text(std::string_view s, std::size_t fieldBytes) noexceptAppend fieldBytes of text: s truncated to fit, NUL-padded to the full width. Fixed width by design — a variable-length string would make every later offset depend on run-time content.
function, declared at include/shulib/diag/blackbox_format.hpp:231.
void zeros(std::size_t n) noexceptAppend n zero bytes (reserved space).
function, declared at include/shulib/diag/blackbox_format.hpp:241.
[[nodiscard]] std::size_t offset() const noexceptHow many bytes have been appended.
function, declared at include/shulib/diag/blackbox_format.hpp:250.
[[nodiscard]] bool ok() const noexceptFalse once any append did not fit (nothing was written for that append).
function, declared at include/shulib/diag/blackbox_format.hpp:252.
class ByteReaderLittle-endian byte reader with a hard end: a read past the end yields zero and latches exhaustion, so a truncated or corrupt file can never read out of bounds and can never half-read a field. Callers check ok().
class, declared at include/shulib/diag/blackbox_format.hpp:271.
explicit ByteReader(std::span<const std::byte> in) noexceptRead from in, starting at offset 0.
function, declared at include/shulib/diag/blackbox_format.hpp:274.
[[nodiscard]] std::uint8_t u8() noexceptRead one unsigned byte (0 past the end).
function, declared at include/shulib/diag/blackbox_format.hpp:277.
[[nodiscard]] bool boolean() noexceptRead a bool: any nonzero byte is true.
function, declared at include/shulib/diag/blackbox_format.hpp:284.
[[nodiscard]] std::uint16_t u16() noexceptRead a 16-bit unsigned value, little-endian.
function, declared at include/shulib/diag/blackbox_format.hpp:286.
[[nodiscard]] std::uint32_t u32() noexceptRead a 32-bit unsigned value, little-endian.
function, declared at include/shulib/diag/blackbox_format.hpp:295.
[[nodiscard]] std::int32_t i32() noexceptRead a 32-bit signed value (two's complement), little-endian.
function, declared at include/shulib/diag/blackbox_format.hpp:306.
[[nodiscard]] double f64() noexceptRead an IEEE-754 binary64 value, little-endian (bit pattern preserved).
function, declared at include/shulib/diag/blackbox_format.hpp:308.
void text(char* dst, std::size_t dstBytes, std::size_t fieldBytes) noexceptRead fieldBytes of NUL-padded text into dst (capacity dstBytes, always NUL-terminated). Bytes beyond the destination are consumed and discarded, so the cursor stays aligned no matter how the caller sized its storage.
function, declared at include/shulib/diag/blackbox_format.hpp:323.
void skip(std::size_t n) noexceptSkip n bytes (reserved space).
function, declared at include/shulib/diag/blackbox_format.hpp:336.
[[nodiscard]] std::size_t offset() const noexceptHow many bytes have been consumed.
function, declared at include/shulib/diag/blackbox_format.hpp:342.
[[nodiscard]] bool ok() const noexceptFalse once any read ran past the end.
function, declared at include/shulib/diag/blackbox_format.hpp:344.
[[nodiscard]] inline std::size_t encodeHeader(std::span<std::byte> out, const SessionInfo& info, double epochSeconds, std::uint32_t ringCapacity, std::uint32_t byteBudget) noexceptEncode the 256-byte file header into out. Returns the bytes written (0 if out is too small). Provenance strings are copied in, truncated to their field widths — an EMPTY build hash stays empty, because MISSING must stay loud all the way to disk.
free function, declared at include/shulib/diag/blackbox_format.hpp:374.
[[nodiscard]] inline bool decodeHeader(std::span<const std::byte> in, BlackboxHeader& out) noexceptDecode a file header. Returns false if in is shorter than the header or the magic does not match; the VERSION is decoded but NOT judged here — BlackboxReader owns the refusal policy, and a caller inspecting a rejected file still wants to see what version it claims to be.
free function, declared at include/shulib/diag/blackbox_format.hpp:404.
[[nodiscard]] inline std::size_t encodeTick(std::span<std::byte> out, const DebugRecord& r) noexceptEncode one DebugRecord. Returns the bytes written, or 0 if out was too small or the layout did not come out to exactly kTickPayloadBytes (a loud, testable failure rather than a silently short record). wheelCount is written clamped to [0, DebugRecord::kMaxWheels] (since H1 Part A1; above 255 it used to wrap).
free function, declared at include/shulib/diag/blackbox_format.hpp:455.
[[nodiscard]] inline math::Angle safeAngle(double radians, bool& corrupt) noexceptRebuild an Angle from a decoded radian value WITHOUT trusting the file: a corrupt or truncated blackbox can contain any bit pattern, and math::Angle's factory rejects non-finite input by precondition. A decoder that throws on a corrupt file is a decoder you cannot use on the file you most need to read, so a non-finite heading decodes to zero and corrupt is raised for the caller to see.
free function, declared at include/shulib/diag/blackbox_format.hpp:520.
[[nodiscard]] inline bool decodeTick(std::span<const std::byte> in, DebugRecord& r, bool& corrupt) noexceptDecode one DebugRecord. Returns false if the payload is not exactly kTickPayloadBytes. corrupt is set (never cleared) when a field could not be represented — a non-finite heading, which decodes to zero (safeAngle), or a wheelCount above DebugRecord::kMaxWheels, which decodes as kMaxWheels (since H1 Part A1).
free function, declared at include/shulib/diag/blackbox_format.hpp:532.
[[nodiscard]] inline std::size_t encodeSummary(std::span<std::byte> out, const RunSummary& s, std::uint32_t blackboxDropped) noexceptEncode one RunSummary. blackboxDropped is the SINK's own drop count, passed in rather than read from the summary so the file always carries the writer's live figure even when the caller assembled the summary before the last drop. Returns the bytes written, or 0 on a layout/space failure.
free function, declared at include/shulib/diag/blackbox_format.hpp:625.
[[nodiscard]] inline bool decodeSummary(std::span<const std::byte> in, RunSummary& s, std::uint32_t& blackboxDropped) noexceptDecode one RunSummary; blackboxDropped receives the sink's own drop count. Returns false if the payload is not exactly kSummaryPayloadBytes.
free function, declared at include/shulib/diag/blackbox_format.hpp:656.
[[nodiscard]] inline std::size_t encodeTriage(std::span<std::byte> out, const TriageInfo& info, const DebugRecord& faultTick) noexceptEncode the D-7 triage block plus the complete record of the tick the fault fired on. Returns the bytes written, or 0 on a layout/space failure.
free function, declared at include/shulib/diag/blackbox_format.hpp:696.
[[nodiscard]] inline bool decodeTriage(std::span<const std::byte> in, TriageInfo& info, DebugRecord& faultTick, bool& corrupt) noexceptDecode a triage frame and the fault tick's record. Returns false if the payload is not exactly kTriagePayloadBytes.
free function, declared at include/shulib/diag/blackbox_format.hpp:721.
[[nodiscard]] inline std::size_t encodeEnd(std::span<std::byte> out, const EndInfo& e) noexceptEncode the graceful-end stamp. Its PRESENCE is the signal that the run closed cleanly; its absence is how a reader knows a file was cut short. Returns the bytes written, or 0 on a layout/space failure.
free function, declared at include/shulib/diag/blackbox_format.hpp:744.
[[nodiscard]] inline bool decodeEnd(std::span<const std::byte> in, EndInfo& e) noexceptDecode the graceful-end stamp. Returns false if the payload is not exactly kEndPayloadBytes.
free function, declared at include/shulib/diag/blackbox_format.hpp:762.
[[nodiscard]] inline std::size_t encodeFrameHeader(std::span<std::byte> out, FrameType type, std::uint16_t payloadBytes) noexceptWrite a frame prefix {type, reserved, payloadBytes} into out. Returns the bytes written (kFrameHeaderBytes) or 0 if it did not fit.
free function, declared at include/shulib/diag/blackbox_format.hpp:780.
The header opens with the reasoning behind these shapes. It is reproduced here in full because a reference that only lists signatures teaches nobody why.
The header’s own reasoning — 59 lines, click to expand
The SHULIB BLACKBOX on-disk format, v1 — the binary record SdSink writes and
BlackboxReader reads (master plan §18; diagnostics-plan D-6/D-7; WS13, chunk E1).
── Why a format document lives in code ─────────────────────────────────────────────
A file outlives the program that wrote it. Six months from now the only thing that
can open a run from a competition day is a decoder that agrees with this layout, so
the layout is written down ONCE, here, with its version, and the encoder and the
decoder are both held to it BY A BYTE-EXACT GOLDEN TEST — not by their agreement
with each other. Two sides that share one bug agree perfectly; that is precisely the
failure a round trip cannot see, which is why test/blackbox_format_test.cpp pins the
literal bytes of the header and of one full tick record.
── The shape ───────────────────────────────────────────────────────────────────────
[ 256-byte file header ][ frame ][ frame ][ frame ] …
Every frame is { u8 type, u8 reserved, u16 payloadBytes } followed by payloadBytes
of payload. A reader that meets a frame type it does not know SKIPS it by its length
and counts it — that is what makes a v1 reader survive a v1.1 writer that appended a
new frame kind, without ever GUESSING at content it does not understand.
── Versioned from byte zero, and refusal over misreading ───────────────────────────
The header carries kFormatVersion. A decoder that mis-reads an old file is worse
than one that refuses it: a wrong number read confidently sends the 2am investigation
somewhere false, while a refusal sends it to the git history. So the reader refuses
any version it was not built for, and ALSO cross-checks the header's self-declared
record width against its own — that catches the specific human error of changing the
layout and forgetting to bump the version.
── Fixed width, and no narrowing ANYWHERE ──────────────────────────────────────────
Every field is fixed width and little-endian, written byte by byte (never a struct
memcpy: padding and ABI are not a file format). Every floating-point field is IEEE-754
binary64, including the per-wheel arrays and the tick-phase slots, where binary32
would have saved ~35% of the file. That trade was made deliberately: the chunk's
central promise is that a decoded record equals the encoded one FIELD BY FIELD, and
with narrowing that sentence would have quietly become "equals after a documented
rounding" — a weaker claim that also makes every future comparison of two runs
depend on rounding behaviour. Bytes are cheap on an SD card; a fuzzy record is not.
── What v1 does NOT carry, said plainly ────────────────────────────────────────────
The log() message channel. Variable-length text in a fixed-width record format is a
different problem with different trade-offs (bounded-vs-truncated strings, per-line
framing), and the terminal (A1) already owns text. The blackbox carries the per-tick
RECORD, the run SUMMARY, and the fault TRIAGE block. SdSink counts the messages it
was handed and writes that count into the end frame, so the omission is visible in
the file rather than silent — a reader can always see that N lines existed elsewhere.
── What H1 (F9, the SHUL/2 wire) inherits ──────────────────────────────────────────
This is a PERSISTENCE contract the moment a file exists, but it is deliberately NOT
the wire: SHUL/2 is streamed, sequenced, checked and versioned on its own terms
(diag/shul2_format.hpp, chunk H1 Part A1). What it inherits is stronger than a field
order: SHUL/2's Tick and Summary payloads ARE this file's bytes — the wire calls
encodeTick()/encodeSummary() and these decoders, and has no encoder of its own, so a
Tick payload read off the SD card can be re-wrapped as a SHUL/2 frame without decoding
it. The consequence runs this way: once F9 freezes (H1 Part B), the tick and summary
layouts below are frozen with it, and a later change to the file's layout must fork its
own encoder rather than move the wire's (the SHUL/2 Tick golden fails if it does not).
What the wire does NOT inherit is this file's refuse-any-other-width rule (its reader
accepts a newer minor's longer frames) or its lack of a check (every SHUL/2 frame
carries a CRC). Nothing here freezes anything yet: F9 is H1 Part B's.