turnframe-runtime 0.1.1

Deterministic turn orchestration runtime for Turnframe: interpretation, reduction, interactions, commands, events and response composition
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
//! Orchestration modes and runtime configuration (spec §11.1, §11.4, Appendix A).
//!
//! Two things live here. The first is [`OrchestrationMode`]: how much rope the
//! model gets before the deterministic pipeline takes over. The second is
//! [`OrchestratorConfig`]: the knobs an application turns, every one of which
//! defaults to the careful setting.
//!
//! # Safety settings are not knobs
//!
//! A few fields exist only so that a configuration file which tries to disable
//! them fails loudly instead of quietly working. [`OrchestratorConfig::validate`]
//! refuses `reject_unknown_fields = false`, `require_evidence_for_mutations =
//! false` and `fail_closed_on_policy_store_error = false`, because the library
//! does not implement the permissive behaviour at all: the plan types deny
//! unknown fields unconditionally, evidence is validated unconditionally, and
//! there is no fail-open path for an unavailable policy source (I19). A config
//! that asks for the permissive behaviour is a config whose author believes
//! something untrue, and that is worth an error.
//!
//! Sandboxed autonomy goes further: the variant cannot be written down without
//! a [`SandboxAcknowledgement`], whose only constructor is named after what it
//! gives up and logs a warning when called.

use std::fmt;
use std::time::Duration;

use serde::de::{Error as _, Unexpected};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use turnframe_core::command::RiskClass;
use turnframe_core::flow::BriefingBudget;
use turnframe_core::plan::limits::PlanLimits;
use turnframe_core::policy::PolicySnapshot;
use turnframe_core::reduce::SourcePolicy;
use turnframe_core::response::ToneProfile;
use turnframe_core::turn::TurnLimits;
use turnframe_tasks::{Budget, TaskProfiles};
use turnframe_understand::Settings;

/// A configuration value the library refuses to work with.
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum ConfigError {
    /// A bound that must allow at least one of something was zero.
    #[error("{field} must be at least 1")]
    MustBePositive {
        /// Dotted path of the field, e.g. `execution.max_commands_per_turn`.
        field: &'static str,
    },
    /// A safety setting was turned off, and the library has no such mode.
    #[error("{field} may not be disabled: {because}")]
    UnsafeSetting {
        /// Dotted path of the field.
        field: &'static str,
        /// Why the setting cannot be disabled.
        because: &'static str,
    },
    /// Two settings ask for incompatible things.
    #[error("{first} contradicts {second}")]
    Contradiction {
        /// Dotted path of the first field.
        first: &'static str,
        /// Dotted path of the second field.
        second: &'static str,
    },
    /// A ratio expressed in per mille left the `0..=1000` range.
    #[error("{field} must be between 0 and 1000 per mille")]
    OutOfRange {
        /// Dotted path of the field.
        field: &'static str,
    },
}

/// The exact phrase that acknowledges sandboxed autonomy, on the wire and in
/// the name of [`SandboxAcknowledgement::i_accept_unreviewed_autonomous_writes`].
pub const SANDBOX_ACKNOWLEDGEMENT: &str = "i-accept-unreviewed-autonomous-writes";

/// Proof that the caller knows what
/// [`OrchestrationMode::SandboxedAutonomous`] gives up.
///
/// The type has no public field and no `Default`, so the variant cannot be
/// written as a struct literal from another crate; the only way in is
/// [`Self::i_accept_unreviewed_autonomous_writes`], which logs a warning. On the
/// wire it is the literal string [`SANDBOX_ACKNOWLEDGEMENT`], so a TOML or JSON
/// configuration has to spell the sentence out too.
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct SandboxAcknowledgement(());

impl SandboxAcknowledgement {
    /// Acknowledges that sandboxed autonomy lets the model drive writes that no
    /// human reviewed, and emits a `WARN` on the `turnframe.config` target.
    ///
    /// Use it only for reversible, non-regulated domains. Even then
    /// [`OrchestrationMode::allows_risk`] keeps refusing the destructive,
    /// irreversible and externally regulated classes (§11.4).
    #[must_use]
    pub fn i_accept_unreviewed_autonomous_writes() -> Self {
        tracing::warn!(
            target: "turnframe.config",
            mode = "sandboxed_autonomous",
            acknowledgement = SANDBOX_ACKNOWLEDGEMENT,
            "sandboxed autonomous orchestration enabled: the model may drive writes without human review"
        );
        Self(())
    }
}

impl fmt::Debug for SandboxAcknowledgement {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(SANDBOX_ACKNOWLEDGEMENT)
    }
}

impl Serialize for SandboxAcknowledgement {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        serializer.serialize_str(SANDBOX_ACKNOWLEDGEMENT)
    }
}

impl<'de> Deserialize<'de> for SandboxAcknowledgement {
    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        let raw = String::deserialize(deserializer)?;
        if raw == SANDBOX_ACKNOWLEDGEMENT {
            Ok(Self::i_accept_unreviewed_autonomous_writes())
        } else {
            Err(D::Error::invalid_value(
                Unexpected::Str(&raw),
                &SANDBOX_ACKNOWLEDGEMENT,
            ))
        }
    }
}

/// What a sandboxed autonomous run may spend before it is cut off.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
#[non_exhaustive]
pub struct ResourceBudget {
    /// Model calls of every purpose, across the whole turn.
    pub max_model_calls: u16,
    /// Read-tool calls across the whole turn.
    pub max_read_calls: u16,
    /// Prompt tokens the turn may send in total.
    pub max_prompt_tokens: u64,
    /// Wall clock the turn may take.
    pub max_wall_clock: Duration,
}

