crabmate 0.5.0

Rust AI agent: OpenAI-compatible chat/completions, function calling, HTTP serve, ops CLI
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
//! DAG 并行调度:就绪检测、choice 剪枝、`for_each` 运行时展开、信号量主循环。

use std::collections::{HashMap, HashSet};
use std::sync::Arc;

use futures_util::FutureExt;
use futures_util::StreamExt;
use futures_util::stream::FuturesUnordered;
use tokio::sync::Semaphore;

use super::super::for_each_expand::expand_pending_for_each;
use super::super::model::{WorkflowNodeSpec, WorkflowSpec};
use super::super::run_if::{node_deps_resolved, node_run_if_satisfied};
use super::super::types::{NodeRunResult, NodeRunStatus};
use super::node::run_node;
use super::trace::{WorkflowTracePush, workflow_trace_push};
use super::{WorkflowApprovalMode, WorkflowToolExecCtx};

/// `execute_workflow_dag` 主调度循环结束后的聚合状态。
pub(crate) struct DagExecutionProgress {
    pub(crate) completed: HashMap<String, NodeRunResult>,
    pub(crate) started: HashSet<String>,
    pub(crate) completion_order: Vec<String>,
    pub(crate) first_failure: Option<NodeRunResult>,
}

/// `inflight` 为空时的处理:正常结束 / fail_fast 退出 / 活锁检测。
/// 返回值:`true` 表示应 break 退出主循环,`false` 表示 continue。
#[allow(clippy::too_many_arguments)]
fn dag_handle_empty_inflight(
    spec: &WorkflowSpec,
    active_nodes: &[WorkflowNodeSpec],
    completed: &mut HashMap<String, NodeRunResult>,
    started: &mut HashSet<String>,
    for_each_pending: &[super::super::model::ForEachPendingSpec],
    first_failure: &Option<NodeRunResult>,
    tool_exec_ctx: &WorkflowToolExecCtx,
    stall_count: &mut u32,
    max_stall: u32,
) -> bool {
    // 正常调度完成
    if dag_schedule_finished(active_nodes, completed, for_each_pending) {
        return true; // break
    }
    // fail_fast 首失败
    if spec.fail_fast && first_failure.is_some() {
        dag_mark_remaining_nodes_fail_fast_skipped(active_nodes, completed, started, tool_exec_ctx);
        return true; // break
    }
    // 活锁检测(P0-3)
    *stall_count += 1;
    if *stall_count > max_stall {
        let err_msg = format!(
            "workflow 调度活锁:连续 {} 次迭代无节点可调度(可能是运行时循环依赖),已强制终止",
            stall_count,
        );
        log::error!(target: "crabmate", "{}", err_msg);
        for node in active_nodes.iter() {
            if !completed.contains_key(&node.id) {
                started.insert(node.id.clone());
                completed.insert(
                    node.id.clone(),
                    NodeRunResult {
                        id: node.id.clone(),
                        status: NodeRunStatus::Failed,
                        output: err_msg.clone().into(),
                        workspace_changed: false,
                        exit_code: None,
                        error_code: Some("workflow_livelock".to_string()),
                        attempt: 0,
                    },
                );
            }
        }
        return true; // break
    }
    false // continue
}

fn node_panic_result(node_id: String, panic_payload: Box<dyn std::any::Any + Send>) -> NodeRunResult {
    let msg = if let Some(s) = panic_payload.downcast_ref::<&str>() {
        format!("workflow 节点 panic:{}", s)
    } else if let Some(s) = panic_payload.downcast_ref::<String>() {
        format!("workflow 节点 panic:{}", s)
    } else {
        "workflow 节点 panic(原因未知)".to_string()
    };
    log::error!(
        target: "crabmate",
        "workflow 节点 panic node_id={} msg={}",
        node_id,
        msg,
    );
    NodeRunResult {
        id: node_id,
        status: NodeRunStatus::Failed,
        output: msg.into(),
        workspace_changed: false,
        exit_code: None,
        error_code: Some("workflow_node_panic".to_string()),
        attempt: 1,
    }
}

async fn run_node_with_permit(
    node: WorkflowNodeSpec,
    approval_mode: WorkflowApprovalMode,
    exec_ctx: WorkflowToolExecCtx,
    completed_snapshot: HashMap<String, NodeRunResult>,
    inject_max_chars: usize,
    permit_sem: Arc<Semaphore>,
) -> NodeRunResult {
    let node_id = node.id.clone();
    let _permit = match permit_sem.acquire_owned().await {
        Ok(p) => p,
        Err(_) => {
            return NodeRunResult {
                id: node_id,
                status: NodeRunStatus::Failed,
                output: "workflow 并发控制异常(semaphore closed)".into(),
                workspace_changed: false,
                exit_code: None,
                error_code: Some("workflow_semaphore_closed".to_string()),
                attempt: 1,
            };
        }
    };
    let node_fut = run_node(
        node,
        approval_mode,
        exec_ctx,
        completed_snapshot,
        inject_max_chars,
        "main",
    );
    match std::panic::AssertUnwindSafe(node_fut).catch_unwind().await {
        Ok(res) => res,
        Err(panic_payload) => node_panic_result(node_id, panic_payload),
    }
}

