beam-core 0.7.1

Shared core types and workflow/session logic for beam
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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
use std::collections::BTreeMap;
use std::fs;
use std::sync::Arc;
use tokio::sync::Mutex;

use anyhow::Result;
use async_trait::async_trait;
use serde_json::Value;

use crate::WorkflowOutputRef;
use crate::workflow_definition::{HostExecutorNode, NodeBase, SubagentNode};
use crate::workflow_snapshot::{DanglingSnapshot, RunState, RunStatus};
use crate::{EventDraft, EventLog, RunSnapshotDTO, WorkflowActor};

use super::test_common::{FakeHooks, temp_run_dir};
use super::*;

/// A hook that counts how many times `recover_dangling_effects` is called.
#[derive(Clone)]
struct RecoveryCountingHooks {
    recovery_calls: Arc<Mutex<usize>>,
}

#[async_trait]
impl WorkflowExecutionHooks for RecoveryCountingHooks {
    async fn execute_subagent(
        &mut self,
        _ctx: WorkflowDispatchRun<'_>,
        _node: &SubagentNode,
        resolved_prompt: String,
    ) -> Result<WorkflowDispatchOutcome> {
        Ok(WorkflowDispatchOutcome::Succeeded {
            output: Value::String(resolved_prompt),
            session: None,
        })
    }

    async fn execute_host_executor(
        &mut self,
        _ctx: WorkflowDispatchRun<'_>,
        _node: &HostExecutorNode,
        resolved_input: Value,
    ) -> Result<WorkflowDispatchOutcome> {
        Ok(WorkflowDispatchOutcome::Succeeded {
            output: resolved_input,
            session: None,
        })
    }

    async fn recover_dangling_effects(
        &mut self,
        _log: &mut EventLog,
        _snapshot: &RunSnapshotDTO,
    ) -> Result<RecoveryResult> {
        *self.recovery_calls.lock().await += 1;
        Ok(RecoveryResult {
            had_progress: false,
            has_remaining_dangling: true,
        })
    }
}

#[tokio::test]
async fn run_loop_calls_recovery_when_dangling_effects_present() {
    let run_dir = temp_run_dir("recovery-call");
    let _ = fs::remove_dir_all(&run_dir);
    fs::create_dir_all(run_dir.join("blobs")).unwrap();
    let paths = crate::BeamPaths::from_root(run_dir.clone());
    let run_id = "run-recovery-call";
    let _ = crate::bootstrap_workflow_run(
        &paths,
        crate::BootstrapWorkflowRunInput {
            run_id,
            workflow_json: r#"{"workflowId":"flow-recovery","version":1,"nodes":{"a":{"type":"hostExecutor","executor":"feishu-send","input":{"chatId":"chat-1","content":"hello"},"unsafeAllowUngated":true}}}"#,
            expected_workflow_id: Some("flow-recovery"),
            params: &BTreeMap::new(),
            initiator: "cli",
            chat_binding: None,
        },
    )
    .unwrap();

    // Activity id that the orchestrator expects for node "a" work.
    let work_activity_id = format!("{}::work::a", run_id);
    let work_attempt_id = format!("{}::work::a::att-1", run_id);

    // Write events that create a dangling effectAttempted activity matching
    // the orchestrator's work activity id.
    {
        let mut log = EventLog::new(run_id, paths.workflow_runs_dir()).unwrap();
        let _ = log
            .append(EventDraft {
                event_type: "attemptCreated".to_string(),
                actor: WorkflowActor::Scheduler,
                payload: serde_json::json!({
                    "nodeId": "a",
                    "activityId": &work_activity_id,
                    "attemptId": &work_attempt_id,
                    "attemptNumber": 1,
                    "inputRef": {
                        "outputHash": "sha256:aa",
                        "outputPath": "/tmp/aa",
                        "outputBytes": 2,
                        "outputSchemaVersion": 1,
                        "contentType": "application/json"
                    }
                }),
                timestamp: None,
                payload_hash: None,
            })
            .unwrap();
        let _ = log
            .append(EventDraft {
                event_type: "effectAttempted".to_string(),
                actor: WorkflowActor::HostExecutor,
                payload: serde_json::json!({
                    "activityId": &work_activity_id,
                    "attemptId": &work_attempt_id,
                    "idempotencyKey": "wf_key_recovery",
                    "inputHash": "sha256:bb",
                    "idempotencyTtlMs": 60000u64,
                    "provider": "feishu-im",
                }),
                timestamp: None,
                payload_hash: None,
            })
            .unwrap();
    }

    let mut rt = WorkflowRuntimeContext {
        log: EventLog::new(run_id, paths.workflow_runs_dir()).unwrap(),
        def: WorkflowDefinition {
            workflow_id: "flow-recovery".to_string(),
            version: 1,
            params: None,
            defaults: None,
            nodes: BTreeMap::from([(
                "a".to_string(),
                WorkflowNode::HostExecutor(HostExecutorNode {
                    base: NodeBase {
                        description: None,
                        depends: None,
                        human_gate: None,
                        retry_policy: None,
                        timeout_ms: None,
                        max_output_bytes: None,
                        output_schema: None,
                        unsafe_allow_ungated: Some(true),
                    },
                    executor: "feishu-send".to_string(),
                    input: serde_json::json!({"chatId":"chat-1","content":"hello"}),
                }),
            )]),
        },
        runs_base_dir: paths.workflow_runs_dir(),
    };

    let recovery_calls = Arc::new(Mutex::new(0usize));
    let mut hooks = RecoveryCountingHooks {
        recovery_calls: recovery_calls.clone(),
    };
    let result = run_loop(&mut rt, &mut hooks, 5, 1).await.unwrap();

    let calls = *recovery_calls.lock().await;
    assert!(
        calls > 0,
        "expected recover_dangling_effects to be called at least once, got {calls}"
    );
    // Since recovery returned had_progress=false, the loop should fall
    // through to run_tick and eventually stop (not loop forever on recovery).
    assert!(
        matches!(
            result.reason,
            RunLoopStopReason::NoProgress | RunLoopStopReason::AwaitingWait
        ),
        "expected NoProgress or AwaitingWait, got {:?}",
        result.reason
    );
    let _ = fs::remove_dir_all(&run_dir);
}

