nap-core 0.8.5

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
// SPDX-FileCopyrightText: 2026 Digital Creations
// SPDX-License-Identifier: MIT
//! Deployment-independent repository API
//!
//! This module provides the unified repository API that applications use.
//! It abstracts away provider-specific details and ensures consistent behavior
//! regardless of whether the repository is hosted on Local, Portals Cloud, or Remote.

pub mod fallback;

use anyhow::{Context, Result};
use std::path::Path;
use std::sync::Arc;
use tracing::info;

use super::provider::{Provider, ProviderManager, ProviderType};
use super::vcs::VcsBackend;
use super::vcs_lore::LoreBackend;

/// Repository API for deployment-independent repository operations
pub struct RepositoryApi {
    nap_home: std::path::PathBuf,
    provider_manager: ProviderManager,
}

impl RepositoryApi {
    /// Create a new repository API
    pub fn new(nap_home: &Path) -> Result<Self> {
        // Ensure NAP home directory exists with proper error handling
        std::fs::create_dir_all(nap_home).with_context(|| {
            format!(
                "Failed to create NAP home directory at '{}'. \
                    Check permissions and disk space.",
                nap_home.display()
            )
        })?;

        let mut provider_manager = ProviderManager::new(nap_home);

        // Try to load configured provider
        provider_manager
            .load_configured_provider()
            .with_context(|| {
                format!(
                    "Failed to load provider configuration from '{}'",
                    nap_home.join("provider.toml").display()
                )
            })?;

        Ok(Self {
            nap_home: nap_home.to_path_buf(),
            provider_manager,
        })
    }

    /// Initialize provider selection
    pub async fn initialize_provider_selection(&mut self) -> Result<ProviderType> {
        info!("Initializing provider selection");

        // Check if provider is already configured
        if let Some(provider) = self.provider_manager.active_provider() {
            info!("Provider already configured: {}", provider.name());
            return Ok(provider.provider_type());
        }

        // Prompt for provider selection (in real implementation, this would be interactive)
        // For now, default to Local
        let provider_type = ProviderType::Local;

        let factory = super::provider::ProviderFactory::new(&self.nap_home);
        let provider = factory.create_provider(provider_type)?;

        self.provider_manager.set_active_provider(provider.clone());
        self.provider_manager
            .save_provider_config(provider.as_ref())?;

        info!("Selected provider: {}", provider_type.as_str());
        Ok(provider_type)
    }

    /// Ensure the active provider is ready
    pub async fn ensure_provider_ready(&self) -> Result<()> {
        if let Some(provider) = self.provider_manager.active_provider() {
            provider.ensure_ready().await?;
            Ok(())
        } else {
            anyhow::bail!(
                "No active provider configured. Call initialize_provider_selection() first."
            );
        }
    }

    /// Create a new repository
    pub async fn create_repository(
        &self,
        repo_id: &str,
        workspace_id: Option<&str>,
    ) -> Result<RepositoryHandle> {
        info!("Creating repository: {}", repo_id);

        let provider = self.provider_manager.active_provider().context(
            "No active provider configured. Call initialize_provider_selection() first.",
        )?;

        // Ensure provider is ready
        provider.ensure_ready().await.with_context(|| {
            format!(
                "Failed to ensure provider '{}' is ready for repository creation",
                provider.name()
            )
        })?;

        // Get Lore URL base and workspace ID
        let lore_url_base = provider.lore_url_base().with_context(|| {
            format!(
                "Failed to get Lore URL base from provider '{}'",
                provider.name()
            )
        })?;
        let workspace = workspace_id.unwrap_or(provider.workspace_id());

        // Create Lore backend using provider configuration
        let lore_backend = LoreBackend::from_provider(&lore_url_base, workspace);

        // Create repository using Lore backend
        let workspace_root = &self.nap_home;
        let repo_path = workspace_root.join(repo_id);

        // Ensure parent directory exists
        if let Some(parent) = repo_path.parent() {
            std::fs::create_dir_all(parent).with_context(|| {
                format!(
                    "Failed to create parent directory for repository at '{}'",
                    repo_path.display()
                )
            })?;
        }

        // Track initial state for potential rollback
        let repo_existed_before = repo_path.exists();

        match lore_backend.init(&repo_path) {
            Ok(()) => {
                info!("Repository created: {}", repo_id);
                Ok(RepositoryHandle {
                    id: repo_id.to_string(),
                    workspace_id: workspace.to_string(),
                    path: repo_path,
                    lore_url_base,
                })
            }
            Err(e) => {
                // Rollback: remove partial repository directory if it was created during this call
                if repo_path.exists() && !repo_existed_before {
                    tracing::warn!(
                        repo_id,
                        path = %repo_path.display(),
                        "Repository creation failed, cleaning up partial repository at '{}'",
                        repo_path.display()
                    );
                    if let Err(cleanup_err) = std::fs::remove_dir_all(&repo_path) {
                        tracing::error!(
                            path = %repo_path.display(),
                            error = %cleanup_err,
                            "Failed to clean up partial repository directory after creation failure"
                        );
                    }
                }
                Err(anyhow::anyhow!(
                    "Failed to create repository '{}': {}. \
                     The operation has been rolled back and no partial state remains.",
                    repo_id,
                    e
                ))
            }
        }
    }

