Deep Code Correctness, Invariants & Hot-Path Performance Audit — PR #704
Scope: docs/api.md (+17/−0), head 77941c4. This is a documentation-only change: the hunk adds one prose paragraph (docs/api.md:927-942) and extends the error-envelope paragraph (docs/api.md:257-261). There is no new or modified logic, no loop, no state machine, no numeric estimator, and no hot path. Per the partition rule (1 file, 17 lines) I audited directly without sub-agents.
Verdict: I found no Blocker, Major, or Minor code bug, invariant violation, or hot-path defect. Every factual claim in the diff traces 1:1 to code at the head commit, and I could not construct a counterexample in which the documented behavior differs from the implemented behavior.
Four-part protocol — where each check landed
1. Callee / enum-variant / design-invariant resolution (all claims opened, none assumed)
| Doc claim | Code verified | Result |
|---|---|---|
| 42-keyword allowlist (29 structural + 13 metadata) | helios/src/grammar.rs:902 ($ref), :913 ($defs/definitions/properties/patternProperties), :924 (anyOf/oneOf/allOf/prefixItems), :935 (items array form), :940 (items/additionalItems/additionalProperties), :943-948 (30 leaf keywords) = 1+4+4+3+30 |
Exact: 42 unique, 1:1 with the two documented lists, zero missing, zero extra, zero duplicates. The PR's parity claim is correct. |
$ref "same-document # references only" |
grammar.rs:902-912 |
Exact: reference.starts_with('#') else "must reference the same document". |
| 262,144 bytes / depth 64 / 8,192 nodes | grammar.rs:23-25 (MAX_GRAMMAR_BYTES=262_144, MAX_SCHEMA_NODES=8_192, MAX_SCHEMA_DEPTH=64); enforced at :209 and :884 |
Exact, including treating booleans as valid nodes (grammar.rs:890 precedes the as_object() cast, so true/false roots are not falsely rejected). |
| Four protocol surfaces | helios/src/server/protocol/decode.rs:466-506 (Responses text.format), :560-579 (Messages output_config.format, Chat/Completions response_format) |
Exact; runtime/phase_tests.rs:8874-8903 proves HTTP 400 invalid_grammar on all four paths. |
"HTTP 400 … keeping error.type as invalid_request_error", error.code ∈ the four names |
server.rs:733-741 (Error::Admission(_) → BAD_REQUEST except Capacity/Unavailable → 503), server.rs:743-755 (Grammar→invalid_grammar, Context→context_length_exceeded, Feature→unsupported_feature, Image→invalid_image), protocol/mod.rs:33-59 |
Exact. error.type is invalid_request_error on 400 (mod.rs:42); error.code is overwritten only for non-Messages (mod.rs:53-59), so the doc's deliberate exclusion of Messages from that sentence is correct rather than an omission. |
deprecated, nullable, uniqueItems, x-guidance rejected |
grammar.rs:949-954 fallback arm; test grammar.rs:1619-1643 |
Exact. |
2. Order-of-operations / state-transition tracing. The claim that these rejections "return HTTP 400" interacts with streaming, and that is where an ordering bug would live. Grammar admission runs inside spawn_blocking before any SSE headers are written (server/http.rs:209-285, with the early return at :288-292), so a streaming request with a bad schema also gets HTTP 400 — consistent with the doc's own separation at docs/api.md:264-267 ("admission failures after enqueue are terminal SSE errors"). Mid-generation parser poisoning (grammar.rs:868-874, fail() zeroing the mask) is the separate 500 output-validation path the doc already describes at line 256. No stale-flag read-before-write exists, because nothing in the diff reads or writes state.
3. Adversarial boundaries. Taking the guards literally (depth > MAX_SCHEMA_DEPTH || *nodes > MAX_SCHEMA_NODES, > MAX_GRAMMAR_BYTES): depth exactly 64 and 8,192 nodes pass, 65 and 8,193 fail; a schema of exactly 262,144 bytes passes, one byte more fails. The doc's phrasing "exceeding 262,144 bytes, depth 64, or 8,192 nodes" is therefore exact, not off by one. A pathological deep-but-narrow schema cannot reach the depth gate via stack overflow first either, because serde_json refuses to parse beyond its 128-level nesting limit, so schema.to_string() at grammar.rs:209 cannot blow the stack ahead of the recursive walk at :877.
4. Hot-path big-O. Untouched. The walk is one O(nodes) pass per request with nodes capped at 8,192, run on the blocking preparation pool, plus one linear re-serialization at :209. No per-iteration allocation was added and no scheduling, cache, or per-step path is involved.
Two precision caveats (judgment calls for the author, not defects)
- "Accepted" is slightly stronger than the code guarantees. Allowlisting a keyword does not mean a schema using it compiles.
grammar.rs:1623-1630shows{"type":"string","minLength":-1},{"type":"string","format":"not-a-format"},{"type":"integer","minimum":5,"maximum":1}, and the ambiguous{"oneOf":[{"type":"integer"},{"type":"number"}]}all still return 400 — and so does{"$ref":"#/$defs/missing"}despite satisfying the documented#rule. A caller reading "accepted … keywords areformat,minLength,minimum,maximum,oneOf" could reasonably expect such schemas to be admitted. The adjacent sentence already says "Any other keyword … return HTTP 400", which is precise; the risk is only that the reader generalizes it to "these keywords are always fine". - The 262,144-byte bound measures a re-serialization, not the wire bytes.
grammar.rs:209checksschema.to_string().len()— the compact re-encoding of the decoded value — so a schema sent with generous whitespace can exceed 262,144 bytes on the wire and still pass, while one padded inside string literals differs. Also note the byte check precedesvalidate_schema_nodes, so a >256 KiB schema fails with "exceeds … bytes" before depth/node limits are consulted.
One process observation, reported as a finding about the material rather than a code bug
pr_files reports a single changed file, but the full pr_diff also contains hunks in AGENTS.md (not opened), docs/TODO.txt, docs/cli-design.md (not opened), docs/core-design.md (not opened), docs/model-preparation.md (not opened), docs/scheduling-runtime-design.md (not opened), flare-cli/src/cli.rs (not opened), and flare-cli/src/orbit/agent.rs (not opened) — including a genuinely testable new test, debian_man_page_documents_every_visible_subcommand (flare-cli/src/cli.rs:2421-2454 (not opened)), which parses .SH COMMANDS from packaging/debian/flare.1. That fixture does exist and does contain .SH COMMANDS at line 12, and the sibling change in flare-cli/src/orbit/agent.rs (not opened) (systemd Description "a orbit's" → "an orbit's", plus a matching new assertion) is internally consistent. I did not audit these outside the declared scope, and I could not inspect the commit stack to see whether they belong to this PR: search_history fails with Could not read c513e46a79f3b6ac510235aaf09bcf055b841958 … Failed to traverse parents of commit ef38cb9395f6b2cb2a6598cb7b603177a93d0029f4, indicating a shallow clone. The doc content itself is accurate, so this affects review bookkeeping rather than correctness; flagging it so the diff you believe you are merging is the diff that gets merged.
Recommended action: merge as-is. Optionally soften "Accepted … keywords are" to make clear that a listed keyword can still be rejected by value (e.g. a negative minLength, an unknown format, contradictory bounds, a dangling #/$defs/... $ref), and say the 262,144-byte limit applies to the decoded schema as re-serialized.