/// A hook that simulates successful recovery: writes activitySucceeded for
/// every dangling effectAttempted activity.
#[derive(Clone)]
struct RecoveringHooks;

#[async_trait]
impl WorkflowExecutionHooks for RecoveringHooks {
    async fn execute_subagent(
        &mut self,
        _ctx: WorkflowDispatchRun<'_>,
        _node: &SubagentNode,
        resolved_prompt: String,
    ) -> Result<WorkflowDispatchOutcome> {
        Ok(WorkflowDispatchOutcome::Succeeded {
            output: Value::String(resolved_prompt),
            session: None,
        })
    }

    async fn execute_host_executor(
        &mut self,
        _ctx: WorkflowDispatchRun<'_>,
        _node: &HostExecutorNode,
        resolved_input: Value,
    ) -> Result<WorkflowDispatchOutcome> {
        Ok(WorkflowDispatchOutcome::Succeeded {
            output: resolved_input,
            session: None,
        })
    }

    async fn recover_dangling_effects(
        &mut self,
        log: &mut EventLog,
        snapshot: &RunSnapshotDTO,
    ) -> Result<RecoveryResult> {
        let mut had_progress = false;
        for activity_id in &snapshot.dangling.effect_attempted {
            if let Some(activity) = snapshot
                .activities
                .iter()
                .find(|a| &a.activity_id == activity_id)
            {
                if let Some(latest) = activity.attempts.last() {
                    let output_ref = WorkflowOutputRef {
                        output_hash: "sha256:recovered".to_string(),
                        output_path: "/tmp/recovered".to_string(),
                        output_bytes: 3,
                        output_schema_version: 1,
                        content_type: Some("application/json".to_string()),
                    };
                    let _ = log.append(EventDraft {
                        event_type: "activitySucceeded".to_string(),
                        actor: WorkflowActor::System,
                        payload: serde_json::json!({
                            "activityId": activity_id,
                            "attemptId": &latest.attempt_id,
                            "outputRef": output_ref,
                        }),
                        timestamp: None,
                        payload_hash: None,
                    })?;
                    had_progress = true;
                }
            }
        }
        Ok(RecoveryResult {
            had_progress,
            has_remaining_dangling: false,
        })
    }
}

