tandem-server 0.7.0

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
// Copyright (c) 2026 Frumu LTD
// Licensed under the Business Source License 1.1

#[tokio::test]
#[serial_test::serial]
async fn workflow_plan_apply_can_materialize_a_disabled_draft_with_planner_metadata() {
    let state = test_state().await;
    configure_openai_provider(&state).await;
    let app = app_router(state.clone());
    let _guard = PlannerEnvGuard::new(&[
        "TANDEM_WORKFLOW_PLANNER_TEST_BUILD_RESPONSE",
        "TANDEM_WORKFLOW_PLANNER_TEST_RESPONSE",
    ]);
    _guard.set(
        "TANDEM_WORKFLOW_PLANNER_TEST_BUILD_RESPONSE",
        json!({
            "action": "build",
            "plan": llm_plan_json(
                "Comparison Workflow",
                "Collect inputs, compare them, and produce a report.",
                manual_schedule_json(),
                "/tmp/ignored",
                vec![
                    step_json("collect_inputs", "collect", "Gather inputs.", &[], "researcher", json!([]), "structured_json"),
                    step_json("compare_results", "compare", "Compare them.", &["collect_inputs"], "analyst", json!([
                        {"from_step_id":"collect_inputs","alias":"comparison_inputs"}
                    ]), "structured_json"),
                    step_json("generate_report", "report", "Generate the report.", &["compare_results"], "writer", json!([
                        {"from_step_id":"compare_results","alias":"comparison_findings"}
                    ]), "report_markdown")
                ],
                Some(json!({
                    "execution_mode": "swarm",
                    "max_parallel_agents": 6,
                    "model_provider": "openai",
                    "model_id": "gpt-5.1",
                    "role_models": {
                        "planner": {
                            "provider_id": "openai",
                            "model_id": "gpt-5.1"
                        }
                    }
                }))
            )
        })
        .to_string(),
    );

    let preview_resp = app
        .clone()
        .oneshot(preview_request(json!({
            "prompt": "Compare two competitor summaries and generate a report",
            "plan_source": "automations_page",
            "allowed_mcp_servers": ["slack", "github", "github"],
            "workspace_root": "/tmp/custom-workspace",
            "operator_preferences": {
                "execution_mode": "swarm",
                "max_parallel_agents": 6,
                "model_provider": "openai",
                "model_id": "gpt-5.1",
                "role_models": {
                    "planner": {
                        "provider_id": "openai",
                        "model_id": "gpt-5.1"
                    }
                }
            }
        })))
        .await
        .expect("preview response");
    assert_eq!(preview_resp.status(), StatusCode::OK);
    let preview_body = to_bytes(preview_resp.into_body(), usize::MAX)
        .await
        .expect("preview body");
    let preview_payload: Value = serde_json::from_slice(&preview_body).expect("preview json");
    let plan_id = preview_payload
        .get("plan")
        .and_then(|plan| plan.get("plan_id"))
        .and_then(Value::as_str)
        .expect("plan id");

    let mut apply_req = Request::builder()
        .method("POST")
        .uri("/workflow-plans/apply")
        .header("x-tandem-org-id", "org-a")
        .header("x-tandem-workspace-id", "workspace-a")
        .header("x-tandem-actor-id", "user-a")
        .header("content-type", "application/json")
        .body(Body::from(
            json!({
                "plan_id": plan_id,
                "creator_id": "control-panel",
                "materialize_as_draft": true,
                "idempotency_key": "workflow-apply-draft-1"
            })
            .to_string(),
        ))
        .expect("apply request");
    apply_req.extensions_mut().insert(verified_workflow_plan_context(
        tandem_types::TenantContext::explicit("org-a", "workspace-a", None),
        "user-a",
    ));
    let apply_resp = app
        .clone()
        .oneshot(apply_req)
        .await
        .expect("apply response");
    let apply_status = apply_resp.status();
    let apply_body = to_bytes(apply_resp.into_body(), usize::MAX)
        .await
        .expect("apply body");
    assert_eq!(apply_status, StatusCode::OK);
    let apply_payload: Value = serde_json::from_slice(&apply_body).expect("apply json");
    let automation_id = apply_payload
        .get("automation")
        .and_then(|row| row.get("automation_id"))
        .and_then(Value::as_str)
        .expect("automation id");
    let stored = state
        .get_automation_v2(automation_id)
        .await
        .expect("stored automation");
    assert_eq!(stored.status, crate::AutomationV2Status::Draft);
    assert_eq!(stored.next_fire_at_ms, None);
    let tenant = stored.tenant_context();
    assert_eq!(tenant.org_id, "org-a");
    assert_eq!(tenant.workspace_id, "workspace-a");
    assert_eq!(tenant.actor_id.as_deref(), Some("user-a"));
    assert_eq!(stored.creator_id, "user-a");
    assert_eq!(
        stored
            .metadata
            .as_ref()
            .and_then(|row| row.get("authoring_actor_id"))
            .and_then(Value::as_str),
        Some("user-a")
    );
    assert_eq!(
        stored
            .metadata
            .as_ref()
            .and_then(|row| row.get("requested_creator_id"))
            .and_then(Value::as_str),
        Some("control-panel")
    );
    assert_eq!(
        stored.workspace_root.as_deref(),
        Some("/tmp/custom-workspace")
    );
    assert_eq!(
        stored
            .metadata
            .as_ref()
            .and_then(|row| row.get("plan_source"))
            .and_then(Value::as_str),
        Some("automations_page")
    );
    assert!(
        stored
            .metadata
            .as_ref()
            .and_then(|row| row.get("plan_package_bundle"))
            .is_some(),
        "plan package bundle should be stored on the automation snapshot"
    );
    assert!(
        stored
            .metadata
            .as_ref()
            .and_then(|row| row.get("plan_package"))
            .is_some(),
        "plan package should be stored on the automation snapshot"
    );
    assert_eq!(
        stored
            .metadata
            .as_ref()
            .and_then(|row| row.get("plan_package"))
            .and_then(|row| row.get("plan_revision"))
            .and_then(Value::as_u64),
        Some(1)
    );
    assert!(
        stored
            .metadata
            .as_ref()
            .and_then(|row| row.get("plan_package_validation"))
            .is_some(),
        "plan package validation should be stored on the automation snapshot"
    );
    assert!(
        stored
            .metadata
            .as_ref()
            .and_then(|row| row.get("approved_plan_materialization"))
            .is_some(),
        "approved plan materialization should be stored on the automation snapshot"
    );
    assert!(
        stored
            .metadata
            .as_ref()
            .and_then(|row| row.get("planner_diagnostics"))
            .is_some(),
        "planner diagnostics should be present on the automation snapshot"
    );
    assert!(apply_payload.get("plan_package_bundle").is_some());
    assert!(apply_payload.get("approved_plan_materialization").is_some());
    let stored_draft = state.get_workflow_plan_draft(plan_id).await.expect("draft");
    assert!(stored_draft.last_success_materialization.is_some());
    assert_eq!(
        stored_draft
            .last_success_materialization
            .as_ref()
            .and_then(|value| value.get("plan_id"))
            .and_then(Value::as_str),
        Some(plan_id)
    );
    assert_eq!(
        stored
            .metadata
            .as_ref()
            .and_then(|row| row.get("approved_plan_materialization"))
            .and_then(|row| row.get("plan_id"))
            .and_then(Value::as_str),
        Some(plan_id)
    );
    let mut replay_req = Request::builder()
        .method("POST")
        .uri("/workflow-plans/apply")
        .header("x-tandem-org-id", "org-a")
        .header("x-tandem-workspace-id", "workspace-a")
        .header("x-tandem-actor-id", "user-a")
        .header("content-type", "application/json")
        .body(Body::from(
            json!({
                "plan_id": plan_id,
                "creator_id": "control-panel",
                "materialize_as_draft": true,
                "idempotency_key": "workflow-apply-draft-1"
            })
            .to_string(),
        ))
        .expect("replay apply request");
    replay_req
        .extensions_mut()
        .insert(verified_workflow_plan_context(
            tandem_types::TenantContext::explicit("org-a", "workspace-a", None),
            "user-a",
        ));
    let replay_resp = app
        .clone()
        .oneshot(replay_req)
        .await
        .expect("replay apply response");
    assert_eq!(replay_resp.status(), StatusCode::OK);
    let replay_body = to_bytes(replay_resp.into_body(), usize::MAX)
        .await
        .expect("replay apply body");
    let replay_payload: Value = serde_json::from_slice(&replay_body).expect("replay apply json");
    assert_eq!(
        replay_payload
            .pointer("/automation/automation_id")
            .and_then(Value::as_str),
        Some(automation_id)
    );
    assert_eq!(
        state
            .list_automations_v2()
            .await
            .into_iter()
            .filter(|automation| automation.automation_id == automation_id)
            .count(),
        1
    );
    let dry_run_req = Request::builder()
        .method("POST")
        .uri(format!("/automations/v2/{automation_id}/run_now"))
        .header("x-tandem-org-id", "org-a")
        .header("x-tandem-workspace-id", "workspace-a")
        .header("x-tandem-actor-id", "user-a")
        .header("content-type", "application/json")
        .body(Body::from(json!({"dry_run": true}).to_string()))
        .expect("dry run request");
    let dry_run_resp = app
        .clone()
        .oneshot(dry_run_req)
        .await
        .expect("dry run response");
    assert_eq!(dry_run_resp.status(), StatusCode::OK);
    let dry_run_body = to_bytes(dry_run_resp.into_body(), usize::MAX)
        .await
        .expect("dry run body");
    let dry_run_payload: Value = serde_json::from_slice(&dry_run_body).expect("dry run json");
    let dry_run_run_id = dry_run_payload
        .get("run")
        .and_then(|row| row.get("run_id"))
        .and_then(Value::as_str)
        .expect("dry run id");
    assert_eq!(
        dry_run_payload
            .get("run")
            .and_then(|row| row.get("trigger_type"))
            .and_then(Value::as_str),
        Some("manual_dry_run")
    );
    let stored_after_run_now = state
        .get_automation_v2(automation_id)
        .await
        .expect("stored automation after manual run");
    let expected_trigger_id = format!("manual-trigger-{dry_run_run_id}");
    let manual_trigger_record = stored_after_run_now
        .metadata
        .as_ref()
        .and_then(|row| row.get("plan_package"))
        .and_then(|row| row.get("manual_trigger_record"))
        .expect("manual trigger record");
    assert_eq!(
        manual_trigger_record
            .get("trigger_id")
            .and_then(Value::as_str),
        Some(expected_trigger_id.as_str())
    );
    assert_eq!(
        manual_trigger_record
            .get("triggered_by")
            .and_then(Value::as_str),
        Some("user-a")
    );
    assert_eq!(
        manual_trigger_record
            .get("trigger_source")
            .and_then(Value::as_str),
        Some("dry_run")
    );
    assert_eq!(
        manual_trigger_record
            .get("dry_run")
            .and_then(Value::as_bool),
        Some(true)
    );
    assert_eq!(
        dry_run_payload
            .get("run")
            .and_then(|row| row.get("automation_snapshot"))
            .and_then(|row| row.get("metadata"))
            .and_then(|row| row.get("plan_package"))
            .and_then(|row| row.get("manual_trigger_record"))
            .and_then(|row| row.get("run_id"))
            .and_then(Value::as_str),
        Some(dry_run_run_id)
    );
    let operator_agent = stored
        .agents
        .iter()
        .find(|agent| agent.agent_id == "agent_writer")
        .expect("writer agent");
    assert!(operator_agent
        .tool_policy
        .allowlist
        .contains(&"mcp.github.*".to_string()));
    assert!(operator_agent
        .tool_policy
        .allowlist
        .contains(&"mcp.slack.*".to_string()));
    assert!(stored
        .flow
        .nodes
        .iter()
        .any(|node| !node.input_refs.is_empty()));
}

