tandem-server 0.6.5

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
#[derive(Debug, Clone)]
struct FakeIncidentMonitorWebhookRequest {
    headers: std::collections::BTreeMap<String, String>,
    body: Vec<u8>,
}

async fn spawn_fake_incident_monitor_webhook_server(
    statuses: Vec<u16>,
    delay_ms: u64,
) -> (
    String,
    Arc<RwLock<Vec<FakeIncidentMonitorWebhookRequest>>>,
    tokio::task::JoinHandle<()>,
) {
    let listener = TcpListener::bind("127.0.0.1:0")
        .await
        .expect("bind fake incident monitor webhook listener");
    let addr = listener
        .local_addr()
        .expect("fake incident monitor webhook addr");
    let requests = Arc::new(RwLock::new(Vec::<FakeIncidentMonitorWebhookRequest>::new()));
    let statuses = Arc::new(RwLock::new(if statuses.is_empty() {
        vec![202]
    } else {
        statuses
    }));
    let app = axum::Router::new().route(
        "/incident",
        axum::routing::post({
            let requests = requests.clone();
            let statuses = statuses.clone();
            move |headers: axum::http::HeaderMap, body: axum::body::Bytes| {
                let requests = requests.clone();
                let statuses = statuses.clone();
                async move {
                    let header_snapshot = headers
                        .iter()
                        .filter_map(|(name, value)| {
                            value.to_str().ok().map(|value| {
                                (name.as_str().to_ascii_lowercase(), value.to_string())
                            })
                        })
                        .collect::<std::collections::BTreeMap<_, _>>();
                    requests.write().await.push(FakeIncidentMonitorWebhookRequest {
                        headers: header_snapshot,
                        body: body.to_vec(),
                    });
                    if delay_ms > 0 {
                        tokio::time::sleep(std::time::Duration::from_millis(delay_ms)).await;
                    }
                    let status = {
                        let mut rows = statuses.write().await;
                        if rows.len() > 1 {
                            rows.remove(0)
                        } else {
                            rows.first().copied().unwrap_or(202)
                        }
                    };
                    (
                        axum::http::StatusCode::from_u16(status).expect("fake webhook status"),
                        "ok",
                    )
                }
            }
        }),
    );
    let server = tokio::spawn(async move {
        axum::serve(listener, app)
            .await
            .expect("serve fake incident monitor webhook");
    });
    (format!("http://{addr}/incident"), requests, server)
}

async fn configure_webhook_incident_monitor_destination(
    state: &AppState,
    endpoint: String,
    destination_config: Value,
) {
    std::env::set_var(
        "TANDEM_TEST_INCIDENT_MONITOR_WEBHOOK_SECRET",
        "test-webhook-signing-secret",
    );
    state
        .put_incident_monitor_config(crate::IncidentMonitorConfig {
            enabled: true,
            repo: Some("acme/platform".to_string()),
            workspace_root: Some("/tmp/acme".to_string()),
            destinations: vec![crate::IncidentMonitorDestinationConfig {
                destination_id: "webhook-primary".to_string(),
                name: "Primary webhook".to_string(),
                kind: crate::IncidentMonitorDestinationKind::Webhook,
                webhook_url: Some(endpoint),
                webhook_secret_ref: Some("env:TANDEM_TEST_INCIDENT_MONITOR_WEBHOOK_SECRET".to_string()),
                config: Some(destination_config),
                ..Default::default()
            }],
            default_destination_ids: vec!["webhook-primary".to_string()],
            ..Default::default()
        })
        .await
        .expect("config");
}

async fn publish_incident_monitor_webhook_draft(
    app: axum::Router,
    draft_id: &str,
) -> (StatusCode, Value) {
    let publish_req = Request::builder()
        .method("POST")
        .uri(format!("/incident-monitor/drafts/{draft_id}/publish"))
        .body(Body::empty())
        .expect("publish request");
    let publish_resp = app.oneshot(publish_req).await.expect("publish response");
    let status = publish_resp.status();
    let body = to_bytes(publish_resp.into_body(), usize::MAX)
        .await
        .expect("publish body");
    (
        status,
        serde_json::from_slice(&body)
            .unwrap_or_else(|_| panic!("{}", String::from_utf8_lossy(&body))),
    )
}

fn assert_tandem_webhook_signature(
    headers: &std::collections::BTreeMap<String, String>,
    body: &[u8],
) {
    let signature = headers.get("x-tandem-signature").expect("signature header");
    let timestamp = signature
        .strip_prefix("t=")
        .and_then(|rest| rest.split_once(",v1=").map(|(timestamp, _)| timestamp))
        .and_then(|value| value.parse::<u64>().ok())
        .expect("signature timestamp");
    let expected = crate::app::state::automation_webhook_signature_header(
        "test-webhook-signing-secret",
        timestamp,
        body,
    );
    assert_eq!(signature, &expected);
    assert_eq!(
        headers.get("x-tandem-signature-scheme").map(String::as_str),
        Some("tandem_hmac_sha256_v1")
    );
}

