tandem-server 0.6.9

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
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
//! Tenant-scoped runtime APIs for long-running goals (TAN-695): lifecycle,
//! graph/lineage/event read models, governed handoff emission and decisions,
//! external-condition wait inspection/resolution, and an SSE change stream.
//!
//! Every event emitted over SSE comes from the durable stateful store in
//! rowid order — the rowid doubles as the SSE event id, so a reconnect with
//! `Last-Event-ID` (or `?cursor=`) resumes with no missing and no duplicate
//! events, regardless of how the in-memory event bus behaved in between.

use super::*;

use tandem_automation::{
    AutomationV2RunRecord, LongRunningGoal, OrchestrationArtifactRef, OrchestrationNodeKind,
    OrchestrationSpec,
};
use tandem_types::{PrincipalKind, PrincipalRef, RequestPrincipal, VerifiedTenantContext};

use crate::stateful_runtime::{
    list_stateful_waits, GoalPauseOutcome, GoalResumeOutcome, GovernedTransitionRequest,
    GovernedTransitionResult, OrchestrationStateStore, OrchestrationTransitionAuthority,
    StartGoalOutcome, StatefulRuntimeStoragePaths, StatefulWaitQuery, WorkflowCompletionResult,
};

const DEFAULT_GOAL_LIST_LIMIT: usize = 100;
const MAX_GOAL_LIST_LIMIT: usize = 500;
const DEFAULT_GOAL_EVENT_LIMIT: usize = 250;
const MAX_GOAL_EVENT_LIMIT: usize = 1_000;
/// SSE replay page size; the stream keeps paging until it drains.
const SSE_REPLAY_PAGE: usize = 500;

/// File-backed artifact admission: the content path must resolve to a real
/// file inside the workspace root — symlink escapes included, because the
/// check runs on the canonicalized path — and the file must match its
/// declared digest. Runs at every emit surface (HTTP and MCP) before a
/// transition is attempted.
pub(super) fn enforce_artifact_content_policy(
    workspace_root: &str,
    artifact: &OrchestrationArtifactRef,
) -> anyhow::Result<()> {
    use anyhow::Context as _;
    let Some(content_path) = artifact.content_path.as_deref() else {
        return Ok(());
    };
    let root = std::path::Path::new(workspace_root)
        .canonicalize()
        .context("workspace root is unavailable for artifact validation")?;
    let resolved = root.join(content_path).canonicalize().with_context(|| {
        format!("handoff artifact content `{content_path}` does not resolve to a workspace file")
    })?;
    if !resolved.starts_with(&root) {
        anyhow::bail!("handoff artifact content `{content_path}` escapes the workspace root");
    }
    if !resolved.is_file() {
        anyhow::bail!("handoff artifact content `{content_path}` is not a regular file");
    }
    if let Some(digest) = artifact.content_digest.as_deref() {
        let expected = digest.strip_prefix("sha256:").unwrap_or(digest);
        let actual = sha256_file_hex(&resolved)?;
        if !expected.eq_ignore_ascii_case(&actual) {
            anyhow::bail!(
                "handoff artifact content digest mismatch for `{content_path}`: declared {expected}, found {actual}"
            );
        }
    }
    Ok(())
}

fn sha256_file_hex(path: &std::path::Path) -> anyhow::Result<String> {
    use sha2::{Digest, Sha256};
    use std::io::Read as _;
    let mut file = std::fs::File::open(path)?;
    let mut hasher = Sha256::new();
    let mut buffer = vec![0u8; 64 * 1024];
    loop {
        let read = file.read(&mut buffer)?;
        if read == 0 {
            break;
        }
        hasher.update(&buffer[..read]);
    }
    Ok(format!("{:x}", hasher.finalize()))
}

fn artifact_policy_response(error: &anyhow::Error) -> Response {
    (
        StatusCode::UNPROCESSABLE_ENTITY,
        Json(json!({
            "error": "artifact_policy_violation",
            "detail": error.to_string(),
        })),
    )
        .into_response()
}

pub(super) fn goal_store(state: &AppState) -> Result<OrchestrationStateStore, Response> {
    OrchestrationStateStore::from_automation_runs_path(&state.automation_v2_runs_path).map_err(
        |error| {
            tracing::error!(?error, "failed to open orchestration store");
            (
                StatusCode::INTERNAL_SERVER_ERROR,
                Json(json!({"error": "orchestration_store_unavailable"})),
            )
                .into_response()
        },
    )
}

pub(super) fn goal_error_response(error: &anyhow::Error) -> Response {
    let message = error.to_string();
    let (status, code) = if message.contains("not found") {
        (StatusCode::NOT_FOUND, "goal_not_found")
    } else if message.contains("tenant scope") {
        // Fail closed: cross-tenant reads are indistinguishable from absence.
        (StatusCode::NOT_FOUND, "goal_not_found")
    } else if message.contains("terminal") {
        (StatusCode::CONFLICT, "goal_terminal")
    } else if message.contains("idempotency key is already bound")
        || message.contains("raced")
        || message.contains("being claimed")
        || message.contains("no longer eligible")
    {
        (StatusCode::CONFLICT, "goal_state_conflict")
    } else if message.contains("not authorized") {
        (StatusCode::FORBIDDEN, "goal_forbidden")
    } else {
        (StatusCode::BAD_REQUEST, "invalid_goal_request")
    };
    (status, Json(json!({"error": code, "detail": message}))).into_response()
}

pub(super) fn request_actor(principal: &RequestPrincipal) -> PrincipalRef {
    PrincipalRef::new(
        PrincipalKind::HumanUser,
        principal.actor_id.as_deref().unwrap_or("anonymous"),
    )
}

