claw-vcs 0.1.1

Claw VCS: intent-native, agent-native version control.
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
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
use clap::Args;

use std::collections::{HashMap, HashSet, VecDeque};

use claw_core::id::ChangeId;
use claw_core::id::ObjectId;
use claw_core::object::Object;
use claw_core::types::{Capsule, Policy, Revision};
use claw_crypto::capsule::verify_capsule;
use claw_merge::emit::merge;
use claw_patch::CodecRegistry;
use claw_policy::{evaluator::evaluate_policy, PolicyContext};
use claw_store::{ClawStore, HeadState};

use super::agent::AgentRegistration;
use crate::config::find_repo_root;
use crate::conflict_writer;
use crate::merge_state::{self, ConflictEntry, MergeInfo, MergeState};
use crate::worktree;

#[derive(Args)]
pub struct IntegrateArgs {
    /// Left ref (default: HEAD's branch)
    #[arg(long)]
    left: Option<String>,
    /// Right ref to integrate
    #[arg(long)]
    right: String,
    /// Author name
    #[arg(short, long, default_value = "anonymous")]
    author: String,
    /// Merge message
    #[arg(short, long, default_value = "Integrate changes")]
    message: String,
    /// Preview policy checks and merge result without updating refs or worktree
    #[arg(long)]
    dry_run: bool,
}

#[derive(Debug, Default)]
struct AgentRegistry {
    by_public_key: HashMap<String, String>,
    by_agent_id: HashMap<String, String>,
}

#[derive(Debug, Default)]
struct ProvenanceContext {
    signer_agent_ids: Vec<String>,
    signer_key_ids: Vec<String>,
    trust_score: Option<f32>,
}

pub fn run(args: IntegrateArgs) -> anyhow::Result<()> {
    let root = find_repo_root()?;
    let store = ClawStore::open(&root)?;
    let registry = CodecRegistry::default();

    // Resolve left ref: default to HEAD's branch
    let left_ref = match args.left {
        Some(r) => r,
        None => {
            let head = store.read_head()?;
            match head {
                HeadState::Symbolic { ref_name } => ref_name,
                HeadState::Detached { .. } => {
                    anyhow::bail!("cannot integrate in detached HEAD state; use --left to specify")
                }
            }
        }
    };

    let left_id = store
        .get_ref(&left_ref)?
        .ok_or_else(|| anyhow::anyhow!("ref not found: {}", left_ref))?;
    let right_id = resolve_revision_ref_or_id(&store, &args.right)?;

    enforce_integration_policies(&store, &left_id, &right_id)?;

    let result = merge(
        &store,
        &registry,
        &left_id,
        &right_id,
        &args.author,
        &args.message,
    )?;

    if result.conflicts.is_empty() {
        if args.dry_run {
            println!("Dry run: integration can be applied cleanly.");
            println!("  Left ref: {left_ref} ({left_id})");
            println!("  Right: {} ({right_id})", args.right);
            if let Some(tree_id) = result.revision.tree {
                println!("  Result tree: {tree_id}");
            }
            println!("  Ref update skipped.");
            println!("  Worktree update skipped.");
            return Ok(());
        }

        // Clean merge: store revision, materialize tree, advance ref
        let rev_id = store.store_object(&Object::Revision(result.revision))?;
        store.update_ref_cas(
            &left_ref,
            Some(&left_id),
            &rev_id,
            &args.author,
            &args.message,
        )?;

        // Materialize merged tree
        if let Some(tree_id) = store.load_object(&rev_id)?.as_revision_tree() {
            worktree::materialize_tree(&store, &tree_id, &root)?;
        }

        println!("Integrated successfully: {rev_id}");
    } else {
        if args.dry_run {
            println!(
                "Dry run: merge would have {} conflict(s).",
                result.conflicts.len()
            );
            for c in &result.conflicts {
                println!("  CONFLICT: {} ({})", c.file_path, c.codec_id);
            }
            println!("  Conflict artifacts skipped.");
            println!("  Merge state write skipped.");
            return Ok(());
        }

        // Conflicted merge: write conflict artifacts, MERGE_STATE.toml, do NOT advance ref
        let mut conflict_entries = Vec::new();

        for conflict in &result.conflicts {
            let base_content =
                load_file_from_revision(&store, &result.ancestor, &conflict.file_path);
            let left_content = load_file_from_revision(&store, &left_id, &conflict.file_path);
            let right_content = load_file_from_revision(&store, &right_id, &conflict.file_path);

            let conflict_id = claw_core::id::ConflictId::new().to_string();

            match conflict.codec_id.as_str() {
                "json/tree" => {
                    conflict_writer::write_json_conflict(
                        &root,
                        &conflict.file_path,
                        &base_content,
                        &left_content,
                        &right_content,
                    )?;
                }
                "binary" => {
                    conflict_writer::write_binary_conflict(
                        &root,
                        &conflict.file_path,
                        &base_content,
                        &left_content,
                        &right_content,
                    )?;
                }
                _ => {
                    conflict_writer::write_text_conflict(
                        &root,
                        &conflict.file_path,
                        &base_content,
                        &left_content,
                        &right_content,
                        &left_ref,
                        &args.right,
                    )?;
                }
            }

            conflict_entries.push(ConflictEntry {
                file_path: conflict.file_path.clone(),
                conflict_id,
                codec_id: conflict.codec_id.clone(),
            });
        }

        // Write MERGE_STATE.toml
        let merge_state = MergeState {
            merge: MergeInfo {
                left_ref: left_ref.clone(),
                right_ref: args.right.clone(),
                left_revision: left_id.to_hex(),
                right_revision: right_id.to_hex(),
                base_revision: result.ancestor.to_hex(),
            },
            conflicts: conflict_entries,
        };
        merge_state::write_to(&store.layout().claw_dir(), &merge_state)?;

        // Also materialize non-conflicting changes from the merged tree
        // Use the left tree as the base for the working copy
        let left_obj = store.load_object(&left_id)?;
        if let Object::Revision(ref rev) = left_obj {
            if let Some(ref tree_id) = rev.tree {
                worktree::materialize_tree(&store, tree_id, &root)?;
            }
        }

        println!(
            "Merge has {} conflict(s). Resolve them and run `claw snapshot` to complete.",
            result.conflicts.len()
        );
        for c in &result.conflicts {
            println!("  CONFLICT: {} ({})", c.file_path, c.codec_id);
        }
    }

    Ok(())
}

