awaken-server 0.2.1

Multi-protocol HTTP server with SSE, mailbox, and protocol adapters for Awaken
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
//! Framework-level run control operations.
//!
//! This service centralizes the semantics used by HTTP routes and protocol
//! adapters for active-run lookup, cancellation, interrupt, HITL decisions, and
//! user input injection.

use serde::{Deserialize, Serialize};
use thiserror::Error;

use awaken_contract::contract::lifecycle::RunStatus;
use awaken_contract::contract::mailbox::MailboxInterrupt;
use awaken_contract::contract::message::Message;
use awaken_contract::contract::storage::{RunQuery, RunRecord, RunWaitingState, StorageError};
use awaken_contract::contract::suspension::ToolCallResume;
use awaken_runtime::RunRequest;

use crate::app::AppState;
use crate::mailbox::{MailboxError, MailboxSubmitResult};

/// How injected user input should interact with any active work.
#[derive(Debug, Clone, Copy, Default, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum InputMode {
    /// Queue the new input behind the current active run.
    #[default]
    Queue,
    /// Interrupt the active run for the thread, then queue the new input.
    InterruptThenQueue,
    /// Append input to the current open waiting run and continue the same run ID.
    ResumeOpenRun,
}

/// Thread interrupt policy.
#[derive(Debug, Clone, Copy, Default, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum InterruptMode {
    /// Request cooperative cancellation and supersede queued mailbox dispatches.
    #[default]
    Graceful,
}

/// A run that is still controllable.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ActiveRun {
    pub thread_id: String,
    pub run_id: String,
    pub agent_id: String,
    pub status: RunStatus,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub termination_reason: Option<awaken_contract::contract::lifecycle::TerminationReason>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub dispatch_id: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub session_id: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub waiting: Option<RunWaitingState>,
}

impl From<RunRecord> for ActiveRun {
    fn from(run: RunRecord) -> Self {
        Self {
            thread_id: run.thread_id,
            run_id: run.run_id,
            agent_id: run.agent_id,
            status: run.status,
            termination_reason: run.termination_reason,
            dispatch_id: run.dispatch_id,
            session_id: run.session_id,
            waiting: run.waiting,
        }
    }
}

