nexo-taskflow 0.1.2

Long-running multi-step task orchestration runtime for Nexo agents.
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
use std::time::Duration;

#[cfg(test)]
use std::sync::Arc;

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use uuid::Uuid;

use crate::manager::FlowManager;
use crate::types::{Flow, FlowError, FlowStatus};

/// Typed view of `Flow.wait_json`. The engine evaluates this at every tick
/// to decide whether a `Waiting` flow can be resumed.
///
/// Host code (e.g. NATS bridge) is intentionally not coupled here — for
/// `ExternalEvent` the host calls `WaitEngine::try_resume_external` when a
/// matching message arrives.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum WaitCondition {
    /// Resume when wall-clock time reaches `at`.
    Timer { at: DateTime<Utc> },
    /// Resume when an external event for `(topic, correlation_id)` arrives.
    ExternalEvent {
        topic: String,
        correlation_id: String,
    },
    /// Only resumed by an explicit `manager.resume(...)` call.
    Manual,
}

impl WaitCondition {
    pub fn into_value(self) -> Value {
        serde_json::to_value(self).expect("WaitCondition is always serializable")
    }

    pub fn from_value(v: &Value) -> Option<Self> {
        serde_json::from_value(v.clone()).ok()
    }
}

/// Drives `Waiting` flows back into `Running` (or `Cancelled`) based on
/// their `WaitCondition`.
///
/// The engine is intentionally minimal — no broker, no internal scheduler.
/// `tick()` is pull-based and idempotent; the host (or Phase 7 heartbeat)
/// invokes it on whatever cadence is appropriate. `run(interval, shutdown)`
/// is provided as a convenience for the common case.
#[derive(Clone)]
pub struct WaitEngine {
    manager: FlowManager,
}

#[derive(Debug, Default, Clone)]
pub struct TickReport {
    pub scanned: usize,
    pub resumed: usize,
    pub cancelled: usize,
    pub still_waiting: usize,
    pub errors: usize,
}

impl WaitEngine {
    pub fn new(manager: FlowManager) -> Self {
        Self { manager }
    }

    pub fn manager(&self) -> &FlowManager {
        &self.manager
    }

    /// Single pass over all `Waiting` flows. Returns counters for telemetry.
    /// `now` is injected for deterministic testing — callers in production
    /// should pass `Utc::now()`.
    pub async fn tick_at(&self, now: DateTime<Utc>) -> TickReport {
        let mut report = TickReport::default();
        let waiting = match self.manager.list_by_status(FlowStatus::Waiting).await {
            Ok(v) => v,
            Err(e) => {
                tracing::warn!(error = %e, "wait engine: failed to list waiting flows");
                report.errors += 1;
                return report;
            }
        };
        report.scanned = waiting.len();

        for flow in waiting {
            match self.evaluate(&flow, now).await {
                Outcome::Resume => match self.manager.resume(flow.id, None).await {
                    Ok(_) => report.resumed += 1,
                    Err(FlowError::CancelPending { .. }) => {
                        // Flow was cancelled between list and resume.
                        match self.manager.cancel(flow.id).await {
                            Ok(_) => report.cancelled += 1,
                            Err(e) => {
                                tracing::warn!(flow_id = %flow.id, error = %e, "wait engine: cancel after CancelPending failed");
                                report.errors += 1;
                            }
                        }
                    }
                    Err(e) => {
                        tracing::warn!(flow_id = %flow.id, error = %e, "wait engine: resume failed");
                        report.errors += 1;
                    }
                },
                Outcome::Cancel => match self.manager.cancel(flow.id).await {
                    Ok(_) => report.cancelled += 1,
                    Err(e) => {
                        tracing::warn!(flow_id = %flow.id, error = %e, "wait engine: cancel failed");
                        report.errors += 1;
                    }
                },
                Outcome::Wait => {
                    report.still_waiting += 1;
                }
                Outcome::Skip(reason) => {
                    tracing::debug!(flow_id = %flow.id, reason, "wait engine: skipping flow");
                }
            }
        }
        report
    }

    pub async fn tick(&self) -> TickReport {
        self.tick_at(Utc::now()).await
    }

