Shul2Reader — the SHUL/2 reader over a WHOLE capture.
This header declares 5 types (50 members) and 1 free function.
Extracted from include/shulib/diag/shul2_reader.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.
enum class CaptureStatuscaptureStatusName— free functionstruct ReaderCountersbytesSkippedframesVerifiedcrcFailureslengthOverrunssessionsrefusedSessionslateJoinsframesDeliveredframesOutsideSessionframesInRefusedSessionsunknownTypeSkippedwrongWidthSkippedextensionBytesIgnoredstaleFrameshelloRepeatshelloMismatcheslostFrameslinkLostFramessessionsLinkLossUnknownendDisagreementssessionsEndedtruncatedtruncatedAt
struct SessionReportclass Shul2Reader
enum class CaptureStatus : std::uint8_tOne named verdict for a whole capture. Final once Shul2Reader::finished() is true.
enum class, declared at include/shulib/diag/shul2_reader.hpp:99.
Empty = 0no bytes at all
enumerator, declared at include/shulib/diag/shul2_reader.hpp:100.
NoFrame = 1bytes, but no frame verified: a bit-7-stripping or NUL-truncating transport, or not SHUL/2 at all
enumerator, declared at include/shulib/diag/shul2_reader.hpp:101.
NoSession = 2frames verified, but no Hello: a newline-translating transport (the signature's CR LF kills every Hello), or a capture that ended before the first re-Hello
enumerator, declared at include/shulib/diag/shul2_reader.hpp:103.
Refused = 3every session was refused; the latest session's verdict says why
enumerator, declared at include/shulib/diag/shul2_reader.hpp:106.
Ok = 4at least one session was accepted
enumerator, declared at include/shulib/diag/shul2_reader.hpp:107.
[[nodiscard]] constexpr const char* captureStatusName(CaptureStatus s) noexceptThe §18.5 spelling of a CaptureStatus, for messages. Never returns null.
free function, declared at include/shulib/diag/shul2_reader.hpp:111.
struct ReaderCountersCapture-wide counts, as of the bytes consumed so far (final once finished()). Byte counts are std::size_t; loss figures saturate rather than wrap.
struct, declared at include/shulib/diag/shul2_reader.hpp:124.
std::size_t bytesSkipped = 0every byte not inside a verified frame
field, declared at include/shulib/diag/shul2_reader.hpp:125.
std::uint32_t framesVerified = 0frames whose sync matched and CRC verified
field, declared at include/shulib/diag/shul2_reader.hpp:126.
std::uint32_t crcFailures = 0whole candidates whose CRC did not verify
field, declared at include/shulib/diag/shul2_reader.hpp:127.
std::uint32_t lengthOverruns = 0tail candidates (a NeedMore) a later frame displaced
field, declared at include/shulib/diag/shul2_reader.hpp:128.
std::uint32_t sessions = 0sessions started (accepted and refused)
field, declared at include/shulib/diag/shul2_reader.hpp:129.
std::uint32_t refusedSessions = 0sessions negotiate() refused
field, declared at include/shulib/diag/shul2_reader.hpp:130.
std::uint32_t lateJoins = 0sessions started at a Hello with seq > 0, none open
field, declared at include/shulib/diag/shul2_reader.hpp:131.
std::uint32_t framesDelivered = 0frames next() handed out
field, declared at include/shulib/diag/shul2_reader.hpp:132.
std::uint32_t framesOutsideSession = 0verified frames with no session open
field, declared at include/shulib/diag/shul2_reader.hpp:133.
std::uint32_t framesInRefusedSessions = 0verified frames of refused sessions
field, declared at include/shulib/diag/shul2_reader.hpp:134.
std::uint32_t unknownTypeSkipped = 0in-session frames of a type this build does not assign
field, declared at include/shulib/diag/shul2_reader.hpp:135.
std::uint32_t wrongWidthSkipped = 0known-type frames whose width disagrees with the Hello
field, declared at include/shulib/diag/shul2_reader.hpp:136.
std::size_t extensionBytesIgnored = 0bytes beyond this build's known widths, not read
field, declared at include/shulib/diag/shul2_reader.hpp:137.
std::uint32_t staleFrames = 0stale or duplicate frames (d ≥ 2^31), skipped
field, declared at include/shulib/diag/shul2_reader.hpp:138.
std::uint32_t helloRepeats = 0re-Hellos identical to their session's Hello
field, declared at include/shulib/diag/shul2_reader.hpp:139.
std::uint32_t helloMismatches = 0re-Hellos at seq > 0 that differed (new session)
field, declared at include/shulib/diag/shul2_reader.hpp:140.
std::uint32_t lostFrames = 0sequence gaps in accepted sessions, summed
field, declared at include/shulib/diag/shul2_reader.hpp:141.
std::uint32_t linkLostFrames = 0link loss, summed over sessions where it is known
field, declared at include/shulib/diag/shul2_reader.hpp:142.
std::uint32_t sessionsLinkLossUnknown = 0accepted sessions whose link loss is unknowable
field, declared at include/shulib/diag/shul2_reader.hpp:143.
std::uint32_t endDisagreements = 0Ends that disagree with what the reader saw
field, declared at include/shulib/diag/shul2_reader.hpp:144.
std::uint32_t sessionsEnded = 0accepted sessions whose End was delivered
field, declared at include/shulib/diag/shul2_reader.hpp:145.
bool truncated = falsethe capture ends inside an undisplaced candidate
field, declared at include/shulib/diag/shul2_reader.hpp:146.
std::size_t truncatedAt = 0that candidate's offset, when truncated
field, declared at include/shulib/diag/shul2_reader.hpp:147.
struct SessionReportWhat the reader knows about ONE session — Shul2Reader::latestSession() is the latest.
struct, declared at include/shulib/diag/shul2_reader.hpp:151.
Hello hello{}Its Hello, decoded (views into the capture). For a session of ANOTHER major only the signature and version are decoded (hello.complete false): the rest is that major's.
field, declared at include/shulib/diag/shul2_reader.hpp:154.
Verdict verdict = Verdict::BadSignaturenegotiate()'s verdict on it
field, declared at include/shulib/diag/shul2_reader.hpp:155.
std::uint32_t helloSeq = 0the sequence number of the Hello that started it
field, declared at include/shulib/diag/shul2_reader.hpp:156.
bool lateJoin = falseTrue when the session did not start at its seq-0 Hello — a late join or a mismatched re-Hello — so its link loss cannot be known.
field, declared at include/shulib/diag/shul2_reader.hpp:159.
std::uint32_t lostFrames = 0its sequence gaps (accepted sessions only)
field, declared at include/shulib/diag/shul2_reader.hpp:160.
bool sawEnd = falseits End was delivered
field, declared at include/shulib/diag/shul2_reader.hpp:161.
EndInfo end{}that End, when sawEnd
field, declared at include/shulib/diag/shul2_reader.hpp:162.
bool linkLossKnown = falselinkLostFrames is meaningful (seq-0 start, End seen)
field, declared at include/shulib/diag/shul2_reader.hpp:163.
std::uint32_t linkLostFrames = 0max(0, lostFrames − end.framesFailed), when known
field, declared at include/shulib/diag/shul2_reader.hpp:164.
class Shul2ReaderThe reader of a whole SHUL/2 capture: point it at bytes the caller holds — it BORROWS the span and allocates nothing — and pull frames with next() until it returns false. Frames are delivered only from accepted sessions and only at the widths their Hello declared; every byte and frame that is not delivered is counted by name in counters(). A corrupted frame costs exactly that frame: scanning resumes one byte on, never at a claimed length. At the tail, a candidate that runs past the end is truncation only if no later frame verifies (header). The verdict for the whole capture is status(), final once finished().
class, declared at include/shulib/diag/shul2_reader.hpp:174.
explicit Shul2Reader(std::span<const std::byte> capture, const ReaderProfile& profile = kThisReader) noexceptRead capture with profile (this build's, unless a test stands up another). Both are borrowed: the capture and the profile's rows must outlive the reader — never hand it a temporary buffer (H1's own test suite did once, and read freed memory). Never throws.
function, declared at include/shulib/diag/shul2_reader.hpp:188.
[[nodiscard]] bool next(Frame& out) noexceptDeliver the next frame, skipping and counting everything that is not delivered. Returns false at the end of the capture, which finishes it (finished() is then true).
function, declared at include/shulib/diag/shul2_reader.hpp:194.
void drain() noexceptRead to the end, discarding the frames — for a caller that wants only the verdict and the counts.
function, declared at include/shulib/diag/shul2_reader.hpp:233.
[[nodiscard]] bool finished() const noexceptTrue once next() has returned false: status() and counters() are then final.
function, declared at include/shulib/diag/shul2_reader.hpp:240.
[[nodiscard]] CaptureStatus status() const noexceptThe capture's verdict (final once finished()).
function, declared at include/shulib/diag/shul2_reader.hpp:243.
[[nodiscard]] const ReaderCounters& counters() const noexceptThe capture-wide counts (final once finished()).
function, declared at include/shulib/diag/shul2_reader.hpp:260.
[[nodiscard]] bool hasSession() const noexceptTrue once any Hello has started a session.
function, declared at include/shulib/diag/shul2_reader.hpp:262.
[[nodiscard]] const SessionReport& latestSession() const noexceptThe latest session (a default report while hasSession() is false).
function, declared at include/shulib/diag/shul2_reader.hpp:264.
[[nodiscard]] std::size_t bytesConsumed() const noexceptBytes of the capture consumed so far.
function, declared at include/shulib/diag/shul2_reader.hpp:266.
struct FrameOne delivered frame: its type, envelope flags, sequence number and whole payload (a view valid while the capture is).
struct, declared at include/shulib/diag/shul2_reader.hpp:178.
FrameType type = FrameType::Tickone of the five known types
field, declared at include/shulib/diag/shul2_reader.hpp:179.
std::uint8_t flags = 0envelope flags as they arrived (never judged)
field, declared at include/shulib/diag/shul2_reader.hpp:180.
std::uint32_t seq = 0its sequence number
field, declared at include/shulib/diag/shul2_reader.hpp:181.
std::span<const std::byte> payload{}the WHOLE payload, appended bytes included
field, declared at include/shulib/diag/shul2_reader.hpp:182.
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 — 86 lines, click to expand
Shul2Reader — the SHUL/2 reader over a WHOLE capture (chunk H1 Part A1, 2026-09-29; the
format is diag/shul2_format.hpp). It ships in the same part as the format for the reason
BlackboxReader shipped with the blackbox: a format nothing can read is not a record, and the
reader is what makes the encoders testable at all.
It borrows the span (a capture the caller already holds — a file, or the bytes a test just
built), allocates nothing, never throws, and never reads past the end. It is built on
scanFrame() and negotiate() and on nothing else, so a LIVE reader (H2's socket loop, which
waits on NeedMore instead) composes the same two functions and meets the same rules. There
is no socket code here.
── Where it differs from BlackboxReader, and why ───────────────────────────────────
At the TAIL. BlackboxReader stops at a frame whose declared length runs past the end and
calls the file truncated — right for a file with no check, where the length is all there is
to believe. Here every frame carries a CRC, so a declared length is only a CLAIM until the
CRC verifies, and a corrupted length near the end of a capture would otherwise throw away
every good frame after it. So a whole capture, which never receives more bytes, treats a
NeedMore at position p as a failed candidate and resumes scanning at p + 1:
* if a frame verifies later, p was a corrupted length — counted in lengthOverruns;
* `truncated` is set if and only if the capture ends inside a candidate no later frame
displaced: the EARLIEST NeedMore after the last verified frame (truncatedAt);
* a capture that ends exactly on a frame boundary is not truncated.
── Delivery ────────────────────────────────────────────────────────────────────────
next() delivers verified frames {type, flags, seq, payload} of the five known types, ONLY
from ACCEPTED sessions (Exact, OlderMinor, NewerMinor), and only when the frame's width
agrees with its session's Hello: a Tick, Summary or End of exactly the declared width; a Log
whose tag and message fit and whose non-text bytes equal the declared logFixedBytes. A frame
that disagrees counts in wrongWidthSkipped. Bytes beyond this build's known width are counted
in extensionBytesIgnored (Hello: payload − the known Hello width; Log: non-text − the known
14; the others: payload − the known width) — that is how a NEWER minor's appended fields are
ignored without being lost from the account. The payload delivered is the WHOLE payload; the
decoders in shul2_format.hpp read the known prefix of it.
── Sessions ────────────────────────────────────────────────────────────────────────
* A Hello with seq 0 starts a new session (a writer that re-opened, or the first).
* A Hello with seq > 0 while no session is open is a LATE JOIN; the session starts there.
* A Hello with seq > 0 inside a session must match that session's Hello byte for byte — a
repeat (helloRepeats), delivered like any frame. Otherwise it is a helloMismatch and
starts a new session.
* An End closes its session. A frame outside any open session (before the first Hello, or
after an End and before the next Hello) counts in framesOutsideSession.
* A REFUSED session's frames — its End and its repeats included — count in
framesInRefusedSessions until the next Hello that differs from its own, and are never
delivered: nothing in a stream this build cannot interpret is interpreted.
* Any role is accepted and reported; delivery is by type, never by role.
── Sequence ────────────────────────────────────────────────────────────────────────
Every frame of an accepted session (unknown types and repeats included — the writer offered
them) is checked against expected = the last accepted seq + 1, modulo 2^32:
d = 0 in order; 0 < d < 2^31 means d frames were lost (lostFrames); d ≥ 2^31 is a stale or
duplicate frame, skipped (staleFrames). A sequence that wraps 0xFFFFFFFF → 0 is in order
and starts nothing: only a HELLO at seq 0 starts a session.
── Link loss, per session ──────────────────────────────────────────────────────────
linkLostFrames = max(0, lostFrames − End.framesFailed): the frames the LINK lost, as opposed
to the ones the writer's device refused (which the writer counted, and which also appear as
gaps). Known ONLY for a session read from its seq-0 Hello whose End was delivered; every
other ACCEPTED session counts in sessionsLinkLossUnknown (a late join, a mismatch-started
session, a session with no End). An End whose framesOffered differs from its own sequence
number counts in endDisagreements; so does one of a seq-0 session whose framesFailed exceeds
the frames the session lost — and the figure then saturates at 0 instead of wrapping. (For a
late-joined session that comparison means nothing: the frames lost before the join were
never seen.)
── What a live reader must hold, and what it must do when its stream ends ──────────
At least kMaxFrameBytes (14 + 4096) bytes. A corrupted length stalls a live reader until that
many bytes have arrived — about nine tick frames, ~90 ms at 100 Hz — and then the frames
inside those bytes are recovered, because scanning resumes one byte on. A whole capture has
no stall: it resumes at once. When a live stream ENDS — the socket closes, the file ends —
what the live reader still holds IS a whole capture, and it must apply the tail rule above to
it: a corrupted length in the last 4 KB would otherwise hold back every frame after it, the
End included, for good, and that session's link loss would become unknowable. (Applying the
rule after an idle timeout as well is a live reader's own policy — H2's: it releases a held
End sooner, at the cost of dropping a frame whose bytes were merely late.)
── What reading costs ──────────────────────────────────────────────────────────────
Linear in the capture for ordinary damage and random bytes (a random sync pair is about one
position in 65,536). The worst case is bytes CRAFTED to hold as many candidates claiming an
accepted length as a byte pattern can — about three per ten bytes — each costing up to 4104
bytes of bitwise CRC before it fails: about 1,200 bytes of CRC per input byte, measured on a
desktop host at roughly 14-23 s per MB of such input (shul2_format.hpp, the resync rule). There
is no time budget here — this reader runs on a host; a reader on a UI thread should bound its own.
Allocation-free and PROS-free, like everything in this tree.