tandem-server 0.6.7

HTTP server for Tandem engine APIs
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
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
#[tokio::test]
async fn automation_v2_misfires_skip_queued_or_running_runs_for_same_automation() {
    let state = test_state_with_path(tmp_resource_file("automation-misfire-dedup"));
    let mut automation = AutomationSpecBuilder::new("auto-misfire-dedup")
        .nodes(vec![AutomationNodeBuilder::new("work").build()])
        .build();
    automation.schedule = AutomationV2Schedule {
        schedule_type: AutomationV2ScheduleType::Interval,
        cron_expression: None,
        interval_seconds: Some(60),
        timezone: "UTC".to_string(),
        misfire_policy: RoutineMisfirePolicy::RunOnce,
    };
    automation.next_fire_at_ms = Some(1_000);
    automation.status = AutomationV2Status::Active;
    {
        let mut automations = state.automations_v2.write().await;
        automations.insert(automation.automation_id.clone(), automation.clone());
    }
    {
        let mut runs = state.automation_v2_runs.write().await;
        runs.insert(
            "run-existing".to_string(),
            AutomationRunBuilder::new("run-existing", "auto-misfire-dedup")
                .status(AutomationRunStatus::Queued)
                .build(),
        );
    }

    let due = state.evaluate_automation_v2_misfires(2_000).await;

    assert!(due.is_empty(), "queued automation run should dedupe timer fire");
    let stored = state
        .automations_v2
        .read()
        .await
        .get("auto-misfire-dedup")
        .cloned()
        .expect("automation stored");
    assert_eq!(stored.next_fire_at_ms, Some(1_000));
    assert_eq!(stored.last_fired_at_ms, None);
}

#[test]
fn prompt_orders_required_workspace_writes_before_run_artifact() {
    let automation = AutomationV2Spec {
        automation_id: "automation-review".to_string(),
        name: "Review".to_string(),
        description: None,
        status: crate::AutomationV2Status::Active,
        schedule: crate::AutomationV2Schedule {
            schedule_type: crate::AutomationV2ScheduleType::Manual,
            cron_expression: None,
            interval_seconds: None,
            timezone: "UTC".to_string(),
            misfire_policy: crate::RoutineMisfirePolicy::RunOnce,
        },
        knowledge: tandem_orchestrator::KnowledgeBinding::default(),
        agents: Vec::new(),
        flow: crate::AutomationFlowSpec { nodes: Vec::new() },
        execution: crate::AutomationExecutionPolicy {
            profile: None,
            max_parallel_agents: Some(1),
            max_total_runtime_ms: None,
            max_total_tool_calls: None,
            max_total_tokens: None,
            max_total_cost_usd: None,
        },
        output_targets: Vec::new(),
        created_at_ms: 0,
        updated_at_ms: 0,
        creator_id: "test".to_string(),
        workspace_root: Some("/tmp".to_string()),
        metadata: None,
        next_fire_at_ms: None,
        last_fired_at_ms: None,
        scope_policy: None,
        watch_conditions: Vec::new(),
        handoff_config: None,
    };
    let node = AutomationFlowNode {
        knowledge: tandem_orchestrator::KnowledgeBinding::default(),
        node_id: "research_sources".to_string(),
        agent_id: "researcher".to_string(),
        objective: "Write a review and durable workspace report.".to_string(),
        depends_on: Vec::new(),
        input_refs: Vec::new(),
        output_contract: Some(AutomationFlowOutputContract {
            kind: "structured_json".to_string(),
            validator: Some(crate::AutomationOutputValidatorKind::StructuredJson),
            enforcement: None,
            schema: None,
            summary_guidance: None,
        }),
        tool_policy: None,
        mcp_policy: None,
        retry_policy: None,
        timeout_ms: None,
        max_tool_calls: None,
        stage_kind: None,
        gate: None,
        metadata: Some(json!({
            "builder": {
                "output_path": ".tandem/artifacts/research-sources.json",
                "must_write_files": ["tandem-review.md"]
            }
        })),
    };
    let agent = AutomationAgentProfile {
        agent_id: "researcher".to_string(),
        template_id: None,
        display_name: "Researcher".to_string(),
        avatar_url: None,
        model_policy: None,
        skills: Vec::new(),
        tool_policy: crate::AutomationAgentToolPolicy {
            allowlist: vec!["read".to_string(), "write".to_string()],
            denylist: Vec::new(),
        },
        mcp_policy: crate::AutomationAgentMcpPolicy {
            allowed_servers: Vec::new(),
            allowed_tools: None,
            allowed_connections: Vec::new(),
        },
        approval_policy: None,
    };

    let prompt = render_automation_v2_prompt(
        &automation,
        "/tmp",
        "run-review",
        &node,
        1,
        &agent,
        &[],
        &["read".to_string(), "write".to_string()],
        None,
        None,
        None,
    );

    let workspace_idx = prompt
        .find("Required Workspace Writes:")
        .expect("workspace writes section");
    let artifact_idx = prompt
        .find("Required Run Artifact:")
        .expect("run artifact section");
    assert!(workspace_idx < artifact_idx);
    assert!(prompt.contains("Write the required workspace file(s) first: `tandem-review.md`"));
    assert!(prompt.contains("Do not rely on, auto-copy, or mirror the run artifact"));
}

