memstead-base 0.27.0

Engine internals for Memstead — store, parser, validators, mem lifecycle, workspace policy. Published as a dependency of the memstead-cli and memstead-mcp products, not as a supported library — pre-1.0, no API stability promise.
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
#![cfg(test)]

use super::*;
use memstead_schema::{builtin_names, type_by_name};
use std::sync::Arc;

fn spec_schema() -> Arc<TypeDefinition> {
    type_by_name(builtin_names::SPEC).unwrap()
}

fn memo_schema() -> Arc<TypeDefinition> {
    type_by_name(builtin_names::MEMO).unwrap()
}

#[test]
fn parse_metadata_types() {
    let meta = parse_metadata("key: value\nnum: 42\nfloat: 0.85\nbool: true\nfalsy: false");
    assert_eq!(meta["key"], MetadataValue::String("value".to_string()));
    assert_eq!(meta["num"], MetadataValue::Integer(42));
    assert_eq!(meta["float"], MetadataValue::Float(0.85));
    assert_eq!(meta["bool"], MetadataValue::Bool(true));
    assert_eq!(meta["falsy"], MetadataValue::Bool(false));
}

#[test]
fn parse_metadata_strips_comments() {
    let meta = parse_metadata("key: value # this is a comment");
    assert_eq!(meta["key"], MetadataValue::String("value".to_string()));
}

/// A ` #` inside a quoted value is part of the value, not a comment;
/// the comment can only begin after the closing quote. Inside double
/// quotes `\"` and `\\` read as the quote and the backslash. A value
/// with no closing quote is read as before (unquoted, cut at ` #`).
#[test]
fn parse_metadata_keeps_a_hash_inside_quotes() {
    let meta = parse_metadata(
        "a: \"difference for Project #1 and Project #3 with 55% and 90%\" # note\n\
         b: 'single #1' # note\n\
         c: \"say \\\"hi\\\" and \\\\ back\"\n\
         d: \"unterminated #1\n\
         e: plain #1 here",
    );
    assert_eq!(
        meta["a"],
        MetadataValue::String(
            "difference for Project #1 and Project #3 with 55% and 90%".to_string()
        )
    );
    assert_eq!(meta["b"], MetadataValue::String("single #1".to_string()));
    assert_eq!(
        meta["c"],
        MetadataValue::String("say \"hi\" and \\ back".to_string())
    );
    assert_eq!(
        meta["d"],
        MetadataValue::String("\"unterminated".to_string())
    );
    assert_eq!(meta["e"], MetadataValue::String("plain".to_string()));
}

#[test]
fn parse_metadata_strips_quotes() {
    let meta = parse_metadata("key: \"quoted value\"\nkey2: 'single'");
    assert_eq!(
        meta["key"],
        MetadataValue::String("quoted value".to_string())
    );
    assert_eq!(meta["key2"], MetadataValue::String("single".to_string()));
}

#[test]
fn parse_metadata_survives_malformed_values() {
    // A lone quote character satisfies both starts_with and ends_with —
    // the old unguarded slice `s[1..s.len()-1]` panicked on it.
    let meta = parse_metadata(
        "key: \"\nkey2: '\nkey3: \"\"\nkey4: ''\nkey5: \"unterminated\nkey6: mixed'\"",
    );
    assert_eq!(meta["key"], MetadataValue::String("\"".to_string()));
    assert_eq!(meta["key2"], MetadataValue::String("'".to_string()));
    assert_eq!(meta["key3"], MetadataValue::String(String::new()));
    assert_eq!(meta["key4"], MetadataValue::String(String::new()));
    assert_eq!(
        meta["key5"],
        MetadataValue::String("\"unterminated".to_string())
    );
    assert_eq!(meta["key6"], MetadataValue::String("mixed'\"".to_string()));

    // More frontmatter shapes that must parse to a value, never panic:
    // colon-only lines, multi-byte values, keyless colons, huge digits.
    let meta = parse_metadata(":\n: value\nkey7: ✓\"\nkey8: 99999999999999999999999999\nkey9: -");
    assert_eq!(meta["key7"], MetadataValue::String("✓\"".to_string()));
    assert_eq!(
        meta["key8"],
        MetadataValue::String("99999999999999999999999999".to_string())
    );
    assert_eq!(meta["key9"], MetadataValue::String("-".to_string()));
}

#[test]
fn parse_metadata_skips_comments_and_empty() {
    let meta = parse_metadata("# comment\n\nkey: val\n---");
    assert_eq!(meta.len(), 1);
    assert_eq!(meta["key"], MetadataValue::String("val".to_string()));
}

#[test]
fn peek_type_finds_value() {
    let content = "---\ntype: memo\ntitle: Test\n---\n# Body\n";
    assert_eq!(
        peek_type_from_frontmatter(content),
        Some("memo".to_string())
    );
}

#[test]
fn peek_type_returns_none_when_missing() {
    let content = "---\ntitle: Test\n---\n# Body\n";
    assert_eq!(peek_type_from_frontmatter(content), None);
}

#[test]
fn peek_type_returns_none_without_frontmatter() {
    let content = "# Just a heading\n\nBody with type: concept inside text.\n";
    assert_eq!(peek_type_from_frontmatter(content), None);
}