impl ResourceBudget {
    /// A small budget: 8 model calls, 16 read calls, 200k prompt tokens, 60s.
    #[must_use]
    pub const fn conservative() -> Self {
        Self {
            max_model_calls: 8,
            max_read_calls: 16,
            max_prompt_tokens: 200_000,
            max_wall_clock: Duration::from_secs(60),
        }
    }

    /// Returns a copy with another model-call budget.
    #[must_use]
    pub const fn with_max_model_calls(mut self, max_model_calls: u16) -> Self {
        self.max_model_calls = max_model_calls;
        self
    }

    /// Returns a copy with another read-call budget.
    #[must_use]
    pub const fn with_max_read_calls(mut self, max_read_calls: u16) -> Self {
        self.max_read_calls = max_read_calls;
        self
    }

    /// Returns a copy with another token budget.
    #[must_use]
    pub const fn with_max_prompt_tokens(mut self, max_prompt_tokens: u64) -> Self {
        self.max_prompt_tokens = max_prompt_tokens;
        self
    }

    /// Returns a copy with another wall-clock budget.
    #[must_use]
    pub const fn with_max_wall_clock(mut self, max_wall_clock: Duration) -> Self {
        self.max_wall_clock = max_wall_clock;
        self
    }

    fn validate(&self) -> Result<(), ConfigError> {
        positive(
            "mode.budget.max_model_calls",
            u64::from(self.max_model_calls),
        )?;
        positive("mode.budget.max_read_calls", u64::from(self.max_read_calls))?;
        positive("mode.budget.max_prompt_tokens", self.max_prompt_tokens)?;
        if self.max_wall_clock.is_zero() {
            return Err(ConfigError::MustBePositive {
                field: "mode.budget.max_wall_clock",
            });
        }
        Ok(())
    }
}

impl Default for ResourceBudget {
    fn default() -> Self {
        Self::conservative()
    }
}

/// How much autonomy the model gets: which risk classes are eligible at all (§11.4).
///
/// | Risk class | `Deterministic` | `SandboxedAutonomous` |
/// | --- | --- | --- |
/// | `ReadOnly`, `ReversibleLowRisk`, `SensitiveDataChange` | yes | yes |
/// | `Destructive`, `Irreversible`, `ExternalRegulated` | yes | **no** |
///
/// A domain that classifies a money movement as `ReversibleLowRisk` has mislabelled it,
/// and no mode can rescue that.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case", deny_unknown_fields)]
#[non_exhaustive]
pub enum OrchestrationMode {
    /// Understanding proposes, the reducer and policy decide. The default.
    #[default]
    Deterministic,
    /// Experimental mode for sandboxed, reversible domains only (§11.4). Build it with
    /// [`Self::sandboxed_autonomous`]; the acknowledgement cannot be built elsewhere.
    SandboxedAutonomous {
        /// What the turn may spend.
        budget: ResourceBudget,
        /// Proof the operator knows what this gives up.
        acknowledgement: SandboxAcknowledgement,
    },
}

impl OrchestrationMode {
    /// Builds the sandboxed autonomous mode.
    ///
    /// The `acknowledgement` argument is the whole point: obtaining one means
    /// calling [`SandboxAcknowledgement::i_accept_unreviewed_autonomous_writes`],
    /// which logs a warning.
    #[must_use]
    pub const fn sandboxed_autonomous(
        budget: ResourceBudget,
        acknowledgement: SandboxAcknowledgement,
    ) -> Self {
        Self::SandboxedAutonomous {
            budget,
            acknowledgement,
        }
    }

    /// Whether commands of `risk` are eligible in this mode (§11.4).
    ///
    /// See the table on [`OrchestrationMode`].
    #[must_use]
    pub fn allows_risk(&self, risk: RiskClass) -> bool {
        match self {
            Self::SandboxedAutonomous { .. } => !matches!(
                risk,
                RiskClass::Destructive | RiskClass::Irreversible | RiskClass::ExternalRegulated
            ),
            Self::Deterministic => true,
        }
    }

    /// The risk classes this mode refuses, in ladder order.
    ///
    /// Feed them to [`PolicySnapshot::forbidden_risk_classes`] — or let
    /// [`OrchestratorConfig::policy_snapshot`] do it.
    #[must_use]
    pub fn forbidden_risk_classes(&self) -> Vec<RiskClass> {
        [
            RiskClass::ReadOnly,
            RiskClass::ReversibleLowRisk,
            RiskClass::SensitiveDataChange,
            RiskClass::Destructive,
            RiskClass::Irreversible,
            RiskClass::ExternalRegulated,
        ]
        .into_iter()
        .filter(|risk| !self.allows_risk(*risk))
        .collect()
    }

    /// The resource budget, for the sandboxed mode only.
    #[must_use]
    pub fn budget(&self) -> Option<&ResourceBudget> {
        match self {
            Self::SandboxedAutonomous { budget, .. } => Some(budget),
            _ => None,
        }
    }

    /// Returns `true` for [`Self::SandboxedAutonomous`].
    #[must_use]
    pub fn is_sandboxed(&self) -> bool {
        matches!(self, Self::SandboxedAutonomous { .. })
    }

    fn validate(&self) -> Result<(), ConfigError> {
        if let Some(budget) = self.budget() {
            budget.validate()?;
        }
        Ok(())
    }
}