/// The actor a mutation is attributed to: the verified assertion's human
/// actor when present, else the request principal. Audit records must name
/// this identity, not the raw transport principal.
pub(super) fn effective_actor(
    principal: &RequestPrincipal,
    verified: Option<&VerifiedTenantContext>,
) -> PrincipalRef {
    verified
        .map(|context| context.human_actor.actor_id.trim())
        .filter(|actor| !actor.is_empty())
        .map(|actor| PrincipalRef::new(PrincipalKind::HumanUser, actor))
        .unwrap_or_else(|| request_actor(principal))
}

/// Run-as/authority-chain context recorded into audit events so receipts can
/// distinguish the effective actor from the delegating principal.
pub(super) fn run_as_context(verified: Option<&VerifiedTenantContext>) -> Value {
    verified
        .map(|context| {
            json!({
                "actor_id": context.human_actor.actor_id,
                "issuer": context.issuer,
                "assertion_id": context.assertion_id,
                "roles": context.roles,
                "org_units": context.org_units,
                "initiated_by": context.authority_chain.initiated_by,
                "executed_as": context.authority_chain.executed_as,
            })
        })
        .unwrap_or(Value::Null)
}

pub(super) fn publish_goal_audit_receipt(
    state: &AppState,
    tenant: &TenantContext,
    event_type: &str,
    goal_id: &str,
    actor: &PrincipalRef,
    verified: Option<&VerifiedTenantContext>,
    mut details: serde_json::Map<String, Value>,
) {
    details.insert("goalID".to_string(), json!(goal_id));
    details.insert("effective_actor".to_string(), json!(actor));
    details.insert("run_as".to_string(), run_as_context(verified));
    state
        .event_bus
        .publish(crate::routines::types::tenant_scoped_engine_event(
            event_type,
            tenant,
            Value::Object(details),
        ));
}

pub(super) fn verified_has_admin_authority(verified: Option<&VerifiedTenantContext>) -> bool {
    verified.is_some_and(|context| {
        context.roles.iter().any(|role| {
            matches!(
                role.as_str(),
                "owner" | "admin" | "hosted:owner" | "hosted:admin" | "enterprise:admin"
            )
        }) || context.capabilities.iter().any(|capability| {
            matches!(
                capability.as_str(),
                "hosted.admin" | "orchestration.control" | "orchestration.write"
            )
        })
    })
}

/// Enterprise authority gate for goal mutations. In hosted and enterprise
/// auth modes the ingress middleware already rejects explicit tenants without
/// a verified assertion, so a present `VerifiedTenantContext` is the signal
/// that enterprise enforcement applies: capability-gated surfaces (approval,
/// wait resolution) then require the named capability or an administrative
/// role. Local single-tenant mode (no assertion) keeps its operator UX. Deny
/// wins: nothing in the request body or tenant headers can satisfy this
/// check.
pub(super) fn require_goal_authority(
    _tenant: &TenantContext,
    verified: Option<&VerifiedTenantContext>,
    required_capability: Option<&str>,
) -> Result<(), Response> {
    let Some(verified) = verified else {
        // Local single-tenant mode: hosted ingress never reaches here
        // unverified because unsigned tenant headers are denied upstream.
        return Ok(());
    };
    if let Some(capability) = required_capability {
        let authorized = verified
            .capabilities
            .iter()
            .any(|value| value == capability)
            || verified.roles.iter().any(|role| {
                matches!(
                    role.as_str(),
                    "owner" | "admin" | "hosted:owner" | "hosted:admin" | "enterprise:admin"
                )
            });
        if !authorized {
            return Err((
                StatusCode::FORBIDDEN,
                Json(json!({
                    "error": "goal_forbidden",
                    "detail": format!("authenticated principal lacks {capability} authority"),
                })),
            )
                .into_response());
        }
    }
    Ok(())
}

/// Goal mutations belong to the goal's initiating actor unless the caller
/// carries administrative authority. Enforced for verified (hosted) callers;
/// the local single-operator is always the owner.
pub(super) fn require_goal_owner(
    _tenant: &TenantContext,
    verified: Option<&VerifiedTenantContext>,
    goal: &LongRunningGoal,
    actor: &PrincipalRef,
) -> Result<(), Response> {
    if verified.is_none() || verified_has_admin_authority(verified) {
        return Ok(());
    }
    let started_by = goal
        .metadata
        .as_ref()
        .and_then(|metadata| metadata.get("started_by"))
        .and_then(|value| {
            value
                .get("id")
                .and_then(Value::as_str)
                .or_else(|| value.as_str())
        });
    if started_by != Some(actor.id.as_str()) {
        return Err((
            StatusCode::FORBIDDEN,
            Json(json!({
                "error": "goal_forbidden",
                "detail": "goal mutation requires its initiating actor or an authorized administrator",
            })),
        )
            .into_response());
    }
    Ok(())
}

/// Authority for emit/approve surfaces, derived from the verified principal.
fn transition_authority(
    principal: &RequestPrincipal,
    verified: Option<&VerifiedTenantContext>,
    can_approve: bool,
) -> OrchestrationTransitionAuthority {
    OrchestrationTransitionAuthority {
        actor: effective_actor(principal, verified),
        can_emit: true,
        can_approve,
    }
}

pub(super) fn load_tenant_goal(
    store: &OrchestrationStateStore,
    tenant: &TenantContext,
    goal_id: &str,
) -> Result<LongRunningGoal, Response> {
    // Scope is enforced in the store's SQL (org/workspace/deployment); the
    // request tenant's actor_id never affects resource visibility.
    match store.get_goal_for_tenant(tenant, goal_id) {
        Ok(Some(goal)) => Ok(goal),
        Ok(None) => Err((
            StatusCode::NOT_FOUND,
            Json(json!({"error": "goal_not_found"})),
        )
            .into_response()),
        Err(error) => Err(goal_error_response(&error)),
    }
}

fn goal_response(goal: &LongRunningGoal) -> Value {
    json!({
        "goal": goal,
        "goal_id": goal.goal_id,
        "status": goal.status,
        "budgets": goal_budgets(goal),
    })
}

