File size: 12,511 Bytes
9425aed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
use crate::a2a_protocol::{A2AReasoningEvent, ReasoningPartition};
use crate::trace::ReasoningTrace;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::RwLock;

/// Streaming mode for reasoning trace subscription
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StreamingMode {
    /// Stream events as they happen
    Live,
    /// Replay recorded events
    Replay,
    /// Summarized view (headers only)
    Summary,
}

/// WebSocket subscription to a reasoning partition
#[derive(Debug, Clone)]
pub struct ReasoningSubscription {
    pub partition: String,
    pub mode: StreamingMode,
    pub agent_id: Option<String>,
    pub created_at: DateTime<Utc>,
}

/// Timeline entry for trace visualization
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TimelineEntry {
    pub step_index: usize,
    pub timestamp: DateTime<Utc>,
    pub description: String,
    pub duration_ms: u64,
}

/// Mermaid sequence diagram for reasoning traces
pub struct MermaidSequenceDiagram {
    pub title: String,
    pub actors: Vec<String>,
    pub interactions: Vec<String>,
}

impl MermaidSequenceDiagram {
    pub fn new(title: String) -> Self {
        MermaidSequenceDiagram {
            title,
            actors: Vec::new(),
            interactions: Vec::new(),
        }
    }

    pub fn add_actor(&mut self, actor: String) {
        if !self.actors.contains(&actor) {
            self.actors.push(actor);
        }
    }

    pub fn add_interaction(&mut self, from: &str, to: &str, label: &str) {
        self.interactions.push(format!("{}->>{}:{}", from, to, label));
    }

    pub fn render(&self) -> String {
        let mut diagram = format!("sequenceDiagram\n    title {}\n", self.title);

        for actor in &self.actors {
            diagram.push_str(&format!("    participant {}\n", actor));
        }
        diagram.push('\n');

        for interaction in &self.interactions {
            diagram.push_str(&format!("    {}\n", interaction));
        }

        diagram
    }
}

/// Generate a Mermaid sequence diagram from multiple reasoning traces
pub fn generate_sequence_diagram(traces: &[ReasoningTrace]) -> MermaidSequenceDiagram {
    let mut diagram = MermaidSequenceDiagram::new("Multi-Agent Reasoning".to_string());

    for trace in traces {
        diagram.add_actor(trace.agent_id.clone());
    }

    // Add interactions based on trace order and relationships
    for (i, trace) in traces.iter().enumerate() {
        if i > 0 {
            let prev = &traces[i - 1];
            diagram.add_interaction(
                &prev.agent_id,
                &trace.agent_id,
                &format!("reasoning({})", trace.competency),
            );
        }
    }

    diagram
}

/// Timeline visualization for a reasoning trace
pub struct TraceTimeline {
    pub trace_id: String,
    pub entries: Vec<TimelineEntry>,
}

impl TraceTimeline {
    pub fn from_trace(trace: &ReasoningTrace) -> Self {
        let mut entries = Vec::new();
        let mut prev_time = trace.created_at;

        for (i, step) in trace.steps.iter().enumerate() {
            let duration_ms = (step.timestamp - prev_time)
                .num_milliseconds()
                .max(0) as u64;

            entries.push(TimelineEntry {
                step_index: i,
                timestamp: step.timestamp,
                description: step.step.description(),
                duration_ms,
            });

            prev_time = step.timestamp;
        }

        TraceTimeline {
            trace_id: trace.trace_id.clone(),
            entries,
        }
    }