/// How much of a turn's files reaches a model.
///
/// # Why nothing ships
///
/// A limit an adopter cannot raise is not configurable, so there is no shipped
/// ceiling here either: an adopter who sets nothing sends every file the turn
/// carried, which is what the user attached and what they expect to be looked
/// at. Attachments are bounded by what one person put in one message, unlike a
/// conversation or a briefing, so there is no runaway to protect anybody from.
///
/// What a budget buys, when one is set, is that the runtime says which files it
/// left out — deterministically to the user and as a fact to the writing stage.
/// A silently truncated request produces an answer about the wrong document,
/// which is worse than an answer about none.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
#[non_exhaustive]
pub struct AttachmentConfig {
    /// How many files may go into one request, or `None` for all of them.
    pub max_files: Option<usize>,
    /// How many bytes of file may go into one request, or `None` for all.
    ///
    /// Counted before encoding, which is what an application can reason about;
    /// the wire form is larger and the provider's own limit is the one that
    /// finally applies.
    pub max_total_bytes: Option<usize>,
}

impl AttachmentConfig {
    /// No limits, which is what ships.
    #[must_use]
    pub const fn conservative() -> Self {
        Self {
            max_files: None,
            max_total_bytes: None,
        }
    }

    /// Returns a copy taking at most `max_files` files per request.
    #[must_use]
    pub const fn with_max_files(mut self, max_files: Option<usize>) -> Self {
        self.max_files = max_files;
        self
    }

    /// Returns a copy taking at most `max_total_bytes` of file per request.
    #[must_use]
    pub const fn with_max_total_bytes(mut self, max_total_bytes: Option<usize>) -> Self {
        self.max_total_bytes = max_total_bytes;
        self
    }
}

impl Default for AttachmentConfig {
    fn default() -> Self {
        Self::conservative()
    }
}

/// How a turn is understood: the limits on what it may ask, the conversation shown, the
/// budget of its model tasks, which acts are verified, and each task kind's settings.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
#[non_exhaustive]
pub struct UnderstandingConfig {
    /// Size limits on what one turn may be understood to ask.
    pub plan_limits: PlanLimits,
    /// Size limits on the turn input.
    pub turn_limits: TurnLimits,
    /// How many past turns are loaded, or `None` for all of them.
    pub transcript_turns: Option<usize>,
    /// How much of a workflow's per-phase guidance reaches a task, per record.
    pub briefing_budget: BriefingBudget,
    /// What understanding one turn may spend.
    pub budget: Budget,
    /// Which acts are verified, and how much conversation extraction sees.
    pub settings: Settings,
    /// Each task kind's model, sampling, votes, repairs and escalation.
    pub tasks: TaskProfiles,
}

impl UnderstandingConfig {
    /// No limits, the whole conversation, the shipped budget, settings and profiles.
    #[must_use]
    pub const fn conservative() -> Self {
        Self {
            plan_limits: PlanLimits::conservative(),
            turn_limits: TurnLimits::conservative(),
            transcript_turns: None,
            briefing_budget: BriefingBudget::conservative(),
            budget: Budget::understanding(),
            settings: Settings::conservative(),
            tasks: TaskProfiles::new(),
        }
    }

    /// Returns a copy with other plan limits.
    #[must_use]
    pub fn with_plan_limits(mut self, plan_limits: PlanLimits) -> Self {
        self.plan_limits = plan_limits;
        self
    }

    /// Returns a copy with other turn limits.
    #[must_use]
    pub fn with_turn_limits(mut self, turn_limits: TurnLimits) -> Self {
        self.turn_limits = turn_limits;
        self
    }

    /// Returns a copy loading at most `transcript_turns` past turns.
    #[must_use]
    pub fn with_transcript_turns(mut self, transcript_turns: Option<usize>) -> Self {
        self.transcript_turns = transcript_turns;
        self
    }

    /// Returns a copy with another briefing budget.
    #[must_use]
    pub fn with_briefing_budget(mut self, budget: BriefingBudget) -> Self {
        self.briefing_budget = budget;
        self
    }

    /// Returns a copy with another task budget.
    #[must_use]
    pub fn with_budget(mut self, budget: Budget) -> Self {
        self.budget = budget;
        self
    }

    /// Returns a copy with other pipeline settings.
    #[must_use]
    pub fn with_settings(mut self, settings: Settings) -> Self {
        self.settings = settings;
        self
    }

    /// Returns a copy with other task profiles.
    #[must_use]
    pub fn with_tasks(mut self, tasks: TaskProfiles) -> Self {
        self.tasks = tasks;
        self
    }

    /// The act limit, when one is set.
    #[must_use]
    pub const fn max_acts(&self) -> Option<usize> {
        self.plan_limits.max_acts
    }

    /// The question limit, when one is set.
    #[must_use]
    pub const fn max_questions(&self) -> Option<usize> {
        self.plan_limits.max_questions
    }

    fn validate(&self) -> Result<(), ConfigError> {
        // A limit that is not set is not a limit of zero.
        for (field, limit) in [
            ("understanding.plan_limits.max_acts", self.max_acts()),
            (
                "understanding.plan_limits.max_questions",
                self.max_questions(),
            ),
            (
                "understanding.plan_limits.max_constraints",
                self.plan_limits.max_constraints,
            ),
            (
                "understanding.turn_limits.max_text_bytes",
                self.turn_limits.max_text_bytes,
            ),
        ] {
            if let Some(limit) = limit {
                positive(field, limit as u64)?;
            }
        }
        positive(
            "understanding.budget.max_parallel",
            self.budget.max_parallel as u64,
        )?;
        positive(
            "understanding.budget.per_call_timeout_secs",
            self.budget.per_call_timeout_secs,
        )
    }
}

impl Default for UnderstandingConfig {
    fn default() -> Self {
        Self::conservative()
    }
}

