Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aegis-runtime-mv/blob/main/tests/failure_provenance_tests.rs # aegis-runtime-mv Native Rust agent runtime: Axum WebSocket/SSE/REST gateway, heartbeat scheduler, dependency-aware task workflow, SQLite storage. Attachment of AienOS. Pre-alpha. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aegis-runtime-mv / tests / failure_provenance_tests.rs` 650 lines ยท 21805 bytes Raw Blame History 1 //! Failure provenance and effect-safe retry tests (issue #38). 2 //! Deterministic: scripted model turns, scripted tool dispatcher, no network. 3 4 use aegis::agent::FailurePolicy; 5 use aegis::{ 6 root_cause, AegisEvent, AgentEngine, CancelToken, ChatStream, ChatTurnResponse, 7 EffectCertainty, EventBus, FailureClass, FailureRecord, InferenceEngine, RetryPolicy, 8 SkillRegistry, ToolCallFunction, ToolCallItem, ToolDispatcher, ToolFault, 9 }; 10 use async_trait::async_trait; 11 use std::collections::{HashMap, VecDeque}; 12 use std::sync::atomic::{AtomicUsize, Ordering}; 13 use std::sync::{Arc, Mutex}; 14 use std::time::Duration; 15 16 const SECRET: &str = "sk-live-SEEDED-SECRET-9f3a77"; 17 18 struct ScriptedModel { 19 turns: Mutex>, 20 asked: AtomicUsize, 21 seen: Mutex>, 22 } 23 24 impl ScriptedModel { 25 fn new(turns: Vec) -> Arc { 26 Arc::new(Self { 27 turns: Mutex::new(turns.into()), 28 asked: AtomicUsize::new(0), 29 seen: Mutex::new(Vec::new()), 30 }) 31 } 32 } 33 34 fn calls(items: &[(&str, &str, serde_json::Value)]) -> ChatTurnResponse { 35 ChatTurnResponse { 36 content: None, 37 reasoning: None, 38 tool_calls: Some( 39 items 40 .iter() 41 .map(|(id, name, args)| ToolCallItem { 42 id: id.to_string(), 43 call_type: "function".to_string(), 44 function: ToolCallFunction { 45 name: name.to_string(), 46 arguments: args.to_string(), 47 }, 48 }) 49 .collect(), 50 ), 51 finish_reason: Some("tool_calls".to_string()), 52 } 53 } 54 55 fn done() -> ChatTurnResponse { 56 ChatTurnResponse { 57 content: Some("finished".to_string()), 58 reasoning: None, 59 tool_calls: None, 60 finish_reason: Some("stop".to_string()), 61 } 62 } 63 64 #[async_trait] 65 impl InferenceEngine for ScriptedModel { 66 fn model_id(&self) -> String { 67 "scripted".into() 68 } 69 fn endpoint(&self) -> String { 70 "none".into() 71 } 72 async fn generate_with_tokens( 73 &self, 74 _p: &str, 75 _s: Option<&str>, 76 _t: Option, 77 _m: Option, 78 ) -> Result> { 79 Ok(String::new()) 80 } 81 async fn generate_chat( 82 &self, 83 _m: &[serde_json::Value], 84 _t: Option, 85 _x: Option, 86 ) -> Result> { 87 Ok(String::new()) 88 } 89 async fn generate_chat_with_tools( 90 &self, 91 m: &[serde_json::Value], 92 _tools: Option<&[serde_json::Value]>, 93 _t: Option, 94 _x: Option, 95 ) -> Result> { 96 self.asked.fetch_add(1, Ordering::SeqCst); 97 self.seen 98 .lock() 99 .unwrap() 100 .push(serde_json::to_string(m).unwrap()); 101 Ok(self.turns.lock().unwrap().pop_front().unwrap_or_else(done)) 102 } 103 async fn stream_chat( 104 &self, 105 _m: &[serde_json::Value], 106 _t: Option, 107 _x: Option, 108 ) -> Result> { 109 Err("not used".into()) 110 } 111 async fn check_health(&self) -> bool { 112 true 113 } 114 } 115 116 #[derive(Clone)] 117 enum Behavior { 118 Ok, 119 Fault(ToolFault), 120 Hang, 121 } 122 123 /// Scripted tool path: per tool name, a queue of behaviours; empty queue = ok. 124 struct Script { 125 queues: Mutex>>, 126 dispatches: AtomicUsize, 127 } 128 129 impl Script { 130 fn new(entries: Vec<(&str, Vec)>) -> Arc { 131 Arc::new(Self { 132 queues: Mutex::new( 133 entries 134 .into_iter() 135 .map(|(k, v)| (k.to_string(), v.into())) 136 .collect(), 137 ), 138 dispatches: AtomicUsize::new(0), 139 }) 140 } 141 fn count(&self) -> usize { 142 self.dispatches.load(Ordering::SeqCst) 143 } 144 } 145 146 #[async_trait] 147 impl ToolDispatcher for Script { 148 async fn dispatch( 149 &self, 150 skill_name: &str, 151 _arguments: &serde_json::Value, 152 ) -> Result { 153 self.dispatches.fetch_add(1, Ordering::SeqCst); 154 let next = self 155 .queues 156 .lock() 157 .unwrap() 158 .get_mut(skill_name) 159 .and_then(|q| q.pop_front()); 160 match next { 161 None | Some(Behavior::Ok) => Ok("ok".to_string()), 162 Some(Behavior::Fault(f)) => Err(f), 163 Some(Behavior::Hang) => { 164 tokio::time::sleep(Duration::from_secs(30)).await; 165 Ok("late".to_string()) 166 } 167 } 168 } 169 } 170 171 fn engine(model: Arc, script: Arc