/// The contract on a carriage-return document: the opening fence is
/// five bytes, the meta slice carries no fence, and the body starts
/// after the closing fence's `\r\n` — the shape the CR-blind site of
/// the 2026-09-04 census got wrong with a four-byte offset constant.
#[test]
fn split_core_crlf_document_borrows_meta_and_body() {
    let content = "---\r\ntype: spec\r\nlevel: M0\r\n---\r\n# Title\r\n\r\nBody.\r\n";
    let (stripped, split) = split_frontmatter_core(content);
    assert_eq!(stripped, content);
    assert_eq!(
        split,
        Frontmatter::Present {
            meta: "type: spec\r\nlevel: M0\r",
            body: "# Title\r\n\r\nBody.\r\n"
        }
    );
    let (meta, body) = frontmatter_parts(content).unwrap();
    assert_eq!(meta, "type: spec\r\nlevel: M0\r");
    assert_eq!(body, "# Title\r\n\r\nBody.\r\n");
    assert_eq!(body_after_frontmatter(content), body);
    // The same document with a byte-order mark splits identically.
    let marked = format!("\u{feff}{content}");
    assert_eq!(frontmatter_parts(&marked), Some((meta, body)));
}

/// A fence anywhere but the first line is not frontmatter: the whole
/// document is body, and nothing is injected or stripped at the first
/// fence found — the shape the fence-searching site of the census got
/// wrong.
#[test]
fn first_fence_not_at_document_start_is_no_frontmatter() {
    let content = "# Intro\n\n---\ntype: spec\n---\n\nBody.\n";
    assert_eq!(
        split_frontmatter_core(content),
        (content, Frontmatter::NoOpeningDelimiter)
    );
    assert_eq!(frontmatter_parts(content), None);
    assert_eq!(body_after_frontmatter(content), content);
    assert_eq!(peek_type_from_frontmatter(content), None);
    // An opening fence that never closes is not frontmatter either.
    let unclosed = "---\ntype: spec\nno close\n";
    assert_eq!(
        split_frontmatter_core(unclosed),
        (unclosed, Frontmatter::Unclosed)
    );
    assert_eq!(frontmatter_parts(unclosed), None);
}

#[test]
fn peek_type_handles_windows_line_endings() {
    let content = "---\r\ntype: principle\r\n---\r\n# Body\r\n";
    assert_eq!(
        peek_type_from_frontmatter(content),
        Some("principle".to_string())
    );
}

#[test]
fn peek_type_strips_quotes_and_comments() {
    let quoted = "---\ntype: \"concept\"\n---\n";
    assert_eq!(
        peek_type_from_frontmatter(quoted),
        Some("concept".to_string())
    );
    let commented = "---\ntype: memo # kind of\n---\n";
    assert_eq!(
        peek_type_from_frontmatter(commented),
        Some("memo".to_string())
    );
}

#[test]
fn peek_type_empty_value_returns_none() {
    let content = "---\ntype:\n---\n";
    assert_eq!(peek_type_from_frontmatter(content), None);
}

#[test]
fn peek_type_ignores_legacy_schema_key() {
    // After the hard break, a bare `schema:` in frontmatter is not
    // recognized as the type key — it's just arbitrary metadata.
    let content = concat!("---\n", "schema", ": memo\n---\n");
    assert_eq!(peek_type_from_frontmatter(content), None);
}

#[test]
fn mask_code_blocks_basic() {
    let input = "before\n```\ncode [[link]]\n```\nafter";
    let masked = mask_code_blocks(input);
    assert!(!masked.contains("[[link]]"));
    assert!(masked.contains("before"));
    assert!(masked.contains("after"));
}

#[test]
fn mask_code_blocks_preserves_line_count() {
    let input = "line1\n```\ncode\nmore code\n```\nline6";
    let masked = mask_code_blocks(input);
    assert_eq!(input.lines().count(), masked.lines().count());
}

#[test]
fn mask_code_blocks_unclosed() {
    let input = "before\n```\ncode\nmore code";
    let masked = mask_code_blocks(input);
    assert!(masked.contains("before"));
    assert!(!masked.contains("code"));
}

#[test]
fn parse_relationships_basic() {
    let text = "- **USES**: [[target-entity]]\n- **PART_OF**: [[parent]]";
    let rels = parse_relationships_with_warnings(text, "specs", None).0;
    assert_eq!(rels.len(), 2);
    assert_eq!(rels[0].rel_type, "USES");
    assert_eq!(rels[0].target.0, "specs--target-entity");
    assert_eq!(rels[1].rel_type, "PART_OF");
    assert_eq!(rels[1].target.0, "specs--parent");
    // Simple form parses without a description.
    assert!(rels[0].description.is_none());
    assert!(rels[1].description.is_none());
}

#[test]
fn parse_relationships_canonical_em_dash_captures_description() {
    let text = "- **OTHER**: [[a]] \u{2014} replaced by checkout-flow";
    let (rels, warnings) = parse_relationships_with_warnings(text, "specs", None);
    assert_eq!(rels.len(), 1);
    assert_eq!(
        rels[0].description.as_deref(),
        Some("replaced by checkout-flow")
    );
    assert!(warnings.is_empty(), "canonical em-dash does not warn");
}

