icydb-core 0.149.30

IcyDB — A schema-first typed query engine and persistence runtime for Internet Computer canisters
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
use std::{fs, path::PathBuf};

fn read_source(relative_path: &str) -> String {
    let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
    path.push(relative_path);

    fs::read_to_string(&path)
        .unwrap_or_else(|err| panic!("failed to read {}: {err}", path.display()))
}

fn rust_sources_under(relative_path: &str) -> Vec<PathBuf> {
    let mut root = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
    root.push(relative_path);

    let mut sources = Vec::new();
    let mut pending = vec![root];
    while let Some(path) = pending.pop() {
        let entries = fs::read_dir(&path)
            .unwrap_or_else(|err| panic!("failed to list {}: {err}", path.display()));
        for entry in entries {
            let path = entry
                .unwrap_or_else(|err| panic!("failed to read directory entry: {err}"))
                .path();
            if path.is_dir() {
                pending.push(path);
            } else if path.extension().is_some_and(|extension| extension == "rs") {
                sources.push(path);
            }
        }
    }

    sources.sort();
    sources
}

fn compact_source(source: &str) -> String {
    source
        .chars()
        .filter(|character| !character.is_whitespace())
        .collect()
}

#[test]
fn data_store_insert_stays_canonical_row_only() {
    let source = read_source("src/db/data/store.rs");

    assert!(
        source.contains("pub(in crate::db) fn insert(&mut self, key: RawDataKey, row: CanonicalRow) -> Option<RawRow>"),
        "DataStore::insert must remain CanonicalRow-only at the production write boundary",
    );
    assert!(
        !source.contains("pub fn insert(&mut self, key: RawDataKey, row: RawRow)"),
        "DataStore::insert must not accept RawRow in production code",
    );
}

#[test]
fn prepared_row_write_payloads_stay_canonical() {
    let prepared_op = read_source("src/db/commit/prepared_op.rs");
    let typed_save = read_source("src/db/executor/mutation/save/typed.rs");
    let structural_save = read_source("src/db/executor/mutation/save/structural.rs");

    assert!(
        prepared_op.contains("pub(crate) data_value: Option<CanonicalRow>,"),
        "prepared row commit ops must carry CanonicalRow after-images",
    );
    assert!(
        !prepared_op.contains("pub(crate) data_value: Option<RawRow>,"),
        "prepared row commit ops must not regress to RawRow after-images",
    );
    assert!(
        typed_save.contains("canonical_row_from_entity_with_accepted_contract(")
            && !typed_save.contains("CanonicalRow::from_entity(entity)?")
            && !typed_save.contains("CanonicalRow::from_generated_entity_for_test(entity)?"),
        "typed save after-image construction must use accepted-contract row emission",
    );
    assert!(
        structural_save
            .contains("fn build_structural_update_after_image_row_with_accepted_contract(")
            && structural_save.contains("accepted_row_decode_contract: AcceptedRowDecodeContract,")
            && structural_save.contains(") -> Result<CanonicalRow, InternalError>"),
        "structural update after-image builder must stay accepted-contract aware and return CanonicalRow",
    );
    assert!(
        structural_save
            .contains("fn build_normalized_structural_after_image_row_with_accepted_contract(")
            && structural_save.contains("canonical_row_from_entity_with_accepted_contract(")
            && !structural_save.contains("MutationInput::from_entity("),
        "normalized structural save after-image builder must use accepted-contract row emission",
    );
    assert!(
        structural_save
            .contains("materialize_entity_from_serialized_structural_patch_with_accepted_contract")
            && !structural_save.contains("materialize_entity_from_serialized_structural_patch_for_generated_model_for_test::<"),
        "structural insert/replace materialization must use accepted-contract decode authority",
    );
}

#[test]
fn accepted_storage_row_contracts_do_not_retain_generated_field_bridge() {
    let structural_row = read_source("src/db/data/structural_row.rs");
    let structural_row_compact = compact_source(&structural_row);
    let row_reader = read_source("src/db/data/persisted_row/reader/core.rs");
    let row_reader_compact = compact_source(&row_reader);
    let persisted_patch = read_source("src/db/data/persisted_row/patch.rs");
    let persisted_patch_compact = compact_source(&persisted_patch);
    let primary_key_reader = read_source("src/db/data/persisted_row/reader/primary_key.rs");
    let primary_key_reader_compact = compact_source(&primary_key_reader);
    let reverse_index = read_source("src/db/relation/reverse_index.rs");
    let reverse_index_compact = compact_source(&reverse_index);
    let save_validation = read_source("src/db/executor/mutation/save_validation.rs");
    let save_validation_compact = compact_source(&save_validation);
    let row_decode = read_source("src/db/executor/terminal/row_decode/mod.rs");

    assert!(
        structural_row.contains("pub(in crate::db) fn from_accepted_schema_snapshot(")
            && structural_row.contains("Self::from_accepted_decode_contract(")
            && structural_row.contains("fn from_generated_model_for_test(")
            && structural_row
                .contains("fn from_generated_model_with_accepted_decode_contract_for_test(")
            && !structural_row.contains("fn from_model(")
            && !structural_row.contains("fn from_model_with_accepted_decode_contract(")
            && !structural_row.contains("fn from_model_with_accepted_schema_snapshot(")
            && !structural_row.contains(
                "Ok(Self::from_generated_model_with_accepted_decode_contract_for_test(\n            model,\n            descriptor.row_decode_contract(),\n        ))",
        ),
        "storage row readers must use accepted-only row contracts after the generated-compatibility proof",
    );
    assert!(
        row_decode
            .contains("pub(in crate::db) fn from_generated_compatible_accepted_decode_contract(")
            && row_decode.contains("fn from_generated_model_for_test(")
            && row_decode.contains("StructuralRowContract::from_accepted_decode_contract(")
            && !row_decode
                .contains("StructuralRowContract::from_generated_model_with_accepted_decode_contract_for_test("),
        "accepted executor row layouts must not retain the generated field bridge after compatibility proof",
    );
    assert!(
        persisted_patch.contains("StructuralRowContract::from_accepted_decode_contract(")
            && persisted_patch
                .contains("Self::validate_payload_slot(&contract, generated_fields, slot)?")
            && !persisted_patch
                .contains("StructuralRowContract::from_generated_model_with_accepted_decode_contract_for_test("),
        "accepted structural patch materialization must validate payload slots through accepted row contracts without retaining the generated field bridge",
    );
    assert!(
        structural_row_compact.contains("required_accepted_field_decode_contract(&self,slot:usize")
            && structural_row_compact
                .contains(".required_field_for_slot(self.entity_path(),slot)?")
            && !structural_row_compact.contains("fnaccepted_field_decode_contract(")
            && structural_row_compact.contains(
                "ifself.accepted_decode_contract.is_some(){letfield=self.required_accepted_field_decode_contract(slot)?;returnOk(field.leaf_codec());}",
            )
            && structural_row_compact.contains(
                "ifself.accepted_decode_contract.is_some(){letfield=self.required_accepted_field_decode_contract(slot)?;returnOk(field.field_name());}",
            ),
        "accepted structural row field-name and leaf-codec lookups must fail closed instead of falling back to generated field metadata",
    );
    assert!(
        row_reader_compact.contains("fnrequired_accepted_field_decode_contract(&self,slot:usize")
            && row_reader_compact.contains(
                "self.contract.required_accepted_field_decode_contract(slot)",
            )
            && row_reader_compact.contains(
                "letaccepted_field=self.required_accepted_field_decode_contract(slot)?;ifletSome(value)=self.required_accepted_value_storage_scalar(slot,accepted_field)?",
            )
            && !row_reader_compact.contains("pub(incrate::db)fnaccepted_field_decode_contract")
            && !row_reader_compact.contains(
                "ifletSome(accepted_field)=self.contract.accepted_field_decode_contract(slot)",
            ),
        "accepted structural row reader scalar fast paths must fail closed instead of treating missing accepted field metadata as a non-fast-path value",
    );
    assert!(
        primary_key_reader_compact.contains("ifcontract.has_accepted_decode_contract(){")
            && primary_key_reader_compact
                .contains("contract.required_accepted_field_decode_contract(primary_key_slot)?")
            && !primary_key_reader_compact
                .contains(".accepted_field_decode_contract(primary_key_slot).ok_or_else(")
            && !primary_key_reader_compact
                .contains("ifletSome(primary_key_field)=contract.accepted_field_decode_contract(primary_key_slot)"),
        "accepted primary-key validation must fail closed instead of falling back to generated field metadata",
    );
    assert!(
        persisted_patch_compact.contains("contract.required_accepted_field_decode_contract(slot)?")
            && persisted_patch_compact
                .contains("contract.required_accepted_field_decode_contract(slot.index())?"),
        "accepted structural patch payload handling must use the shared required accepted-field contract",
    );
    assert!(
        reverse_index_compact
            .contains("source_row_contract.required_accepted_field_decode_contract(slot)?")
            && save_validation_compact
                .contains("contract.required_accepted_field_decode_contract(slot)?")
            && save_validation_compact
                .contains(".required_accepted_field_decode_contract(field_index)?"),
        "relation and save validation accepted field scans must use the shared required accepted-field contract",
    );
}

