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
//! The protocol's half of the session engine: what a message means, and what
//! it was agreed on.
//!
//! # Why the trait has two methods
//!
//! [`classify`](Lifecycle::classify) and [`resolve`](Lifecycle::resolve) are the
//! only two questions the engine ever asks a protocol, and they are asked
//! together for a reason: a protocol that answered "this method is permitted"
//! with one rule and "this method opens the handshake" with another could
//! disagree, and the disagreement would show up as a session whose phase is
//! wrong while every individual answer looks reasonable. One method that
//! classifies, and one that reads a declaration, cannot be split apart without
//! the engine having to reconcile two sources of truth.
//!
//! # The two associated types, and why the engine cannot look inside them
//!
//! `Profile` is MCP's `FeatureProfile`, a u128 bitset, and would be A2A's
//! `Major.Minor` pair. `Version` is a date for MCP and something else
//! entirely for A2A. The engine stores both opaquely and never inspects
//! either: it passes a `Profile` back to the lifecycle whenever it needs a
//! decision, and renders a `Version` into a string for the event log. That is
//! what keeps this module free of MCP types, and it is the same property T09's
//! `MethodTable` has — a protocol drops in as an implementation rather than as
//! a fork.
//!
//! # Implementing it for a protocol with no handshake
//!
//! A2A has none: every request carries an `A2A-Version` header and there is
//! nothing to initialise. The sketch below is the whole of it, and it is a
//! doc-test rather than a stub in the tree so that nobody has to keep a
//! half-implemented protocol compiling. Note what it does *not* need: a
//! version list, a capability set, a transport, or any of the machinery MCP
//! requires, because none of those concepts are referenced here.
use Value;
use crateSessionState;
/// What a message naming a method means for a session in a given state.
/// What a peer declared about itself, in this crate's vocabulary.
/// The reserved metadata a request carried.
///
/// A borrowed view over an already-parsed object, because the transport
/// boundary is where JSON is parsed and nothing downstream of it should meet a
/// raw string. A protocol that reserves no keys is handed an empty view and
/// answers `None`; one that reserves several picks the ones it defines and
/// ignores the rest, because a peer sending a key this protocol has not heard
/// of is a peer running a newer draft, not a peer that sent nonsense.
/// What a protocol answered to a declaration.
/// Why a session could not be established.
///
/// Deliberately smaller than the engine's own error: these are the cases where
/// the *protocol's* answer is "I cannot", and the engine adds its own state
/// errors around them. The distinction matters because only one of the two is
/// something a peer can fix by asking for something different.
/// The vocabulary one protocol brings to the session engine.
///
/// Declared by the engine, which is its only consumer, and implemented by
/// whichever protocol module is in play. See the module docs for why it is two
/// methods and two associated types rather than a wider interface.
///
/// # Examples
///
/// A protocol with no handshake, which is what A2A's per-request version header
/// amounts to. Nothing in this module changes for it.
///
/// ```
/// use protocol_droid::application::session::{
/// Classification, Declaration, DeclarationError, Lifecycle, MetaView, Resolution,
/// };
/// use protocol_droid::domain::session::SessionState;
///
/// /// A2A speaks `Major.Minor`; MCP speaks dates. Nothing here assumes either.
/// #[derive(Debug, Clone, Copy, PartialEq, Eq)]
/// struct MajorMinor(u8, u8);
///
/// impl std::fmt::Display for MajorMinor {
/// fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
/// write!(formatter, "{}.{}", self.0, self.1)
/// }
/// }
///
/// #[derive(Debug, Clone, Copy, PartialEq, Eq)]
/// struct A2AProfile {
/// version: MajorMinor,
/// }
///
/// struct A2ALifecycle {
/// supported: Vec<MajorMinor>,
/// }
///
/// impl Lifecycle for A2ALifecycle {
/// type Profile = A2AProfile;
/// type Version = MajorMinor;
///
/// fn classify(
/// &self,
/// state: SessionState,
/// _profile: Option<Self::Profile>,
/// _method: &str,
/// ) -> Classification {
/// // No handshake, so nothing is ever a handshake — and the engine
/// // does not need to be told what "initialize" means.
/// let _ = state;
/// Classification::Ordinary
/// }
///
/// fn resolve(&self, declaration: Declaration<'_>) -> Result<Resolution<Self::Profile, Self::Version>, DeclarationError> {
/// // A2A negotiates out of its own metadata rather than a version
/// // string, so both arms are read but neither is the version. That
/// // difference is the point: the engine never supplies it.
/// match declaration {
/// Declaration::Stateless { .. } | Declaration::Handshake { .. } => {}
/// }
/// let version = self.supported.first().copied().ok_or(DeclarationError::NoVersion)?;
/// Ok(Resolution {
/// profile: A2AProfile { version },
/// version,
/// echoed: true,
/// })
/// }
/// }
///
/// let lifecycle = A2ALifecycle { supported: vec![MajorMinor(0, 3)] };
/// let resolution = lifecycle
/// .resolve(Declaration::Stateless { meta: MetaView::default() })
/// .expect("a supported version is answered");
/// assert_eq!(resolution.version, MajorMinor(0, 3));
/// assert_eq!(
/// lifecycle.classify(SessionState::Stateless, Some(resolution.profile), "message/send"),
/// Classification::Ordinary,
/// );
/// ```