#[test]
fn parse_relationships_em_dash_inside_description_body() {
    let text = "- **OTHER**: [[a]] \u{2014} note with — inside body";
    let (rels, warnings) = parse_relationships_with_warnings(text, "specs", None);
    assert_eq!(rels.len(), 1);
    assert_eq!(
        rels[0].description.as_deref(),
        Some("note with — inside body"),
        "the parser captures up to end-of-line; em-dashes inside the body survive"
    );
    assert!(warnings.is_empty());
}

#[test]
fn parse_relationships_ambiguous_double_hyphen_warns_and_drops_content() {
    let text = "- **USES**: [[a]] -- legacy delimiter";
    let entity_id = EntityId::new("specs", "src");
    let (rels, warnings) = parse_relationships_with_warnings(text, "specs", Some(&entity_id));
    assert_eq!(rels.len(), 1);
    assert!(rels[0].description.is_none(), "trailing content is dropped");
    assert_eq!(warnings.len(), 1);
    assert!(matches!(
        warnings[0],
        crate::ops::WarningHint::AmbiguousDescriptionDelimiter { .. }
    ));
}

#[test]
fn parse_relationships_ambiguous_single_hyphen_warns_and_drops_content() {
    let text = "- **USES**: [[a]] - single hyphen";
    let entity_id = EntityId::new("specs", "src");
    let (rels, warnings) = parse_relationships_with_warnings(text, "specs", Some(&entity_id));
    assert_eq!(rels.len(), 1);
    assert!(rels[0].description.is_none());
    assert_eq!(warnings.len(), 1);
    assert_eq!(warnings[0].code(), "AMBIGUOUS_DESCRIPTION_DELIMITER");
}

#[test]
fn parse_relationships_hyphenated_slug_target_parses_unambiguously() {
    let text = "- **USES**: [[some-slug-with-hyphens]] \u{2014} ok";
    let (rels, warnings) = parse_relationships_with_warnings(text, "specs", None);
    assert_eq!(rels.len(), 1);
    assert_eq!(rels[0].target.path(), "some-slug-with-hyphens");
    assert_eq!(rels[0].description.as_deref(), Some("ok"));
    assert!(warnings.is_empty());
}

#[test]
fn parse_full_entity() {
    let md = "\
---
type: spec
created_date: 2026-01-15
last_modified: 2026-04-12
level: M0
tags: backend, api
---
# Test Entity

## Identity

This is a test entity.

## Purpose

Testing the parser.

## Relationships

- **USES**: [[other-entity]]

## Specifies

Some specification content with [[inline-link]].
";
    let result = parse_markdown(md, "test-entity.md", &spec_schema(), "specs").unwrap();
    let entity = &result.entity;
    assert_eq!(entity.id.0, "specs--test-entity");
    assert_eq!(entity.title, "Test Entity");
    assert_eq!(entity.mem, "specs");
    assert_eq!(
        entity.metadata["type"],
        MetadataValue::String("spec".to_string())
    );
    assert_eq!(
        entity.metadata["level"],
        MetadataValue::String("M0".to_string())
    );
    assert_eq!(
        entity.metadata["tags"],
        MetadataValue::String("backend, api".to_string())
    );
    assert_eq!(entity.sections["identity"], "This is a test entity.");
    assert_eq!(entity.sections["purpose"], "Testing the parser.");
    assert_eq!(entity.relationships.len(), 1);
    assert_eq!(entity.relationships[0].rel_type, "USES");
    assert_eq!(entity.relationships[0].target.0, "specs--other-entity");
    assert_eq!(result.inline_links.len(), 1);
    assert_eq!(result.inline_links[0].0, "specs--inline-link");
}

#[test]
fn parse_full_entity_memo_schema() {
    let md = "\
---
type: memo
created_date: 2026-01-15
last_modified: 2026-04-12
status: active
tags: decision, architecture
---
# Use Sled For Storage

## Claim

Sled is the right embedded store for this workload.

## Context

We evaluated sled, rocksdb, and sqlite for the in-process graph cache.

## Substance

Sled wins on pure-Rust dependency footprint.
";
    let result = parse_markdown(md, "use-sled.md", &memo_schema(), "memos").unwrap();
    let entity = &result.entity;
    assert_eq!(entity.id.0, "memos--use-sled");
    assert_eq!(entity.title, "Use Sled For Storage");
    assert_eq!(entity.mem, "memos");
    assert_eq!(
        entity.metadata["type"],
        MetadataValue::String("memo".to_string())
    );
    assert_eq!(
        entity.metadata["status"],
        MetadataValue::String("active".to_string())
    );
    assert_eq!(
        entity.sections["claim"],
        "Sled is the right embedded store for this workload."
    );
    assert_eq!(
        entity.sections["context"],
        "We evaluated sled, rocksdb, and sqlite for the in-process graph cache."
    );
    assert_eq!(
        entity.sections["substance"],
        "Sled wins on pure-Rust dependency footprint."
    );
    assert!(!entity.sections.contains_key("identity"));
    assert!(!entity.sections.contains_key("purpose"));
}