pub(super) fn goal_budgets(goal: &LongRunningGoal) -> Value {
    json!({
        "policy": goal.policy,
        "consumed": {
            "hops": goal.hop_count,
            "total_tokens": goal.total_tokens,
            "total_cost_usd": goal.total_cost_usd,
        },
        "remaining": {
            "hops": goal.policy.max_hops.saturating_sub(goal.hop_count),
            "tokens": goal
                .policy
                .max_total_tokens
                .map(|maximum| maximum.saturating_sub(goal.total_tokens)),
            "cost_usd": goal
                .policy
                .max_total_cost_usd
                .map(|maximum| (maximum - goal.total_cost_usd).max(0.0)),
            "deadline_ms": goal
                .policy
                .deadline_at_ms
                .map(|deadline| deadline.saturating_sub(crate::now_ms())),
        },
    })
}

// ---------------------------------------------------------------------------
// Lifecycle
// ---------------------------------------------------------------------------

#[derive(Debug, Deserialize)]
pub(super) struct StartGoalPayload {
    pub orchestration_id: String,
    #[serde(default)]
    pub orchestration_version: Option<u64>,
    pub objective: String,
    pub idempotency_key: String,
    #[serde(default)]
    pub metadata: Option<Value>,
}

pub(super) async fn start_goal(
    State(state): State<AppState>,
    Extension(tenant): Extension<TenantContext>,
    Extension(principal): Extension<RequestPrincipal>,
    verified: Option<Extension<VerifiedTenantContext>>,
    Json(payload): Json<StartGoalPayload>,
) -> Response {
    let verified = verified.as_deref();
    if let Err(response) = require_goal_authority(&tenant, verified, None) {
        return response;
    }
    let actor = effective_actor(&principal, verified);
    let mut metadata = match payload.metadata {
        Some(Value::Object(object)) => object,
        Some(other) => {
            let mut object = serde_json::Map::new();
            object.insert("value".to_string(), other);
            object
        }
        None => serde_json::Map::new(),
    };
    // Goal ownership is transport authority, never caller-controlled data.
    metadata.insert("started_by".to_string(), json!(actor));
    let request = crate::app::state::StartGoalRequest {
        orchestration_id: payload.orchestration_id,
        orchestration_version: payload.orchestration_version,
        objective: payload.objective,
        idempotency_key: payload.idempotency_key,
        metadata: Some(Value::Object(metadata)),
        now_ms: crate::now_ms(),
    };
    match state
        .start_long_running_goal(&tenant, &request, &actor)
        .await
    {
        Ok(StartGoalOutcome::Created { goal, root_run }) => (
            StatusCode::CREATED,
            Json(json!({
                "goal": goal,
                "root_run_id": root_run.run_id,
                "replayed": false,
            })),
        )
            .into_response(),
        Ok(StartGoalOutcome::AlreadyStarted { goal, root_run }) => Json(json!({
            "goal": goal,
            "root_run_id": root_run.run_id,
            "replayed": true,
        }))
        .into_response(),
        Err(error) => goal_error_response(&error),
    }
}

#[derive(Debug, Deserialize, Default)]
pub(super) struct GoalListQuery {
    pub status: Option<String>,
    pub orchestration_id: Option<String>,
    pub limit: Option<usize>,
}

pub(super) async fn list_goals(
    State(state): State<AppState>,
    Extension(tenant): Extension<TenantContext>,
    Query(query): Query<GoalListQuery>,
) -> Response {
    let store = match goal_store(&state) {
        Ok(store) => store,
        Err(response) => return response,
    };
    let limit = query
        .limit
        .unwrap_or(DEFAULT_GOAL_LIST_LIMIT)
        .clamp(1, MAX_GOAL_LIST_LIMIT);
    match store.list_goals(
        &tenant,
        query.status.as_deref(),
        query.orchestration_id.as_deref(),
        limit,
    ) {
        Ok(goals) => Json(json!({"goals": goals, "count": goals.len()})).into_response(),
        Err(error) => goal_error_response(&error),
    }
}

pub(super) async fn get_goal(
    State(state): State<AppState>,
    Extension(tenant): Extension<TenantContext>,
    Path(goal_id): Path<String>,
) -> Response {
    let store = match goal_store(&state) {
        Ok(store) => store,
        Err(response) => return response,
    };
    match load_tenant_goal(&store, &tenant, &goal_id) {
        Ok(goal) => Json(goal_response(&goal)).into_response(),
        Err(response) => response,
    }
}

#[derive(Debug, Deserialize, Default)]
pub(super) struct GoalControlPayload {
    #[serde(default)]
    pub reason: Option<String>,
}

pub(super) async fn pause_goal(
    State(state): State<AppState>,
    Extension(tenant): Extension<TenantContext>,
    Extension(principal): Extension<RequestPrincipal>,
    verified: Option<Extension<VerifiedTenantContext>>,
    Path(goal_id): Path<String>,
    Json(payload): Json<GoalControlPayload>,
) -> Response {
    let verified = verified.as_deref();
    let reason = payload.reason.as_deref().unwrap_or("operator pause");
    let store = match goal_store(&state) {
        Ok(store) => store,
        Err(response) => return response,
    };
    let stored = match load_tenant_goal(&store, &tenant, &goal_id) {
        Ok(goal) => goal,
        Err(response) => return response,
    };
    let actor = effective_actor(&principal, verified);
    if let Err(response) = require_goal_authority(&tenant, verified, None) {
        return response;
    }
    if let Err(response) = require_goal_owner(&tenant, verified, &stored, &actor) {
        return response;
    }
    match state
        .pause_long_running_goal(&goal_id, &stored.tenant_context, reason, &actor)
        .await
    {
        Ok((outcome, goal)) => {
            let outcome_name = match outcome {
                GoalPauseOutcome::Applied => "paused",
                GoalPauseOutcome::AlreadyPaused => "already_paused",
            };
            publish_goal_audit_receipt(
                &state,
                &tenant,
                "orchestration.goal.action_receipt",
                &goal_id,
                &actor,
                verified,
                serde_json::Map::from_iter([
                    ("action".to_string(), json!("pause")),
                    ("outcome".to_string(), json!(outcome_name)),
                ]),
            );
            Json(json!({"goal": goal, "outcome": outcome_name})).into_response()
        }
        Err(error) => goal_error_response(&error),
    }
}

