1use std::collections::VecDeque;
5
6use tokio::sync::watch;
7use zeph_common::SecurityEventCategory;
8
9pub use zeph_llm::{ClassifierMetricsSnapshot, TaskMetricsSnapshot};
10pub use zeph_memory::{CategoryScore, ProbeCategory, ProbeVerdict};
11
12#[derive(Debug, Clone)]
14pub struct SecurityEvent {
15 pub timestamp: u64,
17 pub category: SecurityEventCategory,
18 pub source: String,
20 pub detail: String,
22}
23
24impl SecurityEvent {
25 #[must_use]
26 pub fn new(
27 category: SecurityEventCategory,
28 source: impl Into<String>,
29 detail: impl Into<String>,
30 ) -> Self {
31 let source: String = source
33 .into()
34 .chars()
35 .filter(|c| !c.is_ascii_control())
36 .take(64)
37 .collect();
38 let detail = detail.into();
40 let detail = if detail.len() > 128 {
41 let end = detail.floor_char_boundary(127);
42 format!("{}…", &detail[..end])
43 } else {
44 detail
45 };
46 Self {
47 timestamp: std::time::SystemTime::now()
48 .duration_since(std::time::UNIX_EPOCH)
49 .unwrap_or_default()
50 .as_secs(),
51 category,
52 source,
53 detail,
54 }
55 }
56}
57
58pub const SECURITY_EVENT_CAP: usize = 100;
60
61#[derive(Debug, Clone)]
65pub struct TaskSnapshotRow {
66 pub id: u32,
67 pub title: String,
68 pub status: String,
70 pub agent: Option<String>,
71 pub duration_ms: u64,
72 pub error: Option<String>,
74}
75
76#[derive(Debug, Clone, Default)]
78pub struct TaskGraphSnapshot {
79 pub graph_id: String,
80 pub goal: String,
81 pub status: String,
83 pub tasks: Vec<TaskSnapshotRow>,
84 pub completed_at: Option<std::time::Instant>,
85}
86
87impl TaskGraphSnapshot {
88 #[must_use]
91 pub fn is_stale(&self) -> bool {
92 self.completed_at
93 .is_some_and(|t| t.elapsed().as_secs() > 30)
94 }
95}
96
97#[derive(Debug, Clone, Default)]
101pub struct OrchestrationMetrics {
102 pub plans_total: u64,
103 pub tasks_total: u64,
104 pub tasks_completed: u64,
105 pub tasks_failed: u64,
106 pub tasks_skipped: u64,
107}
108
109#[non_exhaustive]
110#[derive(Debug, Clone, PartialEq, Eq)]
112pub enum McpServerConnectionStatus {
113 Connected,
114 Failed,
115}
116
117#[derive(Debug, Clone)]
119pub struct McpServerStatus {
120 pub id: String,
121 pub status: McpServerConnectionStatus,
122 pub tool_count: usize,
124 pub error: String,
126}
127
128#[derive(Debug, Clone, Default)]
130pub struct SkillConfidence {
131 pub name: String,
132 pub posterior: f64,
133 pub total_uses: u32,
134}
135
136#[derive(Debug, Clone, Default)]
138pub struct SubAgentMetrics {
139 pub id: String,
140 pub name: String,
141 pub state: String,
143 pub turns_used: u32,
144 pub max_turns: u32,
145 pub background: bool,
146 pub elapsed_secs: u64,
147 pub permission_mode: String,
150 pub transcript_dir: Option<String>,
153}
154
155#[derive(Debug, Clone, Default)]
160pub struct TurnTimings {
161 pub prepare_context_ms: u64,
162 pub llm_chat_ms: u64,
163 pub tool_exec_ms: u64,
164 pub persist_message_ms: u64,
165}
166
167#[derive(Debug, Clone, Default)]
174#[allow(clippy::struct_excessive_bools)] pub struct MetricsSnapshot {
176 pub prompt_tokens: u64,
177 pub completion_tokens: u64,
178 pub total_tokens: u64,
179 pub reasoning_tokens: u64,
183 pub context_tokens: u64,
184 pub api_calls: u64,
185 pub active_skills: Vec<String>,
186 pub total_skills: usize,
187 pub mcp_server_count: usize,
189 pub mcp_tool_count: usize,
190 pub mcp_connected_count: usize,
192 pub mcp_servers: Vec<McpServerStatus>,
194 pub active_mcp_tools: Vec<String>,
195 pub sqlite_message_count: u64,
196 pub sqlite_conversation_id: Option<zeph_memory::ConversationId>,
197 pub qdrant_available: bool,
198 pub vector_backend: String,
199 pub embeddings_generated: u64,
200 pub last_llm_latency_ms: u64,
201 pub uptime_seconds: u64,
202 pub provider_name: String,
203 pub model_name: String,
204 pub summaries_count: u64,
205 pub context_compactions: u64,
206 pub compaction_hard_count: u64,
209 pub compaction_turns_after_hard: Vec<u64>,
213 pub compression_events: u64,
214 pub compression_tokens_saved: u64,
215 pub acon_results_compressed: u64,
217 pub acon_tokens_saved: u64,
219 pub tool_output_prunes: u64,
220 pub compaction_probe_passes: u64,
222 pub compaction_probe_soft_failures: u64,
224 pub compaction_probe_failures: u64,
226 pub compaction_probe_errors: u64,
228 pub last_probe_verdict: Option<zeph_memory::ProbeVerdict>,
230 pub last_probe_score: Option<f32>,
233 pub last_probe_category_scores: Option<Vec<zeph_memory::CategoryScore>>,
235 pub compaction_probe_threshold: f32,
237 pub compaction_probe_hard_fail_threshold: f32,
239 pub cache_read_tokens: u64,
240 pub cache_creation_tokens: u64,
241 pub cost_spent_cents: f64,
242 pub cost_cps_cents: Option<f64>,
244 pub cost_successful_tasks: u64,
246 pub provider_cost_breakdown: Vec<(String, crate::cost::ProviderUsage)>,
248 pub filter_raw_tokens: u64,
249 pub filter_saved_tokens: u64,
250 pub filter_applications: u64,
251 pub filter_total_commands: u64,
252 pub filter_filtered_commands: u64,
253 pub filter_confidence_full: u64,
254 pub filter_confidence_partial: u64,
255 pub filter_confidence_fallback: u64,
256 pub cancellations: u64,
257 pub server_compaction_events: u64,
258 pub sanitizer_runs: u64,
259 pub sanitizer_injection_flags: u64,
260 pub sanitizer_injection_fp_local: u64,
266 pub sanitizer_truncations: u64,
267 pub quarantine_invocations: u64,
268 pub quarantine_failures: u64,
269 pub classifier_tool_blocks: u64,
271 pub classifier_tool_suspicious: u64,
273 pub causal_ipi_flags: u64,
275 pub vigil_flags_total: u64,
277 pub vigil_blocks_total: u64,
279 pub exfiltration_images_blocked: u64,
280 pub exfiltration_tool_urls_flagged: u64,
281 pub exfiltration_memory_guards: u64,
282 pub pii_scrub_count: u64,
283 pub pii_ner_timeouts: u64,
285 pub pii_ner_circuit_breaker_trips: u64,
287 pub memory_validation_failures: u64,
288 pub rate_limit_trips: u64,
289 pub pre_execution_blocks: u64,
290 pub pre_execution_warnings: u64,
291 pub guardrail_enabled: bool,
293 pub guardrail_warn_mode: bool,
295 pub sub_agents: Vec<SubAgentMetrics>,
296 pub skill_confidence: Vec<SkillConfidence>,
297 pub scheduled_tasks: Vec<[String; 4]>,
299 pub router_thompson_stats: Vec<(String, f64, f64)>,
301 pub security_events: VecDeque<SecurityEvent>,
303 pub orchestration: OrchestrationMetrics,
304 pub orchestration_graph: Option<TaskGraphSnapshot>,
306 pub graph_community_detection_failures: u64,
307 pub graph_entities_total: u64,
308 pub graph_edges_total: u64,
309 pub graph_communities_total: u64,
310 pub graph_extraction_count: u64,
311 pub graph_extraction_failures: u64,
312 pub extended_context: bool,
315 pub guidelines_version: u32,
317 pub guidelines_updated_at: String,
319 pub tool_cache_hits: u64,
320 pub tool_cache_misses: u64,
321 pub tool_cache_entries: usize,
322 pub semantic_fact_count: u64,
324 pub stt_model: Option<String>,
326 pub compaction_model: Option<String>,
328 pub provider_temperature: Option<f32>,
330 pub provider_top_p: Option<f32>,
332 pub embedding_model: String,
334 pub token_budget: Option<u64>,
336 pub compaction_threshold: Option<u32>,
338 pub vault_backend: String,
340 pub active_channel: String,
342 pub bg_inflight: u64,
344 pub bg_dropped: u64,
346 pub bg_completed: u64,
348 pub bg_enrichment_inflight: u64,
350 pub bg_telemetry_inflight: u64,
352 pub shell_background_runs: Vec<ShellBackgroundRunRow>,
354 pub self_learning_enabled: bool,
356 pub semantic_cache_enabled: bool,
358 pub cache_enabled: bool,
360 pub autosave_enabled: bool,
362 pub classifier: ClassifierMetricsSnapshot,
364 pub last_turn_timings: TurnTimings,
366 pub avg_turn_timings: TurnTimings,
368 pub max_turn_timings: TurnTimings,
372 pub timing_sample_count: u64,
374 pub egress_requests_total: u64,
376 pub egress_dropped_total: u64,
378 pub egress_blocked_total: u64,
380 pub context_max_tokens: u64,
386 pub compaction_last_before: u64,
388 pub compaction_last_after: u64,
390 pub compaction_last_at_ms: u64,
392 pub active_goal: Option<crate::goal::GoalSnapshot>,
394 pub cocoon_connected: Option<bool>,
397 pub cocoon_worker_count: u32,
399 pub cocoon_model_count: usize,
401 pub cocoon_ton_balance: Option<f64>,
403}
404
405#[derive(Debug, Clone, Default, serde::Serialize)]
411pub struct ShellBackgroundRunRow {
412 pub run_id: String,
414 pub command: String,
416 pub elapsed_secs: u64,
418}
419
420#[derive(Debug, Default)]
438pub struct StaticMetricsInit {
439 pub stt_model: Option<String>,
441 pub compaction_model: Option<String>,
443 pub semantic_cache_enabled: bool,
448 pub embedding_model: String,
450 pub self_learning_enabled: bool,
452 pub active_channel: String,
454 pub token_budget: Option<u64>,
456 pub compaction_threshold: Option<u32>,
458 pub vault_backend: String,
460 pub autosave_enabled: bool,
462 pub model_name_override: Option<String>,
466}
467
468fn strip_ctrl(s: &str) -> String {
474 let mut out = String::with_capacity(s.len());
475 let mut chars = s.chars().peekable();
476 while let Some(c) = chars.next() {
477 if c == '\x1b' {
478 if chars.peek() == Some(&'[') {
480 chars.next(); for inner in chars.by_ref() {
482 if ('\x40'..='\x7e').contains(&inner) {
483 break;
484 }
485 }
486 }
487 } else if c.is_control() && c != '\t' && c != '\n' && c != '\r' {
489 } else {
491 out.push(c);
492 }
493 }
494 out
495}
496
497impl From<&zeph_orchestration::TaskGraph> for TaskGraphSnapshot {
499 fn from(graph: &zeph_orchestration::TaskGraph) -> Self {
500 let tasks = graph
501 .tasks
502 .iter()
503 .map(|t| {
504 let error = t
505 .result
506 .as_ref()
507 .filter(|_| t.status == zeph_orchestration::TaskStatus::Failed)
508 .and_then(|r| {
509 if r.output.is_empty() {
510 None
511 } else {
512 let s = strip_ctrl(&r.output);
514 if s.len() > 80 {
515 let end = s.floor_char_boundary(79);
516 Some(format!("{}…", &s[..end]))
517 } else {
518 Some(s)
519 }
520 }
521 });
522 let duration_ms = t.result.as_ref().map_or(0, |r| r.duration_ms);
523 TaskSnapshotRow {
524 id: t.id.as_u32(),
525 title: strip_ctrl(&t.title),
526 status: t.status.to_string(),
527 agent: t.assigned_agent.as_deref().map(strip_ctrl),
528 duration_ms,
529 error,
530 }
531 })
532 .collect();
533 Self {
534 graph_id: graph.id.to_string(),
535 goal: strip_ctrl(&graph.goal),
536 status: graph.status.to_string(),
537 tasks,
538 completed_at: None,
539 }
540 }
541}
542
543pub struct MetricsCollector {
544 tx: watch::Sender<MetricsSnapshot>,
545}
546
547impl MetricsCollector {
548 #[must_use]
549 pub fn new() -> (Self, watch::Receiver<MetricsSnapshot>) {
550 let (tx, rx) = watch::channel(MetricsSnapshot::default());
551 (Self { tx }, rx)
552 }
553
554 pub fn update(&self, f: impl FnOnce(&mut MetricsSnapshot)) {
555 self.tx.send_modify(f);
556 }
557
558 pub fn set_context_max_tokens(&self, max_tokens: u64) {
573 self.tx.send_modify(|m| m.context_max_tokens = max_tokens);
574 }
575
576 pub fn record_compaction(&self, before: u64, after: u64, at_ms: u64) {
594 self.tx.send_modify(|m| {
595 m.compaction_last_before = before;
596 m.compaction_last_after = after;
597 m.compaction_last_at_ms = at_ms;
598 });
599 }
600
601 #[must_use]
607 pub fn sender(&self) -> watch::Sender<MetricsSnapshot> {
608 self.tx.clone()
609 }
610}
611
612pub trait HistogramRecorder: Send + Sync {
650 fn observe_llm_latency(&self, duration: std::time::Duration);
652
653 fn observe_turn_duration(&self, duration: std::time::Duration);
655
656 fn observe_tool_execution(&self, duration: std::time::Duration);
658
659 fn observe_bg_task(&self, class_label: &str, duration: std::time::Duration);
663}
664
665#[cfg(test)]
666mod tests {
667 #![allow(clippy::field_reassign_with_default)]
668
669 use super::*;
670
671 #[test]
672 fn default_metrics_snapshot() {
673 let m = MetricsSnapshot::default();
674 assert_eq!(m.total_tokens, 0);
675 assert_eq!(m.api_calls, 0);
676 assert!(m.active_skills.is_empty());
677 assert!(m.active_mcp_tools.is_empty());
678 assert_eq!(m.mcp_tool_count, 0);
679 assert_eq!(m.mcp_server_count, 0);
680 assert!(m.provider_name.is_empty());
681 assert_eq!(m.summaries_count, 0);
682 assert!(m.stt_model.is_none());
684 assert!(m.compaction_model.is_none());
685 assert!(m.provider_temperature.is_none());
686 assert!(m.provider_top_p.is_none());
687 assert!(m.active_channel.is_empty());
688 assert!(m.embedding_model.is_empty());
689 assert!(m.token_budget.is_none());
690 assert!(!m.self_learning_enabled);
691 assert!(!m.semantic_cache_enabled);
692 }
693
694 #[test]
695 fn metrics_collector_update_phase2_fields() {
696 let (collector, rx) = MetricsCollector::new();
697 collector.update(|m| {
698 m.stt_model = Some("whisper-1".into());
699 m.compaction_model = Some("haiku".into());
700 m.provider_temperature = Some(0.7);
701 m.provider_top_p = Some(0.95);
702 m.active_channel = "tui".into();
703 m.embedding_model = "nomic-embed-text".into();
704 m.token_budget = Some(200_000);
705 m.self_learning_enabled = true;
706 m.semantic_cache_enabled = true;
707 });
708 let s = rx.borrow();
709 assert_eq!(s.stt_model.as_deref(), Some("whisper-1"));
710 assert_eq!(s.compaction_model.as_deref(), Some("haiku"));
711 assert_eq!(s.provider_temperature, Some(0.7));
712 assert_eq!(s.provider_top_p, Some(0.95));
713 assert_eq!(s.active_channel, "tui");
714 assert_eq!(s.embedding_model, "nomic-embed-text");
715 assert_eq!(s.token_budget, Some(200_000));
716 assert!(s.self_learning_enabled);
717 assert!(s.semantic_cache_enabled);
718 }
719
720 #[test]
721 fn metrics_collector_update() {
722 let (collector, rx) = MetricsCollector::new();
723 collector.update(|m| {
724 m.api_calls = 5;
725 m.total_tokens = 1000;
726 });
727 let snapshot = rx.borrow().clone();
728 assert_eq!(snapshot.api_calls, 5);
729 assert_eq!(snapshot.total_tokens, 1000);
730 }
731
732 #[test]
733 fn metrics_collector_multiple_updates() {
734 let (collector, rx) = MetricsCollector::new();
735 collector.update(|m| m.api_calls = 1);
736 collector.update(|m| m.api_calls += 1);
737 assert_eq!(rx.borrow().api_calls, 2);
738 }
739
740 #[test]
741 fn metrics_snapshot_clone() {
742 let mut m = MetricsSnapshot::default();
743 m.provider_name = "ollama".into();
744 let cloned = m.clone();
745 assert_eq!(cloned.provider_name, "ollama");
746 }
747
748 #[test]
749 fn filter_metrics_tracking() {
750 let (collector, rx) = MetricsCollector::new();
751 collector.update(|m| {
752 m.filter_raw_tokens += 250;
753 m.filter_saved_tokens += 200;
754 m.filter_applications += 1;
755 });
756 collector.update(|m| {
757 m.filter_raw_tokens += 100;
758 m.filter_saved_tokens += 80;
759 m.filter_applications += 1;
760 });
761 let s = rx.borrow();
762 assert_eq!(s.filter_raw_tokens, 350);
763 assert_eq!(s.filter_saved_tokens, 280);
764 assert_eq!(s.filter_applications, 2);
765 }
766
767 #[test]
768 fn filter_confidence_and_command_metrics() {
769 let (collector, rx) = MetricsCollector::new();
770 collector.update(|m| {
771 m.filter_total_commands += 1;
772 m.filter_filtered_commands += 1;
773 m.filter_confidence_full += 1;
774 });
775 collector.update(|m| {
776 m.filter_total_commands += 1;
777 m.filter_confidence_partial += 1;
778 });
779 let s = rx.borrow();
780 assert_eq!(s.filter_total_commands, 2);
781 assert_eq!(s.filter_filtered_commands, 1);
782 assert_eq!(s.filter_confidence_full, 1);
783 assert_eq!(s.filter_confidence_partial, 1);
784 assert_eq!(s.filter_confidence_fallback, 0);
785 }
786
787 #[test]
788 fn summaries_count_tracks_summarizations() {
789 let (collector, rx) = MetricsCollector::new();
790 collector.update(|m| m.summaries_count += 1);
791 collector.update(|m| m.summaries_count += 1);
792 assert_eq!(rx.borrow().summaries_count, 2);
793 }
794
795 #[test]
796 fn cancellations_counter_increments() {
797 let (collector, rx) = MetricsCollector::new();
798 assert_eq!(rx.borrow().cancellations, 0);
799 collector.update(|m| m.cancellations += 1);
800 collector.update(|m| m.cancellations += 1);
801 assert_eq!(rx.borrow().cancellations, 2);
802 }
803
804 #[test]
805 fn security_event_detail_exact_128_not_truncated() {
806 let s = "a".repeat(128);
807 let ev = SecurityEvent::new(SecurityEventCategory::InjectionFlag, "src", s.clone());
808 assert_eq!(ev.detail, s, "128-char string must not be truncated");
809 }
810
811 #[test]
812 fn security_event_detail_129_is_truncated() {
813 let s = "a".repeat(129);
814 let ev = SecurityEvent::new(SecurityEventCategory::InjectionFlag, "src", s);
815 assert!(
816 ev.detail.ends_with('…'),
817 "129-char string must end with ellipsis"
818 );
819 assert!(
820 ev.detail.len() <= 130,
821 "truncated detail must be at most 130 bytes"
822 );
823 }
824
825 #[test]
826 fn security_event_detail_multibyte_utf8_no_panic() {
827 let s = "中".repeat(43);
829 let ev = SecurityEvent::new(SecurityEventCategory::InjectionFlag, "src", s);
830 assert!(ev.detail.ends_with('…'));
831 }
832
833 #[test]
834 fn security_event_source_capped_at_64_chars() {
835 let long_source = "x".repeat(200);
836 let ev = SecurityEvent::new(SecurityEventCategory::InjectionFlag, long_source, "detail");
837 assert_eq!(ev.source.len(), 64);
838 }
839
840 #[test]
841 fn security_event_source_strips_control_chars() {
842 let source = "tool\x00name\x1b[31m";
843 let ev = SecurityEvent::new(SecurityEventCategory::InjectionFlag, source, "detail");
844 assert!(!ev.source.contains('\x00'));
845 assert!(!ev.source.contains('\x1b'));
846 }
847
848 #[test]
849 fn security_event_category_as_str() {
850 assert_eq!(SecurityEventCategory::InjectionFlag.as_str(), "injection");
851 assert_eq!(SecurityEventCategory::ExfiltrationBlock.as_str(), "exfil");
852 assert_eq!(SecurityEventCategory::Quarantine.as_str(), "quarantine");
853 assert_eq!(SecurityEventCategory::Truncation.as_str(), "truncation");
854 assert_eq!(
855 SecurityEventCategory::CrossBoundaryMcpToAcp.as_str(),
856 "cross_boundary_mcp_to_acp"
857 );
858 }
859
860 #[test]
861 fn ring_buffer_respects_cap_via_update() {
862 let (collector, rx) = MetricsCollector::new();
863 for i in 0..110u64 {
864 let event = SecurityEvent::new(
865 SecurityEventCategory::InjectionFlag,
866 "src",
867 format!("event {i}"),
868 );
869 collector.update(|m| {
870 if m.security_events.len() >= SECURITY_EVENT_CAP {
871 m.security_events.pop_front();
872 }
873 m.security_events.push_back(event);
874 });
875 }
876 let snap = rx.borrow();
877 assert_eq!(snap.security_events.len(), SECURITY_EVENT_CAP);
878 assert!(snap.security_events.back().unwrap().detail.contains("109"));
880 }
881
882 #[test]
883 fn security_events_empty_by_default() {
884 let m = MetricsSnapshot::default();
885 assert!(m.security_events.is_empty());
886 }
887
888 #[test]
889 fn orchestration_metrics_default_zero() {
890 let m = OrchestrationMetrics::default();
891 assert_eq!(m.plans_total, 0);
892 assert_eq!(m.tasks_total, 0);
893 assert_eq!(m.tasks_completed, 0);
894 assert_eq!(m.tasks_failed, 0);
895 assert_eq!(m.tasks_skipped, 0);
896 }
897
898 #[test]
899 fn metrics_snapshot_includes_orchestration_default_zero() {
900 let m = MetricsSnapshot::default();
901 assert_eq!(m.orchestration.plans_total, 0);
902 assert_eq!(m.orchestration.tasks_total, 0);
903 assert_eq!(m.orchestration.tasks_completed, 0);
904 }
905
906 #[test]
907 fn orchestration_metrics_update_via_collector() {
908 let (collector, rx) = MetricsCollector::new();
909 collector.update(|m| {
910 m.orchestration.plans_total += 1;
911 m.orchestration.tasks_total += 5;
912 m.orchestration.tasks_completed += 3;
913 m.orchestration.tasks_failed += 1;
914 m.orchestration.tasks_skipped += 1;
915 });
916 let s = rx.borrow();
917 assert_eq!(s.orchestration.plans_total, 1);
918 assert_eq!(s.orchestration.tasks_total, 5);
919 assert_eq!(s.orchestration.tasks_completed, 3);
920 assert_eq!(s.orchestration.tasks_failed, 1);
921 assert_eq!(s.orchestration.tasks_skipped, 1);
922 }
923
924 #[test]
925 fn strip_ctrl_removes_escape_sequences() {
926 let input = "hello\x1b[31mworld\x00end";
927 let result = strip_ctrl(input);
928 assert_eq!(result, "helloworldend");
929 }
930
931 #[test]
932 fn strip_ctrl_allows_tab_lf_cr() {
933 let input = "a\tb\nc\rd";
934 let result = strip_ctrl(input);
935 assert_eq!(result, "a\tb\nc\rd");
936 }
937
938 #[test]
939 fn task_graph_snapshot_is_stale_after_30s() {
940 let mut snap = TaskGraphSnapshot::default();
941 assert!(!snap.is_stale());
943 snap.completed_at = Some(std::time::Instant::now());
945 assert!(!snap.is_stale());
946 snap.completed_at = Some(
948 std::time::Instant::now()
949 .checked_sub(std::time::Duration::from_secs(31))
950 .unwrap(),
951 );
952 assert!(snap.is_stale());
953 }
954
955 #[test]
957 fn task_graph_snapshot_from_task_graph_maps_fields() {
958 use zeph_orchestration::{GraphStatus, TaskGraph, TaskNode, TaskResult, TaskStatus};
959
960 let mut graph = TaskGraph::new("My goal");
961 let mut task = TaskNode::new(0, "Do work", "description");
962 task.status = TaskStatus::Failed;
963 task.assigned_agent = Some("agent-1".into());
964 task.result = Some(TaskResult {
965 output: "error occurred here".into(),
966 artifacts: vec![],
967 duration_ms: 1234,
968 agent_id: None,
969 agent_def: None,
970 });
971 graph.tasks.push(task);
972 graph.status = GraphStatus::Failed;
973
974 let snap = TaskGraphSnapshot::from(&graph);
975 assert_eq!(snap.goal, "My goal");
976 assert_eq!(snap.status, "failed");
977 assert_eq!(snap.tasks.len(), 1);
978 let row = &snap.tasks[0];
979 assert_eq!(row.title, "Do work");
980 assert_eq!(row.status, "failed");
981 assert_eq!(row.agent.as_deref(), Some("agent-1"));
982 assert_eq!(row.duration_ms, 1234);
983 assert!(row.error.as_deref().unwrap().contains("error occurred"));
984 }
985
986 #[test]
988 fn task_graph_snapshot_from_compiles_with_feature() {
989 use zeph_orchestration::TaskGraph;
990 let graph = TaskGraph::new("feature flag test");
991 let snap = TaskGraphSnapshot::from(&graph);
992 assert_eq!(snap.goal, "feature flag test");
993 assert!(snap.tasks.is_empty());
994 assert!(!snap.is_stale());
995 }
996
997 #[test]
999 fn task_graph_snapshot_error_truncated_at_80_chars() {
1000 use zeph_orchestration::{TaskGraph, TaskNode, TaskResult, TaskStatus};
1001
1002 let mut graph = TaskGraph::new("goal");
1003 let mut task = TaskNode::new(0, "t", "d");
1004 task.status = TaskStatus::Failed;
1005 task.result = Some(TaskResult {
1006 output: "e".repeat(100),
1007 artifacts: vec![],
1008 duration_ms: 0,
1009 agent_id: None,
1010 agent_def: None,
1011 });
1012 graph.tasks.push(task);
1013
1014 let snap = TaskGraphSnapshot::from(&graph);
1015 let err = snap.tasks[0].error.as_ref().unwrap();
1016 assert!(err.ends_with('…'), "truncated error must end with ellipsis");
1017 assert!(
1018 err.len() <= 83,
1019 "truncated error must not exceed 80 chars + ellipsis"
1020 );
1021 }
1022
1023 #[test]
1025 fn task_graph_snapshot_strips_control_chars_from_title() {
1026 use zeph_orchestration::{TaskGraph, TaskNode};
1027
1028 let mut graph = TaskGraph::new("goal\x1b[31m");
1029 let task = TaskNode::new(0, "title\x00injected", "d");
1030 graph.tasks.push(task);
1031
1032 let snap = TaskGraphSnapshot::from(&graph);
1033 assert!(!snap.goal.contains('\x1b'), "goal must not contain escape");
1034 assert!(
1035 !snap.tasks[0].title.contains('\x00'),
1036 "title must not contain null byte"
1037 );
1038 }
1039
1040 #[test]
1041 fn graph_metrics_default_zero() {
1042 let m = MetricsSnapshot::default();
1043 assert_eq!(m.graph_entities_total, 0);
1044 assert_eq!(m.graph_edges_total, 0);
1045 assert_eq!(m.graph_communities_total, 0);
1046 assert_eq!(m.graph_extraction_count, 0);
1047 assert_eq!(m.graph_extraction_failures, 0);
1048 }
1049
1050 #[test]
1051 fn graph_metrics_update_via_collector() {
1052 let (collector, rx) = MetricsCollector::new();
1053 collector.update(|m| {
1054 m.graph_entities_total = 5;
1055 m.graph_edges_total = 10;
1056 m.graph_communities_total = 2;
1057 m.graph_extraction_count = 7;
1058 m.graph_extraction_failures = 1;
1059 });
1060 let snapshot = rx.borrow().clone();
1061 assert_eq!(snapshot.graph_entities_total, 5);
1062 assert_eq!(snapshot.graph_edges_total, 10);
1063 assert_eq!(snapshot.graph_communities_total, 2);
1064 assert_eq!(snapshot.graph_extraction_count, 7);
1065 assert_eq!(snapshot.graph_extraction_failures, 1);
1066 }
1067
1068 #[test]
1069 fn histogram_recorder_trait_is_object_safe() {
1070 use std::sync::Arc;
1071 use std::time::Duration;
1072
1073 struct NoOpRecorder;
1074 impl HistogramRecorder for NoOpRecorder {
1075 fn observe_llm_latency(&self, _: Duration) {}
1076 fn observe_turn_duration(&self, _: Duration) {}
1077 fn observe_tool_execution(&self, _: Duration) {}
1078 fn observe_bg_task(&self, _: &str, _: Duration) {}
1079 }
1080
1081 let recorder: Arc<dyn HistogramRecorder> = Arc::new(NoOpRecorder);
1083 recorder.observe_llm_latency(Duration::from_millis(500));
1084 recorder.observe_turn_duration(Duration::from_secs(3));
1085 recorder.observe_tool_execution(Duration::from_millis(100));
1086 }
1087}