treadle 0.2.0

A persistent, resumable, human-in-the-loop workflow engine backed by a petgraph DAG
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
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
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
# Treadle Phase 10 Implementation Plan

## Phase 10: Quality Gate Integration

**Goal:** Replace the no-op quality gate stub introduced in Phase 9 with actual `QualityGate::evaluate` calls. The judgement step now runs a real quality gate (when one is attached to a stage), and `QualityVerdict` drives the retry/escalate decision. `ReviewPolicy` variants are fully applied. New events (`QualityCheckPassed`, `QualityCheckFailed`, `Escalated`) provide observability into the quality evaluation lifecycle.

**Prerequisites:**
- Phases 1--9 completed
- `Artefact` trait, `StageOutput`, `Stage::execute` returning `StageOutput` (Phase 6)
- `QualityGate` trait, `QualityVerdict`, `QualityFeedback`, `CriterionResult`, `QualityContext` (Phase 7)
- `RetryBudget`, `ExhaustedAction`, `ReviewPolicy`, `ReviewOutcome`, `resolve_review` (Phase 8)
- Retry loop in `advance()` with no-op quality gate stub, `AttemptRecord`, attempt history, `attempt_timeout`, `RetryScheduled`/`RetryAttempt` events (Phase 9)
- `cargo build` and `cargo test` pass

---

## Milestone 10.1 -- New Events: QualityCheckPassed, QualityCheckFailed, Escalated

### Files to Modify
- `src/event.rs` (add new event variants)

### Implementation Details

#### 10.1.1 Add Event Variants

Add to `WorkflowEvent` in `src/event.rs`:

```rust
/// A quality gate accepted the stage output.
QualityCheckPassed {
    /// The work item's identifier (stringified).
    item_id: String,
    /// The stage name.
    stage: String,
    /// The attempt number that passed.
    attempt: u32,
},

/// A quality gate rejected the stage output.
QualityCheckFailed {
    /// The work item's identifier (stringified).
    item_id: String,
    /// The stage name.
    stage: String,
    /// The attempt number that failed.
    attempt: u32,
    /// A human-readable summary of the quality feedback.
    feedback_summary: String,
},

/// A stage has been escalated to human review.
///
/// Escalation occurs when:
/// - The retry budget is exhausted and `ExhaustedAction::Escalate` is set
/// - The quality gate returns `Uncertain`
/// - The `ReviewPolicy` requires review for the given outcome
Escalated {
    /// The work item's identifier (stringified).
    item_id: String,
    /// The stage name.
    stage: String,
    /// The reason for escalation.
    reason: String,
},
```

#### 10.1.2 Update `item_id()` and `stage()` Accessors

Update the `item_id()` match arm to include the new variants:

```rust
pub fn item_id(&self) -> &str {
    match self {
        Self::StageStarted { item_id, .. }
        | Self::StageCompleted { item_id, .. }
        | Self::StageFailed { item_id, .. }
        | Self::ReviewRequired { item_id, .. }
        | Self::StageSkipped { item_id, .. }
        | Self::StageRetried { item_id, .. }
        | Self::RetryScheduled { item_id, .. }
        | Self::RetryAttempt { item_id, .. }
        | Self::QualityCheckPassed { item_id, .. }
        | Self::QualityCheckFailed { item_id, .. }
        | Self::Escalated { item_id, .. }
        | Self::WorkflowCompleted { item_id } => item_id,
    }
}

pub fn stage(&self) -> Option<&str> {
    match self {
        Self::StageStarted { stage, .. }
        | Self::StageCompleted { stage, .. }
        | Self::StageFailed { stage, .. }
        | Self::ReviewRequired { stage, .. }
        | Self::StageSkipped { stage, .. }
        | Self::StageRetried { stage, .. }
        | Self::RetryScheduled { stage, .. }
        | Self::RetryAttempt { stage, .. }
        | Self::QualityCheckPassed { stage, .. }
        | Self::QualityCheckFailed { stage, .. }
        | Self::Escalated { stage, .. } => Some(stage),
        Self::WorkflowCompleted { .. } => None,
    }
}
```

### Design Decisions

- **`feedback_summary` is a `String`, not a full `QualityFeedback`**: Events should be lightweight and `Clone`-able for the broadcast channel. The full feedback is available in the `AttemptRecord`; the event carries only a human-readable summary for logging and real-time monitoring.
- **`Escalated` covers all escalation paths**: Whether escalation comes from budget exhaustion, an `Uncertain` verdict, or a `ReviewPolicy`, the same event is emitted. The `reason` field distinguishes the cause.
- **Events are `#[non_exhaustive]`**: Already the case from v1; new variants do not break downstream consumers.

### Tests

```rust
#[test]
fn test_quality_check_passed_event() {
    let event = WorkflowEvent::QualityCheckPassed {
        item_id: "item-1".into(),
        stage: "pdf_to_md".into(),
        attempt: 1,
    };
    assert_eq!(event.item_id(), "item-1");
    assert_eq!(event.stage(), Some("pdf_to_md"));
    assert!(!event.is_error());
}

#[test]
fn test_quality_check_failed_event() {
    let event = WorkflowEvent::QualityCheckFailed {
        item_id: "item-1".into(),
        stage: "pdf_to_md".into(),
        attempt: 2,
        feedback_summary: "Tables lost in conversion".into(),
    };
    assert_eq!(event.item_id(), "item-1");
    assert_eq!(event.stage(), Some("pdf_to_md"));
    let debug = format!("{:?}", event);
    assert!(debug.contains("QualityCheckFailed"));
    assert!(debug.contains("Tables lost"));
}

#[test]
fn test_quality_check_failed_event_is_not_error() {
    // QualityCheckFailed is a quality gate rejection, not a stage
    // execution error. The stage ran successfully but produced
    // inadequate output.
    let event = WorkflowEvent::QualityCheckFailed {
        item_id: "item-1".into(),
        stage: "pdf_to_md".into(),
        attempt: 1,
        feedback_summary: "Word count too low".into(),
    };
    assert!(!event.is_error());
}

#[test]
fn test_escalated_event() {
    let event = WorkflowEvent::Escalated {
        item_id: "item-1".into(),
        stage: "pdf_to_md".into(),
        reason: "Retry budget exhausted after 3 attempts".into(),
    };
    assert_eq!(event.item_id(), "item-1");
    assert_eq!(event.stage(), Some("pdf_to_md"));
    let debug = format!("{:?}", event);
    assert!(debug.contains("Escalated"));
    assert!(debug.contains("Retry budget exhausted"));
}

#[test]
fn test_escalated_event_clone() {
    let event = WorkflowEvent::Escalated {
        item_id: "item-1".into(),
        stage: "extract".into(),
        reason: "Uncertain quality verdict".into(),
    };
    let cloned = event.clone();
    assert_eq!(event.item_id(), cloned.item_id());
    assert_eq!(event.stage(), cloned.stage());
}

#[test]
fn test_quality_events_are_not_completion() {
    let passed = WorkflowEvent::QualityCheckPassed {
        item_id: "x".into(),
        stage: "s".into(),
        attempt: 1,
    };
    let failed = WorkflowEvent::QualityCheckFailed {
        item_id: "x".into(),
        stage: "s".into(),
        attempt: 1,
        feedback_summary: "bad".into(),
    };
    let escalated = WorkflowEvent::Escalated {
        item_id: "x".into(),
        stage: "s".into(),
        reason: "r".into(),
    };
    assert!(!passed.is_completion());
    assert!(!failed.is_completion());
    assert!(!escalated.is_completion());
}
```

