interplane.Get Omega
Read in your language

synapse

A component of the AienOS ecosystem.

Brought by drakestapleton

branch main Branches Tags History

synapse / docs / G1_WIRE_FRAMES.md

145 lines · 16020 bytes Raw Blame History Source

G1: Binary Wire Frames and Have-Want Negotiation

ADR 0003 section 9 step G1 (issue aien-spark/synapse#44), section 8 crate synapse-wire: binary frames with a fuzzed decoder, have-want chunk negotiation, and stream transports. Needs A1 (binary codec) and F1 (capability chains), both merged.

Code: crates/synapse-wire/ (frame.rs, transport.rs, session.rs, probe.rs, fuzz.rs, testkit.rs, src/bin/synapse-wire-fuzz.rs); crates/synapse-bench/src/bin/wire-transport.rs; crates/synapse-store/src/codec.rs (encode_envelope, decode_envelope); crates/synapse-core/src/lib.rs (RefusalCode::WireFrameMalformed, RefusalCode::WireProtocolViolation). Tests: crates/synapse-wire/tests/g1_wire.rs, crates/synapse-wire/tests/g1_fuzz.rs. Evidence: evidence/g1-wire-frames/.

1. Frame layout

A frame is a 16-byte header and body_len bytes of body. Integers are little endian and fixed width.

BytesFieldRule
0..4magic SYNWelse BadMagic
4version 1else VersionUnsupported
5kind: 1 Hello, 2 Have, 3 Want, 4 Chunk, 5 Submit, 6 Accepted, 7 Refusedelse UnknownKind
6flags 0else FlagsNonZero
7reserved 0else ReservedNonZero
8..12request_id u32echoed in the reply
12..16body_len u32at most the reader's max_frame_body (default 16 MiB), else LengthOverCap before any body byte is read or allocated

Body primitives follow the A1 codec: byte strings and strings are u32 len | bytes (strings UTF-8), sequences are u32 count | items, fixed arrays are raw, tags are one byte.

KindBody
Hellomax_frame_body u32, max_pending_chunk_bytes u64
Haverepo_id string, digests (u32 count, 32 bytes each, strictly increasing)
Wantdigests (strictly increasing)
Chunkdigest [32], bytes
Submitthe F1 ChainedRequest: chain bytes (CapabilityChain::encode), target_repo_id string, agent_key [32], capability tag (1 to 6), scope paths (u32 count, strings), reference tag (0 none, 1 own drafts, 2 canonical via promotion queue), join change (0, or 1 then [16]), request_nonce [32], timestamp_ms u64, payload bytes (one binary changeset envelope, CODEC_V2 and the A1 envelope body), agent_signature [64]
Acceptedwal_seq u64, changeset_id [32], duplicate 0 or 1
Refusedrefusal code name (ASCII letters and digits, at most 64), message string

There is exactly one encoding per frame. A trailing byte, unknown tag, invalid UTF-8, invalid RepoId, unsorted or repeated digest, a chain that does not re-encode to its own bytes, or an unknown refusal code name is refused, so decode then encode gives back the input bytes. Each refusal is a typed WireRefusal with the absolute byte offset; at the service boundary it becomes RefusalCode::WireFrameMalformed.

There is no checksum: frames ride a reliable ordered stream (Unix socket or TCP), and the content that matters is signed (the envelope by its author, the request by the agent, the chain links by their issuers). The decoder takes the body as its own slice, so it cannot read into the next frame; the stream reader takes the header first, checks body_len against its cap, and only then allocates and reads exactly that many bytes.

2. Have-want negotiation and submission

client                                   server
  Hello ------------------------------->     once per connection, both ways
  <------------------------------- Hello
  Have {repo, digests the change needs} ->
  <---------- Want {offered digests the server lacks}
  Chunk x |Want|, Submit {chained request} ->   one write
  <--------------- Accepted {wal_seq} | Refused {code}

The server lacks a digest when no committed changeset references it and no chunk of the current negotiation carried it, so a blob the server holds is never sent again (FindMissingBlobs style, section 5.9). Server rules, each a typed refusal that resets the negotiation:

BreakRefusal
Any frame before Hello (then the connection closes), a second Hello, or a frame a server never takes from a client (Want, Accepted, Refused)WireProtocolViolation
Have for another repositoryAuthCrossRepoDenied
Chunk not in the last Want, or sent twiceWireProtocolViolation
Chunk bytes that do not hash to its digestBlobDigestMismatch
Chunk bytes over max_pending_chunk_bytes (default 64 MiB)QuotaPayloadTooLarge
Submit before every wanted chunkWireProtocolViolation
Submit payload not one canonical binary envelopeWireFrameMalformed
Chain, signature, capability (Propose), scope, size, rate or nonce check of PolicyEnforcer::authorize_chain (F1)its F1 code
Envelope for another repository; envelope author not the chain's leaf audience; request scope not exactly the envelope's pathsAuthCrossRepoDenied; AuthSignatureInvalid; AuthPathOutOfScope
Malformed frame (then the connection closes, since framing can no longer be trusted)WireFrameMalformed

An accepted change is admitted through SharedStore::submit with group commit, and Accepted is written only after the batch is durable. A resubmission of a committed change returns Accepted with duplicate = 1.

3. Fuzzer: what kind it is

synapse_wire::fuzz is an in-repository coverage-guided mutation fuzzer, written for this step (no libFuzzer, AFL or cargo-fuzz, no new external crates). The frame decoder is generic over a Probe; production passes NoProbe (empty, inlined away), the fuzzer passes EdgeProbe, which records an AFL-style edge map (64 Ki slots, edge = previous site xor current site) over 44 instrumented decoder states in frame.rs (one per branch; the site id is the source line) and the highest input offset read. An input that reaches a new edge or a new hit-count bucket joins the corpus. Mutations: bit flips, random and boundary bytes, boundary u32 values (0, cap - 1, cap, cap + 1, bytes remaining, u32::MAX and others), truncation, insertion, deletion, duplication, splicing two corpus entries, 32-byte block swaps, kind byte rewrites, and half the time a fix-up of body_len to the real body length so mutations reach the body decoder. The random generator is SplitMix64 with a fixed seed; the budget (executions or seconds) and seed are written to spec.json before the first execution.

Limits, stated plainly: coverage is only the instrumented states of frame.rs. The capability chain decoder (synapse-auth) and the envelope decoder (synapse-store::codec) run on every accepted Submit and their outputs are checked, but they are not instrumented, so the fuzzer does not steer toward their branches.

The target is what the server does with untrusted bytes: decode_frame, then decode_envelope on a Submit payload. Every execution checks:

CheckHow
No paniccatch_unwind around the target
No read past a framethe probe's high-water mark never exceeds 16 + body_len; and the same input followed by random trailing bytes gives the identical result whenever the input held the whole frame (a decision that depends on bytes after the frame shows up as a difference)
No allocation above the declared capa counting global allocator records the largest single allocation during the decode; it must not exceed max_frame_body
Canonical acceptan accepted frame re-encodes to exactly the bytes it came from; an accepted payload re-encodes to exactly the payload
Typed refusalanything not accepted is a WireRefusal (payloads: a CodecRefusal)

Seeds are one valid frame of every kind (14 frames, including empty lists, an empty chunk, depth 1 and depth 3 chains, an envelope with evidence and a delete, every option tag) and 126 hand-made malformed frames derived from them (wrong magic, version, kind 0 and 8, flags, reserved, length one over the cap, one byte short, one trailing byte).

Negative controls (negative_control_* in tests/g1_fuzz.rs) plant one bug each in a copy of the target (a panic on a non-zero flags byte, a buffer sized from the declared length before the cap check, an answer that depends on the byte after the frame, a decoder that ignores the reserved byte) and require the same fuzzer and budget to report it. All four are found, so the detectors are live.

3.1 Time-budgeted evidence runs

Both runs: release build with panic unwinding, overflow checks and debug assertions on; 300 s budget, seed 1592615168 and a cap of 8,192 bytes, written to spec.json before the first execution.

RunRevExecutionsEdgesSites reachedViolationsLargest single allocation
1 (fuzz-run1-71fa41e/)71fa41e703,411,4569942 of 431,307 allocation over cap8,760 bytes
2 (fuzz/)2e942f3711,944,19210043 of 4408,160 bytes

Run 1 found a real bug: a Submit whose scope lists many empty paths costs 4 wire bytes per path but 24 bytes of memory per decoded String, so the path list allocation passed the cap. The decoder now refuses a path count whose decoded size would pass the cap (the A1 codec already did this for its sequences), with the regression test fuzz_regression_many_scope_paths_stay_under_allocation_cap (red output in red-green/red-addendum-fuzz-finding.txt). Run 2, on the fixed code, found nothing; its corpus of 201 entries replays as 14 canonical accepts and 187 typed refusals of 14 kinds, with no violation.

The one site neither run reached is line 638 of frame.rs: a capability chain that decodes but does not re-encode to its own bytes. The F1 chain decoder already refuses every non-canonical encoding itself, so this check is very likely unreachable; it stays as a guard.

4. Acceptance

Acceptance item (issue #44)Test or evidenceResult
Every frame kind round-trips to identical bytesevery_frame_kind_round_trips_to_identical_bytes (all 7 kinds, singly, back to back in one buffer, and through the stream reader)PASS
Fuzzing for a budget fixed before the run finds no panic, no read past a frame, no allocation above the declared capfuzzing_frame_decoder_finds_no_panic_no_read_past_frame_no_allocation_over_cap (300,000 executions, seed fixed, in CI); time-budgeted run in evidence/g1-wire-frames/fuzz/ (300 s, seed 1592615168, cap 8192 bytes: 711,944,192 executions, 100 edges, 43 of 44 sites, 0 violations, largest single allocation 8,160 bytes); see section 3.1PASS
Every malformed frame in the fuzz corpus is refused with a typed refusalevery_malformed_frame_in_fuzz_corpus_is_refused_with_typed_refusal (replays the whole corpus: each entry is a canonical frame or a typed refusal, each hand-made malformed seed is refused); corpus replay in evidence/g1-wire-frames/fuzz/result.jsonPASS
Seconds and joules per accepted change across the transport against the boundevidence/g1-wire-frames/transport/ (section 5)MEASURED (see section 5; this is a report, not a pass/fail gate)

Supporting tests: malformed_header_is_refused_with_typed_refusal, malformed_body_is_refused_with_typed_refusal, decoder_never_reads_past_a_frame, stream_reader_refuses_length_over_cap_before_reading_the_body, have_want_sends_only_missing_chunks_and_acknowledges_durable_changes (2 then 1 then 0 chunks as blobs become known; a duplicate is idempotent; a fresh open of the store sees every acknowledged change), server_refuses_protocol_violations_with_typed_refusals, server_closes_connection_on_frame_before_hello_or_malformed_frame, and the four negative controls.

Red before green: evidence/g1-wire-frames/red-green/red.txt (two stub sets: the body decoder unimplemented; the header, trailing-byte and negotiation checks switched off) and green.txt.

5. Transport: seconds and joules per accepted change

Tool: crates/synapse-bench/src/bin/wire-transport.rs, release build, rev 2e942f3, run 2026-10-16. spec.json (arms, change count, repetitions, bound, sensor) was written before the first change; result.json holds every per-change latency in nanoseconds and every energy window.

Setup: one agent submits one change at a time, 2,000 changes per arm, 3 repetitions with the arm order rotated, each arm on a fresh store directory (NVMe, ext4, GROUP_COMMIT_DEFAULT). Every change upserts one new 3,967-byte blob (the Gate 4 median new content per change) and one blob shared by every change (sent once, then skipped by negotiation). Arms:

Bound: ADR 0003 section 6 durable latency for one change, as derived in docs/PHASE5_GROUP_COMMIT.md section 5: t_floor = 0.805 ms + 11,500 B x 0.138 s/GiB = 0.806 ms, E_floor = 22.731 W x t_floor + 0.2766 mJ = 18.6 mJ. Fraction of bound is floor divided by measured (1.0 means at the floor).

ArmMedian s per change (of 3 reps)Fraction of time boundSoC package J per changeFraction of energy boundAbove idle J per change
wire1.628 x 10^-30.49540.6 x 10^-30.4583.6 x 10^-3
direct_auth1.409 x 10^-30.57234.8 x 10^-30.5340.8 x 10^-3
direct1.395 x 10^-30.57833.4 x 10^-30.557-0.3 x 10^-3

Repetitions for wire: 1.354, 1.628 and 1.668 ms per change. The transport (framing, socket hops and negotiation) adds about 0.22 ms per change over the same work in process (wire minus direct_auth). For comparison, the Phase 5 S3 reference for one change was 2.048 ms and 51.7 mJ.

Limits, stated plainly:

6. Limits and deviations