Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

sync-service: implement document delete and stop logging the exists probe as an error (#7894)

teo1b6f3b2fdb4a
diff --git a/services/sync-service/src/d1.rs b/services/sync-service/src/d1.rs
index c539c94dbd..838562a934 100644
--- a/services/sync-service/src/d1.rs
+++ b/services/sync-service/src/d1.rs
@@ -137,6 +137,19 @@ pub async fn insert_blame_many(env: &worker::Env, events: &[BlameEvent]) -> work
Ok(())
}
+/// Delete every peer mapping and blame row recorded for a document.
+pub async fn delete_document_rows(db: D1Database, document_id: &str) -> worker::Result<()> {
+ let statements = [
+ "DELETE FROM peer_user_map WHERE document_id = ?;",
+ "DELETE FROM blame WHERE document_id = ?;",
+ ]
+ .into_iter()
+ .map(|sql| db.prepare(sql).bind(&[document_id.into()]))
+ .collect::<worker::Result<Vec<_>>>()?;
+ db.batch(statements).await?;
+ Ok(())
+}
+
#[derive(serde::Deserialize, serde::Serialize)]
pub struct BlameRow {
pub peer_id: String,
diff --git a/services/sync-service/src/durable_object.rs b/services/sync-service/src/durable_object.rs
index 33ad674117..f86b173b9b 100644
--- a/services/sync-service/src/durable_object.rs
+++ b/services/sync-service/src/durable_object.rs
@@ -50,6 +50,7 @@ pub mod status_codes {
const DOCUMENT_ID_KEY: &str = "DOCUMENT_ID";
mod document_api;
+mod document_delete;
mod document_effects;
mod surface_api;
pub(crate) mod surface_migration;
@@ -59,6 +60,7 @@ use surface_api::{SurfaceLifecycle, session_kind_from_storage_key};
mod path {
pub const CONNECT: &str = "connect";
+ pub const DELETE: &str = "delete";
pub const EXISTS: &str = "exists";
pub const INITIALIZE: &str = "initialize";
pub const RAW: &str = "raw";
@@ -426,8 +428,9 @@ impl DocumentSyncSession {
.await;
}
- // EXIST, PEER, and WAKEUP don't require auth
+ // EXIST, PEER, and WAKEUP don't require auth; DELETE requires the internal key
(path_needs_claims, Some(document_id)) => match path_needs_claims {
+ path::DELETE => return self.delete_handler(&req, document_id).await,
path::EXISTS => return self.exists_handler(document_id).await,
path::PEER => {
return self
@@ -822,15 +825,10 @@ impl DocumentSyncSession {
}
/// Check if provided document_id exists.
- /// 1. is self.document_id set
- /// 2. is document_id it do::kv
- /// 3. does snapshot exist with document_id?
+ /// 1. is self.document_id set, or is document_id in do::kv
+ /// 2. does snapshot exist with document_id?
async fn exists(&self, document_id: &str) -> Result<bool> {
- if self.document_id_is_some() {
- return Ok(true);
- }
- // This gets document_id via dokv if it exists
- if self.document_id().await.is_ok() {
+ if let Ok(Some(_)) = self.try_document_id().await {
return Ok(true);
}
// self.session_storage would not be set because it requires self.document_id be set
@@ -864,32 +862,42 @@ impl DocumentSyncSession {
#[instrument(skip_all, err)]
/// Get the `document_id`. First try checking self.document_id, if not there get from DOKV.
async fn document_id(&self) -> Result<Arc<String>> {
+ self.try_document_id()
+ .await?
+ .ok_or(Error::from("DOCUMENT_ID not found in storage"))
+ }
+
+ /// Like [`Self::document_id`], but `None` instead of an error when this
+ /// object holds no session.
+ async fn try_document_id(&self) -> Result<Option<Arc<String>>> {
if let Some(id) = self
.document_id
.lock("DocumentSyncSession::document_id get within main document_id fn")
.as_ref()
.cloned()
{
- return Ok(id);
+ return Ok(Some(id));
}
- let id: Arc<String> = Arc::new(
- self.state
- .storage()
- .get(DOCUMENT_ID_KEY)
- .await
- .with_context(|| {
- format!(
- "Could not get document_id via DOCUMENT_ID_KEY = [{}] from DO storage",
- DOCUMENT_ID_KEY
- )
- })?
- .ok_or(Error::from("DOCUMENT_ID not found in storage"))?,
- );
+ let Some(id) = self
+ .state
+ .storage()
+ .get::<String>(DOCUMENT_ID_KEY)
+ .await
+ .with_context(|| {
+ format!(
+ "Could not get document_id via DOCUMENT_ID_KEY = [{}] from DO storage",
+ DOCUMENT_ID_KEY
+ )
+ })?
+ else {
+ return Ok(None);
+ };
+ let id = Arc::new(id);
*self
.document_id
.lock("DocumentSyncSession::document_id set within main document_id fn") =
Some(id.clone());
- Ok(id)
+ Ok(Some(id))
}
async fn session_storage(&self) -> Result<Rc<SessionStorage>> {
@@ -940,6 +948,9 @@ pub static ROUTER: LazyLock<Router<&str>> = LazyLock::new(|| {
router
.insert("/document/{document_id}/connect", path::CONNECT)
.unwrap();
+ router
+ .insert("/document/{document_id}/delete", path::DELETE)
+ .unwrap();
router
.insert("/document/{document_id}/exists", path::EXISTS)
.unwrap();
@@ -1243,6 +1254,11 @@ impl DurableObject for DocumentSyncSession {
return Ok(());
}
worker_rs_otel::scope(&self.env, &self.state, async {
+ // Sockets closed by `delete_handler` report back after the session is gone.
+ if self.try_document_id().await?.is_none() {
+ self.forget_websocket_metadata(&ws).await;
+ return Ok(());
+ }
self.validate_surface_sockets(None).await?;
let peer_ids = Wsm::new(self, &ws).get_peer_ids().await?;
for peer_id in peer_ids {
diff --git a/services/sync-service/src/durable_object/document_delete.rs b/services/sync-service/src/durable_object/document_delete.rs
new file mode 100644
index 0000000000..e905d7f24f
--- /dev/null
+++ b/services/sync-service/src/durable_object/document_delete.rs
@@ -0,0 +1,63 @@
+//! Permanent erasure of a document session. Internal-only: the document storage
+//! service calls this after it has deleted the document and its files.
+
+use tracing::warn;
+use worker::{Method, Request, Response, Result};
+
+use super::{DocumentSyncSession, response, status_codes};
+use crate::{
+ auth::is_internal,
+ constants::USER_PEER_D1_BINDING,
+ d1::delete_document_rows,
+ storage::{get_snapshot_storage, snapshot::SnapshotStorage},
+};
+
+impl DocumentSyncSession {
+ /// Erase the snapshot, operation log, socket metadata, peer mappings, and
+ /// blame for `document_id`. Idempotent: a document that never had a
+ /// session is already deleted.
+ pub(super) async fn delete_handler(
+ &self,
+ req: &Request,
+ document_id: &str,
+ ) -> Result<Response> {
+ if !is_internal(req, &self.env)? {
+ return Ok(response(status_codes::UNAUTH));
+ }
+ if req.method() != Method::Delete {
+ return Ok(response(405));
+ }
+ for socket in self.state.get_websockets() {
+ if let Err(error) = socket.close(Some(1008), Some("document deleted")) {
+ warn!(error = ?error, "failed to close socket of deleted document");
+ }
+ }
+ delete_document_rows(self.env.d1(USER_PEER_D1_BINDING)?, document_id).await?;
+ // The KV fallback snapshot lives outside this object. Remove it before
+ // emptying local storage, or `exists` would restore the session from it.
+ get_snapshot_storage(&self.env, &self.state, document_id.to_owned())?
+ .delete_snapshot()
+ .await?;
+ let storage = self.state.storage();
+ // Before compatibility date 2026-02-24, `delete_all` keeps the alarm.
+ storage.delete_alarm().await?;
+ storage.delete_all().await?;
+ self.forget_session();
+ Ok(response(status_codes::OK))
+ }
+
+ fn forget_session(&self) {
+ *self.document_id.lock("delete document id") = None;
+ *self.document_state.lock("delete document state") = None;
+ *self.session_storage.lock("delete session storage") = None;
+ *self.surface_lifecycle.lock("delete surface lifecycle") = None;
+ *self.source_migration.lock("delete source state") = None;
+ self.ws_meta_map.lock("delete socket metadata").clear();
+ self.inbound.lock("delete inbound buffers").clear();
+ self.pending_blame.lock("delete pending blame").clear();
+ self.pending_editors.lock("delete pending editors").clear();
+ for key in self.awareness.keys() {
+ self.awareness.delete(&key);
+ }
+ }
+}
diff --git a/services/sync-service/tests/delete_endpoint.test.ts b/services/sync-service/tests/delete_endpoint.test.ts
new file mode 100644
index 0000000000..a43690a84b
--- /dev/null
+++ b/services/sync-service/tests/delete_endpoint.test.ts
@@ -0,0 +1,133 @@
+import { Miniflare } from "miniflare";
+import { LoroDoc } from "loro-crdt";
+import { beforeEach, describe, expect, test } from "vitest";
+import {
+ INTERNAL_API_SECRET,
+ createTestUser,
+ getTokenForDocument,
+ setupMiniflare,
+ sleep,
+} from "./utils";
+
+let mf: Miniflare;
+
+beforeEach(async () => {
+ mf = await setupMiniflare();
+});
+
+const documentUrl = (documentId: string, operation: string) =>
+ `http://localhost:8787/document/${documentId}/${operation}`;
+
+const internalHeaders = { "x-internal-auth-key": INTERNAL_API_SECRET };
+
+const deleteDocument = (
+ documentId: string,
+ headers: Record<string, string> = internalHeaders,
+ method = "DELETE",
+) => mf.dispatchFetch(documentUrl(documentId, "delete"), { method, headers });
+
+const existsStatus = async (documentId: string) =>
+ (await mf.dispatchFetch(documentUrl(documentId, "exists"))).status;
+
+const rawContent = async (documentId: string) => {
+ const token = getTokenForDocument(documentId, "test-user", "owner");
+ const response = await mf.dispatchFetch(documentUrl(documentId, "raw"), {
+ headers: { Authorization: `Bearer ${token}` },
+ });
+ return { status: response.status, body: await response.text() };
+};
+
+const rowCount = async (table: "peer_user_map" | "blame", documentId: string) => {
+ const db = await mf.getD1Database("USER_PEER_MAPPING");
+ const row = await db
+ .prepare(`SELECT COUNT(*) AS n FROM ${table} WHERE document_id = ?`)
+ .bind(documentId)
+ .first<{ n: number }>();
+ return row?.n;
+};
+
+const insertBlame = async (documentId: string) => {
+ const db = await mf.getD1Database("USER_PEER_MAPPING");
+ await db
+ .prepare(
+ "INSERT INTO blame (document_id, node_id, peer_id, timestamp_ms) VALUES (?, 'node', '1', 0)",
+ )
+ .bind(documentId)
+ .run();
+};
+
+describe("delete endpoint", () => {
+ test("requires the internal key", async () => {
+ const user = await createTestUser(mf, "doc");
+ const ownerToken = getTokenForDocument("doc", "test-user", "owner");
+
+ expect((await deleteDocument("doc", {})).status).toBe(401);
+ expect((await deleteDocument("doc", { Authorization: `Bearer ${ownerToken}` })).status).toBe(401);
+ expect((await deleteDocument("doc", { "x-internal-auth-key": "wrong" })).status).toBe(401);
+ expect(await existsStatus("doc")).toBe(200);
+
+ user.connection.getWebSocket().close();
+ });
+
+ test("rejects methods other than DELETE", async () => {
+ expect((await deleteDocument("doc", internalHeaders, "POST")).status).toBe(405);
+ });
+
+ test("erases the document's session, peers, and blame without touching others", async () => {
+ const user = await createTestUser(mf, "doc", { userId: "user-a" });
+ user.makeChange("confidential");
+ const other = await createTestUser(mf, "other", { userId: "user-b" });
+ other.makeChange("kept");
+ await sleep(200);
+ await insertBlame("doc");
+ await insertBlame("other");
+
+ expect((await rawContent("doc")).body).toContain("confidential");
+ expect(await rowCount("peer_user_map", "doc")).toBe(1);
+
+ const closed = new Promise<{ code: number; reason: string }>((resolve) =>
+ user.connection
+ .getWebSocket()
+ .addEventListener("close", (event) => resolve({ code: event.code, reason: event.reason })),
+ );
+
+ expect((await deleteDocument("doc")).status).toBe(200);
+ expect(await closed).toEqual({ code: 1008, reason: "document deleted" });
+
+ expect(await existsStatus("doc")).toBe(404);
+ expect((await rawContent("doc")).status).toBe(404);
+ expect(await rowCount("peer_user_map", "doc")).toBe(0);
+ expect(await rowCount("blame", "doc")).toBe(0);
+
+ expect(await existsStatus("other")).toBe(200);
+ expect((await rawContent("other")).body).toContain("kept");
+ expect(await rowCount("peer_user_map", "other")).toBe(1);
+ expect(await rowCount("blame", "other")).toBe(1);
+
+ // A new session must not replay the deleted operation log.
+ const reconnected = await createTestUser(mf, "doc", { userId: "user-a" });
+ expect(reconnected.getState()).toBe("");
+
+ reconnected.connection.getWebSocket().close();
+ other.connection.getWebSocket().close();
+ });
+
+ test("removes the KV fallback snapshot", async () => {
+ const doc = new LoroDoc();
+ doc.getText("content").insert(0, "from kv");
+ const kv = await mf.getKVNamespace("SNAPSHOT_STORE_KV");
+ await kv.put("legacy/legacy.snapshot", doc.export({ mode: "snapshot" }));
+ expect(await existsStatus("legacy")).toBe(200);
+
+ expect((await deleteDocument("legacy")).status).toBe(200);
+
+ expect(await kv.get("legacy/legacy.snapshot")).toBeNull();
+ expect(await existsStatus("legacy")).toBe(404);
+ });
+
+ test("succeeds for a document that never had a session", async () => {
+ expect((await deleteDocument("never-synced")).status).toBe(200);
+ expect((await deleteDocument("never-synced")).status).toBe(200);
+ expect(await existsStatus("never-synced")).toBe(404);
+ });
+});
diff --git a/services/sync-service/tests/utils.ts b/services/sync-service/tests/utils.ts
index c42d6f6db8..9c42968a3f 100644
--- a/services/sync-service/tests/utils.ts
+++ b/services/sync-service/tests/utils.ts
@@ -17,14 +17,17 @@ export const sleep = (millis) => new Promise(resolve => setTimeout(resolve, mill
async function migrateDatabase(mf: Miniflare) {
const db = await mf.getD1Database("USER_PEER_MAPPING");
- const migration = fs.readFileSync("database/user-peer-mapping/migrations/0001_add_users.sql", "utf8");
- const statements = migration
- .split(";")
- .map((statement) => statement.trim())
- .filter((statement) => statement.length > 0);
-
- for (const statement of statements) {
- await db.prepare(statement).run();
+ const migrationsDir = "database/user-peer-mapping/migrations";
+ for (const file of fs.readdirSync(migrationsDir).sort()) {
+ const migration = fs.readFileSync(`${migrationsDir}/${file}`, "utf8");
+ const statements = migration
+ .split(";")
+ .map((statement) => statement.trim())
+ .filter((statement) => statement.length > 0);
+
+ for (const statement of statements) {
+ await db.prepare(statement).run();
+ }
}
}