#[test]
fn generated_persisted_row_bridge_helpers_are_named_test_only() {
    let persisted_row_mod = read_source("src/db/data/persisted_row/mod.rs");
    let persisted_patch = read_source("src/db/data/persisted_row/patch.rs");
    let persisted_patch_compact = compact_source(&persisted_patch);
    let data_row = read_source("src/db/data/row.rs");
    let data_row_compact = compact_source(&data_row);
    let row_reader = read_source("src/db/data/persisted_row/reader/core.rs");
    let row_reader_compact = compact_source(&row_reader);
    let patch_writer = read_source("src/db/data/persisted_row/writer.rs");
    let patch_writer_compact = compact_source(&patch_writer);

    assert!(
        persisted_row_mod.contains("#[cfg(test)]\nmod writer;")
            && persisted_row_mod.contains(
                "materialize_entity_from_serialized_structural_patch_for_generated_model_for_test",
            )
            && persisted_row_mod.contains(
                "serialize_entity_slots_as_complete_serialized_patch_for_generated_model_for_test",
            ),
        "generated persisted-row writer and patch bridges must stay behind test-only exports",
    );
    assert!(
        persisted_patch.contains("fn new_for_generated_model_for_test(")
            && persisted_patch.contains(
                "fn materialize_entity_from_serialized_structural_patch_for_generated_model_for_test",
            )
            && persisted_patch.contains(
                "fn canonical_row_from_complete_serialized_structural_patch_for_generated_model_for_test",
            )
            && persisted_patch.contains("fn canonical_row_from_entity_for_generated_model_for_test")
            && persisted_patch.contains(
                "fn serialize_entity_slots_as_complete_serialized_patch_for_generated_model_for_test",
            )
            && persisted_patch.contains(
                "fn materialize_entity_from_serialized_structural_patch_with_accepted_contract",
            )
            && persisted_patch.contains("fn canonical_row_from_entity_with_accepted_contract")
            && !persisted_patch.contains("SerializedPatchPayloads::new(")
            && !persisted_patch_compact
                .contains("fnmaterialize_entity_from_serialized_structural_patch<E>")
            && !persisted_patch_compact
                .contains("fncanonical_row_from_complete_serialized_structural_patch(")
            && !persisted_patch_compact.contains("fncanonical_row_from_entity<E>")
            && !persisted_patch_compact
                .contains("fnserialize_entity_slots_as_complete_serialized_patch<E>"),
        "generated serialized patch materialization and row-emission helpers must not keep neutral production-looking names",
    );
    assert!(
        data_row.contains("fn from_generated_entity_for_test")
            && data_row.contains(
                "fn from_complete_serialized_structural_patch_for_generated_model_for_test",
            )
            && data_row.contains("fn try_decode_with_generated_model_for_test")
            && !data_row_compact.contains("fnfrom_entity<E>")
            && !data_row_compact.contains("fnfrom_complete_serialized_structural_patch(")
            && !data_row_compact.contains("fntry_decode<E"),
        "test-only raw row helpers must make generated-model decode authority explicit",
    );
    assert!(
        row_reader.contains("fn from_raw_row_with_generated_model_for_test(")
            && row_reader.contains("fn from_raw_row_with_unvalidated_generated_model_for_test(",)
            && row_reader.contains("fn from_raw_row_with_validated_contract(")
            && !row_reader_compact.contains("pub(incrate::db)fnfrom_raw_row(")
            && !row_reader_compact.contains("fnfrom_raw_row_with_model("),
        "generated structural slot-reader construction must stay named as a generated test bridge",
    );
    assert!(
        patch_writer.contains("fn for_generated_model_for_test(")
            && !patch_writer_compact.contains("fnfor_model("),
        "generated complete serialized patch writers must not expose neutral for_model construction",
    );
}

#[test]
fn commit_and_delete_relation_row_contracts_use_accepted_snapshots() {
    let structural_row = read_source("src/db/data/structural_row.rs");
    let commit_prepare = read_source("src/db/commit/prepare.rs");
    let relation_validate = read_source("src/db/relation/validate.rs");

    assert!(
        structural_row.contains("pub(in crate::db) fn from_accepted_schema_snapshot(")
            && !structural_row.contains("fn from_model_with_accepted_schema_snapshot("),
        "structural row contracts must expose accepted-snapshot construction without retaining the generated-compatible snapshot constructor",
    );
    assert!(
        commit_prepare.contains(
            "StructuralRowContract::from_accepted_schema_snapshot(authority.entity_path, &accepted)",
        ) && relation_validate
            .contains("StructuralRowContract::from_accepted_schema_snapshot(S::PATH, &accepted)")
            && !commit_prepare
                .contains("StructuralRowContract::from_generated_model_for_test_with_accepted_schema_snapshot")
            && !relation_validate
                .contains("StructuralRowContract::from_generated_model_for_test_with_accepted_schema_snapshot"),
        "commit preflight and delete relation validation must build accepted-only row contracts after schema acceptance",
    );
}

