leviath-core 0.6.3

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

use serde::{Deserialize, Serialize};

/// Largest final output kept, in bytes. Anything longer is cut at a character
/// boundary and flagged [`FinalOutput::truncated`].
///
/// Sits between the log tail the result endpoint already serves (64 KiB) and the
/// cap on reading a file the run wrote (1 MiB). A final output is meant to be an
/// answer, not a payload; an agent with megabytes to hand back should write a
/// file and say where it is.
pub const MAX_FINAL_OUTPUT_BYTES: usize = 256 * 1024;

/// What happens to a submission when its Rhai validator cannot run: the script
/// threw, exhausted its operation budget, or returned something that is neither
/// `()` nor a string.
///
/// Distinct from the validator *rejecting* the answer, which always refuses the
/// submission back to the model. This knob is only about the script itself
/// failing.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum OnValidatorError {
    /// Refuse the submission, sending the script's error text to the model as
    /// retry feedback. The default: an answer nothing checked must not ship as
    /// if it passed, and a `parse_json` throw on malformed output is something
    /// the model can act on.
    #[default]
    Reject,
    /// Record the submission unchecked, as if no validator were declared. For
    /// blueprints that would rather have an unchecked answer than a failed run.
    /// The broken script is still flagged on the run either way.
    Accept,
}

/// What shape an agent should return.
///
/// Declared by a blueprint (`[agent.output]`), narrowed by a stage
/// (`[stages.<name>.output]`), and overridable by whoever starts the run. See
/// [`resolve_output_spec`] for how the three combine.
///
/// Every field is optional, and an entirely empty spec is meaningful: it asks
/// for a final output without constraining its shape.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct OutputSpec {
    /// An opaque label for the shape, carried to the model and recorded beside
    /// the result. `"markdown"`, `"json"`, `"a2ui"`, and
    /// `"application/vnd.acme.report+xml"` are all equally valid and equally
    /// uninterpreted. Consumers that render differently per format (a browser
    /// UI, say) match on this string; the engine never does.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub format: Option<String>,

    /// Free-form guidance folded into the `submit_output` tool description and
    /// the output stage's system prompt. This is where a format that the model
    /// has never seen gets explained.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub instructions: Option<String>,

    /// A literal sample shown to the model verbatim. The most effective lever
    /// for an unusual format, and the reason one needs no code support.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub example: Option<String>,

    /// A JSON Schema describing the answer's shape. When present, a submission
    /// is parsed as JSON and validated against it, and a failure is refused back
    /// to the model so it can correct itself.
    ///
    /// Separate from `format` because they answer different questions.
    /// `format = "json"` asks "does this parse as JSON"; a schema asks "does the
    /// parsed document have the fields I need". A format check comes free for
    /// the handful of formats the engine can parse; shape is only ever checked
    /// when someone writes a schema down.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub schema: Option<serde_json::Value>,

    /// A `.rhai` script that decides whether an answer is valid, as a path
    /// relative to the blueprint directory.
    ///
    /// For a format the engine cannot parse and a shape a JSON Schema cannot
    /// describe. The script defines `fn validate(content)` and returns `()` when
    /// the answer is fine or a string saying what is wrong; the string goes back
    /// to the agent as the same refusal a schema failure produces.
    ///
    /// Written for the format it accompanies, so a caller who overrides the
    /// format retires it along with the schema.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub validator: Option<String>,

    /// What to do when the validator itself cannot run. `None` means the
    /// default, [`OnValidatorError::Reject`]. Travels with the validator: a
    /// caller who retires the validator by overriding the format retires this
    /// setting along with it.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub on_validator_error: Option<OnValidatorError>,

    /// Whether an artifact named after a part the run produced may replace a
    /// different file already at that path in the working directory. `None`
    /// defers to the user's `[mime] overwrite_artifacts`, which defaults to
    /// false: the existing file is left alone and the part is written beside
    /// it under a name carrying its hash.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub overwrite_artifacts: Option<bool>,

    /// The files the stage hands back beside its answer, by name and type.
    /// A submission is checked against them: a `required` one must be
    /// present, and one that is present must be of the declared type.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub artifacts: Vec<ArtifactSpec>,
}

impl OutputSpec {
    /// Whether this spec constrains anything at all. An empty spec still asks
    /// for an output, so this is about wording the request, not skipping it.
    pub fn is_empty(&self) -> bool {
        self.format.is_none()
            && self.instructions.is_none()
            && self.example.is_none()
            && self.schema.is_none()
            && self.validator.is_none()
            && self.on_validator_error.is_none()
            && self.overwrite_artifacts.is_none()
            && self.artifacts.is_empty()
    }
}

/// A file a stage declares it hands back beside its answer.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ArtifactSpec {
    /// What the submission calls it: `final`, `scene`, `track`.
    pub name: String,
    /// The mime type it must be, or a pattern it must match (`video/*`).
    #[serde(rename = "type")]
    pub mime_type: String,
    /// Whether a submission without it is refused.
    #[serde(default)]
    pub required: bool,
    /// What it is for, shown to the model.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
}

/// A file a run produced, as recorded on its answer.
///
/// Every field but `path` is what the run could tell from the bytes: the
/// registry's type (or the declared one), the size, and the hash the run's
/// blob store holds the file under. An answer recorded before artifacts were
/// typed carried a bare path, and reads back as one with the rest unknown.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Artifact {
    /// The name the stage declared, or the file name when it declared none.
    pub name: String,
    /// The file, relative to the working directory. An answer emitted from
    /// routed parts alone never wrote a file, and carries the part's name here
    /// instead; its bytes are reachable only through `sha256`.
    pub path: String,
    /// The file's type.
    pub mime_type: crate::mime::MimeType,
    /// Size in bytes.
    #[serde(default)]
    pub size: u64,
    /// The sha256 the run's blob store holds the bytes under; empty when the
    /// file was too large to store, or the answer predates typed artifacts.
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub sha256: String,
}

impl Artifact {
    /// `name (type, size)`: how a produced file reads in a sentence.
    pub fn short_label(&self) -> String {
        format!(
            "{} ({}, {})",
            self.name,
            self.mime_type,
            crate::mime::human_size(self.size)
        )
    }