    /// Host-driven resume for `ExternalEvent` waits. Invoked when (e.g.) a
    /// NATS subscriber receives a message and resolves it to a flow.
    /// Returns:
    /// - `Ok(Some(flow))` when the flow matched and was resumed
    /// - `Ok(None)` when no flow matched (no-op for caller)
    /// - `Err(_)` for store/manager errors that the caller should log
    pub async fn try_resume_external(
        &self,
        flow_id: Uuid,
        topic: &str,
        correlation_id: &str,
        payload: Option<Value>,
    ) -> Result<Option<Flow>, FlowError> {
        let Some(flow) = self.manager.get(flow_id).await? else {
            return Ok(None);
        };
        if flow.status != FlowStatus::Waiting {
            return Ok(None);
        }
        let cond = match flow.wait_json.as_ref().and_then(WaitCondition::from_value) {
            Some(c) => c,
            None => return Ok(None),
        };
        let matches = matches!(
            &cond,
            WaitCondition::ExternalEvent { topic: t, correlation_id: c }
                if t == topic && c == correlation_id
        );
        if !matches {
            return Ok(None);
        }
        let patch = payload.map(|p| json!({ "resume_event": p }));
        self.manager.resume(flow.id, patch).await.map(Some)
    }

    /// Long-running tick loop. Stops cleanly when `shutdown.cancelled()`
    /// fires. Logs each tick at debug level.
    pub async fn run(&self, interval: Duration, shutdown: tokio_util::sync::CancellationToken) {
        let mut interval_timer = tokio::time::interval(interval);
        interval_timer.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay);
        loop {
            tokio::select! {
                _ = shutdown.cancelled() => {
                    tracing::info!("wait engine: shutdown requested");
                    return;
                }
                _ = interval_timer.tick() => {
                    let report = self.tick().await;
                    if report.scanned > 0 {
                        tracing::debug!(
                            scanned = report.scanned,
                            resumed = report.resumed,
                            cancelled = report.cancelled,
                            still_waiting = report.still_waiting,
                            errors = report.errors,
                            "wait engine tick"
                        );
                    }
                }
            }
        }
    }

    async fn evaluate(&self, flow: &Flow, now: DateTime<Utc>) -> Outcome {
        // Sticky cancel takes priority over wait condition.
        if flow.cancel_requested {
            return Outcome::Cancel;
        }
        let Some(wait_value) = flow.wait_json.as_ref() else {
            return Outcome::Skip("missing wait_json");
        };
        let Some(cond) = WaitCondition::from_value(wait_value) else {
            return Outcome::Skip("unparseable wait_json");
        };
        match cond {
            WaitCondition::Timer { at } => {
                if now >= at {
                    Outcome::Resume
                } else {
                    Outcome::Wait
                }
            }
            // External and Manual conditions are not advanced by ticks —
            // they need an explicit signal (`try_resume_external` or `manager.resume`).
            WaitCondition::ExternalEvent { .. } | WaitCondition::Manual => Outcome::Wait,
        }
    }
}