#[test]
fn accepted_row_decode_contract_runtime_lookups_fail_closed() {
    let schema_runtime = read_source("src/db/schema/runtime.rs");
    let schema_runtime_compact = compact_source(&schema_runtime);
    let relation_save_validate = read_source("src/db/relation/save_validate.rs");
    let relation_save_validate_compact = compact_source(&relation_save_validate);
    let save_validation = read_source("src/db/executor/mutation/save_validation.rs");
    let save_validation_compact = compact_source(&save_validation);

    assert!(
        schema_runtime_compact.contains("pub(incrate::db)fnrequired_field_for_slot(")
            && schema_runtime_compact.contains(
                "InternalError::persisted_row_slot_lookup_out_of_bounds(entity_path,slot)",
            ),
        "accepted row-decode contracts must expose a required slot lookup for accepted runtime authority",
    );
    assert!(
        save_validation_compact
            .contains("accepted_contract.required_field_for_slot(E::PATH,slot)?")
            && save_validation_compact
                .contains("accepted_contract.required_field_for_slot(E::PATH,primary_key_slot)?")
            && save_validation_compact
                .contains("accepted_contract.required_field_for_slot(E::PATH,field_index)?")
            && relation_save_validate_compact
                .contains("accepted_row_decode_contract.required_field_for_slot(E::PATH,slot)?")
            && !save_validation_compact.contains(".field_for_slot(primary_key_slot).ok_or_else(")
            && !save_validation_compact.contains(".field_for_slot(field_index).ok_or_else("),
        "accepted typed-save validation must use required accepted row-decode field lookup",
    );
}

#[test]
fn save_preflight_relations_use_accepted_contracts() {
    let save_validation = read_source("src/db/executor/mutation/save_validation.rs");
    let relation_save_validate = read_source("src/db/relation/save_validate.rs");

    assert!(
        save_validation.contains("validate_save_strong_relations_with_accepted_contract::<E>(")
            && save_validation.contains("self.accepted_row_decode_contract(),")
            && !save_validation.contains("validate_save_strong_relations::<E>(&self.db, entity)?"),
        "save relation preflight must use accepted row contracts instead of reopening E::MODEL relation metadata",
    );
    assert!(
        relation_save_validate
            .contains("validate_save_strong_relations_with_accepted_contract<E>",)
            && !relation_save_validate.contains("strong_relations_for_model_iter")
            && !relation_save_validate.contains("E::MODEL"),
        "save relation validation must derive relation metadata from accepted row contracts",
    );
}

#[test]
fn reverse_relation_runtime_paths_use_accepted_contracts() {
    let reverse_index = read_source("src/db/relation/reverse_index.rs");
    let delete_validate = read_source("src/db/relation/validate.rs");
    let runtime_hooks = read_source("src/db/runtime_hooks/mod.rs");
    let commit_prepare = read_source("src/db/commit/prepare.rs");

    assert!(
        reverse_index.contains("accepted_strong_relations_for_row_contract(")
            && reverse_index.contains("source_row_contract: StructuralRowContract,")
            && !reverse_index.contains("strong_relations_for_model_iter")
            && !reverse_index.contains("source_model: &'static EntityModel"),
        "reverse-index mutation preparation must derive relation fields from accepted row contracts",
    );
    assert!(
        delete_validate.contains("accepted_strong_relations_for_row_contract(")
            && delete_validate.contains("accepted_source_row_contract::<S>(")
            && !delete_validate.contains("model_has_strong_relations_to_target(S::MODEL"),
        "delete-side relation validation must derive relation fields from accepted row contracts",
    );
    assert!(
        !runtime_hooks.contains("model_has_strong_relations_to_target("),
        "delete hook traversal must not use generated model relation metadata as a runtime prefilter",
    );
    assert!(
        commit_prepare
            .contains("row_contract.clone(),\n        structural.data_key.storage_key(),"),
        "commit reverse-index preparation must receive the accepted structural row contract",
    );
}

#[test]
fn forward_index_write_keys_use_accepted_row_contract_slots() {
    let index_key_build = read_source("src/db/index/key/build.rs");
    let index_plan = read_source("src/db/index/plan/mod.rs");
    let unique_plan = read_source("src/db/index/plan/unique.rs");
    let structural_row = read_source("src/db/data/structural_row.rs");
    let predicate_runtime = read_source("src/db/predicate/runtime/mod.rs");

    assert!(
        structural_row.contains("pub(in crate::db) fn field_slot_index_by_name(")
            && structural_row.contains("self.accepted_decode_contract.is_some()"),
        "structural row contracts must expose accepted-first field-name to slot lookup",
    );
    assert!(
        index_key_build.contains("pub(crate) fn new_from_slots_with_contract(")
            && index_key_build.contains("row_contract.field_slot_index_by_name(field)?")
            && !index_key_build.contains("pub(crate) fn new_from_slots(\n")
            && !index_key_build.contains("compile_scalar_index_key_item_program("),
        "write-time index key construction must resolve field slots through accepted row contracts",
    );
    assert!(
        index_key_build.contains("pub(crate) fn new_from_slot_ref_reader_with_schema")
            && index_key_build.contains("schema_info.field_slot_index(field)")
            && !index_key_build.contains("pub(crate) fn new_from_slot_ref_reader(")
            && predicate_runtime.contains("slots.field_leaf_codec(field_slot)")
            && predicate_runtime.contains("slots.required_value_storage_scalar(field_slot)"),
        "cursor anchor and predicate index-key paths must use accepted schema/row-contract slot authority instead of generated model slot lookup",
    );
    assert!(
        index_plan.contains("IndexKey::new_from_slots_with_contract(")
            && index_plan.contains("PredicateProgram::compile_with_row_contract(")
            && index_plan.contains("row_contract,")
            && !index_plan.contains("IndexKey::new_from_slots("),
        "forward-index mutation planning must pass accepted row contracts into index key and predicate construction",
    );
    assert!(
        unique_plan.contains("IndexKey::new_from_slots_with_contract(")
            && unique_plan.contains("row_contract,"),
        "unique-index validation must rebuild stored index keys through accepted row contracts",
    );
    assert!(
        predicate_runtime.contains("slots.field_leaf_codec(field_slot)")
            && predicate_runtime.contains("slots.required_value_storage_scalar(field_slot)")
            && !predicate_runtime.contains("slots\n        .field_decode_contract(field_slot)")
            && !predicate_runtime.contains("slots.field_decode_contract(field_slot)"),
        "conditional-index predicate fast paths must use accepted-aware scalar slot helpers",
    );
}