#[test]
fn parse_entity_without_frontmatter() {
    let md = "# No Frontmatter\n\n## Identity\n\nJust a title and section.";
    let result = parse_markdown(md, "no-fm.md", &spec_schema(), "specs").unwrap();
    assert_eq!(result.entity.title, "No Frontmatter");
    // Only the auto-injected type field should be present
    assert_eq!(result.entity.metadata.len(), 1);
    assert_eq!(
        result.entity.metadata.get("type"),
        Some(&MetadataValue::String("spec".to_string()))
    );
}

#[test]
fn parse_entity_code_blocks_not_detected() {
    let md = "\
---
type: spec
---
# Code Test

## Identity

Test entity.

## Specifies

```
## Not A Section
- **USES**: [[not-a-link]]
```

Real content after code block.
";
    let result = parse_markdown(md, "code-test.md", &spec_schema(), "specs").unwrap();
    // The ## inside code block should NOT be parsed as a section
    assert!(!result.entity.sections.contains_key("not a section"));
    // The wiki-link inside code block should NOT be extracted
    assert!(result.inline_links.is_empty());
}

// Fixture pinned by the adversarial harness (seed 0x5eedf001, case 224):
// a BOM'd file parsed tolerantly as all-body, losing its entire
// frontmatter, while the strict validator and the archive path stripped
// the mark and saw it. That divergence is why the mark is now stripped in
// `split_frontmatter_core` and nowhere else: one implementation cannot
// disagree with itself about where a document begins.
#[test]
fn bom_prefixed_frontmatter_is_recognized() {
    let md = "\u{feff}---\ntype: spec\n---\n# Bom Entity\n\n## Identity\n\nBody.\n";
    assert_eq!(peek_type_from_frontmatter(md), Some("spec".to_string()));
    assert_eq!(
        body_after_frontmatter(md),
        "# Bom Entity\n\n## Identity\n\nBody.\n"
    );
    let (meta, body) = split_frontmatter(md).unwrap();
    assert_eq!(meta, "type: spec");
    assert_eq!(body, "# Bom Entity\n\n## Identity\n\nBody.\n");
    let result = parse_markdown(md, "bom.md", &spec_schema(), "specs").unwrap();
    assert_eq!(
        result.entity.metadata["type"],
        MetadataValue::String("spec".to_string())
    );
    assert_eq!(result.entity.sections["identity"], "Body.");
}

// Fixture pinned by the adversarial harness (seed 0x5eedf001, case 46):
// a section whose content ends inside an open code fence absorbed every
// section the generator wrote after it on the next parse — content
// shifted between sections and the document GREW on every
// parse→generate round. The generator now terminates the open fence;
// the first round normalises, then parse→generate is a fixpoint.
#[test]
fn open_fence_in_section_content_does_not_swallow_following_sections() {
    let md = "\
---
type: spec
---
# Code Test

## Identity

Base.

## Specifies

```
truncated code with no closer";
    let schema = spec_schema();
    let e1 = parse_markdown(md, "open-fence.md", &schema, "specs").unwrap();
    let m1 = crate::entity::generator::generate_markdown(&e1.entity, &schema);
    let e2 = parse_markdown(&m1, "open-fence.md", &schema, "specs").unwrap();
    assert_eq!(
        e2.entity.sections["identity"], "Base.",
        "sections before the open fence survive"
    );
    assert!(
        !e2.entity.sections["specifies"].contains("## Constraints"),
        "the generated sections after the fence are not absorbed into it"
    );
    let m2 = crate::entity::generator::generate_markdown(&e2.entity, &schema);
    assert_eq!(
        m1, m2,
        "parse→generate is a fixpoint after one normalising round"
    );
}

// Fixture pinned by the adversarial harness (seed 0x5eedf001, case 26):
// a document carrying MULTIPLE non-schema sections — reachable on the
// tolerant local-read path, which refuses nothing — reconstructed its
// catch-all in HashMap iteration order, so canonical bytes differed
// from parse to parse of the same input. The catch-all must re-emit
// non-schema sections in document order, and parse→generate must be
// idempotent for such input.
#[test]
fn catch_all_reconstruction_is_document_ordered_and_idempotent() {
    let md = "\
---
type: spec
---
# Multi Unknown

## Identity

Base.

## Claim

First unknown.

## Context

Second unknown.

## Substance

Third unknown.
";
    let schema = spec_schema();
    let e1 = parse_markdown(md, "multi-unknown.md", &schema, "specs").unwrap();
    assert_eq!(
        e1.entity.sections["specifies"],
        "## Claim\nFirst unknown.\n\n## Context\nSecond unknown.\n\n## Substance\nThird unknown.",
        "non-schema sections land in the catch-all in document order"
    );
    let m1 = crate::entity::generator::generate_markdown(&e1.entity, &schema);
    let e2 = parse_markdown(&m1, "multi-unknown.md", &schema, "specs").unwrap();
    let m2 = crate::entity::generator::generate_markdown(&e2.entity, &schema);
    assert_eq!(
        m1, m2,
        "parse→generate is idempotent over multi-unknown-section input"
    );
}

// Fixture pinned by the coverage-guided long tier (local run,
// 2026-08-24; corpus member `crash-fd71330e…`): parse_markdown
// re-trimmed every section value after split_sections had already
// normalised it, silently promoting a whitespace-prefixed first
// line (vertical tab + backticks) to column 0 — where the
// CommonMark referee saw a fence opener the stored form did not
// have, so the section structure shifted between rounds. The
// splitter's trim is the only content trim.
#[test]
fn first_line_whitespace_prefix_survives_storage_and_round_trips() {
    let schema = spec_schema();
    let md = "---\ntype: spec\n---\n# T\n\n## Identity\n\u{b}```\nx\n\n## Purpose\np\n";
    let e1 = parse_markdown(md, "vt.md", &schema, "specs").unwrap();
    assert_eq!(
        e1.entity.sections["identity"], "\u{b}```\nx",
        "the first visible line keeps its whitespace prefix byte-exactly"
    );
    let m1 = crate::entity::generator::generate_markdown(&e1.entity, &schema);
    let e2 = parse_markdown(&m1, "vt.md", &schema, "specs").unwrap();
    let m2 = crate::entity::generator::generate_markdown(&e2.entity, &schema);
    assert_eq!(m1, m2, "parse→generate is a fixpoint");
}

