apr-cli 0.35.0

CLI tool for APR model inspection, debugging, and operations
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
// PMAT-540 Phase 5: Tests for inspect command helper functions

#[cfg(test)]
mod inspect_tests {
    use super::*;
    use std::path::Path;

    // ========================================================================
    // validate_path
    // ========================================================================

    #[test]
    fn validate_path_nonexistent() {
        let result = validate_path(Path::new("/nonexistent/model.apr"));
        assert!(result.is_err());
        match result.unwrap_err() {
            CliError::FileNotFound(_) => {}
            e => panic!("Expected FileNotFound, got {e:?}"),
        }
    }

    #[test]
    fn validate_path_directory() {
        let dir = tempfile::tempdir().expect("create temp dir");
        let result = validate_path(dir.path());
        assert!(result.is_err());
        match result.unwrap_err() {
            CliError::NotAFile(_) => {}
            e => panic!("Expected NotAFile, got {e:?}"),
        }
    }

    #[test]
    fn validate_path_valid_file() {
        let file = tempfile::NamedTempFile::new().expect("create temp file");
        let result = validate_path(file.path());
        assert!(result.is_ok());
    }

    // ========================================================================
    // InspectResult JSON serialization
    // ========================================================================

    #[test]
    fn inspect_result_json_serialization() {
        let result = InspectResult {
            file: "model.apr".to_string(),
            valid: true,
            format: "APR v2".to_string(),
            version: "2.0".to_string(),
            tensor_count: 100,
            size_bytes: 1_000_000,
            checksum_valid: true,
            architecture: Some("llama".to_string()),
            num_layers: Some(32),
            num_heads: Some(32),
            hidden_size: Some(4096),
            vocab_size: Some(128256),
            flags: FlagsInfo {
                lz4_compressed: false,
                zstd_compressed: false,
                encrypted: false,
                signed: false,
                sharded: false,
                quantized: true,
                has_vocab: true,
            },
            metadata: MetadataInfo {
                architecture: Some("llama".to_string()),
                ..MetadataInfo::default()
            },
        };
        let json = serde_json::to_string(&result).expect("serialize");
        assert!(json.contains("model.apr"));
        assert!(json.contains("\"valid\":true"));
        assert!(json.contains("\"tensor_count\":100"));
        assert!(json.contains("\"architecture\":\"llama\""));
        assert!(json.contains("\"quantized\":true"));
    }

    #[test]
    fn inspect_result_skips_none_fields() {
        let result = InspectResult {
            file: "test.apr".to_string(),
            valid: false,
            format: "unknown".to_string(),
            version: "0".to_string(),
            tensor_count: 0,
            size_bytes: 0,
            checksum_valid: false,
            architecture: None,
            num_layers: None,
            num_heads: None,
            hidden_size: None,
            vocab_size: None,
            flags: FlagsInfo {
                lz4_compressed: false,
                zstd_compressed: false,
                encrypted: false,
                signed: false,
                sharded: false,
                quantized: false,
                has_vocab: false,
            },
            metadata: MetadataInfo::default(),
        };
        let json = serde_json::to_string(&result).expect("serialize");
        // Top-level architecture (on InspectResult) has skip_serializing_if
        // but metadata.architecture does NOT — it serializes as null
        assert!(!json.contains("\"num_layers\""), "None num_layers should be skipped");
        assert!(!json.contains("\"hidden_size\""), "None hidden_size should be skipped");
    }

    // ========================================================================
    // FlagsInfo
    // ========================================================================

    #[test]
    fn flags_info_all_false() {
        let flags = FlagsInfo {
            lz4_compressed: false,
            zstd_compressed: false,
            encrypted: false,
            signed: false,
            sharded: false,
            quantized: false,
            has_vocab: false,
        };
        let json = serde_json::to_string(&flags).expect("serialize");
        assert!(json.contains("\"lz4_compressed\":false"));
    }

    // ========================================================================
    // C-APR-PROVENANCE / AC-SHIP2-012 / FALSIFY-SHIP-022
    // ========================================================================

    /// GATE-APR-PROV-002 (JSON half) / INV-APR-PROV-002 / FM-APR-PROV-SILENT-SKIP:
    /// MetadataInfo JSON serialization MUST contain the three provenance keys
    /// with `null` value when they are None, never silently skip them via
    /// `skip_serializing_if`.
    #[test]
    fn falsify_ship_022_inspect_emits_provenance_keys() {
        let meta = MetadataInfo::default();
        let json = serde_json::to_string(&meta).expect("serialize MetadataInfo");
        let parsed: serde_json::Value = serde_json::from_str(&json).expect("parse JSON");
        let obj = parsed.as_object().expect("JSON object at top level");

        for key in ["license", "data_source", "data_license"] {
            assert!(
                obj.contains_key(key),
                "MetadataInfo JSON must emit key `{key}` even when None \
                 (no skip_serializing_if); violating this hides provenance \
                 from auditors (FM-APR-PROV-SILENT-SKIP)"
            );
            assert!(
                obj[key].is_null(),
                "key `{key}` must serialize as null when None, got {:?}",
                obj[key]
            );
        }
    }