/// How the assistant is allowed to talk (spec §18, §19).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
#[non_exhaustive]
pub struct NarrationConfig {
    /// Whether a model writes the answer and transition blocks at all. With
    /// narration off the turn is receipts, notices and cards only.
    pub enabled: bool,
    /// What the narration tasks of one turn may spend.
    #[serde(default = "Budget::narration")]
    pub budget: Budget,
    /// Voice the narrator should use.
    pub tone: ToneProfile,
    /// Cap on one written block, in characters; `None`, the default, is no cap. A
    /// longer block is refused whole and reported, never cut mid-sentence.
    pub max_answer_chars: Option<usize>,
    /// Source requirement for answers resting on general domain knowledge.
    /// Answers resting on case state are always `AuthoritativeOnly`.
    pub default_source_policy: SourcePolicy,
    /// Whether narration may be regenerated after commands committed. This is
    /// the only retry I17 permits once effects may exist.
    pub allow_retry_after_commit: bool,
    /// Whether each understanding step is also said in the turn's language, as a
    /// `TurnEvent::StepSaid`: one small model call per step. Off by default.
    #[serde(default)]
    pub steps: bool,
}

impl NarrationConfig {
    /// Narration on, neutral tone, no cap on answer length, any source,
    /// post-commit narration retries allowed.
    #[must_use]
    pub const fn conservative() -> Self {
        Self {
            enabled: true,
            budget: Budget::narration(),
            tone: ToneProfile::Neutral,
            max_answer_chars: None,
            default_source_policy: SourcePolicy::AnySource,
            allow_retry_after_commit: true,
            steps: false,
        }
    }

    /// Returns a copy with narration switched on or off.
    ///
    /// With narration off the turn is receipts, notices and cards only, and
    /// every question still gets a block — an explicitly unsupported one
    /// (spec §19.4). Silence is never how a question disappears.
    #[must_use]
    pub const fn with_enabled(mut self, enabled: bool) -> Self {
        self.enabled = enabled;
        self
    }

    /// Returns a copy with another budget for the narration tasks.
    #[must_use]
    pub const fn with_budget(mut self, budget: Budget) -> Self {
        self.budget = budget;
        self
    }

    /// Returns a copy that says, or stops saying, each understanding step.
    #[must_use]
    pub const fn with_steps(mut self, steps: bool) -> Self {
        self.steps = steps;
        self
    }

    /// Returns a copy with another tone.
    #[must_use]
    pub const fn with_tone(mut self, tone: ToneProfile) -> Self {
        self.tone = tone;
        self
    }

    /// Returns a copy with another source policy for general-knowledge answers.
    #[must_use]
    pub const fn with_default_source_policy(mut self, policy: SourcePolicy) -> Self {
        self.default_source_policy = policy;
        self
    }

    /// Returns a copy with a cap on one generated answer, or with none.
    #[must_use]
    pub const fn with_max_answer_chars(mut self, max_answer_chars: Option<usize>) -> Self {
        self.max_answer_chars = max_answer_chars;
        self
    }

    fn validate(&self) -> Result<(), ConfigError> {
        match self.max_answer_chars {
            Some(max) => positive("narration.max_answer_chars", max as u64),
            None => Ok(()),
        }
    }
}

impl Default for NarrationConfig {
    fn default() -> Self {
        Self::conservative()
    }
}

/// How server-created cards behave (spec §15).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
#[non_exhaustive]
pub struct InteractionConfig {
    /// Time to live given to cards the runtime creates. `None` means no expiry.
    pub default_ttl: Option<Duration>,
    /// How many candidates a server-defined selection card may offer.
    ///
    /// Beyond this the act is rejected and the user is asked to narrow it down,
    /// rather than shown a truncated list — truncating would let list order
    /// decide which cases the user may pick, which is exactly what I8 forbids.
    pub max_selection_candidates: usize,
    /// Whether a selection card owns unqualified answers for the case it is
    /// filed under (I5).
    ///
    /// The default is `false`, and that is a considered choice. A card asking
    /// "which of these did you mean?" has to be stored against *some* case row,
    /// but the whole point is that nobody knows yet which case the user meant.
    /// Letting it block that row would wedge a case the user may never have been
    /// talking about, and would compete with the blocking card that case might
    /// legitimately need. Turn it on only where a selection really does own the
    /// conversation until it is answered.
    pub selection_cards_block_the_case: bool,
    /// Whether confirmation cards are invalidated by a revision change.
    pub confirmation_cards_bind_to_revision: bool,
    /// Whether a card whose command failed goes back to `Active` so the user
    /// may answer again, instead of being recorded `Failed` (spec §15.5).
    ///
    /// The default is `false`, and that is the careful reading. A command that
    /// the domain refused, or that was planned against a revision the case has
    /// left, will usually be refused again for the same reason; re-offering the
    /// same button invites the user to keep pressing it. Turn it on where the
    /// failure is genuinely transient and the card is still the right question.
    pub restore_card_after_failed_command: bool,
}

impl InteractionConfig {
    /// One-day expiry, at most 8 candidates, non-blocking selection cards,
    /// confirmations bound to the revision they were rendered against.
    #[must_use]
    pub const fn conservative() -> Self {
        Self {
            default_ttl: Some(Duration::from_secs(24 * 60 * 60)),
            max_selection_candidates: 8,
            selection_cards_block_the_case: false,
            confirmation_cards_bind_to_revision: true,
            restore_card_after_failed_command: false,
        }
    }

    /// Returns a copy that restores a card whose command failed.
    #[must_use]
    pub const fn restoring_failed_cards(mut self) -> Self {
        self.restore_card_after_failed_command = true;
        self
    }

    /// Returns a copy with another time to live.
    #[must_use]
    pub const fn with_default_ttl(mut self, ttl: Option<Duration>) -> Self {
        self.default_ttl = ttl;
        self
    }

    /// Returns a copy with another candidate cap.
    #[must_use]
    pub const fn with_max_selection_candidates(mut self, max: usize) -> Self {
        self.max_selection_candidates = max;
        self
    }

