gcf 2.5.1

The AI-native wire format for structured data. 50-92% fewer tokens than JSON, with multi-turn delta encoding for agent loops. 100% comprehension on every frontier model. Zero dependencies (except serde).
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
#![allow(
    clippy::collapsible_if,
    clippy::collapsible_else_if,
    clippy::manual_strip,
    clippy::type_complexity
)]
//! GCF generic decoder: parses GCF generic or graph profile text into serde_json::Value.

use crate::decode::decode;
use crate::scalar::{
    find_closing_brace, parse_quoted_string, parse_scalar, split_field_decl,
    split_respecting_quotes, ScalarValue,
};
use serde_json::{Map, Number, Value};

/// Decode GCF text into a generic `serde_json::Value`.
pub fn decode_generic(input: &str) -> Result<Value, String> {
    let input = input.trim_end_matches(['\n', '\r']);
    if input.is_empty() {
        return Err("missing_header: empty input".into());
    }

    let lines: Vec<&str> = input.split('\n').collect();
    let header = lines[0].trim_end_matches('\r');
    if !header.starts_with("GCF ") {
        return Err("missing_header: first line does not begin with GCF".into());
    }

    let profile = parse_header_profile(header)?;

    if profile == "graph" {
        let p = decode(input).map_err(|e| e.to_string())?;
        return Ok(payload_to_value(&p));
    }

    if profile != "generic" {
        return Err(format!("unknown_profile: {}", profile));
    }

    // Filter body.
    let mut content_lines: Vec<String> = Vec::new();
    let mut deferred_count = 0;
    let mut summary_line = String::new();

    for line in &lines[1..] {
        let line = line.trim_end_matches('\r');
        if line.is_empty() {
            continue;
        }
        // Tab check.
        for c in line.chars() {
            if c == '\t' {
                return Err("tab_indentation: tabs in leading whitespace".into());
            }
            if c != ' ' {
                break;
            }
        }
        let trimmed = line.trim_start();
        if trimmed.starts_with("# ") {
            continue;
        }
        if trimmed.starts_with("##! ") {
            summary_line = trimmed.to_string();
            continue;
        }
        if trimmed.starts_with("## ") && (trimmed.contains("[?]") || trimmed.contains("[?:]")) {
            deferred_count += 1;
        }
        content_lines.push(line.to_string());
    }

    if !summary_line.is_empty() && deferred_count > 0 {
        validate_summary_counts(&summary_line, deferred_count, &content_lines)?;
    }

    if content_lines.is_empty() {
        return Ok(Value::Object(Map::new()));
    }

    let first = content_lines[0].trim_start();

    // Root scalar.
    if first.starts_with('=') {
        if content_lines.len() > 1 {
            return Err("trailing_characters: extra lines after root scalar".into());
        }
        return scalar_to_value(&parse_scalar(first.strip_prefix("=").unwrap(), false)?);
    }

    // Root array.
    if first.starts_with("## [") {
        let (arr, consumed) = parse_array_from_header(&content_lines, 0, 0, &first[3..])?;
        // A root array or keyed map spans the whole document, so any structural line
        // past the consumed rows is a surplus item, not sibling content. The row loop
        // stops at the declared count, so the count assert only catches the deficit
        // case; surplus is caught here (SPEC Section 13: a mismatch, fewer OR more
        // items than declared, is an error).
        if consumed < content_lines.len() {
            return Err(
                "count_mismatch: declared count is fewer than the rows present".to_string(),
            );
        }
        return Ok(arr);
    }

    // Root object.
    let mut result = Map::new();
    parse_object_body(&content_lines, 0, 0, &mut result)?;
    Ok(Value::Object(result))
}

fn parse_header_profile(header: &str) -> Result<String, String> {
    let parts: Vec<&str> = header.split_whitespace().collect();
    if parts.len() < 2 {
        return Err("missing_profile".into());
    }
    let mut seen = std::collections::HashSet::new();
    let mut profile = String::new();
    for p in &parts[1..] {
        let eq = p
            .find('=')
            .ok_or_else(|| format!("malformed_header_field: {}", p))?;
        let key = &p[..eq];
        if !seen.insert(key.to_string()) {
            return Err(format!("duplicate_header_field: {}", key));
        }
        if key == "profile" {
            profile = p[eq + 1..].to_string();
        }
    }
    if profile.is_empty() {
        return Err("missing_profile".into());
    }
    Ok(profile)
}

fn scalar_to_value(sv: &ScalarValue) -> Result<Value, String> {
    match sv {
        ScalarValue::Null => Ok(Value::Null),
        ScalarValue::Bool(b) => Ok(Value::Bool(*b)),
        ScalarValue::Int(i) => Ok(Value::Number(Number::from(*i))),
        ScalarValue::Float(f) => Ok(Value::Number(
            Number::from_f64(*f).unwrap_or_else(|| Number::from(0)),
        )),
        ScalarValue::Str(s) => Ok(Value::String(s.clone())),
        ScalarValue::Missing => Err("invalid_missing: ~ in non-tabular context".into()),
        ScalarValue::Attachment => {
            Err("invalid_attachment_marker: ^ in non-tabular context".into())
        }
    }
}