    /// GATE-APR-PROV-002 (text half) / INV-APR-PROV-002: text rendering
    /// MUST emit each provenance key as the literal "(missing)" when the
    /// field is None, rather than silently omitting the line.
    #[test]
    fn falsify_ship_022_inspect_missing_renders_as_missing() {
        let meta = MetadataInfo::default();
        let rendered = format_provenance_block(&meta);

        assert!(
            rendered.contains("Provenance:"),
            "text output must contain a 'Provenance:' block header; got:\n{rendered}"
        );
        for key in ["license", "data_source", "data_license"] {
            assert!(
                rendered.contains(&format!("{key}: (missing)")),
                "text output must render absent `{key}` as `(missing)`; got:\n{rendered}"
            );
        }
    }

    /// GATE-APR-PROV-002 (text half, populated variant): when provenance
    /// fields are populated, text rendering MUST emit the actual values
    /// (not "(missing)").
    #[test]
    fn falsify_ship_022_inspect_populated_renders_values() {
        let meta = MetadataInfo {
            license: Some("Apache-2.0".to_string()),
            data_source: Some("teacher-only".to_string()),
            data_license: Some("Apache-2.0".to_string()),
            ..Default::default()
        };
        let rendered = format_provenance_block(&meta);

        assert!(rendered.contains("license: Apache-2.0"));
        assert!(rendered.contains("data_source: teacher-only"));
        assert!(rendered.contains("data_license: Apache-2.0"));
        assert!(
            !rendered.contains("(missing)"),
            "populated provenance must not render `(missing)`; got:\n{rendered}"
        );
    }

    // ========================================================================
    // PMAT-690 P0-K — apr inspect surfaces hf_architecture + hf_model_type
    // ========================================================================

    /// PMAT-690 P0-K: `apr inspect --json` MUST emit `hf_architecture` and
    /// `hf_model_type` keys (null when None). Operators query
    /// `apr inspect --json | jq .metadata.hf_architecture` to verify that
    /// the upstream `apr convert` stamping worked. Silently skipping the
    /// keys hides the upstream-producer defect that this contract was
    /// authored to prevent (see memory/feedback_upstream_metadata_masquerade.md).
    #[test]
    fn pmat_690_p0k_inspect_emits_hf_arch_keys_when_none() {
        let meta = MetadataInfo::default();
        let json = serde_json::to_string(&meta).expect("serialize MetadataInfo");
        let parsed: serde_json::Value = serde_json::from_str(&json).expect("parse JSON");
        let obj = parsed.as_object().expect("JSON object at top level");

        // Both keys MUST be present and null (not skipped via
        // skip_serializing_if). The contract on apr inspect is that an
        // operator can grep for `"hf_architecture"` in any output and
        // distinguish "stamped" from "missing".
        for key in ["hf_architecture", "hf_model_type"] {
            assert!(
                obj.contains_key(key),
                "MetadataInfo JSON must emit key `{key}` even when None \
                 (no skip_serializing_if). Auditing the import→pretrain→export \
                 chain requires both keys to be grep-checkable."
            );
            assert!(
                obj[key].is_null(),
                "key `{key}` must serialize as null when None, got {:?}",
                obj[key]
            );
        }
    }

    /// PMAT-690 P0-K: when hf_architecture / hf_model_type are populated,
    /// `apr inspect --json` renders the actual values (not null, not the
    /// architecture-family-lowercase string).
    #[test]
    fn pmat_690_p0k_inspect_emits_hf_arch_values_when_populated() {
        let meta = MetadataInfo {
            architecture: Some("qwen2".to_string()),
            hf_architecture: Some("Qwen2ForCausalLM".to_string()),
            hf_model_type: Some("qwen2".to_string()),
            ..Default::default()
        };
        let json = serde_json::to_string(&meta).expect("serialize MetadataInfo");
        let parsed: serde_json::Value = serde_json::from_str(&json).expect("parse JSON");
        let obj = parsed.as_object().expect("JSON object");

        assert_eq!(
            obj.get("architecture").and_then(|v| v.as_str()),
            Some("qwen2"),
            "architecture (family) must render unchanged"
        );
        assert_eq!(
            obj.get("hf_architecture").and_then(|v| v.as_str()),
            Some("Qwen2ForCausalLM"),
            "hf_architecture (HF class name) must render the canonical string"
        );
        assert_eq!(
            obj.get("hf_model_type").and_then(|v| v.as_str()),
            Some("qwen2"),
            "hf_model_type must render the config.json::model_type value"
        );
    }