    /// The columns after the name in a listing: path, type, size, and the
    /// hash prefix when the bytes are stored.
    pub fn detail_columns(&self) -> String {
        let sha = match self.sha256.is_empty() {
            true => String::new(),
            false => format!(
                "  sha256:{}",
                self.sha256.chars().take(12).collect::<String>()
            ),
        };
        format!(
            "{}  {}  {}{sha}",
            self.path,
            self.mime_type,
            crate::mime::human_size(self.size)
        )
    }

    /// An artifact known only by its path: the shape every answer recorded
    /// before artifacts were typed carried.
    pub fn from_path(path: &str) -> Self {
        let name = path
            .rsplit(['/', '\\'])
            .find(|s| !s.is_empty())
            .unwrap_or(path)
            .to_string();
        Self {
            name,
            path: path.to_string(),
            mime_type: crate::mime::octet_stream(),
            size: 0,
            sha256: String::new(),
        }
    }
}

/// One artifact on the wire: the typed record, or the bare path older
/// answers wrote.
#[derive(Deserialize)]
#[serde(untagged)]
enum ArtifactWire {
    Full(Artifact),
    Path(String),
}

/// Read an artifacts list that may hold bare paths.
fn artifacts_from_wire<'de, D: serde::Deserializer<'de>>(d: D) -> Result<Vec<Artifact>, D::Error> {
    let listed: Vec<ArtifactWire> = Vec::deserialize(d)?;
    Ok(listed
        .into_iter()
        .map(|a| match a {
            ArtifactWire::Full(a) => a,
            ArtifactWire::Path(p) => Artifact::from_path(&p),
        })
        .collect())
}

/// What an agent actually produced, content included.
///
/// [`content`](Self::content) is stored exactly as submitted. Nothing in the
/// engine reformats, re-indents, or re-serializes it, so a consumer that asked
/// for a particular byte sequence receives that byte sequence.
///
/// This is the in-memory and one-shot form: the live ECS component, the
/// completion event, a webhook body, a reply to a waiting parent. What a run's
/// `meta.json` carries is the [`FinalOutputDescriptor`], because that file is
/// read for every run on every listing and must not carry a payload.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct FinalOutput {
    /// The submission, verbatim (subject only to [`MAX_FINAL_OUTPUT_BYTES`]).
    pub content: String,

    /// The format label in effect when this was submitted, if any. Copied from
    /// the resolved spec rather than guessed from the content.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub format: Option<String>,

    /// The stage that produced it. Read by the enforcement gate, which must
    /// tell "this stage submitted" from "some earlier stage did".
    pub stage: String,

    /// Unix seconds at submission.
    pub submitted_at: i64,

    /// Whether [`MAX_FINAL_OUTPUT_BYTES`] cut the content short.
    #[serde(default)]
    pub truncated: bool,

    /// Files the run produced, typed and hashed.
    ///
    /// An answer is one model response; anything larger is a file. A run that
    /// gathers two million rows writes them incrementally and names the file
    /// here, so a consumer can fetch it rather than parse the path out of prose.
    /// Validated to resolve inside the run's working directory, the same rule
    /// the files endpoint enforces when serving one.
    #[serde(
        default,
        skip_serializing_if = "Vec::is_empty",
        deserialize_with = "artifacts_from_wire"
    )]
    pub artifacts: Vec<Artifact>,
}

impl FinalOutput {
    /// Record a submission, truncating at a character boundary if it exceeds
    /// [`MAX_FINAL_OUTPUT_BYTES`].
    ///
    /// Truncation walks back to a boundary rather than slicing by byte index:
    /// this workspace denies `clippy::string_slice` because a byte cut through a
    /// multi-byte character once double-panicked and aborted the whole daemon.
    pub fn new(content: &str, format: Option<String>, stage: String, submitted_at: i64) -> Self {
        let truncated = content.len() > MAX_FINAL_OUTPUT_BYTES;
        let kept = crate::text::truncate_at_boundary(content, MAX_FINAL_OUTPUT_BYTES);
        Self {
            content: kept.to_string(),
            format,
            stage,
            submitted_at,
            truncated,
            artifacts: Vec::new(),
        }
    }

    /// The same submission with `artifacts` attached.
    pub fn with_artifacts(mut self, artifacts: Vec<Artifact>) -> Self {
        self.artifacts = artifacts;
        self
    }

    /// Everything about this answer except the bytes.
    pub fn descriptor(&self) -> FinalOutputDescriptor {
        FinalOutputDescriptor {
            format: self.format.clone(),
            stage: self.stage.clone(),
            submitted_at: self.submitted_at,
            bytes: self.content.len(),
            truncated: self.truncated,
            artifacts: self.artifacts.clone(),
        }
    }
}

/// What a run's `meta.json` records about its answer: everything but the bytes.
///
/// The content lives beside it in a sidecar file
/// ([`FINAL_OUTPUT_FILE`]). `meta.json` is
/// parsed for every run on every `lev ps`, every `/api/runs` page, and every
/// restart scan, so a payload in it is paid for by operations that never wanted
/// it: a thousand answered runs would mean hundreds of megabytes of JSON per
/// listing. A descriptor is a couple of hundred bytes and stays that way.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct FinalOutputDescriptor {
    /// The format label the answer was produced under, if any.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub format: Option<String>,
    /// The stage that produced it.
    pub stage: String,
    /// Unix seconds at submission.
    pub submitted_at: i64,
    /// Size of the answer in bytes, so a caller can decide whether to fetch it.
    #[serde(default)]
    pub bytes: usize,
    /// Whether [`MAX_FINAL_OUTPUT_BYTES`] cut the answer short.
    #[serde(default)]
    pub truncated: bool,
    /// Files the run produced, typed and hashed.
    #[serde(
        default,
        skip_serializing_if = "Vec::is_empty",
        deserialize_with = "artifacts_from_wire"
    )]
    pub artifacts: Vec<Artifact>,
}

/// The file, inside a run's directory, holding the answer's bytes.
///
/// Raw content with no wrapper, so serving it is a read and `lev result --raw`
/// is a copy.
pub const FINAL_OUTPUT_FILE: &str = "final_output";

