stakpak-server 0.3.62

Stakpak: Your DevOps AI Agent. Generate infrastructure code, debug Kubernetes, configure CI/CD, automate deployments, without giving an LLM the keys to production.
Documentation
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
use crate::error::SessionManagerError;
use crate::types::{SessionHandle, SessionRuntimeState};
use stakpak_agent_core::AgentCommand;
use std::{collections::HashMap, future::Future, sync::Arc};
use tokio::sync::RwLock;
use uuid::Uuid;

/// In-memory runtime run coordinator (not persistent session storage).
#[derive(Clone, Default)]
pub struct SessionManager {
    states: Arc<RwLock<HashMap<Uuid, SessionRuntimeState>>>,
}

impl SessionManager {
    pub fn new() -> Self {
        Self::default()
    }

    pub async fn state(&self, session_id: Uuid) -> SessionRuntimeState {
        let guard = self.states.read().await;
        guard
            .get(&session_id)
            .cloned()
            .unwrap_or(SessionRuntimeState::Idle)
    }

    pub async fn active_run_id(&self, session_id: Uuid) -> Option<Uuid> {
        self.state(session_id).await.run_id()
    }

    pub async fn running_runs(&self) -> Vec<(Uuid, Uuid)> {
        let guard = self.states.read().await;
        guard
            .iter()
            .filter_map(|(session_id, state)| match state {
                SessionRuntimeState::Running { run_id, .. } => Some((*session_id, *run_id)),
                SessionRuntimeState::Idle
                | SessionRuntimeState::Starting { .. }
                | SessionRuntimeState::Failed { .. } => None,
            })
            .collect()
    }

    pub async fn start_run<F, Fut>(
        &self,
        session_id: Uuid,
        spawn_actor: F,
    ) -> Result<Uuid, SessionManagerError>
    where
        F: FnOnce(Uuid) -> Fut,
        Fut: Future<Output = Result<SessionHandle, String>>,
    {
        let run_id = {
            let mut guard = self.states.write().await;
            match guard.get(&session_id) {
                Some(SessionRuntimeState::Starting { .. })
                | Some(SessionRuntimeState::Running { .. }) => {
                    return Err(SessionManagerError::SessionAlreadyRunning);
                }
                _ => {}
            }

            let run_id = Uuid::new_v4();
            guard.insert(session_id, SessionRuntimeState::Starting { run_id });
            run_id
        };

        match spawn_actor(run_id).await {
            Ok(handle) => {
                let mut guard = self.states.write().await;
                if matches!(
                    guard.get(&session_id),
                    Some(SessionRuntimeState::Starting { run_id: active_run_id })
                        if *active_run_id == run_id
                ) {
                    guard.insert(session_id, SessionRuntimeState::Running { run_id, handle });
                    Ok(run_id)
                } else {
                    let error = "session state changed before actor startup completed".to_string();
                    guard.insert(
                        session_id,
                        SessionRuntimeState::Failed {
                            last_error: error.clone(),
                        },
                    );
                    Err(SessionManagerError::ActorStartupFailed(error))
                }
            }
            Err(error) => {
                let mut guard = self.states.write().await;
                guard.insert(
                    session_id,
                    SessionRuntimeState::Failed {
                        last_error: error.clone(),
                    },
                );
                Err(SessionManagerError::ActorStartupFailed(error))
            }
        }
    }

    pub async fn mark_run_finished(
        &self,
        session_id: Uuid,
        run_id: Uuid,
        outcome: Result<(), String>,
    ) -> Result<(), SessionManagerError> {
        let mut guard = self.states.write().await;

        match guard.get(&session_id) {
            Some(SessionRuntimeState::Starting {
                run_id: active_run_id,
            })
            | Some(SessionRuntimeState::Running {
                run_id: active_run_id,
                ..
            }) => {
                if *active_run_id != run_id {
                    return Err(SessionManagerError::RunMismatch {
                        active_run_id: *active_run_id,
                        requested_run_id: run_id,
                    });
                }
            }
            Some(SessionRuntimeState::Idle) | None | Some(SessionRuntimeState::Failed { .. }) => {
                return Err(SessionManagerError::SessionNotRunning);
            }
        }

        match outcome {
            Ok(()) => {
                guard.insert(session_id, SessionRuntimeState::Idle);
            }
            Err(error) => {
                guard.insert(
                    session_id,
                    SessionRuntimeState::Failed { last_error: error },
                );
            }
        }

        Ok(())
    }