### Verification Commands

```bash
cargo build
cargo test event
cargo clippy
```

---

## Milestone 10.2 -- Wire Quality Gate Evaluation into Retry Loop

### Files to Modify
- `src/workflow.rs` (modify `execute_stage_with_retry` method)

### Implementation Details

This is the core milestone. The no-op quality gate stub from Phase 9:

```rust
// === QUALITY GATE STUB: always Accepted ===
// In Phase 10, this will call the actual quality gate.
let verdict = QualityVerdict::Accepted;
```

is replaced with actual quality gate evaluation. The full retry loop is revised to handle all three verdict types and all five `ReviewPolicy` variants.

#### 10.2.1 Quality Gate Lookup

The `Workflow` struct already stores quality gates per stage (added in Phase 7 via `WorkflowBuilder::quality_gate()`). Add a helper method to look up the gate:

```rust
/// Returns the quality gate for a stage, if one is attached.
pub(crate) fn get_quality_gate(
    &self,
    stage_name: &str,
) -> Option<&Arc<dyn QualityGate<W>>> {
    self.quality_gates.get(stage_name)
}
```

Where `quality_gates: HashMap<String, Arc<dyn QualityGate<W>>>` was added to `Workflow` in Phase 7.

#### 10.2.2 Build QualityContext

Add a helper to construct the `QualityContext` from the current state:

```rust
/// Builds a [`QualityContext`] for a quality gate evaluation.
fn build_quality_context(
    &self,
    item: &W,
    stage_name: &str,
    attempt: u32,
    previous_attempts: &[AttemptRecord],
) -> QualityContext {
    let budget = self.get_retry_budget(stage_name);
    QualityContext {
        attempt,
        max_attempts: budget.max_attempts,
        previous_attempts: previous_attempts.to_vec(),
        stage_name: stage_name.to_string(),
        item_id: item.id().to_string(),
    }
}
```

#### 10.2.3 ReviewPolicy Resolution Helper

Add a helper that determines whether a given `ReviewPolicy` requires review for a specific situation:

```rust
/// Determines whether the review policy requires human review.
///
/// # Arguments
///
/// * `policy` - The review policy for this stage.
/// * `is_exhausted` - Whether the retry budget was exhausted.
/// * `is_uncertain` - Whether the quality verdict was `Uncertain`.
fn requires_review(
    policy: &ReviewPolicy,
    is_exhausted: bool,
    is_uncertain: bool,
) -> bool {
    match policy {
        ReviewPolicy::Never => false,
        ReviewPolicy::Always => true,
        ReviewPolicy::OnEscalation => is_exhausted,
        ReviewPolicy::OnUncertain => is_uncertain,
        ReviewPolicy::OnEscalationOrUncertain => is_exhausted || is_uncertain,
    }
}
```

#### 10.2.4 Revised `execute_stage_with_retry`

The complete revised method, replacing the Phase 9 stub:

```rust
async fn execute_stage_with_retry(
    &self,
    item: &W,
    stage_name: &str,
    store: &dyn StateStore<W>,
    event_tx: &broadcast::Sender<WorkflowEvent>,
) -> Result<()> {
    let stage = self.get_stage(stage_name)?;
    let budget = self.get_retry_budget(stage_name);
    let review_policy = self.get_review_policy(stage_name);
    let quality_gate = self.get_quality_gate(stage_name);

    let mut attempt: u32 = 1;
    let mut feedback: Option<QualityFeedback> = None;

    loop {
        // Emit RetryAttempt event for retries (attempt > 1)
        if attempt > 1 {
            let _ = event_tx.send(WorkflowEvent::RetryAttempt {
                item_id: item.id().to_string(),
                stage: stage_name.to_string(),
                attempt,
                max_attempts: budget.max_attempts,
                feedback_summary: feedback.as_ref().map(|f| f.summary.clone()),
            });
        }

        // Build context with attempt number and previous feedback
        let mut ctx = StageContext::new(item.id(), stage_name)
            .with_attempt(attempt);
        if let Some(ref fb) = feedback {
            ctx = ctx.with_feedback(serde_json::to_value(fb).unwrap_or_default());
        }
        // Populate upstream artefacts from completed dependencies
        for dep_name in self.dependencies(stage_name)? {
            if let Some(json) = store.get_artefact_summary(
                item.id(), dep_name,
            ).await? {
                ctx = ctx.with_upstream_artefact(
                    dep_name,
                    Box::new(json) as Box<dyn Artefact>,
                );
            }
        }

        // Mark stage as running
        store.set_status(
            item.id(), stage_name, StageStatus::running(),
        ).await?;

        // Execute with optional timeout
        let exec_result = if let Some(timeout_duration) = budget.attempt_timeout {
            match tokio::time::timeout(
                timeout_duration,
                stage.execute(item, &ctx),
            ).await {
                Ok(result) => result,
                Err(_elapsed) => {
                    // Timeout: treat as a failed attempt
                    let record = AttemptRecord::start(attempt)
                        .with_output_summary("Attempt timed out")
                        .complete();
                    store.record_attempt(
                        item.id(), stage_name, record,
                    ).await?;

                    // Check if we can retry
                    if attempt < budget.max_attempts {
                        attempt += 1;
                        feedback = Some(QualityFeedback {
                            summary: format!(
                                "Attempt timed out after {}ms",
                                timeout_duration.as_millis()
                            ),
                            failed_criteria: vec![],
                            guidance: None,
                        });
                        let _ = event_tx.send(WorkflowEvent::RetryScheduled {
                            item_id: item.id().to_string(),
                            stage: stage_name.to_string(),
                            attempt,
                            max_attempts: budget.max_attempts,
                        });
                        if let Some(delay) = budget.delay {
                            tokio::time::sleep(delay).await;
                        }
                        continue;
                    }
                    // Exhausted
                    return self.handle_exhausted(
                        item, stage_name, &budget, &review_policy,
                        store, event_tx,
                    ).await;
                }
            }
        } else {
            stage.execute(item, &ctx).await
        };

        // Handle execution errors
        let output = match exec_result {
            Ok(output) => output,
            Err(e) => {
                let record = AttemptRecord::start(attempt)
                    .with_output_summary(format!("Error: {}", e))
                    .complete();
                store.record_attempt(
                    item.id(), stage_name, record,
                ).await?;
                store.set_status(
                    item.id(),
                    stage_name,
                    StageStatus::failed(e.to_string()),
                ).await?;
                return Err(e);
            }
        };

        // Store artefact summary
        let artefact_json = output.artefacts.as_ref()
            .and_then(|a| a.to_json());
        store.store_artefact_summary(
            item.id(), stage_name, artefact_json.clone(),
        ).await?;

        // === QUALITY GATE EVALUATION ===
        let previous_attempts = store.get_attempts(
            item.id(), stage_name,
        ).await?;

        let verdict = if let Some(gate) = quality_gate {
            let quality_ctx = self.build_quality_context(
                item, stage_name, attempt, &previous_attempts,
            );
            gate.evaluate(item, stage_name, &output, &quality_ctx).await?
        } else {
            // No quality gate attached: behave like v1 (always accepted)
            QualityVerdict::Accepted
        };

        // Record the attempt with its verdict
        let mut record = AttemptRecord::start(attempt)
            .with_artefacts(artefact_json);
        if let Some(ref summary) = output.summary {
            record = record.with_output_summary(summary.clone());
        }
        record = record.with_verdict(&verdict);
        let record = record.complete();
        store.record_attempt(
            item.id(), stage_name, record,
        ).await?;

        // Handle verdict
        match verdict {
            QualityVerdict::Accepted => {
                // Emit quality check passed event
                let _ = event_tx.send(WorkflowEvent::QualityCheckPassed {
                    item_id: item.id().to_string(),
                    stage: stage_name.to_string(),
                    attempt,
                });

                // Apply review policy for the accepted case
                if Self::requires_review(
                    &review_policy,
                    /* is_exhausted */ false,
                    /* is_uncertain */ false,
                ) {
                    // Only ReviewPolicy::Always reaches here
                    store.set_status(
                        item.id(),
                        stage_name,
                        StageStatus::awaiting_review(),
                    ).await?;
                    let _ = event_tx.send(WorkflowEvent::Escalated {
                        item_id: item.id().to_string(),
                        stage: stage_name.to_string(),
                        reason: "ReviewPolicy::Always requires human review"
                            .to_string(),
                    });
                    return Ok(());
                }

                // No review required: stage completes
                store.set_status(
                    item.id(),
                    stage_name,
                    StageStatus::completed(),
                ).await?;
                let _ = event_tx.send(WorkflowEvent::StageCompleted {
                    item_id: item.id().to_string(),
                    stage: stage_name.to_string(),
                });
                return Ok(());
            }

            QualityVerdict::Rejected { feedback: fb } => {
                // Emit quality check failed event
                let _ = event_tx.send(WorkflowEvent::QualityCheckFailed {
                    item_id: item.id().to_string(),
                    stage: stage_name.to_string(),
                    attempt,
                    feedback_summary: fb.summary.clone(),
                });

                // Check if retries remain
                if attempt < budget.max_attempts {
                    attempt += 1;
                    feedback = Some(fb);
                    let _ = event_tx.send(WorkflowEvent::RetryScheduled {
                        item_id: item.id().to_string(),
                        stage: stage_name.to_string(),
                        attempt,
                        max_attempts: budget.max_attempts,
                    });
                    if let Some(delay) = budget.delay {
                        tokio::time::sleep(delay).await;
                    }
                    continue;
                }

                // Retry budget exhausted
                return self.handle_exhausted(
                    item, stage_name, &budget, &review_policy,
                    store, event_tx,
                ).await;
            }

            QualityVerdict::Uncertain { reason } => {
                // Uncertain: escalate to review regardless of retry budget
                let _ = event_tx.send(WorkflowEvent::Escalated {
                    item_id: item.id().to_string(),
                    stage: stage_name.to_string(),
                    reason: reason.clone(),
                });

                store.set_status(
                    item.id(),
                    stage_name,
                    StageStatus::awaiting_review(),
                ).await?;
                return Ok(());
            }
        }
    }
}
```