fn resolve_revision_ref_or_id(store: &ClawStore, value: &str) -> anyhow::Result<ObjectId> {
    if let Some(id) = store.get_ref(value)? {
        return Ok(id);
    }

    if let Ok(id) = ObjectId::from_hex(value) {
        return ensure_revision_id(store, id, value);
    }

    if let Ok(id) = ObjectId::from_display(value) {
        return ensure_revision_id(store, id, value);
    }

    anyhow::bail!("ref not found: {value}")
}

fn ensure_revision_id(store: &ClawStore, id: ObjectId, source: &str) -> anyhow::Result<ObjectId> {
    match store.load_object(&id)? {
        Object::Revision(_) => Ok(id),
        _ => anyhow::bail!("not a revision: {source}"),
    }
}

fn enforce_integration_policies(
    store: &ClawStore,
    left_id: &ObjectId,
    right_id: &ObjectId,
) -> anyhow::Result<()> {
    let applicable = collect_applicable_revisions(store, left_id, right_id)?;
    let agent_registry = load_agent_registry(store)?;

    for rev_id in applicable {
        let rev = load_revision(store, &rev_id)?;
        let policies = load_policies_for_revision(store, &rev)?;
        if policies.is_empty() {
            continue;
        }

        let capsule = load_capsule_for_revision(store, &rev_id, &rev)?.ok_or_else(|| {
            anyhow::anyhow!(
                "policy-gated revision {} has no capsule evidence",
                rev_id.to_hex()
            )
        })?;
        let provenance = verify_capsule_provenance(&capsule, &rev_id, &agent_registry)?;
        let touched_paths = collect_touched_paths(store, &rev)?;
        let context = PolicyContext {
            revision_id: Some(rev_id),
            signer_agent_ids: provenance.signer_agent_ids,
            signer_key_ids: provenance.signer_key_ids,
            touched_paths,
            trust_score: provenance.trust_score,
            now_ms: Some(current_time_ms()),
        };

        for policy in policies {
            evaluate_policy(&policy, &rev, &capsule, &context).map_err(|err| {
                anyhow::anyhow!(
                    "policy '{}' failed for revision {}: {}",
                    policy.policy_id,
                    rev_id.to_hex(),
                    err
                )
            })?;
        }
    }

    Ok(())
}

