khive_runtime/atomic_prepare/
embedding_outcome.rs1use super::{apply_one_post_commit_effect, KhiveRuntime, NamespaceToken};
2use crate::atomic_plan::PostCommitEffect;
3use crate::atomic_runner::CommittedPostCommitEffects;
4use crate::error::RuntimeError;
5use crate::retrieval::EmbeddingTruncationReport;
6
7#[derive(Clone, Copy, Debug, PartialEq, Eq)]
9pub enum ReindexModelStage {
10 Embedding,
11 VectorValidation,
12 VectorStore,
13 VectorPublication,
14}
15
16impl ReindexModelStage {
17 pub const fn as_str(self) -> &'static str {
18 match self {
19 Self::Embedding => "embedding",
20 Self::VectorValidation => "vector_validation",
21 Self::VectorStore => "vector_store",
22 Self::VectorPublication => "vector_publication",
23 }
24 }
25}
26
27#[derive(Clone, Debug, PartialEq, Eq)]
29pub struct ReindexModelFailure {
30 pub model: String,
31 pub stage: ReindexModelStage,
32 pub error: String,
33}
34
35#[derive(Clone, Debug, PartialEq, Eq)]
37pub struct PostCommitEmbeddingOutcome {
38 pub effect: PostCommitEffect,
39 pub truncation: EmbeddingTruncationReport,
40 pub failures: Vec<ReindexModelFailure>,
43}
44
45#[derive(Clone, Debug, PartialEq, Eq)]
49pub struct PostCommitEffectsReport {
50 pub outcomes: Vec<PostCommitEmbeddingOutcome>,
52 pub failures: Vec<String>,
54 pub failed_effects: Vec<(PostCommitEffect, String)>,
57}
58
59impl PostCommitEffectsReport {
60 pub fn failure_error(&self) -> Option<RuntimeError> {
63 if self.failures.is_empty() {
64 return None;
65 }
66 Some(RuntimeError::Internal(format!(
67 "post-commit effects failed after commit: {}",
68 self.failures.join("; ")
69 )))
70 }
71}
72
73pub async fn apply_post_commit_effects_with_failures(
78 runtime: &KhiveRuntime,
79 token: &NamespaceToken,
80 effects: CommittedPostCommitEffects,
81) -> PostCommitEffectsReport {
82 let mut report = PostCommitEffectsReport {
83 outcomes: Vec::new(),
84 failures: Vec::new(),
85 failed_effects: Vec::new(),
86 };
87 for (index, effect) in effects.into_effects().into_iter().enumerate() {
88 let identity = format!("{effect:?}");
89 let attempted = effect.clone();
90 match apply_one_post_commit_effect(runtime, token, effect).await {
91 Ok(Some(outcome)) => report.outcomes.push(outcome),
92 Ok(None) => {}
93 Err(error) => {
94 report
95 .failures
96 .push(format!("effect[{index}] {identity}: {error}"));
97 report.failed_effects.push((attempted, error.to_string()));
98 }
99 }
100 }
101 report
102}