    /// Open an existing repository
    pub async fn open_repository(&self, repo_id: &str) -> Result<RepositoryHandle> {
        info!("Opening repository: {}", repo_id);

        let provider = self.provider_manager.active_provider().context(
            "No active provider configured. Call initialize_provider_selection() first.",
        )?;

        let lore_url_base = provider.lore_url_base().with_context(|| {
            format!(
                "Failed to get Lore URL base from provider '{}'",
                provider.name()
            )
        })?;
        let workspace_id = provider.workspace_id();

        let repo_path = self.nap_home.join(repo_id);

        if !repo_path.exists() {
            anyhow::bail!(
                "Repository '{}' not found at '{}'. \
                 Verify the repository ID and ensure it has been created.",
                repo_id,
                repo_path.display()
            );
        }

        info!("Repository opened: {}", repo_id);

        Ok(RepositoryHandle {
            id: repo_id.to_string(),
            workspace_id: workspace_id.to_string(),
            path: repo_path,
            lore_url_base,
        })
    }

    /// Publish changes (semantic operation)
    pub async fn publish(&self, repo_handle: &RepositoryHandle, message: &str) -> Result<String> {
        info!("Publishing changes to repository: {}", repo_handle.id);

        let provider = self.provider_manager.active_provider().context(
            "No active provider configured. Call initialize_provider_selection() first.",
        )?;

        let workspace_id = provider.workspace_id();
        let lore_backend = LoreBackend::from_provider(&repo_handle.lore_url_base, workspace_id);

        // Commit changes
        let commit_hash = lore_backend
            .commit(&repo_handle.path, message, "nap")
            .with_context(|| {
                format!(
                    "Failed to commit changes to repository '{}' at '{}'. \
                     Check repository state and permissions.",
                    repo_handle.id,
                    repo_handle.path.display()
                )
            })?;

        // Provider-specific publish behavior
        match provider.provider_type() {
            ProviderType::Local => {
                // Local: just commit
                info!("Published locally: {}", commit_hash);
            }
            ProviderType::PortalsCloud | ProviderType::Remote => {
                // Cloud/Remote: commit and push
                // If push fails, we need to rollback the commit to keep local state consistent
                match lore_backend.push(&repo_handle.path, None, None) {
                    Ok(_) => {
                        info!("Published and synchronized: {}", commit_hash);
                    }
                    Err(push_err) => {
                        // Rollback: revert the commit since push failed
                        tracing::warn!(
                            repo_id = %repo_handle.id,
                            commit_hash = %commit_hash,
                            "Push failed, rolling back commit to keep local state consistent"
                        );
                        let rollback_result = lore_backend.revert(&repo_handle.path, &commit_hash);
                        if let Err(revert_err) = rollback_result {
                            tracing::error!(
                                repo_id = %repo_handle.id,
                                commit_hash = %commit_hash,
                                error = %revert_err,
                                "Failed to rollback commit after push failure - repository may be in inconsistent state"
                            );
                            return Err(anyhow::anyhow!(
                                "Failed to push changes to remote for repository '{}'. \
                                 Attempted to rollback commit '{}' but failed. \
                                 Repository may be in inconsistent state. \
                                 Recovery options: \
                                 1. Manually revert the commit: lore revert {} \
                                 2. Reset to previous state: lore reset --hard HEAD~1 \
                                 3. Check repository status: lore status \
                                 Original push error: {}. Rollback error: {}",
                                repo_handle.id,
                                commit_hash,
                                commit_hash,
                                push_err,
                                revert_err
                            ));
                        }
                        return Err(anyhow::anyhow!(
                            "Failed to push changes to remote for repository '{}'. \
                             The commit has been successfully rolled back. \
                             Original error: {}",
                            repo_handle.id,
                            push_err
                        ));
                    }
                }
            }
        }

        Ok(commit_hash)
    }

