locus-core-rs 0.2.0

Core STTP parsing, validation, storage contracts, and application services for Rust
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
use chrono::{DateTime, Utc};
use once_cell::sync::Lazy;
use regex::Regex;

use crate::domain::models::{
    AvecState, CanonicalAst, CanonicalAstLayer, ParseDiagnostic, ParseDiagnosticSeverity,
    ParseProfile, ParseResult, ParseSpan, SttpNode,
};
use crate::parsing::lexicon::{AVEC_COMPRESSION_KEY, AVEC_MODEL_KEY, AVEC_USER_KEY};
use crate::parsing::state_machine::{ParserState, SttpLayerStateMachine};

static TIMESTAMP_RX: Lazy<Regex> = Lazy::new(|| {
    Regex::new(r#"timestamp:\s*"(?P<v>[^"]+)""#).expect("timestamp regex must compile")
});

static TIER_RX: Lazy<Regex> = Lazy::new(|| {
    Regex::new(r"(?i)tier:\s*(?P<v>raw|daily|weekly|monthly|quarterly|yearly)")
        .expect("tier regex must compile")
});

static COMPRESSION_DEPTH_RX: Lazy<Regex> = Lazy::new(|| {
    Regex::new(r"compression_depth:\s*(?P<v>\d+)").expect("compression_depth regex must compile")
});

static PARENT_NODE_RX: Lazy<Regex> = Lazy::new(|| {
    Regex::new(r#"parent_node:\s*(?:ref:(?P<ref>[^,\s}\]]+)|"(?P<quoted>[^"]+)"|(?P<null>null))"#)
        .expect("parent regex must compile")
});

static CONTEXT_SUMMARY_RX: Lazy<Regex> = Lazy::new(|| {
    Regex::new(r#"context_summary:\s*(?:"(?P<quoted>[^"]*)"|(?P<bare>[^,}\n]+))"#)
        .expect("context_summary regex must compile")
});

static AVEC_ENTRY_RX: Lazy<Regex> = Lazy::new(|| {
    Regex::new(r"(?i)\b(?P<k>stability|friction|logic|autonomy|psi)\b\s*:\s*(?P<v>[-+]?\d*\.?\d+)")
        .expect("avec entry regex must compile")
});

static RHO_RX: Lazy<Regex> =
    Lazy::new(|| Regex::new(r"rho:\s*(?P<v>[-+]?\d*\.?\d+)").expect("rho regex must compile"));
static KAPPA_RX: Lazy<Regex> =
    Lazy::new(|| Regex::new(r"kappa:\s*(?P<v>[-+]?\d*\.?\d+)").expect("kappa regex must compile"));
static PSI_RX: Lazy<Regex> =
    Lazy::new(|| Regex::new(r"psi:\s*(?P<v>[-+]?\d*\.?\d+)").expect("psi regex must compile"));
static CONTENT_KEY_RX: Lazy<Regex> = Lazy::new(|| {
    Regex::new(r"^(?P<name>[A-Za-z_][A-Za-z0-9_]*)\(\s*(?P<c>[-+]?\d*\.?\d+)\s*\)$")
        .expect("content key regex must compile")
});

#[derive(Debug, Default, Clone, Copy)]
pub struct SttpNodeParser;

impl SttpNodeParser {
    pub fn new() -> Self {
        Self
    }

    pub fn try_parse(&self, raw: &str, session_id: &str) -> ParseResult {
        self.try_parse_with_profile(raw, session_id, ParseProfile::Tolerant)
    }

    pub fn try_parse_strict(&self, raw: &str, session_id: &str) -> ParseResult {
        self.try_parse_with_profile(raw, session_id, ParseProfile::Strict)
    }

    pub fn try_parse_tolerant(&self, raw: &str, session_id: &str) -> ParseResult {
        self.try_parse_with_profile(raw, session_id, ParseProfile::Tolerant)
    }

    pub fn try_parse_with_profile(
        &self,
        raw: &str,
        session_id: &str,
        profile: ParseProfile,
    ) -> ParseResult {
        let layered = SttpLayerStateMachine::parse(raw);
        let provenance = layered.provenance.unwrap_or(raw);
        let envelope = layered.envelope.unwrap_or(raw);
        let content = layered.content.unwrap_or(raw);
        let metrics = layered.metrics.unwrap_or(raw);
        let mut strict_valid = layered.strict_spine
            && layered.provenance.is_some()
            && layered.envelope.is_some()
            && layered.content.is_some()
            && layered.metrics.is_some();

        let mut diagnostics = to_structured_diagnostics(&layered.diagnostics);
        let canonical_ast = Some(CanonicalAst {
            provenance: layered
                .provenance
                .zip(layered.provenance_span)
                .map(|(source, span)| CanonicalAstLayer {
                    source: source.to_string(),
                    span: to_parse_span(span),
                }),
            envelope: layered
                .envelope
                .zip(layered.envelope_span)
                .map(|(source, span)| CanonicalAstLayer {
                    source: source.to_string(),
                    span: to_parse_span(span),
                }),
            content: layered
                .content
                .zip(layered.content_span)
                .map(|(source, span)| CanonicalAstLayer {
                    source: source.to_string(),
                    span: to_parse_span(span),
                }),
            metrics: layered
                .metrics
                .zip(layered.metrics_span)
                .map(|(source, span)| CanonicalAstLayer {
                    source: source.to_string(),
                    span: to_parse_span(span),
                }),
            strict_spine: layered.strict_spine,
            profile,
        });

        if matches!(layered.state, ParserState::Error) {
            diagnostics.push(ParseDiagnostic {
                code: "STTP_PARSE_LAYER_ERROR".to_string(),
                message: "unable to identify any STTP layers".to_string(),
                severity: ParseDiagnosticSeverity::Fatal,
                strict_impact: true,
                span: None,
            });

            return ParseResult::fail_with_metadata(
                "unable to identify any STTP layers",
                profile,
                diagnostics,
                canonical_ast,
            );
        }

        if matches!(profile, ParseProfile::Strict) && !strict_valid {
            diagnostics.push(ParseDiagnostic {
                code: "STTP_STRICT_PROFILE_VIOLATION".to_string(),
                message: "strict profile requires full layer spine provenance->envelope->content->metrics".to_string(),
                severity: ParseDiagnosticSeverity::Error,
                strict_impact: true,
                span: None,
            });

            return ParseResult::fail_with_metadata(
                "strict profile violation: missing or out-of-order layers",
                profile,
                diagnostics,
                canonical_ast,
            );
        }

        let content_diagnostics = validate_content_schema(raw, content, layered.content_span);
        if !content_diagnostics.is_empty() {
            strict_valid = false;
            for diag in content_diagnostics {
                diagnostics.push(diag);
            }

            if matches!(profile, ParseProfile::Strict) {
                return ParseResult::fail_with_metadata(
                    "strict profile violation: content schema requires field_name(.confidence): value",
                    profile,
                    diagnostics,
                    canonical_ast,
                );
            }
        }

        let user_avec = parse_avec_block(envelope, AVEC_USER_KEY)
            .or_else(|| parse_avec_block(raw, AVEC_USER_KEY))
            .unwrap_or_else(AvecState::zero);

        let model_avec = parse_avec_block(envelope, AVEC_MODEL_KEY)
            .or_else(|| parse_avec_block(raw, AVEC_MODEL_KEY))
            .unwrap_or_else(AvecState::zero);

        let compression_avec = parse_avec_block(metrics, AVEC_COMPRESSION_KEY)
            .or_else(|| parse_avec_block(raw, AVEC_COMPRESSION_KEY))
            .unwrap_or_else(AvecState::zero);

        let node = SttpNode {
            raw: raw.to_string(),
            session_id: session_id.to_string(),
            tier: parse_tier(envelope)
                .or_else(|| parse_tier(raw))
                .unwrap_or_default(),
            timestamp: parse_timestamp(envelope)
                .unwrap_or_else(|| parse_timestamp(raw).unwrap_or_else(Utc::now)),
            compression_depth: parse_int(&COMPRESSION_DEPTH_RX, provenance),
            parent_node_id: parse_parent_node(provenance).or_else(|| parse_parent_node(raw)),
            sync_key: String::new(),
            updated_at: Utc::now(),
            source_metadata: None,
            context_summary: parse_context_summary(provenance)
                .or_else(|| parse_context_summary(raw)),
            embedding: None,
            embedding_model: None,
            embedding_dimensions: None,
            embedded_at: None,
            user_avec,
            model_avec,
            compression_avec: Some(compression_avec),
            rho: parse_float(&RHO_RX, metrics),
            kappa: parse_float(&KAPPA_RX, metrics),
            psi: parse_float(&PSI_RX, metrics),
        };

        ParseResult::ok_with_metadata(node, profile, strict_valid, diagnostics, canonical_ast)
    }
}

fn validate_content_schema(
    raw_node: &str,
    content_layer: &str,
    layer_span: Option<crate::parsing::lexer::Span>,
) -> Vec<ParseDiagnostic> {
    let mut diagnostics = Vec::new();

    let Some(content_object) = extract_first_object(content_layer) else {
        diagnostics.push(ParseDiagnostic {
            code: "STTP_CONTENT_SCHEMA_MISSING_OBJECT".to_string(),
            message: "content layer must contain an object payload".to_string(),
            severity: ParseDiagnosticSeverity::Error,
            strict_impact: true,
            span: layer_span.map(to_parse_span),
        });
        return diagnostics;
    };

    let object_offset = offset_within(content_layer, content_object).unwrap_or(0);
    validate_object_schema(
        raw_node,
        content_layer,
        content_object,
        layer_span,
        object_offset,
        &mut diagnostics,
    );
    diagnostics
}

fn validate_object_schema(
    raw_node: &str,
    content_layer: &str,
    object_content: &str,
    layer_span: Option<crate::parsing::lexer::Span>,
    object_offset: usize,
    diagnostics: &mut Vec<ParseDiagnostic>,
) {
    for pair in split_top_level_pairs(object_content) {
        let Some(colon_idx) = find_top_level_colon(pair.text) else {
            diagnostics.push(ParseDiagnostic {
                code: "STTP_CONTENT_SCHEMA_INVALID_PAIR".to_string(),
                message: format!("content field missing ':' separator: {}", pair.text),
                severity: ParseDiagnosticSeverity::Error,
                strict_impact: true,
                span: project_content_span(
                    raw_node,
                    content_layer,
                    layer_span,
                    object_offset + pair.start,
                    pair.text.len(),
                ),
            });
            continue;
        };

        let raw_key = pair.text[..colon_idx].trim();
        let raw_value = pair.text[colon_idx + 1..].trim();

        let Some(caps) = CONTENT_KEY_RX.captures(raw_key) else {
            diagnostics.push(ParseDiagnostic {
                code: "STTP_CONTENT_SCHEMA_INVALID_KEY".to_string(),
                message: format!(
                    "content key must match field_name(.confidence): found '{raw_key}'"
                ),
                severity: ParseDiagnosticSeverity::Error,
                strict_impact: true,
                span: project_content_span(
                    raw_node,
                    content_layer,
                    layer_span,
                    object_offset + pair.start,
                    raw_key.len(),
                ),
            });
            continue;
        };

        let confidence = caps
            .name("c")
            .and_then(|m| m.as_str().parse::<f32>().ok())
            .unwrap_or(-1.0);
        if !(0.0..=1.0).contains(&confidence) {
            diagnostics.push(ParseDiagnostic {
                code: "STTP_CONTENT_SCHEMA_INVALID_CONFIDENCE".to_string(),
                message: format!(
                    "content confidence must be in [0,1]: found {confidence} for key '{raw_key}'"
                ),
                severity: ParseDiagnosticSeverity::Error,
                strict_impact: true,
                span: project_content_span(
                    raw_node,
                    content_layer,
                    layer_span,
                    object_offset + pair.start,
                    raw_key.len(),
                ),
            });
        }

        if raw_value.is_empty() {
            diagnostics.push(ParseDiagnostic {
                code: "STTP_CONTENT_SCHEMA_MISSING_VALUE".to_string(),
                message: format!("content value is missing for key '{raw_key}'"),
                severity: ParseDiagnosticSeverity::Error,
                strict_impact: true,
                span: project_content_span(
                    raw_node,
                    content_layer,
                    layer_span,
                    object_offset + pair.start + colon_idx + 1,
                    1,
                ),
            });
            continue;
        }

        if raw_value.starts_with('{') && raw_value.ends_with('}') {
            if let Some(inner) = raw_value
                .strip_prefix('{')
                .and_then(|v| v.strip_suffix('}'))
            {
                let nested_offset = object_offset
                    + pair.start
                    + colon_idx
                    + 1
                    + pair.text[colon_idx + 1..].find('{').unwrap_or(0)
                    + 1;
                validate_object_schema(
                    raw_node,
                    content_layer,
                    inner,
                    layer_span,
                    nested_offset,
                    diagnostics,
                );
            }
        }
    }
}

#[derive(Debug, Clone, Copy)]
struct PairSlice<'a> {
    text: &'a str,
    start: usize,
}

fn split_top_level_pairs(input: &str) -> Vec<PairSlice<'_>> {
    let mut parts = Vec::new();
    let mut start = 0usize;
    let mut depth_brace = 0usize;
    let mut depth_bracket = 0usize;
    let mut in_quotes = false;
    let mut escape = false;

    for (idx, ch) in input.char_indices() {
        if in_quotes {
            if escape {
                escape = false;
                continue;
            }
            if ch == '\\' {
                escape = true;
                continue;
            }
            if ch == '"' {
                in_quotes = false;
            }
            continue;
        }

        match ch {
            '"' => in_quotes = true,
            '{' => depth_brace += 1,
            '}' => depth_brace = depth_brace.saturating_sub(1),
            '[' => depth_bracket += 1,
            ']' => depth_bracket = depth_bracket.saturating_sub(1),
            ',' if depth_brace == 0 && depth_bracket == 0 => {
                let part = input[start..idx].trim();
                if !part.is_empty() {
                    let trimmed_start = start + input[start..idx].find(part).unwrap_or(0);
                    parts.push(PairSlice {
                        text: part,
                        start: trimmed_start,
                    });
                }
                start = idx + 1;
            }
            _ => {}
        }
    }

    let tail = input[start..].trim();
    if !tail.is_empty() {
        let trimmed_start = start + input[start..].find(tail).unwrap_or(0);
        parts.push(PairSlice {
            text: tail,
            start: trimmed_start,
        });
    }

    parts
}

fn find_top_level_colon(input: &str) -> Option<usize> {
    let mut depth_brace = 0usize;
    let mut depth_bracket = 0usize;
    let mut in_quotes = false;
    let mut escape = false;

    for (idx, ch) in input.char_indices() {
        if in_quotes {
            if escape {
                escape = false;
                continue;
            }
            if ch == '\\' {
                escape = true;
                continue;
            }
            if ch == '"' {
                in_quotes = false;
            }
            continue;
        }

        match ch {
            '"' => in_quotes = true,
            '{' => depth_brace += 1,
            '}' => depth_brace = depth_brace.saturating_sub(1),
            '[' => depth_bracket += 1,
            ']' => depth_bracket = depth_bracket.saturating_sub(1),
            ':' if depth_brace == 0 && depth_bracket == 0 => return Some(idx),
            _ => {}
        }
    }

    None
}

fn extract_first_object(input: &str) -> Option<&str> {
    let start = input.find('{')?;
    extract_braced_content(input, start)
}

fn to_parse_span(span: crate::parsing::lexer::Span) -> ParseSpan {
    ParseSpan {
        start: span.start,
        end: span.end,
        line: span.line,
        column: span.column,
    }
}

fn offset_within(haystack: &str, needle: &str) -> Option<usize> {
    let haystack_start = haystack.as_ptr() as usize;
    let needle_start = needle.as_ptr() as usize;
    let offset = needle_start.checked_sub(haystack_start)?;
    if offset <= haystack.len() {
        Some(offset)
    } else {
        None
    }
}

fn project_content_span(
    raw_node: &str,
    content_layer: &str,
    layer_span: Option<crate::parsing::lexer::Span>,
    local_offset_in_object: usize,
    len: usize,
) -> Option<ParseSpan> {
    let layer_span = layer_span?;
    let object_offset = extract_first_object(content_layer)
        .and_then(|obj| offset_within(content_layer, obj))
        .unwrap_or(0);

    let start = layer_span.start + object_offset + local_offset_in_object;
    let end = start.saturating_add(len.max(1));
    let (line, column) = line_col_at(raw_node, start);

    Some(ParseSpan {
        start,
        end,
        line,
        column,
    })
}

fn line_col_at(raw: &str, target_index: usize) -> (usize, usize) {
    let mut line = 1usize;
    let mut column = 1usize;
    let mut index = 0usize;

    for ch in raw.chars() {
        if index >= target_index {
            break;
        }

        if ch == '\n' {
            line += 1;
            column = 1;
        } else {
            column += 1;
        }

        index += ch.len_utf8();
    }

    (line, column)
}

fn to_structured_diagnostics(codes: &[String]) -> Vec<ParseDiagnostic> {
    codes
        .iter()
        .map(|code| {
            let (message, severity, strict_impact) = match code.as_str() {
                "non_strict_spine_recovered_tolerantly" => (
                    "layer order deviates from strict spine; tolerant recovery applied",
                    ParseDiagnosticSeverity::Warning,
                    true,
                ),
                "missing_layer_provenance" => (
                    "provenance layer marker not found",
                    ParseDiagnosticSeverity::Error,
                    true,
                ),
                "missing_layer_envelope" => (
                    "envelope layer marker not found",
                    ParseDiagnosticSeverity::Error,
                    true,
                ),
                "missing_layer_content" => (
                    "content layer marker not found",
                    ParseDiagnosticSeverity::Warning,
                    true,
                ),
                "missing_layer_metrics" => (
                    "metrics layer marker not found",
                    ParseDiagnosticSeverity::Error,
                    true,
                ),
                _ => (
                    "parser emitted an unknown diagnostic",
                    ParseDiagnosticSeverity::Info,
                    false,
                ),
            };

            ParseDiagnostic {
                code: code.clone(),
                message: message.to_string(),
                severity,
                strict_impact,
                span: None,
            }
        })
        .collect()
}

fn parse_avec_block(source: &str, key: &str) -> Option<AvecState> {
    let object = extract_named_object(source, key)?;

    let mut stability = None;
    let mut friction = None;
    let mut logic = None;
    let mut autonomy = None;

    for caps in AVEC_ENTRY_RX.captures_iter(object) {
        let name = caps.name("k")?.as_str().to_ascii_lowercase();
        let value = caps
            .name("v")
            .and_then(|v| v.as_str().parse::<f32>().ok())
            .unwrap_or(0.0);

        match name.as_str() {
            "stability" => stability = Some(value),
            "friction" => friction = Some(value),
            "logic" => logic = Some(value),
            "autonomy" => autonomy = Some(value),
            _ => {}
        }
    }

    Some(AvecState {
        stability: stability?,
        friction: friction?,
        logic: logic?,
        autonomy: autonomy?,
    })
}

fn parse_timestamp(raw: &str) -> Option<DateTime<Utc>> {
    let maybe_ts = TIMESTAMP_RX
        .captures(raw)
        .and_then(|c| c.name("v"))
        .map(|m| m.as_str());

    if let Some(ts) = maybe_ts {
        if let Ok(parsed) = DateTime::parse_from_rfc3339(ts) {
            return Some(parsed.with_timezone(&Utc));
        }
    }

    None
}

fn parse_tier(raw: &str) -> Option<String> {
    TIER_RX
        .captures(raw)
        .and_then(|c| c.name("v"))
        .map(|m| m.as_str().to_string())
}

fn parse_parent_node(raw: &str) -> Option<String> {
    let caps = PARENT_NODE_RX.captures(raw)?;
    if caps.name("null").is_some() {
        return None;
    }

    if let Some(v) = caps.name("ref") {
        return Some(v.as_str().to_string());
    }

    if let Some(v) = caps.name("quoted") {
        return Some(v.as_str().to_string());
    }

    None
}

fn parse_context_summary(raw: &str) -> Option<String> {
    let caps = CONTEXT_SUMMARY_RX.captures(raw)?;
    let value = caps
        .name("quoted")
        .or_else(|| caps.name("bare"))
        .map(|m| m.as_str().trim())?;

    if value.is_empty() {
        None
    } else {
        Some(value.to_string())
    }
}

fn parse_int(rx: &Regex, raw: &str) -> i32 {
    rx.captures(raw)
        .and_then(|c| c.name("v"))
        .and_then(|v| v.as_str().parse::<i32>().ok())
        .unwrap_or(0)
}

fn parse_float(rx: &Regex, raw: &str) -> f32 {
    rx.captures(raw)
        .and_then(|c| c.name("v"))
        .and_then(|v| v.as_str().parse::<f32>().ok())
        .unwrap_or(0.0)
}

fn extract_named_object<'a>(source: &'a str, key: &str) -> Option<&'a str> {
    let key_index = source.find(key)?;
    let after_key = &source[key_index + key.len()..];
    let colon_relative = after_key.find(':')?;
    let after_colon = &after_key[colon_relative + 1..];

    let brace_relative = after_colon.find('{')?;
    let absolute_brace_start = key_index + key.len() + colon_relative + 1 + brace_relative;
    extract_braced_content(source, absolute_brace_start)
}

