meerkat-runtime 0.8.23

v9 runtime control-plane for Meerkat agent lifecycle
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
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
//! Bounded, typed dispatch contract for runtime submissions.
//!
//! A host that hands work to the runtime needs three things the raw accept
//! surface does not give it:
//!
//! 1. a bound it supplies itself, so an admission that never answers cannot
//!    masquerade as normal operation (an `await` that never returns never
//!    raises, so a caller's bounded-retry handler never fires);
//! 2. a typed outcome for every fate - durable acceptance, typed refusal, or a
//!    typed timeout that says whether the work is durably queued (retry is
//!    safe) or unknown (retry is not);
//! 3. a mandatory idempotency key, so nobody can add a retry without first
//!    deciding what makes the delivery unique.
//!
//! The third point is not optional garnish. A bounded wait *without* an
//! idempotency seam creates a strictly worse failure than the hang it removes:
//! a slow-but-successful delivery whose caller's bound expired, retried, and
//! delivered twice. The bound here is on the CALLER'S OBSERVATION, never on the
//! work: an expired bound never cancels an in-flight admission, and the
//! mandatory key means a retry collapses at the durable admission point instead
//! of executing a second time.
//!
//! # Scope
//!
//! This contract is admission-scoped. Its success arm means "the runtime
//! admitted the work and it will run", not "the turn finished". Turn results
//! are served by
//! [`SessionServiceRuntimeExt::input_terminal_completion`] and
//! [`SessionServiceRuntimeExt::interaction_terminal_status`].
//!
//! # Collapse guarantee, stated honestly
//!
//! Collapse is owned by the generated admission machine
//! (`ResolveAdmissionIdempotency` / `RegisterAcceptedIdempotency`) plus, on a
//! persistent runtime, the store-owned input index that the persistent driver
//! consults before the live map. That combination is what makes a duplicate
//! submission collapse rather than execute twice.
//!
//! On a runtime with no store, collapse holds only while the admission is
//! retained in live machine state: the machine drops an input's key binding
//! when the terminal input is archived, and nothing survives a restart. That is
//! why [`AdmissionDurability`] and [`SubmitTimeoutDisposition`] are separate
//! typed facts instead of an assumed guarantee.
//!
//! # A durable row is not the same fact as work that will run
//!
//! The key binding outlives the work: a cancelled, abandoned, superseded, or
//! coalesced input keeps its committed row and stays indexed under its key
//! forever, and a later submission under that key collapses onto it. Reporting
//! that as "durably queued" would re-create the very failure this contract
//! exists to remove - a host marking its message dispatched and waiting for a
//! reply that is never coming. So every outcome that names an input also
//! carries [`AdmittedWorkState`], resolved by the same generated MeerkatMachine
//! authority every other surface publishes input state with, and
//! [`BoundedSubmitReport::is_durably_queued`] is true only for work that is
//! both durable AND still owed a run.

use std::sync::Arc;
use std::time::Duration;

use meerkat_core::lifecycle::InputId;
use meerkat_core::types::SessionId;

use crate::accept::{AcceptOutcome, RejectReason};
use crate::identifiers::IdempotencyKey;
use crate::input::Input;
use crate::input_state::InputStateSeed;
use crate::meerkat_machine::dsl::InputPublicTerminalOutcome;
use crate::service_ext::SessionServiceRuntimeExt;
use crate::traits::RuntimeDriverError;

/// Bound applied when a caller supplies none.
///
/// Matches the transport budget the runtime already applies to a peer bridge
/// request, so a host that does not choose gets the same order of magnitude the
/// rest of the runtime already treats as "too long to still be waiting".
pub const DEFAULT_SUBMIT_BOUND: Duration = Duration::from_secs(30);

/// Longest slice of a caller's bound reserved for classifying an expiry.
const MAX_EVIDENCE_READ_BOUND: Duration = Duration::from_secs(5);

/// Shortest classification slice. A caller may ask for a bound too small to
/// carve a useful slice out of; classification still gets this much, so a tiny
/// bound returns an evidence-backed disposition instead of a reflexive
/// "unknown".
const MIN_EVIDENCE_READ_BOUND: Duration = Duration::from_millis(100);

/// Caller-supplied bound on how long a submission may go unanswered.
///
/// There is deliberately no unbounded variant: an unbounded submit is the
/// defect this type exists to remove.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct SubmitBound(Duration);

impl SubmitBound {
    /// Bound this submission by `bound`.
    ///
    /// A zero bound is legal and means "answer from durable evidence, do not
    /// wait": the admission is still started and still runs, but the caller
    /// never observes it, so the outcome is always
    /// [`BoundedSubmitOutcome::TimedOut`] classified from the store-owned
    /// index alone - including when the admission would have answered
    /// instantly. A host that computes its bound must therefore not treat a
    /// zero-bound timeout as a runtime fault.
    #[must_use]
    pub const fn after(bound: Duration) -> Self {
        Self(bound)
    }

    /// The caller-supplied duration.
    #[must_use]
    pub const fn as_duration(self) -> Duration {
        self.0
    }

    /// Split the bound into the slice the admission may consume and the slice
    /// reserved for classifying an expiry.
    ///
    /// The classification slice is a quarter of the bound, floored at
    /// [`MIN_EVIDENCE_READ_BOUND`] and capped at [`MAX_EVIDENCE_READ_BOUND`];
    /// the admission gets whatever remains. Only a bound below that floor
    /// cannot fund both, and such a call still returns within
    /// [`MIN_EVIDENCE_READ_BOUND`] in total. At or above the floor the two
    /// slices always sum to exactly the caller's bound.
    fn split(self) -> (Duration, Duration) {
        let evidence = (self.0 / 4).clamp(MIN_EVIDENCE_READ_BOUND, MAX_EVIDENCE_READ_BOUND);
        (self.0.saturating_sub(evidence), evidence)
    }
}

impl Default for SubmitBound {
    fn default() -> Self {
        Self(DEFAULT_SUBMIT_BOUND)
    }
}

/// One retryable hand-off to the runtime.
///
/// The idempotency key is not optional. A submission that can be retried must
/// name what makes it unique, at the point the retry is introduced rather than
/// after a duplicate reaches a recipient.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BoundedSubmission {
    idempotency_key: IdempotencyKey,
    bound: SubmitBound,
}

impl BoundedSubmission {
    /// Submit under `idempotency_key` with the documented default bound.
    #[must_use]
    pub fn new(idempotency_key: IdempotencyKey) -> Self {
        Self {
            idempotency_key,
            bound: SubmitBound::default(),
        }
    }

    /// Replace the default bound with a caller-supplied one.
    #[must_use]
    pub const fn with_bound(mut self, bound: SubmitBound) -> Self {
        self.bound = bound;
        self
    }

    /// The key this submission collapses on.
    #[must_use]
    pub const fn idempotency_key(&self) -> &IdempotencyKey {
        &self.idempotency_key
    }

    /// The bound this submission is observed under.
    #[must_use]
    pub const fn bound(&self) -> SubmitBound {
        self.bound
    }
}

/// Whether an admission survives a process restart.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum AdmissionDurability {
    /// A committed row for this key exists in the runtime's store-owned input
    /// index. The admission survives restart and a retry under the same key
    /// collapses onto it even on a fresh process.
    Durable,
    /// The runtime admitted the work, but no durable witness is observable:
    /// either the runtime retains nothing across restart, or its index could
    /// not answer. The work runs in this process; a retry under the same key
    /// collapses only while the admission is retained in live machine state.
    ProcessLocalOnly,
}

