use super::{apply_one_post_commit_effect, KhiveRuntime, NamespaceToken};
use crate::atomic_plan::PostCommitEffect;
use crate::atomic_runner::CommittedPostCommitEffects;
use crate::error::RuntimeError;
use crate::retrieval::EmbeddingTruncationReport;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ReindexModelStage {
Embedding,
VectorValidation,
VectorStore,
VectorPublication,
}
impl ReindexModelStage {
pub const fn as_str(self) -> &'static str {
match self {
Self::Embedding => "embedding",
Self::VectorValidation => "vector_validation",
Self::VectorStore => "vector_store",
Self::VectorPublication => "vector_publication",
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ReindexModelFailure {
pub model: String,
pub stage: ReindexModelStage,
pub error: String,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PostCommitEmbeddingOutcome {
pub effect: PostCommitEffect,
pub truncation: EmbeddingTruncationReport,
pub failures: Vec<ReindexModelFailure>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PostCommitEffectsReport {
pub outcomes: Vec<PostCommitEmbeddingOutcome>,
pub failures: Vec<String>,
}
impl PostCommitEffectsReport {
pub fn failure_error(&self) -> Option<RuntimeError> {
if self.failures.is_empty() {
return None;
}
Some(RuntimeError::Internal(format!(
"post-commit effects failed after commit: {}",
self.failures.join("; ")
)))
}
}
pub async fn apply_post_commit_effects_with_failures(
runtime: &KhiveRuntime,
token: &NamespaceToken,
effects: CommittedPostCommitEffects,
) -> PostCommitEffectsReport {
let mut report = PostCommitEffectsReport {
outcomes: Vec::new(),
failures: Vec::new(),
};
for (index, effect) in effects.into_effects().into_iter().enumerate() {
let identity = format!("{effect:?}");
match apply_one_post_commit_effect(runtime, token, effect).await {
Ok(Some(outcome)) => report.outcomes.push(outcome),
Ok(None) => {}
Err(error) => report
.failures
.push(format!("effect[{index}] {identity}: {error}")),
}
}
report
}