mushroomdb 0.7.0

Embedded graph database with Cypher queries, rule triggers, and Arrow export
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
//! `forget` — tombstone a node, remove one property, or retract one fact, and
//! say what is left behind.
//!
//! The decision and the report live here so that every surface gives the
//! same answer: the MCP `forget` tool renders [`ForgetReport`] as prose, and
//! the Python binding returns it as a dict. Before 0.7's last plan this was
//! private to the MCP server.
//!
//! A tombstone is not a redaction. History still holds what was forgotten
//! until the log is pruned, and a note that stated a forgotten fact still
//! says it: the report lists those notes and deletes none of them.

use crate::digest::sanitize;
use crate::memory::identity::{
    identity_props_after_forgetting_name, ALIASES_FIELD, ALIAS_KEYS_FIELD,
};
use crate::memory_schema::NAME_FIELD;
use crate::{GraphDb, PredicateSummary, RuleDef};
use core_rules::engine::DEFAULT_MAX_EDGES;
use core_rules::{evaluate, NodeView};
use core_storage::fs::Fs;
use core_storage::{namespace_of_value, Direction, GraphError, Result, NS_PROP};
use std::collections::BTreeSet;

/// Notes a report names when what it forgot was written about; the rest are
/// counted in [`ForgetReport::notes_total`].
pub const FORGET_NOTE_LIST: usize = 10;

/// The refusal for a call that is not exactly one of the three shapes.
pub const FORGET_SHAPE: &str = "pass exactly one of: key (forget a node), key and prop \
     (forget one property), or fact {subject, predicate, object} (retract one edge)";

/// What to forget: exactly one of a node, one property, or one fact.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ForgetTarget {
    /// Tombstone the node and every edge on it.
    Node { key: String },
    /// Remove one property from a node.
    Prop { key: String, prop: String },
    /// Retract one hand-written edge, `(predicate, subject, object)`.
    Fact {
        subject: String,
        predicate: String,
        object: String,
    },
}

impl ForgetTarget {
    /// The target three optional arguments name, or `None` when they are not
    /// exactly one of the three shapes — the caller answers [`FORGET_SHAPE`].
    #[must_use]
    pub fn from_parts(
        key: Option<String>,
        prop: Option<String>,
        fact: Option<(String, String, String)>,
    ) -> Option<Self> {
        match (key, prop, fact) {
            (Some(key), None, None) => Some(Self::Node { key }),
            (Some(key), Some(prop), None) => Some(Self::Prop { key, prop }),
            (None, None, Some((subject, predicate, object))) => Some(Self::Fact {
                subject,
                predicate,
                object,
            }),
            _ => None,
        }
    }
}

/// What one `forget` call did.
#[derive(Debug, Clone, PartialEq, serde::Serialize)]
pub struct ForgetReport {
    /// `node`, `prop` or `fact`.
    pub mode: &'static str,
    /// The node, property or edge named, as the caller named it.
    pub target: String,
    /// False when there was nothing to forget, and nothing was written.
    pub changed: bool,
    /// Edges removed with a node: written by hand, and derived by rules. For a
    /// property, `derived_edges` counts the rule-derived edges on the node that
    /// the removal retracted, because a rule read that property.
    pub manual_edges: u64,
    pub derived_edges: u64,
    /// The property removed, in `prop` mode.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub prop: Option<String>,
    /// Notes with an `ABOUT` edge to the node, or to both ends of the fact.
    /// Listed, never deleted: their text still says what was forgotten.
    pub notes: Vec<String>,
    pub notes_total: usize,
    /// The first commit history still answers from.
    pub history_floor: u64,
    /// `prop` mode removed `name` from a node that carries `aliases`, and
    /// rewrote that list in the same write: the name's words left it.
    pub aliases_rewritten: bool,
    /// `prop` mode removed `aliases` from a node that still carries
    /// `alias_keys`, the aliases it declared, which still link stubs.
    pub alias_keys_remain: bool,
}

/// Every field a predicate reads, its parts' included, sorted and deduped.
#[must_use]
pub fn predicate_fields(p: &PredicateSummary) -> Vec<String> {
    let mut out: BTreeSet<String> = p.fields.iter().cloned().collect();
    for part in p.parts.iter().flatten() {
        out.extend(predicate_fields(part));
    }
    out.into_iter().collect()
}