/// What generated MeerkatMachine authority says about the work behind one
/// admitted input right now.
///
/// Durability answers "does this survive a restart"; this answers "is anything
/// still going to happen". They are independent: the terminal row of a
/// cancelled input is durable and dead at the same time.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum AdmittedWorkState {
    /// Not terminal: the runtime still owes this input a run, so a caller
    /// waiting on its reply is waiting for something still coming.
    Pending,
    /// Terminal because a run consumed it: the work happened. A retry under
    /// the same key collapses onto the finished row and changes nothing.
    Delivered,
    /// Terminal without this input ever being consumed by a run. Nothing more
    /// will happen for it, and a retry under the SAME key collapses onto this
    /// same dead row instead of re-running the work: a host that still needs
    /// the work done must submit it under a NEW key.
    ///
    /// `outcome` keeps the sub-cases the machine distinguishes rather than
    /// flattening them: `Superseded` and `Coalesced` mean a successor input
    /// carries this work, while `Cancelled` and `Abandoned` mean nothing does.
    TerminalWithoutDelivery { outcome: InputPublicTerminalOutcome },
    /// The machine declined to classify this input's seed, so neither "still
    /// coming" nor "already terminal" is proven. Treated as not-queued
    /// everywhere: a row nobody can classify must never read as a live claim.
    Unclassified,
}

impl AdmittedWorkState {
    /// Whether the runtime still owes this input a run.
    #[must_use]
    pub const fn is_pending(self) -> bool {
        matches!(self, Self::Pending)
    }

    /// Whether this input reached a terminal state without ever running.
    #[must_use]
    pub const fn is_terminal_without_delivery(self) -> bool {
        matches!(self, Self::TerminalWithoutDelivery { .. })
    }
}

/// Why a submission's fate is unknown after its bound expired.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum SubmitUnknownCause {
    /// The store-owned index held no committed row for this key.
    NoDurableWitness,
    /// The durable evidence read failed or could not answer in time, so the
    /// absence of a witness is not evidence of absence.
    EvidenceUnavailable,
}

/// What durable evidence says about work whose submission went unanswered.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum SubmitTimeoutDisposition {
    /// A committed durable row exists for this key, and `state` says what the
    /// machine makes of it: still owed a run, already run, or terminal without
    /// ever running. Retrying under the same key is safe (it collapses onto
    /// this row) but changes nothing, in any of the three cases.
    DurablyAdmitted {
        input_id: InputId,
        state: AdmittedWorkState,
    },
    /// No durable witness was observable. The submission may still commit, may
    /// have been refused, or may never have reached the runtime. Retry only
    /// under the SAME idempotency key, and note that exactly-once is not
    /// guaranteed if this process dies before the admission commits.
    Unknown { cause: SubmitUnknownCause },
}

/// Why a submission produced no definite admission answer.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum SubmitTimeoutCause {
    /// The caller-supplied bound expired first. The admission was NOT
    /// cancelled: the bound is on the caller's observation, never on the work.
    BoundExpired,
    /// The runtime failed without a definite admission answer, which leaves
    /// this submission in the same unknown state a bound expiry does.
    RuntimeIndeterminate { error: RuntimeDriverError },
}

/// Typed reason a submission was declined.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum SubmitRefusal {
    /// The input already carried a different idempotency key, so the
    /// submission has no single unique identity. Nothing was sent to the
    /// runtime.
    IdempotencyKeyConflict {
        supplied: IdempotencyKey,
        carried: IdempotencyKey,
    },
    /// The runtime rejected the input at its admission boundary and named the
    /// reason.
    Admission { reason: RejectReason },
    /// The runtime declined the submission before admission. Nothing was
    /// admitted by this call.
    Runtime { error: RuntimeDriverError },
}

/// Total typed fate of one bounded submission.
///
/// Every path through [`submit_bounded`] lands on exactly one of these; there
/// is no untyped escape hatch and no unbounded wait.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum BoundedSubmitOutcome {
    /// The runtime admitted the work within the bound. `state` is what the
    /// machine says about it: normally [`AdmittedWorkState::Pending`], but an
    /// admission can land already terminal, and this reports that rather than
    /// promising a run.
    Admitted {
        input_id: InputId,
        durability: AdmissionDurability,
        state: AdmittedWorkState,
    },
    /// This submission collapsed onto an admission already made under the same
    /// idempotency key. There is exactly one admitted input, and therefore at
    /// most one execution; which call created it is not distinguished because
    /// it does not change that fact.
    ///
    /// `state` is the half a bare collapse cannot tell you: the key outlives
    /// the work, so the row collapsed onto may be pending, already run, or
    /// terminal without ever having run.
    Collapsed {
        input_id: InputId,
        durability: AdmissionDurability,
        state: AdmittedWorkState,
    },
    /// The runtime declined the work. Nothing was admitted by this call.
    Refused { reason: SubmitRefusal },
    /// The submission went unanswered. `disposition` states what durable
    /// evidence says about the work right now.
    TimedOut {
        cause: SubmitTimeoutCause,
        disposition: SubmitTimeoutDisposition,
    },
}

/// One bounded submission and what came of it.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct BoundedSubmitReport {
    /// The key this submission collapsed on.
    pub idempotency_key: IdempotencyKey,
    /// The bound the caller supplied (or the documented default).
    pub bound: SubmitBound,
    /// The typed fate.
    pub outcome: BoundedSubmitOutcome,
}

impl BoundedSubmitReport {
    /// The input the runtime is holding for this key, when one is known.
    ///
    /// `None` for refusals and for a timeout with no durable witness.
    #[must_use]
    pub const fn admitted_input_id(&self) -> Option<&InputId> {
        match &self.outcome {
            BoundedSubmitOutcome::Admitted { input_id, .. }
            | BoundedSubmitOutcome::Collapsed { input_id, .. }
            | BoundedSubmitOutcome::TimedOut {
                disposition: SubmitTimeoutDisposition::DurablyAdmitted { input_id, .. },
                ..
            } => Some(input_id),
            BoundedSubmitOutcome::Refused { .. } | BoundedSubmitOutcome::TimedOut { .. } => None,
        }
    }

    /// What generated machine authority says about the work behind this key.
    ///
    /// `None` when there is no input to say anything about: a refusal, or a
    /// timeout with no durable witness.
    #[must_use]
    pub const fn work_state(&self) -> Option<AdmittedWorkState> {
        match &self.outcome {
            BoundedSubmitOutcome::Admitted { state, .. }
            | BoundedSubmitOutcome::Collapsed { state, .. }
            | BoundedSubmitOutcome::TimedOut {
                disposition: SubmitTimeoutDisposition::DurablyAdmitted { state, .. },
                ..
            } => Some(*state),
            BoundedSubmitOutcome::Refused { .. } | BoundedSubmitOutcome::TimedOut { .. } => None,
        }
    }

