Skip to main content

assay_core/runtime/
mandate_store.rs

1//! MandateStore: SQLite-backed mandate consumption tracking.
2//!
3//! Provides atomic, idempotent mandate consumption with:
4//! - Single-use / max_uses constraint enforcement
5//! - Nonce replay prevention
6//! - tool_call_id idempotency
7
8use chrono::{DateTime, Utc};
9use rusqlite::Connection;
10use std::path::Path;
11use std::sync::{Arc, Mutex};
12use thiserror::Error;
13
14#[path = "mandate_store_next/mod.rs"]
15mod mandate_store_next;
16
17/// Authorization receipt returned after successful consumption.
18#[derive(Debug, Clone, PartialEq, Eq)]
19pub struct AuthzReceipt {
20    pub mandate_id: String,
21    pub use_id: String,
22    pub use_count: u32,
23    pub consumed_at: DateTime<Utc>,
24    pub tool_call_id: String,
25    /// True if this was a new consumption, false if idempotent retry.
26    /// Used to avoid emitting duplicate lifecycle events on retries.
27    pub was_new: bool,
28}
29
30/// Authorization errors.
31#[derive(Debug, Error, PartialEq, Eq)]
32pub enum AuthzError {
33    #[error("Mandate not found: {mandate_id}")]
34    MandateNotFound { mandate_id: String },
35
36    #[error("Mandate already used (single_use=true)")]
37    AlreadyUsed,
38
39    #[error("Max uses exceeded: {current} > {max}")]
40    MaxUsesExceeded { max: u32, current: u32 },
41
42    #[error("Nonce replay detected: {nonce}")]
43    NonceReplay { nonce: String },
44
45    #[error("Mandate metadata conflict for {mandate_id}: stored {field} differs")]
46    MandateConflict { mandate_id: String, field: String },
47
48    #[error("Invalid mandate constraints: single_use=true with max_uses={max_uses}")]
49    InvalidConstraints { max_uses: u32 },
50
51    #[error("Mandate revoked at {revoked_at}")]
52    Revoked { revoked_at: DateTime<Utc> },
53
54    #[error("Database error: {0}")]
55    Database(String),
56}
57
58impl From<rusqlite::Error> for AuthzError {
59    fn from(e: rusqlite::Error) -> Self {
60        AuthzError::Database(e.to_string())
61    }
62}
63
64/// Mandate metadata for upsert.
65#[derive(Debug, Clone)]
66pub struct MandateMetadata {
67    pub mandate_id: String,
68    pub mandate_kind: String,
69    pub audience: String,
70    pub issuer: String,
71    pub expires_at: Option<DateTime<Utc>>,
72    pub single_use: bool,
73    pub max_uses: Option<u32>,
74    pub canonical_digest: String,
75    pub key_id: String,
76}
77
78/// Parameters for consume_mandate.
79#[derive(Debug, Clone)]
80pub struct ConsumeParams<'a> {
81    pub mandate_id: &'a str,
82    pub tool_call_id: &'a str,
83    pub nonce: Option<&'a str>,
84    pub audience: &'a str,
85    pub issuer: &'a str,
86    pub tool_name: &'a str,
87    pub operation_class: &'a str,
88    pub source_run_id: Option<&'a str>,
89}
90
91/// SQLite-backed mandate store.
92#[derive(Clone)]
93pub struct MandateStore {
94    conn: Arc<Mutex<Connection>>,
95}
96
97impl MandateStore {
98    /// Open a file-backed store.
99    pub fn open(path: &Path) -> Result<Self, AuthzError> {
100        mandate_store_next::schema::open_impl(path)
101    }
102
103    /// Create an in-memory store (for testing).
104    pub fn memory() -> Result<Self, AuthzError> {
105        mandate_store_next::schema::memory_impl()
106    }
107
108    /// Create store from existing connection (for multi-connection tests).
109    pub fn from_connection(conn: Connection) -> Result<Self, AuthzError> {
110        mandate_store_next::schema::from_connection_impl(conn)
111    }
112
113    /// Upsert mandate metadata. Idempotent for same content, errors on conflict.
114    pub fn upsert_mandate(&self, meta: &MandateMetadata) -> Result<(), AuthzError> {
115        mandate_store_next::upsert::upsert_mandate_impl(self, meta)
116    }
117
118    /// Consume mandate atomically. Idempotent on tool_call_id.
119    pub fn consume_mandate(&self, params: &ConsumeParams<'_>) -> Result<AuthzReceipt, AuthzError> {
120        mandate_store_next::txn::consume_mandate_in_txn_impl(self, params)
121    }
122
123    fn consume_mandate_inner(
124        &self,
125        conn: &Connection,
126        params: &ConsumeParams<'_>,
127    ) -> Result<AuthzReceipt, AuthzError> {
128        mandate_store_next::consume::consume_mandate_inner_impl(conn, params)
129    }
130
131    /// Get current use count for a mandate (for testing/debugging).
132    pub fn get_use_count(&self, mandate_id: &str) -> Result<Option<u32>, AuthzError> {
133        mandate_store_next::stats::get_use_count_impl(self, mandate_id)
134    }
135
136    /// Count use records for a mandate (for testing).
137    pub fn count_uses(&self, mandate_id: &str) -> Result<u32, AuthzError> {
138        mandate_store_next::stats::count_uses_impl(self, mandate_id)
139    }
140
141    /// Check if nonce exists (for testing).
142    pub fn nonce_exists(
143        &self,
144        audience: &str,
145        issuer: &str,
146        nonce: &str,
147    ) -> Result<bool, AuthzError> {
148        mandate_store_next::stats::nonce_exists_impl(self, audience, issuer, nonce)
149    }
150
151    // =========================================================================
152    // Revocation API (P0-A)
153    // =========================================================================
154
155    /// Insert or update a revocation record.
156    ///
157    /// Idempotent: re-inserting with same mandate_id updates the record.
158    pub fn upsert_revocation(&self, r: &RevocationRecord) -> Result<(), AuthzError> {
159        mandate_store_next::revocation::upsert_revocation_impl(self, r)
160    }
161
162    /// Get revoked_at timestamp for a mandate (if revoked).
163    pub fn get_revoked_at(&self, mandate_id: &str) -> Result<Option<DateTime<Utc>>, AuthzError> {
164        mandate_store_next::revocation::get_revoked_at_impl(self, mandate_id)
165    }
166
167    /// Check if a mandate is revoked (convenience method).
168    pub fn is_revoked(&self, mandate_id: &str) -> Result<bool, AuthzError> {
169        mandate_store_next::revocation::is_revoked_impl(self, mandate_id)
170    }
171}
172
173/// Revocation record for upsert.
174#[derive(Debug, Clone)]
175pub struct RevocationRecord {
176    pub mandate_id: String,
177    pub revoked_at: DateTime<Utc>,
178    pub reason: Option<String>,
179    pub revoked_by: Option<String>,
180    pub source: Option<String>,
181    pub event_id: Option<String>,
182}
183
184/// Compute deterministic use_id per SPEC-Mandate-v1.0.4 ยง7.4.
185///
186/// ```text
187/// use_id = "sha256:" + hex(SHA256(mandate_id + ":" + tool_call_id + ":" + use_count))
188/// ```
189pub fn compute_use_id(mandate_id: &str, tool_call_id: &str, use_count: u32) -> String {
190    mandate_store_next::stats::compute_use_id_impl(mandate_id, tool_call_id, use_count)
191}
192
193#[cfg(test)]
194#[path = "mandate_store_next/tests.rs"]
195mod tests;