fn extract_braced_content(source: &str, brace_start: usize) -> Option<&str> {
    let bytes = source.as_bytes();
    if *bytes.get(brace_start)? != b'{' {
        return None;
    }

    let mut depth = 0usize;
    for (idx, ch) in source[brace_start..].char_indices() {
        match ch {
            '{' => depth += 1,
            '}' => {
                depth = depth.saturating_sub(1);
                if depth == 0 {
                    let content_start = brace_start + 1;
                    let content_end = brace_start + idx;
                    return source.get(content_start..content_end);
                }
            }
            _ => {}
        }
    }

    None
}

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

    #[test]
    fn should_parse_avec_with_noncanonical_order() {
        let input =
            r#"user_avec: { logic: 0.90, stability: 0.81, autonomy: 0.92, friction: 0.11 }"#;
        let parsed = parse_avec_block(input, AVEC_USER_KEY).expect("avec should parse");

        assert!((parsed.stability - 0.81).abs() < 0.0001);
        assert!((parsed.friction - 0.11).abs() < 0.0001);
        assert!((parsed.logic - 0.90).abs() < 0.0001);
        assert!((parsed.autonomy - 0.92).abs() < 0.0001);
    }

    #[test]
    fn should_extract_nested_object_block() {
        let input = r#"compression_avec: { stability: 0.8, friction: 0.2, logic: 0.9, autonomy: 0.7, ext: { kept: 1 } }"#;
        let object = extract_named_object(input, AVEC_COMPRESSION_KEY).expect("block should parse");
        assert!(object.contains("stability"));
        assert!(object.contains("ext: { kept: 1 }"));
    }

    #[test]
    fn should_accept_content_value_with_or_without_quotes() {
        let quoted = r#"◈⟨ { topic(.91): \"quoted\" } ⟩"#;
        let unquoted = r#"◈⟨ { topic(.91): unquoted_value } ⟩"#;

        let quoted_diagnostics = validate_content_schema(quoted, quoted, None);
        let unquoted_diagnostics = validate_content_schema(unquoted, unquoted, None);

        assert!(quoted_diagnostics.is_empty());
        assert!(unquoted_diagnostics.is_empty());
    }

    #[test]
    fn should_reject_content_without_confidence_signature() {
        let content = r#"◈⟨ { topic: \"invalid\" } ⟩"#;
        let diagnostics = validate_content_schema(content, content, None);

        assert!(
            diagnostics
                .iter()
                .any(|d| d.code == "STTP_CONTENT_SCHEMA_INVALID_KEY")
        );
    }

    #[test]
    fn should_reject_content_confidence_out_of_range() {
        let content = r#"◈⟨ { topic(1.20): \"invalid\" } ⟩"#;
        let diagnostics = validate_content_schema(content, content, None);

        assert!(
            diagnostics
                .iter()
                .any(|d| d.code == "STTP_CONTENT_SCHEMA_INVALID_CONFIDENCE")
        );
    }

    #[test]
    fn should_extract_context_summary_from_prime() {
        let parser = SttpNodeParser::new();
        let raw = r#"
⊕⟨ { trigger: manual, response_format: temporal_node, origin_session: "ctx-test", compression_depth: 1, parent_node: null, prime: { attractor_config: { stability: 0.8, friction: 0.2, logic: 0.9, autonomy: 0.7 }, context_summary: "parser hardening session", relevant_tier: raw, retrieval_budget: 3 } } ⟩
⦿⟨ { timestamp: "2026-03-05T06:30:00Z", tier: raw, session_id: "ctx-test", user_avec: { stability: 0.8, friction: 0.2, logic: 0.9, autonomy: 0.7, psi: 2.6 }, model_avec: { stability: 0.8, friction: 0.2, logic: 0.9, autonomy: 0.7, psi: 2.6 } } ⟩
◈⟨ { note(.99): "ok" } ⟩
⍉⟨ { rho: 0.9, kappa: 0.9, psi: 2.6, compression_avec: { stability: 0.8, friction: 0.2, logic: 0.9, autonomy: 0.7, psi: 2.6 } } ⟩
"#;

        let parsed = parser.try_parse_tolerant(raw, "ctx-test");
        assert!(parsed.success);

        let node = parsed.node.expect("parsed node should exist");
        assert_eq!(
            node.context_summary.as_deref(),
            Some("parser hardening session")
        );
    }

    #[test]
    fn strict_profile_should_fail_on_missing_layer() {
        let parser = SttpNodeParser::new();
        let raw = r#"
⊕⟨ { trigger: manual, compression_depth: 1 } ⟩
⦿⟨ { timestamp: "2026-03-05T06:30:00Z", tier: raw, user_avec: { stability: 0.8, friction: 0.2, logic: 0.9, autonomy: 0.7 }, model_avec: { stability: 0.8, friction: 0.2, logic: 0.9, autonomy: 0.7 } } ⟩
⍉⟨ { rho: 0.1, kappa: 0.2, psi: 2.6, compression_avec: { stability: 0.8, friction: 0.2, logic: 0.9, autonomy: 0.7 } } ⟩
"#;

        let parsed = parser.try_parse_strict(raw, "strict-test");
        assert!(!parsed.success);
        assert_eq!(parsed.profile, ParseProfile::Strict);
        assert!(parsed.canonical_ast.is_some());
        assert!(!parsed.diagnostics.is_empty());
    }

    #[test]
    fn tolerant_profile_should_recover_with_diagnostics() {
        let parser = SttpNodeParser::new();
        let raw = r#"
⊕⟨ { trigger: manual, compression_depth: 1 } ⟩
⦿⟨ { timestamp: "2026-03-05T06:30:00Z", tier: raw, user_avec: { stability: 0.8, friction: 0.2, logic: 0.9, autonomy: 0.7 }, model_avec: { stability: 0.8, friction: 0.2, logic: 0.9, autonomy: 0.7 } } ⟩
⍉⟨ { rho: 0.1, kappa: 0.2, psi: 2.6, compression_avec: { stability: 0.8, friction: 0.2, logic: 0.9, autonomy: 0.7 } } ⟩
"#;

        let parsed = parser.try_parse_tolerant(raw, "tolerant-test");
        assert!(parsed.success);
        assert_eq!(parsed.profile, ParseProfile::Tolerant);
        assert!(!parsed.strict_valid);
        assert!(!parsed.diagnostics.is_empty());
        assert!(parsed.canonical_ast.is_some());
    }
}