    /// Whether the runtime is durably holding work that still owes a run.
    ///
    /// Both halves are load-bearing. Durability comes from the store-owned
    /// index, so the work survives a restart; the state comes from machine
    /// authority, so a terminal row - completed, cancelled, abandoned,
    /// superseded, coalesced - answers false. A durable row for a cancelled
    /// input is exactly the "accepted, never moves" shape, and answering true
    /// for one is how a host is told to stop waiting on nothing.
    ///
    /// False is therefore not one situation but three, and they need opposite
    /// responses: work that already ran (do nothing), work that will never run
    /// (resubmit under a NEW key), and an unknown fate (retry under the SAME
    /// key). Branch on [`Self::work_state`] before acting on a false here;
    /// treating them alike either loses the work or delivers it twice.
    #[must_use]
    pub const fn is_durably_queued(&self) -> bool {
        match &self.outcome {
            BoundedSubmitOutcome::Admitted {
                durability, state, ..
            }
            | BoundedSubmitOutcome::Collapsed {
                durability, state, ..
            } => matches!(durability, AdmissionDurability::Durable) && state.is_pending(),
            BoundedSubmitOutcome::TimedOut { disposition, .. } => matches!(
                disposition,
                SubmitTimeoutDisposition::DurablyAdmitted {
                    state: AdmittedWorkState::Pending,
                    ..
                }
            ),
            BoundedSubmitOutcome::Refused { .. } => false,
        }
    }

    /// Whether this submission resolved onto an input that reached a terminal
    /// state without ever running.
    ///
    /// The host-visible consequence: no reply is coming, and retrying under
    /// this same idempotency key collapses onto the same dead row. Work that
    /// still has to happen needs a new key.
    #[must_use]
    pub const fn is_terminal_without_delivery(&self) -> bool {
        matches!(
            self.work_state(),
            Some(AdmittedWorkState::TerminalWithoutDelivery { .. })
        )
    }
}

/// Submit `input` under a caller-supplied bound and a mandatory idempotency
/// key, and return a typed fate for every path.
///
/// The bound is on the caller's observation, never on the work. The admission
/// runs on its own task, so an expired bound cannot drop-cancel a durable
/// admission mid-commit (which would strand the runtime's own compensation) and
/// cannot turn a slow-but-successful delivery into a lost one. A retry after an
/// expired bound must reuse the same [`BoundedSubmission::idempotency_key`]; it
/// then collapses at the durable admission point instead of delivering twice.
///
/// The call returns within [`BoundedSubmission::bound`], except for a bound
/// below [`MIN_EVIDENCE_READ_BOUND`], which cannot fund the classification
/// slice and so returns within [`MIN_EVIDENCE_READ_BOUND`] instead.
#[must_use = "a bounded submission's typed fate is the point of calling it"]
pub async fn submit_bounded<R>(
    runtime: Arc<R>,
    session_id: &SessionId,
    input: Input,
    submission: BoundedSubmission,
) -> BoundedSubmitReport
where
    R: SessionServiceRuntimeExt + ?Sized + 'static,
{
    let BoundedSubmission {
        idempotency_key,
        bound,
    } = submission;

    let input = match stamp_idempotency_key(input, &idempotency_key) {
        Ok(input) => input,
        Err(carried) => {
            let outcome = BoundedSubmitOutcome::Refused {
                reason: SubmitRefusal::IdempotencyKeyConflict {
                    supplied: idempotency_key.clone(),
                    carried,
                },
            };
            log_submit_outcome(session_id, &idempotency_key, bound, &outcome);
            return BoundedSubmitReport {
                idempotency_key,
                bound,
                outcome,
            };
        }
    };

    let (admission_bound, evidence_bound) = bound.split();
    let mut admission = {
        let runtime = Arc::clone(&runtime);
        let admitting_session_id = session_id.clone();
        crate::tokio::spawn(async move { runtime.accept_input(&admitting_session_id, input).await })
    };

    // A zero admission slice means the caller asked not to wait at all, and it
    // is answered without touching the timer. Racing a zero-length timer
    // against a task that may or may not have been scheduled yet would make a
    // "do not wait" call report whichever won, which is a scheduling detail no
    // caller can act on.
    if admission_bound.is_zero() {
        observe_abandoned_admission(admission, session_id.clone(), idempotency_key.clone());
        let outcome = BoundedSubmitOutcome::TimedOut {
            cause: SubmitTimeoutCause::BoundExpired,
            disposition: timeout_disposition(
                durable_evidence(
                    runtime.as_ref(),
                    session_id,
                    &idempotency_key,
                    evidence_bound,
                )
                .await,
            ),
        };
        log_submit_outcome(session_id, &idempotency_key, bound, &outcome);
        return BoundedSubmitReport {
            idempotency_key,
            bound,
            outcome,
        };
    }

    // The bound is observed through `&mut`, so an expiry leaves the handle
    // intact: the task is never aborted, and its answer stays reachable for an
    // observer instead of being dropped on the floor.
    let outcome = match crate::tokio::time::timeout(admission_bound, &mut admission).await {
        Ok(Ok(Ok(accepted))) => {
            classify_admission(
                runtime.as_ref(),
                session_id,
                &idempotency_key,
                evidence_bound,
                accepted,
            )
            .await
        }
        Ok(Ok(Err(error))) => {
            classify_admission_error(
                runtime.as_ref(),
                session_id,
                &idempotency_key,
                evidence_bound,
                error,
            )
            .await
        }
        Ok(Err(join_error)) => {
            classify_admission_error(
                runtime.as_ref(),
                session_id,
                &idempotency_key,
                evidence_bound,
                RuntimeDriverError::Internal(format!(
                    "runtime admission task did not complete: {join_error}"
                )),
            )
            .await
        }
        Err(_elapsed) => {
            observe_abandoned_admission(admission, session_id.clone(), idempotency_key.clone());
            BoundedSubmitOutcome::TimedOut {
                cause: SubmitTimeoutCause::BoundExpired,
                disposition: timeout_disposition(
                    durable_evidence(
                        runtime.as_ref(),
                        session_id,
                        &idempotency_key,
                        evidence_bound,
                    )
                    .await,
                ),
            }
        }
    };

    log_submit_outcome(session_id, &idempotency_key, bound, &outcome);
    BoundedSubmitReport {
        idempotency_key,
        bound,
        outcome,
    }
}

/// Watch an admission whose caller stopped observing.
///
/// The caller was answered at its bound and will never look at this task
/// again, so without an observer a slow admission that eventually FAILS
/// produces no log line, no event, and no metric: the work was never admitted
/// and nobody in the process can see that. Silence is the failure mode this
/// contract exists to remove, so the fate is logged even though no caller
/// wants it.
fn observe_abandoned_admission(
    admission: crate::tokio::task::JoinHandle<Result<AcceptOutcome, RuntimeDriverError>>,
    session_id: SessionId,
    idempotency_key: IdempotencyKey,
) {
    crate::tokio::spawn(async move {
        match admission.await {
            Ok(Ok(outcome)) => tracing::debug!(
                %session_id,
                %idempotency_key,
                ?outcome,
                "runtime admission answered after the caller's bound expired"
            ),
            Ok(Err(error)) => tracing::error!(
                %session_id,
                %idempotency_key,
                %error,
                "runtime admission failed after the caller's bound expired; the work was never \
                 admitted and no caller is waiting for this answer"
            ),
            Err(join_error) => tracing::error!(
                %session_id,
                %idempotency_key,
                %join_error,
                "runtime admission task ended without an answer after the caller's bound expired; \
                 whether the work was admitted is unknown"
            ),
        }
    });
}

