1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
//! Canonical Send / Recv NodeProto shape — the contract every
//! wire-IR-touching pass + runtime gate agrees on.
//!
//! Per `docs-plan/CORRECTED_ARCHITECTURE.md` §Seam: DSL→Compiler /
//! §Seam: Compiler→Runtime / §Wire format, the wire ops carry a
//! specific NodeProto shape. Scattered string literals and
//! divergent value layouts have repeatedly caused contract drift
//! (the closed CRIT findings around `S5`, `S6`, `S7`, `B6`–`B11`).
//!
//! This module is the **single declarative description** of that
//! shape. The DSL emits to it; the compiler passes mutate within
//! it; the runtime consumes it. [`crate::verify::wire_shape`]
//! checks a `ModelProto` against the contract.
//!
//! ## The shape
//!
//! ### Send
//!
//! ```text
//! NodeProto {
//! op_type: "Send",
//! domain: "ai.bytesandbrains.wire",
//! input: [payload_0, payload_1, ..., payload_{N-1}, peer],
//! output: [handle],
//! attribute: [
//! (name: "peer", type: BYTES, t: <PeerId.to_bytes() multihash>),
//! (name: "dest_suffix.{name}", type: BYTES, t: <multiaddr-bytes>),
//! (name: "deadline_ns", type: INT, i: <i64-ns>), // optional
//! ],
//! metadata_props: [
//! ("ai.bytesandbrains.wire.wire_id", "<u64>"),
//! ("ai.bytesandbrains.wire_transport", "data" | "trigger_only"),
//! ("ai.bytesandbrains.batch_group_id", "<u32>"),
//! ("ai.bytesandbrains.dest_site_name.{name}", "<recv-site-name>"),
//! ],
//! }
//! ```
//!
//! ### Recv
//!
//! ```text
//! NodeProto {
//! op_type: "Recv",
//! domain: "ai.bytesandbrains.wire",
//! input: [],
//! output: [received_0, received_1, ..., received_{N-1}, sender],
//! metadata_props: [
//! ("ai.bytesandbrains.wire.wire_id", "<u64 matching paired Send>"),
//! ("ai.bytesandbrains.wire_transport", "data" | "trigger_only"),
//! ],
//! }
//! ```
//!
//! `wire_id` pairs the Send/Recv halves across the cut.
//! `wire_transport` tells the runtime whether each fill carries a
//! payload (`data`) or is firing-signal-only (`trigger_only`).
//!
//! ## Key invariants the contract pins
//!
//! - **`ATTR_PEER` is bytes, not i64.** The peer attribute on a
//! Send carries the PeerId's canonical multihash byte form
//! (`PeerId::to_bytes()`), NOT a `u64`-collapsed identity hash.
//! Closes `B11`/`S6`. The runtime gates parse via
//! `PeerId::from_bytes(&attr.t)`.
//!
//! - **`wire_id` is the pairing token.** The DSL `Graph::wire`
//! mints a monotonic u64 and stamps it on BOTH halves; the
//! compiler's `discover_wire_edges` pair Send/Recv by it.
//! Closes `B7`.
//!
//! - **`wire_transport` lives on the NodeProto, not on a
//! `WireEdge` clone.** `analyze_wire_edges` mutates
//! `partition.functions[0].node[i].metadata_props` in place; the
//! `WireEdge` carrier is no longer the load-bearing storage for
//! the classification. Closes `B6`/`S5`.
//!
//! - **`SlotFill.type_hash` is populated from the sender side's
//! `T::HASH`.** Receivers dispatch wire bytes via
//! `if envelope.fill.type_hash == T::HASH { T::deserialize(&fill.payload) }`.
//! Closes `S10`.
/// Wire-op domain. All Send / Recv NodeProtos live under here.
pub const WIRE_DOMAIN: &str = "ai.bytesandbrains.wire";
/// op_type for a Send node.
pub const OP_SEND: &str = "Send";
/// op_type for a Recv node.
pub const OP_RECV: &str = "Recv";
/// Attribute key on a Send NodeProto carrying the destination peer
/// as **multihash bytes** (`PeerId::to_bytes()`), i.e. the
/// `attribute.t` (bytes) field, not `attribute.i` (i64).
///
/// Aliased from [`crate::syscall_ids::ATTR_PEER`] so consumers can
/// reach for `bb_ir::wire_shape::ATTR_PEER` or `bb_ir::keys::ATTR_PEER`
/// — they're the same key, kept in one place.
pub use crateATTR_PEER;
/// Attribute key prefix for per-fill multiaddr destination
/// suffixes. Full key is `format!("{DEST_SUFFIX_ATTR_PREFIX}{slot_name}")`.
pub use crateDEST_SUFFIX_ATTR_PREFIX;
/// Attribute key for the optional static deadline (in nanoseconds
/// since the reference clock epoch) stamped by
/// `insert_async_deadlines`.
pub use crateATTR_DEADLINE_NS;
/// `metadata_props` key carrying the wire-pairing token.
pub use crateWIRE_ID_KEY;
/// `metadata_props` key carrying the data-vs-trigger-only
/// classification.
pub use crateWIRE_TRANSPORT_KEY;
use crate;
/// Value of [`WIRE_TRANSPORT_KEY`] for full-payload edges.
pub use crateWIRE_TRANSPORT_DATA;
/// Value of [`WIRE_TRANSPORT_KEY`] for trigger-only edges.
pub use crateWIRE_TRANSPORT_TRIGGER_ONLY;
/// `metadata_props` key prefix for per-fill recv-site names. Full
/// key is `format!("{DEST_SITE_NAME_PREFIX}{slot_name}")`.
pub use crateDEST_SITE_NAME_PREFIX;
/// Return `true` if the NodeProto is a `wire.Send`.
/// Return `true` if the NodeProto is a `wire.Recv`.
/// Read the wire_id metadata stamp from a Send or Recv node.
/// Returns `None` if missing (e.g. legacy hand-built fixtures
/// without wire_id) or non-numeric.
/// Read the destination peer's multihash bytes from a Send / gate
/// NodeProto. Returns `None` if the attribute is absent or carries
/// no byte content. The byte payload lives on `attribute.s` per the
/// ONNX convention for raw bytes (paired with
/// `AttributeType::String`); callers reconstruct the PeerId via
/// `PeerId::from_bytes(read_peer_bytes(node)?)`.
/// Stamp the destination peer onto a Send / gate NodeProto's
/// `attribute.s` (bytes) using the canonical multihash form. Used
/// by the compiler's gate-insertion passes and any pass synthesizing
/// new Send NodeProtos.
/// Stamp the [`WIRE_TRANSPORT_KEY`] classification onto a wire-op
/// NodeProto's `metadata_props` (idempotent: replaces an existing
/// value).