enum Outcome {
    Resume,
    Cancel,
    Wait,
    Skip(&'static str),
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::manager::CreateManagedInput;
    use crate::store::SqliteFlowStore;
    use chrono::Duration as ChronoDuration;

    async fn engine() -> WaitEngine {
        let store = Arc::new(SqliteFlowStore::open(":memory:").await.unwrap());
        WaitEngine::new(FlowManager::new(store))
    }

    fn input() -> CreateManagedInput {
        CreateManagedInput {
            controller_id: "test".into(),
            goal: "test".into(),
            owner_session_key: "owner".into(),
            requester_origin: "user".into(),
            current_step: "init".into(),
            state_json: json!({}),
        }
    }

    async fn put_into_waiting(eng: &WaitEngine, cond: WaitCondition) -> Flow {
        let m = eng.manager();
        let f = m.create_managed(input()).await.unwrap();
        let f = m.start_running(f.id).await.unwrap();
        m.set_waiting(f.id, cond.into_value()).await.unwrap()
    }

    #[tokio::test]
    async fn timer_fires_when_now_past_deadline() {
        let eng = engine().await;
        let past = Utc::now() - ChronoDuration::seconds(60);
        let f = put_into_waiting(&eng, WaitCondition::Timer { at: past }).await;

        let report = eng.tick().await;
        assert_eq!(report.scanned, 1);
        assert_eq!(report.resumed, 1);

        let after = eng.manager().get(f.id).await.unwrap().unwrap();
        assert_eq!(after.status, FlowStatus::Running);
        assert!(after.wait_json.is_none());
    }

    #[tokio::test]
    async fn timer_does_not_fire_before_deadline() {
        let eng = engine().await;
        let future = Utc::now() + ChronoDuration::seconds(60);
        let f = put_into_waiting(&eng, WaitCondition::Timer { at: future }).await;

        let report = eng.tick().await;
        assert_eq!(report.scanned, 1);
        assert_eq!(report.resumed, 0);
        assert_eq!(report.still_waiting, 1);

        let after = eng.manager().get(f.id).await.unwrap().unwrap();
        assert_eq!(after.status, FlowStatus::Waiting);
    }

    #[tokio::test]
    async fn external_event_matches_resumes() {
        let eng = engine().await;
        let f = put_into_waiting(
            &eng,
            WaitCondition::ExternalEvent {
                topic: "agent.delegate.reply".into(),
                correlation_id: "corr-42".into(),
            },
        )
        .await;

        // Tick alone does not resume external waits.
        let report = eng.tick().await;
        assert_eq!(report.resumed, 0);
        assert_eq!(report.still_waiting, 1);

        let resumed = eng
            .try_resume_external(
                f.id,
                "agent.delegate.reply",
                "corr-42",
                Some(json!({"answer": 42})),
            )
            .await
            .unwrap()
            .expect("resumed");
        assert_eq!(resumed.status, FlowStatus::Running);
        assert!(resumed.wait_json.is_none());
        assert_eq!(resumed.state_json["resume_event"]["answer"], 42);
    }

    #[tokio::test]
    async fn external_event_with_wrong_topic_or_id_is_noop() {
        let eng = engine().await;
        let f = put_into_waiting(
            &eng,
            WaitCondition::ExternalEvent {
                topic: "topic-A".into(),
                correlation_id: "id-1".into(),
            },
        )
        .await;

        // Wrong topic.
        let r1 = eng
            .try_resume_external(f.id, "topic-B", "id-1", None)
            .await
            .unwrap();
        assert!(r1.is_none());

        // Right topic, wrong id.
        let r2 = eng
            .try_resume_external(f.id, "topic-A", "id-99", None)
            .await
            .unwrap();
        assert!(r2.is_none());

        let after = eng.manager().get(f.id).await.unwrap().unwrap();
        assert_eq!(after.status, FlowStatus::Waiting);
    }

    #[tokio::test]
    async fn manual_wait_ignored_by_tick() {
        let eng = engine().await;
        let f = put_into_waiting(&eng, WaitCondition::Manual).await;
        let report = eng.tick().await;
        assert_eq!(report.scanned, 1);
        assert_eq!(report.resumed, 0);
        assert_eq!(report.still_waiting, 1);
        let after = eng.manager().get(f.id).await.unwrap().unwrap();
        assert_eq!(after.status, FlowStatus::Waiting);
    }

    #[tokio::test]
    async fn cancel_requested_waiting_flips_to_cancelled_on_tick() {
        let eng = engine().await;
        let future = Utc::now() + ChronoDuration::seconds(60);
        let f = put_into_waiting(&eng, WaitCondition::Timer { at: future }).await;
        eng.manager().request_cancel(f.id).await.unwrap();

        let report = eng.tick().await;
        assert_eq!(report.cancelled, 1);
        assert_eq!(report.resumed, 0);

        let after = eng.manager().get(f.id).await.unwrap().unwrap();
        assert_eq!(after.status, FlowStatus::Cancelled);
    }

    #[tokio::test]
    async fn run_loop_can_be_shut_down() {
        let eng = engine().await;
        let token = tokio_util::sync::CancellationToken::new();
        let token_clone = token.clone();
        let eng_clone = eng.clone();
        let handle = tokio::spawn(async move {
            eng_clone.run(Duration::from_millis(20), token_clone).await;
        });
        // Let it tick a few times, then cancel.
        tokio::time::sleep(Duration::from_millis(60)).await;
        token.cancel();
        // Should exit quickly.
        let r = tokio::time::timeout(Duration::from_millis(200), handle).await;
        assert!(r.is_ok(), "engine did not shut down promptly");
    }

    #[tokio::test]
    async fn try_resume_external_on_unknown_flow_is_noop() {
        let eng = engine().await;
        let r = eng
            .try_resume_external(Uuid::new_v4(), "t", "c", None)
            .await
            .unwrap();
        assert!(r.is_none());
    }

    #[tokio::test]
    async fn try_resume_external_on_running_flow_is_noop() {
        let eng = engine().await;
        let m = eng.manager();
        let f = m.create_managed(input()).await.unwrap();
        let f = m.start_running(f.id).await.unwrap();
        let r = eng.try_resume_external(f.id, "t", "c", None).await.unwrap();
        assert!(r.is_none(), "should ignore non-waiting flows");
    }

    #[test]
    fn wait_condition_round_trip() {
        let original = WaitCondition::Timer { at: Utc::now() };
        let v = original.clone().into_value();
        let parsed = WaitCondition::from_value(&v).expect("round trip");
        match parsed {
            WaitCondition::Timer { .. } => {}
            other => panic!("wrong variant: {other:?}"),
        }
    }
}