nap-core 0.6.0

Core library for the Narrative Addressing Protocol
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
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
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
//! Repository — filesystem layout and manifest CRUD.
//!
//! A NAP repository is a self-contained directory of entities.
//! Repository structure:
//!
//! ```text
//! toystory/               ← repository root
//! ├── repository.yaml     ← repository metadata (name, description, nap config)
//! ├── character/          ← entity type (has .entity-type marker)
//! │   ├── .entity-type    ← marker file
//! │   ├── woody.yaml
//! │   └── slinky.yaml
//! ├── location/
//! │   ├── .entity-type
//! │   └── andys-room.yaml
//! └── scene/
//!     ├── .entity-type
//!     └── pizza-planet-scene.yaml
//! ```

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

use tracing::{debug, info};

use crate::commit::{Change, Commit};
use crate::error::NapError;
use crate::manifest::Manifest;
use crate::resolver::ResolveConfig;
use crate::types::EntityType;
use crate::uri::NapUri;
use crate::vcs::VcsBackend;

/// Marker filename for entity type directories.
pub const ENTITY_TYPE_MARKER: &str = ".entity-type";

/// Sentinel returned as the "commit hash" when a write is performed in
/// unversioned mode (no version-control backend configured).
pub const UNVERSIONED_COMMIT: &str = "unversioned";

/// A NAP repository.
pub struct Repository {
    /// Filesystem path to the repository root.
    pub root: PathBuf,
    /// The repository name (derived from directory name).
    pub repository: String,
    /// The VCS backend (Lore), if a version-control backend is configured.
    /// `None` means the repository operates in unversioned (filesystem-only)
    /// mode: reads/writes work, but VCS-only operations fail informatively.
    vcs: Option<Box<dyn VcsBackend>>,
}

impl Repository {
    /// Open an existing NAP repository at the given path with a VCS backend.
    pub fn open(path: &Path, vcs: Box<dyn VcsBackend>) -> Result<Self, NapError> {
        Self::open_optional(path, Some(vcs))
    }

    /// Open an existing NAP repository at the given path.
    ///
    /// `vcs` may be `None` to operate in unversioned mode.
    pub fn open_optional(path: &Path, vcs: Option<Box<dyn VcsBackend>>) -> Result<Self, NapError> {
        // Check for repository.yaml or repository.yaml to identify valid repository
        if !path.join("repository.yaml").exists() && !path.join("repository.yaml").exists() {
            return Err(NapError::RepositoryNotFound(path.display().to_string()));
        }

        let repository = path
            .file_name()
            .and_then(|n| n.to_str())
            .unwrap_or("unknown")
            .to_string();

        debug!(
            path = %path.display(),
            repository = %repository,
            "opened NAP repository"
        );

        Ok(Self {
            root: path.to_path_buf(),
            repository,
            vcs,
        })
    }

    /// Read the resolve configuration from repository.yaml (or repository.yaml) metadata.
    pub fn read_resolve_config(&self) -> ResolveConfig {
        // Prefer repository.yaml, fall back to repository.yaml
        let repo_yaml_path = self.root.join("repository.yaml");
        let universe_yaml_path = self.root.join("repository.yaml");
        let config_path = if repo_yaml_path.exists() {
            repo_yaml_path
        } else if universe_yaml_path.exists() {
            universe_yaml_path
        } else {
            debug!("no repository.yaml or repository.yaml found, using default ResolveConfig");
            return ResolveConfig::default();
        };

        let yaml_content = match std::fs::read_to_string(&config_path) {
            Ok(content) => content,
            Err(e) => {
                debug!(
                    path = %config_path.display(),
                    error = %e,
                    "failed to read config, using default ResolveConfig"
                );
                return ResolveConfig::default();
            }
        };

        // Parse YAML and extract nap metadata
        let parsed: serde_yaml::Value = match serde_yaml::from_str(&yaml_content) {
            Ok(value) => value,
            Err(e) => {
                debug!(
                    path = %config_path.display(),
                    error = %e,
                    "failed to parse config, using default ResolveConfig"
                );
                return ResolveConfig::default();
            }
        };

        // Extract default_branch from nap metadata
        let default_branch = parsed
            .get("metadata")
            .and_then(|metadata| metadata.get("nap"))
            .and_then(|nap| nap.get("default_branch"))
            .and_then(|branch| branch.as_str())
            .map(|s| s.to_string());

        // Auto-create nap metadata if missing
        if default_branch.is_none() {
            debug!(
                path = %config_path.display(),
                "nap metadata not found, auto-creating with default_branch = 'main'"
            );

            // Read the existing manifest
            let mut manifest = match Manifest::from_file(&config_path) {
                Ok(m) => m,
                Err(e) => {
                    debug!(
                        path = %config_path.display(),
                        error = %e,
                        "failed to read manifest, using default ResolveConfig"
                    );
                    return ResolveConfig::default();
                }
            };

            // Add nap metadata
            manifest.metadata.insert(
                "nap".to_string(),
                serde_yaml::to_value(serde_json::json!({
                    "default_branch": "main"
                }))
                .unwrap(),
            );

            // Write back to file
            if let Err(e) = manifest.to_file(&config_path) {
                debug!(
                    path = %config_path.display(),
                    error = %e,
                    "failed to write nap metadata, using default ResolveConfig"
                );
                return ResolveConfig::default();
            }

            return ResolveConfig {
                default_branch: Some("main".to_string()),
            };
        }

        ResolveConfig { default_branch }
    }