#[tokio::test]
async fn workflow_plan_apply_rolls_back_when_protected_audit_persistence_fails() {
    let state = test_state().await;
    tokio::fs::create_dir_all(&state.protected_audit_path)
        .await
        .expect("make protected audit path unwritable as a file");
    crate::audit::reset_protected_audit_tail_for_test(&state.protected_audit_path).await;
    let app = app_router(state.clone());
    let plan = llm_plan_json(
        "Audited workflow",
        "Create a report only when the required audit can be persisted.",
        manual_schedule_json(),
        "/tmp/workspace",
        vec![step_json(
            "generate_report",
            "report",
            "Generate the report.",
            &[],
            "writer",
            json!([]),
            "report_markdown",
        )],
        None,
    );

    let response = app
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/workflow-plans/apply")
                .header("content-type", "application/json")
                .body(Body::from(
                    json!({
                        "plan": plan,
                        "creator_id": "control-panel",
                        "idempotency_key": "audit-rollback-test",
                    })
                    .to_string(),
                ))
                .expect("apply request"),
        )
        .await
        .expect("apply response");

    assert_eq!(response.status(), StatusCode::INTERNAL_SERVER_ERROR);
    let body = to_bytes(response.into_body(), usize::MAX)
        .await
        .expect("error body");
    let payload: Value = serde_json::from_slice(&body).expect("error json");
    assert_eq!(
        payload.get("code").and_then(Value::as_str),
        Some("PROTECTED_AUDIT_PERSISTENCE_FAILED")
    );
    assert_eq!(
        payload.get("operationApplied").and_then(Value::as_bool),
        Some(false)
    );
    assert!(
        state.list_automations_v2().await.is_empty(),
        "an unaudited workflow materialization must be rolled back"
    );
    assert!(
        state
            .get_idempotency_key(
                &tandem_types::TenantContext::local_implicit(),
                "workflow_plan.apply",
                "audit-rollback-test",
            )
            .await
            .is_none(),
        "a rolled-back apply must release its idempotency reservation"
    );
}

