agent-sdk-core 0.1.0-alpha.4

Product-neutral primitive kernel and contracts for a Rust-first Agent SDK.
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
//! Application-layer coordination over core primitives. Use these services to lower
//! helpers, drive runs, validate output, coordinate tools, approvals, delivery,
//! isolation, telemetry, and feature layers. Methods in this layer may call
//! configured ports, mutate in-memory stores, append journals, or publish events as
//! documented. This file contains the loop state portion of that contract.
//!
use std::collections::BTreeSet;

use serde::{Deserialize, Serialize};

use crate::{
    error::{AgentError, AgentErrorKind, RetryClassification},
    journal::JournalRecordKind,
    recovery::RecoveryClassification,
};

#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
#[serde(rename_all = "snake_case")]
/// Names the finite phases of the SDK run loop.
/// Use it in state tables, journal records, and tests to describe where a run is paused or
/// executing; the enum is a marker only and side effects happen in the driver transition that
/// enters the phase.
pub enum LoopState {
    /// The run has been accepted and initial package, policy, and registry checks are beginning.
    Starting,
    /// The runtime is collecting context contributions before anything is shown to a provider.
    ContextAssembly,
    /// Context has been admitted and is being projected into the provider-visible request shape.
    ProviderProjection,
    /// A provider call is active and model deltas may be emitted.
    ModelStreaming,
    /// Stream rules are inspecting, interrupting, or transforming an active model stream.
    StreamIntervention,
    /// The runtime is interpreting provider output into candidate tool calls or loop actions.
    ToolPlanning,
    /// A tool or extension action is waiting for an approval decision before execution.
    Approval,
    /// Policy or approval denied a requested tool before the executor was started.
    ToolDenied,
    /// A journal-backed tool intent has been recorded and the configured executor may be running.
    ToolExecution,
    /// Cancellation, timeout, stream policy, or host intervention interrupted active work.
    Interrupted,
    /// The run is suspended and requires a wake, resume signal, or durable replay input.
    WaitingForResume,
    /// The runtime is compacting context or summaries before continuing the loop.
    Compaction,
    /// The current iteration completed and the driver is preparing another loop iteration.
    Continue,
    /// Replay, repair, or reconciliation is resolving an incomplete or uncertain effect.
    Recovery,
    /// The run reached a successful terminal result.
    Completed,
    /// The run reached a cancellation terminal result.
    Cancelled,
    /// The run reached an unrecoverable failure terminal result.
    Failed,
}

impl LoopState {
    /// Constant value for the application::loop_state contract. Use it
    /// to keep SDK records and tests aligned on the same stable value.
    pub const ALL: [Self; 17] = [
        Self::Starting,
        Self::ContextAssembly,
        Self::ProviderProjection,
        Self::ModelStreaming,
        Self::StreamIntervention,
        Self::ToolPlanning,
        Self::Approval,
        Self::ToolDenied,
        Self::ToolExecution,
        Self::Interrupted,
        Self::WaitingForResume,
        Self::Compaction,
        Self::Continue,
        Self::Recovery,
        Self::Completed,
        Self::Cancelled,
        Self::Failed,
    ];

    /// Returns the all currently held by this value.
    /// This is state-table metadata only and does not advance a run or mutate loop state.
    pub fn all() -> &'static [Self] {
        &Self::ALL
    }

    /// Returns the stable contract spelling for this loop state.
    /// This is a pure mapping used by fixtures and diagnostics; it does not advance the loop.
    pub fn contract_name(self) -> &'static str {
        match self {
            Self::Starting => "Starting",
            Self::ContextAssembly => "ContextAssembly",
            Self::ProviderProjection => "ProviderProjection",
            Self::ModelStreaming => "ModelStreaming",
            Self::StreamIntervention => "StreamIntervention",
            Self::ToolPlanning => "ToolPlanning",
            Self::Approval => "Approval",
            Self::ToolDenied => "ToolDenied",
            Self::ToolExecution => "ToolExecution",
            Self::Interrupted => "Interrupted",
            Self::WaitingForResume => "WaitingForResume",
            Self::Compaction => "Compaction",
            Self::Continue => "Continue",
            Self::Recovery => "Recovery",
            Self::Completed => "Completed",
            Self::Cancelled => "Cancelled",
            Self::Failed => "Failed",
        }
    }

    /// Reports whether this value is terminal. The check is pure and
    /// does not mutate SDK or host state.
    pub fn is_terminal(self) -> bool {
        matches!(self, Self::Completed | Self::Cancelled)
    }

    /// Returns whether can carry terminal result applies for this state.
    /// This is state-table metadata only and does not advance a run or mutate loop state.
    pub fn can_carry_terminal_result(self) -> bool {
        matches!(self, Self::Completed | Self::Cancelled | Self::Failed)
    }

    /// Returns whether requires cancel transition applies for this state.
    /// This is state-table metadata only and does not advance a run or mutate loop state.
    pub fn requires_cancel_transition(self) -> bool {
        !self.is_terminal()
    }
}