#[tokio::test]
#[serial_test::serial]
#[serial_test::serial(incident_monitor_http)]
async fn incident_monitor_webhook_destination_publishes_signed_payload_and_skips_duplicate() {
    let (endpoint, requests, server) = spawn_fake_incident_monitor_webhook_server(vec![202], 0).await;
    let state = test_state().await;
    configure_webhook_incident_monitor_destination(
        &state,
        endpoint,
        json!({
            "allow_private_networks": true,
            "allow_insecure_http": true,
            "max_attempts": 2
        }),
    )
    .await;

    let app = app_router(state.clone());
    let draft_id =
        create_ready_linear_incident_monitor_draft(app.clone(), "fingerprint-webhook-signed").await;

    let (publish_status, publish_payload) =
        publish_incident_monitor_webhook_draft(app.clone(), &draft_id).await;
    assert_eq!(publish_status, StatusCode::OK, "{publish_payload:?}");
    assert_eq!(
        publish_payload.get("action").and_then(Value::as_str),
        Some("post_webhook")
    );
    assert_eq!(
        publish_payload
            .get("post")
            .and_then(|row| row.get("destination_kind"))
            .and_then(Value::as_str),
        Some("webhook")
    );
    assert_eq!(
        publish_payload
            .get("post")
            .and_then(|row| row.get("receipt"))
            .and_then(|row| row.get("provider"))
            .and_then(Value::as_str),
        Some("webhook")
    );
    assert_eq!(
        publish_payload
            .get("post")
            .and_then(|row| row.get("receipt"))
            .and_then(|row| row.get("status_code"))
            .and_then(Value::as_u64),
        Some(202)
    );
    assert_eq!(
        publish_payload
            .get("external_action")
            .and_then(|row| row.get("capability_id"))
            .and_then(Value::as_str),
        Some("webhook.post")
    );

    let request_snapshot = requests.read().await.clone();
    assert_eq!(request_snapshot.len(), 1);
    let request = &request_snapshot[0];
    assert_tandem_webhook_signature(&request.headers, &request.body);
    assert_eq!(
        request.headers.get("x-tandem-event").map(String::as_str),
        Some("incident_monitor.incident")
    );
    let body_text = String::from_utf8(request.body.clone()).expect("webhook body utf8");
    assert!(!body_text.contains("test-webhook-signing-secret"));
    assert!(!body_text.contains("TANDEM_TEST_INCIDENT_MONITOR_WEBHOOK_SECRET"));
    let body_json: Value = serde_json::from_str(&body_text).expect("webhook json");
    assert_eq!(
        body_json
            .get("destination")
            .and_then(|row| row.get("destination_id"))
            .and_then(Value::as_str),
        Some("webhook-primary")
    );
    assert_eq!(
        body_json
            .get("draft")
            .and_then(|row| row.get("fingerprint"))
            .and_then(Value::as_str),
        Some("fingerprint-webhook-signed")
    );
    assert_eq!(
        body_json
            .get("issue_draft")
            .and_then(|row| row.get("suggested_title"))
            .and_then(Value::as_str),
        Some("Build failure in CI")
    );

    let first_post_id = publish_payload
        .get("post")
        .and_then(|row| row.get("post_id"))
        .and_then(Value::as_str)
        .expect("post id")
        .to_string();
    let (second_status, second_payload) =
        publish_incident_monitor_webhook_draft(app.clone(), &draft_id).await;
    assert_eq!(second_status, StatusCode::OK, "{second_payload:?}");
    assert_eq!(
        second_payload.get("action").and_then(Value::as_str),
        Some("skip_duplicate")
    );
    assert_eq!(
        second_payload
            .get("post")
            .and_then(|row| row.get("post_id"))
            .and_then(Value::as_str),
        Some(first_post_id.as_str())
    );
    assert_eq!(requests.read().await.len(), 1);

    server.abort();
}