pub(super) async fn resume_goal(
    State(state): State<AppState>,
    Extension(tenant): Extension<TenantContext>,
    Extension(principal): Extension<RequestPrincipal>,
    verified: Option<Extension<VerifiedTenantContext>>,
    Path(goal_id): Path<String>,
    Json(payload): Json<GoalControlPayload>,
) -> Response {
    let verified = verified.as_deref();
    let reason = payload.reason.as_deref().unwrap_or("operator resume");
    let store = match goal_store(&state) {
        Ok(store) => store,
        Err(response) => return response,
    };
    let stored = match load_tenant_goal(&store, &tenant, &goal_id) {
        Ok(goal) => goal,
        Err(response) => return response,
    };
    let actor = effective_actor(&principal, verified);
    if let Err(response) = require_goal_authority(&tenant, verified, None) {
        return response;
    }
    if let Err(response) = require_goal_owner(&tenant, verified, &stored, &actor) {
        return response;
    }
    match state
        .resume_long_running_goal(&goal_id, &stored.tenant_context, reason, &actor)
        .await
    {
        Ok((outcome, goal)) => {
            let outcome_name = match outcome {
                GoalResumeOutcome::Applied => "resumed",
                GoalResumeOutcome::NotPaused => "not_paused",
            };
            publish_goal_audit_receipt(
                &state,
                &tenant,
                "orchestration.goal.action_receipt",
                &goal_id,
                &actor,
                verified,
                serde_json::Map::from_iter([
                    ("action".to_string(), json!("resume")),
                    ("outcome".to_string(), json!(outcome_name)),
                ]),
            );
            Json(json!({"goal": goal, "outcome": outcome_name})).into_response()
        }
        Err(error) => goal_error_response(&error),
    }
}

pub(super) async fn cancel_goal(
    State(state): State<AppState>,
    Extension(tenant): Extension<TenantContext>,
    Extension(principal): Extension<RequestPrincipal>,
    verified: Option<Extension<VerifiedTenantContext>>,
    Path(goal_id): Path<String>,
    Json(payload): Json<GoalControlPayload>,
) -> Response {
    let verified = verified.as_deref();
    let reason = payload.reason.as_deref().unwrap_or("operator cancel");
    let store = match goal_store(&state) {
        Ok(store) => store,
        Err(response) => return response,
    };
    let stored = match load_tenant_goal(&store, &tenant, &goal_id) {
        Ok(goal) => goal,
        Err(response) => return response,
    };
    let actor = effective_actor(&principal, verified);
    if let Err(response) = require_goal_authority(&tenant, verified, None) {
        return response;
    }
    if let Err(response) = require_goal_owner(&tenant, verified, &stored, &actor) {
        return response;
    }
    match state
        .cancel_long_running_goal(&goal_id, &stored.tenant_context, reason, &actor)
        .await
    {
        Ok(result) => {
            let outcome = format!("{:?}", result.outcome);
            publish_goal_audit_receipt(
                &state,
                &tenant,
                "orchestration.goal.action_receipt",
                &goal_id,
                &actor,
                verified,
                serde_json::Map::from_iter([
                    ("action".to_string(), json!("cancel")),
                    ("outcome".to_string(), json!(outcome)),
                ]),
            );
            Json(json!({
                "goal": result.goal,
                "outcome": outcome,
                "cancelled_run_id": result.cancelled_run.as_ref().map(|run| &run.run_id),
                "cancelled_wait_ids": result.cancelled_wait_ids,
                "dead_lettered_handoff_ids": result.dead_lettered_handoff_ids,
            }))
            .into_response()
        }
        Err(error) => goal_error_response(&error),
    }
}

// ---------------------------------------------------------------------------
// Read models: graph, runs, events, artifacts, budgets
// ---------------------------------------------------------------------------

