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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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::sync::Arc;

/// Event types for A2A Reasoning protocol over SEB
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Hash)]
#[repr(u16)]
pub enum ReasoningEventType {
    /// Trace has started: agent begins reasoning
    TraceStart = 0x0300,
    /// Single reasoning step emitted
    Step = 0x0301,
    /// Trace has completed
    TraceComplete = 0x0302,
    /// Challenge to a prior trace/step
    Challenge = 0x0303,
    /// Composition of multiple traces
    Composition = 0x0304,
    /// Query for specific reasoning information
    Query = 0x0305,
    /// Response to a query
    Response = 0x0306,
}

impl ReasoningEventType {
    pub fn as_u16(&self) -> u16 {
        *self as u16
    }

    pub fn from_u16(val: u16) -> Option<Self> {
        match val {
            0x0300 => Some(ReasoningEventType::TraceStart),
            0x0301 => Some(ReasoningEventType::Step),
            0x0302 => Some(ReasoningEventType::TraceComplete),
            0x0303 => Some(ReasoningEventType::Challenge),
            0x0304 => Some(ReasoningEventType::Composition),
            0x0305 => Some(ReasoningEventType::Query),
            0x0306 => Some(ReasoningEventType::Response),
            _ => None,
        }
    }
}

/// Partition path for reasoning events
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ReasoningPartition {
    /// reasoning/{agent_id} - Personal reasoning trace stream
    AgentTraces(String),
    /// reasoning/challenges - Global challenge stream
    Challenges,
    /// reasoning/compositions - Global composition stream
    Compositions,
    /// reasoning/queries - Query stream
    Queries,
}

impl ReasoningPartition {
    pub fn path(&self) -> String {
        match self {
            ReasoningPartition::AgentTraces(agent_id) => format!("reasoning/{}", agent_id),
            ReasoningPartition::Challenges => "reasoning/challenges".to_string(),
            ReasoningPartition::Compositions => "reasoning/compositions".to_string(),
            ReasoningPartition::Queries => "reasoning/queries".to_string(),
        }
    }

    pub fn from_path(path: &str) -> Option<Self> {
        match path {
            "reasoning/challenges" => Some(ReasoningPartition::Challenges),
            "reasoning/compositions" => Some(ReasoningPartition::Compositions),
            "reasoning/queries" => Some(ReasoningPartition::Queries),
            p if p.starts_with("reasoning/") => {
                let agent_id = p.strip_prefix("reasoning/")?.to_string();
                if !agent_id.is_empty() && agent_id != "challenges" && agent_id != "compositions"
                    && agent_id != "queries"
                {
                    Some(ReasoningPartition::AgentTraces(agent_id))
                } else {
                    None
                }
            }
            _ => None,
        }
    }
}

/// Payload for TRACE_START event
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TraceStartPayload {
    pub trace_id: String,
    pub agent_id: String,
    pub competency: String,
    pub query: Option<String>,
}

/// Payload for STEP event
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StepPayload {
    pub trace_id: String,
    pub step_index: usize,
    pub step_json: serde_json::Value,
    pub step_hash: String,
}

/// Payload for TRACE_COMPLETE event
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TraceCompletePayload {
    pub trace_id: String,
    pub duration_ms: u64,
    pub step_count: usize,
    pub confidence: Option<f64>,
}

/// Payload for CHALLENGE event
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChallengePayload {
    pub challenge_trace_id: String,
    pub target_trace_id: String,
    pub target_step_index: Option<usize>,
    pub counter_evidence: String,
}

/// Payload for COMPOSITION event
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CompositionPayload {
    pub composition_trace_id: String,
    pub sub_trace_ids: Vec<String>,
    pub composition_rule: String,
}

/// Payload for QUERY event
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QueryPayload {
    pub query_id: String,
    pub agent_id: String,
    pub query_type: String,
    pub query_data: serde_json::Value,
}