#### 10.2.5 Revised `handle_exhausted` Helper

The `handle_exhausted` method from Phase 9 is updated to accept `review_policy` and to apply it when determining the exhausted behaviour:

```rust
async fn handle_exhausted(
    &self,
    item: &W,
    stage_name: &str,
    budget: &RetryBudget,
    review_policy: &ReviewPolicy,
    store: &dyn StateStore<W>,
    event_tx: &broadcast::Sender<WorkflowEvent>,
) -> Result<()> {
    match budget.on_exhausted {
        ExhaustedAction::Fail => {
            // Check if review policy overrides the fail
            if Self::requires_review(
                review_policy,
                /* is_exhausted */ true,
                /* is_uncertain */ false,
            ) {
                store.set_status(
                    item.id(),
                    stage_name,
                    StageStatus::awaiting_review(),
                ).await?;
                let _ = event_tx.send(WorkflowEvent::Escalated {
                    item_id: item.id().to_string(),
                    stage: stage_name.to_string(),
                    reason: "Retry budget exhausted; review policy \
                             requires human review".to_string(),
                });
                return Ok(());
            }

            store.set_status(
                item.id(),
                stage_name,
                StageStatus::failed("Retry budget exhausted"),
            ).await?;
            let _ = event_tx.send(WorkflowEvent::StageFailed {
                item_id: item.id().to_string(),
                stage: stage_name.to_string(),
                error: "Retry budget exhausted".to_string(),
            });
            Ok(())
        }

        ExhaustedAction::Escalate => {
            store.set_status(
                item.id(),
                stage_name,
                StageStatus::awaiting_review(),
            ).await?;
            let _ = event_tx.send(WorkflowEvent::Escalated {
                item_id: item.id().to_string(),
                stage: stage_name.to_string(),
                reason: "Retry budget exhausted".to_string(),
            });
            Ok(())
        }
    }
}
```

### Design Decisions

- **No quality gate = v1 behaviour**: When no quality gate is attached to a stage, the verdict is always `Accepted`. This preserves complete backward compatibility: v1 stages with no quality gate, no retry budget, and `ReviewPolicy::Never` behave identically to v1.
- **`QualityContext` includes previous attempts**: The gate has full visibility into the attempt history, enabling intelligent decisions such as "the last two attempts both failed on table extraction, escalate now rather than retrying with the same strategy."
- **`ReviewPolicy` is consulted on both accepted and exhausted paths**: `ReviewPolicy::Always` blocks even when quality passes. `ReviewPolicy::OnEscalation` blocks when the budget is exhausted, even if `ExhaustedAction::Fail` is set (the review policy overrides the fail action). This gives operators fine-grained control.
- **`Uncertain` always escalates**: The design document specifies that `Uncertain` escalates "regardless of retry budget." This means even if retries remain, an `Uncertain` verdict immediately escalates. The rationale: if the gate cannot determine quality, retrying will not help -- human judgement is required.
- **`requires_review` is a pure function**: It takes the policy and two booleans, making it trivially testable without constructing a full workflow.
- **Events are emitted before status updates where possible**: This ensures subscribers see the event before the state store reflects the change, matching the chronological order of operations.

### Tests