#[tokio::test]
async fn run_loop_replays_after_recovery_writes_events() {
    let run_dir = temp_run_dir("recovery-replay");
    let _ = fs::remove_dir_all(&run_dir);
    fs::create_dir_all(run_dir.join("blobs")).unwrap();
    let paths = crate::BeamPaths::from_root(run_dir.clone());
    let run_id = "run-recovery-replay";
    let _ = crate::bootstrap_workflow_run(
        &paths,
        crate::BootstrapWorkflowRunInput {
            run_id,
            workflow_json: r#"{"workflowId":"flow-replay","version":1,"nodes":{"a":{"type":"hostExecutor","executor":"feishu-send","input":{"chatId":"chat-1","content":"hello"},"unsafeAllowUngated":true}}}"#,
            expected_workflow_id: Some("flow-replay"),
            params: &BTreeMap::new(),
            initiator: "cli",
            chat_binding: None,
        },
    )
    .unwrap();

    // Activity id that the orchestrator expects for node "a" work.
    let work_activity_id = format!("{}::work::a", run_id);
    let work_attempt_id = format!("{}::work::a::att-1", run_id);

    // Write events that create a dangling effectAttempted activity
    {
        let mut log = EventLog::new(run_id, paths.workflow_runs_dir()).unwrap();
        let _ = log
            .append(EventDraft {
                event_type: "attemptCreated".to_string(),
                actor: WorkflowActor::Scheduler,
                payload: serde_json::json!({
                    "nodeId": "a",
                    "activityId": &work_activity_id,
                    "attemptId": &work_attempt_id,
                    "attemptNumber": 1,
                    "inputRef": {
                        "outputHash": "sha256:aa",
                        "outputPath": "/tmp/aa",
                        "outputBytes": 2,
                        "outputSchemaVersion": 1,
                        "contentType": "application/json"
                    }
                }),
                timestamp: None,
                payload_hash: None,
            })
            .unwrap();
        let _ = log
            .append(EventDraft {
                event_type: "effectAttempted".to_string(),
                actor: WorkflowActor::HostExecutor,
                payload: serde_json::json!({
                    "activityId": &work_activity_id,
                    "attemptId": &work_attempt_id,
                    "idempotencyKey": "wf_key_replay",
                    "inputHash": "sha256:bb",
                    "idempotencyTtlMs": 60000u64,
                    "provider": "feishu-im",
                }),
                timestamp: None,
                payload_hash: None,
            })
            .unwrap();
    }

    let mut rt = WorkflowRuntimeContext {
        log: EventLog::new(run_id, paths.workflow_runs_dir()).unwrap(),
        def: WorkflowDefinition {
            workflow_id: "flow-replay".to_string(),
            version: 1,
            params: None,
            defaults: None,
            nodes: BTreeMap::from([(
                "a".to_string(),
                WorkflowNode::HostExecutor(HostExecutorNode {
                    base: NodeBase {
                        description: None,
                        depends: None,
                        human_gate: None,
                        retry_policy: None,
                        timeout_ms: None,
                        max_output_bytes: None,
                        output_schema: None,
                        unsafe_allow_ungated: Some(true),
                    },
                    executor: "feishu-send".to_string(),
                    input: serde_json::json!({"chatId":"chat-1","content":"hello"}),
                }),
            )]),
        },
        runs_base_dir: paths.workflow_runs_dir(),
    };

    let mut hooks = RecoveringHooks;
    let result = run_loop(&mut rt, &mut hooks, 5, 1).await.unwrap();

    // After recovery writes activitySucceeded, the orchestrator should
    // be able to complete the node and the run.  Verify that the run
    // reached a terminal state or made measurable progress.
    let final_snapshot = read_snapshot(&rt).await.unwrap();
    assert!(
        matches!(
            final_snapshot.run.status,
            RunStatus::Succeeded | RunStatus::Failed | RunStatus::Cancelled
        ) || result.ticks > 0,
        "expected recovery to allow progress; reason={:?} ticks={} status={:?}",
        result.reason,
        result.ticks,
        final_snapshot.run.status
    );
    // Verify the recovery event was written
    let events = rt.log.read_all().unwrap();
    let has_recovered = events
        .iter()
        .any(|e| e.event_type == "activitySucceeded" && e.actor == WorkflowActor::System);
    assert!(
        has_recovered,
        "expected a system activitySucceeded from recovery"
    );
    let _ = fs::remove_dir_all(&run_dir);
}