/// Emit one line per bounded submission, loudest for the fates a host cannot
/// otherwise see.
///
/// A typed outcome only helps the caller that received it. The states that
/// stranded the field event - no definite answer, an expired bound, and a key
/// that resolves onto work which will never run - are the ones an operator has
/// to be able to find afterwards without the caller's cooperation.
fn log_submit_outcome(
    session_id: &SessionId,
    idempotency_key: &IdempotencyKey,
    bound: SubmitBound,
    outcome: &BoundedSubmitOutcome,
) {
    let bound_ms = bound.as_duration().as_millis();
    match outcome {
        BoundedSubmitOutcome::TimedOut {
            cause: SubmitTimeoutCause::RuntimeIndeterminate { error },
            disposition,
        } => tracing::error!(
            %session_id,
            %idempotency_key,
            bound_ms,
            ?disposition,
            %error,
            "bounded submit got no definite admission answer"
        ),
        BoundedSubmitOutcome::TimedOut { cause, disposition } => tracing::warn!(
            %session_id,
            %idempotency_key,
            bound_ms,
            ?cause,
            ?disposition,
            "bounded submit exceeded the caller-supplied bound without an admission answer"
        ),
        BoundedSubmitOutcome::Admitted {
            input_id,
            durability,
            state,
        }
        | BoundedSubmitOutcome::Collapsed {
            input_id,
            durability,
            state,
        } => match state {
            AdmittedWorkState::Pending | AdmittedWorkState::Delivered => tracing::debug!(
                %session_id,
                %idempotency_key,
                %input_id,
                ?durability,
                ?state,
                "bounded submit resolved to an admitted input"
            ),
            AdmittedWorkState::TerminalWithoutDelivery { .. } => {
                tracing::warn!(
                    %session_id,
                    %idempotency_key,
                    %input_id,
                    ?durability,
                    ?state,
                    "bounded submit resolved onto an input that will never run; a retry under \
                     this key collapses onto the same row, so work that still has to happen \
                     needs a new key"
                );
            }
            // Deliberately not the line above: an unclassifiable seed is an
            // unknown fate, not a dead one, and a log that says otherwise is
            // the same over-claim in a different place.
            AdmittedWorkState::Unclassified => {
                tracing::warn!(
                    %session_id,
                    %idempotency_key,
                    %input_id,
                    ?durability,
                    "bounded submit resolved onto an input the machine could not classify; \
                     whether its work is still coming is unknown"
                );
            }
        },
        BoundedSubmitOutcome::Refused { reason } => tracing::debug!(
            %session_id,
            %idempotency_key,
            ?reason,
            "bounded submit was refused"
        ),
    }
}

/// Durable evidence for one key, read from the store-owned input index.
#[derive(Debug, Clone, PartialEq, Eq)]
enum DurableEvidence {
    /// A committed row exists, classified by machine authority. The row alone
    /// proves durability; only its state says whether anything will happen.
    Committed {
        input_id: InputId,
        state: AdmittedWorkState,
    },
    /// The index answered and held no row.
    Absent,
    /// The index could not answer, so absence proves nothing.
    Unavailable,
}

/// Classify one machine-owned input seed through generated authority.
///
/// This is the same generated projection every other surface publishes input
/// state with, so the bounded contract cannot drift from what the machine says,
/// and the cancelled-vs-abandoned distinction stays owned by the DSL rather
/// than re-derived here.
///
/// A projection failure means the seed's own facts are inconsistent - a
/// terminal phase with no terminal kind, say. That is reported loudly and
/// under-claimed as [`AdmittedWorkState::Unclassified`] rather than guessed in
/// either direction.
fn classify_work_state(input_id: &InputId, seed: &InputStateSeed) -> AdmittedWorkState {
    match crate::meerkat_machine::resolve_input_public_state_projection(input_id, seed) {
        Ok(projection) => match projection.terminal_outcome {
            None => AdmittedWorkState::Pending,
            Some(InputPublicTerminalOutcome::Completed) => AdmittedWorkState::Delivered,
            Some(outcome) => AdmittedWorkState::TerminalWithoutDelivery { outcome },
        },
        Err(error) => {
            tracing::error!(
                %input_id,
                %error,
                "MeerkatMachine could not classify an admitted input; whether its work is still \
                 coming is unknown"
            );
            AdmittedWorkState::Unclassified
        }
    }
}

/// Read durable evidence without ever failing the caller.
///
/// A classifier that propagates its own read error would hand the host back an
/// untyped fate at exactly the moment the contract exists to prevent one. An
/// uncertain or corrupt index is what [`DurableEvidence::Unavailable`] is for.
async fn durable_evidence<R>(
    runtime: &R,
    session_id: &SessionId,
    idempotency_key: &IdempotencyKey,
    bound: Duration,
) -> DurableEvidence
where
    R: SessionServiceRuntimeExt + ?Sized,
{
    match crate::tokio::time::timeout(
        bound,
        runtime.durable_input_state_by_idempotency_key(session_id, idempotency_key.0.as_str()),
    )
    .await
    {
        Ok(Ok(Some(stored))) => DurableEvidence::Committed {
            state: classify_work_state(&stored.state.input_id, &stored.seed),
            input_id: stored.state.input_id,
        },
        Ok(Ok(None)) => DurableEvidence::Absent,
        Ok(Err(_)) | Err(_) => DurableEvidence::Unavailable,
    }
}

fn admission_durability(evidence: &DurableEvidence) -> AdmissionDurability {
    match evidence {
        DurableEvidence::Committed { .. } => AdmissionDurability::Durable,
        DurableEvidence::Absent | DurableEvidence::Unavailable => {
            AdmissionDurability::ProcessLocalOnly
        }
    }
}

fn timeout_disposition(evidence: DurableEvidence) -> SubmitTimeoutDisposition {
    match evidence {
        DurableEvidence::Committed { input_id, state } => {
            SubmitTimeoutDisposition::DurablyAdmitted { input_id, state }
        }
        DurableEvidence::Absent => SubmitTimeoutDisposition::Unknown {
            cause: SubmitUnknownCause::NoDurableWitness,
        },
        DurableEvidence::Unavailable => SubmitTimeoutDisposition::Unknown {
            cause: SubmitUnknownCause::EvidenceUnavailable,
        },
    }
}

async fn classify_admission<R>(
    runtime: &R,
    session_id: &SessionId,
    idempotency_key: &IdempotencyKey,
    evidence_bound: Duration,
    accepted: AcceptOutcome,
) -> BoundedSubmitOutcome
where
    R: SessionServiceRuntimeExt + ?Sized,
{
    match accepted {
        AcceptOutcome::Accepted {
            input_id, ref seed, ..
        } => {
            let evidence =
                durable_evidence(runtime, session_id, idempotency_key, evidence_bound).await;
            BoundedSubmitOutcome::Admitted {
                durability: admission_durability(&evidence),
                state: classify_work_state(&input_id, seed),
                input_id,
            }
        }
        // The retained receipt, not a fresh read: a persistent runtime may
        // already have archived a terminal collapse target out of live state,
        // and this seed is the exact fact the admission decided on.
        AcceptOutcome::Deduplicated {
            existing_id,
            ref existing_seed,
            ..
        } => {
            let evidence =
                durable_evidence(runtime, session_id, idempotency_key, evidence_bound).await;
            BoundedSubmitOutcome::Collapsed {
                durability: admission_durability(&evidence),
                state: classify_work_state(&existing_id, existing_seed),
                input_id: existing_id,
            }
        }
        AcceptOutcome::Rejected { reason } => BoundedSubmitOutcome::Refused {
            reason: SubmitRefusal::Admission { reason },
        },
    }
}

