xchecker 1.2.0

Spec pipeline with receipts and gateable JSON contracts
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
//! Contracts documentation verification tests
//!
//! Tests that verify CONTRACTS.md:
//! - Accurately describes JCS emission
//! - Documents array sorting rules
//! - Describes deprecation policy
//! - Lists correct schema files
//!
//! Requirements: R5

use std::fs;
use std::path::Path;

#[cfg(test)]
mod tests {
    use super::*;
    use chrono::Utc;
    use serde_json;
    use std::collections::{BTreeMap, HashMap};
    use xchecker::doctor::{CheckStatus, DoctorCheck, DoctorOutput};
    use xchecker::receipt::ReceiptManager;
    use xchecker::status::status::StatusManager;
    use xchecker::types::{FileHash, PacketEvidence, PhaseId, StatusOutput};

    /// Test that CONTRACTS.md accurately describes JCS emission
    ///
    /// This test verifies:
    /// 1. CONTRACTS.md mentions JCS (RFC 8785)
    /// 2. CONTRACTS.md describes canonical emission
    /// 3. Actual JSON outputs use JCS canonicalization
    /// 4. JCS produces byte-identical output regardless of field insertion order
    #[test]
    fn test_jcs_documentation() {
        // 1. Parse CONTRACTS.md and verify it mentions JCS
        let contracts_path = Path::new("docs/reference/CONTRACTS.md");
        assert!(
            contracts_path.exists(),
            "CONTRACTS.md should exist at docs/reference/CONTRACTS.md"
        );

        let contracts_content =
            fs::read_to_string(contracts_path).expect("Failed to read CONTRACTS.md");

        // Verify JCS is documented
        assert!(
            contracts_content.contains("JCS") || contracts_content.contains("RFC 8785"),
            "CONTRACTS.md should mention JCS or RFC 8785"
        );

        // Verify canonical emission is described
        assert!(
            contracts_content.contains("canonical") || contracts_content.contains("Canonical"),
            "CONTRACTS.md should describe canonical emission"
        );

        // Verify it mentions deterministic key ordering
        assert!(
            contracts_content.contains("deterministic") || contracts_content.contains("sorted"),
            "CONTRACTS.md should mention deterministic key ordering or sorting"
        );

        // Verify it mentions the canonicalization backend
        assert!(
            contracts_content.contains("jcs-rfc8785")
                || contracts_content.contains("canonicalization_backend"),
            "CONTRACTS.md should mention the jcs-rfc8785 backend"
        );

        // 2. Verify existing JCS byte-identity tests pass by running them
        // Test Receipt JCS emission
        test_receipt_jcs_byte_identity();

        // Test StatusOutput JCS emission
        test_status_jcs_byte_identity();

        // Test DoctorOutput JCS emission
        test_doctor_jcs_byte_identity();
    }

