commit 63d23057a75e069ef809cb0f60ffb30620895602
parent e55c1749902811732167a2ce5c099e22e964624f
Author: triesap <tyson@radroots.org>
Date: Sat, 24 Oct 2026 16:11:33 +0000
storage_sqlite: share exact retained workspace catalogs
- Use one explicit catalog for namespace creation and read-only admission.
- Preserve every retained outbox schema and protected-grant boundary.
- Refuse same-count schema substitutions without repairing the database.
- Verify actual workspace behavior, strict lint and standalone contracts.
Diffstat:
4 files changed, 286 insertions(+), 143 deletions(-)
diff --git a/crates/storage_sqlite/src/workspace/mod.rs b/crates/storage_sqlite/src/workspace/mod.rs
@@ -19,6 +19,7 @@ mod operation;
mod plan;
mod record;
mod recovery;
+mod schema;
mod snapshot;
mod worker;
diff --git a/crates/storage_sqlite/src/workspace/schema.rs b/crates/storage_sqlite/src/workspace/schema.rs
@@ -0,0 +1,244 @@
+//! Exact retained outbox catalogs, shared by creation and read-only admission.
+
+use super::{WorkspaceError, attempt, delivery, evidence, installation, listing, operation, plan};
+use sqlx::SqliteConnection;
+
+pub(super) const CURRENT_VERSION: i64 = 11;
+const GRANT: &str = "CREATE TABLE workspace_grant (singleton INTEGER PRIMARY KEY CHECK(singleton=1), workspace BLOB NOT NULL CHECK(length(workspace)=32), grant_id BLOB NOT NULL CHECK(length(grant_id)=32), protected BLOB NOT NULL CHECK(length(protected)>0 AND length(protected)<=262144)) STRICT";
+
+pub(super) struct Object {
+ kind: &'static str,
+ name: &'static str,
+ owner: &'static str,
+ pub sql: &'static str,
+}
+impl Object {
+ const fn table(name: &'static str, sql: &'static str) -> Self {
+ Self {
+ kind: "table",
+ name,
+ owner: name,
+ sql,
+ }
+ }
+ const fn index(name: &'static str, owner: &'static str, sql: &'static str) -> Self {
+ Self {
+ kind: "index",
+ name,
+ owner,
+ sql,
+ }
+ }
+}
+
+/// These are retained identities, never automatic migrations. Adding a schema
+/// requires an explicit catalog and its independently qualified admission path.
+pub(super) fn catalog(version: i64) -> Result<Vec<Object>, WorkspaceError> {
+ if !matches!(version, 7..=11) {
+ return Err(WorkspaceError::RecoveryRequired);
+ }
+ let artifacts = version >= 9;
+ let mut objects = vec![
+ Object::table("workspace_grant", GRANT),
+ Object::table(
+ "operations",
+ if version == 7 {
+ operation::TABLE_V7
+ } else {
+ operation::TABLE
+ },
+ ),
+ Object::index(
+ "operation_transaction_identity",
+ "operations",
+ operation::INDEX,
+ ),
+ Object::table("signing_plans", plan::TABLE),
+ Object::table("signing_plan_parts", plan::PARTS),
+ Object::table(
+ "signing_attempts",
+ if artifacts {
+ attempt::TABLE
+ } else {
+ attempt::TABLE_V8
+ },
+ ),
+ Object::index(
+ "signing_attempt_identity",
+ "signing_attempts",
+ attempt::ID_INDEX,
+ ),
+ Object::index(
+ "signing_attempt_transaction_identity",
+ "signing_attempts",
+ attempt::TX_INDEX,
+ ),
+ Object::table(
+ if artifacts {
+ "artifact_evidence"
+ } else {
+ "seal_evidence"
+ },
+ if artifacts {
+ evidence::TABLE
+ } else {
+ evidence::TABLE_V8
+ },
+ ),
+ Object::table(
+ if artifacts {
+ "artifact_evidence_parts"
+ } else {
+ "seal_evidence_parts"
+ },
+ if artifacts {
+ evidence::PARTS
+ } else {
+ evidence::PARTS_V8
+ },
+ ),
+ Object::index(
+ if artifacts {
+ "artifact_evidence_transaction_identity"
+ } else {
+ "seal_evidence_transaction_identity"
+ },
+ if artifacts {
+ "artifact_evidence"
+ } else {
+ "seal_evidence"
+ },
+ if artifacts {
+ evidence::TX_INDEX
+ } else {
+ evidence::TX_INDEX_V8
+ },
+ ),
+ Object::table("chat_messages", installation::TABLE),
+ Object::index(
+ "chat_message_operation_identity",
+ "chat_messages",
+ installation::OP_INDEX,
+ ),
+ Object::index(
+ "chat_message_transaction_identity",
+ "chat_messages",
+ installation::TX_INDEX,
+ ),
+ Object::index(
+ "chat_message_thread",
+ "chat_messages",
+ installation::THREAD_INDEX,
+ ),
+ Object::table(
+ "chat_publication_work",
+ if artifacts {
+ installation::WORK
+ } else {
+ installation::WORK_V8
+ },
+ ),
+ Object::table(
+ if version == 11 {
+ "artifact_delivery_attempts"
+ } else {
+ "chat_delivery_attempts"
+ },
+ if version == 11 {
+ delivery::TABLE
+ } else {
+ delivery::TABLE_V10
+ },
+ ),
+ Object::table(
+ if version == 11 {
+ "artifact_delivery_parts"
+ } else {
+ "chat_delivery_parts"
+ },
+ if version == 11 {
+ delivery::PARTS
+ } else {
+ delivery::PARTS_V10
+ },
+ ),
+ Object::index(
+ if version == 11 {
+ "artifact_delivery_transaction_identity"
+ } else {
+ "chat_delivery_transaction_identity"
+ },
+ if version == 11 {
+ "artifact_delivery_attempts"
+ } else {
+ "chat_delivery_attempts"
+ },
+ if version == 11 {
+ delivery::TX_INDEX
+ } else {
+ delivery::TX_INDEX_V10
+ },
+ ),
+ Object::table(
+ if version == 11 {
+ "artifact_delivery_observations"
+ } else {
+ "chat_delivery_observations"
+ },
+ if version == 11 {
+ delivery::OBSERVATIONS
+ } else {
+ delivery::OBSERVATIONS_V10
+ },
+ ),
+ ];
+ if version >= 10 {
+ objects.extend([
+ Object::table("listing_creations", listing::CREATIONS),
+ Object::index(
+ "listing_coordinate_identity",
+ "listing_creations",
+ listing::COORDINATE_INDEX,
+ ),
+ Object::table("listing_installations", super::listing_installation::TABLE),
+ Object::index(
+ "listing_installation_transaction_identity",
+ "listing_installations",
+ super::listing_installation::TX_INDEX,
+ ),
+ Object::table("public_publication_work", super::listing_installation::WORK),
+ ]);
+ }
+ Ok(objects)
+}
+
+/// The aggregate materializes one scalar, never attacker-sized schema text.
+/// Both the complete object count and each exact identity/definition must match.
+pub(super) async fn admit(
+ connection: &mut SqliteConnection,
+ version: i64,
+) -> Result<(), WorkspaceError> {
+ let objects = catalog(version)?;
+ let mut sql = String::from("SELECT CASE WHEN count(*)=? AND sum(CASE");
+ for _ in &objects {
+ sql.push_str(" WHEN type=? AND name=? AND tbl_name=? AND sql=? THEN 1");
+ }
+ sql.push_str(" ELSE 0 END)=? THEN 1 ELSE 0 END FROM sqlite_schema");
+ let mut query = sqlx::query_scalar(sqlx::AssertSqlSafe(sql)).bind(objects.len() as i64);
+ for object in &objects {
+ query = query
+ .bind(object.kind)
+ .bind(object.name)
+ .bind(object.owner)
+ .bind(object.sql);
+ }
+ let matched: i64 = query
+ .bind(objects.len() as i64)
+ .fetch_one(connection)
+ .await
+ .map_err(|_| WorkspaceError::RecoveryRequired)?;
+ if matched != 1 {
+ return Err(WorkspaceError::RecoveryRequired);
+ }
+ Ok(())
+}
diff --git a/crates/storage_sqlite/src/workspace/worker.rs b/crates/storage_sqlite/src/workspace/worker.rs
@@ -1,7 +1,6 @@
use super::{
AttachmentInput, AttachmentProof, WorkspaceBinding, WorkspaceClock, WorkspaceCrypto,
- WorkspaceError, admission::Journal, attempt, delivery, evidence, fs::Namespace, installation,
- listing, operation, plan, record::GrantRecord,
+ WorkspaceError, admission::Journal, fs::Namespace, record::GrantRecord, schema,
};
use radroots_domain::{CommandDigest, OperationId};
use radroots_storage::command::{
@@ -33,7 +32,6 @@ use tokio::sync::{oneshot, watch};
// Plan jobs retain both their canonical bytes and bounded decoded fields.
const QUEUE_MAX_BYTES: usize = 4 * 1024 * 1024;
const QUEUE_CAPACITY: usize = 32;
-const TABLE: &str = "CREATE TABLE workspace_grant (singleton INTEGER PRIMARY KEY CHECK(singleton=1), workspace BLOB NOT NULL CHECK(length(workspace)=32), grant_id BLOB NOT NULL CHECK(length(grant_id)=32), protected BLOB NOT NULL CHECK(length(protected)>0 AND length(protected)<=262144)) STRICT";
type Reply<T, E = WorkspaceError> = oneshot::Sender<Result<T, E>>;
enum Job {
CaptureDeliveryObservation(
@@ -971,49 +969,8 @@ pub(super) async fn initialize(
.begin()
.await
.map_err(|_| WorkspaceError::StorageFailure)?;
- sqlx::raw_sql(TABLE)
- .execute(&mut *transaction)
- .await
- .map_err(|_| WorkspaceError::StorageFailure)?;
- sqlx::raw_sql(operation::TABLE)
- .execute(&mut *transaction)
- .await
- .map_err(|_| WorkspaceError::StorageFailure)?;
- sqlx::raw_sql(operation::INDEX)
- .execute(&mut *transaction)
- .await
- .map_err(|_| WorkspaceError::StorageFailure)?;
- sqlx::raw_sql(plan::TABLE)
- .execute(&mut *transaction)
- .await
- .map_err(|_| WorkspaceError::StorageFailure)?;
- sqlx::raw_sql(plan::PARTS)
- .execute(&mut *transaction)
- .await
- .map_err(|_| WorkspaceError::StorageFailure)?;
- for definition in [
- attempt::TABLE,
- attempt::ID_INDEX,
- attempt::TX_INDEX,
- evidence::TABLE,
- evidence::PARTS,
- evidence::TX_INDEX,
- installation::TABLE,
- installation::OP_INDEX,
- installation::TX_INDEX,
- installation::THREAD_INDEX,
- installation::WORK,
- delivery::TABLE,
- delivery::PARTS,
- delivery::TX_INDEX,
- delivery::OBSERVATIONS,
- listing::CREATIONS,
- listing::COORDINATE_INDEX,
- super::listing_installation::TABLE,
- super::listing_installation::TX_INDEX,
- super::listing_installation::WORK,
- ] {
- sqlx::raw_sql(definition)
+ for object in schema::catalog(schema::CURRENT_VERSION)? {
+ sqlx::raw_sql(object.sql)
.execute(&mut *transaction)
.await
.map_err(|_| WorkspaceError::StorageFailure)?;
@@ -1040,103 +997,7 @@ pub(super) async fn initialize(
.fetch_one(&mut *connection)
.await
.map_err(|_| WorkspaceError::RecoveryRequired)?;
- if !matches!(version, 7..=11) {
- return Err(WorkspaceError::RecoveryRequired);
- }
- let catalog_sql = "SELECT CASE WHEN count(*)=20 AND sum(CASE WHEN type='table' AND name='workspace_grant' AND tbl_name='workspace_grant' AND sql=? THEN 1 WHEN type='table' AND name='operations' AND tbl_name='operations' AND sql=? THEN 1 WHEN type='table' AND name='signing_plans' AND tbl_name='signing_plans' AND sql=? THEN 1 WHEN type='table' AND name='signing_plan_parts' AND tbl_name='signing_plan_parts' AND sql=? THEN 1 WHEN type='table' AND name='signing_attempts' AND tbl_name='signing_attempts' AND sql=? THEN 1 WHEN type='index' AND name='signing_attempt_identity' AND tbl_name='signing_attempts' AND sql=? THEN 1 WHEN type='index' AND name='signing_attempt_transaction_identity' AND tbl_name='signing_attempts' AND sql=? THEN 1 WHEN type='table' AND name='seal_evidence' AND tbl_name='seal_evidence' AND sql=? THEN 1 WHEN type='table' AND name='seal_evidence_parts' AND tbl_name='seal_evidence_parts' AND sql=? THEN 1 WHEN type='index' AND name='seal_evidence_transaction_identity' AND tbl_name='seal_evidence' AND sql=? THEN 1 WHEN type='index' AND name='operation_transaction_identity' AND tbl_name='operations' AND sql=? THEN 1 WHEN type='table' AND name='chat_messages' AND tbl_name='chat_messages' AND sql=? THEN 1 WHEN type='index' AND name='chat_message_operation_identity' AND tbl_name='chat_messages' AND sql=? THEN 1 WHEN type='index' AND name='chat_message_transaction_identity' AND tbl_name='chat_messages' AND sql=? THEN 1 WHEN type='index' AND name='chat_message_thread' AND tbl_name='chat_messages' AND sql=? THEN 1 WHEN type='table' AND name='chat_publication_work' AND tbl_name='chat_publication_work' AND sql=? THEN 1 WHEN type='table' AND name='chat_delivery_attempts' AND tbl_name='chat_delivery_attempts' AND sql=? THEN 1 WHEN type='table' AND name='chat_delivery_parts' AND tbl_name='chat_delivery_parts' AND sql=? THEN 1 WHEN type='index' AND name='chat_delivery_transaction_identity' AND tbl_name='chat_delivery_attempts' AND sql=? THEN 1 WHEN type='table' AND name='chat_delivery_observations' AND tbl_name='chat_delivery_observations' AND sql=? THEN 1 ELSE 0 END)=20 THEN 1 ELSE 0 END FROM sqlite_schema";
- let catalog_sql = if matches!(version, 9..=11) {
- catalog_sql.replace("seal_evidence", "artifact_evidence")
- } else {
- catalog_sql.to_owned()
- };
- let catalog_sql = if matches!(version, 10 | 11) {
- catalog_sql.replace("count(*)=20", "count(*)=25").replace("END)=20", "END)=25").replace("WHEN type='table' AND name='workspace_grant'", "WHEN type='table' AND name='listing_creations' AND tbl_name='listing_creations' AND sql=? THEN 1 WHEN type='index' AND name='listing_coordinate_identity' AND tbl_name='listing_creations' AND sql=? THEN 1 WHEN type='table' AND name='listing_installations' AND tbl_name='listing_installations' AND sql=? THEN 1 WHEN type='index' AND name='listing_installation_transaction_identity' AND tbl_name='listing_installations' AND sql=? THEN 1 WHEN type='table' AND name='public_publication_work' AND tbl_name='public_publication_work' AND sql=? THEN 1 WHEN type='table' AND name='workspace_grant'")
- } else {
- catalog_sql
- };
- let catalog_sql = if version == 11 {
- catalog_sql.replace("chat_delivery", "artifact_delivery")
- } else {
- catalog_sql
- };
- let catalog_query = sqlx::query_scalar(sqlx::AssertSqlSafe(catalog_sql));
- let catalog_query = if matches!(version, 10 | 11) {
- catalog_query
- .bind(listing::CREATIONS)
- .bind(listing::COORDINATE_INDEX)
- .bind(super::listing_installation::TABLE)
- .bind(super::listing_installation::TX_INDEX)
- .bind(super::listing_installation::WORK)
- } else {
- catalog_query
- };
- let catalog_matches: i64 = catalog_query
- .bind(TABLE)
- .bind(if version == 7 {
- operation::TABLE_V7
- } else {
- operation::TABLE
- })
- .bind(plan::TABLE)
- .bind(plan::PARTS)
- .bind(if matches!(version, 9..=11) {
- attempt::TABLE
- } else {
- attempt::TABLE_V8
- })
- .bind(attempt::ID_INDEX)
- .bind(attempt::TX_INDEX)
- .bind(if matches!(version, 9..=11) {
- evidence::TABLE
- } else {
- evidence::TABLE_V8
- })
- .bind(if matches!(version, 9..=11) {
- evidence::PARTS
- } else {
- evidence::PARTS_V8
- })
- .bind(if matches!(version, 9..=11) {
- evidence::TX_INDEX
- } else {
- evidence::TX_INDEX_V8
- })
- .bind(operation::INDEX)
- .bind(installation::TABLE)
- .bind(installation::OP_INDEX)
- .bind(installation::TX_INDEX)
- .bind(installation::THREAD_INDEX)
- .bind(if matches!(version, 9..=11) {
- installation::WORK
- } else {
- installation::WORK_V8
- })
- .bind(if version == 11 {
- delivery::TABLE
- } else {
- delivery::TABLE_V10
- })
- .bind(if version == 11 {
- delivery::PARTS
- } else {
- delivery::PARTS_V10
- })
- .bind(if version == 11 {
- delivery::TX_INDEX
- } else {
- delivery::TX_INDEX_V10
- })
- .bind(if version == 11 {
- delivery::OBSERVATIONS
- } else {
- delivery::OBSERVATIONS_V10
- })
- .fetch_one(&mut *connection)
- .await
- .map_err(|_| WorkspaceError::RecoveryRequired)?;
- if catalog_matches != 1 {
- return Err(WorkspaceError::RecoveryRequired);
- }
+ schema::admit(connection, version).await?;
let integrity: String = sqlx::query_scalar("PRAGMA quick_check(1)")
.fetch_one(&mut *connection)
.await
diff --git a/crates/storage_sqlite/tests/workspace_attachment.rs b/crates/storage_sqlite/tests/workspace_attachment.rs
@@ -109,6 +109,43 @@ impl Fixture {
fn key() -> PublicKey {
PublicKey::from_hex("79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798").unwrap()
}
+
+#[tokio::test]
+async fn same_size_schema_substitution_is_refused_without_repair() {
+ use sqlx::Connection;
+
+ for statement in [
+ "DROP INDEX operation_transaction_identity; CREATE UNIQUE INDEX operation_transaction_identity ON operations(workspace,operation_id)",
+ "DROP INDEX operation_transaction_identity; CREATE INDEX foreign_index ON operations(workspace,transaction_id)",
+ "CREATE TRIGGER foreign_trigger BEFORE INSERT ON operations BEGIN SELECT RAISE(ABORT,'synthetic catalog fault'); END",
+ ] {
+ let fixture = Fixture::new();
+ let (store, input, _) = fixture.create().await;
+ store.close().await.unwrap();
+ let database = fixture.path.join("execution_v1.sqlite");
+ let options = sqlx::sqlite::SqliteConnectOptions::new().filename(&database);
+ let mut connection = sqlx::SqliteConnection::connect_with(&options)
+ .await
+ .unwrap();
+ sqlx::raw_sql(statement)
+ .execute(&mut connection)
+ .await
+ .unwrap();
+ connection.close().await.unwrap();
+ let retained = std::fs::read(&database).unwrap();
+ assert!(matches!(
+ NativeWorkspace::open_existing(
+ &fixture.path,
+ input,
+ fixture.crypto(),
+ fixture.clock.clone(),
+ )
+ .await,
+ Err(WorkspaceError::RecoveryRequired)
+ ));
+ assert_eq!(std::fs::read(&database).unwrap(), retained);
+ }
+}
fn second_key() -> PublicKey {
PublicKey::from_hex("c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5").unwrap()
}