    /// Render timeline as ASCII chart
    pub fn render_ascii(&self) -> String {
        let trace_id_short = if self.trace_id.len() >= 16 {
            &self.trace_id[0..16]
        } else {
            &self.trace_id
        };
        let mut output = format!("Trace: {}\n", trace_id_short);
        output.push_str("─────────────────────────────\n");

        let max_duration = self.entries.iter().map(|e| e.duration_ms).max().unwrap_or(1);

        for entry in &self.entries {
            let bar_width = if max_duration > 0 {
                ((entry.duration_ms as f64 / max_duration as f64) * 40.0) as usize
            } else {
                1
            };

            output.push_str(&format!(
                "[{:02}] {:50} {} ms {}ms\n",
                entry.step_index,
                &entry.description,
                "β–ˆ".repeat(bar_width),
                entry.duration_ms
            ));
        }

        output
    }
}

/// Stream manager for reasoning events
pub struct ReasoningStreamManager {
    subscriptions: Arc<RwLock<Vec<ReasoningSubscription>>>,
    event_buffer: Arc<RwLock<HashMap<String, Vec<A2AReasoningEvent>>>>, // partition -> events
    traces: Arc<RwLock<HashMap<String, ReasoningTrace>>>, // trace_id -> trace
}

impl ReasoningStreamManager {
    pub fn new() -> Self {
        ReasoningStreamManager {
            subscriptions: Arc::new(RwLock::new(Vec::new())),
            event_buffer: Arc::new(RwLock::new(HashMap::new())),
            traces: Arc::new(RwLock::new(HashMap::new())),
        }
    }

    /// Subscribe to a partition in live mode
    pub async fn subscribe_live(&self, partition: String) -> ReasoningSubscription {
        let agent_id = ReasoningPartition::from_path(&partition)
            .and_then(|p| match p {
                ReasoningPartition::AgentTraces(id) => Some(id),
                _ => None,
            });

        let sub = ReasoningSubscription {
            partition: partition.clone(),
            mode: StreamingMode::Live,
            agent_id,
            created_at: Utc::now(),
        };

        let mut subs = self.subscriptions.write().await;
        subs.push(sub.clone());
        sub
    }

    /// Add an event to the stream
    pub async fn emit_event(&self, event: A2AReasoningEvent) {
        let mut buffer = self.event_buffer.write().await;
        buffer
            .entry(event.partition.clone())
            .or_insert_with(Vec::new)
            .push(event);
    }

    /// Store a trace for later retrieval
    pub async fn store_trace(&self, trace: ReasoningTrace) {
        let mut traces = self.traces.write().await;
        traces.insert(trace.trace_id.clone(), trace);
    }

    /// Get all events for a partition
    pub async fn get_partition_events(&self, partition: &str) -> Vec<A2AReasoningEvent> {
        let buffer = self.event_buffer.read().await;
        buffer
            .get(partition)
            .cloned()
            .unwrap_or_default()
    }

    /// Get all traces
    pub async fn get_traces(&self) -> Vec<ReasoningTrace> {
        let traces = self.traces.read().await;
        traces.values().cloned().collect()
    }

    /// Get a specific trace
    pub async fn get_trace(&self, trace_id: &str) -> Option<ReasoningTrace> {
        let traces = self.traces.read().await;
        traces.get(trace_id).cloned()
    }

    /// Generate timeline for a trace
    pub async fn get_timeline(&self, trace_id: &str) -> Option<TraceTimeline> {
        let trace = self.get_trace(trace_id).await?;
        Some(TraceTimeline::from_trace(&trace))
    }

    /// Get all subscriptions
    pub async fn get_subscriptions(&self) -> Vec<ReasoningSubscription> {
        let subs = self.subscriptions.read().await;
        subs.clone()
    }

    /// Generate Mermaid diagrams for all traces
    pub async fn generate_diagrams(&self) -> Vec<MermaidSequenceDiagram> {
        let traces = self.get_traces().await;

        // Group by competency
        let mut by_competency: HashMap<String, Vec<ReasoningTrace>> = HashMap::new();
        for trace in traces {
            by_competency
                .entry(trace.competency.clone())
                .or_insert_with(Vec::new)
                .push(trace);
        }

        by_competency
            .into_iter()
            .map(|(_competency, traces)| generate_sequence_diagram(&traces))
            .collect()
    }

