code-system-graph-core 1.0.0

Core extraction, linking, and query engine for Code System Graph.
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
use std::collections::BTreeMap;

use code_system_graph_model::{EdgeKind, contains_unsafe_metadata_characters};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use thiserror::Error;

use crate::execution_policy::{ExecutionPolicy, ExecutionPolicyOverrides, InvalidExecutionPolicy};
use crate::extraction_budget::{
    ExtractionBudgetOverrides, ExtractionBudgets, InvalidExtractionBudget
};
use crate::ignore_policy::{IgnorePatternError, validate_excludes, validate_include_defaults};

const MANUAL_ENDPOINT_MAX_BYTES: usize = 2_048;
const MANUAL_CONTRACT_MAX_BYTES: usize = 1_024;
const MANUAL_REASON_MAX_BYTES: usize = 4_096;

/// Strict workspace manifest accepted by the initial registry slice.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct WorkspaceManifest {
    /// Manifest schema version.
    pub version: u32,
    /// Stable workspace name.
    pub name: String,
    /// Additional filesystem roots repositories may resolve beneath.
    #[serde(rename = "allowedRoots", default)]
    pub allowed_roots: Vec<String>,
    /// Repositories indexed by unique alias.
    pub repos: BTreeMap<String, RepositoryConfig>,
    /// Versioned exact relationship declarations and suppressions.
    #[serde(rename = "manualLinks", default)]
    pub manual_links: Vec<ManualLinkConfig>,
    /// Optional operator-owned extraction safety limit overrides.
    #[serde(rename = "extractionBudgets", default)]
    pub extraction_budgets: Option<ExtractionBudgetOverrides>,
    /// Optional operator-owned supervised-execution policy overrides.
    #[serde(rename = "executionPolicy", default)]
    pub execution_policy: Option<ExecutionPolicyOverrides>,
}

/// Repository registration and boundary inputs.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct RepositoryConfig {
    /// Native path as written in the manifest.
    pub path: String,
    /// Optional `OpenAPI` artifact relative to the repository root.
    pub openapi: Option<String>,
    /// Explicit HTTP consumers that cannot yet be extracted from source.
    pub http_consumers: Option<Vec<HttpConsumerConfig>>,
    /// Explicit cross-language integration tests and validated HTTP contracts.
    pub integration_tests: Option<Vec<IntegrationTestConfig>>,
    /// Explicit contract-to-source implementation anchors.
    pub implementations: Option<Vec<ContractImplementationConfig>>,
    /// Additional repository-relative globs omitted from automatic discovery.
    pub excludes: Option<Vec<String>>,
    /// Repository-relative exceptions to reactivable built-in exclusions.
    pub include_defaults: Option<Vec<String>>,
}

/// Exact manual relationship or automatic-link suppression.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct ManualLinkConfig {
    /// Exact source node identifier or stable key.
    pub from: String,
    /// Exact target node identifier or stable key.
    pub to: String,
    /// Concrete graph relationship.
    pub relation: EdgeKind,
    /// Optional contract identity retained for audit context.
    pub contract: Option<String>,
    /// Mandatory human explanation for the override.
    pub reason: String,
    /// Whether to remove the exact automatic relationship instead of creating one.
    #[serde(default)]
    pub suppress: bool,
}

/// Explicit HTTP consumer boundary.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct HttpConsumerConfig {
    /// Upper- or lower-case HTTP method.
    pub method: String,
    /// HTTP path template.
    pub path: String,
    /// Repository-relative evidence path.
    pub source: String,
}

/// Explicit test case that validates an HTTP contract across a repository boundary.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct IntegrationTestConfig {
    /// Test function or scenario name.
    pub name: String,
    /// Repository-relative test source path.
    pub path: String,
    /// Test framework, such as `pytest`.
    pub framework: String,
    /// Source language, such as `python`.
    pub language: String,
    /// HTTP contract validated by this test.
    pub validates: HttpContractConfig,
}

/// Canonicalizable HTTP contract target.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct HttpContractConfig {
    /// HTTP method.
    pub method: String,
    /// HTTP path template.
    pub path: String,
}

/// Explicit source symbol that implements an HTTP contract.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct ContractImplementationConfig {
    /// Source language, such as `rust`.
    pub language: String,
    /// Repository-relative implementation source path.
    pub path: String,
    /// Qualified or local symbol name.
    pub symbol: String,
    /// HTTP contract implemented by the symbol.
    pub implements: HttpContractConfig,
}