    fn validate(&self) -> Result<(), ConfigError> {
        if self.max_selection_candidates < 2 {
            return Err(ConfigError::MustBePositive {
                field: "interaction.max_selection_candidates",
            });
        }
        if self.default_ttl.is_some_and(|ttl| ttl.is_zero()) {
            return Err(ConfigError::MustBePositive {
                field: "interaction.default_ttl",
            });
        }
        Ok(())
    }
}

impl Default for InteractionConfig {
    fn default() -> Self {
        Self::conservative()
    }
}

/// How commands are executed (Appendix A, spec §13.4, §16).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
#[non_exhaustive]
pub struct ExecutionConfig {
    /// Time budget for one command batch.
    pub default_timeout: Duration,
    /// Cap on the commands one turn may compile. Exceeding it refuses the turn
    /// rather than executing a prefix.
    pub max_commands_per_turn: usize,
    /// Must stay `true`: an unavailable policy source stops the turn (I19).
    pub fail_closed_on_policy_store_error: bool,
    /// Whether one case may commit while another fails in the same turn.
    pub allow_cross_case_partial_success: bool,
    /// Whether mutations on one case default to committing together
    /// ([`AtomicityScope::PerCase`](turnframe_core::command::AtomicityScope::PerCase)).
    pub group_mutations_per_case: bool,
}

impl ExecutionConfig {
    /// 30 seconds, 32 commands, fail closed, no cross-case partial success,
    /// per-case grouping.
    #[must_use]
    pub const fn conservative() -> Self {
        Self {
            default_timeout: Duration::from_secs(30),
            max_commands_per_turn: 32,
            fail_closed_on_policy_store_error: true,
            allow_cross_case_partial_success: false,
            group_mutations_per_case: true,
        }
    }

    /// Returns a copy with another command budget.
    #[must_use]
    pub const fn with_max_commands_per_turn(mut self, max: usize) -> Self {
        self.max_commands_per_turn = max;
        self
    }

    /// Returns a copy with another batch timeout.
    #[must_use]
    pub const fn with_default_timeout(mut self, timeout: Duration) -> Self {
        self.default_timeout = timeout;
        self
    }

    fn validate(&self) -> Result<(), ConfigError> {
        if self.default_timeout.is_zero() {
            return Err(ConfigError::MustBePositive {
                field: "execution.default_timeout",
            });
        }
        positive(
            "execution.max_commands_per_turn",
            self.max_commands_per_turn as u64,
        )?;
        if !self.fail_closed_on_policy_store_error {
            return Err(ConfigError::UnsafeSetting {
                field: "execution.fail_closed_on_policy_store_error",
                because: "there is no fail-open path for an unavailable policy source (I19)",
            });
        }
        Ok(())
    }
}

impl Default for ExecutionConfig {
    fn default() -> Self {
        Self::conservative()
    }
}

/// What the runtime records about itself (spec §26).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
#[non_exhaustive]
pub struct ObservabilityConfig {
    /// Whether [`Signal`](turnframe_core::observe::Signal)s are emitted.
    pub emit_metrics: bool,
    /// Whether a [`ReplayRecord`](turnframe_core::replay::ReplayRecord) is
    /// persisted for every turn (I20).
    pub record_replay: bool,
    /// Trace sampling, in per mille, so the configuration stays exactly
    /// comparable and hashable.
    pub trace_sample_per_mille: u16,
}

impl ObservabilityConfig {
    /// Metrics on, replay on, every turn traced.
    #[must_use]
    pub const fn conservative() -> Self {
        Self {
            emit_metrics: true,
            record_replay: true,
            trace_sample_per_mille: 1000,
        }
    }

    /// Returns a copy with another trace sample rate.
    #[must_use]
    pub const fn with_trace_sample_per_mille(mut self, per_mille: u16) -> Self {
        self.trace_sample_per_mille = per_mille;
        self
    }

    fn validate(&self) -> Result<(), ConfigError> {
        if self.trace_sample_per_mille > 1000 {
            return Err(ConfigError::OutOfRange {
                field: "observability.trace_sample_per_mille",
            });
        }
        Ok(())
    }
}

impl Default for ObservabilityConfig {
    fn default() -> Self {
        Self::conservative()
    }
}

/// What may leave the runtime and for how long it is kept (spec §25.5).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
#[non_exhaustive]
pub struct PrivacyConfig {
    /// Whether the user's raw text may be attached to traces and logs.
    pub allow_user_text_in_telemetry: bool,
    /// Whether prompts sent to providers are stored with the replay record.
    pub store_model_prompts: bool,
    /// How long a replay record is kept.
    pub replay_retention_days: u32,
    /// Whether attachment file names are redacted from telemetry.
    pub redact_attachment_filenames: bool,
}

impl PrivacyConfig {
    /// No user text in telemetry, no stored prompts, 90 days of replay,
    /// file names redacted.
    #[must_use]
    pub const fn conservative() -> Self {
        Self {
            allow_user_text_in_telemetry: false,
            store_model_prompts: false,
            replay_retention_days: 90,
            redact_attachment_filenames: true,
        }
    }

    /// Returns a copy with another retention window.
    #[must_use]
    pub const fn with_replay_retention_days(mut self, days: u32) -> Self {
        self.replay_retention_days = days;
        self
    }

    fn validate(&self) -> Result<(), ConfigError> {
        positive(
            "privacy.replay_retention_days",
            u64::from(self.replay_retention_days),
        )
    }
}

impl Default for PrivacyConfig {
    fn default() -> Self {
        Self::conservative()
    }
}