    /// Initialize a new NAP repository with a VCS backend.
    pub fn init(path: &Path, repository: &str, vcs: Box<dyn VcsBackend>) -> Result<Self, NapError> {
        Self::init_optional(path, repository, Some(vcs))
    }

    /// Initialize a new NAP repository.
    ///
    /// `vcs` may be `None` to initialize in unversioned mode — the repository
    /// structure is created and an initial filesystem state is written, but no
    /// version-control workspace or initial commit is created.
    pub fn init_optional(
        path: &Path,
        repository: &str,
        vcs: Option<Box<dyn VcsBackend>>,
    ) -> Result<Self, NapError> {
        let repo_root = path.to_path_buf();
        if repo_root.join("repository.yaml").exists() || repo_root.join("repository.yaml").exists()
        {
            return Err(NapError::RepositoryAlreadyExists(
                repo_root.display().to_string(),
            ));
        }

        info!(
            path = %repo_root.display(),
            repository = %repository,
            "initializing NAP repository"
        );

        // Create directory structure
        std::fs::create_dir_all(&repo_root)?;

        // Create repository.yaml with [nap] metadata
        let mut repo_manifest = Manifest::new(
            repository,
            EntityType::new("world"),
            repository,
            &format!("{repository} Repository"),
        );

        // Add nap configuration to metadata
        repo_manifest.metadata.insert(
            "nap".to_string(),
            serde_yaml::to_value(serde_json::json!({
                "default_branch": "main"
            }))
            .unwrap(),
        );

        repo_manifest.to_file(&repo_root.join("repository.yaml"))?;

        // Initialize VCS + initial commit when a backend is configured
        if let Some(vcs) = vcs.as_ref() {
            vcs.init(&repo_root)?;
            vcs.commit(
                &repo_root,
                &format!("Initialize {repository} repository"),
                "nap-init",
            )?;
        }

        info!(
            path = %repo_root.display(),
            repository = %repository,
            "NAP repository initialized successfully"
        );

        Ok(Self {
            root: repo_root,
            repository: repository.to_string(),
            vcs,
        })
    }

    /// Bootstrap a VCS backend for an already-initialized repository that was
    /// created in unversioned mode.
    ///
    /// Initializes the backend at the repository root and creates an initial
    /// commit capturing the current filesystem state. Used by
    /// `nap backend configure` when a backend is configured after the fact.
    ///
    /// Errors if no backend is available (unversioned mode).
    pub fn bootstrap_vcs(&self, message: &str, author: &str) -> Result<String, NapError> {
        let vcs = self.require_vcs("bootstrap the repository into version control")?;
        vcs.init(&self.root)?;
        let hash = vcs.commit(&self.root, message, author)?;
        info!(
            repository = %self.repository,
            commit_hash = %hash,
            "bootstrapped repository into version control"
        );
        Ok(hash)
    }

    fn require_vcs(&self, operation: &str) -> Result<&dyn VcsBackend, NapError> {
        self.vcs
            .as_deref()
            .ok_or_else(|| NapError::BackendNotConfigured {
                operation: operation.to_string(),
            })
    }