/// Payload for RESPONSE event
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResponsePayload {
    pub query_id: String,
    pub responding_agent: String,
    pub trace_ids: Vec<String>,
    pub results: Vec<serde_json::Value>,
}

/// A2A Reasoning Event - wrapper for all reasoning event types
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct A2AReasoningEvent {
    pub event_type: u16, // ReasoningEventType code
    pub timestamp: DateTime<Utc>,
    pub partition: String,
    pub payload: serde_json::Value,
}

impl A2AReasoningEvent {
    pub fn new(event_type: ReasoningEventType, partition: ReasoningPartition) -> Self {
        A2AReasoningEvent {
            event_type: event_type.as_u16(),
            timestamp: Utc::now(),
            partition: partition.path(),
            payload: serde_json::json!({}),
        }
    }

    pub fn with_trace_start(
        agent_id: String,
        competency: String,
        trace_id: String,
        query: Option<String>,
    ) -> Self {
        let partition = ReasoningPartition::AgentTraces(agent_id.clone());
        let mut event = Self::new(ReasoningEventType::TraceStart, partition);

        let payload = TraceStartPayload {
            trace_id,
            agent_id,
            competency,
            query,
        };

        event.payload = serde_json::to_value(payload).unwrap_or_default();
        event
    }

    pub fn with_step(trace_id: String, step_index: usize, step_json: serde_json::Value) -> Self {
        let step_hash = blake3::hash(
            serde_json::to_string(&step_json)
                .unwrap_or_default()
                .as_bytes(),
        )
        .to_hex()
        .to_string();

        let partition = ReasoningPartition::Queries; // Use queries partition for broadcast
        let mut event = Self::new(ReasoningEventType::Step, partition);

        let payload = StepPayload {
            trace_id,
            step_index,
            step_json,
            step_hash,
        };

        event.payload = serde_json::to_value(payload).unwrap_or_default();
        event
    }

    pub fn with_challenge(
        target_trace_id: String,
        counter_evidence: String,
        target_step_index: Option<usize>,
    ) -> Self {
        let challenge_trace_id = uuid::Uuid::new_v4().to_string();
        let partition = ReasoningPartition::Challenges;
        let mut event = Self::new(ReasoningEventType::Challenge, partition);

        let payload = ChallengePayload {
            challenge_trace_id,
            target_trace_id,
            target_step_index,
            counter_evidence,
        };

        event.payload = serde_json::to_value(payload).unwrap_or_default();
        event
    }

    pub fn with_composition(
        composition_trace_id: String,
        sub_trace_ids: Vec<String>,
        composition_rule: String,
    ) -> Self {
        let partition = ReasoningPartition::Compositions;
        let mut event = Self::new(ReasoningEventType::Composition, partition);

        let payload = CompositionPayload {
            composition_trace_id,
            sub_trace_ids,
            composition_rule,
        };

        event.payload = serde_json::to_value(payload).unwrap_or_default();
        event
    }

    pub fn as_json_line(&self) -> String {
        serde_json::to_string(self).unwrap_or_default()
    }
}

/// A2A Protocol handler for emitting and receiving reasoning events
pub struct A2AProtocolHandler {
    agent_id: String,
    event_log: Arc<tokio::sync::Mutex<Vec<A2AReasoningEvent>>>,
}

impl A2AProtocolHandler {
    pub fn new(agent_id: String) -> Self {
        A2AProtocolHandler {
            agent_id,
            event_log: Arc::new(tokio::sync::Mutex::new(Vec::new())),
        }
    }

    /// Emit a reasoning trace start event
    pub async fn emit_trace_start(
        &self,
        competency: String,
        trace_id: String,
        query: Option<String>,
    ) {
        let event = A2AReasoningEvent::with_trace_start(
            self.agent_id.clone(),
            competency,
            trace_id,
            query,
        );
        let mut log = self.event_log.lock().await;
        log.push(event);
    }

