Skip to main content

qualia_core_db/bundle/stream/policy/grant/
scoped.rs

1//! Bounded multi-barrier effects under the existing local grant.
2use super::{
3    AuthorityBinding, AuthorityOwner, BoundClosure, E, LocalWholeSourceGrant, MutationHandle,
4    PinAuthority,
5};
6
7/// Current-authority gate with the same concrete owner and private grant.
8/// No permit escapes; failed checks and cancellation are terminal even when
9/// the callback catches an error. This is not a remote Share/consent decision.
10pub struct LocalEffectControl<'a, 'data> {
11    owner: &'a mut AuthorityOwner,
12    closure: &'a BoundClosure<'data>,
13    pins: &'a PinAuthority,
14    clock: &'a dyn Fn() -> u64,
15    handle: MutationHandle,
16    binding: AuthorityBinding,
17    ceiling: u64,
18    terminal: bool,
19}
20impl LocalEffectControl<'_, '_> {
21    /// Samples trusted host time internally and checks BOTH current paired pin
22    /// receipts and this exact local grant. Call immediately before each bounded
23    /// effect and after blocking I/O. This cannot undo already completed writes.
24    pub fn recheck(&mut self) -> Result<(), E> {
25        if self.terminal {
26            return Err(E::Spent);
27        }
28        self.terminal = true;
29        for _ in 0..2 {
30            let now = (self.clock)();
31            self.closure.recheck(self.pins, now)?;
32            self.owner
33                .issue_permit(self.handle, self.binding, now, self.ceiling)
34                .map_err(E::Authority)?;
35        }
36        self.terminal = false;
37        Ok(())
38    }
39    pub fn cancel(&mut self) -> Result<(), E> {
40        self.terminal = true;
41        self.owner.revoke(self.handle).map_err(E::Authority)
42    }
43    pub fn closure(&self) -> &BoundClosure<'_> {
44        self.closure
45    }
46}
47impl<'data> LocalWholeSourceGrant<'_, 'data> {
48    /// One synchronous effect with repeated current-authority checks. The
49    /// callback must recheck before each externally meaningful operation;
50    /// entry/exit checks additionally prevent publishing a successful result
51    /// after expiry. Failure does not roll back staging I/O; callers retain
52    /// uncertain bytes as unaccepted candidates and drain through their owner.
53    /// Exact owner/pin borrows remain held only for this bounded call. Do not
54    /// await, reenter these owners, or retain plaintext without its own lease.
55    pub fn accept_local_scoped<T>(
56        &mut self,
57        pins: &PinAuthority,
58        clock: impl Fn() -> u64,
59        effect: impl for<'g> FnOnce(&mut LocalEffectControl<'g, 'data>) -> Result<T, E>,
60    ) -> Result<T, E> {
61        if self.spent {
62            return Err(E::Spent);
63        }
64        self.spent = true;
65        let mut control = LocalEffectControl {
66            owner: self.owner,
67            closure: self.closure,
68            pins,
69            clock: &clock,
70            handle: self.handle,
71            binding: self.binding,
72            ceiling: self.ceiling,
73            terminal: false,
74        };
75        control.recheck()?;
76        let result = effect(&mut control)?;
77        control.recheck()?;
78        Ok(result)
79    }
80}