    /// Get repository history
    pub async fn history(
        &self,
        repo_handle: &RepositoryHandle,
        limit: usize,
    ) -> Result<Vec<CommitInfo>> {
        info!("Getting history for repository: {}", repo_handle.id);

        let provider = self.provider_manager.active_provider().context(
            "No active provider configured. Call initialize_provider_selection() first.",
        )?;

        let workspace_id = provider.workspace_id();
        let lore_backend = LoreBackend::from_provider(&repo_handle.lore_url_base, workspace_id);

        let commits = lore_backend
            .log(&repo_handle.path, None, limit)
            .with_context(|| {
                format!(
                    "Failed to get history for repository '{}' at '{}'. \
                     Check repository state and Lore server connectivity.",
                    repo_handle.id,
                    repo_handle.path.display()
                )
            })?;

        Ok(commits)
    }

    /// Create a branch
    pub async fn create_branch(
        &self,
        repo_handle: &RepositoryHandle,
        branch_name: &str,
    ) -> Result<()> {
        info!(
            "Creating branch: {} in repository: {}",
            branch_name, repo_handle.id
        );

        let provider = self.provider_manager.active_provider().context(
            "No active provider configured. Call initialize_provider_selection() first.",
        )?;

        let workspace_id = provider.workspace_id();
        let lore_backend = LoreBackend::from_provider(&repo_handle.lore_url_base, workspace_id);

        lore_backend
            .create_branch(&repo_handle.path, branch_name)
            .with_context(|| {
                format!(
                    "Failed to create branch '{}' in repository '{}'. \
                     Verify branch name is valid and repository is in a valid state.",
                    branch_name, repo_handle.id
                )
            })?;

        Ok(())
    }

    /// Switch to a branch
    pub async fn switch_branch(
        &self,
        repo_handle: &RepositoryHandle,
        branch_name: &str,
    ) -> Result<()> {
        info!(
            "Switching to branch: {} in repository: {}",
            branch_name, repo_handle.id
        );

        let provider = self.provider_manager.active_provider().context(
            "No active provider configured. Call initialize_provider_selection() first.",
        )?;

        let workspace_id = provider.workspace_id();
        let lore_backend = LoreBackend::from_provider(&repo_handle.lore_url_base, workspace_id);

        lore_backend
            .switch_branch(&repo_handle.path, branch_name)
            .with_context(|| {
                format!(
                    "Failed to switch to branch '{}' in repository '{}'. \
                     Verify branch exists and repository is in a clean state.",
                    branch_name, repo_handle.id
                )
            })?;

        Ok(())
    }

    /// List branches
    pub async fn list_branches(&self, repo_handle: &RepositoryHandle) -> Result<Vec<String>> {
        info!("Listing branches in repository: {}", repo_handle.id);

        let provider = self.provider_manager.active_provider().context(
            "No active provider configured. Call initialize_provider_selection() first.",
        )?;

        let workspace_id = provider.workspace_id();
        let lore_backend = LoreBackend::from_provider(&repo_handle.lore_url_base, workspace_id);

        let branches = lore_backend
            .list_branches(&repo_handle.path)
            .with_context(|| {
                format!(
                    "Failed to list branches in repository '{}'. \
                     Check repository state and Lore server connectivity.",
                    repo_handle.id
                )
            })?;

        Ok(branches)
    }