    /// Emit a reasoning step event
    pub async fn emit_step(&self, trace_id: String, step_index: usize, step_json: serde_json::Value) {
        let event = A2AReasoningEvent::with_step(trace_id, step_index, step_json);
        let mut log = self.event_log.lock().await;
        log.push(event);
    }

    /// Emit a trace complete event
    pub async fn emit_trace_complete(
        &self,
        trace_id: String,
        duration_ms: u64,
        step_count: usize,
        confidence: Option<f64>,
    ) {
        let partition = ReasoningPartition::AgentTraces(self.agent_id.clone());
        let mut event = A2AReasoningEvent::new(ReasoningEventType::TraceComplete, partition);

        let payload = TraceCompletePayload {
            trace_id,
            duration_ms,
            step_count,
            confidence,
        };

        event.payload = serde_json::to_value(payload).unwrap_or_default();
        let mut log = self.event_log.lock().await;
        log.push(event);
    }

    /// Emit a challenge event
    pub async fn emit_challenge(
        &self,
        target_trace_id: String,
        counter_evidence: String,
        target_step_index: Option<usize>,
    ) {
        let event =
            A2AReasoningEvent::with_challenge(target_trace_id, counter_evidence, target_step_index);
        let mut log = self.event_log.lock().await;
        log.push(event);
    }

    /// Emit a composition event
    pub async fn emit_composition(
        &self,
        composition_trace_id: String,
        sub_trace_ids: Vec<String>,
        composition_rule: String,
    ) {
        let event = A2AReasoningEvent::with_composition(composition_trace_id, sub_trace_ids, composition_rule);
        let mut log = self.event_log.lock().await;
        log.push(event);
    }

    /// Get all emitted events
    pub async fn get_events(&self) -> Vec<A2AReasoningEvent> {
        let log = self.event_log.lock().await;
        log.clone()
    }

    /// Get events by partition
    pub async fn get_events_by_partition(&self, partition: &ReasoningPartition) -> Vec<A2AReasoningEvent> {
        let log = self.event_log.lock().await;
        let partition_path = partition.path();
        log.iter()
            .filter(|e| e.partition == partition_path)
            .cloned()
            .collect()
    }

    /// Get events by type
    pub async fn get_events_by_type(&self, event_type: ReasoningEventType) -> Vec<A2AReasoningEvent> {
        let log = self.event_log.lock().await;
        let type_code = event_type.as_u16();
        log.iter()
            .filter(|e| e.event_type == type_code)
            .cloned()
            .collect()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_event_type_codes() {
        assert_eq!(ReasoningEventType::TraceStart.as_u16(), 0x0300);
        assert_eq!(ReasoningEventType::Challenge.as_u16(), 0x0303);
        assert_eq!(ReasoningEventType::from_u16(0x0300), Some(ReasoningEventType::TraceStart));
        assert_eq!(ReasoningEventType::from_u16(0xFFFF), None);
    }

    #[test]
    fn test_partition_paths() {
        let p1 = ReasoningPartition::AgentTraces("agent_001".into());
        assert_eq!(p1.path(), "reasoning/agent_001");

        let p2 = ReasoningPartition::Challenges;
        assert_eq!(p2.path(), "reasoning/challenges");

        assert_eq!(ReasoningPartition::from_path("reasoning/agent_001"), Some(p1));
        assert_eq!(ReasoningPartition::from_path("reasoning/challenges"), Some(p2));
    }

    #[tokio::test]
    async fn test_protocol_handler() {
        let handler = A2AProtocolHandler::new("agent_001".into());

        handler
            .emit_trace_start("verify".into(), "trace_001".into(), None)
            .await;

        let events = handler.get_events().await;
        assert_eq!(events.len(), 1);
        assert_eq!(events[0].event_type, 0x0300);
    }

    #[test]
    fn test_event_creation() {
        let event = A2AReasoningEvent::with_trace_start(
            "agent_001".into(),
            "verify".into(),
            "trace_001".into(),
            Some("query".into()),
        );

        assert_eq!(event.event_type, 0x0300);
        assert!(event.partition.contains("agent_001"));
    }
}