tandem-server 0.4.23

HTTP server for Tandem engine APIs
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
use super::*;

fn sample_blueprint() -> Value {
    json!({
        "mission_id": "mission-preview",
        "title": "Competitive analysis mission",
        "goal": "Produce a cross-functional brief",
        "success_criteria": ["Useful brief", "Actionable recommendations"],
        "shared_context": "Targeting a Q2 planning review.",
        "workspace_root": "/tmp/mission-workspace",
        "orchestrator_template_id": "orchestrator-default",
        "team": {
            "allowed_mcp_servers": ["github"],
            "max_parallel_agents": 3,
            "orchestrator_only_tool_calls": false
        },
        "workstreams": [
            {
                "workstream_id": "research",
                "title": "Research",
                "objective": "Collect competitor signals",
                "role": "researcher",
                "prompt": "Research the market and summarize competitors.",
                "depends_on": [],
                "input_refs": [],
                "output_contract": { "kind": "report_markdown" }
            },
            {
                "workstream_id": "synthesis",
                "title": "Synthesis",
                "objective": "Turn findings into recommendations",
                "role": "analyst",
                "prompt": "Synthesize the findings into a brief.",
                "depends_on": ["research"],
                "input_refs": [{ "from_step_id": "research", "alias": "research_report" }],
                "output_contract": { "kind": "report_markdown" }
            }
        ],
        "review_stages": [
            {
                "stage_id": "approval",
                "stage_kind": "approval",
                "title": "Human approval",
                "target_ids": ["synthesis"],
                "prompt": "",
                "checklist": [],
                "gate": {
                    "required": true,
                    "decisions": ["approve", "rework", "cancel"],
                    "rework_targets": ["synthesis"]
                }
            }
        ]
    })
}

#[tokio::test]
async fn mission_builder_generate_draft_returns_generated_blueprint_and_schedule() {
    let state = test_state().await;
    let app = app_router(state);
    let response = json!({
        "blueprint": {
            "mission_id": "",
            "title": "Weekly release mission",
            "goal": "Prepare a weekly release readiness package",
            "success_criteria": ["Includes risks", "Includes owner and date"],
            "shared_context": "Audience is engineering leadership.",
            "workspace_root": "/tmp/ignored-by-server",
            "workstreams": [
                {
                    "workstream_id": "collect",
                    "title": "Collect release inputs",
                    "objective": "Collect status, blockers, and dependencies",
                    "role": "analyst",
                    "prompt": "Gather current release inputs and summarize them.",
                    "depends_on": [],
                    "input_refs": [],
                    "output_contract": { "kind": "report_markdown" }
                },
                {
                    "workstream_id": "review",
                    "title": "Review release risk",
                    "objective": "Review the release packet and flag risks",
                    "role": "reviewer",
                    "prompt": "Review the release packet and highlight actionable risks.",
                    "depends_on": ["collect"],
                    "input_refs": [{ "from_step_id": "collect", "alias": "release_packet" }],
                    "output_contract": { "kind": "report_markdown" }
                },
                {
                    "workstream_id": "publish",
                    "title": "Publish readiness packet",
                    "objective": "Publish the final release readiness update",
                    "role": "operator",
                    "prompt": "Publish the approved release readiness packet.",
                    "depends_on": ["review"],
                    "input_refs": [{ "from_step_id": "review", "alias": "risk_review" }],
                    "output_contract": { "kind": "report_markdown" }
                }
            ],
            "review_stages": []
        },
        "suggested_schedule": {
            "type": "cron",
            "cron_expression": "0 9 * * 1",
            "timezone": "UTC"
        },
        "generation_warnings": ["verify the release audience before publishing"]
    });

    let previous = std::env::var("TANDEM_MISSION_BUILDER_TEST_GENERATE_RESPONSE").ok();
    std::env::set_var(
        "TANDEM_MISSION_BUILDER_TEST_GENERATE_RESPONSE",
        response.to_string(),
    );
    let resp = app
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/mission-builder/generate-draft")
                .header("content-type", "application/json")
                .body(Body::from(
                    json!({
                        "intent": "Every Monday prepare a release readiness packet for leadership.",
                        "workspace_root": "/tmp/mission-workspace"
                    })
                    .to_string(),
                ))
                .expect("request"),
        )
        .await
        .expect("response");
    if let Some(previous) = previous {
        std::env::set_var("TANDEM_MISSION_BUILDER_TEST_GENERATE_RESPONSE", previous);
    } else {
        std::env::remove_var("TANDEM_MISSION_BUILDER_TEST_GENERATE_RESPONSE");
    }

    assert_eq!(resp.status(), StatusCode::OK);
    let body = to_bytes(resp.into_body(), usize::MAX).await.expect("body");
    let payload: Value = serde_json::from_slice(&body).expect("json");
    assert_eq!(
        payload
            .get("blueprint")
            .and_then(|row| row.get("workspace_root"))
            .and_then(Value::as_str),
        Some("/tmp/mission-workspace")
    );
    assert_eq!(
        payload
            .get("blueprint")
            .and_then(|row| row.get("mission_id"))
            .and_then(Value::as_str)
            .map(|value| value.starts_with("mission_")),
        Some(true)
    );
    assert_eq!(
        payload
            .get("suggested_schedule")
            .and_then(|row| row.get("type"))
            .and_then(Value::as_str),
        Some("cron")
    );
    assert_eq!(
        payload
            .get("validation")
            .and_then(Value::as_array)
            .map(|rows| rows.is_empty()),
        Some(true)
    );
    assert_eq!(
        payload
            .get("generation_warnings")
            .and_then(Value::as_array)
            .map(|rows| rows.len()),
        Some(1)
    );
}