pub(super) async fn get_goal_graph(
    State(state): State<AppState>,
    Extension(tenant): Extension<TenantContext>,
    Path(goal_id): Path<String>,
) -> Response {
    let store = match goal_store(&state) {
        Ok(store) => store,
        Err(response) => return response,
    };
    let goal = match load_tenant_goal(&store, &tenant, &goal_id) {
        Ok(goal) => goal,
        Err(response) => return response,
    };
    let orchestration = match store.get_orchestration_for_tenant(
        &tenant,
        &goal.orchestration_id,
        goal.orchestration_version,
    ) {
        Ok(Some(spec)) => spec,
        Ok(None) => {
            return (
                StatusCode::NOT_FOUND,
                Json(json!({"error": "orchestration_not_found"})),
            )
                .into_response()
        }
        Err(error) => return goal_error_response(&error),
    };
    let links = store
        .list_goal_run_links_for_tenant(&tenant, &goal_id)
        .unwrap_or_default();
    let mut runs_by_node = std::collections::HashMap::<String, Vec<Value>>::new();
    let mut active_run: Option<AutomationV2RunRecord> = None;
    for link in &links {
        let run = state.get_automation_v2_run(&link.run_id).await;
        if goal.active_run_id.as_deref() == Some(link.run_id.as_str()) {
            active_run = run.clone();
        }
        runs_by_node
            .entry(link.orchestration_node_id.clone())
            .or_default()
            .push(json!({
                "run_id": link.run_id,
                "hop_index": link.hop_index,
                "parent_run_id": link.parent_run_id,
                "triggering_handoff_id": link.triggering_handoff_id,
                "status": run.as_ref().map(|run| json!(run.status)).unwrap_or(Value::Null),
            }));
    }
    let nodes = orchestration
        .nodes
        .iter()
        .map(|node| {
            let node_runs = runs_by_node.remove(&node.node_id).unwrap_or_default();
            let node_state = if goal.current_node_id.as_deref() == Some(node.node_id.as_str()) {
                "current"
            } else if !node_runs.is_empty() {
                "visited"
            } else {
                "not_started"
            };
            json!({
                "node_id": node.node_id,
                "name": node.name,
                "kind": node.node,
                "state": node_state,
                "runs": node_runs,
            })
        })
        .collect::<Vec<_>>();
    // The internal Automation V2 stage of the active run: the first pending
    // node in its checkpoint (or the last completed one when it is settling).
    let current_stage = active_run.as_ref().map(|run| {
        json!({
            "run_id": run.run_id,
            "automation_id": run.automation_id,
            "status": run.status,
            "current_node_id": run
                .checkpoint
                .pending_nodes
                .first()
                .or(run.checkpoint.completed_nodes.last()),
            "completed_nodes": run.checkpoint.completed_nodes.len(),
            "pending_nodes": run.checkpoint.pending_nodes.len(),
        })
    });
    Json(json!({
        "goal_id": goal_id,
        "status": goal.status,
        "orchestration_id": goal.orchestration_id,
        "orchestration_version": goal.orchestration_version,
        "current_node_id": goal.current_node_id,
        "current_workflow": current_stage,
        "nodes": nodes,
        "edges": orchestration.edges,
        "budgets": goal_budgets(&goal),
    }))
    .into_response()
}

pub(super) async fn list_goal_runs(
    State(state): State<AppState>,
    Extension(tenant): Extension<TenantContext>,
    Path(goal_id): Path<String>,
) -> Response {
    let store = match goal_store(&state) {
        Ok(store) => store,
        Err(response) => return response,
    };
    let goal = match load_tenant_goal(&store, &tenant, &goal_id) {
        Ok(goal) => goal,
        Err(response) => return response,
    };
    let links = store
        .list_goal_run_links_for_tenant(&tenant, &goal_id)
        .unwrap_or_default();
    let mut rows = Vec::new();
    for link in &links {
        let run = state.get_automation_v2_run(&link.run_id).await;
        rows.push(json!({
            "link": link,
            "run": run,
        }));
    }
    Json(json!({
        "goal_id": goal_id,
        "active_run_id": goal.active_run_id,
        "runs": rows,
        "count": rows.len(),
    }))
    .into_response()
}

#[derive(Debug, Deserialize, Default)]
pub(super) struct GoalEventsQuery {
    /// Durable store cursor (SSE `Last-Event-ID` compatible).
    pub cursor: Option<i64>,
    pub limit: Option<usize>,
}

pub(super) async fn list_goal_events(
    State(state): State<AppState>,
    Extension(tenant): Extension<TenantContext>,
    Path(goal_id): Path<String>,
    Query(query): Query<GoalEventsQuery>,
) -> Response {
    let store = match goal_store(&state) {
        Ok(store) => store,
        Err(response) => return response,
    };
    if let Err(response) = load_tenant_goal(&store, &tenant, &goal_id) {
        return response;
    }
    let limit = query
        .limit
        .unwrap_or(DEFAULT_GOAL_EVENT_LIMIT)
        .clamp(1, MAX_GOAL_EVENT_LIMIT);
    match store.query_goal_events_for_tenant(&tenant, &goal_id, query.cursor, limit) {
        Ok(rows) => {
            let last_cursor = rows.last().map(|row| row.cursor);
            Json(json!({
                "goal_id": goal_id,
                "events": rows
                    .iter()
                    .map(|row| json!({"cursor": row.cursor, "event": goal_event_wire(row.event.clone())}))
                    .collect::<Vec<_>>(),
                "count": rows.len(),
                "last_cursor": last_cursor,
                "event_source": "stateful_runtime",
            }))
            .into_response()
        }
        Err(error) => goal_error_response(&error),
    }
}

#[derive(Debug, Deserialize, Default)]
pub(super) struct GoalStreamQuery {
    pub cursor: Option<i64>,
}