// Fixture pinned by the coverage-guided long tier (0.17.0 release
// readiness run, 2026-09-02; corpus member `crash-1233c134…`): a
// non-schema section whose content ends inside an HTML block of a
// kind no blank line ends (`<!X`, type 4) was merged in front of a
// later piece. In situ a `>` line in the catch-all's own content had
// closed the block, so the later piece's tilde fence opened and
// masked a `## ` line into content; merged, the block stayed open,
// the fence became prose, the masked line surfaced as an empty
// non-schema heading, and the next round dropped it. The merge's
// incremental context close now terminates HTML blocks like fences.
#[test]
fn merged_open_html_block_is_closed_so_later_fences_keep_masking() {
    let schema = spec_schema();
    let md = "\
---
type: spec
---
# T

## Claim
<!X

## Specifies
done >

## Later
~~~
## Hidden
";
    let e1 = parse_markdown(md, "html.md", &schema, "specs").unwrap();
    assert_eq!(
        e1.entity.sections["specifies"],
        "done >\n\n## Claim\n<!X\n>\n\n## Later\n~~~\n## Hidden\n~~~",
        "the open HTML block closes before the next piece, the dangling fence after it"
    );
    let m1 = crate::entity::generator::generate_markdown(&e1.entity, &schema);
    let e2 = parse_markdown(&m1, "html.md", &schema, "specs").unwrap();
    let m2 = crate::entity::generator::generate_markdown(&e2.entity, &schema);
    assert_eq!(m1, m2, "parse→generate is a fixpoint");
}

// Fixture pinned by the CI fuzz run of 2026-09-08 (corpus member
// `crash-c9e7bbf7…`), the mirror image of the case above: two
// adjacent non-schema sections, the first ending inside a `<!X`
// block that the SECOND closes with its `>` line, so the second
// heading stood inside the block. In situ the `<!--` before that
// line was inert inside the block and the tilde fence after it
// masked `## Hidden` into content. Written from a neutral start,
// the `<!--` is a live comment, the fence is dead and `## Hidden`
// is a heading; the second parse split there and carried a second
// `-->`. The splitter now reads a section that stood inside a
// block the way it will be written, so `## Hidden` is a boundary
// on the first parse already (an empty non-schema section, dropped
// as every such section is) and the comment closes before it.
#[test]
fn section_that_stood_inside_the_previous_sections_block_is_read_as_written() {
    let schema = spec_schema();
    let md = "\
---
type: spec
---
# T

## First
<!X

## Second
<!--
>
~~~
## Hidden
";
    let e1 = parse_markdown(md, "html.md", &schema, "specs").unwrap();
    assert_eq!(
        e1.entity.sections["specifies"], "## First\n<!X\n>\n\n## Second\n<!--\n>\n~~~\n-->",
        "the second section ends where a neutral read would split it, and its comment closes there"
    );
    assert_eq!(
        e1.entity.raw_section_headings,
        vec!["First", "Second", "Hidden"],
        "the surfaced line is a boundary on the first parse"
    );
    let m1 = crate::entity::generator::generate_markdown(&e1.entity, &schema);
    let e2 = parse_markdown(&m1, "html.md", &schema, "specs").unwrap();
    let m2 = crate::entity::generator::generate_markdown(&e2.entity, &schema);
    assert_eq!(m1, m2, "parse→generate is a fixpoint");
}

// Fixture pinned by the CI fuzz run of 2026-09-09 (corpus member
// `crash-ce631bf5…`), the sibling with no section before the block:
// the `<!X` block opens in the preamble under the title, so the only
// section's heading stands inside it and no closer between merged
// pieces exists to blame. Same in-situ reading (inert `<!--`, live
// fence, masked `## Hidden`), same neutral reading (live comment,
// dead fence, surfaced heading); the splitter's second pass makes
// the first parse agree with the second.
#[test]
fn section_that_stood_inside_a_preamble_block_is_read_as_written() {
    let schema = spec_schema();
    let md = "\
---
type: spec
---
# T
<!X
## Only
<!--
>
~~~
## Hidden
";
    let e1 = parse_markdown(md, "html.md", &schema, "specs").unwrap();
    assert_eq!(
        e1.entity.sections["specifies"], "## Only\n<!--\n>\n~~~\n-->",
        "the section ends where a neutral read would split it, and its comment closes there"
    );
    let m1 = crate::entity::generator::generate_markdown(&e1.entity, &schema);
    let e2 = parse_markdown(&m1, "html.md", &schema, "specs").unwrap();
    let m2 = crate::entity::generator::generate_markdown(&e2.entity, &schema);
    assert_eq!(m1, m2, "parse→generate is a fixpoint");
}