fn current_time_ms() -> u64 {
    std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|duration| duration.as_millis().min(u128::from(u64::MAX)) as u64)
        .unwrap_or_default()
}

fn verify_capsule_provenance(
    capsule: &Capsule,
    rev_id: &ObjectId,
    registry: &AgentRegistry,
) -> anyhow::Result<ProvenanceContext> {
    if capsule.revision_id != *rev_id {
        anyhow::bail!(
            "capsule revision mismatch: capsule={}, revision={}",
            capsule.revision_id.to_hex(),
            rev_id.to_hex()
        );
    }

    if capsule.signatures.is_empty() {
        anyhow::bail!("capsule has no signatures");
    }

    let mut valid_signers = HashSet::new();
    let mut malformed_signers = 0usize;

    for sig in &capsule.signatures {
        let normalized_signer = sig.signer_id.trim().to_ascii_lowercase();
        let signer_bytes = match hex::decode(&normalized_signer) {
            Ok(bytes) => bytes,
            Err(_) => {
                malformed_signers += 1;
                continue;
            }
        };
        let signer_key: [u8; 32] = match signer_bytes.as_slice().try_into() {
            Ok(key) => key,
            Err(_) => {
                malformed_signers += 1;
                continue;
            }
        };

        let mut candidate = capsule.clone();
        candidate.signatures = vec![sig.clone()];
        if matches!(verify_capsule(&candidate, &signer_key), Ok(true)) {
            valid_signers.insert(normalized_signer);
        }
    }

    if valid_signers.is_empty() {
        if malformed_signers > 0 {
            anyhow::bail!(
                "capsule signatures failed verification ({} malformed signer id(s))",
                malformed_signers
            );
        }
        anyhow::bail!("capsule signatures failed verification");
    }

    let agent_id = capsule.public_fields.agent_id.trim();
    if agent_id.is_empty() {
        anyhow::bail!("capsule public agent_id is empty");
    }
    let expected_key = registry
        .by_agent_id
        .get(agent_id)
        .ok_or_else(|| anyhow::anyhow!("capsule agent '{}' is not registered", agent_id))?;
    if !valid_signers.contains(expected_key) {
        anyhow::bail!(
            "capsule signer does not match registered key for agent '{}'",
            agent_id
        );
    }

    let mut signer_key_ids: Vec<String> = valid_signers.into_iter().collect();
    signer_key_ids.sort();

    let mut signer_agent_ids: Vec<String> = signer_key_ids
        .iter()
        .filter_map(|signer| registry.by_public_key.get(signer).cloned())
        .collect();
    signer_agent_ids.sort();
    signer_agent_ids.dedup();

    Ok(ProvenanceContext {
        signer_agent_ids,
        signer_key_ids,
        trust_score: derive_capsule_trust_score(capsule),
    })
}

fn derive_capsule_trust_score(capsule: &Capsule) -> Option<f32> {
    let total = capsule.public_fields.evidence.len();
    if total == 0 {
        return None;
    }

    let passed = capsule
        .public_fields
        .evidence
        .iter()
        .filter(|e| e.status.eq_ignore_ascii_case("pass"))
        .count();

    Some(passed as f32 / total as f32)
}

fn load_agent_registry(store: &ClawStore) -> anyhow::Result<AgentRegistry> {
    let mut registry = AgentRegistry::default();
    for (_name, obj_id) in store.list_refs("agents")? {
        let obj = store.load_object(&obj_id)?;
        let Object::Blob(blob) = obj else {
            continue;
        };
        let Ok(record) = serde_json::from_slice::<AgentRegistration>(&blob.data) else {
            continue;
        };
        if record.is_revoked() {
            continue;
        }
        let normalized_key = match normalize_public_key_hex(&record.public_key) {
            Some(key) => key,
            None => continue,
        };
        registry
            .by_agent_id
            .insert(record.agent_id.clone(), normalized_key.clone());
        registry
            .by_public_key
            .insert(normalized_key, record.agent_id);
    }
    Ok(registry)
}

fn normalize_public_key_hex(value: &str) -> Option<String> {
    let normalized = value.trim().to_ascii_lowercase();
    let bytes = hex::decode(&normalized).ok()?;
    if bytes.len() != 32 {
        return None;
    }
    Some(normalized)
}