/// Notes with an `ABOUT` edge to every one of `keys`, sorted.
fn notes_about<F: Fs>(db: &GraphDb<F>, keys: &[&str]) -> Vec<String> {
    let mut common: Option<BTreeSet<String>> = None;
    for key in keys {
        let into: BTreeSet<String> = db
            .neighbors(key, "ABOUT", Direction::In)
            .unwrap_or_default()
            .into_iter()
            .filter(|k| db.node_ref(k).is_some_and(|n| n.label() == "Note"))
            .collect();
        common = Some(match common {
            None => into,
            Some(c) => c.intersection(&into).cloned().collect(),
        });
    }
    common.unwrap_or_default().into_iter().collect()
}

/// An edge as `(edge_type, src_key, dst_key)`.
type EdgeTriple = (String, String, String);

/// The rule-derived edges incident on `key`, both directions, each once.
fn derived_edges_on<F: Fs>(db: &GraphDb<F>, key: &str) -> Result<BTreeSet<EdgeTriple>> {
    Ok(db
        .node_edges(key)?
        .into_iter()
        .filter(|e| e.derived)
        .map(|e| (e.edge_type, e.src_key, e.dst_key))
        .collect())
}

/// Forget `target`, in one call on the one handle, so nothing can change
/// between what the report says and what was done.
///
/// # Errors
/// - [`GraphError::KeyNotFound`] for a node or a fact endpoint that does not
///   exist. Nothing is written.
/// - [`GraphError::RuleOwned`] for a fact that is in the store and that the
///   engine will not delete: a rule derived it, or it was written by hand
///   and a rule matches the pair. `detail` is the whole refusal
///   ([`rule_owned_refusal`]): which of the two, the rule and the fields it
///   reads, or, when no rule can be named, the engine's own detail,
///   sanitized. Nothing is written. A fact that is not in the store is never
///   this error: it is `changed: false`, whatever a rule says of the pair.
/// - Any other engine refusal, as it came.
pub fn forget<F: Fs>(db: &mut GraphDb<F>, target: &ForgetTarget) -> Result<ForgetReport> {
    match target {
        ForgetTarget::Node { key } => {
            if !db.has_node(key) {
                return Err(GraphError::KeyNotFound { key: key.clone() });
            }
            let notes = notes_about(db, &[key.as_str()]);
            let label = db
                .node_ref(key)
                .map(|n| n.label().to_string())
                .unwrap_or_default();
            let deleted = db.delete_node(key)?;
            Ok(ForgetReport {
                mode: "node",
                target: format!("{key} ({label})"),
                changed: true,
                manual_edges: deleted.manual_edges,
                derived_edges: deleted.derived_edges,
                prop: None,
                notes_total: notes.len(),
                notes: notes.into_iter().take(FORGET_NOTE_LIST).collect(),
                history_floor: db.stats().history_floor,
                aliases_rewritten: false,
                alias_keys_remain: false,
            })
        }
        ForgetTarget::Prop { key, prop } => {
            // Removing a property re-runs rule retraction, so a rule that read
            // it drops the edges it derived. Compare the node's derived edges
            // either side of the write to say how many went.
            let before = derived_edges_on(db, key)?;
            // A name's words live in `aliases`. When the name goes, the list is
            // rewritten from the key alone in the same commit, so the words
            // leave with it. A node with no `aliases` list, or one holding a
            // value the store cannot read as a list, is left as it is.
            let rewrite = if prop == NAME_FIELD && db.get_prop(key, prop).is_some() {
                identity_props_after_forgetting_name(db, key).unwrap_or_default()
            } else {
                Vec::new()
            };
            let changed = if rewrite.is_empty() {
                db.remove_prop(key, prop)?
            } else {
                let mut batch = db.batch();
                batch.remove_prop(key, prop);
                for (field, value) in &rewrite {
                    batch.set_prop(key, field, value.clone());
                }
                batch.commit().map(|_| true)?
            };
            let aliases_rewritten = changed && !rewrite.is_empty();
            let alias_keys_remain =
                changed && prop == ALIASES_FIELD && db.get_prop(key, ALIAS_KEYS_FIELD).is_some();
            let retracted = if changed {
                let after = derived_edges_on(db, key).unwrap_or_default();
                before.difference(&after).count() as u64
            } else {
                0
            };
            Ok(ForgetReport {
                mode: "prop",
                target: format!("{key}.{prop}"),
                changed,
                manual_edges: 0,
                derived_edges: retracted,
                prop: Some(prop.clone()),
                notes: Vec::new(),
                notes_total: 0,
                history_floor: db.stats().history_floor,
                aliases_rewritten,
                alias_keys_remain,
            })
        }
        ForgetTarget::Fact {
            subject,
            predicate,
            object,
        } => {
            let changed = match db.delete_edge(predicate, subject, object) {
                Ok(c) => c,
                // The engine's guard refuses before it looks for the edge
                // (ledger row 67), so a fact nobody stated can come back
                // `RuleOwned`. An edge that is not there is nothing to
                // retract, whatever a rule says about the pair.
                Err(GraphError::RuleOwned { .. }) if !has_edge(db, predicate, subject, object) => {
                    false
                }
                Err(GraphError::RuleOwned { detail }) => {
                    return Err(GraphError::RuleOwned {
                        detail: rule_owned_refusal(db, predicate, subject, object, &detail),
                    })
                }
                Err(e) => return Err(e),
            };
            let notes = if changed {
                notes_about(db, &[subject.as_str(), object.as_str()])
            } else {
                Vec::new()
            };
            Ok(ForgetReport {
                mode: "fact",
                target: format!("{predicate} {subject} → {object}"),
                changed,
                manual_edges: 0,
                derived_edges: 0,
                prop: None,
                notes_total: notes.len(),
                notes: notes.into_iter().take(FORGET_NOTE_LIST).collect(),
                history_floor: db.stats().history_floor,
                aliases_rewritten: false,
                alias_keys_remain: false,
            })
        }
    }
}