```rust
#[test]
fn test_requires_review_never() {
    assert!(!Workflow::requires_review(&ReviewPolicy::Never, false, false));
    assert!(!Workflow::requires_review(&ReviewPolicy::Never, true, false));
    assert!(!Workflow::requires_review(&ReviewPolicy::Never, false, true));
    assert!(!Workflow::requires_review(&ReviewPolicy::Never, true, true));
}

#[test]
fn test_requires_review_always() {
    assert!(Workflow::requires_review(&ReviewPolicy::Always, false, false));
    assert!(Workflow::requires_review(&ReviewPolicy::Always, true, false));
    assert!(Workflow::requires_review(&ReviewPolicy::Always, false, true));
    assert!(Workflow::requires_review(&ReviewPolicy::Always, true, true));
}

#[test]
fn test_requires_review_on_escalation() {
    assert!(!Workflow::requires_review(&ReviewPolicy::OnEscalation, false, false));
    assert!(Workflow::requires_review(&ReviewPolicy::OnEscalation, true, false));
    assert!(!Workflow::requires_review(&ReviewPolicy::OnEscalation, false, true));
    assert!(Workflow::requires_review(&ReviewPolicy::OnEscalation, true, true));
}

#[test]
fn test_requires_review_on_uncertain() {
    assert!(!Workflow::requires_review(&ReviewPolicy::OnUncertain, false, false));
    assert!(!Workflow::requires_review(&ReviewPolicy::OnUncertain, true, false));
    assert!(Workflow::requires_review(&ReviewPolicy::OnUncertain, false, true));
    assert!(Workflow::requires_review(&ReviewPolicy::OnUncertain, true, true));
}

#[test]
fn test_requires_review_on_escalation_or_uncertain() {
    assert!(!Workflow::requires_review(
        &ReviewPolicy::OnEscalationOrUncertain, false, false,
    ));
    assert!(Workflow::requires_review(
        &ReviewPolicy::OnEscalationOrUncertain, true, false,
    ));
    assert!(Workflow::requires_review(
        &ReviewPolicy::OnEscalationOrUncertain, false, true,
    ));
    assert!(Workflow::requires_review(
        &ReviewPolicy::OnEscalationOrUncertain, true, true,
    ));
}
```

### Verification Commands

```bash
cargo build
cargo test workflow
cargo clippy
```

---

## Milestone 10.3 -- End-to-End Tests

### Files to Create/Modify
- `tests/quality_gate_integration.rs` (new integration test file)

### Implementation Details

#### 10.3.1 Test Gate Implementations

These gates are defined at the top of the integration test file. They implement `QualityGate<TestItem>` and provide controlled behaviour for testing:

```rust
use async_trait::async_trait;
use std::sync::Arc;
use tokio::sync::Mutex;

use treadle::{
    Artefact, AttemptRecord, CriterionResult, ExhaustedAction, QualityContext,
    QualityFeedback, QualityGate, QualityVerdict, Result, RetryBudget,
    ReviewPolicy, Stage, StageContext, StageOutcome, StageOutput, StateStore,
    WorkItem, Workflow, WorkflowEvent, MemoryStateStore,
};

// --- Test work item ---

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
struct TestItem {
    id: String,
}

impl TestItem {
    fn new(id: impl Into<String>) -> Self {
        Self { id: id.into() }
    }
}

impl WorkItem for TestItem {
    fn id(&self) -> &str {
        &self.id
    }
}

// --- Test stage ---

/// A stage that always completes with a JSON artefact.
#[derive(Debug)]
struct SimpleStage;

#[async_trait]
impl Stage<TestItem> for SimpleStage {
    fn name(&self) -> &str {
        "simple"
    }

    async fn execute(
        &self,
        _item: &TestItem,
        _ctx: &StageContext,
    ) -> Result<StageOutput> {
        Ok(StageOutput::completed_with(
            serde_json::json!({"result": "ok"}),
        ))
    }
}

/// A stage that records which attempt numbers it sees.
#[derive(Debug)]
struct AttemptAwareStage {
    attempts_seen: Arc<Mutex<Vec<u32>>>,
}

#[async_trait]
impl Stage<TestItem> for AttemptAwareStage {
    fn name(&self) -> &str {
        "attempt_aware"
    }

    async fn execute(
        &self,
        _item: &TestItem,
        ctx: &StageContext,
    ) -> Result<StageOutput> {
        self.attempts_seen.lock().await.push(ctx.attempt);
        Ok(StageOutput::completed_with(
            serde_json::json!({"attempt": ctx.attempt}),
        ))
    }
}

// --- Test quality gates ---

/// A quality gate that always accepts.
struct AlwaysAcceptGate;

#[async_trait]
impl QualityGate<TestItem> for AlwaysAcceptGate {
    async fn evaluate(
        &self,
        _item: &TestItem,
        _stage: &str,
        _output: &StageOutput,
        _ctx: &QualityContext,
    ) -> Result<QualityVerdict> {
        Ok(QualityVerdict::Accepted)
    }
}

/// A quality gate that always rejects with configurable feedback.
struct AlwaysRejectGate {
    feedback: QualityFeedback,
}

#[async_trait]
impl QualityGate<TestItem> for AlwaysRejectGate {
    async fn evaluate(
        &self,
        _item: &TestItem,
        _stage: &str,
        _output: &StageOutput,
        _ctx: &QualityContext,
    ) -> Result<QualityVerdict> {
        Ok(QualityVerdict::Rejected {
            feedback: self.feedback.clone(),
        })
    }
}

/// A quality gate that rejects until a specific attempt number,
/// then accepts.
struct AcceptOnAttemptGate {
    accept_on: u32,
}

#[async_trait]
impl QualityGate<TestItem> for AcceptOnAttemptGate {
    async fn evaluate(
        &self,
        _item: &TestItem,
        _stage: &str,
        _output: &StageOutput,
        ctx: &QualityContext,
    ) -> Result<QualityVerdict> {
        if ctx.attempt >= self.accept_on {
            Ok(QualityVerdict::Accepted)
        } else {
            Ok(QualityVerdict::Rejected {
                feedback: QualityFeedback {
                    summary: format!(
                        "Not ready yet (attempt {}/{})",
                        ctx.attempt, self.accept_on
                    ),
                    failed_criteria: vec![CriterionResult {
                        name: "readiness".to_string(),
                        expected: format!("attempt >= {}", self.accept_on),
                        actual: format!("attempt {}", ctx.attempt),
                        passed: false,
                    }],
                    guidance: None,
                },
            })
        }
    }
}

/// A quality gate that always returns Uncertain.
struct UncertainGate {
    reason: String,
}

#[async_trait]
impl QualityGate<TestItem> for UncertainGate {
    async fn evaluate(
        &self,
        _item: &TestItem,
        _stage: &str,
        _output: &StageOutput,
        _ctx: &QualityContext,
    ) -> Result<QualityVerdict> {
        Ok(QualityVerdict::Uncertain {
            reason: self.reason.clone(),
        })
    }
}
```

#### 10.3.2 Test 1: Quality Gate Accepts on First Attempt

```rust
#[tokio::test]
async fn test_quality_gate_accepts_on_first_attempt() {
    let workflow = Workflow::builder()
        .stage("a", SimpleStage)
        .quality_gate("a", AlwaysAcceptGate)
        .build()
        .unwrap();

    let store = MemoryStateStore::<TestItem>::new();
    let item = TestItem::new("item-1");

    workflow.advance(&item, &store).await.unwrap();

    let status = store.get_status(item.id(), "a").await.unwrap().unwrap();
    assert_eq!(status.state, StageState::Completed);

    let attempts = store.get_attempts(item.id(), "a").await.unwrap();
    assert_eq!(attempts.len(), 1);
    assert_eq!(attempts[0].attempt, 1);
    assert!(matches!(
        attempts[0].quality_verdict,
        Some(SerializableVerdict::Accepted)
    ));
}
```