/// SSE stream of durable goal events. Replays everything after the cursor
/// (query param or `Last-Event-ID` header), then tails the store, waking on
/// engine-bus activity with a polling floor so no durable write is missed.
pub(super) async fn stream_goal_events(
    State(state): State<AppState>,
    Extension(tenant): Extension<TenantContext>,
    Path(goal_id): Path<String>,
    Query(query): Query<GoalStreamQuery>,
    headers: HeaderMap,
) -> Result<Sse<impl Stream<Item = Result<Event, std::convert::Infallible>>>, Response> {
    let store = goal_store(&state)?;
    load_tenant_goal(&store, &tenant, &goal_id)?;
    let last_event_id = headers
        .get("last-event-id")
        .and_then(|value| value.to_str().ok())
        .and_then(|value| value.trim().parse::<i64>().ok());
    let mut cursor = last_event_id.or(query.cursor).unwrap_or(0);

    let (tx, rx) = tokio::sync::mpsc::channel::<Event>(256);
    let mut bus = state.event_bus.subscribe();
    let scope_tenant = tenant.clone();
    tokio::spawn(async move {
        let ready = Event::default().event("ready").data(
            json!({
                "goal_id": goal_id,
                "cursor": cursor,
                "timestamp_ms": crate::now_ms(),
            })
            .to_string(),
        );
        if tx.send(ready).await.is_err() {
            return;
        }
        loop {
            // Drain everything the durable log has after the cursor. Rowid
            // order plus the id header gives exact-once reconnect semantics.
            loop {
                let rows = match store.query_goal_events_for_tenant(
                    &scope_tenant,
                    &goal_id,
                    Some(cursor),
                    SSE_REPLAY_PAGE,
                ) {
                    Ok(rows) => rows,
                    Err(error) => {
                        let _ = tx
                            .send(Event::default().event("error").data(
                                json!({"error": "goal_event_read_failed", "detail": error.to_string()})
                                    .to_string(),
                            ))
                            .await;
                        return;
                    }
                };
                let drained = rows.len() < SSE_REPLAY_PAGE;
                for row in rows {
                    cursor = row.cursor;
                    let event = Event::default()
                        .id(row.cursor.to_string())
                        .event(row.event.event_type.clone())
                        .data(
                            serde_json::to_string(&json!({
                                "cursor": row.cursor,
                                "event": goal_event_wire(row.event),
                            }))
                            .unwrap_or_default(),
                        );
                    if tx.send(event).await.is_err() {
                        return;
                    }
                }
                if drained {
                    break;
                }
            }
            // Wait for a wake signal: engine-bus activity for this goal, or
            // the polling floor so durable writes from other processes are
            // still picked up promptly.
            tokio::select! {
                received = bus.recv() => {
                    match received {
                        Ok(event) => {
                            let relevant = event.event_type.starts_with("orchestration.goal")
                                && event
                                    .properties
                                    .get("goalID")
                                    .and_then(Value::as_str)
                                    .is_none_or(|id| id == goal_id);
                            if !relevant {
                                continue;
                            }
                        }
                        Err(_) => {
                            // Lagged or closed: fall through to a store poll.
                        }
                    }
                }
                _ = tokio::time::sleep(Duration::from_millis(750)) => {}
            }
            if tx.is_closed() {
                return;
            }
        }
    });
    Ok(Sse::new(futures::StreamExt::map(
        tokio_stream::wrappers::ReceiverStream::new(rx),
        Ok,
    ))
    .keep_alive(KeepAlive::new().interval(Duration::from_secs(10))))
}

pub(super) fn goal_event_wire(
    mut event: crate::stateful_runtime::StatefulRunEventRecord,
) -> crate::stateful_runtime::StatefulRunEventRecord {
    if let Some(payload) = event.payload.as_object_mut() {
        payload.remove("projection_snapshot");
        payload.remove("projection_snapshot_ref");
    }
    event
}

pub(super) async fn list_goal_artifacts(
    State(state): State<AppState>,
    Extension(tenant): Extension<TenantContext>,
    Path(goal_id): Path<String>,
) -> Response {
    let store = match goal_store(&state) {
        Ok(store) => store,
        Err(response) => return response,
    };
    let goal = match load_tenant_goal(&store, &tenant, &goal_id) {
        Ok(goal) => goal,
        Err(response) => return response,
    };
    let handoffs = store
        .list_goal_handoffs_for_tenant(&tenant, &goal_id)
        .unwrap_or_default();
    let artifacts = handoffs
        .iter()
        .map(|handoff| {
            json!({
                "artifact": handoff.artifact,
                "handoff_id": handoff.handoff_id,
                "transition_key": handoff.transition_key,
                "source_run_id": handoff.source_run_id,
                "consumed_by_run_id": handoff.consumed_by_run_id,
                "created_at_ms": handoff.created_at_ms,
            })
        })
        .collect::<Vec<_>>();
    Json(json!({
        "goal_id": goal_id,
        "artifacts": artifacts,
        "final_artifact": goal.final_artifact,
        "count": artifacts.len(),
    }))
    .into_response()
}

pub(super) async fn get_goal_budgets(
    State(state): State<AppState>,
    Extension(tenant): Extension<TenantContext>,
    Path(goal_id): Path<String>,
) -> Response {
    let store = match goal_store(&state) {
        Ok(store) => store,
        Err(response) => return response,
    };
    match load_tenant_goal(&store, &tenant, &goal_id) {
        Ok(goal) => Json(json!({
            "goal_id": goal_id,
            "status": goal.status,
            "budgets": goal_budgets(&goal),
        }))
        .into_response(),
        Err(response) => response,
    }
}

// ---------------------------------------------------------------------------
// Handoffs: emit, list, decide; workflow completion settlement
// ---------------------------------------------------------------------------

pub(super) async fn list_goal_handoffs(
    State(state): State<AppState>,
    Extension(tenant): Extension<TenantContext>,
    Path(goal_id): Path<String>,
) -> Response {
    let store = match goal_store(&state) {
        Ok(store) => store,
        Err(response) => return response,
    };
    if let Err(response) = load_tenant_goal(&store, &tenant, &goal_id) {
        return response;
    }
    match store.list_goal_handoffs_for_tenant(&tenant, &goal_id) {
        Ok(handoffs) => {
            Json(json!({"goal_id": goal_id, "handoffs": handoffs, "count": handoffs.len()}))
                .into_response()
        }
        Err(error) => goal_error_response(&error),
    }
}

#[derive(Debug, Deserialize)]
pub(super) struct EmitTransitionPayload {
    pub transition_key: String,
    pub idempotency_key: String,
    pub artifact: OrchestrationArtifactRef,
}