/// Combine the blueprint's, the stage's, and the caller's output specs into the
/// one that governs a stage. Later levels win field by field, the way
/// [`resolve_nudge`](crate::blueprint::resolve_nudge) cascades.
///
/// Returns `None` when no level asks for an output at all, which is how a stage
/// that has nothing to hand back stays silent.
///
/// # The schema drop
///
/// A caller who names a `format` and supplies no `schema` **drops the declared
/// schema**. Validating an a2ui document against the agent's own JSON schema
/// would be nonsense: the caller asked for a different shape, so the check
/// written for the old shape no longer applies. A caller who wants validation
/// supplies a schema alongside the format. This is the one place where fields do
/// not cascade independently, and it is deliberate.
pub fn resolve_output_spec(
    agent: Option<&OutputSpec>,
    stage: Option<&OutputSpec>,
    request: Option<&OutputSpec>,
) -> Option<OutputSpec> {
    if agent.is_none() && stage.is_none() && request.is_none() {
        return None;
    }

    fn field<T: Clone>(
        agent: Option<&OutputSpec>,
        stage: Option<&OutputSpec>,
        request: Option<&OutputSpec>,
        get: impl Fn(&OutputSpec) -> Option<T>,
    ) -> Option<T> {
        request
            .and_then(&get)
            .or_else(|| stage.and_then(&get))
            .or_else(|| agent.and_then(&get))
    }

    // A shape check is written for one format. When a caller asks for a
    // different one, a check the blueprint declared no longer describes what is
    // being produced, so it is retired rather than applied to something it was
    // never about. A caller who wants their new shape checked supplies their own.
    let declared_format = field(agent, stage, None, |s| s.format.clone());
    let requested_format = request.and_then(|r| r.format.clone());
    let reshaped = requested_format.is_some() && requested_format != declared_format;

    let shape_field = |get: fn(&OutputSpec) -> Option<serde_json::Value>| match reshaped {
        true => request.and_then(get),
        false => field(agent, stage, request, get),
    };
    let validator = match reshaped {
        true => request.and_then(|r| r.validator.clone()),
        false => field(agent, stage, request, |s| s.validator.clone()),
    };
    // The error policy accompanies the validator it is about, so it follows the
    // validator's cascade: retired with it on a reshape, inherited otherwise.
    let on_validator_error = match reshaped {
        true => request.and_then(|r| r.on_validator_error),
        false => field(agent, stage, request, |s| s.on_validator_error),
    };

    // Declared artifacts describe the declared shape, so they go with the
    // schema and the validator when a caller reshapes. Otherwise the nearest
    // non-empty list wins whole: a stage that names its own files replaces
    // the agent's list rather than adding to it.
    let artifacts = match reshaped {
        true => request.map(|r| r.artifacts.clone()).unwrap_or_default(),
        false => field(agent, stage, request, |s| {
            (!s.artifacts.is_empty()).then(|| s.artifacts.clone())
        })
        .unwrap_or_default(),
    };

    Some(OutputSpec {
        format: field(agent, stage, request, |s| s.format.clone()),
        instructions: field(agent, stage, request, |s| s.instructions.clone()),
        example: field(agent, stage, request, |s| s.example.clone()),
        schema: shape_field(|s| s.schema.clone()),
        validator,
        on_validator_error,
        overwrite_artifacts: field(agent, stage, request, |s| s.overwrite_artifacts),
        artifacts,
    })
}

/// The warnings a caller's requested output shape earns at spawn: what
/// [`resolve_output_spec`] will retire, said out loud before it happens.
///
/// Retiring the declared Rhai validator and JSON schema when the request names
/// a different format is deliberate and stays: a check written for one shape
/// cannot judge another. These warnings say so out loud, so a caller who types
/// `--output-format json` over a blueprint with a validator does not go on
/// believing the run is still being checked.
///
/// One line per group of stages losing the same checks, so an agent-level
/// validator shared by four stages reads as one sentence naming four stages.
/// Empty when there is nothing to say: no request, no format in it, the
/// declared format re-stated (which retires nothing), or nothing declared that
/// could be retired. A declared schema the request *replaces* with its own is
/// also not warned about: supplying a schema for the new shape is exactly what
/// the warning would have asked for. A declared validator is always worth the
/// line, because no request can bring a replacement for it.
pub fn retired_check_warnings(
    blueprint: &crate::Blueprint,
    request: Option<&OutputSpec>,
) -> Vec<String> {
    let Some(requested) = request.and_then(|r| r.format.as_deref()) else {
        return Vec::new();
    };
    let request_has_schema = request.is_some_and(|r| r.schema.is_some());
    let mut groups: Vec<(RetiredChecks, Vec<String>)> = Vec::new();
    for stage in &blueprint.stages {
        let Some(retired) = retired_checks_for_stage(
            blueprint.output.as_ref(),
            stage.output.as_ref(),
            requested,
            request_has_schema,
        ) else {
            continue;
        };
        match groups.iter_mut().find(|(g, _)| *g == retired) {
            Some((_, stages)) => stages.push(stage.name.clone()),
            None => groups.push((retired, vec![stage.name.clone()])),
        }
    }
    groups
        .iter()
        .map(|(checks, stages)| checks.warning_line(requested, stages, request_has_schema))
        .collect()
}

/// The declared checks one stage loses to a format override. Two stages with
/// equal values lose the same thing and share one warning line.
#[derive(PartialEq, Eq)]
struct RetiredChecks {
    /// The format the retired checks were written for, when one was declared.
    declared_format: Option<String>,
    /// The retired Rhai validator's path, when one was declared.
    validator: Option<String>,
    /// Whether a declared JSON schema is retired with nothing in its place.
    schema: bool,
}

/// What `requested` retires for one stage, or `None` when it retires nothing.
///
/// Asks [`resolve_output_spec`]'s question ahead of time, with the same
/// cascade: the stage's declaration wins over the agent's, and re-stating the
/// declared format keeps every check. Kept beside it so the two cannot drift.
fn retired_checks_for_stage(
    agent: Option<&OutputSpec>,
    stage: Option<&OutputSpec>,
    requested: &str,
    request_has_schema: bool,
) -> Option<RetiredChecks> {
    let declared =
        |get: fn(&OutputSpec) -> Option<&str>| stage.and_then(get).or_else(|| agent.and_then(get));
    let declared_format = declared(|s| s.format.as_deref());
    if declared_format == Some(requested) {
        return None;
    }
    let validator = declared(|s| s.validator.as_deref());
    let schema = !request_has_schema
        && stage
            .and_then(|s| s.schema.as_ref())
            .or_else(|| agent.and_then(|a| a.schema.as_ref()))
            .is_some();
    if validator.is_none() && !schema {
        return None;
    }
    Some(RetiredChecks {
        declared_format: declared_format.map(str::to_string),
        validator: validator.map(str::to_string),
        schema,
    })
}