#[test]
fn global_distinct_grouped_runtime_keeps_prepared_authority() {
    let grouped_entrypoints = read_source("src/db/executor/pipeline/entrypoints/grouped.rs");
    let aggregate_distinct = read_source("src/db/executor/aggregate/distinct.rs");
    let aggregate_numeric = read_source("src/db/executor/aggregate/numeric/mod.rs");
    let aggregate_execution = read_source("src/db/executor/aggregate/execution.rs");

    assert!(
        grouped_entrypoints.contains(
            "fn grouped_path_runtime(\n        &self,\n        authority: EntityAuthority,"
        ) && grouped_entrypoints.contains("self.grouped_path_runtime(authority)?")
            && !grouped_entrypoints
                .contains("let authority = EntityAuthority::for_generated_type_for_test::<E>();"),
        "grouped runtime preparation must consume prepared accepted authority instead of reopening generated authority",
    );
    assert!(
        aggregate_execution.contains("GlobalDistinct {\n        authority: EntityAuthority,")
            && aggregate_numeric.contains("let authority = plan.authority();")
            && aggregate_distinct
                .contains("self.prepare_grouped_route_runtime(route, authority, None, None)?"),
        "global DISTINCT aggregate execution must carry prepared accepted authority into grouped runtime",
    );
}

#[test]
fn generated_only_prepared_plan_constructor_is_test_only() {
    let prepared_plan = read_source("src/db/executor/prepared_execution_plan/mod.rs");
    let entity_authority = read_source("src/db/executor/authority/entity.rs");
    let executor_mod = read_source("src/db/executor/mod.rs");
    let query_intent = read_source("src/db/query/intent/query.rs");
    let save_mod = read_source("src/db/executor/mutation/save/mod.rs");
    let save_validation = read_source("src/db/executor/mutation/save_validation.rs");
    let save_typed = read_source("src/db/executor/mutation/save/typed.rs");
    let save_batch = read_source("src/db/executor/mutation/save/batch.rs");
    let save_structural = read_source("src/db/executor/mutation/save/structural.rs");
    let session_mod = read_source("src/db/session/mod.rs");
    let session_query_explain = read_source("src/db/session/query/explain.rs");
    let session_query_explain_compact = compact_source(&session_query_explain);
    let session_query_cache = read_source("src/db/session/query/cache.rs");
    let session_sql_explain = read_source("src/db/session/sql/execute/explain.rs");
    let session_sql_explain_compact = compact_source(&session_sql_explain);
    let sql_aggregate_binding = read_source("src/db/sql/lowering/aggregate/command/binding.rs");
    let query_access_plan = read_source("src/db/query/plan/access_plan.rs");

    assert!(
        prepared_plan.contains("#[cfg(test)]\n    pub(in crate::db) fn new(")
            && prepared_plan.contains("#[cfg(test)]\n    fn build(")
            && prepared_plan.contains("EntityAuthority::for_generated_type_for_test::<E>()")
            && prepared_plan.contains(".with_cursor_schema_info_for_test(")
            && entity_authority.contains(
                "#[cfg(test)]\n    pub(in crate::db) const fn for_generated_type_for_test"
            )
            && !entity_authority.contains("pub const fn for_type")
            && executor_mod.contains(
                "#[cfg(test)]\nimpl<E> From<CompiledQuery<E>> for PreparedExecutionPlan<E>"
            ),
        "generated-only PreparedExecutionPlan constructors must stay test-only so runtime plans use accepted-backed shared authority",
    );
    assert!(
        query_intent.contains("#[cfg(test)]\n    pub(in crate::db) fn into_plan("),
        "compiled-query plan extraction must remain test-only with the generated-only prepared-plan conversion",
    );
    assert!(
        save_mod.contains("accepted_schema_info: SchemaInfo,")
            && save_mod
                .contains("pub(in crate::db::executor::mutation) const fn accepted_schema_info(")
            && save_validation.contains("schema: &SchemaInfo,")
            && save_typed.contains("let schema = self.accepted_schema_info();")
            && save_batch.contains("let schema = self.accepted_schema_info();")
            && save_structural.contains("let schema = self.accepted_schema_info();")
            && !save_mod.contains("SchemaInfo::cached_for_generated_entity_model(E::MODEL)")
            && !save_validation.contains("SchemaInfo::cached_for_generated_entity_model(E::MODEL)")
            && !save_typed.contains("Self::schema_info()")
            && !save_batch.contains("Self::schema_info()")
            && !save_structural.contains("Self::schema_info()")
            && !save_validation
                .contains("EntityAuthority::for_generated_type_for_test::<E>().schema_info()"),
        "save validation metadata lookup must use session-selected accepted SchemaInfo instead of reopening generated schema authority",
    );
    assert!(
        session_mod.contains("self.db.recovered_store(E::Store::PATH)?")
            && session_mod.contains("ensure_accepted_schema_snapshot(schema_store, E::ENTITY_TAG, E::PATH, E::MODEL)")
            && session_mod.contains("SchemaInfo::from_accepted_snapshot_for_model(E::MODEL, &accepted_schema)")
            && session_mod.contains("self.save_executor::<E>(contract, schema_info, schema_fingerprint)")
            && !session_mod.contains(
                "self.ensure_accepted_schema_snapshot_for_authority(&EntityAuthority::for_generated_type_for_test::<E>())",
            ),
        "session save bootstrap must pass accepted schema metadata into SaveExecutor without constructing generated executor authority",
    );
    assert!(
        session_query_cache
            .contains("query.try_build_trivial_scalar_load_plan_with_schema_info(schema_info)?")
            && !session_query_cache
                .contains("query.try_build_trivial_scalar_load_plan_for_model_only()?"),
        "shared query cache trivial scalar fast path must finalize executor metadata with accepted SchemaInfo instead of generated schema fallback",
    );
    assert!(
        session_query_explain.contains(
            "plan.finalize_access_choice_for_model_with_indexes_and_schema("
        )
            && query_access_plan.contains(
                "fn finalize_access_choice_for_model_only_with_indexes("
            )
            && session_query_explain_compact.contains(
                "SchemaInfo::from_accepted_snapshot_for_model(query.structural().model(),&accepted_schema,"
            )
            && !session_query_explain.contains("plan.finalize_access_choice_for_model_only_with_indexes(")
            && session_sql_explain.contains(
                "plan.finalize_access_choice_for_model_with_indexes_and_schema("
            )
            && session_sql_explain
                .contains("bind_lowered_sql_query_structural_with_schema(")
            && session_sql_explain.contains(
                "bind_lowered_sql_explain_global_aggregate_structural_with_schema("
            )
            && session_sql_explain_compact.contains(
                "SchemaInfo::from_accepted_snapshot_for_model(authority.model(),accepted_schema)"
            )
            && !session_sql_explain.contains("plan.finalize_access_choice_for_model_only_with_indexes(")
            && !session_sql_explain.contains("bind_lowered_sql_query_structural(")
            && !session_sql_explain
                .contains("bind_lowered_sql_explain_global_aggregate_structural(")
            && sql_aggregate_binding.contains(
                "apply_lowered_base_query_shape_with_schema("
            ),
        "session explain binding and access-choice finalization must use accepted SchemaInfo instead of generated schema fallback",
    );
}