#### 10.3.3 Test 2: Quality Gate Rejects Then Accepts

```rust
#[tokio::test]
async fn test_quality_gate_rejects_then_accepts() {
    let attempts_seen = Arc::new(Mutex::new(Vec::new()));

    let workflow = Workflow::builder()
        .stage("a", AttemptAwareStage {
            attempts_seen: attempts_seen.clone(),
        })
        .quality_gate("a", AcceptOnAttemptGate { accept_on: 2 })
        .retry_budget("a", RetryBudget {
            max_attempts: 3,
            delay: None,
            attempt_timeout: None,
            on_exhausted: ExhaustedAction::Fail,
        })
        .build()
        .unwrap();

    let store = MemoryStateStore::<TestItem>::new();
    let item = TestItem::new("item-1");

    workflow.advance(&item, &store).await.unwrap();

    // Stage should complete on attempt 2
    let status = store.get_status(item.id(), "a").await.unwrap().unwrap();
    assert_eq!(status.state, StageState::Completed);

    // Verify two attempts were recorded
    let attempts = store.get_attempts(item.id(), "a").await.unwrap();
    assert_eq!(attempts.len(), 2);
    assert_eq!(attempts[0].attempt, 1);
    assert!(matches!(
        attempts[0].quality_verdict,
        Some(SerializableVerdict::Rejected)
    ));
    assert_eq!(attempts[1].attempt, 2);
    assert!(matches!(
        attempts[1].quality_verdict,
        Some(SerializableVerdict::Accepted)
    ));

    // Verify the stage saw both attempts
    let seen = attempts_seen.lock().await;
    assert_eq!(*seen, vec![1, 2]);
}
```

#### 10.3.4 Test 3: Quality Gate Exhausts and Fails

```rust
#[tokio::test]
async fn test_quality_gate_exhausts_and_fails() {
    let reject_feedback = QualityFeedback {
        summary: "Tables missing from output".to_string(),
        failed_criteria: vec![CriterionResult {
            name: "table_preservation".to_string(),
            expected: "Tables present".to_string(),
            actual: "No tables found".to_string(),
            passed: false,
        }],
        guidance: Some(serde_json::json!({
            "hint": "Try OCR-based extraction"
        })),
    };

    let workflow = Workflow::builder()
        .stage("a", SimpleStage)
        .quality_gate("a", AlwaysRejectGate {
            feedback: reject_feedback,
        })
        .retry_budget("a", RetryBudget {
            max_attempts: 3,
            delay: None,
            attempt_timeout: None,
            on_exhausted: ExhaustedAction::Fail,
        })
        .build()
        .unwrap();

    let store = MemoryStateStore::<TestItem>::new();
    let item = TestItem::new("item-1");

    workflow.advance(&item, &store).await.unwrap();

    // Stage should be failed
    let status = store.get_status(item.id(), "a").await.unwrap().unwrap();
    assert_eq!(status.state, StageState::Failed);

    // All 3 attempts should be recorded, all rejected
    let attempts = store.get_attempts(item.id(), "a").await.unwrap();
    assert_eq!(attempts.len(), 3);
    for (i, record) in attempts.iter().enumerate() {
        assert_eq!(record.attempt, (i as u32) + 1);
        assert!(matches!(
            record.quality_verdict,
            Some(SerializableVerdict::Rejected)
        ));
    }
}
```

#### 10.3.5 Test 4: Quality Gate Exhausts and Escalates

```rust
#[tokio::test]
async fn test_quality_gate_exhausts_and_escalates() {
    let reject_feedback = QualityFeedback {
        summary: "Insufficient quality".to_string(),
        failed_criteria: vec![],
        guidance: None,
    };

    let workflow = Workflow::builder()
        .stage("a", SimpleStage)
        .quality_gate("a", AlwaysRejectGate {
            feedback: reject_feedback,
        })
        .retry_budget("a", RetryBudget {
            max_attempts: 3,
            delay: None,
            attempt_timeout: None,
            on_exhausted: ExhaustedAction::Escalate,
        })
        .build()
        .unwrap();

    let store = MemoryStateStore::<TestItem>::new();
    let item = TestItem::new("item-1");

    workflow.advance(&item, &store).await.unwrap();

    // Stage should be awaiting review (escalated)
    let status = store.get_status(item.id(), "a").await.unwrap().unwrap();
    assert_eq!(status.state, StageState::AwaitingReview);

    // All 3 attempts should be recorded
    let attempts = store.get_attempts(item.id(), "a").await.unwrap();
    assert_eq!(attempts.len(), 3);
}
```

#### 10.3.6 Test 5: Quality Gate Uncertain Escalates

```rust
#[tokio::test]
async fn test_quality_gate_uncertain_escalates() {
    let workflow = Workflow::builder()
        .stage("a", SimpleStage)
        .quality_gate("a", UncertainGate {
            reason: "Cannot assess table quality automatically".to_string(),
        })
        .retry_budget("a", RetryBudget {
            max_attempts: 5,  // Plenty of retries -- should not be used
            delay: None,
            attempt_timeout: None,
            on_exhausted: ExhaustedAction::Fail,
        })
        .build()
        .unwrap();

    let store = MemoryStateStore::<TestItem>::new();
    let item = TestItem::new("item-1");

    workflow.advance(&item, &store).await.unwrap();

    // Stage should immediately escalate on attempt 1
    let status = store.get_status(item.id(), "a").await.unwrap().unwrap();
    assert_eq!(status.state, StageState::AwaitingReview);

    // Only 1 attempt should be recorded (no retries)
    let attempts = store.get_attempts(item.id(), "a").await.unwrap();
    assert_eq!(attempts.len(), 1);
    assert!(matches!(
        attempts[0].quality_verdict,
        Some(SerializableVerdict::Uncertain { .. })
    ));
}
```

#### 10.3.7 Test 6: ReviewPolicy::Always with Quality Gate

```rust
#[tokio::test]
async fn test_review_policy_always_with_quality_gate() {
    let workflow = Workflow::builder()
        .stage("a", SimpleStage)
        .quality_gate("a", AlwaysAcceptGate)
        .review_policy("a", ReviewPolicy::Always)
        .build()
        .unwrap();

    let store = MemoryStateStore::<TestItem>::new();
    let item = TestItem::new("item-1");

    workflow.advance(&item, &store).await.unwrap();

    // Quality gate accepted, but ReviewPolicy::Always blocks for review
    let status = store.get_status(item.id(), "a").await.unwrap().unwrap();
    assert_eq!(status.state, StageState::AwaitingReview);

    // Only 1 attempt, and it was accepted
    let attempts = store.get_attempts(item.id(), "a").await.unwrap();
    assert_eq!(attempts.len(), 1);
    assert!(matches!(
        attempts[0].quality_verdict,
        Some(SerializableVerdict::Accepted)
    ));
}
```