/// Classify a failed admission against durable evidence FIRST.
///
/// An error raised after a durable claim already exists must never be reported
/// as a refusal: that is the restart-retry case, where the host resubmits under
/// the same key against a runtime that has since been torn down and the
/// committed row is the answer it needs.
///
/// The row's state is carried through for the same reason it is everywhere
/// else. A post-admission failure whose own compensation terminalized the row
/// it just wrote leaves committed evidence behind; reporting that as queued
/// work would tell the host to wait for a reply its runtime already gave up
/// on.
async fn classify_admission_error<R>(
    runtime: &R,
    session_id: &SessionId,
    idempotency_key: &IdempotencyKey,
    evidence_bound: Duration,
    error: RuntimeDriverError,
) -> BoundedSubmitOutcome
where
    R: SessionServiceRuntimeExt + ?Sized,
{
    let evidence = durable_evidence(runtime, session_id, idempotency_key, evidence_bound).await;
    if let DurableEvidence::Committed { input_id, state } = evidence {
        return BoundedSubmitOutcome::Collapsed {
            input_id,
            durability: AdmissionDurability::Durable,
            state,
        };
    }
    if is_definite_refusal(&error) {
        return BoundedSubmitOutcome::Refused {
            reason: SubmitRefusal::Runtime { error },
        };
    }
    BoundedSubmitOutcome::TimedOut {
        cause: SubmitTimeoutCause::RuntimeIndeterminate { error },
        disposition: timeout_disposition(evidence),
    }
}

/// Whether this error means the runtime evaluated the request and declined it
/// without attempting admission.
///
/// Conservative on purpose: only the pre-admission gates qualify. Everything
/// else (internal faults, recovery states, in-progress sagas whose own
/// documentation tells callers to retry and join) leaves the submission's fate
/// genuinely unknown, and must not be dressed up as a decision.
fn is_definite_refusal(error: &RuntimeDriverError) -> bool {
    matches!(
        error,
        RuntimeDriverError::ValidationFailed { .. }
            | RuntimeDriverError::NotReady { .. }
            | RuntimeDriverError::NotFound { .. }
            | RuntimeDriverError::Destroyed
            | RuntimeDriverError::StaleAuthority { .. }
    )
}

/// Stamp the caller-supplied key, or report the one already carried.
///
/// An input that already names a different key has two candidate identities and
/// therefore no single one; collapsing on either would be a guess.
fn stamp_idempotency_key(
    mut input: Input,
    idempotency_key: &IdempotencyKey,
) -> Result<Input, IdempotencyKey> {
    let header = input.header_mut();
    match &header.idempotency_key {
        Some(carried) if carried != idempotency_key => Err(carried.clone()),
        Some(_) => Ok(input),
        None => {
            header.idempotency_key = Some(idempotency_key.clone());
            Ok(input)
        }
    }
}

#[cfg(test)]
#[allow(clippy::expect_used, clippy::unwrap_used)]
mod tests {
    use std::sync::Arc;
    use std::sync::atomic::{AtomicUsize, Ordering};
    use std::time::Duration;

    use chrono::Utc;
    use meerkat_core::lifecycle::{InputId, RunId};
    use meerkat_core::types::SessionId;

    use super::*;
    use crate::input::{InputDurability, InputHeader, InputOrigin, InputVisibility, PromptInput};
    use crate::input_state::{
        InputAbandonReason, InputLifecycleState, InputState, InputStateSeed, InputTerminalOutcome,
        StoredInputState,
    };
    use crate::runtime_state::RuntimeState;

    fn prompt(text: &str) -> Input {
        Input::Prompt(PromptInput {
            header: InputHeader {
                id: InputId::new(),
                timestamp: Utc::now(),
                source: InputOrigin::Operator,
                durability: InputDurability::Durable,
                visibility: InputVisibility::default(),
                idempotency_key: None,
                supersession_key: None,
                correlation_id: None,
            },
            content: meerkat_core::types::ContentInput::Text(text.to_string()),
            typed_turn_appends: Vec::new(),
            injected_context: Vec::new(),
            turn_metadata: None,
        })
    }

    fn stored(input_id: &InputId) -> StoredInputState {
        StoredInputState {
            state: InputState::new_accepted(input_id.clone()),
            seed: InputStateSeed::new_accepted(),
        }
    }

    /// A committed row whose work is over: the shape a key keeps pointing at
    /// long after anything is going to happen for it.
    fn stored_terminal(
        input_id: &InputId,
        phase: InputLifecycleState,
        outcome: InputTerminalOutcome,
    ) -> StoredInputState {
        StoredInputState {
            state: InputState::new_accepted(input_id.clone()),
            seed: InputStateSeed {
                phase,
                terminal_outcome: Some(outcome),
                ..InputStateSeed::new_accepted()
            },
        }
    }

    fn cancelled_seed() -> InputStateSeed {
        InputStateSeed {
            phase: InputLifecycleState::Abandoned,
            terminal_outcome: Some(InputTerminalOutcome::Abandoned {
                reason: InputAbandonReason::Cancelled,
            }),
            ..InputStateSeed::new_accepted()
        }
    }

    /// Controllable stand-in for a runtime adapter.
    ///
    /// Only the two seams the bounded submit actually uses are programmable:
    /// how long admission takes and what it answers, and what the durable index
    /// says. Everything else refuses, so a test cannot silently depend on a
    /// path this contract does not touch.
    struct ScriptedRuntime {
        admission_delay: Duration,
        admission:
            Box<dyn Fn() -> Result<AcceptOutcome, RuntimeDriverError> + Send + Sync + 'static>,
        durable: Box<
            dyn Fn() -> Result<Option<StoredInputState>, RuntimeDriverError>
                + Send
                + Sync
                + 'static,
        >,
        admissions_started: AtomicUsize,
        admissions_finished: AtomicUsize,
    }

    impl ScriptedRuntime {
        fn new() -> Self {
            Self {
                admission_delay: Duration::ZERO,
                admission: Box::new(|| {
                    Ok(AcceptOutcome::Accepted {
                        input_id: InputId::new(),
                        policy: accepted_policy(),
                        state: InputState::new_accepted(InputId::new()),
                        seed: InputStateSeed::new_accepted(),
                    })
                }),
                durable: Box::new(|| Ok(None)),
                admissions_started: AtomicUsize::new(0),
                admissions_finished: AtomicUsize::new(0),
            }
        }

        fn with_admission_delay(mut self, delay: Duration) -> Self {
            self.admission_delay = delay;
            self
        }

        fn with_admission(
            mut self,
            admission: impl Fn() -> Result<AcceptOutcome, RuntimeDriverError> + Send + Sync + 'static,
        ) -> Self {
            self.admission = Box::new(admission);
            self
        }

        fn with_durable(
            mut self,
            durable: impl Fn() -> Result<Option<StoredInputState>, RuntimeDriverError>
            + Send
            + Sync
            + 'static,
        ) -> Self {
            self.durable = Box::new(durable);
            self
        }
    }

    fn accepted_policy() -> crate::policy::PolicyDecision {
        use crate::policy::{
            ApplyMode, ConsumePoint, DrainPolicy, PolicyDecision, QueueMode, RoutingDisposition,
            WakeMode,
        };
        PolicyDecision {
            apply_mode: ApplyMode::StageRunStart,
            wake_mode: WakeMode::WakeIfIdle,
            queue_mode: QueueMode::Fifo,
            consume_point: ConsumePoint::OnRunComplete,
            drain_policy: DrainPolicy::QueueNextTurn,
            routing_disposition: RoutingDisposition::Queue,
            record_transcript: true,
            emit_operator_content: true,
            policy_version: crate::identifiers::PolicyVersion(1),
        }
    }