#[tokio::test]
async fn mission_builder_preview_returns_compiled_automation() {
    let state = test_state().await;
    let app = app_router(state);
    let resp = app
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/mission-builder/compile-preview")
                .header("content-type", "application/json")
                .body(Body::from(
                    json!({ "blueprint": sample_blueprint() }).to_string(),
                ))
                .expect("request"),
        )
        .await
        .expect("response");
    assert_eq!(resp.status(), StatusCode::OK);
    let body = to_bytes(resp.into_body(), usize::MAX).await.expect("body");
    let payload: Value = serde_json::from_slice(&body).expect("json");
    assert_eq!(
        payload
            .get("automation")
            .and_then(|row| row.get("status"))
            .and_then(Value::as_str),
        Some("draft")
    );
    assert_eq!(
        payload
            .get("automation")
            .and_then(|row| row.get("knowledge"))
            .and_then(|row| row.get("reuse_mode"))
            .and_then(Value::as_str),
        Some("preflight")
    );
    assert_eq!(
        payload
            .get("automation")
            .and_then(|row| row.get("knowledge"))
            .and_then(|row| row.get("read_spaces"))
            .and_then(Value::as_array)
            .map(|rows| rows.len()),
        Some(1)
    );
    assert_eq!(
        payload
            .get("automation")
            .and_then(|row| row.get("flow"))
            .and_then(|row| row.get("nodes"))
            .and_then(Value::as_array)
            .map(|rows| rows.len()),
        Some(3)
    );
    let nodes = payload
        .get("automation")
        .and_then(|row| row.get("flow"))
        .and_then(|row| row.get("nodes"))
        .and_then(Value::as_array)
        .expect("nodes");
    let research = nodes
        .iter()
        .find(|row| row.get("node_id").and_then(Value::as_str) == Some("research"))
        .expect("research node");
    assert_eq!(
        research
            .get("knowledge")
            .and_then(|row| row.get("trust_floor"))
            .and_then(Value::as_str),
        Some("promoted")
    );
    assert_eq!(
        research
            .get("knowledge")
            .and_then(|row| row.get("read_spaces"))
            .and_then(Value::as_array)
            .map(|rows| rows.len()),
        Some(1)
    );
    assert_eq!(
        research
            .get("output_contract")
            .and_then(|row| row.get("validator"))
            .and_then(Value::as_str),
        Some("generic_artifact")
    );
    let approval = nodes
        .iter()
        .find(|row| row.get("node_id").and_then(Value::as_str) == Some("approval"))
        .expect("approval node");
    assert_eq!(
        approval
            .get("output_contract")
            .and_then(|row| row.get("validator"))
            .and_then(Value::as_str),
        Some("review_decision")
    );
}