fn parse_object_body(
    lines: &[String],
    start: usize,
    depth: usize,
    out: &mut Map<String, Value>,
) -> Result<usize, String> {
    let ind = "  ".repeat(depth);
    let mut i = start;
    while i < lines.len() {
        let line = &lines[i];
        if depth > 0 && !line.starts_with(&ind) {
            break;
        }
        let content = if depth > 0 {
            &line[ind.len()..]
        } else {
            line.as_str()
        };
        if !content.is_empty() && content.starts_with(' ') {
            return Err("invalid_indent: indentation increases by more than one level".into());
        }

        // Array section.
        if let Some(hdr) = content.strip_prefix("## ") {
            if let Some(bi) = find_header_bracket_start(hdr) {
                let name = parse_key_from_header(&hdr[..bi])?;
                check_dup(out, &name)?;
                let (arr, consumed) = parse_array_from_header(lines, i, depth, &hdr[bi..])?;
                out.insert(name, arr);
                i += consumed;
                continue;
            }
            let name = parse_key_from_header(hdr)?;
            check_dup(out, &name)?;
            i += 1;
            let mut nested = Map::new();
            let consumed = parse_object_body(lines, i, depth + 1, &mut nested)?;
            out.insert(name, Value::Object(nested));
            i += consumed;
            continue;
        }

        // Key=value. Check before inline array so bracket patterns in quoted
        // values (e.g. text="ERR[404]: Not Found") are not misinterpreted.
        if let Some(eq_idx) = find_kv_split(content) {
            if eq_idx > 0 {
                let name = parse_key_from_header(&content[..eq_idx])?;
                check_dup(out, &name)?;
                let val = scalar_to_value(&parse_scalar(&content[eq_idx + 1..], false)?)?;
                out.insert(name, val);
                i += 1;
                continue;
            }
        }

        // Inline array (e.g. items[3]: a,b,c). Only reached if no = found.
        if !content.starts_with('@') && !content.starts_with("##") {
            if let Some(bracket_idx) = content.find('[') {
                if bracket_idx > 0 {
                    let rest = &content[bracket_idx..];
                    if let Some(close_idx) = rest.find(']') {
                        let after = &rest[close_idx + 1..];
                        if after.starts_with(": ") || after == ":" {
                            let name = parse_key_from_header(&content[..bracket_idx])?;
                            check_dup(out, &name)?;
                            let (arr, _) = parse_array_from_header(lines, i, depth, rest)?;
                            out.insert(name, arr);
                            i += 1;
                            continue;
                        }
                    }
                }
            }
        }

        // An object-body line that is not a `## ` section, a `key=value` field, or
        // an inline array is not valid content and MUST NOT be silently skipped
        // (that dropped data: a lossless round-trip hole). A pipe-delimited line is
        // a stray positional inline body with no eligible `^` cell (Section 16.5,
        // orphan_inline_attachment); any other unrecognized line is likewise rejected.
        if content.contains('|') {
            return Err(format!("orphan_inline_attachment: {}", content));
        }
        return Err(format!(
            "invalid_line: unexpected content in object body: {:?}",
            content
        ));
    }
    Ok(i - start)
}

fn find_kv_split(s: &str) -> Option<usize> {
    if s.is_empty() {
        return None;
    }
    let bytes = s.as_bytes();
    if bytes[0] == b'"' {
        let mut i = 1;
        while i < bytes.len() {
            if bytes[i] == b'\\' {
                i += 2;
                continue;
            }
            if bytes[i] == b'"' {
                return if i + 1 < bytes.len() && bytes[i + 1] == b'=' {
                    Some(i + 1)
                } else {
                    None
                };
            }
            i += 1;
        }
        return None;
    }
    let eq_idx = s.find('=')?;
    if let Some(bracket_idx) = s.find('[') {
        if bracket_idx < eq_idx {
            return None;
        }
    }
    Some(eq_idx)
}

/// Find the byte index of the named-array count bracket (" [") that is OUTSIDE
/// any quoted name, so a quoted section/key name containing " [" (e.g.
/// `## "a [1] b"`) is not misread as a named-array header. Mirrors the
/// quote-aware scan in find_closing_brace; char_indices keeps the returned
/// index byte-safe for multibyte names.
fn find_header_bracket_start(s: &str) -> Option<usize> {
    let bytes = s.as_bytes();
    let mut in_quote = false;
    let mut escaped = false;
    for (i, c) in s.char_indices() {
        if escaped {
            escaped = false;
            continue;
        }
        if c == '\\' && in_quote {
            escaped = true;
            continue;
        }
        if c == '"' {
            in_quote = !in_quote;
            continue;
        }
        if !in_quote && c == ' ' && i + 1 < bytes.len() && bytes[i + 1] == b'[' {
            return Some(i);
        }
    }
    None
}