#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
/// Enumerates the finite loop trigger cases.
/// Serialized names are part of the SDK contract; update fixtures when variants change.
pub enum LoopTrigger {
    /// Use this variant when the contract needs to represent start run; selecting it has no side effect by itself.
    StartRun,
    /// Use this variant when the contract needs to represent context ready; selecting it has no side effect by itself.
    ContextReady,
    /// Use this variant when the contract needs to represent projection ready; selecting it has no side effect by itself.
    ProjectionReady,
    /// Use this variant when the contract needs to represent tool use; selecting it has no side effect by itself.
    ToolUse,
    /// Use this variant when the contract needs to represent stream rule match; selecting it has no side effect by itself.
    StreamRuleMatch,
    /// Use this variant when the contract needs to represent end turn; selecting it has no side effect by itself.
    EndTurn,
    /// Use this variant when the contract needs to represent provider failure; selecting it has no side effect by itself.
    ProviderFailure,
    /// Use this variant when the contract needs to represent max iterations reached; selecting it has no side effect by itself.
    MaxIterationsReached {
        /// Outcome used by this record or request.
        outcome: MaxIterationOutcome,
    },
    /// Use this variant when the contract needs to represent compaction needed; selecting it has no side effect by itself.
    CompactionNeeded,
    /// Use this variant when the contract needs to represent stream stop run; selecting it has no side effect by itself.
    StreamStopRun,
    /// Use this variant when the contract needs to represent stream abort and retry; selecting it has no side effect by itself.
    StreamAbortAndRetry,
    /// Use this variant when the contract needs to represent stream pause for approval; selecting it has no side effect by itself.
    StreamPauseForApproval,
    /// Use this variant when the contract needs to represent stream unsafe intervention; selecting it has no side effect by itself.
    StreamUnsafeIntervention,
    /// Use this variant when the contract needs to represent policy allow; selecting it has no side effect by itself.
    PolicyAllow,
    /// Use this variant when the contract needs to represent policy ask; selecting it has no side effect by itself.
    PolicyAsk,
    /// Use this variant when the contract needs to represent policy deny; selecting it has no side effect by itself.
    PolicyDeny,
    /// Use this variant when the contract needs to represent approved; selecting it has no side effect by itself.
    Approved,
    /// Use this variant when the contract needs to represent approval denied; selecting it has no side effect by itself.
    ApprovalDenied,
    /// Use this variant when the contract needs to represent approval timeout; selecting it has no side effect by itself.
    ApprovalTimeout,
    /// Use this variant when the contract needs to represent approval transport fatal; selecting it has no side effect by itself.
    ApprovalTransportFatal,
    /// Use this variant when the contract needs to represent stream approval resumed; selecting it has no side effect by itself.
    StreamApprovalResumed,
    /// Use this variant when the contract needs to represent continue with denied result; selecting it has no side effect by itself.
    ContinueWithDeniedResult,
    /// Use this variant when the contract needs to represent fail on denied; selecting it has no side effect by itself.
    FailOnDenied,
    /// Use this variant when the contract needs to represent tool complete; selecting it has no side effect by itself.
    ToolComplete,
    /// Use this variant when the contract needs to represent tool interrupt; selecting it has no side effect by itself.
    ToolInterrupt,
    /// Use this variant when the contract needs to represent tool failure; selecting it has no side effect by itself.
    ToolFailure,
    /// Use this variant when the contract needs to represent wait for resume; selecting it has no side effect by itself.
    WaitForResume,
    /// Use this variant when the contract needs to represent resume allowed; selecting it has no side effect by itself.
    ResumeAllowed,
    /// Use this variant when the contract needs to represent resume denied; selecting it has no side effect by itself.
    ResumeDenied,
    /// Use this variant when the contract needs to represent compaction complete; selecting it has no side effect by itself.
    CompactionComplete,
    /// Use this variant when the contract needs to represent continue loop; selecting it has no side effect by itself.
    ContinueLoop,
    /// Use this variant when the contract needs to represent failure classified; selecting it has no side effect by itself.
    FailureClassified {
        /// Classification used by this record or request.
        classification: RecoveryClassification,
    },
    /// Use this variant when the contract needs to represent repair applied; selecting it has no side effect by itself.
    RepairApplied,
    /// Use this variant when the contract needs to represent repair completed terminal; selecting it has no side effect by itself.
    RepairCompletedTerminal,
    /// Use this variant when the contract needs to represent recovery irrecoverable; selecting it has no side effect by itself.
    RecoveryIrrecoverable,
    /// Use this variant when the contract needs to represent cancel requested; selecting it has no side effect by itself.
    CancelRequested,
}

#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
/// Enumerates the finite max iteration outcome cases.
/// Serialized names are part of the SDK contract; update fixtures when variants change.
pub enum MaxIterationOutcome {
    /// Use this variant when the contract needs to represent complete; selecting it has no side effect by itself.
    Complete,
    /// Use this variant when the contract needs to represent fail; selecting it has no side effect by itself.
    Fail,
}

#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
#[serde(rename_all = "snake_case")]
/// Enumerates the finite transition guard cases.
/// Serialized names are part of the SDK contract; update fixtures when variants change.
pub enum TransitionGuard {
    /// Use this variant when the contract needs to represent none; selecting it has no side effect by itself.
    None,
    /// Use this variant when the contract needs to represent package valid; selecting it has no side effect by itself.
    PackageValid,
    /// Use this variant when the contract needs to represent budget valid; selecting it has no side effect by itself.
    BudgetValid,
    /// Use this variant when the contract needs to represent package hashes match; selecting it has no side effect by itself.
    PackageHashesMatch,
    /// Use this variant when the contract needs to represent model message has tool calls; selecting it has no side effect by itself.
    ModelMessageHasToolCalls,
    /// Use this variant when the contract needs to represent rule action allowed; selecting it has no side effect by itself.
    RuleActionAllowed,
    /// Use this variant when the contract needs to represent final message complete; selecting it has no side effect by itself.
    FinalMessageComplete,
    /// Use this variant when the contract needs to represent provider failure classified; selecting it has no side effect by itself.
    ProviderFailureClassified,
    /// Use this variant when the contract needs to represent permissions pass; selecting it has no side effect by itself.
    PermissionsPass,
    /// Use this variant when the contract needs to represent dispatcher available or escalation configured; selecting it has no side effect by itself.
    DispatcherAvailableOrEscalationConfigured,
    /// Use this variant when the contract needs to represent denied result allowed; selecting it has no side effect by itself.
    DeniedResultAllowed,
    /// Use this variant when the contract needs to represent decision valid; selecting it has no side effect by itself.
    DecisionValid,
    /// Use this variant when the contract needs to represent timeout elapsed; selecting it has no side effect by itself.
    TimeoutElapsed,
    /// Use this variant when the contract needs to represent approval transport fatal; selecting it has no side effect by itself.
    ApprovalTransportFatal,
    /// Use this variant when the contract needs to represent terminal status appended; selecting it has no side effect by itself.
    TerminalStatusAppended,
    /// Use this variant when the contract needs to represent interrupt resumable; selecting it has no side effect by itself.
    InterruptResumable,
    /// Use this variant when the contract needs to represent resume token required; selecting it has no side effect by itself.
    ResumeTokenRequired,
    /// Use this variant when the contract needs to represent checkpoint and package valid; selecting it has no side effect by itself.
    CheckpointAndPackageValid,
    /// Use this variant when the contract needs to represent protected context preserved; selecting it has no side effect by itself.
    ProtectedContextPreserved,
    /// Use this variant when the contract needs to represent repair plan safe; selecting it has no side effect by itself.
    RepairPlanSafe,
    /// Use this variant when the contract needs to represent invariant restored; selecting it has no side effect by itself.
    InvariantRestored,
    /// Use this variant when the contract needs to represent max iteration budget exhausted; selecting it has no side effect by itself.
    MaxIterationBudgetExhausted,
    /// Use this variant when the contract needs to represent cancellation requested; selecting it has no side effect by itself.
    CancellationRequested,
    /// Use this variant when the contract needs to represent unsafe intervention; selecting it has no side effect by itself.
    UnsafeIntervention,
}

#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
/// Holds transition guard set application-layer state or configuration.
/// Use it with the documented coordinator methods; run, journal, event, provider, or port effects are called out on those methods rather than on construction.
pub struct TransitionGuardSet {
    #[serde(default)]
    satisfied: BTreeSet<TransitionGuard>,
}

impl TransitionGuardSet {
    /// Creates a new application::loop_state value with explicit
    /// caller-provided inputs. This constructor is data-only and
    /// performs no I/O or external side effects.
    pub fn new() -> Self {
        Self::default()
    }