    pub async fn send_command(
        &self,
        session_id: Uuid,
        run_id: Uuid,
        command: AgentCommand,
    ) -> Result<(), SessionManagerError> {
        let command_tx = {
            let guard = self.states.read().await;
            match guard.get(&session_id) {
                Some(SessionRuntimeState::Running {
                    run_id: active_run_id,
                    handle,
                }) => {
                    if *active_run_id != run_id {
                        return Err(SessionManagerError::RunMismatch {
                            active_run_id: *active_run_id,
                            requested_run_id: run_id,
                        });
                    }
                    handle.command_tx.clone()
                }
                Some(SessionRuntimeState::Starting { .. }) => {
                    return Err(SessionManagerError::SessionStarting);
                }
                Some(SessionRuntimeState::Idle)
                | None
                | Some(SessionRuntimeState::Failed { .. }) => {
                    return Err(SessionManagerError::SessionNotRunning);
                }
            }
        };

        command_tx
            .send(command)
            .await
            .map_err(|_| SessionManagerError::CommandChannelClosed)
    }

    pub async fn cancel_run(
        &self,
        session_id: Uuid,
        run_id: Uuid,
    ) -> Result<(), SessionManagerError> {
        let cancel_token = {
            let guard = self.states.read().await;
            match guard.get(&session_id) {
                Some(SessionRuntimeState::Running {
                    run_id: active_run_id,
                    handle,
                }) => {
                    if *active_run_id != run_id {
                        return Err(SessionManagerError::RunMismatch {
                            active_run_id: *active_run_id,
                            requested_run_id: run_id,
                        });
                    }
                    handle.cancel.clone()
                }
                Some(SessionRuntimeState::Starting { .. }) => {
                    return Err(SessionManagerError::SessionStarting);
                }
                Some(SessionRuntimeState::Idle)
                | None
                | Some(SessionRuntimeState::Failed { .. }) => {
                    return Err(SessionManagerError::SessionNotRunning);
                }
            }
        };

        cancel_token.cancel();
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use stakpak_agent_core::AgentCommand;
    use std::sync::Arc;
    use tokio::{sync::Barrier, sync::mpsc, time::Duration};
    use tokio_util::sync::CancellationToken;

    fn make_handle() -> (SessionHandle, mpsc::Receiver<AgentCommand>) {
        let (command_tx, command_rx) = mpsc::channel(8);
        (
            SessionHandle::new(command_tx, CancellationToken::new()),
            command_rx,
        )
    }

    #[tokio::test]
    async fn start_run_is_atomic_under_concurrency() {
        let manager = Arc::new(SessionManager::new());
        let session_id = Uuid::new_v4();
        let barrier = Arc::new(Barrier::new(2));

        let mut tasks = Vec::new();
        for _ in 0..2 {
            let manager_clone = manager.clone();
            let barrier_clone = barrier.clone();
            let session = session_id;
            tasks.push(tokio::spawn(async move {
                barrier_clone.wait().await;
                manager_clone
                    .start_run(session, |_run_id| async {
                        tokio::time::sleep(Duration::from_millis(10)).await;
                        let (handle, _rx) = make_handle();
                        Ok(handle)
                    })
                    .await
            }));
        }

        let mut successes = 0usize;
        let mut conflicts = 0usize;

        for task in tasks {
            match task.await {
                Ok(Ok(_)) => successes += 1,
                Ok(Err(SessionManagerError::SessionAlreadyRunning)) => conflicts += 1,
                Ok(Err(other)) => panic!("unexpected error: {other}"),
                Err(join_error) => panic!("join error: {join_error}"),
            }
        }

        assert_eq!(successes, 1);
        assert_eq!(conflicts, 1);
    }

    #[tokio::test]
    async fn run_scoped_command_rejects_stale_run_id() {
        let manager = SessionManager::new();
        let session_id = Uuid::new_v4();

        let (handle, _rx) = make_handle();
        let run_id = match manager
            .start_run(
                session_id,
                move |_allocated_run_id| async move { Ok(handle) },
            )
            .await
        {
            Ok(run_id) => run_id,
            Err(error) => panic!("start_run should succeed: {error}"),
        };

        let wrong_run_id = Uuid::new_v4();
        let result = manager
            .send_command(session_id, wrong_run_id, AgentCommand::Cancel)
            .await;

        assert_eq!(
            result,
            Err(SessionManagerError::RunMismatch {
                active_run_id: run_id,
                requested_run_id: wrong_run_id,
            })
        );
    }

    #[tokio::test]
    async fn run_scoped_command_accepts_active_run_id() {
        let manager = SessionManager::new();
        let session_id = Uuid::new_v4();

        let (handle, mut rx) = make_handle();
        let run_id = match manager
            .start_run(
                session_id,
                move |_allocated_run_id| async move { Ok(handle) },
            )
            .await
        {
            Ok(run_id) => run_id,
            Err(error) => panic!("start_run should succeed: {error}"),
        };

        let send_result = manager
            .send_command(session_id, run_id, AgentCommand::Cancel)
            .await;
        assert!(send_result.is_ok());

        let received = tokio::time::timeout(Duration::from_millis(50), rx.recv()).await;
        match received {
            Ok(Some(AgentCommand::Cancel)) => {}
            Ok(Some(_other)) => panic!("unexpected command variant"),
            Ok(None) => panic!("command channel closed unexpectedly"),
            Err(timeout_error) => panic!("did not receive command in time: {timeout_error}"),
        }
    }

    #[tokio::test]
    async fn running_runs_lists_only_running_sessions() {
        let manager = SessionManager::new();
        let running_session_id = Uuid::new_v4();
        let finished_session_id = Uuid::new_v4();

        let (running_handle, _running_rx) = make_handle();
        let running_run_id = match manager
            .start_run(running_session_id, move |_allocated_run_id| async move {
                Ok(running_handle)
            })
            .await
        {
            Ok(run_id) => run_id,
            Err(error) => panic!("start_run should succeed: {error}"),
        };

        let (finished_handle, _finished_rx) = make_handle();
        let finished_run_id = match manager
            .start_run(finished_session_id, move |_allocated_run_id| async move {
                Ok(finished_handle)
            })
            .await
        {
            Ok(run_id) => run_id,
            Err(error) => panic!("start_run should succeed: {error}"),
        };

        let mark_finished = manager
            .mark_run_finished(finished_session_id, finished_run_id, Ok(()))
            .await;
        assert!(mark_finished.is_ok());

        let running_runs = manager.running_runs().await;
        assert_eq!(running_runs.len(), 1);
        assert_eq!(running_runs[0], (running_session_id, running_run_id));
    }

    #[tokio::test]
    async fn startup_failure_transitions_to_failed_state() {
        let manager = SessionManager::new();
        let session_id = Uuid::new_v4();

        let result = manager
            .start_run(session_id, |_run_id| async move { Err("boom".to_string()) })
            .await;

        assert_eq!(
            result,
            Err(SessionManagerError::ActorStartupFailed("boom".to_string()))
        );

        let state = manager.state(session_id).await;
        match state {
            SessionRuntimeState::Failed { last_error } => {
                assert_eq!(last_error, "boom".to_string());
            }
            other => panic!("expected failed state, got: {other:?}"),
        }
    }

    #[tokio::test]
    async fn mark_run_finished_requires_active_run_match() {
        let manager = SessionManager::new();
        let session_id = Uuid::new_v4();

        let (handle, _rx) = make_handle();
        let run_id = match manager
            .start_run(
                session_id,
                move |_allocated_run_id| async move { Ok(handle) },
            )
            .await
        {
            Ok(run_id) => run_id,
            Err(error) => panic!("start_run should succeed: {error}"),
        };

        let wrong_run_id = Uuid::new_v4();
        let mismatch = manager
            .mark_run_finished(session_id, wrong_run_id, Ok(()))
            .await;

        assert_eq!(
            mismatch,
            Err(SessionManagerError::RunMismatch {
                active_run_id: run_id,
                requested_run_id: wrong_run_id,
            })
        );

        let finish = manager.mark_run_finished(session_id, run_id, Ok(())).await;
        assert!(finish.is_ok());

        let state = manager.state(session_id).await;
        assert!(matches!(state, SessionRuntimeState::Idle));
    }

    #[tokio::test]
    async fn cancel_run_requires_active_run_match_and_cancels_token() {
        let manager = SessionManager::new();
        let session_id = Uuid::new_v4();

        let (handle, _rx) = make_handle();
        let cancel = handle.cancel.clone();
        let run_id = match manager
            .start_run(
                session_id,
                move |_allocated_run_id| async move { Ok(handle) },
            )
            .await
        {
            Ok(run_id) => run_id,
            Err(error) => panic!("start_run should succeed: {error}"),
        };

        let cancel_result = manager.cancel_run(session_id, run_id).await;
        assert!(cancel_result.is_ok());
        assert!(cancel.is_cancelled());
    }
}