//! `mint`'s KINDS hook — slice 5a-ii's seam (store-erase): the five kinds of //! delete (design 6.2), checked on each change before it is stamped. //! //! - A retract names its kind of delete ([`Delete`]), and nothing else does. //! - Fact: a fact table refuses every delete, here as in the fold. //! - A write at an erased id, or under an erased owning parent, is refused //! locally with the reason it would park `gone` under on arrival. //! - No act retracts an owning child together with an owning ancestor of it //! (3.5 point 3): the ancestor's retract takes the child; a child retract //! beside it would be dropped below T, or, on a table with no history, be //! an undo that cannot name the child's columns. //! //! - A take-back (Undo of an add) is judged here, and no erase borrows its //! reason ([`crate::take_back`]). //! //! Still to come in 5a-ii: the kind a dynamic sink declares per table. use rusqlite::types::Value as SqlValue; use serde_json::Value as JsonValue; use super::{Act, Change, Delete}; use crate::canonical::Kind; use crate::driver::Driver; use crate::error::{Result, StoreError}; use crate::schema::specs::spec_for; use crate::schema::table_specs::OWNING_REFS; pub(crate) fn check(db: &Driver, act: &Act, change: &Change) -> Result<()> { if change.kind == Kind::Retract && change.table == crate::revocation::DEVICES_SUBJECT { return Err(StoreError::refused("Devices are removed by revoking their key; the approval row remains as the revocation audit")); } if change.kind == Kind::Retract && change.delete.is_none() { return Err(StoreError::refused(format!( "`{}` deletes {} {} without naming which kind of delete it is — a retract, an erase, a source-erase or a revocation", act.verb, change.table, change.row_id ))); } if change.kind != Kind::Retract && change.delete.is_some() { return Err(StoreError::refused(format!( "`{}` names a kind of delete on a {} of {} {} — only a delete has one", act.verb, change.kind.as_str(), change.table, change.row_id ))); } if change.kind == Kind::Retract && crate::fact_hooks::is_fact_table(&change.table) && !crate::fact_hooks::erases_history(&change.table, change.delete.as_ref().and_then(|d| d.erase_reason()).is_some()) { return Err(StoreError::refused(format!( "`{}`: {} holds facts, and a fact is never deleted — {} stays", act.verb, change.table, change.row_id ))); } if crate::erase::is_gone(db, &change.table, &change.row_id)? { return Err(StoreError::refused(format!( "{} `{}` writes {} {}, which was erased — a later write at its id is never applied", crate::erase::GONE_REFUSAL, act.verb, change.table, change.row_id ))); } match &change.delete { Some(Delete::Erase { reason }) if reason == crate::take_back::REASON => { return Err(StoreError::refused(format!( "`{}` erases {} {} as taken back without being a take-back — only Undo of an add names that reason, and it is judged", act.verb, change.table, change.row_id ))); } Some(Delete::TakeBack { through }) => { if let Some(why) = crate::take_back::refusal(db, &act.actor, &change.table, &change.row_id, through)? { return Err(StoreError::refused(format!( "`{}` may not take back {} {}: {why}", act.verb, change.table, change.row_id ))); } } _ => {} } if change.kind == Kind::Retract { if let Some((table, row_id)) = retracted_ancestor(db, act, change, 0)? { return Err(StoreError::refused(format!( "`{}` deletes {} {} together with {table} {row_id}, which owns it — the owner's delete takes it, so the act names only the owner", act.verb, change.table, change.row_id ))); } } Ok(()) } /// The owning ancestor of the deleted row that this act also deletes, if /// any. The parent is read from the row, or from its receipts when it is /// dormant. Whatever kinds the two deletes name. fn retracted_ancestor(db: &Driver, act: &Act, change: &Change, depth: usize) -> Result> { ancestor_in_act(db, act, &change.table, &change.row_id, depth) } fn ancestor_in_act( db: &Driver, act: &Act, table: &str, row_id: &str, depth: usize, ) -> Result> { // An owning chain is a handful deep; a self-owning table (comments, // feedback) is walked to its root and no further. if depth > 64 { return Ok(None); } for (child, column, parent) in OWNING_REFS { if *child != table { continue; } let Some(parent_id) = parent_of(db, table, column, row_id)? else { continue; }; if parent_id == row_id && parent == child { continue; } let retracted = act.changes.iter().any(|other| { other.kind == Kind::Retract && other.table == *parent && other.row_id == parent_id }); if retracted { return Ok(Some((parent.to_string(), parent_id))); } if let Some(found) = ancestor_in_act(db, act, parent, &parent_id, depth + 1)? { return Ok(Some(found)); } } Ok(None) } fn parent_of(db: &Driver, table: &str, column: &str, row_id: &str) -> Result> { let Some(spec) = spec_for(table) else { return Ok(None); }; if let Some(row) = db.get( &format!("SELECT {column} AS parent FROM {table} WHERE \"{}\" = ?", spec.pk), &[SqlValue::Text(row_id.to_string())], )? { return Ok(row.text("parent").map(str::to_string)); } Ok(crate::receipts::dormant_fields(db, table, row_id)? .get(column) .and_then(JsonValue::as_str) .map(str::to_string)) } #[cfg(test)] mod removal_guards { use super::*; /// RULE: Removing a device cannot retract its approval and leave its key usable; the approval stays as a revocation audit and removal must use the revocation writer. #[test] fn device_removal_never_retracts_the_approval() { let db = crate::test_support::seeded_file("device:aaaa"); for delete in [Delete::Retract, Delete::Revocation, Delete::Erase { reason: "bin".into() }] { let change = Change::delete("cotton_devices", "synthetic-approval", delete); let act = Act { actor: crate::fixtures::fixture_actor(), verb: "remove".into(), history: super::super::History::None, changes: vec![change.clone()] }; let refusal = check(&db, &act, &change).expect_err("device removal must revoke, never delete the approval"); assert!(refusal.to_string().contains("revok"), "{refusal}"); } } }