    /// Get the full filesystem path to an entity's manifest file.
    pub fn manifest_path(&self, entity_type: &EntityType, entity_id: &str) -> PathBuf {
        let uri = NapUri::new(&self.repository, entity_type.clone(), entity_id);
        self.root.join(uri.manifest_path())
    }

    /// Read a manifest from the repository.
    pub fn read_manifest(
        &self,
        entity_type: &EntityType,
        entity_id: &str,
    ) -> Result<Manifest, NapError> {
        let path = self.manifest_path(entity_type, entity_id);
        debug!(
            path = %path.display(),
            entity_type = %entity_type,
            entity_id = %entity_id,
            "reading manifest"
        );
        Manifest::from_file(&path)
    }

    /// Read a manifest at a specific VCS ref (commit, branch, tag).
    pub fn read_manifest_at_ref(
        &self,
        entity_type: &EntityType,
        entity_id: &str,
        reference: &str,
    ) -> Result<Manifest, NapError> {
        let uri = NapUri::new(&self.repository, entity_type.clone(), entity_id);
        let file_path = uri.manifest_path();

        debug!(
            file_path = %file_path,
            reference = %reference,
            "reading manifest at ref"
        );

        let content = self.require_vcs("read manifest at ref")?.read_file_at_ref(
            &self.root,
            &file_path,
            Some(reference),
        )?;
        Manifest::from_yaml(&content)
    }

    /// Write a manifest to the repository (does NOT commit).
    pub fn write_manifest(&self, manifest: &Manifest) -> Result<PathBuf, NapError> {
        let uri: NapUri = manifest.id.parse()?;
        let entity_type = uri.entity_type.clone();

        // Ensure the entity type directory and marker exist
        self.ensure_entity_type_dir(&entity_type)?;

        let path = self.root.join(uri.manifest_path());

        debug!(
            path = %path.display(),
            manifest_id = %manifest.id,
            "writing manifest"
        );

        manifest.to_file(&path)?;
        Ok(path)
    }

    /// Ensure an entity type directory exists with its marker file.
    fn ensure_entity_type_dir(&self, entity_type: &EntityType) -> Result<(), NapError> {
        let dir = self.root.join(entity_type.directory_name());
        if !dir.exists() {
            std::fs::create_dir_all(&dir)?;
            // Create .entity-type marker file
            let marker = dir.join(ENTITY_TYPE_MARKER);
            std::fs::write(&marker, "")?;
            debug!(
                entity_type = %entity_type,
                path = %dir.display(),
                "created entity type directory with marker"
            );
        }
        Ok(())
    }

    /// Create a new entity manifest and commit it.
    pub fn create_entity(
        &self,
        entity_type: &EntityType,
        entity_id: &str,
        name: &str,
        author: &str,
    ) -> Result<(Manifest, String), NapError> {
        // Ensure entity type directory exists
        self.ensure_entity_type_dir(entity_type)?;

        let mut manifest = Manifest::new(&self.repository, entity_type.clone(), entity_id, name);

        // Check if entity already exists (idempotency guard)
        let path = self.manifest_path(entity_type, entity_id);
        if path.exists() {
            return Err(NapError::Other(format!(
                "entity '{entity_id}' of type '{entity_type}' already exists"
            )));
        }

        manifest.bump_version();

        // Validate against schema before writing
        crate::schema::validate_manifest(&manifest)
            .map_err(|errors| NapError::ManifestValidationError(errors.join("; ")))?;

        // Write the manifest
        self.write_manifest(&manifest)?;

        // Commit via VCS when a backend is configured; otherwise the write
        // above is the only durable change (unversioned mode).
        let commit_hash = match self.vcs.as_ref() {
            Some(vcs) => {
                let commit_message = format!("Create {entity_type} '{name}' ({entity_id})");
                vcs.commit(&self.root, &commit_message, author)?
            }
            None => UNVERSIONED_COMMIT.to_string(),
        };

        info!(
            manifest_id = %manifest.id,
            commit_hash = %commit_hash,
            "created entity"
        );

        Ok((manifest, commit_hash))
    }