#[tokio::test]
async fn mission_builder_apply_persists_draft_automation() {
    let state = test_state().await;
    let app = app_router(state.clone());
    let resp = app
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/mission-builder/apply")
                .header("content-type", "application/json")
                .body(Body::from(
                    json!({
                        "blueprint": sample_blueprint(),
                        "creator_id": "desktop"
                    })
                    .to_string(),
                ))
                .expect("request"),
        )
        .await
        .expect("response");
    assert_eq!(resp.status(), StatusCode::OK);
    let body = to_bytes(resp.into_body(), usize::MAX).await.expect("body");
    let payload: Value = serde_json::from_slice(&body).expect("json");
    let automation_id = 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");
    assert_eq!(stored.status, crate::AutomationV2Status::Draft);
    assert_eq!(
        stored
            .metadata
            .as_ref()
            .and_then(|row| row.get("builder_kind"))
            .and_then(Value::as_str),
        Some("mission_blueprint")
    );
    assert_eq!(stored.knowledge.reuse_mode.to_string(), "preflight");
    assert_eq!(stored.knowledge.read_spaces.len(), 1);
}

#[tokio::test]
async fn mission_builder_apply_preserves_research_web_expectation_metadata() {
    let state = test_state().await;
    let app = app_router(state.clone());
    let mut blueprint = sample_blueprint();
    let workstreams = blueprint
        .get_mut("workstreams")
        .and_then(Value::as_array_mut)
        .expect("workstreams");
    workstreams[0] = json!({
        "workstream_id": "research",
        "title": "Research",
        "objective": "Research the latest competitor signals",
        "role": "researcher",
        "prompt": "Research the latest competitor news and produce a citation-backed brief.",
        "depends_on": [],
        "input_refs": [],
        "output_contract": { "kind": "brief" },
        "metadata": {
            "builder": {
                "web_research_expected": true,
                "note": "require current web coverage"
            }
        }
    });

    let resp = app
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/mission-builder/apply")
                .header("content-type", "application/json")
                .body(Body::from(
                    json!({
                        "blueprint": blueprint,
                        "creator_id": "desktop"
                    })
                    .to_string(),
                ))
                .expect("request"),
        )
        .await
        .expect("response");
    assert_eq!(resp.status(), StatusCode::OK);
    let body = to_bytes(resp.into_body(), usize::MAX).await.expect("body");
    let payload: Value = serde_json::from_slice(&body).expect("json");
    let automation_id = 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");
    let research = stored
        .flow
        .nodes
        .iter()
        .find(|node| node.node_id == "research")
        .expect("research node");
    assert_eq!(
        research
            .metadata
            .as_ref()
            .and_then(|metadata| metadata.get("builder"))
            .and_then(|builder| builder.get("web_research_expected"))
            .and_then(Value::as_bool),
        Some(true)
    );
    assert_eq!(
        research
            .metadata
            .as_ref()
            .and_then(|metadata| metadata.get("builder"))
            .and_then(|builder| builder.get("note"))
            .and_then(Value::as_str),
        Some("require current web coverage")
    );
    assert_eq!(
        research
            .output_contract
            .as_ref()
            .and_then(|contract| contract.validator),
        Some(crate::AutomationOutputValidatorKind::ResearchBrief)
    );
}