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(auth): delete user_onboarding row during account deletion (#7618)
Will Hutchinson22b3c9014567
diff --git a/.github/workspace-dep-closures.json b/.github/workspace-dep-closures.jsonindex 93756f1f81..f2ff67c96f 100644--- a/.github/workspace-dep-closures.json+++ b/.github/workspace-dep-closures.json@@ -1891,6 +1891,7 @@"crates/gtm_invite","crates/harness_id","crates/harness_token",+ "crates/import","crates/initiative","crates/invite_email","crates/ip_extractor",@@ -1920,6 +1921,9 @@"crates/macro_tower_layers","crates/macro_user_id","crates/macro_uuid",+ "crates/mcp_client",+ "crates/mcp_select",+ "crates/mcp_toolset","crates/mention_utils","crates/messages","crates/microsoft_oauth_grant_db_utils",@@ -1946,6 +1950,8 @@"crates/non_empty","crates/notification","crates/notification_state",+ "crates/onboarding",+ "crates/pipedream_mcp","crates/pptx_engine","crates/prompt","crates/properties",diff --git a/.sqlx/query-64aa3a93393e7f41ac67116ad6913b4d46f0b406f538e85152d0aac7f107e95c.json b/.sqlx/query-64aa3a93393e7f41ac67116ad6913b4d46f0b406f538e85152d0aac7f107e95c.jsonnew file mode 100644index 0000000000..70ee9f88a1--- /dev/null+++ b/.sqlx/query-64aa3a93393e7f41ac67116ad6913b4d46f0b406f538e85152d0aac7f107e95c.json@@ -0,0 +1,14 @@+{+ "db_name": "PostgreSQL",+ "query": "\n DELETE FROM user_onboarding\n WHERE user_id = $1\n ",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Text"+ ]+ },+ "nullable": []+ },+ "hash": "64aa3a93393e7f41ac67116ad6913b4d46f0b406f538e85152d0aac7f107e95c"+}diff --git a/Cargo.lock b/Cargo.lockindex 7e76b38a60..12fe80c3bf 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -1984,6 +1984,7 @@ dependencies = ["native_app_service","non_empty","notification",+ "onboarding","rand 0.9.4","rate_limit","redis",@@ -13389,6 +13390,7 @@ dependencies = ["generic_email_domains","import","macro_authorization",+ "macro_db_migrator","macro_user_id","mcp_client","mcp_select",diff --git a/crates/onboarding/Cargo.toml b/crates/onboarding/Cargo.tomlindex 531e6cd780..254b1bc3e6 100644--- a/crates/onboarding/Cargo.toml+++ b/crates/onboarding/Cargo.toml@@ -27,3 +27,6 @@ tracing = { workspace = true }utoipa = { workspace = true }uuid = { workspace = true }workspace-hack = { version = "0.1", path = "../workspace-hack" }++[dev-dependencies]+macro_db_migrator = { path = "../macro_db_migrator" }diff --git a/crates/onboarding/src/domain/ports.rs b/crates/onboarding/src/domain/ports.rsindex 0366a087f4..75cefcbc4b 100644--- a/crates/onboarding/src/domain/ports.rs+++ b/crates/onboarding/src/domain/ports.rs@@ -43,4 +43,10 @@ pub trait OnboardingRepo: Send + Sync + 'static {user: &MacroUserIdStr<'static>,skipped: bool,) -> impl Future<Output = Result<OnboardingRow>> + Send;++ /// Remove the user's onboarding row, for account deletion. Idempotent: a+ /// user who never entered the flow (or whose row is already gone from an+ /// earlier attempt) is not an error.+ fn delete_row(&self, user: &MacroUserIdStr<'static>)+ -> impl Future<Output = Result<()>> + Send;}diff --git a/crates/onboarding/src/domain/service/test.rs b/crates/onboarding/src/domain/service/test.rsindex 7b22632770..b31e0812d8 100644--- a/crates/onboarding/src/domain/service/test.rs+++ b/crates/onboarding/src/domain/service/test.rs@@ -73,6 +73,10 @@ impl OnboardingRepo for MockRepo {..active_row()})}++ async fn delete_row(&self, _user: &MacroUserIdStr<'static>) -> Result<()> {+ Ok(())+ }}struct MockStore {diff --git a/crates/onboarding/src/outbound/pg_onboarding_repo.rs b/crates/onboarding/src/outbound/pg_onboarding_repo.rsindex 6105f86813..b063c196f3 100644--- a/crates/onboarding/src/outbound/pg_onboarding_repo.rs+++ b/crates/onboarding/src/outbound/pg_onboarding_repo.rs@@ -7,6 +7,9 @@ use macro_user_id::user_id::MacroUserIdStr;use sqlx::PgPool;use std::str::FromStr;+#[cfg(test)]+mod test;+/// Postgres-backed onboarding repository (MacroDB).#[derive(Clone)]pub struct PgOnboardingRepo {@@ -105,6 +108,20 @@ impl OnboardingRepo for PgOnboardingRepo {.await?;row.try_into()}++ #[tracing::instrument(skip(self), err)]+ async fn delete_row(&self, user: &MacroUserIdStr<'static>) -> Result<()> {+ sqlx::query!(+ r#"+ DELETE FROM user_onboarding+ WHERE user_id = $1+ "#,+ user.as_ref(),+ )+ .execute(&self.pool)+ .await?;+ Ok(())+ }}// The domain error stays sqlx-free; this adapter owns the mapping so `?`diff --git a/crates/onboarding/src/outbound/pg_onboarding_repo/test.rs b/crates/onboarding/src/outbound/pg_onboarding_repo/test.rsnew file mode 100644index 0000000000..2c8b7cb36e--- /dev/null+++ b/crates/onboarding/src/outbound/pg_onboarding_repo/test.rs@@ -0,0 +1,39 @@+use super::*;+use crate::domain::models::OnboardingStatus;+use macro_db_migrator::MACRO_DB_MIGRATIONS;++fn user(email: &str) -> MacroUserIdStr<'static> {+ MacroUserIdStr::try_from_email(email).expect("valid test user id")+}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn delete_row_removes_only_the_requested_user(pool: PgPool) {+ let repo = PgOnboardingRepo::new(pool);+ let deleted = user("[email protected]");+ let kept = user("[email protected]");+ repo.ensure_row(&deleted).await.unwrap();+ repo.complete(&kept, true).await.unwrap();++ repo.delete_row(&deleted).await.unwrap();++ assert!(repo.get_row(&deleted).await.unwrap().is_none());+ let kept_row = repo.get_row(&kept).await.unwrap().expect("kept row");+ assert_eq!(kept_row.status, OnboardingStatus::Completed);+ assert!(kept_row.skipped);+}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn delete_row_is_idempotent(pool: PgPool) {+ // Account deletion is retried by the identity provider; a user who never+ // entered the flow, or whose row went away on an earlier attempt, must+ // not fail the retry.+ let repo = PgOnboardingRepo::new(pool);+ let never_onboarded = user("[email protected]");++ repo.delete_row(&never_onboarded).await.unwrap();+ repo.ensure_row(&never_onboarded).await.unwrap();+ repo.delete_row(&never_onboarded).await.unwrap();+ repo.delete_row(&never_onboarded).await.unwrap();++ assert!(repo.get_row(&never_onboarded).await.unwrap().is_none());+}diff --git a/services/authentication_service/Cargo.toml b/services/authentication_service/Cargo.tomlindex de7d6a180c..d4a46e498b 100644--- a/services/authentication_service/Cargo.toml+++ b/services/authentication_service/Cargo.toml@@ -136,6 +136,7 @@ model_notifications = { path = "../../crates/model_notifications" }native_app_service = { path = "../../crates/native_app_service" }non_empty = { path = "../../crates/non_empty" }notification = { path = "../../crates/notification" }+onboarding = { path = "../../crates/onboarding" }rand = { workspace = true }rate_limit = { path = "../../crates/rate_limit" }redis = { workspace = true }diff --git a/services/authentication_service/src/api/context.rs b/services/authentication_service/src/api/context.rsindex 3fa38ee5ab..eb092f47bd 100644--- a/services/authentication_service/src/api/context.rs+++ b/services/authentication_service/src/api/context.rs@@ -156,8 +156,12 @@ pub(crate) struct ApiContext {>,pub document_storage_service_client:Arc<document_storage_service_client::DocumentStorageServiceClient>,- pub user_deletion:- Arc<authentication_service::outbound::user_deletion::UserDeletionAdapter<TeamsServiceType>>,+ pub user_deletion: Arc<+ authentication_service::outbound::user_deletion::UserDeletionAdapter<+ TeamsServiceType,+ onboarding::outbound::pg_onboarding_repo::PgOnboardingRepo,+ >,+ >,pub email_service_client: Arc<email::outbound::EmailServiceHttpClient>,pub ses_client: Arc<ses_client::Ses>,pub notification_ingress_service: Arc<NotificationIngressType>,diff --git a/services/authentication_service/src/main.rs b/services/authentication_service/src/main.rsindex a3ac2a23a7..6e140fdcf6 100644--- a/services/authentication_service/src/main.rs+++ b/services/authentication_service/src/main.rs@@ -555,6 +555,7 @@ async fn main() -> anyhow::Result<()> {db.clone(),document_storage_service_client.clone(),teams_service.clone(),+ onboarding::outbound::pg_onboarding_repo::PgOnboardingRepo::new(db.clone()),stripe_client.clone(),config.service_internal_auth_key.to_string(),macro_service_urls::AgentHarnessServiceUrl::new()?.to_string(),diff --git a/services/authentication_service/src/outbound/user_deletion.rs b/services/authentication_service/src/outbound/user_deletion.rsindex 1a3e968649..fccef4de22 100644--- a/services/authentication_service/src/outbound/user_deletion.rs+++ b/services/authentication_service/src/outbound/user_deletion.rs@@ -5,6 +5,7 @@ use std::{sync::Arc, time::Duration};use document_storage_service_client::DocumentStorageServiceClient;use macro_authorization::INTERNAL_API_KEY_HEADER;use macro_user_id::user_id::MacroUserIdStr;+use onboarding::domain::ports::OnboardingRepo;use rootcause::{Report,prelude::{IntoRootcause, ResultExt},@@ -15,12 +16,13 @@ use uuid::Uuid;use crate::service::user::delete_user::UserDeletionGateway;-/// HTTP, in-process team service, Stripe, and database adapter composed at-/// authentication-service startup.-pub struct UserDeletionAdapter<T: TeamService> {+/// HTTP, in-process team service, onboarding repository, Stripe, and database+/// adapter composed at authentication-service startup.+pub struct UserDeletionAdapter<T: TeamService, O: OnboardingRepo> {db: PgPool,documents: Arc<DocumentStorageServiceClient>,teams: Arc<T>,+ onboarding: O,stripe: Arc<stripe::Client>,client: reqwest::Client,internal_key: String,@@ -28,17 +30,22 @@ pub struct UserDeletionAdapter<T: TeamService> {scheduled_action_url: String,}-impl<T: TeamService> UserDeletionAdapter<T> {+impl<T: TeamService, O: OnboardingRepo> UserDeletionAdapter<T, O> {/// Construct a bounded HTTP client. Redirects must not turn a failed/// internal DELETE into an unrelated successful response.////// `internal_key` is the fleet-wide internal service key that the agent/// harness and scheduled-action services validate. This service's own/// inbound key is a different secret and is rejected by them.+ #[expect(+ clippy::too_many_arguments,+ reason = "one argument per owning service; the composition root wires them once"+ )]pub fn new(db: PgPool,documents: Arc<DocumentStorageServiceClient>,teams: Arc<T>,+ onboarding: O,stripe: Arc<stripe::Client>,internal_key: String,harness_url: String,@@ -48,6 +55,7 @@ impl<T: TeamService> UserDeletionAdapter<T> {db,documents,teams,+ onboarding,stripe,client: reqwest::Client::builder().timeout(Duration::from_secs(300))@@ -85,7 +93,7 @@ impl<T: TeamService> UserDeletionAdapter<T> {}}-impl<T: TeamService> UserDeletionGateway for UserDeletionAdapter<T> {+impl<T: TeamService, O: OnboardingRepo> UserDeletionGateway for UserDeletionAdapter<T, O> {async fn delete_scheduled_actions(&self, user: &MacroUserIdStr<'static>) -> Result<(), Report> {self.delete_owned(&self.scheduled_action_url, "scheduled-actions", user).await@@ -114,6 +122,15 @@ impl<T: TeamService> UserDeletionGateway for UserDeletionAdapter<T> {Ok(())}+ async fn delete_onboarding(&self, user: &MacroUserIdStr<'static>) -> Result<(), Report> {+ self.onboarding+ .delete_row(user)+ .await+ .map_err(|error| Report::new(error).into_dynamic())+ .context("failed to delete user onboarding")?;+ Ok(())+ }+async fn delete_profile(&self,user: &MacroUserIdStr<'static>,diff --git a/services/authentication_service/src/service/user/delete_user.rs b/services/authentication_service/src/service/user/delete_user.rsindex 9dbd8da7b6..796c4463f1 100644--- a/services/authentication_service/src/service/user/delete_user.rs+++ b/services/authentication_service/src/service/user/delete_user.rs@@ -31,6 +31,12 @@ pub trait UserDeletionGateway: Send + Sync {&self,user: &MacroUserIdStr<'static>,) -> impl Future<Output = Result<(), Report>> + Send;+ /// Delete the profile's onboarding-flow row. It is keyed by the profile id+ /// with no foreign key to the profile, so it would otherwise outlive it.+ fn delete_onboarding(+ &self,+ user: &MacroUserIdStr<'static>,+ ) -> impl Future<Output = Result<(), Report>> + Send;/// Delete the profile only after all owning services have succeeded.fn delete_profile(&self,@@ -62,6 +68,7 @@ pub async fn delete_user_data(gateway.delete_agent_sessions(user).await?;gateway.leave_teams(user).await?;gateway.delete_items(user).await?;+ gateway.delete_onboarding(user).await?;gateway.delete_profile(user, account).await?;}gateway.delete_billing_customer(account).await?;diff --git a/services/authentication_service/src/service/user/delete_user/test.rs b/services/authentication_service/src/service/user/delete_user/test.rsindex ed00be3733..014235de3b 100644--- a/services/authentication_service/src/service/user/delete_user/test.rs+++ b/services/authentication_service/src/service/user/delete_user/test.rs@@ -31,6 +31,9 @@ impl UserDeletionGateway for FakeGateway {async fn delete_items(&self, user: &MacroUserIdStr<'static>) -> Result<(), Report> {self.call("items", user.as_ref())}+ async fn delete_onboarding(&self, user: &MacroUserIdStr<'static>) -> Result<(), Report> {+ self.call("onboarding", user.as_ref())+ }async fn delete_profile(&self, user: &MacroUserIdStr<'static>, _: &Uuid) -> Result<(), Report> {self.call("profile", user.as_ref())}@@ -52,7 +55,14 @@ fn users() -> Vec<MacroUserIdStr<'static>> {fn expected_calls() -> Vec<String> {let mut expected = Vec::new();for user in users() {- for step in ["actions", "sessions", "teams", "items", "profile"] {+ for step in [+ "actions",+ "sessions",+ "teams",+ "items",+ "onboarding",+ "profile",+ ] {expected.push(format!("{step}:{user}"));}}