fn parse_key_from_header(s: &str) -> Result<String, String> {
    let s = s.trim();
    if s.len() >= 2 && s.starts_with('"') {
        parse_quoted_string(s)
    } else {
        Ok(s.to_string())
    }
}

fn check_dup(map: &Map<String, Value>, key: &str) -> Result<(), String> {
    if map.contains_key(key) {
        Err(format!("duplicate_key: {}", key))
    } else {
        Ok(())
    }
}

fn parse_array_from_header(
    lines: &[String],
    header_line: usize,
    depth: usize,
    bracket_part: &str,
) -> Result<(Value, usize), String> {
    let bp = bracket_part.trim_start();
    if !bp.starts_with('[') {
        return Err("invalid_count".into());
    }
    let close = bp.find(']').ok_or("invalid_count")?;
    let mut count_str = &bp[1..close];
    let after = &bp[close + 1..];

    // A keyed map (SPEC 7.2a) is marked by a `:` after the exact member count
    // (`[N:]`). The colon lives inside the brackets, so it is part of count_str.
    let keyed = count_str.ends_with(':');
    if keyed {
        count_str = &count_str[..count_str.len() - 1];
        if !after.starts_with('{') {
            return Err("keyed_map: missing field declaration".into());
        }
    }

    let count: i64 = if count_str == "?" {
        -1
    } else {
        parse_count(count_str)? as i64
    };

    // A keyed map has at least one member; an empty object is encoded per
    // Section 7.7, never as [0:] (SPEC 7.2a.4).
    if keyed && count == 0 {
        return Err(
            "keyed_map: zero count [0:] is invalid (an empty object uses Section 7.7)".into(),
        );
    }

    if count == 0 && !after.starts_with('{') && !after.starts_with(':') {
        return Ok((Value::Array(vec![]), 1));
    }

    // Inline.
    if after.starts_with(": ") || after == ":" {
        let vals_str = if after.starts_with(": ") {
            after.strip_prefix(": ").unwrap()
        } else {
            ""
        };
        if vals_str.is_empty() {
            if count > 0 {
                return Err(format!("count_mismatch: declared {}, got 0", count));
            }
            return Ok((Value::Array(vec![]), 1));
        }
        let vals = split_respecting_quotes(vals_str, ',');
        if count >= 0 && vals.len() as i64 != count {
            return Err(format!(
                "count_mismatch: declared {}, got {}",
                count,
                vals.len()
            ));
        }
        let parsed: Result<Vec<Value>, String> = vals
            .iter()
            .map(|v| scalar_to_value(&parse_scalar(v.trim(), false)?))
            .collect();
        return Ok((Value::Array(parsed?), 1));
    }

    // Tabular.
    if after.starts_with('{') {
        let brace_end = find_closing_brace(after).ok_or("invalid field declaration")?;
        let fields = split_field_decl(&after[..brace_end + 1])?;
        let (rows, consumed) = parse_tabular_body(lines, header_line + 1, depth, &fields, count)?;
        if count >= 0 && rows.len() as i64 != count {
            return Err(format!(
                "count_mismatch: declared {}, got {}",
                count,
                rows.len()
            ));
        }
        if keyed {
            let map = keyed_rows_to_map(rows, &fields)?;
            return Ok((Value::Object(map), consumed + 1));
        }
        return Ok((Value::Array(rows), consumed + 1));
    }

    // Expanded.
    let (items, consumed) = parse_expanded_body(lines, header_line + 1, depth)?;
    if count >= 0 && items.len() as i64 != count {
        return Err(format!(
            "count_mismatch: declared {}, got {}",
            count,
            items.len()
        ));
    }
    Ok((Value::Array(items), consumed + 1))
}

fn parse_tabular_body(
    lines: &[String],
    start: usize,
    depth: usize,
    fields: &[String],
    expected_count: i64,
) -> Result<(Vec<Value>, usize), String> {
    parse_tabular_body_with_shared(lines, start, depth, fields, expected_count, None)
}

/// Reconstruct the map from decoded keyed-table rows (SPEC 7.2a.4): the first
/// declared field is the member key column, discarded from the value object; the
/// remaining fields form the value object. Rejects a header with fewer than two
/// fields and duplicate member keys.
fn keyed_rows_to_map(rows: Vec<Value>, fields: &[String]) -> Result<Map<String, Value>, String> {
    if fields.len() < 2 {
        return Err("keyed_map: header must declare at least two fields".into());
    }
    let key_label = &fields[0];
    let mut out = Map::new();
    for r in rows {
        let mut row = match r {
            Value::Object(m) => m,
            _ => return Err("keyed_map: row is not an object".into()),
        };
        // Cell 0 always populates the key column (a JSON key is a string, and the
        // Section 2.4 quoting obligation forces cell 0 to round-trip as a string),
        // so a missing key label indicates a malformed row. shift_remove preserves
        // the order of the remaining value fields; the default remove is a
        // swap_remove that would move the last field into the key's slot.
        let key_val = row
            .shift_remove(key_label)
            .ok_or_else(|| format!("keyed_map: row missing key column {}", key_label))?;
        let ks = match key_val {
            Value::String(s) => s,
            other => other.to_string(),
        };
        if out.contains_key(&ks) {
            return Err(format!("keyed_map: duplicate member key {}", ks));
        }
        out.insert(ks, Value::Object(row));
    }
    Ok(out)
}