fn collect_touched_paths(store: &ClawStore, rev: &Revision) -> anyhow::Result<Vec<String>> {
    let mut paths = Vec::new();
    let mut seen = HashSet::new();

    for patch_id in &rev.patches {
        let patch_obj = store.load_object(patch_id)?;
        let patch = match patch_obj {
            Object::Patch(patch) => patch,
            _ => anyhow::bail!(
                "revision patch reference does not point to a patch object: {}",
                patch_id.to_hex()
            ),
        };
        let path = patch.target_path.trim();
        if path.is_empty() {
            continue;
        }
        if seen.insert(path.to_string()) {
            paths.push(path.to_string());
        }
    }

    Ok(paths)
}

fn collect_applicable_revisions(
    store: &ClawStore,
    left_id: &ObjectId,
    right_id: &ObjectId,
) -> anyhow::Result<Vec<ObjectId>> {
    let left_reachable = collect_reachable_revisions(store, left_id)?;
    let mut out = Vec::new();
    let mut seen = HashSet::new();
    let mut queue = VecDeque::from([*right_id]);

    while let Some(id) = queue.pop_front() {
        if !seen.insert(id) {
            continue;
        }
        if left_reachable.contains(&id) {
            continue;
        }

        let rev = load_revision(store, &id)?;
        out.push(id);
        for parent in rev.parents {
            queue.push_back(parent);
        }
    }

    Ok(out)
}

fn collect_reachable_revisions(
    store: &ClawStore,
    start: &ObjectId,
) -> anyhow::Result<HashSet<ObjectId>> {
    let mut reachable = HashSet::new();
    let mut queue = VecDeque::from([*start]);

    while let Some(id) = queue.pop_front() {
        if !reachable.insert(id) {
            continue;
        }
        let rev = load_revision(store, &id)?;
        for parent in rev.parents {
            queue.push_back(parent);
        }
    }

    Ok(reachable)
}

fn load_revision(store: &ClawStore, id: &ObjectId) -> anyhow::Result<Revision> {
    let obj = store.load_object(id)?;
    match obj {
        Object::Revision(rev) => Ok(rev),
        _ => anyhow::bail!("object is not a revision: {}", id.to_hex()),
    }
}

fn load_policies_for_revision(store: &ClawStore, rev: &Revision) -> anyhow::Result<Vec<Policy>> {
    let change_id = match rev.change_id {
        Some(id) => id,
        None => return Ok(vec![]),
    };

    let intent = load_intent_for_change(store, &change_id)?;
    let mut policies = Vec::new();
    let mut seen = HashSet::new();

    for policy_ref in &intent.policy_refs {
        let policy = load_policy_ref(store, policy_ref)?;
        if seen.insert(policy.policy_id.clone()) {
            policies.push(policy);
        }
    }

    Ok(policies)
}

fn load_intent_for_change(
    store: &ClawStore,
    change_id: &ChangeId,
) -> anyhow::Result<claw_core::types::Intent> {
    let change_obj_id = store
        .get_ref(&format!("changes/{change_id}"))?
        .ok_or_else(|| anyhow::anyhow!("change not found: {}", change_id))?;
    let change_obj = store.load_object(&change_obj_id)?;
    let change = match change_obj {
        Object::Change(c) => c,
        _ => anyhow::bail!(
            "change ref does not point to a change object: {}",
            change_id
        ),
    };

    let intent_obj_id = store
        .get_ref(&format!("intents/{}", change.intent_id))?
        .ok_or_else(|| anyhow::anyhow!("intent not found for change: {}", change_id))?;
    let intent_obj = store.load_object(&intent_obj_id)?;
    match intent_obj {
        Object::Intent(intent) => Ok(intent),
        _ => anyhow::bail!(
            "intent ref does not point to an intent object: {}",
            change.intent_id
        ),
    }
}

fn load_policy_ref(store: &ClawStore, policy_ref: &str) -> anyhow::Result<Policy> {
    let ref_name = if store.get_ref(policy_ref)?.is_some() {
        policy_ref.to_string()
    } else {
        format!("policies/{policy_ref}")
    };

    let policy_obj_id = store
        .get_ref(&ref_name)?
        .ok_or_else(|| anyhow::anyhow!("policy ref not found: {}", ref_name))?;
    let policy_obj = store.load_object(&policy_obj_id)?;
    match policy_obj {
        Object::Policy(policy) => Ok(policy),
        _ => anyhow::bail!("ref does not point to a policy object: {}", ref_name),
    }
}

