interplane.Get Omega
Read in your language

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

branch main Branches Tags History

aegis-runtime-mv / src / persistence / sqlite / runs.rs

261 lines · 11986 bytes Raw Blame History
1use crate::orchestration::budget::RunBudget;
2use crate::orchestration::run::{Run, RunState};
3use crate::orchestration::termination::TerminationReason;
4use crate::orchestration::trigger::Trigger;
5use crate::persistence::async_db::AsyncDatabase;
6use crate::persistence::error::PersistenceError;
7use crate::sessions::id::{RunId, SessionId};
8use crate::sessions::store::RunStore;
9use async_trait::async_trait;
10use rusqlite::params;
11
12#[derive(Clone)]
13pub struct SqliteRunStore {
14 db: AsyncDatabase,
15}
16
17impl SqliteRunStore {
18 pub fn new(db: AsyncDatabase) -> Self {
19 Self { db }
20 }
21}
22
23#[async_trait]
24impl RunStore for SqliteRunStore {
25 async fn create(&self, run: &Run) -> Result<(), PersistenceError> {
26 let run = run.clone();
27 self.db
28 .interact(move |conn| {
29 let active_count: i64 = conn.query_row(
30 "SELECT COUNT(*) FROM runs WHERE session_id = ?1 AND state IN ('running', 'waiting_for_approval', 'waiting_for_tool')",
31 params![run.session_id.as_str()],
32 |r| r.get(0),
33 )?;
34 if active_count > 0 {
35 let active_id: String = conn.query_row(
36 "SELECT id FROM runs WHERE session_id = ?1 AND state IN ('running', 'waiting_for_approval', 'waiting_for_tool') LIMIT 1",
37 params![run.session_id.as_str()],
38 |r| r.get(0),
39 )?;
40 return Err(PersistenceError::ActiveRunConflict {
41 session_id: run.session_id.to_string(),
42 active_run_id: active_id,
43 });
44 }
45
46 let trigger_str = serde_json::to_string(&run.trigger)?;
47 let budget_str = serde_json::to_string(&run.budget)?;
48 let term_str = run.termination.as_ref().map(serde_json::to_string).transpose()?;
49
50 conn.execute(
51 "INSERT INTO runs (id, session_id, state, trigger_json, steps_used, inference_calls_used, actions_used, budget_json, started_at, completed_at, termination_json)
52 VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11)",
53 params![
54 run.id.as_str(),
55 run.session_id.as_str(),
56 run.state.as_str(),
57 trigger_str,
58 run.steps_used as i64,
59 run.inference_calls_used as i64,
60 run.actions_used as i64,
61 budget_str,
62 run.started_at,
63 run.completed_at,
64 term_str,
65 ],
66 )?;
67 Ok(())
68 })
69 .await
70 }
71
72 async fn get(&self, id: &RunId) -> Result<Option<Run>, PersistenceError> {
73 let id_str = id.to_string();
74 self.db
75 .interact(move |conn| {
76 let mut stmt = conn.prepare(
77 "SELECT id, session_id, state, trigger_json, steps_used, inference_calls_used, actions_used, budget_json, started_at, completed_at, termination_json
78 FROM runs WHERE id = ?1",
79 )?;
80 let mut rows = stmt.query(params![id_str])?;
81 if let Some(row) = rows.next()? {
82 let id_val: String = row.get(0)?;
83 let session_val: String = row.get(1)?;
84 let state_str: String = row.get(2)?;
85 let trigger_json: String = row.get(3)?;
86 let steps_used: i64 = row.get(4)?;
87 let inference_calls_used: i64 = row.get(5)?;
88 let actions_used: i64 = row.get(6)?;
89 let budget_json: String = row.get(7)?;
90 let started_at: i64 = row.get(8)?;
91 let completed_at: Option<i64> = row.get(9)?;
92 let term_json: Option<String> = row.get(10)?;
93
94 let state = RunState::from_str(&state_str)
95 .ok_or_else(|| PersistenceError::Serialization(format!("Invalid state: {state_str}")))?;
96 let trigger: Trigger = serde_json::from_str(&trigger_json)?;
97 let budget: RunBudget = serde_json::from_str(&budget_json)?;
98 let termination: Option<TerminationReason> = term_json
99 .map(|s| serde_json::from_str(&s))
100 .transpose()?;
101
102 Ok(Some(Run {
103 id: RunId::from_string(id_val),
104 session_id: SessionId::from_string(session_val),
105 state,
106 trigger,
107 steps_used: steps_used as u32,
108 inference_calls_used: inference_calls_used as u32,
109 actions_used: actions_used as u32,
110 budget,
111 started_at,
112 completed_at,
113 termination,
114 }))
115 } else {
116 Ok(None)
117 }
118 })
119 .await
120 }
121
122 async fn update(&self, run: &Run) -> Result<(), PersistenceError> {
123 let run = run.clone();
124 self.db
125 .interact(move |conn| {
126 let budget_str = serde_json::to_string(&run.budget)?;
127 let term_str = run
128 .termination
129 .as_ref()
130 .map(serde_json::to_string)
131 .transpose()?;
132
133 let rows = conn.execute(
134 "UPDATE runs SET state = ?1, steps_used = ?2, inference_calls_used = ?3,
135 actions_used = ?4, budget_json = ?5, completed_at = ?6, termination_json = ?7
136 WHERE id = ?8",
137 params![
138 run.state.as_str(),
139 run.steps_used as i64,
140 run.inference_calls_used as i64,
141 run.actions_used as i64,
142 budget_str,
143 run.completed_at,
144 term_str,
145 run.id.as_str(),
146 ],
147 )?;
148 if rows == 0 {
149 return Err(PersistenceError::NotFound(run.id.to_string()));
150 }
151 Ok(())
152 })
153 .await
154 }
155
156 async fn list_by_session(&self, session_id: &SessionId) -> Result<Vec<Run>, PersistenceError> {
157 let session_id = session_id.clone();
158 self.db
159 .interact(move |conn| {
160 let mut stmt = conn.prepare(
161 "SELECT id, session_id, state, trigger_json, steps_used, inference_calls_used, actions_used, budget_json, started_at, completed_at, termination_json
162 FROM runs WHERE session_id = ?1 ORDER BY started_at DESC",
163 )?;
164 let rows = stmt.query_map(params![session_id.as_str()], |row| {
165 let id_val: String = row.get(0)?;
166 let session_val: String = row.get(1)?;
167 let state_str: String = row.get(2)?;
168 let trigger_json: String = row.get(3)?;
169 let steps_used: i64 = row.get(4)?;
170 let inference_calls_used: i64 = row.get(5)?;
171 let actions_used: i64 = row.get(6)?;
172 let budget_json: String = row.get(7)?;
173 let started_at: i64 = row.get(8)?;
174 let completed_at: Option<i64> = row.get(9)?;
175 let term_json: Option<String> = row.get(10)?;
176 Ok((id_val, session_val, state_str, trigger_json, steps_used, inference_calls_used, actions_used, budget_json, started_at, completed_at, term_json))
177 })?;
178
179 let mut runs = Vec::new();
180 for r in rows {
181 let (id_val, session_val, state_str, trigger_json, steps_used, inference_calls_used, actions_used, budget_json, started_at, completed_at, term_json) = r?;
182 let state = RunState::from_str(&state_str)
183 .ok_or_else(|| PersistenceError::Serialization(format!("Invalid state: {state_str}")))?;
184 let trigger: Trigger = serde_json::from_str(&trigger_json)?;
185 let budget: RunBudget = serde_json::from_str(&budget_json)?;
186 let termination: Option<TerminationReason> = term_json
187 .map(|s| serde_json::from_str(&s))
188 .transpose()?;
189
190 runs.push(Run {
191 id: RunId::from_string(id_val),
192 session_id: SessionId::from_string(session_val),
193 state,
194 trigger,
195 steps_used: steps_used as u32,
196 inference_calls_used: inference_calls_used as u32,
197 actions_used: actions_used as u32,
198 budget,
199 started_at,
200 completed_at,
201 termination,
202 });
203 }
204 Ok(runs)
205 })
206 .await
207 }
208
209 async fn get_active_run(
210 &self,
211 session_id: &SessionId,
212 ) -> Result<Option<Run>, PersistenceError> {
213 let session_id = session_id.clone();
214 self.db
215 .interact(move |conn| {
216 let mut stmt = conn.prepare(
217 "SELECT id, session_id, state, trigger_json, steps_used, inference_calls_used, actions_used, budget_json, started_at, completed_at, termination_json
218 FROM runs WHERE session_id = ?1 AND state IN ('running', 'waiting_for_approval', 'waiting_for_tool') LIMIT 1",
219 )?;
220 let mut rows = stmt.query(params![session_id.as_str()])?;
221 if let Some(row) = rows.next()? {
222 let id_val: String = row.get(0)?;
223 let session_val: String = row.get(1)?;
224 let state_str: String = row.get(2)?;
225 let trigger_json: String = row.get(3)?;
226 let steps_used: i64 = row.get(4)?;
227 let inference_calls_used: i64 = row.get(5)?;
228 let actions_used: i64 = row.get(6)?;
229 let budget_json: String = row.get(7)?;
230 let started_at: i64 = row.get(8)?;
231 let completed_at: Option<i64> = row.get(9)?;
232 let term_json: Option<String> = row.get(10)?;
233
234 let state = RunState::from_str(&state_str)
235 .ok_or_else(|| PersistenceError::Serialization(format!("Invalid state: {state_str}")))?;
236 let trigger: Trigger = serde_json::from_str(&trigger_json)?;
237 let budget: RunBudget = serde_json::from_str(&budget_json)?;
238 let termination: Option<TerminationReason> = term_json
239 .map(|s| serde_json::from_str(&s))
240 .transpose()?;
241
242 Ok(Some(Run {
243 id: RunId::from_string(id_val),
244 session_id: SessionId::from_string(session_val),
245 state,
246 trigger,
247 steps_used: steps_used as u32,
248 inference_calls_used: inference_calls_used as u32,
249 actions_used: actions_used as u32,
250 budget,
251 started_at,
252 completed_at,
253 termination,
254 }))
255 } else {
256 Ok(None)
257 }
258 })
259 .await
260 }
261}