/// Rebuild a decoded row so its keys follow the declared header field order.
/// Each declared field maps to a top-level output key: a plain field maps to
/// itself, a path column ("parent>child") maps to its first segment. Duplicate
/// top-level keys (sibling path columns sharing a parent) are emitted once at
/// the parent's first occurrence. Any keys present in the row but not implied by
/// the declared fields are appended in their current order.
fn reorder_row_by_fields(row: Map<String, Value>, fields: &[String]) -> Map<String, Value> {
    let mut ordered_keys: Vec<String> = Vec::new();
    let mut seen: std::collections::HashSet<String> = std::collections::HashSet::new();
    for f in fields {
        let top = match f.split_once('>') {
            Some((parent, _)) if !parent.is_empty() => parent.to_string(),
            _ => f.clone(),
        };
        if seen.insert(top.clone()) {
            ordered_keys.push(top);
        }
    }

    let mut out = Map::new();
    for k in &ordered_keys {
        if let Some(v) = row.get(k) {
            out.insert(k.clone(), v.clone());
        }
    }
    // Preserve any keys the declared fields did not account for, in row order.
    for (k, v) in &row {
        if !out.contains_key(k) {
            out.insert(k.clone(), v.clone());
        }
    }
    out
}

/// Unflatten path columns into nested objects.
fn unflatten_paths(
    fields: &[String],
    path_columns: &std::collections::HashMap<String, Vec<String>>,
    flat_values: &std::collections::HashMap<String, Value>,
    flat_absent: &std::collections::HashSet<String>,
) -> Map<String, Value> {
    // Group by top-level parent, iterating the declared header field order so both
    // the group order and the leaf order within each nested object follow the header,
    // not HashMap iteration order (SPEC 7.4.6.1 step 7; key ordering is a preserved
    // round-trip property, SPEC 52 and 931).
    let mut groups: std::collections::HashMap<String, Vec<String>> =
        std::collections::HashMap::new();
    let mut group_order: Vec<String> = Vec::new();
    for field_name in fields {
        let paths = match path_columns.get(field_name) {
            Some(p) if !p.is_empty() => p,
            _ => continue,
        };
        let top = &paths[0];
        if !groups.contains_key(top) {
            group_order.push(top.clone());
            groups.insert(top.clone(), Vec::new());
        }
        groups.get_mut(top).unwrap().push(field_name.clone());
    }

    let mut result = Map::new();

    for top in &group_order {
        let field_names = &groups[top];
        let all_absent = field_names.iter().all(|f| flat_absent.contains(f));
        let all_null = field_names.iter().all(|f| {
            if flat_absent.contains(f) {
                return false; // absent is not null
            }
            matches!(flat_values.get(f), Some(Value::Null) | None)
        });

        if all_absent {
            continue; // omit parent key
        }
        if all_null {
            result.insert(top.clone(), Value::Null);
            continue;
        }

        // Build nested structure.
        for field_name in field_names {
            if flat_absent.contains(field_name) {
                continue;
            }
            let paths = &path_columns[field_name];
            let val = flat_values.get(field_name).cloned().unwrap_or(Value::Null);

            let mut current = &mut result;
            for k in &paths[..paths.len() - 1] {
                if !current.contains_key(k) {
                    current.insert(k.clone(), Value::Object(Map::new()));
                }
                current = current.get_mut(k).unwrap().as_object_mut().unwrap();
            }
            current.insert(paths.last().unwrap().clone(), val);
        }
    }

    result
}