    /// Update an existing manifest and commit the changes.
    pub fn commit_manifest(
        &self,
        manifest: &mut Manifest,
        message: &str,
        author: &str,
        changes: Vec<Change>,
    ) -> Result<Commit, NapError> {
        // Validate against schema before writing
        crate::schema::validate_manifest(manifest)
            .map_err(|errors| NapError::ManifestValidationError(errors.join("; ")))?;

        // Bump version
        manifest.bump_version();

        // Write updated manifest.
        self.write_manifest(manifest)?;

        // Compute manifest hash
        let manifest_hash = manifest.content_hash()?.as_str().to_string();

        // VCS commit — produces the new HEAD hash. When no backend is
        // configured (unversioned mode), there is no history to record and the
        // filesystem write above is the only durable change.
        let (parent, vcs_hash) = match self.vcs.as_ref() {
            Some(vcs) => (
                Some(vcs.head_hash(&self.root)?),
                vcs.commit(&self.root, message, author)?,
            ),
            None => (None, UNVERSIONED_COMMIT.to_string()),
        };

        // Create NAP commit metadata with the previous VCS HEAD as parent.
        let nap_commit = Commit::new(parent, author, message, &manifest_hash, changes);

        debug!(
            manifest_id = %manifest.id,
            version = manifest.version,
            nap_commit_id = %nap_commit.id,
            vcs_hash = %vcs_hash,
            "manifest committed"
        );

        Ok(nap_commit)
    }

    /// Get the commit history for a specific entity.
    pub fn history(
        &self,
        entity_type: &EntityType,
        entity_id: &str,
        limit: usize,
    ) -> Result<Vec<crate::vcs::CommitInfo>, NapError> {
        let uri = NapUri::new(&self.repository, entity_type.clone(), entity_id);
        let file_path = uri.manifest_path();
        self.require_vcs("view history")?
            .log(&self.root, Some(&file_path), limit)
    }

    /// List all entity IDs of a given type in the repository.
    pub fn list_entities(&self, entity_type: &EntityType) -> Result<Vec<String>, NapError> {
        let dir = self.root.join(entity_type.directory_name());
        if !dir.exists() {
            return Err(NapError::EntityTypeNotFound(entity_type.to_string()));
        }

        let mut entities = Vec::new();
        for entry in std::fs::read_dir(&dir)? {
            let entry = entry?;
            let path = entry.path();
            if path.extension().and_then(|e| e.to_str()) == Some("yaml")
                && let Some(stem) = path.file_stem().and_then(|s| s.to_str())
            {
                entities.push(stem.to_string());
            }
        }
        entities.sort();
        Ok(entities)
    }

    /// Discover all entity types in this repository.
    ///
    /// Scans the repository root for directories containing a `.entity-type`
    /// marker file OR directories containing at least one `.yaml` file
    /// (implicit discovery for backward compatibility).
    pub fn list_entity_types(&self) -> Result<Vec<EntityType>, NapError> {
        let mut types = Vec::new();
        for entry in std::fs::read_dir(&self.root)? {
            let entry = entry?;
            let path = entry.path();
            if !path.is_dir() {
                continue;
            }
            // Skip hidden directories (.nap, etc.)
            let dir_name = match path.file_name().and_then(|n| n.to_str()) {
                Some(name) => name,
                None => continue,
            };
            if dir_name.starts_with('.') || dir_name == "target" {
                continue;
            }

            // Check for .entity-type marker file (explicit)
            if path.join(ENTITY_TYPE_MARKER).exists() {
                types.push(EntityType::new(dir_name));
                continue;
            }

            // Implicit: directory contains at least one .yaml file
            let has_yaml = std::fs::read_dir(&path)
                .ok()
                .and_then(|entries| {
                    entries
                        .filter_map(|e| e.ok())
                        .find(|e| e.path().extension().and_then(|ext| ext.to_str()) == Some("yaml"))
                        .map(|_| true)
                })
                .unwrap_or(false);

            if has_yaml {
                types.push(EntityType::new(dir_name));
            }
        }
        types.sort_by(|a, b| a.as_str().cmp(b.as_str()));
        Ok(types)
    }

    /// Delete an entity manifest and commit the deletion.
    pub fn delete_entity(
        &self,
        entity_type: &EntityType,
        entity_id: &str,
        author: &str,
    ) -> Result<String, NapError> {
        let path = self.manifest_path(entity_type, entity_id);
        if !path.exists() {
            return Err(NapError::ManifestNotFound(path.display().to_string()));
        }

        std::fs::remove_file(&path)?;

        let message = format!("Delete {entity_type} '{entity_id}'");
        let hash = match self.vcs.as_ref() {
            Some(vcs) => vcs.commit(&self.root, &message, author)?,
            None => UNVERSIONED_COMMIT.to_string(),
        };
        info!(entity_type = %entity_type, entity_id = %entity_id, "deleted entity");
        Ok(hash)
    }