/// Everything the runtime needs to know before it handles a turn (Appendix A).
///
/// Provider routing is deliberately absent: it belongs to the provider layer and
/// this crate depends on `turnframe-core` alone.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
#[non_exhaustive]
pub struct OrchestratorConfig {
    /// How much autonomy the model gets.
    pub mode: OrchestrationMode,
    /// How the turn is understood.
    pub understanding: UnderstandingConfig,
    /// How much of the turn's files reaches a model.
    pub attachments: AttachmentConfig,
    /// How the assistant talks.
    pub narration: NarrationConfig,
    /// How cards behave.
    pub interaction: InteractionConfig,
    /// How commands execute.
    pub execution: ExecutionConfig,
    /// What is measured and recorded.
    pub observability: ObservabilityConfig,
    /// What may leave the runtime.
    pub privacy: PrivacyConfig,
    /// The default effort, and what each level changes.
    #[serde(default)]
    pub effort: crate::effort::EffortConfig,
}

impl OrchestratorConfig {
    /// Every section at its conservative default, in
    /// [`OrchestrationMode::Deterministic`].
    #[must_use]
    pub const fn conservative() -> Self {
        Self {
            mode: OrchestrationMode::Deterministic,
            understanding: UnderstandingConfig::conservative(),
            attachments: AttachmentConfig::conservative(),
            narration: NarrationConfig::conservative(),
            interaction: InteractionConfig::conservative(),
            execution: ExecutionConfig::conservative(),
            observability: ObservabilityConfig::conservative(),
            privacy: PrivacyConfig::conservative(),
            effort: crate::effort::EffortConfig::conservative(),
        }
    }

    /// Returns a copy with another default effort and other level changes.
    #[must_use]
    pub fn with_effort(mut self, effort: crate::effort::EffortConfig) -> Self {
        self.effort = effort;
        self
    }

    /// Returns a copy with another attachment budget.
    #[must_use]
    pub const fn with_attachments(mut self, attachments: AttachmentConfig) -> Self {
        self.attachments = attachments;
        self
    }

    /// Returns a copy in another orchestration mode.
    #[must_use]
    pub fn with_mode(mut self, mode: OrchestrationMode) -> Self {
        self.mode = mode;
        self
    }

    /// Returns a copy with another understanding section.
    #[must_use]
    pub fn with_understanding(mut self, understanding: UnderstandingConfig) -> Self {
        self.understanding = understanding;
        self
    }

    /// Returns a copy with another narration section.
    #[must_use]
    pub const fn with_narration(mut self, narration: NarrationConfig) -> Self {
        self.narration = narration;
        self
    }

    /// Returns a copy with another interaction section.
    #[must_use]
    pub const fn with_interaction(mut self, interaction: InteractionConfig) -> Self {
        self.interaction = interaction;
        self
    }

    /// Returns a copy with another execution section.
    #[must_use]
    pub const fn with_execution(mut self, execution: ExecutionConfig) -> Self {
        self.execution = execution;
        self
    }

    /// Returns a copy with another observability section.
    #[must_use]
    pub const fn with_observability(mut self, observability: ObservabilityConfig) -> Self {
        self.observability = observability;
        self
    }

    /// Returns a copy with another privacy section.
    #[must_use]
    pub const fn with_privacy(mut self, privacy: PrivacyConfig) -> Self {
        self.privacy = privacy;
        self
    }

    /// Tightens `base` with the risk classes the mode refuses (§11.4).
    ///
    /// The result only ever forbids more than `base` did: a mode cannot widen a
    /// policy snapshot, and [`origin_satisfies`](turnframe_core::command::origin_satisfies)
    /// keeps its own floor regardless of either.
    #[must_use]
    pub fn policy_snapshot(&self, base: PolicySnapshot) -> PolicySnapshot {
        let mut snapshot = base;
        for risk in self.mode.forbidden_risk_classes() {
            if !snapshot.forbidden_risk_classes.contains(&risk) {
                snapshot.forbidden_risk_classes.push(risk);
            }
        }
        snapshot.forbidden_risk_classes.sort_unstable();
        snapshot
    }

    /// Checks every section and the rules that span sections.
    ///
    /// Cross-section rules:
    ///
    /// * storing model prompts while the replay record is switched off asks for
    ///   prompts nothing will ever write;
    /// * sandboxed autonomy without a replay record leaves the one mode where
    ///   the model drives writes with no way to explain what it did (I20).
    pub fn validate(&self) -> Result<(), ConfigError> {
        self.mode.validate()?;
        self.understanding.validate()?;
        self.narration.validate()?;
        self.interaction.validate()?;
        self.execution.validate()?;
        self.observability.validate()?;
        self.privacy.validate()?;
        self.validate_effort()?;
        if self.privacy.store_model_prompts && !self.observability.record_replay {
            return Err(ConfigError::Contradiction {
                first: "privacy.store_model_prompts",
                second: "observability.record_replay",
            });
        }
        if self.mode.is_sandboxed() && !self.observability.record_replay {
            return Err(ConfigError::Contradiction {
                first: "mode.sandboxed_autonomous",
                second: "observability.record_replay",
            });
        }
        Ok(())
    }
}

impl Default for OrchestratorConfig {
    fn default() -> Self {
        Self::conservative()
    }
}