    /// Verify Receipt uses JCS and produces byte-identical output
    fn test_receipt_jcs_byte_identity() {
        let _temp_dir = xchecker::paths::with_isolated_home();
        let base_path = xchecker::paths::xchecker_home()
            .join("specs")
            .join("test-jcs-receipt");
        let manager = ReceiptManager::new(&base_path);

        // Create two receipts with fields in different insertion orders
        let outputs1 = vec![
            FileHash {
                path: "artifacts/10-design.md".to_string(),
                blake3_canonicalized: "fedcba9876543210".to_string(),
            },
            FileHash {
                path: "artifacts/00-requirements.md".to_string(),
                blake3_canonicalized: "0123456789abcdef".to_string(),
            },
        ];

        let outputs2 = vec![
            FileHash {
                path: "artifacts/00-requirements.md".to_string(),
                blake3_canonicalized: "0123456789abcdef".to_string(),
            },
            FileHash {
                path: "artifacts/10-design.md".to_string(),
                blake3_canonicalized: "fedcba9876543210".to_string(),
            },
        ];

        let packet = PacketEvidence {
            files: vec![],
            max_bytes: 65536,
            max_lines: 1200,
        };

        let fixed_timestamp = chrono::DateTime::parse_from_rfc3339("2025-01-01T00:00:00Z")
            .unwrap()
            .with_timezone(&Utc);

        // Create receipt 1
        let mut receipt1 = manager.create_receipt(
            "test-jcs",
            PhaseId::Requirements,
            0,
            outputs1,
            "0.1.0",
            "0.8.1",
            "haiku",
            None,
            HashMap::new(),
            packet.clone(),
            None,
            None,
            vec![],
            Some(false),
            "native",
            None,
            None,
            None,
            None,
            None, // pipeline
        );
        receipt1.emitted_at = fixed_timestamp;

        // Create receipt 2 with different insertion order
        let mut receipt2 = manager.create_receipt(
            "test-jcs",
            PhaseId::Requirements,
            0,
            outputs2,
            "0.1.0",
            "0.8.1",
            "haiku",
            None,
            HashMap::new(),
            packet,
            None,
            None,
            vec![],
            Some(false),
            "native",
            None,
            None,
            None,
            None,
            None, // pipeline
        );
        receipt2.emitted_at = fixed_timestamp;

        // Serialize both using JCS
        let json1_value = serde_json::to_value(&receipt1).unwrap();
        let json1_bytes = serde_json_canonicalizer::to_vec(&json1_value).unwrap();
        let json1 = String::from_utf8(json1_bytes).unwrap();

        let json2_value = serde_json::to_value(&receipt2).unwrap();
        let json2_bytes = serde_json_canonicalizer::to_vec(&json2_value).unwrap();
        let json2 = String::from_utf8(json2_bytes).unwrap();

        // Verify byte-identical output
        assert_eq!(
            json1, json2,
            "JCS should produce byte-identical output regardless of field insertion order"
        );

        // Verify the JSON is compact (no extra whitespace)
        assert!(
            !json1.contains("  "),
            "JCS output should not contain extra whitespace"
        );
        assert!(
            !json1.contains('\n'),
            "JCS output should not contain newlines"
        );

        // Verify canonicalization_backend field is present and correct
        assert!(
            json1.contains("\"canonicalization_backend\":\"jcs-rfc8785\""),
            "Receipt should document JCS backend"
        );
    }

    /// Verify `StatusOutput` uses JCS and produces byte-identical output
    fn test_status_jcs_byte_identity() {
        let fixed_timestamp = chrono::DateTime::parse_from_rfc3339("2025-01-01T00:00:00Z")
            .unwrap()
            .with_timezone(&Utc);

        // Create two status outputs with different field insertion orders
        let mut config1 = BTreeMap::new();
        config1.insert(
            "model".to_string(),
            xchecker::types::ConfigValue {
                value: serde_json::Value::String("haiku".to_string()),
                source: xchecker::types::ConfigSource::Default,
            },
        );
        config1.insert(
            "max_turns".to_string(),
            xchecker::types::ConfigValue {
                value: serde_json::Value::Number(6.into()),
                source: xchecker::types::ConfigSource::Config,
            },
        );

        let mut config2 = BTreeMap::new();
        config2.insert(
            "max_turns".to_string(),
            xchecker::types::ConfigValue {
                value: serde_json::Value::Number(6.into()),
                source: xchecker::types::ConfigSource::Config,
            },
        );
        config2.insert(
            "model".to_string(),
            xchecker::types::ConfigValue {
                value: serde_json::Value::String("haiku".to_string()),
                source: xchecker::types::ConfigSource::Default,
            },
        );

        let status1 = StatusOutput {
            schema_version: "1".to_string(),
            emitted_at: fixed_timestamp,
            runner: "native".to_string(),
            runner_distro: None,
            fallback_used: false,
            canonicalization_version: "yaml-v1,md-v1".to_string(),
            canonicalization_backend: "jcs-rfc8785".to_string(),
            artifacts: vec![],
            last_receipt_path: "receipts/requirements-20250101_000000.json".to_string(),
            effective_config: config1,
            lock_drift: None,
            pending_fixups: None,
        };

        let status2 = StatusOutput {
            schema_version: "1".to_string(),
            emitted_at: fixed_timestamp,
            runner: "native".to_string(),
            runner_distro: None,
            fallback_used: false,
            canonicalization_version: "yaml-v1,md-v1".to_string(),
            canonicalization_backend: "jcs-rfc8785".to_string(),
            artifacts: vec![],
            last_receipt_path: "receipts/requirements-20250101_000000.json".to_string(),
            effective_config: config2,
            lock_drift: None,
            pending_fixups: None,
        };

        // Serialize using StatusManager's emit_json (which uses JCS)
        let json1 = StatusManager::emit_json(&status1).unwrap();
        let json2 = StatusManager::emit_json(&status2).unwrap();

        // Verify byte-identical output
        assert_eq!(
            json1, json2,
            "JCS should produce byte-identical output for StatusOutput"
        );

        // Verify canonicalization_backend field is present
        assert!(
            json1.contains("\"canonicalization_backend\":\"jcs-rfc8785\""),
            "StatusOutput should document JCS backend"
        );
    }