    /// Synchronize repository (for cloud/remote providers)
    pub async fn sync(&self, repo_handle: &RepositoryHandle) -> Result<()> {
        info!("Synchronizing repository: {}", repo_handle.id);

        let provider = self.provider_manager.active_provider().context(
            "No active provider configured. Call initialize_provider_selection() first.",
        )?;

        match provider.provider_type() {
            ProviderType::Local => {
                info!("Synchronization not needed for local provider");
                Ok(())
            }
            ProviderType::PortalsCloud | ProviderType::Remote => {
                let workspace_id = provider.workspace_id();
                let lore_backend =
                    LoreBackend::from_provider(&repo_handle.lore_url_base, workspace_id);

                // Record current head before pull for potential rollback
                let head_before_pull = lore_backend.head_hash(&repo_handle.path).ok();

                lore_backend
                    .pull(&repo_handle.path, None, None)
                    .with_context(|| {
                        format!(
                            "Failed to pull changes for repository '{}'. \
                             Check network connectivity and remote server status.",
                            repo_handle.id
                        )
                    })?;

                match lore_backend.push(&repo_handle.path, None, None) {
                    Ok(_) => {
                        info!("Repository synchronized");
                        Ok(())
                    }
                    Err(push_err) => {
                        // Rollback: try to revert to state before pull if we recorded it
                        if let Some(ref old_head) = head_before_pull {
                            tracing::warn!(
                                repo_id = %repo_handle.id,
                                old_head = %old_head,
                                "Push failed after pull, attempting to rollback to previous state"
                            );
                            let rollback_result = lore_backend.revert(&repo_handle.path, old_head);
                            if let Err(revert_err) = rollback_result {
                                tracing::error!(
                                    repo_id = %repo_handle.id,
                                    old_head = %old_head,
                                    error = %revert_err,
                                    "Failed to rollback sync operation - repository may be in inconsistent state"
                                );
                                return Err(anyhow::anyhow!(
                                    "Failed to push changes for repository '{}'. \
                                     Pull was attempted but push failed. \
                                     Attempted to rollback to previous state '{}' but failed. \
                                     Repository may be in inconsistent state. \
                                     Recovery options: \
                                     1. Manually reset to previous state: lore reset --hard {} \
                                     2. Check repository status: lore status \
                                     3. Force sync from remote: lore pull --force \
                                     Original push error: {}. Rollback error: {}",
                                    repo_handle.id,
                                    old_head,
                                    old_head,
                                    push_err,
                                    revert_err
                                ));
                            }
                            return Err(anyhow::anyhow!(
                                "Failed to push changes for repository '{}'. \
                                 Pull was attempted but push failed. \
                                 Successfully rolled back to previous state '{}'. \
                                 Original error: {}",
                                repo_handle.id,
                                old_head,
                                push_err
                            ));
                        }
                        Err(anyhow::anyhow!(
                            "Failed to push changes for repository '{}'. \
                             Pull was attempted but push failed. \
                             Could not rollback (no previous state recorded). \
                             Original error: {}",
                            repo_handle.id,
                            push_err
                        ))
                    }
                }
            }
        }
    }

    /// Delete a repository
    pub async fn delete_repository(&self, repo_handle: &RepositoryHandle) -> Result<()> {
        info!("Deleting repository: {}", repo_handle.id);

        // Remove repository directory
        std::fs::remove_dir_all(&repo_handle.path).with_context(|| {
            format!(
                "Failed to remove repository directory at '{}'. \
                     Check file permissions and ensure no processes are using the repository.",
                repo_handle.path.display()
            )
        })?;

        info!("Repository deleted: {}", repo_handle.id);
        Ok(())
    }

    /// Get the active provider, if any.
    pub fn active_provider(&self) -> Option<&Arc<dyn Provider>> {
        self.provider_manager.active_provider()
    }

    /// Get mutable access to the provider manager (for fallback, etc.).
    pub fn provider_manager_mut(&mut self) -> &mut ProviderManager {
        &mut self.provider_manager
    }

    /// Get the provider manager (read-only).
    pub fn provider_manager(&self) -> &ProviderManager {
        &self.provider_manager
    }

    /// Get provider status
    pub async fn provider_status(&self) -> Result<super::provider::ProviderStatus> {
        let provider = self
            .provider_manager
            .active_provider()
            .context("No active provider configured")?;

        provider.status().await
    }
}

/// Handle to a repository
#[derive(Debug, Clone)]
pub struct RepositoryHandle {
    pub id: String,
    pub workspace_id: String,
    pub path: std::path::PathBuf,
    pub lore_url_base: String,
}

// Re-export CommitInfo from the vcs module (single source of truth).
// repository_api methods return this type; callers should import from here
// or from the vcs module directly.
pub use super::vcs::CommitInfo;

// Re-export fallback functionality
pub use fallback::{FallbackHandler, FallbackResult, FallbackStrategy, RepositoryApiFallback};

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::TempDir;

    #[test]
    fn test_repository_api_creation() {
        let temp_dir = TempDir::new().unwrap();
        let api = RepositoryApi::new(temp_dir.path()).unwrap();
        assert_eq!(api.nap_home, temp_dir.path());
    }

    #[test]
    fn test_repository_handle() {
        let handle = RepositoryHandle {
            id: "test-repo".to_string(),
            workspace_id: "default".to_string(),
            path: std::path::PathBuf::from("/tmp/test-repo"),
            lore_url_base: "lore://localhost:41337".to_string(),
        };

        assert_eq!(handle.id, "test-repo");
        assert_eq!(handle.workspace_id, "default");
    }
}