/// v3 tabular body parser with inline schemas, no-indent attachments, and shared array schemas.
fn parse_tabular_body_with_shared(
    lines: &[String],
    start: usize,
    depth: usize,
    fields: &[String],
    expected_count: i64,
    parent_shared_schemas: Option<&std::collections::HashMap<String, Vec<String>>>,
) -> Result<(Vec<Value>, usize), String> {
    let ind = "  ".repeat(depth);
    let mut rows: Vec<Value> = Vec::new();
    let mut i = start;

    // Detect path columns: fields containing ">".
    let mut path_column_map: std::collections::HashMap<String, Vec<String>> =
        std::collections::HashMap::new();
    for f in fields {
        if f.contains('>') {
            let parts: Vec<String> = f.split('>').map(|s| s.to_string()).collect();
            // Only treat as a path column if all segments are non-empty.
            // A literal key like ">" would split into ["", ""].
            if parts.iter().all(|p| !p.is_empty()) {
                path_column_map.insert(f.clone(), parts);
            }
        }
    }

    // Track inline schemas declared by ^{fields}.
    let mut inline_schemas: std::collections::HashMap<String, Vec<String>> =
        std::collections::HashMap::new();
    // Track shared array schemas: field -> fields list (from first row's attachment).
    let mut shared_array_schemas: std::collections::HashMap<String, Vec<String>> =
        std::collections::HashMap::new();
    if let Some(parent) = parent_shared_schemas {
        for (k, v) in parent {
            shared_array_schemas.insert(k.clone(), v.clone());
        }
    }

    while i < lines.len() {
        let line = &lines[i];
        let content = if depth > 0 {
            if !line.starts_with(&ind) {
                break;
            }
            &line[ind.len()..]
        } else {
            line.as_str()
        };
        if content.starts_with("## ") || content.starts_with("##!") {
            break;
        }
        if !content.is_empty() && content.as_bytes()[0] == b' ' {
            let trimmed = content.trim_start();
            if trimmed.starts_with('.') {
                break; // attachment lines handled below
            }
            break;
        }

        // Strip @N prefix (must be @digits).
        let mut row_data = content;
        let mut row_has_id = false;
        if row_data.starts_with('@') {
            if let Some(sp) = row_data.find(' ') {
                let id_str = &row_data[1..sp];
                let valid_id = !id_str.is_empty() && id_str.bytes().all(|b| b.is_ascii_digit());
                if valid_id {
                    row_data = &row_data[sp + 1..];
                    row_has_id = true;
                }
            }
        }

        let vals = split_respecting_quotes(row_data, '|');
        if vals.len() != fields.len() {
            return Err(format!(
                "row_width_mismatch: expected {} fields, got {}",
                fields.len(),
                vals.len()
            ));
        }

        let mut row: Map<String, Value> = Map::new();
        let mut traditional_att_fields: Vec<String> = Vec::new();
        let mut inline_att_fields: Vec<String> = Vec::new();
        let mut inline_att_order: Vec<String> = Vec::new();

        // Collect path column values for unflattening.
        let mut flat_values: std::collections::HashMap<String, Value> =
            std::collections::HashMap::new();
        let mut flat_absent: std::collections::HashSet<String> = std::collections::HashSet::new();

        for (j, f) in fields.iter().enumerate() {
            let cell_val = &vals[j];

            // Path columns: store values for later unflattening.
            if path_column_map.contains_key(f) {
                let parsed = parse_scalar(cell_val, true)?;
                match parsed {
                    ScalarValue::Missing => {
                        flat_absent.insert(f.clone());
                    }
                    _ => {
                        flat_values.insert(f.clone(), scalar_to_value(&parsed)?);
                    }
                }
                continue;
            }

            // Check for ^{fields} inline schema declaration.
            if cell_val.starts_with("^{") && cell_val.ends_with('}') {
                let schema_str = &cell_val[1..]; // "{field1,field2,...}"
                let ifs = split_field_decl(schema_str)?;
                inline_schemas.insert(f.clone(), ifs);
                inline_att_fields.push(f.clone());
                inline_att_order.push(f.clone());
                continue;
            }

            let parsed = parse_scalar(cell_val, true)?;
            match parsed {
                ScalarValue::Missing => {
                    // absent: skip
                }
                ScalarValue::Attachment => {
                    // Check if this field has a stored inline schema.
                    if inline_schemas.contains_key(f) {
                        inline_att_fields.push(f.clone());
                        inline_att_order.push(f.clone());
                    } else {
                        traditional_att_fields.push(f.clone());
                    }
                }
                _ => {
                    row.insert(f.clone(), scalar_to_value(&parsed)?);
                }
            }
        }

        i += 1;

        // Build ordered list of expected attachment fields from cell order (preserving field order).
        let mut all_att_fields: Vec<String> = Vec::new();
        for f in fields {
            let is_trad = traditional_att_fields.iter().any(|tf| tf == f);
            let is_inline = inline_att_fields.iter().any(|inf| inf == f);
            if is_trad || is_inline {
                all_att_fields.push(f.clone());
            }
        }

        if row_has_id {
            let mut resolved_attachments: std::collections::HashSet<String> =
                std::collections::HashSet::new();
            let mut inline_idx: usize = 0;

            // Columns that carry a `^` marker cell in this row legitimately expect
            // a `.field` body. Any other `.field` is an orphan (Section 16.5) unless
            // its name contains `>` (the flatten-fallback attachment, Section 7.4.6.1.4).
            let mut expected_att: std::collections::HashSet<String> =
                std::collections::HashSet::new();
            for f in &traditional_att_fields {
                expected_att.insert(f.clone());
            }
            for f in &inline_att_fields {
                expected_att.insert(f.clone());
            }

            while i < lines.len() {
                let a_line = &lines[i];
                let a_content = if a_line.starts_with(&format!("{}  ", ind)) {
                    &a_line[ind.len() + 2..]
                } else if a_line.starts_with(&ind) {
                    &a_line[ind.len()..]
                } else {
                    break;
                };

                // Line starts with ".": traditional or prefixed inline attachment.
                if a_content.starts_with('.') {
                    let rest = &a_content[1..];
                    let (att_name, after_name_raw) = parse_attachment_name(rest);
                    let after_name = after_name_raw.trim_start();

                    // Orphan attachment: a `.field` with no matching `^` cell in this
                    // row is only legitimate for a `>`-named field (Section 7.4.6.1.4).
                    // Any other unmatched attachment is rejected rather than silently
                    // injected as an undeclared extra field, which would decode to a
                    // record no encoder produces (Section 16.5, lossless round-trip).
                    if !expected_att.contains(&att_name) && !att_name.contains('>') {
                        return Err(format!("orphan_attachment: {}", att_name));
                    }

                    // Check duplicate.
                    if resolved_attachments.contains(&att_name) {
                        return Err(format!("duplicate_attachment: {}", att_name));
                    }

                    // Check if this field has inline schema and data is pipe-delimited (not {} or [).
                    if let Some(ifs) = inline_schemas.get(&att_name) {
                        if !after_name.starts_with("{}") && !after_name.starts_with('[') {
                            // Prefixed inline data: .fieldname val1|val2|...
                            let inline_vals = split_respecting_quotes(after_name, '|');
                            if inline_vals.len() != ifs.len() {
                                return Err(format!(
                                    "inline_width_mismatch: {} expected {}, got {}",
                                    att_name,
                                    ifs.len(),
                                    inline_vals.len()
                                ));
                            }
                            let mut obj = Map::new();
                            for (k, inf) in ifs.iter().enumerate() {
                                let p = parse_scalar(&inline_vals[k], true)?;
                                match p {
                                    ScalarValue::Missing => {}
                                    _ => {
                                        obj.insert(inf.clone(), scalar_to_value(&p)?);
                                    }
                                }
                            }
                            row.insert(att_name.clone(), Value::Object(obj));
                            resolved_attachments.insert(att_name);
                            i += 1;
                            continue;
                        }
                    }

                    // Traditional attachment: .fieldname {} or .fieldname [N]...
                    let (att_name_t, att_val, consumed, parsed_fields) =
                        parse_attachment_v3(lines, i, rest, depth + 2, &shared_array_schemas)?;
                    // Store authoritative field order from the header for shared schema.
                    if rows.is_empty() {
                        if let Some(pf) = parsed_fields {
                            shared_array_schemas.insert(att_name_t.clone(), pf);
                        }
                    }
                    resolved_attachments.insert(att_name_t.clone());
                    row.insert(att_name_t, att_val);
                    i += consumed;
                    continue;
                }

                // No-prefix line: must be positional inline data.
                let mut found_inline = false;
                let mut next_inline_field = String::new();
                while inline_idx < inline_att_order.len() {
                    let candidate = &inline_att_order[inline_idx];
                    if !resolved_attachments.contains(candidate) {
                        next_inline_field = candidate.clone();
                        found_inline = true;
                        break;
                    }
                    inline_idx += 1;
                }
                if !found_inline {
                    break; // no more inline fields expected
                }

                let ifs = inline_schemas
                    .get(&next_inline_field)
                    .ok_or_else(|| {
                        format!("missing inline schema for field: {}", next_inline_field)
                    })?
                    .clone();
                let inline_vals = split_respecting_quotes(a_content, '|');
                if inline_vals.len() != ifs.len() {
                    return Err(format!(
                        "inline_width_mismatch: {} expected {}, got {}",
                        next_inline_field,
                        ifs.len(),
                        inline_vals.len()
                    ));
                }
                let mut obj = Map::new();
                for (k, inf) in ifs.iter().enumerate() {
                    let p = parse_scalar(&inline_vals[k], true)?;
                    match p {
                        ScalarValue::Missing => {}
                        _ => {
                            obj.insert(inf.clone(), scalar_to_value(&p)?);
                        }
                    }
                }
                resolved_attachments.insert(next_inline_field.clone());
                row.insert(next_inline_field, Value::Object(obj));
                inline_idx += 1;
                i += 1;
            }

            // Verify all attachment fields resolved.
            for f in &all_att_fields {
                if !resolved_attachments.contains(f) {
                    return Err(format!("missing_attachment: {}", f));
                }
            }

            // Check for extra attachment lines after all fields resolved (duplicate).
            if i < lines.len() {
                let extra_line = &lines[i];
                let extra_content = if extra_line.starts_with(&format!("{}  ", ind)) {
                    &extra_line[ind.len() + 2..]
                } else if extra_line.starts_with(&ind) {
                    &extra_line[ind.len()..]
                } else {
                    ""
                };
                if extra_content.starts_with('.') {
                    let (extra_name, _) = parse_attachment_name(&extra_content[1..]);
                    if resolved_attachments.contains(&extra_name) {
                        return Err(format!("duplicate_attachment: {}", extra_name));
                    }
                }
            }
        }

        // Unflatten path columns into nested objects.
        if !path_column_map.is_empty() {
            let nested = unflatten_paths(fields, &path_column_map, &flat_values, &flat_absent);
            for (k, v) in nested {
                row.insert(k, v);
            }
        }

        // Reorder the row's keys to match the declared field order. Scalar cells are
        // inserted in field order during the first pass, but attachment cells and
        // path (flatten) columns are filled in later passes and would otherwise land
        // after all scalars, breaking the input key order the encoder preserved. The
        // declared top-level key for a plain field is the field name; for a path
        // column ("parent>child") it is the first path segment, deduplicated so
        // sibling path columns collapse to one parent key at its first occurrence.
        row = reorder_row_by_fields(row, fields);

        rows.push(Value::Object(row));

        if expected_count >= 0 && rows.len() as i64 >= expected_count {
            break;
        }
    }
    Ok((rows, i - start))
}