    /// Create a branch in the underlying VCS.
    pub fn create_branch(&self, name: &str) -> Result<(), NapError> {
        self.require_vcs("create branch")?
            .create_branch(&self.root, name)
    }

    /// Switch to a branch.
    pub fn switch_branch(&self, name: &str) -> Result<(), NapError> {
        self.require_vcs("switch branch")?
            .switch_branch(&self.root, name)
    }

    /// List branches.
    pub fn list_branches(&self) -> Result<Vec<String>, NapError> {
        self.require_vcs("list branches")?.list_branches(&self.root)
    }

    /// Revert a commit by creating a new VCS commit that undoes the specified one.
    pub fn revert_commit(&self, commit_hash: &str, author: &str) -> Result<String, NapError> {
        let vcs = self.require_vcs("revert commit")?;
        let new_hash = vcs.revert(&self.root, commit_hash)?;

        info!(
            commit = %commit_hash,
            revert = %new_hash,
            author = %author,
            "commit reverted"
        );

        Ok(new_hash)
    }

    /// Get current HEAD hash.
    pub fn head_hash(&self) -> Result<String, NapError> {
        self.require_vcs("read HEAD hash")?.head_hash(&self.root)
    }

    /// Resolve the most recent commit hash on a given branch.
    pub fn resolve_branch_head(&self, branch: &str) -> Result<String, NapError> {
        self.require_vcs("resolve branch head")?
            .resolve_branch_head(&self.root, branch)
    }

    // ── Remote operations ─────────────────────────────────────────

    /// Add a remote to the repository.
    pub fn add_remote(&self, name: &str, url: &str) -> Result<(), NapError> {
        self.require_vcs("add remote")?
            .add_remote(&self.root, name, url)
    }

    /// Remove a remote from the repository.
    pub fn remove_remote(&self, name: &str) -> Result<(), NapError> {
        self.require_vcs("remove remote")?
            .remove_remote(&self.root, name)
    }

    /// List remotes as `(name, url)` pairs.
    pub fn list_remotes(&self) -> Result<Vec<(String, String)>, NapError> {
        self.require_vcs("list remotes")?.list_remotes(&self.root)
    }

    /// Push the current branch to a remote.
    pub fn push(&self, remote: Option<&str>, branch: Option<&str>) -> Result<(), NapError> {
        self.require_vcs("push")?.push(&self.root, remote, branch)
    }

    /// Pull the current branch from a remote.
    pub fn pull(&self, remote: Option<&str>, branch: Option<&str>) -> Result<(), NapError> {
        self.require_vcs("pull")?.pull(&self.root, remote, branch)
    }

