Skip to main content

khive_runtime/atomic_prepare/
embedding_outcome.rs

1use 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/// The eligible-model stage that prevented a note's vector refresh.
8#[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/// An eligible-model failure after the note and its lexical index committed.
28#[derive(Clone, Debug, PartialEq, Eq)]
29pub struct ReindexModelFailure {
30    pub model: String,
31    pub stage: ReindexModelStage,
32    pub error: String,
33}
34
35/// Metadata produced by a reindex effect whose fail-closed stages succeeded.
36#[derive(Clone, Debug, PartialEq, Eq)]
37pub struct PostCommitEmbeddingOutcome {
38    pub effect: PostCommitEffect,
39    pub truncation: EmbeddingTruncationReport,
40    /// Partial vector failures do not undo a committed mutation. Report-aware
41    /// callers can disclose them while legacy callers retain best-effort success.
42    pub failures: Vec<ReindexModelFailure>,
43}
44
45/// Everything one committed unit's post-commit pass produced. A failing effect
46/// never discards the outcomes of the effects that completed, so callers can
47/// still disclose their model failures and truncation advisories.
48#[derive(Clone, Debug, PartialEq, Eq)]
49pub struct PostCommitEffectsReport {
50    /// Outcomes of the reindex effects that completed, in effect order.
51    pub outcomes: Vec<PostCommitEmbeddingOutcome>,
52    /// One `effect[<index>] <effect>: <error>` entry per effect that failed.
53    pub failures: Vec<String>,
54    /// Each failed effect beside its error, in effect order, so a caller can
55    /// name the row an error belongs to.
56    pub failed_effects: Vec<(PostCommitEffect, String)>,
57}
58
59impl PostCommitEffectsReport {
60    /// The aggregate error for the effects that failed, or `None` when every
61    /// effect completed.
62    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
73/// Failure-preserving form of [`super::apply_post_commit_effects_with_report`].
74/// Runs every effect, collecting the outcomes of those that completed and the
75/// error of each that did not, instead of replacing the outcomes with the
76/// first failure.
77pub 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}