aristo-core 0.5.1

Aristo SDK core: shared types, .aristo/index.toml schema, B5b verification, language registry.
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
//! [`MockCanonClient`] — fixture-driven canon client for tests.
//!
//! Reads canned responses from a fixture directory and returns them
//! verbatim. Used by trycmd scenarios and integration tests to
//! exercise the SDK end-to-end without a real server.
//!
//! ## Fixture layout
//!
//! Each fixture file is TOML and corresponds to one endpoint response.
//! The mock client looks up files by a deterministic naming scheme
//! against the request shape:
//!
//! ```text
//! <fixture_dir>/
//!   match.toml                        ← canned POST /canon/match response
//!   entry/<canon_id>/<version>.toml   ← canned GET /canon/entry/<id>?version=<v>
//!   entry/<canon_id>/active.toml      ← canned response when version unspecified
//!   request-verify.toml               ← canned POST /canon/request-verify response
//! ```
//!
//! Multi-fixture scenarios (one stamp + several canon shows) point
//! `ARISTO_CANON_FIXTURE` at the same directory; each endpoint's
//! response is independent.
//!
//! ## When the fixture file is missing
//!
//! The mock returns [`CanonError::Fixture`] with the expected path —
//! tests fail loudly with a "you forgot to add the fixture" message
//! rather than silently no-op-ing.
//!
//! ## Test-only scope
//!
//! This client is shipped in the library (not `#[cfg(test)]`) so
//! integration tests in `aristo-cli` can construct it. Production
//! callers must never wire this up; the `MockCanonClient::from_env`
//! constructor reads `ARISTO_CANON_FIXTURE` to make it deliberately
//! awkward for accidental production use.

use std::fs;
use std::path::{Path, PathBuf};

use super::client::{CanonClient, CanonError};
use super::types::{
    CanonCatalogue, CanonEntry, CanonMatchRequest, CanonMatchResponse, RequestVerifyBody,
    RequestVerifyResponse,
};

/// Fixture-backed canon client.
///
/// Reads canned TOML responses from `fixture_dir` per the layout
/// documented at the module level. Construction is via
/// [`MockCanonClient::new`] (explicit path) or
/// [`MockCanonClient::from_env`] (`ARISTO_CANON_FIXTURE` env var).
#[derive(Debug, Clone)]
pub struct MockCanonClient {
    fixture_dir: PathBuf,
}

impl MockCanonClient {
    /// Construct a mock client reading fixtures from `fixture_dir`.
    pub fn new(fixture_dir: PathBuf) -> Self {
        Self { fixture_dir }
    }

    /// Construct a mock client from the `ARISTO_CANON_FIXTURE` env
    /// var, or `None` if it's unset. Production callers must never
    /// reach this path — env-var-gating makes accidental use loud.
    pub fn from_env() -> Option<Self> {
        let dir = std::env::var("ARISTO_CANON_FIXTURE").ok()?;
        Some(Self::new(PathBuf::from(dir)))
    }

    /// Read and deserialize a fixture file. Returns
    /// [`CanonError::Fixture`] if the file is missing or unparseable.
    fn load<T: for<'de> serde::Deserialize<'de>>(&self, rel: &Path) -> Result<T, CanonError> {
        let path = self.fixture_dir.join(rel);
        let raw = fs::read_to_string(&path)
            .map_err(|e| CanonError::Fixture(format!("read fixture {}: {e}", path.display())))?;
        toml::from_str(&raw)
            .map_err(|e| CanonError::Fixture(format!("parse fixture {}: {e}", path.display())))
    }
}

impl CanonClient for MockCanonClient {
    fn match_annotations(
        &self,
        _req: &CanonMatchRequest,
    ) -> Result<CanonMatchResponse, CanonError> {
        // Single canned response per fixture dir. Real-world fixtures
        // typically construct one match response covering the
        // annotations the test exercises; the SDK's response handling
        // doesn't care that the same response gets returned for
        // different requests because trycmd scenarios run one stamp
        // call per fixture dir.
        self.load(Path::new("match.toml"))
    }