    // ========================================================================
    // PMAT-690 P3-A — `apr inspect --quality` scorer (AC-SHIP2-007 ≥ 90)
    // ========================================================================

    fn mk_header(checksum_valid: bool) -> HeaderData {
        HeaderData {
            version: (2, 0),
            flags: AprV2Flags::default(),
            tensor_count: 0,
            metadata_offset: 0,
            metadata_size: 0,
            tensor_index_offset: 0,
            data_offset: 0,
            checksum_valid,
        }
    }

    /// PMAT-690 P3-A INV-001: a ship-ready model (all 5 sub-scores
    /// populated) scores ≥ 90.
    #[test]
    fn pmat_690_p3a_ship_ready_model_scores_at_least_90() {
        let meta = MetadataInfo {
            architecture: Some("qwen2".to_string()),
            hf_architecture: Some("Qwen2ForCausalLM".to_string()),
            hf_model_type: Some("qwen2".to_string()),
            hidden_size: Some(896),
            num_layers: Some(24),
            num_heads: Some(14),
            license: Some("Apache-2.0".to_string()),
            data_source: Some("teacher-only".to_string()),
            data_license: Some("Apache-2.0".to_string()),
            ..Default::default()
        };
        let mut header = mk_header(true);
        header.flags = header.flags.with(AprV2Flags::HAS_VOCAB);
        let q = compute_quality_score(&meta, &header);
        assert!(
            q.ship_ready,
            "ship-ready model must score ≥ 90 per AC-SHIP2-007 (got {})",
            q.score
        );
        assert!(q.score >= 90, "got {}", q.score);
    }

    /// PMAT-690 P3-A INV-002: a model missing both HF identity AND
    /// provenance scores well below 90 — the §81-§83 cascade scenario.
    #[test]
    fn pmat_690_p3a_no_hf_no_provenance_scores_below_55() {
        let meta = MetadataInfo {
            architecture: Some("qwen2".to_string()),
            hidden_size: Some(896),
            num_layers: Some(24),
            num_heads: Some(14),
            // No hf_architecture, no hf_model_type, no provenance.
            ..Default::default()
        };
        let mut header = mk_header(true);
        header.flags = header.flags.with(AprV2Flags::HAS_VOCAB);
        let q = compute_quality_score(&meta, &header);
        assert!(
            !q.ship_ready,
            "pre-§84 cascade model must NOT be ship-ready (got {})",
            q.score
        );
        // physics(20) + structural(20) + tokenizer(15) = 55 max without
        // provenance + hf_identity.
        assert!(
            q.score <= 55,
            "no provenance + no hf identity must cap at 55 (got {})",
            q.score
        );
    }

    /// PMAT-690 P3-A INV-003: invalid checksum drops the physics
    /// sub-score to 0 and pulls the total below 90.
    #[test]
    fn pmat_690_p3a_invalid_checksum_blocks_ship() {
        let meta = MetadataInfo {
            architecture: Some("qwen2".to_string()),
            hf_architecture: Some("Qwen2ForCausalLM".to_string()),
            hf_model_type: Some("qwen2".to_string()),
            hidden_size: Some(896),
            num_layers: Some(24),
            num_heads: Some(14),
            license: Some("Apache-2.0".to_string()),
            data_source: Some("teacher-only".to_string()),
            data_license: Some("Apache-2.0".to_string()),
            ..Default::default()
        };
        let mut header = mk_header(false); // ← invalid
        header.flags = header.flags.with(AprV2Flags::HAS_VOCAB);
        let q = compute_quality_score(&meta, &header);
        assert_eq!(
            q.physics, 0,
            "invalid checksum must zero physics sub-score"
        );
        assert!(
            !q.ship_ready,
            "model with invalid checksum must NOT be ship-ready (got score {})",
            q.score
        );
    }

    /// PMAT-690 P3-A INV-004: QualityReport JSON contains the
    /// breakdown fields operators need to debug a sub-90 score.
    #[test]
    fn pmat_690_p3a_quality_json_emits_breakdown() {
        let meta = MetadataInfo::default();
        let header = mk_header(true);
        let q = compute_quality_score(&meta, &header);
        let json = q.to_json();
        let obj = json.as_object().expect("JSON object");
        assert!(obj.contains_key("score"));
        assert!(obj.contains_key("ship_ready"));
        assert!(obj.contains_key("threshold"));
        assert_eq!(obj["threshold"].as_i64(), Some(90));
        let breakdown = obj
            .get("breakdown")
            .and_then(|v| v.as_object())
            .expect("breakdown");
        for key in [
            "physics",
            "structural",
            "provenance",
            "hf_identity",
            "tokenizer",
        ] {
            assert!(
                breakdown.contains_key(key),
                "breakdown MUST include `{key}` so operators can debug a sub-90 score"
            );
        }
    }
}