#### 10.3.8 Test 7: ReviewPolicy::OnEscalation Only Triggers on Exhaust

```rust
#[tokio::test]
async fn test_review_policy_on_escalation_only_triggers_on_exhaust() {
    // Scenario A: quality passes -> completes normally (no review)
    let workflow_pass = Workflow::builder()
        .stage("a", SimpleStage)
        .quality_gate("a", AlwaysAcceptGate)
        .review_policy("a", ReviewPolicy::OnEscalation)
        .build()
        .unwrap();

    let store_pass = MemoryStateStore::<TestItem>::new();
    let item = TestItem::new("item-1");

    workflow_pass.advance(&item, &store_pass).await.unwrap();

    let status = store_pass.get_status(item.id(), "a")
        .await.unwrap().unwrap();
    assert_eq!(status.state, StageState::Completed);

    // Scenario B: quality exhausts -> AwaitingReview even with
    // ExhaustedAction::Fail, because ReviewPolicy::OnEscalation overrides
    let reject_feedback = QualityFeedback {
        summary: "bad".to_string(),
        failed_criteria: vec![],
        guidance: None,
    };

    let workflow_fail = Workflow::builder()
        .stage("a", SimpleStage)
        .quality_gate("a", AlwaysRejectGate {
            feedback: reject_feedback,
        })
        .retry_budget("a", RetryBudget {
            max_attempts: 2,
            delay: None,
            attempt_timeout: None,
            on_exhausted: ExhaustedAction::Fail,
        })
        .review_policy("a", ReviewPolicy::OnEscalation)
        .build()
        .unwrap();

    let store_fail = MemoryStateStore::<TestItem>::new();
    let item2 = TestItem::new("item-2");

    workflow_fail.advance(&item2, &store_fail).await.unwrap();

    let status = store_fail.get_status(item2.id(), "a")
        .await.unwrap().unwrap();
    assert_eq!(status.state, StageState::AwaitingReview);
}
```

#### 10.3.9 Test 8: Feedback Threaded to Stage Context

```rust
#[tokio::test]
async fn test_feedback_threaded_to_stage_context() {
    let feedback_seen = Arc::new(Mutex::new(Vec::new()));
    let feedback_capture = feedback_seen.clone();

    /// A stage that captures the feedback from its context.
    #[derive(Debug)]
    struct FeedbackCapturingStage {
        feedback_seen: Arc<Mutex<Vec<Option<serde_json::Value>>>>,
    }

    #[async_trait]
    impl Stage<TestItem> for FeedbackCapturingStage {
        fn name(&self) -> &str {
            "capturing"
        }

        async fn execute(
            &self,
            _item: &TestItem,
            ctx: &StageContext,
        ) -> Result<StageOutput> {
            self.feedback_seen.lock().await.push(ctx.feedback.clone());
            Ok(StageOutput::completed_with(
                serde_json::json!({"attempt": ctx.attempt}),
            ))
        }
    }

    let workflow = Workflow::builder()
        .stage("a", FeedbackCapturingStage {
            feedback_seen: feedback_capture,
        })
        .quality_gate("a", AcceptOnAttemptGate { accept_on: 3 })
        .retry_budget("a", RetryBudget {
            max_attempts: 3,
            delay: None,
            attempt_timeout: None,
            on_exhausted: ExhaustedAction::Fail,
        })
        .build()
        .unwrap();

    let store = MemoryStateStore::<TestItem>::new();
    let item = TestItem::new("item-1");

    workflow.advance(&item, &store).await.unwrap();

    let seen = feedback_seen.lock().await;
    assert_eq!(seen.len(), 3);

    // Attempt 1: no feedback (first attempt)
    assert!(seen[0].is_none());

    // Attempt 2: feedback from the gate's rejection of attempt 1
    assert!(seen[1].is_some());
    let fb1 = seen[1].as_ref().unwrap();
    assert!(fb1["summary"].as_str().unwrap().contains("attempt 1"));

    // Attempt 3: feedback from the gate's rejection of attempt 2
    assert!(seen[2].is_some());
    let fb2 = seen[2].as_ref().unwrap();
    assert!(fb2["summary"].as_str().unwrap().contains("attempt 2"));
}
```

#### 10.3.10 Test 9: Events Emitted Correctly

```rust
#[tokio::test]
async fn test_events_emitted_correctly() {
    let workflow = Workflow::builder()
        .stage("a", SimpleStage)
        .quality_gate("a", AcceptOnAttemptGate { accept_on: 2 })
        .retry_budget("a", RetryBudget {
            max_attempts: 3,
            delay: None,
            attempt_timeout: None,
            on_exhausted: ExhaustedAction::Fail,
        })
        .build()
        .unwrap();

    let mut receiver = workflow.subscribe();
    let store = MemoryStateStore::<TestItem>::new();
    let item = TestItem::new("item-1");

    workflow.advance(&item, &store).await.unwrap();

    // Collect all events
    let mut events = Vec::new();
    while let Ok(event) = receiver.try_recv() {
        events.push(event);
    }

    // Expected sequence:
    // 1. StageStarted (attempt 1 implicit)
    // 2. QualityCheckFailed (attempt 1 rejected)
    // 3. RetryScheduled (attempt 2 scheduled)
    // 4. RetryAttempt (attempt 2 starting)
    // 5. QualityCheckPassed (attempt 2 accepted)
    // 6. StageCompleted

    // Verify QualityCheckFailed was emitted
    let failed_events: Vec<_> = events.iter().filter(|e| {
        matches!(e, WorkflowEvent::QualityCheckFailed { .. })
    }).collect();
    assert_eq!(failed_events.len(), 1);
    if let WorkflowEvent::QualityCheckFailed {
        attempt, feedback_summary, ..
    } = &failed_events[0] {
        assert_eq!(*attempt, 1);
        assert!(feedback_summary.contains("attempt 1"));
    }

    // Verify QualityCheckPassed was emitted
    let passed_events: Vec<_> = events.iter().filter(|e| {
        matches!(e, WorkflowEvent::QualityCheckPassed { .. })
    }).collect();
    assert_eq!(passed_events.len(), 1);
    if let WorkflowEvent::QualityCheckPassed { attempt, .. } = &passed_events[0] {
        assert_eq!(*attempt, 2);
    }

    // Verify RetryScheduled was emitted
    let retry_events: Vec<_> = events.iter().filter(|e| {
        matches!(e, WorkflowEvent::RetryScheduled { .. })
    }).collect();
    assert_eq!(retry_events.len(), 1);

    // Verify RetryAttempt was emitted
    let attempt_events: Vec<_> = events.iter().filter(|e| {
        matches!(e, WorkflowEvent::RetryAttempt { .. })
    }).collect();
    assert_eq!(attempt_events.len(), 1);
    if let WorkflowEvent::RetryAttempt {
        attempt, feedback_summary, ..
    } = &attempt_events[0] {
        assert_eq!(*attempt, 2);
        assert!(feedback_summary.is_some());
    }

    // Verify StageCompleted was emitted
    assert!(events.iter().any(|e| {
        matches!(e, WorkflowEvent::StageCompleted { stage, .. } if stage == "a")
    }));
}

#[tokio::test]
async fn test_escalated_event_emitted_on_uncertain() {
    let workflow = Workflow::builder()
        .stage("a", SimpleStage)
        .quality_gate("a", UncertainGate {
            reason: "Cannot determine quality".to_string(),
        })
        .build()
        .unwrap();

    let mut receiver = workflow.subscribe();
    let store = MemoryStateStore::<TestItem>::new();
    let item = TestItem::new("item-1");

    workflow.advance(&item, &store).await.unwrap();

    let mut events = Vec::new();
    while let Ok(event) = receiver.try_recv() {
        events.push(event);
    }

    let escalated: Vec<_> = events.iter().filter(|e| {
        matches!(e, WorkflowEvent::Escalated { .. })
    }).collect();
    assert_eq!(escalated.len(), 1);
    if let WorkflowEvent::Escalated { reason, .. } = &escalated[0] {
        assert_eq!(reason, "Cannot determine quality");
    }
}

#[tokio::test]
async fn test_escalated_event_emitted_on_exhaust() {
    let reject_feedback = QualityFeedback {
        summary: "bad".to_string(),
        failed_criteria: vec![],
        guidance: None,
    };

    let workflow = Workflow::builder()
        .stage("a", SimpleStage)
        .quality_gate("a", AlwaysRejectGate {
            feedback: reject_feedback,
        })
        .retry_budget("a", RetryBudget {
            max_attempts: 2,
            delay: None,
            attempt_timeout: None,
            on_exhausted: ExhaustedAction::Escalate,
        })
        .build()
        .unwrap();

    let mut receiver = workflow.subscribe();
    let store = MemoryStateStore::<TestItem>::new();
    let item = TestItem::new("item-1");

    workflow.advance(&item, &store).await.unwrap();

    let mut events = Vec::new();
    while let Ok(event) = receiver.try_recv() {
        events.push(event);
    }

    // Should have 2 QualityCheckFailed events + 1 Escalated event
    let failed_count = events.iter()
        .filter(|e| matches!(e, WorkflowEvent::QualityCheckFailed { .. }))
        .count();
    assert_eq!(failed_count, 2);

    let escalated: Vec<_> = events.iter().filter(|e| {
        matches!(e, WorkflowEvent::Escalated { .. })
    }).collect();
    assert_eq!(escalated.len(), 1);
    if let WorkflowEvent::Escalated { reason, .. } = &escalated[0] {
        assert!(reason.contains("exhausted"));
    }
}
```