    fn get_entry(&self, canon_id: &str, version: Option<&str>) -> Result<CanonEntry, CanonError> {
        let file_stem = version.unwrap_or("active");
        let rel = PathBuf::from("entry")
            .join(canon_id)
            .join(format!("{file_stem}.toml"));
        self.load(&rel)
    }

    fn request_verify(
        &self,
        _body: &RequestVerifyBody,
    ) -> Result<RequestVerifyResponse, CanonError> {
        self.load(Path::new("request-verify.toml"))
    }

    fn catalogue(&self) -> Result<CanonCatalogue, CanonError> {
        self.load(Path::new("catalogue.toml"))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::canon::types::{AnnotationMatchInput, CanonMatch, PrefixTier, VerificationMetadata};
    use tempfile::TempDir;

    fn write_match_fixture(dir: &Path, body: &str) {
        fs::write(dir.join("match.toml"), body).unwrap();
    }

    fn write_entry_fixture(dir: &Path, canon_id: &str, version: &str, body: &str) {
        let entry_dir = dir.join("entry").join(canon_id);
        fs::create_dir_all(&entry_dir).unwrap();
        fs::write(entry_dir.join(format!("{version}.toml")), body).unwrap();
    }

    #[test]
    fn match_annotations_loads_handwritten_fixture() {
        // Hand-written TOML to assert that the documented fixture
        // shape (what scenario authors will write by hand) actually
        // deserializes. The round-trip test below covers the
        // symmetrical Rust-serialize-then-load case.
        //
        // TOML's `results = Vec<Vec<CanonMatch>>` shape is expressed
        // via array-of-inline-arrays-of-inline-tables — TOML doesn't
        // have a direct nested-array-of-tables block syntax, so
        // each match candidate is one long inline table on a line.
        // Real-world fixtures (under tests/fixtures/canon/) typically
        // have one or two matches, so this stays manageable.
        let tmp = TempDir::new().unwrap();
        let fixture = r#"
effective_scopes = [":vanilla"]
canon_version = "v0.2.0"
matched_at = "2026-06-15T09:14:22Z"

results = [
    [
        { canon_id = "cell_written_exactly_once_per_page_edit", version = "v0.2.1", canonical_text = "edit_page writes each cell exactly once", confidence = 0.92, scope = ":vanilla", prefix_tier = "aristos:", backed_by = "specialized neural checker", linked = "arta_a1b2c3d4", verification = { coverage_level = "tight", test_binaries = ["monotonicity_property"] } }
    ]
]
"#;
        write_match_fixture(tmp.path(), fixture);

        let client = MockCanonClient::new(tmp.path().to_path_buf());
        let req = CanonMatchRequest {
            annotations: vec![AnnotationMatchInput {
                annotation_text: "test".into(),
                applies_to: vec!["fn".into()],
            }],
            confidence_threshold: 0.85,
            include_suggestions: false,
        };
        let resp = client.match_annotations(&req).unwrap();
        assert_eq!(resp.results.len(), 1);
        assert_eq!(resp.results[0].len(), 1);
        let m: &CanonMatch = &resp.results[0][0];
        assert_eq!(m.canon_id, "cell_written_exactly_once_per_page_edit");
        assert_eq!(m.version, "v0.2.1");
        assert_eq!(m.prefix_tier, PrefixTier::Aristos);
        assert_eq!(m.backed_by.as_deref(), Some("specialized neural checker"));
        assert_eq!(m.scope, ":vanilla");
        assert_eq!(resp.effective_scopes, vec![":vanilla".to_string()]);
        assert_eq!(resp.canon_version, "v0.2.0");
    }

    #[test]
    fn match_annotations_missing_fixture_surfaces_fixture_error() {
        let tmp = TempDir::new().unwrap();
        let client = MockCanonClient::new(tmp.path().to_path_buf());
        let req = CanonMatchRequest {
            annotations: vec![],
            confidence_threshold: 0.5,
            include_suggestions: false,
        };
        let err = client.match_annotations(&req).unwrap_err();
        assert!(matches!(err, CanonError::Fixture(_)));
        // Message should include the path so test failures point at
        // the missing file.
        assert!(err.to_string().contains("match.toml"), "got: {err}");
    }

    #[test]
    fn match_annotations_unparseable_fixture_surfaces_fixture_error() {
        let tmp = TempDir::new().unwrap();
        write_match_fixture(tmp.path(), "this is not valid TOML at all = =");
        let client = MockCanonClient::new(tmp.path().to_path_buf());
        let req = CanonMatchRequest {
            annotations: vec![],
            confidence_threshold: 0.5,
            include_suggestions: false,
        };
        let err = client.match_annotations(&req).unwrap_err();
        assert!(matches!(err, CanonError::Fixture(_)));
    }

    #[test]
    fn get_entry_with_version_loads_versioned_fixture() {
        let tmp = TempDir::new().unwrap();
        let body = r#"
canon_id = "foo"
version = "v0.2.1"
active_version = "v0.2.1"
is_deprecated = false
canon_version = "v0.2.0"
canonical_text = "foo"
applies_to = ["fn"]
category = "invariants"
property_type = "safety"
description = ""
invariant_sketch = ""
examples = []
effective_scopes = [":vanilla"]

[backed_by]
":vanilla" = "specialized neural checker"

[prefix_tier_by_scope]
":vanilla" = "aristos:"

[references]
"#;
        write_entry_fixture(tmp.path(), "foo", "v0.2.1", body);
        let client = MockCanonClient::new(tmp.path().to_path_buf());
        let entry = client.get_entry("foo", Some("v0.2.1")).unwrap();
        assert_eq!(entry.canon_id, "foo");
        assert_eq!(entry.version, "v0.2.1");
        assert_eq!(entry.effective_scopes, vec![":vanilla".to_string()]);
        assert_eq!(
            entry.backed_by.get(":vanilla").and_then(|v| v.as_deref()),
            Some("specialized neural checker")
        );
        assert_eq!(
            entry.prefix_tier_by_scope.get(":vanilla"),
            Some(&crate::canon::PrefixTier::Aristos)
        );
    }

    #[test]
    fn get_entry_without_version_loads_active_fixture() {
        let tmp = TempDir::new().unwrap();
        let body = r#"
canon_id = "foo"
version = "v0.2.1"
active_version = "v0.2.1"
is_deprecated = false
canon_version = "v0.2.0"
canonical_text = "foo"
applies_to = ["fn"]
category = "invariants"
property_type = "safety"
description = ""
invariant_sketch = ""
examples = []
effective_scopes = [":vanilla"]

[backed_by]
":vanilla" = ""

[prefix_tier_by_scope]
":vanilla" = "kanon:"

[references]
"#;
        // Note: TOML has no null; the empty string here decodes via
        // serde as Some("") not None. To represent a true kanon: tier
        // (backed_by[scope] = null) the wire is JSON-only — mock_client
        // fixtures use the prefix_tier_by_scope as the source of truth
        // for the test below.
        write_entry_fixture(tmp.path(), "foo", "active", body);
        let client = MockCanonClient::new(tmp.path().to_path_buf());
        let entry = client.get_entry("foo", None).unwrap();
        assert_eq!(entry.canon_id, "foo");
        // kanon: tier signaled via prefix_tier_by_scope; backed_by may
        // be empty-string in the fixture (TOML limitation).
        assert_eq!(
            entry.prefix_tier_by_scope.get(":vanilla"),
            Some(&crate::canon::PrefixTier::Kanon)
        );
    }

    #[test]
    fn get_entry_missing_fixture_includes_canon_id_in_error() {
        let tmp = TempDir::new().unwrap();
        let client = MockCanonClient::new(tmp.path().to_path_buf());
        let err = client.get_entry("missing_id", Some("v0.1.0")).unwrap_err();
        assert!(matches!(err, CanonError::Fixture(_)));
        let msg = err.to_string();
        assert!(msg.contains("missing_id"), "got: {msg}");
        assert!(msg.contains("v0.1.0"), "got: {msg}");
    }

    #[test]
    fn request_verify_loads_fixture() {
        let tmp = TempDir::new().unwrap();
        let body = r#"
status = "submitted"
canon_id = "foo"
current_backing = "specialized neural checker"
"#;
        fs::write(tmp.path().join("request-verify.toml"), body).unwrap();
        let client = MockCanonClient::new(tmp.path().to_path_buf());
        let resp = client
            .request_verify(&RequestVerifyBody {
                canon_id: "foo".into(),
                notes: None,
            })
            .unwrap();
        assert_eq!(resp.status, "submitted");
        assert_eq!(resp.canon_id, "foo");
        assert_eq!(
            resp.current_backing.as_deref(),
            Some("specialized neural checker")
        );
    }

    #[test]
    fn catalogue_loads_fixture() {
        use crate::canon::{CanonCatalogue, CanonCatalogueEntry};
        let mut backed = std::collections::BTreeMap::new();
        backed.insert(
            ":vanilla".to_string(),
            "specialized neural checker".to_string(),
        );
        let cat = CanonCatalogue {
            notice: vec![],
            entries: vec![
                CanonCatalogueEntry {
                    canon_id: "foo".into(),
                    version: "v0.2.1".into(),
                    canonical_text: "foo text".into(),
                    category: "invariants".into(),
                    applies_to: vec!["fn".into()],
                    backed_by: backed,
                    coverage_level: "tight".into(),
                    spec_refs: vec!["S-001".into()],
                },
                CanonCatalogueEntry {
                    canon_id: "bar".into(),
                    version: "v0.1.0".into(),
                    canonical_text: "bar text".into(),
                    category: "invariants".into(),
                    applies_to: vec!["fn".into()],
                    backed_by: std::collections::BTreeMap::new(),
                    coverage_level: "none".into(),
                    spec_refs: vec![],
                },
            ],
        };
        let tmp = TempDir::new().unwrap();
        fs::write(
            tmp.path().join("catalogue.toml"),
            toml::to_string(&cat).unwrap(),
        )
        .unwrap();
        let client = MockCanonClient::new(tmp.path().to_path_buf());
        let loaded = client.catalogue().unwrap();
        assert_eq!(loaded, cat);
        assert_eq!(loaded.entries[0].tier_label(), "aristos");
        assert_eq!(loaded.entries[1].tier_label(), "kanon");
    }

    #[test]
    fn from_env_returns_none_when_var_unset() {
        // Don't actually unset / re-set ARISTO_CANON_FIXTURE here —
        // env-var manipulation in tests races with parallel tests
        // that might set their own. Instead assert that, in the
        // absence of the env var (which is the normal test
        // environment), from_env returns None.
        if std::env::var("ARISTO_CANON_FIXTURE").is_err() {
            assert!(MockCanonClient::from_env().is_none());
        }
    }

    #[test]
    fn mock_client_is_object_safe() {
        let tmp = TempDir::new().unwrap();
        let _boxed: Box<dyn CanonClient> = Box::new(MockCanonClient::new(tmp.path().to_path_buf()));
    }

    #[test]
    fn match_response_round_trips_through_toml() {
        // Build a known response, write it as TOML, read back via
        // mock client. Asserts the wire shape we serialize is the
        // same shape MockCanonClient deserializes.
        let resp = CanonMatchResponse {
            results: vec![vec![CanonMatch {
                canon_id: "foo".into(),
                version: "v0.1.0".into(),
                canonical_text: "foo text".into(),
                confidence: 0.9,
                scope: ":vanilla".into(),
                prefix_tier: PrefixTier::Kanon,
                backed_by: None,
                linked: Some("arta_xyz1".into()),
                verification: VerificationMetadata {
                    coverage_level: "none".into(),
                    test_binaries: vec![],
                    instrumentation: None,
                },
            }]],
            effective_scopes: vec![":vanilla".into()],
            canon_version: "v0.2.0".into(),
            matched_at: "2026-06-15T09:14:22Z".into(),
            suggestions: None,
        };
        let tmp = TempDir::new().unwrap();
        let toml_text = toml::to_string(&resp).unwrap();
        fs::write(tmp.path().join("match.toml"), toml_text).unwrap();

        let client = MockCanonClient::new(tmp.path().to_path_buf());
        let req = CanonMatchRequest {
            annotations: vec![],
            confidence_threshold: 0.5,
            include_suggestions: false,
        };
        let loaded = client.match_annotations(&req).unwrap();
        assert_eq!(loaded, resp);
    }
}