interplane.Get Omega
Read in your language

aien-sovereign-core-mv

AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.

Brought by drakestapleton

branch main Branches Tags History

aien-sovereign-core-mv / crates / aien-closure / src / declare.rs

164 lines · 6229 bytes Raw Blame History
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16//! Declaration checks: what a unit says it depends on against what it uses.
17//!
18//! One meaning per code, from `aien-protocols` `specs/verified-crumb/SPEC.md`
19//! section 6.2:
20//!
21//! | Situation | Code |
22//! |---|---|
23//! | actual import or edge not declared in the dependency list | `UNDECLARED_IMPORT` |
24//! | declared dependency that is not an actual import or edge | `UNVERIFIED_DEPENDENCY` |
25//! | lock line for something the program never imports | `UNVERIFIED_DEPENDENCY` |
26//! | import, edge or manifest entry without a (correct) lock pin | `DEPENDENCY_NOT_PINNED` |
27//!
28//! The first two rows are one set comparison, [`compare`]. The same function
29//! serves SPEC step 8 (a VC's stored source imports against its
30//! `dependencies[]`), which this crate does not run today because it reads no
31//! VCs. The lock row is [`extra_needs`].
32
33use crate::error::Code;
34use std::collections::BTreeSet;
35
36/// One disagreement between the actual set and the declared set.
37#[derive(Debug, Clone, PartialEq, Eq)]
38pub struct Disagreement {
39 pub code: Code,
40 pub name: String,
41}
42
43/// Compare what a program actually uses with what its dependency list
44/// declares. Actual but undeclared is `UNDECLARED_IMPORT` (the program does
45/// more than it says). Declared but not actual is `UNVERIFIED_DEPENDENCY`
46/// (nothing shows the declaration is an edge). Sorted by code then name.
47pub fn compare(actual: &BTreeSet<String>, declared: &BTreeSet<String>) -> Vec<Disagreement> {
48 let mut out: Vec<Disagreement> = actual
49 .difference(declared)
50 .map(|n| Disagreement {
51 code: Code::UndeclaredImport,
52 name: n.clone(),
53 })
54 .collect();
55 out.extend(declared.difference(actual).map(|n| Disagreement {
56 code: Code::UnverifiedDependency,
57 name: n.clone(),
58 }));
59 out
60}
61
62fn needs_pair(line: &str) -> Option<(&str, &str)> {
63 let mut it = line.split(' ');
64 match (it.next(), it.next(), it.next()) {
65 (Some("needs"), Some(c), Some(d)) => Some((c, d)),
66 _ => None,
67 }
68}
69
70/// `needs <component> <dep> <id>` lines of the lock on disk whose
71/// (component, dep) pair is not in the rebuilt lock: pins for something the
72/// component does not depend on. A line with a known pair but another id is
73/// not extra; it is a wrong pin and stays `DEPENDENCY_NOT_PINNED`.
74pub fn extra_needs(have: &str, want: &str) -> Vec<(String, String)> {
75 let wanted: BTreeSet<(&str, &str)> = want.lines().filter_map(needs_pair).collect();
76 let mut out: Vec<(String, String)> = have
77 .lines()
78 .filter_map(needs_pair)
79 .filter(|p| !wanted.contains(p))
80 .map(|(c, d)| (c.to_string(), d.to_string()))
81 .collect();
82 out.sort();
83 out.dedup();
84 out
85}
86
87/// The lock text with every extra `needs` line removed, so what remains can
88/// be compared with the rebuilt lock for the pin rows.
89pub fn without_extra_needs(have: &str, want: &str) -> String {
90 let wanted: BTreeSet<(&str, &str)> = want.lines().filter_map(needs_pair).collect();
91 let mut s = String::new();
92 for line in have.lines() {
93 if needs_pair(line).is_some_and(|p| !wanted.contains(&p)) {
94 continue;
95 }
96 s.push_str(line);
97 s.push('\n');
98 }
99 s
100}
101
102#[cfg(test)]
103mod tests {
104 use super::*;
105
106 fn set(items: &[&str]) -> BTreeSet<String> {
107 items.iter().map(|s| s.to_string()).collect()
108 }
109
110 fn codes(d: &[Disagreement]) -> Vec<(Code, &str)> {
111 d.iter().map(|x| (x.code, x.name.as_str())).collect()
112 }
113
114 #[test]
115 fn agreement_has_no_findings() {
116 assert!(compare(&set(&["a", "b"]), &set(&["a", "b"])).is_empty());
117 assert!(compare(&set(&[]), &set(&[])).is_empty());
118 }
119
120 #[test]
121 fn row_actual_not_declared_is_undeclared_import() {
122 let d = compare(&set(&["a", "b"]), &set(&["a"]));
123 assert_eq!(codes(&d), vec![(Code::UndeclaredImport, "b")]);
124 // Negative: it is not the extra-declaration code.
125 assert!(d.iter().all(|x| x.code != Code::UnverifiedDependency));
126 }
127
128 #[test]
129 fn row_declared_not_actual_is_unverified_dependency() {
130 let d = compare(&set(&["a"]), &set(&["a", "z"]));
131 assert_eq!(codes(&d), vec![(Code::UnverifiedDependency, "z")]);
132 assert!(d.iter().all(|x| x.code != Code::UndeclaredImport));
133 }
134
135 #[test]
136 fn step8_stored_source_imports_against_dependencies() {
137 // SPEC step 8: the VC's stored source imports are `actual`, the
138 // `dependencies[]` ids are `declared`. Both directions at once.
139 let d = compare(&set(&["id1", "id2"]), &set(&["id2", "id3"]));
140 assert_eq!(
141 codes(&d),
142 vec![
143 (Code::UndeclaredImport, "id1"),
144 (Code::UnverifiedDependency, "id3")
145 ]
146 );
147 }
148
149 #[test]
150 fn extra_needs_finds_only_unknown_pairs() {
151 let want = "closure-lock v1\ncomponent a r s\nneeds a b ID1\n";
152 let same = want.to_string();
153 assert!(extra_needs(&same, want).is_empty());
154 // Known pair, other id: a wrong pin, not an extra one.
155 let wrong = "closure-lock v1\ncomponent a r s\nneeds a b OTHER\n";
156 assert!(extra_needs(wrong, want).is_empty());
157 // Unknown pair: extra.
158 let extra = format!("{want}needs a z ID9\n");
159 assert_eq!(extra_needs(&extra, want), vec![("a".into(), "z".into())]);
160 assert_eq!(without_extra_needs(&extra, want), want);
161 // Missing line is not extra.
162 assert!(extra_needs("closure-lock v1\n", want).is_empty());
163 }
164}