#[test]
fn prompt_includes_email_delivery_metadata_for_notify_user() {
    let automation = AutomationV2Spec {
        automation_id: "automation-email-delivery".to_string(),
        name: "Email Delivery Automation".to_string(),
        description: None,
        status: crate::AutomationV2Status::Active,
        schedule: crate::AutomationV2Schedule {
            schedule_type: crate::AutomationV2ScheduleType::Manual,
            cron_expression: None,
            interval_seconds: None,
            timezone: "UTC".to_string(),
            misfire_policy: crate::RoutineMisfirePolicy::RunOnce,
        },
        knowledge: tandem_orchestrator::KnowledgeBinding::default(),
        agents: Vec::new(),
        flow: crate::AutomationFlowSpec { nodes: Vec::new() },
        execution: crate::AutomationExecutionPolicy {
            profile: None,
            max_parallel_agents: Some(1),
            max_total_runtime_ms: None,
            max_total_tool_calls: None,
            max_total_tokens: None,
            max_total_cost_usd: None,
        },
        output_targets: Vec::new(),
        created_at_ms: 0,
        updated_at_ms: 0,
        creator_id: "test".to_string(),
        workspace_root: Some("/tmp".to_string()),
        metadata: None,
        next_fire_at_ms: None,
        last_fired_at_ms: None,
        scope_policy: None,
        watch_conditions: Vec::new(),
        handoff_config: None,
    };
    let node = AutomationFlowNode {
        knowledge: tandem_orchestrator::KnowledgeBinding::default(),
        node_id: "notify_user".to_string(),
        agent_id: "committer".to_string(),
        objective: "Send the finalized report to the requested email address in the email body using simple HTML.".to_string(),
        depends_on: vec!["generate_report".to_string()],
        input_refs: vec![AutomationFlowInputRef {
            from_step_id: "generate_report".to_string(),
            alias: "report_body".to_string(),
        }],
        output_contract: Some(AutomationFlowOutputContract {
            kind: "approval_gate".to_string(),
            validator: Some(crate::AutomationOutputValidatorKind::ReviewDecision),
            enforcement: None,
            schema: None,
            summary_guidance: None,
        }),
        tool_policy: None,
        mcp_policy: None,
        retry_policy: None,
        timeout_ms: None,
        max_tool_calls: None,
        stage_kind: None,
        gate: None,
        metadata: Some(json!({
            "delivery": {
                "method": "email",
                "to": "recipient@example.com",
                "content_type": "text/html",
                "inline_body_only": true,
                "attachments": false
            }
        })),
    };
    let agent = AutomationAgentProfile {
        agent_id: "committer".to_string(),
        template_id: None,
        display_name: "Committer".to_string(),
        avatar_url: None,
        model_policy: None,
        skills: Vec::new(),
        tool_policy: crate::AutomationAgentToolPolicy {
            allowlist: vec!["*".to_string()],
            denylist: Vec::new(),
        },
        mcp_policy: crate::AutomationAgentMcpPolicy {
            allowed_servers: vec!["composio-1".to_string()],
            allowed_tools: None,
            allowed_connections: Vec::new(),
        },
        approval_policy: None,
    };

    let prompt = render_automation_v2_prompt(
        &automation,
        "/tmp",
        "run-email",
        &node,
        1,
        &agent,
        &[json!({
            "alias": "report_body",
            "from_step_id": "generate_report",
            "output": {
                "content": {
                    "path": ".tandem/artifacts/generate-report.html",
                    "text": "<h1>Tandem Strategic Analysis</h1><p>Rich upstream report body.</p>"
                }
            }
        })],
        &["*".to_string()],
        None,
        None,
        None,
    );

    assert!(prompt.contains("Delivery target:"));
    assert!(prompt.contains("`recipient@example.com`"));
    assert!(prompt.contains("Inline body only: `true`"));
    assert!(prompt.contains("Upstream synthesis rules:"));
    assert!(
        prompt.contains("use the compiled upstream report/body as the email body source of truth")
    );
    assert!(prompt.contains("For email delivery, use the compiled upstream report/body as the email body source of truth."));
    assert!(prompt.contains("Deterministic Delivery Body:"));
    assert!(prompt.contains("Source artifact:"));
    assert!(prompt.contains("generate-report.html"));
    assert!(prompt.contains("<h1>Tandem Strategic Analysis</h1>"));
    assert!(prompt.contains(
        "Do not mark the node completed unless you actually execute an email draft or send tool."
    ));
}