impl OrchestratorConfig {
    /// A level's budget replacing the configured one is held to the same bounds.
    fn validate_effort(&self) -> Result<(), ConfigError> {
        use turnframe_core::effort::Effort;
        const FIELDS: [(Effort, [&str; 4]); 3] = [
            (
                Effort::Low,
                [
                    "effort.low.budget.max_parallel",
                    "effort.low.budget.per_call_timeout_secs",
                    "effort.low.reply_budget.max_parallel",
                    "effort.low.reply_budget.per_call_timeout_secs",
                ],
            ),
            (
                Effort::Medium,
                [
                    "effort.medium.budget.max_parallel",
                    "effort.medium.budget.per_call_timeout_secs",
                    "effort.medium.reply_budget.max_parallel",
                    "effort.medium.reply_budget.per_call_timeout_secs",
                ],
            ),
            (
                Effort::High,
                [
                    "effort.high.budget.max_parallel",
                    "effort.high.budget.per_call_timeout_secs",
                    "effort.high.reply_budget.max_parallel",
                    "effort.high.reply_budget.per_call_timeout_secs",
                ],
            ),
        ];
        for (effort, fields) in FIELDS {
            let overrides = self.effort.overrides(effort);
            for (budget, [parallel, timeout]) in [
                (overrides.budget, [fields[0], fields[1]]),
                (overrides.reply_budget, [fields[2], fields[3]]),
            ] {
                if let Some(budget) = budget {
                    positive(parallel, budget.max_parallel as u64)?;
                    positive(timeout, budget.per_call_timeout_secs)?;
                }
            }
        }
        Ok(())
    }
}

