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
//! Scheduling / run-lifecycle methods of [`DagEngine`]: node dispatch
//! (`tick`/`start_node`), launcher callbacks (`on_node_completed` /
//! `on_node_update`), post-terminal propagation, run completion, persisted
//! restore, and the event-driven wait loop.
use std::panic::{AssertUnwindSafe, catch_unwind};
use std::time::Duration;
use futures::future::join_all;
use tokio_util::sync::CancellationToken;
use super::engine::{DagEngine, NodeOutcome};
use super::engine_state::{cap_chars, emit_state, panic_message};
use super::model::{is_terminal, now_ms};
use super::persist::{PersistedRun, hydrate, max_run_counter};
use super::types::{DagEvent, DagStatus, NodeResult, NodeStatus};
impl DagEngine {
// ── scheduling ──────────────────────────────────────────────────────────
/// Launch every eligible ready node within the concurrency budget.
pub(super) fn tick(&self, run_id: &str) {
loop {
let next = {
let inner = self.inner.lock();
let Some(run) = inner.runs.get(run_id) else {
return;
};
if run.status != DagStatus::Running {
return;
}
let running = run
.nodes
.iter()
.filter(|n| n.status == NodeStatus::Running)
.count();
if running >= run.max_concurrency {
return;
}
match run.nodes.iter().find(|n| n.status == NodeStatus::Ready) {
Some(n) => Some(n.id.clone()),
None => return,
}
};
match next {
Some(id) => self.start_node(run_id, &id),
None => return,
}
}
}
fn start_node(&self, run_id: &str, node_id: &str) {
let (launcher, token) = {
let mut inner = self.inner.lock();
let Some(run) = inner.runs.get_mut(run_id) else {
return;
};
let Some(node) = run.node_mut(node_id) else {
return;
};
// Concurrent ticks can both pick the same ready node; only the
// first wins (the TS original is single-threaded).
if node.status != NodeStatus::Ready {
return;
}
node.status = NodeStatus::Running;
node.launch_gen += 1;
node.started_at = Some(now_ms());
node.job_id = Some(format!("job-{}-{}", run_id, node_id));
let token = CancellationToken::new();
emit_state(run);
let session_id = run.session_id.clone();
// MutexGuard does not split field borrows: touch `jobs` only
// after the `run` borrow has ended (NLL).
inner
.jobs
.insert((run_id.to_string(), node_id.to_string()), token.clone());
let launcher = inner
.session_launchers
.get(&session_id)
.cloned()
.or_else(|| inner.launcher.clone());
(launcher, token)
};
self.begin_node_observation(run_id, node_id);
match launcher {
None => {
// No launch context (tests / misconfiguration): fail now.
self.on_node_completed(
run_id,
node_id,
NodeOutcome {
success: false,
error: Some("no launch context".to_string()),
duration_ms: 0,
attempt: 0,
total_attempts: 0,
input_tokens: 0,
output_tokens: 0,
output: None,
},
);
}
Some(launcher) => {
let result =
catch_unwind(AssertUnwindSafe(|| launcher.launch(run_id, node_id, token)));
if let Err(panic) = result {
self.on_node_completed(
run_id,
node_id,
NodeOutcome {
success: false,
error: Some(panic_message(&panic)),
duration_ms: 0,
attempt: 0,
total_attempts: 0,
input_tokens: 0,
output_tokens: 0,
output: None,
},
);
}
}
}
}
/// Launcher callback: a node's subagent job ended with `outcome`.
pub fn on_node_completed(&self, run_id: &str, node_id: &str, outcome: NodeOutcome) {
let applied = {
let mut inner = self.inner.lock();
let Some(run) = inner.runs.get_mut(run_id) else {
return;
};
let Some(node) = run.node_mut(node_id) else {
return;
};
// Stale report (node was cancelled/skipped/retried meanwhile).
if node.status != NodeStatus::Running {
return;
}
node.completed_at = Some(now_ms());
if outcome.success {
node.status = NodeStatus::Succeeded;
node.error = None;
} else {
node.status = NodeStatus::Failed;
node.error = Some(
outcome
.error
.clone()
.unwrap_or_else(|| "no result".to_string()),
);
}
node.result = Some(NodeResult {
success: outcome.success,
error: outcome.error.clone(),
duration_ms: Some(outcome.duration_ms),
attempt: outcome.attempt,
total_attempts: outcome.total_attempts,
});
node.attempt = node.attempt.max(outcome.total_attempts);
node.input_tokens = Some(outcome.input_tokens);
node.output_tokens = Some(outcome.output_tokens);
if outcome.output.is_some() {
node.output = outcome.output;
}
let status = node.status.clone();
let error = node.error.clone();
emit_state(run);
let event = DagEvent::NodeStatus {
run_id: run_id.to_string(),
session_id: run.session_id.clone().unwrap_or_default(),
node_id: node_id.to_string(),
status: status.clone(),
error,
};
// `run` borrow ends here (NLL) — jobs map is a separate field.
inner
.jobs
.remove(&(run_id.to_string(), node_id.to_string()));
(event, status)
};
self.finish_node_observation(run_id, node_id, applied.1);
self.emit(applied.0);
self.after_node_terminal(run_id, node_id);
self.notify_persist();
}
/// Live token/preview sync while a node is running (mirrors the TS job
/// update handler; refreshes the idle watchdog clock). `launch_gen` is the
/// launch generation the reporting job captured at dispatch time: updates
/// from a stale job (its launch was cancelled/skipped/retried meanwhile)
/// are dropped so they can't pollute a re-launched attempt's tokens/preview
/// or refresh the run's idle clock.
pub fn on_node_update(
&self,
run_id: &str,
node_id: &str,
launch_gen: u64,
input_tokens: Option<u64>,
output_tokens: Option<u64>,
preview: Option<String>,
) {
let mut inner = self.inner.lock();
let Some(run) = inner.runs.get_mut(run_id) else {
return;
};
let Some(node) = run.node_mut(node_id) else {
return;
};
if node.launch_gen != launch_gen {
return;
}
let now = now_ms();
if let Some(t) = input_tokens {
node.input_tokens = Some(t);
}
if let Some(t) = output_tokens {
node.output_tokens = Some(t);
}
if let Some(p) = preview {
node.live_preview = Some(cap_chars(&p, 2048));
}
node.last_active_at = Some(now);
// After the `node` borrow ends (NLL): refresh the run idle clock.
run.last_activity_at = now;
drop(inner);
self.notify_persist();
}
/// Re-derive non-terminal node states after a dependency flipped.
pub(super) fn reconcile(&self, run_id: &str) {
let mut inner = self.inner.lock();
let Some(run) = inner.runs.get_mut(run_id) else {
return;
};
super::model::reconcile(run);
emit_state(run);
}
/// Common post-terminal-node processing: failFast abort, cascade,
/// schedule, maybe finish.
pub(super) fn after_node_terminal(&self, run_id: &str, node_id: &str) {
let (fail_fast, failed) = {
let inner = self.inner.lock();
let Some(run) = inner.runs.get(run_id) else {
return;
};
let failed = run
.node(node_id)
.map(|n| n.status == NodeStatus::Failed)
.unwrap_or(false);
(run.fail_fast, failed)
};
if failed && fail_fast {
self.cancel_run(
run_id,
Some(&format!("failFast: 节点 {node_id} 失败, 终止整个运行")),
);
return;
}
self.reconcile(run_id);
self.tick(run_id);
self.maybe_complete(run_id);
}
/// Terminal when every node is terminal; sets run status failed/completed.
pub fn maybe_complete(&self, run_id: &str) {
let terminal = {
let mut inner = self.inner.lock();
let Some(run) = inner.runs.get_mut(run_id) else {
return;
};
if run.status != DagStatus::Running || !run.nodes.iter().all(|n| is_terminal(&n.status))
{
None
} else {
let has_failure = run
.nodes
.iter()
.any(|n| matches!(n.status, NodeStatus::Failed | NodeStatus::Cancelled));
run.status = if has_failure {
DagStatus::Failed
} else {
DagStatus::Completed
};
run.completed_at = Some(now_ms());
emit_state(run);
Some((
DagEvent::RunStatus {
run_id: run_id.to_string(),
session_id: run.session_id.clone().unwrap_or_default(),
status: run.status.clone(),
error: run.error.clone(),
},
run.status.clone(),
run.session_id.clone(),
))
}
};
if let Some((event, status, session_id)) = terminal {
self.finish_run_observation(run_id, status);
self.emit(event);
self.evict(session_id.as_deref());
self.wake_waiters(run_id);
}
self.notify_persist();
}
/// Resume persisted runs (running nodes demoted to ready and re-scheduled
/// by tick). Skips ids already registered; aligns the dag-N counter.
/// Returns the restored run ids.
pub fn restore(&self, runs: Vec<PersistedRun>) -> Vec<String> {
let mut restored: Vec<String> = Vec::new();
{
let mut inner = self.inner.lock();
for p in runs {
if inner.runs.contains_key(&p.id) {
continue;
}
let run = hydrate(p);
inner.dag_counter = inner
.dag_counter
.max(max_run_counter(std::slice::from_ref(&run)));
let id = run.id.clone();
inner.runs.insert(id.clone(), run);
restored.push(id);
}
}
for id in &restored {
if self
.get_run(id)
.is_some_and(|run| run.status == DagStatus::Running)
{
self.begin_run_observation(id);
}
self.reconcile(id);
self.tick(id);
}
if !restored.is_empty() {
self.notify_persist();
}
restored
}
// ── waiting ─────────────────────────────────────────────────────────────
/// Block until each run reaches a terminal state (event-driven, no
/// polling). One shared deadline for all runs; `(id, true)` means the run
/// was still running when the timeout / idle watchdog fired.
pub async fn wait_for_runs(
&self,
run_ids: &[String],
timeout: Duration,
idle: Option<Duration>,
) -> Vec<(String, bool)> {
if run_ids.is_empty() {
return Vec::new();
}
let deadline = tokio::time::Instant::now() + timeout;
let remaining = deadline.saturating_duration_since(tokio::time::Instant::now());
let waiters: Vec<_> = run_ids
.iter()
.map(|id| {
let engine = self.clone();
let id = id.clone();
async move { engine.wait_for_run(&id, remaining, idle).await }
})
.collect();
join_all(waiters).await
}
/// Condition-variable loop, deadline-bounded; re-checks terminal state
/// every wake so a missed notify (notify_waiters raced our park) is
/// harmless — we fall through to the next sleep.
async fn wait_for_run(
&self,
run_id: &str,
timeout: Duration,
idle: Option<Duration>,
) -> (String, bool) {
self.maybe_complete(run_id);
let deadline = tokio::time::Instant::now() + timeout;
loop {
let terminal = {
let inner = self.inner.lock();
match inner.runs.get(run_id) {
Some(run) => run.status != DagStatus::Running,
// Unknown run: treat as already finished (defensive close).
None => true,
}
};
if terminal {
return (run_id.to_string(), false);
}
let now = tokio::time::Instant::now();
if now >= deadline {
return (run_id.to_string(), true);
}
let last_activity = {
let inner = self.inner.lock();
inner
.runs
.get(run_id)
.map(|r| r.last_activity_at)
.unwrap_or(now_ms())
};
let mut sleep = deadline - now;
if let Some(idle_dur) = idle {
let idle_ms = idle_dur.as_millis() as i64;
let idle_elapsed = (now_ms() - last_activity).max(0);
if idle_elapsed >= idle_ms {
return (run_id.to_string(), true);
}
sleep = sleep.min(Duration::from_millis((idle_ms - idle_elapsed) as u64));
}
let notify = self
.inner
.lock()
.waiters
.entry(run_id.to_string())
.or_default()
.clone();
tokio::select! {
_ = tokio::time::sleep(sleep) => {}
_ = notify.notified() => {}
}
}
}
pub(super) fn wake_waiters(&self, run_id: &str) {
let notify = self.inner.lock().waiters.get(run_id).cloned();
if let Some(notify) = notify {
notify.notify_waiters();
}
}
}
#[cfg(test)]
tests_bridge_macro::tests_bridge!("multiagent/graph/scheduler");