Run frontier MoE models on hardware you already own — pure C, zero deps, experts streamed from disk. Tiny engine, immense model. 🐦
C
test(check_data_logprob_gaps): gate the transcript on the engine preamble and widen the numeric-tail check
monotophicda0c938a3ef5
diff --git a/c/tests/check_data_logprob_gaps.py b/c/tests/check_data_logprob_gaps.pyindex 0abb4165..767647f7 100644--- a/c/tests/check_data_logprob_gaps.py+++ b/c/tests/check_data_logprob_gaps.py@@ -4,18 +4,24 @@Asserts, over a captured raw engine-stdout transcript, that EVERY generatedDATA frame of an opted-in request carries the per-token numeric channel("DATA <id> <n> <lp> <k> [tid tlp]*k") -- accepted draft tokens included.-Speculatively-accepted draft tokens bypass the pick_tok call sites in the mux-loop (packet Fork 5's implementation risk), so a gap would show up as a-legacy 3-field DATA frame in the middle of an opted-in generation.+On today's engine, an accepted speculative-draft token is emitted through+the SAME mux_data() call as any other generated token (mux_spec_emit ->+mux_data, passing the same logits row and requested top-k the ordinary+emit sites use), so the gap this check hunts -- a legacy 3-field DATA+frame appearing mid-generation because a draft-accept path bypassed the+numeric-channel emit -- is NOT producible by the engine as shipped. This+check is retained as REGRESSION COVERAGE for that invariant (a future mux+change that adds a new emit call site without the tail would reintroduce+exactly this defect class), not as a currently-live defect hunt.-Run recipe (orchestrator; needs a real model -- CPU or CUDA build):+Run recipe (orchestrator; needs a real model -- CPU or explicit-CUDA build):# single KV slot + model drafts live = the speculative serve regimecd c && make glmSERVE=1 SERVE_BATCH=1 KV_SLOTS=1 DRAFT=2 CTX=4096 \- ./colibri <snapshot-dir> < submit.txt | tee engine_stdout.raw+ ./colibri <snapshot-dir> < submit.raw | tee engine_stdout.raw- where submit.txt contains one opted-in generation request, e.g. (prompt+ where submit.raw contains one opted-in generation request, e.g. (prompt"Hello" = 5 payload bytes, 64 new tokens, greedy, logprobs top-5):SUBMIT 7 0 5 64 0 1 0 logprobs=5@@ -27,109 +33,654 @@ Run recipe (orchestrator; needs a real model -- CPU or CUDA build):stderr log ("[MTP] ..." / spec acceptance lines) so the run genuinelyexercised the draft-accept emit path rather than trivially passing.-Then:+A raw capture taken this way is a plain pipeline: it is not itself hash-bound+to the binary, container, input, and environment that produced it, and+NOTHING in this module binds it either -- the preamble gate below checks+only that the BANNER/LOADED text is a well-formed, self-consistent record of+what the engine printed about itself; it does not verify that text against+an independently computed binary or container digest. Whichever capture step+feeds this checker, prefer one that additionally binds and retains that+provenance (distinct raw stdout/stderr, the direct engine exit status, and a+hash over the binary/container/input/environment/protocol payloads) over a+bare pipeline or a lone DATA/HIT record, which is not decisive evidence on+its own.- python3 tests/check_data_logprob_gaps.py engine_stdout.raw --id 7 --topk 5+Then, against the resulting transcript, the numeric-channel check runs:++ python3 tests/check_data_logprob_gaps.py engine_stdout.raw \+ --id 7 --topk 5 --vocab 154880Checks performed:- - every DATA frame for --id has exactly 5 + 2k header fields, a parseable- float lp, and k == --topk (k may be < topk only if vocab < topk).- A non-finite lp ("nan"/"inf": degenerate logits, e.g. an all -inf row- after grammar masking) counts as PRESENT -- the channel carried a value,- which is exactly what this audit is for -- and is FLAGGED in the output- without failing the check (whether the engine should serialize such rows- differently is a U7b server-side register question, not a gap);- - ECHO frames (if the request also echoed) cover contiguous positions- 0..P-1, position 0 carrying "nan 0";+ - the transcript's capture shape (full-process / ready-suffix / request-+ only) is named by capture_mode() and enforced explicitly: the leading+ line must be a recognized global preamble record or a legitimate+ request-frame kind, and a transcript with any global record present+ but not correctly led (a "invalid" capture per capture_mode()) fails+ loud -- no transcript reaches the rest of the checks unclassified;+ - the startup BANNER/LOADED preamble, where present, parses exactly+ (the engine_evidence grammar); an unparsed preamble is a named+ failure quoting the offending line, never a silent pass;+ - exactly one ACCEPT and one DONE exist for --id, DONE's emitted count+ equals the positive DATA count, and no targeted ERROR or post-DONE+ frame exists;+ - every DATA/ECHO numeric tail has exactly the advertised fields and+ min(--topk,--vocab) unique token ids in range. Each numeric token+ (target logprob and every top-k logprob) must be spelled as the+ fixed six-decimal form dev's engine prints today ("fixed6", e.g.+ "-0.300000"), the %.17g form an earlier engine build printed+ ("c17g", e.g. "-2.7000000000000002"), or an exact nan/inf/-inf+ spelling -- whichever form parses -- and the VALUE that spelling+ carries must then be finite and non-positive; a nan/inf/-inf+ spelling always fails that value check, by construction, since none+ of those values are finite. That spelling is recognized at all only+ so a non-finite token gets one specific, named rejection ("target+ logprob is not finite/non-positive", quoting the exact token and+ byte offset) instead of a generic "matches neither form" failure --+ it is not an accepted value, just a diagnosed one. On today's engine+ (colibri.c) this is also not a normal, tolerable outcome to shrug+ off: grammar-constrained decoding (GRAMMAR=/SCHEMA=, method F)+ proposes speculative draft tokens verified against the model's own+ unmasked distribution and never zeroes or -inf's any vocabulary+ logit, so a non-finite emitted logprob can only reach the wire+ through the total-vocabulary numerical-collapse fallback documented+ at sample.h's dist_build()/argmax_v() (every logit in the row NaN or+ -inf) -- which those functions' own comments and stderr warning+ label "a numerical blow-up upstream", not a degenerate-but-valid+ row. Failing loud on it here names a real engine bug instead of+ quietly passing it through.+ A token that happens to satisfy BOTH finite spellings exactly+ (e.g. '%.17g' % -1.234567 == '-1.234567', itself also an exact+ six-decimal spelling) is+ "ambiguous": there is no way to tell, from the token alone, which+ engine format produced it, so no per-frame or per-transcript+ "consistent format" rule is enforced -- one was tried and had to be+ dropped (see the module history) because it produced false rejections+ on real %.17g transcripts purely from this ambiguity. The observed+ form counts (fixed6-only / c17g-only / ambiguous / special) are+ reported in the summary line as INFORMATION ONLY; they never affect+ the verdict;+ - ACCEPT's canonical prompt length P has exactly P ECHO frames at+ positions 0..P-1, position 0 carrying "nan 0";- payload framing (n bytes + newline) stays byte-exact throughout, so a- single malformed frame cannot hide by desynchronizing the parse.+ single malformed frame cannot hide by desynchronizing the parse; every+ reported problem cites the byte offset of the offending frame's own+ header line (not the frame that follows it).Exit 0 = no gaps; non-zero = at least one gap/malformed frame (listed)."""import argparse+import collectionsimport math+import os+import reimport sys+_TOOLS_DIR = os.path.join(os.path.dirname(os.path.dirname(+ os.path.abspath(__file__))), "tools")+if _TOOLS_DIR not in sys.path:+ sys.path.insert(0, _TOOLS_DIR)+from engine_evidence import PreambleError, parse_engine_preamble+++_INT32_MAX = 2**31 - 1+_UINT64_MAX = 2**64 - 1+_UINT_RE = re.compile(rb"(?:0|[1-9][0-9]*)")+_C17G_RE = re.compile(+ rb"-?(?:0|[1-9][0-9]*)(?:\.[0-9]+)?"+ rb"(?:e[+-](?:0[0-9]|[1-9][0-9]{1,2}))?")+_FIXED6_RE = re.compile(rb"-?(?:0|[1-9][0-9]*)\.[0-9]{6}")+_SPECIAL_TOKENS = {b"nan": math.nan, b"inf": math.inf, b"-inf": -math.inf}+_READY = b"\x01\x01READY\x01\x01"+_GLOBAL_KINDS = frozenset((+ b"BANNER", b"LOADED", b"READY", b"STAT", b"HWINFO", b"TIERS",+ b"EMAP", b"HITS", b"PROF",+))+_TARGETED_KINDS = frozenset((+ b"ACCEPT", b"DATA", b"ECHO", b"DONE", b"ERROR",+))+_LOWER_HEX_RE = re.compile(rb"[0-9a-f]*")+++def _uint(token, label, maximum=_INT32_MAX):+ if not _UINT_RE.fullmatch(token):+ raise ValueError(f"noncanonical {label}: {token!r}")+ value = int(token.decode("ascii"))+ if value > maximum:+ raise ValueError(f"{label} exceeds {maximum}: {token!r}")+ return value+++def _c17g(token, label):+ if not _C17G_RE.fullmatch(token):+ raise ValueError(f"noncanonical {label} %.17g token: {token!r}")+ value = float(token.decode("ascii"))+ if not math.isfinite(value) or format(value, ".17g").encode("ascii") != token:+ raise ValueError(f"not an exact finite {label} %.17g spelling: {token!r}")+ return value+++def _fixed6(token, label):+ if not _FIXED6_RE.fullmatch(token):+ raise ValueError(f"noncanonical {label} %.6f token: {token!r}")+ value = float(token.decode("ascii"))+ if not math.isfinite(value) or format(value, ".6f").encode("ascii") != token:+ raise ValueError(f"not an exact finite {label} %.6f spelling: {token!r}")+ return value+++def _numeric_value(token, label):+ """Classify and parse one engine numeric-tail token.++ Returns (value, form). "special" is an exact nan/inf/-inf spelling --+ libc's own %f/%g rendering of a non-finite double, which either wire+ format's snprintf call can emit identically. Otherwise the token is+ checked against BOTH finite grammars independently (never short-+ circuited): the fixed six-decimal form dev's engine prints today+ ("fixed6") and the %.17g form an earlier engine build printed+ ("c17g"). A token can satisfy both -- '%.17g' % -1.234567 ==+ '-1.234567', which is ALSO the exact six-decimal spelling of that+ same double -- and when it does, the form is "ambiguous": there is no+ way to tell, from the token alone, which engine format produced it.+ A token matching neither raises ValueError.++ An earlier version of this function tried fixed6 first and reported+ it whenever fixed6 matched, silently hiding the ambiguous case; that+ misclassified a large fraction of genuine %.17g tokens as fixed6 and+ fed a since-removed "consistent form per frame" check false mixed-+ form rejections on real transcripts. Classification is now purely+ informational (see check()/main()) precisely because it cannot be+ made unambiguous from the token alone.+ """+ if token in _SPECIAL_TOKENS:+ return _SPECIAL_TOKENS[token], "special"+ value = None+ is_fixed6 = is_c17g = False+ try:+ value = _fixed6(token, label)+ is_fixed6 = True+ except ValueError:+ pass+ try:+ value = _c17g(token, label)+ is_c17g = True+ except ValueError:+ pass+ if is_fixed6 and is_c17g:+ return value, "ambiguous"+ if is_fixed6:+ return value, "fixed6"+ if is_c17g:+ return value, "c17g"+ raise ValueError(+ f"{label} matches neither the fixed6 nor the c17g nor the "+ f"nan/inf spelling: {token!r}")+++def _fixed_metric(token, places, label, lower=0.0, upper=None):+ pattern=rb"-?(?:0|[1-9][0-9]*)\."+rb"[0-9]{"+str(places).encode()+rb"}"+ if not re.fullmatch(pattern,token):+ raise ValueError(f"noncanonical {label}: {token!r}")+ value=float(token.decode("ascii"))+ if not math.isfinite(value) or value<lower or (upper is not None and value>upper):+ raise ValueError(f"{label} outside [{lower},{upper}]: {token!r}")+ return value+++def _header_fields(line, byte_offset):+ problems=[]+ if not line:+ return [],[f"blank protocol header at byte {byte_offset}"]+ if (any(byte<0x20 or byte>0x7e for byte in line) or+ line.startswith(b" ") or line.endswith(b" ") or b" " in line):+ problems.append(f"noncanonical ASCII/space header at byte {byte_offset}: {line!r}")+ return line.split(),problems+++def _global_header(line, byte_offset):+ """Validate one exact production mux-global line.++ Return None when the line is not a recognized global. Recognized globals+ return a synthetic one-field marker so check() owns their process-level+ lifecycle before any numeric token can collide with a request id.+ """+ if (line.startswith(b"== GLM C engine") or+ line.startswith(b"loaded in")):+ kind = b"BANNER" if line.startswith(b"==") else b"LOADED"+ problems = []+ try:+ text = line.decode("ascii")+ parsed = parse_engine_preamble(text)+ if parsed is None or parsed["kind"].encode("ascii") != kind:+ raise PreambleError("owned preamble kind mismatch")+ except (UnicodeDecodeError, PreambleError) as exc:+ problems.append(+ f"malformed {kind.decode()} preamble at byte {byte_offset}: "+ f"{exc}; {line!r}")+ return [kind], problems++ if line == _READY:+ return [b"READY"], []++ kind = line.split(b" ", 1)[0]+ if kind == b"READY":+ return [b"READY"], [+ f"malformed READY global at byte {byte_offset}: expected {_READY!r}; {line!r}"+ ]+ if kind not in _GLOBAL_KINDS - {b"READY"}:+ return None+ problems = []+ try:+ if kind == b"STAT":+ fields = line.split(b" ")+ if (len(fields) != 5 or fields[1:4] != [b"0", b"0.00", b"0.0"]):+ raise ValueError("expected exact startup STAT fields")+ _fixed_metric(fields[4], 2, "STAT RSS")+ elif kind == b"HWINFO":+ # The final CPU|GPU field is produced by two %s conversions. It is+ # printable free text and may contain repeated spaces; the six+ # numeric/structural prefixes remain exact single-space fields.+ fields = line.split(b" ", 6)+ if len(fields) != 7:+ raise ValueError("expected six HWINFO prefixes and CPU|GPU tail")+ _uint(fields[1], "HWINFO core count")+ _fixed_metric(fields[2], 1, "HWINFO total RAM")+ _fixed_metric(fields[3], 1, "HWINFO available RAM")+ _uint(fields[4], "HWINFO GPU count")+ _fixed_metric(fields[5], 1, "HWINFO total VRAM")+ tail = fields[6]+ if (tail.count(b"|") != 1 or+ any(byte < 0x20 or byte > 0x7e for byte in tail)):+ raise ValueError("HWINFO tail is not printable CPU|GPU text")+ elif kind == b"TIERS":+ fields = line.split(b" ")+ if len(fields) != 6:+ raise ValueError("expected five TIERS fields")+ for index, label in enumerate(("VRAM", "RAM", "disk"), 1):+ _uint(fields[index], f"TIERS {label} count")+ _fixed_metric(fields[4], 2, "TIERS VRAM GB")+ _fixed_metric(fields[5], 2, "TIERS RAM GB")+ elif kind in (b"EMAP", b"HITS"):+ fields = line.split(b" ")+ if len(fields) != 4:+ raise ValueError(f"expected three {kind.decode()} fields")+ rows = _uint(fields[1], f"{kind.decode()} row count")+ cols = _uint(fields[2], f"{kind.decode()} column count")+ if cols < 1 or (kind == b"HITS" and rows < 1):+ raise ValueError(+ f"{kind.decode()} rows/columns outside producer domain")+ payload = fields[3]+ if not _LOWER_HEX_RE.fullmatch(payload):+ raise ValueError(f"{kind.decode()} payload is not lowercase hex")+ cells = rows * cols+ expected = cells * 2 if kind == b"EMAP" else ((cells + 7) // 8) * 2+ if len(payload) != expected:+ raise ValueError(+ f"{kind.decode()} payload length {len(payload)} != {expected}")+ if kind == b"EMAP":+ for index in range(cells):+ cell = int(payload[2 * index:2 * index + 2], 16)+ tier, heat = cell >> 6, cell & 0x3f+ if tier > 2:+ raise ValueError(+ f"EMAP cell {index} tier {tier} outside [0,2]")+ if heat > 32:+ raise ValueError(+ f"EMAP cell {index} heat {heat} outside [0,32]")+ elif cells & 7:+ final = int(payload[-2:], 16)+ used_mask = (1 << (cells & 7)) - 1+ if final & ~used_mask:+ raise ValueError("HITS final byte has nonzero padding bits")+ else: # PROF+ fields = line.split(b" ")+ if len(fields) != 10:+ raise ValueError("expected nine PROF fields")+ _fixed_metric(fields[1], 3, "PROF wall seconds")+ _uint(fields[2], "PROF prompt count")+ _uint(fields[3], "PROF completion count")+ for index, label in enumerate(+ ("disk", "wait", "matmul", "attention", "head"), 4):+ _fixed_metric(fields[index], 3, f"PROF {label} seconds")+ _uint(fields[9], "PROF forward count", _UINT64_MAX)+ except (ValueError, IndexError) as exc:+ problems.append(+ f"malformed {kind.decode()} global at byte {byte_offset}: {exc}; {line!r}")+ return [kind], problems++def parse_frames(blob):- """Parse the serve-mux stdout byte stream into (header_fields, payload)."""+ """Parse frames fail-closed, returning (frames, framing_problems).++ Each frame is (fields, payload, byte_offset), where byte_offset is the+ offset of THIS frame's own header line -- captured before the cursor+ advances past it, so every problem this module reports can cite the+ byte offset of the frame it is actually complaining about, not the+ frame that happens to follow it in the transcript.+ """frames = []+ problems = []i = 0n = len(blob)while i < n:+ line_start = ij = blob.find(b"\n", i)if j < 0:+ problems.append(f"truncated header at byte {line_start}")breakline = blob[i:j]i = j + 1- fields = line.split()+ global_header = _global_header(line, line_start)+ if global_header is None:+ fields, header_problems = _header_fields(line, line_start)+ else:+ fields, header_problems = global_header+ problems.extend(header_problems)if not fields:continuekind = fields[0]- if kind in (b"DATA", b"ECHO") and len(fields) >= 3:+ if kind in (b"DATA", b"ECHO"):+ if len(fields) < 3:+ problems.append(+ f"short {kind.decode(errors='replace')} header at "+ f"byte {line_start}: {fields!r}")+ breaktry:- size = int(fields[2])+ size = _uint(fields[2],"payload size")except ValueError:- frames.append((fields, None))- continue+ problems.append(+ f"invalid payload size at byte {line_start}: {fields!r}")+ break+ if size < 0:+ problems.append(+ f"negative payload size at byte {line_start}: {fields!r}")+ break+ if size > n - i:+ problems.append(+ f"truncated payload at byte {line_start}: need {size} bytes")+ breakpayload = blob[i:i + size]i += sizeif i < n and blob[i:i + 1] == b"\n":i += 1else:- frames.append((fields, b"<BAD TERMINATOR>"))- continue- frames.append((fields, payload))+ problems.append(+ f"missing payload terminator at byte {line_start} "+ f"after {fields!r}")+ break+ frames.append((fields, payload, line_start))else:- frames.append((fields, None))- return frames+ frames.append((fields, None, line_start))+ return frames, problems++def capture_mode(frames):+ """Name the explicit transcript shape without grading its lifecycle."""+ if not frames:+ return "request-only"+ first = frames[0][0][0]+ if first == b"BANNER":+ return "full-process"+ if first == b"READY":+ return "ready-suffix"+ if any(fields[0] in _GLOBAL_KINDS for fields, _, _ in frames):+ return "invalid"+ return "request-only"-def check(frames, request_id, topk):- rid = str(request_id).encode()++def _numeric_tail(fields, field_offset, expected_k, vocab, label, byte_offset):+ """Validate one DATA/ECHO numeric tail starting at fields[field_offset].++ Returns (problems, forms) where forms is the LIST of numeric-token+ "form" tags (see _numeric_value) observed in this tail, in order --+ a list, not a set, so the caller can tally counts. No rule requires a+ tail's forms to agree with one another: per-token classification is+ inherently ambiguous (a token can be an exact spelling under both+ finite grammars at once), so there is no reliable way to tell a+ genuinely mixed-format tail from an entirely single-format one that+ merely contains some ambiguous tokens -- see _numeric_value.+ """problems = []- flags = []+ forms = []+ try:+ lp, lp_form = _numeric_value(fields[field_offset], f"{label} target logprob")+ k = _uint(fields[field_offset + 1], f"{label} top-k count", 32)+ except (ValueError, IndexError):+ return ([f"malformed {label} numeric fields at byte {byte_offset}: "+ f"{fields!r}"], forms)+ forms.append(lp_form)+ if not math.isfinite(lp) or lp > 0.0:+ problems.append(+ f"{label} target logprob is not finite/non-positive at byte "+ f"{byte_offset}: {fields!r}")+ if k != expected_k:+ problems.append(+ f"{label} top-k {k} != expected {expected_k} at byte "+ f"{byte_offset}: {fields!r}")+ want_fields = field_offset + 2 + 2 * k+ if len(fields) != want_fields:+ return (problems + [f"{label} field count {len(fields)} != "+ f"{want_fields} at byte {byte_offset}: {fields!r}"],+ forms)+ ids = []+ for idx in range(k):+ try:+ token_id = _uint(fields[field_offset + 2 + 2 * idx],+ f"{label} token id", _INT32_MAX)+ token_lp, token_form = _numeric_value(+ fields[field_offset + 3 + 2 * idx], f"{label} token logprob")+ except ValueError:+ problems.append(+ f"malformed {label} top-k pair {idx} at byte {byte_offset}: "+ f"{fields!r}")+ continue+ forms.append(token_form)+ if not 0 <= token_id < vocab:+ problems.append(+ f"{label} token id {token_id} outside [0,{vocab}) at byte "+ f"{byte_offset}: {fields!r}")+ if token_id in ids:+ problems.append(+ f"{label} duplicate token id {token_id} at byte "+ f"{byte_offset}: {fields!r}")+ ids.append(token_id)+ if not math.isfinite(token_lp) or token_lp > 0.0:+ problems.append(+ f"{label} token {token_id} logprob is not finite/non-positive "+ f"at byte {byte_offset}: {fields!r}")+ return problems, forms+++def check(frames, framing_problems, request_id, topk, vocab):+ problems = list(framing_problems)+ mode = capture_mode(frames)+ if frames:+ first_kind = frames[0][0][0]+ if first_kind not in _GLOBAL_KINDS and first_kind not in _TARGETED_KINDS:+ problems.append(+ f"unrecognized frame opens the transcript at byte "+ f"{frames[0][2]} (not a global preamble or a request "+ f"frame): {frames[0][0]!r}")+ if mode == "invalid":+ problems.append(+ "invalid capture mode: global records present without a "+ "recognized BANNER/READY lead frame")+ try:+ rid = str(request_id).encode("ascii",errors="strict")+ rid_value=_uint(rid,"requested id",_UINT64_MAX)+ if rid_value < 1:+ raise ValueError("requested id must be positive")+ except (UnicodeEncodeError,ValueError) as exc:+ return 0, 0, problems+[str(exc)], mode, frozenset()data_frames = 0echo_positions = []- for fields, payload in frames:- if len(fields) < 2 or fields[1] != rid:- continue+ accepts = []+ dones = []+ accept_prompts = []+ done_values = []+ done_seen = False+ data_seen = False+ global_indices = {kind: [] for kind in _GLOBAL_KINDS}+ expected_k = min(topk, vocab)+ forms_used = []+ for frame_index, (fields, payload, offset) in enumerate(frames):kind = fields[0]+ if kind in _GLOBAL_KINDS:+ global_indices[kind].append(frame_index)+ continue+ if len(fields)<2:+ continue+ try:+ frame_id=_uint(fields[1],"frame request id",_UINT64_MAX)+ except ValueError as exc:+ if kind in _TARGETED_KINDS:+ problems.append(f"{exc} at byte {offset}")+ continue+ if frame_id!=rid_value:+ continue+ if done_seen:+ problems.append(+ f"frame for id {request_id} after DONE at byte {offset}: "+ f"{fields!r}")+ if kind == b"ACCEPT":+ accepts.append(frame_index)+ if len(fields) != 3:+ problems.append(+ f"malformed ACCEPT frame at byte {offset}: {fields!r}")+ else:+ try:+ prompt=_uint(fields[2],"ACCEPT prompt length")+ if prompt<1:+ raise ValueError+ except ValueError:+ problems.append(+ f"invalid ACCEPT prompt length at byte {offset}: "+ f"{fields!r}")+ else:+ accept_prompts.append(prompt)+ continue+ if kind == b"ERROR":+ problems.append(+ f"target request returned ERROR at byte {offset}: {fields!r}")+ continue+ if kind == b"DONE":+ dones.append(frame_index)+ done_seen = True+ if len(fields) != 9 or fields[2] != b"STAT":+ problems.append(+ f"malformed DONE frame at byte {offset}: {fields!r}")+ done_values.append(None)+ continue+ try:+ emitted=_uint(fields[3],"DONE emitted count")+ tps=_fixed_metric(fields[4],2,"DONE tokens/second")+ hit=_fixed_metric(fields[5],1,"DONE hit percentage",upper=100.0)+ rss=_fixed_metric(fields[6],2,"DONE RSS")+ prompt=_uint(fields[7],"DONE prompt-token count")+ flag=_uint(fields[8],"DONE length_limited",1)+ if prompt<1:+ raise ValueError("DONE prompt-token count is not positive")+ except ValueError:+ problems.append(+ f"malformed DONE stats at byte {offset}: {fields!r}")+ done_values.append(None)+ continue+ done_values.append((emitted,tps,hit,rss,prompt,flag))+ continueif kind == b"DATA":+ data_seen = Truedata_frames += 1+ if not accepts:+ problems.append(+ f"DATA before ACCEPT at byte {offset}: {fields!r}")+ if payload is None:+ problems.append(+ f"DATA missing validated payload at byte {offset}: "+ f"{fields!r}")if len(fields) == 3:- problems.append(f"GAP: legacy 3-field DATA frame #{data_frames}"- f" (payload {payload!r}) has no logprob")- continue- try:- lp = float(fields[3])- k = int(fields[4])- except (ValueError, IndexError):- problems.append(f"malformed DATA numeric fields: {fields!r}")+ problems.append(+ f"GAP: legacy 3-field DATA frame #{data_frames} at byte "+ f"{offset} (payload {payload!r}) has no logprob")continue- if not math.isfinite(lp):- # PRESENT, not a gap: the channel carried a value; degenerate- # logits (an all -inf row, say) legitimately produce nan/inf.- # Flagged so a reviewer sees it; never fails the audit.- flags.append(f"non-finite lp in DATA frame #{data_frames}"- f" (present, flagged): {fields!r}")- if k != topk:- problems.append(f"DATA top-k {k} != requested {topk}: {fields!r}")- if len(fields) != 5 + 2 * k:- problems.append(f"DATA field count {len(fields)} != 5+2k: {fields!r}")+ tail_problems, tail_forms = _numeric_tail(+ fields, 3, expected_k, vocab, "DATA", offset)+ problems.extend(tail_problems)+ forms_used.extend(tail_forms)elif kind == b"ECHO":+ if data_seen:+ problems.append(+ f"ECHO after DATA at byte {offset}: {fields!r}")+ if not accepts:+ problems.append(+ f"ECHO before ACCEPT at byte {offset}: {fields!r}")+ if payload is None:+ problems.append(+ f"ECHO missing validated payload at byte {offset}: "+ f"{fields!r}")try:- pos = int(fields[3])+ pos = _uint(fields[3],"ECHO position")except (ValueError, IndexError):- problems.append(f"malformed ECHO frame: {fields!r}")+ problems.append(+ f"malformed ECHO frame at byte {offset}: {fields!r}")continueecho_positions.append(pos)- if pos == 0 and (fields[4] != b"nan" or fields[5] != b"0"):- problems.append(f"ECHO position 0 should carry 'nan 0': {fields!r}")- if pos > 0 and len(fields) < 6:- problems.append(f"short ECHO frame: {fields!r}")- if echo_positions and echo_positions != list(range(len(echo_positions))):- problems.append(f"ECHO positions not contiguous from 0: {echo_positions}")- return data_frames, len(echo_positions), problems, flags+ if pos == 0:+ if len(fields) != 6 or fields[4] != b"nan" or fields[5] != b"0":+ problems.append(+ f"ECHO position 0 should carry exactly 'nan 0' at "+ f"byte {offset}: {fields!r}")+ else:+ tail_problems, tail_forms = _numeric_tail(+ fields, 4, expected_k, vocab, "ECHO", offset)+ problems.extend(tail_problems)+ forms_used.extend(tail_forms)+ else:+ problems.append(+ f"unknown targeted frame kind at byte {offset}: {fields!r}")+ any_globals = any(global_indices[kind] for kind in _GLOBAL_KINDS)+ if global_indices[b"BANNER"] or global_indices[b"LOADED"]:+ required = (+ (b"BANNER", 0), (b"LOADED", 1), (b"READY", 2), (b"STAT", 3),+ (b"HWINFO", 4), (b"TIERS", 5), (b"EMAP", 6),+ )+ for owned_kind, expected_index in required:+ found = global_indices[owned_kind]+ if (owned_kind in (b"BANNER", b"LOADED", b"READY", b"STAT") and+ found != [expected_index]):+ problems.append(+ f"expected one {owned_kind.decode()} as frame "+ f"{expected_index}, found {found}")+ elif (owned_kind not in (b"BANNER", b"LOADED", b"READY", b"STAT") and+ (not found or found[0] != expected_index)):+ problems.append(+ f"expected startup {owned_kind.decode()} as frame "+ f"{expected_index}, found {found}")+ if len(frames) > 7 and frames[7][0][0] not in (b"ACCEPT", b"ERROR"):+ problems.append(+ f"unexpected full-process pre-request frame 7: {frames[7][0]!r}")+ elif any_globals:+ ready = global_indices[b"READY"]+ stat = global_indices[b"STAT"]+ if ready != [0]:+ problems.append(f"expected one READY as frame 0, found {ready}")+ if stat != [1]:+ problems.append(f"expected one startup STAT as frame 1, found {stat}")+ if len(accepts) != 1:+ problems.append(f"expected exactly one ACCEPT, found {len(accepts)}")+ elif len(accept_prompts)==1:+ prompt=accept_prompts[0]+ if echo_positions!=list(range(prompt)):+ problems.append(f"ECHO positions {echo_positions} != required 0..{prompt-1}")+ if len(dones) != 1:+ problems.append(f"expected exactly one DONE, found {len(dones)}")+ elif len(done_values)==1 and done_values[0] is not None:+ emitted,_,_,_,done_prompt,_=done_values[0]+ if emitted!=data_frames:+ problems.append(f"DONE emitted {emitted} != observed DATA {data_frames}")+ if len(accept_prompts)==1 and done_prompt!=accept_prompts[0]:+ problems.append(f"DONE prompt count {done_prompt} != ACCEPT {accept_prompts[0]}")+ if data_frames<=0:+ problems.append("no DATA frames found for this id -- wrong id, or the run produced nothing")+ return data_frames, len(echo_positions), problems, mode, collections.Counter(forms_used)def main():@@ -138,21 +689,35 @@ def main():parser.add_argument("--id", required=True, help="request id to audit")parser.add_argument("--topk", type=int, required=True,help="the SUBMIT logprobs=k value the request used")+ parser.add_argument("--vocab", type=int, required=True,+ help="checkpoint vocabulary size used to bound token ids")args = parser.parse_args()+ try:+ if _uint(args.id.encode("ascii"), "requested id", _UINT64_MAX) < 1:+ raise ValueError("requested id must be positive")+ except (UnicodeEncodeError, ValueError) as exc:+ parser.error(str(exc))+ if not 1 <= args.topk <= 32:+ parser.error("--topk must be in 1..32 for an opted-in B1 evidence run")+ if args.vocab < 1:+ parser.error("--vocab must be positive")blob = open(args.transcript, "rb").read()- frames = parse_frames(blob)- data_frames, echo_frames, problems, flags = check(frames, args.id, args.topk)- print(f"[gapcheck] id={args.id}: {data_frames} DATA frames, "- f"{echo_frames} ECHO frames, {len(flags)} flagged non-finite")- if data_frames == 0:- problems.append("no DATA frames found for this id -- wrong id, or the "- "run produced nothing (check ERROR frames)")- for flag in flags:- print(f"[gapcheck] FLAG: {flag}")+ frames, framing_problems = parse_frames(blob)+ data_frames, echo_frames, problems, mode, forms_used = check(+ frames, framing_problems, args.id, args.topk, args.vocab)+ # Informational only -- see _numeric_value: per-token form+ # classification is inherently ambiguous, so this tally never affects+ # the verdict, only what a reviewer sees.+ forms_summary = (", ".join(f"{form}={forms_used[form]}"+ for form in sorted(forms_used))+ if forms_used else "none")+ print(f"[gapcheck] id={args.id}: capture_mode={mode}, {data_frames} DATA "+ f"frames, {echo_frames} ECHO frames, numeric form tally: "+ f"{forms_summary}")for problem in problems:print(f"[gapcheck] {problem}")- verdict = ("FAIL" if problems- else "PASS: every generated token carries a logprob value")+ verdict = ("FAIL" if problems else+ "PASS: complete request; every generated token carries a valid logprob table")print(f"[gapcheck] {verdict}")return 1 if problems else 0diff --git a/c/tests/test_check_data_logprob_gaps.py b/c/tests/test_check_data_logprob_gaps.pynew file mode 100644index 00000000..0acee63b--- /dev/null+++ b/c/tests/test_check_data_logprob_gaps.py@@ -0,0 +1,1157 @@+"""tests/check_data_logprob_gaps.py must accept only a complete, well-formed+raw engine-stdout transcript for one opted-in request and reject every+other input: an unparsed or mismatched startup preamble, a legacy 3-field+DATA frame hiding a dropped logprob channel anywhere in the generation, a+numeric tail with the wrong top-k count or an out-of-range/duplicate token+id, malformed framing, a missing or duplicated ACCEPT/DONE, and every other+gap the module's docstring claims to catch.++Checks enumerated from the source (`check_data_logprob_gaps.py`, read in+full before writing this module) and covered below:++- `_uint`, `_c17g`, `_fixed_metric`: the noncanonical-ASCII-integer grammar,+ the exact finite `%.17g` spelling, and the fixed-decimal-place grammar+ with its inclusive bounds.+- `_header_fields` / `_global_header`: non-printable-ASCII and stray-space+ rejection on an ordinary protocol header, and the exact field grammar+ for every recognized mux-global record (BANNER/LOADED preamble, READY,+ STAT, HWINFO, TIERS, EMAP, HITS, PROF).+- `parse_frames`: byte-exact DATA/ECHO payload framing, so a single+ malformed frame cannot desynchronize the parse.+- `capture_mode`: the four explicit transcript shapes it names.+- `_numeric_tail` / `check`: every check enumerated in the module+ docstring, including the legacy 3-field DATA gap this checker exists to+ catch, the `min(topk, vocab)` top-k cap, and out-of-range/duplicate+ token ids.++Every fixture here is a literal transcript built by hand from the module's+documented wire grammar -- no expected value is produced by calling the+checker under test. The transcript-shaped fixtures (`_GapFixture`'s BANNER/+LOADED/VALID/echo/transcript/telemetry/full_capture/complete_capture+helpers) and the `GapCheckerEvidenceTests` methods that exercise them are+carried over unchanged from the checker's own evidence-consumer suite,+excluding the one method that requires a compiled fixture binary.+"""+import ast+import collections+import importlib.util+import inspect+import pathlib+import random+import re+import subprocess+import sys+import tempfile+import unittest++_HERE = pathlib.Path(__file__).resolve().parent+_spec = importlib.util.spec_from_file_location(+ "check_data_logprob_gaps", _HERE / "check_data_logprob_gaps.py")+GAPS = importlib.util.module_from_spec(_spec)+_spec.loader.exec_module(GAPS)+++class _GapFixture(unittest.TestCase):+ BANNER = (+ b"== GLM C engine (glm_moe_dsa), cache=64 experts/layer | "+ b"compute experts@4-bit dense@8-bit | idot: neon-i8mm ==\n")+ LOADED = (+ b"loaded in 1.00s | resident dense: 1.00 MB | "+ b"layers=78 experts=256 | MTP ACTIVE (draft=1)\n")+ VALID = (+ b"ACCEPT 7 1\n"+ b"ECHO 7 1 0 nan 0\n"+ b"x\n"+ b"DATA 7 1 -2.7000000000000002 2 0 -2.7000000000000002 1 -11.123455999999999\n"+ b"x\n"+ b"DONE 7 STAT 1 0.10 100.0 10.00 1 0\n"+ )++ @staticmethod+ def echo(pos):+ if pos == 0:+ return b"ECHO 7 1 0 nan 0\nx\n"+ return (f"ECHO 7 1 {pos} -2.7000000000000002 2 0 -2.7000000000000002 1 -11.123455999999999\n"+ "x\n").encode("ascii")++ @classmethod+ def transcript(cls, prompt=1, positions=None, emitted=1, done_prompt=None,+ tps="0.10", hit="100.0", rss="10.00", flag=0):+ if positions is None:+ positions = list(range(prompt))+ if done_prompt is None:+ done_prompt = prompt+ parts = [f"ACCEPT 7 {prompt}\n".encode("ascii")]+ parts.extend(cls.echo(pos) for pos in positions)+ parts.extend([+ b"DATA 7 1 -2.7000000000000002 2 0 -2.7000000000000002 1 -11.123455999999999\nx\n",+ (f"DONE 7 STAT {emitted} {tps} {hit} {rss} "+ f"{done_prompt} {flag}\n").encode("ascii"),+ ])+ return b"".join(parts)++ def problems(self, blob, topk=2, vocab=3):+ frames, framing = GAPS.parse_frames(blob)+ data, echo, problems, _mode, _forms = GAPS.check(+ frames, framing, "7", topk, vocab)+ return data, echo, problems++ def full_problems(self, blob, topk=2, vocab=3):+ """Like problems(), but also returns capture_mode and the numeric+ form(s) observed -- for tests that need those two new signals."""+ frames, framing = GAPS.parse_frames(blob)+ return GAPS.check(frames, framing, "7", topk, vocab)++ @staticmethod+ def telemetry(cpu=b"AMD Ryzen 9", cores=b"16"):+ return b"".join((+ b"HWINFO " + cores + b" 128.0 64.0 2 48.0 " + cpu ++ b"|CUDA device x2\n",+ b"TIERS 128 256 1024 48.00 32.50\n",+ b"EMAP 2 2 00010203\n",+ b"HITS 2 2 0f\n",+ b"PROF 1.250 1 1 0.100 0.200 0.300 0.400 0.500 7\n",+ ))++ @classmethod+ def full_capture(cls):+ startup = (+ b"\x01\x01READY\x01\x01\n"+ b"STAT 0 0.00 0.0 10.00\n"+ b"HWINFO 16 128.0 64.0 2 48.0 AMD Ryzen 9|CUDA device x2\n"+ b"TIERS 128 256 1024 48.00 32.50\n"+ b"EMAP 2 2 00010203\n"+ )+ other = (+ b"ACCEPT 8 1\n"+ b"ECHO 8 1 0 nan 0\nq\n"+ b"DATA 8 1 -2.7000000000000002 2 0 -2.7000000000000002 1 -11.123455999999999\nq\n" ++ cls.telemetry(cpu=b"AMD Ryzen 9") ++ b"DONE 8 STAT 1 0.10 100.0 10.00 1 0\n"+ )+ target = (+ b"ACCEPT 7 1\n"+ b"ECHO 7 1 0 nan 0\nx\n"+ b"DATA 7 1 -2.7000000000000002 2 0 -2.7000000000000002 1 -11.123455999999999\nx\n" ++ cls.telemetry(cpu=b"AMD Ryzen 9") ++ b"DONE 7 STAT 1 0.10 100.0 10.00 1 0\n"+ )+ return startup + other + target++ @classmethod+ def complete_capture(cls, loaded=None):+ return cls.BANNER + (loaded if loaded is not None else cls.LOADED) + cls.full_capture()+++class GapCheckerEvidenceTests(_GapFixture):+ """Ported unchanged from the checker's own evidence-consumer suite,+ excluding the one method that scores a compiled fixture binary's+ `%.17g` corpus (needs a build, not available here)."""++ def test_complete_request_passes(self):+ for prompt in (1, 3):+ with self.subTest(prompt=prompt):+ data, echo, problems = self.problems(self.transcript(prompt))+ self.assertEqual((data, echo), (1, prompt))+ self.assertEqual(problems, [])++ def test_full_raw_mux_capture_passes_without_filtering(self):+ data, echo, problems = self.problems(self.full_capture())+ self.assertEqual((data, echo), (1, 1))+ self.assertEqual(problems, [])+ frames, _ = GAPS.parse_frames(self.full_capture())+ self.assertEqual(GAPS.capture_mode(frames), "ready-suffix")++ def test_complete_process_capture_passes_without_filtering(self):+ data, echo, problems = self.problems(self.complete_capture())+ self.assertEqual((data, echo), (1, 1))+ self.assertEqual(problems, [])+ frames, _ = GAPS.parse_frames(self.complete_capture())+ self.assertEqual(GAPS.capture_mode(frames), "full-process")++ for state, draft in ((b"ACTIVE", 0), (b"ACTIVE", 1),+ (b"absent", 0), (b"absent", 2),+ (b"DISABLED (multiplexed serve)", 0)):+ loaded = (+ b"loaded in 1.00s | resident dense: 1.00 MB | "+ b"layers=78 experts=256 | MTP " + state ++ b" (draft=" + str(draft).encode("ascii") + b")\n")+ _, _, problems = self.problems(self.complete_capture(loaded))+ self.assertEqual(problems, [], (state, draft, problems))++ def test_complete_process_preamble_lifecycle_is_exact(self):+ base = self.complete_capture()+ suffix = self.full_capture()+ cases = (+ suffix,+ base.replace(self.BANNER, b"", 1),+ base.replace(self.LOADED, b"", 1),+ self.LOADED + self.BANNER + suffix,+ self.BANNER + self.BANNER + self.LOADED + suffix,+ self.BANNER + self.LOADED + self.LOADED + suffix,+ b"unknown preamble\n" + base,+ base.replace(b"idot: neon-i8mm", b"idot: fabricated", 1),+ base.replace(b"loaded in 1.00s", b"loaded in 1.0s", 1),+ )+ # The first case is the separately supported READY-suffix mode.+ self.assertEqual(self.problems(cases[0])[2], [])+ for blob in cases[1:]:+ with self.subTest(blob=blob[:180]):+ _, _, problems = self.problems(blob)+ self.assertTrue(problems)++ def test_load_state_ranges_and_metrics_are_exact_in_full_capture(self):+ base = self.LOADED+ overflow = b"9" * 400 + b".00"+ cases = (+ base.replace(b"ACTIVE (draft=1)",+ b"DISABLED (multiplexed serve) (draft=1)"),+ base.replace(b"ACTIVE", b"fabricated"),+ base.replace(b"draft=1", b"draft=64"),+ base.replace(b"layers=78", b"layers=0"),+ base.replace(b"layers=78", b"layers=129"),+ base.replace(b"experts=256", b"experts=0"),+ base.replace(b"experts=256", b"experts=4097"),+ base.replace(b"loaded in 1.00s", b"loaded in -0.01s"),+ base.replace(b"loaded in 1.00s", b"loaded in nan s"),+ base.replace(b"loaded in 1.00s", b"loaded in infs"),+ base.replace(b"loaded in 1.00s", b"loaded in " + overflow + b"s"),+ base.replace(b"loaded in 1.00s", b"loaded in 1.0s"),+ base.replace(b"resident dense: 1.00 MB",+ b"resident dense: -0.01 MB"),+ base.replace(b"resident dense: 1.00 MB",+ b"resident dense: nan MB"),+ base.replace(b"resident dense: 1.00 MB",+ b"resident dense: " + overflow + b" MB"),+ base.replace(b"resident dense: 1.00 MB",+ b"resident dense: 1.0 MB"),+ )+ for loaded in cases:+ with self.subTest(loaded=loaded):+ _, _, problems = self.problems(self.complete_capture(loaded))+ self.assertTrue(any("malformed LOADED" in p for p in problems),+ problems)++ for layers, experts in ((1, 1), (128, 4096)):+ loaded = base.replace(b"layers=78", f"layers={layers}".encode())+ loaded = loaded.replace(b"experts=256", f"experts={experts}".encode())+ self.assertEqual(self.problems(self.complete_capture(loaded))[2], [])++ def test_emap_and_hits_producer_domains_are_exact(self):+ for byte in (b"00", b"20", b"40", b"60", b"80", b"a0"):+ _, problems = GAPS._global_header(b"EMAP 1 1 " + byte, 0)+ self.assertEqual(problems, [], byte)+ for byte in (b"21", b"61", b"a1"):+ _, problems = GAPS._global_header(b"EMAP 1 1 " + byte, 0)+ self.assertTrue(any("heat" in p for p in problems), problems)+ for byte in (b"c0", b"ff"):+ _, problems = GAPS._global_header(b"EMAP 1 1 " + byte, 0)+ self.assertTrue(any("tier" in p for p in problems), problems)++ _, problems = GAPS._global_header(b"EMAP 0 2 ", 0)+ self.assertEqual(problems, [])+ _, problems = GAPS._global_header(b"EMAP 0 2 00", 0)+ self.assertTrue(any("payload length" in p for p in problems), problems)++ for line in (b"HITS 2 2 0f", b"HITS 1 8 ff"):+ _, problems = GAPS._global_header(line, 0)+ self.assertEqual(problems, [], line)+ for line in (b"HITS 2 2 1f", b"HITS 2 2 ff"):+ _, problems = GAPS._global_header(line, 0)+ self.assertTrue(any("padding" in p for p in problems), problems)++ def test_global_numeric_fields_never_collide_with_request_id(self):+ capture = self.full_capture().replace(+ b"HWINFO 16 128.0", b"HWINFO 7 128.0")+ capture = capture.replace(+ b"TIERS 128 256 1024", b"TIERS 7 256 1024")+ capture = capture.replace(b"EMAP 2 2 00010203", b"EMAP 7 1 00010203040506")+ capture = capture.replace(b"HITS 2 2 0f", b"HITS 7 1 7f")+ data, echo, problems = self.problems(capture)+ self.assertEqual((data, echo), (1, 1))+ self.assertEqual(problems, [])++ def test_malformed_or_misordered_global_records_never_pass(self):+ base = self.full_capture()+ cases = (+ base.replace(b"\x01\x01READY\x01\x01", b"READY", 1),+ base.replace(b"STAT 0 0.00 0.0 10.00", b"STAT 0 0.0 0.0 10.00", 1),+ base.replace(b"HWINFO 16 128.0", b"HWINFO 016 128.0", 1),+ base.replace(b"HWINFO 16 128.0", b"HWINFO 16 128.0", 1),+ base.replace(b"TIERS 128 256", b"TIERS 1_28 256", 1),+ base.replace(b"EMAP 2 2 00010203", b"EMAP 2 2 0001020F", 1),+ base.replace(b"HITS 2 2 0f", b"HITS 2 2 000f", 1),+ base.replace(b"PROF 1.250 1 1", b"PROF 1.25 1 1", 1),+ base.replace(b"PROF 1.250 1 1", b"PROF 1.250 01 1", 1),+ base.replace(b"PROF 1.250 1 1", b"PROF 1.250 1 1_0", 1),+ base.replace(b"\x01\x01READY\x01\x01\n", b"", 1),+ base.replace(b"STAT 0 0.00 0.0 10.00\n", b"", 1),+ base.replace(+ b"\x01\x01READY\x01\x01\nSTAT 0 0.00 0.0 10.00\n",+ b"STAT 0 0.00 0.0 10.00\n\x01\x01READY\x01\x01\n", 1),+ base.replace(+ b"\x01\x01READY\x01\x01\n",+ b"\x01\x01READY\x01\x01\n\x01\x01READY\x01\x01\n", 1),+ base.replace(+ b"STAT 0 0.00 0.0 10.00\n",+ b"STAT 0 0.00 0.0 10.00\nSTAT 0 0.00 0.0 10.00\n", 1),+ )+ for blob in cases:+ with self.subTest(blob=blob[:100]):+ _, _, problems = self.problems(blob)+ self.assertTrue(problems)++ def test_request_id_zero_refuses_before_record_matching(self):+ frames, framing = GAPS.parse_frames(self.full_capture())+ data, echo, problems, mode, forms = GAPS.check(+ frames, framing, "0", 2, 3)+ self.assertEqual((data, echo), (0, 0))+ self.assertEqual(problems, ["requested id must be positive"])+ self.assertEqual(forms, frozenset())++ with tempfile.TemporaryDirectory() as tmp:+ capture = pathlib.Path(tmp) / "capture.raw"+ capture.write_bytes(self.full_capture())+ proc = subprocess.run(+ [sys.executable, str(pathlib.Path(GAPS.__file__)), str(capture),+ "--id", "0", "--topk", "2", "--vocab", "3"],+ stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=False)+ self.assertEqual(proc.returncode, 2)+ self.assertIn(b"requested id must be positive", proc.stderr)++ def test_uint_primitive_and_coordinated_underscore_bite(self):+ for token, value in ((b"0", 0), (b"1", 1), (b"10", 10),+ (b"2147483647", 2147483647)):+ with self.subTest(token=token):+ self.assertEqual(GAPS._uint(token, "fixture"), value)+ for token in (b"+1", b"01", b"1_0", b" 1", b"1 ", b"1\t0",+ "١".encode("utf-8")):+ with self.subTest(token=token):+ with self.assertRaises(ValueError):+ GAPS._uint(token, "fixture")++ parts = [b"ACCEPT 7 1_0\n"]+ parts.extend(self.echo(pos) for pos in range(10))+ parts.extend((+ b"DATA 7 1 -2.7000000000000002 2 0 -2.7000000000000002 1 -11.123455999999999\nx\n",+ b"DONE 7 STAT 1 0.10 100.0 10.00 1_0 0\n",+ ))+ data, echo, problems = self.problems(b"".join(parts))+ self.assertEqual((data, echo), (1, 10))+ self.assertTrue(any("invalid ACCEPT prompt length" in p for p in problems))+ self.assertTrue(any("malformed DONE stats" in p for p in problems))+ self.assertFalse(any("ECHO positions" in p or "DONE prompt count" in p+ for p in problems), problems)++ def test_malformed_framing_never_passes(self):+ bad = [+ b"ACCEPT 7 1\nDATA 7 nope -2.7000000000000002 0\n",+ b"ACCEPT 7 1\nDATA 7 5 -2.7000000000000002 0\nx\n",+ b"ACCEPT 7 1\nDATA 7 1 -2.7000000000000002 0\nx",+ b"ACCEPT 7 1", # missing header newline+ ]+ for blob in bad:+ with self.subTest(blob=blob):+ _, framing_problems = GAPS.parse_frames(blob)+ self.assertTrue(framing_problems)++ def test_invalid_topk_tables_never_pass(self):+ headers = [+ b"DATA 7 1 -2.7000000000000002 2 -1 -2.7000000000000002 1 -11.123455999999999", # out-of-range id+ b"DATA 7 1 -2.7000000000000002 2 0 -2.7000000000000002 0 -11.123455999999999", # duplicate id+ b"DATA 7 1 0.125 2 0 -2.7000000000000002 1 -11.123455999999999", # positive target lp+ b"DATA 7 1 -2.7000000000000002 2 0 0.125 1 -11.123455999999999", # positive top-k lp+ b"DATA 7 1 nan 2 0 -2.7000000000000002 1 -11.123455999999999", # pending nonfinite policy+ b"DATA 7 1 -2.7000000000000002 2 0 -2.7000000000000002 3 -11.123455999999999", # id == vocab+ ]+ for header in headers:+ blob = (b"ACCEPT 7 1\n" + self.echo(0) + header + b"\nx\n" ++ b"DONE 7 STAT 1 0.10 100.0 10.00 1 0\n")+ with self.subTest(header=header):+ _, _, problems = self.problems(blob)+ self.assertTrue(problems)++ def test_missing_lifecycle_or_partial_denominator_never_passes(self):+ no_accept = self.VALID.split(b"\n", 1)[1]+ no_done = self.VALID.rsplit(b"DONE", 1)[0]+ wrong_done = self.VALID.replace(b"DONE 7 STAT 1", b"DONE 7 STAT 2")+ error = self.VALID.replace(+ b"DONE 7 STAT 1 0.10 100.0 10.00 1 0", b"ERROR 7 ENGINE")+ cases = (+ (no_accept, "expected exactly one ACCEPT"),+ (no_done, "expected exactly one DONE"),+ (wrong_done, "DONE emitted 2 != observed DATA 1"),+ (error, "target request returned ERROR"),+ )+ for blob, expected in cases:+ with self.subTest(blob=blob):+ _, _, problems = self.problems(blob)+ self.assertTrue(any(expected in problem for problem in problems), problems)++ def test_unknown_targeted_kind_and_echo_after_data_never_pass(self):+ unknown = self.VALID.replace(+ b"DONE 7 STAT", b"MYSTERY 7 value\nDONE 7 STAT")+ echo_after = (+ b"ACCEPT 7 1\n"+ b"DATA 7 1 -2.7000000000000002 2 0 -2.7000000000000002 1 -11.123455999999999\n"+ b"x\n"+ b"ECHO 7 1 0 nan 0\n"+ b"x\n"+ b"DONE 7 STAT 1 0.10 100.0 10.00 1 0\n"+ )+ for blob in (unknown, echo_after):+ with self.subTest(blob=blob):+ _, _, problems = self.problems(blob)+ self.assertTrue(problems)++ def test_echo_denominator_and_order_are_exact(self):+ cases = ([], [0], [0, 1], [0, 2], [0, 1, 1], [1, 0, 2], [0, 1, 2, 3])+ for positions in cases:+ with self.subTest(positions=positions):+ _, _, problems = self.problems(self.transcript(3, positions))+ self.assertTrue(any("ECHO positions" in p for p in problems), problems)++ def test_done_denominator_and_prompt_joins_bite_independently(self):+ zero_data = self.VALID.replace(+ b"DATA 7 1 -2.7000000000000002 2 0 -2.7000000000000002 1 -11.123455999999999\nx\n", b"").replace(+ b"DONE 7 STAT 1", b"DONE 7 STAT 0")+ data, _, problems = self.problems(zero_data)+ self.assertEqual(data, 0)+ self.assertEqual(+ [p for p in problems if p.startswith("no DATA frames")],+ ["no DATA frames found for this id -- wrong id, or the run produced nothing"])+ _, _, problems = self.problems(self.transcript(1, emitted=2))+ self.assertEqual([p for p in problems if p.startswith("DONE emitted")],+ ["DONE emitted 2 != observed DATA 1"])+ _, _, problems = self.problems(self.transcript(1, done_prompt=2))+ self.assertEqual([p for p in problems if p.startswith("DONE prompt")],+ ["DONE prompt count 2 != ACCEPT 1"])++ def test_done_domains_and_fixed_grammar_bite_independently(self):+ cases = (+ {"tps": "-0.10"}, {"hit": "-0.1"}, {"hit": "100.1"},+ {"rss": "-0.01"}, {"tps": "0.1"}, {"hit": "0.00"},+ {"rss": "10.0"}, {"tps": "+0.10"}, {"tps": "01.00"},+ {"tps": "1e+00"}, {"tps": "nan"}, {"tps": "inf"},+ {"done_prompt": 0}, {"flag": 2},+ )+ for kwargs in cases:+ with self.subTest(kwargs=kwargs):+ _, _, problems = self.problems(self.transcript(1, **kwargs))+ matches = [p for p in problems if p.startswith("malformed DONE stats")]+ self.assertEqual(len(matches), 1, problems)++ _, _, problems = self.problems(self.transcript(+ 1, tps="-0.00", hit="-0.0", rss="-0.00"))+ self.assertEqual(problems, [])++ def test_noncanonical_ascii_numeric_grammar_never_passes(self):+ base = self.VALID+ cases = (+ base.replace(b"ACCEPT 7 1", b"ACCEPT 7 +1"),+ base.replace(b"ACCEPT 7 1", b"ACCEPT 7 1_0"),+ base.replace(b"ECHO 7 1 0", b"ECHO 7 1 +0"),+ base.replace(b"DATA 7 1", b"DATA 7 +1"),+ base.replace(b" -2.7000000000000002 2 ", b" -0_125 2 ", 1),+ base.replace(b" 1 -11.123455999999999", b" 01 -11.123455999999999"),+ base.replace(b"DATA 7 1 ", b"DATA 7 1 "),+ base.replace(b"DATA 7 1 ", b"DATA\t7 1 "),+ base.replace(b"1 -11.123455999999999\nx\n", b"1 -11.123455999999999 \nx\n"),+ base.replace(b"-2.7000000000000002 2", b"-1e-9999 2", 1),+ base.replace(b"-2.7000000000000002 2", b"-1.00000000000000000 2", 1),+ base.replace(b"-2.7000000000000002 2", b"-1e-9 2", 1),+ base.replace(b"-2.7000000000000002 2", b"-1e--09 2", 1),+ base.replace(b"DONE 7 STAT 1", b"DONE 7 STAT 01"),+ base.replace(b" 1 0\n", b" +1 0\n"),+ base.replace(b" 1 0\n", b" 1 00\n"),+ base.replace(b"DATA 7 1", "DATA 7 ١".encode("utf-8")),+ base.replace(b"DATA 7 1 -2.7000000000000002", b"DATA 7 1 -2.7000000000000002junk"),+ )+ for blob in cases:+ with self.subTest(blob=blob):+ _, _, problems = self.problems(blob)+ self.assertTrue(problems)+++class PreambleGateFailsLoudTests(_GapFixture):+ """The preamble gate never silently passes an unparsed BANNER/LOADED+ line: `parse_engine_preamble` raising `PreambleError`, or the module's+ own defensive `is None` branch, both surface as a named problem that+ quotes the offending line."""++ def test_unparsed_banner_is_a_named_failure_quoting_the_line(self):+ bad_banner = self.BANNER.replace(b"idot: neon-i8mm", b"idot: bogus", 1)+ kind, problems = GAPS._global_header(bad_banner.rstrip(b"\n"), 0)+ self.assertEqual(kind, [b"BANNER"])+ self.assertEqual(len(problems), 1)+ self.assertIn("malformed BANNER preamble", problems[0])+ self.assertIn(repr(bad_banner.rstrip(b"\n")), problems[0])++ def test_unparsed_loaded_is_a_named_failure_quoting_the_line(self):+ bad_loaded = self.LOADED.replace(b"MTP ACTIVE", b"MTP fabricated", 1)+ kind, problems = GAPS._global_header(bad_loaded.rstrip(b"\n"), 0)+ self.assertEqual(kind, [b"LOADED"])+ self.assertEqual(len(problems), 1)+ self.assertIn("malformed LOADED preamble", problems[0])+ self.assertIn(repr(bad_loaded.rstrip(b"\n")), problems[0])++ def test_preamble_returning_none_is_defensively_named_not_silently_passed(self):+ # `_global_header` treats a None `parse_engine_preamble` result the+ # same as a raised PreambleError: it is unreachable through the+ # real BANNER/LOADED prefixes (they always parse or raise), so this+ # pins the defensive branch directly by forcing that return value.+ original = GAPS.parse_engine_preamble+ GAPS.parse_engine_preamble = lambda text: None+ try:+ kind, problems = GAPS._global_header(+ self.BANNER.rstrip(b"\n"), 0)+ finally:+ GAPS.parse_engine_preamble = original+ self.assertEqual(kind, [b"BANNER"])+ self.assertEqual(len(problems), 1)+ self.assertIn("malformed BANNER preamble", problems[0])+ self.assertIn(repr(self.BANNER.rstrip(b"\n")), problems[0])++ def test_full_capture_with_unparsed_preamble_fails_the_whole_check(self):+ bogus = self.complete_capture().replace(+ b"idot: neon-i8mm", b"idot: bogus", 1)+ _, _, problems = self.problems(bogus)+ self.assertTrue(any("malformed BANNER preamble" in p for p in problems),+ problems)+++class DispatchPrefixCouplingTests(_GapFixture):+ """`_global_header` independently re-tests the same two dispatch+ prefixes (`"== GLM C engine"`, `"loaded in"`) that+ `engine_evidence.parse_engine_preamble` already dispatches on+ internally, instead of deriving them from the module -- see that+ module's own docstring on the "loaded index" collision it accepts as+ a documented characteristic because no engine anywhere in this tree+ currently emits a colliding line.++ The two copies are byte-identical today, but nothing else enforces+ that: a future edit to either literal alone would silently desync+ "is this line an owned-preamble candidate" between this checker and+ the module it is supposed to defer to, and this tool hard-aborts the+ whole check on anything it decides is a malformed candidate.++ This test pins the coupling from the consumer side: it extracts the+ prefixes from `_global_header`'s LIVE source (never retyped here) and+ asserts the module still treats a genuine, fully well-formed line+ built from each one as an owned-preamble candidate -- a non-None+ result of the CORRECT kind, not merely "raised or didn't return+ None". An earlier version of this test fed the bare prefix alone (an+ incomplete line no real grammar accepts) and accepted a raised+ `PreambleError` as proof of recognition; that is one-directional --+ a module whose dispatch is gutted to unconditionally raise+ `PreambleError` (or unconditionally return some non-None value) for+ EVERY input, recognizing nothing about either prefix specifically,+ would still pass it. Confirmed by mutation: deleting the module's own+ prefix dispatch outright left that version green (see the commit+ history / worker report for the failing-then-fixed proof). Using a+ genuine, complete, well-formed line and requiring the CORRECT typed+ result closes that gap in both directions: a module that stops+ recognizing the checker's literal returns `None` or the wrong `kind`+ and this fails; a module that recognizes something it should not+ would show up in the "vice versa" direction, which is out of scope+ for a consumer-side pin (the module owns its own grammar).+ `engine_evidence.py`/`test_engine_evidence.py` are not modified by+ this fix -- it is entirely a P3-local test change. Contrast+ `engine_evidence.py`'s own `IDOT_KERNELS` test, where a frozen+ literal duplicate of the roster IS the mechanism because its whole+ job is to catch drift in the module it pins; here the duplication+ was incidental until this test made it intentional."""++ @staticmethod+ def _extract_dispatch_prefixes():+ # Match ONLY the leading dispatch condition+ # (`if (line.startswith(b"...") or line.startswith(b"...")):`),+ # not the later `kind = ... if line.startswith(b"==") ...` line+ # that merely disambiguates a label within that branch.+ source = inspect.getsource(GAPS._global_header)+ match = re.search(+ r'if\s*\(\s*line\.startswith\(b"([^"]*)"\)\s*or\s*'+ r'line\.startswith\(b"([^"]*)"\)\s*\)\s*:',+ source)+ assert match is not None, (+ f"could not find _global_header's leading dispatch "+ f"`if (line.startswith(b\"...\") or line.startswith(b\"...\"))`"+ f" condition -- update this test's extraction to match its "+ f"current shape before it can keep pinning the coupling:\n"+ f"{source}")+ return match.groups()++ def test_checker_dispatch_prefixes_stay_recognized_by_the_module(self):+ banner_prefix, loaded_prefix = self._extract_dispatch_prefixes()+ banner_line = self.BANNER.rstrip(b"\n").decode("ascii")+ loaded_line = self.LOADED.rstrip(b"\n").decode("ascii")++ # Sanity: the fixture's own known-good lines must actually start+ # with the literals just extracted, or the pairing below (banner+ # literal <-> BANNER fixture, loaded literal <-> LOADED fixture)+ # would be testing the wrong thing.+ self.assertTrue(banner_line.startswith(banner_prefix),+ (banner_prefix, banner_line))+ self.assertTrue(loaded_line.startswith(loaded_prefix),+ (loaded_prefix, loaded_line))++ banner_result = GAPS.parse_engine_preamble(banner_line)+ self.assertIsNotNone(+ banner_result,+ f"engine_evidence.parse_engine_preamble no longer recognizes "+ f"a genuine BANNER line built from _global_header's own "+ f"{banner_prefix!r} literal (returned None) -- the two "+ f"independently maintained copies of this prefix have "+ f"diverged")+ self.assertEqual(banner_result["kind"], "BANNER")++ loaded_result = GAPS.parse_engine_preamble(loaded_line)+ self.assertIsNotNone(+ loaded_result,+ f"engine_evidence.parse_engine_preamble no longer recognizes "+ f"a genuine LOADED line built from _global_header's own "+ f"{loaded_prefix!r} literal (returned None) -- the two "+ f"independently maintained copies of this prefix have "+ f"diverged")+ self.assertEqual(loaded_result["kind"], "LOADED")+++class LegacyDataFrameGapTests(_GapFixture):+ """A legacy 3-field DATA frame anywhere in the generation is the exact+ gap this checker exists to catch, and it must fail loudly even when+ it is not the last generated token."""++ def test_legacy_three_field_data_frame_mid_generation_fails(self):+ blob = (+ b"ACCEPT 7 1\n"+ b"ECHO 7 1 0 nan 0\nx\n"+ b"DATA 7 1 -2.7000000000000002 2 0 -2.7000000000000002 1 -11.123455999999999\nx\n" # token 1: full channel+ b"DATA 7 1\nx\n" # token 2: legacy gap+ b"DATA 7 1 -2.7000000000000002 2 0 -2.7000000000000002 1 -11.123455999999999\nx\n" # token 3: full channel+ b"DONE 7 STAT 3 0.10 100.0 10.00 1 0\n"+ )+ data, echo, problems = self.problems(blob)+ self.assertEqual(data, 3)+ self.assertTrue(any(+ "GAP: legacy 3-field DATA frame #2" in p for p in problems),+ problems)++ def test_wholly_dropped_data_frame_mid_generation_fails_via_done_count(self):+ # The severer form of the same defect class: one generated token's+ # DATA frame never reached stdout at all (not even degraded to 3+ # fields). Only the DONE-emitted-vs-observed-DATA cross-check can+ # name this -- a checker with no concept of a DONE frame would+ # pass it silently.+ blob = (+ b"ACCEPT 7 1\n"+ b"ECHO 7 1 0 nan 0\nx\n"+ b"DATA 7 1 -2.7000000000000002 2 0 -2.7000000000000002 1 -11.123455999999999\nx\n" # token 1+ b"DATA 7 1 -2.7000000000000002 2 0 -2.7000000000000002 1 -11.123455999999999\nx\n" # token 3 (token 2 missing)+ b"DONE 7 STAT 3 0.10 100.0 10.00 1 0\n"+ )+ data, echo, problems = self.problems(blob)+ self.assertEqual(data, 2)+ self.assertIn("DONE emitted 3 != observed DATA 2", problems)+++class TopkCapAndTokenIdBoundsTests(_GapFixture):+ """The numeric tail enforces k == min(--topk, --vocab) and+ 0 <= tid < vocab, reporting the first offending frame."""++ def test_k_above_expected_but_within_wire_cap_never_passes(self):+ # vocab=40, --topk=20 => expected_k = min(20, 40) = 20; the frame+ # advertises k=25, which is <=32 (the wire's own hard cap) but+ # above the expected value for this request.+ pairs = " ".join(f"{i} -2.7000000000000002" for i in range(25))+ blob = (+ b"ACCEPT 7 1\n" + self.echo(0) ++ f"DATA 7 1 -2.7000000000000002 25 {pairs}\n".encode("ascii") + b"x\n" ++ b"DONE 7 STAT 1 0.10 100.0 10.00 1 0\n")+ _, _, problems = self.problems(blob, topk=20, vocab=40)+ self.assertTrue(any(+ "DATA top-k 25 != expected 20" in p for p in problems), problems)++ def test_k_above_wire_hard_cap_of_32_never_passes(self):+ blob = (+ b"ACCEPT 7 1\n" + self.echo(0) ++ b"DATA 7 1 -2.7000000000000002 33 0 -0.1\nx\n" ++ b"DONE 7 STAT 1 0.10 100.0 10.00 1 0\n")+ _, _, problems = self.problems(blob, topk=32, vocab=40)+ self.assertTrue(any(+ "malformed DATA numeric fields" in p for p in problems), problems)++ def test_token_id_equal_to_vocab_never_passes(self):+ blob = (+ b"ACCEPT 7 1\n" + self.echo(0) ++ b"DATA 7 1 -2.7000000000000002 2 0 -2.7000000000000002 3 -0.25\nx\n" ++ b"DONE 7 STAT 1 0.10 100.0 10.00 1 0\n")+ _, _, problems = self.problems(blob, topk=2, vocab=3)+ self.assertTrue(any(+ "token id 3 outside [0,3)" in p for p in problems), problems)++ def test_token_id_well_above_vocab_never_passes(self):+ blob = (+ b"ACCEPT 7 1\n" + self.echo(0) ++ b"DATA 7 1 -2.7000000000000002 1 999 -2.7000000000000002\nx\n" ++ b"DONE 7 STAT 1 0.10 100.0 10.00 1 0\n")+ _, _, problems = self.problems(blob, topk=1, vocab=40)+ self.assertTrue(any(+ "token id 999 outside [0,40)" in p for p in problems), problems)+++class FrameOrderLifecycleGateTests(_GapFixture):+ """`check()`'s full-process startup order gate (the `required` tuple+ binding BANNER/LOADED/READY/STAT/HWINFO/TIERS/EMAP to frames 0..6) is+ exercised in every existing full-process test, but always alongside+ several other simultaneous defects or via a bare `assertTrue(problems)`+ -- so no existing test's pass/fail status depends on any ONE of the+ seven `required` entries individually. Ablating any single entry from+ `required` (deleting its `(kind, index)` pair) left the pre-fix-round+ 105-test suite green: the same vacuous-gate shape as `IDOT_KERNELS`+ (a gate whose expectation and its only check come from the same,+ un-anchored place).++ Fix shape, matching that precedent: an independent anchor (a frozen+ copy of the expected order, written here rather than derived from+ `check()`'s own construction, checked against what `check()` actually+ contains) plus per-ordinal coverage (one test per entry, each proving+ that ENTRY's own specific "expected ... as frame N" message -- unique+ text per kind, so only that entry's check can produce it -- actually+ fires). `c/colibri.c`'s own emission order is NOT consulted here: that+ cross-check (transcribed wire format vs C source) is a separate,+ cross-cutting, pre-existing gap (flagged elsewhere) affecting every+ literal in both new Python modules, not something to fix piecemeal in+ this one gate.+ """++ # Independent anchor: written by hand, not copied from `check()`'s own+ # construction of `required`. `test_required_order_matches_the_frozen_anchor`+ # cross-checks this against the live tuple `check()` actually uses, so a+ # future reorder/rename in the code is caught here even before any+ # per-ordinal test runs.+ EXPECTED_ORDER = (+ (b"BANNER", 0), (b"LOADED", 1), (b"READY", 2), (b"STAT", 3),+ (b"HWINFO", 4), (b"TIERS", 5), (b"EMAP", 6),+ )++ @staticmethod+ def _extract_required_tuple():+ """Parse the literal `required = (...)` tuple out of `check()`'s+ live source by counting parens to the matching close -- not a+ regex snippet, so it does not care about internal formatting --+ then `ast.literal_eval` it. This is reading the code's actual+ current behavior, not retyping an assumption about it."""+ source = inspect.getsource(GAPS.check)+ marker = "required = ("+ start = source.index(marker)+ paren_start = start + len("required = ")+ depth = 0+ i = paren_start+ while True:+ if source[i] == "(":+ depth += 1+ elif source[i] == ")":+ depth -= 1+ if depth == 0:+ end = i + 1+ break+ i += 1+ return ast.literal_eval(source[paren_start:end])++ def test_required_order_matches_the_frozen_anchor(self):+ self.assertEqual(self._extract_required_tuple(), self.EXPECTED_ORDER)++ def _assert_drop_bites(self, drop_line, kind, expected_index, template):+ blob = self.complete_capture().replace(drop_line, b"", 1)+ _, _, problems = self.problems(blob)+ needle = f"expected {template} {kind} as frame {expected_index}"+ self.assertTrue(any(needle in p for p in problems),+ f"{needle!r} not found in {problems!r}")++ def test_dropping_banner_breaks_its_own_ordinal_check(self):+ self._assert_drop_bites(self.BANNER, "BANNER", 0, "one")++ def test_dropping_loaded_breaks_its_own_ordinal_check(self):+ self._assert_drop_bites(self.LOADED, "LOADED", 1, "one")++ def test_dropping_ready_breaks_its_own_ordinal_check(self):+ self._assert_drop_bites(b"\x01\x01READY\x01\x01\n", "READY", 2, "one")++ def test_dropping_stat_breaks_its_own_ordinal_check(self):+ self._assert_drop_bites(b"STAT 0 0.00 0.0 10.00\n", "STAT", 3, "one")++ def test_dropping_hwinfo_breaks_its_own_ordinal_check(self):+ self._assert_drop_bites(+ b"HWINFO 16 128.0 64.0 2 48.0 AMD Ryzen 9|CUDA device x2\n",+ "HWINFO", 4, "startup")++ def test_dropping_tiers_breaks_its_own_ordinal_check(self):+ self._assert_drop_bites(+ b"TIERS 128 256 1024 48.00 32.50\n", "TIERS", 5, "startup")++ def test_dropping_emap_breaks_its_own_ordinal_check(self):+ self._assert_drop_bites(b"EMAP 2 2 00010203\n", "EMAP", 6, "startup")+++class CaptureModeLiteralTests(unittest.TestCase):+ """`capture_mode` names the four explicit transcript shapes."""++ def test_empty_transcript_is_request_only(self):+ self.assertEqual(GAPS.capture_mode([]), "request-only")++ def test_accept_first_frame_is_request_only(self):+ frames, _ = GAPS.parse_frames(b"ACCEPT 7 1\n")+ self.assertEqual(GAPS.capture_mode(frames), "request-only")++ def test_banner_first_frame_is_full_process(self):+ frames, _ = GAPS.parse_frames(+ b"== GLM C engine (glm_moe_dsa), cache=64 experts/layer | "+ b"compute experts@4-bit dense@8-bit | idot: neon-i8mm ==\n"+ b"ACCEPT 7 1\n")+ self.assertEqual(GAPS.capture_mode(frames), "full-process")++ def test_ready_first_frame_is_ready_suffix(self):+ frames, _ = GAPS.parse_frames(+ b"\x01\x01READY\x01\x01\nACCEPT 7 1\n")+ self.assertEqual(GAPS.capture_mode(frames), "ready-suffix")++ def test_global_after_a_non_global_first_frame_is_invalid(self):+ frames, _ = GAPS.parse_frames(+ b"ACCEPT 7 1\n\x01\x01READY\x01\x01\n")+ self.assertEqual(GAPS.capture_mode(frames), "invalid")+++class FixedMetricAndUintBoundaryTests(unittest.TestCase):+ """`_fixed_metric`'s lower/upper bounds and `_uint`'s maximum+ argument are literal, inclusive boundaries."""++ def test_fixed_metric_lower_bound_is_inclusive(self):+ self.assertEqual(GAPS._fixed_metric(b"0.0", 1, "x", lower=0.0), 0.0)+ with self.assertRaises(ValueError):+ GAPS._fixed_metric(b"-0.1", 1, "x", lower=0.0)++ def test_fixed_metric_upper_bound_is_inclusive(self):+ self.assertEqual(+ GAPS._fixed_metric(b"100.0", 1, "x", upper=100.0), 100.0)+ with self.assertRaises(ValueError):+ GAPS._fixed_metric(b"100.1", 1, "x", upper=100.0)++ def test_fixed_metric_place_count_is_exact(self):+ with self.assertRaises(ValueError):+ GAPS._fixed_metric(b"1.00", 1, "x")+ with self.assertRaises(ValueError):+ GAPS._fixed_metric(b"1.0", 2, "x")++ def test_uint_maximum_is_inclusive(self):+ self.assertEqual(GAPS._uint(b"32", "x", 32), 32)+ with self.assertRaises(ValueError):+ GAPS._uint(b"33", "x", 32)+++++class NumericGrammarFormTests(_GapFixture):+ """The numeric grammar accepts both the fixed %.6f form dev's engine+ prints today and the earlier %.17g form+ (plus an exact nan/inf/-inf spelling), pinned with non-dyadic literal+ values (-0.3, -2.7, -11.123456) so the two encodings are actually+ distinguishable by their token spelling and not merely by the float+ each happens to parse to."""++ def test_fixed6_form_is_accepted_and_reported(self):+ blob = (+ b"ACCEPT 7 1\n" + self.echo(0) ++ b"DATA 7 1 -0.300000 2 0 -2.700000 1 -11.123456\nx\n" ++ b"DONE 7 STAT 1 0.10 100.0 10.00 1 0\n")+ data, echo, problems, mode, forms = self.full_problems(blob)+ self.assertEqual(problems, [])+ self.assertEqual(forms, collections.Counter({"fixed6": 3}))++ def test_c17g_form_is_accepted_and_reported(self):+ blob = (+ b"ACCEPT 7 1\n" + self.echo(0) ++ b"DATA 7 1 -2.7000000000000002 2 0 -11.123455999999999 "+ b"1 -0.29999999999999999\nx\n" ++ b"DONE 7 STAT 1 0.10 100.0 10.00 1 0\n")+ data, echo, problems, mode, forms = self.full_problems(blob)+ self.assertEqual(problems, [])+ self.assertEqual(forms, collections.Counter({"c17g": 3}))++ def test_a_token_matching_both_grammars_is_ambiguous_and_still_accepted(self):+ # '%.17g' % -1.234567 == '-1.234567', which is ALSO the exact+ # six-decimal spelling of that same double: there is no way to+ # tell, from the token alone, which engine format produced it.+ # An earlier version of this check treated a tail containing both+ # an unambiguous fixed6 token and an unambiguous c17g token as a+ # "mixed forms" error; that rule was removed because per-token+ # classification is inherently ambiguous and the rule produced+ # false rejections on real %.17g transcripts (some of whose+ # tokens are, coincidentally, exact six-decimal spellings too).+ # Nothing about mixing forms is rejected anymore -- only a token+ # matching NEITHER grammar is.+ blob = (+ b"ACCEPT 7 1\n" + self.echo(0) ++ b"DATA 7 1 -1.234567 2 0 -2.7000000000000002 1 -0.300000\nx\n" ++ b"DONE 7 STAT 1 0.10 100.0 10.00 1 0\n")+ data, echo, problems, mode, forms = self.full_problems(blob)+ self.assertEqual(problems, [])+ self.assertEqual(forms, collections.Counter(+ {"ambiguous": 1, "c17g": 1, "fixed6": 1}))++ def test_special_nan_inf_tokens_are_syntactically_valid_then_flagged(self):+ # "nan"/"inf"/"-inf" are exact libc renderings either format's+ # snprintf can emit for a non-finite double: syntactically valid+ # under both grammars, but still fail the finite/non-positive+ # semantic check that applies regardless of which form carried it.+ for special in (b"nan", b"inf", b"-inf"):+ with self.subTest(special=special):+ blob = (+ b"ACCEPT 7 1\n" + self.echo(0) ++ b"DATA 7 1 " + special + b" 1 0 -0.300000\nx\n" ++ b"DONE 7 STAT 1 0.10 100.0 10.00 1 0\n")+ _, _, problems, _, forms = self.full_problems(blob, topk=1)+ self.assertTrue(any(+ "target logprob is not finite/non-positive" in p+ for p in problems), problems)+ self.assertIn("special", forms)++ def test_token_matching_neither_form_never_passes(self):+ blob = (+ b"ACCEPT 7 1\n" + self.echo(0) ++ b"DATA 7 1 -0.3 2 0 -2.7 1 -11.123456\nx\n" ++ b"DONE 7 STAT 1 0.10 100.0 10.00 1 0\n")+ _, _, problems = self.problems(blob)+ self.assertTrue(any(+ "malformed DATA numeric fields" in p for p in problems), problems)+++class ByteOffsetTests(unittest.TestCase):+ """Every reported problem cites the byte offset of+ the OFFENDING frame's own header line, not the frame that follows it,+ and numeric-tail problems (previously offset-less) now carry one too.+ """++ def test_malformed_global_offset_cites_its_own_line_not_the_next_one(self):+ first = b"\x01\x01READY\x01\x01\n"+ bad_stat = b"STAT 0 0.0 0.0 10.00\n" # "0.0" should be "0.00"+ blob = first + bad_stat + b"ACCEPT 7 1\n"+ _, problems = GAPS.parse_frames(blob)+ self.assertEqual(len(problems), 1)+ self.assertIn(f"byte {len(first)}", problems[0])+ self.assertNotIn(f"byte {len(first) + len(bad_stat)}", problems[0])++ def test_header_fields_offset_cites_its_own_line_not_the_next_one(self):+ first = b"ACCEPT 7 1\n"+ bad = b" ACCEPT 8 1\n" # leading space: noncanonical header+ blob = first + bad+ _, problems = GAPS.parse_frames(blob)+ self.assertEqual(len(problems), 1)+ self.assertIn(f"byte {len(first)}", problems[0])+ self.assertNotIn(f"byte {len(first) + len(bad)}", problems[0])++ def test_numeric_tail_problem_cites_the_data_frames_own_header(self):+ prefix = b"ACCEPT 7 1\n" + b"ECHO 7 1 0 nan 0\nx\n"+ # positive target lp: a semantic failure inside _numeric_tail.+ bad_data = b"DATA 7 1 0.300000 2 0 -0.300000 1 -0.300000\n"+ blob = prefix + bad_data + b"x\n" + b"DONE 7 STAT 1 0.10 100.0 10.00 1 0\n"+ frames, framing = GAPS.parse_frames(blob)+ _, _, problems, _, _ = GAPS.check(frames, framing, "7", 2, 3)+ offset = len(prefix)+ self.assertTrue(any(+ f"byte {offset}" in p and+ "target logprob is not finite/non-positive" in p+ for p in problems), problems)++ def test_offset_of_a_non_first_offending_frame_is_hand_verified(self):+ # Three frames precede the offending one: ACCEPT (11 bytes: the+ # 10-character line "ACCEPT 7 1" plus its newline), an ECHO at+ # position 0 (17 bytes: "ECHO 7 1 0 nan 0" plus its newline), and+ # that ECHO's one-byte "x" payload plus its terminator (2 bytes).+ # 11 + 17 + 2 = 30 is where the malformed DONE header starts --+ # a hand count, not a value read back from the module under test.+ line0 = b"ACCEPT 7 1\n"+ line1 = b"ECHO 7 1 0 nan 0\n"+ payload1 = b"x\n"+ self.assertEqual(len(line0), 11)+ self.assertEqual(len(line1), 17)+ self.assertEqual(len(payload1), 2)+ bad_done = b"DONE 7 STAT 1 0.10 100.0 10.00 1\n" # missing 1 field+ blob = line0 + line1 + payload1 + bad_done+ frames, framing = GAPS.parse_frames(blob)+ self.assertEqual(len(frames), 3)+ self.assertEqual(frames[2][2], 30)+ _, _, problems, _, _ = GAPS.check(frames, framing, "7", 2, 3)+ self.assertTrue(any(+ "malformed DONE frame at byte 30" in p for p in problems), problems)+++class PreambleGateWiredIntoMainTests(unittest.TestCase):+ """capture_mode() is actually wired into check(), so+ a transcript opening with an unrecognized (garbage) line fails through+ main()'s own CLI path, not only via a function called directly; and+ the resolved capture mode is always reported, never silent."""++ def test_garbage_preamble_fails_via_main_cli_path(self):+ blob = (+ b"GARBAGE NOT A REAL PREAMBLE\n"+ b"ACCEPT 7 1\n"+ b"ECHO 7 1 0 nan 0\nx\n"+ b"DATA 7 1 -0.300000 2 0 -0.300000 1 -0.300000\nx\n"+ b"DONE 7 STAT 1 0.10 100.0 10.00 1 0\n")+ with tempfile.TemporaryDirectory() as tmp:+ capture = pathlib.Path(tmp) / "capture.raw"+ capture.write_bytes(blob)+ proc = subprocess.run(+ [sys.executable, str(pathlib.Path(GAPS.__file__)), str(capture),+ "--id", "7", "--topk", "2", "--vocab", "3"],+ stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=False)+ self.assertEqual(proc.returncode, 1)+ self.assertIn(b"unrecognized frame opens the transcript", proc.stdout)++ def test_capture_mode_and_numeric_form_are_reported_in_main_summary(self):+ with tempfile.TemporaryDirectory() as tmp:+ capture = pathlib.Path(tmp) / "capture.raw"+ capture.write_bytes(_GapFixture.VALID)+ proc = subprocess.run(+ [sys.executable, str(pathlib.Path(GAPS.__file__)), str(capture),+ "--id", "7", "--topk", "2", "--vocab", "3"],+ stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=False)+ self.assertEqual(proc.returncode, 0)+ self.assertIn(b"capture_mode=request-only", proc.stdout)+ self.assertIn(b"numeric form tally: c17g=3", proc.stdout)++ def test_invalid_capture_mode_is_named_via_check(self):+ # A stray global record with no BANNER/READY lead frame at all.+ blob = b"STAT 0 0.00 0.0 10.00\nACCEPT 7 1\n"+ frames, framing = GAPS.parse_frames(blob)+ self.assertEqual(GAPS.capture_mode(frames), "invalid")+ _, _, problems, mode, _ = GAPS.check(frames, framing, "7", 2, 3)+ self.assertEqual(mode, "invalid")+ self.assertTrue(any("invalid capture mode" in p for p in problems), problems)++++class EngineFormatCorpusTests(_GapFixture):+ """A synthesized corpus in the engine's real wire format: every+ numeric token below is spelled exactly as C's `snprintf(..., "%.6f+ %d", ...)` (dev's shipped `logprob_tail()`) would spell it -- not+ hand-picked round numbers, but 210 distinct, non-dyadic values, so no+ single lucky literal is doing the proving. It must pass at head, and+ every one of its numeric tokens is independently confirmed to be+ something the strict %.17g-only grammar this checker's numeric+ parsing predates would have rejected outright.+ """++ N_FRAMES = 210+ VOCAB = 300+ TOPK = 2++ @classmethod+ def _corpus(cls, seed=20260904):+ rng = random.Random(seed)+ parts = [b"ACCEPT 7 1\n", b"ECHO 7 1 0 nan 0\nx\n"]+ tokens = []+ for _ in range(cls.N_FRAMES):+ target_lp = -abs(rng.uniform(1e-6, 20.0))+ target_token = f"{target_lp:.6f}".encode("ascii")+ tokens.append(target_token)+ pair_ids = rng.sample(range(cls.VOCAB), cls.TOPK)+ pieces = [target_token, str(cls.TOPK).encode("ascii")]+ for tid in pair_ids:+ token_lp = -abs(rng.uniform(1e-6, 20.0))+ token_lp_token = f"{token_lp:.6f}".encode("ascii")+ tokens.append(token_lp_token)+ pieces.append(str(tid).encode("ascii"))+ pieces.append(token_lp_token)+ parts.append(b"DATA 7 1 " + b" ".join(pieces) + b"\nx\n")+ parts.append(+ f"DONE 7 STAT {cls.N_FRAMES} 0.10 100.0 10.00 1 0\n".encode("ascii"))+ return b"".join(parts), tokens++ def test_engine_format_corpus_of_210_frames_passes(self):+ blob, tokens = self._corpus()+ self.assertGreaterEqual(len(tokens), self.N_FRAMES)+ frames, framing = GAPS.parse_frames(blob)+ data, echo, problems, mode, forms = GAPS.check(+ frames, framing, "7", self.TOPK, self.VOCAB)+ self.assertEqual(problems, [])+ self.assertEqual(data, self.N_FRAMES)+ # Every token here is a genuine %.6f emission, so none can be+ # classified as c17g-only; some nonetheless land on an exact+ # %.17g spelling too (the false-positive "mixed forms" case a+ # since-removed rejection used to misfire on) and are tallied+ # "ambiguous" rather than "fixed6" -- informational only, and+ # this test's PASS/problems assertions above already prove that+ # tally has no bearing on the verdict.+ self.assertEqual(forms["c17g"], 0)+ self.assertGreater(forms["ambiguous"], 0)+ self.assertEqual(sum(forms.values()), len(tokens))++ def test_engine_format_corpus_values_are_non_dyadic(self):+ # A dyadic value (an exact binary fraction, e.g. 0.125) would make+ # the fixed6/c17g distinction uninteresting for that token, since+ # both forms could spell it exactly. Confirm the corpus avoids+ # that by construction: none of its %.6f tokens round-trips+ # through Python's own dyadic-fraction check.+ _, tokens = self._corpus()+ dyadic = 0+ for token in tokens:+ value = float(token)+ # A double is dyadic (exactly binary-fraction-representable at+ # 6 decimal places) iff multiplying by 1e6 and rounding loses+ # nothing AND the resulting numerator's lowest set bits divide+ # evenly -- simpler and just as decisive here: a dyadic value+ # would print identically under %.17g and %.6f once trailing+ # zeros are accounted for, which none of these do (see the+ # next test) -- this test instead confirms none is a "clean"+ # few-bits-of-mantissa value like *.0, *.5, *.25, *.125.+ frac = abs(value) - int(abs(value))+ eighths = frac * 8+ if abs(eighths - round(eighths)) < 1e-9:+ dyadic += 1+ self.assertEqual(dyadic, 0)++ def test_engine_format_corpus_would_fail_a_c17g_only_grammar(self):+ # Every token in the corpus is a %.6f spelling. Whether any ONE+ # such token also happens to be its own double's shortest %.17g+ # round-trip spelling is unpredictable per-token (it depends on+ # that double's neighborhood, not on the fact that it came from+ # %.6f) -- but the module docstring's actual claim is about the+ # TRANSCRIPT, not any single token: one rejected token anywhere+ # is enough to fail the whole check, since `_numeric_tail` bails+ # out of the entire frame the moment its numeric parse raises.+ # Confirm most tokens are rejected outright by the standalone+ # %.17g parser this module still carries (`_c17g`, kept for the+ # earlier engine's own emitted form), and that at least one+ # rejection lands inside a real DATA frame of the corpus --+ # which is what actually dooms the whole transcript under a+ # %.17g-only grammar.+ blob, tokens = self._corpus()+ rejected = 0+ for token in tokens:+ try:+ GAPS._c17g(token, "corpus token")+ except ValueError:+ rejected += 1+ self.assertGreater(rejected, len(tokens) // 2, (rejected, len(tokens)))++ frame_rejected = False+ for line in blob.split(b"\n"):+ if not line.startswith(b"DATA "):+ continue+ fields = line.split(b" ")+ for field in fields[3:]:+ try:+ GAPS._c17g(field, "corpus token")+ except ValueError:+ frame_rejected = True+ break+ if frame_rejected:+ break+ self.assertTrue(+ frame_rejected,+ "expected at least one DATA frame in the corpus to contain a "+ "token the %.17g-only parser refuses")++if __name__ == "__main__":+ unittest.main()