| 2662 | } |
| 2663 | |
| 2664 | async fn init_vault( |
| 2665 | &self, |
| 2666 | request: Request<InitVaultRequest>, |
| 2667 | ) -> Result<Response<InitVaultResponse>, Status> { |
| 2668 | // `atomic vault init` — inside an EXISTING repository (it requires |
| 2669 | // one), so it routes normally: has_vault → init_vault → track the |
| 2670 | // .vault tree → record the defaults (the local body's exact flow). |
| 2671 | let request = request.into_inner(); |
| 2672 | let handle = self.state.resolve(request.repository.as_ref().unwrap())?; |
| 2673 | self.state.log_rpc("InitVault", Some(&handle)); |
| 2674 | let gate_handle = handle.clone(); |
| 2675 | let _gate = gate_handle.exclusive().await; |
| 2676 | let meta = request.meta.clone(); |
| 2677 | let (already, vault_dir, recorded) = tokio::task::spawn_blocking(move || { |
| 2678 | let (repo, workspace) = |
| 2679 | handle.workspace_repository(atomic_repository::WorkspaceTxnMode::Reconcile)?; |
| 2680 | let working_copy = workspace.working_copy(); |
| 2681 | if repo.has_vault().unwrap_or(false) { |
| 2682 | return Ok::<_, Status>((true, None, false)); |
| 2683 | } |
| 2684 | repo.init_vault().map_err(repository_error)?; |
| 2685 | let vault_dir = repo.vault_dir().display().to_string(); |
| 2686 | // Track every vault file (the local body's recursive add). |
| 2687 | if repo.vault_dir().exists() { |
| 2688 | add_vault_files_recursive(&repo, working_copy, &repo.vault_dir())?; |
| 2689 | } |
| 2690 | // Record the defaults as their own change; NothingToRecord is |
| 2691 | // the local body's silent no-op, other failures only log. |
| 2692 | let header = atomic_core::change::ChangeHeader::new("Initialize vault"); |
| 2693 | let options = atomic_repository::RecordOptions::new() |
| 2694 | .add_path(".vault") |
| 2695 | .detect_raw_renames(false); |
| 2696 | let recorded = match repo.record(working_copy, header, options) { |
| 2697 | Ok(_) => true, |
| 2698 | Err(atomic_repository::RecordError::NothingToRecord) => false, |
| 2699 | Err(_) => false, |
| 2700 | }; |
| 2701 | Ok((false, Some(vault_dir), recorded)) |
| 2702 | }) |
| 2703 | .await |
| 2704 | .map_err(|e| Status::internal(e.to_string()))??; |
| 2705 | Ok(Response::new(InitVaultResponse { |
| 2706 | already_initialized: already, |
| 2707 | vault_dir, |
| 2708 | recorded, |
| 2709 | meta: response_meta(&meta), |
| 2710 | })) |
| 2711 | } |
| 2712 | |
| 2713 | async fn get_vault_context( |
| 2714 | &self, |