| 4321 | } |
| 4322 | |
| 4323 | async fn maintain_knowledge_graph( |
| 4324 | &self, |
| 4325 | request: Request<MaintainKnowledgeGraphRequest>, |
| 4326 | ) -> Result<Response<MaintainKnowledgeGraphResponse>, Status> { |
| 4327 | let request = request.into_inner(); |
| 4328 | let action = KnowledgeMaintainAction::try_from(request.action) |
| 4329 | .unwrap_or(KnowledgeMaintainAction::Unspecified); |
| 4330 | let handle = self.state.resolve(request.repository.as_ref().unwrap())?; |
| 4331 | self.state.log_rpc("MaintainKnowledgeGraph", Some(&handle)); |
| 4332 | let gate = handle.exclusive_owned().await; |
| 4333 | if action == KnowledgeMaintainAction::Reindex && !request.signer_auth_headers.is_empty() { |
| 4334 | let repo = handle.repository()?; |
| 4335 | let (_, remote) = repo.get_default_remote().map_err(repository_error)?; |
| 4336 | if request.signer_remote_url.as_deref() != Some(remote.url.as_str()) { |
| 4337 | return Err(Status::failed_precondition( |
| 4338 | "signer discovery remote changed; retry reindex with fresh credentials", |
| 4339 | )); |
| 4340 | } |
| 4341 | let mut config = atomic_remote::HttpRemoteConfig::new(); |
| 4342 | for (name, value) in &request.signer_auth_headers { |
| 4343 | config = config.with_header(name, value); |
| 4344 | } |
| 4345 | if let Some(client) = crate::signer_refresh::client_for_remote(&remote.url, &config) { |
| 4346 | crate::signer_refresh::refresh(&repo, &client) |
| 4347 | .await |
| 4348 | .map_err(repository_error)?; |
| 4349 | } |
| 4350 | } |
| 4351 | let meta = request.meta.clone(); |
| 4352 | let response = tokio::task::spawn_blocking(move || { |
| 4353 | let _gate = gate; |
| 4354 | let (repo, workspace) = |
| 4355 | handle.workspace_repository(atomic_repository::WorkspaceTxnMode::Reconcile)?; |
| 4356 | let working_copy = workspace.working_copy(); |
| 4357 | let processed = match action { |
| 4358 | KnowledgeMaintainAction::Enrich => { |
| 4359 | // Per-change scope (`enrich --changes`): enrich only |
| 4360 | // the named changes' nodes. |
| 4361 | if !request.changes.is_empty() { |
| 4362 | let mut total: u64 = 0; |
| 4363 | for hash in &request.changes { |
| 4364 | if hash.value.len() != 32 { |
| 4365 | return Err(domain_status( |
| 4366 | ErrorCode::InvalidArgument, |
| 4367 | "hash must be 32 bytes", |
| 4368 | )); |
| 4369 | } |
| 4370 | let mut bytes = [0u8; 32]; |
| 4371 | bytes.copy_from_slice(&hash.value); |
| 4372 | repo.kg_enrich_change(working_copy, &atomic_core::types::Merkle(bytes)) |
| 4373 | .map_err(repository_error)?; |
| 4374 | total += 1; |
| 4375 | } |
| 4376 | return Ok::<_, Status>(MaintainKnowledgeGraphResponse { |
| 4377 | processed: total, |
| 4378 | provider: None, |
| 4379 | dimensions: None, |
| 4380 | path: None, |