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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
//! Peer-bus envelope schema (DOC-60 §11), carrying the §6 identity model.
//!
//! Why: DOC-60 §2's central finding is that all five existing buses address
//! the wrong unit — none of them carries the three identifiers (§6) that make
//! a message routable *and* attributable: the agent DEFINITION, the running
//! INSTANCE, and the per-message CALLER. This module is the one place those
//! three become a normative wire shape, so every producer and consumer agrees
//! on them instead of each inventing an ad hoc source tag.
//! What: [`BusEnvelope`] and its parts — [`BusEdge`], [`CallerIdentity`],
//! [`CallerKind`], [`ChannelOrigin`], [`Recipient`], [`DeliveryState`], and
//! [`BusPayload`]. Serde field names match DOC-60 §11's illustrative JSON so
//! the durable JSONL log (§9) is readable against the spec.
//! Test: `bus::tests` — `envelope_round_trips_json`,
//! `envelope_matches_doc60_field_names`, `peer_request_is_declinable`.
//!
//! Explicitly NOT here, all deferred per this change's scope: the durable
//! inbox and queue-not-spawn behavior (§7), cross-project addressing (§12 Q4),
//! retention policy (§12 Q1), and version-skew negotiation (§12 Q2/Q5).
use ;
/// Which of DOC-60 §5's three edges an envelope crossed.
///
/// Why: the three edges share one envelope and one delivery path by design
/// (§5.3: "a lateral message and a user message look identical from the
/// receiving assistant's side"), so the edge is recorded as data rather than
/// encoded as three separate message types. Search (§9) and consolidation
/// (§10) need to tell them apart after the fact.
/// What: the three §5 edges, serialized in snake_case exactly as §11 spells
/// them.
/// Test: `envelope_matches_doc60_field_names`.
/// What kind of party sent a message (DOC-60 §6c).
///
/// Why: this is the field that structurally prevents a peer message from
/// being mistaken for a user message. ADR-0024's virtual-twin principle makes
/// that distinction load-bearing: a message from `AssistantInstance` carries
/// communication, never command, and a recipient must be able to see that
/// from the envelope alone.
/// What: the three §11 caller kinds.
/// Test: `envelope_matches_doc60_field_names`.
/// Platform-native identity behind a channel-originated message (DOC-60 §8).
///
/// Why: §8 requires all three values be carried separately and "none collapsed
/// into one" — a channel message is not anonymous the way a bare CLI keystroke
/// is, and flattening them to a string would destroy the tenant
/// disambiguation the RBAC layer needs.
/// What: connector name plus the platform's sender, conversation, and tenant
/// ids.
/// Test: `envelope_round_trips_json`.
/// Who sent this message (DOC-60 §6c, one per envelope).
///
/// Why: caller identity is per-message, not session-lived — the same instance
/// can receive from a user on one envelope and a peer on the next, and the
/// reply must be addressable back to whichever asked.
/// What: the kind tag plus the §6a/§6b ids when the caller is an agent, and
/// the §8 channel identity when it is a channel.
/// Test: `envelope_round_trips_json`, `caller_validation_rejects_bare_peer`.
/// The resolved recipient stamped onto a delivered envelope (DOC-60 §11 `to`).
///
/// Why: §11 says `definition_id` is "always present" on `to`. Instance bypass
/// (a sender addressing a live `instance_id` directly) does not weaken that:
/// bypass is a REQUEST-time addressing mode, and the daemon fills BOTH ids
/// from the registry before stamping the envelope. The one case where
/// `definition_id` is absent is a *dropped* bypass record — the instance was
/// already gone, so the daemon has nothing to resolve its definition from.
/// What: the resolved instance id and the definition it belongs to.
/// Test: `bypass_publish_stamps_both_ids`, `dropped_bypass_has_no_definition`.
/// An envelope's own delivery lifecycle (DOC-60 §11).
///
/// Why: ADR-0019's acknowledgment requirement, carried into this bus. The
/// durable log must record a failed delivery as emphatically as a successful
/// one — that record is what makes "never sent" distinguishable from "sent but
/// never read".
/// What: the four §11 states. This change produces only `Delivered` and
/// `Dropped`; `Queued` and `Acked` await §7's durable inbox and the client-side
/// ack path respectively, and are defined now so the log format does not
/// change when they land.
/// Test: `failed_publish_logs_dropped_envelope`.
/// What an envelope actually carries.
///
/// Why: DOC-60 §5.3 makes decline-ability normative, derived from ADR-0024's
/// virtual-twin authority principle: a peer message that REQUESTS action must
/// be decline-able, or it is delegation under another name. That requires the
/// wire shape to distinguish a request from a plain informational message —
/// which is precisely why [`BusPayload::PeerRequest`] and
/// [`BusPayload::PeerResponse`] are separate variants rather than one
/// `chat_text` blob with a convention layered on top. §5.3 leaves the exact
/// shape to a later normative revision and settles only that decline-ability
/// is required; this is the minimum shape that satisfies it.
/// What: three variants, externally tagged by a `type` field per §11, seeded
/// from the `events.rs` vocabulary §3 retirement #4 names as the seed set.
/// Test: `peer_request_is_declinable`, `envelope_round_trips_json`.
/// One addressed, durable message crossing the peer bus (DOC-60 §11).
///
/// Why: this is the unit §9's JSONL log stores, §10's provenance pointer
/// references by `message_id`, and §5's three edges all share. Making it one
/// type — rather than one per edge — is what lets a receiving assistant handle
/// a user message and a peer message through a single path.
/// What: the §11 field set, with `message_id` globally unique and stable
/// across log rotation so a promoted memory can point back at it (§10).
/// Test: `envelope_round_trips_json`, `envelope_matches_doc60_field_names`.