fn mark_run_if_skipped(
    node: &WorkflowNodeSpec,
    started: &mut HashSet<String>,
    completed: &mut HashMap<String, NodeRunResult>,
    tool_exec_ctx: &WorkflowToolExecCtx,
) {
    started.insert(node.id.clone());
    completed.insert(
        node.id.clone(),
        NodeRunResult {
            id: node.id.clone(),
            status: NodeRunStatus::Skipped,
            output: "choice: run_if not satisfied".into(),
            workspace_changed: false,
            exit_code: None,
            error_code: Some("workflow_choice_skipped".to_string()),
            attempt: 0,
        },
    );
    workflow_trace_push(WorkflowTracePush {
        trace: &tool_exec_ctx.trace_events,
        workflow_run_id: tool_exec_ctx.workflow_run_id,
        event: "node_choice_skipped",
        node_id: Some(node.id.as_str()),
        detail: node.run_if.as_ref().map(|_| "run_if=false".to_string()),
        attempt: None,
        status: Some("skipped"),
        elapsed_ms: None,
        error_code: Some("workflow_choice_skipped"),
        tool_name: Some(node.tool_name.as_str()),
        phase: Some("main"),
    });
}

fn trace_for_each_expanded(tool_exec_ctx: &WorkflowToolExecCtx, expanded: &[String]) {
    for id in expanded.iter() {
        workflow_trace_push(WorkflowTracePush {
            trace: &tool_exec_ctx.trace_events,
            workflow_run_id: tool_exec_ctx.workflow_run_id,
            event: "for_each_expanded",
            node_id: Some(id.as_str()),
            detail: None,
            attempt: None,
            status: None,
            elapsed_ms: None,
            error_code: None,
            tool_name: None,
            phase: Some("main"),
        });
    }
}

enum NodeScheduleDecision {
    Ignore,
    SkipRunIf,
    Start,
}

fn node_schedule_decision(
    node: &WorkflowNodeSpec,
    started: &HashSet<String>,
    completed: &HashMap<String, NodeRunResult>,
) -> NodeScheduleDecision {
    if started.contains(&node.id) || completed.contains_key(&node.id) {
        return NodeScheduleDecision::Ignore;
    }
    if !node_deps_resolved(&node.deps, completed) {
        return NodeScheduleDecision::Ignore;
    }
    if !node_run_if_satisfied(node.run_if.as_ref(), completed) {
        return NodeScheduleDecision::SkipRunIf;
    }
    NodeScheduleDecision::Start
}

/// 并行调度就绪节点并等待全部 inflight 完成。
pub(super) async fn dag_run_parallel_schedule_loop(
    spec: &WorkflowSpec,
    approval_mode: WorkflowApprovalMode,
    tool_exec_ctx: WorkflowToolExecCtx,
) -> DagExecutionProgress {
    let mut active_nodes = spec.nodes.clone();
    let mut for_each_pending = spec.for_each_pending.clone();
    let mut completed: HashMap<String, NodeRunResult> = HashMap::new();
    let mut started: HashSet<String> = HashSet::new();
    let mut completion_order: Vec<String> = Vec::new();
    let mut first_failure: Option<NodeRunResult> = None;

    let max_parallelism = spec.max_parallelism.max(1);
    let semaphore = Arc::new(Semaphore::new(max_parallelism));
    let mut inflight: FuturesUnordered<_> = FuturesUnordered::new();

    // P0-3: 运行时活锁检测
    let max_stall = (max_parallelism.max(4) * 2) as u32;
    let mut stall_count: u32 = 0;

    loop {
        let expanded =
            expand_pending_for_each(&mut for_each_pending, &mut active_nodes, &completed);
        trace_for_each_expanded(&tool_exec_ctx, &expanded);

        if !(spec.fail_fast && first_failure.is_some()) {
            for node in active_nodes.iter() {
                match node_schedule_decision(node, &started, &completed) {
                    NodeScheduleDecision::Ignore => {}
                    NodeScheduleDecision::SkipRunIf => {
                        mark_run_if_skipped(node, &mut started, &mut completed, &tool_exec_ctx);
                    }
                    NodeScheduleDecision::Start => {
                        started.insert(node.id.clone());
                        inflight.push(run_node_with_permit(
                            node.clone(),
                            approval_mode.clone(),
                            tool_exec_ctx.clone(),
                            completed.clone(),
                            spec.output_inject_max_chars,
                            semaphore.clone(),
                        ));
                    }
                }
            }
        }

        if inflight.is_empty() {
            if dag_handle_empty_inflight(
                spec,
                &active_nodes,
                &mut completed,
                &mut started,
                &for_each_pending,
                &first_failure,
                &tool_exec_ctx,
                &mut stall_count,
                max_stall,
            ) {
                break;
            }
            continue;
        }

        stall_count = 0;
        let Some(res) = inflight.next().await else {
            continue;
        };
        dag_record_node_completion(
            &res,
            &mut completed,
            &mut completion_order,
            &mut first_failure,
        );
    }

    DagExecutionProgress {
        completed,
        started,
        completion_order,
        first_failure,
    }
}