#[test]
fn standalone_generated_query_planning_is_model_only() {
    let query_plan_pipeline = read_source("src/db/query/plan/pipeline.rs");
    let schema_info = read_source("src/db/schema/info.rs");

    assert!(
        query_plan_pipeline.contains("fn build_query_model_plan_for_model_only")
            && query_plan_pipeline
                .contains("fn build_query_model_plan_with_indexes_for_model_only")
            && query_plan_pipeline.contains("fn try_build_trivial_scalar_load_plan_for_model_only")
            && query_plan_pipeline
                .contains("fn prepare_query_model_scalar_planning_state_for_model_only")
            && query_plan_pipeline
                .contains("SchemaInfo::cached_for_generated_entity_model(query.model()).clone()")
            && schema_info.contains("fn cached_for_generated_entity_model(")
            && !schema_info.contains("fn cached_for_entity_model(")
            && query_plan_pipeline
                .contains("fn prepare_query_model_scalar_planning_state_with_schema_info")
            && !query_plan_pipeline.contains("fn build_query_model_plan<K>")
            && !query_plan_pipeline.contains("fn build_query_model_plan_with_indexes<K>")
            && !query_plan_pipeline.contains("fn prepare_query_model_scalar_planning_state<'"),
        "standalone generated-schema query planning wrappers must stay explicit model-only surfaces",
    );
}

#[test]
fn executor_plan_validation_uses_accepted_schema_info() {
    let access_mod = read_source("src/db/access/mod.rs");
    let access_validate = read_source("src/db/access/validate.rs");
    let entity_authority = read_source("src/db/executor/authority/entity.rs");

    assert!(
        entity_authority.contains("if !plan.has_static_planning_shape()")
            && entity_authority
                .contains("executor plan validation requires planner-frozen static shape",)
            && entity_authority.contains("executor plan validation requires accepted schema info")
            && entity_authority.contains("validate_access_runtime_invariants_with_schema(")
            && !entity_authority.contains("fn schema_info(")
            && !entity_authority
                .contains("SchemaInfo::cached_for_generated_entity_model(self.model)")
            && !entity_authority.contains("validate_access_runtime_invariants_model(")
            && !entity_authority.contains("validate_access_structure_model(self.schema_info()"),
        "executor plan validation must require planner-frozen static shape and authority-carried accepted schema info instead of reopening generated schema authority",
    );
    assert!(
        access_mod.contains("validate_access_runtime_invariants_with_schema")
            && access_validate.contains("schema.field_is_indexed(field)")
            && !access_validate.contains("fn validate_index_reference_model("),
        "runtime access validation must check index references through schema info instead of generated entity model membership",
    );
}

#[test]
fn raw_entity_authority_bootstrap_stays_layout_free() {
    let entity_authority = read_source("src/db/executor/authority/entity.rs");
    let executor_explain = read_source("src/db/executor/explain/mod.rs");
    let route_shape = read_source("src/db/executor/planning/route/contracts/shape.rs");
    let query_plan_covering = read_source("src/db/query/plan/covering/mod.rs");
    let session_query_explain = read_source("src/db/session/query/explain.rs");
    let prepared_plan = read_source("src/db/executor/prepared_execution_plan/mod.rs");
    let session_sql_explain = read_source("src/db/session/sql/execute/explain.rs");

    assert!(
        entity_authority.contains("row_layout: Option<RowLayout>,")
            && entity_authority.contains("row_layout: None,")
            && entity_authority.contains("fn with_generated_row_layout_for_test(")
            && entity_authority.contains("row_layout: Some(RowLayout::from_generated_model_for_test(self.model))")
            && entity_authority.contains(
                "entity authority row layout must be selected from accepted schema or explicit test layout",
            )
            && !entity_authority.contains("row_layout: RowLayout::from_generated_model_for_test(model)"),
        "raw EntityAuthority bootstrap must not attach generated row layout outside explicit test layout construction",
    );
    assert!(
        prepared_plan.contains("assemble_load_execution_node_descriptor_for_authority(")
            && !prepared_plan.contains("self.authority.fields(),")
            && executor_explain.contains("explain_execution_descriptor_from_plan_with_authority(")
            && executor_explain.contains(
                "finalized_execution_diagnostics_from_plan_with_authority_and_descriptor_mutator("
            )
            && session_query_explain
                .contains("accepted_entity_authority_for_schema::<E>(&accepted_schema)")
            && session_query_explain.contains(
                ".explain_execution_descriptor_from_plan_with_authority(&plan, &authority)"
            )
            && session_query_explain.contains(
                ".finalized_execution_diagnostics_from_plan_with_authority_and_descriptor_mutator("
            )
            && executor_explain.contains("explain_execution_descriptor_from_model_only_plan(")
            && executor_explain.contains("freeze_load_execution_route_facts_for_model_only(")
            && !executor_explain.contains("freeze_load_execution_route_facts(")
            && !session_query_explain
                .contains(".explain_execution_descriptor_from_model_only_plan(&plan)")
            && session_sql_explain.contains("freeze_load_execution_route_facts_for_authority(")
            && session_sql_explain.contains(
                ".finalized_execution_diagnostics_from_plan_with_authority_and_descriptor_mutator("
            )
            && !session_sql_explain.contains("authority.fields(),"),
        "prepared/session descriptors and SQL EXPLAIN route facts must consume accepted authority instead of split generated field metadata",
    );
    assert!(
        entity_authority.contains("covering_read_execution_plan_with_schema_info(")
            && entity_authority.contains("covering_hybrid_projection_plan_with_schema_info(")
            && !entity_authority.contains("covering_read_execution_plan_from_fields(")
            && !entity_authority.contains("covering_hybrid_projection_plan_from_fields(")
            && query_plan_covering.contains("fn resolve_covering_field_slot_with_schema(")
            && query_plan_covering.contains("schema.field_slot_index(field_name)?")
            && query_plan_covering.contains("schema.field_kind(field_name).copied()"),
        "authority-owned covering read and hybrid projection planning must resolve projected field slots from accepted SchemaInfo",
    );
    assert!(
        entity_authority.contains("pub(in crate::db) fn aggregate_route_shape")
            || entity_authority.contains("pub(in crate::db) fn aggregate_route_shape<'")
    );
    assert!(
        entity_authority.contains("AggregateRouteShape::new_from_schema_info(")
            && executor_explain
                .contains(".aggregate_route_shape(kind, strategy.explain_projected_field())")
            && !executor_explain
                .contains("strategy.explain_projected_field(),\n            E::MODEL.fields(),")
            && session_sql_explain.contains(".aggregate_route_shape(")
            && !session_sql_explain.contains("model.fields(),")
            && route_shape.contains("pub(in crate::db) fn new_from_schema_info(")
            && route_shape.contains("schema.field_slot_index(target_field)")
            && route_shape.contains(".primary_key_name()"),
        "aggregate execution explain route shapes must use accepted SchemaInfo instead of generated field tables",
    );
}