/// Error returned while validating a workspace manifest.
#[derive(Debug, Error)]
pub enum ManifestError {
    /// YAML could not be decoded according to the strict schema.
    #[error("invalid workspace manifest: {0}")]
    InvalidYaml(#[from] serde_saphyr::DeserializeError),
    /// The manifest uses an unsupported schema version.
    #[error("unsupported manifest version {found}; expected version 1")]
    UnsupportedVersion {
        /// Version supplied by the user.
        found: u32,
    },
    /// A required string is empty after trimming.
    #[error("manifest field `{field}` must not be empty")]
    EmptyField {
        /// Dot-style field location.
        field: String,
    },
    /// A metadata field contains terminal control or bidirectional formatting characters.
    #[error("manifest field `{field}` contains unsafe control or bidirectional characters")]
    UnsafeMetadata {
        /// Dot-style field location.
        field: String,
    },
    /// A bounded manifest metadata field exceeds its hard byte limit.
    #[error("manifest field `{field}` exceeds the {maximum}-byte limit")]
    FieldTooLong {
        /// Dot-style field location.
        field: String,
        /// Non-overridable maximum UTF-8 byte length.
        maximum: usize,
    },
    /// A repository discovery pattern is malformed or unsafe.
    #[error("manifest field `{field}` is invalid: {source}")]
    InvalidIgnorePattern {
        /// Dot-style field location.
        field: String,
        /// Pattern validation failure.
        #[source]
        source: IgnorePatternError,
    },
    /// An extraction budget is zero or cannot be represented internally.
    #[error("invalid workspace manifest: {0}")]
    InvalidExtractionBudget(#[from] InvalidExtractionBudget),
    /// An execution policy value or relationship is invalid.
    #[error("invalid workspace manifest: {0}")]
    InvalidExecutionPolicy(#[from] InvalidExecutionPolicy),
    /// The workspace does not register any repositories.
    #[error("manifest field `repos` must contain at least one repository")]
    EmptyRepositories,
    /// A manual relationship uses the reserved generic relationship kind.
    #[error("manifest field `manualLinks[{index}].relation` must be a concrete edge kind")]
    NonConcreteManualRelation {
        /// Zero-based declaration index.
        index: usize,
    },
    /// A manual relationship names the same literal endpoint twice.
    #[error("manual link at `manualLinks[{index}]` cannot link endpoint `{endpoint}` to itself")]
    ManualSelfLink {
        /// Zero-based declaration index.
        index: usize,
        /// Repeated endpoint literal.
        endpoint: String,
    },
    /// Two manual declarations target the same relationship identity.
    #[error(
        "manual link at `manualLinks[{duplicate}]` duplicates `manualLinks[{first}]` for `{from}` -> `{to}` ({relation:?})"
    )]
    DuplicateManualLink {
        /// Index of the first declaration.
        first: usize,
        /// Index of the repeated declaration.
        duplicate: usize,
        /// Exact source endpoint literal.
        from: String,
        /// Exact target endpoint literal.
        to: String,
        /// Repeated relationship.
        relation: EdgeKind,
    },
}

/// Parses and semantically validates a strict workspace manifest.
///
/// # Errors
///
/// Returns [`ManifestError`] for malformed YAML, unknown keys, unsupported versions, empty
/// fields, or an empty repository registry.
pub fn parse_manifest(input: &str) -> Result<WorkspaceManifest, ManifestError> {
    let manifest: WorkspaceManifest = crate::yaml::from_str(input)?;
    if manifest.version != 1 {
        return Err(ManifestError::UnsupportedVersion {
            found: manifest.version,
        });
    }
    validate_not_empty("name", &manifest.name)?;
    for (index, root) in manifest.allowed_roots.iter().enumerate() {
        validate_not_empty(&format!("allowedRoots[{index}]"), root)?;
    }
    if manifest.repos.is_empty() {
        return Err(ManifestError::EmptyRepositories);
    }
    validate_manual_links(&manifest.manual_links)?;
    ExtractionBudgets::resolve(manifest.extraction_budgets.as_ref())?;
    ExecutionPolicy::resolve(manifest.execution_policy.as_ref())?;
    for (alias, repository) in &manifest.repos {
        validate_not_empty(&format!("repos.{alias}"), alias)?;
        validate_not_empty(&format!("repos.{alias}.path"), &repository.path)?;
        if let Some(openapi) = &repository.openapi {
            validate_not_empty(&format!("repos.{alias}.openapi"), openapi)?;
        }
        for (index, consumer) in repository.http_consumers.iter().flatten().enumerate() {
            validate_not_empty(
                &format!("repos.{alias}.httpConsumers[{index}].method"),
                &consumer.method,
            )?;
            validate_not_empty(
                &format!("repos.{alias}.httpConsumers[{index}].path"),
                &consumer.path,
            )?;
            validate_not_empty(
                &format!("repos.{alias}.httpConsumers[{index}].source"),
                &consumer.source,
            )?;
        }
        for (index, test) in repository.integration_tests.iter().flatten().enumerate() {
            for (field, value) in [
                ("name", test.name.as_str()),
                ("path", test.path.as_str()),
                ("framework", test.framework.as_str()),
                ("language", test.language.as_str()),
                ("validates.method", test.validates.method.as_str()),
                ("validates.path", test.validates.path.as_str()),
            ] {
                validate_not_empty(
                    &format!("repos.{alias}.integrationTests[{index}].{field}"),
                    value,
                )?;
            }
        }
        for (index, implementation) in repository.implementations.iter().flatten().enumerate() {
            for (field, value) in [
                ("language", implementation.language.as_str()),
                ("path", implementation.path.as_str()),
                ("symbol", implementation.symbol.as_str()),
                (
                    "implements.method",
                    implementation.implements.method.as_str(),
                ),
                ("implements.path", implementation.implements.path.as_str()),
            ] {
                validate_not_empty(
                    &format!("repos.{alias}.implementations[{index}].{field}"),
                    value,
                )?;
            }
        }
        validate_repository_ignore_patterns(alias, repository)?;
    }
    Ok(manifest)
}

fn validate_repository_ignore_patterns(
    alias: &str,
    repository: &RepositoryConfig,
) -> Result<(), ManifestError> {
    if let Some(patterns) = &repository.excludes {
        validate_excludes(patterns).map_err(|source| ManifestError::InvalidIgnorePattern {
            field: format!("repos.{alias}.excludes"),
            source,
        })?;
    }
    if let Some(patterns) = &repository.include_defaults {
        validate_include_defaults(patterns).map_err(|source| {
            ManifestError::InvalidIgnorePattern {
                field: format!("repos.{alias}.includeDefaults"),
                source,
            }
        })?;
    }
    Ok(())
}

/// Validates manually constructed relationship declarations.
///
/// # Errors
///
/// Returns [`ManifestError`] for empty or unsafe metadata, reserved relationship kinds,
/// self-links, or duplicate source/target/relation declarations.
pub fn validate_manual_links(links: &[ManualLinkConfig]) -> Result<(), ManifestError> {
    let mut identities = BTreeMap::new();
    for (index, link) in links.iter().enumerate() {
        validate_not_empty(&format!("manualLinks[{index}].from"), &link.from)?;
        validate_not_empty(&format!("manualLinks[{index}].to"), &link.to)?;
        validate_not_empty(&format!("manualLinks[{index}].reason"), &link.reason)?;
        validate_maximum(
            &format!("manualLinks[{index}].from"),
            &link.from,
            MANUAL_ENDPOINT_MAX_BYTES,
        )?;
        validate_maximum(
            &format!("manualLinks[{index}].to"),
            &link.to,
            MANUAL_ENDPOINT_MAX_BYTES,
        )?;
        validate_maximum(
            &format!("manualLinks[{index}].reason"),
            &link.reason,
            MANUAL_REASON_MAX_BYTES,
        )?;
        if let Some(contract) = &link.contract {
            validate_not_empty(&format!("manualLinks[{index}].contract"), contract)?;
            validate_maximum(
                &format!("manualLinks[{index}].contract"),
                contract,
                MANUAL_CONTRACT_MAX_BYTES,
            )?;
        }
        if link.relation == EdgeKind::ManualLink {
            return Err(ManifestError::NonConcreteManualRelation { index });
        }
        if link.from == link.to {
            return Err(ManifestError::ManualSelfLink {
                index,
                endpoint: link.from.clone(),
            });
        }
        let identity = (link.from.clone(), link.to.clone(), link.relation);
        if let Some(first) = identities.insert(identity, index) {
            return Err(ManifestError::DuplicateManualLink {
                first,
                duplicate: index,
                from: link.from.clone(),
                to: link.to.clone(),
                relation: link.relation,
            });
        }
    }
    Ok(())
}

fn validate_not_empty(field: &str, value: &str) -> Result<(), ManifestError> {
    if value.trim().is_empty() {
        return Err(ManifestError::EmptyField {
            field: field.to_owned(),
        });
    }
    if contains_unsafe_metadata_characters(value) {
        return Err(ManifestError::UnsafeMetadata {
            field: field.to_owned(),
        });
    }
    Ok(())
}

fn validate_maximum(field: &str, value: &str, maximum: usize) -> Result<(), ManifestError> {
    if value.len() > maximum {
        return Err(ManifestError::FieldTooLong {
            field: field.to_owned(),
            maximum,
        });
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use code_system_graph_model::EdgeKind;

    use super::{MANUAL_REASON_MAX_BYTES, ManifestError, parse_manifest};
    use crate::{ExecutionPolicy, ExtractionBudgets, IgnorePatternError};

    const VALID: &str = r"
version: 1
name: commerce
repos:
  web:
    path: ../web
    httpConsumers:
      - method: POST
        path: /api/orders
        source: src/checkout.ts
";

    #[test]
    fn parse_manifest_should_accept_strict_valid_input() {
        let result = parse_manifest(VALID);

        assert!(result.is_ok(), "unexpected manifest error: {result:?}");
    }

    #[test]
    fn extraction_budgets_should_be_optional_and_resolve_safe_defaults() {
        let manifest = parse_manifest(VALID).expect("manifest without advanced budgets is valid");
        let effective = ExtractionBudgets::resolve(manifest.extraction_budgets.as_ref())
            .expect("safe defaults are valid");

        assert_eq!(effective, ExtractionBudgets::default());
    }

    #[test]
    fn extraction_budgets_should_accept_partial_higher_and_lower_overrides() {
        let input = VALID.replace(
            "name: commerce",
            "name: commerce\nextractionBudgets:\n  maxInputBytesPerArtifact: 1024\n  maxStructuralDepthPerArtifact: 128",
        );
        let manifest = parse_manifest(&input).expect("partial override should be valid");
        let effective = ExtractionBudgets::resolve(manifest.extraction_budgets.as_ref())
            .expect("partial override should resolve");

        assert_eq!(effective.max_input_bytes_per_artifact, 1_024);
        assert_eq!(effective.max_structural_depth_per_artifact, 128);
        assert_eq!(
            effective.max_ast_depth_per_artifact,
            ExtractionBudgets::default().max_ast_depth_per_artifact
        );
    }

    #[test]
    fn extraction_budgets_should_accept_a_complete_override() {
        let input = VALID.replace(
            "name: commerce",
            "name: commerce\nextractionBudgets:\n  maxInputBytesPerArtifact: 1\n  maxStructuralDepthPerArtifact: 2\n  maxAstDepthPerArtifact: 3\n  maxWorkUnitsPerArtifact: 4\n  maxTreeSitterNodesPerArtifact: 5\n  maxObservationsPerArtifact: 6\n  maxAccumulatedStringBytesPerArtifact: 7\n  maxSerializedOutputBytesPerArtifact: 8\n  maxStringBytesPerValue: 9\n  maxPortablePathBytesPerValue: 10\n  maxIdentifierBytesPerValue: 11\n  maxStructuredWallTimeMsPerArtifact: 12\n  maxTreeSitterWallTimeMsPerArtifact: 13",
        );
        let manifest = parse_manifest(&input).expect("complete override should be valid");
        let effective = ExtractionBudgets::resolve(manifest.extraction_budgets.as_ref())
            .expect("complete override should resolve");

        assert_eq!(effective.max_input_bytes_per_artifact, 1);
        assert_eq!(effective.max_structural_depth_per_artifact, 2);
        assert_eq!(effective.max_ast_depth_per_artifact, 3);
        assert_eq!(effective.max_work_units_per_artifact, 4);
        assert_eq!(effective.max_tree_sitter_nodes_per_artifact, 5);
        assert_eq!(effective.max_observations_per_artifact, 6);
        assert_eq!(effective.max_accumulated_string_bytes_per_artifact, 7);
        assert_eq!(effective.max_serialized_output_bytes_per_artifact, 8);
        assert_eq!(effective.max_string_bytes_per_value, 9);
        assert_eq!(effective.max_portable_path_bytes_per_value, 10);
        assert_eq!(effective.max_identifier_bytes_per_value, 11);
        assert_eq!(effective.max_structured_wall_time_ms_per_artifact, 12);
        assert_eq!(effective.max_tree_sitter_wall_time_ms_per_artifact, 13);
    }

    #[test]
    fn extraction_budgets_should_reject_zero_overflow_and_unknown_fields() {
        let zero = VALID.replace(
            "name: commerce",
            "name: commerce\nextractionBudgets:\n  maxWorkUnitsPerArtifact: 0",
        );
        let overflow = VALID.replace(
            "name: commerce",
            "name: commerce\nextractionBudgets:\n  maxWorkUnitsPerArtifact: 18446744073709551616",
        );
        let unknown = VALID.replace(
            "name: commerce",
            "name: commerce\nextractionBudgets:\n  maxWorkUnitsPerArtifact: 1\n  maximumMagic: 2",
        );

        assert!(matches!(
            parse_manifest(&zero),
            Err(ManifestError::InvalidExtractionBudget(_))
        ));
        assert!(matches!(
            parse_manifest(&overflow),
            Err(ManifestError::InvalidYaml(_))
        ));
        assert!(matches!(
            parse_manifest(&unknown),
            Err(ManifestError::InvalidYaml(_))
        ));
    }

    #[test]
    fn execution_policy_should_resolve_defaults_and_partial_overrides() {
        let manifest = parse_manifest(VALID).expect("manifest without execution policy is valid");
        assert_eq!(
            ExecutionPolicy::resolve(manifest.execution_policy.as_ref()).expect("defaults"),
            ExecutionPolicy::default()
        );

        let input = VALID.replace(
            "name: commerce",
            "name: commerce\nexecutionPolicy:\n  maxScanWallTimeMs: 28800000\n  maxNoProgressTimeMs: 600000",
        );
        let manifest = parse_manifest(&input).expect("partial policy override is valid");
        let effective = ExecutionPolicy::resolve(manifest.execution_policy.as_ref())
            .expect("partial policy resolves");
        assert_eq!(effective.max_scan_wall_time_ms, 28_800_000);
        assert_eq!(effective.max_no_progress_time_ms, 600_000);
        assert_eq!(
            effective.max_worker_memory_bytes,
            ExecutionPolicy::default().max_worker_memory_bytes
        );
    }

    #[test]
    fn execution_policy_should_reject_zero_overflow_unknown_and_invalid_relationships() {
        let zero = VALID.replace(
            "name: commerce",
            "name: commerce\nexecutionPolicy:\n  maxWorkerMemoryBytes: 0",
        );
        let overflow = VALID.replace(
            "name: commerce",
            "name: commerce\nexecutionPolicy:\n  maxWorkerMemoryBytes: 18446744073709551616",
        );
        let unknown = VALID.replace(
            "name: commerce",
            "name: commerce\nexecutionPolicy:\n  maximumMagic: 1",
        );
        let invalid = VALID.replace(
            "name: commerce",
            "name: commerce\nexecutionPolicy:\n  maxScanWallTimeMs: 1000\n  maxNoProgressTimeMs: 1001\n  maxCodeGraphSyncWallTimeMsPerRepo: 1000\n  gracefulTerminationMs: 1",
        );

        assert!(matches!(
            parse_manifest(&zero),
            Err(ManifestError::InvalidExecutionPolicy(_))
        ));
        assert!(matches!(
            parse_manifest(&overflow),
            Err(ManifestError::InvalidYaml(_))
        ));
        assert!(matches!(
            parse_manifest(&unknown),
            Err(ManifestError::InvalidYaml(_))
        ));
        let invalid_result = parse_manifest(&invalid);
        assert!(
            matches!(
                invalid_result,
                Err(ManifestError::InvalidExecutionPolicy(_))
            ),
            "unexpected invalid relationship result: {invalid_result:?}"
        );
    }

    #[test]
    fn parse_manifest_should_accept_repository_ignore_patterns() {
        let input = VALID.replace(
            "    path: ../web",
            "    path: ../web\n    excludes: [coverage/**]\n    includeDefaults: [vendor/internal/**]",
        );

        let result = parse_manifest(&input);

        assert!(result.is_ok(), "unexpected manifest error: {result:?}");
    }

    #[test]
    fn parse_manifest_should_reject_protected_default_include() {
        let input = VALID.replace(
            "    path: ../web",
            "    path: ../web\n    includeDefaults: [.codegraph/**]",
        );

        let result = parse_manifest(&input);

        assert!(matches!(
            result,
            Err(ManifestError::InvalidIgnorePattern { field, .. })
                if field == "repos.web.includeDefaults"
        ));
    }

    #[test]
    fn parse_manifest_should_reject_unsupported_ignore_syntax() {
        let input = VALID.replace(
            "    path: ../web",
            "    path: ../web\n    excludes: [\"src/[ab]/**\"]",
        );

        let result = parse_manifest(&input);

        assert!(matches!(
            result,
            Err(ManifestError::InvalidIgnorePattern {
                source: IgnorePatternError::UnsupportedSyntax(_),
                ..
            })
        ));
    }

    #[test]
    fn parse_manifest_should_reject_unknown_keys() {
        let result =
            parse_manifest(&VALID.replace("name: commerce", "name: commerce\nextra: true"));

        assert!(matches!(result, Err(ManifestError::InvalidYaml(_))));
    }

    #[test]
    fn parse_manifest_should_reject_unsupported_version() {
        let result = parse_manifest(&VALID.replace("version: 1", "version: 2"));

        assert!(matches!(
            result,
            Err(ManifestError::UnsupportedVersion { found: 2 })
        ));
    }

    #[test]
    fn parse_manifest_should_reject_bidi_metadata() {
        let result = parse_manifest(&VALID.replace("commerce", "commerce\u{202e}txt"));

        assert!(matches!(
            result,
            Err(ManifestError::UnsafeMetadata { field }) if field == "name"
        ));
    }

    #[test]
    fn parse_manifest_should_accept_exact_manual_link() {
        let input = format!(
            "{VALID}manualLinks:\n  - from: node:web\n    to: service:api\n    relation: consumes\n    contract: POST /orders\n    reason: Manual checkout boundary\n"
        );

        let result = parse_manifest(&input).map(|manifest| manifest.manual_links);

        assert!(matches!(
            result,
            Ok(links)
                if links.len() == 1
                    && links[0].relation == EdgeKind::Consumes
                    && !links[0].suppress
        ));
    }

    #[test]
    fn parse_manifest_should_reject_unknown_manual_link_fields() {
        let input = format!(
            "{VALID}manualLinks:\n  - from: node:web\n    to: service:api\n    relation: consumes\n    reason: Explicit dependency\n    approximate: true\n"
        );

        let result = parse_manifest(&input);

        assert!(matches!(result, Err(ManifestError::InvalidYaml(_))));
    }

    #[test]
    fn parse_manifest_should_reject_empty_manual_link_reason() {
        let input = format!(
            "{VALID}manualLinks:\n  - from: node:web\n    to: service:api\n    relation: consumes\n    reason: '   '\n"
        );

        let result = parse_manifest(&input);

        assert!(matches!(
            result,
            Err(ManifestError::EmptyField { field })
                if field == "manualLinks[0].reason"
        ));
    }

    #[test]
    fn parse_manifest_should_reject_oversized_manual_link_reason() {
        let input = format!(
            "{VALID}manualLinks:\n  - from: node:web\n    to: service:api\n    relation: consumes\n    reason: {}\n",
            "x".repeat(MANUAL_REASON_MAX_BYTES + 1)
        );

        let result = parse_manifest(&input);

        assert!(matches!(
            result,
            Err(ManifestError::FieldTooLong { field, maximum })
                if field == "manualLinks[0].reason" && maximum == MANUAL_REASON_MAX_BYTES
        ));
    }

    #[test]
    fn parse_manifest_should_reject_unsafe_manual_link_metadata() {
        let input = format!(
            "{VALID}manualLinks:\n  - from: node:web\u{202e}\n    to: service:api\n    relation: consumes\n    reason: Explicit dependency\n"
        );

        let result = parse_manifest(&input);

        assert!(matches!(
            result,
            Err(ManifestError::UnsafeMetadata { field })
                if field == "manualLinks[0].from"
        ));
    }

    #[test]
    fn parse_manifest_should_reject_duplicate_manual_link_identity() {
        let declaration = "  - from: node:web\n    to: service:api\n    relation: consumes\n    reason: Explicit dependency\n";
        let input = format!("{VALID}manualLinks:\n{declaration}{declaration}");

        let result = parse_manifest(&input);

        assert!(matches!(
            result,
            Err(ManifestError::DuplicateManualLink {
                first: 0,
                duplicate: 1,
                ..
            })
        ));
    }

    #[test]
    fn parse_manifest_should_reject_literal_manual_self_link() {
        let input = format!(
            "{VALID}manualLinks:\n  - from: node:web\n    to: node:web\n    relation: consumes\n    reason: Invalid self link\n"
        );

        let result = parse_manifest(&input);

        assert!(matches!(
            result,
            Err(ManifestError::ManualSelfLink { index: 0, .. })
        ));
    }
}