    /// Returns an updated value with with configured.
    /// This is data-only and does not perform I/O, call host ports, append journals, publish
    /// events, or start processes.
    pub fn with(mut self, guard: TransitionGuard) -> Self {
        self.satisfied.insert(guard);
        self
    }

    /// Reads the stored contains without registry or runtime work.
    /// This is data-only and does not perform I/O, call host ports, append journals, publish
    /// events, or start processes.
    pub fn contains(&self, guard: TransitionGuard) -> bool {
        guard == TransitionGuard::None || self.satisfied.contains(&guard)
    }

    /// Returns for rule derived from the supplied state.
    /// This uses only local coordinator state and performs no hidden host work.
    pub fn for_rule(rule: &TransitionRule) -> Self {
        if rule.guard == TransitionGuard::None {
            Self::default()
        } else {
            Self::default().with(rule.guard)
        }
    }
}

impl<const N: usize> From<[TransitionGuard; N]> for TransitionGuardSet {
    fn from(guards: [TransitionGuard; N]) -> Self {
        let mut set = Self::default();
        for guard in guards {
            set.satisfied.insert(guard);
        }
        set
    }
}

#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
/// Enumerates the finite checkpoint policy cases.
/// Serialized names are part of the SDK contract; update fixtures when variants change.
pub enum CheckpointPolicy {
    /// Use this variant when the contract needs to represent none; selecting it has no side effect by itself.
    None,
    /// Use this variant when the contract needs to represent before; selecting it has no side effect by itself.
    Before,
    /// Use this variant when the contract needs to represent after; selecting it has no side effect by itself.
    After,
    /// Use this variant when the contract needs to represent before and after; selecting it has no side effect by itself.
    BeforeAndAfter,
    /// Use this variant when the contract needs to represent terminal; selecting it has no side effect by itself.
    Terminal,
}

#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
/// Enumerates the finite side effect policy cases.
/// Serialized names are part of the SDK contract; update fixtures when variants change.
pub enum SideEffectPolicy {
    /// Use this variant when the contract needs to represent none; selecting it has no side effect by itself.
    None,
    /// Use this variant when the contract needs to represent intent before effect; selecting it has no side effect by itself.
    IntentBeforeEffect,
    /// Use this variant when the contract needs to represent idempotent retry allowed; selecting it has no side effect by itself.
    IdempotentRetryAllowed,
    /// Use this variant when the contract needs to represent non idempotent fail closed; selecting it has no side effect by itself.
    NonIdempotentFailClosed,
    /// Use this variant when the contract needs to represent reconcile required; selecting it has no side effect by itself.
    ReconcileRequired,
}

#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
/// Enumerates the finite loop event kind cases.
/// Serialized names are part of the SDK contract; update fixtures when variants change.
pub enum LoopEventKind {
    /// Use this variant when the contract needs to represent run started; selecting it has no side effect by itself.
    RunStarted,
    /// Use this variant when the contract needs to represent run completed; selecting it has no side effect by itself.
    RunCompleted,
    /// Use this variant when the contract needs to represent run failed; selecting it has no side effect by itself.
    RunFailed,
    /// Use this variant when the contract needs to represent run cancelled; selecting it has no side effect by itself.
    RunCancelled,
    /// Use this variant when the contract needs to represent run cancel requested; selecting it has no side effect by itself.
    RunCancelRequested,
    /// Use this variant when the contract needs to represent run checkpointed; selecting it has no side effect by itself.
    RunCheckpointed,
    /// Use this variant when the contract needs to represent run resume requested; selecting it has no side effect by itself.
    RunResumeRequested,
    /// Use this variant when the contract needs to represent run resume failed; selecting it has no side effect by itself.
    RunResumeFailed,
    /// Use this variant when the contract needs to represent context assembled; selecting it has no side effect by itself.
    ContextAssembled,
    /// Use this variant when the contract needs to represent provider request projected; selecting it has no side effect by itself.
    ProviderRequestProjected,
    /// Use this variant when the contract needs to represent model attempt started; selecting it has no side effect by itself.
    ModelAttemptStarted,
    /// Use this variant when the contract needs to represent model attempt failed; selecting it has no side effect by itself.
    ModelAttemptFailed,
    /// Use this variant when the contract needs to represent model message completed; selecting it has no side effect by itself.
    ModelMessageCompleted,
    /// Use this variant when the contract needs to represent tool requested; selecting it has no side effect by itself.
    ToolRequested,
    /// Use this variant when the contract needs to represent tool approval required; selecting it has no side effect by itself.
    ToolApprovalRequired,
    /// Use this variant when the contract needs to represent tool started; selecting it has no side effect by itself.
    ToolStarted,
    /// Use this variant when the contract needs to represent tool completed; selecting it has no side effect by itself.
    ToolCompleted,
    /// Use this variant when the contract needs to represent tool failed; selecting it has no side effect by itself.
    ToolFailed,
    /// Use this variant when the contract needs to represent tool interrupted; selecting it has no side effect by itself.
    ToolInterrupted,
    /// Use this variant when the contract needs to represent approval requested; selecting it has no side effect by itself.
    ApprovalRequested,
    /// Use this variant when the contract needs to represent approval responded; selecting it has no side effect by itself.
    ApprovalResponded,
    /// Use this variant when the contract needs to represent approval timed out; selecting it has no side effect by itself.
    ApprovalTimedOut,
    /// Use this variant when the contract needs to represent approval denied; selecting it has no side effect by itself.
    ApprovalDenied,
    /// Use this variant when the contract needs to represent stream rule matched; selecting it has no side effect by itself.
    StreamRuleMatched,
    /// Use this variant when the contract needs to represent stream intervention applied; selecting it has no side effect by itself.
    StreamInterventionApplied,
    /// Use this variant when the contract needs to represent context compaction completed; selecting it has no side effect by itself.
    ContextCompactionCompleted,
    /// Use this variant when the contract needs to represent recovery planned; selecting it has no side effect by itself.
    RecoveryPlanned,
    /// Use this variant when the contract needs to represent replay completed; selecting it has no side effect by itself.
    ReplayCompleted,
}

#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
/// Enumerates the finite loop terminal status cases.
/// Serialized names are part of the SDK contract; update fixtures when variants change.
pub enum LoopTerminalStatus {
    /// Use this variant when the contract needs to represent completed; selecting it has no side effect by itself.
    Completed,
    /// Use this variant when the contract needs to represent cancelled; selecting it has no side effect by itself.
    Cancelled,
    /// Use this variant when the contract needs to represent failed; selecting it has no side effect by itself.
    Failed,
}