// Fixture pinned by the coverage-guided long tier (first dispatch,
// 2026-08-24; corpus member `crash-de0c69e0…`): the splitter's full
// content trim promoted an INDENTED heading-lookalike on the first
// content line (` ## Specifies`) to column 0 inside stored content;
// the catch-all re-emit then made the next parse read it as a real
// duplicate section heading, whose first-wins rule dropped the
// content — structure from content, and a broken fixpoint. Leading
// blank lines still drop; the first visible line keeps its
// indentation.
#[test]
fn indented_heading_lookalike_stays_content_and_round_trips() {
    let md = "\
---
type: spec
---
# Promoted Heading

## Identity

Base.

## Unknown Extra

 ## Specifies

Some content that must survive.
";
    let schema = spec_schema();
    let e1 = parse_markdown(md, "indent.md", &schema, "specs").unwrap();
    assert!(
        e1.entity.sections["specifies"].contains(" ## Specifies"),
        "the indented lookalike keeps its indentation inside the catch-all"
    );
    assert!(
        e1.entity.sections["specifies"].contains("Some content that must survive."),
        "content after the lookalike is preserved"
    );
    let m1 = crate::entity::generator::generate_markdown(&e1.entity, &schema);
    let e2 = parse_markdown(&m1, "indent.md", &schema, "specs").unwrap();
    let m2 = crate::entity::generator::generate_markdown(&e2.entity, &schema);
    assert_eq!(
        m1, m2,
        "parse→generate is a fixpoint after one normalising round"
    );
    assert!(
        e2.entity.sections["specifies"].contains("Some content that must survive."),
        "no content is lost across rounds"
    );
}

#[test]
fn compute_hash_deterministic() {
    let hash1 = compute_hash("test content");
    let hash2 = compute_hash("test content");
    assert_eq!(hash1, hash2);
    assert_eq!(hash1.len(), 16);
}

#[test]
fn compute_hash_differs() {
    let hash1 = compute_hash("content a");
    let hash2 = compute_hash("content b");
    assert_ne!(hash1, hash2);
}

#[test]
fn is_float_literal_matches() {
    assert!(is_float_literal("0.85"));
    assert!(is_float_literal("-1.5"));
    assert!(is_float_literal("100.0"));
    assert!(!is_float_literal(".5"));
    assert!(!is_float_literal("1."));
    assert!(!is_float_literal("42"));
    assert!(!is_float_literal("hello"));
}

#[test]
fn is_integer_literal_matches() {
    assert!(is_integer_literal("42"));
    assert!(is_integer_literal("-1"));
    assert!(is_integer_literal("0"));
    assert!(!is_integer_literal("0.5"));
    assert!(!is_integer_literal("hello"));
    assert!(!is_integer_literal(""));
}

// Regression lock for metadata-key order. The parser reads frontmatter
// line-by-line into an IndexMap, so metadata iteration yields the file's
// declared key order. Render sites iterate entity.metadata directly (see
// `render::render_entity_markdown`), so any regression to HashMap
// reintroduces hash-seed-dependent frontmatter ordering in MCP output.
#[test]
fn parse_preserves_frontmatter_key_order() {
    let md = "\
---
type: principle
universality: domain-wide
authority: proposed
tags: a, b, c
created_date: 2026-01-15
last_modified: 2026-04-12
---
# Key Order
";
    let result = parse_markdown(
        md,
        "key-order.md",
        &type_by_name(builtin_names::PRINCIPLE).unwrap(),
        "knowledge",
    )
    .unwrap();
    let keys: Vec<&str> = result.entity.metadata.keys().map(|s| s.as_str()).collect();
    assert_eq!(
        keys,
        vec![
            "type",
            "universality",
            "authority",
            "tags",
            "created_date",
            "last_modified",
        ],
        "metadata iteration must preserve frontmatter declaration order"
    );
}

// Regression lock for section-order round-trip stability. Today this
// passes by construction: the parser inserts keys in schema-declared
// order, the generator writes them in schema-declared order, and
// `IndexMap` preserves that order across re-parses. HashMap iteration
// order was the hole — an IndexMap-based entity.sections closes it.
// Keep the test; if a future refactor reintroduces a HashMap anywhere on
// the parse/write path, this catches it.
#[test]
fn parse_write_roundtrip_preserves_section_order() {
    let md = "\
---
type: spec
created_date: 2026-01-15
last_modified: 2026-04-12
level: M0
---
# Order Roundtrip

## Identity

Identity content.

## Purpose

Purpose content.

## Specifies

Specifies content.
";
    let schema = spec_schema();
    let first = parse_markdown(md, "order-roundtrip.md", &schema, "specs").unwrap();
    let regenerated = crate::entity::generator::generate_markdown(&first.entity, &schema);
    let second = parse_markdown(&regenerated, "order-roundtrip.md", &schema, "specs").unwrap();

    let first_keys: Vec<&String> = first.entity.sections.keys().collect();
    let second_keys: Vec<&String> = second.entity.sections.keys().collect();
    assert_eq!(
        first_keys, second_keys,
        "section iteration order must survive parse -> generate -> parse"
    );
}