/// Parse attachment name from the rest of an attachment line (after the leading dot).
/// Returns (name, remainder_after_name).
fn parse_attachment_name(rest: &str) -> (String, &str) {
    if rest.starts_with('"') {
        let bytes = rest.as_bytes();
        let mut j = 1;
        while j < bytes.len() {
            if bytes[j] == b'\\' {
                j += 2;
                continue;
            }
            if bytes[j] == b'"' {
                if let Ok(parsed) = parse_quoted_string(&rest[..j + 1]) {
                    return (parsed, &rest[j + 1..]);
                }
                return (String::new(), rest);
            }
            j += 1;
        }
        (String::new(), rest)
    } else {
        if let Some(sp) = rest.find(' ') {
            (rest[..sp].to_string(), &rest[sp..])
        } else {
            (rest.to_string(), "")
        }
    }
}

/// v3 parse_attachment that returns parsed field names for shared schema support.
/// Returns (name, value, lines_consumed, parsed_fields).
fn parse_attachment_v3(
    lines: &[String],
    line_idx: usize,
    rest: &str,
    depth: usize,
    shared_schemas: &std::collections::HashMap<String, Vec<String>>,
) -> Result<(String, Value, usize, Option<Vec<String>>), String> {
    let (name, after_name_raw) = parse_attachment_name(rest);
    if name.is_empty() && !rest.starts_with("\"\"") {
        return Err("invalid attachment".into());
    }
    let after_name = after_name_raw.trim_start();

    // Object: {}
    if after_name.starts_with("{}") {
        let mut nested = Map::new();
        let consumed = parse_object_body(lines, line_idx + 1, depth, &mut nested)?;
        return Ok((name, Value::Object(nested), consumed + 1, None));
    }

    // Array: [N]{fields} or [N]: or [N]
    if after_name.starts_with('[') {
        let close_bracket = after_name
            .find(']')
            .ok_or_else(|| "invalid_count: missing ]".to_string())?;
        let after_close = &after_name[close_bracket + 1..];

        // [N]{fields} - has its own schema.
        if after_close.starts_with('{') {
            let end_brace = find_closing_brace(after_close);
            let mut parsed_fields: Option<Vec<String>> = None;
            if let Some(eb) = end_brace {
                if let Ok(pf) = split_field_decl(&after_close[..eb + 1]) {
                    parsed_fields = Some(pf);
                }
            }
            let (arr, consumed) = parse_array_from_header(lines, line_idx, depth, after_name)?;
            return Ok((name, arr, consumed, parsed_fields));
        }

        // [N]: values (inline primitive array): don't use shared schema.
        if after_close.starts_with(": ") || after_close == ":" {
            let (arr, consumed) = parse_array_from_header(lines, line_idx, depth, after_name)?;
            return Ok((name, arr, consumed, None));
        }

        // [N] without {fields}: check for shared schema.
        // Only use shared schema if the next line looks tabular (not @N expanded).
        if let Some(sf) = shared_schemas.get(&name) {
            let count_str = &after_name[1..close_bracket];
            let count: i64 = if count_str == "?" {
                -1
            } else {
                parse_count(count_str)? as i64
            };
            if count == 0 {
                return Ok((name, Value::Array(vec![]), 1, None));
            }
            // Peek at next line: if it starts with @ it's expanded, not tabular.
            let mut use_shared = true;
            let next_idx = line_idx + 1;
            let indent_str = "  ".repeat(depth);
            if next_idx < lines.len() {
                let next_line = &lines[next_idx];
                let next_content = if depth > 0 && next_line.starts_with(&indent_str) {
                    &next_line[indent_str.len()..]
                } else {
                    next_line.as_str()
                };
                if next_content.trim_start().starts_with('@') {
                    use_shared = false;
                }
            }
            if use_shared {
                let (tab_rows, consumed) = parse_tabular_body_with_shared(
                    lines,
                    line_idx + 1,
                    depth,
                    sf,
                    count,
                    Some(shared_schemas),
                )?;
                if count >= 0 && tab_rows.len() as i64 != count {
                    return Err(format!(
                        "count_mismatch: declared {}, got {}",
                        count,
                        tab_rows.len()
                    ));
                }
                return Ok((name, Value::Array(tab_rows), consumed + 1, None));
            }
        }

        // No shared schema: standard expanded array.
        let (arr, consumed) = parse_array_from_header(lines, line_idx, depth, after_name)?;
        return Ok((name, arr, consumed, None));
    }

    // Scalar: =value (field names containing ">" excluded from tabular columns).
    if after_name.starts_with('=') {
        let val_str = &after_name[1..];
        let parsed = parse_scalar(val_str, true)?;
        match parsed {
            ScalarValue::Missing => Ok((name, Value::Null, 1, None)),
            _ => Ok((name, scalar_to_value(&parsed)?, 1, None)),
        }
    } else {
        Err(format!("invalid attachment form: {}", after_name))
    }
}