#[tokio::test]
async fn run_loop_no_infinite_loop_when_recovery_cannot_progress() {
    let run_dir = temp_run_dir("recovery-stuck");
    let _ = fs::remove_dir_all(&run_dir);
    fs::create_dir_all(run_dir.join("blobs")).unwrap();
    let paths = crate::BeamPaths::from_root(run_dir.clone());
    let run_id = "run-recovery-stuck";
    let _ = crate::bootstrap_workflow_run(
        &paths,
        crate::BootstrapWorkflowRunInput {
            run_id,
            workflow_json: r#"{"workflowId":"flow-stuck","version":1,"nodes":{"a":{"type":"hostExecutor","executor":"feishu-send","input":{"chatId":"chat-1","content":"hello"},"unsafeAllowUngated":true}}}"#,
            expected_workflow_id: Some("flow-stuck"),
            params: &BTreeMap::new(),
            initiator: "cli",
            chat_binding: None,
        },
    )
    .unwrap();

    // Activity id that the orchestrator expects for node "a" work.
    let work_activity_id = format!("{}::work::a", run_id);
    let work_attempt_id = format!("{}::work::a::att-1", run_id);

    // Write events that create a dangling effectAttempted activity
    {
        let mut log = EventLog::new(run_id, paths.workflow_runs_dir()).unwrap();
        let _ = log
            .append(EventDraft {
                event_type: "attemptCreated".to_string(),
                actor: WorkflowActor::Scheduler,
                payload: serde_json::json!({
                    "nodeId": "a",
                    "activityId": &work_activity_id,
                    "attemptId": &work_attempt_id,
                    "attemptNumber": 1,
                    "inputRef": {
                        "outputHash": "sha256:aa",
                        "outputPath": "/tmp/aa",
                        "outputBytes": 2,
                        "outputSchemaVersion": 1,
                        "contentType": "application/json"
                    }
                }),
                timestamp: None,
                payload_hash: None,
            })
            .unwrap();
        let _ = log
            .append(EventDraft {
                event_type: "effectAttempted".to_string(),
                actor: WorkflowActor::HostExecutor,
                payload: serde_json::json!({
                    "activityId": &work_activity_id,
                    "attemptId": &work_attempt_id,
                    "idempotencyKey": "wf_key_stuck",
                    "inputHash": "sha256:bb",
                    "idempotencyTtlMs": 60000u64,
                    "provider": "unknown-provider",
                }),
                timestamp: None,
                payload_hash: None,
            })
            .unwrap();
    }

    let mut rt = WorkflowRuntimeContext {
        log: EventLog::new(run_id, paths.workflow_runs_dir()).unwrap(),
        def: WorkflowDefinition {
            workflow_id: "flow-stuck".to_string(),
            version: 1,
            params: None,
            defaults: None,
            nodes: BTreeMap::from([(
                "a".to_string(),
                WorkflowNode::HostExecutor(HostExecutorNode {
                    base: NodeBase {
                        description: None,
                        depends: None,
                        human_gate: None,
                        retry_policy: None,
                        timeout_ms: None,
                        max_output_bytes: None,
                        output_schema: None,
                        unsafe_allow_ungated: Some(true),
                    },
                    executor: "feishu-send".to_string(),
                    input: serde_json::json!({"chatId":"chat-1","content":"hello"}),
                }),
            )]),
        },
        runs_base_dir: paths.workflow_runs_dir(),
    };

    // Use FakeHooks which returns had_progress=false for recovery.
    let mut hooks = FakeHooks;
    let result = run_loop(&mut rt, &mut hooks, 10, 1).await.unwrap();

    // The loop must NOT return MaxTicks (that would mean it consumed all
    // ticks in unsuccessful recovery attempts).  With no real progress,
    // it should return NoProgress or AwaitingWait.
    assert!(
        !matches!(result.reason, RunLoopStopReason::MaxTicks),
        "expected run_loop to stop without exhausting max_ticks on unrecoverable dangling effects"
    );
    assert!(
        matches!(
            result.reason,
            RunLoopStopReason::NoProgress | RunLoopStopReason::AwaitingWait
        ),
        "expected NoProgress or AwaitingWait, got {:?}",
        result.reason
    );
    let _ = fs::remove_dir_all(&run_dir);
}