#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
/// Enumerates the finite loop stop reason cases.
/// Serialized names are part of the SDK contract; update fixtures when variants change.
pub enum LoopStopReason {
    /// Use this variant when the contract needs to represent end turn; selecting it has no side effect by itself.
    EndTurn,
    /// Use this variant when the contract needs to represent stream rule stop; selecting it has no side effect by itself.
    StreamRuleStop,
    /// Use this variant when the contract needs to represent max iterations; selecting it has no side effect by itself.
    MaxIterations {
        /// Outcome used by this record or request.
        outcome: MaxIterationOutcome,
    },
    /// Use this variant when the contract needs to represent cancelled; selecting it has no side effect by itself.
    Cancelled,
    /// Use this variant when the contract needs to represent provider failure; selecting it has no side effect by itself.
    ProviderFailure,
    /// Use this variant when the contract needs to represent unsafe stream intervention; selecting it has no side effect by itself.
    UnsafeStreamIntervention,
    /// Use this variant when the contract needs to represent tool denied; selecting it has no side effect by itself.
    ToolDenied,
    /// Use this variant when the contract needs to represent approval denied; selecting it has no side effect by itself.
    ApprovalDenied,
    /// Use this variant when the contract needs to represent approval timeout; selecting it has no side effect by itself.
    ApprovalTimeout,
    /// Use this variant when the contract needs to represent approval transport fatal; selecting it has no side effect by itself.
    ApprovalTransportFatal,
    /// Use this variant when the contract needs to represent tool failure; selecting it has no side effect by itself.
    ToolFailure,
    /// Use this variant when the contract needs to represent resume denied; selecting it has no side effect by itself.
    ResumeDenied,
    /// Use this variant when the contract needs to represent recovery completed; selecting it has no side effect by itself.
    RecoveryCompleted,
    /// Use this variant when the contract needs to represent recovery irrecoverable; selecting it has no side effect by itself.
    RecoveryIrrecoverable,
}

#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
/// Holds loop terminal result application-layer state or configuration.
/// Use it with the documented coordinator methods; run, journal, event, provider, or port effects are called out on those methods rather than on construction.
pub struct LoopTerminalResult {
    /// Finite status for this record or lifecycle stage.
    pub status: LoopTerminalStatus,
    /// Stop reason used by this record or request.
    pub stop_reason: LoopStopReason,
}

#[derive(Clone, Debug, Eq, PartialEq)]
/// Holds transition rule application-layer state or configuration.
/// Use it with the documented coordinator methods; run, journal, event, provider, or port effects are called out on those methods rather than on construction.
pub struct TransitionRule {
    /// From state used by this record or request.
    pub from_state: LoopState,
    /// Trigger used by this record or request.
    pub trigger: LoopTrigger,
    /// Guard used by this record or request.
    pub guard: TransitionGuard,
    /// Bounded events included in this record. Limits and truncation are
    /// represented by companion metadata when applicable.
    pub events: &'static [LoopEventKind],
    /// Journal record kinds produced by this capability or feature.
    /// Use them to keep replay and recovery fixtures aligned with the public contract.
    pub journal_records: &'static [JournalRecordKind],
    /// Checkpoint policy used by this record or request.
    pub checkpoint_policy: CheckpointPolicy,
    /// Side effect policy used by this record or request.
    pub side_effect_policy: SideEffectPolicy,
    /// Next state used by this record or request.
    pub next_state: LoopState,
    /// Optional terminal result value.
    /// When absent, callers should use the documented default or skip that optional behavior.
    pub terminal_result: Option<LoopTerminalResult>,
    /// Optional recovery classification value.
    /// When absent, callers should use the documented default or skip that optional behavior.
    pub recovery_classification: Option<RecoveryClassification>,
}

impl TransitionRule {
    /// Returns output derived from the supplied state.
    /// This uses only local coordinator state and performs no hidden host work.
    pub fn output(&self) -> TransitionOutput {
        TransitionOutput {
            from_state: self.from_state,
            trigger: self.trigger,
            guard: self.guard,
            events: self.events.to_vec(),
            journal_records: self.journal_records.to_vec(),
            checkpoint_policy: self.checkpoint_policy,
            side_effect_policy: self.side_effect_policy,
            next_state: self.next_state,
            terminal_result: self.terminal_result,
            recovery_classification: self.recovery_classification,
        }
    }
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
/// Holds transition input application-layer state or configuration.
/// Use it with the documented coordinator methods; run, journal, event, provider, or port effects are called out on those methods rather than on construction.
pub struct TransitionInput {
    /// State used by this record or request.
    pub state: LoopState,
    /// Trigger used by this record or request.
    pub trigger: LoopTrigger,
    #[serde(default)]
    /// Guards used by this record or request.
    pub guards: TransitionGuardSet,
}

impl TransitionInput {
    /// Creates a new application::loop_state value with explicit
    /// caller-provided inputs. This constructor is data-only and
    /// performs no I/O or external side effects.
    pub fn new(state: LoopState, trigger: LoopTrigger) -> Self {
        Self {
            state,
            trigger,
            guards: TransitionGuardSet::default(),
        }
    }

    /// Returns this value with its guard setting replaced. The method
    /// follows builder-style data construction and does not execute
    /// external work.
    pub fn with_guard(mut self, guard: TransitionGuard) -> Self {
        self.guards = self.guards.with(guard);
        self
    }

    /// Returns this value with its guards setting replaced. The method
    /// follows builder-style data construction and does not execute
    /// external work.
    pub fn with_guards(mut self, guards: TransitionGuardSet) -> Self {
        self.guards = guards;
        self
    }
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
/// Holds transition output application-layer state or configuration.
/// Use it with the documented coordinator methods; run, journal, event, provider, or port effects are called out on those methods rather than on construction.
pub struct TransitionOutput {
    /// From state used by this record or request.
    pub from_state: LoopState,
    /// Trigger used by this record or request.
    pub trigger: LoopTrigger,
    /// Guard used by this record or request.
    pub guard: TransitionGuard,
    /// Bounded events included in this record. Limits and truncation are
    /// represented by companion metadata when applicable.
    pub events: Vec<LoopEventKind>,
    /// Journal record kinds produced by this capability or feature.
    /// Use them to keep replay and recovery fixtures aligned with the public contract.
    pub journal_records: Vec<JournalRecordKind>,
    /// Checkpoint policy used by this record or request.
    pub checkpoint_policy: CheckpointPolicy,
    /// Side effect policy used by this record or request.
    pub side_effect_policy: SideEffectPolicy,
    /// Next state used by this record or request.
    pub next_state: LoopState,
    #[serde(skip_serializing_if = "Option::is_none")]
    /// Optional terminal result value.
    /// When absent, callers should use the documented default or skip that optional behavior.
    pub terminal_result: Option<LoopTerminalResult>,
    #[serde(skip_serializing_if = "Option::is_none")]
    /// Optional recovery classification value.
    /// When absent, callers should use the documented default or skip that optional behavior.
    pub recovery_classification: Option<RecoveryClassification>,
}

#[derive(Clone, Copy, Debug, Default)]
/// Holds agent state machine application-layer state or configuration.
/// Use it with the documented coordinator methods; run, journal, event, provider, or port effects are called out on those methods rather than on construction.
pub struct AgentStateMachine;

impl AgentStateMachine {
    /// Returns the transition table derived from this value.
    /// This uses only local coordinator state and performs no hidden host work.
    pub fn transition_table(&self) -> &'static [TransitionRule] {
        transition_table()
    }

