Skip to main content

khive_request/
atomic.rs

1//! Pure ADR-099 atomic-admissibility preflight over parsed operations.
2
3use khive_types::pack::{atomic_admissibility, AtomicRejectionReason, ATOMIC_ADMISSIBLE_VERBS};
4
5use crate::types::ParsedOp;
6
7/// One atomic-preflight rejection.
8///
9/// See `crates/khive-request/docs/api/atomic-admissibility.md` for reason semantics.
10#[derive(Debug, Clone, PartialEq, Eq)]
11pub struct AtomicRejection {
12    /// Zero-based operation position.
13    pub op_index: usize,
14    /// Rejected tool name.
15    pub tool: String,
16    /// Static admissibility category.
17    pub reason: AtomicRejectionReason,
18}
19
20impl std::fmt::Display for AtomicRejection {
21    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
22        let why: std::borrow::Cow<'static, str> = match self.reason {
23            AtomicRejectionReason::EmbeddingBearing => {
24                "embedding-bearing verbs are not yet atomic-eligible".into()
25            }
26            AtomicRejectionReason::Read => "read verbs have no write plan to apply".into(),
27            AtomicRejectionReason::Unlisted => "not on the v1 atomic-admissible verb list".into(),
28            AtomicRejectionReason::KnownUnimplemented if self.tool == "merge" => {
29                "merge is not yet supported under --atomic (admissible per ADR-099 D3, but \
30                 full-parity field-folding/index-cleanup/edge-conflict-resolution is deferred \
31                 this slice); use the non-atomic merge verb instead"
32                    .into()
33            }
34            AtomicRejectionReason::KnownUnimplemented => format!(
35                "admissible per ADR-099 D3 but has no --atomic prepare/apply seam implemented \
36                 in this slice yet; use the non-atomic {:?} verb instead",
37                self.tool
38            )
39            .into(),
40        };
41        write!(
42            f,
43            "op {} (`{}`) is not atomic-admissible: {}. Admissible verbs: {}",
44            self.op_index,
45            self.tool,
46            why,
47            ATOMIC_ADMISSIBLE_VERBS.join(", ")
48        )
49    }
50}
51
52/// Checks every tool name against the ADR-099 v1 atomic policy.
53///
54/// Returns every rejection in input order; an empty vector means every op is
55/// admissible. This function performs no I/O or execution and must run before
56/// an atomic prepare pass.
57/// See `crates/khive-request/docs/api/atomic-admissibility.md` for policy boundaries.
58pub fn check_atomic_admissible(ops: &[ParsedOp]) -> Vec<AtomicRejection> {
59    ops.iter()
60        .enumerate()
61        .filter_map(|(op_index, op)| {
62            atomic_admissibility(&op.tool).map(|reason| AtomicRejection {
63                op_index,
64                tool: op.tool.clone(),
65                reason,
66            })
67        })
68        .collect()
69}
70
71#[cfg(test)]
72#[path = "atomic_tests.rs"]
73mod tests;