lib

Core libraries for Radroots
git clone https://radroots.dev/git/lib.git
Log | Files | Refs | README

workspace_admission.rs (33226B)


      1 #![cfg(unix)]
      2 
      3 #[path = "support/workspace_plans.rs"]
      4 mod plans;
      5 
      6 #[path = "support/public_admission.rs"]
      7 mod public_admission;
      8 
      9 use radroots_domain::{
     10     CommandContextData, CommandDigest, CorrelationId, OperationId, PublicKey, UnixSeconds,
     11 };
     12 use radroots_secrets::{
     13     SecretId, SecretRef,
     14     envelope::Nonce,
     15     file::{FileOpenMode, FileProvider},
     16     id::{BackendKind, KeyVersion},
     17     provider::AccessPolicy,
     18     wrapping::SecretMaterial,
     19 };
     20 use radroots_storage::{
     21     command::{
     22         AdmissionOutcome, AdmissionRequest, OperationError, OperationJournal, PreparedOperation,
     23     },
     24     workspace::{
     25         AttachmentInput, AttachmentProof, WorkspaceBinding, WorkspaceClock, WorkspaceError,
     26     },
     27 };
     28 use radroots_storage_sqlite::workspace::{LocalOutboxConfig, NativeWorkspace, WorkspaceCrypto};
     29 use serde_json::json;
     30 use sqlx::{Connection, SqliteConnection};
     31 use std::{
     32     path::PathBuf,
     33     sync::{
     34         Arc, Mutex,
     35         atomic::{AtomicU64, Ordering},
     36         mpsc,
     37     },
     38     time::Duration,
     39 };
     40 
     41 struct Pause {
     42     skip: usize,
     43     reached: mpsc::SyncSender<()>,
     44     resume: mpsc::Receiver<()>,
     45 }
     46 struct Clock {
     47     now: AtomicU64,
     48     pause: Mutex<Option<Pause>>,
     49 }
     50 impl Clock {
     51     fn arm(&self, skip: usize) -> (mpsc::Receiver<()>, mpsc::SyncSender<()>) {
     52         let (reached, receive) = mpsc::sync_channel(1);
     53         let (resume, released) = mpsc::sync_channel(1);
     54         *self.pause.lock().unwrap() = Some(Pause {
     55             skip,
     56             reached,
     57             resume: released,
     58         });
     59         (receive, resume)
     60     }
     61 }
     62 impl WorkspaceClock for Clock {
     63     fn now(&self) -> Result<UnixSeconds, WorkspaceError> {
     64         let pause = {
     65             let mut selected = self.pause.lock().unwrap();
     66             if let Some(pause) = selected.as_mut() {
     67                 if pause.skip == 0 {
     68                     selected.take()
     69                 } else {
     70                     pause.skip -= 1;
     71                     None
     72                 }
     73             } else {
     74                 None
     75             }
     76         };
     77         if let Some(pause) = pause {
     78             pause
     79                 .reached
     80                 .send(())
     81                 .map_err(|_| WorkspaceError::Unavailable)?;
     82             pause
     83                 .resume
     84                 .recv_timeout(Duration::from_secs(10))
     85                 .map_err(|_| WorkspaceError::Unavailable)?;
     86         }
     87         UnixSeconds::new(self.now.load(Ordering::SeqCst)).map_err(|_| WorkspaceError::Unavailable)
     88     }
     89 }
     90 struct Fixture {
     91     _root: tempfile::TempDir,
     92     path: PathBuf,
     93     provider: Arc<FileProvider>,
     94     clock: Arc<Clock>,
     95 }
     96 impl Fixture {
     97     fn new() -> Self {
     98         let root = tempfile::tempdir().unwrap();
     99         let base = root.path().canonicalize().unwrap();
    100         let provider = Arc::new(
    101             FileProvider::open(
    102                 base.join("vault"),
    103                 FileOpenMode::CreateNew,
    104                 SecretMaterial::from_slice(&[0xa5; 32]).unwrap(),
    105             )
    106             .unwrap(),
    107         );
    108         provider
    109             .provision(
    110                 &reference(),
    111                 &SecretMaterial::from_slice(&[0x41; 32]).unwrap(),
    112                 Nonce::new([0x11; 24]),
    113             )
    114             .unwrap();
    115         Self {
    116             _root: root,
    117             path: base.join("outbox"),
    118             provider,
    119             clock: Arc::new(Clock {
    120                 now: AtomicU64::new(100),
    121                 pause: Mutex::new(None),
    122             }),
    123         }
    124     }
    125     fn crypto(&self) -> WorkspaceCrypto {
    126         WorkspaceCrypto::new(
    127             self.provider.clone(),
    128             reference(),
    129             SecretMaterial::from_slice(&[0x41; 32]).unwrap(),
    130             AccessPolicy::standard(),
    131         )
    132         .unwrap()
    133     }
    134     async fn create(
    135         &self,
    136     ) -> (
    137         Arc<NativeWorkspace>,
    138         AttachmentInput,
    139         AttachmentProof,
    140         WorkspaceBinding,
    141     ) {
    142         self.create_with_expiry(200).await
    143     }
    144     async fn create_with_expiry(
    145         &self,
    146         expiry: u64,
    147     ) -> (
    148         Arc<NativeWorkspace>,
    149         AttachmentInput,
    150         AttachmentProof,
    151         WorkspaceBinding,
    152     ) {
    153         self.create_scoped(expiry, false).await
    154     }
    155     async fn create_scoped(
    156         &self,
    157         expiry: u64,
    158         seller: bool,
    159     ) -> (
    160         Arc<NativeWorkspace>,
    161         AttachmentInput,
    162         AttachmentProof,
    163         WorkspaceBinding,
    164     ) {
    165         let config = if seller {
    166             LocalOutboxConfig::seller
    167         } else {
    168             LocalOutboxConfig::new
    169         };
    170         let (store, input, proof) = NativeWorkspace::initialize_local_outbox(
    171             &self.path,
    172             config(
    173                 key(),
    174                 key(),
    175                 "synthetic-device",
    176                 UnixSeconds::new(expiry).unwrap(),
    177             )
    178             .unwrap(),
    179             self.crypto(),
    180             self.clock.clone(),
    181         )
    182         .await
    183         .unwrap();
    184         let binding = store.authenticate(input.clone(), &proof).await.unwrap();
    185         (Arc::new(store), input, proof, binding)
    186     }
    187     async fn database(&self) -> SqliteConnection {
    188         SqliteConnection::connect_with(
    189             &sqlx::sqlite::SqliteConnectOptions::new()
    190                 .filename(self.path.join("execution_v1.sqlite"))
    191                 .create_if_missing(false),
    192         )
    193         .await
    194         .unwrap()
    195     }
    196     async fn reopen(&self, input: AttachmentInput) -> NativeWorkspace {
    197         NativeWorkspace::open_existing(&self.path, input, self.crypto(), self.clock.clone())
    198             .await
    199             .unwrap()
    200     }
    201 }
    202 fn reference() -> SecretRef {
    203     SecretRef::new(
    204         SecretId::parse("workspace-key").unwrap(),
    205         BackendKind::File,
    206         KeyVersion::new(1).unwrap(),
    207     )
    208 }
    209 fn key() -> PublicKey {
    210     PublicKey::from_hex("79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798").unwrap()
    211 }
    212 fn copy(proof: &AttachmentProof) -> AttachmentProof {
    213     proof.with_secret(|secret| AttachmentProof::from_protected_input(secret).unwrap())
    214 }
    215 fn request(binding: &WorkspaceBinding, id: u8, text: &str, correlation: &str) -> AdmissionRequest {
    216     request_until(binding, id, text, correlation, 150)
    217 }
    218 fn request_until(
    219     binding: &WorkspaceBinding,
    220     id: u8,
    221     text: &str,
    222     correlation: &str,
    223     not_after: u64,
    224 ) -> AdmissionRequest {
    225     let input = binding.input();
    226     let context = CommandContextData::new(
    227         OperationId::from_bytes([id; 32]),
    228         binding.actor(),
    229         input.workspace_id(),
    230         input.authority(),
    231         input.route_digest(),
    232         None,
    233         None,
    234         UnixSeconds::new(not_after).unwrap(),
    235         Some(CorrelationId::new(correlation).unwrap()),
    236     )
    237     .unwrap();
    238     let bytes = serde_json::to_vec(&json!({"type": "inbox.inquire", "context": context,
    239         "input": {"thread_id": "66".repeat(32), "peer": key(), "source": null, "text": text}}))
    240     .unwrap();
    241     AdmissionRequest::from_protected_wire(binding.clone(), &bytes).unwrap()
    242 }
    243 fn committed(outcome: AdmissionOutcome) -> PreparedOperation {
    244     match outcome {
    245         AdmissionOutcome::Committed(result) => result,
    246         AdmissionOutcome::Unknown(_) => panic!("unexpected unknown completion"),
    247     }
    248 }
    249 async fn reached(receiver: mpsc::Receiver<()>) {
    250     tokio::task::spawn_blocking(move || receiver.recv_timeout(Duration::from_secs(10)))
    251         .await
    252         .unwrap()
    253         .unwrap();
    254 }
    255 
    256 #[tokio::test]
    257 async fn actual_commit_reopen_and_replay_preserve_original_identity_and_private_command() {
    258     let fixture = Fixture::new();
    259     let (store, input, proof, binding) = fixture.create().await;
    260     let captured = request(
    261         &binding,
    262         1,
    263         "private-cabbage-inquiry-🧺",
    264         "original-observer",
    265     );
    266     let digest = captured.command().digest();
    267     let original = committed(
    268         store
    269             .admit_command_and_hold(captured, copy(&proof))
    270             .await
    271             .unwrap(),
    272     );
    273     let anchor = original.identity();
    274     assert_eq!(original.revision().get(), 1);
    275     assert_eq!(
    276         original.request().chat_command().unwrap().text(),
    277         "private-cabbage-inquiry-🧺"
    278     );
    279     assert_eq!(original.request().binding().input(), &input);
    280     assert!(
    281         store
    282             .operation(
    283                 binding.clone(),
    284                 OperationId::from_bytes([9; 32]),
    285                 None,
    286                 copy(&proof)
    287             )
    288             .await
    289             .unwrap()
    290             .is_none()
    291     );
    292     store.close().await.unwrap();
    293     for name in ["execution_v1.sqlite", "execution_v1.sqlite-wal"] {
    294         if let Ok(bytes) = std::fs::read(fixture.path.join(name)) {
    295             assert!(
    296                 !bytes
    297                     .windows(b"private-cabbage-inquiry".len())
    298                     .any(|value| value == b"private-cabbage-inquiry")
    299             );
    300         }
    301     }
    302     fixture.clock.now.store(160, Ordering::SeqCst);
    303     let reopened = fixture.reopen(input).await;
    304     let retained = reopened
    305         .operation(
    306             binding.clone(),
    307             anchor.operation_id(),
    308             Some(digest),
    309             copy(&proof),
    310         )
    311         .await
    312         .unwrap()
    313         .unwrap();
    314     assert_eq!(retained.identity(), anchor);
    315     let replay = committed(
    316         reopened
    317             .admit_command_and_hold(
    318                 request(&binding, 1, "private-cabbage-inquiry-🧺", "new-observer"),
    319                 copy(&proof),
    320             )
    321             .await
    322             .unwrap(),
    323     );
    324     assert_eq!(replay.identity(), anchor);
    325     assert_eq!(
    326         replay
    327             .request()
    328             .command()
    329             .context()
    330             .correlation_id()
    331             .unwrap()
    332             .as_str(),
    333         "original-observer"
    334     );
    335     assert!(matches!(
    336         reopened
    337             .admit_command_and_hold(request(&binding, 1, "changed", "new"), copy(&proof))
    338             .await,
    339         Err(OperationError::IdempotencyConflict)
    340     ));
    341     assert!(matches!(
    342         reopened
    343             .operation(
    344                 binding.clone(),
    345                 anchor.operation_id(),
    346                 Some(CommandDigest::from_bytes([0; 32])),
    347                 copy(&proof)
    348             )
    349             .await,
    350         Err(OperationError::IdempotencyConflict)
    351     ));
    352     assert!(matches!(
    353         reopened
    354             .admit_command_and_hold(
    355                 request(&binding, 2, "new expired command", "new"),
    356                 copy(&proof)
    357             )
    358             .await,
    359         Err(OperationError::DeadlineExceeded)
    360     ));
    361     let mut database = fixture.database().await;
    362     let count: i64 = sqlx::query_scalar("SELECT count(*) FROM operations")
    363         .fetch_one(&mut database)
    364         .await
    365         .unwrap();
    366     assert_eq!(count, 1);
    367     database.close().await.unwrap();
    368     reopened.close().await.unwrap();
    369 }
    370 
    371 #[tokio::test]
    372 async fn current_proof_and_revocation_are_checked_for_admission_and_retained_queries() {
    373     let fixture = Fixture::new();
    374     let (store, input, proof, binding) = fixture.create().await;
    375     let wrong = AttachmentProof::from_protected_input(&[0; 32]).unwrap();
    376     assert!(matches!(
    377         store
    378             .admit_command_and_hold(request(&binding, 3, "private", "one"), wrong)
    379             .await,
    380         Err(OperationError::Workspace(WorkspaceError::Unauthorized))
    381     ));
    382     committed(
    383         store
    384             .admit_command_and_hold(request(&binding, 3, "private", "one"), copy(&proof))
    385             .await
    386             .unwrap(),
    387     );
    388     store.revoke(input, &proof).await.unwrap();
    389     assert!(matches!(
    390         store
    391             .operation(
    392                 binding.clone(),
    393                 OperationId::from_bytes([3; 32]),
    394                 None,
    395                 copy(&proof)
    396             )
    397             .await,
    398         Err(OperationError::Workspace(WorkspaceError::GrantRevoked))
    399     ));
    400     assert!(matches!(
    401         store
    402             .admit_command_and_hold(request(&binding, 3, "private", "retry"), copy(&proof))
    403             .await,
    404         Err(OperationError::Workspace(WorkspaceError::GrantRevoked))
    405     ));
    406     store.close().await.unwrap();
    407 }
    408 
    409 #[tokio::test]
    410 async fn transaction_time_clock_rejects_expiry_after_capture_and_rolls_back() {
    411     let fixture = Fixture::new();
    412     let (store, _, proof, binding) = fixture.create().await;
    413     let captured = request(&binding, 4, "late", "one");
    414     let (arrived, resume) = fixture.clock.arm(1);
    415     let owner = store.clone();
    416     let secret = copy(&proof);
    417     let task = tokio::spawn(async move { owner.admit_command_and_hold(captured, secret).await });
    418     reached(arrived).await;
    419     fixture.clock.now.store(150, Ordering::SeqCst);
    420     resume.send(()).unwrap();
    421     assert!(matches!(
    422         task.await.unwrap(),
    423         Err(OperationError::DeadlineExceeded)
    424     ));
    425     assert!(
    426         store
    427             .operation(
    428                 binding,
    429                 OperationId::from_bytes([4; 32]),
    430                 None,
    431                 copy(&proof)
    432             )
    433             .await
    434             .unwrap()
    435             .is_none()
    436     );
    437     store.close().await.unwrap();
    438 }
    439 
    440 #[tokio::test]
    441 async fn lost_observer_does_not_cancel_admitted_transaction_and_close_drains_it() {
    442     let fixture = Fixture::new();
    443     let (store, input, proof, binding) = fixture.create().await;
    444     let captured = request(&binding, 5, "retained after observer cancellation", "gone");
    445     let digest = captured.command().digest();
    446     let (arrived, resume) = fixture.clock.arm(1);
    447     let owner = store.clone();
    448     let secret = copy(&proof);
    449     let task = tokio::spawn(async move { owner.admit_command_and_hold(captured, secret).await });
    450     reached(arrived).await;
    451     task.abort();
    452     assert!(task.await.unwrap_err().is_cancelled());
    453     resume.send(()).unwrap();
    454     store.close().await.unwrap();
    455     let reopened = fixture.reopen(input).await;
    456     let result = reopened
    457         .operation(
    458             binding,
    459             OperationId::from_bytes([5; 32]),
    460             Some(digest),
    461             copy(&proof),
    462         )
    463         .await
    464         .unwrap()
    465         .unwrap();
    466     assert_eq!(
    467         result.request().chat_command().unwrap().text(),
    468         "retained after observer cancellation"
    469     );
    470     reopened.close().await.unwrap();
    471 }
    472 
    473 #[tokio::test]
    474 async fn actual_sqlite_statement_failure_confirms_rollback_before_known_failure() {
    475     let fixture = Fixture::new();
    476     let (store, _, proof, binding) = fixture.create().await;
    477     let mut database = fixture.database().await;
    478     sqlx::raw_sql("CREATE TRIGGER fail_admission BEFORE INSERT ON operations BEGIN SELECT RAISE(ABORT,'isolated fixture'); END")
    479         .execute(&mut database).await.unwrap();
    480     assert!(matches!(
    481         store
    482             .admit_command_and_hold(request(&binding, 6, "rollback", "one"), copy(&proof))
    483             .await,
    484         Err(OperationError::StorageFailure)
    485     ));
    486     let count: i64 = sqlx::query_scalar("SELECT count(*) FROM operations")
    487         .fetch_one(&mut database)
    488         .await
    489         .unwrap();
    490     assert_eq!(count, 0);
    491     sqlx::raw_sql("DROP TRIGGER fail_admission")
    492         .execute(&mut database)
    493         .await
    494         .unwrap();
    495     committed(
    496         store
    497             .admit_command_and_hold(request(&binding, 6, "rollback", "two"), copy(&proof))
    498             .await
    499             .unwrap(),
    500     );
    501     database.close().await.unwrap();
    502     store.close().await.unwrap();
    503 }
    504 
    505 #[tokio::test]
    506 async fn actual_sqlite_commit_failure_stays_unknown_until_safe_close_and_reopen() {
    507     let fixture = Fixture::new();
    508     let (store, input, proof, binding) = fixture.create().await;
    509     let mut database = fixture.database().await;
    510     sqlx::raw_sql("CREATE TABLE deferred_fault(value INTEGER REFERENCES workspace_grant(singleton) DEFERRABLE INITIALLY DEFERRED); CREATE TRIGGER fail_commit AFTER INSERT ON operations BEGIN INSERT INTO deferred_fault VALUES(2); END")
    511         .execute(&mut database).await.unwrap();
    512     let captured = request(&binding, 7, "uncertain completion", "one");
    513     let digest = captured.command().digest();
    514     let anchor = match store
    515         .admit_command_and_hold(captured, copy(&proof))
    516         .await
    517         .unwrap()
    518     {
    519         AdmissionOutcome::Unknown(anchor) => anchor,
    520         AdmissionOutcome::Committed(_) => panic!("deferred constraint should fail COMMIT"),
    521     };
    522     assert_eq!(anchor.operation_id(), OperationId::from_bytes([7; 32]));
    523     assert_eq!(anchor.command_digest(), digest);
    524     assert!(anchor.transaction_id().is_some());
    525     assert!(matches!(
    526         store
    527             .operation(
    528                 binding.clone(),
    529                 anchor.operation_id(),
    530                 Some(digest),
    531                 copy(&proof)
    532             )
    533             .await,
    534         Err(OperationError::RecoveryRequired)
    535     ));
    536     assert!(matches!(
    537         store
    538             .admit_command_and_hold(request(&binding, 8, "blocked", "two"), copy(&proof))
    539             .await,
    540         Err(OperationError::RecoveryRequired)
    541     ));
    542     store.close().await.unwrap();
    543     sqlx::raw_sql("DROP TRIGGER fail_commit; DROP TABLE deferred_fault")
    544         .execute(&mut database)
    545         .await
    546         .unwrap();
    547     database.close().await.unwrap();
    548     let reopened = fixture.reopen(input).await;
    549     // No signer or transport has run in this Prepared-only component. This is
    550     // reconciliation of this original T0, not a general no-external-effect proof.
    551     assert!(
    552         reopened
    553             .operation(binding, anchor.operation_id(), Some(digest), copy(&proof))
    554             .await
    555             .unwrap()
    556             .is_none()
    557     );
    558     reopened.close().await.unwrap();
    559 }
    560 
    561 #[tokio::test]
    562 async fn protected_grant_is_rechecked_inside_t0_after_the_pretransaction_observation() {
    563     let fixture = Fixture::new();
    564     let (store, _, proof, binding) = fixture.create().await;
    565     let captured = request(&binding, 9, "fenced", "one");
    566     let mut database = fixture.database().await;
    567     let original: Vec<u8> = sqlx::query_scalar("SELECT protected FROM workspace_grant")
    568         .fetch_one(&mut database)
    569         .await
    570         .unwrap();
    571     let (arrived, resume) = fixture.clock.arm(0);
    572     let owner = store.clone();
    573     let secret = copy(&proof);
    574     let task = tokio::spawn(async move { owner.admit_command_and_hold(captured, secret).await });
    575     reached(arrived).await;
    576     sqlx::query("UPDATE workspace_grant SET protected=?")
    577         .bind([0; 8].as_slice())
    578         .execute(&mut database)
    579         .await
    580         .unwrap();
    581     resume.send(()).unwrap();
    582     assert!(matches!(
    583         task.await.unwrap(),
    584         Err(OperationError::RecoveryRequired)
    585     ));
    586     let count: i64 = sqlx::query_scalar("SELECT count(*) FROM operations")
    587         .fetch_one(&mut database)
    588         .await
    589         .unwrap();
    590     assert_eq!(count, 0);
    591     let retained: Vec<u8> = sqlx::query_scalar("SELECT protected FROM workspace_grant")
    592         .fetch_one(&mut database)
    593         .await
    594         .unwrap();
    595     assert_eq!(retained, [0; 8]);
    596     sqlx::query("UPDATE workspace_grant SET protected=?")
    597         .bind(original)
    598         .execute(&mut database)
    599         .await
    600         .unwrap();
    601     database.close().await.unwrap();
    602     store.close().await.unwrap();
    603 }
    604 
    605 #[tokio::test]
    606 async fn unqualified_history_and_approval_descriptors_never_become_admitted_commands() {
    607     let fixture = Fixture::new();
    608     let (store, _, proof, binding) = fixture.create().await;
    609     let base = request(&binding, 10, "untrusted selectors", "one");
    610     let wire: serde_json::Value = serde_json::from_slice(base.command().protected_wire()).unwrap();
    611     let mut reply = wire.clone();
    612     reply["type"] = json!("inbox.reply");
    613     reply["input"].as_object_mut().unwrap().remove("source");
    614     reply["input"]["parent"] = json!("11".repeat(32));
    615     let captured = AdmissionRequest::from_protected_wire(
    616         binding.clone(),
    617         &serde_json::to_vec(&reply).unwrap(),
    618     )
    619     .unwrap();
    620     assert!(matches!(
    621         store.admit_command_and_hold(captured, copy(&proof)).await,
    622         Err(OperationError::MissingHistory)
    623     ));
    624     let mut approval = wire.clone();
    625     approval["context"]["approval"] =
    626         json!({"intent_digest": "22".repeat(32), "scope": "caller-supplied", "expires_at": "150"});
    627     let captured = AdmissionRequest::from_protected_wire(
    628         binding.clone(),
    629         &serde_json::to_vec(&approval).unwrap(),
    630     )
    631     .unwrap();
    632     assert!(matches!(
    633         store.admit_command_and_hold(captured, copy(&proof)).await,
    634         Err(OperationError::UnverifiedApproval)
    635     ));
    636     let other = WorkspaceBinding::from_authenticated_host(
    637         binding.input().clone(),
    638         binding.scope(),
    639         binding.actor(),
    640         "substituted-device",
    641         binding.grant_revision(),
    642         binding.expires_at(),
    643         binding.observed_at(),
    644     )
    645     .unwrap();
    646     assert!(matches!(
    647         store
    648             .admit_command_and_hold(request(&other, 10, "private", "one"), copy(&proof))
    649             .await,
    650         Err(OperationError::Unauthorized)
    651     ));
    652     for (actor, revision, expiry) in [
    653         (
    654             radroots_domain::ActorRef::new(
    655                 binding.actor().participant(),
    656                 radroots_domain::ActorRole::Seller,
    657                 None,
    658                 binding.actor().grant_id(),
    659             ),
    660             binding.grant_revision(),
    661             binding.expires_at(),
    662         ),
    663         (
    664             binding.actor(),
    665             radroots_domain::Sequence::new(2).unwrap(),
    666             binding.expires_at(),
    667         ),
    668         (
    669             binding.actor(),
    670             binding.grant_revision(),
    671             UnixSeconds::new(201).unwrap(),
    672         ),
    673     ] {
    674         let substituted = WorkspaceBinding::from_authenticated_host(
    675             binding.input().clone(),
    676             if actor.role() == radroots_domain::ActorRole::Seller {
    677                 radroots_storage::workspace::WorkspaceScope::NewSellerOutbox
    678             } else {
    679                 binding.scope()
    680             },
    681             actor,
    682             binding.principal_id(),
    683             revision,
    684             expiry,
    685             binding.observed_at(),
    686         )
    687         .unwrap();
    688         assert!(matches!(
    689             store
    690                 .admit_command_and_hold(request(&substituted, 10, "private", "one"), copy(&proof))
    691                 .await,
    692             Err(OperationError::Unauthorized)
    693         ));
    694     }
    695     let mut expected = wire.clone();
    696     expected["context"]["expected"] = json!({"order_id": "33".repeat(32), "agreement": null,
    697         "seller_sequence": "0", "buyer_head": null, "checkpoint": binding.input().checkpoint()});
    698     let captured = AdmissionRequest::from_protected_wire(
    699         binding.clone(),
    700         &serde_json::to_vec(&expected).unwrap(),
    701     )
    702     .unwrap();
    703     assert!(matches!(
    704         store.admit_command_and_hold(captured, copy(&proof)).await,
    705         Err(OperationError::MissingHistory)
    706     ));
    707     let mut beyond_grant = wire;
    708     beyond_grant["context"]["not_after"] = json!("201");
    709     let captured = AdmissionRequest::from_protected_wire(
    710         binding.clone(),
    711         &serde_json::to_vec(&beyond_grant).unwrap(),
    712     )
    713     .unwrap();
    714     assert!(matches!(
    715         store.admit_command_and_hold(captured, copy(&proof)).await,
    716         Err(OperationError::DeadlineExceeded)
    717     ));
    718     let mut database = fixture.database().await;
    719     let count: i64 = sqlx::query_scalar("SELECT count(*) FROM operations")
    720         .fetch_one(&mut database)
    721         .await
    722         .unwrap();
    723     assert_eq!(count, 0);
    724     database.close().await.unwrap();
    725     store.close().await.unwrap();
    726 }
    727 
    728 #[tokio::test]
    729 async fn retained_ciphertext_is_bound_to_the_original_operation_and_never_repaired_by_query() {
    730     let fixture = Fixture::new();
    731     let (store, _, proof, binding) = fixture.create().await;
    732     committed(
    733         store
    734             .admit_command_and_hold(
    735                 request(&binding, 11, "first private command", "one"),
    736                 copy(&proof),
    737             )
    738             .await
    739             .unwrap(),
    740     );
    741     committed(
    742         store
    743             .admit_command_and_hold(
    744                 request(&binding, 12, "second private command", "two"),
    745                 copy(&proof),
    746             )
    747             .await
    748             .unwrap(),
    749     );
    750     let mut database = fixture.database().await;
    751     let protected: Vec<u8> =
    752         sqlx::query_scalar("SELECT protected FROM operations WHERE operation_id=?")
    753             .bind([11; 32].as_slice())
    754             .fetch_one(&mut database)
    755             .await
    756             .unwrap();
    757     sqlx::query("UPDATE operations SET protected=? WHERE operation_id=?")
    758         .bind(&protected)
    759         .bind([12; 32].as_slice())
    760         .execute(&mut database)
    761         .await
    762         .unwrap();
    763     assert!(matches!(
    764         store
    765             .operation(
    766                 binding,
    767                 OperationId::from_bytes([12; 32]),
    768                 None,
    769                 copy(&proof)
    770             )
    771             .await,
    772         Err(OperationError::Workspace(WorkspaceError::RecoveryRequired))
    773     ));
    774     let unchanged: Vec<u8> =
    775         sqlx::query_scalar("SELECT protected FROM operations WHERE operation_id=?")
    776             .bind([12; 32].as_slice())
    777             .fetch_one(&mut database)
    778             .await
    779             .unwrap();
    780     assert_eq!(unchanged, protected);
    781     database.close().await.unwrap();
    782     store.close().await.unwrap();
    783 }
    784 
    785 #[tokio::test]
    786 async fn predecessor_grant_only_schema_is_preserved_for_explicit_cutover() {
    787     let fixture = Fixture::new();
    788     let (store, input, _, _) = fixture.create().await;
    789     store.close().await.unwrap();
    790     let mut database = fixture.database().await;
    791     sqlx::raw_sql(
    792         "DROP TABLE chat_publication_work; DROP TABLE chat_messages; DROP TABLE artifact_evidence_parts; DROP TABLE artifact_evidence; DROP TABLE signing_attempts; DROP TABLE signing_plan_parts; DROP TABLE signing_plans; DROP INDEX operation_transaction_identity; DROP TABLE operations; PRAGMA user_version=1",
    793     )
    794     .execute(&mut database)
    795     .await
    796     .unwrap();
    797     database.close().await.unwrap();
    798     let path = fixture.path.join("execution_v1.sqlite");
    799     let before = std::fs::read(&path).unwrap();
    800     assert!(matches!(
    801         NativeWorkspace::open_existing(
    802             &fixture.path,
    803             input,
    804             fixture.crypto(),
    805             fixture.clock.clone()
    806         )
    807         .await,
    808         Err(WorkspaceError::RecoveryRequired)
    809     ));
    810     assert_eq!(std::fs::read(path).unwrap(), before);
    811 }
    812 
    813 #[tokio::test]
    814 async fn bounded_admission_queue_refuses_overflow_and_drains_every_accepted_request() {
    815     let fixture = Fixture::new();
    816     let (store, _, proof, binding) = fixture.create().await;
    817     let (arrived, resume) = fixture.clock.arm(1);
    818     let owner = store.clone();
    819     let first = request(&binding, 64, "first", "one");
    820     let secret = copy(&proof);
    821     let blocked = tokio::spawn(async move { owner.admit_command_and_hold(first, secret).await });
    822     reached(arrived).await;
    823     let mut queued = Vec::new();
    824     for id in 1..=32 {
    825         let mut future =
    826             store.admit_command_and_hold(request(&binding, id, "queued", "one"), copy(&proof));
    827         assert!(futures::poll!(future.as_mut()).is_pending());
    828         queued.push(future);
    829     }
    830     assert!(matches!(
    831         store
    832             .admit_command_and_hold(request(&binding, 33, "overflow", "one"), copy(&proof))
    833             .await,
    834         Err(OperationError::Workspace(WorkspaceError::Unavailable))
    835     ));
    836     resume.send(()).unwrap();
    837     committed(blocked.await.unwrap().unwrap());
    838     store.close().await.unwrap();
    839     for future in queued {
    840         committed(future.await.unwrap());
    841     }
    842     let mut database = fixture.database().await;
    843     let count: i64 = sqlx::query_scalar("SELECT count(*) FROM operations")
    844         .fetch_one(&mut database)
    845         .await
    846         .unwrap();
    847     assert_eq!(count, 33);
    848     database.close().await.unwrap();
    849     assert!(matches!(
    850         store
    851             .admit_command_and_hold(request(&binding, 65, "closed", "one"), copy(&proof))
    852             .await,
    853         Err(OperationError::Workspace(WorkspaceError::Closed))
    854     ));
    855 }
    856 
    857 #[tokio::test]
    858 async fn retained_first_issue_window_never_restarts_on_reopen_or_replay() {
    859     let fixture = Fixture::new();
    860     let (store, input, proof, binding) = fixture.create_with_expiry(2000).await;
    861     let original = committed(
    862         store
    863             .admit_command_and_hold(
    864                 request_until(&binding, 66, "fixed window", "one", 900),
    865                 copy(&proof),
    866             )
    867             .await
    868             .unwrap(),
    869     );
    870     let anchor = original.identity();
    871     assert_eq!(original.issuance_window().prepared_at().get(), 100);
    872     assert_eq!(
    873         original.issuance_window().first_issue_not_after().get(),
    874         220
    875     );
    876     store.close().await.unwrap();
    877     fixture.clock.now.store(500, Ordering::SeqCst);
    878     let reopened = fixture.reopen(input).await;
    879     let recovered = reopened
    880         .operation(
    881             binding.clone(),
    882             anchor.operation_id(),
    883             Some(anchor.command_digest()),
    884             copy(&proof),
    885         )
    886         .await
    887         .unwrap()
    888         .unwrap();
    889     assert_eq!(recovered.issuance_window(), original.issuance_window());
    890     let replay = committed(
    891         reopened
    892             .admit_command_and_hold(
    893                 request_until(&binding, 66, "fixed window", "retry", 900),
    894                 copy(&proof),
    895             )
    896             .await
    897             .unwrap(),
    898     );
    899     assert_eq!(replay.identity(), anchor);
    900     assert_eq!(replay.issuance_window(), original.issuance_window());
    901     let fresh = committed(
    902         reopened
    903             .admit_command_and_hold(
    904                 request_until(&binding, 67, "new operation", "new", 900),
    905                 copy(&proof),
    906             )
    907             .await
    908             .unwrap(),
    909     );
    910     assert_eq!(fresh.issuance_window().prepared_at().get(), 500);
    911     assert_eq!(fresh.issuance_window().first_issue_not_after().get(), 620);
    912     reopened.close().await.unwrap();
    913 }
    914 
    915 #[tokio::test]
    916 async fn predecessor_preparation_schema_marker_is_preserved_without_inferring_a_window() {
    917     let fixture = Fixture::new();
    918     let (store, input, proof, binding) = fixture.create().await;
    919     committed(
    920         store
    921             .admit_command_and_hold(request(&binding, 70, "retained", "one"), copy(&proof))
    922             .await
    923             .unwrap(),
    924     );
    925     store.close().await.unwrap();
    926     let mut database = fixture.database().await;
    927     sqlx::query("PRAGMA user_version=2")
    928         .execute(&mut database)
    929         .await
    930         .unwrap();
    931     database.close().await.unwrap();
    932     let path = fixture.path.join("execution_v1.sqlite");
    933     let before = std::fs::read(&path).unwrap();
    934     assert!(matches!(
    935         NativeWorkspace::open_existing(
    936             &fixture.path,
    937             input,
    938             fixture.crypto(),
    939             fixture.clock.clone()
    940         )
    941         .await,
    942         Err(WorkspaceError::RecoveryRequired)
    943     ));
    944     assert_eq!(std::fs::read(path).unwrap(), before);
    945 }
    946 
    947 #[tokio::test]
    948 async fn transaction_checks_first_window_expiry_and_clock_rollback_after_preparation() {
    949     let fixture = Fixture::new();
    950     let (store, _, proof, binding) = fixture.create_with_expiry(2000).await;
    951     for (id, changed_clock) in [(68, 220), (69, 90)] {
    952         fixture.clock.now.store(100, Ordering::SeqCst);
    953         let request = request_until(&binding, id, "clock boundary", "one", 900);
    954         let (arrived, resume) = fixture.clock.arm(1);
    955         let owner = store.clone();
    956         let secret = copy(&proof);
    957         let task = tokio::spawn(async move { owner.admit_command_and_hold(request, secret).await });
    958         reached(arrived).await;
    959         fixture.clock.now.store(changed_clock, Ordering::SeqCst);
    960         resume.send(()).unwrap();
    961         let result = task.await.unwrap();
    962         if changed_clock == 220 {
    963             assert!(matches!(result, Err(OperationError::DeadlineExceeded)));
    964         } else {
    965             assert!(matches!(
    966                 result,
    967                 Err(OperationError::Workspace(WorkspaceError::Unavailable))
    968             ));
    969         }
    970         fixture.clock.now.store(100, Ordering::SeqCst);
    971         assert!(
    972             store
    973                 .operation(
    974                     binding.clone(),
    975                     OperationId::from_bytes([id; 32]),
    976                     None,
    977                     copy(&proof)
    978                 )
    979                 .await
    980                 .unwrap()
    981                 .is_none()
    982         );
    983     }
    984     store.close().await.unwrap();
    985 }
    986 
    987 #[tokio::test]
    988 async fn new_preparation_refuses_clock_rollback_before_its_first_observation() {
    989     let fixture = Fixture::new();
    990     let (store, _, proof, binding) = fixture.create().await;
    991     fixture.clock.now.store(90, Ordering::SeqCst);
    992     assert!(matches!(
    993         store
    994             .admit_command_and_hold(request(&binding, 71, "clock rollback", "one"), copy(&proof))
    995             .await,
    996         Err(OperationError::Workspace(WorkspaceError::Unavailable))
    997     ));
    998     fixture.clock.now.store(100, Ordering::SeqCst);
    999     assert!(
   1000         store
   1001             .operation(binding, OperationId::from_bytes([71; 32]), None, proof)
   1002             .await
   1003             .unwrap()
   1004             .is_none()
   1005     );
   1006     store.close().await.unwrap();
   1007 }