#[test]
fn generated_row_contract_runtime_fallbacks_are_test_only() {
    let persisted_row_contract = read_source("src/db/data/persisted_row/contract.rs");
    let persisted_row_patch = read_source("src/db/data/persisted_row/patch.rs");

    assert!(
        persisted_row_contract
            .contains("#[cfg(test)]\nfn decode_runtime_value_from_generated_row_contract(")
            && persisted_row_contract
                .contains("#[cfg(test)]\nfn decode_scalar_slot_value_from_generated_row_contract",)
            && persisted_row_contract.contains(
                "#[cfg(test)]\nfn validate_non_scalar_slot_value_with_generated_row_contract(",
            )
            && persisted_row_contract.contains(
                "#[cfg(test)]\npub(in crate::db::data::persisted_row) fn canonical_row_from_runtime_value_source_with_generated_contract",
            )
            && persisted_row_contract
                .contains("#[cfg(not(test))]\nfn generated_row_contract_reached_runtime_boundary(")
            && persisted_row_contract.contains("requires accepted row contract for entity",),
        "row-contract runtime decode/validation must fail closed in production instead of falling back to generated field metadata",
    );
    assert!(
        persisted_row_patch.contains(
            "#[cfg(test)]\nfn canonical_row_from_structural_slot_reader_with_generated_contract(",
        ) && persisted_row_patch
            .contains("raw row canonicalization requires accepted row contract for entity",),
        "raw-row canonicalization must not retain a production generated-contract fallback",
    );
}

#[test]
fn sql_command_lowering_uses_accepted_schema_for_runtime_explain() {
    let sql_compile_core = read_source("src/db/session/sql/compile/core.rs");
    let sql_lowering_prepare = read_source("src/db/sql/lowering/prepare.rs");
    let sql_lowering_select = read_source("src/db/sql/lowering/select/mod.rs");
    let sql_global_aggregate_binding =
        read_source("src/db/sql/lowering/aggregate/command/binding.rs");

    assert!(
        sql_compile_core.contains("Self::compile_explain(statement, entity_name, model, schema)",)
            && sql_compile_core.contains(
                "lower_sql_command_from_prepared_statement_with_schema(prepared, model, schema)",
            )
            && !sql_compile_core
                .contains("lower_sql_command_from_prepared_statement(prepared, model)"),
        "runtime SQL EXPLAIN compilation must lower with accepted SchemaInfo instead of generated model schema fallback",
    );
    assert!(
        sql_lowering_prepare.contains(
            "#[cfg(test)]\npub(crate) fn lower_sql_command_from_prepared_statement_for_model_only("
        ) && sql_lowering_prepare
            .contains("pub(crate) fn lower_sql_command_from_prepared_statement_with_schema(")
            && sql_lowering_prepare.contains("fn lower_prepared_statement_for_model_only(")
            && sql_lowering_prepare.contains("fn lower_prepared_statement_with_schema(")
            && sql_lowering_prepare.contains("fn lower_explain_select_prepared_for_model_only(")
            && sql_lowering_prepare.contains("fn lower_explain_select_prepared_with_schema(")
            && sql_lowering_prepare
                .contains("lower_select_shape_with_schema(statement.clone(), model, schema)"),
        "shared SQL command lowering must keep generated-schema command lowering test-only/model-only and expose an accepted-schema runtime path",
    );
    assert!(
        sql_lowering_select.contains(
            "#[cfg(test)]\npub(in crate::db::sql::lowering) fn lower_select_shape_for_model_only("
        ) && sql_lowering_select
            .contains("pub(in crate::db::sql::lowering) fn lower_select_shape_with_schema(",),
        "generated-schema SELECT lowering must remain test-only/model-only while runtime callers use explicit SchemaInfo",
    );
    assert!(
        sql_global_aggregate_binding.contains("#[cfg(test)]\n    fn into_typed_for_model_only")
            && sql_global_aggregate_binding
                .contains("compile_sql_global_aggregate_command_for_model_only")
            && sql_global_aggregate_binding
                .contains("compile_sql_global_aggregate_command_from_prepared_for_model_only")
            && sql_global_aggregate_binding
                .contains("bind_lowered_sql_global_aggregate_command_for_model_only")
            && sql_global_aggregate_binding
                .contains("compile_sql_global_aggregate_command_core_from_prepared_with_schema")
            && !sql_global_aggregate_binding.contains("fn into_typed<E:")
            && !sql_global_aggregate_binding
                .contains("pub(crate) fn compile_sql_global_aggregate_command<E:")
            && !sql_global_aggregate_binding
                .contains("pub(crate) fn compile_sql_global_aggregate_command_from_prepared<E:")
            && !sql_global_aggregate_binding
                .contains("fn bind_lowered_sql_global_aggregate_command<E:"),
        "generated-schema SQL global aggregate lowering must be explicitly test-only/model-only while runtime aggregate binding uses explicit SchemaInfo",
    );
}

#[test]
fn typed_runtime_dispatch_selects_accepted_entity_authority_at_session_boundary() {
    let session_mod = read_source("src/db/session/mod.rs");
    let session_query_cache = read_source("src/db/session/query/cache.rs");
    let session_sql_cache = read_source("src/db/session/sql/cache.rs");
    let session_sql_execute = read_source("src/db/session/sql/execute/mod.rs");
    let session_sql_explain = read_source("src/db/session/sql/execute/explain.rs");
    let session_sql_write = read_source("src/db/session/sql/execute/write.rs");
    let entity_authority = read_source("src/db/executor/authority/entity.rs");

    assert!(
        session_mod.contains("pub(in crate::db) fn accepted_entity_authority<E>")
            && session_mod.contains("pub(in crate::db) fn accepted_entity_authority_for_schema<E>")
            && session_mod.contains("EntityAuthority::from_accepted_schema_for_type::<E>(")
            && !session_mod.contains("EntityAuthority::for_generated_type_for_test::<E>()")
            && entity_authority.contains("fn from_accepted_schema_for_type<E>")
            && entity_authority
                .contains("AcceptedRowLayoutRuntimeDescriptor::from_generated_compatible_schema(")
            && entity_authority.contains("with_accepted_row_decode_contract(")
            && session_query_cache.contains("accepted_entity_authority::<E>()")
            && session_query_cache.contains("cached_shared_query_plan_for_accepted_authority(")
            && !session_query_cache.contains("EntityAuthority::for_generated_type_for_test::<E>()")
            && session_sql_cache.contains("accepted_entity_authority::<E>()")
            && !session_sql_cache.contains("EntityAuthority::for_generated_type_for_test::<E>()")
            && session_sql_execute.contains("sql_select_prepared_plan_for_entity::<E>(query)")
            && session_sql_execute.contains("accepted_entity_authority::<E>()")
            && !session_sql_execute.contains("EntityAuthority::for_generated_type_for_test::<E>()")
            && session_sql_explain.contains("accepted_schema: &AcceptedSchemaSnapshot")
            && session_sql_explain.contains("cached_shared_query_plan_for_accepted_authority(")
            && !session_sql_explain.contains("ensure_accepted_schema_snapshot_for_authority(")
            && !session_sql_explain.contains("cached_shared_query_plan_for_authority(")
            && session_sql_write.contains("accepted_entity_authority_for_schema::<E>")
            && !session_sql_write.contains("EntityAuthority::for_generated_type_for_test::<E>()"),
        "typed runtime SQL/query dispatch must select accepted EntityAuthority at the session boundary instead of passing generated authority to lower helpers",
    );
}