    /// Validates the application::loop_state invariants and returns a
    /// typed error on failure. Validation is pure and does not perform
    /// I/O, dispatch, journal appends, or adapter calls.
    pub fn validate_transition(
        &self,
        input: TransitionInput,
    ) -> Result<TransitionOutput, AgentError> {
        validate_transition(input)
    }
}

/// Validates the application::loop_state invariants and returns a typed
/// error on failure. Validation is pure and does not perform I/O,
/// dispatch, journal appends, or adapter calls.
pub fn validate_transition(input: TransitionInput) -> Result<TransitionOutput, AgentError> {
    let rule = transition_table()
        .iter()
        .find(|rule| rule.from_state == input.state && rule.trigger == input.trigger)
        .ok_or_else(|| invalid_transition(input.state, input.trigger, None))?;

    if !input.guards.contains(rule.guard) {
        return Err(invalid_transition(
            input.state,
            input.trigger,
            Some(rule.guard),
        ));
    }

    Ok(rule.output())
}

/// Returns the transition table derived from this value.
/// This derives SDK state locally and does not call host adapters.
pub fn transition_table() -> &'static [TransitionRule] {
    TRANSITION_TABLE
}

/// Returns the contract state names derived from this value.
/// This derives SDK state locally and does not call host adapters.
pub fn contract_state_names() -> &'static [&'static str] {
    CONTRACT_STATE_NAMES
}

fn invalid_transition(
    state: LoopState,
    trigger: LoopTrigger,
    missing_guard: Option<TransitionGuard>,
) -> AgentError {
    let guard_message = missing_guard
        .map(|guard| format!("; missing guard {guard:?}"))
        .unwrap_or_default();
    AgentError::new(
        AgentErrorKind::InvalidStateTransition,
        RetryClassification::RepairNeeded,
        format!(
            "loop transition from {:?} with trigger {:?} is not allowed{}",
            state, trigger, guard_message
        ),
    )
}

const CONTRACT_STATE_NAMES: &[&str] = &[
    "Starting",
    "ContextAssembly",
    "ProviderProjection",
    "ModelStreaming",
    "StreamIntervention",
    "ToolPlanning",
    "Approval",
    "ToolDenied",
    "ToolExecution",
    "Interrupted",
    "WaitingForResume",
    "Compaction",
    "Continue",
    "Recovery",
    "Completed",
    "Cancelled",
    "Failed",
];

