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
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
//! Pure, deterministic fold of the event log into [`MissionState`].
//!
//! `state.json` is only a cache of `fold(events)`; the log is the source of
//! truth. `fold` == `fold(first)` + repeated [`apply`] (property-tested), so
//! the engine can maintain state incrementally while any reader can rebuild
//! it from scratch and get byte-identical JSON.
use crate::error::{EngineError, Result};
use crate::events::{Event, EventKind};
use crate::types::*;
use std::collections::{BTreeMap, BTreeSet};
use std::path::Path;
/// Reserved `run_id` for `validation.finding` events produced by the engine
/// itself (final contract gate command failures) rather than a validator run.
pub const ENGINE_RUN_ID: &str = "engine";
/// Fold a contiguous event slice into a state. The first event MUST be
/// `mission.created`.
pub fn fold(events: &[Event]) -> Result<MissionState> {
let first = events
.first()
.ok_or_else(|| EngineError::InvalidState("cannot fold an empty event log".to_string()))?;
let mut state = initial_state(first)?;
for event in &events[1..] {
apply(&mut state, event)?;
}
Ok(state)
}
/// Apply one event on top of an existing state. `event.seq` must be exactly
/// `state.last_seq + 1` (fold passes contiguous events; anything else is a
/// caller bug or log corruption).
pub fn apply(state: &mut MissionState, event: &Event) -> Result<()> {
if event.seq != state.last_seq + 1 {
return Err(EngineError::InvalidState(format!(
"non-contiguous apply: state at seq {}, event seq {}",
state.last_seq, event.seq
)));
}
match &event.kind {
EventKind::MissionCreated { .. } => {
return Err(EngineError::InvalidState(format!(
"mission.created at seq {} is only valid as the first event",
event.seq
)));
}
EventKind::PlanApproved { plan, base_sha } => {
// Status guard, the sibling of `expect_pending_grant` below
// (audit 2026-09-01 H6). `approve_plan` only ever emits this from
// `Planning`, so a `plan.approved` folded on top of a running,
// blocked, or completed mission did not come from the approval
// path: it is a late forgery appended to the log, and applying it
// would replace the milestone set, the contract, the touch set,
// and the command grants wholesale. Ignored rather than a fold
// error, so one bad line cannot make an existing mission
// permanently unreadable.
if state.mission.status != MissionStatus::Planning {
tracing::warn!(
seq = event.seq,
status = ?state.mission.status,
"ignoring plan.approved outside Planning status"
);
state.last_seq = event.seq;
return Ok(());
}
crate::contract_controls::validate(&plan.validation_contract)?;
state.mission.base_sha = base_sha.clone();
state.mission.goal = plan.goal.clone();
state.mission.validation_contract = plan.validation_contract.clone();
state.mission.milestones = plan
.milestones
.iter()
.enumerate()
.map(|(mi, pm)| Milestone {
id: format!("ms-{}", mi + 1),
title: pm.title.clone(),
features: pm
.features
.iter()
.enumerate()
.map(|(fi, pf)| Feature {
id: format!("f-{}-{}", mi + 1, fi + 1),
title: pf.title.clone(),
spec: pf.spec.clone(),
validation_criteria: pf.validation_criteria.clone(),
origin: FeatureOrigin::Plan,
status: FeatureStatus::Pending,
worker_runs: Vec::new(),
commits: Vec::new(),
respawns: 0,
})
.collect(),
status: MilestoneStatus::Pending,
fix_cycles: 0,
start_sha: None,
validator_guidance: None,
})
.collect();
state.mission.command_grants = plan.command_grants.clone();
state.mission.touch_set = plan.touch_set.clone();
// The Flight Rules approval pin (KRZ-342 D-E) folds with the plan
// it was approved with — the mission's standards authority from
// here on.
state.mission.standards_manifest = plan.standards_manifest.as_deref().cloned();
state.mission.reviewer_independence = plan.reviewer_independence;
state.mission.status = MissionStatus::Approved;
state.latest_plan_revision = 0;
state.pending_revision = None;
}
EventKind::PlanRevisionProposed {
revision,
plan,
instructions,
} => {
if *revision == 0 {
return Err(EngineError::InvalidState(
"plan.revision.proposed revision must be >= 1".to_string(),
));
}
state.latest_plan_revision = state.latest_plan_revision.max(*revision);
state.pending_revision = Some(PendingRevision {
revision: *revision,
plan: plan.clone(),
instructions: instructions.clone(),
});
}
EventKind::PlanRevised { revision, plan } => {
if let Some(pending) = &state.pending_revision {
if pending.revision != *revision {
return Err(EngineError::InvalidState(format!(
"plan.revised revision {revision} does not match pending revision {}",
pending.revision
)));
}
}
apply_revised_plan(state, plan, *revision)?;
state.latest_plan_revision = state.latest_plan_revision.max(*revision);
state.pending_revision = None;
}
EventKind::PlanRevisionRejected { revision, .. } => {
if let Some(pending) = &state.pending_revision {
if pending.revision != *revision {
return Err(EngineError::InvalidState(format!(
"plan.revision.rejected revision {revision} does not match pending revision {}",
pending.revision
)));
}
}
state.latest_plan_revision = state.latest_plan_revision.max(*revision);
state.pending_revision = None;
}
EventKind::GrantRequested {
milestone_id,
kind,
command,
} => {
milestone_mut(state, milestone_id)?; // existence check
if command.trim().is_empty() {
return Err(EngineError::InvalidState(
"grant.requested command must not be empty".to_string(),
));
}
state.pending_grant_request = Some(PendingGrantRequest {
milestone_id: milestone_id.clone(),
kind: *kind,
command: command.clone(),
});
}
EventKind::GrantApproved { kind, command } => {
// Cross-check against the parked request (mirrors PlanRevised): a
// forged or replayed grant.approved with no matching pending
// request — or one naming a different kind/target than was
// requested — must never silently widen an allow-list.
expect_pending_grant(state, *kind, command, "grant.approved")?;
// Extend-only, deduped: the approved target joins the list `kind`
// selects so the retried run clears the boundary.
let list = match kind {
GrantKind::Command => &mut state.mission.command_grants,
GrantKind::TouchPath => &mut state.mission.touch_set,
GrantKind::WorkerDeny => &mut state.mission.deny_exceptions,
GrantKind::Egress => &mut state.mission.egress_grants,
};
if !list.iter().any(|c| c == command) {
list.push(command.clone());
}
state.pending_grant_request = None;
}
EventKind::GrantDenied { kind, command, .. } => {
expect_pending_grant(state, *kind, command, "grant.denied")?;
state.pending_grant_request = None;
}
EventKind::MilestoneStarted {
milestone_id,
start_sha,
} => {
if state.executor_tier() == ExecutorTier::Local {
state.local_executor_milestones += 1;
}
let ms = milestone_mut(state, milestone_id)?;
ms.status = MilestoneStatus::Active;
ms.start_sha = Some(start_sha.clone());
if state.mission.status == MissionStatus::Approved {
state.mission.status = MissionStatus::Running;
}
}
EventKind::FeatureStarted { feature_id } => {
feature_mut(state, feature_id)?.status = FeatureStatus::Active;
}
EventKind::FeatureProgress {
feature_id,
base_sha,
commits,
} => {
if feature_mut(state, feature_id)?.status != FeatureStatus::Active {
return Err(EngineError::InvalidState(format!(
"feature.progress for inactive feature '{feature_id}'"
)));
}
if state
.feature_base_shas
.get(feature_id)
.is_some_and(|existing| existing != base_sha)
{
return Err(EngineError::InvalidState(format!(
"feature.progress changed the baseline for '{feature_id}'"
)));
}
state
.feature_base_shas
.insert(feature_id.clone(), base_sha.clone());
record_feature_commits(feature_mut(state, feature_id)?, commits);
}
EventKind::WorkerSpawned {
run_id,
role,
feature_id,
milestone_id,
candidate,
executor_route: _,
sdk_session_id,
model,
backend,
quant,
weight_hash,
prompt_hash,
transcript_path,
} => {
if state.runs.contains_key(run_id) {
return Err(EngineError::InvalidState(format!(
"duplicate worker.spawned for run '{run_id}'"
)));
}
if let Some(mid) = milestone_id {
milestone_mut(state, mid)?; // existence check
}
if let Some(fid) = feature_id {
let feature = feature_mut(state, fid)?;
feature.worker_runs.push(run_id.clone());
// A 2nd+ run on the same feature is a respawn — EXCEPT a
// dispatch-pool candidate (KRZ-303): the N sibling streams
// are ONE logical dispatch of the unit, not N-1 retries, and
// the pool path has no judgement-driven respawn loop at all,
// so counting them would silently deplete `max_respawns`.
if feature.worker_runs.len() > 1 && candidate.is_none() {
feature.respawns += 1;
}
}
state.runs.insert(
run_id.clone(),
WorkerRun {
backend: *backend,
id: run_id.clone(),
role: *role,
feature_id: feature_id.clone(),
milestone_id: milestone_id.clone(),
candidate: candidate.clone(),
sdk_session_id: sdk_session_id.clone(),
model: model.clone(),
quant: quant.clone(),
weight_hash: weight_hash.clone(),
started_at: event.ts,
ended_at: None,
tokens: TokenUsage::default(),
cost_usd: None,
transcript_path: transcript_path.clone(),
result: None,
report: None,
prompt_hash: prompt_hash.clone(),
},
);
if state.mission.status == MissionStatus::Approved {
state.mission.status = MissionStatus::Running;
}
}
EventKind::WorkerMessage { run_id, .. } => {
run_mut(state, run_id)?; // stream delta: existence check only
}
EventKind::WorkerEgressDenied { run_id, .. } => {
// Audit-only runtime evidence. The durable event deliberately
// adds no mutable state shape; consumers select the relevant run
// ids directly from the validated log. Still validate the run
// reference so a hand-edited event cannot cite a nonexistent
// session.
run_mut(state, run_id)?;
}
EventKind::WorkerCompleted {
run_id,
result,
tokens,
cost_usd,
report,
} => {
let run = run_mut(state, run_id)?;
run.result = Some(*result);
run.tokens = tokens.clone();
run.cost_usd = *cost_usd;
run.report = report.clone();
run.ended_at = Some(event.ts);
state.totals.add(tokens);
state.total_cost_usd += cost_usd.unwrap_or(0.0);
}
EventKind::FeatureCompleted {
feature_id,
commits,
} => {
let cumulative = state.feature_base_shas.contains_key(feature_id);
let feature = feature_mut(state, feature_id)?;
feature.status = FeatureStatus::Complete;
if cumulative {
record_feature_commits(feature, commits);
} else {
feature.commits.extend(commits.iter().cloned());
}
}
EventKind::FeatureFailed {
feature_id,
commits,
..
} => {
let cumulative = state.feature_base_shas.contains_key(feature_id);
let feature = feature_mut(state, feature_id)?;
feature.status = FeatureStatus::Failed;
// Record any commits the failure landed on the mission branch:
// the fix-feature supersession guard reads `commits.is_empty()`
// to tell a failed-COMMITLESS feature (re-proposable — the
// m-eee81f auth-death wedge) from failed-with-real-work (started;
// a duplicate fixfeature.created must reject).
if cumulative {
record_feature_commits(feature, commits);
} else {
feature.commits.extend(commits.iter().cloned());
}
}
EventKind::FeatureSkipped { feature_id, .. } => {
feature_mut(state, feature_id)?.status = FeatureStatus::Skipped;
}
EventKind::MilestoneValidating { milestone_id } => {
milestone_mut(state, milestone_id)?.status = MilestoneStatus::Validating;
}
EventKind::ValidationFinding {
milestone_id,
run_id,
..
} => {
// No structural change; validate references as a corruption guard.
// run_id "engine" is reserved for findings the engine itself
// produces (final contract gate command failures) — no session
// exists behind them, so the run lookup is skipped.
milestone_mut(state, milestone_id)?;
if run_id != ENGINE_RUN_ID {
run_mut(state, run_id)?;
}
}
EventKind::ValidatorTamper {
milestone_id,
run_id,
..
} => {
// Audit record of the failed immutability assertion; the
// accompanying milestone.blocked drives status. Validate
// references as a corruption guard (mirrors validation.finding).
milestone_mut(state, milestone_id)?;
if run_id != ENGINE_RUN_ID {
run_mut(state, run_id)?;
}
}
EventKind::ValidationSnapshot { milestone_id, .. } => {
// Audit record of the throwaway checkout a validator session
// ran in; no structural state change, and no run id exists at
// emit time (the session starts after the snapshot). Validate
// the milestone reference as a corruption guard only.
milestone_mut(state, milestone_id)?;
}
EventKind::ValidationConfirm {
milestone_id,
local_run_id,
confirm_run_id,
..
} => {
// Audit-only record (KRZ-206b, the gate.result additive
// template): the local-vs-frontier comparison drives no state
// transition — a disagreement's finding already flows through
// validation.finding, and the miss rate reads this event back
// off the log, so state shape does not grow. Validate all
// references as a corruption guard (mirrors validation.finding):
// both run ids name real sessions (the local primary and the
// frontier confirmation), so a hand-edited confirm cannot cite
// a run the log never recorded.
milestone_mut(state, milestone_id)?;
run_mut(state, local_run_id)?;
run_mut(state, confirm_run_id)?;
}
EventKind::ValidationPtyTranscript { milestone_id, .. } => {
// Audit-only record (ticket pty-functional-validation): the
// verdict reaches the round through the functional validator's
// evidence block, not through this event, so it drives no state
// transition (mirrors validation.snapshot). No run id exists at
// emit time — the evidence pass is engine-run — so only the
// milestone reference is validated as a corruption guard.
milestone_mut(state, milestone_id)?;
}
EventKind::GateResult { .. } => {
// Audit-only record (KRZ-312): one gate evaluation — id, ladder
// position, verdict, artefact handle. Gate results drive no
// state transition (the advisory posture of contract gates is
// unchanged: verdicts inform, they never block), and the ladder
// a replay reconstructs is read from the events themselves, so
// state shape intentionally does not grow. And unlike
// validation.finding there is no milestone/run reference on the
// payload to validate as a corruption guard — the arm is a pure
// no-op, exactly like secret.redacted below.
}
EventKind::HookGateFired { run_id, .. } => {
// Record-only (KRZ-302, the gate.result additive template): the
// in-process hook verdict already happened inside the session,
// and the engine-side sweep remains the authoritative layer —
// so this drives no state transition and state shape does not
// grow. Validate the run reference as a corruption guard
// (mirrors validation.finding); the run id is engine-stamped at
// fold time, so this cannot be aimed at a run the log never
// recorded.
run_mut(state, run_id)?;
}
EventKind::DivergenceNoted {
unit, candidates, ..
} => {
// Audit record of the candidate comparison (KRZ-304); the
// accompanying milestone.blocked drives the park, and the
// `diverged` verdict is deliberately NEVER folded into any
// state a decision could key on — agreement between models is a
// signal to log, never a criterion to trust. Validate
// references as a corruption guard (mirrors validation.finding):
// the unit names a feature, every candidate a recorded run.
feature_mut(state, unit)?;
for candidate in candidates {
run_mut(state, &candidate.run_id)?;
}
}
EventKind::DivergenceResolved { unit, .. } => {
// The judgement record (KRZ-304). The unit joins the folded
// resolution set — the engine's restart-safe memory for "this
// unit was already judged" (at most one resolution per unit;
// the set insert keeps a duplicated hand-written event benign).
// Reference validation as a corruption guard, as above.
feature_mut(state, unit)?;
state.resolved_divergence_units.insert(unit.clone());
}
EventKind::FixFeatureCreated {
milestone_id,
feature,
} => {
let ms = milestone_mut(state, milestone_id)?;
if let Some(existing) = ms.features.iter().find(|f| f.id == feature.id) {
// A duplicate with an IDENTICAL proposal is an idempotent
// replay — a retried emission after a crash between emit
// and fold (mission m-83d1ed). Event-sourced recovery must
// no-op it, not brick — and it must NOT skip the last_seq
// advance at the tail, or the next event fails contiguity
// (the m-83d1ed wedge's second form).
if existing.title == feature.title
&& existing.spec == feature.spec
&& existing.validation_criteria == feature.validation_criteria
{
// fall through to the tail: seq advances, state unchanged
} else {
// A duplicate with a DIFFERENT payload is an implicit
// SUPERSESSION when the prior feature never produced work:
// an unstarted (Pending) or failed-and-commitless feature
// can be re-proposed by a re-plan — this is the normal
// shape after new findings (m-83d1ed re-proposed the same
// id twice). Failed-with-runs-but-no-commits is the same
// class: runs that never committed produced no work (the
// m-eee81f wedge — three infra-failed runs made
// `worker_runs` non-empty and bricked every re-proposal);
// their records stay in the log. A feature whose run is
// in flight is Active, so runs alone are not the "started"
// signal. The successor REPLACES the prior payload in
// place and restarts as Pending; the original payload is
// not lost — it lives in this same event log (the first
// fixfeature.created). Once a feature has started,
// committed, or completed, a revision is genuinely
// shadowing and stays loudly invalid.
let idx = ms
.features
.iter()
.position(|f| f.id == feature.id)
.expect("found above");
let existing = &ms.features[idx];
let prior_started = matches!(
existing.status,
FeatureStatus::Active | FeatureStatus::Complete | FeatureStatus::Skipped
) || !existing.commits.is_empty();
if prior_started {
return Err(EngineError::InvalidState(format!(
"duplicate fixfeature.created for feature '{}' with a different payload",
feature.id
)));
}
tracing::info!(
feature_id = %feature.id,
"fixfeature re-proposed before any work: folding as implicit supersession"
);
if ms.status == MilestoneStatus::Validating {
ms.fix_cycles += 1;
ms.status = MilestoneStatus::Active;
}
let mut successor = feature.clone();
successor.status = FeatureStatus::Pending;
ms.features[idx] = successor;
state.feature_base_shas.remove(&feature.id);
}
} else {
// One fix-cycle increment per validation round: the first
// fixfeature after milestone.validating flips the milestone back
// to Active; later fixfeatures in the same round arrive while
// Active and do not increment.
if ms.status == MilestoneStatus::Validating {
ms.fix_cycles += 1;
ms.status = MilestoneStatus::Active;
}
ms.features.push(feature.clone());
}
}
EventKind::TierEscalated { milestone_id, .. } => {
state.config.worker.backend = None;
state.config.worker.base_url = None;
state.config.worker.context_budget = None;
state.config.worker.temperature = None;
state.escalated_milestones += 1;
let ms = milestone_mut(state, milestone_id)?;
ms.status = MilestoneStatus::Active;
ms.fix_cycles = 0;
}
EventKind::WorkerEscalated { run_id, .. } => {
// Record-only (KRZ-331, the gate.result additive template): the
// worker's escalation request is provenance — the judgement turn
// (the frontier advisor) acts on the report, and nothing a
// decision could key on changes here: the validator route, the
// executor tier, the respawn budget, and every milestone status
// are deliberately untouched, so a worker escalation can never
// bypass the floor's validator requirements (contrast
// tier.escalated above, the orchestrator-initiated tier flip,
// which DOES rewrite worker config). Validate the run reference
// as a corruption guard (mirrors hook.gate.fired): the run id is
// engine-stamped at emit time, so this cannot be aimed at a run
// the log never recorded.
run_mut(state, run_id)?;
}
EventKind::QuestionOpened {
question_id,
role,
text,
options,
run_id,
feature_id,
milestone_id,
} => {
// The pending-decision projection's open edge (ticket
// structured-human-question-events). NOT record-only: the open
// question IS state a surface renders and an answer cross-checks
// against, so it folds onto `pending_questions` — but it gates
// NOTHING in the run loop (contrast grant.requested's park).
// Validate references as a corruption guard (mirrors
// validation.finding), then dedupe like fixfeature.created: a
// duplicated open with an IDENTICAL payload is an idempotent
// replay (no double-push, no id-counter bump); the same id with
// a DIFFERENT payload is shadowing and stays loudly invalid.
if question_id.trim().is_empty() {
return Err(EngineError::InvalidState(
"question.opened question id must not be empty".to_string(),
));
}
if text.trim().is_empty() {
return Err(EngineError::InvalidState(format!(
"question.opened {question_id} text must not be empty"
)));
}
if let Some(run_id) = run_id {
run_mut(state, run_id)?;
}
if let Some(feature_id) = feature_id {
feature_mut(state, feature_id)?;
}
if let Some(milestone_id) = milestone_id {
milestone_mut(state, milestone_id)?;
}
if let Some(existing) = state
.pending_questions
.iter()
.find(|q| q.question_id == *question_id)
{
let identical = existing.role == *role
&& existing.text == *text
&& existing.options == *options
&& existing.run_id == *run_id
&& existing.feature_id == *feature_id
&& existing.milestone_id == *milestone_id;
if !identical {
return Err(EngineError::InvalidState(format!(
"duplicate question.opened for '{question_id}' with a different payload"
)));
}
// fall through to the tail: seq advances, state unchanged
} else {
state.question_count += 1;
state.pending_questions.push(PendingQuestion {
question_id: question_id.clone(),
role: *role,
text: text.clone(),
options: options.clone(),
run_id: run_id.clone(),
feature_id: feature_id.clone(),
milestone_id: milestone_id.clone(),
});
}
}
EventKind::QuestionAnswered {
question_id,
answer,
..
} => {
// The projection's answer edge: cross-check against the parked
// question (mirrors expect_pending_grant — a stale or forged
// answer for a question that is not open fails the fold), remove
// it, then route the answer onto `pending_user_messages` — the
// EXISTING consult path (D-X: answers ride the msg machinery,
// never a new delivery mechanism), so the orchestrator's next
// user-message consult consumes the answer and a restart replays
// it from the log alone.
let question = take_pending_question(state, question_id, "question.answered")?;
state.pending_user_messages.push(format!(
"answer to question {} (\"{}\"): {}",
question.question_id, question.text, answer
));
}
EventKind::QuestionCleared { question_id, .. } => {
// The projection's clear edge: same parked-question cross-check
// as the answer (a clear for a question that is not open is
// corruption, never a silent no-op).
take_pending_question(state, question_id, "question.cleared")?;
}
EventKind::MilestoneBlocked { milestone_id, .. } => {
milestone_mut(state, milestone_id)?.status = MilestoneStatus::Blocked;
state.mission.status = MissionStatus::Blocked;
}
EventKind::MilestoneUnblocked {
milestone_id,
validator_guidance,
..
} => {
let ms = milestone_mut(state, milestone_id)?;
ms.status = MilestoneStatus::Active;
// Latest unblock wins (including None — a bare unblock clears
// guidance left by an earlier one).
ms.validator_guidance = validator_guidance.clone();
state.mission.status = MissionStatus::Running;
}
EventKind::MilestoneCompleted { milestone_id, .. } => {
let ms = milestone_mut(state, milestone_id)?;
ms.status = MilestoneStatus::Complete;
// Guidance served its purpose; never leak it into a later
// milestone's (or a re-run's) validators.
ms.validator_guidance = None;
}
EventKind::MissionValidating {} => {
state.mission.status = MissionStatus::Validating;
}
EventKind::MissionPaused {} => {
state.mission.status = MissionStatus::Paused;
}
EventKind::MissionResumed {} => {
state.mission.status = MissionStatus::Running;
}
EventKind::UserMessage { text, .. } => {
state.pending_user_messages.push(text.clone());
}
EventKind::OrchestratorDecision { summary, .. } => {
state.recent_decisions.push(summary.clone());
while state.recent_decisions.len() > MAX_RECENT_DECISIONS {
state.recent_decisions.remove(0);
}
// A decision marks the queued user messages as consumed.
state.pending_user_messages.clear();
}
EventKind::SecretRedacted { .. } => {
// Audit-only: the write boundary already redacted the event that
// preceded this marker. State shape intentionally does not grow.
}
EventKind::ConfigChanged { patch } => {
let mut value = serde_json::to_value(&state.config)?;
deep_merge(&mut value, patch);
state.config = serde_json::from_value(value).map_err(|e| {
EngineError::Config(format!("config.changed patch produced invalid config: {e}"))
})?;
}
EventKind::MissionCompleted {} => {
state.mission.status = MissionStatus::Complete;
}
EventKind::MissionFailed { .. } => {
state.mission.status = MissionStatus::Failed;
}
EventKind::MissionAbandoned { .. } => {
state.mission.status = MissionStatus::Abandoned;
}
EventKind::WorkspaceProvisioned { provider, .. } => {
// The last provisioned provider kind is the durable record (D-E);
// a resume re-provisions and supersedes it with the same value.
state.workspace_provider = Some(provider.clone());
}
EventKind::WorkspaceReadinessReport { .. } => {
// Audit-only artifact (D-E): the readiness outcome lives on the
// event trail; state shape intentionally does not grow from it.
}
EventKind::WorkspaceTeardown { state: outcome, .. } => {
// The teardown outcome (ticket workspace-idle-hibernate) folds
// into the last-known workspace lifecycle — latest transition
// wins (append-only order), with the event's own ts as the
// workspace-hours anchor for cost tooling. Teardown events
// without an outcome (old keep-only logs) leave it untouched.
if let Some(outcome) = outcome {
state.workspace_lifecycle = Some(WorkspaceLifecycle {
state: outcome.clone(),
ts: event.ts,
});
}
}
EventKind::WorkspaceProviderPinned {
provider,
template,
version,
} => {
// The approval-time consent pin (D-B). Emitted once per
// approve_plan; a retried approval after a failed attempt re-pins
// (last pin wins).
state.workspace_pin = Some(WorkspacePin {
provider: provider.clone(),
template: template.clone(),
version: version.clone(),
});
}
EventKind::StandardsResolved { .. }
| EventKind::StandardsDrifted { .. }
| EventKind::StandardsWaiverApproved { .. }
| EventKind::StandardsAttestationApproved { .. } => {
// Audit-only (KRZ-342 D-H; KRZ-344 D-I): the pin itself folds
// with plan.approved; these events are the queryable provenance,
// refusal, and waiver evidence. The coverage fold joins waivers
// straight from the log — state shape intentionally does not
// grow.
}
}
state.last_seq = event.seq;
Ok(())
}
/// Newest-last cap on `MissionState::recent_decisions`.
const MAX_RECENT_DECISIONS: usize = 10;
fn record_feature_commits(feature: &mut Feature, commits: &[String]) {
for commit in commits {
if !feature
.commits
.iter()
.any(|existing| existing.split_whitespace().next() == commit.split_whitespace().next())
{
feature.commits.push(commit.clone());
}
}
}
fn initial_state(event: &Event) -> Result<MissionState> {
let EventKind::MissionCreated {
goal,
base_branch,
mission_branch,
config,
} = &event.kind
else {
return Err(EngineError::InvalidState(format!(
"first event must be mission.created, found '{}'",
event.kind.type_name()
)));
};
Ok(MissionState {
feature_base_shas: BTreeMap::new(),
mission: Mission {
id: event.mission_id.clone(),
goal: goal.clone(),
validation_contract: Vec::new(),
milestones: Vec::new(),
status: MissionStatus::Planning,
created_at: event.ts,
base_branch: base_branch.clone(),
base_sha: None,
mission_branch: mission_branch.clone(),
command_grants: Vec::new(),
touch_set: Vec::new(),
deny_exceptions: Vec::new(),
egress_grants: Vec::new(),
standards_manifest: None,
reviewer_independence: None,
// The seed-time route record (ticket routing-rules-config): the
// folded task class exists only on THIS event's goal, so the
// decision is derived here, once — deterministically equal to
// what create applied (routing::seed_executor_route).
executor_route: crate::routing::seed_executor_route(config, goal),
},
runs: BTreeMap::new(),
totals: TokenUsage::default(),
total_cost_usd: 0.0,
pending_user_messages: Vec::new(),
recent_decisions: Vec::new(),
config: config.clone(),
latest_plan_revision: 0,
pending_revision: None,
pending_grant_request: None,
pending_questions: Vec::new(),
question_count: 0,
last_seq: event.seq,
escalated_milestones: 0,
local_executor_milestones: 0,
workspace_provider: None,
workspace_pin: None,
workspace_lifecycle: None,
resolved_divergence_units: BTreeSet::new(),
})
}
/// Assert that `kind`+`command` match the parked `pending_grant_request`. Both
/// `grant.approved` and `grant.denied` gate on this, so a forged or replayed
/// decision event can neither widen an allow-list (approve) nor clear a request
/// the operator never saw (deny) — and can't apply to the WRONG list by
/// swapping the kind. Mirrors the `pending.revision` cross-check that
/// `PlanRevised`/`PlanRevisionRejected` perform.
fn expect_pending_grant(
state: &MissionState,
kind: GrantKind,
command: &str,
event: &str,
) -> Result<()> {
match &state.pending_grant_request {
Some(pending) if pending.kind == kind && pending.command == command => Ok(()),
Some(pending) => Err(EngineError::InvalidState(format!(
"{event} {kind:?} {command:?} does not match pending grant {:?} {:?}",
pending.kind, pending.command
))),
None => Err(EngineError::InvalidState(format!(
"{event} with no pending grant request"
))),
}
}
/// Remove and return the parked question `question_id` names, or fail the
/// fold. Both `question.answered` and `question.cleared` gate on this
/// (mirrors [`expect_pending_grant`]): a stale, replayed, or forged
/// resolution for a question that is not open — never asked, already
/// answered, already cleared — is corruption, not a silent no-op.
fn take_pending_question(
state: &mut MissionState,
question_id: &str,
event: &str,
) -> Result<PendingQuestion> {
let Some(index) = state
.pending_questions
.iter()
.position(|q| q.question_id == question_id)
else {
return Err(EngineError::InvalidState(format!(
"{event} for question '{question_id}' that is not open"
)));
};
Ok(state.pending_questions.remove(index))
}
fn apply_revised_plan(state: &mut MissionState, plan: &Plan, revision: u32) -> Result<()> {
crate::contract_controls::validate(&plan.validation_contract)?;
if plan.reviewer_independence != state.mission.reviewer_independence {
return Err(EngineError::Config(
"plan.revised cannot replace the approved reviewerIndependence policy".into(),
));
}
ensure_contract_extends(
&state.mission.validation_contract,
&plan.validation_contract,
)?;
ensure_strings_extend(
"commandGrants",
&state.mission.command_grants,
&plan.command_grants,
)?;
ensure_strings_extend("touchSet", &state.mission.touch_set, &plan.touch_set)?;
let completed_prefix = state
.mission
.milestones
.iter()
.position(|m| m.status != MilestoneStatus::Complete)
.unwrap_or(state.mission.milestones.len());
if plan.milestones.len() < completed_prefix {
return Err(EngineError::InvalidState(
"plan.revised drops completed milestones".to_string(),
));
}
let mut revised_milestones = Vec::new();
for (idx, existing) in state
.mission
.milestones
.iter()
.enumerate()
.take(completed_prefix)
{
let Some(plan_milestone) = plan.milestones.get(idx) else {
return Err(EngineError::InvalidState(format!(
"plan.revised drops completed milestone '{}'",
existing.title
)));
};
if !completed_milestone_matches(existing, plan_milestone) {
return Err(EngineError::InvalidState(format!(
"plan.revised alters completed milestone '{}'",
existing.title
)));
}
revised_milestones.push(existing.clone());
}
for (idx, plan_milestone) in plan.milestones.iter().enumerate().skip(completed_prefix) {
if let Some(existing) = state.mission.milestones.get(idx) {
revised_milestones.push(merge_revised_milestone(existing, plan_milestone, revision));
} else {
revised_milestones.push(new_plan_milestone(idx, plan_milestone));
}
}
state.mission.goal = plan.goal.clone();
state.mission.validation_contract = plan.validation_contract.clone();
state.mission.command_grants = plan.command_grants.clone();
state.mission.touch_set = plan.touch_set.clone();
// The Flight Rules pin (KRZ-342 D-E) is NEVER re-read from a revision:
// the planner never authors policy, and no revision flow re-validates a
// carried manifest against the trusted source — folding one would let a
// re-plan substitute weakened policy into the consent artifact. The
// approval-time pin stands for the mission's life; an envelope escape is
// caught by the final-validation check (which re-resolves the pinned
// base snapshot against actual paths) and by the merge drift check.
state.mission.milestones = revised_milestones;
Ok(())
}
/// Validate that a `PlanRevised { revision, plan }` event would fold cleanly
/// onto `state`, WITHOUT mutating it. The orchestrator calls this before it
/// durably appends the event — `emit` appends before it folds — so a revision
/// the reducer would reject is refused up front instead of poisoning the
/// append-only log. A failed fold on replay would otherwise error on every
/// subsequent load and permanently brick the mission.
pub fn dry_run_revised_plan(state: &MissionState, plan: &Plan, revision: u32) -> Result<()> {
apply_revised_plan(&mut state.clone(), plan, revision)
}
fn ensure_contract_extends(existing: &[Assertion], revised: &[Assertion]) -> Result<()> {
for old in existing {
let Some(new) = revised.iter().find(|a| a.id == old.id) else {
return Err(EngineError::InvalidState(format!(
"plan.revised removes validation assertion '{}'",
old.id
)));
};
if old.statement != new.statement
|| old.check != new.check
|| old.command != new.command
|| old.negative_control != new.negative_control
{
return Err(EngineError::InvalidState(format!(
"plan.revised weakens or changes validation assertion '{}'",
old.id
)));
}
}
Ok(())
}
fn ensure_strings_extend(label: &str, existing: &[String], revised: &[String]) -> Result<()> {
for old in existing {
if !revised.iter().any(|new| new == old) {
return Err(EngineError::InvalidState(format!(
"plan.revised removes {label} entry '{old}'"
)));
}
}
Ok(())
}
fn completed_milestone_matches(existing: &Milestone, revised: &PlanMilestone) -> bool {
existing.title.trim() == revised.title.trim()
&& completed_features_match(&existing.features, &revised.features)
}
/// Whether a completed milestone's features are reproduced UNCHANGED in a
/// revised plan: same count, and same title/spec/validation-criteria in order,
/// compared TRIMMED. This is the single source of truth for the "completed
/// work is frozen" rule — the orchestrator's pre-emit gate
/// (`completed_features_unchanged`) delegates here so the gate and the reducer
/// can never diverge. The leniency is deliberate: a regenerated plan will not
/// echo incidental whitespace back byte-for-byte, and whitespace is not a
/// content change. An exact compare here would let the gate accept a revision
/// the reducer then rejects, and because `emit` appends before it folds, that
/// leaves an unfoldable event in the append-only log and bricks the mission.
pub(crate) fn completed_features_match(existing: &[Feature], revised: &[PlanFeature]) -> bool {
existing.len() == revised.len()
&& existing.iter().zip(revised).all(|(a, b)| {
a.title.trim() == b.title.trim()
&& a.spec.trim() == b.spec.trim()
&& a.validation_criteria.len() == b.validation_criteria.len()
&& a.validation_criteria
.iter()
.zip(&b.validation_criteria)
.all(|(x, y)| x.trim() == y.trim())
})
}
fn merge_revised_milestone(
existing: &Milestone,
revised: &PlanMilestone,
revision: u32,
) -> Milestone {
let mut features = Vec::new();
let mut new_count = 0usize;
for feature in &existing.features {
let matching = revised
.features
.iter()
.find(|candidate| norm_title(&candidate.title) == norm_title(&feature.title));
match (feature.status, matching) {
(FeatureStatus::Pending, Some(plan_feature)) => {
let mut updated = feature.clone();
updated.title = plan_feature.title.clone();
updated.spec = plan_feature.spec.clone();
updated.validation_criteria = plan_feature.validation_criteria.clone();
features.push(updated);
}
(FeatureStatus::Pending, None) => {
let mut skipped = feature.clone();
skipped.status = FeatureStatus::Skipped;
features.push(skipped);
}
_ => features.push(feature.clone()),
}
}
for plan_feature in &revised.features {
let already_present = existing
.features
.iter()
.any(|feature| norm_title(&feature.title) == norm_title(&plan_feature.title));
if !already_present {
new_count += 1;
features.push(Feature {
id: format!("{}-rev-{revision}-{new_count}", existing.id),
title: plan_feature.title.clone(),
spec: plan_feature.spec.clone(),
validation_criteria: plan_feature.validation_criteria.clone(),
origin: FeatureOrigin::Plan,
status: FeatureStatus::Pending,
worker_runs: Vec::new(),
commits: Vec::new(),
respawns: 0,
});
}
}
Milestone {
id: existing.id.clone(),
title: revised.title.clone(),
features,
status: existing.status,
fix_cycles: existing.fix_cycles,
start_sha: existing.start_sha.clone(),
// A revision rebuilds the milestone but does not unblock it — folded
// operator guidance survives, exactly like fix_cycles and start_sha.
validator_guidance: existing.validator_guidance.clone(),
}
}
fn new_plan_milestone(idx: usize, plan_milestone: &PlanMilestone) -> Milestone {
Milestone {
id: format!("ms-{}", idx + 1),
title: plan_milestone.title.clone(),
features: plan_milestone
.features
.iter()
.enumerate()
.map(|(fi, feature)| Feature {
id: format!("f-{}-{}", idx + 1, fi + 1),
title: feature.title.clone(),
spec: feature.spec.clone(),
validation_criteria: feature.validation_criteria.clone(),
origin: FeatureOrigin::Plan,
status: FeatureStatus::Pending,
worker_runs: Vec::new(),
commits: Vec::new(),
respawns: 0,
})
.collect(),
status: MilestoneStatus::Pending,
fix_cycles: 0,
start_sha: None,
validator_guidance: None,
}
}
fn norm_title(title: &str) -> String {
title.trim().to_ascii_lowercase()
}
fn milestone_mut<'a>(state: &'a mut MissionState, id: &str) -> Result<&'a mut Milestone> {
state
.mission
.milestones
.iter_mut()
.find(|m| m.id == id)
.ok_or_else(|| {
EngineError::InvalidState(format!("event references unknown milestone '{id}'"))
})
}
fn feature_mut<'a>(state: &'a mut MissionState, id: &str) -> Result<&'a mut Feature> {
state
.mission
.milestones
.iter_mut()
.flat_map(|m| m.features.iter_mut())
.find(|f| f.id == id)
.ok_or_else(|| {
EngineError::InvalidState(format!("event references unknown feature '{id}'"))
})
}
fn run_mut<'a>(state: &'a mut MissionState, id: &str) -> Result<&'a mut WorkerRun> {
state
.runs
.get_mut(id)
.ok_or_else(|| EngineError::InvalidState(format!("event references unknown run '{id}'")))
}
/// Recursive JSON merge: objects merge key-by-key, anything else in the patch
/// replaces the base value wholesale. `pub(crate)` so the provenance replay
/// (provenance.rs) evolves its tracked config by the SAME merge — a second
/// spelling of "how a config.changed patch applies" could drift from this one.
pub(crate) fn deep_merge(base: &mut serde_json::Value, patch: &serde_json::Value) {
use serde_json::Value;
match (base, patch) {
(Value::Object(base_map), Value::Object(patch_map)) => {
for (key, patch_value) in patch_map {
deep_merge(
base_map.entry(key.clone()).or_insert(Value::Null),
patch_value,
);
}
}
(base_slot, patch_value) => *base_slot = patch_value.clone(),
}
}
// ---------------------------------------------------------------------------
// Snapshot cache (state.json)
// ---------------------------------------------------------------------------
/// Serialize the state pretty-printed to a sibling tmp file, then atomically
/// rename over `path` so readers never observe a half-written snapshot.
pub fn write_snapshot(state: &MissionState, path: &Path) -> Result<()> {
let file_name = path.file_name().ok_or_else(|| {
EngineError::InvalidState(format!("snapshot path {} has no file name", path.display()))
})?;
let tmp_name = format!("{}.tmp", file_name.to_string_lossy());
let (parent, pinned_name) = crate::paths::open_parent_nofollow(path)?;
// Refuse hostile leaves before opening. The no-follow open below is the
// authoritative race-safe check; this metadata pass gives a useful error
// for directories, fifos, devices, and pre-existing symlinks.
for (name, display) in [
(pinned_name.as_os_str(), path.to_path_buf()),
(
std::ffi::OsStr::new(&tmp_name),
path.with_file_name(&tmp_name),
),
] {
match parent.symlink_metadata(name) {
Ok(metadata) if metadata.file_type().is_file() => {}
Ok(_) => {
return Err(EngineError::InvalidState(format!(
"refusing snapshot write through non-regular path {}",
display.display()
)))
}
Err(error) if error.kind() == std::io::ErrorKind::NotFound => {}
Err(error) => return Err(error.into()),
}
}
let json = serde_json::to_string_pretty(state)?;
{
use cap_fs_ext::OpenOptionsFollowExt as _;
use cap_primitives::fs::FollowSymlinks;
let mut options = cap_std::fs::OpenOptions::new();
options
.write(true)
.create(true)
.truncate(true)
.follow(FollowSymlinks::No);
let mut file = parent.open_with(&tmp_name, &options)?.into_std();
std::io::Write::write_all(&mut file, json.as_bytes())?;
file.sync_data()?;
}
parent.rename(&tmp_name, &parent, &pinned_name)?;
Ok(())
}
/// Read a snapshot previously written by [`write_snapshot`]. A symlinked
/// `state.json` — or any symlinked component above it — is refused (P1
/// mission-path-no-follow), never read through: mission-layout paths are
/// pinned capability-relative from the trusted repo-root anchor (7th-pass
/// review); out-of-layout paths (test scratch) use the weaker
/// canonicalize tier — see [`crate::paths::open_read_nofollow`].
pub fn read_snapshot(path: &Path) -> Result<MissionState> {
use std::io::Read;
let mut content = String::new();
crate::paths::open_read_nofollow(path)?.read_to_string(&mut content)?;
Ok(serde_json::from_str(&content)?)
}
#[cfg(test)]
mod negative_control_tests {
use super::*;
fn event(seq: u64, kind: EventKind) -> Event {
Event {
seq,
ts: chrono::Utc::now(),
mission_id: "m-controls".into(),
kind,
}
}
fn plan() -> Plan {
serde_json::from_value(serde_json::json!({
"goal": "control tests",
"validationContract": [{
"id": "a-1", "statement": "reject the defect", "check": "command", "command": "sh check.sh",
"negativeControl": {
"checkerFiles": [{"path": "check.sh", "content": "check"}],
"validFiles": [{"path": "value.txt", "content": "valid"}],
"defectiveFiles": [{"path": "value.txt", "content": "defect"}],
"expectedFailure": "wrong-value"
}
}],
"milestones": [{"title": "one", "features": [{"title": "change", "spec": "change", "validationCriteria": []}]}]
})).unwrap()
}
fn approved(plan: &Plan) -> Result<MissionState> {
fold(&[
event(
1,
EventKind::MissionCreated {
goal: plan.goal.clone(),
base_branch: "main".into(),
mission_branch: "kranz/mission-m-controls".into(),
config: MissionConfig::default(),
},
),
event(
2,
EventKind::PlanApproved {
plan: plan.clone(),
base_sha: Some("base".into()),
},
),
])
}
#[test]
fn negative_control_replay_and_revision_validation_agree() {
let plan = plan();
let state = approved(&plan).unwrap();
dry_run_revised_plan(&state, &plan, 1).unwrap();
let mut added = plan.clone();
let mut extra = added.validation_contract[0].clone();
extra.id = "a-2".into();
added.validation_contract.push(extra);
crate::planning::validate_revised_plan_for_gate(&state.mission, &added).unwrap();
dry_run_revised_plan(&state, &added, 1).unwrap();
for mutation in ["removed", "changed", "invalid-new"] {
let mut revised = plan.clone();
match mutation {
"removed" => revised.validation_contract[0].negative_control = None,
"changed" => {
revised.validation_contract[0]
.negative_control
.as_mut()
.unwrap()
.expected_failure = "other-defect".into()
}
_ => {
revised = added.clone();
revised.validation_contract[1]
.negative_control
.as_mut()
.unwrap()
.timeout_seconds = 0;
}
}
assert!(
crate::planning::validate_revised_plan_for_gate(&state.mission, &revised).is_err(),
"pre-emit {mutation}"
);
assert!(
dry_run_revised_plan(&state, &revised, 1).is_err(),
"fold {mutation}"
);
}
let mut malformed = plan;
malformed.validation_contract[0]
.negative_control
.as_mut()
.unwrap()
.timeout_seconds = 0;
assert!(
approved(&malformed).is_err(),
"new malformed control cannot enter via plan.approved replay"
);
}
#[test]
fn negative_control_checks_preserve_legacy_pty_revision_replay() {
let mut old = plan();
old.validation_contract = serde_json::from_value(serde_json::json!([{
"id": "pty", "statement": "legacy terminal check", "check": "pty-script",
"ptyScript": {"command": "old", "steps": []}
}]))
.unwrap();
let state = approved(&old).unwrap();
assert!(state.mission.validation_contract[0]
.negative_control
.is_none());
let mut revised = old;
revised.validation_contract[0]
.pty_script
.as_mut()
.unwrap()
.command = "new".into();
crate::planning::validate_revised_plan_for_gate(&state.mission, &revised).unwrap();
dry_run_revised_plan(&state, &revised, 1).unwrap();
}
}
#[cfg(test)]
mod executor_tier_tests {
use super::*;
use crate::events::EventKind;
fn created_event(config: MissionConfig) -> Event {
Event {
seq: 1,
ts: chrono::Utc::now(),
mission_id: "m-test".to_string(),
kind: EventKind::MissionCreated {
goal: "ship the thing".to_string(),
base_branch: "main".to_string(),
mission_branch: "kranz/mission-m-test".to_string(),
config,
},
}
}
#[test]
fn executor_routing_applies_local_worker_backend_folds_to_local_tier() {
let mut config = MissionConfig::default();
config.worker.backend = Some("local".to_string());
let state = fold(&[created_event(config)]).unwrap();
assert_eq!(state.executor_tier(), ExecutorTier::Local);
}
#[test]
fn executor_routing_applies_non_local_worker_backend_folds_to_frontier_tier() {
let mut config = MissionConfig::default();
config.worker.backend = Some("codex".to_string());
let state = fold(&[created_event(config)]).unwrap();
assert_eq!(state.executor_tier(), ExecutorTier::Frontier);
}
#[test]
fn validator_stays_frontier_regardless_of_worker_routing() {
let mut config = MissionConfig::default();
config.worker.backend = Some("local".to_string());
let state = fold(&[created_event(config)]).unwrap();
assert_eq!(
state.config.backend_kind(Role::ValidatorScrutiny),
BackendKind::Claude
);
assert_eq!(
state.config.backend_kind(Role::ValidatorFunctional),
BackendKind::Claude
);
}
}
#[cfg(test)]
mod hook_gate_projection_tests {
use super::*;
use crate::events::EventKind;
fn event(seq: u64, kind: EventKind) -> Event {
Event {
seq,
ts: chrono::Utc::now(),
mission_id: "m-test".to_string(),
kind,
}
}
fn spawned(seq: u64, run_id: &str) -> Event {
event(
seq,
EventKind::WorkerSpawned {
backend: None,
run_id: run_id.to_string(),
role: Role::Worker,
feature_id: None,
milestone_id: None,
candidate: None,
executor_route: None,
sdk_session_id: "s-1".to_string(),
model: "m".to_string(),
quant: "n/a".to_string(),
weight_hash: None,
prompt_hash: "h".to_string(),
transcript_path: "runs/r-1.jsonl".to_string(),
},
)
}
/// The hook.gate.fired fold arm is record-only (KRZ-302): the run
/// reference is validated as a corruption guard, and state shape does
/// not grow — the mission's course is unchanged by the in-process
/// verdict (the engine-side sweep remains the authoritative layer).
#[test]
fn hook_gate_projection_event_folds_record_only() {
let created = event(
1,
EventKind::MissionCreated {
goal: "g".to_string(),
base_branch: "main".to_string(),
mission_branch: "kranz/mission-m-test".to_string(),
config: MissionConfig::default(),
},
);
let fired = event(
3,
EventKind::HookGateFired {
run_id: "r-1".to_string(),
gate: "out-of-contract-write".to_string(),
hook_event: "PreToolUse".to_string(),
tool: "Write".to_string(),
subject: "docs/oops.md".to_string(),
verdict: "blocked".to_string(),
detail: Some("outside the touch set".to_string()),
},
);
let with_hook = fold(&[created.clone(), spawned(2, "r-1"), fired]).unwrap();
let without_hook = fold(&[created, spawned(2, "r-1")]).unwrap();
// No state transition: identical mission status, run set, and run
// outcome with and without the hook event.
assert_eq!(with_hook.mission.status, without_hook.mission.status);
assert_eq!(with_hook.runs.len(), 1);
assert!(with_hook.runs["r-1"].result.is_none());
assert_eq!(
with_hook.mission.milestones.len(),
without_hook.mission.milestones.len()
);
}
/// The run reference is a corruption guard: a hook.gate.fired naming a
/// run the log never recorded refuses the fold (the run id is
/// engine-stamped at fold time, so this can only be log corruption).
#[test]
fn hook_gate_projection_event_with_unknown_run_is_refused() {
let created = event(
1,
EventKind::MissionCreated {
goal: "g".to_string(),
base_branch: "main".to_string(),
mission_branch: "kranz/mission-m-test".to_string(),
config: MissionConfig::default(),
},
);
let bogus = event(
2,
EventKind::HookGateFired {
run_id: "no-such-run".to_string(),
gate: "out-of-contract-write".to_string(),
hook_event: "PreToolUse".to_string(),
tool: "Write".to_string(),
subject: "docs/oops.md".to_string(),
verdict: "blocked".to_string(),
detail: None,
},
);
assert!(fold(&[created, bogus]).is_err());
}
}
#[cfg(test)]
mod routing_abstraction_tests {
use super::*;
use crate::events::EventKind;
fn event(seq: u64, kind: EventKind) -> Event {
Event {
seq,
ts: chrono::Utc::now(),
mission_id: "m-test".to_string(),
kind,
}
}
fn created_with_local_worker() -> Event {
let mut config = MissionConfig::default();
config.worker.backend = Some("local".to_string());
event(
1,
EventKind::MissionCreated {
goal: "g".to_string(),
base_branch: "main".to_string(),
mission_branch: "kranz/mission-m-test".to_string(),
config,
},
)
}
fn spawned(seq: u64, run_id: &str) -> Event {
event(
seq,
EventKind::WorkerSpawned {
backend: None,
run_id: run_id.to_string(),
role: Role::Worker,
feature_id: None,
milestone_id: None,
candidate: None,
executor_route: None,
sdk_session_id: "s-1".to_string(),
model: "m".to_string(),
quant: "n/a".to_string(),
weight_hash: None,
prompt_hash: "h".to_string(),
transcript_path: "runs/r-1.jsonl".to_string(),
},
)
}
/// The worker.escalated fold arm is record-only (KRZ-331, the gate.result
/// template): a worker escalation NEVER bypasses the floor's validator
/// requirements — the validator route, the executor tier, and every
/// decision-keyed counter fold exactly as if the event were absent
/// (contrast tier.escalated, the orchestrator-initiated valve, which
/// deliberately rewrites worker config). Only the run reference is
/// validated, as a corruption guard (mirrors hook.gate.fired).
#[test]
fn routing_abstraction_escalation_folds_record_only_leaving_validators_untouched() {
let escalated = event(
3,
EventKind::WorkerEscalated {
run_id: "r-1".to_string(),
feature_id: "f-1-1".to_string(),
from: ExecutorTier::Local,
to: ExecutorTier::Frontier,
reason: "spec ambiguity beyond my confidence".to_string(),
},
);
let with = fold(&[created_with_local_worker(), spawned(2, "r-1"), escalated]).unwrap();
let without = fold(&[created_with_local_worker(), spawned(2, "r-1")]).unwrap();
// The WHOLE config is identical with and without the escalation —
// validator backends are inside it, so this pins "validator route
// unaffected" exactly, not by a sampled field.
assert_eq!(with.config, without.config);
assert_eq!(
with.config.backend_kind(Role::ValidatorScrutiny),
BackendKind::Claude
);
assert_eq!(
with.config.backend_kind(Role::ValidatorFunctional),
BackendKind::Claude
);
// The worker escalation never flips the executor tier (that flip is
// tier.escalated's job, and it is orchestrator-initiated only).
assert_eq!(with.executor_tier(), ExecutorTier::Local);
// No state transition of any kind: same mission status, same run
// set, same escalation counters.
assert_eq!(with.mission.status, without.mission.status);
assert_eq!(with.runs.len(), without.runs.len());
assert_eq!(with.escalated_milestones, without.escalated_milestones);
assert_eq!(
with.local_executor_milestones,
without.local_executor_milestones
);
}
/// The run reference is a corruption guard: a worker.escalated naming a
/// run the log never recorded refuses the fold (the run id is
/// engine-stamped at emit time, so this can only be log corruption).
#[test]
fn routing_abstraction_escalation_with_unknown_run_is_refused() {
let bogus = event(
2,
EventKind::WorkerEscalated {
run_id: "no-such-run".to_string(),
feature_id: "f-1-1".to_string(),
from: ExecutorTier::Local,
to: ExecutorTier::Frontier,
reason: "r".to_string(),
},
);
let mut config = MissionConfig::default();
config.worker.backend = Some("local".to_string());
let created = event(
1,
EventKind::MissionCreated {
goal: "g".to_string(),
base_branch: "main".to_string(),
mission_branch: "kranz/mission-m-test".to_string(),
config,
},
);
assert!(fold(&[created, bogus]).is_err());
}
}