    fn unsupported(method: &str) -> RuntimeDriverError {
        RuntimeDriverError::Internal(format!("{method} is out of scope for ScriptedRuntime"))
    }

    #[async_trait::async_trait]
    impl SessionServiceRuntimeExt for ScriptedRuntime {
        async fn accept_input(
            &self,
            _session_id: &SessionId,
            _input: Input,
        ) -> Result<AcceptOutcome, RuntimeDriverError> {
            self.admissions_started.fetch_add(1, Ordering::SeqCst);
            if !self.admission_delay.is_zero() {
                tokio::time::sleep(self.admission_delay).await;
            }
            let outcome = (self.admission)();
            self.admissions_finished.fetch_add(1, Ordering::SeqCst);
            outcome
        }

        async fn accept_input_with_completion(
            &self,
            _session_id: &SessionId,
            _input: Input,
        ) -> Result<(AcceptOutcome, Option<crate::completion::CompletionHandle>), RuntimeDriverError>
        {
            Err(unsupported("accept_input_with_completion"))
        }

        async fn runtime_state(
            &self,
            _session_id: &SessionId,
        ) -> Result<RuntimeState, RuntimeDriverError> {
            Err(unsupported("runtime_state"))
        }

        async fn retire_runtime(
            &self,
            _session_id: &SessionId,
        ) -> Result<crate::traits::RetireReport, RuntimeDriverError> {
            Err(unsupported("retire_runtime"))
        }

        async fn reset_runtime(
            &self,
            _session_id: &SessionId,
        ) -> Result<crate::traits::ResetReport, RuntimeDriverError> {
            Err(unsupported("reset_runtime"))
        }

        async fn input_state(
            &self,
            _session_id: &SessionId,
            _input_id: &InputId,
        ) -> Result<Option<StoredInputState>, RuntimeDriverError> {
            Err(unsupported("input_state"))
        }

        async fn input_terminal_completion(
            &self,
            _session_id: &SessionId,
            _input_id: &InputId,
        ) -> Result<Option<crate::completion::CompletionOutcome>, RuntimeDriverError> {
            Err(unsupported("input_terminal_completion"))
        }

        async fn input_state_by_idempotency_key(
            &self,
            _session_id: &SessionId,
            _idempotency_key: &str,
        ) -> Result<Option<StoredInputState>, RuntimeDriverError> {
            Err(unsupported("input_state_by_idempotency_key"))
        }

        async fn durable_input_state_by_idempotency_key(
            &self,
            _session_id: &SessionId,
            _idempotency_key: &str,
        ) -> Result<Option<StoredInputState>, RuntimeDriverError> {
            (self.durable)()
        }

        async fn interaction_terminal_status(
            &self,
            _session_id: &SessionId,
            _selector: crate::terminal_status::InteractionSelector,
        ) -> Result<
            Option<
                crate::terminal_status::Sourced<crate::terminal_status::InteractionTerminalReport>,
            >,
            RuntimeDriverError,
        > {
            Err(unsupported("interaction_terminal_status"))
        }

        async fn run_terminal_status(
            &self,
            _session_id: &SessionId,
            _run_id: &RunId,
        ) -> Result<
            crate::terminal_status::Sourced<crate::terminal_status::RunTerminalReport>,
            RuntimeDriverError,
        > {
            Err(unsupported("run_terminal_status"))
        }

        async fn list_active_inputs(
            &self,
            _session_id: &SessionId,
        ) -> Result<Vec<InputId>, RuntimeDriverError> {
            Err(unsupported("list_active_inputs"))
        }

        async fn reconfigure_session_llm_identity(
            &self,
            _session_id: &SessionId,
            _request: crate::meerkat_machine_types::SessionLlmReconfigureRequest,
        ) -> Result<crate::meerkat_machine_types::SessionLlmReconfigureReport, RuntimeDriverError>
        {
            Err(unsupported("reconfigure_session_llm_identity"))
        }
    }

    #[test]
    fn absent_bound_uses_the_documented_default() {
        let submission = BoundedSubmission::new(IdempotencyKey::new("k"));
        assert_eq!(submission.bound(), SubmitBound::default());
        assert_eq!(submission.bound().as_duration(), DEFAULT_SUBMIT_BOUND);
    }

    #[test]
    fn caller_supplied_bound_is_honored_and_reserves_a_classification_slice() {
        let bound = SubmitBound::after(Duration::from_secs(20));
        let (admission, evidence) = bound.split();
        assert_eq!(evidence, Duration::from_secs(5));
        assert_eq!(admission, Duration::from_secs(15));
        assert_eq!(admission + evidence, bound.as_duration());
    }

    #[test]
    fn a_bound_too_small_to_split_still_funds_classification() {
        let bound = SubmitBound::after(Duration::from_millis(10));
        let (admission, evidence) = bound.split();
        assert_eq!(admission, Duration::ZERO);
        assert_eq!(evidence, MIN_EVIDENCE_READ_BOUND);
    }

    #[tokio::test]
    async fn expired_bound_reports_durably_queued_from_the_store_index() {
        let queued = InputId::new();
        let witness = queued.clone();
        let runtime = Arc::new(
            ScriptedRuntime::new()
                .with_admission_delay(Duration::from_secs(30))
                .with_durable(move || Ok(Some(stored(&witness)))),
        );

        let report = submit_bounded(
            Arc::clone(&runtime),
            &SessionId::new(),
            prompt("slow admission"),
            BoundedSubmission::new(IdempotencyKey::new("telegram-42"))
                .with_bound(SubmitBound::after(Duration::from_millis(20))),
        )
        .await;

        match &report.outcome {
            BoundedSubmitOutcome::TimedOut {
                cause: SubmitTimeoutCause::BoundExpired,
                disposition:
                    SubmitTimeoutDisposition::DurablyAdmitted {
                        input_id,
                        state: AdmittedWorkState::Pending,
                    },
            } => assert_eq!(input_id, &queued),
            other => panic!("expected a durably-queued timeout, got {other:?}"),
        }
        assert!(report.is_durably_queued());
    }

    #[tokio::test]
    async fn an_expired_bound_over_a_cancelled_row_is_not_reported_as_queued() {
        let cancelled = InputId::new();
        let witness = cancelled.clone();
        let runtime = Arc::new(
            ScriptedRuntime::new()
                .with_admission_delay(Duration::from_secs(30))
                .with_durable(move || {
                    Ok(Some(stored_terminal(
                        &witness,
                        InputLifecycleState::Abandoned,
                        InputTerminalOutcome::Abandoned {
                            reason: InputAbandonReason::Cancelled,
                        },
                    )))
                }),
        );

        let report = submit_bounded(
            Arc::clone(&runtime),
            &SessionId::new(),
            prompt("slow admission over a cancelled key"),
            BoundedSubmission::new(IdempotencyKey::new("telegram-50"))
                .with_bound(SubmitBound::after(Duration::from_millis(20))),
        )
        .await;

        match &report.outcome {
            BoundedSubmitOutcome::TimedOut {
                cause: SubmitTimeoutCause::BoundExpired,
                disposition:
                    SubmitTimeoutDisposition::DurablyAdmitted {
                        input_id,
                        state:
                            AdmittedWorkState::TerminalWithoutDelivery {
                                outcome: InputPublicTerminalOutcome::Cancelled,
                            },
                    },
            } => assert_eq!(input_id, &cancelled),
            other => panic!("expected a cancelled durable witness, got {other:?}"),
        }
        assert!(
            !report.is_durably_queued(),
            "a durable row for cancelled work is not queued work"
        );
        assert!(report.is_terminal_without_delivery());
    }