#[tokio::test]
#[serial_test::serial]
#[serial_test::serial(incident_monitor_http)]
async fn incident_monitor_webhook_destination_blocks_private_url_by_default() {
    let (endpoint, requests, server) = spawn_fake_incident_monitor_webhook_server(vec![202], 0).await;
    let state = test_state().await;
    configure_webhook_incident_monitor_destination(
        &state,
        endpoint,
        json!({
            "allow_insecure_http": true,
            "max_attempts": 1
        }),
    )
    .await;

    let app = app_router(state.clone());
    let draft_id =
        create_ready_linear_incident_monitor_draft(app.clone(), "fingerprint-webhook-private").await;

    let (publish_status, publish_payload) =
        publish_incident_monitor_webhook_draft(app.clone(), &draft_id).await;
    assert_eq!(publish_status, StatusCode::BAD_REQUEST);
    assert!(
        publish_payload
            .get("detail")
            .and_then(Value::as_str)
            .is_some_and(|detail| detail.contains("localhost/private network")
                || detail.contains("private or internal address")),
        "private URL should be blocked: {publish_payload:?}"
    );
    assert_eq!(requests.read().await.len(), 0);
    // TAN-545: a not-ready destination (here a private/localhost webhook) is
    // blocked at the fail-closed readiness gate before any delivery attempt, so
    // no receipt is recorded — matching the adapters' pre-execution guards.
    assert!(state.list_incident_monitor_posts(10).await.is_empty());

    server.abort();
}

#[tokio::test]
#[serial_test::serial]
#[serial_test::serial(incident_monitor_http)]
async fn incident_monitor_webhook_destination_blocks_ipv4_mapped_private_ipv6_url() {
    let (endpoint, requests, server) = spawn_fake_incident_monitor_webhook_server(vec![202], 0).await;
    let port = reqwest::Url::parse(&endpoint)
        .expect("parse fake webhook endpoint")
        .port()
        .expect("fake webhook port");
    let mapped_endpoint = format!("http://[::ffff:127.0.0.1]:{port}/incident");
    let state = test_state().await;
    configure_webhook_incident_monitor_destination(
        &state,
        mapped_endpoint,
        json!({
            "allow_insecure_http": true,
            "max_attempts": 1
        }),
    )
    .await;

    let app = app_router(state.clone());
    let draft_id = create_ready_linear_incident_monitor_draft(
        app.clone(),
        "fingerprint-webhook-ipv4-mapped-private",
    )
    .await;

    let (publish_status, publish_payload) =
        publish_incident_monitor_webhook_draft(app.clone(), &draft_id).await;
    assert_eq!(publish_status, StatusCode::BAD_REQUEST);
    assert!(
        publish_payload
            .get("detail")
            .and_then(Value::as_str)
            .is_some_and(|detail| detail.contains("private or internal address")),
        "IPv4-mapped private IPv6 URL should be blocked: {publish_payload:?}"
    );
    assert_eq!(requests.read().await.len(), 0);

    server.abort();
}

#[tokio::test]
#[serial_test::serial]
#[serial_test::serial(incident_monitor_http)]
async fn incident_monitor_webhook_destination_retries_retryable_failure() {
    let (endpoint, requests, server) =
        spawn_fake_incident_monitor_webhook_server(vec![500, 202], 0).await;
    let state = test_state().await;
    configure_webhook_incident_monitor_destination(
        &state,
        endpoint,
        json!({
            "allow_private_networks": true,
            "allow_insecure_http": true,
            "max_attempts": 2
        }),
    )
    .await;

    let app = app_router(state.clone());
    let draft_id =
        create_ready_linear_incident_monitor_draft(app.clone(), "fingerprint-webhook-retry").await;

    let (publish_status, publish_payload) =
        publish_incident_monitor_webhook_draft(app.clone(), &draft_id).await;
    assert_eq!(publish_status, StatusCode::OK, "{publish_payload:?}");
    assert_eq!(requests.read().await.len(), 2);
    assert_eq!(
        publish_payload
            .get("post")
            .and_then(|row| row.get("receipt"))
            .and_then(|row| row.get("attempt_count"))
            .and_then(Value::as_u64),
        Some(2)
    );
    assert_eq!(
        publish_payload
            .get("post")
            .and_then(|row| row.get("receipt"))
            .and_then(|row| row.get("status_code"))
            .and_then(Value::as_u64),
        Some(202)
    );

    server.abort();
}