#[test]
fn prompt_compacts_upstream_outputs_for_downstream_nodes() {
    let automation = AutomationV2Spec {
        automation_id: "automation-compact-upstream".to_string(),
        name: "Compact Upstream Automation".to_string(),
        description: None,
        status: crate::AutomationV2Status::Active,
        schedule: crate::AutomationV2Schedule {
            schedule_type: crate::AutomationV2ScheduleType::Manual,
            cron_expression: None,
            interval_seconds: None,
            timezone: "UTC".to_string(),
            misfire_policy: crate::RoutineMisfirePolicy::RunOnce,
        },
        knowledge: tandem_orchestrator::KnowledgeBinding::default(),
        agents: Vec::new(),
        flow: crate::AutomationFlowSpec { nodes: Vec::new() },
        execution: crate::AutomationExecutionPolicy {
            profile: None,
            max_parallel_agents: Some(1),
            max_total_runtime_ms: None,
            max_total_tool_calls: None,
            max_total_tokens: None,
            max_total_cost_usd: None,
        },
        output_targets: Vec::new(),
        created_at_ms: 0,
        updated_at_ms: 0,
        creator_id: "test".to_string(),
        workspace_root: Some("/tmp".to_string()),
        metadata: None,
        next_fire_at_ms: None,
        last_fired_at_ms: None,
        scope_policy: None,
        watch_conditions: Vec::new(),
        handoff_config: None,
    };
    let node = AutomationFlowNode {
        knowledge: tandem_orchestrator::KnowledgeBinding::default(),
        node_id: "analyze_findings".to_string(),
        agent_id: "analyst".to_string(),
        objective: "Synthesize the clustered themes into a concise analysis and recommendations."
            .to_string(),
        depends_on: vec!["cluster_topics".to_string()],
        input_refs: vec![AutomationFlowInputRef {
            from_step_id: "cluster_topics".to_string(),
            alias: "topic_clusters".to_string(),
        }],
        output_contract: Some(AutomationFlowOutputContract {
            kind: "structured_json".to_string(),
            validator: Some(crate::AutomationOutputValidatorKind::StructuredJson),
            enforcement: None,
            schema: None,
            summary_guidance: None,
        }),
        tool_policy: None,
        mcp_policy: None,
        retry_policy: None,
        timeout_ms: None,
        max_tool_calls: None,
        stage_kind: None,
        gate: None,
        metadata: None,
    };
    let agent = AutomationAgentProfile {
        agent_id: "analyst".to_string(),
        template_id: None,
        display_name: "Analyst".to_string(),
        avatar_url: None,
        model_policy: None,
        skills: Vec::new(),
        tool_policy: crate::AutomationAgentToolPolicy {
            allowlist: vec!["*".to_string()],
            denylist: Vec::new(),
        },
        mcp_policy: crate::AutomationAgentMcpPolicy {
            allowed_servers: Vec::new(),
            allowed_tools: None,
            allowed_connections: Vec::new(),
        },
        approval_policy: None,
    };
    let upstream_inputs = vec![json!({
        "alias": "topic_clusters",
        "from_step_id": "cluster_topics",
        "output": {
            "status": "completed",
            "phase": "completed",
            "summary": "Clustered pain points into report themes.",
            "contract_kind": "structured_json",
            "artifact_validation": {
                "accepted_artifact_path": ".tandem/artifacts/cluster-topics.json",
                "artifact_candidates": [{"source": "verified_output", "score": 999}],
                "validation_outcome": "passed",
                "warning_count": 0
            },
            "validator_summary": {
                "kind": "structured_json",
                "outcome": "passed",
                "warning_count": 0
            },
            "tool_telemetry": {
                "executed_tools": ["read", "write"],
                "tool_call_counts": {"read": 1, "write": 1}
            },
            "content": {
                "path": ".tandem/artifacts/cluster-topics.json",
                "raw_assistant_text": "very verbose narrative",
                "text": "{\"themes\":[{\"id\":\"T1\",\"summary\":\"alpha\"}],\"cross_cutting_observation\":\"beta\"}"
            }
        }
    })];

    let prompt = render_automation_v2_prompt(
        &automation,
        "/tmp",
        "run-compact",
        &node,
        1,
        &agent,
        &upstream_inputs,
        &["*".to_string()],
        None,
        None,
        None,
    );

    assert!(prompt.contains("\"themes\""));
    assert!(prompt.contains("\"cross_cutting_observation\""));
    assert!(!prompt.contains("artifact_candidates"));
    assert!(!prompt.contains("raw_assistant_text"));
    assert!(!prompt.contains("tool_call_counts"));
}