pub(super) async fn emit_goal_transition(
    State(state): State<AppState>,
    Extension(tenant): Extension<TenantContext>,
    Extension(principal): Extension<RequestPrincipal>,
    verified: Option<Extension<VerifiedTenantContext>>,
    Path(goal_id): Path<String>,
    Json(payload): Json<EmitTransitionPayload>,
) -> Response {
    let verified = verified.as_deref();
    let store = match goal_store(&state) {
        Ok(store) => store,
        Err(response) => return response,
    };
    let stored = match load_tenant_goal(&store, &tenant, &goal_id) {
        // Terminal goals reject transition emissions with a stable contract
        // (only governed recovery operations may touch them).
        Ok(goal) if goal.status.is_terminal() => {
            return (
                StatusCode::CONFLICT,
                Json(json!({
                    "error": "goal_terminal",
                    "detail": "terminal goals cannot emit transitions",
                    "status": goal.status,
                })),
            )
                .into_response()
        }
        Ok(goal) => goal,
        Err(response) => return response,
    };
    let actor = effective_actor(&principal, verified);
    if let Err(response) = require_goal_authority(&tenant, verified, None) {
        return response;
    }
    if let Err(response) = require_goal_owner(&tenant, verified, &stored, &actor) {
        return response;
    }
    let workspace_root = state.workspace_index.snapshot().await.root;
    if let Err(error) = enforce_artifact_content_policy(&workspace_root, &payload.artifact) {
        return artifact_policy_response(&error);
    }
    let request = GovernedTransitionRequest {
        transition_key: payload.transition_key,
        idempotency_key: payload.idempotency_key,
        artifact: payload.artifact,
        now_ms: crate::now_ms(),
    };
    let authority = transition_authority(&principal, verified, false);
    match state
        .emit_orchestration_transition(&goal_id, &request, &authority)
        .await
    {
        Ok(GovernedTransitionResult::Committed {
            commit,
            handoff,
            downstream_run,
            link,
            goal,
        }) => Json(json!({
            "outcome": "committed",
            "commit": format!("{commit:?}"),
            "handoff": handoff,
            "downstream_run_id": downstream_run.run_id,
            "link": link,
            "goal": goal,
        }))
        .into_response(),
        Ok(GovernedTransitionResult::PendingApproval { handoff, goal }) => (
            StatusCode::ACCEPTED,
            Json(json!({
                "outcome": "pending_approval",
                "handoff": handoff,
                "goal": goal,
            })),
        )
            .into_response(),
        Err(error) => goal_error_response(&error),
    }
}

#[derive(Debug, Deserialize)]
pub(super) struct HandoffDecisionPayload {
    /// `approve` or `reject`.
    pub decision: String,
    #[serde(default)]
    pub reason: Option<String>,
}

pub(super) async fn decide_goal_handoff(
    State(state): State<AppState>,
    Extension(tenant): Extension<TenantContext>,
    Extension(principal): Extension<RequestPrincipal>,
    verified: Option<Extension<VerifiedTenantContext>>,
    Path((goal_id, handoff_id)): Path<(String, String)>,
    Json(payload): Json<HandoffDecisionPayload>,
) -> Response {
    let verified = verified.as_deref();
    // Approvals move authority between workflows: they require the approval
    // capability (or an administrative role) on explicit tenants.
    if let Err(response) = require_goal_authority(&tenant, verified, Some("orchestration.approve"))
    {
        return response;
    }
    let approve = match payload.decision.as_str() {
        "approve" => true,
        "reject" => false,
        _ => {
            return (
                StatusCode::BAD_REQUEST,
                Json(json!({
                    "error": "invalid_goal_request",
                    "detail": "decision must be approve or reject",
                })),
            )
                .into_response()
        }
    };
    let store = match goal_store(&state) {
        Ok(store) => store,
        Err(response) => return response,
    };
    let stored = match load_tenant_goal(&store, &tenant, &goal_id) {
        Ok(goal) => goal,
        Err(response) => return response,
    };
    match store.get_workflow_handoff_for_tenant(&tenant, &handoff_id) {
        Ok(Some(handoff)) if handoff.goal_id == goal_id => {}
        Ok(_) => {
            return (
                StatusCode::NOT_FOUND,
                Json(json!({"error": "handoff_not_found"})),
            )
                .into_response()
        }
        Err(error) => return goal_error_response(&error),
    }
    let authority = transition_authority(&principal, verified, true);
    match store.decide_pending_handoff(
        &handoff_id,
        &stored.tenant_context,
        approve,
        &authority,
        crate::now_ms(),
    ) {
        Ok(handoff) => {
            state
                .event_bus
                .publish(crate::routines::types::tenant_scoped_engine_event(
                    "orchestration.goal.handoff_decided",
                    &tenant,
                    json!({
                        "goalID": goal_id,
                        "handoffID": handoff_id,
                        "approved": approve,
                        "reason": payload.reason,
                        "effective_actor": authority.actor,
                        "run_as": run_as_context(verified),
                    }),
                ));
            Json(json!({"handoff": handoff})).into_response()
        }
        Err(error) => goal_error_response(&error),
    }
}

#[derive(Debug, Deserialize, Default)]
pub(super) struct CompletionPayload {
    #[serde(default)]
    pub transition_key: Option<String>,
    #[serde(default)]
    pub final_artifact: Option<OrchestrationArtifactRef>,
}

/// Settle the active workflow's completion: either into a terminal node via
/// `transition_key`, or into the awaiting-transition state when omitted.
pub(super) async fn settle_goal_completion(
    State(state): State<AppState>,
    Extension(tenant): Extension<TenantContext>,
    Extension(principal): Extension<RequestPrincipal>,
    verified: Option<Extension<VerifiedTenantContext>>,
    Path(goal_id): Path<String>,
    Json(payload): Json<CompletionPayload>,
) -> Response {
    let verified = verified.as_deref();
    if let Err(response) = require_goal_authority(&tenant, verified, None) {
        return response;
    }
    let store = match goal_store(&state) {
        Ok(store) => store,
        Err(response) => return response,
    };
    let goal = match load_tenant_goal(&store, &tenant, &goal_id) {
        Ok(goal) if goal.status.is_terminal() => {
            return (
                StatusCode::CONFLICT,
                Json(json!({
                    "error": "goal_terminal",
                    "detail": "terminal goals cannot settle workflow completion",
                    "status": goal.status,
                })),
            )
                .into_response()
        }
        Ok(goal) => goal,
        Err(response) => return response,
    };
    let actor = effective_actor(&principal, verified);
    if let Err(response) = require_goal_owner(&tenant, verified, &goal, &actor) {
        return response;
    }
    if let Some(final_artifact) = payload.final_artifact.as_ref() {
        let workspace_root = state.workspace_index.snapshot().await.root;
        if let Err(error) = enforce_artifact_content_policy(&workspace_root, final_artifact) {
            return artifact_policy_response(&error);
        }
    }
    let authority = transition_authority(&principal, verified, false);
    match state
        .settle_orchestration_workflow_completion(
            &goal_id,
            payload.transition_key.as_deref(),
            payload.final_artifact,
            &authority,
        )
        .await
    {
        Ok(WorkflowCompletionResult::Waiting { goal }) => {
            Json(json!({"outcome": "awaiting_transition", "goal": goal})).into_response()
        }
        Ok(WorkflowCompletionResult::Terminal { goal }) => {
            state
                .event_bus
                .publish(crate::routines::types::tenant_scoped_engine_event(
                    "orchestration.goal.terminal",
                    &tenant,
                    json!({"goalID": goal_id, "status": goal.status}),
                ));
            Json(json!({"outcome": "terminal", "goal": goal})).into_response()
        }
        Err(error) => goal_error_response(&error),
    }
}