    /// Verify `DoctorOutput` uses JCS and produces byte-identical output
    fn test_doctor_jcs_byte_identity() {
        let fixed_timestamp = chrono::DateTime::parse_from_rfc3339("2025-01-01T00:00:00Z")
            .unwrap()
            .with_timezone(&Utc);

        // Create two doctor outputs with checks in different insertion orders
        let checks1 = vec![
            DoctorCheck {
                name: "zebra_check".to_string(),
                status: CheckStatus::Pass,
                details: "test".to_string(),
            },
            DoctorCheck {
                name: "alpha_check".to_string(),
                status: CheckStatus::Pass,
                details: "test".to_string(),
            },
        ];

        let checks2 = vec![
            DoctorCheck {
                name: "alpha_check".to_string(),
                status: CheckStatus::Pass,
                details: "test".to_string(),
            },
            DoctorCheck {
                name: "zebra_check".to_string(),
                status: CheckStatus::Pass,
                details: "test".to_string(),
            },
        ];

        let mut output1 = DoctorOutput {
            schema_version: "1".to_string(),
            emitted_at: fixed_timestamp,
            ok: true,
            checks: checks1,
            cache_stats: None,
        };

        let mut output2 = DoctorOutput {
            schema_version: "1".to_string(),
            emitted_at: fixed_timestamp,
            ok: true,
            checks: checks2,
            cache_stats: None,
        };

        // Sort both (as the actual implementation does)
        output1.checks.sort_by(|a, b| a.name.cmp(&b.name));
        output2.checks.sort_by(|a, b| a.name.cmp(&b.name));

        // Serialize using JCS
        let json1_value = serde_json::to_value(&output1).unwrap();
        let json1_bytes = serde_json_canonicalizer::to_vec(&json1_value).unwrap();
        let json1 = String::from_utf8(json1_bytes).unwrap();

        let json2_value = serde_json::to_value(&output2).unwrap();
        let json2_bytes = serde_json_canonicalizer::to_vec(&json2_value).unwrap();
        let json2 = String::from_utf8(json2_bytes).unwrap();

        // Verify byte-identical output
        assert_eq!(
            json1, json2,
            "JCS should produce byte-identical output for DoctorOutput"
        );
    }

    /// Test that CONTRACTS.md accurately describes array sorting rules
    ///
    /// This test verifies:
    /// 1. CONTRACTS.md documents array sorting rules
    /// 2. Documentation matches implementation (outputs by path, artifacts by path, checks by name)
    /// 3. Existing tests enforce sorting (from operational-polish spec)
    /// 4. Byte-identical JCS test exists and passes (catches field reordering)
    #[test]
    fn test_array_sorting_documentation() {
        // 1. Parse CONTRACTS.md and verify it documents array sorting
        let contracts_path = Path::new("docs/reference/CONTRACTS.md");
        assert!(
            contracts_path.exists(),
            "CONTRACTS.md should exist at docs/reference/CONTRACTS.md"
        );

        let contracts_content =
            fs::read_to_string(contracts_path).expect("Failed to read CONTRACTS.md");

        // Verify array sorting is documented
        assert!(
            contracts_content.contains("Array Ordering")
                || contracts_content.contains("array") && contracts_content.contains("sorted"),
            "CONTRACTS.md should document array sorting/ordering"
        );

        // 2. Verify documentation matches implementation
        // Check that CONTRACTS.md mentions the specific sorting rules

        // Receipts: outputs sorted by path
        assert!(
            contracts_content.contains("outputs") && contracts_content.contains("path"),
            "CONTRACTS.md should document that receipt outputs are sorted by path"
        );

        // Status: artifacts sorted by path
        assert!(
            contracts_content.contains("artifacts") && contracts_content.contains("path"),
            "CONTRACTS.md should document that status artifacts are sorted by path"
        );

        // Doctor: checks sorted by name
        assert!(
            contracts_content.contains("checks") && contracts_content.contains("name"),
            "CONTRACTS.md should document that doctor checks are sorted by name"
        );

        // Verify the documentation mentions deterministic output
        assert!(
            contracts_content.contains("deterministic") || contracts_content.contains("stable"),
            "CONTRACTS.md should mention deterministic/stable output from array sorting"
        );

        // 3. Verify existing tests enforce sorting
        // These tests should already exist from the operational-polish spec

        // Run the existing array sorting tests to verify they pass
        test_receipt_outputs_sorted();
        test_status_artifacts_sorted();
        test_doctor_checks_sorted();

        // 4. Verify byte-identical JCS test exists and passes
        // This was already verified in test_jcs_documentation, but we'll confirm again
        test_receipt_jcs_byte_identity();
        test_status_jcs_byte_identity();
        test_doctor_jcs_byte_identity();

        println!("✓ CONTRACTS.md accurately documents array sorting rules");
        println!(
            "✓ Documentation matches implementation (outputs by path, artifacts by path, checks by name)"
        );
        println!("✓ Existing tests enforce sorting");
        println!("✓ Byte-identical JCS tests pass");
    }