#[tokio::test]
async fn workflow_plan_apply_preserves_recovered_automation_when_audit_retry_fails() {
    let state = test_state().await;
    let app = app_router(state.clone());
    let request_body = json!({
        "plan": llm_plan_json(
            "Recovered audited workflow",
            "Preserve an existing materialization while retrying its audit.",
            manual_schedule_json(),
            "/tmp/workspace",
            vec![step_json(
                "generate_report",
                "report",
                "Generate the report.",
                &[],
                "writer",
                json!([]),
                "report_markdown",
            )],
            None,
        ),
        "creator_id": "control-panel",
        "idempotency_key": "audit-recovery-test",
    })
    .to_string();

    let initial_response = app
        .clone()
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/workflow-plans/apply")
                .header("content-type", "application/json")
                .body(Body::from(request_body.clone()))
                .expect("initial apply request"),
        )
        .await
        .expect("initial apply response");
    assert_eq!(initial_response.status(), StatusCode::OK);
    let initial_body = to_bytes(initial_response.into_body(), usize::MAX)
        .await
        .expect("initial apply body");
    let initial_payload: Value = serde_json::from_slice(&initial_body).expect("initial apply json");
    let automation_id = initial_payload
        .pointer("/automation/automation_id")
        .and_then(Value::as_str)
        .expect("automation id")
        .to_string();
    assert!(state.get_automation_governance(&automation_id).await.is_some());

    state.idempotency_keys.write().await.clear();
    tokio::fs::remove_file(&state.protected_audit_path)
        .await
        .expect("remove working protected audit file");
    tokio::fs::create_dir_all(&state.protected_audit_path)
        .await
        .expect("make protected audit path unwritable as a file");
    crate::audit::reset_protected_audit_tail_for_test(&state.protected_audit_path).await;

    let retry_response = app
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/workflow-plans/apply")
                .header("content-type", "application/json")
                .body(Body::from(request_body))
                .expect("retry apply request"),
        )
        .await
        .expect("retry apply response");
    assert_eq!(retry_response.status(), StatusCode::INTERNAL_SERVER_ERROR);
    let retry_body = to_bytes(retry_response.into_body(), usize::MAX)
        .await
        .expect("retry error body");
    let retry_payload: Value = serde_json::from_slice(&retry_body).expect("retry error json");
    assert_eq!(
        retry_payload.get("code").and_then(Value::as_str),
        Some("PROTECTED_AUDIT_PERSISTENCE_FAILED")
    );
    assert_eq!(
        retry_payload
            .get("operationApplied")
            .and_then(Value::as_bool),
        Some(true)
    );
    assert_eq!(
        retry_payload.get("retryable").and_then(Value::as_bool),
        Some(true)
    );
    assert!(
        state.get_automation_v2(&automation_id).await.is_some(),
        "an existing recovered automation must not be rolled back by a retry"
    );
    assert!(
        state.get_automation_governance(&automation_id).await.is_some(),
        "an existing recovered automation must retain its governance record"
    );
    assert!(
        state
            .get_idempotency_key(
                &tandem_types::TenantContext::local_implicit(),
                "workflow_plan.apply",
                "audit-recovery-test",
            )
            .await
            .is_none(),
        "the failed audit retry must release its idempotency reservation"
    );
}