const TRANSITION_TABLE: &[TransitionRule] = &[
    TransitionRule {
        from_state: LoopState::Starting,
        trigger: LoopTrigger::StartRun,
        guard: TransitionGuard::PackageValid,
        events: &[LoopEventKind::RunStarted],
        journal_records: &[JournalRecordKind::Run],
        checkpoint_policy: CheckpointPolicy::After,
        side_effect_policy: SideEffectPolicy::None,
        next_state: LoopState::ContextAssembly,
        terminal_result: None,
        recovery_classification: None,
    },
    TransitionRule {
        from_state: LoopState::ContextAssembly,
        trigger: LoopTrigger::ContextReady,
        guard: TransitionGuard::BudgetValid,
        events: &[LoopEventKind::ContextAssembled],
        journal_records: &[JournalRecordKind::Context],
        checkpoint_policy: CheckpointPolicy::After,
        side_effect_policy: SideEffectPolicy::None,
        next_state: LoopState::ProviderProjection,
        terminal_result: None,
        recovery_classification: None,
    },
    TransitionRule {
        from_state: LoopState::ProviderProjection,
        trigger: LoopTrigger::ProjectionReady,
        guard: TransitionGuard::PackageHashesMatch,
        events: &[
            LoopEventKind::ProviderRequestProjected,
            LoopEventKind::ModelAttemptStarted,
        ],
        journal_records: &[JournalRecordKind::Context, JournalRecordKind::ModelAttempt],
        checkpoint_policy: CheckpointPolicy::Before,
        side_effect_policy: SideEffectPolicy::IntentBeforeEffect,
        next_state: LoopState::ModelStreaming,
        terminal_result: None,
        recovery_classification: None,
    },
    TransitionRule {
        from_state: LoopState::ModelStreaming,
        trigger: LoopTrigger::ToolUse,
        guard: TransitionGuard::ModelMessageHasToolCalls,
        events: &[
            LoopEventKind::ModelMessageCompleted,
            LoopEventKind::ToolRequested,
        ],
        journal_records: &[JournalRecordKind::ModelAttempt, JournalRecordKind::Tool],
        checkpoint_policy: CheckpointPolicy::After,
        side_effect_policy: SideEffectPolicy::None,
        next_state: LoopState::ToolPlanning,
        terminal_result: None,
        recovery_classification: None,
    },
    TransitionRule {
        from_state: LoopState::ModelStreaming,
        trigger: LoopTrigger::StreamRuleMatch,
        guard: TransitionGuard::RuleActionAllowed,
        events: &[LoopEventKind::StreamRuleMatched],
        journal_records: &[JournalRecordKind::StreamRule],
        checkpoint_policy: CheckpointPolicy::None,
        side_effect_policy: SideEffectPolicy::None,
        next_state: LoopState::StreamIntervention,
        terminal_result: None,
        recovery_classification: None,
    },
    TransitionRule {
        from_state: LoopState::ModelStreaming,
        trigger: LoopTrigger::CompactionNeeded,
        guard: TransitionGuard::BudgetValid,
        events: &[LoopEventKind::RunCheckpointed],
        journal_records: &[JournalRecordKind::Context],
        checkpoint_policy: CheckpointPolicy::Before,
        side_effect_policy: SideEffectPolicy::None,
        next_state: LoopState::Compaction,
        terminal_result: None,
        recovery_classification: None,
    },
    TransitionRule {
        from_state: LoopState::ModelStreaming,
        trigger: LoopTrigger::EndTurn,
        guard: TransitionGuard::FinalMessageComplete,
        events: &[
            LoopEventKind::ModelMessageCompleted,
            LoopEventKind::RunCompleted,
        ],
        journal_records: &[
            JournalRecordKind::ModelAttempt,
            JournalRecordKind::Message,
            JournalRecordKind::Run,
        ],
        checkpoint_policy: CheckpointPolicy::Terminal,
        side_effect_policy: SideEffectPolicy::None,
        next_state: LoopState::Completed,
        terminal_result: Some(LoopTerminalResult {
            status: LoopTerminalStatus::Completed,
            stop_reason: LoopStopReason::EndTurn,
        }),
        recovery_classification: None,
    },
    TransitionRule {
        from_state: LoopState::ModelStreaming,
        trigger: LoopTrigger::ProviderFailure,
        guard: TransitionGuard::ProviderFailureClassified,
        events: &[LoopEventKind::ModelAttemptFailed, LoopEventKind::RunFailed],
        journal_records: &[
            JournalRecordKind::ModelAttempt,
            JournalRecordKind::Recovery,
            JournalRecordKind::Run,
        ],
        checkpoint_policy: CheckpointPolicy::Terminal,
        side_effect_policy: SideEffectPolicy::ReconcileRequired,
        next_state: LoopState::Failed,
        terminal_result: Some(LoopTerminalResult {
            status: LoopTerminalStatus::Failed,
            stop_reason: LoopStopReason::ProviderFailure,
        }),
        recovery_classification: None,
    },
    TransitionRule {
        from_state: LoopState::StreamIntervention,
        trigger: LoopTrigger::StreamStopRun,
        guard: TransitionGuard::RuleActionAllowed,
        events: &[
            LoopEventKind::StreamInterventionApplied,
            LoopEventKind::RunCompleted,
        ],
        journal_records: &[JournalRecordKind::StreamRule, JournalRecordKind::Run],
        checkpoint_policy: CheckpointPolicy::Terminal,
        side_effect_policy: SideEffectPolicy::None,
        next_state: LoopState::Completed,
        terminal_result: Some(LoopTerminalResult {
            status: LoopTerminalStatus::Completed,
            stop_reason: LoopStopReason::StreamRuleStop,
        }),
        recovery_classification: None,
    },
    TransitionRule {
        from_state: LoopState::StreamIntervention,
        trigger: LoopTrigger::StreamAbortAndRetry,
        guard: TransitionGuard::RuleActionAllowed,
        events: &[LoopEventKind::StreamInterventionApplied],
        journal_records: &[
            JournalRecordKind::StreamRule,
            JournalRecordKind::ModelAttempt,
        ],
        checkpoint_policy: CheckpointPolicy::After,
        side_effect_policy: SideEffectPolicy::IdempotentRetryAllowed,
        next_state: LoopState::ProviderProjection,
        terminal_result: None,
        recovery_classification: None,
    },
    TransitionRule {
        from_state: LoopState::StreamIntervention,
        trigger: LoopTrigger::StreamPauseForApproval,
        guard: TransitionGuard::RuleActionAllowed,
        events: &[
            LoopEventKind::StreamInterventionApplied,
            LoopEventKind::ApprovalRequested,
        ],
        journal_records: &[JournalRecordKind::StreamRule, JournalRecordKind::Approval],
        checkpoint_policy: CheckpointPolicy::After,
        side_effect_policy: SideEffectPolicy::None,
        next_state: LoopState::Approval,
        terminal_result: None,
        recovery_classification: None,
    },
    TransitionRule {
        from_state: LoopState::StreamIntervention,
        trigger: LoopTrigger::StreamUnsafeIntervention,
        guard: TransitionGuard::UnsafeIntervention,
        events: &[LoopEventKind::RunFailed],
        journal_records: &[JournalRecordKind::StreamRule, JournalRecordKind::Run],
        checkpoint_policy: CheckpointPolicy::Terminal,
        side_effect_policy: SideEffectPolicy::None,
        next_state: LoopState::Failed,
        terminal_result: Some(LoopTerminalResult {
            status: LoopTerminalStatus::Failed,
            stop_reason: LoopStopReason::UnsafeStreamIntervention,
        }),
        recovery_classification: None,
    },
    TransitionRule {
        from_state: LoopState::ToolPlanning,
        trigger: LoopTrigger::PolicyAllow,
        guard: TransitionGuard::PermissionsPass,
        events: &[LoopEventKind::ToolStarted],
        journal_records: &[JournalRecordKind::Tool],
        checkpoint_policy: CheckpointPolicy::Before,
        side_effect_policy: SideEffectPolicy::IntentBeforeEffect,
        next_state: LoopState::ToolExecution,
        terminal_result: None,
        recovery_classification: None,
    },
    TransitionRule {
        from_state: LoopState::ToolPlanning,
        trigger: LoopTrigger::PolicyAsk,
        guard: TransitionGuard::DispatcherAvailableOrEscalationConfigured,
        events: &[LoopEventKind::ApprovalRequested],
        journal_records: &[JournalRecordKind::Approval],
        checkpoint_policy: CheckpointPolicy::After,
        side_effect_policy: SideEffectPolicy::None,
        next_state: LoopState::Approval,
        terminal_result: None,
        recovery_classification: None,
    },
    TransitionRule {
        from_state: LoopState::ToolPlanning,
        trigger: LoopTrigger::PolicyDeny,
        guard: TransitionGuard::DeniedResultAllowed,
        events: &[
            LoopEventKind::ToolApprovalRequired,
            LoopEventKind::ApprovalDenied,
        ],
        journal_records: &[JournalRecordKind::Approval, JournalRecordKind::Tool],
        checkpoint_policy: CheckpointPolicy::After,
        side_effect_policy: SideEffectPolicy::None,
        next_state: LoopState::ToolDenied,
        terminal_result: None,
        recovery_classification: None,
    },
    TransitionRule {
        from_state: LoopState::Approval,
        trigger: LoopTrigger::Approved,
        guard: TransitionGuard::DecisionValid,
        events: &[LoopEventKind::ApprovalResponded, LoopEventKind::ToolStarted],
        journal_records: &[JournalRecordKind::Approval, JournalRecordKind::Tool],
        checkpoint_policy: CheckpointPolicy::Before,
        side_effect_policy: SideEffectPolicy::IntentBeforeEffect,
        next_state: LoopState::ToolExecution,
        terminal_result: None,
        recovery_classification: None,
    },
    TransitionRule {
        from_state: LoopState::Approval,
        trigger: LoopTrigger::ApprovalDenied,
        guard: TransitionGuard::DecisionValid,
        events: &[
            LoopEventKind::ApprovalResponded,
            LoopEventKind::ApprovalDenied,
        ],
        journal_records: &[JournalRecordKind::Approval],
        checkpoint_policy: CheckpointPolicy::After,
        side_effect_policy: SideEffectPolicy::None,
        next_state: LoopState::ToolDenied,
        terminal_result: None,
        recovery_classification: None,
    },
    TransitionRule {
        from_state: LoopState::Approval,
        trigger: LoopTrigger::ApprovalTimeout,
        guard: TransitionGuard::TimeoutElapsed,
        events: &[
            LoopEventKind::ApprovalTimedOut,
            LoopEventKind::ApprovalDenied,
        ],
        journal_records: &[JournalRecordKind::Approval],
        checkpoint_policy: CheckpointPolicy::After,
        side_effect_policy: SideEffectPolicy::None,
        next_state: LoopState::ToolDenied,
        terminal_result: None,
        recovery_classification: None,
    },
    TransitionRule {
        from_state: LoopState::Approval,
        trigger: LoopTrigger::ApprovalTransportFatal,
        guard: TransitionGuard::ApprovalTransportFatal,
        events: &[LoopEventKind::RunFailed],
        journal_records: &[JournalRecordKind::Approval, JournalRecordKind::Recovery],
        checkpoint_policy: CheckpointPolicy::After,
        side_effect_policy: SideEffectPolicy::ReconcileRequired,
        next_state: LoopState::Failed,
        terminal_result: Some(LoopTerminalResult {
            status: LoopTerminalStatus::Failed,
            stop_reason: LoopStopReason::ApprovalTransportFatal,
        }),
        recovery_classification: None,
    },
    TransitionRule {
        from_state: LoopState::Approval,
        trigger: LoopTrigger::StreamApprovalResumed,
        guard: TransitionGuard::DecisionValid,
        events: &[LoopEventKind::ApprovalResponded],
        journal_records: &[JournalRecordKind::Approval],
        checkpoint_policy: CheckpointPolicy::After,
        side_effect_policy: SideEffectPolicy::None,
        next_state: LoopState::ProviderProjection,
        terminal_result: None,
        recovery_classification: None,
    },
    TransitionRule {
        from_state: LoopState::ToolDenied,
        trigger: LoopTrigger::ContinueWithDeniedResult,
        guard: TransitionGuard::DeniedResultAllowed,
        events: &[LoopEventKind::ToolCompleted],
        journal_records: &[JournalRecordKind::Tool],
        checkpoint_policy: CheckpointPolicy::After,
        side_effect_policy: SideEffectPolicy::None,
        next_state: LoopState::Continue,
        terminal_result: None,
        recovery_classification: None,
    },
    TransitionRule {
        from_state: LoopState::ToolDenied,
        trigger: LoopTrigger::FailOnDenied,
        guard: TransitionGuard::DecisionValid,
        events: &[LoopEventKind::RunFailed],
        journal_records: &[JournalRecordKind::Recovery, JournalRecordKind::Run],
        checkpoint_policy: CheckpointPolicy::Terminal,
        side_effect_policy: SideEffectPolicy::None,
        next_state: LoopState::Failed,
        terminal_result: Some(LoopTerminalResult {
            status: LoopTerminalStatus::Failed,
            stop_reason: LoopStopReason::ToolDenied,
        }),
        recovery_classification: None,
    },
    TransitionRule {
        from_state: LoopState::ToolExecution,
        trigger: LoopTrigger::ToolComplete,
        guard: TransitionGuard::TerminalStatusAppended,
        events: &[LoopEventKind::ToolCompleted],
        journal_records: &[JournalRecordKind::Tool],
        checkpoint_policy: CheckpointPolicy::After,
        side_effect_policy: SideEffectPolicy::None,
        next_state: LoopState::Continue,
        terminal_result: None,
        recovery_classification: None,
    },
    TransitionRule {
        from_state: LoopState::ToolExecution,
        trigger: LoopTrigger::ToolInterrupt,
        guard: TransitionGuard::InterruptResumable,
        events: &[LoopEventKind::ToolInterrupted],
        journal_records: &[JournalRecordKind::Tool],
        checkpoint_policy: CheckpointPolicy::After,
        side_effect_policy: SideEffectPolicy::ReconcileRequired,
        next_state: LoopState::Interrupted,
        terminal_result: None,
        recovery_classification: None,
    },
    TransitionRule {
        from_state: LoopState::ToolExecution,
        trigger: LoopTrigger::ToolFailure,
        guard: TransitionGuard::TerminalStatusAppended,
        events: &[LoopEventKind::ToolFailed, LoopEventKind::RunFailed],
        journal_records: &[
            JournalRecordKind::Tool,
            JournalRecordKind::Recovery,
            JournalRecordKind::Run,
        ],
        checkpoint_policy: CheckpointPolicy::Terminal,
        side_effect_policy: SideEffectPolicy::ReconcileRequired,
        next_state: LoopState::Failed,
        terminal_result: Some(LoopTerminalResult {
            status: LoopTerminalStatus::Failed,
            stop_reason: LoopStopReason::ToolFailure,
        }),
        recovery_classification: None,
    },
    TransitionRule {
        from_state: LoopState::Interrupted,
        trigger: LoopTrigger::WaitForResume,
        guard: TransitionGuard::ResumeTokenRequired,
        events: &[LoopEventKind::RunCheckpointed],
        journal_records: &[JournalRecordKind::Recovery],
        checkpoint_policy: CheckpointPolicy::After,
        side_effect_policy: SideEffectPolicy::None,
        next_state: LoopState::WaitingForResume,
        terminal_result: None,
        recovery_classification: None,
    },
    TransitionRule {
        from_state: LoopState::WaitingForResume,
        trigger: LoopTrigger::ResumeAllowed,
        guard: TransitionGuard::CheckpointAndPackageValid,
        events: &[
            LoopEventKind::RunResumeRequested,
            LoopEventKind::ReplayCompleted,
        ],
        journal_records: &[JournalRecordKind::Recovery],
        checkpoint_policy: CheckpointPolicy::After,
        side_effect_policy: SideEffectPolicy::IdempotentRetryAllowed,
        next_state: LoopState::ToolExecution,
        terminal_result: None,
        recovery_classification: None,
    },
    TransitionRule {
        from_state: LoopState::WaitingForResume,
        trigger: LoopTrigger::ResumeDenied,
        guard: TransitionGuard::CheckpointAndPackageValid,
        events: &[LoopEventKind::RunResumeFailed, LoopEventKind::RunFailed],
        journal_records: &[JournalRecordKind::Recovery, JournalRecordKind::Run],
        checkpoint_policy: CheckpointPolicy::Terminal,
        side_effect_policy: SideEffectPolicy::None,
        next_state: LoopState::Failed,
        terminal_result: Some(LoopTerminalResult {
            status: LoopTerminalStatus::Failed,
            stop_reason: LoopStopReason::ResumeDenied,
        }),
        recovery_classification: None,
    },
    TransitionRule {
        from_state: LoopState::Compaction,
        trigger: LoopTrigger::CompactionComplete,
        guard: TransitionGuard::ProtectedContextPreserved,
        events: &[LoopEventKind::ContextCompactionCompleted],
        journal_records: &[JournalRecordKind::Context],
        checkpoint_policy: CheckpointPolicy::After,
        side_effect_policy: SideEffectPolicy::None,
        next_state: LoopState::ContextAssembly,
        terminal_result: None,
        recovery_classification: None,
    },
    TransitionRule {
        from_state: LoopState::Continue,
        trigger: LoopTrigger::ContinueLoop,
        guard: TransitionGuard::BudgetValid,
        events: &[],
        journal_records: &[],
        checkpoint_policy: CheckpointPolicy::None,
        side_effect_policy: SideEffectPolicy::None,
        next_state: LoopState::ContextAssembly,
        terminal_result: None,
        recovery_classification: None,
    },
    TransitionRule {
        from_state: LoopState::Continue,
        trigger: LoopTrigger::MaxIterationsReached {
            outcome: MaxIterationOutcome::Complete,
        },
        guard: TransitionGuard::MaxIterationBudgetExhausted,
        events: &[LoopEventKind::RunCompleted],
        journal_records: &[JournalRecordKind::Run],
        checkpoint_policy: CheckpointPolicy::Terminal,
        side_effect_policy: SideEffectPolicy::None,
        next_state: LoopState::Completed,
        terminal_result: Some(LoopTerminalResult {
            status: LoopTerminalStatus::Completed,
            stop_reason: LoopStopReason::MaxIterations {
                outcome: MaxIterationOutcome::Complete,
            },
        }),
        recovery_classification: None,
    },
    TransitionRule {
        from_state: LoopState::Continue,
        trigger: LoopTrigger::MaxIterationsReached {
            outcome: MaxIterationOutcome::Fail,
        },
        guard: TransitionGuard::MaxIterationBudgetExhausted,
        events: &[LoopEventKind::RunFailed],
        journal_records: &[JournalRecordKind::Run],
        checkpoint_policy: CheckpointPolicy::Terminal,
        side_effect_policy: SideEffectPolicy::None,
        next_state: LoopState::Failed,
        terminal_result: Some(LoopTerminalResult {
            status: LoopTerminalStatus::Failed,
            stop_reason: LoopStopReason::MaxIterations {
                outcome: MaxIterationOutcome::Fail,
            },
        }),
        recovery_classification: None,
    },
    TransitionRule {
        from_state: LoopState::Failed,
        trigger: LoopTrigger::FailureClassified {
            classification: RecoveryClassification::RetryableSafeStep,
        },
        guard: TransitionGuard::RepairPlanSafe,
        events: &[LoopEventKind::RecoveryPlanned],
        journal_records: &[JournalRecordKind::Recovery],
        checkpoint_policy: CheckpointPolicy::After,
        side_effect_policy: SideEffectPolicy::IdempotentRetryAllowed,
        next_state: LoopState::Recovery,
        terminal_result: None,
        recovery_classification: Some(RecoveryClassification::RetryableSafeStep),
    },
    TransitionRule {
        from_state: LoopState::Failed,
        trigger: LoopTrigger::FailureClassified {
            classification: RecoveryClassification::ReconcileRequired,
        },
        guard: TransitionGuard::RepairPlanSafe,
        events: &[LoopEventKind::RecoveryPlanned],
        journal_records: &[JournalRecordKind::Recovery],
        checkpoint_policy: CheckpointPolicy::After,
        side_effect_policy: SideEffectPolicy::ReconcileRequired,
        next_state: LoopState::Recovery,
        terminal_result: None,
        recovery_classification: Some(RecoveryClassification::ReconcileRequired),
    },
    TransitionRule {
        from_state: LoopState::Failed,
        trigger: LoopTrigger::FailureClassified {
            classification: RecoveryClassification::RepairRequired,
        },
        guard: TransitionGuard::RepairPlanSafe,
        events: &[LoopEventKind::RecoveryPlanned],
        journal_records: &[JournalRecordKind::Recovery],
        checkpoint_policy: CheckpointPolicy::After,
        side_effect_policy: SideEffectPolicy::ReconcileRequired,
        next_state: LoopState::Recovery,
        terminal_result: None,
        recovery_classification: Some(RecoveryClassification::RepairRequired),
    },
    TransitionRule {
        from_state: LoopState::Recovery,
        trigger: LoopTrigger::RepairApplied,
        guard: TransitionGuard::InvariantRestored,
        events: &[LoopEventKind::ReplayCompleted],
        journal_records: &[JournalRecordKind::Recovery],
        checkpoint_policy: CheckpointPolicy::After,
        side_effect_policy: SideEffectPolicy::None,
        next_state: LoopState::ContextAssembly,
        terminal_result: None,
        recovery_classification: None,
    },
    TransitionRule {
        from_state: LoopState::Recovery,
        trigger: LoopTrigger::RepairCompletedTerminal,
        guard: TransitionGuard::InvariantRestored,
        events: &[LoopEventKind::ReplayCompleted, LoopEventKind::RunCompleted],
        journal_records: &[JournalRecordKind::Recovery, JournalRecordKind::Run],
        checkpoint_policy: CheckpointPolicy::Terminal,
        side_effect_policy: SideEffectPolicy::None,
        next_state: LoopState::Completed,
        terminal_result: Some(LoopTerminalResult {
            status: LoopTerminalStatus::Completed,
            stop_reason: LoopStopReason::RecoveryCompleted,
        }),
        recovery_classification: None,
    },
    TransitionRule {
        from_state: LoopState::Recovery,
        trigger: LoopTrigger::RecoveryIrrecoverable,
        guard: TransitionGuard::RepairPlanSafe,
        events: &[LoopEventKind::RunFailed],
        journal_records: &[JournalRecordKind::Recovery, JournalRecordKind::Run],
        checkpoint_policy: CheckpointPolicy::Terminal,
        side_effect_policy: SideEffectPolicy::None,
        next_state: LoopState::Failed,
        terminal_result: Some(LoopTerminalResult {
            status: LoopTerminalStatus::Failed,
            stop_reason: LoopStopReason::RecoveryIrrecoverable,
        }),
        recovery_classification: Some(RecoveryClassification::Irrecoverable),
    },
    cancel_rule(LoopState::Starting),
    cancel_rule(LoopState::ContextAssembly),
    cancel_rule(LoopState::ProviderProjection),
    cancel_rule(LoopState::ModelStreaming),
    cancel_rule(LoopState::StreamIntervention),
    cancel_rule(LoopState::ToolPlanning),
    cancel_rule(LoopState::Approval),
    cancel_rule(LoopState::ToolDenied),
    cancel_rule(LoopState::ToolExecution),
    cancel_rule(LoopState::Interrupted),
    cancel_rule(LoopState::WaitingForResume),
    cancel_rule(LoopState::Compaction),
    cancel_rule(LoopState::Continue),
    cancel_rule(LoopState::Recovery),
    cancel_rule(LoopState::Failed),
];

const fn cancel_rule(from_state: LoopState) -> TransitionRule {
    TransitionRule {
        from_state,
        trigger: LoopTrigger::CancelRequested,
        guard: TransitionGuard::CancellationRequested,
        events: &[
            LoopEventKind::RunCancelRequested,
            LoopEventKind::RunCancelled,
        ],
        journal_records: &[JournalRecordKind::Recovery, JournalRecordKind::Run],
        checkpoint_policy: CheckpointPolicy::Terminal,
        side_effect_policy: SideEffectPolicy::ReconcileRequired,
        next_state: LoopState::Cancelled,
        terminal_result: Some(LoopTerminalResult {
            status: LoopTerminalStatus::Cancelled,
            stop_reason: LoopStopReason::Cancelled,
        }),
        recovery_classification: None,
    }
}