use smos_domain::enums::FactStatus;
use smos_domain::{Fact, NliResult};
use crate::log_nonfatal;
use crate::ports::{FactRepository, NliClassifier, SessionRepository};
use super::FinalizeSession;
use super::outcome::FactOutcome;
impl<'a, FR, SR, NC> FinalizeSession<'a, FR, SR, NC>
where
FR: FactRepository,
SR: SessionRepository,
NC: NliClassifier,
{
pub(crate) async fn apply_conflict_flag(
&self,
pending: &Fact,
existing: &Fact,
pool: &mut Vec<Fact>,
) -> FactOutcome {
let mut existing_mut = existing.clone();
let mut pending_mut = pending.clone();
log_nonfatal!(
existing_mut.flag_conflict_bidirectional(&mut pending_mut),
existing = %existing_mut.id(),
pending = %pending_mut.id(),
"flag_conflict_bidirectional failed"
);
log_nonfatal!(
self.facts.save(&existing_mut).await,
fact = %existing_mut.id(),
"save existing after flag failed"
);
if let Err(e) = self.facts.save(&pending_mut).await {
tracing::warn!(fact = %pending_mut.id(), error = %e, "save pending after flag failed");
return FactOutcome::Skipped;
}
pool.push(pending.clone());
FactOutcome::Conflict
}
pub(crate) async fn apply_merge(
&self,
pending: &Fact,
existing: &Fact,
nli: &NliResult,
pool: &mut Vec<Fact>,
) -> FactOutcome {
let mut existing_mut = existing.clone();
log_nonfatal!(
existing_mut.merge_into(pending),
fact = %existing_mut.id(),
"merge_into failed"
);
log_nonfatal!(
existing_mut.reclassify(Some(nli), self.confidence_cfg),
fact = %existing_mut.id(),
"reclassify(existing) failed"
);
if let Err(e) = self.facts.save(&existing_mut).await {
tracing::warn!(fact = %existing_mut.id(), error = %e, "save merged existing failed");
return FactOutcome::Skipped;
}
let mut pending_mut = pending.clone();
if let Err(e) = pending_mut.set_status_and_confidence(
FactStatus::Rejected,
pending_mut.confidence(),
self.confidence_cfg,
) {
tracing::warn!(fact = %pending_mut.id(), error = %e, "reject pending twin failed");
} else if let Err(e) = self.facts.save(&pending_mut).await {
tracing::warn!(fact = %pending_mut.id(), error = %e, "save rejected pending failed");
}
pool.push(existing_mut);
FactOutcome::Merged
}
pub(crate) async fn finalize_standalone(
&self,
pending: &Fact,
nli: Option<&NliResult>,
pool: &mut Vec<Fact>,
) -> FactOutcome {
let mut fact = pending.clone();
log_nonfatal!(
fact.reclassify(nli, self.confidence_cfg),
fact = %fact.id(),
"reclassify(standalone) failed"
);
if let Err(e) = self.facts.save(&fact).await {
tracing::warn!(fact = %fact.id(), error = %e, "save standalone failed");
return FactOutcome::Skipped;
}
pool.push(fact);
FactOutcome::Finalized
}
}