use khive_storage::note::Note;
use super::{KhiveRuntime, NamespaceToken, PostCommitEffect, PostCommitEmbeddingOutcome};
use crate::curation::note_reindex::NoteReindexReport;
use crate::error::RuntimeResult;
#[cfg(test)]
use crate::retrieval::EmbeddingTruncationReport;
use uuid::Uuid;
pub(super) async fn apply(
runtime: &KhiveRuntime,
token: &NamespaceToken,
note_id: Uuid,
version: i64,
) -> RuntimeResult<Option<PostCommitEmbeddingOutcome>> {
let Some(note) = runtime.notes(token)?.get_note(note_id).await? else {
return Ok(None);
};
if note.version != version {
return Ok(None);
}
let reindex = runtime.reindex_note_with_report(token, ¬e).await;
notify_if_current(runtime, token, ¬e, reindex).await
}
async fn notify_if_current(
runtime: &KhiveRuntime,
token: &NamespaceToken,
note: &Note,
reindex: RuntimeResult<impl Into<NoteReindexReport>>,
) -> RuntimeResult<Option<PostCommitEmbeddingOutcome>> {
let current = match runtime.notes(token)?.get_note(note.id).await {
Ok(current) => current,
Err(error) => return reindex.and(Err(error.into())),
};
if current.is_none_or(|current| current.version != note.version) {
return reindex.map(|_| None);
}
runtime.fire_note_mutation_hook(¬e.kind, note.id).await;
reindex.map(|report| {
let report = report.into();
Some(PostCommitEmbeddingOutcome {
effect: PostCommitEffect::ReindexNote {
note_id: note.id,
version: note.version,
},
truncation: report.truncation,
failures: report.failures,
})
})
}
#[cfg(test)]
#[path = "note_reindex_effect_tests.rs"]
mod tests;