fn dag_record_node_completion(
    res: &NodeRunResult,
    completed: &mut HashMap<String, NodeRunResult>,
    completion_order: &mut Vec<String>,
    first_failure: &mut Option<NodeRunResult>,
) {
    if res.status == NodeRunStatus::Passed {
        completion_order.push(res.id.clone());
        completed.insert(res.id.clone(), res.clone());
        return;
    }
    if res.status == NodeRunStatus::Skipped {
        completed.insert(res.id.clone(), res.clone());
        return;
    }
    if first_failure.is_none() {
        *first_failure = Some(res.clone());
    }
    completed.insert(
        res.id.clone(),
        NodeRunResult {
            id: res.id.clone(),
            status: NodeRunStatus::Failed,
            output: res.output.clone(),
            workspace_changed: res.workspace_changed,
            exit_code: res.exit_code,
            error_code: res.error_code.clone(),
            attempt: res.attempt,
        },
    );
}

fn dag_schedule_finished(
    nodes: &[WorkflowNodeSpec],
    completed: &HashMap<String, NodeRunResult>,
    for_each_pending: &[super::super::model::ForEachPendingSpec],
) -> bool {
    nodes.iter().all(|n| completed.contains_key(&n.id)) && for_each_pending.is_empty()
}

/// `fail_fast` 且已有首失败后:将尚未完成的节点标为跳过,避免调度器在 `inflight` 为空时 tight-loop。
fn dag_mark_remaining_nodes_fail_fast_skipped(
    nodes: &[WorkflowNodeSpec],
    completed: &mut HashMap<String, NodeRunResult>,
    started: &mut HashSet<String>,
    tool_exec_ctx: &WorkflowToolExecCtx,
) {
    for node in nodes {
        if completed.contains_key(&node.id) {
            continue;
        }
        started.insert(node.id.clone());
        completed.insert(
            node.id.clone(),
            NodeRunResult {
                id: node.id.clone(),
                status: NodeRunStatus::Skipped,
                output: "fail_fast: workflow aborted after first failure".into(),
                workspace_changed: false,
                exit_code: None,
                error_code: Some("workflow_fail_fast_aborted".to_string()),
                attempt: 0,
            },
        );
        workflow_trace_push(WorkflowTracePush {
            trace: &tool_exec_ctx.trace_events,
            workflow_run_id: tool_exec_ctx.workflow_run_id,
            event: "node_fail_fast_skipped",
            node_id: Some(node.id.as_str()),
            detail: Some("fail_fast after upstream failure".to_string()),
            attempt: None,
            status: Some("skipped"),
            elapsed_ms: None,
            error_code: Some("workflow_fail_fast_aborted"),
            tool_name: Some(node.tool_name.as_str()),
            phase: Some("main"),
        });
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::cm_workflow::model::WorkflowNodeSpec;
    use std::collections::HashMap;

    fn sample_nodes() -> Vec<WorkflowNodeSpec> {
        vec![
            WorkflowNodeSpec {
                id: "a".into(),
                tool_name: "tool_a".into(),
                tool_args: serde_json::json!({}),
                deps: vec![],
                requires_approval: false,
                timeout_secs: None,
                compensate_with: vec![],
                max_retries: 0,
                node_tool_role: None,
                run_if: None,
            },
            WorkflowNodeSpec {
                id: "b".into(),
                tool_name: "tool_b".into(),
                tool_args: serde_json::json!({}),
                deps: vec!["a".into()],
                requires_approval: false,
                timeout_secs: None,
                compensate_with: vec![],
                max_retries: 0,
                node_tool_role: None,
                run_if: None,
            },
        ]
    }

    #[test]
    fn dag_schedule_finished_requires_all_nodes_completed() {
        let nodes = sample_nodes();
        let mut completed = HashMap::new();
        assert!(!dag_schedule_finished(&nodes, &completed, &[]));
        completed.insert(
            "a".into(),
            NodeRunResult {
                id: "a".into(),
                status: NodeRunStatus::Failed,
                output: "fail".into(),
                workspace_changed: false,
                exit_code: None,
                error_code: None,
                attempt: 1,
            },
        );
        assert!(!dag_schedule_finished(&nodes, &completed, &[]));
    }
}