fn parse_expanded_body(
    lines: &[String],
    start: usize,
    depth: usize,
) -> Result<(Vec<Value>, usize), String> {
    let ind = "  ".repeat(depth);
    let mut items: Vec<Value> = Vec::new();
    let mut i = start;

    while i < lines.len() {
        let line = &lines[i];
        let content = if depth > 0 {
            if !line.starts_with(&ind) {
                break;
            }
            &line[ind.len()..]
        } else {
            line.as_str()
        };
        if content.starts_with("## ") || content.starts_with("##!") {
            break;
        }
        if !content.starts_with('@') {
            break;
        }

        let sp = match content.find(' ') {
            Some(s) => s,
            None => break,
        };

        // Validate item ID.
        let id_str = &content[1..sp];
        if let Ok(id) = id_str.parse::<usize>() {
            if id != items.len() {
                return Err(format!(
                    "invalid_item_id: expected @{}, got @{}",
                    items.len(),
                    id_str
                ));
            }
        }

        let marker = &content[sp + 1..];

        if marker.starts_with('=') {
            let val = scalar_to_value(&parse_scalar(marker.strip_prefix("=").unwrap(), false)?)?;
            items.push(val);
            i += 1;
            continue;
        }
        if marker.starts_with("{}") {
            let mut nested = Map::new();
            i += 1;
            let consumed = parse_object_body(lines, i, depth + 1, &mut nested)?;
            items.push(Value::Object(nested));
            i += consumed;
            continue;
        }
        if marker.starts_with('[') {
            let (arr, consumed) = parse_array_from_header(lines, i, depth + 1, marker)?;
            items.push(arr);
            i += consumed;
            continue;
        }
        break;
    }
    Ok((items, i - start))
}