    #[tokio::test]
    async fn a_collapse_onto_cancelled_work_is_not_reported_as_queued() {
        let cancelled = InputId::new();
        let dedup_target = cancelled.clone();
        let witness = cancelled.clone();
        let runtime = Arc::new(
            ScriptedRuntime::new()
                .with_admission(move || {
                    Ok(AcceptOutcome::Deduplicated {
                        input_id: InputId::new(),
                        existing_id: dedup_target.clone(),
                        existing_seed: cancelled_seed(),
                    })
                })
                .with_durable(move || {
                    Ok(Some(stored_terminal(
                        &witness,
                        InputLifecycleState::Abandoned,
                        InputTerminalOutcome::Abandoned {
                            reason: InputAbandonReason::Cancelled,
                        },
                    )))
                }),
        );

        let report = submit_bounded(
            Arc::clone(&runtime),
            &SessionId::new(),
            prompt("retry of a cancelled message"),
            BoundedSubmission::new(IdempotencyKey::new("telegram-51")),
        )
        .await;

        match &report.outcome {
            BoundedSubmitOutcome::Collapsed {
                input_id,
                durability: AdmissionDurability::Durable,
                state:
                    AdmittedWorkState::TerminalWithoutDelivery {
                        outcome: InputPublicTerminalOutcome::Cancelled,
                    },
            } => assert_eq!(input_id, &cancelled),
            other => panic!("expected a collapse onto cancelled work, got {other:?}"),
        }
        assert!(
            !report.is_durably_queued(),
            "collapsing onto a cancelled input must not report queued work"
        );
        assert!(report.is_terminal_without_delivery());
    }

    #[tokio::test]
    async fn a_collapse_onto_completed_work_reports_delivery_not_queueing() {
        let completed = InputId::new();
        let dedup_target = completed.clone();
        let witness = completed.clone();
        let runtime = Arc::new(
            ScriptedRuntime::new()
                .with_admission(move || {
                    Ok(AcceptOutcome::Deduplicated {
                        input_id: InputId::new(),
                        existing_id: dedup_target.clone(),
                        existing_seed: InputStateSeed {
                            phase: InputLifecycleState::Consumed,
                            terminal_outcome: Some(InputTerminalOutcome::Consumed),
                            ..InputStateSeed::new_accepted()
                        },
                    })
                })
                .with_durable(move || {
                    Ok(Some(stored_terminal(
                        &witness,
                        InputLifecycleState::Consumed,
                        InputTerminalOutcome::Consumed,
                    )))
                }),
        );

        let report = submit_bounded(
            Arc::clone(&runtime),
            &SessionId::new(),
            prompt("retry of a message that already ran"),
            BoundedSubmission::new(IdempotencyKey::new("telegram-52")),
        )
        .await;

        match &report.outcome {
            BoundedSubmitOutcome::Collapsed {
                input_id,
                durability: AdmissionDurability::Durable,
                state: AdmittedWorkState::Delivered,
            } => assert_eq!(input_id, &completed),
            other => panic!("expected a collapse onto completed work, got {other:?}"),
        }
        assert!(
            !report.is_durably_queued(),
            "work that already ran is not queued work"
        );
        assert!(!report.is_terminal_without_delivery());
    }

    #[tokio::test]
    async fn an_inconsistent_seed_is_unclassified_rather_than_assumed_queued() {
        let inconsistent = InputId::new();
        let dedup_target = inconsistent.clone();
        let runtime = Arc::new(ScriptedRuntime::new().with_admission(move || {
            Ok(AcceptOutcome::Deduplicated {
                input_id: InputId::new(),
                existing_id: dedup_target.clone(),
                // A terminal phase with no terminal outcome matches no
                // generated projection: the seed's own facts disagree.
                existing_seed: InputStateSeed {
                    phase: InputLifecycleState::Abandoned,
                    ..InputStateSeed::new_accepted()
                },
            })
        }));

        let report = submit_bounded(
            Arc::clone(&runtime),
            &SessionId::new(),
            prompt("unclassifiable collapse target"),
            BoundedSubmission::new(IdempotencyKey::new("telegram-53")),
        )
        .await;

        match &report.outcome {
            BoundedSubmitOutcome::Collapsed {
                input_id,
                state: AdmittedWorkState::Unclassified,
                ..
            } => assert_eq!(input_id, &inconsistent),
            other => panic!("expected an unclassified collapse target, got {other:?}"),
        }
        assert!(!report.is_durably_queued());
    }

    #[tokio::test]
    async fn expired_bound_reports_unknown_without_a_durable_witness() {
        let runtime = Arc::new(
            ScriptedRuntime::new()
                .with_admission_delay(Duration::from_secs(30))
                .with_durable(|| Ok(None)),
        );

        let report = submit_bounded(
            Arc::clone(&runtime),
            &SessionId::new(),
            prompt("slow admission"),
            BoundedSubmission::new(IdempotencyKey::new("telegram-43"))
                .with_bound(SubmitBound::after(Duration::from_millis(20))),
        )
        .await;

        assert!(matches!(
            report.outcome,
            BoundedSubmitOutcome::TimedOut {
                cause: SubmitTimeoutCause::BoundExpired,
                disposition: SubmitTimeoutDisposition::Unknown {
                    cause: SubmitUnknownCause::NoDurableWitness
                },
            }
        ));
        assert!(!report.is_durably_queued());
    }

    #[tokio::test]
    async fn a_failing_evidence_read_is_unknown_rather_than_an_untyped_error() {
        let runtime = Arc::new(
            ScriptedRuntime::new()
                .with_admission_delay(Duration::from_secs(30))
                .with_durable(|| {
                    Err(RuntimeDriverError::RecoveryRepairBlocked {
                        evidence_digest: None,
                        reason: "durable idempotency-index corruption".to_string(),
                    })
                }),
        );

        let report = submit_bounded(
            Arc::clone(&runtime),
            &SessionId::new(),
            prompt("slow admission"),
            BoundedSubmission::new(IdempotencyKey::new("telegram-44"))
                .with_bound(SubmitBound::after(Duration::from_millis(20))),
        )
        .await;

        assert!(matches!(
            report.outcome,
            BoundedSubmitOutcome::TimedOut {
                disposition: SubmitTimeoutDisposition::Unknown {
                    cause: SubmitUnknownCause::EvidenceUnavailable
                },
                ..
            }
        ));
    }

    #[tokio::test]
    async fn an_expired_bound_never_cancels_the_admission() {
        let runtime =
            Arc::new(ScriptedRuntime::new().with_admission_delay(Duration::from_millis(150)));

        let report = submit_bounded(
            Arc::clone(&runtime),
            &SessionId::new(),
            prompt("slow but successful"),
            BoundedSubmission::new(IdempotencyKey::new("telegram-45"))
                .with_bound(SubmitBound::after(Duration::from_millis(20))),
        )
        .await;
        assert!(matches!(
            report.outcome,
            BoundedSubmitOutcome::TimedOut { .. }
        ));
        assert_eq!(runtime.admissions_finished.load(Ordering::SeqCst), 0);

        tokio::time::sleep(Duration::from_millis(300)).await;
        assert_eq!(
            runtime.admissions_finished.load(Ordering::SeqCst),
            1,
            "the detached admission must still complete after the caller's bound expired"
        );
    }