fn load_capsule_for_revision(
    store: &ClawStore,
    rev_id: &ObjectId,
    rev: &Revision,
) -> anyhow::Result<Option<Capsule>> {
    let capsule_id = if let Some(id) = rev.capsule_id {
        Some(id)
    } else {
        let full = rev_id.to_hex();
        store
            .get_ref(&format!("capsules/by-revision/{full}"))?
            .or_else(|| {
                let prefix = &full[..16];
                store
                    .get_ref(&format!("capsules/by-revision/{prefix}"))
                    .ok()
                    .flatten()
            })
    };

    let Some(capsule_id) = capsule_id else {
        return Ok(None);
    };
    let capsule_obj = store.load_object(&capsule_id)?;
    match capsule_obj {
        Object::Capsule(capsule) => Ok(Some(capsule)),
        _ => anyhow::bail!(
            "capsule mapping points to non-capsule object for revision {}",
            rev_id.to_hex()
        ),
    }
}

fn load_file_from_revision(store: &ClawStore, rev_id: &ObjectId, path: &str) -> Vec<u8> {
    let obj = match store.load_object(rev_id) {
        Ok(o) => o,
        Err(_) => return vec![],
    };
    let tree_id = match obj {
        Object::Revision(ref rev) => match rev.tree {
            Some(t) => t,
            None => return vec![],
        },
        _ => return vec![],
    };
    find_blob_in_tree(store, &tree_id, path).unwrap_or_default()
}

fn find_blob_in_tree(store: &ClawStore, tree_id: &ObjectId, path: &str) -> Option<Vec<u8>> {
    let parts: Vec<&str> = path.split('/').collect();
    find_blob_recursive(store, tree_id, &parts)
}

fn find_blob_recursive(store: &ClawStore, tree_id: &ObjectId, parts: &[&str]) -> Option<Vec<u8>> {
    if parts.is_empty() {
        return None;
    }
    let obj = store.load_object(tree_id).ok()?;
    let tree = match obj {
        Object::Tree(t) => t,
        _ => return None,
    };
    for entry in &tree.entries {
        if entry.name == parts[0] {
            if parts.len() == 1 {
                let blob_obj = store.load_object(&entry.object_id).ok()?;
                if let Object::Blob(b) = blob_obj {
                    return Some(b.data);
                }
                return None;
            } else {
                return find_blob_recursive(store, &entry.object_id, &parts[1..]);
            }
        }
    }
    None
}

// Helper trait for Object
trait ObjectExt {
    fn as_revision_tree(&self) -> Option<ObjectId>;
}