#[tokio::test]
async fn execute_collect_inputs_node_uses_deterministic_shortcut() {
    let workspace_root = std::env::temp_dir().join(format!(
        "tandem-collect-inputs-exec-{}",
        uuid::Uuid::new_v4()
    ));
    std::fs::create_dir_all(&workspace_root).expect("workspace");

    let automation = AutomationV2Spec {
        automation_id: "automation-inline-collect-inputs".to_string(),
        name: "Collect Inputs Shortcut".to_string(),
        description: None,
        status: crate::AutomationV2Status::Active,
        schedule: crate::AutomationV2Schedule {
            schedule_type: crate::AutomationV2ScheduleType::Manual,
            cron_expression: None,
            interval_seconds: None,
            timezone: "UTC".to_string(),
            misfire_policy: RoutineMisfirePolicy::RunOnce,
        },
        knowledge: tandem_orchestrator::KnowledgeBinding::default(),
        agents: vec![AutomationAgentProfile {
            agent_id: "agent_planner".to_string(),
            template_id: None,
            display_name: "Planner".to_string(),
            avatar_url: None,
            model_policy: Some(json!({
                "default_model": "openrouter/not-a-real-model"
            })),
            skills: Vec::new(),
            tool_policy: AutomationAgentToolPolicy {
                allowlist: vec!["*".to_string()],
                denylist: Vec::new(),
            },
            mcp_policy: AutomationAgentMcpPolicy {
                allowed_servers: Vec::new(),
                allowed_tools: None,
                allowed_connections: Vec::new(),
            },
            approval_policy: None,
        }],
        flow: AutomationFlowSpec {
            nodes: vec![AutomationFlowNode {
                knowledge: tandem_orchestrator::KnowledgeBinding::default(),
                node_id: "collect_inputs".to_string(),
                agent_id: "agent_planner".to_string(),
                objective: "Capture the report topic, delivery target, and formatting constraints."
                    .to_string(),
                depends_on: Vec::new(),
                input_refs: Vec::new(),
                output_contract: Some(AutomationFlowOutputContract {
                    kind: "brief".to_string(),
                    validator: Some(crate::AutomationOutputValidatorKind::GenericArtifact),
                    enforcement: None,
                    schema: None,
                    summary_guidance: None,
                }),
                tool_policy: None,
                mcp_policy: None,
                retry_policy: None,
                timeout_ms: None,
                max_tool_calls: None,
                stage_kind: None,
                gate: None,
                metadata: Some(json!({
                    "inputs": {
                        "topic": "autonomous AI agentic workflows",
                        "delivery_email": "recipient@example.com",
                        "email_format": "simple html",
                        "attachments_allowed": false
                    }
                })),
            }],
        },
        execution: AutomationExecutionPolicy {
            profile: None,
            max_parallel_agents: Some(1),
            max_total_runtime_ms: None,
            max_total_tool_calls: None,
            max_total_tokens: None,
            max_total_cost_usd: None,
        },
        output_targets: Vec::new(),
        created_at_ms: crate::now_ms(),
        updated_at_ms: crate::now_ms(),
        creator_id: "test".to_string(),
        workspace_root: Some(workspace_root.to_string_lossy().to_string()),
        metadata: Some(json!({
            "context_materialization": {
                "routines": [
                    {
                        "routine_id": "collect_inputs",
                        "visible_context_objects": [],
                        "step_context_bindings": [
                            {
                                "step_id": "collect_inputs",
                                "context_reads": ["ctx:collect_inputs:mission.goal"],
                                "context_writes": []
                            }
                        ]
                    }
                ]
            }
        })),
        next_fire_at_ms: None,
        last_fired_at_ms: None,
        scope_policy: None,
        watch_conditions: Vec::new(),
        handoff_config: None,
    };

    let state = ready_test_state().await;
    let run = state
        .create_automation_v2_run(&automation, "manual")
        .await
        .expect("create run");
    assert_eq!(
        run.runtime_context
            .as_ref()
            .map(|context| context.routines.len()),
        Some(1)
    );
    state
        .update_automation_v2_run(&run.run_id, |row| {
            row.runtime_context = None;
        })
        .await
        .expect("clear runtime context");
    let claimed = state
        .claim_specific_automation_v2_run(&run.run_id)
        .await
        .expect("claim run");
    assert_eq!(
        claimed
            .runtime_context
            .as_ref()
            .map(|context| context.routines.len()),
        Some(1)
    );
    let node = automation.flow.nodes.first().expect("collect_inputs node");
    let agent = automation.agents.first().expect("planner agent");

    let output = execute_automation_v2_node(&state, &claimed.run_id, &automation, node, agent)
        .await
        .expect("execute collect_inputs");

    assert_eq!(
        output.get("status").and_then(Value::as_str),
        Some("completed")
    );
    assert_eq!(
        output
            .get("artifact_validation")
            .and_then(|value| value.get("deterministic_artifact"))
            .and_then(Value::as_bool),
        Some(true)
    );
    assert_eq!(
        output
            .get("artifact_validation")
            .and_then(|value| value.get("deterministic_source"))
            .and_then(Value::as_str),
        Some("node_metadata_inputs")
    );

    let artifact_path = workspace_root
        .join(".tandem/runs")
        .join(&claimed.run_id)
        .join("artifacts")
        .join("collect-inputs.json");
    assert!(artifact_path.exists());
    let artifact_text = std::fs::read_to_string(&artifact_path).expect("artifact text");
    assert!(artifact_text.contains("autonomous AI agentic workflows"));

    let session_id = output
        .get("content")
        .and_then(|value| value.get("session_id"))
        .and_then(Value::as_str)
        .expect("session id");
    let session = state
        .storage
        .get_session(session_id)
        .await
        .expect("deterministic session");
    assert!(session.messages.iter().all(|message| {
        message
            .parts
            .iter()
            .all(|part| !matches!(part, tandem_types::MessagePart::ToolInvocation { .. }))
    }));

    let _ = std::fs::remove_dir_all(&workspace_root);
}