#[test]
fn cursor_boundary_validation_uses_authority_schema_info() {
    let cursor_boundary = read_source("src/db/cursor/boundary.rs");
    let cursor_mod = read_source("src/db/cursor/mod.rs");
    let cursor_spine = read_source("src/db/cursor/spine.rs");
    let continuation = read_source("src/db/query/plan/continuation.rs");
    let entity_authority = read_source("src/db/executor/authority/entity.rs");
    let entity_authority_compact = compact_source(&entity_authority);

    assert!(
        cursor_boundary.contains("schema: &SchemaInfo,")
            && !cursor_boundary.contains("SchemaInfo::cached_for_generated_entity_model(model)")
            && !cursor_boundary.contains("fn boundary_schema("),
        "cursor boundary validation must consume caller-supplied schema info instead of reopening generated schema metadata",
    );
    assert!(
        cursor_mod.contains("schema: &SchemaInfo,")
            && cursor_spine.contains("fn schema_info(&self) -> &SchemaInfo;")
            && continuation.contains("schema_info: &SchemaInfo,"),
        "cursor preparation and revalidation must thread planner/session-selected schema info through the cursor spine",
    );
    assert!(
        entity_authority.contains("accepted_schema_info: Option<Arc<SchemaInfo>>,")
            && entity_authority_compact
                .contains("fncursor_schema_info(&self)->Result<&SchemaInfo,CursorPlanError>")
            && entity_authority.contains("contract.prepare_scalar_cursor(")
            && entity_authority.contains("contract.revalidate_scalar_cursor(")
            && entity_authority_compact.contains(
                "authority.with_accepted_row_decode_contract(row_shape,row_decode_contract,schema_info",
            ),
        "entity authority must carry accepted schema info into scalar cursor validation",
    );
}

#[test]
fn prepared_static_shape_finalization_uses_authority_schema_info() {
    let entity_authority = read_source("src/db/executor/authority/entity.rs");
    let query_plan_logical = read_source("src/db/query/plan/semantics/logical.rs");
    let predicate_runtime = read_source("src/db/predicate/runtime/mod.rs");
    let predicate_capability = read_source("src/db/predicate/capability.rs");
    let schema_info = read_source("src/db/schema/info.rs");

    assert!(
        entity_authority.contains(
            "plan.finalize_static_planning_shape_for_model_with_schema(self.model, schema_info)",
        ) && !entity_authority.contains(".finalize_static_planning_shape_for_model_only(self.model)")
            && !entity_authority.contains("PreparedShapeFinalizationOutcome::GeneratedFallback")
            && query_plan_logical.contains(
                "#[cfg(test)]\n    pub(in crate::db) fn finalize_static_planning_shape_for_model_only("
            ),
        "prepared execution finalization must use authority-carried schema info and keep generated static-shape finalization model-only and test-only",
    );
    assert!(
        schema_info.contains("leaf_codec: LeafCodec,")
            && schema_info.contains("pub(in crate::db) fn field_slot_has_scalar_leaf")
            && predicate_runtime.contains("pub(in crate::db) fn compile_with_schema_info(")
            && predicate_runtime.contains("schema_info.field_slot_index(field_name)")
            && predicate_runtime
                .contains("PredicateCapabilityContext::runtime_schema(schema_info)")
            && query_plan_logical
                .contains("PredicateProgram::compile_with_schema_info(schema_info, predicate)")
            && !query_plan_logical
                .contains("PredicateProgram::compile_for_model_only(model, predicate)")
            && predicate_runtime
                .contains("#[cfg(test)]\n    pub(in crate::db) fn compile_for_model_only(")
            && predicate_capability
                .contains("#[cfg(test)]\n    pub(in crate::db) fn runtime_for_model_only("),
        "prepared predicate compilation and scalar fast-path classification must use schema info, keeping generated model wrappers model-only and test-only",
    );
}

#[test]
fn scalar_aggregate_expression_compilation_uses_accepted_schema_info() {
    let scalar_expr = read_source("src/db/query/plan/expr/scalar.rs");
    let scalar_expr_mod = read_source("src/db/query/plan/expr/mod.rs");
    let aggregate_helpers = read_source("src/db/sql/lowering/aggregate/lowering/helpers.rs");
    let aggregate_terminal = read_source("src/db/executor/aggregate/scalar_terminals/terminal.rs");
    let aggregate_request = read_source("src/db/executor/aggregate/scalar_terminals/request.rs");
    let aggregate_runtime = read_source("src/db/executor/aggregate/scalar_terminals/mod.rs");
    let session_global_aggregate = read_source("src/db/session/sql/execute/global_aggregate.rs");
    let session_sql_execute = read_source("src/db/session/sql/execute/mod.rs");

    assert!(
        scalar_expr.contains(
            "#[cfg(test)]\n#[must_use]\npub(in crate::db) fn compile_scalar_projection_expr_for_model_only("
        ) && scalar_expr_mod.contains(
            "#[cfg(test)]\npub(in crate::db) use scalar::compile_scalar_projection_expr_for_model_only;"
        ),
        "generated-schema scalar projection compiler wrapper must stay explicitly model-only and test-only",
    );
    assert!(
        scalar_expr.contains(
            "pub(in crate::db) fn compile_scalar_projection_expr_with_schema(\n    schema: &SchemaInfo,"
        ) && scalar_expr.contains(
            "pub(in crate::db) fn compile_scalar_projection_plan_with_schema(\n    schema: &SchemaInfo,"
        ) && !scalar_expr.contains(
            "pub(in crate::db) fn compile_scalar_projection_expr_with_schema(\n    model:"
        ) && !scalar_expr.contains(
            "pub(in crate::db) fn compile_scalar_projection_plan_with_schema(\n    model:"
        ),
        "shared scalar projection compilers must take schema info directly without generated model parameters",
    );
    assert!(
        aggregate_helpers.contains("schema: &SchemaInfo,")
            && aggregate_helpers
                .contains("compile_scalar_projection_expr_with_schema(schema, expr)",)
            && !aggregate_helpers
                .contains("compile_scalar_projection_expr_for_model_only(model, expr)"),
        "SQL aggregate scalar-expression validation must compile against caller-supplied schema info",
    );
    assert!(
        aggregate_terminal.contains("schema: &SchemaInfo,")
            && aggregate_terminal
                .contains("compile_scalar_projection_expr_from_schema(schema, expr)",)
            && aggregate_terminal.contains("schema.field_slot_index(field.as_str()).is_none()")
            && !aggregate_terminal.contains("EntityModel")
            && !aggregate_terminal
                .contains("compile_scalar_projection_expr_for_model_only(model, expr)")
            && !aggregate_terminal.contains("model.resolve_field_slot(field.as_str()).is_none()"),
        "structural aggregate terminal expression compilation must use accepted schema info for scalar slot resolution",
    );
    assert!(
        aggregate_request.contains("schema_info: SchemaInfo,")
            && aggregate_request.contains("pub(super) const fn schema_info(&self) -> &SchemaInfo")
            && aggregate_runtime.contains("request.schema_info()")
            && aggregate_runtime
                .contains("terminal.uses_shared_count_terminal(request.schema_info())")
            && !aggregate_runtime.contains("terminal.uses_shared_count_terminal(E::MODEL)")
            && !aggregate_runtime.contains(
                "compile_structural_scalar_aggregate_terminal(\n                    E::MODEL,"
            )
            && aggregate_terminal
                .contains("pub(super) fn uses_shared_count_terminal(&self, schema: &SchemaInfo)")
            && aggregate_terminal
                .contains("schema\n                    .field_nullable(target_slot.field())")
            && !aggregate_terminal.contains("model.fields().get(target_slot.index())")
            && session_global_aggregate.contains("accepted_schema_info_for_entity::<E>()")
            && session_global_aggregate.contains(
                "StructuralAggregateRequest::new(terminals, projection, having, schema_info)",
            ),
        "session global aggregate execution must carry accepted schema info into structural aggregate runtime",
    );
    assert!(
        session_global_aggregate.contains("fn execute_global_aggregate_statement<E>")
            && !session_global_aggregate.contains("EntityAuthority")
            && !session_global_aggregate.contains("for_authority")
            && session_sql_execute.contains(
                "CompiledSqlCommand::GlobalAggregate { command } => {\n                self.execute_global_aggregate_statement::<E>(",
            )
            && !session_sql_execute.contains(
                "CompiledSqlCommand::GlobalAggregate { command } => {\n                let authority = EntityAuthority::for_generated_type_for_test::<E>();",
            ),
        "SQL global aggregate execution must not retain a generated EntityAuthority bootstrap lane",
    );
}