fn positive(field: &'static str, value: u64) -> Result<(), ConfigError> {
    if value == 0 {
        Err(ConfigError::MustBePositive { field })
    } else {
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn sandbox() -> OrchestrationMode {
        OrchestrationMode::sandboxed_autonomous(
            ResourceBudget::conservative(),
            SandboxAcknowledgement::i_accept_unreviewed_autonomous_writes(),
        )
    }

    #[test]
    fn mode_risk_table() {
        let sandboxed = sandbox();
        let table = [
            (RiskClass::ReadOnly, true),
            (RiskClass::ReversibleLowRisk, true),
            (RiskClass::SensitiveDataChange, true),
            (RiskClass::Destructive, false),
            (RiskClass::Irreversible, false),
            (RiskClass::ExternalRegulated, false),
        ];
        for (risk, sandboxed_allows) in table {
            assert!(OrchestrationMode::Deterministic.allows_risk(risk));
            assert_eq!(sandboxed.allows_risk(risk), sandboxed_allows, "{risk:?}");
        }
        assert!(
            OrchestrationMode::Deterministic
                .forbidden_risk_classes()
                .is_empty()
        );
    }

    #[test]
    fn mode_shape_helpers() {
        assert!(sandbox().is_sandboxed());
        assert_eq!(sandbox().budget(), Some(&ResourceBudget::conservative()));
        assert_eq!(OrchestrationMode::Deterministic.budget(), None);
        assert_eq!(
            OrchestrationMode::default(),
            OrchestrationMode::Deterministic
        );
    }

    #[test]
    fn a_sandbox_tightens_the_policy_snapshot_and_never_widens_it() {
        let config = OrchestratorConfig::conservative().with_mode(sandbox());
        let snapshot = config.policy_snapshot(PolicySnapshot::conservative());
        assert!(snapshot.is_risk_forbidden(RiskClass::Destructive));
        assert!(snapshot.is_risk_forbidden(RiskClass::ExternalRegulated));
        assert!(!snapshot.is_risk_forbidden(RiskClass::ReversibleLowRisk));
        // A snapshot that already forbade more keeps forbidding it.
        let strict = PolicySnapshot::sandbox();
        let merged = OrchestratorConfig::conservative().policy_snapshot(strict.clone());
        assert_eq!(
            merged.forbidden_risk_classes.len(),
            strict.forbidden_risk_classes.len()
        );
        assert!(merged.is_risk_forbidden(RiskClass::SensitiveDataChange));
    }

    #[test]
    fn the_acknowledgement_is_the_only_way_into_the_sandbox() {
        let json = serde_json::to_value(sandbox()).unwrap();
        assert_eq!(json["kind"], "sandboxed_autonomous");
        assert_eq!(json["acknowledgement"], SANDBOX_ACKNOWLEDGEMENT);
        let back: OrchestrationMode = serde_json::from_value(json).unwrap();
        assert_eq!(back, sandbox());
        let mut wrong = serde_json::to_value(sandbox()).unwrap();
        wrong["acknowledgement"] = serde_json::json!("sure why not");
        assert!(serde_json::from_value::<OrchestrationMode>(wrong).is_err());
        let mut missing = serde_json::to_value(sandbox()).unwrap();
        missing
            .as_object_mut()
            .unwrap()
            .remove("acknowledgement")
            .unwrap();
        assert!(serde_json::from_value::<OrchestrationMode>(missing).is_err());
        assert_eq!(
            format!(
                "{:?}",
                SandboxAcknowledgement::i_accept_unreviewed_autonomous_writes()
            ),
            SANDBOX_ACKNOWLEDGEMENT
        );
    }

    #[test]
    fn conservative_config_validates_and_round_trips() {
        let config = OrchestratorConfig::conservative();
        assert_eq!(config.validate(), Ok(()));
        assert_eq!(config, OrchestratorConfig::default());
        let json = serde_json::to_string(&config).unwrap();
        let back: OrchestratorConfig = serde_json::from_str(&json).unwrap();
        assert_eq!(back, config);
        assert!(serde_json::from_str::<OrchestratorConfig>(r#"{"extra": 1}"#).is_err());
        // Nothing is bounded unless a deployment bounds it.
        assert_eq!(config.understanding.max_acts(), None);
        assert_eq!(config.understanding.max_questions(), None);
    }

    #[test]
    fn config_validation_table() {
        let base = OrchestratorConfig::conservative();
        let cases: Vec<(&str, OrchestratorConfig, Option<ConfigError>)> = vec![
            ("conservative", base.clone(), None),
            (
                "no acts allowed",
                base.clone().with_understanding(
                    UnderstandingConfig::conservative()
                        .with_plan_limits(PlanLimits::conservative().with_max_acts(Some(0))),
                ),
                Some(ConfigError::MustBePositive {
                    field: "understanding.plan_limits.max_acts",
                }),
            ),
            (
                "silent answers",
                base.clone().with_narration(NarrationConfig {
                    max_answer_chars: Some(0),
                    ..NarrationConfig::conservative()
                }),
                Some(ConfigError::MustBePositive {
                    field: "narration.max_answer_chars",
                }),
            ),
            (
                "selection with one candidate",
                base.clone().with_interaction(
                    InteractionConfig::conservative().with_max_selection_candidates(1),
                ),
                Some(ConfigError::MustBePositive {
                    field: "interaction.max_selection_candidates",
                }),
            ),
            (
                "cards expiring instantly",
                base.clone().with_interaction(
                    InteractionConfig::conservative().with_default_ttl(Some(Duration::ZERO)),
                ),
                Some(ConfigError::MustBePositive {
                    field: "interaction.default_ttl",
                }),
            ),
            (
                "no command budget",
                base.clone()
                    .with_execution(ExecutionConfig::conservative().with_max_commands_per_turn(0)),
                Some(ConfigError::MustBePositive {
                    field: "execution.max_commands_per_turn",
                }),
            ),
            (
                "instant timeout",
                base.clone().with_execution(
                    ExecutionConfig::conservative().with_default_timeout(Duration::ZERO),
                ),
                Some(ConfigError::MustBePositive {
                    field: "execution.default_timeout",
                }),
            ),
            (
                "fail open on policy",
                base.clone().with_execution(ExecutionConfig {
                    fail_closed_on_policy_store_error: false,
                    ..ExecutionConfig::conservative()
                }),
                Some(ConfigError::UnsafeSetting {
                    field: "execution.fail_closed_on_policy_store_error",
                    because: "there is no fail-open path for an unavailable policy source (I19)",
                }),
            ),
            (
                "impossible sampling",
                base.clone().with_observability(
                    ObservabilityConfig::conservative().with_trace_sample_per_mille(1001),
                ),
                Some(ConfigError::OutOfRange {
                    field: "observability.trace_sample_per_mille",
                }),
            ),
            (
                "no retention",
                base.clone()
                    .with_privacy(PrivacyConfig::conservative().with_replay_retention_days(0)),
                Some(ConfigError::MustBePositive {
                    field: "privacy.replay_retention_days",
                }),
            ),
            (
                "prompts stored but no replay",
                base.clone()
                    .with_privacy(PrivacyConfig {
                        store_model_prompts: true,
                        ..PrivacyConfig::conservative()
                    })
                    .with_observability(ObservabilityConfig {
                        record_replay: false,
                        ..ObservabilityConfig::conservative()
                    }),
                Some(ConfigError::Contradiction {
                    first: "privacy.store_model_prompts",
                    second: "observability.record_replay",
                }),
            ),
            (
                "sandbox without replay",
                base.clone()
                    .with_mode(sandbox())
                    .with_observability(ObservabilityConfig {
                        record_replay: false,
                        ..ObservabilityConfig::conservative()
                    }),
                Some(ConfigError::Contradiction {
                    first: "mode.sandboxed_autonomous",
                    second: "observability.record_replay",
                }),
            ),
            (
                "empty sandbox budget",
                base.with_mode(OrchestrationMode::sandboxed_autonomous(
                    ResourceBudget::conservative().with_max_model_calls(0),
                    SandboxAcknowledgement::i_accept_unreviewed_autonomous_writes(),
                )),
                Some(ConfigError::MustBePositive {
                    field: "mode.budget.max_model_calls",
                }),
            ),
        ];
        for (name, config, expected) in cases {
            assert_eq!(config.validate().err(), expected, "case {name}");
        }
    }

    #[test]
    fn budget_builders_and_defaults() {
        let budget = ResourceBudget::default()
            .with_max_read_calls(3)
            .with_max_prompt_tokens(10)
            .with_max_wall_clock(Duration::from_secs(5));
        assert_eq!(budget.max_read_calls, 3);
        assert_eq!(budget.max_prompt_tokens, 10);
        assert_eq!(budget.max_wall_clock, Duration::from_secs(5));
        assert_eq!(budget.max_model_calls, 8);
        assert_eq!(
            ResourceBudget::conservative()
                .with_max_wall_clock(Duration::ZERO)
                .validate(),
            Err(ConfigError::MustBePositive {
                field: "mode.budget.max_wall_clock"
            })
        );
    }

    #[test]
    fn section_builders_keep_the_rest() {
        let narration = NarrationConfig::conservative()
            .with_tone(ToneProfile::Neutral)
            .with_default_source_policy(SourcePolicy::AuthoritativeOnly);
        assert_eq!(
            narration.default_source_policy,
            SourcePolicy::AuthoritativeOnly
        );
        assert!(narration.enabled);
        let understanding =
            UnderstandingConfig::conservative().with_turn_limits(TurnLimits::conservative());
        assert_eq!(understanding.budget, Budget::understanding());
        assert_eq!(
            UnderstandingConfig::default(),
            UnderstandingConfig::conservative()
        );
        assert_eq!(NarrationConfig::default(), NarrationConfig::conservative());
        assert_eq!(
            InteractionConfig::default(),
            InteractionConfig::conservative()
        );
        assert_eq!(ExecutionConfig::default(), ExecutionConfig::conservative());
        assert_eq!(
            ObservabilityConfig::default(),
            ObservabilityConfig::conservative()
        );
        assert_eq!(PrivacyConfig::default(), PrivacyConfig::conservative());
    }
}