/// Whether the edge `(predicate, subject, object)` is in the store.
fn has_edge<F: Fs>(db: &GraphDb<F>, predicate: &str, subject: &str, object: &str) -> bool {
    db.neighbors(subject, predicate, Direction::Out)
        .is_ok_and(|keys| keys.iter().any(|k| k == object))
}

/// Why a fact edge that exists cannot be retracted, in a sentence that is
/// true of this edge.
///
/// The engine refuses the delete in two cases, and they are not the same
/// fact:
///
/// - **A rule derived the edge.** It is in provenance, and the refusal names
///   exactly the rules [`GraphDb::explain`] attributes it to.
/// - **The edge was written by hand and a rule matches the pair.** It is in
///   no provenance, so no rule is said to have derived it. The refusal names
///   the rules the engine's guard refused for — `guard_matches` below, the
///   same test — and not every rule that shares the edge type and the
///   endpoint labels. It says those rules *would derive it again* only when
///   every one of them would (`would_derive`); the guard asks less than
///   that, so otherwise it says they match the two nodes' properties and may.
///
/// When neither names a rule, the engine's own `detail` is returned,
/// sanitized.
///
/// [`forget`] calls this only for an edge that is in the store: an absent one
/// is nothing to retract, and "was written by hand" would be false of it.
pub fn rule_owned_refusal<F: Fs>(
    db: &GraphDb<F>,
    predicate: &str,
    subject: &str,
    object: &str,
    detail: &str,
) -> String {
    // Which rule derived this edge is something the engine knows: `explain`
    // answers from provenance, one entry per derived edge between the two
    // keys. Matching on edge type and labels alone names every look-alike —
    // every rule of the identity preset derives `SAME_AS`.
    let by_provenance: BTreeSet<String> = db
        .explain(subject, object)
        .unwrap_or_default()
        .into_iter()
        .filter(|e| e.edge_type == predicate && e.src_key == subject && e.dst_key == object)
        .map(|e| e.rule)
        .collect();
    let rules = db.rules();
    let derived_by: Vec<&RuleDef> = rules
        .iter()
        .filter(|r| by_provenance.contains(&r.name))
        .collect();
    if !derived_by.is_empty() {
        let (names, fields) = names_and_fields(&derived_by);
        return format!(
            "refused: {} {} → {} is derived by rule {names}. It changes only when the fields \
             that rule reads change ({fields}), or when the rule is deleted. Nothing was written.",
            sanitize(predicate),
            sanitize(subject),
            sanitize(object),
        );
    }
    let matching: Vec<&RuleDef> = rules
        .iter()
        .filter(|r| guard_matches(db, r, predicate, subject, object))
        .collect();
    if matching.is_empty() {
        return sanitize(detail);
    }
    let (names, fields) = names_and_fields(&matching);
    // One claim for the whole list, so it has to hold for every rule in it.
    let claim = if matching
        .iter()
        .all(|r| would_derive(db, r, subject, object))
    {
        "would derive it again"
    } else {
        "matches these two nodes' properties and may derive it again"
    };
    format!(
        "refused: {} {} → {} was written by hand and no rule derived it, but rule {names} \
         {claim}, so the delete is refused. It can be deleted once the fields that rule reads \
         ({fields}) no longer match, or once the rule is deleted. Nothing was written.",
        sanitize(predicate),
        sanitize(subject),
        sanitize(object),
    )
}