#[test]
fn fluent_terminal_field_slots_use_accepted_schema_info() {
    let fluent_builder = read_source("src/db/query/fluent/load/builder.rs");
    let fluent_validation = read_source("src/db/query/fluent/load/validation.rs");
    let query_intent = read_source("src/db/query/intent/model.rs");
    let sql_select = read_source("src/db/sql/lowering/select/mod.rs");
    let sql_select_aggregate = read_source("src/db/sql/lowering/select/aggregate.rs");
    let sql_prepare = read_source("src/db/sql/lowering/prepare.rs");
    let session_sql_compile = read_source("src/db/session/sql/compile/core.rs");
    let session_mod = read_source("src/db/session/mod.rs");
    let symbols = read_source("src/db/query/plan/validate/symbols.rs");
    let query_plan_group = read_source("src/db/query/plan/group.rs");
    let query_plan_logical = read_source("src/db/query/plan/semantics/logical.rs");
    let sql_aggregate_strategy = read_source("src/db/sql/lowering/aggregate/strategy.rs");
    let session_global_aggregate = read_source("src/db/session/sql/execute/global_aggregate.rs");

    assert!(
        fluent_validation.contains("accepted_schema_info_for_entity::<E>()")
            && fluent_validation.contains("resolve_aggregate_target_field_slot_with_schema(")
            && !fluent_validation.contains("resolve_aggregate_target_field_slot(E::MODEL"),
        "fluent aggregate/projection terminal field slots must resolve through accepted schema info instead of generated model slot order",
    );
    assert!(
        fluent_builder.contains("query.group_by_with_schema(&field, &schema)")
            && fluent_builder
                .contains("query.having_group_with_schema(&field, &schema, op, value)")
            && sql_select.contains("query.group_by_with_schema(field, schema)?")
            && sql_select_aggregate.contains("schema: &SchemaInfo,")
            && sql_select_aggregate.contains("resolve_group_field_slot_with_schema(")
            && !sql_select_aggregate.contains("resolve_group_field_slot(model")
            && query_intent.contains("fn push_group_field_with_schema(")
            && query_intent.contains("fn push_having_group_clause_with_schema("),
        "fluent and SQL grouped field slots must use accepted schema info at session/lowering boundaries",
    );
    assert!(
        sql_prepare.contains("fn lower_prepared_sql_select_statement_with_schema(")
            && session_sql_compile.contains("lower_prepared_sql_select_statement_with_schema("),
        "session SQL SELECT compilation must lower grouped HAVING canonicalization with accepted schema info",
    );
    assert!(
        session_mod.contains("pub(in crate::db) fn accepted_schema_info_for_entity<E>")
            && session_mod.contains("SchemaInfo::from_accepted_snapshot_for_model(")
            && symbols
                .contains("pub(in crate::db) fn resolve_aggregate_target_field_slot_with_schema(")
            && symbols.contains("pub(in crate::db) fn resolve_group_field_slot_with_schema(")
            && symbols.contains(".field_slot_index(field)")
            && !symbols.contains("fn resolve_aggregate_target_field_slot(")
            && !symbols.contains("model.resolve_field_slot(field)?;"),
        "session and planner symbol helpers must expose accepted-schema field-slot resolution",
    );
    assert!(
        session_global_aggregate.contains("accepted_schema_info_for_entity::<E>()")
            && !session_global_aggregate.contains("ensure_accepted_schema_snapshot::<E>()"),
        "SQL global aggregate execution should reuse the session accepted-schema info helper",
    );
    assert!(
        sql_aggregate_strategy.contains("resolve_aggregate_target_field_slot_with_schema(")
            && !sql_aggregate_strategy.contains("resolve_aggregate_target_field_slot(model"),
        "SQL aggregate field-target strategies must resolve terminal slots through accepted schema info",
    );
    assert!(
        query_plan_group.contains("pub(in crate::db) fn resolve_with_schema_info(")
            && query_plan_group.contains("pub(in crate::db) fn grouped_aggregate_execution_specs(\n    schema_info: &SchemaInfo,")
            && query_plan_group.contains(
                "pub(in crate::db) fn resolved_grouped_distinct_execution_strategy_with_schema_info("
            )
            && query_plan_group.contains("kind: schema_info.field_kind(field).copied(),")
            && !query_plan_group.contains("resolve_for_model(")
            && !query_plan_group.contains("resolved_grouped_distinct_execution_strategy_for_model")
            && !query_plan_group.contains("model_field.name()")
            && query_plan_logical.contains("grouped_aggregate_execution_specs(\n        schema_info,")
            && query_plan_logical
                .contains("resolved_grouped_distinct_execution_strategy_with_schema_info("),
        "grouped aggregate execution specs and grouped DISTINCT target slots must resolve through schema info, not generated model fields",
    );
}

#[test]
fn value_stays_out_of_persisted_field_contracts() {
    let forbidden_impls = [
        "implPersistedFieldSlotCodecforValue",
        "implPersistedFieldSlotCodecforVec<Value>",
        "implPersistedStructuredFieldCodecforValue",
        "implPersistedStructuredFieldCodecforVec<Value>",
        "implFieldTypeMetaforValue",
        "implFieldTypeMetaforVec<Value>",
    ];
    let mut violations = Vec::new();

    // Scan production source only. Compile-fail fixtures intentionally mention
    // these shapes so user-facing errors stay locked down separately.
    for path in rust_sources_under("src") {
        let source = fs::read_to_string(&path)
            .unwrap_or_else(|err| panic!("failed to read {}: {err}", path.display()));
        let compact = compact_source(&source);
        for forbidden in forbidden_impls {
            if compact.contains(forbidden) {
                violations.push(format!("{} contains {forbidden}", path.display()));
            }
        }
    }

    assert!(
        violations.is_empty(),
        "Value is runtime-only and must not implement persisted-field contracts:\n{}",
        violations.join("\n"),
    );
}