spacedb_sdk/session.rs
1//! A session — who is acting, what they're allowed, and what they can spend.
2//!
3//! A session binds an actor mID, the capability that authorizes its ops, a
4//! [`Budget`] (seeded from the capability's own `budget_micro_mata`, so an agent
5//! literally spends from its grant), and a [`CausalSession`] that tracks the
6//! frontier it has observed for read-your-writes / monotonic reads.
7
8use spacedb_access::{Did, SignedCapability};
9use spacedb_consistency::CausalSession;
10use spacedb_meter::Budget;
11
12/// An authenticated, budgeted context for operations.
13pub struct Session {
14 pub(crate) actor: Did,
15 pub(crate) capability: SignedCapability,
16 pub(crate) budget: Budget,
17 pub(crate) causal: CausalSession,
18}
19
20impl Session {
21 pub(crate) fn from_capability(capability: SignedCapability) -> Self {
22 let actor = capability.capability.bearer.clone();
23 // An agent spends from the budget its capability carries; no budget means
24 // it can't perform any priced op (fails closed).
25 let budget = Budget::new(capability.capability.budget_micro_mata.unwrap_or(0));
26 Self {
27 actor,
28 capability,
29 budget,
30 causal: CausalSession::new(),
31 }
32 }
33
34 /// The acting mID.
35 pub fn actor(&self) -> &Did {
36 &self.actor
37 }
38
39 /// Remaining spend allowance, in micro-`$MATA`.
40 pub fn budget_remaining(&self) -> u64 {
41 self.budget.remaining()
42 }
43}