Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
fix(collab-surface): reserve document ids and require ready surfaces for tokens (#7192)
teo529c4afe67f1
diff --git a/.github/workspace-dep-closures.json b/.github/workspace-dep-closures.jsonindex f158f6f558..eeada27566 100644--- a/.github/workspace-dep-closures.json+++ b/.github/workspace-dep-closures.json@@ -3078,6 +3078,9 @@"crates/document_sub_type","crates/entity_access","crates/entity_access_db_utils",+ "crates/entity_registry/entity_registry",+ "crates/entity_registry/entity_registry_db_utils",+ "crates/entity_registry/shared_entity_registry","crates/genai_telemetry","crates/harness_id","crates/harness_token",@@ -3088,6 +3091,7 @@"crates/lexical_client","crates/macro_auth","crates/macro_authorization",+ "crates/macro_db_client","crates/macro_entrypoint","crates/macro_env","crates/macro_env_var",@@ -3108,10 +3112,13 @@"crates/model_owner","crates/model_user","crates/models_bulk_upload",+ "crates/models_dcs","crates/models_entity_access_management",+ "crates/models_opensearch","crates/models_pagination","crates/models_permissions","crates/models_properties",+ "crates/models_team","crates/non_empty","crates/notification","crates/notification_state",@@ -3123,6 +3130,7 @@"crates/sync_service","crates/sync_service_client","crates/system_properties",+ "crates/user_quota","crates/workspace-hack"],"comms_db_client": [diff --git a/.sqlx/query-5307e0b89349055b2045f1a14c302ce32b1ac6b2869e91a54bbb24dd656bd76d.json b/.sqlx/query-5307e0b89349055b2045f1a14c302ce32b1ac6b2869e91a54bbb24dd656bd76d.jsonnew file mode 100644index 0000000000..2338a5e93c--- /dev/null+++ b/.sqlx/query-5307e0b89349055b2045f1a14c302ce32b1ac6b2869e91a54bbb24dd656bd76d.json@@ -0,0 +1,14 @@+{+ "db_name": "PostgreSQL",+ "query": "UPDATE \"Document\" SET \"deletedAt\" = now() WHERE id = $1",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Text"+ ]+ },+ "nullable": []+ },+ "hash": "5307e0b89349055b2045f1a14c302ce32b1ac6b2869e91a54bbb24dd656bd76d"+}diff --git a/.sqlx/query-647501db840c893cdf8959bc01e70520b364baa832ffa0573179af9cd9efda53.json b/.sqlx/query-647501db840c893cdf8959bc01e70520b364baa832ffa0573179af9cd9efda53.jsonnew file mode 100644index 0000000000..f446f36b8e--- /dev/null+++ b/.sqlx/query-647501db840c893cdf8959bc01e70520b364baa832ffa0573179af9cd9efda53.json@@ -0,0 +1,22 @@+{+ "db_name": "PostgreSQL",+ "query": "\n SELECT EXISTS (\n SELECT 1 FROM collab_surfaces WHERE id = $1 AND deleted_at IS NOT NULL\n ) AS \"deleted!\"\n ",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "deleted!",+ "type_info": "Bool"+ }+ ],+ "parameters": {+ "Left": [+ "Uuid"+ ]+ },+ "nullable": [+ null+ ]+ },+ "hash": "647501db840c893cdf8959bc01e70520b364baa832ffa0573179af9cd9efda53"+}diff --git a/.sqlx/query-7b960e99de9758063ef0f189bec909f9703d817af2a7aed68e6a5fc1bc94159e.json b/.sqlx/query-7b960e99de9758063ef0f189bec909f9703d817af2a7aed68e6a5fc1bc94159e.jsonnew file mode 100644index 0000000000..e47b9c574f--- /dev/null+++ b/.sqlx/query-7b960e99de9758063ef0f189bec909f9703d817af2a7aed68e6a5fc1bc94159e.json@@ -0,0 +1,15 @@+{+ "db_name": "PostgreSQL",+ "query": "INSERT INTO \"Document\" (id, name, owner, \"fileType\") VALUES ($1, 'doc', $2, 'md')",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Text",+ "Text"+ ]+ },+ "nullable": []+ },+ "hash": "7b960e99de9758063ef0f189bec909f9703d817af2a7aed68e6a5fc1bc94159e"+}diff --git a/Cargo.lock b/Cargo.lockindex b4af896e7b..7c14ad205f 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -4227,6 +4227,7 @@ dependencies = ["entity_access","lexical_client","macro_authorization",+ "macro_db_client","macro_db_migrator","macro_sync_service_jwt","macro_user_id",diff --git a/apps/web/src/lib/service-clients/service-storage/openapi.json b/apps/web/src/lib/service-clients/service-storage/openapi.jsonindex c7119e0c3e..fbb58e3cf9 100644--- a/apps/web/src/lib/service-clients/service-storage/openapi.json+++ b/apps/web/src/lib/service-clients/service-storage/openapi.json@@ -4922,6 +4922,16 @@}}},+ "409": {+ "description": "The surface id is already in use",+ "content": {+ "application/json": {+ "schema": {+ "$ref": "#/components/schemas/ErrorResponse"+ }+ }+ }+ },"410": {"description": "The surface id was deleted and cannot be reused","content": {@@ -5068,6 +5078,16 @@}}},+ "409": {+ "description": "The surface is not initialized yet, or its id is in use",+ "content": {+ "application/json": {+ "schema": {+ "$ref": "#/components/schemas/ErrorResponse"+ }+ }+ }+ },"500": {"description": "","content": {diff --git a/crates/collab_surface/Cargo.toml b/crates/collab_surface/Cargo.tomlindex c27f976c71..be267352e0 100644--- a/crates/collab_surface/Cargo.toml+++ b/crates/collab_surface/Cargo.toml@@ -16,7 +16,7 @@ inbound = []outbound = ["dep:lexical_client", "dep:sync_service_client", "ports"]ports = ["dep:entity_access", "dep:tracing"]-postgres = ["dep:sqlx", "outbound"]+postgres = ["dep:macro_db_client", "dep:sqlx", "outbound"][dependencies]axum = { workspace = true, optional = true }@@ -24,6 +24,7 @@ chrono = { workspace = true, features = ["serde"] }entity_access = { path = "../entity_access", optional = true, default-features = false, features = ["ports"] }lexical_client = { path = "../lexical_client", optional = true }macro_authorization = { path = "../macro_authorization", optional = true, default-features = false, features = ["axum"] }+macro_db_client = { path = "../macro_db_client", optional = true }macro_sync_service_jwt = { path = "../macro_sync_service_jwt" }macro_user_id = { path = "../macro_user_id" }macro_uuid = { path = "../macro_uuid" }@@ -36,6 +37,7 @@ serde = { workspace = true }sqlx = { workspace = true, optional = true }sync_service_client = { path = "../sync_service_client", optional = true }thiserror = { workspace = true }+tokio = { workspace = true }tokio-retry = { workspace = true }tracing = { workspace = true, optional = true }utoipa = { workspace = true, optional = true }diff --git a/crates/collab_surface/src/domain/models.rs b/crates/collab_surface/src/domain/models.rsindex bb472bf87a..9f227d21ea 100644--- a/crates/collab_surface/src/domain/models.rs+++ b/crates/collab_surface/src/domain/models.rs@@ -72,6 +72,15 @@ pub enum CollabSurfaceError {/// loudly instead of looking like a race to retry.#[error("this surface id was deleted and cannot be reused")]Gone,+ /// The surface id is taken in the sync-service document namespace that+ /// surfaces share: it names a document, or (for a new surface) already has+ /// a session. A new surface only ever creates its own session.+ #[error("this surface id is already in use")]+ IdReserved,+ /// The surface exists but has not proven its sync-service session is its+ /// own (initialization failed or never ran), so it cannot be connected to.+ #[error("collab surface is not ready")]+ NotReady,/// The request was invalid.#[error("{0}")]BadRequest(String),diff --git a/crates/collab_surface/src/domain/ports.rs b/crates/collab_surface/src/domain/ports.rsindex 6db30bd161..d05351c5d3 100644--- a/crates/collab_surface/src/domain/ports.rs+++ b/crates/collab_surface/src/domain/ports.rs@@ -38,9 +38,25 @@ pub trait CollabSurfaceRepo: Send + Sync + 'static {/// Soft-delete a surface. Idempotent.fn soft_delete(&self, id: Uuid) -> impl Future<Output = Result<(), Self::Err>> + Send;++ /// Whether `id` belongs to a soft-deleted surface.+ fn is_deleted(&self, id: Uuid) -> impl Future<Output = Result<bool, Self::Err>> + Send;+}++/// Outbound port onto the document id namespace, which surfaces share in+/// sync-service. `outbound::document_ids::PgDocumentIds` implements it over+/// macro_db_client's `does_document_exist` helper, which owns reads of the+/// documents table.+pub trait DocumentIds: Send + Sync + 'static {+ /// Whether `id` names a document, live or soft-deleted.+ fn is_document_id(+ &self,+ id: Uuid,+ ) -> impl Future<Output = Result<bool, rootcause::Report>> + Send;}-/// Outbound port that boots a surface's sync-service session from markdown.+/// Outbound port for a surface's sync-service session: boots it from markdown+/// and checks whether it exists.////// Implementations convert the markdown to a Loro snapshot (an empty string/// maps to the canonical blank-document snapshot) and store it as the@@ -54,6 +70,15 @@ pub trait SurfaceInitializer: Send + Sync + 'static {surface_id: &str,markdown: &str,) -> impl Future<Output = Result<(), CollabSurfaceError>> + Send;++ /// Whether a session for `surface_id` exists. Only asked for a new id,+ /// before its row is inserted. Sync-service answers yes as soon as a+ /// session knows its id (a client connected, say), not only once it is+ /// initialized: the conservative answer for an id nothing should use yet.+ fn session_exists(+ &self,+ surface_id: &str,+ ) -> impl Future<Output = Result<bool, CollabSurfaceError>> + Send;}/// The collab-surface use-cases, generic over the outbound ports.@@ -67,6 +92,10 @@ pub trait CollabSurfaceService: Send + Sync + 'static {/// row (an earlier ensure died or failed mid-init) has its/// initialization retried./// - soft-deleted → [`CollabSurfaceError::Gone`]; ids are never reused.+ /// - not a random (v4 or v7) UUID → [`CollabSurfaceError::BadRequest`].+ /// - the id names a document, or a new id already has a sync-service+ /// session → [`CollabSurfaceError::IdReserved`], before any row is+ /// written: a new surface only ever creates its own session.////// Concurrent ensures for the same id converge: the insert is/// conflict-tolerant and the initializer treats an already-initialized@@ -96,8 +125,10 @@ pub trait CollabSurfaceService: Send + Sync + 'static {id: Uuid,) -> impl Future<Output = Result<Entity<'static>, CollabSurfaceError>> + Send;- /// Mint a sync-service connection token for the surface, at the access- /// level implied by the caller's permission on the parent entity.+ /// Mint a sync-service connection token for a `ready` surface, at the+ /// access level implied by the caller's permission on the parent entity.+ /// A surface whose id names a document is refused+ /// ([`CollabSurfaceError::IdReserved`]), whenever it was bound.fn mint_token(&self,user_id: &MacroUserIdStr<'_>,diff --git a/crates/collab_surface/src/domain/service.rs b/crates/collab_surface/src/domain/service.rsindex 6c22d3f81c..3168005fde 100644--- a/crates/collab_surface/src/domain/service.rs+++ b/crates/collab_surface/src/domain/service.rs@@ -12,25 +12,29 @@ use model_entity::Entity;use uuid::Uuid;use crate::domain::models::{CollabSurface, CollabSurfaceError, SurfaceState};-use crate::domain::ports::{CollabSurfaceRepo, CollabSurfaceService, SurfaceInitializer};+use crate::domain::ports::{+ CollabSurfaceRepo, CollabSurfaceService, DocumentIds, SurfaceInitializer,+};use crate::domain::token::{access_level_for, encode_surface_token};/// Upper bound on initial markdown, mirroring the lexical-service request cap.const MAX_INITIAL_MARKDOWN_LEN: usize = 1_000_000;/// Production implementation of [`CollabSurfaceService`].-pub struct CollabSurfaceServiceImpl<R, I> {+pub struct CollabSurfaceServiceImpl<R, I, D> {repo: Arc<R>,initializer: Arc<I>,+ documents: Arc<D>,jwt_secret: String,}-impl<R, I> CollabSurfaceServiceImpl<R, I> {+impl<R, I, D> CollabSurfaceServiceImpl<R, I, D> {/// Build the service from its ports and the sync-service JWT secret.- pub fn new(repo: Arc<R>, initializer: Arc<I>, jwt_secret: String) -> Self {+ pub fn new(repo: Arc<R>, initializer: Arc<I>, documents: Arc<D>, jwt_secret: String) -> Self {Self {repo,initializer,+ documents,jwt_secret,}}@@ -58,6 +62,15 @@ fn resolve_parent(.with_entity_string(entity.entity_id.to_string()))}+/// Surface ids are chosen by the caller, so the public API only takes random+/// ones (UUID v4 or v7).+fn is_random_id(id: Uuid) -> bool {+ matches!(+ id.get_version(),+ Some(uuid::Version::Random | uuid::Version::SortRand)+ )+}+/// Verify a receipt (already minted against the surface's parent by the/// inbound layer) actually names that parent. Defense in depth: the inbound/// layer resolves the parent via [`CollabSurfaceService::get_parent`], so a@@ -75,10 +88,11 @@ fn verify_receipt_matches_parent(Ok(())}-impl<R, I> CollabSurfaceService for CollabSurfaceServiceImpl<R, I>+impl<R, I, D> CollabSurfaceService for CollabSurfaceServiceImpl<R, I, D>whereR: CollabSurfaceRepo,I: SurfaceInitializer,+ D: DocumentIds,{#[tracing::instrument(err, skip(self, user_id, parent_receipt, initial_markdown))]async fn ensure_surface(@@ -88,6 +102,11 @@ whereid: Uuid,initial_markdown: String,) -> Result<CollabSurface, CollabSurfaceError> {+ if !is_random_id(id) {+ return Err(CollabSurfaceError::BadRequest(+ "surface ids must be random UUIDs".to_string(),+ ));+ }if initial_markdown.len() > MAX_INITIAL_MARKDOWN_LEN {return Err(CollabSurfaceError::BadRequest(format!("initial markdown exceeds {MAX_INITIAL_MARKDOWN_LEN} bytes"@@ -99,9 +118,18 @@ where// its initialization retried in `finish_init`.if let Some(existing) = self.get_optional(id).await? {verify_receipt_matches_parent(&existing, &parent)?;+ if existing.state == SurfaceState::Pending {+ // A document may have taken the id since the row was written.+ self.refuse_document_id(id).await?;+ }return self.finish_init(existing, &initial_markdown).await;}+ // A new surface creates its own session, so its id must be free in the+ // namespace surfaces share with documents: no document and no session+ // yet. Checked before inserting, so a refusal leaves no row behind.+ self.refuse_taken_id(id).await?;+let now = chrono::Utc::now();let surface = CollabSurface {id,@@ -118,8 +146,9 @@ where.map_err(|e| rootcause::Report::new(e).into_dynamic())?;if !inserted {- // Lost a race with a concurrent ensure, or the id belongs to a- // soft-deleted surface (which never comes back).+ // Lost a race with a concurrent ensure (which passed the same+ // checks), or the id belongs to a soft-deleted surface (which never+ // comes back).let Some(existing) = self.get_optional(id).await? else {return Err(CollabSurfaceError::Gone);};@@ -158,6 +187,14 @@ wherelet parent = resolve_parent(user_id, &parent_receipt)?;let surface = self.get_live(id).await?;verify_receipt_matches_parent(&surface, &parent)?;+ // A pending row has not proven that its session is the surface's own+ // (initialization failed or never ran), so it is not connectable.+ if surface.state != SurfaceState::Ready {+ return Err(CollabSurfaceError::NotReady);+ }+ // Checked on every mint, not only at creation, so a surface whose id+ // names a document never connects to it, however it was bound.+ self.refuse_document_id(surface.id).await?;let access_level = access_level_for(parent_receipt.entity_permission())?;encode_surface_token(@@ -198,10 +235,11 @@ where}}-impl<R, I> CollabSurfaceServiceImpl<R, I>+impl<R, I, D> CollabSurfaceServiceImpl<R, I, D>whereR: CollabSurfaceRepo,I: SurfaceInitializer,+ D: DocumentIds,{/// Fetch a live (non-deleted) surface or `NotFound`.async fn get_live(&self, id: Uuid) -> Result<CollabSurface, CollabSurfaceError> {@@ -219,10 +257,45 @@ where.map_err(|e| rootcause::Report::new(e).into_dynamic())?)}+ /// Refuse an id that names a document. Surfaces share the document+ /// namespace in sync-service, so a surface never uses a document's id.+ async fn refuse_document_id(&self, id: Uuid) -> Result<(), CollabSurfaceError> {+ if self.documents.is_document_id(id).await? {+ return Err(CollabSurfaceError::IdReserved);+ }+ Ok(())+ }++ /// Refuse a new id that names a document or already has a sync-service+ /// session: the initializer would take that session as this surface's. A+ /// deleted surface keeps its session, so its id is `Gone` rather than+ /// reserved; that is only looked up once the check fires.+ async fn refuse_taken_id(&self, id: Uuid) -> Result<(), CollabSurfaceError> {+ let session_id = id.to_string();+ let (names_document, has_session) = tokio::try_join!(+ async { Ok::<_, CollabSurfaceError>(self.documents.is_document_id(id).await?) },+ self.initializer.session_exists(&session_id),+ )?;+ if !(names_document || has_session) {+ return Ok(());+ }+ let deleted = self+ .repo+ .is_deleted(id)+ .await+ .map_err(|e| rootcause::Report::new(e).into_dynamic())?;+ Err(if deleted {+ CollabSurfaceError::Gone+ } else {+ CollabSurfaceError::IdReserved+ })+ }+/// Take a surface the caller may act on to `Ready`, (re)initializing its/// sync-service session when it is still `Pending`. The initializer treats/// an already-initialized session as success, so this is safe to run- /// concurrently and after partial failures.+ /// concurrently and after partial failures: a pending row whose session+ /// was initialized before `mark_ready` failed heals here.async fn finish_init(&self,surface: CollabSurface,diff --git a/crates/collab_surface/src/domain/service/test.rs b/crates/collab_surface/src/domain/service/test.rsindex bbfe1cea0a..870ceb833c 100644--- a/crates/collab_surface/src/domain/service/test.rs+++ b/crates/collab_surface/src/domain/service/test.rs@@ -44,11 +44,31 @@ fn edit_permission() -> EntityPermission {}}-/// In-memory repo: one optional surface plus operation flags.+/// In-memory repo: one optional surface plus operation flags. Also stands in+/// for the document namespace.#[derive(Default)]struct MemRepo {surface: std::sync::Mutex<Option<CollabSurface>>,soft_deleted: AtomicBool,+ /// Whether every id reads as naming an existing document.+ document_ids: AtomicBool,+ /// How many times the document namespace was consulted.+ document_lookups: AtomicUsize,+ /// How many upcoming `mark_ready` calls fail.+ mark_ready_failures: AtomicUsize,+}++impl MemRepo {+ /// A repo already holding `surface`, as an earlier ensure left it.+ fn holding(surface: CollabSurface) -> Arc<Self> {+ let repo = Arc::new(Self::default());+ *repo.surface.lock().unwrap() = Some(surface);+ repo+ }++ fn stored(&self) -> Option<CollabSurface> {+ self.surface.lock().unwrap().clone()+ }}#[derive(Debug, thiserror::Error)]@@ -82,6 +102,13 @@ impl CollabSurfaceRepo for Arc<MemRepo> {}async fn mark_ready(&self, _id: Uuid) -> Result<(), MemErr> {+ if self+ .mark_ready_failures+ .fetch_update(Ordering::SeqCst, Ordering::SeqCst, |n| n.checked_sub(1))+ .is_ok()+ {+ return Err(MemErr);+ }if let Some(s) = self.surface.lock().unwrap().as_mut() {s.state = SurfaceState::Ready;}@@ -92,19 +119,69 @@ impl CollabSurfaceRepo for Arc<MemRepo> {self.soft_deleted.store(true, Ordering::SeqCst);Ok(())}++ async fn is_deleted(&self, id: Uuid) -> Result<bool, MemErr> {+ Ok(self.soft_deleted.load(Ordering::SeqCst)+ && self+ .surface+ .lock()+ .unwrap()+ .as_ref()+ .is_some_and(|s| s.id == id))+ }}-fn service_with(- repo: Arc<MemRepo>,- initializer: MockSurfaceInitializer,-) -> CollabSurfaceServiceImpl<Arc<MemRepo>, MockSurfaceInitializer> {- CollabSurfaceServiceImpl::new(Arc::new(repo), Arc::new(initializer), SECRET.to_string())+impl DocumentIds for Arc<MemRepo> {+ async fn is_document_id(&self, _id: Uuid) -> Result<bool, rootcause::Report> {+ self.document_lookups.fetch_add(1, Ordering::SeqCst);+ Ok(self.document_ids.load(Ordering::SeqCst))+ }+}++type TestService = CollabSurfaceServiceImpl<Arc<MemRepo>, MockSurfaceInitializer, Arc<MemRepo>>;++fn service_with(repo: Arc<MemRepo>, initializer: MockSurfaceInitializer) -> TestService {+ CollabSurfaceServiceImpl::new(+ Arc::new(repo.clone()),+ Arc::new(initializer),+ Arc::new(repo),+ SECRET.to_string(),+ )+}++/// An initializer that finds no session under any id yet.+fn no_sessions() -> MockSurfaceInitializer {+ let mut init = MockSurfaceInitializer::new();+ init.expect_session_exists()+ .returning(|_| Box::pin(async { Ok(false) }));+ init+}++/// A pending surface an earlier ensure left behind.+fn pending_surface(id: Uuid) -> CollabSurface {+ let now = chrono::Utc::now();+ CollabSurface {+ id,+ parent: EntityType::Channel.with_entity_string("chan-1".to_string()),+ state: SurfaceState::Pending,+ created_at: now,+ updated_at: now,+ }+}++fn channel_receipt() -> EntityAccessReceipt<AnyEntityPermission> {+ receipt_for(+ "macro|[email protected]",+ EntityType::Channel,+ "chan-1",+ edit_permission(),+ )}#[tokio::test]async fn ensure_creates_initializes_then_marks_ready() {let repo = Arc::new(MemRepo::default());- let mut init = MockSurfaceInitializer::new();+ let mut init = no_sessions();init.expect_initialize().times(1).returning(|_, _| Box::pin(async { Ok(()) }));@@ -133,7 +210,7 @@ async fn ensure_creates_initializes_then_marks_ready() {#[tokio::test]async fn ensure_is_idempotent_for_a_ready_surface() {let repo = Arc::new(MemRepo::default());- let mut init = MockSurfaceInitializer::new();+ let mut init = no_sessions();// Exactly one initialization across both ensures: the second sees a// ready surface and does not touch the initializer.init.expect_initialize()@@ -172,7 +249,7 @@ async fn ensure_is_idempotent_for_a_ready_surface() {#[tokio::test]async fn ensure_retries_init_for_a_pending_surface() {let repo = Arc::new(MemRepo::default());- let mut init = MockSurfaceInitializer::new();+ let mut init = no_sessions();// First ensure: init fails, row stays pending. Second ensure: init// succeeds and the surface becomes ready.let calls = Arc::new(AtomicUsize::new(0));@@ -222,7 +299,7 @@ async fn ensure_retries_init_for_a_pending_surface() {#[tokio::test]async fn ensure_maps_insert_conflict_on_deleted_id_to_gone() {let repo = Arc::new(MemRepo::default());- let mut init = MockSurfaceInitializer::new();+ let mut init = no_sessions();init.expect_initialize().returning(|_, _| Box::pin(async { Ok(()) }));@@ -254,7 +331,7 @@ async fn ensure_maps_insert_conflict_on_deleted_id_to_gone() {#[tokio::test]async fn ensure_rejects_mismatched_parent() {let repo = Arc::new(MemRepo::default());- let mut init = MockSurfaceInitializer::new();+ let mut init = no_sessions();init.expect_initialize().returning(|_, _| Box::pin(async { Ok(()) }));let svc = service_with(repo, init);@@ -305,7 +382,7 @@ async fn ensure_rejects_receipt_for_other_user() {#[tokio::test]async fn mint_token_maps_channel_role_to_edit() {let repo = Arc::new(MemRepo::default());- let mut init = MockSurfaceInitializer::new();+ let mut init = no_sessions();init.expect_initialize().returning(|_, _| Box::pin(async { Ok(()) }));let svc = service_with(repo, init);@@ -344,7 +421,7 @@ async fn mint_token_maps_channel_role_to_edit() {#[tokio::test]async fn mint_token_rejects_receipt_for_wrong_parent() {let repo = Arc::new(MemRepo::default());- let mut init = MockSurfaceInitializer::new();+ let mut init = no_sessions();init.expect_initialize().returning(|_, _| Box::pin(async { Ok(()) }));let svc = service_with(repo, init);@@ -382,7 +459,7 @@ async fn mint_token_rejects_receipt_for_wrong_parent() {#[tokio::test]async fn delete_requires_edit_capable_permission() {let repo = Arc::new(MemRepo::default());- let mut init = MockSurfaceInitializer::new();+ let mut init = no_sessions();init.expect_initialize().returning(|_, _| Box::pin(async { Ok(()) }));let svc = service_with(repo.clone(), init);@@ -433,3 +510,337 @@ async fn delete_requires_edit_capable_permission() {let gone = svc.get_parent(surface.id).await.unwrap_err();assert!(matches!(gone, CollabSurfaceError::NotFound));}++#[tokio::test]+async fn ensure_refuses_an_id_that_names_a_document() {+ let repo = Arc::new(MemRepo::default());+ repo.document_ids.store(true, Ordering::SeqCst);+ // The initializer would report the document's session as "already+ // initialized" and let the surface adopt it; it must never be reached.+ let svc = service_with(repo.clone(), no_sessions());+ let receipt = receipt_for(+ "macro|[email protected]",+ EntityType::Document,+ "doc-2",+ EntityPermission::AccessLevel {+ access_level: AccessLevel::Owner,+ },+ );++ let id = surface_id();+ for _ in 0..2 {+ let err = svc+ .ensure_surface(&user("macro|[email protected]"), receipt.clone(), id, String::new())+ .await+ .unwrap_err();+ assert!(matches!(err, CollabSurfaceError::IdReserved));+ }+ // Nothing is left behind binding the id to the caller's parent.+ assert!(repo.surface.lock().unwrap().is_none());+}++#[tokio::test]+async fn a_pending_surface_with_a_document_id_is_never_initialized() {+ let repo = Arc::new(MemRepo::default());+ repo.document_ids.store(true, Ordering::SeqCst);+ let id = surface_id();+ let now = chrono::Utc::now();+ // A pending row for a document id, as written before ids were reserved.+ *repo.surface.lock().unwrap() = Some(CollabSurface {+ id,+ parent: EntityType::Document.with_entity_string("doc-1".to_string()),+ state: SurfaceState::Pending,+ created_at: now,+ updated_at: now,+ });+ let svc = service_with(repo.clone(), MockSurfaceInitializer::new());+ let receipt = receipt_for(+ "macro|[email protected]",+ EntityType::Document,+ "doc-1",+ edit_permission(),+ );++ let err = svc+ .ensure_surface(&user("macro|[email protected]"), receipt, id, String::new())+ .await+ .unwrap_err();++ assert!(matches!(err, CollabSurfaceError::IdReserved));+ assert_eq!(+ repo.surface.lock().unwrap().as_ref().unwrap().state,+ SurfaceState::Pending+ );+}++#[tokio::test]+async fn mint_token_refuses_a_pending_surface() {+ let repo = Arc::new(MemRepo::default());+ let mut init = no_sessions();+ init.expect_initialize().returning(|_, _| {+ Box::pin(async {+ Err(CollabSurfaceError::Internal(+ rootcause::Report::new(MemErr).into_dynamic(),+ ))+ })+ });+ let svc = service_with(repo.clone(), init);+ let id = surface_id();+ let receipt = || {+ receipt_for(+ "macro|[email protected]",+ EntityType::Channel,+ "chan-1",+ edit_permission(),+ )+ };++ svc.ensure_surface(&user("macro|[email protected]"), receipt(), id, String::new())+ .await+ .unwrap_err();+ assert_eq!(+ repo.surface.lock().unwrap().as_ref().unwrap().state,+ SurfaceState::Pending+ );++ // A pending row has not proven its session is its own.+ let err = svc+ .mint_token(&user("macro|[email protected]"), receipt(), id)+ .await+ .unwrap_err();+ assert!(matches!(err, CollabSurfaceError::NotReady));+}++#[tokio::test]+async fn parent_comment_access_mints_a_read_only_token() {+ let repo = Arc::new(MemRepo::default());+ let mut init = no_sessions();+ init.expect_initialize()+ .returning(|_, _| Box::pin(async { Ok(()) }));+ let svc = service_with(repo, init);+ let id = surface_id();+ let receipt = |access_level| {+ receipt_for(+ "macro|[email protected]",+ EntityType::Document,+ "doc-1",+ EntityPermission::AccessLevel { access_level },+ )+ };+ svc.ensure_surface(+ &user("macro|[email protected]"),+ receipt(AccessLevel::Edit),+ id,+ String::new(),+ )+ .await+ .unwrap();++ for (parent, minted) in [+ (AccessLevel::View, AccessLevel::View),+ (AccessLevel::Comment, AccessLevel::View),+ (AccessLevel::Edit, AccessLevel::Edit),+ (AccessLevel::Owner, AccessLevel::Owner),+ ] {+ let token = svc+ .mint_token(&user("macro|[email protected]"), receipt(parent), id)+ .await+ .unwrap();+ let claims: model::document::DocumentPermissionsToken =+ macro_sync_service_jwt::decode(token.as_str(), SECRET).unwrap();+ assert_eq!(claims.document_id, id.to_string());+ assert_eq!(claims.access_level, minted, "{parent:?}");+ }+}++#[tokio::test]+async fn a_fresh_ensure_checks_the_document_namespace_once() {+ let repo = Arc::new(MemRepo::default());+ let mut init = no_sessions();+ init.expect_initialize()+ .times(1)+ .returning(|_, _| Box::pin(async { Ok(()) }));+ let svc = service_with(repo.clone(), init);++ let surface = svc+ .ensure_surface(+ &user("macro|[email protected]"),+ channel_receipt(),+ surface_id(),+ String::new(),+ )+ .await+ .unwrap();++ assert_eq!(surface.state, SurfaceState::Ready);+ assert_eq!(repo.document_lookups.load(Ordering::SeqCst), 1);+}++#[tokio::test]+async fn ensure_refuses_an_id_that_already_has_a_session() {+ let repo = Arc::new(MemRepo::default());+ let mut init = MockSurfaceInitializer::new();+ // The id has a session no surface created; it must never be initialized+ // over (the initializer would report it as already initialized).+ init.expect_session_exists()+ .returning(|_| Box::pin(async { Ok(true) }));+ let svc = service_with(repo.clone(), init);++ let err = svc+ .ensure_surface(+ &user("macro|[email protected]"),+ channel_receipt(),+ surface_id(),+ String::new(),+ )+ .await+ .unwrap_err();++ assert!(matches!(err, CollabSurfaceError::IdReserved));+ // Refused before anything is written.+ assert!(repo.stored().is_none());+}++#[tokio::test]+async fn a_pending_surface_whose_session_exists_heals_on_retry() {+ let repo = Arc::new(MemRepo::default());+ // The first ensure initializes the session, then fails to mark it ready.+ repo.mark_ready_failures.store(1, Ordering::SeqCst);+ let mut init = MockSurfaceInitializer::new();+ // Only the fresh ensure checks for a session; the retry trusts its row.+ init.expect_session_exists()+ .times(1)+ .returning(|_| Box::pin(async { Ok(false) }));+ // The retry finds the session the first ensure created, which the+ // initializer reports as success.+ init.expect_initialize()+ .times(2)+ .returning(|_, _| Box::pin(async { Ok(()) }));+ let svc = service_with(repo.clone(), init);+ let id = surface_id();++ svc.ensure_surface(&user("macro|[email protected]"), channel_receipt(), id, String::new())+ .await+ .unwrap_err();+ assert_eq!(repo.stored().unwrap().state, SurfaceState::Pending);++ let surface = svc+ .ensure_surface(&user("macro|[email protected]"), channel_receipt(), id, String::new())+ .await+ .unwrap();+ assert_eq!(surface.state, SurfaceState::Ready);+}++#[tokio::test]+async fn concurrent_ensures_of_a_new_id_both_end_ready() {+ let repo = Arc::new(MemRepo::default());+ let mut init = MockSurfaceInitializer::new();+ // Both ensures pass the pre-insert checks before either inserts.+ let checked = Arc::new(AtomicUsize::new(0));+ init.expect_session_exists().times(2).returning(move |_| {+ let checked = checked.clone();+ Box::pin(async move {+ checked.fetch_add(1, Ordering::SeqCst);+ while checked.load(Ordering::SeqCst) < 2 {+ tokio::task::yield_now().await;+ }+ Ok(false)+ })+ });+ init.expect_initialize()+ .times(1..=2)+ .returning(|_, _| Box::pin(async { Ok(()) }));+ let svc = service_with(repo.clone(), init);+ let id = surface_id();++ let caller = user("macro|[email protected]");+ let (first, second) = tokio::join!(+ svc.ensure_surface(&caller, channel_receipt(), id, String::new()),+ svc.ensure_surface(&caller, channel_receipt(), id, String::new()),+ );++ // The loser re-reads the winner's row: no extra lookups, no `Gone`.+ assert_eq!(first.unwrap().state, SurfaceState::Ready);+ assert_eq!(second.unwrap().state, SurfaceState::Ready);+ assert_eq!(repo.document_lookups.load(Ordering::SeqCst), 2);+}++#[tokio::test]+async fn mint_token_refuses_a_ready_surface_whose_id_names_a_document() {+ let id = surface_id();+ // A ready binding to a document's id, as written before ids were reserved.+ let repo = MemRepo::holding(CollabSurface {+ state: SurfaceState::Ready,+ ..pending_surface(id)+ });+ repo.document_ids.store(true, Ordering::SeqCst);+ let svc = service_with(repo, MockSurfaceInitializer::new());++ let err = svc+ .mint_token(&user("macro|[email protected]"), channel_receipt(), id)+ .await+ .unwrap_err();++ assert!(matches!(err, CollabSurfaceError::IdReserved));+}++#[tokio::test]+async fn ensure_on_a_deleted_id_is_gone_even_though_its_session_remains() {+ let id = surface_id();+ // Deletion leaves the surface's sync-service session behind.+ let repo = MemRepo::holding(CollabSurface {+ state: SurfaceState::Ready,+ ..pending_surface(id)+ });+ repo.soft_deleted.store(true, Ordering::SeqCst);+ let mut init = MockSurfaceInitializer::new();+ init.expect_session_exists()+ .returning(|_| Box::pin(async { Ok(true) }));+ let svc = service_with(repo, init);++ let err = svc+ .ensure_surface(&user("macro|[email protected]"), channel_receipt(), id, String::new())+ .await+ .unwrap_err();++ assert!(matches!(err, CollabSurfaceError::Gone));+}++#[tokio::test]+async fn ensure_accepts_random_ids() {+ let repo = Arc::new(MemRepo::default());+ let mut init = no_sessions();+ init.expect_initialize()+ .times(2)+ .returning(|_, _| Box::pin(async { Ok(()) }));+ let svc = service_with(repo.clone(), init);++ // v4, as a browser's crypto.randomUUID() makes, and v7.+ for id in [uuid::Uuid::new_v4(), surface_id()] {+ let surface = svc+ .ensure_surface(&user("macro|[email protected]"), channel_receipt(), id, String::new())+ .await+ .unwrap();+ assert_eq!(surface.state, SurfaceState::Ready);+ *repo.surface.lock().unwrap() = None;+ }+}++#[tokio::test]+async fn ensure_refuses_ids_that_are_not_random() {+ let repo = Arc::new(MemRepo::default());+ let svc = service_with(repo.clone(), MockSurfaceInitializer::new());++ for id in [+ uuid::Uuid::new_v5(&uuid::Uuid::NAMESPACE_OID, b"surface"),+ uuid::Uuid::nil(),+ ] {+ let err = svc+ .ensure_surface(&user("macro|[email protected]"), channel_receipt(), id, String::new())+ .await+ .unwrap_err();+ assert!(matches!(err, CollabSurfaceError::BadRequest(_)));+ }+ assert!(repo.stored().is_none());+ assert_eq!(repo.document_lookups.load(Ordering::SeqCst), 0);+}diff --git a/crates/collab_surface/src/domain/token.rs b/crates/collab_surface/src/domain/token.rsindex 89de698ea3..af450129bb 100644--- a/crates/collab_surface/src/domain/token.rs+++ b/crates/collab_surface/src/domain/token.rs@@ -21,10 +21,15 @@ use crate::domain::models::CollabSurfaceError;/// The sync-service access level implied by a parent-entity permission.////// Channel members collaborate as editors; a view-only channel presence gets a-/// read-only session. Team roles have no surface semantics yet and fail-/// closed.+/// read-only session. Surfaces have no comment layer, and sync-service lets a+/// Comment grant write to a document-namespace session, so comment access on+/// the parent is read-only here. Team roles have no surface semantics yet and+/// fail closed.pub fn access_level_for(permission: &EntityPermission) -> Result<AccessLevel, CollabSurfaceError> {match permission {+ EntityPermission::AccessLevel {+ access_level: AccessLevel::Comment,+ } => Ok(AccessLevel::View),EntityPermission::AccessLevel { access_level } => Ok(*access_level),EntityPermission::ChannelRole { .. } => Ok(AccessLevel::Edit),EntityPermission::ChannelViewOnly => Ok(AccessLevel::View),diff --git a/crates/collab_surface/src/inbound/axum_router.rs b/crates/collab_surface/src/inbound/axum_router.rsindex a38fdc8bd6..190ea1ab52 100644--- a/crates/collab_surface/src/inbound/axum_router.rs+++ b/crates/collab_surface/src/inbound/axum_router.rs@@ -271,6 +271,7 @@ where(status = 401, description = "Missing or invalid credentials", body = ErrorResponse),(status = 403, description = "No access to the parent entity", body = ErrorResponse),(status = 404, description = "The parent entity does not exist", body = ErrorResponse),+ (status = 409, description = "The surface id is already in use", body = ErrorResponse),(status = 410, description = "The surface id was deleted and cannot be reused", body = ErrorResponse),(status = 422, description = "Malformed request body (plain text)"),(status = 500, body = ErrorResponse),@@ -356,6 +357,7 @@ where(status = 401, description = "Missing or invalid credentials", body = ErrorResponse),(status = 403, body = ErrorResponse),(status = 404, body = ErrorResponse),+ (status = 409, description = "The surface is not initialized yet, or its id is in use", body = ErrorResponse),(status = 500, body = ErrorResponse),))]@@ -431,6 +433,7 @@ impl IntoResponse for CollabSurfaceError {StatusCode::NOT_FOUND}CollabSurfaceError::Gone => StatusCode::GONE,+ CollabSurfaceError::IdReserved | CollabSurfaceError::NotReady => StatusCode::CONFLICT,CollabSurfaceError::BadRequest(_) => StatusCode::BAD_REQUEST,CollabSurfaceError::AccessDenied => StatusCode::FORBIDDEN,CollabSurfaceError::Internal(_) => StatusCode::INTERNAL_SERVER_ERROR,diff --git a/crates/collab_surface/src/outbound.rs b/crates/collab_surface/src/outbound.rsindex 1a2255b52b..5078cf1e46 100644--- a/crates/collab_surface/src/outbound.rs+++ b/crates/collab_surface/src/outbound.rs@@ -1,5 +1,7 @@//! Outbound (driven) adapters for collab surfaces.+#[cfg(feature = "postgres")]+pub mod document_ids;#[cfg(feature = "postgres")]pub mod pg_collab_surface_repo;pub mod surface_init;diff --git a/crates/collab_surface/src/outbound/document_ids.rs b/crates/collab_surface/src/outbound/document_ids.rsnew file mode 100644index 0000000000..75e993196e--- /dev/null+++ b/crates/collab_surface/src/outbound/document_ids.rs@@ -0,0 +1,35 @@+//! The document id namespace, which surfaces share in sync-service, read+//! through macro_db_client's helper for the documents table.++use sqlx::PgPool;+use uuid::Uuid;++use crate::domain::ports::DocumentIds;++/// [`DocumentIds`] over macro_db_client's `does_document_exist`.+#[derive(Debug, Clone)]+pub struct PgDocumentIds {+ pool: PgPool,+}++impl PgDocumentIds {+ /// Read document ids from the shared Postgres pool.+ pub fn new(pool: PgPool) -> Self {+ Self { pool }+ }+}++impl DocumentIds for PgDocumentIds {+ #[tracing::instrument(err, skip(self))]+ async fn is_document_id(&self, id: Uuid) -> Result<bool, rootcause::Report> {+ // Document ids are canonical lowercase text, and the sync-service+ // session key is that exact spelling. Soft-deleted documents keep their+ // session, so the helper deliberately counts them.+ macro_db_client::dcs::does_document_exist::does_document_exist(+ self.pool.clone(),+ &id.to_string(),+ )+ .await+ .map_err(|e| rootcause::report!("failed to look up document id {id}: {e:?}").into_dynamic())+ }+}diff --git a/crates/collab_surface/src/outbound/pg_collab_surface_repo.rs b/crates/collab_surface/src/outbound/pg_collab_surface_repo.rsindex 01678d1ae4..bd3f28f692 100644--- a/crates/collab_surface/src/outbound/pg_collab_surface_repo.rs+++ b/crates/collab_surface/src/outbound/pg_collab_surface_repo.rs@@ -171,6 +171,19 @@ impl CollabSurfaceRepo for PgCollabSurfaceRepo {Ok(())}+ async fn is_deleted(&self, id: Uuid) -> Result<bool, CollabSurfaceRepoErr> {+ Ok(sqlx::query_scalar!(+ r#"+ SELECT EXISTS (+ SELECT 1 FROM collab_surfaces WHERE id = $1 AND deleted_at IS NOT NULL+ ) AS "deleted!"+ "#,+ id,+ )+ .fetch_one(&self.pool)+ .await?)+ }+async fn soft_delete(&self, id: Uuid) -> Result<(), CollabSurfaceRepoErr> {sqlx::query!(r#"diff --git a/crates/collab_surface/src/outbound/pg_collab_surface_repo/test.rs b/crates/collab_surface/src/outbound/pg_collab_surface_repo/test.rsindex 0e01d62cbf..07c00ce360 100644--- a/crates/collab_surface/src/outbound/pg_collab_surface_repo/test.rs+++ b/crates/collab_surface/src/outbound/pg_collab_surface_repo/test.rs@@ -105,3 +105,22 @@ async fn list_by_parent_with_non_uuid_parent_reads_empty(pool: PgPool) {.unwrap();assert!(listed.is_empty());}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn is_deleted_reads_soft_deleted_rows_only(pool: PgPool) {+ let repo = PgCollabSurfaceRepo::new(pool);+ let live = new_surface(EntityType::Channel, CHANNEL_1);+ let deleted = new_surface(EntityType::Channel, CHANNEL_1);+ repo.insert(&live).await.unwrap();+ repo.insert(&deleted).await.unwrap();+ repo.soft_delete(deleted.id).await.unwrap();++ assert!(repo.is_deleted(deleted.id).await.unwrap());+ assert!(!repo.is_deleted(live.id).await.unwrap());+ assert!(+ !repo+ .is_deleted(macro_uuid::generate_uuid_v7())+ .await+ .unwrap()+ );+}diff --git a/crates/collab_surface/src/outbound/surface_init.rs b/crates/collab_surface/src/outbound/surface_init.rsindex 63552a8b21..70a1f9456b 100644--- a/crates/collab_surface/src/outbound/surface_init.rs+++ b/crates/collab_surface/src/outbound/surface_init.rs@@ -70,7 +70,8 @@ impl SurfaceInitializer for LexicalSyncSurfaceInitializer {Ok(()) => Ok(()),// Initialization is one-shot on the sync-service side, so "snapshot// already exists" means an earlier or concurrent ensure won the- // init — success for our purposes. This is what makes `ensure`+ // init — success for our purposes (a new id was checked to have no+ // session before its row was written). This is what makes `ensure`// idempotent across retries and races.Err(e) if e.to_string().contains("snapshot already exists") => {tracing::debug!(@@ -85,4 +86,19 @@ impl SurfaceInitializer for LexicalSyncSurfaceInitializer {)),}}++ #[tracing::instrument(err, skip(self))]+ async fn session_exists(&self, surface_id: &str) -> Result<bool, CollabSurfaceError> {+ // `exists` is true once the durable object has the id set (e.g. after a+ // client connected), which is broader than "initialized".+ self.sync_service_client+ .exists(surface_id)+ .await+ .map_err(|e| {+ CollabSurfaceError::Internal(+ rootcause::report!("failed to check sync-service session: {e:?}")+ .into_dynamic(),+ )+ })+ }}diff --git a/crates/documents/src/domain/service.rs b/crates/documents/src/domain/service.rsindex aabfccd29f..e47b70b0a0 100644--- a/crates/documents/src/domain/service.rs+++ b/crates/documents/src/domain/service.rs@@ -608,6 +608,33 @@ impl<}}+ /// A new document starts its own collaborative session, so a caller-chosen+ /// id must not already have one: the document would take it over instead.+ /// Only random (v4/v7) ids can name a collab surface's session, the kind a+ /// document must never adopt. Other ids, such as the deterministic v5 ids+ /// of starter documents, can retry after a partial create left a session.+ async fn refuse_existing_session(&self, id: Option<uuid::Uuid>) -> Result<(), DocumentError> {+ let Some(id) = id.filter(|id| {+ matches!(+ id.get_version(),+ Some(uuid::Version::Random | uuid::Version::SortRand)+ )+ }) else {+ return Ok(());+ };+ if self+ .sync_service_client+ .exists(&id.to_string())+ .await+ .map_err(DocumentError::Internal)?+ {+ return Err(DocumentError::Conflict(+ "document id is already in use".to_string(),+ ));+ }+ Ok(())+ }+/// Clean up a document on creation error.async fn cleanup_document(&self, document_id: &str) {if let Err(e) = self.repo.delete_document_by_id(document_id).await {@@ -1306,6 +1333,7 @@ impl<}document.team_id = Some(*team);}+ self.refuse_existing_session(document.id).await?;let owner = principal.owner();let file_type = document.file_type;@@ -1355,6 +1383,14 @@ impl<});}+ // Imports reuse documents by attachment and content, so their ids are+ // always server-chosen and never meet an existing session.+ if args.document.id.is_some() {+ return Err(DocumentError::BadRequest(+ "email attachment imports cannot choose a document id".to_string(),+ ));+ }+let file_type = args.document.file_type;let project_id = args.document.project_id;let sha = args.document.sha.clone();diff --git a/crates/documents/src/domain/service/tests.rs b/crates/documents/src/domain/service/tests.rsindex e5b2f0cfb3..b49cc106d9 100644--- a/crates/documents/src/domain/service/tests.rs+++ b/crates/documents/src/domain/service/tests.rs@@ -3169,3 +3169,142 @@ fn presigned_url_path_for_bot_and_team_owners_uses_their_principals() {"https://cdn.example.test/00000000-0000-0000-0000-000000000456/doc-1/7");}++/// A random (v4) id: the kind a collab surface could also hold.+fn random_id(seed: u128) -> uuid::Uuid {+ uuid::Builder::from_random_bytes(seed.to_be_bytes()).into_uuid()+}++fn sync_with_session(+ id: uuid::Uuid,+ exists: bool,+) -> crate::domain::ports::sync::MockDocumentSyncPort {+ let mut sync = crate::domain::ports::sync::MockDocumentSyncPort::new();+ sync.expect_exists()+ .withf(move |document_id| document_id == id.to_string())+ .times(1)+ .returning(move |_| Box::pin(std::future::ready(Ok(exists))));+ sync+}++#[tokio::test]+async fn a_caller_chosen_id_that_already_has_a_session_is_refused() {+ let id = random_id(42);+ let mut repo = make_mock_repo();+ repo.expect_get_owner_team().times(0);+ repo.expect_create_document().times(0);+ let service = spreadsheet_test_service(repo, sync_with_session(id, true));+ let mut document = new_document(FileType::Md);+ document.id = Some(id);++ let err = DocumentService::create_document(+ &service,+ &CreationPrincipal::User(test_user()),+ document,+ None,+ )+ .await+ .unwrap_err();++ assert!(matches!(err, DocumentError::Conflict(_)));+}++#[tokio::test]+async fn a_caller_chosen_id_without_a_session_is_created() {+ let id = random_id(43);+ let mut repo = make_mock_repo();+ repo.expect_get_owner_team()+ .returning(|_| Box::pin(std::future::ready(Ok(None))));+ repo.expect_create_document()+ .withf(move |args, _| args.document.id == Some(id))+ .times(1)+ .returning(|_, _| {+ let mut metadata = make_test_metadata();+ metadata.file_type = Some("spreadsheet".to_string());+ Box::pin(std::future::ready(Ok(metadata)))+ });+ repo.expect_set_document_content()+ .returning(|_, _| Box::pin(std::future::ready(Ok(()))));+ repo.expect_get_team_task_metadata()+ .returning(|_| Box::pin(std::future::ready(Ok(None))));+ let mut sync = sync_with_session(id, false);+ sync.expect_initialize_spreadsheet()+ .times(1)+ .returning(|_| Box::pin(std::future::ready(Ok(()))));+ let service = spreadsheet_test_service(repo, sync);+ let mut document = spreadsheet_document();+ document.id = Some(id);++ DocumentService::create_document(+ &service,+ &CreationPrincipal::User(test_user()),+ document,+ None,+ )+ .await+ .unwrap();+}++#[tokio::test]+async fn a_deterministic_id_with_a_leftover_session_is_created() {+ // Starter documents use v5 ids, which no collab surface can have, so a+ // retry after a partial create that left a session still creates them.+ let id = uuid::Uuid::new_v5(&uuid::Uuid::NAMESPACE_OID, b"starter-document");+ let mut repo = make_mock_repo();+ repo.expect_get_owner_team()+ .returning(|_| Box::pin(std::future::ready(Ok(None))));+ repo.expect_create_document()+ .withf(move |args, _| args.document.id == Some(id))+ .times(1)+ .returning(|_, _| {+ let mut metadata = make_test_metadata();+ metadata.file_type = Some("spreadsheet".to_string());+ Box::pin(std::future::ready(Ok(metadata)))+ });+ repo.expect_set_document_content()+ .returning(|_, _| Box::pin(std::future::ready(Ok(()))));+ repo.expect_get_team_task_metadata()+ .returning(|_| Box::pin(std::future::ready(Ok(None))));+ let mut sync = crate::domain::ports::sync::MockDocumentSyncPort::new();+ sync.expect_exists().times(0);+ sync.expect_initialize_spreadsheet()+ .times(1)+ .returning(|_| Box::pin(std::future::ready(Ok(()))));+ let service = spreadsheet_test_service(repo, sync);+ let mut document = spreadsheet_document();+ document.id = Some(id);++ DocumentService::create_document(+ &service,+ &CreationPrincipal::User(test_user()),+ document,+ None,+ )+ .await+ .unwrap();+}++#[tokio::test]+async fn an_email_import_cannot_choose_its_document_id() {+ let mut repo = make_mock_repo();+ repo.expect_get_owner_team().times(0);+ repo.expect_import_email_attachment_document().times(0);+ let mut sync = crate::domain::ports::sync::MockDocumentSyncPort::new();+ sync.expect_exists().times(0);+ let service = spreadsheet_test_service(repo, sync);+ let mut document = new_document(FileType::Pdf);+ document.id = Some(random_id(44));++ let err = DocumentService::import_email_attachment(+ &service,+ ImportEmailAttachmentRepoArgs {+ email_attachment_id: uuid::Uuid::from_u128(45),+ owner: test_user(),+ document,+ },+ )+ .await+ .unwrap_err();++ assert!(matches!(err, DocumentError::BadRequest(_)));+}diff --git a/crates/macro_db_client/src/dcs/does_document_exist.rs b/crates/macro_db_client/src/dcs/does_document_exist.rsindex b76ce89935..4768d4c387 100644--- a/crates/macro_db_client/src/dcs/does_document_exist.rs+++ b/crates/macro_db_client/src/dcs/does_document_exist.rs@@ -1,3 +1,6 @@+#[cfg(test)]+mod test;+use sqlx::{Pool, Postgres};#[tracing::instrument(skip(db))]diff --git a/crates/macro_db_client/src/dcs/does_document_exist/test.rs b/crates/macro_db_client/src/dcs/does_document_exist/test.rsnew file mode 100644index 0000000000..582fbd9e9c--- /dev/null+++ b/crates/macro_db_client/src/dcs/does_document_exist/test.rs@@ -0,0 +1,31 @@+use sqlx::{Pool, Postgres};++use super::*;++#[sqlx::test]+async fn counts_live_and_soft_deleted_documents(pool: Pool<Postgres>) {+ let owner = "macro|[email protected]";+ for id in ["live-document", "deleted-document"] {+ sqlx::query!(+ r#"INSERT INTO "Document" (id, name, owner, "fileType") VALUES ($1, 'doc', $2, 'md')"#,+ id,+ owner,+ )+ .execute(&pool)+ .await+ .unwrap();+ }+ sqlx::query!(+ r#"UPDATE "Document" SET "deletedAt" = now() WHERE id = $1"#,+ "deleted-document",+ )+ .execute(&pool)+ .await+ .unwrap();++ // A soft-deleted document keeps its id (and its sync-service session).+ for id in ["live-document", "deleted-document"] {+ assert!(does_document_exist(pool.clone(), id).await.unwrap(), "{id}");+ }+ assert!(!does_document_exist(pool, "missing-document").await.unwrap());+}diff --git a/packages/sdk/generated/storage/types.gen.ts b/packages/sdk/generated/storage/types.gen.tsindex 40818537d7..e94fed2125 100644--- a/packages/sdk/generated/storage/types.gen.ts+++ b/packages/sdk/generated/storage/types.gen.ts@@ -15600,6 +15600,10 @@ export type EnsureCollabSurfaceErrors = {* The parent entity does not exist*/404: ErrorResponse;+ /**+ * The surface id is already in use+ */+ 409: ErrorResponse;/*** The surface id was deleted and cannot be reused*/@@ -15638,6 +15642,10 @@ export type CreateCollabSurfaceTokenErrors = {401: ErrorResponse;403: ErrorResponse;404: ErrorResponse;+ /**+ * The surface is not initialized yet, or its id is in use+ */+ 409: ErrorResponse;500: ErrorResponse;};diff --git a/packages/sdk/specs/storage.json b/packages/sdk/specs/storage.jsonindex c7119e0c3e..fbb58e3cf9 100644--- a/packages/sdk/specs/storage.json+++ b/packages/sdk/specs/storage.json@@ -4922,6 +4922,16 @@}}},+ "409": {+ "description": "The surface id is already in use",+ "content": {+ "application/json": {+ "schema": {+ "$ref": "#/components/schemas/ErrorResponse"+ }+ }+ }+ },"410": {"description": "The surface id was deleted and cannot be reused","content": {@@ -5068,6 +5078,16 @@}}},+ "409": {+ "description": "The surface is not initialized yet, or its id is in use",+ "content": {+ "application/json": {+ "schema": {+ "$ref": "#/components/schemas/ErrorResponse"+ }+ }+ }+ },"500": {"description": "","content": {diff --git a/services/document_storage_service/src/api/context.rs b/services/document_storage_service/src/api/context.rsindex 8cce2c895b..cbc268fd79 100644--- a/services/document_storage_service/src/api/context.rs+++ b/services/document_storage_service/src/api/context.rs@@ -660,8 +660,11 @@ pub(crate) type DssInitiativeState =InitiativeRouterState<InitiativeServiceType, EntityAccessService, AuthorizationService>;/// Type alias for the collab-surface service.-pub(crate) type CollabSurfaceServiceType =- CollabSurfaceServiceImpl<PgCollabSurfaceRepo, LexicalSyncSurfaceInitializer>;+pub(crate) type CollabSurfaceServiceType = CollabSurfaceServiceImpl<+ PgCollabSurfaceRepo,+ LexicalSyncSurfaceInitializer,+ collab_surface::outbound::document_ids::PgDocumentIds,+>;/// Type alias for the collab-surface router state.pub(crate) type DssCollabSurfaceState =diff --git a/services/document_storage_service/src/main.rs b/services/document_storage_service/src/main.rsindex e7e5b73b10..dba9d5d42b 100644--- a/services/document_storage_service/src/main.rs+++ b/services/document_storage_service/src/main.rs@@ -1458,6 +1458,9 @@ async fn run() -> anyhow::Result<()> {lexical_client.as_ref().clone(),sync_service_client.as_ref().clone(),)),+ Arc::new(collab_surface::outbound::document_ids::PgDocumentIds::new(+ db.clone(),+ )),config.document_permission_jwt.as_ref().to_string(),);