    /// Get summary statistics
    pub async fn get_summary(&self) -> HashMap<String, serde_json::Value> {
        let traces = self.get_traces().await;
        let buffer = self.event_buffer.read().await;

        let mut summary = HashMap::new();

        summary.insert(
            "total_traces".to_string(),
            serde_json::json!(traces.len()),
        );

        summary.insert(
            "total_events".to_string(),
            serde_json::json!(buffer.values().map(|v| v.len()).sum::<usize>()),
        );

        let total_steps = traces.iter().map(|t| t.steps.len()).sum::<usize>();
        summary.insert(
            "total_steps".to_string(),
            serde_json::json!(total_steps),
        );

        let agents: std::collections::HashSet<_> = traces.iter().map(|t| t.agent_id.clone()).collect();
        summary.insert(
            "unique_agents".to_string(),
            serde_json::json!(agents.len()),
        );

        let competencies: std::collections::HashSet<_> = traces.iter().map(|t| t.competency.clone()).collect();
        summary.insert(
            "competencies".to_string(),
            serde_json::json!(Vec::from_iter(competencies)),
        );

        summary
    }
}

impl Default for ReasoningStreamManager {
    fn default() -> Self {
        Self::new()
    }
}

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

    #[test]
    fn test_mermaid_diagram_generation() {
        let mut t1 = ReasoningTrace::new("kernel_001".into(), "verify".into(), 1);
        t1.finalize();

        let mut t2 = ReasoningTrace::new("policy_001".into(), "authorize".into(), 1);
        t2.add_parent(crate::trace::TraceRelation::Extends {
            parent_trace_id: t1.trace_id.clone(),
        });
        t2.finalize();

        let diagram = generate_sequence_diagram(&[t1, t2]);
        let rendered = diagram.render();

        assert!(rendered.contains("sequenceDiagram"));
        assert!(rendered.contains("kernel_001"));
        assert!(rendered.contains("policy_001"));
    }

    #[test]
    fn test_timeline_rendering() {
        let mut trace = ReasoningTrace::new("agent_001".into(), "verify".into(), 1);
        trace.add_step(ReasoningStep::Retrieve {
            source: "L1".into(),
            symbol: "test".into(),
            result: serde_json::json!({}),
        });
        trace.add_step(ReasoningStep::Verify {
            target: "sig".into(),
            method: "ed25519".into(),
            valid: true,
            error: None,
        });

        let timeline = TraceTimeline::from_trace(&trace);
        let ascii = timeline.render_ascii();

        assert!(ascii.contains("Trace:"));
        assert!(ascii.contains("Retrieve"));
        assert!(ascii.contains("Verify"));
    }

    #[tokio::test]
    async fn test_stream_manager() {
        let manager = ReasoningStreamManager::new();

        let sub = manager.subscribe_live("reasoning/agent_001".into()).await;
        assert_eq!(sub.partition, "reasoning/agent_001");

        let subs = manager.get_subscriptions().await;
        assert_eq!(subs.len(), 1);
    }

    #[tokio::test]
    async fn test_emit_and_retrieve_events() {
        let manager = ReasoningStreamManager::new();

        let event =
            A2AReasoningEvent::with_trace_start("agent_001".into(), "verify".into(), "trace_001".into(), None);
        manager.emit_event(event.clone()).await;

        let events = manager.get_partition_events(&event.partition).await;
        assert_eq!(events.len(), 1);
    }

    #[tokio::test]
    async fn test_store_and_retrieve_trace() {
        let manager = ReasoningStreamManager::new();

        let mut trace = ReasoningTrace::new("agent_001".into(), "verify".into(), 1);
        trace.finalize();
        let trace_id = trace.trace_id.clone();

        manager.store_trace(trace).await;

        let retrieved = manager.get_trace(&trace_id).await;
        assert!(retrieved.is_some());
    }
}