diff --git a/.crumb b/.crumb index f8c53eb4..ef30574a 100644 --- a/.crumb +++ b/.crumb @@ -77,14 +77,14 @@ } ], "children_root": "sha256:2773d5ada0e6ae8b446499314f4a704361ab91a14a35259a62607b7c731d0ab9", - "compiled_at": "2026-10-08T07:09:52Z", + "compiled_at": "2026-10-08T07:24:47Z", "compiler": "crumb-compile/1", - "digest": "sha256:bd0a783a67322a7dff55098b23e5c40b92319f80220d29ccfaa7e1aea0f80aef", + "digest": "sha256:5c098ea7292505ac4b2fda35b8779401f5414f9c7038f54792ea7bf89aa5b067", "evidence_files": 187, "evidence_root": "sha256:27ee0353cb4d4aae2b7dbce2bfe0d9f4ef2a94cd7a4555b84684d59372503280", "files": 1360, - "generated_at_commit": "47a05e71c440970b1539e2bf1256b2950a973435", - "source_tree": "sha256:bd6748a40fdb373aa29fd6058b1fa9ff35a4ddc8eb33f173a6f13b6e37896cf5" + "generated_at_commit": "a32e2e7b20adfa3088e38e9fef0f1f38018e7727", + "source_tree": "sha256:be9e619e5a2fd891b4ff812bb276d4a13199a144b229a7183bde2f2ece643e40" } }, "history": [], diff --git a/crates/aien-runtime/src/effects.rs b/crates/aien-runtime/src/effects.rs index 38e642f6..53cb8601 100644 --- a/crates/aien-runtime/src/effects.rs +++ b/crates/aien-runtime/src/effects.rs @@ -1415,11 +1415,13 @@ pub fn authorize( } let prior = held.sha256()?; for &gid in l.minted_by_commit.get(&c.id).into_iter().flatten() { - if l.revoked.contains_key(&gid) || l.stops.iter().any(|&s| s > gid) { - continue; - } let g = &l.grants[&gid]; + // The spent intent is judged FIRST: a revoke or a stop after the grant + // never undoes a DONE effect, nor settles an intent that may still + // land. Only an unspent revoked or stopped grant gives way. + let dead = l.revoked.contains_key(&gid) || l.stops.iter().any(|&s| s > gid); match l.spent.get(&gid).map(|i| &l.intents[i]) { + None if dead => continue, // Settled: the grant is spent for good (a new effect needs a // new grant, as before). Some(row) if row.state == EffectState::Done => { diff --git a/crates/aien-runtime/tests/minted_grant_test.rs b/crates/aien-runtime/tests/minted_grant_test.rs index eaf2b434..4c3afc86 100644 --- a/crates/aien-runtime/tests/minted_grant_test.rs +++ b/crates/aien-runtime/tests/minted_grant_test.rs @@ -40,6 +40,14 @@ fn noted(r: ControlResponse) -> ComposeNoteReport { } } +/// A stop, resume or revoke answer. +fn controlled(r: ControlResponse) -> aien_runtime::control::ComposeControlReport { + match r { + ControlResponse::ComposeControlled(c) => *c, + other => panic!("expected ComposeControlled, got {other:?}"), + } +} + /// The refusal text of `r`; panics when the attack succeeded. fn refusal(r: ControlResponse, attack: &str) -> String { match r { @@ -377,3 +385,74 @@ linked_test!(no_new_grant_after_a_grant_settled_done, |fx| { r.cx_promotion = second.cx_promotion; noted(effects::mint_grant(&fx.b, &r)); }); + +// A grant that settled DONE stays spent for its commit whatever happens to it +// later. Before this fix a stop (then resume), or a revoke after the write, +// made the authorize step skip the spent grant and mint a second one, and the +// same committed proposal gave a second DONE effect (recovery matrix R6(d)). +linked_test!(no_new_grant_after_done_even_after_stop_and_resume, |fx| { + let g = noted(fx.mint()).id; + let i = noted(fx.open(g)).id; + std::fs::write(fx.target(), CONTENT).unwrap(); + assert_eq!(fx.ack_state(i), "DONE"); + controlled(effects::control(&fx.b, "stop", "drake", None)); + controlled(effects::control(&fx.b, "resume", "drake", None)); + let e = refusal(fx.mint(), "second grant after DONE, stop and resume"); + assert!( + e.contains("AlreadySpent") && e.contains("settled DONE"), + "{e}" + ); +}); + +linked_test!(no_new_grant_after_done_even_after_revoke, |fx| { + let g = noted(fx.mint()).id; + let i = noted(fx.open(g)).id; + std::fs::write(fx.target(), CONTENT).unwrap(); + assert_eq!(fx.ack_state(i), "DONE"); + controlled(effects::control(&fx.b, "revoke", "drake", Some(g))); + let e = refusal(fx.mint(), "second grant after DONE and revoke"); + assert!( + e.contains("AlreadySpent") && e.contains("settled DONE"), + "{e}" + ); +}); + +// An unsettled intent blocks a new grant even when its own grant was later +// made stale by a stop or revoked: the first write may still land. +linked_test!( + no_new_grant_while_a_stale_or_revoked_grants_intent_is_unsettled, + |fx| { + let g = noted(fx.mint()).id; + noted(fx.open(g)); + controlled(effects::control(&fx.b, "stop", "drake", None)); + controlled(effects::control(&fx.b, "resume", "drake", None)); + let e = refusal( + fx.mint(), + "second grant over an open intent, stop and resume", + ); + assert!(e.contains("ReconciliationRequired"), "{e}"); + controlled(effects::control(&fx.b, "revoke", "drake", Some(g))); + let e = refusal(fx.mint(), "second grant over an open intent, revoked"); + assert!(e.contains("ReconciliationRequired"), "{e}"); + } +); + +// The one path that must stay open (sc#321 review): a grant whose intent +// settled NOT_DONE (nothing was written) does not block a new grant. +linked_test!( + a_grant_whose_intent_settled_not_done_can_be_authorized_again, + |fx| { + let g = noted(fx.mint()).id; + let i = noted(effects::open_intent(&fx.b, &fx.req(g, false))).id; + let line = effects::reconcile_at_start(&fx.b); + assert!(line.starts_with("Reconcile: checked 1"), "{line}"); + let l = match fx.b.recall(&[], None) { + ControlResponse::ComposeRecalled(r) => effects::Ledger::from_records(&r.host).unwrap(), + other => panic!("{other:?}"), + }; + assert_eq!(l.intents[&i].state.name(), "NOT_DONE"); + let g2 = noted(fx.mint()).id; + assert_ne!(g2, g, "a new, distinct grant"); + assert!(!fx.ws.join("NOTES.md").exists()); + } +);