impl ObjectExt for Object {
    fn as_revision_tree(&self) -> Option<ObjectId> {
        match self {
            Object::Revision(rev) => rev.tree,
            _ => None,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::{
        collect_applicable_revisions, enforce_integration_policies, load_agent_registry,
        resolve_revision_ref_or_id, verify_capsule_provenance, AgentRegistry, IntegrateArgs,
    };
    use crate::commands::agent::AgentRegistration;
    use clap::Parser;
    use claw_core::id::{ChangeId, IntentId};
    use claw_core::object::Object;
    use claw_core::types::Blob;
    use claw_core::types::{
        Change, ChangeStatus, EvidencePolicy, Intent, IntentStatus, Policy, Revision, Visibility,
    };
    use claw_crypto::capsule::build_capsule;
    use claw_crypto::keypair::KeyPair;
    use claw_store::ClawStore;

    #[derive(Parser)]
    struct TestCli {
        #[command(flatten)]
        args: IntegrateArgs,
    }

    #[test]
    fn parses_dry_run_flag() {
        let cli = TestCli::parse_from(["claw", "--right", "heads/feature", "--dry-run"]);

        assert_eq!(cli.args.right, "heads/feature");
        assert!(cli.args.dry_run);
    }

    fn test_store() -> (tempfile::TempDir, ClawStore) {
        let tmp = tempfile::tempdir().unwrap();
        let store = ClawStore::init(tmp.path()).unwrap();
        (tmp, store)
    }

    fn register_agent(store: &ClawStore, name: &str, keypair: &KeyPair) {
        let now = 1;
        let record = AgentRegistration {
            schema_version: 2,
            agent_id: name.to_string(),
            agent_version: Some("test".to_string()),
            public_key: hex::encode(keypair.public_key_bytes()),
            private_key: None,
            revoked_at_ms: None,
            revocation_reason: None,
            created_at_ms: now,
            updated_at_ms: now,
        };
        let blob = Object::Blob(Blob {
            data: serde_json::to_vec(&record).unwrap(),
            media_type: Some("application/json".to_string()),
        });
        let blob_id = store.store_object(&blob).unwrap();
        store.set_ref(&format!("agents/{name}"), &blob_id).unwrap();
    }

    #[test]
    fn agent_registry_skips_revoked_registrations() {
        let (_tmp, store) = test_store();
        let keypair = KeyPair::generate();
        let record = AgentRegistration {
            schema_version: 2,
            agent_id: "agent".to_string(),
            agent_version: Some("test".to_string()),
            public_key: hex::encode(keypair.public_key_bytes()),
            private_key: None,
            revoked_at_ms: Some(2),
            revocation_reason: Some("compromised".to_string()),
            created_at_ms: 1,
            updated_at_ms: 2,
        };
        let blob = Object::Blob(Blob {
            data: serde_json::to_vec(&record).unwrap(),
            media_type: Some("application/json".to_string()),
        });
        let blob_id = store.store_object(&blob).unwrap();
        store.set_ref("agents/agent", &blob_id).unwrap();

        let registry = load_agent_registry(&store).unwrap();
        assert!(registry.by_agent_id.is_empty());
        assert!(registry.by_public_key.is_empty());
    }

    #[test]
    fn collect_applicable_revisions_excludes_left_side() {
        let (_tmp, store) = test_store();

        let base = store
            .store_object(&Object::Revision(Revision {
                change_id: None,
                parents: vec![],
                patches: vec![],
                snapshot_base: None,
                tree: None,
                capsule_id: None,
                author: "test".to_string(),
                created_at_ms: 1,
                summary: "base".to_string(),
                policy_evidence: vec![],
            }))
            .unwrap();

        let right_only = store
            .store_object(&Object::Revision(Revision {
                change_id: None,
                parents: vec![base],
                patches: vec![],
                snapshot_base: None,
                tree: None,
                capsule_id: None,
                author: "test".to_string(),
                created_at_ms: 2,
                summary: "right".to_string(),
                policy_evidence: vec![],
            }))
            .unwrap();

        let applicable = collect_applicable_revisions(&store, &base, &right_only).unwrap();
        assert_eq!(applicable, vec![right_only]);
    }

    #[test]
    fn policy_check_blocks_integration_when_evidence_missing() {
        let (_tmp, store) = test_store();

        let intent_id = IntentId::new();
        let change_id = ChangeId::new();

        let intent_obj = Object::Intent(Intent {
            id: intent_id,
            title: "intent".to_string(),
            goal: "goal".to_string(),
            constraints: vec![],
            acceptance_tests: vec![],
            links: vec![],
            policy_refs: vec!["ci-required".to_string()],
            agents: vec![],
            change_ids: vec![],
            depends_on: vec![],
            supersedes: vec![],
            status: IntentStatus::Open,
            created_at_ms: 1,
            updated_at_ms: 1,
        });
        let intent_obj_id = store.store_object(&intent_obj).unwrap();
        store
            .set_ref(&format!("intents/{intent_id}"), &intent_obj_id)
            .unwrap();

        let change_obj = Object::Change(Change {
            id: change_id,
            intent_id,
            head_revision: None,
            workstream_id: None,
            status: ChangeStatus::Open,
            created_at_ms: 1,
            updated_at_ms: 1,
        });
        let change_obj_id = store.store_object(&change_obj).unwrap();
        store
            .set_ref(&format!("changes/{change_id}"), &change_obj_id)
            .unwrap();

        let policy_obj = Object::Policy(Policy {
            policy_id: "ci-required".to_string(),
            required_checks: vec!["ci/test".to_string()],
            required_reviewers: vec![],
            sensitive_paths: vec![],
            quarantine_lane: false,
            min_trust_score: None,
            visibility: Visibility::Public,
            authorized_recipients: vec![],
            revoked_recipients: vec![],
            evidence_policy: EvidencePolicy::default(),
        });
        let policy_obj_id = store.store_object(&policy_obj).unwrap();
        store
            .set_ref("policies/ci-required", &policy_obj_id)
            .unwrap();

        let left = store
            .store_object(&Object::Revision(Revision {
                change_id: None,
                parents: vec![],
                patches: vec![],
                snapshot_base: None,
                tree: None,
                capsule_id: None,
                author: "test".to_string(),
                created_at_ms: 2,
                summary: "left".to_string(),
                policy_evidence: vec![],
            }))
            .unwrap();

        let right = store
            .store_object(&Object::Revision(Revision {
                change_id: Some(change_id),
                parents: vec![left],
                patches: vec![],
                snapshot_base: None,
                tree: None,
                capsule_id: None,
                author: "test".to_string(),
                created_at_ms: 3,
                summary: "right".to_string(),
                policy_evidence: vec![],
            }))
            .unwrap();

        let keypair = KeyPair::generate();
        register_agent(&store, "agent", &keypair);
        let capsule_obj = Object::Capsule(
            build_capsule(
                &right,
                claw_core::types::CapsulePublic {
                    agent_id: "agent".to_string(),
                    agent_version: None,
                    toolchain_digest: None,
                    env_fingerprint: None,
                    evidence: vec![],
                },
                None,
                None,
                &keypair,
            )
            .unwrap(),
        );
        let capsule_id = store.store_object(&capsule_obj).unwrap();
        store
            .set_ref(
                &format!("capsules/by-revision/{}", &right.to_hex()[..16]),
                &capsule_id,
            )
            .unwrap();

        let err = enforce_integration_policies(&store, &left, &right).unwrap_err();
        assert!(
            err.to_string().contains("missing required check"),
            "unexpected error: {err}"
        );
    }

    #[test]
    fn provenance_check_rejects_revision_mismatch() {
        let keypair = KeyPair::generate();
        let revision_a = claw_core::hash::content_hash(claw_core::object::TypeTag::Revision, b"a");
        let revision_b = claw_core::hash::content_hash(claw_core::object::TypeTag::Revision, b"b");

        let capsule = build_capsule(
            &revision_a,
            claw_core::types::CapsulePublic {
                agent_id: "agent".to_string(),
                agent_version: None,
                toolchain_digest: None,
                env_fingerprint: None,
                evidence: vec![],
            },
            None,
            None,
            &keypair,
        )
        .unwrap();

        let err = verify_capsule_provenance(&capsule, &revision_b, &AgentRegistry::default())
            .unwrap_err();
        assert!(err.to_string().contains("capsule revision mismatch"));
    }

    #[test]
    fn provenance_check_rejects_unregistered_agent() {
        let keypair = KeyPair::generate();
        let revision = claw_core::hash::content_hash(claw_core::object::TypeTag::Revision, b"rev");

        let capsule = build_capsule(
            &revision,
            claw_core::types::CapsulePublic {
                agent_id: "unknown".to_string(),
                agent_version: None,
                toolchain_digest: None,
                env_fingerprint: None,
                evidence: vec![],
            },
            None,
            None,
            &keypair,
        )
        .unwrap();

        let err =
            verify_capsule_provenance(&capsule, &revision, &AgentRegistry::default()).unwrap_err();
        assert!(err.to_string().contains("is not registered"));
    }

    #[test]
    fn resolve_revision_ref_or_id_accepts_display_id() {
        let (_tmp, store) = test_store();
        let rev_id = store
            .store_object(&Object::Revision(Revision {
                change_id: None,
                parents: vec![],
                patches: vec![],
                snapshot_base: None,
                tree: None,
                capsule_id: None,
                author: "test".to_string(),
                created_at_ms: 1,
                summary: "rev".to_string(),
                policy_evidence: vec![],
            }))
            .unwrap();

        let resolved = resolve_revision_ref_or_id(&store, &rev_id.to_string()).unwrap();
        assert_eq!(resolved, rev_id);
    }

    #[test]
    fn resolve_revision_ref_or_id_rejects_non_revision_object() {
        let (_tmp, store) = test_store();
        let blob_id = store
            .store_object(&Object::Blob(Blob {
                data: b"blob".to_vec(),
                media_type: None,
            }))
            .unwrap();

        let err = resolve_revision_ref_or_id(&store, &blob_id.to_string()).unwrap_err();
        assert!(err.to_string().contains("not a revision"));
    }
}