// ---------------------------------------------------------------------------
// Waits: list, inspect, resolve
// ---------------------------------------------------------------------------

pub(super) fn goal_waits(
    state: &AppState,
    tenant: &TenantContext,
    store: &OrchestrationStateStore,
    goal_id: &str,
) -> Vec<crate::stateful_runtime::StatefulWaitRecord> {
    let run_ids = store
        .list_goal_run_links_for_tenant(tenant, goal_id)
        .unwrap_or_default()
        .into_iter()
        .map(|link| link.run_id)
        .collect::<std::collections::HashSet<_>>();
    let paths = StatefulRuntimeStoragePaths::from_runtime_events_path(&state.runtime_events_path);
    list_stateful_waits(
        &paths.waits_path,
        tenant,
        StatefulWaitQuery {
            run_id: None,
            wait_kind: None,
            status: None,
            limit: None,
        },
    )
    .into_iter()
    .filter(|wait| run_ids.contains(&wait.run_id))
    .collect()
}

pub(super) async fn list_goal_waits(
    State(state): State<AppState>,
    Extension(tenant): Extension<TenantContext>,
    Path(goal_id): Path<String>,
) -> Response {
    let store = match goal_store(&state) {
        Ok(store) => store,
        Err(response) => return response,
    };
    let stored = match load_tenant_goal(&store, &tenant, &goal_id) {
        Ok(goal) => goal,
        Err(response) => return response,
    };
    let waits = goal_waits(&state, &stored.tenant_context, &store, &goal_id);
    Json(json!({"goal_id": goal_id, "waits": waits, "count": waits.len()})).into_response()
}

pub(super) async fn get_goal_wait(
    State(state): State<AppState>,
    Extension(tenant): Extension<TenantContext>,
    Path((goal_id, wait_id)): Path<(String, String)>,
) -> Response {
    let store = match goal_store(&state) {
        Ok(store) => store,
        Err(response) => return response,
    };
    let stored = match load_tenant_goal(&store, &tenant, &goal_id) {
        Ok(goal) => goal,
        Err(response) => return response,
    };
    match goal_waits(&state, &stored.tenant_context, &store, &goal_id)
        .into_iter()
        .find(|wait| wait.wait_id == wait_id)
    {
        Some(wait) => Json(json!({"goal_id": goal_id, "wait": wait})).into_response(),
        None => (
            StatusCode::NOT_FOUND,
            Json(json!({"error": "wait_not_found"})),
        )
            .into_response(),
    }
}

#[derive(Debug, Deserialize)]
pub(super) struct WaitResolutionPayload {
    pub idempotency_key: String,
    #[serde(default)]
    pub payload: Value,
}

pub(super) async fn resolve_goal_wait(
    State(state): State<AppState>,
    Extension(tenant): Extension<TenantContext>,
    Extension(principal): Extension<RequestPrincipal>,
    verified: Option<Extension<VerifiedTenantContext>>,
    Path((goal_id, wait_id)): Path<(String, String)>,
    Json(payload): Json<WaitResolutionPayload>,
) -> Response {
    let verified = verified.as_deref();
    // Wait resolution injects external state into a paused run: it requires
    // the resolve capability (or an administrative role) on explicit tenants.
    if let Err(response) =
        require_goal_authority(&tenant, verified, Some("orchestration.resolve_wait"))
    {
        return response;
    }
    let store = match goal_store(&state) {
        Ok(store) => store,
        Err(response) => return response,
    };
    let stored = match load_tenant_goal(&store, &tenant, &goal_id) {
        Ok(goal) => goal,
        Err(response) => return response,
    };
    if !goal_waits(&state, &stored.tenant_context, &store, &goal_id)
        .iter()
        .any(|wait| wait.wait_id == wait_id)
    {
        return (
            StatusCode::NOT_FOUND,
            Json(json!({"error": "wait_not_found"})),
        )
            .into_response();
    }
    match state
        .resolve_automation_v2_external_wait(
            &stored.tenant_context,
            &wait_id,
            &payload.idempotency_key,
            payload.payload,
        )
        .await
    {
        Ok(Some(wait)) => {
            let actor = effective_actor(&principal, verified);
            publish_goal_audit_receipt(
                &state,
                &tenant,
                "orchestration.goal.wait_resolution_receipt",
                &goal_id,
                &actor,
                verified,
                serde_json::Map::from_iter([("waitID".to_string(), json!(wait_id))]),
            );
            Json(json!({"goal_id": goal_id, "wait": wait})).into_response()
        }
        Ok(None) => (
            StatusCode::CONFLICT,
            Json(json!({
                "error": "wait_resolution_conflict",
                "detail": "wait is not eligible for this resolution (already woken with a different idempotency key, or not claimable)",
            })),
        )
            .into_response(),
        Err(error) => goal_error_response(&error),
    }
}