#### 10.3.11 Test 10: Stage Without Gate Behaves Like v1

```rust
#[tokio::test]
async fn test_stage_without_gate_behaves_like_v1() {
    // No quality gate, no retry budget, no review policy
    let workflow = Workflow::builder()
        .stage("a", SimpleStage)
        .build()
        .unwrap();

    let store = MemoryStateStore::<TestItem>::new();
    let item = TestItem::new("item-1");

    workflow.advance(&item, &store).await.unwrap();

    // Stage should complete normally
    let status = store.get_status(item.id(), "a").await.unwrap().unwrap();
    assert_eq!(status.state, StageState::Completed);

    // Exactly 1 attempt recorded
    let attempts = store.get_attempts(item.id(), "a").await.unwrap();
    assert_eq!(attempts.len(), 1);

    // Verdict should be Accepted (implicit, no gate)
    assert!(matches!(
        attempts[0].quality_verdict,
        Some(SerializableVerdict::Accepted)
    ));
}
```

#### 10.3.12 Additional Edge Case Tests

```rust
#[tokio::test]
async fn test_quality_gate_with_multi_stage_pipeline() {
    // Verify that quality gates on one stage do not affect other stages
    let workflow = Workflow::builder()
        .stage("a", SimpleStage)
        .stage("b", SimpleStage)
        .dependency("b", "a")
        .quality_gate("a", AlwaysAcceptGate)
        // b has no quality gate
        .build()
        .unwrap();

    let store = MemoryStateStore::<TestItem>::new();
    let item = TestItem::new("item-1");

    workflow.advance(&item, &store).await.unwrap();

    let status_a = store.get_status(item.id(), "a").await.unwrap().unwrap();
    let status_b = store.get_status(item.id(), "b").await.unwrap().unwrap();
    assert_eq!(status_a.state, StageState::Completed);
    assert_eq!(status_b.state, StageState::Completed);
}

#[tokio::test]
async fn test_quality_gate_retry_budget_default_is_one_attempt() {
    // Default retry budget: max_attempts=1, on_exhausted=Fail
    // If the gate rejects, the stage should fail immediately (no retry)
    let reject_feedback = QualityFeedback {
        summary: "rejected".to_string(),
        failed_criteria: vec![],
        guidance: None,
    };

    let workflow = Workflow::builder()
        .stage("a", SimpleStage)
        .quality_gate("a", AlwaysRejectGate {
            feedback: reject_feedback,
        })
        // No retry_budget call: uses default (max_attempts=1, Fail)
        .build()
        .unwrap();

    let store = MemoryStateStore::<TestItem>::new();
    let item = TestItem::new("item-1");

    workflow.advance(&item, &store).await.unwrap();

    let status = store.get_status(item.id(), "a").await.unwrap().unwrap();
    assert_eq!(status.state, StageState::Failed);

    // Only 1 attempt
    let attempts = store.get_attempts(item.id(), "a").await.unwrap();
    assert_eq!(attempts.len(), 1);
}

#[tokio::test]
async fn test_review_policy_on_uncertain_with_accepted_verdict() {
    // ReviewPolicy::OnUncertain should NOT block when the verdict is Accepted
    let workflow = Workflow::builder()
        .stage("a", SimpleStage)
        .quality_gate("a", AlwaysAcceptGate)
        .review_policy("a", ReviewPolicy::OnUncertain)
        .build()
        .unwrap();

    let store = MemoryStateStore::<TestItem>::new();
    let item = TestItem::new("item-1");

    workflow.advance(&item, &store).await.unwrap();

    let status = store.get_status(item.id(), "a").await.unwrap().unwrap();
    assert_eq!(status.state, StageState::Completed);
}

#[tokio::test]
async fn test_review_policy_on_uncertain_blocks_on_uncertain() {
    // ReviewPolicy::OnUncertain SHOULD block when the verdict is Uncertain
    let workflow = Workflow::builder()
        .stage("a", SimpleStage)
        .quality_gate("a", UncertainGate {
            reason: "Ambiguous output".to_string(),
        })
        .review_policy("a", ReviewPolicy::OnUncertain)
        .build()
        .unwrap();

    let store = MemoryStateStore::<TestItem>::new();
    let item = TestItem::new("item-1");

    workflow.advance(&item, &store).await.unwrap();

    // Uncertain always escalates to review, and OnUncertain agrees
    let status = store.get_status(item.id(), "a").await.unwrap().unwrap();
    assert_eq!(status.state, StageState::AwaitingReview);
}

#[tokio::test]
async fn test_review_policy_on_escalation_or_uncertain_covers_both() {
    // Scenario A: exhaustion -> AwaitingReview
    let reject_feedback = QualityFeedback {
        summary: "bad".to_string(),
        failed_criteria: vec![],
        guidance: None,
    };

    let workflow_exhaust = Workflow::builder()
        .stage("a", SimpleStage)
        .quality_gate("a", AlwaysRejectGate {
            feedback: reject_feedback,
        })
        .retry_budget("a", RetryBudget {
            max_attempts: 1,
            delay: None,
            attempt_timeout: None,
            on_exhausted: ExhaustedAction::Fail,
        })
        .review_policy("a", ReviewPolicy::OnEscalationOrUncertain)
        .build()
        .unwrap();

    let store_a = MemoryStateStore::<TestItem>::new();
    let item_a = TestItem::new("item-a");

    workflow_exhaust.advance(&item_a, &store_a).await.unwrap();

    let status_a = store_a.get_status(item_a.id(), "a")
        .await.unwrap().unwrap();
    assert_eq!(status_a.state, StageState::AwaitingReview);

    // Scenario B: uncertain -> AwaitingReview
    let workflow_uncertain = Workflow::builder()
        .stage("a", SimpleStage)
        .quality_gate("a", UncertainGate {
            reason: "unsure".to_string(),
        })
        .review_policy("a", ReviewPolicy::OnEscalationOrUncertain)
        .build()
        .unwrap();

    let store_b = MemoryStateStore::<TestItem>::new();
    let item_b = TestItem::new("item-b");

    workflow_uncertain.advance(&item_b, &store_b).await.unwrap();

    let status_b = store_b.get_status(item_b.id(), "a")
        .await.unwrap().unwrap();
    assert_eq!(status_b.state, StageState::AwaitingReview);
}
```