// ------------------------------------------------------------------
// Heading-spans extraction (H3–H6)
//
// These lock the parser contract: one extra pass per section that
// records H3+ headings as a side-struct. Flat storage; level skips
// are tolerated; code blocks are ignored. See
// `extract_heading_spans`.
// ------------------------------------------------------------------

#[test]
fn parser_extracts_single_h3() {
    let md = "\
---
type: spec
---
# Entity

## Identity

Body.

## Specifies

### Response Shapes

Content under response shapes.
";
    let result = parse_markdown(md, "h3-single.md", &spec_schema(), "specs").unwrap();
    let spans = result
        .entity
        .heading_spans
        .get("specifies")
        .expect("specifies section should have spans");
    assert_eq!(spans.len(), 1);
    assert_eq!(spans[0].level, 3);
    assert_eq!(spans[0].title, "Response Shapes");
    // The section is trimmed, so the H3 sits at offset 0.
    assert_eq!(spans[0].start_offset, 0);
    let section = result.entity.sections.get("specifies").unwrap();
    assert_eq!(spans[0].end_offset, section.len());
    // Non-specifies sections either get no entry or the content has no H3+ headings.
    assert!(
        result
            .entity
            .heading_spans
            .get("identity")
            .is_none_or(Vec::is_empty)
    );
}

#[test]
fn parser_extracts_nested_h3_h4() {
    let md = "\
---
type: spec
---
# Entity

## Identity

Body.

## Specifies

### Outer

Outer body.

#### Inner

Inner body.
";
    let result = parse_markdown(md, "h3-h4.md", &spec_schema(), "specs").unwrap();
    let spans = result.entity.heading_spans.get("specifies").unwrap();
    assert_eq!(spans.len(), 2, "both H3 and H4 must be recorded");
    assert_eq!(spans[0].level, 3);
    assert_eq!(spans[0].title, "Outer");
    assert_eq!(spans[1].level, 4);
    assert_eq!(spans[1].title, "Inner");
    assert!(
        spans[0].start_offset < spans[1].start_offset,
        "spans must be in document order"
    );
    // H3 contains H4: H3.end_offset must cover H4.start_offset.
    assert!(
        spans[0].end_offset > spans[1].start_offset,
        "outer H3 must contain inner H4 by offset"
    );
}

#[test]
fn parser_ignores_headings_in_code_blocks() {
    let md = "\
---
type: spec
---
# Entity

## Identity

Body.

## Specifies

Prefix.

```
### Not a heading
Still code.
```

Suffix.
";
    let result = parse_markdown(md, "h3-code.md", &spec_schema(), "specs").unwrap();
    let spans = result
        .entity
        .heading_spans
        .get("specifies")
        .cloned()
        .unwrap_or_default();
    assert!(
        spans.is_empty(),
        "a '### ' inside a fenced block must not register as a heading span: {spans:?}"
    );
}

#[test]
fn parser_handles_level_skip() {
    let md = "\
---
type: spec
---
# Entity

## Identity

Body.

## Specifies

#### Skipped To H4

Content under a sudden H4 — no virtual H3 is inserted.
";
    let result = parse_markdown(md, "h2-h4.md", &spec_schema(), "specs").unwrap();
    let spans = result.entity.heading_spans.get("specifies").unwrap();
    assert_eq!(spans.len(), 1);
    assert_eq!(spans[0].level, 4);
    assert_eq!(spans[0].title, "Skipped To H4");
}

#[test]
fn parser_handles_duplicate_siblings() {
    let md = "\
---
type: spec
---
# Entity

## Identity

Body.

## Specifies

### Same Title

First occurrence body.

### Same Title

Second occurrence body.
";
    let result = parse_markdown(md, "h3-dup.md", &spec_schema(), "specs").unwrap();
    let spans = result.entity.heading_spans.get("specifies").unwrap();
    assert_eq!(spans.len(), 2, "duplicate siblings must produce two spans");
    assert_eq!(spans[0].title, spans[1].title);
    assert_ne!(
        spans[0].start_offset, spans[1].start_offset,
        "spans with identical titles must be distinguishable by offset"
    );
    // Siblings at the same level: neither contains the other.
    assert!(
        spans[0].end_offset <= spans[1].start_offset,
        "first sibling must close before the second starts"
    );
}

// Duplicate `## Heading` lines for a schema-declared key collapse to the
// first occurrence's body and emit a `DuplicateSectionHeading` warning.
// Catch-all keys absorb arbitrary headings by design and do not warn.

