diff --git a/Cargo.lock b/Cargo.lock index 1528668..f71b829 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1738,6 +1738,7 @@ dependencies = [ "predicates", "quire-qualify", "quire-rs", + "semver", "serde", "serde_json", "sha2", diff --git a/Cargo.toml b/Cargo.toml index b7e32d7..34d4ef6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,6 +28,7 @@ anyhow = "1" glob = "0.3" toml = "1" url = "2" +semver = "1" tempfile = "3" time = { version = "0.3", features = ["formatting"] } # FR-034: pure-Rust git object-store readers. The `gix` facade is not used diff --git a/spec/functional/FR-030-relations-subcommand.md b/spec/functional/FR-030-relations-subcommand.md index f3a6b9f..b2ac4ec 100644 --- a/spec/functional/FR-030-relations-subcommand.md +++ b/spec/functional/FR-030-relations-subcommand.md @@ -51,6 +51,10 @@ finding names the member identity. `--repo` together with `--scope`, or with the FR-017's explicit roots and their order. For `relations` only, the CLI also loads the installed providers named by each explicit module's `semantic.imports`, matching both the provider's `semantic.package` and top-level manifest `version` exactly; it follows those imports transitively. +It also loads each explicit module's declared top-level `depends_on` modules, matching the +manifest module `name` and `version_range`, and follows those dependencies transitively. A declared +dependency with no installed matching module is refused with the requiring module, dependency name, +and a hint to add `--module ` or install the dependency. It searches the installed module roots (`IX_FILAMENT_MODULES_PATH`, `IX_SCHEMA_PATH`, then the default install root), adds only exact matches after the explicit roots, and leaves missing-provider diagnostics to quire-rs. Unrelated installed modules do not load, and the CLI adds no vocabulary or @@ -133,6 +137,7 @@ with nothing on stdout. | FR-030-AC-27 | With only a security module explicit, whose `semantic.imports` pins process `0.3.0`, architecture `0.5.0` and ISO `0.3.0`, `quire relations` loads only the installed providers matching those package/version pairs and retains the security module's relations using each provider's edge vocabulary; unrelated installed modules are absent, and no `semantic.import-unresolved` diagnostic is emitted. | Test | | FR-030-AC-28 | With one `required_relations` declaration dropped for an undeclared `record_keys` kind beside a valid sibling, default and `--design` JSON preserve the engine's `RelationReport.errors` objects unchanged and the complete valid report; default and `--design` human output print each error before entries exactly as `error: dropped relation from module : `. Default, `--design`, `--flow` and `--health-data-flows` retain their complete valid report (with the latter two preserving their own stdout schema), write one such error line per dropped relation to stderr, and exit 1 regardless of `--strict`; a dropped non-`required_relations` declaration instead exits 3 with nothing on stdout. | Test | | FR-030-AC-29 | A fixture requirement covered only by targets that the declaration's `to_where` selector rejects prints each rejected counterpart beneath its row as `to_where rejected: has `, quoting a string value and printing `absent` for a missing field, in human output; `--json` carries them as `to_rejected` ([FR-084](ix://agent-ix/quire-rs/FR-084)). | Test | +| FR-030-AC-30 | With only a module explicit whose top-level `depends_on` names installed modules by name and version range, `quire relations` loads matching dependencies transitively and exposes their relation vocabularies; unrelated and out-of-range modules are absent. If a dependency has no matching installed module, it exits 3 before reporting unknown archetypes or verbs and names the requiring module, missing dependency, version range, and `--module ` remedy. | Test | ## Dependencies diff --git a/src/commands/mod.rs b/src/commands/mod.rs index f84a4e0..bf33c16 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -69,6 +69,7 @@ use std::path::{Path, PathBuf}; use anyhow::Context; use ix_cli_kit::streams::Diagnostics; +use ix_trace_rs::implements; use quire_cli::io::emit_quire_diagnostics; use quire_rs::Registry; @@ -186,14 +187,14 @@ pub fn load_module_set_registry(ctx: &Ctx, modules: &[String]) -> anyhow::Result load_module_set_roots(ctx, &roots) } -/// Load explicit relation modules and only the installed semantic providers -/// their imports declare, preserving the explicit roots first. +/// Load explicit relation modules and their declared installed providers, +/// preserving the explicit roots first. pub fn load_relation_module_set_registry( ctx: &Ctx, modules: &[String], ) -> anyhow::Result { let mut roots = validate_module_roots(modules)?; - add_semantic_import_providers(&mut roots); + add_declared_module_providers(&mut roots).map_err(failure::invalid)?; load_module_set_roots(ctx, &roots) } @@ -224,25 +225,43 @@ fn load_module_set_roots(ctx: &Ctx, roots: &[PathBuf]) -> anyhow::Result, version: String, imports: BTreeMap, + dependencies: Vec, } -/// Resolve the transitive `semantic.imports` closure from installed modules. -/// The engine must see explicit roots and providers in one load so imported -/// vocabularies participate in its ordinary merge and diagnostic behavior. -fn add_semantic_import_providers(roots: &mut Vec) { +#[derive(Debug, serde::Deserialize)] +struct DeclaredModuleDependency { + name: String, + version_range: String, +} + +/// Resolve declared `depends_on` modules and the transitive `semantic.imports` +/// closure. The engine must see explicit roots and providers in one load so +/// module vocabularies participate in its ordinary merge and diagnostics. +#[implements("FR-030-AC-30")] +fn add_declared_module_providers(roots: &mut Vec) -> anyhow::Result<()> { let mut available = BTreeSet::new(); let mut pending = BTreeSet::new(); + let mut module_dependencies = BTreeSet::new(); for root in roots.iter() { if let Some(module) = semantic_metadata(root) { - available.insert((module.package, module.version)); + if let Some(package) = module.package { + available.insert((package, module.version.clone())); + } pending.extend(module.imports); + module_dependencies.extend( + module + .dependencies + .into_iter() + .map(|dependency| (dependency.name, dependency.version_range)), + ); } } - if pending.is_empty() { - return; + if pending.is_empty() && module_dependencies.is_empty() { + return Ok(()); } let mut candidates = Vec::new(); @@ -276,7 +295,8 @@ fn add_semantic_import_providers(roots: &mut Vec) { continue; } let Some(provider) = candidates.iter().find(|module| { - (module.package.as_str(), module.version.as_str()) == (&dependency.0, &dependency.1) + module.package.as_deref() == Some(dependency.0.as_str()) + && module.version == dependency.1 }) else { continue; }; @@ -291,6 +311,50 @@ fn add_semantic_import_providers(roots: &mut Vec) { .map(|(package, version)| (package.clone(), version.clone())), ); } + + let mut resolved_modules = BTreeSet::new(); + while let Some((name, version_range)) = module_dependencies.pop_first() { + if !resolved_modules.insert((name.clone(), version_range.clone())) { + continue; + } + let requirement = semver::VersionReq::parse(&version_range).with_context(|| { + format!("invalid depends_on version range '{version_range}' for {name}") + })?; + let provider = candidates.iter().find(|module| { + module.name == name + && semver::Version::parse(&module.version) + .is_ok_and(|version| requirement.matches(&version)) + }); + let Some(provider) = provider else { + let requiring = roots + .iter() + .filter_map(|root| semantic_metadata(root)) + .find(|module| { + module.dependencies.iter().any(|dependency| { + dependency.name == name && dependency.version_range == version_range + }) + }) + .map(|module| module.name) + .unwrap_or_else(|| "an explicit module".to_owned()); + anyhow::bail!("{requiring} depends on {name} ({version_range}); add --module or install the module") + }; + if !roots.iter().any(|root| root == &provider.root) { + roots.push(provider.root.clone()); + } + module_dependencies.extend( + provider + .dependencies + .iter() + .map(|dependency| (dependency.name.clone(), dependency.version_range.clone())), + ); + pending.extend( + provider + .imports + .iter() + .map(|(package, version)| (package.clone(), version.clone())), + ); + } + Ok(()) } /// Read only the manifest identity and semantic dependency map needed to @@ -298,15 +362,31 @@ fn add_semantic_import_providers(roots: &mut Vec) { /// diagnose with its normal, complete error context. fn semantic_metadata(module_root: &Path) -> Option { let manifest = quire_rs::loader::manifest::load_manifest(module_root).ok()?; - let semantic = serde_json::to_value(manifest.semantic?).ok()?; - let package = semantic.get("package")?.as_str()?.to_owned(); - let imports: BTreeMap = - serde_json::from_value(semantic.get("imports")?.clone()).ok()?; + let semantic = manifest + .semantic + .and_then(|value| serde_json::to_value(value).ok()); + let package = semantic + .as_ref() + .and_then(|value| value.get("package")) + .and_then(serde_json::Value::as_str) + .map(str::to_owned); + let imports: BTreeMap = semantic + .as_ref() + .and_then(|value| value.get("imports")) + .and_then(|value| serde_json::from_value(value.clone()).ok()) + .unwrap_or_default(); + let dependencies = manifest + .depends_on + .and_then(|value| serde_json::to_value(value).ok()) + .and_then(|value| serde_json::from_value(value).ok()) + .unwrap_or_default(); Some(ModuleSemanticMetadata { root: module_root.to_path_buf(), + name: manifest.name?, package, version: manifest.version?, imports, + dependencies, }) } diff --git a/tests/cli_relations_imports.rs b/tests/cli_relations_imports.rs index 02837e4..92adf0d 100644 --- a/tests/cli_relations_imports.rs +++ b/tests/cli_relations_imports.rs @@ -1,4 +1,4 @@ -//! Explicit relation modules load their declared semantic import providers. +//! Explicit relation modules load declared module dependencies and semantic imports. mod common; use ix_trace_rs::trace; @@ -24,7 +24,7 @@ fn write_module(root: &std::path::Path, name: &str, manifest: &str) { fs::write(module.join("manifest.yaml"), manifest).expect("module manifest"); } -#[trace("FR-030-AC-27")] +#[trace("FR-030-AC-27", "FR-030-AC-30")] #[test] fn explicit_security_module_loads_imported_edge_vocabulary_providers() { let temp = TempDir::new().expect("temporary directory"); @@ -47,9 +47,7 @@ semantic: package: agent-ix/spec-objects-security exports: [] imports: - agent-ix/spec-artifacts-process: 0.3.0 - agent-ix/spec-objects-architecture: 0.5.0 - agent-ix/spec-artifacts-iso: 0.3.0 + example/import-provider: 1.0.0 targets: [json-schema, markdown] mappings: [] compatibility_posture: additive @@ -80,21 +78,35 @@ traceability: to: [SecurityItem] direction: outgoing check: transitive-edge + - name: uses-semantic-import-edge + from: SecurityItem + edges: [semantic_import_edge] + to: [SecurityItem] + direction: outgoing + check: semantic-import-edge "#, ) .expect("security manifest"); + fs::write( + security.join("manifest.yaml"), + format!( + "{}\ndepends_on:\n - name: spec-artifacts-process\n version_range: ^0.3.0\n - name: spec-objects-architecture\n version_range: ^0.5.0\n - name: spec-artifacts-iso\n version_range: ^0.3.0\n", + fs::read_to_string(security.join("manifest.yaml")).expect("security manifest") + ), + ) + .expect("security dependencies"); - write_module( - &installed, - "process", - &provider_manifest( + let process_manifest = format!( + "{}\ndepends_on:\n - name: process-common\n version_range: ^1.0.0\n", + provider_manifest( "spec-artifacts-process", "0.3.0", "agent-ix/spec-artifacts-process", "process_edge", - "agent-ix/process-common: 1.0.0", - ), + "", + ) ); + write_module(&installed, "process", &process_manifest); write_module( &installed, "architecture", @@ -117,10 +129,21 @@ traceability: "", ), ); + write_module( + &installed, + "semantic-import", + &provider_manifest( + "semantic-import-provider", + "1.0.0", + "example/import-provider", + "semantic_import_edge", + "", + ), + ); let mut wrong_version = provider_manifest( - "spec-artifacts-process-wrong-version", - "0.3.1", + "spec-artifacts-process", + "0.4.0", "agent-ix/spec-artifacts-process", "wrong_process_edge", "", @@ -187,14 +210,72 @@ traceability: "uses-process-edge", "uses-architecture-edge", "uses-iso-edge", - "uses-transitive-edge" + "uses-transitive-edge", + "uses-semantic-import-edge" ] ); assert!(!names.contains(&"unrelated-provider-relation")); assert!(!names.contains(&"wrong-version-provider-relation")); assert!( !String::from_utf8_lossy(&output.stderr).contains("semantic.import-unresolved"), - "all exact semantic import providers loaded: {}", + "all declared module dependency providers loaded: {}", + String::from_utf8_lossy(&output.stderr) + ); +} + +#[trace("FR-030-AC-30")] +#[test] +fn explicit_relation_module_names_a_missing_declared_dependency() { + let temp = TempDir::new().expect("temporary directory"); + let modules = temp.path().join("modules"); + let installed = temp.path().join("installed"); + let scope = temp.path().join("repo"); + fs::create_dir_all(&installed).expect("installed module root"); + fs::create_dir_all(scope.join("spec")).expect("document root"); + let parent = modules.join("parent"); + fs::create_dir_all(&parent).expect("parent module root"); + fs::write( + parent.join("manifest.yaml"), + "name: parent-module\nversion: 1.0.0\ndepends_on:\n - name: absent-provider\n version_range: ^2.1.0\nsemantic:\n package: example/parent\n imports: {}\n", + ) + .expect("parent manifest"); + write_module( + &installed, + "absent-provider-wrong-version", + &provider_manifest( + "absent-provider", + "3.0.0", + "example/absent-provider", + "irrelevant_edge", + "", + ), + ); + + let mut command = common::quire(); + let output = command + .args([ + "relations", + "--scope", + scope.to_str().expect("scope path"), + "--module", + parent.to_str().expect("parent path"), + "--json", + ]) + .env("IX_HOME", temp.path().join("ix-home")) + .env("IX_FILAMENT_MODULES_PATH", &installed) + .env_remove("IX_SCHEMA_PATH") + .output() + .expect("run quire relations"); + assert_eq!( + output.status.code(), + Some(3), + "stdout: {}\nstderr: {}", + String::from_utf8_lossy(&output.stdout), String::from_utf8_lossy(&output.stderr) ); + let stderr = String::from_utf8_lossy(&output.stderr); + assert!(stderr.contains("parent-module"), "{stderr}"); + assert!(stderr.contains("absent-provider"), "{stderr}"); + assert!(stderr.contains("^2.1.0"), "{stderr}"); + assert!(stderr.contains("--module "), "{stderr}"); }