    /// A caller that asks not to wait must get the same answer whether or not
    /// the admission task happened to be scheduled first. Anything else makes
    /// the reported fate a scheduling detail.
    #[tokio::test]
    async fn a_zero_bound_never_reports_an_admission_answer() {
        let runtime = Arc::new(ScriptedRuntime::new());

        let report = submit_bounded(
            Arc::clone(&runtime),
            &SessionId::new(),
            prompt("instant admission, zero bound"),
            BoundedSubmission::new(IdempotencyKey::new("telegram-55"))
                .with_bound(SubmitBound::after(Duration::ZERO)),
        )
        .await;

        assert!(
            matches!(
                report.outcome,
                BoundedSubmitOutcome::TimedOut {
                    cause: SubmitTimeoutCause::BoundExpired,
                    disposition: SubmitTimeoutDisposition::Unknown {
                        cause: SubmitUnknownCause::NoDurableWitness
                    },
                }
            ),
            "a zero bound must not observe the admission, got {:?}",
            report.outcome
        );

        tokio::time::sleep(Duration::from_millis(50)).await;
        assert_eq!(
            runtime.admissions_finished.load(Ordering::SeqCst),
            1,
            "not waiting for the answer must not stop the work"
        );
    }

    #[tokio::test]
    async fn a_conflicting_carried_key_is_refused_without_submitting() {
        let runtime = Arc::new(ScriptedRuntime::new());
        let mut input = prompt("already identified");
        input.header_mut().idempotency_key = Some(IdempotencyKey::new("original"));

        let report = submit_bounded(
            Arc::clone(&runtime),
            &SessionId::new(),
            input,
            BoundedSubmission::new(IdempotencyKey::new("different")),
        )
        .await;

        match &report.outcome {
            BoundedSubmitOutcome::Refused {
                reason: SubmitRefusal::IdempotencyKeyConflict { supplied, carried },
            } => {
                assert_eq!(supplied, &IdempotencyKey::new("different"));
                assert_eq!(carried, &IdempotencyKey::new("original"));
            }
            other => panic!("expected a key-conflict refusal, got {other:?}"),
        }
        assert_eq!(runtime.admissions_started.load(Ordering::SeqCst), 0);
    }

    #[tokio::test]
    async fn a_pre_admission_refusal_is_typed_and_definite() {
        let runtime = Arc::new(ScriptedRuntime::new().with_admission(|| {
            Err(RuntimeDriverError::NotReady {
                state: RuntimeState::Stopped,
            })
        }));

        let report = submit_bounded(
            Arc::clone(&runtime),
            &SessionId::new(),
            prompt("refused"),
            BoundedSubmission::new(IdempotencyKey::new("telegram-46")),
        )
        .await;

        assert!(matches!(
            report.outcome,
            BoundedSubmitOutcome::Refused {
                reason: SubmitRefusal::Runtime {
                    error: RuntimeDriverError::NotReady { .. }
                }
            }
        ));
    }

    #[tokio::test]
    async fn an_indeterminate_runtime_failure_is_not_dressed_up_as_a_refusal() {
        let runtime = Arc::new(
            ScriptedRuntime::new()
                .with_admission(|| Err(RuntimeDriverError::Internal("wedged".to_string()))),
        );

        let report = submit_bounded(
            Arc::clone(&runtime),
            &SessionId::new(),
            prompt("indeterminate"),
            BoundedSubmission::new(IdempotencyKey::new("telegram-47")),
        )
        .await;

        assert!(matches!(
            report.outcome,
            BoundedSubmitOutcome::TimedOut {
                cause: SubmitTimeoutCause::RuntimeIndeterminate { .. },
                disposition: SubmitTimeoutDisposition::Unknown { .. },
            }
        ));
    }

    #[tokio::test]
    async fn a_failure_after_a_durable_claim_collapses_instead_of_refusing() {
        let queued = InputId::new();
        let witness = queued.clone();
        let runtime = Arc::new(
            ScriptedRuntime::new()
                .with_admission(|| {
                    Err(RuntimeDriverError::NotReady {
                        state: RuntimeState::Destroyed,
                    })
                })
                .with_durable(move || Ok(Some(stored(&witness)))),
        );

        let report = submit_bounded(
            Arc::clone(&runtime),
            &SessionId::new(),
            prompt("retry after restart"),
            BoundedSubmission::new(IdempotencyKey::new("telegram-48")),
        )
        .await;

        match &report.outcome {
            BoundedSubmitOutcome::Collapsed {
                input_id,
                durability: AdmissionDurability::Durable,
                state: AdmittedWorkState::Pending,
            } => assert_eq!(input_id, &queued),
            other => panic!("expected a durable collapse, got {other:?}"),
        }
        assert!(report.is_durably_queued());
    }

    /// The compensation path: a post-admission failure whose own rollback
    /// terminalized the row it had already committed. Durable evidence exists,
    /// so this is not a refusal - but nothing is coming, and the report has to
    /// say so on the very first call.
    #[tokio::test]
    async fn a_failure_that_compensated_its_own_admission_reports_dead_work() {
        let compensated = InputId::new();
        let witness = compensated.clone();
        let runtime = Arc::new(
            ScriptedRuntime::new()
                .with_admission(|| {
                    Err(RuntimeDriverError::Internal(
                        "post-admission staging failed; accepted input terminalized".to_string(),
                    ))
                })
                .with_durable(move || {
                    Ok(Some(stored_terminal(
                        &witness,
                        InputLifecycleState::Abandoned,
                        InputTerminalOutcome::Abandoned {
                            reason: InputAbandonReason::Cancelled,
                        },
                    )))
                }),
        );

        let report = submit_bounded(
            Arc::clone(&runtime),
            &SessionId::new(),
            prompt("compensated admission"),
            BoundedSubmission::new(IdempotencyKey::new("telegram-54")),
        )
        .await;

        match &report.outcome {
            BoundedSubmitOutcome::Collapsed {
                input_id,
                durability: AdmissionDurability::Durable,
                state:
                    AdmittedWorkState::TerminalWithoutDelivery {
                        outcome: InputPublicTerminalOutcome::Cancelled,
                    },
            } => assert_eq!(input_id, &compensated),
            other => panic!("expected a compensated-admission collapse, got {other:?}"),
        }
        assert!(!report.is_durably_queued());
        assert!(report.is_terminal_without_delivery());
    }

    #[tokio::test]
    async fn admission_without_durable_evidence_is_reported_process_local() {
        let admitted = InputId::new();
        let echoed = admitted.clone();
        let runtime = Arc::new(
            ScriptedRuntime::new()
                .with_admission(move || {
                    Ok(AcceptOutcome::Accepted {
                        input_id: echoed.clone(),
                        policy: accepted_policy(),
                        state: InputState::new_accepted(echoed.clone()),
                        seed: InputStateSeed::new_accepted(),
                    })
                })
                .with_durable(|| Ok(None)),
        );

        let report = submit_bounded(
            Arc::clone(&runtime),
            &SessionId::new(),
            prompt("ephemeral admission"),
            BoundedSubmission::new(IdempotencyKey::new("telegram-49")),
        )
        .await;

        match &report.outcome {
            BoundedSubmitOutcome::Admitted {
                input_id,
                durability: AdmissionDurability::ProcessLocalOnly,
                state: AdmittedWorkState::Pending,
            } => assert_eq!(input_id, &admitted),
            other => panic!("expected a process-local admission, got {other:?}"),
        }
        assert!(!report.is_durably_queued());
    }
}