roder-dynamic-workflows 0.1.3

Agentic software development tools and SDKs for Roder.
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
mod executor;
mod plan;
mod script_stack;
mod state;
mod task;

use std::collections::{BTreeMap, VecDeque};
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};

use roder_api::dynamic_workflows::{
    WorkflowApproval, WorkflowCheckpointRecorded, WorkflowRun, WorkflowRunId, WorkflowRunResumed,
    WorkflowRunStatus, WorkflowScript,
};
use roder_api::events::{RoderEvent, ThreadId, TurnId};
use roder_api::subagents::{SubagentExitReason, SubagentRequest, SubagentResult};
use time::OffsetDateTime;
use tokio::sync::{Mutex, Notify, broadcast};
use tokio::task::{JoinHandle, JoinSet};

pub use self::task::WorkflowTaskExecutor;
use crate::host_api::{WorkflowAgentLaunch, WorkflowExecution};
use crate::model::{WorkflowRunInput, WorkflowRuntimeOptions};
use crate::runner::WorkflowScriptRuntime;
use crate::store::{WorkflowCachedAgentResult, WorkflowCheckpointStore};

pub use self::executor::{SubagentDispatcherWorkflowExecutor, WorkflowAgentExecutor};
use self::plan::{
    PlannedAgent, execution_context, mark_restarted_agent_completed, phases_for_execution,
    plan_agents, render_final_report,
};
use self::script_stack::run_script_on_dedicated_stack;
use self::state::{
    mark_agent_completed, mark_agent_error, mark_agent_failed, mark_agent_started,
    mark_phase_completed, mark_phase_started, mark_run_completed, mark_run_paused,
    mark_run_started, mark_run_stopped,
};

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct WorkflowRunRequest {
    pub run_id: WorkflowRunId,
    pub thread_id: Option<ThreadId>,
    pub turn_id: Option<TurnId>,
    pub script: WorkflowScript,
    pub arguments: serde_json::Value,
    pub start_paused: bool,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub approval: Option<WorkflowApproval>,
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct WorkflowRunSnapshot {
    pub run: WorkflowRun,
    pub report: Option<String>,
    pub reused_agent_results: u32,
}

#[derive(Debug, Clone)]
pub struct WorkflowAgentExecutionContext {
    pub run_id: WorkflowRunId,
    pub phase_id: String,
    pub agent_id: String,
    pub thread_id: Option<ThreadId>,
    pub turn_id: Option<TurnId>,
    pub stopped: Arc<AtomicBool>,
}

#[derive(Debug, Clone)]
pub struct WorkflowAgentExecutionRequest {
    pub launch: WorkflowAgentLaunch,
    pub subagent_request: SubagentRequest,
    pub cache_key: crate::store::WorkflowAgentCacheKey,
}

#[derive(Clone)]
pub struct WorkflowRunner {
    executor: Arc<dyn WorkflowAgentExecutor>,
    script_runtime: WorkflowScriptRuntime,
    store: WorkflowCheckpointStore,
    events: broadcast::Sender<RoderEvent>,
}

impl WorkflowRunner {
    pub fn new(
        executor: Arc<dyn WorkflowAgentExecutor>,
        store: WorkflowCheckpointStore,
        options: WorkflowRuntimeOptions,
    ) -> Self {
        let (events, _) = broadcast::channel(1024);
        Self {
            executor,
            script_runtime: WorkflowScriptRuntime::new(options),
            store,
            events,
        }
    }

    pub fn subscribe(&self) -> broadcast::Receiver<RoderEvent> {
        self.events.subscribe()
    }

    pub async fn start(&self, request: WorkflowRunRequest) -> anyhow::Result<WorkflowRunHandle> {
        let source = request
            .script
            .body
            .as_deref()
            .ok_or_else(|| anyhow::anyhow!("workflow script body is required to run"))?;
        let checkpoints = self.store.read_checkpoints(&request.run_id)?;
        let mut input = WorkflowRunInput::new(request.run_id.clone());
        input.arguments = request.arguments.clone();
        input.checkpoints = checkpoints;
        let execution =
            run_script_on_dedicated_stack(self.script_runtime.clone(), source.to_string(), input)
                .await?;
        let planned = plan_agents(
            &request.run_id,
            &request.thread_id,
            &request.turn_id,
            &execution,
        );
        let state = Arc::new(Mutex::new(WorkflowRunSnapshot {
            run: initial_run(&request, &execution, &planned),
            report: None,
            reused_agent_results: 0,
        }));
        let control = Arc::new(WorkflowRunControl::new(request.start_paused));
        let planned = Arc::new(planned);
        let join = spawn_run(
            self.clone(),
            state.clone(),
            control.clone(),
            planned.clone(),
            execution,
        );

        Ok(WorkflowRunHandle {
            run_id: request.run_id,
            state,
            control,
            planned,
            executor: self.executor.clone(),
            store: self.store.clone(),
            events: self.events.clone(),
            join: Arc::new(Mutex::new(Some(join))),
        })
    }

    async fn run_to_completion(
        self,
        state: Arc<Mutex<WorkflowRunSnapshot>>,
        control: Arc<WorkflowRunControl>,
        planned: Arc<BTreeMap<String, PlannedAgent>>,
        execution: WorkflowExecution,
    ) -> anyhow::Result<WorkflowRunSnapshot> {
        mark_run_started(&self.events, &state).await;
        if control.paused.load(Ordering::SeqCst) {
            mark_run_paused(&self.events, &state, Some("started paused".to_string())).await;
        }
        self.persist_script_checkpoints(&state, &execution).await?;

        let phases = { state.lock().await.run.phases.clone() };
        let mut ordered_results = Vec::new();
        for phase in phases {
            if self.wait_if_paused_or_stopped(&state, &control).await? {
                return Ok(state.lock().await.clone());
            }
            mark_phase_started(&self.events, &state, &phase.phase_id).await;
            ordered_results.extend(
                self.run_phase_agents(&state, &control, &planned, &phase.phase_id)
                    .await?,
            );
            mark_phase_completed(&self.events, &state, &phase.phase_id).await;
        }

        let report = render_final_report(&execution.report, &ordered_results);
        mark_run_completed(&self.events, &state, report).await;
        Ok(state.lock().await.clone())
    }

    async fn run_phase_agents(
        &self,
        state: &Arc<Mutex<WorkflowRunSnapshot>>,
        control: &Arc<WorkflowRunControl>,
        planned: &BTreeMap<String, PlannedAgent>,
        phase_id: &str,
    ) -> anyhow::Result<Vec<(String, SubagentResult)>> {
        let max_concurrent = state.lock().await.run.limits.max_concurrent_agents.max(1) as usize;
        let mut queue = planned
            .values()
            .filter(|agent| agent.phase_id == phase_id)
            .cloned()
            .collect::<VecDeque<_>>();
        let mut active = JoinSet::new();
        let mut results = Vec::new();

        loop {
            while active.len() < max_concurrent && !queue.is_empty() {
                if self.wait_if_paused_or_stopped(state, control).await? {
                    active.abort_all();
                    return Ok(results);
                }
                let next = queue.pop_front().expect("queue is not empty");
                let run_id = { state.lock().await.run.run_id.clone() };
                if let Some(cached) = self.store.find_agent_result(&run_id, &next.cache_key)? {
                    mark_agent_completed(
                        &self.events,
                        state,
                        &next.agent_id,
                        cached.result.clone(),
                        true,
                    )
                    .await;
                    results.push((next.agent_id, cached.result));
                    continue;
                }
                mark_agent_started(&self.events, state, &next.agent_id).await;
                active.spawn(execute_planned_agent(
                    self.executor.clone(),
                    state.clone(),
                    control.stopped.clone(),
                    next,
                ));
            }

            if active.is_empty() {
                break;
            }
            let (agent_id, result) = active.join_next().await.expect("active task exists")?;
            self.record_agent_result(state, planned, &agent_id, result, &mut results)
                .await?;
            if control.stopped.load(Ordering::SeqCst) {
                active.abort_all();
                mark_run_stopped(&self.events, state, Some("stopped".to_string())).await;
                return Ok(results);
            }
        }

        Ok(results)
    }

    async fn record_agent_result(
        &self,
        state: &Arc<Mutex<WorkflowRunSnapshot>>,
        planned: &BTreeMap<String, PlannedAgent>,
        agent_id: &str,
        result: anyhow::Result<SubagentResult>,
        results: &mut Vec<(String, SubagentResult)>,
    ) -> anyhow::Result<()> {
        match result {
            Ok(result) if result.exit_reason == SubagentExitReason::Completed => {
                if let Some(planned) = planned.get(agent_id) {
                    let run_id = { state.lock().await.run.run_id.clone() };
                    self.store.append_agent_result(
                        &run_id,
                        &WorkflowCachedAgentResult {
                            key: planned.cache_key.clone(),
                            result: result.clone(),
                            completed_at: OffsetDateTime::now_utc(),
                        },
                    )?;
                }
                mark_agent_completed(&self.events, state, agent_id, result.clone(), false).await;
                results.push((agent_id.to_string(), result));
            }
            Ok(result) => {
                mark_agent_failed(
                    &self.events,
                    state,
                    agent_id,
                    result.clone(),
                    format!("subagent exited with {:?}", result.exit_reason),
                )
                .await;
                results.push((agent_id.to_string(), result));
            }
            Err(error) => {
                mark_agent_error(&self.events, state, agent_id, error.to_string()).await;
            }
        }
        Ok(())
    }

    async fn persist_script_checkpoints(
        &self,
        state: &Arc<Mutex<WorkflowRunSnapshot>>,
        execution: &WorkflowExecution,
    ) -> anyhow::Result<()> {
        for checkpoint in &execution.checkpoints {
            let run = state.lock().await.run.clone();
            self.store.append_checkpoint(&run.run_id, checkpoint)?;
            let _ = self.events.send(RoderEvent::WorkflowCheckpointRecorded(
                WorkflowCheckpointRecorded {
                    run_id: run.run_id,
                    thread_id: run.thread_id,
                    turn_id: run.turn_id,
                    phase_id: None,
                    key: checkpoint.key.clone(),
                    byte_count: checkpoint.byte_count,
                    timestamp: OffsetDateTime::now_utc(),
                },
            ));
        }
        Ok(())
    }

    async fn wait_if_paused_or_stopped(
        &self,
        state: &Arc<Mutex<WorkflowRunSnapshot>>,
        control: &Arc<WorkflowRunControl>,
    ) -> anyhow::Result<bool> {
        while control.paused.load(Ordering::SeqCst) {
            if control.stopped.load(Ordering::SeqCst) {
                mark_run_stopped(&self.events, state, Some("stopped".to_string())).await;
                return Ok(true);
            }
            control.notify.notified().await;
        }
        if control.stopped.load(Ordering::SeqCst) {
            mark_run_stopped(&self.events, state, Some("stopped".to_string())).await;
            return Ok(true);
        }
        Ok(false)
    }
}

pub struct WorkflowRunHandle {
    run_id: WorkflowRunId,
    state: Arc<Mutex<WorkflowRunSnapshot>>,
    control: Arc<WorkflowRunControl>,
    planned: Arc<BTreeMap<String, PlannedAgent>>,
    executor: Arc<dyn WorkflowAgentExecutor>,
    store: WorkflowCheckpointStore,
    events: broadcast::Sender<RoderEvent>,
    join: Arc<Mutex<Option<JoinHandle<anyhow::Result<WorkflowRunSnapshot>>>>>,
}

impl WorkflowRunHandle {
    pub fn run_id(&self) -> &str {
        &self.run_id
    }

    pub async fn snapshot(&self) -> WorkflowRunSnapshot {
        self.state.lock().await.clone()
    }

    pub async fn wait(&self) -> anyhow::Result<WorkflowRunSnapshot> {
        let join = self.join.lock().await.take();
        if let Some(join) = join {
            return join.await?;
        }
        Ok(self.snapshot().await)
    }

    pub async fn pause(&self, reason: Option<String>) {
        self.control.paused.store(true, Ordering::SeqCst);
        mark_run_paused(&self.events, &self.state, reason).await;
    }

    pub async fn resume(&self) {
        self.control.paused.store(false, Ordering::SeqCst);
        self.control.notify.notify_waiters();
        let mut snapshot = self.state.lock().await;
        snapshot.run.status = WorkflowRunStatus::Running;
        let run = snapshot.run.clone();
        drop(snapshot);
        let _ = self
            .events
            .send(RoderEvent::WorkflowRunResumed(WorkflowRunResumed {
                run_id: run.run_id,
                thread_id: run.thread_id,
                turn_id: run.turn_id,
                status: WorkflowRunStatus::Running,
                timestamp: OffsetDateTime::now_utc(),
            }));
    }

    pub async fn stop(&self, reason: Option<String>) {
        self.control.stopped.store(true, Ordering::SeqCst);
        self.control.paused.store(false, Ordering::SeqCst);
        self.control.notify.notify_waiters();
        mark_run_stopped(&self.events, &self.state, reason).await;
    }

    pub async fn restart_agent(&self, agent_id: &str) -> anyhow::Result<bool> {
        let Some(planned) = self.planned.get(agent_id).cloned() else {
            return Ok(false);
        };
        self.store
            .invalidate_agent_results(&self.run_id, agent_id)?;
        let context = execution_context(&self.state, &planned, self.control.stopped.clone()).await;
        let result = self
            .executor
            .execute_agent(
                context,
                WorkflowAgentExecutionRequest {
                    launch: planned.launch.clone(),
                    subagent_request: planned.request.clone(),
                    cache_key: planned.cache_key.clone(),
                },
            )
            .await?;
        self.store.append_agent_result(
            &self.run_id,
            &WorkflowCachedAgentResult {
                key: planned.cache_key,
                result: result.clone(),
                completed_at: OffsetDateTime::now_utc(),
            },
        )?;
        mark_restarted_agent_completed(&self.state, agent_id, result).await;
        Ok(true)
    }
}

struct WorkflowRunControl {
    paused: AtomicBool,
    stopped: Arc<AtomicBool>,
    notify: Notify,
}

impl WorkflowRunControl {
    fn new(paused: bool) -> Self {
        Self {
            paused: AtomicBool::new(paused),
            stopped: Arc::new(AtomicBool::new(false)),
            notify: Notify::new(),
        }
    }
}

fn spawn_run(
    runner: WorkflowRunner,
    state: Arc<Mutex<WorkflowRunSnapshot>>,
    control: Arc<WorkflowRunControl>,
    planned: Arc<BTreeMap<String, PlannedAgent>>,
    execution: WorkflowExecution,
) -> JoinHandle<anyhow::Result<WorkflowRunSnapshot>> {
    tokio::spawn(async move {
        runner
            .run_to_completion(state, control, planned, execution)
            .await
    })
}

async fn execute_planned_agent(
    executor: Arc<dyn WorkflowAgentExecutor>,
    state: Arc<Mutex<WorkflowRunSnapshot>>,
    stopped: Arc<AtomicBool>,
    planned: PlannedAgent,
) -> (String, anyhow::Result<SubagentResult>) {
    let context = execution_context(&state, &planned, stopped).await;
    let request = WorkflowAgentExecutionRequest {
        launch: planned.launch,
        subagent_request: planned.request,
        cache_key: planned.cache_key,
    };
    let agent_id = context.agent_id.clone();
    (agent_id, executor.execute_agent(context, request).await)
}

fn initial_run(
    request: &WorkflowRunRequest,
    execution: &WorkflowExecution,
    planned: &BTreeMap<String, PlannedAgent>,
) -> WorkflowRun {
    let now = OffsetDateTime::now_utc();
    WorkflowRun {
        run_id: request.run_id.clone(),
        thread_id: request.thread_id.clone(),
        turn_id: request.turn_id.clone(),
        script: request.script.clone(),
        status: WorkflowRunStatus::Queued,
        limits: execution.definition.limits.clone(),
        phases: phases_for_execution(execution),
        agents: planned
            .values()
            .map(|planned| planned.agent.clone())
            .collect(),
        approval: request.approval.clone(),
        cost_estimate: None,
        summary: None,
        error: None,
        created_at: now,
        updated_at: now,
        started_at: None,
        completed_at: None,
    }
}