/// Errors raised by framework run-control operations.
#[derive(Debug, Error)]
pub enum RunControlError {
    #[error("thread not found: {0}")]
    ThreadNotFound(String),
    #[error("run not found: {0}")]
    RunNotFound(String),
    #[error("decision target not found: {0}")]
    DecisionTargetNotFound(String),
    #[error("storage error: {0}")]
    Store(#[from] StorageError),
    #[error("mailbox error: {0}")]
    Mailbox(#[from] MailboxError),
}

/// Unified control plane for active runs.
#[derive(Clone)]
pub struct RunControlService {
    state: AppState,
}

impl RunControlService {
    pub fn new(state: AppState) -> Self {
        Self { state }
    }

    /// Return the most recent running or waiting run for a thread.
    pub async fn get_active_run(
        &self,
        thread_id: &str,
    ) -> Result<Option<ActiveRun>, RunControlError> {
        if let Some(thread) = self.state.store.load_thread(thread_id).await? {
            if let Some(active) = self
                .load_projected_run(
                    thread_id,
                    thread.active_run_id.as_deref(),
                    &[RunStatus::Running],
                )
                .await?
            {
                return Ok(Some(active));
            }

            if let Some(open) = self
                .load_projected_run(
                    thread_id,
                    thread.open_run_id.as_deref(),
                    &[RunStatus::Running, RunStatus::Waiting],
                )
                .await?
            {
                return Ok(Some(open));
            }
        }

        self.scan_active_run(thread_id).await
    }

    async fn load_projected_run(
        &self,
        thread_id: &str,
        run_id: Option<&str>,
        allowed_statuses: &[RunStatus],
    ) -> Result<Option<ActiveRun>, RunControlError> {
        let Some(run_id) = run_id else {
            return Ok(None);
        };
        let Some(run) = self.state.store.load_run(run_id).await? else {
            return Ok(None);
        };
        if run.thread_id == thread_id && allowed_statuses.contains(&run.status) {
            Ok(Some(ActiveRun::from(run)))
        } else {
            Ok(None)
        }
    }

    async fn scan_active_run(&self, thread_id: &str) -> Result<Option<ActiveRun>, RunControlError> {
        let mut candidates = Vec::new();
        for status in [RunStatus::Running, RunStatus::Waiting] {
            let page = self
                .state
                .store
                .list_runs(&RunQuery {
                    offset: 0,
                    limit: 200,
                    thread_id: Some(thread_id.to_string()),
                    status: Some(status),
                })
                .await?;
            candidates.extend(page.items);
        }

        Ok(candidates
            .into_iter()
            .max_by_key(|run| run.updated_at)
            .map(ActiveRun::from))
    }

    /// Submit a tool-call decision to a waiting active run.
    ///
    /// Remote live delivery is at-least-once when its ack is lost before the
    /// durable fallback is enqueued; `(tool_call_id, decision_id)` identifies
    /// duplicate decisions.
    pub async fn decide(
        &self,
        id: &str,
        tool_call_id: String,
        resume: ToolCallResume,
    ) -> Result<(), RunControlError> {
        if self
            .state
            .mailbox
            .send_decision_live(id, tool_call_id.clone(), resume.clone())
            .await?
        {
            Ok(())
        } else {
            self.enqueue_durable_decision(id, tool_call_id, resume)
                .await
        }
    }

    async fn enqueue_durable_decision(
        &self,
        id: &str,
        tool_call_id: String,
        resume: ToolCallResume,
    ) -> Result<(), RunControlError> {
        let run = if let Some(run) = self.state.store.load_run(id).await? {
            run
        } else if let Some(active) = self.get_active_run(id).await? {
            self.state
                .store
                .load_run(&active.run_id)
                .await?
                .ok_or_else(|| RunControlError::RunNotFound(active.run_id.clone()))?
        } else {
            return Err(RunControlError::DecisionTargetNotFound(id.to_string()));
        };

        if run.status != RunStatus::Waiting
            || !run.is_resumable_waiting()
            || !waiting_contains_ticket(&run, &tool_call_id)
        {
            return Err(RunControlError::DecisionTargetNotFound(id.to_string()));
        }

        let request = RunRequest::new(run.thread_id.clone(), Vec::new())
            .with_agent_id(run.agent_id.clone())
            .with_continue_run_id(run.run_id.clone())
            .with_decisions(vec![(tool_call_id, resume)]);
        self.state.mailbox.submit_background(request).await?;
        Ok(())
    }

    /// Cancel an active run or queued mailbox dispatch by run ID, dispatch ID, or thread ID.
    pub async fn cancel_run(&self, id: &str) -> Result<(), RunControlError> {
        if self.state.mailbox.cancel(id).await? {
            Ok(())
        } else {
            Err(RunControlError::RunNotFound(id.to_string()))
        }
    }

    /// Interrupt a thread, superseding queued work and cancelling the active run.
    pub async fn interrupt_thread(
        &self,
        thread_id: &str,
        _mode: InterruptMode,
    ) -> Result<MailboxInterrupt, RunControlError> {
        let interrupted = self.state.mailbox.interrupt(thread_id).await?;
        if interrupted.active_dispatch.is_some() || interrupted.superseded_count > 0 {
            Ok(interrupted)
        } else {
            Err(RunControlError::ThreadNotFound(thread_id.to_string()))
        }
    }

    /// Inject messages into a thread, optionally interrupting the active run first.
    pub async fn inject_user_input(
        &self,
        thread_id: &str,
        agent_id: Option<String>,
        messages: Vec<Message>,
        mode: InputMode,
    ) -> Result<MailboxSubmitResult, RunControlError> {
        let thread = self
            .state
            .store
            .load_thread(thread_id)
            .await?
            .ok_or_else(|| RunControlError::ThreadNotFound(thread_id.to_string()))?;

        if mode == InputMode::InterruptThenQueue {
            let _ = self
                .state
                .mailbox
                .interrupt(thread_id)
                .await
                .map_err(RunControlError::Mailbox)?;
        }

        let mut request = RunRequest::new(thread_id.to_string(), messages);
        if mode == InputMode::ResumeOpenRun {
            let run = self
                .load_open_waiting_run(thread_id, thread.open_run_id.as_deref())
                .await?;
            request = request
                .with_agent_id(run.agent_id)
                .with_continue_run_id(run.run_id);
        } else if let Some(agent_id) = agent_id {
            request = request.with_agent_id(agent_id);
        }

        self.state
            .mailbox
            .submit_background(request)
            .await
            .map_err(RunControlError::Mailbox)
    }

    /// Inject messages into the active run when possible, otherwise queue them.
    pub async fn inject_user_input_live_then_queue(
        &self,
        thread_id: &str,
        agent_id: Option<String>,
        messages: Vec<Message>,
    ) -> Result<MailboxSubmitResult, RunControlError> {
        let _thread = self
            .state
            .store
            .load_thread(thread_id)
            .await?
            .ok_or_else(|| RunControlError::ThreadNotFound(thread_id.to_string()))?;

        let mut request = RunRequest::new(thread_id.to_string(), messages);
        if let Some(agent_id) = agent_id {
            request = request.with_agent_id(agent_id);
        }

        self.state
            .mailbox
            .submit_live_then_queue(request, None)
            .await
            .map_err(RunControlError::Mailbox)
    }

    /// Inject messages using an existing run as the thread and agent anchor.
    pub async fn inject_run_input(
        &self,
        run_id: &str,
        messages: Vec<Message>,
        mode: InputMode,
    ) -> Result<MailboxSubmitResult, RunControlError> {
        let run = self
            .state
            .store
            .load_run(run_id)
            .await?
            .ok_or_else(|| RunControlError::RunNotFound(run_id.to_string()))?;

        if mode == InputMode::InterruptThenQueue {
            return self
                .inject_user_input(&run.thread_id, Some(run.agent_id), messages, mode)
                .await;
        }

        if run.status == RunStatus::Waiting && run.is_resumable_waiting() {
            let request = RunRequest::new(run.thread_id.clone(), messages)
                .with_agent_id(run.agent_id)
                .with_continue_run_id(run.run_id);
            return self
                .state
                .mailbox
                .submit_background(request)
                .await
                .map_err(RunControlError::Mailbox);
        }

        self.inject_user_input(&run.thread_id, Some(run.agent_id), messages, mode)
            .await
    }

    /// Inject messages using an existing run as the live-delivery anchor.
    pub async fn inject_run_input_live_then_queue(
        &self,
        run_id: &str,
        messages: Vec<Message>,
    ) -> Result<MailboxSubmitResult, RunControlError> {
        let run = self
            .state
            .store
            .load_run(run_id)
            .await?
            .ok_or_else(|| RunControlError::RunNotFound(run_id.to_string()))?;

        let request =
            RunRequest::new(run.thread_id.clone(), messages).with_agent_id(run.agent_id.clone());
        self.state
            .mailbox
            .submit_live_then_queue(request, Some(&run.run_id))
            .await
            .map_err(RunControlError::Mailbox)
    }

    async fn load_open_waiting_run(
        &self,
        thread_id: &str,
        open_run_id: Option<&str>,
    ) -> Result<RunRecord, RunControlError> {
        let Some(open_run_id) = open_run_id else {
            return Err(RunControlError::RunNotFound(format!(
                "open run for thread {thread_id}"
            )));
        };
        let run = self
            .state
            .store
            .load_run(open_run_id)
            .await?
            .ok_or_else(|| RunControlError::RunNotFound(open_run_id.to_string()))?;
        if run.thread_id != thread_id
            || run.status != RunStatus::Waiting
            || !run.is_resumable_waiting()
        {
            return Err(RunControlError::RunNotFound(open_run_id.to_string()));
        }
        Ok(run)
    }
}

fn waiting_contains_ticket(run: &RunRecord, target: &str) -> bool {
    let Some(waiting) = run.waiting.as_ref() else {
        return false;
    };
    waiting.ticket_ids.iter().any(|id| id == target)
        || waiting
            .tickets
            .iter()
            .any(|ticket| ticket.ticket_id == target || ticket.tool_call_id == target)
}