    /// Access the VCS backend (for the resolver to read files at specific refs).
    ///
    /// Returns `None` when no version-control backend is configured (unversioned mode).
    pub fn vcs(&self) -> Option<&dyn VcsBackend> {
        self.vcs.as_deref()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::test_utils::{MockBackend, mock_repo};
    use tempfile::TempDir;

    #[test]
    fn test_mock_backend_contract() {
        crate::test_utils::contract::run_repository_contract(MockBackend::new());
    }

    #[test]
    fn test_init_creates_structure() {
        let tmp = TempDir::new().unwrap();
        let repo = mock_repo(&tmp);

        assert!(repo.root.join("repository.yaml").exists());
    }

    #[test]
    fn test_create_and_read_entity() {
        let tmp = TempDir::new().unwrap();
        let repo = mock_repo(&tmp);

        let (manifest, hash) = repo
            .create_entity(
                &EntityType::new("character"),
                "hero",
                "The Hero",
                "test-author",
            )
            .unwrap();

        assert_eq!(manifest.name, "The Hero");
        assert_eq!(manifest.entity_type.as_str(), "character");
        assert_eq!(manifest.version, 1);
        assert_eq!(repo.head_hash().unwrap(), hash);

        // Read it back
        let read_back = repo
            .read_manifest(&EntityType::new("character"), "hero")
            .unwrap();
        assert_eq!(read_back.name, "The Hero");
        assert_eq!(read_back.version, 1);
    }

    #[test]
    fn test_create_entity_auto_creates_type_directory() {
        let tmp = TempDir::new().unwrap();
        let repo = mock_repo(&tmp);

        // Create entity of a custom type
        repo.create_entity(&EntityType::new("pokemon"), "pikachu", "Pikachu", "test")
            .unwrap();

        // Verify the type directory and marker exist
        assert!(repo.root.join("pokemon").exists());
        assert!(repo.root.join("pokemon").join(".entity-type").exists());
        assert!(repo.root.join("pokemon/pikachu.yaml").exists());
    }

    #[test]
    fn test_list_entity_types() {
        let tmp = TempDir::new().unwrap();
        let repo = mock_repo(&tmp);

        // Create entities of different types
        repo.create_entity(&EntityType::new("character"), "hero", "Hero", "author")
            .unwrap();
        repo.create_entity(&EntityType::new("location"), "village", "Village", "author")
            .unwrap();
        repo.create_entity(&EntityType::new("pokemon"), "pikachu", "Pikachu", "author")
            .unwrap();

        let types = repo.list_entity_types().unwrap();
        assert!(types.contains(&EntityType::new("character")));
        assert!(types.contains(&EntityType::new("location")));
        assert!(types.contains(&EntityType::new("pokemon")));
    }

    #[test]
    fn test_list_entities() {
        let tmp = TempDir::new().unwrap();
        let repo = mock_repo(&tmp);

        repo.create_entity(&EntityType::new("character"), "alice", "Alice", "author")
            .unwrap();
        repo.create_entity(&EntityType::new("character"), "bob", "Bob", "author")
            .unwrap();

        let chars = repo.list_entities(&EntityType::new("character")).unwrap();
        assert_eq!(chars, vec!["alice", "bob"]);
    }

    #[test]
    fn test_commit_manifest_updates() {
        let tmp = TempDir::new().unwrap();
        let repo = mock_repo(&tmp);

        let (mut manifest, create_hash) = repo
            .create_entity(&EntityType::new("character"), "hero", "The Hero", "author")
            .unwrap();

        // Modify and commit
        manifest.set_property("toy_type", serde_yaml::Value::String("elf".to_string()));
        let changes = vec![Change::set("properties.toy_type", None, "elf".to_string())];
        let commit = repo
            .commit_manifest(&mut manifest, "set toy_type to elf", "author", changes)
            .unwrap();

        assert!(!commit.id.is_empty());
        assert_eq!(commit.message, "set toy_type to elf");
        assert_eq!(commit.parent.as_deref(), Some(create_hash.as_str()));

        // Verify version incremented
        let read_back = repo
            .read_manifest(&EntityType::new("character"), "hero")
            .unwrap();
        assert!(read_back.version >= 2);
    }

    #[test]
    fn test_history() {
        let tmp = TempDir::new().unwrap();
        let repo = mock_repo(&tmp);

        let (mut manifest, _) = repo
            .create_entity(&EntityType::new("character"), "hero", "The Hero", "author")
            .unwrap();

        manifest.set_property(
            "name",
            serde_yaml::Value::String("Updated Hero".to_string()),
        );
        repo.commit_manifest(&mut manifest, "update name", "author", vec![])
            .unwrap();

        let hist = repo
            .history(&EntityType::new("character"), "hero", 10)
            .unwrap();
        assert!(hist.len() >= 2);
    }

    #[test]
    fn test_revert_commit() {
        let tmp = TempDir::new().unwrap();
        let repo = mock_repo(&tmp);

        // Create entity and note its name
        let (mut manifest, _) = repo
            .create_entity(&EntityType::new("character"), "hero", "The Hero", "author")
            .unwrap();
        assert_eq!(manifest.name, "The Hero");

        // Modify and commit
        manifest.set_property("toy_type", serde_yaml::Value::String("elf".to_string()));
        let changes = vec![Change::set("properties.toy_type", None, "elf".to_string())];
        repo.commit_manifest(&mut manifest, "set toy_type to elf", "author", changes)
            .unwrap();
        let update_hash = repo.head_hash().unwrap();

        // Revert the VCS commit.
        let revert_hash = repo.revert_commit(&update_hash, "author").unwrap();
        assert!(!revert_hash.is_empty());

        // Verify the manifest remains normal YAML without a cached head field.
        let manifest_path = repo.manifest_path(&EntityType::new("character"), "hero");
        let manifest_yaml = std::fs::read_to_string(&manifest_path).unwrap();
        assert!(!manifest_yaml.contains("\nhead:"));

        // Verify the revert appears in history
        let hist = repo
            .history(&EntityType::new("character"), "hero", 10)
            .unwrap();
        assert!(hist.iter().any(|c| c.id == revert_hash));
    }

    // ── Unversioned mode ────────────────────────────────────────────────

    fn unversioned_repo(tmp: &TempDir) -> Repository {
        let repo_path = tmp.path().join("testverse");
        Repository::init_optional(&repo_path, "testverse", None).unwrap()
    }

    #[test]
    fn test_unversioned_init_creates_structure() {
        let tmp = TempDir::new().unwrap();
        let repo = unversioned_repo(&tmp);

        assert!(repo.root.join("repository.yaml").exists());
        // No backend was provided, so VCS is absent.
        assert!(repo.vcs().is_none());
    }

    #[test]
    fn test_unversioned_create_returns_sentinel_commit() {
        let tmp = TempDir::new().unwrap();
        let repo = unversioned_repo(&tmp);

        let (manifest, hash) = repo
            .create_entity(
                &EntityType::new("character"),
                "hero",
                "The Hero",
                "test-author",
            )
            .unwrap();

        assert_eq!(manifest.name, "The Hero");
        assert_eq!(hash, UNVERSIONED_COMMIT);
        assert!(repo.root.join("character/hero.yaml").exists());

        // The file is durable even without a backend.
        let read_back = repo
            .read_manifest(&EntityType::new("character"), "hero")
            .unwrap();
        assert_eq!(read_back.name, "The Hero");
    }

    #[test]
    fn test_unversioned_commit_manifest_persists() {
        let tmp = TempDir::new().unwrap();
        let repo = unversioned_repo(&tmp);

        let (mut manifest, _) = repo
            .create_entity(&EntityType::new("character"), "hero", "The Hero", "author")
            .unwrap();

        manifest.set_property("toy_type", serde_yaml::Value::String("elf".to_string()));
        let changes = vec![Change::set("properties.toy_type", None, "elf".to_string())];
        let commit = repo
            .commit_manifest(&mut manifest, "set toy_type to elf", "author", changes)
            .unwrap();

        // No VCS history exists, so the commit has no parent.
        assert!(commit.parent.is_none());

        let read_back = repo
            .read_manifest(&EntityType::new("character"), "hero")
            .unwrap();
        assert_eq!(read_back.properties["toy_type"], "elf");
    }

    #[test]
    fn test_unversioned_vcs_operations_error() {
        let tmp = TempDir::new().unwrap();
        let repo = unversioned_repo(&tmp);

        repo.create_entity(&EntityType::new("character"), "hero", "The Hero", "author")
            .unwrap();

        // VCS-only operations fail with a BackendNotConfigured error.
        let err = repo
            .history(&EntityType::new("character"), "hero", 10)
            .unwrap_err();
        assert!(matches!(err, NapError::BackendNotConfigured { .. }));

        let err = repo.list_branches().unwrap_err();
        assert!(matches!(err, NapError::BackendNotConfigured { .. }));

        let err = repo.head_hash().unwrap_err();
        assert!(matches!(err, NapError::BackendNotConfigured { .. }));

        let err = repo.push(Some("origin"), None).unwrap_err();
        assert!(matches!(err, NapError::BackendNotConfigured { .. }));

        // The error message guides the user toward configuration.
        let msg = err.to_string();
        assert!(msg.contains("nap backend configure"));
    }

    #[test]
    fn test_bootstrap_vcs_attaches_backend_and_commits() {
        let tmp = TempDir::new().unwrap();
        let unversioned = unversioned_repo(&tmp);

        // Seed some filesystem state while unversioned.
        unversioned
            .create_entity(&EntityType::new("character"), "hero", "The Hero", "author")
            .unwrap();

        // Attach a backend and bootstrap the current state as the baseline.
        let backend = MockBackend::new();
        let bootstrapped =
            Repository::open_optional(&unversioned.root, Some(Box::new(backend))).unwrap();

        let hash = bootstrapped
            .bootstrap_vcs("Initialize existing NAP repository", "nap")
            .unwrap();

        assert!(!hash.is_empty());
        assert!(bootstrapped.head_hash().unwrap() == hash);
    }

    #[test]
    fn test_bootstrap_vcs_without_backend_errors() {
        let tmp = TempDir::new().unwrap();
        let repo = unversioned_repo(&tmp);

        let err = repo
            .bootstrap_vcs("Initialize existing NAP repository", "nap")
            .unwrap_err();
        assert!(matches!(err, NapError::BackendNotConfigured { .. }));
        let msg = err.to_string();
        assert!(msg.contains("nap backend configure"));
    }
}

// ── Integration tests: Repository + LoreBackend ─────────────────────
// These require a running Lore server. Run with:
//   cargo test --features lore-integration
#[cfg(all(test, feature = "lore-integration"))]
mod lore_integration_tests {
    use super::*;
    use crate::vcs_lore::LoreBackend;
    use std::time::{SystemTime, UNIX_EPOCH};
    use tempfile::TempDir;