fn parse_count(s: &str) -> Result<usize, String> {
    if s == "0" {
        return Ok(0);
    }
    if s.is_empty() || s.starts_with('0') {
        return Err(format!("invalid_count: {}", s));
    }
    s.parse::<usize>()
        .map_err(|_| format!("invalid_count: {}", s))
}

fn payload_to_value(p: &crate::types::Payload) -> Value {
    let syms: Vec<Value> = p
        .symbols
        .iter()
        .map(|s| {
            serde_json::json!({
                "qualifiedName": s.qualified_name,
                "kind": s.kind,
                "score": s.score,
                "provenance": s.provenance,
                "distance": s.distance,
            })
        })
        .collect();
    let edges: Vec<Value> = p
        .edges
        .iter()
        .map(|e| {
            serde_json::json!({
                "source": e.source,
                "target": e.target,
                "edgeType": e.edge_type,
                "status": e.status,
            })
        })
        .collect();
    serde_json::json!({
        "tool": p.tool,
        "tokenBudget": p.token_budget,
        "tokensUsed": p.tokens_used,
        "packRoot": p.pack_root,
        "symbols": syms,
        "edges": edges,
    })
}

fn validate_summary_counts(
    summary_line: &str,
    deferred_count: usize,
    content_lines: &[String],
) -> Result<(), String> {
    let counts_str = summary_line
        .split_whitespace()
        .find(|p| p.starts_with("counts="))
        .map(|p| &p[7..])
        .unwrap_or("");
    if counts_str.is_empty() {
        return Ok(());
    }
    let count_vals: Vec<&str> = counts_str.split(',').collect();
    if count_vals.len() != deferred_count {
        return Err(format!(
            "count_mismatch: summary has {} count entries but {} deferred sections",
            count_vals.len(),
            deferred_count
        ));
    }
    let mut actual_counts: Vec<usize> = Vec::new();
    let mut in_deferred = false;
    let mut current_count = 0;
    for line in content_lines {
        let trimmed = line.trim_start();
        if trimmed.starts_with("## ") && (trimmed.contains("[?]") || trimmed.contains("[?:]")) {
            if in_deferred {
                actual_counts.push(current_count);
            }
            in_deferred = true;
            current_count = 0;
            continue;
        }
        if trimmed.starts_with("## ") {
            if in_deferred {
                actual_counts.push(current_count);
                in_deferred = false;
            }
            continue;
        }
        if in_deferred && !trimmed.starts_with(' ') && !trimmed.starts_with('.') {
            current_count += 1;
        }
    }
    if in_deferred {
        actual_counts.push(current_count);
    }
    for (idx, cv) in count_vals.iter().enumerate() {
        let declared: usize = cv
            .parse()
            .map_err(|_| format!("count_mismatch: invalid count value '{}'", cv))?;
        if idx < actual_counts.len() && declared != actual_counts[idx] {
            return Err(format!(
                "count_mismatch: section {} declared {} in summary, actual {}",
                idx, declared, actual_counts[idx]
            ));
        }
    }
    Ok(())
}