#[test]
fn duplicate_declared_heading_two_populated_keeps_first_warns() {
    let md = "---\ntype: spec\n---\n# Title\n\n## Identity\n\nfirst body\n\n## Identity\n\nsecond body\n";
    let result = parse_markdown(md, "x.md", &spec_schema(), "v").unwrap();
    assert_eq!(
        result.entity.sections.get("identity").map(String::as_str),
        Some("first body"),
        "first body must win"
    );
    assert!(
        !result
            .entity
            .sections
            .get("identity")
            .unwrap()
            .contains("## Identity"),
        "storage value must not embed a duplicate heading"
    );
    assert_eq!(result.parse_warnings.len(), 1);
    match &result.parse_warnings[0] {
        crate::ops::WarningHint::DuplicateSectionHeading {
            section_key,
            heading,
            occurrences,
            ..
        } => {
            assert_eq!(section_key, "identity");
            assert_eq!(heading, "Identity");
            assert_eq!(*occurrences, 2);
        }
        other => panic!("expected DuplicateSectionHeading, got {other:?}"),
    }
}

#[test]
fn duplicate_declared_heading_blank_then_populated_keeps_blank() {
    // First-wins is mechanical: a blank first occurrence wins over a
    // populated second one. The warning surfaces so the operator
    // notices content was discarded.
    let md = "---\ntype: spec\n---\n# Title\n\n## Identity\n\n## Identity\n\nleftover content\n";
    let result = parse_markdown(md, "x.md", &spec_schema(), "v").unwrap();
    assert_eq!(
        result.entity.sections.get("identity").map(String::as_str),
        Some(""),
        "first (blank) occurrence wins; second body is dropped"
    );
    assert_eq!(result.parse_warnings.len(), 1);
}

#[test]
fn duplicate_declared_heading_three_occurrences() {
    let md = "---\ntype: spec\n---\n# Title\n\n## Constraints\n\nA\n\n## Constraints\n\n## Constraints\n\nC\n";
    let result = parse_markdown(md, "x.md", &spec_schema(), "v").unwrap();
    assert_eq!(
        result
            .entity
            .sections
            .get("constraints")
            .map(String::as_str),
        Some("A"),
    );
    assert_eq!(result.parse_warnings.len(), 1);
    match &result.parse_warnings[0] {
        crate::ops::WarningHint::DuplicateSectionHeading { occurrences, .. } => {
            assert_eq!(*occurrences, 3);
        }
        _ => unreachable!(),
    }
}

#[test]
fn no_warning_when_each_declared_section_appears_once() {
    let md = "---\ntype: spec\n---\n# Title\n\n## Identity\n\nID\n\n## Purpose\n\nP\n\n## Constraints\n\nC\n";
    let result = parse_markdown(md, "x.md", &spec_schema(), "v").unwrap();
    assert!(result.parse_warnings.is_empty());
}

#[test]
fn no_warning_when_catch_all_section_repeats() {
    // `specifies` is the spec schema's catch-all section. Repetition
    // there is silent — duplicates only warn for non-catch-all keys.
    let md = "---\ntype: spec\n---\n# Title\n\n## Specifies\n\nfirst\n\n## Specifies\n\nsecond\n";
    let result = parse_markdown(md, "x.md", &spec_schema(), "v").unwrap();
    assert!(
        result.parse_warnings.is_empty(),
        "catch-all repetition must not warn"
    );
}

// Three `## Realization` headings on a spec entity. The default-schema
// `spec` does not declare `realization`, so it flows to the catch-all
// `specifies` bucket and emits no warning, but the storage must still
// not concatenate duplicate heading bytes — that was the bug being
// fixed. Workspaces that declare `realization` (e.g. `software@0.1.0`)
// additionally surface a `DuplicateSectionHeading` warning.
#[test]
fn duplicate_realization_does_not_concatenate_headers_in_storage() {
    let md = "---\ntype: spec\n---\n# Title\n\n## Identity\n\nID\n\n## Realization\n\n- a.mjs\n- b.mjs\n\n## Realization\n\n## Realization\n\n- c.mjs\n\n## Constraints\n\nC\n";
    let result = parse_markdown(md, "x.md", &spec_schema(), "v").unwrap();
    let catch_all = result.entity.sections.get("specifies").unwrap();
    let header_count = catch_all.matches("## Realization").count();
    assert!(
        header_count <= 1,
        "catch-all bucket must not contain multiple `## Realization` headers — got {header_count}: {catch_all:?}"
    );
}

// After a parse → render round-trip, an entity that was loaded from a
// markdown file with three `## Identity` headings emits exactly one
// `## Identity` heading on re-render. This is the self-heal contract:
// the next read-modify-write of a duplicate-heading entity collapses
// the markdown to one heading per declared section.
#[test]
fn parse_render_round_trip_collapses_duplicate_headings() {
    let md = "---\ntype: spec\n---\n# Title\n\n## Identity\n\nA\n\n## Identity\n\n## Identity\n\nC\n\n## Purpose\n\nP\n";
    let result = parse_markdown(md, "x.md", &spec_schema(), "v").unwrap();
    let rendered = crate::render::render_entity_markdown(&result.entity, None);
    let identity_count = rendered.matches("## Identity").count();
    assert_eq!(
        identity_count, 1,
        "rendered output must carry exactly one `## Identity`, got {identity_count}: {rendered}"
    );
    // First-wins: the rendered Identity body is `A`, not `C`.
    assert!(rendered.contains("\n## Identity\n\nA\n"));
    assert!(!rendered.contains("C\n"), "second body must not survive");
}