// TAN-600: the workflow-artifact data-boundary guard must fire on exactly the
// artifact the email-delivery prompt fold embeds, with the audit-safe event
// shape shared with the other source guards. Env-touching, so it joins the
// DEFAULT serial group used by the other data-boundary tests.
#[test]
#[serial_test::serial]
fn workflow_artifact_guard_covers_the_folded_delivery_artifact() {
    let previous_mode = std::env::var("TANDEM_DATA_BOUNDARY_MODE").ok();
    std::env::set_var("TANDEM_DATA_BOUNDARY_MODE", "audit");

    let automation = AutomationV2Spec {
        automation_id: "automation-artifact-guard".to_string(),
        name: "Artifact Guard Automation".to_string(),
        description: None,
        status: crate::AutomationV2Status::Active,
        schedule: crate::AutomationV2Schedule {
            schedule_type: crate::AutomationV2ScheduleType::Manual,
            cron_expression: None,
            interval_seconds: None,
            timezone: "UTC".to_string(),
            misfire_policy: crate::RoutineMisfirePolicy::RunOnce,
        },
        knowledge: tandem_orchestrator::KnowledgeBinding::default(),
        agents: Vec::new(),
        flow: crate::AutomationFlowSpec { nodes: Vec::new() },
        execution: crate::AutomationExecutionPolicy {
            profile: None,
            max_parallel_agents: Some(1),
            max_total_runtime_ms: None,
            max_total_tool_calls: None,
            max_total_tokens: None,
            max_total_cost_usd: None,
        },
        output_targets: Vec::new(),
        created_at_ms: 0,
        updated_at_ms: 0,
        creator_id: "test".to_string(),
        workspace_root: Some("/tmp".to_string()),
        metadata: None,
        next_fire_at_ms: None,
        last_fired_at_ms: None,
        scope_policy: None,
        watch_conditions: Vec::new(),
        handoff_config: None,
    };
    let node = AutomationFlowNode {
        knowledge: tandem_orchestrator::KnowledgeBinding::default(),
        node_id: "notify_user".to_string(),
        agent_id: "committer".to_string(),
        objective: "Send the finalized report by email.".to_string(),
        depends_on: vec!["generate_report".to_string()],
        input_refs: vec![AutomationFlowInputRef {
            from_step_id: "generate_report".to_string(),
            alias: "report_body".to_string(),
        }],
        output_contract: None,
        tool_policy: None,
        mcp_policy: None,
        retry_policy: None,
        timeout_ms: None,
        max_tool_calls: None,
        stage_kind: None,
        gate: None,
        metadata: Some(json!({
            "delivery": {
                "method": "email",
                "to": "recipient@example.com",
                "content_type": "text/html",
                "inline_body_only": true,
                "attachments": false
            }
        })),
    };
    let secret = "sk-live-abcdef1234567890";
    // A clean decoy that out-lengths the sensitive artifact ONLY before
    // normalization: its text is JSON whose bulk is a `context_writes` blob
    // the prompt path strips. If the guard selected from pre-normalized
    // inputs it would scan this decoy, find nothing, and emit no event while
    // the prompt embeds the sensitive artifact (Codex P2 on PR #1789).
    let decoy_text = serde_json::to_string(&json!({
        "summary": "clean upstream summary",
        "context_writes": (0..40)
            .map(|index| format!("ctx:wfplan-decoy-{index}:node:artifact"))
            .collect::<Vec<_>>(),
    }))
    .expect("decoy json");
    let upstream_inputs = vec![
        json!({
            "alias": "report_body",
            "from_step_id": "generate_report",
            "output": {
                "content": {
                    "path": ".tandem/artifacts/generate-report.html",
                    "text": format!(
                        "<h1>Quarterly Report</h1><p>api_key={} must be rotated.</p>{}",
                        secret,
                        "<p>Body paragraph with enough length to win post-normalization.</p>"
                    )
                }
            }
        }),
        json!({
            "alias": "decoy_summary",
            "from_step_id": "collect_context",
            "output": {
                "content": {
                    "path": ".tandem/artifacts/decoy-summary.json",
                    "text": decoy_text
                }
            }
        }),
    ];
    let mut tenant = tandem_types::TenantContext::local_implicit();
    tenant.org_id = "org-artifact".to_string();
    tenant.workspace_id = "workspace-artifact".to_string();

    let event = crate::app::state::automation::prompting_impl::workflow_artifact_boundary_event(
        "/tmp",
        "run-artifact-guard",
        &node,
        &upstream_inputs,
        None,
        &tenant,
    )
    .expect("artifact with findings must produce a boundary event");
    assert_eq!(event.properties["sourceKind"], "workflow_artifact");
    assert_eq!(event.properties["runID"], "run-artifact-guard");
    assert_eq!(event.properties["auditOnly"], true);
    assert_eq!(event.properties["enforced"], false);
    assert_eq!(event.properties["tenant"]["organization_id"], "org-artifact");
    let serialized = serde_json::to_string(&event.properties).expect("json");
    assert!(
        !serialized.contains(secret),
        "guard event must carry safe evidence only: {serialized}"
    );

    // Anti-drift: the guard scanned the same artifact the fold embeds. The
    // selector is the single selection rule, and the rendered prompt labels
    // the same source artifact.
    let normalized =
        crate::app::state::automation::prompting_impl::automation_prompt_normalize_upstream_inputs(
            "/tmp",
            "run-artifact-guard",
            &upstream_inputs,
            None,
        );
    let (selected_text, selected_path) =
        crate::app::state::automation::prompting_impl::automation_prompt_select_delivery_artifact(
            &normalized,
        )
        .expect("selector must pick the delivery artifact");
    assert!(
        selected_text.contains(secret),
        "post-normalization selection must pick the sensitive artifact, not the decoy"
    );
    assert!(selected_path.contains("generate-report.html"));
    let agent = AutomationAgentProfile {
        agent_id: "committer".to_string(),
        template_id: None,
        display_name: "Committer".to_string(),
        avatar_url: None,
        model_policy: None,
        skills: Vec::new(),
        tool_policy: crate::AutomationAgentToolPolicy {
            allowlist: vec!["*".to_string()],
            denylist: Vec::new(),
        },
        mcp_policy: crate::AutomationAgentMcpPolicy {
            allowed_servers: Vec::new(),
            allowed_tools: None,
            allowed_connections: Vec::new(),
        },
        approval_policy: None,
    };
    let prompt = render_automation_v2_prompt(
        &automation,
        "/tmp",
        "run-artifact-guard",
        &node,
        1,
        &agent,
        &upstream_inputs,
        &["*".to_string()],
        None,
        None,
        None,
    );
    assert!(prompt.contains("Deterministic Delivery Body:"));
    assert!(
        prompt.contains(&selected_path),
        "fold must label the same artifact the guard scanned"
    );

    // Nodes without email delivery fold no artifact, so no event fires.
    let mut plain_node = node.clone();
    plain_node.metadata = None;
    assert!(
        crate::app::state::automation::prompting_impl::workflow_artifact_boundary_event(
            "/tmp",
            "run-artifact-guard",
            &plain_node,
            &upstream_inputs,
            None,
            &tenant,
        )
        .is_none()
    );

    // Boundary off: guard is silent even for email-delivery nodes.
    std::env::remove_var("TANDEM_DATA_BOUNDARY_MODE");
    assert!(
        crate::app::state::automation::prompting_impl::workflow_artifact_boundary_event(
            "/tmp",
            "run-artifact-guard",
            &node,
            &upstream_inputs,
            None,
            &tenant,
        )
        .is_none()
    );
    if let Some(previous) = previous_mode {
        std::env::set_var("TANDEM_DATA_BOUNDARY_MODE", previous);
    }
}