impl RetiredChecks {
    /// The warning itself, worded for a person on any spawn path: what was
    /// requested, what it retires, and how to get the new shape checked. The
    /// closing advice depends on the request: a caller who already brought a
    /// schema for the new shape has nothing further to supply.
    fn warning_line(&self, requested: &str, stages: &[String], request_has_schema: bool) -> String {
        let cause = match &self.declared_format {
            Some(declared) => format!(
                "requested output format '{requested}' differs from the declared '{declared}'"
            ),
            None => format!(
                "requested output format '{requested}' reshapes an output declared without a \
                 format"
            ),
        };
        let what = match (&self.validator, self.schema) {
            (Some(v), true) => format!("the Rhai validator '{v}' and the JSON schema"),
            (Some(v), false) => format!("the Rhai validator '{v}'"),
            (None, _) => "the JSON schema".to_string(),
        };
        let tail = match request_has_schema {
            true => "the schema supplied with the request is what checks the answer now",
            false => {
                "nothing checks the answer's shape; supply a schema with the request if the new \
                 shape needs one"
            }
        };
        format!(
            "{cause}: {what} declared for {} will not run, because a check written for one shape \
             cannot judge another. Instead, {tail}.",
            stage_phrase(stages)
        )
    }
}

/// `stage 'plan'`, `stages 'plan' and 'wrap'`, `stages 'a', 'b', and 'c'`.
/// Callers only group stages they saw, so the slice is never empty.
fn stage_phrase(stages: &[String]) -> String {
    let quoted: Vec<String> = stages.iter().map(|s| format!("'{s}'")).collect();
    match quoted.split_last() {
        Some((last, [])) => format!("stage {last}"),
        Some((last, [first])) => format!("stages {first} and {last}"),
        Some((last, head)) => format!("stages {}, and {last}", head.join(", ")),
        // Unreachable by construction; an empty phrase keeps the sentence
        // grammatical if a future caller ever passes one.
        None => "its stages".to_string(),
    }
}