    /// Verify Receipt outputs are sorted by path
    fn test_receipt_outputs_sorted() {
        let _temp_dir = xchecker::paths::with_isolated_home();
        let base_path = xchecker::paths::xchecker_home()
            .join("specs")
            .join("test-sorting");
        let manager = ReceiptManager::new(&base_path);

        // Create outputs in non-alphabetical order
        let outputs = vec![
            FileHash {
                path: "artifacts/20-tasks.md".to_string(),
                blake3_canonicalized: "cccccccccccccccc".to_string(),
            },
            FileHash {
                path: "artifacts/00-requirements.md".to_string(),
                blake3_canonicalized: "aaaaaaaaaaaaaaaa".to_string(),
            },
            FileHash {
                path: "artifacts/10-design.md".to_string(),
                blake3_canonicalized: "bbbbbbbbbbbbbbbb".to_string(),
            },
        ];

        let packet = PacketEvidence {
            files: vec![],
            max_bytes: 65536,
            max_lines: 1200,
        };

        let receipt = manager.create_receipt(
            "test-sorting",
            PhaseId::Requirements,
            0,
            outputs,
            "0.1.0",
            "0.8.1",
            "haiku",
            None,
            HashMap::new(),
            packet,
            None,
            None,
            vec![],
            Some(false),
            "native",
            None,
            None,
            None,
            None,
            None, // pipeline
        );

        // Verify outputs are sorted by path
        assert_eq!(receipt.outputs.len(), 3);
        assert_eq!(receipt.outputs[0].path, "artifacts/00-requirements.md");
        assert_eq!(receipt.outputs[1].path, "artifacts/10-design.md");
        assert_eq!(receipt.outputs[2].path, "artifacts/20-tasks.md");
    }

    /// Verify Status artifacts are sorted by path
    fn test_status_artifacts_sorted() {
        use xchecker::types::ArtifactInfo;

        // Create artifacts in non-alphabetical order
        let mut artifacts = [
            ArtifactInfo {
                path: "artifacts/20-tasks.md".to_string(),
                blake3_first8: "cccccccc".to_string(),
            },
            ArtifactInfo {
                path: "artifacts/00-requirements.md".to_string(),
                blake3_first8: "aaaaaaaa".to_string(),
            },
            ArtifactInfo {
                path: "artifacts/10-design.md".to_string(),
                blake3_first8: "bbbbbbbb".to_string(),
            },
        ];

        // Sort as the implementation does
        artifacts.sort_by(|a, b| a.path.cmp(&b.path));

        // Verify sorted order
        assert_eq!(artifacts[0].path, "artifacts/00-requirements.md");
        assert_eq!(artifacts[1].path, "artifacts/10-design.md");
        assert_eq!(artifacts[2].path, "artifacts/20-tasks.md");
    }

    /// Verify Doctor checks are sorted by name
    fn test_doctor_checks_sorted() {
        // Create checks in non-alphabetical order
        let mut checks = [
            DoctorCheck {
                name: "zebra_check".to_string(),
                status: CheckStatus::Pass,
                details: "test".to_string(),
            },
            DoctorCheck {
                name: "alpha_check".to_string(),
                status: CheckStatus::Pass,
                details: "test".to_string(),
            },
            DoctorCheck {
                name: "middle_check".to_string(),
                status: CheckStatus::Pass,
                details: "test".to_string(),
            },
        ];

        // Sort as the implementation does
        checks.sort_by(|a, b| a.name.cmp(&b.name));

        // Verify sorted order
        assert_eq!(checks[0].name, "alpha_check");
        assert_eq!(checks[1].name, "middle_check");
        assert_eq!(checks[2].name, "zebra_check");
    }