/// The rules' names, and every field their predicates read, each sanitized
/// and comma-joined; the fields sorted and deduped.
fn names_and_fields(rules: &[&RuleDef]) -> (String, String) {
    let names: Vec<String> = rules.iter().map(|r| sanitize(&r.name)).collect();
    let mut fields: BTreeSet<String> = BTreeSet::new();
    for r in rules {
        fields.extend(predicate_fields(&PredicateSummary::from(&r.predicate)));
    }
    let fields: Vec<String> = fields.iter().map(|f| sanitize(f)).collect();
    (names.join(", "), fields.join(", "))
}

/// Whether `rule`'s predicate holds between the nodes `a` and `b`.
fn predicate_holds<F: Fs>(db: &GraphDb<F>, rule: &RuleDef, a: &str, b: &str) -> bool {
    let a_props = |field: &str| db.get_prop(a, field);
    let b_props = |field: &str| db.get_prop(b, field);
    evaluate(
        &rule.predicate,
        &NodeView {
            key: a,
            props: &a_props,
        },
        &NodeView {
            key: b,
            props: &b_props,
        },
    )
    .is_some()
}

/// Whether the engine's delete guard refuses `(predicate, subject, object)`
/// on account of `rule`.
///
/// A mirror of the private `would_derive` in `db.rs`, which is why a
/// hand-written edge comes back `RuleOwned`: the rule's edge type and
/// endpoint labels, then its predicate evaluated on the pair. It is all the
/// guard asks. It does not look at the rule's via hop, its namespace or its
/// per-source cap (ledger row 67), so a rule can match here and still not be
/// one that would derive the edge — see [`would_derive`].
fn guard_matches<F: Fs>(
    db: &GraphDb<F>,
    rule: &RuleDef,
    predicate: &str,
    subject: &str,
    object: &str,
) -> bool {
    if subject == object || rule.edge_type != predicate {
        return false;
    }
    let label_is = |key: &str, label: &str| db.node_ref(key).is_some_and(|n| n.label() == label);
    label_is(subject, &rule.src_label)
        && label_is(object, &rule.dst_label)
        && predicate_holds(db, rule, subject, object)
}

/// Whether `rule`, which [`guard_matches`] for this pair, would in fact
/// derive `subject → object` from what the store holds now.
///
/// What the guard leaves out, as the rule engine applies it:
///
/// - **Namespace.** A scoped rule sees a node only in its own namespace, and
///   that goes for the via node as well as the two ends.
/// - **The via hop.** A via-hop rule evaluates its predicate between a via
///   node and the destination, not between the two ends: some node of
///   `via_label`, one `via_edge` hop from the source in `via_dir`, has to
///   satisfy it.
/// - **The cap.** A rule with `max_edges: Some(k)` keeps its best `k`
///   targets per source; without one, the rule stops at a global budget.
///   Ranking the candidates is the engine's business, so this answers yes
///   only when the cap cannot bind: no more candidates than the cap admits.
///   Past that the answer is no, which errs toward the weaker claim.
fn would_derive<F: Fs>(db: &GraphDb<F>, rule: &RuleDef, subject: &str, object: &str) -> bool {
    let sees = |key: &str| match rule.namespace.as_deref() {
        None => true,
        Some(ns) => namespace_of_value(db.get_prop(key, NS_PROP).as_ref()) == ns,
    };
    if !sees(subject) || !sees(object) {
        return false;
    }
    let sources = db.nodes_with_label(&rule.src_label).len() as u64;
    let targets = db.nodes_with_label(&rule.dst_label).len() as u64;
    let cap_cannot_bind = match rule.max_edges {
        Some(k) => targets <= k,
        None => sources.saturating_mul(targets) <= DEFAULT_MAX_EDGES,
    };
    if !cap_cannot_bind {
        return false;
    }
    let (Some(via_label), Some(via_edge)) = (rule.via_label.as_deref(), rule.via_edge.as_deref())
    else {
        // A plain rule: the guard already evaluated its predicate on the pair.
        return true;
    };
    db.neighbors(subject, via_edge, rule.via_dir.unwrap_or(Direction::Out))
        .unwrap_or_default()
        .iter()
        .any(|via| {
            db.node_ref(via).is_some_and(|n| n.label() == via_label)
                && sees(via)
                && predicate_holds(db, rule, via, object)
        })
}