    fn unique_suffix() -> u64 {
        SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_nanos() as u64
    }

    fn setup_lore_repo() -> (TempDir, Repository) {
        let repository = format!("ri-{}", unique_suffix());
        let tmp = TempDir::new().unwrap();
        let repo_path = tmp.path().join(&repository);
        let repo =
            Repository::init(&repo_path, &repository, Box::new(LoreBackend::from_env())).unwrap();
        (tmp, repo)
    }

    #[test]
    fn test_lore_init_creates_structure() {
        let (_tmp, repo) = setup_lore_repo();
        assert!(repo.root.join(".nap").exists());
        assert!(repo.root.join("repository.yaml").exists());
    }

    #[test]
    fn test_lore_create_and_read_entity() {
        let (_tmp, repo) = setup_lore_repo();

        let (manifest, _hash) = repo
            .create_entity(
                &EntityType::new("character"),
                "hero",
                "Test Hero",
                "integration-test",
            )
            .unwrap();
        assert_eq!(manifest.name, "Test Hero");

        let read_back = repo
            .read_manifest(&EntityType::new("character"), "hero")
            .unwrap();
        assert_eq!(read_back.name, "Test Hero");
    }

    #[test]
    fn test_lore_commit_and_branch() {
        let (_tmp, repo) = setup_lore_repo();

        let (mut manifest, _) = repo
            .create_entity(
                &EntityType::new("character"),
                "hero",
                "Test Hero",
                "integration-test",
            )
            .unwrap();

        manifest.set_property("toy_type", serde_yaml::Value::String("plush".to_string()));
        let changes = vec![Change::set(
            "properties.toy_type",
            None,
            "plush".to_string(),
        )];
        repo.commit_manifest(&mut manifest, "add toy_type", "integration-test", changes)
            .unwrap();

        let read_back = repo
            .read_manifest(&EntityType::new("character"), "hero")
            .unwrap();
        assert_eq!(read_back.version, 2);

        repo.create_branch("feature-branch").unwrap();
        let branches = repo.list_branches().unwrap();
        assert!(branches.contains(&"feature-branch".to_string()));
    }

    #[test]
    fn test_lore_delete_entity() {
        let (_tmp, repo) = setup_lore_repo();

        repo.create_entity(
            &EntityType::new("character"),
            "hero",
            "Test Hero",
            "integration-test",
        )
        .unwrap();

        repo.delete_entity(&EntityType::new("character"), "hero", "integration-test")
            .unwrap();

        let entities = repo.list_entities(&EntityType::new("character")).unwrap();
        assert!(!entities.contains(&"hero".to_string()));
    }

    #[test]
    fn test_lore_history() {
        let (_tmp, repo) = setup_lore_repo();

        let (mut manifest, _) = repo
            .create_entity(
                &EntityType::new("character"),
                "hero",
                "Test Hero",
                "integration-test",
            )
            .unwrap();

        manifest.set_property(
            "name",
            serde_yaml::Value::String("Updated Hero".to_string()),
        );
        repo.commit_manifest(&mut manifest, "update name", "integration-test", vec![])
            .unwrap();

        let hist = repo
            .history(&EntityType::new("character"), "hero", 10)
            .unwrap();
        assert!(hist.len() >= 2);
    }
}