    /// Test that CONTRACTS.md accurately describes deprecation policy
    ///
    /// This test verifies:
    /// 1. CONTRACTS.md documents the deprecation policy
    /// 2. Policy matches implementation approach
    /// 3. Schema files exist as documented
    #[test]
    fn test_deprecation_policy() {
        // 1. Parse CONTRACTS.md and verify it documents deprecation policy
        let contracts_path = Path::new("docs/reference/CONTRACTS.md");
        assert!(
            contracts_path.exists(),
            "CONTRACTS.md should exist at docs/reference/CONTRACTS.md"
        );

        let contracts_content =
            fs::read_to_string(contracts_path).expect("Failed to read CONTRACTS.md");

        // Verify deprecation policy is documented
        assert!(
            contracts_content.contains("Deprecation") || contracts_content.contains("deprecation"),
            "CONTRACTS.md should document deprecation policy"
        );

        // Verify it mentions version lifecycle
        assert!(
            contracts_content.contains("lifecycle") || contracts_content.contains("Lifecycle"),
            "CONTRACTS.md should describe schema version lifecycle"
        );

        // Verify it mentions dual support during transition
        assert!(
            contracts_content.contains("dual")
                || contracts_content.contains("both") && contracts_content.contains("supported"),
            "CONTRACTS.md should mention dual version support during transition"
        );

        // Verify it mentions a specific deprecation period (e.g., 6 months)
        assert!(
            contracts_content.contains("6 months") || contracts_content.contains("month"),
            "CONTRACTS.md should specify deprecation period duration"
        );

        // Verify it describes breaking vs additive changes
        assert!(
            contracts_content.contains("Breaking") || contracts_content.contains("breaking"),
            "CONTRACTS.md should describe breaking changes"
        );

        assert!(
            contracts_content.contains("Additive")
                || contracts_content.contains("additive")
                || contracts_content.contains("optional"),
            "CONTRACTS.md should describe additive changes"
        );

        // Verify it mentions version bumps for breaking changes
        assert!(
            contracts_content.contains("version bump")
                || contracts_content.contains("incrementing"),
            "CONTRACTS.md should mention version bumps for breaking changes"
        );

        // 2. Verify policy matches implementation approach
        // Check that the documented policy aligns with how schemas are versioned

        // Verify it mentions schema_version field
        assert!(
            contracts_content.contains("schema_version"),
            "CONTRACTS.md should mention schema_version field"
        );

        // Verify it describes forward compatibility (consumers ignore unknown fields)
        assert!(
            contracts_content.contains("ignore unknown")
                || contracts_content.contains("additionalProperties"),
            "CONTRACTS.md should describe forward compatibility via ignoring unknown fields"
        );

        // Verify it describes backward compatibility (producers can add fields)
        assert!(
            contracts_content.contains("backward") || contracts_content.contains("Backward"),
            "CONTRACTS.md should describe backward compatibility"
        );

        // 3. Verify schema files exist as documented
        let schema_files = vec![
            "schemas/receipt.v1.json",
            "schemas/status.v1.json",
            "schemas/doctor.v1.json",
        ];

        for schema_file in schema_files {
            let schema_path = Path::new(schema_file);
            assert!(
                schema_path.exists(),
                "Schema file {schema_file} should exist as documented in CONTRACTS.md"
            );

            // Verify the schema file is mentioned in CONTRACTS.md
            let schema_name = schema_path.file_name().unwrap().to_str().unwrap();
            assert!(
                contracts_content.contains(schema_name),
                "CONTRACTS.md should mention schema file {schema_name}"
            );
        }

        // Verify example files are mentioned
        assert!(
            contracts_content.contains("docs/schemas/") || contracts_content.contains("example"),
            "CONTRACTS.md should mention example payload files"
        );

        println!("✓ CONTRACTS.md documents deprecation policy");
        println!(
            "✓ Policy matches implementation approach (schema_version, forward/backward compatibility)"
        );
        println!("✓ All documented schema files exist");
    }
}