/// Render a resolved spec as the guidance an agent reads.
///
/// Used twice for the same text: once in the `submit_output` tool description
/// and once in an output stage's system prompt. Saying it in both places matters
/// most for a format the model has no prior knowledge of, which is exactly the
/// case this module is built to support.
///
/// A constrained spec closes with a precedence sentence, because without one
/// this text and the stage's own system prompt are two peer instructions and
/// which wins is model-dependent: a stage prompt saying "lead with the
/// diagnosis" beats `--output-instructions "reply with only the integer"` on
/// some models and loses on others. By the time this runs, [`resolve_output_spec`]
/// has already picked one winner per field - a caller's flag replaces the
/// blueprint's line rather than joining it - so there is exactly one shape here
/// and it is the one that should govern. The sentence is scoped to presentation
/// so a bare `format` does not read as licence to drop content.
///
/// Returns an empty string for a spec that constrains nothing, so callers can
/// append it unconditionally.
pub fn describe_spec(spec: &OutputSpec) -> String {
    let mut parts = Vec::new();
    if let Some(format) = &spec.format {
        parts.push(format!("Return it in this format: {format}."));
    }
    if let Some(instructions) = &spec.instructions {
        parts.push(instructions.clone());
    }
    if let Some(schema) = &spec.schema {
        parts.push(format!(
            "It must be JSON valid against this schema:\n{schema}"
        ));
    }
    if let Some(example) = &spec.example {
        parts.push(format!(
            "Here is an example of the expected shape:\n{example}"
        ));
    }
    if !spec.artifacts.is_empty() {
        let listed: Vec<String> = spec
            .artifacts
            .iter()
            .map(|a| {
                let mut line = format!("- {} ({}", a.name, a.mime_type);
                if a.required {
                    line.push_str(", required");
                }
                line.push(')');
                if let Some(d) = &a.description {
                    line.push_str(": ");
                    line.push_str(d);
                }
                line
            })
            .collect();
        parts.push(format!(
            "Hand back these files in `artifacts`, each as {{ name, path }} with the name \
             given here and the path of the file you wrote:\n{}",
            listed.join("\n")
        ));
    }
    if !parts.is_empty() {
        parts.push(
            "This governs how the answer is presented. Where anything else you were told says \
             to present it differently - its length, its structure, what to lead with - follow \
             this."
                .to_string(),
        );
    }
    parts.join("\n\n")
}

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

    fn spec(format: Option<&str>, schema: Option<serde_json::Value>) -> OutputSpec {
        OutputSpec {
            format: format.map(str::to_string),
            schema,
            ..OutputSpec::default()
        }
    }

    /// The artifacts list is how an answer points at what it could never
    /// contain: a dataset, a report, a directory of generated files. It travels
    /// with the descriptor so a caller can fetch them without parsing paths back
    /// out of prose.
    #[test]
    fn artifacts_attach_to_a_submission_and_reach_the_descriptor() {
        let output = FinalOutput::new(
            "the summary",
            Some("markdown".to_string()),
            "present".to_string(),
            42,
        )
        .with_artifacts(vec![
            Artifact::from_path("data/dataset.csv"),
            Artifact::from_path("report.pdf"),
        ]);

        assert_eq!(output.artifacts[0].path, "data/dataset.csv");
        assert_eq!(output.artifacts[0].name, "dataset.csv");
        assert_eq!(output.artifacts[1].name, "report.pdf");
        assert_eq!(output.descriptor().artifacts, output.artifacts);
        // An answer recorded before artifacts were typed carried bare paths.
        let old: FinalOutputDescriptor = serde_json::from_str(
            "{\"stage\":\"s\",\"submitted_at\":1,\"artifacts\":[\"a/b.csv\",{\"name\":\"final\",\"path\":\"out.mp4\",\"mime_type\":\"video/mp4\",\"size\":9}]}",
        )
        .unwrap();
        assert_eq!(old.artifacts[0].name, "b.csv");
        assert_eq!(
            old.artifacts[0].mime_type.as_str(),
            "application/octet-stream"
        );
        assert_eq!(old.artifacts[1].name, "final");
        assert_eq!(old.artifacts[1].size, 9);
        assert_eq!(Artifact::from_path("").name, "");
        assert!(
            serde_json::from_str::<FinalOutputDescriptor>(
                "{\"stage\":\"s\",\"submitted_at\":1,\"artifacts\":5}"
            )
            .is_err()
        );
        assert_eq!(Artifact::from_path("dir\\x.png").name, "x.png");
        // The bytes stay out of the descriptor: it goes in `meta.json`, which is
        // read for every run in a listing.
        assert_eq!(output.descriptor().bytes, "the summary".len());
    }

    #[test]
    fn a_submission_carries_no_artifacts_unless_given_some() {
        assert!(
            FinalOutput::new("x", None, "present".to_string(), 0)
                .artifacts
                .is_empty()
        );
    }

    #[test]
    fn declared_artifacts_cascade_whole_and_retire_on_a_reshape() {
        let art = |name: &str| ArtifactSpec {
            name: name.to_string(),
            mime_type: "video/*".to_string(),
            required: true,
            description: None,
        };
        let agent = OutputSpec {
            format: Some("markdown".to_string()),
            artifacts: vec![art("agent-file")],
            ..OutputSpec::default()
        };
        let stage = OutputSpec {
            artifacts: vec![art("stage-file")],
            ..OutputSpec::default()
        };
        // The nearest non-empty list, whole.
        let resolved = resolve_output_spec(Some(&agent), Some(&stage), None).unwrap();
        assert_eq!(resolved.artifacts[0].name, "stage-file");
        let resolved = resolve_output_spec(Some(&agent), None, None).unwrap();
        assert_eq!(resolved.artifacts[0].name, "agent-file");
        // A reshaping request retires them with the schema and validator.
        let request = OutputSpec {
            format: Some("json".to_string()),
            ..OutputSpec::default()
        };
        let resolved = resolve_output_spec(Some(&agent), Some(&stage), Some(&request)).unwrap();
        assert!(resolved.artifacts.is_empty());
        // Unless it brings its own.
        let request = OutputSpec {
            format: Some("json".to_string()),
            artifacts: vec![art("request-file")],
            ..OutputSpec::default()
        };
        let resolved = resolve_output_spec(Some(&agent), Some(&stage), Some(&request)).unwrap();
        assert_eq!(resolved.artifacts[0].name, "request-file");
        assert!(!agent.is_empty());
    }

    #[test]
    fn empty_spec_constrains_nothing() {
        assert!(OutputSpec::default().is_empty());
        assert!(!spec(Some("json"), None).is_empty());
        assert!(!spec(None, Some(json!({}))).is_empty());
        assert!(
            !OutputSpec {
                instructions: Some("be brief".to_string()),
                ..OutputSpec::default()
            }
            .is_empty()
        );
        assert!(
            !OutputSpec {
                example: Some("<doc/>".to_string()),
                ..OutputSpec::default()
            }
            .is_empty()
        );
        assert!(
            !OutputSpec {
                on_validator_error: Some(OnValidatorError::Accept),
                ..OutputSpec::default()
            }
            .is_empty()
        );
    }

    #[test]
    fn no_level_asking_for_output_resolves_to_none() {
        assert_eq!(resolve_output_spec(None, None, None), None);
    }

    #[test]
    fn later_levels_win_field_by_field() {
        let agent = OutputSpec {
            format: Some("markdown".to_string()),
            instructions: Some("agent guidance".to_string()),
            example: Some("agent example".to_string()),
            schema: None,
            validator: None,
            on_validator_error: None,
            overwrite_artifacts: None,
            artifacts: Vec::new(),
        };
        let stage = OutputSpec {
            instructions: Some("stage guidance".to_string()),
            ..OutputSpec::default()
        };
        let resolved = resolve_output_spec(Some(&agent), Some(&stage), None)
            .expect("some level asked for an output");
        // The stage narrows one field; the rest fall through to the agent.
        assert_eq!(resolved.instructions.as_deref(), Some("stage guidance"));
        assert_eq!(resolved.format.as_deref(), Some("markdown"));
        assert_eq!(resolved.example.as_deref(), Some("agent example"));
    }

    /// The listing columns every surface prints: the hash prefix rides along
    /// only when the bytes were stored.
    #[test]
    fn an_artifact_lists_its_columns_with_the_hash_only_when_stored() {
        let mut artifact = Artifact::from_path("out/hero.png");
        assert!(
            artifact
                .short_label()
                .starts_with("hero.png (application/octet-stream, ")
        );
        let bare = artifact.detail_columns();
        assert!(
            bare.starts_with("out/hero.png  application/octet-stream  "),
            "{bare}"
        );
        assert!(!bare.contains("sha256"));
        artifact.sha256 = "abcdef0123456789".repeat(4);
        assert!(artifact.detail_columns().ends_with("  sha256:abcdef012345"));
    }

    #[test]
    fn a_stage_alone_can_ask_for_an_output() {
        let stage = spec(Some("a2ui"), None);
        let resolved =
            resolve_output_spec(None, Some(&stage), None).expect("the stage asked for one");
        assert_eq!(resolved.format.as_deref(), Some("a2ui"));
    }

    /// Naming the format the blueprint already declared keeps its schema: a
    /// caller who asks for exactly what is on offer must not lose the check
    /// that comes with it.
    #[test]
    fn re_stating_the_declared_format_keeps_its_shape_checks() {
        let agent = OutputSpec {
            format: Some("json".to_string()),
            schema: Some(json!({"type": "object"})),
            validator: Some("v.rhai".to_string()),
            ..OutputSpec::default()
        };
        let request = spec(Some("json"), None);
        let resolved = resolve_output_spec(Some(&agent), None, Some(&request))
            .expect("the agent asked for one");
        assert_eq!(resolved.schema, Some(json!({"type": "object"})));
        assert_eq!(resolved.validator.as_deref(), Some("v.rhai"));
    }

    /// A Rhai validator is written for one format, so it retires with the schema
    /// when a caller asks for a different one - and its error policy, which is
    /// about that validator, retires with it.
    #[test]
    fn reshaping_retires_the_validator_too() {
        let agent = OutputSpec {
            format: Some("a2ui".to_string()),
            validator: Some("a2ui.rhai".to_string()),
            on_validator_error: Some(OnValidatorError::Accept),
            ..OutputSpec::default()
        };
        let request = spec(Some("xml"), None);
        let resolved = resolve_output_spec(Some(&agent), None, Some(&request))
            .expect("the agent asked for one");
        assert_eq!(resolved.format.as_deref(), Some("xml"));
        assert_eq!(resolved.validator, None);
        assert_eq!(resolved.on_validator_error, None);
    }

    /// The error policy cascades the way the validator does: a stage's setting
    /// beats the agent's.
    #[test]
    fn the_stage_error_policy_overrides_the_agents() {
        let agent = OutputSpec {
            format: Some("a2ui".to_string()),
            validator: Some("a2ui.rhai".to_string()),
            on_validator_error: Some(OnValidatorError::Reject),
            ..OutputSpec::default()
        };
        let stage = OutputSpec {
            on_validator_error: Some(OnValidatorError::Accept),
            ..OutputSpec::default()
        };
        let resolved =
            resolve_output_spec(Some(&agent), Some(&stage), None).expect("the agent asked for one");
        assert_eq!(resolved.on_validator_error, Some(OnValidatorError::Accept));
        assert_eq!(
            resolved.validator.as_deref(),
            Some("a2ui.rhai"),
            "the validator itself still falls through from the agent"
        );
    }

    /// The artifact overwrite policy cascades on its own, reshape or not: a
    /// stage beats the agent, a request beats both, and unset stays unset so
    /// the user's config can decide.
    #[test]
    fn the_overwrite_policy_cascades_field_by_field() {
        let agent = OutputSpec {
            format: Some("markdown".to_string()),
            overwrite_artifacts: Some(true),
            ..OutputSpec::default()
        };
        let stage = OutputSpec {
            overwrite_artifacts: Some(false),
            ..OutputSpec::default()
        };
        let resolved = resolve_output_spec(Some(&agent), None, None).unwrap();
        assert_eq!(resolved.overwrite_artifacts, Some(true));
        let resolved = resolve_output_spec(Some(&agent), Some(&stage), None).unwrap();
        assert_eq!(resolved.overwrite_artifacts, Some(false));
        let request = OutputSpec {
            format: Some("json".to_string()),
            overwrite_artifacts: Some(true),
            ..OutputSpec::default()
        };
        let resolved = resolve_output_spec(Some(&agent), Some(&stage), Some(&request)).unwrap();
        assert_eq!(resolved.overwrite_artifacts, Some(true));
        let resolved = resolve_output_spec(None, Some(&OutputSpec::default()), None).unwrap();
        assert_eq!(resolved.overwrite_artifacts, None);
        assert!(
            !OutputSpec {
                overwrite_artifacts: Some(false),
                ..OutputSpec::default()
            }
            .is_empty()
        );
    }

    /// A caller who reshapes and brings their own validator can bring their own
    /// error policy with it.
    #[test]
    fn a_reshaping_caller_can_supply_their_own_error_policy() {
        let agent = OutputSpec {
            format: Some("a2ui".to_string()),
            validator: Some("a2ui.rhai".to_string()),
            ..OutputSpec::default()
        };
        let request = OutputSpec {
            format: Some("xml".to_string()),
            validator: Some("xml.rhai".to_string()),
            on_validator_error: Some(OnValidatorError::Accept),
            ..OutputSpec::default()
        };
        let resolved = resolve_output_spec(Some(&agent), None, Some(&request))
            .expect("the agent asked for one");
        assert_eq!(resolved.validator.as_deref(), Some("xml.rhai"));
        assert_eq!(resolved.on_validator_error, Some(OnValidatorError::Accept));
    }

    /// A caller that brings its own checks keeps them.
    #[test]
    fn a_caller_can_supply_shape_checks_with_its_own_format() {
        let agent = OutputSpec {
            format: Some("a2ui".to_string()),
            validator: Some("a2ui.rhai".to_string()),
            ..OutputSpec::default()
        };
        let request = OutputSpec {
            format: Some("json".to_string()),
            schema: Some(json!({"type": "array"})),
            ..OutputSpec::default()
        };
        let resolved = resolve_output_spec(Some(&agent), None, Some(&request))
            .expect("the agent asked for one");
        assert_eq!(resolved.schema, Some(json!({"type": "array"})));
        assert_eq!(resolved.validator, None, "the agent's own is still retired");
    }

    #[test]
    fn a_caller_reshaping_the_output_drops_the_declared_schema() {
        let agent = spec(Some("json"), Some(json!({"type": "object"})));
        // Caller names a different format and supplies no schema of its own:
        // the schema written for the old shape no longer applies.
        let request = spec(Some("a2ui"), None);
        let resolved = resolve_output_spec(Some(&agent), None, Some(&request))
            .expect("the agent asked for one");
        assert_eq!(resolved.format.as_deref(), Some("a2ui"));
        assert_eq!(resolved.schema, None);
    }

    #[test]
    fn a_caller_supplying_its_own_schema_keeps_it() {
        let agent = spec(Some("json"), Some(json!({"type": "object"})));
        let request = spec(Some("json"), Some(json!({"type": "array"})));
        let resolved = resolve_output_spec(Some(&agent), None, Some(&request))
            .expect("the agent asked for one");
        assert_eq!(resolved.schema, Some(json!({"type": "array"})));
    }

    #[test]
    fn a_caller_that_names_no_format_leaves_the_schema_alone() {
        let agent = spec(Some("json"), Some(json!({"type": "object"})));
        // Only instructions differ, so the declared shape still stands.
        let request = OutputSpec {
            instructions: Some("keep it short".to_string()),
            ..OutputSpec::default()
        };
        let resolved = resolve_output_spec(Some(&agent), None, Some(&request))
            .expect("the agent asked for one");
        assert_eq!(resolved.format.as_deref(), Some("json"));
        assert_eq!(resolved.schema, Some(json!({"type": "object"})));
    }

    /// A parsed blueprint whose agent-level output is `agent`, over stages
    /// named and shaped by `stages`. Both take TOML output tables (or `None`),
    /// because a manifest is where these declarations really come from.
    fn blueprint_with(agent: Option<&str>, stages: &[(&str, Option<&str>)]) -> crate::Blueprint {
        let mut manifest =
            String::from("[agent]\nname = \"checked\"\nversion = \"1.0.0\"\ndescription = \"d\"\n");
        if let Some(fields) = agent {
            manifest.push_str(&format!("\n[agent.output]\n{fields}\n"));
        }
        for (name, output) in stages {
            manifest.push_str(&format!("\n[stages.{name}]\nsystem_prompt = \"p\"\n"));
            if let Some(fields) = output {
                manifest.push_str(&format!("\n[stages.{name}.output]\n{fields}\n"));
            }
        }
        crate::manifest::parse_manifest(&manifest).expect("the test manifest parses")
    }

    /// The headline: a differing format retires the declared validator, and
    /// the warning names the stage, the script, and both formats.
    #[test]
    fn a_differing_format_earns_a_warning_naming_the_retired_validator() {
        let bp = blueprint_with(
            Some("format = \"markdown\"\nvalidator = \"checks/report.rhai\""),
            &[("plan", None)],
        );
        let request = spec(Some("json"), None);
        let warnings = retired_check_warnings(&bp, Some(&request));
        assert_eq!(warnings.len(), 1, "{warnings:?}");
        let line = &warnings[0];
        assert!(line.contains("'json'"), "{line}");
        assert!(line.contains("'markdown'"), "{line}");
        assert!(
            line.contains("the Rhai validator 'checks/report.rhai'"),
            "{line}"
        );
        assert!(line.contains("stage 'plan'"), "{line}");
    }

    /// Re-stating the declared format keeps the checks (see
    /// `re_stating_the_declared_format_keeps_its_shape_checks`), so it earns
    /// no warning - and neither does a request with no format in it, nor no
    /// request at all.
    #[test]
    fn nothing_retired_means_nothing_warned() {
        let bp = blueprint_with(
            Some("format = \"markdown\"\nvalidator = \"v.rhai\"\nschema = { type = \"object\" }"),
            &[("plan", None)],
        );
        let restated = spec(Some("markdown"), None);
        assert!(retired_check_warnings(&bp, Some(&restated)).is_empty());
        let formatless = OutputSpec {
            instructions: Some("keep it short".to_string()),
            ..OutputSpec::default()
        };
        assert!(retired_check_warnings(&bp, Some(&formatless)).is_empty());
        assert!(retired_check_warnings(&bp, None).is_empty());
        // And a blueprint with nothing retirable has nothing to lose.
        let unchecked = blueprint_with(Some("format = \"markdown\""), &[("plan", None)]);
        let reshaped = spec(Some("json"), None);
        assert!(retired_check_warnings(&unchecked, Some(&reshaped)).is_empty());
    }

    /// A schema alone, a validator alone, and the two together each word the
    /// loss precisely.
    #[test]
    fn the_warning_names_exactly_what_is_lost() {
        let request = spec(Some("json"), None);
        let schema_only = blueprint_with(
            Some("format = \"markdown\"\nschema = { type = \"object\" }"),
            &[("plan", None)],
        );
        let warnings = retired_check_warnings(&schema_only, Some(&request));
        assert!(
            warnings[0].contains("the JSON schema declared"),
            "{warnings:?}"
        );
        let both = blueprint_with(
            Some("format = \"markdown\"\nvalidator = \"v.rhai\"\nschema = { type = \"object\" }"),
            &[("plan", None)],
        );
        let warnings = retired_check_warnings(&both, Some(&request));
        assert!(
            warnings[0].contains("the Rhai validator 'v.rhai' and the JSON schema"),
            "{warnings:?}"
        );
    }

    /// A caller who brings a schema for the new shape replaced the declared
    /// one on purpose, so only the validator - which nothing can replace - is
    /// still worth a warning.
    #[test]
    fn a_replacement_schema_is_not_warned_about() {
        let bp = blueprint_with(
            Some("format = \"markdown\"\nvalidator = \"v.rhai\"\nschema = { type = \"object\" }"),
            &[("plan", None)],
        );
        let request = spec(Some("json"), Some(json!({"type": "array"})));
        let warnings = retired_check_warnings(&bp, Some(&request));
        assert_eq!(warnings.len(), 1, "{warnings:?}");
        assert!(
            warnings[0].contains("the Rhai validator 'v.rhai'"),
            "{warnings:?}"
        );
        assert!(
            !warnings[0].contains("JSON schema declared"),
            "{warnings:?}"
        );
        // And the advice acknowledges the schema they brought rather than
        // asking for one.
        assert!(
            warnings[0].contains("the schema supplied with the request"),
            "{warnings:?}"
        );
        // With nothing but the schema declared, the replacement leaves nothing
        // retired at all.
        let schema_only = blueprint_with(
            Some("format = \"markdown\"\nschema = { type = \"object\" }"),
            &[("plan", None)],
        );
        assert!(retired_check_warnings(&schema_only, Some(&request)).is_empty());
    }

    /// An agent-level validator shared by several stages is one line naming
    /// them all, and a stage with its own distinct declaration gets its own.
    #[test]
    fn stages_losing_the_same_checks_share_one_line() {
        let bp = blueprint_with(
            Some("format = \"markdown\"\nvalidator = \"shared.rhai\""),
            &[
                ("plan", None),
                ("draft", None),
                ("wrap", Some("format = \"a2ui\"\nvalidator = \"a2ui.rhai\"")),
            ],
        );
        let request = spec(Some("json"), None);
        let warnings = retired_check_warnings(&bp, Some(&request));
        assert_eq!(warnings.len(), 2, "{warnings:?}");
        assert!(
            warnings[0].contains("stages 'plan' and 'draft'"),
            "{warnings:?}"
        );
        assert!(warnings[1].contains("stage 'wrap'"), "{warnings:?}");
        assert!(warnings[1].contains("'a2ui'"), "{warnings:?}");
    }

    /// A declaration that lives only on a stage retires the same way: the
    /// warning does not need an agent-level `[agent.output]` to exist.
    #[test]
    fn a_stage_level_declaration_retires_without_an_agent_one() {
        let bp = blueprint_with(
            None,
            &[(
                "plan",
                Some("format = \"markdown\"\nvalidator = \"v.rhai\""),
            )],
        );
        let request = spec(Some("json"), None);
        let warnings = retired_check_warnings(&bp, Some(&request));
        assert_eq!(warnings.len(), 1, "{warnings:?}");
        assert!(
            warnings[0].contains("the Rhai validator 'v.rhai'"),
            "{warnings:?}"
        );
    }

    /// A stage that re-declares the requested format keeps its checks even
    /// while its siblings lose theirs, because the cascade is per stage.
    #[test]
    fn a_stage_already_in_the_requested_format_keeps_its_checks() {
        let bp = blueprint_with(
            Some("format = \"markdown\"\nvalidator = \"shared.rhai\""),
            &[("plan", None), ("emit", Some("format = \"json\""))],
        );
        let request = spec(Some("json"), None);
        let warnings = retired_check_warnings(&bp, Some(&request));
        assert_eq!(warnings.len(), 1, "{warnings:?}");
        assert!(warnings[0].contains("stage 'plan'"), "{warnings:?}");
    }

    /// A validator declared without any format is still retired by naming one
    /// (the request reshapes an output that never named its shape), and the
    /// warning says so without inventing a declared format.
    #[test]
    fn a_formatless_declaration_is_reshaped_by_any_request() {
        let bp = blueprint_with(Some("validator = \"v.rhai\""), &[("plan", None)]);
        let request = spec(Some("json"), None);
        let warnings = retired_check_warnings(&bp, Some(&request));
        assert_eq!(warnings.len(), 1, "{warnings:?}");
        assert!(
            warnings[0].contains("declared without a format"),
            "{warnings:?}"
        );
    }

    /// The three list shapes, plus the guard for a slice no caller produces.
    #[test]
    fn stage_phrases_read_as_prose() {
        let names = |names: &[&str]| names.iter().map(|n| n.to_string()).collect::<Vec<_>>();
        assert_eq!(stage_phrase(&names(&["a"])), "stage 'a'");
        assert_eq!(stage_phrase(&names(&["a", "b"])), "stages 'a' and 'b'");
        assert_eq!(
            stage_phrase(&names(&["a", "b", "c"])),
            "stages 'a', 'b', and 'c'"
        );
        assert_eq!(stage_phrase(&[]), "its stages");
    }

    #[test]
    fn short_content_is_stored_verbatim() {
        let out = FinalOutput::new(
            "done: 3 files",
            Some("markdown".to_string()),
            "wrap".into(),
            7,
        );
        assert_eq!(out.content, "done: 3 files");
        assert_eq!(out.format.as_deref(), Some("markdown"));
        assert_eq!(out.stage, "wrap");
        assert_eq!(out.submitted_at, 7);
        assert!(!out.truncated);
    }

    #[test]
    fn oversized_content_is_cut_at_a_char_boundary_and_flagged() {
        // A multi-byte character straddling the cap: slicing by byte index here
        // is what once aborted the daemon, so the cut must walk back.
        let mut content = "a".repeat(MAX_FINAL_OUTPUT_BYTES - 1);
        content.push('\u{1f600}');
        let out = FinalOutput::new(&content, None, "wrap".into(), 0);
        assert!(out.truncated);
        assert_eq!(out.content.len(), MAX_FINAL_OUTPUT_BYTES - 1);
        assert!(out.format.is_none());
    }

    #[test]
    fn describe_spec_is_empty_when_nothing_is_constrained() {
        assert_eq!(describe_spec(&OutputSpec::default()), "");
    }

    #[test]
    fn describe_spec_renders_every_field_it_has() {
        let described = describe_spec(&OutputSpec {
            format: Some("a2ui".to_string()),
            instructions: Some("One card per finding.".to_string()),
            example: Some("{\"root\": {}}".to_string()),
            schema: Some(json!({"type": "object"})),
            validator: None,
            on_validator_error: None,
            overwrite_artifacts: None,
            artifacts: Vec::new(),
        });
        assert!(described.contains("Return it in this format: a2ui."));
        assert!(described.contains("One card per finding."));
        assert!(described.contains("valid against this schema"));
        assert!(described.contains("{\"root\": {}}"));
        let with_files = describe_spec(&OutputSpec {
            artifacts: vec![
                ArtifactSpec {
                    name: "final".to_string(),
                    mime_type: "video/mp4".to_string(),
                    required: true,
                    description: Some("the cut".to_string()),
                },
                ArtifactSpec {
                    name: "notes".to_string(),
                    mime_type: "text/*".to_string(),
                    required: false,
                    description: None,
                },
            ],
            ..OutputSpec::default()
        });
        assert!(
            with_files.contains("- final (video/mp4, required): the cut\n- notes (text/*)"),
            "{with_files}"
        );
    }

    /// Without this the spec and the stage's own system prompt are two peer
    /// instructions, and a strongly-shaped stage prompt wins on some models
    /// and loses on others.
    #[test]
    fn a_constrained_spec_says_it_outranks_the_stage_prompt() {
        let described = describe_spec(&OutputSpec {
            instructions: Some("Reply with only the integer.".to_string()),
            ..OutputSpec::default()
        });
        assert!(
            described.contains("Where anything else you were told"),
            "{described}"
        );
        // Last, so it is read as governing what precedes it rather than as one
        // more line the next paragraph can override.
        assert!(
            described.trim_end().ends_with("follow this."),
            "{described}"
        );
    }

    /// A format on its own is still a shape, so it still outranks a prompt that
    /// describes a different one.
    #[test]
    fn a_format_only_spec_claims_precedence_too() {
        let described = describe_spec(&OutputSpec {
            format: Some("text".to_string()),
            ..OutputSpec::default()
        });
        assert!(
            described.contains("Where anything else you were told"),
            "{described}"
        );
    }

    /// The claim is scoped to presentation. A spec that constrains nothing must
    /// not tell a model to disregard its stage prompt.
    #[test]
    fn an_unconstrained_spec_claims_nothing() {
        assert!(!describe_spec(&OutputSpec::default()).contains("follow this"));
    }

    #[test]
    fn a_spec_round_trips_through_serde() {
        let original = spec(Some("a2ui"), Some(json!({"type": "object"})));
        let text = serde_json::to_string(&original).expect("a spec serializes");
        let back: OutputSpec = serde_json::from_str(&text).expect("and deserializes");
        assert_eq!(back, original);
        // Unset fields stay off the wire rather than serializing as nulls.
        assert!(!text.contains("instructions"));
        assert!(!text.contains("on_validator_error"));
    }

    /// The wire spelling is the manifest spelling: `accept` and `reject`,
    /// nothing else. A request naming a third policy is refused rather than
    /// quietly mapped to either behaviour.
    #[test]
    fn the_error_policy_uses_the_manifest_spelling_on_the_wire() {
        let original = OutputSpec {
            on_validator_error: Some(OnValidatorError::Accept),
            ..OutputSpec::default()
        };
        let text = serde_json::to_string(&original).expect("a spec serializes");
        assert!(text.contains(r#""on_validator_error":"accept""#), "{text}");
        let back: OutputSpec = serde_json::from_str(&text).expect("and deserializes");
        assert_eq!(back, original);

        let rejected = serde_json::from_str::<OutputSpec>(r#"{"on_validator_error":"sometimes"}"#);
        assert!(rejected.is_err(), "an unknown policy must not deserialize");
    }

    #[test]
    fn a_final_output_round_trips_through_serde() {
        let original = FinalOutput::new("answer", None, "wrap".into(), 1);
        let text = serde_json::to_string(&original).expect("an output serializes");
        let back: FinalOutput = serde_json::from_str(&text).expect("and deserializes");
        assert_eq!(back, original);
    }
}