#[tokio::test]
async fn default_recovery_result_has_correct_semantics() {
    // Default implementation: had_progress=false always.
    // has_remaining_dangling should match whether the snapshot actually
    // contains dangling effect_attempted activities.

    // Snapshot with no dangling effects → has_remaining_dangling=false
    let empty_snapshot = RunSnapshotDTO {
        run_id: "r".to_string(),
        run: RunState {
            run_id: "r".to_string(),
            status: RunStatus::Running,
            workflow_id: None,
            revision_id: None,
            initiator: None,
            input: None,
            output: None,
            failed_node_id: None,
            root_cause_event_id: None,
            cancel_origin_event_id: None,
            bot_snapshots: None,
            cancelled_run_intent: None,
            cancelled_node_intents: BTreeMap::new(),
        },
        last_seq: 0,
        nodes: vec![],
        activities: vec![],
        loops: None,
        dangling: DanglingSnapshot {
            activities: vec![],
            effect_attempted: vec![],
            waits: vec![],
            wait_resolutions: vec![],
            cancels: vec![],
        },
        outputs: BTreeMap::new(),
        attempt_io: BTreeMap::new(),
        chat_binding: None,
        updated_at: 0,
    };

    // Need a real EventLog for the &mut reference, but FakeHooks doesn't use it.
    let run_dir = temp_run_dir("recovery-semantics");
    let _ = fs::remove_dir_all(&run_dir);
    fs::create_dir_all(&run_dir).unwrap();
    let paths = crate::BeamPaths::from_root(run_dir.clone());
    let mut log = EventLog::new("r", paths.workflow_runs_dir()).unwrap();

    // Use the trait method via a concrete type to test default impl.
    // We call it through a concrete struct that doesn't override the default.
    #[derive(Clone)]
    struct DefaultingHooks;
    #[async_trait]
    impl WorkflowExecutionHooks for DefaultingHooks {
        async fn execute_subagent(
            &mut self,
            _ctx: WorkflowDispatchRun<'_>,
            _node: &SubagentNode,
            _resolved_prompt: String,
        ) -> Result<WorkflowDispatchOutcome> {
            unreachable!()
        }
        async fn execute_host_executor(
            &mut self,
            _ctx: WorkflowDispatchRun<'_>,
            _node: &HostExecutorNode,
            _resolved_input: Value,
        ) -> Result<WorkflowDispatchOutcome> {
            unreachable!()
        }
        // Uses default recover_dangling_effects
    }

    let mut hooks = DefaultingHooks;

    // Empty: has_remaining_dangling should be false
    let result = hooks
        .recover_dangling_effects(&mut log, &empty_snapshot)
        .await
        .unwrap();
    assert!(!result.had_progress, "default: had_progress must be false");
    assert!(
        !result.has_remaining_dangling,
        "empty dangling → has_remaining_dangling must be false"
    );

    // With dangling effects: has_remaining_dangling should be true
    let dangling_snapshot = RunSnapshotDTO {
        dangling: DanglingSnapshot {
            effect_attempted: vec!["act-1".to_string()],
            ..empty_snapshot.dangling.clone()
        },
        ..empty_snapshot.clone()
    };
    let result2 = hooks
        .recover_dangling_effects(&mut log, &dangling_snapshot)
        .await
        .unwrap();
    assert!(!result2.had_progress, "default: had_progress must be false");
    assert!(
        result2.has_remaining_dangling,
        "dangling present → has_remaining_dangling must be true"
    );

    let _ = fs::remove_dir_all(&run_dir);
}