#[tokio::test]
#[serial_test::serial]
#[serial_test::serial(incident_monitor_http)]
async fn incident_monitor_webhook_destination_records_non_retryable_failure_receipt() {
    let (endpoint, requests, server) = spawn_fake_incident_monitor_webhook_server(vec![400], 0).await;
    let state = test_state().await;
    configure_webhook_incident_monitor_destination(
        &state,
        endpoint,
        json!({
            "allow_private_networks": true,
            "allow_insecure_http": true,
            "max_attempts": 3
        }),
    )
    .await;

    let app = app_router(state.clone());
    let draft_id =
        create_ready_linear_incident_monitor_draft(app.clone(), "fingerprint-webhook-400").await;

    let (publish_status, publish_payload) =
        publish_incident_monitor_webhook_draft(app.clone(), &draft_id).await;
    assert_eq!(publish_status, StatusCode::BAD_REQUEST);
    assert_eq!(requests.read().await.len(), 1);
    let posts = state.list_incident_monitor_posts(10).await;
    assert_eq!(posts.len(), 1);
    assert_eq!(posts[0].status, "failed");
    assert_eq!(
        posts[0]
            .receipt
            .as_ref()
            .and_then(|row| row.get("status_code"))
            .and_then(Value::as_u64),
        Some(400)
    );
    assert_eq!(
        posts[0]
            .receipt
            .as_ref()
            .and_then(|row| row.get("attempt_count"))
            .and_then(Value::as_u64),
        Some(1)
    );
    assert!(
        publish_payload
            .get("detail")
            .and_then(Value::as_str)
            .is_some_and(|detail| detail.contains("HTTP status 400")),
        "publish should expose non-retryable status: {publish_payload:?}"
    );

    server.abort();
}

#[tokio::test]
#[serial_test::serial]
#[serial_test::serial(incident_monitor_http)]
async fn incident_monitor_webhook_destination_records_timeout_failure_receipt() {
    let (endpoint, requests, server) = spawn_fake_incident_monitor_webhook_server(vec![202], 400).await;
    let state = test_state().await;
    configure_webhook_incident_monitor_destination(
        &state,
        endpoint,
        json!({
            "allow_private_networks": true,
            "allow_insecure_http": true,
            "max_attempts": 1,
            "timeout_ms": 250
        }),
    )
    .await;

    let app = app_router(state.clone());
    let draft_id =
        create_ready_linear_incident_monitor_draft(app.clone(), "fingerprint-webhook-timeout").await;

    let (publish_status, publish_payload) =
        publish_incident_monitor_webhook_draft(app.clone(), &draft_id).await;
    assert_eq!(publish_status, StatusCode::BAD_REQUEST);
    assert_eq!(requests.read().await.len(), 1);
    let posts = state.list_incident_monitor_posts(10).await;
    assert_eq!(posts.len(), 1);
    assert_eq!(posts[0].status, "failed");
    assert_eq!(
        posts[0]
            .receipt
            .as_ref()
            .and_then(|row| row.get("attempt_count"))
            .and_then(Value::as_u64),
        Some(1)
    );
    assert!(
        publish_payload
            .get("detail")
            .and_then(Value::as_str)
            .is_some_and(|detail| detail.contains("timed out")),
        "publish should expose timeout: {publish_payload:?}"
    );

    server.abort();
}

#[tokio::test]
#[serial_test::serial(incident_monitor_http)]
async fn incident_monitor_failure_receipt_attributes_actual_destination() {
    // TAN-552: a failed publish routed to a non-GitHub destination must be
    // recorded against that destination, not always attributed to legacy GitHub
    // (which would also risk suppressing a later real GitHub create).
    let state = test_state().await;
    state
        .put_incident_monitor_config(crate::IncidentMonitorConfig {
            enabled: true,
            repo: Some("acme/platform".to_string()),
            workspace_root: Some("/tmp/acme".to_string()),
            destinations: vec![crate::IncidentMonitorDestinationConfig {
                destination_id: "webhook-primary".to_string(),
                name: "Primary webhook".to_string(),
                kind: crate::IncidentMonitorDestinationKind::Webhook,
                webhook_url: Some("https://example.com/incident-hook".to_string()),
                ..Default::default()
            }],
            default_destination_ids: vec!["webhook-primary".to_string()],
            ..Default::default()
        })
        .await
        .expect("config");

    let draft = state
        .submit_incident_monitor_draft(crate::IncidentMonitorSubmission {
            source: Some("manual".to_string()),
            title: Some("Webhook destination failure".to_string()),
            detail: Some("delivery failed".to_string()),
            risk_level: Some("medium".to_string()),
            confidence: Some("medium".to_string()),
            ..Default::default()
        })
        .await
        .expect("draft");

    let post = crate::incident_monitor::router::record_publish_failure(
        &state,
        &draft,
        None,
        "auto_post",
        None,
        "boom",
    )
    .await
    .expect("record failure");

    assert_eq!(post.destination_id.as_deref(), Some("webhook-primary"));
    assert_eq!(
        post.destination_kind,
        Some(crate::IncidentMonitorDestinationKind::Webhook)
    );
    assert!(
        post.receipt
            .as_ref()
            .and_then(|receipt| receipt.get("provider"))
            .and_then(Value::as_str)
            == Some("webhook"),
        "receipt should record the webhook provider: {:?}",
        post.receipt
    );
}