### Verification Commands

```bash
cargo build --all-features
cargo test --all-features
cargo test --test quality_gate_integration
cargo clippy --all-features
```

---

## Phase 10 Completion Checklist

- [ ] `cargo build` succeeds
- [ ] `cargo test` passes all tests (including all v1 and prior v2 phase tests)
- [ ] `cargo clippy -- -D warnings` clean
- [ ] `QualityCheckPassed`, `QualityCheckFailed`, `Escalated` events added to `WorkflowEvent`
- [ ] `item_id()` and `stage()` accessors updated for new event variants
- [ ] No-op quality gate stub replaced with real `QualityGate::evaluate` calls
- [ ] `QualityVerdict::Accepted` path emits `QualityCheckPassed`, applies `ReviewPolicy`
- [ ] `QualityVerdict::Rejected` path emits `QualityCheckFailed`, retries or exhausts
- [ ] `QualityVerdict::Uncertain` path emits `Escalated`, immediately escalates to review
- [ ] `ReviewPolicy::Never` allows completion without review
- [ ] `ReviewPolicy::Always` blocks for review even when quality passes
- [ ] `ReviewPolicy::OnEscalation` blocks for review only when budget exhausted
- [ ] `ReviewPolicy::OnUncertain` blocks for review only on uncertain verdict
- [ ] `ReviewPolicy::OnEscalationOrUncertain` blocks on either condition
- [ ] `handle_exhausted` respects `ReviewPolicy` override of `ExhaustedAction::Fail`
- [ ] `QualityContext` correctly populated with attempt history
- [ ] Feedback threaded from quality gate rejection to `StageContext.feedback` on retry
- [ ] Stages without quality gates behave identically to v1
- [ ] Default retry budget (max_attempts=1, Fail) works correctly with quality gates
- [ ] End-to-end integration tests all pass
- [ ] All existing tests still pass

### Public API After Phase 10

```rust
// New in Phase 10
treadle::WorkflowEvent::QualityCheckPassed   // Quality gate accepted output
treadle::WorkflowEvent::QualityCheckFailed   // Quality gate rejected output
treadle::WorkflowEvent::Escalated            // Stage escalated to human review

// Modified in Phase 10
treadle::Workflow                             // execute_stage_with_retry now calls real gates
// (internal change; public API signature unchanged)

// Unchanged from prior phases (used by Phase 10)
treadle::QualityGate                         // Phase 7
treadle::QualityVerdict                      // Phase 7
treadle::QualityFeedback                     // Phase 7
treadle::QualityContext                      // Phase 7, updated Phase 9
treadle::RetryBudget                         // Phase 8
treadle::ExhaustedAction                     // Phase 8
treadle::ReviewPolicy                        // Phase 8
treadle::AttemptRecord                       // Phase 9
treadle::SerializableVerdict                 // Phase 9
```

---

## Notes on Rust Guidelines Applied

### Anti-Patterns Avoided
- **AP-01**: No boolean arguments in public API. `ReviewPolicy` uses named enum variants rather than boolean fields. The internal `requires_review` helper uses two booleans, but it is `pub(crate)` and tested exhaustively.
- **AP-03**: All types involved in async boundaries (`QualityGate`, `StageOutput`, `QualityContext`) are `Send + Sync`.
- **AP-09**: No `unwrap()` in library code. `serde_json::to_value(fb).unwrap_or_default()` uses a safe fallback. Event sends use `let _ = event_tx.send(...)` to ignore channel errors.
- **AP-12**: No unnecessary clones. `QualityFeedback` is cloned only when threading feedback to the next attempt (necessary because the verdict consumes the feedback). Event fields are `String` values created from `to_string()`, avoiding cloning large structures.

### Core Idioms Applied
- **ID-01**: `WorkflowEvent` is `#[non_exhaustive]`, allowing new variants without breaking downstream.
- **ID-04**: Builder pattern (`StageContext::new().with_attempt().with_feedback()`) used for context construction.
- **ID-11**: `Serialize`/`Deserialize` on `AttemptRecord` for persistence.
- **ID-35**: Builder methods consuming and returning `Self`.

### Error Handling
- **EH-07**: Quality gate evaluation errors (`gate.evaluate()` returning `Err`) propagate up via `?`. This is intentional: a gate that fails to evaluate (as opposed to returning `Rejected`) is a system error, not a quality judgement.
- Timeout is treated as a retryable failure with synthetic feedback, not a hard error.
- `ExhaustedAction::Fail` can be overridden by `ReviewPolicy::OnEscalation`, preventing data loss when operators want both fail semantics and escalation review.

### Concurrency
- Quality gate evaluation happens within the retry loop, which processes one item at a time through a stage. Different items may be evaluated concurrently (as documented on the `QualityGate` trait).
- `tokio::time::timeout` wraps stage execution only, not quality gate evaluation. If a quality gate hangs, that is a separate concern (addressed by the `attempt_timeout` wrapping the entire attempt in a future phase if needed).

### Testing Strategy
- Test gates (`AlwaysAcceptGate`, `AlwaysRejectGate`, `AcceptOnAttemptGate`, `UncertainGate`) provide deterministic, controllable behaviour.
- Tests cover all verdict types (Accepted, Rejected, Uncertain), all `ReviewPolicy` variants, budget exhaustion with both `ExhaustedAction` variants, feedback threading, event emission, and v1 backward compatibility.
- `Arc<Mutex<Vec<_>>>` captures state across async boundaries in tests without introducing flakiness.
- Integration tests are in a separate file (`tests/quality_gate_integration.rs`) to exercise the public API.