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.

feat(slack): import archives (#7311)

Will Hutchinson1d967fbca356
diff --git a/.github/services-config.json b/.github/services-config.json
index d6b48ac0a8..2e82fdc2c7 100644
--- a/.github/services-config.json
+++ b/.github/services-config.json
@@ -122,6 +122,13 @@
"document-storage-service": {
"source_paths": [
"services/document_storage_service/**",
+ "services/slack_import_worker/**",
+ "crates/slack_integration/**",
+ "crates/channels/**",
+ "crates/messages/**",
+ "crates/import/**",
+ "crates/macro_queues/**",
+ "crates/macro_service_urls/**",
"crates/call/**",
"crates/github/**",
"crates/initiative/**",
@@ -132,7 +139,8 @@
],
"stack_path": "infra/stacks/cloud-storage-service/**",
"deploy_binaries": [
- "document_storage_service"
+ "document_storage_service",
+ "slack_import_worker"
],
"deploy_lambdas": [
"delete_chat_handler",
@@ -193,7 +201,11 @@
},
"search-processing-service": {
"source_paths": [
- "services/search_processing_service/**"
+ "services/search_processing_service/**",
+ "crates/models_search/**",
+ "crates/models_opensearch/**",
+ "crates/opensearch_client/**",
+ "crates/comms_db_client/**"
],
"stack_path": "infra/stacks/search-processing-service/**",
"deploy_binaries": [
diff --git a/.github/workspace-dep-closures.json b/.github/workspace-dep-closures.json
index 34ed79a673..eb03c6d6ce 100644
--- a/.github/workspace-dep-closures.json
+++ b/.github/workspace-dep-closures.json
@@ -4190,6 +4190,7 @@
"crates/secretsmanager_client",
"crates/share_permission_db_utils",
"crates/skills",
+ "crates/slack_integration",
"crates/soup",
"crates/soup_filter_projection",
"crates/soup_realtime",
@@ -10425,6 +10426,209 @@
"crates/user_quota",
"crates/workspace-hack"
],
+ "slack_import_worker": [
+ "crates/activity",
+ "crates/agent",
+ "crates/agent_fold",
+ "crates/agent_runtime_protocol",
+ "crates/ai_billing",
+ "crates/ai_toolset",
+ "crates/ai_usage",
+ "crates/analytics_client",
+ "crates/attachment",
+ "crates/authentication_service_client",
+ "crates/bot_id",
+ "crates/bot_token",
+ "crates/broadcast",
+ "crates/calendar_events",
+ "crates/call",
+ "crates/channel_sender",
+ "crates/channels",
+ "crates/connection",
+ "crates/connection_gateway_client",
+ "crates/connection_gateway_models",
+ "crates/contacts",
+ "crates/cowlike",
+ "crates/crm",
+ "crates/database_env_vars",
+ "crates/document_sub_type",
+ "crates/email",
+ "crates/email_utils",
+ "crates/entity_access",
+ "crates/entity_access_db_utils",
+ "crates/entity_access_management",
+ "crates/entity_mutation",
+ "crates/filter_ast",
+ "crates/frecency",
+ "crates/genai_telemetry",
+ "crates/generic_email_domains",
+ "crates/google_token",
+ "crates/harness_id",
+ "crates/harness_token",
+ "crates/import",
+ "crates/initiative",
+ "crates/invite_email",
+ "crates/ip_extractor",
+ "crates/item_filters",
+ "crates/kafka_util",
+ "crates/last_online_tracker",
+ "crates/lexical_client",
+ "crates/macro_auth",
+ "crates/macro_authorization",
+ "crates/macro_aws_config",
+ "crates/macro_config",
+ "crates/macro_config_derive",
+ "crates/macro_entrypoint",
+ "crates/macro_env",
+ "crates/macro_env_var",
+ "crates/macro_event_broker",
+ "crates/macro_event_topics",
+ "crates/macro_queues",
+ "crates/macro_service_urls",
+ "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/model",
+ "crates/model-entity",
+ "crates/model-error-response",
+ "crates/model_file_type",
+ "crates/model_notifications",
+ "crates/model_owner",
+ "crates/model_user",
+ "crates/models_ai_projection",
+ "crates/models_bulk_upload",
+ "crates/models_email",
+ "crates/models_entity_access_management",
+ "crates/models_pagination",
+ "crates/models_permissions",
+ "crates/models_properties",
+ "crates/models_sfs",
+ "crates/non_empty",
+ "crates/notification",
+ "crates/notification_state",
+ "crates/pipedream_mcp",
+ "crates/prompt",
+ "crates/properties",
+ "crates/rate_limit",
+ "crates/remote_env_var",
+ "crates/roles_and_permissions",
+ "crates/s3_key",
+ "crates/share_permission_db_utils",
+ "crates/slack_integration",
+ "crates/sqs_client",
+ "crates/sqs_worker",
+ "crates/static_file_service_client",
+ "crates/system_properties",
+ "crates/teams",
+ "crates/unfurl",
+ "crates/workspace-hack",
+ "services/slack_import_worker"
+ ],
+ "slack_integration": [
+ "crates/activity",
+ "crates/agent",
+ "crates/agent_fold",
+ "crates/agent_runtime_protocol",
+ "crates/ai_billing",
+ "crates/ai_toolset",
+ "crates/ai_usage",
+ "crates/analytics_client",
+ "crates/attachment",
+ "crates/authentication_service_client",
+ "crates/bot_id",
+ "crates/bot_token",
+ "crates/broadcast",
+ "crates/calendar_events",
+ "crates/call",
+ "crates/channel_sender",
+ "crates/channels",
+ "crates/connection",
+ "crates/connection_gateway_client",
+ "crates/connection_gateway_models",
+ "crates/contacts",
+ "crates/cowlike",
+ "crates/crm",
+ "crates/database_env_vars",
+ "crates/document_sub_type",
+ "crates/email",
+ "crates/email_utils",
+ "crates/entity_access",
+ "crates/entity_access_db_utils",
+ "crates/entity_access_management",
+ "crates/entity_mutation",
+ "crates/filter_ast",
+ "crates/frecency",
+ "crates/genai_telemetry",
+ "crates/generic_email_domains",
+ "crates/google_token",
+ "crates/harness_id",
+ "crates/harness_token",
+ "crates/import",
+ "crates/initiative",
+ "crates/invite_email",
+ "crates/ip_extractor",
+ "crates/item_filters",
+ "crates/kafka_util",
+ "crates/last_online_tracker",
+ "crates/lexical_client",
+ "crates/macro_auth",
+ "crates/macro_authorization",
+ "crates/macro_aws_config",
+ "crates/macro_entrypoint",
+ "crates/macro_env",
+ "crates/macro_env_var",
+ "crates/macro_event_broker",
+ "crates/macro_event_topics",
+ "crates/macro_queues",
+ "crates/macro_service_urls",
+ "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/model",
+ "crates/model-entity",
+ "crates/model-error-response",
+ "crates/model_file_type",
+ "crates/model_notifications",
+ "crates/model_owner",
+ "crates/model_user",
+ "crates/models_ai_projection",
+ "crates/models_bulk_upload",
+ "crates/models_email",
+ "crates/models_entity_access_management",
+ "crates/models_pagination",
+ "crates/models_permissions",
+ "crates/models_properties",
+ "crates/models_sfs",
+ "crates/non_empty",
+ "crates/notification",
+ "crates/notification_state",
+ "crates/pipedream_mcp",
+ "crates/prompt",
+ "crates/properties",
+ "crates/rate_limit",
+ "crates/remote_env_var",
+ "crates/roles_and_permissions",
+ "crates/s3_key",
+ "crates/share_permission_db_utils",
+ "crates/slack_integration",
+ "crates/sqs_client",
+ "crates/sqs_worker",
+ "crates/static_file_service_client",
+ "crates/system_properties",
+ "crates/teams",
+ "crates/unfurl",
+ "crates/workspace-hack"
+ ],
"sns_client": [
"crates/sns_client",
"crates/workspace-hack"
diff --git a/.sqlx/query-004a56e3626c6650d0a334c9d15ed0ec710fcd635b47111629c6951d62f09c15.json b/.sqlx/query-004a56e3626c6650d0a334c9d15ed0ec710fcd635b47111629c6951d62f09c15.json
new file mode 100644
index 0000000000..44c1b637b5
--- /dev/null
+++ b/.sqlx/query-004a56e3626c6650d0a334c9d15ed0ec710fcd635b47111629c6951d62f09c15.json
@@ -0,0 +1,55 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT u.object_key, u.sha256, u.byte_length, u.record_count, u.verified_version_id, u.verified_etag\n FROM slack_import_upload u JOIN slack_import_job j ON j.id = u.job_id\n WHERE j.team_id = $1 AND j.id = $2\n AND u.slack_channel_id IS NOT DISTINCT FROM $3 AND u.part_index IS NOT DISTINCT FROM $4",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "object_key",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 1,
+ "name": "sha256",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 2,
+ "name": "byte_length",
+ "type_info": "Int8"
+ },
+ {
+ "ordinal": 3,
+ "name": "record_count",
+ "type_info": "Int4"
+ },
+ {
+ "ordinal": 4,
+ "name": "verified_version_id",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 5,
+ "name": "verified_etag",
+ "type_info": "Text"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Uuid",
+ "Text",
+ "Int4"
+ ]
+ },
+ "nullable": [
+ false,
+ false,
+ false,
+ true,
+ true,
+ true
+ ]
+ },
+ "hash": "004a56e3626c6650d0a334c9d15ed0ec710fcd635b47111629c6951d62f09c15"
+}
diff --git a/.sqlx/query-029f1971b01466f068bb7dcb3fcc849a709a98668618890a35defc8479bcceb3.json b/.sqlx/query-029f1971b01466f068bb7dcb3fcc849a709a98668618890a35defc8479bcceb3.json
new file mode 100644
index 0000000000..900de8afff
--- /dev/null
+++ b/.sqlx/query-029f1971b01466f068bb7dcb3fcc849a709a98668618890a35defc8479bcceb3.json
@@ -0,0 +1,36 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT status, attempts, lease_expires_at > clock_timestamp() AS active\n FROM slack_import_conversation WHERE job_id = $1 AND slack_channel_id = $2 AND event_generation = $3",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "status",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 1,
+ "name": "attempts",
+ "type_info": "Int4"
+ },
+ {
+ "ordinal": 2,
+ "name": "active",
+ "type_info": "Bool"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Text",
+ "Int8"
+ ]
+ },
+ "nullable": [
+ false,
+ false,
+ null
+ ]
+ },
+ "hash": "029f1971b01466f068bb7dcb3fcc849a709a98668618890a35defc8479bcceb3"
+}
diff --git a/.sqlx/query-05d52e30277ab985b7ba27789736abab59b27e56a33c01e4b90729fa6112e832.json b/.sqlx/query-05d52e30277ab985b7ba27789736abab59b27e56a33c01e4b90729fa6112e832.json
new file mode 100644
index 0000000000..a38f634366
--- /dev/null
+++ b/.sqlx/query-05d52e30277ab985b7ba27789736abab59b27e56a33c01e4b90729fa6112e832.json
@@ -0,0 +1,15 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "UPDATE comms_channel_participants SET left_at = now(), role = 'admin' WHERE channel_id = $1 AND user_id = $2",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Text"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "05d52e30277ab985b7ba27789736abab59b27e56a33c01e4b90729fa6112e832"
+}
diff --git a/.sqlx/query-06ce4bdbaea2e34a9704c4f5f721e7c7bdc821de74570592df4b7a4cf55be75a.json b/.sqlx/query-06ce4bdbaea2e34a9704c4f5f721e7c7bdc821de74570592df4b7a4cf55be75a.json
new file mode 100644
index 0000000000..7a50b3c417
--- /dev/null
+++ b/.sqlx/query-06ce4bdbaea2e34a9704c4f5f721e7c7bdc821de74570592df4b7a4cf55be75a.json
@@ -0,0 +1,36 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT slack_ts, message_id, channel_id FROM slack_import_message_map\n WHERE team_id = $1 AND slack_channel_id = $2 AND slack_ts = ANY($3)",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "slack_ts",
+ "type_info": "Int8"
+ },
+ {
+ "ordinal": 1,
+ "name": "message_id",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 2,
+ "name": "channel_id",
+ "type_info": "Uuid"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Text",
+ "Int8Array"
+ ]
+ },
+ "nullable": [
+ false,
+ false,
+ false
+ ]
+ },
+ "hash": "06ce4bdbaea2e34a9704c4f5f721e7c7bdc821de74570592df4b7a4cf55be75a"
+}
diff --git a/.sqlx/query-07c9405f9c16c26ee5e680743616b3d79d785f7319155c023dad2d5c9f24c34a.json b/.sqlx/query-07c9405f9c16c26ee5e680743616b3d79d785f7319155c023dad2d5c9f24c34a.json
new file mode 100644
index 0000000000..c3da76ac1a
--- /dev/null
+++ b/.sqlx/query-07c9405f9c16c26ee5e680743616b3d79d785f7319155c023dad2d5c9f24c34a.json
@@ -0,0 +1,22 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT team_id FROM slack_import_job WHERE id = $1 FOR UPDATE",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "team_id",
+ "type_info": "Uuid"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ false
+ ]
+ },
+ "hash": "07c9405f9c16c26ee5e680743616b3d79d785f7319155c023dad2d5c9f24c34a"
+}
diff --git a/.sqlx/query-0847577b2a01482adb9ee7766aeee698483be5c7f7acd8906f1fcb601ebfd848.json b/.sqlx/query-0847577b2a01482adb9ee7766aeee698483be5c7f7acd8906f1fcb601ebfd848.json
new file mode 100644
index 0000000000..7ecd7886c1
--- /dev/null
+++ b/.sqlx/query-0847577b2a01482adb9ee7766aeee698483be5c7f7acd8906f1fcb601ebfd848.json
@@ -0,0 +1,20 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT COUNT(*) FROM comms_activity",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "count",
+ "type_info": "Int8"
+ }
+ ],
+ "parameters": {
+ "Left": []
+ },
+ "nullable": [
+ null
+ ]
+ },
+ "hash": "0847577b2a01482adb9ee7766aeee698483be5c7f7acd8906f1fcb601ebfd848"
+}
diff --git a/.sqlx/query-089deab58d419b15a5c39a9d8af9b0a2afa0e8182e707176a3dbdda3309fd4c4.json b/.sqlx/query-089deab58d419b15a5c39a9d8af9b0a2afa0e8182e707176a3dbdda3309fd4c4.json
new file mode 100644
index 0000000000..8027ac0056
--- /dev/null
+++ b/.sqlx/query-089deab58d419b15a5c39a9d8af9b0a2afa0e8182e707176a3dbdda3309fd4c4.json
@@ -0,0 +1,14 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "UPDATE slack_import_conversation SET status = 'skipped',\n settled_at = clock_timestamp(), updated_at = clock_timestamp()\n WHERE job_id = $1 AND status IN ('awaiting_uploads', 'queued')",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "089deab58d419b15a5c39a9d8af9b0a2afa0e8182e707176a3dbdda3309fd4c4"
+}
diff --git a/.sqlx/query-0ab18665acc9da09f15d847bba3342df7b5d550efbaba767323e294e1f6ab2d2.json b/.sqlx/query-0ab18665acc9da09f15d847bba3342df7b5d550efbaba767323e294e1f6ab2d2.json
new file mode 100644
index 0000000000..f5b8216478
--- /dev/null
+++ b/.sqlx/query-0ab18665acc9da09f15d847bba3342df7b5d550efbaba767323e294e1f6ab2d2.json
@@ -0,0 +1,64 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT kind, name, folder, member_ids, creator_id, source_created_at, archived, message_count\n FROM slack_import_conversation WHERE job_id = $1 AND slack_channel_id = 'C1'",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "kind",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 1,
+ "name": "name",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 2,
+ "name": "folder",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 3,
+ "name": "member_ids",
+ "type_info": "TextArray"
+ },
+ {
+ "ordinal": 4,
+ "name": "creator_id",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 5,
+ "name": "source_created_at",
+ "type_info": "Int8"
+ },
+ {
+ "ordinal": 6,
+ "name": "archived",
+ "type_info": "Bool"
+ },
+ {
+ "ordinal": 7,
+ "name": "message_count",
+ "type_info": "Int8"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ false,
+ false,
+ false,
+ false,
+ true,
+ true,
+ false,
+ true
+ ]
+ },
+ "hash": "0ab18665acc9da09f15d847bba3342df7b5d550efbaba767323e294e1f6ab2d2"
+}
diff --git a/.sqlx/query-0ae8c27482edcfc91383f78847d4650d33d3a234aebc51cea8b36a88d5c5dcfe.json b/.sqlx/query-0ae8c27482edcfc91383f78847d4650d33d3a234aebc51cea8b36a88d5c5dcfe.json
new file mode 100644
index 0000000000..9c98c485ad
--- /dev/null
+++ b/.sqlx/query-0ae8c27482edcfc91383f78847d4650d33d3a234aebc51cea8b36a88d5c5dcfe.json
@@ -0,0 +1,39 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n INSERT INTO comms_channels\n (id, name, owner_id, channel_type, team_id, auto_join_team, created_at, updated_at)\n VALUES ($1, $2, $3, $4, $5, false, $6, $6)\n ON CONFLICT (id) DO NOTHING\n RETURNING id\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "id",
+ "type_info": "Uuid"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Varchar",
+ "Text",
+ {
+ "Custom": {
+ "name": "comms_channel_type",
+ "kind": {
+ "Enum": [
+ "public",
+ "private",
+ "direct_message",
+ "team"
+ ]
+ }
+ }
+ },
+ "Uuid",
+ "Timestamptz"
+ ]
+ },
+ "nullable": [
+ false
+ ]
+ },
+ "hash": "0ae8c27482edcfc91383f78847d4650d33d3a234aebc51cea8b36a88d5c5dcfe"
+}
diff --git a/.sqlx/query-0bbe4ec1f097ea1f596bad71d08b089e225aeb6403fe3ee5746023f2ab00e8e8.json b/.sqlx/query-0bbe4ec1f097ea1f596bad71d08b089e225aeb6403fe3ee5746023f2ab00e8e8.json
new file mode 100644
index 0000000000..099c195505
--- /dev/null
+++ b/.sqlx/query-0bbe4ec1f097ea1f596bad71d08b089e225aeb6403fe3ee5746023f2ab00e8e8.json
@@ -0,0 +1,23 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT thread_id FROM comms_messages WHERE channel_id = $1 AND id <> $2",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "thread_id",
+ "type_info": "Uuid"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ true
+ ]
+ },
+ "hash": "0bbe4ec1f097ea1f596bad71d08b089e225aeb6403fe3ee5746023f2ab00e8e8"
+}
diff --git a/.sqlx/query-0c21ff7e4a4fdf45b51fe4f429b35362b8dbcbf70968a7b752fdf09bb8e4a6fd.json b/.sqlx/query-0c21ff7e4a4fdf45b51fe4f429b35362b8dbcbf70968a7b752fdf09bb8e4a6fd.json
new file mode 100644
index 0000000000..0d03c9a895
--- /dev/null
+++ b/.sqlx/query-0c21ff7e4a4fdf45b51fe4f429b35362b8dbcbf70968a7b752fdf09bb8e4a6fd.json
@@ -0,0 +1,15 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "UPDATE slack_import_job SET revision = revision + 1, updated_at = clock_timestamp() WHERE team_id = $1 AND id = $2",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "0c21ff7e4a4fdf45b51fe4f429b35362b8dbcbf70968a7b752fdf09bb8e4a6fd"
+}
diff --git a/.sqlx/query-0d0b3ef3f9f49f24dd11513be24ad93b12fd6c16c6265aaf9afcb9089a01c674.json b/.sqlx/query-0d0b3ef3f9f49f24dd11513be24ad93b12fd6c16c6265aaf9afcb9089a01c674.json
new file mode 100644
index 0000000000..9f111e3fb5
--- /dev/null
+++ b/.sqlx/query-0d0b3ef3f9f49f24dd11513be24ad93b12fd6c16c6265aaf9afcb9089a01c674.json
@@ -0,0 +1,38 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n SELECT channel_id AS \"channel_id!\", id AS message_id, created_at\n FROM comms_messages\n WHERE channel_id IS NOT NULL\n AND ($2::timestamptz IS NULL OR (created_at, id) > ($2, $3::uuid))\n AND ($4::uuid[] IS NULL OR channel_id = ANY($4))\n AND (\n $5::bool IS NULL\n OR ($5 AND deleted_at IS NOT NULL)\n OR (NOT $5 AND deleted_at IS NULL)\n )\n ORDER BY created_at ASC, id ASC\n LIMIT $1\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "channel_id!",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 1,
+ "name": "message_id",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 2,
+ "name": "created_at",
+ "type_info": "Timestamptz"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Int8",
+ "Timestamptz",
+ "Uuid",
+ "UuidArray",
+ "Bool"
+ ]
+ },
+ "nullable": [
+ true,
+ false,
+ false
+ ]
+ },
+ "hash": "0d0b3ef3f9f49f24dd11513be24ad93b12fd6c16c6265aaf9afcb9089a01c674"
+}
diff --git a/.sqlx/query-0e0d7f7cc825498b13cbc11523bc405c1b3f7e13b29b90f34ce099ec50d782d7.json b/.sqlx/query-0e0d7f7cc825498b13cbc11523bc405c1b3f7e13b29b90f34ce099ec50d782d7.json
new file mode 100644
index 0000000000..5d482d362b
--- /dev/null
+++ b/.sqlx/query-0e0d7f7cc825498b13cbc11523bc405c1b3f7e13b29b90f34ce099ec50d782d7.json
@@ -0,0 +1,15 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "UPDATE slack_import_outbox SET cancelled_at = clock_timestamp()\n WHERE job_id = $1 AND slack_channel_id = $2 AND kind = 'search'\n AND published_at IS NULL AND cancelled_at IS NULL",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Text"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "0e0d7f7cc825498b13cbc11523bc405c1b3f7e13b29b90f34ce099ec50d782d7"
+}
diff --git a/.sqlx/query-0e1d0a9ad82ca80af62e6127b5e17fb5506136da70d6b576e0b6618057c0e5b0.json b/.sqlx/query-0e1d0a9ad82ca80af62e6127b5e17fb5506136da70d6b576e0b6618057c0e5b0.json
new file mode 100644
index 0000000000..bf8f2fe7f0
--- /dev/null
+++ b/.sqlx/query-0e1d0a9ad82ca80af62e6127b5e17fb5506136da70d6b576e0b6618057c0e5b0.json
@@ -0,0 +1,16 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "UPDATE slack_import_outbox o SET attempts = o.attempts + 1,\n available_at = clock_timestamp() + interval '60 seconds'\n FROM slack_import_conversation c\n WHERE o.job_id = $1 AND o.slack_channel_id = $2 AND o.kind = 'import' AND o.generation = $3\n AND o.published_at IS NULL AND o.cancelled_at IS NULL AND o.available_at <= clock_timestamp()\n AND c.job_id = o.job_id AND c.slack_channel_id = o.slack_channel_id\n AND c.event_generation = o.generation AND c.status IN ('queued', 'importing')",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Text",
+ "Int8"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "0e1d0a9ad82ca80af62e6127b5e17fb5506136da70d6b576e0b6618057c0e5b0"
+}
diff --git a/.sqlx/query-0f96c44538cedde6542afa59428aa308b0b816f29de728ef1aadf8c0a13ec791.json b/.sqlx/query-0f96c44538cedde6542afa59428aa308b0b816f29de728ef1aadf8c0a13ec791.json
new file mode 100644
index 0000000000..4ad255b8be
--- /dev/null
+++ b/.sqlx/query-0f96c44538cedde6542afa59428aa308b0b816f29de728ef1aadf8c0a13ec791.json
@@ -0,0 +1,22 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT auto_join_team FROM comms_channels WHERE id = $1",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "auto_join_team",
+ "type_info": "Bool"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ false
+ ]
+ },
+ "hash": "0f96c44538cedde6542afa59428aa308b0b816f29de728ef1aadf8c0a13ec791"
+}
diff --git a/.sqlx/query-102699bbba66483ee8462091dfb444ecf60ef7e55c674379b17d0d0b22b52b6e.json b/.sqlx/query-102699bbba66483ee8462091dfb444ecf60ef7e55c674379b17d0d0b22b52b6e.json
new file mode 100644
index 0000000000..0443c93750
--- /dev/null
+++ b/.sqlx/query-102699bbba66483ee8462091dfb444ecf60ef7e55c674379b17d0d0b22b52b6e.json
@@ -0,0 +1,15 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "UPDATE comms_channel_participants SET role = 'admin', left_at = $2 WHERE channel_id = $1 AND user_id = 'macro|[email protected]'",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Timestamptz"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "102699bbba66483ee8462091dfb444ecf60ef7e55c674379b17d0d0b22b52b6e"
+}
diff --git a/.sqlx/query-11cac3a33a2e7c4a45702a17f5e82d6f697b1c296ec07c37d6a39ad34ab7252e.json b/.sqlx/query-11cac3a33a2e7c4a45702a17f5e82d6f697b1c296ec07c37d6a39ad34ab7252e.json
new file mode 100644
index 0000000000..94809a2e84
--- /dev/null
+++ b/.sqlx/query-11cac3a33a2e7c4a45702a17f5e82d6f697b1c296ec07c37d6a39ad34ab7252e.json
@@ -0,0 +1,23 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n SELECT EXISTS (\n SELECT 1 FROM import_target_reservation\n WHERE channel_id = $1 AND team_id <> $2\n ) OR EXISTS (\n SELECT 1 FROM import_entity\n WHERE source = 'slack' AND status = 'imported' AND entity_type = 'channel'\n AND entity_id = $1::text AND team_id <> $2\n ) AS \"exists!\"\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "exists!",
+ "type_info": "Bool"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ null
+ ]
+ },
+ "hash": "11cac3a33a2e7c4a45702a17f5e82d6f697b1c296ec07c37d6a39ad34ab7252e"
+}
diff --git a/.sqlx/query-14d172fd70252bfe96ce970424b2cfbc14f3dbb80285c4748573952f69c81184.json b/.sqlx/query-14d172fd70252bfe96ce970424b2cfbc14f3dbb80285c4748573952f69c81184.json
new file mode 100644
index 0000000000..31a51b1c37
--- /dev/null
+++ b/.sqlx/query-14d172fd70252bfe96ce970424b2cfbc14f3dbb80285c4748573952f69c81184.json
@@ -0,0 +1,20 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT completed_at IS NULL AS \"pending!\" FROM slack_import_message_reference",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "pending!",
+ "type_info": "Bool"
+ }
+ ],
+ "parameters": {
+ "Left": []
+ },
+ "nullable": [
+ null
+ ]
+ },
+ "hash": "14d172fd70252bfe96ce970424b2cfbc14f3dbb80285c4748573952f69c81184"
+}
diff --git a/.sqlx/query-16f84c9c47d08651164ca26e073f25060d7bf74df0b82758196e906cf7ca1822.json b/.sqlx/query-16f84c9c47d08651164ca26e073f25060d7bf74df0b82758196e906cf7ca1822.json
new file mode 100644
index 0000000000..902e46c448
--- /dev/null
+++ b/.sqlx/query-16f84c9c47d08651164ca26e073f25060d7bf74df0b82758196e906cf7ca1822.json
@@ -0,0 +1,47 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT j.user_id, c.kind, c.channel_id, c.checkpoint_part, c.checkpoint_record\n FROM slack_import_job j JOIN slack_import_conversation c ON c.job_id = j.id\n WHERE j.id = $1 AND c.slack_channel_id = $2",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "user_id",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 1,
+ "name": "kind",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 2,
+ "name": "channel_id",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 3,
+ "name": "checkpoint_part",
+ "type_info": "Int4"
+ },
+ {
+ "ordinal": 4,
+ "name": "checkpoint_record",
+ "type_info": "Int4"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Text"
+ ]
+ },
+ "nullable": [
+ false,
+ false,
+ true,
+ false,
+ false
+ ]
+ },
+ "hash": "16f84c9c47d08651164ca26e073f25060d7bf74df0b82758196e906cf7ca1822"
+}
diff --git a/.sqlx/query-17c9482742dc820a4aee1e1dea6ae53ff717f781135b9baa9d687e831668b124.json b/.sqlx/query-17c9482742dc820a4aee1e1dea6ae53ff717f781135b9baa9d687e831668b124.json
new file mode 100644
index 0000000000..2be3a17380
--- /dev/null
+++ b/.sqlx/query-17c9482742dc820a4aee1e1dea6ae53ff717f781135b9baa9d687e831668b124.json
@@ -0,0 +1,24 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT t.root_id FROM comms_message_threads t\n JOIN comms_messages m ON m.id = t.root_id\n WHERE t.root_id = ANY($1) AND t.parent_entity_type = 'channel'\n AND t.parent_entity_id = $2 AND m.channel_id = $3\n AND m.thread_id IS NULL AND m.deleted_at IS NULL AND t.deleted_at IS NULL\n ORDER BY t.root_id FOR UPDATE OF t, m",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "root_id",
+ "type_info": "Uuid"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "UuidArray",
+ "Text",
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ false
+ ]
+ },
+ "hash": "17c9482742dc820a4aee1e1dea6ae53ff717f781135b9baa9d687e831668b124"
+}
diff --git a/.sqlx/query-17d37146ef07af9aced554388cabcd17811935d85d1d27aa6af1fbb2aa319afb.json b/.sqlx/query-17d37146ef07af9aced554388cabcd17811935d85d1d27aa6af1fbb2aa319afb.json
new file mode 100644
index 0000000000..ad9af882dc
--- /dev/null
+++ b/.sqlx/query-17d37146ef07af9aced554388cabcd17811935d85d1d27aa6af1fbb2aa319afb.json
@@ -0,0 +1,24 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n INSERT INTO comms_channels\n (id, owner_id, channel_type, auto_join_team, created_at, updated_at)\n VALUES ($1, $2, 'direct_message', false, COALESCE($3, NOW()), COALESCE($3, NOW()))\n ON CONFLICT (id) DO NOTHING\n RETURNING created_at\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "created_at",
+ "type_info": "Timestamptz"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Text",
+ "Timestamptz"
+ ]
+ },
+ "nullable": [
+ false
+ ]
+ },
+ "hash": "17d37146ef07af9aced554388cabcd17811935d85d1d27aa6af1fbb2aa319afb"
+}
diff --git a/.sqlx/query-18bf1cc68f468b81306d322aa12b9a7e748da35c8fbebd5ea9a0d6aa0052f0fd.json b/.sqlx/query-18bf1cc68f468b81306d322aa12b9a7e748da35c8fbebd5ea9a0d6aa0052f0fd.json
new file mode 100644
index 0000000000..784518a50e
--- /dev/null
+++ b/.sqlx/query-18bf1cc68f468b81306d322aa12b9a7e748da35c8fbebd5ea9a0d6aa0052f0fd.json
@@ -0,0 +1,50 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n SELECT\n (SELECT count(*) FROM notification) AS \"notifications!\",\n (SELECT count(*) FROM user_notification) AS \"user_notifications!\",\n (SELECT count(*) FROM notification_email_sent) AS \"emails!\",\n (SELECT count(*) FROM channel_notification_email_sent) AS \"channel_emails!\",\n (SELECT count(*) FROM comms_activity) AS \"channel_activity!\",\n (SELECT count(*) FROM activity_events) AS \"activity_events!\"\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "notifications!",
+ "type_info": "Int8"
+ },
+ {
+ "ordinal": 1,
+ "name": "user_notifications!",
+ "type_info": "Int8"
+ },
+ {
+ "ordinal": 2,
+ "name": "emails!",
+ "type_info": "Int8"
+ },
+ {
+ "ordinal": 3,
+ "name": "channel_emails!",
+ "type_info": "Int8"
+ },
+ {
+ "ordinal": 4,
+ "name": "channel_activity!",
+ "type_info": "Int8"
+ },
+ {
+ "ordinal": 5,
+ "name": "activity_events!",
+ "type_info": "Int8"
+ }
+ ],
+ "parameters": {
+ "Left": []
+ },
+ "nullable": [
+ null,
+ null,
+ null,
+ null,
+ null,
+ null
+ ]
+ },
+ "hash": "18bf1cc68f468b81306d322aa12b9a7e748da35c8fbebd5ea9a0d6aa0052f0fd"
+}
diff --git a/.sqlx/query-19ce2f8feae6f5365c4dc7b7ca8f6b1297a5cafe8e58cf4069b7e93dfba27a41.json b/.sqlx/query-19ce2f8feae6f5365c4dc7b7ca8f6b1297a5cafe8e58cf4069b7e93dfba27a41.json
new file mode 100644
index 0000000000..7dcd38d4dc
--- /dev/null
+++ b/.sqlx/query-19ce2f8feae6f5365c4dc7b7ca8f6b1297a5cafe8e58cf4069b7e93dfba27a41.json
@@ -0,0 +1,19 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "INSERT INTO slack_import_message_reference\n (job_id, slack_channel_id, message_id, channel_id, importer_version, template)\n VALUES ($1, $2, $3, $4, $5, $6)\n ON CONFLICT (job_id, message_id) DO NOTHING",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Text",
+ "Uuid",
+ "Uuid",
+ "Int2",
+ "Jsonb"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "19ce2f8feae6f5365c4dc7b7ca8f6b1297a5cafe8e58cf4069b7e93dfba27a41"
+}
diff --git a/.sqlx/query-1a06c4d4e5a33a745e965c292a5daf3c2b258c4233f1803b12e8bdd3fd810024.json b/.sqlx/query-1a06c4d4e5a33a745e965c292a5daf3c2b258c4233f1803b12e8bdd3fd810024.json
new file mode 100644
index 0000000000..a75753c142
--- /dev/null
+++ b/.sqlx/query-1a06c4d4e5a33a745e965c292a5daf3c2b258c4233f1803b12e8bdd3fd810024.json
@@ -0,0 +1,23 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT channel_id FROM slack_import_conversation WHERE job_id = $1 AND slack_channel_id = $2",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "channel_id",
+ "type_info": "Uuid"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Text"
+ ]
+ },
+ "nullable": [
+ true
+ ]
+ },
+ "hash": "1a06c4d4e5a33a745e965c292a5daf3c2b258c4233f1803b12e8bdd3fd810024"
+}
diff --git a/.sqlx/query-1c9d5061decc3b127dc9111749cc87b55e811cd75cc094a3bbf2fd3cb5639c47.json b/.sqlx/query-1c9d5061decc3b127dc9111749cc87b55e811cd75cc094a3bbf2fd3cb5639c47.json
new file mode 100644
index 0000000000..37fa896f3c
--- /dev/null
+++ b/.sqlx/query-1c9d5061decc3b127dc9111749cc87b55e811cd75cc094a3bbf2fd3cb5639c47.json
@@ -0,0 +1,16 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "INSERT INTO slack_import_outbox (job_id, slack_channel_id, kind, generation)\n VALUES ($1, $2, 'search', $3) ON CONFLICT DO NOTHING",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Text",
+ "Int8"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "1c9d5061decc3b127dc9111749cc87b55e811cd75cc094a3bbf2fd3cb5639c47"
+}
diff --git a/.sqlx/query-1cc11e930b7393652ee7194be7ecf330ff6088d59827d5eeba85ac8143021802.json b/.sqlx/query-1cc11e930b7393652ee7194be7ecf330ff6088d59827d5eeba85ac8143021802.json
new file mode 100644
index 0000000000..fd0ada2a46
--- /dev/null
+++ b/.sqlx/query-1cc11e930b7393652ee7194be7ecf330ff6088d59827d5eeba85ac8143021802.json
@@ -0,0 +1,16 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "INSERT INTO slack_import_outbox (job_id, slack_channel_id, kind, generation)\n VALUES ($1, $2, 'search', $3) ON CONFLICT (job_id, slack_channel_id, kind, generation) DO NOTHING",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Text",
+ "Int8"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "1cc11e930b7393652ee7194be7ecf330ff6088d59827d5eeba85ac8143021802"
+}
diff --git a/.sqlx/query-1cdb98ff007c44a911e786904d2657589b685a342dd6ce620594b6c32944f187.json b/.sqlx/query-1cdb98ff007c44a911e786904d2657589b685a342dd6ce620594b6c32944f187.json
new file mode 100644
index 0000000000..141e1ca5aa
--- /dev/null
+++ b/.sqlx/query-1cdb98ff007c44a911e786904d2657589b685a342dd6ce620594b6c32944f187.json
@@ -0,0 +1,16 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n UPDATE import_target_reservation\n SET state = 'ready', channel_id = $3, updated_at = now()\n WHERE team_id = $1 AND source = 'slack' AND foreign_id = $2 AND state = 'pending'\n ",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Text",
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "1cdb98ff007c44a911e786904d2657589b685a342dd6ce620594b6c32944f187"
+}
diff --git a/.sqlx/query-1d1e843f022fad0f5b60aec9b32f37238acd0a09ed0d6ca2353663e3034439a1.json b/.sqlx/query-1d1e843f022fad0f5b60aec9b32f37238acd0a09ed0d6ca2353663e3034439a1.json
new file mode 100644
index 0000000000..88cb2b86db
--- /dev/null
+++ b/.sqlx/query-1d1e843f022fad0f5b60aec9b32f37238acd0a09ed0d6ca2353663e3034439a1.json
@@ -0,0 +1,14 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "UPDATE slack_import_outbox SET cancelled_at = clock_timestamp()\n WHERE job_id = $1 AND kind = 'import' AND published_at IS NULL AND cancelled_at IS NULL",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "1d1e843f022fad0f5b60aec9b32f37238acd0a09ed0d6ca2353663e3034439a1"
+}
diff --git a/.sqlx/query-20d19851add36857c81a73b3767b645806cfaf1c5e6db1be2d39a9ee1c75bbea.json b/.sqlx/query-20d19851add36857c81a73b3767b645806cfaf1c5e6db1be2d39a9ee1c75bbea.json
new file mode 100644
index 0000000000..10883b7e59
--- /dev/null
+++ b/.sqlx/query-20d19851add36857c81a73b3767b645806cfaf1c5e6db1be2d39a9ee1c75bbea.json
@@ -0,0 +1,14 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "INSERT INTO \"User\" (id, email, macro_user_id) VALUES ('macro|[email protected]', '[email protected]', $1)",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "20d19851add36857c81a73b3767b645806cfaf1c5e6db1be2d39a9ee1c75bbea"
+}
diff --git a/.sqlx/query-227394b31cc92f54d863a268c57687d547dd43ee79232d56cccc9237155f356a.json b/.sqlx/query-227394b31cc92f54d863a268c57687d547dd43ee79232d56cccc9237155f356a.json
new file mode 100644
index 0000000000..4ba454ceaf
--- /dev/null
+++ b/.sqlx/query-227394b31cc92f54d863a268c57687d547dd43ee79232d56cccc9237155f356a.json
@@ -0,0 +1,22 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT count(*) AS \"count!\" FROM slack_import_outbox WHERE job_id = $1",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "count!",
+ "type_info": "Int8"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ null
+ ]
+ },
+ "hash": "227394b31cc92f54d863a268c57687d547dd43ee79232d56cccc9237155f356a"
+}
diff --git a/.sqlx/query-2416870bd69a64d042ec84ece4336270c81c2dd92e71a5d247992fb1e30c5f66.json b/.sqlx/query-2416870bd69a64d042ec84ece4336270c81c2dd92e71a5d247992fb1e30c5f66.json
new file mode 100644
index 0000000000..0e511612d0
--- /dev/null
+++ b/.sqlx/query-2416870bd69a64d042ec84ece4336270c81c2dd92e71a5d247992fb1e30c5f66.json
@@ -0,0 +1,14 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "UPDATE team_user SET team_role = 'member' WHERE team_id = $1",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "2416870bd69a64d042ec84ece4336270c81c2dd92e71a5d247992fb1e30c5f66"
+}
diff --git a/.sqlx/query-24869f147463bf664432de6852f5f44aa9853414132fc73a4611b191bf892bab.json b/.sqlx/query-24869f147463bf664432de6852f5f44aa9853414132fc73a4611b191bf892bab.json
new file mode 100644
index 0000000000..5701e969cd
--- /dev/null
+++ b/.sqlx/query-24869f147463bf664432de6852f5f44aa9853414132fc73a4611b191bf892bab.json
@@ -0,0 +1,34 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT m.id AS message_id, m.channel_id AS \"channel_id!\", t.root_id\n FROM comms_messages m\n JOIN comms_message_threads t ON t.root_id = coalesce(m.thread_id, m.id)\n JOIN comms_messages root ON root.id = t.root_id\n WHERE m.id = ANY($1) AND m.deleted_at IS NULL AND t.deleted_at IS NULL\n AND root.deleted_at IS NULL AND root.thread_id IS NULL\n AND m.parent_entity_type = 'channel' AND m.parent_entity_id = m.channel_id::text\n AND t.parent_entity_type = 'channel' AND t.parent_entity_id = m.channel_id::text\n AND root.channel_id = m.channel_id AND root.parent_entity_type = 'channel'\n AND root.parent_entity_id = m.channel_id::text",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "message_id",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 1,
+ "name": "channel_id!",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 2,
+ "name": "root_id",
+ "type_info": "Uuid"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "UuidArray"
+ ]
+ },
+ "nullable": [
+ false,
+ true,
+ false
+ ]
+ },
+ "hash": "24869f147463bf664432de6852f5f44aa9853414132fc73a4611b191bf892bab"
+}
diff --git a/.sqlx/query-25dff15deca6673982c8bd6428baab25419ff50cd37f77936294c7822afe7e01.json b/.sqlx/query-25dff15deca6673982c8bd6428baab25419ff50cd37f77936294c7822afe7e01.json
new file mode 100644
index 0000000000..fc80a506f9
--- /dev/null
+++ b/.sqlx/query-25dff15deca6673982c8bd6428baab25419ff50cd37f77936294c7822afe7e01.json
@@ -0,0 +1,27 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT EXISTS (SELECT 1 FROM slack_import_conversation WHERE job_id = $1 AND slack_channel_id = $2\n AND status = 'importing' AND event_generation = $3 AND lease_owner = $4 AND lease_token = $5\n AND lease_generation = $6 AND lease_expires_at > clock_timestamp()) AS \"valid!\"",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "valid!",
+ "type_info": "Bool"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Text",
+ "Int8",
+ "Uuid",
+ "Uuid",
+ "Int8"
+ ]
+ },
+ "nullable": [
+ null
+ ]
+ },
+ "hash": "25dff15deca6673982c8bd6428baab25419ff50cd37f77936294c7822afe7e01"
+}
diff --git a/.sqlx/query-266f4903b439c5748e2da6049871337bdc4d7a7ebba4859783d5624251eddaa3.json b/.sqlx/query-266f4903b439c5748e2da6049871337bdc4d7a7ebba4859783d5624251eddaa3.json
new file mode 100644
index 0000000000..3173fbab2a
--- /dev/null
+++ b/.sqlx/query-266f4903b439c5748e2da6049871337bdc4d7a7ebba4859783d5624251eddaa3.json
@@ -0,0 +1,101 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n SELECT\n c.id as \"channel_id\",\n c.name as \"name\",\n c.channel_type AS \"channel_type: ChannelType\",\n c.org_id as \"org_id\",\n m.id as \"message_id\",\n m.thread_id as \"thread_id\",\n m.sender_id as \"sender_id\",\n m.imported_author,\n m.content as \"content\",\n m.created_at as \"created_at\",\n m.updated_at as \"updated_at\",\n m.deleted_at::timestamptz as \"deleted_at\"\n FROM\n comms_messages m\n JOIN\n comms_channels c on m.parent_entity_type = 'channel' AND m.parent_entity_id = c.id::text\n WHERE\n m.id = $1\n AND c.id = $2\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "channel_id",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 1,
+ "name": "name",
+ "type_info": "Varchar"
+ },
+ {
+ "ordinal": 2,
+ "name": "channel_type: ChannelType",
+ "type_info": {
+ "Custom": {
+ "name": "comms_channel_type",
+ "kind": {
+ "Enum": [
+ "public",
+ "private",
+ "direct_message",
+ "team"
+ ]
+ }
+ }
+ }
+ },
+ {
+ "ordinal": 3,
+ "name": "org_id",
+ "type_info": "Int8"
+ },
+ {
+ "ordinal": 4,
+ "name": "message_id",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 5,
+ "name": "thread_id",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 6,
+ "name": "sender_id",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 7,
+ "name": "imported_author",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 8,
+ "name": "content",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 9,
+ "name": "created_at",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 10,
+ "name": "updated_at",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 11,
+ "name": "deleted_at",
+ "type_info": "Timestamptz"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ false,
+ true,
+ false,
+ true,
+ false,
+ true,
+ false,
+ true,
+ false,
+ false,
+ false,
+ null
+ ]
+ },
+ "hash": "266f4903b439c5748e2da6049871337bdc4d7a7ebba4859783d5624251eddaa3"
+}
diff --git a/.sqlx/query-28ec32ca127f48edddced384d3dedf34058409e2ff4932baee7601b157bce83e.json b/.sqlx/query-28ec32ca127f48edddced384d3dedf34058409e2ff4932baee7601b157bce83e.json
new file mode 100644
index 0000000000..71745ad37f
--- /dev/null
+++ b/.sqlx/query-28ec32ca127f48edddced384d3dedf34058409e2ff4932baee7601b157bce83e.json
@@ -0,0 +1,23 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "UPDATE slack_import_conversation SET search_dirty_generation = search_dirty_generation + 1,\n search_state = 'pending', search_receipt_id = NULL, search_submitted_generation = NULL,\n search_updated_at = clock_timestamp(), updated_at = clock_timestamp()\n WHERE job_id = $1 AND slack_channel_id = $2 RETURNING search_dirty_generation",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "search_dirty_generation",
+ "type_info": "Int8"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Text"
+ ]
+ },
+ "nullable": [
+ false
+ ]
+ },
+ "hash": "28ec32ca127f48edddced384d3dedf34058409e2ff4932baee7601b157bce83e"
+}
diff --git a/.sqlx/query-29f9f77b6c17f93ab79ac80d982df0f59a37d194403af98a3e5a303ed0846b32.json b/.sqlx/query-29f9f77b6c17f93ab79ac80d982df0f59a37d194403af98a3e5a303ed0846b32.json
new file mode 100644
index 0000000000..8f10daf7b0
--- /dev/null
+++ b/.sqlx/query-29f9f77b6c17f93ab79ac80d982df0f59a37d194403af98a3e5a303ed0846b32.json
@@ -0,0 +1,16 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "UPDATE slack_import_outbox SET published_at = clock_timestamp()\n WHERE job_id = $1 AND slack_channel_id = $2 AND kind = 'import' AND generation = $3\n AND published_at IS NULL AND cancelled_at IS NULL",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Text",
+ "Int8"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "29f9f77b6c17f93ab79ac80d982df0f59a37d194403af98a3e5a303ed0846b32"
+}
diff --git a/.sqlx/query-2b33ab9211a3c6e7e0fd8cca628898876daad1df82e296fde9050da49845257c.json b/.sqlx/query-2b33ab9211a3c6e7e0fd8cca628898876daad1df82e296fde9050da49845257c.json
new file mode 100644
index 0000000000..5769bf7303
--- /dev/null
+++ b/.sqlx/query-2b33ab9211a3c6e7e0fd8cca628898876daad1df82e296fde9050da49845257c.json
@@ -0,0 +1,49 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT s.ordinality AS \"position!\", r.state AS \"state?\", c.id AS \"id?\", c.name AS \"name?\",\n c.channel_type::text AS \"kind?\"\n FROM unnest($1::text[]) WITH ORDINALITY AS s(id, ordinality)\n JOIN import_source_binding b ON b.team_id = $2\n AND b.slack_workspace_id IS NOT DISTINCT FROM $3\n AND b.confirmed_unknown_at IS NOT DISTINCT FROM $4\n LEFT JOIN import_target_reservation r\n ON r.team_id = $2 AND r.source = 'slack' AND r.foreign_id = s.id\n LEFT JOIN LATERAL (\n SELECT count(*) AS count, min(entity_id) AS id,\n bool_and(entity_type = 'channel' AND entity_id IS NOT NULL) AS valid\n FROM (SELECT DISTINCT entity_id, entity_type FROM import_entity\n WHERE team_id = $2 AND source = 'slack' AND foreign_id = s.id\n AND status = 'imported' LIMIT 2) mappings\n ) legacy ON true\n LEFT JOIN comms_channels c ON c.id =\n CASE WHEN r.state = 'ready' AND r.channel_id = r.candidate_channel_id\n AND (legacy.count = 0 OR (legacy.count = 1 AND legacy.valid\n AND legacy.id = r.channel_id::text)) THEN r.channel_id\n WHEN r.state IS NULL AND legacy.count = 1 AND legacy.valid\n AND legacy.id ~ '^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$'\n THEN legacy.id::uuid END\n AND ((c.channel_type = 'team' AND c.team_id = $2)\n OR (r.state = 'ready' AND c.channel_type IN ('private', 'direct_message')\n AND c.team_id IS NULL))\n AND NOT EXISTS (SELECT 1 FROM import_target_reservation other\n WHERE other.channel_id = c.id AND other.team_id <> $2)\n AND NOT EXISTS (SELECT 1 FROM import_entity other\n WHERE other.source = 'slack' AND other.status = 'imported'\n AND other.entity_type = 'channel' AND other.entity_id = c.id::text\n AND other.team_id <> $2)\n ORDER BY s.ordinality",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "position!",
+ "type_info": "Int8"
+ },
+ {
+ "ordinal": 1,
+ "name": "state?",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 2,
+ "name": "id?",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 3,
+ "name": "name?",
+ "type_info": "Varchar"
+ },
+ {
+ "ordinal": 4,
+ "name": "kind?",
+ "type_info": "Text"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "TextArray",
+ "Uuid",
+ "Text",
+ "Timestamptz"
+ ]
+ },
+ "nullable": [
+ null,
+ false,
+ false,
+ true,
+ null
+ ]
+ },
+ "hash": "2b33ab9211a3c6e7e0fd8cca628898876daad1df82e296fde9050da49845257c"
+}
diff --git a/.sqlx/query-2c666e5086fac5cadf9d1eb34e5d4321cafb42e6f7d4c316edf9cb1b8b0a2b35.json b/.sqlx/query-2c666e5086fac5cadf9d1eb34e5d4321cafb42e6f7d4c316edf9cb1b8b0a2b35.json
new file mode 100644
index 0000000000..fddd8d00ed
--- /dev/null
+++ b/.sqlx/query-2c666e5086fac5cadf9d1eb34e5d4321cafb42e6f7d4c316edf9cb1b8b0a2b35.json
@@ -0,0 +1,23 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT u.job_id FROM slack_import_upload u\n JOIN slack_import_job j ON j.id = u.job_id\n WHERE j.team_id = $1 AND j.id = ANY($2)\n AND u.slack_channel_id IS NULL AND u.verified_at IS NOT NULL",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "job_id",
+ "type_info": "Uuid"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "UuidArray"
+ ]
+ },
+ "nullable": [
+ false
+ ]
+ },
+ "hash": "2c666e5086fac5cadf9d1eb34e5d4321cafb42e6f7d4c316edf9cb1b8b0a2b35"
+}
diff --git a/.sqlx/query-2d29668b4717cb9a6a6d4a0afa942112008f6cff5562eca725c7b1ffe7985b8d.json b/.sqlx/query-2d29668b4717cb9a6a6d4a0afa942112008f6cff5562eca725c7b1ffe7985b8d.json
new file mode 100644
index 0000000000..1457748364
--- /dev/null
+++ b/.sqlx/query-2d29668b4717cb9a6a6d4a0afa942112008f6cff5562eca725c7b1ffe7985b8d.json
@@ -0,0 +1,14 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "DELETE FROM slack_import_job WHERE id = $1",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "2d29668b4717cb9a6a6d4a0afa942112008f6cff5562eca725c7b1ffe7985b8d"
+}
diff --git a/.sqlx/query-35e571d9c4e3bc5fd717c8866d8b27c2f4baab1338b5e206646209eb3d2f3d1c.json b/.sqlx/query-35e571d9c4e3bc5fd717c8866d8b27c2f4baab1338b5e206646209eb3d2f3d1c.json
new file mode 100644
index 0000000000..32f097438e
--- /dev/null
+++ b/.sqlx/query-35e571d9c4e3bc5fd717c8866d8b27c2f4baab1338b5e206646209eb3d2f3d1c.json
@@ -0,0 +1,26 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT j.id, j.team_id FROM slack_import_job j\n WHERE j.registration_closed_at IS NOT NULL\n AND j.status IN ('uploading', 'processing', 'cancelling')\n AND NOT EXISTS (SELECT 1 FROM slack_import_conversation c WHERE c.job_id = j.id\n AND c.status IN ('awaiting_uploads', 'queued', 'importing'))\n AND EXISTS (SELECT 1 FROM slack_import_message_reference r\n WHERE r.job_id = j.id AND r.completed_at IS NULL)\n ORDER BY j.id LIMIT 1 FOR UPDATE OF j SKIP LOCKED",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "id",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 1,
+ "name": "team_id",
+ "type_info": "Uuid"
+ }
+ ],
+ "parameters": {
+ "Left": []
+ },
+ "nullable": [
+ false,
+ false
+ ]
+ },
+ "hash": "35e571d9c4e3bc5fd717c8866d8b27c2f4baab1338b5e206646209eb3d2f3d1c"
+}
diff --git a/.sqlx/query-370541e82eea62ff832bc2a199f39a4843cd777c9b5a3eb813129b534f6699bd.json b/.sqlx/query-370541e82eea62ff832bc2a199f39a4843cd777c9b5a3eb813129b534f6699bd.json
new file mode 100644
index 0000000000..71c01477e7
--- /dev/null
+++ b/.sqlx/query-370541e82eea62ff832bc2a199f39a4843cd777c9b5a3eb813129b534f6699bd.json
@@ -0,0 +1,38 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT (SELECT count(*) FROM comms_messages) AS \"messages!\", (SELECT count(*) FROM comms_reactions) AS \"reactions!\", (SELECT count(*) FROM slack_import_message_map) AS \"mappings!\", (SELECT count(*) FROM slack_import_outbox WHERE kind = 'search') AS \"search!\"",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "messages!",
+ "type_info": "Int8"
+ },
+ {
+ "ordinal": 1,
+ "name": "reactions!",
+ "type_info": "Int8"
+ },
+ {
+ "ordinal": 2,
+ "name": "mappings!",
+ "type_info": "Int8"
+ },
+ {
+ "ordinal": 3,
+ "name": "search!",
+ "type_info": "Int8"
+ }
+ ],
+ "parameters": {
+ "Left": []
+ },
+ "nullable": [
+ null,
+ null,
+ null,
+ null
+ ]
+ },
+ "hash": "370541e82eea62ff832bc2a199f39a4843cd777c9b5a3eb813129b534f6699bd"
+}
diff --git a/.sqlx/query-3882cb8e18a28bd02eee37d6b6e2b33cf2f4fce48129f22103f0f128eff26dd0.json b/.sqlx/query-3882cb8e18a28bd02eee37d6b6e2b33cf2f4fce48129f22103f0f128eff26dd0.json
new file mode 100644
index 0000000000..72d7765eed
--- /dev/null
+++ b/.sqlx/query-3882cb8e18a28bd02eee37d6b6e2b33cf2f4fce48129f22103f0f128eff26dd0.json
@@ -0,0 +1,18 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "UPDATE slack_import_conversation c\n SET part_count = $4, manifest_sha256 = $5, sealed_at = clock_timestamp(), updated_at = clock_timestamp()\n FROM slack_import_job j\n WHERE j.team_id = $1 AND j.id = $2 AND c.job_id = j.id AND c.slack_channel_id = $3",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Uuid",
+ "Text",
+ "Int4",
+ "Text"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "3882cb8e18a28bd02eee37d6b6e2b33cf2f4fce48129f22103f0f128eff26dd0"
+}
diff --git a/.sqlx/query-39c3d0b5452226c96c0c2dda07c83f334a984973a6e6db2aed491fbd231e142e.json b/.sqlx/query-39c3d0b5452226c96c0c2dda07c83f334a984973a6e6db2aed491fbd231e142e.json
new file mode 100644
index 0000000000..ced42dedfa
--- /dev/null
+++ b/.sqlx/query-39c3d0b5452226c96c0c2dda07c83f334a984973a6e6db2aed491fbd231e142e.json
@@ -0,0 +1,40 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT channel_type AS \"channel_type: ChannelType\", team_id\n FROM comms_channels WHERE id = $1 FOR UPDATE",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "channel_type: ChannelType",
+ "type_info": {
+ "Custom": {
+ "name": "comms_channel_type",
+ "kind": {
+ "Enum": [
+ "public",
+ "private",
+ "direct_message",
+ "team"
+ ]
+ }
+ }
+ }
+ },
+ {
+ "ordinal": 1,
+ "name": "team_id",
+ "type_info": "Uuid"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ false,
+ true
+ ]
+ },
+ "hash": "39c3d0b5452226c96c0c2dda07c83f334a984973a6e6db2aed491fbd231e142e"
+}
diff --git a/.sqlx/query-3a51f140f71b9faece1b1851f5144ea3ef6fa7f99da6a57045d4196f3b03d851.json b/.sqlx/query-3a51f140f71b9faece1b1851f5144ea3ef6fa7f99da6a57045d4196f3b03d851.json
new file mode 100644
index 0000000000..8cda30975e
--- /dev/null
+++ b/.sqlx/query-3a51f140f71b9faece1b1851f5144ea3ef6fa7f99da6a57045d4196f3b03d851.json
@@ -0,0 +1,20 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT pg_advisory_unlock(27001)",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "pg_advisory_unlock",
+ "type_info": "Bool"
+ }
+ ],
+ "parameters": {
+ "Left": []
+ },
+ "nullable": [
+ null
+ ]
+ },
+ "hash": "3a51f140f71b9faece1b1851f5144ea3ef6fa7f99da6a57045d4196f3b03d851"
+}
diff --git a/.sqlx/query-3b9870cd256b218e29b98d15e67ae60c4fe9c7c3fa085103723b0cb5165882fa.json b/.sqlx/query-3b9870cd256b218e29b98d15e67ae60c4fe9c7c3fa085103723b0cb5165882fa.json
new file mode 100644
index 0000000000..83e6d2251b
--- /dev/null
+++ b/.sqlx/query-3b9870cd256b218e29b98d15e67ae60c4fe9c7c3fa085103723b0cb5165882fa.json
@@ -0,0 +1,16 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "UPDATE slack_import_conversation SET channel_id = $3 WHERE job_id = $1 AND slack_channel_id = $2",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Text",
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "3b9870cd256b218e29b98d15e67ae60c4fe9c7c3fa085103723b0cb5165882fa"
+}
diff --git a/.sqlx/query-409b637f5e3b6dc968b7eac8b7183864a1d158717c507ed4a156479abf53b4e8.json b/.sqlx/query-409b637f5e3b6dc968b7eac8b7183864a1d158717c507ed4a156479abf53b4e8.json
new file mode 100644
index 0000000000..4dc6854094
--- /dev/null
+++ b/.sqlx/query-409b637f5e3b6dc968b7eac8b7183864a1d158717c507ed4a156479abf53b4e8.json
@@ -0,0 +1,20 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT message_id FROM slack_import_message_map",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "message_id",
+ "type_info": "Uuid"
+ }
+ ],
+ "parameters": {
+ "Left": []
+ },
+ "nullable": [
+ false
+ ]
+ },
+ "hash": "409b637f5e3b6dc968b7eac8b7183864a1d158717c507ed4a156479abf53b4e8"
+}
diff --git a/.sqlx/query-430ee6519c72200b7a1b911d0f85d3860085500cf9e88ef696ebb77d5345e9a6.json b/.sqlx/query-430ee6519c72200b7a1b911d0f85d3860085500cf9e88ef696ebb77d5345e9a6.json
new file mode 100644
index 0000000000..c8fb053e2f
--- /dev/null
+++ b/.sqlx/query-430ee6519c72200b7a1b911d0f85d3860085500cf9e88ef696ebb77d5345e9a6.json
@@ -0,0 +1,22 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT created_at FROM comms_reactions WHERE message_id = $1",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "created_at",
+ "type_info": "Timestamptz"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ false
+ ]
+ },
+ "hash": "430ee6519c72200b7a1b911d0f85d3860085500cf9e88ef696ebb77d5345e9a6"
+}
diff --git a/.sqlx/query-4c06c20732d36578c86bcfe55a58b0b63d82a57e0871837943d00bf29889c7c9.json b/.sqlx/query-4c06c20732d36578c86bcfe55a58b0b63d82a57e0871837943d00bf29889c7c9.json
new file mode 100644
index 0000000000..b76149e70d
--- /dev/null
+++ b/.sqlx/query-4c06c20732d36578c86bcfe55a58b0b63d82a57e0871837943d00bf29889c7c9.json
@@ -0,0 +1,16 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "INSERT INTO \"User\" (id, email, macro_user_id) VALUES ($1, '[email protected]', $2), ('macro|[email protected]', '[email protected]', $3)",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Text",
+ "Uuid",
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "4c06c20732d36578c86bcfe55a58b0b63d82a57e0871837943d00bf29889c7c9"
+}
diff --git a/.sqlx/query-4c5a7613d85a7b1f1e336256b2dd7f8a89f6b82418086dc1de8914e9d3879fd7.json b/.sqlx/query-4c5a7613d85a7b1f1e336256b2dd7f8a89f6b82418086dc1de8914e9d3879fd7.json
new file mode 100644
index 0000000000..4729d6f441
--- /dev/null
+++ b/.sqlx/query-4c5a7613d85a7b1f1e336256b2dd7f8a89f6b82418086dc1de8914e9d3879fd7.json
@@ -0,0 +1,40 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT name, created_at, auto_join_team, team_id FROM comms_channels WHERE id = $1",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "name",
+ "type_info": "Varchar"
+ },
+ {
+ "ordinal": 1,
+ "name": "created_at",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 2,
+ "name": "auto_join_team",
+ "type_info": "Bool"
+ },
+ {
+ "ordinal": 3,
+ "name": "team_id",
+ "type_info": "Uuid"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ true,
+ false,
+ false,
+ true
+ ]
+ },
+ "hash": "4c5a7613d85a7b1f1e336256b2dd7f8a89f6b82418086dc1de8914e9d3879fd7"
+}
diff --git a/.sqlx/query-4cbeb5b99dd01c1b6c121a0ac90046a8acf7c79c3848d30bd2ad0ae24482acfe.json b/.sqlx/query-4cbeb5b99dd01c1b6c121a0ac90046a8acf7c79c3848d30bd2ad0ae24482acfe.json
new file mode 100644
index 0000000000..436c964b61
--- /dev/null
+++ b/.sqlx/query-4cbeb5b99dd01c1b6c121a0ac90046a8acf7c79c3848d30bd2ad0ae24482acfe.json
@@ -0,0 +1,125 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT c.job_id, c.slack_channel_id, c.name, c.kind, c.archived, c.status, c.channel_id, c.part_count,\n c.processed, c.imported, c.duplicates, c.skipped, c.reactions,\n c.search_state, c.search_receipt_id, c.last_error, c.warnings,\n (SELECT count(*) FROM slack_import_upload u\n WHERE u.job_id = c.job_id AND u.slack_channel_id = c.slack_channel_id\n AND u.verified_at IS NOT NULL) AS \"verified_parts!\"\n FROM slack_import_conversation c JOIN slack_import_job j ON j.id = c.job_id\n WHERE j.team_id = $1 AND j.id = ANY($2)\n ORDER BY c.job_id DESC, c.slack_channel_id",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "job_id",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 1,
+ "name": "slack_channel_id",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 2,
+ "name": "name",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 3,
+ "name": "kind",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 4,
+ "name": "archived",
+ "type_info": "Bool"
+ },
+ {
+ "ordinal": 5,
+ "name": "status",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 6,
+ "name": "channel_id",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 7,
+ "name": "part_count",
+ "type_info": "Int4"
+ },
+ {
+ "ordinal": 8,
+ "name": "processed",
+ "type_info": "Int8"
+ },
+ {
+ "ordinal": 9,
+ "name": "imported",
+ "type_info": "Int8"
+ },
+ {
+ "ordinal": 10,
+ "name": "duplicates",
+ "type_info": "Int8"
+ },
+ {
+ "ordinal": 11,
+ "name": "skipped",
+ "type_info": "Int8"
+ },
+ {
+ "ordinal": 12,
+ "name": "reactions",
+ "type_info": "Int8"
+ },
+ {
+ "ordinal": 13,
+ "name": "search_state",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 14,
+ "name": "search_receipt_id",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 15,
+ "name": "last_error",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 16,
+ "name": "warnings",
+ "type_info": "TextArray"
+ },
+ {
+ "ordinal": 17,
+ "name": "verified_parts!",
+ "type_info": "Int8"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "UuidArray"
+ ]
+ },
+ "nullable": [
+ false,
+ false,
+ false,
+ false,
+ false,
+ false,
+ true,
+ true,
+ false,
+ false,
+ false,
+ false,
+ false,
+ false,
+ true,
+ true,
+ false,
+ null
+ ]
+ },
+ "hash": "4cbeb5b99dd01c1b6c121a0ac90046a8acf7c79c3848d30bd2ad0ae24482acfe"
+}
diff --git a/.sqlx/query-4dc8e5fb101c7e05efde4720e9dc538eecf360dc1e872223634a96df79678418.json b/.sqlx/query-4dc8e5fb101c7e05efde4720e9dc538eecf360dc1e872223634a96df79678418.json
new file mode 100644
index 0000000000..692e031df3
--- /dev/null
+++ b/.sqlx/query-4dc8e5fb101c7e05efde4720e9dc538eecf360dc1e872223634a96df79678418.json
@@ -0,0 +1,40 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT source_created_at, resolved_created_at, creator_id, message_count FROM slack_import_conversation WHERE job_id = $1 AND slack_channel_id = 'D1'",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "source_created_at",
+ "type_info": "Int8"
+ },
+ {
+ "ordinal": 1,
+ "name": "resolved_created_at",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 2,
+ "name": "creator_id",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 3,
+ "name": "message_count",
+ "type_info": "Int8"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ true,
+ true,
+ true,
+ true
+ ]
+ },
+ "hash": "4dc8e5fb101c7e05efde4720e9dc538eecf360dc1e872223634a96df79678418"
+}
diff --git a/.sqlx/query-4dcba2eab98a49084c07a494077c9e81cabd75490b4a1ae5fd518dffd0f47c63.json b/.sqlx/query-4dcba2eab98a49084c07a494077c9e81cabd75490b4a1ae5fd518dffd0f47c63.json
new file mode 100644
index 0000000000..3a7cd1e32f
--- /dev/null
+++ b/.sqlx/query-4dcba2eab98a49084c07a494077c9e81cabd75490b4a1ae5fd518dffd0f47c63.json
@@ -0,0 +1,31 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT part_index AS \"part_index!\", record_count AS \"record_count!\" FROM slack_import_upload\n WHERE job_id = $1 AND slack_channel_id = $2 AND part_index >= $3 AND part_index <= $4\n ORDER BY part_index",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "part_index!",
+ "type_info": "Int4"
+ },
+ {
+ "ordinal": 1,
+ "name": "record_count!",
+ "type_info": "Int4"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Text",
+ "Int4",
+ "Int4"
+ ]
+ },
+ "nullable": [
+ true,
+ true
+ ]
+ },
+ "hash": "4dcba2eab98a49084c07a494077c9e81cabd75490b4a1ae5fd518dffd0f47c63"
+}
diff --git a/.sqlx/query-4e1c8fefb75c5a2e0b2b4b31629025b7d9b648d538b93efbfcd42c13707bc98d.json b/.sqlx/query-4e1c8fefb75c5a2e0b2b4b31629025b7d9b648d538b93efbfcd42c13707bc98d.json
new file mode 100644
index 0000000000..6c05a91a87
--- /dev/null
+++ b/.sqlx/query-4e1c8fefb75c5a2e0b2b4b31629025b7d9b648d538b93efbfcd42c13707bc98d.json
@@ -0,0 +1,36 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n SELECT DISTINCT entity_id, entity_type,\n (team_id IS NULL OR team_id = $3) AS \"compatible!\"\n FROM import_entity\n WHERE source = 'slack' AND foreign_id = $1 AND status = 'imported'\n AND (user_id = $2 OR team_id = $3)\n LIMIT 2\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "entity_id",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 1,
+ "name": "entity_type",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 2,
+ "name": "compatible!",
+ "type_info": "Bool"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Text",
+ "Text",
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ true,
+ true,
+ null
+ ]
+ },
+ "hash": "4e1c8fefb75c5a2e0b2b4b31629025b7d9b648d538b93efbfcd42c13707bc98d"
+}
diff --git a/.sqlx/query-4e84745f9618a807c80b74ad581730db76a0d75d20ed029798842e715fac125c.json b/.sqlx/query-4e84745f9618a807c80b74ad581730db76a0d75d20ed029798842e715fac125c.json
new file mode 100644
index 0000000000..88ff73d8ab
--- /dev/null
+++ b/.sqlx/query-4e84745f9618a807c80b74ad581730db76a0d75d20ed029798842e715fac125c.json
@@ -0,0 +1,17 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n INSERT INTO comms_messages (id, parent_entity_type, parent_entity_id, thread_id, sender_id, content, created_at)\n VALUES ($1, 'channel', $2, $3, 'macro|[email protected]', 'reply', '2020-01-02T00:00:00Z'),\n ($4, 'document', 'doc-backfill-test', NULL, 'macro|[email protected]', 'document', '2020-01-02T00:00:00Z')\n ",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Text",
+ "Uuid",
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "4e84745f9618a807c80b74ad581730db76a0d75d20ed029798842e715fac125c"
+}
diff --git a/.sqlx/query-4fc233f6f1e289e9acb848611c00b92d5c783802d3df1ff5a2893888246a960d.json b/.sqlx/query-4fc233f6f1e289e9acb848611c00b92d5c783802d3df1ff5a2893888246a960d.json
new file mode 100644
index 0000000000..71d62aeb00
--- /dev/null
+++ b/.sqlx/query-4fc233f6f1e289e9acb848611c00b92d5c783802d3df1ff5a2893888246a960d.json
@@ -0,0 +1,15 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "UPDATE comms_channel_participants SET left_at = now() WHERE channel_id = $1 AND user_id = $2",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Text"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "4fc233f6f1e289e9acb848611c00b92d5c783802d3df1ff5a2893888246a960d"
+}
diff --git a/.sqlx/query-50b96d640b2a960215c974fd09b9757b9c900ab89b71393e32535ca67634487c.json b/.sqlx/query-50b96d640b2a960215c974fd09b9757b9c900ab89b71393e32535ca67634487c.json
new file mode 100644
index 0000000000..507a695306
--- /dev/null
+++ b/.sqlx/query-50b96d640b2a960215c974fd09b9757b9c900ab89b71393e32535ca67634487c.json
@@ -0,0 +1,20 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT pg_advisory_lock(27001)",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "pg_advisory_lock",
+ "type_info": "Void"
+ }
+ ],
+ "parameters": {
+ "Left": []
+ },
+ "nullable": [
+ null
+ ]
+ },
+ "hash": "50b96d640b2a960215c974fd09b9757b9c900ab89b71393e32535ca67634487c"
+}
diff --git a/.sqlx/query-526d0d03d698cedf01017f32d59a864b92521d97afe0175685da55c675e71c77.json b/.sqlx/query-526d0d03d698cedf01017f32d59a864b92521d97afe0175685da55c675e71c77.json
new file mode 100644
index 0000000000..8295651eb7
--- /dev/null
+++ b/.sqlx/query-526d0d03d698cedf01017f32d59a864b92521d97afe0175685da55c675e71c77.json
@@ -0,0 +1,26 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT job_id, message_id FROM slack_import_message_reference",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "job_id",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 1,
+ "name": "message_id",
+ "type_info": "Uuid"
+ }
+ ],
+ "parameters": {
+ "Left": []
+ },
+ "nullable": [
+ false,
+ false
+ ]
+ },
+ "hash": "526d0d03d698cedf01017f32d59a864b92521d97afe0175685da55c675e71c77"
+}
diff --git a/.sqlx/query-537a363c19383e913eab300a7ef363cd28d60a4a08bcf1fd53bec05b245b787a.json b/.sqlx/query-537a363c19383e913eab300a7ef363cd28d60a4a08bcf1fd53bec05b245b787a.json
new file mode 100644
index 0000000000..d513f4d4e6
--- /dev/null
+++ b/.sqlx/query-537a363c19383e913eab300a7ef363cd28d60a4a08bcf1fd53bec05b245b787a.json
@@ -0,0 +1,17 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "UPDATE slack_import_outbox SET\n published_at = CASE WHEN $4 = 'completed' THEN coalesce(published_at, clock_timestamp()) ELSE published_at END,\n available_at = clock_timestamp() + interval '60 seconds'\n WHERE job_id = $1 AND slack_channel_id = ANY($2) AND kind = 'search' AND generation = $3",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "TextArray",
+ "Int8",
+ "Text"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "537a363c19383e913eab300a7ef363cd28d60a4a08bcf1fd53bec05b245b787a"
+}
diff --git a/.sqlx/query-538cf30650432b57113d61f2146a3c8f0bd38d970ff71b5250a8c0360f37bfcc.json b/.sqlx/query-538cf30650432b57113d61f2146a3c8f0bd38d970ff71b5250a8c0360f37bfcc.json
new file mode 100644
index 0000000000..15b268200e
--- /dev/null
+++ b/.sqlx/query-538cf30650432b57113d61f2146a3c8f0bd38d970ff71b5250a8c0360f37bfcc.json
@@ -0,0 +1,16 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "INSERT INTO slack_import_conversation\n (job_id, slack_channel_id, kind, name, folder, member_ids,\n creator_id, source_created_at, archived, message_count)\n SELECT j.id, m.slack_channel_id, m.kind, m.name, m.folder, m.member_ids,\n m.creator_id, m.source_created_at, m.archived, m.message_count\n FROM slack_import_job j CROSS JOIN jsonb_to_recordset($3) AS m(\n slack_channel_id text, kind text, name text, folder text,\n member_ids text[], creator_id text, source_created_at bigint,\n archived boolean, message_count bigint)\n WHERE j.team_id = $1 AND j.id = $2\n ON CONFLICT (job_id, slack_channel_id) DO NOTHING",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Uuid",
+ "Jsonb"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "538cf30650432b57113d61f2146a3c8f0bd38d970ff71b5250a8c0360f37bfcc"
+}
diff --git a/.sqlx/query-5417eeb2b1d7b6a1185f10ecb03a3bd66ae34884eb5b99494a041caa4c6df25a.json b/.sqlx/query-5417eeb2b1d7b6a1185f10ecb03a3bd66ae34884eb5b99494a041caa4c6df25a.json
new file mode 100644
index 0000000000..6f73cacc1b
--- /dev/null
+++ b/.sqlx/query-5417eeb2b1d7b6a1185f10ecb03a3bd66ae34884eb5b99494a041caa4c6df25a.json
@@ -0,0 +1,22 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT event_generation FROM slack_import_conversation WHERE job_id = $1 AND slack_channel_id = 'C1'",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "event_generation",
+ "type_info": "Int8"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ false
+ ]
+ },
+ "hash": "5417eeb2b1d7b6a1185f10ecb03a3bd66ae34884eb5b99494a041caa4c6df25a"
+}
diff --git a/.sqlx/query-547c9234ea07bf7994b7fc129156a8ce65b19977181ed5c45a33b3c58631d981.json b/.sqlx/query-547c9234ea07bf7994b7fc129156a8ce65b19977181ed5c45a33b3c58631d981.json
new file mode 100644
index 0000000000..794434f8ed
--- /dev/null
+++ b/.sqlx/query-547c9234ea07bf7994b7fc129156a8ce65b19977181ed5c45a33b3c58631d981.json
@@ -0,0 +1,35 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT s.index AS \"index!\", m.message_id, m.channel_id\n FROM jsonb_to_recordset($1) AS s(index integer, channel text, ts bigint)\n JOIN slack_import_message_map m ON m.team_id = $2\n AND m.slack_channel_id = s.channel AND m.slack_ts = s.ts",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "index!",
+ "type_info": "Int4"
+ },
+ {
+ "ordinal": 1,
+ "name": "message_id",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 2,
+ "name": "channel_id",
+ "type_info": "Uuid"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Jsonb",
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ null,
+ false,
+ false
+ ]
+ },
+ "hash": "547c9234ea07bf7994b7fc129156a8ce65b19977181ed5c45a33b3c58631d981"
+}
diff --git a/.sqlx/query-556a228af37ea6fc972040c2b9c4a1ccab301b9959db6a95fdf1001282d947bd.json b/.sqlx/query-556a228af37ea6fc972040c2b9c4a1ccab301b9959db6a95fdf1001282d947bd.json
new file mode 100644
index 0000000000..e5223771ec
--- /dev/null
+++ b/.sqlx/query-556a228af37ea6fc972040c2b9c4a1ccab301b9959db6a95fdf1001282d947bd.json
@@ -0,0 +1,47 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT status, registration_closed_at, include_message_history, registered_bytes,\n (staging_expires_at <= clock_timestamp()) AS \"expired!\"\n FROM slack_import_job WHERE team_id = $1 AND id = $2 FOR UPDATE",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "status",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 1,
+ "name": "registration_closed_at",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 2,
+ "name": "include_message_history",
+ "type_info": "Bool"
+ },
+ {
+ "ordinal": 3,
+ "name": "registered_bytes",
+ "type_info": "Int8"
+ },
+ {
+ "ordinal": 4,
+ "name": "expired!",
+ "type_info": "Bool"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ false,
+ true,
+ false,
+ false,
+ null
+ ]
+ },
+ "hash": "556a228af37ea6fc972040c2b9c4a1ccab301b9959db6a95fdf1001282d947bd"
+}
diff --git a/.sqlx/query-567b8d6383ddfe7d7a9fd1f5de9098d3d4194eb0ebf2d3954542127a78c440a7.json b/.sqlx/query-567b8d6383ddfe7d7a9fd1f5de9098d3d4194eb0ebf2d3954542127a78c440a7.json
new file mode 100644
index 0000000000..88e6019276
--- /dev/null
+++ b/.sqlx/query-567b8d6383ddfe7d7a9fd1f5de9098d3d4194eb0ebf2d3954542127a78c440a7.json
@@ -0,0 +1,16 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "INSERT INTO comms_channel_participants (channel_id, user_id, role, joined_at, left_at) SELECT $1, user_id, role, joined_at, $3 FROM comms_channel_participants WHERE channel_id = $2",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Uuid",
+ "Timestamptz"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "567b8d6383ddfe7d7a9fd1f5de9098d3d4194eb0ebf2d3954542127a78c440a7"
+}
diff --git a/.sqlx/query-56c53754a1c62a2f2ea12d062c0296b472ba20058ab8c98e5938882da862991f.json b/.sqlx/query-56c53754a1c62a2f2ea12d062c0296b472ba20058ab8c98e5938882da862991f.json
new file mode 100644
index 0000000000..30bc53a3d0
--- /dev/null
+++ b/.sqlx/query-56c53754a1c62a2f2ea12d062c0296b472ba20058ab8c98e5938882da862991f.json
@@ -0,0 +1,15 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "WITH ready AS (\n UPDATE slack_import_conversation c\n SET status = 'queued', event_generation = event_generation + 1, updated_at = clock_timestamp()\n FROM slack_import_job j\n WHERE j.team_id = $1 AND j.id = $2 AND c.job_id = j.id\n AND j.status = 'uploading' AND j.registration_closed_at IS NULL\n AND j.cancel_requested_at IS NULL\n AND c.status = 'awaiting_uploads' AND c.sealed_at IS NOT NULL\n AND EXISTS (SELECT 1 FROM slack_import_upload u\n WHERE u.job_id = j.id AND u.slack_channel_id IS NULL AND u.verified_at IS NOT NULL)\n AND c.part_count = (SELECT count(*) FROM slack_import_upload u\n WHERE u.job_id = c.job_id AND u.slack_channel_id = c.slack_channel_id\n AND u.verified_at IS NOT NULL)\n RETURNING c.job_id, c.slack_channel_id, c.event_generation\n )\n INSERT INTO slack_import_outbox (job_id, slack_channel_id, kind, generation)\n SELECT job_id, slack_channel_id, 'import', event_generation FROM ready\n ON CONFLICT (job_id, slack_channel_id, kind, generation) DO NOTHING",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "56c53754a1c62a2f2ea12d062c0296b472ba20058ab8c98e5938882da862991f"
+}
diff --git a/.sqlx/query-58016ffa6318e7c7bf0a233c97d4cc039a0ce08ce6a42ca6bd2c5312cfaacbd4.json b/.sqlx/query-58016ffa6318e7c7bf0a233c97d4cc039a0ce08ce6a42ca6bd2c5312cfaacbd4.json
new file mode 100644
index 0000000000..1b69f3a8b0
--- /dev/null
+++ b/.sqlx/query-58016ffa6318e7c7bf0a233c97d4cc039a0ce08ce6a42ca6bd2c5312cfaacbd4.json
@@ -0,0 +1,20 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT count(*) FROM comms_reactions",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "count",
+ "type_info": "Int8"
+ }
+ ],
+ "parameters": {
+ "Left": []
+ },
+ "nullable": [
+ null
+ ]
+ },
+ "hash": "58016ffa6318e7c7bf0a233c97d4cc039a0ce08ce6a42ca6bd2c5312cfaacbd4"
+}
diff --git a/.sqlx/query-58b7a93ac4ac873e446f3a61c653bf4181c5996d25809e6b34661fd9100903a4.json b/.sqlx/query-58b7a93ac4ac873e446f3a61c653bf4181c5996d25809e6b34661fd9100903a4.json
new file mode 100644
index 0000000000..45d0f5532d
--- /dev/null
+++ b/.sqlx/query-58b7a93ac4ac873e446f3a61c653bf4181c5996d25809e6b34661fd9100903a4.json
@@ -0,0 +1,24 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT c.slack_channel_id FROM slack_import_conversation c\n JOIN slack_import_job j ON j.id = c.job_id\n WHERE j.team_id = $1 AND j.id = $2 AND c.slack_channel_id = ANY($3)\n AND c.status IN ('awaiting_uploads', 'queued', 'importing')",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "slack_channel_id",
+ "type_info": "Text"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Uuid",
+ "TextArray"
+ ]
+ },
+ "nullable": [
+ false
+ ]
+ },
+ "hash": "58b7a93ac4ac873e446f3a61c653bf4181c5996d25809e6b34661fd9100903a4"
+}
diff --git a/.sqlx/query-597cb5e7c59b50d224cf1588235a833d782e6bb4185c949757584d790e0fe56d.json b/.sqlx/query-597cb5e7c59b50d224cf1588235a833d782e6bb4185c949757584d790e0fe56d.json
new file mode 100644
index 0000000000..972cf1ea7b
--- /dev/null
+++ b/.sqlx/query-597cb5e7c59b50d224cf1588235a833d782e6bb4185c949757584d790e0fe56d.json
@@ -0,0 +1,16 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "UPDATE slack_import_conversation SET warnings = array_append(warnings, $3)\n WHERE job_id = $1 AND slack_channel_id = $2 AND NOT ($3 = ANY(warnings))",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Text",
+ "Text"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "597cb5e7c59b50d224cf1588235a833d782e6bb4185c949757584d790e0fe56d"
+}
diff --git a/.sqlx/query-5b306d29053e2926b145c31b331295ebcc120ad05b03b795cc9723545f7815cb.json b/.sqlx/query-5b306d29053e2926b145c31b331295ebcc120ad05b03b795cc9723545f7815cb.json
new file mode 100644
index 0000000000..c9cd2a8e22
--- /dev/null
+++ b/.sqlx/query-5b306d29053e2926b145c31b331295ebcc120ad05b03b795cc9723545f7815cb.json
@@ -0,0 +1,14 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "UPDATE slack_import_conversation SET lease_expires_at = clock_timestamp() - interval '1 second' WHERE job_id = $1",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "5b306d29053e2926b145c31b331295ebcc120ad05b03b795cc9723545f7815cb"
+}
diff --git a/.sqlx/query-5c61df8f8596e73b68ee4ceab0636c10383ae2c04066b5804e3c8de193e694d6.json b/.sqlx/query-5c61df8f8596e73b68ee4ceab0636c10383ae2c04066b5804e3c8de193e694d6.json
new file mode 100644
index 0000000000..01dbc02f72
--- /dev/null
+++ b/.sqlx/query-5c61df8f8596e73b68ee4ceab0636c10383ae2c04066b5804e3c8de193e694d6.json
@@ -0,0 +1,29 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "INSERT INTO slack_import_job\n (id, team_id, user_id, idempotency_token, request_sha256,\n source_workspace_id, confirmed_unknown, include_message_history)\n VALUES ($1, $2, $3, $4, $5, $6, $7, $8)\n ON CONFLICT (team_id, user_id, idempotency_token) DO NOTHING\n RETURNING id",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "id",
+ "type_info": "Uuid"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Uuid",
+ "Text",
+ "Uuid",
+ "Text",
+ "Text",
+ "Bool",
+ "Bool"
+ ]
+ },
+ "nullable": [
+ false
+ ]
+ },
+ "hash": "5c61df8f8596e73b68ee4ceab0636c10383ae2c04066b5804e3c8de193e694d6"
+}
diff --git a/.sqlx/query-612735cf543ddca6001668303d4050ab0fec75a4b97a639bf5e3f3a35df9c2df.json b/.sqlx/query-612735cf543ddca6001668303d4050ab0fec75a4b97a639bf5e3f3a35df9c2df.json
new file mode 100644
index 0000000000..206b5621f3
--- /dev/null
+++ b/.sqlx/query-612735cf543ddca6001668303d4050ab0fec75a4b97a639bf5e3f3a35df9c2df.json
@@ -0,0 +1,15 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "UPDATE slack_import_conversation SET lease_expires_at = clock_timestamp() - interval '1 second' WHERE job_id = $1 AND slack_channel_id = $2",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Text"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "612735cf543ddca6001668303d4050ab0fec75a4b97a639bf5e3f3a35df9c2df"
+}
diff --git a/.sqlx/query-633851f2ef2a49c338a54e7ce207fa407f70c3bdb211c144db973d9f8027a087.json b/.sqlx/query-633851f2ef2a49c338a54e7ce207fa407f70c3bdb211c144db973d9f8027a087.json
new file mode 100644
index 0000000000..6c90910ad0
--- /dev/null
+++ b/.sqlx/query-633851f2ef2a49c338a54e7ce207fa407f70c3bdb211c144db973d9f8027a087.json
@@ -0,0 +1,22 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT updated_at FROM comms_channels WHERE id = $1",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "updated_at",
+ "type_info": "Timestamptz"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ false
+ ]
+ },
+ "hash": "633851f2ef2a49c338a54e7ce207fa407f70c3bdb211c144db973d9f8027a087"
+}
diff --git a/.sqlx/query-637ecfc4ea514f2aac0bd1ef27738bb9db884b5b3dc604aecb2bd8b60b9c3d29.json b/.sqlx/query-637ecfc4ea514f2aac0bd1ef27738bb9db884b5b3dc604aecb2bd8b60b9c3d29.json
new file mode 100644
index 0000000000..3768cd9d12
--- /dev/null
+++ b/.sqlx/query-637ecfc4ea514f2aac0bd1ef27738bb9db884b5b3dc604aecb2bd8b60b9c3d29.json
@@ -0,0 +1,14 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "INSERT INTO team (id, name, owner_id) VALUES ($1, 'Lifecycle', 'macro|[email protected]')",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "637ecfc4ea514f2aac0bd1ef27738bb9db884b5b3dc604aecb2bd8b60b9c3d29"
+}
diff --git a/.sqlx/query-662f5ccc9966fd852ef6466d6914c8e22cab300c88e2147520f818affdf5f34c.json b/.sqlx/query-662f5ccc9966fd852ef6466d6914c8e22cab300c88e2147520f818affdf5f34c.json
new file mode 100644
index 0000000000..e03e2fc22d
--- /dev/null
+++ b/.sqlx/query-662f5ccc9966fd852ef6466d6914c8e22cab300c88e2147520f818affdf5f34c.json
@@ -0,0 +1,14 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "UPDATE slack_import_conversation SET lease_expires_at = clock_timestamp() - interval '1 second' WHERE job_id = $1 AND status = 'importing'",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "662f5ccc9966fd852ef6466d6914c8e22cab300c88e2147520f818affdf5f34c"
+}
diff --git a/.sqlx/query-6823d54d359b4a38b614d373d485f8e44123069eca14bd1f20153582f78b7632.json b/.sqlx/query-6823d54d359b4a38b614d373d485f8e44123069eca14bd1f20153582f78b7632.json
new file mode 100644
index 0000000000..64e63bdb66
--- /dev/null
+++ b/.sqlx/query-6823d54d359b4a38b614d373d485f8e44123069eca14bd1f20153582f78b7632.json
@@ -0,0 +1,71 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT checkpoint_part, checkpoint_record, part_count, channel_id,\n processed, imported, duplicates, skipped, reactions\n FROM slack_import_conversation WHERE job_id = $1 AND slack_channel_id = $2",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "checkpoint_part",
+ "type_info": "Int4"
+ },
+ {
+ "ordinal": 1,
+ "name": "checkpoint_record",
+ "type_info": "Int4"
+ },
+ {
+ "ordinal": 2,
+ "name": "part_count",
+ "type_info": "Int4"
+ },
+ {
+ "ordinal": 3,
+ "name": "channel_id",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 4,
+ "name": "processed",
+ "type_info": "Int8"
+ },
+ {
+ "ordinal": 5,
+ "name": "imported",
+ "type_info": "Int8"
+ },
+ {
+ "ordinal": 6,
+ "name": "duplicates",
+ "type_info": "Int8"
+ },
+ {
+ "ordinal": 7,
+ "name": "skipped",
+ "type_info": "Int8"
+ },
+ {
+ "ordinal": 8,
+ "name": "reactions",
+ "type_info": "Int8"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Text"
+ ]
+ },
+ "nullable": [
+ false,
+ false,
+ true,
+ true,
+ false,
+ false,
+ false,
+ false,
+ false
+ ]
+ },
+ "hash": "6823d54d359b4a38b614d373d485f8e44123069eca14bd1f20153582f78b7632"
+}
diff --git a/.sqlx/query-6951612c82fe6990c82ae48d53fcff3dd5aa06fbd440684c2d12d049f90e2f31.json b/.sqlx/query-6951612c82fe6990c82ae48d53fcff3dd5aa06fbd440684c2d12d049f90e2f31.json
new file mode 100644
index 0000000000..a5fe35751f
--- /dev/null
+++ b/.sqlx/query-6951612c82fe6990c82ae48d53fcff3dd5aa06fbd440684c2d12d049f90e2f31.json
@@ -0,0 +1,36 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT count(*) AS \"count!\", max(u.part_index) AS last_index,\n encode(digest(coalesce(string_agg(\n u.part_index::text || ':' || u.sha256 || ':' || u.byte_length::text || ':' || u.record_count::text || E'\\n',\n '' ORDER BY u.part_index), ''), 'sha256'), 'hex') AS \"sha256!\"\n FROM slack_import_upload u JOIN slack_import_job j ON j.id = u.job_id\n WHERE j.team_id = $1 AND j.id = $2 AND u.slack_channel_id = $3",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "count!",
+ "type_info": "Int8"
+ },
+ {
+ "ordinal": 1,
+ "name": "last_index",
+ "type_info": "Int4"
+ },
+ {
+ "ordinal": 2,
+ "name": "sha256!",
+ "type_info": "Text"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Uuid",
+ "Text"
+ ]
+ },
+ "nullable": [
+ null,
+ null,
+ null
+ ]
+ },
+ "hash": "6951612c82fe6990c82ae48d53fcff3dd5aa06fbd440684c2d12d049f90e2f31"
+}
diff --git a/.sqlx/query-6a6f629b0b617bf5cf39f0e493f0d8ac42d87a8d1e536d48710b2acdbc0bb93b.json b/.sqlx/query-6a6f629b0b617bf5cf39f0e493f0d8ac42d87a8d1e536d48710b2acdbc0bb93b.json
new file mode 100644
index 0000000000..25577da7ab
--- /dev/null
+++ b/.sqlx/query-6a6f629b0b617bf5cf39f0e493f0d8ac42d87a8d1e536d48710b2acdbc0bb93b.json
@@ -0,0 +1,22 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT COUNT(*) FROM comms_channel_participants WHERE channel_id = $1 AND left_at IS NOT NULL",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "count",
+ "type_info": "Int8"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ null
+ ]
+ },
+ "hash": "6a6f629b0b617bf5cf39f0e493f0d8ac42d87a8d1e536d48710b2acdbc0bb93b"
+}
diff --git a/.sqlx/query-6b6cb9b9ba799d85ba99931b6c395a454581fb2c45780f79457e108980e9f834.json b/.sqlx/query-6b6cb9b9ba799d85ba99931b6c395a454581fb2c45780f79457e108980e9f834.json
new file mode 100644
index 0000000000..65e3358829
--- /dev/null
+++ b/.sqlx/query-6b6cb9b9ba799d85ba99931b6c395a454581fb2c45780f79457e108980e9f834.json
@@ -0,0 +1,15 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "UPDATE slack_import_job SET\n registration_closed_at = coalesce(registration_closed_at, clock_timestamp()),\n cancel_requested_at = clock_timestamp(), status = 'cancelling'\n WHERE team_id = $1 AND id = $2 AND status IN ('uploading', 'processing')",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "6b6cb9b9ba799d85ba99931b6c395a454581fb2c45780f79457e108980e9f834"
+}
diff --git a/.sqlx/query-6b97d07d2a1ca458ff76c6c00b3b4154096bfa144486e3ba1858e541e017a64f.json b/.sqlx/query-6b97d07d2a1ca458ff76c6c00b3b4154096bfa144486e3ba1858e541e017a64f.json
new file mode 100644
index 0000000000..446b29cd6d
--- /dev/null
+++ b/.sqlx/query-6b97d07d2a1ca458ff76c6c00b3b4154096bfa144486e3ba1858e541e017a64f.json
@@ -0,0 +1,14 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "INSERT INTO macro_user (id, username, email, stripe_customer_id) VALUES ($1, 'lifecycle', '[email protected]', 'lifecycle-customer')",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "6b97d07d2a1ca458ff76c6c00b3b4154096bfa144486e3ba1858e541e017a64f"
+}
diff --git a/.sqlx/query-6c6941bffceef2a8421c6795ec7b9b6bd907176dfdc122ff9f7493290bcbfd88.json b/.sqlx/query-6c6941bffceef2a8421c6795ec7b9b6bd907176dfdc122ff9f7493290bcbfd88.json
new file mode 100644
index 0000000000..80ed788614
--- /dev/null
+++ b/.sqlx/query-6c6941bffceef2a8421c6795ec7b9b6bd907176dfdc122ff9f7493290bcbfd88.json
@@ -0,0 +1,19 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "UPDATE slack_import_upload u\n SET verified_at = clock_timestamp(), verified_version_id = $5, verified_etag = $6\n FROM slack_import_job j\n WHERE j.team_id = $1 AND j.id = $2 AND u.job_id = j.id\n AND u.slack_channel_id IS NOT DISTINCT FROM $3\n AND u.part_index IS NOT DISTINCT FROM $4 AND u.verified_at IS NULL",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Uuid",
+ "Text",
+ "Int4",
+ "Text",
+ "Text"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "6c6941bffceef2a8421c6795ec7b9b6bd907176dfdc122ff9f7493290bcbfd88"
+}
diff --git a/.sqlx/query-6d81455fd223ed55ed47a74f5ebcbe9e89b1bc01c66bd2da58702c020f5dfabe.json b/.sqlx/query-6d81455fd223ed55ed47a74f5ebcbe9e89b1bc01c66bd2da58702c020f5dfabe.json
new file mode 100644
index 0000000000..3ededd60f6
--- /dev/null
+++ b/.sqlx/query-6d81455fd223ed55ed47a74f5ebcbe9e89b1bc01c66bd2da58702c020f5dfabe.json
@@ -0,0 +1,16 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "INSERT INTO comms_channels (id, name, channel_type, team_id, owner_id)\n VALUES ($1, CASE WHEN $2 = 'direct_message' THEN NULL ELSE 'Historical' END,\n $2::comms_channel_type, $3, 'macro|[email protected]')",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Text",
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "6d81455fd223ed55ed47a74f5ebcbe9e89b1bc01c66bd2da58702c020f5dfabe"
+}
diff --git a/.sqlx/query-6e165ca672ced9ce1765bd4ac02d0aeac6bd45d0d86b0a8aa5b5f6c24d5f722e.json b/.sqlx/query-6e165ca672ced9ce1765bd4ac02d0aeac6bd45d0d86b0a8aa5b5f6c24d5f722e.json
new file mode 100644
index 0000000000..e62a825123
--- /dev/null
+++ b/.sqlx/query-6e165ca672ced9ce1765bd4ac02d0aeac6bd45d0d86b0a8aa5b5f6c24d5f722e.json
@@ -0,0 +1,15 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "UPDATE comms_channel_participants SET role = 'admin' WHERE channel_id = $1 AND user_id = $2",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Text"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "6e165ca672ced9ce1765bd4ac02d0aeac6bd45d0d86b0a8aa5b5f6c24d5f722e"
+}
diff --git a/.sqlx/query-706d5663b1a21352d76c9b2154b908a511dfd129b487cc486fcc97ac7706f5c5.json b/.sqlx/query-706d5663b1a21352d76c9b2154b908a511dfd129b487cc486fcc97ac7706f5c5.json
new file mode 100644
index 0000000000..5506b4164a
--- /dev/null
+++ b/.sqlx/query-706d5663b1a21352d76c9b2154b908a511dfd129b487cc486fcc97ac7706f5c5.json
@@ -0,0 +1,30 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT j.team_id, j.user_id FROM slack_import_job j\n JOIN slack_import_conversation c ON c.job_id = j.id\n WHERE j.id = $1 AND c.slack_channel_id = $2 AND c.event_generation = $3\n AND c.status IN ('queued', 'importing') AND j.cancel_requested_at IS NULL",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "team_id",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 1,
+ "name": "user_id",
+ "type_info": "Text"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Text",
+ "Int8"
+ ]
+ },
+ "nullable": [
+ false,
+ false
+ ]
+ },
+ "hash": "706d5663b1a21352d76c9b2154b908a511dfd129b487cc486fcc97ac7706f5c5"
+}
diff --git a/.sqlx/query-71a2a32ea6eaf82ae9e2ae9835c8941a7375cfc7ce3609a8f40b8f7280583145.json b/.sqlx/query-71a2a32ea6eaf82ae9e2ae9835c8941a7375cfc7ce3609a8f40b8f7280583145.json
new file mode 100644
index 0000000000..373e4f75ad
--- /dev/null
+++ b/.sqlx/query-71a2a32ea6eaf82ae9e2ae9835c8941a7375cfc7ce3609a8f40b8f7280583145.json
@@ -0,0 +1,23 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n SELECT c.id FROM comms_channels c\n WHERE c.channel_type = 'direct_message'\n AND EXISTS (SELECT 1 FROM comms_channel_participants p\n WHERE p.channel_id = c.id AND p.user_id = $1)\n AND EXISTS (SELECT 1 FROM comms_channel_participants p\n WHERE p.channel_id = c.id AND p.user_id = $2)\n AND (SELECT COUNT(*) FROM comms_channel_participants p WHERE p.channel_id = c.id) = 2\n ORDER BY c.created_at, c.id\n LIMIT 1\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "id",
+ "type_info": "Uuid"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Text",
+ "Text"
+ ]
+ },
+ "nullable": [
+ false
+ ]
+ },
+ "hash": "71a2a32ea6eaf82ae9e2ae9835c8941a7375cfc7ce3609a8f40b8f7280583145"
+}
diff --git a/.sqlx/query-72e877a37af22f14848c03c35dfd12fc302d6e645938ff20aa253d7433cd3c44.json b/.sqlx/query-72e877a37af22f14848c03c35dfd12fc302d6e645938ff20aa253d7433cd3c44.json
new file mode 100644
index 0000000000..07dc97aedf
--- /dev/null
+++ b/.sqlx/query-72e877a37af22f14848c03c35dfd12fc302d6e645938ff20aa253d7433cd3c44.json
@@ -0,0 +1,14 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "UPDATE slack_import_outbox o SET cancelled_at = clock_timestamp()\n FROM slack_import_conversation c WHERE o.job_id = $1 AND c.job_id = o.job_id\n AND c.slack_channel_id = o.slack_channel_id AND o.kind = 'import'\n AND o.published_at IS NULL AND o.cancelled_at IS NULL\n AND (o.generation <> c.event_generation OR c.status NOT IN ('queued', 'importing'))",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "72e877a37af22f14848c03c35dfd12fc302d6e645938ff20aa253d7433cd3c44"
+}
diff --git a/.sqlx/query-7357d61151be99fc37aada71f9a9e5cd7a0552b25d288523b14d955a7552ab8c.json b/.sqlx/query-7357d61151be99fc37aada71f9a9e5cd7a0552b25d288523b14d955a7552ab8c.json
new file mode 100644
index 0000000000..0feff3d898
--- /dev/null
+++ b/.sqlx/query-7357d61151be99fc37aada71f9a9e5cd7a0552b25d288523b14d955a7552ab8c.json
@@ -0,0 +1,14 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "INSERT INTO \"User\" (id, email, macro_user_id) VALUES ('macro|[email protected]', '[email protected]', $1)",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "7357d61151be99fc37aada71f9a9e5cd7a0552b25d288523b14d955a7552ab8c"
+}
diff --git a/.sqlx/query-73997a3c3273379693f7d1e6a3f29a9487e15195e7c92a8d02398ee6179b9bda.json b/.sqlx/query-73997a3c3273379693f7d1e6a3f29a9487e15195e7c92a8d02398ee6179b9bda.json
new file mode 100644
index 0000000000..907c92bdbf
--- /dev/null
+++ b/.sqlx/query-73997a3c3273379693f7d1e6a3f29a9487e15195e7c92a8d02398ee6179b9bda.json
@@ -0,0 +1,14 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "INSERT INTO macro_user (id, username, email, stripe_customer_id) VALUES ($1, 't17', '[email protected]', 't17-customer')",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "73997a3c3273379693f7d1e6a3f29a9487e15195e7c92a8d02398ee6179b9bda"
+}
diff --git a/.sqlx/query-739a6cd1ecd514293464056b25aef8c6724a1d3843aa40bc5e8bc182b9cdf34e.json b/.sqlx/query-739a6cd1ecd514293464056b25aef8c6724a1d3843aa40bc5e8bc182b9cdf34e.json
new file mode 100644
index 0000000000..dcf27198f7
--- /dev/null
+++ b/.sqlx/query-739a6cd1ecd514293464056b25aef8c6724a1d3843aa40bc5e8bc182b9cdf34e.json
@@ -0,0 +1,14 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "UPDATE slack_import_job SET registration_closed_at = coalesce(registration_closed_at, clock_timestamp()),\n finalized_at = coalesce(finalized_at, clock_timestamp()) WHERE id = $1",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "739a6cd1ecd514293464056b25aef8c6724a1d3843aa40bc5e8bc182b9cdf34e"
+}
diff --git a/.sqlx/query-73f8cc44692b9ea9d2baa6b6b23f368bedd89527b6d43adb53e10480702e878a.json b/.sqlx/query-73f8cc44692b9ea9d2baa6b6b23f368bedd89527b6d43adb53e10480702e878a.json
new file mode 100644
index 0000000000..06e540eb12
--- /dev/null
+++ b/.sqlx/query-73f8cc44692b9ea9d2baa6b6b23f368bedd89527b6d43adb53e10480702e878a.json
@@ -0,0 +1,22 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT j.id FROM slack_import_job j WHERE j.status IN ('uploading', 'processing', 'cancelling')\n AND (j.staging_expires_at <= clock_timestamp() + interval '1 day'\n OR EXISTS (SELECT 1 FROM slack_import_conversation c WHERE c.job_id = j.id\n AND c.status = 'importing' AND c.lease_expires_at <= clock_timestamp()))\n ORDER BY j.staging_expires_at, j.id LIMIT $1",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "id",
+ "type_info": "Uuid"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Int8"
+ ]
+ },
+ "nullable": [
+ false
+ ]
+ },
+ "hash": "73f8cc44692b9ea9d2baa6b6b23f368bedd89527b6d43adb53e10480702e878a"
+}
diff --git a/.sqlx/query-748de90309e774e622be549fad11792f5c722e8a6022ece1656534860a384b06.json b/.sqlx/query-748de90309e774e622be549fad11792f5c722e8a6022ece1656534860a384b06.json
new file mode 100644
index 0000000000..3eaa986b0e
--- /dev/null
+++ b/.sqlx/query-748de90309e774e622be549fad11792f5c722e8a6022ece1656534860a384b06.json
@@ -0,0 +1,15 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "UPDATE slack_import_job SET registration_closed_at = clock_timestamp(),\n finalized_at = clock_timestamp() WHERE team_id = $1 AND id = $2\n AND registration_closed_at IS NULL",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "748de90309e774e622be549fad11792f5c722e8a6022ece1656534860a384b06"
+}
diff --git a/.sqlx/query-7507caf42d9e35d16a767c916a1eb6e4486a45a585078aa7570a20e0c816bc2d.json b/.sqlx/query-7507caf42d9e35d16a767c916a1eb6e4486a45a585078aa7570a20e0c816bc2d.json
new file mode 100644
index 0000000000..5ad9286b34
--- /dev/null
+++ b/.sqlx/query-7507caf42d9e35d16a767c916a1eb6e4486a45a585078aa7570a20e0c816bc2d.json
@@ -0,0 +1,20 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT message_id FROM slack_import_message_reference",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "message_id",
+ "type_info": "Uuid"
+ }
+ ],
+ "parameters": {
+ "Left": []
+ },
+ "nullable": [
+ false
+ ]
+ },
+ "hash": "7507caf42d9e35d16a767c916a1eb6e4486a45a585078aa7570a20e0c816bc2d"
+}
diff --git a/.sqlx/query-7550baf37fc11d382c34ab38427a290bbc9719715ac95a86e49a9abc6e77f5fb.json b/.sqlx/query-7550baf37fc11d382c34ab38427a290bbc9719715ac95a86e49a9abc6e77f5fb.json
new file mode 100644
index 0000000000..677c2281ea
--- /dev/null
+++ b/.sqlx/query-7550baf37fc11d382c34ab38427a290bbc9719715ac95a86e49a9abc6e77f5fb.json
@@ -0,0 +1,14 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "INSERT INTO comms_channels (id, name, channel_type, owner_id) VALUES ($1, 'historical', 'private', 'macro|[email protected]')",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "7550baf37fc11d382c34ab38427a290bbc9719715ac95a86e49a9abc6e77f5fb"
+}
diff --git a/.sqlx/query-7679b2e926cd4130da8993cfd3984c6ed07268f5779072d63c116cc80ec4defb.json b/.sqlx/query-7679b2e926cd4130da8993cfd3984c6ed07268f5779072d63c116cc80ec4defb.json
new file mode 100644
index 0000000000..e71eed77db
--- /dev/null
+++ b/.sqlx/query-7679b2e926cd4130da8993cfd3984c6ed07268f5779072d63c116cc80ec4defb.json
@@ -0,0 +1,17 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "INSERT INTO comms_messages (id, channel_id, sender_id, content) VALUES ($1, $2, $3, $4)",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Uuid",
+ "Text",
+ "Text"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "7679b2e926cd4130da8993cfd3984c6ed07268f5779072d63c116cc80ec4defb"
+}
diff --git a/.sqlx/query-7741ba766649c26e9de4e64e8a67d616fb9d468dd78e1f6faeb6d9353cabe091.json b/.sqlx/query-7741ba766649c26e9de4e64e8a67d616fb9d468dd78e1f6faeb6d9353cabe091.json
new file mode 100644
index 0000000000..7a51c23dc4
--- /dev/null
+++ b/.sqlx/query-7741ba766649c26e9de4e64e8a67d616fb9d468dd78e1f6faeb6d9353cabe091.json
@@ -0,0 +1,30 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n INSERT INTO import_source_binding (team_id, slack_workspace_id, confirmed_unknown_at)\n VALUES ($1, $2, CASE WHEN $2::text IS NULL AND $3 THEN now() END)\n ON CONFLICT (team_id) DO UPDATE\n SET slack_workspace_id = COALESCE(import_source_binding.slack_workspace_id, EXCLUDED.slack_workspace_id),\n confirmed_unknown_at = COALESCE(import_source_binding.confirmed_unknown_at, EXCLUDED.confirmed_unknown_at)\n WHERE import_source_binding.slack_workspace_id IS NULL\n OR EXCLUDED.slack_workspace_id IS NULL\n OR import_source_binding.slack_workspace_id = EXCLUDED.slack_workspace_id\n RETURNING slack_workspace_id, confirmed_unknown_at\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "slack_workspace_id",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 1,
+ "name": "confirmed_unknown_at",
+ "type_info": "Timestamptz"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Text",
+ "Bool"
+ ]
+ },
+ "nullable": [
+ true,
+ true
+ ]
+ },
+ "hash": "7741ba766649c26e9de4e64e8a67d616fb9d468dd78e1f6faeb6d9353cabe091"
+}
diff --git a/.sqlx/query-7b30b8c97e900134cf9cd5cae49f29f36453665d8ed1bc029ee62ebd2814a8f4.json b/.sqlx/query-7b30b8c97e900134cf9cd5cae49f29f36453665d8ed1bc029ee62ebd2814a8f4.json
new file mode 100644
index 0000000000..5f7cd5a4d3
--- /dev/null
+++ b/.sqlx/query-7b30b8c97e900134cf9cd5cae49f29f36453665d8ed1bc029ee62ebd2814a8f4.json
@@ -0,0 +1,17 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "UPDATE slack_import_conversation SET status = $3, last_error = $4,\n lease_owner = NULL, lease_token = NULL, lease_expires_at = NULL, heartbeat_at = NULL,\n settled_at = clock_timestamp(), updated_at = clock_timestamp()\n WHERE job_id = $1 AND slack_channel_id = $2",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Text",
+ "Text",
+ "Text"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "7b30b8c97e900134cf9cd5cae49f29f36453665d8ed1bc029ee62ebd2814a8f4"
+}
diff --git a/.sqlx/query-7c4a3f3a694d4005d372a04d2b7f529eed8c3be87deff2bd76aef749db8b7fcf.json b/.sqlx/query-7c4a3f3a694d4005d372a04d2b7f529eed8c3be87deff2bd76aef749db8b7fcf.json
new file mode 100644
index 0000000000..2340b4856e
--- /dev/null
+++ b/.sqlx/query-7c4a3f3a694d4005d372a04d2b7f529eed8c3be87deff2bd76aef749db8b7fcf.json
@@ -0,0 +1,20 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT id FROM comms_messages ORDER BY id",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "id",
+ "type_info": "Uuid"
+ }
+ ],
+ "parameters": {
+ "Left": []
+ },
+ "nullable": [
+ false
+ ]
+ },
+ "hash": "7c4a3f3a694d4005d372a04d2b7f529eed8c3be87deff2bd76aef749db8b7fcf"
+}
diff --git a/.sqlx/query-7c70af198faef5fdb3bcf374164b855261cb6d68ab105ea540dcda3d19760d9c.json b/.sqlx/query-7c70af198faef5fdb3bcf374164b855261cb6d68ab105ea540dcda3d19760d9c.json
new file mode 100644
index 0000000000..577257f5d4
--- /dev/null
+++ b/.sqlx/query-7c70af198faef5fdb3bcf374164b855261cb6d68ab105ea540dcda3d19760d9c.json
@@ -0,0 +1,14 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "INSERT INTO team (id, name, owner_id) VALUES ($1, 'Atomic history', 'macro|[email protected]')",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "7c70af198faef5fdb3bcf374164b855261cb6d68ab105ea540dcda3d19760d9c"
+}
diff --git a/.sqlx/query-7d6ddfd9f54a47761fbfc2a6994c52e978af082443e0034e73e1288762b7c868.json b/.sqlx/query-7d6ddfd9f54a47761fbfc2a6994c52e978af082443e0034e73e1288762b7c868.json
new file mode 100644
index 0000000000..88ec0d30f2
--- /dev/null
+++ b/.sqlx/query-7d6ddfd9f54a47761fbfc2a6994c52e978af082443e0034e73e1288762b7c868.json
@@ -0,0 +1,15 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "INSERT INTO comms_channels (id, name, channel_type, owner_id) VALUES ($1, 'History', 'private', $2)",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Text"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "7d6ddfd9f54a47761fbfc2a6994c52e978af082443e0034e73e1288762b7c868"
+}
diff --git a/.sqlx/query-7ed909a3256b9eccf56895bdb8edec3901029a2e72db81b74ebbfc6a626680f2.json b/.sqlx/query-7ed909a3256b9eccf56895bdb8edec3901029a2e72db81b74ebbfc6a626680f2.json
new file mode 100644
index 0000000000..2389bc8624
--- /dev/null
+++ b/.sqlx/query-7ed909a3256b9eccf56895bdb8edec3901029a2e72db81b74ebbfc6a626680f2.json
@@ -0,0 +1,51 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT user_id, role AS \"role: ParticipantRole\", joined_at, left_at\n FROM comms_channel_participants WHERE channel_id = $1 ORDER BY user_id",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "user_id",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 1,
+ "name": "role: ParticipantRole",
+ "type_info": {
+ "Custom": {
+ "name": "comms_participant_role",
+ "kind": {
+ "Enum": [
+ "owner",
+ "admin",
+ "member"
+ ]
+ }
+ }
+ }
+ },
+ {
+ "ordinal": 2,
+ "name": "joined_at",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 3,
+ "name": "left_at",
+ "type_info": "Timestamptz"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ false,
+ false,
+ false,
+ true
+ ]
+ },
+ "hash": "7ed909a3256b9eccf56895bdb8edec3901029a2e72db81b74ebbfc6a626680f2"
+}
diff --git a/.sqlx/query-8529dbdb87b3db2fdc811f033037615068009f9aad3ef766d39bc16d0a0126cb.json b/.sqlx/query-8529dbdb87b3db2fdc811f033037615068009f9aad3ef766d39bc16d0a0126cb.json
new file mode 100644
index 0000000000..c831f0ef47
--- /dev/null
+++ b/.sqlx/query-8529dbdb87b3db2fdc811f033037615068009f9aad3ef766d39bc16d0a0126cb.json
@@ -0,0 +1,14 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "UPDATE comms_messages SET edited_at = now() WHERE id = $1",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "8529dbdb87b3db2fdc811f033037615068009f9aad3ef766d39bc16d0a0126cb"
+}
diff --git a/.sqlx/query-853d49f9fb31c959a73c428da3a80d3d8a4c7f8d71faa164be649e5f4ad86dfe.json b/.sqlx/query-853d49f9fb31c959a73c428da3a80d3d8a4c7f8d71faa164be649e5f4ad86dfe.json
new file mode 100644
index 0000000000..d1d0953e16
--- /dev/null
+++ b/.sqlx/query-853d49f9fb31c959a73c428da3a80d3d8a4c7f8d71faa164be649e5f4ad86dfe.json
@@ -0,0 +1,14 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "UPDATE slack_import_job SET registration_closed_at = now(), cancel_requested_at = now(), status = 'cancelled' WHERE id = $1",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "853d49f9fb31c959a73c428da3a80d3d8a4c7f8d71faa164be649e5f4ad86dfe"
+}
diff --git a/.sqlx/query-8703f902bdb928b819906b89acb6997fede5a88b98f7a9a6c55a5019295c1a96.json b/.sqlx/query-8703f902bdb928b819906b89acb6997fede5a88b98f7a9a6c55a5019295c1a96.json
new file mode 100644
index 0000000000..a3f2c35f6e
--- /dev/null
+++ b/.sqlx/query-8703f902bdb928b819906b89acb6997fede5a88b98f7a9a6c55a5019295c1a96.json
@@ -0,0 +1,14 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "UPDATE slack_import_conversation SET status = 'skipped',\n warnings = array_append(warnings, 'uploads_incomplete'),\n settled_at = clock_timestamp(), updated_at = clock_timestamp()\n WHERE job_id = $1 AND status = 'awaiting_uploads'",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "8703f902bdb928b819906b89acb6997fede5a88b98f7a9a6c55a5019295c1a96"
+}
diff --git a/.sqlx/query-87e0ab7d9cb677457aacd502ea09d9c19e36b4b3e610d96c1afced87e3537226.json b/.sqlx/query-87e0ab7d9cb677457aacd502ea09d9c19e36b4b3e610d96c1afced87e3537226.json
new file mode 100644
index 0000000000..ef67dab2aa
--- /dev/null
+++ b/.sqlx/query-87e0ab7d9cb677457aacd502ea09d9c19e36b4b3e610d96c1afced87e3537226.json
@@ -0,0 +1,14 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "UPDATE comms_channels SET auto_join_team = true WHERE id = $1",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "87e0ab7d9cb677457aacd502ea09d9c19e36b4b3e610d96c1afced87e3537226"
+}
diff --git a/.sqlx/query-899e8bb47b467043816a2d2f6d7db2db6e78a77e636f32e5652a43bb0a824651.json b/.sqlx/query-899e8bb47b467043816a2d2f6d7db2db6e78a77e636f32e5652a43bb0a824651.json
new file mode 100644
index 0000000000..44a6df4df9
--- /dev/null
+++ b/.sqlx/query-899e8bb47b467043816a2d2f6d7db2db6e78a77e636f32e5652a43bb0a824651.json
@@ -0,0 +1,30 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "UPDATE slack_import_conversation SET heartbeat_at = clock_timestamp(),\n lease_expires_at = clock_timestamp() + make_interval(secs => $3)\n WHERE job_id = $1 AND slack_channel_id = $2 RETURNING heartbeat_at, lease_expires_at",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "heartbeat_at",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 1,
+ "name": "lease_expires_at",
+ "type_info": "Timestamptz"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Text",
+ "Float8"
+ ]
+ },
+ "nullable": [
+ true,
+ true
+ ]
+ },
+ "hash": "899e8bb47b467043816a2d2f6d7db2db6e78a77e636f32e5652a43bb0a824651"
+}
diff --git a/.sqlx/query-8b396eaf27c45aa0d66fc504b9a2b49c85a842dc1ac6002531a7017465818a6a.json b/.sqlx/query-8b396eaf27c45aa0d66fc504b9a2b49c85a842dc1ac6002531a7017465818a6a.json
new file mode 100644
index 0000000000..c504b13e4f
--- /dev/null
+++ b/.sqlx/query-8b396eaf27c45aa0d66fc504b9a2b49c85a842dc1ac6002531a7017465818a6a.json
@@ -0,0 +1,22 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT count(*) FROM slack_import_outbox WHERE job_id = $1 AND kind = 'import' AND published_at IS NULL",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "count",
+ "type_info": "Int8"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ null
+ ]
+ },
+ "hash": "8b396eaf27c45aa0d66fc504b9a2b49c85a842dc1ac6002531a7017465818a6a"
+}
diff --git a/.sqlx/query-8c4c0691b05fc128568678813e4d02f08dba64fd908363763f46360f09470bb4.json b/.sqlx/query-8c4c0691b05fc128568678813e4d02f08dba64fd908363763f46360f09470bb4.json
new file mode 100644
index 0000000000..60ed80862e
--- /dev/null
+++ b/.sqlx/query-8c4c0691b05fc128568678813e4d02f08dba64fd908363763f46360f09470bb4.json
@@ -0,0 +1,18 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "INSERT INTO comms_entity_mentions\n (id, source_entity_type, source_entity_id, entity_type, entity_id, user_id, created_at)\n SELECT input.id, 'message', input.message_id, 'user', input.user_id,\n input.sender_id, input.created_at\n FROM UNNEST($1::uuid[], $2::text[], $3::text[], $4::text[], $5::timestamptz[])\n AS input(id, message_id, user_id, sender_id, created_at)\n ON CONFLICT (id) DO NOTHING",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "UuidArray",
+ "TextArray",
+ "TextArray",
+ "TextArray",
+ "TimestamptzArray"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "8c4c0691b05fc128568678813e4d02f08dba64fd908363763f46360f09470bb4"
+}
diff --git a/.sqlx/query-8c867b211e545a855485b1b1e13f90d54835695ff7ba90f2400f920d50ad5caf.json b/.sqlx/query-8c867b211e545a855485b1b1e13f90d54835695ff7ba90f2400f920d50ad5caf.json
new file mode 100644
index 0000000000..591a002331
--- /dev/null
+++ b/.sqlx/query-8c867b211e545a855485b1b1e13f90d54835695ff7ba90f2400f920d50ad5caf.json
@@ -0,0 +1,104 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "UPDATE slack_import_conversation SET status = 'importing', lease_owner = $3,\n lease_token = $4, lease_generation = lease_generation + 1, attempts = attempts + 1,\n lease_expires_at = clock_timestamp() + make_interval(secs => $5),\n heartbeat_at = clock_timestamp(), updated_at = clock_timestamp()\n WHERE job_id = $1 AND slack_channel_id = $2\n RETURNING lease_generation, attempts, lease_expires_at, heartbeat_at,\n kind, name, folder, member_ids, creator_id, source_created_at, archived, message_count,\n checkpoint_part, checkpoint_record",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "lease_generation",
+ "type_info": "Int8"
+ },
+ {
+ "ordinal": 1,
+ "name": "attempts",
+ "type_info": "Int4"
+ },
+ {
+ "ordinal": 2,
+ "name": "lease_expires_at",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 3,
+ "name": "heartbeat_at",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 4,
+ "name": "kind",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 5,
+ "name": "name",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 6,
+ "name": "folder",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 7,
+ "name": "member_ids",
+ "type_info": "TextArray"
+ },
+ {
+ "ordinal": 8,
+ "name": "creator_id",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 9,
+ "name": "source_created_at",
+ "type_info": "Int8"
+ },
+ {
+ "ordinal": 10,
+ "name": "archived",
+ "type_info": "Bool"
+ },
+ {
+ "ordinal": 11,
+ "name": "message_count",
+ "type_info": "Int8"
+ },
+ {
+ "ordinal": 12,
+ "name": "checkpoint_part",
+ "type_info": "Int4"
+ },
+ {
+ "ordinal": 13,
+ "name": "checkpoint_record",
+ "type_info": "Int4"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Text",
+ "Uuid",
+ "Uuid",
+ "Float8"
+ ]
+ },
+ "nullable": [
+ false,
+ false,
+ true,
+ true,
+ false,
+ false,
+ false,
+ false,
+ true,
+ true,
+ false,
+ true,
+ false,
+ false
+ ]
+ },
+ "hash": "8c867b211e545a855485b1b1e13f90d54835695ff7ba90f2400f920d50ad5caf"
+}
diff --git a/.sqlx/query-8d1a3e662ff1757a905271ed857c72de32d49a9bf348634ee1c8d9afca21359b.json b/.sqlx/query-8d1a3e662ff1757a905271ed857c72de32d49a9bf348634ee1c8d9afca21359b.json
new file mode 100644
index 0000000000..578cb5b7c3
--- /dev/null
+++ b/.sqlx/query-8d1a3e662ff1757a905271ed857c72de32d49a9bf348634ee1c8d9afca21359b.json
@@ -0,0 +1,14 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "UPDATE comms_messages SET deleted_at = now() WHERE id = $1",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "8d1a3e662ff1757a905271ed857c72de32d49a9bf348634ee1c8d9afca21359b"
+}
diff --git a/.sqlx/query-8e8027056ea54b5a3279a1f41673c5aa108a05cc405c9639c6f6bdb54b3a7be2.json b/.sqlx/query-8e8027056ea54b5a3279a1f41673c5aa108a05cc405c9639c6f6bdb54b3a7be2.json
new file mode 100644
index 0000000000..2deca21ae8
--- /dev/null
+++ b/.sqlx/query-8e8027056ea54b5a3279a1f41673c5aa108a05cc405c9639c6f6bdb54b3a7be2.json
@@ -0,0 +1,20 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT count(*) FROM slack_import_message_reference WHERE completed_at IS NOT NULL",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "count",
+ "type_info": "Int8"
+ }
+ ],
+ "parameters": {
+ "Left": []
+ },
+ "nullable": [
+ null
+ ]
+ },
+ "hash": "8e8027056ea54b5a3279a1f41673c5aa108a05cc405c9639c6f6bdb54b3a7be2"
+}
diff --git a/.sqlx/query-8f7951cdcbe053bb5a863104f0900418980ba2ff65799d433eec2f2e3365b296.json b/.sqlx/query-8f7951cdcbe053bb5a863104f0900418980ba2ff65799d433eec2f2e3365b296.json
new file mode 100644
index 0000000000..81fd176b83
--- /dev/null
+++ b/.sqlx/query-8f7951cdcbe053bb5a863104f0900418980ba2ff65799d433eec2f2e3365b296.json
@@ -0,0 +1,14 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "UPDATE comms_message_threads SET deleted_at = now() WHERE root_id = $1",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "8f7951cdcbe053bb5a863104f0900418980ba2ff65799d433eec2f2e3365b296"
+}
diff --git a/.sqlx/query-8f888c31aeffdc31e0e51b4ed789a256b69c88516c0a9848821a39b155333f08.json b/.sqlx/query-8f888c31aeffdc31e0e51b4ed789a256b69c88516c0a9848821a39b155333f08.json
new file mode 100644
index 0000000000..fc100740ca
--- /dev/null
+++ b/.sqlx/query-8f888c31aeffdc31e0e51b4ed789a256b69c88516c0a9848821a39b155333f08.json
@@ -0,0 +1,14 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "UPDATE slack_import_conversation SET status = 'skipped', warnings = array_append(warnings, 'uploads_incomplete'),\n settled_at = clock_timestamp(), updated_at = clock_timestamp()\n WHERE job_id = $1 AND status = 'awaiting_uploads'",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "8f888c31aeffdc31e0e51b4ed789a256b69c88516c0a9848821a39b155333f08"
+}
diff --git a/.sqlx/query-9189ac92c1adf89c7d2f0864cda61e8ad81da4e58c1a49429627b155a8709889.json b/.sqlx/query-9189ac92c1adf89c7d2f0864cda61e8ad81da4e58c1a49429627b155a8709889.json
new file mode 100644
index 0000000000..b5a387359e
--- /dev/null
+++ b/.sqlx/query-9189ac92c1adf89c7d2f0864cda61e8ad81da4e58c1a49429627b155a8709889.json
@@ -0,0 +1,23 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT user_id FROM slack_import_job WHERE team_id = $1 AND id = $2",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "user_id",
+ "type_info": "Text"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ false
+ ]
+ },
+ "hash": "9189ac92c1adf89c7d2f0864cda61e8ad81da4e58c1a49429627b155a8709889"
+}
diff --git a/.sqlx/query-91ce7a400ca4decbbb562dfe4091777a5e21d4696d09dd5ebd5ee5c3e880714d.json b/.sqlx/query-91ce7a400ca4decbbb562dfe4091777a5e21d4696d09dd5ebd5ee5c3e880714d.json
new file mode 100644
index 0000000000..81d3b32108
--- /dev/null
+++ b/.sqlx/query-91ce7a400ca4decbbb562dfe4091777a5e21d4696d09dd5ebd5ee5c3e880714d.json
@@ -0,0 +1,16 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "INSERT INTO macro_user (id, username, email, stripe_customer_id) VALUES ($1, $2, $3, $2)",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Text",
+ "Text"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "91ce7a400ca4decbbb562dfe4091777a5e21d4696d09dd5ebd5ee5c3e880714d"
+}
diff --git a/.sqlx/query-9241b1410316592468e6557e3fddbecd0e549214bb3027e9c8596510b2aa5c9e.json b/.sqlx/query-9241b1410316592468e6557e3fddbecd0e549214bb3027e9c8596510b2aa5c9e.json
new file mode 100644
index 0000000000..6b399923c5
--- /dev/null
+++ b/.sqlx/query-9241b1410316592468e6557e3fddbecd0e549214bb3027e9c8596510b2aa5c9e.json
@@ -0,0 +1,20 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT count(*) FROM import_target_reservation",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "count",
+ "type_info": "Int8"
+ }
+ ],
+ "parameters": {
+ "Left": []
+ },
+ "nullable": [
+ null
+ ]
+ },
+ "hash": "9241b1410316592468e6557e3fddbecd0e549214bb3027e9c8596510b2aa5c9e"
+}
diff --git a/.sqlx/query-944d28ee5d144db813a9da046b83c9f945fa0a0d04e1df1bc111eb3b31365ed4.json b/.sqlx/query-944d28ee5d144db813a9da046b83c9f945fa0a0d04e1df1bc111eb3b31365ed4.json
new file mode 100644
index 0000000000..901dba2b11
--- /dev/null
+++ b/.sqlx/query-944d28ee5d144db813a9da046b83c9f945fa0a0d04e1df1bc111eb3b31365ed4.json
@@ -0,0 +1,22 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT content FROM comms_messages WHERE id = $1",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "content",
+ "type_info": "Text"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ false
+ ]
+ },
+ "hash": "944d28ee5d144db813a9da046b83c9f945fa0a0d04e1df1bc111eb3b31365ed4"
+}
diff --git a/.sqlx/query-94aaff529db9654ef6a09e33144ec3d51dc6276176910e607dd613eb1b54fc2a.json b/.sqlx/query-94aaff529db9654ef6a09e33144ec3d51dc6276176910e607dd613eb1b54fc2a.json
new file mode 100644
index 0000000000..906b3b1d0d
--- /dev/null
+++ b/.sqlx/query-94aaff529db9654ef6a09e33144ec3d51dc6276176910e607dd613eb1b54fc2a.json
@@ -0,0 +1,21 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "INSERT INTO slack_import_upload\n (job_id, slack_channel_id, part_index, object_key, sha256, byte_length, record_count)\n SELECT id, $3, $4, $5, $6, $7, $8 FROM slack_import_job\n WHERE team_id = $1 AND id = $2\n ON CONFLICT (job_id, slack_channel_id, part_index) DO NOTHING",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Uuid",
+ "Text",
+ "Int4",
+ "Text",
+ "Text",
+ "Int8",
+ "Int4"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "94aaff529db9654ef6a09e33144ec3d51dc6276176910e607dd613eb1b54fc2a"
+}
diff --git a/.sqlx/query-97faa0a85a5dff3830e9be6c9ac139c88da8ec033327158d502b71ea2823824a.json b/.sqlx/query-97faa0a85a5dff3830e9be6c9ac139c88da8ec033327158d502b71ea2823824a.json
new file mode 100644
index 0000000000..7f6d66a1c9
--- /dev/null
+++ b/.sqlx/query-97faa0a85a5dff3830e9be6c9ac139c88da8ec033327158d502b71ea2823824a.json
@@ -0,0 +1,15 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "UPDATE comms_channels SET created_at = $2 WHERE id = $1",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Timestamptz"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "97faa0a85a5dff3830e9be6c9ac139c88da8ec033327158d502b71ea2823824a"
+}
diff --git a/.sqlx/query-989f907a6a02ec7ee25af05a32a570c14d069b0b2b5cddcdf0c203a7d6e24204.json b/.sqlx/query-989f907a6a02ec7ee25af05a32a570c14d069b0b2b5cddcdf0c203a7d6e24204.json
new file mode 100644
index 0000000000..7fce1f0e9f
--- /dev/null
+++ b/.sqlx/query-989f907a6a02ec7ee25af05a32a570c14d069b0b2b5cddcdf0c203a7d6e24204.json
@@ -0,0 +1,15 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "UPDATE comms_channels SET updated_at = GREATEST(updated_at, $2) WHERE id = $1",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Timestamptz"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "989f907a6a02ec7ee25af05a32a570c14d069b0b2b5cddcdf0c203a7d6e24204"
+}
diff --git a/.sqlx/query-98e9071cd2057f95b954cd0651f219789b90bf920fa7c38da5fc0a6c12edd58d.json b/.sqlx/query-98e9071cd2057f95b954cd0651f219789b90bf920fa7c38da5fc0a6c12edd58d.json
new file mode 100644
index 0000000000..8f87370cbd
--- /dev/null
+++ b/.sqlx/query-98e9071cd2057f95b954cd0651f219789b90bf920fa7c38da5fc0a6c12edd58d.json
@@ -0,0 +1,22 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT registered_bytes FROM slack_import_job WHERE id = $1",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "registered_bytes",
+ "type_info": "Int8"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ false
+ ]
+ },
+ "hash": "98e9071cd2057f95b954cd0651f219789b90bf920fa7c38da5fc0a6c12edd58d"
+}
diff --git a/.sqlx/query-99eebd2c649e7f1092f0ec2aba294427a832efac94a9a493569196707906aeed.json b/.sqlx/query-99eebd2c649e7f1092f0ec2aba294427a832efac94a9a493569196707906aeed.json
new file mode 100644
index 0000000000..6be4a16469
--- /dev/null
+++ b/.sqlx/query-99eebd2c649e7f1092f0ec2aba294427a832efac94a9a493569196707906aeed.json
@@ -0,0 +1,30 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT status, lease_expires_at > clock_timestamp() AS active FROM slack_import_conversation\n WHERE job_id = $1 AND slack_channel_id = $2 AND event_generation = $3",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "status",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 1,
+ "name": "active",
+ "type_info": "Bool"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Text",
+ "Int8"
+ ]
+ },
+ "nullable": [
+ false,
+ null
+ ]
+ },
+ "hash": "99eebd2c649e7f1092f0ec2aba294427a832efac94a9a493569196707906aeed"
+}
diff --git a/.sqlx/query-9a8884281232e860f1af27ce98cd5e3678c61bcbcc5e6df7640c80982719cd9b.json b/.sqlx/query-9a8884281232e860f1af27ce98cd5e3678c61bcbcc5e6df7640c80982719cd9b.json
new file mode 100644
index 0000000000..224124fad9
--- /dev/null
+++ b/.sqlx/query-9a8884281232e860f1af27ce98cd5e3678c61bcbcc5e6df7640c80982719cd9b.json
@@ -0,0 +1,14 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "UPDATE slack_import_job SET staging_expires_at = clock_timestamp() + interval '12 hours' WHERE id = $1",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "9a8884281232e860f1af27ce98cd5e3678c61bcbcc5e6df7640c80982719cd9b"
+}
diff --git a/.sqlx/query-9adbcd4e580d7125e78d05c6ccafd3cd3510542c513207b573ac44e7a4af1e74.json b/.sqlx/query-9adbcd4e580d7125e78d05c6ccafd3cd3510542c513207b573ac44e7a4af1e74.json
new file mode 100644
index 0000000000..89d0f70ab4
--- /dev/null
+++ b/.sqlx/query-9adbcd4e580d7125e78d05c6ccafd3cd3510542c513207b573ac44e7a4af1e74.json
@@ -0,0 +1,29 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT role::text AS \"role!\", left_at FROM comms_channel_participants WHERE channel_id = $1 AND user_id = $2",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "role!",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 1,
+ "name": "left_at",
+ "type_info": "Timestamptz"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Text"
+ ]
+ },
+ "nullable": [
+ null,
+ true
+ ]
+ },
+ "hash": "9adbcd4e580d7125e78d05c6ccafd3cd3510542c513207b573ac44e7a4af1e74"
+}
diff --git a/.sqlx/query-9adc8edbe71364b37068c05e4a4f5f955f31fedb9d900dbcef143e270e375757.json b/.sqlx/query-9adc8edbe71364b37068c05e4a4f5f955f31fedb9d900dbcef143e270e375757.json
new file mode 100644
index 0000000000..b3b94bf403
--- /dev/null
+++ b/.sqlx/query-9adc8edbe71364b37068c05e4a4f5f955f31fedb9d900dbcef143e270e375757.json
@@ -0,0 +1,20 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT content FROM comms_messages",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "content",
+ "type_info": "Text"
+ }
+ ],
+ "parameters": {
+ "Left": []
+ },
+ "nullable": [
+ false
+ ]
+ },
+ "hash": "9adc8edbe71364b37068c05e4a4f5f955f31fedb9d900dbcef143e270e375757"
+}
diff --git a/.sqlx/query-9b8c52bba7cb857f99dcd345a787713bc221e0f2dbdded93f1146294431db3dd.json b/.sqlx/query-9b8c52bba7cb857f99dcd345a787713bc221e0f2dbdded93f1146294431db3dd.json
new file mode 100644
index 0000000000..7a1e33e871
--- /dev/null
+++ b/.sqlx/query-9b8c52bba7cb857f99dcd345a787713bc221e0f2dbdded93f1146294431db3dd.json
@@ -0,0 +1,15 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "INSERT INTO team (id, name, owner_id) VALUES ($1, 'Ledger test', $2)",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Text"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "9b8c52bba7cb857f99dcd345a787713bc221e0f2dbdded93f1146294431db3dd"
+}
diff --git a/.sqlx/query-9ba814ffded0974c86808d9512d7ad789e20afd71ee38125b1555aba651bbdda.json b/.sqlx/query-9ba814ffded0974c86808d9512d7ad789e20afd71ee38125b1555aba651bbdda.json
new file mode 100644
index 0000000000..804add3ba5
--- /dev/null
+++ b/.sqlx/query-9ba814ffded0974c86808d9512d7ad789e20afd71ee38125b1555aba651bbdda.json
@@ -0,0 +1,14 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "INSERT INTO team_user (team_id, user_id, team_role) VALUES ($1, 'macro|[email protected]', 'owner')",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "9ba814ffded0974c86808d9512d7ad789e20afd71ee38125b1555aba651bbdda"
+}
diff --git a/.sqlx/query-a221a12748715fda53d1ec99282d1a7a0596caf3efbee6ee84c8d749cc9cf8eb.json b/.sqlx/query-a221a12748715fda53d1ec99282d1a7a0596caf3efbee6ee84c8d749cc9cf8eb.json
new file mode 100644
index 0000000000..0c722124e6
--- /dev/null
+++ b/.sqlx/query-a221a12748715fda53d1ec99282d1a7a0596caf3efbee6ee84c8d749cc9cf8eb.json
@@ -0,0 +1,28 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "UPDATE slack_import_conversation SET search_state = $4, search_receipt_id = $5,\n search_submitted_generation = CASE WHEN $5::uuid IS NOT NULL THEN $3 ELSE search_submitted_generation END,\n search_attempts = search_attempts + CASE WHEN $4 = 'submitted' THEN 1 ELSE 0 END,\n search_updated_at = clock_timestamp(), updated_at = clock_timestamp()\n WHERE job_id = $1 AND channel_id = ANY($2) AND search_dirty_generation = $3\n AND search_state <> 'completed' AND search_receipt_id IS NOT DISTINCT FROM $6\n AND search_updated_at = $7\n RETURNING slack_channel_id",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "slack_channel_id",
+ "type_info": "Text"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "UuidArray",
+ "Int8",
+ "Text",
+ "Uuid",
+ "Uuid",
+ "Timestamptz"
+ ]
+ },
+ "nullable": [
+ false
+ ]
+ },
+ "hash": "a221a12748715fda53d1ec99282d1a7a0596caf3efbee6ee84c8d749cc9cf8eb"
+}
diff --git a/.sqlx/query-a4163b581b520ec932e6103ef6f4a0840b407a89b750fe08f529016103c7b5c8.json b/.sqlx/query-a4163b581b520ec932e6103ef6f4a0840b407a89b750fe08f529016103c7b5c8.json
new file mode 100644
index 0000000000..306fd24e57
--- /dev/null
+++ b/.sqlx/query-a4163b581b520ec932e6103ef6f4a0840b407a89b750fe08f529016103c7b5c8.json
@@ -0,0 +1,28 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT slack_workspace_id, confirmed_unknown_at FROM import_source_binding WHERE team_id = $1",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "slack_workspace_id",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 1,
+ "name": "confirmed_unknown_at",
+ "type_info": "Timestamptz"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ true,
+ true
+ ]
+ },
+ "hash": "a4163b581b520ec932e6103ef6f4a0840b407a89b750fe08f529016103c7b5c8"
+}
diff --git a/.sqlx/query-a4dbf23b71bcb8653395683a703a13007db7e68529b9f6f0bb0d3661f9728944.json b/.sqlx/query-a4dbf23b71bcb8653395683a703a13007db7e68529b9f6f0bb0d3661f9728944.json
new file mode 100644
index 0000000000..7cd0bd8178
--- /dev/null
+++ b/.sqlx/query-a4dbf23b71bcb8653395683a703a13007db7e68529b9f6f0bb0d3661f9728944.json
@@ -0,0 +1,20 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT count(*) FROM comms_entity_mentions",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "count",
+ "type_info": "Int8"
+ }
+ ],
+ "parameters": {
+ "Left": []
+ },
+ "nullable": [
+ null
+ ]
+ },
+ "hash": "a4dbf23b71bcb8653395683a703a13007db7e68529b9f6f0bb0d3661f9728944"
+}
diff --git a/.sqlx/query-a583fa61f96ae3bc3cbbdf24de61255130e01c857e9a27b2fcdb1c3eb2a4ac93.json b/.sqlx/query-a583fa61f96ae3bc3cbbdf24de61255130e01c857e9a27b2fcdb1c3eb2a4ac93.json
new file mode 100644
index 0000000000..231562444c
--- /dev/null
+++ b/.sqlx/query-a583fa61f96ae3bc3cbbdf24de61255130e01c857e9a27b2fcdb1c3eb2a4ac93.json
@@ -0,0 +1,14 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "UPDATE slack_import_outbox SET available_at = clock_timestamp() - interval '1 second' WHERE job_id = $1",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "a583fa61f96ae3bc3cbbdf24de61255130e01c857e9a27b2fcdb1c3eb2a4ac93"
+}
diff --git a/.sqlx/query-a5cce4c93b2edc3a1cb95e8abdb428abc2fb2b728da758b273e913ac89764e2b.json b/.sqlx/query-a5cce4c93b2edc3a1cb95e8abdb428abc2fb2b728da758b273e913ac89764e2b.json
new file mode 100644
index 0000000000..11abf5fc92
--- /dev/null
+++ b/.sqlx/query-a5cce4c93b2edc3a1cb95e8abdb428abc2fb2b728da758b273e913ac89764e2b.json
@@ -0,0 +1,14 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "WITH result AS (\n SELECT CASE\n WHEN j.cancel_requested_at IS NOT NULL THEN CASE WHEN EXISTS (\n SELECT 1 FROM slack_import_conversation WHERE job_id = j.id\n AND (status = 'importing' OR search_state IN ('pending', 'submitted', 'failed'))\n ) OR EXISTS (SELECT 1 FROM slack_import_message_reference\n WHERE job_id = j.id AND completed_at IS NULL)\n THEN 'cancelling' ELSE 'cancelled' END\n WHEN j.registration_closed_at IS NULL THEN 'uploading'\n WHEN EXISTS (SELECT 1 FROM slack_import_conversation WHERE job_id = j.id\n AND (status IN ('awaiting_uploads', 'queued', 'importing')\n OR search_state IN ('pending', 'submitted', 'failed'))) THEN 'processing'\n WHEN EXISTS (SELECT 1 FROM slack_import_message_reference\n WHERE job_id = j.id AND completed_at IS NULL) THEN 'processing'\n WHEN NOT EXISTS (SELECT 1 FROM slack_import_conversation WHERE job_id = j.id AND status = 'failed') THEN 'completed'\n WHEN EXISTS (SELECT 1 FROM slack_import_conversation WHERE job_id = j.id AND (status = 'completed' OR imported > 0))\n THEN 'completed_with_errors'\n ELSE 'failed' END AS status\n FROM slack_import_job j WHERE j.id = $1\n ) UPDATE slack_import_job j SET status = r.status, revision = revision + 1, updated_at = clock_timestamp(),\n settled_at = CASE WHEN r.status IN ('completed', 'completed_with_errors', 'failed', 'cancelled')\n THEN coalesce(j.settled_at, clock_timestamp()) ELSE NULL END\n FROM result r WHERE j.id = $1\n AND j.status NOT IN ('completed', 'completed_with_errors', 'failed', 'cancelled')",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "a5cce4c93b2edc3a1cb95e8abdb428abc2fb2b728da758b273e913ac89764e2b"
+}
diff --git a/.sqlx/query-a7fec80ef3e17e04b8d68f34f52955cb6573201d0a11be57ef378f03631fd70b.json b/.sqlx/query-a7fec80ef3e17e04b8d68f34f52955cb6573201d0a11be57ef378f03631fd70b.json
new file mode 100644
index 0000000000..6bf891451f
--- /dev/null
+++ b/.sqlx/query-a7fec80ef3e17e04b8d68f34f52955cb6573201d0a11be57ef378f03631fd70b.json
@@ -0,0 +1,20 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT count(*) FROM comms_attachments",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "count",
+ "type_info": "Int8"
+ }
+ ],
+ "parameters": {
+ "Left": []
+ },
+ "nullable": [
+ null
+ ]
+ },
+ "hash": "a7fec80ef3e17e04b8d68f34f52955cb6573201d0a11be57ef378f03631fd70b"
+}
diff --git a/.sqlx/query-ad1cf332379d3e42f823fd5726066d48837af9ac5ec780853ebe402cbf1d626c.json b/.sqlx/query-ad1cf332379d3e42f823fd5726066d48837af9ac5ec780853ebe402cbf1d626c.json
new file mode 100644
index 0000000000..9c5c44ab4e
--- /dev/null
+++ b/.sqlx/query-ad1cf332379d3e42f823fd5726066d48837af9ac5ec780853ebe402cbf1d626c.json
@@ -0,0 +1,34 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT channel_type AS \"channel_type: ChannelType\"\n FROM comms_channels WHERE id = $1 FOR UPDATE",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "channel_type: ChannelType",
+ "type_info": {
+ "Custom": {
+ "name": "comms_channel_type",
+ "kind": {
+ "Enum": [
+ "public",
+ "private",
+ "direct_message",
+ "team"
+ ]
+ }
+ }
+ }
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ false
+ ]
+ },
+ "hash": "ad1cf332379d3e42f823fd5726066d48837af9ac5ec780853ebe402cbf1d626c"
+}
diff --git a/.sqlx/query-ad96cfb0ad76c6895be87809f9f9b09b9f959b8ded27f73e24b005485b02e80c.json b/.sqlx/query-ad96cfb0ad76c6895be87809f9f9b09b9f959b8ded27f73e24b005485b02e80c.json
new file mode 100644
index 0000000000..4e37b6191e
--- /dev/null
+++ b/.sqlx/query-ad96cfb0ad76c6895be87809f9f9b09b9f959b8ded27f73e24b005485b02e80c.json
@@ -0,0 +1,20 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT count(*) FROM comms_messages",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "count",
+ "type_info": "Int8"
+ }
+ ],
+ "parameters": {
+ "Left": []
+ },
+ "nullable": [
+ null
+ ]
+ },
+ "hash": "ad96cfb0ad76c6895be87809f9f9b09b9f959b8ded27f73e24b005485b02e80c"
+}
diff --git a/.sqlx/query-b149ef3a3e7909790400eadede7f614f254c2132882ba3655d1cdaadf82a0632.json b/.sqlx/query-b149ef3a3e7909790400eadede7f614f254c2132882ba3655d1cdaadf82a0632.json
new file mode 100644
index 0000000000..59d9a18a90
--- /dev/null
+++ b/.sqlx/query-b149ef3a3e7909790400eadede7f614f254c2132882ba3655d1cdaadf82a0632.json
@@ -0,0 +1,61 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "UPDATE slack_import_conversation SET checkpoint_part = $3, checkpoint_record = $4,\n processed = processed + $5, imported = imported + $6, duplicates = duplicates + $7,\n skipped = skipped + $8, reactions = reactions + $9,\n search_dirty_generation = search_dirty_generation + CASE WHEN $10 THEN 1 ELSE 0 END,\n search_state = CASE WHEN $10 THEN 'pending' ELSE search_state END,\n search_receipt_id = CASE WHEN $10 THEN NULL ELSE search_receipt_id END,\n search_submitted_generation = CASE WHEN $10 THEN NULL ELSE search_submitted_generation END,\n search_updated_at = CASE WHEN $10 THEN clock_timestamp() ELSE search_updated_at END,\n updated_at = clock_timestamp()\n WHERE job_id = $1 AND slack_channel_id = $2\n RETURNING processed, imported, duplicates, skipped, reactions, search_dirty_generation",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "processed",
+ "type_info": "Int8"
+ },
+ {
+ "ordinal": 1,
+ "name": "imported",
+ "type_info": "Int8"
+ },
+ {
+ "ordinal": 2,
+ "name": "duplicates",
+ "type_info": "Int8"
+ },
+ {
+ "ordinal": 3,
+ "name": "skipped",
+ "type_info": "Int8"
+ },
+ {
+ "ordinal": 4,
+ "name": "reactions",
+ "type_info": "Int8"
+ },
+ {
+ "ordinal": 5,
+ "name": "search_dirty_generation",
+ "type_info": "Int8"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Text",
+ "Int4",
+ "Int4",
+ "Int8",
+ "Int8",
+ "Int8",
+ "Int8",
+ "Int8",
+ "Bool"
+ ]
+ },
+ "nullable": [
+ false,
+ false,
+ false,
+ false,
+ false,
+ false
+ ]
+ },
+ "hash": "b149ef3a3e7909790400eadede7f614f254c2132882ba3655d1cdaadf82a0632"
+}
diff --git a/.sqlx/query-b15b170259a92429a064a36320aaeb3055571b627dd0bf3192e1213f03b6a4c1.json b/.sqlx/query-b15b170259a92429a064a36320aaeb3055571b627dd0bf3192e1213f03b6a4c1.json
new file mode 100644
index 0000000000..117cba5bbf
--- /dev/null
+++ b/.sqlx/query-b15b170259a92429a064a36320aaeb3055571b627dd0bf3192e1213f03b6a4c1.json
@@ -0,0 +1,16 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "INSERT INTO comms_channel_participants (channel_id, user_id, role, joined_at)\n SELECT $1, user_id, 'member'::comms_participant_role, $3 FROM UNNEST($2::text[]) AS user_id\n ON CONFLICT (channel_id, user_id) DO NOTHING",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "TextArray",
+ "Timestamptz"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "b15b170259a92429a064a36320aaeb3055571b627dd0bf3192e1213f03b6a4c1"
+}
diff --git a/.sqlx/query-b2f7930c5411b79a83e97828ede15ee9fe20c78cc5c55f5dd253fd82b5a2f15f.json b/.sqlx/query-b2f7930c5411b79a83e97828ede15ee9fe20c78cc5c55f5dd253fd82b5a2f15f.json
new file mode 100644
index 0000000000..8207b30247
--- /dev/null
+++ b/.sqlx/query-b2f7930c5411b79a83e97828ede15ee9fe20c78cc5c55f5dd253fd82b5a2f15f.json
@@ -0,0 +1,65 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT slack_channel_id, part_index, object_key, sha256, byte_length, record_count,\n verified_version_id, verified_etag FROM slack_import_upload\n WHERE job_id = $1 AND (slack_channel_id IS NULL OR slack_channel_id = $2)\n AND verified_at IS NOT NULL ORDER BY part_index NULLS FIRST",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "slack_channel_id",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 1,
+ "name": "part_index",
+ "type_info": "Int4"
+ },
+ {
+ "ordinal": 2,
+ "name": "object_key",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 3,
+ "name": "sha256",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 4,
+ "name": "byte_length",
+ "type_info": "Int8"
+ },
+ {
+ "ordinal": 5,
+ "name": "record_count",
+ "type_info": "Int4"
+ },
+ {
+ "ordinal": 6,
+ "name": "verified_version_id",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 7,
+ "name": "verified_etag",
+ "type_info": "Text"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Text"
+ ]
+ },
+ "nullable": [
+ true,
+ true,
+ false,
+ false,
+ false,
+ true,
+ true,
+ true
+ ]
+ },
+ "hash": "b2f7930c5411b79a83e97828ede15ee9fe20c78cc5c55f5dd253fd82b5a2f15f"
+}
diff --git a/.sqlx/query-b36743219901c666fead6b74a0ef0ba114d994b2e03064ad43c26e4d9bc69b4e.json b/.sqlx/query-b36743219901c666fead6b74a0ef0ba114d994b2e03064ad43c26e4d9bc69b4e.json
new file mode 100644
index 0000000000..163dae7910
--- /dev/null
+++ b/.sqlx/query-b36743219901c666fead6b74a0ef0ba114d994b2e03064ad43c26e4d9bc69b4e.json
@@ -0,0 +1,15 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "UPDATE slack_import_outbox SET cancelled_at = clock_timestamp()\n WHERE job_id = $1 AND slack_channel_id = $2 AND kind = 'import'\n AND published_at IS NULL AND cancelled_at IS NULL",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Text"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "b36743219901c666fead6b74a0ef0ba114d994b2e03064ad43c26e4d9bc69b4e"
+}
diff --git a/.sqlx/query-b3d3fbba4d014d0aa509eb7f1fc895dfa4cbf38507e37358c31859be89f08ce0.json b/.sqlx/query-b3d3fbba4d014d0aa509eb7f1fc895dfa4cbf38507e37358c31859be89f08ce0.json
new file mode 100644
index 0000000000..fe6dcfde32
--- /dev/null
+++ b/.sqlx/query-b3d3fbba4d014d0aa509eb7f1fc895dfa4cbf38507e37358c31859be89f08ce0.json
@@ -0,0 +1,20 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT count(*) FROM comms_channel_participants",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "count",
+ "type_info": "Int8"
+ }
+ ],
+ "parameters": {
+ "Left": []
+ },
+ "nullable": [
+ null
+ ]
+ },
+ "hash": "b3d3fbba4d014d0aa509eb7f1fc895dfa4cbf38507e37358c31859be89f08ce0"
+}
diff --git a/.sqlx/query-b3f6579a5fe4359baf945f0d58654c489318870b0996348ab069f9a8a4c8f732.json b/.sqlx/query-b3f6579a5fe4359baf945f0d58654c489318870b0996348ab069f9a8a4c8f732.json
new file mode 100644
index 0000000000..1a292cc103
--- /dev/null
+++ b/.sqlx/query-b3f6579a5fe4359baf945f0d58654c489318870b0996348ab069f9a8a4c8f732.json
@@ -0,0 +1,14 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "UPDATE comms_channels SET channel_type = 'public' WHERE id = $1",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "b3f6579a5fe4359baf945f0d58654c489318870b0996348ab069f9a8a4c8f732"
+}
diff --git a/.sqlx/query-b41dde9fef44e5f02c094f203b990a309d27ea69acd8670123c736e7dd4a6f26.json b/.sqlx/query-b41dde9fef44e5f02c094f203b990a309d27ea69acd8670123c736e7dd4a6f26.json
new file mode 100644
index 0000000000..2695f0478c
--- /dev/null
+++ b/.sqlx/query-b41dde9fef44e5f02c094f203b990a309d27ea69acd8670123c736e7dd4a6f26.json
@@ -0,0 +1,40 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT channel_type AS \"channel_type: ChannelType\", team_id\n FROM comms_channels WHERE id = $1 FOR UPDATE",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "channel_type: ChannelType",
+ "type_info": {
+ "Custom": {
+ "name": "comms_channel_type",
+ "kind": {
+ "Enum": [
+ "public",
+ "private",
+ "direct_message",
+ "team"
+ ]
+ }
+ }
+ }
+ },
+ {
+ "ordinal": 1,
+ "name": "team_id",
+ "type_info": "Uuid"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ false,
+ true
+ ]
+ },
+ "hash": "b41dde9fef44e5f02c094f203b990a309d27ea69acd8670123c736e7dd4a6f26"
+}
diff --git a/.sqlx/query-b46d01ab978242d753ca1627575ac9339e580c45a232befd7b741649f493ae74.json b/.sqlx/query-b46d01ab978242d753ca1627575ac9339e580c45a232befd7b741649f493ae74.json
new file mode 100644
index 0000000000..176ff8cf89
--- /dev/null
+++ b/.sqlx/query-b46d01ab978242d753ca1627575ac9339e580c45a232befd7b741649f493ae74.json
@@ -0,0 +1,52 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT name, owner_id, team_id, auto_join_team, created_at, updated_at FROM comms_channels WHERE id = $1",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "name",
+ "type_info": "Varchar"
+ },
+ {
+ "ordinal": 1,
+ "name": "owner_id",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 2,
+ "name": "team_id",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 3,
+ "name": "auto_join_team",
+ "type_info": "Bool"
+ },
+ {
+ "ordinal": 4,
+ "name": "created_at",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 5,
+ "name": "updated_at",
+ "type_info": "Timestamptz"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ true,
+ false,
+ true,
+ false,
+ false,
+ false
+ ]
+ },
+ "hash": "b46d01ab978242d753ca1627575ac9339e580c45a232befd7b741649f493ae74"
+}
diff --git a/.sqlx/query-b6fa99a5db3447279ae30107af41a61d9668eba1442dad425d9b9617341350ba.json b/.sqlx/query-b6fa99a5db3447279ae30107af41a61d9668eba1442dad425d9b9617341350ba.json
new file mode 100644
index 0000000000..baf3b1251c
--- /dev/null
+++ b/.sqlx/query-b6fa99a5db3447279ae30107af41a61d9668eba1442dad425d9b9617341350ba.json
@@ -0,0 +1,30 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT id, request_sha256 FROM slack_import_job\n WHERE team_id = $1 AND user_id = $2 AND idempotency_token = $3\n FOR UPDATE",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "id",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 1,
+ "name": "request_sha256",
+ "type_info": "Text"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Text",
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ false,
+ false
+ ]
+ },
+ "hash": "b6fa99a5db3447279ae30107af41a61d9668eba1442dad425d9b9617341350ba"
+}
diff --git a/.sqlx/query-b90d70a8ccab3c00ac870f0ea456e1dafcbc3aac244610129c64b67d4b3c74bb.json b/.sqlx/query-b90d70a8ccab3c00ac870f0ea456e1dafcbc3aac244610129c64b67d4b3c74bb.json
new file mode 100644
index 0000000000..9691599283
--- /dev/null
+++ b/.sqlx/query-b90d70a8ccab3c00ac870f0ea456e1dafcbc3aac244610129c64b67d4b3c74bb.json
@@ -0,0 +1,18 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "UPDATE comms_messages SET content = $5\n WHERE id = $1 AND channel_id = $2\n AND parent_entity_type = 'channel' AND parent_entity_id = $2::text\n AND content = $3 AND content <> $5 AND import_metadata @> $4\n AND deleted_at IS NULL AND edited_at IS NULL AND updated_at = created_at\n AND NOT EXISTS (SELECT 1 FROM comms_message_threads t\n WHERE t.root_id = coalesce(comms_messages.thread_id, comms_messages.id)\n AND t.deleted_at IS NOT NULL)",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Uuid",
+ "Text",
+ "Jsonb",
+ "Text"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "b90d70a8ccab3c00ac870f0ea456e1dafcbc3aac244610129c64b67d4b3c74bb"
+}
diff --git a/.sqlx/query-bd091acfa5b872c5e83ea62a9be05cbd205ca770468279a3cc4f18d2dac261fe.json b/.sqlx/query-bd091acfa5b872c5e83ea62a9be05cbd205ca770468279a3cc4f18d2dac261fe.json
new file mode 100644
index 0000000000..554e4bcfaf
--- /dev/null
+++ b/.sqlx/query-bd091acfa5b872c5e83ea62a9be05cbd205ca770468279a3cc4f18d2dac261fe.json
@@ -0,0 +1,14 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "UPDATE slack_import_conversation SET lease_expires_at = now() - interval '1 minute' WHERE job_id = $1",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "bd091acfa5b872c5e83ea62a9be05cbd205ca770468279a3cc4f18d2dac261fe"
+}
diff --git a/.sqlx/query-c01dce763feba1acbd0f4a86e88a5e8bbf09fc49c4ede548118eaecaff8eccb1.json b/.sqlx/query-c01dce763feba1acbd0f4a86e88a5e8bbf09fc49c4ede548118eaecaff8eccb1.json
new file mode 100644
index 0000000000..804aa48819
--- /dev/null
+++ b/.sqlx/query-c01dce763feba1acbd0f4a86e88a5e8bbf09fc49c4ede548118eaecaff8eccb1.json
@@ -0,0 +1,17 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "INSERT INTO comms_reactions (message_id, user_id, emoji, created_at)\n SELECT input.message_id, input.user_id, input.emoji, input.created_at\n FROM UNNEST($1::uuid[], $2::text[], $3::text[], $4::timestamptz[])\n AS input(message_id, user_id, emoji, created_at)\n ON CONFLICT (message_id, emoji, user_id) DO NOTHING",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "UuidArray",
+ "TextArray",
+ "TextArray",
+ "TimestamptzArray"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "c01dce763feba1acbd0f4a86e88a5e8bbf09fc49c4ede548118eaecaff8eccb1"
+}
diff --git a/.sqlx/query-c1b85553ac1040afcabace1af994a892161e8da09e0f05f1fb742a85a4671b9b.json b/.sqlx/query-c1b85553ac1040afcabace1af994a892161e8da09e0f05f1fb742a85a4671b9b.json
new file mode 100644
index 0000000000..67407d942c
--- /dev/null
+++ b/.sqlx/query-c1b85553ac1040afcabace1af994a892161e8da09e0f05f1fb742a85a4671b9b.json
@@ -0,0 +1,16 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n INSERT INTO comms_messages (id, channel_id, sender_id, content, created_at, deleted_at)\n SELECT id, channel_id, 'macro|[email protected]', 'history',\n '2020-01-01T00:00:00.123456Z'::timestamptz, deleted_at\n FROM UNNEST($1::uuid[], $2::uuid[], $3::timestamptz[]) AS rows(id, channel_id, deleted_at)\n ",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "UuidArray",
+ "UuidArray",
+ "TimestamptzArray"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "c1b85553ac1040afcabace1af994a892161e8da09e0f05f1fb742a85a4671b9b"
+}
diff --git a/.sqlx/query-c69d6ed9b74f4be0b67a0ddde6645f0b76ab12a19afb17e42d26663fc8e802c3.json b/.sqlx/query-c69d6ed9b74f4be0b67a0ddde6645f0b76ab12a19afb17e42d26663fc8e802c3.json
new file mode 100644
index 0000000000..0b3d6c3902
--- /dev/null
+++ b/.sqlx/query-c69d6ed9b74f4be0b67a0ddde6645f0b76ab12a19afb17e42d26663fc8e802c3.json
@@ -0,0 +1,28 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT created_at, user_id FROM comms_entity_mentions WHERE source_entity_type = 'message' AND source_entity_id = $1",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "created_at",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 1,
+ "name": "user_id",
+ "type_info": "Varchar"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Text"
+ ]
+ },
+ "nullable": [
+ false,
+ true
+ ]
+ },
+ "hash": "c69d6ed9b74f4be0b67a0ddde6645f0b76ab12a19afb17e42d26663fc8e802c3"
+}
diff --git a/.sqlx/query-c71a08cf79ef9d4b283f9a3257e501bed4f2d2de822dd231d26efed662272a94.json b/.sqlx/query-c71a08cf79ef9d4b283f9a3257e501bed4f2d2de822dd231d26efed662272a94.json
new file mode 100644
index 0000000000..7598132253
--- /dev/null
+++ b/.sqlx/query-c71a08cf79ef9d4b283f9a3257e501bed4f2d2de822dd231d26efed662272a94.json
@@ -0,0 +1,52 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT c.job_id, c.channel_id AS \"channel_id!\", c.search_dirty_generation, c.search_state,\n c.search_receipt_id, c.search_updated_at FROM slack_import_conversation c\n JOIN slack_import_outbox o ON o.job_id = c.job_id AND o.slack_channel_id = c.slack_channel_id\n AND o.kind = 'search' AND o.generation = c.search_dirty_generation\n WHERE c.channel_id IS NOT NULL AND c.search_state IN ('pending', 'submitted', 'failed')\n AND o.cancelled_at IS NULL AND o.available_at <= clock_timestamp()\n AND NOT EXISTS (SELECT 1 FROM slack_import_message_reference r\n WHERE r.job_id = c.job_id AND r.completed_at IS NULL)\n ORDER BY c.search_updated_at, c.job_id, c.slack_channel_id LIMIT $1",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "job_id",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 1,
+ "name": "channel_id!",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 2,
+ "name": "search_dirty_generation",
+ "type_info": "Int8"
+ },
+ {
+ "ordinal": 3,
+ "name": "search_state",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 4,
+ "name": "search_receipt_id",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 5,
+ "name": "search_updated_at",
+ "type_info": "Timestamptz"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Int8"
+ ]
+ },
+ "nullable": [
+ false,
+ true,
+ false,
+ false,
+ true,
+ false
+ ]
+ },
+ "hash": "c71a08cf79ef9d4b283f9a3257e501bed4f2d2de822dd231d26efed662272a94"
+}
diff --git a/.sqlx/query-c8eb5a917d05660ebe5cb4b4b5f2e0ac3df40a5a37824acefe24411bfca8b67a.json b/.sqlx/query-c8eb5a917d05660ebe5cb4b4b5f2e0ac3df40a5a37824acefe24411bfca8b67a.json
new file mode 100644
index 0000000000..dcf254b3bb
--- /dev/null
+++ b/.sqlx/query-c8eb5a917d05660ebe5cb4b4b5f2e0ac3df40a5a37824acefe24411bfca8b67a.json
@@ -0,0 +1,15 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "UPDATE slack_import_message_reference SET template = NULL, completed_at = clock_timestamp()\n WHERE job_id = $1 AND message_id = $2 AND completed_at IS NULL",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "c8eb5a917d05660ebe5cb4b4b5f2e0ac3df40a5a37824acefe24411bfca8b67a"
+}
diff --git a/.sqlx/query-cba527a9ad52c0021a4efbc387beb723e26e3f62d07340217a5875493306acae.json b/.sqlx/query-cba527a9ad52c0021a4efbc387beb723e26e3f62d07340217a5875493306acae.json
new file mode 100644
index 0000000000..81fba78f56
--- /dev/null
+++ b/.sqlx/query-cba527a9ad52c0021a4efbc387beb723e26e3f62d07340217a5875493306acae.json
@@ -0,0 +1,25 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "INSERT INTO comms_messages\n (id, channel_id, parent_entity_type, parent_entity_id, thread_id,\n sender_id, imported_author, content, created_at, updated_at, edited_at,\n import_metadata, import_order)\n SELECT input.id, $1, 'channel', $2, input.thread_id, input.sender_id,\n input.imported_author, input.content, input.created_at, input.updated_at,\n input.edited_at, input.import_metadata, input.import_order\n FROM UNNEST($3::uuid[], $4::uuid[], $5::text[], $6::text[], $7::text[],\n $8::timestamptz[], $9::timestamptz[], $10::timestamp[], $11::jsonb[], $12::bigint[])\n AS input(id, thread_id, sender_id, imported_author, content, created_at,\n updated_at, edited_at, import_metadata, import_order)\n ON CONFLICT (id) DO NOTHING",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Text",
+ "UuidArray",
+ "UuidArray",
+ "TextArray",
+ "TextArray",
+ "TextArray",
+ "TimestamptzArray",
+ "TimestamptzArray",
+ "TimestampArray",
+ "JsonbArray",
+ "Int8Array"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "cba527a9ad52c0021a4efbc387beb723e26e3f62d07340217a5875493306acae"
+}
diff --git a/.sqlx/query-cdb67d7c03e18b81c5c1cc9ddd26d81999d7deccc030620155029797e6c5082b.json b/.sqlx/query-cdb67d7c03e18b81c5c1cc9ddd26d81999d7deccc030620155029797e6c5082b.json
new file mode 100644
index 0000000000..674815f5c0
--- /dev/null
+++ b/.sqlx/query-cdb67d7c03e18b81c5c1cc9ddd26d81999d7deccc030620155029797e6c5082b.json
@@ -0,0 +1,15 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "INSERT INTO macro_user (id, username, email, stripe_customer_id) VALUES ($1, 'archive', '[email protected]', 'archive-customer'), ($2, 'foreign', '[email protected]', 'foreign-customer')",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "cdb67d7c03e18b81c5c1cc9ddd26d81999d7deccc030620155029797e6c5082b"
+}
diff --git a/.sqlx/query-d0c6486247b6fa54849d8c77c7febe9efcd9460ef93ba0ec161e11f103585ada.json b/.sqlx/query-d0c6486247b6fa54849d8c77c7febe9efcd9460ef93ba0ec161e11f103585ada.json
new file mode 100644
index 0000000000..6a586f77fc
--- /dev/null
+++ b/.sqlx/query-d0c6486247b6fa54849d8c77c7febe9efcd9460ef93ba0ec161e11f103585ada.json
@@ -0,0 +1,28 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT s.index AS \"index!\", m.message_id FROM jsonb_to_recordset($1) AS s(index integer, team uuid, channel text, ts bigint)\n JOIN slack_import_message_map m ON m.team_id = s.team AND m.slack_channel_id = s.channel AND m.slack_ts = s.ts",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "index!",
+ "type_info": "Int4"
+ },
+ {
+ "ordinal": 1,
+ "name": "message_id",
+ "type_info": "Uuid"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Jsonb"
+ ]
+ },
+ "nullable": [
+ null,
+ false
+ ]
+ },
+ "hash": "d0c6486247b6fa54849d8c77c7febe9efcd9460ef93ba0ec161e11f103585ada"
+}
diff --git a/.sqlx/query-d30d13d5007fcc16ede37aa80a3ccca085867c4481451b6551a6620f9561b76d.json b/.sqlx/query-d30d13d5007fcc16ede37aa80a3ccca085867c4481451b6551a6620f9561b76d.json
new file mode 100644
index 0000000000..77d6757f0a
--- /dev/null
+++ b/.sqlx/query-d30d13d5007fcc16ede37aa80a3ccca085867c4481451b6551a6620f9561b76d.json
@@ -0,0 +1,42 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT user_id, role::text AS \"role!\", joined_at, left_at\n FROM comms_channel_participants WHERE channel_id = $1\n AND ($2 OR user_id = $3) ORDER BY user_id LIMIT 3 FOR UPDATE",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "user_id",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 1,
+ "name": "role!",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 2,
+ "name": "joined_at",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 3,
+ "name": "left_at",
+ "type_info": "Timestamptz"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Bool",
+ "Text"
+ ]
+ },
+ "nullable": [
+ false,
+ null,
+ false,
+ true
+ ]
+ },
+ "hash": "d30d13d5007fcc16ede37aa80a3ccca085867c4481451b6551a6620f9561b76d"
+}
diff --git a/.sqlx/query-d33c134eba8107a87fe05325e03c3c5f66d6e5beb40989427ba31b117fbb6dae.json b/.sqlx/query-d33c134eba8107a87fe05325e03c3c5f66d6e5beb40989427ba31b117fbb6dae.json
new file mode 100644
index 0000000000..1d932b2739
--- /dev/null
+++ b/.sqlx/query-d33c134eba8107a87fe05325e03c3c5f66d6e5beb40989427ba31b117fbb6dae.json
@@ -0,0 +1,20 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT COUNT(*) FROM comms_channels",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "count",
+ "type_info": "Int8"
+ }
+ ],
+ "parameters": {
+ "Left": []
+ },
+ "nullable": [
+ null
+ ]
+ },
+ "hash": "d33c134eba8107a87fe05325e03c3c5f66d6e5beb40989427ba31b117fbb6dae"
+}
diff --git a/.sqlx/query-d390c2e9563c62e31a26c4797b1831a97429bf181bacab118ff2b99ee58d1e61.json b/.sqlx/query-d390c2e9563c62e31a26c4797b1831a97429bf181bacab118ff2b99ee58d1e61.json
new file mode 100644
index 0000000000..74ce3111b4
--- /dev/null
+++ b/.sqlx/query-d390c2e9563c62e31a26c4797b1831a97429bf181bacab118ff2b99ee58d1e61.json
@@ -0,0 +1,22 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT COUNT(*) FROM comms_channel_participants WHERE channel_id = $1",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "count",
+ "type_info": "Int8"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ null
+ ]
+ },
+ "hash": "d390c2e9563c62e31a26c4797b1831a97429bf181bacab118ff2b99ee58d1e61"
+}
diff --git a/.sqlx/query-d4254119e95786200931aad8d4b8172c2ace7a42531ce193a86d4b035d148387.json b/.sqlx/query-d4254119e95786200931aad8d4b8172c2ace7a42531ce193a86d4b035d148387.json
new file mode 100644
index 0000000000..36ca7522e4
--- /dev/null
+++ b/.sqlx/query-d4254119e95786200931aad8d4b8172c2ace7a42531ce193a86d4b035d148387.json
@@ -0,0 +1,16 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "UPDATE slack_import_job SET registered_bytes = $3 WHERE team_id = $1 AND id = $2",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Uuid",
+ "Int8"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "d4254119e95786200931aad8d4b8172c2ace7a42531ce193a86d4b035d148387"
+}
diff --git a/.sqlx/query-d4607d38e56b4c58e6d25816028eac94d90ad55597f43b9cb09e8d935dddcf70.json b/.sqlx/query-d4607d38e56b4c58e6d25816028eac94d90ad55597f43b9cb09e8d935dddcf70.json
new file mode 100644
index 0000000000..3b579e43bd
--- /dev/null
+++ b/.sqlx/query-d4607d38e56b4c58e6d25816028eac94d90ad55597f43b9cb09e8d935dddcf70.json
@@ -0,0 +1,52 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT team_id, user_id, created_at, (cancel_requested_at IS NOT NULL) AS \"cancelled!\",\n (staging_expires_at <= clock_timestamp() + interval '1 day') AS \"expired!\",\n (status IN ('completed', 'completed_with_errors', 'failed', 'cancelled')) AS \"terminal!\"\n FROM slack_import_job WHERE id = $1 FOR UPDATE",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "team_id",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 1,
+ "name": "user_id",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 2,
+ "name": "created_at",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 3,
+ "name": "cancelled!",
+ "type_info": "Bool"
+ },
+ {
+ "ordinal": 4,
+ "name": "expired!",
+ "type_info": "Bool"
+ },
+ {
+ "ordinal": 5,
+ "name": "terminal!",
+ "type_info": "Bool"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ false,
+ false,
+ false,
+ null,
+ null,
+ null
+ ]
+ },
+ "hash": "d4607d38e56b4c58e6d25816028eac94d90ad55597f43b9cb09e8d935dddcf70"
+}
diff --git a/.sqlx/query-d5d3a3f46f7cf1a9a6ab4bbd5df9dc1e75f5deddcf822ba1f2fb83169f4837ff.json b/.sqlx/query-d5d3a3f46f7cf1a9a6ab4bbd5df9dc1e75f5deddcf822ba1f2fb83169f4837ff.json
new file mode 100644
index 0000000000..b6518b89b5
--- /dev/null
+++ b/.sqlx/query-d5d3a3f46f7cf1a9a6ab4bbd5df9dc1e75f5deddcf822ba1f2fb83169f4837ff.json
@@ -0,0 +1,20 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT COUNT(*) FROM \"User\"",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "count",
+ "type_info": "Int8"
+ }
+ ],
+ "parameters": {
+ "Left": []
+ },
+ "nullable": [
+ null
+ ]
+ },
+ "hash": "d5d3a3f46f7cf1a9a6ab4bbd5df9dc1e75f5deddcf822ba1f2fb83169f4837ff"
+}
diff --git a/.sqlx/query-d6232109e7acd878393e1570ac0faa24eee7b9744e62950fb9ac123f4994fad8.json b/.sqlx/query-d6232109e7acd878393e1570ac0faa24eee7b9744e62950fb9ac123f4994fad8.json
new file mode 100644
index 0000000000..dceec9fa94
--- /dev/null
+++ b/.sqlx/query-d6232109e7acd878393e1570ac0faa24eee7b9744e62950fb9ac123f4994fad8.json
@@ -0,0 +1,25 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "INSERT INTO slack_import_message_map (team_id, slack_channel_id, slack_ts, message_id, channel_id)\n SELECT $1, $2, m.ts, m.message_id, $3 FROM jsonb_to_recordset($4) AS m(ts bigint, message_id uuid)\n ORDER BY m.ts ON CONFLICT (team_id, slack_channel_id, slack_ts) DO NOTHING RETURNING slack_ts",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "slack_ts",
+ "type_info": "Int8"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Text",
+ "Uuid",
+ "Jsonb"
+ ]
+ },
+ "nullable": [
+ false
+ ]
+ },
+ "hash": "d6232109e7acd878393e1570ac0faa24eee7b9744e62950fb9ac123f4994fad8"
+}
diff --git a/.sqlx/query-d656828c55e8ec6d84b6b32efdfe21c7dcf54a66192198f6541e3f1f89a33992.json b/.sqlx/query-d656828c55e8ec6d84b6b32efdfe21c7dcf54a66192198f6541e3f1f89a33992.json
new file mode 100644
index 0000000000..a1a088757d
--- /dev/null
+++ b/.sqlx/query-d656828c55e8ec6d84b6b32efdfe21c7dcf54a66192198f6541e3f1f89a33992.json
@@ -0,0 +1,23 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT checkpoint_part = part_count AND checkpoint_record = 0 AS \"complete!\"\n FROM slack_import_conversation WHERE job_id = $1 AND slack_channel_id = $2",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "complete!",
+ "type_info": "Bool"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Text"
+ ]
+ },
+ "nullable": [
+ null
+ ]
+ },
+ "hash": "d656828c55e8ec6d84b6b32efdfe21c7dcf54a66192198f6541e3f1f89a33992"
+}
diff --git a/.sqlx/query-d82233b2b97a64be5dfbfe4ddb5bf9c36cf8516279191ffac82df02b95cb3524.json b/.sqlx/query-d82233b2b97a64be5dfbfe4ddb5bf9c36cf8516279191ffac82df02b95cb3524.json
new file mode 100644
index 0000000000..58d59eb1bf
--- /dev/null
+++ b/.sqlx/query-d82233b2b97a64be5dfbfe4ddb5bf9c36cf8516279191ffac82df02b95cb3524.json
@@ -0,0 +1,15 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "INSERT INTO comms_channels (id, owner_id, channel_type, created_at) VALUES ($1, 'macro|[email protected]', 'direct_message', $2)",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Timestamptz"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "d82233b2b97a64be5dfbfe4ddb5bf9c36cf8516279191ffac82df02b95cb3524"
+}
diff --git a/.sqlx/query-d97a72359e8b3cd8601e2d017ee6bee90e55bdcf2e51e8b4bcc8d23b26301eee.json b/.sqlx/query-d97a72359e8b3cd8601e2d017ee6bee90e55bdcf2e51e8b4bcc8d23b26301eee.json
new file mode 100644
index 0000000000..85cc9a5328
--- /dev/null
+++ b/.sqlx/query-d97a72359e8b3cd8601e2d017ee6bee90e55bdcf2e51e8b4bcc8d23b26301eee.json
@@ -0,0 +1,14 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "INSERT INTO comms_channel_participants (channel_id, user_id, role, left_at) VALUES ($1, 'macro|[email protected]', 'member', NOW())",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "d97a72359e8b3cd8601e2d017ee6bee90e55bdcf2e51e8b4bcc8d23b26301eee"
+}
diff --git a/.sqlx/query-dc23710c1f5ef697a46fc8f94b039d8ceeae690dfdb03f894600772287396006.json b/.sqlx/query-dc23710c1f5ef697a46fc8f94b039d8ceeae690dfdb03f894600772287396006.json
new file mode 100644
index 0000000000..a094e32d6c
--- /dev/null
+++ b/.sqlx/query-dc23710c1f5ef697a46fc8f94b039d8ceeae690dfdb03f894600772287396006.json
@@ -0,0 +1,28 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT team_id, channel_type::text AS \"kind!\" FROM comms_channels WHERE id = $1 FOR UPDATE",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "team_id",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 1,
+ "name": "kind!",
+ "type_info": "Text"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ true,
+ null
+ ]
+ },
+ "hash": "dc23710c1f5ef697a46fc8f94b039d8ceeae690dfdb03f894600772287396006"
+}
diff --git a/.sqlx/query-dc2ec6a135cda3f8dba621e94607fef4e969871d201ff20c7fc49fbfdc083643.json b/.sqlx/query-dc2ec6a135cda3f8dba621e94607fef4e969871d201ff20c7fc49fbfdc083643.json
new file mode 100644
index 0000000000..bddc44d156
--- /dev/null
+++ b/.sqlx/query-dc2ec6a135cda3f8dba621e94607fef4e969871d201ff20c7fc49fbfdc083643.json
@@ -0,0 +1,14 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "UPDATE comms_messages SET content = 'live edit', edited_at = now() WHERE id = $1",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "dc2ec6a135cda3f8dba621e94607fef4e969871d201ff20c7fc49fbfdc083643"
+}
diff --git a/.sqlx/query-dccb25443a931c517f20948f076aa52bad64d3401182432fde55bbad8f75f921.json b/.sqlx/query-dccb25443a931c517f20948f076aa52bad64d3401182432fde55bbad8f75f921.json
new file mode 100644
index 0000000000..f2a40c7f09
--- /dev/null
+++ b/.sqlx/query-dccb25443a931c517f20948f076aa52bad64d3401182432fde55bbad8f75f921.json
@@ -0,0 +1,20 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT count(*) FROM comms_message_threads",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "count",
+ "type_info": "Int8"
+ }
+ ],
+ "parameters": {
+ "Left": []
+ },
+ "nullable": [
+ null
+ ]
+ },
+ "hash": "dccb25443a931c517f20948f076aa52bad64d3401182432fde55bbad8f75f921"
+}
diff --git a/.sqlx/query-dee4c075f75a05b8a209c0e771d9e4471485983dfa7e07ddf3c03803bb217fe1.json b/.sqlx/query-dee4c075f75a05b8a209c0e771d9e4471485983dfa7e07ddf3c03803bb217fe1.json
new file mode 100644
index 0000000000..cb54490e5d
--- /dev/null
+++ b/.sqlx/query-dee4c075f75a05b8a209c0e771d9e4471485983dfa7e07ddf3c03803bb217fe1.json
@@ -0,0 +1,14 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "UPDATE comms_messages SET content = 'live', edited_at = now() WHERE id = $1",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "dee4c075f75a05b8a209c0e771d9e4471485983dfa7e07ddf3c03803bb217fe1"
+}
diff --git a/.sqlx/query-e02aeee47ef3a5a376c3154c98b066a7e77d326248f2a647b4a8e87fe32fc2b1.json b/.sqlx/query-e02aeee47ef3a5a376c3154c98b066a7e77d326248f2a647b4a8e87fe32fc2b1.json
new file mode 100644
index 0000000000..1d7fba0fb2
--- /dev/null
+++ b/.sqlx/query-e02aeee47ef3a5a376c3154c98b066a7e77d326248f2a647b4a8e87fe32fc2b1.json
@@ -0,0 +1,20 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT count(*) FROM slack_import_message_reference",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "count",
+ "type_info": "Int8"
+ }
+ ],
+ "parameters": {
+ "Left": []
+ },
+ "nullable": [
+ null
+ ]
+ },
+ "hash": "e02aeee47ef3a5a376c3154c98b066a7e77d326248f2a647b4a8e87fe32fc2b1"
+}
diff --git a/.sqlx/query-e1f1183dbb1b368f91f9a8ca1268d71fcf7b2ac3aa1fffe5bf452de54a29c104.json b/.sqlx/query-e1f1183dbb1b368f91f9a8ca1268d71fcf7b2ac3aa1fffe5bf452de54a29c104.json
new file mode 100644
index 0000000000..fb8b7b89b7
--- /dev/null
+++ b/.sqlx/query-e1f1183dbb1b368f91f9a8ca1268d71fcf7b2ac3aa1fffe5bf452de54a29c104.json
@@ -0,0 +1,26 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n INSERT INTO import_target_reservation\n (team_id, source, foreign_id, candidate_channel_id, channel_id, state)\n VALUES ($1, 'slack', $2, $3, $4, $5)\n ON CONFLICT (team_id, source, foreign_id) DO NOTHING\n RETURNING candidate_channel_id\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "candidate_channel_id",
+ "type_info": "Uuid"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Text",
+ "Uuid",
+ "Uuid",
+ "Text"
+ ]
+ },
+ "nullable": [
+ false
+ ]
+ },
+ "hash": "e1f1183dbb1b368f91f9a8ca1268d71fcf7b2ac3aa1fffe5bf452de54a29c104"
+}
diff --git a/.sqlx/query-e3aa177e983faf3a77dca3ebde016eaee7d225f279333475c5c78fd88dce24bc.json b/.sqlx/query-e3aa177e983faf3a77dca3ebde016eaee7d225f279333475c5c78fd88dce24bc.json
new file mode 100644
index 0000000000..fef4445d7f
--- /dev/null
+++ b/.sqlx/query-e3aa177e983faf3a77dca3ebde016eaee7d225f279333475c5c78fd88dce24bc.json
@@ -0,0 +1,24 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT c.sealed_at FROM slack_import_conversation c\n JOIN slack_import_job j ON j.id = c.job_id\n WHERE j.team_id = $1 AND j.id = $2 AND c.slack_channel_id = $3",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "sealed_at",
+ "type_info": "Timestamptz"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Uuid",
+ "Text"
+ ]
+ },
+ "nullable": [
+ true
+ ]
+ },
+ "hash": "e3aa177e983faf3a77dca3ebde016eaee7d225f279333475c5c78fd88dce24bc"
+}
diff --git a/.sqlx/query-e4b7973901b35028d8ddf8bcac7b93c1b58beecbb2a38a1aa7095210cc311150.json b/.sqlx/query-e4b7973901b35028d8ddf8bcac7b93c1b58beecbb2a38a1aa7095210cc311150.json
new file mode 100644
index 0000000000..cf23315619
--- /dev/null
+++ b/.sqlx/query-e4b7973901b35028d8ddf8bcac7b93c1b58beecbb2a38a1aa7095210cc311150.json
@@ -0,0 +1,14 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "INSERT INTO comms_channels (id, name, channel_type, owner_id) VALUES ($1, 'source', 'private', 'macro|[email protected]')",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "e4b7973901b35028d8ddf8bcac7b93c1b58beecbb2a38a1aa7095210cc311150"
+}
diff --git a/.sqlx/query-e4e2f979bf0f8b7dccd44a3a63be28fdfe12b11f6e4729cac81428fac6535e0c.json b/.sqlx/query-e4e2f979bf0f8b7dccd44a3a63be28fdfe12b11f6e4729cac81428fac6535e0c.json
new file mode 100644
index 0000000000..83af7187c8
--- /dev/null
+++ b/.sqlx/query-e4e2f979bf0f8b7dccd44a3a63be28fdfe12b11f6e4729cac81428fac6535e0c.json
@@ -0,0 +1,22 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT template IS NULL AS \"cleared!\" FROM slack_import_message_reference WHERE job_id = $1",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "cleared!",
+ "type_info": "Bool"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ null
+ ]
+ },
+ "hash": "e4e2f979bf0f8b7dccd44a3a63be28fdfe12b11f6e4729cac81428fac6535e0c"
+}
diff --git a/.sqlx/query-e5296403ab8cb98d690e39dbf06732a1072d29fd739ab9389fdc91633f3c35ae.json b/.sqlx/query-e5296403ab8cb98d690e39dbf06732a1072d29fd739ab9389fdc91633f3c35ae.json
new file mode 100644
index 0000000000..0eaa26d08b
--- /dev/null
+++ b/.sqlx/query-e5296403ab8cb98d690e39dbf06732a1072d29fd739ab9389fdc91633f3c35ae.json
@@ -0,0 +1,34 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT o.job_id, o.slack_channel_id, o.generation FROM slack_import_outbox o\n JOIN slack_import_job j ON j.id = o.job_id\n JOIN slack_import_conversation c ON c.job_id = o.job_id AND c.slack_channel_id = o.slack_channel_id\n WHERE o.kind = 'import' AND o.published_at IS NULL AND o.cancelled_at IS NULL\n AND o.available_at <= clock_timestamp() AND j.cancel_requested_at IS NULL\n AND j.staging_expires_at > clock_timestamp() + interval '1 day'\n AND c.status IN ('queued', 'importing') AND c.event_generation = o.generation\n ORDER BY o.available_at, o.job_id, o.slack_channel_id LIMIT $1",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "job_id",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 1,
+ "name": "slack_channel_id",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 2,
+ "name": "generation",
+ "type_info": "Int8"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Int8"
+ ]
+ },
+ "nullable": [
+ false,
+ false,
+ false
+ ]
+ },
+ "hash": "e5296403ab8cb98d690e39dbf06732a1072d29fd739ab9389fdc91633f3c35ae"
+}
diff --git a/.sqlx/query-e604c1694fb32fda9999e462c75a8a73c77188111365da9c9daf8181f2fc4801.json b/.sqlx/query-e604c1694fb32fda9999e462c75a8a73c77188111365da9c9daf8181f2fc4801.json
new file mode 100644
index 0000000000..eb67c9d30c
--- /dev/null
+++ b/.sqlx/query-e604c1694fb32fda9999e462c75a8a73c77188111365da9c9daf8181f2fc4801.json
@@ -0,0 +1,35 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT user_id, source_workspace_id, confirmed_unknown FROM slack_import_job WHERE team_id = $1 AND id = $2",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "user_id",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 1,
+ "name": "source_workspace_id",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 2,
+ "name": "confirmed_unknown",
+ "type_info": "Bool"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ false,
+ true,
+ false
+ ]
+ },
+ "hash": "e604c1694fb32fda9999e462c75a8a73c77188111365da9c9daf8181f2fc4801"
+}
diff --git a/.sqlx/query-e62f7eb658fa6fce4cdcf225f60246b23e9d6aa4db3c14872565398d374360f4.json b/.sqlx/query-e62f7eb658fa6fce4cdcf225f60246b23e9d6aa4db3c14872565398d374360f4.json
new file mode 100644
index 0000000000..3d9581ed48
--- /dev/null
+++ b/.sqlx/query-e62f7eb658fa6fce4cdcf225f60246b23e9d6aa4db3c14872565398d374360f4.json
@@ -0,0 +1,30 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT c.part_count, c.manifest_sha256 FROM slack_import_conversation c\n JOIN slack_import_job j ON j.id = c.job_id\n WHERE j.team_id = $1 AND j.id = $2 AND c.slack_channel_id = $3",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "part_count",
+ "type_info": "Int4"
+ },
+ {
+ "ordinal": 1,
+ "name": "manifest_sha256",
+ "type_info": "Text"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Uuid",
+ "Text"
+ ]
+ },
+ "nullable": [
+ true,
+ true
+ ]
+ },
+ "hash": "e62f7eb658fa6fce4cdcf225f60246b23e9d6aa4db3c14872565398d374360f4"
+}
diff --git a/.sqlx/query-e8716b5ae331e16652de2e4691250b6060db0e4e7c4c744c9f157ab32f6d9224.json b/.sqlx/query-e8716b5ae331e16652de2e4691250b6060db0e4e7c4c744c9f157ab32f6d9224.json
new file mode 100644
index 0000000000..345ddd8e80
--- /dev/null
+++ b/.sqlx/query-e8716b5ae331e16652de2e4691250b6060db0e4e7c4c744c9f157ab32f6d9224.json
@@ -0,0 +1,46 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT channel_id, parent_entity_type, parent_entity_id, import_metadata, import_order FROM comms_messages WHERE id = $1",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "channel_id",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 1,
+ "name": "parent_entity_type",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 2,
+ "name": "parent_entity_id",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 3,
+ "name": "import_metadata",
+ "type_info": "Jsonb"
+ },
+ {
+ "ordinal": 4,
+ "name": "import_order",
+ "type_info": "Int8"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ true,
+ false,
+ false,
+ true,
+ true
+ ]
+ },
+ "hash": "e8716b5ae331e16652de2e4691250b6060db0e4e7c4c744c9f157ab32f6d9224"
+}
diff --git a/.sqlx/query-e8b84f8bea9c2d7daea0147a21ba48645ad93cba0b8ebb6da3458faa6b226118.json b/.sqlx/query-e8b84f8bea9c2d7daea0147a21ba48645ad93cba0b8ebb6da3458faa6b226118.json
new file mode 100644
index 0000000000..4f8da595a0
--- /dev/null
+++ b/.sqlx/query-e8b84f8bea9c2d7daea0147a21ba48645ad93cba0b8ebb6da3458faa6b226118.json
@@ -0,0 +1,17 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "WITH recovered AS (\n UPDATE slack_import_conversation SET\n status = CASE WHEN $2 THEN 'skipped' WHEN $3 OR attempts >= $4 THEN 'failed' ELSE 'queued' END,\n last_error = CASE WHEN $3 OR attempts >= $4 THEN 'internal' ELSE last_error END,\n settled_at = CASE WHEN $2 OR $3 OR attempts >= $4 THEN clock_timestamp() ELSE NULL END,\n event_generation = event_generation + 1,\n lease_owner = NULL, lease_token = NULL, lease_expires_at = NULL, heartbeat_at = NULL,\n updated_at = clock_timestamp()\n WHERE job_id = $1 AND ((status = 'importing' AND lease_expires_at <= clock_timestamp())\n OR (status = 'queued' AND ($2 OR $3)))\n RETURNING job_id, slack_channel_id, event_generation, status\n ) INSERT INTO slack_import_outbox (job_id, slack_channel_id, kind, generation)\n SELECT job_id, slack_channel_id, 'import', event_generation FROM recovered WHERE status = 'queued'\n ON CONFLICT (job_id, slack_channel_id, kind, generation) DO NOTHING",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Bool",
+ "Bool",
+ "Int4"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "e8b84f8bea9c2d7daea0147a21ba48645ad93cba0b8ebb6da3458faa6b226118"
+}
diff --git a/.sqlx/query-e8b8bc8df52278e768a8d865161b3e1cb3d85383d399287b714146d42db1dfe3.json b/.sqlx/query-e8b8bc8df52278e768a8d865161b3e1cb3d85383d399287b714146d42db1dfe3.json
new file mode 100644
index 0000000000..0fd230bdfe
--- /dev/null
+++ b/.sqlx/query-e8b8bc8df52278e768a8d865161b3e1cb3d85383d399287b714146d42db1dfe3.json
@@ -0,0 +1,29 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n SELECT candidate_channel_id, state FROM import_target_reservation\n WHERE team_id = $1 AND source = 'slack' AND foreign_id = $2\n FOR UPDATE\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "candidate_channel_id",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 1,
+ "name": "state",
+ "type_info": "Text"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Text"
+ ]
+ },
+ "nullable": [
+ false,
+ false
+ ]
+ },
+ "hash": "e8b8bc8df52278e768a8d865161b3e1cb3d85383d399287b714146d42db1dfe3"
+}
diff --git a/.sqlx/query-e95e1cc63a4ea20a7586d8f67fa69c6a67af25630d6e4bdefbc380421207a141.json b/.sqlx/query-e95e1cc63a4ea20a7586d8f67fa69c6a67af25630d6e4bdefbc380421207a141.json
new file mode 100644
index 0000000000..27047eae6b
--- /dev/null
+++ b/.sqlx/query-e95e1cc63a4ea20a7586d8f67fa69c6a67af25630d6e4bdefbc380421207a141.json
@@ -0,0 +1,16 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "INSERT INTO comms_channel_participants (channel_id, user_id, role, joined_at)\n VALUES ($1, $2, 'owner', $3) ON CONFLICT (channel_id, user_id) DO NOTHING",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Text",
+ "Timestamptz"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "e95e1cc63a4ea20a7586d8f67fa69c6a67af25630d6e4bdefbc380421207a141"
+}
diff --git a/.sqlx/query-ec6a86191fd9b73e86f19b543e6bc977c5397850c402248fd014ae97f210f5b6.json b/.sqlx/query-ec6a86191fd9b73e86f19b543e6bc977c5397850c402248fd014ae97f210f5b6.json
new file mode 100644
index 0000000000..308196510b
--- /dev/null
+++ b/.sqlx/query-ec6a86191fd9b73e86f19b543e6bc977c5397850c402248fd014ae97f210f5b6.json
@@ -0,0 +1,40 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT content, created_at, updated_at, edited_at FROM comms_messages WHERE id = $1",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "content",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 1,
+ "name": "created_at",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 2,
+ "name": "updated_at",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 3,
+ "name": "edited_at",
+ "type_info": "Timestamp"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ false,
+ false,
+ false,
+ true
+ ]
+ },
+ "hash": "ec6a86191fd9b73e86f19b543e6bc977c5397850c402248fd014ae97f210f5b6"
+}
diff --git a/.sqlx/query-ed1c423836a64765d2943e28f8b12efb3d4df076738e92296dd1598656b13f4d.json b/.sqlx/query-ed1c423836a64765d2943e28f8b12efb3d4df076738e92296dd1598656b13f4d.json
new file mode 100644
index 0000000000..95d4b40c32
--- /dev/null
+++ b/.sqlx/query-ed1c423836a64765d2943e28f8b12efb3d4df076738e92296dd1598656b13f4d.json
@@ -0,0 +1,15 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "INSERT INTO team_user (user_id, team_id, team_role) VALUES ($1, $2, 'admin')\n ON CONFLICT (user_id) DO UPDATE SET team_id = EXCLUDED.team_id",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Text",
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "ed1c423836a64765d2943e28f8b12efb3d4df076738e92296dd1598656b13f4d"
+}
diff --git a/.sqlx/query-f03e2a95a8bb58368fd8a258bb99055e23d3e495be1743acc8b99c6daf884e97.json b/.sqlx/query-f03e2a95a8bb58368fd8a258bb99055e23d3e495be1743acc8b99c6daf884e97.json
new file mode 100644
index 0000000000..7507dea1e1
--- /dev/null
+++ b/.sqlx/query-f03e2a95a8bb58368fd8a258bb99055e23d3e495be1743acc8b99c6daf884e97.json
@@ -0,0 +1,46 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT slack_channel_id, message_id, channel_id, importer_version, template\n FROM slack_import_message_reference WHERE job_id = $1 AND completed_at IS NULL\n ORDER BY message_id LIMIT 1",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "slack_channel_id",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 1,
+ "name": "message_id",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 2,
+ "name": "channel_id",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 3,
+ "name": "importer_version",
+ "type_info": "Int2"
+ },
+ {
+ "ordinal": 4,
+ "name": "template",
+ "type_info": "Jsonb"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ false,
+ false,
+ false,
+ false,
+ true
+ ]
+ },
+ "hash": "f03e2a95a8bb58368fd8a258bb99055e23d3e495be1743acc8b99c6daf884e97"
+}
diff --git a/.sqlx/query-f325000252f1fc7932394223f3f99905eabf712914b4f7b9b63a8653950285a0.json b/.sqlx/query-f325000252f1fc7932394223f3f99905eabf712914b4f7b9b63a8653950285a0.json
new file mode 100644
index 0000000000..6107d1a2a8
--- /dev/null
+++ b/.sqlx/query-f325000252f1fc7932394223f3f99905eabf712914b4f7b9b63a8653950285a0.json
@@ -0,0 +1,66 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT j.id, j.status, j.revision, j.created_at, j.updated_at,\n j.registration_closed_at, j.source_workspace_id, j.include_message_history\n FROM slack_import_job j\n WHERE j.team_id = $1 AND ($2::uuid IS NULL OR j.id = $2)\n AND ($3::uuid IS NULL OR j.id < $3)\n ORDER BY j.id DESC LIMIT 50 FOR SHARE OF j",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "id",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 1,
+ "name": "status",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 2,
+ "name": "revision",
+ "type_info": "Int8"
+ },
+ {
+ "ordinal": 3,
+ "name": "created_at",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 4,
+ "name": "updated_at",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 5,
+ "name": "registration_closed_at",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 6,
+ "name": "source_workspace_id",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 7,
+ "name": "include_message_history",
+ "type_info": "Bool"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Uuid",
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ false,
+ false,
+ false,
+ false,
+ false,
+ true,
+ true,
+ false
+ ]
+ },
+ "hash": "f325000252f1fc7932394223f3f99905eabf712914b4f7b9b63a8653950285a0"
+}
diff --git a/.sqlx/query-f35f0aea15af8da6867b9d83ec9337506100007f3124b9023c4651da20a5d954.json b/.sqlx/query-f35f0aea15af8da6867b9d83ec9337506100007f3124b9023c4651da20a5d954.json
new file mode 100644
index 0000000000..de08d9e6c7
--- /dev/null
+++ b/.sqlx/query-f35f0aea15af8da6867b9d83ec9337506100007f3124b9023c4651da20a5d954.json
@@ -0,0 +1,14 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "UPDATE slack_import_outbox SET available_at = clock_timestamp() - interval '1 second' WHERE job_id = $1 AND kind = 'search'",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "f35f0aea15af8da6867b9d83ec9337506100007f3124b9023c4651da20a5d954"
+}
diff --git a/.sqlx/query-f4508457e87ff8fe3702c661f4c5a11eadf7d30b7be63741ca4aad8ee2c2b8c9.json b/.sqlx/query-f4508457e87ff8fe3702c661f4c5a11eadf7d30b7be63741ca4aad8ee2c2b8c9.json
new file mode 100644
index 0000000000..b459b2ee13
--- /dev/null
+++ b/.sqlx/query-f4508457e87ff8fe3702c661f4c5a11eadf7d30b7be63741ca4aad8ee2c2b8c9.json
@@ -0,0 +1,14 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "UPDATE comms_messages SET deleted_at = now() WHERE channel_id = $1",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "f4508457e87ff8fe3702c661f4c5a11eadf7d30b7be63741ca4aad8ee2c2b8c9"
+}
diff --git a/.sqlx/query-f666bbb70cea5292ca1a0e8d353ce43d9a8e4bdd2f0db3b3b5f9e96a5b80c852.json b/.sqlx/query-f666bbb70cea5292ca1a0e8d353ce43d9a8e4bdd2f0db3b3b5f9e96a5b80c852.json
new file mode 100644
index 0000000000..b0f94cd0b7
--- /dev/null
+++ b/.sqlx/query-f666bbb70cea5292ca1a0e8d353ce43d9a8e4bdd2f0db3b3b5f9e96a5b80c852.json
@@ -0,0 +1,14 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "UPDATE slack_import_job SET staging_expires_at = clock_timestamp() + interval '12 hours' WHERE team_id = $1",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "f666bbb70cea5292ca1a0e8d353ce43d9a8e4bdd2f0db3b3b5f9e96a5b80c852"
+}
diff --git a/.sqlx/query-908627bdd27f344ce26c501bb7431b621173c04bfda9678021360817c3f25103.json b/.sqlx/query-f77d0dbbbc6bc89b676be55a9939ffbd908da49d228156dbed63c097fd25d39b.json
similarity index 76%
rename from .sqlx/query-908627bdd27f344ce26c501bb7431b621173c04bfda9678021360817c3f25103.json
rename to .sqlx/query-f77d0dbbbc6bc89b676be55a9939ffbd908da49d228156dbed63c097fd25d39b.json
index ad59b616b1..9433c7fcc7 100644
--- a/.sqlx/query-908627bdd27f344ce26c501bb7431b621173c04bfda9678021360817c3f25103.json
+++ b/.sqlx/query-f77d0dbbbc6bc89b676be55a9939ffbd908da49d228156dbed63c097fd25d39b.json
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
- "query": "\n SELECT\n c.id as \"channel_id\",\n c.name as \"name\",\n c.channel_type AS \"channel_type: ChannelType\",\n c.org_id as \"org_id\",\n m.id as \"message_id\",\n m.thread_id as \"thread_id\",\n m.sender_id as \"sender_id\",\n m.content as \"content\",\n m.created_at as \"created_at\",\n m.updated_at as \"updated_at\",\n m.deleted_at::timestamptz as \"deleted_at\"\n FROM\n comms_messages m\n JOIN\n comms_channels c on c.\"id\" = m.\"channel_id\"\n WHERE\n m.id = $1\n AND c.id = $2\n ",
+ "query": "\n SELECT\n c.id as \"channel_id\",\n c.name as \"name\",\n c.channel_type AS \"channel_type: ChannelType\",\n c.org_id as \"org_id\",\n m.id as \"message_id\",\n m.thread_id as \"thread_id\",\n m.sender_id as \"sender_id\",\n m.imported_author,\n m.content as \"content\",\n m.created_at as \"created_at\",\n m.updated_at as \"updated_at\",\n m.deleted_at::timestamptz as \"deleted_at\"\n FROM\n comms_messages m\n JOIN\n comms_channels c on c.\"id\" = m.\"channel_id\"\n WHERE\n m.id = $1\n AND c.id = $2\n ",
"describe": {
"columns": [
{
@@ -52,21 +52,26 @@
},
{
"ordinal": 7,
- "name": "content",
+ "name": "imported_author",
"type_info": "Text"
},
{
"ordinal": 8,
+ "name": "content",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 9,
"name": "created_at",
"type_info": "Timestamptz"
},
{
- "ordinal": 9,
+ "ordinal": 10,
"name": "updated_at",
"type_info": "Timestamptz"
},
{
- "ordinal": 10,
+ "ordinal": 11,
"name": "deleted_at",
"type_info": "Timestamptz"
}
@@ -85,11 +90,12 @@
false,
true,
false,
+ true,
false,
false,
false,
null
]
},
- "hash": "908627bdd27f344ce26c501bb7431b621173c04bfda9678021360817c3f25103"
+ "hash": "f77d0dbbbc6bc89b676be55a9939ffbd908da49d228156dbed63c097fd25d39b"
}
diff --git a/.sqlx/query-f9bb9f0f9ca19b97547a4e3cdd7cfff65270fa16bbd9df7be6cb27b641624930.json b/.sqlx/query-f9bb9f0f9ca19b97547a4e3cdd7cfff65270fa16bbd9df7be6cb27b641624930.json
new file mode 100644
index 0000000000..658d51a574
--- /dev/null
+++ b/.sqlx/query-f9bb9f0f9ca19b97547a4e3cdd7cfff65270fa16bbd9df7be6cb27b641624930.json
@@ -0,0 +1,16 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "INSERT INTO team (id, name, owner_id) VALUES ($1, 'Archive', $3), ($2, 'Foreign', 'macro|[email protected]')",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Uuid",
+ "Text"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "f9bb9f0f9ca19b97547a4e3cdd7cfff65270fa16bbd9df7be6cb27b641624930"
+}
diff --git a/.sqlx/query-f9f08b3339a271ed59f187c186d48ffa2db11396cb13563558d58d04c6b74b3a.json b/.sqlx/query-f9f08b3339a271ed59f187c186d48ffa2db11396cb13563558d58d04c6b74b3a.json
new file mode 100644
index 0000000000..e6c0819afa
--- /dev/null
+++ b/.sqlx/query-f9f08b3339a271ed59f187c186d48ffa2db11396cb13563558d58d04c6b74b3a.json
@@ -0,0 +1,40 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT owner_id, created_at, updated_at, team_id FROM comms_channels WHERE id = $1",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "owner_id",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 1,
+ "name": "created_at",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 2,
+ "name": "updated_at",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 3,
+ "name": "team_id",
+ "type_info": "Uuid"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ false,
+ false,
+ false,
+ true
+ ]
+ },
+ "hash": "f9f08b3339a271ed59f187c186d48ffa2db11396cb13563558d58d04c6b74b3a"
+}
diff --git a/.sqlx/query-fcf137663dd5235fcac50d9d59e315fb0eedbe6960718bca269581bef3881a2f.json b/.sqlx/query-fcf137663dd5235fcac50d9d59e315fb0eedbe6960718bca269581bef3881a2f.json
new file mode 100644
index 0000000000..fe1621b177
--- /dev/null
+++ b/.sqlx/query-fcf137663dd5235fcac50d9d59e315fb0eedbe6960718bca269581bef3881a2f.json
@@ -0,0 +1,14 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "UPDATE comms_channel_participants SET left_at = NULL WHERE channel_id = $1",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "fcf137663dd5235fcac50d9d59e315fb0eedbe6960718bca269581bef3881a2f"
+}
diff --git a/.sqlx/query-fe287fb5b7cf1346a3ce18b91694be599181321febba7e87364c463ef44bc0ec.json b/.sqlx/query-fe287fb5b7cf1346a3ce18b91694be599181321febba7e87364c463ef44bc0ec.json
new file mode 100644
index 0000000000..0d85b3999f
--- /dev/null
+++ b/.sqlx/query-fe287fb5b7cf1346a3ce18b91694be599181321febba7e87364c463ef44bc0ec.json
@@ -0,0 +1,22 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT EXISTS(SELECT 1 FROM pg_stat_activity WHERE application_name = $1 AND wait_event = 'advisory')",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "exists",
+ "type_info": "Bool"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Text"
+ ]
+ },
+ "nullable": [
+ null
+ ]
+ },
+ "hash": "fe287fb5b7cf1346a3ce18b91694be599181321febba7e87364c463ef44bc0ec"
+}
diff --git a/.sqlx/query-fee0e22fe6c75d31138f3adddaef07525ee7ae1ea5772f9e33d7329c97656540.json b/.sqlx/query-fee0e22fe6c75d31138f3adddaef07525ee7ae1ea5772f9e33d7329c97656540.json
new file mode 100644
index 0000000000..5372856a9a
--- /dev/null
+++ b/.sqlx/query-fee0e22fe6c75d31138f3adddaef07525ee7ae1ea5772f9e33d7329c97656540.json
@@ -0,0 +1,20 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT count(*) FROM comms_activity",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "count",
+ "type_info": "Int8"
+ }
+ ],
+ "parameters": {
+ "Left": []
+ },
+ "nullable": [
+ null
+ ]
+ },
+ "hash": "fee0e22fe6c75d31138f3adddaef07525ee7ae1ea5772f9e33d7329c97656540"
+}
diff --git a/.sqlx/query-ff4f9706948ceb43f91a086786fc478181bf3cd54dbcef505e9407a4350d0538.json b/.sqlx/query-ff4f9706948ceb43f91a086786fc478181bf3cd54dbcef505e9407a4350d0538.json
new file mode 100644
index 0000000000..adf271abfc
--- /dev/null
+++ b/.sqlx/query-ff4f9706948ceb43f91a086786fc478181bf3cd54dbcef505e9407a4350d0538.json
@@ -0,0 +1,16 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n INSERT INTO comms_messages (id, channel_id, sender_id, content, imported_author)\n VALUES ($1, $2, 'macro|[email protected]', 'Historical content', $3)\n ON CONFLICT (id) DO UPDATE SET imported_author = EXCLUDED.imported_author\n ",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Uuid",
+ "Text"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "ff4f9706948ceb43f91a086786fc478181bf3cd54dbcef505e9407a4350d0538"
+}
diff --git a/Cargo.lock b/Cargo.lock
index 259668457c..7884c69330 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -5962,6 +5962,7 @@ dependencies = [
"harnesses",
"hex",
"http-body-util",
+ "import",
"initiative",
"initiative_documents",
"jsonwebtoken 10.3.0",
@@ -6025,6 +6026,7 @@ dependencies = [
"secretsmanager_client",
"serde",
"serde_json",
+ "slack_integration",
"soup",
"soup_realtime",
"sqlx",
@@ -11598,6 +11600,7 @@ dependencies = [
"serde",
"serde_json",
"thiserror 2.0.18",
+ "url",
"workspace-hack",
]
@@ -15807,6 +15810,7 @@ dependencies = [
"anyhow",
"model_owner",
"urlencoding",
+ "uuid",
"workspace-hack",
]
@@ -17064,6 +17068,77 @@ version = "0.4.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0c790de23124f9ab44544d7ac05d60440adc586479ce501c1d6d7da3cd8c9cf5"
+[[package]]
+name = "slack_import_worker"
+version = "0.1.0"
+dependencies = [
+ "aws-sdk-s3",
+ "aws-sdk-sqs",
+ "bot_id",
+ "channel_sender",
+ "channels",
+ "chrono",
+ "database_env_vars",
+ "entity_access",
+ "import",
+ "macro_aws_config",
+ "macro_config",
+ "macro_entrypoint",
+ "macro_queues",
+ "macro_service_urls",
+ "macro_user_id",
+ "mention_utils",
+ "messages",
+ "reqwest 0.13.4",
+ "rootcause",
+ "serde",
+ "serde_json",
+ "sha2 0.10.9",
+ "slack_integration",
+ "sqlx",
+ "tokio",
+ "tokio-util",
+ "uuid",
+ "workspace-hack",
+]
+
+[[package]]
+name = "slack_integration"
+version = "0.1.0"
+dependencies = [
+ "aws-sdk-s3",
+ "aws-sdk-sqs",
+ "axum",
+ "base64 0.22.1",
+ "chrono",
+ "connection_gateway_client",
+ "entity_access",
+ "futures",
+ "import",
+ "macro_authorization",
+ "macro_aws_config",
+ "macro_queues",
+ "macro_user_id",
+ "mention_utils",
+ "model_user",
+ "reqwest 0.13.4",
+ "rootcause",
+ "s3_key",
+ "serde",
+ "serde_json",
+ "sha2 0.10.9",
+ "sqlx",
+ "thiserror 2.0.18",
+ "tokio",
+ "tokio-util",
+ "tower 0.5.3",
+ "tracing",
+ "url",
+ "utoipa",
+ "uuid",
+ "workspace-hack",
+]
+
[[package]]
name = "smallvec"
version = "1.15.1"
diff --git a/Cargo.toml b/Cargo.toml
index 86cca1c8ef..ee1fa5469a 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -91,6 +91,7 @@ members = [
"crates/reminders",
"crates/search_service",
"crates/skills",
+ "crates/slack_integration",
"crates/soup_filter_cache_adapter",
"crates/soup_filter_projection",
"crates/soup_realtime",
@@ -136,6 +137,7 @@ members = [
"services/search_processing_service",
"services/search_upload_handler",
"services/sha_cleanup_worker",
+ "services/slack_import_worker",
"services/static_file_service",
"services/sync-service",
"services/sync-service/bebop-schema",
diff --git a/apps/web/package.json b/apps/web/package.json
index b234ae7573..355f3695ea 100644
--- a/apps/web/package.json
+++ b/apps/web/package.json
@@ -109,6 +109,7 @@
"@standard-schema/spec": "^1.1.0",
"@tailwindcss/vite": "^4.2.4",
"@tanstack/pacer": "^0.17.3",
+ "@tanstack/query-core": "5.90.2",
"@tanstack/solid-query": "5.90.3",
"@tanstack/solid-virtual": "3.13.37",
"@tauri-apps/api": "^2.8.0",
diff --git a/apps/web/scripts/generate-slack-emoji-map.ts b/apps/web/scripts/generate-slack-emoji-map.ts
new file mode 100644
index 0000000000..2c987d951c
--- /dev/null
+++ b/apps/web/scripts/generate-slack-emoji-map.ts
@@ -0,0 +1,75 @@
+import GithubShortcodes from 'emojibase-data/en/shortcodes/github.json';
+import SlackShortcodes from 'emojibase-data/en/shortcodes/iamcal.json';
+import EmojibasePackage from 'emojibase-data/package.json';
+import OrderedEmojiData from 'unicode-emoji-json/data-ordered-emoji.json';
+import UnicodePackage from 'unicode-emoji-json/package.json';
+
+// Pinned provenance and licenses: crates/slack_integration/data/README.md.
+if (
+ EmojibasePackage.version !== '17.0.0' ||
+ UnicodePackage.version !== '0.8.0'
+) {
+ throw new Error(
+ 'Emoji package versions changed; review the dataset and provenance'
+ );
+}
+
+const outputPath = new URL(
+ '../../../crates/slack_integration/data/emoji-shortcodes.json',
+ import.meta.url
+);
+
+function canonicalKey(codepoints: string[]): string {
+ return codepoints
+ .map((hex) => Number.parseInt(hex, 16))
+ .filter((point) => point !== 0xfe0f)
+ .join('-');
+}
+
+const canonicalEmoji = new Map(
+ OrderedEmojiData.map((emoji) => [
+ canonicalKey([...emoji].map((char) => char.codePointAt(0)!.toString(16))),
+ emoji,
+ ])
+);
+const shortcodes = new Map<string, string>();
+// Slack/iamcal wins conflicts (notably :email:); GitHub adds compatible aliases.
+for (const source of [GithubShortcodes, SlackShortcodes]) {
+ for (const [hexcode, names] of Object.entries(source)) {
+ const emoji = canonicalEmoji.get(canonicalKey(hexcode.split('-')));
+ if (
+ !emoji ||
+ [...emoji].some((char) => {
+ const point = char.codePointAt(0)!;
+ return point >= 0x1f3fb && point <= 0x1f3ff;
+ })
+ ) {
+ continue;
+ }
+ for (const name of Array.isArray(names) ? names : [names]) {
+ shortcodes.set(name, emoji);
+ }
+ }
+}
+
+// Stable ASCII JSON is easy to diff and does not depend on locale ordering.
+const output = `${JSON.stringify(
+ Object.fromEntries(
+ [...shortcodes.keys()].sort().map((name) => [name, shortcodes.get(name)])
+ ),
+ null,
+ 2
+).replace(
+ /[\u007f-\uffff]/g,
+ (char) => `\\u${char.charCodeAt(0).toString(16).padStart(4, '0')}`
+)}\n`;
+
+if (Bun.argv.includes('--check')) {
+ if ((await Bun.file(outputPath).text()) !== output) {
+ throw new Error('Stale Slack emoji map; rerun this script without --check');
+ }
+ console.log(`Checked ${shortcodes.size} Slack shortcodes`);
+} else {
+ await Bun.write(outputPath, output);
+ console.log(`Wrote ${shortcodes.size} Slack shortcodes`);
+}
diff --git a/apps/web/src/features/settings/Team.tsx b/apps/web/src/features/settings/Team.tsx
index 83efb13143..407261f42b 100644
--- a/apps/web/src/features/settings/Team.tsx
+++ b/apps/web/src/features/settings/Team.tsx
@@ -1,4 +1,5 @@
import { PLAN_BY_TIER, PLANS } from '@app/features/paywall/plans';
+import { SlackImport } from '@app/features/slack-import/slack-import';
import { toast } from '@core/component/Toast/Toast';
import {
getLinkShareScope,
@@ -1486,6 +1487,7 @@ function TeamManagement(props: {
</SettingsSection>
<SettingsSection title="Connections">
+ <SlackImport teamId={props.teamId} isAdmin={isAdminOrOwner()} />
<SettingsCard>
<IntegrationRow
icon={<GithubIcon />}
diff --git a/apps/web/src/features/slack-import/components/conversation-picker.tsx b/apps/web/src/features/slack-import/components/conversation-picker.tsx
new file mode 100644
index 0000000000..32944bcca6
--- /dev/null
+++ b/apps/web/src/features/slack-import/components/conversation-picker.tsx
@@ -0,0 +1,172 @@
+import { For, type JSX, onMount, Show } from 'solid-js';
+import type { ArchiveDiscovery, ConversationKind } from '../core/export';
+
+type Props = {
+ discovery: ArchiveDiscovery;
+ selected: ReadonlySet<string>;
+ unsupported: ReadonlySet<string>;
+ filter: string;
+ showArchived: boolean;
+ onFilter(value: string): void;
+ onShowArchived(value: boolean): void;
+ onSelect(ids: string[], selected: boolean): void;
+};
+
+const groups: { kind: ConversationKind; label: string }[] = [
+ { kind: 'public_channel', label: 'Public Slack channels → Team channels' },
+ { kind: 'private_channel', label: 'Private channels' },
+ { kind: 'direct_message', label: 'Direct messages' },
+ { kind: 'group_direct_message', label: 'Group DMs → Private channels' },
+];
+
+export function ConversationPicker(props: Props): JSX.Element {
+ let search: HTMLInputElement | undefined;
+ // Discovery replaces the disabled file input inside an already-open dialog.
+ onMount(() => search?.focus());
+ const visible = () =>
+ props.discovery.conversations.filter(
+ (conversation) =>
+ (props.showArchived || !conversation.archived) &&
+ `${conversation.name} ${conversation.slackChannelId}`
+ .toLowerCase()
+ .includes(props.filter.toLowerCase())
+ );
+ const selectable = () =>
+ visible().filter(
+ (conversation) => !props.unsupported.has(conversation.slackChannelId)
+ );
+ const allSelected = () =>
+ selectable().length > 0 &&
+ selectable().every((conversation) =>
+ props.selected.has(conversation.slackChannelId)
+ );
+
+ return (
+ <section class="flex flex-col gap-3" aria-label="Conversations">
+ <label class="flex flex-col gap-1 text-sm">
+ Filter conversations
+ <input
+ ref={search}
+ type="search"
+ class="rounded border border-edge-muted bg-input p-2 text-ink"
+ value={props.filter}
+ onInput={(event) => props.onFilter(event.currentTarget.value)}
+ />
+ </label>
+ <label class="flex items-center gap-2 text-sm">
+ <input
+ type="checkbox"
+ checked={props.showArchived}
+ onChange={(event) =>
+ props.onShowArchived(event.currentTarget.checked)
+ }
+ />
+ Show archived conversations
+ </label>
+ <label class="flex items-center gap-2 text-sm">
+ <input
+ type="checkbox"
+ checked={allSelected()}
+ disabled={!selectable().length}
+ onChange={(event) =>
+ props.onSelect(
+ selectable().map((conversation) => conversation.slackChannelId),
+ event.currentTarget.checked
+ )
+ }
+ />
+ Select all visible conversations
+ </label>
+ <button
+ type="button"
+ class="self-start text-sm underline disabled:opacity-50"
+ disabled={
+ !selectable().some((conversation) =>
+ props.selected.has(conversation.slackChannelId)
+ )
+ }
+ onClick={() =>
+ props.onSelect(
+ selectable().map((conversation) => conversation.slackChannelId),
+ false
+ )
+ }
+ >
+ Clear visible selection
+ </button>
+ <p class="text-sm text-ink-muted">
+ {props.selected.size} selected. Filtering does not clear selections.
+ Message counts are not yet counted.
+ </p>
+ <div class="max-h-72 overflow-y-auto rounded border border-edge-muted p-3">
+ <Show
+ when={visible().length}
+ fallback={
+ <p class="text-sm text-ink-muted">No matching conversations.</p>
+ }
+ >
+ <For each={groups}>
+ {(group) => (
+ <Show
+ when={visible().some(
+ (conversation) => conversation.kind === group.kind
+ )}
+ >
+ <fieldset class="mb-3 min-w-0">
+ <legend class="text-sm font-medium">{group.label}</legend>
+ <For
+ each={visible().filter(
+ (conversation) => conversation.kind === group.kind
+ )}
+ >
+ {(conversation) => (
+ <label class="flex items-start gap-2 py-2 text-sm">
+ <input
+ type="checkbox"
+ class="mt-1 shrink-0"
+ checked={props.selected.has(
+ conversation.slackChannelId
+ )}
+ disabled={props.unsupported.has(
+ conversation.slackChannelId
+ )}
+ onChange={(event) =>
+ props.onSelect(
+ [conversation.slackChannelId],
+ event.currentTarget.checked
+ )
+ }
+ />
+ <span class="min-w-0 break-words">
+ {conversation.name || conversation.slackChannelId}
+ <span class="block text-xs text-ink-muted">
+ {conversation.slackChannelId} ·{' '}
+ {conversation.memberIds.length} source members ·{' '}
+ {conversation.messageCount == null
+ ? 'Messages not yet counted'
+ : `${conversation.messageCount} messages`}
+ {conversation.archived ? ' · Archived' : ''}
+ </span>
+ <Show
+ when={props.unsupported.has(
+ conversation.slackChannelId
+ )}
+ >
+ <span class="block text-xs text-ink-muted">
+ Unavailable: a DM requires exactly two distinct
+ members with email addresses.
+ </span>
+ </Show>
+ </span>
+ </label>
+ )}
+ </For>
+ </fieldset>
+ </Show>
+ )}
+ </For>
+ </Show>
+ </div>
+ </section>
+ );
+}
diff --git a/apps/web/src/features/slack-import/components/import-progress.tsx b/apps/web/src/features/slack-import/components/import-progress.tsx
new file mode 100644
index 0000000000..d3f3333c62
--- /dev/null
+++ b/apps/web/src/features/slack-import/components/import-progress.tsx
@@ -0,0 +1,148 @@
+import { For, Index, type JSX, Show } from 'solid-js';
+import type {
+ ImportConversation,
+ ImportJob,
+ ImportUploadProgress,
+} from '../context/contracts';
+
+type Props = {
+ job?: ImportJob;
+ phase?: string;
+ upload?: ImportUploadProgress;
+ /** Undefined unless the host can confirm current access. */
+ channelHref(id: string): string | undefined;
+};
+
+function label(value: string): string {
+ return value.replaceAll('_', ' ');
+}
+
+function ConversationProgress(
+ props: Pick<Props, 'channelHref'> & {
+ conversation: ImportConversation;
+ }
+): JSX.Element {
+ const conversation = () => props.conversation;
+ const href = (): string | undefined => {
+ const { status, channelId } = conversation();
+ if (status === 'skipped' || !channelId) return undefined;
+ return props.channelHref(channelId);
+ };
+ return (
+ <article class="min-w-0 rounded border border-edge-muted p-3">
+ <h4 class="break-words font-medium">
+ {conversation().name || conversation().slackChannelId}
+ </h4>
+ <p class="text-xs text-ink-muted">
+ {conversation().slackChannelId} · {label(conversation().kind)}
+ {conversation().archived ? ' · Archived' : ''}
+ </p>
+ <p>
+ {label(conversation().status)} · {conversation().verifiedParts} /{' '}
+ {conversation().partCount ?? '?'} parts verified
+ </p>
+ <p class="text-ink-muted">
+ {conversation().counters.processed} processed ·{' '}
+ {conversation().counters.imported} imported ·{' '}
+ {conversation().counters.duplicates} duplicates ·{' '}
+ {conversation().counters.skipped} skipped ·{' '}
+ {conversation().counters.reactions} reactions
+ </p>
+ <p class="text-xs text-ink-muted">
+ Search: {label(conversation().searchStatus)}
+ </p>
+ <Show when={conversation().error}>
+ {(error) => <p role="alert">Error: {label(error())}</p>}
+ </Show>
+ <For each={conversation().warnings}>
+ {(warning) => <p class="text-xs text-ink-muted">{label(warning)}</p>}
+ </For>
+ <Show
+ when={
+ conversation().status === 'skipped' &&
+ !conversation().warnings.length &&
+ !conversation().error
+ }
+ >
+ <p class="text-xs text-ink-muted">
+ Skipped by the server; no additional reason provided.
+ </p>
+ </Show>
+ <Show when={href()}>
+ {(href) => (
+ <a class="inline-block mt-2 underline" href={href()}>
+ Open channel
+ </a>
+ )}
+ </Show>
+ </article>
+ );
+}
+
+export function ImportProgress(props: Props): JSX.Element {
+ return (
+ <section class="flex flex-col gap-3 text-sm" aria-label="Import progress">
+ <p role="status">{label(props.job?.status ?? props.phase ?? 'idle')}</p>
+ <Show when={props.upload}>
+ {(upload) => (
+ <div class="flex flex-col gap-1">
+ <label for="slack-upload-progress">
+ Upload: {upload().loaded.toLocaleString()} /{' '}
+ {upload().total.toLocaleString()} bytes prepared so far
+ </label>
+ <progress
+ id="slack-upload-progress"
+ class="w-full"
+ max={Math.max(1, upload().total)}
+ value={upload().indeterminate ? undefined : upload().loaded}
+ />
+ <p class="text-xs text-ink-muted">
+ The total grows as history is read. File and attachment bytes are
+ never uploaded.
+ </p>
+ </div>
+ )}
+ </Show>
+ <Show when={props.job}>
+ {(job) => (
+ <>
+ <p class="break-all text-xs text-ink-muted">
+ Job {job().jobId} · {job().createdAt}
+ </p>
+ <p>
+ {job().conversations.length} selected ·{' '}
+ {job().includeMessageHistory
+ ? 'With message history'
+ : 'Without message history'}
+ </p>
+ <p class="text-xs text-ink-muted">
+ Source:{' '}
+ {props.job?.source.kind === 'known'
+ ? props.job.source.sourceId
+ : 'Confirmed unknown workspace'}
+ </p>
+ <Show
+ when={
+ job().status === 'cancelling' || job().status === 'cancelled'
+ }
+ >
+ <p>
+ Cancellation is not rollback. Committed messages and channels
+ remain; running conversations may finish before cancellation
+ settles.
+ </p>
+ </Show>
+ <Index each={job().conversations}>
+ {(conversation) => (
+ <ConversationProgress
+ conversation={conversation()}
+ channelHref={props.channelHref}
+ />
+ )}
+ </Index>
+ </>
+ )}
+ </Show>
+ </section>
+ );
+}
diff --git a/apps/web/src/features/slack-import/components/slack-import-card.tsx b/apps/web/src/features/slack-import/components/slack-import-card.tsx
new file mode 100644
index 0000000000..d1abfe167a
--- /dev/null
+++ b/apps/web/src/features/slack-import/components/slack-import-card.tsx
@@ -0,0 +1,22 @@
+import SlackIcon from '@icon/mcp-slack.svg';
+import { Button } from '@ui';
+import type { JSX } from 'solid-js';
+import { IntegrationRow, SettingsCard } from '../../settings/primitives';
+
+type Props = { onOpen(trigger: HTMLButtonElement): void };
+
+export function SlackImportCard(props: Props): JSX.Element {
+ return (
+ <SettingsCard>
+ <IntegrationRow
+ icon={<SlackIcon />}
+ title="Import from Slack"
+ description="Import conversations from a Slack export, with optional message history."
+ >
+ <Button onClick={(event) => props.onOpen(event.currentTarget)}>
+ Import from Slack
+ </Button>
+ </IntegrationRow>
+ </SettingsCard>
+ );
+}
diff --git a/apps/web/src/features/slack-import/context/contracts.ts b/apps/web/src/features/slack-import/context/contracts.ts
new file mode 100644
index 0000000000..abe5aab876
--- /dev/null
+++ b/apps/web/src/features/slack-import/context/contracts.ts
@@ -0,0 +1,182 @@
+import type { Accessor } from 'solid-js';
+import type {
+ ArchiveDiscovery,
+ ArchiveLimits,
+ ConversationMetadata,
+} from '../core/export';
+import type {
+ ConversationSeal,
+ PartDescriptor,
+ PartLimits,
+} from '../core/worker-protocol';
+
+export type ImportJobStatus =
+ | 'uploading'
+ | 'processing'
+ | 'completed'
+ | 'completed_with_errors'
+ | 'failed'
+ | 'cancelling'
+ | 'cancelled';
+
+export type ImportLimits = ArchiveLimits &
+ PartLimits & { registrationBatch: number };
+export type ImportSourceIdentity =
+ | { kind: 'known'; sourceId: string }
+ | { kind: 'confirmed_unknown' };
+export type ImportSourceBinding = ImportSourceIdentity | { kind: 'unbound' };
+
+export type ImportConversation = {
+ slackChannelId: string;
+ name: string;
+ kind: ConversationMetadata['kind'];
+ archived: boolean;
+ channelId: string | undefined;
+ status:
+ | 'awaiting_uploads'
+ | 'queued'
+ | 'importing'
+ | 'completed'
+ | 'skipped'
+ | 'failed';
+ counters: {
+ processed: number;
+ imported: number;
+ duplicates: number;
+ skipped: number;
+ reactions: number;
+ };
+ partCount: number | undefined;
+ verifiedParts: number;
+ searchStatus: 'not_needed' | 'pending' | 'submitted' | 'completed' | 'failed';
+ error: string | undefined;
+ warnings: readonly string[];
+};
+
+export type ImportJob = {
+ source: ImportSourceIdentity;
+ includeMessageHistory: boolean;
+ teamId: string;
+ jobId: string;
+ status: ImportJobStatus;
+ revision: number;
+ createdAt: string;
+ updatedAt: string;
+ registrationClosed: boolean;
+ usersVerified: boolean;
+ limits: ImportLimits;
+ conversations: readonly ImportConversation[];
+};
+
+export type ImportPage = {
+ jobs: readonly ImportJob[];
+ limits: ImportLimits;
+ sourceBinding: ImportSourceBinding;
+ nextCursor: string | undefined;
+};
+
+export type ImportSourceInputs = {
+ teamId: Accessor<string | undefined>;
+ jobId: Accessor<string | undefined>;
+ /** Includes the host's admin/feature gate. Disabled sources expose no cached receipts. */
+ enabled: Accessor<boolean>;
+ before?: Accessor<string | undefined>;
+};
+
+export type ImportSource = {
+ /** Undefined until a receipt for the current enabled scope is available. */
+ page: Accessor<ImportPage | undefined>;
+ job: Accessor<ImportJob | undefined>;
+ isLoading: Accessor<boolean>;
+ /** A background error may coexist with available data. */
+ error: Accessor<Error | undefined>;
+ refresh(): Promise<void>;
+};
+
+export type ImportScope = { teamId: string; jobId: string };
+export type CreateImport = {
+ idempotencyToken: string;
+ includeMessageHistory: boolean;
+ source: ImportSourceIdentity;
+ conversations: ConversationMetadata[];
+};
+export type ImportUploadId =
+ | { kind: 'users' }
+ | { kind: 'conversation_part'; slackChannelId: string; partIndex: number };
+export type ImportUploadDescriptor = {
+ upload: ImportUploadId;
+ sha256: string;
+ byteLength: number;
+ recordCount?: number | null;
+};
+/** Bytes for parts prepared so far, not the compressed ZIP size. */
+export type ImportUploadProgress = {
+ loaded: number;
+ total: number;
+ indeterminate: boolean;
+};
+
+export type ImportUploadOptions = {
+ signal?: AbortSignal;
+ onProgress?: (
+ progress:
+ | { kind: 'bytes'; loaded: number; total: number }
+ | { kind: 'indeterminate'; total: number }
+ ) => void;
+};
+
+export class ImportUploadError extends Error {
+ constructor(
+ public readonly code:
+ | 'ABORTED'
+ | 'EXPIRED'
+ | 'SIZE_MISMATCH'
+ | 'INVALID_GRANT'
+ | 'NETWORK_ERROR'
+ | 'HTTP_ERROR',
+ public readonly status?: number
+ ) {
+ super(`Slack import upload failed: ${code}`);
+ this.name = 'ImportUploadError';
+ }
+}
+
+/** A bounded, short-lived capability, not a transport URL or storage key. */
+export type ImportUpload = {
+ descriptor: ImportUploadDescriptor;
+ expiresAt: string;
+ put(
+ blob: Blob,
+ options?: ImportUploadOptions
+ ): Promise<'uploaded' | 'already-exists'>;
+};
+
+/** One selected file/session. Callbacks are backpressured and must finish before proceeding. */
+export type ArchiveSource = {
+ discover(file: Blob, limits: ArchiveLimits): Promise<ArchiveDiscovery>;
+ prepare(options: {
+ selectedIds: string[];
+ includeMessageHistory: boolean;
+ partLimits: PartLimits;
+ part(descriptor: PartDescriptor, blob: Blob): Promise<void>;
+ seal(seal: ConversationSeal): Promise<void>;
+ }): Promise<void>;
+ /** Stops pending work, rejects outstanding requests and deletes scratch storage. */
+ dispose(): Promise<void>;
+};
+
+export type ImportCommands = {
+ create(teamId: string, request: CreateImport): Promise<ImportJob>;
+ register(
+ scope: ImportScope,
+ descriptors: ImportUploadDescriptor[]
+ ): Promise<ImportUpload[]>;
+ /** Required after either PUT outcome; seals may be submitted with no uploads. */
+ complete(
+ scope: ImportScope,
+ uploads: ImportUploadId[],
+ seal?: ConversationSeal
+ ): Promise<ImportJob>;
+ finalize(scope: ImportScope): Promise<ImportJob>;
+ cancel(scope: ImportScope): Promise<ImportJob>;
+};
diff --git a/apps/web/src/features/slack-import/context/import-context.tsx b/apps/web/src/features/slack-import/context/import-context.tsx
new file mode 100644
index 0000000000..a61720ad2b
--- /dev/null
+++ b/apps/web/src/features/slack-import/context/import-context.tsx
@@ -0,0 +1,34 @@
+import { createContext, type JSX, useContext } from 'solid-js';
+import type {
+ ArchiveSource,
+ ImportCommands,
+ ImportSource,
+ ImportSourceInputs,
+} from './contracts';
+
+export type ImportContext = {
+ createSource(inputs: ImportSourceInputs): {
+ source: ImportSource;
+ commands: ImportCommands;
+ };
+ createArchive(): ArchiveSource;
+ protectFile(): () => void;
+ newToken(): string;
+};
+
+const Context = createContext<ImportContext>();
+
+export function ImportProvider(props: {
+ context: ImportContext;
+ children: JSX.Element;
+}): JSX.Element {
+ return (
+ <Context.Provider value={props.context}>{props.children}</Context.Provider>
+ );
+}
+
+export function useImportContext(): ImportContext {
+ const context = useContext(Context);
+ if (!context) throw new Error('ImportProvider is required');
+ return context;
+}
diff --git a/apps/web/src/features/slack-import/core/export.test.ts b/apps/web/src/features/slack-import/core/export.test.ts
new file mode 100644
index 0000000000..6d0725c867
--- /dev/null
+++ b/apps/web/src/features/slack-import/core/export.test.ts
@@ -0,0 +1,190 @@
+// @vitest-environment node
+import { describe, expect, it } from 'vitest';
+import {
+ corporateEntries,
+ enterpriseEntries,
+ standardEntries,
+ USERS,
+} from '../tests/zip-fixtures';
+import {
+ type ArchiveDiscovery,
+ parseDay,
+ resolveExport,
+ validateArchivePath,
+} from './export';
+
+function resolve(entries: Record<string, unknown>): ArchiveDiscovery {
+ return resolveExport(
+ new Map(Object.entries(entries).filter(([path]) => !path.includes('/'))),
+ Object.keys(entries).filter((path) => path.includes('/'))
+ );
+}
+
+describe('Slack export metadata', () => {
+ it('preserves complete metadata and explicitly unknown counts/source', () => {
+ const discovery = resolve(standardEntries());
+ expect(discovery.source).toEqual({ kind: 'unknown' });
+ expect(discovery.conversations).toEqual([
+ {
+ slackChannelId: 'C100',
+ kind: 'public_channel',
+ name: 'general',
+ folder: 'general',
+ memberIds: ['U100', 'U200'],
+ creatorId: null,
+ createdAt: '1700000000.000000',
+ archived: false,
+ messageCount: null,
+ },
+ ]);
+ expect(discovery.users[0].profile?.email).toBe('[email protected]');
+ });
+
+ it('uses file kind, not ID prefix; missing creation dates remain null', () => {
+ expect(
+ resolve(corporateEntries()).conversations.map((item) => [
+ item.kind,
+ item.createdAt,
+ ])
+ ).toEqual([
+ ['private_channel', null],
+ ['direct_message', null],
+ ['group_direct_message', null],
+ ]);
+ });
+
+ it('accepts Enterprise users without mistaking user teams for archive identity', () => {
+ const discovery = resolve(enterpriseEntries());
+ expect(discovery.users[0].id).toBe('W100');
+ expect(discovery.source).toEqual({ kind: 'unknown' });
+ expect(
+ resolve({ ...enterpriseEntries(), 'team.json': { id: 'T100' } }).source
+ ).toEqual({ kind: 'known', sourceId: 'T100' });
+ });
+
+ it('merges identical users but rejects conflicting records across both filenames', () => {
+ expect(
+ resolve({ ...standardEntries(), 'org_users.json': USERS }).users
+ ).toHaveLength(2);
+ expect(() =>
+ resolve({
+ ...standardEntries(),
+ 'org_users.json': [{ id: 'U100', name: 'different' }],
+ })
+ ).toThrow('conflicting');
+ });
+
+ it('resolves renamed channels by ID or previous-name metadata, never by guessing', () => {
+ const roots = new Map<string, unknown>([
+ ['users.json', USERS],
+ [
+ 'channels.json',
+ [
+ {
+ id: 'C100',
+ name: 'renamed',
+ previous_names: ['general'],
+ members: [],
+ },
+ ],
+ ],
+ ]);
+ expect(
+ resolveExport(roots, ['general/2020-01-01.json']).conversations[0].folder
+ ).toBe('general');
+ expect(
+ resolveExport(roots, ['C100/2020-01-01.json']).conversations[0].folder
+ ).toBe('C100');
+ expect(() => resolveExport(roots, ['unknown/2020-01-01.json'])).toThrow(
+ 'cannot be resolved'
+ );
+ expect(() =>
+ resolveExport(roots, ['general/2020-01-01.json', 'C100/2020-01-01.json'])
+ ).toThrow('matches');
+ });
+
+ it('rejects shared folder names and duplicate conversation IDs', () => {
+ const entries = standardEntries();
+ entries['groups.json'] = [{ id: 'G100', name: 'general', members: [] }];
+ expect(() => resolve(entries)).toThrow('matches');
+ entries['groups.json'] = [{ id: 'C100', name: 'other', members: [] }];
+ expect(() => resolve(entries)).toThrow('conflicting');
+ });
+
+ it('requires users, conversation metadata and full member lists', () => {
+ expect(() => resolve({ 'channels.json': [] })).toThrow('requires');
+ expect(() => resolve({ 'users.json': [] })).toThrow('requires');
+ expect(() =>
+ resolve({
+ 'users.json': [],
+ 'channels.json': [{ id: 'C100', name: 'general' }],
+ })
+ ).toThrow('invalid');
+ });
+
+ it.each(['1700000000.000001', '1700000000.000002'])(
+ 'retains exact creation timestamp %s',
+ (created) => {
+ const entries = {
+ 'users.json': [],
+ 'channels.json': [{ id: 'C100', members: [], created }],
+ };
+ expect(resolve(entries).conversations[0].createdAt).toBe(created);
+ }
+ );
+
+ it.each(['1.0000001', '253402300800', -1, 1.5])(
+ 'rejects invalid creation time %s',
+ (created) => {
+ expect(() =>
+ resolve({
+ 'users.json': [],
+ 'channels.json': [{ id: 'C100', members: [], created }],
+ })
+ ).toThrow('invalid');
+ }
+ );
+});
+
+describe('paths and day JSON', () => {
+ it.each([
+ '../users.json',
+ '/users.json',
+ 'a/../b',
+ 'a\\b',
+ 'a//b',
+ 'a/%2e%2e',
+ 'C:/users.json',
+ 'FC:F100:../users.json',
+ 'FC:F100:notes/../users.json',
+ 'FC:F100:notes/%2e%2e/users.json',
+ 'FC:F100:notes\\users.json',
+ 'FC:F100:notes:stream/2020-01-01.json',
+ 'FC:not-a-file:notes/2020-01-01.json',
+ 'a/\u0000b',
+ 'a/'.repeat(600),
+ ])('rejects unsafe path %j', (path) => {
+ expect(() => validateArchivePath(path)).toThrow('unsafe');
+ });
+ it('allows Unicode folders, spaces and directory entries', () => {
+ expect(() =>
+ validateArchivePath('project café/2020-01-01.json')
+ ).not.toThrow();
+ expect(() => validateArchivePath('project café/')).not.toThrow();
+ expect(() => validateArchivePath('FC:F100:Project café/')).not.toThrow();
+ expect(() =>
+ validateArchivePath('FC:F100:Project café/2020-01-01.json')
+ ).not.toThrow();
+ });
+ it.each(['{', '{}', '[null]', '[1]', '[[]]'])(
+ 'rejects malformed day JSON %s',
+ (text) => {
+ expect(() => parseDay(new TextEncoder().encode(text))).toThrow(
+ 'valid JSON array'
+ );
+ }
+ );
+ it('rejects invalid UTF-8 instead of silently replacing content', () => {
+ expect(() => parseDay(new Uint8Array([0xff]))).toThrow('valid JSON');
+ });
+});
diff --git a/apps/web/src/features/slack-import/core/export.ts b/apps/web/src/features/slack-import/core/export.ts
new file mode 100644
index 0000000000..e79a24a52b
--- /dev/null
+++ b/apps/web/src/features/slack-import/core/export.ts
@@ -0,0 +1,289 @@
+import { z } from 'zod';
+
+/** Browser-owned subset of the effective limits in SLACK_ARCHIVE_IMPORT_CONTRACT.md. */
+export type ArchiveLimits = {
+ jsonBytes: number;
+ selectedBytes: number;
+ zipEntries: number;
+ conversations: number;
+};
+
+export const DEFAULT_ARCHIVE_LIMITS: Readonly<ArchiveLimits> = {
+ jsonBytes: 32 * 1024 * 1024,
+ selectedBytes: 2 * 1024 * 1024 * 1024,
+ zipEntries: 100_000,
+ conversations: 2_000,
+};
+
+export type ArchiveErrorCode =
+ | 'invalid_zip'
+ | 'unsupported_zip'
+ | 'unsafe_path'
+ | 'duplicate_entry'
+ | 'missing_metadata'
+ | 'invalid_metadata'
+ | 'ambiguous_folder'
+ | 'unresolved_folder'
+ | 'invalid_json'
+ | 'json_limit'
+ | 'compressed_limit'
+ | 'selected_limit'
+ | 'entry_limit'
+ | 'invalid_selection'
+ | 'cancelled'
+ | 'invalid_state'
+ | 'invalid_message'
+ | 'record_limit'
+ | 'storage_quota'
+ | 'storage_unavailable';
+
+const ERROR_MESSAGES: Record<ArchiveErrorCode, string> = {
+ invalid_zip: 'The ZIP is damaged or inconsistent.',
+ unsupported_zip:
+ 'Only single-volume, unencrypted ZIPs using Store or Deflate are supported; ZIP64 is not supported.',
+ unsafe_path: 'The ZIP contains an unsafe path.',
+ duplicate_entry: 'The ZIP contains duplicate entry paths.',
+ missing_metadata:
+ 'The archive requires users.json or org_users.json and conversation metadata at its root.',
+ invalid_metadata: 'Slack metadata is invalid or conflicting.',
+ ambiguous_folder:
+ 'More than one metadata entry or folder matches a conversation.',
+ unresolved_folder:
+ 'A history folder cannot be resolved through Slack metadata.',
+ invalid_json: 'A metadata or day file is not a valid JSON array of objects.',
+ json_limit:
+ 'A users, metadata or day JSON file exceeds the configured expanded-byte limit (32 MiB by default).',
+ compressed_limit:
+ 'A JSON entry exceeds the compressed-byte safety limit (twice the JSON limit plus 64 KiB).',
+ selected_limit:
+ 'Selected expanded data exceeds the configured byte limit (2 GiB by default).',
+ entry_limit: 'The ZIP exceeds the configured entry limit.',
+ invalid_selection:
+ 'Select a nonempty, unique set of known conversations within the configured limit.',
+ cancelled: 'Archive reading was cancelled.',
+ invalid_state: 'The archive operation is not valid in the current state.',
+ invalid_message: 'A Slack message has invalid fields or timestamps.',
+ record_limit: 'A normalized message exceeds the record or part byte limit.',
+ storage_quota:
+ 'Browser storage is full. Free disk space and retry the archive.',
+ storage_unavailable:
+ 'Temporary browser storage is unavailable. Retry in a browser with IndexedDB enabled.',
+};
+
+export class ArchiveError extends Error {
+ constructor(public readonly code: ArchiveErrorCode) {
+ super(ERROR_MESSAGES[code]);
+ this.name = 'ArchiveError';
+ }
+}
+
+export function validateArchiveLimits(limits: ArchiveLimits): void {
+ if (Object.values(limits).some((n) => !Number.isSafeInteger(n) || n <= 0)) {
+ throw new ArchiveError('invalid_metadata');
+ }
+}
+
+export function isSafeSegment(value: string): boolean {
+ // biome-ignore lint/suspicious/noControlCharactersInRegex: ZIP paths must reject control characters.
+ const unsafe = /[\\/%\u0000-\u001f\u007f:]/;
+ return (
+ value.length > 0 &&
+ value !== '.' &&
+ value !== '..' &&
+ !unsafe.test(value) &&
+ new TextEncoder().encode(value).length <= 255
+ );
+}
+
+/** Slack exports file/canvas discussions separately from channel history. */
+export function isFileConversationFolder(value: string): boolean {
+ const match = /^FC:F[A-Z0-9]+:(.+)$/.exec(value);
+ return (
+ match !== null &&
+ isSafeSegment(match[1]) &&
+ new TextEncoder().encode(value).length <= 255
+ );
+}
+
+export function validateArchivePath(path: string): void {
+ const segments = path.replace(/\/$/, '').split('/');
+ if (
+ new TextEncoder().encode(path).length > 1024 ||
+ !segments.every(
+ (segment, index) =>
+ isSafeSegment(segment) ||
+ (index === 0 && isFileConversationFolder(segment))
+ )
+ ) {
+ throw new ArchiveError('unsafe_path');
+ }
+}
+
+export const CONVERSATION_FILES = {
+ 'channels.json': 'public_channel',
+ 'groups.json': 'private_channel',
+ 'dms.json': 'direct_message',
+ 'mpims.json': 'group_direct_message',
+} as const;
+
+export type ConversationKind =
+ (typeof CONVERSATION_FILES)[keyof typeof CONVERSATION_FILES];
+export type ConversationMetadata = {
+ slackChannelId: string;
+ kind: ConversationKind;
+ name: string;
+ folder: string;
+ memberIds: string[];
+ creatorId: string | null;
+ createdAt: string | null;
+ archived: boolean;
+ /** Discovery never inflates day files to count messages. */
+ messageCount: null;
+};
+
+const userId = z.string().regex(/^[UW][A-Z0-9]{1,63}$/);
+const profile = z.object({
+ email: z.string().nullable().default(null),
+ display_name: z.string().nullable().default(null),
+ real_name: z.string().nullable().default(null),
+});
+const user = z.object({
+ id: userId,
+ name: z.string().nullable().default(null),
+ real_name: z.string().nullable().default(null),
+ profile: profile.nullable().default(null),
+ is_bot: z.boolean().default(false),
+});
+export type ExportUser = z.infer<typeof user>;
+
+const conversation = z.object({
+ id: z.string().regex(/^[CGD][A-Z0-9]{1,63}$/),
+ name: z.string().nullable().default(null),
+ members: z.array(userId),
+ creator: userId.nullable().default(null),
+ created: z
+ .union([z.number().int().nonnegative().max(253402300799), z.string()])
+ .nullable()
+ .default(null),
+ is_archived: z.boolean().default(false),
+ // Some export tools preserve explicit folder/previous-name metadata after renaming.
+ folder: z.string().optional(),
+ previous_names: z.array(z.string()).default([]),
+});
+
+export type ArchiveDiscovery = {
+ /** Unknown is not the administrator's affirmative confirmed_unknown consent. */
+ source: { kind: 'known'; sourceId: string } | { kind: 'unknown' };
+ conversations: ConversationMetadata[];
+ users: ExportUser[];
+ dayEntries: { path: string; slackChannelId: string }[];
+};
+
+export function parseJson(bytes: Uint8Array): unknown {
+ try {
+ return JSON.parse(new TextDecoder('utf-8', { fatal: true }).decode(bytes));
+ } catch {
+ throw new ArchiveError('invalid_json');
+ }
+}
+
+export function parseDay(bytes: Uint8Array): Record<string, unknown>[] {
+ const value = parseJson(bytes);
+ if (
+ !Array.isArray(value) ||
+ !value.every(
+ (item) =>
+ item !== null && typeof item === 'object' && !Array.isArray(item)
+ )
+ ) {
+ throw new ArchiveError('invalid_json');
+ }
+ return value;
+}
+
+function creationTime(value: string | number | null): string | null {
+ if (value === null) return null;
+ const parts = /^(\d{1,12})(?:\.(\d{1,6}))?$/.exec(String(value));
+ if (!parts || BigInt(parts[1]) > 253402300799n)
+ throw new ArchiveError('invalid_metadata');
+ return `${BigInt(parts[1])}.${(parts[2] ?? '').padEnd(6, '0')}`;
+}
+
+function parseUsers(roots: ReadonlyMap<string, unknown>): ExportUser[] {
+ const users = new Map<string, ExportUser>();
+ if (!roots.has('users.json') && !roots.has('org_users.json'))
+ throw new ArchiveError('missing_metadata');
+ for (const file of ['users.json', 'org_users.json']) {
+ if (!roots.has(file)) continue;
+ const parsed = z.array(user).safeParse(roots.get(file));
+ if (!parsed.success) throw new ArchiveError('invalid_metadata');
+ for (const item of parsed.data) {
+ const existing = users.get(item.id);
+ if (existing && JSON.stringify(existing) !== JSON.stringify(item))
+ throw new ArchiveError('invalid_metadata');
+ users.set(item.id, item);
+ }
+ }
+ return [...users.values()];
+}
+
+/** Pure metadata resolution. Never guess from message bodies or a user's team_id. */
+export function resolveExport(
+ roots: ReadonlyMap<string, unknown>,
+ dayPaths: string[]
+): ArchiveDiscovery {
+ const users = parseUsers(roots);
+ const folders = new Set(dayPaths.map((path) => path.split('/')[0]));
+ const conversations: ConversationMetadata[] = [];
+ const ids = new Set<string>();
+ const folderOwners = new Map<string, string>();
+ let foundMetadata = false;
+ for (const [file, kind] of Object.entries(CONVERSATION_FILES)) {
+ if (!roots.has(file)) continue;
+ foundMetadata = true;
+ const parsed = z.array(conversation).safeParse(roots.get(file));
+ if (!parsed.success) throw new ArchiveError('invalid_metadata');
+ for (const item of parsed.data) {
+ if (ids.has(item.id)) throw new ArchiveError('invalid_metadata');
+ ids.add(item.id);
+ const aliases = new Set(
+ [item.id, item.name, item.folder, ...item.previous_names].filter(
+ (name): name is string => name != null
+ )
+ );
+ if (![...aliases].every(isSafeSegment))
+ throw new ArchiveError('unsafe_path');
+ const matches = [...aliases].filter((alias) => folders.has(alias));
+ if (matches.length > 1) throw new ArchiveError('ambiguous_folder');
+ const folder = matches[0] ?? item.folder ?? item.name ?? item.id;
+ if (folderOwners.has(folder)) throw new ArchiveError('ambiguous_folder');
+ folderOwners.set(folder, item.id);
+ conversations.push({
+ slackChannelId: item.id,
+ kind,
+ name: item.name ?? item.id,
+ folder,
+ memberIds: item.members,
+ creatorId: item.creator,
+ createdAt: creationTime(item.created),
+ archived: item.is_archived,
+ messageCount: null,
+ });
+ }
+ }
+ if (!foundMetadata) throw new ArchiveError('missing_metadata');
+ const dayEntries = dayPaths.map((path) => {
+ const slackChannelId = folderOwners.get(path.split('/')[0]);
+ if (!slackChannelId) throw new ArchiveError('unresolved_folder');
+ return { path, slackChannelId };
+ });
+ let source: ArchiveDiscovery['source'] = { kind: 'unknown' };
+ if (roots.has('team.json')) {
+ const team = z
+ .object({ id: z.string().regex(/^T[A-Z0-9]{1,63}$/) })
+ .safeParse(roots.get('team.json'));
+ if (!team.success) throw new ArchiveError('invalid_metadata');
+ source = { kind: 'known', sourceId: team.data.id };
+ }
+ return { source, conversations, users, dayEntries };
+}
diff --git a/apps/web/src/features/slack-import/core/worker-protocol.ts b/apps/web/src/features/slack-import/core/worker-protocol.ts
new file mode 100644
index 0000000000..4f893541fa
--- /dev/null
+++ b/apps/web/src/features/slack-import/core/worker-protocol.ts
@@ -0,0 +1,74 @@
+import type {
+ ArchiveDiscovery,
+ ArchiveErrorCode,
+ ArchiveLimits,
+} from './export';
+
+export type PartLimits = {
+ partBytes: number;
+ partRecords: number;
+ recordBytes: number;
+};
+
+export type PartDescriptor = {
+ upload: {
+ kind: 'conversation_part';
+ slackChannelId: string;
+ partIndex: number;
+ };
+ sha256: string;
+ byteLength: number;
+ recordCount: number;
+};
+
+export type ConversationSeal = {
+ slackChannelId: string;
+ partCount: number;
+ manifestSha256: string;
+};
+
+/** One archive session per worker; every response echoes the caller's session ID. */
+export type ArchiveWorkerRequest =
+ | {
+ type: 'discover';
+ sessionId: string;
+ archive: Blob;
+ limits?: ArchiveLimits;
+ }
+ | {
+ type: 'read_history';
+ sessionId: string;
+ selectedIds: string[];
+ includeMessageHistory: boolean;
+ partLimits?: PartLimits;
+ }
+ | { type: 'ack_part'; sessionId: string; sequence: number }
+ | { type: 'cancel'; sessionId: string };
+
+export type ArchiveWorkerResponse =
+ | { type: 'discovered'; sessionId: string; discovery: ArchiveDiscovery }
+ | {
+ type: 'part';
+ sessionId: string;
+ sequence: number;
+ descriptor: PartDescriptor;
+ bytes: Uint8Array<ArrayBuffer>;
+ }
+ | { type: 'seal'; sessionId: string; seal: ConversationSeal }
+ | { type: 'complete'; sessionId: string }
+ | { type: 'cancelled'; sessionId: string }
+ | {
+ type: 'error';
+ sessionId: string;
+ code: ArchiveErrorCode;
+ message: string;
+ };
+
+/** A part must be durably consumed before ack_part; at most one part is outstanding.
+ * No network/upload capability belongs in discovery. Use disposeSlackImportWorker
+ * on owner disposal so forced termination also deletes session scratch storage.
+ */
+export type ArchiveWorkerPort = {
+ postMessage(message: ArchiveWorkerResponse, transfer?: Transferable[]): void;
+ onmessage: ((event: MessageEvent<ArchiveWorkerRequest>) => void) | null;
+};
diff --git a/apps/web/src/features/slack-import/primitives/import-controller.test.ts b/apps/web/src/features/slack-import/primitives/import-controller.test.ts
new file mode 100644
index 0000000000..651fc09987
--- /dev/null
+++ b/apps/web/src/features/slack-import/primitives/import-controller.test.ts
@@ -0,0 +1,679 @@
+import { webcrypto } from 'node:crypto';
+import { createRoot } from 'solid-js';
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+import { ImportUploadError } from '../context/contracts';
+import type { ArchiveWorkerResponse } from '../core/worker-protocol';
+import {
+ createArchiveSource,
+ protectImportFile,
+} from '../queries/archive-source';
+import {
+ archiveDiscovery,
+ deferred,
+ fakeImportSources,
+ historyPart,
+ historySeal,
+ importLimits,
+ importReceipt,
+} from '../tests/fake-sources';
+import { createImportController } from './import-controller';
+
+const cleanups: (() => void)[] = [];
+beforeEach(() => {
+ vi.stubGlobal('crypto', webcrypto);
+});
+afterEach(() => {
+ for (const dispose of cleanups.splice(0)) dispose();
+ vi.unstubAllGlobals();
+});
+
+function setup() {
+ return createRoot((dispose) => {
+ cleanups.push(dispose);
+ const sources = fakeImportSources();
+ const controller = createImportController(sources);
+ return { controller, sources, dispose };
+ });
+}
+const selection = { selectedIds: ['C123'], sourceConfirmed: true };
+async function select(
+ controller: ReturnType<typeof createImportController>
+): Promise<void> {
+ await controller.discover(new Blob(['zip']));
+}
+
+describe('import controller', () => {
+ it('reports prepared upload bytes including verified existing objects', async () => {
+ const { controller, sources } = setup();
+ const progress: ReturnType<typeof controller.uploadProgress>[] = [];
+ sources.put.mockImplementation(async (blob, options) => {
+ options?.onProgress?.({ kind: 'indeterminate', total: blob.size });
+ progress.push(controller.uploadProgress());
+ options?.onProgress?.({ kind: 'bytes', loaded: 1, total: blob.size });
+ progress.push(controller.uploadProgress());
+ return 'already-exists';
+ });
+ await select(controller);
+ await controller.start(selection);
+ expect(progress.some((value) => value.indeterminate)).toBe(true);
+ expect(progress.some((value) => value.loaded > 0)).toBe(true);
+ expect(controller.uploadProgress()).toEqual({
+ loaded: 4,
+ total: 4,
+ indeterminate: false,
+ });
+ });
+
+ it('does not upload during discovery or selection, and requires source confirmation', async () => {
+ const { controller, sources } = setup();
+ await select(controller);
+ expect(controller.phase()).toBe('selecting');
+ expect(sources.commands.create).not.toHaveBeenCalled();
+ expect(sources.protectFile).toHaveBeenCalledTimes(1);
+ expect(sources.release).toHaveBeenCalledTimes(1);
+ await expect(
+ controller.start({ ...selection, sourceConfirmed: false })
+ ).rejects.toThrow('Confirm');
+ expect(sources.commands.create).not.toHaveBeenCalled();
+ });
+
+ it('rejects workspace mismatches, unsupported DMs and duplicate selections', async () => {
+ const { controller, sources } = setup();
+ const found = archiveDiscovery();
+ found.source = { kind: 'known', sourceId: 'T123' };
+ found.conversations[0].kind = 'direct_message';
+ sources.archive.discover.mockResolvedValue(found);
+ await select(controller);
+ sources.setBinding({ kind: 'known', sourceId: 'T456' });
+ await expect(controller.start(selection)).rejects.toThrow(
+ 'different Slack workspace'
+ );
+ sources.setBinding({ kind: 'unbound' });
+ expect(controller.skips()).toEqual([
+ { slackChannelId: 'C123', reason: 'unsupported_dm' },
+ ]);
+ await expect(controller.start(selection)).rejects.toThrow('supported');
+ await expect(
+ controller.start({ ...selection, selectedIds: ['C123', 'C123'] })
+ ).rejects.toThrow('supported');
+ expect(sources.commands.create).not.toHaveBeenCalled();
+ });
+
+ it('rejects empty selection and cancels review without any network writes', async () => {
+ const { controller, sources } = setup();
+ await select(controller);
+ await expect(
+ controller.start({ ...selection, selectedIds: [] })
+ ).rejects.toThrow('Select supported');
+ await controller.cancel();
+ expect(sources.newToken).not.toHaveBeenCalled();
+ expect(sources.commands.create).not.toHaveBeenCalled();
+ expect(sources.commands.cancel).not.toHaveBeenCalled();
+ expect(sources.commands.register).not.toHaveBeenCalled();
+ expect(sources.archive.dispose).toHaveBeenCalledOnce();
+ });
+
+ it('does not count unselected unsupported DMs as selected skips', async () => {
+ const { controller, sources } = setup();
+ const found = archiveDiscovery();
+ found.conversations.push({
+ ...found.conversations[0],
+ slackChannelId: 'DB',
+ kind: 'direct_message',
+ });
+ sources.archive.discover.mockResolvedValue(found);
+ await select(controller);
+ expect(controller.skips()).toHaveLength(1);
+ await controller.start(selection);
+ expect(controller.skips()).toEqual([]);
+ expect(
+ controller
+ .job()
+ ?.conversations.map((conversation) => conversation.slackChannelId)
+ ).toEqual(['C123']);
+ });
+
+ it.each(['part', 'seal'] as const)(
+ 'rejects an unselected worker %s before registration/completion',
+ async (kind) => {
+ const { controller, sources } = setup();
+ sources.archive.prepare.mockImplementation(async (options) => {
+ if (kind === 'part')
+ await options.part(
+ {
+ ...historyPart(),
+ upload: {
+ kind: 'conversation_part',
+ slackChannelId: 'CB',
+ partIndex: 0,
+ },
+ },
+ new Blob(['[]'])
+ );
+ else await options.seal({ ...historySeal(0), slackChannelId: 'CB' });
+ });
+ await select(controller);
+ await controller.start(selection);
+ expect(controller.phase()).toBe('interrupted');
+ expect(
+ sources.commands.register.mock.calls.every((call) =>
+ call[1].every((descriptor) => descriptor.upload.kind === 'users')
+ )
+ ).toBe(true);
+ expect(sources.commands.complete).not.toHaveBeenCalled();
+ expect(sources.commands.finalize).not.toHaveBeenCalled();
+ }
+ );
+
+ it('creates once, seals the complete manifest, finalizes and monitors revisions', async () => {
+ const { controller, sources } = setup();
+ await select(controller);
+ await Promise.all([
+ controller.start(selection),
+ controller.start(selection),
+ ]);
+ expect(sources.commands.create).toHaveBeenCalledTimes(1);
+ expect(sources.commands.create.mock.calls[0][1]).toMatchObject({
+ source: { kind: 'confirmed_unknown' },
+ includeMessageHistory: true,
+ });
+ expect(sources.events.indexOf('seal:1')).toBeGreaterThan(
+ sources.events.indexOf('complete:conversation_part')
+ );
+ expect(sources.events.at(-1)).toBe('finalize');
+ expect(controller.phase()).toBe('monitoring');
+ expect(sources.release).toHaveBeenCalledTimes(2);
+ expect(sources.archive.dispose).toHaveBeenCalledTimes(1);
+ sources.setObserved(importReceipt({ revision: 50, status: 'completed' }));
+ expect(controller.phase()).toBe('terminal');
+ sources.setObserved(importReceipt({ revision: 1, status: 'uploading' }));
+ expect(controller.job()?.status).toBe('completed');
+ expect(controller.phase()).toBe('terminal');
+ sources.setObserved(
+ importReceipt({ jobId: 'another', revision: 100, status: 'failed' })
+ );
+ expect(controller.job()?.jobId).toBe('job');
+ });
+
+ it.each([false, true])(
+ 'supports zero-part shape-only/empty history (history=%s)',
+ async (includeMessageHistory) => {
+ const { controller, sources } = setup();
+ sources.archive.prepare.mockImplementation(async (options) => {
+ await options.seal(historySeal(0));
+ });
+ await select(controller);
+ await controller.start({ ...selection, includeMessageHistory });
+ expect(
+ sources.archive.prepare.mock.calls[0][0].includeMessageHistory
+ ).toBe(includeMessageHistory);
+ expect(sources.commands.register).toHaveBeenCalledTimes(1);
+ expect(sources.commands.complete).toHaveBeenCalledWith(
+ { teamId: 'team', jobId: 'job' },
+ [],
+ historySeal(0)
+ );
+ expect(sources.commands.finalize).toHaveBeenCalledTimes(1);
+ }
+ );
+
+ it('waits for users-last readiness and keeps concurrency bounded to two lanes', async () => {
+ const { controller, sources } = setup();
+ const users = deferred<'uploaded'>();
+ let active = 0;
+ let maximum = 0;
+ sources.put.mockImplementation(async () => {
+ active++;
+ maximum = Math.max(maximum, active);
+ if (sources.put.mock.calls.length === 1) await users.promise;
+ active--;
+ return 'uploaded';
+ });
+ sources.archive.prepare.mockImplementation(async (options) => {
+ for (let index = 0; index < 4; index++)
+ await options.part(historyPart(index), new Blob(['[]']));
+ await options.seal(historySeal(4));
+ });
+ await select(controller);
+ const running = controller.start(selection);
+ await vi.waitFor(() => expect(sources.events).toContain('seal:4'));
+ expect(sources.commands.finalize).not.toHaveBeenCalled();
+ expect(sources.protectFile).toHaveBeenCalledTimes(2);
+ expect(sources.release).toHaveBeenCalledTimes(1);
+ expect(maximum).toBe(2);
+ users.resolve('uploaded');
+ await running;
+ expect(sources.events.at(-2)).toBe('complete:users');
+ expect(sources.commands.finalize).toHaveBeenCalledTimes(1);
+ });
+
+ it('deduplicates part and seal callbacks without changing a sealed manifest', async () => {
+ const { controller, sources } = setup();
+ sources.archive.prepare.mockImplementation(async (options) => {
+ const descriptor = historyPart();
+ await Promise.all([
+ options.part(descriptor, new Blob(['[]'])),
+ options.part(descriptor, new Blob(['[]'])),
+ ]);
+ await options.seal(historySeal());
+ await options.seal(historySeal());
+ });
+ await select(controller);
+ await controller.start(selection);
+ expect(sources.commands.register).toHaveBeenCalledTimes(2);
+ expect(sources.events.filter((event) => event === 'seal:1')).toHaveLength(
+ 1
+ );
+ });
+
+ it.each(['missing', 'changed', 'after-seal'])(
+ 'rejects %s manifest data without silently finalizing',
+ async (kind) => {
+ const { controller, sources } = setup();
+ sources.archive.prepare.mockImplementation(async (options) => {
+ if (kind === 'missing') {
+ await options.seal(historySeal(1));
+ return;
+ }
+ await options.part(historyPart(), new Blob(['[]']));
+ if (kind === 'changed')
+ await options.part(
+ { ...historyPart(), sha256: 'c'.repeat(64) },
+ new Blob(['[]'])
+ );
+ else {
+ await options.seal(historySeal());
+ await options.part(historyPart(1), new Blob(['[]']));
+ }
+ });
+ await select(controller);
+ await controller.start(selection);
+ expect(controller.phase()).toBe('interrupted');
+ expect(sources.commands.finalize).not.toHaveBeenCalled();
+ expect(sources.release).toHaveBeenCalledTimes(2);
+ await controller.finalizeWithSkips();
+ expect(sources.commands.finalize).toHaveBeenCalledTimes(1);
+ expect(sources.commands.create).toHaveBeenCalledTimes(1);
+ }
+ );
+
+ it('renews expired URLs just in time with exactly the same descriptor', async () => {
+ const { controller, sources } = setup();
+ sources.put.mockRejectedValueOnce(new ImportUploadError('EXPIRED'));
+ const complete = sources.commands.complete.getMockImplementation()!;
+ sources.commands.complete.mockImplementation(async (...args) => {
+ if (
+ args[1][0]?.kind === 'users' &&
+ sources.commands.register.mock.calls.filter(
+ (call) => call[1][0].upload.kind === 'users'
+ ).length === 1
+ )
+ throw new Error('HEAD: not found');
+ return complete(...args);
+ });
+ await select(controller);
+ await controller.start(selection);
+ const registrations = sources.commands.register.mock.calls.filter(
+ (call) => call[1][0].upload.kind === 'users'
+ );
+ expect(registrations).toHaveLength(2);
+ expect(registrations[0][1]).toEqual(registrations[1][1]);
+ expect(sources.commands.create).toHaveBeenCalledTimes(1);
+ expect(controller.phase()).toBe('monitoring');
+ });
+
+ it.each(['network', 'already-exists'])(
+ 'verifies HEAD after a %s PUT outcome',
+ async (outcome) => {
+ const { controller, sources } = setup();
+ if (outcome === 'network')
+ sources.put.mockRejectedValueOnce(
+ new ImportUploadError('NETWORK_ERROR')
+ );
+ else sources.put.mockResolvedValueOnce('already-exists');
+ await select(controller);
+ await controller.start(selection);
+ expect(sources.commands.register).toHaveBeenCalledTimes(2);
+ expect(
+ sources.commands.complete.mock.calls.filter(
+ (call) => call[1][0]?.kind === 'users'
+ )
+ ).toHaveLength(1);
+ expect(controller.phase()).toBe('monitoring');
+ }
+ );
+
+ it('bounds failed PUT retries and allows cancellation after interruption', async () => {
+ const { controller, sources } = setup();
+ sources.archive.prepare.mockImplementation(async (options) => {
+ await options.seal(historySeal(0));
+ });
+ sources.put.mockRejectedValue(new ImportUploadError('NETWORK_ERROR'));
+ const complete = sources.commands.complete.getMockImplementation()!;
+ sources.commands.complete.mockImplementation(async (...args) => {
+ if (args[1].length) throw new Error('not found');
+ return complete(...args);
+ });
+ await select(controller);
+ await controller.start(selection);
+ expect(sources.put).toHaveBeenCalledTimes(3);
+ expect(controller.phase()).toBe('interrupted');
+ expect(sources.commands.finalize).not.toHaveBeenCalled();
+ expect(sources.release).toHaveBeenCalledTimes(2);
+ await controller.cancel();
+ expect(controller.phase()).toBe('terminal');
+ expect(sources.commands.cancel).toHaveBeenCalledTimes(1);
+ });
+
+ it('recovers lost creation responses using the same request/token', async () => {
+ const { controller, sources } = setup();
+ sources.commands.create.mockRejectedValue(new Error('lost response'));
+ await select(controller);
+ await controller.start(selection);
+ expect(controller.phase()).toBe('interrupted');
+ expect(sources.commands.create).toHaveBeenCalledTimes(3);
+ sources.commands.create.mockResolvedValue(importReceipt());
+ await controller.recoverJob();
+ await controller.finalizeWithSkips();
+ const requests = sources.commands.create.mock.calls.map((call) => call[1]);
+ expect(
+ requests.every(
+ (request) => JSON.stringify(request) === JSON.stringify(requests[0])
+ )
+ ).toBe(true);
+ expect(sources.newToken).toHaveBeenCalledTimes(1);
+ expect(sources.archive.prepare).not.toHaveBeenCalled();
+ });
+
+ it('cancels a delayed create before starting uploads', async () => {
+ const { controller, sources } = setup();
+ const created = deferred<ReturnType<typeof importReceipt>>();
+ sources.commands.create.mockReturnValue(created.promise);
+ await select(controller);
+ const running = controller.start(selection);
+ const cancelling = controller.cancel();
+ created.resolve(importReceipt());
+ await Promise.all([running, cancelling]);
+ expect(sources.commands.cancel).toHaveBeenCalledWith({
+ teamId: 'team',
+ jobId: 'job',
+ });
+ expect(sources.commands.register).not.toHaveBeenCalled();
+ expect(controller.phase()).toBe('terminal');
+ });
+
+ it('aborts active PUTs, ignores delayed completions and cleans up during cancellation', async () => {
+ const { controller, sources } = setup();
+ const pending = deferred<'uploaded'>();
+ sources.put.mockImplementation(async (_, options) => {
+ options?.signal?.addEventListener(
+ 'abort',
+ () => pending.reject(new ImportUploadError('ABORTED')),
+ { once: true }
+ );
+ return pending.promise;
+ });
+ await select(controller);
+ const running = controller.start(selection);
+ await vi.waitFor(() => expect(sources.put).toHaveBeenCalled());
+ await controller.cancel();
+ await running;
+ expect(sources.commands.finalize).not.toHaveBeenCalled();
+ expect(sources.release).toHaveBeenCalledTimes(2);
+ expect(sources.archive.dispose).toHaveBeenCalledTimes(1);
+ expect(controller.phase()).toBe('terminal');
+ });
+
+ it('cleans up on owner disposal and ignores late archive responses', async () => {
+ const { controller, sources, dispose } = setup();
+ const discovery = deferred<ReturnType<typeof archiveDiscovery>>();
+ sources.archive.discover.mockReturnValue(discovery.promise);
+ const running = select(controller);
+ dispose();
+ discovery.resolve(archiveDiscovery());
+ await running;
+ expect(controller.phase()).toBe('disposed');
+ expect(controller.discovery()).toBeUndefined();
+ expect(sources.archive.dispose).toHaveBeenCalledTimes(1);
+ });
+
+ it('keeps a created job even if subsequent tracking setup fails', async () => {
+ const { controller, sources } = setup();
+ sources.onJobCreated.mockRejectedValue(new Error('cache failed'));
+ await select(controller);
+ await controller.start(selection);
+ expect(sources.commands.create).toHaveBeenCalledTimes(1);
+ expect(sources.onJobCreated).toHaveBeenCalledWith(
+ expect.objectContaining({ jobId: 'job' })
+ );
+ expect(controller.phase()).toBe('monitoring');
+ expect(controller.warning()).toContain('Import created');
+ });
+
+ it('keeps committed finalization distinct from cache/toast failure', async () => {
+ const { controller, sources } = setup();
+ sources.onFinalized.mockRejectedValue(new Error('toast failed'));
+ await select(controller);
+ await controller.start(selection);
+ expect(controller.phase()).toBe('monitoring');
+ expect(controller.error()).toBeUndefined();
+ expect(controller.warning()).toContain('Import saved');
+ await controller.finalizeWithSkips();
+ expect(sources.commands.finalize).toHaveBeenCalledTimes(1);
+ expect(sources.release).toHaveBeenCalledTimes(2);
+ });
+
+ it('releases discovery protection on a reader error', async () => {
+ const { controller, sources } = setup();
+ sources.archive.discover.mockRejectedValue(new Error('broken ZIP'));
+ await select(controller);
+ expect(controller.phase()).toBe('interrupted');
+ expect(sources.release).toHaveBeenCalledTimes(1);
+ expect(sources.archive.dispose).toHaveBeenCalledTimes(1);
+ expect(sources.commands.create).not.toHaveBeenCalled();
+ });
+
+ it('ignores a late registration response after cancellation', async () => {
+ const { controller, sources } = setup();
+ const registered =
+ deferred<Awaited<ReturnType<typeof sources.commands.register>>>();
+ sources.commands.register.mockReturnValue(registered.promise);
+ await select(controller);
+ const running = controller.start(selection);
+ await vi.waitFor(() =>
+ expect(sources.commands.register).toHaveBeenCalled()
+ );
+ const cancelling = controller.cancel();
+ expect(sources.release).toHaveBeenCalledTimes(2);
+ registered.resolve([]);
+ await Promise.all([running, cancelling]);
+ expect(sources.put).not.toHaveBeenCalled();
+ expect(sources.commands.finalize).not.toHaveBeenCalled();
+ expect(controller.phase()).toBe('terminal');
+ });
+
+ it('retries uncertain finalization without recreating or altering the sealed manifest', async () => {
+ const { controller, sources } = setup();
+ sources.commands.finalize.mockRejectedValueOnce(new Error('lost response'));
+ await select(controller);
+ await controller.start(selection);
+ expect(controller.phase()).toBe('interrupted');
+ await controller.finalizeWithSkips();
+ expect(controller.phase()).toBe('monitoring');
+ expect(sources.commands.create).toHaveBeenCalledTimes(1);
+ expect(sources.commands.register).toHaveBeenCalledTimes(2);
+ expect(sources.events.filter((event) => event === 'seal:1')).toHaveLength(
+ 1
+ );
+ expect(sources.commands.finalize).toHaveBeenCalledTimes(2);
+ });
+
+ it('does not let a delayed finalize response undo a cancellation intent', async () => {
+ const { controller, sources } = setup();
+ const finalized = deferred<ReturnType<typeof importReceipt>>();
+ sources.commands.finalize.mockReturnValue(finalized.promise);
+ await select(controller);
+ const running = controller.start(selection);
+ await vi.waitFor(() => expect(controller.phase()).toBe('finalizing'));
+ const cancelling = controller.cancel();
+ finalized.resolve(
+ importReceipt({
+ revision: 10,
+ status: 'processing',
+ registrationClosed: true,
+ })
+ );
+ sources.commands.cancel.mockResolvedValue(
+ importReceipt({ revision: 11, status: 'cancelled' })
+ );
+ await Promise.all([running, cancelling]);
+ expect(controller.phase()).toBe('terminal');
+ expect(sources.onFinalized).not.toHaveBeenCalled();
+ });
+
+ it('disposes during upload even when a transport ignores the abort signal', async () => {
+ const { controller, sources, dispose } = setup();
+ const uploaded = deferred<'uploaded'>();
+ sources.put.mockReturnValue(uploaded.promise);
+ await select(controller);
+ const running = controller.start(selection);
+ await vi.waitFor(() => expect(sources.put).toHaveBeenCalled());
+ dispose();
+ expect(sources.release).toHaveBeenCalledTimes(2);
+ uploaded.resolve('uploaded');
+ await running;
+ expect(controller.phase()).toBe('disposed');
+ expect(sources.commands.complete).not.toHaveBeenCalled();
+ expect(sources.commands.finalize).not.toHaveBeenCalled();
+ });
+
+ it('exposes sanitized skip reasons but no inaccessible target identity', async () => {
+ const { controller, sources } = setup();
+ await select(controller);
+ await controller.start(selection);
+ const receipt = importReceipt({
+ revision: 100,
+ status: 'completed_with_errors',
+ });
+ receipt.conversations = [
+ {
+ ...receipt.conversations[0],
+ status: 'skipped',
+ channelId: 'secret-target',
+ warnings: ['target_unavailable'],
+ },
+ ];
+ sources.setObserved(receipt);
+ expect(controller.skips()).toEqual([
+ { slackChannelId: 'C123', reason: 'target_unavailable' },
+ ]);
+ expect(controller.job()?.conversations[0].channelId).toBeUndefined();
+ });
+});
+
+describe('archive capability integration', () => {
+ it('constructs lazily, backpressures callbacks, ignores duplicate/stale responses and deletes scratch storage', async () => {
+ const worker = {
+ postMessage: vi.fn(),
+ onmessage: null,
+ onerror: null,
+ } as unknown as Worker;
+ const createWorker = vi.fn(() => worker);
+ const disposeWorker = vi.fn(async () => {});
+ const source = createArchiveSource('session', createWorker, disposeWorker);
+ function send(response: ArchiveWorkerResponse): void {
+ worker.onmessage?.call(worker, {
+ data: response,
+ } as MessageEvent<ArchiveWorkerResponse>);
+ }
+ expect(createWorker).not.toHaveBeenCalled();
+ const discovery = source.discover(new Blob(), importLimits);
+ send({
+ type: 'discovered',
+ sessionId: 'other',
+ discovery: archiveDiscovery(),
+ });
+ send({
+ type: 'discovered',
+ sessionId: 'session',
+ discovery: archiveDiscovery(),
+ });
+ await discovery;
+ const gate = deferred<void>();
+ const part = vi.fn(async () => {
+ await gate.promise;
+ });
+ const seal = vi.fn(async () => {});
+ const prepared = source.prepare({
+ ...selection,
+ includeMessageHistory: true,
+ partLimits: importLimits,
+ part,
+ seal,
+ });
+ const response: ArchiveWorkerResponse = {
+ type: 'part',
+ sessionId: 'session',
+ sequence: 0,
+ descriptor: historyPart(),
+ bytes: new Uint8Array([1, 2]),
+ };
+ send(response);
+ send(response);
+ send({ type: 'seal', sessionId: 'session', seal: historySeal() });
+ send({ type: 'seal', sessionId: 'session', seal: historySeal() });
+ send({ type: 'complete', sessionId: 'session' });
+ send({ type: 'complete', sessionId: 'session' });
+ expect(worker.postMessage).not.toHaveBeenCalledWith(
+ expect.objectContaining({ type: 'ack_part' })
+ );
+ gate.resolve();
+ await prepared;
+ expect(part).toHaveBeenCalledTimes(1);
+ expect(seal).toHaveBeenCalledTimes(1);
+ expect(worker.postMessage).toHaveBeenCalledWith({
+ type: 'ack_part',
+ sessionId: 'session',
+ sequence: 0,
+ });
+ await source.dispose();
+ await source.dispose();
+ expect(disposeWorker).toHaveBeenCalledTimes(1);
+ expect(worker.onmessage).toBeNull();
+ });
+
+ it('rejects pending discovery on disposal', async () => {
+ const worker = {
+ postMessage: vi.fn(),
+ onmessage: null,
+ onerror: null,
+ } as unknown as Worker;
+ const source = createArchiveSource(
+ 'session',
+ () => worker,
+ async () => {}
+ );
+ const discovering = source.discover(new Blob(), importLimits);
+ const rejected = expect(discovering).rejects.toThrow('cancelled');
+ await source.dispose();
+ await rejected;
+ });
+
+ it('registers beforeunload only for the local-file hold and releases it once', () => {
+ const release = vi.fn();
+ const add = vi.spyOn(window, 'addEventListener');
+ const remove = vi.spyOn(window, 'removeEventListener');
+ const unprotect = protectImportFile(() => release);
+ expect(add).toHaveBeenCalledWith('beforeunload', expect.any(Function));
+ const listener = add.mock.calls[0][1];
+ const event = new Event('beforeunload', { cancelable: true });
+ window.dispatchEvent(event);
+ expect(event.defaultPrevented).toBe(true);
+ unprotect();
+ unprotect();
+ expect(remove).toHaveBeenCalledWith('beforeunload', listener);
+ expect(release).toHaveBeenCalledTimes(1);
+ add.mockRestore();
+ remove.mockRestore();
+ });
+});
diff --git a/apps/web/src/features/slack-import/primitives/import-controller.ts b/apps/web/src/features/slack-import/primitives/import-controller.ts
new file mode 100644
index 0000000000..919d47e003
--- /dev/null
+++ b/apps/web/src/features/slack-import/primitives/import-controller.ts
@@ -0,0 +1,660 @@
+import { type Accessor, createMemo, createSignal, onCleanup } from 'solid-js';
+import type {
+ ArchiveSource,
+ CreateImport,
+ ImportCommands,
+ ImportJob,
+ ImportLimits,
+ ImportSource,
+ ImportSourceBinding,
+ ImportUploadDescriptor,
+ ImportUploadProgress,
+} from '../context/contracts';
+import { ImportUploadError } from '../context/contracts';
+import type { ArchiveDiscovery } from '../core/export';
+import type { ConversationSeal, PartDescriptor } from '../core/worker-protocol';
+
+export type ImportPhase =
+ | 'idle'
+ | 'discovering'
+ | 'selecting'
+ | 'preparing'
+ | 'uploading'
+ | 'interrupted'
+ | 'finalizing'
+ | 'cancelling'
+ | 'monitoring'
+ | 'terminal'
+ | 'disposed';
+
+export type ImportSkip = {
+ slackChannelId: string;
+ reason:
+ | 'unsupported_dm'
+ | 'target_unavailable'
+ | 'uploads_incomplete'
+ | 'skipped';
+};
+
+export type ImportControllerOptions = {
+ teamId: string;
+ limits: ImportLimits;
+ sourceBinding: Accessor<ImportSourceBinding>;
+ archive: ArchiveSource;
+ commands: ImportCommands;
+ source: ImportSource;
+ /** Acquire both the automatic-reload hold and beforeunload listener. */
+ protectFile(): () => void;
+ newToken(): string;
+ /** Point the host's job source at this receipt. Failure must not retry creation. */
+ onJobCreated?(job: ImportJob): Promise<void>;
+ /** Presentation/cache failures here never undo successful server writes. */
+ onFinalized?(job: ImportJob): Promise<void>;
+};
+
+export type ImportController = {
+ phase: Accessor<ImportPhase>;
+ discovery: Accessor<ArchiveDiscovery | undefined>;
+ job: Accessor<ImportJob | undefined>;
+ jobId: Accessor<string | undefined>;
+ error: Accessor<string | undefined>;
+ warning: Accessor<string | undefined>;
+ skips: Accessor<readonly ImportSkip[]>;
+ uploadProgress: Accessor<ImportUploadProgress>;
+ discover(file: Blob): Promise<void>;
+ start(selection: {
+ selectedIds: string[];
+ includeMessageHistory?: boolean;
+ sourceConfirmed: boolean;
+ }): Promise<void>;
+ /** Recover an uncertain create response with the original token, never a new job. */
+ recoverJob(): Promise<void>;
+ finalizeWithSkips(): Promise<void>;
+ cancel(): Promise<void>;
+ refresh(): Promise<void>;
+ dispose(): Promise<void>;
+};
+
+function terminal(job: ImportJob): boolean {
+ return ['completed', 'completed_with_errors', 'failed', 'cancelled'].includes(
+ job.status
+ );
+}
+
+function descriptorKey(descriptor: ImportUploadDescriptor): string {
+ const id = descriptor.upload;
+ return id.kind === 'users' ? 'users' : `${id.slackChannelId}:${id.partIndex}`;
+}
+
+function descriptorSignature(descriptor: ImportUploadDescriptor): string {
+ return `${descriptorKey(descriptor)}:${descriptor.sha256}:${descriptor.byteLength}:${descriptor.recordCount ?? ''}`;
+}
+
+function unsupportedDirectMessages(discovery: ArchiveDiscovery): Set<string> {
+ const emails = new Map(
+ discovery.users.map((user) => [
+ user.id,
+ user.is_bot || user.id === 'USLACKBOT'
+ ? undefined
+ : user.profile?.email?.toLowerCase(),
+ ])
+ );
+ return new Set(
+ discovery.conversations
+ .filter((conversation) => {
+ if (conversation.kind !== 'direct_message') return false;
+ const members = conversation.memberIds.map((id) => emails.get(id));
+ return (
+ members.length !== 2 ||
+ members.some((email) => !email) ||
+ members[0] === members[1]
+ );
+ })
+ .map((conversation) => conversation.slackChannelId)
+ );
+}
+
+/** One controller per file. At most two uploads run: users and one backpressured history part. */
+export function createImportController(
+ options: ImportControllerOptions
+): ImportController {
+ const { archive, commands, source } = options;
+ const [stage, setStage] = createSignal<ImportPhase>('idle');
+ const [discovery, setDiscovery] = createSignal<ArchiveDiscovery>();
+ const [receipt, setReceipt] = createSignal<ImportJob>();
+ const [error, setError] = createSignal<string>();
+ const [warning, setWarning] = createSignal<string>();
+ const [uploadProgress, setUploadProgress] =
+ createSignal<ImportUploadProgress>({
+ loaded: 0,
+ total: 0,
+ indeterminate: false,
+ });
+ const byteProgress = new Map<string, ImportUploadProgress>();
+ function reportBytes(key: string, progress: ImportUploadProgress): void {
+ if (disposed) return;
+ byteProgress.set(key, progress);
+ const parts = [...byteProgress.values()];
+ setUploadProgress({
+ loaded: parts.reduce((sum, part) => sum + part.loaded, 0),
+ total: parts.reduce((sum, part) => sum + part.total, 0),
+ indeterminate: parts.some((part) => part.indeterminate),
+ });
+ }
+ const abort = new AbortController();
+ let disposed = false;
+ let releaseFile: (() => void) | undefined;
+ let localWork: Promise<void> | undefined;
+ let request: CreateImport | undefined;
+ let durableJob: ImportJob | undefined;
+ let commandBusy = false;
+ let cleanup: Promise<void> | undefined;
+ const uploads = new Map<string, { signature: string; done: Promise<void> }>();
+ const sealed = new Map<string, { signature: string; done: Promise<void> }>();
+
+ function checkActive(): void {
+ if (disposed || abort.signal.aborted) throw new Error('Import interrupted');
+ }
+
+ function accept(job: ImportJob): void {
+ if (!durableJob || job.revision >= durableJob.revision) {
+ durableJob = job;
+ if (!disposed) setReceipt(job);
+ }
+ }
+
+ const job = createMemo<ImportJob | undefined>((previous) => {
+ const observed = source.job();
+ const saved = receipt();
+ const latest =
+ observed &&
+ saved &&
+ observed.teamId === saved.teamId &&
+ observed.jobId === saved.jobId &&
+ observed.revision >= saved.revision
+ ? observed
+ : saved;
+ if (!latest) return previous;
+ if (previous && previous.revision > latest.revision) return previous;
+ // Defense in depth: skip receipts must never expose an inaccessible target identity.
+ return {
+ ...latest,
+ conversations: latest.conversations.map((conversation) => ({
+ ...conversation,
+ channelId:
+ conversation.status === 'skipped'
+ ? undefined
+ : conversation.channelId,
+ })),
+ };
+ });
+
+ function scope(): { teamId: string; jobId: string } {
+ if (!durableJob) throw new Error('No import job');
+ return { teamId: options.teamId, jobId: durableJob.jobId };
+ }
+
+ function releaseProtection(): void {
+ releaseFile?.();
+ releaseFile = undefined;
+ }
+
+ async function releaseLocal(): Promise<void> {
+ releaseProtection();
+ cleanup ??= archive.dispose();
+ try {
+ await cleanup;
+ } catch {
+ if (!disposed)
+ setWarning('Temporary archive storage could not be removed.');
+ }
+ }
+
+ async function ensureJob(): Promise<void> {
+ if (durableJob) return;
+ if (!request) throw new Error('No import request');
+ for (let attempt = 0; ; attempt++) {
+ try {
+ // Even after cancellation, recover a delayed write so cancel can address it.
+ accept(await commands.create(options.teamId, structuredClone(request)));
+ break;
+ } catch (error) {
+ if (attempt === 2) throw error;
+ }
+ }
+ if (disposed) return;
+ try {
+ await options.onJobCreated?.(durableJob!);
+ } catch {
+ if (!disposed)
+ setWarning('Import created; progress tracking could not be started.');
+ }
+ }
+
+ async function uploadPart(
+ descriptor: ImportUploadDescriptor,
+ blob: Blob
+ ): Promise<void> {
+ const key = descriptorKey(descriptor);
+ const total = descriptor.byteLength;
+ for (let attempt = 0; ; attempt++) {
+ checkActive();
+ reportBytes(key, { loaded: 0, total, indeterminate: false });
+ try {
+ // Register one immutable descriptor only when a PUT slot is available.
+ const grants = await commands.register(scope(), [
+ structuredClone(descriptor),
+ ]);
+ checkActive();
+ const grant = grants[0];
+ if (
+ grants.length !== 1 ||
+ !grant ||
+ descriptorSignature(grant.descriptor) !==
+ descriptorSignature(descriptor)
+ )
+ throw new ImportUploadError('INVALID_GRANT');
+ try {
+ await grant.put(blob, {
+ signal: abort.signal,
+ onProgress: (progress) =>
+ reportBytes(key, {
+ loaded:
+ progress.kind === 'bytes'
+ ? Math.min(progress.loaded, total)
+ : 0,
+ total,
+ indeterminate: progress.kind === 'indeterminate',
+ }),
+ });
+ } catch (putError) {
+ checkActive();
+ // A lost PUT response may still have committed the immutable object.
+ // Completion performs server-side HEAD/checksum validation, including 412 retries.
+ try {
+ const verified = await commands.complete(scope(), [
+ descriptor.upload,
+ ]);
+ checkActive();
+ accept(verified);
+ reportBytes(key, { loaded: total, total, indeterminate: false });
+ return;
+ } catch {
+ throw putError;
+ }
+ }
+ checkActive();
+ const verified = await commands.complete(scope(), [descriptor.upload]);
+ checkActive();
+ accept(verified);
+ reportBytes(key, { loaded: total, total, indeterminate: false });
+ return;
+ } catch (failure) {
+ checkActive();
+ if (
+ attempt === 2 ||
+ (failure instanceof ImportUploadError &&
+ ['SIZE_MISMATCH', 'INVALID_GRANT', 'ABORTED'].includes(
+ failure.code
+ ))
+ )
+ throw failure;
+ }
+ }
+ }
+
+ function consumePart(
+ descriptor: ImportUploadDescriptor,
+ blob: Blob
+ ): Promise<void> {
+ checkActive();
+ const snapshot = structuredClone(descriptor);
+ const key = descriptorKey(snapshot);
+ const signature = descriptorSignature(snapshot);
+ const previous = uploads.get(key);
+ if (previous) {
+ if (signature !== previous.signature)
+ throw new Error('Descriptor changed');
+ return previous.done;
+ }
+ if (snapshot.upload.kind === 'conversation_part') {
+ const id = snapshot.upload.slackChannelId;
+ if (
+ !request?.conversations.some(
+ (conversation) => conversation.slackChannelId === id
+ ) ||
+ sealed.has(id)
+ )
+ throw new Error('Unexpected part');
+ }
+ setStage('uploading');
+ const done = uploadPart(snapshot, blob);
+ uploads.set(key, { signature, done });
+ return done;
+ }
+
+ function consumeSeal(seal: ConversationSeal): Promise<void> {
+ checkActive();
+ const snapshot = { ...seal };
+ const id = snapshot.slackChannelId;
+ const signature = `${id}:${snapshot.partCount}:${snapshot.manifestSha256}`;
+ const previous = sealed.get(id);
+ if (previous) {
+ if (signature !== previous.signature) throw new Error('Seal changed');
+ return previous.done;
+ }
+ if (
+ !request?.conversations.some(
+ (conversation) => conversation.slackChannelId === id
+ ) ||
+ !Number.isSafeInteger(snapshot.partCount) ||
+ snapshot.partCount < 0
+ )
+ throw new Error('Unexpected seal');
+ const parts = [...uploads.keys()].filter((key) => key.startsWith(`${id}:`));
+ if (parts.length !== snapshot.partCount)
+ throw new Error('Incomplete manifest');
+ const pending: Promise<void>[] = [];
+ for (let index = 0; index < snapshot.partCount; index++) {
+ const part = uploads.get(`${id}:${index}`);
+ if (!part) throw new Error('Noncontiguous manifest');
+ pending.push(part.done);
+ }
+ async function sealConversation(): Promise<void> {
+ await Promise.all(pending);
+ checkActive();
+ const result = await commands.complete(scope(), [], snapshot);
+ checkActive();
+ accept(result);
+ }
+ const done = sealConversation();
+ sealed.set(id, { signature, done });
+ return done;
+ }
+
+ async function finalize(): Promise<void> {
+ setStage('finalizing');
+ const result = await commands.finalize(scope());
+ accept(result);
+ if (disposed || stage() === 'cancelling') return;
+ setStage('monitoring');
+ try {
+ await options.onFinalized?.(result);
+ } catch {
+ if (!disposed)
+ setWarning('Import saved; the display could not be refreshed.');
+ }
+ }
+
+ async function runImport(users: ArchiveDiscovery['users']): Promise<void> {
+ try {
+ await ensureJob();
+ checkActive();
+ const limits = durableJob!.limits;
+ const bytes = new TextEncoder().encode(JSON.stringify(users));
+ if (bytes.byteLength > limits.jsonBytes)
+ throw new Error('Users metadata exceeds the upload limit');
+ const hash = new Uint8Array(await crypto.subtle.digest('SHA-256', bytes));
+ checkActive();
+ const usersDescriptor: ImportUploadDescriptor = {
+ upload: { kind: 'users' },
+ byteLength: bytes.byteLength,
+ sha256: Array.from(hash, (byte) =>
+ byte.toString(16).padStart(2, '0')
+ ).join(''),
+ };
+ // Attach both rejection handlers immediately; failure aborts the other lane.
+ async function lane(work: () => Promise<void>): Promise<void> {
+ try {
+ await work();
+ } catch (failure) {
+ abort.abort();
+ await releaseLocal();
+ throw failure;
+ }
+ }
+ const results = await Promise.allSettled([
+ lane(() => consumePart(usersDescriptor, new Blob([bytes]))),
+ lane(() =>
+ archive.prepare({
+ selectedIds: request!.conversations.map(
+ (conversation) => conversation.slackChannelId
+ ),
+ includeMessageHistory: request!.includeMessageHistory,
+ partLimits: limits,
+ part: (descriptor: PartDescriptor, blob: Blob) =>
+ consumePart(descriptor, blob),
+ seal: consumeSeal,
+ })
+ ),
+ ]);
+ if (results.some((result) => result.status === 'rejected'))
+ throw new Error('Local upload interrupted');
+ checkActive();
+ if (sealed.size !== request!.conversations.length)
+ throw new Error('Missing conversation seal');
+ await Promise.all([...sealed.values()].map((seal) => seal.done));
+ await releaseLocal();
+ checkActive();
+ await finalize();
+ } catch {
+ if (!disposed && stage() !== 'cancelling') {
+ setError(
+ 'Import interrupted. Recover the job if necessary, then explicitly finalize with skips or cancel.'
+ );
+ setStage('interrupted');
+ }
+ } finally {
+ await releaseLocal();
+ }
+ }
+
+ async function dispose(): Promise<void> {
+ if (disposed) return;
+ disposed = true;
+ abort.abort();
+ setStage('disposed');
+ await releaseLocal();
+ }
+ onCleanup(() => {
+ void dispose();
+ });
+
+ return {
+ phase: () => {
+ const current = job();
+ if (stage() === 'monitoring' && current && terminal(current))
+ return 'terminal';
+ return stage();
+ },
+ discovery,
+ uploadProgress,
+ job,
+ jobId: () => receipt()?.jobId,
+ error,
+ warning: () =>
+ warning() ??
+ (source.error()
+ ? 'Progress could not be refreshed. The server import may still be running.'
+ : undefined),
+ skips: () => {
+ const found = discovery();
+ const skips = new Map<string, ImportSkip>();
+ if (found && !request)
+ for (const slackChannelId of unsupportedDirectMessages(found))
+ skips.set(slackChannelId, {
+ slackChannelId,
+ reason: 'unsupported_dm',
+ });
+ for (const conversation of job()?.conversations ?? []) {
+ if (conversation.status !== 'skipped') continue;
+ let reason: ImportSkip['reason'] = 'skipped';
+ if (
+ conversation.warnings.includes('target_unavailable') ||
+ conversation.error === 'unavailable'
+ )
+ reason = 'target_unavailable';
+ else if (conversation.warnings.includes('unresolvable_direct_message'))
+ reason = 'unsupported_dm';
+ else if (conversation.warnings.includes('uploads_incomplete'))
+ reason = 'uploads_incomplete';
+ skips.set(conversation.slackChannelId, {
+ slackChannelId: conversation.slackChannelId,
+ reason,
+ });
+ }
+ return [...skips.values()];
+ },
+ async discover(file) {
+ if (stage() !== 'idle') return;
+ setStage('discovering');
+ async function discoverArchive(): Promise<void> {
+ try {
+ releaseFile = options.protectFile();
+ const found = await archive.discover(file, options.limits);
+ checkActive();
+ setDiscovery(found);
+ setStage('selecting');
+ } catch {
+ if (!disposed && stage() !== 'cancelling') {
+ setError('The archive could not be read. Choose a new file.');
+ setStage('interrupted');
+ }
+ await releaseLocal();
+ } finally {
+ releaseProtection();
+ }
+ }
+ localWork = discoverArchive();
+ await localWork;
+ },
+ async start(selection) {
+ if (stage() !== 'selecting') return;
+ const found = discovery()!;
+ const binding = options.sourceBinding();
+ if (!selection.sourceConfirmed)
+ throw new Error('Confirm the Slack source binding before importing.');
+ if (
+ found.source.kind === 'known' &&
+ binding.kind === 'known' &&
+ found.source.sourceId !== binding.sourceId
+ )
+ throw new Error('The archive belongs to a different Slack workspace.');
+ const ids = new Set(selection.selectedIds);
+ const unsupported = unsupportedDirectMessages(found);
+ if (
+ !ids.size ||
+ ids.size !== selection.selectedIds.length ||
+ ids.size > options.limits.conversations ||
+ [...ids].some(
+ (id) =>
+ !found.conversations.some(
+ (conversation) => conversation.slackChannelId === id
+ ) || unsupported.has(id)
+ )
+ )
+ throw new Error(
+ 'Select supported conversations within the import limit.'
+ );
+ request = {
+ idempotencyToken: options.newToken(),
+ includeMessageHistory: selection.includeMessageHistory ?? true,
+ source:
+ found.source.kind === 'known'
+ ? { ...found.source }
+ : { kind: 'confirmed_unknown' },
+ conversations: structuredClone(
+ found.conversations.filter((conversation) =>
+ ids.has(conversation.slackChannelId)
+ )
+ ),
+ };
+ setStage('preparing');
+ try {
+ releaseFile = options.protectFile();
+ } catch {
+ setStage('interrupted');
+ setError('The local file could not be protected.');
+ await releaseLocal();
+ return;
+ }
+ localWork = runImport(found.users);
+ await localWork;
+ },
+ async recoverJob() {
+ if (
+ disposed ||
+ commandBusy ||
+ stage() !== 'interrupted' ||
+ durableJob ||
+ !request
+ )
+ return;
+ commandBusy = true;
+ try {
+ await ensureJob();
+ } catch {
+ if (!disposed)
+ setError(
+ 'The job receipt could not be recovered. Retry with the same import.'
+ );
+ } finally {
+ commandBusy = false;
+ }
+ },
+ async finalizeWithSkips() {
+ if (disposed || commandBusy || stage() !== 'interrupted' || !durableJob)
+ return;
+ commandBusy = true;
+ setError(undefined);
+ try {
+ await finalize();
+ } catch {
+ if (!disposed) {
+ setStage('interrupted');
+ setError('Finalization was not confirmed. Retry or cancel.');
+ }
+ } finally {
+ commandBusy = false;
+ }
+ },
+ async cancel() {
+ if (disposed || commandBusy || stage() === 'terminal') return;
+ commandBusy = true;
+ setStage('cancelling');
+ abort.abort();
+ await releaseLocal();
+ await localWork;
+ try {
+ if (request && !durableJob) await ensureJob();
+ if (disposed) return;
+ const current = job();
+ if (durableJob && !(current && terminal(current)))
+ accept(await commands.cancel(scope()));
+ if (!disposed) {
+ setError(undefined);
+ setStage(durableJob ? 'monitoring' : 'terminal');
+ }
+ } catch {
+ if (!disposed) {
+ setStage('interrupted');
+ setError('Cancellation was not confirmed. Retry cancel.');
+ }
+ } finally {
+ commandBusy = false;
+ }
+ },
+ async refresh() {
+ if (disposed) return;
+ try {
+ await source.refresh();
+ } catch {
+ if (!disposed)
+ setWarning(
+ 'Progress could not be refreshed. The server import may still be running.'
+ );
+ }
+ },
+ dispose,
+ };
+}
diff --git a/apps/web/src/features/slack-import/queries/archive-source.ts b/apps/web/src/features/slack-import/queries/archive-source.ts
new file mode 100644
index 0000000000..da9147fb08
--- /dev/null
+++ b/apps/web/src/features/slack-import/queries/archive-source.ts
@@ -0,0 +1,155 @@
+import {
+ createSlackImportWorker,
+ disposeSlackImportWorker,
+} from '../../../lib/workers/slack-import/worker-client';
+import type { ArchiveSource } from '../context/contracts';
+import { ArchiveError } from '../core/export';
+import type {
+ ArchiveWorkerRequest,
+ ArchiveWorkerResponse,
+} from '../core/worker-protocol';
+
+/** Lazy, single-file adapter. No worker, listener or scratch DB exists before discover. */
+export function createArchiveSource(
+ sessionId: string = crypto.randomUUID(),
+ createWorker: () => Worker = createSlackImportWorker,
+ disposeWorker: typeof disposeSlackImportWorker = disposeSlackImportWorker
+): ArchiveSource {
+ let worker: Worker | undefined;
+ let disposed = false;
+ let discovered = false;
+ let preparing = false;
+ let rejectPending: ((error: Error) => void) | undefined;
+ let handle: ((response: ArchiveWorkerResponse) => Promise<void>) | undefined;
+ const queue: ArchiveWorkerResponse[] = [];
+ let draining = false;
+ let disposal: Promise<void> | undefined;
+
+ function post(request: ArchiveWorkerRequest): void {
+ if (disposed || !worker) throw new ArchiveError('cancelled');
+ worker.postMessage(request);
+ }
+
+ async function drain(): Promise<void> {
+ if (draining) return;
+ draining = true;
+ try {
+ while (!disposed && queue.length) {
+ const response = queue.shift()!;
+ if (response.type === 'error') throw new ArchiveError(response.code);
+ if (response.type === 'cancelled') throw new ArchiveError('cancelled');
+ await handle?.(response);
+ }
+ } catch (error) {
+ rejectPending?.(
+ error instanceof Error ? error : new ArchiveError('invalid_state')
+ );
+ handle = undefined;
+ queue.length = 0;
+ } finally {
+ draining = false;
+ }
+ }
+
+ return {
+ discover(file, limits) {
+ if (disposed || worker)
+ return Promise.reject(new ArchiveError('invalid_state'));
+ worker = createWorker();
+ worker.onmessage = (event: MessageEvent<ArchiveWorkerResponse>) => {
+ if (disposed || event.data.sessionId !== sessionId) return;
+ queue.push(event.data);
+ void drain();
+ };
+ worker.onerror = () => rejectPending?.(new ArchiveError('invalid_zip'));
+ return new Promise((resolve, reject) => {
+ rejectPending = reject;
+ handle = async (response) => {
+ if (response.type !== 'discovered' || discovered) return;
+ discovered = true;
+ handle = undefined;
+ rejectPending = undefined;
+ resolve(response.discovery);
+ };
+ post({ type: 'discover', sessionId, archive: file, limits });
+ });
+ },
+ prepare(options) {
+ if (disposed || !discovered || preparing)
+ return Promise.reject(new ArchiveError('invalid_state'));
+ preparing = true;
+ let nextSequence = 0;
+ const seals = new Map<string, string>();
+ return new Promise((resolve, reject) => {
+ rejectPending = reject;
+ handle = async (response) => {
+ if (response.type === 'part') {
+ // A duplicate callback must never upload twice or acknowledge a newer part.
+ if (response.sequence < nextSequence) return;
+ if (response.sequence !== nextSequence)
+ throw new ArchiveError('invalid_state');
+ await options.part(response.descriptor, new Blob([response.bytes]));
+ post({ type: 'ack_part', sessionId, sequence: nextSequence });
+ nextSequence++;
+ } else if (response.type === 'seal') {
+ const id = response.seal.slackChannelId;
+ const signature = `${id}:${response.seal.partCount}:${response.seal.manifestSha256}`;
+ const previous = seals.get(id);
+ if (previous !== undefined && previous !== signature)
+ throw new ArchiveError('invalid_state');
+ if (previous !== undefined) return;
+ await options.seal(response.seal);
+ seals.set(id, signature);
+ } else if (response.type === 'complete') {
+ handle = undefined;
+ rejectPending = undefined;
+ resolve();
+ }
+ };
+ post({
+ type: 'read_history',
+ sessionId,
+ selectedIds: options.selectedIds,
+ includeMessageHistory: options.includeMessageHistory,
+ partLimits: options.partLimits,
+ });
+ });
+ },
+ dispose() {
+ if (disposal) return disposal;
+ disposed = true;
+ rejectPending?.(new ArchiveError('cancelled'));
+ rejectPending = undefined;
+ handle = undefined;
+ queue.length = 0;
+ if (worker) {
+ worker.onmessage = null;
+ worker.onerror = null;
+ disposal = disposeWorker(worker, sessionId);
+ } else {
+ disposal = Promise.resolve();
+ }
+ return disposal;
+ },
+ };
+}
+
+/** Inject the app's reload hold at composition; importing this adapter has no global effects. */
+export function protectImportFile(
+ holdAutomaticReload: () => () => void,
+ target: Pick<Window, 'addEventListener' | 'removeEventListener'> = window
+): () => void {
+ const release = holdAutomaticReload();
+ function beforeUnload(event: BeforeUnloadEvent): void {
+ event.preventDefault();
+ event.returnValue = '';
+ }
+ target.addEventListener('beforeunload', beforeUnload);
+ let released = false;
+ return () => {
+ if (released) return;
+ released = true;
+ target.removeEventListener('beforeunload', beforeUnload);
+ release();
+ };
+}
diff --git a/apps/web/src/features/slack-import/queries/import-source.test.ts b/apps/web/src/features/slack-import/queries/import-source.test.ts
new file mode 100644
index 0000000000..42d7dea467
--- /dev/null
+++ b/apps/web/src/features/slack-import/queries/import-source.test.ts
@@ -0,0 +1,714 @@
+import { webcrypto } from 'node:crypto';
+import { fetchWithToken } from '@core/util/fetchWithToken';
+import {
+ importIsActive,
+ type SlackImportClient,
+ slackImportJobOptions,
+ slackImportListOptions,
+} from '@queries/slack-import';
+import { slackImportKeys } from '@queries/slack-import/keys';
+import { storageServiceClient } from '@service-storage/client';
+import type { ImportPage } from '@service-storage/generated/schemas/importPage';
+import type { ImportProgress } from '@service-storage/generated/schemas/importProgress';
+import type { UploadGrant } from '@service-storage/generated/schemas/uploadGrant';
+import type { uploadSlackImport } from '@service-storage/slack-import-upload';
+import { focusManager, QueryClient } from '@tanstack/solid-query';
+import { err, ok } from 'neverthrow';
+import { createRoot, createSignal, onCleanup } from 'solid-js';
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+import { type CreateImport, ImportUploadError } from '../context/contracts';
+import { createImportController } from '../primitives/import-controller';
+import { archiveDiscovery, fakeImportSources } from '../tests/fake-sources';
+import {
+ createImportSource,
+ type ImportSourceDependencies,
+} from './import-source';
+
+// Keep the real storage-client serialization; replace only authentication/transport
+// and unrelated application singletons imported by the legacy client module.
+vi.mock('@core/util/fetchWithToken', () => ({ fetchWithToken: vi.fn() }));
+vi.mock('@core/constant/featureFlags', () => ({ ENABLE_DOCX_TO_PDF: false }));
+vi.mock('@core/constant/PaywallState', () => ({
+ PaywallKey: {},
+ usePaywallState: () => ({ showPaywall: vi.fn() }),
+}));
+vi.mock('@core/util/mockClient', () => ({ registerClient: vi.fn() }));
+
+const teamId = '01900000-0000-7000-8000-000000000001';
+const jobId = '01900000-0000-7000-8000-000000000002';
+const otherTeam = '01900000-0000-7000-8000-000000000003';
+const otherJob = '01900000-0000-7000-8000-000000000004';
+const identity = { teamId, jobId };
+const limits = {
+ conversations: 2000,
+ databaseBatchBytes: 4194304,
+ databaseBatchMessages: 500,
+ jsonBytes: 33554432,
+ partBytes: 16777216,
+ partRecords: 20000,
+ recordBytes: 1048576,
+ registrationBatch: 50,
+ selectedBytes: 2147483648,
+ zipEntries: 100000,
+};
+
+function receipt(overrides: Partial<ImportProgress> = {}): ImportProgress {
+ return {
+ jobId,
+ source: { kind: 'confirmed_unknown' },
+ includeMessageHistory: false,
+ status: 'uploading',
+ revision: 1,
+ createdAt: '2026-09-30T00:00:00Z',
+ updatedAt: '2026-09-30T00:00:00Z',
+ usersVerified: false,
+ limits,
+ conversations: [
+ {
+ slackChannelId: 'C123',
+ name: 'General',
+ kind: 'public_channel',
+ archived: false,
+ status: 'awaiting_uploads',
+ verifiedParts: 0,
+ counters: {
+ processed: 0,
+ imported: 0,
+ skipped: 0,
+ duplicates: 0,
+ reactions: 0,
+ },
+ search: { status: 'not_needed' },
+ warnings: [],
+ },
+ ],
+ ...overrides,
+ };
+}
+function page(job = receipt()): ImportPage {
+ return { jobs: [job], limits, sourceBinding: { kind: 'unbound' } };
+}
+function fakeClient() {
+ return {
+ listSlackImports: vi.fn<SlackImportClient['listSlackImports']>(async () =>
+ ok(page())
+ ),
+ getSlackImport: vi.fn<SlackImportClient['getSlackImport']>(async () =>
+ ok(receipt())
+ ),
+ createSlackImport: vi.fn<SlackImportClient['createSlackImport']>(async () =>
+ ok(receipt())
+ ),
+ registerSlackImportUploads: vi.fn<
+ SlackImportClient['registerSlackImportUploads']
+ >(async () => ok([])),
+ completeSlackImportUploads: vi.fn<
+ SlackImportClient['completeSlackImportUploads']
+ >(async () => ok(receipt())),
+ finalizeSlackImport: vi.fn<SlackImportClient['finalizeSlackImport']>(
+ async () => ok(receipt())
+ ),
+ cancelSlackImport: vi.fn<SlackImportClient['cancelSlackImport']>(async () =>
+ ok(receipt({ status: 'cancelled' }))
+ ),
+ };
+}
+
+const cleanups: (() => void)[] = [];
+beforeEach(() => {
+ vi.useFakeTimers();
+ focusManager.setFocused(true);
+});
+afterEach(() => {
+ for (const cleanup of cleanups.splice(0)) cleanup();
+ focusManager.setFocused(undefined);
+ vi.useRealTimers();
+ vi.unstubAllGlobals();
+ vi.mocked(fetchWithToken).mockReset();
+});
+
+function mount(initialEnabled = true, transport?: SlackImportClient) {
+ const client = fakeClient();
+ const queryClient = new QueryClient({
+ defaultOptions: {
+ queries: { retry: false, gcTime: 0 },
+ mutations: { retry: false },
+ },
+ });
+ type Listener = Parameters<ImportSourceDependencies['gatewayEffect']>[0];
+ const listeners = new Set<Listener>();
+ const upload = vi.fn<typeof uploadSlackImport>(async () => ok('uploaded'));
+ const [team, setTeam] = createSignal<string | undefined>(teamId);
+ const [job, setJob] = createSignal<string | undefined>(jobId);
+ const [enabled, setEnabled] = createSignal(initialEnabled);
+ const mounted = createRoot((dispose) => {
+ const adapter = createImportSource(
+ {
+ client: transport ?? client,
+ queryClient,
+ upload,
+ gatewayEffect(listener) {
+ listeners.add(listener);
+ onCleanup(() => listeners.delete(listener));
+ },
+ },
+ { teamId: team, jobId: job, enabled }
+ );
+ return { ...adapter, dispose };
+ });
+ cleanups.push(() => {
+ mounted.dispose();
+ queryClient.clear();
+ });
+ return {
+ ...mounted,
+ client,
+ queryClient,
+ upload,
+ listeners,
+ setTeam,
+ setJob,
+ setEnabled,
+ emit(data: unknown, type = 'slack_import_updated') {
+ for (const listener of listeners) listener({ type, data });
+ },
+ };
+}
+async function settle(): Promise<void> {
+ await vi.advanceTimersByTimeAsync(1);
+}
+
+describe('import source', () => {
+ it('uses stable empty placeholders, never previous-team receipts', () => {
+ const client = fakeClient();
+ const first = slackImportListOptions(client, teamId, true);
+ const next = slackImportListOptions(client, otherTeam, true);
+ expect(first.placeholderData).toBe(next.placeholderData);
+ expect(first.placeholderData).toEqual({ teamId: null, value: null });
+ expect(slackImportJobOptions(client, identity, true).placeholderData).toBe(
+ slackImportJobOptions(client, { teamId: otherTeam, jobId }, false)
+ .placeholderData
+ );
+ });
+
+ it.each(['uploading', 'processing', 'cancelling'] as const)(
+ 'treats %s as active work',
+ (status) => {
+ expect(importIsActive(receipt({ status }))).toBe(true);
+ }
+ );
+
+ it.each([
+ 'completed',
+ 'completed_with_errors',
+ 'failed',
+ 'cancelled',
+ ] as const)('treats %s as idle without pending indexing', (status) => {
+ expect(importIsActive(receipt({ status }))).toBe(false);
+ });
+
+ it('reports initial failures without reading pending resource data', async () => {
+ const h = mount(false);
+ const failure = err([
+ { code: 'NETWORK_ERROR' as const, message: 'offline' },
+ ]);
+ h.client.listSlackImports.mockResolvedValue(failure);
+ h.client.getSlackImport.mockResolvedValue(failure);
+ h.setEnabled(true);
+ await settle();
+ expect(h.source.page()).toBeUndefined();
+ expect(h.source.job()).toBeUndefined();
+ expect(h.source.error()?.message).toBe('offline');
+ });
+
+ it('releases query polling and hides receipts on disposal', async () => {
+ const h = mount();
+ await settle();
+ expect(h.source.job()?.jobId).toBe(jobId);
+ h.dispose();
+ expect(h.source.job()).toBeUndefined();
+ expect(h.source.page()).toBeUndefined();
+ expect(h.listeners.size).toBe(0);
+ const calls = h.client.getSlackImport.mock.calls.length;
+ await vi.advanceTimersByTimeAsync(30000);
+ expect(h.client.getSlackImport).toHaveBeenCalledTimes(calls);
+ });
+ it('projects domain receipts without leaking transport envelopes or search receipts', async () => {
+ const h = mount();
+ await settle();
+ const job = h.source.job();
+ expect(job?.teamId).toBe(teamId);
+ expect(job?.conversations[0]).toMatchObject({
+ searchStatus: 'not_needed',
+ partCount: undefined,
+ });
+ expect(job?.limits).not.toHaveProperty('databaseBatchBytes');
+ expect(job?.conversations[0]).not.toHaveProperty('search');
+ expect(h.source.page()?.jobs[0]).toEqual(job);
+ });
+
+ it('recovers missed events at 3s while active, then polls idle receipts at 15s', async () => {
+ const h = mount();
+ await settle();
+ h.client.getSlackImport.mockResolvedValue(
+ ok(receipt({ status: 'completed', revision: 2 }))
+ );
+ h.client.listSlackImports.mockResolvedValue(
+ ok(page(receipt({ status: 'completed', revision: 2 })))
+ );
+ await vi.advanceTimersByTimeAsync(3000);
+ expect(h.source.job()?.status).toBe('completed');
+ expect(h.source.page()?.jobs[0].revision).toBe(2);
+ const calls = h.client.getSlackImport.mock.calls.length;
+ await vi.advanceTimersByTimeAsync(14000);
+ expect(h.client.getSlackImport).toHaveBeenCalledTimes(calls);
+ await vi.advanceTimersByTimeAsync(1000);
+ expect(h.client.getSlackImport).toHaveBeenCalledTimes(calls + 1);
+ });
+
+ it('keeps fast polling while completed history still needs indexing', async () => {
+ const h = mount();
+ const job = receipt({ status: 'completed' });
+ job.conversations[0].search = { status: 'submitted', receiptId: 'receipt' };
+ h.client.getSlackImport.mockResolvedValue(ok(job));
+ await settle();
+ await vi.advanceTimersByTimeAsync(3000);
+ const calls = h.client.getSlackImport.mock.calls.length;
+ await vi.advanceTimersByTimeAsync(3000);
+ expect(h.client.getSlackImport).toHaveBeenCalledTimes(calls + 1);
+ });
+
+ it('does not fetch or listen when disabled, and hides previously cached receipts', async () => {
+ const h = mount(false);
+ await settle();
+ expect(h.client.listSlackImports).not.toHaveBeenCalled();
+ expect(h.client.getSlackImport).not.toHaveBeenCalled();
+ expect(h.listeners.size).toBe(0);
+ expect(h.source.page()).toBeUndefined();
+ expect(h.source.job()).toBeUndefined();
+ h.setEnabled(true);
+ await settle();
+ expect(h.source.job()?.jobId).toBe(jobId);
+ h.setEnabled(false);
+ expect(h.source.job()).toBeUndefined();
+ expect(h.source.page()).toBeUndefined();
+ await settle();
+ expect(h.listeners.size).toBe(0);
+ const calls = h.client.getSlackImport.mock.calls.length;
+ await vi.advanceTimersByTimeAsync(30000);
+ expect(h.client.getSlackImport).toHaveBeenCalledTimes(calls);
+ await h.source.refresh();
+ expect(h.client.getSlackImport).toHaveBeenCalledTimes(calls);
+ });
+
+ it('gates absent teams and jobs explicitly', async () => {
+ const h = mount(false);
+ h.setTeam(undefined);
+ h.setJob(undefined);
+ h.setEnabled(true);
+ await settle();
+ expect(h.client.listSlackImports).not.toHaveBeenCalled();
+ h.setTeam(teamId);
+ await settle();
+ expect(h.client.listSlackImports).toHaveBeenCalledOnce();
+ expect(h.client.getSlackImport).not.toHaveBeenCalled();
+ expect(h.source.job()).toBeUndefined();
+ });
+
+ it('never displays an old team/job while a new scope is pending and cleans up listeners', async () => {
+ const h = mount();
+ await settle();
+ expect(h.listeners.size).toBe(1);
+ const oldListener = [...h.listeners][0];
+ h.client.getSlackImport.mockImplementation(
+ async () => new Promise(() => {})
+ );
+ h.client.listSlackImports.mockImplementation(
+ async () => new Promise(() => {})
+ );
+ h.setTeam(otherTeam);
+ expect(h.source.job()).toBeUndefined();
+ expect(h.source.page()).toBeUndefined();
+ await settle();
+ expect(h.listeners.size).toBe(1);
+ expect(h.listeners.has(oldListener)).toBe(false);
+ h.setTeam(teamId);
+ await settle();
+ h.setJob(otherJob);
+ expect(h.source.job()).toBeUndefined();
+ h.dispose();
+ expect(h.listeners.size).toBe(0);
+ expect(h.source.job()).toBeUndefined();
+ });
+
+ it('ignores stale in-flight responses after a team switch', async () => {
+ const h = mount(false);
+ const pending =
+ Promise.withResolvers<
+ Awaited<ReturnType<SlackImportClient['getSlackImport']>>
+ >();
+ h.client.getSlackImport.mockReturnValue(pending.promise);
+ h.setEnabled(true);
+ await settle();
+ h.client.getSlackImport.mockImplementation(
+ async () => new Promise(() => {})
+ );
+ h.setTeam(otherTeam);
+ await settle();
+ pending.resolve(ok(receipt()));
+ await settle();
+ expect(h.source.job()).toBeUndefined();
+ });
+
+ it('accepts string/object events, ignores malformed/unrelated events, and scopes invalidation', async () => {
+ const h = mount();
+ await settle();
+ const invalidate = vi.spyOn(h.queryClient, 'invalidateQueries');
+ const event = { teamId, jobId, revision: 2, status: 'processing' };
+ for (const data of [
+ 'bad',
+ null,
+ {},
+ { ...event, revision: '2' },
+ { ...event, teamId: otherTeam },
+ ])
+ h.emit(data);
+ h.emit(event, 'import_updated');
+ expect(invalidate).not.toHaveBeenCalled();
+ h.emit(event);
+ h.emit(JSON.stringify(event));
+ expect(invalidate).toHaveBeenCalledTimes(4);
+ expect(invalidate).toHaveBeenCalledWith({
+ queryKey: slackImportKeys.job(teamId, jobId).queryKey,
+ exact: true,
+ });
+ h.dispose();
+ h.emit(event);
+ expect(invalidate).toHaveBeenCalledTimes(4);
+ });
+
+ it('retains available data through background failures and refresh awaits requests', async () => {
+ const h = mount();
+ await settle();
+ h.client.getSlackImport.mockResolvedValue(
+ err([{ code: 'NETWORK_ERROR', message: 'offline' }])
+ );
+ await expect(h.source.refresh()).rejects.toThrow('offline');
+ await settle();
+ expect(h.source.job()?.jobId).toBe(jobId);
+ expect(h.source.error()).toBeInstanceOf(Error);
+ });
+});
+
+const createRequest: CreateImport = {
+ idempotencyToken: jobId,
+ includeMessageHistory: false,
+ source: { kind: 'confirmed_unknown' },
+ conversations: [
+ {
+ slackChannelId: 'C123',
+ kind: 'public_channel',
+ name: 'General',
+ folder: 'general',
+ memberIds: [],
+ creatorId: null,
+ createdAt: null,
+ archived: false,
+ messageCount: null,
+ },
+ ],
+};
+const grant: UploadGrant = {
+ descriptor: {
+ upload: { kind: 'users' },
+ sha256: 'a'.repeat(64),
+ byteLength: 2,
+ },
+ expiresAt: '2099-01-01T00:00:00Z',
+ url: 'https://storage.invalid/upload',
+ requiredHeaders: {},
+};
+
+describe('import commands', () => {
+ it('serializes exactly the frozen A/C confirmation through the real storage client, including retry and zero-history seals', async () => {
+ vi.useRealTimers();
+ vi.stubGlobal('crypto', webcrypto);
+ const fake = fakeImportSources();
+ const found = archiveDiscovery();
+ found.conversations = ['CA', 'CB', 'CC'].map((slackChannelId) => ({
+ ...found.conversations[0],
+ slackChannelId,
+ name: 'duplicate name',
+ folder: slackChannelId,
+ memberIds: ['U1', 'U2'],
+ creatorId: 'U1',
+ createdAt: '1700000000.000001',
+ archived: slackChannelId === 'CC',
+ messageCount: null,
+ }));
+ fake.archive.discover.mockResolvedValue(found);
+ fake.archive.prepare.mockImplementation(async (options) => {
+ expect(options.selectedIds).toEqual(['CA', 'CC']);
+ expect(options.includeMessageHistory).toBe(false);
+ for (const slackChannelId of options.selectedIds)
+ await options.seal({
+ slackChannelId,
+ partCount: 0,
+ manifestSha256: 'b'.repeat(64),
+ });
+ });
+ const confirmed: CreateImport = {
+ idempotencyToken: jobId,
+ source: { kind: 'confirmed_unknown' },
+ includeMessageHistory: false,
+ conversations: structuredClone([
+ found.conversations[0],
+ found.conversations[2],
+ ]),
+ };
+ const persisted = receipt({
+ conversations: confirmed.conversations.map((metadata) => ({
+ ...receipt().conversations[0],
+ slackChannelId: metadata.slackChannelId,
+ name: metadata.name,
+ kind: metadata.kind,
+ archived: metadata.archived,
+ })),
+ });
+ const creates: unknown[] = [];
+ const writes: { url: string; body: unknown }[] = [];
+ vi.mocked(fetchWithToken).mockImplementation(async (input, init) => {
+ const url = typeof input === 'string' ? input : input.url;
+ const body =
+ typeof init?.body === 'string' ? JSON.parse(init.body) : undefined;
+ if (init?.method === 'POST') writes.push({ url, body });
+ if (url.endsWith('/slack/imports') && init?.method === 'POST') {
+ creates.push(body);
+ // A caller mutating discovery after confirmation must not change the retry.
+ found.conversations[0].memberIds.push('U3');
+ found.conversations[2].name = 'changed locally';
+ if (creates.length === 1)
+ return err([{ code: 'NETWORK_ERROR', message: 'lost response' }]);
+ return ok(persisted);
+ }
+ if (url.endsWith('/uploads'))
+ return ok(
+ body.descriptors.map((descriptor: UploadGrant['descriptor']) => ({
+ ...grant,
+ descriptor,
+ }))
+ );
+ if (url.endsWith('/slack/imports')) return ok(page(persisted));
+ return ok(persisted);
+ });
+ const adapter = mount(true, storageServiceClient);
+ const controller = createRoot((dispose) => {
+ cleanups.push(dispose);
+ return createImportController({
+ ...fake,
+ teamId,
+ commands: adapter.commands,
+ source: adapter.source,
+ newToken: () => jobId,
+ });
+ });
+ await controller.discover(new Blob(['zip']));
+ expect(writes).toEqual([]);
+ const selection = {
+ selectedIds: ['CA', 'CC'],
+ includeMessageHistory: false,
+ sourceConfirmed: true,
+ };
+ await Promise.all([
+ controller.start(selection),
+ controller.start({ ...selection, selectedIds: ['CB'] }),
+ ]);
+ expect(controller.phase()).toBe('monitoring');
+ expect(creates).toEqual([confirmed, confirmed]);
+ expect(
+ writes
+ .filter((write) => write.url.endsWith('/uploads'))
+ .map((write) => write.body)
+ ).toEqual([
+ { descriptors: [expect.objectContaining({ upload: { kind: 'users' } })] },
+ ]);
+ expect(
+ writes
+ .filter((write) => write.url.endsWith('/uploads/complete'))
+ .map((write) => write.body)
+ ).toEqual(
+ expect.arrayContaining([
+ {
+ uploads: [],
+ seal: {
+ slackChannelId: 'CA',
+ partCount: 0,
+ manifestSha256: 'b'.repeat(64),
+ },
+ },
+ {
+ uploads: [],
+ seal: {
+ slackChannelId: 'CC',
+ partCount: 0,
+ manifestSha256: 'b'.repeat(64),
+ },
+ },
+ ])
+ );
+ await adapter.source.refresh();
+ expect(
+ adapter.source
+ .job()
+ ?.conversations.map((conversation) => conversation.slackChannelId)
+ ).toEqual(['CA', 'CC']);
+ expect(adapter.source.job()?.includeMessageHistory).toBe(false);
+ });
+
+ it('rejects oversized route batches before sending them', async () => {
+ const h = mount();
+ await expect(
+ h.commands.register(
+ identity,
+ Array.from({ length: 51 }, () => grant.descriptor)
+ )
+ ).rejects.toThrow('batch limit');
+ await expect(
+ h.commands.complete(
+ identity,
+ Array.from({ length: 51 }, () => grant.descriptor.upload)
+ )
+ ).rejects.toThrow('batch limit');
+ expect(h.client.registerSlackImportUploads).not.toHaveBeenCalled();
+ expect(h.client.completeSlackImportUploads).not.toHaveBeenCalled();
+ });
+
+ it('preserves upload error codes for grant renewal without completing failed uploads', async () => {
+ const h = mount();
+ h.client.registerSlackImportUploads.mockResolvedValue(ok([grant]));
+ const [capability] = await h.commands.register(identity, [
+ grant.descriptor,
+ ]);
+ h.upload.mockResolvedValue(err({ code: 'EXPIRED' }));
+ await expect(capability.put(new Blob(['{}']))).rejects.toEqual(
+ new ImportUploadError('EXPIRED')
+ );
+ h.upload.mockResolvedValue(err({ code: 'HTTP_ERROR', status: 403 }));
+ await expect(capability.put(new Blob(['{}']))).rejects.toMatchObject({
+ code: 'HTTP_ERROR',
+ status: 403,
+ });
+ expect(h.client.completeSlackImportUploads).not.toHaveBeenCalled();
+ });
+
+ it('keeps failed route requests separate from successful mutations and invalidates only the captured scope', async () => {
+ const h = mount();
+ await settle();
+ const invalidate = vi.spyOn(h.queryClient, 'invalidateQueries');
+ h.client.finalizeSlackImport.mockResolvedValueOnce(
+ err([{ code: 'NETWORK_ERROR', message: 'offline' }])
+ );
+ await expect(h.commands.finalize(identity)).rejects.toThrow('offline');
+ expect(invalidate).not.toHaveBeenCalled();
+ await h.commands.finalize(identity);
+ expect(invalidate).toHaveBeenCalledWith({
+ queryKey: slackImportKeys.job(teamId, jobId).queryKey,
+ exact: true,
+ });
+ });
+ it('routes mutations and bounded PUTs through injected storage capabilities', async () => {
+ const h = mount();
+ await settle();
+ expect((await h.commands.create(teamId, createRequest)).teamId).toBe(
+ teamId
+ );
+ expect(h.client.createSlackImport).toHaveBeenCalledWith({
+ body: createRequest,
+ });
+ h.client.registerSlackImportUploads.mockResolvedValue(ok([grant]));
+ const [capability] = await h.commands.register(identity, [
+ grant.descriptor,
+ ]);
+ expect(h.client.registerSlackImportUploads).toHaveBeenCalledWith({
+ jobId,
+ body: { descriptors: [grant.descriptor] },
+ });
+ expect(capability).not.toHaveProperty('url');
+ h.upload.mockResolvedValue(ok('already-exists'));
+ const blob = new Blob(['{}']);
+ expect(await capability.put(blob)).toBe('already-exists');
+ expect(h.upload).toHaveBeenCalledWith(
+ grant,
+ blob,
+ expect.objectContaining({ signal: expect.any(AbortSignal) })
+ );
+ // A 412 still requires explicit server verification, never automatic overwrite/completion.
+ expect(h.client.completeSlackImportUploads).not.toHaveBeenCalled();
+ const seal = {
+ slackChannelId: 'C123',
+ partCount: 0,
+ manifestSha256: 'b'.repeat(64),
+ };
+ await h.commands.complete(identity, [], seal);
+ expect(h.client.completeSlackImportUploads).toHaveBeenCalledWith({
+ jobId,
+ body: { uploads: [], seal },
+ });
+ await h.commands.finalize(identity);
+ expect(h.client.finalizeSlackImport).toHaveBeenCalledWith({ jobId });
+ expect((await h.commands.cancel(identity)).status).toBe('cancelled');
+ expect(h.client.cancelSlackImport).toHaveBeenCalledWith({ jobId });
+ });
+
+ it('rejects stale commands, late mutation responses and stale upload grants', async () => {
+ const h = mount();
+ h.client.registerSlackImportUploads.mockResolvedValue(ok([grant]));
+ const [capability] = await h.commands.register(identity, [
+ grant.descriptor,
+ ]);
+ const pending =
+ Promise.withResolvers<
+ Awaited<ReturnType<SlackImportClient['createSlackImport']>>
+ >();
+ h.client.createSlackImport.mockReturnValue(pending.promise);
+ const result = h.commands.create(teamId, createRequest);
+ const assertion = expect(result).rejects.toThrow('scope');
+ h.setTeam(otherTeam);
+ await settle();
+ pending.resolve(ok(receipt()));
+ await assertion;
+ await expect(h.commands.cancel(identity)).rejects.toThrow('scope');
+ await expect(capability.put(new Blob(['{}']))).rejects.toThrow('scope');
+ h.setTeam(teamId);
+ await expect(capability.put(new Blob(['{}']))).rejects.toThrow('scope');
+ expect(h.upload).not.toHaveBeenCalled();
+ expect(h.client.cancelSlackImport).not.toHaveBeenCalled();
+ });
+
+ it.each(['disposal', 'team change'] as const)(
+ 'aborts active uploads on %s',
+ async (action) => {
+ const h = mount();
+ h.client.registerSlackImportUploads.mockResolvedValue(ok([grant]));
+ const [capability] = await h.commands.register(identity, [
+ grant.descriptor,
+ ]);
+ h.upload.mockImplementation(
+ async (_grant, _blob, options) =>
+ new Promise((resolve) => {
+ options?.signal?.addEventListener(
+ 'abort',
+ () => resolve(err({ code: 'ABORTED' })),
+ { once: true }
+ );
+ })
+ );
+ const result = capability.put(new Blob(['{}']));
+ const assertion = expect(result).rejects.toThrow('scope');
+ if (action === 'disposal') h.dispose();
+ else h.setTeam(otherTeam);
+ await assertion;
+ await expect(h.commands.finalize(identity)).rejects.toThrow('scope');
+ }
+ );
+});
diff --git a/apps/web/src/features/slack-import/queries/import-source.ts b/apps/web/src/features/slack-import/queries/import-source.ts
new file mode 100644
index 0000000000..c58890d75f
--- /dev/null
+++ b/apps/web/src/features/slack-import/queries/import-source.ts
@@ -0,0 +1,283 @@
+import {
+ invalidateSlackImport,
+ parseSlackImportUpdate,
+ type SlackImportClient,
+ slackImportJobOptions,
+ slackImportListOptions,
+ slackImportMutationOptions,
+} from '@queries/slack-import';
+import type { createConnectionWebsocketEffect } from '@service-connection/websocket';
+import type { ImportLimits as WireLimits } from '@service-storage/generated/schemas/importLimits';
+import type { ImportProgress } from '@service-storage/generated/schemas/importProgress';
+import type { uploadSlackImport } from '@service-storage/slack-import-upload';
+import { type QueryClient, useMutation, useQuery } from '@tanstack/solid-query';
+import { createEffect, createMemo, on, onCleanup } from 'solid-js';
+import {
+ type ImportCommands,
+ type ImportJob,
+ type ImportLimits,
+ type ImportSource,
+ type ImportSourceInputs,
+ ImportUploadError,
+} from '../context/contracts';
+
+export type ImportSourceDependencies = {
+ client: SlackImportClient;
+ queryClient: QueryClient;
+ /** Supply the existing generic gateway effect from the app-facing entry point. */
+ gatewayEffect: typeof createConnectionWebsocketEffect;
+ upload: typeof uploadSlackImport;
+};
+
+function toLimits(limits: WireLimits): ImportLimits {
+ return {
+ conversations: limits.conversations,
+ jsonBytes: limits.jsonBytes,
+ selectedBytes: limits.selectedBytes,
+ zipEntries: limits.zipEntries,
+ partBytes: limits.partBytes,
+ partRecords: limits.partRecords,
+ recordBytes: limits.recordBytes,
+ registrationBatch: limits.registrationBatch,
+ };
+}
+
+export function toImportJob(teamId: string, job: ImportProgress): ImportJob {
+ return {
+ teamId,
+ source: { ...job.source },
+ includeMessageHistory: job.includeMessageHistory,
+ jobId: job.jobId,
+ status: job.status,
+ revision: job.revision,
+ createdAt: job.createdAt,
+ updatedAt: job.updatedAt,
+ registrationClosed: job.registrationClosedAt != null,
+ usersVerified: job.usersVerified,
+ limits: toLimits(job.limits),
+ conversations: job.conversations.map((conversation) => ({
+ slackChannelId: conversation.slackChannelId,
+ name: conversation.name,
+ kind: conversation.kind,
+ archived: conversation.archived,
+ channelId:
+ conversation.status === 'skipped'
+ ? undefined
+ : (conversation.channelId ?? undefined),
+ status: conversation.status,
+ counters: { ...conversation.counters },
+ partCount: conversation.partCount ?? undefined,
+ verifiedParts: conversation.verifiedParts,
+ searchStatus: conversation.search.status,
+ error: conversation.error ?? undefined,
+ warnings: [...conversation.warnings],
+ })),
+ };
+}
+
+/** Construct under the consuming Solid owner; no production services are imported at runtime. */
+export function createImportSource(
+ dependencies: ImportSourceDependencies,
+ inputs: ImportSourceInputs
+): { source: ImportSource; commands: ImportCommands } {
+ const { client, queryClient, gatewayEffect, upload } = dependencies;
+ let disposed = false;
+ const scope = createMemo(() => ({
+ teamId: inputs.teamId(),
+ enabled: inputs.enabled(),
+ }));
+ const enabled = () => !disposed && scope().enabled && Boolean(scope().teamId);
+ const activeUploads = new Set<AbortController>();
+ function abortUploads(): void {
+ for (const controller of activeUploads) controller.abort();
+ activeUploads.clear();
+ }
+ onCleanup(() => {
+ disposed = true;
+ abortUploads();
+ });
+
+ const pageQuery = useQuery(
+ () =>
+ slackImportListOptions(
+ client,
+ inputs.teamId() ?? '',
+ enabled(),
+ inputs.before?.()
+ ),
+ () => queryClient
+ );
+ const jobQuery = useQuery(
+ () =>
+ slackImportJobOptions(
+ client,
+ { teamId: inputs.teamId() ?? '', jobId: inputs.jobId() ?? '' },
+ enabled()
+ ),
+ () => queryClient
+ );
+
+ createEffect(
+ on(scope, () => {
+ onCleanup(abortUploads);
+ if (!enabled()) return;
+ gatewayEffect((message) => {
+ if (!enabled() || message.type !== 'slack_import_updated') return;
+ const event = parseSlackImportUpdate(message.data);
+ if (!event || event.teamId !== inputs.teamId()) return;
+ void invalidateSlackImport(queryClient, event);
+ });
+ })
+ );
+
+ const page = createMemo(() => {
+ if (!enabled() || !(pageQuery.isSuccess || pageQuery.isRefetchError))
+ return undefined;
+ const data = pageQuery.data;
+ if (!data?.value || !data.teamId || data.teamId !== inputs.teamId())
+ return undefined;
+ const page = data.value;
+ const teamId = data.teamId;
+ return {
+ jobs: page.jobs.map((job) => toImportJob(teamId, job)),
+ limits: toLimits(page.limits),
+ sourceBinding: { ...page.sourceBinding },
+ nextCursor: page.nextCursor ?? undefined,
+ };
+ });
+ const job = createMemo(() => {
+ if (
+ !enabled() ||
+ !inputs.jobId() ||
+ !(jobQuery.isSuccess || jobQuery.isRefetchError)
+ )
+ return undefined;
+ const data = jobQuery.data;
+ if (
+ !data?.value ||
+ !data.teamId ||
+ data.teamId !== inputs.teamId() ||
+ data.value.jobId !== inputs.jobId()
+ )
+ return undefined;
+ return toImportJob(data.teamId, data.value);
+ });
+ const source: ImportSource = {
+ page: () => (enabled() ? page() : undefined),
+ job: () => (enabled() ? job() : undefined),
+ isLoading: () =>
+ enabled() &&
+ ((pageQuery.isPlaceholderData && pageQuery.isFetching) ||
+ (jobQuery.isPlaceholderData && jobQuery.isFetching)),
+ error: () =>
+ enabled() ? (pageQuery.error ?? jobQuery.error ?? undefined) : undefined,
+ async refresh() {
+ if (!enabled()) return;
+ await Promise.all([
+ pageQuery.refetch({ throwOnError: true }),
+ ...(inputs.jobId() ? [jobQuery.refetch({ throwOnError: true })] : []),
+ ]);
+ },
+ };
+
+ const options = slackImportMutationOptions(client, queryClient);
+ const create = useMutation(
+ () => options.create,
+ () => queryClient
+ );
+ const register = useMutation(
+ () => options.register,
+ () => queryClient
+ );
+ const complete = useMutation(
+ () => options.complete,
+ () => queryClient
+ );
+ const finalize = useMutation(
+ () => options.finalize,
+ () => queryClient
+ );
+ const cancel = useMutation(
+ () => options.cancel,
+ () => queryClient
+ );
+
+ function requireScope(teamId: string): () => void {
+ const started = scope();
+ function check(): void {
+ if (!enabled() || started !== scope() || teamId !== inputs.teamId()) {
+ throw new Error('Slack import scope is no longer active');
+ }
+ }
+ check();
+ return check;
+ }
+
+ const commands: ImportCommands = {
+ async create(teamId, body) {
+ const check = requireScope(teamId);
+ const job = await create.mutateAsync({ teamId, body });
+ check();
+ return toImportJob(teamId, job);
+ },
+ async register(identity, descriptors) {
+ const check = requireScope(identity.teamId);
+ const grants = await register.mutateAsync({
+ ...identity,
+ body: { descriptors },
+ });
+ check();
+ return grants.map((grant) => ({
+ descriptor: {
+ ...grant.descriptor,
+ upload: { ...grant.descriptor.upload },
+ },
+ expiresAt: grant.expiresAt,
+ async put(blob, options) {
+ check();
+ const controller = new AbortController();
+ activeUploads.add(controller);
+ try {
+ const signal = options?.signal
+ ? AbortSignal.any([options.signal, controller.signal])
+ : controller.signal;
+ const result = await upload(grant, blob, { ...options, signal });
+ check();
+ if (result.isErr()) {
+ const error = result.error;
+ throw new ImportUploadError(
+ error.code,
+ error.code === 'HTTP_ERROR' ? error.status : undefined
+ );
+ }
+ return result.value;
+ } finally {
+ activeUploads.delete(controller);
+ }
+ },
+ }));
+ },
+ async complete(identity, uploads, seal) {
+ const check = requireScope(identity.teamId);
+ const job = await complete.mutateAsync({
+ ...identity,
+ body: { uploads, seal },
+ });
+ check();
+ return toImportJob(identity.teamId, job);
+ },
+ async finalize(identity) {
+ const check = requireScope(identity.teamId);
+ const job = await finalize.mutateAsync(identity);
+ check();
+ return toImportJob(identity.teamId, job);
+ },
+ async cancel(identity) {
+ const check = requireScope(identity.teamId);
+ const job = await cancel.mutateAsync(identity);
+ check();
+ return toImportJob(identity.teamId, job);
+ },
+ };
+ return { source, commands };
+}
diff --git a/apps/web/src/features/slack-import/slack-import.test.tsx b/apps/web/src/features/slack-import/slack-import.test.tsx
new file mode 100644
index 0000000000..607c6d2b98
--- /dev/null
+++ b/apps/web/src/features/slack-import/slack-import.test.tsx
@@ -0,0 +1,112 @@
+import { cleanup, render, screen, waitFor } from '@solidjs/testing-library';
+import { createSignal } from 'solid-js';
+import { afterEach, describe, expect, it, vi } from 'vitest';
+import { importReceipt } from './tests/fake-sources';
+
+const host = vi.hoisted(() => ({
+ enabled: true,
+ channels: vi.fn(() => ({ isSuccess: false, data: [] as { id: string }[] })),
+ invalidateChannels: vi.fn(async () => {}),
+ invalidateSoup: vi.fn(),
+ revalidate: vi.fn(async () => {}),
+ createSource: vi.fn(),
+ view: vi.fn(),
+ props: undefined as
+ | {
+ onCompleted(): Promise<void>;
+ channelHref(id: string): string | undefined;
+ }
+ | undefined,
+}));
+vi.mock('@app/lib/analytics/posthog', () => ({
+ useFeatureFlag: () => () => ({ enabled: host.enabled }),
+}));
+vi.mock('@core/constant/featureFlags', () => ({
+ enableSlackArchiveImport: {},
+}));
+vi.mock('@core/util/reloadForNewerBuild', () => ({
+ holdAutomaticReload: vi.fn(),
+}));
+vi.mock('@queries/channel/channels', () => ({
+ useListChannelsQuery: host.channels,
+ invalidateListChannels: host.invalidateChannels,
+}));
+vi.mock('@queries/client', () => ({ queryClient: {} }));
+vi.mock('@queries/soup/graphql/channel-list-revalidation', () => ({
+ revalidateChannelLists: host.revalidate,
+}));
+vi.mock('@queries/soup/normalized-cache', () => ({
+ invalidateAllSoup: host.invalidateSoup,
+}));
+vi.mock('@service-connection/websocket', () => ({
+ createConnectionWebsocketEffect: vi.fn(),
+}));
+vi.mock('@service-storage/client', () => ({ storageServiceClient: {} }));
+vi.mock('@service-storage/graphql-soup', () => ({
+ getGraphqlSoupClient: () => ({}),
+}));
+vi.mock('@service-storage/slack-import-upload', () => ({
+ uploadSlackImport: vi.fn(),
+}));
+vi.mock('./queries/import-source', () => ({
+ createImportSource: host.createSource,
+}));
+vi.mock('./queries/archive-source', () => ({
+ createArchiveSource: vi.fn(),
+ protectImportFile: vi.fn(),
+}));
+vi.mock('./views/import-dialog', async () => {
+ const { useImportContext } = await import('./context/import-context');
+ return {
+ ImportDialog: (
+ props: NonNullable<typeof host.props> & { teamId: string }
+ ) => {
+ host.view(props.teamId);
+ host.props = props;
+ useImportContext().createSource({
+ teamId: () => props.teamId,
+ jobId: () => undefined,
+ enabled: () => true,
+ });
+ return <p>Mounted import</p>;
+ },
+ };
+});
+
+import { SlackImport } from './slack-import';
+
+afterEach(() => {
+ cleanup();
+ vi.clearAllMocks();
+ host.enabled = true;
+});
+
+describe('Slack import production boundary', () => {
+ it.each([
+ { isAdmin: false, enabled: true },
+ { isAdmin: true, enabled: false },
+ ])('mounts no queries for $isAdmin admin / $enabled flag', (gate) => {
+ host.enabled = gate.enabled;
+ render(() => <SlackImport teamId="team" isAdmin={gate.isAdmin} />);
+ expect(screen.queryByText('Mounted import')).toBeNull();
+ expect(host.channels).not.toHaveBeenCalled();
+ expect(host.createSource).not.toHaveBeenCalled();
+ });
+
+ it('remounts on team changes, unmounts on role revocation and wires all completion refreshes', async () => {
+ const [admin, setAdmin] = createSignal(true);
+ const [teamId, setTeamId] = createSignal('one');
+ render(() => <SlackImport teamId={teamId()} isAdmin={admin()} />);
+ await screen.findByText('Mounted import');
+ expect(host.createSource).toHaveBeenCalledOnce();
+ await host.props!.onCompleted();
+ expect(host.invalidateChannels).toHaveBeenCalledOnce();
+ expect(host.invalidateSoup).toHaveBeenCalledOnce();
+ expect(host.revalidate).toHaveBeenCalledOnce();
+ expect(host.props!.channelHref(importReceipt().jobId)).toBeUndefined();
+ setTeamId('two');
+ await waitFor(() => expect(host.view).toHaveBeenLastCalledWith('two'));
+ setAdmin(false);
+ expect(screen.queryByText('Mounted import')).toBeNull();
+ });
+});
diff --git a/apps/web/src/features/slack-import/slack-import.tsx b/apps/web/src/features/slack-import/slack-import.tsx
new file mode 100644
index 0000000000..9e80f1ae20
--- /dev/null
+++ b/apps/web/src/features/slack-import/slack-import.tsx
@@ -0,0 +1,89 @@
+import { useFeatureFlag } from '@app/lib/analytics/posthog';
+import { enableSlackArchiveImport } from '@core/constant/featureFlags';
+import { holdAutomaticReload } from '@core/util/reloadForNewerBuild';
+import {
+ invalidateListChannels,
+ useListChannelsQuery,
+} from '@queries/channel/channels';
+import { queryClient } from '@queries/client';
+import { revalidateChannelLists } from '@queries/soup/graphql/channel-list-revalidation';
+import { invalidateAllSoup } from '@queries/soup/normalized-cache';
+import { createConnectionWebsocketEffect } from '@service-connection/websocket';
+// ast-grep-ignore: tsx-no-service-client-outside-queries -- Composition only; endpoint calls remain in the query adapter.
+import { storageServiceClient } from '@service-storage/client';
+import { getGraphqlSoupClient } from '@service-storage/graphql-soup';
+import { uploadSlackImport } from '@service-storage/slack-import-upload';
+import { type JSX, Show, Suspense } from 'solid-js';
+import { ImportProvider } from './context/import-context';
+import {
+ createArchiveSource,
+ protectImportFile,
+} from './queries/archive-source';
+import { createImportSource } from './queries/import-source';
+import { ImportDialog } from './views/import-dialog';
+
+type Props = { teamId: string; isAdmin: boolean };
+
+export function SlackImport(props: Props): JSX.Element {
+ const flag = useFeatureFlag(enableSlackArchiveImport);
+ return (
+ <Show when={props.isAdmin && flag().enabled}>
+ <Show when={props.teamId} keyed>
+ {(teamId) => (
+ <Suspense
+ fallback={
+ <p class="text-sm text-ink-muted">Loading Slack import…</p>
+ }
+ >
+ <SlackImportSettings teamId={teamId} />
+ </Suspense>
+ )}
+ </Show>
+ </Show>
+ );
+}
+
+function SlackImportSettings(props: { teamId: string }): JSX.Element {
+ const channels = useListChannelsQuery();
+ const client = getGraphqlSoupClient();
+ function channelHref(id: string): string | undefined {
+ if (
+ !channels.isSuccess ||
+ !channels.data.some((channel) => channel.id === id)
+ )
+ return undefined;
+ return `/app/channel/${encodeURIComponent(id)}`;
+ }
+ async function onCompleted(): Promise<void> {
+ invalidateAllSoup();
+ await Promise.all([
+ invalidateListChannels(),
+ revalidateChannelLists(client),
+ ]);
+ }
+ return (
+ <ImportProvider
+ context={{
+ createSource: (inputs) =>
+ createImportSource(
+ {
+ client: storageServiceClient,
+ queryClient,
+ gatewayEffect: createConnectionWebsocketEffect,
+ upload: uploadSlackImport,
+ },
+ inputs
+ ),
+ createArchive: createArchiveSource,
+ protectFile: () => protectImportFile(holdAutomaticReload),
+ newToken: () => crypto.randomUUID(),
+ }}
+ >
+ <ImportDialog
+ teamId={props.teamId}
+ onCompleted={onCompleted}
+ channelHref={channelHref}
+ />
+ </ImportProvider>
+ );
+}
diff --git a/apps/web/src/features/slack-import/tests/fake-sources.ts b/apps/web/src/features/slack-import/tests/fake-sources.ts
new file mode 100644
index 0000000000..f8bf4ede3d
--- /dev/null
+++ b/apps/web/src/features/slack-import/tests/fake-sources.ts
@@ -0,0 +1,212 @@
+import { createSignal } from 'solid-js';
+import { vi } from 'vitest';
+import type {
+ ArchiveSource,
+ ImportCommands,
+ ImportJob,
+ ImportLimits,
+ ImportSource,
+ ImportSourceBinding,
+ ImportUpload,
+} from '../context/contracts';
+import type { ArchiveDiscovery } from '../core/export';
+import type { ConversationSeal, PartDescriptor } from '../core/worker-protocol';
+
+export function deferred<T>(): {
+ promise: Promise<T>;
+ resolve(value: T): void;
+ reject(error: Error): void;
+} {
+ let resolve!: (value: T) => void;
+ let reject!: (error: Error) => void;
+ const promise = new Promise<T>((yes, no) => {
+ resolve = yes;
+ reject = no;
+ });
+ return { promise, resolve, reject };
+}
+
+export const importLimits: ImportLimits = {
+ conversations: 2000,
+ jsonBytes: 33554432,
+ selectedBytes: 2147483648,
+ zipEntries: 100000,
+ partBytes: 16777216,
+ partRecords: 20000,
+ recordBytes: 1048576,
+ registrationBatch: 50,
+};
+
+export function archiveDiscovery(): ArchiveDiscovery {
+ return {
+ source: { kind: 'unknown' },
+ users: [],
+ dayEntries: [],
+ conversations: [
+ {
+ slackChannelId: 'C123',
+ kind: 'public_channel',
+ name: 'general',
+ folder: 'general',
+ memberIds: [],
+ creatorId: null,
+ createdAt: null,
+ archived: false,
+ messageCount: null,
+ },
+ ],
+ };
+}
+
+export function importReceipt(overrides: Partial<ImportJob> = {}): ImportJob {
+ return {
+ teamId: 'team',
+ source: { kind: 'confirmed_unknown' },
+ includeMessageHistory: true,
+ jobId: 'job',
+ status: 'uploading',
+ revision: 1,
+ createdAt: '',
+ updatedAt: '',
+ registrationClosed: false,
+ usersVerified: false,
+ limits: importLimits,
+ conversations: [
+ {
+ slackChannelId: 'C123',
+ name: 'general',
+ kind: 'public_channel',
+ archived: false,
+ channelId: undefined,
+ status: 'awaiting_uploads',
+ counters: {
+ processed: 0,
+ imported: 0,
+ duplicates: 0,
+ skipped: 0,
+ reactions: 0,
+ },
+ partCount: undefined,
+ verifiedParts: 0,
+ searchStatus: 'not_needed',
+ error: undefined,
+ warnings: [],
+ },
+ ],
+ ...overrides,
+ };
+}
+
+export function historyPart(partIndex = 0): PartDescriptor {
+ return {
+ upload: { kind: 'conversation_part', slackChannelId: 'C123', partIndex },
+ sha256: 'a'.repeat(64),
+ byteLength: 2,
+ recordCount: 1,
+ };
+}
+
+export function historySeal(partCount = 1): ConversationSeal {
+ return { slackChannelId: 'C123', partCount, manifestSha256: 'b'.repeat(64) };
+}
+
+export function fakeImportSources() {
+ const [observed, setObserved] = createSignal<ImportJob>();
+ const [binding, setBinding] = createSignal<ImportSourceBinding>({
+ kind: 'unbound',
+ });
+ const events: string[] = [];
+ const stop = new AbortController();
+ const release = vi.fn();
+ const put = vi.fn<ImportUpload['put']>(async () => 'uploaded');
+ const archive = {
+ discover: vi.fn<ArchiveSource['discover']>(async () => archiveDiscovery()),
+ prepare: vi.fn<ArchiveSource['prepare']>(async (options) => {
+ if (options.includeMessageHistory)
+ await options.part(historyPart(), new Blob(['[]']));
+ await options.seal(historySeal(options.includeMessageHistory ? 1 : 0));
+ }),
+ dispose: vi.fn<ArchiveSource['dispose']>(async () => {
+ stop.abort();
+ }),
+ };
+ let receipt = importReceipt();
+ function update(overrides: Partial<ImportJob>): ImportJob {
+ receipt = { ...receipt, ...overrides, revision: receipt.revision + 1 };
+ return receipt;
+ }
+ const commands = {
+ create: vi.fn<ImportCommands['create']>(async (_, request) => {
+ events.push('create');
+ receipt = {
+ ...receipt,
+ source: structuredClone(request.source),
+ includeMessageHistory: request.includeMessageHistory,
+ conversations: request.conversations.map((conversation) => ({
+ ...importReceipt().conversations[0],
+ slackChannelId: conversation.slackChannelId,
+ name: conversation.name,
+ kind: conversation.kind,
+ archived: conversation.archived,
+ })),
+ };
+ return receipt;
+ }),
+ register: vi.fn<ImportCommands['register']>(async (_, descriptors) => {
+ return descriptors.map((descriptor) => {
+ events.push(`register:${descriptor.upload.kind}`);
+ return { descriptor: structuredClone(descriptor), expiresAt: '', put };
+ });
+ }),
+ complete: vi.fn<ImportCommands['complete']>(async (_, uploads, seal) => {
+ if (seal) {
+ events.push(`seal:${seal.partCount}`);
+ return update({
+ conversations: receipt.conversations.map((conversation) => ({
+ ...conversation,
+ partCount: seal.partCount,
+ })),
+ });
+ }
+ events.push(`complete:${uploads[0]?.kind}`);
+ return update({
+ usersVerified:
+ receipt.usersVerified ||
+ uploads.some((upload) => upload.kind === 'users'),
+ });
+ }),
+ finalize: vi.fn<ImportCommands['finalize']>(async () => {
+ events.push('finalize');
+ return update({ registrationClosed: true, status: 'processing' });
+ }),
+ cancel: vi.fn<ImportCommands['cancel']>(async () => {
+ events.push('cancel');
+ return update({ registrationClosed: true, status: 'cancelled' });
+ }),
+ };
+ const source: ImportSource = {
+ page: () => undefined,
+ job: observed,
+ isLoading: () => false,
+ error: () => undefined,
+ refresh: vi.fn(async () => {}),
+ };
+ return {
+ teamId: 'team',
+ limits: importLimits,
+ archive,
+ commands,
+ source,
+ sourceBinding: binding,
+ setBinding,
+ setObserved,
+ put,
+ events,
+ stop,
+ protectFile: vi.fn(() => release),
+ release,
+ newToken: vi.fn(() => 'stable-token'),
+ onJobCreated: vi.fn(async (_job: ImportJob) => {}),
+ onFinalized: vi.fn(async (_job: ImportJob) => {}),
+ };
+}
diff --git a/apps/web/src/features/slack-import/tests/zip-fixtures.ts b/apps/web/src/features/slack-import/tests/zip-fixtures.ts
new file mode 100644
index 0000000000..4f759792b1
--- /dev/null
+++ b/apps/web/src/features/slack-import/tests/zip-fixtures.ts
@@ -0,0 +1,146 @@
+import { strToU8, Zip, ZipDeflate, zipSync } from 'fflate';
+
+/** Synthetic archives only; strings represent literal (possibly malformed) file bytes. */
+export function zipFixture(
+ entries: Record<string, unknown>,
+ compressed = true
+): Uint8Array {
+ return zipSync(
+ Object.fromEntries(
+ Object.entries(entries).map(([path, value]) => [
+ path,
+ strToU8(typeof value === 'string' ? value : JSON.stringify(value)),
+ ])
+ ),
+ { level: compressed ? 6 : 0 }
+ );
+}
+
+/** Streaming ZIPs use data descriptors rather than sizes in local headers. */
+export function streamingZipFixture(
+ entries: Record<string, unknown>
+): Uint8Array {
+ const chunks: Uint8Array[] = [];
+ const zip = new Zip((error, chunk) => {
+ if (error) throw error;
+ chunks.push(chunk);
+ });
+ for (const [path, value] of Object.entries(entries)) {
+ const file = new ZipDeflate(path);
+ zip.add(file);
+ file.push(strToU8(JSON.stringify(value)), true);
+ }
+ zip.end();
+ const bytes = new Uint8Array(
+ chunks.reduce((size, chunk) => size + chunk.length, 0)
+ );
+ let offset = 0;
+ for (const chunk of chunks) {
+ bytes.set(chunk, offset);
+ offset += chunk.length;
+ }
+ return bytes;
+}
+
+export function zipBlob(bytes: Uint8Array): Blob {
+ return new Blob([new Uint8Array(bytes).buffer]);
+}
+
+export const USERS = [
+ { id: 'U100', name: 'alice', profile: { email: '[email protected]' } },
+ { id: 'U200', name: 'bob', profile: null },
+];
+export const MESSAGE = {
+ type: 'message',
+ user: 'U100',
+ ts: '1700000000.000001',
+ text: 'Synthetic history',
+};
+
+export function standardEntries(): Record<string, unknown> {
+ return {
+ 'users.json': USERS,
+ 'channels.json': [
+ {
+ id: 'C100',
+ name: 'general',
+ members: ['U100', 'U200'],
+ created: 1700000000,
+ },
+ ],
+ 'general/2023-11-14.json': [MESSAGE],
+ };
+}
+
+export function corporateEntries(): Record<string, unknown> {
+ return {
+ 'users.json': USERS,
+ 'groups.json': [
+ {
+ id: 'C200',
+ name: 'private',
+ members: ['U100', 'U200'],
+ is_archived: true,
+ },
+ ],
+ 'dms.json': [{ id: 'D100', members: ['U100', 'U200'] }],
+ 'mpims.json': [
+ { id: 'G100', name: 'mpdm-example', members: ['U100', 'U200', 'U300'] },
+ ],
+ 'private/2023-11-14.json': [MESSAGE],
+ 'D100/2023-11-14.json': [MESSAGE],
+ 'mpdm-example/2023-11-14.json': [MESSAGE],
+ };
+}
+
+export function enterpriseEntries(): Record<string, unknown> {
+ return {
+ 'org_users.json': [
+ {
+ id: 'W100',
+ name: 'enterprise-alice',
+ profile: { email: '[email protected]' },
+ enterprise_user: { teams: ['T100'] },
+ },
+ ],
+ 'channels.json': [
+ { id: 'C300', name: 'enterprise', members: ['W100', 'U999'] },
+ ],
+ 'enterprise/2023-11-14.json': [MESSAGE],
+ };
+}
+
+/** Mutate paired headers without changing the payload, for adversarial ZIP fixtures. */
+export function patchEntry(
+ bytes: Uint8Array,
+ path: string,
+ patch: (header: DataView, central: boolean) => void
+): Uint8Array {
+ const result = bytes.slice();
+ const data = new DataView(
+ result.buffer,
+ result.byteOffset,
+ result.byteLength
+ );
+ const end = result.length - 22;
+ let offset = data.getUint32(end + 16, true);
+ const count = data.getUint16(end + 10, true);
+ for (let index = 0; index < count; index++) {
+ const nameSize = data.getUint16(offset + 28, true);
+ const name = new TextDecoder().decode(
+ result.subarray(offset + 46, offset + 46 + nameSize)
+ );
+ if (name === path) {
+ const local = data.getUint32(offset + 42, true);
+ patch(new DataView(result.buffer, result.byteOffset + local), false);
+ patch(new DataView(result.buffer, result.byteOffset + offset), true);
+ return result;
+ }
+ offset +=
+ 46 +
+ nameSize +
+ data.getUint16(offset + 30, true) +
+ data.getUint16(offset + 32, true);
+ }
+ throw new Error('Fixture entry not found');
+}
diff --git a/apps/web/src/features/slack-import/views/import-dialog.test.tsx b/apps/web/src/features/slack-import/views/import-dialog.test.tsx
new file mode 100644
index 0000000000..f74ec1669d
--- /dev/null
+++ b/apps/web/src/features/slack-import/views/import-dialog.test.tsx
@@ -0,0 +1,435 @@
+import { webcrypto } from 'node:crypto';
+import {
+ cleanup,
+ fireEvent,
+ render,
+ screen,
+ waitFor,
+} from '@solidjs/testing-library';
+import { createSignal, Suspense } from 'solid-js';
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+import type { ImportPage, ImportSourceInputs } from '../context/contracts';
+import { ImportProvider } from '../context/import-context';
+import {
+ archiveDiscovery,
+ deferred,
+ fakeImportSources,
+ importLimits,
+ importReceipt,
+} from '../tests/fake-sources';
+import { ImportDialog } from './import-dialog';
+
+// Shared responsive Dialog needs a host platform; feature capabilities remain injected.
+vi.mock('@core/mobile/isMobile', () => ({ isMobile: () => false }));
+vi.mock('@core/mobile/virtualKeyboard', () => ({
+ virtualKeyboardVisible: () => false,
+}));
+
+let motionStyles: HTMLStyleElement;
+beforeEach(() => {
+ motionStyles = document.createElement('style');
+ motionStyles.textContent =
+ '* { transition-duration: 0s; animation-name: none; }';
+ document.head.append(motionStyles);
+ vi.stubGlobal('crypto', webcrypto);
+ vi.stubGlobal('scrollTo', vi.fn());
+});
+afterEach(() => {
+ cleanup();
+ motionStyles.remove();
+ vi.unstubAllGlobals();
+});
+
+function setup() {
+ const fake = fakeImportSources();
+ const [page, setPage] = createSignal<ImportPage>({
+ jobs: [],
+ limits: importLimits,
+ sourceBinding: { kind: 'unbound' },
+ nextCursor: undefined,
+ });
+ const [error, setError] = createSignal<Error>();
+ let inputs!: ImportSourceInputs;
+ const createSource = vi.fn((value: ImportSourceInputs) => {
+ inputs = value;
+ return { source: { ...fake.source, page, error }, commands: fake.commands };
+ });
+ const createArchive = vi.fn(() => fake.archive);
+ const completed = vi.fn(async () => {});
+ render(() => (
+ <Suspense fallback={<p>Settings suspended</p>}>
+ <ImportProvider
+ context={{
+ createSource,
+ createArchive,
+ protectFile: fake.protectFile,
+ newToken: fake.newToken,
+ }}
+ >
+ <ImportDialog
+ teamId="team"
+ onCompleted={completed}
+ channelHref={(id) =>
+ id === 'accessible' ? `/app/channel/${id}` : undefined
+ }
+ />
+ </ImportProvider>
+ </Suspense>
+ ));
+ const trigger = screen.getByRole('button', { name: 'Import from Slack' });
+ trigger.focus();
+ fireEvent.click(trigger);
+ async function choose(): Promise<void> {
+ await waitFor(() =>
+ expect(document.activeElement).toBe(
+ screen.getByLabelText('Slack export ZIP')
+ )
+ );
+ fireEvent.change(screen.getByLabelText('Slack export ZIP'), {
+ target: { files: [new File(['zip'], 'slack.zip')] },
+ });
+ await screen.findByLabelText('Filter conversations');
+ await waitFor(() =>
+ expect(
+ (
+ screen.getByRole('button', {
+ name: 'Choose another archive',
+ }) as HTMLButtonElement
+ ).disabled
+ ).toBe(false)
+ );
+ }
+ return {
+ fake,
+ choose,
+ page,
+ setPage,
+ setError,
+ createSource,
+ createArchive,
+ completed,
+ trigger,
+ inputs: () => inputs,
+ };
+}
+
+describe('Slack import dialog', () => {
+ it('discovers locally, filters/groups selections and requires source confirmation before upload', async () => {
+ const view = setup();
+ const discovery = archiveDiscovery();
+ discovery.conversations.push(
+ {
+ ...discovery.conversations[0],
+ slackChannelId: 'COLD',
+ name: 'old',
+ archived: true,
+ },
+ {
+ ...discovery.conversations[0],
+ slackChannelId: 'D123',
+ name: 'unsupported DM',
+ kind: 'direct_message',
+ }
+ );
+ view.fake.archive.discover.mockResolvedValue(discovery);
+ expect(view.createArchive).not.toHaveBeenCalled();
+ await view.choose();
+ expect(view.fake.commands.create).not.toHaveBeenCalled();
+ expect(screen.queryByLabelText(/old.*source members/)).toBeNull();
+ expect(
+ (screen.getByLabelText(/unsupported DM/) as HTMLInputElement).disabled
+ ).toBe(true);
+ fireEvent.click(screen.getByLabelText('Select all visible conversations'));
+ expect(screen.getByText(/1 selected/)).toBeTruthy();
+ fireEvent.click(screen.getByLabelText('Show archived conversations'));
+ fireEvent.input(screen.getByLabelText('Filter conversations'), {
+ target: { value: 'old' },
+ });
+ fireEvent.click(screen.getByLabelText('Select all visible conversations'));
+ expect(screen.getByText(/2 selected/)).toBeTruthy();
+ fireEvent.input(screen.getByLabelText('Filter conversations'), {
+ target: { value: '' },
+ });
+ fireEvent.click(screen.getByLabelText(/old.*source members/));
+ expect(
+ (
+ screen.getByRole('button', {
+ name: 'Import selected channels (1)',
+ }) as HTMLButtonElement
+ ).disabled
+ ).toBe(true);
+ fireEvent.click(screen.getByLabelText(/I confirm this archive/));
+ fireEvent.click(screen.getByLabelText('Include message history'));
+ fireEvent.click(
+ screen.getByRole('button', { name: 'Import selected channels (1)' })
+ );
+ await waitFor(() =>
+ expect(view.fake.commands.finalize).toHaveBeenCalledOnce()
+ );
+ expect(view.fake.commands.create.mock.calls[0][1]).toMatchObject({
+ includeMessageHistory: false,
+ source: { kind: 'confirmed_unknown' },
+ conversations: [{ slackChannelId: 'C123' }],
+ });
+ expect(
+ view.fake.archive.prepare.mock.calls[0][0].includeMessageHistory
+ ).toBe(false);
+ expect(screen.getByLabelText(/Upload:/)).toBeTruthy();
+ });
+
+ it('discards unconfirmed selection on close, makes no writes and restores focus on reopen', async () => {
+ const view = setup();
+ await view.choose();
+ fireEvent.click(screen.getByLabelText('Select all visible conversations'));
+ fireEvent.click(screen.getByRole('button', { name: 'Close' }));
+ await waitFor(() => expect(document.activeElement).toBe(view.trigger));
+ fireEvent.click(view.trigger);
+ await waitFor(() =>
+ expect(document.activeElement).toBe(
+ screen.getByLabelText('Slack export ZIP')
+ )
+ );
+ expect(screen.queryByLabelText('Filter conversations')).toBeNull();
+ expect(view.fake.archive.discover).toHaveBeenCalledOnce();
+ expect(view.fake.archive.dispose).toHaveBeenCalledOnce();
+ expect(view.fake.commands.create).not.toHaveBeenCalled();
+ expect(view.fake.commands.register).not.toHaveBeenCalled();
+ expect(view.fake.commands.cancel).not.toHaveBeenCalled();
+ expect(view.createSource).toHaveBeenCalledOnce();
+ });
+
+ it('confirms A/C by ID across filters, freezes review during creation and reopens persisted progress', async () => {
+ const view = setup();
+ const found = archiveDiscovery();
+ found.conversations = ['CA', 'CB', 'CC'].map((slackChannelId) => ({
+ ...found.conversations[0],
+ slackChannelId,
+ name: 'same name',
+ folder: slackChannelId,
+ }));
+ view.fake.archive.discover.mockResolvedValue(found);
+ view.fake.archive.prepare.mockImplementation(async (options) => {
+ for (const slackChannelId of options.selectedIds)
+ await options.seal({
+ slackChannelId,
+ partCount: 0,
+ manifestSha256: 'b'.repeat(64),
+ });
+ });
+ const created = deferred<ReturnType<typeof importReceipt>>();
+ view.fake.commands.create.mockReturnValue(created.promise);
+ await view.choose();
+ fireEvent.click(screen.getByLabelText(/I confirm this archive/));
+ expect(
+ (
+ screen.getByRole('button', {
+ name: 'Import selected channels (0)',
+ }) as HTMLButtonElement
+ ).disabled
+ ).toBe(true);
+ for (const id of ['CA', 'CC']) {
+ fireEvent.input(screen.getByLabelText('Filter conversations'), {
+ target: { value: id },
+ });
+ fireEvent.click(
+ screen.getByLabelText('Select all visible conversations')
+ );
+ }
+ fireEvent.input(screen.getByLabelText('Filter conversations'), {
+ target: { value: 'CB' },
+ });
+ expect(screen.getByText(/2 selected/)).toBeTruthy();
+ expect(
+ (screen.getByLabelText(/same name.*CB/) as HTMLInputElement).checked
+ ).toBe(false);
+ fireEvent.input(screen.getByLabelText('Filter conversations'), {
+ target: { value: '' },
+ });
+ const confirm = screen.getByRole('button', {
+ name: 'Import selected channels (2)',
+ });
+ fireEvent.click(confirm);
+ fireEvent.click(confirm);
+ expect(view.fake.commands.create).toHaveBeenCalledOnce();
+ expect(view.fake.commands.create.mock.calls[0][1].conversations).toEqual([
+ found.conversations[0],
+ found.conversations[2],
+ ]);
+ expect(screen.queryByLabelText('Filter conversations')).toBeNull();
+ fireEvent.click(screen.getByRole('button', { name: 'Close' }));
+ created.resolve(
+ importReceipt({
+ conversations: ['CA', 'CC'].map((slackChannelId) => ({
+ ...importReceipt().conversations[0],
+ slackChannelId,
+ name: `Saved ${slackChannelId}`,
+ status: 'failed',
+ error: 'invalid_input',
+ })),
+ })
+ );
+ await waitFor(() =>
+ expect(view.fake.commands.finalize).toHaveBeenCalledOnce()
+ );
+ // The fake completion methods return their own receipt; publish the server's final snapshot.
+ view.fake.setObserved(
+ importReceipt({
+ revision: 100,
+ status: 'failed',
+ conversations: ['CA', 'CC'].map((slackChannelId) => ({
+ ...importReceipt().conversations[0],
+ slackChannelId,
+ name: `Saved ${slackChannelId}`,
+ status: 'failed',
+ error: 'invalid_input',
+ })),
+ })
+ );
+ fireEvent.click(view.trigger);
+ await screen.findByText('Saved CA');
+ expect(screen.getByText('Saved CC')).toBeTruthy();
+ expect(screen.queryByLabelText('Filter conversations')).toBeNull();
+ expect(view.fake.archive.discover).toHaveBeenCalledOnce();
+ });
+
+ it('cancels an unconfirmed review without creating or cancelling a server job', async () => {
+ const view = setup();
+ await view.choose();
+ fireEvent.click(
+ screen.getByRole('button', {
+ name: 'Cancel import (keep partial results)',
+ })
+ );
+ await waitFor(() =>
+ expect(view.fake.archive.dispose).toHaveBeenCalledOnce()
+ );
+ expect(view.fake.commands.create).not.toHaveBeenCalled();
+ expect(view.fake.commands.cancel).not.toHaveBeenCalled();
+ expect(view.fake.commands.register).not.toHaveBeenCalled();
+ });
+
+ it('keeps the dialog mounted through polling/errors and observes each terminal job once', async () => {
+ const view = setup();
+ const dialog = screen.getByRole('dialog');
+ const processing = importReceipt({ status: 'processing' });
+ view.setPage({ ...view.page(), jobs: [processing] });
+ fireEvent.click(screen.getByRole('button', { name: 'job · processing' }));
+ const result = importReceipt({
+ status: 'cancelled',
+ revision: 8,
+ conversations: [
+ {
+ ...processing.conversations[0],
+ channelId: 'accessible',
+ status: 'completed',
+ counters: {
+ processed: 12,
+ imported: 9,
+ skipped: 1,
+ duplicates: 2,
+ reactions: 3,
+ },
+ },
+ {
+ ...processing.conversations[0],
+ slackChannelId: 'COTHER',
+ channelId: 'inaccessible',
+ status: 'skipped',
+ warnings: ['target_unavailable'],
+ },
+ ],
+ });
+ view.fake.setObserved(result);
+ view.setPage({ ...view.page(), jobs: [result] });
+ await waitFor(() => expect(view.completed).toHaveBeenCalledOnce());
+ const channelLink = screen.getByRole('link');
+ channelLink.focus();
+ view.fake.setObserved({
+ ...result,
+ revision: 9,
+ conversations: structuredClone(result.conversations),
+ });
+ expect(document.activeElement).toBe(channelLink);
+ view.setError(new Error('offline'));
+ expect(screen.getByRole('dialog')).toBe(dialog);
+ expect(screen.queryByText('Settings suspended')).toBeNull();
+ expect(view.completed).toHaveBeenCalledOnce();
+ expect(screen.getByText(/9 imported/)).toBeTruthy();
+ expect(screen.getByText('target unavailable')).toBeTruthy();
+ expect(screen.getAllByRole('link')).toHaveLength(1);
+ expect(screen.getByRole('link').getAttribute('href')).toBe(
+ '/app/channel/accessible'
+ );
+ expect(screen.getByText(/Cancellation is not rollback/)).toBeTruthy();
+ });
+
+ it('can cancel while uploading and keeps committed partial counts visible', async () => {
+ const view = setup();
+ const upload = deferred<'uploaded'>();
+ view.fake.put.mockImplementation(async (_, options) => {
+ options?.onProgress?.({ kind: 'bytes', loaded: 1, total: 2 });
+ options?.signal?.addEventListener(
+ 'abort',
+ () => upload.reject(new Error('aborted')),
+ { once: true }
+ );
+ return upload.promise;
+ });
+ await view.choose();
+ fireEvent.click(screen.getByLabelText('Select all visible conversations'));
+ fireEvent.click(screen.getByLabelText(/I confirm this archive/));
+ fireEvent.click(
+ screen.getByRole('button', { name: 'Import selected channels (1)' })
+ );
+ await waitFor(() => expect(view.fake.put).toHaveBeenCalled());
+ expect(screen.getByLabelText(/Upload:/).getAttribute('value')).toBe('2');
+ fireEvent.click(
+ screen.getByRole('button', {
+ name: 'Cancel import (keep partial results)',
+ })
+ );
+ await waitFor(() =>
+ expect(view.fake.commands.cancel).toHaveBeenCalledOnce()
+ );
+ expect(view.fake.commands.finalize).not.toHaveBeenCalled();
+ });
+
+ it('finalizes a historical upload with skips without creating an archive worker', async () => {
+ const view = setup();
+ view.setPage({ ...view.page(), jobs: [importReceipt()] });
+ fireEvent.click(screen.getByRole('button', { name: 'job · uploading' }));
+ fireEvent.click(
+ screen.getByRole('button', { name: 'Finalize with skips' })
+ );
+ await waitFor(() =>
+ expect(view.fake.commands.finalize).toHaveBeenCalledWith({
+ teamId: 'team',
+ jobId: 'job',
+ })
+ );
+ expect(view.createArchive).not.toHaveBeenCalled();
+ expect(screen.getByText('general')).toBeTruthy();
+ expect(screen.getByText(/With message history/)).toBeTruthy();
+ });
+
+ it('shows actionable errors for historical cancellation and supports history pagination', async () => {
+ const view = setup();
+ view.setPage({
+ ...view.page(),
+ jobs: [importReceipt()],
+ nextCursor: 'cursor',
+ });
+ fireEvent.click(screen.getByRole('button', { name: 'job · uploading' }));
+ view.fake.commands.cancel.mockRejectedValue(new Error('request failed'));
+ fireEvent.click(
+ screen.getByRole('button', {
+ name: 'Cancel import (keep partial results)',
+ })
+ );
+ await screen.findByText(/Cancellation was not confirmed/);
+ fireEvent.click(screen.getByRole('button', { name: 'Older imports' }));
+ expect(view.inputs().before?.()).toBe('cursor');
+ fireEvent.click(screen.getByRole('button', { name: 'Newest imports' }));
+ expect(view.inputs().before?.()).toBeUndefined();
+ });
+});
diff --git a/apps/web/src/features/slack-import/views/import-dialog.tsx b/apps/web/src/features/slack-import/views/import-dialog.tsx
new file mode 100644
index 0000000000..1fec103989
--- /dev/null
+++ b/apps/web/src/features/slack-import/views/import-dialog.tsx
@@ -0,0 +1,488 @@
+import { Button, Dialog } from '@ui';
+import {
+ createEffect,
+ createRoot,
+ createSignal,
+ getOwner,
+ Index,
+ type JSX,
+ on,
+ onCleanup,
+ Show,
+} from 'solid-js';
+import { ConversationPicker } from '../components/conversation-picker';
+import { ImportProgress } from '../components/import-progress';
+import { SlackImportCard } from '../components/slack-import-card';
+import type { ImportJob } from '../context/contracts';
+import { useImportContext } from '../context/import-context';
+import {
+ createImportController,
+ type ImportController,
+} from '../primitives/import-controller';
+
+type Props = {
+ teamId: string;
+ onCompleted(job: ImportJob): Promise<void>;
+ channelHref(id: string): string | undefined;
+};
+
+function terminal(job: ImportJob): boolean {
+ return ['completed', 'completed_with_errors', 'failed', 'cancelled'].includes(
+ job.status
+ );
+}
+
+/** Confirmed jobs outlive dialog content; closing an unconfirmed review discards its file. */
+export function ImportDialog(props: Props): JSX.Element {
+ const context = useImportContext();
+ const owner = getOwner();
+ const [open, setOpen] = createSignal(false);
+ let opener: HTMLButtonElement | undefined;
+ const [jobId, setJobId] = createSignal<string>();
+ const [before, setBefore] = createSignal<string>();
+ const { source, commands } = context.createSource({
+ teamId: () => props.teamId,
+ jobId,
+ enabled: () => true,
+ before,
+ });
+ const [controller, setController] = createSignal<ImportController>();
+ const [selected, setSelected] = createSignal<ReadonlySet<string>>(new Set());
+ const [filter, setFilter] = createSignal('');
+ const [showArchived, setShowArchived] = createSignal(false);
+ const [includeHistory, setIncludeHistory] = createSignal(true);
+ const [confirmed, setConfirmed] = createSignal(false);
+ const [actionError, setActionError] = createSignal<string>();
+ const [refreshWarning, setRefreshWarning] = createSignal<string>();
+ const [busy, setBusy] = createSignal(false);
+ const [cancelling, setCancelling] = createSignal(false);
+ const [historyReceipt, setHistoryReceipt] = createSignal<ImportJob>();
+ let disposeSession: (() => void) | undefined;
+ onCleanup(() => disposeSession?.());
+
+ const job = (): ImportJob | undefined => {
+ const current = controller()?.job();
+ if (current) return current;
+ const observed = source.job();
+ const saved = historyReceipt();
+ if (
+ saved &&
+ saved.jobId === jobId() &&
+ (!observed || saved.revision > observed.revision)
+ )
+ return saved;
+ return observed ?? source.page()?.jobs.find((job) => job.jobId === jobId());
+ };
+ const localActive = () => {
+ const phase = controller()?.phase();
+ return phase !== undefined && !['monitoring', 'terminal'].includes(phase);
+ };
+ const observedCompletions = new Set<string>();
+ async function refreshCompleted(job: ImportJob): Promise<void> {
+ try {
+ await props.onCompleted(job);
+ } catch {
+ setRefreshWarning(
+ 'Import results are saved, but channel lists could not be refreshed.'
+ );
+ }
+ }
+ // Cache invalidation is an imperative host effect, never derived feature state.
+ createEffect(
+ on(
+ () => [...(source.page()?.jobs ?? []), ...(job() ? [job()!] : [])],
+ (jobs) => {
+ for (const receipt of jobs) {
+ if (!terminal(receipt) || observedCompletions.has(receipt.jobId))
+ continue;
+ observedCompletions.add(receipt.jobId);
+ void refreshCompleted(receipt);
+ }
+ }
+ )
+ );
+
+ function resetSession(): void {
+ disposeSession?.();
+ disposeSession = undefined;
+ setController(undefined);
+ setActionError(undefined);
+ setSelected(new Set<string>());
+ setConfirmed(false);
+ setFilter('');
+ setShowArchived(false);
+ setIncludeHistory(true);
+ }
+
+ function changeOpen(next: boolean): void {
+ if (!next) {
+ const phase = controller()?.phase();
+ if (phase === 'idle' || phase === 'discovering' || phase === 'selecting')
+ resetSession();
+ }
+ setOpen(next);
+ }
+
+ async function chooseFile(file: File | undefined): Promise<void> {
+ const page = source.page();
+ if (!file || !page || localActive()) return;
+ resetSession();
+ setJobId(undefined);
+ setHistoryReceipt(undefined);
+ const session = createRoot((dispose) => {
+ disposeSession = dispose;
+ return createImportController({
+ teamId: props.teamId,
+ limits: page.limits,
+ sourceBinding: () => source.page()?.sourceBinding ?? page.sourceBinding,
+ archive: context.createArchive(),
+ commands,
+ source,
+ protectFile: context.protectFile,
+ newToken: context.newToken,
+ async onJobCreated(job) {
+ setJobId(job.jobId);
+ },
+ });
+ }, owner);
+ setController(session);
+ await session.discover(file);
+ }
+
+ async function runAction(action: () => Promise<void>): Promise<void> {
+ if (busy()) return;
+ setBusy(true);
+ setActionError(undefined);
+ try {
+ await action();
+ } catch (error) {
+ setActionError(
+ error instanceof Error
+ ? error.message
+ : 'The action could not be completed.'
+ );
+ } finally {
+ setBusy(false);
+ }
+ }
+
+ async function finalizeHistory(): Promise<void> {
+ const receipt = job();
+ if (!receipt || receipt.status !== 'uploading' || controller()) return;
+ setHistoryReceipt(
+ await commands.finalize({ teamId: props.teamId, jobId: receipt.jobId })
+ );
+ }
+
+ async function cancel(): Promise<void> {
+ if (cancelling()) return;
+ setCancelling(true);
+ setActionError(undefined);
+ try {
+ const active = controller();
+ if (active) return await active.cancel();
+ const receipt = job();
+ if (!receipt || terminal(receipt)) return;
+ setHistoryReceipt(
+ await commands.cancel({ teamId: props.teamId, jobId: receipt.jobId })
+ );
+ } catch {
+ setActionError(
+ 'Cancellation was not confirmed. Refresh progress or retry cancel.'
+ );
+ } finally {
+ setCancelling(false);
+ }
+ }
+
+ function select(ids: string[], checked: boolean): void {
+ if (controller()?.phase() !== 'selecting') return;
+ setSelected((previous) => {
+ const next = new Set(previous);
+ for (const id of ids) {
+ if (checked) next.add(id);
+ else next.delete(id);
+ }
+ return next;
+ });
+ }
+
+ const discovery = () => controller()?.discovery();
+ const canCancel = () =>
+ (controller() && controller()?.phase() !== 'terminal') ||
+ (job() && !terminal(job()!));
+ const cancelPending = () =>
+ cancelling() ||
+ controller()?.phase() === 'cancelling' ||
+ job()?.status === 'cancelling';
+ const canChooseAnother = () =>
+ controller() &&
+ (!localActive() ||
+ controller()?.phase() === 'selecting' ||
+ (controller()?.phase() === 'interrupted' && !discovery()));
+ const fileDisabled = () => !source.page() || localActive() || busy();
+ const error = () => actionError() ?? controller()?.error();
+ const warning = () => refreshWarning() ?? controller()?.warning();
+ const unsupported = () =>
+ new Set(
+ controller()
+ ?.skips()
+ .filter((skip) => skip.reason === 'unsupported_dm')
+ .map((skip) => skip.slackChannelId)
+ );
+ const namedAuthors = () =>
+ discovery()?.users.filter(
+ (user) => !user.profile?.email || user.is_bot || user.id === 'USLACKBOT'
+ ).length ?? 0;
+ const archiveSource = (): string => {
+ const source = discovery()?.source;
+ return source?.kind === 'known' ? source.sourceId : 'Unknown';
+ };
+ const boundSource = () => {
+ const binding = source.page()?.sourceBinding;
+ if (!binding || binding.kind === 'unbound') return 'Not yet bound';
+ return binding.kind === 'known'
+ ? binding.sourceId
+ : 'Previously confirmed unknown workspace';
+ };
+
+ return (
+ <>
+ <SlackImportCard
+ onOpen={(trigger) => {
+ opener = trigger;
+ setOpen(true);
+ }}
+ />
+ <Dialog
+ open={open()}
+ onOpenChange={changeOpen}
+ position="center"
+ onCloseAutoFocus={(event) => {
+ // The card is outside Dialog's trigger context; retain its focus owner.
+ event.preventDefault();
+ if (!open()) opener?.focus();
+ }}
+ >
+ <div class="max-h-[85dvh] overflow-y-auto p-4 sm:p-6 flex flex-col gap-4 text-ink">
+ <Dialog.Title class="text-lg font-semibold">
+ Import from Slack
+ </Dialog.Title>
+ <Dialog.Description class="text-sm text-ink-muted">
+ Select a Slack export ZIP. Parsing stays in your browser; only
+ selected conversation data is uploaded. Keep this Settings page open
+ until uploads finish.
+ </Dialog.Description>
+ <label class="flex flex-col gap-1 text-sm">
+ Slack export ZIP
+ {/* The mobile focus trap requires tabindex=-1 even on disabled inputs. */}
+ <input
+ type="file"
+ accept=".zip,application/zip"
+ disabled={fileDisabled()}
+ tabIndex={fileDisabled() ? -1 : undefined}
+ onChange={(event) => {
+ const file = event.currentTarget.files?.[0];
+ event.currentTarget.value = '';
+ void runAction(() => chooseFile(file));
+ }}
+ />
+ </label>
+ <p class="text-xs text-ink-muted">
+ Files, attachments and attachment-only messages are not imported.
+ Encrypted, multi-volume and ZIP64 archives are unsupported. This is
+ not a live Slack connection.
+ </p>
+ <Show when={!source.page() && !source.error()}>
+ <p role="status">Loading import settings…</p>
+ </Show>
+ <Show when={source.error()}>
+ <p role="alert">
+ Progress could not be refreshed. Existing server imports may still
+ be running.
+ </p>
+ </Show>
+ <Show when={error()}>{(error) => <p role="alert">{error()}</p>}</Show>
+ <Show when={warning()}>
+ {(warning) => <p role="status">{warning()}</p>}
+ </Show>
+ <Show when={controller()?.phase() === 'selecting' && discovery()}>
+ {(found) => (
+ <>
+ <ConversationPicker
+ discovery={found()}
+ selected={selected()}
+ unsupported={unsupported()}
+ filter={filter()}
+ showArchived={showArchived()}
+ onFilter={setFilter}
+ onShowArchived={setShowArchived}
+ onSelect={select}
+ />
+ <label class="flex items-center gap-2 text-sm">
+ <input
+ type="checkbox"
+ checked={includeHistory()}
+ onChange={(event) =>
+ setIncludeHistory(event.currentTarget.checked)
+ }
+ />
+ Include message history
+ </label>
+ <p class="text-sm text-ink-muted">
+ {found().users.length - namedAuthors()} source users have
+ email attribution; {namedAuthors()} use Slack-name attribution
+ via the system bot. Emails are lowercased as exported, without
+ alias matching or a team roster lookup. External email
+ addresses can become channel members. DMs require two distinct
+ email-bearing members; importing never adds you as a third
+ member.
+ </p>
+ <p class="text-sm text-ink-muted">
+ Public Slack channels become Team channels with explicit
+ members, never globally Public. Existing channel names, roles
+ and membership history are preserved.
+ </p>
+ <p class="text-sm">
+ Team source: {boundSource()}. Archive source:{' '}
+ {archiveSource()}.
+ </p>
+ <label class="flex items-start gap-2 text-sm">
+ <input
+ type="checkbox"
+ class="mt-1"
+ checked={confirmed()}
+ onChange={(event) =>
+ setConfirmed(event.currentTarget.checked)
+ }
+ />
+ I confirm this archive belongs to this team's Slack workspace,
+ including when its identity is unknown. The source binding is
+ immutable; this team cannot import another Slack workspace.
+ </label>
+ <Button
+ variant="strong"
+ disabled={!selected().size || !confirmed() || busy()}
+ onClick={() =>
+ void runAction(() =>
+ controller()!.start({
+ selectedIds: [...selected()],
+ includeMessageHistory: includeHistory(),
+ sourceConfirmed: confirmed(),
+ })
+ )
+ }
+ >
+ Import selected channels ({selected().size})
+ </Button>
+ </>
+ )}
+ </Show>
+ <Show when={controller() || job()}>
+ <ImportProgress
+ job={job()}
+ phase={controller()?.phase()}
+ upload={controller()?.uploadProgress()}
+ channelHref={props.channelHref}
+ />
+ </Show>
+ <p class="text-xs text-ink-muted">
+ Cancellation stops future work, not work already committed. Running
+ conversations may finish. Imported channels and messages are not
+ rolled back.
+ </p>
+ <Show when={!controller() && job()?.status === 'uploading'}>
+ <p class="text-sm text-ink-muted">
+ Server progress has been recovered without the ZIP. Local uploads
+ do not resume after reload. Finalize with skips to process
+ verified conversations, or cancel and confirm a new import.
+ </p>
+ <Button
+ disabled={busy() || cancelling()}
+ onClick={() => void runAction(finalizeHistory)}
+ >
+ Finalize with skips
+ </Button>
+ </Show>
+ <div class="flex flex-wrap gap-2">
+ <Show when={controller()?.phase() === 'interrupted' && discovery()}>
+ <Show
+ when={controller()?.job()}
+ fallback={
+ <Button
+ disabled={busy()}
+ onClick={() =>
+ void runAction(() => controller()!.recoverJob())
+ }
+ >
+ Recover job receipt
+ </Button>
+ }
+ >
+ <Button
+ disabled={busy()}
+ onClick={() =>
+ void runAction(() => controller()!.finalizeWithSkips())
+ }
+ >
+ Finalize with skips
+ </Button>
+ </Show>
+ </Show>
+ <Show when={canCancel()}>
+ <Button disabled={cancelPending()} onClick={() => void cancel()}>
+ Cancel import (keep partial results)
+ </Button>
+ </Show>
+ <Show when={canChooseAnother()}>
+ <Button disabled={busy() || cancelling()} onClick={resetSession}>
+ Choose another archive
+ </Button>
+ </Show>
+ <Button
+ disabled={busy()}
+ onClick={() => void runAction(() => source.refresh())}
+ >
+ Refresh progress
+ </Button>
+ <Button onClick={() => changeOpen(false)}>Close</Button>
+ </div>
+ <section class="flex flex-col gap-2" aria-label="Job history">
+ <h3 class="font-medium">Job history</h3>
+ <Index each={source.page()?.jobs}>
+ {(receipt) => (
+ <Button
+ class="h-auto break-all text-left justify-start"
+ disabled={localActive() || busy()}
+ onClick={() => {
+ resetSession();
+ setHistoryReceipt(undefined);
+ setJobId(receipt().jobId);
+ }}
+ >
+ {receipt().createdAt || receipt().jobId} ·{' '}
+ {receipt().status.replaceAll('_', ' ')}
+ </Button>
+ )}
+ </Index>
+ <Show when={source.page()?.jobs.length === 0}>
+ <p class="text-sm text-ink-muted">No imports on this page.</p>
+ </Show>
+ <div class="flex flex-wrap gap-2">
+ <Show when={before()}>
+ <Button onClick={() => setBefore(undefined)}>
+ Newest imports
+ </Button>
+ </Show>
+ <Show when={source.page()?.nextCursor}>
+ <Button onClick={() => setBefore(source.page()?.nextCursor)}>
+ Older imports
+ </Button>
+ </Show>
+ </div>
+ </section>
+ </div>
+ </Dialog>
+ </>
+ );
+}
diff --git a/apps/web/src/lib/core/constant/featureFlags.ts b/apps/web/src/lib/core/constant/featureFlags.ts
index fe345e2bf0..953c3ca044 100644
--- a/apps/web/src/lib/core/constant/featureFlags.ts
+++ b/apps/web/src/lib/core/constant/featureFlags.ts
@@ -114,6 +114,13 @@ export const PROD_MODE_ENV = import.meta.env.MODE === 'production';
const onInDev = DEV_MODE_ENV || undefined;
+// Leave the production PostHog flag disabled until archive-import rollout.
+export const enableSlackArchiveImport = defineFlag({
+ key: 'enable-slack-archive-import',
+ env: 'ENABLE_SLACK_ARCHIVE_IMPORT',
+ default: onInDev,
+});
+
/** Shows the Reviews shortcut in Tasks; copied Reviews links remain accessible. */
export const enableTasksReviews = defineFlag({
key: 'enable-tasks-reviews',
diff --git a/apps/web/src/lib/queries/slack-import/index.test.ts b/apps/web/src/lib/queries/slack-import/index.test.ts
new file mode 100644
index 0000000000..557b2eed2e
--- /dev/null
+++ b/apps/web/src/lib/queries/slack-import/index.test.ts
@@ -0,0 +1,74 @@
+import { QueryClient } from '@tanstack/solid-query';
+import { describe, expect, it } from 'vitest';
+import { invalidateSlackImport, parseSlackImportUpdate } from './index';
+import { slackImportKeys } from './keys';
+
+const teamId = '01900000-0000-7000-8000-000000000001';
+const jobId = '01900000-0000-7000-8000-000000000002';
+const otherTeam = '01900000-0000-7000-8000-000000000003';
+const otherJob = '01900000-0000-7000-8000-000000000004';
+const event = { teamId, jobId, revision: 1, status: 'processing' };
+
+describe('Slack import cache identity and invalidation', () => {
+ it('keys jobs and every receipt page by team', () => {
+ expect(slackImportKeys.job(teamId, jobId).queryKey).not.toEqual(
+ slackImportKeys.job(otherTeam, jobId).queryKey
+ );
+ expect(slackImportKeys.list(teamId).queryKey).not.toEqual(
+ slackImportKeys.list(otherTeam).queryKey
+ );
+ });
+
+ it('invalidates only the matching job and that team’s list pages', async () => {
+ const cache = new QueryClient();
+ const matching = [
+ slackImportKeys.list(teamId).queryKey,
+ slackImportKeys.list(teamId, jobId).queryKey,
+ slackImportKeys.job(teamId, jobId).queryKey,
+ ];
+ const unrelated = [
+ slackImportKeys.list(otherTeam).queryKey,
+ slackImportKeys.job(otherTeam, jobId).queryKey,
+ slackImportKeys.job(teamId, otherJob).queryKey,
+ ['channel', 'listChannels'],
+ ];
+ for (const key of [...matching, ...unrelated]) cache.setQueryData(key, {});
+ await invalidateSlackImport(cache, { teamId, jobId });
+ for (const key of matching)
+ expect(cache.getQueryState(key)?.isInvalidated).toBe(true);
+ for (const key of unrelated)
+ expect(cache.getQueryState(key)?.isInvalidated).toBe(false);
+ cache.clear();
+ });
+});
+
+describe('slack_import_updated payloads', () => {
+ it('accepts object and JSON-string envelopes without trusting extra fields', () => {
+ expect(parseSlackImportUpdate(event)).toEqual(event);
+ expect(parseSlackImportUpdate(JSON.stringify(event))).toEqual(event);
+ expect(
+ parseSlackImportUpdate({ ...event, key: 'not-a-cache-key' })
+ ).toEqual(event);
+ });
+
+ it.each([
+ undefined,
+ null,
+ [],
+ 1,
+ 'not json',
+ '{}',
+ JSON.stringify(JSON.stringify(event)),
+ { ...event, teamId: '' },
+ { ...event, jobId: '../job' },
+ { ...event, revision: -1 },
+ { ...event, revision: 0.5 },
+ { ...event, revision: Number.MAX_SAFE_INTEGER + 1 },
+ { ...event, revision: '1' },
+ { ...event, revision: undefined },
+ { ...event, status: 'unknown' },
+ { ...event, status: undefined },
+ ])('ignores malformed payload %j', (payload) => {
+ expect(parseSlackImportUpdate(payload)).toBeUndefined();
+ });
+});
diff --git a/apps/web/src/lib/queries/slack-import/index.ts b/apps/web/src/lib/queries/slack-import/index.ts
new file mode 100644
index 0000000000..df5e03728f
--- /dev/null
+++ b/apps/web/src/lib/queries/slack-import/index.ts
@@ -0,0 +1,210 @@
+import { throwOnErr } from '@core/util/result';
+import type { storageServiceClient } from '@service-storage/client';
+import type { ImportPage } from '@service-storage/generated/schemas/importPage';
+import type { ImportProgress } from '@service-storage/generated/schemas/importProgress';
+import { JobStatus } from '@service-storage/generated/schemas/jobStatus';
+import type { SlackCompleteRequest } from '@service-storage/generated/schemas/slackCompleteRequest';
+import type { SlackCreateRequest } from '@service-storage/generated/schemas/slackCreateRequest';
+import type { SlackRegisterRequest } from '@service-storage/generated/schemas/slackRegisterRequest';
+import type { UploadGrant } from '@service-storage/generated/schemas/uploadGrant';
+import {
+ type QueryClient,
+ queryOptions,
+ type UndefinedInitialDataOptions,
+ type UseMutationOptions,
+} from '@tanstack/solid-query';
+import { z } from 'zod';
+import { slackImportKeys } from './keys';
+
+export type SlackImportClient = Pick<
+ typeof storageServiceClient,
+ | 'listSlackImports'
+ | 'getSlackImport'
+ | 'createSlackImport'
+ | 'registerSlackImportUploads'
+ | 'completeSlackImportUploads'
+ | 'finalizeSlackImport'
+ | 'cancelSlackImport'
+>;
+
+export type ImportScope = { teamId: string; jobId: string };
+type ScopedData<T> = { teamId: string | null; value: T | null };
+const EMPTY_LIST: ScopedData<ImportPage> = { teamId: null, value: null };
+const EMPTY_JOB: ScopedData<ImportProgress> = { teamId: null, value: null };
+const MAX_UPLOAD_BATCH = 50;
+
+export function importIsActive(job: ImportProgress): boolean {
+ return (
+ job.status === 'uploading' ||
+ job.status === 'processing' ||
+ job.status === 'cancelling' ||
+ job.conversations.some(
+ (conversation) =>
+ conversation.search.status === 'pending' ||
+ conversation.search.status === 'submitted'
+ )
+ );
+}
+
+export function slackImportListOptions(
+ client: SlackImportClient,
+ teamId: string,
+ enabled: boolean,
+ before?: string
+): ReturnType<UndefinedInitialDataOptions<ScopedData<ImportPage>>> {
+ return queryOptions<ScopedData<ImportPage>>({
+ queryKey: slackImportKeys.list(teamId, before).queryKey,
+ enabled: enabled && Boolean(teamId),
+ queryFn: async ({ signal }): Promise<ScopedData<ImportPage>> => ({
+ teamId,
+ value: await throwOnErr(() =>
+ client.listSlackImports({ before, signal })
+ ),
+ }),
+ placeholderData: EMPTY_LIST,
+ staleTime: 0,
+ refetchInterval: (query) =>
+ query.state.data?.value?.jobs.some(importIsActive) ? 3_000 : 15_000,
+ });
+}
+
+export function slackImportJobOptions(
+ client: SlackImportClient,
+ scope: ImportScope,
+ enabled: boolean
+): ReturnType<UndefinedInitialDataOptions<ScopedData<ImportProgress>>> {
+ return queryOptions<ScopedData<ImportProgress>>({
+ queryKey: slackImportKeys.job(scope.teamId, scope.jobId).queryKey,
+ enabled: enabled && Boolean(scope.teamId && scope.jobId),
+ queryFn: async ({ signal }): Promise<ScopedData<ImportProgress>> => ({
+ teamId: scope.teamId,
+ value: await throwOnErr(() =>
+ client.getSlackImport({ jobId: scope.jobId, signal })
+ ),
+ }),
+ placeholderData: EMPTY_JOB,
+ staleTime: 0,
+ refetchInterval: (query) =>
+ query.state.data?.value && importIsActive(query.state.data.value)
+ ? 3_000
+ : 15_000,
+ });
+}
+
+const updateSchema = z.object({
+ teamId: z.string().uuid(),
+ jobId: z.string().uuid(),
+ revision: z.number().int().nonnegative().max(Number.MAX_SAFE_INTEGER),
+ status: z.nativeEnum(JobStatus),
+});
+
+export function parseSlackImportUpdate(
+ payload: unknown
+): z.infer<typeof updateSchema> | undefined {
+ if (typeof payload === 'string') {
+ try {
+ payload = JSON.parse(payload);
+ } catch {
+ return undefined;
+ }
+ }
+ const parsed = updateSchema.safeParse(payload);
+ return parsed.success ? parsed.data : undefined;
+}
+
+/** List pages for this team plus exactly this receipt, never another team's cache. */
+export async function invalidateSlackImport(
+ cache: QueryClient,
+ scope: ImportScope
+): Promise<void> {
+ await Promise.all([
+ cache.invalidateQueries({
+ queryKey: slackImportKeys.list._def,
+ predicate: (query) => query.queryKey[2] === scope.teamId,
+ }),
+ cache.invalidateQueries({
+ queryKey: slackImportKeys.job(scope.teamId, scope.jobId).queryKey,
+ exact: true,
+ }),
+ ]);
+}
+
+type MutationOptions<Data, Variables> = ReturnType<
+ UseMutationOptions<Data, Error, Variables>
+>;
+type ImportMutationOptions = {
+ create: MutationOptions<
+ ImportProgress,
+ { teamId: string; body: SlackCreateRequest }
+ >;
+ register: MutationOptions<
+ UploadGrant[],
+ ImportScope & { body: SlackRegisterRequest }
+ >;
+ complete: MutationOptions<
+ ImportProgress,
+ ImportScope & { body: SlackCompleteRequest }
+ >;
+ finalize: MutationOptions<ImportProgress, ImportScope>;
+ cancel: MutationOptions<ImportProgress, ImportScope>;
+};
+
+/** Scope is carried in mutation variables, not read later from a changing team signal. */
+export function slackImportMutationOptions(
+ client: SlackImportClient,
+ cache: QueryClient
+): ImportMutationOptions {
+ function updated(_: unknown, scope: ImportScope): void {
+ // Cache work must not turn a successful write into a failed command.
+ void invalidateSlackImport(cache, scope);
+ }
+ return {
+ create: {
+ mutationFn: async (args: { teamId: string; body: SlackCreateRequest }) =>
+ throwOnErr(() => client.createSlackImport({ body: args.body })),
+ onSuccess: (job: ImportProgress, args: { teamId: string }) =>
+ updated(job, { teamId: args.teamId, jobId: job.jobId }),
+ },
+ register: {
+ mutationFn: async (
+ args: ImportScope & { body: SlackRegisterRequest }
+ ) => {
+ if (args.body.descriptors.length > MAX_UPLOAD_BATCH) {
+ throw new Error('Slack import registration exceeds the batch limit');
+ }
+ return throwOnErr(() =>
+ client.registerSlackImportUploads({
+ jobId: args.jobId,
+ body: args.body,
+ })
+ );
+ },
+ },
+ complete: {
+ mutationFn: async (
+ args: ImportScope & { body: SlackCompleteRequest }
+ ) => {
+ if (args.body.uploads.length > MAX_UPLOAD_BATCH) {
+ throw new Error('Slack import completion exceeds the batch limit');
+ }
+ return throwOnErr(() =>
+ client.completeSlackImportUploads({
+ jobId: args.jobId,
+ body: args.body,
+ })
+ );
+ },
+ onSuccess: updated,
+ },
+ finalize: {
+ mutationFn: async (args: ImportScope) =>
+ throwOnErr(() => client.finalizeSlackImport({ jobId: args.jobId })),
+ onSuccess: updated,
+ },
+ cancel: {
+ mutationFn: async (args: ImportScope) =>
+ throwOnErr(() => client.cancelSlackImport({ jobId: args.jobId })),
+ onSuccess: updated,
+ },
+ };
+}
diff --git a/apps/web/src/lib/queries/slack-import/keys.ts b/apps/web/src/lib/queries/slack-import/keys.ts
new file mode 100644
index 0000000000..f9fdb07b7a
--- /dev/null
+++ b/apps/web/src/lib/queries/slack-import/keys.ts
@@ -0,0 +1,6 @@
+import { createQueryKeys } from '@lukemorales/query-key-factory';
+
+export const slackImportKeys = createQueryKeys('slack-import', {
+ list: (teamId: string, before?: string) => [teamId, before ?? ''],
+ job: (teamId: string, jobId: string) => [teamId, jobId],
+});
diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/initiator.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/initiator.ts
index adb00ec021..09577e3a99 100644
--- a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/initiator.ts
+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/initiator.ts
@@ -15,4 +15,5 @@ export type Initiator = (typeof Initiator)[keyof typeof Initiator];
export const Initiator = {
onboarding: 'onboarding',
chat: 'chat',
+ archive: 'archive',
} as const;
diff --git a/apps/web/src/lib/service-clients/service-cognition/openapi.json b/apps/web/src/lib/service-clients/service-cognition/openapi.json
index a97e82efa5..734087bc99 100644
--- a/apps/web/src/lib/service-clients/service-cognition/openapi.json
+++ b/apps/web/src/lib/service-clients/service-cognition/openapi.json
@@ -3811,7 +3811,7 @@
"Initiator": {
"type": "string",
"description": "Where an import entity was first staged from. Provenance only — never a\nvisibility filter.",
- "enum": ["onboarding", "chat"]
+ "enum": ["onboarding", "chat", "archive"]
},
"JwtPayload": {
"type": "object",
diff --git a/apps/web/src/lib/service-clients/service-storage/client.ts b/apps/web/src/lib/service-clients/service-storage/client.ts
index c5e3a74449..4cec813dde 100644
--- a/apps/web/src/lib/service-clients/service-storage/client.ts
+++ b/apps/web/src/lib/service-clients/service-storage/client.ts
@@ -118,11 +118,15 @@ import type { GithubPullRequestsResponse } from './generated/schemas/githubPullR
import type { GroupedSoupGroupPage } from './generated/schemas/groupedSoupGroupPage';
import type { GroupedSoupInitialPage } from './generated/schemas/groupedSoupInitialPage';
import type { GroupedSoupSort } from './generated/schemas/groupedSoupSort';
+import type { ImportPage } from './generated/schemas/importPage';
+import type { ImportProgress } from './generated/schemas/importProgress';
import type { Item } from './generated/schemas/item';
+import type { JobId } from './generated/schemas/jobId';
import type { ListFavoritesParams } from './generated/schemas/listFavoritesParams';
import type { ListOccurrencesParams } from './generated/schemas/listOccurrencesParams';
import type { ListReminderCollectionParams } from './generated/schemas/listReminderCollectionParams';
import type { ListRemindersParams } from './generated/schemas/listRemindersParams';
+import type { ListSlackImportsParams } from './generated/schemas/listSlackImportsParams';
import type { ListTeamOutOfOfficeParams } from './generated/schemas/listTeamOutOfOfficeParams';
import type { LocationResponseV3 } from './generated/schemas/locationResponseV3';
import type { PairingDetails } from './generated/schemas/pairingDetails';
@@ -151,6 +155,9 @@ import type { SetChannelPictureRequest } from './generated/schemas/setChannelPic
import type { SetCompanyNameRequest } from './generated/schemas/setCompanyNameRequest';
import type { SetContactNameRequest } from './generated/schemas/setContactNameRequest';
import type { SharePermissionV2 } from './generated/schemas/sharePermissionV2';
+import type { SlackCompleteRequest } from './generated/schemas/slackCompleteRequest';
+import type { SlackCreateRequest } from './generated/schemas/slackCreateRequest';
+import type { SlackRegisterRequest } from './generated/schemas/slackRegisterRequest';
import type { SmartTagPreview } from './generated/schemas/smartTagPreview';
import type { SoupPage } from './generated/schemas/soupPage';
import type { SyncServiceVersionID } from './generated/schemas/syncServiceVersionID';
@@ -160,6 +167,7 @@ import type { UpdateAgentRequest } from './generated/schemas/updateAgentRequest'
import type { UpdateCrmTeamSettingsRequest } from './generated/schemas/updateCrmTeamSettingsRequest';
import type { UpdateReminderRequest } from './generated/schemas/updateReminderRequest';
import type { UploadExtractFolderHandler200 } from './generated/schemas/uploadExtractFolderHandler200';
+import type { UploadGrant } from './generated/schemas/uploadGrant';
import type { UserApiKeysList } from './generated/schemas/userApiKeysList';
import type { UserPinsResponse } from './generated/schemas/userPinsResponse';
import type { UserViewsResponse } from './generated/schemas/userViewsResponse';
@@ -384,7 +392,88 @@ const { showPaywall } = usePaywallState();
/** Machine-readable code the backend returns when a document name is too long. */
export const DOCUMENT_NAME_TOO_LONG_CODE = 'DOCUMENT_NAME_TOO_LONG' as const;
+type SlackImportJobArgs = { jobId: JobId; signal?: AbortSignal };
+
export const storageServiceClient = {
+ async createSlackImport(args: {
+ body: SlackCreateRequest;
+ signal?: AbortSignal;
+ }) {
+ return dssFetch<ImportProgress>('/slack/imports', {
+ method: 'POST',
+ body: JSON.stringify(args.body),
+ signal: args.signal,
+ });
+ },
+
+ async listSlackImports(
+ args: ListSlackImportsParams & { signal?: AbortSignal } = {}
+ ) {
+ const params = new URLSearchParams();
+ if (args.before) params.set('before', args.before);
+ const query = params.toString();
+ return dssFetch<ImportPage>(`/slack/imports${query ? `?${query}` : ''}`, {
+ method: 'GET',
+ signal: args.signal,
+ });
+ },
+
+ async getSlackImport(args: SlackImportJobArgs) {
+ return dssFetch<ImportProgress>(
+ `/slack/imports/${encodeURIComponent(args.jobId)}`,
+ {
+ method: 'GET',
+ signal: args.signal,
+ }
+ );
+ },
+
+ async registerSlackImportUploads(
+ args: SlackImportJobArgs & { body: SlackRegisterRequest }
+ ) {
+ return dssFetch<UploadGrant[]>(
+ `/slack/imports/${encodeURIComponent(args.jobId)}/uploads`,
+ {
+ method: 'POST',
+ body: JSON.stringify(args.body),
+ signal: args.signal,
+ }
+ );
+ },
+
+ async completeSlackImportUploads(
+ args: SlackImportJobArgs & { body: SlackCompleteRequest }
+ ) {
+ return dssFetch<ImportProgress>(
+ `/slack/imports/${encodeURIComponent(args.jobId)}/uploads/complete`,
+ {
+ method: 'POST',
+ body: JSON.stringify(args.body),
+ signal: args.signal,
+ }
+ );
+ },
+
+ async finalizeSlackImport(args: SlackImportJobArgs) {
+ return dssFetch<ImportProgress>(
+ `/slack/imports/${encodeURIComponent(args.jobId)}/finalize`,
+ {
+ method: 'POST',
+ signal: args.signal,
+ }
+ );
+ },
+
+ async cancelSlackImport(args: SlackImportJobArgs) {
+ return dssFetch<ImportProgress>(
+ `/slack/imports/${encodeURIComponent(args.jobId)}/cancel`,
+ {
+ method: 'POST',
+ signal: args.signal,
+ }
+ );
+ },
+
async ping() {
return (await dssFetch<SuccessResponse>(`/ping`)).map(
(result) => result.data
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/completeUploads.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/completeUploads.ts
new file mode 100644
index 0000000000..9a87feb8e7
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/completeUploads.ts
@@ -0,0 +1,20 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+import type { CompleteUploadsSeal } from './completeUploadsSeal';
+import type { UploadId } from './uploadId';
+
+/**
+ * Explicit upload completion; a seal can be submitted separately with an empty list.
+ */
+export interface CompleteUploads {
+ seal?: CompleteUploadsSeal;
+ /**
+ * Registered identities to verify against storage (at most 50).
+ * @maxItems 50
+ */
+ uploads: UploadId[];
+}
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/completeUploadsSeal.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/completeUploadsSeal.ts
new file mode 100644
index 0000000000..961ec7b4d5
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/completeUploadsSeal.ts
@@ -0,0 +1,9 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+import type { ConversationSeal } from './conversationSeal';
+
+export type CompleteUploadsSeal = null | ConversationSeal;
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/conversationId.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/conversationId.ts
new file mode 100644
index 0000000000..78c06eb327
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/conversationId.ts
@@ -0,0 +1,11 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+
+/**
+ * Source Slack conversation ID (C, G or D prefix); unique only within a source.
+ */
+export type ConversationId = string;
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/conversationKind.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/conversationKind.ts
new file mode 100644
index 0000000000..5912261334
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/conversationKind.ts
@@ -0,0 +1,20 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+
+/**
+ * Slack conversation kind. Public Slack channels map to Macro Team, never Public.
+ */
+export type ConversationKind =
+ (typeof ConversationKind)[keyof typeof ConversationKind];
+
+// eslint-disable-next-line @typescript-eslint/no-redeclare
+export const ConversationKind = {
+ public_channel: 'public_channel',
+ private_channel: 'private_channel',
+ direct_message: 'direct_message',
+ group_direct_message: 'group_direct_message',
+} as const;
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/conversationMetadata.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/conversationMetadata.ts
new file mode 100644
index 0000000000..233fde5c5e
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/conversationMetadata.ts
@@ -0,0 +1,39 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+
+import type { ConversationId } from './conversationId';
+import type { ConversationKind } from './conversationKind';
+import type { ConversationMetadataCreatedAt } from './conversationMetadataCreatedAt';
+import type { ConversationMetadataCreatorId } from './conversationMetadataCreatorId';
+import type { ConversationMetadataMessageCount } from './conversationMetadataMessageCount';
+import type { KeySegment } from './keySegment';
+import type { SlackUserId } from './slackUserId';
+
+/**
+ * Full selected conversation metadata, persisted before granting any uploads.
+ */
+export interface ConversationMetadata {
+ /** Source archived flag (does not silently archive a reused Macro target). */
+ archived: boolean;
+ createdAt?: ConversationMetadataCreatedAt;
+ creatorId?: ConversationMetadataCreatorId;
+ /** Single archive folder name, not a backend object key. */
+ folder: KeySegment;
+ /** Source conversation kind. */
+ kind: ConversationKind;
+ /** Complete source member list, including members unknown to Macro. */
+ memberIds: SlackUserId[];
+ /**
+ * Advisory only; null means not yet counted, not zero.
+ * @minimum 0
+ */
+ messageCount?: ConversationMetadataMessageCount;
+ /** Original display name; not a storage key. */
+ name: string;
+ /** Stable Slack identity. */
+ slackChannelId: ConversationId;
+}
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/conversationMetadataCreatedAt.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/conversationMetadataCreatedAt.ts
new file mode 100644
index 0000000000..836d587ef0
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/conversationMetadataCreatedAt.ts
@@ -0,0 +1,9 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+import type { SlackTimestamp } from './slackTimestamp';
+
+export type ConversationMetadataCreatedAt = null | SlackTimestamp;
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/conversationMetadataCreatorId.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/conversationMetadataCreatorId.ts
new file mode 100644
index 0000000000..0b1b26ce0e
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/conversationMetadataCreatorId.ts
@@ -0,0 +1,9 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+import type { SlackUserId } from './slackUserId';
+
+export type ConversationMetadataCreatorId = null | SlackUserId;
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/conversationMetadataMessageCount.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/conversationMetadataMessageCount.ts
new file mode 100644
index 0000000000..31105352e7
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/conversationMetadataMessageCount.ts
@@ -0,0 +1,12 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+
+/**
+ * Advisory only; null means not yet counted, not zero.
+ * @minimum 0
+ */
+export type ConversationMetadataMessageCount = number | null;
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/conversationProgress.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/conversationProgress.ts
new file mode 100644
index 0000000000..07f73b0501
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/conversationProgress.ts
@@ -0,0 +1,51 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+
+import type { ConversationId } from './conversationId';
+import type { ConversationKind } from './conversationKind';
+import type { ConversationProgressChannelId } from './conversationProgressChannelId';
+import type { ConversationProgressError } from './conversationProgressError';
+import type { ConversationProgressPartCount } from './conversationProgressPartCount';
+import type { ConversationStatus } from './conversationStatus';
+import type { ImportCounters } from './importCounters';
+import type { ImportWarning } from './importWarning';
+import type { SearchState } from './searchState';
+
+/**
+ * Admin-visible progress for one selected conversation.
+ */
+export interface ConversationProgress {
+ /** Whether the selected source conversation was archived. */
+ archived: boolean;
+ /** Authorized target only; absent for inaccessible reused targets. */
+ channelId?: ConversationProgressChannelId;
+ /** Durably committed record counts. */
+ counters: ImportCounters;
+ error?: ConversationProgressError;
+ /** Persisted source kind. */
+ kind: ConversationKind;
+ /** Persisted source display name, never the reused Macro target's name. */
+ name: string;
+ /**
+ * Null until sealed; zero is a valid sealed empty manifest.
+ * @minimum 0
+ */
+ partCount?: ConversationProgressPartCount;
+ /** Independent indexing state. */
+ search: SearchState;
+ /** Source identity (always visible to the importing team's administrator). */
+ slackChannelId: ConversationId;
+ /** Durable state. */
+ status: ConversationStatus;
+ /**
+ * Number of parts whose object identity has been verified.
+ * @minimum 0
+ */
+ verifiedParts: number;
+ /** Non-fatal metadata/skip explanations. */
+ warnings: ImportWarning[];
+}
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/conversationProgressChannelId.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/conversationProgressChannelId.ts
new file mode 100644
index 0000000000..70019e8bb9
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/conversationProgressChannelId.ts
@@ -0,0 +1,11 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+
+/**
+ * Authorized target only; absent for inaccessible reused targets.
+ */
+export type ConversationProgressChannelId = string | null;
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/conversationProgressError.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/conversationProgressError.ts
new file mode 100644
index 0000000000..e4601dfecf
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/conversationProgressError.ts
@@ -0,0 +1,9 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+import type { ImportError } from './importError';
+
+export type ConversationProgressError = null | ImportError;
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/conversationProgressPartCount.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/conversationProgressPartCount.ts
new file mode 100644
index 0000000000..ea6666f61a
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/conversationProgressPartCount.ts
@@ -0,0 +1,12 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+
+/**
+ * Null until sealed; zero is a valid sealed empty manifest.
+ * @minimum 0
+ */
+export type ConversationProgressPartCount = number | null;
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/conversationSeal.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/conversationSeal.ts
new file mode 100644
index 0000000000..c832ef3408
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/conversationSeal.ts
@@ -0,0 +1,24 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+
+import type { ConversationId } from './conversationId';
+import type { Sha256Digest } from './sha256Digest';
+
+/**
+ * Complete immutable expected part set for one conversation, including zero parts.
+ */
+export interface ConversationSeal {
+ /** SHA-256 of canonical descriptor lines; zero parts hash the empty byte string. */
+ manifestSha256: Sha256Digest;
+ /**
+ * Exact number of registered parts, starting at index zero.
+ * @minimum 0
+ */
+ partCount: number;
+ /** Selected source conversation. */
+ slackChannelId: ConversationId;
+}
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/conversationStatus.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/conversationStatus.ts
new file mode 100644
index 0000000000..a53e3c1152
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/conversationStatus.ts
@@ -0,0 +1,22 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+
+/**
+ * Durable per-conversation lifecycle.
+ */
+export type ConversationStatus =
+ (typeof ConversationStatus)[keyof typeof ConversationStatus];
+
+// eslint-disable-next-line @typescript-eslint/no-redeclare
+export const ConversationStatus = {
+ awaiting_uploads: 'awaiting_uploads',
+ queued: 'queued',
+ importing: 'importing',
+ completed: 'completed',
+ skipped: 'skipped',
+ failed: 'failed',
+} as const;
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/createImport.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/createImport.ts
new file mode 100644
index 0000000000..4fcbc9bbe5
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/createImport.ts
@@ -0,0 +1,27 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+import type { ConversationMetadata } from './conversationMetadata';
+import type { CreateToken } from './createToken';
+import type { SourceIdentity } from './sourceIdentity';
+
+/**
+ * Create command. Team identity is deliberately absent.
+ */
+export interface CreateImport {
+ /**
+ * Full, unique selected conversations (at most the configured bound).
+ * @minItems 1
+ * @maxItems 2000
+ */
+ conversations: ConversationMetadata[];
+ /** Replay with the same semantic payload returns the original job; changes conflict. */
+ idempotencyToken: CreateToken;
+ /** Default true at the client; false still requires users and zero-part seals. */
+ includeMessageHistory: boolean;
+ /** Binding or explicit unknown-source confirmation. */
+ source: SourceIdentity;
+}
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/createToken.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/createToken.ts
new file mode 100644
index 0000000000..8ccc4c06d5
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/createToken.ts
@@ -0,0 +1,11 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+
+/**
+ * Client-generated create idempotency token, scoped to team and administrator.
+ */
+export type CreateToken = string;
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/importCounters.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/importCounters.ts
new file mode 100644
index 0000000000..60b4e264d1
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/importCounters.ts
@@ -0,0 +1,37 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+
+/**
+ * Committed counters, never optimistic browser or uncommitted worker counts.
+ */
+export interface ImportCounters {
+ /**
+ * Previously committed source identities.
+ * @minimum 0
+ */
+ duplicates: number;
+ /**
+ * Newly persisted messages.
+ * @minimum 0
+ */
+ imported: number;
+ /**
+ * Source records examined through the committed checkpoint.
+ * @minimum 0
+ */
+ processed: number;
+ /**
+ * Newly persisted reactions.
+ * @minimum 0
+ */
+ reactions: number;
+ /**
+ * Unsupported, empty or otherwise deliberately skipped records.
+ * @minimum 0
+ */
+ skipped: number;
+}
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/importError.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/importError.ts
new file mode 100644
index 0000000000..33d9c3485a
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/importError.ts
@@ -0,0 +1,26 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+
+/**
+ * Public error codes; never carry raw provider errors, keys, emails or source text.
+ */
+export type ImportError = (typeof ImportError)[keyof typeof ImportError];
+
+// eslint-disable-next-line @typescript-eslint/no-redeclare
+export const ImportError = {
+ disabled: 'disabled',
+ invalid_input: 'invalid_input',
+ limit_exceeded: 'limit_exceeded',
+ unavailable: 'unavailable',
+ admin_required: 'admin_required',
+ source_mismatch: 'source_mismatch',
+ conflict: 'conflict',
+ upload_mismatch: 'upload_mismatch',
+ lease_lost: 'lease_lost',
+ retryable: 'retryable',
+ internal: 'internal',
+} as const;
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/importLimits.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/importLimits.ts
new file mode 100644
index 0000000000..b18fc1b8ef
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/importLimits.ts
@@ -0,0 +1,62 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+
+/**
+ * Configurable bounds shared with browser staging and enforced again by the worker.
+ */
+export interface ImportLimits {
+ /**
+ * Maximum selected conversations.
+ * @minimum 0
+ */
+ conversations: number;
+ /**
+ * Maximum historical batch payload bytes.
+ * @minimum 0
+ */
+ databaseBatchBytes: number;
+ /**
+ * Maximum messages in a historical batch.
+ * @minimum 0
+ */
+ databaseBatchMessages: number;
+ /**
+ * Maximum users, root metadata or individual day JSON bytes.
+ * @minimum 0
+ */
+ jsonBytes: number;
+ /**
+ * Maximum NDJSON part bytes.
+ * @minimum 0
+ */
+ partBytes: number;
+ /**
+ * Maximum records per part.
+ * @minimum 0
+ */
+ partRecords: number;
+ /**
+ * Maximum bytes per NDJSON record, including newline.
+ * @minimum 0
+ */
+ recordBytes: number;
+ /**
+ * Maximum descriptors or completion identities per call.
+ * @minimum 0
+ */
+ registrationBatch: number;
+ /**
+ * Maximum selected temporary data bytes across all uploads.
+ * @minimum 0
+ */
+ selectedBytes: number;
+ /**
+ * Maximum ZIP entries scanned by the browser.
+ * @minimum 0
+ */
+ zipEntries: number;
+}
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/importPage.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/importPage.ts
new file mode 100644
index 0000000000..403faa3162
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/importPage.ts
@@ -0,0 +1,24 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+
+import type { ImportLimits } from './importLimits';
+import type { ImportPageNextCursor } from './importPageNextCursor';
+import type { ImportProgress } from './importProgress';
+import type { SourceBinding } from './sourceBinding';
+
+/**
+ * Admin list result, also supplying upload limits before a job exists.
+ */
+export interface ImportPage {
+ /** At most 50 job receipts, newest first. */
+ jobs: ImportProgress[];
+ /** Effective server limits. */
+ limits: ImportLimits;
+ nextCursor?: ImportPageNextCursor;
+ /** Binding summary used by the picker confirmation. */
+ sourceBinding: SourceBinding;
+}
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/importPageNextCursor.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/importPageNextCursor.ts
new file mode 100644
index 0000000000..241a3fc9ce
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/importPageNextCursor.ts
@@ -0,0 +1,9 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+import type { JobId } from './jobId';
+
+export type ImportPageNextCursor = null | JobId;
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/importProgress.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/importProgress.ts
new file mode 100644
index 0000000000..c6d3be7582
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/importProgress.ts
@@ -0,0 +1,43 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+import type { ConversationProgress } from './conversationProgress';
+import type { ImportLimits } from './importLimits';
+import type { ImportProgressRegistrationClosedAt } from './importProgressRegistrationClosedAt';
+import type { JobId } from './jobId';
+import type { JobStatus } from './jobStatus';
+import type { SourceIdentity } from './sourceIdentity';
+
+/**
+ * Common receipt returned by create, completion, finalize, cancel and progress.
+ */
+export interface ImportProgress {
+ /** All selected conversations, bounded by the create limit. */
+ conversations: ConversationProgress[];
+ /** Persisted creation time, also the final historical-time fallback. */
+ createdAt: string;
+ /** Immutable history option confirmed when creating this job. */
+ includeMessageHistory: boolean;
+ /** Stable job identity. */
+ jobId: JobId;
+ /** Server bounds for staging and registration. */
+ limits: ImportLimits;
+ /** Registration closure time; cancellation also closes registration. */
+ registrationClosedAt?: ImportProgressRegistrationClosedAt;
+ /**
+ * Monotonic job revision for polling/websocket invalidation.
+ * @minimum 0
+ */
+ revision: number;
+ /** Immutable source confirmation from this job, not the current team binding. */
+ source: SourceIdentity;
+ /** Durable job lifecycle. */
+ status: JobStatus;
+ /** Last durable lifecycle/progress update. */
+ updatedAt: string;
+ /** Whether users metadata is verified and pinned. */
+ usersVerified: boolean;
+}
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/importProgressRegistrationClosedAt.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/importProgressRegistrationClosedAt.ts
new file mode 100644
index 0000000000..54b3a875ae
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/importProgressRegistrationClosedAt.ts
@@ -0,0 +1,11 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+
+/**
+ * Registration closure time; cancellation also closes registration.
+ */
+export type ImportProgressRegistrationClosedAt = string | null;
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/importWarning.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/importWarning.ts
new file mode 100644
index 0000000000..e62013cbbe
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/importWarning.ts
@@ -0,0 +1,20 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+
+/**
+ * Non-fatal, sanitized explanations displayed in admin progress.
+ */
+export type ImportWarning = (typeof ImportWarning)[keyof typeof ImportWarning];
+
+// eslint-disable-next-line @typescript-eslint/no-redeclare
+export const ImportWarning = {
+ creation_time_from_message: 'creation_time_from_message',
+ creation_time_from_job: 'creation_time_from_job',
+ unresolvable_direct_message: 'unresolvable_direct_message',
+ target_unavailable: 'target_unavailable',
+ uploads_incomplete: 'uploads_incomplete',
+} as const;
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/index.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/index.ts
index 4e1fadcc97..a732c93411 100644
--- a/apps/web/src/lib/service-clients/service-storage/generated/schemas/index.ts
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/index.ts
@@ -332,7 +332,21 @@ export * from './commandFailure';
export * from './commandRejectedMetadata';
export * from './commandRejectedMetadataActor';
export * from './commandRejectedMetadataAnnouncementMessageId';
+export * from './completeUploads';
+export * from './completeUploadsSeal';
export * from './conferenceProvider';
+export * from './conversationId';
+export * from './conversationKind';
+export * from './conversationMetadata';
+export * from './conversationMetadataCreatedAt';
+export * from './conversationMetadataCreatorId';
+export * from './conversationMetadataMessageCount';
+export * from './conversationProgress';
+export * from './conversationProgressChannelId';
+export * from './conversationProgressError';
+export * from './conversationProgressPartCount';
+export * from './conversationSeal';
+export * from './conversationStatus';
export * from './copyDocumentParams';
export * from './copyDocumentQueryParams';
export * from './copyDocumentQueryParamsVersionId';
@@ -395,6 +409,7 @@ export * from './createdUserApiKey';
export * from './createEntityMentionRequest';
export * from './createEntityMentionResponse';
export * from './createEntityMentionResponseUserId';
+export * from './createImport';
export * from './createInitiativeRequest';
export * from './createInitiativeRequestDescription';
export * from './createInitiativeRequestMemberIds';
@@ -441,6 +456,7 @@ export * from './createTaskRequestTeamId';
export * from './createTaskResponse';
export * from './createTaskResponseTeamId';
export * from './createTaskResponseTeamTaskId';
+export * from './createToken';
export * from './createUnthreadedAnchorRequest';
export * from './createUnthreadedAnchorRequestOneOf';
export * from './createUnthreadedAnchorRequestOneOfAllOf';
@@ -815,7 +831,15 @@ export * from './hashMapOneOfFour';
export * from './hashMapOneOfFourAllOf';
export * from './hashMapOneOfFourAllOfType';
export * from './highlightType';
+export * from './importCounters';
+export * from './importError';
export * from './importedAuthor';
+export * from './importLimits';
+export * from './importPage';
+export * from './importPageNextCursor';
+export * from './importProgress';
+export * from './importProgressRegistrationClosedAt';
+export * from './importWarning';
export * from './inFlightTurnSummary';
export * from './inFlightTurnSummaryActor';
export * from './inFlightTurnSummaryAnnouncementMessageId';
@@ -834,6 +858,9 @@ export * from './inviteMeetingRequest';
export * from './inviteMeetingUsersRequest';
export * from './item';
export * from './itemWithUserAccessLevel';
+export * from './jobId';
+export * from './jobStatus';
+export * from './keySegment';
export * from './latestMessage';
export * from './latestMessageLatestMessage';
export * from './latestMessageLatestNonThreadMessage';
@@ -845,6 +872,7 @@ export * from './listOccurrencesParams';
export * from './listReminderCollectionParams';
export * from './listRemindersEntityTypeItem';
export * from './listRemindersParams';
+export * from './listSlackImportsParams';
export * from './listTeamOutOfOfficeParams';
export * from './listWebhooksResponse';
export * from './locationResponseData';
@@ -1085,6 +1113,7 @@ export * from './propertyValueOneOfType';
export * from './reactionInput';
export * from './reactionInputNonce';
export * from './recentlyDeletedResponseData';
+export * from './registerUploads';
export * from './reminder';
export * from './reminderCollectionPage';
export * from './reminderCollectionPageNextCursor';
@@ -1126,6 +1155,17 @@ export * from './saveDocumentResponseData';
export * from './saveDocumentResponseDataPresignedUrl';
export * from './searchContactsParams';
export * from './searchContactsResponse';
+export * from './searchState';
+export * from './searchStateOneOf';
+export * from './searchStateOneOfFive';
+export * from './searchStateOneOfFiveStatus';
+export * from './searchStateOneOfNine';
+export * from './searchStateOneOfNineStatus';
+export * from './searchStateOneOfSeven';
+export * from './searchStateOneOfSevenStatus';
+export * from './searchStateOneOfStatus';
+export * from './searchStateOneOfThree';
+export * from './searchStateOneOfThreeStatus';
export * from './sessionDeletedMetadata';
export * from './sessionIdentity';
export * from './sessionIdentityOrigin';
@@ -1169,6 +1209,7 @@ export * from './setPropertyValueOneOfSevenType';
export * from './setPropertyValueOneOfThree';
export * from './setPropertyValueOneOfThreeType';
export * from './setPropertyValueOneOfType';
+export * from './sha256Digest';
export * from './sharedEmailFilter';
export * from './sharePermissionV2';
export * from './sharePermissionV2ChannelSharePermissions';
@@ -1177,6 +1218,11 @@ export * from './sharePermissionV2LinkShareAccessLevel';
export * from './sharePermissionV2TeamShareAccessLevel';
export * from './shortIdResponse';
export * from './simpleMention';
+export * from './slackCompleteRequest';
+export * from './slackCreateRequest';
+export * from './slackRegisterRequest';
+export * from './slackTimestamp';
+export * from './slackUserId';
export * from './smartTagChannelMatch';
export * from './smartTagPreview';
export * from './soupAgentSessionSoupPropertiesField';
@@ -1364,6 +1410,19 @@ export * from './soupThreadInfo';
export * from './soupThreadInfoLatestReplyAt';
export * from './soupThreadReply';
export * from './soupThreadReplyEditedAt';
+export * from './sourceBinding';
+export * from './sourceBindingOneOf';
+export * from './sourceBindingOneOfFive';
+export * from './sourceBindingOneOfFiveKind';
+export * from './sourceBindingOneOfKind';
+export * from './sourceBindingOneOfThree';
+export * from './sourceBindingOneOfThreeKind';
+export * from './sourceId';
+export * from './sourceIdentity';
+export * from './sourceIdentityOneOf';
+export * from './sourceIdentityOneOfKind';
+export * from './sourceIdentityOneOfThree';
+export * from './sourceIdentityOneOfThreeKind';
export * from './starterDocumentsResponse';
export * from './streamEventsParams';
export * from './string';
@@ -1451,6 +1510,8 @@ export * from './updateSharePermissionRequestV2ChannelSharePermissions';
export * from './updateSharePermissionRequestV2LinkShare';
export * from './updateSharePermissionRequestV2LinkShareAccessLevel';
export * from './updateSharePermissionRequestV2TeamShareAccessLevel';
+export * from './uploadDescriptor';
+export * from './uploadDescriptorRecordCount';
export * from './uploadDocumentStatus';
export * from './uploadExtractFolderHandler200';
export * from './uploadExtractFolderHandler200DataOneOf';
@@ -1461,6 +1522,13 @@ export * from './uploadFolderHandler200';
export * from './uploadFolderHandler200DataOneOf';
export * from './uploadFolderRequest';
export * from './uploadFolderRequestParentId';
+export * from './uploadGrant';
+export * from './uploadGrantRequiredHeaders';
+export * from './uploadId';
+export * from './uploadIdOneOf';
+export * from './uploadIdOneOfKind';
+export * from './uploadIdOneOfThree';
+export * from './uploadIdOneOfThreeKind';
export * from './upsertUserDocumentViewLocationRequest';
export * from './userApiKeyId';
export * from './userApiKeyInfo';
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/jobId.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/jobId.ts
new file mode 100644
index 0000000000..943500d6e6
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/jobId.ts
@@ -0,0 +1,11 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+
+/**
+ * Import job identity. Generate UUIDv7 in application code.
+ */
+export type JobId = string;
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/jobStatus.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/jobStatus.ts
new file mode 100644
index 0000000000..650fd69c82
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/jobStatus.ts
@@ -0,0 +1,22 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+
+/**
+ * Job lifecycle. Cancellation remains in progress while any lease is active.
+ */
+export type JobStatus = (typeof JobStatus)[keyof typeof JobStatus];
+
+// eslint-disable-next-line @typescript-eslint/no-redeclare
+export const JobStatus = {
+ uploading: 'uploading',
+ processing: 'processing',
+ completed: 'completed',
+ completed_with_errors: 'completed_with_errors',
+ failed: 'failed',
+ cancelling: 'cancelling',
+ cancelled: 'cancelled',
+} as const;
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/keySegment.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/keySegment.ts
new file mode 100644
index 0000000000..4ba4003f09
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/keySegment.ts
@@ -0,0 +1,11 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+
+/**
+ * Single safe source folder/key segment; never a path or authorization proof.
+ */
+export type KeySegment = string;
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/listSlackImportsParams.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/listSlackImportsParams.ts
new file mode 100644
index 0000000000..b167dad583
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/listSlackImportsParams.ts
@@ -0,0 +1,11 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+import type { JobId } from './jobId';
+
+export type ListSlackImportsParams = {
+ before?: JobId;
+};
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/registerUploads.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/registerUploads.ts
new file mode 100644
index 0000000000..0077140f78
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/registerUploads.ts
@@ -0,0 +1,18 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+import type { UploadDescriptor } from './uploadDescriptor';
+
+/**
+ * Batched registration, also used to renew an unchanged descriptor's grant.
+ */
+export interface RegisterUploads {
+ /**
+ * At most 50 descriptors; no duplicate identities in a call.
+ * @maxItems 50
+ */
+ descriptors: UploadDescriptor[];
+}
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/searchState.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/searchState.ts
new file mode 100644
index 0000000000..54d0174563
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/searchState.ts
@@ -0,0 +1,22 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+import type { SearchStateOneOf } from './searchStateOneOf';
+import type { SearchStateOneOfFive } from './searchStateOneOfFive';
+import type { SearchStateOneOfNine } from './searchStateOneOfNine';
+import type { SearchStateOneOfSeven } from './searchStateOneOfSeven';
+import type { SearchStateOneOfThree } from './searchStateOneOfThree';
+
+/**
+ * Search publication/receipt state. Acceptance is not completed publication;
+even completed publication still awaits consumer indexing and refresh.
+ */
+export type SearchState =
+ | SearchStateOneOf
+ | SearchStateOneOfThree
+ | SearchStateOneOfFive
+ | SearchStateOneOfSeven
+ | SearchStateOneOfNine;
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/searchStateOneOf.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/searchStateOneOf.ts
new file mode 100644
index 0000000000..b0005f19f6
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/searchStateOneOf.ts
@@ -0,0 +1,14 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+import type { SearchStateOneOfStatus } from './searchStateOneOfStatus';
+
+/**
+ * No committed history requires indexing.
+ */
+export type SearchStateOneOf = {
+ status: SearchStateOneOfStatus;
+};
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/searchStateOneOfFive.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/searchStateOneOfFive.ts
new file mode 100644
index 0000000000..ae068bf903
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/searchStateOneOfFive.ts
@@ -0,0 +1,16 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+import type { SearchStateOneOfFiveStatus } from './searchStateOneOfFiveStatus';
+
+/**
+ * Search service accepted the request, but has not completed it.
+ */
+export type SearchStateOneOfFive = {
+ /** Receipt from the search service. */
+ receiptId: string;
+ status: SearchStateOneOfFiveStatus;
+};
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/searchStateOneOfFiveStatus.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/searchStateOneOfFiveStatus.ts
new file mode 100644
index 0000000000..a564a41034
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/searchStateOneOfFiveStatus.ts
@@ -0,0 +1,14 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+
+export type SearchStateOneOfFiveStatus =
+ (typeof SearchStateOneOfFiveStatus)[keyof typeof SearchStateOneOfFiveStatus];
+
+// eslint-disable-next-line @typescript-eslint/no-redeclare
+export const SearchStateOneOfFiveStatus = {
+ submitted: 'submitted',
+} as const;
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/searchStateOneOfNine.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/searchStateOneOfNine.ts
new file mode 100644
index 0000000000..3c178cd5a5
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/searchStateOneOfNine.ts
@@ -0,0 +1,14 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+import type { SearchStateOneOfNineStatus } from './searchStateOneOfNineStatus';
+
+/**
+ * Publication failed; retained history must be retried independently.
+ */
+export type SearchStateOneOfNine = {
+ status: SearchStateOneOfNineStatus;
+};
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/searchStateOneOfNineStatus.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/searchStateOneOfNineStatus.ts
new file mode 100644
index 0000000000..9951e85a91
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/searchStateOneOfNineStatus.ts
@@ -0,0 +1,14 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+
+export type SearchStateOneOfNineStatus =
+ (typeof SearchStateOneOfNineStatus)[keyof typeof SearchStateOneOfNineStatus];
+
+// eslint-disable-next-line @typescript-eslint/no-redeclare
+export const SearchStateOneOfNineStatus = {
+ failed: 'failed',
+} as const;
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/searchStateOneOfSeven.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/searchStateOneOfSeven.ts
new file mode 100644
index 0000000000..1441b6cf3e
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/searchStateOneOfSeven.ts
@@ -0,0 +1,14 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+import type { SearchStateOneOfSevenStatus } from './searchStateOneOfSevenStatus';
+
+/**
+ * Search service durably published the scope; eventual indexing is separate.
+ */
+export type SearchStateOneOfSeven = {
+ status: SearchStateOneOfSevenStatus;
+};
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/searchStateOneOfSevenStatus.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/searchStateOneOfSevenStatus.ts
new file mode 100644
index 0000000000..5446f0d665
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/searchStateOneOfSevenStatus.ts
@@ -0,0 +1,14 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+
+export type SearchStateOneOfSevenStatus =
+ (typeof SearchStateOneOfSevenStatus)[keyof typeof SearchStateOneOfSevenStatus];
+
+// eslint-disable-next-line @typescript-eslint/no-redeclare
+export const SearchStateOneOfSevenStatus = {
+ completed: 'completed',
+} as const;
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/searchStateOneOfStatus.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/searchStateOneOfStatus.ts
new file mode 100644
index 0000000000..d489b0b883
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/searchStateOneOfStatus.ts
@@ -0,0 +1,14 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+
+export type SearchStateOneOfStatus =
+ (typeof SearchStateOneOfStatus)[keyof typeof SearchStateOneOfStatus];
+
+// eslint-disable-next-line @typescript-eslint/no-redeclare
+export const SearchStateOneOfStatus = {
+ not_needed: 'not_needed',
+} as const;
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/searchStateOneOfThree.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/searchStateOneOfThree.ts
new file mode 100644
index 0000000000..519bc8b1f6
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/searchStateOneOfThree.ts
@@ -0,0 +1,14 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+import type { SearchStateOneOfThreeStatus } from './searchStateOneOfThreeStatus';
+
+/**
+ * Committed history requires an outbox-backed scoped submission.
+ */
+export type SearchStateOneOfThree = {
+ status: SearchStateOneOfThreeStatus;
+};
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/searchStateOneOfThreeStatus.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/searchStateOneOfThreeStatus.ts
new file mode 100644
index 0000000000..6d340e10cb
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/searchStateOneOfThreeStatus.ts
@@ -0,0 +1,14 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+
+export type SearchStateOneOfThreeStatus =
+ (typeof SearchStateOneOfThreeStatus)[keyof typeof SearchStateOneOfThreeStatus];
+
+// eslint-disable-next-line @typescript-eslint/no-redeclare
+export const SearchStateOneOfThreeStatus = {
+ pending: 'pending',
+} as const;
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/sha256Digest.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/sha256Digest.ts
new file mode 100644
index 0000000000..9766e9f959
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/sha256Digest.ts
@@ -0,0 +1,11 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+
+/**
+ * SHA-256 encoded as exactly 64 lowercase hexadecimal characters.
+ */
+export type Sha256Digest = string;
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/slackCompleteRequest.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/slackCompleteRequest.ts
new file mode 100644
index 0000000000..3c6554dfaa
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/slackCompleteRequest.ts
@@ -0,0 +1,12 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+import type { CompleteUploads } from './completeUploads';
+
+/**
+ * At most fifty registered identities to verify and an optional immutable conversation seal.
+ */
+export type SlackCompleteRequest = CompleteUploads;
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/slackCreateRequest.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/slackCreateRequest.ts
new file mode 100644
index 0000000000..8391613df7
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/slackCreateRequest.ts
@@ -0,0 +1,12 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+import type { CreateImport } from './createImport';
+
+/**
+ * Bounded full-metadata create payload. Domain validation applies effective limits again.
+ */
+export type SlackCreateRequest = CreateImport;
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/slackRegisterRequest.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/slackRegisterRequest.ts
new file mode 100644
index 0000000000..32d9285c2a
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/slackRegisterRequest.ts
@@ -0,0 +1,12 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+import type { RegisterUploads } from './registerUploads';
+
+/**
+ * At most fifty immutable upload descriptors, without client-supplied storage keys.
+ */
+export type SlackRegisterRequest = RegisterUploads;
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/slackTimestamp.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/slackTimestamp.ts
new file mode 100644
index 0000000000..59ff2aa96a
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/slackTimestamp.ts
@@ -0,0 +1,12 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+
+/**
+ * Slack time represented exactly as nonnegative Unix microseconds.
+JSON is a string with six fractional digits, never a floating-point number.
+ */
+export type SlackTimestamp = string;
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/slackUserId.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/slackUserId.ts
new file mode 100644
index 0000000000..2407e65389
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/slackUserId.ts
@@ -0,0 +1,11 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+
+/**
+ * Slack member identity (U or W prefix), including USLACKBOT.
+ */
+export type SlackUserId = string;
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/sourceBinding.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/sourceBinding.ts
new file mode 100644
index 0000000000..11079536b9
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/sourceBinding.ts
@@ -0,0 +1,17 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+import type { SourceBindingOneOf } from './sourceBindingOneOf';
+import type { SourceBindingOneOfFive } from './sourceBindingOneOfFive';
+import type { SourceBindingOneOfThree } from './sourceBindingOneOfThree';
+
+/**
+ * Team's durable single-source binding, including confirmed unidentified archives.
+ */
+export type SourceBinding =
+ | SourceBindingOneOf
+ | SourceBindingOneOfThree
+ | SourceBindingOneOfFive;
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/sourceBindingOneOf.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/sourceBindingOneOf.ts
new file mode 100644
index 0000000000..4fa6460ecf
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/sourceBindingOneOf.ts
@@ -0,0 +1,14 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+import type { SourceBindingOneOfKind } from './sourceBindingOneOfKind';
+
+/**
+ * No import has bound this team yet.
+ */
+export type SourceBindingOneOf = {
+ kind: SourceBindingOneOfKind;
+};
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/sourceBindingOneOfFive.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/sourceBindingOneOfFive.ts
new file mode 100644
index 0000000000..e324487ca3
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/sourceBindingOneOfFive.ts
@@ -0,0 +1,17 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+import type { SourceBindingOneOfFiveKind } from './sourceBindingOneOfFiveKind';
+import type { SourceId } from './sourceId';
+
+/**
+ * Known Slack workspace; reject mismatches.
+ */
+export type SourceBindingOneOfFive = {
+ kind: SourceBindingOneOfFiveKind;
+ /** Bound source identity. */
+ sourceId: SourceId;
+};
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/sourceBindingOneOfFiveKind.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/sourceBindingOneOfFiveKind.ts
new file mode 100644
index 0000000000..7e78bf9a9b
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/sourceBindingOneOfFiveKind.ts
@@ -0,0 +1,14 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+
+export type SourceBindingOneOfFiveKind =
+ (typeof SourceBindingOneOfFiveKind)[keyof typeof SourceBindingOneOfFiveKind];
+
+// eslint-disable-next-line @typescript-eslint/no-redeclare
+export const SourceBindingOneOfFiveKind = {
+ known: 'known',
+} as const;
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/sourceBindingOneOfKind.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/sourceBindingOneOfKind.ts
new file mode 100644
index 0000000000..f16c7ef593
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/sourceBindingOneOfKind.ts
@@ -0,0 +1,14 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+
+export type SourceBindingOneOfKind =
+ (typeof SourceBindingOneOfKind)[keyof typeof SourceBindingOneOfKind];
+
+// eslint-disable-next-line @typescript-eslint/no-redeclare
+export const SourceBindingOneOfKind = {
+ unbound: 'unbound',
+} as const;
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/sourceBindingOneOfThree.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/sourceBindingOneOfThree.ts
new file mode 100644
index 0000000000..2b91679c89
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/sourceBindingOneOfThree.ts
@@ -0,0 +1,14 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+import type { SourceBindingOneOfThreeKind } from './sourceBindingOneOfThreeKind';
+
+/**
+ * Legacy/onboarding or explicitly confirmed unidentified source.
+ */
+export type SourceBindingOneOfThree = {
+ kind: SourceBindingOneOfThreeKind;
+};
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/sourceBindingOneOfThreeKind.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/sourceBindingOneOfThreeKind.ts
new file mode 100644
index 0000000000..72b25a8dc2
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/sourceBindingOneOfThreeKind.ts
@@ -0,0 +1,14 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+
+export type SourceBindingOneOfThreeKind =
+ (typeof SourceBindingOneOfThreeKind)[keyof typeof SourceBindingOneOfThreeKind];
+
+// eslint-disable-next-line @typescript-eslint/no-redeclare
+export const SourceBindingOneOfThreeKind = {
+ confirmed_unknown: 'confirmed_unknown',
+} as const;
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/sourceId.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/sourceId.ts
new file mode 100644
index 0000000000..54511256af
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/sourceId.ts
@@ -0,0 +1,11 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+
+/**
+ * Known Slack workspace identity (T prefix); an enterprise ID alone is insufficient.
+ */
+export type SourceId = string;
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/sourceIdentity.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/sourceIdentity.ts
new file mode 100644
index 0000000000..25dc37094f
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/sourceIdentity.ts
@@ -0,0 +1,13 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+import type { SourceIdentityOneOf } from './sourceIdentityOneOf';
+import type { SourceIdentityOneOfThree } from './sourceIdentityOneOfThree';
+
+/**
+ * Source identity supplied when creating a job. One binding per Macro team in v1.
+ */
+export type SourceIdentity = SourceIdentityOneOf | SourceIdentityOneOfThree;
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/sourceIdentityOneOf.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/sourceIdentityOneOf.ts
new file mode 100644
index 0000000000..64239c967b
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/sourceIdentityOneOf.ts
@@ -0,0 +1,18 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+
+import type { SourceId } from './sourceId';
+import type { SourceIdentityOneOfKind } from './sourceIdentityOneOfKind';
+
+/**
+ * Archive names its Slack workspace; reject a conflicting team binding.
+ */
+export type SourceIdentityOneOf = {
+ kind: SourceIdentityOneOfKind;
+ /** Workspace identifier in the archive. */
+ sourceId: SourceId;
+};
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/sourceIdentityOneOfKind.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/sourceIdentityOneOfKind.ts
new file mode 100644
index 0000000000..abd3728efb
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/sourceIdentityOneOfKind.ts
@@ -0,0 +1,14 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+
+export type SourceIdentityOneOfKind =
+ (typeof SourceIdentityOneOfKind)[keyof typeof SourceIdentityOneOfKind];
+
+// eslint-disable-next-line @typescript-eslint/no-redeclare
+export const SourceIdentityOneOfKind = {
+ known: 'known',
+} as const;
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/sourceIdentityOneOfThree.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/sourceIdentityOneOfThree.ts
new file mode 100644
index 0000000000..ac20603df5
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/sourceIdentityOneOfThree.ts
@@ -0,0 +1,15 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+import type { SourceIdentityOneOfThreeKind } from './sourceIdentityOneOfThreeKind';
+
+/**
+ * Explicit confirmation that an unidentified archive belongs to this team's source.
+Choosing this variant is affirmative confirmation, not an inferred default.
+ */
+export type SourceIdentityOneOfThree = {
+ kind: SourceIdentityOneOfThreeKind;
+};
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/sourceIdentityOneOfThreeKind.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/sourceIdentityOneOfThreeKind.ts
new file mode 100644
index 0000000000..abea410509
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/sourceIdentityOneOfThreeKind.ts
@@ -0,0 +1,14 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+
+export type SourceIdentityOneOfThreeKind =
+ (typeof SourceIdentityOneOfThreeKind)[keyof typeof SourceIdentityOneOfThreeKind];
+
+// eslint-disable-next-line @typescript-eslint/no-redeclare
+export const SourceIdentityOneOfThreeKind = {
+ confirmed_unknown: 'confirmed_unknown',
+} as const;
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/uploadDescriptor.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/uploadDescriptor.ts
new file mode 100644
index 0000000000..86d7a2380e
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/uploadDescriptor.ts
@@ -0,0 +1,30 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+
+import type { Sha256Digest } from './sha256Digest';
+import type { UploadDescriptorRecordCount } from './uploadDescriptorRecordCount';
+import type { UploadId } from './uploadId';
+
+/**
+ * Immutable expected object properties, registered before signing an upload.
+ */
+export interface UploadDescriptor {
+ /**
+ * Exact byte length including delimiters.
+ * @minimum 0
+ */
+ byteLength: number;
+ /**
+ * Exact NDJSON record count; null for the users JSON payload.
+ * @minimum 0
+ */
+ recordCount?: UploadDescriptorRecordCount;
+ /** SHA-256 of the exact uploaded bytes, not the ETag. */
+ sha256: Sha256Digest;
+ /** Manifest identity within this job. */
+ upload: UploadId;
+}
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/uploadDescriptorRecordCount.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/uploadDescriptorRecordCount.ts
new file mode 100644
index 0000000000..82af6cf99b
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/uploadDescriptorRecordCount.ts
@@ -0,0 +1,12 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+
+/**
+ * Exact NDJSON record count; null for the users JSON payload.
+ * @minimum 0
+ */
+export type UploadDescriptorRecordCount = number | null;
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/uploadGrant.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/uploadGrant.ts
new file mode 100644
index 0000000000..a0933a8758
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/uploadGrant.ts
@@ -0,0 +1,23 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+import type { UploadDescriptor } from './uploadDescriptor';
+import type { UploadGrantRequiredHeaders } from './uploadGrantRequiredHeaders';
+
+/**
+ * Signed upload permission returned just before PUT; never persisted as an identity.
+ */
+export interface UploadGrant {
+ /** Exact immutable descriptor being signed. */
+ descriptor: UploadDescriptor;
+ /** Grant expiry; callers renew by re-registering the identical descriptor. */
+ expiresAt: string;
+ /** Required signed upload headers (including checksum, content type and create-only
+condition). Browser-forbidden Content-Length is derived from the exact Blob bytes. */
+ requiredHeaders: UploadGrantRequiredHeaders;
+ /** Short-lived signed destination; must not be logged. */
+ url: string;
+}
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/uploadGrantRequiredHeaders.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/uploadGrantRequiredHeaders.ts
new file mode 100644
index 0000000000..08985560f5
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/uploadGrantRequiredHeaders.ts
@@ -0,0 +1,12 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+
+/**
+ * Required signed upload headers (including checksum, content type and create-only
+condition). Browser-forbidden Content-Length is derived from the exact Blob bytes.
+ */
+export type UploadGrantRequiredHeaders = { [key: string]: string };
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/uploadId.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/uploadId.ts
new file mode 100644
index 0000000000..23409ebd06
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/uploadId.ts
@@ -0,0 +1,13 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+import type { UploadIdOneOf } from './uploadIdOneOf';
+import type { UploadIdOneOfThree } from './uploadIdOneOfThree';
+
+/**
+ * Identity in a job's persisted manifest. No user-supplied object keys.
+ */
+export type UploadId = UploadIdOneOf | UploadIdOneOfThree;
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/uploadIdOneOf.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/uploadIdOneOf.ts
new file mode 100644
index 0000000000..73fddaac0a
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/uploadIdOneOf.ts
@@ -0,0 +1,14 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+import type { UploadIdOneOfKind } from './uploadIdOneOfKind';
+
+/**
+ * One normalized users payload from users.json and/or org_users.json.
+ */
+export type UploadIdOneOf = {
+ kind: UploadIdOneOfKind;
+};
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/uploadIdOneOfKind.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/uploadIdOneOfKind.ts
new file mode 100644
index 0000000000..8b254c30af
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/uploadIdOneOfKind.ts
@@ -0,0 +1,14 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+
+export type UploadIdOneOfKind =
+ (typeof UploadIdOneOfKind)[keyof typeof UploadIdOneOfKind];
+
+// eslint-disable-next-line @typescript-eslint/no-redeclare
+export const UploadIdOneOfKind = {
+ users: 'users',
+} as const;
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/uploadIdOneOfThree.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/uploadIdOneOfThree.ts
new file mode 100644
index 0000000000..c1d3255462
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/uploadIdOneOfThree.ts
@@ -0,0 +1,23 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+
+import type { ConversationId } from './conversationId';
+import type { UploadIdOneOfThreeKind } from './uploadIdOneOfThreeKind';
+
+/**
+ * One NDJSON part in a selected conversation.
+ */
+export type UploadIdOneOfThree = {
+ kind: UploadIdOneOfThreeKind;
+ /**
+ * Zero-based part index; sealed manifests must be contiguous.
+ * @minimum 0
+ */
+ partIndex: number;
+ /** Selected Slack conversation. */
+ slackChannelId: ConversationId;
+};
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/uploadIdOneOfThreeKind.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/uploadIdOneOfThreeKind.ts
new file mode 100644
index 0000000000..255e9904b1
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/uploadIdOneOfThreeKind.ts
@@ -0,0 +1,14 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+
+export type UploadIdOneOfThreeKind =
+ (typeof UploadIdOneOfThreeKind)[keyof typeof UploadIdOneOfThreeKind];
+
+// eslint-disable-next-line @typescript-eslint/no-redeclare
+export const UploadIdOneOfThreeKind = {
+ conversation_part: 'conversation_part',
+} as const;
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/zod.ts b/apps/web/src/lib/service-clients/service-storage/generated/zod.ts
index 5ec211e83b..dd6cd7bcb3 100644
--- a/apps/web/src/lib/service-clients/service-storage/generated/zod.ts
+++ b/apps/web/src/lib/service-clients/service-storage/generated/zod.ts
@@ -34458,6 +34458,2589 @@ export const patchViewHandlerBody = zod.object({
name: zod.string().nullish(),
});
+/**
+ * @summary List this administrator's team receipts, effective server limits and source binding.
+ */
+export const listSlackImportsQueryParams = zod.object({
+ before: zod.uuid().optional(),
+});
+
+export const listSlackImportsResponseJobsItemConversationsItemCountersDuplicatesMin = 0;
+
+export const listSlackImportsResponseJobsItemConversationsItemCountersImportedMin = 0;
+
+export const listSlackImportsResponseJobsItemConversationsItemCountersProcessedMin = 0;
+
+export const listSlackImportsResponseJobsItemConversationsItemCountersReactionsMin = 0;
+
+export const listSlackImportsResponseJobsItemConversationsItemCountersSkippedMin = 0;
+
+export const listSlackImportsResponseJobsItemConversationsItemPartCountMin = 0;
+
+export const listSlackImportsResponseJobsItemConversationsItemVerifiedPartsMin = 0;
+
+export const listSlackImportsResponseJobsItemLimitsConversationsMin = 0;
+
+export const listSlackImportsResponseJobsItemLimitsDatabaseBatchBytesMin = 0;
+
+export const listSlackImportsResponseJobsItemLimitsDatabaseBatchMessagesMin = 0;
+
+export const listSlackImportsResponseJobsItemLimitsJsonBytesMin = 0;
+
+export const listSlackImportsResponseJobsItemLimitsPartBytesMin = 0;
+
+export const listSlackImportsResponseJobsItemLimitsPartRecordsMin = 0;
+
+export const listSlackImportsResponseJobsItemLimitsRecordBytesMin = 0;
+
+export const listSlackImportsResponseJobsItemLimitsRegistrationBatchMin = 0;
+
+export const listSlackImportsResponseJobsItemLimitsSelectedBytesMin = 0;
+
+export const listSlackImportsResponseJobsItemLimitsZipEntriesMin = 0;
+
+export const listSlackImportsResponseJobsItemRevisionMin = 0;
+
+export const listSlackImportsResponseLimitsConversationsMin = 0;
+
+export const listSlackImportsResponseLimitsDatabaseBatchBytesMin = 0;
+
+export const listSlackImportsResponseLimitsDatabaseBatchMessagesMin = 0;
+
+export const listSlackImportsResponseLimitsJsonBytesMin = 0;
+
+export const listSlackImportsResponseLimitsPartBytesMin = 0;
+
+export const listSlackImportsResponseLimitsPartRecordsMin = 0;
+
+export const listSlackImportsResponseLimitsRecordBytesMin = 0;
+
+export const listSlackImportsResponseLimitsRegistrationBatchMin = 0;
+
+export const listSlackImportsResponseLimitsSelectedBytesMin = 0;
+
+export const listSlackImportsResponseLimitsZipEntriesMin = 0;
+
+export const listSlackImportsResponse = zod
+ .object({
+ jobs: zod
+ .array(
+ zod
+ .object({
+ conversations: zod
+ .array(
+ zod
+ .object({
+ archived: zod
+ .boolean()
+ .describe(
+ 'Whether the selected source conversation was archived.'
+ ),
+ channelId: zod
+ .uuid()
+ .nullish()
+ .describe(
+ 'Authorized target only; absent for inaccessible reused targets.'
+ ),
+ counters: zod
+ .object({
+ duplicates: zod
+ .number()
+ .min(
+ listSlackImportsResponseJobsItemConversationsItemCountersDuplicatesMin
+ )
+ .describe('Previously committed source identities.'),
+ imported: zod
+ .number()
+ .min(
+ listSlackImportsResponseJobsItemConversationsItemCountersImportedMin
+ )
+ .describe('Newly persisted messages.'),
+ processed: zod
+ .number()
+ .min(
+ listSlackImportsResponseJobsItemConversationsItemCountersProcessedMin
+ )
+ .describe(
+ 'Source records examined through the committed checkpoint.'
+ ),
+ reactions: zod
+ .number()
+ .min(
+ listSlackImportsResponseJobsItemConversationsItemCountersReactionsMin
+ )
+ .describe('Newly persisted reactions.'),
+ skipped: zod
+ .number()
+ .min(
+ listSlackImportsResponseJobsItemConversationsItemCountersSkippedMin
+ )
+ .describe(
+ 'Unsupported, empty or otherwise deliberately skipped records.'
+ ),
+ })
+ .describe(
+ 'Committed counters, never optimistic browser or uncommitted worker counts.'
+ ),
+ error: zod
+ .union([
+ zod.null(),
+ zod
+ .enum([
+ 'disabled',
+ 'invalid_input',
+ 'limit_exceeded',
+ 'unavailable',
+ 'admin_required',
+ 'source_mismatch',
+ 'conflict',
+ 'upload_mismatch',
+ 'lease_lost',
+ 'retryable',
+ 'internal',
+ ])
+ .describe(
+ 'Public error codes; never carry raw provider errors, keys, emails or source text.'
+ ),
+ ])
+ .optional(),
+ kind: zod
+ .enum([
+ 'public_channel',
+ 'private_channel',
+ 'direct_message',
+ 'group_direct_message',
+ ])
+ .describe(
+ 'Slack conversation kind. Public Slack channels map to Macro Team, never Public.'
+ ),
+ name: zod
+ .string()
+ .describe(
+ "Persisted source display name, never the reused Macro target's name."
+ ),
+ partCount: zod
+ .number()
+ .min(
+ listSlackImportsResponseJobsItemConversationsItemPartCountMin
+ )
+ .nullish()
+ .describe(
+ 'Null until sealed; zero is a valid sealed empty manifest.'
+ ),
+ search: zod
+ .union([
+ zod
+ .object({
+ status: zod.enum(['not_needed']),
+ })
+ .describe('No committed history requires indexing.'),
+ zod
+ .object({
+ status: zod.enum(['pending']),
+ })
+ .describe(
+ 'Committed history requires an outbox-backed scoped submission.'
+ ),
+ zod
+ .object({
+ receiptId: zod
+ .uuid()
+ .describe('Receipt from the search service.'),
+ status: zod.enum(['submitted']),
+ })
+ .describe(
+ 'Search service accepted the request, but has not completed it.'
+ ),
+ zod
+ .object({
+ status: zod.enum(['completed']),
+ })
+ .describe(
+ 'Search service durably published the scope; eventual indexing is separate.'
+ ),
+ zod
+ .object({
+ status: zod.enum(['failed']),
+ })
+ .describe(
+ 'Publication failed; retained history must be retried independently.'
+ ),
+ ])
+ .describe(
+ 'Search publication\/receipt state. Acceptance is not completed publication;\neven completed publication still awaits consumer indexing and refresh.'
+ ),
+ slackChannelId: zod
+ .string()
+ .describe(
+ 'Source Slack conversation ID (C, G or D prefix); unique only within a source.'
+ ),
+ status: zod
+ .enum([
+ 'awaiting_uploads',
+ 'queued',
+ 'importing',
+ 'completed',
+ 'skipped',
+ 'failed',
+ ])
+ .describe('Durable per-conversation lifecycle.'),
+ verifiedParts: zod
+ .number()
+ .min(
+ listSlackImportsResponseJobsItemConversationsItemVerifiedPartsMin
+ )
+ .describe(
+ 'Number of parts whose object identity has been verified.'
+ ),
+ warnings: zod
+ .array(
+ zod
+ .enum([
+ 'creation_time_from_message',
+ 'creation_time_from_job',
+ 'unresolvable_direct_message',
+ 'target_unavailable',
+ 'uploads_incomplete',
+ ])
+ .describe(
+ 'Non-fatal, sanitized explanations displayed in admin progress.'
+ )
+ )
+ .describe('Non-fatal metadata\/skip explanations.'),
+ })
+ .describe(
+ 'Admin-visible progress for one selected conversation.'
+ )
+ )
+ .describe(
+ 'All selected conversations, bounded by the create limit.'
+ ),
+ createdAt: zod.iso
+ .datetime({})
+ .describe(
+ 'Persisted creation time, also the final historical-time fallback.'
+ ),
+ includeMessageHistory: zod
+ .boolean()
+ .describe(
+ 'Immutable history option confirmed when creating this job.'
+ ),
+ jobId: zod
+ .uuid()
+ .describe(
+ 'Import job identity. Generate UUIDv7 in application code.'
+ ),
+ limits: zod
+ .object({
+ conversations: zod
+ .number()
+ .min(listSlackImportsResponseJobsItemLimitsConversationsMin)
+ .describe('Maximum selected conversations.'),
+ databaseBatchBytes: zod
+ .number()
+ .min(
+ listSlackImportsResponseJobsItemLimitsDatabaseBatchBytesMin
+ )
+ .describe('Maximum historical batch payload bytes.'),
+ databaseBatchMessages: zod
+ .number()
+ .min(
+ listSlackImportsResponseJobsItemLimitsDatabaseBatchMessagesMin
+ )
+ .describe('Maximum messages in a historical batch.'),
+ jsonBytes: zod
+ .number()
+ .min(listSlackImportsResponseJobsItemLimitsJsonBytesMin)
+ .describe(
+ 'Maximum users, root metadata or individual day JSON bytes.'
+ ),
+ partBytes: zod
+ .number()
+ .min(listSlackImportsResponseJobsItemLimitsPartBytesMin)
+ .describe('Maximum NDJSON part bytes.'),
+ partRecords: zod
+ .number()
+ .min(listSlackImportsResponseJobsItemLimitsPartRecordsMin)
+ .describe('Maximum records per part.'),
+ recordBytes: zod
+ .number()
+ .min(listSlackImportsResponseJobsItemLimitsRecordBytesMin)
+ .describe(
+ 'Maximum bytes per NDJSON record, including newline.'
+ ),
+ registrationBatch: zod
+ .number()
+ .min(
+ listSlackImportsResponseJobsItemLimitsRegistrationBatchMin
+ )
+ .describe(
+ 'Maximum descriptors or completion identities per call.'
+ ),
+ selectedBytes: zod
+ .number()
+ .min(listSlackImportsResponseJobsItemLimitsSelectedBytesMin)
+ .describe(
+ 'Maximum selected temporary data bytes across all uploads.'
+ ),
+ zipEntries: zod
+ .number()
+ .min(listSlackImportsResponseJobsItemLimitsZipEntriesMin)
+ .describe('Maximum ZIP entries scanned by the browser.'),
+ })
+ .describe(
+ 'Configurable bounds shared with browser staging and enforced again by the worker.'
+ ),
+ registrationClosedAt: zod.iso
+ .datetime({})
+ .nullish()
+ .describe(
+ 'Registration closure time; cancellation also closes registration.'
+ ),
+ revision: zod
+ .number()
+ .min(listSlackImportsResponseJobsItemRevisionMin)
+ .describe(
+ 'Monotonic job revision for polling\/websocket invalidation.'
+ ),
+ source: zod
+ .union([
+ zod
+ .object({
+ kind: zod.enum(['known']),
+ sourceId: zod
+ .string()
+ .describe(
+ 'Known Slack workspace identity (T prefix); an enterprise ID alone is insufficient.'
+ ),
+ })
+ .describe(
+ 'Archive names its Slack workspace; reject a conflicting team binding.'
+ ),
+ zod
+ .object({
+ kind: zod.enum(['confirmed_unknown']),
+ })
+ .describe(
+ "Explicit confirmation that an unidentified archive belongs to this team's source.\nChoosing this variant is affirmative confirmation, not an inferred default."
+ ),
+ ])
+ .describe(
+ 'Source identity supplied when creating a job. One binding per Macro team in v1.'
+ ),
+ status: zod
+ .enum([
+ 'uploading',
+ 'processing',
+ 'completed',
+ 'completed_with_errors',
+ 'failed',
+ 'cancelling',
+ 'cancelled',
+ ])
+ .describe(
+ 'Job lifecycle. Cancellation remains in progress while any lease is active.'
+ ),
+ updatedAt: zod.iso
+ .datetime({})
+ .describe('Last durable lifecycle\/progress update.'),
+ usersVerified: zod
+ .boolean()
+ .describe('Whether users metadata is verified and pinned.'),
+ })
+ .describe(
+ 'Common receipt returned by create, completion, finalize, cancel and progress.'
+ )
+ )
+ .describe('At most 50 job receipts, newest first.'),
+ limits: zod
+ .object({
+ conversations: zod
+ .number()
+ .min(listSlackImportsResponseLimitsConversationsMin)
+ .describe('Maximum selected conversations.'),
+ databaseBatchBytes: zod
+ .number()
+ .min(listSlackImportsResponseLimitsDatabaseBatchBytesMin)
+ .describe('Maximum historical batch payload bytes.'),
+ databaseBatchMessages: zod
+ .number()
+ .min(listSlackImportsResponseLimitsDatabaseBatchMessagesMin)
+ .describe('Maximum messages in a historical batch.'),
+ jsonBytes: zod
+ .number()
+ .min(listSlackImportsResponseLimitsJsonBytesMin)
+ .describe(
+ 'Maximum users, root metadata or individual day JSON bytes.'
+ ),
+ partBytes: zod
+ .number()
+ .min(listSlackImportsResponseLimitsPartBytesMin)
+ .describe('Maximum NDJSON part bytes.'),
+ partRecords: zod
+ .number()
+ .min(listSlackImportsResponseLimitsPartRecordsMin)
+ .describe('Maximum records per part.'),
+ recordBytes: zod
+ .number()
+ .min(listSlackImportsResponseLimitsRecordBytesMin)
+ .describe('Maximum bytes per NDJSON record, including newline.'),
+ registrationBatch: zod
+ .number()
+ .min(listSlackImportsResponseLimitsRegistrationBatchMin)
+ .describe('Maximum descriptors or completion identities per call.'),
+ selectedBytes: zod
+ .number()
+ .min(listSlackImportsResponseLimitsSelectedBytesMin)
+ .describe(
+ 'Maximum selected temporary data bytes across all uploads.'
+ ),
+ zipEntries: zod
+ .number()
+ .min(listSlackImportsResponseLimitsZipEntriesMin)
+ .describe('Maximum ZIP entries scanned by the browser.'),
+ })
+ .describe(
+ 'Configurable bounds shared with browser staging and enforced again by the worker.'
+ ),
+ nextCursor: zod
+ .union([
+ zod.null(),
+ zod
+ .uuid()
+ .describe(
+ 'Import job identity. Generate UUIDv7 in application code.'
+ ),
+ ])
+ .optional(),
+ sourceBinding: zod
+ .union([
+ zod
+ .object({
+ kind: zod.enum(['unbound']),
+ })
+ .describe('No import has bound this team yet.'),
+ zod
+ .object({
+ kind: zod.enum(['confirmed_unknown']),
+ })
+ .describe(
+ 'Legacy\/onboarding or explicitly confirmed unidentified source.'
+ ),
+ zod
+ .object({
+ kind: zod.enum(['known']),
+ sourceId: zod
+ .string()
+ .describe(
+ 'Known Slack workspace identity (T prefix); an enterprise ID alone is insufficient.'
+ ),
+ })
+ .describe('Known Slack workspace; reject mismatches.'),
+ ])
+ .describe(
+ "Team's durable single-source binding, including confirmed unidentified archives."
+ ),
+ })
+ .describe(
+ 'Admin list result, also supplying upload limits before a job exists.'
+ );
+
+/**
+ * @summary Create or replay a job using a scoped idempotency token and full selected metadata.
+Missing creation timestamps and unknown message counts are represented as null.
+ */
+export const createSlackImportBodyConversationsItemMessageCountMin = 0;
+
+export const createSlackImportBodyConversationsMax = 2000;
+
+export const createSlackImportBody = zod
+ .object({
+ conversations: zod
+ .array(
+ zod
+ .object({
+ archived: zod
+ .boolean()
+ .describe(
+ 'Source archived flag (does not silently archive a reused Macro target).'
+ ),
+ createdAt: zod
+ .union([
+ zod.null(),
+ zod
+ .string()
+ .describe(
+ 'Slack time represented exactly as nonnegative Unix microseconds.\nJSON is a string with six fractional digits, never a floating-point number.'
+ ),
+ ])
+ .optional(),
+ creatorId: zod
+ .union([
+ zod.null(),
+ zod
+ .string()
+ .describe(
+ 'Slack member identity (U or W prefix), including USLACKBOT.'
+ ),
+ ])
+ .optional(),
+ folder: zod
+ .string()
+ .describe(
+ 'Single safe source folder\/key segment; never a path or authorization proof.'
+ ),
+ kind: zod
+ .enum([
+ 'public_channel',
+ 'private_channel',
+ 'direct_message',
+ 'group_direct_message',
+ ])
+ .describe(
+ 'Slack conversation kind. Public Slack channels map to Macro Team, never Public.'
+ ),
+ memberIds: zod
+ .array(
+ zod
+ .string()
+ .describe(
+ 'Slack member identity (U or W prefix), including USLACKBOT.'
+ )
+ )
+ .describe(
+ 'Complete source member list, including members unknown to Macro.'
+ ),
+ messageCount: zod
+ .number()
+ .min(createSlackImportBodyConversationsItemMessageCountMin)
+ .nullish()
+ .describe('Advisory only; null means not yet counted, not zero.'),
+ name: zod
+ .string()
+ .describe('Original display name; not a storage key.'),
+ slackChannelId: zod
+ .string()
+ .describe(
+ 'Source Slack conversation ID (C, G or D prefix); unique only within a source.'
+ ),
+ })
+ .describe(
+ 'Full selected conversation metadata, persisted before granting any uploads.'
+ )
+ )
+ .min(1)
+ .max(createSlackImportBodyConversationsMax)
+ .describe(
+ 'Full, unique selected conversations (at most the configured bound).'
+ ),
+ idempotencyToken: zod
+ .uuid()
+ .describe(
+ 'Client-generated create idempotency token, scoped to team and administrator.'
+ ),
+ includeMessageHistory: zod
+ .boolean()
+ .describe(
+ 'Default true at the client; false still requires users and zero-part seals.'
+ ),
+ source: zod
+ .union([
+ zod
+ .object({
+ kind: zod.enum(['known']),
+ sourceId: zod
+ .string()
+ .describe(
+ 'Known Slack workspace identity (T prefix); an enterprise ID alone is insufficient.'
+ ),
+ })
+ .describe(
+ 'Archive names its Slack workspace; reject a conflicting team binding.'
+ ),
+ zod
+ .object({
+ kind: zod.enum(['confirmed_unknown']),
+ })
+ .describe(
+ "Explicit confirmation that an unidentified archive belongs to this team's source.\nChoosing this variant is affirmative confirmation, not an inferred default."
+ ),
+ ])
+ .describe(
+ 'Source identity supplied when creating a job. One binding per Macro team in v1.'
+ ),
+ })
+ .describe('Create command. Team identity is deliberately absent.');
+
+export const createSlackImportResponseConversationsItemCountersDuplicatesMin = 0;
+
+export const createSlackImportResponseConversationsItemCountersImportedMin = 0;
+
+export const createSlackImportResponseConversationsItemCountersProcessedMin = 0;
+
+export const createSlackImportResponseConversationsItemCountersReactionsMin = 0;
+
+export const createSlackImportResponseConversationsItemCountersSkippedMin = 0;
+
+export const createSlackImportResponseConversationsItemPartCountMin = 0;
+
+export const createSlackImportResponseConversationsItemVerifiedPartsMin = 0;
+
+export const createSlackImportResponseLimitsConversationsMin = 0;
+
+export const createSlackImportResponseLimitsDatabaseBatchBytesMin = 0;
+
+export const createSlackImportResponseLimitsDatabaseBatchMessagesMin = 0;
+
+export const createSlackImportResponseLimitsJsonBytesMin = 0;
+
+export const createSlackImportResponseLimitsPartBytesMin = 0;
+
+export const createSlackImportResponseLimitsPartRecordsMin = 0;
+
+export const createSlackImportResponseLimitsRecordBytesMin = 0;
+
+export const createSlackImportResponseLimitsRegistrationBatchMin = 0;
+
+export const createSlackImportResponseLimitsSelectedBytesMin = 0;
+
+export const createSlackImportResponseLimitsZipEntriesMin = 0;
+
+export const createSlackImportResponseRevisionMin = 0;
+
+export const createSlackImportResponse = zod
+ .object({
+ conversations: zod
+ .array(
+ zod
+ .object({
+ archived: zod
+ .boolean()
+ .describe(
+ 'Whether the selected source conversation was archived.'
+ ),
+ channelId: zod
+ .uuid()
+ .nullish()
+ .describe(
+ 'Authorized target only; absent for inaccessible reused targets.'
+ ),
+ counters: zod
+ .object({
+ duplicates: zod
+ .number()
+ .min(
+ createSlackImportResponseConversationsItemCountersDuplicatesMin
+ )
+ .describe('Previously committed source identities.'),
+ imported: zod
+ .number()
+ .min(
+ createSlackImportResponseConversationsItemCountersImportedMin
+ )
+ .describe('Newly persisted messages.'),
+ processed: zod
+ .number()
+ .min(
+ createSlackImportResponseConversationsItemCountersProcessedMin
+ )
+ .describe(
+ 'Source records examined through the committed checkpoint.'
+ ),
+ reactions: zod
+ .number()
+ .min(
+ createSlackImportResponseConversationsItemCountersReactionsMin
+ )
+ .describe('Newly persisted reactions.'),
+ skipped: zod
+ .number()
+ .min(
+ createSlackImportResponseConversationsItemCountersSkippedMin
+ )
+ .describe(
+ 'Unsupported, empty or otherwise deliberately skipped records.'
+ ),
+ })
+ .describe(
+ 'Committed counters, never optimistic browser or uncommitted worker counts.'
+ ),
+ error: zod
+ .union([
+ zod.null(),
+ zod
+ .enum([
+ 'disabled',
+ 'invalid_input',
+ 'limit_exceeded',
+ 'unavailable',
+ 'admin_required',
+ 'source_mismatch',
+ 'conflict',
+ 'upload_mismatch',
+ 'lease_lost',
+ 'retryable',
+ 'internal',
+ ])
+ .describe(
+ 'Public error codes; never carry raw provider errors, keys, emails or source text.'
+ ),
+ ])
+ .optional(),
+ kind: zod
+ .enum([
+ 'public_channel',
+ 'private_channel',
+ 'direct_message',
+ 'group_direct_message',
+ ])
+ .describe(
+ 'Slack conversation kind. Public Slack channels map to Macro Team, never Public.'
+ ),
+ name: zod
+ .string()
+ .describe(
+ "Persisted source display name, never the reused Macro target's name."
+ ),
+ partCount: zod
+ .number()
+ .min(createSlackImportResponseConversationsItemPartCountMin)
+ .nullish()
+ .describe(
+ 'Null until sealed; zero is a valid sealed empty manifest.'
+ ),
+ search: zod
+ .union([
+ zod
+ .object({
+ status: zod.enum(['not_needed']),
+ })
+ .describe('No committed history requires indexing.'),
+ zod
+ .object({
+ status: zod.enum(['pending']),
+ })
+ .describe(
+ 'Committed history requires an outbox-backed scoped submission.'
+ ),
+ zod
+ .object({
+ receiptId: zod
+ .uuid()
+ .describe('Receipt from the search service.'),
+ status: zod.enum(['submitted']),
+ })
+ .describe(
+ 'Search service accepted the request, but has not completed it.'
+ ),
+ zod
+ .object({
+ status: zod.enum(['completed']),
+ })
+ .describe(
+ 'Search service durably published the scope; eventual indexing is separate.'
+ ),
+ zod
+ .object({
+ status: zod.enum(['failed']),
+ })
+ .describe(
+ 'Publication failed; retained history must be retried independently.'
+ ),
+ ])
+ .describe(
+ 'Search publication\/receipt state. Acceptance is not completed publication;\neven completed publication still awaits consumer indexing and refresh.'
+ ),
+ slackChannelId: zod
+ .string()
+ .describe(
+ 'Source Slack conversation ID (C, G or D prefix); unique only within a source.'
+ ),
+ status: zod
+ .enum([
+ 'awaiting_uploads',
+ 'queued',
+ 'importing',
+ 'completed',
+ 'skipped',
+ 'failed',
+ ])
+ .describe('Durable per-conversation lifecycle.'),
+ verifiedParts: zod
+ .number()
+ .min(createSlackImportResponseConversationsItemVerifiedPartsMin)
+ .describe(
+ 'Number of parts whose object identity has been verified.'
+ ),
+ warnings: zod
+ .array(
+ zod
+ .enum([
+ 'creation_time_from_message',
+ 'creation_time_from_job',
+ 'unresolvable_direct_message',
+ 'target_unavailable',
+ 'uploads_incomplete',
+ ])
+ .describe(
+ 'Non-fatal, sanitized explanations displayed in admin progress.'
+ )
+ )
+ .describe('Non-fatal metadata\/skip explanations.'),
+ })
+ .describe('Admin-visible progress for one selected conversation.')
+ )
+ .describe('All selected conversations, bounded by the create limit.'),
+ createdAt: zod.iso
+ .datetime({})
+ .describe(
+ 'Persisted creation time, also the final historical-time fallback.'
+ ),
+ includeMessageHistory: zod
+ .boolean()
+ .describe('Immutable history option confirmed when creating this job.'),
+ jobId: zod
+ .uuid()
+ .describe('Import job identity. Generate UUIDv7 in application code.'),
+ limits: zod
+ .object({
+ conversations: zod
+ .number()
+ .min(createSlackImportResponseLimitsConversationsMin)
+ .describe('Maximum selected conversations.'),
+ databaseBatchBytes: zod
+ .number()
+ .min(createSlackImportResponseLimitsDatabaseBatchBytesMin)
+ .describe('Maximum historical batch payload bytes.'),
+ databaseBatchMessages: zod
+ .number()
+ .min(createSlackImportResponseLimitsDatabaseBatchMessagesMin)
+ .describe('Maximum messages in a historical batch.'),
+ jsonBytes: zod
+ .number()
+ .min(createSlackImportResponseLimitsJsonBytesMin)
+ .describe(
+ 'Maximum users, root metadata or individual day JSON bytes.'
+ ),
+ partBytes: zod
+ .number()
+ .min(createSlackImportResponseLimitsPartBytesMin)
+ .describe('Maximum NDJSON part bytes.'),
+ partRecords: zod
+ .number()
+ .min(createSlackImportResponseLimitsPartRecordsMin)
+ .describe('Maximum records per part.'),
+ recordBytes: zod
+ .number()
+ .min(createSlackImportResponseLimitsRecordBytesMin)
+ .describe('Maximum bytes per NDJSON record, including newline.'),
+ registrationBatch: zod
+ .number()
+ .min(createSlackImportResponseLimitsRegistrationBatchMin)
+ .describe('Maximum descriptors or completion identities per call.'),
+ selectedBytes: zod
+ .number()
+ .min(createSlackImportResponseLimitsSelectedBytesMin)
+ .describe(
+ 'Maximum selected temporary data bytes across all uploads.'
+ ),
+ zipEntries: zod
+ .number()
+ .min(createSlackImportResponseLimitsZipEntriesMin)
+ .describe('Maximum ZIP entries scanned by the browser.'),
+ })
+ .describe(
+ 'Configurable bounds shared with browser staging and enforced again by the worker.'
+ ),
+ registrationClosedAt: zod.iso
+ .datetime({})
+ .nullish()
+ .describe(
+ 'Registration closure time; cancellation also closes registration.'
+ ),
+ revision: zod
+ .number()
+ .min(createSlackImportResponseRevisionMin)
+ .describe('Monotonic job revision for polling\/websocket invalidation.'),
+ source: zod
+ .union([
+ zod
+ .object({
+ kind: zod.enum(['known']),
+ sourceId: zod
+ .string()
+ .describe(
+ 'Known Slack workspace identity (T prefix); an enterprise ID alone is insufficient.'
+ ),
+ })
+ .describe(
+ 'Archive names its Slack workspace; reject a conflicting team binding.'
+ ),
+ zod
+ .object({
+ kind: zod.enum(['confirmed_unknown']),
+ })
+ .describe(
+ "Explicit confirmation that an unidentified archive belongs to this team's source.\nChoosing this variant is affirmative confirmation, not an inferred default."
+ ),
+ ])
+ .describe(
+ 'Source identity supplied when creating a job. One binding per Macro team in v1.'
+ ),
+ status: zod
+ .enum([
+ 'uploading',
+ 'processing',
+ 'completed',
+ 'completed_with_errors',
+ 'failed',
+ 'cancelling',
+ 'cancelled',
+ ])
+ .describe(
+ 'Job lifecycle. Cancellation remains in progress while any lease is active.'
+ ),
+ updatedAt: zod.iso
+ .datetime({})
+ .describe('Last durable lifecycle\/progress update.'),
+ usersVerified: zod
+ .boolean()
+ .describe('Whether users metadata is verified and pinned.'),
+ })
+ .describe(
+ 'Common receipt returned by create, completion, finalize, cancel and progress.'
+ );
+
+/**
+ * @summary Read sanitized progress; foreign jobs are indistinguishable from missing jobs.
+ */
+export const getSlackImportParams = zod.object({
+ job_id: zod.uuid(),
+});
+
+export const getSlackImportResponseConversationsItemCountersDuplicatesMin = 0;
+
+export const getSlackImportResponseConversationsItemCountersImportedMin = 0;
+
+export const getSlackImportResponseConversationsItemCountersProcessedMin = 0;
+
+export const getSlackImportResponseConversationsItemCountersReactionsMin = 0;
+
+export const getSlackImportResponseConversationsItemCountersSkippedMin = 0;
+
+export const getSlackImportResponseConversationsItemPartCountMin = 0;
+
+export const getSlackImportResponseConversationsItemVerifiedPartsMin = 0;
+
+export const getSlackImportResponseLimitsConversationsMin = 0;
+
+export const getSlackImportResponseLimitsDatabaseBatchBytesMin = 0;
+
+export const getSlackImportResponseLimitsDatabaseBatchMessagesMin = 0;
+
+export const getSlackImportResponseLimitsJsonBytesMin = 0;
+
+export const getSlackImportResponseLimitsPartBytesMin = 0;
+
+export const getSlackImportResponseLimitsPartRecordsMin = 0;
+
+export const getSlackImportResponseLimitsRecordBytesMin = 0;
+
+export const getSlackImportResponseLimitsRegistrationBatchMin = 0;
+
+export const getSlackImportResponseLimitsSelectedBytesMin = 0;
+
+export const getSlackImportResponseLimitsZipEntriesMin = 0;
+
+export const getSlackImportResponseRevisionMin = 0;
+
+export const getSlackImportResponse = zod
+ .object({
+ conversations: zod
+ .array(
+ zod
+ .object({
+ archived: zod
+ .boolean()
+ .describe(
+ 'Whether the selected source conversation was archived.'
+ ),
+ channelId: zod
+ .uuid()
+ .nullish()
+ .describe(
+ 'Authorized target only; absent for inaccessible reused targets.'
+ ),
+ counters: zod
+ .object({
+ duplicates: zod
+ .number()
+ .min(
+ getSlackImportResponseConversationsItemCountersDuplicatesMin
+ )
+ .describe('Previously committed source identities.'),
+ imported: zod
+ .number()
+ .min(
+ getSlackImportResponseConversationsItemCountersImportedMin
+ )
+ .describe('Newly persisted messages.'),
+ processed: zod
+ .number()
+ .min(
+ getSlackImportResponseConversationsItemCountersProcessedMin
+ )
+ .describe(
+ 'Source records examined through the committed checkpoint.'
+ ),
+ reactions: zod
+ .number()
+ .min(
+ getSlackImportResponseConversationsItemCountersReactionsMin
+ )
+ .describe('Newly persisted reactions.'),
+ skipped: zod
+ .number()
+ .min(
+ getSlackImportResponseConversationsItemCountersSkippedMin
+ )
+ .describe(
+ 'Unsupported, empty or otherwise deliberately skipped records.'
+ ),
+ })
+ .describe(
+ 'Committed counters, never optimistic browser or uncommitted worker counts.'
+ ),
+ error: zod
+ .union([
+ zod.null(),
+ zod
+ .enum([
+ 'disabled',
+ 'invalid_input',
+ 'limit_exceeded',
+ 'unavailable',
+ 'admin_required',
+ 'source_mismatch',
+ 'conflict',
+ 'upload_mismatch',
+ 'lease_lost',
+ 'retryable',
+ 'internal',
+ ])
+ .describe(
+ 'Public error codes; never carry raw provider errors, keys, emails or source text.'
+ ),
+ ])
+ .optional(),
+ kind: zod
+ .enum([
+ 'public_channel',
+ 'private_channel',
+ 'direct_message',
+ 'group_direct_message',
+ ])
+ .describe(
+ 'Slack conversation kind. Public Slack channels map to Macro Team, never Public.'
+ ),
+ name: zod
+ .string()
+ .describe(
+ "Persisted source display name, never the reused Macro target's name."
+ ),
+ partCount: zod
+ .number()
+ .min(getSlackImportResponseConversationsItemPartCountMin)
+ .nullish()
+ .describe(
+ 'Null until sealed; zero is a valid sealed empty manifest.'
+ ),
+ search: zod
+ .union([
+ zod
+ .object({
+ status: zod.enum(['not_needed']),
+ })
+ .describe('No committed history requires indexing.'),
+ zod
+ .object({
+ status: zod.enum(['pending']),
+ })
+ .describe(
+ 'Committed history requires an outbox-backed scoped submission.'
+ ),
+ zod
+ .object({
+ receiptId: zod
+ .uuid()
+ .describe('Receipt from the search service.'),
+ status: zod.enum(['submitted']),
+ })
+ .describe(
+ 'Search service accepted the request, but has not completed it.'
+ ),
+ zod
+ .object({
+ status: zod.enum(['completed']),
+ })
+ .describe(
+ 'Search service durably published the scope; eventual indexing is separate.'
+ ),
+ zod
+ .object({
+ status: zod.enum(['failed']),
+ })
+ .describe(
+ 'Publication failed; retained history must be retried independently.'
+ ),
+ ])
+ .describe(
+ 'Search publication\/receipt state. Acceptance is not completed publication;\neven completed publication still awaits consumer indexing and refresh.'
+ ),
+ slackChannelId: zod
+ .string()
+ .describe(
+ 'Source Slack conversation ID (C, G or D prefix); unique only within a source.'
+ ),
+ status: zod
+ .enum([
+ 'awaiting_uploads',
+ 'queued',
+ 'importing',
+ 'completed',
+ 'skipped',
+ 'failed',
+ ])
+ .describe('Durable per-conversation lifecycle.'),
+ verifiedParts: zod
+ .number()
+ .min(getSlackImportResponseConversationsItemVerifiedPartsMin)
+ .describe(
+ 'Number of parts whose object identity has been verified.'
+ ),
+ warnings: zod
+ .array(
+ zod
+ .enum([
+ 'creation_time_from_message',
+ 'creation_time_from_job',
+ 'unresolvable_direct_message',
+ 'target_unavailable',
+ 'uploads_incomplete',
+ ])
+ .describe(
+ 'Non-fatal, sanitized explanations displayed in admin progress.'
+ )
+ )
+ .describe('Non-fatal metadata\/skip explanations.'),
+ })
+ .describe('Admin-visible progress for one selected conversation.')
+ )
+ .describe('All selected conversations, bounded by the create limit.'),
+ createdAt: zod.iso
+ .datetime({})
+ .describe(
+ 'Persisted creation time, also the final historical-time fallback.'
+ ),
+ includeMessageHistory: zod
+ .boolean()
+ .describe('Immutable history option confirmed when creating this job.'),
+ jobId: zod
+ .uuid()
+ .describe('Import job identity. Generate UUIDv7 in application code.'),
+ limits: zod
+ .object({
+ conversations: zod
+ .number()
+ .min(getSlackImportResponseLimitsConversationsMin)
+ .describe('Maximum selected conversations.'),
+ databaseBatchBytes: zod
+ .number()
+ .min(getSlackImportResponseLimitsDatabaseBatchBytesMin)
+ .describe('Maximum historical batch payload bytes.'),
+ databaseBatchMessages: zod
+ .number()
+ .min(getSlackImportResponseLimitsDatabaseBatchMessagesMin)
+ .describe('Maximum messages in a historical batch.'),
+ jsonBytes: zod
+ .number()
+ .min(getSlackImportResponseLimitsJsonBytesMin)
+ .describe(
+ 'Maximum users, root metadata or individual day JSON bytes.'
+ ),
+ partBytes: zod
+ .number()
+ .min(getSlackImportResponseLimitsPartBytesMin)
+ .describe('Maximum NDJSON part bytes.'),
+ partRecords: zod
+ .number()
+ .min(getSlackImportResponseLimitsPartRecordsMin)
+ .describe('Maximum records per part.'),
+ recordBytes: zod
+ .number()
+ .min(getSlackImportResponseLimitsRecordBytesMin)
+ .describe('Maximum bytes per NDJSON record, including newline.'),
+ registrationBatch: zod
+ .number()
+ .min(getSlackImportResponseLimitsRegistrationBatchMin)
+ .describe('Maximum descriptors or completion identities per call.'),
+ selectedBytes: zod
+ .number()
+ .min(getSlackImportResponseLimitsSelectedBytesMin)
+ .describe(
+ 'Maximum selected temporary data bytes across all uploads.'
+ ),
+ zipEntries: zod
+ .number()
+ .min(getSlackImportResponseLimitsZipEntriesMin)
+ .describe('Maximum ZIP entries scanned by the browser.'),
+ })
+ .describe(
+ 'Configurable bounds shared with browser staging and enforced again by the worker.'
+ ),
+ registrationClosedAt: zod.iso
+ .datetime({})
+ .nullish()
+ .describe(
+ 'Registration closure time; cancellation also closes registration.'
+ ),
+ revision: zod
+ .number()
+ .min(getSlackImportResponseRevisionMin)
+ .describe('Monotonic job revision for polling\/websocket invalidation.'),
+ source: zod
+ .union([
+ zod
+ .object({
+ kind: zod.enum(['known']),
+ sourceId: zod
+ .string()
+ .describe(
+ 'Known Slack workspace identity (T prefix); an enterprise ID alone is insufficient.'
+ ),
+ })
+ .describe(
+ 'Archive names its Slack workspace; reject a conflicting team binding.'
+ ),
+ zod
+ .object({
+ kind: zod.enum(['confirmed_unknown']),
+ })
+ .describe(
+ "Explicit confirmation that an unidentified archive belongs to this team's source.\nChoosing this variant is affirmative confirmation, not an inferred default."
+ ),
+ ])
+ .describe(
+ 'Source identity supplied when creating a job. One binding per Macro team in v1.'
+ ),
+ status: zod
+ .enum([
+ 'uploading',
+ 'processing',
+ 'completed',
+ 'completed_with_errors',
+ 'failed',
+ 'cancelling',
+ 'cancelled',
+ ])
+ .describe(
+ 'Job lifecycle. Cancellation remains in progress while any lease is active.'
+ ),
+ updatedAt: zod.iso
+ .datetime({})
+ .describe('Last durable lifecycle\/progress update.'),
+ usersVerified: zod
+ .boolean()
+ .describe('Whether users metadata is verified and pinned.'),
+ })
+ .describe(
+ 'Common receipt returned by create, completion, finalize, cancel and progress.'
+ );
+
+/**
+ * @summary Stop unclaimed work. Active leases may settle; cancellation does not roll back history.
+ */
+export const cancelSlackImportParams = zod.object({
+ job_id: zod.uuid(),
+});
+
+export const cancelSlackImportResponseConversationsItemCountersDuplicatesMin = 0;
+
+export const cancelSlackImportResponseConversationsItemCountersImportedMin = 0;
+
+export const cancelSlackImportResponseConversationsItemCountersProcessedMin = 0;
+
+export const cancelSlackImportResponseConversationsItemCountersReactionsMin = 0;
+
+export const cancelSlackImportResponseConversationsItemCountersSkippedMin = 0;
+
+export const cancelSlackImportResponseConversationsItemPartCountMin = 0;
+
+export const cancelSlackImportResponseConversationsItemVerifiedPartsMin = 0;
+
+export const cancelSlackImportResponseLimitsConversationsMin = 0;
+
+export const cancelSlackImportResponseLimitsDatabaseBatchBytesMin = 0;
+
+export const cancelSlackImportResponseLimitsDatabaseBatchMessagesMin = 0;
+
+export const cancelSlackImportResponseLimitsJsonBytesMin = 0;
+
+export const cancelSlackImportResponseLimitsPartBytesMin = 0;
+
+export const cancelSlackImportResponseLimitsPartRecordsMin = 0;
+
+export const cancelSlackImportResponseLimitsRecordBytesMin = 0;
+
+export const cancelSlackImportResponseLimitsRegistrationBatchMin = 0;
+
+export const cancelSlackImportResponseLimitsSelectedBytesMin = 0;
+
+export const cancelSlackImportResponseLimitsZipEntriesMin = 0;
+
+export const cancelSlackImportResponseRevisionMin = 0;
+
+export const cancelSlackImportResponse = zod
+ .object({
+ conversations: zod
+ .array(
+ zod
+ .object({
+ archived: zod
+ .boolean()
+ .describe(
+ 'Whether the selected source conversation was archived.'
+ ),
+ channelId: zod
+ .uuid()
+ .nullish()
+ .describe(
+ 'Authorized target only; absent for inaccessible reused targets.'
+ ),
+ counters: zod
+ .object({
+ duplicates: zod
+ .number()
+ .min(
+ cancelSlackImportResponseConversationsItemCountersDuplicatesMin
+ )
+ .describe('Previously committed source identities.'),
+ imported: zod
+ .number()
+ .min(
+ cancelSlackImportResponseConversationsItemCountersImportedMin
+ )
+ .describe('Newly persisted messages.'),
+ processed: zod
+ .number()
+ .min(
+ cancelSlackImportResponseConversationsItemCountersProcessedMin
+ )
+ .describe(
+ 'Source records examined through the committed checkpoint.'
+ ),
+ reactions: zod
+ .number()
+ .min(
+ cancelSlackImportResponseConversationsItemCountersReactionsMin
+ )
+ .describe('Newly persisted reactions.'),
+ skipped: zod
+ .number()
+ .min(
+ cancelSlackImportResponseConversationsItemCountersSkippedMin
+ )
+ .describe(
+ 'Unsupported, empty or otherwise deliberately skipped records.'
+ ),
+ })
+ .describe(
+ 'Committed counters, never optimistic browser or uncommitted worker counts.'
+ ),
+ error: zod
+ .union([
+ zod.null(),
+ zod
+ .enum([
+ 'disabled',
+ 'invalid_input',
+ 'limit_exceeded',
+ 'unavailable',
+ 'admin_required',
+ 'source_mismatch',
+ 'conflict',
+ 'upload_mismatch',
+ 'lease_lost',
+ 'retryable',
+ 'internal',
+ ])
+ .describe(
+ 'Public error codes; never carry raw provider errors, keys, emails or source text.'
+ ),
+ ])
+ .optional(),
+ kind: zod
+ .enum([
+ 'public_channel',
+ 'private_channel',
+ 'direct_message',
+ 'group_direct_message',
+ ])
+ .describe(
+ 'Slack conversation kind. Public Slack channels map to Macro Team, never Public.'
+ ),
+ name: zod
+ .string()
+ .describe(
+ "Persisted source display name, never the reused Macro target's name."
+ ),
+ partCount: zod
+ .number()
+ .min(cancelSlackImportResponseConversationsItemPartCountMin)
+ .nullish()
+ .describe(
+ 'Null until sealed; zero is a valid sealed empty manifest.'
+ ),
+ search: zod
+ .union([
+ zod
+ .object({
+ status: zod.enum(['not_needed']),
+ })
+ .describe('No committed history requires indexing.'),
+ zod
+ .object({
+ status: zod.enum(['pending']),
+ })
+ .describe(
+ 'Committed history requires an outbox-backed scoped submission.'
+ ),
+ zod
+ .object({
+ receiptId: zod
+ .uuid()
+ .describe('Receipt from the search service.'),
+ status: zod.enum(['submitted']),
+ })
+ .describe(
+ 'Search service accepted the request, but has not completed it.'
+ ),
+ zod
+ .object({
+ status: zod.enum(['completed']),
+ })
+ .describe(
+ 'Search service durably published the scope; eventual indexing is separate.'
+ ),
+ zod
+ .object({
+ status: zod.enum(['failed']),
+ })
+ .describe(
+ 'Publication failed; retained history must be retried independently.'
+ ),
+ ])
+ .describe(
+ 'Search publication\/receipt state. Acceptance is not completed publication;\neven completed publication still awaits consumer indexing and refresh.'
+ ),
+ slackChannelId: zod
+ .string()
+ .describe(
+ 'Source Slack conversation ID (C, G or D prefix); unique only within a source.'
+ ),
+ status: zod
+ .enum([
+ 'awaiting_uploads',
+ 'queued',
+ 'importing',
+ 'completed',
+ 'skipped',
+ 'failed',
+ ])
+ .describe('Durable per-conversation lifecycle.'),
+ verifiedParts: zod
+ .number()
+ .min(cancelSlackImportResponseConversationsItemVerifiedPartsMin)
+ .describe(
+ 'Number of parts whose object identity has been verified.'
+ ),
+ warnings: zod
+ .array(
+ zod
+ .enum([
+ 'creation_time_from_message',
+ 'creation_time_from_job',
+ 'unresolvable_direct_message',
+ 'target_unavailable',
+ 'uploads_incomplete',
+ ])
+ .describe(
+ 'Non-fatal, sanitized explanations displayed in admin progress.'
+ )
+ )
+ .describe('Non-fatal metadata\/skip explanations.'),
+ })
+ .describe('Admin-visible progress for one selected conversation.')
+ )
+ .describe('All selected conversations, bounded by the create limit.'),
+ createdAt: zod.iso
+ .datetime({})
+ .describe(
+ 'Persisted creation time, also the final historical-time fallback.'
+ ),
+ includeMessageHistory: zod
+ .boolean()
+ .describe('Immutable history option confirmed when creating this job.'),
+ jobId: zod
+ .uuid()
+ .describe('Import job identity. Generate UUIDv7 in application code.'),
+ limits: zod
+ .object({
+ conversations: zod
+ .number()
+ .min(cancelSlackImportResponseLimitsConversationsMin)
+ .describe('Maximum selected conversations.'),
+ databaseBatchBytes: zod
+ .number()
+ .min(cancelSlackImportResponseLimitsDatabaseBatchBytesMin)
+ .describe('Maximum historical batch payload bytes.'),
+ databaseBatchMessages: zod
+ .number()
+ .min(cancelSlackImportResponseLimitsDatabaseBatchMessagesMin)
+ .describe('Maximum messages in a historical batch.'),
+ jsonBytes: zod
+ .number()
+ .min(cancelSlackImportResponseLimitsJsonBytesMin)
+ .describe(
+ 'Maximum users, root metadata or individual day JSON bytes.'
+ ),
+ partBytes: zod
+ .number()
+ .min(cancelSlackImportResponseLimitsPartBytesMin)
+ .describe('Maximum NDJSON part bytes.'),
+ partRecords: zod
+ .number()
+ .min(cancelSlackImportResponseLimitsPartRecordsMin)
+ .describe('Maximum records per part.'),
+ recordBytes: zod
+ .number()
+ .min(cancelSlackImportResponseLimitsRecordBytesMin)
+ .describe('Maximum bytes per NDJSON record, including newline.'),
+ registrationBatch: zod
+ .number()
+ .min(cancelSlackImportResponseLimitsRegistrationBatchMin)
+ .describe('Maximum descriptors or completion identities per call.'),
+ selectedBytes: zod
+ .number()
+ .min(cancelSlackImportResponseLimitsSelectedBytesMin)
+ .describe(
+ 'Maximum selected temporary data bytes across all uploads.'
+ ),
+ zipEntries: zod
+ .number()
+ .min(cancelSlackImportResponseLimitsZipEntriesMin)
+ .describe('Maximum ZIP entries scanned by the browser.'),
+ })
+ .describe(
+ 'Configurable bounds shared with browser staging and enforced again by the worker.'
+ ),
+ registrationClosedAt: zod.iso
+ .datetime({})
+ .nullish()
+ .describe(
+ 'Registration closure time; cancellation also closes registration.'
+ ),
+ revision: zod
+ .number()
+ .min(cancelSlackImportResponseRevisionMin)
+ .describe('Monotonic job revision for polling\/websocket invalidation.'),
+ source: zod
+ .union([
+ zod
+ .object({
+ kind: zod.enum(['known']),
+ sourceId: zod
+ .string()
+ .describe(
+ 'Known Slack workspace identity (T prefix); an enterprise ID alone is insufficient.'
+ ),
+ })
+ .describe(
+ 'Archive names its Slack workspace; reject a conflicting team binding.'
+ ),
+ zod
+ .object({
+ kind: zod.enum(['confirmed_unknown']),
+ })
+ .describe(
+ "Explicit confirmation that an unidentified archive belongs to this team's source.\nChoosing this variant is affirmative confirmation, not an inferred default."
+ ),
+ ])
+ .describe(
+ 'Source identity supplied when creating a job. One binding per Macro team in v1.'
+ ),
+ status: zod
+ .enum([
+ 'uploading',
+ 'processing',
+ 'completed',
+ 'completed_with_errors',
+ 'failed',
+ 'cancelling',
+ 'cancelled',
+ ])
+ .describe(
+ 'Job lifecycle. Cancellation remains in progress while any lease is active.'
+ ),
+ updatedAt: zod.iso
+ .datetime({})
+ .describe('Last durable lifecycle\/progress update.'),
+ usersVerified: zod
+ .boolean()
+ .describe('Whether users metadata is verified and pinned.'),
+ })
+ .describe(
+ 'Common receipt returned by create, completion, finalize, cancel and progress.'
+ );
+
+/**
+ * @summary Close registration idempotently; never-ready conversations are skipped, not queued work.
+ */
+export const finalizeSlackImportParams = zod.object({
+ job_id: zod.uuid(),
+});
+
+export const finalizeSlackImportResponseConversationsItemCountersDuplicatesMin = 0;
+
+export const finalizeSlackImportResponseConversationsItemCountersImportedMin = 0;
+
+export const finalizeSlackImportResponseConversationsItemCountersProcessedMin = 0;
+
+export const finalizeSlackImportResponseConversationsItemCountersReactionsMin = 0;
+
+export const finalizeSlackImportResponseConversationsItemCountersSkippedMin = 0;
+
+export const finalizeSlackImportResponseConversationsItemPartCountMin = 0;
+
+export const finalizeSlackImportResponseConversationsItemVerifiedPartsMin = 0;
+
+export const finalizeSlackImportResponseLimitsConversationsMin = 0;
+
+export const finalizeSlackImportResponseLimitsDatabaseBatchBytesMin = 0;
+
+export const finalizeSlackImportResponseLimitsDatabaseBatchMessagesMin = 0;
+
+export const finalizeSlackImportResponseLimitsJsonBytesMin = 0;
+
+export const finalizeSlackImportResponseLimitsPartBytesMin = 0;
+
+export const finalizeSlackImportResponseLimitsPartRecordsMin = 0;
+
+export const finalizeSlackImportResponseLimitsRecordBytesMin = 0;
+
+export const finalizeSlackImportResponseLimitsRegistrationBatchMin = 0;
+
+export const finalizeSlackImportResponseLimitsSelectedBytesMin = 0;
+
+export const finalizeSlackImportResponseLimitsZipEntriesMin = 0;
+
+export const finalizeSlackImportResponseRevisionMin = 0;
+
+export const finalizeSlackImportResponse = zod
+ .object({
+ conversations: zod
+ .array(
+ zod
+ .object({
+ archived: zod
+ .boolean()
+ .describe(
+ 'Whether the selected source conversation was archived.'
+ ),
+ channelId: zod
+ .uuid()
+ .nullish()
+ .describe(
+ 'Authorized target only; absent for inaccessible reused targets.'
+ ),
+ counters: zod
+ .object({
+ duplicates: zod
+ .number()
+ .min(
+ finalizeSlackImportResponseConversationsItemCountersDuplicatesMin
+ )
+ .describe('Previously committed source identities.'),
+ imported: zod
+ .number()
+ .min(
+ finalizeSlackImportResponseConversationsItemCountersImportedMin
+ )
+ .describe('Newly persisted messages.'),
+ processed: zod
+ .number()
+ .min(
+ finalizeSlackImportResponseConversationsItemCountersProcessedMin
+ )
+ .describe(
+ 'Source records examined through the committed checkpoint.'
+ ),
+ reactions: zod
+ .number()
+ .min(
+ finalizeSlackImportResponseConversationsItemCountersReactionsMin
+ )
+ .describe('Newly persisted reactions.'),
+ skipped: zod
+ .number()
+ .min(
+ finalizeSlackImportResponseConversationsItemCountersSkippedMin
+ )
+ .describe(
+ 'Unsupported, empty or otherwise deliberately skipped records.'
+ ),
+ })
+ .describe(
+ 'Committed counters, never optimistic browser or uncommitted worker counts.'
+ ),
+ error: zod
+ .union([
+ zod.null(),
+ zod
+ .enum([
+ 'disabled',
+ 'invalid_input',
+ 'limit_exceeded',
+ 'unavailable',
+ 'admin_required',
+ 'source_mismatch',
+ 'conflict',
+ 'upload_mismatch',
+ 'lease_lost',
+ 'retryable',
+ 'internal',
+ ])
+ .describe(
+ 'Public error codes; never carry raw provider errors, keys, emails or source text.'
+ ),
+ ])
+ .optional(),
+ kind: zod
+ .enum([
+ 'public_channel',
+ 'private_channel',
+ 'direct_message',
+ 'group_direct_message',
+ ])
+ .describe(
+ 'Slack conversation kind. Public Slack channels map to Macro Team, never Public.'
+ ),
+ name: zod
+ .string()
+ .describe(
+ "Persisted source display name, never the reused Macro target's name."
+ ),
+ partCount: zod
+ .number()
+ .min(finalizeSlackImportResponseConversationsItemPartCountMin)
+ .nullish()
+ .describe(
+ 'Null until sealed; zero is a valid sealed empty manifest.'
+ ),
+ search: zod
+ .union([
+ zod
+ .object({
+ status: zod.enum(['not_needed']),
+ })
+ .describe('No committed history requires indexing.'),
+ zod
+ .object({
+ status: zod.enum(['pending']),
+ })
+ .describe(
+ 'Committed history requires an outbox-backed scoped submission.'
+ ),
+ zod
+ .object({
+ receiptId: zod
+ .uuid()
+ .describe('Receipt from the search service.'),
+ status: zod.enum(['submitted']),
+ })
+ .describe(
+ 'Search service accepted the request, but has not completed it.'
+ ),
+ zod
+ .object({
+ status: zod.enum(['completed']),
+ })
+ .describe(
+ 'Search service durably published the scope; eventual indexing is separate.'
+ ),
+ zod
+ .object({
+ status: zod.enum(['failed']),
+ })
+ .describe(
+ 'Publication failed; retained history must be retried independently.'
+ ),
+ ])
+ .describe(
+ 'Search publication\/receipt state. Acceptance is not completed publication;\neven completed publication still awaits consumer indexing and refresh.'
+ ),
+ slackChannelId: zod
+ .string()
+ .describe(
+ 'Source Slack conversation ID (C, G or D prefix); unique only within a source.'
+ ),
+ status: zod
+ .enum([
+ 'awaiting_uploads',
+ 'queued',
+ 'importing',
+ 'completed',
+ 'skipped',
+ 'failed',
+ ])
+ .describe('Durable per-conversation lifecycle.'),
+ verifiedParts: zod
+ .number()
+ .min(finalizeSlackImportResponseConversationsItemVerifiedPartsMin)
+ .describe(
+ 'Number of parts whose object identity has been verified.'
+ ),
+ warnings: zod
+ .array(
+ zod
+ .enum([
+ 'creation_time_from_message',
+ 'creation_time_from_job',
+ 'unresolvable_direct_message',
+ 'target_unavailable',
+ 'uploads_incomplete',
+ ])
+ .describe(
+ 'Non-fatal, sanitized explanations displayed in admin progress.'
+ )
+ )
+ .describe('Non-fatal metadata\/skip explanations.'),
+ })
+ .describe('Admin-visible progress for one selected conversation.')
+ )
+ .describe('All selected conversations, bounded by the create limit.'),
+ createdAt: zod.iso
+ .datetime({})
+ .describe(
+ 'Persisted creation time, also the final historical-time fallback.'
+ ),
+ includeMessageHistory: zod
+ .boolean()
+ .describe('Immutable history option confirmed when creating this job.'),
+ jobId: zod
+ .uuid()
+ .describe('Import job identity. Generate UUIDv7 in application code.'),
+ limits: zod
+ .object({
+ conversations: zod
+ .number()
+ .min(finalizeSlackImportResponseLimitsConversationsMin)
+ .describe('Maximum selected conversations.'),
+ databaseBatchBytes: zod
+ .number()
+ .min(finalizeSlackImportResponseLimitsDatabaseBatchBytesMin)
+ .describe('Maximum historical batch payload bytes.'),
+ databaseBatchMessages: zod
+ .number()
+ .min(finalizeSlackImportResponseLimitsDatabaseBatchMessagesMin)
+ .describe('Maximum messages in a historical batch.'),
+ jsonBytes: zod
+ .number()
+ .min(finalizeSlackImportResponseLimitsJsonBytesMin)
+ .describe(
+ 'Maximum users, root metadata or individual day JSON bytes.'
+ ),
+ partBytes: zod
+ .number()
+ .min(finalizeSlackImportResponseLimitsPartBytesMin)
+ .describe('Maximum NDJSON part bytes.'),
+ partRecords: zod
+ .number()
+ .min(finalizeSlackImportResponseLimitsPartRecordsMin)
+ .describe('Maximum records per part.'),
+ recordBytes: zod
+ .number()
+ .min(finalizeSlackImportResponseLimitsRecordBytesMin)
+ .describe('Maximum bytes per NDJSON record, including newline.'),
+ registrationBatch: zod
+ .number()
+ .min(finalizeSlackImportResponseLimitsRegistrationBatchMin)
+ .describe('Maximum descriptors or completion identities per call.'),
+ selectedBytes: zod
+ .number()
+ .min(finalizeSlackImportResponseLimitsSelectedBytesMin)
+ .describe(
+ 'Maximum selected temporary data bytes across all uploads.'
+ ),
+ zipEntries: zod
+ .number()
+ .min(finalizeSlackImportResponseLimitsZipEntriesMin)
+ .describe('Maximum ZIP entries scanned by the browser.'),
+ })
+ .describe(
+ 'Configurable bounds shared with browser staging and enforced again by the worker.'
+ ),
+ registrationClosedAt: zod.iso
+ .datetime({})
+ .nullish()
+ .describe(
+ 'Registration closure time; cancellation also closes registration.'
+ ),
+ revision: zod
+ .number()
+ .min(finalizeSlackImportResponseRevisionMin)
+ .describe('Monotonic job revision for polling\/websocket invalidation.'),
+ source: zod
+ .union([
+ zod
+ .object({
+ kind: zod.enum(['known']),
+ sourceId: zod
+ .string()
+ .describe(
+ 'Known Slack workspace identity (T prefix); an enterprise ID alone is insufficient.'
+ ),
+ })
+ .describe(
+ 'Archive names its Slack workspace; reject a conflicting team binding.'
+ ),
+ zod
+ .object({
+ kind: zod.enum(['confirmed_unknown']),
+ })
+ .describe(
+ "Explicit confirmation that an unidentified archive belongs to this team's source.\nChoosing this variant is affirmative confirmation, not an inferred default."
+ ),
+ ])
+ .describe(
+ 'Source identity supplied when creating a job. One binding per Macro team in v1.'
+ ),
+ status: zod
+ .enum([
+ 'uploading',
+ 'processing',
+ 'completed',
+ 'completed_with_errors',
+ 'failed',
+ 'cancelling',
+ 'cancelled',
+ ])
+ .describe(
+ 'Job lifecycle. Cancellation remains in progress while any lease is active.'
+ ),
+ updatedAt: zod.iso
+ .datetime({})
+ .describe('Last durable lifecycle\/progress update.'),
+ usersVerified: zod
+ .boolean()
+ .describe('Whether users metadata is verified and pinned.'),
+ })
+ .describe(
+ 'Common receipt returned by create, completion, finalize, cancel and progress.'
+ );
+
+/**
+ * @summary Issue short-lived, checksum/length-bound, create-only PUT grants. Do not log or persist URLs.
+Retry a PUT returning 412 by completing/verifying the existing object, not overwriting it.
+ */
+export const registerSlackImportUploadsParams = zod.object({
+ job_id: zod.uuid(),
+});
+
+export const registerSlackImportUploadsBodyDescriptorsItemByteLengthMin = 0;
+
+export const registerSlackImportUploadsBodyDescriptorsItemRecordCountMin = 0;
+
+export const registerSlackImportUploadsBodyDescriptorsItemUploadPartIndexMin = 0;
+
+export const registerSlackImportUploadsBodyDescriptorsMax = 50;
+
+export const registerSlackImportUploadsBody = zod
+ .object({
+ descriptors: zod
+ .array(
+ zod
+ .object({
+ byteLength: zod
+ .number()
+ .min(registerSlackImportUploadsBodyDescriptorsItemByteLengthMin)
+ .describe('Exact byte length including delimiters.'),
+ recordCount: zod
+ .number()
+ .min(registerSlackImportUploadsBodyDescriptorsItemRecordCountMin)
+ .nullish()
+ .describe(
+ 'Exact NDJSON record count; null for the users JSON payload.'
+ ),
+ sha256: zod
+ .string()
+ .describe(
+ 'SHA-256 encoded as exactly 64 lowercase hexadecimal characters.'
+ ),
+ upload: zod
+ .union([
+ zod
+ .object({
+ kind: zod.enum(['users']),
+ })
+ .describe(
+ 'One normalized users payload from users.json and\/or org_users.json.'
+ ),
+ zod
+ .object({
+ kind: zod.enum(['conversation_part']),
+ partIndex: zod
+ .number()
+ .min(
+ registerSlackImportUploadsBodyDescriptorsItemUploadPartIndexMin
+ )
+ .describe(
+ 'Zero-based part index; sealed manifests must be contiguous.'
+ ),
+ slackChannelId: zod
+ .string()
+ .describe(
+ 'Source Slack conversation ID (C, G or D prefix); unique only within a source.'
+ ),
+ })
+ .describe('One NDJSON part in a selected conversation.'),
+ ])
+ .describe(
+ "Identity in a job's persisted manifest. No user-supplied object keys."
+ ),
+ })
+ .describe(
+ 'Immutable expected object properties, registered before signing an upload.'
+ )
+ )
+ .max(registerSlackImportUploadsBodyDescriptorsMax)
+ .describe('At most 50 descriptors; no duplicate identities in a call.'),
+ })
+ .describe(
+ "Batched registration, also used to renew an unchanged descriptor's grant."
+ );
+
+export const registerSlackImportUploadsResponseDescriptorByteLengthMin = 0;
+
+export const registerSlackImportUploadsResponseDescriptorRecordCountMin = 0;
+
+export const registerSlackImportUploadsResponseDescriptorUploadPartIndexMin = 0;
+
+export const registerSlackImportUploadsResponseItem = zod
+ .object({
+ descriptor: zod
+ .object({
+ byteLength: zod
+ .number()
+ .min(registerSlackImportUploadsResponseDescriptorByteLengthMin)
+ .describe('Exact byte length including delimiters.'),
+ recordCount: zod
+ .number()
+ .min(registerSlackImportUploadsResponseDescriptorRecordCountMin)
+ .nullish()
+ .describe(
+ 'Exact NDJSON record count; null for the users JSON payload.'
+ ),
+ sha256: zod
+ .string()
+ .describe(
+ 'SHA-256 encoded as exactly 64 lowercase hexadecimal characters.'
+ ),
+ upload: zod
+ .union([
+ zod
+ .object({
+ kind: zod.enum(['users']),
+ })
+ .describe(
+ 'One normalized users payload from users.json and\/or org_users.json.'
+ ),
+ zod
+ .object({
+ kind: zod.enum(['conversation_part']),
+ partIndex: zod
+ .number()
+ .min(
+ registerSlackImportUploadsResponseDescriptorUploadPartIndexMin
+ )
+ .describe(
+ 'Zero-based part index; sealed manifests must be contiguous.'
+ ),
+ slackChannelId: zod
+ .string()
+ .describe(
+ 'Source Slack conversation ID (C, G or D prefix); unique only within a source.'
+ ),
+ })
+ .describe('One NDJSON part in a selected conversation.'),
+ ])
+ .describe(
+ "Identity in a job's persisted manifest. No user-supplied object keys."
+ ),
+ })
+ .describe(
+ 'Immutable expected object properties, registered before signing an upload.'
+ ),
+ expiresAt: zod.iso
+ .datetime({})
+ .describe(
+ 'Grant expiry; callers renew by re-registering the identical descriptor.'
+ ),
+ requiredHeaders: zod
+ .record(zod.string(), zod.string())
+ .describe(
+ 'Required signed upload headers (including checksum, content type and create-only\ncondition). Browser-forbidden Content-Length is derived from the exact Blob bytes.'
+ ),
+ url: zod
+ .string()
+ .describe('Short-lived signed destination; must not be logged.'),
+ })
+ .describe(
+ 'Signed upload permission returned just before PUT; never persisted as an identity.'
+ );
+export const registerSlackImportUploadsResponse = zod.array(
+ registerSlackImportUploadsResponseItem
+);
+
+/**
+ * @summary Verify storage checksums and lengths for registered objects. A seal fixes the ENTIRE
+descriptor set (contiguous indices, sizes and hashes), not just this completion batch.
+Empty uploads with a zero-part seal support shape-only or empty-history imports.
+Work becomes ready only after verified users metadata, a seal and every required part.
+ */
+export const completeSlackImportUploadsParams = zod.object({
+ job_id: zod.uuid(),
+});
+
+export const completeSlackImportUploadsBodySealPartCountMin = 0;
+
+export const completeSlackImportUploadsBodyUploadsItemPartIndexMin = 0;
+
+export const completeSlackImportUploadsBodyUploadsMax = 50;
+
+export const completeSlackImportUploadsBody = zod
+ .object({
+ seal: zod
+ .union([
+ zod.null(),
+ zod
+ .object({
+ manifestSha256: zod
+ .string()
+ .describe(
+ 'SHA-256 encoded as exactly 64 lowercase hexadecimal characters.'
+ ),
+ partCount: zod
+ .number()
+ .min(completeSlackImportUploadsBodySealPartCountMin)
+ .describe(
+ 'Exact number of registered parts, starting at index zero.'
+ ),
+ slackChannelId: zod
+ .string()
+ .describe(
+ 'Source Slack conversation ID (C, G or D prefix); unique only within a source.'
+ ),
+ })
+ .describe(
+ 'Complete immutable expected part set for one conversation, including zero parts.'
+ ),
+ ])
+ .optional(),
+ uploads: zod
+ .array(
+ zod
+ .union([
+ zod
+ .object({
+ kind: zod.enum(['users']),
+ })
+ .describe(
+ 'One normalized users payload from users.json and\/or org_users.json.'
+ ),
+ zod
+ .object({
+ kind: zod.enum(['conversation_part']),
+ partIndex: zod
+ .number()
+ .min(completeSlackImportUploadsBodyUploadsItemPartIndexMin)
+ .describe(
+ 'Zero-based part index; sealed manifests must be contiguous.'
+ ),
+ slackChannelId: zod
+ .string()
+ .describe(
+ 'Source Slack conversation ID (C, G or D prefix); unique only within a source.'
+ ),
+ })
+ .describe('One NDJSON part in a selected conversation.'),
+ ])
+ .describe(
+ "Identity in a job's persisted manifest. No user-supplied object keys."
+ )
+ )
+ .max(completeSlackImportUploadsBodyUploadsMax)
+ .describe(
+ 'Registered identities to verify against storage (at most 50).'
+ ),
+ })
+ .describe(
+ 'Explicit upload completion; a seal can be submitted separately with an empty list.'
+ );
+
+export const completeSlackImportUploadsResponseConversationsItemCountersDuplicatesMin = 0;
+
+export const completeSlackImportUploadsResponseConversationsItemCountersImportedMin = 0;
+
+export const completeSlackImportUploadsResponseConversationsItemCountersProcessedMin = 0;
+
+export const completeSlackImportUploadsResponseConversationsItemCountersReactionsMin = 0;
+
+export const completeSlackImportUploadsResponseConversationsItemCountersSkippedMin = 0;
+
+export const completeSlackImportUploadsResponseConversationsItemPartCountMin = 0;
+
+export const completeSlackImportUploadsResponseConversationsItemVerifiedPartsMin = 0;
+
+export const completeSlackImportUploadsResponseLimitsConversationsMin = 0;
+
+export const completeSlackImportUploadsResponseLimitsDatabaseBatchBytesMin = 0;
+
+export const completeSlackImportUploadsResponseLimitsDatabaseBatchMessagesMin = 0;
+
+export const completeSlackImportUploadsResponseLimitsJsonBytesMin = 0;
+
+export const completeSlackImportUploadsResponseLimitsPartBytesMin = 0;
+
+export const completeSlackImportUploadsResponseLimitsPartRecordsMin = 0;
+
+export const completeSlackImportUploadsResponseLimitsRecordBytesMin = 0;
+
+export const completeSlackImportUploadsResponseLimitsRegistrationBatchMin = 0;
+
+export const completeSlackImportUploadsResponseLimitsSelectedBytesMin = 0;
+
+export const completeSlackImportUploadsResponseLimitsZipEntriesMin = 0;
+
+export const completeSlackImportUploadsResponseRevisionMin = 0;
+
+export const completeSlackImportUploadsResponse = zod
+ .object({
+ conversations: zod
+ .array(
+ zod
+ .object({
+ archived: zod
+ .boolean()
+ .describe(
+ 'Whether the selected source conversation was archived.'
+ ),
+ channelId: zod
+ .uuid()
+ .nullish()
+ .describe(
+ 'Authorized target only; absent for inaccessible reused targets.'
+ ),
+ counters: zod
+ .object({
+ duplicates: zod
+ .number()
+ .min(
+ completeSlackImportUploadsResponseConversationsItemCountersDuplicatesMin
+ )
+ .describe('Previously committed source identities.'),
+ imported: zod
+ .number()
+ .min(
+ completeSlackImportUploadsResponseConversationsItemCountersImportedMin
+ )
+ .describe('Newly persisted messages.'),
+ processed: zod
+ .number()
+ .min(
+ completeSlackImportUploadsResponseConversationsItemCountersProcessedMin
+ )
+ .describe(
+ 'Source records examined through the committed checkpoint.'
+ ),
+ reactions: zod
+ .number()
+ .min(
+ completeSlackImportUploadsResponseConversationsItemCountersReactionsMin
+ )
+ .describe('Newly persisted reactions.'),
+ skipped: zod
+ .number()
+ .min(
+ completeSlackImportUploadsResponseConversationsItemCountersSkippedMin
+ )
+ .describe(
+ 'Unsupported, empty or otherwise deliberately skipped records.'
+ ),
+ })
+ .describe(
+ 'Committed counters, never optimistic browser or uncommitted worker counts.'
+ ),
+ error: zod
+ .union([
+ zod.null(),
+ zod
+ .enum([
+ 'disabled',
+ 'invalid_input',
+ 'limit_exceeded',
+ 'unavailable',
+ 'admin_required',
+ 'source_mismatch',
+ 'conflict',
+ 'upload_mismatch',
+ 'lease_lost',
+ 'retryable',
+ 'internal',
+ ])
+ .describe(
+ 'Public error codes; never carry raw provider errors, keys, emails or source text.'
+ ),
+ ])
+ .optional(),
+ kind: zod
+ .enum([
+ 'public_channel',
+ 'private_channel',
+ 'direct_message',
+ 'group_direct_message',
+ ])
+ .describe(
+ 'Slack conversation kind. Public Slack channels map to Macro Team, never Public.'
+ ),
+ name: zod
+ .string()
+ .describe(
+ "Persisted source display name, never the reused Macro target's name."
+ ),
+ partCount: zod
+ .number()
+ .min(
+ completeSlackImportUploadsResponseConversationsItemPartCountMin
+ )
+ .nullish()
+ .describe(
+ 'Null until sealed; zero is a valid sealed empty manifest.'
+ ),
+ search: zod
+ .union([
+ zod
+ .object({
+ status: zod.enum(['not_needed']),
+ })
+ .describe('No committed history requires indexing.'),
+ zod
+ .object({
+ status: zod.enum(['pending']),
+ })
+ .describe(
+ 'Committed history requires an outbox-backed scoped submission.'
+ ),
+ zod
+ .object({
+ receiptId: zod
+ .uuid()
+ .describe('Receipt from the search service.'),
+ status: zod.enum(['submitted']),
+ })
+ .describe(
+ 'Search service accepted the request, but has not completed it.'
+ ),
+ zod
+ .object({
+ status: zod.enum(['completed']),
+ })
+ .describe(
+ 'Search service durably published the scope; eventual indexing is separate.'
+ ),
+ zod
+ .object({
+ status: zod.enum(['failed']),
+ })
+ .describe(
+ 'Publication failed; retained history must be retried independently.'
+ ),
+ ])
+ .describe(
+ 'Search publication\/receipt state. Acceptance is not completed publication;\neven completed publication still awaits consumer indexing and refresh.'
+ ),
+ slackChannelId: zod
+ .string()
+ .describe(
+ 'Source Slack conversation ID (C, G or D prefix); unique only within a source.'
+ ),
+ status: zod
+ .enum([
+ 'awaiting_uploads',
+ 'queued',
+ 'importing',
+ 'completed',
+ 'skipped',
+ 'failed',
+ ])
+ .describe('Durable per-conversation lifecycle.'),
+ verifiedParts: zod
+ .number()
+ .min(
+ completeSlackImportUploadsResponseConversationsItemVerifiedPartsMin
+ )
+ .describe(
+ 'Number of parts whose object identity has been verified.'
+ ),
+ warnings: zod
+ .array(
+ zod
+ .enum([
+ 'creation_time_from_message',
+ 'creation_time_from_job',
+ 'unresolvable_direct_message',
+ 'target_unavailable',
+ 'uploads_incomplete',
+ ])
+ .describe(
+ 'Non-fatal, sanitized explanations displayed in admin progress.'
+ )
+ )
+ .describe('Non-fatal metadata\/skip explanations.'),
+ })
+ .describe('Admin-visible progress for one selected conversation.')
+ )
+ .describe('All selected conversations, bounded by the create limit.'),
+ createdAt: zod.iso
+ .datetime({})
+ .describe(
+ 'Persisted creation time, also the final historical-time fallback.'
+ ),
+ includeMessageHistory: zod
+ .boolean()
+ .describe('Immutable history option confirmed when creating this job.'),
+ jobId: zod
+ .uuid()
+ .describe('Import job identity. Generate UUIDv7 in application code.'),
+ limits: zod
+ .object({
+ conversations: zod
+ .number()
+ .min(completeSlackImportUploadsResponseLimitsConversationsMin)
+ .describe('Maximum selected conversations.'),
+ databaseBatchBytes: zod
+ .number()
+ .min(completeSlackImportUploadsResponseLimitsDatabaseBatchBytesMin)
+ .describe('Maximum historical batch payload bytes.'),
+ databaseBatchMessages: zod
+ .number()
+ .min(completeSlackImportUploadsResponseLimitsDatabaseBatchMessagesMin)
+ .describe('Maximum messages in a historical batch.'),
+ jsonBytes: zod
+ .number()
+ .min(completeSlackImportUploadsResponseLimitsJsonBytesMin)
+ .describe(
+ 'Maximum users, root metadata or individual day JSON bytes.'
+ ),
+ partBytes: zod
+ .number()
+ .min(completeSlackImportUploadsResponseLimitsPartBytesMin)
+ .describe('Maximum NDJSON part bytes.'),
+ partRecords: zod
+ .number()
+ .min(completeSlackImportUploadsResponseLimitsPartRecordsMin)
+ .describe('Maximum records per part.'),
+ recordBytes: zod
+ .number()
+ .min(completeSlackImportUploadsResponseLimitsRecordBytesMin)
+ .describe('Maximum bytes per NDJSON record, including newline.'),
+ registrationBatch: zod
+ .number()
+ .min(completeSlackImportUploadsResponseLimitsRegistrationBatchMin)
+ .describe('Maximum descriptors or completion identities per call.'),
+ selectedBytes: zod
+ .number()
+ .min(completeSlackImportUploadsResponseLimitsSelectedBytesMin)
+ .describe(
+ 'Maximum selected temporary data bytes across all uploads.'
+ ),
+ zipEntries: zod
+ .number()
+ .min(completeSlackImportUploadsResponseLimitsZipEntriesMin)
+ .describe('Maximum ZIP entries scanned by the browser.'),
+ })
+ .describe(
+ 'Configurable bounds shared with browser staging and enforced again by the worker.'
+ ),
+ registrationClosedAt: zod.iso
+ .datetime({})
+ .nullish()
+ .describe(
+ 'Registration closure time; cancellation also closes registration.'
+ ),
+ revision: zod
+ .number()
+ .min(completeSlackImportUploadsResponseRevisionMin)
+ .describe('Monotonic job revision for polling\/websocket invalidation.'),
+ source: zod
+ .union([
+ zod
+ .object({
+ kind: zod.enum(['known']),
+ sourceId: zod
+ .string()
+ .describe(
+ 'Known Slack workspace identity (T prefix); an enterprise ID alone is insufficient.'
+ ),
+ })
+ .describe(
+ 'Archive names its Slack workspace; reject a conflicting team binding.'
+ ),
+ zod
+ .object({
+ kind: zod.enum(['confirmed_unknown']),
+ })
+ .describe(
+ "Explicit confirmation that an unidentified archive belongs to this team's source.\nChoosing this variant is affirmative confirmation, not an inferred default."
+ ),
+ ])
+ .describe(
+ 'Source identity supplied when creating a job. One binding per Macro team in v1.'
+ ),
+ status: zod
+ .enum([
+ 'uploading',
+ 'processing',
+ 'completed',
+ 'completed_with_errors',
+ 'failed',
+ 'cancelling',
+ 'cancelled',
+ ])
+ .describe(
+ 'Job lifecycle. Cancellation remains in progress while any lease is active.'
+ ),
+ updatedAt: zod.iso
+ .datetime({})
+ .describe('Last durable lifecycle\/progress update.'),
+ usersVerified: zod
+ .boolean()
+ .describe('Whether users metadata is verified and pinned.'),
+ })
+ .describe(
+ 'Common receipt returned by create, completion, finalize, cancel and progress.'
+ );
+
export const bulkWakeupSyncServiceDocumentsBody = zod.object({
document_ids: zod.array(zod.string()),
});
diff --git a/apps/web/src/lib/service-clients/service-storage/openapi.json b/apps/web/src/lib/service-clients/service-storage/openapi.json
index 73a22bb2b4..ad3f56f920 100644
--- a/apps/web/src/lib/service-clients/service-storage/openapi.json
+++ b/apps/web/src/lib/service-clients/service-storage/openapi.json
@@ -13352,6 +13352,275 @@
}
}
},
+ "/slack/imports": {
+ "get": {
+ "tags": ["slack"],
+ "summary": "List this administrator's team receipts, effective server limits and source binding.",
+ "operationId": "list_slack_imports",
+ "parameters": [
+ {
+ "name": "before",
+ "in": "query",
+ "required": false,
+ "schema": {
+ "$ref": "#/components/schemas/JobId"
+ }
+ }
+ ],
+ "responses": {
+ "200": {
+ "description": "",
+ "content": {
+ "application/json": {
+ "schema": {
+ "$ref": "#/components/schemas/ImportPage"
+ }
+ }
+ }
+ }
+ }
+ },
+ "post": {
+ "tags": ["slack"],
+ "summary": "Create or replay a job using a scoped idempotency token and full selected metadata.\nMissing creation timestamps and unknown message counts are represented as null.",
+ "operationId": "create_slack_import",
+ "requestBody": {
+ "content": {
+ "application/json": {
+ "schema": {
+ "$ref": "#/components/schemas/SlackCreateRequest"
+ }
+ }
+ },
+ "required": true
+ },
+ "responses": {
+ "200": {
+ "description": "",
+ "content": {
+ "application/json": {
+ "schema": {
+ "$ref": "#/components/schemas/ImportProgress"
+ }
+ }
+ }
+ },
+ "401": {
+ "description": "Authentication or team administrator access required"
+ },
+ "403": {
+ "description": "Team administrator required"
+ },
+ "413": {
+ "description": "Metadata limit exceeded"
+ },
+ "503": {
+ "description": "Slack imports disabled or temporarily unavailable"
+ }
+ }
+ }
+ },
+ "/slack/imports/{job_id}": {
+ "get": {
+ "tags": ["slack"],
+ "summary": "Read sanitized progress; foreign jobs are indistinguishable from missing jobs.",
+ "operationId": "get_slack_import",
+ "parameters": [
+ {
+ "name": "job_id",
+ "in": "path",
+ "required": true,
+ "schema": {
+ "$ref": "#/components/schemas/JobId"
+ }
+ }
+ ],
+ "responses": {
+ "200": {
+ "description": "",
+ "content": {
+ "application/json": {
+ "schema": {
+ "$ref": "#/components/schemas/ImportProgress"
+ }
+ }
+ }
+ },
+ "404": {
+ "description": "Unknown or inaccessible job"
+ }
+ }
+ }
+ },
+ "/slack/imports/{job_id}/cancel": {
+ "post": {
+ "tags": ["slack"],
+ "summary": "Stop unclaimed work. Active leases may settle; cancellation does not roll back history.",
+ "operationId": "cancel_slack_import",
+ "parameters": [
+ {
+ "name": "job_id",
+ "in": "path",
+ "required": true,
+ "schema": {
+ "$ref": "#/components/schemas/JobId"
+ }
+ }
+ ],
+ "responses": {
+ "200": {
+ "description": "",
+ "content": {
+ "application/json": {
+ "schema": {
+ "$ref": "#/components/schemas/ImportProgress"
+ }
+ }
+ }
+ },
+ "404": {
+ "description": "Unknown or inaccessible job"
+ }
+ }
+ }
+ },
+ "/slack/imports/{job_id}/finalize": {
+ "post": {
+ "tags": ["slack"],
+ "summary": "Close registration idempotently; never-ready conversations are skipped, not queued work.",
+ "operationId": "finalize_slack_import",
+ "parameters": [
+ {
+ "name": "job_id",
+ "in": "path",
+ "required": true,
+ "schema": {
+ "$ref": "#/components/schemas/JobId"
+ }
+ }
+ ],
+ "responses": {
+ "200": {
+ "description": "",
+ "content": {
+ "application/json": {
+ "schema": {
+ "$ref": "#/components/schemas/ImportProgress"
+ }
+ }
+ }
+ },
+ "404": {
+ "description": "Unknown or inaccessible job"
+ }
+ }
+ }
+ },
+ "/slack/imports/{job_id}/uploads": {
+ "post": {
+ "tags": ["slack"],
+ "summary": "Issue short-lived, checksum/length-bound, create-only PUT grants. Do not log or persist URLs.\nRetry a PUT returning 412 by completing/verifying the existing object, not overwriting it.",
+ "operationId": "register_slack_import_uploads",
+ "parameters": [
+ {
+ "name": "job_id",
+ "in": "path",
+ "required": true,
+ "schema": {
+ "$ref": "#/components/schemas/JobId"
+ }
+ }
+ ],
+ "requestBody": {
+ "content": {
+ "application/json": {
+ "schema": {
+ "$ref": "#/components/schemas/SlackRegisterRequest"
+ }
+ }
+ },
+ "required": true
+ },
+ "responses": {
+ "200": {
+ "description": "",
+ "content": {
+ "application/json": {
+ "schema": {
+ "type": "array",
+ "items": {
+ "$ref": "#/components/schemas/UploadGrant"
+ }
+ }
+ }
+ }
+ },
+ "404": {
+ "description": "Unknown or inaccessible job"
+ },
+ "413": {
+ "description": "Body limit exceeded"
+ },
+ "503": {
+ "description": "Slack imports disabled"
+ }
+ }
+ }
+ },
+ "/slack/imports/{job_id}/uploads/complete": {
+ "post": {
+ "tags": ["slack"],
+ "summary": "Verify storage checksums and lengths for registered objects. A seal fixes the ENTIRE\ndescriptor set (contiguous indices, sizes and hashes), not just this completion batch.\nEmpty uploads with a zero-part seal support shape-only or empty-history imports.\nWork becomes ready only after verified users metadata, a seal and every required part.",
+ "operationId": "complete_slack_import_uploads",
+ "parameters": [
+ {
+ "name": "job_id",
+ "in": "path",
+ "required": true,
+ "schema": {
+ "$ref": "#/components/schemas/JobId"
+ }
+ }
+ ],
+ "requestBody": {
+ "content": {
+ "application/json": {
+ "schema": {
+ "$ref": "#/components/schemas/SlackCompleteRequest"
+ }
+ }
+ },
+ "required": true
+ },
+ "responses": {
+ "200": {
+ "description": "",
+ "content": {
+ "application/json": {
+ "schema": {
+ "$ref": "#/components/schemas/ImportProgress"
+ }
+ }
+ }
+ },
+ "404": {
+ "description": "Unknown or inaccessible job"
+ },
+ "409": {
+ "description": "Immutable manifest conflict"
+ },
+ "413": {
+ "description": "Body limit exceeded"
+ },
+ "422": {
+ "description": "Object verification failed"
+ },
+ "503": {
+ "description": "Slack imports disabled"
+ }
+ }
+ }
+ },
"/sync_service/wakeup": {
"post": {
"tags": ["sync_service"],
@@ -18056,40 +18325,265 @@
"description": "An accepted command was refused before it could start a runtime turn.",
"required": ["identity", "action_id", "failure"],
"properties": {
- "action_id": {
- "$ref": "#/components/schemas/AgentActionId",
- "description": "The command that will not execute."
+ "action_id": {
+ "$ref": "#/components/schemas/AgentActionId",
+ "description": "The command that will not execute."
+ },
+ "actor": {
+ "oneOf": [
+ {
+ "type": "null"
+ },
+ {
+ "$ref": "#/components/schemas/MacroUserIdStr",
+ "description": "Who submitted the command, if acting on a user's behalf."
+ }
+ ]
+ },
+ "announcement_message_id": {
+ "type": ["string", "null"],
+ "format": "uuid",
+ "description": "The command's thread announcement, when one was created."
+ },
+ "failure": {
+ "$ref": "#/components/schemas/CommandFailure",
+ "description": "Safe details for clients and downstream consumers."
+ },
+ "identity": {
+ "$ref": "#/components/schemas/SessionIdentity",
+ "description": "The session."
+ }
+ }
+ },
+ "CompleteUploads": {
+ "type": "object",
+ "description": "Explicit upload completion; a seal can be submitted separately with an empty list.",
+ "required": ["uploads"],
+ "properties": {
+ "seal": {
+ "oneOf": [
+ {
+ "type": "null"
+ },
+ {
+ "$ref": "#/components/schemas/ConversationSeal",
+ "description": "Optional immutable expected part set; identical retries are idempotent."
+ }
+ ]
+ },
+ "uploads": {
+ "type": "array",
+ "items": {
+ "$ref": "#/components/schemas/UploadId"
+ },
+ "description": "Registered identities to verify against storage (at most 50).",
+ "maxItems": 50
+ }
+ },
+ "additionalProperties": false
+ },
+ "ConferenceProvider": {
+ "type": "string",
+ "description": "The conferencing system backing an event's join URL.\n\nMacro generates only Google Meet conferences, so this distinguishes one it\ncreated from a third party's — Zoom and friends arriving as `addOn`\nconference data, or a legacy classic Hangout. Clients use it to label the\nconference and to tell whether the Meet toggle reflects a Macro-managed\nconference.\n\nIt does not gate mutation. An explicit request replaces or detaches any\nconference, third-party included, exactly as deleting the event would;\nwhat protects a conference is that omitting the field leaves it untouched,\nso an unrelated edit never disturbs it.",
+ "enum": ["google_meet", "other"]
+ },
+ "ConversationId": {
+ "type": "string",
+ "description": "Source Slack conversation ID (C, G or D prefix); unique only within a source."
+ },
+ "ConversationKind": {
+ "type": "string",
+ "description": "Slack conversation kind. Public Slack channels map to Macro Team, never Public.",
+ "enum": [
+ "public_channel",
+ "private_channel",
+ "direct_message",
+ "group_direct_message"
+ ]
+ },
+ "ConversationMetadata": {
+ "type": "object",
+ "description": "Full selected conversation metadata, persisted before granting any uploads.",
+ "required": [
+ "slackChannelId",
+ "kind",
+ "name",
+ "folder",
+ "memberIds",
+ "archived"
+ ],
+ "properties": {
+ "archived": {
+ "type": "boolean",
+ "description": "Source archived flag (does not silently archive a reused Macro target)."
+ },
+ "createdAt": {
+ "oneOf": [
+ {
+ "type": "null"
+ },
+ {
+ "$ref": "#/components/schemas/SlackTimestamp",
+ "description": "Missing time is resolved once from earliest message or persisted job time."
+ }
+ ]
+ },
+ "creatorId": {
+ "oneOf": [
+ {
+ "type": "null"
+ },
+ {
+ "$ref": "#/components/schemas/SlackUserId",
+ "description": "Source creator when available."
+ }
+ ]
+ },
+ "folder": {
+ "$ref": "#/components/schemas/KeySegment",
+ "description": "Single archive folder name, not a backend object key."
+ },
+ "kind": {
+ "$ref": "#/components/schemas/ConversationKind",
+ "description": "Source conversation kind."
+ },
+ "memberIds": {
+ "type": "array",
+ "items": {
+ "$ref": "#/components/schemas/SlackUserId"
+ },
+ "description": "Complete source member list, including members unknown to Macro."
+ },
+ "messageCount": {
+ "type": ["integer", "null"],
+ "format": "int64",
+ "description": "Advisory only; null means not yet counted, not zero.",
+ "minimum": 0
+ },
+ "name": {
+ "type": "string",
+ "description": "Original display name; not a storage key."
+ },
+ "slackChannelId": {
+ "$ref": "#/components/schemas/ConversationId",
+ "description": "Stable Slack identity."
+ }
+ },
+ "additionalProperties": false
+ },
+ "ConversationProgress": {
+ "type": "object",
+ "description": "Admin-visible progress for one selected conversation.",
+ "required": [
+ "slackChannelId",
+ "name",
+ "kind",
+ "archived",
+ "status",
+ "verifiedParts",
+ "counters",
+ "search",
+ "warnings"
+ ],
+ "properties": {
+ "archived": {
+ "type": "boolean",
+ "description": "Whether the selected source conversation was archived."
},
- "actor": {
+ "channelId": {
+ "type": ["string", "null"],
+ "format": "uuid",
+ "description": "Authorized target only; absent for inaccessible reused targets."
+ },
+ "counters": {
+ "$ref": "#/components/schemas/ImportCounters",
+ "description": "Durably committed record counts."
+ },
+ "error": {
"oneOf": [
{
"type": "null"
},
{
- "$ref": "#/components/schemas/MacroUserIdStr",
- "description": "Who submitted the command, if acting on a user's behalf."
+ "$ref": "#/components/schemas/ImportError",
+ "description": "Sanitized terminal or retry diagnostic."
}
]
},
- "announcement_message_id": {
- "type": ["string", "null"],
- "format": "uuid",
- "description": "The command's thread announcement, when one was created."
+ "kind": {
+ "$ref": "#/components/schemas/ConversationKind",
+ "description": "Persisted source kind."
},
- "failure": {
- "$ref": "#/components/schemas/CommandFailure",
- "description": "Safe details for clients and downstream consumers."
+ "name": {
+ "type": "string",
+ "description": "Persisted source display name, never the reused Macro target's name."
},
- "identity": {
- "$ref": "#/components/schemas/SessionIdentity",
- "description": "The session."
+ "partCount": {
+ "type": ["integer", "null"],
+ "format": "int32",
+ "description": "Null until sealed; zero is a valid sealed empty manifest.",
+ "minimum": 0
+ },
+ "search": {
+ "$ref": "#/components/schemas/SearchState",
+ "description": "Independent indexing state."
+ },
+ "slackChannelId": {
+ "$ref": "#/components/schemas/ConversationId",
+ "description": "Source identity (always visible to the importing team's administrator)."
+ },
+ "status": {
+ "$ref": "#/components/schemas/ConversationStatus",
+ "description": "Durable state."
+ },
+ "verifiedParts": {
+ "type": "integer",
+ "format": "int32",
+ "description": "Number of parts whose object identity has been verified.",
+ "minimum": 0
+ },
+ "warnings": {
+ "type": "array",
+ "items": {
+ "$ref": "#/components/schemas/ImportWarning"
+ },
+ "description": "Non-fatal metadata/skip explanations."
}
}
},
- "ConferenceProvider": {
+ "ConversationSeal": {
+ "type": "object",
+ "description": "Complete immutable expected part set for one conversation, including zero parts.",
+ "required": ["slackChannelId", "partCount", "manifestSha256"],
+ "properties": {
+ "manifestSha256": {
+ "$ref": "#/components/schemas/Sha256Digest",
+ "description": "SHA-256 of canonical descriptor lines; zero parts hash the empty byte string."
+ },
+ "partCount": {
+ "type": "integer",
+ "format": "int32",
+ "description": "Exact number of registered parts, starting at index zero.",
+ "minimum": 0
+ },
+ "slackChannelId": {
+ "$ref": "#/components/schemas/ConversationId",
+ "description": "Selected source conversation."
+ }
+ },
+ "additionalProperties": false
+ },
+ "ConversationStatus": {
"type": "string",
- "description": "The conferencing system backing an event's join URL.\n\nMacro generates only Google Meet conferences, so this distinguishes one it\ncreated from a third party's — Zoom and friends arriving as `addOn`\nconference data, or a legacy classic Hangout. Clients use it to label the\nconference and to tell whether the Meet toggle reflects a Macro-managed\nconference.\n\nIt does not gate mutation. An explicit request replaces or detaches any\nconference, third-party included, exactly as deleting the event would;\nwhat protects a conference is that omitting the field leaves it untouched,\nso an unrelated edit never disturbs it.",
- "enum": ["google_meet", "other"]
+ "description": "Durable per-conversation lifecycle.",
+ "enum": [
+ "awaiting_uploads",
+ "queued",
+ "importing",
+ "completed",
+ "skipped",
+ "failed"
+ ]
},
"CopyDocumentQueryParams": {
"type": "object",
@@ -18649,6 +19143,40 @@
}
}
},
+ "CreateImport": {
+ "type": "object",
+ "description": "Create command. Team identity is deliberately absent.",
+ "required": [
+ "idempotencyToken",
+ "source",
+ "includeMessageHistory",
+ "conversations"
+ ],
+ "properties": {
+ "conversations": {
+ "type": "array",
+ "items": {
+ "$ref": "#/components/schemas/ConversationMetadata"
+ },
+ "description": "Full, unique selected conversations (at most the configured bound).",
+ "maxItems": 2000,
+ "minItems": 1
+ },
+ "idempotencyToken": {
+ "$ref": "#/components/schemas/CreateToken",
+ "description": "Replay with the same semantic payload returns the original job; changes conflict."
+ },
+ "includeMessageHistory": {
+ "type": "boolean",
+ "description": "Default true at the client; false still requires users and zero-part seals."
+ },
+ "source": {
+ "$ref": "#/components/schemas/SourceIdentity",
+ "description": "Binding or explicit unknown-source confirmation."
+ }
+ },
+ "additionalProperties": false
+ },
"CreateInitiativeRequest": {
"type": "object",
"description": "Create-initiative HTTP body.",
@@ -19006,6 +19534,11 @@
}
}
},
+ "CreateToken": {
+ "type": "string",
+ "format": "uuid",
+ "description": "Client-generated create idempotency token, scoped to team and administrator."
+ },
"CreateUnthreadedAnchorRequest": {
"oneOf": [
{
@@ -23408,68 +23941,320 @@
"type": "string",
"description": "The session's display name."
},
- "owner_id": {
- "type": "string",
- "description": "The user the session belongs to."
+ "owner_id": {
+ "type": "string",
+ "description": "The user the session belongs to."
+ },
+ "session_id": {
+ "type": "string",
+ "format": "uuid",
+ "description": "The session id."
+ },
+ "status": {
+ "type": "string",
+ "description": "Session lifecycle status, e.g. `no_messages`, `active`."
+ }
+ }
+ },
+ "HashMap": {
+ "type": "object",
+ "additionalProperties": {
+ "oneOf": [
+ {
+ "allOf": [
+ {
+ "$ref": "#/components/schemas/PresignedUrl"
+ },
+ {
+ "type": "object",
+ "required": ["type"],
+ "properties": {
+ "type": {
+ "type": "string",
+ "enum": ["external"]
+ }
+ }
+ }
+ ]
+ },
+ {
+ "allOf": [
+ {
+ "$ref": "#/components/schemas/S3ObjectInfo"
+ },
+ {
+ "type": "object",
+ "required": ["type"],
+ "properties": {
+ "type": {
+ "type": "string",
+ "enum": ["internal"]
+ }
+ }
+ }
+ ]
+ }
+ ]
+ },
+ "propertyNames": {
+ "type": "string"
+ }
+ },
+ "HighlightType": {
+ "type": "integer",
+ "enum": [1, 2, 3]
+ },
+ "ImportCounters": {
+ "type": "object",
+ "description": "Committed counters, never optimistic browser or uncommitted worker counts.",
+ "required": [
+ "processed",
+ "imported",
+ "duplicates",
+ "skipped",
+ "reactions"
+ ],
+ "properties": {
+ "duplicates": {
+ "type": "integer",
+ "format": "int64",
+ "description": "Previously committed source identities.",
+ "minimum": 0
+ },
+ "imported": {
+ "type": "integer",
+ "format": "int64",
+ "description": "Newly persisted messages.",
+ "minimum": 0
+ },
+ "processed": {
+ "type": "integer",
+ "format": "int64",
+ "description": "Source records examined through the committed checkpoint.",
+ "minimum": 0
+ },
+ "reactions": {
+ "type": "integer",
+ "format": "int64",
+ "description": "Newly persisted reactions.",
+ "minimum": 0
+ },
+ "skipped": {
+ "type": "integer",
+ "format": "int64",
+ "description": "Unsupported, empty or otherwise deliberately skipped records.",
+ "minimum": 0
+ }
+ }
+ },
+ "ImportError": {
+ "type": "string",
+ "description": "Public error codes; never carry raw provider errors, keys, emails or source text.",
+ "enum": [
+ "disabled",
+ "invalid_input",
+ "limit_exceeded",
+ "unavailable",
+ "admin_required",
+ "source_mismatch",
+ "conflict",
+ "upload_mismatch",
+ "lease_lost",
+ "retryable",
+ "internal"
+ ]
+ },
+ "ImportLimits": {
+ "type": "object",
+ "description": "Configurable bounds shared with browser staging and enforced again by the worker.",
+ "required": [
+ "partBytes",
+ "partRecords",
+ "recordBytes",
+ "jsonBytes",
+ "selectedBytes",
+ "zipEntries",
+ "conversations",
+ "registrationBatch",
+ "databaseBatchBytes",
+ "databaseBatchMessages"
+ ],
+ "properties": {
+ "conversations": {
+ "type": "integer",
+ "format": "int32",
+ "description": "Maximum selected conversations.",
+ "minimum": 0
+ },
+ "databaseBatchBytes": {
+ "type": "integer",
+ "format": "int64",
+ "description": "Maximum historical batch payload bytes.",
+ "minimum": 0
+ },
+ "databaseBatchMessages": {
+ "type": "integer",
+ "format": "int32",
+ "description": "Maximum messages in a historical batch.",
+ "minimum": 0
+ },
+ "jsonBytes": {
+ "type": "integer",
+ "format": "int64",
+ "description": "Maximum users, root metadata or individual day JSON bytes.",
+ "minimum": 0
+ },
+ "partBytes": {
+ "type": "integer",
+ "format": "int64",
+ "description": "Maximum NDJSON part bytes.",
+ "minimum": 0
+ },
+ "partRecords": {
+ "type": "integer",
+ "format": "int32",
+ "description": "Maximum records per part.",
+ "minimum": 0
+ },
+ "recordBytes": {
+ "type": "integer",
+ "format": "int64",
+ "description": "Maximum bytes per NDJSON record, including newline.",
+ "minimum": 0
+ },
+ "registrationBatch": {
+ "type": "integer",
+ "format": "int32",
+ "description": "Maximum descriptors or completion identities per call.",
+ "minimum": 0
+ },
+ "selectedBytes": {
+ "type": "integer",
+ "format": "int64",
+ "description": "Maximum selected temporary data bytes across all uploads.",
+ "minimum": 0
+ },
+ "zipEntries": {
+ "type": "integer",
+ "format": "int32",
+ "description": "Maximum ZIP entries scanned by the browser.",
+ "minimum": 0
+ }
+ }
+ },
+ "ImportPage": {
+ "type": "object",
+ "description": "Admin list result, also supplying upload limits before a job exists.",
+ "required": ["jobs", "limits", "sourceBinding"],
+ "properties": {
+ "jobs": {
+ "type": "array",
+ "items": {
+ "$ref": "#/components/schemas/ImportProgress"
+ },
+ "description": "At most 50 job receipts, newest first."
+ },
+ "limits": {
+ "$ref": "#/components/schemas/ImportLimits",
+ "description": "Effective server limits."
+ },
+ "nextCursor": {
+ "oneOf": [
+ {
+ "type": "null"
+ },
+ {
+ "$ref": "#/components/schemas/JobId",
+ "description": "Exclusive cursor for the next page; null when exhausted."
+ }
+ ]
+ },
+ "sourceBinding": {
+ "$ref": "#/components/schemas/SourceBinding",
+ "description": "Binding summary used by the picker confirmation."
+ }
+ }
+ },
+ "ImportProgress": {
+ "type": "object",
+ "description": "Common receipt returned by create, completion, finalize, cancel and progress.",
+ "required": [
+ "source",
+ "includeMessageHistory",
+ "jobId",
+ "status",
+ "revision",
+ "createdAt",
+ "updatedAt",
+ "usersVerified",
+ "limits",
+ "conversations"
+ ],
+ "properties": {
+ "conversations": {
+ "type": "array",
+ "items": {
+ "$ref": "#/components/schemas/ConversationProgress"
+ },
+ "description": "All selected conversations, bounded by the create limit."
+ },
+ "createdAt": {
+ "type": "string",
+ "format": "date-time",
+ "description": "Persisted creation time, also the final historical-time fallback."
+ },
+ "includeMessageHistory": {
+ "type": "boolean",
+ "description": "Immutable history option confirmed when creating this job."
+ },
+ "jobId": {
+ "$ref": "#/components/schemas/JobId",
+ "description": "Stable job identity."
+ },
+ "limits": {
+ "$ref": "#/components/schemas/ImportLimits",
+ "description": "Server bounds for staging and registration."
+ },
+ "registrationClosedAt": {
+ "type": ["string", "null"],
+ "format": "date-time",
+ "description": "Registration closure time; cancellation also closes registration."
},
- "session_id": {
- "type": "string",
- "format": "uuid",
- "description": "The session id."
+ "revision": {
+ "type": "integer",
+ "format": "int64",
+ "description": "Monotonic job revision for polling/websocket invalidation.",
+ "minimum": 0
+ },
+ "source": {
+ "$ref": "#/components/schemas/SourceIdentity",
+ "description": "Immutable source confirmation from this job, not the current team binding."
},
"status": {
+ "$ref": "#/components/schemas/JobStatus",
+ "description": "Durable job lifecycle."
+ },
+ "updatedAt": {
"type": "string",
- "description": "Session lifecycle status, e.g. `no_messages`, `active`."
+ "format": "date-time",
+ "description": "Last durable lifecycle/progress update."
+ },
+ "usersVerified": {
+ "type": "boolean",
+ "description": "Whether users metadata is verified and pinned."
}
}
},
- "HashMap": {
- "type": "object",
- "additionalProperties": {
- "oneOf": [
- {
- "allOf": [
- {
- "$ref": "#/components/schemas/PresignedUrl"
- },
- {
- "type": "object",
- "required": ["type"],
- "properties": {
- "type": {
- "type": "string",
- "enum": ["external"]
- }
- }
- }
- ]
- },
- {
- "allOf": [
- {
- "$ref": "#/components/schemas/S3ObjectInfo"
- },
- {
- "type": "object",
- "required": ["type"],
- "properties": {
- "type": {
- "type": "string",
- "enum": ["internal"]
- }
- }
- }
- ]
- }
- ]
- },
- "propertyNames": {
- "type": "string"
- }
- },
- "HighlightType": {
- "type": "integer",
- "enum": [1, 2, 3]
+ "ImportWarning": {
+ "type": "string",
+ "description": "Non-fatal, sanitized explanations displayed in admin progress.",
+ "enum": [
+ "creation_time_from_message",
+ "creation_time_from_job",
+ "unresolvable_direct_message",
+ "target_unavailable",
+ "uploads_incomplete"
+ ]
},
"ImportedAuthor": {
"type": "object",
@@ -23762,6 +24547,28 @@
}
}
},
+ "JobId": {
+ "type": "string",
+ "format": "uuid",
+ "description": "Import job identity. Generate UUIDv7 in application code."
+ },
+ "JobStatus": {
+ "type": "string",
+ "description": "Job lifecycle. Cancellation remains in progress while any lease is active.",
+ "enum": [
+ "uploading",
+ "processing",
+ "completed",
+ "completed_with_errors",
+ "failed",
+ "cancelling",
+ "cancelled"
+ ]
+ },
+ "KeySegment": {
+ "type": "string",
+ "description": "Single safe source folder/key segment; never a path or authorization proof."
+ },
"LatestMessage": {
"type": "object",
"description": "Latest-message bundle for soup payloads.",
@@ -26543,6 +27350,22 @@
}
}
},
+ "RegisterUploads": {
+ "type": "object",
+ "description": "Batched registration, also used to renew an unchanged descriptor's grant.",
+ "required": ["descriptors"],
+ "properties": {
+ "descriptors": {
+ "type": "array",
+ "items": {
+ "$ref": "#/components/schemas/UploadDescriptor"
+ },
+ "description": "At most 50 descriptors; no duplicate identities in a call.",
+ "maxItems": 50
+ }
+ },
+ "additionalProperties": false
+ },
"Reminder": {
"type": "object",
"description": "A reminder belonging to a user.\n\n`user_id` is deliberately absent: a reminder is only ever read by its owner,\nso the field would be redundant on the wire.",
@@ -26978,6 +27801,71 @@
}
}
},
+ "SearchState": {
+ "oneOf": [
+ {
+ "type": "object",
+ "description": "No committed history requires indexing.",
+ "required": ["status"],
+ "properties": {
+ "status": {
+ "type": "string",
+ "enum": ["not_needed"]
+ }
+ }
+ },
+ {
+ "type": "object",
+ "description": "Committed history requires an outbox-backed scoped submission.",
+ "required": ["status"],
+ "properties": {
+ "status": {
+ "type": "string",
+ "enum": ["pending"]
+ }
+ }
+ },
+ {
+ "type": "object",
+ "description": "Search service accepted the request, but has not completed it.",
+ "required": ["receiptId", "status"],
+ "properties": {
+ "receiptId": {
+ "type": "string",
+ "format": "uuid",
+ "description": "Receipt from the search service."
+ },
+ "status": {
+ "type": "string",
+ "enum": ["submitted"]
+ }
+ }
+ },
+ {
+ "type": "object",
+ "description": "Search service durably published the scope; eventual indexing is separate.",
+ "required": ["status"],
+ "properties": {
+ "status": {
+ "type": "string",
+ "enum": ["completed"]
+ }
+ }
+ },
+ {
+ "type": "object",
+ "description": "Publication failed; retained history must be retried independently.",
+ "required": ["status"],
+ "properties": {
+ "status": {
+ "type": "string",
+ "enum": ["failed"]
+ }
+ }
+ }
+ ],
+ "description": "Search publication/receipt state. Acceptance is not completed publication;\neven completed publication still awaits consumer indexing and refresh."
+ },
"SessionDeletedMetadata": {
"type": "object",
"description": "The session was deleted.",
@@ -27403,6 +28291,10 @@
],
"description": "Type-safe enum for setting entity property values - provides compile-time validation."
},
+ "Sha256Digest": {
+ "type": "string",
+ "description": "SHA-256 encoded as exactly 64 lowercase hexadecimal characters."
+ },
"SharePermissionV2": {
"type": "object",
"required": ["id", "owner"],
@@ -27488,6 +28380,26 @@
}
}
},
+ "SlackCompleteRequest": {
+ "$ref": "#/components/schemas/CompleteUploads",
+ "description": "At most fifty registered identities to verify and an optional immutable conversation seal."
+ },
+ "SlackCreateRequest": {
+ "$ref": "#/components/schemas/CreateImport",
+ "description": "Bounded full-metadata create payload. Domain validation applies effective limits again."
+ },
+ "SlackRegisterRequest": {
+ "$ref": "#/components/schemas/RegisterUploads",
+ "description": "At most fifty immutable upload descriptors, without client-supplied storage keys."
+ },
+ "SlackTimestamp": {
+ "type": "string",
+ "description": "Slack time represented exactly as nonnegative Unix microseconds.\nJSON is a string with six fractional digits, never a floating-point number."
+ },
+ "SlackUserId": {
+ "type": "string",
+ "description": "Slack member identity (U or W prefix), including USLACKBOT."
+ },
"SmartTagChannelMatch": {
"type": "object",
"description": "A channel visible to the caller that matches a smart tag rule.",
@@ -29505,6 +30417,83 @@
}
}
},
+ "SourceBinding": {
+ "oneOf": [
+ {
+ "type": "object",
+ "description": "No import has bound this team yet.",
+ "required": ["kind"],
+ "properties": {
+ "kind": {
+ "type": "string",
+ "enum": ["unbound"]
+ }
+ }
+ },
+ {
+ "type": "object",
+ "description": "Legacy/onboarding or explicitly confirmed unidentified source.",
+ "required": ["kind"],
+ "properties": {
+ "kind": {
+ "type": "string",
+ "enum": ["confirmed_unknown"]
+ }
+ }
+ },
+ {
+ "type": "object",
+ "description": "Known Slack workspace; reject mismatches.",
+ "required": ["sourceId", "kind"],
+ "properties": {
+ "kind": {
+ "type": "string",
+ "enum": ["known"]
+ },
+ "sourceId": {
+ "$ref": "#/components/schemas/SourceId",
+ "description": "Bound source identity."
+ }
+ }
+ }
+ ],
+ "description": "Team's durable single-source binding, including confirmed unidentified archives."
+ },
+ "SourceId": {
+ "type": "string",
+ "description": "Known Slack workspace identity (T prefix); an enterprise ID alone is insufficient."
+ },
+ "SourceIdentity": {
+ "oneOf": [
+ {
+ "type": "object",
+ "description": "Archive names its Slack workspace; reject a conflicting team binding.",
+ "required": ["sourceId", "kind"],
+ "properties": {
+ "kind": {
+ "type": "string",
+ "enum": ["known"]
+ },
+ "sourceId": {
+ "$ref": "#/components/schemas/SourceId",
+ "description": "Workspace identifier in the archive."
+ }
+ }
+ },
+ {
+ "type": "object",
+ "description": "Explicit confirmation that an unidentified archive belongs to this team's source.\nChoosing this variant is affirmative confirmation, not an inferred default.",
+ "required": ["kind"],
+ "properties": {
+ "kind": {
+ "type": "string",
+ "enum": ["confirmed_unknown"]
+ }
+ }
+ }
+ ],
+ "description": "Source identity supplied when creating a job. One binding per Macro team in v1."
+ },
"StarterDocumentsResponse": {
"type": "object",
"description": "The deterministic starter document ids for the current user.",
@@ -30375,6 +31364,34 @@
}
}
},
+ "UploadDescriptor": {
+ "type": "object",
+ "description": "Immutable expected object properties, registered before signing an upload.",
+ "required": ["upload", "sha256", "byteLength"],
+ "properties": {
+ "byteLength": {
+ "type": "integer",
+ "format": "int64",
+ "description": "Exact byte length including delimiters.",
+ "minimum": 0
+ },
+ "recordCount": {
+ "type": ["integer", "null"],
+ "format": "int32",
+ "description": "Exact NDJSON record count; null for the users JSON payload.",
+ "minimum": 0
+ },
+ "sha256": {
+ "$ref": "#/components/schemas/Sha256Digest",
+ "description": "SHA-256 of the exact uploaded bytes, not the ETag."
+ },
+ "upload": {
+ "$ref": "#/components/schemas/UploadId",
+ "description": "Manifest identity within this job."
+ }
+ },
+ "additionalProperties": false
+ },
"UploadDocumentStatus": {
"type": "string",
"description": "The upload status of the document",
@@ -30420,6 +31437,73 @@
}
}
},
+ "UploadGrant": {
+ "type": "object",
+ "description": "Signed upload permission returned just before PUT; never persisted as an identity.",
+ "required": ["descriptor", "url", "requiredHeaders", "expiresAt"],
+ "properties": {
+ "descriptor": {
+ "$ref": "#/components/schemas/UploadDescriptor",
+ "description": "Exact immutable descriptor being signed."
+ },
+ "expiresAt": {
+ "type": "string",
+ "format": "date-time",
+ "description": "Grant expiry; callers renew by re-registering the identical descriptor."
+ },
+ "requiredHeaders": {
+ "type": "object",
+ "description": "Required signed upload headers (including checksum, content type and create-only\ncondition). Browser-forbidden Content-Length is derived from the exact Blob bytes.",
+ "additionalProperties": {
+ "type": "string"
+ },
+ "propertyNames": {
+ "type": "string"
+ }
+ },
+ "url": {
+ "type": "string",
+ "description": "Short-lived signed destination; must not be logged."
+ }
+ }
+ },
+ "UploadId": {
+ "oneOf": [
+ {
+ "type": "object",
+ "description": "One normalized users payload from users.json and/or org_users.json.",
+ "required": ["kind"],
+ "properties": {
+ "kind": {
+ "type": "string",
+ "enum": ["users"]
+ }
+ }
+ },
+ {
+ "type": "object",
+ "description": "One NDJSON part in a selected conversation.",
+ "required": ["slackChannelId", "partIndex", "kind"],
+ "properties": {
+ "kind": {
+ "type": "string",
+ "enum": ["conversation_part"]
+ },
+ "partIndex": {
+ "type": "integer",
+ "format": "int32",
+ "description": "Zero-based part index; sealed manifests must be contiguous.",
+ "minimum": 0
+ },
+ "slackChannelId": {
+ "$ref": "#/components/schemas/ConversationId",
+ "description": "Selected Slack conversation."
+ }
+ }
+ }
+ ],
+ "description": "Identity in a job's persisted manifest. No user-supplied object keys."
+ },
"UpsertUserDocumentViewLocationRequest": {
"type": "object",
"required": ["location"],
diff --git a/apps/web/src/lib/service-clients/service-storage/slack-import-upload.test.ts b/apps/web/src/lib/service-clients/service-storage/slack-import-upload.test.ts
new file mode 100644
index 0000000000..025f632ed4
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/slack-import-upload.test.ts
@@ -0,0 +1,338 @@
+import { isTauri } from '@core/util/platform';
+import { platformFetch } from '@core/util/platformFetch';
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+import type { UploadGrant } from './generated/schemas/uploadGrant';
+import { uploadSlackImport } from './slack-import-upload';
+
+vi.mock('@core/util/platform', () => ({ isTauri: vi.fn(() => false) }));
+vi.mock('@core/util/platformFetch', () => ({ platformFetch: vi.fn() }));
+
+class FakeXhr {
+ static instances: FakeXhr[] = [];
+ status = 0;
+ withCredentials = false;
+ onload: (() => void) | null = null;
+ onerror: (() => void) | null = null;
+ onabort: (() => void) | null = null;
+ ontimeout: (() => void) | null = null;
+ upload = {
+ onprogress: null as ((event: Partial<ProgressEvent>) => void) | null,
+ };
+ open = vi.fn();
+ setRequestHeader = vi.fn();
+ send = vi.fn();
+ abort = vi.fn(() => this.onabort?.());
+
+ constructor() {
+ FakeXhr.instances.push(this);
+ }
+
+ respond(status: number): void {
+ this.status = status;
+ this.onload?.();
+ }
+}
+
+function grant(): UploadGrant {
+ return {
+ descriptor: {
+ upload: { kind: 'users' },
+ byteLength: 2,
+ sha256:
+ '4f53cda18c2baa0c0354bb5f9a3ecbe5ed12ab4d8e11ba873c2f11161202b945',
+ recordCount: null,
+ },
+ url: 'https://staging.example/users.json?signed=secret',
+ expiresAt: new Date(Date.now() + 300_000).toISOString(),
+ requiredHeaders: {
+ 'content-type': 'application/json',
+ 'if-none-match': '*',
+ 'x-amz-checksum-sha256': 'T1PNoYwrqgwDVLtfmj7L5e0Sq02OEbqHPC8RFhICuUU=',
+ },
+ };
+}
+
+function xhr(): FakeXhr {
+ return FakeXhr.instances[0];
+}
+
+beforeEach(() => {
+ vi.resetAllMocks();
+ FakeXhr.instances = [];
+ vi.stubGlobal('XMLHttpRequest', FakeXhr);
+});
+afterEach(() => vi.unstubAllGlobals());
+
+describe('browser Slack import uploads', () => {
+ it('sends exact signed headers and the original Blob, with actual byte progress', async () => {
+ const permission = grant();
+ const blob = new Blob(['[]'], { type: 'text/plain' });
+ const onProgress = vi.fn();
+ const pending = uploadSlackImport(permission, blob, { onProgress });
+ expect(xhr().open).toHaveBeenCalledWith('PUT', permission.url);
+ expect(xhr().withCredentials).toBe(false);
+ expect(xhr().send).toHaveBeenCalledWith(blob);
+ expect(xhr().setRequestHeader.mock.calls).toEqual(
+ Object.entries(permission.requiredHeaders)
+ );
+ expect(onProgress).not.toHaveBeenCalled();
+ xhr().upload.onprogress?.({ loaded: 1, total: 2, lengthComputable: true });
+ expect(onProgress).toHaveBeenLastCalledWith({
+ kind: 'bytes',
+ loaded: 1,
+ total: 2,
+ });
+ xhr().respond(200);
+ expect((await pending)._unsafeUnwrap()).toBe('uploaded');
+ expect(onProgress).toHaveBeenCalledTimes(1);
+ expect(xhr().upload.onprogress).toBeNull();
+ });
+
+ it('reports indeterminate progress when XHR cannot measure it', async () => {
+ const onProgress = vi.fn();
+ const pending = uploadSlackImport(grant(), new Blob(['[]']), {
+ onProgress,
+ });
+ xhr().upload.onprogress?.({ loaded: 0, lengthComputable: false });
+ expect(onProgress).toHaveBeenCalledWith({
+ kind: 'indeterminate',
+ total: 2,
+ });
+ xhr().respond(204);
+ expect((await pending).isOk()).toBe(true);
+ });
+
+ it('returns 412 for completion verification without retrying or overwriting', async () => {
+ const pending = uploadSlackImport(grant(), new Blob(['[]']));
+ xhr().respond(412);
+ expect((await pending)._unsafeUnwrap()).toBe('already-exists');
+ expect(FakeXhr.instances).toHaveLength(1);
+ expect(xhr().send).toHaveBeenCalledTimes(1);
+ expect(xhr().setRequestHeader).toHaveBeenCalledWith('if-none-match', '*');
+ });
+
+ it.each([403, 409, 500])(
+ 'preserves HTTP failure status %i',
+ async (status) => {
+ const pending = uploadSlackImport(grant(), new Blob(['[]']));
+ xhr().respond(status);
+ expect((await pending)._unsafeUnwrapErr()).toEqual({
+ code: 'HTTP_ERROR',
+ status,
+ });
+ }
+ );
+
+ it.each(['onerror', 'ontimeout'] as const)(
+ 'handles %s without leaking the URL',
+ async (event) => {
+ const pending = uploadSlackImport(grant(), new Blob(['[]']));
+ xhr()[event]?.();
+ expect((await pending)._unsafeUnwrapErr()).toEqual({
+ code: 'NETWORK_ERROR',
+ });
+ }
+ );
+
+ it('handles a synchronous transport failure', async () => {
+ vi.stubGlobal(
+ 'XMLHttpRequest',
+ class extends FakeXhr {
+ constructor() {
+ super();
+ this.send.mockImplementation(() => {
+ throw new Error('Network unavailable');
+ });
+ }
+ }
+ );
+ const result = await uploadSlackImport(grant(), new Blob(['[]']));
+ expect(result._unsafeUnwrapErr()).toEqual({ code: 'NETWORK_ERROR' });
+ expect(xhr().onload).toBeNull();
+ });
+
+ it('treats status zero as network failure', async () => {
+ const pending = uploadSlackImport(grant(), new Blob(['[]']));
+ xhr().respond(0);
+ expect((await pending)._unsafeUnwrapErr()).toEqual({
+ code: 'NETWORK_ERROR',
+ });
+ });
+
+ it('aborts an in-flight request and removes its signal listener', async () => {
+ const controller = new AbortController();
+ const remove = vi.spyOn(controller.signal, 'removeEventListener');
+ const pending = uploadSlackImport(grant(), new Blob(['[]']), {
+ signal: controller.signal,
+ });
+ controller.abort();
+ expect((await pending)._unsafeUnwrapErr()).toEqual({ code: 'ABORTED' });
+ expect(xhr().abort).toHaveBeenCalledTimes(1);
+ expect(remove).toHaveBeenCalledWith('abort', expect.any(Function));
+ expect(xhr().onload).toBeNull();
+ });
+
+ it('does not abort a finished upload when the signal later aborts', async () => {
+ const controller = new AbortController();
+ const pending = uploadSlackImport(grant(), new Blob(['[]']), {
+ signal: controller.signal,
+ });
+ xhr().respond(200);
+ await pending;
+ controller.abort();
+ expect(xhr().abort).not.toHaveBeenCalled();
+ });
+});
+
+describe.each([false, true])('upload preflight (Tauri: %s)', (native) => {
+ beforeEach(() => vi.mocked(isTauri).mockReturnValue(native));
+ afterEach(() => {
+ expect(FakeXhr.instances).toHaveLength(0);
+ expect(platformFetch).not.toHaveBeenCalled();
+ });
+
+ it('rejects mismatched byte length, including multibyte text', async () => {
+ const result = await uploadSlackImport(grant(), new Blob(['éé']));
+ expect(result._unsafeUnwrapErr()).toEqual({ code: 'SIZE_MISMATCH' });
+ });
+
+ it('rejects expired grants before starting a PUT', async () => {
+ const permission = { ...grant(), expiresAt: new Date(0).toISOString() };
+ expect(
+ (await uploadSlackImport(permission, new Blob(['[]'])))._unsafeUnwrapErr()
+ ).toEqual({ code: 'EXPIRED' });
+ });
+
+ it('rejects malformed expiry', async () => {
+ const permission = { ...grant(), expiresAt: 'invalid' };
+ expect(
+ (await uploadSlackImport(permission, new Blob(['[]'])))._unsafeUnwrapErr()
+ ).toEqual({ code: 'INVALID_GRANT' });
+ });
+
+ it.each(['Content-Length', 'Host', 'Cookie', 'Origin', 'Sec-Fetch-Mode'])(
+ 'never sets forbidden header %s',
+ async (header) => {
+ const permission = grant();
+ permission.requiredHeaders[header] = '2';
+ expect(
+ (
+ await uploadSlackImport(permission, new Blob(['[]']))
+ )._unsafeUnwrapErr()
+ ).toEqual({ code: 'INVALID_GRANT' });
+ }
+ );
+
+ it.each(['if-none-match', 'content-type', 'x-amz-checksum-sha256'])(
+ 'rejects grants missing %s',
+ async (header) => {
+ const permission = grant();
+ delete permission.requiredHeaders[header];
+ expect(
+ (
+ await uploadSlackImport(permission, new Blob(['[]']))
+ )._unsafeUnwrapErr()
+ ).toEqual({ code: 'INVALID_GRANT' });
+ }
+ );
+
+ it('rejects malformed header values as invalid grants', async () => {
+ const permission = grant();
+ permission.requiredHeaders['content-type'] =
+ 'application/json\r\nInjected: value';
+ const result = await uploadSlackImport(permission, new Blob(['[]']));
+ expect(result._unsafeUnwrapErr()).toEqual({ code: 'INVALID_GRANT' });
+ });
+
+ it('requires create-only rather than overwrite permission', async () => {
+ const permission = grant();
+ permission.requiredHeaders['if-none-match'] = 'other';
+ const result = await uploadSlackImport(permission, new Blob(['[]']));
+ expect(result._unsafeUnwrapErr()).toEqual({ code: 'INVALID_GRANT' });
+ });
+
+ it('does not send an already-aborted upload', async () => {
+ const controller = new AbortController();
+ controller.abort();
+ const result = await uploadSlackImport(grant(), new Blob(['[]']), {
+ signal: controller.signal,
+ });
+ expect(result._unsafeUnwrapErr()).toEqual({ code: 'ABORTED' });
+ });
+});
+
+describe('Tauri Slack import uploads', () => {
+ beforeEach(() => vi.mocked(isTauri).mockReturnValue(true));
+
+ it.each([200, 412])(
+ 'uses platformFetch with indeterminate progress for status %i',
+ async (status) => {
+ vi.mocked(platformFetch).mockResolvedValue(
+ new Response(null, { status })
+ );
+ const permission = grant();
+ const body = new Blob(['[]']);
+ const signal = new AbortController().signal;
+ const onProgress = vi.fn();
+ const result = await uploadSlackImport(permission, body, {
+ signal,
+ onProgress,
+ });
+ expect(result._unsafeUnwrap()).toBe(
+ status === 412 ? 'already-exists' : 'uploaded'
+ );
+ expect(platformFetch).toHaveBeenCalledExactlyOnceWith(permission.url, {
+ method: 'PUT',
+ body,
+ signal,
+ headers: permission.requiredHeaders,
+ credentials: 'omit',
+ redirect: 'error',
+ });
+ expect(onProgress.mock.calls).toEqual([
+ [{ kind: 'indeterminate', total: 2 }],
+ ]);
+ expect(FakeXhr.instances).toHaveLength(0);
+ }
+ );
+
+ it('reports native network errors without logging signed URLs', async () => {
+ vi.mocked(platformFetch).mockRejectedValue(new Error(grant().url));
+ expect(
+ (await uploadSlackImport(grant(), new Blob(['[]'])))._unsafeUnwrapErr()
+ ).toEqual({ code: 'NETWORK_ERROR' });
+ });
+
+ it('releases native response bodies without reading or trusting them', async () => {
+ const cancel = vi.fn();
+ const body = new ReadableStream({ cancel });
+ vi.mocked(platformFetch).mockResolvedValue(
+ new Response(body, { status: 412 })
+ );
+ const result = await uploadSlackImport(grant(), new Blob(['[]']));
+ expect(result._unsafeUnwrap()).toBe('already-exists');
+ expect(cancel).toHaveBeenCalledTimes(1);
+ });
+
+ it('preserves native HTTP failures', async () => {
+ vi.mocked(platformFetch).mockResolvedValue(
+ new Response(null, { status: 403 })
+ );
+ expect(
+ (await uploadSlackImport(grant(), new Blob(['[]'])))._unsafeUnwrapErr()
+ ).toEqual({ code: 'HTTP_ERROR', status: 403 });
+ });
+
+ it('forwards abort to the native transport', async () => {
+ const controller = new AbortController();
+ vi.mocked(platformFetch).mockImplementation(async (_url, options) => {
+ expect(options?.signal).toBe(controller.signal);
+ controller.abort();
+ throw new DOMException('Aborted', 'AbortError');
+ });
+ const result = await uploadSlackImport(grant(), new Blob(['[]']), {
+ signal: controller.signal,
+ });
+ expect(result._unsafeUnwrapErr()).toEqual({ code: 'ABORTED' });
+ });
+});
diff --git a/apps/web/src/lib/service-clients/service-storage/slack-import-upload.ts b/apps/web/src/lib/service-clients/service-storage/slack-import-upload.ts
new file mode 100644
index 0000000000..395c8e383b
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/slack-import-upload.ts
@@ -0,0 +1,158 @@
+import { isTauri } from '@core/util/platform';
+import { platformFetch } from '@core/util/platformFetch';
+import { err, ok, type Result } from 'neverthrow';
+import type { UploadGrant } from './generated/schemas/uploadGrant';
+
+export type SlackUploadProgress =
+ | { kind: 'bytes'; loaded: number; total: number }
+ | { kind: 'indeterminate'; total: number };
+
+export type SlackUploadError =
+ | {
+ code:
+ | 'ABORTED'
+ | 'EXPIRED'
+ | 'SIZE_MISMATCH'
+ | 'INVALID_GRANT'
+ | 'NETWORK_ERROR';
+ }
+ | { code: 'HTTP_ERROR'; status: number };
+
+/** Both outcomes require uploads/complete to verify the persisted object. */
+export type SlackUploadOutcome = 'uploaded' | 'already-exists';
+type UploadResult = Result<SlackUploadOutcome, SlackUploadError>;
+
+export type SlackUploadOptions = {
+ signal?: AbortSignal;
+ onProgress?: (progress: SlackUploadProgress) => void;
+};
+
+function uploadStatus(status: number): UploadResult {
+ if (status === 412) return ok('already-exists');
+ if (status >= 200 && status < 300) return ok('uploaded');
+ if (status === 0) return err({ code: 'NETWORK_ERROR' });
+ return err({ code: 'HTTP_ERROR', status });
+}
+
+/**
+ * PUT exact bytes once, without app credentials or automatic retries. A 412 is
+ * never retried as an overwrite. The query layer owns completion verification
+ * and renewal of expired grants using the identical registered descriptor.
+ */
+export async function uploadSlackImport(
+ grant: UploadGrant,
+ blob: Blob,
+ options: SlackUploadOptions = {}
+): Promise<UploadResult> {
+ if (options.signal?.aborted) return err({ code: 'ABORTED' });
+ if (
+ !Number.isSafeInteger(grant.descriptor.byteLength) ||
+ blob.size !== grant.descriptor.byteLength
+ ) {
+ return err({ code: 'SIZE_MISMATCH' });
+ }
+ const expiresAt = Date.parse(grant.expiresAt);
+ if (!Number.isFinite(expiresAt)) return err({ code: 'INVALID_GRANT' });
+ if (expiresAt <= Date.now()) return err({ code: 'EXPIRED' });
+
+ // Only S3 upload headers are accepted. In particular, Content-Length and
+ // Host must be derived by the transport, never set as browser headers.
+ const headers = Object.entries(grant.requiredHeaders);
+ if (
+ headers.some(
+ ([name]) => !/^(content-type|if-none-match|x-amz-[a-z0-9-]+)$/i.test(name)
+ )
+ ) {
+ return err({ code: 'INVALID_GRANT' });
+ }
+ try {
+ const normalized = new Headers(grant.requiredHeaders);
+ if (
+ normalized.get('if-none-match') !== '*' ||
+ !normalized.get('content-type') ||
+ !normalized.get('x-amz-checksum-sha256')
+ ) {
+ return err({ code: 'INVALID_GRANT' });
+ }
+ } catch {
+ return err({ code: 'INVALID_GRANT' });
+ }
+
+ if (isTauri()) return uploadWithNativeFetch(grant, blob, options);
+ return uploadWithXhr(grant, blob, options);
+}
+
+async function uploadWithNativeFetch(
+ grant: UploadGrant,
+ blob: Blob,
+ options: SlackUploadOptions
+): Promise<UploadResult> {
+ // The native HTTP plugin does not expose upload byte progress.
+ options.onProgress?.({ kind: 'indeterminate', total: blob.size });
+ try {
+ const response = await platformFetch(grant.url, {
+ method: 'PUT',
+ body: blob,
+ headers: grant.requiredHeaders,
+ signal: options.signal,
+ credentials: 'omit',
+ redirect: 'error',
+ });
+ // Release the native response resource; neither ETag nor the response body
+ // substitutes for the server's checksum/length completion verification.
+ await response.body?.cancel();
+ if (options.signal?.aborted) return err({ code: 'ABORTED' });
+ return uploadStatus(response.status);
+ } catch {
+ return err({ code: options.signal?.aborted ? 'ABORTED' : 'NETWORK_ERROR' });
+ }
+}
+
+function uploadWithXhr(
+ grant: UploadGrant,
+ blob: Blob,
+ { signal, onProgress }: SlackUploadOptions
+): Promise<UploadResult> {
+ return new Promise((resolve) => {
+ const xhr = new XMLHttpRequest();
+ function finish(result: UploadResult): void {
+ signal?.removeEventListener('abort', abort);
+ xhr.onload = null;
+ xhr.onerror = null;
+ xhr.onabort = null;
+ xhr.ontimeout = null;
+ xhr.upload.onprogress = null;
+ resolve(result);
+ }
+ function abort(): void {
+ xhr.abort();
+ finish(err({ code: 'ABORTED' }));
+ }
+ xhr.onload = () => finish(uploadStatus(xhr.status));
+ xhr.onerror = () => finish(err({ code: 'NETWORK_ERROR' }));
+ xhr.ontimeout = () => finish(err({ code: 'NETWORK_ERROR' }));
+ xhr.onabort = () => finish(err({ code: 'ABORTED' }));
+ xhr.upload.onprogress = (event) => {
+ if (event.lengthComputable) {
+ onProgress?.({ kind: 'bytes', loaded: event.loaded, total: blob.size });
+ } else {
+ onProgress?.({ kind: 'indeterminate', total: blob.size });
+ }
+ };
+ signal?.addEventListener('abort', abort, { once: true });
+ if (signal?.aborted) {
+ abort();
+ return;
+ }
+ try {
+ xhr.open('PUT', grant.url);
+ xhr.withCredentials = false;
+ for (const [name, value] of Object.entries(grant.requiredHeaders)) {
+ xhr.setRequestHeader(name, value);
+ }
+ xhr.send(blob);
+ } catch {
+ finish(err({ code: 'NETWORK_ERROR' }));
+ }
+ });
+}
diff --git a/apps/web/src/lib/workers/slack-import/archive-reader.test.ts b/apps/web/src/lib/workers/slack-import/archive-reader.test.ts
new file mode 100644
index 0000000000..683609b653
--- /dev/null
+++ b/apps/web/src/lib/workers/slack-import/archive-reader.test.ts
@@ -0,0 +1,691 @@
+// @vitest-environment node
+import 'fake-indexeddb/auto';
+import { afterEach, describe, expect, it, vi } from 'vitest';
+import { DEFAULT_ARCHIVE_LIMITS } from '../../../features/slack-import/core/export';
+import type {
+ ArchiveWorkerPort,
+ ArchiveWorkerRequest,
+ ArchiveWorkerResponse,
+} from '../../../features/slack-import/core/worker-protocol';
+import {
+ corporateEntries,
+ enterpriseEntries,
+ MESSAGE,
+ patchEntry,
+ standardEntries,
+ streamingZipFixture,
+ zipBlob,
+ zipFixture,
+} from '../../../features/slack-import/tests/zip-fixtures';
+import { type ArchiveDay, ArchiveReader } from './archive-reader';
+import { installArchiveWorker } from './unzip-worker';
+import { createSlackImportWorker } from './worker-client';
+
+async function collect(
+ reader: ArchiveReader,
+ ids: string[],
+ history = true
+): Promise<ArchiveDay[]> {
+ const days = [];
+ for await (const day of reader.readHistory(ids, history)) days.push(day);
+ return days;
+}
+
+function workerHarness(): {
+ messages: ArchiveWorkerResponse[];
+ send(request: ArchiveWorkerRequest): void;
+ wait(type: ArchiveWorkerResponse['type']): Promise<void>;
+} {
+ const messages: ArchiveWorkerResponse[] = [];
+ const port: ArchiveWorkerPort = {
+ postMessage: (message) => messages.push(message),
+ onmessage: null,
+ };
+ installArchiveWorker(port);
+ return {
+ messages,
+ send(request: ArchiveWorkerRequest): void {
+ port.onmessage?.({ data: request } as MessageEvent<ArchiveWorkerRequest>);
+ },
+ async wait(type: ArchiveWorkerResponse['type']): Promise<void> {
+ await vi.waitFor(() =>
+ expect(messages.some((message) => message.type === type)).toBe(true)
+ );
+ },
+ };
+}
+
+afterEach(() => {
+ vi.unstubAllGlobals();
+ vi.restoreAllMocks();
+});
+
+describe('bounded ZIP reader', () => {
+ it.each([standardEntries, corporateEntries, enterpriseEntries])(
+ 'discovers and parses supported export roots',
+ async (entries) => {
+ const reader = new ArchiveReader(zipBlob(zipFixture(entries())));
+ const discovery = await reader.discover();
+ expect(
+ discovery.conversations.every((item) => item.messageCount === null)
+ ).toBe(true);
+ const days = await collect(
+ reader,
+ discovery.conversations.map((item) => item.slackChannelId)
+ );
+ expect(days).toHaveLength(discovery.dayEntries.length);
+ expect(days.every((day) => day.records[0].ts === MESSAGE.ts)).toBe(true);
+ }
+ );
+
+ it('ignores Slack file/canvas discussions without inflating them', async () => {
+ const reader = new ArchiveReader(
+ zipBlob(
+ zipFixture({
+ ...standardEntries(),
+ 'FC:F100:Project notes/': '',
+ 'FC:F100:Project notes/2023-11-14.json': 'not channel JSON',
+ 'file_conversations.json': [{ id: 'F100', name: 'Project notes' }],
+ })
+ )
+ );
+ const discovery = await reader.discover();
+ expect(discovery.conversations.map((item) => item.slackChannelId)).toEqual([
+ 'C100',
+ ]);
+ expect(discovery.dayEntries).toHaveLength(1);
+ const days = await collect(reader, ['C100']);
+ expect(days).toHaveLength(1);
+ expect(days[0].records).toEqual([MESSAGE]);
+ });
+
+ it('supports streaming ZIP data descriptors and rejects descriptor conflicts', async () => {
+ const bytes = streamingZipFixture(standardEntries());
+ const reader = new ArchiveReader(zipBlob(bytes));
+ await reader.discover();
+ expect(await collect(reader, ['C100'])).toHaveLength(1);
+ const forged = patchEntry(bytes, 'users.json', (header, central) => {
+ if (central) header.setUint32(24, 1, true);
+ });
+ await expect(
+ new ArchiveReader(zipBlob(forged)).discover()
+ ).rejects.toMatchObject({ code: 'invalid_zip' });
+ });
+
+ it('uses bounded Blob slices across large central directories and day files', async () => {
+ const entries = standardEntries();
+ entries['general/2023-11-14.json'] = [
+ { ...MESSAGE, text: 'x'.repeat(200_000) },
+ ];
+ for (let i = 0; i < 1500; i++)
+ entries[`files/attachment-${i}.bin`] = 'ignored';
+ const blob = zipBlob(zipFixture(entries, false));
+ const slice = vi.spyOn(blob, 'slice');
+ const readWholeArchive = vi.spyOn(blob, 'arrayBuffer');
+ const reader = new ArchiveReader(blob);
+ await reader.discover();
+ expect(await collect(reader, ['C100'])).toHaveLength(1);
+ expect(readWholeArchive).not.toHaveBeenCalled();
+ expect(
+ slice.mock.calls.every(
+ ([start, end]) => (end ?? 0) - (start ?? 0) <= 65557
+ )
+ ).toBe(true);
+ });
+
+ it('does not parse any history or make a network/upload request during discovery', async () => {
+ const fetch = vi.fn(() => {
+ throw new Error('No network allowed');
+ });
+ vi.stubGlobal('fetch', fetch);
+ vi.stubGlobal(
+ 'XMLHttpRequest',
+ vi.fn(() => {
+ throw new Error('No uploads allowed');
+ })
+ );
+ const reader = new ArchiveReader(
+ zipBlob(
+ zipFixture({
+ ...standardEntries(),
+ 'general/2023-11-14.json': 'not JSON',
+ })
+ )
+ );
+ const discovery = await reader.discover();
+ expect(discovery.conversations[0].messageCount).toBeNull();
+ expect(fetch).not.toHaveBeenCalled();
+ expect(XMLHttpRequest).not.toHaveBeenCalled();
+ await expect(collect(reader, ['C100'])).rejects.toMatchObject({
+ code: 'invalid_json',
+ });
+ });
+
+ it('shape-only skips malformed/oversized history entirely', async () => {
+ const reader = new ArchiveReader(
+ zipBlob(
+ zipFixture({
+ ...standardEntries(),
+ 'general/2023-11-14.json': 'x'.repeat(4000),
+ })
+ ),
+ { ...DEFAULT_ARCHIVE_LIMITS, jsonBytes: 1024 }
+ );
+ await reader.discover();
+ expect(await collect(reader, ['C100'], false)).toEqual([]);
+ });
+
+ it('supports metadata-only conversations with empty history', async () => {
+ const entries = standardEntries();
+ delete entries['general/2023-11-14.json'];
+ const reader = new ArchiveReader(zipBlob(zipFixture(entries)));
+ const discovery = await reader.discover();
+ expect(discovery.conversations[0].messageCount).toBeNull();
+ expect(await collect(reader, ['C100'])).toEqual([]);
+ });
+
+ it('bounds compressed input as well as output, even with forged original sizes', async () => {
+ const path = 'general/2023-11-14.json';
+ const bytes = patchEntry(
+ zipFixture({ ...standardEntries(), [path]: 'x'.repeat(100_000) }, false),
+ path,
+ (header, central) => header.setUint32(central ? 24 : 22, 1, true)
+ );
+ const reader = new ArchiveReader(zipBlob(bytes), {
+ ...DEFAULT_ARCHIVE_LIMITS,
+ jsonBytes: 1024,
+ });
+ await reader.discover();
+ await expect(collect(reader, ['C100'])).rejects.toMatchObject({
+ code: 'compressed_limit',
+ });
+ });
+
+ it('reads all selected folders together once, skipping unselected bodies', async () => {
+ const entries = corporateEntries();
+ entries['private/2023-11-15.json'] = [
+ { ...MESSAGE, ts: '1700000000.000002' },
+ ];
+ entries['mpdm-example/2023-11-14.json'] = 'malformed, but not selected';
+ const reader = new ArchiveReader(zipBlob(zipFixture(entries, false)));
+ await reader.discover();
+ const days = await collect(reader, ['C200', 'D100']);
+ expect(days.map((day) => day.slackChannelId)).toEqual([
+ 'C200',
+ 'D100',
+ 'C200',
+ ]);
+ await expect(collect(reader, ['C200'])).rejects.toMatchObject({
+ code: 'invalid_state',
+ });
+ });
+
+ it('does not inflate attachment bytes', async () => {
+ const bytes = zipFixture({
+ ...standardEntries(),
+ 'files/ignored.bin': 'x'.repeat(200_000),
+ });
+ const reader = new ArchiveReader(zipBlob(bytes), {
+ ...DEFAULT_ARCHIVE_LIMITS,
+ jsonBytes: 1024,
+ });
+ await reader.discover();
+ expect(await collect(reader, ['C100'])).toHaveLength(1);
+ });
+
+ it.each([
+ '../users.json',
+ '/root.json',
+ 'general/../day.json',
+ 'general\\2020-01-01.json',
+ ])('rejects unsafe ZIP paths %s', async (path) => {
+ const reader = new ArchiveReader(
+ zipBlob(zipFixture({ ...standardEntries(), [path]: [] }))
+ );
+ await expect(reader.discover()).rejects.toMatchObject({
+ code: 'unsafe_path',
+ });
+ });
+
+ it('rejects conflicting duplicate ZIP entries, including local names', async () => {
+ const bytes = patchEntry(
+ zipFixture({ ...standardEntries(), 'other.json': [1] }),
+ 'other.json',
+ (header, central) => {
+ const start = central ? 46 : 30;
+ for (const [i, byte] of new TextEncoder()
+ .encode('users.json')
+ .entries())
+ header.setUint8(start + i, byte);
+ }
+ );
+ await expect(
+ new ArchiveReader(zipBlob(bytes)).discover()
+ ).rejects.toMatchObject({ code: 'duplicate_entry' });
+ });
+
+ it.each([
+ [
+ 'encrypted',
+ (header: DataView, central: boolean) =>
+ header.setUint16(central ? 8 : 6, 1, true),
+ ],
+ [
+ 'unsupported compression',
+ (header: DataView, central: boolean) =>
+ header.setUint16(central ? 10 : 8, 99, true),
+ ],
+ [
+ 'ZIP64',
+ (header: DataView, central: boolean) =>
+ header.setUint16(central ? 6 : 4, 45, true),
+ ],
+ [
+ 'symlink',
+ (header: DataView, central: boolean) => {
+ if (central) header.setUint32(38, 0xa0000000, true);
+ },
+ ],
+ ] as const)('rejects %s archives', async (_, patch) => {
+ const bytes = patchEntry(
+ zipFixture(standardEntries()),
+ 'users.json',
+ patch
+ );
+ await expect(
+ new ArchiveReader(zipBlob(bytes)).discover()
+ ).rejects.toMatchObject({ code: 'unsupported_zip' });
+ });
+
+ it('rejects truncated ZIPs, CRC corruption and local/central inconsistencies', async () => {
+ const valid = zipFixture(standardEntries());
+ await expect(
+ new ArchiveReader(zipBlob(valid.slice(0, -1))).discover()
+ ).rejects.toMatchObject({ code: 'invalid_zip' });
+ const badCrc = patchEntry(valid, 'users.json', (header, central) =>
+ header.setUint32(central ? 16 : 14, 0, true)
+ );
+ await expect(
+ new ArchiveReader(zipBlob(badCrc)).discover()
+ ).rejects.toMatchObject({ code: 'invalid_zip' });
+ const badLocal = patchEntry(valid, 'users.json', (header, central) => {
+ if (!central) header.setUint16(8, 99, true);
+ });
+ await expect(
+ new ArchiveReader(zipBlob(badLocal)).discover()
+ ).rejects.toMatchObject({ code: 'invalid_zip' });
+ });
+
+ it('rejects missing metadata and unresolved/nested history instead of silently omitting it', async () => {
+ await expect(
+ new ArchiveReader(zipBlob(zipFixture({ 'users.json': [] }))).discover()
+ ).rejects.toMatchObject({ code: 'missing_metadata' });
+ await expect(
+ new ArchiveReader(
+ zipBlob(
+ zipFixture({ ...standardEntries(), 'unknown/2020-01-01.json': [] })
+ )
+ ).discover()
+ ).rejects.toMatchObject({ code: 'unresolved_folder' });
+ await expect(
+ new ArchiveReader(
+ zipBlob(
+ zipFixture({
+ ...standardEntries(),
+ 'team/general/2020-01-01.json': [],
+ })
+ )
+ ).discover()
+ ).rejects.toMatchObject({ code: 'unresolved_folder' });
+ });
+
+ it('enforces entry and selection limits', async () => {
+ await expect(
+ new ArchiveReader(zipBlob(zipFixture(standardEntries())), {
+ ...DEFAULT_ARCHIVE_LIMITS,
+ zipEntries: 2,
+ }).discover()
+ ).rejects.toMatchObject({ code: 'entry_limit' });
+ for (const ids of [[], ['missing'], ['C100', 'C100']]) {
+ const reader = new ArchiveReader(zipBlob(zipFixture(standardEntries())));
+ await reader.discover();
+ await expect(collect(reader, ids)).rejects.toMatchObject({
+ code: 'invalid_selection',
+ });
+ }
+ const reader = new ArchiveReader(zipBlob(zipFixture(corporateEntries())), {
+ ...DEFAULT_ARCHIVE_LIMITS,
+ conversations: 1,
+ });
+ await reader.discover();
+ await expect(collect(reader, ['C200', 'D100'])).rejects.toMatchObject({
+ code: 'invalid_selection',
+ });
+ });
+
+ it('applies actual expanded-byte limits to metadata and day ZIP bombs with forged sizes', async () => {
+ for (const path of ['users.json', 'general/2023-11-14.json']) {
+ const bytes = patchEntry(
+ zipFixture({ ...standardEntries(), [path]: 'x'.repeat(2_000_000) }),
+ path,
+ (header, central) => header.setUint32(central ? 24 : 22, 1, true)
+ );
+ const reader = new ArchiveReader(zipBlob(bytes), {
+ ...DEFAULT_ARCHIVE_LIMITS,
+ jsonBytes: 1024,
+ });
+ if (path === 'users.json')
+ await expect(reader.discover()).rejects.toMatchObject({
+ code: 'json_limit',
+ });
+ else {
+ await reader.discover();
+ await expect(collect(reader, ['C100'])).rejects.toMatchObject({
+ code: 'json_limit',
+ });
+ }
+ }
+ });
+
+ it('limits total actual selected expansion, including metadata and multibyte text', async () => {
+ const entries = {
+ ...standardEntries(),
+ 'general/2023-11-14.json': [{ ...MESSAGE, text: 'é'.repeat(500) }],
+ };
+ const reader = new ArchiveReader(zipBlob(zipFixture(entries)), {
+ ...DEFAULT_ARCHIVE_LIMITS,
+ selectedBytes: 1000,
+ });
+ await reader.discover();
+ await expect(collect(reader, ['C100'])).rejects.toMatchObject({
+ code: 'selected_limit',
+ });
+ });
+
+ it('enforces the documented default 32 MiB against an actual forged-size expansion', async () => {
+ const path = 'general/2023-11-14.json';
+ const bytes = patchEntry(
+ zipFixture({
+ ...standardEntries(),
+ [path]: 'x'.repeat(DEFAULT_ARCHIVE_LIMITS.jsonBytes + 1),
+ }),
+ path,
+ (header, central) => header.setUint32(central ? 24 : 22, 1, true)
+ );
+ const reader = new ArchiveReader(zipBlob(bytes));
+ await reader.discover();
+ await expect(collect(reader, ['C100'])).rejects.toMatchObject({
+ code: 'json_limit',
+ });
+ });
+
+ it('enforces total expansion even when every advertised day size is smaller', async () => {
+ const path = 'general/2023-11-14.json';
+ const bytes = patchEntry(
+ zipFixture({ ...standardEntries(), [path]: 'x'.repeat(10_000) }),
+ path,
+ (header, central) => header.setUint32(central ? 24 : 22, 1, true)
+ );
+ const reader = new ArchiveReader(zipBlob(bytes), {
+ ...DEFAULT_ARCHIVE_LIMITS,
+ selectedBytes: 1000,
+ });
+ await reader.discover();
+ await expect(collect(reader, ['C100'])).rejects.toMatchObject({
+ code: 'selected_limit',
+ });
+ });
+
+ it('accepts the exact day byte ceiling but rejects one byte above it', async () => {
+ const content = [{ ...MESSAGE, text: 'é'.repeat(1000) }];
+ const length = new TextEncoder().encode(JSON.stringify(content)).length;
+ for (const limit of [length, length - 1]) {
+ const reader = new ArchiveReader(
+ zipBlob(
+ zipFixture({
+ ...standardEntries(),
+ 'general/2023-11-14.json': content,
+ })
+ ),
+ { ...DEFAULT_ARCHIVE_LIMITS, jsonBytes: limit }
+ );
+ await reader.discover();
+ if (limit === length)
+ expect(await collect(reader, ['C100'])).toHaveLength(1);
+ else
+ await expect(collect(reader, ['C100'])).rejects.toMatchObject({
+ code: 'json_limit',
+ });
+ }
+ });
+});
+
+describe('lazy worker and cancellation', () => {
+ it('stages a large multi-conversation archive in two phases with bounded output', async () => {
+ const count = 20_001;
+ const entries: Record<string, unknown> = {
+ 'users.json': [],
+ 'channels.json': ['C100', 'C200'].map((id) => ({
+ id,
+ name: id,
+ members: [],
+ })),
+ };
+ for (const id of ['C100', 'C200']) {
+ entries[`${id}/2023-11-14.json`] = Array.from(
+ { length: count },
+ (_, n) => ({
+ ts: `${1700000000 + n}.000001`,
+ text: 'x'.repeat(512),
+ })
+ );
+ }
+ // Stored ZIP keeps the input large, rather than hiding growth behind compression.
+ const blob = zipBlob(zipFixture(entries, false));
+ expect(blob.size).toBeGreaterThan(20 * 1024 * 1024);
+ const slices = vi.spyOn(blob, 'slice');
+ const whole = vi.spyOn(blob, 'arrayBuffer');
+ const discover = vi.spyOn(ArchiveReader.prototype, 'discover');
+ const history = vi.spyOn(ArchiveReader.prototype, 'readHistory');
+ let records = 0;
+ let parts = 0;
+ let complete = false;
+ let failure: ArchiveWorkerResponse | undefined;
+ const seals: string[] = [];
+ const sessionId = 'large-two-phase';
+ const port: ArchiveWorkerPort = {
+ onmessage: null,
+ postMessage(message) {
+ if (message.type === 'discovered') {
+ send({
+ type: 'read_history',
+ sessionId,
+ selectedIds: ['C100', 'C200'],
+ includeMessageHistory: true,
+ });
+ } else if (message.type === 'part') {
+ // Keep counters, not all output parts: the consumer releases each part.
+ expect(message.bytes.byteLength).toBeLessThanOrEqual(
+ 16 * 1024 * 1024
+ );
+ expect(message.descriptor.recordCount).toBeLessThanOrEqual(20_000);
+ records += message.descriptor.recordCount ?? 0;
+ parts++;
+ send({ type: 'ack_part', sessionId, sequence: message.sequence });
+ } else if (message.type === 'seal') {
+ seals.push(message.seal.slackChannelId);
+ expect(message.seal.partCount).toBe(2);
+ } else if (message.type === 'complete') complete = true;
+ else if (message.type === 'error') failure = message;
+ },
+ };
+ function send(request: ArchiveWorkerRequest): void {
+ // Emulate worker message delivery rather than re-entering its callback.
+ queueMicrotask(() =>
+ port.onmessage?.({
+ data: request,
+ } as MessageEvent<ArchiveWorkerRequest>)
+ );
+ }
+ installArchiveWorker(port);
+ send({ type: 'discover', sessionId, archive: blob });
+ await vi.waitFor(
+ () => {
+ expect(failure).toBeUndefined();
+ expect(complete).toBe(true);
+ },
+ { timeout: 60_000 }
+ );
+ expect(discover).toHaveBeenCalledTimes(1);
+ expect(history).toHaveBeenCalledTimes(1);
+ expect(whole).not.toHaveBeenCalled();
+ expect(
+ slices.mock.calls.every(
+ ([start, end]) => (end ?? 0) - (start ?? 0) <= 65557
+ )
+ ).toBe(true);
+ expect(records).toBe(count * 2);
+ expect(parts).toBe(4);
+ expect(seals.sort()).toEqual(['C100', 'C200']);
+ expect(
+ (await indexedDB.databases()).some(
+ (db) => db.name === `slack-import-scratch:${sessionId}`
+ )
+ ).toBe(false);
+ }, 65_000);
+
+ it('does not construct a worker on module import; the factory creates it on action', async () => {
+ const Worker = vi.fn(function () {});
+ vi.stubGlobal('Worker', Worker);
+ vi.resetModules();
+ await import('./worker-client');
+ expect(Worker).not.toHaveBeenCalled();
+ createSlackImportWorker();
+ expect(Worker).toHaveBeenCalledExactlyOnceWith(expect.any(URL), {
+ type: 'module',
+ });
+ });
+
+ it('cancels a worker during a pending file read, without late discovery or requests', async () => {
+ let release: (() => void) | undefined;
+ const bytes = zipFixture(standardEntries());
+ const blob = {
+ size: bytes.length,
+ slice: () => ({
+ arrayBuffer: async () => {
+ await new Promise<void>((resolve) => {
+ release = resolve;
+ });
+ return new Uint8Array(bytes).buffer;
+ },
+ }),
+ } as unknown as Blob;
+ const worker = workerHarness();
+ worker.send({ type: 'discover', sessionId: 's', archive: blob });
+ expect(release).toBeDefined();
+ worker.send({ type: 'cancel', sessionId: 's' });
+ release?.();
+ await worker.wait('cancelled');
+ expect(worker.messages).toEqual([{ type: 'cancelled', sessionId: 's' }]);
+ });
+
+ it('permits only one unacknowledged part and can cancel while backpressured', async () => {
+ const worker = workerHarness();
+ worker.send({
+ type: 'discover',
+ sessionId: 's',
+ archive: zipBlob(zipFixture(corporateEntries())),
+ });
+ await worker.wait('discovered');
+ worker.send({
+ type: 'read_history',
+ sessionId: 's',
+ selectedIds: ['C200', 'D100'],
+ includeMessageHistory: true,
+ });
+ await worker.wait('part');
+ worker.send({ type: 'ack_part', sessionId: 'other', sequence: 0 });
+ worker.send({ type: 'ack_part', sessionId: 's', sequence: 99 });
+ await new Promise((resolve) => setTimeout(resolve, 20));
+ expect(
+ worker.messages.filter((message) => message.type === 'part')
+ ).toHaveLength(1);
+ worker.send({ type: 'ack_part', sessionId: 's', sequence: 0 });
+ await vi.waitFor(() =>
+ expect(
+ worker.messages.filter((message) => message.type === 'part')
+ ).toHaveLength(2)
+ );
+ worker.send({ type: 'cancel', sessionId: 's' });
+ await worker.wait('cancelled');
+ expect(worker.messages.some((message) => message.type === 'complete')).toBe(
+ false
+ );
+ expect(
+ (await indexedDB.databases()).some(
+ (db) => db.name === 'slack-import-scratch:s'
+ )
+ ).toBe(false);
+ });
+
+ it('reports quota failures only after cleanup and allows a fresh session', async () => {
+ const worker = workerHarness();
+ worker.send({
+ type: 'discover',
+ sessionId: 'quota',
+ archive: zipBlob(zipFixture(standardEntries())),
+ });
+ await worker.wait('discovered');
+ vi.spyOn(IDBObjectStore.prototype, 'put').mockImplementationOnce(() => {
+ throw new DOMException('sensitive provider error', 'QuotaExceededError');
+ });
+ worker.send({
+ type: 'read_history',
+ sessionId: 'quota',
+ selectedIds: ['C100'],
+ includeMessageHistory: true,
+ });
+ await worker.wait('error');
+ expect(worker.messages.at(-1)).toMatchObject({ code: 'storage_quota' });
+ expect(
+ (await indexedDB.databases()).some(
+ (db) => db.name === 'slack-import-scratch:quota'
+ )
+ ).toBe(false);
+ worker.send({
+ type: 'discover',
+ sessionId: 'retry',
+ archive: zipBlob(zipFixture(standardEntries())),
+ });
+ await vi.waitFor(() =>
+ expect(worker.messages.at(-1)).toMatchObject({
+ type: 'discovered',
+ sessionId: 'retry',
+ })
+ );
+ worker.send({ type: 'cancel', sessionId: 'retry' });
+ await worker.wait('cancelled');
+ });
+
+ it('completes shape-only with an empty seal and no parts', async () => {
+ const worker = workerHarness();
+ worker.send({
+ type: 'discover',
+ sessionId: 's',
+ archive: zipBlob(zipFixture(standardEntries())),
+ });
+ await worker.wait('discovered');
+ worker.send({
+ type: 'read_history',
+ sessionId: 's',
+ selectedIds: ['C100'],
+ includeMessageHistory: false,
+ });
+ await worker.wait('complete');
+ expect(worker.messages.map((message) => message.type)).toEqual([
+ 'discovered',
+ 'seal',
+ 'complete',
+ ]);
+ });
+});
diff --git a/apps/web/src/lib/workers/slack-import/archive-reader.ts b/apps/web/src/lib/workers/slack-import/archive-reader.ts
new file mode 100644
index 0000000000..dc70543eb6
--- /dev/null
+++ b/apps/web/src/lib/workers/slack-import/archive-reader.ts
@@ -0,0 +1,467 @@
+import { Inflate } from 'fflate';
+import {
+ type ArchiveDiscovery,
+ ArchiveError,
+ type ArchiveLimits,
+ CONVERSATION_FILES,
+ DEFAULT_ARCHIVE_LIMITS,
+ isFileConversationFolder,
+ parseDay,
+ parseJson,
+ resolveExport,
+ validateArchiveLimits,
+ validateArchivePath,
+} from '../../../features/slack-import/core/export';
+
+const SLICE_BYTES = 64 * 1024;
+// A small compressed push bounds fflate's temporary expansion even for a ZIP bomb.
+const INFLATE_BYTES = 1024;
+const ROOT_FILES = new Set([
+ ...Object.keys(CONVERSATION_FILES),
+ 'users.json',
+ 'org_users.json',
+ 'team.json',
+]);
+const DAY_PATH = /^[^/]+\/\d{4}-\d{2}-\d{2}\.json$/;
+
+export type ArchiveDay = {
+ path: string;
+ slackChannelId: string;
+ expandedBytes: number;
+ records: Record<string, unknown>[];
+};
+type ZipEntry = {
+ path: string;
+ flags: number;
+ method: number;
+ crc: number;
+ compressedBytes: number;
+ expandedBytes: number;
+ localOffset: number;
+ dataOffset: number;
+};
+
+function checkCancelled(signal?: AbortSignal): void {
+ if (signal?.aborted) throw new ArchiveError('cancelled');
+}
+
+async function yieldToCancellation(signal?: AbortSignal): Promise<void> {
+ await new Promise<void>((resolve) => setTimeout(resolve, 0));
+ checkCancelled(signal);
+}
+
+/** Bounded random-access slices; the ZIP itself is never materialized in memory. */
+class ZipSlices {
+ private offset = 0;
+ private buffer = new Uint8Array(0);
+
+ constructor(
+ private readonly blob: Blob,
+ private readonly signal?: AbortSignal
+ ) {}
+
+ async read(offset: number, length: number): Promise<Uint8Array> {
+ checkCancelled(this.signal);
+ if (offset < 0 || length < 0 || offset + length > this.blob.size)
+ throw new ArchiveError('invalid_zip');
+ if (
+ offset < this.offset ||
+ offset + length > this.offset + this.buffer.length
+ ) {
+ this.buffer = new Uint8Array(
+ await this.blob
+ .slice(offset, offset + Math.max(length, SLICE_BYTES))
+ .arrayBuffer()
+ );
+ this.offset = offset;
+ checkCancelled(this.signal);
+ }
+ return this.buffer.subarray(
+ offset - this.offset,
+ offset - this.offset + length
+ );
+ }
+}
+
+function view(bytes: Uint8Array): DataView {
+ return new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
+}
+
+function decodePath(bytes: Uint8Array): string {
+ try {
+ const path = new TextDecoder('utf-8', { fatal: true }).decode(bytes);
+ validateArchivePath(path);
+ return path;
+ } catch (error) {
+ if (error instanceof ArchiveError) throw error;
+ throw new ArchiveError('unsupported_zip');
+ }
+}
+
+function checkExtra(bytes: Uint8Array): void {
+ const data = view(bytes);
+ for (let offset = 0; offset < bytes.length; ) {
+ if (offset + 4 > bytes.length) throw new ArchiveError('invalid_zip');
+ const kind = data.getUint16(offset, true);
+ const size = data.getUint16(offset + 2, true);
+ if (kind === 1) throw new ArchiveError('unsupported_zip');
+ offset += 4 + size;
+ if (offset > bytes.length) throw new ArchiveError('invalid_zip');
+ }
+}
+
+type ZipDirectory = { offset: number; end: number; count: number };
+
+async function readDirectoryLocation(
+ blob: Blob,
+ slices: ZipSlices,
+ limits: ArchiveLimits
+): Promise<ZipDirectory> {
+ const tailOffset = Math.max(0, blob.size - 65557);
+ const tail = await slices.read(tailOffset, blob.size - tailOffset);
+ const end = view(tail);
+ let eocd = tail.length - 22;
+ while (
+ eocd >= 0 &&
+ (end.getUint32(eocd, true) !== 0x06054b50 ||
+ eocd + 22 + end.getUint16(eocd + 20, true) !== tail.length)
+ )
+ eocd--;
+ if (eocd < 0) throw new ArchiveError('invalid_zip');
+ const count = end.getUint16(eocd + 10, true);
+ const directoryBytes = end.getUint32(eocd + 12, true);
+ const directoryOffset = end.getUint32(eocd + 16, true);
+ if (
+ end.getUint16(eocd + 4, true) !== 0 ||
+ end.getUint16(eocd + 6, true) !== 0 ||
+ end.getUint16(eocd + 8, true) !== count ||
+ count === 65535 ||
+ directoryBytes === 0xffffffff ||
+ directoryOffset === 0xffffffff
+ )
+ throw new ArchiveError('unsupported_zip');
+ if (count > limits.zipEntries) throw new ArchiveError('entry_limit');
+ const directoryEnd = directoryOffset + directoryBytes;
+ if (directoryEnd !== tailOffset + eocd) throw new ArchiveError('invalid_zip');
+ return { offset: directoryOffset, end: directoryEnd, count };
+}
+
+async function readDirectoryEntries(
+ slices: ZipSlices,
+ directory: ZipDirectory,
+ signal?: AbortSignal
+): Promise<ZipEntry[]> {
+ const { offset: directoryOffset, end: directoryEnd, count } = directory;
+ const entries: ZipEntry[] = [];
+ const paths = new Set<string>();
+ let offset = directoryOffset;
+ for (let i = 0; i < count; i++) {
+ if (i % 128 === 0) await yieldToCancellation(signal);
+ if (offset + 46 > directoryEnd) throw new ArchiveError('invalid_zip');
+ const header = view(await slices.read(offset, 46));
+ if (header.getUint32(0, true) !== 0x02014b50)
+ throw new ArchiveError('invalid_zip');
+ const flags = header.getUint16(8, true);
+ const method = header.getUint16(10, true);
+ const nameBytes = header.getUint16(28, true);
+ const extraBytes = header.getUint16(30, true);
+ const commentBytes = header.getUint16(32, true);
+ const mode = (header.getUint32(38, true) >>> 16) & 0xf000;
+ if (
+ (flags & ~0x080e) !== 0 ||
+ ![0, 8].includes(method) ||
+ header.getUint16(6, true) > 20 ||
+ header.getUint16(34, true) !== 0 ||
+ ![0, 0x4000, 0x8000].includes(mode)
+ )
+ throw new ArchiveError('unsupported_zip');
+ const nextOffset = offset + 46 + nameBytes + extraBytes + commentBytes;
+ if (nextOffset > directoryEnd) throw new ArchiveError('invalid_zip');
+ const path = decodePath(await slices.read(offset + 46, nameBytes));
+ const canonicalPath = path.replace(/\/$/, '');
+ if (paths.has(canonicalPath)) throw new ArchiveError('duplicate_entry');
+ paths.add(canonicalPath);
+ checkExtra(await slices.read(offset + 46 + nameBytes, extraBytes));
+ const entry: ZipEntry = {
+ path,
+ flags,
+ method,
+ crc: header.getUint32(16, true),
+ compressedBytes: header.getUint32(20, true),
+ expandedBytes: header.getUint32(24, true),
+ localOffset: header.getUint32(42, true),
+ dataOffset: 0,
+ };
+ if (
+ [entry.compressedBytes, entry.expandedBytes, entry.localOffset].includes(
+ 0xffffffff
+ )
+ )
+ throw new ArchiveError('unsupported_zip');
+ entries.push(entry);
+ offset = nextOffset;
+ }
+ if (offset !== directoryEnd) throw new ArchiveError('invalid_zip');
+ return entries;
+}
+
+async function validateLocalEntry(
+ slices: ZipSlices,
+ entry: ZipEntry,
+ previousEnd: number,
+ directoryOffset: number
+): Promise<number> {
+ if (
+ entry.localOffset < previousEnd ||
+ entry.localOffset + 30 > directoryOffset
+ )
+ throw new ArchiveError('invalid_zip');
+ const header = view(await slices.read(entry.localOffset, 30));
+ const nameBytes = header.getUint16(26, true);
+ const extraBytes = header.getUint16(28, true);
+ if (
+ header.getUint32(0, true) !== 0x04034b50 ||
+ header.getUint16(4, true) > 20 ||
+ header.getUint16(6, true) !== entry.flags ||
+ header.getUint16(8, true) !== entry.method
+ )
+ throw new ArchiveError('invalid_zip');
+ entry.dataOffset = entry.localOffset + 30 + nameBytes + extraBytes;
+ previousEnd = entry.dataOffset + entry.compressedBytes;
+ if (previousEnd > directoryOffset) throw new ArchiveError('invalid_zip');
+ if (
+ decodePath(await slices.read(entry.localOffset + 30, nameBytes)) !==
+ entry.path
+ )
+ throw new ArchiveError('invalid_zip');
+ checkExtra(await slices.read(entry.localOffset + 30 + nameBytes, extraBytes));
+ if (entry.flags & 8) {
+ if (previousEnd + 12 > directoryOffset)
+ throw new ArchiveError('invalid_zip');
+ const signature = view(await slices.read(previousEnd, 4)).getUint32(
+ 0,
+ true
+ );
+ if (signature === 0x08074b50) previousEnd += 4;
+ if (previousEnd + 12 > directoryOffset)
+ throw new ArchiveError('invalid_zip');
+ const descriptor = view(await slices.read(previousEnd, 12));
+ if (
+ descriptor.getUint32(0, true) !== entry.crc ||
+ descriptor.getUint32(4, true) !== entry.compressedBytes ||
+ descriptor.getUint32(8, true) !== entry.expandedBytes
+ )
+ throw new ArchiveError('invalid_zip');
+ previousEnd += 12;
+ } else if (
+ header.getUint32(14, true) !== entry.crc ||
+ header.getUint32(18, true) !== entry.compressedBytes ||
+ header.getUint32(22, true) !== entry.expandedBytes
+ )
+ throw new ArchiveError('invalid_zip');
+ return previousEnd;
+}
+
+/** Index central/local headers, never inflate messages or ignored attachment files. */
+async function indexZip(
+ blob: Blob,
+ limits: ArchiveLimits,
+ signal?: AbortSignal
+): Promise<ZipEntry[]> {
+ const slices = new ZipSlices(blob, signal);
+ const directory = await readDirectoryLocation(blob, slices, limits);
+ const entries = await readDirectoryEntries(slices, directory, signal);
+ // Reject overlapping data and local/central disagreement before showing a picker.
+ entries.sort((a, b) => a.localOffset - b.localOffset);
+ let previousEnd = 0;
+ for (const [index, entry] of entries.entries()) {
+ if (index % 128 === 0) await yieldToCancellation(signal);
+ previousEnd = await validateLocalEntry(
+ slices,
+ entry,
+ previousEnd,
+ directory.offset
+ );
+ }
+ return entries;
+}
+
+const CRC_TABLE = Uint32Array.from({ length: 256 }, (_, value) => {
+ for (let bit = 0; bit < 8; bit++)
+ value = (value >>> 1) ^ (value & 1 ? 0xedb88320 : 0);
+ return value;
+});
+
+async function readJsonEntry(
+ blob: Blob,
+ entry: ZipEntry,
+ limits: ArchiveLimits,
+ account: (bytes: number) => void,
+ signal?: AbortSignal
+): Promise<Uint8Array> {
+ if (entry.expandedBytes > limits.jsonBytes)
+ throw new ArchiveError('json_limit');
+ // Deflate may retain unconsumed input (e.g. padding after an end marker).
+ // Bound compressed input too, with generous room for compression overhead.
+ if (entry.compressedBytes > limits.jsonBytes * 2 + SLICE_BYTES)
+ throw new ArchiveError('compressed_limit');
+ const chunks: Uint8Array[] = [];
+ let length = 0;
+ let crc = 0xffffffff;
+ function receive(chunk: Uint8Array): void {
+ length += chunk.length;
+ if (length > limits.jsonBytes) throw new ArchiveError('json_limit');
+ account(chunk.length);
+ for (const byte of chunk) crc = CRC_TABLE[(crc ^ byte) & 255] ^ (crc >>> 8);
+ if (chunk.length) chunks.push(chunk.slice());
+ }
+ const inflate = entry.method === 8 ? new Inflate(receive) : undefined;
+ const slices = new ZipSlices(blob, signal);
+ try {
+ for (
+ let offset = 0;
+ offset < entry.compressedBytes;
+ offset += SLICE_BYTES
+ ) {
+ await yieldToCancellation(signal);
+ const chunk = await slices.read(
+ entry.dataOffset + offset,
+ Math.min(SLICE_BYTES, entry.compressedBytes - offset)
+ );
+ if (!inflate) receive(chunk);
+ else {
+ for (let inner = 0; inner < chunk.length; inner += INFLATE_BYTES) {
+ const end = Math.min(inner + INFLATE_BYTES, chunk.length);
+ inflate.push(
+ chunk.subarray(inner, end),
+ offset + end === entry.compressedBytes
+ );
+ }
+ }
+ }
+ if (inflate && entry.compressedBytes === 0)
+ inflate.push(new Uint8Array(), true);
+ } catch (error) {
+ if (error instanceof ArchiveError) throw error;
+ throw new ArchiveError('invalid_zip');
+ }
+ if (length !== entry.expandedBytes || (crc ^ 0xffffffff) >>> 0 !== entry.crc)
+ throw new ArchiveError('invalid_zip');
+ const bytes = new Uint8Array(length);
+ let offset = 0;
+ for (const chunk of chunks) {
+ bytes.set(chunk, offset);
+ offset += chunk.length;
+ }
+ return bytes;
+}
+
+/** Holds only bounded metadata and ZIP indices between passes, never history. */
+export class ArchiveReader {
+ private discovery: ArchiveDiscovery | undefined;
+ private entries: ZipEntry[] = [];
+ private metadataBytes = 0;
+ private started = false;
+ private historyStarted = false;
+
+ constructor(
+ private readonly blob: Blob,
+ private readonly limits: ArchiveLimits = DEFAULT_ARCHIVE_LIMITS
+ ) {
+ validateArchiveLimits(limits);
+ }
+
+ async discover(signal?: AbortSignal): Promise<ArchiveDiscovery> {
+ if (this.started) throw new ArchiveError('invalid_state');
+ this.started = true;
+ this.entries = await indexZip(this.blob, this.limits, signal);
+ const roots = new Map<string, unknown>();
+ const dayPaths: string[] = [];
+ for (const entry of this.entries) {
+ // File/canvas discussions are not channels. Validate their ZIP headers,
+ // but never inflate or upload their content, just like attachments.
+ if (isFileConversationFolder(entry.path.split('/')[0])) continue;
+ if (ROOT_FILES.has(entry.path)) {
+ const bytes = await readJsonEntry(
+ this.blob,
+ entry,
+ this.limits,
+ (size) => {
+ this.metadataBytes += size;
+ if (this.metadataBytes > this.limits.selectedBytes)
+ throw new ArchiveError('selected_limit');
+ },
+ signal
+ );
+ roots.set(entry.path, parseJson(bytes));
+ } else if (DAY_PATH.test(entry.path)) dayPaths.push(entry.path);
+ else if (
+ /\/\d{4}-\d{2}-\d{2}\.json$/.test(entry.path) &&
+ !entry.path.startsWith('__MACOSX/')
+ ) {
+ // Nested/unknown history layouts must not silently become empty imports.
+ throw new ArchiveError('unresolved_folder');
+ }
+ }
+ this.discovery = resolveExport(roots, dayPaths);
+ const usersBytes = new TextEncoder().encode(
+ JSON.stringify(this.discovery.users)
+ ).length;
+ if (usersBytes > this.limits.jsonBytes)
+ throw new ArchiveError('json_limit');
+ checkCancelled(signal);
+ return this.discovery;
+ }
+
+ /** One pass across ALL selected folders; awaiting the consumer provides backpressure. */
+ async *readHistory(
+ selectedIds: readonly string[],
+ includeMessageHistory: boolean,
+ signal?: AbortSignal
+ ): AsyncGenerator<ArchiveDay> {
+ checkCancelled(signal);
+ if (!this.discovery || this.historyStarted)
+ throw new ArchiveError('invalid_state');
+ const selected = new Set(selectedIds);
+ const known = new Set(
+ this.discovery.conversations.map((item) => item.slackChannelId)
+ );
+ if (
+ !selected.size ||
+ selected.size !== selectedIds.length ||
+ selected.size > this.limits.conversations ||
+ [...selected].some((id) => !known.has(id))
+ )
+ throw new ArchiveError('invalid_selection');
+ this.historyStarted = true;
+ if (!includeMessageHistory) return;
+ const days = new Map(
+ this.discovery.dayEntries
+ .filter((day) => selected.has(day.slackChannelId))
+ .map((day) => [day.path, day.slackChannelId])
+ );
+ let selectedBytes = this.metadataBytes;
+ for (const entry of this.entries) {
+ const slackChannelId = days.get(entry.path);
+ if (!slackChannelId) continue;
+ const bytes = await readJsonEntry(
+ this.blob,
+ entry,
+ this.limits,
+ (size) => {
+ selectedBytes += size;
+ if (selectedBytes > this.limits.selectedBytes)
+ throw new ArchiveError('selected_limit');
+ },
+ signal
+ );
+ yield {
+ path: entry.path,
+ slackChannelId,
+ expandedBytes: bytes.length,
+ records: parseDay(bytes),
+ };
+ checkCancelled(signal);
+ }
+ }
+}
diff --git a/apps/web/src/lib/workers/slack-import/message-store.test.ts b/apps/web/src/lib/workers/slack-import/message-store.test.ts
new file mode 100644
index 0000000000..fa84d7e623
--- /dev/null
+++ b/apps/web/src/lib/workers/slack-import/message-store.test.ts
@@ -0,0 +1,220 @@
+// @vitest-environment node
+import 'fake-indexeddb/auto';
+import { readFileSync } from 'node:fs';
+import { afterEach, describe, expect, it, vi } from 'vitest';
+import {
+ exactTimestamp,
+ MessageStore,
+ normalizeMessage,
+} from './message-store';
+
+const stores: MessageStore[] = [];
+async function open(maxBytes = 2 ** 31): Promise<MessageStore> {
+ const store = await MessageStore.open(crypto.randomUUID(), maxBytes);
+ stores.push(store);
+ return store;
+}
+async function lines(
+ store: MessageStore,
+ conversation = 'C100'
+): Promise<string[]> {
+ const result: string[] = [];
+ for await (const item of store.ordered(conversation)) result.push(item.line);
+ return result;
+}
+
+afterEach(async () => {
+ vi.restoreAllMocks();
+ for (const store of stores.splice(0)) await store.dispose();
+});
+
+describe('exact normalization and disk-backed dedupe', () => {
+ it('normalizes the shared Rust events fixture, strips ignored payloads and skips nonmessages/deletions', async () => {
+ const records = readFileSync(
+ new URL(
+ '../../../../../../crates/slack_integration/tests/fixtures/message-events.ndjson',
+ import.meta.url
+ ),
+ 'utf8'
+ )
+ .trim()
+ .split('\n')
+ .map((line) => JSON.parse(line));
+ const store = await open();
+ await store.stage('C100', records);
+ const result = (await lines(store)).map((line) => JSON.parse(line));
+ expect(result).toHaveLength(7);
+ expect(result[0]).toMatchObject({
+ ts: '1700000000.000001',
+ thread_ts: '1700000000.000001',
+ });
+ expect(result[2]).toMatchObject({
+ ts: '1700086400.000003',
+ text: 'Edited next-day reply',
+ });
+ expect(result[3].thread_ts).toBe('1699999999.999999');
+ expect(JSON.stringify(result)).not.toMatch(
+ /replies|attachments|files|previous_message|unknown/
+ );
+ });
+
+ it('orders exact timestamps of different widths and normalizes equivalent identities', async () => {
+ const store = await open();
+ await store.stage('C100', [
+ { ts: '253402300799.999999' },
+ { ts: '10.000001' },
+ { ts: '9.999999' },
+ { ts: '00010.000001', text: 'winner' },
+ { ts: '10.000002' },
+ { ts: '0.1' },
+ ]);
+ await store.stage('C200', [{ ts: '0.1', text: 'different conversation' }]);
+ expect((await lines(store)).map((line) => JSON.parse(line).ts)).toEqual([
+ '0.100000',
+ '9.999999',
+ '10.000001',
+ '10.000002',
+ '253402300799.999999',
+ ]);
+ expect(await lines(store, 'C200')).toHaveLength(1);
+ });
+
+ it('chooses latest edited.ts, then lexical normalized content, regardless of day order', async () => {
+ const records = [
+ { ts: '1.0', text: 'original' },
+ {
+ type: 'message',
+ subtype: 'message_changed',
+ ts: '100.0',
+ message: { ts: '1.0', edited: { ts: '3.0' }, text: 'z' },
+ },
+ { ts: '1.0', edited: { ts: '4.0' }, text: 'a' },
+ {
+ ts: '1.0',
+ edited: { ts: '4.0' },
+ text: 'b',
+ files: [{ huge: 'ignored' }],
+ },
+ ];
+ const first = await open();
+ const second = await open();
+ for (const record of records) await first.stage('C100', [record]);
+ await second.stage('C100', [...records].reverse());
+ expect(await lines(first)).toEqual(await lines(second));
+ expect(JSON.parse((await lines(first))[0]).text).toBe('b');
+ const reordered = { text: 'b', edited: { ts: '4.0' }, ts: '1.0' };
+ expect(normalizeMessage('C100', reordered)).toEqual(
+ normalizeMessage('C100', records[3])
+ );
+ });
+
+ it.each(['1', '1.0000001', '-1.0', '253402300800.0', '1e2.0', 1.1])(
+ 'rejects invalid timestamp %s',
+ (value) => {
+ expect(() => exactTimestamp(value)).toThrow();
+ }
+ );
+
+ it('rejects malformed messages but ignores deletion identities', () => {
+ for (const record of [
+ {},
+ { ts: '1.0', thread_ts: 1 },
+ { subtype: 'message_changed' },
+ { ts: '1.0', edited: { ts: 'bad' } },
+ ])
+ expect(() => normalizeMessage('C100', record)).toThrow();
+ expect(
+ normalizeMessage('C100', { subtype: 'message_deleted' })
+ ).toBeUndefined();
+ expect(() =>
+ normalizeMessage('C100', { ts: '1.0', text: 'é'.repeat(524288) })
+ ).toThrowError(expect.objectContaining({ code: 'record_limit' }));
+ expect(
+ normalizeMessage('C100', { ts: '1.0', files: ['x'.repeat(2 ** 20)] })
+ ?.byteLength
+ ).toBeLessThan(100);
+ });
+
+ it('pages without missing adjacent microseconds and permits slow asynchronous consumers', async () => {
+ const store = await open();
+ await store.stage(
+ 'C100',
+ Array.from({ length: 300 }, (_, n) => ({
+ ts: `1.${String(300 - n).padStart(6, '0')}`,
+ }))
+ );
+ const result: string[] = [];
+ for await (const message of store.ordered('C100')) {
+ if (result.length % 128 === 0)
+ await new Promise((resolve) => setTimeout(resolve, 5));
+ result.push(JSON.parse(message.line).ts);
+ }
+ expect(result).toHaveLength(300);
+ expect(result).toEqual([...result].sort());
+ expect(new Set(result).size).toBe(300);
+ });
+
+ it('bounds total staged bytes and rolls back an oversized batch', async () => {
+ const store = await open(200);
+ await expect(
+ store.stage('C100', [{ ts: '1.0' }, { ts: '2.0', text: 'x'.repeat(200) }])
+ ).rejects.toMatchObject({ code: 'selected_limit' });
+ expect(await lines(store)).toEqual([]);
+ });
+
+ it('sanitizes quota failures and cleans scratch storage', async () => {
+ const store = await open();
+ await store.stage('C100', [{ ts: '1.0' }]);
+ vi.spyOn(IDBDatabase.prototype, 'transaction').mockImplementationOnce(
+ () => {
+ throw new DOMException('private details', 'QuotaExceededError');
+ }
+ );
+ await expect(store.stage('C100', [{ ts: '2.0' }])).rejects.toMatchObject({
+ code: 'storage_quota',
+ });
+ await store.dispose();
+ });
+
+ it('cleans an abandoned session before reuse and isolates simultaneous sessions', async () => {
+ const id = crypto.randomUUID();
+ const first = await MessageStore.open(id, 10000);
+ const other = await open();
+ await first.stage('C100', [{ ts: '1.0' }]);
+ await other.stage('C100', [{ ts: '2.0' }]);
+ // versionchange closes the old connection, as a terminated worker would.
+ const replacement = await MessageStore.open(id, 10000);
+ expect(await lines(replacement)).toEqual([]);
+ expect(await lines(other)).toHaveLength(1);
+ await replacement.dispose();
+ expect(
+ (await indexedDB.databases()).some(
+ (db) => db.name === `slack-import-scratch:${id}`
+ )
+ ).toBe(false);
+ });
+
+ it('rolls back and reports a synchronous quota failure inside a write callback', async () => {
+ const store = await open();
+ vi.spyOn(IDBObjectStore.prototype, 'put').mockImplementationOnce(() => {
+ throw new DOMException('private details', 'QuotaExceededError');
+ });
+ await expect(store.stage('C100', [{ ts: '1.0' }])).rejects.toMatchObject({
+ code: 'storage_quota',
+ });
+ expect(await lines(store)).toEqual([]);
+ });
+
+ it('cancels staging and cursor consumption', async () => {
+ const store = await open();
+ await store.stage('C100', [{ ts: '1.0' }]);
+ const abort = new AbortController();
+ abort.abort();
+ await expect(
+ store.stage('C100', [{ ts: '2.0' }], abort.signal)
+ ).rejects.toMatchObject({ code: 'cancelled' });
+ await expect(
+ store.ordered('C100', abort.signal).next()
+ ).rejects.toMatchObject({ code: 'cancelled' });
+ });
+});
diff --git a/apps/web/src/lib/workers/slack-import/message-store.ts b/apps/web/src/lib/workers/slack-import/message-store.ts
new file mode 100644
index 0000000000..83da8cccb7
--- /dev/null
+++ b/apps/web/src/lib/workers/slack-import/message-store.ts
@@ -0,0 +1,308 @@
+import { z } from 'zod';
+import { ArchiveError } from '../../../features/slack-import/core/export';
+
+export const MAX_RECORD_BYTES = 1024 * 1024;
+const PAGE_BYTES = 1024 * 1024;
+const PAGE_RECORDS = 128;
+const encoder = new TextEncoder();
+const optionalText = z.string().nullish();
+const userId = z.string().regex(/^[UW][A-Z0-9]{1,63}$/);
+const timestamp = z.string().transform((value) => exactTimestamp(value));
+const messageSchema = z.object({
+ ts: timestamp,
+ thread_ts: timestamp.nullish(),
+ subtype: optionalText,
+ text: z.string().default(''),
+ user: userId.nullish(),
+ user_profile: z
+ .object({
+ email: optionalText,
+ display_name: optionalText,
+ real_name: optionalText,
+ })
+ .nullish(),
+ username: optionalText,
+ bot_profile: z.object({ name: optionalText }).nullish(),
+ reactions: z
+ .array(
+ z.object({
+ name: z.string(),
+ users: z.array(userId).default([]),
+ ts: timestamp.nullish(),
+ })
+ )
+ .default([]),
+});
+
+/** Canonical decimal timestamps match Rust's SlackTimestamp without floating point. */
+export function exactTimestamp(value: unknown): string {
+ if (typeof value !== 'string') throw new ArchiveError('invalid_message');
+ const match = /^(\d{1,12})\.(\d{1,6})$/.exec(value);
+ if (!match || BigInt(match[1]) > 253402300799n)
+ throw new ArchiveError('invalid_message');
+ return `${BigInt(match[1])}.${match[2].padEnd(6, '0')}`;
+}
+
+/** Pad seconds to twelve digits so IndexedDB string ordering is chronological. */
+function sortKey(value: string): string {
+ return value.padStart(19, '0');
+}
+
+function object(value: unknown): Record<string, unknown> {
+ if (!value || typeof value !== 'object' || Array.isArray(value))
+ throw new ArchiveError('invalid_message');
+ return value as Record<string, unknown>;
+}
+
+export type StoredMessage = {
+ conversation: string;
+ time: string;
+ revision: string;
+ line: string;
+ byteLength: number;
+};
+
+/** Whitelist the Rust ExportRecord fields; attachments, blocks and replies never reach disk.
+ * Edits use the inner identity. Missing edited.ts on wrappers falls back to event time.
+ * Equal revisions choose the lexicographically greatest normalized line (not arrival order).
+ */
+export function normalizeMessage(
+ conversation: string,
+ record: Record<string, unknown>
+): StoredMessage | undefined {
+ if (record.type != null && record.type !== 'message') return;
+ const changed = record.subtype === 'message_changed';
+ const source = changed ? object(record.message) : record;
+ if (source.subtype === 'message_deleted' || source.subtype === 'tombstone')
+ return;
+ const parsed = messageSchema.safeParse(source);
+ if (!parsed.success) throw new ArchiveError('invalid_message');
+ const message = parsed.data;
+ let revision = message.ts;
+ if (source.edited != null)
+ revision = exactTimestamp(object(source.edited).ts);
+ else if (changed)
+ revision = exactTimestamp(record.event_ts ?? record.ts ?? message.ts);
+ const line = `${JSON.stringify({ type: 'message', ...message })}\n`;
+ const byteLength = encoder.encode(line).length;
+ if (byteLength > MAX_RECORD_BYTES) throw new ArchiveError('record_limit');
+ return {
+ conversation,
+ time: sortKey(message.ts),
+ revision: sortKey(revision),
+ line,
+ byteLength,
+ };
+}
+
+function storageError(error: unknown): ArchiveError {
+ if (error instanceof ArchiveError) return error;
+ if (error instanceof DOMException && error.name === 'QuotaExceededError')
+ return new ArchiveError('storage_quota');
+ return new ArchiveError('storage_unavailable');
+}
+
+function databaseName(sessionId: string): string {
+ return `slack-import-scratch:${sessionId}`;
+}
+
+/** Also call after Worker.terminate(): a terminated worker cannot execute finally. */
+export async function deleteMessageStore(sessionId: string): Promise<void> {
+ try {
+ await new Promise<void>((resolve, reject) => {
+ const request = indexedDB.deleteDatabase(databaseName(sessionId));
+ request.onsuccess = () => resolve();
+ request.onerror = () => reject(request.error);
+ request.onblocked = () => reject(new ArchiveError('storage_unavailable'));
+ });
+ } catch (error) {
+ throw storageError(error);
+ }
+}
+
+/** One writer per session. Transactions and cursor pages are byte- and count-bounded. */
+export class MessageStore {
+ private totalBytes = 0;
+
+ private constructor(
+ private readonly db: IDBDatabase,
+ private readonly sessionId: string,
+ private readonly maxBytes: number
+ ) {}
+
+ static async open(
+ sessionId: string,
+ maxBytes: number
+ ): Promise<MessageStore> {
+ // Reusing a caller's session after termination must never reuse partial history.
+ await deleteMessageStore(sessionId);
+ try {
+ const db = await new Promise<IDBDatabase>((resolve, reject) => {
+ const request = indexedDB.open(databaseName(sessionId), 1);
+ request.onupgradeneeded = () => {
+ const messages = request.result.createObjectStore('messages', {
+ keyPath: ['conversation', 'time'],
+ });
+ messages.createIndex('conversation', 'conversation');
+ };
+ request.onsuccess = () => resolve(request.result);
+ request.onerror = () => reject(request.error);
+ });
+ db.onversionchange = () => db.close();
+ return new MessageStore(db, sessionId, maxBytes);
+ } catch (error) {
+ throw storageError(error);
+ }
+ }
+
+ async stage(
+ conversation: string,
+ records: readonly Record<string, unknown>[],
+ signal?: AbortSignal
+ ): Promise<void> {
+ let batch: StoredMessage[] = [];
+ let bytes = 0;
+ for (const record of records) {
+ if (signal?.aborted) throw new ArchiveError('cancelled');
+ const message = normalizeMessage(conversation, record);
+ if (!message) continue;
+ if (
+ batch.length &&
+ (batch.length >= PAGE_RECORDS ||
+ bytes + message.byteLength > PAGE_BYTES)
+ ) {
+ await this.writeBatch(batch, signal);
+ batch = [];
+ bytes = 0;
+ }
+ batch.push(message);
+ bytes += message.byteLength;
+ }
+ if (batch.length) await this.writeBatch(batch, signal);
+ }
+
+ private async writeBatch(
+ batch: StoredMessage[],
+ signal?: AbortSignal
+ ): Promise<void> {
+ if (signal?.aborted) throw new ArchiveError('cancelled');
+ try {
+ await new Promise<void>((resolve, reject) => {
+ const tx = this.db.transaction('messages', 'readwrite');
+ const store = tx.objectStore('messages');
+ let bytes = this.totalBytes;
+ let failure: ArchiveError | undefined;
+ function abort(): void {
+ tx.abort();
+ }
+ signal?.addEventListener('abort', abort, { once: true });
+ tx.oncomplete = () => {
+ signal?.removeEventListener('abort', abort);
+ this.totalBytes = bytes;
+ resolve();
+ };
+ tx.onabort = () => {
+ signal?.removeEventListener('abort', abort);
+ reject(
+ signal?.aborted
+ ? new ArchiveError('cancelled')
+ : (failure ?? tx.error)
+ );
+ };
+ // Queue each lookup after the previous write, including duplicates in this batch.
+ let index = 0;
+ const next = (): void => {
+ const message = batch[index++];
+ if (!message) return;
+ const request = store.get([message.conversation, message.time]);
+ request.onsuccess = () => {
+ const existing: StoredMessage | undefined = request.result;
+ if (
+ existing &&
+ (existing.revision > message.revision ||
+ (existing.revision === message.revision &&
+ existing.line >= message.line))
+ ) {
+ next();
+ return;
+ }
+ bytes += message.byteLength - (existing?.byteLength ?? 0);
+ if (bytes > this.maxBytes) {
+ failure = new ArchiveError('selected_limit');
+ tx.abort();
+ return;
+ }
+ try {
+ store.put(message).onsuccess = next;
+ } catch (error) {
+ failure = storageError(error);
+ tx.abort();
+ }
+ };
+ };
+ next();
+ });
+ } catch (error) {
+ throw storageError(error);
+ }
+ }
+
+ private async page(
+ conversation: string,
+ after?: string
+ ): Promise<StoredMessage[]> {
+ try {
+ return await new Promise<StoredMessage[]>((resolve, reject) => {
+ const tx = this.db.transaction('messages', 'readonly');
+ const range = IDBKeyRange.bound(
+ [conversation, after ?? ''],
+ [conversation, '\uffff'],
+ after !== undefined
+ );
+ const request = tx.objectStore('messages').openCursor(range);
+ const page: StoredMessage[] = [];
+ let bytes = 0;
+ request.onsuccess = () => {
+ const cursor = request.result;
+ if (!cursor) return;
+ const message: StoredMessage = cursor.value;
+ if (
+ page.length &&
+ (page.length >= PAGE_RECORDS ||
+ bytes + message.byteLength > PAGE_BYTES)
+ )
+ return;
+ page.push(message);
+ bytes += message.byteLength;
+ cursor.continue();
+ };
+ tx.oncomplete = () => resolve(page);
+ tx.onabort = () => reject(tx.error);
+ });
+ } catch (error) {
+ throw storageError(error);
+ }
+ }
+
+ async *ordered(
+ conversation: string,
+ signal?: AbortSignal
+ ): AsyncGenerator<StoredMessage> {
+ let after: string | undefined;
+ while (true) {
+ if (signal?.aborted) throw new ArchiveError('cancelled');
+ const page = await this.page(conversation, after);
+ if (!page.length) return;
+ for (const message of page) {
+ if (signal?.aborted) throw new ArchiveError('cancelled');
+ yield message;
+ }
+ after = page[page.length - 1].time;
+ }
+ }
+
+ async dispose(): Promise<void> {
+ this.db.close();
+ await deleteMessageStore(this.sessionId);
+ }
+}
diff --git a/apps/web/src/lib/workers/slack-import/ndjson-parts.test.ts b/apps/web/src/lib/workers/slack-import/ndjson-parts.test.ts
new file mode 100644
index 0000000000..54cb8a2f9c
--- /dev/null
+++ b/apps/web/src/lib/workers/slack-import/ndjson-parts.test.ts
@@ -0,0 +1,147 @@
+// @vitest-environment node
+import { createHash } from 'node:crypto';
+import { describe, expect, it } from 'vitest';
+import type { PartLimits } from '../../../features/slack-import/core/worker-protocol';
+import { normalizeMessage, type StoredMessage } from './message-store';
+import {
+ DEFAULT_PART_LIMITS,
+ type NdjsonOutput,
+ ndjsonParts,
+} from './ndjson-parts';
+
+function hash(value: string | Uint8Array): string {
+ return createHash('sha256').update(value).digest('hex');
+}
+async function* messages(
+ count: number,
+ text = 'hello'
+): AsyncGenerator<StoredMessage> {
+ for (let n = 0; n < count; n++) {
+ const message = normalizeMessage('C100', { ts: `${n}.0`, text });
+ if (message) yield message;
+ }
+}
+async function collect(
+ count: number,
+ text = 'hello',
+ limits: PartLimits = DEFAULT_PART_LIMITS
+): Promise<NdjsonOutput[]> {
+ const result = [];
+ for await (const item of ndjsonParts('C100', messages(count, text), limits))
+ result.push(item);
+ return result;
+}
+function parts(
+ output: NdjsonOutput[]
+): Extract<NdjsonOutput, { type: 'part' }>[] {
+ return output.filter((item) => item.type === 'part');
+}
+
+describe('bounded NDJSON parts and canonical manifest', () => {
+ it('seals zero parts with the Rust empty-manifest vector', async () => {
+ expect(await collect(0)).toEqual([
+ {
+ type: 'seal',
+ seal: {
+ slackChannelId: 'C100',
+ partCount: 0,
+ manifestSha256:
+ 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855',
+ },
+ },
+ ]);
+ });
+
+ it('caps records at 20,000 and hashes actual LF-terminated bytes', async () => {
+ const output = await collect(20_001);
+ const result = parts(output);
+ expect(result.map((item) => item.descriptor.recordCount)).toEqual([
+ 20_000, 1,
+ ]);
+ let manifest = '';
+ for (const [index, item] of result.entries()) {
+ expect(item.descriptor.sha256).toBe(hash(item.bytes));
+ expect(item.descriptor.byteLength).toBe(item.bytes.length);
+ expect(item.bytes.at(-1)).toBe(10);
+ manifest += `${index}:${item.descriptor.sha256}:${item.bytes.length}:${item.descriptor.recordCount}\n`;
+ }
+ expect(output.at(-1)).toMatchObject({
+ seal: { partCount: 2, manifestSha256: hash(manifest) },
+ });
+ });
+
+ it('splits at UTF-8 byte boundaries, not character counts, with deterministic repeated output', async () => {
+ const text = '界é';
+ const bytes =
+ normalizeMessage('C100', { ts: '0.0', text })?.byteLength ?? 0;
+ const limits = { ...DEFAULT_PART_LIMITS, partBytes: bytes * 2 - 1 };
+ const output = await collect(3, text, limits);
+ expect(parts(output).map((part) => part.descriptor.recordCount)).toEqual([
+ 1, 1, 1,
+ ]);
+ expect(await collect(3, text, limits)).toEqual(output);
+ const exact = parts(
+ await collect(3, text, { ...limits, partBytes: bytes * 2 })
+ );
+ expect(exact.map((part) => part.descriptor.recordCount)).toEqual([2, 1]);
+ expect(
+ exact
+ .map((part) =>
+ new TextDecoder('utf-8', { fatal: true }).decode(part.bytes)
+ )
+ .join('')
+ ).toContain(text);
+ });
+
+ it('enforces the actual default 16 MiB ceiling with multibyte records', async () => {
+ const output = await collect(18, '界'.repeat(330_000));
+ const result = parts(output);
+ expect(result).toHaveLength(2);
+ expect(result.every((part) => part.bytes.length <= 16 * 1024 * 1024)).toBe(
+ true
+ );
+ expect(result.map((part) => part.descriptor.recordCount)).toEqual([16, 2]);
+ });
+
+ it('enforces per-record bytes including LF and refuses limits above the contract', async () => {
+ const size =
+ normalizeMessage('C100', { ts: '0.0', text: 'hello' })?.byteLength ?? 0;
+ expect(
+ parts(
+ await collect(1, 'hello', { ...DEFAULT_PART_LIMITS, recordBytes: size })
+ )
+ ).toHaveLength(1);
+ await expect(
+ collect(1, 'hello', { ...DEFAULT_PART_LIMITS, recordBytes: size - 1 })
+ ).rejects.toMatchObject({ code: 'record_limit' });
+ await expect(
+ collect(1, 'hello', { ...DEFAULT_PART_LIMITS, partBytes: size - 1 })
+ ).rejects.toMatchObject({ code: 'record_limit' });
+ await expect(
+ collect(1, 'hello', { ...DEFAULT_PART_LIMITS, partRecords: 20_001 })
+ ).rejects.toMatchObject({ code: 'invalid_metadata' });
+ });
+
+ it('does not prefetch beyond one lookahead record while a part is outstanding', async () => {
+ let reads = 0;
+ async function* source(): AsyncGenerator<StoredMessage> {
+ for await (const message of messages(100)) {
+ reads++;
+ yield message;
+ }
+ }
+ const abort = new AbortController();
+ const iterator = ndjsonParts(
+ 'C100',
+ source(),
+ { ...DEFAULT_PART_LIMITS, partRecords: 2 },
+ abort.signal
+ );
+ expect((await iterator.next()).value?.type).toBe('part');
+ await new Promise((resolve) => setTimeout(resolve, 10));
+ expect(reads).toBe(3);
+ abort.abort();
+ await expect(iterator.next()).rejects.toMatchObject({ code: 'cancelled' });
+ expect(reads).toBe(3);
+ });
+});
diff --git a/apps/web/src/lib/workers/slack-import/ndjson-parts.ts b/apps/web/src/lib/workers/slack-import/ndjson-parts.ts
new file mode 100644
index 0000000000..f4bdf6ac9f
--- /dev/null
+++ b/apps/web/src/lib/workers/slack-import/ndjson-parts.ts
@@ -0,0 +1,105 @@
+import { Sha256 } from '@aws-crypto/sha256-js';
+import { ArchiveError } from '../../../features/slack-import/core/export';
+import type {
+ ConversationSeal,
+ PartDescriptor,
+ PartLimits,
+} from '../../../features/slack-import/core/worker-protocol';
+import { MAX_RECORD_BYTES, type StoredMessage } from './message-store';
+
+export const DEFAULT_PART_LIMITS: Readonly<PartLimits> = {
+ partBytes: 16 * 1024 * 1024,
+ partRecords: 20_000,
+ recordBytes: MAX_RECORD_BYTES,
+};
+
+export type NdjsonOutput =
+ | { type: 'part'; descriptor: PartDescriptor; bytes: Uint8Array<ArrayBuffer> }
+ | { type: 'seal'; seal: ConversationSeal };
+
+function hex(bytes: Uint8Array): string {
+ return Array.from(bytes, (byte) => byte.toString(16).padStart(2, '0')).join(
+ ''
+ );
+}
+
+function validateLimits(limits: PartLimits): void {
+ for (const key of ['partBytes', 'partRecords', 'recordBytes'] as const) {
+ if (
+ !Number.isSafeInteger(limits[key]) ||
+ limits[key] <= 0 ||
+ limits[key] > DEFAULT_PART_LIMITS[key]
+ )
+ throw new ArchiveError('invalid_metadata');
+ }
+}
+
+/** One bounded part plus the cursor's bounded page, never an array of parts/descriptors.
+ * Resuming the iterator is the consumer's acknowledgement; no prefetch runs during yield.
+ * The incremental manifest hash follows ConversationSeal::from_descriptors in Rust.
+ */
+export async function* ndjsonParts(
+ slackChannelId: string,
+ messages: AsyncIterable<StoredMessage>,
+ limits: PartLimits = DEFAULT_PART_LIMITS,
+ signal?: AbortSignal
+): AsyncGenerator<NdjsonOutput> {
+ validateLimits(limits);
+ const encoder = new TextEncoder();
+ const manifest = new Sha256();
+ let buffer = new Uint8Array(0);
+ let length = 0;
+ let count = 0;
+ let partIndex = 0;
+
+ async function finishPart(): Promise<
+ Extract<NdjsonOutput, { type: 'part' }>
+ > {
+ const bytes = buffer.slice(0, length);
+ const sha256 = hex(
+ new Uint8Array(await crypto.subtle.digest('SHA-256', bytes))
+ );
+ if (signal?.aborted) throw new ArchiveError('cancelled');
+ const descriptor: PartDescriptor = {
+ upload: { kind: 'conversation_part', slackChannelId, partIndex },
+ sha256,
+ byteLength: length,
+ recordCount: count,
+ };
+ manifest.update(`${partIndex}:${sha256}:${length}:${count}\n`);
+ partIndex++;
+ length = 0;
+ count = 0;
+ return { type: 'part', descriptor, bytes };
+ }
+
+ for await (const message of messages) {
+ if (signal?.aborted) throw new ArchiveError('cancelled');
+ const bytes = encoder.encode(message.line);
+ if (bytes.length > limits.recordBytes || bytes.length > limits.partBytes)
+ throw new ArchiveError('record_limit');
+ if (
+ count &&
+ (count === limits.partRecords || length + bytes.length > limits.partBytes)
+ ) {
+ yield await finishPart();
+ if (signal?.aborted) throw new ArchiveError('cancelled');
+ }
+ if (!buffer.length) buffer = new Uint8Array(limits.partBytes);
+ buffer.set(bytes, length);
+ length += bytes.length;
+ count++;
+ }
+ if (signal?.aborted) throw new ArchiveError('cancelled');
+ if (count) yield await finishPart();
+ buffer = new Uint8Array(0);
+ if (signal?.aborted) throw new ArchiveError('cancelled');
+ yield {
+ type: 'seal',
+ seal: {
+ slackChannelId,
+ partCount: partIndex,
+ manifestSha256: hex(manifest.digestSync()),
+ },
+ };
+}
diff --git a/apps/web/src/lib/workers/slack-import/unzip-worker.ts b/apps/web/src/lib/workers/slack-import/unzip-worker.ts
new file mode 100644
index 0000000000..3fe56a47ef
--- /dev/null
+++ b/apps/web/src/lib/workers/slack-import/unzip-worker.ts
@@ -0,0 +1,191 @@
+import { match } from 'ts-pattern';
+import {
+ ArchiveError,
+ DEFAULT_ARCHIVE_LIMITS,
+} from '../../../features/slack-import/core/export';
+import type {
+ ArchiveWorkerPort,
+ ArchiveWorkerRequest,
+} from '../../../features/slack-import/core/worker-protocol';
+import { ArchiveReader } from './archive-reader';
+import { deleteMessageStore, MessageStore } from './message-store';
+import { ndjsonParts } from './ndjson-parts';
+
+type Session = {
+ id: string;
+ reader: ArchiveReader;
+ abort: AbortController;
+ phase: 'discovering' | 'discovered' | 'reading';
+ sequence: number;
+ selectedBytes: number;
+ acknowledge?: () => void;
+};
+
+/** Kept injectable so worker lifetime/cancellation can be tested without app services. */
+export function installArchiveWorker(port: ArchiveWorkerPort): void {
+ let session: Session | undefined;
+
+ function reportError(id: string, error: unknown): void {
+ const failure =
+ error instanceof ArchiveError ? error : new ArchiveError('invalid_zip');
+ if (failure.code === 'cancelled')
+ port.postMessage({ type: 'cancelled', sessionId: id });
+ else
+ port.postMessage({
+ type: 'error',
+ sessionId: id,
+ code: failure.code,
+ message: failure.message,
+ });
+ }
+
+ async function discover(
+ request: Extract<ArchiveWorkerRequest, { type: 'discover' }>
+ ): Promise<void> {
+ if (session) {
+ reportError(request.sessionId, new ArchiveError('invalid_state'));
+ return;
+ }
+ try {
+ const current: Session = {
+ id: request.sessionId,
+ reader: new ArchiveReader(request.archive, request.limits),
+ abort: new AbortController(),
+ phase: 'discovering',
+ sequence: 0,
+ selectedBytes:
+ request.limits?.selectedBytes ?? DEFAULT_ARCHIVE_LIMITS.selectedBytes,
+ };
+ session = current;
+ const discovery = await current.reader.discover(current.abort.signal);
+ current.selectedBytes -= new TextEncoder().encode(
+ JSON.stringify(discovery.users)
+ ).length;
+ if (current.selectedBytes < 0) throw new ArchiveError('selected_limit');
+ current.phase = 'discovered';
+ port.postMessage({
+ type: 'discovered',
+ sessionId: current.id,
+ discovery,
+ });
+ } catch (error) {
+ session = undefined;
+ reportError(request.sessionId, error);
+ }
+ }
+
+ async function readHistory(
+ request: Extract<ArchiveWorkerRequest, { type: 'read_history' }>
+ ): Promise<void> {
+ if (
+ !session ||
+ session.id !== request.sessionId ||
+ session.phase !== 'discovered'
+ ) {
+ reportError(request.sessionId, new ArchiveError('invalid_state'));
+ return;
+ }
+ const current = session;
+ current.phase = 'reading';
+ let store: MessageStore | undefined;
+ let failure: unknown;
+ try {
+ store = await MessageStore.open(current.id, current.selectedBytes);
+ for await (const day of current.reader.readHistory(
+ request.selectedIds,
+ request.includeMessageHistory,
+ current.abort.signal
+ )) {
+ await store.stage(
+ day.slackChannelId,
+ day.records,
+ current.abort.signal
+ );
+ }
+ for (const id of [...request.selectedIds].sort()) {
+ for await (const output of ndjsonParts(
+ id,
+ store.ordered(id, current.abort.signal),
+ request.partLimits,
+ current.abort.signal
+ )) {
+ if (output.type === 'seal') {
+ port.postMessage({ ...output, sessionId: current.id });
+ continue;
+ }
+ await new Promise<void>((resolve, reject) => {
+ function abort(): void {
+ current.acknowledge = undefined;
+ reject(new ArchiveError('cancelled'));
+ }
+ if (current.abort.signal.aborted) {
+ abort();
+ return;
+ }
+ current.abort.signal.addEventListener('abort', abort, {
+ once: true,
+ });
+ current.acknowledge = () => {
+ current.abort.signal.removeEventListener('abort', abort);
+ current.acknowledge = undefined;
+ resolve();
+ };
+ port.postMessage(
+ {
+ ...output,
+ sessionId: current.id,
+ sequence: current.sequence,
+ },
+ [output.bytes.buffer]
+ );
+ });
+ current.sequence++;
+ }
+ }
+ } catch (error) {
+ failure = error;
+ } finally {
+ try {
+ if (store) await store.dispose();
+ else await deleteMessageStore(current.id);
+ } catch (error) {
+ failure ??= error;
+ }
+ session = undefined;
+ }
+ if (current.abort.signal.aborted) failure = new ArchiveError('cancelled');
+ if (failure) reportError(current.id, failure);
+ else port.postMessage({ type: 'complete', sessionId: current.id });
+ }
+
+ port.onmessage = ({ data }) => {
+ match(data)
+ .with({ type: 'discover' }, (request) => {
+ void discover(request);
+ })
+ .with({ type: 'read_history' }, (request) => {
+ void readHistory(request);
+ })
+ .with({ type: 'ack_part' }, (request) => {
+ if (
+ session?.id === request.sessionId &&
+ session.sequence === request.sequence
+ )
+ session.acknowledge?.();
+ })
+ .with({ type: 'cancel' }, (request) => {
+ if (session?.id !== request.sessionId) return;
+ session.abort.abort();
+ if (session.phase === 'discovered') {
+ session = undefined;
+ port.postMessage({ type: 'cancelled', sessionId: request.sessionId });
+ }
+ })
+ .exhaustive();
+ };
+}
+
+// Worker entry point only: importing in a browser window does not install a handler.
+if (typeof self !== 'undefined' && typeof document === 'undefined') {
+ installArchiveWorker(self);
+}
diff --git a/apps/web/src/lib/workers/slack-import/worker-client.ts b/apps/web/src/lib/workers/slack-import/worker-client.ts
new file mode 100644
index 0000000000..0c29506566
--- /dev/null
+++ b/apps/web/src/lib/workers/slack-import/worker-client.ts
@@ -0,0 +1,19 @@
+import { deleteMessageStore } from './message-store';
+
+/** Await on owner disposal; Worker.terminate() alone cannot clean IndexedDB. */
+export async function disposeSlackImportWorker(
+ worker: Worker,
+ sessionId: string
+): Promise<void> {
+ worker.terminate();
+ await deleteMessageStore(sessionId);
+}
+
+/** Call from the file-picker action only; importing this module starts no worker.
+ * Keep construction outside the worker's import graph to avoid recursive bundling.
+ */
+export function createSlackImportWorker(): Worker {
+ return new Worker(new URL('./unzip-worker.ts', import.meta.url), {
+ type: 'module',
+ });
+}
diff --git a/apps/web/tests/e2e/fixtures/slack-import.html b/apps/web/tests/e2e/fixtures/slack-import.html
new file mode 100644
index 0000000000..b340cf94da
--- /dev/null
+++ b/apps/web/tests/e2e/fixtures/slack-import.html
@@ -0,0 +1,5 @@
+<!doctype html>
+<html lang="en">
+ <head><meta charset="UTF-8"><title>Slack import selection fixture</title></head>
+ <body><div id="root"></div><script type="module" src="./slack-import.tsx"></script></body>
+</html>
diff --git a/apps/web/tests/e2e/fixtures/slack-import.tsx b/apps/web/tests/e2e/fixtures/slack-import.tsx
new file mode 100644
index 0000000000..2331eac702
--- /dev/null
+++ b/apps/web/tests/e2e/fixtures/slack-import.tsx
@@ -0,0 +1,158 @@
+import { createSignal, Suspense } from 'solid-js';
+import { render } from 'solid-js/web';
+import type {
+ ImportCommands,
+ ImportJob,
+ ImportLimits,
+} from '../../../src/features/slack-import/context/contracts';
+import { ImportProvider } from '../../../src/features/slack-import/context/import-context';
+import { DEFAULT_ARCHIVE_LIMITS } from '../../../src/features/slack-import/core/export';
+import { createArchiveSource } from '../../../src/features/slack-import/queries/archive-source';
+import { ImportDialog } from '../../../src/features/slack-import/views/import-dialog';
+
+// Real dialog/controller/ZIP worker, deterministic fake server. Serialization and
+// server authorization are independently covered by adapter and PostgreSQL tests.
+const limits: ImportLimits = {
+ ...DEFAULT_ARCHIVE_LIMITS,
+ partBytes: 16777216,
+ partRecords: 20000,
+ recordBytes: 1048576,
+ registrationBatch: 50,
+};
+const saved = localStorage.getItem('slack-test-job');
+const [job, setJob] = createSignal<ImportJob | undefined>(
+ saved ? JSON.parse(saved) : undefined
+);
+function record(kind: string, body: unknown): void {
+ const writes = JSON.parse(localStorage.getItem('slack-test-writes') ?? '[]');
+ writes.push({ kind, body });
+ localStorage.setItem('slack-test-writes', JSON.stringify(writes));
+}
+function persist(receipt: ImportJob): ImportJob {
+ const next = { ...receipt, revision: receipt.revision + 1 };
+ localStorage.setItem('slack-test-job', JSON.stringify(next));
+ setJob(next);
+ return next;
+}
+const commands: ImportCommands = {
+ async create(teamId, request) {
+ record('create', request);
+ return persist({
+ teamId,
+ jobId: crypto.randomUUID(),
+ source: request.source,
+ includeMessageHistory: request.includeMessageHistory,
+ status: 'uploading',
+ revision: 0,
+ createdAt: '2026-09-30T00:00:00Z',
+ updatedAt: '2026-09-30T00:00:00Z',
+ registrationClosed: false,
+ usersVerified: false,
+ limits,
+ conversations: request.conversations.map((metadata) => ({
+ slackChannelId: metadata.slackChannelId,
+ name: metadata.name,
+ kind: metadata.kind,
+ archived: metadata.archived,
+ channelId: undefined,
+ status: 'awaiting_uploads',
+ counters: {
+ processed: 0,
+ imported: 0,
+ skipped: 0,
+ duplicates: 0,
+ reactions: 0,
+ },
+ verifiedParts: 0,
+ partCount: undefined,
+ searchStatus: 'not_needed',
+ error: undefined,
+ warnings: [],
+ })),
+ });
+ },
+ async register(_scope, descriptors) {
+ record('register', descriptors);
+ return descriptors.map((descriptor) => ({
+ descriptor,
+ expiresAt: '2099-01-01T00:00:00Z',
+ async put() {
+ return 'uploaded' as const;
+ },
+ }));
+ },
+ async complete(_scope, uploads, seal) {
+ record('complete', { uploads, seal });
+ return persist({
+ ...job()!,
+ usersVerified:
+ job()!.usersVerified ||
+ uploads.some((upload) => upload.kind === 'users'),
+ conversations: job()!.conversations.map((conversation) => ({
+ ...conversation,
+ partCount:
+ seal?.slackChannelId === conversation.slackChannelId
+ ? seal.partCount
+ : conversation.partCount,
+ })),
+ });
+ },
+ async finalize() {
+ record('finalize', {});
+ return persist({
+ ...job()!,
+ status: 'completed_with_errors',
+ registrationClosed: true,
+ conversations: job()!.conversations.map((conversation) => ({
+ ...conversation,
+ status: conversation.slackChannelId === 'CC' ? 'failed' : 'completed',
+ error:
+ conversation.slackChannelId === 'CC' ? 'invalid_input' : undefined,
+ })),
+ });
+ },
+ async cancel() {
+ record('cancel', {});
+ return persist({
+ ...job()!,
+ status: 'cancelled',
+ registrationClosed: true,
+ });
+ },
+};
+
+render(
+ () => (
+ <Suspense>
+ <ImportProvider
+ context={{
+ createSource: (inputs) => ({
+ commands,
+ source: {
+ page: () => ({
+ jobs: job() ? [job()!] : [],
+ limits,
+ sourceBinding: { kind: 'unbound' },
+ nextCursor: undefined,
+ }),
+ job: () => (job()?.jobId === inputs.jobId() ? job() : undefined),
+ isLoading: () => false,
+ error: () => undefined,
+ async refresh() {},
+ },
+ }),
+ createArchive: createArchiveSource,
+ protectFile: () => () => {},
+ newToken: () => crypto.randomUUID(),
+ }}
+ >
+ <ImportDialog
+ teamId="team"
+ onCompleted={async () => {}}
+ channelHref={() => undefined}
+ />
+ </ImportProvider>
+ </Suspense>
+ ),
+ document.getElementById('root')!
+);
diff --git a/apps/web/tests/e2e/slack-import.spec.ts b/apps/web/tests/e2e/slack-import.spec.ts
new file mode 100644
index 0000000000..a193f87b22
--- /dev/null
+++ b/apps/web/tests/e2e/slack-import.spec.ts
@@ -0,0 +1,399 @@
+import { expect, type Request, test } from '@playwright/test';
+import { strToU8, zipSync } from 'fflate';
+import type { ImportProgress } from '../../src/lib/service-clients/service-storage/generated/schemas/importProgress';
+import type { SlackCreateRequest } from '../../src/lib/service-clients/service-storage/generated/schemas/slackCreateRequest';
+import type { SlackRegisterRequest } from '../../src/lib/service-clients/service-storage/generated/schemas/slackRegisterRequest';
+
+function archive(): Buffer {
+ const channels = ['CA', 'CB', 'CC'].map((id) => ({
+ id,
+ folder: id,
+ name: id === 'CB' ? 'unselected' : 'same name',
+ members: ['U1'],
+ created: 1700000000,
+ is_archived: id === 'CC',
+ }));
+ return Buffer.from(
+ zipSync({
+ 'channels.json': strToU8(JSON.stringify(channels)),
+ 'users.json': strToU8(
+ JSON.stringify([
+ { id: 'U1', name: 'one', profile: { email: '[email protected]' } },
+ { id: 'U2', name: 'ArchiveOnlyZebraAuthor' },
+ ])
+ ),
+ ...Object.fromEntries(
+ ['CA', 'CB', 'CC'].map((id) => [
+ `${id}/2023-11-14.json`,
+ strToU8(
+ JSON.stringify([
+ {
+ type: 'message',
+ user: 'U2',
+ ts: '1700000000.000001',
+ text: 'Reference <#CB|unselected> and <#CC|same name>. Visit <https://example.com/|External example>.',
+ reactions: [{ name: 'thumbsup', users: ['U1'], count: 1 }],
+ },
+ {
+ type: 'message',
+ user: 'U1',
+ ts: '1700000001.000001',
+ thread_ts: '1700000000.000001',
+ text: 'Historical reply with literal `<m-link>{"url":"javascript:alert(1)"}</m-link>`.',
+ },
+ ])
+ ),
+ ])
+ ),
+ })
+ );
+}
+
+// No hosted writes: the fixture injects a fake durable server but uses the real
+// view, controller, lazy ZIP worker and IndexedDB cleanup in a Chromium browser.
+for (const history of [false, true]) {
+ test(`popup confirmation is immutable and reload tracking needs no ZIP (history=${history})`, async ({
+ page,
+ }) => {
+ await page.goto('/tests/e2e/fixtures/slack-import.html');
+ const opener = page.getByRole('button', { name: 'Import from Slack' });
+ await opener.click();
+ const file = page.getByLabel('Slack export ZIP');
+ await expect(file).toBeFocused();
+ await file.setInputFiles({
+ name: 'slack.zip',
+ mimeType: 'application/zip',
+ buffer: archive(),
+ });
+ await expect(page.getByLabel('Filter conversations')).toBeVisible();
+ await expect(page.getByLabel('Filter conversations')).toBeFocused();
+ expect(
+ await page.evaluate(() => localStorage.getItem('slack-test-writes'))
+ ).toBeNull();
+ await page.keyboard.press('Escape');
+ await expect(opener).toBeFocused();
+ await opener.click();
+ await expect(file).toBeFocused();
+ await expect(page.getByLabel('Filter conversations')).toHaveCount(0);
+ expect(
+ await page.evaluate(() => localStorage.getItem('slack-test-writes'))
+ ).toBeNull();
+
+ await file.setInputFiles({
+ name: 'slack.zip',
+ mimeType: 'application/zip',
+ buffer: archive(),
+ });
+ await page.getByLabel(/I confirm this archive/).check();
+ await expect(
+ page.getByRole('button', { name: 'Import selected channels (0)' })
+ ).toBeDisabled();
+ await page.getByLabel('Show archived conversations').check();
+ for (const id of ['CA', 'CC']) {
+ await page.getByLabel('Filter conversations').fill(id);
+ const all = page.getByLabel('Select all visible conversations');
+ await all.focus();
+ await page.keyboard.press('Space');
+ }
+ await page.getByLabel('Filter conversations').fill('CB');
+ await expect(
+ page.getByLabel('Select all visible conversations')
+ ).not.toBeChecked();
+ await expect(
+ page.getByRole('button', { name: 'Clear visible selection' })
+ ).toBeDisabled();
+ await page.getByLabel('Select all visible conversations').check();
+ await page.getByRole('button', { name: 'Clear visible selection' }).click();
+ await expect(page.getByText('2 selected.', { exact: false })).toBeVisible();
+ if (!history) await page.getByLabel('Include message history').uncheck();
+ await page
+ .getByRole('button', { name: 'Import selected channels (2)' })
+ .click();
+ await expect(
+ page.getByText('completed with errors', { exact: true })
+ ).toBeVisible();
+ const writes = await page.evaluate(() =>
+ JSON.parse(localStorage.getItem('slack-test-writes') ?? '[]')
+ );
+ const creates = writes.filter(
+ (write: { kind: string }) => write.kind === 'create'
+ );
+ expect(creates).toHaveLength(1);
+ expect(
+ creates[0].body.conversations.map(
+ (conversation: { slackChannelId: string }) =>
+ conversation.slackChannelId
+ )
+ ).toEqual(['CA', 'CC']);
+ expect(creates[0].body.includeMessageHistory).toBe(history);
+ const registrations = writes
+ .filter((write: { kind: string }) => write.kind === 'register')
+ .flatMap(
+ (write: {
+ body: { upload: { kind: string; slackChannelId?: string } }[];
+ }) => write.body.map((descriptor) => descriptor.upload)
+ );
+ expect(registrations).toEqual(
+ expect.arrayContaining(
+ history
+ ? [
+ { kind: 'users' },
+ { kind: 'conversation_part', slackChannelId: 'CA', partIndex: 0 },
+ { kind: 'conversation_part', slackChannelId: 'CC', partIndex: 0 },
+ ]
+ : [{ kind: 'users' }]
+ )
+ );
+ expect(registrations).toHaveLength(history ? 3 : 1);
+ await page.reload();
+ await opener.click();
+ await page
+ .getByRole('button', { name: /2026-09-30.*completed with errors/ })
+ .click();
+ await expect(
+ page.getByText(
+ `2 selected · ${history ? 'With' : 'Without'} message history`
+ )
+ ).toBeVisible();
+ await expect(
+ page.getByRole('heading', { name: 'same name', exact: true })
+ ).toHaveCount(2);
+ await expect(page.getByText('Error: invalid input')).toBeVisible();
+ await expect(page.getByText('unselected', { exact: true })).toHaveCount(0);
+ await expect(page.getByLabel('Filter conversations')).toHaveCount(0);
+ expect(
+ await page.evaluate(() =>
+ JSON.parse(localStorage.getItem('slack-test-writes') ?? '[]')
+ )
+ ).toEqual(writes);
+ });
+}
+
+// Deliberately opt-in: this exercises production Settings/adapters and writes to
+// an isolated local team. The fixture tests above do not prove HTTP persistence.
+test.describe('local backend selection contract', () => {
+ test.skip(
+ process.env.SLACK_IMPORT_LIVE_E2E !== 'true',
+ 'Requires a local admin team, backend/worker admission and the frontend flag'
+ );
+
+ for (const history of [false, true]) {
+ test(`actual create/upload receipt survives reload (history=${history})`, async ({
+ page,
+ baseURL,
+ }) => {
+ test.setTimeout(180_000);
+ expect(process.env.LOCAL_E2E).toBe('true');
+ for (const origin of [baseURL, process.env.LOCAL_E2E_BACKEND_ORIGIN]) {
+ expect(origin, 'An isolated local origin is required').toBeTruthy();
+ expect(new URL(origin!).hostname).toMatch(
+ /^(localhost|127\.0\.0\.1|[a-z0-9-]+\.localhost)$/
+ );
+ }
+ const writes: Request[] = [];
+ const workers: string[] = [];
+ page.on('worker', (worker) => workers.push(worker.url()));
+ page.on('request', (request) => {
+ if (
+ (request.method() === 'POST' &&
+ request.url().includes('/slack/imports')) ||
+ (request.method() === 'PUT' &&
+ request.url().includes('slack-import/'))
+ )
+ writes.push(request);
+ });
+ await page.goto('/app/settings/team');
+ const opener = page.getByRole('button', { name: 'Import from Slack' });
+ await opener.click();
+ const file = page.getByLabel('Slack export ZIP');
+ await expect(file).toBeVisible();
+ expect(
+ workers.filter((url) => url.includes('unzip-worker'))
+ ).toHaveLength(0);
+ const zip = {
+ name: 'synthetic.zip',
+ mimeType: 'application/zip',
+ buffer: archive(),
+ };
+ await file.setInputFiles(zip);
+ await expect(page.getByLabel('Filter conversations')).toBeVisible();
+ await page.keyboard.press('Escape');
+ await expect(opener).toBeFocused();
+ expect(writes).toHaveLength(0);
+ await opener.click();
+ await file.setInputFiles(zip);
+ await page.getByLabel(/I confirm this archive/).check();
+ await page.getByLabel('Show archived conversations').check();
+ for (const id of ['CA', 'CC']) {
+ await page.getByLabel('Filter conversations').fill(id);
+ await page.getByLabel('Select all visible conversations').check();
+ }
+ await page.getByLabel('Filter conversations').fill('CB');
+ await expect(
+ page.getByRole('button', { name: 'Clear visible selection' })
+ ).toBeDisabled();
+ if (!history) await page.getByLabel('Include message history').uncheck();
+ const created = page.waitForResponse(
+ (response) =>
+ response.request().method() === 'POST' &&
+ new URL(response.url()).pathname.endsWith('/slack/imports')
+ );
+ const finalized = page.waitForResponse(
+ (response) =>
+ response.request().method() === 'POST' &&
+ new URL(response.url()).pathname.endsWith('/finalize'),
+ { timeout: 30_000 }
+ );
+ await page
+ .getByRole('button', { name: 'Import selected channels (2)' })
+ .click();
+ const response = await created;
+ expect(response.ok()).toBe(true);
+ const request = response.request();
+ const body: SlackCreateRequest = request.postDataJSON();
+ expect(
+ body.conversations.map((item) => item.slackChannelId).sort()
+ ).toEqual(['CA', 'CC']);
+ expect(body.includeMessageHistory).toBe(history);
+ const receipt: ImportProgress = await response.json();
+ expect(
+ receipt.conversations.map((item) => item.slackChannelId).sort()
+ ).toEqual(['CA', 'CC']);
+ expect(receipt.includeMessageHistory).toBe(history);
+ expect((await finalized).ok()).toBe(true);
+ const registrations = writes.filter((request) =>
+ request.url().endsWith('/uploads')
+ );
+ const descriptors = registrations.flatMap(
+ (request) =>
+ (request.postDataJSON() as SlackRegisterRequest).descriptors
+ );
+ expect(
+ descriptors
+ .filter((item) => item.upload.kind === 'conversation_part')
+ .map((item) =>
+ item.upload.kind === 'conversation_part'
+ ? item.upload.slackChannelId
+ : ''
+ )
+ .sort()
+ ).toEqual(history ? ['CA', 'CC'] : []);
+ // Real PUTs must have succeeded for finalization to be reached: this covers
+ // browser signed-header/CORS behavior rather than a fake upload callback.
+ const puts = writes.filter((request) => request.method() === 'PUT');
+ expect(puts).toHaveLength(history ? 3 : 1);
+ for (const put of puts) {
+ // Docker-only hostnames must never escape into browser grants. Inspect
+ // only the hostname, not the signed URL (which must not enter reports).
+ expect(new URL(put.url()).hostname).toMatch(
+ /^(localhost|127\.0\.0\.1)$/
+ );
+ const signedHeaders = await put.allHeaders();
+ expect(signedHeaders['if-none-match']).toBe('*');
+ expect(signedHeaders['x-amz-checksum-sha256']).toBeTruthy();
+ expect((await put.response())?.ok()).toBe(true);
+ }
+ const headers = await request.allHeaders();
+ const auth = headers.authorization
+ ? { authorization: headers.authorization }
+ : {};
+ const detailURL = `${response.url()}/${receipt.jobId}`;
+ const persisted = await page.request.get(detailURL, { headers: auth });
+ expect(persisted.ok()).toBe(true);
+ const stored: ImportProgress = await persisted.json();
+ expect(
+ stored.conversations.map((item) => item.slackChannelId).sort()
+ ).toEqual(['CA', 'CC']);
+ expect(stored.includeMessageHistory).toBe(history);
+ await page.reload();
+ await opener.click();
+ await page
+ .getByRole('region', { name: 'Job history' })
+ .getByRole('button')
+ .filter({ hasText: receipt.createdAt })
+ .click();
+ await expect(
+ page.getByText(
+ `2 selected · ${history ? 'With' : 'Without'} message history`
+ )
+ ).toBeVisible();
+ await expect(
+ page.getByRole('heading', { name: 'same name', exact: true })
+ ).toHaveCount(2);
+ await expect(page.getByText('unselected', { exact: true })).toHaveCount(
+ 0
+ );
+
+ // Test tampering while registration is OPEN, not merely a closed-job error.
+ const tamper = await page.request.post(response.url(), {
+ headers: auth,
+ data: { ...body, idempotencyToken: crypto.randomUUID() },
+ });
+ expect(tamper.ok()).toBe(true);
+ const tamperJob: ImportProgress = await tamper.json();
+ const tamperURL = `${response.url()}/${tamperJob.jobId}`;
+ try {
+ const registration = await page.request.post(`${tamperURL}/uploads`, {
+ headers: auth,
+ data: {
+ descriptors: [
+ {
+ upload: {
+ kind: 'conversation_part',
+ slackChannelId: 'CB',
+ partIndex: 0,
+ },
+ sha256: 'a'.repeat(64),
+ byteLength: 3,
+ recordCount: 1,
+ },
+ ],
+ },
+ });
+ expect(registration.status()).toBeGreaterThanOrEqual(400);
+ expect(registration.status()).toBeLessThan(500);
+ const seal = await page.request.post(`${tamperURL}/uploads/complete`, {
+ headers: auth,
+ data: {
+ uploads: [],
+ seal: {
+ slackChannelId: 'CB',
+ partCount: 0,
+ manifestSha256:
+ 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855',
+ },
+ },
+ });
+ expect(seal.status()).toBeGreaterThanOrEqual(400);
+ expect(seal.status()).toBeLessThan(500);
+ } finally {
+ expect(
+ (
+ await page.request.post(`${tamperURL}/cancel`, { headers: auth })
+ ).ok()
+ ).toBe(true);
+ }
+ let completed: ImportProgress | undefined;
+ await expect
+ .poll(
+ async () => {
+ const response = await page.request.get(detailURL, {
+ headers: auth,
+ });
+ expect(response.ok()).toBe(true);
+ completed = await response.json();
+ return completed?.status;
+ },
+ { timeout: 90_000, intervals: [1000] }
+ )
+ .toBe('completed');
+ expect(
+ completed?.conversations.map((item) => item.slackChannelId).sort()
+ ).toEqual(['CA', 'CC']);
+ for (const conversation of completed!.conversations) {
+ expect(conversation.status).toBe('completed');
+ expect(conversation.counters.processed).toBe(history ? 2 : 0);
+ }
+ });
+ }
+});
diff --git a/apps/web/vitest.config.ts b/apps/web/vitest.config.ts
index 6255d84e1a..46016c3e8e 100644
--- a/apps/web/vitest.config.ts
+++ b/apps/web/vitest.config.ts
@@ -216,6 +216,7 @@ export default defineConfig({
'src/lib/fullcalendar-solid/**/*.{test,spec}.{ts,tsx}',
'src/lib/persistence/**/*.{test,spec}.{ts,tsx}',
'src/lib/utils/**/*.{test,spec}.{ts,tsx}',
+ 'src/lib/workers/slack-import/**/*.{test,spec}.{ts,tsx}',
'src/routes/**/*.{test,spec}.{ts,tsx}',
],
name: 'app',
diff --git a/bun.lock b/bun.lock
index d9b69c0501..7d5e2f0f22 100644
--- a/bun.lock
+++ b/bun.lock
@@ -110,6 +110,7 @@
"@standard-schema/spec": "^1.1.0",
"@tailwindcss/vite": "^4.2.4",
"@tanstack/pacer": "^0.17.3",
+ "@tanstack/query-core": "5.90.2",
"@tanstack/solid-query": "5.90.3",
"@tanstack/solid-virtual": "3.13.37",
"@tauri-apps/api": "^2.8.0",
diff --git a/crates/ai_tools/src/import_channels.rs b/crates/ai_tools/src/import_channels.rs
new file mode 100644
index 0000000000..57275b343d
--- /dev/null
+++ b/crates/ai_tools/src/import_channels.rs
@@ -0,0 +1,41 @@
+//! Translate onboarding's reserved target into the owning channel API.
+
+use crate::ToolChannelMessagesService;
+use import::domain::models::ImportTargetReservation;
+use macro_user_id::user_id::MacroUserIdStr;
+use uuid::Uuid;
+
+#[cfg(test)]
+mod test;
+
+pub(crate) async fn create_channel(
+ service: &ToolChannelMessagesService,
+ user: &MacroUserIdStr<'static>,
+ name: &str,
+ target: &ImportTargetReservation,
+ participant_emails: &[String],
+ roster: &[MacroUserIdStr<'static>],
+) -> anyhow::Result<Uuid> {
+ // Preserve onboarding's best-effort team roster matching, not archive's raw
+ // email mapping. Unknown collaborators and bots are not invited here.
+ let participants = participant_emails
+ .iter()
+ .filter_map(|email| {
+ roster
+ .iter()
+ .find(|member| member.email_str().eq_ignore_ascii_case(email))
+ .cloned()
+ })
+ .collect();
+ let ensured = service
+ .create_reserved_team_channel(
+ user.clone(),
+ target.channel_id,
+ target.key.team_id,
+ name.to_string(),
+ participants,
+ )
+ .await
+ .map_err(|error| anyhow::anyhow!("failed to ensure imported channel: {error:?}"))?;
+ Ok(ensured.id)
+}
diff --git a/crates/ai_tools/src/import_channels/test.rs b/crates/ai_tools/src/import_channels/test.rs
new file mode 100644
index 0000000000..5279814276
--- /dev/null
+++ b/crates/ai_tools/src/import_channels/test.rs
@@ -0,0 +1,268 @@
+//! Cross-domain composition tests: real reservations, onboarding ledger and channel persistence.
+
+use channels::{
+ domain::{
+ historical::{HistoricalChannel, HistoricalChannelKind},
+ models::GetChannelsRequest,
+ ports::{ChannelListRepo, HistoricalChannelRepo},
+ service::ChannelServiceImpl,
+ },
+ outbound::pg_channels_repo::PgChannelsRepo,
+};
+use import::{
+ domain::{
+ models::{
+ ImportEntity, ImportSource, ImportStatus, ImportTargetKey, ImportTargetKind,
+ ImportTargetReservation, Initiator, SlackConversationId,
+ },
+ ports::{
+ CanonicalImportRepo, EntityCreator, ImportRepo, ImportedDocumentProperties,
+ ImportedTaskProperties,
+ },
+ service::{ImportService, ImportServiceImpl},
+ },
+ outbound::pg_import_repo::PgImportRepo,
+};
+use macro_user_id::user_id::MacroUserIdStr;
+use mcp_select::{ConnectorRef, ConnectorSelect, UserMcpTools};
+use models_pagination::{Query, SimpleSortMethod};
+use std::{
+ collections::HashSet,
+ sync::{
+ Arc,
+ atomic::{AtomicBool, Ordering},
+ },
+ time::Duration,
+};
+use uuid::Uuid;
+
+struct NoConnectors;
+
+impl ConnectorSelect for NoConnectors {
+ async fn user_toolset(&self, _: &MacroUserIdStr<'static>) -> UserMcpTools {
+ unreachable!("deterministic Slack imports never invoke MCP")
+ }
+ async fn connector_toolset(
+ &self,
+ _: &MacroUserIdStr<'static>,
+ _: ConnectorRef<'_>,
+ ) -> anyhow::Result<Option<UserMcpTools>> {
+ unreachable!("deterministic Slack imports never invoke MCP")
+ }
+ async fn connector_connected(
+ &self,
+ _: &MacroUserIdStr<'static>,
+ _: ConnectorRef<'_>,
+ ) -> anyhow::Result<bool> {
+ unreachable!("deterministic Slack imports never invoke MCP")
+ }
+}
+
+struct ReservedCreator {
+ service: ChannelServiceImpl<PgChannelsRepo>,
+ lose_response: AtomicBool,
+}
+
+impl EntityCreator for ReservedCreator {
+ async fn create_task(
+ &self,
+ _: &MacroUserIdStr<'static>,
+ _: &str,
+ _: &str,
+ _: &ImportedTaskProperties,
+ ) -> anyhow::Result<String> {
+ unreachable!()
+ }
+ async fn create_markdown_doc(
+ &self,
+ _: &MacroUserIdStr<'static>,
+ _: &str,
+ _: &str,
+ _: &ImportedDocumentProperties,
+ ) -> anyhow::Result<String> {
+ unreachable!()
+ }
+ async fn create_channel(
+ &self,
+ user: &MacroUserIdStr<'static>,
+ name: &str,
+ target: &ImportTargetReservation,
+ _: &[String],
+ ) -> anyhow::Result<Uuid> {
+ let persisted = self
+ .service
+ .create_reserved_team_channel(
+ user.clone(),
+ target.channel_id,
+ target.key.team_id,
+ name.to_string(),
+ HashSet::new(),
+ )
+ .await
+ .map_err(|error| anyhow::anyhow!("{error:?}"))?;
+ anyhow::ensure!(
+ !self.lose_response.swap(false, Ordering::SeqCst),
+ "lost creation response"
+ );
+ Ok(persisted.id)
+ }
+}
+
+fn user() -> MacroUserIdStr<'static> {
+ MacroUserIdStr::try_from("macro|[email protected]".to_string()).unwrap()
+}
+
+fn key() -> ImportTargetKey {
+ ImportTargetKey {
+ team_id: Uuid::from_u128(0x11111111_1111_1111_1111_111111111111),
+ foreign_id: SlackConversationId::new("C0123456789").unwrap(),
+ }
+}
+
+async fn stage(repo: &PgImportRepo) -> ImportEntity {
+ repo.upsert_staged(
+ &user(),
+ ImportSource::Slack,
+ Initiator::Onboarding,
+ key().foreign_id.as_str(),
+ &serde_json::json!({"name": "engineering", "channel_id": key().foreign_id.as_str()}),
+ )
+ .await
+ .unwrap()
+ .unwrap()
+}
+
+async fn settled(repo: &PgImportRepo, id: Uuid) -> ImportEntity {
+ tokio::time::timeout(Duration::from_secs(10), async {
+ loop {
+ let row = repo.get(&user(), id).await.unwrap().unwrap();
+ if row.status != ImportStatus::Importing {
+ return row;
+ }
+ tokio::time::sleep(Duration::from_millis(10)).await;
+ }
+ })
+ .await
+ .expect("onboarding should settle")
+}
+
+async fn channel_count(repo: &PgChannelsRepo) -> usize {
+ repo.get_user_channels_with_participants(
+ GetChannelsRequest {
+ macro_id: user(),
+ limit: Some(100),
+ include_frecency: false,
+ query: Query::Sort(SimpleSortMethod::UpdatedAt, None),
+ }
+ .into_params(),
+ )
+ .await
+ .unwrap()
+ .len()
+}
+
+async fn archive(repo: &PgImportRepo, channels: &PgChannelsRepo) -> Uuid {
+ let admin = MacroUserIdStr::try_from("macro|[email protected]".to_string()).unwrap();
+ let target = repo
+ .reserve_target(&admin, &key(), ImportTargetKind::Team, None)
+ .await
+ .unwrap();
+ let persisted = channels
+ .create_historical_channel(&HistoricalChannel {
+ id: target.channel_id,
+ name: "archive name".to_string(),
+ kind: HistoricalChannelKind::Team(key().team_id),
+ owner: admin,
+ participants: HashSet::from([user()]),
+ created_at: chrono::DateTime::from_timestamp(1_000, 0).unwrap(),
+ })
+ .await
+ .unwrap();
+ repo.complete_target(&key(), persisted.id, ImportTargetKind::Team)
+ .await
+ .unwrap()
+ .channel_id
+}
+
+#[sqlx::test(
+ migrations = "../macro_db_client/migrations",
+ fixtures(path = "../../../channels/fixtures", scripts("channels_repo"))
+)]
+async fn concurrent_onboarding_and_archive_publish_one_canonical_ledger_target(pool: sqlx::PgPool) {
+ let repo = PgImportRepo::new(pool.clone());
+ let channels = PgChannelsRepo::new(pool);
+ let before = channel_count(&channels).await;
+ let row = stage(&repo).await;
+ let service = ImportServiceImpl::new(
+ repo.clone(),
+ Arc::new(NoConnectors),
+ Arc::new(ReservedCreator {
+ service: ChannelServiceImpl::new(channels.clone()),
+ lose_response: AtomicBool::new(false),
+ }),
+ Arc::new(ai_usage::NoOpUsageRecorder),
+ );
+ let (run, archived) = tokio::join!(
+ service.run_import(user(), vec![row.id], vec![]),
+ archive(&repo, &channels),
+ );
+ assert_eq!(run.unwrap().importing, 1);
+ let row = settled(&repo, row.id).await;
+ assert_eq!(row.status, ImportStatus::Imported);
+ assert_eq!(row.entity_id, Some(archived.to_string()));
+ assert_eq!(row.team_id, Some(key().team_id));
+ let canonical = repo
+ .reserve_target(&user(), &key(), ImportTargetKind::Team, None)
+ .await
+ .unwrap();
+ assert!(canonical.ready);
+ assert_eq!(canonical.channel_id, archived);
+ assert_eq!(channel_count(&channels).await, before + 1);
+}
+
+#[sqlx::test(
+ migrations = "../macro_db_client/migrations",
+ fixtures(path = "../../../channels/fixtures", scripts("channels_repo"))
+)]
+async fn onboarding_crash_retry_completes_the_original_reserved_channel(pool: sqlx::PgPool) {
+ let repo = PgImportRepo::new(pool.clone());
+ let channels = PgChannelsRepo::new(pool);
+ let before = channel_count(&channels).await;
+ let row = stage(&repo).await;
+ let service = ImportServiceImpl::new(
+ repo.clone(),
+ Arc::new(NoConnectors),
+ Arc::new(ReservedCreator {
+ service: ChannelServiceImpl::new(channels.clone()),
+ lose_response: AtomicBool::new(true),
+ }),
+ Arc::new(ai_usage::NoOpUsageRecorder),
+ );
+ service
+ .run_import(user(), vec![row.id], vec![])
+ .await
+ .unwrap();
+ let failed = settled(&repo, row.id).await;
+ assert_eq!(failed.status, ImportStatus::Staged);
+ assert!(
+ failed
+ .last_error
+ .unwrap()
+ .contains("lost creation response")
+ );
+ let pending = repo
+ .reserve_target(&user(), &key(), ImportTargetKind::Team, None)
+ .await
+ .unwrap();
+ assert!(!pending.ready);
+ assert_eq!(channel_count(&channels).await, before + 1);
+ service
+ .run_import(user(), vec![row.id], vec![])
+ .await
+ .unwrap();
+ let imported = settled(&repo, row.id).await;
+ assert_eq!(imported.status, ImportStatus::Imported);
+ assert_eq!(imported.entity_id, Some(pending.channel_id.to_string()));
+ assert_eq!(archive(&repo, &channels).await, pending.channel_id);
+ assert_eq!(channel_count(&channels).await, before + 1);
+}
diff --git a/crates/ai_tools/src/lib.rs b/crates/ai_tools/src/lib.rs
index 7a66eeec3d..9f2f3d7d17 100644
--- a/crates/ai_tools/src/lib.rs
+++ b/crates/ai_tools/src/lib.rs
@@ -10,6 +10,7 @@ mod test;
pub mod ai_operations;
mod build_context;
mod display_results;
+mod import_channels;
mod mcp_app_catalog;
mod schemas;
pub mod search;
diff --git a/crates/ai_tools/src/tool_context.rs b/crates/ai_tools/src/tool_context.rs
index eb9c1bdeb8..dbc1fe9c00 100644
--- a/crates/ai_tools/src/tool_context.rs
+++ b/crates/ai_tools/src/tool_context.rs
@@ -1407,48 +1407,19 @@ impl import::domain::ports::EntityCreator for ToolEntityCreator {
&self,
user: &MacroUserIdStr<'static>,
name: &str,
- team_id: Option<uuid::Uuid>,
+ target: &import::domain::models::ImportTargetReservation,
participant_emails: &[String],
- ) -> anyhow::Result<String> {
- use channels::domain::ports::ChannelService as _;
- // Teammates who were in the source channel join the Macro one; emails
- // with no roster match (external collaborators, bots) are skipped.
+ ) -> anyhow::Result<uuid::Uuid> {
let (_, roster) = self.team_roster(user).await;
- let mut participants: std::collections::HashSet<MacroUserIdStr<'static>> =
- std::iter::once(user.clone()).collect();
- for email in participant_emails {
- if let Some(member) = roster
- .iter()
- .find(|member| member.email_str().eq_ignore_ascii_case(email))
- {
- participants.insert(member.clone());
- }
- }
- let request = channels::domain::models::CreateChannelRequest {
- name: Some(name.to_string()),
- channel_type: if team_id.is_some() {
- channels::domain::models::ChannelType::Team
- } else {
- channels::domain::models::ChannelType::Public
- },
- team_id,
- // The creator is always included (the service requires a
- // non-empty participant list and the repo filters out the
- // owner), plus any teammates matched by email above. Explicit
- // membership mirrors the source channel — never the whole team.
- auto_join_team: false,
- participants,
- };
- let response = self
- .channel_service
- .create_channel(
- channels::domain::models::Sender::new_from_user(user.clone()),
- None,
- request,
- )
- .await
- .map_err(|e| anyhow::anyhow!("failed to create channel: {e:?}"))?;
- Ok(response.id)
+ crate::import_channels::create_channel(
+ self.channel_service.as_ref(),
+ user,
+ name,
+ target,
+ participant_emails,
+ &roster,
+ )
+ .await
}
}
diff --git a/crates/channels/src/domain/historical.rs b/crates/channels/src/domain/historical.rs
new file mode 100644
index 0000000000..90b20622c1
--- /dev/null
+++ b/crates/channels/src/domain/historical.rs
@@ -0,0 +1,72 @@
+//! Trusted archive operations, separate from live channel side effects.
+//!
+//! The importing domain must authorize the team and any reused target before writing.
+//! These persistence commands do not grant import authorization or record provenance.
+
+use std::collections::HashSet;
+
+use chrono::{DateTime, Utc};
+use macro_user_id::user_id::MacroUserIdStr;
+use uuid::Uuid;
+
+use super::dm::DmPair;
+
+/// Archive channels are never globally public. Private provenance lives in the importer.
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub enum HistoricalChannelKind {
+ /// Explicit-membership team channel, with auto-join disabled on creation.
+ Team(Uuid),
+ /// Private channel without a team foreign key.
+ Private,
+}
+
+/// Creation with a durable, importer-reserved UUIDv7. Reuse must preserve settings.
+#[derive(Debug, Clone)]
+pub struct HistoricalChannel {
+ /// Stable reservation ID, reused after crashes.
+ pub id: Uuid,
+ /// Name used only on initial creation.
+ pub name: String,
+ /// Target type and optional team identity.
+ pub kind: HistoricalChannelKind,
+ /// Importing administrator, used as owner only on initial creation.
+ pub owner: MacroUserIdStr<'static>,
+ /// Explicit source memberships, in addition to the new channel's owner.
+ pub participants: HashSet<MacroUserIdStr<'static>>,
+ /// Resolved source creation time, used only on initial creation.
+ pub created_at: DateTime<Utc>,
+}
+
+/// Whether an atomic ensure operation actually created a channel.
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub struct EnsuredChannel {
+ /// Canonical channel ID, which may differ from a proposed DM ID.
+ pub id: Uuid,
+ /// Only the creator may dispatch live creation effects.
+ pub created: bool,
+}
+
+/// Creation policy for the shared, pair-locked DM primitive.
+#[derive(Debug, Clone)]
+pub enum DmCreation {
+ /// Present-day creation with ordinary activity, owned by one member of the pair.
+ Live(MacroUserIdStr<'static>),
+ /// Silent historical creation. The normalized pair's first member owns the DM.
+ Historical {
+ /// Caller-reserved UUIDv7, used only when no exact pair already exists.
+ id: Uuid,
+ /// Source creation time; existing timestamps and memberships are untouched.
+ created_at: DateTime<Utc>,
+ },
+}
+
+impl DmCreation {
+ /// Resolve ownership without ever introducing a third participant.
+ pub fn owner<'a>(&'a self, pair: &'a DmPair) -> anyhow::Result<&'a MacroUserIdStr<'static>> {
+ match self {
+ Self::Live(owner) if owner == pair.lo() || owner == pair.hi() => Ok(owner),
+ Self::Live(_) => anyhow::bail!("DM owner must belong to the pair"),
+ Self::Historical { .. } => Ok(pair.lo()),
+ }
+ }
+}
diff --git a/crates/channels/src/domain/mod.rs b/crates/channels/src/domain/mod.rs
index eb833a8fbb..0026edab16 100644
--- a/crates/channels/src/domain/mod.rs
+++ b/crates/channels/src/domain/mod.rs
@@ -12,6 +12,8 @@ pub mod events;
/// Channel audience policy for authored group mentions.
#[cfg(feature = "ports")]
pub mod group_mentions;
+/// Silent historical creation commands and shared atomic DM creation policy.
+pub mod historical;
/// Legacy channel list service implementation.
#[cfg(feature = "list")]
pub mod list_service;
diff --git a/crates/channels/src/domain/ports.rs b/crates/channels/src/domain/ports.rs
index 6ce56f29ba..569ef7c336 100644
--- a/crates/channels/src/domain/ports.rs
+++ b/crates/channels/src/domain/ports.rs
@@ -24,6 +24,7 @@ use crate::domain::{
events::ChannelEvent,
};
use channel_sender::ChannelSender;
+use chrono::{DateTime, Utc};
use entity_access::domain::models::{EntityAccessReceipt, MemberParticipantRole};
use macro_user_id::user_id::MacroUserIdStr;
use models_pagination::{CreatedAt, Query};
@@ -122,6 +123,42 @@ pub trait ChannelAttachmentRepo: Send + Sync + 'static {
) -> impl Future<Output = Result<Vec<RecentChannelMessage>, Self::Err>> + Send;
}
+/// Reserved channel persistence shared by archive and onboarding imports.
+/// Archive operations are silent; onboarding records first-creation activity.
+/// Import authorization and durable provenance belong to the importing domain.
+pub trait HistoricalChannelRepo: Send + Sync + 'static {
+ /// Create an explicit-ID Team/Private channel with its initial members, or return
+ /// the existing compatible target without changing any of its settings or members.
+ fn create_historical_channel(
+ &self,
+ channel: &super::historical::HistoricalChannel,
+ ) -> impl Future<Output = anyhow::Result<super::historical::EnsuredChannel>> + Send;
+
+ /// Ensure a reserved Team channel for onboarding. Initial creation also writes
+ /// the owner's ordinary activity; reuse has no effects. The service authorizes
+ /// team membership and dispatches creation events only for the winning creator.
+ fn create_onboarding_channel(
+ &self,
+ channel: &super::historical::HistoricalChannel,
+ ) -> impl Future<Output = anyhow::Result<super::historical::EnsuredChannel>> + Send;
+
+ /// Insert missing members silently. Never demote roles or reactivate leavers.
+ /// Reject DMs so this operation cannot introduce a third member.
+ fn insert_historical_participants(
+ &self,
+ channel_id: Uuid,
+ participants: &[MacroUserIdStr<'static>],
+ joined_at: DateTime<Utc>,
+ ) -> impl Future<Output = anyhow::Result<()>> + Send;
+
+ /// Advance channel activity monotonically without creating ordinary activity rows.
+ fn advance_historical_activity(
+ &self,
+ channel_id: Uuid,
+ activity_at: DateTime<Utc>,
+ ) -> impl Future<Output = anyhow::Result<()>> + Send;
+}
+
/// Repository for channel persistence and query data.
#[cfg_attr(test, mockall::automock(type Err = anyhow::Error;))]
pub trait ChannelRepo: Send + Sync + 'static {
@@ -242,7 +279,16 @@ pub trait ChannelRepo: Send + Sync + 'static {
channel_ids: &[Uuid],
) -> impl Future<Output = Result<(), Self::Err>> + Send;
- /// Fetch an existing direct message channel.
+ /// Atomically find or create an exact two-member DM under a normalized pair lock.
+ /// Reuse preserves all settings, roles and timestamps, including historical leavers.
+ /// Historical callers must authorize reuse before writing history into the target.
+ fn ensure_dm(
+ &self,
+ pair: super::dm::DmPair,
+ creation: super::historical::DmCreation,
+ ) -> impl Future<Output = Result<super::historical::EnsuredChannel, Self::Err>> + Send;
+
+ /// Fetch an existing exact two-member DM, deterministically choosing the oldest.
fn maybe_get_dm<'a>(
&self,
user_id: MacroUserIdStr<'a>,
diff --git a/crates/channels/src/domain/service.rs b/crates/channels/src/domain/service.rs
index df8e5267c4..e3331bc4e4 100644
--- a/crates/channels/src/domain/service.rs
+++ b/crates/channels/src/domain/service.rs
@@ -22,9 +22,10 @@ use entity_access::domain::models::{
};
use macro_user_id::user_id::MacroUserIdStr;
use models_pagination::{CreatedAt, PaginateOn, Query};
-use std::collections::HashSet;
use uuid::Uuid;
+mod import;
+
#[cfg(test)]
mod test;
@@ -715,25 +716,30 @@ where
pair: DmPair,
owner: MacroUserIdStr<'static>,
) -> Result<GetOrCreateChannelResponse, ChannelMutationErr> {
- let existing_channel_id = self
+ let participant_user_ids = vec![pair.lo().clone(), pair.hi().clone()];
+ let ensured = self
.repo
- .maybe_get_dm(pair.lo().clone(), pair.hi().clone())
+ .ensure_dm(pair, super::historical::DmCreation::Live(owner.clone()))
.await
.map_err(|e| ChannelMutationErr::Repo(e.into()))?;
- self.get_or_create_channel(
- existing_channel_id,
- owner,
- None,
- crate::domain::models::CreateChannelRequest {
- name: None,
+ let action = if ensured.created {
+ self.events.dispatch(ChannelEvent::ChannelCreated {
+ channel_id: ensured.id,
+ actor: ChannelSender::new_from_user(owner),
+ on_behalf_of: None,
channel_type: ChannelType::DirectMessage,
- team_id: None,
- auto_join_team: false,
- participants: HashSet::from([pair.lo().clone(), pair.hi().clone()]),
- },
- )
- .await
+ channel_name: None,
+ participant_user_ids,
+ });
+ GetOrCreateAction::Create
+ } else {
+ GetOrCreateAction::Get
+ };
+ Ok(GetOrCreateChannelResponse {
+ channel_id: ensured.id.to_string(),
+ action,
+ })
}
async fn create_channel_record<'a>(
diff --git a/crates/channels/src/domain/service/import.rs b/crates/channels/src/domain/service/import.rs
new file mode 100644
index 0000000000..55502fe576
--- /dev/null
+++ b/crates/channels/src/domain/service/import.rs
@@ -0,0 +1,66 @@
+//! Reserved onboarding creation, sharing archive persistence without replaying live effects.
+
+use super::ChannelServiceImpl;
+use crate::domain::{
+ events::ChannelEvent,
+ historical::{EnsuredChannel, HistoricalChannel, HistoricalChannelKind},
+ models::{ChannelType, Sender},
+ ports::{ChannelEventDispatcher, ChannelMutationErr, ChannelRepo, HistoricalChannelRepo},
+};
+use macro_user_id::user_id::MacroUserIdStr;
+use std::collections::HashSet;
+use uuid::Uuid;
+
+impl<R, E, P, F> ChannelServiceImpl<R, E, P, F>
+where
+ R: ChannelRepo + HistoricalChannelRepo,
+ E: ChannelEventDispatcher,
+{
+ /// Create or reuse a reserved onboarding Team channel. Team membership is
+ /// checked on every call; reuse never changes memberships, names or settings.
+ /// Only initial creation emits the ordinary channel-created event.
+ pub async fn create_reserved_team_channel(
+ &self,
+ owner: MacroUserIdStr<'static>,
+ id: Uuid,
+ team_id: Uuid,
+ name: String,
+ mut participants: HashSet<MacroUserIdStr<'static>>,
+ ) -> Result<EnsuredChannel, ChannelMutationErr> {
+ let has_team = self
+ .repo
+ .user_has_team(owner.as_ref().to_string(), team_id)
+ .await
+ .map_err(|error| ChannelMutationErr::Repo(error.into()))?;
+ if !has_team {
+ return Err(ChannelMutationErr::Unauthorized(
+ "you do not have access to that team".to_string(),
+ ));
+ }
+ participants.insert(owner.clone());
+ let channel = HistoricalChannel {
+ id,
+ name,
+ kind: HistoricalChannelKind::Team(team_id),
+ owner,
+ participants,
+ created_at: chrono::Utc::now(),
+ };
+ let ensured = self
+ .repo
+ .create_onboarding_channel(&channel)
+ .await
+ .map_err(ChannelMutationErr::Repo)?;
+ if ensured.created {
+ self.events.dispatch(ChannelEvent::ChannelCreated {
+ channel_id: ensured.id,
+ actor: Sender::new_from_user(channel.owner),
+ on_behalf_of: None,
+ channel_type: ChannelType::Team,
+ channel_name: Some(channel.name),
+ participant_user_ids: channel.participants.into_iter().collect(),
+ });
+ }
+ Ok(ensured)
+ }
+}
diff --git a/crates/channels/src/domain/service/test.rs b/crates/channels/src/domain/service/test.rs
index a0c5867fb3..a9e9b068d6 100644
--- a/crates/channels/src/domain/service/test.rs
+++ b/crates/channels/src/domain/service/test.rs
@@ -15,7 +15,10 @@ use crate::domain::{
use chrono::Utc;
use entity_access::domain::models::ParticipantRole as AccessRole;
use macro_user_id::{cowlike::CowLike, user_id::MacroUserIdStr};
-use std::sync::{Arc, Mutex};
+use std::{
+ collections::HashSet,
+ sync::{Arc, Mutex},
+};
fn empty_repo() -> MockChannelRepo {
let mut repo = MockChannelRepo::new();
@@ -255,6 +258,18 @@ impl ChannelRepo for FakeMutationRepo {
Ok(())
}
+ async fn ensure_dm(
+ &self,
+ pair: crate::domain::dm::DmPair,
+ creation: crate::domain::historical::DmCreation,
+ ) -> Result<crate::domain::historical::EnsuredChannel, Self::Err> {
+ creation.owner(&pair)?;
+ Ok(crate::domain::historical::EnsuredChannel {
+ id: self.state.lock().unwrap().channel_id,
+ created: true,
+ })
+ }
+
async fn maybe_get_dm(
&self,
_user_id: MacroUserIdStr<'_>,
@@ -814,9 +829,14 @@ async fn ensure_dms_does_not_dispatch_for_existing_channel() {
let joiner = macro_id("macro|[email protected]");
let teammate = macro_id("macro|[email protected]");
let mut repo = MockChannelRepo::new();
- repo.expect_maybe_get_dm()
- .once()
- .returning(move |_, _| Box::pin(async move { Ok(Some(channel_id)) }));
+ repo.expect_ensure_dm().once().returning(move |_, _| {
+ Box::pin(async move {
+ Ok(crate::domain::historical::EnsuredChannel {
+ id: channel_id,
+ created: false,
+ })
+ })
+ });
let events = FakeEvents::default();
let service = ChannelServiceImpl::with_dependencies(
repo,
@@ -867,9 +887,14 @@ async fn get_or_create_dm_returns_get_for_existing_pair() {
let actor = macro_id("macro|[email protected]");
let recipient = macro_id("macro|[email protected]");
let mut repo = MockChannelRepo::new();
- repo.expect_maybe_get_dm()
- .once()
- .returning(move |_, _| Box::pin(async move { Ok(Some(channel_id)) }));
+ repo.expect_ensure_dm().once().returning(move |_, _| {
+ Box::pin(async move {
+ Ok(crate::domain::historical::EnsuredChannel {
+ id: channel_id,
+ created: false,
+ })
+ })
+ });
let events = FakeEvents::default();
let service = ChannelServiceImpl::with_dependencies(
repo,
diff --git a/crates/channels/src/outbound/pg_channels_repo.rs b/crates/channels/src/outbound/pg_channels_repo.rs
index 36d0c49691..e19ba8dd79 100644
--- a/crates/channels/src/outbound/pg_channels_repo.rs
+++ b/crates/channels/src/outbound/pg_channels_repo.rs
@@ -1,3 +1,6 @@
+/// Silent historical persistence and transaction-aware activity updates.
+pub mod historical;
+
#[cfg(test)]
mod tests;
@@ -2255,37 +2258,21 @@ impl ChannelRepo for PgChannelsRepo {
Ok(())
}
+ async fn ensure_dm(
+ &self,
+ pair: crate::domain::dm::DmPair,
+ creation: crate::domain::historical::DmCreation,
+ ) -> Result<crate::domain::historical::EnsuredChannel, Self::Err> {
+ historical::ensure_dm(self, pair, creation).await
+ }
+
async fn maybe_get_dm(
&self,
user_id: MacroUserIdStr<'_>,
recipient_id: MacroUserIdStr<'_>,
) -> Result<Option<Uuid>, Self::Err> {
- let row = sqlx::query_as!(
- ChannelIdRow,
- r#"
- SELECT id
- FROM comms_channels
- WHERE channel_type = 'direct_message'::comms_channel_type
- AND EXISTS (
- SELECT 1
- FROM comms_channel_participants cp
- WHERE cp.channel_id = comms_channels.id
- AND cp.user_id = $1
- )
- AND EXISTS (
- SELECT 1
- FROM comms_channel_participants cp
- WHERE cp.channel_id = comms_channels.id
- AND cp.user_id = $2
- )
- "#,
- user_id.as_ref(),
- recipient_id.as_ref(),
- )
- .fetch_optional(&self.pool)
- .await
- .context("unable to get direct message channel")?;
- Ok(row.map(|row| row.id))
+ let pair = crate::domain::dm::DmPair::new(user_id, recipient_id)?;
+ historical::find_dm(&mut *self.pool.acquire().await?, &pair).await
}
async fn maybe_get_private_channel(
diff --git a/crates/channels/src/outbound/pg_channels_repo/historical.rs b/crates/channels/src/outbound/pg_channels_repo/historical.rs
new file mode 100644
index 0000000000..9c27f0b338
--- /dev/null
+++ b/crates/channels/src/outbound/pg_channels_repo/historical.rs
@@ -0,0 +1,360 @@
+//! Silent archive persistence and the shared live/archive DM transaction.
+
+use chrono::{DateTime, Utc};
+use macro_user_id::user_id::MacroUserIdStr;
+use sqlx::{PgConnection, Postgres, Transaction};
+use uuid::Uuid;
+
+use crate::domain::{
+ dm::DmPair,
+ historical::{DmCreation, EnsuredChannel, HistoricalChannel, HistoricalChannelKind},
+ models::ChannelType,
+ ports::HistoricalChannelRepo,
+};
+
+use super::{PgChannelsRepo, create_activity};
+
+#[cfg(test)]
+mod test;
+
+impl PgChannelsRepo {
+ async fn ensure_reserved_channel(
+ &self,
+ channel: &HistoricalChannel,
+ live_activity: bool,
+ ) -> anyhow::Result<EnsuredChannel> {
+ let mut tx = self.pool.begin().await?;
+ let result = Self::ensure_reserved_channel_in(&mut tx, channel, live_activity).await?;
+ tx.commit().await?;
+ Ok(result)
+ }
+
+ /// Create only the reserved identity in the caller's transaction. No live effects
+ /// are requested by archive callers; existing targets are never modified.
+ pub async fn ensure_reserved_channel_in(
+ tx: &mut Transaction<'_, Postgres>,
+ channel: &HistoricalChannel,
+ live_activity: bool,
+ ) -> anyhow::Result<EnsuredChannel> {
+ validate_reserved_id(channel.id)?;
+ let (channel_type, team_id) = match channel.kind {
+ HistoricalChannelKind::Team(team_id) => (ChannelType::Team, Some(team_id)),
+ HistoricalChannelKind::Private => (ChannelType::Private, None),
+ };
+ let inserted = sqlx::query_scalar!(
+ r#"
+ INSERT INTO comms_channels
+ (id, name, owner_id, channel_type, team_id, auto_join_team, created_at, updated_at)
+ VALUES ($1, $2, $3, $4, $5, false, $6, $6)
+ ON CONFLICT (id) DO NOTHING
+ RETURNING id
+ "#,
+ channel.id,
+ channel.name,
+ channel.owner.as_ref(),
+ channel_type as ChannelType,
+ team_id,
+ channel.created_at,
+ )
+ .fetch_optional(&mut **tx)
+ .await?;
+ let created = inserted.is_some();
+ if created {
+ insert_owner(tx, channel.id, &channel.owner, channel.created_at).await?;
+ let participants = channel.participants.iter().cloned().collect::<Vec<_>>();
+ insert_members(tx, channel.id, &participants, channel.created_at).await?;
+ if live_activity {
+ create_activity(&mut **tx, channel.id, channel.owner.as_ref()).await?;
+ }
+ } else {
+ // An explicit ID is not authority to repurpose a different target.
+ let existing = sqlx::query!(
+ r#"SELECT channel_type AS "channel_type: ChannelType", team_id
+ FROM comms_channels WHERE id = $1 FOR UPDATE"#,
+ channel.id,
+ )
+ .fetch_one(&mut **tx)
+ .await?;
+ anyhow::ensure!(
+ existing.channel_type == channel_type && existing.team_id == team_id,
+ "historical channel reservation conflicts with existing target"
+ );
+ }
+ Ok(EnsuredChannel {
+ id: channel.id,
+ created,
+ })
+ }
+}
+
+impl HistoricalChannelRepo for PgChannelsRepo {
+ async fn create_historical_channel(
+ &self,
+ channel: &HistoricalChannel,
+ ) -> anyhow::Result<EnsuredChannel> {
+ self.ensure_reserved_channel(channel, false).await
+ }
+
+ async fn create_onboarding_channel(
+ &self,
+ channel: &HistoricalChannel,
+ ) -> anyhow::Result<EnsuredChannel> {
+ anyhow::ensure!(
+ matches!(channel.kind, HistoricalChannelKind::Team(_)),
+ "onboarding requires a Team channel"
+ );
+ self.ensure_reserved_channel(channel, true).await
+ }
+
+ async fn insert_historical_participants(
+ &self,
+ channel_id: Uuid,
+ participants: &[MacroUserIdStr<'static>],
+ joined_at: DateTime<Utc>,
+ ) -> anyhow::Result<()> {
+ let mut tx = self.pool.begin().await?;
+ let channel_type = sqlx::query_scalar!(
+ r#"SELECT channel_type AS "channel_type: ChannelType"
+ FROM comms_channels WHERE id = $1 FOR UPDATE"#,
+ channel_id,
+ )
+ .fetch_one(&mut *tx)
+ .await?;
+ anyhow::ensure!(
+ matches!(channel_type, ChannelType::Team | ChannelType::Private),
+ "historical membership insertion requires a Team or Private channel"
+ );
+ insert_members(&mut tx, channel_id, participants, joined_at).await?;
+ tx.commit().await?;
+ Ok(())
+ }
+
+ async fn advance_historical_activity(
+ &self,
+ channel_id: Uuid,
+ activity_at: DateTime<Utc>,
+ ) -> anyhow::Result<()> {
+ let mut tx = self.pool.begin().await?;
+ advance_historical_activity(&mut tx, channel_id, activity_at).await?;
+ tx.commit().await?;
+ Ok(())
+ }
+}
+
+/// Update historical activity inside the composition root's batch transaction.
+/// Does not emit events or write per-user activity; never backdates newer live activity.
+pub async fn advance_historical_activity(
+ tx: &mut Transaction<'_, Postgres>,
+ channel_id: Uuid,
+ activity_at: DateTime<Utc>,
+) -> anyhow::Result<()> {
+ let updated = sqlx::query!(
+ "UPDATE comms_channels SET updated_at = GREATEST(updated_at, $2) WHERE id = $1",
+ channel_id,
+ activity_at,
+ )
+ .execute(&mut **tx)
+ .await?;
+ anyhow::ensure!(updated.rows_affected() == 1, "historical channel not found");
+ Ok(())
+}
+
+pub(super) async fn ensure_dm(
+ repo: &PgChannelsRepo,
+ pair: DmPair,
+ creation: DmCreation,
+) -> anyhow::Result<EnsuredChannel> {
+ let mut tx = repo.pool.begin().await?;
+ let result = ensure_dm_in(&mut tx, pair, creation).await?;
+ tx.commit().await?;
+ Ok(result)
+}
+
+/// Shared pair lock and silent creation inside an import coordinator transaction.
+/// The caller must reject a returned ID different from its canonical reservation.
+pub async fn ensure_dm_in(
+ tx: &mut Transaction<'_, Postgres>,
+ pair: DmPair,
+ creation: DmCreation,
+) -> anyhow::Result<EnsuredChannel> {
+ let owner = creation.owner(&pair)?;
+ let (id, created_at) = match &creation {
+ DmCreation::Live(_) => (macro_uuid::generate_uuid_v7(), None),
+ DmCreation::Historical { id, created_at } => {
+ validate_reserved_id(*id)?;
+ (*id, Some(*created_at))
+ }
+ };
+ // Length-prefix the first identity so arbitrary valid email characters cannot
+ // make two distinct pairs share a lock key. Hash collisions only serialize work.
+ let lock_key = format!(
+ "channels:dm:{}:{}{}",
+ pair.lo().as_ref().len(),
+ pair.lo(),
+ pair.hi()
+ );
+ sqlx::query!(
+ "SELECT pg_advisory_xact_lock(hashtextextended($1, 0))",
+ lock_key
+ )
+ .execute(&mut **tx)
+ .await?;
+ // This must be a separate statement after acquiring the lock: READ COMMITTED
+ // then observes a concurrent creator that committed while we were waiting.
+ if let Some(id) = find_dm(tx, &pair).await? {
+ return Ok(EnsuredChannel { id, created: false });
+ }
+ let inserted_at = sqlx::query_scalar!(
+ r#"
+ INSERT INTO comms_channels
+ (id, owner_id, channel_type, auto_join_team, created_at, updated_at)
+ VALUES ($1, $2, 'direct_message', false, COALESCE($3, NOW()), COALESCE($3, NOW()))
+ ON CONFLICT (id) DO NOTHING
+ RETURNING created_at
+ "#,
+ id,
+ owner.as_ref(),
+ created_at,
+ )
+ .fetch_optional(&mut **tx)
+ .await?;
+ let created_at = inserted_at
+ .ok_or_else(|| anyhow::anyhow!("DM reservation conflicts with existing target"))?;
+ insert_owner(tx, id, owner, created_at).await?;
+ insert_members(tx, id, &[pair.lo().clone(), pair.hi().clone()], created_at).await?;
+ if matches!(creation, DmCreation::Live(_)) {
+ create_activity(&mut **tx, id, owner.as_ref()).await?;
+ }
+ Ok(EnsuredChannel { id, created: true })
+}
+
+/// Discover an exact pair without modifying it.
+pub async fn find_dm(connection: &mut PgConnection, pair: &DmPair) -> anyhow::Result<Option<Uuid>> {
+ Ok(sqlx::query_scalar!(
+ r#"
+ SELECT c.id FROM comms_channels c
+ WHERE c.channel_type = 'direct_message'
+ AND EXISTS (SELECT 1 FROM comms_channel_participants p
+ WHERE p.channel_id = c.id AND p.user_id = $1)
+ AND EXISTS (SELECT 1 FROM comms_channel_participants p
+ WHERE p.channel_id = c.id AND p.user_id = $2)
+ AND (SELECT COUNT(*) FROM comms_channel_participants p WHERE p.channel_id = c.id) = 2
+ ORDER BY c.created_at, c.id
+ LIMIT 1
+ "#,
+ pair.lo().as_ref(),
+ pair.hi().as_ref(),
+ )
+ .fetch_optional(connection)
+ .await?)
+}
+
+async fn insert_owner(
+ tx: &mut Transaction<'_, Postgres>,
+ channel_id: Uuid,
+ owner: &MacroUserIdStr<'_>,
+ joined_at: DateTime<Utc>,
+) -> anyhow::Result<()> {
+ sqlx::query!(
+ r#"INSERT INTO comms_channel_participants (channel_id, user_id, role, joined_at)
+ VALUES ($1, $2, 'owner', $3) ON CONFLICT (channel_id, user_id) DO NOTHING"#,
+ channel_id,
+ owner.as_ref(),
+ joined_at,
+ )
+ .execute(&mut **tx)
+ .await?;
+ Ok(())
+}
+
+/// Insert silent memberships after the caller locks and authorizes a non-DM target.
+/// Existing roles and historical leavers are preserved.
+pub async fn insert_members(
+ tx: &mut Transaction<'_, Postgres>,
+ channel_id: Uuid,
+ participants: &[MacroUserIdStr<'static>],
+ joined_at: DateTime<Utc>,
+) -> anyhow::Result<()> {
+ let user_ids = participants
+ .iter()
+ .map(ToString::to_string)
+ .collect::<Vec<_>>();
+ sqlx::query!(
+ r#"INSERT INTO comms_channel_participants (channel_id, user_id, role, joined_at)
+ SELECT $1, user_id, 'member'::comms_participant_role, $3 FROM UNNEST($2::text[]) AS user_id
+ ON CONFLICT (channel_id, user_id) DO NOTHING"#,
+ channel_id,
+ &user_ids,
+ joined_at,
+ )
+ .execute(&mut **tx)
+ .await?;
+ Ok(())
+}
+
+/// Locked channel facts for import authorization.
+pub struct LockedChannel {
+ /// Persisted visibility.
+ pub kind: ChannelType,
+ /// Team identity (only Team channels may have one).
+ pub team: Option<Uuid>,
+ /// Up to three DM members (three rejects a malformed DM); only the
+ /// requester for other channel kinds.
+ pub participants: Vec<crate::domain::models::ChannelParticipant>,
+}
+
+/// Lock channel identity and the memberships needed for authorization. Taking
+/// the channel lock also blocks new participant FK inserts. Non-DM checks never
+/// load an entire potentially large channel's roster.
+pub async fn inspect_in(
+ tx: &mut Transaction<'_, Postgres>,
+ id: Uuid,
+ requester: Option<&MacroUserIdStr<'_>>,
+) -> anyhow::Result<Option<LockedChannel>> {
+ let row = sqlx::query!(
+ r#"SELECT channel_type AS "channel_type: ChannelType", team_id
+ FROM comms_channels WHERE id = $1 FOR UPDATE"#,
+ id,
+ )
+ .fetch_optional(&mut **tx)
+ .await?;
+ let Some(row) = row else {
+ return Ok(None);
+ };
+ let participants = sqlx::query!(
+ r#"SELECT user_id, role::text AS "role!", joined_at, left_at
+ FROM comms_channel_participants WHERE channel_id = $1
+ AND ($2 OR user_id = $3) ORDER BY user_id LIMIT 3 FOR UPDATE"#,
+ id,
+ row.channel_type == ChannelType::DirectMessage,
+ requester.map(|user| user.as_ref()),
+ )
+ .fetch_all(&mut **tx)
+ .await?;
+ let participants = participants
+ .into_iter()
+ .map(|p| {
+ Ok(crate::domain::models::ChannelParticipant {
+ channel_id: id,
+ user_id: p.user_id,
+ role: p.role.parse().map_err(anyhow::Error::msg)?,
+ joined_at: p.joined_at,
+ left_at: p.left_at,
+ })
+ })
+ .collect::<anyhow::Result<Vec<_>>>()?;
+ Ok(Some(LockedChannel {
+ kind: row.channel_type,
+ team: row.team_id,
+ participants,
+ }))
+}
+
+fn validate_reserved_id(id: Uuid) -> anyhow::Result<()> {
+ anyhow::ensure!(
+ id.get_variant() == uuid::Variant::RFC4122
+ && id.get_version() == Some(uuid::Version::SortRand),
+ "historical channel ID must be UUIDv7"
+ );
+ Ok(())
+}
diff --git a/crates/channels/src/outbound/pg_channels_repo/historical/test.rs b/crates/channels/src/outbound/pg_channels_repo/historical/test.rs
new file mode 100644
index 0000000000..3dfe04dc87
--- /dev/null
+++ b/crates/channels/src/outbound/pg_channels_repo/historical/test.rs
@@ -0,0 +1,619 @@
+use super::*;
+use crate::domain::{
+ events::ChannelEvent,
+ ports::{ChannelEventDispatcher, ChannelMutationErr},
+ service::NoopChannelReferenceSharePermissions,
+};
+use crate::domain::{
+ models::{
+ GetChannelsRequest, GetOrCreateAction, GetOrCreateDmRequest, ParticipantRole, Sender,
+ },
+ ports::{ChannelListRepo, ChannelRepo, ChannelService},
+ service::ChannelServiceImpl,
+};
+use macro_db_migrator::MACRO_DB_MIGRATIONS;
+use models_pagination::{Query, SimpleSortMethod};
+use sqlx::PgPool;
+use std::{
+ collections::HashSet,
+ sync::{Arc, Mutex},
+};
+
+#[derive(Clone, Default)]
+struct CreationEvents(Arc<Mutex<Vec<Uuid>>>);
+
+impl ChannelEventDispatcher for CreationEvents {
+ fn dispatch(&self, event: ChannelEvent) {
+ let ChannelEvent::ChannelCreated { channel_id, .. } = event else {
+ panic!("unexpected event");
+ };
+ self.0.lock().unwrap().push(channel_id);
+ }
+}
+
+fn user(email: &str) -> MacroUserIdStr<'static> {
+ MacroUserIdStr::try_from(format!("macro|{email}")).unwrap()
+}
+
+fn timestamp(seconds: i64) -> DateTime<Utc> {
+ DateTime::from_timestamp(seconds, 0).unwrap()
+}
+
+fn pair() -> DmPair {
+ DmPair::new(user("[email protected]"), user("[email protected]")).unwrap()
+}
+
+fn historical_dm(id: Uuid) -> DmCreation {
+ DmCreation::Historical {
+ id,
+ created_at: timestamp(1_000),
+ }
+}
+
+fn channel() -> HistoricalChannel {
+ HistoricalChannel {
+ id: macro_uuid::generate_uuid_v7(),
+ name: "source name".into(),
+ kind: HistoricalChannelKind::Private,
+ owner: user("[email protected]"),
+ participants: HashSet::from([user("[email protected]"), user("[email protected]")]),
+ created_at: timestamp(1_000),
+ }
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn historical_replay_preserves_settings_roles_leavers_and_live_activity(pool: PgPool) {
+ let repo = PgChannelsRepo::new(pool.clone());
+ let mut request = channel();
+ assert!(
+ repo.create_historical_channel(&request)
+ .await
+ .unwrap()
+ .created
+ );
+ sqlx::query!(
+ "UPDATE comms_channel_participants SET role = 'admin', left_at = $2 WHERE channel_id = $1 AND user_id = 'macro|[email protected]'",
+ request.id, timestamp(2_000),
+ ).execute(&pool).await.unwrap();
+ repo.advance_historical_activity(request.id, timestamp(4_000))
+ .await
+ .unwrap();
+ request.name = "do not rename".into();
+ request.owner = user("[email protected]");
+ request.created_at = timestamp(500);
+ assert!(
+ !repo
+ .create_historical_channel(&request)
+ .await
+ .unwrap()
+ .created
+ );
+ let members = vec![
+ ];
+ repo.insert_historical_participants(request.id, &members, timestamp(500))
+ .await
+ .unwrap();
+ repo.insert_historical_participants(request.id, &members, timestamp(600))
+ .await
+ .unwrap();
+ repo.advance_historical_activity(request.id, timestamp(3_000))
+ .await
+ .unwrap();
+
+ let row = sqlx::query!(
+ "SELECT name, owner_id, team_id, auto_join_team, created_at, updated_at FROM comms_channels WHERE id = $1",
+ request.id,
+ ).fetch_one(&pool).await.unwrap();
+ assert_eq!(row.name.as_deref(), Some("source name"));
+ assert_eq!(row.owner_id, "macro|[email protected]");
+ assert_eq!(row.team_id, None);
+ assert!(!row.auto_join_team);
+ assert_eq!(row.created_at, timestamp(1_000));
+ assert_eq!(row.updated_at, timestamp(4_000));
+ let members = sqlx::query!(
+ r#"SELECT user_id, role AS "role: ParticipantRole", joined_at, left_at
+ FROM comms_channel_participants WHERE channel_id = $1 ORDER BY user_id"#,
+ request.id,
+ )
+ .fetch_all(&pool)
+ .await
+ .unwrap();
+ assert_eq!(members.len(), 4);
+ let leaver = members
+ .iter()
+ .find(|p| p.user_id == "macro|[email protected]")
+ .unwrap();
+ assert_eq!(leaver.role, ParticipantRole::Admin);
+ assert_eq!(leaver.joined_at, timestamp(1_000));
+ assert_eq!(leaver.left_at, Some(timestamp(2_000)));
+ let owner = members
+ .iter()
+ .find(|p| p.user_id == "macro|[email protected]")
+ .unwrap();
+ assert_eq!(owner.role, ParticipantRole::Owner);
+ assert_eq!(owner.joined_at, timestamp(1_000));
+ assert_eq!(owner.left_at, None);
+ let added = members
+ .iter()
+ .find(|p| p.user_id == "macro|[email protected]")
+ .unwrap();
+ assert_eq!(added.joined_at, timestamp(500));
+ assert_eq!(
+ sqlx::query_scalar!("SELECT COUNT(*) FROM comms_activity")
+ .fetch_one(&pool)
+ .await
+ .unwrap(),
+ Some(0)
+ );
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn concurrent_live_and_import_dm_creation_share_one_channel(pool: PgPool) {
+ let repo = PgChannelsRepo::new(pool.clone());
+ let service = ChannelServiceImpl::new(repo.clone());
+ let mut calls = Vec::new();
+ for index in 0..20 {
+ let repo = repo.clone();
+ let service = service.clone();
+ calls.push(tokio::spawn(async move {
+ if index % 2 == 0 {
+ repo.ensure_dm(pair(), historical_dm(macro_uuid::generate_uuid_v7()))
+ .await
+ .unwrap()
+ } else {
+ let response = service
+ .get_or_create_dm(
+ Sender::new_from_user(pair().hi().clone()),
+ GetOrCreateDmRequest {
+ recipient_id: pair().lo().clone(),
+ },
+ )
+ .await
+ .unwrap();
+ EnsuredChannel {
+ id: response.channel_id.parse().unwrap(),
+ created: response.action == GetOrCreateAction::Create,
+ }
+ }
+ }));
+ }
+ let mut ids = HashSet::new();
+ let mut created = 0;
+ for call in calls {
+ let result = call.await.unwrap();
+ ids.insert(result.id);
+ created += usize::from(result.created);
+ }
+ assert_eq!(ids.len(), 1);
+ assert_eq!(created, 1);
+ let id = *ids.iter().next().unwrap();
+ let participants = repo.get_channel_participants(id).await.unwrap();
+ assert_eq!(participants.len(), 2);
+ assert_eq!(
+ participants
+ .iter()
+ .filter(|p| p.role == ParticipantRole::Owner)
+ .count(),
+ 1
+ );
+ assert_eq!(
+ sqlx::query_scalar!(r#"SELECT COUNT(*) FROM "User""#)
+ .fetch_one(&pool)
+ .await
+ .unwrap(),
+ Some(0)
+ );
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn malformed_dm_is_not_reused_and_exact_duplicates_choose_oldest_then_id(pool: PgPool) {
+ let repo = PgChannelsRepo::new(pool.clone());
+ let malformed = repo
+ .ensure_dm(pair(), historical_dm(macro_uuid::generate_uuid_v7()))
+ .await
+ .unwrap();
+ // Legacy corruption includes even a departed third member.
+ sqlx::query!(
+ "INSERT INTO comms_channel_participants (channel_id, user_id, role, left_at) VALUES ($1, 'macro|[email protected]', 'member', NOW())",
+ malformed.id,
+ ).execute(&pool).await.unwrap();
+ let exact = repo
+ .ensure_dm(pair(), historical_dm(macro_uuid::generate_uuid_v7()))
+ .await
+ .unwrap();
+ assert!(exact.created);
+ assert_ne!(malformed.id, exact.id);
+ assert!(
+ repo.insert_historical_participants(
+ exact.id,
+ &[user("[email protected]")],
+ timestamp(1_000)
+ )
+ .await
+ .is_err()
+ );
+
+ // Simulate pre-lock duplicate DMs, including a historical leaver.
+ let duplicate_id = macro_uuid::generate_uuid_v7();
+ sqlx::query!(
+ "INSERT INTO comms_channels (id, owner_id, channel_type, created_at) VALUES ($1, 'macro|[email protected]', 'direct_message', $2)",
+ duplicate_id, timestamp(1_000),
+ ).execute(&pool).await.unwrap();
+ sqlx::query!(
+ "INSERT INTO comms_channel_participants (channel_id, user_id, role, joined_at, left_at) SELECT $1, user_id, role, joined_at, $3 FROM comms_channel_participants WHERE channel_id = $2",
+ duplicate_id, exact.id, timestamp(2_000),
+ ).execute(&pool).await.unwrap();
+ let expected = exact.id.min(duplicate_id);
+ assert_eq!(
+ repo.ensure_dm(pair(), DmCreation::Live(pair().hi().clone()))
+ .await
+ .unwrap(),
+ EnsuredChannel {
+ id: expected,
+ created: false
+ }
+ );
+ assert_eq!(
+ repo.maybe_get_dm(pair().hi().clone(), pair().lo().clone())
+ .await
+ .unwrap(),
+ Some(expected)
+ );
+ sqlx::query!(
+ "UPDATE comms_channels SET created_at = $2 WHERE id = $1",
+ duplicate_id,
+ timestamp(900)
+ )
+ .execute(&pool)
+ .await
+ .unwrap();
+ assert_eq!(
+ repo.ensure_dm(pair(), historical_dm(macro_uuid::generate_uuid_v7()))
+ .await
+ .unwrap()
+ .id,
+ duplicate_id
+ );
+ let left_count = sqlx::query_scalar!("SELECT COUNT(*) FROM comms_channel_participants WHERE channel_id = $1 AND left_at IS NOT NULL", duplicate_id).fetch_one(&pool).await.unwrap();
+ assert_eq!(left_count, Some(2));
+ assert_eq!(
+ sqlx::query_scalar!("SELECT COUNT(*) FROM comms_activity")
+ .fetch_one(&pool)
+ .await
+ .unwrap(),
+ Some(0)
+ );
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn importing_a_live_dm_preserves_owner_and_creates_no_additional_activity(pool: PgPool) {
+ let repo = PgChannelsRepo::new(pool.clone());
+ let live = repo
+ .ensure_dm(pair(), DmCreation::Live(pair().hi().clone()))
+ .await
+ .unwrap();
+ let before = repo.get_channel_participants(live.id).await.unwrap();
+ let imported = repo
+ .ensure_dm(pair(), historical_dm(macro_uuid::generate_uuid_v7()))
+ .await
+ .unwrap();
+ assert_eq!(
+ imported,
+ EnsuredChannel {
+ id: live.id,
+ created: false
+ }
+ );
+ let after = repo.get_channel_participants(live.id).await.unwrap();
+ for original in before {
+ let member = after
+ .iter()
+ .find(|p| p.user_id == original.user_id)
+ .unwrap();
+ assert_eq!(member.role, original.role);
+ assert_eq!(member.joined_at, original.joined_at);
+ }
+ assert_eq!(
+ after
+ .iter()
+ .find(|p| p.role == ParticipantRole::Owner)
+ .unwrap()
+ .user_id,
+ pair().hi().as_ref()
+ );
+ assert_eq!(
+ sqlx::query_scalar!("SELECT COUNT(*) FROM comms_activity")
+ .fetch_one(&pool)
+ .await
+ .unwrap(),
+ Some(1)
+ );
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn dm_rejects_outsider_owner_and_conflicting_reservation(pool: PgPool) {
+ let repo = PgChannelsRepo::new(pool.clone());
+ assert!(
+ repo.ensure_dm(pair(), DmCreation::Live(user("[email protected]")))
+ .await
+ .is_err()
+ );
+ let request = channel();
+ repo.create_historical_channel(&request).await.unwrap();
+ assert!(
+ repo.ensure_dm(pair(), historical_dm(request.id))
+ .await
+ .is_err()
+ );
+ let count = sqlx::query_scalar!("SELECT COUNT(*) FROM comms_channels")
+ .fetch_one(&pool)
+ .await
+ .unwrap();
+ assert_eq!(count, Some(1));
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn historical_dm_preserves_live_timestamps_and_activity_update_rolls_back(pool: PgPool) {
+ let repo = PgChannelsRepo::new(pool.clone());
+ let id = macro_uuid::generate_uuid_v7();
+ assert_eq!(
+ repo.ensure_dm(pair(), historical_dm(id)).await.unwrap(),
+ EnsuredChannel { id, created: true }
+ );
+ let initial = sqlx::query!(
+ "SELECT owner_id, created_at, updated_at, team_id FROM comms_channels WHERE id = $1",
+ id,
+ )
+ .fetch_one(&pool)
+ .await
+ .unwrap();
+ assert_eq!(initial.owner_id, pair().lo().as_ref());
+ assert_eq!(initial.team_id, None);
+ assert_eq!(initial.created_at, timestamp(1_000));
+ assert_eq!(initial.updated_at, timestamp(1_000));
+ assert!(
+ repo.get_channel_participants(id)
+ .await
+ .unwrap()
+ .iter()
+ .all(|p| p.joined_at == timestamp(1_000))
+ );
+
+ let mut tx = pool.begin().await.unwrap();
+ advance_historical_activity(&mut tx, id, timestamp(2_000))
+ .await
+ .unwrap();
+ tx.rollback().await.unwrap();
+ let rolled_back =
+ sqlx::query_scalar!("SELECT updated_at FROM comms_channels WHERE id = $1", id)
+ .fetch_one(&pool)
+ .await
+ .unwrap();
+ assert_eq!(rolled_back, timestamp(1_000));
+
+ repo.touch_channel_updated_at(id).await.unwrap();
+ let live_at = sqlx::query_scalar!("SELECT updated_at FROM comms_channels WHERE id = $1", id)
+ .fetch_one(&pool)
+ .await
+ .unwrap();
+ repo.advance_historical_activity(id, timestamp(2_000))
+ .await
+ .unwrap();
+ assert!(
+ !repo
+ .ensure_dm(pair(), historical_dm(macro_uuid::generate_uuid_v7()))
+ .await
+ .unwrap()
+ .created
+ );
+ let after = sqlx::query_scalar!("SELECT updated_at FROM comms_channels WHERE id = $1", id)
+ .fetch_one(&pool)
+ .await
+ .unwrap();
+ assert_eq!(after, live_at);
+ assert_eq!(
+ sqlx::query_scalar!("SELECT COUNT(*) FROM comms_activity")
+ .fetch_one(&pool)
+ .await
+ .unwrap(),
+ Some(0)
+ );
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn list_includes_unregistered_memberships_without_activity(pool: PgPool) {
+ let repo = PgChannelsRepo::new(pool);
+ let request = channel();
+ repo.create_historical_channel(&request).await.unwrap();
+ let params = GetChannelsRequest {
+ macro_id: user("[email protected]"),
+ limit: Some(10),
+ include_frecency: false,
+ query: Query::Sort(SimpleSortMethod::UpdatedAt, None),
+ }
+ .into_params();
+ let channels = repo
+ .get_user_channels_with_participants(params)
+ .await
+ .unwrap();
+ assert_eq!(channels.len(), 1);
+ assert_eq!(channels[0].channel.id, request.id);
+ assert_eq!(channels[0].participants.len(), 3);
+}
+
+#[sqlx::test(
+ fixtures(path = "../../../../fixtures", scripts("channels_repo")),
+ migrator = "MACRO_DB_MIGRATIONS"
+)]
+async fn team_creation_is_explicit_and_reuse_preserves_auto_join(pool: PgPool) {
+ let repo = PgChannelsRepo::new(pool.clone());
+ let mut request = channel();
+ request.kind =
+ HistoricalChannelKind::Team(Uuid::from_u128(0x11111111_1111_1111_1111_111111111111));
+ let (first, second) = tokio::join!(
+ repo.create_historical_channel(&request),
+ repo.create_historical_channel(&request)
+ );
+ assert_ne!(first.unwrap().created, second.unwrap().created);
+ let participants = repo.get_channel_participants(request.id).await.unwrap();
+ assert_eq!(participants.len(), 3);
+ assert_eq!(
+ participants
+ .iter()
+ .find(|p| p.role == ParticipantRole::Owner)
+ .unwrap()
+ .user_id,
+ request.owner.as_ref()
+ );
+ let auto_join = sqlx::query_scalar!(
+ "SELECT auto_join_team FROM comms_channels WHERE id = $1",
+ request.id
+ )
+ .fetch_one(&pool)
+ .await
+ .unwrap();
+ assert!(!auto_join);
+ sqlx::query!(
+ "UPDATE comms_channels SET auto_join_team = true WHERE id = $1",
+ request.id
+ )
+ .execute(&pool)
+ .await
+ .unwrap();
+ assert!(
+ !repo
+ .create_historical_channel(&request)
+ .await
+ .unwrap()
+ .created
+ );
+ assert!(
+ sqlx::query_scalar!(
+ "SELECT auto_join_team FROM comms_channels WHERE id = $1",
+ request.id
+ )
+ .fetch_one(&pool)
+ .await
+ .unwrap()
+ );
+ request.kind = HistoricalChannelKind::Private;
+ assert!(repo.create_historical_channel(&request).await.is_err());
+}
+
+#[sqlx::test(
+ fixtures(path = "../../../../fixtures", scripts("channels_repo")),
+ migrator = "MACRO_DB_MIGRATIONS"
+)]
+async fn onboarding_and_archive_share_reserved_target_without_replaying_effects(pool: PgPool) {
+ let repo = PgChannelsRepo::new(pool.clone());
+ let events = CreationEvents::default();
+ let service = ChannelServiceImpl::with_dependencies(
+ repo.clone(),
+ events.clone(),
+ NoopChannelReferenceSharePermissions,
+ );
+ let team_id = Uuid::from_u128(0x11111111_1111_1111_1111_111111111111);
+ for order in 0..3 {
+ let before_channels = sqlx::query_scalar!("SELECT COUNT(*) FROM comms_channels")
+ .fetch_one(&pool)
+ .await
+ .unwrap()
+ .unwrap();
+ let before_activity = sqlx::query_scalar!("SELECT COUNT(*) FROM comms_activity")
+ .fetch_one(&pool)
+ .await
+ .unwrap()
+ .unwrap();
+ let mut request = channel();
+ request.kind = HistoricalChannelKind::Team(team_id);
+ request.owner = user("[email protected]");
+ let onboarding = || {
+ service.create_reserved_team_channel(
+ request.owner.clone(),
+ request.id,
+ team_id,
+ "onboarding name".into(),
+ HashSet::new(),
+ )
+ };
+ let (archive, onboarded) = match order {
+ 0 => (
+ repo.create_historical_channel(&request).await.unwrap(),
+ onboarding().await.unwrap(),
+ ),
+ 1 => {
+ let onboarded = onboarding().await.unwrap();
+ (
+ repo.create_historical_channel(&request).await.unwrap(),
+ onboarded,
+ )
+ }
+ _ => {
+ let (archive, onboarded) =
+ tokio::join!(repo.create_historical_channel(&request), onboarding());
+ (archive.unwrap(), onboarded.unwrap())
+ }
+ };
+ assert_eq!(archive.id, onboarded.id);
+ assert_ne!(archive.created, onboarded.created);
+ assert!(!onboarding().await.unwrap().created);
+ assert!(
+ !repo
+ .create_historical_channel(&request)
+ .await
+ .unwrap()
+ .created
+ );
+ assert_eq!(
+ events
+ .0
+ .lock()
+ .unwrap()
+ .iter()
+ .filter(|id| **id == request.id)
+ .count(),
+ usize::from(onboarded.created)
+ );
+ assert_eq!(
+ sqlx::query_scalar!("SELECT COUNT(*) FROM comms_channels")
+ .fetch_one(&pool)
+ .await
+ .unwrap(),
+ Some(before_channels + 1)
+ );
+ assert_eq!(
+ sqlx::query_scalar!("SELECT COUNT(*) FROM comms_activity")
+ .fetch_one(&pool)
+ .await
+ .unwrap(),
+ Some(before_activity + i64::from(onboarded.created))
+ );
+ let persisted = sqlx::query!(
+ "SELECT name, owner_id, team_id, auto_join_team, created_at, updated_at FROM comms_channels WHERE id = $1",
+ request.id,
+ ).fetch_one(&pool).await.unwrap();
+ assert_eq!(persisted.team_id, Some(team_id));
+ assert!(!persisted.auto_join_team);
+ if archive.created {
+ assert_eq!(persisted.name.as_deref(), Some(request.name.as_str()));
+ assert_eq!(persisted.created_at, request.created_at);
+ } else {
+ assert_eq!(persisted.name.as_deref(), Some("onboarding name"));
+ assert!(persisted.created_at > request.created_at);
+ }
+ // Reuse does not grant access to a different team's caller.
+ let denied = service
+ .create_reserved_team_channel(
+ request.id,
+ team_id,
+ "forbidden".into(),
+ HashSet::new(),
+ )
+ .await;
+ assert!(matches!(denied, Err(ChannelMutationErr::Unauthorized(_))));
+ }
+}
diff --git a/crates/comms_db_client/src/messages/get_channel_message.rs b/crates/comms_db_client/src/messages/get_channel_message.rs
index ca2811d1dd..ec123c7453 100644
--- a/crates/comms_db_client/src/messages/get_channel_message.rs
+++ b/crates/comms_db_client/src/messages/get_channel_message.rs
@@ -1,5 +1,5 @@
use crate::model::SimpleMention;
-use channels::domain::models::{ChannelType, GetChannelMessageResponse, RecentChannelMessage};
+use channels::domain::models::ChannelType;
use uuid::Uuid;
#[derive(Debug, serde::Serialize, serde::Deserialize)]
@@ -11,12 +11,20 @@ pub struct ChannelMessageInfo {
pub message_id: Uuid,
pub thread_id: Option<Uuid>,
pub sender_id: String,
+ pub imported_author: Option<String>,
pub content: String,
pub created_at: chrono::DateTime<chrono::Utc>,
pub updated_at: chrono::DateTime<chrono::Utc>,
pub deleted_at: Option<chrono::DateTime<chrono::Utc>>,
}
+/// Search-only projection; fallback author text is not a Macro sender identity.
+#[derive(Debug)]
+pub struct ChannelMessageForSearch {
+ pub message: ChannelMessageInfo,
+ pub mentions: Vec<String>,
+}
+
/// Gets a channel message by channel id and message id
/// This is used to get content necessary to populate the channel message for search
#[tracing::instrument(skip(db))]
@@ -24,7 +32,7 @@ pub async fn get_channel_message_by_id(
db: &sqlx::Pool<sqlx::Postgres>,
channel_id: &Uuid,
message_id: &Uuid,
-) -> anyhow::Result<GetChannelMessageResponse> {
+) -> anyhow::Result<ChannelMessageForSearch> {
let mentions: Vec<String> = sqlx::query!(
r#"
SELECT
@@ -58,6 +66,7 @@ pub async fn get_channel_message_by_id(
m.id as "message_id",
m.thread_id as "thread_id",
m.sender_id as "sender_id",
+ m.imported_author,
m.content as "content",
m.created_at as "created_at",
m.updated_at as "updated_at",
@@ -76,20 +85,11 @@ pub async fn get_channel_message_by_id(
.fetch_one(db)
.await?;
- Ok(GetChannelMessageResponse {
- channel_id: channel_message_info.channel_id,
- name: channel_message_info.name,
- channel_type: channel_message_info.channel_type,
- org_id: channel_message_info.org_id,
- channel_message: RecentChannelMessage {
- message_id: channel_message_info.message_id,
- thread_id: channel_message_info.thread_id,
- sender_id: channel_message_info.sender_id,
- content: channel_message_info.content,
- created_at: channel_message_info.created_at,
- updated_at: channel_message_info.updated_at,
- deleted_at: channel_message_info.deleted_at,
- mentions,
- },
+ Ok(ChannelMessageForSearch {
+ message: channel_message_info,
+ mentions,
})
}
+
+#[cfg(test)]
+mod test;
diff --git a/crates/comms_db_client/src/messages/get_channel_message/test.rs b/crates/comms_db_client/src/messages/get_channel_message/test.rs
new file mode 100644
index 0000000000..fd55ebf249
--- /dev/null
+++ b/crates/comms_db_client/src/messages/get_channel_message/test.rs
@@ -0,0 +1,37 @@
+use super::*;
+use macro_db_migrator::MACRO_DB_MIGRATIONS;
+
+#[sqlx::test(
+ migrator = "MACRO_DB_MIGRATIONS",
+ fixtures(path = "../../../fixtures", scripts("channels"))
+)]
+async fn search_projection_preserves_author_and_sender(pool: sqlx::PgPool) -> anyhow::Result<()> {
+ let channel_id = Uuid::from_u128(0x11111111_1111_1111_1111_111111111111);
+ let message_id = macro_uuid::generate_uuid_v7();
+ for author in [Some("unique-archive-bot"), None] {
+ sqlx::query!(
+ r#"
+ INSERT INTO comms_messages (id, channel_id, sender_id, content, imported_author)
+ VALUES ($1, $2, 'macro|[email protected]', 'Historical content', $3)
+ ON CONFLICT (id) DO UPDATE SET imported_author = EXCLUDED.imported_author
+ "#,
+ message_id,
+ channel_id,
+ author,
+ )
+ .execute(&pool)
+ .await?;
+ let result = get_channel_message_by_id(&pool, &channel_id, &message_id).await?;
+ assert_eq!(result.message.imported_author.as_deref(), author);
+ assert_eq!(result.message.sender_id, "macro|[email protected]");
+ assert_eq!(result.message.content, "Historical content");
+ assert_eq!(result.message.message_id, message_id);
+ assert!(result.mentions.is_empty());
+ }
+ assert!(
+ get_channel_message_by_id(&pool, &Uuid::nil(), &message_id)
+ .await
+ .is_err()
+ );
+ Ok(())
+}
diff --git a/crates/comms_db_client/src/messages/get_messages.rs b/crates/comms_db_client/src/messages/get_messages.rs
index 2a67c68f77..23f41cb102 100644
--- a/crates/comms_db_client/src/messages/get_messages.rs
+++ b/crates/comms_db_client/src/messages/get_messages.rs
@@ -1,7 +1,64 @@
use anyhow::{Context, Result};
+use chrono::{DateTime, Utc};
use sqlx::{Pool, Postgres};
use uuid::Uuid;
+/// One channel message and its stable search-backfill pagination key.
+#[derive(Debug)]
+pub struct ChannelMessageBackfillRow {
+ /// Owning channel (non-channel messages are excluded).
+ pub channel_id: Uuid,
+ /// Message to publish to search.
+ pub message_id: Uuid,
+ /// Original creation timestamp; ID breaks ties between messages.
+ pub created_at: DateTime<Utc>,
+}
+
+/// Enumerate channel messages in stable `(created_at, id)` order.
+///
+/// `None` includes all channels, while `Some(&[])` includes none. Each page is
+/// a fresh read, not a snapshot: history committed behind the cursor requires
+/// another scoped run after the import commits. Repeating a run is safe because
+/// downstream indexing upserts by message ID.
+#[tracing::instrument(skip(db), err)]
+pub async fn get_channel_messages_for_search_backfill(
+ db: &Pool<Postgres>,
+ limit: i64,
+ cursor: Option<(DateTime<Utc>, Uuid)>,
+ channel_ids: Option<&[Uuid]>,
+ only_deleted: Option<bool>,
+) -> Result<Vec<ChannelMessageBackfillRow>> {
+ let (created_at, message_id) = cursor.unzip();
+ sqlx::query_as!(
+ ChannelMessageBackfillRow,
+ r#"
+ SELECT channel_id AS "channel_id!", id AS message_id, created_at
+ FROM comms_messages
+ WHERE channel_id IS NOT NULL
+ AND ($2::timestamptz IS NULL OR (created_at, id) > ($2, $3::uuid))
+ AND ($4::uuid[] IS NULL OR channel_id = ANY($4))
+ AND (
+ $5::bool IS NULL
+ OR ($5 AND deleted_at IS NOT NULL)
+ OR (NOT $5 AND deleted_at IS NULL)
+ )
+ ORDER BY created_at ASC, id ASC
+ LIMIT $1
+ "#,
+ limit,
+ created_at,
+ message_id,
+ channel_ids,
+ only_deleted,
+ )
+ .fetch_all(db)
+ .await
+ .context("unable to get channel messages for search backfill")
+}
+
+#[cfg(test)]
+mod test;
+
/// Paginated query to get all messages and their channel id
/// Used for backfilling search
#[tracing::instrument(skip(db))]
diff --git a/crates/comms_db_client/src/messages/get_messages/test.rs b/crates/comms_db_client/src/messages/get_messages/test.rs
new file mode 100644
index 0000000000..0ba6da9f71
--- /dev/null
+++ b/crates/comms_db_client/src/messages/get_messages/test.rs
@@ -0,0 +1,138 @@
+use super::*;
+use macro_db_migrator::MACRO_DB_MIGRATIONS;
+
+const CHANNEL: Uuid = Uuid::from_u128(0x11111111_1111_1111_1111_111111111111);
+const OTHER_CHANNEL: Uuid = Uuid::from_u128(0x33333333_3333_3333_3333_333333333333);
+
+async fn seed(pool: &Pool<Postgres>) -> Result<Vec<Uuid>> {
+ let mut ids: Vec<_> = (0..4).map(|_| macro_uuid::generate_uuid_v7()).collect();
+ ids.sort_unstable();
+ sqlx::query!(
+ r#"
+ INSERT INTO comms_messages (id, channel_id, sender_id, content, created_at, deleted_at)
+ SELECT id, channel_id, 'macro|[email protected]', 'history',
+ '2020-01-01T00:00:00.123456Z'::timestamptz, deleted_at
+ FROM UNNEST($1::uuid[], $2::uuid[], $3::timestamptz[]) AS rows(id, channel_id, deleted_at)
+ "#,
+ &ids,
+ &[CHANNEL, CHANNEL, OTHER_CHANNEL, CHANNEL],
+ &[None, None, None, Some(Utc::now())] as &[Option<DateTime<Utc>>],
+ )
+ .execute(pool)
+ .await?;
+ Ok(ids)
+}
+
+#[sqlx::test(
+ migrator = "MACRO_DB_MIGRATIONS",
+ fixtures(path = "../../../fixtures", scripts("channels"))
+)]
+async fn backfill_includes_replies_but_not_document_messages(pool: Pool<Postgres>) -> Result<()> {
+ let messages = seed(&pool).await?;
+ let reply = macro_uuid::generate_uuid_v7();
+ let document_message = macro_uuid::generate_uuid_v7();
+ sqlx::query!(
+ r#"
+ INSERT INTO comms_messages (id, parent_entity_type, parent_entity_id, thread_id, sender_id, content, created_at)
+ VALUES ($1, 'channel', $2, $3, 'macro|[email protected]', 'reply', '2020-01-02T00:00:00Z'),
+ ($4, 'document', 'doc-backfill-test', NULL, 'macro|[email protected]', 'document', '2020-01-02T00:00:00Z')
+ "#,
+ reply,
+ CHANNEL.to_string(),
+ messages[0],
+ document_message,
+ )
+ .execute(&pool)
+ .await?;
+ let mut cursor = None;
+ let mut found = Vec::new();
+ loop {
+ let page = get_channel_messages_for_search_backfill(&pool, 1, cursor, None, None).await?;
+ let Some(row) = page.last() else { break };
+ cursor = Some((row.created_at, row.message_id));
+ found.extend(ids(&page));
+ }
+ assert_eq!(found, [messages, vec![reply]].concat());
+ Ok(())
+}
+
+fn ids(rows: &[ChannelMessageBackfillRow]) -> Vec<Uuid> {
+ rows.iter().map(|row| row.message_id).collect()
+}
+
+#[sqlx::test(
+ migrator = "MACRO_DB_MIGRATIONS",
+ fixtures(path = "../../../fixtures", scripts("channels"))
+)]
+async fn backfill_scope_and_deletion_filters(pool: Pool<Postgres>) -> Result<()> {
+ let messages = seed(&pool).await?;
+ let all = get_channel_messages_for_search_backfill(&pool, 10, None, None, None).await?;
+ assert_eq!(ids(&all), messages);
+
+ for (filter, expected) in [
+ (None, vec![messages[0], messages[1], messages[3]]),
+ (Some(false), vec![messages[0], messages[1]]),
+ (Some(true), vec![messages[3]]),
+ ] {
+ let rows =
+ get_channel_messages_for_search_backfill(&pool, 10, None, Some(&[CHANNEL]), filter)
+ .await?;
+ assert_eq!(ids(&rows), expected);
+ assert!(rows.iter().all(|row| row.channel_id == CHANNEL));
+ }
+ for scope in [vec![], vec![Uuid::nil()]] {
+ assert!(
+ get_channel_messages_for_search_backfill(&pool, 10, None, Some(&scope), None)
+ .await?
+ .is_empty()
+ );
+ }
+ let multiple = get_channel_messages_for_search_backfill(
+ &pool,
+ 10,
+ None,
+ Some(&[CHANNEL, OTHER_CHANNEL, CHANNEL]),
+ None,
+ )
+ .await?;
+ assert_eq!(ids(&multiple), messages);
+ Ok(())
+}
+
+#[sqlx::test(
+ migrator = "MACRO_DB_MIGRATIONS",
+ fixtures(path = "../../../fixtures", scripts("channels"))
+)]
+async fn backfill_keyset_preserves_timestamp_ties_after_deletion(
+ pool: Pool<Postgres>,
+) -> Result<()> {
+ let messages = seed(&pool).await?;
+ let first =
+ get_channel_messages_for_search_backfill(&pool, 1, None, Some(&[CHANNEL]), Some(false))
+ .await?;
+ assert_eq!(ids(&first), vec![messages[0]]);
+ let cursor = Some((first[0].created_at, first[0].message_id));
+ // Removing a row from the active scan would shift an offset past the next
+ // message. The keyset must still return the other row at this timestamp.
+ sqlx::query!(
+ "UPDATE comms_messages SET deleted_at = now() WHERE id = $1",
+ messages[0]
+ )
+ .execute(&pool)
+ .await?;
+ let second =
+ get_channel_messages_for_search_backfill(&pool, 1, cursor, Some(&[CHANNEL]), Some(false))
+ .await?;
+ assert_eq!(ids(&second), vec![messages[1]]);
+ assert_eq!(first[0].created_at, second[0].created_at);
+ let end = get_channel_messages_for_search_backfill(
+ &pool,
+ 1,
+ Some((second[0].created_at, second[0].message_id)),
+ Some(&[CHANNEL]),
+ Some(false),
+ )
+ .await?;
+ assert!(end.is_empty());
+ Ok(())
+}
diff --git a/crates/entity_access/src/domain/service.rs b/crates/entity_access/src/domain/service.rs
index 763465692d..8ceccdddc4 100644
--- a/crates/entity_access/src/domain/service.rs
+++ b/crates/entity_access/src/domain/service.rs
@@ -176,7 +176,14 @@ where
.get_entity_permission(Some(user_id), entity_id, entity_type, user_org_id)
.await;
}
+ self.get_team_permission(user_id, entity_id).await
+ }
+ async fn get_team_permission(
+ &self,
+ user_id: &MacroUserId<Lowercase<'_>>,
+ entity_id: &str,
+ ) -> Result<EntityPermission, AccessError> {
let requested_team_id = Uuid::parse_str(entity_id)
.map_err(|_| AccessError::BadRequest("Invalid team ID format"))?;
let user_team = self
@@ -519,6 +526,10 @@ where
user_org_id: Option<i64>,
) -> Result<EntityPermission, AccessError> {
match entity_type {
+ EntityType::Team => {
+ let user_id = user_id.ok_or(AccessError::Unauthorized)?;
+ self.get_team_permission(user_id, entity_id).await
+ }
EntityType::Document
| EntityType::Chat
| EntityType::Project
diff --git a/crates/entity_access/src/domain/service/test.rs b/crates/entity_access/src/domain/service/test.rs
index 7a4901d07f..c61d51acf3 100644
--- a/crates/entity_access/src/domain/service/test.rs
+++ b/crates/entity_access/src/domain/service/test.rs
@@ -752,6 +752,59 @@ async fn test_unsupported_entity_type_returns_none() {
// --- get_entity_permission tests ---
+#[tokio::test]
+async fn team_admin_receipt_checks_current_role_and_exact_team() {
+ let repo = MockRepo::new();
+ let service = EntityAccessServiceImpl::new(repo.clone());
+ let user = test_user_id();
+ let team = Uuid::now_v7();
+ let team_id = team.to_string();
+ for role in [TeamRole::Owner, TeamRole::Admin, TeamRole::Member] {
+ *repo.user_team.lock().await = Some(UserTeamInfo {
+ team_id: team,
+ role,
+ });
+ let receipt = service
+ .generate_entity_access_receipt::<AdminTeamRole>(
+ &user.0,
+ None,
+ &team_id,
+ EntityType::Team,
+ )
+ .await;
+ assert_eq!(receipt.is_ok(), role != TeamRole::Member);
+ assert!(
+ service
+ .generate_entity_access_receipt::<AdminTeamRole>(
+ &user.0,
+ None,
+ &Uuid::now_v7().to_string(),
+ EntityType::Team,
+ )
+ .await
+ .is_err()
+ );
+ }
+ *repo.user_team.lock().await = None;
+ assert!(
+ service
+ .generate_entity_access_receipt::<AdminTeamRole>(
+ &user.0,
+ None,
+ &team_id,
+ EntityType::Team,
+ )
+ .await
+ .is_err()
+ );
+ assert!(
+ service
+ .get_entity_permission(None, &team_id, EntityType::Team, None)
+ .await
+ .is_err()
+ );
+}
+
#[tokio::test]
async fn test_get_entity_permission_document_returns_access_level() {
let repo = MockRepo::new().with_document_access(AccessLevel::Edit);
diff --git a/crates/import/src/domain/models.rs b/crates/import/src/domain/models.rs
index 3f65f9b462..cd2d997aec 100644
--- a/crates/import/src/domain/models.rs
+++ b/crates/import/src/domain/models.rs
@@ -163,6 +163,8 @@ pub enum Initiator {
Onboarding,
/// Staged by an AI chat session.
Chat,
+ /// Imported from an administrator-uploaded archive.
+ Archive,
}
/// Lifecycle of a gather run (one per user × source).
@@ -196,6 +198,110 @@ pub enum RunStatus {
Dismissed,
}
+/// A stable Slack conversation ID, never a legacy channel name.
+#[derive(Debug, Clone, PartialEq, Eq, Hash)]
+pub struct SlackConversationId(String);
+
+impl SlackConversationId {
+ /// Validate the source ID without trimming or name normalization.
+ pub fn new(value: &str) -> Option<Self> {
+ valid_slack_id(value, b"CGD").then(|| Self(value.to_owned()))
+ }
+
+ /// The exact source identifier.
+ pub fn as_str(&self) -> &str {
+ &self.0
+ }
+}
+
+/// A stable Slack workspace ID.
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct SlackWorkspaceId(String);
+
+impl SlackWorkspaceId {
+ /// Validate a Slack workspace ID.
+ pub fn new(value: &str) -> Option<Self> {
+ valid_slack_id(value, b"T").then(|| Self(value.to_owned()))
+ }
+
+ /// The exact source identifier.
+ pub fn as_str(&self) -> &str {
+ &self.0
+ }
+}
+
+fn valid_slack_id(value: &str, prefixes: &[u8]) -> bool {
+ (2..=64).contains(&value.len())
+ && prefixes.contains(&value.as_bytes()[0])
+ && value
+ .bytes()
+ .all(|byte| byte.is_ascii_uppercase() || byte.is_ascii_digit())
+}
+
+/// Durable single-workspace binding for a Macro team. Absence of this row means unbound.
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct ImportSourceBinding {
+ /// Known workspace, if the source supplied identity.
+ pub workspace_id: Option<SlackWorkspaceId>,
+ /// First explicit administrator confirmation of an unidentified archive.
+ pub confirmed_unknown_at: Option<DateTime<Utc>>,
+}
+
+/// Explicit canonical namespace. This API supports only the Slack source in v1;
+/// it never derives the team from the requesting user's current membership.
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct ImportTargetKey {
+ /// Team that owns the source namespace, including private/DM provenance.
+ pub team_id: Uuid,
+ /// Stable source identity, not a name-only onboarding identifier.
+ pub foreign_id: SlackConversationId,
+}
+
+/// Allowed target shapes. Slack public channels become Team, never Public.
+#[derive(Debug, Clone, Copy, PartialEq, Eq, strum::AsRefStr)]
+#[strum(serialize_all = "snake_case")]
+pub enum ImportTargetKind {
+ /// Shared with exactly the explicitly supplied team.
+ Team,
+ /// Private channel or group DM; team provenance lives only in the reservation.
+ Private,
+ /// Two-person DM; pair validation/authorization belongs to the channel service.
+ DirectMessage,
+}
+
+/// A committed reservation survives crashes before channel creation.
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct ImportTargetReservation {
+ /// Canonical source namespace.
+ pub key: ImportTargetKey,
+ /// Stable UUID to pass to idempotent channel creation, or an existing claimed target.
+ pub channel_id: Uuid,
+ /// Whether channel creation has been durably completed.
+ pub ready: bool,
+}
+
+/// Maximum exact source IDs per canonical read; validated IDs bound input bytes
+/// to 32 KiB, below the archive database batch byte ceiling.
+pub const MAX_TARGET_LOOKUP: usize = 500;
+
+/// Read-only canonical mapping state. Pending candidates are deliberately hidden.
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub enum ImportTargetLookup {
+ /// No unambiguous, compatible target exists.
+ Missing,
+ /// Creation has not durably completed.
+ Pending,
+ /// Existing compatible channel; provenance is not a read-access grant.
+ Ready {
+ /// Canonical channel identity.
+ channel_id: Uuid,
+ /// Persisted channel kind.
+ kind: ImportTargetKind,
+ /// Persisted name, disclose only after authorization.
+ name: String,
+ },
+}
+
// ---------------------------------------------------------------------------
// Per-source metadata
// ---------------------------------------------------------------------------
diff --git a/crates/import/src/domain/models/test.rs b/crates/import/src/domain/models/test.rs
index 1d9a0ee114..bf5c531b88 100644
--- a/crates/import/src/domain/models/test.rs
+++ b/crates/import/src/domain/models/test.rs
@@ -24,7 +24,7 @@ fn enums_round_trip_through_strings() {
status.as_ref()
);
}
- for initiator in [Initiator::Onboarding, Initiator::Chat] {
+ for initiator in [Initiator::Onboarding, Initiator::Chat, Initiator::Archive] {
assert_eq!(Initiator::from_str(initiator.as_ref()).unwrap(), initiator);
}
for run in [
@@ -39,6 +39,33 @@ fn enums_round_trip_through_strings() {
}
}
+#[test]
+fn canonical_slack_ids_reject_names_and_wrong_namespaces() {
+ for id in ["C0123456789", "GABC123", "D123", "C1"] {
+ assert_eq!(SlackConversationId::new(id).unwrap().as_str(), id);
+ }
+ for id in [
+ "",
+ "C",
+ "general",
+ "#general",
+ "#C0123456789",
+ "Channels",
+ " C123",
+ "C123 ",
+ "T123",
+ "C../123",
+ "Cé",
+ ] {
+ assert!(SlackConversationId::new(id).is_none(), "{id}");
+ }
+ assert!(SlackConversationId::new(&format!("C{}", "A".repeat(64))).is_none());
+ assert_eq!(SlackWorkspaceId::new("T123").unwrap().as_str(), "T123");
+ for id in ["", "T", "team", "Tlower", "C123", " T123"] {
+ assert!(SlackWorkspaceId::new(id).is_none(), "{id}");
+ }
+}
+
#[test]
fn normalize_foreign_id_per_source() {
// Linear: trimmed pass-through, blank means none.
diff --git a/crates/import/src/domain/ports.rs b/crates/import/src/domain/ports.rs
index 7766409be2..658d13c401 100644
--- a/crates/import/src/domain/ports.rs
+++ b/crates/import/src/domain/ports.rs
@@ -1,6 +1,9 @@
//! Ports (traits) the import domain depends on.
-use super::models::{ImportEntity, ImportRun, ImportSource, ImportStatus, Initiator, RunStatus};
+use super::models::{
+ ImportEntity, ImportRun, ImportSource, ImportSourceBinding, ImportStatus, ImportTargetKey,
+ ImportTargetKind, ImportTargetReservation, Initiator, RunStatus, SlackWorkspaceId,
+};
use macro_user_id::user_id::MacroUserIdStr;
use thiserror::Error;
use uuid::Uuid;
@@ -17,6 +20,18 @@ pub enum ImportError {
/// Metadata failed to (de)serialize.
#[error("invalid import metadata: {0}")]
Metadata(#[from] serde_json::Error),
+ /// An unidentified archive requires explicit administrator confirmation.
+ #[error("source identity requires administrator confirmation")]
+ SourceConfirmationRequired,
+ /// The team is already bound to a different Slack workspace.
+ #[error("source workspace does not match the team's binding")]
+ SourceMismatch,
+ /// Historical mappings or target shape/provenance are incompatible. No target IDs are exposed.
+ #[error("conflicting import target mapping")]
+ TargetConflict,
+ /// Completion must follow a committed reservation for exactly this UUID.
+ #[error("import target is not reserved")]
+ TargetNotReserved,
/// Anything else.
#[error(transparent)]
Other(#[from] anyhow::Error),
@@ -238,6 +253,71 @@ pub trait ImportRepo: Send + Sync + 'static {
) -> impl Future<Output = Result<u64>> + Send;
}
+/// Narrow canonical Slack ledger persistence shared by archive and onboarding.
+///
+/// Callers authorize the explicit team before invoking this port. Target access,
+/// exact DM membership, and administrator authorization remain domain-service
+/// responsibilities: a reservation is provenance, not an access grant.
+/// Each operation commits and releases its connection before returning.
+pub trait CanonicalImportRepo: Send + Sync + 'static {
+ /// Read the durable binding; `None` means the team is not yet bound.
+ fn source_binding(
+ &self,
+ team_id: Uuid,
+ ) -> impl Future<Output = Result<Option<ImportSourceBinding>>> + Send;
+
+ /// Bind once, reject known mismatches, and retain the first unknown-source
+ /// confirmation timestamp. Every unidentified archive requires confirmation,
+ /// even when a known binding exists. A later known identity may resolve a
+ /// previously confirmed-unknown binding but never replace a known identity.
+ fn bind_source(
+ &self,
+ team_id: Uuid,
+ workspace_id: Option<&SlackWorkspaceId>,
+ confirmed_unknown: bool,
+ ) -> impl Future<Output = Result<ImportSourceBinding>> + Send;
+
+ /// Reserve one stable UUID, considering BOTH own imported rows (even after a
+ /// team change) and rows explicitly shared with this team. Reject ambiguous,
+ /// cross-team, non-channel or incompatible mappings rather than choosing one.
+ /// `existing_channel_id` is an optional already-authorized target to claim
+ /// (e.g. an exact-pair DM); it must agree with every historical mapping and
+ /// cannot replace a committed pending UUID. Without it, allocate UUIDv7.
+ /// Private/DM historical targets require either this explicit authorization
+ /// or an earlier ready reservation in this namespace.
+ fn reserve_target(
+ &self,
+ user: &MacroUserIdStr<'static>,
+ key: &ImportTargetKey,
+ kind: ImportTargetKind,
+ existing_channel_id: Option<Uuid>,
+ ) -> impl Future<Output = Result<ImportTargetReservation>> + Send;
+
+ /// Complete exactly the reserved UUID after idempotent channel creation.
+ /// Revalidate target type/team and preserve the first mapping. Private/DM
+ /// provenance is stored ONLY in the reservation, never a team-visible ledger
+ /// row. Team ledger rows may be published separately through `ImportRepo`.
+ fn complete_target(
+ &self,
+ key: &ImportTargetKey,
+ channel_id: Uuid,
+ kind: ImportTargetKind,
+ ) -> impl Future<Output = Result<ImportTargetReservation>> + Send;
+}
+
+/// Side-effect-free canonical reads, separate from reservation and legacy listings.
+pub trait ImportTargetReader: Send + Sync + 'static {
+ /// Exact IDs in an explicit bound namespace. At most 500 IDs per call; returns
+ /// one outcome per input, in order. Unknown-source confirmation is not domain
+ /// evidence. Never creates reservations, locks rows, or grants read access.
+ fn lookup_targets(
+ &self,
+ team: Uuid,
+ binding: &ImportSourceBinding,
+ channels: &[super::models::SlackConversationId],
+ ) -> impl Future<Output = Result<Vec<super::models::ImportTargetLookup>>> + Send;
+}
+
/// System properties to set on an imported task, already normalized to
/// Macro's vocabulary by the import service (the creator applies them
/// best-effort — a property that fails to apply never fails the import).
@@ -343,15 +423,16 @@ pub trait EntityCreator: Send + Sync + 'static {
properties: &ImportedDocumentProperties,
) -> impl Future<Output = anyhow::Result<String>> + Send;
- /// Create a channel, shared with `team_id` when given (public
- /// otherwise). `participant_emails` are matched against the team roster
- /// best-effort; matches join the channel alongside the creator. Returns
- /// the new entity id.
+ /// Ensure the reserved Slack Team channel, using its canonical UUID and
+ /// normalized source identity. Reuse must preserve existing settings and
+ /// memberships and must not replay first-creation effects. Participant emails
+ /// are matched against the team roster best-effort on initial creation only.
+ /// Returns the persisted channel ID, never a discarded creation candidate.
fn create_channel(
&self,
user: &MacroUserIdStr<'static>,
name: &str,
- team_id: Option<Uuid>,
+ target: &ImportTargetReservation,
participant_emails: &[String],
- ) -> impl Future<Output = anyhow::Result<String>> + Send;
+ ) -> impl Future<Output = anyhow::Result<Uuid>> + Send;
}
diff --git a/crates/import/src/domain/service.rs b/crates/import/src/domain/service.rs
index 4f600ea7f6..2ddbc0056b 100644
--- a/crates/import/src/domain/service.rs
+++ b/crates/import/src/domain/service.rs
@@ -13,8 +13,8 @@
use super::models::*;
use super::ports::{
- EntityCreator, ImportError, ImportRepo, ImportedDocumentProperties, ImportedDocumentProperty,
- ImportedDocumentPropertyValue, ImportedTaskProperties, Result,
+ CanonicalImportRepo, EntityCreator, ImportError, ImportRepo, ImportedDocumentProperties,
+ ImportedDocumentProperty, ImportedDocumentPropertyValue, ImportedTaskProperties, Result,
};
use crate::inbound::toolset::{
ImportToolContext, ToolPolicy, gather_toolset, notion_import_toolset,
@@ -34,6 +34,7 @@ use uuid::Uuid;
mod admission;
mod prompts;
+mod slack;
#[cfg(test)]
mod test;
@@ -356,7 +357,7 @@ impl<R, S, C> ImportServiceImpl<R, S, C> {
impl<R, S, C> ImportServiceImpl<R, S, C>
where
- R: ImportRepo + Clone,
+ R: ImportRepo + CanonicalImportRepo + Clone,
S: ConnectorSelect,
C: EntityCreator,
{
@@ -812,29 +813,23 @@ where
}
}
ImportSource::Slack => {
- match serde_json::from_value::<SlackChannelMeta>(row.metadata.clone()) {
- Ok(meta) => {
- // Channels always associate with the user's team when
- // they have one — that is what makes team dedup work.
- match self.repo.user_team_id(user).await {
- Ok(team_id) => {
- // Teammates who were in the Slack channel join
- // the Macro one (matched by email downstream).
- let emails: Vec<String> = meta
- .participants
- .iter()
- .filter_map(|p| p.email.clone())
- .collect();
- self.creator
- .create_channel(user, &meta.name, team_id, &emails)
- .await
- .map(|id| (id, team_id))
- }
- Err(e) => Err(anyhow::anyhow!("team lookup failed: {e}")),
- }
- }
- Err(e) => Err(anyhow::anyhow!("invalid slack metadata: {e}")),
+ async {
+ let team_id = self
+ .repo
+ .user_team_id(user)
+ .await?
+ .ok_or_else(|| anyhow::anyhow!("Slack import requires a team"))?;
+ let id = slack::ensure_channel(
+ &self.repo,
+ self.creator.as_ref(),
+ user,
+ row,
+ team_id,
+ )
+ .await?;
+ Ok((id.to_string(), Some(team_id)))
}
+ .await
}
// Notion rows go through the agent session, never here.
ImportSource::Notion => return,
@@ -1014,7 +1009,7 @@ where
impl<R, S, C> ImportService for ImportServiceImpl<R, S, C>
where
- R: ImportRepo + Clone,
+ R: ImportRepo + CanonicalImportRepo + Clone,
S: ConnectorSelect,
C: EntityCreator,
{
@@ -1203,7 +1198,7 @@ where
impl<R, S, C> ImportStager for ImportServiceImpl<R, S, C>
where
- R: ImportRepo + Clone,
+ R: ImportRepo + CanonicalImportRepo + Clone,
S: ConnectorSelect,
C: EntityCreator,
{
@@ -1346,7 +1341,7 @@ where
impl<R, S, C> NotionPageImporter for ImportServiceImpl<R, S, C>
where
- R: ImportRepo + Clone,
+ R: ImportRepo + CanonicalImportRepo + Clone,
S: ConnectorSelect,
C: EntityCreator,
{
@@ -1481,7 +1476,7 @@ where
impl<R, S, C> ImportFinalizer for ImportServiceImpl<R, S, C>
where
- R: ImportRepo + Clone,
+ R: ImportRepo + CanonicalImportRepo + Clone,
S: ConnectorSelect,
C: EntityCreator,
{
diff --git a/crates/import/src/domain/service/slack.rs b/crates/import/src/domain/service/slack.rs
new file mode 100644
index 0000000000..c673e94170
--- /dev/null
+++ b/crates/import/src/domain/service/slack.rs
@@ -0,0 +1,57 @@
+//! Slack creation is fenced by a durable canonical target, not the per-user ledger CAS.
+
+use crate::domain::{
+ models::{
+ ImportEntity, ImportTargetKey, ImportTargetKind, SlackChannelMeta, SlackConversationId,
+ },
+ ports::{CanonicalImportRepo, EntityCreator},
+};
+use macro_user_id::user_id::MacroUserIdStr;
+use uuid::Uuid;
+
+#[cfg(test)]
+mod test;
+
+pub(super) async fn ensure_channel(
+ repo: &impl CanonicalImportRepo,
+ creator: &impl EntityCreator,
+ user: &MacroUserIdStr<'static>,
+ row: &ImportEntity,
+ team_id: Uuid,
+) -> anyhow::Result<Uuid> {
+ let meta: SlackChannelMeta = serde_json::from_value(row.metadata.clone())?;
+ // Never guess a source ID from a channel name. Older normalization prefixed
+ // G/D IDs with '#'; strip that prefix only when the result is a valid ID.
+ let foreign_id = SlackConversationId::new(row.foreign_id.trim_start_matches('#'))
+ .ok_or_else(|| anyhow::anyhow!("Slack import requires a stable conversation ID"))?;
+ if let Some(metadata_id) = &meta.channel_id {
+ anyhow::ensure!(
+ metadata_id.trim() == foreign_id.as_str(),
+ "Slack metadata does not match the source identity"
+ );
+ }
+ let key = ImportTargetKey {
+ team_id,
+ foreign_id,
+ };
+ let target = repo
+ .reserve_target(user, &key, ImportTargetKind::Team, None)
+ .await?;
+ if target.ready {
+ return Ok(target.channel_id);
+ }
+ let emails = meta
+ .participants
+ .iter()
+ .filter_map(|participant| participant.email.clone())
+ .collect::<Vec<_>>();
+ let channel_id = creator
+ .create_channel(user, &meta.name, &target, &emails)
+ .await?;
+ // Completion validates the persisted ID against the reservation. On retry,
+ // either the same pending UUID is ensured again or a ready mapping is reused.
+ let completed = repo
+ .complete_target(&key, channel_id, ImportTargetKind::Team)
+ .await?;
+ Ok(completed.channel_id)
+}
diff --git a/crates/import/src/domain/service/slack/test.rs b/crates/import/src/domain/service/slack/test.rs
new file mode 100644
index 0000000000..31c1229dc7
--- /dev/null
+++ b/crates/import/src/domain/service/slack/test.rs
@@ -0,0 +1,295 @@
+use super::*;
+use crate::domain::{
+ models::{
+ ImportSource, ImportSourceBinding, ImportStatus, ImportTargetReservation, Initiator,
+ SlackWorkspaceId,
+ },
+ ports::{ImportError, ImportedDocumentProperties, ImportedTaskProperties, Result},
+};
+use std::{collections::HashSet, sync::Mutex};
+
+#[derive(Default)]
+struct State {
+ target: Option<ImportTargetReservation>,
+ channels: HashSet<Uuid>,
+ live_creations: usize,
+ creator_calls: usize,
+ fail_before_creation: bool,
+ fail_after_creation: bool,
+ fail_completion: bool,
+ wrong_result: bool,
+}
+
+#[derive(Default)]
+struct Harness(Mutex<State>);
+
+impl CanonicalImportRepo for Harness {
+ async fn source_binding(&self, _: Uuid) -> Result<Option<ImportSourceBinding>> {
+ unreachable!("onboarding does not confirm unknown archive identities")
+ }
+
+ async fn bind_source(
+ &self,
+ _: Uuid,
+ _: Option<&SlackWorkspaceId>,
+ _: bool,
+ ) -> Result<ImportSourceBinding> {
+ unreachable!("onboarding does not confirm unknown archive identities")
+ }
+
+ async fn reserve_target(
+ &self,
+ _: &MacroUserIdStr<'static>,
+ key: &ImportTargetKey,
+ kind: ImportTargetKind,
+ existing: Option<Uuid>,
+ ) -> Result<ImportTargetReservation> {
+ assert_eq!(kind, ImportTargetKind::Team);
+ assert_eq!(existing, None);
+ let target = {
+ let mut state = self.0.lock().unwrap();
+ state
+ .target
+ .get_or_insert_with(|| ImportTargetReservation {
+ key: key.clone(),
+ channel_id: Uuid::now_v7(),
+ ready: false,
+ })
+ .clone()
+ };
+ assert_eq!(&target.key, key);
+ tokio::task::yield_now().await;
+ Ok(target)
+ }
+
+ async fn complete_target(
+ &self,
+ key: &ImportTargetKey,
+ channel_id: Uuid,
+ kind: ImportTargetKind,
+ ) -> Result<ImportTargetReservation> {
+ assert_eq!(kind, ImportTargetKind::Team);
+ let mut state = self.0.lock().unwrap();
+ if std::mem::take(&mut state.fail_completion) {
+ return Err(ImportError::Other(anyhow::anyhow!(
+ "interrupted before completion"
+ )));
+ }
+ if !state.channels.contains(&channel_id) {
+ return Err(ImportError::TargetConflict);
+ }
+ let target = state.target.as_mut().unwrap();
+ if &target.key != key || target.channel_id != channel_id {
+ return Err(ImportError::TargetConflict);
+ }
+ target.ready = true;
+ Ok(target.clone())
+ }
+}
+
+impl EntityCreator for Harness {
+ async fn create_task(
+ &self,
+ _: &MacroUserIdStr<'static>,
+ _: &str,
+ _: &str,
+ _: &ImportedTaskProperties,
+ ) -> anyhow::Result<String> {
+ unreachable!()
+ }
+
+ async fn create_markdown_doc(
+ &self,
+ _: &MacroUserIdStr<'static>,
+ _: &str,
+ _: &str,
+ _: &ImportedDocumentProperties,
+ ) -> anyhow::Result<String> {
+ unreachable!()
+ }
+
+ async fn create_channel(
+ &self,
+ _: &MacroUserIdStr<'static>,
+ _: &str,
+ target: &ImportTargetReservation,
+ _: &[String],
+ ) -> anyhow::Result<Uuid> {
+ tokio::task::yield_now().await;
+ let mut state = self.0.lock().unwrap();
+ state.creator_calls += 1;
+ anyhow::ensure!(
+ !std::mem::take(&mut state.fail_before_creation),
+ "interrupted after reservation"
+ );
+ if state.channels.insert(target.channel_id) {
+ state.live_creations += 1;
+ }
+ anyhow::ensure!(
+ !std::mem::take(&mut state.fail_after_creation),
+ "lost creation response"
+ );
+ if state.wrong_result {
+ return Ok(Uuid::now_v7());
+ }
+ Ok(target.channel_id)
+ }
+}
+
+fn fixture() -> (MacroUserIdStr<'static>, ImportEntity, ImportTargetKey) {
+ let user = MacroUserIdStr::try_from("macro|[email protected]".to_string()).unwrap();
+ let key = ImportTargetKey {
+ team_id: Uuid::now_v7(),
+ foreign_id: SlackConversationId::new("C0123456789").unwrap(),
+ };
+ let row = ImportEntity {
+ id: Uuid::now_v7(),
+ user_id: user.to_string(),
+ team_id: None,
+ source: ImportSource::Slack,
+ foreign_id: key.foreign_id.as_str().to_string(),
+ status: ImportStatus::Importing,
+ initiator: Initiator::Onboarding,
+ metadata: serde_json::json!({"name": "engineering", "channel_id": key.foreign_id.as_str()}),
+ entity_id: None,
+ entity_type: None,
+ last_error: None,
+ created_at: chrono::Utc::now(),
+ updated_at: chrono::Utc::now(),
+ };
+ (user, row, key)
+}
+
+impl Harness {
+ async fn archive(&self, key: &ImportTargetKey) -> Uuid {
+ // A different admin uses the same namespace and silently ensures the
+ // target, just as the archive worker does through its channel port.
+ let admin = MacroUserIdStr::try_from("macro|[email protected]".to_string()).unwrap();
+ let target = self
+ .reserve_target(&admin, key, ImportTargetKind::Team, None)
+ .await
+ .unwrap();
+ self.0.lock().unwrap().channels.insert(target.channel_id);
+ self.complete_target(key, target.channel_id, ImportTargetKind::Team)
+ .await
+ .unwrap()
+ .channel_id
+ }
+
+ fn assert_canonical(&self, id: Uuid, live_creations: usize) {
+ let state = self.0.lock().unwrap();
+ assert_eq!(state.channels, HashSet::from([id]));
+ assert_eq!(state.target.as_ref().unwrap().channel_id, id);
+ assert!(state.target.as_ref().unwrap().ready);
+ assert_eq!(state.live_creations, live_creations);
+ }
+}
+
+#[tokio::test]
+async fn archive_first_onboarding_reuses_without_invites_or_events() {
+ let h = Harness::default();
+ let (user, row, key) = fixture();
+ let archive = h.archive(&key).await;
+ assert_eq!(
+ ensure_channel(&h, &h, &user, &row, key.team_id)
+ .await
+ .unwrap(),
+ archive
+ );
+ h.assert_canonical(archive, 0);
+ assert_eq!(h.0.lock().unwrap().creator_calls, 0);
+}
+
+#[tokio::test]
+async fn onboarding_first_archive_reuses_target() {
+ let h = Harness::default();
+ let (user, row, key) = fixture();
+ let onboarding = ensure_channel(&h, &h, &user, &row, key.team_id)
+ .await
+ .unwrap();
+ assert_eq!(h.archive(&key).await, onboarding);
+ h.assert_canonical(onboarding, 1);
+}
+
+#[tokio::test]
+async fn concurrent_onboarding_and_archive_reservations_converge() {
+ let h = Harness::default();
+ let (user, row, key) = fixture();
+ let (onboarding, archive) = tokio::join!(
+ ensure_channel(&h, &h, &user, &row, key.team_id),
+ h.archive(&key),
+ );
+ assert_eq!(onboarding.unwrap(), archive);
+ let effects = h.0.lock().unwrap().live_creations;
+ assert!(effects <= 1);
+ h.assert_canonical(archive, effects);
+}
+
+#[tokio::test]
+async fn crash_retry_at_each_boundary_keeps_one_channel_and_mapping() {
+ for boundary in 0..3 {
+ let h = Harness::default();
+ let (user, row, key) = fixture();
+ {
+ let mut state = h.0.lock().unwrap();
+ state.fail_before_creation = boundary == 0;
+ state.fail_after_creation = boundary == 1;
+ state.fail_completion = boundary == 2;
+ }
+ assert!(
+ ensure_channel(&h, &h, &user, &row, key.team_id)
+ .await
+ .is_err()
+ );
+ let reserved = h.0.lock().unwrap().target.as_ref().unwrap().channel_id;
+ assert_eq!(
+ ensure_channel(&h, &h, &user, &row, key.team_id)
+ .await
+ .unwrap(),
+ reserved
+ );
+ // Also covers a crash after canonical completion but before the legacy
+ // importing-row CAS: retry returns the ready target without creating.
+ let calls = h.0.lock().unwrap().creator_calls;
+ assert_eq!(
+ ensure_channel(&h, &h, &user, &row, key.team_id)
+ .await
+ .unwrap(),
+ reserved
+ );
+ assert_eq!(h.0.lock().unwrap().creator_calls, calls);
+ h.assert_canonical(reserved, 1);
+ }
+}
+
+#[tokio::test]
+async fn completion_uses_returned_persisted_id_not_candidate() {
+ let h = Harness::default();
+ h.0.lock().unwrap().wrong_result = true;
+ let (user, row, key) = fixture();
+ assert!(
+ ensure_channel(&h, &h, &user, &row, key.team_id)
+ .await
+ .is_err()
+ );
+ assert!(!h.0.lock().unwrap().target.as_ref().unwrap().ready);
+}
+
+#[tokio::test]
+async fn ambiguous_or_missing_source_identity_fails_before_reservation() {
+ let h = Harness::default();
+ let (user, mut row, key) = fixture();
+ row.metadata["channel_id"] = serde_json::json!("COTHER");
+ assert!(
+ ensure_channel(&h, &h, &user, &row, key.team_id)
+ .await
+ .is_err()
+ );
+ row.foreign_id = "#engineering".to_string();
+ assert!(
+ ensure_channel(&h, &h, &user, &row, key.team_id)
+ .await
+ .is_err()
+ );
+ assert!(h.0.lock().unwrap().target.is_none());
+}
diff --git a/crates/import/src/domain/service/test/admission.rs b/crates/import/src/domain/service/test/admission.rs
index 407451ddbe..cebf1c4f25 100644
--- a/crates/import/src/domain/service/test/admission.rs
+++ b/crates/import/src/domain/service/test/admission.rs
@@ -53,6 +53,8 @@ struct Repo(Arc<Mutex<Ledger>>);
struct Ledger {
rows: Vec<ImportEntity>,
runs: Vec<ImportRun>,
+ team_id: Option<Uuid>,
+ target: Option<ImportTargetReservation>,
}
impl Repo {
@@ -62,7 +64,11 @@ impl Repo {
user_id: user().to_string(),
team_id: None,
source,
- foreign_id: Uuid::now_v7().to_string(),
+ foreign_id: if source == ImportSource::Slack {
+ "C0123456789".into()
+ } else {
+ Uuid::now_v7().to_string()
+ },
status: ImportStatus::Staged,
initiator: Initiator::Onboarding,
metadata: serde_json::json!({"title": "A page", "name": "general"}),
@@ -77,6 +83,61 @@ impl Repo {
}
}
+// Mixed imports still reserve and complete Slack targets when AI admission is
+// denied. Binding an archive source is outside these billing-policy tests.
+impl CanonicalImportRepo for Repo {
+ async fn source_binding(&self, _: Uuid) -> Result<Option<ImportSourceBinding>> {
+ panic!("admission fixtures must not read canonical bindings")
+ }
+
+ async fn bind_source(
+ &self,
+ _: Uuid,
+ _: Option<&SlackWorkspaceId>,
+ _: bool,
+ ) -> Result<ImportSourceBinding> {
+ panic!("admission fixtures must not bind canonical sources")
+ }
+
+ async fn reserve_target(
+ &self,
+ caller: &MacroUserIdStr<'static>,
+ key: &ImportTargetKey,
+ kind: ImportTargetKind,
+ existing: Option<Uuid>,
+ ) -> Result<ImportTargetReservation> {
+ assert_eq!(caller, &user());
+ assert_eq!(kind, ImportTargetKind::Team);
+ assert_eq!(existing, None);
+ let mut ledger = self.0.lock().unwrap();
+ assert_eq!(Some(key.team_id), ledger.team_id);
+ let target = ledger
+ .target
+ .get_or_insert_with(|| ImportTargetReservation {
+ key: key.clone(),
+ channel_id: Uuid::now_v7(),
+ ready: false,
+ });
+ assert_eq!(&target.key, key);
+ Ok(target.clone())
+ }
+
+ async fn complete_target(
+ &self,
+ key: &ImportTargetKey,
+ channel_id: Uuid,
+ kind: ImportTargetKind,
+ ) -> Result<ImportTargetReservation> {
+ assert_eq!(kind, ImportTargetKind::Team);
+ let mut ledger = self.0.lock().unwrap();
+ let target = ledger.target.as_mut().unwrap();
+ assert_eq!(&target.key, key);
+ assert_eq!(target.channel_id, channel_id);
+ target.ready = true;
+ Ok(target.clone())
+ }
+}
+
impl ImportRepo for Repo {
async fn list_runs(&self, caller: &MacroUserIdStr<'static>) -> Result<Vec<ImportRun>> {
assert_eq!(caller, &user());
@@ -227,7 +288,7 @@ impl ImportRepo for Repo {
Ok(true)
}
async fn user_team_id(&self, _: &MacroUserIdStr<'static>) -> Result<Option<Uuid>> {
- Ok(None)
+ Ok(self.0.lock().unwrap().team_id)
}
async fn fail_stale_importing(&self, _: &MacroUserIdStr<'static>, _: i64) -> Result<u64> {
Ok(0)
@@ -368,11 +429,11 @@ impl EntityCreator for Creator {
&self,
_: &MacroUserIdStr<'static>,
_: &str,
- _: Option<Uuid>,
+ target: &ImportTargetReservation,
_: &[String],
- ) -> anyhow::Result<String> {
+ ) -> anyhow::Result<Uuid> {
self.0.fetch_add(1, Ordering::SeqCst);
- Ok(Uuid::now_v7().to_string())
+ Ok(target.channel_id)
}
}
@@ -691,6 +752,7 @@ async fn delayed_notion_work_rechecks_current_allowance_and_mixed_work_survives(
*admission.error.lock().unwrap() = None;
let service = service(admission.clone());
service.admit_ai(&user()).await.unwrap();
+ service.repo.0.lock().unwrap().team_id = Some(Uuid::now_v7());
let notion = service.repo.seed(ImportSource::Notion);
let linear = service.repo.seed(ImportSource::Linear);
let slack = service.repo.seed(ImportSource::Slack);
diff --git a/crates/import/src/lib.rs b/crates/import/src/lib.rs
index b8906c8f64..48850ebd8b 100644
--- a/crates/import/src/lib.rs
+++ b/crates/import/src/lib.rs
@@ -18,3 +18,4 @@
pub mod domain;
pub mod inbound;
pub mod outbound;
+pub mod source_binding;
diff --git a/crates/import/src/outbound/pg_import_repo.rs b/crates/import/src/outbound/pg_import_repo.rs
index 5aabf4c13c..6fa8eb9425 100644
--- a/crates/import/src/outbound/pg_import_repo.rs
+++ b/crates/import/src/outbound/pg_import_repo.rs
@@ -15,6 +15,9 @@ use std::collections::HashSet;
use std::str::FromStr;
use uuid::Uuid;
+/// Canonical target transaction helpers for composition roots.
+pub mod targets;
+
#[cfg(test)]
mod test;
diff --git a/crates/import/src/outbound/pg_import_repo/targets.rs b/crates/import/src/outbound/pg_import_repo/targets.rs
new file mode 100644
index 0000000000..16483affdc
--- /dev/null
+++ b/crates/import/src/outbound/pg_import_repo/targets.rs
@@ -0,0 +1,381 @@
+//! Short-lived transactions for canonical Slack provenance. Never hold a
+//! connection while the caller creates a channel or imports history.
+
+use super::PgImportRepo;
+use crate::domain::models::{
+ ImportSourceBinding, ImportTargetKey, ImportTargetKind, ImportTargetReservation,
+ SlackWorkspaceId,
+};
+use crate::domain::ports::{CanonicalImportRepo, ImportError, Result};
+use chrono::{DateTime, Utc};
+use macro_user_id::user_id::MacroUserIdStr;
+use sqlx::{Postgres, Transaction};
+use uuid::Uuid;
+
+struct BindingRow {
+ slack_workspace_id: Option<String>,
+ confirmed_unknown_at: Option<DateTime<Utc>>,
+}
+
+impl TryFrom<BindingRow> for ImportSourceBinding {
+ type Error = ImportError;
+
+ fn try_from(row: BindingRow) -> Result<Self> {
+ let workspace_id = row
+ .slack_workspace_id
+ .map(|id| SlackWorkspaceId::new(&id).ok_or(ImportError::SourceMismatch))
+ .transpose()?;
+ Ok(Self {
+ workspace_id,
+ confirmed_unknown_at: row.confirmed_unknown_at,
+ })
+ }
+}
+
+impl crate::domain::ports::ImportTargetReader for PgImportRepo {
+ async fn lookup_targets(
+ &self,
+ team: Uuid,
+ binding: &ImportSourceBinding,
+ channels: &[crate::domain::models::SlackConversationId],
+ ) -> Result<Vec<crate::domain::models::ImportTargetLookup>> {
+ use crate::domain::models::ImportTargetLookup;
+ if channels.len() > crate::domain::models::MAX_TARGET_LOOKUP {
+ return Err(ImportError::TargetConflict);
+ }
+ let ids: Vec<_> = channels.iter().map(|id| id.as_str()).collect();
+ let workspace = binding.workspace_id.as_ref().map(SlackWorkspaceId::as_str);
+ let rows = sqlx::query!(
+ r#"SELECT s.ordinality AS "position!", r.state AS "state?", c.id AS "id?", c.name AS "name?",
+ c.channel_type::text AS "kind?"
+ FROM unnest($1::text[]) WITH ORDINALITY AS s(id, ordinality)
+ JOIN import_source_binding b ON b.team_id = $2
+ AND b.slack_workspace_id IS NOT DISTINCT FROM $3
+ AND b.confirmed_unknown_at IS NOT DISTINCT FROM $4
+ LEFT JOIN import_target_reservation r
+ ON r.team_id = $2 AND r.source = 'slack' AND r.foreign_id = s.id
+ LEFT JOIN LATERAL (
+ SELECT count(*) AS count, min(entity_id) AS id,
+ bool_and(entity_type = 'channel' AND entity_id IS NOT NULL) AS valid
+ FROM (SELECT DISTINCT entity_id, entity_type FROM import_entity
+ WHERE team_id = $2 AND source = 'slack' AND foreign_id = s.id
+ AND status = 'imported' LIMIT 2) mappings
+ ) legacy ON true
+ LEFT JOIN comms_channels c ON c.id =
+ CASE WHEN r.state = 'ready' AND r.channel_id = r.candidate_channel_id
+ AND (legacy.count = 0 OR (legacy.count = 1 AND legacy.valid
+ AND legacy.id = r.channel_id::text)) THEN r.channel_id
+ WHEN r.state IS NULL AND legacy.count = 1 AND legacy.valid
+ AND legacy.id ~ '^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$'
+ THEN legacy.id::uuid END
+ AND ((c.channel_type = 'team' AND c.team_id = $2)
+ OR (r.state = 'ready' AND c.channel_type IN ('private', 'direct_message')
+ AND c.team_id IS NULL))
+ AND NOT EXISTS (SELECT 1 FROM import_target_reservation other
+ WHERE other.channel_id = c.id AND other.team_id <> $2)
+ AND NOT EXISTS (SELECT 1 FROM import_entity other
+ WHERE other.source = 'slack' AND other.status = 'imported'
+ AND other.entity_type = 'channel' AND other.entity_id = c.id::text
+ AND other.team_id <> $2)
+ ORDER BY s.ordinality"#,
+ &ids as _, team, workspace, binding.confirmed_unknown_at,
+ ).fetch_all(&self.pool).await?;
+ let mut result = vec![ImportTargetLookup::Missing; channels.len()];
+ for row in rows {
+ let outcome = match (row.state.as_deref(), row.id, row.kind.as_deref()) {
+ (Some("pending"), _, _) => ImportTargetLookup::Pending,
+ (_, Some(channel_id), Some(kind)) => {
+ let kind = match kind {
+ "team" => ImportTargetKind::Team,
+ "private" => ImportTargetKind::Private,
+ "direct_message" => ImportTargetKind::DirectMessage,
+ _ => continue,
+ };
+ ImportTargetLookup::Ready {
+ channel_id,
+ kind,
+ name: row.name.unwrap_or_default(),
+ }
+ }
+ _ => ImportTargetLookup::Missing,
+ };
+ result[(row.position - 1) as usize] = outcome;
+ }
+ Ok(result)
+ }
+}
+
+struct ReservationRow {
+ candidate_channel_id: Uuid,
+ state: String,
+}
+
+impl CanonicalImportRepo for PgImportRepo {
+ async fn source_binding(&self, team_id: Uuid) -> Result<Option<ImportSourceBinding>> {
+ sqlx::query_as!(
+ BindingRow,
+ "SELECT slack_workspace_id, confirmed_unknown_at FROM import_source_binding WHERE team_id = $1",
+ team_id,
+ )
+ .fetch_optional(&self.pool)
+ .await?
+ .map(ImportSourceBinding::try_from)
+ .transpose()
+ }
+
+ async fn bind_source(
+ &self,
+ team_id: Uuid,
+ workspace_id: Option<&SlackWorkspaceId>,
+ confirmed_unknown: bool,
+ ) -> Result<ImportSourceBinding> {
+ crate::source_binding::bind_source(&self.pool, team_id, workspace_id, confirmed_unknown)
+ .await
+ }
+
+ async fn reserve_target(
+ &self,
+ user: &MacroUserIdStr<'static>,
+ key: &ImportTargetKey,
+ kind: ImportTargetKind,
+ existing_channel_id: Option<Uuid>,
+ ) -> Result<ImportTargetReservation> {
+ let mut tx = self.pool.begin().await?;
+ lock_key(&mut tx, key).await?;
+ let reserved = reservation(&mut tx, key).await?;
+ if reserved.as_ref().is_some_and(|row| row.state == "conflict") {
+ return Err(ImportError::TargetConflict);
+ }
+
+ // DISTINCT + LIMIT bounds results while still detecting ambiguity. The
+ // two branches use the ledger's own-row unique key and team lookup index.
+ // Never infer the historical team from current team_user membership.
+ let mappings = sqlx::query!(
+ r#"
+ SELECT DISTINCT entity_id, entity_type,
+ (team_id IS NULL OR team_id = $3) AS "compatible!"
+ FROM import_entity
+ WHERE source = 'slack' AND foreign_id = $1 AND status = 'imported'
+ AND (user_id = $2 OR team_id = $3)
+ LIMIT 2
+ "#,
+ key.foreign_id.as_str(),
+ user.as_ref(),
+ key.team_id,
+ )
+ .fetch_all(&mut *tx)
+ .await?;
+ let mut candidate = existing_channel_id;
+ for mapping in mappings {
+ if mapping.entity_type.as_deref() != Some("channel") || !mapping.compatible {
+ return Err(ImportError::TargetConflict);
+ }
+ let id = mapping
+ .entity_id
+ .as_deref()
+ .and_then(|id| Uuid::parse_str(id).ok())
+ .ok_or(ImportError::TargetConflict)?;
+ if candidate.is_some_and(|candidate| candidate != id) {
+ return Err(ImportError::TargetConflict);
+ }
+ candidate = Some(id);
+ }
+
+ let (channel_id, ready) = match reserved {
+ Some(row) => {
+ if candidate.is_some_and(|id| id != row.candidate_channel_id) {
+ return Err(ImportError::TargetConflict);
+ }
+ let ready = row.state == "ready";
+ if ready || candidate.is_some() {
+ validate_channel(&mut tx, key, row.candidate_channel_id, kind).await?;
+ }
+ (row.candidate_channel_id, ready)
+ }
+ None => {
+ if let Some(id) = candidate {
+ // A legacy private mapping is not an access grant. Only an
+ // explicitly authorized claim may establish its provenance.
+ if kind != ImportTargetKind::Team && existing_channel_id != Some(id) {
+ return Err(ImportError::TargetConflict);
+ }
+ validate_channel(&mut tx, key, id, kind).await?;
+ }
+ let channel_id = candidate.unwrap_or_else(Uuid::now_v7);
+ let ready = candidate.is_some();
+ let inserted = sqlx::query_scalar!(
+ r#"
+ INSERT INTO import_target_reservation
+ (team_id, source, foreign_id, candidate_channel_id, channel_id, state)
+ VALUES ($1, 'slack', $2, $3, $4, $5)
+ ON CONFLICT (team_id, source, foreign_id) DO NOTHING
+ RETURNING candidate_channel_id
+ "#,
+ key.team_id,
+ key.foreign_id.as_str(),
+ channel_id,
+ ready.then_some(channel_id),
+ if ready { "ready" } else { "pending" },
+ )
+ .fetch_optional(&mut *tx)
+ .await?;
+ if inserted != Some(channel_id) {
+ return Err(ImportError::TargetConflict);
+ }
+ (channel_id, ready)
+ }
+ };
+ tx.commit().await?;
+ Ok(ImportTargetReservation {
+ key: key.clone(),
+ channel_id,
+ ready,
+ })
+ }
+
+ async fn complete_target(
+ &self,
+ key: &ImportTargetKey,
+ channel_id: Uuid,
+ kind: ImportTargetKind,
+ ) -> Result<ImportTargetReservation> {
+ let mut tx = self.pool.begin().await?;
+ let result = complete_target_in(&mut tx, key, channel_id, kind).await?;
+ tx.commit().await?;
+ Ok(result)
+ }
+}
+
+/// Complete a reserved target alongside channel creation in a caller-owned transaction.
+pub async fn complete_target_in(
+ tx: &mut Transaction<'_, Postgres>,
+ key: &ImportTargetKey,
+ channel_id: Uuid,
+ kind: ImportTargetKind,
+) -> Result<ImportTargetReservation> {
+ lock_key(tx, key).await?;
+ let row = reservation(tx, key)
+ .await?
+ .ok_or(ImportError::TargetNotReserved)?;
+ if row.candidate_channel_id != channel_id || row.state == "conflict" {
+ return Err(ImportError::TargetConflict);
+ }
+ validate_channel(tx, key, channel_id, kind).await?;
+ sqlx::query!(
+ r#"
+ UPDATE import_target_reservation
+ SET state = 'ready', channel_id = $3, updated_at = now()
+ WHERE team_id = $1 AND source = 'slack' AND foreign_id = $2 AND state = 'pending'
+ "#,
+ key.team_id,
+ key.foreign_id.as_str(),
+ channel_id,
+ )
+ .execute(&mut **tx)
+ .await?;
+ Ok(ImportTargetReservation {
+ key: key.clone(),
+ channel_id,
+ ready: true,
+ })
+}
+
+/// Lock the canonical source key before channel or message operations. Returns
+/// validated same-team provenance only for a ready, compatible reservation.
+/// A missing reservation is allowed for read-only target discovery.
+pub async fn lock_target_in(
+ tx: &mut Transaction<'_, Postgres>,
+ key: &ImportTargetKey,
+ channel_id: Uuid,
+ kind: ImportTargetKind,
+ required: bool,
+) -> Result<bool> {
+ lock_key(tx, key).await?;
+ let Some(row) = reservation(tx, key).await? else {
+ if required {
+ return Err(ImportError::TargetNotReserved);
+ }
+ return Ok(false);
+ };
+ if row.candidate_channel_id != channel_id || row.state == "conflict" {
+ return Err(ImportError::TargetConflict);
+ }
+ if row.state == "ready" {
+ validate_channel(tx, key, channel_id, kind).await?;
+ return Ok(true);
+ }
+ Ok(false)
+}
+
+async fn lock_key(tx: &mut Transaction<'_, Postgres>, key: &ImportTargetKey) -> Result<()> {
+ // A transaction-only lock also covers the absent-row case. Hash collisions
+ // merely serialize unrelated reservations; uniqueness comes from the PK.
+ sqlx::query!(
+ "SELECT pg_advisory_xact_lock(hashtextextended($1, 0))",
+ format!("import:{}:slack:{}", key.team_id, key.foreign_id.as_str()),
+ )
+ .execute(&mut **tx)
+ .await?;
+ Ok(())
+}
+
+async fn reservation(
+ tx: &mut Transaction<'_, Postgres>,
+ key: &ImportTargetKey,
+) -> Result<Option<ReservationRow>> {
+ Ok(sqlx::query_as!(
+ ReservationRow,
+ r#"
+ SELECT candidate_channel_id, state FROM import_target_reservation
+ WHERE team_id = $1 AND source = 'slack' AND foreign_id = $2
+ FOR UPDATE
+ "#,
+ key.team_id,
+ key.foreign_id.as_str(),
+ )
+ .fetch_optional(&mut **tx)
+ .await?)
+}
+
+async fn validate_channel(
+ tx: &mut Transaction<'_, Postgres>,
+ key: &ImportTargetKey,
+ channel_id: Uuid,
+ kind: ImportTargetKind,
+) -> Result<()> {
+ // Channel row locking serializes claims of the same existing private/DM
+ // across different team namespaces, as well as deletion/type changes.
+ let channel = sqlx::query!(
+ "SELECT team_id, channel_type::text AS \"kind!\" FROM comms_channels WHERE id = $1 FOR UPDATE",
+ channel_id,
+ )
+ .fetch_optional(&mut **tx)
+ .await?
+ .ok_or(ImportError::TargetConflict)?;
+ if channel.kind != kind.as_ref()
+ || (kind == ImportTargetKind::Team && channel.team_id != Some(key.team_id))
+ || (kind != ImportTargetKind::Team && channel.team_id.is_some())
+ {
+ return Err(ImportError::TargetConflict);
+ }
+ let other_team = sqlx::query_scalar!(
+ r#"
+ SELECT EXISTS (
+ SELECT 1 FROM import_target_reservation
+ WHERE channel_id = $1 AND team_id <> $2
+ ) OR EXISTS (
+ SELECT 1 FROM import_entity
+ WHERE source = 'slack' AND status = 'imported' AND entity_type = 'channel'
+ AND entity_id = $1::text AND team_id <> $2
+ ) AS "exists!"
+ "#,
+ channel_id,
+ key.team_id,
+ )
+ .fetch_one(&mut **tx)
+ .await?;
+ if other_team {
+ return Err(ImportError::TargetConflict);
+ }
+ Ok(())
+}
diff --git a/crates/import/src/outbound/pg_import_repo/test.rs b/crates/import/src/outbound/pg_import_repo/test.rs
index 8167182ecd..a0548ea944 100644
--- a/crates/import/src/outbound/pg_import_repo/test.rs
+++ b/crates/import/src/outbound/pg_import_repo/test.rs
@@ -1,7 +1,94 @@
use super::*;
+use crate::domain::models::{
+ ImportTargetKey, ImportTargetKind, SlackConversationId, SlackWorkspaceId,
+};
+use crate::domain::ports::CanonicalImportRepo;
use macro_db_migrator::MACRO_DB_MIGRATIONS;
+use macro_user_id::cowlike::CowLike;
use sqlx::{Pool, Postgres};
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn read_only_targets_are_exact_scoped_and_never_reserve(pool: PgPool) {
+ use crate::domain::{models::ImportTargetLookup, ports::ImportTargetReader};
+ let admin = ledger_actor(&pool, "reader").await;
+ let peer = ledger_actor(&pool, "other-reader").await;
+ let team = ledger_team(&pool, &admin).await;
+ let other_team = ledger_team(&pool, &peer).await;
+ let repo = PgImportRepo::new(pool.clone());
+ let binding = repo
+ .bind_source(team, Some(&SlackWorkspaceId::new("T1").unwrap()), false)
+ .await
+ .unwrap();
+ let other_binding = repo
+ .bind_source(
+ other_team,
+ Some(&SlackWorkspaceId::new("T2").unwrap()),
+ false,
+ )
+ .await
+ .unwrap();
+ let channel = Uuid::now_v7();
+ let other_channel = Uuid::now_v7();
+ ledger_channel(&pool, channel, Some(team), ImportTargetKind::Team).await;
+ ledger_channel(
+ &pool,
+ other_channel,
+ Some(other_team),
+ ImportTargetKind::Team,
+ )
+ .await;
+ legacy_mapping(&repo, &admin, &target_key(team, "C1"), channel).await;
+ legacy_mapping(&repo, &peer, &target_key(other_team, "C1"), other_channel).await;
+ repo.reserve_target(
+ &admin,
+ &target_key(team, "C2"),
+ ImportTargetKind::Team,
+ None,
+ )
+ .await
+ .unwrap();
+ let ids: Vec<_> = ["C1", "C2", "C3"]
+ .map(|id| SlackConversationId::new(id).unwrap())
+ .into();
+ let before = sqlx::query_scalar!("SELECT count(*) FROM import_target_reservation")
+ .fetch_one(&pool)
+ .await
+ .unwrap();
+ let rows = repo.lookup_targets(team, &binding, &ids).await.unwrap();
+ assert!(
+ matches!(&rows[0], ImportTargetLookup::Ready { channel_id, .. } if *channel_id == channel)
+ );
+ assert_eq!(
+ rows[1..],
+ [ImportTargetLookup::Pending, ImportTargetLookup::Missing]
+ );
+ assert!(
+ matches!(&repo.lookup_targets(other_team, &other_binding, &ids).await.unwrap()[0], ImportTargetLookup::Ready { channel_id, .. } if *channel_id == other_channel)
+ );
+ assert_eq!(
+ repo.lookup_targets(team, &other_binding, &ids)
+ .await
+ .unwrap(),
+ vec![ImportTargetLookup::Missing; 3]
+ );
+ assert_eq!(
+ before,
+ sqlx::query_scalar!("SELECT count(*) FROM import_target_reservation")
+ .fetch_one(&pool)
+ .await
+ .unwrap()
+ );
+ // Same-name or ambiguous legacy rows cannot select an arbitrary channel.
+ let conflict = Uuid::now_v7();
+ ledger_channel(&pool, conflict, Some(team), ImportTargetKind::Team).await;
+ let third = ledger_actor(&pool, "conflicting-reader").await;
+ legacy_mapping(&repo, &third, &target_key(team, "C1"), conflict).await;
+ assert_eq!(
+ repo.lookup_targets(team, &binding, &ids).await.unwrap()[0],
+ ImportTargetLookup::Missing
+ );
+}
+
fn user() -> MacroUserIdStr<'static> {
MacroUserIdStr::parse_from_str("macro|[email protected]").expect("valid user id")
}
@@ -205,3 +292,502 @@ async fn interrupted_auto_import_run_is_reconciled(pool: Pool<Postgres>) {
Some("one or more automatic imports failed")
);
}
+
+async fn ledger_actor(pool: &PgPool, label: &str) -> MacroUserIdStr<'static> {
+ let id = Uuid::now_v7();
+ let email = format!("{label}@example.com");
+ let user_id = format!("macro|{email}");
+ sqlx::query!(
+ "INSERT INTO macro_user (id, username, email, stripe_customer_id) VALUES ($1, $2, $3, $2)",
+ id,
+ label,
+ email,
+ )
+ .execute(pool)
+ .await
+ .unwrap();
+ sqlx::query!(
+ "INSERT INTO \"User\" (id, email, macro_user_id) VALUES ($1, $2, $3)",
+ user_id,
+ email,
+ id,
+ )
+ .execute(pool)
+ .await
+ .unwrap();
+ MacroUserIdStr::parse_from_str(&user_id)
+ .unwrap()
+ .into_owned()
+}
+
+async fn ledger_team(pool: &PgPool, owner: &MacroUserIdStr<'static>) -> Uuid {
+ let id = Uuid::now_v7();
+ sqlx::query!(
+ "INSERT INTO team (id, name, owner_id) VALUES ($1, 'Ledger test', $2)",
+ id,
+ owner.as_ref(),
+ )
+ .execute(pool)
+ .await
+ .unwrap();
+ join_ledger_team(pool, owner, id).await;
+ id
+}
+
+async fn join_ledger_team(pool: &PgPool, user: &MacroUserIdStr<'static>, team: Uuid) {
+ sqlx::query!(
+ r#"INSERT INTO team_user (user_id, team_id, team_role) VALUES ($1, $2, 'admin')
+ ON CONFLICT (user_id) DO UPDATE SET team_id = EXCLUDED.team_id"#,
+ user.as_ref(),
+ team,
+ )
+ .execute(pool)
+ .await
+ .unwrap();
+}
+
+fn target_key(team_id: Uuid, foreign_id: &str) -> ImportTargetKey {
+ ImportTargetKey {
+ team_id,
+ foreign_id: SlackConversationId::new(foreign_id).unwrap(),
+ }
+}
+
+async fn ledger_channel(pool: &PgPool, id: Uuid, team: Option<Uuid>, kind: ImportTargetKind) {
+ sqlx::query!(
+ r#"INSERT INTO comms_channels (id, name, channel_type, team_id, owner_id)
+ VALUES ($1, CASE WHEN $2 = 'direct_message' THEN NULL ELSE 'Historical' END,
+ $2::comms_channel_type, $3, 'macro|[email protected]')"#,
+ id,
+ kind.as_ref() as &str,
+ team,
+ )
+ .execute(pool)
+ .await
+ .unwrap();
+}
+
+async fn legacy_mapping(
+ repo: &PgImportRepo,
+ user: &MacroUserIdStr<'static>,
+ key: &ImportTargetKey,
+ channel_id: Uuid,
+) {
+ repo.upsert_imported(
+ user,
+ ImportSource::Slack,
+ Initiator::Onboarding,
+ key.foreign_id.as_str(),
+ &serde_json::json!({"name": "Historical"}),
+ &channel_id.to_string(),
+ "channel",
+ Some(key.team_id),
+ )
+ .await
+ .unwrap();
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn two_admins_reserve_one_recoverable_uuid(pool: PgPool) {
+ let admin = ledger_actor(&pool, "admin").await;
+ let peer = ledger_actor(&pool, "peer").await;
+ let team = ledger_team(&pool, &admin).await;
+ join_ledger_team(&pool, &peer, team).await;
+ let repo = PgImportRepo::new(pool.clone());
+ let key = target_key(team, "C123");
+ let (a, b) = tokio::join!(
+ repo.reserve_target(&admin, &key, ImportTargetKind::Team, None),
+ repo.reserve_target(&peer, &key, ImportTargetKind::Team, None),
+ );
+ let reserved = a.unwrap();
+ assert_eq!(reserved, b.unwrap());
+ assert!(!reserved.ready);
+ assert_eq!(reserved.channel_id.get_version_num(), 7);
+ assert_eq!(
+ PgImportRepo::new(pool.clone())
+ .reserve_target(&admin, &key, ImportTargetKind::Team, None)
+ .await
+ .unwrap(),
+ reserved,
+ "a new repository instance recovers the committed pending UUID"
+ );
+ assert!(matches!(
+ repo.complete_target(&key, reserved.channel_id, ImportTargetKind::Team)
+ .await,
+ Err(ImportError::TargetConflict)
+ ));
+ ledger_channel(
+ &pool,
+ reserved.channel_id,
+ Some(team),
+ ImportTargetKind::Team,
+ )
+ .await;
+ // Simulate a crash after creation but before completion.
+ assert_eq!(
+ repo.reserve_target(&peer, &key, ImportTargetKind::Team, None)
+ .await
+ .unwrap(),
+ reserved
+ );
+ assert!(matches!(
+ repo.complete_target(&key, Uuid::now_v7(), ImportTargetKind::Team)
+ .await,
+ Err(ImportError::TargetConflict)
+ ));
+ let (a, b) = tokio::join!(
+ repo.complete_target(&key, reserved.channel_id, ImportTargetKind::Team),
+ repo.complete_target(&key, reserved.channel_id, ImportTargetKind::Team),
+ );
+ assert_eq!(a.as_ref().unwrap(), b.as_ref().unwrap());
+ assert!(a.unwrap().ready);
+ assert!(
+ repo.reserve_target(&peer, &key, ImportTargetKind::Team, None)
+ .await
+ .unwrap()
+ .ready
+ );
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn reservations_find_own_and_explicit_team_mappings(pool: PgPool) {
+ let admin = ledger_actor(&pool, "admin").await;
+ let peer = ledger_actor(&pool, "peer").await;
+ let team = ledger_team(&pool, &admin).await;
+ join_ledger_team(&pool, &peer, team).await;
+ let repo = PgImportRepo::new(pool.clone());
+ let key = target_key(team, "COWN");
+ let channel = Uuid::now_v7();
+ ledger_channel(&pool, channel, Some(team), ImportTargetKind::Team).await;
+ legacy_mapping(&repo, &admin, &key, channel).await;
+ assert!(
+ repo.find_team_imported(&admin, ImportSource::Slack, "COWN")
+ .await
+ .unwrap()
+ .is_none()
+ );
+ let own = repo
+ .reserve_target(&admin, &key, ImportTargetKind::Team, None)
+ .await
+ .unwrap();
+ assert_eq!(own.channel_id, channel);
+ assert!(own.ready);
+
+ let other = target_key(team, "CPEER");
+ legacy_mapping(&repo, &peer, &other, channel).await;
+ let new_owner = ledger_actor(&pool, "new-owner").await;
+ let new_team = ledger_team(&pool, &new_owner).await;
+ join_ledger_team(&pool, &admin, new_team).await;
+ assert!(
+ repo.find_team_imported(&admin, ImportSource::Slack, "CPEER")
+ .await
+ .unwrap()
+ .is_none()
+ );
+ // The explicit scope, not the admin's newly joined team, controls lookup.
+ assert_eq!(
+ repo.reserve_target(&admin, &other, ImportTargetKind::Team, None)
+ .await
+ .unwrap()
+ .channel_id,
+ channel
+ );
+ assert!(matches!(
+ repo.reserve_target(
+ &admin,
+ &target_key(new_team, "COWN"),
+ ImportTargetKind::Team,
+ None
+ )
+ .await,
+ Err(ImportError::TargetConflict)
+ ));
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn conflicting_historical_rows_fail_closed(pool: PgPool) {
+ let admin = ledger_actor(&pool, "admin").await;
+ let peer = ledger_actor(&pool, "peer").await;
+ let third = ledger_actor(&pool, "third").await;
+ let team = ledger_team(&pool, &admin).await;
+ let repo = PgImportRepo::new(pool.clone());
+ let key = target_key(team, "CCONFLICT");
+ let first = Uuid::now_v7();
+ let second = Uuid::now_v7();
+ ledger_channel(&pool, first, Some(team), ImportTargetKind::Team).await;
+ ledger_channel(&pool, second, Some(team), ImportTargetKind::Team).await;
+ legacy_mapping(&repo, &admin, &key, first).await;
+ legacy_mapping(&repo, &peer, &key, first).await;
+ // Repeated compatible mappings must not hide a third conflicting mapping.
+ legacy_mapping(&repo, &third, &key, second).await;
+ assert!(matches!(
+ repo.reserve_target(&admin, &key, ImportTargetKind::Team, None)
+ .await,
+ Err(ImportError::TargetConflict)
+ ));
+ assert!(matches!(
+ repo.complete_target(&key, first, ImportTargetKind::Team)
+ .await,
+ Err(ImportError::TargetNotReserved)
+ ));
+
+ let non_channel = target_key(team, "CNOTCHANNEL");
+ repo.upsert_imported(
+ &admin,
+ ImportSource::Slack,
+ Initiator::Chat,
+ non_channel.foreign_id.as_str(),
+ &serde_json::json!({}),
+ &first.to_string(),
+ "task",
+ Some(team),
+ )
+ .await
+ .unwrap();
+ assert!(matches!(
+ repo.reserve_target(&admin, &non_channel, ImportTargetKind::Team, None)
+ .await,
+ Err(ImportError::TargetConflict)
+ ));
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn names_cannot_claim_slack_ids_and_pending_ids_cannot_be_replaced(pool: PgPool) {
+ let admin = ledger_actor(&pool, "admin").await;
+ let team = ledger_team(&pool, &admin).await;
+ let repo = PgImportRepo::new(pool.clone());
+ let channel = Uuid::now_v7();
+ ledger_channel(&pool, channel, Some(team), ImportTargetKind::Team).await;
+ repo.upsert_imported(
+ &admin,
+ ImportSource::Slack,
+ Initiator::Onboarding,
+ "#C123",
+ &serde_json::json!({"name": "C123", "channel_id": "C123"}),
+ &channel.to_string(),
+ "channel",
+ Some(team),
+ )
+ .await
+ .unwrap();
+ let key = target_key(team, "C123");
+ let reserved = repo
+ .reserve_target(&admin, &key, ImportTargetKind::Team, None)
+ .await
+ .unwrap();
+ assert_ne!(reserved.channel_id, channel);
+ assert!(!reserved.ready);
+ assert!(matches!(
+ repo.reserve_target(&admin, &key, ImportTargetKind::Team, Some(channel))
+ .await,
+ Err(ImportError::TargetConflict)
+ ));
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn private_and_dm_provenance_stays_out_of_legacy_listing(pool: PgPool) {
+ let admin = ledger_actor(&pool, "admin").await;
+ let peer = ledger_actor(&pool, "peer").await;
+ let team = ledger_team(&pool, &admin).await;
+ join_ledger_team(&pool, &peer, team).await;
+ let repo = PgImportRepo::new(pool.clone());
+ for (source, kind) in [
+ ("GPRIVATE", ImportTargetKind::Private),
+ ("DDM", ImportTargetKind::DirectMessage),
+ ] {
+ let key = target_key(team, source);
+ let reserved = repo.reserve_target(&admin, &key, kind, None).await.unwrap();
+ ledger_channel(&pool, reserved.channel_id, None, kind).await;
+ repo.complete_target(&key, reserved.channel_id, kind)
+ .await
+ .unwrap();
+ assert!(
+ repo.reserve_target(&peer, &key, kind, None)
+ .await
+ .unwrap()
+ .ready
+ );
+ assert!(
+ repo.find_team_imported(&peer, ImportSource::Slack, source)
+ .await
+ .unwrap()
+ .is_none()
+ );
+ assert!(repo.list(&peer, None, None).await.unwrap().is_empty());
+ assert!(repo.list(&admin, None, None).await.unwrap().is_empty());
+ assert!(matches!(
+ repo.reserve_target(&admin, &key, ImportTargetKind::Team, None)
+ .await,
+ Err(ImportError::TargetConflict)
+ ));
+ }
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn authorized_existing_claims_cannot_cross_teams(pool: PgPool) {
+ let admin = ledger_actor(&pool, "admin").await;
+ let peer = ledger_actor(&pool, "peer").await;
+ let team = ledger_team(&pool, &admin).await;
+ let other_team = ledger_team(&pool, &peer).await;
+ let repo = PgImportRepo::new(pool.clone());
+ let channel = Uuid::now_v7();
+ ledger_channel(&pool, channel, None, ImportTargetKind::DirectMessage).await;
+ let a_key = target_key(team, "D123");
+ let b_key = target_key(other_team, "D123");
+ let (a, b) = tokio::join!(
+ repo.reserve_target(
+ &admin,
+ &a_key,
+ ImportTargetKind::DirectMessage,
+ Some(channel)
+ ),
+ repo.reserve_target(
+ &peer,
+ &b_key,
+ ImportTargetKind::DirectMessage,
+ Some(channel)
+ ),
+ );
+ assert_ne!(
+ a.is_ok(),
+ b.is_ok(),
+ "only one team may establish provenance"
+ );
+ let (winner, loser) = if a.is_ok() { (a, b) } else { (b, a) };
+ assert_eq!(winner.unwrap().channel_id, channel);
+ assert!(matches!(loser, Err(ImportError::TargetConflict)));
+
+ let team_channel = Uuid::now_v7();
+ ledger_channel(
+ &pool,
+ team_channel,
+ Some(other_team),
+ ImportTargetKind::Team,
+ )
+ .await;
+ assert!(matches!(
+ repo.reserve_target(
+ &admin,
+ &target_key(team, "CWRONG"),
+ ImportTargetKind::Team,
+ Some(team_channel)
+ )
+ .await,
+ Err(ImportError::TargetConflict)
+ ));
+
+ let legacy_private = Uuid::now_v7();
+ ledger_channel(&pool, legacy_private, None, ImportTargetKind::Private).await;
+ legacy_mapping(
+ &repo,
+ &peer,
+ &target_key(other_team, "GOLD"),
+ legacy_private,
+ )
+ .await;
+ assert!(matches!(
+ repo.reserve_target(
+ &admin,
+ &target_key(team, "GNEW"),
+ ImportTargetKind::Private,
+ Some(legacy_private)
+ )
+ .await,
+ Err(ImportError::TargetConflict)
+ ));
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn private_legacy_mapping_requires_authorized_claim(pool: PgPool) {
+ let admin = ledger_actor(&pool, "admin").await;
+ let team = ledger_team(&pool, &admin).await;
+ let repo = PgImportRepo::new(pool.clone());
+ let channel = Uuid::now_v7();
+ ledger_channel(&pool, channel, None, ImportTargetKind::Private).await;
+ let key = target_key(team, "G123");
+ repo.upsert_imported(
+ &admin,
+ ImportSource::Slack,
+ Initiator::Chat,
+ "G123",
+ &serde_json::json!({}),
+ &channel.to_string(),
+ "channel",
+ None,
+ )
+ .await
+ .unwrap();
+ assert!(matches!(
+ repo.reserve_target(&admin, &key, ImportTargetKind::Private, None)
+ .await,
+ Err(ImportError::TargetConflict)
+ ));
+ let reserved = repo
+ .reserve_target(&admin, &key, ImportTargetKind::Private, Some(channel))
+ .await
+ .unwrap();
+ assert_eq!(reserved.channel_id, channel);
+ assert!(reserved.ready);
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn source_binding_requires_confirmation_and_rejects_second_workspace(pool: PgPool) {
+ let admin = ledger_actor(&pool, "admin").await;
+ let team = ledger_team(&pool, &admin).await;
+ let repo = PgImportRepo::new(pool);
+ assert!(repo.source_binding(team).await.unwrap().is_none());
+ assert!(matches!(
+ repo.bind_source(team, None, false).await,
+ Err(ImportError::SourceConfirmationRequired)
+ ));
+ let unknown = repo.bind_source(team, None, true).await.unwrap();
+ assert!(unknown.workspace_id.is_none());
+ assert!(unknown.confirmed_unknown_at.is_some());
+ assert_eq!(repo.bind_source(team, None, true).await.unwrap(), unknown);
+ let workspace = SlackWorkspaceId::new("T123").unwrap();
+ let known = repo
+ .bind_source(team, Some(&workspace), false)
+ .await
+ .unwrap();
+ assert_eq!(known.workspace_id.as_ref(), Some(&workspace));
+ assert_eq!(known.confirmed_unknown_at, unknown.confirmed_unknown_at);
+ assert_eq!(
+ repo.bind_source(team, Some(&workspace), false)
+ .await
+ .unwrap(),
+ known
+ );
+ assert!(matches!(
+ repo.bind_source(team, None, false).await,
+ Err(ImportError::SourceConfirmationRequired)
+ ));
+ assert_eq!(repo.bind_source(team, None, true).await.unwrap(), known);
+ assert!(matches!(
+ repo.bind_source(team, Some(&SlackWorkspaceId::new("TSECOND").unwrap()), true)
+ .await,
+ Err(ImportError::SourceMismatch)
+ ));
+ assert_eq!(repo.source_binding(team).await.unwrap().unwrap(), known);
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn concurrent_different_workspace_bindings_have_one_winner(pool: PgPool) {
+ let admin = ledger_actor(&pool, "admin").await;
+ let team = ledger_team(&pool, &admin).await;
+ let repo = PgImportRepo::new(pool);
+ let first = SlackWorkspaceId::new("TFIRST").unwrap();
+ let second = SlackWorkspaceId::new("TSECOND").unwrap();
+ let (a, b) = tokio::join!(
+ repo.bind_source(team, Some(&first), false),
+ repo.bind_source(team, Some(&second), false)
+ );
+ assert_ne!(a.is_ok(), b.is_ok());
+ let (winner, loser) = if a.is_ok() { (a, b) } else { (b, a) };
+ assert!(matches!(loser, Err(ImportError::SourceMismatch)));
+ let known = winner.unwrap();
+ assert_eq!(repo.source_binding(team).await.unwrap().unwrap(), known);
+ assert!(known.confirmed_unknown_at.is_none());
+ let confirmed = repo.bind_source(team, None, true).await.unwrap();
+ assert_eq!(confirmed.workspace_id, known.workspace_id);
+ assert!(confirmed.confirmed_unknown_at.is_some());
+}
diff --git a/crates/import/src/source_binding.rs b/crates/import/src/source_binding.rs
new file mode 100644
index 0000000000..0197aaeb46
--- /dev/null
+++ b/crates/import/src/source_binding.rs
@@ -0,0 +1,46 @@
+//! Transaction-aware source binding for import composition roots.
+
+use crate::domain::models::{ImportSourceBinding, SlackWorkspaceId};
+use crate::domain::ports::{ImportError, Result};
+use sqlx::PgExecutor;
+use uuid::Uuid;
+
+/// Bind the team's single Slack source on the caller's transaction/connection.
+/// Known bindings are immutable; unknown bindings can be refined to a known source.
+/// This helper lets archive job creation and provenance commit or roll back together.
+pub async fn bind_source<'e>(
+ executor: impl PgExecutor<'e>,
+ team_id: Uuid,
+ workspace_id: Option<&SlackWorkspaceId>,
+ confirmed_unknown: bool,
+) -> Result<ImportSourceBinding> {
+ if workspace_id.is_none() && !confirmed_unknown {
+ return Err(ImportError::SourceConfirmationRequired);
+ }
+ let row = sqlx::query!(
+ r#"
+ INSERT INTO import_source_binding (team_id, slack_workspace_id, confirmed_unknown_at)
+ VALUES ($1, $2, CASE WHEN $2::text IS NULL AND $3 THEN now() END)
+ ON CONFLICT (team_id) DO UPDATE
+ SET slack_workspace_id = COALESCE(import_source_binding.slack_workspace_id, EXCLUDED.slack_workspace_id),
+ confirmed_unknown_at = COALESCE(import_source_binding.confirmed_unknown_at, EXCLUDED.confirmed_unknown_at)
+ WHERE import_source_binding.slack_workspace_id IS NULL
+ OR EXCLUDED.slack_workspace_id IS NULL
+ OR import_source_binding.slack_workspace_id = EXCLUDED.slack_workspace_id
+ RETURNING slack_workspace_id, confirmed_unknown_at
+ "#,
+ team_id,
+ workspace_id.map(SlackWorkspaceId::as_str),
+ confirmed_unknown,
+ )
+ .fetch_optional(executor)
+ .await?
+ .ok_or(ImportError::SourceMismatch)?;
+ Ok(ImportSourceBinding {
+ workspace_id: row
+ .slack_workspace_id
+ .map(|id| SlackWorkspaceId::new(&id).ok_or(ImportError::SourceMismatch))
+ .transpose()?,
+ confirmed_unknown_at: row.confirmed_unknown_at,
+ })
+}
diff --git a/crates/macro_db_client/migrations/20260930171728_slack_archive_import.sql b/crates/macro_db_client/migrations/20260930171728_slack_archive_import.sql
new file mode 100644
index 0000000000..c10589e4d8
--- /dev/null
+++ b/crates/macro_db_client/migrations/20260930171728_slack_archive_import.sql
@@ -0,0 +1,343 @@
+-- Import-owned provenance outlives job receipts and staging objects. Team deletion
+-- removes provenance; channel/message deletion removes only its own mappings.
+-- No FK in this migration can delete a user-visible channel or message.
+CREATE TABLE import_source_binding (
+ team_id uuid PRIMARY KEY REFERENCES team (id) ON DELETE CASCADE,
+ slack_workspace_id text CHECK (slack_workspace_id ~ '^T[A-Z0-9]{1,63}$'),
+ confirmed_unknown_at timestamptz,
+ created_at timestamptz NOT NULL DEFAULT now(),
+ CHECK (slack_workspace_id IS NOT NULL OR confirmed_unknown_at IS NOT NULL)
+);
+
+CREATE FUNCTION preserve_import_source_binding() RETURNS trigger
+LANGUAGE plpgsql AS $$
+BEGIN
+ IF NEW.team_id <> OLD.team_id OR
+ (OLD.slack_workspace_id IS NOT NULL AND
+ NEW.slack_workspace_id IS DISTINCT FROM OLD.slack_workspace_id) THEN
+ RAISE EXCEPTION 'import source binding is immutable' USING ERRCODE = 'check_violation';
+ END IF;
+ RETURN NEW;
+END;
+$$;
+CREATE TRIGGER preserve_import_source_binding
+BEFORE UPDATE ON import_source_binding
+FOR EACH ROW EXECUTE FUNCTION preserve_import_source_binding();
+
+-- Shared by onboarding and archive, not attached to an archive job. The candidate
+-- deliberately has no channel FK: allocation must commit BEFORE channel creation.
+-- Deleting a ready target releases its reservation; deleting a job never does.
+CREATE TABLE import_target_reservation (
+ team_id uuid NOT NULL REFERENCES team (id) ON DELETE CASCADE,
+ source text NOT NULL CHECK (source = 'slack'),
+ foreign_id text NOT NULL CHECK (foreign_id ~ '^[CGD][A-Z0-9]{1,63}$'),
+ candidate_channel_id uuid NOT NULL,
+ channel_id uuid REFERENCES comms_channels (id) ON DELETE CASCADE,
+ state text NOT NULL DEFAULT 'pending' CHECK (state IN ('pending', 'ready', 'conflict')),
+ created_at timestamptz NOT NULL DEFAULT now(),
+ updated_at timestamptz NOT NULL DEFAULT now(),
+ PRIMARY KEY (team_id, source, foreign_id),
+ CHECK ((state = 'ready') = (channel_id IS NOT NULL))
+);
+CREATE INDEX import_target_reservation_channel_idx ON import_target_reservation (channel_id)
+ WHERE channel_id IS NOT NULL;
+
+CREATE FUNCTION preserve_import_target_reservation() RETURNS trigger
+LANGUAGE plpgsql AS $$
+BEGIN
+ IF (NEW.team_id, NEW.source, NEW.foreign_id, NEW.candidate_channel_id)
+ IS DISTINCT FROM (OLD.team_id, OLD.source, OLD.foreign_id, OLD.candidate_channel_id)
+ OR (OLD.state = 'ready' AND
+ (NEW.state, NEW.channel_id) IS DISTINCT FROM (OLD.state, OLD.channel_id)) THEN
+ RAISE EXCEPTION 'import target reservation is immutable' USING ERRCODE = 'check_violation';
+ END IF;
+ RETURN NEW;
+END;
+$$;
+CREATE TRIGGER preserve_import_target_reservation
+BEFORE UPDATE ON import_target_reservation
+FOR EACH ROW EXECUTE FUNCTION preserve_import_target_reservation();
+
+CREATE TABLE slack_import_job (
+ id uuid PRIMARY KEY,
+ team_id uuid NOT NULL REFERENCES team (id) ON DELETE CASCADE,
+ -- Historical actor identity survives account deletion; workers reauthorize it.
+ user_id text NOT NULL,
+ idempotency_token uuid NOT NULL,
+ -- Canonical semantic create payload hash, including full selected metadata.
+ request_sha256 text NOT NULL CHECK (request_sha256 ~ '^[0-9a-f]{64}$'),
+ source_workspace_id text CHECK (source_workspace_id ~ '^T[A-Z0-9]{1,63}$'),
+ confirmed_unknown boolean NOT NULL DEFAULT false,
+ include_message_history boolean NOT NULL,
+ status text NOT NULL DEFAULT 'uploading' CHECK (status IN (
+ 'uploading', 'processing', 'completed', 'completed_with_errors',
+ 'failed', 'cancelling', 'cancelled')),
+ revision bigint NOT NULL DEFAULT 0 CHECK (revision >= 0),
+ registered_bytes bigint NOT NULL DEFAULT 0 CHECK (registered_bytes >= 0),
+ created_at timestamptz NOT NULL DEFAULT now(),
+ updated_at timestamptz NOT NULL DEFAULT now(),
+ registration_closed_at timestamptz,
+ finalized_at timestamptz,
+ cancel_requested_at timestamptz,
+ settled_at timestamptz,
+ staging_expires_at timestamptz NOT NULL DEFAULT (now() + interval '14 days'),
+ UNIQUE (team_id, user_id, idempotency_token),
+ CHECK (source_workspace_id IS NOT NULL OR confirmed_unknown),
+ CHECK (finalized_at IS NULL OR registration_closed_at IS NOT NULL),
+ CHECK (cancel_requested_at IS NULL OR registration_closed_at IS NOT NULL),
+ CHECK (status NOT IN ('cancelling', 'cancelled') OR cancel_requested_at IS NOT NULL)
+);
+CREATE INDEX slack_import_job_team_listing_idx ON slack_import_job (team_id, id DESC);
+CREATE INDEX slack_import_job_staging_expiry_idx ON slack_import_job (staging_expires_at)
+ WHERE registration_closed_at IS NULL;
+
+CREATE FUNCTION preserve_slack_import_job_request() RETURNS trigger
+LANGUAGE plpgsql AS $$
+BEGIN
+ IF (NEW.id, NEW.team_id, NEW.user_id, NEW.idempotency_token, NEW.request_sha256,
+ NEW.source_workspace_id, NEW.confirmed_unknown, NEW.include_message_history, NEW.created_at)
+ IS DISTINCT FROM
+ (OLD.id, OLD.team_id, OLD.user_id, OLD.idempotency_token, OLD.request_sha256,
+ OLD.source_workspace_id, OLD.confirmed_unknown, OLD.include_message_history, OLD.created_at)
+ OR (OLD.registration_closed_at IS NOT NULL AND
+ NEW.registration_closed_at IS DISTINCT FROM OLD.registration_closed_at) THEN
+ RAISE EXCEPTION 'import request or registration closure is immutable' USING ERRCODE = 'check_violation';
+ END IF;
+ RETURN NEW;
+END;
+$$;
+CREATE TRIGGER preserve_slack_import_job_request
+BEFORE UPDATE ON slack_import_job
+FOR EACH ROW EXECUTE FUNCTION preserve_slack_import_job_request();
+
+CREATE TABLE slack_import_conversation (
+ job_id uuid NOT NULL REFERENCES slack_import_job (id) ON DELETE CASCADE,
+ slack_channel_id text NOT NULL CHECK (slack_channel_id ~ '^[CGD][A-Z0-9]{1,63}$'),
+ kind text NOT NULL CHECK (kind IN ('public_channel', 'private_channel', 'direct_message', 'group_direct_message')),
+ name text NOT NULL,
+ folder text NOT NULL,
+ member_ids text[] NOT NULL,
+ creator_id text,
+ -- Exact source microseconds, never floating point; absence is meaningful.
+ source_created_at bigint CHECK (source_created_at BETWEEN 0 AND 253402300799999999),
+ resolved_created_at timestamptz,
+ archived boolean NOT NULL,
+ message_count bigint CHECK (message_count >= 0),
+ channel_id uuid REFERENCES comms_channels (id) ON DELETE SET NULL,
+ status text NOT NULL DEFAULT 'awaiting_uploads' CHECK (status IN (
+ 'awaiting_uploads', 'queued', 'importing', 'completed', 'failed', 'skipped')),
+ sealed_at timestamptz,
+ part_count integer CHECK (part_count >= 0),
+ manifest_sha256 text CHECK (manifest_sha256 ~ '^[0-9a-f]{64}$'),
+ processed bigint NOT NULL DEFAULT 0 CHECK (processed >= 0),
+ imported bigint NOT NULL DEFAULT 0 CHECK (imported >= 0),
+ duplicates bigint NOT NULL DEFAULT 0 CHECK (duplicates >= 0),
+ skipped bigint NOT NULL DEFAULT 0 CHECK (skipped >= 0),
+ reactions bigint NOT NULL DEFAULT 0 CHECK (reactions >= 0),
+ checkpoint_part integer NOT NULL DEFAULT 0 CHECK (checkpoint_part >= 0),
+ checkpoint_record integer NOT NULL DEFAULT 0 CHECK (checkpoint_record >= 0),
+ lease_owner uuid,
+ lease_token uuid,
+ lease_generation bigint NOT NULL DEFAULT 0 CHECK (lease_generation >= 0),
+ lease_expires_at timestamptz,
+ heartbeat_at timestamptz,
+ attempts integer NOT NULL DEFAULT 0 CHECK (attempts >= 0),
+ event_generation bigint NOT NULL DEFAULT 0 CHECK (event_generation >= 0),
+ search_state text NOT NULL DEFAULT 'not_needed' CHECK (search_state IN (
+ 'not_needed', 'pending', 'submitted', 'completed', 'failed')),
+ search_dirty_generation bigint NOT NULL DEFAULT 0 CHECK (search_dirty_generation >= 0),
+ search_submitted_generation bigint CHECK (search_submitted_generation >= 0),
+ search_receipt_id uuid,
+ search_updated_at timestamptz NOT NULL DEFAULT now(),
+ search_attempts integer NOT NULL DEFAULT 0 CHECK (search_attempts >= 0),
+ -- Only sanitized domain error/warning codes, never provider diagnostics.
+ last_error text,
+ warnings text[] NOT NULL DEFAULT '{}',
+ created_at timestamptz NOT NULL DEFAULT now(),
+ updated_at timestamptz NOT NULL DEFAULT now(),
+ settled_at timestamptz,
+ PRIMARY KEY (job_id, slack_channel_id),
+ CHECK (processed = imported + duplicates + skipped),
+ CHECK (num_nonnulls(sealed_at, part_count, manifest_sha256) IN (0, 3)),
+ CHECK (num_nonnulls(lease_owner, lease_token, lease_expires_at, heartbeat_at) IN (0, 4)),
+ CHECK ((status = 'importing') = (lease_token IS NOT NULL)),
+ CHECK (status <> 'importing' OR (lease_generation > 0 AND attempts > 0)),
+ CHECK (part_count IS NULL OR checkpoint_part <= part_count),
+ CHECK (part_count IS NULL OR checkpoint_part <> part_count OR checkpoint_record = 0),
+ CHECK (search_submitted_generation <= search_dirty_generation),
+ CHECK (search_state <> 'submitted' OR
+ (search_receipt_id IS NOT NULL AND search_submitted_generation IS NOT NULL))
+);
+CREATE INDEX slack_import_conversation_expired_lease_idx
+ ON slack_import_conversation (lease_expires_at) WHERE status = 'importing';
+CREATE INDEX slack_import_conversation_search_idx
+ ON slack_import_conversation (search_updated_at) WHERE search_state IN ('pending', 'submitted', 'failed');
+CREATE INDEX slack_import_conversation_channel_idx ON slack_import_conversation (channel_id)
+ WHERE channel_id IS NOT NULL;
+
+-- One users object and a natural key for each selected conversation part. NULL
+-- channel/index identify users, not a synthetic Slack ID or negative part index.
+-- S3 lifecycle expires BYTES after 14 days; retain descriptors with the receipt
+-- until job cleanup. No staging cleanup may delete provenance or message maps.
+CREATE TABLE slack_import_upload (
+ job_id uuid NOT NULL REFERENCES slack_import_job (id) ON DELETE CASCADE,
+ slack_channel_id text,
+ part_index integer CHECK (part_index >= 0),
+ object_key text NOT NULL UNIQUE CHECK (length(object_key) BETWEEN 1 AND 1024),
+ sha256 text NOT NULL CHECK (sha256 ~ '^[0-9a-f]{64}$'),
+ byte_length bigint NOT NULL CHECK (byte_length > 0),
+ record_count integer CHECK (record_count > 0),
+ created_at timestamptz NOT NULL DEFAULT now(),
+ verified_at timestamptz,
+ verified_version_id text CHECK (length(verified_version_id) > 0),
+ verified_etag text CHECK (length(verified_etag) > 0),
+ UNIQUE NULLS NOT DISTINCT (job_id, slack_channel_id, part_index),
+ FOREIGN KEY (job_id, slack_channel_id)
+ REFERENCES slack_import_conversation (job_id, slack_channel_id) ON DELETE CASCADE,
+ CHECK (num_nonnulls(slack_channel_id, part_index, record_count) IN (0, 3)),
+ CHECK ((verified_at IS NOT NULL) =
+ (verified_version_id IS NOT NULL OR verified_etag IS NOT NULL))
+);
+
+-- Lock job before conversation in every registration/seal transaction. This
+-- makes a concurrent seal/finalize and registration serialize on the same row.
+CREATE FUNCTION protect_slack_import_upload() RETURNS trigger
+LANGUAGE plpgsql AS $$
+DECLARE
+ job slack_import_job;
+ seal_time timestamptz;
+BEGIN
+ IF TG_OP = 'DELETE' THEN
+ -- Cascading job cleanup is allowed; independent descriptor deletion is not.
+ IF EXISTS (SELECT 1 FROM slack_import_job WHERE id = OLD.job_id) THEN
+ RAISE EXCEPTION 'import descriptors cannot be deleted' USING ERRCODE = 'check_violation';
+ END IF;
+ RETURN OLD;
+ END IF;
+ SELECT * INTO job FROM slack_import_job WHERE id = NEW.job_id FOR UPDATE;
+ IF TG_OP = 'UPDATE' THEN
+ IF (NEW.job_id, NEW.slack_channel_id, NEW.part_index, NEW.object_key,
+ NEW.sha256, NEW.byte_length, NEW.record_count, NEW.created_at)
+ IS DISTINCT FROM
+ (OLD.job_id, OLD.slack_channel_id, OLD.part_index, OLD.object_key,
+ OLD.sha256, OLD.byte_length, OLD.record_count, OLD.created_at)
+ OR (OLD.verified_at IS NOT NULL AND
+ (NEW.verified_at, NEW.verified_version_id, NEW.verified_etag) IS DISTINCT FROM
+ (OLD.verified_at, OLD.verified_version_id, OLD.verified_etag)) THEN
+ RAISE EXCEPTION 'import descriptor or verification is immutable' USING ERRCODE = 'check_violation';
+ END IF;
+ IF NEW IS NOT DISTINCT FROM OLD THEN
+ RETURN NEW;
+ END IF;
+ END IF;
+ IF job.registration_closed_at IS NOT NULL THEN
+ RAISE EXCEPTION 'import registration is closed' USING ERRCODE = 'check_violation';
+ END IF;
+ IF TG_OP = 'INSERT' AND NEW.slack_channel_id IS NOT NULL THEN
+ SELECT sealed_at INTO seal_time FROM slack_import_conversation
+ WHERE job_id = NEW.job_id AND slack_channel_id = NEW.slack_channel_id FOR UPDATE;
+ IF seal_time IS NOT NULL OR NOT job.include_message_history THEN
+ RAISE EXCEPTION 'import manifest is sealed or history disabled' USING ERRCODE = 'check_violation';
+ END IF;
+ END IF;
+ RETURN NEW;
+END;
+$$;
+CREATE TRIGGER protect_slack_import_upload
+BEFORE INSERT OR UPDATE OR DELETE ON slack_import_upload
+FOR EACH ROW EXECUTE FUNCTION protect_slack_import_upload();
+
+CREATE FUNCTION protect_slack_import_conversation() RETURNS trigger
+LANGUAGE plpgsql AS $$
+DECLARE
+ expected_count integer;
+ last_index integer;
+ expected_hash text;
+BEGIN
+ IF TG_OP = 'INSERT' THEN
+ IF num_nonnulls(NEW.sealed_at, NEW.part_count, NEW.manifest_sha256) > 0 THEN
+ RAISE EXCEPTION 'create conversation before sealing its manifest' USING ERRCODE = 'check_violation';
+ END IF;
+ RETURN NEW;
+ END IF;
+ IF (NEW.job_id, NEW.slack_channel_id, NEW.kind, NEW.name, NEW.folder,
+ NEW.member_ids, NEW.creator_id, NEW.source_created_at, NEW.archived, NEW.message_count)
+ IS DISTINCT FROM
+ (OLD.job_id, OLD.slack_channel_id, OLD.kind, OLD.name, OLD.folder,
+ OLD.member_ids, OLD.creator_id, OLD.source_created_at, OLD.archived, OLD.message_count)
+ OR (OLD.sealed_at IS NOT NULL AND
+ (NEW.sealed_at, NEW.part_count, NEW.manifest_sha256) IS DISTINCT FROM
+ (OLD.sealed_at, OLD.part_count, OLD.manifest_sha256))
+ OR (OLD.resolved_created_at IS NOT NULL AND
+ NEW.resolved_created_at IS DISTINCT FROM OLD.resolved_created_at) THEN
+ RAISE EXCEPTION 'import metadata or seal is immutable' USING ERRCODE = 'check_violation';
+ END IF;
+ IF OLD.sealed_at IS NULL AND NEW.sealed_at IS NOT NULL THEN
+ IF EXISTS (SELECT 1 FROM slack_import_job
+ WHERE id = NEW.job_id AND registration_closed_at IS NOT NULL) THEN
+ RAISE EXCEPTION 'import registration is closed' USING ERRCODE = 'check_violation';
+ END IF;
+ SELECT count(*), max(part_index),
+ encode(digest(coalesce(string_agg(
+ part_index::text || ':' || sha256 || ':' || byte_length::text || ':' || record_count::text || E'\n',
+ '' ORDER BY part_index), ''), 'sha256'), 'hex')
+ INTO expected_count, last_index, expected_hash
+ FROM slack_import_upload WHERE job_id = NEW.job_id AND slack_channel_id = NEW.slack_channel_id;
+ IF NEW.part_count IS DISTINCT FROM expected_count
+ OR (expected_count > 0 AND last_index <> expected_count - 1)
+ OR NEW.manifest_sha256 IS DISTINCT FROM expected_hash THEN
+ RAISE EXCEPTION 'import seal does not match descriptors' USING ERRCODE = 'check_violation';
+ END IF;
+ END IF;
+ RETURN NEW;
+END;
+$$;
+CREATE TRIGGER protect_slack_import_conversation
+BEFORE INSERT OR UPDATE ON slack_import_conversation
+FOR EACH ROW EXECUTE FUNCTION protect_slack_import_conversation();
+
+CREATE TABLE slack_import_outbox (
+ job_id uuid NOT NULL,
+ slack_channel_id text NOT NULL,
+ kind text NOT NULL CHECK (kind IN ('import', 'search')),
+ generation bigint NOT NULL CHECK (generation > 0),
+ created_at timestamptz NOT NULL DEFAULT now(),
+ available_at timestamptz NOT NULL DEFAULT now(),
+ published_at timestamptz,
+ cancelled_at timestamptz,
+ attempts integer NOT NULL DEFAULT 0 CHECK (attempts >= 0),
+ last_error text,
+ PRIMARY KEY (job_id, slack_channel_id, kind, generation),
+ FOREIGN KEY (job_id, slack_channel_id)
+ REFERENCES slack_import_conversation (job_id, slack_channel_id) ON DELETE CASCADE,
+ CHECK (num_nonnulls(published_at, cancelled_at) <= 1)
+);
+CREATE INDEX slack_import_outbox_pending_idx ON slack_import_outbox (available_at, created_at)
+ WHERE published_at IS NULL AND cancelled_at IS NULL;
+
+-- No job FK: first-commit-wins dedupe survives every job/staging cleanup. Soft
+-- message deletion retains the mapping; hard deletion deliberately removes it.
+-- The deferred composite FK permits mapping-before-message writes in one batch
+-- and proves that the mapped message actually belongs to the recorded channel.
+CREATE TABLE slack_import_message_map (
+ team_id uuid NOT NULL REFERENCES team (id) ON DELETE CASCADE,
+ slack_channel_id text NOT NULL CHECK (slack_channel_id ~ '^[CGD][A-Z0-9]{1,63}$'),
+ slack_ts bigint NOT NULL CHECK (slack_ts BETWEEN 0 AND 253402300799999999),
+ message_id uuid NOT NULL,
+ channel_id uuid NOT NULL,
+ parent_entity_type text GENERATED ALWAYS AS ('channel'::text) STORED,
+ parent_entity_id text GENERATED ALWAYS AS (channel_id::text) STORED,
+ created_at timestamptz NOT NULL DEFAULT now(),
+ PRIMARY KEY (team_id, slack_channel_id, slack_ts),
+ FOREIGN KEY (message_id, parent_entity_type, parent_entity_id)
+ REFERENCES comms_messages (id, parent_entity_type, parent_entity_id)
+ ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
+);
+CREATE INDEX slack_import_message_map_message_idx ON slack_import_message_map (message_id);
+
+-- Additive compatibility: deployed onboarding/chat writers keep their defaults,
+-- insert shapes, and per-user uniqueness. Do not alter channel team/private rules.
+ALTER TABLE import_entity DROP CONSTRAINT import_entity_initiator_check;
+ALTER TABLE import_entity ADD CONSTRAINT import_entity_initiator_check
+ CHECK (initiator IN ('onboarding', 'chat', 'archive'));
+
diff --git a/crates/macro_db_client/migrations/20261001163358_slack_import_message_references.sql b/crates/macro_db_client/migrations/20261001163358_slack_import_message_references.sql
new file mode 100644
index 0000000000..122ec12226
--- /dev/null
+++ b/crates/macro_db_client/migrations/20261001163358_slack_import_message_references.sql
@@ -0,0 +1,27 @@
+-- Temporary, job-owned reconciliation evidence. Neither messages nor long-lived
+-- source mappings depend on this table; job cleanup only removes these intents.
+-- One bounded template per first-committed message, not one copy per occurrence.
+CREATE TABLE slack_import_message_reference (
+ job_id uuid NOT NULL,
+ slack_channel_id text NOT NULL,
+ message_id uuid NOT NULL,
+ channel_id uuid NOT NULL,
+ importer_version smallint NOT NULL CHECK (importer_version > 0),
+ template jsonb,
+ completed_at timestamptz,
+ PRIMARY KEY (job_id, message_id),
+ FOREIGN KEY (job_id, slack_channel_id)
+ REFERENCES slack_import_conversation (job_id, slack_channel_id) ON DELETE CASCADE,
+ CHECK ((completed_at IS NULL) = (template IS NOT NULL)),
+ CHECK (template IS NULL OR (
+ jsonb_typeof(template) = 'object'
+ AND octet_length(template::text) <= 4194304
+ AND jsonb_typeof(template->'references') = 'array'
+ AND jsonb_array_length(template->'references') BETWEEN 1 AND 256
+ ))
+);
+
+-- Completed rows are small durable checkpoints; large templates are released on
+-- settlement and all checkpoints follow the existing job retention/cleanup policy.
+CREATE INDEX slack_import_message_reference_pending
+ ON slack_import_message_reference (job_id, message_id) WHERE completed_at IS NULL;
diff --git a/crates/macro_queues/src/lib.rs b/crates/macro_queues/src/lib.rs
index a3b799b666..d03bbb8a50 100644
--- a/crates/macro_queues/src/lib.rs
+++ b/crates/macro_queues/src/lib.rs
@@ -450,6 +450,18 @@ queue! {
/// bare queue names emitted by the xtask local environment, while `dev` and
/// `prod` use the `{base}-queue-{stack}` names provisioned by Pulumi.
pub struct Queues {
+ /// Queue for Slack archive conversation imports.
+ pub SlackImportQueue {
+ local: "slack-import-queue",
+ dev: "slack-import-queue-dev",
+ prod: "slack-import-queue-prod",
+ },
+ /// Dead-letter queue provisioned by the shared Pulumi Queue component.
+ pub SlackImportDlq {
+ local: "slack-import-dlq",
+ dev: "slack-import-dlq-dev",
+ prod: "slack-import-dlq-prod",
+ },
/// Queue for document text extraction jobs.
pub DocumentTextExtractorQueue {
local: "document-text-extractor-lambda-queue",
diff --git a/crates/macro_queues/src/test.rs b/crates/macro_queues/src/test.rs
index fbd88eadb8..75d0cdb4b2 100644
--- a/crates/macro_queues/src/test.rs
+++ b/crates/macro_queues/src/test.rs
@@ -11,6 +11,37 @@ crate::queue! {
}
}
+#[test]
+fn slack_import_names_match_pulumi_queue_component() {
+ for (environment, suffix) in [
+ (Environment::Local, ""),
+ (Environment::Develop, "-dev"),
+ (Environment::Production, "-prod"),
+ ] {
+ assert_eq!(
+ SlackImportQueue::default_for_environment(environment).as_str(),
+ format!("slack-import-queue{suffix}")
+ );
+ assert_eq!(
+ SlackImportDlq::default_for_environment(environment).as_str(),
+ format!("slack-import-dlq{suffix}")
+ );
+ }
+ with_mock_override_env(
+ |name| Ok(format!("override-{name}")),
+ || {
+ assert_eq!(
+ SlackImportQueue::new().as_str(),
+ "override-OVERRIDE_SLACK_IMPORT_QUEUE"
+ );
+ assert_eq!(
+ SlackImportDlq::new().as_str(),
+ "override-OVERRIDE_SLACK_IMPORT_DLQ"
+ );
+ },
+ );
+}
+
fn missing_override(_: &'static str) -> Result<String, std::env::VarError> {
Err(std::env::VarError::NotPresent)
}
diff --git a/crates/mention_utils/Cargo.toml b/crates/mention_utils/Cargo.toml
index 7a3953d1af..ccc29ac8df 100644
--- a/crates/mention_utils/Cargo.toml
+++ b/crates/mention_utils/Cargo.toml
@@ -13,6 +13,7 @@ nom = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
thiserror = { workspace = true }
+url = { workspace = true }
workspace-hack = { version = "0.1", path = "../workspace-hack" }
[dev-dependencies]
diff --git a/crates/mention_utils/src/serialize.rs b/crates/mention_utils/src/serialize.rs
index 5539aa7d22..053722c85b 100644
--- a/crates/mention_utils/src/serialize.rs
+++ b/crates/mention_utils/src/serialize.rs
@@ -14,7 +14,7 @@ struct UserMention<'a> {
/// Serialize a Macro user as an in-app user mention node.
pub fn user_mention(user_id: &MacroUserIdStr<'_>) -> Result<String, serde_json::Error> {
- let payload = serde_json::to_string(&UserMention {
+ let payload = tag_json(&UserMention {
user_id: user_id.as_ref(),
email: user_id.email_str(),
})?;
@@ -26,20 +26,114 @@ pub fn user_mention(user_id: &MacroUserIdStr<'_>) -> Result<String, serde_json::
/// field.
pub fn bot_mention(bot_id: BotId, display_name: &str) -> Result<String, serde_json::Error> {
let storage_id = bot_id.into_storage_id();
- let payload = serde_json::to_string(&UserMention {
+ let payload = tag_json(&UserMention {
user_id: storage_id.as_ref(),
email: display_name,
})?;
Ok(format!("<m-user-mention>{payload}</m-user-mention>"))
}
+/// Serialize JSON without literal tag delimiters. JSON decoding restores the
+/// original text; HTML/entity escaping here would change labels.
+fn tag_json(value: &impl Serialize) -> Result<String, serde_json::Error> {
+ Ok(serde_json::to_string(value)?
+ .replace('<', "\\u003c")
+ .replace('>', "\\u003e"))
+}
+
+/// Invalid external link input: scheme policy is separate from URL syntax.
+#[derive(Debug, thiserror::Error)]
+pub enum LinkError {
+ /// Only HTTP, HTTPS and mailto are supported by the frontend.
+ #[error("unsupported URL scheme")]
+ UnsafeScheme,
+ /// The URL is malformed or contains ambiguous literal delimiters/controls.
+ #[error("malformed URL")]
+ MalformedUrl,
+}
+
+/// A validated external URL with an untrusted display label, not markdown.
+#[derive(Serialize)]
+pub struct ExternalLink<'a> {
+ url: &'a str,
+ text: &'a str,
+ title: &'static str,
+}
+
+impl<'a> ExternalLink<'a> {
+ /// Validate independently of serialization, preserving the original URL.
+ pub fn new(url: &'a str, text: &'a str) -> Result<Self, LinkError> {
+ let (scheme, rest) = url.split_once(':').ok_or(LinkError::MalformedUrl)?;
+ if !matches!(
+ scheme.to_ascii_lowercase().as_str(),
+ "http" | "https" | "mailto"
+ ) {
+ return Err(LinkError::UnsafeScheme);
+ }
+ if url.chars().any(|c| {
+ c.is_whitespace() || c.is_control() || matches!(c, '<' | '>' | '\\' | '"' | '`')
+ }) {
+ return Err(LinkError::MalformedUrl);
+ }
+ let parsed = url::Url::parse(url).map_err(|_| LinkError::MalformedUrl)?;
+ if parsed.scheme() == "mailto" {
+ if parsed.path().is_empty() {
+ return Err(LinkError::MalformedUrl);
+ }
+ } else if !rest.starts_with("//") || rest.starts_with("///") || parsed.host_str().is_none()
+ {
+ return Err(LinkError::MalformedUrl);
+ }
+ Ok(Self {
+ url,
+ text,
+ title: "",
+ })
+ }
+
+ /// Emit the native Lexical link representation.
+ pub fn serialize(&self) -> Result<String, serde_json::Error> {
+ Ok(format!("<m-link>{}</m-link>", tag_json(self)?))
+ }
+}
+
+/// Channel navigation parameters use snake_case inside camelCase mention JSON.
+#[derive(Debug, Clone, Copy, Default, Serialize)]
+pub struct ChannelMentionParams<'a> {
+ /// Canonical Macro message UUID, never a Slack timestamp.
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub channel_message_id: Option<&'a str>,
+ /// Canonical persisted Macro root UUID for a thread/reply target.
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub channel_thread_id: Option<&'a str>,
+}
+
+/// Serialize a channel or a message within that channel. `document_id` is always
+/// the channel UUID, including when message navigation parameters are present.
+pub fn channel_mention(
+ document_id: &str,
+ document_name: &str,
+ params: ChannelMentionParams<'_>,
+) -> Result<String, serde_json::Error> {
+ let payload = tag_json(&DocumentMention {
+ document_id,
+ block_name: "channel",
+ document_name,
+ block_params: params,
+ collapsed: false,
+ })?;
+ Ok(format!(
+ "<m-document-mention>{payload}</m-document-mention>"
+ ))
+}
+
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
-struct DocumentMention<'a> {
+struct DocumentMention<'a, P> {
document_id: &'a str,
block_name: &'a str,
document_name: &'a str,
- block_params: serde_json::Map<String, serde_json::Value>,
+ block_params: P,
collapsed: bool,
}
diff --git a/crates/mention_utils/src/serialize/test.rs b/crates/mention_utils/src/serialize/test.rs
index 56fa78fce0..845bb9f777 100644
--- a/crates/mention_utils/src/serialize/test.rs
+++ b/crates/mention_utils/src/serialize/test.rs
@@ -1,6 +1,102 @@
use macro_user_id::user_id::MacroUserIdStr;
use super::*;
+use crate::parse::{ParsedDocumentMention, ParsedLink, ParsedUserMention, XmlTaggedParsed};
+
+#[test]
+fn native_links_and_channels_round_trip_untrusted_labels() {
+ for label in [
+ "quote \" and \\",
+ "line\nnext",
+ "&amp; &lt;",
+ "世界 café",
+ "</m-link><m-user-mention>nested</m-user-mention>",
+ "</m-document-mention>",
+ ] {
+ let link = ExternalLink::new("https://example.com/a(b)?x=1&y=2", label)
+ .unwrap()
+ .serialize()
+ .unwrap();
+ let (rest, parsed) = ParsedLink::parse(&link).unwrap();
+ assert!(rest.is_empty());
+ assert_eq!(parsed.text, label);
+ assert_eq!(parsed.url, "https://example.com/a(b)?x=1&y=2");
+ let channel =
+ channel_mention("channel-id", label, ChannelMentionParams::default()).unwrap();
+ let (rest, parsed) = ParsedDocumentMention::parse(&channel).unwrap();
+ assert!(rest.is_empty());
+ assert_eq!(parsed.document_name, label);
+ let bot = bot_mention(bot_id::MACRO_AI_BOT_ID, label).unwrap();
+ let (rest, parsed) = ParsedUserMention::parse(&bot).unwrap();
+ assert!(rest.is_empty());
+ assert_eq!(parsed.email.as_deref(), Some(label));
+ }
+}
+
+#[test]
+fn link_scheme_policy_is_distinct_from_url_validation() {
+ for url in [
+ "javascript:alert(1)",
+ "data:text/plain,x",
+ "file:///tmp/x",
+ "vbscript:bad",
+ ] {
+ assert!(matches!(
+ ExternalLink::new(url, "label"),
+ Err(LinkError::UnsafeScheme)
+ ));
+ }
+ for url in [
+ "https://",
+ "https:///x",
+ "https://[bad]",
+ "https://a:bad",
+ "https://a\n/x",
+ "https:\\evil",
+ "https://a/<x>",
+ "mailto:",
+ "//example.com",
+ ] {
+ assert!(
+ matches!(
+ ExternalLink::new(url, "label"),
+ Err(LinkError::MalformedUrl)
+ ),
+ "{url}"
+ );
+ }
+ for url in [
+ "https://example.com",
+ "HTTP://example.com",
+ "mailto:[email protected]",
+ "https://example.com/世界",
+ "https://[::1]:8080/a",
+ ] {
+ assert!(ExternalLink::new(url, "label").is_ok(), "{url}");
+ }
+}
+
+#[test]
+fn channel_navigation_matches_lexical_field_names_and_defaults() {
+ assert_eq!(
+ channel_mention("channel", "general", ChannelMentionParams::default()).unwrap(),
+ r#"<m-document-mention>{"documentId":"channel","blockName":"channel","documentName":"general","blockParams":{},"collapsed":false}</m-document-mention>"#
+ );
+ let result = channel_mention(
+ "channel",
+ "general",
+ ChannelMentionParams {
+ channel_message_id: Some("message"),
+ channel_thread_id: Some("root"),
+ },
+ )
+ .unwrap();
+ assert!(
+ result.contains(
+ r#""blockParams":{"channel_message_id":"message","channel_thread_id":"root"}"#
+ )
+ );
+}
#[test]
fn serializes_user_mention() {
diff --git a/crates/messages/src/domain/historical.rs b/crates/messages/src/domain/historical.rs
new file mode 100644
index 0000000000..9f502c8de4
--- /dev/null
+++ b/crates/messages/src/domain/historical.rs
@@ -0,0 +1,202 @@
+//! Bounded, trusted historical channel writes, separate from live message commands.
+
+use std::collections::HashSet;
+
+use channel_sender::ChannelSender;
+use chrono::{DateTime, Utc};
+use macro_user_id::user_id::MacroUserIdStr;
+use serde::Serialize;
+use uuid::Uuid;
+
+use super::ports::MessageError;
+
+/// Maximum messages in one historical transaction batch.
+pub const MAX_HISTORICAL_MESSAGES: usize = 500;
+/// Maximum summed serialized message bytes per batch, including references and metadata.
+pub const MAX_HISTORICAL_BATCH_BYTES: usize = 4 * 1024 * 1024;
+/// Maximum serialized input bytes for one message, including references and metadata.
+pub const MAX_HISTORICAL_MESSAGE_BYTES: usize = 1024 * 1024;
+
+/// Live persisted channel message identity, without body or author disclosure.
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct HistoricalMessageTarget {
+ /// Actual message ID.
+ pub message_id: Uuid,
+ /// Actual owning channel, checked against parent ownership.
+ pub channel_id: Uuid,
+ /// Persisted live root (self for roots and orphan-imported replies).
+ pub root_id: Uuid,
+}
+
+/// Compare-and-set body reconciliation, not a live edit. The importer must still
+/// authorize disclosure and atomically persist its checkpoint/search marker.
+pub struct HistoricalBodyPatch {
+ /// Exact first-committed message and owning channel.
+ pub message_id: Uuid,
+ /// Expected channel ownership.
+ pub channel_id: Uuid,
+ /// Job recorded by the original writer, never a later duplicate import.
+ pub job_id: Uuid,
+ /// Original importer schema/body guard.
+ pub importer_version: i16,
+ /// Original fallback body, compared exactly at update time.
+ pub expected_body: String,
+ /// Authorized rendered replacement.
+ pub body: String,
+}
+
+/// A channel already authorized by the importing domain and its bounded messages.
+/// Source deduplication, fencing, mappings and checkpoints belong to the caller.
+#[derive(Debug, Clone, Serialize)]
+pub struct HistoricalBatch {
+ /// Authorized channel; all thread roots must belong to this channel.
+ pub channel_id: Uuid,
+ /// New messages only. A conflicting message ID rejects the entire batch.
+ pub messages: Vec<HistoricalMessage>,
+}
+
+/// Caller-attributed historical content. No live-message effects are requested.
+#[derive(Debug, Clone, Serialize)]
+pub struct HistoricalMessage {
+ /// Caller-generated UUIDv7, also used by the caller's durable source mapping.
+ pub id: Uuid,
+ /// Root in this batch or a previously committed batch; never another reply.
+ pub thread_id: Option<Uuid>,
+ /// Original author, or the system bot for an unmapped author.
+ pub sender: ChannelSender<'static>,
+ /// Display attribution for imported authors without a Macro identity.
+ pub imported_author: Option<String>,
+ /// Nonempty converted Markdown; attachment bytes are not accepted.
+ pub content: String,
+ /// Original creation time, with at most microsecond precision.
+ pub created_at: DateTime<Utc>,
+ /// Historical update time, not the time of import.
+ pub updated_at: DateTime<Utc>,
+ /// Original edit time, if supplied.
+ pub edited_at: Option<DateTime<Utc>>,
+ /// Source-owned provenance, including orphan-thread identity when applicable.
+ pub import_metadata: serde_json::Value,
+ /// Nonnegative source ordering, retained across batch boundaries.
+ pub import_order: i64,
+ /// Historical reactions; duplicate natural keys are ignored, never updated.
+ pub reactions: Vec<HistoricalReaction>,
+ /// User-only mentions; these do not share entities or notify recipients.
+ pub mentions: Vec<HistoricalUserMention>,
+}
+
+/// A historical reaction uses its existing natural key, not a surrogate ID.
+#[derive(Debug, Clone, Serialize)]
+pub struct HistoricalReaction {
+ /// Mapped human identity; no roster membership is required.
+ pub user_id: MacroUserIdStr<'static>,
+ /// Converted Unicode reaction, bounded by the database's 32-character limit.
+ pub emoji: String,
+ /// Original reaction time, or source message time when absent.
+ pub created_at: DateTime<Utc>,
+}
+
+/// A user mention stored without invoking live reference or sharing services.
+#[derive(Debug, Clone, Serialize)]
+pub struct HistoricalUserMention {
+ /// Caller-generated UUIDv7.
+ pub id: Uuid,
+ /// Mentioned human identity.
+ pub user_id: MacroUserIdStr<'static>,
+}
+
+impl HistoricalBatch {
+ /// Validate input bounds without allocating SQL arrays or a serialized copy.
+ /// Database-dependent thread ownership is checked by the persistence adapter.
+ pub fn validate(&self) -> Result<(), MessageError> {
+ if self.channel_id.is_nil() || self.messages.len() > MAX_HISTORICAL_MESSAGES {
+ return Err(MessageError::Invalid(
+ "invalid historical batch size or channel",
+ ));
+ }
+ let mut ids = HashSet::new();
+ let mut bytes = 0;
+ for message in &self.messages {
+ let mut size = MessageSize(0);
+ serde_json::to_writer(&mut size, message)
+ .map_err(|_| MessageError::Invalid("historical message exceeds byte limit"))?;
+ bytes += size.0;
+ if bytes > MAX_HISTORICAL_BATCH_BYTES {
+ return Err(MessageError::Invalid("historical batch exceeds byte limit"));
+ }
+ if !is_uuid_v7(message.id) || !ids.insert(message.id) {
+ return Err(MessageError::Invalid(
+ "historical message IDs must be unique UUIDv7s",
+ ));
+ }
+ if message.thread_id == Some(message.id)
+ || message.content.trim().is_empty()
+ || message.import_order < 0
+ || message
+ .imported_author
+ .as_ref()
+ .is_some_and(|name| name.trim().is_empty())
+ {
+ return Err(MessageError::Invalid("invalid historical message"));
+ }
+ validate_timestamp(message.created_at)?;
+ validate_timestamp(message.updated_at)?;
+ if message.updated_at < message.created_at {
+ return Err(MessageError::Invalid("historical update precedes creation"));
+ }
+ if let Some(edited_at) = message.edited_at {
+ validate_timestamp(edited_at)?;
+ if edited_at < message.created_at || edited_at > message.updated_at {
+ return Err(MessageError::Invalid(
+ "historical edit outside message lifetime",
+ ));
+ }
+ }
+ for reaction in &message.reactions {
+ validate_timestamp(reaction.created_at)?;
+ if reaction.emoji.trim().is_empty() || reaction.emoji.chars().count() > 32 {
+ return Err(MessageError::Invalid("invalid historical reaction"));
+ }
+ }
+ for mention in &message.mentions {
+ if !is_uuid_v7(mention.id) || !ids.insert(mention.id) {
+ return Err(MessageError::Invalid(
+ "historical mention IDs must be unique UUIDv7s",
+ ));
+ }
+ }
+ }
+ Ok(())
+ }
+}
+
+// Count encoded bytes without allocating another copy of untrusted content.
+struct MessageSize(usize);
+
+impl std::io::Write for MessageSize {
+ fn write(&mut self, bytes: &[u8]) -> std::io::Result<usize> {
+ if bytes.len() > MAX_HISTORICAL_MESSAGE_BYTES - self.0 {
+ return Err(std::io::ErrorKind::InvalidData.into());
+ }
+ self.0 += bytes.len();
+ Ok(bytes.len())
+ }
+
+ fn flush(&mut self) -> std::io::Result<()> {
+ Ok(())
+ }
+}
+
+fn is_uuid_v7(id: Uuid) -> bool {
+ id.get_variant() == uuid::Variant::RFC4122 && id.get_version() == Some(uuid::Version::SortRand)
+}
+
+fn validate_timestamp(timestamp: DateTime<Utc>) -> Result<(), MessageError> {
+ // Same interoperable range as import source timestamps: epoch through year 9999.
+ if !(0..=253_402_300_799).contains(&timestamp.timestamp())
+ || timestamp.timestamp_subsec_nanos() >= 1_000_000_000
+ || !timestamp.timestamp_subsec_nanos().is_multiple_of(1000)
+ {
+ return Err(MessageError::Invalid("invalid historical timestamp"));
+ }
+ Ok(())
+}
diff --git a/crates/messages/src/domain/mod.rs b/crates/messages/src/domain/mod.rs
index 95f93a24ba..ac6beb3363 100644
--- a/crates/messages/src/domain/mod.rs
+++ b/crates/messages/src/domain/mod.rs
@@ -12,6 +12,9 @@ pub mod delivery;
pub mod effects;
/// Transport-independent committed message facts.
pub mod events;
+/// Bounded historical writes, isolated from live message effects.
+#[cfg(feature = "ports")]
+pub mod historical;
/// Parent-independent mention identities.
pub mod mentions;
/// Shared message and thread models.
diff --git a/crates/messages/src/domain/ports.rs b/crates/messages/src/domain/ports.rs
index 5d70e40992..636137ff56 100644
--- a/crates/messages/src/domain/ports.rs
+++ b/crates/messages/src/domain/ports.rs
@@ -344,6 +344,32 @@ pub trait MessageRepository: Send + Sync + 'static {
}
}
+/// Read-only message targets. Returned identities are not authorization grants.
+pub trait HistoricalMessageReader: Send + Sync + 'static {
+ /// At most 500 distinct IDs. Omit deleted messages, deleted roots, and invalid
+ /// parent/channel relationships. Never create threads or update activity.
+ fn lookup_historical_targets(
+ &self,
+ ids: &[Uuid],
+ ) -> impl Future<Output = Result<Vec<super::historical::HistoricalMessageTarget>, MessageError>> + Send;
+}
+
+/// Silent historical persistence for trusted import compositions. Implementations
+/// atomically persist only messages, thread structure, reactions and user mentions:
+/// no activity, sharing, notifications, bots, contacts, broker or realtime effects.
+/// The caller authorizes the channel and owns source deduplication. Importers that
+/// also commit mappings/checkpoints must compose owning-crate transaction helpers
+/// instead of calling this standalone transaction boundary.
+pub trait HistoricalMessageRepository: Send + Sync + 'static {
+ /// Insert a bounded batch. Existing message IDs reject the whole batch; source
+ /// mappings must be resolved before calling. Roots may precede replies in an
+ /// earlier batch or appear anywhere in this one.
+ fn insert_historical(
+ &self,
+ batch: &super::historical::HistoricalBatch,
+ ) -> impl Future<Output = Result<(), MessageError>> + Send;
+}
+
/// Publish committed changes, deriving delivery policy from the persisted parent.
pub trait MessageEventPublisher: Send + Sync + 'static {
/// Deliver a change through realtime and contextual notification adapters.
diff --git a/crates/messages/src/outbound/pg_message_repo.rs b/crates/messages/src/outbound/pg_message_repo.rs
index 81bff5d981..0e61e40f10 100644
--- a/crates/messages/src/outbound/pg_message_repo.rs
+++ b/crates/messages/src/outbound/pg_message_repo.rs
@@ -6,6 +6,7 @@ use sqlx::{PgPool, Postgres, Transaction, types::Json};
use std::collections::HashMap;
use uuid::Uuid;
+mod historical;
mod timeline;
#[cfg(test)]
diff --git a/crates/messages/src/outbound/pg_message_repo/historical.rs b/crates/messages/src/outbound/pg_message_repo/historical.rs
new file mode 100644
index 0000000000..c5f7569031
--- /dev/null
+++ b/crates/messages/src/outbound/pg_message_repo/historical.rs
@@ -0,0 +1,315 @@
+use std::collections::HashSet;
+
+use sqlx::Acquire;
+
+use super::{PgMessageRepository, database_error};
+use crate::domain::{
+ historical::{HistoricalBatch, HistoricalMessage},
+ ports::{HistoricalMessageRepository, MessageError},
+};
+
+#[cfg(test)]
+mod test;
+
+impl crate::domain::ports::HistoricalMessageReader for PgMessageRepository {
+ async fn lookup_historical_targets(
+ &self,
+ ids: &[uuid::Uuid],
+ ) -> Result<Vec<crate::domain::historical::HistoricalMessageTarget>, MessageError> {
+ use crate::domain::historical::{HistoricalMessageTarget, MAX_HISTORICAL_MESSAGES};
+ if ids.len() > MAX_HISTORICAL_MESSAGES {
+ return Err(MessageError::Invalid("too many reference targets"));
+ }
+ sqlx::query_as!(
+ HistoricalMessageTarget,
+ r#"SELECT m.id AS message_id, m.channel_id AS "channel_id!", t.root_id
+ FROM comms_messages m
+ JOIN comms_message_threads t ON t.root_id = coalesce(m.thread_id, m.id)
+ JOIN comms_messages root ON root.id = t.root_id
+ WHERE m.id = ANY($1) AND m.deleted_at IS NULL AND t.deleted_at IS NULL
+ AND root.deleted_at IS NULL AND root.thread_id IS NULL
+ AND m.parent_entity_type = 'channel' AND m.parent_entity_id = m.channel_id::text
+ AND t.parent_entity_type = 'channel' AND t.parent_entity_id = m.channel_id::text
+ AND root.channel_id = m.channel_id AND root.parent_entity_type = 'channel'
+ AND root.parent_entity_id = m.channel_id::text"#,
+ ids,
+ )
+ .fetch_all(&self.pool)
+ .await
+ .map_err(database_error)
+ }
+}
+
+impl HistoricalMessageRepository for PgMessageRepository {
+ async fn insert_historical(&self, batch: &HistoricalBatch) -> Result<(), MessageError> {
+ let mut tx = self.pool.begin().await.map_err(database_error)?;
+ Self::insert_historical_in(&mut tx, batch).await?;
+ tx.commit().await.map_err(database_error)
+ }
+}
+
+impl PgMessageRepository {
+ /// Patch only an unchanged, undeleted importer-written body. Live edits (even
+ /// edit/revert) invalidate the timestamp guard. No timestamp, thread, reaction,
+ /// author or activity is changed and no live-message effects are emitted.
+ pub async fn patch_historical_body_in(
+ tx: &mut sqlx::Transaction<'_, sqlx::Postgres>,
+ patch: &crate::domain::historical::HistoricalBodyPatch,
+ ) -> Result<bool, MessageError> {
+ if patch.body.trim().is_empty()
+ || patch.body.len() > crate::domain::historical::MAX_HISTORICAL_MESSAGE_BYTES
+ {
+ return Err(MessageError::Invalid("invalid historical body patch"));
+ }
+ let guard = serde_json::json!({
+ "slack_import_job": patch.job_id,
+ "body_version": patch.importer_version,
+ });
+ Ok(sqlx::query!(
+ r#"UPDATE comms_messages SET content = $5
+ WHERE id = $1 AND channel_id = $2
+ AND parent_entity_type = 'channel' AND parent_entity_id = $2::text
+ AND content = $3 AND content <> $5 AND import_metadata @> $4
+ AND deleted_at IS NULL AND edited_at IS NULL AND updated_at = created_at
+ AND NOT EXISTS (SELECT 1 FROM comms_message_threads t
+ WHERE t.root_id = coalesce(comms_messages.thread_id, comms_messages.id)
+ AND t.deleted_at IS NOT NULL)"#,
+ patch.message_id,
+ patch.channel_id,
+ patch.expected_body,
+ guard,
+ patch.body,
+ )
+ .execute(&mut **tx)
+ .await
+ .map_err(database_error)?
+ .rows_affected()
+ == 1)
+ }
+
+ /// Persist historical channel messages in a caller-owned transaction without
+ /// publishing effects or committing that transaction. The composition root
+ /// must authorize the channel and atomically persist its source mappings,
+ /// fenced checkpoint, counters and dirty-search marker in the same transaction.
+ ///
+ /// A savepoint makes even late reaction failures roll back the whole batch.
+ /// Existing message IDs are conflicts, not permission to change old content;
+ /// the caller must resolve source deduplication before invoking this helper.
+ pub async fn insert_historical_in(
+ tx: &mut sqlx::Transaction<'_, sqlx::Postgres>,
+ batch: &HistoricalBatch,
+ ) -> Result<(), MessageError> {
+ batch.validate()?;
+ let mut batch_tx = tx.begin().await.map_err(database_error)?;
+ let result = insert_batch(&mut batch_tx, batch).await;
+ match result {
+ Ok(()) => batch_tx.commit().await.map_err(database_error),
+ Err(error) => {
+ batch_tx.rollback().await.map_err(database_error)?;
+ Err(error)
+ }
+ }
+ }
+}
+
+async fn insert_batch(
+ tx: &mut sqlx::Transaction<'_, sqlx::Postgres>,
+ batch: &HistoricalBatch,
+) -> Result<(), MessageError> {
+ let (roots, replies): (Vec<_>, Vec<_>) = batch
+ .messages
+ .iter()
+ .partition(|message| message.thread_id.is_none());
+ insert_messages(tx, batch.channel_id, &roots).await?;
+
+ let root_ids: Vec<_> = replies
+ .iter()
+ .filter_map(|message| message.thread_id)
+ .collect::<HashSet<_>>()
+ .into_iter()
+ .collect();
+ if !root_ids.is_empty() {
+ // Lock both structural and message state until the caller commits, so a
+ // concurrent tombstone cannot invalidate this ownership check.
+ let roots = sqlx::query!(
+ r#"SELECT t.root_id FROM comms_message_threads t
+ JOIN comms_messages m ON m.id = t.root_id
+ WHERE t.root_id = ANY($1) AND t.parent_entity_type = 'channel'
+ AND t.parent_entity_id = $2 AND m.channel_id = $3
+ AND m.thread_id IS NULL AND m.deleted_at IS NULL AND t.deleted_at IS NULL
+ ORDER BY t.root_id FOR UPDATE OF t, m"#,
+ &root_ids,
+ batch.channel_id.to_string(),
+ batch.channel_id,
+ )
+ .fetch_all(&mut **tx)
+ .await
+ .map_err(database_error)?;
+ if roots.len() != root_ids.len() {
+ return Err(MessageError::Invalid(
+ "historical reply must name a live root in its channel",
+ ));
+ }
+ }
+ insert_messages(tx, batch.channel_id, &replies).await?;
+ insert_reactions(tx, batch).await?;
+ insert_mentions(tx, batch).await
+}
+
+async fn insert_messages(
+ tx: &mut sqlx::Transaction<'_, sqlx::Postgres>,
+ channel_id: uuid::Uuid,
+ messages: &[&HistoricalMessage],
+) -> Result<(), MessageError> {
+ if messages.is_empty() {
+ return Ok(());
+ }
+ let ids: Vec<_> = messages.iter().map(|message| message.id).collect();
+ let threads: Vec<_> = messages.iter().map(|message| message.thread_id).collect();
+ let senders: Vec<_> = messages
+ .iter()
+ .map(|message| message.sender.as_ref())
+ .collect();
+ let authors: Vec<_> = messages
+ .iter()
+ .map(|message| message.imported_author.as_deref())
+ .collect();
+ let content: Vec<_> = messages
+ .iter()
+ .map(|message| message.content.as_str())
+ .collect();
+ let created: Vec<_> = messages.iter().map(|message| message.created_at).collect();
+ let updated: Vec<_> = messages.iter().map(|message| message.updated_at).collect();
+ let edited: Vec<_> = messages
+ .iter()
+ .map(|message| message.edited_at.map(|time| time.naive_utc()))
+ .collect();
+ let metadata: Vec<_> = messages
+ .iter()
+ .map(|message| message.import_metadata.clone())
+ .collect();
+ let order: Vec<_> = messages
+ .iter()
+ .map(|message| message.import_order)
+ .collect();
+ // Separate root/reply statements allow the root-thread trigger to run first.
+ // Its created_at/updated_at values come from these backdated message columns.
+ let result = sqlx::query!(
+ r#"INSERT INTO comms_messages
+ (id, channel_id, parent_entity_type, parent_entity_id, thread_id,
+ sender_id, imported_author, content, created_at, updated_at, edited_at,
+ import_metadata, import_order)
+ SELECT input.id, $1, 'channel', $2, input.thread_id, input.sender_id,
+ input.imported_author, input.content, input.created_at, input.updated_at,
+ input.edited_at, input.import_metadata, input.import_order
+ FROM UNNEST($3::uuid[], $4::uuid[], $5::text[], $6::text[], $7::text[],
+ $8::timestamptz[], $9::timestamptz[], $10::timestamp[], $11::jsonb[], $12::bigint[])
+ AS input(id, thread_id, sender_id, imported_author, content, created_at,
+ updated_at, edited_at, import_metadata, import_order)
+ ON CONFLICT (id) DO NOTHING"#,
+ channel_id,
+ channel_id.to_string(),
+ &ids,
+ &threads as _,
+ &senders as _,
+ &authors as _,
+ &content as _,
+ &created,
+ &updated,
+ &edited as _,
+ &metadata,
+ &order,
+ )
+ .execute(&mut **tx)
+ .await
+ .map_err(database_error)?;
+ if result.rows_affected() != messages.len() as u64 {
+ return Err(MessageError::Conflict);
+ }
+ Ok(())
+}
+
+async fn insert_reactions(
+ tx: &mut sqlx::Transaction<'_, sqlx::Postgres>,
+ batch: &HistoricalBatch,
+) -> Result<(), MessageError> {
+ let mut messages = Vec::new();
+ let mut users = Vec::new();
+ let mut emoji = Vec::new();
+ let mut created = Vec::new();
+ for message in &batch.messages {
+ for reaction in &message.reactions {
+ messages.push(message.id);
+ users.push(reaction.user_id.as_ref());
+ emoji.push(reaction.emoji.as_str());
+ created.push(reaction.created_at);
+ }
+ }
+ if messages.is_empty() {
+ return Ok(());
+ }
+ sqlx::query!(
+ r#"INSERT INTO comms_reactions (message_id, user_id, emoji, created_at)
+ SELECT input.message_id, input.user_id, input.emoji, input.created_at
+ FROM UNNEST($1::uuid[], $2::text[], $3::text[], $4::timestamptz[])
+ AS input(message_id, user_id, emoji, created_at)
+ ON CONFLICT (message_id, emoji, user_id) DO NOTHING"#,
+ &messages,
+ &users as _,
+ &emoji as _,
+ &created,
+ )
+ .execute(&mut **tx)
+ .await
+ .map_err(database_error)?;
+ Ok(())
+}
+
+async fn insert_mentions(
+ tx: &mut sqlx::Transaction<'_, sqlx::Postgres>,
+ batch: &HistoricalBatch,
+) -> Result<(), MessageError> {
+ let mut ids = Vec::new();
+ let mut messages = Vec::new();
+ let mut users = Vec::new();
+ let mut senders = Vec::new();
+ let mut created = Vec::new();
+ for message in &batch.messages {
+ let mut mentioned = HashSet::new();
+ for mention in &message.mentions {
+ if mentioned.insert(&mention.user_id) {
+ ids.push(mention.id);
+ messages.push(message.id.to_string());
+ users.push(mention.user_id.as_ref());
+ senders.push(message.sender.as_ref());
+ created.push(message.created_at);
+ }
+ }
+ }
+ if ids.is_empty() {
+ return Ok(());
+ }
+ let result = sqlx::query!(
+ r#"INSERT INTO comms_entity_mentions
+ (id, source_entity_type, source_entity_id, entity_type, entity_id, user_id, created_at)
+ SELECT input.id, 'message', input.message_id, 'user', input.user_id,
+ input.sender_id, input.created_at
+ FROM UNNEST($1::uuid[], $2::text[], $3::text[], $4::text[], $5::timestamptz[])
+ AS input(id, message_id, user_id, sender_id, created_at)
+ ON CONFLICT (id) DO NOTHING"#,
+ &ids,
+ &messages,
+ &users as _,
+ &senders as _,
+ &created,
+ )
+ .execute(&mut **tx)
+ .await
+ .map_err(database_error)?;
+ if result.rows_affected() != ids.len() as u64 {
+ return Err(MessageError::Invalid(
+ "historical mention id already exists",
+ ));
+ }
+ Ok(())
+}
diff --git a/crates/messages/src/outbound/pg_message_repo/historical/test.rs b/crates/messages/src/outbound/pg_message_repo/historical/test.rs
new file mode 100644
index 0000000000..fe7db1db26
--- /dev/null
+++ b/crates/messages/src/outbound/pg_message_repo/historical/test.rs
@@ -0,0 +1,578 @@
+use chrono::{DateTime, Duration, Utc};
+use macro_db_migrator::MACRO_DB_MIGRATIONS;
+use sqlx::PgPool;
+use uuid::Uuid;
+
+use super::*;
+use crate::domain::{
+ historical::{
+ HistoricalReaction, HistoricalUserMention, MAX_HISTORICAL_BATCH_BYTES,
+ MAX_HISTORICAL_MESSAGE_BYTES, MAX_HISTORICAL_MESSAGES,
+ },
+ models::MessageParent,
+ ports::MessageRepository,
+};
+
+const USER: &str = "macro|[email protected]";
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn read_targets_validate_live_roots_and_preserve_orphan_structure(pool: PgPool) {
+ use crate::domain::ports::HistoricalMessageReader;
+ let channel_id = channel(&pool).await;
+ let repo = PgMessageRepository::new(pool.clone());
+ let root = message(None, 0);
+ let reply = message(Some(root.id), 1);
+ let mut orphan = message(None, 2);
+ orphan.import_metadata = serde_json::json!({"orphaned_thread_ts": "1.000001"});
+ repo.insert_historical(&HistoricalBatch {
+ channel_id,
+ messages: vec![root.clone(), reply.clone(), orphan.clone()],
+ })
+ .await
+ .unwrap();
+ let ids = [root.id, reply.id, orphan.id, Uuid::now_v7()];
+ let found = repo.lookup_historical_targets(&ids).await.unwrap();
+ assert_eq!(found.len(), 3);
+ assert!(found.iter().all(|m| m.channel_id == channel_id));
+ assert!(
+ found
+ .iter()
+ .any(|m| m.message_id == reply.id && m.root_id == root.id)
+ );
+ assert!(
+ found
+ .iter()
+ .any(|m| m.message_id == orphan.id && m.root_id == orphan.id)
+ );
+ sqlx::query!(
+ "UPDATE comms_messages SET deleted_at = now() WHERE id = $1",
+ root.id
+ )
+ .execute(&pool)
+ .await
+ .unwrap();
+ let found = repo.lookup_historical_targets(&ids).await.unwrap();
+ assert_eq!(found.len(), 1);
+ assert_eq!(found[0].message_id, orphan.id);
+ sqlx::query!(
+ "UPDATE comms_message_threads SET deleted_at = now() WHERE root_id = $1",
+ orphan.id
+ )
+ .execute(&pool)
+ .await
+ .unwrap();
+ assert!(
+ repo.lookup_historical_targets(&ids)
+ .await
+ .unwrap()
+ .is_empty()
+ );
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn body_patch_is_guarded_silent_and_transactional(pool: PgPool) {
+ use crate::domain::historical::HistoricalBodyPatch;
+ let channel_id = channel(&pool).await;
+ let repo = PgMessageRepository::new(pool.clone());
+ let job = Uuid::now_v7();
+ let mut original = message(None, 0);
+ original.updated_at = original.created_at;
+ original.edited_at = None;
+ original.import_metadata = serde_json::json!({"slack_import_job": job, "body_version": 1});
+ let mut rows = vec![];
+ for index in 0..6 {
+ let mut row = original.clone();
+ row.id = Uuid::now_v7();
+ row.import_order = index;
+ rows.push(row);
+ }
+ repo.insert_historical(&HistoricalBatch {
+ channel_id,
+ messages: rows.clone(),
+ })
+ .await
+ .unwrap();
+ sqlx::query!(
+ "UPDATE comms_messages SET content = 'live edit', edited_at = now() WHERE id = $1",
+ rows[1].id
+ )
+ .execute(&pool)
+ .await
+ .unwrap();
+ sqlx::query!(
+ "UPDATE comms_messages SET deleted_at = now() WHERE id = $1",
+ rows[2].id
+ )
+ .execute(&pool)
+ .await
+ .unwrap();
+ // Edit/revert has the same text but must still be protected.
+ sqlx::query!(
+ "UPDATE comms_messages SET edited_at = now() WHERE id = $1",
+ rows[3].id
+ )
+ .execute(&pool)
+ .await
+ .unwrap();
+ sqlx::query!(
+ "UPDATE comms_message_threads SET deleted_at = now() WHERE root_id = $1",
+ rows[4].id
+ )
+ .execute(&pool)
+ .await
+ .unwrap();
+ for (index, row) in rows.iter().enumerate() {
+ let patch = HistoricalBodyPatch {
+ message_id: row.id,
+ channel_id,
+ job_id: job,
+ importer_version: if index == 5 { 2 } else { 1 },
+ expected_body: row.content.clone(),
+ body: "resolved body".into(),
+ };
+ let mut tx = pool.begin().await.unwrap();
+ assert_eq!(
+ PgMessageRepository::patch_historical_body_in(&mut tx, &patch)
+ .await
+ .unwrap(),
+ index == 0
+ );
+ tx.rollback().await.unwrap();
+ let mut tx = pool.begin().await.unwrap();
+ assert_eq!(
+ PgMessageRepository::patch_historical_body_in(&mut tx, &patch)
+ .await
+ .unwrap(),
+ index == 0
+ );
+ tx.commit().await.unwrap();
+ }
+ let stored = repo
+ .get(&MessageParent::Channel(channel_id), rows[0].id)
+ .await
+ .unwrap()
+ .unwrap();
+ assert_eq!(stored.content, "resolved body");
+ assert_eq!(stored.created_at, original.created_at);
+ assert_eq!(stored.updated_at, original.updated_at);
+ assert_eq!(stored.edited_at, None);
+ assert_eq!(stored.sender_id, original.sender);
+ assert!(stored.thread_id.is_none());
+ assert_eq!(
+ sqlx::query_scalar!("SELECT count(*) FROM comms_entity_mentions")
+ .fetch_one(&pool)
+ .await
+ .unwrap(),
+ Some(0)
+ );
+ assert_eq!(
+ sqlx::query_scalar!("SELECT count(*) FROM comms_reactions")
+ .fetch_one(&pool)
+ .await
+ .unwrap(),
+ Some(0)
+ );
+}
+
+fn historical_time() -> DateTime<Utc> {
+ DateTime::from_timestamp(1_600_000_000, 123_456_000).unwrap()
+}
+
+fn message(thread_id: Option<Uuid>, import_order: i64) -> HistoricalMessage {
+ HistoricalMessage {
+ id: macro_uuid::generate_uuid_v7(),
+ thread_id,
+ sender: USER.try_into().unwrap(),
+ imported_author: None,
+ content: "Historical content".into(),
+ created_at: historical_time(),
+ updated_at: historical_time() + Duration::seconds(1),
+ edited_at: Some(historical_time() + Duration::seconds(1)),
+ import_metadata: serde_json::json!({"source": "slack", "ts": "1600000000.123456"}),
+ import_order,
+ reactions: vec![],
+ mentions: vec![],
+ }
+}
+
+async fn channel(pool: &PgPool) -> Uuid {
+ let id = macro_uuid::generate_uuid_v7();
+ sqlx::query!(
+ "INSERT INTO comms_channels (id, name, channel_type, owner_id) VALUES ($1, 'History', 'private', $2)",
+ id, USER,
+ ).execute(pool).await.unwrap();
+ id
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn roots_replies_attribution_and_order_across_batches(pool: PgPool) {
+ let channel_id = channel(&pool).await;
+ let parent = MessageParent::Channel(channel_id);
+ let repo = PgMessageRepository::new(pool.clone());
+ let mut root = message(None, 0);
+ root.sender = channel_sender::ChannelSender::new_from_bot(bot_id::MACRO_SYSTEM_BOT_ID);
+ root.imported_author = Some("Archive Author".into());
+ let later = message(Some(root.id), 20);
+ // Replies may occur before their root in the input vector.
+ repo.insert_historical(&HistoricalBatch {
+ channel_id,
+ messages: vec![later.clone(), root.clone()],
+ })
+ .await
+ .unwrap();
+ let earlier = message(Some(root.id), 10);
+ repo.insert_historical(&HistoricalBatch {
+ channel_id,
+ messages: vec![earlier.clone()],
+ })
+ .await
+ .unwrap();
+
+ let hydrated = repo.get(&parent, root.id).await.unwrap().unwrap();
+ assert_eq!(hydrated.imported_author.unwrap().name, "Archive Author");
+ assert_eq!(hydrated.sender_id, root.sender);
+ assert!(hydrated.bot_profile.is_some());
+ assert_eq!(hydrated.created_at, root.created_at);
+ assert_eq!(hydrated.updated_at, root.updated_at);
+ assert_eq!(hydrated.edited_at, root.edited_at);
+ let thread = repo.thread(&parent, root.id).await.unwrap().unwrap();
+ assert_eq!(thread.created_at, root.created_at);
+ assert_eq!(thread.updated_at, root.updated_at);
+ let replies = repo.replies(&parent, root.id).await.unwrap();
+ assert_eq!(
+ replies.iter().map(|reply| reply.id).collect::<Vec<_>>(),
+ vec![earlier.id, later.id]
+ );
+ let stored = sqlx::query!(
+ "SELECT channel_id, parent_entity_type, parent_entity_id, import_metadata, import_order FROM comms_messages WHERE id = $1",
+ root.id,
+ ).fetch_one(&pool).await.unwrap();
+ assert_eq!(stored.channel_id, Some(channel_id));
+ assert_eq!(stored.parent_entity_type, "channel");
+ assert_eq!(stored.parent_entity_id, channel_id.to_string());
+ assert_eq!(stored.import_metadata, Some(root.import_metadata));
+ assert_eq!(stored.import_order, Some(0));
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn reactions_mentions_are_unique_and_have_no_live_effects(pool: PgPool) {
+ let channel_id = channel(&pool).await;
+ let before = sqlx::query!(
+ "SELECT updated_at FROM comms_channels WHERE id = $1",
+ channel_id
+ )
+ .fetch_one(&pool)
+ .await
+ .unwrap();
+ let repo = PgMessageRepository::new(pool.clone());
+ let mut root = message(None, 0);
+ let reaction = HistoricalReaction {
+ user_id: USER.try_into().unwrap(),
+ emoji: "\u{1f44d}".into(),
+ created_at: historical_time(),
+ };
+ root.reactions = vec![reaction.clone(), reaction];
+ root.mentions = (0..2)
+ .map(|_| HistoricalUserMention {
+ id: macro_uuid::generate_uuid_v7(),
+ user_id: USER.try_into().unwrap(),
+ })
+ .collect();
+ repo.insert_historical(&HistoricalBatch {
+ channel_id,
+ messages: vec![root.clone()],
+ })
+ .await
+ .unwrap();
+ let hydrated = repo
+ .get(&MessageParent::Channel(channel_id), root.id)
+ .await
+ .unwrap()
+ .unwrap();
+ assert_eq!(hydrated.reactions.len(), 1);
+ assert_eq!(hydrated.reactions[0].users, vec![USER]);
+ assert_eq!(hydrated.mentions.len(), 1);
+ assert_eq!(hydrated.mentions[0].entity_type, "user");
+ let reaction = sqlx::query!(
+ "SELECT created_at FROM comms_reactions WHERE message_id = $1",
+ root.id
+ )
+ .fetch_one(&pool)
+ .await
+ .unwrap();
+ assert_eq!(reaction.created_at, historical_time());
+ let mention = sqlx::query!(
+ "SELECT created_at, user_id FROM comms_entity_mentions WHERE source_entity_type = 'message' AND source_entity_id = $1",
+ root.id.to_string(),
+ ).fetch_one(&pool).await.unwrap();
+ assert_eq!(mention.created_at, historical_time());
+ assert_eq!(mention.user_id.as_deref(), Some(USER));
+ assert_eq!(
+ sqlx::query_scalar!("SELECT count(*) FROM comms_activity")
+ .fetch_one(&pool)
+ .await
+ .unwrap(),
+ Some(0)
+ );
+ assert_eq!(
+ sqlx::query_scalar!("SELECT count(*) FROM comms_channel_participants")
+ .fetch_one(&pool)
+ .await
+ .unwrap(),
+ Some(0)
+ );
+ assert_eq!(
+ sqlx::query_scalar!("SELECT count(*) FROM comms_attachments")
+ .fetch_one(&pool)
+ .await
+ .unwrap(),
+ Some(0)
+ );
+ let after = sqlx::query!(
+ "SELECT updated_at FROM comms_channels WHERE id = $1",
+ channel_id
+ )
+ .fetch_one(&pool)
+ .await
+ .unwrap();
+ assert_eq!(before.updated_at, after.updated_at);
+ // This adapter has no event publisher/effect capability. The historical port
+ // is implemented directly, never through MessageCommands or live services.
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn rejects_foreign_missing_reply_and_deleted_roots(pool: PgPool) {
+ let first_channel = channel(&pool).await;
+ let second_channel = channel(&pool).await;
+ let repo = PgMessageRepository::new(pool.clone());
+ let root = message(None, 0);
+ let reply = message(Some(root.id), 1);
+ repo.insert_historical(&HistoricalBatch {
+ channel_id: first_channel,
+ messages: vec![root.clone(), reply.clone()],
+ })
+ .await
+ .unwrap();
+ for (channel_id, root_id) in [
+ (second_channel, root.id),
+ (first_channel, reply.id),
+ (first_channel, macro_uuid::generate_uuid_v7()),
+ ] {
+ let new_root = message(None, 2);
+ let result = repo
+ .insert_historical(&HistoricalBatch {
+ channel_id,
+ messages: vec![new_root.clone(), message(Some(root_id), 3)],
+ })
+ .await;
+ assert!(matches!(result, Err(MessageError::Invalid(_))));
+ assert!(
+ repo.get(&MessageParent::Channel(channel_id), new_root.id)
+ .await
+ .unwrap()
+ .is_none()
+ );
+ }
+ repo.delete_thread(&MessageParent::Channel(first_channel), root.id)
+ .await
+ .unwrap();
+ assert!(matches!(
+ repo.insert_historical(&HistoricalBatch {
+ channel_id: first_channel,
+ messages: vec![message(Some(root.id), 4)],
+ })
+ .await,
+ Err(MessageError::Invalid(_))
+ ));
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn reaction_failure_rolls_back_savepoint_and_outer_transaction_stays_usable(pool: PgPool) {
+ let channel_id = channel(&pool).await;
+ let mut root = message(None, 0);
+ root.reactions.push(HistoricalReaction {
+ user_id: USER.try_into().unwrap(),
+ // PostgreSQL rejects NUL in text during the reaction statement, after
+ // messages and their root-thread rows have already been inserted.
+ emoji: "\0".into(),
+ created_at: historical_time(),
+ });
+ let mut tx = pool.begin().await.unwrap();
+ assert!(
+ PgMessageRepository::insert_historical_in(
+ &mut tx,
+ &HistoricalBatch {
+ channel_id,
+ messages: vec![root.clone()],
+ }
+ )
+ .await
+ .is_err()
+ );
+ assert_eq!(
+ sqlx::query_scalar!("SELECT count(*) FROM comms_messages")
+ .fetch_one(&mut *tx)
+ .await
+ .unwrap(),
+ Some(0)
+ );
+ assert_eq!(
+ sqlx::query_scalar!("SELECT count(*) FROM comms_message_threads")
+ .fetch_one(&mut *tx)
+ .await
+ .unwrap(),
+ Some(0)
+ );
+ root.reactions.clear();
+ PgMessageRepository::insert_historical_in(
+ &mut tx,
+ &HistoricalBatch {
+ channel_id,
+ messages: vec![root.clone()],
+ },
+ )
+ .await
+ .unwrap();
+ // Successful helper calls still do not commit the caller's transaction.
+ tx.rollback().await.unwrap();
+ assert_eq!(
+ sqlx::query_scalar!("SELECT count(*) FROM comms_messages")
+ .fetch_one(&pool)
+ .await
+ .unwrap(),
+ Some(0)
+ );
+ assert_eq!(
+ sqlx::query_scalar!("SELECT count(*) FROM comms_reactions")
+ .fetch_one(&pool)
+ .await
+ .unwrap(),
+ Some(0)
+ );
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn existing_id_is_a_conflict_without_partial_writes_or_overwrite(pool: PgPool) {
+ let channel_id = channel(&pool).await;
+ let repo = PgMessageRepository::new(pool.clone());
+ let mut original = message(None, 0);
+ original.mentions.push(HistoricalUserMention {
+ id: macro_uuid::generate_uuid_v7(),
+ user_id: USER.try_into().unwrap(),
+ });
+ repo.insert_historical(&HistoricalBatch {
+ channel_id,
+ messages: vec![original.clone()],
+ })
+ .await
+ .unwrap();
+ original.content = "Replacement must not win".into();
+ let new_root = message(None, 1);
+ assert!(matches!(
+ repo.insert_historical(&HistoricalBatch {
+ channel_id,
+ messages: vec![new_root.clone(), original.clone()],
+ })
+ .await,
+ Err(MessageError::Conflict)
+ ));
+ let parent = MessageParent::Channel(channel_id);
+ assert!(repo.get(&parent, new_root.id).await.unwrap().is_none());
+ assert_eq!(
+ repo.get(&parent, original.id)
+ .await
+ .unwrap()
+ .unwrap()
+ .content,
+ "Historical content"
+ );
+ let mut conflicting_mention = new_root;
+ conflicting_mention.mentions = original.mentions;
+ assert!(matches!(
+ repo.insert_historical(&HistoricalBatch {
+ channel_id,
+ messages: vec![conflicting_mention.clone()],
+ })
+ .await,
+ Err(MessageError::Invalid(
+ "historical mention id already exists"
+ ))
+ ));
+ assert!(
+ repo.get(&parent, conflicting_mention.id)
+ .await
+ .unwrap()
+ .is_none()
+ );
+}
+
+#[test]
+fn validates_timestamps_ids_counts_and_byte_limits() {
+ let valid = HistoricalBatch {
+ channel_id: macro_uuid::generate_uuid_v7(),
+ messages: vec![message(None, 0)],
+ };
+ valid.validate().unwrap();
+ let invalid_changes: Vec<fn(&mut HistoricalMessage)> = vec![
+ |m| m.id = Uuid::nil(),
+ |m| m.id = "00000000-0000-7000-0000-000000000000".parse().unwrap(),
+ |m| m.id = "00000000-0000-4000-8000-000000000000".parse().unwrap(),
+ |m| {
+ m.mentions.push(HistoricalUserMention {
+ id: m.id,
+ user_id: USER.try_into().unwrap(),
+ })
+ },
+ |m| m.thread_id = Some(m.id),
+ |m| m.import_order = -1,
+ |m| m.content = " \n".into(),
+ |m| m.created_at = DateTime::from_timestamp(-1, 0).unwrap(),
+ |m| m.updated_at = DateTime::from_timestamp(253_402_300_800, 0).unwrap(),
+ |m| m.created_at += Duration::nanoseconds(1),
+ |m| m.updated_at = m.created_at - Duration::seconds(1),
+ |m| m.edited_at = Some(m.updated_at + Duration::seconds(1)),
+ |m| m.content = "a".repeat(MAX_HISTORICAL_MESSAGE_BYTES),
+ |m| {
+ m.import_metadata =
+ serde_json::json!({"large": "a".repeat(MAX_HISTORICAL_MESSAGE_BYTES)})
+ },
+ |m| {
+ m.reactions.push(HistoricalReaction {
+ user_id: USER.try_into().unwrap(),
+ emoji: "a".repeat(33),
+ created_at: historical_time(),
+ })
+ },
+ ];
+ for change in invalid_changes {
+ let mut invalid = valid.clone();
+ change(&mut invalid.messages[0]);
+ assert!(invalid.validate().is_err());
+ }
+ let mut oversized = valid.clone();
+ oversized.messages = (0..=MAX_HISTORICAL_MESSAGES)
+ .map(|_| message(None, 0))
+ .collect();
+ assert!(oversized.validate().is_err());
+ oversized.messages.pop();
+ oversized.validate().unwrap();
+ oversized.messages = (0..5)
+ .map(|_| {
+ let mut m = message(None, 0);
+ m.content = "a".repeat(MAX_HISTORICAL_BATCH_BYTES / 5);
+ m
+ })
+ .collect();
+ assert!(oversized.validate().is_err());
+ let mut duplicate = valid.clone();
+ duplicate.messages.push(duplicate.messages[0].clone());
+ assert!(duplicate.validate().is_err());
+ for seconds in [0, 253_402_300_799] {
+ let mut boundary = valid.clone();
+ let message = &mut boundary.messages[0];
+ message.created_at = DateTime::from_timestamp(seconds, 999_999_000).unwrap();
+ message.updated_at = message.created_at;
+ message.edited_at = None;
+ boundary.validate().unwrap();
+ }
+}
diff --git a/crates/opensearch_client/src/search/channels/test.rs b/crates/opensearch_client/src/search/channels/test.rs
index 9709c66d8e..d394f78ef6 100644
--- a/crates/opensearch_client/src/search/channels/test.rs
+++ b/crates/opensearch_client/src/search/channels/test.rs
@@ -46,6 +46,22 @@ fn millis_timestamps_are_distinct_within_a_second() {
);
}
+#[test]
+fn author_only_match_without_highlight_keeps_message_and_thread_deep_links() {
+ for thread in [None, Some(uuid::Uuid::now_v7())] {
+ let mut source = channel_index(Some(1_700_000_000_123));
+ let message_id = source.message_id;
+ source.thread_id = thread.unwrap_or(message_id);
+ let result = channel_hit_to_search_hit(hit_for(source));
+ let Some(SearchGotoContent::Channels(goto)) = result.goto else {
+ panic!("channel hit must retain a deep link without highlights");
+ };
+ assert_eq!(goto.channel_message_id, message_id);
+ assert_eq!(goto.thread_id, thread);
+ assert_eq!(goto.sender_id, "macro|[email protected]");
+ }
+}
+
#[test]
fn test_build_bool_query() -> anyhow::Result<()> {
let builder = ChannelMessageQueryBuilder::new(vec!["test".to_string()])
diff --git a/crates/opensearch_client/src/search/unified/test.rs b/crates/opensearch_client/src/search/unified/test.rs
index 84a4396c50..4f1d5b15a1 100644
--- a/crates/opensearch_client/src/search/unified/test.rs
+++ b/crates/opensearch_client/src/search/unified/test.rs
@@ -5,6 +5,40 @@ use chrono::Utc;
use models_search_cursor::SearchMethodCursor;
use opensearch_query_builder::ToOpenSearchJson;
+#[test]
+fn author_only_channel_match_without_highlight_keeps_deep_link() {
+ let channel_id = uuid::Uuid::now_v7();
+ let message_id = uuid::Uuid::now_v7();
+ for thread in [None, Some(uuid::Uuid::now_v7())] {
+ let source = UnifiedSearchIndex::from_matched(
+ &["channels".into()],
+ serde_json::json!({
+ "entity_id": channel_id, "message_id": message_id,
+ "thread_id": thread.unwrap_or(message_id), "channel_type": "private",
+ "sender_id": "macro|[email protected]", "mentions": [],
+ "imported_author": "quartzarchivebot"
+ }),
+ )
+ .unwrap();
+ let hits = expand_hit_into_search_hits(Hit {
+ source,
+ index: "channels_v2".into(),
+ matched_queries: vec!["channels".into()],
+ score: Some(1.0),
+ highlight: None,
+ inner_hits: None,
+ });
+ assert_eq!(hits.len(), 1);
+ assert_eq!(hits[0].entity_id, channel_id);
+ let Some(SearchGotoContent::Channels(goto)) = &hits[0].goto else {
+ panic!("channel hit must retain a deep link without highlights");
+ };
+ assert_eq!(goto.channel_message_id, message_id);
+ assert_eq!(goto.thread_id, thread);
+ assert_eq!(goto.sender_id, "macro|[email protected]");
+ }
+}
+
#[test]
fn agent_sessions_expand_and_paginate_as_entities_not_messages() {
let session_id = uuid::Uuid::from_u128(1);
diff --git a/crates/opensearch_client/src/upsert/channel_message.rs b/crates/opensearch_client/src/upsert/channel_message.rs
index e8ba14a271..46611e7ec4 100644
--- a/crates/opensearch_client/src/upsert/channel_message.rs
+++ b/crates/opensearch_client/src/upsert/channel_message.rs
@@ -13,12 +13,17 @@ pub struct UpsertChannelMessageArgs {
pub message_id: String,
pub thread_id: String,
pub sender_id: String,
+ /// Searchable fallback display name, never an authenticated sender identity.
+ pub imported_author: Option<String>,
pub mentions: Vec<String>,
pub content: String,
pub created_at_millis: EpochMillis,
pub updated_at_millis: EpochMillis,
}
+#[cfg(test)]
+mod test;
+
#[tracing::instrument(skip(client))]
pub(crate) async fn upsert_channel_message(
client: &opensearch::OpenSearch,
diff --git a/crates/opensearch_client/src/upsert/channel_message/test.rs b/crates/opensearch_client/src/upsert/channel_message/test.rs
new file mode 100644
index 0000000000..6e2669d8aa
--- /dev/null
+++ b/crates/opensearch_client/src/upsert/channel_message/test.rs
@@ -0,0 +1,104 @@
+use super::*;
+use serde_json::json;
+
+fn args() -> UpsertChannelMessageArgs {
+ let message_id = uuid::Uuid::now_v7().to_string();
+ UpsertChannelMessageArgs {
+ channel_id: uuid::Uuid::now_v7().to_string(),
+ channel_type: "private".into(),
+ org_id: None,
+ message_id: message_id.clone(),
+ thread_id: message_id,
+ sender_id: "macro|[email protected]".into(),
+ imported_author: Some("quartzarchivebot".into()),
+ mentions: vec![],
+ content: "Historical message body".into(),
+ created_at_millis: EpochMillis::new(1_600_000_000_123).unwrap(),
+ updated_at_millis: EpochMillis::new(1_600_000_000_123).unwrap(),
+ }
+}
+
+#[test]
+fn author_is_text_not_content_or_sender() {
+ let mut args = args();
+ for author in [Some("quartzarchivebot".to_owned()), None] {
+ args.imported_author = author.clone();
+ let value = serde_json::to_value(&args).unwrap();
+ assert_eq!(value["imported_author"], json!(author));
+ assert_eq!(value["content"], "Historical message body");
+ assert_eq!(value["sender_id"], "macro|[email protected]");
+ assert_eq!(value["entity_id"], args.channel_id);
+ assert_eq!(value["message_id"], args.message_id);
+ }
+}
+
+/// Scratch-index integration of copy_to with the production access/sender query.
+#[tokio::test]
+#[ignore = "requires local OpenSearch at http://localhost:9200"]
+async fn imported_author_matches_only_with_channel_access() {
+ use crate::{OpensearchClient, search::channels::ChannelMessageQueryBuilder};
+ use opensearch::{
+ SearchParts,
+ indices::{IndicesDeleteParts, IndicesRefreshParts},
+ };
+ use opensearch_query_builder::ToOpenSearchJson;
+
+ let client =
+ OpensearchClient::new("http://localhost:9200".into(), "".into(), "".into()).unwrap();
+ let index = format!("imported-author-test-{}", uuid::Uuid::now_v7());
+ client.ensure_index_exists(&index, json!({
+ "settings": {"number_of_shards": 1, "number_of_replicas": 0,
+ "analysis": {"analyzer": {"content_text": {
+ "type": "custom", "tokenizer": "standard", "filter": ["icu_folding"]
+ }}}},
+ "mappings": {"dynamic": false, "properties": {
+ "entity_id": {"type": "keyword"},
+ "sender_id": {"type": "keyword"},
+ "content": {"type": "text", "analyzer": "content_text"},
+ "imported_author": {"type": "text", "analyzer": "content_text", "copy_to": "content"}
+ }}
+ })).await.unwrap();
+
+ let result: anyhow::Result<()> = async {
+ let args = args();
+ client.upsert_channel_message(&args, Some(&index)).await?;
+ client.inner.indices().refresh(IndicesRefreshParts::Index(&[&index]))
+ .send().await?.error_for_status_code()?;
+ for (channel_id, sender_ids, expected) in [
+ (args.channel_id.clone(), vec![], 1),
+ (uuid::Uuid::now_v7().to_string(), vec![], 0),
+ (args.channel_id.clone(), vec!["quartzarchivebot".into()], 0),
+ (args.channel_id.clone(), vec![args.sender_id.clone()], 1),
+ ] {
+ let builder = ChannelMessageQueryBuilder::new(vec!["quartzarchivebot".into()])
+ .user_id("macro|[email protected]")
+ .ids_only(true).ids(vec![channel_id]).sender_ids(sender_ids);
+ let mut query = builder.build_bool_query()?.build().to_json();
+ // Only redirect the production index constraint to our isolated scratch index.
+ let filters = query["bool"]["filter"].as_array_mut().unwrap();
+ let index_filter = filters.iter_mut().find(|filter| filter["term"]["_index"].is_string()).unwrap();
+ index_filter["term"]["_index"] = json!(index);
+ let response: serde_json::Value = client.inner.search(SearchParts::Index(&[&index]))
+ .body(json!({"query": query, "highlight": {"fields": {"content": {"type": "plain"}}}}))
+ .send().await?.error_for_status_code()?.json().await?;
+ assert_eq!(response["hits"]["total"]["value"], expected);
+ if expected == 1 {
+ let hit = &response["hits"]["hits"][0];
+ assert_eq!(hit["_source"]["content"], args.content);
+ assert_eq!(hit["_source"]["message_id"], args.message_id);
+ assert_eq!(hit["_source"]["thread_id"], args.thread_id);
+ }
+ }
+ Ok(())
+ }.await;
+ client
+ .inner
+ .indices()
+ .delete(IndicesDeleteParts::Index(&[&index]))
+ .send()
+ .await
+ .unwrap()
+ .error_for_status_code()
+ .unwrap();
+ result.unwrap();
+}
diff --git a/crates/s3_key/Cargo.toml b/crates/s3_key/Cargo.toml
index 5fbc33279e..bd4485ac70 100644
--- a/crates/s3_key/Cargo.toml
+++ b/crates/s3_key/Cargo.toml
@@ -8,4 +8,5 @@ publish = false
anyhow = { workspace = true }
model_owner = { path = "../model_owner" }
urlencoding = { workspace = true }
+uuid = { workspace = true }
workspace-hack = { version = "0.1", path = "../workspace-hack" }
diff --git a/crates/s3_key/src/lib.rs b/crates/s3_key/src/lib.rs
index 1613f3b0a1..d33db193f3 100644
--- a/crates/s3_key/src/lib.rs
+++ b/crates/s3_key/src/lib.rs
@@ -11,5 +11,7 @@ pub use document_key::{
};
mod bulk_upload_key;
pub use bulk_upload_key::BulkUploadStagingKey;
+mod slack_import_key;
+pub use slack_import_key::{InvalidSlackImportKey, SlackImportKey};
mod static_file_key;
pub use static_file_key::StaticFileKey;
diff --git a/crates/s3_key/src/slack_import_key.rs b/crates/s3_key/src/slack_import_key.rs
new file mode 100644
index 0000000000..f3fc45d381
--- /dev/null
+++ b/crates/s3_key/src/slack_import_key.rs
@@ -0,0 +1,125 @@
+use std::{fmt, str::FromStr};
+
+use uuid::Uuid;
+
+#[cfg(test)]
+mod test;
+
+/// Canonical staging key: `slack-import/{team}/{job}/users.json` or
+/// `slack-import/{team}/{job}/{conversation}/{part}.ndjson`.
+/// A key's shape is not authorization; callers must resolve it through a manifest.
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct SlackImportKey {
+ team: Uuid,
+ job: Uuid,
+ part: Option<(String, u32)>,
+}
+
+/// A malformed or noncanonical Slack staging key, without exposing the input.
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub struct InvalidSlackImportKey;
+
+impl fmt::Display for InvalidSlackImportKey {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ f.write_str("invalid Slack import key")
+ }
+}
+
+impl std::error::Error for InvalidSlackImportKey {}
+
+impl SlackImportKey {
+ /// Build the normalized users payload key.
+ pub fn users(team: Uuid, job: Uuid) -> Result<Self, InvalidSlackImportKey> {
+ if team.is_nil() || team.is_max() || job.is_nil() || job.is_max() {
+ return Err(InvalidSlackImportKey);
+ }
+ Ok(Self {
+ team,
+ job,
+ part: None,
+ })
+ }
+
+ /// Build a zero-based conversation part key, rejecting unsafe/non-Slack IDs.
+ pub fn conversation_part(
+ team: Uuid,
+ job: Uuid,
+ conversation: &str,
+ part: u32,
+ ) -> Result<Self, InvalidSlackImportKey> {
+ if !(2..=64).contains(&conversation.len())
+ || !matches!(conversation.as_bytes()[0], b'C' | b'G' | b'D')
+ || !conversation
+ .bytes()
+ .all(|b| b.is_ascii_uppercase() || b.is_ascii_digit())
+ {
+ return Err(InvalidSlackImportKey);
+ }
+ let mut key = Self::users(team, job)?;
+ key.part = Some((conversation.to_owned(), part));
+ Ok(key)
+ }
+
+ /// Owning Macro team.
+ pub fn team(&self) -> Uuid {
+ self.team
+ }
+
+ /// Owning import job.
+ pub fn job(&self) -> Uuid {
+ self.job
+ }
+
+ /// Conversation identity and index, or `None` for users metadata.
+ pub fn part(&self) -> Option<(&str, u32)> {
+ self.part.as_ref().map(|(id, index)| (id.as_str(), *index))
+ }
+
+ /// Render the canonical S3 key.
+ pub fn to_key(&self) -> String {
+ self.to_string()
+ }
+
+ /// Parse only the exact canonical spelling (no encoding, aliases or padding).
+ pub fn from_s3_key(value: &str) -> Result<Self, InvalidSlackImportKey> {
+ value.parse()
+ }
+}
+
+impl fmt::Display for SlackImportKey {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ write!(f, "slack-import/{}/{}/", self.team, self.job)?;
+ match &self.part {
+ None => f.write_str("users.json"),
+ Some((id, part)) => write!(f, "{id}/{part}.ndjson"),
+ }
+ }
+}
+
+impl FromStr for SlackImportKey {
+ type Err = InvalidSlackImportKey;
+
+ fn from_str(value: &str) -> Result<Self, Self::Err> {
+ let segments: Vec<_> = value.split('/').collect();
+ let key = match segments.as_slice() {
+ ["slack-import", team, job, "users.json"] => Self::users(
+ team.parse().map_err(|_| InvalidSlackImportKey)?,
+ job.parse().map_err(|_| InvalidSlackImportKey)?,
+ )?,
+ ["slack-import", team, job, conversation, file] => Self::conversation_part(
+ team.parse().map_err(|_| InvalidSlackImportKey)?,
+ job.parse().map_err(|_| InvalidSlackImportKey)?,
+ conversation,
+ file.strip_suffix(".ndjson")
+ .ok_or(InvalidSlackImportKey)?
+ .parse()
+ .map_err(|_| InvalidSlackImportKey)?,
+ )?,
+ _ => return Err(InvalidSlackImportKey),
+ };
+ if key.to_string() != value {
+ return Err(InvalidSlackImportKey);
+ }
+ Ok(key)
+ }
+}
diff --git a/crates/s3_key/src/slack_import_key/test.rs b/crates/s3_key/src/slack_import_key/test.rs
new file mode 100644
index 0000000000..98e5dd7592
--- /dev/null
+++ b/crates/s3_key/src/slack_import_key/test.rs
@@ -0,0 +1,70 @@
+use super::*;
+
+const TEAM: &str = "01980000-0000-7000-8000-000000000001";
+const JOB: &str = "01980000-0000-7000-8000-000000000002";
+
+#[test]
+fn canonical_keys_round_trip() {
+ let team = TEAM.parse().unwrap();
+ let job = JOB.parse().unwrap();
+ let users = SlackImportKey::users(team, job).unwrap();
+ assert_eq!(
+ users.to_key(),
+ format!("slack-import/{TEAM}/{JOB}/users.json")
+ );
+ for key in [
+ users,
+ SlackImportKey::conversation_part(team, job, "C123", 0).unwrap(),
+ SlackImportKey::conversation_part(team, job, "D123", u32::MAX).unwrap(),
+ ] {
+ assert_eq!(key.to_string().parse::<SlackImportKey>().unwrap(), key);
+ assert_eq!(key.team(), team);
+ assert_eq!(key.job(), job);
+ }
+}
+
+#[test]
+fn rejects_unsafe_and_noncanonical_keys() {
+ let prefix = format!("slack-import/{TEAM}/{JOB}");
+ for suffix in [
+ "",
+ "users.json/",
+ "users.json/extra",
+ "org_users.json",
+ "../users.json",
+ "C123/00.ndjson",
+ "C123/+1.ndjson",
+ "C123/-1.ndjson",
+ "C123/4294967296.ndjson",
+ "C123/1.NDJSON",
+ "C123%2f/1.ndjson",
+ "C123\\/1.ndjson",
+ "C123\n/1.ndjson",
+ "c123/1.ndjson",
+ "C/1.ndjson",
+ "/users.json",
+ "U123/1.ndjson",
+ ] {
+ assert!(
+ format!("{prefix}/{suffix}")
+ .parse::<SlackImportKey>()
+ .is_err(),
+ "{suffix:?}"
+ );
+ }
+ for value in [
+ format!("/{prefix}/users.json"),
+ format!("s3://bucket/{prefix}/users.json"),
+ format!("slack-import/{}/{JOB}/users.json", TEAM.replace('-', "")),
+ format!("slack-import/01980000-ABCD-7000-8000-000000000001/{JOB}/users.json"),
+ format!("slack-import/{}/{JOB}/users.json", Uuid::nil()),
+ ] {
+ assert!(value.parse::<SlackImportKey>().is_err());
+ }
+ for id in ["..", "C1/..", "C%31", "C 1", "Cé", ""] {
+ assert!(
+ SlackImportKey::conversation_part(TEAM.parse().unwrap(), JOB.parse().unwrap(), id, 0)
+ .is_err()
+ );
+ }
+}
diff --git a/crates/slack_integration/Cargo.toml b/crates/slack_integration/Cargo.toml
new file mode 100644
index 0000000000..04a97392c0
--- /dev/null
+++ b/crates/slack_integration/Cargo.toml
@@ -0,0 +1,56 @@
+[package]
+edition = "2024"
+name = "slack_integration"
+publish = false
+version = "0.1.0"
+
+[features]
+default = []
+# Adapter implementations are added separately; models remain usable alone.
+inbound = ["dep:axum", "dep:utoipa", "dep:macro_authorization", "entity_access/inbound", "ports"]
+outbound = ["ports"]
+ports = ["dep:entity_access", "dep:futures", "dep:rootcause", "dep:tracing"]
+gateway = ["dep:connection_gateway_client", "outbound"]
+ledger = ["dep:import", "outbound"]
+postgres = ["dep:sqlx", "ledger"]
+s3 = ["dep:aws-sdk-s3", "dep:base64", "dep:macro_aws_config", "dep:s3_key", "dep:tokio", "outbound"]
+sqs = ["dep:aws-sdk-sqs", "dep:macro_queues", "outbound"]
+search = ["dep:reqwest", "outbound"]
+worker = ["dep:tokio", "dep:tokio-util", "ports"]
+
+[dependencies]
+aws-sdk-s3 = { workspace = true, optional = true }
+aws-sdk-sqs = { workspace = true, optional = true }
+axum = { workspace = true, optional = true }
+base64 = { workspace = true, optional = true }
+chrono = { workspace = true }
+connection_gateway_client = { path = "../connection_gateway_client", optional = true }
+entity_access = { path = "../entity_access", optional = true, default-features = false }
+futures = { workspace = true, optional = true }
+import = { path = "../import", optional = true }
+macro_authorization = { path = "../macro_authorization", default-features = false, features = ["axum"], optional = true }
+macro_aws_config = { path = "../macro_aws_config", optional = true }
+macro_queues = { path = "../macro_queues", optional = true }
+macro_user_id = { path = "../macro_user_id" }
+mention_utils = { path = "../mention_utils" }
+reqwest = { workspace = true, optional = true }
+rootcause = { workspace = true, optional = true }
+s3_key = { path = "../s3_key", optional = true }
+serde = { workspace = true }
+serde_json = { workspace = true }
+sha2 = { workspace = true }
+sqlx = { workspace = true, optional = true }
+thiserror = { workspace = true }
+tokio = { workspace = true, optional = true, features = ["io-util"] }
+tokio-util = { workspace = true, optional = true }
+tracing = { workspace = true, optional = true }
+utoipa = { workspace = true, optional = true }
+url = { workspace = true }
+uuid = { workspace = true }
+workspace-hack = { version = "0.1", path = "../workspace-hack" }
+
+[dev-dependencies]
+model_user = { path = "../model_user" }
+tokio = { workspace = true }
+tower = { workspace = true }
+
diff --git a/crates/slack_integration/data/README.md b/crates/slack_integration/data/README.md
new file mode 100644
index 0000000000..229eb74a95
--- /dev/null
+++ b/crates/slack_integration/data/README.md
@@ -0,0 +1,59 @@
+# Slack reaction shortcodes
+
+`emoji-shortcodes.json` is generated, not hand-maintained. It maps names without
+colons to the same Unicode strings the frontend uses for stored reactions.
+
+From the repository root, with the locked frontend packages installed:
+
+```sh
+bun apps/web/scripts/generate-slack-emoji-map.ts
+bun apps/web/scripts/generate-slack-emoji-map.ts --check
+```
+
+## Pinned provenance
+
+- [emojibase-data](https://github.com/milesj/emojibase/tree/master/packages/data)
+ **17.0.0**, MIT: `en/shortcodes/iamcal.json` (Slack-compatible names) and
+ `en/shortcodes/github.json` (additional aliases). Slack/iamcal wins collisions,
+ including `email`. Includes `+1`/`thumbsup`, `-1`/`thumbsdown`,
+ `hankey`/`poop`/`shit`, and `flag-*` names where supplied upstream.
+- [unicode-emoji-json](https://github.com/muan/unicode-emoji-json) **0.8.0**,
+ MIT: `data-ordered-emoji.json`, for canonical output matching
+ `apps/web/src/lib/core/component/Emoji/emojis.ts`. Join keys ignore FE0F;
+ output strings are copied intact, not normalized independently in Rust.
+- Exact package integrity hashes are recorded in the root `bun.lock`. The
+ generator rejects version drift; review this file and regression tests on bumps.
+
+Entries absent from the frontend's canonical emoji set and skin-tone variants
+are omitted. The Rust converter strips Slack `::skin-tone-2` through
+`::skin-tone-6` suffixes before lookup, including paired-tone suffixes. Unknown
+and workspace-custom shortcodes are dropped from reactions, not fetched from
+Slack. Message text shortcodes stay literal; this table is for reactions only.
+The JSON uses ASCII escapes and sorted keys for deterministic review.
+
+## Licenses
+
+Both inputs are distributed under the following MIT permission grant. Retain
+both copyright notices with the generated data:
+
+Copyright (c) 2017-2019 Miles Johnson (emojibase-data)
+
+Copyright (c) 2019 Mu-An Chiou (unicode-emoji-json)
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
diff --git a/crates/slack_integration/data/emoji-shortcodes.json b/crates/slack_integration/data/emoji-shortcodes.json
new file mode 100644
index 0000000000..29d543f565
--- /dev/null
+++ b/crates/slack_integration/data/emoji-shortcodes.json
@@ -0,0 +1,2534 @@
+{
+ "100": "\ud83d\udcaf",
+ "1234": "\ud83d\udd22",
+ "+1": "\ud83d\udc4d",
+ "-1": "\ud83d\udc4e",
+ "1st_place_medal": "\ud83e\udd47",
+ "2nd_place_medal": "\ud83e\udd48",
+ "3rd_place_medal": "\ud83e\udd49",
+ "8ball": "\ud83c\udfb1",
+ "a": "\ud83c\udd70\ufe0f",
+ "ab": "\ud83c\udd8e",
+ "abacus": "\ud83e\uddee",
+ "abc": "\ud83d\udd24",
+ "abcd": "\ud83d\udd21",
+ "accept": "\ud83c\ude51",
+ "accordion": "\ud83e\ude97",
+ "adhesive_bandage": "\ud83e\ude79",
+ "admission_tickets": "\ud83c\udf9f\ufe0f",
+ "adult": "\ud83e\uddd1",
+ "aerial_tramway": "\ud83d\udea1",
+ "afghanistan": "\ud83c\udde6\ud83c\uddeb",
+ "airplane": "\u2708\ufe0f",
+ "airplane_arriving": "\ud83d\udeec",
+ "airplane_departure": "\ud83d\udeeb",
+ "aland_islands": "\ud83c\udde6\ud83c\uddfd",
+ "alarm_clock": "\u23f0",
+ "albania": "\ud83c\udde6\ud83c\uddf1",
+ "alembic": "\u2697\ufe0f",
+ "algeria": "\ud83c\udde9\ud83c\uddff",
+ "alien": "\ud83d\udc7d",
+ "ambulance": "\ud83d\ude91",
+ "american_samoa": "\ud83c\udde6\ud83c\uddf8",
+ "amphora": "\ud83c\udffa",
+ "anatomical_heart": "\ud83e\udec0",
+ "anchor": "\u2693",
+ "andorra": "\ud83c\udde6\ud83c\udde9",
+ "angel": "\ud83d\udc7c",
+ "anger": "\ud83d\udca2",
+ "angola": "\ud83c\udde6\ud83c\uddf4",
+ "angry": "\ud83d\ude20",
+ "anguilla": "\ud83c\udde6\ud83c\uddee",
+ "anguished": "\ud83d\ude27",
+ "ant": "\ud83d\udc1c",
+ "antarctica": "\ud83c\udde6\ud83c\uddf6",
+ "antigua_barbuda": "\ud83c\udde6\ud83c\uddec",
+ "apple": "\ud83c\udf4e",
+ "aquarius": "\u2652",
+ "argentina": "\ud83c\udde6\ud83c\uddf7",
+ "aries": "\u2648",
+ "armenia": "\ud83c\udde6\ud83c\uddf2",
+ "arrow_backward": "\u25c0\ufe0f",
+ "arrow_double_down": "\u23ec",
+ "arrow_double_up": "\u23eb",
+ "arrow_down": "\u2b07\ufe0f",
+ "arrow_down_small": "\ud83d\udd3d",
+ "arrow_forward": "\u25b6\ufe0f",
+ "arrow_heading_down": "\u2935\ufe0f",
+ "arrow_heading_up": "\u2934\ufe0f",
+ "arrow_left": "\u2b05\ufe0f",
+ "arrow_lower_left": "\u2199\ufe0f",
+ "arrow_lower_right": "\u2198\ufe0f",
+ "arrow_right": "\u27a1\ufe0f",
+ "arrow_right_hook": "\u21aa\ufe0f",
+ "arrow_up": "\u2b06\ufe0f",
+ "arrow_up_down": "\u2195\ufe0f",
+ "arrow_up_small": "\ud83d\udd3c",
+ "arrow_upper_left": "\u2196\ufe0f",
+ "arrow_upper_right": "\u2197\ufe0f",
+ "arrows_clockwise": "\ud83d\udd03",
+ "arrows_counterclockwise": "\ud83d\udd04",
+ "art": "\ud83c\udfa8",
+ "articulated_lorry": "\ud83d\ude9b",
+ "artificial_satellite": "\ud83d\udef0\ufe0f",
+ "artist": "\ud83e\uddd1\u200d\ud83c\udfa8",
+ "aruba": "\ud83c\udde6\ud83c\uddfc",
+ "ascension_island": "\ud83c\udde6\ud83c\udde8",
+ "asterisk": "*\ufe0f\u20e3",
+ "astonished": "\ud83d\ude32",
+ "astronaut": "\ud83e\uddd1\u200d\ud83d\ude80",
+ "athletic_shoe": "\ud83d\udc5f",
+ "atm": "\ud83c\udfe7",
+ "atom_symbol": "\u269b\ufe0f",
+ "australia": "\ud83c\udde6\ud83c\uddfa",
+ "austria": "\ud83c\udde6\ud83c\uddf9",
+ "auto_rickshaw": "\ud83d\udefa",
+ "avocado": "\ud83e\udd51",
+ "axe": "\ud83e\ude93",
+ "azerbaijan": "\ud83c\udde6\ud83c\uddff",
+ "b": "\ud83c\udd71\ufe0f",
+ "baby": "\ud83d\udc76",
+ "baby_bottle": "\ud83c\udf7c",
+ "baby_chick": "\ud83d\udc24",
+ "baby_symbol": "\ud83d\udebc",
+ "back": "\ud83d\udd19",
+ "bacon": "\ud83e\udd53",
+ "badger": "\ud83e\udda1",
+ "badminton": "\ud83c\udff8",
+ "badminton_racquet_and_shuttlecock": "\ud83c\udff8",
+ "bagel": "\ud83e\udd6f",
+ "baggage_claim": "\ud83d\udec4",
+ "baguette_bread": "\ud83e\udd56",
+ "bahamas": "\ud83c\udde7\ud83c\uddf8",
+ "bahrain": "\ud83c\udde7\ud83c\udded",
+ "balance_scale": "\u2696\ufe0f",
+ "bald_man": "\ud83d\udc68\u200d\ud83e\uddb2",
+ "bald_person": "\ud83e\uddd1\u200d\ud83e\uddb2",
+ "bald_woman": "\ud83d\udc69\u200d\ud83e\uddb2",
+ "ballet_shoes": "\ud83e\ude70",
+ "balloon": "\ud83c\udf88",
+ "ballot_box": "\ud83d\uddf3\ufe0f",
+ "ballot_box_with_ballot": "\ud83d\uddf3\ufe0f",
+ "ballot_box_with_check": "\u2611\ufe0f",
+ "bamboo": "\ud83c\udf8d",
+ "banana": "\ud83c\udf4c",
+ "bangbang": "\u203c\ufe0f",
+ "bangladesh": "\ud83c\udde7\ud83c\udde9",
+ "banjo": "\ud83e\ude95",
+ "bank": "\ud83c\udfe6",
+ "bar_chart": "\ud83d\udcca",
+ "barbados": "\ud83c\udde7\ud83c\udde7",
+ "barber": "\ud83d\udc88",
+ "barely_sunny": "\ud83c\udf25\ufe0f",
+ "baseball": "\u26be",
+ "basket": "\ud83e\uddfa",
+ "basketball": "\ud83c\udfc0",
+ "basketball_man": "\u26f9\ufe0f\u200d\u2642\ufe0f",
+ "basketball_woman": "\u26f9\ufe0f\u200d\u2640\ufe0f",
+ "bat": "\ud83e\udd87",
+ "bath": "\ud83d\udec0",
+ "bathtub": "\ud83d\udec1",
+ "battery": "\ud83d\udd0b",
+ "beach_umbrella": "\ud83c\udfd6\ufe0f",
+ "beach_with_umbrella": "\ud83c\udfd6\ufe0f",
+ "beans": "\ud83e\uded8",
+ "bear": "\ud83d\udc3b",
+ "bearded_person": "\ud83e\uddd4",
+ "beaver": "\ud83e\uddab",
+ "bed": "\ud83d\udecf\ufe0f",
+ "bee": "\ud83d\udc1d",
+ "beer": "\ud83c\udf7a",
+ "beers": "\ud83c\udf7b",
+ "beetle": "\ud83e\udeb2",
+ "beginner": "\ud83d\udd30",
+ "belarus": "\ud83c\udde7\ud83c\uddfe",
+ "belgium": "\ud83c\udde7\ud83c\uddea",
+ "belize": "\ud83c\udde7\ud83c\uddff",
+ "bell": "\ud83d\udd14",
+ "bell_pepper": "\ud83e\uded1",
+ "bellhop_bell": "\ud83d\udece\ufe0f",
+ "benin": "\ud83c\udde7\ud83c\uddef",
+ "bento": "\ud83c\udf71",
+ "bermuda": "\ud83c\udde7\ud83c\uddf2",
+ "beverage_box": "\ud83e\uddc3",
+ "bhutan": "\ud83c\udde7\ud83c\uddf9",
+ "bicyclist": "\ud83d\udeb4",
+ "bike": "\ud83d\udeb2",
+ "biking_man": "\ud83d\udeb4\u200d\u2642\ufe0f",
+ "biking_woman": "\ud83d\udeb4\u200d\u2640\ufe0f",
+ "bikini": "\ud83d\udc59",
+ "billed_cap": "\ud83e\udde2",
+ "biohazard": "\u2623\ufe0f",
+ "biohazard_sign": "\u2623\ufe0f",
+ "bird": "\ud83d\udc26",
+ "birthday": "\ud83c\udf82",
+ "bison": "\ud83e\uddac",
+ "biting_lip": "\ud83e\udee6",
+ "black_bird": "\ud83d\udc26\u200d\u2b1b",
+ "black_cat": "\ud83d\udc08\u200d\u2b1b",
+ "black_circle": "\u26ab",
+ "black_circle_for_record": "\u23fa\ufe0f",
+ "black_flag": "\ud83c\udff4",
+ "black_heart": "\ud83d\udda4",
+ "black_joker": "\ud83c\udccf",
+ "black_large_square": "\u2b1b",
+ "black_left_pointing_double_triangle_with_vertical_bar": "\u23ee\ufe0f",
+ "black_medium_small_square": "\u25fe",
+ "black_medium_square": "\u25fc\ufe0f",
+ "black_nib": "\u2712\ufe0f",
+ "black_right_pointing_double_triangle_with_vertical_bar": "\u23ed\ufe0f",
+ "black_right_pointing_triangle_with_double_vertical_bar": "\u23ef\ufe0f",
+ "black_small_square": "\u25aa\ufe0f",
+ "black_square_button": "\ud83d\udd32",
+ "black_square_for_stop": "\u23f9\ufe0f",
+ "blond-haired-man": "\ud83d\udc71\u200d\u2642\ufe0f",
+ "blond-haired-woman": "\ud83d\udc71\u200d\u2640\ufe0f",
+ "blond_haired_man": "\ud83d\udc71\u200d\u2642\ufe0f",
+ "blond_haired_person": "\ud83d\udc71",
+ "blond_haired_woman": "\ud83d\udc71\u200d\u2640\ufe0f",
+ "blonde_woman": "\ud83d\udc71\u200d\u2640\ufe0f",
+ "blossom": "\ud83c\udf3c",
+ "blowfish": "\ud83d\udc21",
+ "blue_book": "\ud83d\udcd8",
+ "blue_car": "\ud83d\ude99",
+ "blue_heart": "\ud83d\udc99",
+ "blue_square": "\ud83d\udfe6",
+ "blueberries": "\ud83e\uded0",
+ "blush": "\ud83d\ude0a",
+ "boar": "\ud83d\udc17",
+ "boat": "\u26f5",
+ "bolivia": "\ud83c\udde7\ud83c\uddf4",
+ "bomb": "\ud83d\udca3",
+ "bone": "\ud83e\uddb4",
+ "book": "\ud83d\udcd6",
+ "bookmark": "\ud83d\udd16",
+ "bookmark_tabs": "\ud83d\udcd1",
+ "books": "\ud83d\udcda",
+ "boom": "\ud83d\udca5",
+ "boomerang": "\ud83e\ude83",
+ "boot": "\ud83d\udc62",
+ "bosnia_herzegovina": "\ud83c\udde7\ud83c\udde6",
+ "botswana": "\ud83c\udde7\ud83c\uddfc",
+ "bouncing_ball_man": "\u26f9\ufe0f\u200d\u2642\ufe0f",
+ "bouncing_ball_person": "\u26f9\ufe0f",
+ "bouncing_ball_woman": "\u26f9\ufe0f\u200d\u2640\ufe0f",
+ "bouquet": "\ud83d\udc90",
+ "bouvet_island": "\ud83c\udde7\ud83c\uddfb",
+ "bow": "\ud83d\ude47",
+ "bow_and_arrow": "\ud83c\udff9",
+ "bowing_man": "\ud83d\ude47\u200d\u2642\ufe0f",
+ "bowing_woman": "\ud83d\ude47\u200d\u2640\ufe0f",
+ "bowl_with_spoon": "\ud83e\udd63",
+ "bowling": "\ud83c\udfb3",
+ "boxing_glove": "\ud83e\udd4a",
+ "boy": "\ud83d\udc66",
+ "brain": "\ud83e\udde0",
+ "brazil": "\ud83c\udde7\ud83c\uddf7",
+ "bread": "\ud83c\udf5e",
+ "breast-feeding": "\ud83e\udd31",
+ "breast_feeding": "\ud83e\udd31",
+ "bricks": "\ud83e\uddf1",
+ "bride_with_veil": "\ud83d\udc70",
+ "bridge_at_night": "\ud83c\udf09",
+ "briefcase": "\ud83d\udcbc",
+ "briefs": "\ud83e\ude72",
+ "british_indian_ocean_territory": "\ud83c\uddee\ud83c\uddf4",
+ "british_virgin_islands": "\ud83c\uddfb\ud83c\uddec",
+ "broccoli": "\ud83e\udd66",
+ "broken_chain": "\u26d3\ufe0f\u200d\ud83d\udca5",
+ "broken_heart": "\ud83d\udc94",
+ "broom": "\ud83e\uddf9",
+ "brown_circle": "\ud83d\udfe4",
+ "brown_heart": "\ud83e\udd0e",
+ "brown_mushroom": "\ud83c\udf44\u200d\ud83d\udfeb",
+ "brown_square": "\ud83d\udfeb",
+ "brunei": "\ud83c\udde7\ud83c\uddf3",
+ "bubble_tea": "\ud83e\uddcb",
+ "bubbles": "\ud83e\udee7",
+ "bucket": "\ud83e\udea3",
+ "bug": "\ud83d\udc1b",
+ "building_construction": "\ud83c\udfd7\ufe0f",
+ "bulb": "\ud83d\udca1",
+ "bulgaria": "\ud83c\udde7\ud83c\uddec",
+ "bullettrain_front": "\ud83d\ude85",
+ "bullettrain_side": "\ud83d\ude84",
+ "burkina_faso": "\ud83c\udde7\ud83c\uddeb",
+ "burrito": "\ud83c\udf2f",
+ "burundi": "\ud83c\udde7\ud83c\uddee",
+ "bus": "\ud83d\ude8c",
+ "business_suit_levitating": "\ud83d\udd74\ufe0f",
+ "busstop": "\ud83d\ude8f",
+ "bust_in_silhouette": "\ud83d\udc64",
+ "busts_in_silhouette": "\ud83d\udc65",
+ "butter": "\ud83e\uddc8",
+ "butterfly": "\ud83e\udd8b",
+ "cactus": "\ud83c\udf35",
+ "cake": "\ud83c\udf70",
+ "calendar": "\ud83d\udcc6",
+ "call_me_hand": "\ud83e\udd19",
+ "calling": "\ud83d\udcf2",
+ "cambodia": "\ud83c\uddf0\ud83c\udded",
+ "camel": "\ud83d\udc2b",
+ "camera": "\ud83d\udcf7",
+ "camera_flash": "\ud83d\udcf8",
+ "camera_with_flash": "\ud83d\udcf8",
+ "cameroon": "\ud83c\udde8\ud83c\uddf2",
+ "camping": "\ud83c\udfd5\ufe0f",
+ "canada": "\ud83c\udde8\ud83c\udde6",
+ "canary_islands": "\ud83c\uddee\ud83c\udde8",
+ "cancer": "\u264b",
+ "candle": "\ud83d\udd6f\ufe0f",
+ "candy": "\ud83c\udf6c",
+ "canned_food": "\ud83e\udd6b",
+ "canoe": "\ud83d\udef6",
+ "cape_verde": "\ud83c\udde8\ud83c\uddfb",
+ "capital_abcd": "\ud83d\udd20",
+ "capricorn": "\u2651",
+ "car": "\ud83d\ude97",
+ "card_file_box": "\ud83d\uddc3\ufe0f",
+ "card_index": "\ud83d\udcc7",
+ "card_index_dividers": "\ud83d\uddc2\ufe0f",
+ "caribbean_netherlands": "\ud83c\udde7\ud83c\uddf6",
+ "carousel_horse": "\ud83c\udfa0",
+ "carpentry_saw": "\ud83e\ude9a",
+ "carrot": "\ud83e\udd55",
+ "cartwheeling": "\ud83e\udd38",
+ "cat": "\ud83d\udc31",
+ "cat2": "\ud83d\udc08",
+ "cayman_islands": "\ud83c\uddf0\ud83c\uddfe",
+ "cd": "\ud83d\udcbf",
+ "central_african_republic": "\ud83c\udde8\ud83c\uddeb",
+ "ceuta_melilla": "\ud83c\uddea\ud83c\udde6",
+ "chad": "\ud83c\uddf9\ud83c\udde9",
+ "chains": "\u26d3\ufe0f",
+ "chair": "\ud83e\ude91",
+ "champagne": "\ud83c\udf7e",
+ "chart": "\ud83d\udcb9",
+ "chart_with_downwards_trend": "\ud83d\udcc9",
+ "chart_with_upwards_trend": "\ud83d\udcc8",
+ "checkered_flag": "\ud83c\udfc1",
+ "cheese": "\ud83e\uddc0",
+ "cheese_wedge": "\ud83e\uddc0",
+ "cherries": "\ud83c\udf52",
+ "cherry_blossom": "\ud83c\udf38",
+ "chess_pawn": "\u265f\ufe0f",
+ "chestnut": "\ud83c\udf30",
+ "chicken": "\ud83d\udc14",
+ "child": "\ud83e\uddd2",
+ "children_crossing": "\ud83d\udeb8",
+ "chile": "\ud83c\udde8\ud83c\uddf1",
+ "chipmunk": "\ud83d\udc3f\ufe0f",
+ "chocolate_bar": "\ud83c\udf6b",
+ "chopsticks": "\ud83e\udd62",
+ "christmas_island": "\ud83c\udde8\ud83c\uddfd",
+ "christmas_tree": "\ud83c\udf84",
+ "church": "\u26ea",
+ "cinema": "\ud83c\udfa6",
+ "circus_tent": "\ud83c\udfaa",
+ "city_sunrise": "\ud83c\udf07",
+ "city_sunset": "\ud83c\udf06",
+ "cityscape": "\ud83c\udfd9\ufe0f",
+ "cl": "\ud83c\udd91",
+ "clamp": "\ud83d\udddc\ufe0f",
+ "clap": "\ud83d\udc4f",
+ "clapper": "\ud83c\udfac",
+ "classical_building": "\ud83c\udfdb\ufe0f",
+ "climbing": "\ud83e\uddd7",
+ "climbing_man": "\ud83e\uddd7\u200d\u2642\ufe0f",
+ "climbing_woman": "\ud83e\uddd7\u200d\u2640\ufe0f",
+ "clinking_glasses": "\ud83e\udd42",
+ "clipboard": "\ud83d\udccb",
+ "clipperton_island": "\ud83c\udde8\ud83c\uddf5",
+ "clock1": "\ud83d\udd50",
+ "clock10": "\ud83d\udd59",
+ "clock1030": "\ud83d\udd65",
+ "clock11": "\ud83d\udd5a",
+ "clock1130": "\ud83d\udd66",
+ "clock12": "\ud83d\udd5b",
+ "clock1230": "\ud83d\udd67",
+ "clock130": "\ud83d\udd5c",
+ "clock2": "\ud83d\udd51",
+ "clock230": "\ud83d\udd5d",
+ "clock3": "\ud83d\udd52",
+ "clock330": "\ud83d\udd5e",
+ "clock4": "\ud83d\udd53",
+ "clock430": "\ud83d\udd5f",
+ "clock5": "\ud83d\udd54",
+ "clock530": "\ud83d\udd60",
+ "clock6": "\ud83d\udd55",
+ "clock630": "\ud83d\udd61",
+ "clock7": "\ud83d\udd56",
+ "clock730": "\ud83d\udd62",
+ "clock8": "\ud83d\udd57",
+ "clock830": "\ud83d\udd63",
+ "clock9": "\ud83d\udd58",
+ "clock930": "\ud83d\udd64",
+ "closed_book": "\ud83d\udcd5",
+ "closed_lock_with_key": "\ud83d\udd10",
+ "closed_umbrella": "\ud83c\udf02",
+ "cloud": "\u2601\ufe0f",
+ "cloud_with_lightning": "\ud83c\udf29\ufe0f",
+ "cloud_with_lightning_and_rain": "\u26c8\ufe0f",
+ "cloud_with_rain": "\ud83c\udf27\ufe0f",
+ "cloud_with_snow": "\ud83c\udf28\ufe0f",
+ "clown_face": "\ud83e\udd21",
+ "clubs": "\u2663\ufe0f",
+ "cn": "\ud83c\udde8\ud83c\uddf3",
+ "coat": "\ud83e\udde5",
+ "cockroach": "\ud83e\udeb3",
+ "cocktail": "\ud83c\udf78",
+ "coconut": "\ud83e\udd65",
+ "cocos_islands": "\ud83c\udde8\ud83c\udde8",
+ "coffee": "\u2615",
+ "coffin": "\u26b0\ufe0f",
+ "coin": "\ud83e\ude99",
+ "cold_face": "\ud83e\udd76",
+ "cold_sweat": "\ud83d\ude30",
+ "collision": "\ud83d\udca5",
+ "colombia": "\ud83c\udde8\ud83c\uddf4",
+ "comet": "\u2604\ufe0f",
+ "comoros": "\ud83c\uddf0\ud83c\uddf2",
+ "compass": "\ud83e\udded",
+ "compression": "\ud83d\udddc\ufe0f",
+ "computer": "\ud83d\udcbb",
+ "computer_mouse": "\ud83d\uddb1\ufe0f",
+ "confetti_ball": "\ud83c\udf8a",
+ "confounded": "\ud83d\ude16",
+ "confused": "\ud83d\ude15",
+ "congo_brazzaville": "\ud83c\udde8\ud83c\uddec",
+ "congo_kinshasa": "\ud83c\udde8\ud83c\udde9",
+ "congratulations": "\u3297\ufe0f",
+ "construction": "\ud83d\udea7",
+ "construction_worker": "\ud83d\udc77",
+ "construction_worker_man": "\ud83d\udc77\u200d\u2642\ufe0f",
+ "construction_worker_woman": "\ud83d\udc77\u200d\u2640\ufe0f",
+ "control_knobs": "\ud83c\udf9b\ufe0f",
+ "convenience_store": "\ud83c\udfea",
+ "cook": "\ud83e\uddd1\u200d\ud83c\udf73",
+ "cook_islands": "\ud83c\udde8\ud83c\uddf0",
+ "cookie": "\ud83c\udf6a",
+ "cooking": "\ud83c\udf73",
+ "cool": "\ud83c\udd92",
+ "cop": "\ud83d\udc6e",
+ "copyright": "\u00a9\ufe0f",
+ "coral": "\ud83e\udeb8",
+ "corn": "\ud83c\udf3d",
+ "costa_rica": "\ud83c\udde8\ud83c\uddf7",
+ "cote_divoire": "\ud83c\udde8\ud83c\uddee",
+ "couch_and_lamp": "\ud83d\udecb\ufe0f",
+ "couple": "\ud83d\udc6b",
+ "couple_with_heart": "\ud83d\udc91",
+ "couple_with_heart_man_man": "\ud83d\udc68\u200d\u2764\ufe0f\u200d\ud83d\udc68",
+ "couple_with_heart_woman_man": "\ud83d\udc69\u200d\u2764\ufe0f\u200d\ud83d\udc68",
+ "couple_with_heart_woman_woman": "\ud83d\udc69\u200d\u2764\ufe0f\u200d\ud83d\udc69",
+ "couplekiss": "\ud83d\udc8f",
+ "couplekiss_man_man": "\ud83d\udc68\u200d\u2764\ufe0f\u200d\ud83d\udc8b\u200d\ud83d\udc68",
+ "couplekiss_man_woman": "\ud83d\udc69\u200d\u2764\ufe0f\u200d\ud83d\udc8b\u200d\ud83d\udc68",
+ "couplekiss_woman_woman": "\ud83d\udc69\u200d\u2764\ufe0f\u200d\ud83d\udc8b\u200d\ud83d\udc69",
+ "cow": "\ud83d\udc2e",
+ "cow2": "\ud83d\udc04",
+ "cowboy_hat_face": "\ud83e\udd20",
+ "crab": "\ud83e\udd80",
+ "crayon": "\ud83d\udd8d\ufe0f",
+ "credit_card": "\ud83d\udcb3",
+ "crescent_moon": "\ud83c\udf19",
+ "cricket": "\ud83e\udd97",
+ "cricket_bat_and_ball": "\ud83c\udfcf",
+ "cricket_game": "\ud83c\udfcf",
+ "croatia": "\ud83c\udded\ud83c\uddf7",
+ "crocodile": "\ud83d\udc0a",
+ "croissant": "\ud83e\udd50",
+ "crossed_fingers": "\ud83e\udd1e",
+ "crossed_flags": "\ud83c\udf8c",
+ "crossed_swords": "\u2694\ufe0f",
+ "crown": "\ud83d\udc51",
+ "crutch": "\ud83e\ude7c",
+ "cry": "\ud83d\ude22",
+ "crying_cat_face": "\ud83d\ude3f",
+ "crystal_ball": "\ud83d\udd2e",
+ "cuba": "\ud83c\udde8\ud83c\uddfa",
+ "cucumber": "\ud83e\udd52",
+ "cup_with_straw": "\ud83e\udd64",
+ "cupcake": "\ud83e\uddc1",
+ "cupid": "\ud83d\udc98",
+ "curacao": "\ud83c\udde8\ud83c\uddfc",
+ "curling_stone": "\ud83e\udd4c",
+ "curly_haired_man": "\ud83d\udc68\u200d\ud83e\uddb1",
+ "curly_haired_person": "\ud83e\uddd1\u200d\ud83e\uddb1",
+ "curly_haired_woman": "\ud83d\udc69\u200d\ud83e\uddb1",
+ "curly_loop": "\u27b0",
+ "currency_exchange": "\ud83d\udcb1",
+ "curry": "\ud83c\udf5b",
+ "cursing_face": "\ud83e\udd2c",
+ "custard": "\ud83c\udf6e",
+ "customs": "\ud83d\udec3",
+ "cut_of_meat": "\ud83e\udd69",
+ "cyclone": "\ud83c\udf00",
+ "cyprus": "\ud83c\udde8\ud83c\uddfe",
+ "czech_republic": "\ud83c\udde8\ud83c\uddff",
+ "dagger": "\ud83d\udde1\ufe0f",
+ "dagger_knife": "\ud83d\udde1\ufe0f",
+ "dancer": "\ud83d\udc83",
+ "dancers": "\ud83d\udc6f",
+ "dancing_men": "\ud83d\udc6f\u200d\u2642\ufe0f",
+ "dancing_women": "\ud83d\udc6f\u200d\u2640\ufe0f",
+ "dango": "\ud83c\udf61",
+ "dark_sunglasses": "\ud83d\udd76\ufe0f",
+ "dart": "\ud83c\udfaf",
+ "dash": "\ud83d\udca8",
+ "date": "\ud83d\udcc5",
+ "de": "\ud83c\udde9\ud83c\uddea",
+ "deaf_man": "\ud83e\uddcf\u200d\u2642\ufe0f",
+ "deaf_person": "\ud83e\uddcf",
+ "deaf_woman": "\ud83e\uddcf\u200d\u2640\ufe0f",
+ "deciduous_tree": "\ud83c\udf33",
+ "deer": "\ud83e\udd8c",
+ "denmark": "\ud83c\udde9\ud83c\uddf0",
+ "department_store": "\ud83c\udfec",
+ "derelict_house": "\ud83c\udfda\ufe0f",
+ "derelict_house_building": "\ud83c\udfda\ufe0f",
+ "desert": "\ud83c\udfdc\ufe0f",
+ "desert_island": "\ud83c\udfdd\ufe0f",
+ "desktop_computer": "\ud83d\udda5\ufe0f",
+ "detective": "\ud83d\udd75\ufe0f",
+ "diamond_shape_with_a_dot_inside": "\ud83d\udca0",
+ "diamonds": "\u2666\ufe0f",
+ "diego_garcia": "\ud83c\udde9\ud83c\uddec",
+ "disappointed": "\ud83d\ude1e",
+ "disappointed_relieved": "\ud83d\ude25",
+ "disguised_face": "\ud83e\udd78",
+ "diving_mask": "\ud83e\udd3f",
+ "diya_lamp": "\ud83e\ude94",
+ "dizzy": "\ud83d\udcab",
+ "dizzy_face": "\ud83d\ude35",
+ "djibouti": "\ud83c\udde9\ud83c\uddef",
+ "dna": "\ud83e\uddec",
+ "do_not_litter": "\ud83d\udeaf",
+ "dodo": "\ud83e\udda4",
+ "dog": "\ud83d\udc36",
+ "dog2": "\ud83d\udc15",
+ "dollar": "\ud83d\udcb5",
+ "dolls": "\ud83c\udf8e",
+ "dolphin": "\ud83d\udc2c",
+ "dominica": "\ud83c\udde9\ud83c\uddf2",
+ "dominican_republic": "\ud83c\udde9\ud83c\uddf4",
+ "donkey": "\ud83e\udecf",
+ "door": "\ud83d\udeaa",
+ "dotted_line_face": "\ud83e\udee5",
+ "double_vertical_bar": "\u23f8\ufe0f",
+ "doughnut": "\ud83c\udf69",
+ "dove": "\ud83d\udd4a\ufe0f",
+ "dove_of_peace": "\ud83d\udd4a\ufe0f",
+ "dragon": "\ud83d\udc09",
+ "dragon_face": "\ud83d\udc32",
+ "dress": "\ud83d\udc57",
+ "dromedary_camel": "\ud83d\udc2a",
+ "drooling_face": "\ud83e\udd24",
+ "drop_of_blood": "\ud83e\ude78",
+ "droplet": "\ud83d\udca7",
+ "drum": "\ud83e\udd41",
+ "drum_with_drumsticks": "\ud83e\udd41",
+ "duck": "\ud83e\udd86",
+ "dumpling": "\ud83e\udd5f",
+ "dvd": "\ud83d\udcc0",
+ "e-mail": "\ud83d\udce7",
+ "eagle": "\ud83e\udd85",
+ "ear": "\ud83d\udc42",
+ "ear_of_rice": "\ud83c\udf3e",
+ "ear_with_hearing_aid": "\ud83e\uddbb",
+ "earth_africa": "\ud83c\udf0d",
+ "earth_americas": "\ud83c\udf0e",
+ "earth_asia": "\ud83c\udf0f",
+ "ecuador": "\ud83c\uddea\ud83c\udde8",
+ "egg": "\ud83e\udd5a",
+ "eggplant": "\ud83c\udf46",
+ "egypt": "\ud83c\uddea\ud83c\uddec",
+ "eight": "8\ufe0f\u20e3",
+ "eight_pointed_black_star": "\u2734\ufe0f",
+ "eight_spoked_asterisk": "\u2733\ufe0f",
+ "eject": "\u23cf\ufe0f",
+ "eject_button": "\u23cf\ufe0f",
+ "el_salvador": "\ud83c\uddf8\ud83c\uddfb",
+ "electric_plug": "\ud83d\udd0c",
+ "elephant": "\ud83d\udc18",
+ "elevator": "\ud83d\uded7",
+ "elf": "\ud83e\udddd",
+ "elf_man": "\ud83e\udddd\u200d\u2642\ufe0f",
+ "elf_woman": "\ud83e\udddd\u200d\u2640\ufe0f",
+ "email": "\u2709\ufe0f",
+ "empty_nest": "\ud83e\udeb9",
+ "end": "\ud83d\udd1a",
+ "england": "\ud83c\udff4\udb40\udc67\udb40\udc62\udb40\udc65\udb40\udc6e\udb40\udc67\udb40\udc7f",
+ "envelope": "\u2709\ufe0f",
+ "envelope_with_arrow": "\ud83d\udce9",
+ "equatorial_guinea": "\ud83c\uddec\ud83c\uddf6",
+ "eritrea": "\ud83c\uddea\ud83c\uddf7",
+ "es": "\ud83c\uddea\ud83c\uddf8",
+ "estonia": "\ud83c\uddea\ud83c\uddea",
+ "ethiopia": "\ud83c\uddea\ud83c\uddf9",
+ "eu": "\ud83c\uddea\ud83c\uddfa",
+ "euro": "\ud83d\udcb6",
+ "european_castle": "\ud83c\udff0",
+ "european_post_office": "\ud83c\udfe4",
+ "european_union": "\ud83c\uddea\ud83c\uddfa",
+ "evergreen_tree": "\ud83c\udf32",
+ "exclamation": "\u2757",
+ "exploding_head": "\ud83e\udd2f",
+ "expressionless": "\ud83d\ude11",
+ "eye": "\ud83d\udc41\ufe0f",
+ "eye-in-speech-bubble": "\ud83d\udc41\ufe0f\u200d\ud83d\udde8\ufe0f",
+ "eye_speech_bubble": "\ud83d\udc41\ufe0f\u200d\ud83d\udde8\ufe0f",
+ "eyeglasses": "\ud83d\udc53",
+ "eyes": "\ud83d\udc40",
+ "face_exhaling": "\ud83d\ude2e\u200d\ud83d\udca8",
+ "face_holding_back_tears": "\ud83e\udd79",
+ "face_in_clouds": "\ud83d\ude36\u200d\ud83c\udf2b\ufe0f",
+ "face_palm": "\ud83e\udd26",
+ "face_vomiting": "\ud83e\udd2e",
+ "face_with_bags_under_eyes": "\ud83e\udee9",
+ "face_with_cowboy_hat": "\ud83e\udd20",
+ "face_with_diagonal_mouth": "\ud83e\udee4",
+ "face_with_finger_covering_closed_lips": "\ud83e\udd2b",
+ "face_with_hand_over_mouth": "\ud83e\udd2d",
+ "face_with_head_bandage": "\ud83e\udd15",
+ "face_with_monocle": "\ud83e\uddd0",
+ "face_with_one_eyebrow_raised": "\ud83e\udd28",
+ "face_with_open_eyes_and_hand_over_mouth": "\ud83e\udee2",
+ "face_with_open_mouth_vomiting": "\ud83e\udd2e",
+ "face_with_peeking_eye": "\ud83e\udee3",
+ "face_with_raised_eyebrow": "\ud83e\udd28",
+ "face_with_rolling_eyes": "\ud83d\ude44",
+ "face_with_spiral_eyes": "\ud83d\ude35\u200d\ud83d\udcab",
+ "face_with_symbols_on_mouth": "\ud83e\udd2c",
+ "face_with_thermometer": "\ud83e\udd12",
+ "facepalm": "\ud83e\udd26",
+ "facepunch": "\ud83d\udc4a",
+ "factory": "\ud83c\udfed",
+ "factory_worker": "\ud83e\uddd1\u200d\ud83c\udfed",
+ "fairy": "\ud83e\uddda",
+ "fairy_man": "\ud83e\uddda\u200d\u2642\ufe0f",
+ "fairy_woman": "\ud83e\uddda\u200d\u2640\ufe0f",
+ "falafel": "\ud83e\uddc6",
+ "falkland_islands": "\ud83c\uddeb\ud83c\uddf0",
+ "fallen_leaf": "\ud83c\udf42",
+ "family": "\ud83d\udc6a",
+ "family_adult_adult_child": "\ud83e\uddd1\u200d\ud83e\uddd1\u200d\ud83e\uddd2",
+ "family_adult_adult_child_child": "\ud83e\uddd1\u200d\ud83e\uddd1\u200d\ud83e\uddd2\u200d\ud83e\uddd2",
+ "family_adult_child": "\ud83e\uddd1\u200d\ud83e\uddd2",
+ "family_adult_child_child": "\ud83e\uddd1\u200d\ud83e\uddd2\u200d\ud83e\uddd2",
+ "family_man_boy": "\ud83d\udc68\u200d\ud83d\udc66",
+ "family_man_boy_boy": "\ud83d\udc68\u200d\ud83d\udc66\u200d\ud83d\udc66",
+ "family_man_girl": "\ud83d\udc68\u200d\ud83d\udc67",
+ "family_man_girl_boy": "\ud83d\udc68\u200d\ud83d\udc67\u200d\ud83d\udc66",
+ "family_man_girl_girl": "\ud83d\udc68\u200d\ud83d\udc67\u200d\ud83d\udc67",
+ "family_man_man_boy": "\ud83d\udc68\u200d\ud83d\udc68\u200d\ud83d\udc66",
+ "family_man_man_boy_boy": "\ud83d\udc68\u200d\ud83d\udc68\u200d\ud83d\udc66\u200d\ud83d\udc66",
+ "family_man_man_girl": "\ud83d\udc68\u200d\ud83d\udc68\u200d\ud83d\udc67",
+ "family_man_man_girl_boy": "\ud83d\udc68\u200d\ud83d\udc68\u200d\ud83d\udc67\u200d\ud83d\udc66",
+ "family_man_man_girl_girl": "\ud83d\udc68\u200d\ud83d\udc68\u200d\ud83d\udc67\u200d\ud83d\udc67",
+ "family_man_woman_boy": "\ud83d\udc68\u200d\ud83d\udc69\u200d\ud83d\udc66",
+ "family_man_woman_boy_boy": "\ud83d\udc68\u200d\ud83d\udc69\u200d\ud83d\udc66\u200d\ud83d\udc66",
+ "family_man_woman_girl": "\ud83d\udc68\u200d\ud83d\udc69\u200d\ud83d\udc67",
+ "family_man_woman_girl_boy": "\ud83d\udc68\u200d\ud83d\udc69\u200d\ud83d\udc67\u200d\ud83d\udc66",
+ "family_man_woman_girl_girl": "\ud83d\udc68\u200d\ud83d\udc69\u200d\ud83d\udc67\u200d\ud83d\udc67",
+ "family_woman_boy": "\ud83d\udc69\u200d\ud83d\udc66",
+ "family_woman_boy_boy": "\ud83d\udc69\u200d\ud83d\udc66\u200d\ud83d\udc66",
+ "family_woman_girl": "\ud83d\udc69\u200d\ud83d\udc67",
+ "family_woman_girl_boy": "\ud83d\udc69\u200d\ud83d\udc67\u200d\ud83d\udc66",
+ "family_woman_girl_girl": "\ud83d\udc69\u200d\ud83d\udc67\u200d\ud83d\udc67",
+ "family_woman_woman_boy": "\ud83d\udc69\u200d\ud83d\udc69\u200d\ud83d\udc66",
+ "family_woman_woman_boy_boy": "\ud83d\udc69\u200d\ud83d\udc69\u200d\ud83d\udc66\u200d\ud83d\udc66",
+ "family_woman_woman_girl": "\ud83d\udc69\u200d\ud83d\udc69\u200d\ud83d\udc67",
+ "family_woman_woman_girl_boy": "\ud83d\udc69\u200d\ud83d\udc69\u200d\ud83d\udc67\u200d\ud83d\udc66",
+ "family_woman_woman_girl_girl": "\ud83d\udc69\u200d\ud83d\udc69\u200d\ud83d\udc67\u200d\ud83d\udc67",
+ "farmer": "\ud83e\uddd1\u200d\ud83c\udf3e",
+ "faroe_islands": "\ud83c\uddeb\ud83c\uddf4",
+ "fast_forward": "\u23e9",
+ "fax": "\ud83d\udce0",
+ "fearful": "\ud83d\ude28",
+ "feather": "\ud83e\udeb6",
+ "feet": "\ud83d\udc3e",
+ "female-artist": "\ud83d\udc69\u200d\ud83c\udfa8",
+ "female-astronaut": "\ud83d\udc69\u200d\ud83d\ude80",
+ "female-construction-worker": "\ud83d\udc77\u200d\u2640\ufe0f",
+ "female-cook": "\ud83d\udc69\u200d\ud83c\udf73",
+ "female-detective": "\ud83d\udd75\ufe0f\u200d\u2640\ufe0f",
+ "female-doctor": "\ud83d\udc69\u200d\u2695\ufe0f",
+ "female-factory-worker": "\ud83d\udc69\u200d\ud83c\udfed",
+ "female-farmer": "\ud83d\udc69\u200d\ud83c\udf3e",
+ "female-firefighter": "\ud83d\udc69\u200d\ud83d\ude92",
+ "female-guard": "\ud83d\udc82\u200d\u2640\ufe0f",
+ "female-judge": "\ud83d\udc69\u200d\u2696\ufe0f",
+ "female-mechanic": "\ud83d\udc69\u200d\ud83d\udd27",
+ "female-office-worker": "\ud83d\udc69\u200d\ud83d\udcbc",
+ "female-pilot": "\ud83d\udc69\u200d\u2708\ufe0f",
+ "female-police-officer": "\ud83d\udc6e\u200d\u2640\ufe0f",
+ "female-scientist": "\ud83d\udc69\u200d\ud83d\udd2c",
+ "female-singer": "\ud83d\udc69\u200d\ud83c\udfa4",
+ "female-student": "\ud83d\udc69\u200d\ud83c\udf93",
+ "female-teacher": "\ud83d\udc69\u200d\ud83c\udfeb",
+ "female-technologist": "\ud83d\udc69\u200d\ud83d\udcbb",
+ "female_detective": "\ud83d\udd75\ufe0f\u200d\u2640\ufe0f",
+ "female_elf": "\ud83e\udddd\u200d\u2640\ufe0f",
+ "female_fairy": "\ud83e\uddda\u200d\u2640\ufe0f",
+ "female_genie": "\ud83e\uddde\u200d\u2640\ufe0f",
+ "female_mage": "\ud83e\uddd9\u200d\u2640\ufe0f",
+ "female_sign": "\u2640\ufe0f",
+ "female_superhero": "\ud83e\uddb8\u200d\u2640\ufe0f",
+ "female_supervillain": "\ud83e\uddb9\u200d\u2640\ufe0f",
+ "female_vampire": "\ud83e\udddb\u200d\u2640\ufe0f",
+ "female_zombie": "\ud83e\udddf\u200d\u2640\ufe0f",
+ "fencer": "\ud83e\udd3a",
+ "ferris_wheel": "\ud83c\udfa1",
+ "ferry": "\u26f4\ufe0f",
+ "field_hockey": "\ud83c\udfd1",
+ "field_hockey_stick_and_ball": "\ud83c\udfd1",
+ "fiji": "\ud83c\uddeb\ud83c\uddef",
+ "file_cabinet": "\ud83d\uddc4\ufe0f",
+ "file_folder": "\ud83d\udcc1",
+ "film_frames": "\ud83c\udf9e\ufe0f",
+ "film_projector": "\ud83d\udcfd\ufe0f",
+ "film_strip": "\ud83c\udf9e\ufe0f",
+ "fingerprint": "\ud83e\udec6",
+ "finland": "\ud83c\uddeb\ud83c\uddee",
+ "fire": "\ud83d\udd25",
+ "fire_engine": "\ud83d\ude92",
+ "fire_extinguisher": "\ud83e\uddef",
+ "firecracker": "\ud83e\udde8",
+ "firefighter": "\ud83e\uddd1\u200d\ud83d\ude92",
+ "fireworks": "\ud83c\udf86",
+ "first_place_medal": "\ud83e\udd47",
+ "first_quarter_moon": "\ud83c\udf13",
+ "first_quarter_moon_with_face": "\ud83c\udf1b",
+ "fish": "\ud83d\udc1f",
+ "fish_cake": "\ud83c\udf65",
+ "fishing_pole_and_fish": "\ud83c\udfa3",
+ "fist": "\u270a",
+ "fist_left": "\ud83e\udd1b",
+ "fist_oncoming": "\ud83d\udc4a",
+ "fist_raised": "\u270a",
+ "fist_right": "\ud83e\udd1c",
+ "five": "5\ufe0f\u20e3",
+ "flag-ac": "\ud83c\udde6\ud83c\udde8",
+ "flag-ad": "\ud83c\udde6\ud83c\udde9",
+ "flag-ae": "\ud83c\udde6\ud83c\uddea",
+ "flag-af": "\ud83c\udde6\ud83c\uddeb",
+ "flag-ag": "\ud83c\udde6\ud83c\uddec",
+ "flag-ai": "\ud83c\udde6\ud83c\uddee",
+ "flag-al": "\ud83c\udde6\ud83c\uddf1",
+ "flag-am": "\ud83c\udde6\ud83c\uddf2",
+ "flag-ao": "\ud83c\udde6\ud83c\uddf4",
+ "flag-aq": "\ud83c\udde6\ud83c\uddf6",
+ "flag-ar": "\ud83c\udde6\ud83c\uddf7",
+ "flag-as": "\ud83c\udde6\ud83c\uddf8",
+ "flag-at": "\ud83c\udde6\ud83c\uddf9",
+ "flag-au": "\ud83c\udde6\ud83c\uddfa",
+ "flag-aw": "\ud83c\udde6\ud83c\uddfc",
+ "flag-ax": "\ud83c\udde6\ud83c\uddfd",
+ "flag-az": "\ud83c\udde6\ud83c\uddff",
+ "flag-ba": "\ud83c\udde7\ud83c\udde6",
+ "flag-bb": "\ud83c\udde7\ud83c\udde7",
+ "flag-bd": "\ud83c\udde7\ud83c\udde9",
+ "flag-be": "\ud83c\udde7\ud83c\uddea",
+ "flag-bf": "\ud83c\udde7\ud83c\uddeb",
+ "flag-bg": "\ud83c\udde7\ud83c\uddec",
+ "flag-bh": "\ud83c\udde7\ud83c\udded",
+ "flag-bi": "\ud83c\udde7\ud83c\uddee",
+ "flag-bj": "\ud83c\udde7\ud83c\uddef",
+ "flag-bl": "\ud83c\udde7\ud83c\uddf1",
+ "flag-bm": "\ud83c\udde7\ud83c\uddf2",
+ "flag-bn": "\ud83c\udde7\ud83c\uddf3",
+ "flag-bo": "\ud83c\udde7\ud83c\uddf4",
+ "flag-bq": "\ud83c\udde7\ud83c\uddf6",
+ "flag-br": "\ud83c\udde7\ud83c\uddf7",
+ "flag-bs": "\ud83c\udde7\ud83c\uddf8",
+ "flag-bt": "\ud83c\udde7\ud83c\uddf9",
+ "flag-bv": "\ud83c\udde7\ud83c\uddfb",
+ "flag-bw": "\ud83c\udde7\ud83c\uddfc",
+ "flag-by": "\ud83c\udde7\ud83c\uddfe",
+ "flag-bz": "\ud83c\udde7\ud83c\uddff",
+ "flag-ca": "\ud83c\udde8\ud83c\udde6",
+ "flag-cc": "\ud83c\udde8\ud83c\udde8",
+ "flag-cd": "\ud83c\udde8\ud83c\udde9",
+ "flag-cf": "\ud83c\udde8\ud83c\uddeb",
+ "flag-cg": "\ud83c\udde8\ud83c\uddec",
+ "flag-ch": "\ud83c\udde8\ud83c\udded",
+ "flag-ci": "\ud83c\udde8\ud83c\uddee",
+ "flag-ck": "\ud83c\udde8\ud83c\uddf0",
+ "flag-cl": "\ud83c\udde8\ud83c\uddf1",
+ "flag-cm": "\ud83c\udde8\ud83c\uddf2",
+ "flag-cn": "\ud83c\udde8\ud83c\uddf3",
+ "flag-co": "\ud83c\udde8\ud83c\uddf4",
+ "flag-cp": "\ud83c\udde8\ud83c\uddf5",
+ "flag-cr": "\ud83c\udde8\ud83c\uddf7",
+ "flag-cu": "\ud83c\udde8\ud83c\uddfa",
+ "flag-cv": "\ud83c\udde8\ud83c\uddfb",
+ "flag-cw": "\ud83c\udde8\ud83c\uddfc",
+ "flag-cx": "\ud83c\udde8\ud83c\uddfd",
+ "flag-cy": "\ud83c\udde8\ud83c\uddfe",
+ "flag-cz": "\ud83c\udde8\ud83c\uddff",
+ "flag-de": "\ud83c\udde9\ud83c\uddea",
+ "flag-dg": "\ud83c\udde9\ud83c\uddec",
+ "flag-dj": "\ud83c\udde9\ud83c\uddef",
+ "flag-dk": "\ud83c\udde9\ud83c\uddf0",
+ "flag-dm": "\ud83c\udde9\ud83c\uddf2",
+ "flag-do": "\ud83c\udde9\ud83c\uddf4",
+ "flag-dz": "\ud83c\udde9\ud83c\uddff",
+ "flag-ea": "\ud83c\uddea\ud83c\udde6",
+ "flag-ec": "\ud83c\uddea\ud83c\udde8",
+ "flag-ee": "\ud83c\uddea\ud83c\uddea",
+ "flag-eg": "\ud83c\uddea\ud83c\uddec",
+ "flag-eh": "\ud83c\uddea\ud83c\udded",
+ "flag-england": "\ud83c\udff4\udb40\udc67\udb40\udc62\udb40\udc65\udb40\udc6e\udb40\udc67\udb40\udc7f",
+ "flag-er": "\ud83c\uddea\ud83c\uddf7",
+ "flag-es": "\ud83c\uddea\ud83c\uddf8",
+ "flag-et": "\ud83c\uddea\ud83c\uddf9",
+ "flag-eu": "\ud83c\uddea\ud83c\uddfa",
+ "flag-fi": "\ud83c\uddeb\ud83c\uddee",
+ "flag-fj": "\ud83c\uddeb\ud83c\uddef",
+ "flag-fk": "\ud83c\uddeb\ud83c\uddf0",
+ "flag-fm": "\ud83c\uddeb\ud83c\uddf2",
+ "flag-fo": "\ud83c\uddeb\ud83c\uddf4",
+ "flag-fr": "\ud83c\uddeb\ud83c\uddf7",
+ "flag-ga": "\ud83c\uddec\ud83c\udde6",
+ "flag-gb": "\ud83c\uddec\ud83c\udde7",
+ "flag-gd": "\ud83c\uddec\ud83c\udde9",
+ "flag-ge": "\ud83c\uddec\ud83c\uddea",
+ "flag-gf": "\ud83c\uddec\ud83c\uddeb",
+ "flag-gg": "\ud83c\uddec\ud83c\uddec",
+ "flag-gh": "\ud83c\uddec\ud83c\udded",
+ "flag-gi": "\ud83c\uddec\ud83c\uddee",
+ "flag-gl": "\ud83c\uddec\ud83c\uddf1",
+ "flag-gm": "\ud83c\uddec\ud83c\uddf2",
+ "flag-gn": "\ud83c\uddec\ud83c\uddf3",
+ "flag-gp": "\ud83c\uddec\ud83c\uddf5",
+ "flag-gq": "\ud83c\uddec\ud83c\uddf6",
+ "flag-gr": "\ud83c\uddec\ud83c\uddf7",
+ "flag-gs": "\ud83c\uddec\ud83c\uddf8",
+ "flag-gt": "\ud83c\uddec\ud83c\uddf9",
+ "flag-gu": "\ud83c\uddec\ud83c\uddfa",
+ "flag-gw": "\ud83c\uddec\ud83c\uddfc",
+ "flag-gy": "\ud83c\uddec\ud83c\uddfe",
+ "flag-hk": "\ud83c\udded\ud83c\uddf0",
+ "flag-hm": "\ud83c\udded\ud83c\uddf2",
+ "flag-hn": "\ud83c\udded\ud83c\uddf3",
+ "flag-hr": "\ud83c\udded\ud83c\uddf7",
+ "flag-ht": "\ud83c\udded\ud83c\uddf9",
+ "flag-hu": "\ud83c\udded\ud83c\uddfa",
+ "flag-ic": "\ud83c\uddee\ud83c\udde8",
+ "flag-id": "\ud83c\uddee\ud83c\udde9",
+ "flag-ie": "\ud83c\uddee\ud83c\uddea",
+ "flag-il": "\ud83c\uddee\ud83c\uddf1",
+ "flag-im": "\ud83c\uddee\ud83c\uddf2",
+ "flag-in": "\ud83c\uddee\ud83c\uddf3",
+ "flag-io": "\ud83c\uddee\ud83c\uddf4",
+ "flag-iq": "\ud83c\uddee\ud83c\uddf6",
+ "flag-ir": "\ud83c\uddee\ud83c\uddf7",
+ "flag-is": "\ud83c\uddee\ud83c\uddf8",
+ "flag-it": "\ud83c\uddee\ud83c\uddf9",
+ "flag-je": "\ud83c\uddef\ud83c\uddea",
+ "flag-jm": "\ud83c\uddef\ud83c\uddf2",
+ "flag-jo": "\ud83c\uddef\ud83c\uddf4",
+ "flag-jp": "\ud83c\uddef\ud83c\uddf5",
+ "flag-ke": "\ud83c\uddf0\ud83c\uddea",
+ "flag-kg": "\ud83c\uddf0\ud83c\uddec",
+ "flag-kh": "\ud83c\uddf0\ud83c\udded",
+ "flag-ki": "\ud83c\uddf0\ud83c\uddee",
+ "flag-km": "\ud83c\uddf0\ud83c\uddf2",
+ "flag-kn": "\ud83c\uddf0\ud83c\uddf3",
+ "flag-kp": "\ud83c\uddf0\ud83c\uddf5",
+ "flag-kr": "\ud83c\uddf0\ud83c\uddf7",
+ "flag-kw": "\ud83c\uddf0\ud83c\uddfc",
+ "flag-ky": "\ud83c\uddf0\ud83c\uddfe",
+ "flag-kz": "\ud83c\uddf0\ud83c\uddff",
+ "flag-la": "\ud83c\uddf1\ud83c\udde6",
+ "flag-lb": "\ud83c\uddf1\ud83c\udde7",
+ "flag-lc": "\ud83c\uddf1\ud83c\udde8",
+ "flag-li": "\ud83c\uddf1\ud83c\uddee",
+ "flag-lk": "\ud83c\uddf1\ud83c\uddf0",
+ "flag-lr": "\ud83c\uddf1\ud83c\uddf7",
+ "flag-ls": "\ud83c\uddf1\ud83c\uddf8",
+ "flag-lt": "\ud83c\uddf1\ud83c\uddf9",
+ "flag-lu": "\ud83c\uddf1\ud83c\uddfa",
+ "flag-lv": "\ud83c\uddf1\ud83c\uddfb",
+ "flag-ly": "\ud83c\uddf1\ud83c\uddfe",
+ "flag-ma": "\ud83c\uddf2\ud83c\udde6",
+ "flag-mc": "\ud83c\uddf2\ud83c\udde8",
+ "flag-md": "\ud83c\uddf2\ud83c\udde9",
+ "flag-me": "\ud83c\uddf2\ud83c\uddea",
+ "flag-mf": "\ud83c\uddf2\ud83c\uddeb",
+ "flag-mg": "\ud83c\uddf2\ud83c\uddec",
+ "flag-mh": "\ud83c\uddf2\ud83c\udded",
+ "flag-mk": "\ud83c\uddf2\ud83c\uddf0",
+ "flag-ml": "\ud83c\uddf2\ud83c\uddf1",
+ "flag-mm": "\ud83c\uddf2\ud83c\uddf2",
+ "flag-mn": "\ud83c\uddf2\ud83c\uddf3",
+ "flag-mo": "\ud83c\uddf2\ud83c\uddf4",
+ "flag-mp": "\ud83c\uddf2\ud83c\uddf5",
+ "flag-mq": "\ud83c\uddf2\ud83c\uddf6",
+ "flag-mr": "\ud83c\uddf2\ud83c\uddf7",
+ "flag-ms": "\ud83c\uddf2\ud83c\uddf8",
+ "flag-mt": "\ud83c\uddf2\ud83c\uddf9",
+ "flag-mu": "\ud83c\uddf2\ud83c\uddfa",
+ "flag-mv": "\ud83c\uddf2\ud83c\uddfb",
+ "flag-mw": "\ud83c\uddf2\ud83c\uddfc",
+ "flag-mx": "\ud83c\uddf2\ud83c\uddfd",
+ "flag-my": "\ud83c\uddf2\ud83c\uddfe",
+ "flag-mz": "\ud83c\uddf2\ud83c\uddff",
+ "flag-na": "\ud83c\uddf3\ud83c\udde6",
+ "flag-nc": "\ud83c\uddf3\ud83c\udde8",
+ "flag-ne": "\ud83c\uddf3\ud83c\uddea",
+ "flag-nf": "\ud83c\uddf3\ud83c\uddeb",
+ "flag-ng": "\ud83c\uddf3\ud83c\uddec",
+ "flag-ni": "\ud83c\uddf3\ud83c\uddee",
+ "flag-nl": "\ud83c\uddf3\ud83c\uddf1",
+ "flag-no": "\ud83c\uddf3\ud83c\uddf4",
+ "flag-np": "\ud83c\uddf3\ud83c\uddf5",
+ "flag-nr": "\ud83c\uddf3\ud83c\uddf7",
+ "flag-nu": "\ud83c\uddf3\ud83c\uddfa",
+ "flag-nz": "\ud83c\uddf3\ud83c\uddff",
+ "flag-om": "\ud83c\uddf4\ud83c\uddf2",
+ "flag-pa": "\ud83c\uddf5\ud83c\udde6",
+ "flag-pe": "\ud83c\uddf5\ud83c\uddea",
+ "flag-pf": "\ud83c\uddf5\ud83c\uddeb",
+ "flag-pg": "\ud83c\uddf5\ud83c\uddec",
+ "flag-ph": "\ud83c\uddf5\ud83c\udded",
+ "flag-pk": "\ud83c\uddf5\ud83c\uddf0",
+ "flag-pl": "\ud83c\uddf5\ud83c\uddf1",
+ "flag-pm": "\ud83c\uddf5\ud83c\uddf2",
+ "flag-pn": "\ud83c\uddf5\ud83c\uddf3",
+ "flag-pr": "\ud83c\uddf5\ud83c\uddf7",
+ "flag-ps": "\ud83c\uddf5\ud83c\uddf8",
+ "flag-pt": "\ud83c\uddf5\ud83c\uddf9",
+ "flag-pw": "\ud83c\uddf5\ud83c\uddfc",
+ "flag-py": "\ud83c\uddf5\ud83c\uddfe",
+ "flag-qa": "\ud83c\uddf6\ud83c\udde6",
+ "flag-re": "\ud83c\uddf7\ud83c\uddea",
+ "flag-ro": "\ud83c\uddf7\ud83c\uddf4",
+ "flag-rs": "\ud83c\uddf7\ud83c\uddf8",
+ "flag-ru": "\ud83c\uddf7\ud83c\uddfa",
+ "flag-rw": "\ud83c\uddf7\ud83c\uddfc",
+ "flag-sa": "\ud83c\uddf8\ud83c\udde6",
+ "flag-sark": "\ud83c\udde8\ud83c\uddf6",
+ "flag-sb": "\ud83c\uddf8\ud83c\udde7",
+ "flag-sc": "\ud83c\uddf8\ud83c\udde8",
+ "flag-scotland": "\ud83c\udff4\udb40\udc67\udb40\udc62\udb40\udc73\udb40\udc63\udb40\udc74\udb40\udc7f",
+ "flag-sd": "\ud83c\uddf8\ud83c\udde9",
+ "flag-se": "\ud83c\uddf8\ud83c\uddea",
+ "flag-sg": "\ud83c\uddf8\ud83c\uddec",
+ "flag-sh": "\ud83c\uddf8\ud83c\udded",
+ "flag-si": "\ud83c\uddf8\ud83c\uddee",
+ "flag-sj": "\ud83c\uddf8\ud83c\uddef",
+ "flag-sk": "\ud83c\uddf8\ud83c\uddf0",
+ "flag-sl": "\ud83c\uddf8\ud83c\uddf1",
+ "flag-sm": "\ud83c\uddf8\ud83c\uddf2",
+ "flag-sn": "\ud83c\uddf8\ud83c\uddf3",
+ "flag-so": "\ud83c\uddf8\ud83c\uddf4",
+ "flag-sr": "\ud83c\uddf8\ud83c\uddf7",
+ "flag-ss": "\ud83c\uddf8\ud83c\uddf8",
+ "flag-st": "\ud83c\uddf8\ud83c\uddf9",
+ "flag-sv": "\ud83c\uddf8\ud83c\uddfb",
+ "flag-sx": "\ud83c\uddf8\ud83c\uddfd",
+ "flag-sy": "\ud83c\uddf8\ud83c\uddfe",
+ "flag-sz": "\ud83c\uddf8\ud83c\uddff",
+ "flag-ta": "\ud83c\uddf9\ud83c\udde6",
+ "flag-tc": "\ud83c\uddf9\ud83c\udde8",
+ "flag-td": "\ud83c\uddf9\ud83c\udde9",
+ "flag-tf": "\ud83c\uddf9\ud83c\uddeb",
+ "flag-tg": "\ud83c\uddf9\ud83c\uddec",
+ "flag-th": "\ud83c\uddf9\ud83c\udded",
+ "flag-tj": "\ud83c\uddf9\ud83c\uddef",
+ "flag-tk": "\ud83c\uddf9\ud83c\uddf0",
+ "flag-tl": "\ud83c\uddf9\ud83c\uddf1",
+ "flag-tm": "\ud83c\uddf9\ud83c\uddf2",
+ "flag-tn": "\ud83c\uddf9\ud83c\uddf3",
+ "flag-to": "\ud83c\uddf9\ud83c\uddf4",
+ "flag-tr": "\ud83c\uddf9\ud83c\uddf7",
+ "flag-tt": "\ud83c\uddf9\ud83c\uddf9",
+ "flag-tv": "\ud83c\uddf9\ud83c\uddfb",
+ "flag-tw": "\ud83c\uddf9\ud83c\uddfc",
+ "flag-tz": "\ud83c\uddf9\ud83c\uddff",
+ "flag-ua": "\ud83c\uddfa\ud83c\udde6",
+ "flag-ug": "\ud83c\uddfa\ud83c\uddec",
+ "flag-um": "\ud83c\uddfa\ud83c\uddf2",
+ "flag-un": "\ud83c\uddfa\ud83c\uddf3",
+ "flag-us": "\ud83c\uddfa\ud83c\uddf8",
+ "flag-uy": "\ud83c\uddfa\ud83c\uddfe",
+ "flag-uz": "\ud83c\uddfa\ud83c\uddff",
+ "flag-va": "\ud83c\uddfb\ud83c\udde6",
+ "flag-vc": "\ud83c\uddfb\ud83c\udde8",
+ "flag-ve": "\ud83c\uddfb\ud83c\uddea",
+ "flag-vg": "\ud83c\uddfb\ud83c\uddec",
+ "flag-vi": "\ud83c\uddfb\ud83c\uddee",
+ "flag-vn": "\ud83c\uddfb\ud83c\uddf3",
+ "flag-vu": "\ud83c\uddfb\ud83c\uddfa",
+ "flag-wales": "\ud83c\udff4\udb40\udc67\udb40\udc62\udb40\udc77\udb40\udc6c\udb40\udc73\udb40\udc7f",
+ "flag-wf": "\ud83c\uddfc\ud83c\uddeb",
+ "flag-ws": "\ud83c\uddfc\ud83c\uddf8",
+ "flag-xk": "\ud83c\uddfd\ud83c\uddf0",
+ "flag-ye": "\ud83c\uddfe\ud83c\uddea",
+ "flag-yt": "\ud83c\uddfe\ud83c\uddf9",
+ "flag-za": "\ud83c\uddff\ud83c\udde6",
+ "flag-zm": "\ud83c\uddff\ud83c\uddf2",
+ "flag-zw": "\ud83c\uddff\ud83c\uddfc",
+ "flags": "\ud83c\udf8f",
+ "flamingo": "\ud83e\udda9",
+ "flashlight": "\ud83d\udd26",
+ "flat_shoe": "\ud83e\udd7f",
+ "flatbread": "\ud83e\uded3",
+ "fleur_de_lis": "\u269c\ufe0f",
+ "flight_arrival": "\ud83d\udeec",
+ "flight_departure": "\ud83d\udeeb",
+ "flipper": "\ud83d\udc2c",
+ "floppy_disk": "\ud83d\udcbe",
+ "flower_playing_cards": "\ud83c\udfb4",
+ "flushed": "\ud83d\ude33",
+ "flute": "\ud83e\ude88",
+ "fly": "\ud83e\udeb0",
+ "flying_disc": "\ud83e\udd4f",
+ "flying_saucer": "\ud83d\udef8",
+ "fog": "\ud83c\udf2b\ufe0f",
+ "foggy": "\ud83c\udf01",
+ "folding_hand_fan": "\ud83e\udead",
+ "fondue": "\ud83e\uded5",
+ "foot": "\ud83e\uddb6",
+ "football": "\ud83c\udfc8",
+ "footprints": "\ud83d\udc63",
+ "fork_and_knife": "\ud83c\udf74",
+ "fortune_cookie": "\ud83e\udd60",
+ "fountain": "\u26f2",
+ "fountain_pen": "\ud83d\udd8b\ufe0f",
+ "four": "4\ufe0f\u20e3",
+ "four_leaf_clover": "\ud83c\udf40",
+ "fox_face": "\ud83e\udd8a",
+ "fr": "\ud83c\uddeb\ud83c\uddf7",
+ "frame_with_picture": "\ud83d\uddbc\ufe0f",
+ "framed_picture": "\ud83d\uddbc\ufe0f",
+ "free": "\ud83c\udd93",
+ "french_guiana": "\ud83c\uddec\ud83c\uddeb",
+ "french_polynesia": "\ud83c\uddf5\ud83c\uddeb",
+ "french_southern_territories": "\ud83c\uddf9\ud83c\uddeb",
+ "fried_egg": "\ud83c\udf73",
+ "fried_shrimp": "\ud83c\udf64",
+ "fries": "\ud83c\udf5f",
+ "frog": "\ud83d\udc38",
+ "frowning": "\ud83d\ude26",
+ "frowning_face": "\u2639\ufe0f",
+ "frowning_man": "\ud83d\ude4d\u200d\u2642\ufe0f",
+ "frowning_person": "\ud83d\ude4d",
+ "frowning_woman": "\ud83d\ude4d\u200d\u2640\ufe0f",
+ "fu": "\ud83d\udd95",
+ "fuelpump": "\u26fd",
+ "full_moon": "\ud83c\udf15",
+ "full_moon_with_face": "\ud83c\udf1d",
+ "funeral_urn": "\u26b1\ufe0f",
+ "gabon": "\ud83c\uddec\ud83c\udde6",
+ "gambia": "\ud83c\uddec\ud83c\uddf2",
+ "game_die": "\ud83c\udfb2",
+ "garlic": "\ud83e\uddc4",
+ "gb": "\ud83c\uddec\ud83c\udde7",
+ "gear": "\u2699\ufe0f",
+ "gem": "\ud83d\udc8e",
+ "gemini": "\u264a",
+ "genie": "\ud83e\uddde",
+ "genie_man": "\ud83e\uddde\u200d\u2642\ufe0f",
+ "genie_woman": "\ud83e\uddde\u200d\u2640\ufe0f",
+ "georgia": "\ud83c\uddec\ud83c\uddea",
+ "ghana": "\ud83c\uddec\ud83c\udded",
+ "ghost": "\ud83d\udc7b",
+ "gibraltar": "\ud83c\uddec\ud83c\uddee",
+ "gift": "\ud83c\udf81",
+ "gift_heart": "\ud83d\udc9d",
+ "ginger_root": "\ud83e\udeda",
+ "giraffe": "\ud83e\udd92",
+ "giraffe_face": "\ud83e\udd92",
+ "girl": "\ud83d\udc67",
+ "glass_of_milk": "\ud83e\udd5b",
+ "globe_with_meridians": "\ud83c\udf10",
+ "gloves": "\ud83e\udde4",
+ "goal_net": "\ud83e\udd45",
+ "goat": "\ud83d\udc10",
+ "goggles": "\ud83e\udd7d",
+ "golf": "\u26f3",
+ "golfer": "\ud83c\udfcc\ufe0f",
+ "golfing": "\ud83c\udfcc\ufe0f",
+ "golfing_man": "\ud83c\udfcc\ufe0f\u200d\u2642\ufe0f",
+ "golfing_woman": "\ud83c\udfcc\ufe0f\u200d\u2640\ufe0f",
+ "goose": "\ud83e\udebf",
+ "gorilla": "\ud83e\udd8d",
+ "grapes": "\ud83c\udf47",
+ "greece": "\ud83c\uddec\ud83c\uddf7",
+ "green_apple": "\ud83c\udf4f",
+ "green_book": "\ud83d\udcd7",
+ "green_circle": "\ud83d\udfe2",
+ "green_heart": "\ud83d\udc9a",
+ "green_salad": "\ud83e\udd57",
+ "green_square": "\ud83d\udfe9",
+ "greenland": "\ud83c\uddec\ud83c\uddf1",
+ "grenada": "\ud83c\uddec\ud83c\udde9",
+ "grey_exclamation": "\u2755",
+ "grey_heart": "\ud83e\ude76",
+ "grey_question": "\u2754",
+ "grimacing": "\ud83d\ude2c",
+ "grin": "\ud83d\ude01",
+ "grinning": "\ud83d\ude00",
+ "grinning_face_with_one_large_and_one_small_eye": "\ud83e\udd2a",
+ "grinning_face_with_star_eyes": "\ud83e\udd29",
+ "guadeloupe": "\ud83c\uddec\ud83c\uddf5",
+ "guam": "\ud83c\uddec\ud83c\uddfa",
+ "guard": "\ud83d\udc82",
+ "guardsman": "\ud83d\udc82",
+ "guardswoman": "\ud83d\udc82\u200d\u2640\ufe0f",
+ "guatemala": "\ud83c\uddec\ud83c\uddf9",
+ "guernsey": "\ud83c\uddec\ud83c\uddec",
+ "guide_dog": "\ud83e\uddae",
+ "guinea": "\ud83c\uddec\ud83c\uddf3",
+ "guinea_bissau": "\ud83c\uddec\ud83c\uddfc",
+ "guitar": "\ud83c\udfb8",
+ "gun": "\ud83d\udd2b",
+ "guyana": "\ud83c\uddec\ud83c\uddfe",
+ "hair_pick": "\ud83e\udeae",
+ "haircut": "\ud83d\udc87",
+ "haircut_man": "\ud83d\udc87\u200d\u2642\ufe0f",
+ "haircut_woman": "\ud83d\udc87\u200d\u2640\ufe0f",
+ "haiti": "\ud83c\udded\ud83c\uddf9",
+ "hamburger": "\ud83c\udf54",
+ "hammer": "\ud83d\udd28",
+ "hammer_and_pick": "\u2692\ufe0f",
+ "hammer_and_wrench": "\ud83d\udee0\ufe0f",
+ "hamsa": "\ud83e\udeac",
+ "hamster": "\ud83d\udc39",
+ "hand": "\u270b",
+ "hand_over_mouth": "\ud83e\udd2d",
+ "hand_with_index_and_middle_fingers_crossed": "\ud83e\udd1e",
+ "hand_with_index_finger_and_thumb_crossed": "\ud83e\udef0",
+ "handbag": "\ud83d\udc5c",
+ "handball": "\ud83e\udd3e",
+ "handball_person": "\ud83e\udd3e",
+ "handshake": "\ud83e\udd1d",
+ "hankey": "\ud83d\udca9",
+ "harp": "\ud83e\ude89",
+ "hash": "#\ufe0f\u20e3",
+ "hatched_chick": "\ud83d\udc25",
+ "hatching_chick": "\ud83d\udc23",
+ "head_shaking_horizontally": "\ud83d\ude42\u200d\u2194\ufe0f",
+ "head_shaking_vertically": "\ud83d\ude42\u200d\u2195\ufe0f",
+ "headphones": "\ud83c\udfa7",
+ "headstone": "\ud83e\udea6",
+ "health_worker": "\ud83e\uddd1\u200d\u2695\ufe0f",
+ "hear_no_evil": "\ud83d\ude49",
+ "heard_mcdonald_islands": "\ud83c\udded\ud83c\uddf2",
+ "heart": "\u2764\ufe0f",
+ "heart_decoration": "\ud83d\udc9f",
+ "heart_eyes": "\ud83d\ude0d",
+ "heart_eyes_cat": "\ud83d\ude3b",
+ "heart_hands": "\ud83e\udef6",
+ "heart_on_fire": "\u2764\ufe0f\u200d\ud83d\udd25",
+ "heartbeat": "\ud83d\udc93",
+ "heartpulse": "\ud83d\udc97",
+ "hearts": "\u2665\ufe0f",
+ "heavy_check_mark": "\u2714\ufe0f",
+ "heavy_division_sign": "\u2797",
+ "heavy_dollar_sign": "\ud83d\udcb2",
+ "heavy_equals_sign": "\ud83d\udff0",
+ "heavy_exclamation_mark": "\u2757",
+ "heavy_heart_exclamation": "\u2763\ufe0f",
+ "heavy_heart_exclamation_mark_ornament": "\u2763\ufe0f",
+ "heavy_minus_sign": "\u2796",
+ "heavy_multiplication_x": "\u2716\ufe0f",
+ "heavy_plus_sign": "\u2795",
+ "hedgehog": "\ud83e\udd94",
+ "helicopter": "\ud83d\ude81",
+ "helmet_with_white_cross": "\u26d1\ufe0f",
+ "herb": "\ud83c\udf3f",
+ "hibiscus": "\ud83c\udf3a",
+ "high_brightness": "\ud83d\udd06",
+ "high_heel": "\ud83d\udc60",
+ "hiking_boot": "\ud83e\udd7e",
+ "hindu_temple": "\ud83d\uded5",
+ "hippopotamus": "\ud83e\udd9b",
+ "hocho": "\ud83d\udd2a",
+ "hole": "\ud83d\udd73\ufe0f",
+ "honduras": "\ud83c\udded\ud83c\uddf3",
+ "honey_pot": "\ud83c\udf6f",
+ "honeybee": "\ud83d\udc1d",
+ "hong_kong": "\ud83c\udded\ud83c\uddf0",
+ "hook": "\ud83e\ude9d",
+ "horse": "\ud83d\udc34",
+ "horse_racing": "\ud83c\udfc7",
+ "hospital": "\ud83c\udfe5",
+ "hot_face": "\ud83e\udd75",
+ "hot_pepper": "\ud83c\udf36\ufe0f",
+ "hotdog": "\ud83c\udf2d",
+ "hotel": "\ud83c\udfe8",
+ "hotsprings": "\u2668\ufe0f",
+ "hourglass": "\u231b",
+ "hourglass_flowing_sand": "\u23f3",
+ "house": "\ud83c\udfe0",
+ "house_buildings": "\ud83c\udfd8\ufe0f",
+ "house_with_garden": "\ud83c\udfe1",
+ "houses": "\ud83c\udfd8\ufe0f",
+ "hugging_face": "\ud83e\udd17",
+ "hugs": "\ud83e\udd17",
+ "hungary": "\ud83c\udded\ud83c\uddfa",
+ "hushed": "\ud83d\ude2f",
+ "hut": "\ud83d\uded6",
+ "hyacinth": "\ud83e\udebb",
+ "i_love_you_hand_sign": "\ud83e\udd1f",
+ "ice_cream": "\ud83c\udf68",
+ "ice_cube": "\ud83e\uddca",
+ "ice_hockey": "\ud83c\udfd2",
+ "ice_hockey_stick_and_puck": "\ud83c\udfd2",
+ "ice_skate": "\u26f8\ufe0f",
+ "icecream": "\ud83c\udf66",
+ "iceland": "\ud83c\uddee\ud83c\uddf8",
+ "id": "\ud83c\udd94",
+ "identification_card": "\ud83e\udeaa",
+ "ideograph_advantage": "\ud83c\ude50",
+ "imp": "\ud83d\udc7f",
+ "inbox_tray": "\ud83d\udce5",
+ "incoming_envelope": "\ud83d\udce8",
+ "index_pointing_at_the_viewer": "\ud83e\udef5",
+ "india": "\ud83c\uddee\ud83c\uddf3",
+ "indonesia": "\ud83c\uddee\ud83c\udde9",
+ "infinity": "\u267e\ufe0f",
+ "information_desk_person": "\ud83d\udc81",
+ "information_source": "\u2139\ufe0f",
+ "innocent": "\ud83d\ude07",
+ "interrobang": "\u2049\ufe0f",
+ "iphone": "\ud83d\udcf1",
+ "iran": "\ud83c\uddee\ud83c\uddf7",
+ "iraq": "\ud83c\uddee\ud83c\uddf6",
+ "ireland": "\ud83c\uddee\ud83c\uddea",
+ "isle_of_man": "\ud83c\uddee\ud83c\uddf2",
+ "israel": "\ud83c\uddee\ud83c\uddf1",
+ "it": "\ud83c\uddee\ud83c\uddf9",
+ "izakaya_lantern": "\ud83c\udfee",
+ "jack_o_lantern": "\ud83c\udf83",
+ "jamaica": "\ud83c\uddef\ud83c\uddf2",
+ "japan": "\ud83d\uddfe",
+ "japanese_castle": "\ud83c\udfef",
+ "japanese_goblin": "\ud83d\udc7a",
+ "japanese_ogre": "\ud83d\udc79",
+ "jar": "\ud83e\uded9",
+ "jeans": "\ud83d\udc56",
+ "jellyfish": "\ud83e\udebc",
+ "jersey": "\ud83c\uddef\ud83c\uddea",
+ "jigsaw": "\ud83e\udde9",
+ "jordan": "\ud83c\uddef\ud83c\uddf4",
+ "joy": "\ud83d\ude02",
+ "joy_cat": "\ud83d\ude39",
+ "joystick": "\ud83d\udd79\ufe0f",
+ "jp": "\ud83c\uddef\ud83c\uddf5",
+ "judge": "\ud83e\uddd1\u200d\u2696\ufe0f",
+ "juggling": "\ud83e\udd39",
+ "juggling_person": "\ud83e\udd39",
+ "kaaba": "\ud83d\udd4b",
+ "kangaroo": "\ud83e\udd98",
+ "kazakhstan": "\ud83c\uddf0\ud83c\uddff",
+ "kenya": "\ud83c\uddf0\ud83c\uddea",
+ "key": "\ud83d\udd11",
+ "keyboard": "\u2328\ufe0f",
+ "keycap_star": "*\ufe0f\u20e3",
+ "keycap_ten": "\ud83d\udd1f",
+ "khanda": "\ud83e\udeaf",
+ "kick_scooter": "\ud83d\udef4",
+ "kimono": "\ud83d\udc58",
+ "kiribati": "\ud83c\uddf0\ud83c\uddee",
+ "kiss": "\ud83d\udc8b",
+ "kissing": "\ud83d\ude17",
+ "kissing_cat": "\ud83d\ude3d",
+ "kissing_closed_eyes": "\ud83d\ude1a",
+ "kissing_heart": "\ud83d\ude18",
+ "kissing_smiling_eyes": "\ud83d\ude19",
+ "kite": "\ud83e\ude81",
+ "kiwi_fruit": "\ud83e\udd5d",
+ "kiwifruit": "\ud83e\udd5d",
+ "kneeling_man": "\ud83e\uddce\u200d\u2642\ufe0f",
+ "kneeling_person": "\ud83e\uddce",
+ "kneeling_woman": "\ud83e\uddce\u200d\u2640\ufe0f",
+ "knife": "\ud83d\udd2a",
+ "knife_fork_plate": "\ud83c\udf7d\ufe0f",
+ "knot": "\ud83e\udea2",
+ "koala": "\ud83d\udc28",
+ "koko": "\ud83c\ude01",
+ "kosovo": "\ud83c\uddfd\ud83c\uddf0",
+ "kr": "\ud83c\uddf0\ud83c\uddf7",
+ "kuwait": "\ud83c\uddf0\ud83c\uddfc",
+ "kyrgyzstan": "\ud83c\uddf0\ud83c\uddec",
+ "lab_coat": "\ud83e\udd7c",
+ "label": "\ud83c\udff7\ufe0f",
+ "lacrosse": "\ud83e\udd4d",
+ "ladder": "\ud83e\ude9c",
+ "lady_beetle": "\ud83d\udc1e",
+ "ladybug": "\ud83d\udc1e",
+ "lantern": "\ud83c\udfee",
+ "laos": "\ud83c\uddf1\ud83c\udde6",
+ "large_blue_circle": "\ud83d\udd35",
+ "large_blue_diamond": "\ud83d\udd37",
+ "large_blue_square": "\ud83d\udfe6",
+ "large_brown_circle": "\ud83d\udfe4",
+ "large_brown_square": "\ud83d\udfeb",
+ "large_green_circle": "\ud83d\udfe2",
+ "large_green_square": "\ud83d\udfe9",
+ "large_orange_circle": "\ud83d\udfe0",
+ "large_orange_diamond": "\ud83d\udd36",
+ "large_orange_square": "\ud83d\udfe7",
+ "large_purple_circle": "\ud83d\udfe3",
+ "large_purple_square": "\ud83d\udfea",
+ "large_red_square": "\ud83d\udfe5",
+ "large_yellow_circle": "\ud83d\udfe1",
+ "large_yellow_square": "\ud83d\udfe8",
+ "last_quarter_moon": "\ud83c\udf17",
+ "last_quarter_moon_with_face": "\ud83c\udf1c",
+ "latin_cross": "\u271d\ufe0f",
+ "latvia": "\ud83c\uddf1\ud83c\uddfb",
+ "laughing": "\ud83d\ude06",
+ "leafless_tree": "\ud83e\udebe",
+ "leafy_green": "\ud83e\udd6c",
+ "leaves": "\ud83c\udf43",
+ "lebanon": "\ud83c\uddf1\ud83c\udde7",
+ "ledger": "\ud83d\udcd2",
+ "left-facing_fist": "\ud83e\udd1b",
+ "left_luggage": "\ud83d\udec5",
+ "left_right_arrow": "\u2194\ufe0f",
+ "left_speech_bubble": "\ud83d\udde8\ufe0f",
+ "leftwards_arrow_with_hook": "\u21a9\ufe0f",
+ "leftwards_hand": "\ud83e\udef2",
+ "leftwards_pushing_hand": "\ud83e\udef7",
+ "leg": "\ud83e\uddb5",
+ "lemon": "\ud83c\udf4b",
+ "leo": "\u264c",
+ "leopard": "\ud83d\udc06",
+ "lesotho": "\ud83c\uddf1\ud83c\uddf8",
+ "level_slider": "\ud83c\udf9a\ufe0f",
+ "liberia": "\ud83c\uddf1\ud83c\uddf7",
+ "libra": "\u264e",
+ "libya": "\ud83c\uddf1\ud83c\uddfe",
+ "liechtenstein": "\ud83c\uddf1\ud83c\uddee",
+ "light_blue_heart": "\ud83e\ude75",
+ "light_rail": "\ud83d\ude88",
+ "lightning": "\ud83c\udf29\ufe0f",
+ "lightning_cloud": "\ud83c\udf29\ufe0f",
+ "lime": "\ud83c\udf4b\u200d\ud83d\udfe9",
+ "link": "\ud83d\udd17",
+ "linked_paperclips": "\ud83d\udd87\ufe0f",
+ "lion": "\ud83e\udd81",
+ "lion_face": "\ud83e\udd81",
+ "lips": "\ud83d\udc44",
+ "lipstick": "\ud83d\udc84",
+ "lithuania": "\ud83c\uddf1\ud83c\uddf9",
+ "lizard": "\ud83e\udd8e",
+ "llama": "\ud83e\udd99",
+ "lobster": "\ud83e\udd9e",
+ "lock": "\ud83d\udd12",
+ "lock_with_ink_pen": "\ud83d\udd0f",
+ "lollipop": "\ud83c\udf6d",
+ "long_drum": "\ud83e\ude98",
+ "loop": "\u27bf",
+ "lotion_bottle": "\ud83e\uddf4",
+ "lotus": "\ud83e\udeb7",
+ "lotus_position": "\ud83e\uddd8",
+ "lotus_position_man": "\ud83e\uddd8\u200d\u2642\ufe0f",
+ "lotus_position_woman": "\ud83e\uddd8\u200d\u2640\ufe0f",
+ "loud_sound": "\ud83d\udd0a",
+ "loudspeaker": "\ud83d\udce2",
+ "love_hotel": "\ud83c\udfe9",
+ "love_letter": "\ud83d\udc8c",
+ "love_you_gesture": "\ud83e\udd1f",
+ "low_battery": "\ud83e\udeab",
+ "low_brightness": "\ud83d\udd05",
+ "lower_left_ballpoint_pen": "\ud83d\udd8a\ufe0f",
+ "lower_left_crayon": "\ud83d\udd8d\ufe0f",
+ "lower_left_fountain_pen": "\ud83d\udd8b\ufe0f",
+ "lower_left_paintbrush": "\ud83d\udd8c\ufe0f",
+ "luggage": "\ud83e\uddf3",
+ "lungs": "\ud83e\udec1",
+ "luxembourg": "\ud83c\uddf1\ud83c\uddfa",
+ "lying_face": "\ud83e\udd25",
+ "m": "\u24c2\ufe0f",
+ "macau": "\ud83c\uddf2\ud83c\uddf4",
+ "macedonia": "\ud83c\uddf2\ud83c\uddf0",
+ "madagascar": "\ud83c\uddf2\ud83c\uddec",
+ "mag": "\ud83d\udd0d",
+ "mag_right": "\ud83d\udd0e",
+ "mage": "\ud83e\uddd9",
+ "mage_man": "\ud83e\uddd9\u200d\u2642\ufe0f",
+ "mage_woman": "\ud83e\uddd9\u200d\u2640\ufe0f",
+ "magic_wand": "\ud83e\ude84",
+ "magnet": "\ud83e\uddf2",
+ "mahjong": "\ud83c\udc04",
+ "mailbox": "\ud83d\udceb",
+ "mailbox_closed": "\ud83d\udcea",
+ "mailbox_with_mail": "\ud83d\udcec",
+ "mailbox_with_no_mail": "\ud83d\udced",
+ "malawi": "\ud83c\uddf2\ud83c\uddfc",
+ "malaysia": "\ud83c\uddf2\ud83c\uddfe",
+ "maldives": "\ud83c\uddf2\ud83c\uddfb",
+ "male-artist": "\ud83d\udc68\u200d\ud83c\udfa8",
+ "male-astronaut": "\ud83d\udc68\u200d\ud83d\ude80",
+ "male-construction-worker": "\ud83d\udc77\u200d\u2642\ufe0f",
+ "male-cook": "\ud83d\udc68\u200d\ud83c\udf73",
+ "male-detective": "\ud83d\udd75\ufe0f\u200d\u2642\ufe0f",
+ "male-doctor": "\ud83d\udc68\u200d\u2695\ufe0f",
+ "male-factory-worker": "\ud83d\udc68\u200d\ud83c\udfed",
+ "male-farmer": "\ud83d\udc68\u200d\ud83c\udf3e",
+ "male-firefighter": "\ud83d\udc68\u200d\ud83d\ude92",
+ "male-guard": "\ud83d\udc82\u200d\u2642\ufe0f",
+ "male-judge": "\ud83d\udc68\u200d\u2696\ufe0f",
+ "male-mechanic": "\ud83d\udc68\u200d\ud83d\udd27",
+ "male-office-worker": "\ud83d\udc68\u200d\ud83d\udcbc",
+ "male-pilot": "\ud83d\udc68\u200d\u2708\ufe0f",
+ "male-police-officer": "\ud83d\udc6e\u200d\u2642\ufe0f",
+ "male-scientist": "\ud83d\udc68\u200d\ud83d\udd2c",
+ "male-singer": "\ud83d\udc68\u200d\ud83c\udfa4",
+ "male-student": "\ud83d\udc68\u200d\ud83c\udf93",
+ "male-teacher": "\ud83d\udc68\u200d\ud83c\udfeb",
+ "male-technologist": "\ud83d\udc68\u200d\ud83d\udcbb",
+ "male_detective": "\ud83d\udd75\ufe0f\u200d\u2642\ufe0f",
+ "male_elf": "\ud83e\udddd\u200d\u2642\ufe0f",
+ "male_fairy": "\ud83e\uddda\u200d\u2642\ufe0f",
+ "male_genie": "\ud83e\uddde\u200d\u2642\ufe0f",
+ "male_mage": "\ud83e\uddd9\u200d\u2642\ufe0f",
+ "male_sign": "\u2642\ufe0f",
+ "male_superhero": "\ud83e\uddb8\u200d\u2642\ufe0f",
+ "male_supervillain": "\ud83e\uddb9\u200d\u2642\ufe0f",
+ "male_vampire": "\ud83e\udddb\u200d\u2642\ufe0f",
+ "male_zombie": "\ud83e\udddf\u200d\u2642\ufe0f",
+ "mali": "\ud83c\uddf2\ud83c\uddf1",
+ "malta": "\ud83c\uddf2\ud83c\uddf9",
+ "mammoth": "\ud83e\udda3",
+ "man": "\ud83d\udc68",
+ "man-biking": "\ud83d\udeb4\u200d\u2642\ufe0f",
+ "man-bouncing-ball": "\u26f9\ufe0f\u200d\u2642\ufe0f",
+ "man-bowing": "\ud83d\ude47\u200d\u2642\ufe0f",
+ "man-boy": "\ud83d\udc68\u200d\ud83d\udc66",
+ "man-boy-boy": "\ud83d\udc68\u200d\ud83d\udc66\u200d\ud83d\udc66",
+ "man-cartwheeling": "\ud83e\udd38\u200d\u2642\ufe0f",
+ "man-facepalming": "\ud83e\udd26\u200d\u2642\ufe0f",
+ "man-frowning": "\ud83d\ude4d\u200d\u2642\ufe0f",
+ "man-gesturing-no": "\ud83d\ude45\u200d\u2642\ufe0f",
+ "man-gesturing-ok": "\ud83d\ude46\u200d\u2642\ufe0f",
+ "man-getting-haircut": "\ud83d\udc87\u200d\u2642\ufe0f",
+ "man-getting-massage": "\ud83d\udc86\u200d\u2642\ufe0f",
+ "man-girl": "\ud83d\udc68\u200d\ud83d\udc67",
+ "man-girl-boy": "\ud83d\udc68\u200d\ud83d\udc67\u200d\ud83d\udc66",
+ "man-girl-girl": "\ud83d\udc68\u200d\ud83d\udc67\u200d\ud83d\udc67",
+ "man-golfing": "\ud83c\udfcc\ufe0f\u200d\u2642\ufe0f",
+ "man-heart-man": "\ud83d\udc68\u200d\u2764\ufe0f\u200d\ud83d\udc68",
+ "man-juggling": "\ud83e\udd39\u200d\u2642\ufe0f",
+ "man-kiss-man": "\ud83d\udc68\u200d\u2764\ufe0f\u200d\ud83d\udc8b\u200d\ud83d\udc68",
+ "man-lifting-weights": "\ud83c\udfcb\ufe0f\u200d\u2642\ufe0f",
+ "man-man-boy": "\ud83d\udc68\u200d\ud83d\udc68\u200d\ud83d\udc66",
+ "man-man-boy-boy": "\ud83d\udc68\u200d\ud83d\udc68\u200d\ud83d\udc66\u200d\ud83d\udc66",
+ "man-man-girl": "\ud83d\udc68\u200d\ud83d\udc68\u200d\ud83d\udc67",
+ "man-man-girl-boy": "\ud83d\udc68\u200d\ud83d\udc68\u200d\ud83d\udc67\u200d\ud83d\udc66",
+ "man-man-girl-girl": "\ud83d\udc68\u200d\ud83d\udc68\u200d\ud83d\udc67\u200d\ud83d\udc67",
+ "man-mountain-biking": "\ud83d\udeb5\u200d\u2642\ufe0f",
+ "man-playing-handball": "\ud83e\udd3e\u200d\u2642\ufe0f",
+ "man-playing-water-polo": "\ud83e\udd3d\u200d\u2642\ufe0f",
+ "man-pouting": "\ud83d\ude4e\u200d\u2642\ufe0f",
+ "man-raising-hand": "\ud83d\ude4b\u200d\u2642\ufe0f",
+ "man-rowing-boat": "\ud83d\udea3\u200d\u2642\ufe0f",
+ "man-running": "\ud83c\udfc3\u200d\u2642\ufe0f",
+ "man-shrugging": "\ud83e\udd37\u200d\u2642\ufe0f",
+ "man-surfing": "\ud83c\udfc4\u200d\u2642\ufe0f",
+ "man-swimming": "\ud83c\udfca\u200d\u2642\ufe0f",
+ "man-tipping-hand": "\ud83d\udc81\u200d\u2642\ufe0f",
+ "man-walking": "\ud83d\udeb6\u200d\u2642\ufe0f",
+ "man-wearing-turban": "\ud83d\udc73\u200d\u2642\ufe0f",
+ "man-with-bunny-ears-partying": "\ud83d\udc6f\u200d\u2642\ufe0f",
+ "man-woman-boy": "\ud83d\udc68\u200d\ud83d\udc69\u200d\ud83d\udc66",
+ "man-woman-boy-boy": "\ud83d\udc68\u200d\ud83d\udc69\u200d\ud83d\udc66\u200d\ud83d\udc66",
+ "man-woman-girl": "\ud83d\udc68\u200d\ud83d\udc69\u200d\ud83d\udc67",
+ "man-woman-girl-boy": "\ud83d\udc68\u200d\ud83d\udc69\u200d\ud83d\udc67\u200d\ud83d\udc66",
+ "man-woman-girl-girl": "\ud83d\udc68\u200d\ud83d\udc69\u200d\ud83d\udc67\u200d\ud83d\udc67",
+ "man-wrestling": "\ud83e\udd3c\u200d\u2642\ufe0f",
+ "man_and_woman_holding_hands": "\ud83d\udc6b",
+ "man_artist": "\ud83d\udc68\u200d\ud83c\udfa8",
+ "man_astronaut": "\ud83d\udc68\u200d\ud83d\ude80",
+ "man_beard": "\ud83e\uddd4\u200d\u2642\ufe0f",
+ "man_cartwheeling": "\ud83e\udd38\u200d\u2642\ufe0f",
+ "man_climbing": "\ud83e\uddd7\u200d\u2642\ufe0f",
+ "man_cook": "\ud83d\udc68\u200d\ud83c\udf73",
+ "man_dancing": "\ud83d\udd7a",
+ "man_facepalming": "\ud83e\udd26\u200d\u2642\ufe0f",
+ "man_factory_worker": "\ud83d\udc68\u200d\ud83c\udfed",
+ "man_farmer": "\ud83d\udc68\u200d\ud83c\udf3e",
+ "man_feeding_baby": "\ud83d\udc68\u200d\ud83c\udf7c",
+ "man_firefighter": "\ud83d\udc68\u200d\ud83d\ude92",
+ "man_health_worker": "\ud83d\udc68\u200d\u2695\ufe0f",
+ "man_in_business_suit_levitating": "\ud83d\udd74\ufe0f",
+ "man_in_lotus_position": "\ud83e\uddd8\u200d\u2642\ufe0f",
+ "man_in_manual_wheelchair": "\ud83d\udc68\u200d\ud83e\uddbd",
+ "man_in_manual_wheelchair_facing_right": "\ud83d\udc68\u200d\ud83e\uddbd\u200d\u27a1\ufe0f",
+ "man_in_motorized_wheelchair": "\ud83d\udc68\u200d\ud83e\uddbc",
+ "man_in_motorized_wheelchair_facing_right": "\ud83d\udc68\u200d\ud83e\uddbc\u200d\u27a1\ufe0f",
+ "man_in_steamy_room": "\ud83e\uddd6\u200d\u2642\ufe0f",
+ "man_in_tuxedo": "\ud83e\udd35\u200d\u2642\ufe0f",
+ "man_judge": "\ud83d\udc68\u200d\u2696\ufe0f",
+ "man_juggling": "\ud83e\udd39\u200d\u2642\ufe0f",
+ "man_kneeling": "\ud83e\uddce\u200d\u2642\ufe0f",
+ "man_kneeling_facing_right": "\ud83e\uddce\u200d\u2642\ufe0f\u200d\u27a1\ufe0f",
+ "man_mechanic": "\ud83d\udc68\u200d\ud83d\udd27",
+ "man_office_worker": "\ud83d\udc68\u200d\ud83d\udcbc",
+ "man_pilot": "\ud83d\udc68\u200d\u2708\ufe0f",
+ "man_playing_handball": "\ud83e\udd3e\u200d\u2642\ufe0f",
+ "man_playing_water_polo": "\ud83e\udd3d\u200d\u2642\ufe0f",
+ "man_running_facing_right": "\ud83c\udfc3\u200d\u2642\ufe0f\u200d\u27a1\ufe0f",
+ "man_scientist": "\ud83d\udc68\u200d\ud83d\udd2c",
+ "man_shrugging": "\ud83e\udd37\u200d\u2642\ufe0f",
+ "man_singer": "\ud83d\udc68\u200d\ud83c\udfa4",
+ "man_standing": "\ud83e\uddcd\u200d\u2642\ufe0f",
+ "man_student": "\ud83d\udc68\u200d\ud83c\udf93",
+ "man_teacher": "\ud83d\udc68\u200d\ud83c\udfeb",
+ "man_technologist": "\ud83d\udc68\u200d\ud83d\udcbb",
+ "man_walking_facing_right": "\ud83d\udeb6\u200d\u2642\ufe0f\u200d\u27a1\ufe0f",
+ "man_with_beard": "\ud83e\uddd4\u200d\u2642\ufe0f",
+ "man_with_gua_pi_mao": "\ud83d\udc72",
+ "man_with_probing_cane": "\ud83d\udc68\u200d\ud83e\uddaf",
+ "man_with_turban": "\ud83d\udc73",
+ "man_with_veil": "\ud83d\udc70\u200d\u2642\ufe0f",
+ "man_with_white_cane_facing_right": "\ud83d\udc68\u200d\ud83e\uddaf\u200d\u27a1\ufe0f",
+ "mandarin": "\ud83c\udf4a",
+ "mango": "\ud83e\udd6d",
+ "mans_shoe": "\ud83d\udc5e",
+ "mantelpiece_clock": "\ud83d\udd70\ufe0f",
+ "manual_wheelchair": "\ud83e\uddbd",
+ "maple_leaf": "\ud83c\udf41",
+ "maracas": "\ud83e\ude87",
+ "marshall_islands": "\ud83c\uddf2\ud83c\udded",
+ "martial_arts_uniform": "\ud83e\udd4b",
+ "martinique": "\ud83c\uddf2\ud83c\uddf6",
+ "mask": "\ud83d\ude37",
+ "massage": "\ud83d\udc86",
+ "massage_man": "\ud83d\udc86\u200d\u2642\ufe0f",
+ "massage_woman": "\ud83d\udc86\u200d\u2640\ufe0f",
+ "mate": "\ud83e\uddc9",
+ "mate_drink": "\ud83e\uddc9",
+ "mauritania": "\ud83c\uddf2\ud83c\uddf7",
+ "mauritius": "\ud83c\uddf2\ud83c\uddfa",
+ "mayotte": "\ud83c\uddfe\ud83c\uddf9",
+ "meat_on_bone": "\ud83c\udf56",
+ "mechanic": "\ud83e\uddd1\u200d\ud83d\udd27",
+ "mechanical_arm": "\ud83e\uddbe",
+ "mechanical_leg": "\ud83e\uddbf",
+ "medal": "\ud83c\udf96\ufe0f",
+ "medal_military": "\ud83c\udf96\ufe0f",
+ "medal_sports": "\ud83c\udfc5",
+ "medical_symbol": "\u2695\ufe0f",
+ "mega": "\ud83d\udce3",
+ "melon": "\ud83c\udf48",
+ "melting_face": "\ud83e\udee0",
+ "memo": "\ud83d\udcdd",
+ "men-with-bunny-ears-partying": "\ud83d\udc6f\u200d\u2642\ufe0f",
+ "men_holding_hands": "\ud83d\udc6c",
+ "men_wrestling": "\ud83e\udd3c\u200d\u2642\ufe0f",
+ "mending_heart": "\u2764\ufe0f\u200d\ud83e\ude79",
+ "menorah": "\ud83d\udd4e",
+ "menorah_with_nine_branches": "\ud83d\udd4e",
+ "mens": "\ud83d\udeb9",
+ "mermaid": "\ud83e\udddc\u200d\u2640\ufe0f",
+ "merman": "\ud83e\udddc\u200d\u2642\ufe0f",
+ "merperson": "\ud83e\udddc",
+ "metal": "\ud83e\udd18",
+ "metro": "\ud83d\ude87",
+ "mexico": "\ud83c\uddf2\ud83c\uddfd",
+ "microbe": "\ud83e\udda0",
+ "micronesia": "\ud83c\uddeb\ud83c\uddf2",
+ "microphone": "\ud83c\udfa4",
+ "microscope": "\ud83d\udd2c",
+ "middle_finger": "\ud83d\udd95",
+ "military_helmet": "\ud83e\ude96",
+ "milk_glass": "\ud83e\udd5b",
+ "milky_way": "\ud83c\udf0c",
+ "minibus": "\ud83d\ude90",
+ "minidisc": "\ud83d\udcbd",
+ "mirror": "\ud83e\ude9e",
+ "mirror_ball": "\ud83e\udea9",
+ "mobile_phone_off": "\ud83d\udcf4",
+ "moldova": "\ud83c\uddf2\ud83c\udde9",
+ "monaco": "\ud83c\uddf2\ud83c\udde8",
+ "money_mouth_face": "\ud83e\udd11",
+ "money_with_wings": "\ud83d\udcb8",
+ "moneybag": "\ud83d\udcb0",
+ "mongolia": "\ud83c\uddf2\ud83c\uddf3",
+ "monkey": "\ud83d\udc12",
+ "monkey_face": "\ud83d\udc35",
+ "monocle_face": "\ud83e\uddd0",
+ "monorail": "\ud83d\ude9d",
+ "montenegro": "\ud83c\uddf2\ud83c\uddea",
+ "montserrat": "\ud83c\uddf2\ud83c\uddf8",
+ "moon": "\ud83c\udf14",
+ "moon_cake": "\ud83e\udd6e",
+ "moose": "\ud83e\udece",
+ "morocco": "\ud83c\uddf2\ud83c\udde6",
+ "mortar_board": "\ud83c\udf93",
+ "mosque": "\ud83d\udd4c",
+ "mosquito": "\ud83e\udd9f",
+ "mostly_sunny": "\ud83c\udf24\ufe0f",
+ "mother_christmas": "\ud83e\udd36",
+ "motor_boat": "\ud83d\udee5\ufe0f",
+ "motor_scooter": "\ud83d\udef5",
+ "motorcycle": "\ud83c\udfcd\ufe0f",
+ "motorized_wheelchair": "\ud83e\uddbc",
+ "motorway": "\ud83d\udee3\ufe0f",
+ "mount_fuji": "\ud83d\uddfb",
+ "mountain": "\u26f0\ufe0f",
+ "mountain_bicyclist": "\ud83d\udeb5",
+ "mountain_biking_man": "\ud83d\udeb5\u200d\u2642\ufe0f",
+ "mountain_biking_woman": "\ud83d\udeb5\u200d\u2640\ufe0f",
+ "mountain_cableway": "\ud83d\udea0",
+ "mountain_railway": "\ud83d\ude9e",
+ "mountain_snow": "\ud83c\udfd4\ufe0f",
+ "mouse": "\ud83d\udc2d",
+ "mouse2": "\ud83d\udc01",
+ "mouse_trap": "\ud83e\udea4",
+ "movie_camera": "\ud83c\udfa5",
+ "moyai": "\ud83d\uddff",
+ "mozambique": "\ud83c\uddf2\ud83c\uddff",
+ "mrs_claus": "\ud83e\udd36",
+ "muscle": "\ud83d\udcaa",
+ "mushroom": "\ud83c\udf44",
+ "musical_keyboard": "\ud83c\udfb9",
+ "musical_note": "\ud83c\udfb5",
+ "musical_score": "\ud83c\udfbc",
+ "mute": "\ud83d\udd07",
+ "mx_claus": "\ud83e\uddd1\u200d\ud83c\udf84",
+ "myanmar": "\ud83c\uddf2\ud83c\uddf2",
+ "nail_care": "\ud83d\udc85",
+ "name_badge": "\ud83d\udcdb",
+ "namibia": "\ud83c\uddf3\ud83c\udde6",
+ "national_park": "\ud83c\udfde\ufe0f",
+ "nauru": "\ud83c\uddf3\ud83c\uddf7",
+ "nauseated_face": "\ud83e\udd22",
+ "nazar_amulet": "\ud83e\uddff",
+ "necktie": "\ud83d\udc54",
+ "negative_squared_cross_mark": "\u274e",
+ "nepal": "\ud83c\uddf3\ud83c\uddf5",
+ "nerd_face": "\ud83e\udd13",
+ "nest_with_eggs": "\ud83e\udeba",
+ "nesting_dolls": "\ud83e\ude86",
+ "netherlands": "\ud83c\uddf3\ud83c\uddf1",
+ "neutral_face": "\ud83d\ude10",
+ "new": "\ud83c\udd95",
+ "new_caledonia": "\ud83c\uddf3\ud83c\udde8",
+ "new_moon": "\ud83c\udf11",
+ "new_moon_with_face": "\ud83c\udf1a",
+ "new_zealand": "\ud83c\uddf3\ud83c\uddff",
+ "newspaper": "\ud83d\udcf0",
+ "newspaper_roll": "\ud83d\uddde\ufe0f",
+ "next_track_button": "\u23ed\ufe0f",
+ "ng": "\ud83c\udd96",
+ "ng_man": "\ud83d\ude45\u200d\u2642\ufe0f",
+ "ng_woman": "\ud83d\ude45\u200d\u2640\ufe0f",
+ "nicaragua": "\ud83c\uddf3\ud83c\uddee",
+ "niger": "\ud83c\uddf3\ud83c\uddea",
+ "nigeria": "\ud83c\uddf3\ud83c\uddec",
+ "night_with_stars": "\ud83c\udf03",
+ "nine": "9\ufe0f\u20e3",
+ "ninja": "\ud83e\udd77",
+ "niue": "\ud83c\uddf3\ud83c\uddfa",
+ "no_bell": "\ud83d\udd15",
+ "no_bicycles": "\ud83d\udeb3",
+ "no_entry": "\u26d4",
+ "no_entry_sign": "\ud83d\udeab",
+ "no_good": "\ud83d\ude45",
+ "no_good_man": "\ud83d\ude45\u200d\u2642\ufe0f",
+ "no_good_woman": "\ud83d\ude45\u200d\u2640\ufe0f",
+ "no_mobile_phones": "\ud83d\udcf5",
+ "no_mouth": "\ud83d\ude36",
+ "no_pedestrians": "\ud83d\udeb7",
+ "no_smoking": "\ud83d\udead",
+ "non-potable_water": "\ud83d\udeb1",
+ "norfolk_island": "\ud83c\uddf3\ud83c\uddeb",
+ "north_korea": "\ud83c\uddf0\ud83c\uddf5",
+ "northern_mariana_islands": "\ud83c\uddf2\ud83c\uddf5",
+ "norway": "\ud83c\uddf3\ud83c\uddf4",
+ "nose": "\ud83d\udc43",
+ "notebook": "\ud83d\udcd3",
+ "notebook_with_decorative_cover": "\ud83d\udcd4",
+ "notes": "\ud83c\udfb6",
+ "nut_and_bolt": "\ud83d\udd29",
+ "o": "\u2b55",
+ "o2": "\ud83c\udd7e\ufe0f",
+ "ocean": "\ud83c\udf0a",
+ "octagonal_sign": "\ud83d\uded1",
+ "octopus": "\ud83d\udc19",
+ "oden": "\ud83c\udf62",
+ "office": "\ud83c\udfe2",
+ "office_worker": "\ud83e\uddd1\u200d\ud83d\udcbc",
+ "oil_drum": "\ud83d\udee2\ufe0f",
+ "ok": "\ud83c\udd97",
+ "ok_hand": "\ud83d\udc4c",
+ "ok_man": "\ud83d\ude46\u200d\u2642\ufe0f",
+ "ok_person": "\ud83d\ude46",
+ "ok_woman": "\ud83d\ude46",
+ "old_key": "\ud83d\udddd\ufe0f",
+ "older_adult": "\ud83e\uddd3",
+ "older_man": "\ud83d\udc74",
+ "older_woman": "\ud83d\udc75",
+ "olive": "\ud83e\uded2",
+ "om": "\ud83d\udd49\ufe0f",
+ "om_symbol": "\ud83d\udd49\ufe0f",
+ "oman": "\ud83c\uddf4\ud83c\uddf2",
+ "on": "\ud83d\udd1b",
+ "oncoming_automobile": "\ud83d\ude98",
+ "oncoming_bus": "\ud83d\ude8d",
+ "oncoming_police_car": "\ud83d\ude94",
+ "oncoming_taxi": "\ud83d\ude96",
+ "one": "1\ufe0f\u20e3",
+ "one-piece_swimsuit": "\ud83e\ude71",
+ "one_piece_swimsuit": "\ud83e\ude71",
+ "onion": "\ud83e\uddc5",
+ "open_book": "\ud83d\udcd6",
+ "open_file_folder": "\ud83d\udcc2",
+ "open_hands": "\ud83d\udc50",
+ "open_mouth": "\ud83d\ude2e",
+ "open_umbrella": "\u2602\ufe0f",
+ "ophiuchus": "\u26ce",
+ "orange": "\ud83c\udf4a",
+ "orange_book": "\ud83d\udcd9",
+ "orange_circle": "\ud83d\udfe0",
+ "orange_heart": "\ud83e\udde1",
+ "orange_square": "\ud83d\udfe7",
+ "orangutan": "\ud83e\udda7",
+ "orthodox_cross": "\u2626\ufe0f",
+ "otter": "\ud83e\udda6",
+ "outbox_tray": "\ud83d\udce4",
+ "owl": "\ud83e\udd89",
+ "ox": "\ud83d\udc02",
+ "oyster": "\ud83e\uddaa",
+ "package": "\ud83d\udce6",
+ "page_facing_up": "\ud83d\udcc4",
+ "page_with_curl": "\ud83d\udcc3",
+ "pager": "\ud83d\udcdf",
+ "paintbrush": "\ud83d\udd8c\ufe0f",
+ "pakistan": "\ud83c\uddf5\ud83c\uddf0",
+ "palau": "\ud83c\uddf5\ud83c\uddfc",
+ "palestinian_territories": "\ud83c\uddf5\ud83c\uddf8",
+ "palm_down_hand": "\ud83e\udef3",
+ "palm_tree": "\ud83c\udf34",
+ "palm_up_hand": "\ud83e\udef4",
+ "palms_up_together": "\ud83e\udd32",
+ "panama": "\ud83c\uddf5\ud83c\udde6",
+ "pancakes": "\ud83e\udd5e",
+ "panda_face": "\ud83d\udc3c",
+ "paperclip": "\ud83d\udcce",
+ "paperclips": "\ud83d\udd87\ufe0f",
+ "papua_new_guinea": "\ud83c\uddf5\ud83c\uddec",
+ "parachute": "\ud83e\ude82",
+ "paraguay": "\ud83c\uddf5\ud83c\uddfe",
+ "parasol_on_ground": "\u26f1\ufe0f",
+ "parking": "\ud83c\udd7f\ufe0f",
+ "parrot": "\ud83e\udd9c",
+ "part_alternation_mark": "\u303d\ufe0f",
+ "partly_sunny": "\u26c5",
+ "partly_sunny_rain": "\ud83c\udf26\ufe0f",
+ "partying_face": "\ud83e\udd73",
+ "passenger_ship": "\ud83d\udef3\ufe0f",
+ "passport_control": "\ud83d\udec2",
+ "pause_button": "\u23f8\ufe0f",
+ "paw_prints": "\ud83d\udc3e",
+ "pea_pod": "\ud83e\udedb",
+ "peace_symbol": "\u262e\ufe0f",
+ "peach": "\ud83c\udf51",
+ "peacock": "\ud83e\udd9a",
+ "peanuts": "\ud83e\udd5c",
+ "pear": "\ud83c\udf50",
+ "pen": "\ud83d\udd8a\ufe0f",
+ "pencil": "\ud83d\udcdd",
+ "pencil2": "\u270f\ufe0f",
+ "penguin": "\ud83d\udc27",
+ "pensive": "\ud83d\ude14",
+ "people_holding_hands": "\ud83e\uddd1\u200d\ud83e\udd1d\u200d\ud83e\uddd1",
+ "people_hugging": "\ud83e\udec2",
+ "performing_arts": "\ud83c\udfad",
+ "persevere": "\ud83d\ude23",
+ "person_bald": "\ud83e\uddd1\u200d\ud83e\uddb2",
+ "person_climbing": "\ud83e\uddd7",
+ "person_curly_hair": "\ud83e\uddd1\u200d\ud83e\uddb1",
+ "person_doing_cartwheel": "\ud83e\udd38",
+ "person_feeding_baby": "\ud83e\uddd1\u200d\ud83c\udf7c",
+ "person_fencing": "\ud83e\udd3a",
+ "person_frowning": "\ud83d\ude4d",
+ "person_in_lotus_position": "\ud83e\uddd8",
+ "person_in_manual_wheelchair": "\ud83e\uddd1\u200d\ud83e\uddbd",
+ "person_in_manual_wheelchair_facing_right": "\ud83e\uddd1\u200d\ud83e\uddbd\u200d\u27a1\ufe0f",
+ "person_in_motorized_wheelchair": "\ud83e\uddd1\u200d\ud83e\uddbc",
+ "person_in_motorized_wheelchair_facing_right": "\ud83e\uddd1\u200d\ud83e\uddbc\u200d\u27a1\ufe0f",
+ "person_in_steamy_room": "\ud83e\uddd6",
+ "person_in_tuxedo": "\ud83e\udd35",
+ "person_kneeling_facing_right": "\ud83e\uddce\u200d\u27a1\ufe0f",
+ "person_red_hair": "\ud83e\uddd1\u200d\ud83e\uddb0",
+ "person_running_facing_right": "\ud83c\udfc3\u200d\u27a1\ufe0f",
+ "person_walking_facing_right": "\ud83d\udeb6\u200d\u27a1\ufe0f",
+ "person_white_hair": "\ud83e\uddd1\u200d\ud83e\uddb3",
+ "person_with_ball": "\u26f9\ufe0f",
+ "person_with_blond_hair": "\ud83d\udc71",
+ "person_with_crown": "\ud83e\udec5",
+ "person_with_headscarf": "\ud83e\uddd5",
+ "person_with_pouting_face": "\ud83d\ude4e",
+ "person_with_probing_cane": "\ud83e\uddd1\u200d\ud83e\uddaf",
+ "person_with_turban": "\ud83d\udc73",
+ "person_with_veil": "\ud83d\udc70",
+ "person_with_white_cane_facing_right": "\ud83e\uddd1\u200d\ud83e\uddaf\u200d\u27a1\ufe0f",
+ "peru": "\ud83c\uddf5\ud83c\uddea",
+ "petri_dish": "\ud83e\uddeb",
+ "philippines": "\ud83c\uddf5\ud83c\udded",
+ "phoenix": "\ud83d\udc26\u200d\ud83d\udd25",
+ "phone": "\u260e\ufe0f",
+ "pick": "\u26cf\ufe0f",
+ "pickup_truck": "\ud83d\udefb",
+ "pie": "\ud83e\udd67",
+ "pig": "\ud83d\udc37",
+ "pig2": "\ud83d\udc16",
+ "pig_nose": "\ud83d\udc3d",
+ "pill": "\ud83d\udc8a",
+ "pilot": "\ud83e\uddd1\u200d\u2708\ufe0f",
+ "pinata": "\ud83e\ude85",
+ "pinched_fingers": "\ud83e\udd0c",
+ "pinching_hand": "\ud83e\udd0f",
+ "pineapple": "\ud83c\udf4d",
+ "ping_pong": "\ud83c\udfd3",
+ "pink_heart": "\ud83e\ude77",
+ "pirate_flag": "\ud83c\udff4\u200d\u2620\ufe0f",
+ "pisces": "\u2653",
+ "pitcairn_islands": "\ud83c\uddf5\ud83c\uddf3",
+ "pizza": "\ud83c\udf55",
+ "placard": "\ud83e\udea7",
+ "place_of_worship": "\ud83d\uded0",
+ "plate_with_cutlery": "\ud83c\udf7d\ufe0f",
+ "play_or_pause_button": "\u23ef\ufe0f",
+ "playground_slide": "\ud83d\udedd",
+ "pleading_face": "\ud83e\udd7a",
+ "plunger": "\ud83e\udea0",
+ "point_down": "\ud83d\udc47",
+ "point_left": "\ud83d\udc48",
+ "point_right": "\ud83d\udc49",
+ "point_up": "\u261d\ufe0f",
+ "point_up_2": "\ud83d\udc46",
+ "poland": "\ud83c\uddf5\ud83c\uddf1",
+ "polar_bear": "\ud83d\udc3b\u200d\u2744\ufe0f",
+ "police_car": "\ud83d\ude93",
+ "police_officer": "\ud83d\udc6e",
+ "policeman": "\ud83d\udc6e\u200d\u2642\ufe0f",
+ "policewoman": "\ud83d\udc6e\u200d\u2640\ufe0f",
+ "poodle": "\ud83d\udc29",
+ "poop": "\ud83d\udca9",
+ "popcorn": "\ud83c\udf7f",
+ "portugal": "\ud83c\uddf5\ud83c\uddf9",
+ "post_office": "\ud83c\udfe3",
+ "postal_horn": "\ud83d\udcef",
+ "postbox": "\ud83d\udcee",
+ "potable_water": "\ud83d\udeb0",
+ "potato": "\ud83e\udd54",
+ "potted_plant": "\ud83e\udeb4",
+ "pouch": "\ud83d\udc5d",
+ "poultry_leg": "\ud83c\udf57",
+ "pound": "\ud83d\udcb7",
+ "pouring_liquid": "\ud83e\uded7",
+ "pout": "\ud83d\ude21",
+ "pouting_cat": "\ud83d\ude3e",
+ "pouting_face": "\ud83d\ude4e",
+ "pouting_man": "\ud83d\ude4e\u200d\u2642\ufe0f",
+ "pouting_woman": "\ud83d\ude4e\u200d\u2640\ufe0f",
+ "pray": "\ud83d\ude4f",
+ "prayer_beads": "\ud83d\udcff",
+ "pregnant_man": "\ud83e\udec3",
+ "pregnant_person": "\ud83e\udec4",
+ "pregnant_woman": "\ud83e\udd30",
+ "pretzel": "\ud83e\udd68",
+ "previous_track_button": "\u23ee\ufe0f",
+ "prince": "\ud83e\udd34",
+ "princess": "\ud83d\udc78",
+ "printer": "\ud83d\udda8\ufe0f",
+ "probing_cane": "\ud83e\uddaf",
+ "puerto_rico": "\ud83c\uddf5\ud83c\uddf7",
+ "punch": "\ud83d\udc4a",
+ "purple_circle": "\ud83d\udfe3",
+ "purple_heart": "\ud83d\udc9c",
+ "purple_square": "\ud83d\udfea",
+ "purse": "\ud83d\udc5b",
+ "pushpin": "\ud83d\udccc",
+ "put_litter_in_its_place": "\ud83d\udeae",
+ "qatar": "\ud83c\uddf6\ud83c\udde6",
+ "question": "\u2753",
+ "rabbit": "\ud83d\udc30",
+ "rabbit2": "\ud83d\udc07",
+ "raccoon": "\ud83e\udd9d",
+ "racehorse": "\ud83d\udc0e",
+ "racing_car": "\ud83c\udfce\ufe0f",
+ "racing_motorcycle": "\ud83c\udfcd\ufe0f",
+ "radio": "\ud83d\udcfb",
+ "radio_button": "\ud83d\udd18",
+ "radioactive": "\u2622\ufe0f",
+ "radioactive_sign": "\u2622\ufe0f",
+ "rage": "\ud83d\ude21",
+ "railway_car": "\ud83d\ude83",
+ "railway_track": "\ud83d\udee4\ufe0f",
+ "rain_cloud": "\ud83c\udf27\ufe0f",
+ "rainbow": "\ud83c\udf08",
+ "rainbow-flag": "\ud83c\udff3\ufe0f\u200d\ud83c\udf08",
+ "rainbow_flag": "\ud83c\udff3\ufe0f\u200d\ud83c\udf08",
+ "raised_back_of_hand": "\ud83e\udd1a",
+ "raised_eyebrow": "\ud83e\udd28",
+ "raised_hand": "\u270b",
+ "raised_hand_with_fingers_splayed": "\ud83d\udd90\ufe0f",
+ "raised_hands": "\ud83d\ude4c",
+ "raising_hand": "\ud83d\ude4b",
+ "raising_hand_man": "\ud83d\ude4b\u200d\u2642\ufe0f",
+ "raising_hand_woman": "\ud83d\ude4b\u200d\u2640\ufe0f",
+ "ram": "\ud83d\udc0f",
+ "ramen": "\ud83c\udf5c",
+ "rat": "\ud83d\udc00",
+ "razor": "\ud83e\ude92",
+ "receipt": "\ud83e\uddfe",
+ "record_button": "\u23fa\ufe0f",
+ "recycle": "\u267b\ufe0f",
+ "red_car": "\ud83d\ude97",
+ "red_circle": "\ud83d\udd34",
+ "red_envelope": "\ud83e\udde7",
+ "red_haired_man": "\ud83d\udc68\u200d\ud83e\uddb0",
+ "red_haired_person": "\ud83e\uddd1\u200d\ud83e\uddb0",
+ "red_haired_woman": "\ud83d\udc69\u200d\ud83e\uddb0",
+ "red_square": "\ud83d\udfe5",
+ "registered": "\u00ae\ufe0f",
+ "relaxed": "\u263a\ufe0f",
+ "relieved": "\ud83d\ude0c",
+ "reminder_ribbon": "\ud83c\udf97\ufe0f",
+ "repeat": "\ud83d\udd01",
+ "repeat_one": "\ud83d\udd02",
+ "rescue_worker_helmet": "\u26d1\ufe0f",
+ "restroom": "\ud83d\udebb",
+ "reunion": "\ud83c\uddf7\ud83c\uddea",
+ "reversed_hand_with_middle_finger_extended": "\ud83d\udd95",
+ "revolving_hearts": "\ud83d\udc9e",
+ "rewind": "\u23ea",
+ "rhinoceros": "\ud83e\udd8f",
+ "ribbon": "\ud83c\udf80",
+ "rice": "\ud83c\udf5a",
+ "rice_ball": "\ud83c\udf59",
+ "rice_cracker": "\ud83c\udf58",
+ "rice_scene": "\ud83c\udf91",
+ "right-facing_fist": "\ud83e\udd1c",
+ "right_anger_bubble": "\ud83d\uddef\ufe0f",
+ "rightwards_hand": "\ud83e\udef1",
+ "rightwards_pushing_hand": "\ud83e\udef8",
+ "ring": "\ud83d\udc8d",
+ "ring_buoy": "\ud83d\udedf",
+ "ringed_planet": "\ud83e\ude90",
+ "robot": "\ud83e\udd16",
+ "robot_face": "\ud83e\udd16",
+ "rock": "\ud83e\udea8",
+ "rocket": "\ud83d\ude80",
+ "rofl": "\ud83e\udd23",
+ "roll_eyes": "\ud83d\ude44",
+ "roll_of_paper": "\ud83e\uddfb",
+ "rolled_up_newspaper": "\ud83d\uddde\ufe0f",
+ "roller_coaster": "\ud83c\udfa2",
+ "roller_skate": "\ud83d\udefc",
+ "rolling_on_the_floor_laughing": "\ud83e\udd23",
+ "romania": "\ud83c\uddf7\ud83c\uddf4",
+ "rooster": "\ud83d\udc13",
+ "root_vegetable": "\ud83e\udedc",
+ "rose": "\ud83c\udf39",
+ "rosette": "\ud83c\udff5\ufe0f",
+ "rotating_light": "\ud83d\udea8",
+ "round_pushpin": "\ud83d\udccd",
+ "rowboat": "\ud83d\udea3",
+ "rowing_man": "\ud83d\udea3\u200d\u2642\ufe0f",
+ "rowing_woman": "\ud83d\udea3\u200d\u2640\ufe0f",
+ "ru": "\ud83c\uddf7\ud83c\uddfa",
+ "rugby_football": "\ud83c\udfc9",
+ "runner": "\ud83c\udfc3",
+ "running": "\ud83c\udfc3",
+ "running_man": "\ud83c\udfc3\u200d\u2642\ufe0f",
+ "running_shirt_with_sash": "\ud83c\udfbd",
+ "running_woman": "\ud83c\udfc3\u200d\u2640\ufe0f",
+ "rwanda": "\ud83c\uddf7\ud83c\uddfc",
+ "sa": "\ud83c\ude02\ufe0f",
+ "safety_pin": "\ud83e\uddf7",
+ "safety_vest": "\ud83e\uddba",
+ "sagittarius": "\u2650",
+ "sailboat": "\u26f5",
+ "sake": "\ud83c\udf76",
+ "salt": "\ud83e\uddc2",
+ "saluting_face": "\ud83e\udee1",
+ "samoa": "\ud83c\uddfc\ud83c\uddf8",
+ "san_marino": "\ud83c\uddf8\ud83c\uddf2",
+ "sandal": "\ud83d\udc61",
+ "sandwich": "\ud83e\udd6a",
+ "santa": "\ud83c\udf85",
+ "sao_tome_principe": "\ud83c\uddf8\ud83c\uddf9",
+ "sari": "\ud83e\udd7b",
+ "sassy_man": "\ud83d\udc81\u200d\u2642\ufe0f",
+ "sassy_woman": "\ud83d\udc81\u200d\u2640\ufe0f",
+ "satellite": "\ud83d\udef0\ufe0f",
+ "satellite_antenna": "\ud83d\udce1",
+ "satisfied": "\ud83d\ude06",
+ "saudi_arabia": "\ud83c\uddf8\ud83c\udde6",
+ "sauna_man": "\ud83e\uddd6\u200d\u2642\ufe0f",
+ "sauna_person": "\ud83e\uddd6",
+ "sauna_woman": "\ud83e\uddd6\u200d\u2640\ufe0f",
+ "sauropod": "\ud83e\udd95",
+ "saxophone": "\ud83c\udfb7",
+ "scales": "\u2696\ufe0f",
+ "scarf": "\ud83e\udde3",
+ "school": "\ud83c\udfeb",
+ "school_satchel": "\ud83c\udf92",
+ "scientist": "\ud83e\uddd1\u200d\ud83d\udd2c",
+ "scissors": "\u2702\ufe0f",
+ "scooter": "\ud83d\udef4",
+ "scorpion": "\ud83e\udd82",
+ "scorpius": "\u264f",
+ "scotland": "\ud83c\udff4\udb40\udc67\udb40\udc62\udb40\udc73\udb40\udc63\udb40\udc74\udb40\udc7f",
+ "scream": "\ud83d\ude31",
+ "scream_cat": "\ud83d\ude40",
+ "screwdriver": "\ud83e\ude9b",
+ "scroll": "\ud83d\udcdc",
+ "seal": "\ud83e\uddad",
+ "seat": "\ud83d\udcba",
+ "second_place_medal": "\ud83e\udd48",
+ "secret": "\u3299\ufe0f",
+ "see_no_evil": "\ud83d\ude48",
+ "seedling": "\ud83c\udf31",
+ "selfie": "\ud83e\udd33",
+ "senegal": "\ud83c\uddf8\ud83c\uddf3",
+ "serbia": "\ud83c\uddf7\ud83c\uddf8",
+ "serious_face_with_symbols_covering_mouth": "\ud83e\udd2c",
+ "service_dog": "\ud83d\udc15\u200d\ud83e\uddba",
+ "seven": "7\ufe0f\u20e3",
+ "sewing_needle": "\ud83e\udea1",
+ "seychelles": "\ud83c\uddf8\ud83c\udde8",
+ "shaking_face": "\ud83e\udee8",
+ "shallow_pan_of_food": "\ud83e\udd58",
+ "shamrock": "\u2618\ufe0f",
+ "shark": "\ud83e\udd88",
+ "shaved_ice": "\ud83c\udf67",
+ "sheep": "\ud83d\udc11",
+ "shell": "\ud83d\udc1a",
+ "shield": "\ud83d\udee1\ufe0f",
+ "shinto_shrine": "\u26e9\ufe0f",
+ "ship": "\ud83d\udea2",
+ "shirt": "\ud83d\udc55",
+ "shit": "\ud83d\udca9",
+ "shocked_face_with_exploding_head": "\ud83e\udd2f",
+ "shoe": "\ud83d\udc5e",
+ "shopping": "\ud83d\udecd\ufe0f",
+ "shopping_bags": "\ud83d\udecd\ufe0f",
+ "shopping_cart": "\ud83d\uded2",
+ "shopping_trolley": "\ud83d\uded2",
+ "shorts": "\ud83e\ude73",
+ "shovel": "\ud83e\ude8f",
+ "shower": "\ud83d\udebf",
+ "shrimp": "\ud83e\udd90",
+ "shrug": "\ud83e\udd37",
+ "shushing_face": "\ud83e\udd2b",
+ "sierra_leone": "\ud83c\uddf8\ud83c\uddf1",
+ "sign_of_the_horns": "\ud83e\udd18",
+ "signal_strength": "\ud83d\udcf6",
+ "singapore": "\ud83c\uddf8\ud83c\uddec",
+ "singer": "\ud83e\uddd1\u200d\ud83c\udfa4",
+ "sint_maarten": "\ud83c\uddf8\ud83c\uddfd",
+ "six": "6\ufe0f\u20e3",
+ "six_pointed_star": "\ud83d\udd2f",
+ "skateboard": "\ud83d\udef9",
+ "ski": "\ud83c\udfbf",
+ "skier": "\u26f7\ufe0f",
+ "skull": "\ud83d\udc80",
+ "skull_and_crossbones": "\u2620\ufe0f",
+ "skunk": "\ud83e\udda8",
+ "sled": "\ud83d\udef7",
+ "sleeping": "\ud83d\ude34",
+ "sleeping_accommodation": "\ud83d\udecc",
+ "sleeping_bed": "\ud83d\udecc",
+ "sleepy": "\ud83d\ude2a",
+ "sleuth_or_spy": "\ud83d\udd75\ufe0f",
+ "slightly_frowning_face": "\ud83d\ude41",
+ "slightly_smiling_face": "\ud83d\ude42",
+ "slot_machine": "\ud83c\udfb0",
+ "sloth": "\ud83e\udda5",
+ "slovakia": "\ud83c\uddf8\ud83c\uddf0",
+ "slovenia": "\ud83c\uddf8\ud83c\uddee",
+ "small_airplane": "\ud83d\udee9\ufe0f",
+ "small_blue_diamond": "\ud83d\udd39",
+ "small_orange_diamond": "\ud83d\udd38",
+ "small_red_triangle": "\ud83d\udd3a",
+ "small_red_triangle_down": "\ud83d\udd3b",
+ "smile": "\ud83d\ude04",
+ "smile_cat": "\ud83d\ude38",
+ "smiley": "\ud83d\ude03",
+ "smiley_cat": "\ud83d\ude3a",
+ "smiling_face_with_3_hearts": "\ud83e\udd70",
+ "smiling_face_with_smiling_eyes_and_hand_covering_mouth": "\ud83e\udd2d",
+ "smiling_face_with_tear": "\ud83e\udd72",
+ "smiling_face_with_three_hearts": "\ud83e\udd70",
+ "smiling_imp": "\ud83d\ude08",
+ "smirk": "\ud83d\ude0f",
+ "smirk_cat": "\ud83d\ude3c",
+ "smoking": "\ud83d\udeac",
+ "snail": "\ud83d\udc0c",
+ "snake": "\ud83d\udc0d",
+ "sneezing_face": "\ud83e\udd27",
+ "snow_capped_mountain": "\ud83c\udfd4\ufe0f",
+ "snow_cloud": "\ud83c\udf28\ufe0f",
+ "snowboarder": "\ud83c\udfc2",
+ "snowflake": "\u2744\ufe0f",
+ "snowman": "\u2603\ufe0f",
+ "snowman_with_snow": "\u2603\ufe0f",
+ "snowman_without_snow": "\u26c4",
+ "soap": "\ud83e\uddfc",
+ "sob": "\ud83d\ude2d",
+ "soccer": "\u26bd",
+ "socks": "\ud83e\udde6",
+ "softball": "\ud83e\udd4e",
+ "solomon_islands": "\ud83c\uddf8\ud83c\udde7",
+ "somalia": "\ud83c\uddf8\ud83c\uddf4",
+ "soon": "\ud83d\udd1c",
+ "sos": "\ud83c\udd98",
+ "sound": "\ud83d\udd09",
+ "south_africa": "\ud83c\uddff\ud83c\udde6",
+ "south_georgia_south_sandwich_islands": "\ud83c\uddec\ud83c\uddf8",
+ "south_sudan": "\ud83c\uddf8\ud83c\uddf8",
+ "space_invader": "\ud83d\udc7e",
+ "spades": "\u2660\ufe0f",
+ "spaghetti": "\ud83c\udf5d",
+ "sparkle": "\u2747\ufe0f",
+ "sparkler": "\ud83c\udf87",
+ "sparkles": "\u2728",
+ "sparkling_heart": "\ud83d\udc96",
+ "speak_no_evil": "\ud83d\ude4a",
+ "speaker": "\ud83d\udd08",
+ "speaking_head": "\ud83d\udde3\ufe0f",
+ "speaking_head_in_silhouette": "\ud83d\udde3\ufe0f",
+ "speech_balloon": "\ud83d\udcac",
+ "speedboat": "\ud83d\udea4",
+ "spider": "\ud83d\udd77\ufe0f",
+ "spider_web": "\ud83d\udd78\ufe0f",
+ "spiral_calendar": "\ud83d\uddd3\ufe0f",
+ "spiral_calendar_pad": "\ud83d\uddd3\ufe0f",
+ "spiral_note_pad": "\ud83d\uddd2\ufe0f",
+ "spiral_notepad": "\ud83d\uddd2\ufe0f",
+ "splatter": "\ud83e\udedf",
+ "spock-hand": "\ud83d\udd96",
+ "sponge": "\ud83e\uddfd",
+ "spoon": "\ud83e\udd44",
+ "sports_medal": "\ud83c\udfc5",
+ "squid": "\ud83e\udd91",
+ "sri_lanka": "\ud83c\uddf1\ud83c\uddf0",
+ "st_barthelemy": "\ud83c\udde7\ud83c\uddf1",
+ "st_helena": "\ud83c\uddf8\ud83c\udded",
+ "st_kitts_nevis": "\ud83c\uddf0\ud83c\uddf3",
+ "st_lucia": "\ud83c\uddf1\ud83c\udde8",
+ "st_martin": "\ud83c\uddf2\ud83c\uddeb",
+ "st_pierre_miquelon": "\ud83c\uddf5\ud83c\uddf2",
+ "st_vincent_grenadines": "\ud83c\uddfb\ud83c\udde8",
+ "stadium": "\ud83c\udfdf\ufe0f",
+ "staff_of_aesculapius": "\u2695\ufe0f",
+ "standing_man": "\ud83e\uddcd\u200d\u2642\ufe0f",
+ "standing_person": "\ud83e\uddcd",
+ "standing_woman": "\ud83e\uddcd\u200d\u2640\ufe0f",
+ "star": "\u2b50",
+ "star-struck": "\ud83e\udd29",
+ "star2": "\ud83c\udf1f",
+ "star_and_crescent": "\u262a\ufe0f",
+ "star_of_david": "\u2721\ufe0f",
+ "star_struck": "\ud83e\udd29",
+ "stars": "\ud83c\udf20",
+ "station": "\ud83d\ude89",
+ "statue_of_liberty": "\ud83d\uddfd",
+ "steam_locomotive": "\ud83d\ude82",
+ "stethoscope": "\ud83e\ude7a",
+ "stew": "\ud83c\udf72",
+ "stop_button": "\u23f9\ufe0f",
+ "stop_sign": "\ud83d\uded1",
+ "stopwatch": "\u23f1\ufe0f",
+ "straight_ruler": "\ud83d\udccf",
+ "strawberry": "\ud83c\udf53",
+ "stuck_out_tongue": "\ud83d\ude1b",
+ "stuck_out_tongue_closed_eyes": "\ud83d\ude1d",
+ "stuck_out_tongue_winking_eye": "\ud83d\ude1c",
+ "student": "\ud83e\uddd1\u200d\ud83c\udf93",
+ "studio_microphone": "\ud83c\udf99\ufe0f",
+ "stuffed_flatbread": "\ud83e\udd59",
+ "sudan": "\ud83c\uddf8\ud83c\udde9",
+ "sun_behind_cloud": "\ud83c\udf25\ufe0f",
+ "sun_behind_large_cloud": "\ud83c\udf25\ufe0f",
+ "sun_behind_rain_cloud": "\ud83c\udf26\ufe0f",
+ "sun_behind_small_cloud": "\ud83c\udf24\ufe0f",
+ "sun_small_cloud": "\ud83c\udf24\ufe0f",
+ "sun_with_face": "\ud83c\udf1e",
+ "sunflower": "\ud83c\udf3b",
+ "sunglasses": "\ud83d\ude0e",
+ "sunny": "\u2600\ufe0f",
+ "sunrise": "\ud83c\udf05",
+ "sunrise_over_mountains": "\ud83c\udf04",
+ "superhero": "\ud83e\uddb8",
+ "superhero_man": "\ud83e\uddb8\u200d\u2642\ufe0f",
+ "superhero_woman": "\ud83e\uddb8\u200d\u2640\ufe0f",
+ "supervillain": "\ud83e\uddb9",
+ "supervillain_man": "\ud83e\uddb9\u200d\u2642\ufe0f",
+ "supervillain_woman": "\ud83e\uddb9\u200d\u2640\ufe0f",
+ "surfer": "\ud83c\udfc4",
+ "surfing_man": "\ud83c\udfc4\u200d\u2642\ufe0f",
+ "surfing_woman": "\ud83c\udfc4\u200d\u2640\ufe0f",
+ "suriname": "\ud83c\uddf8\ud83c\uddf7",
+ "sushi": "\ud83c\udf63",
+ "suspension_railway": "\ud83d\ude9f",
+ "svalbard_jan_mayen": "\ud83c\uddf8\ud83c\uddef",
+ "swan": "\ud83e\udda2",
+ "swaziland": "\ud83c\uddf8\ud83c\uddff",
+ "sweat": "\ud83d\ude13",
+ "sweat_drops": "\ud83d\udca6",
+ "sweat_smile": "\ud83d\ude05",
+ "sweden": "\ud83c\uddf8\ud83c\uddea",
+ "sweet_potato": "\ud83c\udf60",
+ "swim_brief": "\ud83e\ude72",
+ "swimmer": "\ud83c\udfca",
+ "swimming_man": "\ud83c\udfca\u200d\u2642\ufe0f",
+ "swimming_woman": "\ud83c\udfca\u200d\u2640\ufe0f",
+ "switzerland": "\ud83c\udde8\ud83c\udded",
+ "symbols": "\ud83d\udd23",
+ "synagogue": "\ud83d\udd4d",
+ "syria": "\ud83c\uddf8\ud83c\uddfe",
+ "syringe": "\ud83d\udc89",
+ "t-rex": "\ud83e\udd96",
+ "table_tennis_paddle_and_ball": "\ud83c\udfd3",
+ "taco": "\ud83c\udf2e",
+ "tada": "\ud83c\udf89",
+ "taiwan": "\ud83c\uddf9\ud83c\uddfc",
+ "tajikistan": "\ud83c\uddf9\ud83c\uddef",
+ "takeout_box": "\ud83e\udd61",
+ "tamale": "\ud83e\uded4",
+ "tanabata_tree": "\ud83c\udf8b",
+ "tangerine": "\ud83c\udf4a",
+ "tanzania": "\ud83c\uddf9\ud83c\uddff",
+ "taurus": "\u2649",
+ "taxi": "\ud83d\ude95",
+ "tea": "\ud83c\udf75",
+ "teacher": "\ud83e\uddd1\u200d\ud83c\udfeb",
+ "teapot": "\ud83e\uded6",
+ "technologist": "\ud83e\uddd1\u200d\ud83d\udcbb",
+ "teddy_bear": "\ud83e\uddf8",
+ "telephone": "\u260e\ufe0f",
+ "telephone_receiver": "\ud83d\udcde",
+ "telescope": "\ud83d\udd2d",
+ "tennis": "\ud83c\udfbe",
+ "tent": "\u26fa",
+ "test_tube": "\ud83e\uddea",
+ "thailand": "\ud83c\uddf9\ud83c\udded",
+ "the_horns": "\ud83e\udd18",
+ "thermometer": "\ud83c\udf21\ufe0f",
+ "thinking": "\ud83e\udd14",
+ "thinking_face": "\ud83e\udd14",
+ "third_place_medal": "\ud83e\udd49",
+ "thong_sandal": "\ud83e\ude74",
+ "thought_balloon": "\ud83d\udcad",
+ "thread": "\ud83e\uddf5",
+ "three": "3\ufe0f\u20e3",
+ "three_button_mouse": "\ud83d\uddb1\ufe0f",
+ "thumbsdown": "\ud83d\udc4e",
+ "thumbsup": "\ud83d\udc4d",
+ "thunder_cloud_and_rain": "\u26c8\ufe0f",
+ "ticket": "\ud83c\udfab",
+ "tickets": "\ud83c\udf9f\ufe0f",
+ "tiger": "\ud83d\udc2f",
+ "tiger2": "\ud83d\udc05",
+ "timer_clock": "\u23f2\ufe0f",
+ "timor_leste": "\ud83c\uddf9\ud83c\uddf1",
+ "tipping_hand_man": "\ud83d\udc81\u200d\u2642\ufe0f",
+ "tipping_hand_person": "\ud83d\udc81",
+ "tipping_hand_woman": "\ud83d\udc81\u200d\u2640\ufe0f",
+ "tired_face": "\ud83d\ude2b",
+ "tm": "\u2122\ufe0f",
+ "togo": "\ud83c\uddf9\ud83c\uddec",
+ "toilet": "\ud83d\udebd",
+ "tokelau": "\ud83c\uddf9\ud83c\uddf0",
+ "tokyo_tower": "\ud83d\uddfc",
+ "tomato": "\ud83c\udf45",
+ "tonga": "\ud83c\uddf9\ud83c\uddf4",
+ "tongue": "\ud83d\udc45",
+ "toolbox": "\ud83e\uddf0",
+ "tooth": "\ud83e\uddb7",
+ "toothbrush": "\ud83e\udea5",
+ "top": "\ud83d\udd1d",
+ "tophat": "\ud83c\udfa9",
+ "tornado": "\ud83c\udf2a\ufe0f",
+ "tornado_cloud": "\ud83c\udf2a\ufe0f",
+ "tr": "\ud83c\uddf9\ud83c\uddf7",
+ "trackball": "\ud83d\uddb2\ufe0f",
+ "tractor": "\ud83d\ude9c",
+ "traffic_light": "\ud83d\udea5",
+ "train": "\ud83d\ude8b",
+ "train2": "\ud83d\ude86",
+ "tram": "\ud83d\ude8a",
+ "transgender_flag": "\ud83c\udff3\ufe0f\u200d\u26a7\ufe0f",
+ "transgender_symbol": "\u26a7\ufe0f",
+ "triangular_flag_on_post": "\ud83d\udea9",
+ "triangular_ruler": "\ud83d\udcd0",
+ "trident": "\ud83d\udd31",
+ "trinidad_tobago": "\ud83c\uddf9\ud83c\uddf9",
+ "tristan_da_cunha": "\ud83c\uddf9\ud83c\udde6",
+ "triumph": "\ud83d\ude24",
+ "troll": "\ud83e\uddcc",
+ "trolleybus": "\ud83d\ude8e",
+ "trophy": "\ud83c\udfc6",
+ "tropical_drink": "\ud83c\udf79",
+ "tropical_fish": "\ud83d\udc20",
+ "truck": "\ud83d\ude9a",
+ "trumpet": "\ud83c\udfba",
+ "tshirt": "\ud83d\udc55",
+ "tulip": "\ud83c\udf37",
+ "tumbler_glass": "\ud83e\udd43",
+ "tunisia": "\ud83c\uddf9\ud83c\uddf3",
+ "turkey": "\ud83e\udd83",
+ "turkmenistan": "\ud83c\uddf9\ud83c\uddf2",
+ "turks_caicos_islands": "\ud83c\uddf9\ud83c\udde8",
+ "turtle": "\ud83d\udc22",
+ "tuvalu": "\ud83c\uddf9\ud83c\uddfb",
+ "tv": "\ud83d\udcfa",
+ "twisted_rightwards_arrows": "\ud83d\udd00",
+ "two": "2\ufe0f\u20e3",
+ "two_hearts": "\ud83d\udc95",
+ "two_men_holding_hands": "\ud83d\udc6c",
+ "two_women_holding_hands": "\ud83d\udc6d",
+ "u5272": "\ud83c\ude39",
+ "u5408": "\ud83c\ude34",
+ "u55b6": "\ud83c\ude3a",
+ "u6307": "\ud83c\ude2f",
+ "u6708": "\ud83c\ude37\ufe0f",
+ "u6709": "\ud83c\ude36",
+ "u6e80": "\ud83c\ude35",
+ "u7121": "\ud83c\ude1a",
+ "u7533": "\ud83c\ude38",
+ "u7981": "\ud83c\ude32",
+ "u7a7a": "\ud83c\ude33",
+ "uganda": "\ud83c\uddfa\ud83c\uddec",
+ "uk": "\ud83c\uddec\ud83c\udde7",
+ "ukraine": "\ud83c\uddfa\ud83c\udde6",
+ "umbrella": "\u2602\ufe0f",
+ "umbrella_on_ground": "\u26f1\ufe0f",
+ "umbrella_with_rain_drops": "\u2614",
+ "unamused": "\ud83d\ude12",
+ "underage": "\ud83d\udd1e",
+ "unicorn": "\ud83e\udd84",
+ "unicorn_face": "\ud83e\udd84",
+ "united_arab_emirates": "\ud83c\udde6\ud83c\uddea",
+ "united_nations": "\ud83c\uddfa\ud83c\uddf3",
+ "unlock": "\ud83d\udd13",
+ "up": "\ud83c\udd99",
+ "upside_down_face": "\ud83d\ude43",
+ "uruguay": "\ud83c\uddfa\ud83c\uddfe",
+ "us": "\ud83c\uddfa\ud83c\uddf8",
+ "us_outlying_islands": "\ud83c\uddfa\ud83c\uddf2",
+ "us_virgin_islands": "\ud83c\uddfb\ud83c\uddee",
+ "uzbekistan": "\ud83c\uddfa\ud83c\uddff",
+ "v": "\u270c\ufe0f",
+ "vampire": "\ud83e\udddb",
+ "vampire_man": "\ud83e\udddb\u200d\u2642\ufe0f",
+ "vampire_woman": "\ud83e\udddb\u200d\u2640\ufe0f",
+ "vanuatu": "\ud83c\uddfb\ud83c\uddfa",
+ "vatican_city": "\ud83c\uddfb\ud83c\udde6",
+ "venezuela": "\ud83c\uddfb\ud83c\uddea",
+ "vertical_traffic_light": "\ud83d\udea6",
+ "vhs": "\ud83d\udcfc",
+ "vibration_mode": "\ud83d\udcf3",
+ "video_camera": "\ud83d\udcf9",
+ "video_game": "\ud83c\udfae",
+ "vietnam": "\ud83c\uddfb\ud83c\uddf3",
+ "violin": "\ud83c\udfbb",
+ "virgo": "\u264d",
+ "volcano": "\ud83c\udf0b",
+ "volleyball": "\ud83c\udfd0",
+ "vomiting_face": "\ud83e\udd2e",
+ "vs": "\ud83c\udd9a",
+ "vulcan_salute": "\ud83d\udd96",
+ "waffle": "\ud83e\uddc7",
+ "wales": "\ud83c\udff4\udb40\udc67\udb40\udc62\udb40\udc77\udb40\udc6c\udb40\udc73\udb40\udc7f",
+ "walking": "\ud83d\udeb6",
+ "walking_man": "\ud83d\udeb6\u200d\u2642\ufe0f",
+ "walking_woman": "\ud83d\udeb6\u200d\u2640\ufe0f",
+ "wallis_futuna": "\ud83c\uddfc\ud83c\uddeb",
+ "waning_crescent_moon": "\ud83c\udf18",
+ "waning_gibbous_moon": "\ud83c\udf16",
+ "warning": "\u26a0\ufe0f",
+ "wastebasket": "\ud83d\uddd1\ufe0f",
+ "watch": "\u231a",
+ "water_buffalo": "\ud83d\udc03",
+ "water_polo": "\ud83e\udd3d",
+ "watermelon": "\ud83c\udf49",
+ "wave": "\ud83d\udc4b",
+ "waving_black_flag": "\ud83c\udff4",
+ "waving_white_flag": "\ud83c\udff3\ufe0f",
+ "wavy_dash": "\u3030\ufe0f",
+ "waxing_crescent_moon": "\ud83c\udf12",
+ "waxing_gibbous_moon": "\ud83c\udf14",
+ "wc": "\ud83d\udebe",
+ "weary": "\ud83d\ude29",
+ "wedding": "\ud83d\udc92",
+ "weight_lifter": "\ud83c\udfcb\ufe0f",
+ "weight_lifting": "\ud83c\udfcb\ufe0f",
+ "weight_lifting_man": "\ud83c\udfcb\ufe0f\u200d\u2642\ufe0f",
+ "weight_lifting_woman": "\ud83c\udfcb\ufe0f\u200d\u2640\ufe0f",
+ "western_sahara": "\ud83c\uddea\ud83c\udded",
+ "whale": "\ud83d\udc33",
+ "whale2": "\ud83d\udc0b",
+ "wheel": "\ud83d\udede",
+ "wheel_of_dharma": "\u2638\ufe0f",
+ "wheelchair": "\u267f",
+ "white_check_mark": "\u2705",
+ "white_circle": "\u26aa",
+ "white_flag": "\ud83c\udff3\ufe0f",
+ "white_flower": "\ud83d\udcae",
+ "white_frowning_face": "\u2639\ufe0f",
+ "white_haired_man": "\ud83d\udc68\u200d\ud83e\uddb3",
+ "white_haired_person": "\ud83e\uddd1\u200d\ud83e\uddb3",
+ "white_haired_woman": "\ud83d\udc69\u200d\ud83e\uddb3",
+ "white_heart": "\ud83e\udd0d",
+ "white_large_square": "\u2b1c",
+ "white_medium_small_square": "\u25fd",
+ "white_medium_square": "\u25fb\ufe0f",
+ "white_small_square": "\u25ab\ufe0f",
+ "white_square_button": "\ud83d\udd33",
+ "wilted_flower": "\ud83e\udd40",
+ "wind_blowing_face": "\ud83c\udf2c\ufe0f",
+ "wind_chime": "\ud83c\udf90",
+ "wind_face": "\ud83c\udf2c\ufe0f",
+ "window": "\ud83e\ude9f",
+ "wine_glass": "\ud83c\udf77",
+ "wing": "\ud83e\udebd",
+ "wink": "\ud83d\ude09",
+ "wireless": "\ud83d\udedc",
+ "wolf": "\ud83d\udc3a",
+ "woman": "\ud83d\udc69",
+ "woman-biking": "\ud83d\udeb4\u200d\u2640\ufe0f",
+ "woman-bouncing-ball": "\u26f9\ufe0f\u200d\u2640\ufe0f",
+ "woman-bowing": "\ud83d\ude47\u200d\u2640\ufe0f",
+ "woman-boy": "\ud83d\udc69\u200d\ud83d\udc66",
+ "woman-boy-boy": "\ud83d\udc69\u200d\ud83d\udc66\u200d\ud83d\udc66",
+ "woman-cartwheeling": "\ud83e\udd38\u200d\u2640\ufe0f",
+ "woman-facepalming": "\ud83e\udd26\u200d\u2640\ufe0f",
+ "woman-frowning": "\ud83d\ude4d\u200d\u2640\ufe0f",
+ "woman-gesturing-no": "\ud83d\ude45\u200d\u2640\ufe0f",
+ "woman-gesturing-ok": "\ud83d\ude46\u200d\u2640\ufe0f",
+ "woman-getting-haircut": "\ud83d\udc87\u200d\u2640\ufe0f",
+ "woman-getting-massage": "\ud83d\udc86\u200d\u2640\ufe0f",
+ "woman-girl": "\ud83d\udc69\u200d\ud83d\udc67",
+ "woman-girl-boy": "\ud83d\udc69\u200d\ud83d\udc67\u200d\ud83d\udc66",
+ "woman-girl-girl": "\ud83d\udc69\u200d\ud83d\udc67\u200d\ud83d\udc67",
+ "woman-golfing": "\ud83c\udfcc\ufe0f\u200d\u2640\ufe0f",
+ "woman-heart-man": "\ud83d\udc69\u200d\u2764\ufe0f\u200d\ud83d\udc68",
+ "woman-heart-woman": "\ud83d\udc69\u200d\u2764\ufe0f\u200d\ud83d\udc69",
+ "woman-juggling": "\ud83e\udd39\u200d\u2640\ufe0f",
+ "woman-kiss-man": "\ud83d\udc69\u200d\u2764\ufe0f\u200d\ud83d\udc8b\u200d\ud83d\udc68",
+ "woman-kiss-woman": "\ud83d\udc69\u200d\u2764\ufe0f\u200d\ud83d\udc8b\u200d\ud83d\udc69",
+ "woman-lifting-weights": "\ud83c\udfcb\ufe0f\u200d\u2640\ufe0f",
+ "woman-mountain-biking": "\ud83d\udeb5\u200d\u2640\ufe0f",
+ "woman-playing-handball": "\ud83e\udd3e\u200d\u2640\ufe0f",
+ "woman-playing-water-polo": "\ud83e\udd3d\u200d\u2640\ufe0f",
+ "woman-pouting": "\ud83d\ude4e\u200d\u2640\ufe0f",
+ "woman-raising-hand": "\ud83d\ude4b\u200d\u2640\ufe0f",
+ "woman-rowing-boat": "\ud83d\udea3\u200d\u2640\ufe0f",
+ "woman-running": "\ud83c\udfc3\u200d\u2640\ufe0f",
+ "woman-shrugging": "\ud83e\udd37\u200d\u2640\ufe0f",
+ "woman-surfing": "\ud83c\udfc4\u200d\u2640\ufe0f",
+ "woman-swimming": "\ud83c\udfca\u200d\u2640\ufe0f",
+ "woman-tipping-hand": "\ud83d\udc81\u200d\u2640\ufe0f",
+ "woman-walking": "\ud83d\udeb6\u200d\u2640\ufe0f",
+ "woman-wearing-turban": "\ud83d\udc73\u200d\u2640\ufe0f",
+ "woman-with-bunny-ears-partying": "\ud83d\udc6f\u200d\u2640\ufe0f",
+ "woman-woman-boy": "\ud83d\udc69\u200d\ud83d\udc69\u200d\ud83d\udc66",
+ "woman-woman-boy-boy": "\ud83d\udc69\u200d\ud83d\udc69\u200d\ud83d\udc66\u200d\ud83d\udc66",
+ "woman-woman-girl": "\ud83d\udc69\u200d\ud83d\udc69\u200d\ud83d\udc67",
+ "woman-woman-girl-boy": "\ud83d\udc69\u200d\ud83d\udc69\u200d\ud83d\udc67\u200d\ud83d\udc66",
+ "woman-woman-girl-girl": "\ud83d\udc69\u200d\ud83d\udc69\u200d\ud83d\udc67\u200d\ud83d\udc67",
+ "woman-wrestling": "\ud83e\udd3c\u200d\u2640\ufe0f",
+ "woman_and_man_holding_hands": "\ud83d\udc6b",
+ "woman_artist": "\ud83d\udc69\u200d\ud83c\udfa8",
+ "woman_astronaut": "\ud83d\udc69\u200d\ud83d\ude80",
+ "woman_beard": "\ud83e\uddd4\u200d\u2640\ufe0f",
+ "woman_cartwheeling": "\ud83e\udd38\u200d\u2640\ufe0f",
+ "woman_climbing": "\ud83e\uddd7\u200d\u2640\ufe0f",
+ "woman_cook": "\ud83d\udc69\u200d\ud83c\udf73",
+ "woman_dancing": "\ud83d\udc83",
+ "woman_facepalming": "\ud83e\udd26\u200d\u2640\ufe0f",
+ "woman_factory_worker": "\ud83d\udc69\u200d\ud83c\udfed",
+ "woman_farmer": "\ud83d\udc69\u200d\ud83c\udf3e",
+ "woman_feeding_baby": "\ud83d\udc69\u200d\ud83c\udf7c",
+ "woman_firefighter": "\ud83d\udc69\u200d\ud83d\ude92",
+ "woman_health_worker": "\ud83d\udc69\u200d\u2695\ufe0f",
+ "woman_in_lotus_position": "\ud83e\uddd8\u200d\u2640\ufe0f",
+ "woman_in_manual_wheelchair": "\ud83d\udc69\u200d\ud83e\uddbd",
+ "woman_in_manual_wheelchair_facing_right": "\ud83d\udc69\u200d\ud83e\uddbd\u200d\u27a1\ufe0f",
+ "woman_in_motorized_wheelchair": "\ud83d\udc69\u200d\ud83e\uddbc",
+ "woman_in_motorized_wheelchair_facing_right": "\ud83d\udc69\u200d\ud83e\uddbc\u200d\u27a1\ufe0f",
+ "woman_in_steamy_room": "\ud83e\uddd6\u200d\u2640\ufe0f",
+ "woman_in_tuxedo": "\ud83e\udd35\u200d\u2640\ufe0f",
+ "woman_judge": "\ud83d\udc69\u200d\u2696\ufe0f",
+ "woman_juggling": "\ud83e\udd39\u200d\u2640\ufe0f",
+ "woman_kneeling": "\ud83e\uddce\u200d\u2640\ufe0f",
+ "woman_kneeling_facing_right": "\ud83e\uddce\u200d\u2640\ufe0f\u200d\u27a1\ufe0f",
+ "woman_mechanic": "\ud83d\udc69\u200d\ud83d\udd27",
+ "woman_office_worker": "\ud83d\udc69\u200d\ud83d\udcbc",
+ "woman_pilot": "\ud83d\udc69\u200d\u2708\ufe0f",
+ "woman_playing_handball": "\ud83e\udd3e\u200d\u2640\ufe0f",
+ "woman_playing_water_polo": "\ud83e\udd3d\u200d\u2640\ufe0f",
+ "woman_running_facing_right": "\ud83c\udfc3\u200d\u2640\ufe0f\u200d\u27a1\ufe0f",
+ "woman_scientist": "\ud83d\udc69\u200d\ud83d\udd2c",
+ "woman_shrugging": "\ud83e\udd37\u200d\u2640\ufe0f",
+ "woman_singer": "\ud83d\udc69\u200d\ud83c\udfa4",
+ "woman_standing": "\ud83e\uddcd\u200d\u2640\ufe0f",
+ "woman_student": "\ud83d\udc69\u200d\ud83c\udf93",
+ "woman_teacher": "\ud83d\udc69\u200d\ud83c\udfeb",
+ "woman_technologist": "\ud83d\udc69\u200d\ud83d\udcbb",
+ "woman_walking_facing_right": "\ud83d\udeb6\u200d\u2640\ufe0f\u200d\u27a1\ufe0f",
+ "woman_with_beard": "\ud83e\uddd4\u200d\u2640\ufe0f",
+ "woman_with_headscarf": "\ud83e\uddd5",
+ "woman_with_probing_cane": "\ud83d\udc69\u200d\ud83e\uddaf",
+ "woman_with_turban": "\ud83d\udc73\u200d\u2640\ufe0f",
+ "woman_with_veil": "\ud83d\udc70\u200d\u2640\ufe0f",
+ "woman_with_white_cane_facing_right": "\ud83d\udc69\u200d\ud83e\uddaf\u200d\u27a1\ufe0f",
+ "womans_clothes": "\ud83d\udc5a",
+ "womans_flat_shoe": "\ud83e\udd7f",
+ "womans_hat": "\ud83d\udc52",
+ "women-with-bunny-ears-partying": "\ud83d\udc6f\u200d\u2640\ufe0f",
+ "women_holding_hands": "\ud83d\udc6d",
+ "women_wrestling": "\ud83e\udd3c\u200d\u2640\ufe0f",
+ "womens": "\ud83d\udeba",
+ "wood": "\ud83e\udeb5",
+ "woozy_face": "\ud83e\udd74",
+ "world_map": "\ud83d\uddfa\ufe0f",
+ "worm": "\ud83e\udeb1",
+ "worried": "\ud83d\ude1f",
+ "wrench": "\ud83d\udd27",
+ "wrestlers": "\ud83e\udd3c",
+ "wrestling": "\ud83e\udd3c",
+ "writing_hand": "\u270d\ufe0f",
+ "x": "\u274c",
+ "x-ray": "\ud83e\ude7b",
+ "x_ray": "\ud83e\ude7b",
+ "yarn": "\ud83e\uddf6",
+ "yawning_face": "\ud83e\udd71",
+ "yellow_circle": "\ud83d\udfe1",
+ "yellow_heart": "\ud83d\udc9b",
+ "yellow_square": "\ud83d\udfe8",
+ "yemen": "\ud83c\uddfe\ud83c\uddea",
+ "yen": "\ud83d\udcb4",
+ "yin_yang": "\u262f\ufe0f",
+ "yo-yo": "\ud83e\ude80",
+ "yo_yo": "\ud83e\ude80",
+ "yum": "\ud83d\ude0b",
+ "zambia": "\ud83c\uddff\ud83c\uddf2",
+ "zany_face": "\ud83e\udd2a",
+ "zap": "\u26a1",
+ "zebra": "\ud83e\udd93",
+ "zebra_face": "\ud83e\udd93",
+ "zero": "0\ufe0f\u20e3",
+ "zimbabwe": "\ud83c\uddff\ud83c\uddfc",
+ "zipper_mouth_face": "\ud83e\udd10",
+ "zombie": "\ud83e\udddf",
+ "zombie_man": "\ud83e\udddf\u200d\u2642\ufe0f",
+ "zombie_woman": "\ud83e\udddf\u200d\u2640\ufe0f",
+ "zzz": "\ud83d\udca4"
+}
diff --git a/crates/slack_integration/src/domain.rs b/crates/slack_integration/src/domain.rs
new file mode 100644
index 0000000000..d9bba28fcb
--- /dev/null
+++ b/crates/slack_integration/src/domain.rs
@@ -0,0 +1,15 @@
+//! Archive import models and inward-facing capability contracts.
+
+pub mod models;
+pub mod slack;
+
+#[cfg(feature = "ports")]
+pub mod importer;
+#[cfg(feature = "ports")]
+pub mod maintenance;
+#[cfg(feature = "ports")]
+pub mod ports;
+#[cfg(feature = "ports")]
+pub mod reference_reconciliation;
+#[cfg(feature = "ports")]
+pub mod service;
diff --git a/crates/slack_integration/src/domain/importer.rs b/crates/slack_integration/src/domain/importer.rs
new file mode 100644
index 0000000000..8a19fa2eea
--- /dev/null
+++ b/crates/slack_integration/src/domain/importer.rs
@@ -0,0 +1,542 @@
+//! Conversation orchestration. No live-message path, transport, SQL or roster
+//! lookup is involved. The driver owns independent lease/visibility heartbeats;
+//! every persistence port must fence writes even while this future is blocked.
+
+use std::collections::{BTreeMap, HashMap, HashSet};
+
+use chrono::{DateTime, Duration, Utc};
+use macro_user_id::user_id::MacroUserIdStr;
+use uuid::Uuid;
+
+use super::{
+ models::*,
+ ports::*,
+ slack::{
+ export::{NormalizedMessage, NormalizedRecord},
+ mrkdwn::MrkdwnConverter,
+ reactions,
+ users::{ResolvedAuthor, UserDirectory},
+ },
+};
+
+mod stream;
+pub mod targets;
+
+use targets::{ImportTargets, TargetFacts, TargetKind, TargetPlan};
+
+impl<R, S, L, T, H, A> ImportWorker for ConversationImporter<R, S, L, T, H, A>
+where
+ R: ExecutionRepo,
+ S: ImportStorage,
+ L: ImportLedger,
+ T: ImportTargets,
+ H: HistoricalSink,
+ A: ImportAuthorizer,
+{
+ async fn claim(&self, event: &ImportEvent, owner: WorkerId) -> PortResult<ClaimOutcome> {
+ self.claim(event, owner).await
+ }
+
+ async fn import(&self, context: &ClaimedConversation) -> PortResult<WorkerOutcome> {
+ self.import(context).await
+ }
+
+ async fn heartbeat(&self, lease: &Lease) -> PortResult<Lease> {
+ self.repo.heartbeat(lease).await
+ }
+}
+
+#[cfg(test)]
+mod test;
+
+/// Importer configuration supplied by the composition root, not the archive.
+pub struct ImporterConfig {
+ /// Effective worker/server bounds.
+ pub limits: ImportLimits,
+}
+
+/// Domain workflow over persistence, storage, ledger, target and authorization ports.
+pub struct ConversationImporter<R, S, L, T, H, A> {
+ repo: R,
+ storage: S,
+ ledger: L,
+ targets: T,
+ sink: H,
+ authorizer: A,
+ limits: ImportLimits,
+}
+
+impl<R, S, L, T, H, A> ConversationImporter<R, S, L, T, H, A> {
+ /// Construct with validated bounds; no environment access in the domain.
+ pub fn new(
+ repo: R,
+ storage: S,
+ ledger: L,
+ targets: T,
+ sink: H,
+ authorizer: A,
+ config: ImporterConfig,
+ ) -> Result<Self, ImportError> {
+ let ImporterConfig { limits } = config;
+ if limits.database_batch_messages == 0
+ || limits.database_batch_messages > ImportLimits::default().database_batch_messages
+ || limits.database_batch_bytes == 0
+ || limits.record_bytes == 0
+ || limits.part_bytes < limits.record_bytes
+ || limits.part_records == 0
+ || limits.json_bytes == 0
+ || limits.selected_bytes < limits.part_bytes.max(limits.json_bytes)
+ {
+ return Err(ImportError::InvalidInput);
+ }
+ Ok(Self {
+ repo,
+ storage,
+ ledger,
+ targets,
+ sink,
+ authorizer,
+ limits,
+ })
+ }
+}
+
+impl<R, S, L, T, H, A> ConversationImporter<R, S, L, T, H, A>
+where
+ R: ExecutionRepo,
+ S: ImportStorage,
+ L: ImportLedger,
+ T: ImportTargets,
+ H: HistoricalSink,
+ A: ImportAuthorizer,
+{
+ /// Revalidate the durable requester BEFORE claiming/reclaiming. An unauthorized
+ /// request cannot acquire a lease; the driver reconciles exhausted deliveries.
+ /// Separating claim lets the driver start heartbeats before reading any objects.
+ pub async fn claim(&self, event: &ImportEvent, owner: WorkerId) -> PortResult<ClaimOutcome> {
+ let Some((team, requester)) = self.repo.requester(event).await? else {
+ return Ok(ClaimOutcome::Obsolete);
+ };
+ self.authorizer.require_admin(team, &requester).await?;
+ self.repo.claim(event, owner).await
+ }
+
+ /// Import one claimed conversation and durably settle success or permanent
+ /// rejection. Transient errors and lost leases are returned for driver recovery;
+ /// already committed history/search work is never rolled back or recounted.
+ pub async fn import(&self, context: &ClaimedConversation) -> PortResult<WorkerOutcome> {
+ let result = self.run(context).await;
+ let (status, error) = match result {
+ Ok(status) => (status, None),
+ Err(error) => {
+ let code = *error.current_context();
+ match code {
+ ImportError::Retryable | ImportError::LeaseLost | ImportError::Internal => {
+ return Err(error);
+ }
+ ImportError::Unavailable | ImportError::Conflict => {
+ // Receipts redact even a previously bound target on reclaim;
+ // keep its internal identity for partial-history search work.
+ self.targets
+ .warn(&context.lease, ImportWarning::TargetUnavailable)
+ .await?;
+ (ConversationStatus::Skipped, Some(ImportError::Unavailable))
+ }
+ _ => (ConversationStatus::Failed, Some(code)),
+ }
+ }
+ };
+ self.repo.settle(&context.lease, status, error).await?;
+ Ok(WorkerOutcome::Acknowledge)
+ }
+
+ async fn run(&self, context: &ClaimedConversation) -> PortResult<ConversationStatus> {
+ self.authorizer
+ .require_admin(context.team_id, &context.requested_by)
+ .await?;
+ if context.lease.event.slack_channel_id != context.metadata.slack_channel_id {
+ return Err(ImportError::InvalidInput.into());
+ }
+ let users = stream::users(&self.storage, &context.users, &self.limits).await?;
+ let mut reader = stream::ArchiveReader::new(&self.storage, context, self.limits)?;
+ // Reading from the beginning also makes missing-time fallback stable on retry.
+ let first = reader.next().await?;
+ let Some(plan) = self
+ .resolve_target(context, &users, first.as_ref().map(|record| record.ts))
+ .await?
+ else {
+ return Ok(ConversationStatus::Skipped);
+ };
+ let channels =
+ BTreeMap::from([(context.metadata.slack_channel_id.clone(), plan.name.clone())]);
+ let converter = MrkdwnConverter {
+ users: &users,
+ channels: &channels,
+ };
+ let mut batch = PendingBatch::new(context);
+ let mut record = first;
+ while let Some(current) = record {
+ if !current.committed {
+ let message = match current.normalized {
+ NormalizedRecord::Message(message) => self.convert(
+ *message,
+ current.order,
+ plan.channel_id,
+ context.team_id,
+ &users,
+ &converter,
+ )?,
+ NormalizedRecord::Skipped(_) => None,
+ };
+ let bytes = message
+ .as_ref()
+ .map(historical_message_bytes)
+ .transpose()?
+ .unwrap_or(0);
+ if bytes > self.limits.database_batch_bytes {
+ return Err(ImportError::LimitExceeded.into());
+ }
+ if batch.records > 0
+ && (batch.records >= self.limits.database_batch_messages
+ || batch.bytes + bytes > self.limits.database_batch_bytes)
+ {
+ self.flush(&mut batch).await?;
+ }
+ batch.records += 1;
+ batch.bytes += bytes;
+ batch.batch.checkpoint = current.next;
+ if let Some(message) = message {
+ batch.batch.messages.push(message);
+ } else {
+ batch.batch.skipped += 1;
+ }
+ }
+ record = reader.next().await?;
+ }
+ self.flush(&mut batch).await?;
+ // Search remains pending in the durable outbox; completion here means
+ // history persistence, not queue publication or OpenSearch refresh.
+ Ok(ConversationStatus::Completed)
+ }
+
+ async fn resolve_target(
+ &self,
+ context: &ClaimedConversation,
+ users: &UserDirectory,
+ first: Option<SlackTimestamp>,
+ ) -> PortResult<Option<TargetPlan>> {
+ let metadata = &context.metadata;
+ let pair = if metadata.kind == ConversationKind::DirectMessage {
+ let Some(pair) = users.direct_message_members(&metadata.member_ids) else {
+ self.targets
+ .warn(&context.lease, ImportWarning::UnresolvableDirectMessage)
+ .await?;
+ return Ok(None);
+ };
+ Some(pair)
+ } else {
+ None
+ };
+ let mut members: HashSet<_> = metadata
+ .member_ids
+ .iter()
+ .filter_map(|id| users.participant(id))
+ .collect();
+ let requester_is_source_member = members.contains(&context.requested_by);
+ let owner = match &pair {
+ Some(pair) => pair
+ .iter()
+ .min_by(|a, b| a.as_ref().cmp(b.as_ref()))
+ .expect("pair")
+ .clone(),
+ None => {
+ members.insert(context.requested_by.clone());
+ context.requested_by.clone()
+ }
+ };
+ let name = if metadata.kind == ConversationKind::GroupDirectMessage {
+ metadata
+ .member_ids
+ .iter()
+ .map(|id| users.display_name(id))
+ .collect::<Vec<_>>()
+ .join(", ")
+ } else {
+ metadata.name.clone()
+ };
+ let mut warnings = Vec::new();
+ let created_at = if let Some(ts) = metadata.created_at {
+ timestamp(ts)?
+ } else if let Some(ts) = first {
+ warnings.push(ImportWarning::CreationTimeFromMessage);
+ timestamp(ts)?
+ } else {
+ warnings.push(ImportWarning::CreationTimeFromJob);
+ context.job_created_at
+ };
+ // Discovery is read-only: do not call an ensure-DM primitive before access
+ // checks, since it can find an unrelated pair or race another creator.
+ let existing = match &pair {
+ Some(pair) => self.targets.find_dm(pair).await?,
+ None => None,
+ };
+ if let Some(facts) = &existing {
+ self.authorize(context, &members, facts).await?;
+ }
+ let reservation = self
+ .ledger
+ .reserve(
+ context.team_id,
+ &context.requested_by,
+ metadata,
+ existing.as_ref().map(|facts| facts.id),
+ )
+ .await?;
+ if reservation.status == ReservationStatus::Conflict
+ || reservation.team_id != context.team_id
+ || reservation.slack_channel_id != metadata.slack_channel_id
+ {
+ return Err(ImportError::Unavailable.into());
+ }
+ let mut plan = TargetPlan {
+ channel_id: reservation.channel_id,
+ metadata: metadata.clone(),
+ team_id: context.team_id,
+ requested_by: context.requested_by.clone(),
+ owner,
+ members,
+ name,
+ created_at,
+ };
+ let created = match self.targets.inspect(plan.channel_id).await? {
+ Some(facts) => {
+ if facts.id != plan.channel_id {
+ return Err(ImportError::Unavailable.into());
+ }
+ self.authorize(context, &plan.members, &facts).await?;
+ false
+ }
+ None if reservation.status == ReservationStatus::Ready => {
+ return Err(ImportError::Unavailable.into());
+ }
+ None => {
+ let (facts, created) = self.targets.create(&context.lease, &plan).await?;
+ if facts.id != plan.channel_id || !compatible(context, &plan.members, &facts) {
+ return Err(ImportError::Unavailable.into());
+ }
+ if !created {
+ self.authorize(context, &plan.members, &facts).await?;
+ }
+ created
+ }
+ };
+ if !created && !requester_is_source_member && pair.is_none() {
+ // Reuse never adds the admin merely to establish ownership/access.
+ plan.members.remove(&context.requested_by);
+ }
+ self.ledger.complete(&reservation, metadata.kind).await?;
+ self.targets.bind(&context.lease, &plan, &warnings).await?;
+ Ok(Some(plan))
+ }
+
+ async fn authorize(
+ &self,
+ context: &ClaimedConversation,
+ members: &HashSet<MacroUserIdStr<'static>>,
+ facts: &TargetFacts,
+ ) -> PortResult<()> {
+ if !compatible(context, members, facts) {
+ return Err(ImportError::Unavailable.into());
+ }
+ self.authorizer
+ .require_target(
+ context.team_id,
+ &context.requested_by,
+ &context.metadata,
+ facts.id,
+ )
+ .await
+ }
+
+ fn convert(
+ &self,
+ message: NormalizedMessage,
+ order: u64,
+ channel: Uuid,
+ team: TeamId,
+ users: &UserDirectory,
+ converter: &MrkdwnConverter<'_>,
+ ) -> PortResult<Option<HistoricalMessage>> {
+ let Some(converted) = converter
+ .message_with_references(&message.content)
+ .map_err(|_| ImportError::LimitExceeded)?
+ else {
+ return Ok(None);
+ };
+ let (sender, imported_author) = match users.author(&message.content) {
+ ResolvedAuthor::User(user) => (HistoricalSender::User(user), None),
+ ResolvedAuthor::SystemBot { imported_author } => {
+ (HistoricalSender::SystemBot, Some(imported_author))
+ }
+ };
+ let parent_ts = message
+ .thread_reference()
+ .parent_lookup()
+ .map(|parent| parent.ts);
+ let reactions = reactions::convert(&message.content.reactions, users, message.identity.ts);
+ Ok(Some(HistoricalMessage {
+ id: Uuid::now_v7(),
+ source: message.identity.in_team(team),
+ channel_id: channel,
+ parent_id: None,
+ orphaned_thread_ts: parent_ts,
+ sender,
+ imported_author,
+ content: converted.body,
+ user_mentions: converted.user_mentions,
+ body_references: converted.references,
+ import_order: order,
+ reactions,
+ }))
+ }
+
+ async fn flush(&self, pending: &mut PendingBatch) -> PortResult<()> {
+ if pending.records == 0 {
+ return Ok(());
+ }
+ let mut sources = HashSet::new();
+ for message in &pending.batch.messages {
+ sources.insert(message.source.clone());
+ if let Some(ts) = message.orphaned_thread_ts {
+ sources.insert(SourceMessageId {
+ ts,
+ ..message.source.clone()
+ });
+ }
+ }
+ let sources: Vec<_> = sources.into_iter().collect();
+ let mut mappings = HashMap::new();
+ for chunk in sources.chunks(self.limits.database_batch_messages as usize) {
+ mappings.extend(self.sink.lookup(chunk).await?);
+ }
+ // Durable IDs beat speculative IDs, including an existing root in this
+ // batch. The atomic sink re-resolves races and must not overwrite live edits.
+ for message in &mut pending.batch.messages {
+ if let Some(id) = mappings.get(&message.source) {
+ message.id = *id;
+ }
+ }
+ for message in &mut pending.batch.messages {
+ if let Some(ts) = message.orphaned_thread_ts {
+ let parent = SourceMessageId {
+ ts,
+ ..message.source.clone()
+ };
+ if let Some(id) = mappings.get(&parent) {
+ message.parent_id = Some(*id);
+ message.orphaned_thread_ts = None;
+ }
+ }
+ mappings.entry(message.source.clone()).or_insert(message.id);
+ }
+ let batch = HistoricalBatch {
+ lease: pending.batch.lease.clone(),
+ messages: std::mem::take(&mut pending.batch.messages),
+ checkpoint: pending.batch.checkpoint,
+ skipped: pending.batch.skipped,
+ };
+ self.sink.commit(batch).await?;
+ pending.batch.skipped = 0;
+ pending.records = 0;
+ pending.bytes = 0;
+ Ok(())
+ }
+}
+
+fn compatible(
+ context: &ClaimedConversation,
+ members: &HashSet<MacroUserIdStr<'static>>,
+ facts: &TargetFacts,
+) -> bool {
+ match (context.metadata.kind, facts.kind) {
+ (ConversationKind::PublicChannel, TargetKind::Team(team)) => team == context.team_id,
+ (
+ ConversationKind::PrivateChannel | ConversationKind::GroupDirectMessage,
+ TargetKind::Private,
+ ) => true,
+ (ConversationKind::DirectMessage, TargetKind::DirectMessage) => {
+ facts.dm_members.len() == 2 && &facts.dm_members == members
+ }
+ _ => false,
+ }
+}
+
+fn timestamp(ts: SlackTimestamp) -> PortResult<DateTime<Utc>> {
+ DateTime::from_timestamp_micros(ts.unix_micros())
+ .ok_or_else(|| ImportError::InvalidInput.into())
+}
+
+struct PendingBatch {
+ batch: HistoricalBatch,
+ records: u32,
+ bytes: u64,
+}
+
+impl PendingBatch {
+ fn new(context: &ClaimedConversation) -> Self {
+ Self {
+ batch: HistoricalBatch {
+ lease: context.lease.clone(),
+ messages: Vec::new(),
+ checkpoint: context.checkpoint,
+ skipped: 0,
+ },
+ records: 0,
+ bytes: 0,
+ }
+ }
+}
+
+/// Serialized message payload bound, also usable by the atomic coordinator.
+/// Includes metadata/reactions and headroom for resolving a pending parent UUID.
+pub fn historical_message_bytes(message: &HistoricalMessage) -> PortResult<u64> {
+ let parent_headroom = if message.parent_id.is_none() && message.orphaned_thread_ts.is_some() {
+ 36
+ } else {
+ 0
+ };
+ serde_json::to_vec(message)
+ .map(|bytes| bytes.len() as u64 + parent_headroom)
+ .map_err(|_| ImportError::Internal.into())
+}
+
+/// Reconcile one durable dirty generation. Persist the ACTUAL service receipt;
+/// repeated submissions are safe because scoped search publication uses upserts.
+/// Completed means durable publication, not eventual consumer indexing/refresh.
+/// Running receipts are not rewritten, preserving their original stale deadline.
+pub async fn reconcile_search(
+ repo: &impl ExecutionRepo,
+ client: &impl SearchBackfillClient,
+ clock: &impl Clock,
+ request: &SearchBackfill,
+) -> PortResult<()> {
+ match request.state {
+ SearchState::NotNeeded | SearchState::Completed => return Ok(()),
+ SearchState::Submitted { receipt_id } => match client.progress(receipt_id).await? {
+ SearchState::Completed => {
+ return repo.record_search(request, SearchState::Completed).await;
+ }
+ SearchState::Submitted { receipt_id: polled } if polled == receipt_id => {
+ if clock.now().signed_duration_since(request.updated_at) < Duration::minutes(30) {
+ return Ok(());
+ }
+ }
+ SearchState::Failed => (),
+ _ => return Err(ImportError::Retryable.into()),
+ },
+ SearchState::Pending | SearchState::Failed => (),
+ }
+ let receipt_id = client.submit(request).await?;
+ repo.record_search(request, SearchState::Submitted { receipt_id })
+ .await
+}
diff --git a/crates/slack_integration/src/domain/importer/stream.rs b/crates/slack_integration/src/domain/importer/stream.rs
new file mode 100644
index 0000000000..f9afbf515f
--- /dev/null
+++ b/crates/slack_integration/src/domain/importer/stream.rs
@@ -0,0 +1,255 @@
+//! Bounded pinned-object readers. Replays validate the prefix too, so ordering
+//! across a resumed part boundary never relies on an in-memory previous worker.
+
+use futures::StreamExt;
+use sha2::{Digest, Sha256};
+
+use crate::domain::{
+ models::*,
+ ports::{ByteStream, ImportStorage, PortResult},
+ slack::{
+ export::{ExportRecord, NormalizedRecord},
+ users::{ExportUser, UserDirectory},
+ },
+};
+
+pub(super) async fn users(
+ storage: &impl ImportStorage,
+ upload: &VerifiedUpload,
+ limits: &ImportLimits,
+) -> PortResult<UserDirectory> {
+ if upload.registered.descriptor.upload != UploadId::Users {
+ return Err(ImportError::InvalidInput.into());
+ }
+ upload
+ .registered
+ .descriptor
+ .validate(limits)
+ .map_err(|_| ImportError::LimitExceeded)?;
+ let mut stream = storage.read(upload).await?;
+ let mut bytes = Vec::new();
+ while let Some(chunk) = stream.next().await {
+ let chunk = chunk?;
+ if bytes.len() as u64 + chunk.len() as u64 > upload.registered.descriptor.byte_length {
+ return Err(ImportError::UploadMismatch.into());
+ }
+ bytes.extend_from_slice(&chunk);
+ }
+ verify(
+ &upload.registered.descriptor,
+ bytes.len() as u64,
+ Sha256::digest(&bytes).into(),
+ )?;
+ let users: Vec<ExportUser> =
+ serde_json::from_slice(&bytes).map_err(|_| ImportError::InvalidInput)?;
+ UserDirectory::new(users).map_err(|_| ImportError::InvalidInput.into())
+}
+
+pub(super) struct Record {
+ pub normalized: NormalizedRecord,
+ pub ts: SlackTimestamp,
+ pub next: Checkpoint,
+ pub order: u64,
+ pub committed: bool,
+}
+
+pub(super) struct ArchiveReader<'a, S> {
+ storage: &'a S,
+ context: &'a ClaimedConversation,
+ limits: ImportLimits,
+ part: Option<PartReader>,
+ index: usize,
+ previous: Option<SlackTimestamp>,
+ order: u64,
+}
+
+impl<'a, S: ImportStorage> ArchiveReader<'a, S> {
+ pub fn new(
+ storage: &'a S,
+ context: &'a ClaimedConversation,
+ limits: ImportLimits,
+ ) -> PortResult<Self> {
+ let mut total = context.users.registered.descriptor.byte_length;
+ for (index, part) in context.parts.iter().enumerate() {
+ let descriptor = &part.registered.descriptor;
+ descriptor
+ .validate(&limits)
+ .map_err(|_| ImportError::LimitExceeded)?;
+ if descriptor.upload
+ != (UploadId::ConversationPart {
+ slack_channel_id: context.metadata.slack_channel_id.clone(),
+ part_index: u32::try_from(index).map_err(|_| ImportError::LimitExceeded)?,
+ })
+ {
+ return Err(ImportError::InvalidInput.into());
+ }
+ total = total
+ .checked_add(descriptor.byte_length)
+ .filter(|n| *n <= limits.selected_bytes)
+ .ok_or(ImportError::LimitExceeded)?;
+ }
+ let checkpoint = context.checkpoint;
+ let valid_checkpoint = if checkpoint.part_index as usize == context.parts.len() {
+ checkpoint.record_index == 0
+ } else {
+ context
+ .parts
+ .get(checkpoint.part_index as usize)
+ .is_some_and(|part| {
+ part.registered
+ .descriptor
+ .record_count
+ .is_some_and(|count| checkpoint.record_index < count)
+ })
+ };
+ if !valid_checkpoint || total > limits.selected_bytes {
+ return Err(ImportError::InvalidInput.into());
+ }
+ Ok(Self {
+ storage,
+ context,
+ limits,
+ part: None,
+ index: 0,
+ previous: None,
+ order: 0,
+ })
+ }
+
+ pub async fn next(&mut self) -> PortResult<Option<Record>> {
+ let Some(upload) = self.context.parts.get(self.index) else {
+ return Ok(None);
+ };
+ if self.part.is_none() {
+ self.part = Some(PartReader::new(
+ self.storage.read(upload).await?,
+ self.limits.record_bytes,
+ ));
+ }
+ let part = self.part.as_mut().expect("opened part");
+ let line = part
+ .line(&upload.registered.descriptor)
+ .await?
+ .ok_or(ImportError::UploadMismatch)?;
+ let position = Checkpoint {
+ part_index: self.index as u32,
+ record_index: part.records - 1,
+ };
+ let record: ExportRecord =
+ serde_json::from_slice(&line).map_err(|_| ImportError::InvalidInput)?;
+ let outer_ts = record.outer.ts;
+ let normalized = record
+ .normalize(self.context.metadata.slack_channel_id.clone())
+ .map_err(|_| ImportError::InvalidInput)?;
+ let ts = match &normalized {
+ NormalizedRecord::Message(message) => message.identity.ts,
+ NormalizedRecord::Skipped(_) => outer_ts.ok_or(ImportError::InvalidInput)?,
+ };
+ if self.previous.is_some_and(|previous| ts <= previous) {
+ return Err(ImportError::InvalidInput.into());
+ }
+ self.previous = Some(ts);
+ let mut next = Checkpoint {
+ part_index: self.index as u32,
+ record_index: part.records,
+ };
+ if Some(part.records) == upload.registered.descriptor.record_count {
+ // Verify exact count, bytes, final LF and digest before checkpointing
+ // the part boundary. Any extra line is an explicit mismatch.
+ if part.line(&upload.registered.descriptor).await?.is_some() {
+ return Err(ImportError::UploadMismatch.into());
+ }
+ self.index += 1;
+ self.part = None;
+ next = Checkpoint {
+ part_index: self.index as u32,
+ record_index: 0,
+ };
+ }
+ let order = self.order;
+ self.order += 1;
+ let checkpoint = self.context.checkpoint;
+ let committed = (position.part_index, position.record_index)
+ < (checkpoint.part_index, checkpoint.record_index);
+ Ok(Some(Record {
+ normalized,
+ ts,
+ next,
+ order,
+ committed,
+ }))
+ }
+}
+
+struct PartReader {
+ stream: ByteStream,
+ chunk: Vec<u8>,
+ offset: usize,
+ bytes: u64,
+ records: u32,
+ hash: Sha256,
+ record_limit: u64,
+}
+
+impl PartReader {
+ fn new(stream: ByteStream, record_limit: u64) -> Self {
+ Self {
+ stream,
+ chunk: Vec::new(),
+ offset: 0,
+ bytes: 0,
+ records: 0,
+ hash: Sha256::new(),
+ record_limit,
+ }
+ }
+
+ async fn line(&mut self, descriptor: &UploadDescriptor) -> PortResult<Option<Vec<u8>>> {
+ let mut line = Vec::new();
+ loop {
+ if self.offset == self.chunk.len() {
+ let Some(chunk) = self.stream.next().await else {
+ if !line.is_empty() || Some(self.records) != descriptor.record_count {
+ return Err(ImportError::UploadMismatch.into());
+ }
+ verify(descriptor, self.bytes, self.hash.clone().finalize().into())?;
+ return Ok(None);
+ };
+ self.chunk = chunk?;
+ self.offset = 0;
+ self.bytes = self
+ .bytes
+ .checked_add(self.chunk.len() as u64)
+ .filter(|n| *n <= descriptor.byte_length)
+ .ok_or(ImportError::UploadMismatch)?;
+ self.hash.update(&self.chunk);
+ }
+ let rest = &self.chunk[self.offset..];
+ let newline = rest.iter().position(|byte| *byte == b'\n');
+ let take = newline.map_or(rest.len(), |index| index + 1);
+ if line.len() as u64 + take as u64 > self.record_limit {
+ return Err(ImportError::LimitExceeded.into());
+ }
+ line.extend_from_slice(&rest[..take]);
+ self.offset += take;
+ if newline.is_some() {
+ self.records = self
+ .records
+ .checked_add(1)
+ .ok_or(ImportError::LimitExceeded)?;
+ if self.records > descriptor.record_count.ok_or(ImportError::InvalidInput)? {
+ return Err(ImportError::UploadMismatch.into());
+ }
+ return Ok(Some(line));
+ }
+ }
+ }
+}
+
+fn verify(descriptor: &UploadDescriptor, bytes: u64, hash: [u8; 32]) -> PortResult<()> {
+ let digest: String = hash.iter().map(|byte| format!("{byte:02x}")).collect();
+ if bytes != descriptor.byte_length || digest != descriptor.sha256.as_str() {
+ return Err(ImportError::UploadMismatch.into());
+ }
+ Ok(())
+}
diff --git a/crates/slack_integration/src/domain/importer/targets.rs b/crates/slack_integration/src/domain/importer/targets.rs
new file mode 100644
index 0000000000..0a5a0c37fb
--- /dev/null
+++ b/crates/slack_integration/src/domain/importer/targets.rs
@@ -0,0 +1,142 @@
+//! Channel capabilities supplied by the worker composition root. Authorization
+//! decisions remain in the importer; adapters recheck them under write locks.
+
+use std::{collections::HashSet, future::Future};
+
+use chrono::{DateTime, Utc};
+use macro_user_id::user_id::MacroUserIdStr;
+use uuid::Uuid;
+
+use crate::domain::{models::*, ports::PortResult};
+
+#[cfg(test)]
+mod test;
+
+/// Actual channel visibility, including the deliberately forbidden public kind.
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub enum TargetKind {
+ /// Team channel; must belong to the importing team.
+ Team(TeamId),
+ /// Private channel; team provenance is held by the ledger, not the channel.
+ Private,
+ /// An exact two-person DM.
+ DirectMessage,
+ /// Never an acceptable archive target.
+ Public,
+}
+
+/// Persisted channel facts, not facts claimed by the archive.
+#[derive(Debug, Clone)]
+pub struct TargetFacts {
+ /// Candidate channel identity. Never return rejected facts in public progress.
+ pub id: Uuid,
+ /// Actual persisted visibility/team.
+ pub kind: TargetKind,
+ /// Complete membership (including leavers) for a DM; empty for other kinds.
+ pub dm_members: HashSet<MacroUserIdStr<'static>>,
+}
+
+/// Requester's current active authority on a reused channel.
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub enum TargetAccess {
+ /// No active membership.
+ None,
+ /// Can participate, but cannot grant new participants access.
+ Participant,
+ /// Can manage participants (channel owner or administrator).
+ ManageParticipants,
+}
+
+/// Archive target policy, applied again to locked facts before grants or writes.
+/// Provenance never overrides type/team compatibility. Private imports can grant
+/// source members access, so ordinary membership alone is insufficient.
+pub fn authorize_target(
+ team: TeamId,
+ source: ConversationKind,
+ target: &TargetFacts,
+ prior_import: bool,
+ access: TargetAccess,
+) -> PortResult<()> {
+ let allowed = match (source, target.kind) {
+ (ConversationKind::PublicChannel, TargetKind::Team(actual)) => actual == team,
+ (
+ ConversationKind::PrivateChannel | ConversationKind::GroupDirectMessage,
+ TargetKind::Private,
+ ) => prior_import || access == TargetAccess::ManageParticipants,
+ (ConversationKind::DirectMessage, TargetKind::DirectMessage) => {
+ target.dm_members.len() == 2 && (prior_import || access != TargetAccess::None)
+ }
+ _ => false,
+ };
+ if allowed {
+ Ok(())
+ } else {
+ Err(ImportError::Unavailable.into())
+ }
+}
+
+/// Silent creation and membership command, after source member resolution.
+#[derive(Debug, Clone)]
+pub struct TargetPlan {
+ /// Canonical reserved identity; creation must not substitute a different DM.
+ pub channel_id: Uuid,
+ /// Source kind and identity used for authorization rechecks.
+ pub metadata: ConversationMetadata,
+ /// Import namespace.
+ pub team_id: TeamId,
+ /// Original requester (not necessarily a participant in a DM).
+ pub requested_by: MacroUserIdStr<'static>,
+ /// Owner on creation only. For DMs this is one of the exact pair.
+ pub owner: MacroUserIdStr<'static>,
+ /// Full deduplicated raw-email membership; includes the requesting owner
+ /// only on new non-DMs. Reuse does not add an owner absent from source members.
+ pub members: HashSet<MacroUserIdStr<'static>>,
+ /// Name on creation only; group DMs use source member display names.
+ pub name: String,
+ /// Historical creation time on creation only.
+ pub created_at: DateTime<Utc>,
+}
+
+/// Channel discovery and fenced silent persistence, implemented at composition.
+/// No method may consult account/roster existence to resolve source membership.
+pub trait ImportTargets: Send + Sync + 'static {
+ /// Read an exact pair without modifying participants or creating a channel.
+ fn find_dm(
+ &self,
+ pair: &[MacroUserIdStr<'static>; 2],
+ ) -> impl Future<Output = PortResult<Option<TargetFacts>>> + Send;
+
+ /// Read actual type/team/membership for a reserved identity.
+ fn inspect(
+ &self,
+ channel: Uuid,
+ ) -> impl Future<Output = PortResult<Option<TargetFacts>>> + Send;
+
+ /// Idempotently create ONLY the reserved target, fenced by the lease. Preserve
+ /// all existing settings/memberships on reuse. Return whether newly created.
+ /// Pair races must fail closed if a different DM wins: never grant provenance
+ /// to an unrelated DM or silently retarget a committed reservation.
+ fn create(
+ &self,
+ lease: &Lease,
+ plan: &TargetPlan,
+ ) -> impl Future<Output = PortResult<(TargetFacts, bool)>> + Send;
+
+ /// Recheck compatibility and authorization under the fence, silently insert
+ /// members without demoting/reactivating existing members, and bind progress to
+ /// this authorized target. No DM participants may be added or removed here.
+ /// Persist warnings idempotently. The channel/ledger operations are recoverable.
+ fn bind(
+ &self,
+ lease: &Lease,
+ plan: &TargetPlan,
+ warnings: &[ImportWarning],
+ ) -> impl Future<Output = PortResult<()>> + Send;
+
+ /// Persist a sanitized resolution warning under the lease without a target ID.
+ fn warn(
+ &self,
+ lease: &Lease,
+ warning: ImportWarning,
+ ) -> impl Future<Output = PortResult<()>> + Send;
+}
diff --git a/crates/slack_integration/src/domain/importer/targets/test.rs b/crates/slack_integration/src/domain/importer/targets/test.rs
new file mode 100644
index 0000000000..1d3647746b
--- /dev/null
+++ b/crates/slack_integration/src/domain/importer/targets/test.rs
@@ -0,0 +1,71 @@
+use super::*;
+
+#[test]
+fn target_authority_never_overrides_visibility_or_namespace() {
+ let team = Uuid::now_v7().try_into().unwrap();
+ let other_team = Uuid::now_v7().try_into().unwrap();
+ let pair = HashSet::from([
+ MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap(),
+ MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap(),
+ ]);
+ for source in [
+ ConversationKind::PublicChannel,
+ ConversationKind::PrivateChannel,
+ ConversationKind::GroupDirectMessage,
+ ConversationKind::DirectMessage,
+ ] {
+ for kind in [
+ TargetKind::Public,
+ TargetKind::Team(team),
+ TargetKind::Team(other_team),
+ TargetKind::Private,
+ TargetKind::DirectMessage,
+ ] {
+ for prior in [false, true] {
+ for access in [
+ TargetAccess::None,
+ TargetAccess::Participant,
+ TargetAccess::ManageParticipants,
+ ] {
+ let expected = match (source, kind) {
+ (ConversationKind::PublicChannel, TargetKind::Team(actual)) => {
+ actual == team
+ }
+ (
+ ConversationKind::PrivateChannel | ConversationKind::GroupDirectMessage,
+ TargetKind::Private,
+ ) => prior || access == TargetAccess::ManageParticipants,
+ (ConversationKind::DirectMessage, TargetKind::DirectMessage) => {
+ prior || access != TargetAccess::None
+ }
+ _ => false,
+ };
+ let facts = TargetFacts {
+ id: Uuid::now_v7(),
+ kind,
+ dm_members: pair.clone(),
+ };
+ assert_eq!(
+ authorize_target(team, source, &facts, prior, access).is_ok(),
+ expected
+ );
+ }
+ }
+ }
+ }
+ let malformed = TargetFacts {
+ id: Uuid::now_v7(),
+ kind: TargetKind::DirectMessage,
+ dm_members: HashSet::new(),
+ };
+ assert!(
+ authorize_target(
+ team,
+ ConversationKind::DirectMessage,
+ &malformed,
+ true,
+ TargetAccess::ManageParticipants
+ )
+ .is_err()
+ );
+}
diff --git a/crates/slack_integration/src/domain/importer/test.rs b/crates/slack_integration/src/domain/importer/test.rs
new file mode 100644
index 0000000000..539ee452cb
--- /dev/null
+++ b/crates/slack_integration/src/domain/importer/test.rs
@@ -0,0 +1,964 @@
+use super::*;
+use sha2::{Digest, Sha256};
+use std::sync::{Arc, Mutex};
+use targets::*;
+
+#[derive(Default)]
+struct State {
+ objects: HashMap<ObjectKey, Vec<u8>>,
+ targets: HashMap<Uuid, TargetFacts>,
+ dm: Option<TargetFacts>,
+ reserved: Option<Uuid>,
+ complete: usize,
+ plans: Vec<TargetPlan>,
+ warnings: Vec<ImportWarning>,
+ created: Vec<TargetPlan>,
+ commits: Vec<HistoricalBatch>,
+ mappings: HashMap<SourceMessageId, Uuid>,
+ stored: HashMap<Uuid, HistoricalMessage>,
+ checkpoint: Checkpoint,
+ counters: ImportCounters,
+ settlements: Vec<(ConversationStatus, Option<ImportError>)>,
+ fail_commit: Option<usize>,
+ admin_revoked: bool,
+ target_denied: bool,
+ claims: usize,
+ requester: Option<(TeamId, MacroUserIdStr<'static>)>,
+ claim: Option<ClaimedConversation>,
+ search_state: Option<SearchState>,
+ search_failure: bool,
+ submissions: Vec<(JobId, Vec<Uuid>, u64)>,
+ receipts: Vec<Uuid>,
+ polls: Vec<Uuid>,
+ recorded_search: Vec<SearchState>,
+}
+
+#[derive(Clone, Default)]
+struct Fake(Arc<Mutex<State>>);
+
+type Importer = ConversationImporter<Fake, Fake, Fake, Fake, Fake, Fake>;
+
+fn user(email: &str) -> MacroUserIdStr<'static> {
+ MacroUserIdStr::try_from_email(email).unwrap()
+}
+fn ts(seconds: u32) -> SlackTimestamp {
+ format!("{seconds}.000001").parse().unwrap()
+}
+fn now() -> DateTime<Utc> {
+ DateTime::from_timestamp(1_800_000_000, 0).unwrap()
+}
+
+fn importer(fake: &Fake, limits: ImportLimits) -> Importer {
+ ConversationImporter::new(
+ fake.clone(),
+ fake.clone(),
+ fake.clone(),
+ fake.clone(),
+ fake.clone(),
+ fake.clone(),
+ ImporterConfig { limits },
+ )
+ .unwrap()
+}
+
+fn upload(fake: &Fake, identity: UploadId, key: &str, bytes: Vec<u8>) -> VerifiedUpload {
+ let record_count = match identity {
+ UploadId::Users => None,
+ _ => Some(bytes.iter().filter(|b| **b == b'\n').count() as u32),
+ };
+ let registered = RegisteredUpload {
+ descriptor: UploadDescriptor {
+ upload: identity,
+ sha256: format!("{:x}", Sha256::digest(&bytes)).parse().unwrap(),
+ byte_length: bytes.len() as u64,
+ record_count,
+ },
+ key: key.parse().unwrap(),
+ };
+ fake.0
+ .lock()
+ .unwrap()
+ .objects
+ .insert(registered.key.clone(), bytes);
+ VerifiedUpload {
+ registered,
+ identity: ObjectIdentity::EntityTag("pinned".parse().unwrap()),
+ }
+}
+
+fn fixture(kind: ConversationKind, parts: &[&str]) -> (Fake, ClaimedConversation) {
+ let fake = Fake::default();
+ let team_id = Uuid::now_v7().try_into().unwrap();
+ let job_id = Uuid::now_v7().try_into().unwrap();
+ let slack_channel_id: ConversationId = "C123".parse().unwrap();
+ let users = upload(&fake, UploadId::Users, "users", serde_json::to_vec(&serde_json::json!([
+ {"id":"U1","name":"alice","profile":{"email":"[email protected]","display_name":"Alice"}},
+ {"id":"U2","name":"bob","profile":{"email":"[email protected]","display_name":"Bob"}},
+ {"id":"U3","name":"phantom"}
+ ])).unwrap());
+ let parts = parts
+ .iter()
+ .enumerate()
+ .map(|(index, bytes)| {
+ upload(
+ &fake,
+ UploadId::ConversationPart {
+ slack_channel_id: slack_channel_id.clone(),
+ part_index: index as u32,
+ },
+ &format!("part-{index}"),
+ bytes.as_bytes().to_vec(),
+ )
+ })
+ .collect();
+ let context = ClaimedConversation {
+ lease: Lease {
+ event: ImportEvent {
+ job_id,
+ slack_channel_id: slack_channel_id.clone(),
+ generation: 1,
+ },
+ owner: Uuid::now_v7().try_into().unwrap(),
+ token: Uuid::now_v7().try_into().unwrap(),
+ generation: 1,
+ expires_at: now() + Duration::minutes(3),
+ heartbeat_at: now(),
+ attempts: 1,
+ },
+ team_id,
+ requested_by: user("[email protected]"),
+ metadata: ConversationMetadata {
+ slack_channel_id,
+ kind,
+ name: "archive".into(),
+ folder: "archive".parse().unwrap(),
+ member_ids: vec!["U1".parse().unwrap(), "U2".parse().unwrap()],
+ creator_id: None,
+ created_at: None,
+ archived: false,
+ message_count: None,
+ },
+ users,
+ parts,
+ checkpoint: Checkpoint::default(),
+ job_created_at: now(),
+ };
+ let mut state = fake.0.lock().unwrap();
+ state.requester = Some((team_id, context.requested_by.clone()));
+ state.claim = Some(context.clone());
+ drop(state);
+ (fake, context)
+}
+
+fn source(context: &ClaimedConversation, seconds: u32) -> SourceMessageId {
+ SourceMessageId {
+ team_id: context.team_id,
+ slack_channel_id: context.metadata.slack_channel_id.clone(),
+ ts: ts(seconds),
+ }
+}
+
+fn facts(plan: &TargetPlan) -> TargetFacts {
+ let kind = match plan.metadata.kind {
+ ConversationKind::PublicChannel => TargetKind::Team(plan.team_id),
+ ConversationKind::PrivateChannel | ConversationKind::GroupDirectMessage => {
+ TargetKind::Private
+ }
+ ConversationKind::DirectMessage => TargetKind::DirectMessage,
+ };
+ TargetFacts {
+ id: plan.channel_id,
+ kind,
+ dm_members: if kind == TargetKind::DirectMessage {
+ plan.members.clone()
+ } else {
+ HashSet::new()
+ },
+ }
+}
+
+impl ImportStorage for Fake {
+ async fn grant(&self, _: &RegisteredUpload) -> PortResult<UploadGrant> {
+ unreachable!()
+ }
+ async fn verify(&self, _: &RegisteredUpload) -> PortResult<VerifiedUpload> {
+ unreachable!()
+ }
+ async fn read(&self, upload: &VerifiedUpload) -> PortResult<ByteStream> {
+ assert!(matches!(upload.identity, ObjectIdentity::EntityTag(_)));
+ let bytes = self.0.lock().unwrap().objects[&upload.registered.key].clone();
+ let chunks: Vec<_> = bytes.chunks(7).map(|chunk| Ok(chunk.to_vec())).collect();
+ Ok(Box::pin(futures::stream::iter(chunks)))
+ }
+}
+
+impl ImportAuthorizer for Fake {
+ async fn require_admin(&self, _: TeamId, _: &MacroUserIdStr<'_>) -> PortResult<()> {
+ if self.0.lock().unwrap().admin_revoked {
+ Err(ImportError::AdminRequired.into())
+ } else {
+ Ok(())
+ }
+ }
+ async fn require_target(
+ &self,
+ _: TeamId,
+ _: &MacroUserIdStr<'_>,
+ _: &ConversationMetadata,
+ _: Uuid,
+ ) -> PortResult<()> {
+ if self.0.lock().unwrap().target_denied {
+ Err(ImportError::Unavailable.into())
+ } else {
+ Ok(())
+ }
+ }
+}
+
+impl ImportLedger for Fake {
+ async fn source_binding(&self, _: TeamId) -> PortResult<SourceBinding> {
+ unreachable!()
+ }
+ async fn reserve(
+ &self,
+ team: TeamId,
+ _: &MacroUserIdStr<'static>,
+ metadata: &ConversationMetadata,
+ existing: Option<Uuid>,
+ ) -> PortResult<TargetReservation> {
+ let mut state = self.0.lock().unwrap();
+ let id = *state
+ .reserved
+ .get_or_insert_with(|| existing.unwrap_or_else(Uuid::now_v7));
+ if existing.is_some_and(|existing| existing != id) {
+ return Err(ImportError::Conflict.into());
+ }
+ Ok(TargetReservation {
+ team_id: team,
+ slack_channel_id: metadata.slack_channel_id.clone(),
+ channel_id: id,
+ status: ReservationStatus::Pending,
+ })
+ }
+ async fn complete(&self, _: &TargetReservation, _: ConversationKind) -> PortResult<()> {
+ self.0.lock().unwrap().complete += 1;
+ Ok(())
+ }
+}
+
+impl ImportTargets for Fake {
+ async fn find_dm(&self, _: &[MacroUserIdStr<'static>; 2]) -> PortResult<Option<TargetFacts>> {
+ Ok(self.0.lock().unwrap().dm.clone())
+ }
+ async fn inspect(&self, id: Uuid) -> PortResult<Option<TargetFacts>> {
+ Ok(self.0.lock().unwrap().targets.get(&id).cloned())
+ }
+ async fn create(&self, _: &Lease, plan: &TargetPlan) -> PortResult<(TargetFacts, bool)> {
+ let mut state = self.0.lock().unwrap();
+ let facts = facts(plan);
+ state.targets.insert(plan.channel_id, facts.clone());
+ state.created.push(plan.clone());
+ Ok((facts, true))
+ }
+ async fn bind(
+ &self,
+ _: &Lease,
+ plan: &TargetPlan,
+ warnings: &[ImportWarning],
+ ) -> PortResult<()> {
+ let mut state = self.0.lock().unwrap();
+ state.plans.push(plan.clone());
+ state.warnings.extend_from_slice(warnings);
+ Ok(())
+ }
+ async fn warn(&self, _: &Lease, warning: ImportWarning) -> PortResult<()> {
+ self.0.lock().unwrap().warnings.push(warning);
+ Ok(())
+ }
+}
+
+impl HistoricalSink for Fake {
+ async fn lookup(
+ &self,
+ sources: &[SourceMessageId],
+ ) -> PortResult<Vec<(SourceMessageId, Uuid)>> {
+ let state = self.0.lock().unwrap();
+ Ok(sources
+ .iter()
+ .filter_map(|source| state.mappings.get(source).map(|id| (source.clone(), *id)))
+ .collect())
+ }
+ async fn commit(&self, batch: HistoricalBatch) -> PortResult<ImportCounters> {
+ let mut state = self.0.lock().unwrap();
+ if state.fail_commit == Some(state.commits.len()) {
+ state.fail_commit = None;
+ return Err(ImportError::Retryable.into());
+ }
+ assert!(
+ (batch.checkpoint.part_index, batch.checkpoint.record_index)
+ > (state.checkpoint.part_index, state.checkpoint.record_index)
+ );
+ state.counters.processed += batch.messages.len() as u64 + batch.skipped;
+ state.counters.skipped += batch.skipped;
+ for message in &batch.messages {
+ if state.mappings.contains_key(&message.source) {
+ state.counters.duplicates += 1;
+ continue;
+ }
+ state.counters.imported += 1;
+ state.counters.reactions += message.reactions.len() as u64;
+ state.mappings.insert(message.source.clone(), message.id);
+ state.stored.insert(message.id, message.clone());
+ }
+ state.checkpoint = batch.checkpoint;
+ state.commits.push(batch);
+ Ok(state.counters)
+ }
+}
+
+impl ExecutionRepo for Fake {
+ async fn requester(
+ &self,
+ _: &ImportEvent,
+ ) -> PortResult<Option<(TeamId, MacroUserIdStr<'static>)>> {
+ Ok(self.0.lock().unwrap().requester.clone())
+ }
+ async fn claim(&self, _: &ImportEvent, _: WorkerId) -> PortResult<ClaimOutcome> {
+ let mut state = self.0.lock().unwrap();
+ state.claims += 1;
+ Ok(ClaimOutcome::Claimed(Box::new(
+ state.claim.clone().unwrap(),
+ )))
+ }
+ async fn heartbeat(&self, _: &Lease) -> PortResult<Lease> {
+ unreachable!()
+ }
+ async fn settle(
+ &self,
+ _: &Lease,
+ status: ConversationStatus,
+ error: Option<ImportError>,
+ ) -> PortResult<()> {
+ self.0.lock().unwrap().settlements.push((status, error));
+ Ok(())
+ }
+ async fn pending_events(&self, _: u32) -> PortResult<Vec<ImportEvent>> {
+ unreachable!()
+ }
+ async fn mark_published(&self, _: &ImportEvent) -> PortResult<()> {
+ unreachable!()
+ }
+ async fn dead_letter(&self, _: &ImportEvent) -> PortResult<WorkerOutcome> {
+ unreachable!()
+ }
+ async fn reconcile(&self, _: u32) -> PortResult<()> {
+ unreachable!()
+ }
+ async fn pending_search(&self, _: u32) -> PortResult<Vec<SearchBackfill>> {
+ unreachable!()
+ }
+ async fn record_search(&self, _: &SearchBackfill, state: SearchState) -> PortResult<()> {
+ self.0.lock().unwrap().recorded_search.push(state);
+ Ok(())
+ }
+}
+
+impl Clock for Fake {
+ fn now(&self) -> DateTime<Utc> {
+ now()
+ }
+}
+impl SearchBackfillClient for Fake {
+ async fn submit(&self, request: &SearchBackfill) -> PortResult<Uuid> {
+ let mut state = self.0.lock().unwrap();
+ if state.search_failure {
+ return Err(ImportError::Retryable.into());
+ }
+ state.submissions.push((
+ request.job_id,
+ request.channel_ids.clone(),
+ request.generation,
+ ));
+ let receipt = Uuid::now_v7();
+ state.receipts.push(receipt);
+ Ok(receipt)
+ }
+ async fn progress(&self, receipt: Uuid) -> PortResult<SearchState> {
+ let mut state = self.0.lock().unwrap();
+ state.polls.push(receipt);
+ Ok(state
+ .search_state
+ .clone()
+ .unwrap_or(SearchState::Submitted {
+ receipt_id: receipt,
+ }))
+ }
+}
+
+#[tokio::test]
+async fn all_four_shapes_resolve_full_members_without_roster_and_zero_history() {
+ for kind in [
+ ConversationKind::PublicChannel,
+ ConversationKind::PrivateChannel,
+ ConversationKind::DirectMessage,
+ ConversationKind::GroupDirectMessage,
+ ] {
+ let (fake, mut context) = fixture(kind, &[]);
+ if kind != ConversationKind::DirectMessage {
+ context.metadata.member_ids.push("U3".parse().unwrap());
+ }
+ importer(&fake, ImportLimits::default())
+ .import(&context)
+ .await
+ .unwrap();
+ let state = fake.0.lock().unwrap();
+ let plan = &state.plans[0];
+ assert!(plan.members.contains(&user("[email protected]")));
+ assert!(plan.members.contains(&user("[email protected]")));
+ assert_eq!(plan.created_at, context.job_created_at);
+ assert!(state.warnings.contains(&ImportWarning::CreationTimeFromJob));
+ if kind == ConversationKind::DirectMessage {
+ assert_eq!(plan.members.len(), 2);
+ assert!(!plan.members.contains(&context.requested_by));
+ assert!(plan.members.contains(&plan.owner));
+ } else {
+ assert_eq!(plan.members.len(), 3);
+ assert_eq!(plan.owner, context.requested_by);
+ }
+ if kind == ConversationKind::GroupDirectMessage {
+ assert_eq!(plan.name, "Alice, Bob, phantom");
+ }
+ assert!(state.commits.is_empty());
+ assert_eq!(state.settlements, [(ConversationStatus::Completed, None)]);
+ }
+}
+
+#[tokio::test]
+async fn invalid_dm_pairs_are_skipped_without_reservation_or_admin_membership() {
+ for members in [vec!["U1", "U3"], vec!["U1", "U1"], vec!["U1", "U2", "U3"]] {
+ let (fake, mut context) = fixture(ConversationKind::DirectMessage, &[]);
+ context.metadata.member_ids = members.iter().map(|id| id.parse().unwrap()).collect();
+ importer(&fake, ImportLimits::default())
+ .import(&context)
+ .await
+ .unwrap();
+ let state = fake.0.lock().unwrap();
+ assert!(state.reserved.is_none());
+ assert_eq!(state.warnings, [ImportWarning::UnresolvableDirectMessage]);
+ assert_eq!(state.settlements[0].0, ConversationStatus::Skipped);
+ }
+}
+
+#[tokio::test]
+async fn unauthorized_dm_discovery_never_reserves_binds_or_exposes_target() {
+ let (fake, context) = fixture(ConversationKind::DirectMessage, &[]);
+ let target = TargetFacts {
+ id: Uuid::now_v7(),
+ kind: TargetKind::DirectMessage,
+ dm_members: HashSet::from([user("[email protected]"), user("[email protected]")]),
+ };
+ {
+ let mut state = fake.0.lock().unwrap();
+ state.dm = Some(target);
+ state.target_denied = true;
+ }
+ importer(&fake, ImportLimits::default())
+ .import(&context)
+ .await
+ .unwrap();
+ let state = fake.0.lock().unwrap();
+ assert!(state.reserved.is_none());
+ assert_eq!(state.warnings, [ImportWarning::TargetUnavailable]);
+ assert!(state.plans.is_empty());
+ assert!(state.created.is_empty());
+ assert_eq!(
+ state.settlements,
+ [(ConversationStatus::Skipped, Some(ImportError::Unavailable))]
+ );
+}
+
+#[tokio::test]
+async fn reused_targets_reject_public_cross_team_wrong_kind_and_denied_private() {
+ let cases = [
+ (ConversationKind::PublicChannel, TargetKind::Public, false),
+ (
+ ConversationKind::PublicChannel,
+ TargetKind::Team(Uuid::now_v7().try_into().unwrap()),
+ false,
+ ),
+ (ConversationKind::PublicChannel, TargetKind::Private, false),
+ (ConversationKind::PrivateChannel, TargetKind::Private, true),
+ ];
+ for (source_kind, target_kind, denied) in cases {
+ let (fake, context) = fixture(source_kind, &[]);
+ let id = Uuid::now_v7();
+ {
+ let mut state = fake.0.lock().unwrap();
+ state.reserved = Some(id);
+ state.targets.insert(
+ id,
+ TargetFacts {
+ id,
+ kind: target_kind,
+ dm_members: HashSet::new(),
+ },
+ );
+ state.target_denied = denied;
+ }
+ importer(&fake, ImportLimits::default())
+ .import(&context)
+ .await
+ .unwrap();
+ let state = fake.0.lock().unwrap();
+ assert!(state.plans.is_empty());
+ assert!(state.created.is_empty());
+ assert_eq!(state.complete, 0);
+ assert_eq!(
+ state.settlements,
+ [(ConversationStatus::Skipped, Some(ImportError::Unavailable))]
+ );
+ }
+}
+
+#[tokio::test]
+async fn authorized_reuse_does_not_recreate_target() {
+ for kind in [
+ ConversationKind::PublicChannel,
+ ConversationKind::PrivateChannel,
+ ConversationKind::DirectMessage,
+ ] {
+ let (fake, context) = fixture(kind, &[]);
+ let id = Uuid::now_v7();
+ let target = TargetFacts {
+ id,
+ kind: match kind {
+ ConversationKind::PublicChannel => TargetKind::Team(context.team_id),
+ ConversationKind::DirectMessage => TargetKind::DirectMessage,
+ _ => TargetKind::Private,
+ },
+ dm_members: HashSet::from([user("[email protected]"), user("[email protected]")]),
+ };
+ {
+ let mut state = fake.0.lock().unwrap();
+ state.reserved = Some(id);
+ state.targets.insert(id, target.clone());
+ if kind == ConversationKind::DirectMessage {
+ state.dm = Some(target);
+ }
+ }
+ importer(&fake, ImportLimits::default())
+ .import(&context)
+ .await
+ .unwrap();
+ let state = fake.0.lock().unwrap();
+ assert!(state.created.is_empty());
+ assert_eq!(state.plans[0].channel_id, id);
+ assert!(!state.plans[0].members.contains(&context.requested_by));
+ }
+}
+
+#[tokio::test]
+async fn admin_revocation_prevents_claim_and_rechecks_already_claimed_work() {
+ let (fake, context) = fixture(ConversationKind::PublicChannel, &[]);
+ fake.0.lock().unwrap().admin_revoked = true;
+ let importer = importer(&fake, ImportLimits::default());
+ assert_eq!(
+ importer
+ .claim(&context.lease.event, context.lease.owner)
+ .await
+ .unwrap_err()
+ .into_current_context(),
+ ImportError::AdminRequired
+ );
+ importer.import(&context).await.unwrap();
+ let state = fake.0.lock().unwrap();
+ assert_eq!(state.claims, 0);
+ assert!(state.created.is_empty());
+ assert_eq!(
+ state.settlements,
+ [(ConversationStatus::Failed, Some(ImportError::AdminRequired))]
+ );
+}
+
+#[tokio::test]
+async fn native_conversion_persists_safe_templates_and_only_active_mentions() {
+ let parts = [concat!(
+ r#"{"ts":"1.000001","text":"<#C1|self> <#C2|forward> <@U1> `<@U1>` https://example.slack.com/archives/C2/p2000001"}"#,
+ "\n",
+ )];
+ let (fake, context) = fixture(ConversationKind::PublicChannel, &parts);
+ importer(&fake, ImportLimits::default())
+ .import(&context)
+ .await
+ .unwrap();
+ let state = fake.0.lock().unwrap();
+ let message = &state.stored[&state.mappings[&source(&context, 1)]];
+ assert_eq!(message.user_mentions, vec![user("[email protected]")]);
+ assert_eq!(message.body_references.len(), 3);
+ assert!(message.content.contains("<m-link>"));
+ assert!(!message.content.contains("m-document-mention"));
+ let template = crate::domain::slack::references::ConvertedText {
+ body: message.content.clone(),
+ user_mentions: message.user_mentions.clone(),
+ references: message.body_references.clone(),
+ };
+ assert_eq!(template.render(&[]).unwrap(), message.content);
+}
+
+#[tokio::test]
+async fn conversion_threads_skipped_roots_and_source_metadata_survive_bounded_batches() {
+ let parts = [
+ "{\"ts\":\"1.000001\",\"text\":\"root\",\"user\":\"U1\",\"replies\":[{\"ts\":\"3.000001\"}]}\n{\"ts\":\"2.000001\",\"text\":\"\",\"files\":[{}]}\n",
+ "{\"ts\":\"3.000001\",\"thread_ts\":\"1.000001\",\"text\":\"*reply*\",\"username\":\"Archive Bot\",\"subtype\":\"bot_message\",\"reactions\":[{\"name\":\"thumbsup\",\"users\":[\"U1\",\"U1\",\"U3\"]}]}\n{\"ts\":\"4.000001\",\"thread_ts\":\"2.000001\",\"text\":\"orphan\"}\n{\"ts\":\"5.000001\",\"text\":\"not a reply\"}\n",
+ ];
+ for batch_size in [1, 5] {
+ let (fake, context) = fixture(ConversationKind::PublicChannel, &parts);
+ let limits = ImportLimits {
+ database_batch_messages: batch_size,
+ ..ImportLimits::default()
+ };
+ importer(&fake, limits).import(&context).await.unwrap();
+ let state = fake.0.lock().unwrap();
+ let message = |seconds| &state.stored[&state.mappings[&source(&context, seconds)]];
+ assert_eq!(message(3).parent_id, Some(message(1).id));
+ assert_eq!(message(3).content, "**reply**");
+ assert_eq!(message(3).imported_author.as_deref(), Some("Archive Bot"));
+ assert_eq!(message(3).sender, HistoricalSender::SystemBot);
+ assert_eq!(message(3).reactions.len(), 1);
+ assert_eq!(message(3).reactions[0].created_at, ts(3));
+ assert_eq!(message(4).orphaned_thread_ts, Some(ts(2)));
+ assert_eq!(message(4).parent_id, None);
+ assert_eq!(message(5).parent_id, None);
+ assert_eq!(message(1).source, source(&context, 1));
+ assert_eq!(
+ message(1).sender,
+ HistoricalSender::User(user("[email protected]"))
+ );
+ assert_eq!(message(1).imported_author, None);
+ assert_eq!(message(3).import_order, 2);
+ assert_eq!(
+ state.counters,
+ ImportCounters {
+ processed: 5,
+ imported: 4,
+ skipped: 1,
+ reactions: 1,
+ duplicates: 0
+ }
+ );
+ assert_eq!(
+ state.checkpoint,
+ Checkpoint {
+ part_index: 2,
+ record_index: 0
+ }
+ );
+ assert_eq!(state.plans[0].created_at, timestamp(ts(1)).unwrap());
+ assert!(
+ state
+ .warnings
+ .contains(&ImportWarning::CreationTimeFromMessage)
+ );
+ assert!(
+ state
+ .commits
+ .iter()
+ .all(|batch| batch.messages.len() <= batch_size as usize)
+ );
+ }
+}
+
+#[tokio::test]
+async fn cross_job_parent_and_duplicate_mapping_win_without_overwriting_live_edits() {
+ let (fake, context) = fixture(
+ ConversationKind::PrivateChannel,
+ &["{\"ts\":\"1.000001\",\"text\":\"original\"}\n"],
+ );
+ let importer = importer(&fake, ImportLimits::default());
+ importer.import(&context).await.unwrap();
+ let root = fake.0.lock().unwrap().mappings[&source(&context, 1)];
+ fake.0
+ .lock()
+ .unwrap()
+ .stored
+ .get_mut(&root)
+ .unwrap()
+ .content = "live edit".into();
+ let mut second = context.clone();
+ second.lease.event.job_id = Uuid::now_v7().try_into().unwrap();
+ second.parts = vec![upload(&fake, UploadId::ConversationPart { slack_channel_id: second.metadata.slack_channel_id.clone(), part_index: 0 }, "later", b"{\"ts\":\"1.000001\",\"text\":\"export edit\"}\n{\"ts\":\"2.000001\",\"thread_ts\":\"1.000001\",\"text\":\"reply\"}\n".to_vec())];
+ {
+ let mut state = fake.0.lock().unwrap();
+ state.checkpoint = Checkpoint::default();
+ state.counters = ImportCounters::default();
+ }
+ importer.import(&second).await.unwrap();
+ let state = fake.0.lock().unwrap();
+ assert_eq!(state.stored[&root].content, "live edit");
+ assert_eq!(
+ state.stored[&state.mappings[&source(&context, 2)]].parent_id,
+ Some(root)
+ );
+ assert_eq!(state.counters.imported, 1);
+ assert_eq!(state.counters.duplicates, 1);
+}
+
+#[tokio::test]
+async fn parent_from_an_earlier_job_need_not_be_present_in_the_current_archive() {
+ let (fake, context) = fixture(
+ ConversationKind::PrivateChannel,
+ &["{\"ts\":\"2.000001\",\"thread_ts\":\"1.000001\",\"text\":\"reply\"}\n"],
+ );
+ let root = Uuid::now_v7();
+ fake.0
+ .lock()
+ .unwrap()
+ .mappings
+ .insert(source(&context, 1), root);
+ importer(&fake, ImportLimits::default())
+ .import(&context)
+ .await
+ .unwrap();
+ let state = fake.0.lock().unwrap();
+ let reply = &state.stored[&state.mappings[&source(&context, 2)]];
+ assert_eq!(reply.parent_id, Some(root));
+ assert_eq!(reply.orphaned_thread_ts, None);
+ assert_eq!(state.counters.imported, 1);
+}
+
+#[tokio::test]
+async fn retry_after_committed_batch_replays_prefix_without_double_counting() {
+ let (fake, mut context) = fixture(
+ ConversationKind::PublicChannel,
+ &[
+ "{\"ts\":\"1.000001\",\"text\":\"root\"}\n{\"ts\":\"2.000001\",\"thread_ts\":\"1.000001\",\"text\":\"reply\"}\n{\"ts\":\"3.000001\",\"text\":\"last\"}\n",
+ ],
+ );
+ let importer = importer(
+ &fake,
+ ImportLimits {
+ database_batch_messages: 1,
+ ..ImportLimits::default()
+ },
+ );
+ fake.0.lock().unwrap().fail_commit = Some(1);
+ assert_eq!(
+ importer
+ .import(&context)
+ .await
+ .unwrap_err()
+ .into_current_context(),
+ ImportError::Retryable
+ );
+ {
+ let state = fake.0.lock().unwrap();
+ assert_eq!(state.counters.imported, 1);
+ assert!(state.settlements.is_empty());
+ context.checkpoint = state.checkpoint;
+ }
+ context.lease.generation += 1;
+ importer.import(&context).await.unwrap();
+ let state = fake.0.lock().unwrap();
+ assert_eq!(state.counters.processed, 3);
+ assert_eq!(state.counters.imported, 3);
+ assert_eq!(state.counters.duplicates, 0);
+ let root = state.mappings[&source(&context, 1)];
+ assert_eq!(
+ state.stored[&state.mappings[&source(&context, 2)]].parent_id,
+ Some(root)
+ );
+ assert_eq!(state.created.len(), 1);
+}
+
+#[tokio::test]
+async fn monotonic_normalized_time_is_validated_across_parts_and_replayed_prefix() {
+ for checkpoint in [
+ Checkpoint::default(),
+ Checkpoint {
+ part_index: 1,
+ record_index: 0,
+ },
+ ] {
+ for second in [
+ "{\"ts\":\"1.000001\",\"text\":\"duplicate\"}\n",
+ "{\"ts\":\"9.000001\",\"subtype\":\"message_changed\",\"message\":{\"ts\":\"0.000001\",\"text\":\"older inner identity\"}}\n",
+ ] {
+ let (fake, mut context) = fixture(
+ ConversationKind::PublicChannel,
+ &["{\"ts\":\"1.000001\",\"text\":\"root\"}\n", second],
+ );
+ context.checkpoint = checkpoint;
+ importer(&fake, ImportLimits::default())
+ .import(&context)
+ .await
+ .unwrap();
+ let state = fake.0.lock().unwrap();
+ assert_eq!(
+ state.settlements[0],
+ (ConversationStatus::Failed, Some(ImportError::InvalidInput))
+ );
+ assert!(state.commits.is_empty());
+ }
+ }
+}
+
+#[tokio::test]
+async fn byte_record_manifest_and_digest_limits_are_rechecked() {
+ for case in 0..11 {
+ let (fake, mut context) = fixture(
+ ConversationKind::PublicChannel,
+ &["{\"ts\":\"1.000001\",\"text\":\"hello\"}\n"],
+ );
+ let mut limits = ImportLimits::default();
+ match case {
+ 0 => limits.record_bytes = 10,
+ 1 => limits.part_bytes = 10,
+ 2 => context.parts[0].registered.descriptor.record_count = Some(2),
+ 3 => context.parts[0].registered.descriptor.sha256 = "0".repeat(64).parse().unwrap(),
+ 4 => context.parts[0].registered.descriptor.byte_length += 1,
+ 5 => {
+ context.parts[0].registered.descriptor.upload = UploadId::ConversationPart {
+ slack_channel_id: "COTHER".parse().unwrap(),
+ part_index: 0,
+ }
+ }
+ 6 => limits.database_batch_bytes = 1,
+ 7 => {
+ let mut state = fake.0.lock().unwrap();
+ state
+ .objects
+ .get_mut(&context.users.registered.key)
+ .unwrap()
+ .push(b' ');
+ }
+ 8 => context.parts[0].registered.descriptor.record_count = Some(20_001),
+ 9 => limits.json_bytes = 10,
+ 10 => {
+ let key = &context.parts[0].registered.key;
+ fake.0.lock().unwrap().objects.get_mut(key).unwrap().pop();
+ context.parts[0].registered.descriptor.byte_length -= 1;
+ }
+ _ => unreachable!(),
+ }
+ if case == 1 {
+ limits.record_bytes = 10;
+ }
+ importer(&fake, limits).import(&context).await.unwrap();
+ let state = fake.0.lock().unwrap();
+ assert_eq!(
+ state.settlements[0].0,
+ ConversationStatus::Failed,
+ "case {case}"
+ );
+ assert!(state.commits.is_empty());
+ }
+}
+
+#[tokio::test]
+async fn skipped_only_records_checkpoint_and_byte_ceiling_split_batches() {
+ let (fake, context) = fixture(
+ ConversationKind::PublicChannel,
+ &[
+ "{\"ts\":\"1.000001\",\"text\":\"\"}\n{\"ts\":\"2.000001\",\"subtype\":\"channel_join\"}\n",
+ ],
+ );
+ importer(
+ &fake,
+ ImportLimits {
+ database_batch_messages: 1,
+ ..ImportLimits::default()
+ },
+ )
+ .import(&context)
+ .await
+ .unwrap();
+ {
+ let state = fake.0.lock().unwrap();
+ assert_eq!(state.commits.len(), 2);
+ assert_eq!(state.counters.skipped, 2);
+ }
+ let (fake, context) = fixture(
+ ConversationKind::PublicChannel,
+ &["{\"ts\":\"1.000001\",\"text\":\"one\"}\n{\"ts\":\"2.000001\",\"text\":\"two\"}\n"],
+ );
+ importer(
+ &fake,
+ ImportLimits {
+ database_batch_bytes: 700,
+ ..ImportLimits::default()
+ },
+ )
+ .import(&context)
+ .await
+ .unwrap();
+ let state = fake.0.lock().unwrap();
+ assert_eq!(state.commits.len(), 2);
+ assert!(state.commits.iter().all(|batch| {
+ batch
+ .messages
+ .iter()
+ .map(|message| historical_message_bytes(message).unwrap())
+ .sum::<u64>()
+ <= 700
+ }));
+}
+
+#[tokio::test]
+async fn failed_missing_and_stale_search_receipts_retry_and_only_publication_completes() {
+ let fake = Fake::default();
+ let mut request = SearchBackfill {
+ job_id: Uuid::now_v7().try_into().unwrap(),
+ channel_ids: vec![Uuid::now_v7()],
+ generation: 7,
+ state: SearchState::Pending,
+ updated_at: now(),
+ };
+ fake.0.lock().unwrap().search_failure = true;
+ assert!(
+ reconcile_search(&fake, &fake, &fake, &request)
+ .await
+ .is_err()
+ );
+ assert!(fake.0.lock().unwrap().recorded_search.is_empty());
+ fake.0.lock().unwrap().search_failure = false;
+ reconcile_search(&fake, &fake, &fake, &request)
+ .await
+ .unwrap();
+ let receipt = fake.0.lock().unwrap().receipts[0];
+ assert_eq!(
+ fake.0.lock().unwrap().recorded_search,
+ [SearchState::Submitted {
+ receipt_id: receipt
+ }]
+ );
+ request.state = SearchState::Submitted {
+ receipt_id: receipt,
+ };
+ reconcile_search(&fake, &fake, &fake, &request)
+ .await
+ .unwrap();
+ assert_eq!(fake.0.lock().unwrap().recorded_search.len(), 1);
+ request.updated_at = now() - Duration::minutes(31);
+ reconcile_search(&fake, &fake, &fake, &request)
+ .await
+ .unwrap();
+ assert_eq!(fake.0.lock().unwrap().receipts.len(), 2);
+ fake.0.lock().unwrap().search_state = Some(SearchState::Failed);
+ reconcile_search(&fake, &fake, &fake, &request)
+ .await
+ .unwrap();
+ assert_eq!(fake.0.lock().unwrap().receipts.len(), 3);
+ fake.0.lock().unwrap().search_state = Some(SearchState::Completed);
+ reconcile_search(&fake, &fake, &fake, &request)
+ .await
+ .unwrap();
+ let state = fake.0.lock().unwrap();
+ assert_eq!(state.recorded_search.last(), Some(&SearchState::Completed));
+ assert!(state.polls.iter().all(|id| *id == receipt));
+ assert!(
+ state
+ .submissions
+ .iter()
+ .all(|scope| scope == &(request.job_id, request.channel_ids.clone(), 7))
+ );
+}
diff --git a/crates/slack_integration/src/domain/maintenance.rs b/crates/slack_integration/src/domain/maintenance.rs
new file mode 100644
index 0000000000..8c2a66f161
--- /dev/null
+++ b/crates/slack_integration/src/domain/maintenance.rs
@@ -0,0 +1,85 @@
+//! Durable publication and recovery policy, independent of the queue transport.
+
+use std::future::Future;
+
+use super::{importer::reconcile_search, models::*, ports::*};
+
+#[cfg(test)]
+mod test;
+
+const PAGE_SIZE: u32 = 50;
+
+/// Recovery use cases remain available when new claims/publication are disabled.
+pub trait Maintenance: Send + Sync {
+ /// Publish a bounded outbox page, acknowledging only positively accepted sends.
+ fn publish(&self) -> impl Future<Output = PortResult<()>> + Send;
+ /// Recover expired work and reconcile partial-history search receipts.
+ fn reconcile(&self) -> impl Future<Output = PortResult<()>> + Send;
+ /// Persist exhausted work before allowing deletion; defer while a fence is active.
+ fn dead_letter(
+ &self,
+ event: &ImportEvent,
+ ) -> impl Future<Output = PortResult<WorkerOutcome>> + Send;
+}
+
+/// Domain service for outbox delivery, cancellation settlement and search recovery.
+pub struct ImportMaintenance<R, Q, S, C, F> {
+ repo: R,
+ queue: Q,
+ search: S,
+ clock: C,
+ references: F,
+}
+
+impl<R, Q, S, C, F> ImportMaintenance<R, Q, S, C, F> {
+ /// Compose only domain capabilities; configuration is supplied by the application.
+ pub fn new(repo: R, queue: Q, search: S, clock: C, references: F) -> Self {
+ Self {
+ repo,
+ queue,
+ search,
+ clock,
+ references,
+ }
+ }
+}
+
+impl<R: ExecutionRepo, Q: ImportQueue, S: SearchBackfillClient, C: Clock, F: ReferenceReconciler>
+ Maintenance for ImportMaintenance<R, Q, S, C, F>
+{
+ async fn publish(&self) -> PortResult<()> {
+ let mut failure = None;
+ for event in self.repo.pending_events(PAGE_SIZE).await? {
+ let result = async {
+ self.queue.publish(&event).await?;
+ self.repo.mark_published(&event).await
+ }
+ .await;
+ if let Err(error) = result {
+ // Never log the report: provider diagnostics can contain object keys/content.
+ tracing::warn!(code = ?error.current_context(), operation = "outbox", "slack import retry");
+ failure = Some(error);
+ }
+ }
+ failure.map_or(Ok(()), Err)
+ }
+
+ async fn reconcile(&self) -> PortResult<()> {
+ self.repo.reconcile(PAGE_SIZE).await?;
+ // Search recovery must continue even if one reference batch needs retry.
+ let mut failure = self.references.reconcile_references(PAGE_SIZE).await.err();
+ for request in self.repo.pending_search(PAGE_SIZE).await? {
+ if let Err(error) =
+ reconcile_search(&self.repo, &self.search, &self.clock, &request).await
+ {
+ tracing::warn!(code = ?error.current_context(), operation = "search", "slack import retry");
+ failure = Some(error);
+ }
+ }
+ failure.map_or(Ok(()), Err)
+ }
+
+ async fn dead_letter(&self, event: &ImportEvent) -> PortResult<WorkerOutcome> {
+ self.repo.dead_letter(event).await
+ }
+}
diff --git a/crates/slack_integration/src/domain/maintenance/test.rs b/crates/slack_integration/src/domain/maintenance/test.rs
new file mode 100644
index 0000000000..066f6cc6ad
--- /dev/null
+++ b/crates/slack_integration/src/domain/maintenance/test.rs
@@ -0,0 +1,202 @@
+use super::*;
+use chrono::{DateTime, Utc};
+use macro_user_id::user_id::MacroUserIdStr;
+use std::sync::{Arc, Mutex};
+use uuid::Uuid;
+
+#[derive(Default)]
+struct State {
+ pending: Vec<ImportEvent>,
+ sent: Vec<ImportEvent>,
+ marked: Vec<ImportEvent>,
+ send_fails: bool,
+ mark_fails: bool,
+ reconciled: usize,
+ references_reconciled: usize,
+ active: bool,
+ failed: bool,
+ searches: Vec<SearchBackfill>,
+ search_states: Vec<SearchState>,
+}
+
+#[derive(Clone, Default)]
+struct Fake(Arc<Mutex<State>>);
+
+impl ExecutionRepo for Fake {
+ async fn requester(
+ &self,
+ _: &ImportEvent,
+ ) -> PortResult<Option<(TeamId, MacroUserIdStr<'static>)>> {
+ unreachable!()
+ }
+ async fn claim(&self, _: &ImportEvent, _: WorkerId) -> PortResult<ClaimOutcome> {
+ unreachable!()
+ }
+ async fn heartbeat(&self, _: &Lease) -> PortResult<Lease> {
+ unreachable!()
+ }
+ async fn settle(
+ &self,
+ _: &Lease,
+ _: ConversationStatus,
+ _: Option<ImportError>,
+ ) -> PortResult<()> {
+ unreachable!()
+ }
+ async fn pending_events(&self, limit: u32) -> PortResult<Vec<ImportEvent>> {
+ assert_eq!(limit, PAGE_SIZE);
+ Ok(self.0.lock().unwrap().pending.clone())
+ }
+ async fn mark_published(&self, event: &ImportEvent) -> PortResult<()> {
+ let mut state = self.0.lock().unwrap();
+ assert!(
+ state.sent.contains(event),
+ "never acknowledge an unsent event"
+ );
+ if state.mark_fails {
+ return Err(ImportError::Retryable.into());
+ }
+ state.marked.push(event.clone());
+ state.pending.retain(|pending| pending != event);
+ Ok(())
+ }
+ async fn dead_letter(&self, _: &ImportEvent) -> PortResult<WorkerOutcome> {
+ let mut state = self.0.lock().unwrap();
+ if state.active {
+ return Ok(WorkerOutcome::Defer);
+ }
+ state.failed = true;
+ Ok(WorkerOutcome::Acknowledge)
+ }
+ async fn reconcile(&self, limit: u32) -> PortResult<()> {
+ assert_eq!(limit, PAGE_SIZE);
+ self.0.lock().unwrap().reconciled += 1;
+ Ok(())
+ }
+ async fn pending_search(&self, limit: u32) -> PortResult<Vec<SearchBackfill>> {
+ assert_eq!(limit, PAGE_SIZE);
+ Ok(self.0.lock().unwrap().searches.clone())
+ }
+ async fn record_search(&self, _: &SearchBackfill, state: SearchState) -> PortResult<()> {
+ self.0.lock().unwrap().search_states.push(state);
+ Ok(())
+ }
+}
+
+impl ImportQueue for Fake {
+ async fn publish(&self, event: &ImportEvent) -> PortResult<()> {
+ let mut state = self.0.lock().unwrap();
+ if state.send_fails {
+ return Err(ImportError::Retryable.into());
+ }
+ state.sent.push(event.clone());
+ Ok(())
+ }
+}
+
+impl SearchBackfillClient for Fake {
+ async fn submit(&self, _: &SearchBackfill) -> PortResult<Uuid> {
+ Ok(Uuid::now_v7())
+ }
+ async fn progress(&self, _: Uuid) -> PortResult<SearchState> {
+ Ok(SearchState::Completed)
+ }
+}
+
+impl ReferenceReconciler for Fake {
+ async fn reconcile_references(&self, limit: u32) -> PortResult<()> {
+ assert_eq!(limit, PAGE_SIZE);
+ self.0.lock().unwrap().references_reconciled += 1;
+ Ok(())
+ }
+}
+
+impl Clock for Fake {
+ fn now(&self) -> DateTime<Utc> {
+ Utc::now()
+ }
+}
+
+fn fixture() -> (Fake, ImportMaintenance<Fake, Fake, Fake, Fake, Fake>) {
+ let fake = Fake::default();
+ fake.0.lock().unwrap().pending.push(ImportEvent {
+ job_id: Uuid::now_v7().try_into().unwrap(),
+ slack_channel_id: "C123".parse().unwrap(),
+ generation: 1,
+ });
+ let service = ImportMaintenance::new(
+ fake.clone(),
+ fake.clone(),
+ fake.clone(),
+ fake.clone(),
+ fake.clone(),
+ );
+ (fake, service)
+}
+
+#[tokio::test]
+async fn outbox_send_then_ack_crash_republishes_without_losing_work() {
+ let (fake, service) = fixture();
+ fake.0.lock().unwrap().send_fails = true;
+ assert!(service.publish().await.is_err());
+ assert!(fake.0.lock().unwrap().marked.is_empty());
+ fake.0.lock().unwrap().send_fails = false;
+ fake.0.lock().unwrap().mark_fails = true;
+ assert!(service.publish().await.is_err());
+ assert_eq!(fake.0.lock().unwrap().pending.len(), 1);
+ fake.0.lock().unwrap().mark_fails = false;
+ service.publish().await.unwrap();
+ let state = fake.0.lock().unwrap();
+ assert_eq!(state.sent.len(), 2);
+ assert_eq!(state.sent[0], state.sent[1]);
+ assert_eq!(state.marked.len(), 1);
+ assert!(state.pending.is_empty());
+}
+
+#[tokio::test]
+async fn dead_letter_defers_active_owner_then_acknowledges_durable_failure() {
+ let (fake, service) = fixture();
+ let event = fake.0.lock().unwrap().pending[0].clone();
+ fake.0.lock().unwrap().active = true;
+ assert_eq!(
+ service.dead_letter(&event).await.unwrap(),
+ WorkerOutcome::Defer
+ );
+ assert!(!fake.0.lock().unwrap().failed);
+ fake.0.lock().unwrap().active = false;
+ assert_eq!(
+ service.dead_letter(&event).await.unwrap(),
+ WorkerOutcome::Acknowledge
+ );
+ assert!(fake.0.lock().unwrap().failed);
+}
+
+#[tokio::test]
+async fn recovery_reconciles_partial_search_without_publishing_new_imports() {
+ let (fake, service) = fixture();
+ let job_id = fake.0.lock().unwrap().pending[0].job_id;
+ fake.0.lock().unwrap().searches.push(SearchBackfill {
+ job_id,
+ channel_ids: vec![Uuid::now_v7()],
+ generation: 2,
+ state: SearchState::Pending,
+ updated_at: Utc::now(),
+ });
+ service.reconcile().await.unwrap();
+ {
+ let mut state = fake.0.lock().unwrap();
+ assert_eq!(state.reconciled, 1);
+ assert_eq!(state.references_reconciled, 1);
+ assert!(state.sent.is_empty());
+ assert!(matches!(
+ state.search_states[0],
+ SearchState::Submitted { .. }
+ ));
+ state.searches[0].state = state.search_states[0].clone();
+ }
+ service.reconcile().await.unwrap();
+ assert_eq!(
+ fake.0.lock().unwrap().search_states[1],
+ SearchState::Completed
+ );
+}
diff --git a/crates/slack_integration/src/domain/models.rs b/crates/slack_integration/src/domain/models.rs
new file mode 100644
index 0000000000..72284e188e
--- /dev/null
+++ b/crates/slack_integration/src/domain/models.rs
@@ -0,0 +1,1021 @@
+//! Stable import identities, commands and receipts. See `docs/SLACK_ARCHIVE_IMPORT_CONTRACT.md`.
+
+use std::{collections::BTreeMap, fmt, str::FromStr};
+
+use chrono::{DateTime, Utc};
+use macro_user_id::user_id::MacroUserIdStr;
+use serde::{Deserialize, Serialize};
+use sha2::{Digest, Sha256};
+use uuid::Uuid;
+
+#[cfg(test)]
+mod test;
+
+/// Safe, content-free validation failure; never includes supplied input.
+#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
+pub enum ValidationError {
+ /// An identifier has an invalid shape.
+ #[error("invalid identifier")]
+ InvalidId,
+ /// A path component could escape its namespace.
+ #[error("unsafe key segment")]
+ UnsafeKeySegment,
+ /// A timestamp is malformed or out of range.
+ #[error("invalid Slack timestamp")]
+ InvalidTimestamp,
+ /// SHA-256 must be 64 lowercase hexadecimal characters.
+ #[error("invalid SHA-256")]
+ InvalidDigest,
+ /// A configured byte, record or collection bound was exceeded.
+ #[error("import limit exceeded")]
+ LimitExceeded,
+ /// Descriptors are not the exact contiguous manifest declared by the seal.
+ #[error("invalid manifest")]
+ InvalidManifest,
+}
+
+macro_rules! uuid_id {
+ ($name:ident, $doc:literal) => {
+ #[doc = $doc]
+ #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
+ #[serde(try_from = "Uuid", into = "Uuid")]
+ #[cfg_attr(feature = "inbound", derive(utoipa::ToSchema))]
+ pub struct $name(Uuid);
+
+ impl TryFrom<Uuid> for $name {
+ type Error = ValidationError;
+ fn try_from(value: Uuid) -> Result<Self, Self::Error> {
+ if value.is_nil() || value.is_max() {
+ return Err(ValidationError::InvalidId);
+ }
+ Ok(Self(value))
+ }
+ }
+
+ impl From<$name> for Uuid {
+ fn from(value: $name) -> Self {
+ value.0
+ }
+ }
+
+ impl FromStr for $name {
+ type Err = ValidationError;
+ fn from_str(value: &str) -> Result<Self, Self::Err> {
+ Uuid::parse_str(value)
+ .map_err(|_| ValidationError::InvalidId)?
+ .try_into()
+ }
+ }
+
+ impl fmt::Display for $name {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ self.0.fmt(f)
+ }
+ }
+ };
+}
+
+uuid_id!(
+ JobId,
+ "Import job identity. Generate UUIDv7 in application code."
+);
+uuid_id!(
+ TeamId,
+ "Macro team identity, derived from an admin receipt, not request JSON."
+);
+uuid_id!(
+ CreateToken,
+ "Client-generated create idempotency token, scoped to team and administrator."
+);
+uuid_id!(
+ LeaseToken,
+ "Unpredictable fencing token for one worker claim; never part of public progress."
+);
+uuid_id!(WorkerId, "Identity of a worker process holding a lease.");
+
+macro_rules! string_id {
+ ($name:ident, $doc:literal, $validate:expr, $error:expr) => {
+ #[doc = $doc]
+ #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
+ #[serde(try_from = "String", into = "String")]
+ #[cfg_attr(feature = "inbound", derive(utoipa::ToSchema))]
+ pub struct $name(String);
+
+ impl $name {
+ /// The validated string representation.
+ pub fn as_str(&self) -> &str {
+ &self.0
+ }
+ }
+
+ impl TryFrom<String> for $name {
+ type Error = ValidationError;
+ fn try_from(value: String) -> Result<Self, Self::Error> {
+ if !($validate)(&value) {
+ return Err($error);
+ }
+ Ok(Self(value))
+ }
+ }
+
+ impl From<$name> for String {
+ fn from(value: $name) -> Self {
+ value.0
+ }
+ }
+
+ impl FromStr for $name {
+ type Err = ValidationError;
+ fn from_str(value: &str) -> Result<Self, Self::Err> {
+ value.to_owned().try_into()
+ }
+ }
+
+ impl fmt::Display for $name {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ self.0.fmt(f)
+ }
+ }
+ };
+}
+
+fn slack_id(value: &str, prefixes: &[u8]) -> bool {
+ (2..=64).contains(&value.len())
+ && prefixes.contains(&value.as_bytes()[0])
+ && value
+ .bytes()
+ .all(|b| b.is_ascii_uppercase() || b.is_ascii_digit())
+}
+
+fn safe_segment(value: &str) -> bool {
+ !value.is_empty()
+ && value.len() <= 255
+ && value != "."
+ && value != ".."
+ && !value
+ .chars()
+ .any(|c| c.is_control() || matches!(c, '/' | '\\' | '%'))
+}
+
+string_id!(
+ ConversationId,
+ "Source Slack conversation ID (C, G or D prefix); unique only within a source.",
+ |v: &str| slack_id(v, b"CGD"),
+ ValidationError::InvalidId
+);
+string_id!(
+ SourceId,
+ "Known Slack workspace identity (T prefix); an enterprise ID alone is insufficient.",
+ |v: &str| slack_id(v, b"T"),
+ ValidationError::InvalidId
+);
+string_id!(
+ SlackUserId,
+ "Slack member identity (U or W prefix), including USLACKBOT.",
+ |v: &str| slack_id(v, b"UW"),
+ ValidationError::InvalidId
+);
+string_id!(
+ KeySegment,
+ "Single safe source folder/key segment; never a path or authorization proof.",
+ safe_segment,
+ ValidationError::UnsafeKeySegment
+);
+string_id!(
+ ObjectKey,
+ "Server-generated staging object key. Never accepted in client or queue commands.",
+ |v: &str| v.len() <= 1024 && v.split('/').all(safe_segment),
+ ValidationError::UnsafeKeySegment
+);
+string_id!(
+ Sha256Digest,
+ "SHA-256 encoded as exactly 64 lowercase hexadecimal characters.",
+ |v: &str| v.len() == 64
+ && v.bytes()
+ .all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b)),
+ ValidationError::InvalidDigest
+);
+
+/// Slack time represented exactly as nonnegative Unix microseconds.
+/// JSON is a string with six fractional digits, never a floating-point number.
+#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
+#[serde(try_from = "String", into = "String")]
+#[cfg_attr(feature = "inbound", derive(utoipa::ToSchema))]
+#[cfg_attr(feature = "inbound", schema(value_type = String))]
+pub struct SlackTimestamp(i64);
+
+impl SlackTimestamp {
+ /// Exact number of microseconds since the Unix epoch.
+ pub fn unix_micros(self) -> i64 {
+ self.0
+ }
+}
+
+impl FromStr for SlackTimestamp {
+ type Err = ValidationError;
+ fn from_str(value: &str) -> Result<Self, Self::Err> {
+ let invalid = ValidationError::InvalidTimestamp;
+ let (seconds, fraction) = value.split_once('.').ok_or(invalid)?;
+ if seconds.is_empty()
+ || seconds.len() > 12
+ || !(1..=6).contains(&fraction.len())
+ || !seconds
+ .bytes()
+ .chain(fraction.bytes())
+ .all(|b| b.is_ascii_digit())
+ {
+ return Err(invalid);
+ }
+ let seconds: i64 = seconds.parse().map_err(|_| invalid)?;
+ // Bound to year 9999 for interoperable database/browser timestamps.
+ if seconds > 253_402_300_799 {
+ return Err(invalid);
+ }
+ let fraction: i64 =
+ fraction.parse::<i64>().map_err(|_| invalid)? * 10_i64.pow(6 - fraction.len() as u32);
+ Ok(Self(seconds * 1_000_000 + fraction))
+ }
+}
+
+impl TryFrom<String> for SlackTimestamp {
+ type Error = ValidationError;
+ fn try_from(value: String) -> Result<Self, Self::Error> {
+ value.parse()
+ }
+}
+
+impl From<SlackTimestamp> for String {
+ fn from(value: SlackTimestamp) -> Self {
+ value.to_string()
+ }
+}
+
+impl fmt::Display for SlackTimestamp {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ write!(f, "{}.{:06}", self.0 / 1_000_000, self.0 % 1_000_000)
+ }
+}
+
+/// Source identity supplied when creating a job. One binding per Macro team in v1.
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+#[serde(
+ tag = "kind",
+ rename_all = "snake_case",
+ rename_all_fields = "camelCase"
+)]
+#[cfg_attr(feature = "inbound", derive(utoipa::ToSchema))]
+pub enum SourceIdentity {
+ /// Archive names its Slack workspace; reject a conflicting team binding.
+ Known {
+ /// Workspace identifier in the archive.
+ #[serde(rename = "sourceId")]
+ source_id: SourceId,
+ },
+ /// Explicit confirmation that an unidentified archive belongs to this team's source.
+ /// Choosing this variant is affirmative confirmation, not an inferred default.
+ ConfirmedUnknown,
+}
+
+/// Slack conversation kind. Public Slack channels map to Macro Team, never Public.
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
+#[serde(rename_all = "snake_case")]
+#[cfg_attr(feature = "inbound", derive(utoipa::ToSchema))]
+pub enum ConversationKind {
+ /// Team channel with explicit members and auto-join disabled.
+ PublicChannel,
+ /// Private Macro channel.
+ PrivateChannel,
+ /// Exactly two distinct mapped email identities; otherwise skip.
+ DirectMessage,
+ /// Private Macro channel named from source member display names.
+ GroupDirectMessage,
+}
+
+/// Full selected conversation metadata, persisted before granting any uploads.
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+#[serde(rename_all = "camelCase", deny_unknown_fields)]
+#[cfg_attr(feature = "inbound", derive(utoipa::ToSchema))]
+pub struct ConversationMetadata {
+ /// Stable Slack identity.
+ pub slack_channel_id: ConversationId,
+ /// Source conversation kind.
+ pub kind: ConversationKind,
+ /// Original display name; not a storage key.
+ pub name: String,
+ /// Single archive folder name, not a backend object key.
+ pub folder: KeySegment,
+ /// Complete source member list, including members unknown to Macro.
+ pub member_ids: Vec<SlackUserId>,
+ /// Source creator when available.
+ pub creator_id: Option<SlackUserId>,
+ /// Missing time is resolved once from earliest message or persisted job time.
+ pub created_at: Option<SlackTimestamp>,
+ /// Source archived flag (does not silently archive a reused Macro target).
+ pub archived: bool,
+ /// Advisory only; null means not yet counted, not zero.
+ pub message_count: Option<u64>,
+}
+
+/// Create command. Team identity is deliberately absent.
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+#[serde(rename_all = "camelCase", deny_unknown_fields)]
+#[cfg_attr(feature = "inbound", derive(utoipa::ToSchema))]
+pub struct CreateImport {
+ /// Replay with the same semantic payload returns the original job; changes conflict.
+ pub idempotency_token: CreateToken,
+ /// Binding or explicit unknown-source confirmation.
+ pub source: SourceIdentity,
+ /// Default true at the client; false still requires users and zero-part seals.
+ pub include_message_history: bool,
+ /// Full, unique selected conversations (at most the configured bound).
+ #[cfg_attr(feature = "inbound", schema(min_items = 1, max_items = 2000))]
+ pub conversations: Vec<ConversationMetadata>,
+}
+
+/// Configurable bounds shared with browser staging and enforced again by the worker.
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
+#[serde(rename_all = "camelCase")]
+#[cfg_attr(feature = "inbound", derive(utoipa::ToSchema))]
+pub struct ImportLimits {
+ /// Maximum NDJSON part bytes.
+ pub part_bytes: u64,
+ /// Maximum records per part.
+ pub part_records: u32,
+ /// Maximum bytes per NDJSON record, including newline.
+ pub record_bytes: u64,
+ /// Maximum users, root metadata or individual day JSON bytes.
+ pub json_bytes: u64,
+ /// Maximum selected temporary data bytes across all uploads.
+ pub selected_bytes: u64,
+ /// Maximum ZIP entries scanned by the browser.
+ pub zip_entries: u32,
+ /// Maximum selected conversations.
+ pub conversations: u32,
+ /// Maximum descriptors or completion identities per call.
+ pub registration_batch: u32,
+ /// Maximum historical batch payload bytes.
+ pub database_batch_bytes: u64,
+ /// Maximum messages in a historical batch.
+ pub database_batch_messages: u32,
+}
+
+impl Default for ImportLimits {
+ fn default() -> Self {
+ Self {
+ part_bytes: 16 * 1024 * 1024,
+ part_records: 20_000,
+ record_bytes: 1024 * 1024,
+ json_bytes: 32 * 1024 * 1024,
+ selected_bytes: 2 * 1024 * 1024 * 1024,
+ zip_entries: 100_000,
+ conversations: 2_000,
+ registration_batch: 50,
+ database_batch_bytes: 4 * 1024 * 1024,
+ database_batch_messages: 500,
+ }
+ }
+}
+
+/// Identity in a job's persisted manifest. No user-supplied object keys.
+#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
+#[serde(
+ tag = "kind",
+ rename_all = "snake_case",
+ rename_all_fields = "camelCase",
+ deny_unknown_fields
+)]
+#[cfg_attr(feature = "inbound", derive(utoipa::ToSchema))]
+pub enum UploadId {
+ /// One normalized users payload from users.json and/or org_users.json.
+ Users,
+ /// One NDJSON part in a selected conversation.
+ ConversationPart {
+ /// Selected Slack conversation.
+ #[serde(rename = "slackChannelId")]
+ slack_channel_id: ConversationId,
+ /// Zero-based part index; sealed manifests must be contiguous.
+ #[serde(rename = "partIndex")]
+ part_index: u32,
+ },
+}
+
+/// Immutable expected object properties, registered before signing an upload.
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+#[serde(rename_all = "camelCase", deny_unknown_fields)]
+#[cfg_attr(feature = "inbound", derive(utoipa::ToSchema))]
+pub struct UploadDescriptor {
+ /// Manifest identity within this job.
+ pub upload: UploadId,
+ /// SHA-256 of the exact uploaded bytes, not the ETag.
+ pub sha256: Sha256Digest,
+ /// Exact byte length including delimiters.
+ pub byte_length: u64,
+ /// Exact NDJSON record count; null for the users JSON payload.
+ pub record_count: Option<u32>,
+}
+
+impl UploadDescriptor {
+ /// Check per-object limits and the users/NDJSON distinction.
+ /// The service separately checks aggregate bytes and selection membership.
+ pub fn validate(&self, limits: &ImportLimits) -> Result<(), ValidationError> {
+ let valid = match &self.upload {
+ UploadId::Users => {
+ self.record_count.is_none()
+ && self.byte_length > 0
+ && self.byte_length <= limits.json_bytes
+ }
+ UploadId::ConversationPart { .. } => {
+ self.byte_length > 0
+ && self.byte_length <= limits.part_bytes
+ && self
+ .record_count
+ .is_some_and(|n| n > 0 && n <= limits.part_records)
+ }
+ };
+ if valid {
+ Ok(())
+ } else {
+ Err(ValidationError::LimitExceeded)
+ }
+ }
+}
+
+/// Batched registration, also used to renew an unchanged descriptor's grant.
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+#[serde(rename_all = "camelCase", deny_unknown_fields)]
+#[cfg_attr(feature = "inbound", derive(utoipa::ToSchema))]
+pub struct RegisterUploads {
+ /// At most 50 descriptors; no duplicate identities in a call.
+ #[cfg_attr(feature = "inbound", schema(max_items = 50))]
+ pub descriptors: Vec<UploadDescriptor>,
+}
+
+/// Complete immutable expected part set for one conversation, including zero parts.
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+#[serde(rename_all = "camelCase", deny_unknown_fields)]
+#[cfg_attr(feature = "inbound", derive(utoipa::ToSchema))]
+pub struct ConversationSeal {
+ /// Selected source conversation.
+ pub slack_channel_id: ConversationId,
+ /// Exact number of registered parts, starting at index zero.
+ pub part_count: u32,
+ /// SHA-256 of canonical descriptor lines; zero parts hash the empty byte string.
+ pub manifest_sha256: Sha256Digest,
+}
+
+impl ConversationSeal {
+ /// Validate and hash descriptors sorted by index, independently of registration order.
+ /// Input must contain only this conversation's parts, with no gaps or duplicates.
+ pub fn from_descriptors(
+ slack_channel_id: ConversationId,
+ descriptors: &[UploadDescriptor],
+ limits: &ImportLimits,
+ ) -> Result<Self, ValidationError> {
+ let mut parts = Vec::with_capacity(descriptors.len());
+ let mut total_bytes = 0_u64;
+ for descriptor in descriptors {
+ descriptor.validate(limits)?;
+ let UploadId::ConversationPart {
+ slack_channel_id: id,
+ part_index,
+ } = &descriptor.upload
+ else {
+ return Err(ValidationError::InvalidManifest);
+ };
+ if *id != slack_channel_id {
+ return Err(ValidationError::InvalidManifest);
+ }
+ total_bytes = total_bytes
+ .checked_add(descriptor.byte_length)
+ .filter(|n| *n <= limits.selected_bytes)
+ .ok_or(ValidationError::LimitExceeded)?;
+ parts.push((*part_index, descriptor));
+ }
+ parts.sort_unstable_by_key(|(index, _)| *index);
+ let mut hash = Sha256::new();
+ for (expected, (index, descriptor)) in parts.iter().enumerate() {
+ if usize::try_from(*index).ok() != Some(expected) {
+ return Err(ValidationError::InvalidManifest);
+ }
+ let records = descriptor
+ .record_count
+ .ok_or(ValidationError::InvalidManifest)?;
+ hash.update(format!(
+ "{index}:{}:{}:{records}\n",
+ descriptor.sha256, descriptor.byte_length
+ ));
+ }
+ Ok(Self {
+ slack_channel_id,
+ part_count: parts
+ .len()
+ .try_into()
+ .map_err(|_| ValidationError::LimitExceeded)?,
+ manifest_sha256: format!("{:x}", hash.finalize()).parse()?,
+ })
+ }
+}
+
+/// Explicit upload completion; a seal can be submitted separately with an empty list.
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+#[serde(rename_all = "camelCase", deny_unknown_fields)]
+#[cfg_attr(feature = "inbound", derive(utoipa::ToSchema))]
+pub struct CompleteUploads {
+ /// Registered identities to verify against storage (at most 50).
+ #[cfg_attr(feature = "inbound", schema(max_items = 50))]
+ pub uploads: Vec<UploadId>,
+ /// Optional immutable expected part set; identical retries are idempotent.
+ pub seal: Option<ConversationSeal>,
+}
+
+/// Request for an existing job (finalize, cancel or progress); no body-supplied team.
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
+#[serde(rename_all = "camelCase", deny_unknown_fields)]
+pub struct JobCommand {
+ /// Job identity from the resource path.
+ pub job_id: JobId,
+}
+
+/// Job lifecycle. Cancellation remains in progress while any lease is active.
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
+#[serde(rename_all = "snake_case")]
+#[cfg_attr(feature = "inbound", derive(utoipa::ToSchema))]
+pub enum JobStatus {
+ /// Registration is open; some conversations may already be running.
+ Uploading,
+ /// Registration closed; ready conversations are queued or importing.
+ Processing,
+ /// All selected conversations settled successfully or were deliberately skipped.
+ Completed,
+ /// All work settled, with at least one failure and some successful work.
+ CompletedWithErrors,
+ /// Terminal failure without successful conversations.
+ Failed,
+ /// No further claims/publications; existing leases are settling.
+ Cancelling,
+ /// Terminal cancellation, retaining committed partial history.
+ Cancelled,
+}
+
+/// Durable per-conversation lifecycle.
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
+#[serde(rename_all = "snake_case")]
+#[cfg_attr(feature = "inbound", derive(utoipa::ToSchema))]
+pub enum ConversationStatus {
+ /// Manifest or verified uploads are incomplete.
+ AwaitingUploads,
+ /// Readiness and a unique outbox event have committed together.
+ Queued,
+ /// Claimed under a renewable fenced lease.
+ Importing,
+ /// Historical persistence completed; indexing is tracked separately.
+ Completed,
+ /// Never ready at finalize, unauthorized target, unsupported DM, or cancelled unclaimed work.
+ Skipped,
+ /// Exhausted or non-retryable failure; partial committed counts remain visible.
+ Failed,
+}
+
+/// Public error codes; never carry raw provider errors, keys, emails or source text.
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, thiserror::Error)]
+#[serde(rename_all = "snake_case")]
+#[cfg_attr(feature = "inbound", derive(utoipa::ToSchema))]
+pub enum ImportError {
+ /// Creation and upload operations are disabled by server configuration.
+ #[error("Slack imports are disabled")]
+ Disabled,
+ /// Malformed command or export data.
+ #[error("invalid import input")]
+ InvalidInput,
+ /// A configured bound was exceeded.
+ #[error("import limit exceeded")]
+ LimitExceeded,
+ /// Unknown or inaccessible job/target, deliberately indistinguishable.
+ #[error("import unavailable")]
+ Unavailable,
+ /// Requester is no longer a team administrator.
+ #[error("administrator access required")]
+ AdminRequired,
+ /// Known source differs from the team's bound source.
+ #[error("Slack workspace mismatch")]
+ SourceMismatch,
+ /// Idempotency token, descriptor, seal or lifecycle conflict.
+ #[error("import state conflict")]
+ Conflict,
+ /// Object checksum, length or identity disagrees with the persisted descriptor.
+ #[error("upload verification failed")]
+ UploadMismatch,
+ /// Current lease no longer permits writes.
+ #[error("import lease lost")]
+ LeaseLost,
+ /// Transient dependency outage; safe to retry using the same identity.
+ #[error("import temporarily unavailable")]
+ Retryable,
+ /// Exhausted retries or an internal failure, without leaking diagnostics.
+ #[error("import failed")]
+ Internal,
+}
+
+/// Non-fatal, sanitized explanations displayed in admin progress.
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
+#[serde(rename_all = "snake_case")]
+#[cfg_attr(feature = "inbound", derive(utoipa::ToSchema))]
+pub enum ImportWarning {
+ /// Creation time resolved from earliest message.
+ CreationTimeFromMessage,
+ /// No source time; persisted job creation time used.
+ CreationTimeFromJob,
+ /// DM does not map to exactly two distinct email identities.
+ UnresolvableDirectMessage,
+ /// Existing target could not be authorized; no target ID may be exposed.
+ TargetUnavailable,
+ /// Conversation never became ready before finalization.
+ UploadsIncomplete,
+}
+
+/// Committed counters, never optimistic browser or uncommitted worker counts.
+#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
+#[serde(rename_all = "camelCase")]
+#[cfg_attr(feature = "inbound", derive(utoipa::ToSchema))]
+pub struct ImportCounters {
+ /// Source records examined through the committed checkpoint.
+ pub processed: u64,
+ /// Newly persisted messages.
+ pub imported: u64,
+ /// Previously committed source identities.
+ pub duplicates: u64,
+ /// Unsupported, empty or otherwise deliberately skipped records.
+ pub skipped: u64,
+ /// Newly persisted reactions.
+ pub reactions: u64,
+}
+
+/// Search publication/receipt state. Acceptance is not completed publication;
+/// even completed publication still awaits consumer indexing and refresh.
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+#[serde(
+ tag = "status",
+ rename_all = "snake_case",
+ rename_all_fields = "camelCase"
+)]
+#[cfg_attr(feature = "inbound", derive(utoipa::ToSchema))]
+pub enum SearchState {
+ /// No committed history requires indexing.
+ NotNeeded,
+ /// Committed history requires an outbox-backed scoped submission.
+ Pending,
+ /// Search service accepted the request, but has not completed it.
+ Submitted {
+ /// Receipt from the search service.
+ #[serde(rename = "receiptId")]
+ receipt_id: Uuid,
+ },
+ /// Search service durably published the scope; eventual indexing is separate.
+ Completed,
+ /// Publication failed; retained history must be retried independently.
+ Failed,
+}
+
+/// Admin-visible progress for one selected conversation.
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+#[serde(rename_all = "camelCase")]
+#[cfg_attr(feature = "inbound", derive(utoipa::ToSchema))]
+pub struct ConversationProgress {
+ /// Source identity (always visible to the importing team's administrator).
+ pub slack_channel_id: ConversationId,
+ /// Persisted source display name, never the reused Macro target's name.
+ pub name: String,
+ /// Persisted source kind.
+ pub kind: ConversationKind,
+ /// Whether the selected source conversation was archived.
+ pub archived: bool,
+ /// Durable state.
+ pub status: ConversationStatus,
+ /// Authorized target only; absent for inaccessible reused targets.
+ pub channel_id: Option<Uuid>,
+ /// Null until sealed; zero is a valid sealed empty manifest.
+ pub part_count: Option<u32>,
+ /// Number of parts whose object identity has been verified.
+ pub verified_parts: u32,
+ /// Durably committed record counts.
+ pub counters: ImportCounters,
+ /// Independent indexing state.
+ pub search: SearchState,
+ /// Sanitized terminal or retry diagnostic.
+ pub error: Option<ImportError>,
+ /// Non-fatal metadata/skip explanations.
+ pub warnings: Vec<ImportWarning>,
+}
+
+/// Common receipt returned by create, completion, finalize, cancel and progress.
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+#[serde(rename_all = "camelCase")]
+#[cfg_attr(feature = "inbound", derive(utoipa::ToSchema))]
+pub struct ImportProgress {
+ /// Immutable source confirmation from this job, not the current team binding.
+ pub source: SourceIdentity,
+ /// Immutable history option confirmed when creating this job.
+ pub include_message_history: bool,
+ /// Stable job identity.
+ pub job_id: JobId,
+ /// Durable job lifecycle.
+ pub status: JobStatus,
+ /// Monotonic job revision for polling/websocket invalidation.
+ pub revision: u64,
+ /// Persisted creation time, also the final historical-time fallback.
+ pub created_at: DateTime<Utc>,
+ /// Last durable lifecycle/progress update.
+ pub updated_at: DateTime<Utc>,
+ /// Registration closure time; cancellation also closes registration.
+ pub registration_closed_at: Option<DateTime<Utc>>,
+ /// Whether users metadata is verified and pinned.
+ pub users_verified: bool,
+ /// Server bounds for staging and registration.
+ pub limits: ImportLimits,
+ /// All selected conversations, bounded by the create limit.
+ pub conversations: Vec<ConversationProgress>,
+}
+
+/// Team's durable single-source binding, including confirmed unidentified archives.
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+#[serde(
+ tag = "kind",
+ rename_all = "snake_case",
+ rename_all_fields = "camelCase"
+)]
+#[cfg_attr(feature = "inbound", derive(utoipa::ToSchema))]
+pub enum SourceBinding {
+ /// No import has bound this team yet.
+ Unbound,
+ /// Legacy/onboarding or explicitly confirmed unidentified source.
+ ConfirmedUnknown,
+ /// Known Slack workspace; reject mismatches.
+ Known {
+ /// Bound source identity.
+ #[serde(rename = "sourceId")]
+ source_id: SourceId,
+ },
+}
+
+/// Admin list result, also supplying upload limits before a job exists.
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+#[serde(rename_all = "camelCase")]
+#[cfg_attr(feature = "inbound", derive(utoipa::ToSchema))]
+pub struct ImportPage {
+ /// At most 50 job receipts, newest first.
+ pub jobs: Vec<ImportProgress>,
+ /// Exclusive cursor for the next page; null when exhausted.
+ pub next_cursor: Option<JobId>,
+ /// Effective server limits.
+ pub limits: ImportLimits,
+ /// Binding summary used by the picker confirmation.
+ pub source_binding: SourceBinding,
+}
+
+/// Signed upload permission returned just before PUT; never persisted as an identity.
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+#[serde(rename_all = "camelCase")]
+#[cfg_attr(feature = "inbound", derive(utoipa::ToSchema))]
+pub struct UploadGrant {
+ /// Exact immutable descriptor being signed.
+ pub descriptor: UploadDescriptor,
+ /// Short-lived signed destination; must not be logged.
+ pub url: String,
+ /// Required signed upload headers (including checksum, content type and create-only
+ /// condition). Browser-forbidden Content-Length is derived from the exact Blob bytes.
+ pub required_headers: BTreeMap<String, String>,
+ /// Grant expiry; callers renew by re-registering the identical descriptor.
+ pub expires_at: DateTime<Utc>,
+}
+
+/// Server-owned manifest entry used by storage adapters, never by request deserialization.
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct RegisteredUpload {
+ /// Expected bytes and manifest identity.
+ pub descriptor: UploadDescriptor,
+ /// Key generated server-side and looked up through the owning job's manifest.
+ pub key: ObjectKey,
+}
+
+string_id!(
+ ObjectValidator,
+ "Opaque storage version or entity tag, used only to pin reads; never interpreted as SHA-256.",
+ |v: &str| !v.is_empty() && v.len() <= 1024 && !v.chars().any(char::is_control),
+ ValidationError::InvalidId
+);
+
+/// Verified immutable object identity; adapters must enforce the selected read condition.
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub enum ObjectIdentity {
+ /// Read this exact storage version.
+ Version(ObjectValidator),
+ /// Conditional read against the verified entity tag when versioning is unavailable.
+ EntityTag(ObjectValidator),
+}
+
+/// Verified object and mandatory read pin. The streamed digest is checked independently.
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct VerifiedUpload {
+ /// Persisted manifest entry whose checksum and length storage verified.
+ pub registered: RegisteredUpload,
+ /// Version or entity-tag precondition; an unpinned read is never valid.
+ pub identity: ObjectIdentity,
+}
+
+/// Queue payload: identity and event generation only, never storage locations or authority.
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+#[serde(rename_all = "camelCase", deny_unknown_fields)]
+pub struct ImportEvent {
+ /// Job owning the work.
+ pub job_id: JobId,
+ /// Selected source conversation.
+ pub slack_channel_id: ConversationId,
+ /// Durable outbox generation; distinct from the execution lease generation.
+ pub generation: u64,
+}
+
+/// Renewable worker fencing capability; every batch/final transition checks all fields.
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct Lease {
+ /// Work identity and publication generation.
+ pub event: ImportEvent,
+ /// Claiming process.
+ pub owner: WorkerId,
+ /// Fresh token per claim/reclaim.
+ pub token: LeaseToken,
+ /// Monotonically increasing execution generation.
+ pub generation: u64,
+ /// Persisted expiry, evaluated against the database clock.
+ pub expires_at: DateTime<Utc>,
+ /// Last persisted heartbeat time.
+ pub heartbeat_at: DateTime<Utc>,
+ /// Durable claim attempt count, including reclaims.
+ pub attempts: u32,
+}
+
+/// Exact restart position in the sorted, deduplicated NDJSON stream.
+#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
+#[serde(rename_all = "camelCase")]
+pub struct Checkpoint {
+ /// Next part to read (part count means end of stream).
+ pub part_index: u32,
+ /// Next zero-based line within that part.
+ pub record_index: u32,
+}
+
+/// Trusted persisted context loaded on claim, never reconstructed from a queue body.
+#[derive(Debug, Clone)]
+pub struct ClaimedConversation {
+ /// Current fenced lease.
+ pub lease: Lease,
+ /// Owning team for source-scoped dedupe and authorization.
+ pub team_id: TeamId,
+ /// Original requesting admin; revalidated before claim/reclaim.
+ pub requested_by: MacroUserIdStr<'static>,
+ /// Full selected source metadata.
+ pub metadata: ConversationMetadata,
+ /// Verified users payload, required even for shape-only imports.
+ pub users: VerifiedUpload,
+ /// Sealed verified parts in index order (possibly empty).
+ pub parts: Vec<VerifiedUpload>,
+ /// Durably committed restart position.
+ pub checkpoint: Checkpoint,
+ /// Stable fallback when creation time is absent.
+ pub job_created_at: DateTime<Utc>,
+}
+
+/// Claim result distinguishes busy work from obsolete events so a duplicate delivery
+/// cannot accidentally acknowledge away another worker's recovery opportunity.
+#[derive(Debug, Clone)]
+pub enum ClaimOutcome {
+ /// Newly claimed or reclaimed persisted work.
+ Claimed(Box<ClaimedConversation>),
+ /// A current worker owns the work; defer delivery without acknowledging it.
+ ActiveLease,
+ /// Work is durably terminal, unknown or superseded by a newer durable event.
+ Obsolete,
+}
+
+/// Driver decision after processing an identity-only queue event.
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub enum WorkerOutcome {
+ /// Durable settlement or obsolete event; safe to acknowledge.
+ Acknowledge,
+ /// Active work still needs recovery coverage; do not acknowledge.
+ Defer,
+}
+
+/// Natural identity of a message, shared by thread lookup and durable dedupe.
+#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize)]
+pub struct SourceMessageId {
+ /// Team's single bound Slack source namespace.
+ pub team_id: TeamId,
+ /// Source conversation, not target channel.
+ pub slack_channel_id: ConversationId,
+ /// Original exact Slack time.
+ pub ts: SlackTimestamp,
+}
+
+/// Historical attribution distinguishes an email-mapped user from the platform bot.
+/// The composition root resolves `SystemBot` to the canonical `MACRO_SYSTEM_BOT_ID`.
+#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
+pub enum HistoricalSender {
+ /// Raw lowercase-email identity, without roster lookup.
+ User(MacroUserIdStr<'static>),
+ /// Fallback for unknown authors, accompanied by `imported_author`.
+ SystemBot,
+}
+
+/// Converted message passed to the atomic historical sink, not the live send path.
+#[derive(Debug, Clone, Serialize)]
+pub struct HistoricalMessage {
+ /// Caller-generated UUIDv7; existing source mapping wins across jobs.
+ pub id: Uuid,
+ /// Durable source-scoped identity.
+ pub source: SourceMessageId,
+ /// Already authorized target channel.
+ pub channel_id: Uuid,
+ /// Resolved root message ID, validated as belonging to this target.
+ pub parent_id: Option<Uuid>,
+ /// Original thread root time when its parent is absent/skipped; retained as import
+ /// metadata without promising automatic repair when a later archive supplies it.
+ pub orphaned_thread_ts: Option<SlackTimestamp>,
+ /// Lowercase raw-email identity or the system bot.
+ pub sender: HistoricalSender,
+ /// Slack display name only for system-bot fallback attribution.
+ pub imported_author: Option<String>,
+ /// Converted, nonempty Macro markdown (attachment bytes are excluded).
+ pub content: String,
+ /// Only user identities emitted by conversion outside code; never rediscover
+ /// these by parsing the serialized body.
+ pub user_mentions: Vec<MacroUserIdStr<'static>>,
+ /// Typed occurrences against the safe initial content, never speculative IDs.
+ pub body_references: Vec<super::slack::references::ReferenceIntent>,
+ /// Deterministic source ordering for equal-time historical display.
+ pub import_order: u64,
+ /// Converted, deduplicated reactions with email-bearing actors only.
+ pub reactions: Vec<HistoricalReaction>,
+}
+
+/// One silent historical reaction; source message time supplies missing reaction time.
+#[derive(Debug, Clone, Serialize)]
+pub struct HistoricalReaction {
+ /// Raw lowercase email mapped to a Macro identity.
+ pub user_id: MacroUserIdStr<'static>,
+ /// Supported Unicode reaction, after shortcode conversion.
+ pub emoji: String,
+ /// Original reaction time or its source-message fallback.
+ pub created_at: SlackTimestamp,
+}
+
+/// Unit of atomic persistence across owning-crate transaction helpers.
+#[derive(Debug, Clone)]
+pub struct HistoricalBatch {
+ /// Required fencing capability, checked inside the shared transaction.
+ pub lease: Lease,
+ /// Bounded by both message count and bytes.
+ pub messages: Vec<HistoricalMessage>,
+ /// Resume point committed with the rows and mappings.
+ pub checkpoint: Checkpoint,
+ /// Examined records skipped before persistence; sink computes inserts/duplicates/reactions.
+ pub skipped: u64,
+}
+
+/// Durable canonical target reservation state.
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub enum ReservationStatus {
+ /// Stable candidate allocated, creation/ledger completion still recoverable.
+ Pending,
+ /// Canonical target is ready.
+ Ready,
+ /// Ambiguous legacy mappings; fail closed rather than choose a target.
+ Conflict,
+}
+
+/// Canonical import-owned target reservation shared with onboarding.
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct TargetReservation {
+ /// Bound source namespace owner.
+ pub team_id: TeamId,
+ /// Slack natural key; source is Slack, initiator may be onboarding or archive.
+ pub slack_channel_id: ConversationId,
+ /// Stable UUIDv7 allocated before idempotent channel creation.
+ pub channel_id: Uuid,
+ /// Creation/ledger reconciliation state.
+ pub status: ReservationStatus,
+}
+
+/// Scoped channel indexing request; acceptance alone never means indexing is finished.
+#[derive(Debug, Clone)]
+pub struct SearchBackfill {
+ /// Import that owns this indexing attempt.
+ pub job_id: JobId,
+ /// Only these authorized imported channels may be enumerated.
+ pub channel_ids: Vec<Uuid>,
+ /// Dirty-search generation used to deduplicate retries.
+ pub generation: u64,
+ /// Persisted submission state, including the receipt to poll when submitted.
+ pub state: SearchState,
+ /// Last submission/state update, used to reconcile stale receipts.
+ pub updated_at: DateTime<Utc>,
+}
diff --git a/crates/slack_integration/src/domain/models/test.rs b/crates/slack_integration/src/domain/models/test.rs
new file mode 100644
index 0000000000..4b9a82cf72
--- /dev/null
+++ b/crates/slack_integration/src/domain/models/test.rs
@@ -0,0 +1,374 @@
+use super::*;
+use serde_json::{from_value, json, to_value};
+
+fn conversation() -> ConversationId {
+ "C012ABC".parse().unwrap()
+}
+
+fn descriptor(index: u32) -> UploadDescriptor {
+ UploadDescriptor {
+ upload: UploadId::ConversationPart {
+ slack_channel_id: conversation(),
+ part_index: index,
+ },
+ sha256: "a".repeat(64).parse().unwrap(),
+ byte_length: 100,
+ record_count: Some(2),
+ }
+}
+
+#[test]
+fn uuid_ids_validate_on_construction_and_deserialization() {
+ for invalid in [
+ "",
+ "not-a-uuid",
+ "00000000-0000-0000-0000-000000000000",
+ "ffffffff-ffff-ffff-ffff-ffffffffffff",
+ ] {
+ assert!(invalid.parse::<JobId>().is_err());
+ assert!(from_value::<JobId>(json!(invalid)).is_err());
+ assert!(invalid.parse::<CreateToken>().is_err());
+ assert!(invalid.parse::<LeaseToken>().is_err());
+ assert!(invalid.parse::<TeamId>().is_err());
+ assert!(invalid.parse::<WorkerId>().is_err());
+ }
+ let uuid = Uuid::now_v7();
+ let job = JobId::try_from(uuid).unwrap();
+ assert_eq!(Uuid::from(job), uuid);
+ assert_eq!(from_value::<JobId>(to_value(job).unwrap()).unwrap(), job);
+}
+
+#[test]
+fn source_ids_are_typed_and_path_safe() {
+ for valid in ["C012AB", "G012AB", "D012AB"] {
+ assert!(valid.parse::<ConversationId>().is_ok());
+ }
+ for invalid in [
+ "", "C", "c012", "T012", "C/123", "C..", "C%2F", "Cλ", "C123\n",
+ ] {
+ assert!(invalid.parse::<ConversationId>().is_err());
+ assert!(from_value::<ConversationId>(json!(invalid)).is_err());
+ }
+ assert!(
+ format!("C{}", "1".repeat(64))
+ .parse::<ConversationId>()
+ .is_err()
+ );
+ assert!("T012ABC".parse::<SourceId>().is_ok());
+ assert!("E012ABC".parse::<SourceId>().is_err());
+ assert!("C012ABC".parse::<SourceId>().is_err());
+ for valid in ["U012ABC", "W012ABC", "USLACKBOT"] {
+ assert!(valid.parse::<SlackUserId>().is_ok());
+ }
+ assert!("T012ABC".parse::<SlackUserId>().is_err());
+}
+
+#[test]
+fn key_segments_and_server_keys_reject_traversal() {
+ for invalid in [
+ "",
+ ".",
+ "..",
+ "../channel",
+ "a/b",
+ "a\\b",
+ "a\0b",
+ "a\nb",
+ "%2e%2e",
+ "a%2fb",
+ ] {
+ assert!(invalid.parse::<KeySegment>().is_err(), "{invalid:?}");
+ assert!(from_value::<KeySegment>(json!(invalid)).is_err());
+ }
+ assert!("a".repeat(256).parse::<KeySegment>().is_err());
+ assert!("engineering team-2026".parse::<KeySegment>().is_ok());
+ assert!("slack/jobs/C123/0.ndjson".parse::<ObjectKey>().is_ok());
+ for invalid in ["/absolute", "a//b", "a/../b", "a/./b", "a/", "a/%2f/b"] {
+ assert!(invalid.parse::<ObjectKey>().is_err());
+ }
+}
+
+#[test]
+fn timestamps_preserve_microseconds_without_floats() {
+ let earlier: SlackTimestamp = "1727711111.000001".parse().unwrap();
+ let later: SlackTimestamp = "1727711111.000002".parse().unwrap();
+ assert_eq!(earlier.unix_micros(), 1_727_711_111_000_001);
+ assert_eq!(later.unix_micros() - earlier.unix_micros(), 1);
+ assert!(later > earlier);
+ assert_eq!(to_value(earlier).unwrap(), json!("1727711111.000001"));
+ assert_eq!(
+ "0001.1".parse::<SlackTimestamp>().unwrap().to_string(),
+ "1.100000"
+ );
+ let max: SlackTimestamp = "253402300799.999999".parse().unwrap();
+ assert_eq!(max.to_string(), "253402300799.999999");
+ assert_eq!(
+ from_value::<SlackTimestamp>(to_value(max).unwrap()).unwrap(),
+ max
+ );
+ assert_eq!("0.0".parse::<SlackTimestamp>().unwrap().unix_micros(), 0);
+}
+
+#[test]
+fn timestamps_reject_lossy_or_invalid_forms() {
+ for invalid in [
+ "",
+ "1",
+ "1.",
+ ".1",
+ "-1.000001",
+ "+1.0",
+ "1.0000001",
+ "1.1.1",
+ "1e3.1",
+ "NaN",
+ "1. 1",
+ "253402300800.0",
+ "999999999999999999999.0",
+ ] {
+ assert!(invalid.parse::<SlackTimestamp>().is_err(), "{invalid}");
+ assert!(from_value::<SlackTimestamp>(json!(invalid)).is_err());
+ }
+ assert!(from_value::<SlackTimestamp>(json!(1727711111.000001_f64)).is_err());
+}
+
+#[test]
+fn digest_is_exact_lowercase_sha256() {
+ assert!("f".repeat(64).parse::<Sha256Digest>().is_ok());
+ for invalid in [
+ "F".repeat(64),
+ "g".repeat(64),
+ "a".repeat(63),
+ "a".repeat(65),
+ ] {
+ assert!(invalid.parse::<Sha256Digest>().is_err());
+ }
+}
+
+#[test]
+fn status_wire_names_are_stable() {
+ for (status, wire) in [
+ (JobStatus::Uploading, "uploading"),
+ (JobStatus::Processing, "processing"),
+ (JobStatus::Completed, "completed"),
+ (JobStatus::CompletedWithErrors, "completed_with_errors"),
+ (JobStatus::Failed, "failed"),
+ (JobStatus::Cancelling, "cancelling"),
+ (JobStatus::Cancelled, "cancelled"),
+ ] {
+ assert_eq!(to_value(status).unwrap(), json!(wire));
+ assert_eq!(from_value::<JobStatus>(json!(wire)).unwrap(), status);
+ }
+ for (status, wire) in [
+ (ConversationStatus::AwaitingUploads, "awaiting_uploads"),
+ (ConversationStatus::Queued, "queued"),
+ (ConversationStatus::Importing, "importing"),
+ (ConversationStatus::Completed, "completed"),
+ (ConversationStatus::Skipped, "skipped"),
+ (ConversationStatus::Failed, "failed"),
+ ] {
+ assert_eq!(to_value(status).unwrap(), json!(wire));
+ assert_eq!(
+ from_value::<ConversationStatus>(json!(wire)).unwrap(),
+ status
+ );
+ }
+ assert!(from_value::<JobStatus>(json!("done")).is_err());
+}
+
+#[test]
+fn zero_part_seal_is_explicit_and_deterministic() {
+ let seal =
+ ConversationSeal::from_descriptors(conversation(), &[], &ImportLimits::default()).unwrap();
+ assert_eq!(
+ to_value(&seal).unwrap(),
+ json!({
+ "slackChannelId": "C012ABC", "partCount": 0,
+ "manifestSha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
+ })
+ );
+ let completion = CompleteUploads {
+ uploads: vec![],
+ seal: Some(seal),
+ };
+ assert_eq!(
+ from_value::<CompleteUploads>(to_value(&completion).unwrap()).unwrap(),
+ completion
+ );
+ assert_ne!(
+ to_value(Some(0_u32)).unwrap(),
+ to_value(None::<u32>).unwrap()
+ );
+}
+
+#[test]
+fn manifest_hash_is_canonical_and_registration_order_independent() {
+ let limits = ImportLimits::default();
+ let seal = ConversationSeal::from_descriptors(
+ conversation(),
+ &[descriptor(1), descriptor(0)],
+ &limits,
+ )
+ .unwrap();
+ let ordered = ConversationSeal::from_descriptors(
+ conversation(),
+ &[descriptor(0), descriptor(1)],
+ &limits,
+ )
+ .unwrap();
+ assert_eq!(seal, ordered);
+ let canonical = format!("0:{}:100:2\n1:{}:100:2\n", "a".repeat(64), "a".repeat(64));
+ assert_eq!(
+ seal.manifest_sha256.as_str(),
+ format!("{:x}", Sha256::digest(canonical))
+ );
+ let mut changed = descriptor(1);
+ changed.byte_length += 1;
+ assert_ne!(
+ seal,
+ ConversationSeal::from_descriptors(conversation(), &[descriptor(0), changed], &limits)
+ .unwrap()
+ );
+}
+
+#[test]
+fn manifests_reject_gaps_duplicates_wrong_conversations_and_users() {
+ let limits = ImportLimits::default();
+ for parts in [
+ vec![descriptor(1)],
+ vec![descriptor(0), descriptor(0)],
+ vec![descriptor(0), descriptor(2)],
+ ] {
+ assert_eq!(
+ ConversationSeal::from_descriptors(conversation(), &parts, &limits),
+ Err(ValidationError::InvalidManifest)
+ );
+ }
+ assert!(
+ ConversationSeal::from_descriptors("COTHER".parse().unwrap(), &[descriptor(0)], &limits)
+ .is_err()
+ );
+ let mut users = descriptor(0);
+ users.upload = UploadId::Users;
+ users.record_count = None;
+ assert!(users.validate(&limits).is_ok());
+ assert_eq!(
+ ConversationSeal::from_descriptors(conversation(), &[users], &limits),
+ Err(ValidationError::InvalidManifest)
+ );
+}
+
+#[test]
+fn descriptors_enforce_byte_and_record_bounds() {
+ let limits = ImportLimits::default();
+ let mut part = descriptor(0);
+ for bytes in [0, limits.part_bytes + 1] {
+ part.byte_length = bytes;
+ assert!(part.validate(&limits).is_err());
+ }
+ part.byte_length = limits.part_bytes;
+ part.record_count = Some(limits.part_records);
+ assert!(part.validate(&limits).is_ok());
+ for count in [None, Some(0), Some(limits.part_records + 1)] {
+ part.record_count = count;
+ assert!(part.validate(&limits).is_err());
+ }
+ let small = ImportLimits {
+ selected_bytes: 199,
+ ..limits
+ };
+ assert_eq!(
+ ConversationSeal::from_descriptors(conversation(), &[descriptor(0), descriptor(1)], &small),
+ Err(ValidationError::LimitExceeded)
+ );
+ part.upload = UploadId::Users;
+ part.record_count = None;
+ part.byte_length = limits.json_bytes;
+ assert!(part.validate(&limits).is_ok());
+ part.byte_length += 1;
+ assert!(part.validate(&limits).is_err());
+}
+
+#[test]
+fn create_requires_explicit_source_confirmation_and_full_metadata() {
+ let command = json!({
+ "idempotencyToken": Uuid::now_v7(),
+ "source": { "kind": "confirmed_unknown" },
+ "includeMessageHistory": false,
+ "conversations": [{
+ "slackChannelId": "C012ABC", "kind": "public_channel", "name": "general",
+ "folder": "general", "memberIds": ["U012ABC"], "creatorId": null,
+ "createdAt": null, "archived": false, "messageCount": null
+ }]
+ });
+ let create: CreateImport = from_value(command.clone()).unwrap();
+ assert_eq!(create.source, SourceIdentity::ConfirmedUnknown);
+ assert_eq!(to_value(&create).unwrap(), command);
+ let mut missing = command.clone();
+ missing["conversations"][0]
+ .as_object_mut()
+ .unwrap()
+ .remove("memberIds");
+ assert!(from_value::<CreateImport>(missing).is_err());
+ let mut missing = command.clone();
+ missing.as_object_mut().unwrap().remove("source");
+ assert!(from_value::<CreateImport>(missing).is_err());
+ let mut injected = command;
+ injected["teamId"] = json!(Uuid::now_v7());
+ assert!(from_value::<CreateImport>(injected).is_err());
+}
+
+#[test]
+fn queue_messages_cannot_smuggle_storage_keys_or_authority() {
+ let event = ImportEvent {
+ job_id: Uuid::now_v7().try_into().unwrap(),
+ slack_channel_id: conversation(),
+ generation: 1,
+ };
+ let encoded = to_value(&event).unwrap();
+ assert_eq!(encoded.as_object().unwrap().len(), 3);
+ assert_eq!(from_value::<ImportEvent>(encoded.clone()).unwrap(), event);
+ for forbidden in ["key", "s3Key", "teamId", "leaseToken"] {
+ let mut injected = encoded.clone();
+ injected[forbidden] = json!("untrusted");
+ assert!(from_value::<ImportEvent>(injected).is_err());
+ }
+}
+
+#[test]
+fn verified_object_validators_are_nonempty_and_do_not_claim_to_be_checksums() {
+ for invalid in ["", "version\n", "tag\0"] {
+ assert!(invalid.parse::<ObjectValidator>().is_err());
+ }
+ assert!("v".repeat(1025).parse::<ObjectValidator>().is_err());
+ let tag: ObjectValidator = "\"opaque-multipart-2\"".parse().unwrap();
+ assert!(tag.as_str().parse::<Sha256Digest>().is_err());
+ assert!(matches!(
+ ObjectIdentity::EntityTag(tag),
+ ObjectIdentity::EntityTag(_)
+ ));
+}
+
+#[test]
+fn diagnostics_and_search_receipts_have_sanitized_stable_shapes() {
+ assert_eq!(
+ to_value(ImportError::UploadMismatch).unwrap(),
+ json!("upload_mismatch")
+ );
+ assert_eq!(ImportError::Unavailable.to_string(), "import unavailable");
+ let receipt = Uuid::now_v7();
+ assert_eq!(
+ to_value(SearchState::Submitted {
+ receipt_id: receipt
+ })
+ .unwrap(),
+ json!({ "status": "submitted", "receiptId": receipt })
+ );
+ assert_eq!(
+ to_value(SourceIdentity::Known {
+ source_id: "T012ABC".parse().unwrap()
+ })
+ .unwrap(),
+ json!({ "kind": "known", "sourceId": "T012ABC" })
+ );
+}
diff --git a/crates/slack_integration/src/domain/ports.rs b/crates/slack_integration/src/domain/ports.rs
new file mode 100644
index 0000000000..4ac5527a0d
--- /dev/null
+++ b/crates/slack_integration/src/domain/ports.rs
@@ -0,0 +1,466 @@
+//! Capability contracts. Lifecycle policy belongs in domain services; adapters implement
+//! atomic persistence and external I/O. Only the worker composition root coordinates
+//! transaction-aware helpers from other domains.
+
+use std::{future::Future, pin::Pin};
+
+use chrono::{DateTime, Utc};
+use entity_access::domain::models::{AdminTeamRole, EntityAccessReceipt};
+use futures::Stream;
+use macro_user_id::user_id::MacroUserIdStr;
+use uuid::Uuid;
+
+use super::models::*;
+
+/// Internal failures retain diagnostic causes without serializing them into progress.
+pub type PortResult<T> = Result<T, rootcause::Report<ImportError>>;
+
+/// Atomic, fenced reconciliation supplied by the worker composition root.
+pub trait ReferenceReconciler: Send + Sync {
+ /// Settle at most this many bounded templates, including cancelled/partial work.
+ /// Each body patch, completion checkpoint and search marker commits together.
+ fn reconcile_references(&self, limit: u32) -> impl Future<Output = PortResult<()>> + Send;
+}
+
+/// Bounded chunks, not a buffered whole object. Adapters cap chunk size and stop at
+/// the descriptor byte limit; consumers additionally enforce NDJSON line/record bounds.
+pub type ByteStream = Pin<Box<dyn Stream<Item = PortResult<Vec<u8>>> + Send>>;
+
+/// Admin-facing use cases. Every call derives the team and actor from the receipt,
+/// checks job ownership, and never trusts a body-supplied team or object location.
+pub trait ImportService: Send + Sync + 'static {
+ /// Persist full metadata/source binding. Same scoped token and semantic payload
+ /// returns the original job even after registration closes; different payload conflicts.
+ fn create(
+ &self,
+ access: EntityAccessReceipt<AdminTeamRole>,
+ command: CreateImport,
+ ) -> impl Future<Output = Result<ImportProgress, ImportError>> + Send;
+
+ /// Register at most 50 descriptors and issue short-lived, conditional upload grants.
+ /// Identical retries renew grants; sealed descriptors cannot change or be extended.
+ fn register_uploads(
+ &self,
+ access: EntityAccessReceipt<AdminTeamRole>,
+ job: JobId,
+ command: RegisterUploads,
+ ) -> impl Future<Output = Result<Vec<UploadGrant>, ImportError>> + Send;
+
+ /// Verify persisted descriptors against storage; atomically seal and enqueue via
+ /// outbox only when users and every required part are verified. Never publish directly.
+ fn complete_uploads(
+ &self,
+ access: EntityAccessReceipt<AdminTeamRole>,
+ job: JobId,
+ command: CompleteUploads,
+ ) -> impl Future<Output = Result<ImportProgress, ImportError>> + Send;
+
+ /// Idempotently close registration and skip never-ready conversations, without
+ /// skipping queued/importing work. Shares the job lock/CAS with completion/cancel.
+ fn finalize(
+ &self,
+ access: EntityAccessReceipt<AdminTeamRole>,
+ command: JobCommand,
+ ) -> impl Future<Output = Result<ImportProgress, ImportError>> + Send;
+
+ /// Stop future claims/publication and skip unclaimed work. Active leases may finish;
+ /// terminal cancellation waits for settlement. Committed history is never rolled back.
+ fn cancel(
+ &self,
+ access: EntityAccessReceipt<AdminTeamRole>,
+ command: JobCommand,
+ ) -> impl Future<Output = Result<ImportProgress, ImportError>> + Send;
+
+ /// Return sanitized durable progress; inaccessible jobs are indistinguishable from missing jobs.
+ fn progress(
+ &self,
+ access: EntityAccessReceipt<AdminTeamRole>,
+ command: JobCommand,
+ ) -> impl Future<Output = Result<ImportProgress, ImportError>> + Send;
+
+ /// Up to 50 receipts in descending job-ID order, exclusively before `before`.
+ fn list(
+ &self,
+ access: EntityAccessReceipt<AdminTeamRole>,
+ before: Option<JobId>,
+ ) -> impl Future<Output = Result<ImportPage, ImportError>> + Send;
+}
+
+/// Claimed-work use cases, separate from administrator and maintenance services.
+/// The split lets the driver start heartbeats before slow storage or database work.
+pub trait ImportWorker: Send + Sync + 'static {
+ /// Load the durable requester and revalidate authorization before claiming/reclaiming.
+ fn claim(
+ &self,
+ event: &ImportEvent,
+ owner: WorkerId,
+ ) -> impl Future<Output = PortResult<ClaimOutcome>> + Send;
+
+ /// Process bounded atomic batches and durably settle permanent outcomes.
+ fn import(
+ &self,
+ context: &ClaimedConversation,
+ ) -> impl Future<Output = PortResult<WorkerOutcome>> + Send;
+
+ /// Renew the fence using database time. Any failure stops the current attempt.
+ fn heartbeat(&self, lease: &Lease) -> impl Future<Output = PortResult<Lease>> + Send;
+}
+
+/// Team-scoped persistence used by the admin service. All mutations serialize on
+/// the same job lock/CAS. Implementations enforce persisted invariants inside the
+/// transaction as well as accepting domain-validated commands.
+pub trait ImportRepo: Send + Sync + 'static {
+ /// Atomically bind the source and create metadata under (team, admin, token).
+ /// Check semantic equality on replay; never overwrite an existing create payload.
+ fn create(
+ &self,
+ team: TeamId,
+ admin: &MacroUserIdStr<'_>,
+ command: &CreateImport,
+ limits: &ImportLimits,
+ ) -> impl Future<Output = PortResult<ImportProgress>> + Send;
+
+ /// Persist immutable descriptors and server-generated keys. Reject unknown
+ /// conversations, aggregate byte overflow and additions/changes after seal/closure.
+ /// Exact descriptor retries remain readable for grant renewal while uploads are open.
+ fn register(
+ &self,
+ team: TeamId,
+ job: JobId,
+ command: &RegisterUploads,
+ ) -> impl Future<Output = PortResult<Vec<RegisteredUpload>>> + Send;
+
+ /// Resolve only identities already registered to this team/job. Prefix matching
+ /// is never sufficient authorization for a storage object.
+ fn uploads(
+ &self,
+ team: TeamId,
+ job: JobId,
+ uploads: &[UploadId],
+ ) -> impl Future<Output = PortResult<Vec<RegisteredUpload>>> + Send;
+
+ /// Compare verified objects to persisted descriptors and apply an optional seal
+ /// against the ENTIRE registered set. Readiness plus unique outbox events commit
+ /// atomically. A verified users payload can make several sealed conversations ready.
+ /// Replays are idempotent; no terminal state can be resurrected.
+ fn complete(
+ &self,
+ team: TeamId,
+ job: JobId,
+ verified: &[VerifiedUpload],
+ seal: Option<&ConversationSeal>,
+ ) -> impl Future<Output = PortResult<ImportProgress>> + Send;
+
+ /// Close registration, skip only never-ready work, and recompute the job atomically.
+ fn finalize(
+ &self,
+ team: TeamId,
+ job: JobId,
+ ) -> impl Future<Output = PortResult<ImportProgress>> + Send;
+
+ /// Close registration, suppress future claims/publications, skip unclaimed work,
+ /// and enter Cancelling until all active leases settle.
+ fn cancel(
+ &self,
+ team: TeamId,
+ job: JobId,
+ ) -> impl Future<Output = PortResult<ImportProgress>> + Send;
+
+ /// Load a team-owned receipt, without exposing storage/lease/internal diagnostics.
+ fn progress(
+ &self,
+ team: TeamId,
+ job: JobId,
+ ) -> impl Future<Output = PortResult<Option<ImportProgress>>> + Send;
+
+ /// Original human requester, resolved only within the authorized team/job.
+ /// Used for best-effort invalidation, never exposed in progress payloads.
+ fn requested_by(
+ &self,
+ team: TeamId,
+ job: JobId,
+ ) -> impl Future<Output = PortResult<Option<MacroUserIdStr<'static>>>> + Send;
+
+ /// At most 50 team-owned receipts, descending job ID, exclusively before the cursor.
+ fn list(
+ &self,
+ team: TeamId,
+ before: Option<JobId>,
+ ) -> impl Future<Output = PortResult<Vec<ImportProgress>>> + Send;
+}
+
+/// Durable execution state; no historical message inserts belong in this repository.
+pub trait ExecutionRepo: Send + Sync + 'static {
+ /// Look up the persisted requester before attempting a claim; queue contents are
+ /// not authorization. None means unknown/stale work and must not disclose job data.
+ fn requester(
+ &self,
+ event: &ImportEvent,
+ ) -> impl Future<Output = PortResult<Option<(TeamId, MacroUserIdStr<'static>)>>> + Send;
+
+ /// Claim Queued work or reclaim expired Importing work using database time and a
+ /// fresh token/generation. Reject cancellation and stale event generations. The
+ /// caller must revalidate the persisted administrator before every claim/reclaim.
+ fn claim(
+ &self,
+ event: &ImportEvent,
+ owner: WorkerId,
+ ) -> impl Future<Output = PortResult<ClaimOutcome>> + Send;
+
+ /// Renew only an unexpired matching lease. Return its new persisted expiry.
+ /// The queue driver separately extends delivery visibility every 60 seconds.
+ fn heartbeat(&self, lease: &Lease) -> impl Future<Output = PortResult<Lease>> + Send;
+
+ /// Fenced final transition plus job recomputation. Only Completed, Skipped or
+ /// Failed are allowed. Running work may finish after cancellation; committed
+ /// history must retain/schedule its dirty-search work even on failure.
+ fn settle(
+ &self,
+ lease: &Lease,
+ status: ConversationStatus,
+ error: Option<ImportError>,
+ ) -> impl Future<Output = PortResult<()>> + Send;
+
+ /// Unpublished import events in bounded pages. Exclude cancelled jobs under
+ /// the same job lock/CAS; deliveries racing cancellation still fail claim.
+ fn pending_events(
+ &self,
+ limit: u32,
+ ) -> impl Future<Output = PortResult<Vec<ImportEvent>>> + Send;
+
+ /// Mark an exact outbox generation published only after successful send.
+ /// Send followed by a crash may duplicate delivery, but cannot lose work.
+ fn mark_published(&self, event: &ImportEvent) -> impl Future<Output = PortResult<()>> + Send;
+
+ /// Persist an exhausted event's failure and recompute its job; stale generations
+ /// and newer active leases must not be failed by an old dead-letter delivery.
+ fn dead_letter(
+ &self,
+ event: &ImportEvent,
+ ) -> impl Future<Output = PortResult<WorkerOutcome>> + Send;
+
+ /// Bounded recovery using database time: republish recoverable expired work;
+ /// settle cancelled expired work without restarting; close abandoned uploads.
+ /// Retain partial counts and dirty search markers in every path.
+ fn reconcile(&self, limit: u32) -> impl Future<Output = PortResult<()>> + Send;
+
+ /// Pending scoped search work, including partial failed/cancelled imports.
+ fn pending_search(
+ &self,
+ limit: u32,
+ ) -> impl Future<Output = PortResult<Vec<SearchBackfill>>> + Send;
+
+ /// Store submission receipts or polled state only for the matching dirty generation.
+ /// A receipt for an older generation cannot clear newer committed dirty work.
+ fn record_search(
+ &self,
+ request: &SearchBackfill,
+ state: SearchState,
+ ) -> impl Future<Output = PortResult<()>> + Send;
+}
+
+/// Immutable bounded object storage. Implementations never unzip an archive.
+pub trait ImportStorage: Send + Sync + 'static {
+ /// Sign checksum, exact length, derived content type and create-only condition.
+ /// A retry reporting "already exists" requests verification, never overwrite access.
+ fn grant(
+ &self,
+ upload: &RegisteredUpload,
+ ) -> impl Future<Output = PortResult<UploadGrant>> + Send;
+
+ /// Compare storage's actual checksum and length (not ETag/user metadata), returning
+ /// the pinned identity only when all immutable descriptor properties match.
+ fn verify(
+ &self,
+ upload: &RegisteredUpload,
+ ) -> impl Future<Output = PortResult<VerifiedUpload>> + Send;
+
+ /// Stream bounded chunks from a pinned verified object. Reject a changed identity
+ /// and enforce the total byte limit even if storage supplied incorrect metadata.
+ fn read(&self, upload: &VerifiedUpload) -> impl Future<Output = PortResult<ByteStream>> + Send;
+}
+
+/// Publication only. Receive/ack/visibility handles belong to the worker driver,
+/// not to domain messages, and no queue payload carries trusted storage keys.
+pub trait ImportQueue: Send + Sync + 'static {
+ /// Publish an identity-only event; duplicates are expected and safe.
+ fn publish(&self, event: &ImportEvent) -> impl Future<Output = PortResult<()>> + Send;
+}
+
+/// Delivery controls with opaque adapter-owned receipts. Payloads remain untrusted.
+/// The driver receives only one delivery per available processing slot.
+pub trait ImportConsumer: Send + Sync {
+ /// Opaque acknowledgement capability; never logged or persisted as domain state.
+ type Delivery: Send + Sync;
+
+ /// Long-poll one message from the main queue or its dead-letter queue.
+ fn receive(
+ &self,
+ dead_letter: bool,
+ ) -> impl Future<Output = PortResult<Option<Self::Delivery>>> + Send;
+ /// Strictly decoded identity plus the provider's approximate receive count.
+ fn envelope(delivery: &Self::Delivery) -> (Result<ImportEvent, ImportError>, u32);
+ /// Extend delivery visibility independently of the database fence.
+ fn extend(&self, delivery: &Self::Delivery) -> impl Future<Output = PortResult<()>> + Send;
+ /// Acknowledge only a durable outcome, or an unidentifiable poison envelope.
+ fn delete(&self, delivery: &Self::Delivery) -> impl Future<Output = PortResult<()>> + Send;
+}
+
+/// Separate requester and target authorization, evaluated by domain orchestration.
+pub trait ImportAuthorizer: Send + Sync + 'static {
+ /// Revalidate the original requester's current team-admin role before work/reclaim.
+ fn require_admin(
+ &self,
+ team: TeamId,
+ user: &MacroUserIdStr<'_>,
+ ) -> impl Future<Output = PortResult<()>> + Send;
+
+ /// Authorize target type, team provenance and requester access independently of
+ /// source claims. Private/DM reuse requires access or durable team import provenance.
+ /// Failure skips the conversation without revealing an inaccessible target ID.
+ fn require_target(
+ &self,
+ team: TeamId,
+ user: &MacroUserIdStr<'_>,
+ metadata: &ConversationMetadata,
+ target: Uuid,
+ ) -> impl Future<Output = PortResult<()>> + Send;
+}
+
+/// Read-only disclosure for administrator receipts, separate from import-write authority.
+pub trait ImportProgressAccess: Send + Sync + 'static {
+ /// Return targets in which this viewer currently participates. Conservatively
+ /// omit all other IDs, including team-visible channels without participation.
+ /// Inputs are deduplicated and bounded to 500 IDs per call.
+ fn participating_targets(
+ &self,
+ viewer: &MacroUserIdStr<'_>,
+ targets: &[Uuid],
+ ) -> impl Future<Output = PortResult<Vec<Uuid>>> + Send;
+}
+
+/// Best-effort invalidation only; persisted polling is the source of truth.
+pub trait ImportNotifier: Send + Sync + 'static {
+ /// Notify the requesting administrator's gateway entity of a job revision using
+ /// `slack_import_updated`; other admins poll. No keys, source content, target IDs
+ /// or detailed progress in the payload. Caller revalidates admin access first.
+ fn invalidate(
+ &self,
+ requester: &MacroUserIdStr<'_>,
+ team: TeamId,
+ job: JobId,
+ revision: u64,
+ status: JobStatus,
+ ) -> impl Future<Output = PortResult<()>> + Send;
+}
+
+/// Import-owned canonical target/ledger service shared by archive and onboarding.
+/// Cross-domain adapters depend on this port, not the import crate's outbound module.
+pub trait ImportLedger: Send + Sync + 'static {
+ /// Read the team's single-source binding for the admin picker.
+ fn source_binding(
+ &self,
+ team: TeamId,
+ ) -> impl Future<Output = PortResult<SourceBinding>> + Send;
+
+ /// Atomically reserve or return the stable target for (team, Slack, foreign ID).
+ /// Ambiguous legacy mappings fail closed; never allocate a second candidate.
+ /// Pass the persisted requester and metadata, not queue-supplied context. An
+ /// existing target must already be independently authorized by the caller.
+ fn reserve(
+ &self,
+ team: TeamId,
+ requester: &MacroUserIdStr<'static>,
+ metadata: &ConversationMetadata,
+ authorized_existing_target: Option<Uuid>,
+ ) -> impl Future<Output = PortResult<TargetReservation>> + Send;
+
+ /// Mark a successfully created reserved target ready, preserving the first mapping.
+ /// Private/DM provenance must not leak through the team-wide onboarding listing.
+ fn complete(
+ &self,
+ reservation: &TargetReservation,
+ kind: ConversationKind,
+ ) -> impl Future<Output = PortResult<()>> + Send;
+}
+
+/// Import-owned read-only source-message mapping capability.
+pub trait SourceMessageReader: Send + Sync {
+ /// Explicit team namespace, bounded exact identities; mapping alone proves
+ /// neither message existence nor read access. Reject mixed-team inputs.
+ fn reference_mappings(
+ &self,
+ team: TeamId,
+ sources: &[SourceMessageId],
+ ) -> impl Future<
+ Output = PortResult<Vec<super::slack::references::resolve::StoredMessageMapping>>,
+ > + Send;
+}
+
+/// Batched read-only reference capabilities. Every method is bounded by the
+/// database record/byte ceilings; none may reserve targets or mutate membership.
+/// Context must come from persisted job ownership, not a client-supplied requester.
+pub trait ReferenceLookup: Send + Sync {
+ /// Load canonical channel facts in input order, without authorizing disclosure.
+ fn channels(
+ &self,
+ context: &super::slack::references::resolve::ReferenceContext,
+ channels: &[ConversationId],
+ ) -> impl Future<Output = PortResult<Vec<super::slack::references::resolve::ChannelMapping>>> + Send;
+
+ /// Selected nonterminal conversations in this job which may supply references.
+ fn pending_channels(
+ &self,
+ context: &super::slack::references::resolve::ReferenceContext,
+ channels: &[ConversationId],
+ ) -> impl Future<Output = PortResult<Vec<ConversationId>>> + Send;
+
+ /// Read source mappings and validate actual message ownership/deletion and root
+ /// state through the message owner. Results correspond exactly to input order.
+ fn messages(
+ &self,
+ context: &super::slack::references::resolve::ReferenceContext,
+ sources: &[SourceMessageId],
+ ) -> impl Future<Output = PortResult<Vec<super::slack::references::resolve::MessageMapping>>> + Send;
+
+ /// Current read-access facts, never the import-write provenance shortcut.
+ fn disclosure_access(
+ &self,
+ context: &super::slack::references::resolve::ReferenceContext,
+ channels: &[Uuid],
+ ) -> impl Future<Output = PortResult<super::slack::references::resolve::DisclosureAccess>> + Send;
+}
+
+/// Worker composition-root coordinator for a transaction spanning owning-crate helpers.
+/// Domain signatures never expose SQL transactions. No live send/message effects.
+pub trait HistoricalSink: Send + Sync + 'static {
+ /// Resolve bounded thread-parent/source mappings; first committed source ID wins.
+ fn lookup(
+ &self,
+ sources: &[SourceMessageId],
+ ) -> impl Future<Output = PortResult<Vec<(SourceMessageId, Uuid)>>> + Send;
+
+ /// Atomically commit messages, reactions, mentions, source mappings, checkpoint,
+ /// counters and dirty-search marker under the lease fence. Roll back EVERYTHING
+ /// on failure; validate team/channel/parent ownership and count/byte bounds.
+ /// Return cumulative committed counters, not speculative attempted inserts.
+ fn commit(
+ &self,
+ batch: HistoricalBatch,
+ ) -> impl Future<Output = PortResult<ImportCounters>> + Send;
+}
+
+/// Explicit scoped search indexing, independent of historical persistence completion.
+pub trait SearchBackfillClient: Send + Sync + 'static {
+ /// Submit only named imported channels; return acceptance receipt, not completion.
+ fn submit(&self, request: &SearchBackfill) -> impl Future<Output = PortResult<Uuid>> + Send;
+
+ /// Poll a stored receipt and distinguish accepted/running from complete/failed.
+ fn progress(&self, receipt: Uuid) -> impl Future<Output = PortResult<SearchState>> + Send;
+}
+
+/// Injectable time for grant expiry and service policy; lease fences use database time.
+pub trait Clock: Send + Sync + 'static {
+ /// Current wall-clock time.
+ fn now(&self) -> DateTime<Utc>;
+}
diff --git a/crates/slack_integration/src/domain/reference_reconciliation.rs b/crates/slack_integration/src/domain/reference_reconciliation.rs
new file mode 100644
index 0000000000..ac921b647a
--- /dev/null
+++ b/crates/slack_integration/src/domain/reference_reconciliation.rs
@@ -0,0 +1,63 @@
+//! Final within-job reference resolution. All selected work must have settled;
+//! pending/missing targets now keep their source-only fallback permanently.
+
+use super::{
+ models::*,
+ ports::{PortResult, ReferenceLookup},
+ slack::references::{
+ ConvertedText,
+ resolve::{ReferenceContext, ReferenceOutcome, resolve_batch},
+ },
+};
+
+#[cfg(test)]
+mod test;
+
+/// Persisted template and importer-body guard version. Changes require explicit
+/// compatibility handling; an unknown version is closed without rewriting a body.
+pub const IMPORTER_BODY_VERSION: i16 = 1;
+
+/// Resolve only against committed canonical mappings and current disclosure access.
+/// The caller holds a reconciliation fence and atomically guards the body update,
+/// completes the intent, and schedules scoped search publication.
+pub async fn final_body(
+ lookup: &impl ReferenceLookup,
+ context: &ReferenceContext,
+ template: &ConvertedText,
+ version: i16,
+ limits: &ImportLimits,
+) -> PortResult<Option<String>> {
+ if version != IMPORTER_BODY_VERSION {
+ return Ok(None);
+ }
+ let outcomes =
+ match resolve_batch(lookup, context, std::slice::from_ref(template), limits).await {
+ Ok(outcomes) => outcomes,
+ // Invalid durable evidence must not hold a partial/cancelled job forever.
+ Err(error)
+ if matches!(
+ error.current_context(),
+ ImportError::InvalidInput | ImportError::LimitExceeded
+ ) =>
+ {
+ return Ok(None);
+ }
+ Err(error) => return Err(error),
+ };
+ let targets: Vec<_> = outcomes
+ .into_iter()
+ .flatten()
+ .map(|outcome| match outcome {
+ ReferenceOutcome::Resolved(target) => Some(target),
+ ReferenceOutcome::Pending | ReferenceOutcome::Fallback => None,
+ })
+ .collect();
+ let Ok(body) = template.render(&targets) else {
+ return Ok(None);
+ };
+ // Expansion must not turn a valid fallback into an unbounded stored message.
+ if body == template.body || body.len() > limits.record_bytes as usize {
+ return Ok(None);
+ }
+ Ok(Some(body))
+}
diff --git a/crates/slack_integration/src/domain/reference_reconciliation/test.rs b/crates/slack_integration/src/domain/reference_reconciliation/test.rs
new file mode 100644
index 0000000000..7a6825ee2e
--- /dev/null
+++ b/crates/slack_integration/src/domain/reference_reconciliation/test.rs
@@ -0,0 +1,198 @@
+use super::*;
+use crate::domain::slack::{mrkdwn::MrkdwnConverter, references::resolve::*, users::UserDirectory};
+use macro_user_id::user_id::MacroUserIdStr;
+use std::collections::{BTreeMap, HashMap};
+use uuid::Uuid;
+
+struct Lookup {
+ channel: Uuid,
+ messages: HashMap<SlackTimestamp, Uuid>,
+ allowed: bool,
+ pending: bool,
+}
+impl ReferenceLookup for Lookup {
+ async fn channels(
+ &self,
+ _: &ReferenceContext,
+ channels: &[ConversationId],
+ ) -> PortResult<Vec<ChannelMapping>> {
+ Ok(channels
+ .iter()
+ .map(|_| ChannelMapping::Ready {
+ id: self.channel,
+ name: "canonical".into(),
+ team_visible: true,
+ })
+ .collect())
+ }
+ async fn pending_channels(
+ &self,
+ _: &ReferenceContext,
+ channels: &[ConversationId],
+ ) -> PortResult<Vec<ConversationId>> {
+ Ok(if self.pending {
+ channels.to_vec()
+ } else {
+ vec![]
+ })
+ }
+ async fn messages(
+ &self,
+ _: &ReferenceContext,
+ sources: &[SourceMessageId],
+ ) -> PortResult<Vec<MessageMapping>> {
+ Ok(sources
+ .iter()
+ .map(|source| match self.messages.get(&source.ts) {
+ Some(id) => MessageMapping::Ready {
+ channel: self.channel,
+ message: *id,
+ root: *id,
+ },
+ None => MessageMapping::Missing,
+ })
+ .collect())
+ }
+ async fn disclosure_access(
+ &self,
+ _: &ReferenceContext,
+ _: &[Uuid],
+ ) -> PortResult<DisclosureAccess> {
+ Ok(DisclosureAccess {
+ team_member: self.allowed,
+ ..Default::default()
+ })
+ }
+}
+fn context() -> ReferenceContext {
+ ReferenceContext {
+ team: Uuid::now_v7().try_into().unwrap(),
+ job: Uuid::now_v7().try_into().unwrap(),
+ requester: MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap(),
+ source: SourceIdentity::Known {
+ source_id: "T1".parse().unwrap(),
+ },
+ binding: SourceBinding::Known {
+ source_id: "T1".parse().unwrap(),
+ },
+ domains: vec![WorkspaceDomain {
+ source: "T1".parse().unwrap(),
+ hostname: "example.slack.com".into(),
+ }],
+ }
+}
+fn template(source: &str) -> ConvertedText {
+ MrkdwnConverter {
+ users: &UserDirectory::default(),
+ channels: &BTreeMap::new(),
+ }
+ .convert_with_references(source, false)
+ .unwrap()
+}
+
+#[tokio::test]
+async fn self_cycles_prior_imports_and_reverse_order_use_only_final_canonical_ids() {
+ let ids = [Uuid::now_v7(), Uuid::now_v7(), Uuid::now_v7()];
+ let context = context();
+ let mut lookup = Lookup {
+ channel: Uuid::now_v7(),
+ messages: HashMap::new(),
+ allowed: true,
+ pending: false,
+ };
+ let records: Vec<_> = (1..=3).map(|index| template(&format!(
+ "https://example.slack.com/archives/C{index}/p{index}000001 https://example.slack.com/archives/C{}/p{}000001",
+ index % 3 + 1, index % 3 + 1,
+ ))).collect();
+ let mut expected = Vec::new();
+ for order in [[0, 1, 2], [2, 1, 0], [1, 2, 0]] {
+ lookup.messages.clear();
+ // Models independently committed parts/conversations, including prior-job maps.
+ for index in order {
+ lookup
+ .messages
+ .insert(format!("{}.000001", index + 1).parse().unwrap(), ids[index]);
+ }
+ let mut bodies = Vec::new();
+ for (index, record) in records.iter().enumerate() {
+ assert!(!record.body.contains("m-document-mention"));
+ let body = final_body(
+ &lookup,
+ &context,
+ record,
+ IMPORTER_BODY_VERSION,
+ &ImportLimits::default(),
+ )
+ .await
+ .unwrap()
+ .unwrap();
+ assert!(body.contains(&ids[index].to_string()));
+ assert!(body.contains(&ids[(index + 1) % 3].to_string()));
+ assert!(body.contains(&lookup.channel.to_string()));
+ bodies.push(body);
+ }
+ if expected.is_empty() {
+ expected = bodies;
+ } else {
+ assert_eq!(bodies, expected);
+ }
+ }
+}
+
+#[tokio::test]
+async fn missing_skipped_shape_only_pending_revoked_and_unproven_targets_close_to_fallback() {
+ let mut lookup = Lookup {
+ channel: Uuid::now_v7(),
+ messages: HashMap::new(),
+ allowed: true,
+ pending: true,
+ };
+ let record = template("https://example.slack.com/archives/C1/p1000001");
+ let mut context = context();
+ let limits = ImportLimits::default();
+ assert!(
+ final_body(&lookup, &context, &record, 1, &limits)
+ .await
+ .unwrap()
+ .is_none()
+ );
+ lookup.pending = false;
+ assert!(
+ final_body(&lookup, &context, &record, 1, &limits)
+ .await
+ .unwrap()
+ .is_none()
+ );
+ lookup
+ .messages
+ .insert("1.000001".parse().unwrap(), Uuid::now_v7());
+ lookup.allowed = false;
+ assert!(
+ final_body(&lookup, &context, &record, 1, &limits)
+ .await
+ .unwrap()
+ .is_none()
+ );
+ lookup.allowed = true;
+ assert!(
+ final_body(&lookup, &context, &record, 2, &limits)
+ .await
+ .unwrap()
+ .is_none()
+ );
+ context.domains.clear();
+ assert!(
+ final_body(&lookup, &context, &record, 1, &limits)
+ .await
+ .unwrap()
+ .is_none()
+ );
+ let mut invalid = record;
+ invalid.references[0].range.start = usize::MAX;
+ assert!(
+ final_body(&lookup, &context, &invalid, 1, &limits)
+ .await
+ .unwrap()
+ .is_none()
+ );
+}
diff --git a/crates/slack_integration/src/domain/service.rs b/crates/slack_integration/src/domain/service.rs
new file mode 100644
index 0000000000..ad995f8be8
--- /dev/null
+++ b/crates/slack_integration/src/domain/service.rs
@@ -0,0 +1,381 @@
+//! Administrator use cases. Receipts scope every repository operation; storage
+//! only sees persisted manifest entries. Readiness is committed by the repository
+//! with its outbox, never by sending a queue message from this service.
+
+use std::collections::HashSet;
+
+use entity_access::domain::models::{AdminTeamRole, EntityAccessReceipt, EntityType};
+
+use super::{models::*, ports::*};
+
+#[cfg(test)]
+mod test;
+
+/// Admin orchestration with a team-scoped backend rollout predicate. The host
+/// supplies current configuration; frontend feature flags confer no authority.
+#[derive(Clone)]
+pub struct SlackImportService<R, S, L, N, A, G> {
+ repo: R,
+ storage: S,
+ ledger: L,
+ notifier: N,
+ authorizer: A,
+ enabled: G,
+ limits: ImportLimits,
+}
+
+impl<R, S, L, N, A, G> SlackImportService<R, S, L, N, A, G> {
+ /// Construct with effective limits shared by the repository and browser.
+ /// Reject unusable configuration before serving requests.
+ pub fn new(
+ repo: R,
+ storage: S,
+ ledger: L,
+ notifier: N,
+ authorizer: A,
+ enabled: G,
+ limits: ImportLimits,
+ ) -> Result<Self, ImportError> {
+ if limits.registration_batch == 0
+ || limits.registration_batch > 50
+ || limits.conversations == 0
+ || limits.zip_entries == 0
+ || limits.part_records == 0
+ || limits.database_batch_messages == 0
+ || limits.record_bytes == 0
+ || limits.part_bytes < limits.record_bytes
+ || limits.json_bytes == 0
+ || limits.database_batch_bytes < limits.record_bytes
+ || limits.selected_bytes < limits.part_bytes.max(limits.json_bytes)
+ || limits.selected_bytes > i64::MAX as u64
+ {
+ return Err(ImportError::InvalidInput);
+ }
+ Ok(Self {
+ repo,
+ storage,
+ ledger,
+ notifier,
+ authorizer,
+ enabled,
+ limits,
+ })
+ }
+}
+
+impl<R, S, L, N, A, G> SlackImportService<R, S, L, N, A, G>
+where
+ R: ImportRepo,
+ S: ImportStorage,
+ L: ImportLedger,
+ N: ImportNotifier,
+ A: ImportAuthorizer + ImportProgressAccess,
+ G: Fn(TeamId) -> bool + Send + Sync + 'static,
+{
+ async fn disclose_targets(
+ &self,
+ access: &EntityAccessReceipt<AdminTeamRole>,
+ jobs: &mut [ImportProgress],
+ ) {
+ let targets: Vec<_> = jobs
+ .iter()
+ .flat_map(|job| &job.conversations)
+ .filter_map(|conversation| conversation.channel_id)
+ .collect::<HashSet<_>>()
+ .into_iter()
+ .collect();
+ let mut visible = HashSet::new();
+ if let Ok(viewer) = access.get_authenticated_user() {
+ for batch in targets.chunks(500) {
+ // A disclosure outage must not turn a committed write into a failed
+ // mutation. Keep progress and source metadata, but fail closed on IDs.
+ match self.authorizer.participating_targets(viewer, batch).await {
+ Ok(ids) => visible.extend(ids),
+ Err(error) => {
+ tracing::warn!(error = ?error.current_context(), "Slack import target disclosure unavailable")
+ }
+ }
+ }
+ }
+ for conversation in jobs.iter_mut().flat_map(|job| &mut job.conversations) {
+ if conversation.status == ConversationStatus::Skipped
+ || conversation
+ .channel_id
+ .is_some_and(|id| !visible.contains(&id))
+ {
+ conversation.channel_id = None;
+ }
+ }
+ }
+
+ fn team(&self, access: &EntityAccessReceipt<AdminTeamRole>) -> Result<TeamId, ImportError> {
+ if access.entity().entity_type != EntityType::Team {
+ return Err(ImportError::AdminRequired);
+ }
+ access
+ .entity()
+ .entity_id
+ .parse()
+ .map_err(|_| ImportError::AdminRequired)
+ }
+
+ fn require_enabled(&self, team: TeamId) -> Result<(), ImportError> {
+ if !(self.enabled)(team) {
+ return Err(ImportError::Disabled);
+ }
+ Ok(())
+ }
+
+ async fn notify(&self, team: TeamId, progress: &ImportProgress) {
+ // The caller may be another administrator. Never notify that caller in
+ // place of the original requester, and revalidate the requester first.
+ let result: PortResult<()> = async {
+ let Some(requester) = self.repo.requested_by(team, progress.job_id).await? else {
+ return Ok(());
+ };
+ self.authorizer.require_admin(team, &requester).await?;
+ self.notifier
+ .invalidate(
+ &requester,
+ team,
+ progress.job_id,
+ progress.revision,
+ progress.status,
+ )
+ .await
+ }
+ .await;
+ let _ = result.inspect_err(|error| {
+ tracing::warn!(error = ?error.current_context(), "Slack import invalidation failed; polling remains available");
+ });
+ }
+}
+
+impl<R, S, L, N, A, G> ImportService for SlackImportService<R, S, L, N, A, G>
+where
+ R: ImportRepo,
+ S: ImportStorage,
+ L: ImportLedger,
+ N: ImportNotifier,
+ A: ImportAuthorizer + ImportProgressAccess,
+ G: Fn(TeamId) -> bool + Send + Sync + 'static,
+{
+ async fn create(
+ &self,
+ access: EntityAccessReceipt<AdminTeamRole>,
+ command: CreateImport,
+ ) -> Result<ImportProgress, ImportError> {
+ let team = self.team(&access)?;
+ self.require_enabled(team)?;
+ // V1 requires a directly authenticated human, not a bot principal or
+ // even a user-scoped bot's acting-user context.
+ let requester = access
+ .get_authenticated_user()
+ .map_err(|_| ImportError::AdminRequired)?;
+ validate_create(&command, &self.limits)?;
+ // Binding comparison and creation are one transaction, including replay
+ // checks. A separate preflight binding check would race other creates.
+ let mut progress = self
+ .repo
+ .create(team, requester, &command, &self.limits)
+ .await
+ .map_err(public_error)?;
+ self.notify(team, &progress).await;
+ self.disclose_targets(&access, std::slice::from_mut(&mut progress))
+ .await;
+ Ok(progress)
+ }
+
+ async fn register_uploads(
+ &self,
+ access: EntityAccessReceipt<AdminTeamRole>,
+ job: JobId,
+ command: RegisterUploads,
+ ) -> Result<Vec<UploadGrant>, ImportError> {
+ let team = self.team(&access)?;
+ self.require_enabled(team)?;
+ validate_batch(command.descriptors.iter().map(|d| &d.upload), &self.limits)?;
+ for descriptor in &command.descriptors {
+ descriptor
+ .validate(&self.limits)
+ .map_err(|_| ImportError::LimitExceeded)?;
+ }
+ // The transaction checks selection, immutable seals, closure and total
+ // bytes before any signing. Exact retries do not consume quota twice.
+ let registered = self
+ .repo
+ .register(team, job, &command)
+ .await
+ .map_err(public_error)?;
+ let mut grants = Vec::with_capacity(registered.len());
+ for upload in registered {
+ grants.push(self.storage.grant(&upload).await.map_err(public_error)?);
+ }
+ Ok(grants)
+ }
+
+ async fn complete_uploads(
+ &self,
+ access: EntityAccessReceipt<AdminTeamRole>,
+ job: JobId,
+ command: CompleteUploads,
+ ) -> Result<ImportProgress, ImportError> {
+ let team = self.team(&access)?;
+ self.require_enabled(team)?;
+ validate_batch(command.uploads.iter(), &self.limits)?;
+ // Resolve the ENTIRE batch before issuing any HEAD; an unregistered or
+ // wrong-team identity must not trigger storage I/O for a partial batch.
+ let registered = self
+ .repo
+ .uploads(team, job, &command.uploads)
+ .await
+ .map_err(public_error)?;
+ let mut verified = Vec::with_capacity(registered.len());
+ for upload in registered {
+ verified.push(self.storage.verify(&upload).await.map_err(public_error)?);
+ }
+ let mut progress = self
+ .repo
+ .complete(team, job, &verified, command.seal.as_ref())
+ .await
+ .map_err(public_error)?;
+ self.notify(team, &progress).await;
+ self.disclose_targets(&access, std::slice::from_mut(&mut progress))
+ .await;
+ Ok(progress)
+ }
+
+ async fn finalize(
+ &self,
+ access: EntityAccessReceipt<AdminTeamRole>,
+ command: JobCommand,
+ ) -> Result<ImportProgress, ImportError> {
+ let team = self.team(&access)?;
+ let mut progress = self
+ .repo
+ .finalize(team, command.job_id)
+ .await
+ .map_err(public_error)?;
+ self.notify(team, &progress).await;
+ self.disclose_targets(&access, std::slice::from_mut(&mut progress))
+ .await;
+ Ok(progress)
+ }
+
+ async fn cancel(
+ &self,
+ access: EntityAccessReceipt<AdminTeamRole>,
+ command: JobCommand,
+ ) -> Result<ImportProgress, ImportError> {
+ let team = self.team(&access)?;
+ let mut progress = self
+ .repo
+ .cancel(team, command.job_id)
+ .await
+ .map_err(public_error)?;
+ self.notify(team, &progress).await;
+ self.disclose_targets(&access, std::slice::from_mut(&mut progress))
+ .await;
+ Ok(progress)
+ }
+
+ async fn progress(
+ &self,
+ access: EntityAccessReceipt<AdminTeamRole>,
+ command: JobCommand,
+ ) -> Result<ImportProgress, ImportError> {
+ let team = self.team(&access)?;
+ let mut progress = self
+ .repo
+ .progress(team, command.job_id)
+ .await
+ .map_err(public_error)?
+ .ok_or(ImportError::Unavailable)?;
+ self.disclose_targets(&access, std::slice::from_mut(&mut progress))
+ .await;
+ Ok(progress)
+ }
+
+ async fn list(
+ &self,
+ access: EntityAccessReceipt<AdminTeamRole>,
+ before: Option<JobId>,
+ ) -> Result<ImportPage, ImportError> {
+ let team = self.team(&access)?;
+ let mut jobs = self.repo.list(team, before).await.map_err(public_error)?;
+ self.disclose_targets(&access, &mut jobs).await;
+ let next_cursor = if jobs.len() == 50 {
+ jobs.last().map(|job| job.job_id)
+ } else {
+ None
+ };
+ let source_binding = self
+ .ledger
+ .source_binding(team)
+ .await
+ .map_err(public_error)?;
+ Ok(ImportPage {
+ jobs,
+ next_cursor,
+ limits: self.limits,
+ source_binding,
+ })
+ }
+}
+
+fn public_error(error: rootcause::Report<ImportError>) -> ImportError {
+ error.into_current_context()
+}
+
+fn validate_batch<'a>(
+ uploads: impl Iterator<Item = &'a UploadId>,
+ limits: &ImportLimits,
+) -> Result<(), ImportError> {
+ let mut ids = HashSet::new();
+ for upload in uploads {
+ if !ids.insert(upload) {
+ return Err(ImportError::InvalidInput);
+ }
+ if ids.len() > limits.registration_batch as usize {
+ return Err(ImportError::LimitExceeded);
+ }
+ }
+ Ok(())
+}
+
+fn validate_create(command: &CreateImport, limits: &ImportLimits) -> Result<(), ImportError> {
+ if command.conversations.is_empty() {
+ return Err(ImportError::InvalidInput);
+ }
+ if command.conversations.len() > limits.conversations as usize {
+ return Err(ImportError::LimitExceeded);
+ }
+ let mut ids = HashSet::new();
+ let mut folders = HashSet::new();
+ for metadata in &command.conversations {
+ if !ids.insert(&metadata.slack_channel_id)
+ || !folders.insert(&metadata.folder)
+ || metadata.name.trim().is_empty()
+ || metadata.name.chars().any(char::is_control)
+ {
+ return Err(ImportError::InvalidInput);
+ }
+ if metadata
+ .message_count
+ .is_some_and(|count| count > i64::MAX as u64)
+ {
+ return Err(ImportError::LimitExceeded);
+ }
+ // Do not infer kind from a Slack ID prefix or reject missing timestamps,
+ // empty memberships, or unsupported DM pairs: those are valid source
+ // metadata and have explicit worker fallback/skip behavior.
+ }
+ if serde_json::to_vec(command)
+ .map_err(|_| ImportError::InvalidInput)?
+ .len() as u64
+ > limits.json_bytes
+ {
+ return Err(ImportError::LimitExceeded);
+ }
+ Ok(())
+}
diff --git a/crates/slack_integration/src/domain/service/test.rs b/crates/slack_integration/src/domain/service/test.rs
new file mode 100644
index 0000000000..59c0fa8d0a
--- /dev/null
+++ b/crates/slack_integration/src/domain/service/test.rs
@@ -0,0 +1,1012 @@
+use super::*;
+use chrono::Utc;
+use entity_access::domain::models::{
+ BotIdStr, BotReceiptScope, Entity, EntityAccessAuth, EntityPermission, TeamRole,
+};
+use macro_user_id::{cowlike::CowLike, user_id::MacroUserIdStr};
+use std::{
+ collections::HashMap,
+ sync::{
+ Arc, Mutex,
+ atomic::{AtomicBool, Ordering},
+ },
+};
+use uuid::Uuid;
+
+#[derive(Clone, Default)]
+struct Fake(Arc<Mutex<State>>);
+
+#[derive(Default)]
+struct State {
+ owner: Option<(TeamId, MacroUserIdStr<'static>, CreateImport)>,
+ progress: Option<ImportProgress>,
+ uploads: HashMap<UploadId, RegisteredUpload>,
+ sealed: bool,
+ calls: Vec<&'static str>,
+ notified: Vec<String>,
+ fail_notification: bool,
+ fail_disclosure: bool,
+ readable_targets: HashSet<Uuid>,
+ disclosure_calls: Vec<(String, Vec<Uuid>)>,
+ revoked: bool,
+ binding: Option<SourceId>,
+}
+
+fn team() -> TeamId {
+ Uuid::from_u128(1).try_into().unwrap()
+}
+fn user() -> MacroUserIdStr<'static> {
+ MacroUserIdStr::parse_from_str("macro|[email protected]")
+ .unwrap()
+ .into_owned()
+}
+fn access(team: TeamId) -> EntityAccessReceipt<AdminTeamRole> {
+ receipt(team, EntityAccessAuth::Authenticated(user()))
+}
+fn receipt(team: TeamId, auth: EntityAccessAuth) -> EntityAccessReceipt<AdminTeamRole> {
+ EntityAccessReceipt::try_new(
+ auth,
+ Entity {
+ entity_id: team.to_string(),
+ entity_type: EntityType::Team,
+ },
+ EntityPermission::TeamRole {
+ role: TeamRole::Admin,
+ },
+ )
+ .unwrap()
+}
+fn command() -> CreateImport {
+ CreateImport {
+ idempotency_token: Uuid::now_v7().try_into().unwrap(),
+ source: SourceIdentity::Known {
+ source_id: "T123".parse().unwrap(),
+ },
+ include_message_history: true,
+ conversations: vec![ConversationMetadata {
+ slack_channel_id: "C123".parse().unwrap(),
+ kind: ConversationKind::PublicChannel,
+ name: "general".into(),
+ folder: "general".parse().unwrap(),
+ member_ids: vec!["U123".parse().unwrap()],
+ creator_id: None,
+ created_at: None,
+ archived: false,
+ message_count: None,
+ }],
+ }
+}
+fn descriptor(upload: UploadId) -> UploadDescriptor {
+ let record_count = match upload {
+ UploadId::Users => None,
+ _ => Some(1),
+ };
+ UploadDescriptor {
+ upload,
+ sha256: "a".repeat(64).parse().unwrap(),
+ byte_length: 10,
+ record_count,
+ }
+}
+fn part(index: u32) -> UploadId {
+ UploadId::ConversationPart {
+ slack_channel_id: "C123".parse().unwrap(),
+ part_index: index,
+ }
+}
+fn setup() -> (impl ImportService, Fake, Arc<AtomicBool>) {
+ setup_limits(ImportLimits::default())
+}
+fn setup_limits(limits: ImportLimits) -> (impl ImportService, Fake, Arc<AtomicBool>) {
+ let fake = Fake::default();
+ let enabled = Arc::new(AtomicBool::new(true));
+ let flag = enabled.clone();
+ let service = SlackImportService::new(
+ fake.clone(),
+ fake.clone(),
+ fake.clone(),
+ fake.clone(),
+ fake.clone(),
+ move |_| flag.load(Ordering::SeqCst),
+ limits,
+ )
+ .unwrap();
+ (service, fake, enabled)
+}
+
+impl State {
+ fn owned(&self, team: TeamId, job: JobId) -> PortResult<ImportProgress> {
+ if self
+ .owner
+ .as_ref()
+ .is_some_and(|(owner, _, _)| *owner == team)
+ && self.progress.as_ref().is_some_and(|p| p.job_id == job)
+ {
+ return Ok(self.progress.clone().unwrap());
+ }
+ Err(ImportError::Unavailable.into())
+ }
+ fn open(&self, team: TeamId, job: JobId) -> PortResult<ImportProgress> {
+ let p = self.owned(team, job)?;
+ if p.registration_closed_at.is_some() {
+ return Err(ImportError::Conflict.into());
+ }
+ Ok(p)
+ }
+ fn settle(
+ &mut self,
+ team: TeamId,
+ job: JobId,
+ status: JobStatus,
+ ) -> PortResult<ImportProgress> {
+ let mut p = self.owned(team, job)?;
+ p.status = status;
+ p.registration_closed_at = Some(Utc::now());
+ p.revision += 1;
+ self.progress = Some(p.clone());
+ Ok(p)
+ }
+}
+
+impl ImportRepo for Fake {
+ async fn create(
+ &self,
+ team: TeamId,
+ admin: &MacroUserIdStr<'_>,
+ command: &CreateImport,
+ limits: &ImportLimits,
+ ) -> PortResult<ImportProgress> {
+ let mut s = self.0.lock().unwrap();
+ s.calls.push("create");
+ if let SourceIdentity::Known { source_id } = &command.source {
+ if s.binding.as_ref().is_some_and(|bound| bound != source_id) {
+ return Err(ImportError::SourceMismatch.into());
+ }
+ s.binding = Some(source_id.clone());
+ }
+ if let Some((owner, requester, original)) = &s.owner
+ && *owner == team
+ && requester == admin
+ && original.idempotency_token == command.idempotency_token
+ {
+ if original != command {
+ return Err(ImportError::Conflict.into());
+ }
+ return Ok(s.progress.clone().unwrap());
+ }
+ s.owner = Some((team, admin.clone().into_owned(), command.clone()));
+ let now = Utc::now();
+ let p = ImportProgress {
+ source: command.source.clone(),
+ include_message_history: command.include_message_history,
+ job_id: Uuid::now_v7().try_into().unwrap(),
+ status: JobStatus::Uploading,
+ revision: 0,
+ created_at: now,
+ updated_at: now,
+ registration_closed_at: None,
+ users_verified: false,
+ limits: *limits,
+ conversations: vec![],
+ };
+ s.progress = Some(p.clone());
+ Ok(p)
+ }
+ async fn register(
+ &self,
+ team: TeamId,
+ job: JobId,
+ command: &RegisterUploads,
+ ) -> PortResult<Vec<RegisteredUpload>> {
+ let mut s = self.0.lock().unwrap();
+ let p = s.open(team, job)?;
+ s.calls.push("register");
+ let mut pending = s.uploads.clone();
+ let mut result = vec![];
+ for d in &command.descriptors {
+ if let Some(existing) = pending.get(&d.upload) {
+ if existing.descriptor != *d {
+ return Err(ImportError::Conflict.into());
+ }
+ result.push(existing.clone());
+ continue;
+ }
+ if matches!(d.upload, UploadId::ConversationPart { .. }) && s.sealed {
+ return Err(ImportError::Conflict.into());
+ }
+ let upload = RegisteredUpload {
+ descriptor: d.clone(),
+ key: format!("trusted/{job}/{}", pending.len()).parse().unwrap(),
+ };
+ result.push(upload.clone());
+ pending.insert(d.upload.clone(), upload);
+ }
+ if pending
+ .values()
+ .map(|u| u.descriptor.byte_length)
+ .sum::<u64>()
+ > p.limits.selected_bytes
+ {
+ return Err(ImportError::LimitExceeded.into());
+ }
+ s.uploads = pending;
+ Ok(result)
+ }
+ async fn uploads(
+ &self,
+ team: TeamId,
+ job: JobId,
+ uploads: &[UploadId],
+ ) -> PortResult<Vec<RegisteredUpload>> {
+ let mut s = self.0.lock().unwrap();
+ s.owned(team, job)?;
+ s.calls.push("resolve");
+ uploads
+ .iter()
+ .map(|id| {
+ s.uploads
+ .get(id)
+ .cloned()
+ .ok_or_else(|| ImportError::Unavailable.into())
+ })
+ .collect()
+ }
+ async fn complete(
+ &self,
+ team: TeamId,
+ job: JobId,
+ verified: &[VerifiedUpload],
+ seal: Option<&ConversationSeal>,
+ ) -> PortResult<ImportProgress> {
+ let mut s = self.0.lock().unwrap();
+ let mut p = s.open(team, job)?;
+ s.calls.push("complete_and_outbox");
+ for upload in verified {
+ assert_eq!(
+ s.uploads.get(&upload.registered.descriptor.upload),
+ Some(&upload.registered)
+ );
+ if upload.registered.descriptor.upload == UploadId::Users {
+ p.users_verified = true;
+ }
+ }
+ if let Some(seal) = seal {
+ let parts = s
+ .uploads
+ .values()
+ .filter(|u| u.descriptor.upload != UploadId::Users)
+ .map(|u| u.descriptor.clone())
+ .collect::<Vec<_>>();
+ if ConversationSeal::from_descriptors(seal.slack_channel_id.clone(), &parts, &p.limits)
+ .unwrap()
+ != *seal
+ {
+ return Err(ImportError::Conflict.into());
+ }
+ s.sealed = true;
+ }
+ p.revision += 1;
+ s.progress = Some(p.clone());
+ Ok(p)
+ }
+ async fn finalize(&self, team: TeamId, job: JobId) -> PortResult<ImportProgress> {
+ self.0
+ .lock()
+ .unwrap()
+ .settle(team, job, JobStatus::Completed)
+ }
+ async fn cancel(&self, team: TeamId, job: JobId) -> PortResult<ImportProgress> {
+ self.0
+ .lock()
+ .unwrap()
+ .settle(team, job, JobStatus::Cancelled)
+ }
+ async fn progress(&self, team: TeamId, job: JobId) -> PortResult<Option<ImportProgress>> {
+ Ok(self.0.lock().unwrap().owned(team, job).ok())
+ }
+ async fn requested_by(
+ &self,
+ team: TeamId,
+ job: JobId,
+ ) -> PortResult<Option<MacroUserIdStr<'static>>> {
+ let s = self.0.lock().unwrap();
+ s.owned(team, job)?;
+ Ok(s.owner.as_ref().map(|(_, user, _)| user.clone()))
+ }
+ async fn list(&self, team: TeamId, _before: Option<JobId>) -> PortResult<Vec<ImportProgress>> {
+ let s = self.0.lock().unwrap();
+ Ok(s.progress
+ .iter()
+ .filter(|p| s.owned(team, p.job_id).is_ok())
+ .cloned()
+ .collect())
+ }
+}
+
+impl ImportStorage for Fake {
+ async fn grant(&self, upload: &RegisteredUpload) -> PortResult<UploadGrant> {
+ self.0.lock().unwrap().calls.push("grant");
+ Ok(UploadGrant {
+ descriptor: upload.descriptor.clone(),
+ url: "https://example.com".into(),
+ required_headers: Default::default(),
+ expires_at: Utc::now(),
+ })
+ }
+ async fn verify(&self, upload: &RegisteredUpload) -> PortResult<VerifiedUpload> {
+ assert!(upload.key.as_str().starts_with("trusted/"));
+ self.0.lock().unwrap().calls.push("head");
+ Ok(VerifiedUpload {
+ registered: upload.clone(),
+ identity: ObjectIdentity::Version("version".parse().unwrap()),
+ })
+ }
+ async fn read(&self, _: &VerifiedUpload) -> PortResult<ByteStream> {
+ panic!("admin service must never read object bodies")
+ }
+}
+impl ImportLedger for Fake {
+ async fn source_binding(&self, _: TeamId) -> PortResult<SourceBinding> {
+ Ok(SourceBinding::Unbound)
+ }
+ async fn reserve(
+ &self,
+ _: TeamId,
+ _: &MacroUserIdStr<'static>,
+ _: &ConversationMetadata,
+ _: Option<Uuid>,
+ ) -> PortResult<TargetReservation> {
+ panic!("admin service must not create targets")
+ }
+ async fn complete(&self, _: &TargetReservation, _: ConversationKind) -> PortResult<()> {
+ panic!("admin service must not complete targets")
+ }
+}
+impl ImportAuthorizer for Fake {
+ async fn require_admin(&self, _: TeamId, _: &MacroUserIdStr<'_>) -> PortResult<()> {
+ let mut s = self.0.lock().unwrap();
+ s.calls.push("revalidate");
+ if s.revoked {
+ return Err(ImportError::AdminRequired.into());
+ }
+ Ok(())
+ }
+ async fn require_target(
+ &self,
+ _: TeamId,
+ _: &MacroUserIdStr<'_>,
+ _: &ConversationMetadata,
+ _: Uuid,
+ ) -> PortResult<()> {
+ panic!("admin service must not authorize worker targets")
+ }
+}
+impl ImportProgressAccess for Fake {
+ async fn participating_targets(
+ &self,
+ viewer: &MacroUserIdStr<'_>,
+ targets: &[Uuid],
+ ) -> PortResult<Vec<Uuid>> {
+ let mut state = self.0.lock().unwrap();
+ state
+ .disclosure_calls
+ .push((viewer.to_string(), targets.to_vec()));
+ if state.fail_disclosure {
+ return Err(ImportError::Retryable.into());
+ }
+ Ok(targets
+ .iter()
+ .filter(|id| state.readable_targets.contains(id))
+ .copied()
+ .collect())
+ }
+}
+
+impl ImportNotifier for Fake {
+ async fn invalidate(
+ &self,
+ requester: &MacroUserIdStr<'_>,
+ _: TeamId,
+ job: JobId,
+ revision: u64,
+ status: JobStatus,
+ ) -> PortResult<()> {
+ let mut s = self.0.lock().unwrap();
+ let p = s.progress.as_ref().expect("commit before notification");
+ assert_eq!((p.job_id, p.revision, p.status), (job, revision, status));
+ s.calls.push("notify");
+ s.notified.push(requester.to_string());
+ if s.fail_notification {
+ return Err(ImportError::Retryable.into());
+ }
+ Ok(())
+ }
+}
+
+#[tokio::test]
+async fn receipts_revalidate_disclosure_without_hiding_selected_work_or_failing_committed_writes() {
+ let (svc, fake, _) = setup();
+ let command = command();
+ let job = svc
+ .create(access(team()), command.clone())
+ .await
+ .unwrap()
+ .job_id;
+ let readable = Uuid::now_v7();
+ let hidden = Uuid::now_v7();
+ let source = ConversationProgress {
+ slack_channel_id: "CA".parse().unwrap(),
+ name: "Source name".into(),
+ kind: ConversationKind::PrivateChannel,
+ archived: true,
+ status: ConversationStatus::Completed,
+ channel_id: Some(readable),
+ part_count: Some(0),
+ verified_parts: 0,
+ counters: ImportCounters::default(),
+ search: SearchState::NotNeeded,
+ error: None,
+ warnings: vec![],
+ };
+ {
+ let mut state = fake.0.lock().unwrap();
+ state.readable_targets.insert(readable);
+ state.progress.as_mut().unwrap().conversations = vec![
+ source.clone(),
+ ConversationProgress {
+ slack_channel_id: "CC".parse().unwrap(),
+ channel_id: Some(hidden),
+ status: ConversationStatus::Failed,
+ error: Some(ImportError::InvalidInput),
+ ..source
+ },
+ ];
+ }
+ let detail = svc
+ .progress(access(team()), JobCommand { job_id: job })
+ .await
+ .unwrap();
+ assert_eq!(detail.conversations[0].channel_id, Some(readable));
+ assert_eq!(detail.conversations[1].channel_id, None);
+ assert_eq!(detail.conversations[1].name, "Source name");
+ assert_eq!(
+ detail.conversations[1].error,
+ Some(ImportError::InvalidInput)
+ );
+ assert_eq!(
+ svc.list(access(team()), None).await.unwrap().jobs,
+ [detail.clone()]
+ );
+ assert_eq!(svc.create(access(team()), command).await.unwrap(), detail);
+ // Another admin must be checked as the viewer, not the original requesting admin.
+ let other = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();
+ svc.progress(
+ receipt(team(), EntityAccessAuth::Authenticated(other)),
+ JobCommand { job_id: job },
+ )
+ .await
+ .unwrap();
+ assert_eq!(
+ fake.0.lock().unwrap().disclosure_calls.last().unwrap().0,
+ );
+ fake.0.lock().unwrap().fail_disclosure = true;
+ let complete = svc
+ .complete_uploads(
+ access(team()),
+ job,
+ CompleteUploads {
+ uploads: vec![],
+ seal: None,
+ },
+ )
+ .await
+ .unwrap();
+ let finalized = svc
+ .finalize(access(team()), JobCommand { job_id: job })
+ .await
+ .unwrap();
+ let cancelled = svc
+ .cancel(access(team()), JobCommand { job_id: job })
+ .await
+ .unwrap();
+ for receipt in [complete, finalized, cancelled] {
+ assert_eq!(receipt.conversations.len(), 2);
+ assert!(receipt.conversations.iter().all(|c| c.channel_id.is_none()));
+ }
+ assert_eq!(
+ fake.0
+ .lock()
+ .unwrap()
+ .progress
+ .as_ref()
+ .unwrap()
+ .conversations[1]
+ .channel_id,
+ Some(hidden)
+ );
+}
+
+#[tokio::test]
+async fn create_is_idempotent_and_source_mismatch_is_sanitized() {
+ let (svc, fake, _) = setup();
+ let cmd = command();
+ let first = svc.create(access(team()), cmd.clone()).await.unwrap();
+ assert_eq!(
+ svc.create(access(team()), cmd.clone()).await.unwrap(),
+ first
+ );
+ let mut mismatch = command();
+ mismatch.source = SourceIdentity::Known {
+ source_id: "T999".parse().unwrap(),
+ };
+ assert_eq!(
+ svc.create(access(team()), mismatch).await,
+ Err(ImportError::SourceMismatch)
+ );
+ assert_eq!(fake.0.lock().unwrap().progress.as_ref(), Some(&first));
+ let mut changed = cmd;
+ changed.include_message_history = false;
+ assert_eq!(
+ svc.create(access(team()), changed).await,
+ Err(ImportError::Conflict)
+ );
+}
+
+#[tokio::test]
+async fn wrong_team_cannot_read_mutate_or_head() {
+ let (svc, fake, _) = setup();
+ let p = svc.create(access(team()), command()).await.unwrap();
+ let other: TeamId = Uuid::from_u128(2).try_into().unwrap();
+ let job = JobCommand { job_id: p.job_id };
+ assert_eq!(
+ svc.progress(access(other), job).await,
+ Err(ImportError::Unavailable)
+ );
+ assert!(svc.list(access(other), None).await.unwrap().jobs.is_empty());
+ assert_eq!(
+ svc.register_uploads(
+ access(other),
+ p.job_id,
+ RegisterUploads {
+ descriptors: vec![descriptor(UploadId::Users)]
+ }
+ )
+ .await,
+ Err(ImportError::Unavailable)
+ );
+ assert_eq!(
+ svc.complete_uploads(
+ access(other),
+ p.job_id,
+ CompleteUploads {
+ uploads: vec![UploadId::Users],
+ seal: None
+ }
+ )
+ .await,
+ Err(ImportError::Unavailable)
+ );
+ assert_eq!(
+ svc.finalize(access(other), job).await,
+ Err(ImportError::Unavailable)
+ );
+ assert_eq!(
+ svc.cancel(access(other), job).await,
+ Err(ImportError::Unavailable)
+ );
+ assert!(!fake.0.lock().unwrap().calls.contains(&"head"));
+ assert!(!fake.0.lock().unwrap().calls.contains(&"grant"));
+}
+
+#[tokio::test]
+async fn create_rejects_bot_internal_and_wrong_entity_receipts() {
+ let (svc, fake, _) = setup();
+ for auth in [
+ EntityAccessAuth::Internal,
+ EntityAccessAuth::Unauthenticated,
+ ] {
+ assert_eq!(
+ svc.create(receipt(team(), auth), command()).await,
+ Err(ImportError::AdminRequired)
+ );
+ }
+ for scope in [
+ BotReceiptScope::Team {
+ team_id: team().into(),
+ },
+ BotReceiptScope::User {
+ acting_user: user(),
+ },
+ ] {
+ let bot = EntityAccessReceipt::try_new_bot(
+ BotIdStr::parse_from_str("bot|00000000-0000-0000-0000-000000000001")
+ .unwrap()
+ .into_owned(),
+ scope,
+ Entity {
+ entity_id: team().to_string(),
+ entity_type: EntityType::Team,
+ },
+ EntityPermission::TeamRole {
+ role: TeamRole::Admin,
+ },
+ )
+ .unwrap();
+ assert_eq!(
+ svc.create(bot, command()).await,
+ Err(ImportError::AdminRequired)
+ );
+ }
+ let wrong_entity = EntityAccessReceipt::try_new_authenticated_user(
+ user(),
+ Entity {
+ entity_id: team().to_string(),
+ entity_type: EntityType::Channel,
+ },
+ EntityPermission::TeamRole {
+ role: TeamRole::Admin,
+ },
+ )
+ .unwrap();
+ assert_eq!(
+ svc.create(wrong_entity, command()).await,
+ Err(ImportError::AdminRequired)
+ );
+ assert!(fake.0.lock().unwrap().calls.is_empty());
+}
+
+#[tokio::test]
+async fn disabled_backend_gate_blocks_uploads_but_preserves_existing_receipts() {
+ let (svc, fake, flag) = setup();
+ let p = svc.create(access(team()), command()).await.unwrap();
+ fake.0.lock().unwrap().calls.clear();
+ flag.store(false, Ordering::SeqCst);
+ let job = JobCommand { job_id: p.job_id };
+ assert_eq!(
+ svc.create(access(team()), command()).await,
+ Err(ImportError::Disabled)
+ );
+ assert_eq!(
+ svc.register_uploads(
+ access(team()),
+ p.job_id,
+ RegisterUploads {
+ descriptors: vec![]
+ }
+ )
+ .await,
+ Err(ImportError::Disabled)
+ );
+ assert_eq!(
+ svc.complete_uploads(
+ access(team()),
+ p.job_id,
+ CompleteUploads {
+ uploads: vec![],
+ seal: None
+ }
+ )
+ .await,
+ Err(ImportError::Disabled)
+ );
+ assert!(fake.0.lock().unwrap().calls.is_empty());
+ assert!(svc.progress(access(team()), job).await.is_ok());
+ assert!(svc.list(access(team()), None).await.is_ok());
+ assert!(svc.finalize(access(team()), job).await.is_ok());
+ assert!(svc.cancel(access(team()), job).await.is_ok());
+}
+
+#[tokio::test]
+async fn invalid_metadata_and_create_quotas_never_reach_repo() {
+ let (svc, fake, _) = setup_limits(ImportLimits {
+ conversations: 1,
+ ..Default::default()
+ });
+ let mut cmd = command();
+ cmd.conversations.clear();
+ assert_eq!(
+ svc.create(access(team()), cmd).await,
+ Err(ImportError::InvalidInput)
+ );
+ let mut cmd = command();
+ cmd.conversations.push(cmd.conversations[0].clone());
+ assert_eq!(
+ svc.create(access(team()), cmd).await,
+ Err(ImportError::LimitExceeded)
+ );
+ for name in ["", " \t", "bad\0name"] {
+ let mut cmd = command();
+ cmd.conversations[0].name = name.into();
+ assert_eq!(
+ svc.create(access(team()), cmd).await,
+ Err(ImportError::InvalidInput)
+ );
+ }
+ let mut cmd = command();
+ cmd.conversations[0].message_count = Some(u64::MAX);
+ assert_eq!(
+ svc.create(access(team()), cmd).await,
+ Err(ImportError::LimitExceeded)
+ );
+ assert!(fake.0.lock().unwrap().calls.is_empty());
+}
+
+#[tokio::test]
+async fn duplicate_metadata_and_oversized_metadata_rejected() {
+ let (svc, _, _) = setup_limits(ImportLimits {
+ json_bytes: 1024,
+ ..Default::default()
+ });
+ let mut cmd = command();
+ cmd.conversations.push(cmd.conversations[0].clone());
+ assert_eq!(
+ svc.create(access(team()), cmd.clone()).await,
+ Err(ImportError::InvalidInput)
+ );
+ cmd.conversations[1].slack_channel_id = "C999".parse().unwrap();
+ assert_eq!(
+ svc.create(access(team()), cmd).await,
+ Err(ImportError::InvalidInput)
+ );
+ let mut cmd = command();
+ cmd.conversations[0].name = "x".repeat(1025);
+ assert_eq!(
+ svc.create(access(team()), cmd).await,
+ Err(ImportError::LimitExceeded)
+ );
+}
+
+#[tokio::test]
+async fn upload_quotas_duplicates_and_partial_authorization_prevent_storage_io() {
+ let (svc, fake, _) = setup();
+ let p = svc.create(access(team()), command()).await.unwrap();
+ let mut oversized = descriptor(part(0));
+ oversized.byte_length = p.limits.part_bytes + 1;
+ assert_eq!(
+ svc.register_uploads(
+ access(team()),
+ p.job_id,
+ RegisterUploads {
+ descriptors: vec![oversized]
+ }
+ )
+ .await,
+ Err(ImportError::LimitExceeded)
+ );
+ assert_eq!(
+ svc.register_uploads(
+ access(team()),
+ p.job_id,
+ RegisterUploads {
+ descriptors: (0..51).map(|n| descriptor(part(n))).collect()
+ }
+ )
+ .await,
+ Err(ImportError::LimitExceeded)
+ );
+ assert_eq!(
+ svc.register_uploads(
+ access(team()),
+ p.job_id,
+ RegisterUploads {
+ descriptors: vec![descriptor(UploadId::Users); 2]
+ }
+ )
+ .await,
+ Err(ImportError::InvalidInput)
+ );
+ assert!(!fake.0.lock().unwrap().calls.contains(&"grant"));
+ svc.register_uploads(
+ access(team()),
+ p.job_id,
+ RegisterUploads {
+ descriptors: vec![descriptor(UploadId::Users)],
+ },
+ )
+ .await
+ .unwrap();
+ for (uploads, error) in [
+ (
+ vec![UploadId::Users, UploadId::Users],
+ ImportError::InvalidInput,
+ ),
+ ((0..51).map(part).collect(), ImportError::LimitExceeded),
+ (vec![UploadId::Users, part(0)], ImportError::Unavailable),
+ ] {
+ assert_eq!(
+ svc.complete_uploads(
+ access(team()),
+ p.job_id,
+ CompleteUploads {
+ uploads,
+ seal: None
+ }
+ )
+ .await,
+ Err(error)
+ );
+ }
+ assert!(!fake.0.lock().unwrap().calls.contains(&"head"));
+}
+
+#[tokio::test]
+async fn aggregate_quota_enforced_before_grants_and_retries_do_not_consume_it() {
+ let limits = ImportLimits {
+ part_bytes: 10,
+ json_bytes: 1024,
+ selected_bytes: 1024,
+ record_bytes: 10,
+ ..Default::default()
+ };
+ let (svc, fake, _) = setup_limits(limits);
+ let p = svc.create(access(team()), command()).await.unwrap();
+ let mut users = descriptor(UploadId::Users);
+ users.byte_length = 1024;
+ for _ in 0..2 {
+ svc.register_uploads(
+ access(team()),
+ p.job_id,
+ RegisterUploads {
+ descriptors: vec![users.clone()],
+ },
+ )
+ .await
+ .unwrap();
+ }
+ fake.0.lock().unwrap().calls.clear();
+ assert_eq!(
+ svc.register_uploads(
+ access(team()),
+ p.job_id,
+ RegisterUploads {
+ descriptors: vec![descriptor(part(0))]
+ }
+ )
+ .await,
+ Err(ImportError::LimitExceeded)
+ );
+ assert!(!fake.0.lock().unwrap().calls.contains(&"grant"));
+}
+
+#[tokio::test]
+async fn completion_commits_before_best_effort_notification_and_seal_is_immutable() {
+ let (svc, fake, _) = setup();
+ let p = svc.create(access(team()), command()).await.unwrap();
+ let d = descriptor(part(0));
+ svc.register_uploads(
+ access(team()),
+ p.job_id,
+ RegisterUploads {
+ descriptors: vec![descriptor(UploadId::Users), d.clone()],
+ },
+ )
+ .await
+ .unwrap();
+ {
+ let mut s = fake.0.lock().unwrap();
+ s.calls.clear();
+ s.fail_notification = true;
+ }
+ let seal = ConversationSeal::from_descriptors(
+ "C123".parse().unwrap(),
+ std::slice::from_ref(&d),
+ &p.limits,
+ )
+ .unwrap();
+ let committed = svc
+ .complete_uploads(
+ access(team()),
+ p.job_id,
+ CompleteUploads {
+ uploads: vec![UploadId::Users, part(0)],
+ seal: Some(seal),
+ },
+ )
+ .await
+ .unwrap();
+ assert!(committed.users_verified);
+ assert_eq!(
+ fake.0.lock().unwrap().calls,
+ vec![
+ "resolve",
+ "head",
+ "head",
+ "complete_and_outbox",
+ "revalidate",
+ "notify"
+ ]
+ );
+ assert_eq!(
+ svc.progress(access(team()), JobCommand { job_id: p.job_id })
+ .await
+ .unwrap(),
+ committed
+ );
+ svc.register_uploads(
+ access(team()),
+ p.job_id,
+ RegisterUploads {
+ descriptors: vec![d.clone()],
+ },
+ )
+ .await
+ .unwrap();
+ let mut changed = d;
+ changed.byte_length += 1;
+ for descriptors in [vec![changed], vec![descriptor(part(1))]] {
+ assert_eq!(
+ svc.register_uploads(access(team()), p.job_id, RegisterUploads { descriptors })
+ .await,
+ Err(ImportError::Conflict)
+ );
+ }
+}
+
+#[tokio::test]
+async fn shape_only_seal_and_other_admin_notify_original_requester_only() {
+ let (svc, fake, _) = setup();
+ let mut cmd = command();
+ cmd.include_message_history = false;
+ let p = svc.create(access(team()), cmd).await.unwrap();
+ let seal = ConversationSeal::from_descriptors("C123".parse().unwrap(), &[], &p.limits).unwrap();
+ svc.complete_uploads(
+ access(team()),
+ p.job_id,
+ CompleteUploads {
+ uploads: vec![],
+ seal: Some(seal),
+ },
+ )
+ .await
+ .unwrap();
+ let other = MacroUserIdStr::parse_from_str("macro|[email protected]")
+ .unwrap()
+ .into_owned();
+ fake.0.lock().unwrap().fail_notification = true;
+ let result = svc
+ .finalize(
+ receipt(team(), EntityAccessAuth::Authenticated(other)),
+ JobCommand { job_id: p.job_id },
+ )
+ .await
+ .unwrap();
+ assert_eq!(result.status, JobStatus::Completed);
+ assert!(
+ fake.0
+ .lock()
+ .unwrap()
+ .notified
+ .iter()
+ .all(|id| id == user().as_ref())
+ );
+ assert!(!fake.0.lock().unwrap().calls.contains(&"head"));
+ assert_eq!(
+ svc.register_uploads(
+ access(team()),
+ p.job_id,
+ RegisterUploads {
+ descriptors: vec![descriptor(UploadId::Users)]
+ }
+ )
+ .await,
+ Err(ImportError::Conflict)
+ );
+}
+
+#[tokio::test]
+async fn revoked_requester_is_not_notified_and_cancel_still_commits() {
+ let (svc, fake, _) = setup();
+ let p = svc.create(access(team()), command()).await.unwrap();
+ {
+ let mut s = fake.0.lock().unwrap();
+ s.revoked = true;
+ s.notified.clear();
+ }
+ let p = svc
+ .cancel(access(team()), JobCommand { job_id: p.job_id })
+ .await
+ .unwrap();
+ assert_eq!(p.status, JobStatus::Cancelled);
+ assert!(fake.0.lock().unwrap().notified.is_empty());
+}
diff --git a/crates/slack_integration/src/domain/slack.rs b/crates/slack_integration/src/domain/slack.rs
new file mode 100644
index 0000000000..451c634099
--- /dev/null
+++ b/crates/slack_integration/src/domain/slack.rs
@@ -0,0 +1,11 @@
+//! Pure Slack export parsing. Callers enforce archive/record byte limits before decoding.
+//!
+//! These models accept unknown export fields and message subtypes. They perform no
+//! network lookups, target authorization, ZIP extraction, or historical writes.
+
+pub mod export;
+pub mod mrkdwn;
+pub mod reactions;
+pub mod references;
+pub mod threads;
+pub mod users;
diff --git a/crates/slack_integration/src/domain/slack/export.rs b/crates/slack_integration/src/domain/slack/export.rs
new file mode 100644
index 0000000000..4150b39c9b
--- /dev/null
+++ b/crates/slack_integration/src/domain/slack/export.rs
@@ -0,0 +1,256 @@
+//! Root conversation metadata and individual message/event records.
+//!
+//! Deserialize each root file as `Vec<ExportConversation>` and each day file as
+//! `Vec<ExportRecord>`, or decode NDJSON one record at a time. Unknown fields
+//! (including attachments, files, blocks and `replies[]`) are discarded.
+
+use std::str::FromStr;
+
+use serde::{Deserialize, Deserializer, Serialize, de};
+
+use crate::domain::models::{
+ ConversationId, ConversationKind, ConversationMetadata, KeySegment, SlackTimestamp,
+ SlackUserId, ValidationError,
+};
+
+use super::{threads::MessageIdentity, users::UserProfile};
+
+#[cfg(test)]
+mod test;
+
+/// Supported root conversation lists; filename, not ID prefix, determines kind.
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub enum ConversationFile {
+ /// Public Slack channels, mapped to Macro Team channels.
+ Channels,
+ /// Private Slack channels.
+ Groups,
+ /// Two-person direct messages.
+ DirectMessages,
+ /// Group direct messages.
+ GroupDirectMessages,
+}
+
+impl FromStr for ConversationFile {
+ type Err = ValidationError;
+
+ fn from_str(name: &str) -> Result<Self, Self::Err> {
+ match name {
+ "channels.json" => Ok(Self::Channels),
+ "groups.json" => Ok(Self::Groups),
+ "dms.json" => Ok(Self::DirectMessages),
+ "mpims.json" => Ok(Self::GroupDirectMessages),
+ _ => Err(ValidationError::InvalidId),
+ }
+ }
+}
+
+impl ConversationFile {
+ /// Domain mapping, never globally Public.
+ pub fn kind(self) -> ConversationKind {
+ match self {
+ Self::Channels => ConversationKind::PublicChannel,
+ Self::Groups => ConversationKind::PrivateChannel,
+ Self::DirectMessages => ConversationKind::DirectMessage,
+ Self::GroupDirectMessages => ConversationKind::GroupDirectMessage,
+ }
+ }
+}
+
+/// One entry from channels.json, groups.json, dms.json or mpims.json.
+#[derive(Debug, Clone, Deserialize)]
+pub struct ExportConversation {
+ /// Source identity; prefix alone does not determine visibility.
+ pub id: ConversationId,
+ /// DMs need not have a name.
+ pub name: Option<String>,
+ /// Full source membership, including unknown users.
+ #[serde(default)]
+ pub members: Vec<SlackUserId>,
+ /// Optional source creator.
+ pub creator: Option<SlackUserId>,
+ /// Integer seconds or exact string timestamp; missing stays missing.
+ #[serde(default, deserialize_with = "creation_time")]
+ pub created: Option<SlackTimestamp>,
+ /// Source archival flag.
+ #[serde(default)]
+ pub is_archived: bool,
+}
+
+impl ExportConversation {
+ /// Build persisted metadata using the folder found by the archive indexer.
+ /// Folder discovery stays separate: display names are not trusted paths, and
+ /// DM/export variants need not use the same folder naming convention.
+ pub fn into_metadata(self, file: ConversationFile, folder: KeySegment) -> ConversationMetadata {
+ ConversationMetadata {
+ name: self.name.unwrap_or_else(|| self.id.to_string()),
+ slack_channel_id: self.id,
+ kind: file.kind(),
+ folder,
+ member_ids: self.members,
+ creator_id: self.creator,
+ created_at: self.created,
+ archived: self.is_archived,
+ message_count: None,
+ }
+ }
+}
+
+fn creation_time<'de, D>(deserializer: D) -> Result<Option<SlackTimestamp>, D::Error>
+where
+ D: Deserializer<'de>,
+{
+ #[derive(Deserialize)]
+ #[serde(untagged)]
+ enum Time {
+ Seconds(u64),
+ Text(String),
+ }
+
+ let Some(value) = Option::<Time>::deserialize(deserializer)? else {
+ return Ok(None);
+ };
+ let text = match value {
+ Time::Seconds(seconds) => format!("{seconds}.000000"),
+ Time::Text(text) if text.contains('.') => text,
+ Time::Text(text) => format!("{text}.000000"),
+ };
+ text.parse().map(Some).map_err(de::Error::custom)
+}
+
+/// Source reaction, before shortcode conversion and email-based actor dedupe.
+#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
+pub struct ExportReaction {
+ /// Slack shortcode (possibly custom/unsupported).
+ pub name: String,
+ /// Actor list is authoritative, not the advisory `count` field.
+ #[serde(default)]
+ pub users: Vec<SlackUserId>,
+ /// Optional exact reaction time; standard archives omit it.
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub ts: Option<SlackTimestamp>,
+}
+
+/// Message content retained for author, mrkdwn and reaction conversion.
+#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize)]
+pub struct MessageContent {
+ /// Unknown subtypes remain available to the converter.
+ pub subtype: Option<String>,
+ /// Empty/missing text is allowed; the converter decides whether to skip it.
+ #[serde(default)]
+ pub text: String,
+ /// Slack Connect, phantom and missing-directory users are valid source IDs.
+ pub user: Option<SlackUserId>,
+ /// Per-message profile, useful when the root directory is incomplete.
+ pub user_profile: Option<UserProfile>,
+ /// Legacy bot display name.
+ pub username: Option<String>,
+ /// Newer bot profile display name.
+ pub bot_profile: Option<BotProfile>,
+ /// Reaction records; attachment/file/blocks fields are deliberately ignored.
+ #[serde(default)]
+ pub reactions: Vec<ExportReaction>,
+}
+
+/// Minimal bot profile; no bot identity is mapped to a human email.
+#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
+pub struct BotProfile {
+ /// Source display name.
+ pub name: Option<String>,
+}
+
+/// Message-shaped portion of a record. Event wrappers need not have a timestamp.
+#[derive(Debug, Clone, Deserialize)]
+pub struct ExportMessage {
+ /// Record type; absent is accepted for nested edited message snapshots.
+ #[serde(rename = "type")]
+ pub record_type: Option<String>,
+ /// Original message timestamp, never the edit-event timestamp.
+ pub ts: Option<SlackTimestamp>,
+ /// Thread root identity; `replies[]` is intentionally not retained.
+ pub thread_ts: Option<SlackTimestamp>,
+ /// Fields used by subsequent conversion.
+ #[serde(flatten)]
+ pub content: MessageContent,
+}
+
+/// A day-file or NDJSON record, including Slack's `message_changed` wrapper.
+#[derive(Debug, Clone, Deserialize)]
+pub struct ExportRecord {
+ /// Outer event/message fields.
+ #[serde(flatten)]
+ pub outer: ExportMessage,
+ /// Current snapshot for an edit; previous_message is deliberately ignored.
+ pub message: Option<ExportMessage>,
+}
+
+/// Why a record intentionally does not produce an importable message.
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub enum SkippedRecord {
+ /// Not a message event.
+ NonMessage,
+ /// Deletion event; v1 never deletes an already imported Macro message.
+ Deleted,
+ /// Tombstone has no importable body; v1 does not propagate deletions.
+ Tombstone,
+}
+
+/// Normalization outcome, separate from later text/housekeeping filtering.
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub enum NormalizedRecord {
+ /// Ordinary message or the current snapshot of an edit event.
+ Message(Box<NormalizedMessage>),
+ /// Deliberately ignored source event.
+ Skipped(SkippedRecord),
+}
+
+/// Exact message identity plus content; independent of day-file boundaries.
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct NormalizedMessage {
+ /// Source conversation and exact original time.
+ pub identity: MessageIdentity,
+ /// Root timestamp, including the self-reference present on some roots.
+ pub thread_ts: Option<SlackTimestamp>,
+ /// Current text/author/reactions, ready for conversion.
+ pub content: MessageContent,
+}
+
+impl ExportRecord {
+ /// Normalize an edit using its inner message's identity, not event_ts/outer ts.
+ /// Unknown subtypes are preserved. Missing message timestamps fail explicitly.
+ /// Deletions/tombstones are skipped only: they do not mutate earlier records
+ /// or durable mappings. Across jobs the first committed source identity wins;
+ /// this parser is not an edit/delete synchronization engine.
+ pub fn normalize(
+ self,
+ conversation: ConversationId,
+ ) -> Result<NormalizedRecord, ValidationError> {
+ let mut message = self.outer;
+ if message
+ .record_type
+ .as_deref()
+ .is_some_and(|t| t != "message")
+ {
+ return Ok(NormalizedRecord::Skipped(SkippedRecord::NonMessage));
+ }
+ if message.content.subtype.as_deref() == Some("message_changed") {
+ message = self.message.ok_or(ValidationError::InvalidTimestamp)?;
+ }
+ let skip = match message.content.subtype.as_deref() {
+ Some("message_deleted") => Some(SkippedRecord::Deleted),
+ Some("tombstone") => Some(SkippedRecord::Tombstone),
+ _ => None,
+ };
+ if let Some(reason) = skip {
+ return Ok(NormalizedRecord::Skipped(reason));
+ }
+ Ok(NormalizedRecord::Message(Box::new(NormalizedMessage {
+ identity: MessageIdentity {
+ conversation,
+ ts: message.ts.ok_or(ValidationError::InvalidTimestamp)?,
+ },
+ thread_ts: message.thread_ts,
+ content: message.content,
+ })))
+ }
+}
diff --git a/crates/slack_integration/src/domain/slack/export/test.rs b/crates/slack_integration/src/domain/slack/export/test.rs
new file mode 100644
index 0000000000..b0a12ab213
--- /dev/null
+++ b/crates/slack_integration/src/domain/slack/export/test.rs
@@ -0,0 +1,182 @@
+use super::*;
+
+const STANDARD: &str = include_str!("../../../../tests/fixtures/standard-export.json");
+const CORPORATE: &str = include_str!("../../../../tests/fixtures/corporate-export.json");
+const ENTERPRISE: &str = include_str!("../../../../tests/fixtures/enterprise-export.json");
+const EVENTS: &str = include_str!("../../../../tests/fixtures/message-events.ndjson");
+
+#[test]
+fn root_files_preserve_full_metadata_and_kind() {
+ for (fixture, filename, kind, count) in [
+ (
+ STANDARD,
+ "channels.json",
+ ConversationKind::PublicChannel,
+ 2,
+ ),
+ (
+ CORPORATE,
+ "groups.json",
+ ConversationKind::PrivateChannel,
+ 1,
+ ),
+ (CORPORATE, "dms.json", ConversationKind::DirectMessage, 3),
+ (
+ CORPORATE,
+ "mpims.json",
+ ConversationKind::GroupDirectMessage,
+ 1,
+ ),
+ (
+ ENTERPRISE,
+ "channels.json",
+ ConversationKind::PublicChannel,
+ 1,
+ ),
+ ] {
+ let root: serde_json::Value = serde_json::from_str(fixture).unwrap();
+ let records: Vec<ExportConversation> =
+ serde_json::from_value(root[filename].clone()).unwrap();
+ assert_eq!(records.len(), count);
+ for record in records {
+ let folder = record.name.clone().unwrap_or_else(|| record.id.to_string());
+ let metadata = record
+ .clone()
+ .into_metadata(filename.parse().unwrap(), folder.parse().unwrap());
+ assert_eq!(metadata.kind, kind);
+ assert_eq!(metadata.member_ids, record.members);
+ assert_eq!(metadata.creator_id, record.creator);
+ assert_eq!(metadata.created_at, record.created);
+ assert_eq!(metadata.archived, record.is_archived);
+ assert_eq!(metadata.message_count, None);
+ assert_eq!(metadata.folder.as_str(), folder);
+ }
+ }
+ assert!("other.json".parse::<ConversationFile>().is_err());
+}
+
+#[test]
+fn creation_time_is_exact_and_missing_is_not_replaced_with_now() {
+ for (created, expected) in [
+ ("1700000000", Some("1700000000.000000")),
+ ("\"1700000000\"", Some("1700000000.000000")),
+ ("\"1700000000.000001\"", Some("1700000000.000001")),
+ ("null", None),
+ ] {
+ let json = format!(r#"{{"id":"D100","created":{created}}}"#);
+ let conversation: ExportConversation = serde_json::from_str(&json).unwrap();
+ assert_eq!(conversation.created, expected.map(|s| s.parse().unwrap()));
+ }
+ let missing: ExportConversation = serde_json::from_str(r#"{"id":"D100"}"#).unwrap();
+ assert_eq!(missing.created, None);
+ let metadata = missing.into_metadata(ConversationFile::DirectMessages, "D100".parse().unwrap());
+ assert_eq!(metadata.name, "D100");
+ assert_eq!(metadata.created_at, None);
+}
+
+#[test]
+fn malformed_or_floating_creation_times_fail() {
+ for created in ["-1", "1.5", "253402300800", "\"invalid\"", "\"1.0000001\""] {
+ let json = format!(r#"{{"id":"C100","created":{created}}}"#);
+ assert!(serde_json::from_str::<ExportConversation>(&json).is_err());
+ }
+}
+
+fn events() -> Vec<NormalizedRecord> {
+ EVENTS
+ .lines()
+ .map(|line| {
+ serde_json::from_str::<ExportRecord>(line)
+ .unwrap()
+ .normalize("C100".parse().unwrap())
+ .unwrap()
+ })
+ .collect()
+}
+
+#[test]
+fn edit_wrapper_uses_current_snapshot_and_original_identity() {
+ let records = events();
+ let NormalizedRecord::Message(original) = &records[2] else {
+ panic!()
+ };
+ let NormalizedRecord::Message(edited) = &records[3] else {
+ panic!()
+ };
+ assert_eq!(edited.identity, original.identity);
+ assert_eq!(edited.thread_ts, original.thread_ts);
+ assert_eq!(edited.identity.ts.to_string(), "1700086400.000003");
+ assert_eq!(edited.content.text, "Edited next-day reply");
+ assert_eq!(edited.content.user, original.content.user);
+}
+
+#[test]
+fn unknown_subtypes_survive_and_attachment_bytes_do_not() {
+ let records = events();
+ let NormalizedRecord::Message(root) = &records[0] else {
+ panic!()
+ };
+ let NormalizedRecord::Message(next) = &records[1] else {
+ panic!()
+ };
+ assert_eq!(
+ next.content.subtype.as_deref(),
+ Some("future_slack_subtype")
+ );
+ assert_eq!(
+ next.identity.ts.unix_micros() - root.identity.ts.unix_micros(),
+ 1
+ );
+ let serialized = serde_json::to_value(&root.content).unwrap();
+ for discarded in ["files", "attachments", "blocks", "replies"] {
+ assert!(serialized.get(discarded).is_none());
+ }
+}
+
+#[test]
+fn deletions_tombstones_and_non_messages_are_explicit_skips() {
+ let records = events();
+ assert_eq!(
+ records[4],
+ NormalizedRecord::Skipped(SkippedRecord::Deleted)
+ );
+ assert_eq!(
+ records[5],
+ NormalizedRecord::Skipped(SkippedRecord::Tombstone)
+ );
+ assert_eq!(
+ records[10],
+ NormalizedRecord::Skipped(SkippedRecord::NonMessage)
+ );
+ // A deletion record does not erase a previously parsed root.
+ assert!(matches!(records[0], NormalizedRecord::Message(_)));
+}
+
+#[test]
+fn missing_message_or_timestamp_fails_instead_of_using_event_time() {
+ for json in [
+ r#"{"type":"message","text":"missing time"}"#,
+ r#"{"type":"message","subtype":"message_changed","ts":"1.000001"}"#,
+ r#"{"type":"message","subtype":"message_changed","ts":"1.000001","message":{"text":"missing inner time"}}"#,
+ ] {
+ let record: ExportRecord = serde_json::from_str(json).unwrap();
+ assert_eq!(
+ record.normalize("C100".parse().unwrap()),
+ Err(ValidationError::InvalidTimestamp)
+ );
+ }
+ for ts in ["1.000001", "\"1.0000001\"", "\"NaN\""] {
+ let json = format!(r#"{{"type":"message","ts":{ts}}}"#);
+ assert!(serde_json::from_str::<ExportRecord>(&json).is_err());
+ }
+}
+
+#[test]
+fn reactions_ignore_advisory_counts_and_keep_actor_ids() {
+ let content: MessageContent = serde_json::from_str(
+ r#"{"reactions":[{"name":"thumbsup","count":999,"users":["U100","W100"]}]}"#,
+ )
+ .unwrap();
+ assert_eq!(content.reactions[0].name, "thumbsup");
+ assert_eq!(content.reactions[0].users.len(), 2);
+}
diff --git a/crates/slack_integration/src/domain/slack/mrkdwn.rs b/crates/slack_integration/src/domain/slack/mrkdwn.rs
new file mode 100644
index 0000000000..8c44217cf5
--- /dev/null
+++ b/crates/slack_integration/src/domain/slack/mrkdwn.rs
@@ -0,0 +1,701 @@
+//! Code-aware Slack mrkdwn to Macro markdown. Source markup is never trusted as
+//! Macro entities. Links and mapped users use native nodes; channel/message
+//! references carry source evidence for later authorized resolution. Broadcasts
+//! and subteams remain historical display text, not notifications.
+
+use std::collections::BTreeMap;
+
+use macro_user_id::user_id::MacroUserIdStr;
+use mention_utils::serialize::ExternalLink;
+
+use crate::domain::models::{ConversationId, SlackUserId};
+
+use super::{
+ export::MessageContent,
+ references::{
+ ConvertedText, MAX_REFERENCES, MAX_SOURCE_BYTES, ReferenceError, ReferenceIntent,
+ SourceReference, parse_permalink,
+ },
+ users::UserDirectory,
+};
+
+#[cfg(test)]
+mod test;
+
+/// A skip is a counted source message, not a conversion error.
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub enum SkippedMessage {
+ /// Channel/group housekeeping, pin, deletion or tombstone event.
+ Housekeeping,
+ /// No text after conversion; files/attachments/blocks do not supply a body.
+ Empty,
+}
+
+/// Workers increment messages_skipped once for each `Skipped` result (including
+/// `Empty`), and pass only `Text` to the historical sink.
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub enum MessageConversion {
+ /// Nonempty Macro markdown.
+ Text(String),
+ /// Intentionally ignored source message.
+ Skipped(SkippedMessage),
+}
+
+/// Metadata-bounded conversion context; no history map or network lookup.
+pub struct MrkdwnConverter<'a> {
+ /// Archive user directory for mention attribution.
+ pub users: &'a UserDirectory,
+ /// Source channel names for tokens without a label. No Macro channel IDs.
+ pub channels: &'a BTreeMap<ConversationId, String>,
+}
+
+impl MrkdwnConverter<'_> {
+ /// Convert only retained text. Unknown subtypes and file_share are imported;
+ /// thread_broadcast is not filtered and its existing thread_ts lookup remains
+ /// authoritative. This does not inspect attachments or manufacture a root.
+ pub fn message(&self, message: &MessageContent) -> MessageConversion {
+ if message.subtype.as_deref().is_some_and(is_housekeeping) {
+ return MessageConversion::Skipped(SkippedMessage::Housekeeping);
+ }
+ let content = self
+ .convert_with_italics(
+ &message.text,
+ message.subtype.as_deref() == Some("me_message"),
+ None,
+ )
+ .expect("legacy conversion has no limits")
+ .body;
+ if content.trim().is_empty() {
+ return MessageConversion::Skipped(SkippedMessage::Empty);
+ }
+ MessageConversion::Text(content)
+ }
+
+ /// Bounded worker conversion retaining only actively emitted mention identities.
+ /// Housekeeping and empty messages remain counted skips.
+ pub fn message_with_references(
+ &self,
+ message: &MessageContent,
+ ) -> Result<Option<ConvertedText>, ReferenceError> {
+ if message.subtype.as_deref().is_some_and(is_housekeeping) {
+ return Ok(None);
+ }
+ let converted = self.convert_with_references(
+ &message.text,
+ message.subtype.as_deref() == Some("me_message"),
+ )?;
+ Ok((!converted.body.trim().is_empty()).then_some(converted))
+ }
+
+ /// Preserve single-tick spans and line-start triple fences byte-for-byte;
+ /// malformed/unsupported code delimiters fall back to escaped text. Elsewhere
+ /// decode Slack's three entities once and translate recognized tokens. Encoded
+ /// angle brackets never become active Slack or Macro mention syntax.
+ pub fn convert(&self, source: &str) -> String {
+ self.convert_with_italics(source, false, None)
+ .expect("legacy conversion has no limits")
+ .body
+ }
+
+ /// Additive bounded conversion with explicit reference/mention evidence.
+ /// `italic` supports Slack me_message without changing baseline worker APIs.
+ pub fn convert_with_references(
+ &self,
+ source: &str,
+ italic: bool,
+ ) -> Result<ConvertedText, ReferenceError> {
+ if source.len() > MAX_SOURCE_BYTES {
+ return Err(ReferenceError::LimitExceeded);
+ }
+ self.convert_with_italics(source, italic, Some(ConversionBudget::default()))
+ }
+
+ fn convert_with_italics(
+ &self,
+ source: &str,
+ italic: bool,
+ mut budget: Option<ConversionBudget>,
+ ) -> Result<ConvertedText, ReferenceError> {
+ let mut tokens = Vec::new();
+ let mut openers = Vec::new();
+ let mut user_mentions = Vec::new();
+ let mut offset = 0;
+ while offset < source.len() {
+ let rest = &source[offset..];
+ let ch = rest.chars().next().expect("nonempty suffix");
+ if ch == '`' {
+ let mut line_start = offset;
+ while line_start > 0
+ && offset - line_start < 3
+ && source.as_bytes()[line_start - 1] == b' '
+ {
+ line_start -= 1;
+ }
+ let fence_position = line_start == 0 || source.as_bytes()[line_start - 1] == b'\n';
+ if let Some((length, block)) = code_span(rest, fence_position) {
+ if block {
+ openers.clear();
+ tokens.push(Token::Boundary(rest[..length].to_owned()));
+ } else {
+ push_text(&mut tokens, &rest[..length]);
+ }
+ offset += length;
+ continue;
+ }
+ // Malformed inline code is literal text, not a safe container for
+ // raw entity markup. Consume once to keep adversarial input bounded.
+ push_text(&mut tokens, &escape_text(rest));
+ break;
+ }
+ if ch == '\\'
+ && let Some(next) = rest[1..].chars().next().filter(char::is_ascii_punctuation)
+ {
+ push_text(&mut tokens, &escape_text(&next.to_string()));
+ offset += 1 + next.len_utf8();
+ continue;
+ }
+ if ch == '\n' {
+ openers.clear();
+ tokens.push(Token::Boundary("\n".to_owned()));
+ offset += 1;
+ continue;
+ }
+ if ch == '<' {
+ if let Some(length) = source_macro_tag_span(rest) {
+ push_text(&mut tokens, &escape_text(&unescape(&rest[..length])));
+ offset += length;
+ continue;
+ }
+ // Stop at nested openers/newlines as well, so malformed input is
+ // linear-time rather than repeatedly scanning the entire suffix.
+ if let Some(end) = rest[1..].find(['<', '>', '\n']) {
+ let end = end + 1;
+ if rest.as_bytes()[end] == b'>' {
+ let token = self.angle(&rest[1..end], &mut user_mentions);
+ if let Some(budget) = &mut budget {
+ budget.record(&token, user_mentions.len())?;
+ }
+ tokens.push(token);
+ offset += end + 1;
+ continue;
+ }
+ push_text(&mut tokens, &escape_text(&rest[..end]));
+ offset += end;
+ continue;
+ }
+ push_text(&mut tokens, &escape_text(rest));
+ break;
+ }
+ let before = source[..offset].chars().next_back();
+ if (before.is_none_or(|c| {
+ !c.is_alphanumeric() && !matches!(c, '_' | '/' | '@' | ':' | '.' | '-')
+ }) || matches!(tokens.last(), Some(Token::Delimiter { open: true, .. })))
+ && let Some(length) = bare_url_length(rest, &openers)
+ {
+ let token = self.link(&unescape(&rest[..length]), None);
+ if let Some(budget) = &mut budget {
+ budget.record(&token, user_mentions.len())?;
+ }
+ tokens.push(token);
+ offset += length;
+ continue;
+ }
+ let line_start = offset == 0 || source.as_bytes()[offset - 1] == b'\n';
+ if line_start && (ch == '>' || rest.starts_with("&gt;")) {
+ let mut quote = String::new();
+ while source[offset..].starts_with('>') || source[offset..].starts_with("&gt;") {
+ quote.push('>');
+ offset += if source[offset..].starts_with('>') {
+ 1
+ } else {
+ 4
+ };
+ }
+ openers.clear();
+ tokens.push(Token::Boundary(quote));
+ continue;
+ }
+ if let Some((entity, value)) = slack_entity(rest) {
+ push_text(&mut tokens, &escape_text(value));
+ offset += entity.len();
+ continue;
+ }
+ if matches!(ch, '*' | '_' | '~') {
+ let before = source[..offset].chars().next_back();
+ let after = rest[1..].chars().next();
+ let open = after.is_some_and(|c| !c.is_whitespace() && c != ch)
+ && before.is_none_or(|c| !c.is_alphanumeric() && c != ch);
+ let close = before.is_some_and(|c| !c.is_whitespace() && c != ch)
+ && after.is_none_or(|c| !c.is_alphanumeric() && c != ch);
+ push_delimiter(&mut tokens, &mut openers, ch, open, close);
+ } else {
+ push_text(&mut tokens, &escape_text(&ch.to_string()));
+ }
+ offset += ch.len_utf8();
+ }
+ let (body, references) = render(tokens, italic);
+ Ok(ConvertedText {
+ body,
+ references,
+ user_mentions,
+ })
+ }
+
+ fn angle(&self, token: &str, user_mentions: &mut Vec<MacroUserIdStr<'static>>) -> Token {
+ let (target, label) = token
+ .split_once('|')
+ .map_or((token, None), |(a, b)| (a, Some(b)));
+ if let Some(raw_id) = target.strip_prefix('@')
+ && let Ok(id) = raw_id.parse::<SlackUserId>()
+ {
+ if let Some(user) = self.users.participant(&id) {
+ let mention = mention_utils::serialize::user_mention(&user)
+ .expect("serializing a validated user ID cannot fail");
+ user_mentions.push(user);
+ return Token::Text(mention);
+ }
+ let name = label
+ .map(unescape)
+ .unwrap_or_else(|| self.users.display_name(&id).to_owned());
+ return Token::Text(format!("@{}", escape_text(&name)));
+ }
+ if let Some(raw_id) = target.strip_prefix('#') {
+ let name = label
+ .or_else(|| {
+ raw_id
+ .parse::<ConversationId>()
+ .ok()
+ .and_then(|id| self.channels.get(&id).map(String::as_str))
+ })
+ .unwrap_or(raw_id);
+ let fallback = format!("#{}", escape_text(&unescape(name)));
+ return match raw_id.parse::<ConversationId>() {
+ Ok(channel) => Token::Reference {
+ source: SourceReference::Channel { channel },
+ fallback,
+ },
+ Err(_) => Token::Text(fallback),
+ };
+ }
+ if matches!(target, "!here" | "!channel" | "!everyone") {
+ return Token::Text(format!("@{}", &target[1..]));
+ }
+ if let Some(id) = target.strip_prefix("!subteam^") {
+ let name = unescape(label.unwrap_or(id));
+ return Token::Text(format!("@{}", escape_text(name.trim_start_matches('@'))));
+ }
+ if target.starts_with("!date^") {
+ return Token::Text(escape_text(&unescape(label.unwrap_or(target))));
+ }
+ let url = unescape(target);
+ let label = label.map(unescape);
+ if ExternalLink::new(&url, "").is_ok() {
+ return self.link(&url, label.as_deref());
+ }
+ // Unsafe/unknown links and arbitrary Macro/HTML tags are display text only.
+ if let Some(label) = label {
+ return Token::Text(escape_text(&label));
+ }
+ Token::Text(escape_text(&format!("<{}>", unescape(token))))
+ }
+
+ fn link(&self, url: &str, label: Option<&str>) -> Token {
+ let text = label.unwrap_or_else(|| {
+ if url
+ .get(..7)
+ .is_some_and(|prefix| prefix.eq_ignore_ascii_case("mailto:"))
+ {
+ &url[7..]
+ } else {
+ url
+ }
+ });
+ let Ok(link) = ExternalLink::new(url, text) else {
+ return Token::Text(escape_text(text));
+ };
+ let fallback = link
+ .serialize()
+ .expect("serializing string fields cannot fail");
+ match parse_permalink(url) {
+ Some(source) => Token::Reference { source, fallback },
+ None => Token::Text(fallback),
+ }
+ }
+}
+
+/// Source-supplied Macro markup is one inert region, including any URLs or Slack
+/// tokens inside it. This only identifies literal tag boundaries, never decodes
+/// their payload. Unclosed regions remain inert through EOF. Nested tags are
+/// counted in one pass so hostile input cannot cause repeated suffix scans.
+fn source_macro_tag_span(rest: &str) -> Option<usize> {
+ let (first_end, closing, self_closing) = source_macro_tag(rest)?;
+ if closing || self_closing {
+ return Some(first_end);
+ }
+ let mut depth = 1usize;
+ let mut offset = first_end;
+ while let Some(next) = rest[offset..].find('<') {
+ offset += next;
+ if let Some((length, closing, self_closing)) = source_macro_tag(&rest[offset..]) {
+ if closing {
+ depth -= 1;
+ } else if !self_closing {
+ depth += 1;
+ }
+ offset += length;
+ if depth == 0 {
+ return Some(offset);
+ }
+ } else {
+ offset += 1;
+ }
+ }
+ Some(rest.len())
+}
+
+fn source_macro_tag(rest: &str) -> Option<(usize, bool, bool)> {
+ let tag = rest.strip_prefix('<')?;
+ let closing = tag.starts_with('/');
+ let name = tag.strip_prefix('/').unwrap_or(tag);
+ if !name
+ .get(..2)
+ .is_some_and(|prefix| prefix.eq_ignore_ascii_case("m-"))
+ {
+ return None;
+ }
+ let name_length = name
+ .bytes()
+ .take_while(|b| b.is_ascii_alphanumeric() || *b == b'-')
+ .count();
+ let tail = &name[name_length..];
+ let self_closing = tail.starts_with("/>");
+ if name_length <= 2 || !(tail.starts_with('>') || self_closing) {
+ return None;
+ }
+ Some((
+ 1 + usize::from(closing) + name_length + 1 + usize::from(self_closing),
+ closing,
+ self_closing,
+ ))
+}
+
+/// Return only spans the target markdown parser can also recognize as code.
+fn code_span(rest: &str, fence_position: bool) -> Option<(usize, bool)> {
+ let run = rest.bytes().take_while(|b| *b == b'`').count();
+ // Slack uses single ticks or triple fences. Macro does not recognize arbitrary
+ // CommonMark code-span runs, so passing those through could activate <m-*> tags.
+ if run != 1 && !(run == 3 && fence_position) {
+ return None;
+ }
+ let mut first_line_end = run;
+ // Scan only as far as the next closing run/newline, not the entire remaining
+ // line for every span. Exact runs cannot close on part of a longer run.
+ while let Some(index) = rest[first_line_end..].find(['`', '\n']) {
+ first_line_end += index;
+ if rest.as_bytes()[first_line_end] == b'\n' {
+ break;
+ }
+ let closing_run = rest[first_line_end..]
+ .bytes()
+ .take_while(|b| *b == b'`')
+ .count();
+ first_line_end += closing_run;
+ if closing_run == run {
+ return Some((first_line_end, run == 3));
+ }
+ if run == 1 {
+ return None;
+ }
+ }
+ if !rest[first_line_end..].starts_with('\n') {
+ first_line_end = rest.len();
+ }
+ if run < 3 || !fence_position {
+ return None;
+ }
+ let mut offset = first_line_end;
+ for line in rest[first_line_end..].split_inclusive('\n') {
+ let trimmed = line.trim_start_matches([' ', '\t']);
+ let closing_run = trimmed.bytes().take_while(|b| *b == b'`').count();
+ // Match Macro's closing fences, including arbitrary space/tab indentation.
+ // Missing an accepted close would pass subsequent source tags through raw.
+ if closing_run >= run && trimmed[closing_run..].trim().is_empty() {
+ return Some((offset + line.trim_end_matches('\n').len(), true));
+ }
+ offset += line.len();
+ }
+ // A fence at line start extends through EOF when no closing fence exists.
+ Some((rest.len(), true))
+}
+
+fn is_housekeeping(subtype: &str) -> bool {
+ subtype.starts_with("group_")
+ || matches!(
+ subtype,
+ "channel_join"
+ | "channel_leave"
+ | "channel_name"
+ | "channel_topic"
+ | "channel_purpose"
+ | "channel_archive"
+ | "channel_unarchive"
+ | "pinned_item"
+ | "unpinned_item"
+ | "tombstone"
+ | "message_deleted"
+ )
+}
+
+fn slack_entity(text: &str) -> Option<(&'static str, &'static str)> {
+ [("&amp;", "&"), ("&lt;", "<"), ("&gt;", ">")]
+ .into_iter()
+ .find(|(entity, _)| text.starts_with(entity))
+}
+
+fn unescape(text: &str) -> String {
+ // Ampersand last prevents recursive decoding of &amp;lt; into an opener.
+ text.replace("&lt;", "<")
+ .replace("&gt;", ">")
+ .replace("&amp;", "&")
+}
+
+fn escape_text(text: &str) -> String {
+ let mut out = String::with_capacity(text.len());
+ for ch in text.chars() {
+ match ch {
+ '<' => out.push_str("&lt;"),
+ '>' => out.push_str("&gt;"),
+ '&' => out.push_str("&amp;"),
+ '\\' | '`' | '*' | '_' | '~' | '[' | ']' | '(' | ')' | '!' | '#' | '+' | '-' | '.'
+ | '|' => {
+ out.push('\\');
+ out.push(ch);
+ }
+ _ => out.push(ch),
+ }
+ }
+ out
+}
+
+/// Protocol-mode autolinks, like the frontend's default linkify mode. Balanced
+/// path parentheses belong to the URL; unmatched closing punctuation does not.
+fn bare_url_length(rest: &str, openers: &[(char, usize)]) -> Option<usize> {
+ if !["https://", "http://", "mailto:"].iter().any(|prefix| {
+ rest.get(..prefix.len())
+ .is_some_and(|s| s.eq_ignore_ascii_case(prefix))
+ }) {
+ return None;
+ }
+ let authority_start = rest.find("://").map_or(0, |index| index + 3);
+ let path_start = rest[authority_start..]
+ .find(['/', '?', '#'])
+ .map(|index| authority_start + index);
+ let mut end = 0;
+ let mut parentheses = 0usize;
+ for (index, ch) in rest.char_indices() {
+ if ch.is_whitespace()
+ || ch.is_control()
+ || matches!(ch, '<' | '>' | '"' | '\'' | '`' | '\\' | '[' | ']')
+ {
+ break;
+ }
+ if path_start.is_none_or(|start| index < start)
+ && (matches!(ch, '*' | '~' | ',' | ';' | '!' | '(' | ')')
+ || rest[index..].starts_with(".."))
+ {
+ break;
+ }
+ if ch == '(' {
+ parentheses += 1;
+ }
+ if ch == ')' {
+ if parentheses == 0 {
+ break;
+ }
+ parentheses -= 1;
+ }
+ end = index + ch.len_utf8();
+ }
+ let mut candidate = &rest[..end];
+ while candidate.ends_with(['.', ',', '!', '?', ':', ';'])
+ || openers
+ .iter()
+ .any(|(marker, _)| candidate.ends_with(*marker))
+ {
+ // Slack entities belong to the URL and must be decoded before URL
+ // validation; their final semicolon is not sentence punctuation.
+ if ["&amp;", "&lt;", "&gt;"]
+ .iter()
+ .any(|entity| candidate.ends_with(entity))
+ {
+ break;
+ }
+ candidate = &candidate[..candidate.len() - 1];
+ }
+ ExternalLink::new(&unescape(candidate), "").ok()?;
+ Some(candidate.len())
+}
+
+#[derive(Debug)]
+enum Token {
+ Text(String),
+ Reference {
+ source: SourceReference,
+ fallback: String,
+ },
+ // Quotes, newlines and code blocks cannot be enclosed by emphasis.
+ Boundary(String),
+ Delimiter {
+ marker: char,
+ open: bool,
+ matched: bool,
+ },
+}
+
+/// Bound generated metadata before retaining more tokens, rather than allocating
+/// the whole body and only then rejecting it. Source text escaping expands by at
+/// most a constant factor; repeated archive names need this independent budget.
+#[derive(Default)]
+struct ConversionBudget {
+ generated_bytes: usize,
+ references: usize,
+}
+
+impl ConversionBudget {
+ fn record(&mut self, token: &Token, user_mentions: usize) -> Result<(), ReferenceError> {
+ match token {
+ Token::Text(text) => self.generated_bytes += text.len(),
+ Token::Reference { fallback, .. } => {
+ self.generated_bytes += fallback.len();
+ self.references += 1;
+ }
+ _ => unreachable!("only generated text and references consume this budget"),
+ }
+ if self.generated_bytes > MAX_SOURCE_BYTES
+ || self.references > MAX_REFERENCES
+ || user_mentions > MAX_REFERENCES
+ {
+ return Err(ReferenceError::LimitExceeded);
+ }
+ Ok(())
+ }
+}
+
+fn push_text(tokens: &mut Vec<Token>, text: &str) {
+ if let Some(Token::Text(previous)) = tokens.last_mut() {
+ previous.push_str(text);
+ } else {
+ tokens.push(Token::Text(text.to_owned()));
+ }
+}
+
+fn push_delimiter(
+ tokens: &mut Vec<Token>,
+ openers: &mut Vec<(char, usize)>,
+ marker: char,
+ open: bool,
+ close: bool,
+) {
+ let index = tokens.len();
+ tokens.push(Token::Delimiter {
+ marker,
+ open,
+ matched: false,
+ });
+ // At most one opener per style. Match while tokenizing so URL boundaries
+ // know which trailing style markers belong to surrounding Slack formatting.
+ if close && openers.last().is_some_and(|(style, _)| *style == marker) {
+ let (_, start) = openers.pop().expect("matching opener");
+ for index in [start, index] {
+ if let Token::Delimiter { matched, .. } = &mut tokens[index] {
+ *matched = true;
+ }
+ }
+ } else if open && !openers.iter().any(|(style, _)| *style == marker) {
+ openers.push((marker, index));
+ }
+}
+
+fn render(tokens: Vec<Token>, italic: bool) -> (String, Vec<ReferenceIntent>) {
+ let mut out = String::new();
+ let mut segment = String::new();
+ let mut references = Vec::new();
+ let mut segment_references = Vec::new();
+ for token in tokens {
+ match token {
+ Token::Boundary(text) => {
+ flush_segment(
+ &mut out,
+ &segment,
+ italic,
+ &mut segment_references,
+ &mut references,
+ );
+ segment.clear();
+ out.push_str(&text);
+ }
+ Token::Text(text) => segment.push_str(&text),
+ Token::Reference { source, fallback } => {
+ let start = segment.len();
+ segment.push_str(&fallback);
+ segment_references.push(ReferenceIntent {
+ source,
+ range: start..segment.len(),
+ fallback,
+ });
+ }
+ Token::Delimiter {
+ marker,
+ matched: false,
+ ..
+ } => segment.push_str(&escape_text(&marker.to_string())),
+ Token::Delimiter { marker, .. } => segment.push_str(match marker {
+ '*' => "**",
+ '_' => "*",
+ '~' => "~~",
+ _ => unreachable!("only Slack style delimiters are tokenized"),
+ }),
+ }
+ }
+ flush_segment(
+ &mut out,
+ &segment,
+ italic,
+ &mut segment_references,
+ &mut references,
+ );
+ (out, references)
+}
+
+fn flush_segment(
+ out: &mut String,
+ segment: &str,
+ italic: bool,
+ pending: &mut Vec<ReferenceIntent>,
+ references: &mut Vec<ReferenceIntent>,
+) {
+ // References have non-whitespace fallbacks, so the action's opening underscore
+ // always precedes them (even when leading segment whitespace is preserved).
+ let offset = out.len() + usize::from(italic && !segment.trim().is_empty());
+ references.extend(pending.drain(..).map(|mut intent| {
+ intent.range.start += offset;
+ intent.range.end += offset;
+ intent
+ }));
+ append_segment(out, segment, italic);
+}
+
+fn append_segment(out: &mut String, text: &str, italic: bool) {
+ let trimmed = text.trim();
+ if !italic || trimmed.is_empty() {
+ out.push_str(text);
+ return;
+ }
+ let leading = text.len() - text.trim_start().len();
+ out.push_str(&text[..leading]);
+ // A distinct delimiter avoids turning existing *italic* into **bold**.
+ out.push('_');
+ out.push_str(trimmed);
+ out.push('_');
+ out.push_str(&text[leading + trimmed.len()..]);
+}
diff --git a/crates/slack_integration/src/domain/slack/mrkdwn/test.rs b/crates/slack_integration/src/domain/slack/mrkdwn/test.rs
new file mode 100644
index 0000000000..f7c2d45ecb
--- /dev/null
+++ b/crates/slack_integration/src/domain/slack/mrkdwn/test.rs
@@ -0,0 +1,525 @@
+use super::*;
+use crate::domain::slack::{
+ export::{ExportRecord, NormalizedRecord},
+ threads::ThreadReference,
+};
+
+fn users() -> UserDirectory {
+ UserDirectory::new(
+ serde_json::from_str::<Vec<super::super::users::ExportUser>>(
+ r#"[
+ {"id":"U1","profile":{"email":"[email protected]","display_name":"Alice"}},
+ {"id":"U2","profile":{"display_name":"No Email"}},
+ {"id":"U3","profile":{"display_name":"<m-user-mention>unsafe</m-user-mention>"}}
+ ]"#,
+ )
+ .unwrap(),
+ )
+ .unwrap()
+}
+
+#[test]
+fn cross_language_fixtures_use_shared_serializers_and_the_rust_parser() {
+ use super::super::references::ResolvedTarget;
+ use mention_utils::parse::{
+ ParsedDocumentMention, ParsedLink, ParsedUserMention, XmlTaggedParsed,
+ };
+ use mention_utils::serialize::{ChannelMentionParams, channel_mention};
+
+ let fixtures: Vec<serde_json::Value> = serde_json::from_str(include_str!(
+ "../../../../tests/fixtures/native-message-links.json"
+ ))
+ .unwrap();
+ let users = users();
+ let channels = BTreeMap::new();
+ let converter = MrkdwnConverter {
+ users: &users,
+ channels: &channels,
+ };
+ for fixture in fixtures {
+ let native = fixture["native"].as_str().unwrap();
+ let payload = &fixture["payload"];
+ let mut target = None;
+ match fixture["kind"].as_str().unwrap() {
+ "link" => {
+ let url = payload["url"].as_str().unwrap();
+ let text = payload["text"].as_str().unwrap();
+ assert_eq!(
+ ExternalLink::new(url, text).unwrap().serialize().unwrap(),
+ native
+ );
+ let (rest, parsed) = ParsedLink::parse(native).unwrap();
+ assert!(rest.is_empty());
+ assert_eq!(parsed.url, url);
+ assert_eq!(parsed.text, text);
+ }
+ "user-mention" => {
+ let (rest, parsed) = ParsedUserMention::parse(native).unwrap();
+ assert!(rest.is_empty());
+ assert_eq!(parsed.email.as_deref(), payload["email"].as_str());
+ }
+ "document-mention" => {
+ let channel_id = payload["documentId"].as_str().unwrap();
+ let name = payload["documentName"].as_str().unwrap();
+ let message = payload["blockParams"]["channel_message_id"].as_str();
+ let thread = payload["blockParams"]["channel_thread_id"].as_str();
+ assert_eq!(
+ channel_mention(
+ channel_id,
+ name,
+ ChannelMentionParams {
+ channel_message_id: message,
+ channel_thread_id: thread
+ }
+ )
+ .unwrap(),
+ native
+ );
+ let (rest, parsed) = ParsedDocumentMention::parse(native).unwrap();
+ assert!(rest.is_empty());
+ assert_eq!(parsed.document_name, name);
+ target = Some(match message {
+ Some(message) => ResolvedTarget::Message {
+ channel_id: channel_id.parse().unwrap(),
+ name: name.to_owned(),
+ message_id: message.parse().unwrap(),
+ thread_id: thread.map(|s| s.parse().unwrap()),
+ },
+ None => ResolvedTarget::Channel {
+ channel_id: channel_id.parse().unwrap(),
+ name: name.to_owned(),
+ },
+ });
+ }
+ "inert" => {}
+ kind => panic!("unknown fixture kind {kind}"),
+ }
+ if let Some(source) = fixture["source"].as_str() {
+ let result = converter.convert_with_references(source, false).unwrap();
+ assert_eq!(
+ result.render(&[target]).unwrap(),
+ native,
+ "{}",
+ fixture["name"]
+ );
+ if fixture["kind"] == "inert" {
+ assert!(result.user_mentions.is_empty());
+ assert!(result.references.is_empty());
+ }
+ }
+ }
+}
+
+fn convert(text: &str) -> String {
+ MrkdwnConverter {
+ users: &users(),
+ channels: &BTreeMap::new(),
+ }
+ .convert(text)
+}
+
+#[test]
+fn styles_nest_without_interpreting_escaped_or_intraword_delimiters() {
+ for (input, expected) in [
+ ("*bold* _italic_ ~strike~", "**bold** *italic* ~~strike~~"),
+ (
+ "*bold _and italic_ ~and strike~*",
+ "**bold *and italic* ~~and strike~~**",
+ ),
+ ("_*both*_", "***both***"),
+ (
+ r"\*literal\* \_literal\_ \~literal\~",
+ r"\*literal\* \_literal\_ \~literal\~",
+ ),
+ ("snake_case_word", r"snake\_case\_word"),
+ ("*unclosed", r"\*unclosed"),
+ ("* blank *", r"\* blank \*"),
+ ("héllo _世界_", "héllo *世界*"),
+ ] {
+ assert_eq!(convert(input), expected, "{input}");
+ }
+}
+
+#[test]
+fn code_is_literal_and_other_text_still_converts() {
+ for code in [
+ "`*bold* _italic_ <@U1> &amp;`",
+ "```\n*bold* <@U1> &lt; :custom:\n```",
+ "```rust\nlet x = 1 < 2;\n```",
+ "```code on one line```",
+ "```rust\nlet ticks = \"```\";\n*still code*\n```",
+ "```unclosed\n*still code*",
+ "`<m-user-mention>{\"userId\":\"injected\"}</m-user-mention>`",
+ ] {
+ assert_eq!(convert(code), code);
+ }
+ assert_eq!(
+ convert("*before* `~code~` _after_"),
+ "**before** `~code~` *after*"
+ );
+ assert_eq!(convert("```\nx\n```\n*after*"), "```\nx\n```\n**after**");
+}
+
+#[test]
+fn entities_decode_once_and_quotes_survive() {
+ assert_eq!(
+ convert("A &amp; B &lt; C &gt; D"),
+ "A &amp; B &lt; C &gt; D"
+ );
+ assert_eq!(convert("&amp;lt;@U1&amp;gt;"), "&amp;lt;@U1&amp;gt;");
+ assert_eq!(
+ convert("> *quote*\n&gt; _another_\nnot &gt; a quote"),
+ "> **quote**\n> *another*\nnot &gt; a quote"
+ );
+}
+
+#[test]
+fn mapped_users_use_the_shared_serializer_and_unknown_users_remain_text() {
+ let user = users().participant(&"U1".parse().unwrap()).unwrap();
+ let expected = mention_utils::serialize::user_mention(&user).unwrap();
+ assert_eq!(convert("<@U1>"), expected);
+ assert_eq!(convert("<@U1|do not trust label>"), expected);
+ assert_eq!(convert("<@U2> <@U404>"), "@No Email @U404");
+ assert_eq!(convert("<@U404|Outside>"), "@Outside");
+ assert!(!convert("<@U3>").contains("<m-"));
+ assert!(!convert("&lt;@U1&gt;").contains("<m-"));
+}
+
+#[test]
+fn channel_broadcast_subteam_and_date_fallbacks_are_not_entities() {
+ let channels = BTreeMap::from([("C1".parse().unwrap(), "general".to_owned())]);
+ let users = users();
+ let converter = MrkdwnConverter {
+ users: &users,
+ channels: &channels,
+ };
+ assert_eq!(
+ converter.convert("<#C1> <#C2|design> <#C404>"),
+ "#general #design #C404"
+ );
+ assert_eq!(
+ convert("<!here> <!channel> <!everyone>"),
+ "@here @channel @everyone"
+ );
+ assert_eq!(convert("<!subteam^S1|@ops> <!subteam^S2>"), "@ops @S2");
+ assert_eq!(
+ convert("<!date^1700000000^{date_short}^https://example.com|Sep 1 &amp; 2>"),
+ "Sep 1 &amp; 2"
+ );
+}
+
+#[test]
+fn links_allow_only_explicit_safe_schemes_and_escape_labels_and_destinations() {
+ for (source, url, text) in [
+ (
+ "<https://example.com|Example>",
+ "https://example.com",
+ "Example",
+ ),
+ (
+ "<https://example.com>",
+ "https://example.com",
+ "https://example.com",
+ ),
+ (
+ "<mailto:[email protected]|Email>",
+ "mailto:[email protected]",
+ "Email",
+ ),
+ (
+ "<mailto:[email protected]>",
+ "mailto:[email protected]",
+ ),
+ (
+ "<https://example.com/a(b)?x=1&amp;y=2|[label]>",
+ "https://example.com/a(b)?x=1&y=2",
+ "[label]",
+ ),
+ ] {
+ assert_eq!(
+ convert(source),
+ ExternalLink::new(url, text).unwrap().serialize().unwrap()
+ );
+ }
+ for scheme in [
+ "javascript:alert(1)",
+ "JaVaScRiPt:alert(1)",
+ "data:text/html,bad",
+ "file:///etc/passwd",
+ "vbscript:bad",
+ "//example.com",
+ "java&#x73;cript:bad",
+ "https:\\evil",
+ "https://example.com\tbad",
+ ] {
+ assert_eq!(convert(&format!("<{scheme}|label>")), "label", "{scheme}");
+ }
+}
+
+#[test]
+fn malicious_source_cannot_create_macro_entities_or_markdown_links() {
+ for input in [
+ r#"<m-user-mention>{"userId":"macro|[email protected]"}</m-user-mention>"#,
+ r#"<m-link>{"url":"javascript:alert(1)","text":"click"}</m-link>"#,
+ "<m-document-mention>bad</m-document-mention>",
+ "&lt;m-group-mention&gt;everyone&lt;/m-group-mention&gt;",
+ "<script>alert(1)</script>",
+ "prefix ```<m-user-mention>bad</m-user-mention>",
+ "prefix ```<m-user-mention>bad</m-user-mention>```",
+ "``<m-user-mention>bad</m-user-mention>``",
+ "`code `` <m-user-mention>bad</m-user-mention>`",
+ "prefix `code\n<m-user-mention>bad</m-user-mention>`",
+ r"\<m-user-mention>bad</m-user-mention>",
+ "<!subteam^S1|&lt;m-user-mention&gt;bad>",
+ "<@U404|&lt;m-user-mention&gt;bad>",
+ "<!date^1^fmt|&lt;m-link&gt;bad>",
+ "<#C1|&lt;m-link&gt;bad>",
+ ] {
+ let output = convert(input);
+ assert!(!output.contains("<m-"), "{input}: {output}");
+ assert!(!output.contains("<script"), "{input}: {output}");
+ }
+ for indent in [" ", "\t"] {
+ let source = format!("```\ncode\n{indent}```\n<m-user-mention>bad</m-user-mention>");
+ assert!(!convert(&source).contains("<m-"));
+ }
+ assert_eq!(
+ convert("[click](javascript:bad) ![image](data:bad)"),
+ r"\[click\]\(javascript:bad\) \!\[image\]\(data:bad\)"
+ );
+}
+
+#[test]
+fn housekeeping_and_empty_results_are_countable_and_attachments_are_ignored() {
+ let users = users();
+ let channels = BTreeMap::new();
+ let converter = MrkdwnConverter {
+ users: &users,
+ channels: &channels,
+ };
+ let mut skipped = 0;
+ for subtype in [
+ "channel_join",
+ "channel_leave",
+ "channel_name",
+ "channel_topic",
+ "channel_purpose",
+ "channel_archive",
+ "channel_unarchive",
+ "group_join",
+ "group_leave",
+ "group_topic",
+ "group_unknown",
+ "pinned_item",
+ "unpinned_item",
+ "tombstone",
+ "message_deleted",
+ ] {
+ let message = MessageContent {
+ subtype: Some(subtype.to_owned()),
+ text: "housekeeping".to_owned(),
+ ..Default::default()
+ };
+ assert_eq!(
+ converter.message(&message),
+ MessageConversion::Skipped(SkippedMessage::Housekeeping)
+ );
+ skipped += 1;
+ }
+ let attachment_only: MessageContent = serde_json::from_str(r#"{"subtype":"file_share","text":" \n\t","files":[{"title":"ignore"}],"attachments":[{"text":"ignore"}],"blocks":[{"text":"ignore"}]}"#).unwrap();
+ if converter.message(&attachment_only) == MessageConversion::Skipped(SkippedMessage::Empty) {
+ skipped += 1;
+ }
+ assert_eq!(skipped, 16);
+ assert_eq!(
+ converter.message(&MessageContent {
+ text: "<!date^1700000000^{date_short}|>".to_owned(),
+ ..Default::default()
+ }),
+ MessageConversion::Skipped(SkippedMessage::Empty)
+ );
+ for subtype in ["file_share", "bot_message", "unknown"] {
+ assert_eq!(
+ converter.message(&MessageContent {
+ subtype: Some(subtype.to_owned()),
+ text: "text".to_owned(),
+ ..Default::default()
+ }),
+ MessageConversion::Text("text".to_owned())
+ );
+ }
+ assert_eq!(
+ converter.message(&MessageContent {
+ subtype: Some("me_message".to_owned()),
+ text: "waves *hello*".to_owned(),
+ ..Default::default()
+ }),
+ MessageConversion::Text("_waves **hello**_".to_owned())
+ );
+}
+
+#[test]
+fn actions_keep_code_blocks_quotes_and_existing_italics() {
+ let users = users();
+ let channels = BTreeMap::new();
+ let converter = MrkdwnConverter {
+ users: &users,
+ channels: &channels,
+ };
+ let message = MessageContent {
+ subtype: Some("me_message".to_owned()),
+ text: "> _waves_\n```\n*code* <@U1>\n```\n says `hi` ".to_owned(),
+ ..Default::default()
+ };
+ assert_eq!(
+ converter.message(&message),
+ MessageConversion::Text("> _*waves*_\n```\n*code* <@U1>\n```\n _says `hi`_ ".to_owned())
+ );
+ assert_eq!(convert(">> *nested*"), ">> **nested**");
+}
+
+#[test]
+fn thread_broadcast_remains_a_reply_after_conversion() {
+ let record: ExportRecord = serde_json::from_str(r#"{"type":"message","subtype":"thread_broadcast","ts":"1700000001.000002","thread_ts":"1700000000.000001","text":"*reply*"}"#).unwrap();
+ let NormalizedRecord::Message(message) = record.normalize("C1".parse().unwrap()).unwrap()
+ else {
+ panic!("expected message");
+ };
+ assert!(matches!(
+ message.thread_reference(),
+ ThreadReference::Reply(_)
+ ));
+ assert_eq!(
+ MrkdwnConverter {
+ users: &users(),
+ channels: &BTreeMap::new()
+ }
+ .message(&message.content),
+ MessageConversion::Text("**reply**".to_owned())
+ );
+}
+
+#[test]
+fn malformed_input_is_bounded_and_keeps_visible_text() {
+ let source = "<".repeat(100_000);
+ assert_eq!(convert(&source), "&lt;".repeat(100_000));
+ let code_spans = "`code` ".repeat(50_000);
+ assert_eq!(convert(&code_spans), code_spans);
+ assert_eq!(convert("<unclosed *literal*"), r"&lt;unclosed \*literal\*");
+ assert_eq!(convert(":workspace_custom:"), r":workspace\_custom:");
+}
+
+#[test]
+fn autolinks_keep_balanced_parentheses_and_exclude_trailing_punctuation() {
+ for (source, url, prefix, suffix) in [
+ ("https://example.com.", "https://example.com", "", r"\."),
+ (
+ "(https://example.com/a(b)).",
+ "https://example.com/a(b)",
+ r"\(",
+ r"\)\.",
+ ),
+ (
+ "https://example.com/a(b(c)),",
+ "https://example.com/a(b(c))",
+ "",
+ ",",
+ ),
+ ("*https://example.com*", "https://example.com", "**", "**"),
+ ("_https://example.com_", "https://example.com", "*", "*"),
+ (
+ "https://example.com/?x=1&amp;y=2!",
+ "https://example.com/?x=1&y=2",
+ "",
+ r"\!",
+ ),
+ (
+ "https://example.com/?x=1&amp;",
+ "https://example.com/?x=1&",
+ "",
+ "",
+ ),
+ (
+ "https://example.com/path_",
+ "https://example.com/path_",
+ "",
+ "",
+ ),
+ (
+ "https://example.com/a*bc~",
+ "https://example.com/a*bc~",
+ "",
+ "",
+ ),
+ (
+ "_see https://example.com/path_",
+ "https://example.com/path",
+ "*see ",
+ "*",
+ ),
+ (
+ "https://example.com,more",
+ "https://example.com",
+ "",
+ ",more",
+ ),
+ ] {
+ let link = ExternalLink::new(url, url).unwrap().serialize().unwrap();
+ assert_eq!(
+ convert(source),
+ format!("{prefix}{link}{suffix}"),
+ "{source}"
+ );
+ }
+ for source in ["`https://example.com`", "```\nhttps://example.com\n```"] {
+ assert_eq!(convert(source), source);
+ }
+ for source in [
+ "xhttps://example.com",
+ "https://[invalid",
+ "https://example.com:bad",
+ ] {
+ assert!(!convert(source).contains("<m-link>"), "{source}");
+ }
+}
+
+#[test]
+fn source_macro_tags_are_inert_including_nested_urls_and_slack_tokens() {
+ for source in [
+ r#"<m-link>{"url":"https://example.com","text":"label","title":""}</m-link>"#,
+ "<m-user-mention><@U1> <#C1> <m-link>https://example.com</m-link> <@U1></m-user-mention>",
+ "<M-LINK>https://example.com</M-LINK>",
+ "<m-user-mention><@U1> https://example.com",
+ ] {
+ assert_eq!(convert(source), escape_text(source));
+ }
+ assert_eq!(
+ convert("<m-link>https://example.com</m-link> *after*"),
+ format!(
+ "{} **after**",
+ escape_text("<m-link>https://example.com</m-link>")
+ )
+ );
+}
+
+#[test]
+fn only_actively_emitted_users_are_reported() {
+ let users = users();
+ let channels = BTreeMap::new();
+ let converter = MrkdwnConverter {
+ users: &users,
+ channels: &channels,
+ };
+ let source = "<@U1> `<@U1>` `<m-user-mention>{\"userId\":\"macro|[email protected]\",\"email\":\"[email protected]\"}</m-user-mention>` <@U2> <!channel> <!subteam^S1|ops>\n```\n<@U1>\n```";
+ let result = converter.convert_with_references(source, false).unwrap();
+ assert_eq!(
+ result.user_mentions,
+ vec![users.participant(&"U1".parse().unwrap()).unwrap()]
+ );
+ assert!(result.references.is_empty());
+ assert_eq!(
+ converter.convert_with_references(&"<@U1> ".repeat(MAX_REFERENCES + 1), false),
+ Err(ReferenceError::LimitExceeded)
+ );
+}
diff --git a/crates/slack_integration/src/domain/slack/reactions.rs b/crates/slack_integration/src/domain/slack/reactions.rs
new file mode 100644
index 0000000000..0e680d1e05
--- /dev/null
+++ b/crates/slack_integration/src/domain/slack/reactions.rs
@@ -0,0 +1,65 @@
+//! Pinned Unicode reactions with raw-email actor mapping, never live side effects.
+
+use std::{collections::BTreeMap, sync::LazyLock};
+
+use crate::domain::models::{HistoricalReaction, SlackTimestamp};
+
+use super::{export::ExportReaction, users::UserDirectory};
+
+#[cfg(test)]
+mod test;
+
+static SHORTCODES: LazyLock<BTreeMap<&'static str, String>> = LazyLock::new(|| {
+ serde_json::from_str(include_str!("../../../data/emoji-shortcodes.json"))
+ .expect("checked-in generated shortcode map must be valid JSON")
+});
+
+/// Resolve a Slack name, stripping only valid skin-tone suffixes. Unknown/custom
+/// names and standalone skin tones have no fallback reaction. Optional surrounding
+/// colons are accepted, but arbitrary colon-separated names are not truncated.
+pub fn shortcode(name: &str) -> Option<&'static str> {
+ let mut name = name;
+ if let Some(inner) = name.strip_prefix(':').and_then(|s| s.strip_suffix(':')) {
+ name = inner;
+ }
+ while let Some((base, tone)) = name.rsplit_once("::skin-tone-") {
+ if !matches!(tone, "2" | "3" | "4" | "5" | "6") {
+ return None;
+ }
+ name = base;
+ }
+ SHORTCODES.get(name).map(String::as_str)
+}
+
+/// Convert one message's reactions. Actor lists, not Slack's advisory counts, are
+/// authoritative. Deduplication happens after both shortcode and email mapping;
+/// the earliest supplied/fallback timestamp wins independently of input order.
+/// Standard Slack archives have no reaction time, so use the exact message time.
+pub fn convert(
+ reactions: &[ExportReaction],
+ users: &UserDirectory,
+ message_time: SlackTimestamp,
+) -> Vec<HistoricalReaction> {
+ let mut converted: BTreeMap<(String, &str), HistoricalReaction> = BTreeMap::new();
+ for reaction in reactions {
+ let Some(emoji) = shortcode(&reaction.name) else {
+ continue;
+ };
+ for user in &reaction.users {
+ let Some(user_id) = users.participant(user) else {
+ continue;
+ };
+ let created_at = reaction.ts.unwrap_or(message_time);
+ let key = (user_id.as_ref().to_owned(), emoji);
+ converted
+ .entry(key)
+ .and_modify(|existing| existing.created_at = existing.created_at.min(created_at))
+ .or_insert_with(|| HistoricalReaction {
+ user_id,
+ emoji: emoji.to_owned(),
+ created_at,
+ });
+ }
+ }
+ converted.into_values().collect()
+}
diff --git a/crates/slack_integration/src/domain/slack/reactions/test.rs b/crates/slack_integration/src/domain/slack/reactions/test.rs
new file mode 100644
index 0000000000..7557a8ee6a
--- /dev/null
+++ b/crates/slack_integration/src/domain/slack/reactions/test.rs
@@ -0,0 +1,111 @@
+use super::*;
+use crate::domain::slack::users::ExportUser;
+
+#[test]
+fn slack_aliases_match_canonical_frontend_unicode() {
+ for (names, expected) in [
+ (&["+1", "thumbsup"][..], "\u{1f44d}"),
+ (&["-1", "thumbsdown"][..], "\u{1f44e}"),
+ (&["hankey", "poop", "shit"][..], "\u{1f4a9}"),
+ (&["email", "envelope"][..], "\u{2709}\u{fe0f}"),
+ (&["us", "flag-us"][..], "\u{1f1fa}\u{1f1f8}"),
+ (&["heart"][..], "\u{2764}\u{fe0f}"),
+ (&["thinking_face"][..], "\u{1f914}"),
+ ] {
+ for name in names {
+ assert_eq!(shortcode(name), Some(expected), "{name}");
+ }
+ }
+ assert!(SHORTCODES.len() > 2000);
+ assert!(SHORTCODES.values().all(|value| {
+ !value
+ .chars()
+ .any(|c| ('\u{1f3fb}'..='\u{1f3ff}').contains(&c))
+ }));
+}
+
+#[test]
+fn valid_skin_tones_strip_but_custom_names_do_not_guess() {
+ for name in [
+ "thumbsup::skin-tone-2",
+ "+1::skin-tone-6",
+ ":thumbsup::skin-tone-3:",
+ "thumbsup::skin-tone-2::skin-tone-6",
+ ":+1:",
+ ] {
+ assert_eq!(shortcode(name), shortcode("thumbsup"));
+ }
+ for name in [
+ "custom_company",
+ "party_parrot",
+ "thumbsup::skin-tone-7",
+ "thumbsup::skin-tone-2::custom",
+ "thumbsup:custom",
+ "skin-tone-2",
+ "",
+ "::",
+ "THUMBSUP",
+ ] {
+ assert_eq!(shortcode(name), None, "{name}");
+ }
+}
+
+#[test]
+fn reactors_dedupe_after_email_alias_and_tone_mapping_with_exact_times() {
+ let users = UserDirectory::new(
+ serde_json::from_str::<Vec<ExportUser>>(
+ r#"[
+ {"id":"U1","profile":{"email":"[email protected]"}},
+ {"id":"U2","profile":{"email":"[email protected]"}},
+ {"id":"U3","profile":{"email":"[email protected]"}},
+ {"id":"U4","profile":{"display_name":"No Email"}},
+ {"id":"U5","is_bot":true,"profile":{"email":"[email protected]"}}
+ ]"#,
+ )
+ .unwrap(),
+ )
+ .unwrap();
+ let reactions: Vec<ExportReaction> = serde_json::from_str(
+ r#"[
+ {"name":"thumbsup::skin-tone-3","users":["U1","U1","U2","U3","U4","U5","U404"],"count":999},
+ {"name":"+1","users":["U2"],"ts":"1700000000.000002"},
+ {"name":"thumbsup","users":["U2"],"ts":"1700000000.000001"},
+ {"name":"heart","users":["U1"]},
+ {"name":"custom_company","users":["U1"]}
+ ]"#,
+ )
+ .unwrap();
+ let message_time = "1700000000.000000".parse().unwrap();
+ let converted = convert(&reactions, &users, message_time);
+ assert_eq!(converted.len(), 3);
+ assert!(converted.iter().all(|r| r.created_at == message_time));
+ assert_eq!(
+ converted
+ .iter()
+ .filter(|r| r.user_id.as_ref() == "macro|[email protected]")
+ .count(),
+ 2
+ );
+ assert_eq!(
+ converted
+ .iter()
+ .filter(|r| r.user_id.as_ref() == "macro|[email protected]")
+ .count(),
+ 1
+ );
+ let explicit = convert(&reactions[1..3], &users, message_time);
+ assert_eq!(explicit.len(), 1);
+ assert_eq!(explicit[0].created_at, "1700000000.000001".parse().unwrap());
+ let mut reversed = reactions.clone();
+ reversed.reverse();
+ let snapshot = |values: Vec<HistoricalReaction>| {
+ values
+ .into_iter()
+ .map(|r| (r.user_id, r.emoji, r.created_at))
+ .collect::<Vec<_>>()
+ };
+ assert_eq!(
+ snapshot(converted),
+ snapshot(convert(&reversed, &users, message_time))
+ );
+}
diff --git a/crates/slack_integration/src/domain/slack/references.rs b/crates/slack_integration/src/domain/slack/references.rs
new file mode 100644
index 0000000000..e74494bd59
--- /dev/null
+++ b/crates/slack_integration/src/domain/slack/references.rs
@@ -0,0 +1,255 @@
+//! Typed, bounded conversion evidence. Resolution is a separate authorized,
+//! source-scoped read-only use case; a Slack hostname alone is not source proof.
+
+use std::ops::Range;
+
+use macro_user_id::user_id::MacroUserIdStr;
+use mention_utils::serialize::{ChannelMentionParams, channel_mention};
+use serde::{Deserialize, Serialize};
+use uuid::Uuid;
+
+use crate::domain::models::{ConversationId, SlackTimestamp};
+
+/// Read-only, source-proven and authorized canonical resolution.
+#[cfg(feature = "ports")]
+pub mod resolve;
+
+#[cfg(test)]
+mod test;
+
+/// Maximum source text and cumulative generated token/fallback bytes accepted
+/// by the additive reference conversion API (separate budgets).
+pub const MAX_SOURCE_BYTES: usize = 1024 * 1024;
+/// Maximum reference occurrences and emitted user-mention occurrences per body.
+pub const MAX_REFERENCES: usize = 256;
+
+/// Exact source identity, never an inferred Macro target.
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+#[serde(tag = "kind", rename_all = "snake_case")]
+pub enum SourceReference {
+ /// Slack channel token in this archive's bound source.
+ Channel {
+ /// Source channel identifier.
+ channel: ConversationId,
+ },
+ /// A syntactically supported Slack permalink, not yet trusted source evidence.
+ Message {
+ /// URL hostname. Must match independently established source/domain evidence.
+ hostname: String,
+ /// Source channel identifier.
+ channel: ConversationId,
+ /// Exact message timestamp from the path (not thread_ts).
+ timestamp: SlackTimestamp,
+ /// Optional source root hint. Never a Macro root ID.
+ thread_timestamp: Option<SlackTimestamp>,
+ },
+}
+
+/// A reference occurrence in an immutable fallback body. Byte ranges are produced
+/// during rendering, not discovered by searching or parsing serialized tags.
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+pub struct ReferenceIntent {
+ /// Identity to look up with team, bound source, and requester read access.
+ pub source: SourceReference,
+ /// Byte range in `ConvertedText::body` occupied by the fallback.
+ pub range: Range<usize>,
+ /// Source-only fallback; no inaccessible Macro names/IDs may enrich it.
+ pub fallback: String,
+}
+
+/// Additive converter result. Persist this alongside the original imported body
+/// when deferring resolution; never parse the body to recover mention identities.
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+pub struct ConvertedText {
+ /// Safe initially rendered body, including code verbatim.
+ pub body: String,
+ /// Only user mentions actively emitted outside code, in occurrence order.
+ pub user_mentions: Vec<MacroUserIdStr<'static>>,
+ /// Bounded source reference occurrences, in body order.
+ pub references: Vec<ReferenceIntent>,
+}
+
+/// Canonical authorized targets supplied by a subsequent resolver. Constructing
+/// these values does not grant access; the caller must verify read access and
+/// matching source identity first. The UI still enforces per-viewer access.
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub enum ResolvedTarget {
+ /// Authorized Macro channel.
+ Channel {
+ /// Canonical channel UUID.
+ channel_id: Uuid,
+ /// Authorized entity name, not an arbitrary source label.
+ name: String,
+ },
+ /// Authorized Macro message; never downgrade a missing message to a channel.
+ Message {
+ /// Canonical channel UUID (the mention's documentId).
+ channel_id: Uuid,
+ /// Authorized channel name.
+ name: String,
+ /// Canonical message UUID.
+ message_id: Uuid,
+ /// Persisted root UUID, if known.
+ thread_id: Option<Uuid>,
+ },
+}
+
+/// Invalid or oversized conversion/template input.
+#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
+pub enum ReferenceError {
+ /// Reject rather than silently dropping reference or mention evidence.
+ #[error("source text or reference count exceeds conversion limits")]
+ LimitExceeded,
+ /// A persisted template was modified or has invalid/overlapping ranges.
+ #[error("invalid reference template")]
+ InvalidTemplate,
+}
+
+impl ConvertedText {
+ /// Render replacements by occurrence against the original template, never a
+ /// live edited body. Missing or incompatible targets retain the exact fallback.
+ /// Persistence must separately fence updates against live edits/deletion.
+ pub fn render(&self, targets: &[Option<ResolvedTarget>]) -> Result<String, ReferenceError> {
+ if self.references.len() > MAX_REFERENCES || self.user_mentions.len() > MAX_REFERENCES {
+ return Err(ReferenceError::LimitExceeded);
+ }
+ let mut out = String::new();
+ let mut cursor = 0;
+ for (index, intent) in self.references.iter().enumerate() {
+ let range = &intent.range;
+ if range.start < cursor
+ || self.body.get(range.clone()) != Some(intent.fallback.as_str())
+ {
+ return Err(ReferenceError::InvalidTemplate);
+ }
+ out.push_str(
+ self.body
+ .get(cursor..range.start)
+ .ok_or(ReferenceError::InvalidTemplate)?,
+ );
+ let replacement = targets
+ .get(index)
+ .and_then(Option::as_ref)
+ .and_then(|target| intent.source.render_target(target));
+ out.push_str(replacement.as_deref().unwrap_or(&intent.fallback));
+ cursor = range.end;
+ }
+ out.push_str(
+ self.body
+ .get(cursor..)
+ .ok_or(ReferenceError::InvalidTemplate)?,
+ );
+ Ok(out)
+ }
+}
+
+impl SourceReference {
+ fn render_target(&self, target: &ResolvedTarget) -> Option<String> {
+ let (channel_id, name, message_id, thread_id) = match (self, target) {
+ (Self::Channel { .. }, ResolvedTarget::Channel { channel_id, name }) => {
+ (channel_id, name, None, None)
+ }
+ (
+ Self::Message { .. },
+ ResolvedTarget::Message {
+ channel_id,
+ name,
+ message_id,
+ thread_id,
+ },
+ ) => (
+ channel_id,
+ name,
+ Some(message_id.to_string()),
+ thread_id.map(|id| id.to_string()),
+ ),
+ _ => return None,
+ };
+ Some(
+ channel_mention(
+ &channel_id.to_string(),
+ name,
+ ChannelMentionParams {
+ channel_message_id: message_id.as_deref(),
+ channel_thread_id: thread_id.as_deref(),
+ },
+ )
+ .expect("serializing string fields cannot fail"),
+ )
+ }
+}
+
+/// Recognize HTTPS workspace Slack `/archives/C…/p<seconds><six micros>`
+/// permalinks, including reply URLs with exact `thread_ts` and optional `cid`.
+/// Reject credentials, custom ports, fragments, extra path segments, duplicate or
+/// unknown query keys. This recognizes syntax only; it never authorizes resolution.
+pub fn parse_permalink(input: &str) -> Option<SourceReference> {
+ mention_utils::serialize::ExternalLink::new(input, "").ok()?;
+ let url = url::Url::parse(input).ok()?;
+ // WHATWG URL parsing normalizes dot segments. Do not expand our documented
+ // permalink forms by accepting a different source path after normalization.
+ let (_, authority_and_path) = input.split_once("://")?;
+ let path_start = authority_and_path.find('/')?;
+ if authority_and_path[..path_start].contains('@') {
+ return None;
+ }
+ let raw_path = authority_and_path[path_start..].split(['?', '#']).next()?;
+ if raw_path != url.path() {
+ return None;
+ }
+ let hostname = url.host_str()?;
+ let workspace = hostname.strip_suffix(".slack.com")?;
+ if url.scheme() != "https"
+ || workspace.is_empty()
+ || workspace.contains('.')
+ || !workspace
+ .bytes()
+ .all(|b| b.is_ascii_alphanumeric() || b == b'-')
+ || !url.username().is_empty()
+ || url.password().is_some()
+ || url.port().is_some()
+ || url.fragment().is_some()
+ {
+ return None;
+ }
+ let parts: Vec<_> = url.path().split('/').collect();
+ let ["", "archives", channel, message] = parts.as_slice() else {
+ return None;
+ };
+ let channel: ConversationId = channel.parse().ok()?;
+ let digits = message.strip_prefix('p')?;
+ if !(7..=18).contains(&digits.len()) || !digits.bytes().all(|b| b.is_ascii_digit()) {
+ return None;
+ }
+ let (seconds, micros) = digits.split_at(digits.len() - 6);
+ let timestamp = format!("{seconds}.{micros}").parse().ok()?;
+ let mut thread_timestamp = None;
+ let mut cid = None;
+ for (key, value) in url.query_pairs() {
+ match key.as_ref() {
+ "thread_ts" if thread_timestamp.is_none() => {
+ let (_, fraction) = value.split_once('.')?;
+ if fraction.len() != 6 {
+ return None;
+ }
+ thread_timestamp = Some(value.parse().ok()?);
+ }
+ "cid" if cid.is_none() => {
+ cid = Some(value.parse::<ConversationId>().ok()?);
+ if cid.as_ref() != Some(&channel) {
+ return None;
+ }
+ }
+ _ => return None,
+ }
+ }
+ if thread_timestamp.is_some_and(|root| root > timestamp) {
+ return None;
+ }
+ Some(SourceReference::Message {
+ hostname: hostname.to_owned(),
+ channel,
+ timestamp,
+ thread_timestamp,
+ })
+}
diff --git a/crates/slack_integration/src/domain/slack/references/resolve.rs b/crates/slack_integration/src/domain/slack/references/resolve.rs
new file mode 100644
index 0000000000..bd83493050
--- /dev/null
+++ b/crates/slack_integration/src/domain/slack/references/resolve.rs
@@ -0,0 +1,309 @@
+//! Bounded read-only resolution policy. Source proof precedes identity lookup;
+//! import provenance never substitutes for current disclosure permission.
+
+use std::collections::{HashMap, HashSet};
+
+use macro_user_id::user_id::MacroUserIdStr;
+use uuid::Uuid;
+
+use super::{ConvertedText, MAX_REFERENCES, ResolvedTarget, SourceReference};
+use crate::domain::{
+ models::*,
+ ports::{PortResult, ReferenceLookup},
+};
+
+/// Durable mapping without claims about message existence, ownership or access.
+#[derive(Debug, Clone)]
+pub struct StoredMessageMapping {
+ /// Exact source identity.
+ pub source: SourceMessageId,
+ /// Mapped channel, to compare against actual message ownership.
+ pub channel: Uuid,
+ /// Mapped message.
+ pub message: Uuid,
+}
+
+/// Persisted job scope, supplied by a trusted repository, not archive text.
+#[derive(Debug, Clone)]
+pub struct ReferenceContext {
+ /// Team namespace.
+ pub team: TeamId,
+ /// Job whose selected work may still supply missing mappings.
+ pub job: JobId,
+ /// Original administrator; current read access is checked separately.
+ pub requester: MacroUserIdStr<'static>,
+ /// Archive identity or explicit unknown-source confirmation.
+ pub source: SourceIdentity,
+ /// Current durable source binding.
+ pub binding: SourceBinding,
+ /// Independently established workspace/domain associations. Production v1 has
+ /// no domain-evidence store and MUST leave this empty. Never derive from URLs,
+ /// archive labels, unknown-source confirmation, DNS, or a network fetch.
+ pub domains: Vec<WorkspaceDomain>,
+}
+
+/// Trusted same-source evidence, not a domain claim from the archive or URL.
+#[derive(Debug, Clone)]
+pub struct WorkspaceDomain {
+ /// Workspace proven to own this exact hostname.
+ pub source: SourceId,
+ /// Exact normalized hostname; aliases require their own proof.
+ pub hostname: String,
+}
+
+/// Canonical channel facts, still requiring disclosure authorization.
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub enum ChannelMapping {
+ /// No compatible mapping. May still be selected in this job.
+ Missing,
+ /// A reservation exists, but no canonical created target may be disclosed.
+ Pending,
+ /// Existing, type/team-compatible channel from this bound namespace.
+ Ready {
+ /// Canonical channel.
+ id: Uuid,
+ /// Whether team membership grants read access (never true for private/DM).
+ team_visible: bool,
+ /// Persisted label; not yet authorized for disclosure.
+ name: String,
+ },
+}
+
+/// Exact source lookup result. Mapping and actual owning message must agree.
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub enum MessageMapping {
+ /// Absent mapping; selected work may still produce it.
+ Missing,
+ /// Mapping exists but its message is deleted or ownership is inconsistent.
+ Invalid,
+ /// Durable mapping and live message/root agree on the canonical channel.
+ Ready {
+ /// Mapping's canonical channel, checked against actual ownership.
+ channel: Uuid,
+ /// Actual message UUID, never a reserved candidate.
+ message: Uuid,
+ /// Actual persisted root (self for roots/orphan-imported replies).
+ root: Uuid,
+ },
+}
+
+/// Facts from current read-access checks, not import-write authorization.
+#[derive(Debug, Clone, Default)]
+pub struct DisclosureAccess {
+ /// Requester currently belongs to the explicit team.
+ pub team_member: bool,
+ /// Active participant channels; historical leavers are excluded.
+ pub participant_channels: HashSet<Uuid>,
+}
+
+/// No target metadata is carried by a pending or fallback result.
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub enum ReferenceOutcome {
+ /// Current read access and canonical identity are established.
+ Resolved(ResolvedTarget),
+ /// Retry after selected work settles. Never render a speculative UUID.
+ Pending,
+ /// Keep the original escaped token/external link verbatim.
+ Fallback,
+}
+
+impl ReferenceContext {
+ fn compatible(&self) -> bool {
+ match (&self.source, &self.binding) {
+ (_, SourceBinding::Unbound) => false,
+ (SourceIdentity::Known { source_id }, SourceBinding::Known { source_id: bound }) => {
+ source_id == bound
+ }
+ (SourceIdentity::Known { .. }, _) => false,
+ (SourceIdentity::ConfirmedUnknown, _) => true,
+ }
+ }
+
+ fn permits(&self, reference: &SourceReference) -> bool {
+ if !self.compatible() {
+ return false;
+ }
+ match reference {
+ SourceReference::Channel { .. } => true,
+ SourceReference::Message { hostname, .. } => {
+ let SourceBinding::Known { source_id } = &self.binding else {
+ return false;
+ };
+ self.domains
+ .iter()
+ .any(|domain| domain.source == *source_id && domain.hostname == *hostname)
+ }
+ }
+ }
+}
+
+/// Resolve one database-sized batch of converter records. Deduplicate exact source
+/// identities, then chunk database calls at the record ceiling. The entire batch,
+/// including template/intents, must fit the database byte ceiling. No archive map.
+/// Call again during deferred reconciliation; this never persists replacements.
+pub async fn resolve_batch<R: ReferenceLookup>(
+ lookup: &R,
+ context: &ReferenceContext,
+ records: &[ConvertedText],
+ limits: &ImportLimits,
+) -> PortResult<Vec<Vec<ReferenceOutcome>>> {
+ let max = limits
+ .database_batch_messages
+ .min(ImportLimits::default().database_batch_messages) as usize;
+ if max == 0 || records.len() > max {
+ return Err(ImportError::LimitExceeded.into());
+ }
+ let mut bytes = 0_u64;
+ for record in records {
+ if record.references.len() > MAX_REFERENCES || record.user_mentions.len() > MAX_REFERENCES {
+ return Err(ImportError::LimitExceeded.into());
+ }
+ bytes = bytes
+ .checked_add(
+ serde_json::to_vec(record)
+ .map_err(|_| ImportError::InvalidInput)?
+ .len() as u64,
+ )
+ .ok_or(ImportError::LimitExceeded)?;
+ if bytes
+ > limits
+ .database_batch_bytes
+ .min(ImportLimits::default().database_batch_bytes)
+ {
+ return Err(ImportError::LimitExceeded.into());
+ }
+ record.render(&[]).map_err(|_| ImportError::InvalidInput)?;
+ }
+ let mut channels = HashSet::new();
+ let mut sources = HashSet::new();
+ for intent in records.iter().flat_map(|record| &record.references) {
+ if !context.permits(&intent.source) {
+ continue;
+ }
+ match &intent.source {
+ SourceReference::Channel { channel } => {
+ channels.insert(channel.clone());
+ }
+ SourceReference::Message {
+ channel, timestamp, ..
+ } => {
+ channels.insert(channel.clone());
+ sources.insert(SourceMessageId {
+ team_id: context.team,
+ slack_channel_id: channel.clone(),
+ ts: *timestamp,
+ });
+ }
+ }
+ }
+ let channels: Vec<_> = channels.into_iter().collect();
+ let sources: Vec<_> = sources.into_iter().collect();
+ let mut targets = HashMap::new();
+ let mut pending = HashSet::new();
+ for chunk in channels.chunks(max) {
+ let rows = lookup.channels(context, chunk).await?;
+ if rows.len() != chunk.len() {
+ return Err(ImportError::Internal.into());
+ }
+ targets.extend(chunk.iter().cloned().zip(rows));
+ pending.extend(lookup.pending_channels(context, chunk).await?);
+ }
+ let mut messages = HashMap::new();
+ for chunk in sources.chunks(max) {
+ let rows = lookup.messages(context, chunk).await?;
+ if rows.len() != chunk.len() {
+ return Err(ImportError::Internal.into());
+ }
+ messages.extend(chunk.iter().cloned().zip(rows));
+ }
+ let ids: Vec<_> = targets
+ .values()
+ .filter_map(|mapping| match mapping {
+ ChannelMapping::Ready { id, .. } => Some(*id),
+ _ => None,
+ })
+ .collect::<HashSet<_>>()
+ .into_iter()
+ .collect();
+ let mut access = DisclosureAccess::default();
+ for chunk in ids.chunks(max) {
+ let facts = lookup.disclosure_access(context, chunk).await?;
+ access.team_member = facts.team_member;
+ access
+ .participant_channels
+ .extend(facts.participant_channels);
+ }
+ Ok(records
+ .iter()
+ .map(|record| {
+ record
+ .references
+ .iter()
+ .map(|intent| {
+ resolve_reference(
+ context,
+ &intent.source,
+ &targets,
+ &pending,
+ &messages,
+ &access,
+ )
+ })
+ .collect()
+ })
+ .collect())
+}
+
+fn resolve_reference(
+ context: &ReferenceContext,
+ reference: &SourceReference,
+ targets: &HashMap<ConversationId, ChannelMapping>,
+ pending: &HashSet<ConversationId>,
+ messages: &HashMap<SourceMessageId, MessageMapping>,
+ access: &DisclosureAccess,
+) -> ReferenceOutcome {
+ if !context.permits(reference) {
+ return ReferenceOutcome::Fallback;
+ }
+ let channel = match reference {
+ SourceReference::Channel { channel } | SourceReference::Message { channel, .. } => channel,
+ };
+ let (id, name, team_visible) = match targets.get(channel) {
+ Some(ChannelMapping::Ready {
+ id,
+ name,
+ team_visible,
+ }) => (*id, name, *team_visible),
+ Some(ChannelMapping::Pending) => return ReferenceOutcome::Pending,
+ _ if pending.contains(channel) => return ReferenceOutcome::Pending,
+ _ => return ReferenceOutcome::Fallback,
+ };
+ if !(access.participant_channels.contains(&id) || (team_visible && access.team_member)) {
+ return ReferenceOutcome::Fallback;
+ }
+ let SourceReference::Message { timestamp, .. } = reference else {
+ return ReferenceOutcome::Resolved(ResolvedTarget::Channel {
+ channel_id: id,
+ name: name.clone(),
+ });
+ };
+ let source = SourceMessageId {
+ team_id: context.team,
+ slack_channel_id: channel.clone(),
+ ts: *timestamp,
+ };
+ match messages.get(&source) {
+ Some(MessageMapping::Ready {
+ channel,
+ message,
+ root,
+ }) if *channel == id => ReferenceOutcome::Resolved(ResolvedTarget::Message {
+ channel_id: id,
+ name: name.clone(),
+ message_id: *message,
+ thread_id: Some(*root),
+ }),
+ Some(MessageMapping::Missing) if pending.contains(channel) => ReferenceOutcome::Pending,
+ _ => ReferenceOutcome::Fallback,
+ }
+}
diff --git a/crates/slack_integration/src/domain/slack/references/test.rs b/crates/slack_integration/src/domain/slack/references/test.rs
new file mode 100644
index 0000000000..642002711e
--- /dev/null
+++ b/crates/slack_integration/src/domain/slack/references/test.rs
@@ -0,0 +1,468 @@
+#[cfg(feature = "ports")]
+use super::resolve::*;
+use super::*;
+use crate::domain::slack::{mrkdwn::MrkdwnConverter, users::UserDirectory};
+#[cfg(feature = "ports")]
+use crate::domain::{
+ models::*,
+ ports::{PortResult, ReferenceLookup},
+};
+use std::collections::BTreeMap;
+#[cfg(feature = "ports")]
+use std::{
+ collections::{HashMap, HashSet},
+ sync::Mutex,
+};
+
+#[cfg(feature = "ports")]
+#[derive(Default)]
+struct Lookup {
+ channels: HashMap<ConversationId, ChannelMapping>,
+ messages: HashMap<SlackTimestamp, MessageMapping>,
+ pending: Vec<ConversationId>,
+ access: DisclosureAccess,
+ calls: Mutex<Vec<usize>>,
+}
+
+#[cfg(feature = "ports")]
+impl ReferenceLookup for Lookup {
+ async fn channels(
+ &self,
+ _: &ReferenceContext,
+ ids: &[ConversationId],
+ ) -> PortResult<Vec<ChannelMapping>> {
+ self.calls.lock().unwrap().push(ids.len());
+ assert_eq!(ids.iter().collect::<HashSet<_>>().len(), ids.len());
+ Ok(ids
+ .iter()
+ .map(|id| {
+ self.channels
+ .get(id)
+ .cloned()
+ .unwrap_or(ChannelMapping::Missing)
+ })
+ .collect())
+ }
+ async fn pending_channels(
+ &self,
+ _: &ReferenceContext,
+ _: &[ConversationId],
+ ) -> PortResult<Vec<ConversationId>> {
+ Ok(self.pending.clone())
+ }
+ async fn messages(
+ &self,
+ context: &ReferenceContext,
+ sources: &[SourceMessageId],
+ ) -> PortResult<Vec<MessageMapping>> {
+ self.calls.lock().unwrap().push(sources.len());
+ assert_eq!(sources.iter().collect::<HashSet<_>>().len(), sources.len());
+ assert!(sources.iter().all(|s| s.team_id == context.team));
+ Ok(sources
+ .iter()
+ .map(|s| {
+ self.messages
+ .get(&s.ts)
+ .cloned()
+ .unwrap_or(MessageMapping::Missing)
+ })
+ .collect())
+ }
+ async fn disclosure_access(
+ &self,
+ _: &ReferenceContext,
+ _: &[Uuid],
+ ) -> PortResult<DisclosureAccess> {
+ Ok(self.access.clone())
+ }
+}
+
+#[cfg(feature = "ports")]
+fn context() -> ReferenceContext {
+ ReferenceContext {
+ team: Uuid::now_v7().try_into().unwrap(),
+ job: Uuid::now_v7().try_into().unwrap(),
+ requester: MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap(),
+ source: SourceIdentity::Known {
+ source_id: "T1".parse().unwrap(),
+ },
+ binding: SourceBinding::Known {
+ source_id: "T1".parse().unwrap(),
+ },
+ domains: vec![WorkspaceDomain {
+ source: "T1".parse().unwrap(),
+ hostname: "example.slack.com".into(),
+ }],
+ }
+}
+
+#[cfg(feature = "ports")]
+#[tokio::test]
+async fn source_proof_precedes_all_identity_lookups() {
+ let lookup = Lookup::default();
+ let records = vec![conversion(
+ "https://example.slack.com/archives/C1/p1700000000000001",
+ false,
+ )];
+ let mut context = context();
+ context.domains.clear();
+ assert_eq!(
+ resolve_batch(&lookup, &context, &records, &ImportLimits::default())
+ .await
+ .unwrap(),
+ vec![vec![ReferenceOutcome::Fallback]]
+ );
+ assert!(lookup.calls.lock().unwrap().is_empty());
+ context.domains.push(WorkspaceDomain {
+ source: "T2".parse().unwrap(),
+ hostname: "example.slack.com".into(),
+ });
+ resolve_batch(&lookup, &context, &records, &ImportLimits::default())
+ .await
+ .unwrap();
+ context.domains[0].source = "T1".parse().unwrap();
+ context.domains[0].hostname = "alias.slack.com".into();
+ resolve_batch(&lookup, &context, &records, &ImportLimits::default())
+ .await
+ .unwrap();
+ context.binding = SourceBinding::ConfirmedUnknown;
+ context.source = SourceIdentity::ConfirmedUnknown;
+ resolve_batch(&lookup, &context, &records, &ImportLimits::default())
+ .await
+ .unwrap();
+ assert!(lookup.calls.lock().unwrap().is_empty());
+ context.binding = SourceBinding::Known {
+ source_id: "T2".parse().unwrap(),
+ };
+ context.source = SourceIdentity::Known {
+ source_id: "T1".parse().unwrap(),
+ };
+ resolve_batch(
+ &lookup,
+ &context,
+ &[conversion("<#C1>", false)],
+ &ImportLimits::default(),
+ )
+ .await
+ .unwrap();
+ assert!(lookup.calls.lock().unwrap().is_empty());
+ assert!(records[0].body.contains("<m-link>"));
+}
+
+#[cfg(feature = "ports")]
+#[tokio::test]
+async fn canonical_ids_exact_micros_and_disclosure_access_control_outcomes() {
+ let channel = Uuid::now_v7();
+ let other = Uuid::now_v7();
+ let root = Uuid::now_v7();
+ let reply = Uuid::now_v7();
+ let records = vec![conversion(
+ "<#C1|same> <#C2|same> <#C3> <#C4> <#C5> https://example.slack.com/archives/C1/p1000001 https://example.slack.com/archives/C1/p1000002 https://example.slack.com/archives/C1/p1000003 https://example.slack.com/archives/C1/p1000004 https://example.slack.com/archives/C1/p1000005",
+ false,
+ )];
+ let mut lookup = Lookup {
+ channels: HashMap::from([
+ (
+ "C1".parse().unwrap(),
+ ChannelMapping::Ready {
+ id: channel,
+ name: "authorized".into(),
+ team_visible: false,
+ },
+ ),
+ (
+ "C2".parse().unwrap(),
+ ChannelMapping::Ready {
+ id: other,
+ name: "authorized".into(),
+ team_visible: true,
+ },
+ ),
+ ("C3".parse().unwrap(), ChannelMapping::Pending),
+ ]),
+ messages: HashMap::from([
+ (
+ "1.000001".parse().unwrap(),
+ MessageMapping::Ready {
+ channel,
+ message: root,
+ root,
+ },
+ ),
+ (
+ "1.000002".parse().unwrap(),
+ MessageMapping::Ready {
+ channel,
+ message: reply,
+ root,
+ },
+ ),
+ (
+ "1.000003".parse().unwrap(),
+ MessageMapping::Ready {
+ channel: other,
+ message: reply,
+ root,
+ },
+ ),
+ ("1.000004".parse().unwrap(), MessageMapping::Invalid),
+ ]),
+ pending: vec!["C1".parse().unwrap(), "C4".parse().unwrap()],
+ access: DisclosureAccess {
+ team_member: true,
+ participant_channels: HashSet::from([channel]),
+ },
+ ..Default::default()
+ };
+ let result = resolve_batch(&lookup, &context(), &records, &ImportLimits::default())
+ .await
+ .unwrap()
+ .remove(0);
+ assert_eq!(
+ result[0],
+ ReferenceOutcome::Resolved(ResolvedTarget::Channel {
+ channel_id: channel,
+ name: "authorized".into()
+ })
+ );
+ assert_eq!(
+ result[1],
+ ReferenceOutcome::Resolved(ResolvedTarget::Channel {
+ channel_id: other,
+ name: "authorized".into()
+ })
+ );
+ assert_eq!(
+ &result[2..5],
+ &[
+ ReferenceOutcome::Pending,
+ ReferenceOutcome::Pending,
+ ReferenceOutcome::Fallback
+ ]
+ );
+ assert_eq!(
+ result[5],
+ ReferenceOutcome::Resolved(ResolvedTarget::Message {
+ channel_id: channel,
+ name: "authorized".into(),
+ message_id: root,
+ thread_id: Some(root)
+ })
+ );
+ assert_eq!(
+ result[6],
+ ReferenceOutcome::Resolved(ResolvedTarget::Message {
+ channel_id: channel,
+ name: "authorized".into(),
+ message_id: reply,
+ thread_id: Some(root)
+ })
+ );
+ assert_eq!(
+ &result[7..],
+ &[
+ ReferenceOutcome::Fallback,
+ ReferenceOutcome::Fallback,
+ ReferenceOutcome::Pending
+ ]
+ );
+ lookup.access.participant_channels.clear();
+ let revoked = resolve_batch(&lookup, &context(), &records, &ImportLimits::default())
+ .await
+ .unwrap()
+ .remove(0);
+ assert_eq!(revoked[0], ReferenceOutcome::Fallback);
+ assert!(
+ revoked[5..]
+ .iter()
+ .all(|r| *r == ReferenceOutcome::Fallback)
+ );
+}
+
+#[cfg(feature = "ports")]
+#[tokio::test]
+async fn reference_reads_are_deduplicated_chunked_and_byte_bounded() {
+ let lookup = Lookup::default();
+ let record = conversion(&"<#C1> ".repeat(MAX_REFERENCES), false);
+ let records = vec![record; 3];
+ resolve_batch(&lookup, &context(), &records, &ImportLimits::default())
+ .await
+ .unwrap();
+ assert_eq!(*lookup.calls.lock().unwrap(), vec![1]);
+ lookup.calls.lock().unwrap().clear();
+ let records = (0..3)
+ .map(|r| {
+ conversion(
+ &(0..MAX_REFERENCES)
+ .map(|i| format!("<#C{}> ", r * MAX_REFERENCES + i))
+ .collect::<String>(),
+ false,
+ )
+ })
+ .collect::<Vec<_>>();
+ resolve_batch(&lookup, &context(), &records, &ImportLimits::default())
+ .await
+ .unwrap();
+ assert_eq!(*lookup.calls.lock().unwrap(), vec![500, 268]);
+ let limits = ImportLimits {
+ database_batch_bytes: 1,
+ ..Default::default()
+ };
+ assert!(
+ resolve_batch(&lookup, &context(), &records, &limits)
+ .await
+ .is_err()
+ );
+}
+
+fn conversion(text: &str, italic: bool) -> ConvertedText {
+ MrkdwnConverter {
+ users: &UserDirectory::new(vec![]).unwrap(),
+ channels: &BTreeMap::new(),
+ }
+ .convert_with_references(text, italic)
+ .unwrap()
+}
+
+#[test]
+fn permalinks_keep_exact_message_and_thread_identities() {
+ for timestamp in ["1700000000.000001", "1700000000.000002"] {
+ let input = format!(
+ "https://example.slack.com/archives/C1/p{}?thread_ts=1699999999.999999&cid=C1",
+ timestamp.replace('.', "")
+ );
+ let Some(SourceReference::Message {
+ hostname,
+ channel,
+ timestamp: parsed,
+ thread_timestamp,
+ }) = parse_permalink(&input)
+ else {
+ panic!("expected permalink");
+ };
+ assert_eq!(hostname, "example.slack.com");
+ assert_eq!(channel.as_str(), "C1");
+ assert_eq!(parsed.to_string(), timestamp);
+ assert_eq!(thread_timestamp.unwrap().to_string(), "1699999999.999999");
+ }
+}
+
+#[test]
+fn unsupported_permalinks_are_not_resolution_evidence() {
+ for input in [
+ "http://example.slack.com/archives/C1/p1700000000000001",
+ "https://slack.com/archives/C1/p1700000000000001",
+ "https://a.b.slack.com/archives/C1/p1700000000000001",
+ "https://example.slack.com.evil.com/archives/C1/p1700000000000001",
+ "https://[email protected]/archives/C1/p1700000000000001",
+ "https://@example.slack.com/archives/C1/p1700000000000001",
+ "https://example.slack.com/archives/C1/p1700000000000001?thread_ts=1700000000.000002",
+ "https://example.slack.com/archives/C1/p1700000000000001?cid=C1&cid=C2",
+ "https://example.slack.com:8443/archives/C1/p1700000000000001",
+ "https://example.slack.com/archives/C1/p1700000000000001/extra",
+ "https://example.slack.com/archives/C1/p1700000000000001#fragment",
+ "https://example.slack.com/archives/C1/p1700000000000001?cid=C2",
+ "https://example.slack.com/archives/C1/p1700000000000001?thread_ts=1.1",
+ "https://example.slack.com/archives/C1/p1700000000000001?thread_ts=1.000001&thread_ts=1.000002",
+ "https://example.slack.com/archives/C1/p1700000000000001?unknown=1",
+ "https://example.slack.com/archives/C1/p170000000000000x",
+ "https://example.slack.com/archives/C1/p999999999999999999",
+ "https://app.slack.com/client/T1/C1",
+ "https://example.slack.com/other/../archives/C1/p1700000000000001",
+ "https://example.slack.com/archives/C1/./p1700000000000001",
+ "https://example.slack.com/archives/C1/p1700000000000001\n",
+ ] {
+ assert!(parse_permalink(input).is_none(), "{input}");
+ }
+}
+
+#[test]
+fn templates_render_by_occurrence_without_searching_serialized_bodies() {
+ for italic in [false, true] {
+ let result = conversion(
+ " *<#C1|源>* <https://example.slack.com/archives/C1/p1700000000000001|reply>\n> <#C1|other> ",
+ italic,
+ );
+ assert_eq!(result.references.len(), 3);
+ assert_eq!(result.render(&[]).unwrap(), result.body);
+ for intent in &result.references {
+ assert_eq!(result.body[intent.range.clone()], intent.fallback);
+ }
+ let channel_id = Uuid::from_u128(1);
+ let message_id = Uuid::from_u128(2);
+ let thread_id = Uuid::from_u128(3);
+ let channel = ResolvedTarget::Channel {
+ channel_id,
+ name: "general".into(),
+ };
+ let message = ResolvedTarget::Message {
+ channel_id,
+ name: "general".into(),
+ message_id,
+ thread_id: Some(thread_id),
+ };
+ let rendered = result
+ .render(&[Some(channel.clone()), Some(message), None])
+ .unwrap();
+ assert!(rendered.contains(&format!("\"documentId\":\"{channel_id}\"")));
+ assert!(rendered.contains(&format!("\"channel_message_id\":\"{message_id}\"")));
+ assert!(rendered.contains(&format!("\"channel_thread_id\":\"{thread_id}\"")));
+ assert!(rendered.contains("#other"));
+ // Never silently downgrade message intent into a channel mention.
+ assert_eq!(result.render(&[None, Some(channel)]).unwrap(), result.body);
+ let decoded: ConvertedText =
+ serde_json::from_str(&serde_json::to_string(&result).unwrap()).unwrap();
+ assert_eq!(decoded, result);
+ }
+}
+
+#[test]
+fn invalid_templates_and_excessive_intents_are_rejected() {
+ let mut result = conversion("<#C1>", false);
+ result.references[0].range.start = 100;
+ assert_eq!(result.render(&[]), Err(ReferenceError::InvalidTemplate));
+ let users = UserDirectory::new(vec![]).unwrap();
+ let channels = BTreeMap::new();
+ let converter = MrkdwnConverter {
+ users: &users,
+ channels: &channels,
+ };
+ assert!(
+ converter
+ .convert_with_references(&"<#C1> ".repeat(MAX_REFERENCES), false)
+ .is_ok()
+ );
+ assert_eq!(
+ converter.convert_with_references(&"<#C1> ".repeat(MAX_REFERENCES + 1), false),
+ Err(ReferenceError::LimitExceeded)
+ );
+ assert_eq!(
+ converter.convert_with_references(&"x".repeat(MAX_SOURCE_BYTES + 1), false),
+ Err(ReferenceError::LimitExceeded)
+ );
+}
+
+#[test]
+fn repeated_archive_metadata_has_an_independent_expansion_budget() {
+ let channels = BTreeMap::from([("C1".parse().unwrap(), "x".repeat(64 * 1024))]);
+ let users = UserDirectory::new(vec![]).unwrap();
+ let converter = MrkdwnConverter {
+ users: &users,
+ channels: &channels,
+ };
+ assert_eq!(
+ converter.convert_with_references(&"<#C1> ".repeat(MAX_REFERENCES), false),
+ Err(ReferenceError::LimitExceeded)
+ );
+}
+
+#[test]
+fn fallbacks_use_only_source_metadata_and_code_has_no_intents() {
+ let result = conversion(
+ "<#C1> <#C2|source name> ` <#C3> `\n```\nhttps://example.slack.com/archives/C4/p1700000000000001\n```",
+ false,
+ );
+ assert_eq!(result.references.len(), 2);
+ assert_eq!(result.references[0].fallback, "#C1");
+ assert_eq!(result.references[1].fallback, "#source name");
+}
diff --git a/crates/slack_integration/src/domain/slack/threads.rs b/crates/slack_integration/src/domain/slack/threads.rs
new file mode 100644
index 0000000000..a4595f271a
--- /dev/null
+++ b/crates/slack_integration/src/domain/slack/threads.rs
@@ -0,0 +1,88 @@
+//! Stateless thread lookup planning. No conversation-sized history map is retained.
+
+use uuid::Uuid;
+
+use crate::domain::models::{ConversationId, SlackTimestamp, SourceMessageId, TeamId};
+
+use super::export::NormalizedMessage;
+
+#[cfg(test)]
+mod test;
+
+/// Exact source identity before attaching the importing team's namespace.
+#[derive(Debug, Clone, PartialEq, Eq, Hash)]
+pub struct MessageIdentity {
+ /// Source conversation, never inferred from the timestamp or filename date.
+ pub conversation: ConversationId,
+ /// Integer seconds/microseconds; no floating-point rounding.
+ pub ts: SlackTimestamp,
+}
+
+impl MessageIdentity {
+ /// Scope a lookup/dedupe key to the team's bound Slack workspace.
+ pub fn in_team(self, team_id: TeamId) -> SourceMessageId {
+ SourceMessageId {
+ team_id,
+ slack_channel_id: self.conversation,
+ ts: self.ts,
+ }
+ }
+}
+
+/// Parent lookup requirement derived exclusively from thread_ts, never replies[].
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub enum ThreadReference {
+ /// No thread_ts, or a root's self-referencing thread_ts.
+ Root,
+ /// Look up this exact root, even when it is in a different day file/part/job.
+ Reply(MessageIdentity),
+}
+
+/// Outcome after the worker resolves a parent using its bounded batch and durable
+/// mappings. A missing parent is retained as metadata, not silently forgotten.
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub enum ThreadPlacement {
+ /// Top-level source message.
+ Root,
+ /// Existing/imported root, already checked by the worker for target ownership.
+ Reply(Uuid),
+ /// Root is missing/skipped; import at top level with orphaned_thread_ts.
+ /// A later archive does not automatically repair this relationship in v1.
+ Orphan(SlackTimestamp),
+}
+
+impl NormalizedMessage {
+ /// Return the lookup key without maintaining state across messages or files.
+ pub fn thread_reference(&self) -> ThreadReference {
+ match self.thread_ts {
+ Some(ts) if ts != self.identity.ts => ThreadReference::Reply(MessageIdentity {
+ conversation: self.identity.conversation.clone(),
+ ts,
+ }),
+ _ => ThreadReference::Root,
+ }
+ }
+}
+
+impl ThreadReference {
+ /// Key to resolve in the current bounded batch or the durable source mapping.
+ /// Callers must finish lookup before treating absence as an orphan.
+ pub fn parent_lookup(&self) -> Option<&MessageIdentity> {
+ match self {
+ Self::Root => None,
+ Self::Reply(parent) => Some(parent),
+ }
+ }
+
+ /// Apply a completed lookup. Supplying a parent does not itself authorize it:
+ /// the historical sink still validates that it belongs to the target channel.
+ pub fn resolve(&self, parent_id: Option<Uuid>) -> ThreadPlacement {
+ match self {
+ Self::Root => ThreadPlacement::Root,
+ Self::Reply(parent) => match parent_id {
+ Some(id) => ThreadPlacement::Reply(id),
+ None => ThreadPlacement::Orphan(parent.ts),
+ },
+ }
+ }
+}
diff --git a/crates/slack_integration/src/domain/slack/threads/test.rs b/crates/slack_integration/src/domain/slack/threads/test.rs
new file mode 100644
index 0000000000..646d6a9e68
--- /dev/null
+++ b/crates/slack_integration/src/domain/slack/threads/test.rs
@@ -0,0 +1,82 @@
+use super::*;
+use crate::domain::slack::export::{ExportRecord, NormalizedRecord};
+
+fn message(line: &str, conversation: &str) -> NormalizedMessage {
+ let record: ExportRecord = serde_json::from_str(line).unwrap();
+ let NormalizedRecord::Message(message) =
+ record.normalize(conversation.parse().unwrap()).unwrap()
+ else {
+ panic!("expected message");
+ };
+ *message
+}
+
+#[test]
+fn cross_day_reply_and_edit_request_the_same_exact_root() {
+ let lines: Vec<_> = include_str!("../../../../tests/fixtures/message-events.ndjson")
+ .lines()
+ .collect();
+ let root = message(lines[0], "C100");
+ assert_eq!(root.thread_reference(), ThreadReference::Root);
+ for line in [&lines[2], &lines[3]] {
+ let reply = message(line, "C100");
+ assert_eq!(
+ reply.thread_reference().parent_lookup(),
+ Some(&root.identity)
+ );
+ assert!(reply.identity.ts.unix_micros() - root.identity.ts.unix_micros() >= 86_400_000_000);
+ let parent_id = Uuid::now_v7();
+ assert_eq!(
+ reply.thread_reference().resolve(Some(parent_id)),
+ ThreadPlacement::Reply(parent_id)
+ );
+ }
+}
+
+#[test]
+fn absent_roots_keep_orphan_metadata_without_a_history_map() {
+ let line = include_str!("../../../../tests/fixtures/message-events.ndjson")
+ .lines()
+ .nth(6)
+ .unwrap();
+ let orphan = message(line, "C100").thread_reference();
+ let ts = "1699999999.999999".parse().unwrap();
+ assert_eq!(
+ orphan.parent_lookup(),
+ Some(&MessageIdentity {
+ conversation: "C100".parse().unwrap(),
+ ts
+ })
+ );
+ assert_eq!(orphan.resolve(None), ThreadPlacement::Orphan(ts));
+}
+
+#[test]
+fn replies_array_does_not_make_a_message_a_reply() {
+ let root = message(
+ r#"{"ts":"1.000001","replies":[{"ts":"2.000001","user":"U100"}]}"#,
+ "C100",
+ );
+ assert_eq!(root.thread_reference().parent_lookup(), None);
+ assert_eq!(root.thread_reference().resolve(None), ThreadPlacement::Root);
+}
+
+#[test]
+fn lookup_identity_includes_conversation_team_and_every_microsecond() {
+ let first = message(r#"{"ts":"1.000001","thread_ts":"1.000002"}"#, "C100");
+ let other = message(r#"{"ts":"1.000001","thread_ts":"1.000002"}"#, "C200");
+ assert_ne!(first.identity, other.identity);
+ let parent = first.thread_reference().parent_lookup().unwrap().clone();
+ assert_ne!(first.identity, parent);
+ assert_ne!(first.thread_reference(), other.thread_reference());
+ let team = TeamId::try_from(Uuid::now_v7()).unwrap();
+ let other_team = TeamId::try_from(Uuid::now_v7()).unwrap();
+ assert_ne!(
+ parent.clone().in_team(team),
+ parent.clone().in_team(other_team)
+ );
+ let scoped = parent.in_team(team);
+ assert_eq!(scoped.team_id, team);
+ assert_eq!(scoped.slack_channel_id.as_str(), "C100");
+ assert_eq!(scoped.ts.to_string(), "1.000002");
+}
diff --git a/crates/slack_integration/src/domain/slack/users.rs b/crates/slack_integration/src/domain/slack/users.rs
new file mode 100644
index 0000000000..b1b94ca209
--- /dev/null
+++ b/crates/slack_integration/src/domain/slack/users.rs
@@ -0,0 +1,218 @@
+//! Archive-only attribution. No roster lookup, contact sync, or email alias normalization.
+
+use std::{collections::BTreeMap, str::FromStr};
+
+use macro_user_id::user_id::MacroUserIdStr;
+use serde::{Deserialize, Serialize};
+
+use crate::domain::models::{SlackUserId, ValidationError};
+
+use super::export::MessageContent;
+
+#[cfg(test)]
+mod test;
+
+/// Both files contain arrays of `ExportUser`, normalized into one directory.
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub enum UsersFile {
+ /// Standard or corporate workspace directory.
+ Users,
+ /// Enterprise directory (including W-prefixed identities).
+ OrgUsers,
+}
+
+impl FromStr for UsersFile {
+ type Err = ValidationError;
+
+ fn from_str(name: &str) -> Result<Self, Self::Err> {
+ match name {
+ "users.json" => Ok(Self::Users),
+ "org_users.json" => Ok(Self::OrgUsers),
+ _ => Err(ValidationError::InvalidId),
+ }
+ }
+}
+
+/// Shared subset of root and per-message user profiles; unknown fields are ignored.
+#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize)]
+pub struct UserProfile {
+ /// Raw source email; only lowercased, never canonicalized.
+ pub email: Option<String>,
+ /// Preferred nonempty display name.
+ pub display_name: Option<String>,
+ /// Fallback full name.
+ pub real_name: Option<String>,
+}
+
+impl UserProfile {
+ fn display_name(&self) -> Option<&str> {
+ nonempty(self.display_name.as_deref()).or_else(|| nonempty(self.real_name.as_deref()))
+ }
+
+ fn macro_user(&self) -> Option<MacroUserIdStr<'static>> {
+ // This constructor validates and lowercases without stripping dots/plus aliases.
+ MacroUserIdStr::try_from_email(self.email.as_deref()?).ok()
+ }
+}
+
+/// User record from either supported users filename.
+#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
+pub struct ExportUser {
+ /// Slack-local identity; missing directory entries are not invalid message authors.
+ pub id: SlackUserId,
+ /// Source username.
+ pub name: Option<String>,
+ /// Legacy top-level full name.
+ pub real_name: Option<String>,
+ /// Missing/null profiles are common for phantom users.
+ pub profile: Option<UserProfile>,
+ /// Bot accounts do not become human participants.
+ #[serde(default)]
+ pub is_bot: bool,
+}
+
+impl ExportUser {
+ fn display_name(&self) -> Option<&str> {
+ self.profile
+ .as_ref()
+ .and_then(UserProfile::display_name)
+ .or_else(|| nonempty(self.real_name.as_deref()))
+ .or_else(|| nonempty(self.name.as_deref()))
+ }
+
+ fn is_system_author(&self) -> bool {
+ self.is_bot || self.id.as_str() == "USLACKBOT"
+ }
+}
+
+/// Attribution decision. The worker materializes `SystemBot` with
+/// `bot_id::MACRO_SYSTEM_BOT_ID`; the parser neither hardcodes nor configures a bot ID.
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub enum ResolvedAuthor {
+ /// Lowercase raw-email Macro identity, without imported_author.
+ User(MacroUserIdStr<'static>),
+ /// System-bot sender with the source name retained for imported_author.
+ SystemBot {
+ /// Slack display/username or source ID when no name is available.
+ imported_author: String,
+ },
+}
+
+/// Bounded by the users payload, not message history. Duplicate emails are allowed;
+/// they deliberately resolve to the same Macro identity.
+#[derive(Debug, Clone, Default)]
+pub struct UserDirectory {
+ users: BTreeMap<SlackUserId, ExportUser>,
+}
+
+impl UserDirectory {
+ /// Combine decoded users/org_users records. Identical repeated IDs are accepted;
+ /// conflicting records fail rather than choosing attribution by file order.
+ pub fn new(users: impl IntoIterator<Item = ExportUser>) -> Result<Self, ValidationError> {
+ let mut directory = Self::default();
+ for user in users {
+ if let Some(existing) = directory.users.get(&user.id) {
+ if existing != &user {
+ return Err(ValidationError::InvalidId);
+ }
+ } else {
+ directory.users.insert(user.id.clone(), user);
+ }
+ }
+ Ok(directory)
+ }
+
+ /// Display name for source membership labels, including no-email/unknown members.
+ pub fn display_name<'a>(&'a self, id: &'a SlackUserId) -> &'a str {
+ self.users
+ .get(id)
+ .and_then(ExportUser::display_name)
+ .unwrap_or_else(|| id.as_str())
+ }
+
+ /// Resolve a member/reactor without inventing an email or consulting a roster.
+ pub fn participant(&self, id: &SlackUserId) -> Option<MacroUserIdStr<'static>> {
+ let user = self.users.get(id)?;
+ if user.is_system_author() {
+ return None;
+ }
+ user.profile.as_ref()?.macro_user()
+ }
+
+ /// DMs require exactly two source members and two distinct mapped identities.
+ /// Missing email, bots and duplicate-email pairs are unsupported, not invitations
+ /// to substitute the importing administrator.
+ pub fn direct_message_members(
+ &self,
+ members: &[SlackUserId],
+ ) -> Option<[MacroUserIdStr<'static>; 2]> {
+ let [first, second] = members else {
+ return None;
+ };
+ let first = self.participant(first)?;
+ let second = self.participant(second)?;
+ (first != second).then_some([first, second])
+ }
+
+ /// Resolve a message author. Directory email takes precedence, then the embedded
+ /// profile. Explicit bot_message and USLACKBOT always use the system bot, even
+ /// when a payload contains a human user/profile email.
+ pub fn author(&self, message: &MessageContent) -> ResolvedAuthor {
+ let user = message.user.as_ref().and_then(|id| self.users.get(id));
+ let is_bot = message.subtype.as_deref() == Some("bot_message")
+ || message
+ .user
+ .as_ref()
+ .is_some_and(|id| id.as_str() == "USLACKBOT")
+ || user.is_some_and(ExportUser::is_system_author);
+ if !is_bot {
+ let mapped = message
+ .user
+ .as_ref()
+ .and_then(|id| self.participant(id))
+ .or_else(|| {
+ message
+ .user_profile
+ .as_ref()
+ .and_then(UserProfile::macro_user)
+ });
+ if let Some(mapped) = mapped {
+ return ResolvedAuthor::User(mapped);
+ }
+ }
+ let bot_name = if is_bot {
+ nonempty(message.username.as_deref()).or_else(|| {
+ message
+ .bot_profile
+ .as_ref()
+ .and_then(|profile| nonempty(profile.name.as_deref()))
+ })
+ } else {
+ None
+ };
+ let name = bot_name
+ .or_else(|| {
+ message
+ .user_profile
+ .as_ref()
+ .and_then(UserProfile::display_name)
+ })
+ .or_else(|| user.and_then(ExportUser::display_name))
+ .or_else(|| nonempty(message.username.as_deref()))
+ .or_else(|| {
+ message
+ .bot_profile
+ .as_ref()
+ .and_then(|profile| nonempty(profile.name.as_deref()))
+ })
+ .or_else(|| message.user.as_ref().map(SlackUserId::as_str))
+ .unwrap_or("Unknown Slack author");
+ ResolvedAuthor::SystemBot {
+ imported_author: name.to_owned(),
+ }
+ }
+}
+
+fn nonempty(value: Option<&str>) -> Option<&str> {
+ value.filter(|value| !value.trim().is_empty())
+}
diff --git a/crates/slack_integration/src/domain/slack/users/test.rs b/crates/slack_integration/src/domain/slack/users/test.rs
new file mode 100644
index 0000000000..0ea9b4af33
--- /dev/null
+++ b/crates/slack_integration/src/domain/slack/users/test.rs
@@ -0,0 +1,194 @@
+use super::*;
+
+fn directory() -> UserDirectory {
+ let mut users = Vec::new();
+ for (json, filename) in [
+ (
+ include_str!("../../../../tests/fixtures/standard-export.json"),
+ "users.json",
+ ),
+ (
+ include_str!("../../../../tests/fixtures/corporate-export.json"),
+ "users.json",
+ ),
+ (
+ include_str!("../../../../tests/fixtures/enterprise-export.json"),
+ "org_users.json",
+ ),
+ ] {
+ let root: serde_json::Value = serde_json::from_str(json).unwrap();
+ assert!(filename.parse::<UsersFile>().is_ok());
+ users.extend(serde_json::from_value::<Vec<ExportUser>>(root[filename].clone()).unwrap());
+ }
+ UserDirectory::new(users).unwrap()
+}
+
+fn author(directory: &UserDirectory, json: &str) -> ResolvedAuthor {
+ directory.author(&serde_json::from_str(json).unwrap())
+}
+
+fn system(name: &str) -> ResolvedAuthor {
+ ResolvedAuthor::SystemBot {
+ imported_author: name.to_owned(),
+ }
+}
+
+#[test]
+fn lowercases_raw_emails_without_canonicalizing_dots_or_plus_aliases() {
+ let directory = directory();
+ let id = directory.participant(&"U100".parse().unwrap()).unwrap();
+ assert_eq!(id.as_ref(), "macro|[email protected]");
+ assert_eq!(
+ author(&directory, r#"{"user":"U100"}"#),
+ ResolvedAuthor::User(id)
+ );
+ assert_eq!(
+ directory
+ .participant(&"W100".parse().unwrap())
+ .unwrap()
+ .as_ref(),
+ );
+}
+
+#[test]
+fn duplicate_emails_map_to_one_identity_but_not_a_self_dm() {
+ let directory = directory();
+ let first = "U100".parse().unwrap();
+ let duplicate = "U300".parse().unwrap();
+ assert_eq!(
+ directory.participant(&first),
+ directory.participant(&duplicate)
+ );
+ assert!(
+ directory
+ .direct_message_members(&[first, duplicate])
+ .is_none()
+ );
+}
+
+#[test]
+fn dm_members_must_be_exactly_two_distinct_email_bearing_users() {
+ let directory = directory();
+ for ids in [
+ vec![],
+ vec!["U100"],
+ vec!["U100", "U100"],
+ vec!["U100", "U400"],
+ vec!["U100", "U404"],
+ vec!["U100", "U500"],
+ vec!["U100", "USLACKBOT"],
+ vec!["U100", "U200", "W100"],
+ ] {
+ let ids: Vec<_> = ids.into_iter().map(|id| id.parse().unwrap()).collect();
+ assert!(directory.direct_message_members(&ids).is_none());
+ }
+ let pair = directory
+ .direct_message_members(&["U100".parse().unwrap(), "U200".parse().unwrap()])
+ .unwrap();
+ assert_eq!(pair[0].as_ref(), "macro|[email protected]");
+ assert_eq!(pair[1].as_ref(), "macro|[email protected]");
+}
+
+#[test]
+fn phantom_connect_and_missing_users_get_source_names_or_ids() {
+ let directory = directory();
+ for (json, name) in [
+ (r#"{"user":"W200"}"#, "phantom"),
+ (r#"{"user":"U999"}"#, "Connect Guest"),
+ (r#"{"user":"U404"}"#, "U404"),
+ (
+ r#"{"user":"U404","user_profile":{"display_name":"Message Guest"}}"#,
+ "Message Guest",
+ ),
+ (r#"{}"#, "Unknown Slack author"),
+ ] {
+ assert_eq!(author(&directory, json), system(name));
+ }
+ assert_eq!(directory.display_name(&"U404".parse().unwrap()), "U404");
+ assert_eq!(
+ directory.display_name(&"U400".parse().unwrap()),
+ "External Example"
+ );
+}
+
+#[test]
+fn embedded_profile_supplies_email_without_modifying_membership_directory() {
+ let directory = directory();
+ for id in ["U999", "U404"] {
+ let json = format!(
+ r#"{{"user":"{id}","user_profile":{{"email":"[email protected]"}}}}"#
+ );
+ assert_eq!(
+ author(&directory, &json),
+ ResolvedAuthor::User(
+ MacroUserIdStr::try_from_email("[email protected]").unwrap()
+ )
+ );
+ assert!(directory.participant(&id.parse().unwrap()).is_none());
+ }
+ let resolved = author(
+ &directory,
+ r#"{"user":"U200","user_profile":{"email":"[email protected]"}}"#,
+ );
+ assert_eq!(
+ resolved,
+ ResolvedAuthor::User(directory.participant(&"U200".parse().unwrap()).unwrap())
+ );
+}
+
+#[test]
+fn explicit_bot_and_slackbot_never_impersonate_email_bearing_users() {
+ let directory = directory();
+ for (json, name) in [
+ (
+ r#"{"subtype":"bot_message","user":"U100","username":"Build Bot","user_profile":{"email":"[email protected]"}}"#,
+ "Build Bot",
+ ),
+ (
+ r#"{"subtype":"bot_message","bot_profile":{"name":"Profile Bot"}}"#,
+ "Profile Bot",
+ ),
+ (
+ r#"{"user":"USLACKBOT","user_profile":{"email":"[email protected]"}}"#,
+ "slackbot",
+ ),
+ (r#"{"user":"U500"}"#, "integration"),
+ ] {
+ assert_eq!(author(&directory, json), system(name));
+ }
+ assert_eq!(
+ author(
+ &UserDirectory::default(),
+ r#"{"user":"USLACKBOT","user_profile":{"email":"[email protected]"}}"#
+ ),
+ system("USLACKBOT")
+ );
+}
+
+#[test]
+fn invalid_emails_and_blank_display_names_fall_back_safely() {
+ for email in [
+ "",
+ "not-an-email",
+ ] {
+ let message: MessageContent = serde_json::from_value(serde_json::json!({
+ "user_profile": {"email": email, "display_name": " ", "real_name": "Fallback Name"}
+ }))
+ .unwrap();
+ assert_eq!(directory().author(&message), system("Fallback Name"));
+ }
+}
+
+#[test]
+fn repeated_ids_are_idempotent_but_conflicting_ids_fail_closed() {
+ let user: ExportUser =
+ serde_json::from_str(r#"{"id":"U100","profile":{"email":"[email protected]"}}"#).unwrap();
+ assert!(UserDirectory::new([user.clone(), user.clone()]).is_ok());
+ let mut changed = user.clone();
+ changed.profile.as_mut().unwrap().email = Some("[email protected]".to_owned());
+ assert!(UserDirectory::new([user, changed]).is_err());
+ assert!("team.json".parse::<UsersFile>().is_err());
+}
diff --git a/crates/slack_integration/src/inbound.rs b/crates/slack_integration/src/inbound.rs
new file mode 100644
index 0000000000..227c65926c
--- /dev/null
+++ b/crates/slack_integration/src/inbound.rs
@@ -0,0 +1,6 @@
+//! Transport adapters for administrator import use cases.
+
+#[cfg(feature = "inbound")]
+pub mod axum_router;
+#[cfg(feature = "worker")]
+pub mod worker;
diff --git a/crates/slack_integration/src/inbound/axum_router.rs b/crates/slack_integration/src/inbound/axum_router.rs
new file mode 100644
index 0000000000..a5fa73608b
--- /dev/null
+++ b/crates/slack_integration/src/inbound/axum_router.rs
@@ -0,0 +1,134 @@
+//! Admin-only routes. Handlers pass typed team receipts to the domain service;
+//! neither team IDs nor storage keys are accepted from the request body.
+
+use std::sync::Arc;
+
+use axum::{
+ Json, Router,
+ extract::{DefaultBodyLimit, FromRef},
+ http::StatusCode,
+ response::{IntoResponse, Response},
+ routing::{get, post},
+};
+use entity_access::domain::ports::EntityAccessService;
+use macro_authorization::{MacroAuthorizationService, MacroAuthorizationState};
+
+use crate::domain::{models::ImportError, ports::ImportService};
+
+pub mod create;
+pub mod jobs;
+#[cfg(test)]
+mod test;
+pub mod uploads;
+
+/// Full selected metadata is bounded independently from small upload descriptors.
+pub const CREATE_BODY_BYTES: usize = 32 * 1024 * 1024;
+/// Enough for fifty descriptors or completion identities and one seal.
+pub const UPLOAD_BODY_BYTES: usize = 64 * 1024;
+
+/// Shared capabilities supplied by the hosting composition root.
+pub struct SlackRouterState<T, Eas, Auth> {
+ /// Administrator use cases, not worker execution.
+ pub service: Arc<T>,
+ /// Standard team receipt provider.
+ pub entity_access_service: Arc<Eas>,
+ /// Standard authentication provider.
+ pub authorization_state: MacroAuthorizationState<Auth>,
+}
+
+impl<T, Eas, Auth> Clone for SlackRouterState<T, Eas, Auth> {
+ fn clone(&self) -> Self {
+ Self {
+ service: self.service.clone(),
+ entity_access_service: self.entity_access_service.clone(),
+ authorization_state: self.authorization_state.clone(),
+ }
+ }
+}
+
+impl<T, Eas, Auth> FromRef<SlackRouterState<T, Eas, Auth>> for Arc<Eas> {
+ fn from_ref(state: &SlackRouterState<T, Eas, Auth>) -> Self {
+ state.entity_access_service.clone()
+ }
+}
+
+impl<T, Eas, Auth> FromRef<SlackRouterState<T, Eas, Auth>> for MacroAuthorizationState<Auth> {
+ fn from_ref(state: &SlackRouterState<T, Eas, Auth>) -> Self {
+ state.authorization_state.clone()
+ }
+}
+
+/// Build seven operations, mounted by DSS under `/slack`. No queue poller is started.
+pub fn slack_router<T, Eas, Auth, S>(state: SlackRouterState<T, Eas, Auth>) -> Router<S>
+where
+ T: ImportService,
+ Eas: EntityAccessService,
+ Auth: MacroAuthorizationService,
+ S: Send + Sync + 'static,
+{
+ Router::new()
+ .route(
+ "/imports",
+ get(jobs::list::<T, Eas, Auth>)
+ .post(create::create::<T, Eas, Auth>)
+ .layer(DefaultBodyLimit::max(CREATE_BODY_BYTES)),
+ )
+ .route("/imports/{job_id}", get(jobs::progress::<T, Eas, Auth>))
+ .route(
+ "/imports/{job_id}/uploads",
+ post(uploads::register::<T, Eas, Auth>).layer(DefaultBodyLimit::max(UPLOAD_BODY_BYTES)),
+ )
+ .route(
+ "/imports/{job_id}/uploads/complete",
+ post(uploads::complete::<T, Eas, Auth>).layer(DefaultBodyLimit::max(UPLOAD_BODY_BYTES)),
+ )
+ .route(
+ "/imports/{job_id}/finalize",
+ post(jobs::finalize::<T, Eas, Auth>),
+ )
+ .route(
+ "/imports/{job_id}/cancel",
+ post(jobs::cancel::<T, Eas, Auth>),
+ )
+ .with_state(state)
+}
+
+/// OpenAPI operations, merged into the hosting service's document.
+#[derive(utoipa::OpenApi)]
+#[openapi(paths(
+ create::create,
+ uploads::register,
+ uploads::complete,
+ jobs::list,
+ jobs::progress,
+ jobs::finalize,
+ jobs::cancel
+))]
+pub struct SlackApiDoc;
+
+/// Transport mapping of sanitized domain errors. Never serialize diagnostic reports.
+pub struct ApiError(ImportError);
+
+impl From<ImportError> for ApiError {
+ fn from(error: ImportError) -> Self {
+ Self(error)
+ }
+}
+
+impl IntoResponse for ApiError {
+ fn into_response(self) -> Response {
+ let status = match self.0 {
+ ImportError::InvalidInput => StatusCode::BAD_REQUEST,
+ ImportError::LimitExceeded => StatusCode::PAYLOAD_TOO_LARGE,
+ ImportError::Unavailable => StatusCode::NOT_FOUND,
+ ImportError::AdminRequired => StatusCode::FORBIDDEN,
+ ImportError::SourceMismatch | ImportError::Conflict | ImportError::LeaseLost => {
+ StatusCode::CONFLICT
+ }
+ ImportError::UploadMismatch => StatusCode::UNPROCESSABLE_ENTITY,
+ ImportError::Disabled | ImportError::Retryable => StatusCode::SERVICE_UNAVAILABLE,
+ ImportError::Internal => StatusCode::INTERNAL_SERVER_ERROR,
+ };
+ (status, Json(self.0)).into_response()
+ }
+}
diff --git a/crates/slack_integration/src/inbound/axum_router/create.rs b/crates/slack_integration/src/inbound/axum_router/create.rs
new file mode 100644
index 0000000000..69c9008350
--- /dev/null
+++ b/crates/slack_integration/src/inbound/axum_router/create.rs
@@ -0,0 +1,58 @@
+//! Creation accepts full conversation metadata, never a ZIP or file bytes.
+
+use axum::{Json, extract::State};
+use entity_access::{
+ domain::{models::AdminTeamRole, ports::EntityAccessService},
+ inbound::axum_extractors::MacroUserTeamExtractorV2,
+};
+use macro_authorization::MacroAuthorizationService;
+use serde::Deserialize;
+use utoipa::ToSchema;
+
+use super::{ApiError, SlackRouterState};
+use crate::domain::{
+ models::{CreateImport, ImportError, ImportLimits, ImportProgress},
+ ports::ImportService,
+};
+
+/// Bounded full-metadata create payload. Domain validation applies effective limits again.
+#[derive(Deserialize, ToSchema)]
+#[serde(try_from = "CreateImport")]
+#[schema(as = SlackCreateRequest, value_type = CreateImport)]
+pub struct CreateRequest(CreateImport);
+
+impl TryFrom<CreateImport> for CreateRequest {
+ type Error = ImportError;
+
+ fn try_from(command: CreateImport) -> Result<Self, Self::Error> {
+ if command.conversations.len() > ImportLimits::default().conversations as usize {
+ return Err(ImportError::LimitExceeded);
+ }
+ Ok(Self(command))
+ }
+}
+
+/// Create or replay a job using a scoped idempotency token and full selected metadata.
+/// Missing creation timestamps and unknown message counts are represented as null.
+#[utoipa::path(post, path = "/slack/imports", tag = "slack", operation_id = "create_slack_import",
+ request_body = CreateRequest,
+ responses((status = 200, body = ImportProgress), (status = 401, description = "Authentication or team administrator access required"),
+ (status = 403, description = "Team administrator required"), (status = 413, description = "Metadata limit exceeded"),
+ (status = 503, description = "Slack imports disabled or temporarily unavailable")))]
+pub async fn create<T, Eas, Auth>(
+ State(state): State<SlackRouterState<T, Eas, Auth>>,
+ access: MacroUserTeamExtractorV2<AdminTeamRole, Eas, Auth>,
+ Json(request): Json<CreateRequest>,
+) -> Result<Json<ImportProgress>, ApiError>
+where
+ T: ImportService,
+ Eas: EntityAccessService,
+ Auth: MacroAuthorizationService,
+{
+ Ok(Json(
+ state
+ .service
+ .create(access.entity_access_receipt, request.0)
+ .await?,
+ ))
+}
diff --git a/crates/slack_integration/src/inbound/axum_router/jobs.rs b/crates/slack_integration/src/inbound/axum_router/jobs.rs
new file mode 100644
index 0000000000..eedc5106d4
--- /dev/null
+++ b/crates/slack_integration/src/inbound/axum_router/jobs.rs
@@ -0,0 +1,110 @@
+//! Read and settle durable receipts, including while new imports are disabled.
+
+use axum::{
+ Json,
+ extract::{Path, Query, State},
+};
+use entity_access::{
+ domain::{models::AdminTeamRole, ports::EntityAccessService},
+ inbound::axum_extractors::MacroUserTeamExtractorV2,
+};
+use macro_authorization::MacroAuthorizationService;
+use serde::Deserialize;
+
+use super::{ApiError, SlackRouterState};
+use crate::domain::{models::*, ports::ImportService};
+
+/// Exclusive cursor. The service always bounds pages to fifty receipts.
+#[derive(Deserialize)]
+#[serde(deny_unknown_fields)]
+pub struct ListQuery {
+ /// Return jobs older than this job UUID.
+ pub before: Option<JobId>,
+}
+
+/// List this administrator's team receipts, effective server limits and source binding.
+#[utoipa::path(get, path = "/slack/imports", tag = "slack", operation_id = "list_slack_imports",
+ params(("before" = Option<JobId>, Query)), responses((status = 200, body = ImportPage)))]
+pub async fn list<T, Eas, Auth>(
+ State(state): State<SlackRouterState<T, Eas, Auth>>,
+ access: MacroUserTeamExtractorV2<AdminTeamRole, Eas, Auth>,
+ Query(query): Query<ListQuery>,
+) -> Result<Json<ImportPage>, ApiError>
+where
+ T: ImportService,
+ Eas: EntityAccessService,
+ Auth: MacroAuthorizationService,
+{
+ Ok(Json(
+ state
+ .service
+ .list(access.entity_access_receipt, query.before)
+ .await?,
+ ))
+}
+
+/// Read sanitized progress; foreign jobs are indistinguishable from missing jobs.
+#[utoipa::path(get, path = "/slack/imports/{job_id}", tag = "slack", operation_id = "get_slack_import",
+ params(("job_id" = JobId, Path)), responses((status = 200, body = ImportProgress),
+ (status = 404, description = "Unknown or inaccessible job")))]
+pub async fn progress<T, Eas, Auth>(
+ State(state): State<SlackRouterState<T, Eas, Auth>>,
+ access: MacroUserTeamExtractorV2<AdminTeamRole, Eas, Auth>,
+ Path(job_id): Path<JobId>,
+) -> Result<Json<ImportProgress>, ApiError>
+where
+ T: ImportService,
+ Eas: EntityAccessService,
+ Auth: MacroAuthorizationService,
+{
+ Ok(Json(
+ state
+ .service
+ .progress(access.entity_access_receipt, JobCommand { job_id })
+ .await?,
+ ))
+}
+
+/// Close registration idempotently; never-ready conversations are skipped, not queued work.
+#[utoipa::path(post, path = "/slack/imports/{job_id}/finalize", tag = "slack", operation_id = "finalize_slack_import",
+ params(("job_id" = JobId, Path)), responses((status = 200, body = ImportProgress),
+ (status = 404, description = "Unknown or inaccessible job")))]
+pub async fn finalize<T, Eas, Auth>(
+ State(state): State<SlackRouterState<T, Eas, Auth>>,
+ access: MacroUserTeamExtractorV2<AdminTeamRole, Eas, Auth>,
+ Path(job_id): Path<JobId>,
+) -> Result<Json<ImportProgress>, ApiError>
+where
+ T: ImportService,
+ Eas: EntityAccessService,
+ Auth: MacroAuthorizationService,
+{
+ Ok(Json(
+ state
+ .service
+ .finalize(access.entity_access_receipt, JobCommand { job_id })
+ .await?,
+ ))
+}
+
+/// Stop unclaimed work. Active leases may settle; cancellation does not roll back history.
+#[utoipa::path(post, path = "/slack/imports/{job_id}/cancel", tag = "slack", operation_id = "cancel_slack_import",
+ params(("job_id" = JobId, Path)), responses((status = 200, body = ImportProgress),
+ (status = 404, description = "Unknown or inaccessible job")))]
+pub async fn cancel<T, Eas, Auth>(
+ State(state): State<SlackRouterState<T, Eas, Auth>>,
+ access: MacroUserTeamExtractorV2<AdminTeamRole, Eas, Auth>,
+ Path(job_id): Path<JobId>,
+) -> Result<Json<ImportProgress>, ApiError>
+where
+ T: ImportService,
+ Eas: EntityAccessService,
+ Auth: MacroAuthorizationService,
+{
+ Ok(Json(
+ state
+ .service
+ .cancel(access.entity_access_receipt, JobCommand { job_id })
+ .await?,
+ ))
+}
diff --git a/crates/slack_integration/src/inbound/axum_router/test.rs b/crates/slack_integration/src/inbound/axum_router/test.rs
new file mode 100644
index 0000000000..0dbead2e38
--- /dev/null
+++ b/crates/slack_integration/src/inbound/axum_router/test.rs
@@ -0,0 +1,488 @@
+use super::*;
+use crate::domain::models::*;
+use axum::{
+ body::{Body, to_bytes},
+ http::Request,
+};
+use entity_access::domain::models::{
+ AccessError, AccessLevel, AdminTeamRole, BotAccessScope, BotId, CallChannelInfo,
+ EntityAccessReceipt, EntityPermission, EntityType, RequiredPermission, TeamRole, UserTeamInfo,
+};
+use macro_authorization::{InternalIdentityClaims, MacroAuthorizationError};
+use macro_user_id::{
+ lowercased::Lowercase,
+ user_id::{MacroUserId, MacroUserIdStr},
+};
+use model_user::UserContext;
+use rootcause::Report;
+use serde_json::{Value, json};
+use std::sync::atomic::{AtomicUsize, Ordering};
+use tower::ServiceExt;
+use utoipa::OpenApi;
+use uuid::Uuid;
+
+const TEAM: &str = "01900000-0000-7000-8000-000000000001";
+const JOB: &str = "01900000-0000-7000-8000-000000000002";
+const FOREIGN_JOB: &str = "01900000-0000-7000-8000-000000000003";
+const USER: &str = "macro|[email protected]";
+
+#[derive(Clone)]
+struct Auth;
+impl MacroAuthorizationService for Auth {
+ async fn authorize(&self, jwt: &str) -> Result<UserContext, Report<MacroAuthorizationError>> {
+ if jwt != "valid" {
+ return Err(MacroAuthorizationError::InvalidCredentials.into());
+ }
+ Ok(UserContext {
+ user_id: USER.into(),
+ fusion_user_id: "fusion-admin".into(),
+ permissions: None,
+ organization_id: None,
+ })
+ }
+ async fn authorize_internal(
+ &self,
+ _: &str,
+ _: InternalIdentityClaims,
+ ) -> Result<Option<UserContext>, Report<MacroAuthorizationError>> {
+ Err(MacroAuthorizationError::InvalidCredentials.into())
+ }
+}
+
+#[derive(Clone)]
+struct Access(TeamRole);
+impl EntityAccessService for Access {
+ async fn get_user_team(
+ &self,
+ _: &MacroUserId<Lowercase<'_>>,
+ ) -> Result<Option<UserTeamInfo>, AccessError> {
+ Ok(Some(UserTeamInfo {
+ team_id: TEAM.parse().unwrap(),
+ role: self.0,
+ }))
+ }
+ async fn generate_entity_access_receipt<P: RequiredPermission>(
+ &self,
+ _: &MacroUserId<Lowercase<'_>>,
+ _: Option<i64>,
+ _: &str,
+ _: EntityType,
+ ) -> Result<EntityAccessReceipt<P>, AccessError> {
+ unreachable!()
+ }
+ async fn generate_bot_entity_access_receipt<P: RequiredPermission>(
+ &self,
+ _: BotId,
+ _: BotAccessScope,
+ _: &str,
+ _: EntityType,
+ ) -> Result<EntityAccessReceipt<P>, AccessError> {
+ unreachable!()
+ }
+ async fn get_access_level(
+ &self,
+ _: Option<&MacroUserId<Lowercase<'_>>>,
+ _: &str,
+ _: EntityType,
+ ) -> Result<Option<AccessLevel>, AccessError> {
+ unreachable!()
+ }
+ async fn check_access(
+ &self,
+ _: Option<&MacroUserId<Lowercase<'_>>>,
+ _: &str,
+ _: EntityType,
+ _: AccessLevel,
+ ) -> Result<AccessLevel, AccessError> {
+ unreachable!()
+ }
+ async fn check_public_access(
+ &self,
+ _: &str,
+ _: EntityType,
+ _: AccessLevel,
+ ) -> Result<AccessLevel, AccessError> {
+ unreachable!()
+ }
+ async fn get_entity_permission(
+ &self,
+ _: Option<&MacroUserId<Lowercase<'_>>>,
+ _: &str,
+ _: EntityType,
+ _: Option<i64>,
+ ) -> Result<EntityPermission, AccessError> {
+ unreachable!()
+ }
+ async fn get_crm_entity_permission_with_team(
+ &self,
+ _: Option<&MacroUserId<Lowercase<'_>>>,
+ _: &str,
+ _: EntityType,
+ ) -> Result<(EntityPermission, Uuid, TeamRole), AccessError> {
+ unreachable!()
+ }
+ async fn get_users_by_entity(
+ &self,
+ _: &str,
+ _: EntityType,
+ ) -> Result<Vec<MacroUserIdStr<'static>>, AccessError> {
+ unreachable!()
+ }
+ async fn get_call_channel(&self, _: &Uuid) -> Result<Option<CallChannelInfo>, AccessError> {
+ unreachable!()
+ }
+ async fn get_call_channel_by_channel_id(
+ &self,
+ _: &Uuid,
+ ) -> Result<Option<CallChannelInfo>, AccessError> {
+ unreachable!()
+ }
+}
+
+#[derive(Default)]
+struct Service(AtomicUsize, std::sync::Mutex<Option<CreateImport>>);
+impl Service {
+ fn receipt(
+ &self,
+ access: EntityAccessReceipt<AdminTeamRole>,
+ job: JobId,
+ ) -> Result<ImportProgress, ImportError> {
+ assert_eq!(access.entity().entity_id, TEAM);
+ assert_eq!(access.get_authenticated_user().unwrap().as_ref(), USER);
+ self.0.fetch_add(1, Ordering::SeqCst);
+ if job.to_string() != JOB {
+ return Err(ImportError::Unavailable);
+ }
+ Ok(ImportProgress {
+ source: SourceIdentity::ConfirmedUnknown,
+ include_message_history: false,
+ job_id: job,
+ status: JobStatus::Uploading,
+ revision: 1,
+ created_at: chrono::Utc::now(),
+ updated_at: chrono::Utc::now(),
+ registration_closed_at: None,
+ users_verified: false,
+ limits: ImportLimits::default(),
+ conversations: vec![],
+ })
+ }
+}
+impl ImportService for Service {
+ async fn create(
+ &self,
+ access: EntityAccessReceipt<AdminTeamRole>,
+ command: CreateImport,
+ ) -> Result<ImportProgress, ImportError> {
+ assert_eq!(command.conversations[0].member_ids[0].as_str(), "U1");
+ *self.1.lock().unwrap() = Some(command);
+ self.receipt(access, JOB.parse().unwrap())
+ }
+ async fn register_uploads(
+ &self,
+ access: EntityAccessReceipt<AdminTeamRole>,
+ job: JobId,
+ command: RegisterUploads,
+ ) -> Result<Vec<UploadGrant>, ImportError> {
+ self.receipt(access, job)?;
+ Ok(command
+ .descriptors
+ .into_iter()
+ .map(|descriptor| UploadGrant {
+ descriptor,
+ url: "https://staging.invalid/signed-secret".into(),
+ required_headers: Default::default(),
+ expires_at: chrono::Utc::now(),
+ })
+ .collect())
+ }
+ async fn complete_uploads(
+ &self,
+ access: EntityAccessReceipt<AdminTeamRole>,
+ job: JobId,
+ command: CompleteUploads,
+ ) -> Result<ImportProgress, ImportError> {
+ assert_eq!(command.seal.unwrap().part_count, 0);
+ self.receipt(access, job)
+ }
+ async fn finalize(
+ &self,
+ access: EntityAccessReceipt<AdminTeamRole>,
+ command: JobCommand,
+ ) -> Result<ImportProgress, ImportError> {
+ self.receipt(access, command.job_id)
+ }
+ async fn cancel(
+ &self,
+ access: EntityAccessReceipt<AdminTeamRole>,
+ command: JobCommand,
+ ) -> Result<ImportProgress, ImportError> {
+ self.receipt(access, command.job_id)
+ }
+ async fn progress(
+ &self,
+ access: EntityAccessReceipt<AdminTeamRole>,
+ command: JobCommand,
+ ) -> Result<ImportProgress, ImportError> {
+ self.receipt(access, command.job_id)
+ }
+ async fn list(
+ &self,
+ access: EntityAccessReceipt<AdminTeamRole>,
+ _: Option<JobId>,
+ ) -> Result<ImportPage, ImportError> {
+ Ok(ImportPage {
+ jobs: vec![self.receipt(access, JOB.parse().unwrap())?],
+ next_cursor: None,
+ limits: ImportLimits::default(),
+ source_binding: SourceBinding::ConfirmedUnknown,
+ })
+ }
+}
+
+fn router(role: TeamRole) -> (Router, Arc<Service>) {
+ let service = Arc::new(Service::default());
+ let router = Router::new().nest(
+ "/slack",
+ slack_router(SlackRouterState {
+ service: service.clone(),
+ entity_access_service: Arc::new(Access(role)),
+ authorization_state: MacroAuthorizationState::new(Arc::new(Auth)),
+ }),
+ );
+ (router, service)
+}
+
+fn request(method: &str, path: &str, authenticated: bool, body: impl Into<Body>) -> Request<Body> {
+ let mut builder = Request::builder()
+ .method(method)
+ .uri(path)
+ .header("content-type", "application/json");
+ if authenticated {
+ builder = builder.header("authorization", "Bearer valid");
+ }
+ builder.body(body.into()).unwrap()
+}
+
+fn operations(job: &str) -> Vec<(&'static str, String)> {
+ vec![
+ ("POST", "/slack/imports".into()),
+ ("GET", "/slack/imports".into()),
+ ("GET", format!("/slack/imports/{job}")),
+ ("POST", format!("/slack/imports/{job}/uploads")),
+ ("POST", format!("/slack/imports/{job}/uploads/complete")),
+ ("POST", format!("/slack/imports/{job}/finalize")),
+ ("POST", format!("/slack/imports/{job}/cancel")),
+ ]
+}
+
+fn create_body() -> Value {
+ json!({"idempotencyToken": JOB, "source": {"kind": "confirmed_unknown"}, "includeMessageHistory": false,
+ "conversations": [{"slackChannelId": "C1", "kind": "public_channel", "name": "general", "folder": "general",
+ "memberIds": ["U1"], "creatorId": null, "createdAt": null, "archived": false, "messageCount": null}]})
+}
+
+fn seal_body() -> Value {
+ json!({"uploads": [], "seal": {"slackChannelId": "C1", "partCount": 0, "manifestSha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"}})
+}
+
+#[tokio::test]
+async fn all_seven_operations_require_authenticated_team_admin() {
+ for (role, authenticated, status) in [
+ (TeamRole::Admin, false, StatusCode::UNAUTHORIZED),
+ (TeamRole::Member, true, StatusCode::UNAUTHORIZED),
+ ] {
+ let (app, service) = router(role);
+ for (method, path) in operations(JOB) {
+ let response = app
+ .clone()
+ .oneshot(request(method, &path, authenticated, "{}"))
+ .await
+ .unwrap();
+ assert_eq!(response.status(), status, "{method} {path}");
+ }
+ assert_eq!(service.0.load(Ordering::SeqCst), 0);
+ }
+}
+
+#[tokio::test]
+async fn receipts_metadata_and_seals_are_forwarded() {
+ let (app, service) = router(TeamRole::Admin);
+ for (method, path) in operations(JOB) {
+ let body = if path.ends_with("/complete") {
+ seal_body()
+ } else if path.ends_with("/uploads") {
+ json!({"descriptors": []})
+ } else if path == "/slack/imports" && method == "POST" {
+ create_body()
+ } else {
+ json!({})
+ };
+ let response = app
+ .clone()
+ .oneshot(request(method, &path, true, body.to_string()))
+ .await
+ .unwrap();
+ assert_eq!(response.status(), StatusCode::OK, "{path}");
+ let body: Value = serde_json::from_slice(
+ &to_bytes(response.into_body(), CREATE_BODY_BYTES)
+ .await
+ .unwrap(),
+ )
+ .unwrap();
+ if method == "GET" && path == "/slack/imports" {
+ assert_eq!(body["limits"]["registrationBatch"], 50);
+ assert_eq!(body["sourceBinding"]["kind"], "confirmed_unknown");
+ }
+ if path != "/slack/imports" && !path.ends_with("/uploads") {
+ assert_eq!(body["source"]["kind"], "confirmed_unknown");
+ assert_eq!(body["includeMessageHistory"], false);
+ }
+ assert!(!body.to_string().contains("signed-secret"));
+ }
+ assert_eq!(service.0.load(Ordering::SeqCst), 7);
+}
+
+#[tokio::test]
+async fn create_http_boundary_preserves_exact_confirmed_metadata_and_options() {
+ let (app, service) = router(TeamRole::Admin);
+ let mut body = create_body();
+ let mut selected = Vec::new();
+ for id in ["CA", "CC"] {
+ let mut metadata = body["conversations"][0].clone();
+ metadata["slackChannelId"] = json!(id);
+ metadata["folder"] = json!(id);
+ metadata["creatorId"] = json!("U1");
+ metadata["createdAt"] = json!("1700000000.000001");
+ metadata["archived"] = json!(id == "CC");
+ selected.push(metadata);
+ }
+ body["conversations"] = json!(selected);
+ let response = app
+ .oneshot(request("POST", "/slack/imports", true, body.to_string()))
+ .await
+ .unwrap();
+ assert_eq!(response.status(), StatusCode::OK);
+ assert_eq!(
+ serde_json::to_value(service.1.lock().unwrap().as_ref().unwrap()).unwrap(),
+ body
+ );
+}
+
+#[tokio::test]
+async fn foreign_jobs_map_to_nondisclosing_not_found_on_every_job_operation() {
+ let (app, _) = router(TeamRole::Admin);
+ for (method, path) in operations(FOREIGN_JOB).into_iter().skip(2) {
+ let body = if path.ends_with("/complete") {
+ seal_body()
+ } else {
+ json!({"descriptors": []})
+ };
+ let response = app
+ .clone()
+ .oneshot(request(method, &path, true, body.to_string()))
+ .await
+ .unwrap();
+ assert_eq!(response.status(), StatusCode::NOT_FOUND, "{path}");
+ assert_eq!(
+ to_bytes(response.into_body(), 1024).await.unwrap(),
+ "\"unavailable\""
+ );
+ }
+}
+
+#[tokio::test]
+async fn route_specific_body_limits_reject_before_service() {
+ let (app, service) = router(TeamRole::Admin);
+ for (path, limit) in [
+ ("/slack/imports".into(), CREATE_BODY_BYTES),
+ (format!("/slack/imports/{JOB}/uploads"), UPLOAD_BODY_BYTES),
+ (
+ format!("/slack/imports/{JOB}/uploads/complete"),
+ UPLOAD_BODY_BYTES,
+ ),
+ ] {
+ let response = app
+ .clone()
+ .oneshot(request("POST", &path, true, " ".repeat(limit + 1)))
+ .await
+ .unwrap();
+ assert_eq!(response.status(), StatusCode::PAYLOAD_TOO_LARGE, "{path}");
+ }
+ assert_eq!(service.0.load(Ordering::SeqCst), 0);
+}
+
+#[tokio::test]
+async fn grants_are_uncacheable_and_not_part_of_progress() {
+ let (app, _) = router(TeamRole::Admin);
+ let response = app.oneshot(request("POST", &format!("/slack/imports/{JOB}/uploads"), true,
+ json!({"descriptors": [{"upload": {"kind": "users"}, "sha256": "a".repeat(64), "byteLength": 10, "recordCount": null}]}).to_string())).await.unwrap();
+ assert_eq!(response.status(), StatusCode::OK);
+ assert_eq!(response.headers()["cache-control"], "no-store");
+ let body = to_bytes(response.into_body(), 4096).await.unwrap();
+ assert!(String::from_utf8_lossy(&body).contains("signed-secret"));
+}
+
+#[tokio::test]
+async fn disabled_errors_are_explicit_and_distinct_from_missing_jobs() {
+ let response = ApiError::from(ImportError::Disabled).into_response();
+ assert_eq!(response.status(), StatusCode::SERVICE_UNAVAILABLE);
+ assert_eq!(
+ to_bytes(response.into_body(), 1024).await.unwrap(),
+ "\"disabled\""
+ );
+ assert_eq!(
+ ApiError::from(ImportError::Unavailable)
+ .into_response()
+ .status(),
+ StatusCode::NOT_FOUND,
+ );
+}
+
+#[test]
+fn bounded_dtos_reject_oversized_collections_and_body_supplied_team() {
+ let mut body = create_body();
+ body["conversations"] = json!(vec![body["conversations"][0].clone(); 2001]);
+ assert!(serde_json::from_value::<create::CreateRequest>(body).is_err());
+ let mut body = create_body();
+ body["teamId"] = json!(TEAM);
+ assert!(serde_json::from_value::<create::CreateRequest>(body).is_err());
+ assert!(
+ serde_json::from_value::<uploads::CompleteRequest>(
+ json!({"uploads": vec![json!({"kind": "users"}); 51], "seal": null})
+ )
+ .is_err()
+ );
+}
+
+#[test]
+fn openapi_registers_all_operations_and_metadata_seal_schemas() {
+ let doc = serde_json::to_value(SlackApiDoc::openapi()).unwrap();
+ for (method, path) in operations("{job_id}") {
+ assert!(doc["paths"][path][method.to_lowercase()].is_object());
+ }
+ let schemas = &doc["components"]["schemas"];
+ assert!(schemas["ConversationMetadata"]["properties"]["memberIds"].is_object());
+ assert!(schemas["ConversationSeal"]["properties"]["manifestSha256"].is_object());
+ assert!(schemas["ImportPage"]["properties"]["sourceBinding"].is_object());
+ for field in ["name", "kind", "archived"] {
+ assert!(schemas["ConversationProgress"]["properties"][field].is_object());
+ }
+ for field in ["source", "includeMessageHistory"] {
+ assert!(schemas["ImportProgress"]["properties"][field].is_object());
+ }
+ // Utoipa does not infer serde's rename_all_fields for enum payloads.
+ for (name, variant, fields) in [
+ ("SourceIdentity", 0, vec!["sourceId"]),
+ ("SourceBinding", 2, vec!["sourceId"]),
+ ("UploadId", 1, vec!["slackChannelId", "partIndex"]),
+ ("SearchState", 2, vec!["receiptId"]),
+ ] {
+ let properties = &schemas[name]["oneOf"][variant]["properties"];
+ for field in fields {
+ assert!(properties[field].is_object(), "{name}.{field}");
+ }
+ }
+ assert_eq!(schemas["SlackTimestamp"]["type"], "string");
+ assert!(schemas["ImportProgress"]["properties"].get("url").is_none());
+}
diff --git a/crates/slack_integration/src/inbound/axum_router/uploads.rs b/crates/slack_integration/src/inbound/axum_router/uploads.rs
new file mode 100644
index 0000000000..92e244a4bc
--- /dev/null
+++ b/crates/slack_integration/src/inbound/axum_router/uploads.rs
@@ -0,0 +1,103 @@
+//! Bounded manifest registration and explicit verification/sealing.
+
+use axum::{
+ Json,
+ extract::{Path, State},
+ http::header,
+};
+use entity_access::{
+ domain::{models::AdminTeamRole, ports::EntityAccessService},
+ inbound::axum_extractors::MacroUserTeamExtractorV2,
+};
+use macro_authorization::MacroAuthorizationService;
+use serde::Deserialize;
+use utoipa::ToSchema;
+
+use super::{ApiError, SlackRouterState};
+use crate::domain::{models::*, ports::ImportService};
+
+/// At most fifty immutable upload descriptors, without client-supplied storage keys.
+#[derive(Deserialize, ToSchema)]
+#[serde(try_from = "RegisterUploads")]
+#[schema(as = SlackRegisterRequest, value_type = RegisterUploads)]
+pub struct RegisterRequest(RegisterUploads);
+
+impl TryFrom<RegisterUploads> for RegisterRequest {
+ type Error = ImportError;
+
+ fn try_from(command: RegisterUploads) -> Result<Self, Self::Error> {
+ if command.descriptors.len() > ImportLimits::default().registration_batch as usize {
+ return Err(ImportError::LimitExceeded);
+ }
+ Ok(Self(command))
+ }
+}
+
+/// At most fifty registered identities to verify and an optional immutable conversation seal.
+#[derive(Deserialize, ToSchema)]
+#[serde(try_from = "CompleteUploads")]
+#[schema(as = SlackCompleteRequest, value_type = CompleteUploads)]
+pub struct CompleteRequest(CompleteUploads);
+
+impl TryFrom<CompleteUploads> for CompleteRequest {
+ type Error = ImportError;
+
+ fn try_from(command: CompleteUploads) -> Result<Self, Self::Error> {
+ if command.uploads.len() > ImportLimits::default().registration_batch as usize {
+ return Err(ImportError::LimitExceeded);
+ }
+ Ok(Self(command))
+ }
+}
+
+/// Issue short-lived, checksum/length-bound, create-only PUT grants. Do not log or persist URLs.
+/// Retry a PUT returning 412 by completing/verifying the existing object, not overwriting it.
+#[utoipa::path(post, path = "/slack/imports/{job_id}/uploads", tag = "slack", operation_id = "register_slack_import_uploads",
+ params(("job_id" = JobId, Path)), request_body = RegisterRequest,
+ responses((status = 200, body = Vec<UploadGrant>), (status = 404, description = "Unknown or inaccessible job"),
+ (status = 413, description = "Body limit exceeded"), (status = 503, description = "Slack imports disabled")))]
+pub async fn register<T, Eas, Auth>(
+ State(state): State<SlackRouterState<T, Eas, Auth>>,
+ access: MacroUserTeamExtractorV2<AdminTeamRole, Eas, Auth>,
+ Path(job): Path<JobId>,
+ Json(request): Json<RegisterRequest>,
+) -> Result<impl axum::response::IntoResponse, ApiError>
+where
+ T: ImportService,
+ Eas: EntityAccessService,
+ Auth: MacroAuthorizationService,
+{
+ let grants = state
+ .service
+ .register_uploads(access.entity_access_receipt, job, request.0)
+ .await?;
+ Ok(([(header::CACHE_CONTROL, "no-store")], Json(grants)))
+}
+
+/// Verify storage checksums and lengths for registered objects. A seal fixes the ENTIRE
+/// descriptor set (contiguous indices, sizes and hashes), not just this completion batch.
+/// Empty uploads with a zero-part seal support shape-only or empty-history imports.
+/// Work becomes ready only after verified users metadata, a seal and every required part.
+#[utoipa::path(post, path = "/slack/imports/{job_id}/uploads/complete", tag = "slack", operation_id = "complete_slack_import_uploads",
+ params(("job_id" = JobId, Path)), request_body = CompleteRequest,
+ responses((status = 200, body = ImportProgress), (status = 404, description = "Unknown or inaccessible job"),
+ (status = 409, description = "Immutable manifest conflict"), (status = 413, description = "Body limit exceeded"),
+ (status = 422, description = "Object verification failed"), (status = 503, description = "Slack imports disabled")))]
+pub async fn complete<T, Eas, Auth>(
+ State(state): State<SlackRouterState<T, Eas, Auth>>,
+ access: MacroUserTeamExtractorV2<AdminTeamRole, Eas, Auth>,
+ Path(job): Path<JobId>,
+ Json(request): Json<CompleteRequest>,
+) -> Result<Json<ImportProgress>, ApiError>
+where
+ T: ImportService,
+ Eas: EntityAccessService,
+ Auth: MacroAuthorizationService,
+{
+ Ok(Json(
+ state
+ .service
+ .complete_uploads(access.entity_access_receipt, job, request.0)
+ .await?,
+ ))
+}
diff --git a/crates/slack_integration/src/inbound/worker.rs b/crates/slack_integration/src/inbound/worker.rs
new file mode 100644
index 0000000000..6ef63f4feb
--- /dev/null
+++ b/crates/slack_integration/src/inbound/worker.rs
@@ -0,0 +1,261 @@
+//! Bounded queue driver. Domain services own claims, durable outcomes and recovery.
+//! No detached import futures survive shutdown or loss of a heartbeat.
+
+use std::{future::Future, time::Duration};
+
+use futures::future::join_all;
+use tokio::time::{Instant, MissedTickBehavior, interval_at, sleep, timeout};
+use tokio_util::sync::CancellationToken;
+
+use crate::domain::{
+ maintenance::Maintenance,
+ models::*,
+ ports::{ImportConsumer, ImportWorker, PortResult},
+};
+
+#[cfg(test)]
+mod test;
+
+/// Validated operational bounds. A restart changes enablement; disabled instances
+/// still reconcile DLQ records, expired/cancelled leases and dirty search work.
+#[derive(Clone, Copy)]
+pub struct WorkerConfig {
+ enabled: bool,
+ concurrency: u8,
+ max_receives: u32,
+ heartbeat: Duration,
+ operation_timeout: Duration,
+ retry_delay: Duration,
+ maintenance_interval: Duration,
+ drain_timeout: Duration,
+}
+
+impl WorkerConfig {
+ /// Default to one import at a time; permit at most two. Reserve ten seconds
+ /// of the task's 120-second stop budget for runtime/telemetry teardown.
+ pub fn new(enabled: bool, concurrency: u8, max_receives: u32) -> Result<Self, ImportError> {
+ if !(1..=2).contains(&concurrency) || max_receives == 0 {
+ return Err(ImportError::InvalidInput);
+ }
+ Ok(Self {
+ enabled,
+ concurrency,
+ max_receives,
+ heartbeat: Duration::from_secs(60),
+ operation_timeout: Duration::from_secs(30),
+ retry_delay: Duration::from_secs(5),
+ maintenance_interval: Duration::from_secs(10),
+ drain_timeout: Duration::from_secs(110),
+ })
+ }
+}
+
+/// Run independent claim, publication, recovery and DLQ loops. On cancellation,
+/// stop receiving immediately and drain active imports with heartbeats intact.
+/// At the deadline drop all futures: committed checkpoints and expired fenced
+/// leases are recovered by another instance, never acknowledged speculatively.
+pub async fn run(
+ work: &impl ImportWorker,
+ maintenance: &impl Maintenance,
+ queue: &impl ImportConsumer,
+ owner: WorkerId,
+ config: WorkerConfig,
+ stop: CancellationToken,
+) {
+ let lanes = async {
+ if config.enabled {
+ join_all(
+ (0..config.concurrency)
+ .map(|_| consume(work, maintenance, queue, owner, config, &stop, false)),
+ )
+ .await;
+ }
+ };
+ let loops = async {
+ tokio::join!(
+ lanes,
+ consume(work, maintenance, queue, owner, config, &stop, true),
+ maintain(maintenance, config, &stop, false),
+ maintain(maintenance, config, &stop, true),
+ );
+ };
+ tokio::pin!(loops);
+ tokio::select! {
+ biased;
+ _ = stop.cancelled() => (),
+ _ = &mut loops => return,
+ }
+ if timeout(config.drain_timeout, loops).await.is_err() {
+ tracing::warn!(
+ operation = "shutdown",
+ outcome = "checkpoint_recovery",
+ "slack import drain deadline"
+ );
+ }
+}
+
+async fn consume(
+ work: &impl ImportWorker,
+ maintenance: &impl Maintenance,
+ queue: &impl ImportConsumer,
+ owner: WorkerId,
+ config: WorkerConfig,
+ stop: &CancellationToken,
+ dead_letter: bool,
+) {
+ loop {
+ let received = tokio::select! {
+ biased;
+ _ = stop.cancelled() => return,
+ result = bounded(config, queue.receive(dead_letter)) => result,
+ };
+ if stop.is_cancelled() {
+ return;
+ }
+ match received {
+ Ok(Some(delivery)) => {
+ let result = handle(
+ work,
+ maintenance,
+ queue,
+ &delivery,
+ owner,
+ config,
+ dead_letter,
+ )
+ .await;
+ match result {
+ Ok(WorkerOutcome::Acknowledge) => {
+ report("delete", bounded(config, queue.delete(&delivery)).await);
+ }
+ Ok(WorkerOutcome::Defer) => {
+ tracing::info!(
+ operation = "delivery",
+ outcome = "deferred",
+ dead_letter,
+ "slack import delivery"
+ );
+ }
+ Err(error) => report("delivery", Err(error)),
+ }
+ }
+ Ok(None) => (),
+ Err(error) => {
+ report("receive", Err(error));
+ tokio::select! {
+ _ = stop.cancelled() => return,
+ _ = sleep(config.retry_delay) => (),
+ }
+ }
+ }
+ }
+}
+
+async fn handle<Q: ImportConsumer>(
+ work: &impl ImportWorker,
+ maintenance: &impl Maintenance,
+ queue: &Q,
+ delivery: &Q::Delivery,
+ owner: WorkerId,
+ config: WorkerConfig,
+ dead_letter: bool,
+) -> PortResult<WorkerOutcome> {
+ let (event, receive_count) = Q::envelope(delivery);
+ let event = match event {
+ Ok(event) => event,
+ Err(code) => {
+ tracing::warn!(
+ ?code,
+ dead_letter,
+ operation = "malformed",
+ "slack import invalid envelope"
+ );
+ // Main poison messages redrive to DLQ for a separate sanitized signal.
+ // No trusted identity exists to update. Never log the raw payload.
+ return Ok(if dead_letter {
+ WorkerOutcome::Acknowledge
+ } else {
+ WorkerOutcome::Defer
+ });
+ }
+ };
+ let process = async {
+ if dead_letter || receive_count >= config.max_receives {
+ return bounded(config, maintenance.dead_letter(&event)).await;
+ }
+ match bounded(config, work.claim(&event, owner)).await? {
+ ClaimOutcome::Obsolete => Ok(WorkerOutcome::Acknowledge),
+ ClaimOutcome::ActiveLease => Ok(WorkerOutcome::Defer),
+ ClaimOutcome::Claimed(context) => {
+ // These futures are polled independently of storage reads, target
+ // resolution and batch commits. A failed renewal drops the importer.
+ tokio::select! {
+ biased;
+ result = heartbeat(config, || async { work.heartbeat(&context.lease).await.map(|_| ()) }) => result,
+ result = work.import(&context) => result,
+ }
+ }
+ }
+ };
+ tokio::select! {
+ biased;
+ result = heartbeat(config, || queue.extend(delivery)) => result,
+ result = process => result,
+ }
+}
+
+async fn heartbeat<F, Fut>(config: WorkerConfig, mut renew: F) -> PortResult<WorkerOutcome>
+where
+ F: FnMut() -> Fut,
+ Fut: Future<Output = PortResult<()>>,
+{
+ let mut ticks = interval_at(Instant::now() + config.heartbeat, config.heartbeat);
+ ticks.set_missed_tick_behavior(MissedTickBehavior::Delay);
+ loop {
+ ticks.tick().await;
+ bounded(config, renew()).await?;
+ }
+}
+
+async fn maintain(
+ service: &impl Maintenance,
+ config: WorkerConfig,
+ stop: &CancellationToken,
+ publication: bool,
+) {
+ if publication && !config.enabled {
+ return;
+ }
+ let operation = if publication { "outbox" } else { "reconcile" };
+ loop {
+ tokio::select! {
+ biased;
+ _ = stop.cancelled() => return,
+ result = bounded(config, async {
+ if publication { service.publish().await } else { service.reconcile().await }
+ }) => report(operation, result),
+ }
+ tokio::select! {
+ _ = stop.cancelled() => return,
+ _ = sleep(config.maintenance_interval) => (),
+ }
+ }
+}
+
+async fn bounded<T>(
+ config: WorkerConfig,
+ operation: impl Future<Output = PortResult<T>>,
+) -> PortResult<T> {
+ timeout(config.operation_timeout, operation)
+ .await
+ .map_err(|_| ImportError::Retryable)?
+}
+
+fn report(operation: &'static str, result: PortResult<()>) {
+ match result {
+ Ok(()) => tracing::info!(operation, outcome = "success", "slack import operation"),
+ Err(error) => {
+ tracing::warn!(operation, code = ?error.current_context(), "slack import retry")
+ }
+ }
+}
diff --git a/crates/slack_integration/src/inbound/worker/test.rs b/crates/slack_integration/src/inbound/worker/test.rs
new file mode 100644
index 0000000000..9d166f4637
--- /dev/null
+++ b/crates/slack_integration/src/inbound/worker/test.rs
@@ -0,0 +1,555 @@
+use super::*;
+use chrono::Utc;
+use macro_user_id::user_id::MacroUserIdStr;
+use std::{
+ collections::VecDeque,
+ sync::{Arc, Mutex},
+};
+use uuid::Uuid;
+
+#[derive(Clone)]
+struct Delivery {
+ event: Result<ImportEvent, ImportError>,
+ receives: u32,
+}
+
+#[derive(Default)]
+struct State {
+ main: VecDeque<Delivery>,
+ dlq: VecDeque<Delivery>,
+ active: bool,
+ settled: bool,
+ generation: u64,
+ checkpoint: u32,
+ claims: usize,
+ imports: usize,
+ deletes: usize,
+ heartbeats: usize,
+ visibility: usize,
+ publications: usize,
+ reconciliations: usize,
+ dead_letters: usize,
+ heartbeat_error: Option<ImportError>,
+ visibility_error: bool,
+ heartbeat_hangs: bool,
+ import_error: Option<ImportError>,
+ claim_error: Option<ImportError>,
+ dlq_error: bool,
+ delete_error: bool,
+ publication_hangs: bool,
+ import_delay: Duration,
+}
+
+#[derive(Clone, Default)]
+struct Fake {
+ state: Arc<Mutex<State>>,
+ started: CancellationToken,
+ stop: CancellationToken,
+}
+
+fn event() -> ImportEvent {
+ ImportEvent {
+ job_id: Uuid::now_v7().try_into().unwrap(),
+ slack_channel_id: "C123".parse().unwrap(),
+ generation: 1,
+ }
+}
+
+fn owner() -> WorkerId {
+ Uuid::now_v7().try_into().unwrap()
+}
+
+fn delivery() -> Delivery {
+ Delivery {
+ event: Ok(event()),
+ receives: 1,
+ }
+}
+
+fn context(event: &ImportEvent, owner: WorkerId, generation: u64) -> ClaimedConversation {
+ let now = Utc::now();
+ ClaimedConversation {
+ lease: Lease {
+ event: event.clone(),
+ owner,
+ token: Uuid::now_v7().try_into().unwrap(),
+ generation,
+ expires_at: now + chrono::Duration::minutes(3),
+ heartbeat_at: now,
+ attempts: generation as u32,
+ },
+ team_id: Uuid::now_v7().try_into().unwrap(),
+ requested_by: MacroUserIdStr::try_from_email("[email protected]").unwrap(),
+ metadata: ConversationMetadata {
+ slack_channel_id: event.slack_channel_id.clone(),
+ kind: ConversationKind::PublicChannel,
+ name: "test".into(),
+ folder: "test".parse().unwrap(),
+ member_ids: vec![],
+ creator_id: None,
+ created_at: None,
+ archived: false,
+ message_count: None,
+ },
+ users: VerifiedUpload {
+ registered: RegisteredUpload {
+ descriptor: UploadDescriptor {
+ upload: UploadId::Users,
+ sha256: "0".repeat(64).parse().unwrap(),
+ byte_length: 2,
+ record_count: None,
+ },
+ key: "users".parse().unwrap(),
+ },
+ identity: ObjectIdentity::EntityTag("pinned".parse().unwrap()),
+ },
+ parts: vec![],
+ checkpoint: Checkpoint::default(),
+ job_created_at: now,
+ }
+}
+
+impl ImportWorker for Fake {
+ async fn claim(&self, event: &ImportEvent, owner: WorkerId) -> PortResult<ClaimOutcome> {
+ let mut state = self.state.lock().unwrap();
+ state.claims += 1;
+ if let Some(code) = state.claim_error {
+ return Err(code.into());
+ }
+ if state.settled {
+ return Ok(ClaimOutcome::Obsolete);
+ }
+ if state.active {
+ return Ok(ClaimOutcome::ActiveLease);
+ }
+ state.active = true;
+ state.generation += 1;
+ Ok(ClaimOutcome::Claimed(Box::new(context(
+ event,
+ owner,
+ state.generation,
+ ))))
+ }
+
+ async fn import(&self, context: &ClaimedConversation) -> PortResult<WorkerOutcome> {
+ let delay = {
+ let mut state = self.state.lock().unwrap();
+ state.imports += 1;
+ state.checkpoint = 1; // One durable batch before blocking on slow I/O.
+ state.import_delay
+ };
+ self.started.cancel();
+ sleep(delay).await;
+ let mut state = self.state.lock().unwrap();
+ if let Some(code) = state.import_error {
+ return Err(code.into());
+ }
+ assert_eq!(context.lease.generation, state.generation);
+ state.checkpoint = 2;
+ state.settled = true;
+ state.active = false;
+ Ok(WorkerOutcome::Acknowledge)
+ }
+
+ async fn heartbeat(&self, lease: &Lease) -> PortResult<Lease> {
+ let hangs = {
+ let mut state = self.state.lock().unwrap();
+ state.heartbeats += 1;
+ if let Some(code) = state.heartbeat_error {
+ return Err(code.into());
+ }
+ state.heartbeat_hangs
+ };
+ if hangs {
+ std::future::pending::<()>().await;
+ }
+ Ok(lease.clone())
+ }
+}
+
+impl ImportConsumer for Fake {
+ type Delivery = Delivery;
+
+ async fn receive(&self, dead_letter: bool) -> PortResult<Option<Delivery>> {
+ let delivery = {
+ let mut state = self.state.lock().unwrap();
+ if dead_letter {
+ state.dlq.pop_front()
+ } else {
+ state.main.pop_front()
+ }
+ };
+ if delivery.is_none() {
+ std::future::pending::<()>().await;
+ }
+ Ok(delivery)
+ }
+
+ fn envelope(delivery: &Delivery) -> (Result<ImportEvent, ImportError>, u32) {
+ (delivery.event.clone(), delivery.receives)
+ }
+
+ async fn extend(&self, _: &Delivery) -> PortResult<()> {
+ let mut state = self.state.lock().unwrap();
+ state.visibility += 1;
+ if state.visibility_error {
+ return Err(ImportError::Retryable.into());
+ }
+ Ok(())
+ }
+
+ async fn delete(&self, delivery: &Delivery) -> PortResult<()> {
+ let mut state = self.state.lock().unwrap();
+ assert!(
+ state.settled || delivery.event.is_err(),
+ "no speculative ack"
+ );
+ state.deletes += 1;
+ self.stop.cancel();
+ if state.delete_error {
+ return Err(ImportError::Retryable.into());
+ }
+ Ok(())
+ }
+}
+
+impl Maintenance for Fake {
+ async fn publish(&self) -> PortResult<()> {
+ let hangs = {
+ let mut state = self.state.lock().unwrap();
+ state.publications += 1;
+ state.publication_hangs
+ };
+ if hangs {
+ std::future::pending::<()>().await;
+ }
+ Ok(())
+ }
+ async fn reconcile(&self) -> PortResult<()> {
+ self.state.lock().unwrap().reconciliations += 1;
+ Ok(())
+ }
+ async fn dead_letter(&self, _: &ImportEvent) -> PortResult<WorkerOutcome> {
+ let mut state = self.state.lock().unwrap();
+ state.dead_letters += 1;
+ if state.active {
+ return Ok(WorkerOutcome::Defer);
+ }
+ if state.dlq_error {
+ return Err(ImportError::Retryable.into());
+ }
+ state.settled = true;
+ Ok(WorkerOutcome::Acknowledge)
+ }
+}
+
+fn config() -> WorkerConfig {
+ WorkerConfig::new(true, 1, 5).unwrap()
+}
+
+async fn attempt(fake: &Fake, delivery: &Delivery, dlq: bool) -> PortResult<WorkerOutcome> {
+ handle(fake, fake, fake, delivery, owner(), config(), dlq).await
+}
+
+#[test]
+fn validates_bounded_concurrency_and_drain_budget() {
+ assert!(WorkerConfig::new(true, 0, 5).is_err());
+ assert!(WorkerConfig::new(true, 3, 5).is_err());
+ assert!(WorkerConfig::new(true, 1, 0).is_err());
+ assert!(WorkerConfig::new(true, 2, 5).is_ok());
+ assert!(config().drain_timeout < Duration::from_secs(120));
+}
+
+#[tokio::test(start_paused = true)]
+async fn only_available_slots_receive_and_claim() {
+ use std::sync::atomic::{AtomicUsize, Ordering};
+
+ struct Work(AtomicUsize);
+ impl ImportWorker for Work {
+ async fn claim(&self, event: &ImportEvent, owner: WorkerId) -> PortResult<ClaimOutcome> {
+ self.0.fetch_add(1, Ordering::SeqCst);
+ Ok(ClaimOutcome::Claimed(Box::new(context(event, owner, 1))))
+ }
+ async fn import(&self, _: &ClaimedConversation) -> PortResult<WorkerOutcome> {
+ std::future::pending().await
+ }
+ async fn heartbeat(&self, lease: &Lease) -> PortResult<Lease> {
+ Ok(lease.clone())
+ }
+ }
+
+ for concurrency in [1, 2] {
+ let fake = Fake::default();
+ fake.state
+ .lock()
+ .unwrap()
+ .main
+ .extend((0..4).map(|_| delivery()));
+ let work = Work(AtomicUsize::new(0));
+ let stop = async {
+ sleep(Duration::from_secs(1)).await;
+ fake.stop.cancel();
+ };
+ tokio::join!(
+ run(
+ &work,
+ &fake,
+ &fake,
+ owner(),
+ WorkerConfig::new(true, concurrency, 5).unwrap(),
+ fake.stop.clone()
+ ),
+ stop,
+ );
+ assert_eq!(work.0.load(Ordering::SeqCst), usize::from(concurrency));
+ assert_eq!(
+ fake.state.lock().unwrap().main.len(),
+ 4 - usize::from(concurrency)
+ );
+ assert_eq!(fake.state.lock().unwrap().deletes, 0);
+ }
+}
+
+#[tokio::test(start_paused = true)]
+async fn duplicate_active_delivery_defers_then_terminal_delivery_acknowledges() {
+ let fake = Fake::default();
+ fake.state.lock().unwrap().active = true;
+ assert_eq!(
+ attempt(&fake, &delivery(), false).await.unwrap(),
+ WorkerOutcome::Defer
+ );
+ assert_eq!(fake.state.lock().unwrap().imports, 0);
+ fake.state.lock().unwrap().settled = true;
+ assert_eq!(
+ attempt(&fake, &delivery(), false).await.unwrap(),
+ WorkerOutcome::Acknowledge
+ );
+}
+
+#[tokio::test(start_paused = true)]
+async fn heartbeats_run_during_slow_batches() {
+ let fake = Fake::default();
+ fake.state.lock().unwrap().import_delay = Duration::from_secs(125);
+ assert_eq!(
+ attempt(&fake, &delivery(), false).await.unwrap(),
+ WorkerOutcome::Acknowledge
+ );
+ let state = fake.state.lock().unwrap();
+ assert_eq!(state.heartbeats, 2);
+ assert_eq!(state.visibility, 2);
+ assert!(state.settled);
+}
+
+#[tokio::test(start_paused = true)]
+async fn visibility_failure_drops_import_without_settlement() {
+ let fake = Fake::default();
+ {
+ let mut state = fake.state.lock().unwrap();
+ state.import_delay = Duration::from_secs(125);
+ state.visibility_error = true;
+ }
+ assert_eq!(
+ *attempt(&fake, &delivery(), false)
+ .await
+ .unwrap_err()
+ .current_context(),
+ ImportError::Retryable
+ );
+ tokio::time::advance(Duration::from_secs(200)).await;
+ let state = fake.state.lock().unwrap();
+ assert_eq!(state.checkpoint, 1);
+ assert!(!state.settled);
+ assert_eq!(state.deletes, 0);
+}
+
+#[tokio::test(start_paused = true)]
+async fn lost_fence_and_stalled_heartbeat_stop_writes() {
+ for hangs in [false, true] {
+ let fake = Fake::default();
+ {
+ let mut state = fake.state.lock().unwrap();
+ state.import_delay = Duration::from_secs(125);
+ state.heartbeat_hangs = hangs;
+ if !hangs {
+ state.heartbeat_error = Some(ImportError::LeaseLost);
+ }
+ }
+ let error = attempt(&fake, &delivery(), false).await.unwrap_err();
+ assert_eq!(
+ *error.current_context(),
+ if hangs {
+ ImportError::Retryable
+ } else {
+ ImportError::LeaseLost
+ }
+ );
+ assert_eq!(fake.state.lock().unwrap().checkpoint, 1);
+ assert!(!fake.state.lock().unwrap().settled);
+ }
+}
+
+#[tokio::test(start_paused = true)]
+async fn killed_attempt_reclaims_checkpoint_with_new_fence() {
+ let fake = Fake::default();
+ fake.state.lock().unwrap().import_delay = Duration::from_secs(125);
+ let delivery = delivery();
+ assert!(
+ timeout(Duration::from_secs(10), attempt(&fake, &delivery, false))
+ .await
+ .is_err()
+ );
+ assert_eq!(fake.state.lock().unwrap().checkpoint, 1);
+ assert_eq!(
+ attempt(&fake, &delivery, false).await.unwrap(),
+ WorkerOutcome::Defer
+ );
+ // Simulate database-time expiry/reconciliation, not process-local unlocking.
+ fake.state.lock().unwrap().active = false;
+ assert_eq!(
+ attempt(&fake, &delivery, false).await.unwrap(),
+ WorkerOutcome::Acknowledge
+ );
+ assert_eq!(fake.state.lock().unwrap().generation, 2);
+}
+
+#[tokio::test(start_paused = true)]
+async fn exhausted_receives_and_dlq_require_durable_failure_and_defer_active_lease() {
+ for dlq in [false, true] {
+ let fake = Fake::default();
+ let mut delivery = delivery();
+ delivery.receives = 5;
+ fake.state.lock().unwrap().active = true;
+ assert_eq!(
+ attempt(&fake, &delivery, dlq).await.unwrap(),
+ WorkerOutcome::Defer
+ );
+ fake.state.lock().unwrap().active = false;
+ fake.state.lock().unwrap().dlq_error = true;
+ assert!(attempt(&fake, &delivery, dlq).await.is_err());
+ assert!(!fake.state.lock().unwrap().settled);
+ fake.state.lock().unwrap().dlq_error = false;
+ assert_eq!(
+ attempt(&fake, &delivery, dlq).await.unwrap(),
+ WorkerOutcome::Acknowledge
+ );
+ assert_eq!(fake.state.lock().unwrap().claims, 0);
+ }
+}
+
+#[tokio::test(start_paused = true)]
+async fn malformed_main_redrives_and_malformed_dlq_drops_without_domain_calls() {
+ let fake = Fake::default();
+ let bad = Delivery {
+ event: Err(ImportError::InvalidInput),
+ receives: 1,
+ };
+ assert_eq!(
+ attempt(&fake, &bad, false).await.unwrap(),
+ WorkerOutcome::Defer
+ );
+ assert_eq!(
+ attempt(&fake, &bad, true).await.unwrap(),
+ WorkerOutcome::Acknowledge
+ );
+ assert_eq!(fake.state.lock().unwrap().claims, 0);
+ assert_eq!(fake.state.lock().unwrap().dead_letters, 0);
+}
+
+#[tokio::test(start_paused = true)]
+async fn transient_errors_and_revoked_admin_never_ack() {
+ for claim in [false, true] {
+ let fake = Fake::default();
+ if claim {
+ fake.state.lock().unwrap().claim_error = Some(ImportError::Unavailable);
+ } else {
+ fake.state.lock().unwrap().import_error = Some(ImportError::Retryable);
+ }
+ assert!(attempt(&fake, &delivery(), false).await.is_err());
+ assert!(!fake.state.lock().unwrap().settled);
+ assert_eq!(fake.state.lock().unwrap().deletes, 0);
+ }
+}
+
+#[tokio::test(start_paused = true)]
+async fn delete_failure_is_safe_to_redeliver_after_durable_settlement() {
+ let fake = Fake::default();
+ fake.state.lock().unwrap().main.push_back(delivery());
+ fake.state.lock().unwrap().delete_error = true;
+ run(&fake, &fake, &fake, owner(), config(), fake.stop.clone()).await;
+ assert_eq!(fake.state.lock().unwrap().deletes, 1);
+ assert_eq!(
+ attempt(&fake, &delivery(), false).await.unwrap(),
+ WorkerOutcome::Acknowledge
+ );
+ assert_eq!(fake.state.lock().unwrap().imports, 1);
+}
+
+#[tokio::test(start_paused = true)]
+async fn disabled_mode_keeps_recovery_but_never_receives_main_or_publishes() {
+ let fake = Fake::default();
+ fake.state.lock().unwrap().main.push_back(delivery());
+ let stop = async {
+ sleep(Duration::from_secs(15)).await;
+ fake.stop.cancel();
+ };
+ tokio::join!(
+ run(
+ &fake,
+ &fake,
+ &fake,
+ owner(),
+ WorkerConfig::new(false, 1, 5).unwrap(),
+ fake.stop.clone()
+ ),
+ stop
+ );
+ let state = fake.state.lock().unwrap();
+ assert_eq!(state.claims, 0);
+ assert_eq!(state.main.len(), 1);
+ assert_eq!(state.publications, 0);
+ assert!(state.reconciliations > 0);
+}
+
+#[tokio::test(start_paused = true)]
+async fn slow_publisher_does_not_block_recovery_and_is_retried() {
+ let fake = Fake::default();
+ fake.state.lock().unwrap().publication_hangs = true;
+ let stop = async {
+ sleep(Duration::from_secs(75)).await;
+ fake.stop.cancel();
+ };
+ tokio::join!(
+ run(&fake, &fake, &fake, owner(), config(), fake.stop.clone()),
+ stop
+ );
+ let state = fake.state.lock().unwrap();
+ assert_eq!(state.publications, 2);
+ assert!(state.reconciliations >= 7);
+}
+
+#[tokio::test(start_paused = true)]
+async fn shutdown_during_batch_drains_or_leaves_recoverable_checkpoint() {
+ for completes in [false, true] {
+ let fake = Fake::default();
+ {
+ let mut state = fake.state.lock().unwrap();
+ state.import_delay = Duration::from_secs(if completes { 90 } else { 300 });
+ state.main.push_back(delivery());
+ state.main.push_back(delivery());
+ }
+ let stop = async {
+ fake.started.cancelled().await;
+ fake.stop.cancel();
+ };
+ tokio::join!(
+ run(&fake, &fake, &fake, owner(), config(), fake.stop.clone()),
+ stop
+ );
+ let state = fake.state.lock().unwrap();
+ assert_eq!(state.claims, 1);
+ assert_eq!(state.main.len(), 1);
+ assert_eq!(state.deletes, usize::from(completes));
+ assert_eq!(state.checkpoint, if completes { 2 } else { 1 });
+ assert!(state.heartbeats >= 1);
+ }
+}
diff --git a/crates/slack_integration/src/lib.rs b/crates/slack_integration/src/lib.rs
new file mode 100644
index 0000000000..78197c9027
--- /dev/null
+++ b/crates/slack_integration/src/lib.rs
@@ -0,0 +1,11 @@
+#![deny(missing_docs)]
+//! Browser-staged Slack archive imports, with transport-free lifecycle contracts.
+//!
+//! Models are always available. Enable `ports` for service and adapter contracts.
+//! Enable `postgres` for durable job and upload persistence.
+
+pub mod domain;
+#[cfg(any(feature = "inbound", feature = "worker"))]
+pub mod inbound;
+#[cfg(feature = "outbound")]
+pub mod outbound;
diff --git a/crates/slack_integration/src/outbound.rs b/crates/slack_integration/src/outbound.rs
new file mode 100644
index 0000000000..1c5539b723
--- /dev/null
+++ b/crates/slack_integration/src/outbound.rs
@@ -0,0 +1,25 @@
+//! External adapters for the import domain.
+
+#[cfg(all(
+ test,
+ any(
+ feature = "s3",
+ feature = "sqs",
+ feature = "gateway",
+ feature = "search"
+ )
+))]
+mod test_support;
+
+#[cfg(feature = "gateway")]
+pub mod gateway_notifier;
+#[cfg(feature = "ledger")]
+pub mod import_ledger;
+#[cfg(feature = "postgres")]
+pub mod pg_slack_import_repo;
+#[cfg(feature = "s3")]
+pub mod s3_storage;
+#[cfg(feature = "search")]
+pub mod search_backfill;
+#[cfg(feature = "sqs")]
+pub mod sqs_queue;
diff --git a/crates/slack_integration/src/outbound/gateway_notifier.rs b/crates/slack_integration/src/outbound/gateway_notifier.rs
new file mode 100644
index 0000000000..82f31139ef
--- /dev/null
+++ b/crates/slack_integration/src/outbound/gateway_notifier.rs
@@ -0,0 +1,128 @@
+//! Requester-only gateway invalidation. Polling remains authoritative; throttling
+//! is bounded and process-local, not a durable progress or delivery guarantee.
+
+use std::{
+ collections::HashMap,
+ sync::{Arc, Mutex},
+ time::{Duration, Instant},
+};
+
+use connection_gateway_client::ConnectionGatewayClient;
+use entity_access::domain::models::EntityType;
+use macro_user_id::user_id::MacroUserIdStr;
+use serde_json::Value;
+
+use crate::domain::{
+ models::*,
+ ports::{ImportNotifier, PortResult},
+};
+
+#[cfg(test)]
+mod test;
+
+/// Generic gateway event type consumed as a query invalidation by the browser.
+pub const SLACK_IMPORT_UPDATED: &str = "slack_import_updated";
+const THROTTLE_INTERVAL: Duration = Duration::from_secs(2);
+const RETENTION: Duration = Duration::from_secs(300);
+const MAX_TRACKED_JOBS: usize = 4096;
+
+/// Shared clones use one throttle. No source metadata, storage keys, conversation
+/// IDs, target IDs or counters are transmitted through this adapter.
+#[derive(Clone)]
+pub struct GatewayImportNotifier {
+ gateway: Arc<ConnectionGatewayClient>,
+ throttle: Arc<Mutex<Throttle>>,
+}
+
+impl GatewayImportNotifier {
+ /// Construct a requester-only notifier with a two-second progress throttle.
+ pub fn new(gateway: Arc<ConnectionGatewayClient>) -> Self {
+ Self {
+ gateway,
+ throttle: Arc::new(Mutex::new(Throttle::default())),
+ }
+ }
+}
+
+impl ImportNotifier for GatewayImportNotifier {
+ async fn invalidate(
+ &self,
+ requester: &MacroUserIdStr<'_>,
+ team: TeamId,
+ job: JobId,
+ revision: u64,
+ status: JobStatus,
+ ) -> PortResult<()> {
+ let send = self
+ .throttle
+ .lock()
+ .map_err(|_| ImportError::Internal)?
+ .admit(team, job, revision, status, Instant::now());
+ if !send {
+ return Ok(());
+ }
+ let entity = EntityType::User.with_entity_str(requester.as_ref());
+ self.gateway
+ .send_message(
+ entity,
+ SLACK_IMPORT_UPDATED.to_owned(),
+ payload(team, job, revision, status),
+ )
+ .await
+ .map_err(|error| rootcause::report!(error).context(ImportError::Retryable))?;
+ Ok(())
+ }
+}
+
+fn payload(team: TeamId, job: JobId, revision: u64, status: JobStatus) -> Value {
+ // Supply an OBJECT to send_message. The gateway serializes it into the
+ // websocket envelope's JSON-string data field; pre-encoding double-quotes it.
+ serde_json::json!({ "jobId": job, "teamId": team, "revision": revision, "status": status })
+}
+
+#[derive(Default)]
+struct Throttle {
+ sent: HashMap<(TeamId, JobId), Sent>,
+}
+
+struct Sent {
+ at: Instant,
+ revision: u64,
+ status: JobStatus,
+}
+
+impl Throttle {
+ fn admit(
+ &mut self,
+ team: TeamId,
+ job: JobId,
+ revision: u64,
+ status: JobStatus,
+ now: Instant,
+ ) -> bool {
+ self.sent
+ .retain(|_, sent| now.duration_since(sent.at) < RETENTION);
+ if let Some(sent) = self.sent.get(&(team, job)) {
+ if revision <= sent.revision
+ || (status == sent.status && now.duration_since(sent.at) < THROTTLE_INTERVAL)
+ {
+ return false;
+ }
+ } else if self.sent.len() >= MAX_TRACKED_JOBS {
+ // A saturated process falls back to polling instead of growing an
+ // unbounded cache or producing a burst of unthrottled notifications.
+ return false;
+ }
+ // Reserve before I/O, without holding the lock across await. A failed
+ // attempt is throttled too; later updates/polling recover lost hints.
+ self.sent.insert(
+ (team, job),
+ Sent {
+ at: now,
+ revision,
+ status,
+ },
+ );
+ true
+ }
+}
diff --git a/crates/slack_integration/src/outbound/gateway_notifier/test.rs b/crates/slack_integration/src/outbound/gateway_notifier/test.rs
new file mode 100644
index 0000000000..019e468978
--- /dev/null
+++ b/crates/slack_integration/src/outbound/gateway_notifier/test.rs
@@ -0,0 +1,144 @@
+use super::*;
+use crate::outbound::test_support::mock_http;
+use connection_gateway_client::models::SendMessageBody;
+use uuid::Uuid;
+
+fn team() -> TeamId {
+ Uuid::from_u128(1).try_into().unwrap()
+}
+fn job() -> JobId {
+ Uuid::from_u128(2).try_into().unwrap()
+}
+
+#[test]
+fn throttle_deduplicates_versions_and_allows_status_transitions() {
+ let mut throttle = Throttle::default();
+ let now = Instant::now();
+ assert!(throttle.admit(team(), job(), 1, JobStatus::Uploading, now));
+ assert!(!throttle.admit(
+ team(),
+ job(),
+ 1,
+ JobStatus::Uploading,
+ now + THROTTLE_INTERVAL
+ ));
+ assert!(!throttle.admit(team(), job(), 2, JobStatus::Uploading, now));
+ assert!(throttle.admit(
+ team(),
+ job(),
+ 2,
+ JobStatus::Uploading,
+ now + THROTTLE_INTERVAL
+ ));
+ assert!(!throttle.admit(
+ team(),
+ job(),
+ 1,
+ JobStatus::Uploading,
+ now + THROTTLE_INTERVAL
+ ));
+ assert!(throttle.admit(
+ team(),
+ job(),
+ 3,
+ JobStatus::Completed,
+ now + THROTTLE_INTERVAL
+ ));
+ assert!(!throttle.admit(
+ team(),
+ job(),
+ 3,
+ JobStatus::Completed,
+ now + THROTTLE_INTERVAL
+ ));
+}
+
+#[test]
+fn throttle_memory_is_bounded_and_entries_expire() {
+ let mut throttle = Throttle::default();
+ let now = Instant::now();
+ for id in 1..=MAX_TRACKED_JOBS {
+ assert!(throttle.admit(
+ team(),
+ Uuid::from_u128(id as u128).try_into().unwrap(),
+ 0,
+ JobStatus::Uploading,
+ now
+ ));
+ }
+ let extra = Uuid::from_u128(MAX_TRACKED_JOBS as u128 + 1)
+ .try_into()
+ .unwrap();
+ assert!(!throttle.admit(team(), extra, 0, JobStatus::Uploading, now));
+ assert_eq!(throttle.sent.len(), MAX_TRACKED_JOBS);
+ assert!(throttle.admit(team(), extra, 1, JobStatus::Uploading, now + RETENTION));
+ assert_eq!(throttle.sent.len(), 1);
+}
+
+#[tokio::test]
+async fn gateway_uses_requester_entity_and_json_object_for_string_framing() {
+ let body = r#"{"receipts":[]}"#;
+ let response = format!(
+ "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}",
+ body.len()
+ );
+ let (endpoint, requests) = mock_http(vec![response]);
+ let notifier = GatewayImportNotifier::new(Arc::new(ConnectionGatewayClient::new(
+ "test".into(),
+ endpoint,
+ )));
+ let requester = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();
+ notifier
+ .invalidate(&requester, team(), job(), 9, JobStatus::Processing)
+ .await
+ .unwrap();
+ // Clones share throttling, including duplicate revisions.
+ notifier
+ .clone()
+ .invalidate(&requester, team(), job(), 9, JobStatus::Processing)
+ .await
+ .unwrap();
+ let request = requests.recv_timeout(Duration::from_secs(2)).unwrap();
+ let (headers, body) = request.split_once("\r\n\r\n").unwrap();
+ assert!(headers.starts_with("POST /message/send/user/macro|[email protected] HTTP/1.1"));
+ let body: SendMessageBody = serde_json::from_str(body).unwrap();
+ assert_eq!(body.message_type, "slack_import_updated");
+ assert_eq!(
+ body.message,
+ serde_json::json!({ "jobId": job(), "teamId": team(), "revision": 9, "status": "processing" })
+ );
+ assert!(body.message.is_object());
+ // services/connection_gateway/src/api/message.rs calls message.to_string()
+ // before constructing its websocket Message. The browser JSON.parse(data)
+ // must yield the object, not a second encoded JSON string.
+ let websocket_data = body.message.to_string();
+ assert_eq!(
+ serde_json::from_str::<Value>(&websocket_data).unwrap(),
+ body.message
+ );
+}
+
+#[tokio::test]
+async fn gateway_failure_is_retryable_and_attempts_are_throttled() {
+ let (endpoint, requests) = mock_http(vec![
+ "HTTP/1.1 503 Service Unavailable\r\nContent-Length: 0\r\nConnection: close\r\n\r\n".into(),
+ ]);
+ let notifier = GatewayImportNotifier::new(Arc::new(ConnectionGatewayClient::new(
+ "test".into(),
+ endpoint,
+ )));
+ let requester = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();
+ assert_eq!(
+ notifier
+ .invalidate(&requester, team(), job(), 1, JobStatus::Uploading)
+ .await
+ .unwrap_err()
+ .into_current_context(),
+ ImportError::Retryable
+ );
+ notifier
+ .invalidate(&requester, team(), job(), 2, JobStatus::Uploading)
+ .await
+ .unwrap();
+ assert!(requests.recv_timeout(Duration::from_secs(2)).is_ok());
+}
diff --git a/crates/slack_integration/src/outbound/import_ledger.rs b/crates/slack_integration/src/outbound/import_ledger.rs
new file mode 100644
index 0000000000..2649d0ff00
--- /dev/null
+++ b/crates/slack_integration/src/outbound/import_ledger.rs
@@ -0,0 +1,181 @@
+//! Translation to the import domain's canonical ledger port. No concrete
+//! repository construction or cross-domain outbound imports belong here.
+
+use import::domain::{models as ledger, ports::CanonicalImportRepo};
+use macro_user_id::user_id::MacroUserIdStr;
+use uuid::Uuid;
+
+use crate::domain::{
+ models::*,
+ ports::{ImportLedger, PortResult},
+};
+
+#[cfg(test)]
+mod test;
+
+/// Archive adapter over the owning import domain's narrow canonical port.
+#[derive(Clone)]
+pub struct CanonicalImportLedger<R> {
+ repo: R,
+}
+
+impl<R> CanonicalImportLedger<R> {
+ /// Wrap a canonical port supplied by the composition root.
+ pub fn new(repo: R) -> Self {
+ Self { repo }
+ }
+}
+
+impl<R: CanonicalImportRepo> ImportLedger for CanonicalImportLedger<R> {
+ async fn source_binding(&self, team: TeamId) -> PortResult<SourceBinding> {
+ match self
+ .repo
+ .source_binding(team.into())
+ .await
+ .map_err(map_error)?
+ {
+ None => Ok(SourceBinding::Unbound),
+ Some(binding) => match binding.workspace_id {
+ Some(id) => Ok(SourceBinding::Known {
+ source_id: id.as_str().parse().map_err(|_| ImportError::Internal)?,
+ }),
+ None => Ok(SourceBinding::ConfirmedUnknown),
+ },
+ }
+ }
+
+ async fn reserve(
+ &self,
+ team: TeamId,
+ requester: &MacroUserIdStr<'static>,
+ metadata: &ConversationMetadata,
+ authorized_existing_target: Option<Uuid>,
+ ) -> PortResult<TargetReservation> {
+ let key = target_key(team, &metadata.slack_channel_id)?;
+ let reservation = self
+ .repo
+ .reserve_target(
+ requester,
+ &key,
+ target_kind(metadata.kind),
+ authorized_existing_target,
+ )
+ .await
+ .map_err(map_error)?;
+ Ok(TargetReservation {
+ team_id: team,
+ slack_channel_id: metadata.slack_channel_id.clone(),
+ channel_id: reservation.channel_id,
+ status: if reservation.ready {
+ ReservationStatus::Ready
+ } else {
+ ReservationStatus::Pending
+ },
+ })
+ }
+
+ async fn complete(
+ &self,
+ reservation: &TargetReservation,
+ kind: ConversationKind,
+ ) -> PortResult<()> {
+ self.repo
+ .complete_target(
+ &target_key(reservation.team_id, &reservation.slack_channel_id)?,
+ reservation.channel_id,
+ target_kind(kind),
+ )
+ .await
+ .map_err(map_error)?;
+ Ok(())
+ }
+}
+
+impl<R: CanonicalImportRepo + import::domain::ports::ImportTargetReader> CanonicalImportLedger<R> {
+ /// Read exact targets without reservation, while fencing against a changed
+ /// binding. Returned labels are internal facts until domain disclosure checks.
+ pub async fn reference_channels(
+ &self,
+ team: TeamId,
+ expected: &SourceBinding,
+ channels: &[ConversationId],
+ ) -> PortResult<Vec<crate::domain::slack::references::resolve::ChannelMapping>> {
+ use crate::domain::slack::references::resolve::ChannelMapping;
+ let Some(binding) = self
+ .repo
+ .source_binding(team.into())
+ .await
+ .map_err(map_error)?
+ else {
+ return Ok(vec![ChannelMapping::Missing; channels.len()]);
+ };
+ let compatible = match (expected, binding.workspace_id.as_ref()) {
+ (SourceBinding::Known { source_id }, Some(bound)) => {
+ source_id.as_str() == bound.as_str()
+ }
+ (SourceBinding::ConfirmedUnknown, None) => binding.confirmed_unknown_at.is_some(),
+ _ => false,
+ };
+ if !compatible {
+ return Ok(vec![ChannelMapping::Missing; channels.len()]);
+ }
+ let ids = channels
+ .iter()
+ .map(|id| {
+ ledger::SlackConversationId::new(id.as_str()).ok_or(ImportError::InvalidInput)
+ })
+ .collect::<Result<Vec<_>, _>>()?;
+ Ok(self
+ .repo
+ .lookup_targets(team.into(), &binding, &ids)
+ .await
+ .map_err(map_error)?
+ .into_iter()
+ .map(|row| match row {
+ ledger::ImportTargetLookup::Missing => ChannelMapping::Missing,
+ ledger::ImportTargetLookup::Pending => ChannelMapping::Pending,
+ ledger::ImportTargetLookup::Ready {
+ channel_id,
+ kind,
+ name,
+ } => ChannelMapping::Ready {
+ id: channel_id,
+ name,
+ team_visible: kind == ledger::ImportTargetKind::Team,
+ },
+ })
+ .collect())
+ }
+}
+
+fn target_key(team: TeamId, conversation: &ConversationId) -> PortResult<ledger::ImportTargetKey> {
+ Ok(ledger::ImportTargetKey {
+ team_id: team.into(),
+ foreign_id: ledger::SlackConversationId::new(conversation.as_str())
+ .ok_or(ImportError::InvalidInput)?,
+ })
+}
+
+fn target_kind(kind: ConversationKind) -> ledger::ImportTargetKind {
+ match kind {
+ ConversationKind::PublicChannel => ledger::ImportTargetKind::Team,
+ ConversationKind::PrivateChannel | ConversationKind::GroupDirectMessage => {
+ ledger::ImportTargetKind::Private
+ }
+ ConversationKind::DirectMessage => ledger::ImportTargetKind::DirectMessage,
+ }
+}
+
+fn map_error(error: import::domain::ports::ImportError) -> rootcause::Report<ImportError> {
+ use import::domain::ports::ImportError as LedgerError;
+ let code = match &error {
+ LedgerError::SourceMismatch => ImportError::SourceMismatch,
+ LedgerError::SourceConfirmationRequired => ImportError::InvalidInput,
+ LedgerError::TargetConflict | LedgerError::TargetNotReserved => ImportError::Conflict,
+ LedgerError::Db(_) | LedgerError::Other(_) => ImportError::Retryable,
+ // Canonical ledger operations do not perform AI work. An admission
+ // error here is an unexpected adapter failure, not permission to proceed.
+ LedgerError::Admission(_) | LedgerError::Metadata(_) => ImportError::Internal,
+ };
+ rootcause::Report::new(error).context(code)
+}
diff --git a/crates/slack_integration/src/outbound/import_ledger/test.rs b/crates/slack_integration/src/outbound/import_ledger/test.rs
new file mode 100644
index 0000000000..c15976f024
--- /dev/null
+++ b/crates/slack_integration/src/outbound/import_ledger/test.rs
@@ -0,0 +1,186 @@
+use super::*;
+use import::domain::ports::{ImportError as LedgerError, Result as LedgerResult};
+use macro_user_id::cowlike::CowLike;
+
+struct Canonical {
+ binding: Option<ledger::ImportSourceBinding>,
+ kind: ledger::ImportTargetKind,
+ conflict: bool,
+}
+
+fn team() -> TeamId {
+ Uuid::from_u128(1).try_into().unwrap()
+}
+fn target() -> Uuid {
+ Uuid::from_u128(2)
+}
+fn metadata(kind: ConversationKind) -> ConversationMetadata {
+ ConversationMetadata {
+ slack_channel_id: "G123".parse().unwrap(),
+ kind,
+ name: "private".into(),
+ folder: "private".parse().unwrap(),
+ member_ids: vec![],
+ creator_id: None,
+ created_at: None,
+ archived: false,
+ message_count: None,
+ }
+}
+
+impl CanonicalImportRepo for Canonical {
+ async fn source_binding(
+ &self,
+ team_id: Uuid,
+ ) -> LedgerResult<Option<ledger::ImportSourceBinding>> {
+ assert_eq!(team_id, Uuid::from(team()));
+ Ok(self.binding.clone())
+ }
+ async fn bind_source(
+ &self,
+ _: Uuid,
+ _: Option<&ledger::SlackWorkspaceId>,
+ _: bool,
+ ) -> LedgerResult<ledger::ImportSourceBinding> {
+ panic!("source binding must commit with job creation, not in this adapter")
+ }
+ async fn reserve_target(
+ &self,
+ user: &MacroUserIdStr<'static>,
+ key: &ledger::ImportTargetKey,
+ kind: ledger::ImportTargetKind,
+ existing: Option<Uuid>,
+ ) -> LedgerResult<ledger::ImportTargetReservation> {
+ assert_eq!(user.as_ref(), "macro|[email protected]");
+ assert_eq!(key.team_id, Uuid::from(team()));
+ assert_eq!(key.foreign_id.as_str(), "G123");
+ assert_eq!(kind, self.kind);
+ assert_eq!(existing, Some(target()));
+ if self.conflict {
+ return Err(LedgerError::TargetConflict);
+ }
+ Ok(ledger::ImportTargetReservation {
+ key: key.clone(),
+ channel_id: target(),
+ ready: false,
+ })
+ }
+ async fn complete_target(
+ &self,
+ key: &ledger::ImportTargetKey,
+ channel_id: Uuid,
+ kind: ledger::ImportTargetKind,
+ ) -> LedgerResult<ledger::ImportTargetReservation> {
+ assert_eq!(key.team_id, Uuid::from(team()));
+ assert_eq!(key.foreign_id.as_str(), "G123");
+ assert_eq!(channel_id, target());
+ assert_eq!(kind, self.kind);
+ Ok(ledger::ImportTargetReservation {
+ key: key.clone(),
+ channel_id,
+ ready: true,
+ })
+ }
+}
+
+#[tokio::test]
+async fn translates_binding_without_exposing_ledger_internals() {
+ for (binding, expected) in [
+ (None, SourceBinding::Unbound),
+ (
+ Some(ledger::ImportSourceBinding {
+ workspace_id: None,
+ confirmed_unknown_at: Some(chrono::Utc::now()),
+ }),
+ SourceBinding::ConfirmedUnknown,
+ ),
+ (
+ Some(ledger::ImportSourceBinding {
+ workspace_id: ledger::SlackWorkspaceId::new("T123"),
+ confirmed_unknown_at: None,
+ }),
+ SourceBinding::Known {
+ source_id: "T123".parse().unwrap(),
+ },
+ ),
+ ] {
+ let adapter = CanonicalImportLedger::new(Canonical {
+ binding,
+ kind: ledger::ImportTargetKind::Team,
+ conflict: false,
+ });
+ assert_eq!(adapter.source_binding(team()).await.unwrap(), expected);
+ }
+}
+
+#[tokio::test]
+async fn reservation_passes_requester_explicit_team_and_authorized_target_for_every_kind() {
+ let requester = MacroUserIdStr::parse_from_str("macro|[email protected]")
+ .unwrap()
+ .into_owned();
+ for (source_kind, kind) in [
+ (
+ ConversationKind::PublicChannel,
+ ledger::ImportTargetKind::Team,
+ ),
+ (
+ ConversationKind::PrivateChannel,
+ ledger::ImportTargetKind::Private,
+ ),
+ (
+ ConversationKind::GroupDirectMessage,
+ ledger::ImportTargetKind::Private,
+ ),
+ (
+ ConversationKind::DirectMessage,
+ ledger::ImportTargetKind::DirectMessage,
+ ),
+ ] {
+ let adapter = CanonicalImportLedger::new(Canonical {
+ binding: None,
+ kind,
+ conflict: false,
+ });
+ let reservation = adapter
+ .reserve(team(), &requester, &metadata(source_kind), Some(target()))
+ .await
+ .unwrap();
+ assert_eq!(reservation.channel_id, target());
+ assert_eq!(reservation.status, ReservationStatus::Pending);
+ adapter.complete(&reservation, source_kind).await.unwrap();
+ }
+}
+
+#[test]
+fn unexpected_ai_admission_error_fails_closed() {
+ let admission = serde_json::from_str(r#""unavailable""#).unwrap();
+ assert_eq!(
+ map_error(LedgerError::Admission(admission)).into_current_context(),
+ ImportError::Internal
+ );
+}
+
+#[tokio::test]
+async fn ambiguous_mapping_fails_closed_without_target_disclosure() {
+ let requester = MacroUserIdStr::parse_from_str("macro|[email protected]")
+ .unwrap()
+ .into_owned();
+ let adapter = CanonicalImportLedger::new(Canonical {
+ binding: None,
+ kind: ledger::ImportTargetKind::Private,
+ conflict: true,
+ });
+ assert_eq!(
+ adapter
+ .reserve(
+ team(),
+ &requester,
+ &metadata(ConversationKind::PrivateChannel),
+ Some(target())
+ )
+ .await
+ .unwrap_err()
+ .into_current_context(),
+ ImportError::Conflict
+ );
+}
diff --git a/crates/slack_integration/src/outbound/pg_slack_import_repo.rs b/crates/slack_integration/src/outbound/pg_slack_import_repo.rs
new file mode 100644
index 0000000000..0d549deb70
--- /dev/null
+++ b/crates/slack_integration/src/outbound/pg_slack_import_repo.rs
@@ -0,0 +1,399 @@
+//! PostgreSQL job and manifest persistence. Mutations lock the team-owned job
+//! before touching conversations, uploads or outbox rows. Lifecycle operations
+//! must use this same lock order.
+
+use std::collections::{HashMap, HashSet};
+
+use chrono::{DateTime, Utc};
+use macro_user_id::user_id::MacroUserIdStr;
+use serde::de::DeserializeOwned;
+use sha2::{Digest, Sha256};
+use sqlx::{PgConnection, PgPool};
+use uuid::Uuid;
+
+use crate::domain::{models::*, ports::PortResult};
+
+/// Transaction-scoped import mappings, checkpoints and dirty-search persistence.
+pub mod batches;
+mod lifecycle;
+/// Job-scoped body-reference templates and atomic reconciliation checkpoints.
+pub mod references;
+mod uploads;
+
+/// Durable Slack archive repository. Administrator/target authorization remains
+/// in the domain service; every repository lookup additionally checks team ownership.
+#[derive(Clone)]
+pub struct PgSlackImportRepo {
+ pool: PgPool,
+ limits: ImportLimits,
+}
+
+impl PgSlackImportRepo {
+ /// Use the composition root's effective server limits for validation and receipts.
+ pub fn new(pool: PgPool, limits: ImportLimits) -> Self {
+ Self { pool, limits }
+ }
+
+ /// Atomically bind the source and persist the complete semantic create payload.
+ /// Reordered conversations/members are equivalent; changed metadata conflicts.
+ pub async fn create(
+ &self,
+ team: TeamId,
+ admin: &MacroUserIdStr<'_>,
+ command: &CreateImport,
+ limits: &ImportLimits,
+ ) -> PortResult<ImportProgress> {
+ if limits != &self.limits {
+ return Err(ImportError::InvalidInput.into());
+ }
+ let canonical = canonical_create(command, limits)?;
+ let hash = format!(
+ "{:x}",
+ Sha256::digest(serde_json::to_vec(&canonical).map_err(internal)?)
+ );
+ let mut tx = self.pool.begin().await.map_err(internal)?;
+ let id = Uuid::now_v7();
+ let source = match &command.source {
+ SourceIdentity::Known { source_id } => Some(source_id.as_str()),
+ SourceIdentity::ConfirmedUnknown => None,
+ };
+ // The unique scoped token serializes concurrent creates, including a lost
+ // response retry. DO NOTHING never rewrites historical request metadata.
+ let inserted = sqlx::query_scalar!(
+ r#"INSERT INTO slack_import_job
+ (id, team_id, user_id, idempotency_token, request_sha256,
+ source_workspace_id, confirmed_unknown, include_message_history)
+ VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
+ ON CONFLICT (team_id, user_id, idempotency_token) DO NOTHING
+ RETURNING id"#,
+ id,
+ Uuid::from(team),
+ admin.as_ref(),
+ Uuid::from(command.idempotency_token),
+ hash,
+ source,
+ source.is_none(),
+ command.include_message_history,
+ )
+ .fetch_optional(&mut *tx)
+ .await
+ .map_err(internal)?;
+ let job = match inserted {
+ Some(id) => {
+ // This owning-crate transaction API is shared with onboarding;
+ // source provenance and the job must roll back together.
+ let workspace = source.and_then(import::domain::models::SlackWorkspaceId::new);
+ import::source_binding::bind_source(
+ &mut *tx,
+ team.into(),
+ workspace.as_ref(),
+ source.is_none(),
+ )
+ .await
+ .map_err(|error| {
+ let code = match error {
+ import::domain::ports::ImportError::SourceMismatch => {
+ ImportError::SourceMismatch
+ }
+ _ => ImportError::Internal,
+ };
+ rootcause::Report::new(error).context(code)
+ })?;
+ let metadata = serde_json::Value::Array(canonical.conversations.iter().map(|conversation| {
+ serde_json::json!({
+ "slack_channel_id": conversation.slack_channel_id,
+ "kind": conversation.kind,
+ "name": conversation.name,
+ "folder": conversation.folder,
+ "member_ids": conversation.member_ids,
+ "creator_id": conversation.creator_id,
+ "source_created_at": conversation.created_at.map(SlackTimestamp::unix_micros),
+ "archived": conversation.archived,
+ "message_count": conversation.message_count,
+ })
+ }).collect());
+ sqlx::query!(
+ r#"INSERT INTO slack_import_conversation
+ (job_id, slack_channel_id, kind, name, folder, member_ids,
+ creator_id, source_created_at, archived, message_count)
+ SELECT j.id, m.slack_channel_id, m.kind, m.name, m.folder, m.member_ids,
+ m.creator_id, m.source_created_at, m.archived, m.message_count
+ FROM slack_import_job j CROSS JOIN jsonb_to_recordset($3) AS m(
+ slack_channel_id text, kind text, name text, folder text,
+ member_ids text[], creator_id text, source_created_at bigint,
+ archived boolean, message_count bigint)
+ WHERE j.team_id = $1 AND j.id = $2
+ ON CONFLICT (job_id, slack_channel_id) DO NOTHING"#,
+ Uuid::from(team),
+ id,
+ metadata,
+ )
+ .execute(&mut *tx)
+ .await
+ .map_err(internal)?;
+ id
+ }
+ None => {
+ let existing = sqlx::query!(
+ r#"SELECT id, request_sha256 FROM slack_import_job
+ WHERE team_id = $1 AND user_id = $2 AND idempotency_token = $3
+ FOR UPDATE"#,
+ Uuid::from(team),
+ admin.as_ref(),
+ Uuid::from(command.idempotency_token),
+ )
+ .fetch_one(&mut *tx)
+ .await
+ .map_err(internal)?;
+ if existing.request_sha256 != hash {
+ return Err(ImportError::Conflict.into());
+ }
+ existing.id
+ }
+ };
+ let progress = self
+ .receipt(&mut tx, team, job.try_into().map_err(internal)?)
+ .await?;
+ tx.commit().await.map_err(internal)?;
+ Ok(progress)
+ }
+
+ /// Return a consistent team-owned receipt or None for an inaccessible job.
+ pub async fn progress(&self, team: TeamId, job: JobId) -> PortResult<Option<ImportProgress>> {
+ let mut tx = self.pool.begin().await.map_err(internal)?;
+ let progress = self.receipts(&mut tx, team, Some(job), None).await?.pop();
+ tx.commit().await.map_err(internal)?;
+ Ok(progress)
+ }
+
+ /// Return at most 50 receipts in descending UUIDv7 order, exclusively before the cursor.
+ pub async fn list(
+ &self,
+ team: TeamId,
+ before: Option<JobId>,
+ ) -> PortResult<Vec<ImportProgress>> {
+ let mut tx = self.pool.begin().await.map_err(internal)?;
+ let progress = self.receipts(&mut tx, team, None, before).await?;
+ tx.commit().await.map_err(internal)?;
+ Ok(progress)
+ }
+
+ async fn receipt(
+ &self,
+ connection: &mut PgConnection,
+ team: TeamId,
+ job: JobId,
+ ) -> PortResult<ImportProgress> {
+ self.receipts(connection, team, Some(job), None)
+ .await?
+ .pop()
+ .ok_or_else(|| ImportError::Unavailable.into())
+ }
+
+ async fn receipts(
+ &self,
+ connection: &mut PgConnection,
+ team: TeamId,
+ job: Option<JobId>,
+ before: Option<JobId>,
+ ) -> PortResult<Vec<ImportProgress>> {
+ // Lock jobs before reading any child rows. At READ COMMITTED a locking
+ // SELECT can see a newer job after waiting; child subqueries in that same
+ // statement would still use its older snapshot. Hydrate in bounded batches
+ // only after acquiring the locks, never once per receipt.
+ let rows = sqlx::query!(
+ r#"SELECT j.id, j.status, j.revision, j.created_at, j.updated_at,
+ j.registration_closed_at, j.source_workspace_id, j.include_message_history
+ FROM slack_import_job j
+ WHERE j.team_id = $1 AND ($2::uuid IS NULL OR j.id = $2)
+ AND ($3::uuid IS NULL OR j.id < $3)
+ ORDER BY j.id DESC LIMIT 50 FOR SHARE OF j"#,
+ Uuid::from(team),
+ job.map(Uuid::from),
+ before.map(Uuid::from),
+ )
+ .fetch_all(&mut *connection)
+ .await
+ .map_err(internal)?;
+ let ids: Vec<Uuid> = rows.iter().map(|row| row.id).collect();
+ let users_verified: HashSet<Uuid> = sqlx::query_scalar!(
+ r#"SELECT u.job_id FROM slack_import_upload u
+ JOIN slack_import_job j ON j.id = u.job_id
+ WHERE j.team_id = $1 AND j.id = ANY($2)
+ AND u.slack_channel_id IS NULL AND u.verified_at IS NOT NULL"#,
+ Uuid::from(team),
+ &ids,
+ )
+ .fetch_all(&mut *connection)
+ .await
+ .map_err(internal)?
+ .into_iter()
+ .collect();
+ let conversations = sqlx::query!(
+ r#"SELECT c.job_id, c.slack_channel_id, c.name, c.kind, c.archived, c.status, c.channel_id, c.part_count,
+ c.processed, c.imported, c.duplicates, c.skipped, c.reactions,
+ c.search_state, c.search_receipt_id, c.last_error, c.warnings,
+ (SELECT count(*) FROM slack_import_upload u
+ WHERE u.job_id = c.job_id AND u.slack_channel_id = c.slack_channel_id
+ AND u.verified_at IS NOT NULL) AS "verified_parts!"
+ FROM slack_import_conversation c JOIN slack_import_job j ON j.id = c.job_id
+ WHERE j.team_id = $1 AND j.id = ANY($2)
+ ORDER BY c.job_id DESC, c.slack_channel_id"#,
+ Uuid::from(team),
+ &ids,
+ )
+ .fetch_all(&mut *connection)
+ .await
+ .map_err(internal)?;
+ let mut by_job: HashMap<Uuid, Vec<ConversationProgress>> = HashMap::new();
+ for row in conversations {
+ let search = match row.search_state.as_str() {
+ "not_needed" => SearchState::NotNeeded,
+ "pending" => SearchState::Pending,
+ "submitted" => SearchState::Submitted {
+ receipt_id: row.search_receipt_id.ok_or(ImportError::Internal)?,
+ },
+ "completed" => SearchState::Completed,
+ "failed" => SearchState::Failed,
+ _ => return Err(ImportError::Internal.into()),
+ };
+ let warnings = row
+ .warnings
+ .iter()
+ .map(|v| parse_enum(v))
+ .collect::<PortResult<Vec<ImportWarning>>>()?;
+ let channel_id = if warnings.contains(&ImportWarning::TargetUnavailable) {
+ None
+ } else {
+ row.channel_id
+ };
+ by_job
+ .entry(row.job_id)
+ .or_default()
+ .push(ConversationProgress {
+ slack_channel_id: row.slack_channel_id.parse().map_err(internal)?,
+ name: row.name,
+ kind: parse_enum(&row.kind)?,
+ archived: row.archived,
+ status: parse_enum(&row.status)?,
+ channel_id,
+ part_count: row.part_count.map(|n| n as u32),
+ verified_parts: row.verified_parts.try_into().map_err(internal)?,
+ counters: ImportCounters {
+ processed: row.processed as u64,
+ imported: row.imported as u64,
+ duplicates: row.duplicates as u64,
+ skipped: row.skipped as u64,
+ reactions: row.reactions as u64,
+ },
+ search,
+ error: row.last_error.as_deref().map(parse_enum).transpose()?,
+ warnings,
+ });
+ }
+ rows.into_iter()
+ .map(|row| {
+ Ok(ImportProgress {
+ source: match row.source_workspace_id {
+ Some(source) => SourceIdentity::Known {
+ source_id: source.parse().map_err(internal)?,
+ },
+ None => SourceIdentity::ConfirmedUnknown,
+ },
+ include_message_history: row.include_message_history,
+ job_id: row.id.try_into().map_err(internal)?,
+ status: parse_enum(&row.status)?,
+ revision: row.revision as u64,
+ created_at: row.created_at,
+ updated_at: row.updated_at,
+ registration_closed_at: row.registration_closed_at,
+ users_verified: users_verified.contains(&row.id),
+ limits: self.limits,
+ conversations: by_job.remove(&row.id).unwrap_or_default(),
+ })
+ })
+ .collect()
+ }
+}
+
+struct LockedJob {
+ status: String,
+ registration_closed_at: Option<DateTime<Utc>>,
+ include_message_history: bool,
+ registered_bytes: i64,
+ expired: bool,
+}
+
+impl LockedJob {
+ fn require_open(&self) -> PortResult<()> {
+ if self.status != "uploading" || self.registration_closed_at.is_some() || self.expired {
+ return Err(ImportError::Conflict.into());
+ }
+ Ok(())
+ }
+}
+
+async fn lock_job(
+ connection: &mut PgConnection,
+ team: TeamId,
+ job: JobId,
+) -> PortResult<LockedJob> {
+ sqlx::query_as!(
+ LockedJob,
+ r#"SELECT status, registration_closed_at, include_message_history, registered_bytes,
+ (staging_expires_at <= clock_timestamp()) AS "expired!"
+ FROM slack_import_job WHERE team_id = $1 AND id = $2 FOR UPDATE"#,
+ Uuid::from(team),
+ Uuid::from(job),
+ )
+ .fetch_optional(connection)
+ .await
+ .map_err(internal)?
+ .ok_or_else(|| ImportError::Unavailable.into())
+}
+
+async fn bump_revision(connection: &mut PgConnection, team: TeamId, job: JobId) -> PortResult<()> {
+ sqlx::query!(
+ "UPDATE slack_import_job SET revision = revision + 1, updated_at = clock_timestamp() WHERE team_id = $1 AND id = $2",
+ Uuid::from(team), Uuid::from(job),
+ ).execute(connection).await.map_err(internal)?;
+ Ok(())
+}
+
+fn canonical_create(command: &CreateImport, limits: &ImportLimits) -> PortResult<CreateImport> {
+ if command.conversations.len() > limits.conversations as usize {
+ return Err(ImportError::LimitExceeded.into());
+ }
+ let mut canonical = command.clone();
+ let mut ids = HashSet::new();
+ for conversation in &mut canonical.conversations {
+ if !ids.insert(conversation.slack_channel_id.clone()) {
+ return Err(ImportError::InvalidInput.into());
+ }
+ if conversation
+ .message_count
+ .is_some_and(|n| n > i64::MAX as u64)
+ {
+ return Err(ImportError::LimitExceeded.into());
+ }
+ conversation.member_ids.sort_unstable();
+ conversation.member_ids.dedup();
+ }
+ canonical
+ .conversations
+ .sort_unstable_by(|a, b| a.slack_channel_id.cmp(&b.slack_channel_id));
+ if serde_json::to_vec(&canonical).map_err(internal)?.len() as u64 > limits.json_bytes {
+ return Err(ImportError::LimitExceeded.into());
+ }
+ Ok(canonical)
+}
+
+fn parse_enum<T: DeserializeOwned>(value: &str) -> PortResult<T> {
+ serde_json::from_value(serde_json::Value::String(value.to_owned())).map_err(internal)
+}
+
+fn internal(
+ error: impl std::error::Error + Send + Sync + 'static,
+) -> rootcause::Report<ImportError> {
+ rootcause::Report::new(error).context(ImportError::Internal)
+}
diff --git a/crates/slack_integration/src/outbound/pg_slack_import_repo/batches.rs b/crates/slack_integration/src/outbound/pg_slack_import_repo/batches.rs
new file mode 100644
index 0000000000..37cb20f2fa
--- /dev/null
+++ b/crates/slack_integration/src/outbound/pg_slack_import_repo/batches.rs
@@ -0,0 +1,613 @@
+//! Import-owned helpers for the worker composition root's shared transaction.
+//! No helper writes comms tables. The caller must roll back the entire transaction
+//! on any error, including a failed final fence check, and commit only after finish.
+
+use macro_user_id::cowlike::CowLike;
+use sqlx::{Postgres, Transaction};
+
+use super::{
+ lifecycle::{fence, number},
+ *,
+};
+
+#[cfg(test)]
+mod test;
+
+/// Trusted persisted context read under the execution fence.
+pub struct WriteContext {
+ /// Namespace owning the job.
+ pub team: TeamId,
+ /// Persisted administrator, never supplied by a queue message.
+ pub requester: MacroUserIdStr<'static>,
+ /// Persisted source kind.
+ pub kind: ConversationKind,
+ /// Previously authorized target, if bound.
+ pub channel: Option<Uuid>,
+ /// Current committed restart point.
+ pub checkpoint: Checkpoint,
+}
+
+/// Fence before any owning-crate writes and load their trusted context.
+pub async fn write_context(
+ tx: &mut Transaction<'_, Postgres>,
+ lease: &Lease,
+) -> PortResult<WriteContext> {
+ let team = fence(tx, lease).await?;
+ let row = sqlx::query!(
+ r#"SELECT j.user_id, c.kind, c.channel_id, c.checkpoint_part, c.checkpoint_record
+ FROM slack_import_job j JOIN slack_import_conversation c ON c.job_id = j.id
+ WHERE j.id = $1 AND c.slack_channel_id = $2"#,
+ Uuid::from(lease.event.job_id),
+ lease.event.slack_channel_id.as_str(),
+ )
+ .fetch_one(&mut **tx)
+ .await
+ .map_err(internal)?;
+ Ok(WriteContext {
+ team,
+ requester: MacroUserIdStr::parse_from_str(&row.user_id)
+ .map_err(internal)?
+ .into_owned(),
+ kind: parse_enum(&row.kind)?,
+ channel: row.channel_id,
+ checkpoint: Checkpoint {
+ part_index: row.checkpoint_part as u32,
+ record_index: row.checkpoint_record as u32,
+ },
+ })
+}
+
+/// Persist warnings idempotently under the same fence as target binding.
+pub async fn record_warnings(
+ tx: &mut Transaction<'_, Postgres>,
+ lease: &Lease,
+ warnings: &[ImportWarning],
+) -> PortResult<()> {
+ let team = fence(tx, lease).await?;
+ for warning in warnings {
+ let warning = super::lifecycle::enum_string(warning)?;
+ sqlx::query!(
+ r#"UPDATE slack_import_conversation SET warnings = array_append(warnings, $3)
+ WHERE job_id = $1 AND slack_channel_id = $2 AND NOT ($3 = ANY(warnings))"#,
+ Uuid::from(lease.event.job_id),
+ lease.event.slack_channel_id.as_str(),
+ warning,
+ )
+ .execute(&mut **tx)
+ .await
+ .map_err(internal)?;
+ }
+ bump_revision(tx, team, lease.event.job_id).await
+}
+
+/// A mapping selected by first-commit-wins source identity.
+#[derive(Debug)]
+pub struct MessageMapping {
+ /// Exact source identity.
+ pub source: SourceMessageId,
+ /// Existing message ID or the newly reserved candidate.
+ pub message_id: Uuid,
+ /// Only newly reserved candidates may be inserted into comms by the coordinator.
+ pub inserted: bool,
+}
+
+/// Result of checking a batch's expected and next checkpoints under its lease.
+pub enum BatchStart<'a, 'c> {
+ /// The entire checkpoint is already committed; do not write messages or reactions.
+ Replayed(ImportCounters),
+ /// Holds the job lock for all owning-crate operations in the shared transaction.
+ Ready(FencedBatch<'a, 'c>),
+}
+
+/// Locked import batch. Use `transaction` for other owning-crate helpers, then
+/// `finish` before committing. A dropped/error batch must roll back the transaction.
+pub struct FencedBatch<'a, 'c> {
+ tx: &'a mut Transaction<'c, Postgres>,
+ lease: &'a Lease,
+ team: TeamId,
+ channel: Option<Uuid>,
+ next: Checkpoint,
+ records: u64,
+ mapped: bool,
+ imported: u64,
+ duplicates: u64,
+}
+
+/// Persist the authorized target, including for zero-part shape-only imports.
+/// The coordinator calls the channel owner's creation helpers in this transaction;
+/// the immediate channel FK requires creation before this call. Existing targets
+/// are immutable and authorization remains the caller's domain responsibility.
+pub async fn bind_target(
+ tx: &mut Transaction<'_, Postgres>,
+ lease: &Lease,
+ channel: Uuid,
+) -> PortResult<()> {
+ let team = fence(tx, lease).await?;
+ let row = sqlx::query!(
+ "SELECT channel_id FROM slack_import_conversation WHERE job_id = $1 AND slack_channel_id = $2",
+ Uuid::from(lease.event.job_id), lease.event.slack_channel_id.as_str(),
+ ).fetch_one(&mut **tx).await.map_err(internal)?;
+ match row.channel_id {
+ Some(existing) if existing != channel => return Err(ImportError::Conflict.into()),
+ Some(_) => return Ok(()),
+ None => (),
+ }
+ sqlx::query!(
+ "UPDATE slack_import_conversation SET channel_id = $3 WHERE job_id = $1 AND slack_channel_id = $2",
+ Uuid::from(lease.event.job_id), lease.event.slack_channel_id.as_str(), channel,
+ ).execute(&mut **tx).await.map_err(internal)?;
+ bump_revision(tx, team, lease.event.job_id).await
+}
+
+/// Check the execution fence and exact expected restart point before ANY writes.
+/// A target is supplied only after domain authorization; an existing target cannot
+/// be changed. Checkpoint distance includes every skipped record and part boundary.
+pub async fn begin_batch<'a, 'c>(
+ tx: &'a mut Transaction<'c, Postgres>,
+ lease: &'a Lease,
+ expected: Checkpoint,
+ next: Checkpoint,
+ channel: Option<Uuid>,
+) -> PortResult<BatchStart<'a, 'c>> {
+ let team = fence(tx, lease).await?;
+ let row = sqlx::query!(
+ r#"SELECT checkpoint_part, checkpoint_record, part_count, channel_id,
+ processed, imported, duplicates, skipped, reactions
+ FROM slack_import_conversation WHERE job_id = $1 AND slack_channel_id = $2"#,
+ Uuid::from(lease.event.job_id),
+ lease.event.slack_channel_id.as_str(),
+ )
+ .fetch_one(&mut **tx)
+ .await
+ .map_err(internal)?;
+ let current = Checkpoint {
+ part_index: row.checkpoint_part as u32,
+ record_index: row.checkpoint_record as u32,
+ };
+ if position(next) <= position(current) {
+ return Ok(BatchStart::Replayed(ImportCounters {
+ processed: row.processed as u64,
+ imported: row.imported as u64,
+ duplicates: row.duplicates as u64,
+ skipped: row.skipped as u64,
+ reactions: row.reactions as u64,
+ }));
+ }
+ if current != expected || row.channel_id.is_some_and(|id| channel != Some(id)) {
+ return Err(ImportError::Conflict.into());
+ }
+ let count = row.part_count.ok_or(ImportError::Conflict)? as u32;
+ if next.part_index > count || (next.part_index == count && next.record_index != 0) {
+ return Err(ImportError::InvalidInput.into());
+ }
+ let parts = sqlx::query!(
+ r#"SELECT part_index AS "part_index!", record_count AS "record_count!" FROM slack_import_upload
+ WHERE job_id = $1 AND slack_channel_id = $2 AND part_index >= $3 AND part_index <= $4
+ ORDER BY part_index"#,
+ Uuid::from(lease.event.job_id), lease.event.slack_channel_id.as_str(),
+ i32::try_from(current.part_index).map_err(internal)?, i32::try_from(next.part_index).map_err(internal)?,
+ ).fetch_all(&mut **tx).await.map_err(internal)?;
+ let mut records = 0_u64;
+ for part in parts {
+ let index = part.part_index as u32;
+ let end = if index == next.part_index {
+ next.record_index
+ } else {
+ part.record_count as u32
+ };
+ let start = if index == current.part_index {
+ current.record_index
+ } else {
+ 0
+ };
+ // A consumed part is represented by the next part's record zero, not
+ // by a second spelling at the end of the old part.
+ if end > part.record_count as u32
+ || end < start
+ || (index == next.part_index && end == part.record_count as u32)
+ {
+ return Err(ImportError::InvalidInput.into());
+ }
+ records += u64::from(end - start);
+ }
+ if records == 0 {
+ return Err(ImportError::InvalidInput.into());
+ }
+ sqlx::query!(
+ "UPDATE slack_import_conversation SET channel_id = $3 WHERE job_id = $1 AND slack_channel_id = $2",
+ Uuid::from(lease.event.job_id), lease.event.slack_channel_id.as_str(), channel,
+ ).execute(&mut **tx).await.map_err(internal)?;
+ Ok(BatchStart::Ready(FencedBatch {
+ tx,
+ lease,
+ team,
+ channel,
+ next,
+ records,
+ mapped: false,
+ imported: 0,
+ duplicates: 0,
+ }))
+}
+
+impl<'c> FencedBatch<'_, 'c> {
+ /// Borrow the SAME transaction for message/reaction owning-crate helpers.
+ pub fn transaction(&mut self) -> &mut Transaction<'c, Postgres> {
+ self.tx
+ }
+
+ /// Reserve source mappings in timestamp order before writing messages. Deferred
+ /// FKs ensure a failed/missing comms insert rolls back the mappings as well.
+ /// The composition root enforces its configured message/byte ceilings too.
+ pub async fn reserve_mappings(
+ &mut self,
+ messages: &[HistoricalMessage],
+ ) -> PortResult<Vec<MessageMapping>> {
+ if self.mapped || messages.len() > ImportLimits::default().database_batch_messages as usize
+ {
+ return Err(ImportError::LimitExceeded.into());
+ }
+ let mut identities = HashSet::new();
+ let mut ids = HashSet::new();
+ for message in messages {
+ if message.source.team_id != self.team
+ || message.source.slack_channel_id != self.lease.event.slack_channel_id
+ || Some(message.channel_id) != self.channel
+ || !identities.insert(message.source.ts)
+ || !ids.insert(message.id)
+ {
+ return Err(ImportError::InvalidInput.into());
+ }
+ }
+ let payload = serde_json::Value::Array(
+ messages
+ .iter()
+ .map(|m| {
+ serde_json::json!({
+ "ts": m.source.ts.unix_micros(), "message_id": m.id,
+ })
+ })
+ .collect(),
+ );
+ let inserted = sqlx::query!(
+ r#"INSERT INTO slack_import_message_map (team_id, slack_channel_id, slack_ts, message_id, channel_id)
+ SELECT $1, $2, m.ts, m.message_id, $3 FROM jsonb_to_recordset($4) AS m(ts bigint, message_id uuid)
+ ORDER BY m.ts ON CONFLICT (team_id, slack_channel_id, slack_ts) DO NOTHING RETURNING slack_ts"#,
+ Uuid::from(self.team), self.lease.event.slack_channel_id.as_str(), self.channel, payload,
+ ).fetch_all(&mut **self.tx).await.map_err(internal)?;
+ let inserted: HashSet<i64> = inserted.into_iter().map(|r| r.slack_ts).collect();
+ // A separate READ COMMITTED statement observes a conflicting mapping after
+ // waiting for the other job's insert to commit; an INSERT CTE cannot do that.
+ let timestamps: Vec<i64> = messages.iter().map(|m| m.source.ts.unix_micros()).collect();
+ let rows = sqlx::query!(
+ r#"SELECT slack_ts, message_id, channel_id FROM slack_import_message_map
+ WHERE team_id = $1 AND slack_channel_id = $2 AND slack_ts = ANY($3)"#,
+ Uuid::from(self.team),
+ self.lease.event.slack_channel_id.as_str(),
+ &timestamps,
+ )
+ .fetch_all(&mut **self.tx)
+ .await
+ .map_err(internal)?;
+ if rows.iter().any(|r| Some(r.channel_id) != self.channel) {
+ return Err(ImportError::Conflict.into());
+ }
+ let mappings: HashMap<i64, Uuid> = rows
+ .into_iter()
+ .map(|r| (r.slack_ts, r.message_id))
+ .collect();
+ let result = messages
+ .iter()
+ .map(|m| {
+ Ok(MessageMapping {
+ source: m.source.clone(),
+ message_id: *mappings
+ .get(&m.source.ts.unix_micros())
+ .ok_or(ImportError::Internal)?,
+ inserted: inserted.contains(&m.source.ts.unix_micros()),
+ })
+ })
+ .collect::<PortResult<Vec<_>>>()?;
+ self.mapped = true;
+ self.imported = inserted.len() as u64;
+ self.duplicates = messages.len() as u64 - self.imported;
+ Ok(result)
+ }
+
+ /// Commit checkpoint/counters and search outbox state alongside the caller's
+ /// rows. `reactions` counts actual new reaction inserts, never attempted writes.
+ /// Skipped-only batches are durable too. Recheck time after potentially slow SQL.
+ pub async fn finish(self, skipped: u64, reactions: u64) -> PortResult<ImportCounters> {
+ if self
+ .imported
+ .checked_add(self.duplicates)
+ .and_then(|n| n.checked_add(skipped))
+ != Some(self.records)
+ {
+ return Err(ImportError::InvalidInput.into());
+ }
+ fence(self.tx, self.lease).await?;
+ let dirty = self.imported > 0 || reactions > 0;
+ if dirty && self.channel.is_none() {
+ return Err(ImportError::InvalidInput.into());
+ }
+ let row = sqlx::query!(
+ r#"UPDATE slack_import_conversation SET checkpoint_part = $3, checkpoint_record = $4,
+ processed = processed + $5, imported = imported + $6, duplicates = duplicates + $7,
+ skipped = skipped + $8, reactions = reactions + $9,
+ search_dirty_generation = search_dirty_generation + CASE WHEN $10 THEN 1 ELSE 0 END,
+ search_state = CASE WHEN $10 THEN 'pending' ELSE search_state END,
+ search_receipt_id = CASE WHEN $10 THEN NULL ELSE search_receipt_id END,
+ search_submitted_generation = CASE WHEN $10 THEN NULL ELSE search_submitted_generation END,
+ search_updated_at = CASE WHEN $10 THEN clock_timestamp() ELSE search_updated_at END,
+ updated_at = clock_timestamp()
+ WHERE job_id = $1 AND slack_channel_id = $2
+ RETURNING processed, imported, duplicates, skipped, reactions, search_dirty_generation"#,
+ Uuid::from(self.lease.event.job_id), self.lease.event.slack_channel_id.as_str(),
+ i32::try_from(self.next.part_index).map_err(internal)?, i32::try_from(self.next.record_index).map_err(internal)?,
+ number(self.records)?, number(self.imported)?, number(self.duplicates)?, number(skipped)?, number(reactions)?, dirty,
+ ).fetch_one(&mut **self.tx).await.map_err(internal)?;
+ if dirty {
+ sqlx::query!(
+ r#"UPDATE slack_import_outbox SET cancelled_at = clock_timestamp()
+ WHERE job_id = $1 AND slack_channel_id = $2 AND kind = 'search'
+ AND published_at IS NULL AND cancelled_at IS NULL"#,
+ Uuid::from(self.lease.event.job_id),
+ self.lease.event.slack_channel_id.as_str(),
+ )
+ .execute(&mut **self.tx)
+ .await
+ .map_err(internal)?;
+ sqlx::query!(
+ r#"INSERT INTO slack_import_outbox (job_id, slack_channel_id, kind, generation)
+ VALUES ($1, $2, 'search', $3) ON CONFLICT (job_id, slack_channel_id, kind, generation) DO NOTHING"#,
+ Uuid::from(self.lease.event.job_id), self.lease.event.slack_channel_id.as_str(), row.search_dirty_generation,
+ ).execute(&mut **self.tx).await.map_err(internal)?;
+ }
+ bump_revision(self.tx, self.team, self.lease.event.job_id).await?;
+ Ok(ImportCounters {
+ processed: row.processed as u64,
+ imported: row.imported as u64,
+ duplicates: row.duplicates as u64,
+ skipped: row.skipped as u64,
+ reactions: row.reactions as u64,
+ })
+ }
+}
+
+/// Resolve a bounded set of exact source mappings for thread parents. No ownership
+/// is inferred from these IDs: the message owner validates target/parent membership.
+pub async fn lookup(
+ connection: &mut PgConnection,
+ sources: &[SourceMessageId],
+) -> PortResult<Vec<(SourceMessageId, Uuid)>> {
+ if sources.len() > ImportLimits::default().database_batch_messages as usize {
+ return Err(ImportError::LimitExceeded.into());
+ }
+ let payload = serde_json::Value::Array(sources.iter().enumerate().map(|(index, s)| serde_json::json!({
+ "index": index, "team": Uuid::from(s.team_id), "channel": s.slack_channel_id, "ts": s.ts.unix_micros(),
+ })).collect());
+ let rows = sqlx::query!(
+ r#"SELECT s.index AS "index!", m.message_id FROM jsonb_to_recordset($1) AS s(index integer, team uuid, channel text, ts bigint)
+ JOIN slack_import_message_map m ON m.team_id = s.team AND m.slack_channel_id = s.channel AND m.slack_ts = s.ts"#,
+ payload,
+ ).fetch_all(connection).await.map_err(internal)?;
+ Ok(rows
+ .into_iter()
+ .map(|r| (sources[r.index as usize].clone(), r.message_id))
+ .collect())
+}
+
+impl crate::domain::ports::SourceMessageReader for PgSlackImportRepo {
+ async fn reference_mappings(
+ &self,
+ team: TeamId,
+ sources: &[SourceMessageId],
+ ) -> PortResult<Vec<crate::domain::slack::references::resolve::StoredMessageMapping>> {
+ use crate::domain::slack::references::resolve::StoredMessageMapping;
+ if sources.len()
+ > self
+ .limits
+ .database_batch_messages
+ .min(ImportLimits::default().database_batch_messages) as usize
+ || sources.iter().any(|source| source.team_id != team)
+ {
+ return Err(ImportError::LimitExceeded.into());
+ }
+ let payload = serde_json::Value::Array(
+ sources
+ .iter()
+ .enumerate()
+ .map(|(index, s)| {
+ serde_json::json!({
+ "index": index, "channel": s.slack_channel_id, "ts": s.ts.unix_micros(),
+ })
+ })
+ .collect(),
+ );
+ if serde_json::to_vec(&payload).map_err(internal)?.len() as u64
+ > self.limits.database_batch_bytes
+ {
+ return Err(ImportError::LimitExceeded.into());
+ }
+ let rows = sqlx::query!(
+ r#"SELECT s.index AS "index!", m.message_id, m.channel_id
+ FROM jsonb_to_recordset($1) AS s(index integer, channel text, ts bigint)
+ JOIN slack_import_message_map m ON m.team_id = $2
+ AND m.slack_channel_id = s.channel AND m.slack_ts = s.ts"#,
+ payload,
+ Uuid::from(team),
+ )
+ .fetch_all(&self.pool)
+ .await
+ .map_err(internal)?;
+ Ok(rows
+ .into_iter()
+ .map(|row| StoredMessageMapping {
+ source: sources[row.index as usize].clone(),
+ channel: row.channel_id,
+ message: row.message_id,
+ })
+ .collect())
+ }
+}
+
+impl PgSlackImportRepo {
+ /// Load source scope and requester only from an explicitly team-owned job.
+ /// Binding and domain evidence are supplied by the owning ledger, not SQL here.
+ pub async fn reference_job(
+ &self,
+ team: TeamId,
+ job: JobId,
+ ) -> PortResult<Option<(MacroUserIdStr<'static>, SourceIdentity)>> {
+ let row = sqlx::query!(
+ "SELECT user_id, source_workspace_id, confirmed_unknown FROM slack_import_job WHERE team_id = $1 AND id = $2",
+ Uuid::from(team), Uuid::from(job),
+ ).fetch_optional(&self.pool).await.map_err(internal)?;
+ row.map(|row| {
+ let source = match row.source_workspace_id {
+ Some(source) => SourceIdentity::Known {
+ source_id: source.parse().map_err(internal)?,
+ },
+ None if row.confirmed_unknown => SourceIdentity::ConfirmedUnknown,
+ None => return Err(ImportError::Unavailable.into()),
+ };
+ Ok((
+ MacroUserIdStr::parse_from_str(&row.user_id)
+ .map_err(internal)?
+ .into_owned(),
+ source,
+ ))
+ })
+ .transpose()
+ }
+
+ /// Bounded selected work with remaining resolution opportunity; no target IDs.
+ pub async fn pending_reference_channels(
+ &self,
+ team: TeamId,
+ job: JobId,
+ channels: &[ConversationId],
+ ) -> PortResult<Vec<ConversationId>> {
+ if channels.len()
+ > self
+ .limits
+ .database_batch_messages
+ .min(ImportLimits::default().database_batch_messages) as usize
+ {
+ return Err(ImportError::LimitExceeded.into());
+ }
+ let ids: Vec<_> = channels.iter().map(ConversationId::as_str).collect();
+ let rows = sqlx::query_scalar!(
+ r#"SELECT c.slack_channel_id FROM slack_import_conversation c
+ JOIN slack_import_job j ON j.id = c.job_id
+ WHERE j.team_id = $1 AND j.id = $2 AND c.slack_channel_id = ANY($3)
+ AND c.status IN ('awaiting_uploads', 'queued', 'importing')"#,
+ Uuid::from(team),
+ Uuid::from(job),
+ &ids as _,
+ )
+ .fetch_all(&self.pool)
+ .await
+ .map_err(internal)?;
+ rows.into_iter()
+ .map(|id| id.parse().map_err(internal))
+ .collect()
+ }
+}
+
+fn position(checkpoint: Checkpoint) -> (u32, u32) {
+ (checkpoint.part_index, checkpoint.record_index)
+}
+
+pub(super) async fn pending_search(pool: &PgPool, limit: u32) -> PortResult<Vec<SearchBackfill>> {
+ let rows = sqlx::query!(
+ r#"SELECT c.job_id, c.channel_id AS "channel_id!", c.search_dirty_generation, c.search_state,
+ c.search_receipt_id, c.search_updated_at FROM slack_import_conversation c
+ JOIN slack_import_outbox o ON o.job_id = c.job_id AND o.slack_channel_id = c.slack_channel_id
+ AND o.kind = 'search' AND o.generation = c.search_dirty_generation
+ WHERE c.channel_id IS NOT NULL AND c.search_state IN ('pending', 'submitted', 'failed')
+ AND o.cancelled_at IS NULL AND o.available_at <= clock_timestamp()
+ AND NOT EXISTS (SELECT 1 FROM slack_import_message_reference r
+ WHERE r.job_id = c.job_id AND r.completed_at IS NULL)
+ ORDER BY c.search_updated_at, c.job_id, c.slack_channel_id LIMIT $1"#, i64::from(limit),
+ ).fetch_all(pool).await.map_err(internal)?;
+ rows.into_iter()
+ .map(|r| {
+ Ok(SearchBackfill {
+ job_id: r.job_id.try_into().map_err(internal)?,
+ channel_ids: vec![r.channel_id],
+ generation: r.search_dirty_generation as u64,
+ state: match r.search_state.as_str() {
+ "submitted" => SearchState::Submitted {
+ receipt_id: r.search_receipt_id.ok_or(ImportError::Internal)?,
+ },
+ "failed" => SearchState::Failed,
+ _ => SearchState::Pending,
+ },
+ updated_at: r.search_updated_at,
+ })
+ })
+ .collect()
+}
+
+pub(super) async fn record_search(
+ pool: &PgPool,
+ request: &SearchBackfill,
+ state: SearchState,
+) -> PortResult<()> {
+ if request.channel_ids.is_empty()
+ || request.channel_ids.len() > 50
+ || matches!(state, SearchState::NotNeeded)
+ {
+ return Err(ImportError::InvalidInput.into());
+ }
+ let mut tx = pool.begin().await.map_err(internal)?;
+ let team = sqlx::query_scalar!(
+ "SELECT team_id FROM slack_import_job WHERE id = $1 FOR UPDATE",
+ Uuid::from(request.job_id)
+ )
+ .fetch_optional(&mut *tx)
+ .await
+ .map_err(internal)?;
+ let Some(team) = team else {
+ return Ok(());
+ };
+ let (status, receipt) = match state {
+ SearchState::Submitted { receipt_id } => ("submitted", Some(receipt_id)),
+ SearchState::Pending => ("pending", None),
+ SearchState::Completed => ("completed", None),
+ SearchState::Failed => ("failed", None),
+ SearchState::NotNeeded => return Err(ImportError::InvalidInput.into()),
+ };
+ let previous_receipt = match request.state {
+ SearchState::Submitted { receipt_id } => Some(receipt_id),
+ _ => None,
+ };
+ let rows = sqlx::query!(
+ r#"UPDATE slack_import_conversation SET search_state = $4, search_receipt_id = $5,
+ search_submitted_generation = CASE WHEN $5::uuid IS NOT NULL THEN $3 ELSE search_submitted_generation END,
+ search_attempts = search_attempts + CASE WHEN $4 = 'submitted' THEN 1 ELSE 0 END,
+ search_updated_at = clock_timestamp(), updated_at = clock_timestamp()
+ WHERE job_id = $1 AND channel_id = ANY($2) AND search_dirty_generation = $3
+ AND search_state <> 'completed' AND search_receipt_id IS NOT DISTINCT FROM $6
+ AND search_updated_at = $7
+ RETURNING slack_channel_id"#,
+ Uuid::from(request.job_id), &request.channel_ids, number(request.generation)?, status, receipt,
+ previous_receipt, request.updated_at,
+ ).fetch_all(&mut *tx).await.map_err(internal)?;
+ if !rows.is_empty() {
+ let channels: Vec<String> = rows.into_iter().map(|r| r.slack_channel_id).collect();
+ sqlx::query!(
+ r#"UPDATE slack_import_outbox SET
+ published_at = CASE WHEN $4 = 'completed' THEN coalesce(published_at, clock_timestamp()) ELSE published_at END,
+ available_at = clock_timestamp() + interval '60 seconds'
+ WHERE job_id = $1 AND slack_channel_id = ANY($2) AND kind = 'search' AND generation = $3"#,
+ Uuid::from(request.job_id), &channels, number(request.generation)?, status,
+ ).execute(&mut *tx).await.map_err(internal)?;
+ bump_revision(&mut tx, team.try_into().map_err(internal)?, request.job_id).await?;
+ super::lifecycle::recompute(&mut tx, request.job_id).await?;
+ }
+ tx.commit().await.map_err(internal)?;
+ Ok(())
+}
diff --git a/crates/slack_integration/src/outbound/pg_slack_import_repo/batches/test.rs b/crates/slack_integration/src/outbound/pg_slack_import_repo/batches/test.rs
new file mode 100644
index 0000000000..1898006145
--- /dev/null
+++ b/crates/slack_integration/src/outbound/pg_slack_import_repo/batches/test.rs
@@ -0,0 +1,595 @@
+use super::super::lifecycle::test::{claim, expire, fixture, fixture_parts};
+use super::*;
+use crate::domain::ports::{ExecutionRepo, ImportRepo};
+
+fn checkpoint(part_index: u32, record_index: u32) -> Checkpoint {
+ Checkpoint {
+ part_index,
+ record_index,
+ }
+}
+
+async fn target(pool: &PgPool) -> Uuid {
+ let id = Uuid::now_v7();
+ sqlx::query!("INSERT INTO comms_channels (id, name, channel_type, owner_id) VALUES ($1, 'historical', 'private', 'macro|[email protected]')", id).execute(pool).await.unwrap();
+ id
+}
+
+fn message(team: TeamId, channel: Uuid, seconds: u32) -> HistoricalMessage {
+ HistoricalMessage {
+ id: Uuid::now_v7(),
+ source: SourceMessageId {
+ team_id: team,
+ slack_channel_id: "C1".parse().unwrap(),
+ ts: format!("{seconds}.000001").parse().unwrap(),
+ },
+ channel_id: channel,
+ parent_id: None,
+ orphaned_thread_ts: None,
+ sender: HistoricalSender::User(
+ MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap(),
+ ),
+ imported_author: None,
+ content: "historical".into(),
+ user_mentions: vec![],
+ body_references: vec![],
+ import_order: 0,
+ reactions: vec![],
+ }
+}
+
+// Emulate the composition root calling the message owner's helper in the SAME
+// transaction. Production import helpers never write comms tables.
+async fn insert_message(batch: &mut FencedBatch<'_, '_>, message: &HistoricalMessage) {
+ let HistoricalSender::User(sender) = &message.sender else {
+ panic!("user fixture")
+ };
+ sqlx::query!(
+ "INSERT INTO comms_messages (id, channel_id, sender_id, content) VALUES ($1, $2, $3, $4)",
+ message.id,
+ message.channel_id,
+ sender.as_ref(),
+ message.content,
+ )
+ .execute(&mut **batch.transaction())
+ .await
+ .unwrap();
+}
+
+#[sqlx::test(migrations = "../macro_db_client/migrations")]
+async fn shape_only_target_binding_is_fenced_and_immutable(pool: PgPool) {
+ let (repo, team, event) = fixture(&pool, None).await;
+ let running = claim(&repo, &event).await;
+ let channel = target(&pool).await;
+ let other = target(&pool).await;
+ let mut tx = pool.begin().await.unwrap();
+ bind_target(&mut tx, &running.lease, channel).await.unwrap();
+ bind_target(&mut tx, &running.lease, channel).await.unwrap();
+ assert!(bind_target(&mut tx, &running.lease, other).await.is_err());
+ tx.commit().await.unwrap();
+ repo.settle(&running.lease, ConversationStatus::Completed, None)
+ .await
+ .unwrap();
+ let progress = repo.finalize(team, event.job_id).await.unwrap();
+ assert_eq!(progress.status, JobStatus::Completed);
+ assert_eq!(progress.conversations[0].channel_id, Some(channel));
+ let mut tx = pool.begin().await.unwrap();
+ assert!(bind_target(&mut tx, &running.lease, channel).await.is_err());
+}
+
+#[sqlx::test(migrations = "../macro_db_client/migrations")]
+async fn concurrent_jobs_share_first_committed_message_mapping(pool: PgPool) {
+ let (repo, team, event) = fixture(&pool, Some(1)).await;
+ let first = claim(&repo, &event).await;
+ let second_job = repo
+ .create(
+ team,
+ &first.requested_by,
+ &CreateImport {
+ idempotency_token: Uuid::now_v7().try_into().unwrap(),
+ source: SourceIdentity::ConfirmedUnknown,
+ include_message_history: true,
+ conversations: vec![first.metadata.clone()],
+ },
+ &ImportLimits::default(),
+ )
+ .await
+ .unwrap()
+ .job_id;
+ let descriptors = vec![
+ first.users.registered.descriptor.clone(),
+ first.parts[0].registered.descriptor.clone(),
+ ];
+ let seal = ConversationSeal::from_descriptors(
+ event.slack_channel_id.clone(),
+ &descriptors[1..],
+ &ImportLimits::default(),
+ )
+ .unwrap();
+ let uploads: Vec<_> = repo
+ .register(team, second_job, &RegisterUploads { descriptors })
+ .await
+ .unwrap()
+ .into_iter()
+ .map(|registered| VerifiedUpload {
+ registered,
+ identity: ObjectIdentity::Version("v1".parse().unwrap()),
+ })
+ .collect();
+ repo.complete(team, second_job, &uploads, Some(&seal))
+ .await
+ .unwrap();
+ let second = claim(
+ &repo,
+ &ImportEvent {
+ job_id: second_job,
+ ..event.clone()
+ },
+ )
+ .await;
+ let channel = target(&pool).await;
+ let first_message = message(team, channel, 1);
+ let second_message = message(team, channel, 1);
+ async fn commit(
+ pool: &PgPool,
+ lease: &Lease,
+ message: &HistoricalMessage,
+ ) -> (Uuid, ImportCounters) {
+ let mut tx = pool.begin().await.unwrap();
+ let BatchStart::Ready(mut batch) = begin_batch(
+ &mut tx,
+ lease,
+ checkpoint(0, 0),
+ checkpoint(1, 0),
+ Some(message.channel_id),
+ )
+ .await
+ .unwrap() else {
+ panic!("new batch");
+ };
+ let mappings = batch
+ .reserve_mappings(std::slice::from_ref(message))
+ .await
+ .unwrap();
+ if mappings[0].inserted {
+ insert_message(&mut batch, message).await;
+ }
+ let counters = batch.finish(0, 0).await.unwrap();
+ tx.commit().await.unwrap();
+ (mappings[0].message_id, counters)
+ }
+ let (left, right) = futures::join!(
+ commit(&pool, &first.lease, &first_message),
+ commit(&pool, &second.lease, &second_message)
+ );
+ assert_eq!(left.0, right.0);
+ assert_eq!(left.1.imported + right.1.imported, 1);
+ assert_eq!(left.1.duplicates + right.1.duplicates, 1);
+ use crate::domain::ports::SourceMessageReader;
+ let sources = [
+ first_message.source.clone(),
+ SourceMessageId {
+ ts: "1.000002".parse().unwrap(),
+ ..first_message.source.clone()
+ },
+ ];
+ let before = repo.progress(team, event.job_id).await.unwrap();
+ let found = repo.reference_mappings(team, &sources).await.unwrap();
+ assert_eq!(found.len(), 1);
+ assert_eq!(found[0].message, left.0);
+ assert_eq!(found[0].channel, channel);
+ assert_eq!(found[0].source.ts.to_string(), "1.000001");
+ assert_eq!(before, repo.progress(team, event.job_id).await.unwrap());
+ let other_team = Uuid::now_v7().try_into().unwrap();
+ assert!(repo.reference_mappings(other_team, &sources).await.is_err());
+ let foreign = SourceMessageId {
+ team_id: other_team,
+ ..sources[0].clone()
+ };
+ assert!(
+ repo.reference_mappings(other_team, &[foreign])
+ .await
+ .unwrap()
+ .is_empty()
+ );
+}
+
+#[sqlx::test(migrations = "../macro_db_client/migrations")]
+async fn checkpoints_cross_part_boundaries_without_counting_twice(pool: PgPool) {
+ let (repo, team, event) = fixture_parts(&pool, Some(2), 2).await;
+ let running = claim(&repo, &event).await;
+ let mut tx = pool.begin().await.unwrap();
+ // End-of-part must be spelled as the next part, not an ambiguous extra line.
+ assert!(
+ begin_batch(
+ &mut tx,
+ &running.lease,
+ checkpoint(0, 0),
+ checkpoint(0, 2),
+ None
+ )
+ .await
+ .is_err()
+ );
+ let BatchStart::Ready(batch) = begin_batch(
+ &mut tx,
+ &running.lease,
+ checkpoint(0, 0),
+ checkpoint(1, 1),
+ None,
+ )
+ .await
+ .unwrap() else {
+ panic!("new batch");
+ };
+ assert_eq!(batch.finish(3, 0).await.unwrap().processed, 3);
+ tx.commit().await.unwrap();
+ expire(&pool, &event).await;
+ let resumed = claim(&repo, &event).await;
+ assert_eq!(resumed.checkpoint, checkpoint(1, 1));
+ let mut tx = pool.begin().await.unwrap();
+ let BatchStart::Ready(batch) = begin_batch(
+ &mut tx,
+ &resumed.lease,
+ checkpoint(1, 1),
+ checkpoint(2, 0),
+ None,
+ )
+ .await
+ .unwrap() else {
+ panic!("last batch");
+ };
+ assert_eq!(batch.finish(1, 0).await.unwrap().processed, 4);
+ tx.commit().await.unwrap();
+ repo.settle(&resumed.lease, ConversationStatus::Completed, None)
+ .await
+ .unwrap();
+ assert_eq!(
+ repo.finalize(team, event.job_id)
+ .await
+ .unwrap()
+ .conversations[0]
+ .counters
+ .skipped,
+ 4
+ );
+}
+
+#[sqlx::test(migrations = "../macro_db_client/migrations")]
+async fn skipped_records_checkpoint_replay_and_reclaim(pool: PgPool) {
+ let (repo, team, event) = fixture(&pool, Some(3)).await;
+ let running = claim(&repo, &event).await;
+ let mut tx = pool.begin().await.unwrap();
+ let BatchStart::Ready(batch) = begin_batch(
+ &mut tx,
+ &running.lease,
+ checkpoint(0, 0),
+ checkpoint(0, 2),
+ None,
+ )
+ .await
+ .unwrap() else {
+ panic!("new batch");
+ };
+ let counters = batch.finish(2, 0).await.unwrap();
+ tx.commit().await.unwrap();
+ assert_eq!(counters.skipped, 2);
+ let mut tx = pool.begin().await.unwrap();
+ let BatchStart::Replayed(replayed) = begin_batch(
+ &mut tx,
+ &running.lease,
+ checkpoint(0, 0),
+ checkpoint(0, 2),
+ None,
+ )
+ .await
+ .unwrap() else {
+ panic!("replay");
+ };
+ assert_eq!(counters, replayed);
+ tx.commit().await.unwrap();
+ expire(&pool, &event).await;
+ let resumed = claim(&repo, &event).await;
+ assert_eq!(resumed.checkpoint, checkpoint(0, 2));
+ let mut tx = pool.begin().await.unwrap();
+ assert!(
+ begin_batch(
+ &mut tx,
+ &running.lease,
+ checkpoint(0, 2),
+ checkpoint(1, 0),
+ None
+ )
+ .await
+ .is_err()
+ );
+ tx.rollback().await.unwrap();
+ let mut tx = pool.begin().await.unwrap();
+ let BatchStart::Ready(batch) = begin_batch(
+ &mut tx,
+ &resumed.lease,
+ checkpoint(0, 2),
+ checkpoint(1, 0),
+ None,
+ )
+ .await
+ .unwrap() else {
+ panic!("remaining batch");
+ };
+ assert_eq!(batch.finish(1, 0).await.unwrap().skipped, 3);
+ tx.commit().await.unwrap();
+ repo.settle(&resumed.lease, ConversationStatus::Completed, None)
+ .await
+ .unwrap();
+ let progress = repo.finalize(team, event.job_id).await.unwrap();
+ assert_eq!(progress.status, JobStatus::Completed);
+ assert_eq!(progress.conversations[0].counters.processed, 3);
+ assert!(repo.pending_search(50).await.unwrap().is_empty());
+}
+
+#[sqlx::test(migrations = "../macro_db_client/migrations")]
+async fn mappings_messages_counters_and_search_rollback_together(pool: PgPool) {
+ let (repo, team, event) = fixture(&pool, Some(1)).await;
+ let running = claim(&repo, &event).await;
+ let channel = target(&pool).await;
+ let message = message(team, channel, 1);
+ // A mapping without a message cannot commit because its FK is deferred.
+ let mut tx = pool.begin().await.unwrap();
+ let BatchStart::Ready(mut batch) = begin_batch(
+ &mut tx,
+ &running.lease,
+ checkpoint(0, 0),
+ checkpoint(1, 0),
+ Some(channel),
+ )
+ .await
+ .unwrap() else {
+ panic!("new batch");
+ };
+ assert!(
+ batch
+ .reserve_mappings(std::slice::from_ref(&message))
+ .await
+ .unwrap()[0]
+ .inserted
+ );
+ batch.finish(0, 0).await.unwrap();
+ assert!(tx.commit().await.is_err());
+ assert_eq!(
+ repo.progress(team, event.job_id)
+ .await
+ .unwrap()
+ .unwrap()
+ .conversations[0]
+ .counters,
+ ImportCounters::default()
+ );
+ assert!(repo.pending_search(50).await.unwrap().is_empty());
+ let mut tx = pool.begin().await.unwrap();
+ let BatchStart::Ready(mut batch) = begin_batch(
+ &mut tx,
+ &running.lease,
+ checkpoint(0, 0),
+ checkpoint(1, 0),
+ Some(channel),
+ )
+ .await
+ .unwrap() else {
+ panic!("retry");
+ };
+ assert!(
+ batch
+ .reserve_mappings(std::slice::from_ref(&message))
+ .await
+ .unwrap()[0]
+ .inserted
+ );
+ insert_message(&mut batch, &message).await;
+ batch.finish(0, 0).await.unwrap();
+ tx.commit().await.unwrap();
+ let found = lookup(
+ &mut pool.acquire().await.unwrap(),
+ std::slice::from_ref(&message.source),
+ )
+ .await
+ .unwrap();
+ assert_eq!(found, vec![(message.source, message.id)]);
+ let progress = repo.progress(team, event.job_id).await.unwrap().unwrap();
+ assert_eq!(progress.conversations[0].counters.imported, 1);
+ assert_eq!(progress.conversations[0].search, SearchState::Pending);
+ assert_eq!(repo.pending_search(50).await.unwrap().len(), 1);
+}
+
+#[sqlx::test(migrations = "../macro_db_client/migrations")]
+async fn duplicates_keep_first_mapping_and_partial_failure_keeps_search(pool: PgPool) {
+ let (repo, team, event) = fixture(&pool, Some(3)).await;
+ let running = claim(&repo, &event).await;
+ let channel = target(&pool).await;
+ let original = message(team, channel, 1);
+ for index in 0..2 {
+ let mut candidate = original.clone();
+ if index == 1 {
+ candidate.id = Uuid::now_v7();
+ }
+ let mut tx = pool.begin().await.unwrap();
+ let BatchStart::Ready(mut batch) = begin_batch(
+ &mut tx,
+ &running.lease,
+ checkpoint(0, index),
+ checkpoint(0, index + 1),
+ Some(channel),
+ )
+ .await
+ .unwrap() else {
+ panic!("new batch");
+ };
+ let maps = batch
+ .reserve_mappings(std::slice::from_ref(&candidate))
+ .await
+ .unwrap();
+ assert_eq!(maps[0].message_id, original.id);
+ assert_eq!(maps[0].inserted, index == 0);
+ if maps[0].inserted {
+ insert_message(&mut batch, &candidate).await;
+ }
+ batch.finish(0, 0).await.unwrap();
+ tx.commit().await.unwrap();
+ }
+ repo.settle(
+ &running.lease,
+ ConversationStatus::Failed,
+ Some(ImportError::Internal),
+ )
+ .await
+ .unwrap();
+ let progress = repo.finalize(team, event.job_id).await.unwrap();
+ assert_eq!(progress.status, JobStatus::Processing);
+ assert_eq!(progress.conversations[0].counters.imported, 1);
+ assert_eq!(progress.conversations[0].counters.duplicates, 1);
+ let request = repo.pending_search(50).await.unwrap().pop().unwrap();
+ repo.record_search(&request, SearchState::Completed)
+ .await
+ .unwrap();
+ assert_eq!(
+ repo.progress(team, event.job_id)
+ .await
+ .unwrap()
+ .unwrap()
+ .status,
+ JobStatus::CompletedWithErrors
+ );
+}
+
+#[sqlx::test(migrations = "../macro_db_client/migrations")]
+async fn stale_search_ack_cannot_clear_new_dirty_generation(pool: PgPool) {
+ let (repo, team, event) = fixture(&pool, Some(3)).await;
+ let running = claim(&repo, &event).await;
+ let channel = target(&pool).await;
+ let mut requests = Vec::new();
+ for index in 0..2 {
+ let message = message(team, channel, index + 1);
+ let mut tx = pool.begin().await.unwrap();
+ let BatchStart::Ready(mut batch) = begin_batch(
+ &mut tx,
+ &running.lease,
+ checkpoint(0, index),
+ checkpoint(0, index + 1),
+ Some(channel),
+ )
+ .await
+ .unwrap() else {
+ panic!("new batch");
+ };
+ batch
+ .reserve_mappings(std::slice::from_ref(&message))
+ .await
+ .unwrap();
+ insert_message(&mut batch, &message).await;
+ batch.finish(0, 0).await.unwrap();
+ tx.commit().await.unwrap();
+ requests.push(repo.pending_search(50).await.unwrap().pop().unwrap());
+ }
+ repo.record_search(&requests[0], SearchState::Completed)
+ .await
+ .unwrap();
+ assert_eq!(repo.pending_search(50).await.unwrap()[0].generation, 2);
+ let receipt_id = Uuid::now_v7();
+ repo.record_search(&requests[1], SearchState::Submitted { receipt_id })
+ .await
+ .unwrap();
+ // A duplicate acceptance may not overwrite a newer receipt or reset its poll clock.
+ repo.record_search(
+ &requests[1],
+ SearchState::Submitted {
+ receipt_id: Uuid::now_v7(),
+ },
+ )
+ .await
+ .unwrap();
+ sqlx::query!("UPDATE slack_import_outbox SET available_at = clock_timestamp() - interval '1 second' WHERE job_id = $1 AND kind = 'search'", Uuid::from(event.job_id)).execute(&pool).await.unwrap();
+ let submitted = repo.pending_search(50).await.unwrap().pop().unwrap();
+ assert_eq!(submitted.state, SearchState::Submitted { receipt_id });
+ // Partial cancellation must retain the search outbox after lease settlement.
+ repo.cancel(team, event.job_id).await.unwrap();
+ expire(&pool, &event).await;
+ repo.reconcile(50).await.unwrap();
+ assert_eq!(repo.pending_search(50).await.unwrap().len(), 1);
+ repo.record_search(&submitted, SearchState::Completed)
+ .await
+ .unwrap();
+ assert!(repo.pending_search(50).await.unwrap().is_empty());
+ assert_eq!(
+ repo.progress(team, event.job_id)
+ .await
+ .unwrap()
+ .unwrap()
+ .conversations[0]
+ .search,
+ SearchState::Completed
+ );
+}
+
+#[sqlx::test(migrations = "../macro_db_client/migrations")]
+async fn invalid_checkpoints_and_late_fence_roll_back(pool: PgPool) {
+ let (repo, team, event) = fixture(&pool, Some(3)).await;
+ let running = claim(&repo, &event).await;
+ let mut tx = pool.begin().await.unwrap();
+ assert!(
+ begin_batch(
+ &mut tx,
+ &running.lease,
+ checkpoint(0, 1),
+ checkpoint(0, 2),
+ None
+ )
+ .await
+ .is_err()
+ );
+ assert!(
+ begin_batch(
+ &mut tx,
+ &running.lease,
+ checkpoint(0, 0),
+ checkpoint(0, 4),
+ None
+ )
+ .await
+ .is_err()
+ );
+ assert!(
+ begin_batch(
+ &mut tx,
+ &running.lease,
+ checkpoint(0, 0),
+ checkpoint(2, 0),
+ None
+ )
+ .await
+ .is_err()
+ );
+ let BatchStart::Ready(mut batch) = begin_batch(
+ &mut tx,
+ &running.lease,
+ checkpoint(0, 0),
+ checkpoint(0, 2),
+ None,
+ )
+ .await
+ .unwrap() else {
+ panic!("new batch");
+ };
+ // Simulate time elapsing while the coordinator is inside the transaction.
+ sqlx::query!("UPDATE slack_import_conversation SET lease_expires_at = clock_timestamp() - interval '1 second' WHERE job_id = $1", Uuid::from(event.job_id)).execute(&mut **batch.transaction()).await.unwrap();
+ assert!(batch.finish(2, 0).await.is_err());
+ tx.rollback().await.unwrap();
+ assert_eq!(
+ repo.progress(team, event.job_id)
+ .await
+ .unwrap()
+ .unwrap()
+ .conversations[0]
+ .counters
+ .processed,
+ 0
+ );
+}
diff --git a/crates/slack_integration/src/outbound/pg_slack_import_repo/lifecycle.rs b/crates/slack_integration/src/outbound/pg_slack_import_repo/lifecycle.rs
new file mode 100644
index 0000000000..555c0a0dfc
--- /dev/null
+++ b/crates/slack_integration/src/outbound/pg_slack_import_repo/lifecycle.rs
@@ -0,0 +1,662 @@
+//! Job-first locking, execution fencing and durable queue recovery.
+
+use macro_user_id::cowlike::CowLike;
+
+use crate::domain::ports::{ExecutionRepo, ImportRepo};
+
+use super::*;
+
+#[cfg(test)]
+pub(super) mod test;
+
+const LEASE_SECONDS: f64 = 180.0;
+const MAX_ATTEMPTS: i32 = 5;
+const PAGE_LIMIT: u32 = 50;
+
+impl ImportRepo for PgSlackImportRepo {
+ async fn create(
+ &self,
+ team: TeamId,
+ admin: &MacroUserIdStr<'_>,
+ command: &CreateImport,
+ limits: &ImportLimits,
+ ) -> PortResult<ImportProgress> {
+ Self::create(self, team, admin, command, limits).await
+ }
+ async fn register(
+ &self,
+ team: TeamId,
+ job: JobId,
+ command: &RegisterUploads,
+ ) -> PortResult<Vec<RegisteredUpload>> {
+ Self::register(self, team, job, command).await
+ }
+ async fn uploads(
+ &self,
+ team: TeamId,
+ job: JobId,
+ uploads: &[UploadId],
+ ) -> PortResult<Vec<RegisteredUpload>> {
+ Self::uploads(self, team, job, uploads).await
+ }
+ async fn complete(
+ &self,
+ team: TeamId,
+ job: JobId,
+ verified: &[VerifiedUpload],
+ seal: Option<&ConversationSeal>,
+ ) -> PortResult<ImportProgress> {
+ Self::complete(self, team, job, verified, seal).await
+ }
+ async fn finalize(&self, team: TeamId, job: JobId) -> PortResult<ImportProgress> {
+ let mut tx = self.pool.begin().await.map_err(internal)?;
+ lock_job(&mut tx, team, job).await?;
+ let changed = sqlx::query!(
+ r#"UPDATE slack_import_job SET registration_closed_at = clock_timestamp(),
+ finalized_at = clock_timestamp() WHERE team_id = $1 AND id = $2
+ AND registration_closed_at IS NULL"#,
+ Uuid::from(team),
+ Uuid::from(job),
+ )
+ .execute(&mut *tx)
+ .await
+ .map_err(internal)?
+ .rows_affected()
+ > 0;
+ if changed {
+ sqlx::query!(
+ r#"UPDATE slack_import_conversation SET status = 'skipped',
+ warnings = array_append(warnings, 'uploads_incomplete'),
+ settled_at = clock_timestamp(), updated_at = clock_timestamp()
+ WHERE job_id = $1 AND status = 'awaiting_uploads'"#,
+ Uuid::from(job),
+ )
+ .execute(&mut *tx)
+ .await
+ .map_err(internal)?;
+ recompute(&mut tx, job).await?;
+ }
+ let result = self.receipt(&mut tx, team, job).await?;
+ tx.commit().await.map_err(internal)?;
+ Ok(result)
+ }
+ async fn cancel(&self, team: TeamId, job: JobId) -> PortResult<ImportProgress> {
+ let mut tx = self.pool.begin().await.map_err(internal)?;
+ lock_job(&mut tx, team, job).await?;
+ let changed = sqlx::query!(
+ r#"UPDATE slack_import_job SET
+ registration_closed_at = coalesce(registration_closed_at, clock_timestamp()),
+ cancel_requested_at = clock_timestamp(), status = 'cancelling'
+ WHERE team_id = $1 AND id = $2 AND status IN ('uploading', 'processing')"#,
+ Uuid::from(team),
+ Uuid::from(job),
+ )
+ .execute(&mut *tx)
+ .await
+ .map_err(internal)?
+ .rows_affected()
+ > 0;
+ if changed {
+ sqlx::query!(
+ r#"UPDATE slack_import_conversation SET status = 'skipped',
+ settled_at = clock_timestamp(), updated_at = clock_timestamp()
+ WHERE job_id = $1 AND status IN ('awaiting_uploads', 'queued')"#,
+ Uuid::from(job),
+ )
+ .execute(&mut *tx)
+ .await
+ .map_err(internal)?;
+ sqlx::query!(
+ r#"UPDATE slack_import_outbox SET cancelled_at = clock_timestamp()
+ WHERE job_id = $1 AND kind = 'import' AND published_at IS NULL AND cancelled_at IS NULL"#,
+ Uuid::from(job),
+ ).execute(&mut *tx).await.map_err(internal)?;
+ recompute(&mut tx, job).await?;
+ }
+ let result = self.receipt(&mut tx, team, job).await?;
+ tx.commit().await.map_err(internal)?;
+ Ok(result)
+ }
+ async fn progress(&self, team: TeamId, job: JobId) -> PortResult<Option<ImportProgress>> {
+ Self::progress(self, team, job).await
+ }
+ async fn requested_by(
+ &self,
+ team: TeamId,
+ job: JobId,
+ ) -> PortResult<Option<MacroUserIdStr<'static>>> {
+ sqlx::query_scalar!(
+ "SELECT user_id FROM slack_import_job WHERE team_id = $1 AND id = $2",
+ Uuid::from(team),
+ Uuid::from(job),
+ )
+ .fetch_optional(&self.pool)
+ .await
+ .map_err(internal)?
+ .map(|user| {
+ MacroUserIdStr::parse_from_str(&user)
+ .map(CowLike::into_owned)
+ .map_err(internal)
+ })
+ .transpose()
+ }
+ async fn list(&self, team: TeamId, before: Option<JobId>) -> PortResult<Vec<ImportProgress>> {
+ Self::list(self, team, before).await
+ }
+}
+
+impl ExecutionRepo for PgSlackImportRepo {
+ async fn requester(
+ &self,
+ event: &ImportEvent,
+ ) -> PortResult<Option<(TeamId, MacroUserIdStr<'static>)>> {
+ let generation = number(event.generation)?;
+ let row = sqlx::query!(
+ r#"SELECT j.team_id, j.user_id FROM slack_import_job j
+ JOIN slack_import_conversation c ON c.job_id = j.id
+ WHERE j.id = $1 AND c.slack_channel_id = $2 AND c.event_generation = $3
+ AND c.status IN ('queued', 'importing') AND j.cancel_requested_at IS NULL"#,
+ Uuid::from(event.job_id),
+ event.slack_channel_id.as_str(),
+ generation,
+ )
+ .fetch_optional(&self.pool)
+ .await
+ .map_err(internal)?;
+ row.map(|r| {
+ Ok((
+ r.team_id.try_into().map_err(internal)?,
+ MacroUserIdStr::parse_from_str(&r.user_id)
+ .map_err(internal)?
+ .into_owned(),
+ ))
+ })
+ .transpose()
+ }
+
+ async fn claim(&self, event: &ImportEvent, owner: WorkerId) -> PortResult<ClaimOutcome> {
+ let mut tx = self.pool.begin().await.map_err(internal)?;
+ let Some(job) = lock_execution_job(&mut tx, event.job_id).await? else {
+ tx.rollback().await.map_err(internal)?;
+ return Ok(ClaimOutcome::Obsolete);
+ };
+ if job.cancelled || job.expired || job.terminal {
+ // Drop only queues rollback. Release the job lock before maintenance
+ // can try to reconcile its references with SKIP LOCKED.
+ tx.rollback().await.map_err(internal)?;
+ return Ok(ClaimOutcome::Obsolete);
+ }
+ let generation = number(event.generation)?;
+ let row = sqlx::query!(
+ r#"SELECT status, attempts, lease_expires_at > clock_timestamp() AS active
+ FROM slack_import_conversation WHERE job_id = $1 AND slack_channel_id = $2 AND event_generation = $3"#,
+ Uuid::from(event.job_id), event.slack_channel_id.as_str(), generation,
+ ).fetch_optional(&mut *tx).await.map_err(internal)?;
+ let Some(row) = row else {
+ tx.rollback().await.map_err(internal)?;
+ return Ok(ClaimOutcome::Obsolete);
+ };
+ if row.status == "importing" && row.active == Some(true) {
+ tx.rollback().await.map_err(internal)?;
+ return Ok(ClaimOutcome::ActiveLease);
+ }
+ if !matches!(row.status.as_str(), "queued" | "importing") {
+ tx.rollback().await.map_err(internal)?;
+ return Ok(ClaimOutcome::Obsolete);
+ }
+ if row.attempts >= MAX_ATTEMPTS {
+ terminal(&mut tx, event, "failed", Some(ImportError::Internal)).await?;
+ tx.commit().await.map_err(internal)?;
+ return Ok(ClaimOutcome::Obsolete);
+ }
+ let token = Uuid::now_v7();
+ let row = sqlx::query!(
+ r#"UPDATE slack_import_conversation SET status = 'importing', lease_owner = $3,
+ lease_token = $4, lease_generation = lease_generation + 1, attempts = attempts + 1,
+ lease_expires_at = clock_timestamp() + make_interval(secs => $5),
+ heartbeat_at = clock_timestamp(), updated_at = clock_timestamp()
+ WHERE job_id = $1 AND slack_channel_id = $2
+ RETURNING lease_generation, attempts, lease_expires_at, heartbeat_at,
+ kind, name, folder, member_ids, creator_id, source_created_at, archived, message_count,
+ checkpoint_part, checkpoint_record"#,
+ Uuid::from(event.job_id), event.slack_channel_id.as_str(), Uuid::from(owner), token, LEASE_SECONDS,
+ ).fetch_one(&mut *tx).await.map_err(internal)?;
+ let uploads = verified_uploads(&mut tx, event).await?;
+ let mut users = None;
+ let mut parts = Vec::new();
+ for upload in uploads {
+ match upload.registered.descriptor.upload {
+ UploadId::Users => users = Some(upload),
+ UploadId::ConversationPart { .. } => parts.push(upload),
+ }
+ }
+ bump_revision(
+ &mut tx,
+ job.team_id.try_into().map_err(internal)?,
+ event.job_id,
+ )
+ .await?;
+ let claimed = ClaimedConversation {
+ lease: Lease {
+ event: event.clone(),
+ owner,
+ token: token.try_into().map_err(internal)?,
+ generation: row.lease_generation as u64,
+ expires_at: row.lease_expires_at.ok_or(ImportError::Internal)?,
+ heartbeat_at: row.heartbeat_at.ok_or(ImportError::Internal)?,
+ attempts: row.attempts as u32,
+ },
+ team_id: job.team_id.try_into().map_err(internal)?,
+ requested_by: MacroUserIdStr::parse_from_str(&job.user_id)
+ .map_err(internal)?
+ .into_owned(),
+ metadata: ConversationMetadata {
+ slack_channel_id: event.slack_channel_id.clone(),
+ kind: parse_enum(&row.kind)?,
+ name: row.name,
+ folder: row.folder.parse().map_err(internal)?,
+ member_ids: row
+ .member_ids
+ .iter()
+ .map(|s| s.parse().map_err(internal))
+ .collect::<PortResult<_>>()?,
+ creator_id: row
+ .creator_id
+ .map(|s| s.parse().map_err(internal))
+ .transpose()?,
+ created_at: row.source_created_at.map(timestamp).transpose()?,
+ archived: row.archived,
+ message_count: row.message_count.map(|n| n as u64),
+ },
+ users: users.ok_or(ImportError::Internal)?,
+ parts,
+ checkpoint: Checkpoint {
+ part_index: row.checkpoint_part as u32,
+ record_index: row.checkpoint_record as u32,
+ },
+ job_created_at: job.created_at,
+ };
+ tx.commit().await.map_err(internal)?;
+ Ok(ClaimOutcome::Claimed(Box::new(claimed)))
+ }
+
+ async fn heartbeat(&self, lease: &Lease) -> PortResult<Lease> {
+ let mut tx = self.pool.begin().await.map_err(internal)?;
+ fence(&mut tx, lease).await?;
+ let row = sqlx::query!(
+ r#"UPDATE slack_import_conversation SET heartbeat_at = clock_timestamp(),
+ lease_expires_at = clock_timestamp() + make_interval(secs => $3)
+ WHERE job_id = $1 AND slack_channel_id = $2 RETURNING heartbeat_at, lease_expires_at"#,
+ Uuid::from(lease.event.job_id), lease.event.slack_channel_id.as_str(), LEASE_SECONDS,
+ ).fetch_one(&mut *tx).await.map_err(internal)?;
+ let mut renewed = lease.clone();
+ renewed.heartbeat_at = row.heartbeat_at.ok_or(ImportError::Internal)?;
+ renewed.expires_at = row.lease_expires_at.ok_or(ImportError::Internal)?;
+ tx.commit().await.map_err(internal)?;
+ Ok(renewed)
+ }
+
+ async fn settle(
+ &self,
+ lease: &Lease,
+ status: ConversationStatus,
+ error: Option<ImportError>,
+ ) -> PortResult<()> {
+ let status = match status {
+ ConversationStatus::Completed => "completed",
+ ConversationStatus::Skipped => "skipped",
+ ConversationStatus::Failed => "failed",
+ _ => return Err(ImportError::InvalidInput.into()),
+ };
+ let mut tx = self.pool.begin().await.map_err(internal)?;
+ fence(&mut tx, lease).await?;
+ if status == "completed" {
+ let complete = sqlx::query_scalar!(
+ r#"SELECT checkpoint_part = part_count AND checkpoint_record = 0 AS "complete!"
+ FROM slack_import_conversation WHERE job_id = $1 AND slack_channel_id = $2"#,
+ Uuid::from(lease.event.job_id),
+ lease.event.slack_channel_id.as_str(),
+ )
+ .fetch_one(&mut *tx)
+ .await
+ .map_err(internal)?;
+ if !complete {
+ return Err(ImportError::Conflict.into());
+ }
+ }
+ terminal(&mut tx, &lease.event, status, error).await?;
+ tx.commit().await.map_err(internal)?;
+ Ok(())
+ }
+
+ async fn pending_events(&self, limit: u32) -> PortResult<Vec<ImportEvent>> {
+ // Candidate selection never locks a child before its job. A short durable
+ // publication reservation recovers automatically after a publisher crash.
+ let candidates = sqlx::query!(
+ r#"SELECT o.job_id, o.slack_channel_id, o.generation FROM slack_import_outbox o
+ JOIN slack_import_job j ON j.id = o.job_id
+ JOIN slack_import_conversation c ON c.job_id = o.job_id AND c.slack_channel_id = o.slack_channel_id
+ WHERE o.kind = 'import' AND o.published_at IS NULL AND o.cancelled_at IS NULL
+ AND o.available_at <= clock_timestamp() AND j.cancel_requested_at IS NULL
+ AND j.staging_expires_at > clock_timestamp() + interval '1 day'
+ AND c.status IN ('queued', 'importing') AND c.event_generation = o.generation
+ ORDER BY o.available_at, o.job_id, o.slack_channel_id LIMIT $1"#, i64::from(limit.min(PAGE_LIMIT)),
+ ).fetch_all(&self.pool).await.map_err(internal)?;
+ let mut events = Vec::new();
+ for row in candidates {
+ let mut tx = self.pool.begin().await.map_err(internal)?;
+ let job = row.job_id.try_into().map_err(internal)?;
+ let Some(state) = lock_execution_job(&mut tx, job).await? else {
+ continue;
+ };
+ if state.cancelled || state.expired || state.terminal {
+ continue;
+ }
+ let changed = sqlx::query!(
+ r#"UPDATE slack_import_outbox o SET attempts = o.attempts + 1,
+ available_at = clock_timestamp() + interval '60 seconds'
+ FROM slack_import_conversation c
+ WHERE o.job_id = $1 AND o.slack_channel_id = $2 AND o.kind = 'import' AND o.generation = $3
+ AND o.published_at IS NULL AND o.cancelled_at IS NULL AND o.available_at <= clock_timestamp()
+ AND c.job_id = o.job_id AND c.slack_channel_id = o.slack_channel_id
+ AND c.event_generation = o.generation AND c.status IN ('queued', 'importing')"#,
+ row.job_id, row.slack_channel_id, row.generation,
+ ).execute(&mut *tx).await.map_err(internal)?.rows_affected() > 0;
+ tx.commit().await.map_err(internal)?;
+ if changed {
+ events.push(ImportEvent {
+ job_id: job,
+ slack_channel_id: row.slack_channel_id.parse().map_err(internal)?,
+ generation: row.generation as u64,
+ });
+ }
+ }
+ Ok(events)
+ }
+
+ async fn mark_published(&self, event: &ImportEvent) -> PortResult<()> {
+ let mut tx = self.pool.begin().await.map_err(internal)?;
+ let Some(_) = lock_execution_job(&mut tx, event.job_id).await? else {
+ return Ok(());
+ };
+ sqlx::query!(
+ r#"UPDATE slack_import_outbox SET published_at = clock_timestamp()
+ WHERE job_id = $1 AND slack_channel_id = $2 AND kind = 'import' AND generation = $3
+ AND published_at IS NULL AND cancelled_at IS NULL"#,
+ Uuid::from(event.job_id),
+ event.slack_channel_id.as_str(),
+ number(event.generation)?,
+ )
+ .execute(&mut *tx)
+ .await
+ .map_err(internal)?;
+ tx.commit().await.map_err(internal)?;
+ Ok(())
+ }
+
+ async fn dead_letter(&self, event: &ImportEvent) -> PortResult<WorkerOutcome> {
+ let mut tx = self.pool.begin().await.map_err(internal)?;
+ let Some(job) = lock_execution_job(&mut tx, event.job_id).await? else {
+ return Ok(WorkerOutcome::Acknowledge);
+ };
+ let row = sqlx::query!(
+ r#"SELECT status, lease_expires_at > clock_timestamp() AS active FROM slack_import_conversation
+ WHERE job_id = $1 AND slack_channel_id = $2 AND event_generation = $3"#,
+ Uuid::from(event.job_id), event.slack_channel_id.as_str(), number(event.generation)?,
+ ).fetch_optional(&mut *tx).await.map_err(internal)?;
+ if let Some(row) = row {
+ if row.status == "importing" && row.active == Some(true) {
+ return Ok(WorkerOutcome::Defer);
+ }
+ if matches!(row.status.as_str(), "queued" | "importing") {
+ let status = if job.cancelled { "skipped" } else { "failed" };
+ terminal(&mut tx, event, status, Some(ImportError::Internal)).await?;
+ }
+ }
+ tx.commit().await.map_err(internal)?;
+ Ok(WorkerOutcome::Acknowledge)
+ }
+
+ async fn reconcile(&self, limit: u32) -> PortResult<()> {
+ let jobs = sqlx::query_scalar!(
+ r#"SELECT j.id FROM slack_import_job j WHERE j.status IN ('uploading', 'processing', 'cancelling')
+ AND (j.staging_expires_at <= clock_timestamp() + interval '1 day'
+ OR EXISTS (SELECT 1 FROM slack_import_conversation c WHERE c.job_id = j.id
+ AND c.status = 'importing' AND c.lease_expires_at <= clock_timestamp()))
+ ORDER BY j.staging_expires_at, j.id LIMIT $1"#, i64::from(limit.min(PAGE_LIMIT)),
+ ).fetch_all(&self.pool).await.map_err(internal)?;
+ for id in jobs {
+ let job = id.try_into().map_err(internal)?;
+ let mut tx = self.pool.begin().await.map_err(internal)?;
+ let Some(state) = lock_execution_job(&mut tx, job).await? else {
+ continue;
+ };
+ if state.terminal {
+ continue;
+ }
+ if state.expired {
+ sqlx::query!(
+ r#"UPDATE slack_import_job SET registration_closed_at = coalesce(registration_closed_at, clock_timestamp()),
+ finalized_at = coalesce(finalized_at, clock_timestamp()) WHERE id = $1"#, id,
+ ).execute(&mut *tx).await.map_err(internal)?;
+ sqlx::query!(
+ r#"UPDATE slack_import_conversation SET status = 'skipped', warnings = array_append(warnings, 'uploads_incomplete'),
+ settled_at = clock_timestamp(), updated_at = clock_timestamp()
+ WHERE job_id = $1 AND status = 'awaiting_uploads'"#, id,
+ ).execute(&mut *tx).await.map_err(internal)?;
+ }
+ // Clear all expired leases, even after cancellation, but never revoke
+ // an unexpired worker. New event generations fence late queue/DLQ deliveries.
+ sqlx::query!(
+ r#"WITH recovered AS (
+ UPDATE slack_import_conversation SET
+ status = CASE WHEN $2 THEN 'skipped' WHEN $3 OR attempts >= $4 THEN 'failed' ELSE 'queued' END,
+ last_error = CASE WHEN $3 OR attempts >= $4 THEN 'internal' ELSE last_error END,
+ settled_at = CASE WHEN $2 OR $3 OR attempts >= $4 THEN clock_timestamp() ELSE NULL END,
+ event_generation = event_generation + 1,
+ lease_owner = NULL, lease_token = NULL, lease_expires_at = NULL, heartbeat_at = NULL,
+ updated_at = clock_timestamp()
+ WHERE job_id = $1 AND ((status = 'importing' AND lease_expires_at <= clock_timestamp())
+ OR (status = 'queued' AND ($2 OR $3)))
+ RETURNING job_id, slack_channel_id, event_generation, status
+ ) INSERT INTO slack_import_outbox (job_id, slack_channel_id, kind, generation)
+ SELECT job_id, slack_channel_id, 'import', event_generation FROM recovered WHERE status = 'queued'
+ ON CONFLICT (job_id, slack_channel_id, kind, generation) DO NOTHING"#,
+ id, state.cancelled, state.expired, MAX_ATTEMPTS,
+ ).execute(&mut *tx).await.map_err(internal)?;
+ sqlx::query!(
+ r#"UPDATE slack_import_outbox o SET cancelled_at = clock_timestamp()
+ FROM slack_import_conversation c WHERE o.job_id = $1 AND c.job_id = o.job_id
+ AND c.slack_channel_id = o.slack_channel_id AND o.kind = 'import'
+ AND o.published_at IS NULL AND o.cancelled_at IS NULL
+ AND (o.generation <> c.event_generation OR c.status NOT IN ('queued', 'importing'))"#, id,
+ ).execute(&mut *tx).await.map_err(internal)?;
+ recompute(&mut tx, job).await?;
+ tx.commit().await.map_err(internal)?;
+ }
+ Ok(())
+ }
+
+ async fn pending_search(&self, limit: u32) -> PortResult<Vec<SearchBackfill>> {
+ batches::pending_search(&self.pool, limit.min(PAGE_LIMIT)).await
+ }
+ async fn record_search(&self, request: &SearchBackfill, state: SearchState) -> PortResult<()> {
+ batches::record_search(&self.pool, request, state).await
+ }
+}
+
+struct ExecutionJob {
+ team_id: Uuid,
+ user_id: String,
+ created_at: DateTime<Utc>,
+ cancelled: bool,
+ expired: bool,
+ terminal: bool,
+}
+
+async fn lock_execution_job(
+ connection: &mut PgConnection,
+ job: JobId,
+) -> PortResult<Option<ExecutionJob>> {
+ sqlx::query_as!(
+ ExecutionJob,
+ r#"SELECT team_id, user_id, created_at, (cancel_requested_at IS NOT NULL) AS "cancelled!",
+ (staging_expires_at <= clock_timestamp() + interval '1 day') AS "expired!",
+ (status IN ('completed', 'completed_with_errors', 'failed', 'cancelled')) AS "terminal!"
+ FROM slack_import_job WHERE id = $1 FOR UPDATE"#,
+ Uuid::from(job),
+ )
+ .fetch_optional(connection)
+ .await
+ .map_err(internal)
+}
+
+/// Lock the job and check every durable fence field, using database time.
+/// Cancellation intentionally does not invalidate a still-running lease.
+pub(super) async fn fence(connection: &mut PgConnection, lease: &Lease) -> PortResult<TeamId> {
+ let job = lock_execution_job(connection, lease.event.job_id)
+ .await?
+ .ok_or(ImportError::LeaseLost)?;
+ if job.terminal || job.expired {
+ return Err(ImportError::LeaseLost.into());
+ }
+ let valid = sqlx::query_scalar!(
+ r#"SELECT EXISTS (SELECT 1 FROM slack_import_conversation WHERE job_id = $1 AND slack_channel_id = $2
+ AND status = 'importing' AND event_generation = $3 AND lease_owner = $4 AND lease_token = $5
+ AND lease_generation = $6 AND lease_expires_at > clock_timestamp()) AS "valid!""#,
+ Uuid::from(lease.event.job_id), lease.event.slack_channel_id.as_str(), number(lease.event.generation)?,
+ Uuid::from(lease.owner), Uuid::from(lease.token), number(lease.generation)?,
+ ).fetch_one(connection).await.map_err(internal)?;
+ if !valid {
+ return Err(ImportError::LeaseLost.into());
+ }
+ job.team_id.try_into().map_err(internal)
+}
+
+async fn terminal(
+ connection: &mut PgConnection,
+ event: &ImportEvent,
+ status: &str,
+ error: Option<ImportError>,
+) -> PortResult<()> {
+ let error = error.map(enum_string).transpose()?;
+ sqlx::query!(
+ r#"UPDATE slack_import_conversation SET status = $3, last_error = $4,
+ lease_owner = NULL, lease_token = NULL, lease_expires_at = NULL, heartbeat_at = NULL,
+ settled_at = clock_timestamp(), updated_at = clock_timestamp()
+ WHERE job_id = $1 AND slack_channel_id = $2"#,
+ Uuid::from(event.job_id),
+ event.slack_channel_id.as_str(),
+ status,
+ error,
+ )
+ .execute(&mut *connection)
+ .await
+ .map_err(internal)?;
+ sqlx::query!(
+ r#"UPDATE slack_import_outbox SET cancelled_at = clock_timestamp()
+ WHERE job_id = $1 AND slack_channel_id = $2 AND kind = 'import'
+ AND published_at IS NULL AND cancelled_at IS NULL"#,
+ Uuid::from(event.job_id),
+ event.slack_channel_id.as_str(),
+ )
+ .execute(&mut *connection)
+ .await
+ .map_err(internal)?;
+ recompute(connection, event.job_id).await
+}
+
+pub(super) async fn recompute(connection: &mut PgConnection, job: JobId) -> PortResult<()> {
+ // Called only under the job lock. Registration, not the presence of work,
+ // determines whether the job can settle; empty finalized jobs complete too.
+ sqlx::query!(
+ r#"WITH result AS (
+ SELECT CASE
+ WHEN j.cancel_requested_at IS NOT NULL THEN CASE WHEN EXISTS (
+ SELECT 1 FROM slack_import_conversation WHERE job_id = j.id
+ AND (status = 'importing' OR search_state IN ('pending', 'submitted', 'failed'))
+ ) OR EXISTS (SELECT 1 FROM slack_import_message_reference
+ WHERE job_id = j.id AND completed_at IS NULL)
+ THEN 'cancelling' ELSE 'cancelled' END
+ WHEN j.registration_closed_at IS NULL THEN 'uploading'
+ WHEN EXISTS (SELECT 1 FROM slack_import_conversation WHERE job_id = j.id
+ AND (status IN ('awaiting_uploads', 'queued', 'importing')
+ OR search_state IN ('pending', 'submitted', 'failed'))) THEN 'processing'
+ WHEN EXISTS (SELECT 1 FROM slack_import_message_reference
+ WHERE job_id = j.id AND completed_at IS NULL) THEN 'processing'
+ WHEN NOT EXISTS (SELECT 1 FROM slack_import_conversation WHERE job_id = j.id AND status = 'failed') THEN 'completed'
+ WHEN EXISTS (SELECT 1 FROM slack_import_conversation WHERE job_id = j.id AND (status = 'completed' OR imported > 0))
+ THEN 'completed_with_errors'
+ ELSE 'failed' END AS status
+ FROM slack_import_job j WHERE j.id = $1
+ ) UPDATE slack_import_job j SET status = r.status, revision = revision + 1, updated_at = clock_timestamp(),
+ settled_at = CASE WHEN r.status IN ('completed', 'completed_with_errors', 'failed', 'cancelled')
+ THEN coalesce(j.settled_at, clock_timestamp()) ELSE NULL END
+ FROM result r WHERE j.id = $1
+ AND j.status NOT IN ('completed', 'completed_with_errors', 'failed', 'cancelled')"#, Uuid::from(job),
+ ).execute(connection).await.map_err(internal)?;
+ Ok(())
+}
+
+async fn verified_uploads(
+ connection: &mut PgConnection,
+ event: &ImportEvent,
+) -> PortResult<Vec<VerifiedUpload>> {
+ let rows = sqlx::query!(
+ r#"SELECT slack_channel_id, part_index, object_key, sha256, byte_length, record_count,
+ verified_version_id, verified_etag FROM slack_import_upload
+ WHERE job_id = $1 AND (slack_channel_id IS NULL OR slack_channel_id = $2)
+ AND verified_at IS NOT NULL ORDER BY part_index NULLS FIRST"#,
+ Uuid::from(event.job_id),
+ event.slack_channel_id.as_str(),
+ )
+ .fetch_all(connection)
+ .await
+ .map_err(internal)?;
+ rows.into_iter()
+ .map(|r| {
+ let upload = match r.part_index {
+ Some(index) => UploadId::ConversationPart {
+ slack_channel_id: event.slack_channel_id.clone(),
+ part_index: index as u32,
+ },
+ None => UploadId::Users,
+ };
+ let identity = match (r.verified_version_id, r.verified_etag) {
+ (Some(value), _) => ObjectIdentity::Version(value.parse().map_err(internal)?),
+ (_, Some(value)) => ObjectIdentity::EntityTag(value.parse().map_err(internal)?),
+ _ => return Err(ImportError::Internal.into()),
+ };
+ Ok(VerifiedUpload {
+ registered: RegisteredUpload {
+ descriptor: UploadDescriptor {
+ upload,
+ sha256: r.sha256.parse().map_err(internal)?,
+ byte_length: r.byte_length as u64,
+ record_count: r.record_count.map(|n| n as u32),
+ },
+ key: r.object_key.parse().map_err(internal)?,
+ },
+ identity,
+ })
+ })
+ .collect()
+}
+
+fn timestamp(micros: i64) -> PortResult<SlackTimestamp> {
+ format!("{}.{:06}", micros / 1_000_000, micros % 1_000_000)
+ .parse()
+ .map_err(internal)
+}
+
+pub(super) fn number(value: u64) -> PortResult<i64> {
+ i64::try_from(value).map_err(|_| ImportError::InvalidInput.into())
+}
+
+pub(super) fn enum_string(value: impl serde::Serialize) -> PortResult<String> {
+ serde_json::to_value(value)
+ .map_err(internal)?
+ .as_str()
+ .map(str::to_owned)
+ .ok_or_else(|| ImportError::Internal.into())
+}
diff --git a/crates/slack_integration/src/outbound/pg_slack_import_repo/lifecycle/test.rs b/crates/slack_integration/src/outbound/pg_slack_import_repo/lifecycle/test.rs
new file mode 100644
index 0000000000..0af323a04e
--- /dev/null
+++ b/crates/slack_integration/src/outbound/pg_slack_import_repo/lifecycle/test.rs
@@ -0,0 +1,439 @@
+use super::*;
+
+pub(crate) async fn fixture(
+ pool: &PgPool,
+ records: Option<u32>,
+) -> (PgSlackImportRepo, TeamId, ImportEvent) {
+ fixture_parts(pool, records, 1).await
+}
+
+pub(crate) async fn fixture_parts(
+ pool: &PgPool,
+ records: Option<u32>,
+ parts: u32,
+) -> (PgSlackImportRepo, TeamId, ImportEvent) {
+ let user = Uuid::now_v7();
+ let team = Uuid::now_v7();
+ sqlx::query!("INSERT INTO macro_user (id, username, email, stripe_customer_id) VALUES ($1, 'lifecycle', '[email protected]', 'lifecycle-customer')", user).execute(pool).await.unwrap();
+ sqlx::query!(r#"INSERT INTO "User" (id, email, macro_user_id) VALUES ('macro|[email protected]', '[email protected]', $1)"#, user).execute(pool).await.unwrap();
+ sqlx::query!("INSERT INTO team (id, name, owner_id) VALUES ($1, 'Lifecycle', 'macro|[email protected]')", team).execute(pool).await.unwrap();
+ let team = team.try_into().unwrap();
+ let repo = PgSlackImportRepo::new(pool.clone(), ImportLimits::default());
+ let job = repo
+ .create(
+ team,
+ &MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap(),
+ &CreateImport {
+ idempotency_token: Uuid::now_v7().try_into().unwrap(),
+ source: SourceIdentity::ConfirmedUnknown,
+ include_message_history: records.is_some(),
+ conversations: vec![ConversationMetadata {
+ slack_channel_id: "C1".parse().unwrap(),
+ kind: ConversationKind::PublicChannel,
+ name: "source".into(),
+ folder: "source".parse().unwrap(),
+ member_ids: vec![],
+ creator_id: None,
+ created_at: None,
+ archived: false,
+ message_count: None,
+ }],
+ },
+ &ImportLimits::default(),
+ )
+ .await
+ .unwrap()
+ .job_id;
+ let mut descriptors = vec![UploadDescriptor {
+ upload: UploadId::Users,
+ sha256: "a".repeat(64).parse().unwrap(),
+ byte_length: 10,
+ record_count: None,
+ }];
+ if let Some(records) = records {
+ for part_index in 0..parts {
+ descriptors.push(UploadDescriptor {
+ upload: UploadId::ConversationPart {
+ slack_channel_id: "C1".parse().unwrap(),
+ part_index,
+ },
+ sha256: "b".repeat(64).parse().unwrap(),
+ byte_length: 100,
+ record_count: Some(records),
+ });
+ }
+ }
+ let seal = ConversationSeal::from_descriptors(
+ "C1".parse().unwrap(),
+ &descriptors[1..],
+ &ImportLimits::default(),
+ )
+ .unwrap();
+ let verified: Vec<_> = repo
+ .register(team, job, &RegisterUploads { descriptors })
+ .await
+ .unwrap()
+ .into_iter()
+ .map(|registered| VerifiedUpload {
+ registered,
+ identity: ObjectIdentity::Version("v1".parse().unwrap()),
+ })
+ .collect();
+ repo.complete(team, job, &verified, Some(&seal))
+ .await
+ .unwrap();
+ (
+ repo,
+ team,
+ ImportEvent {
+ job_id: job,
+ slack_channel_id: "C1".parse().unwrap(),
+ generation: 1,
+ },
+ )
+}
+
+pub(crate) async fn claim(repo: &PgSlackImportRepo, event: &ImportEvent) -> ClaimedConversation {
+ let ClaimOutcome::Claimed(claimed) = repo
+ .claim(event, Uuid::now_v7().try_into().unwrap())
+ .await
+ .unwrap()
+ else {
+ panic!("expected claim");
+ };
+ *claimed
+}
+
+pub(crate) async fn expire(pool: &PgPool, event: &ImportEvent) {
+ sqlx::query!("UPDATE slack_import_conversation SET lease_expires_at = clock_timestamp() - interval '1 second' WHERE job_id = $1 AND slack_channel_id = $2", Uuid::from(event.job_id), event.slack_channel_id.as_str()).execute(pool).await.unwrap();
+}
+
+fn assert_error<T>(result: PortResult<T>, expected: ImportError) {
+ match result {
+ Err(error) => assert_eq!(error.into_current_context(), expected),
+ Ok(_) => panic!("expected error"),
+ }
+}
+
+#[sqlx::test(migrations = "../macro_db_client/migrations")]
+async fn claim_heartbeat_reclaim_fences_stale_worker(pool: PgPool) {
+ let (repo, team, event) = fixture(&pool, Some(3)).await;
+ assert_eq!(repo.requester(&event).await.unwrap().unwrap().0, team);
+ assert_eq!(
+ repo.requested_by(team, event.job_id)
+ .await
+ .unwrap()
+ .unwrap()
+ .as_ref(),
+ );
+ assert!(
+ repo.requested_by(Uuid::now_v7().try_into().unwrap(), event.job_id)
+ .await
+ .unwrap()
+ .is_none()
+ );
+ assert!(
+ repo.requested_by(team, Uuid::now_v7().try_into().unwrap())
+ .await
+ .unwrap()
+ .is_none()
+ );
+ let first = claim(&repo, &event).await;
+ assert_eq!(first.parts.len(), 1);
+ assert_eq!(first.checkpoint, Checkpoint::default());
+ assert_eq!(first.lease.attempts, 1);
+ assert!(matches!(
+ repo.claim(&event, Uuid::now_v7().try_into().unwrap())
+ .await
+ .unwrap(),
+ ClaimOutcome::ActiveLease
+ ));
+ let renewed = repo.heartbeat(&first.lease).await.unwrap();
+ assert!(renewed.expires_at >= first.lease.expires_at);
+ assert_eq!(
+ repo.dead_letter(&event).await.unwrap(),
+ WorkerOutcome::Defer
+ );
+ expire(&pool, &event).await;
+ assert_error(repo.heartbeat(&renewed).await, ImportError::LeaseLost);
+ let second = claim(&repo, &event).await;
+ assert_eq!(second.lease.generation, first.lease.generation + 1);
+ assert_eq!(second.lease.attempts, 2);
+ assert_ne!(second.lease.token, first.lease.token);
+ assert_error(
+ repo.settle(&first.lease, ConversationStatus::Failed, None)
+ .await,
+ ImportError::LeaseLost,
+ );
+ assert_error(
+ repo.settle(&second.lease, ConversationStatus::Completed, None)
+ .await,
+ ImportError::Conflict,
+ );
+ repo.settle(
+ &second.lease,
+ ConversationStatus::Failed,
+ Some(ImportError::Internal),
+ )
+ .await
+ .unwrap();
+ let progress = repo.finalize(team, event.job_id).await.unwrap();
+ assert_eq!(progress.status, JobStatus::Failed);
+}
+
+#[sqlx::test(migrations = "../macro_db_client/migrations")]
+async fn finalize_cancel_race_and_duplicate_terminal_updates(pool: PgPool) {
+ let (repo, team, event) = fixture(&pool, None).await;
+ let (finalized, cancelled) = futures::join!(
+ repo.finalize(team, event.job_id),
+ repo.cancel(team, event.job_id)
+ );
+ finalized.unwrap();
+ cancelled.unwrap();
+ let before = repo.progress(team, event.job_id).await.unwrap().unwrap();
+ assert_eq!(before.status, JobStatus::Cancelled);
+ assert_eq!(before.conversations[0].status, ConversationStatus::Skipped);
+ assert_eq!(repo.finalize(team, event.job_id).await.unwrap(), before);
+ assert_eq!(repo.cancel(team, event.job_id).await.unwrap(), before);
+ assert_eq!(
+ repo.dead_letter(&event).await.unwrap(),
+ WorkerOutcome::Acknowledge
+ );
+ repo.mark_published(&event).await.unwrap();
+ assert_eq!(
+ repo.progress(team, event.job_id).await.unwrap().unwrap(),
+ before
+ );
+ assert!(repo.pending_events(50).await.unwrap().is_empty());
+}
+
+#[sqlx::test(migrations = "../macro_db_client/migrations")]
+async fn cancel_claim_race_and_running_completion(pool: PgPool) {
+ let (repo, team, event) = fixture(&pool, None).await;
+ let (claim, cancel) = futures::join!(
+ repo.claim(&event, Uuid::now_v7().try_into().unwrap()),
+ repo.cancel(team, event.job_id)
+ );
+ cancel.unwrap();
+ match claim.unwrap() {
+ ClaimOutcome::Claimed(claimed) => {
+ assert_eq!(
+ repo.progress(team, event.job_id)
+ .await
+ .unwrap()
+ .unwrap()
+ .status,
+ JobStatus::Cancelling
+ );
+ repo.heartbeat(&claimed.lease).await.unwrap();
+ repo.settle(&claimed.lease, ConversationStatus::Completed, None)
+ .await
+ .unwrap();
+ assert_error(
+ repo.settle(&claimed.lease, ConversationStatus::Completed, None)
+ .await,
+ ImportError::LeaseLost,
+ );
+ }
+ ClaimOutcome::Obsolete => (),
+ ClaimOutcome::ActiveLease => panic!("no other worker"),
+ }
+ let progress = repo.progress(team, event.job_id).await.unwrap().unwrap();
+ assert_eq!(progress.status, JobStatus::Cancelled);
+ assert!(matches!(
+ repo.claim(&event, Uuid::now_v7().try_into().unwrap())
+ .await
+ .unwrap(),
+ ClaimOutcome::Obsolete
+ ));
+}
+
+#[sqlx::test(migrations = "../macro_db_client/migrations")]
+async fn lost_publish_ack_recovers_and_reconcile_supersedes_old_dlq(pool: PgPool) {
+ let (repo, _, event) = fixture(&pool, None).await;
+ assert_eq!(repo.pending_events(1).await.unwrap(), vec![event.clone()]);
+ assert!(repo.pending_events(1).await.unwrap().is_empty());
+ sqlx::query!("UPDATE slack_import_outbox SET available_at = clock_timestamp() - interval '1 second' WHERE job_id = $1", Uuid::from(event.job_id)).execute(&pool).await.unwrap();
+ assert_eq!(repo.pending_events(1).await.unwrap(), vec![event.clone()]);
+ repo.mark_published(&event).await.unwrap();
+ repo.mark_published(&event).await.unwrap();
+ assert!(repo.pending_events(1).await.unwrap().is_empty());
+ let old = claim(&repo, &event).await;
+ expire(&pool, &event).await;
+ repo.reconcile(50).await.unwrap();
+ let new = repo.pending_events(1).await.unwrap().pop().unwrap();
+ assert_eq!(new.generation, event.generation + 1);
+ assert_eq!(
+ repo.dead_letter(&event).await.unwrap(),
+ WorkerOutcome::Acknowledge
+ );
+ assert_error(repo.heartbeat(&old.lease).await, ImportError::LeaseLost);
+ let claimed = claim(&repo, &new).await;
+ assert_eq!(claimed.lease.attempts, 2);
+ assert_eq!(repo.dead_letter(&new).await.unwrap(), WorkerOutcome::Defer);
+}
+
+#[sqlx::test(migrations = "../macro_db_client/migrations")]
+async fn cancellation_expired_lease_settles_without_restart(pool: PgPool) {
+ let (repo, team, event) = fixture(&pool, None).await;
+ let running = claim(&repo, &event).await;
+ assert_eq!(
+ repo.cancel(team, event.job_id).await.unwrap().status,
+ JobStatus::Cancelling
+ );
+ expire(&pool, &event).await;
+ repo.reconcile(50).await.unwrap();
+ let progress = repo.progress(team, event.job_id).await.unwrap().unwrap();
+ assert_eq!(progress.status, JobStatus::Cancelled);
+ assert_eq!(
+ progress.conversations[0].status,
+ ConversationStatus::Skipped
+ );
+ assert!(repo.pending_events(50).await.unwrap().is_empty());
+ assert_error(repo.heartbeat(&running.lease).await, ImportError::LeaseLost);
+}
+
+#[sqlx::test(migrations = "../macro_db_client/migrations")]
+async fn retry_budget_and_dlq_are_duplicate_safe(pool: PgPool) {
+ let (repo, team, event) = fixture(&pool, None).await;
+ for attempt in 1..=MAX_ATTEMPTS {
+ let running = claim(&repo, &event).await;
+ assert_eq!(running.lease.attempts, attempt as u32);
+ expire(&pool, &event).await;
+ }
+ assert!(matches!(
+ repo.claim(&event, Uuid::now_v7().try_into().unwrap())
+ .await
+ .unwrap(),
+ ClaimOutcome::Obsolete
+ ));
+ let before = repo.finalize(team, event.job_id).await.unwrap();
+ assert_eq!(before.status, JobStatus::Failed);
+ assert_eq!(
+ repo.dead_letter(&event).await.unwrap(),
+ WorkerOutcome::Acknowledge
+ );
+ assert_eq!(
+ repo.dead_letter(&event).await.unwrap(),
+ WorkerOutcome::Acknowledge
+ );
+ assert_eq!(
+ repo.progress(team, event.job_id).await.unwrap().unwrap(),
+ before
+ );
+}
+
+#[sqlx::test(migrations = "../macro_db_client/migrations")]
+async fn expiry_precedes_s3_lifecycle_and_empty_jobs_settle(pool: PgPool) {
+ let (repo, team, event) = fixture(&pool, None).await;
+ sqlx::query!("UPDATE slack_import_job SET staging_expires_at = clock_timestamp() + interval '12 hours' WHERE id = $1", Uuid::from(event.job_id)).execute(&pool).await.unwrap();
+ assert!(matches!(
+ repo.claim(&event, Uuid::now_v7().try_into().unwrap())
+ .await
+ .unwrap(),
+ ClaimOutcome::Obsolete
+ ));
+ repo.reconcile(50).await.unwrap();
+ assert_eq!(
+ repo.progress(team, event.job_id)
+ .await
+ .unwrap()
+ .unwrap()
+ .status,
+ JobStatus::Failed
+ );
+ let empty = repo
+ .create(
+ team,
+ &MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap(),
+ &CreateImport {
+ idempotency_token: Uuid::now_v7().try_into().unwrap(),
+ source: SourceIdentity::ConfirmedUnknown,
+ include_message_history: false,
+ conversations: vec![],
+ },
+ &ImportLimits::default(),
+ )
+ .await
+ .unwrap();
+ assert_eq!(
+ repo.finalize(team, empty.job_id).await.unwrap().status,
+ JobStatus::Completed
+ );
+ assert_eq!(
+ repo.cancel(team, empty.job_id).await.unwrap().status,
+ JobStatus::Completed
+ );
+}
+
+#[sqlx::test(migrations = "../macro_db_client/migrations")]
+async fn dead_letter_settles_queued_work_once(pool: PgPool) {
+ let (repo, team, event) = fixture(&pool, Some(1)).await;
+ assert_eq!(
+ repo.dead_letter(&event).await.unwrap(),
+ WorkerOutcome::Acknowledge
+ );
+ let before = repo.finalize(team, event.job_id).await.unwrap();
+ assert_eq!(before.status, JobStatus::Failed);
+ assert_eq!(before.conversations[0].status, ConversationStatus::Failed);
+ assert_eq!(
+ repo.dead_letter(&event).await.unwrap(),
+ WorkerOutcome::Acknowledge
+ );
+ assert_eq!(
+ repo.progress(team, event.job_id).await.unwrap().unwrap(),
+ before
+ );
+}
+
+#[sqlx::test(migrations = "../macro_db_client/migrations")]
+async fn expiry_closes_abandoned_uploads_and_expired_importing(pool: PgPool) {
+ let (repo, team, event) = fixture(&pool, Some(1)).await;
+ let running = claim(&repo, &event).await;
+ let abandoned = repo
+ .create(
+ team,
+ &running.requested_by,
+ &CreateImport {
+ idempotency_token: Uuid::now_v7().try_into().unwrap(),
+ source: SourceIdentity::ConfirmedUnknown,
+ include_message_history: true,
+ conversations: vec![running.metadata.clone()],
+ },
+ &ImportLimits::default(),
+ )
+ .await
+ .unwrap()
+ .job_id;
+ sqlx::query!("UPDATE slack_import_job SET staging_expires_at = clock_timestamp() + interval '12 hours' WHERE team_id = $1", Uuid::from(team)).execute(&pool).await.unwrap();
+ assert_error(repo.heartbeat(&running.lease).await, ImportError::LeaseLost);
+ expire(&pool, &event).await;
+ repo.reconcile(50).await.unwrap();
+ let failed = repo.progress(team, event.job_id).await.unwrap().unwrap();
+ assert_eq!(failed.status, JobStatus::Failed);
+ let skipped = repo.progress(team, abandoned).await.unwrap().unwrap();
+ assert_eq!(skipped.status, JobStatus::Completed);
+ assert_eq!(skipped.conversations[0].status, ConversationStatus::Skipped);
+ assert_eq!(
+ skipped.conversations[0].warnings,
+ vec![ImportWarning::UploadsIncomplete]
+ );
+}
+
+#[sqlx::test(migrations = "../macro_db_client/migrations")]
+async fn shape_only_completion_ignores_late_dlq(pool: PgPool) {
+ let (repo, team, event) = fixture(&pool, None).await;
+ let running = claim(&repo, &event).await;
+ repo.settle(&running.lease, ConversationStatus::Completed, None)
+ .await
+ .unwrap();
+ assert_eq!(
+ repo.finalize(team, event.job_id).await.unwrap().status,
+ JobStatus::Completed
+ );
+ assert_eq!(
+ repo.dead_letter(&event).await.unwrap(),
+ WorkerOutcome::Acknowledge
+ );
+}
diff --git a/crates/slack_integration/src/outbound/pg_slack_import_repo/references.rs b/crates/slack_integration/src/outbound/pg_slack_import_repo/references.rs
new file mode 100644
index 0000000000..339511d6e7
--- /dev/null
+++ b/crates/slack_integration/src/outbound/pg_slack_import_repo/references.rs
@@ -0,0 +1,188 @@
+//! Job-scoped bounded templates and transaction-held reconciliation fences.
+//! Locking the job (not a conversation lease) permits cancellation recovery without
+//! reclaiming imports. A crashed transaction releases the fence and rolls back all
+//! work. SKIP LOCKED allows independent maintenance processes to make progress.
+
+use sqlx::{Postgres, Transaction};
+
+use crate::domain::{
+ reference_reconciliation::IMPORTER_BODY_VERSION, slack::references::ConvertedText,
+};
+
+use super::*;
+
+#[cfg(test)]
+mod test;
+
+/// Persist only a newly won canonical source mapping, in its historical transaction.
+/// Duplicate-import losers must never create intents for the winner's live body.
+pub async fn insert_in(
+ tx: &mut Transaction<'_, Postgres>,
+ lease: &Lease,
+ message: &HistoricalMessage,
+) -> PortResult<()> {
+ if message.body_references.is_empty() {
+ return Ok(());
+ }
+ let team = lifecycle::fence(tx, lease).await?;
+ if message.source.team_id != team
+ || message.source.slack_channel_id != lease.event.slack_channel_id
+ {
+ return Err(ImportError::InvalidInput.into());
+ }
+ let template = ConvertedText {
+ body: message.content.clone(),
+ user_mentions: message.user_mentions.clone(),
+ references: message.body_references.clone(),
+ };
+ template
+ .render(&[])
+ .map_err(|_| ImportError::InvalidInput)?;
+ let template = serde_json::to_value(template).map_err(internal)?;
+ if serde_json::to_vec(&template).map_err(internal)?.len() > 4 * 1024 * 1024 {
+ return Err(ImportError::LimitExceeded.into());
+ }
+ sqlx::query!(
+ r#"INSERT INTO slack_import_message_reference
+ (job_id, slack_channel_id, message_id, channel_id, importer_version, template)
+ VALUES ($1, $2, $3, $4, $5, $6)
+ ON CONFLICT (job_id, message_id) DO NOTHING"#,
+ Uuid::from(lease.event.job_id),
+ lease.event.slack_channel_id.as_str(),
+ message.id,
+ message.channel_id,
+ IMPORTER_BODY_VERSION,
+ template,
+ )
+ .execute(&mut **tx)
+ .await
+ .map_err(internal)?;
+ Ok(())
+}
+
+/// Durable immutable input. Message existence/body ownership is checked by its owner.
+pub struct ReferenceWork {
+ /// Job's team namespace.
+ pub team: TeamId,
+ /// Owning job.
+ pub job: JobId,
+ /// Source conversation owning search work.
+ pub conversation: ConversationId,
+ /// Final mapped message, never a discarded candidate UUID.
+ pub message: Uuid,
+ /// Expected owning channel.
+ pub channel: Uuid,
+ /// Importer schema/body guard.
+ pub version: i16,
+ /// None for corrupt/unsupported evidence: close without touching the body.
+ pub template: Option<ConvertedText>,
+}
+
+/// Unforgeable transaction-scoped fence. No lease can expire mid-patch: the job
+/// lock is held through completion and commit, and no work escapes this transaction.
+pub struct FencedReference<'a, 'c> {
+ tx: &'a mut Transaction<'c, Postgres>,
+ work: ReferenceWork,
+}
+
+/// Select one template (at most 4 MiB) after all selected conversations settle.
+/// Registration must close too: uploads can otherwise still create future targets.
+pub async fn next_in<'a, 'c>(
+ tx: &'a mut Transaction<'c, Postgres>,
+) -> PortResult<Option<FencedReference<'a, 'c>>> {
+ let job = sqlx::query!(
+ r#"SELECT j.id, j.team_id FROM slack_import_job j
+ WHERE j.registration_closed_at IS NOT NULL
+ AND j.status IN ('uploading', 'processing', 'cancelling')
+ AND NOT EXISTS (SELECT 1 FROM slack_import_conversation c WHERE c.job_id = j.id
+ AND c.status IN ('awaiting_uploads', 'queued', 'importing'))
+ AND EXISTS (SELECT 1 FROM slack_import_message_reference r
+ WHERE r.job_id = j.id AND r.completed_at IS NULL)
+ ORDER BY j.id LIMIT 1 FOR UPDATE OF j SKIP LOCKED"#,
+ )
+ .fetch_optional(&mut **tx)
+ .await
+ .map_err(internal)?;
+ let Some(job) = job else {
+ return Ok(None);
+ };
+ let row = sqlx::query!(
+ r#"SELECT slack_channel_id, message_id, channel_id, importer_version, template
+ FROM slack_import_message_reference WHERE job_id = $1 AND completed_at IS NULL
+ ORDER BY message_id LIMIT 1"#,
+ job.id,
+ )
+ .fetch_optional(&mut **tx)
+ .await
+ .map_err(internal)?;
+ // Another transaction may have finished the last intent between the candidate
+ // snapshot and our acquiring its job lock.
+ let Some(row) = row else {
+ return Ok(None);
+ };
+ let work = ReferenceWork {
+ team: job.team_id.try_into().map_err(internal)?,
+ job: job.id.try_into().map_err(internal)?,
+ conversation: row.slack_channel_id.parse().map_err(internal)?,
+ message: row.message_id,
+ channel: row.channel_id,
+ version: row.importer_version,
+ template: row
+ .template
+ .and_then(|value| serde_json::from_value(value).ok()),
+ };
+ Ok(Some(FencedReference { tx, work }))
+}
+
+impl<'c> FencedReference<'_, 'c> {
+ /// Immutable durable evidence loaded under the fence.
+ pub fn work(&self) -> &ReferenceWork {
+ &self.work
+ }
+
+ /// Borrow the same transaction for the message owner's guarded body update.
+ pub fn transaction(&mut self) -> &mut Transaction<'c, Postgres> {
+ self.tx
+ }
+
+ /// Close the intent permanently and atomically dirty search if a body changed.
+ /// Clear bulky source evidence; retain a small completion checkpoint until cleanup.
+ pub async fn finish(self, changed: bool) -> PortResult<()> {
+ let job = Uuid::from(self.work.job);
+ sqlx::query!(
+ r#"UPDATE slack_import_message_reference SET template = NULL, completed_at = clock_timestamp()
+ WHERE job_id = $1 AND message_id = $2 AND completed_at IS NULL"#,
+ job, self.work.message,
+ ).execute(&mut **self.tx).await.map_err(internal)?;
+ if changed {
+ let generation = sqlx::query_scalar!(
+ r#"UPDATE slack_import_conversation SET search_dirty_generation = search_dirty_generation + 1,
+ search_state = 'pending', search_receipt_id = NULL, search_submitted_generation = NULL,
+ search_updated_at = clock_timestamp(), updated_at = clock_timestamp()
+ WHERE job_id = $1 AND slack_channel_id = $2 RETURNING search_dirty_generation"#,
+ job, self.work.conversation.as_str(),
+ ).fetch_one(&mut **self.tx).await.map_err(internal)?;
+ sqlx::query!(
+ r#"UPDATE slack_import_outbox SET cancelled_at = clock_timestamp()
+ WHERE job_id = $1 AND slack_channel_id = $2 AND kind = 'search'
+ AND published_at IS NULL AND cancelled_at IS NULL"#,
+ job,
+ self.work.conversation.as_str(),
+ )
+ .execute(&mut **self.tx)
+ .await
+ .map_err(internal)?;
+ sqlx::query!(
+ r#"INSERT INTO slack_import_outbox (job_id, slack_channel_id, kind, generation)
+ VALUES ($1, $2, 'search', $3) ON CONFLICT DO NOTHING"#,
+ job,
+ self.work.conversation.as_str(),
+ generation,
+ )
+ .execute(&mut **self.tx)
+ .await
+ .map_err(internal)?;
+ }
+ lifecycle::recompute(self.tx, self.work.job).await
+ }
+}
diff --git a/crates/slack_integration/src/outbound/pg_slack_import_repo/references/test.rs b/crates/slack_integration/src/outbound/pg_slack_import_repo/references/test.rs
new file mode 100644
index 0000000000..aa4c4848a1
--- /dev/null
+++ b/crates/slack_integration/src/outbound/pg_slack_import_repo/references/test.rs
@@ -0,0 +1,218 @@
+use super::super::lifecycle::test::{claim, expire, fixture};
+use super::*;
+use crate::domain::{
+ ports::{ExecutionRepo, ImportRepo},
+ slack::{mrkdwn::MrkdwnConverter, users::UserDirectory},
+};
+use std::collections::BTreeMap;
+
+async fn intent(pool: &PgPool, context: &ClaimedConversation) -> HistoricalMessage {
+ let channel = Uuid::now_v7();
+ sqlx::query!("INSERT INTO comms_channels (id, name, channel_type, owner_id) VALUES ($1, 'source', 'private', 'macro|[email protected]')", channel).execute(pool).await.unwrap();
+ let text = MrkdwnConverter {
+ users: &UserDirectory::default(),
+ channels: &BTreeMap::new(),
+ }
+ .convert_with_references("<#C1|source>", false)
+ .unwrap();
+ let message = HistoricalMessage {
+ id: Uuid::now_v7(),
+ source: SourceMessageId {
+ team_id: context.team_id,
+ slack_channel_id: context.metadata.slack_channel_id.clone(),
+ ts: "1.000001".parse().unwrap(),
+ },
+ channel_id: channel,
+ parent_id: None,
+ orphaned_thread_ts: None,
+ sender: HistoricalSender::SystemBot,
+ imported_author: Some("source".into()),
+ content: text.body,
+ user_mentions: text.user_mentions,
+ body_references: text.references,
+ import_order: 0,
+ reactions: vec![],
+ };
+ let mut tx = pool.begin().await.unwrap();
+ batches::bind_target(&mut tx, &context.lease, channel)
+ .await
+ .unwrap();
+ insert_in(&mut tx, &context.lease, &message).await.unwrap();
+ tx.commit().await.unwrap();
+ message
+}
+
+#[sqlx::test(migrations = "../macro_db_client/migrations")]
+async fn settlement_waits_for_atomic_references_then_required_search(pool: PgPool) {
+ let (repo, team, event) = fixture(&pool, None).await;
+ let context = claim(&repo, &event).await;
+ intent(&pool, &context).await;
+ repo.finalize(team, event.job_id).await.unwrap();
+ let mut tx = pool.begin().await.unwrap();
+ assert!(next_in(&mut tx).await.unwrap().is_none());
+ tx.rollback().await.unwrap();
+ repo.settle(&context.lease, ConversationStatus::Completed, None)
+ .await
+ .unwrap();
+ assert_eq!(
+ repo.progress(team, event.job_id)
+ .await
+ .unwrap()
+ .unwrap()
+ .status,
+ JobStatus::Processing
+ );
+ let mut tx = pool.begin().await.unwrap();
+ let fenced = next_in(&mut tx).await.unwrap().unwrap();
+ let mut concurrent = pool.begin().await.unwrap();
+ assert!(
+ next_in(&mut concurrent).await.unwrap().is_none(),
+ "job lock fences competing maintenance"
+ );
+ concurrent.rollback().await.unwrap();
+ fenced.finish(true).await.unwrap();
+ tx.rollback().await.unwrap(); // crash after patch/intent/search writes
+ assert!(repo.pending_search(50).await.unwrap().is_empty());
+ let mut restarted = pool.begin().await.unwrap();
+ let fenced = next_in(&mut restarted).await.unwrap().unwrap();
+ fenced.finish(true).await.unwrap();
+ restarted.commit().await.unwrap();
+ assert_eq!(
+ repo.progress(team, event.job_id)
+ .await
+ .unwrap()
+ .unwrap()
+ .status,
+ JobStatus::Processing
+ );
+ let request = repo.pending_search(50).await.unwrap().pop().unwrap();
+ repo.record_search(&request, SearchState::Completed)
+ .await
+ .unwrap();
+ assert_eq!(
+ repo.progress(team, event.job_id)
+ .await
+ .unwrap()
+ .unwrap()
+ .status,
+ JobStatus::Completed
+ );
+ let mut replay = pool.begin().await.unwrap();
+ assert!(next_in(&mut replay).await.unwrap().is_none());
+ replay.rollback().await.unwrap();
+ repo.record_search(&request, SearchState::Failed)
+ .await
+ .unwrap();
+ assert_eq!(
+ repo.progress(team, event.job_id)
+ .await
+ .unwrap()
+ .unwrap()
+ .status,
+ JobStatus::Completed
+ );
+}
+
+#[sqlx::test(migrations = "../macro_db_client/migrations")]
+async fn expired_abandoned_job_retains_resolution_opportunity_without_staging(pool: PgPool) {
+ let (repo, team, event) = fixture(&pool, None).await;
+ let context = claim(&repo, &event).await;
+ intent(&pool, &context).await;
+ sqlx::query!("UPDATE slack_import_job SET staging_expires_at = clock_timestamp() + interval '12 hours' WHERE id = $1", Uuid::from(event.job_id)).execute(&pool).await.unwrap();
+ expire(&pool, &event).await;
+ repo.reconcile(50).await.unwrap();
+ assert_eq!(
+ repo.progress(team, event.job_id)
+ .await
+ .unwrap()
+ .unwrap()
+ .status,
+ JobStatus::Processing
+ );
+ let mut tx = pool.begin().await.unwrap();
+ next_in(&mut tx)
+ .await
+ .unwrap()
+ .unwrap()
+ .finish(false)
+ .await
+ .unwrap();
+ tx.commit().await.unwrap();
+ assert_eq!(
+ repo.progress(team, event.job_id)
+ .await
+ .unwrap()
+ .unwrap()
+ .status,
+ JobStatus::Failed
+ );
+}
+
+#[sqlx::test(migrations = "../macro_db_client/migrations")]
+async fn cancellation_expiry_stale_lease_and_cleanup_close_without_reclaim(pool: PgPool) {
+ let (repo, team, event) = fixture(&pool, None).await;
+ let context = claim(&repo, &event).await;
+ let message = intent(&pool, &context).await;
+ repo.cancel(team, event.job_id).await.unwrap();
+ expire(&pool, &event).await;
+ let mut tx = pool.begin().await.unwrap();
+ assert_eq!(
+ *insert_in(&mut tx, &context.lease, &message)
+ .await
+ .unwrap_err()
+ .current_context(),
+ ImportError::LeaseLost
+ );
+ tx.rollback().await.unwrap();
+ repo.reconcile(50).await.unwrap();
+ assert_eq!(
+ repo.progress(team, event.job_id)
+ .await
+ .unwrap()
+ .unwrap()
+ .status,
+ JobStatus::Cancelling
+ );
+ // Reserve a separate maintenance connection before the obsolete delivery.
+ // Reusing the claim's connection could flush a drop-queued rollback and mask
+ // a lingering job lock from SKIP LOCKED.
+ let mut tx = pool.begin().await.unwrap();
+ assert!(matches!(
+ repo.claim(&event, Uuid::now_v7().try_into().unwrap())
+ .await
+ .unwrap(),
+ ClaimOutcome::Obsolete
+ ));
+ next_in(&mut tx)
+ .await
+ .unwrap()
+ .expect("obsolete claim must release its job lock before returning")
+ .finish(false)
+ .await
+ .unwrap();
+ tx.commit().await.unwrap();
+ assert_eq!(
+ repo.progress(team, event.job_id)
+ .await
+ .unwrap()
+ .unwrap()
+ .status,
+ JobStatus::Cancelled
+ );
+ let cleared = sqlx::query_scalar!("SELECT template IS NULL AS \"cleared!\" FROM slack_import_message_reference WHERE job_id = $1", Uuid::from(event.job_id)).fetch_one(&pool).await.unwrap();
+ assert!(cleared);
+ sqlx::query!(
+ "DELETE FROM slack_import_job WHERE id = $1",
+ Uuid::from(event.job_id)
+ )
+ .execute(&pool)
+ .await
+ .unwrap();
+ assert_eq!(
+ sqlx::query_scalar!("SELECT count(*) FROM slack_import_message_reference")
+ .fetch_one(&pool)
+ .await
+ .unwrap(),
+ Some(0)
+ );
+}
diff --git a/crates/slack_integration/src/outbound/pg_slack_import_repo/uploads.rs b/crates/slack_integration/src/outbound/pg_slack_import_repo/uploads.rs
new file mode 100644
index 0000000000..c3512c440c
--- /dev/null
+++ b/crates/slack_integration/src/outbound/pg_slack_import_repo/uploads.rs
@@ -0,0 +1,362 @@
+//! Immutable descriptors, explicit verification, sealing and atomic readiness.
+
+use super::*;
+
+#[cfg(test)]
+mod test;
+
+struct UploadRow {
+ object_key: String,
+ sha256: String,
+ byte_length: i64,
+ record_count: Option<i32>,
+ verified_version_id: Option<String>,
+ verified_etag: Option<String>,
+}
+
+impl UploadRow {
+ fn registered(&self, upload: UploadId) -> PortResult<RegisteredUpload> {
+ Ok(RegisteredUpload {
+ descriptor: UploadDescriptor {
+ upload,
+ sha256: self.sha256.parse().map_err(internal)?,
+ byte_length: self.byte_length as u64,
+ record_count: self.record_count.map(|n| n as u32),
+ },
+ key: self.object_key.parse().map_err(internal)?,
+ })
+ }
+
+ fn identity(&self) -> PortResult<Option<ObjectIdentity>> {
+ if let Some(version) = &self.verified_version_id {
+ return Ok(Some(ObjectIdentity::Version(
+ version.parse().map_err(internal)?,
+ )));
+ }
+ self.verified_etag
+ .as_ref()
+ .map(|etag| Ok(ObjectIdentity::EntityTag(etag.parse().map_err(internal)?)))
+ .transpose()
+ }
+}
+
+impl PgSlackImportRepo {
+ /// Register a bounded batch atomically, returning persisted server-owned keys.
+ /// Exact retries do not consume additional bytes or change the job revision.
+ pub async fn register(
+ &self,
+ team: TeamId,
+ job: JobId,
+ command: &RegisterUploads,
+ ) -> PortResult<Vec<RegisteredUpload>> {
+ check_batch(command.descriptors.iter().map(|d| &d.upload), &self.limits)?;
+ let mut tx = self.pool.begin().await.map_err(internal)?;
+ let state = lock_job(&mut tx, team, job).await?;
+ state.require_open()?;
+ let mut total = state.registered_bytes as u64;
+ let mut registered = Vec::with_capacity(command.descriptors.len());
+ for descriptor in &command.descriptors {
+ descriptor.validate(&self.limits).map_err(|error| {
+ rootcause::Report::new(error).context(ImportError::LimitExceeded)
+ })?;
+ let existing = find_upload(&mut tx, team, job, &descriptor.upload).await?;
+ if let Some(row) = existing {
+ let upload = row.registered(descriptor.upload.clone())?;
+ if upload.descriptor != *descriptor {
+ return Err(ImportError::Conflict.into());
+ }
+ registered.push(upload);
+ continue;
+ }
+ let (channel, index) = upload_columns(&descriptor.upload)?;
+ if let Some(channel) = channel {
+ let conversation = sqlx::query!(
+ r#"SELECT c.sealed_at FROM slack_import_conversation c
+ JOIN slack_import_job j ON j.id = c.job_id
+ WHERE j.team_id = $1 AND j.id = $2 AND c.slack_channel_id = $3"#,
+ Uuid::from(team),
+ Uuid::from(job),
+ channel,
+ )
+ .fetch_optional(&mut *tx)
+ .await
+ .map_err(internal)?
+ .ok_or(ImportError::Unavailable)?;
+ if conversation.sealed_at.is_some() || !state.include_message_history {
+ return Err(ImportError::Conflict.into());
+ }
+ }
+ total = total
+ .checked_add(descriptor.byte_length)
+ .filter(|n| *n <= self.limits.selected_bytes && *n <= i64::MAX as u64)
+ .ok_or(ImportError::LimitExceeded)?;
+ let key = object_key(team, job, &descriptor.upload)?;
+ let bytes =
+ i64::try_from(descriptor.byte_length).map_err(|_| ImportError::LimitExceeded)?;
+ let records = descriptor
+ .record_count
+ .map(i32::try_from)
+ .transpose()
+ .map_err(|_| ImportError::LimitExceeded)?;
+ sqlx::query!(
+ r#"INSERT INTO slack_import_upload
+ (job_id, slack_channel_id, part_index, object_key, sha256, byte_length, record_count)
+ SELECT id, $3, $4, $5, $6, $7, $8 FROM slack_import_job
+ WHERE team_id = $1 AND id = $2
+ ON CONFLICT (job_id, slack_channel_id, part_index) DO NOTHING"#,
+ Uuid::from(team), Uuid::from(job), channel, index, key.as_str(),
+ descriptor.sha256.as_str(), bytes, records,
+ ).execute(&mut *tx).await.map_err(internal)?;
+ registered.push(RegisteredUpload {
+ descriptor: descriptor.clone(),
+ key,
+ });
+ }
+ if total != state.registered_bytes as u64 {
+ sqlx::query!(
+ "UPDATE slack_import_job SET registered_bytes = $3 WHERE team_id = $1 AND id = $2",
+ Uuid::from(team),
+ Uuid::from(job),
+ total as i64,
+ )
+ .execute(&mut *tx)
+ .await
+ .map_err(internal)?;
+ bump_revision(&mut tx, team, job).await?;
+ }
+ tx.commit().await.map_err(internal)?;
+ Ok(registered)
+ }
+
+ /// Resolve only persisted identities belonging to this team and job.
+ /// No key or key prefix can confer permission to verify an object.
+ pub async fn uploads(
+ &self,
+ team: TeamId,
+ job: JobId,
+ uploads: &[UploadId],
+ ) -> PortResult<Vec<RegisteredUpload>> {
+ check_batch(uploads.iter(), &self.limits)?;
+ let mut tx = self.pool.begin().await.map_err(internal)?;
+ lock_job(&mut tx, team, job).await?;
+ let mut registered = Vec::with_capacity(uploads.len());
+ for upload in uploads {
+ registered.push(
+ find_upload(&mut tx, team, job, upload)
+ .await?
+ .ok_or(ImportError::Unavailable)?
+ .registered(upload.clone())?,
+ );
+ }
+ tx.commit().await.map_err(internal)?;
+ Ok(registered)
+ }
+
+ /// Record storage-verified immutable identities and optionally seal the entire
+ /// manifest. Readiness and unique import events commit in this transaction.
+ /// After closure, only exact completion/seal replays are accepted.
+ pub async fn complete(
+ &self,
+ team: TeamId,
+ job: JobId,
+ verified: &[VerifiedUpload],
+ seal: Option<&ConversationSeal>,
+ ) -> PortResult<ImportProgress> {
+ check_batch(
+ verified.iter().map(|v| &v.registered.descriptor.upload),
+ &self.limits,
+ )?;
+ let mut tx = self.pool.begin().await.map_err(internal)?;
+ let state = lock_job(&mut tx, team, job).await?;
+ let mut changed = false;
+ for upload in verified {
+ let id = &upload.registered.descriptor.upload;
+ let row = find_upload(&mut tx, team, job, id)
+ .await?
+ .ok_or(ImportError::Unavailable)?;
+ if row.registered(id.clone())? != upload.registered {
+ return Err(ImportError::UploadMismatch.into());
+ }
+ if let Some(identity) = row.identity()? {
+ if identity != upload.identity {
+ return Err(ImportError::UploadMismatch.into());
+ }
+ continue;
+ }
+ state.require_open()?;
+ let (version, etag) = match &upload.identity {
+ ObjectIdentity::Version(value) => (Some(value.as_str()), None),
+ ObjectIdentity::EntityTag(value) => (None, Some(value.as_str())),
+ };
+ let (channel, index) = upload_columns(id)?;
+ sqlx::query!(
+ r#"UPDATE slack_import_upload u
+ SET verified_at = clock_timestamp(), verified_version_id = $5, verified_etag = $6
+ FROM slack_import_job j
+ WHERE j.team_id = $1 AND j.id = $2 AND u.job_id = j.id
+ AND u.slack_channel_id IS NOT DISTINCT FROM $3
+ AND u.part_index IS NOT DISTINCT FROM $4 AND u.verified_at IS NULL"#,
+ Uuid::from(team),
+ Uuid::from(job),
+ channel,
+ index,
+ version,
+ etag,
+ )
+ .execute(&mut *tx)
+ .await
+ .map_err(internal)?;
+ changed = true;
+ }
+ if let Some(seal) = seal {
+ changed |= seal_conversation(&mut tx, team, job, &state, seal).await?;
+ }
+ if state.require_open().is_ok() {
+ // A users completion may release many previously sealed conversations.
+ // Sealing alone (including a zero-part seal) cannot bypass users.
+ let queued = sqlx::query!(
+ r#"WITH ready AS (
+ UPDATE slack_import_conversation c
+ SET status = 'queued', event_generation = event_generation + 1, updated_at = clock_timestamp()
+ FROM slack_import_job j
+ WHERE j.team_id = $1 AND j.id = $2 AND c.job_id = j.id
+ AND j.status = 'uploading' AND j.registration_closed_at IS NULL
+ AND j.cancel_requested_at IS NULL
+ AND c.status = 'awaiting_uploads' AND c.sealed_at IS NOT NULL
+ AND EXISTS (SELECT 1 FROM slack_import_upload u
+ WHERE u.job_id = j.id AND u.slack_channel_id IS NULL AND u.verified_at IS NOT NULL)
+ AND c.part_count = (SELECT count(*) FROM slack_import_upload u
+ WHERE u.job_id = c.job_id AND u.slack_channel_id = c.slack_channel_id
+ AND u.verified_at IS NOT NULL)
+ RETURNING c.job_id, c.slack_channel_id, c.event_generation
+ )
+ INSERT INTO slack_import_outbox (job_id, slack_channel_id, kind, generation)
+ SELECT job_id, slack_channel_id, 'import', event_generation FROM ready
+ ON CONFLICT (job_id, slack_channel_id, kind, generation) DO NOTHING"#,
+ Uuid::from(team), Uuid::from(job),
+ ).execute(&mut *tx).await.map_err(internal)?;
+ changed |= queued.rows_affected() > 0;
+ }
+ if changed {
+ bump_revision(&mut tx, team, job).await?;
+ }
+ let progress = self.receipt(&mut tx, team, job).await?;
+ tx.commit().await.map_err(internal)?;
+ Ok(progress)
+ }
+}
+
+async fn find_upload(
+ connection: &mut PgConnection,
+ team: TeamId,
+ job: JobId,
+ upload: &UploadId,
+) -> PortResult<Option<UploadRow>> {
+ let (channel, index) = upload_columns(upload)?;
+ sqlx::query_as!(
+ UploadRow,
+ r#"SELECT u.object_key, u.sha256, u.byte_length, u.record_count, u.verified_version_id, u.verified_etag
+ FROM slack_import_upload u JOIN slack_import_job j ON j.id = u.job_id
+ WHERE j.team_id = $1 AND j.id = $2
+ AND u.slack_channel_id IS NOT DISTINCT FROM $3 AND u.part_index IS NOT DISTINCT FROM $4"#,
+ Uuid::from(team), Uuid::from(job), channel, index,
+ ).fetch_optional(connection).await.map_err(internal)
+}
+
+async fn seal_conversation(
+ connection: &mut PgConnection,
+ team: TeamId,
+ job: JobId,
+ state: &LockedJob,
+ seal: &ConversationSeal,
+) -> PortResult<bool> {
+ let row = sqlx::query!(
+ r#"SELECT c.part_count, c.manifest_sha256 FROM slack_import_conversation c
+ JOIN slack_import_job j ON j.id = c.job_id
+ WHERE j.team_id = $1 AND j.id = $2 AND c.slack_channel_id = $3"#,
+ Uuid::from(team),
+ Uuid::from(job),
+ seal.slack_channel_id.as_str(),
+ )
+ .fetch_optional(&mut *connection)
+ .await
+ .map_err(internal)?
+ .ok_or(ImportError::Unavailable)?;
+ if let Some(count) = row.part_count {
+ if count as u32 != seal.part_count
+ || row.manifest_sha256.as_deref() != Some(seal.manifest_sha256.as_str())
+ {
+ return Err(ImportError::Conflict.into());
+ }
+ return Ok(false);
+ }
+ state.require_open()?;
+ // Aggregate the complete stored set rather than trusting a completion batch.
+ // The schema trigger independently validates this same canonical digest.
+ let manifest = sqlx::query!(
+ r#"SELECT count(*) AS "count!", max(u.part_index) AS last_index,
+ encode(digest(coalesce(string_agg(
+ u.part_index::text || ':' || u.sha256 || ':' || u.byte_length::text || ':' || u.record_count::text || E'\n',
+ '' ORDER BY u.part_index), ''), 'sha256'), 'hex') AS "sha256!"
+ FROM slack_import_upload u JOIN slack_import_job j ON j.id = u.job_id
+ WHERE j.team_id = $1 AND j.id = $2 AND u.slack_channel_id = $3"#,
+ Uuid::from(team), Uuid::from(job), seal.slack_channel_id.as_str(),
+ ).fetch_one(&mut *connection).await.map_err(internal)?;
+ if manifest.count != i64::from(seal.part_count)
+ || (manifest.count > 0 && manifest.last_index.map(i64::from) != Some(manifest.count - 1))
+ || manifest.sha256 != seal.manifest_sha256.as_str()
+ {
+ return Err(ImportError::Conflict.into());
+ }
+ let count = i32::try_from(seal.part_count).map_err(|_| ImportError::LimitExceeded)?;
+ sqlx::query!(
+ r#"UPDATE slack_import_conversation c
+ SET part_count = $4, manifest_sha256 = $5, sealed_at = clock_timestamp(), updated_at = clock_timestamp()
+ FROM slack_import_job j
+ WHERE j.team_id = $1 AND j.id = $2 AND c.job_id = j.id AND c.slack_channel_id = $3"#,
+ Uuid::from(team), Uuid::from(job), seal.slack_channel_id.as_str(), count, seal.manifest_sha256.as_str(),
+ ).execute(connection).await.map_err(internal)?;
+ Ok(true)
+}
+
+fn upload_columns(upload: &UploadId) -> PortResult<(Option<&str>, Option<i32>)> {
+ match upload {
+ UploadId::Users => Ok((None, None)),
+ UploadId::ConversationPart {
+ slack_channel_id,
+ part_index,
+ } => Ok((
+ Some(slack_channel_id.as_str()),
+ Some(i32::try_from(*part_index).map_err(|_| ImportError::LimitExceeded)?),
+ )),
+ }
+}
+
+fn object_key(team: TeamId, job: JobId, upload: &UploadId) -> PortResult<ObjectKey> {
+ let suffix = match upload {
+ UploadId::Users => "users.json".to_owned(),
+ UploadId::ConversationPart {
+ slack_channel_id,
+ part_index,
+ } => format!("{slack_channel_id}/{part_index}.ndjson"),
+ };
+ format!("slack-import/{team}/{job}/{suffix}")
+ .parse()
+ .map_err(internal)
+}
+
+fn check_batch<'a>(
+ uploads: impl Iterator<Item = &'a UploadId>,
+ limits: &ImportLimits,
+) -> PortResult<()> {
+ let mut seen = HashSet::new();
+ for upload in uploads {
+ if !seen.insert(upload) {
+ return Err(ImportError::InvalidInput.into());
+ }
+ if seen.len() > limits.registration_batch.min(50) as usize {
+ return Err(ImportError::LimitExceeded.into());
+ }
+ }
+ Ok(())
+}
diff --git a/crates/slack_integration/src/outbound/pg_slack_import_repo/uploads/test.rs b/crates/slack_integration/src/outbound/pg_slack_import_repo/uploads/test.rs
new file mode 100644
index 0000000000..e0bbe0ea59
--- /dev/null
+++ b/crates/slack_integration/src/outbound/pg_slack_import_repo/uploads/test.rs
@@ -0,0 +1,961 @@
+use super::*;
+
+const ADMIN: &str = "macro|[email protected]";
+
+fn admin() -> MacroUserIdStr<'static> {
+ MacroUserIdStr::parse_from_str(ADMIN).unwrap()
+}
+
+async fn fixture(pool: &PgPool) -> (PgSlackImportRepo, TeamId, TeamId) {
+ let user = Uuid::now_v7();
+ let team = Uuid::now_v7();
+ let foreign = Uuid::now_v7();
+ sqlx::query!(
+ "INSERT INTO macro_user (id, username, email, stripe_customer_id) VALUES ($1, 'archive', '[email protected]', 'archive-customer'), ($2, 'foreign', '[email protected]', 'foreign-customer')",
+ user, foreign,
+ ).execute(pool).await.unwrap();
+ sqlx::query!(
+ r#"INSERT INTO "User" (id, email, macro_user_id) VALUES ($1, '[email protected]', $2), ('macro|[email protected]', '[email protected]', $3)"#,
+ ADMIN,
+ user,
+ foreign,
+ )
+ .execute(pool)
+ .await
+ .unwrap();
+ sqlx::query!(
+ "INSERT INTO team (id, name, owner_id) VALUES ($1, 'Archive', $3), ($2, 'Foreign', 'macro|[email protected]')",
+ team,
+ foreign,
+ ADMIN,
+ )
+ .execute(pool)
+ .await
+ .unwrap();
+ (
+ PgSlackImportRepo::new(pool.clone(), ImportLimits::default()),
+ team.try_into().unwrap(),
+ foreign.try_into().unwrap(),
+ )
+}
+
+fn metadata(id: &str) -> ConversationMetadata {
+ ConversationMetadata {
+ slack_channel_id: id.parse().unwrap(),
+ kind: ConversationKind::PrivateChannel,
+ name: "Source name".into(),
+ folder: "source-folder".parse().unwrap(),
+ member_ids: vec!["U2".parse().unwrap(), "U1".parse().unwrap()],
+ creator_id: Some("U2".parse().unwrap()),
+ created_at: Some("1700000000.000001".parse().unwrap()),
+ archived: true,
+ message_count: Some(12),
+ }
+}
+
+fn command(history: bool, conversations: &[&str]) -> CreateImport {
+ CreateImport {
+ idempotency_token: Uuid::now_v7().try_into().unwrap(),
+ source: SourceIdentity::ConfirmedUnknown,
+ include_message_history: history,
+ conversations: conversations.iter().map(|id| metadata(id)).collect(),
+ }
+}
+
+async fn create(
+ repo: &PgSlackImportRepo,
+ team: TeamId,
+ history: bool,
+ conversations: &[&str],
+) -> JobId {
+ repo.create(
+ team,
+ &admin(),
+ &command(history, conversations),
+ &repo.limits,
+ )
+ .await
+ .unwrap()
+ .job_id
+}
+
+fn descriptor(upload: UploadId) -> UploadDescriptor {
+ let record_count = match upload {
+ UploadId::Users => None,
+ _ => Some(1),
+ };
+ UploadDescriptor {
+ upload,
+ sha256: "a".repeat(64).parse().unwrap(),
+ byte_length: 10,
+ record_count,
+ }
+}
+
+fn part(index: u32) -> UploadDescriptor {
+ descriptor(UploadId::ConversationPart {
+ slack_channel_id: "C1".parse().unwrap(),
+ part_index: index,
+ })
+}
+
+fn seal(parts: &[UploadDescriptor]) -> ConversationSeal {
+ ConversationSeal::from_descriptors("C1".parse().unwrap(), parts, &ImportLimits::default())
+ .unwrap()
+}
+
+async fn register(
+ repo: &PgSlackImportRepo,
+ team: TeamId,
+ job: JobId,
+ descriptors: &[UploadDescriptor],
+) -> Vec<VerifiedUpload> {
+ repo.register(
+ team,
+ job,
+ &RegisterUploads {
+ descriptors: descriptors.to_vec(),
+ },
+ )
+ .await
+ .unwrap()
+ .into_iter()
+ .map(|registered| VerifiedUpload {
+ registered,
+ identity: ObjectIdentity::Version("version-one".parse().unwrap()),
+ })
+ .collect()
+}
+
+fn error<T: std::fmt::Debug>(result: PortResult<T>, expected: ImportError) {
+ assert_eq!(result.unwrap_err().into_current_context(), expected);
+}
+
+async fn outbox_count(pool: &PgPool, job: JobId) -> i64 {
+ sqlx::query_scalar!(
+ "SELECT count(*) AS \"count!\" FROM slack_import_outbox WHERE job_id = $1",
+ Uuid::from(job)
+ )
+ .fetch_one(pool)
+ .await
+ .unwrap()
+}
+
+#[sqlx::test(migrations = "../macro_db_client/migrations")]
+async fn create_replays_semantic_metadata_and_scopes_tokens(pool: PgPool) {
+ let (repo, team, foreign) = fixture(&pool).await;
+ let mut command = command(true, &["C2", "C1"]);
+ let original = repo
+ .create(team, &admin(), &command, &repo.limits)
+ .await
+ .unwrap();
+ command.conversations.reverse();
+ for metadata in &mut command.conversations {
+ metadata.member_ids.reverse();
+ }
+ assert_eq!(
+ repo.create(team, &admin(), &command, &repo.limits)
+ .await
+ .unwrap(),
+ original
+ );
+ let other = repo
+ .create(foreign, &admin(), &command, &repo.limits)
+ .await
+ .unwrap();
+ assert_ne!(original.job_id, other.job_id);
+ let row = sqlx::query!(
+ r#"SELECT kind, name, folder, member_ids, creator_id, source_created_at, archived, message_count
+ FROM slack_import_conversation WHERE job_id = $1 AND slack_channel_id = 'C1'"#,
+ Uuid::from(original.job_id),
+ ).fetch_one(&pool).await.unwrap();
+ assert_eq!(row.kind, "private_channel");
+ assert_eq!(row.name, "Source name");
+ assert_eq!(row.folder, "source-folder");
+ assert_eq!(row.member_ids, ["U1", "U2"]);
+ assert_eq!(row.creator_id.as_deref(), Some("U2"));
+ assert_eq!(row.source_created_at, Some(1700000000000001));
+ assert!(row.archived);
+ assert_eq!(row.message_count, Some(12));
+ command.conversations[0].name = "changed".into();
+ error(
+ repo.create(team, &admin(), &command, &repo.limits).await,
+ ImportError::Conflict,
+ );
+ let mut duplicate = command.clone();
+ duplicate
+ .conversations
+ .push(duplicate.conversations[0].clone());
+ error(
+ repo.create(team, &admin(), &duplicate, &repo.limits).await,
+ ImportError::InvalidInput,
+ );
+}
+
+#[sqlx::test(migrations = "../macro_db_client/migrations")]
+async fn confirmed_selection_is_the_durable_upload_and_work_scope(pool: PgPool) {
+ use crate::domain::ports::{ExecutionRepo, ImportRepo, SourceMessageReader};
+
+ let (repo, team, _) = fixture(&pool).await;
+ let mut selected = command(true, &["CA", "CC"]);
+ for metadata in &mut selected.conversations {
+ metadata.folder = metadata.slack_channel_id.as_str().parse().unwrap();
+ }
+ let original = repo
+ .create(team, &admin(), &selected, &repo.limits)
+ .await
+ .unwrap();
+ let job = original.job_id;
+ // Same names do not collapse distinct source IDs. The receipt survives without the ZIP.
+ assert_eq!(original.source, selected.source);
+ assert!(original.include_message_history);
+ assert_eq!(
+ original
+ .conversations
+ .iter()
+ .map(|c| c.slack_channel_id.as_str())
+ .collect::<Vec<_>>(),
+ ["CA", "CC"]
+ );
+ assert!(
+ original
+ .conversations
+ .iter()
+ .all(|c| c.name == "Source name"
+ && c.kind == ConversationKind::PrivateChannel
+ && c.archived)
+ );
+ assert_eq!(repo.progress(team, job).await.unwrap().unwrap(), original);
+ assert_eq!(repo.list(team, None).await.unwrap(), [original.clone()]);
+ for altered in [
+ CreateImport {
+ include_message_history: false,
+ ..selected.clone()
+ },
+ CreateImport {
+ conversations: vec![metadata("CA"), metadata("CB")],
+ ..selected.clone()
+ },
+ ] {
+ error(
+ repo.create(team, &admin(), &altered, &repo.limits).await,
+ ImportError::Conflict,
+ );
+ }
+ assert_eq!(
+ repo.create(team, &admin(), &selected, &repo.limits)
+ .await
+ .unwrap(),
+ original
+ );
+
+ let unselected = descriptor(UploadId::ConversationPart {
+ slack_channel_id: "CB".parse().unwrap(),
+ part_index: 0,
+ });
+ error(
+ repo.register(
+ team,
+ job,
+ &RegisterUploads {
+ descriptors: vec![unselected.clone()],
+ },
+ )
+ .await,
+ ImportError::Unavailable,
+ );
+ error(
+ repo.uploads(team, job, &[unselected.upload.clone()]).await,
+ ImportError::Unavailable,
+ );
+ let forged = VerifiedUpload {
+ registered: RegisteredUpload {
+ key: object_key(team, job, &unselected.upload).unwrap(),
+ descriptor: unselected,
+ },
+ identity: ObjectIdentity::Version("forged".parse().unwrap()),
+ };
+ error(
+ repo.complete(team, job, &[forged], None).await,
+ ImportError::Unavailable,
+ );
+ let unselected_seal =
+ ConversationSeal::from_descriptors("CB".parse().unwrap(), &[], &repo.limits).unwrap();
+ error(
+ repo.complete(team, job, &[], Some(&unselected_seal)).await,
+ ImportError::Unavailable,
+ );
+ let event = ImportEvent {
+ job_id: job,
+ slack_channel_id: "CB".parse().unwrap(),
+ generation: 1,
+ };
+ assert!(repo.requester(&event).await.unwrap().is_none());
+ assert!(matches!(
+ repo.claim(&event, Uuid::now_v7().try_into().unwrap())
+ .await
+ .unwrap(),
+ ClaimOutcome::Obsolete
+ ));
+ assert_eq!(outbox_count(&pool, job).await, 0);
+
+ // Looking up a link to B cannot turn it into selected/pending work or invent a message.
+ assert!(
+ repo.pending_reference_channels(team, job, &["CB".parse().unwrap()])
+ .await
+ .unwrap()
+ .is_empty()
+ );
+ assert!(
+ repo.reference_mappings(
+ team,
+ &[SourceMessageId {
+ team_id: team,
+ slack_channel_id: "CB".parse().unwrap(),
+ ts: "1700000000.000001".parse().unwrap(),
+ }]
+ )
+ .await
+ .unwrap()
+ .is_empty()
+ );
+ assert_eq!(repo.progress(team, job).await.unwrap().unwrap(), original);
+
+ // Empty selected history still requires users. Only A becomes ready; C remains a selected skip.
+ let seal =
+ ConversationSeal::from_descriptors("CA".parse().unwrap(), &[], &repo.limits).unwrap();
+ repo.complete(team, job, &[], Some(&seal)).await.unwrap();
+ assert_eq!(outbox_count(&pool, job).await, 0);
+ let users = register(&repo, team, job, &[descriptor(UploadId::Users)]).await;
+ repo.complete(team, job, &users, None).await.unwrap();
+ let finalized = repo.finalize(team, job).await.unwrap();
+ assert_eq!(outbox_count(&pool, job).await, 1);
+ assert_eq!(finalized.conversations.len(), 2);
+ assert_eq!(
+ finalized.conversations[0].status,
+ ConversationStatus::Queued
+ );
+ assert_eq!(
+ finalized.conversations[1].status,
+ ConversationStatus::Skipped
+ );
+ assert_eq!(
+ finalized.conversations[1].warnings,
+ [ImportWarning::UploadsIncomplete]
+ );
+ assert_eq!(repo.progress(team, job).await.unwrap().unwrap(), finalized);
+}
+
+#[sqlx::test(migrations = "../macro_db_client/migrations")]
+async fn source_binding_and_job_commit_atomically(pool: PgPool) {
+ let (repo, team, _) = fixture(&pool).await;
+ let mut known = command(true, &["C1"]);
+ known.source = SourceIdentity::Known {
+ source_id: "T1".parse().unwrap(),
+ };
+ let admin = admin();
+ let (first, replay) = futures::join!(
+ repo.create(team, &admin, &known, &repo.limits),
+ repo.create(team, &admin, &known, &repo.limits),
+ );
+ assert_eq!(first.unwrap().job_id, replay.unwrap().job_id);
+ let mut mismatch = known.clone();
+ mismatch.idempotency_token = Uuid::now_v7().try_into().unwrap();
+ mismatch.source = SourceIdentity::Known {
+ source_id: "T2".parse().unwrap(),
+ };
+ error(
+ repo.create(team, &admin, &mismatch, &repo.limits).await,
+ ImportError::SourceMismatch,
+ );
+ assert_eq!(repo.list(team, None).await.unwrap().len(), 1);
+ // An exact create retry stays readable after registration closes.
+ let job = repo.list(team, None).await.unwrap()[0].job_id;
+ close(&pool, job).await;
+ assert_eq!(
+ repo.create(team, &admin, &known, &repo.limits)
+ .await
+ .unwrap()
+ .job_id,
+ job
+ );
+}
+
+#[sqlx::test(migrations = "../macro_db_client/migrations")]
+async fn list_is_bounded_exclusive_and_team_scoped(pool: PgPool) {
+ let (repo, team, foreign) = fixture(&pool).await;
+ let mut ids = Vec::new();
+ for _ in 0..52 {
+ ids.push(create(&repo, team, false, &[]).await);
+ }
+ let other = create(&repo, foreign, false, &[]).await;
+ let first = repo.list(team, None).await.unwrap();
+ assert_eq!(first.len(), 50);
+ ids.reverse();
+ assert_eq!(
+ first.iter().map(|p| p.job_id).collect::<Vec<_>>(),
+ ids[..50]
+ );
+ let next = repo.list(team, Some(first[49].job_id)).await.unwrap();
+ assert_eq!(next.iter().map(|p| p.job_id).collect::<Vec<_>>(), ids[50..]);
+ assert!(repo.progress(team, other).await.unwrap().is_none());
+ assert!(first.iter().all(|p| p.conversations.is_empty()));
+}
+
+#[sqlx::test(migrations = "../macro_db_client/migrations")]
+async fn completion_requires_seal_and_users_uploaded_last_releases_all(pool: PgPool) {
+ let (repo, team, _) = fixture(&pool).await;
+ let job = create(&repo, team, true, &["C1", "C2"]).await;
+ let parts = [part(0), part(1)];
+ let uploads = register(&repo, team, job, &parts).await;
+ let progress = repo.complete(team, job, &uploads, None).await.unwrap();
+ assert_eq!(progress.conversations[0].verified_parts, 2);
+ assert_eq!(
+ progress.conversations[0].status,
+ ConversationStatus::AwaitingUploads
+ );
+ let progress = repo
+ .complete(team, job, &[], Some(&seal(&parts)))
+ .await
+ .unwrap();
+ assert_eq!(
+ progress.conversations[0].status,
+ ConversationStatus::AwaitingUploads
+ );
+ let empty =
+ ConversationSeal::from_descriptors("C2".parse().unwrap(), &[], &repo.limits).unwrap();
+ repo.complete(team, job, &[], Some(&empty)).await.unwrap();
+ assert_eq!(outbox_count(&pool, job).await, 0);
+ let users = register(&repo, team, job, &[descriptor(UploadId::Users)]).await;
+ let progress = repo.complete(team, job, &users, None).await.unwrap();
+ assert!(progress.users_verified);
+ assert!(
+ progress
+ .conversations
+ .iter()
+ .all(|c| c.status == ConversationStatus::Queued)
+ );
+ assert_eq!(outbox_count(&pool, job).await, 2);
+ assert_eq!(
+ repo.complete(team, job, &users, None).await.unwrap(),
+ progress
+ );
+ assert_eq!(
+ repo.complete(team, job, &uploads, Some(&seal(&parts)))
+ .await
+ .unwrap(),
+ progress
+ );
+}
+
+#[sqlx::test(migrations = "../macro_db_client/migrations")]
+async fn seals_reject_missing_middle_wrong_hash_and_truncated_sets_atomically(pool: PgPool) {
+ let (repo, team, _) = fixture(&pool).await;
+ let job = create(&repo, team, true, &["C1"]).await;
+ let parts = [part(0), part(1), part(2)];
+ let uploads = register(&repo, team, job, &[parts[0].clone(), parts[2].clone()]).await;
+ error(
+ repo.complete(team, job, &uploads, Some(&seal(&parts)))
+ .await,
+ ImportError::Conflict,
+ );
+ // Invalid seals roll back object verification too.
+ assert_eq!(
+ repo.progress(team, job)
+ .await
+ .unwrap()
+ .unwrap()
+ .conversations[0]
+ .verified_parts,
+ 0
+ );
+ register(&repo, team, job, &[part(1)]).await;
+ error(
+ repo.complete(team, job, &[], Some(&seal(&parts[..2])))
+ .await,
+ ImportError::Conflict,
+ );
+ let mut wrong_hash = seal(&parts);
+ wrong_hash.manifest_sha256 = "b".repeat(64).parse().unwrap();
+ error(
+ repo.complete(team, job, &[], Some(&wrong_hash)).await,
+ ImportError::Conflict,
+ );
+ repo.complete(team, job, &uploads, Some(&seal(&parts)))
+ .await
+ .unwrap();
+ let users = register(&repo, team, job, &[descriptor(UploadId::Users)]).await;
+ let waiting = repo.complete(team, job, &users, None).await.unwrap();
+ assert_eq!(
+ waiting.conversations[0].status,
+ ConversationStatus::AwaitingUploads
+ );
+ let middle = register(&repo, team, job, &[part(1)]).await;
+ let ready = repo.complete(team, job, &middle, None).await.unwrap();
+ assert_eq!(ready.conversations[0].status, ConversationStatus::Queued);
+}
+
+#[sqlx::test(migrations = "../macro_db_client/migrations")]
+async fn descriptors_and_seals_are_immutable_but_exact_retries_succeed(pool: PgPool) {
+ let (repo, team, _) = fixture(&pool).await;
+ let job = create(&repo, team, true, &["C1"]).await;
+ let original = register(&repo, team, job, &[part(0)]).await;
+ let progress = repo.progress(team, job).await.unwrap().unwrap();
+ assert_eq!(register(&repo, team, job, &[part(0)]).await, original);
+ assert_eq!(repo.progress(team, job).await.unwrap().unwrap(), progress);
+ let mut changed_hash = part(0);
+ changed_hash.sha256 = "b".repeat(64).parse().unwrap();
+ let mut changed_size = part(0);
+ changed_size.byte_length += 1;
+ let mut changed_records = part(0);
+ changed_records.record_count = Some(2);
+ for descriptor in [changed_hash, changed_size, changed_records] {
+ error(
+ repo.register(
+ team,
+ job,
+ &RegisterUploads {
+ descriptors: vec![descriptor],
+ },
+ )
+ .await,
+ ImportError::Conflict,
+ );
+ }
+ repo.complete(team, job, &[], Some(&seal(&[part(0)])))
+ .await
+ .unwrap();
+ assert_eq!(register(&repo, team, job, &[part(0)]).await, original);
+ error(
+ repo.register(
+ team,
+ job,
+ &RegisterUploads {
+ descriptors: vec![part(1)],
+ },
+ )
+ .await,
+ ImportError::Conflict,
+ );
+ error(
+ repo.complete(team, job, &[], Some(&seal(&[]))).await,
+ ImportError::Conflict,
+ );
+}
+
+#[sqlx::test(migrations = "../macro_db_client/migrations")]
+async fn zero_part_shape_and_empty_history_still_require_users(pool: PgPool) {
+ let (repo, team, _) = fixture(&pool).await;
+ for history in [false, true] {
+ let job = create(&repo, team, history, &["C1"]).await;
+ if !history {
+ error(
+ repo.register(
+ team,
+ job,
+ &RegisterUploads {
+ descriptors: vec![part(0)],
+ },
+ )
+ .await,
+ ImportError::Conflict,
+ );
+ }
+ let progress = repo
+ .complete(team, job, &[], Some(&seal(&[])))
+ .await
+ .unwrap();
+ assert_eq!(progress.conversations[0].part_count, Some(0));
+ assert_eq!(
+ progress.conversations[0].status,
+ ConversationStatus::AwaitingUploads
+ );
+ let users = register(&repo, team, job, &[descriptor(UploadId::Users)]).await;
+ let progress = repo.complete(team, job, &users, None).await.unwrap();
+ assert_eq!(progress.conversations[0].status, ConversationStatus::Queued);
+ assert_eq!(outbox_count(&pool, job).await, 1);
+ assert!(repo.uploads(team, job, &[part(0).upload]).await.is_err());
+ }
+ let empty_job = create(&repo, team, false, &[]).await;
+ let users = register(&repo, team, empty_job, &[descriptor(UploadId::Users)]).await;
+ assert!(
+ repo.complete(team, empty_job, &users, None)
+ .await
+ .unwrap()
+ .conversations
+ .is_empty()
+ );
+ assert_eq!(outbox_count(&pool, empty_job).await, 0);
+}
+
+#[sqlx::test(migrations = "../macro_db_client/migrations")]
+async fn keys_do_not_authorize_foreign_jobs_teams_or_part_identities(pool: PgPool) {
+ let (repo, team, foreign) = fixture(&pool).await;
+ let job = create(&repo, team, true, &["C1"]).await;
+ let other_job = create(&repo, team, true, &["C1"]).await;
+ let uploads = register(
+ &repo,
+ team,
+ job,
+ &[descriptor(UploadId::Users), part(0), part(1)],
+ )
+ .await;
+ register(
+ &repo,
+ team,
+ other_job,
+ &[descriptor(UploadId::Users), part(0)],
+ )
+ .await;
+ error(
+ repo.complete(team, other_job, &uploads[..1], None).await,
+ ImportError::UploadMismatch,
+ );
+ error(
+ repo.complete(foreign, job, &uploads, None).await,
+ ImportError::Unavailable,
+ );
+ error(
+ repo.register(
+ foreign,
+ job,
+ &RegisterUploads {
+ descriptors: vec![part(0)],
+ },
+ )
+ .await,
+ ImportError::Unavailable,
+ );
+ error(
+ repo.uploads(foreign, job, &[UploadId::Users]).await,
+ ImportError::Unavailable,
+ );
+ error(
+ repo.complete(foreign, job, &[], Some(&seal(&[part(0), part(1)])))
+ .await,
+ ImportError::Unavailable,
+ );
+ for replacement in [UploadId::Users, part(1).upload] {
+ let mut changed = uploads[1].clone();
+ changed.registered.descriptor.upload = replacement;
+ error(
+ repo.complete(team, job, &[changed], None).await,
+ ImportError::UploadMismatch,
+ );
+ }
+ let mut forged = uploads[1].clone();
+ forged.registered.key = format!("slack-import/{team}/{job}/unregistered.ndjson")
+ .parse()
+ .unwrap();
+ error(
+ repo.complete(team, job, &[forged], None).await,
+ ImportError::UploadMismatch,
+ );
+ error(
+ repo.complete(
+ team,
+ job,
+ &[],
+ Some(&ConversationSeal {
+ slack_channel_id: "C2".parse().unwrap(),
+ ..seal(&[])
+ }),
+ )
+ .await,
+ ImportError::Unavailable,
+ );
+}
+
+#[sqlx::test(migrations = "../macro_db_client/migrations")]
+async fn verification_pins_cannot_change_and_duplicate_completion_is_stable(pool: PgPool) {
+ let (repo, team, _) = fixture(&pool).await;
+ let job = create(&repo, team, true, &["C1"]).await;
+ let mut users = register(&repo, team, job, &[descriptor(UploadId::Users)]).await;
+ users[0].identity = ObjectIdentity::EntityTag("opaque-etag".parse().unwrap());
+ let first = repo
+ .complete(team, job, &users, Some(&seal(&[])))
+ .await
+ .unwrap();
+ assert_eq!(
+ repo.complete(team, job, &users, Some(&seal(&[])))
+ .await
+ .unwrap(),
+ first
+ );
+ let mut changed = users[0].clone();
+ changed.identity = ObjectIdentity::Version("version-two".parse().unwrap());
+ error(
+ repo.complete(team, job, &[changed], None).await,
+ ImportError::UploadMismatch,
+ );
+ assert_eq!(outbox_count(&pool, job).await, 1);
+}
+
+#[sqlx::test(migrations = "../macro_db_client/migrations")]
+async fn concurrent_readiness_publishes_one_event_and_counts_bytes_once(pool: PgPool) {
+ let (repo, team, _) = fixture(&pool).await;
+ let job = create(&repo, team, true, &["C1"]).await;
+ let command = RegisterUploads {
+ descriptors: vec![descriptor(UploadId::Users), part(0)],
+ };
+ let (one, two) = futures::join!(
+ repo.register(team, job, &command),
+ repo.register(team, job, &command)
+ );
+ assert_eq!(one.unwrap(), two.unwrap());
+ let bytes = sqlx::query_scalar!(
+ "SELECT registered_bytes FROM slack_import_job WHERE id = $1",
+ Uuid::from(job)
+ )
+ .fetch_one(&pool)
+ .await
+ .unwrap();
+ assert_eq!(bytes, 20);
+ let uploads = register(&repo, team, job, &command.descriptors).await;
+ let seal = seal(&[part(0)]);
+ let (one, two) = futures::join!(
+ repo.complete(team, job, &uploads, Some(&seal)),
+ repo.complete(team, job, &uploads, Some(&seal)),
+ );
+ assert_eq!(one.unwrap(), two.unwrap());
+ assert_eq!(outbox_count(&pool, job).await, 1);
+ let generation = sqlx::query_scalar!("SELECT event_generation FROM slack_import_conversation WHERE job_id = $1 AND slack_channel_id = 'C1'", Uuid::from(job))
+ .fetch_one(&pool).await.unwrap();
+ assert_eq!(generation, 1);
+}
+
+#[sqlx::test(migrations = "../macro_db_client/migrations")]
+async fn registration_racing_seal_cannot_extend_a_sealed_manifest(pool: PgPool) {
+ let (repo, team, _) = fixture(&pool).await;
+ let job = create(&repo, team, true, &["C1"]).await;
+ register(&repo, team, job, &[part(0)]).await;
+ let addition = RegisterUploads {
+ descriptors: vec![part(1)],
+ };
+ let original_seal = seal(&[part(0)]);
+ let (registration, sealing) = futures::join!(
+ repo.register(team, job, &addition),
+ repo.complete(team, job, &[], Some(&original_seal)),
+ );
+ match (registration, sealing) {
+ (Ok(_), Err(error)) => {
+ assert_eq!(error.into_current_context(), ImportError::Conflict);
+ repo.complete(team, job, &[], Some(&seal(&[part(0), part(1)])))
+ .await
+ .unwrap();
+ }
+ (Err(error), Ok(progress)) => {
+ assert_eq!(error.into_current_context(), ImportError::Conflict);
+ assert_eq!(progress.conversations[0].part_count, Some(1));
+ }
+ outcome => panic!("exactly one racing operation must succeed: {outcome:?}"),
+ }
+ assert_eq!(outbox_count(&pool, job).await, 0);
+}
+
+#[sqlx::test(migrations = "../macro_db_client/migrations")]
+async fn users_first_and_concurrent_last_completions_queue_only_sealed_work(pool: PgPool) {
+ let (repo, team, _) = fixture(&pool).await;
+ let job = create(&repo, team, true, &["C1"]).await;
+ let uploads = register(&repo, team, job, &[descriptor(UploadId::Users), part(0)]).await;
+ let progress = repo.complete(team, job, &uploads, None).await.unwrap();
+ assert!(progress.users_verified);
+ assert_eq!(
+ progress.conversations[0].status,
+ ConversationStatus::AwaitingUploads
+ );
+ assert_eq!(outbox_count(&pool, job).await, 0);
+ repo.complete(team, job, &[], Some(&seal(&[part(0)])))
+ .await
+ .unwrap();
+ assert_eq!(outbox_count(&pool, job).await, 1);
+
+ let job = create(&repo, team, true, &["C1"]).await;
+ let uploads = register(&repo, team, job, &[descriptor(UploadId::Users), part(0)]).await;
+ repo.complete(team, job, &[], Some(&seal(&[part(0)])))
+ .await
+ .unwrap();
+ let (users, parts) = futures::join!(
+ repo.complete(team, job, &uploads[..1], None),
+ repo.complete(team, job, &uploads[1..], None),
+ );
+ users.unwrap();
+ parts.unwrap();
+ let progress = repo.progress(team, job).await.unwrap().unwrap();
+ assert_eq!(progress.conversations[0].status, ConversationStatus::Queued);
+ assert_eq!(outbox_count(&pool, job).await, 1);
+}
+
+#[sqlx::test(migrations = "../macro_db_client/migrations")]
+async fn absent_metadata_stays_absent_and_unknown_binding_can_be_refined(pool: PgPool) {
+ let (repo, team, _) = fixture(&pool).await;
+ let mut command = command(false, &["D1"]);
+ let metadata = &mut command.conversations[0];
+ metadata.kind = ConversationKind::DirectMessage;
+ metadata.created_at = None;
+ metadata.creator_id = None;
+ metadata.message_count = None;
+ let first = repo
+ .create(team, &admin(), &command, &repo.limits)
+ .await
+ .unwrap();
+ let row = sqlx::query!(
+ "SELECT source_created_at, resolved_created_at, creator_id, message_count FROM slack_import_conversation WHERE job_id = $1 AND slack_channel_id = 'D1'",
+ Uuid::from(first.job_id),
+ ).fetch_one(&pool).await.unwrap();
+ assert!(row.source_created_at.is_none());
+ assert!(row.resolved_created_at.is_none());
+ assert!(row.creator_id.is_none());
+ assert!(row.message_count.is_none());
+ command.idempotency_token = Uuid::now_v7().try_into().unwrap();
+ command.source = SourceIdentity::Known {
+ source_id: "T1".parse().unwrap(),
+ };
+ repo.create(team, &admin(), &command, &repo.limits)
+ .await
+ .unwrap();
+ command.idempotency_token = Uuid::now_v7().try_into().unwrap();
+ command.source = SourceIdentity::Known {
+ source_id: "T2".parse().unwrap(),
+ };
+ error(
+ repo.create(team, &admin(), &command, &repo.limits).await,
+ ImportError::SourceMismatch,
+ );
+ assert_eq!(repo.list(team, None).await.unwrap().len(), 2);
+}
+
+async fn close(pool: &PgPool, job: JobId) {
+ sqlx::query!(
+ "UPDATE slack_import_job SET registration_closed_at = now(), cancel_requested_at = now(), status = 'cancelled' WHERE id = $1",
+ Uuid::from(job),
+ ).execute(pool).await.unwrap();
+}
+
+#[sqlx::test(migrations = "../macro_db_client/migrations")]
+async fn closed_jobs_accept_only_exact_replays_without_resurrection(pool: PgPool) {
+ let (repo, team, _) = fixture(&pool).await;
+ let job = create(&repo, team, true, &["C1"]).await;
+ let uploads = register(&repo, team, job, &[descriptor(UploadId::Users), part(0)]).await;
+ repo.complete(team, job, &uploads[..1], None).await.unwrap();
+ close(&pool, job).await;
+ let replay = repo.complete(team, job, &uploads[..1], None).await.unwrap();
+ assert_eq!(replay.status, JobStatus::Cancelled);
+ error(
+ repo.complete(team, job, &uploads[1..], None).await,
+ ImportError::Conflict,
+ );
+ error(
+ repo.complete(team, job, &[], Some(&seal(&[part(0)]))).await,
+ ImportError::Conflict,
+ );
+ error(
+ repo.register(
+ team,
+ job,
+ &RegisterUploads {
+ descriptors: vec![part(0)],
+ },
+ )
+ .await,
+ ImportError::Conflict,
+ );
+ assert_eq!(outbox_count(&pool, job).await, 0);
+}
+
+#[sqlx::test(migrations = "../macro_db_client/migrations")]
+async fn bounds_and_batch_failures_leave_no_partial_registration(pool: PgPool) {
+ let (mut repo, team, _) = fixture(&pool).await;
+ repo.limits.selected_bytes = 15;
+ let job = create(&repo, team, true, &["C1"]).await;
+ error(
+ repo.register(
+ team,
+ job,
+ &RegisterUploads {
+ descriptors: vec![part(0), part(1)],
+ },
+ )
+ .await,
+ ImportError::LimitExceeded,
+ );
+ error(
+ repo.uploads(team, job, &[part(0).upload]).await,
+ ImportError::Unavailable,
+ );
+ error(
+ repo.register(
+ team,
+ job,
+ &RegisterUploads {
+ descriptors: vec![part(0), part(0)],
+ },
+ )
+ .await,
+ ImportError::InvalidInput,
+ );
+ error(
+ repo.register(
+ team,
+ job,
+ &RegisterUploads {
+ descriptors: (0..51).map(part).collect(),
+ },
+ )
+ .await,
+ ImportError::LimitExceeded,
+ );
+ let mut bad_users = descriptor(UploadId::Users);
+ bad_users.record_count = Some(1);
+ error(
+ repo.register(
+ team,
+ job,
+ &RegisterUploads {
+ descriptors: vec![bad_users],
+ },
+ )
+ .await,
+ ImportError::LimitExceeded,
+ );
+ let mut oversized = part(0);
+ oversized.byte_length = repo.limits.part_bytes + 1;
+ error(
+ repo.register(
+ team,
+ job,
+ &RegisterUploads {
+ descriptors: vec![oversized],
+ },
+ )
+ .await,
+ ImportError::LimitExceeded,
+ );
+ error(
+ repo.register(
+ team,
+ job,
+ &RegisterUploads {
+ descriptors: vec![part(u32::MAX)],
+ },
+ )
+ .await,
+ ImportError::LimitExceeded,
+ );
+ let mut unknown = part(0);
+ unknown.upload = UploadId::ConversationPart {
+ slack_channel_id: "C404".parse().unwrap(),
+ part_index: 0,
+ };
+ error(
+ repo.register(
+ team,
+ job,
+ &RegisterUploads {
+ descriptors: vec![unknown],
+ },
+ )
+ .await,
+ ImportError::Unavailable,
+ );
+}
diff --git a/crates/slack_integration/src/outbound/s3_storage.rs b/crates/slack_integration/src/outbound/s3_storage.rs
new file mode 100644
index 0000000000..e8fb9a7d37
--- /dev/null
+++ b/crates/slack_integration/src/outbound/s3_storage.rs
@@ -0,0 +1,269 @@
+//! Immutable, checksum-bound uploads and bounded streaming reads using the S3 SDK.
+
+use std::time::{Duration, SystemTime};
+
+use aws_sdk_s3::{Client, presigning::PresigningConfig, types::ChecksumMode};
+use base64::{Engine, engine::general_purpose::STANDARD};
+use chrono::{DateTime, Utc};
+use futures::stream;
+use s3_key::SlackImportKey;
+use sha2::{Digest, Sha256};
+use tokio::io::AsyncReadExt;
+
+use crate::domain::{
+ models::*,
+ ports::{ByteStream, ImportStorage, PortResult},
+};
+
+#[cfg(test)]
+mod test;
+
+const GRANT_LIFETIME: Duration = Duration::from_secs(300);
+const READ_CHUNK_BYTES: usize = 64 * 1024;
+
+/// S3 staging adapter. The composition root supplies an already configured AWS client.
+/// Manifest lookup and team/job authorization remain the domain service's responsibility.
+#[derive(Clone)]
+pub struct S3ImportStorage {
+ client: Client,
+ bucket: String,
+ limits: ImportLimits,
+}
+
+impl S3ImportStorage {
+ /// Validate configuration once. Grants expire after five minutes.
+ pub fn new(client: Client, bucket: String, limits: ImportLimits) -> PortResult<Self> {
+ if bucket.is_empty()
+ || limits.part_bytes == 0
+ || limits.json_bytes == 0
+ || limits.part_bytes > i64::MAX as u64
+ || limits.json_bytes > i64::MAX as u64
+ {
+ return Err(ImportError::InvalidInput.into());
+ }
+ Ok(Self {
+ client,
+ bucket,
+ limits,
+ })
+ }
+
+ fn validate(&self, upload: &RegisteredUpload) -> PortResult<()> {
+ upload
+ .descriptor
+ .validate(&self.limits)
+ .map_err(|error| rootcause::Report::new(error).context(ImportError::LimitExceeded))?;
+ let key = SlackImportKey::from_s3_key(upload.key.as_str())
+ .map_err(|error| rootcause::Report::new(error).context(ImportError::UploadMismatch))?;
+ let matches = match (&upload.descriptor.upload, key.part()) {
+ (UploadId::Users, None) => true,
+ (
+ UploadId::ConversationPart {
+ slack_channel_id,
+ part_index,
+ },
+ Some((id, index)),
+ ) => slack_channel_id.as_str() == id && *part_index == index,
+ _ => false,
+ };
+ if !matches {
+ return Err(ImportError::UploadMismatch.into());
+ }
+ Ok(())
+ }
+}
+
+impl ImportStorage for S3ImportStorage {
+ async fn grant(&self, upload: &RegisteredUpload) -> PortResult<UploadGrant> {
+ self.validate(upload)?;
+ let start = SystemTime::now();
+ let config = PresigningConfig::builder()
+ .start_time(start)
+ .expires_in(GRANT_LIFETIME)
+ .build()
+ .map_err(|error| rootcause::Report::new(error).context(ImportError::Internal))?;
+ let signed = self
+ .client
+ .put_object()
+ .bucket(&self.bucket)
+ .key(upload.key.as_str())
+ .content_type(content_type(&upload.descriptor.upload))
+ .content_length(upload.descriptor.byte_length as i64)
+ .checksum_sha256(checksum(&upload.descriptor.sha256))
+ .if_none_match("*")
+ .presigned(config)
+ .await
+ .map_err(|error| rootcause::Report::new(error).context(ImportError::Retryable))?;
+ // Content-Length is signed, but browsers must derive it from the exact Blob
+ // rather than trying to set this forbidden request header in fetch().
+ let required_headers = signed
+ .headers()
+ .filter(|(name, _)| {
+ !name.eq_ignore_ascii_case("content-length") && !name.eq_ignore_ascii_case("host")
+ })
+ .map(|(name, value)| (name.to_owned(), value.to_owned()))
+ .collect();
+ Ok(UploadGrant {
+ descriptor: upload.descriptor.clone(),
+ url: macro_aws_config::transform_aws_url(signed.uri()),
+ required_headers,
+ expires_at: DateTime::<Utc>::from(start + GRANT_LIFETIME),
+ })
+ }
+
+ async fn verify(&self, upload: &RegisteredUpload) -> PortResult<VerifiedUpload> {
+ self.validate(upload)?;
+ let head = self
+ .client
+ .head_object()
+ .bucket(&self.bucket)
+ .key(upload.key.as_str())
+ .checksum_mode(ChecksumMode::Enabled)
+ .send()
+ .await
+ .map_err(|error| {
+ let code = match error.raw_response().map(|r| r.status().as_u16()) {
+ Some(404 | 412) => ImportError::UploadMismatch,
+ _ => ImportError::Retryable,
+ };
+ rootcause::Report::new(error).context(code)
+ })?;
+ verify_properties(
+ upload,
+ head.content_length(),
+ head.checksum_sha256(),
+ head.content_type(),
+ )?;
+ // S3's literal "null" version is mutable in unversioned/suspended buckets.
+ let identity = if let Some(version) = head.version_id().filter(|v| *v != "null") {
+ ObjectIdentity::Version(validator(version)?)
+ } else {
+ ObjectIdentity::EntityTag(validator(head.e_tag().ok_or(ImportError::UploadMismatch)?)?)
+ };
+ Ok(VerifiedUpload {
+ registered: upload.clone(),
+ identity,
+ })
+ }
+
+ async fn read(&self, upload: &VerifiedUpload) -> PortResult<ByteStream> {
+ self.validate(&upload.registered)?;
+ let mut request = self
+ .client
+ .get_object()
+ .bucket(&self.bucket)
+ .key(upload.registered.key.as_str())
+ .checksum_mode(ChecksumMode::Enabled);
+ request = match &upload.identity {
+ ObjectIdentity::Version(version) if version.as_str() != "null" => {
+ request.version_id(version.as_str())
+ }
+ ObjectIdentity::Version(_) => return Err(ImportError::UploadMismatch.into()),
+ ObjectIdentity::EntityTag(tag) => request.if_match(tag.as_str()),
+ };
+ let output = request.send().await.map_err(|error| {
+ let code = match error.raw_response().map(|r| r.status().as_u16()) {
+ Some(404 | 412) => ImportError::UploadMismatch,
+ _ => ImportError::Retryable,
+ };
+ rootcause::Report::new(error).context(code)
+ })?;
+ verify_properties(
+ &upload.registered,
+ output.content_length(),
+ output.checksum_sha256(),
+ output.content_type(),
+ )?;
+ let pinned = match &upload.identity {
+ ObjectIdentity::Version(version) => output.version_id() == Some(version.as_str()),
+ ObjectIdentity::EntityTag(tag) => output.e_tag() == Some(tag.as_str()),
+ };
+ if !pinned {
+ return Err(ImportError::UploadMismatch.into());
+ }
+ Ok(bounded_stream(output.body, &upload.registered.descriptor))
+ }
+}
+
+fn validator(value: &str) -> PortResult<ObjectValidator> {
+ value
+ .parse()
+ .map_err(|error| rootcause::Report::new(error).context(ImportError::UploadMismatch))
+}
+
+fn content_type(upload: &UploadId) -> &'static str {
+ match upload {
+ UploadId::Users => "application/json",
+ UploadId::ConversationPart { .. } => "application/x-ndjson",
+ }
+}
+
+fn checksum(digest: &Sha256Digest) -> String {
+ // The domain type already guarantees exactly 64 lowercase hexadecimal digits.
+ let bytes: Vec<_> = digest
+ .as_str()
+ .as_bytes()
+ .chunks_exact(2)
+ .map(|pair| {
+ fn digit(b: u8) -> u8 {
+ if b <= b'9' { b - b'0' } else { b - b'a' + 10 }
+ }
+ (digit(pair[0]) << 4) | digit(pair[1])
+ })
+ .collect();
+ STANDARD.encode(bytes)
+}
+
+fn verify_properties(
+ upload: &RegisteredUpload,
+ length: Option<i64>,
+ sha256: Option<&str>,
+ mime: Option<&str>,
+) -> PortResult<()> {
+ if length != Some(upload.descriptor.byte_length as i64)
+ || sha256 != Some(checksum(&upload.descriptor.sha256).as_str())
+ || mime != Some(content_type(&upload.descriptor.upload))
+ {
+ return Err(ImportError::UploadMismatch.into());
+ }
+ Ok(())
+}
+
+fn bounded_stream(
+ body: aws_sdk_s3::primitives::ByteStream,
+ descriptor: &UploadDescriptor,
+) -> ByteStream {
+ let expected_length = descriptor.byte_length;
+ let expected_digest = descriptor.sha256.clone();
+ let state = (body.into_async_read(), 0u64, Sha256::new());
+ Box::pin(stream::try_unfold(
+ state,
+ move |(mut reader, mut total, mut digest)| {
+ let expected_digest = expected_digest.clone();
+ async move {
+ // Probe at most one byte past the descriptor, never buffer the object.
+ let capacity =
+ (expected_length - total).min(READ_CHUNK_BYTES as u64 - 1) as usize + 1;
+ let mut bytes = vec![0; capacity];
+ let count = reader.read(&mut bytes).await.map_err(|error| {
+ rootcause::Report::new(error).context(ImportError::Retryable)
+ })?;
+ total += count as u64;
+ if total > expected_length {
+ return Err(ImportError::LimitExceeded.into());
+ }
+ if count == 0 {
+ if total != expected_length
+ || format!("{:x}", digest.finalize()) != expected_digest.as_str()
+ {
+ return Err(ImportError::UploadMismatch.into());
+ }
+ return Ok(None);
+ }
+ bytes.truncate(count);
+ digest.update(&bytes);
+ Ok(Some((bytes, (reader, total, digest))))
+ }
+ },
+ ))
+}
diff --git a/crates/slack_integration/src/outbound/s3_storage/test.rs b/crates/slack_integration/src/outbound/s3_storage/test.rs
new file mode 100644
index 0000000000..55a184f4c5
--- /dev/null
+++ b/crates/slack_integration/src/outbound/s3_storage/test.rs
@@ -0,0 +1,295 @@
+use super::*;
+use crate::outbound::test_support::mock_http;
+use aws_sdk_s3::config::{BehaviorVersion, Credentials, Region, retry::RetryConfig};
+use futures::{StreamExt, TryStreamExt};
+
+const KEY: &str = "slack-import/01980000-0000-7000-8000-000000000001/01980000-0000-7000-8000-000000000002/users.json";
+
+fn upload(bytes: &[u8]) -> RegisteredUpload {
+ RegisteredUpload {
+ key: KEY.parse().unwrap(),
+ descriptor: UploadDescriptor {
+ upload: UploadId::Users,
+ sha256: format!("{:x}", Sha256::digest(bytes)).parse().unwrap(),
+ byte_length: bytes.len() as u64,
+ record_count: None,
+ },
+ }
+}
+
+fn storage(endpoint: &str) -> S3ImportStorage {
+ let config = aws_sdk_s3::Config::builder()
+ .behavior_version(BehaviorVersion::latest())
+ .region(Region::new("us-east-1"))
+ .credentials_provider(Credentials::new("test", "test", None, None, "test"))
+ .endpoint_url(endpoint)
+ .force_path_style(true)
+ .retry_config(RetryConfig::disabled())
+ .build();
+ S3ImportStorage::new(
+ Client::from_conf(config),
+ "staging".into(),
+ ImportLimits::default(),
+ )
+ .unwrap()
+}
+
+fn response(upload: &RegisteredUpload, version: Option<&str>, body: &str) -> String {
+ let version = version
+ .map(|v| format!("x-amz-version-id: {v}\r\n"))
+ .unwrap_or_default();
+ format!(
+ "HTTP/1.1 200 OK\r\nConnection: close\r\nContent-Length: {}\r\nContent-Type: application/json\r\nx-amz-checksum-sha256: {}\r\nETag: \"original\"\r\n{version}\r\n{body}",
+ upload.descriptor.byte_length,
+ checksum(&upload.descriptor.sha256)
+ )
+}
+
+#[tokio::test]
+async fn grants_sign_create_only_checksum_length_type_and_short_expiry() {
+ let storage = storage("http://localhost:9000");
+ let upload = upload(b"[]");
+ let before = Utc::now();
+ let grant = storage.grant(&upload).await.unwrap();
+ assert_eq!(
+ grant
+ .required_headers
+ .get("if-none-match")
+ .map(String::as_str),
+ Some("*")
+ );
+ assert_eq!(
+ grant
+ .required_headers
+ .get("content-type")
+ .map(String::as_str),
+ Some("application/json")
+ );
+ assert_eq!(
+ grant.required_headers.get("x-amz-checksum-sha256"),
+ Some(&checksum(&upload.descriptor.sha256))
+ );
+ assert!(!grant.required_headers.contains_key("content-length"));
+ assert!(grant.url.contains("X-Amz-Expires=300"));
+ let signed_headers = grant
+ .url
+ .split("X-Amz-SignedHeaders=")
+ .nth(1)
+ .unwrap()
+ .split('&')
+ .next()
+ .unwrap();
+ for header in [
+ "content-length",
+ "content-type",
+ "if-none-match",
+ "x-amz-checksum-sha256",
+ ] {
+ assert!(
+ signed_headers.contains(header),
+ "unsigned {header}: {signed_headers}"
+ );
+ }
+ assert!(grant.expires_at >= before + chrono::Duration::seconds(300));
+ assert!(grant.expires_at <= Utc::now() + chrono::Duration::seconds(300));
+ // Retrying never replaces create-only with overwrite permission.
+ assert_eq!(
+ storage.grant(&upload).await.unwrap().required_headers,
+ grant.required_headers
+ );
+}
+
+#[tokio::test]
+async fn conversation_grant_binds_exact_part_and_ndjson_content_type() {
+ let mut upload = upload(b"{}\n");
+ upload.key = KEY.replace("users.json", "C123/4.ndjson").parse().unwrap();
+ upload.descriptor.upload = UploadId::ConversationPart {
+ slack_channel_id: "C123".parse().unwrap(),
+ part_index: 4,
+ };
+ upload.descriptor.record_count = Some(1);
+ let storage = storage("http://localhost:9000");
+ let grant = storage.grant(&upload).await.unwrap();
+ assert!(grant.url.contains("/C123/4.ndjson?"));
+ assert_eq!(
+ grant.required_headers["content-type"],
+ "application/x-ndjson"
+ );
+ for suffix in ["C123/3.ndjson", "G123/4.ndjson", "users.json"] {
+ upload.key = KEY.replace("users.json", suffix).parse().unwrap();
+ assert!(storage.grant(&upload).await.is_err());
+ }
+}
+
+#[tokio::test]
+async fn rejects_altered_keys_and_oversized_descriptors_before_io() {
+ let storage = storage("http://127.0.0.1:1");
+ let mut upload = upload(b"[]");
+ for suffix in ["C123/0.ndjson", "users.json/extra", "C123/00.ndjson"] {
+ upload.key = KEY.replace("users.json", suffix).parse().unwrap();
+ assert!(storage.grant(&upload).await.is_err());
+ assert!(storage.verify(&upload).await.is_err());
+ }
+ upload.key = KEY.parse().unwrap();
+ upload.descriptor.byte_length = u64::MAX;
+ assert!(storage.grant(&upload).await.is_err());
+}
+
+#[tokio::test]
+async fn head_verifies_checksum_size_type_and_pins_identity() {
+ let upload = upload(b"[]");
+ for (version, expected) in [
+ (Some("v1"), ObjectIdentity::Version("v1".parse().unwrap())),
+ (
+ None,
+ ObjectIdentity::EntityTag("\"original\"".parse().unwrap()),
+ ),
+ (
+ Some("null"),
+ ObjectIdentity::EntityTag("\"original\"".parse().unwrap()),
+ ),
+ ] {
+ let (endpoint, requests) = mock_http(vec![response(&upload, version, "")]);
+ assert_eq!(
+ storage(&endpoint).verify(&upload).await.unwrap().identity,
+ expected
+ );
+ let request = requests.recv().unwrap().to_lowercase();
+ assert!(request.starts_with("head /staging/slack-import/"));
+ assert!(request.contains("x-amz-checksum-mode: enabled"));
+ }
+ let valid = response(&upload, None, "");
+ for invalid in [
+ valid.replace("Content-Length: 2", "Content-Length: 3"),
+ valid.replace(
+ &checksum(&upload.descriptor.sha256),
+ &STANDARD.encode([0u8; 32]),
+ ),
+ valid.replace("application/json", "text/plain"),
+ valid.replace(
+ &format!(
+ "x-amz-checksum-sha256: {}\r\n",
+ checksum(&upload.descriptor.sha256)
+ ),
+ "",
+ ),
+ valid.replace("ETag: \"original\"\r\n", ""),
+ ] {
+ let (endpoint, _) = mock_http(vec![invalid]);
+ assert!(storage(&endpoint).verify(&upload).await.is_err());
+ }
+}
+
+#[tokio::test]
+async fn get_enforces_version_or_etag_and_streams_verified_bytes() {
+ let registered = upload(b"[]");
+ for identity in [
+ ObjectIdentity::Version("v1".parse().unwrap()),
+ ObjectIdentity::EntityTag("\"original\"".parse().unwrap()),
+ ] {
+ let (endpoint, requests) = mock_http(vec![response(&registered, Some("v1"), "[]")]);
+ let upload = VerifiedUpload {
+ registered: registered.clone(),
+ identity: identity.clone(),
+ };
+ let chunks: Vec<_> = storage(&endpoint)
+ .read(&upload)
+ .await
+ .unwrap()
+ .try_collect()
+ .await
+ .unwrap();
+ assert_eq!(chunks.concat(), b"[]");
+ let request = requests.recv().unwrap().to_lowercase();
+ match identity {
+ ObjectIdentity::Version(_) => assert!(request.contains("versionid=v1")),
+ ObjectIdentity::EntityTag(_) => assert!(request.contains("if-match: \"original\"")),
+ }
+ }
+}
+
+#[tokio::test]
+async fn overwritten_or_unpinned_objects_are_rejected() {
+ let registered = upload(b"[]");
+ let upload = VerifiedUpload {
+ registered: registered.clone(),
+ identity: ObjectIdentity::EntityTag("\"original\"".parse().unwrap()),
+ };
+ let precondition =
+ "HTTP/1.1 412 Precondition Failed\r\nContent-Length: 0\r\nConnection: close\r\n\r\n"
+ .to_owned();
+ for response in [
+ precondition,
+ response(&registered, None, "[]").replace("ETag: \"original\"", "ETag: \"changed\""),
+ ] {
+ let (endpoint, _) = mock_http(vec![response]);
+ assert!(storage(&endpoint).read(&upload).await.is_err());
+ }
+ let invalid_version = VerifiedUpload {
+ identity: ObjectIdentity::Version("null".parse().unwrap()),
+ ..upload
+ };
+ assert!(
+ storage("http://127.0.0.1:1")
+ .read(&invalid_version)
+ .await
+ .is_err()
+ );
+}
+
+#[tokio::test]
+async fn get_revalidates_metadata_and_never_accepts_corrupt_body() {
+ let registered = upload(b"[]");
+ let upload = VerifiedUpload {
+ registered: registered.clone(),
+ identity: ObjectIdentity::Version("v1".parse().unwrap()),
+ };
+ let valid = response(&registered, Some("v1"), "[]");
+ for invalid in [
+ valid.replace("Content-Length: 2", "Content-Length: 3"),
+ valid.replace(
+ &checksum(&registered.descriptor.sha256),
+ &STANDARD.encode([0u8; 32]),
+ ),
+ valid.replace("x-amz-version-id: v1", "x-amz-version-id: v2"),
+ ] {
+ let (endpoint, _) = mock_http(vec![invalid]);
+ assert!(storage(&endpoint).read(&upload).await.is_err());
+ }
+ for body in ["{}", "["] {
+ let (endpoint, _) = mock_http(vec![response(&registered, Some("v1"), body)]);
+ let stream = storage(&endpoint).read(&upload).await.unwrap();
+ assert!(stream.try_collect::<Vec<_>>().await.is_err());
+ }
+}
+
+#[tokio::test]
+async fn stream_checks_actual_bytes_digest_and_chunk_bound_independently_of_metadata() {
+ use aws_sdk_s3::primitives::ByteStream as AwsStream;
+ let data = vec![b'x'; READ_CHUNK_BYTES * 3 + 5];
+ let descriptor = upload(&data).descriptor;
+ let chunks: Vec<_> = bounded_stream(AwsStream::from(data.clone()), &descriptor)
+ .try_collect()
+ .await
+ .unwrap();
+ assert!(chunks.iter().all(|chunk| chunk.len() <= READ_CHUNK_BYTES));
+ assert_eq!(chunks.concat(), data);
+ for invalid in [
+ vec![b'x'; data.len() + 1],
+ vec![b'x'; data.len() - 1],
+ vec![b'y'; data.len()],
+ ] {
+ let mut stream = bounded_stream(AwsStream::from(invalid), &descriptor);
+ let mut delivered = 0;
+ loop {
+ match stream.next().await {
+ Some(Ok(chunk)) => {
+ delivered += chunk.len();
+ assert!(delivered <= data.len());
+ }
+ Some(Err(_)) => break,
+ None => panic!("invalid stream accepted"),
+ }
+ }
+ }
+}
diff --git a/crates/slack_integration/src/outbound/search_backfill.rs b/crates/slack_integration/src/outbound/search_backfill.rs
new file mode 100644
index 0000000000..3b6616caa3
--- /dev/null
+++ b/crates/slack_integration/src/outbound/search_backfill.rs
@@ -0,0 +1,160 @@
+//! Authenticated search-processing backfill adapter. The HTTP receipt is a job
+//! identity, NOT a signal that source publication or OpenSearch indexing finished.
+
+use std::time::Duration;
+
+use reqwest::{
+ Client, Response, StatusCode, Url,
+ header::{HeaderMap, HeaderValue},
+};
+use serde::{Deserialize, de::DeserializeOwned};
+use uuid::Uuid;
+
+use crate::domain::{
+ models::{ImportError, SearchBackfill, SearchState},
+ ports::{PortResult, SearchBackfillClient},
+};
+
+#[cfg(test)]
+mod test;
+
+const RESPONSE_BYTES: usize = 64 * 1024;
+
+/// Scoped HTTP client for the existing search-processing internal backfill API.
+#[derive(Clone)]
+pub struct HttpSearchBackfill {
+ client: Client,
+ base: Url,
+}
+
+impl HttpSearchBackfill {
+ /// Validate configuration at composition time. Redirects are disabled so an
+ /// internal authorization header cannot be forwarded to another origin.
+ pub fn new(base: &str, internal_key: &str) -> PortResult<Self> {
+ let base = Url::parse(&format!("{}/", base.trim_end_matches('/')))
+ .map_err(|_| ImportError::InvalidInput)?;
+ if !matches!(base.scheme(), "http" | "https")
+ || base.query().is_some()
+ || base.fragment().is_some()
+ || !base.username().is_empty()
+ || base.password().is_some()
+ || internal_key.is_empty()
+ {
+ return Err(ImportError::InvalidInput.into());
+ }
+ let mut headers = HeaderMap::new();
+ let mut key = HeaderValue::from_str(internal_key).map_err(|_| ImportError::InvalidInput)?;
+ key.set_sensitive(true);
+ headers.insert("x-internal-auth-key", key);
+ let client = Client::builder()
+ .default_headers(headers)
+ .redirect(reqwest::redirect::Policy::none())
+ .timeout(Duration::from_secs(30))
+ .build()
+ .map_err(|_| ImportError::Internal)?;
+ Ok(Self { client, base })
+ }
+
+ fn endpoint(&self, path: &str) -> PortResult<Url> {
+ self.base
+ .join(&format!("internal/backfill/{path}"))
+ .map_err(|_| ImportError::Internal.into())
+ }
+}
+
+impl SearchBackfillClient for HttpSearchBackfill {
+ async fn submit(&self, request: &SearchBackfill) -> PortResult<Uuid> {
+ // None/omitted scope would backfill the entire deployment. Fail closed.
+ if request.channel_ids.is_empty()
+ || request.channel_ids.len() > 50
+ || request
+ .channel_ids
+ .iter()
+ .any(|id| id.is_nil() || id.is_max())
+ {
+ return Err(ImportError::InvalidInput.into());
+ }
+ let response = self
+ .client
+ .post(self.endpoint("channels")?)
+ .json(
+ &serde_json::json!({"channel_ids": request.channel_ids, "deletion_filter": "any"}),
+ )
+ .send()
+ .await
+ .map_err(|_| ImportError::Retryable)?;
+ if response.status() != StatusCode::ACCEPTED {
+ return Err(ImportError::Retryable.into());
+ }
+ let receipt: Accepted = decode(response).await?;
+ valid_receipt(receipt.job_id)?;
+ Ok(receipt.job_id)
+ }
+
+ async fn progress(&self, receipt: Uuid) -> PortResult<SearchState> {
+ valid_receipt(receipt)?;
+ let response = self
+ .client
+ .get(self.endpoint(&receipt.to_string())?)
+ .send()
+ .await
+ .map_err(|_| ImportError::Retryable)?;
+ if response.status() == StatusCode::NOT_FOUND {
+ // Expired/lost registry entry: the domain resubmits the same scope.
+ return Ok(SearchState::Failed);
+ }
+ if response.status() != StatusCode::OK {
+ return Err(ImportError::Retryable.into());
+ }
+ let snapshot: Snapshot = decode(response).await?;
+ if snapshot.job_id != receipt {
+ return Err(ImportError::Retryable.into());
+ }
+ match snapshot.status {
+ Status::Running => Ok(SearchState::Submitted {
+ receipt_id: receipt,
+ }),
+ Status::Completed => Ok(SearchState::Completed),
+ Status::Failed | Status::Cancelled => Ok(SearchState::Failed),
+ }
+ }
+}
+
+fn valid_receipt(receipt: Uuid) -> PortResult<()> {
+ if receipt.is_nil() || receipt.is_max() {
+ return Err(ImportError::Retryable.into());
+ }
+ Ok(())
+}
+
+async fn decode<T: DeserializeOwned>(mut response: Response) -> PortResult<T> {
+ let mut bytes = Vec::new();
+ while let Some(chunk) = response.chunk().await.map_err(|_| ImportError::Retryable)? {
+ if bytes.len() + chunk.len() > RESPONSE_BYTES {
+ return Err(ImportError::Retryable.into());
+ }
+ bytes.extend_from_slice(&chunk);
+ }
+ // Never retain provider errors/bodies in public progress or logs.
+ serde_json::from_slice(&bytes).map_err(|_| ImportError::Retryable.into())
+}
+
+#[derive(Deserialize)]
+struct Accepted {
+ job_id: Uuid,
+}
+
+#[derive(Deserialize)]
+struct Snapshot {
+ job_id: Uuid,
+ status: Status,
+}
+
+#[derive(Deserialize)]
+#[serde(rename_all = "snake_case")]
+enum Status {
+ Running,
+ Completed,
+ Failed,
+ Cancelled,
+}
diff --git a/crates/slack_integration/src/outbound/search_backfill/test.rs b/crates/slack_integration/src/outbound/search_backfill/test.rs
new file mode 100644
index 0000000000..eca9cdd1e5
--- /dev/null
+++ b/crates/slack_integration/src/outbound/search_backfill/test.rs
@@ -0,0 +1,153 @@
+use super::*;
+use crate::outbound::test_support::mock_http;
+use chrono::Utc;
+
+fn response(status: &str, body: &str) -> String {
+ format!(
+ "HTTP/1.1 {status}\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}",
+ body.len()
+ )
+}
+
+fn request() -> SearchBackfill {
+ SearchBackfill {
+ job_id: Uuid::now_v7().try_into().unwrap(),
+ channel_ids: vec![Uuid::now_v7()],
+ generation: 4,
+ state: SearchState::Pending,
+ updated_at: Utc::now(),
+ }
+}
+
+#[tokio::test]
+async fn submits_exact_scope_with_auth_and_polls_actual_receipt_until_publication() {
+ let id = Uuid::now_v7();
+ let (endpoint, requests) = mock_http(vec![
+ response("202 Accepted", &format!(r#"{{"job_id":"{id}"}}"#)),
+ response(
+ "200 OK",
+ &format!(r#"{{"job_id":"{id}","status":"running","enqueued":5}}"#),
+ ),
+ response(
+ "200 OK",
+ &format!(r#"{{"job_id":"{id}","status":"completed","enqueued":10}}"#),
+ ),
+ ]);
+ let client = HttpSearchBackfill::new(&endpoint, "test-internal-key").unwrap();
+ let scope = request();
+ assert_eq!(client.submit(&scope).await.unwrap(), id);
+ assert_eq!(
+ client.progress(id).await.unwrap(),
+ SearchState::Submitted { receipt_id: id }
+ );
+ assert_eq!(client.progress(id).await.unwrap(), SearchState::Completed);
+ let post = requests.recv().unwrap();
+ assert!(post.starts_with("POST /internal/backfill/channels "));
+ assert!(
+ post.to_lowercase()
+ .contains("x-internal-auth-key: test-internal-key")
+ );
+ let body: serde_json::Value =
+ serde_json::from_str(post.split_once("\r\n\r\n").unwrap().1).unwrap();
+ assert_eq!(
+ body,
+ serde_json::json!({"channel_ids": scope.channel_ids, "deletion_filter":"any"})
+ );
+ for _ in 0..2 {
+ assert!(
+ requests
+ .recv()
+ .unwrap()
+ .starts_with(&format!("GET /internal/backfill/{id} "))
+ );
+ }
+}
+
+#[tokio::test]
+async fn missing_failed_cancelled_and_transient_receipts_are_not_completed() {
+ let id = Uuid::now_v7();
+ let (endpoint, _) = mock_http(vec![
+ response("404 Not Found", "unknown job id"),
+ response(
+ "200 OK",
+ &format!(r#"{{"job_id":"{id}","status":"failed","error":"provider details"}}"#),
+ ),
+ response(
+ "200 OK",
+ &format!(r#"{{"job_id":"{id}","status":"cancelled"}}"#),
+ ),
+ response("503 Service Unavailable", "provider details"),
+ response(
+ "200 OK",
+ &format!(r#"{{"job_id":"{}","status":"completed"}}"#, Uuid::now_v7()),
+ ),
+ response("200 OK", "invalid json"),
+ ]);
+ let client = HttpSearchBackfill::new(&endpoint, "test-key").unwrap();
+ for _ in 0..3 {
+ assert_eq!(client.progress(id).await.unwrap(), SearchState::Failed);
+ }
+ for _ in 0..3 {
+ assert_eq!(
+ client
+ .progress(id)
+ .await
+ .unwrap_err()
+ .into_current_context(),
+ ImportError::Retryable
+ );
+ }
+}
+
+#[tokio::test]
+async fn submit_does_not_confuse_success_status_or_invalid_body_with_acceptance() {
+ let (endpoint, _) = mock_http(vec![
+ response("200 OK", &format!(r#"{{"job_id":"{}"}}"#, Uuid::now_v7())),
+ response("202 Accepted", r#"{"job_id":"not-a-receipt"}"#),
+ response(
+ "202 Accepted",
+ &format!(r#"{{"job_id":"{}"}}"#, Uuid::nil()),
+ ),
+ response("202 Accepted", &"x".repeat(RESPONSE_BYTES + 1)),
+ ]);
+ let client = HttpSearchBackfill::new(&endpoint, "test-key").unwrap();
+ for _ in 0..4 {
+ assert_eq!(
+ client
+ .submit(&request())
+ .await
+ .unwrap_err()
+ .into_current_context(),
+ ImportError::Retryable
+ );
+ }
+}
+
+#[tokio::test]
+async fn empty_scope_is_never_a_global_backfill() {
+ let client = HttpSearchBackfill::new("http://127.0.0.1:1", "test-key").unwrap();
+ let mut request = request();
+ request.channel_ids.clear();
+ assert_eq!(
+ client
+ .submit(&request)
+ .await
+ .unwrap_err()
+ .into_current_context(),
+ ImportError::InvalidInput
+ );
+}
+
+#[test]
+fn invalid_configuration_fails_at_construction() {
+ for base in [
+ "file:///tmp/search",
+ "https://example.test/?key=secret",
+ "https://user:[email protected]",
+ "https://example.test/#fragment",
+ ] {
+ assert!(HttpSearchBackfill::new(base, "key").is_err());
+ }
+ assert!(HttpSearchBackfill::new("https://example.test", "").is_err());
+ assert!(HttpSearchBackfill::new("https://example.test", "bad\nkey").is_err());
+}
diff --git a/crates/slack_integration/src/outbound/sqs_queue.rs b/crates/slack_integration/src/outbound/sqs_queue.rs
new file mode 100644
index 0000000000..0e1fb5bc13
--- /dev/null
+++ b/crates/slack_integration/src/outbound/sqs_queue.rs
@@ -0,0 +1,296 @@
+//! SQS publication and worker delivery controls, including a separate DLQ path.
+
+use std::collections::HashSet;
+
+use aws_sdk_sqs::{
+ Client,
+ types::{Message, MessageSystemAttributeName, SendMessageBatchRequestEntry},
+};
+use macro_queues::{SlackImportDlq, SlackImportQueue};
+
+use crate::domain::{
+ models::{ImportError, ImportEvent},
+ ports::{ImportConsumer, ImportQueue, PortResult},
+};
+
+#[cfg(test)]
+mod test;
+
+const BATCH_SIZE: usize = 10;
+const POLL_SECONDS: i32 = 20;
+const VISIBILITY_SECONDS: i32 = 180;
+const MAX_VISIBILITY_SECONDS: u32 = 43_200;
+
+/// Which queue to poll. Delivery handles remember their origin for ack/heartbeat.
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub enum QueueSource {
+ /// Ready conversation work.
+ Main,
+ /// Exhausted work requiring durable reconciliation before acknowledgement.
+ DeadLetter,
+}
+
+/// One opaque receipt. Malformed payloads remain accessible to the driver rather
+/// than discarding healthy messages in the same receive batch.
+pub struct Delivery {
+ source: QueueSource,
+ receipt: String,
+ /// Strictly decoded identity-only payload; not an authorization capability.
+ pub event: Result<ImportEvent, ImportError>,
+ /// SQS approximate receive count, including redelivery attempts.
+ pub receive_count: u32,
+}
+
+/// Per-entry batch result. Only `published` entries may be marked published in the outbox.
+#[derive(Debug)]
+pub struct BatchPublication {
+ /// Events positively acknowledged by SQS.
+ pub published: Vec<ImportEvent>,
+ /// Rejected entries, retained for retry or operator reconciliation.
+ pub failed: Vec<PublicationFailure>,
+}
+
+/// One failed entry from an otherwise successful HTTP batch response.
+#[derive(Debug)]
+pub struct PublicationFailure {
+ /// The event that was not accepted.
+ pub event: ImportEvent,
+ /// SQS considers this a sender error, not a transient provider failure.
+ pub sender_fault: bool,
+}
+
+/// AWS adapter configured by the composition root, never from domain environment access.
+#[derive(Clone)]
+pub struct SqsImportQueue {
+ client: Client,
+ main_url: String,
+ dlq_url: String,
+}
+
+impl SqsImportQueue {
+ /// Resolve typed queue names (or explicit URL overrides) at startup.
+ pub async fn new(
+ client: Client,
+ main: &SlackImportQueue,
+ dlq: &SlackImportDlq,
+ ) -> PortResult<Self> {
+ let main_url = resolve_url(&client, main.as_str()).await?;
+ let dlq_url = resolve_url(&client, dlq.as_str()).await?;
+ if main_url == dlq_url {
+ return Err(ImportError::InvalidInput.into());
+ }
+ Ok(Self {
+ client,
+ main_url,
+ dlq_url,
+ })
+ }
+
+ fn url(&self, source: QueueSource) -> &str {
+ match source {
+ QueueSource::Main => &self.main_url,
+ QueueSource::DeadLetter => &self.dlq_url,
+ }
+ }
+
+ /// Publish at most ten events. A partial HTTP success is NOT whole-batch success.
+ /// Transport failures leave every event pending; retrying may duplicate accepted work.
+ pub async fn publish_batch(&self, events: &[ImportEvent]) -> PortResult<BatchPublication> {
+ if events.is_empty() || events.len() > BATCH_SIZE {
+ return Err(ImportError::InvalidInput.into());
+ }
+ let entries = events
+ .iter()
+ .enumerate()
+ .map(|(index, event)| {
+ let body = serde_json::to_string(event).map_err(|error| {
+ rootcause::Report::new(error).context(ImportError::Internal)
+ })?;
+ SendMessageBatchRequestEntry::builder()
+ .id(index.to_string())
+ .message_body(body)
+ .build()
+ .map_err(|error| rootcause::Report::new(error).context(ImportError::Internal))
+ })
+ .collect::<PortResult<Vec<_>>>()?;
+ let response = self
+ .client
+ .send_message_batch()
+ .queue_url(&self.main_url)
+ .set_entries(Some(entries))
+ .send()
+ .await
+ .map_err(|error| rootcause::Report::new(error).context(ImportError::Retryable))?;
+ let mut seen = HashSet::new();
+ let mut result = BatchPublication {
+ published: Vec::new(),
+ failed: Vec::new(),
+ };
+ for success in response.successful() {
+ let index = response_index(success.id(), events.len(), &mut seen)?;
+ result.published.push(events[index].clone());
+ }
+ for failure in response.failed() {
+ let index = response_index(failure.id(), events.len(), &mut seen)?;
+ result.failed.push(PublicationFailure {
+ event: events[index].clone(),
+ sender_fault: failure.sender_fault(),
+ });
+ }
+ if seen.len() != events.len() {
+ return Err(ImportError::Retryable.into());
+ }
+ Ok(result)
+ }
+
+ /// Long-poll up to ten messages, requesting receive counts and a three-minute lease.
+ /// The worker must heartbeat visibility every minute while durable work is active.
+ pub async fn receive(&self, source: QueueSource) -> PortResult<Vec<Delivery>> {
+ self.receive_bounded(source, BATCH_SIZE as i32).await
+ }
+
+ async fn receive_bounded(&self, source: QueueSource, limit: i32) -> PortResult<Vec<Delivery>> {
+ let output = self
+ .client
+ .receive_message()
+ .queue_url(self.url(source))
+ .max_number_of_messages(limit)
+ .wait_time_seconds(POLL_SECONDS)
+ .visibility_timeout(VISIBILITY_SECONDS)
+ .message_system_attribute_names(MessageSystemAttributeName::ApproximateReceiveCount)
+ .send()
+ .await
+ .map_err(|error| rootcause::Report::new(error).context(ImportError::Retryable))?;
+ output
+ .messages
+ .unwrap_or_default()
+ .into_iter()
+ .map(|message| delivery(source, message))
+ .collect()
+ }
+
+ /// Extend the delivery's own queue visibility, including DLQ reconciliation work.
+ /// This does not renew the database lease; the worker must renew both.
+ pub async fn extend_visibility(&self, delivery: &Delivery, seconds: u32) -> PortResult<()> {
+ if seconds > MAX_VISIBILITY_SECONDS {
+ return Err(ImportError::InvalidInput.into());
+ }
+ self.client
+ .change_message_visibility()
+ .queue_url(self.url(delivery.source))
+ .receipt_handle(&delivery.receipt)
+ .visibility_timeout(seconds as i32)
+ .send()
+ .await
+ .map_err(|error| rootcause::Report::new(error).context(ImportError::Retryable))?;
+ Ok(())
+ }
+
+ /// Delete only after the driver has durably settled/reconciled the work.
+ pub async fn delete(&self, delivery: &Delivery) -> PortResult<()> {
+ self.client
+ .delete_message()
+ .queue_url(self.url(delivery.source))
+ .receipt_handle(&delivery.receipt)
+ .send()
+ .await
+ .map_err(|error| rootcause::Report::new(error).context(ImportError::Retryable))?;
+ Ok(())
+ }
+}
+
+impl ImportConsumer for SqsImportQueue {
+ type Delivery = Delivery;
+
+ async fn receive(&self, dead_letter: bool) -> PortResult<Option<Delivery>> {
+ let source = if dead_letter {
+ QueueSource::DeadLetter
+ } else {
+ QueueSource::Main
+ };
+ Ok(self.receive_bounded(source, 1).await?.pop())
+ }
+
+ fn envelope(delivery: &Delivery) -> (Result<ImportEvent, ImportError>, u32) {
+ (delivery.event.clone(), delivery.receive_count)
+ }
+
+ async fn extend(&self, delivery: &Delivery) -> PortResult<()> {
+ self.extend_visibility(delivery, VISIBILITY_SECONDS as u32)
+ .await
+ }
+
+ async fn delete(&self, delivery: &Delivery) -> PortResult<()> {
+ self.delete(delivery).await
+ }
+}
+
+impl ImportQueue for SqsImportQueue {
+ async fn publish(&self, event: &ImportEvent) -> PortResult<()> {
+ let result = self.publish_batch(std::slice::from_ref(event)).await?;
+ if let Some(failure) = result.failed.first() {
+ return Err(if failure.sender_fault {
+ ImportError::Internal
+ } else {
+ ImportError::Retryable
+ }
+ .into());
+ }
+ Ok(())
+ }
+}
+
+async fn resolve_url(client: &Client, queue: &str) -> PortResult<String> {
+ if queue.starts_with("https://") || queue.starts_with("http://") {
+ return Ok(queue.to_owned());
+ }
+ if queue.is_empty() {
+ return Err(ImportError::InvalidInput.into());
+ }
+ client
+ .get_queue_url()
+ .queue_name(queue)
+ .send()
+ .await
+ .map_err(|error| rootcause::Report::new(error).context(ImportError::Retryable))?
+ .queue_url
+ .filter(|url| !url.is_empty())
+ .ok_or_else(|| ImportError::Internal.into())
+}
+
+fn response_index(id: &str, length: usize, seen: &mut HashSet<usize>) -> PortResult<usize> {
+ let index = id
+ .parse::<usize>()
+ .ok()
+ .filter(|index| *index < length)
+ .ok_or(ImportError::Retryable)?;
+ if id != index.to_string() || !seen.insert(index) {
+ return Err(ImportError::Retryable.into());
+ }
+ Ok(index)
+}
+
+fn delivery(source: QueueSource, message: Message) -> PortResult<Delivery> {
+ let receipt = message
+ .receipt_handle
+ .filter(|value| !value.is_empty())
+ .ok_or(ImportError::Retryable)?;
+ let receive_count = message
+ .attributes
+ .as_ref()
+ .and_then(|attributes| attributes.get(&MessageSystemAttributeName::ApproximateReceiveCount))
+ .and_then(|value| value.parse::<u32>().ok())
+ .filter(|count| *count > 0)
+ .ok_or(ImportError::Retryable)?;
+ let event = message
+ .body
+ .as_deref()
+ .ok_or(ImportError::InvalidInput)
+ .and_then(|body| serde_json::from_str(body).map_err(|_| ImportError::InvalidInput));
+ Ok(Delivery {
+ source,
+ receipt,
+ event,
+ receive_count,
+ })
+}
diff --git a/crates/slack_integration/src/outbound/sqs_queue/test.rs b/crates/slack_integration/src/outbound/sqs_queue/test.rs
new file mode 100644
index 0000000000..836390e09b
--- /dev/null
+++ b/crates/slack_integration/src/outbound/sqs_queue/test.rs
@@ -0,0 +1,181 @@
+use super::*;
+use crate::outbound::test_support::mock_http;
+use aws_sdk_sqs::config::{BehaviorVersion, Credentials, Region, retry::RetryConfig};
+use serde_json::{Value, json};
+
+fn event(generation: u64) -> ImportEvent {
+ ImportEvent {
+ job_id: "01980000-0000-7000-8000-000000000001".parse().unwrap(),
+ slack_channel_id: "C123".parse().unwrap(),
+ generation,
+ }
+}
+
+fn response(body: Value) -> String {
+ let body = body.to_string();
+ format!(
+ "HTTP/1.1 200 OK\r\nConnection: close\r\nContent-Type: application/x-amz-json-1.0\r\nContent-Length: {}\r\n\r\n{body}",
+ body.len()
+ )
+}
+
+fn client(endpoint: &str) -> Client {
+ Client::from_conf(
+ aws_sdk_sqs::Config::builder()
+ .behavior_version(BehaviorVersion::latest())
+ .region(Region::new("us-east-1"))
+ .credentials_provider(Credentials::new("test", "test", None, None, "test"))
+ .endpoint_url(endpoint)
+ .retry_config(RetryConfig::disabled())
+ .build(),
+ )
+}
+
+async fn queue(endpoint: &str) -> SqsImportQueue {
+ SqsImportQueue::new(
+ client(endpoint),
+ &SlackImportQueue::from_owned(format!("{endpoint}/main")),
+ &SlackImportDlq::from_owned(format!("{endpoint}/dlq")),
+ )
+ .await
+ .unwrap()
+}
+
+fn request_body(request: &str) -> Value {
+ serde_json::from_str(request.split_once("\r\n\r\n").unwrap().1).unwrap()
+}
+
+#[tokio::test]
+async fn resolves_typed_names_at_startup() {
+ let (endpoint, requests) = mock_http(vec![
+ response(json!({"QueueUrl":"https://queue/main"})),
+ response(json!({"QueueUrl":"https://queue/dlq"})),
+ ]);
+ let queue = SqsImportQueue::new(
+ client(&endpoint),
+ &SlackImportQueue::dev(),
+ &SlackImportDlq::dev(),
+ )
+ .await
+ .unwrap();
+ assert_eq!(queue.main_url, "https://queue/main");
+ assert_eq!(queue.dlq_url, "https://queue/dlq");
+ assert_eq!(
+ request_body(&requests.recv().unwrap())["QueueName"],
+ "slack-import-queue-dev"
+ );
+ assert_eq!(
+ request_body(&requests.recv().unwrap())["QueueName"],
+ "slack-import-dlq-dev"
+ );
+}
+
+#[tokio::test]
+async fn partial_batch_success_only_acknowledges_accepted_events() {
+ let (endpoint, requests) = mock_http(vec![response(json!({
+ "Successful": [{"Id":"1", "MessageId":"m1", "MD5OfMessageBody":"unused"}],
+ "Failed": [{"Id":"0", "Code":"ServiceUnavailable", "SenderFault":false}]
+ }))]);
+ let events = [event(1), event(2)];
+ let result = queue(&endpoint).await.publish_batch(&events).await.unwrap();
+ assert_eq!(result.published, vec![event(2)]);
+ assert_eq!(result.failed.len(), 1);
+ assert_eq!(result.failed[0].event, event(1));
+ assert!(!result.failed[0].sender_fault);
+ let body = request_body(&requests.recv().unwrap());
+ assert_eq!(body["Entries"][0]["Id"], "0");
+ let payload: Value =
+ serde_json::from_str(body["Entries"][0]["MessageBody"].as_str().unwrap()).unwrap();
+ assert_eq!(payload.as_object().unwrap().len(), 3);
+ assert_eq!(payload, serde_json::to_value(event(1)).unwrap());
+}
+
+#[tokio::test]
+async fn publication_rejects_failed_missing_duplicate_and_unknown_results() {
+ for body in [
+ json!({"Successful":[], "Failed":[{"Id":"0", "Code":"Invalid", "SenderFault":true}]}),
+ json!({"Successful":[], "Failed":[]}),
+ json!({"Successful":[{"Id":"9", "MessageId":"m", "MD5OfMessageBody":"x"}], "Failed":[]}),
+ json!({"Successful":[{"Id":"0", "MessageId":"m", "MD5OfMessageBody":"x"}], "Failed":[{"Id":"0", "Code":"Invalid", "SenderFault":false}]}),
+ ] {
+ let (endpoint, _) = mock_http(vec![response(body)]);
+ assert!(queue(&endpoint).await.publish(&event(1)).await.is_err());
+ }
+ let queue = queue("http://127.0.0.1:1").await;
+ assert!(queue.publish_batch(&[]).await.is_err());
+ assert!(queue.publish_batch(&vec![event(1); 11]).await.is_err());
+}
+
+#[tokio::test]
+async fn long_poll_counts_visibility_and_ack_use_delivery_origin_including_dlq() {
+ for source in [QueueSource::Main, QueueSource::DeadLetter] {
+ let body = json!({"Messages": [
+ {"MessageId":"m", "ReceiptHandle":"receipt", "Body":serde_json::to_string(&event(1)).unwrap(), "Attributes":{"ApproximateReceiveCount":"5"}},
+ {"MessageId":"bad", "ReceiptHandle":"bad-receipt", "Body":"{\"key\":\"untrusted\"}", "Attributes":{"ApproximateReceiveCount":"1"}}
+ ]});
+ let (endpoint, requests) = mock_http(vec![
+ response(body),
+ response(json!({})),
+ response(json!({})),
+ ]);
+ let queue = queue(&endpoint).await;
+ let deliveries = queue.receive(source).await.unwrap();
+ assert_eq!(deliveries.len(), 2);
+ assert_eq!(deliveries[0].receive_count, 5);
+ assert_eq!(deliveries[0].event, Ok(event(1)));
+ assert_eq!(deliveries[1].event, Err(ImportError::InvalidInput));
+ assert!(
+ queue
+ .extend_visibility(&deliveries[0], 43_201)
+ .await
+ .is_err()
+ );
+ queue.extend_visibility(&deliveries[0], 180).await.unwrap();
+ queue.delete(&deliveries[0]).await.unwrap();
+ let poll = request_body(&requests.recv().unwrap());
+ assert_eq!(poll["WaitTimeSeconds"], 20);
+ assert_eq!(poll["MaxNumberOfMessages"], 10);
+ assert_eq!(poll["VisibilityTimeout"], 180);
+ assert_eq!(
+ poll["MessageSystemAttributeNames"],
+ json!(["ApproximateReceiveCount"])
+ );
+ assert_eq!(poll["QueueUrl"], queue.url(source));
+ let visibility = request_body(&requests.recv().unwrap());
+ assert_eq!(visibility["QueueUrl"], queue.url(source));
+ assert_eq!(visibility["VisibilityTimeout"], 180);
+ assert_eq!(visibility["ReceiptHandle"], "receipt");
+ let delete = request_body(&requests.recv().unwrap());
+ assert_eq!(delete["QueueUrl"], queue.url(source));
+ assert_eq!(delete["ReceiptHandle"], "receipt");
+ }
+}
+
+#[tokio::test]
+async fn driver_polls_one_delivery_per_available_slot() {
+ let (endpoint, requests) = mock_http(vec![response(json!({"Messages": []}))]);
+ assert!(
+ ImportConsumer::receive(&queue(&endpoint).await, false)
+ .await
+ .unwrap()
+ .is_none()
+ );
+ let poll = request_body(&requests.recv().unwrap());
+ assert_eq!(poll["MaxNumberOfMessages"], 1);
+ assert_eq!(poll["VisibilityTimeout"], 180);
+}
+
+#[test]
+fn missing_receipt_or_receive_count_never_becomes_an_acknowledgeable_delivery() {
+ assert!(delivery(QueueSource::Main, Message::builder().body("{}").build()).is_err());
+ assert!(
+ delivery(
+ QueueSource::Main,
+ Message::builder()
+ .receipt_handle("receipt")
+ .body("{}")
+ .build()
+ )
+ .is_err()
+ );
+}
diff --git a/crates/slack_integration/src/outbound/test_support.rs b/crates/slack_integration/src/outbound/test_support.rs
new file mode 100644
index 0000000000..d7828c5d01
--- /dev/null
+++ b/crates/slack_integration/src/outbound/test_support.rs
@@ -0,0 +1,60 @@
+//! Tiny local HTTP peer for exercising the real AWS serializers and response handling.
+
+use std::{
+ io::{Read, Write},
+ net::TcpListener,
+ sync::mpsc::{self, Receiver},
+ thread,
+ time::{Duration, Instant},
+};
+
+pub(super) fn mock_http(responses: Vec<String>) -> (String, Receiver<String>) {
+ let listener = TcpListener::bind("127.0.0.1:0").unwrap();
+ listener.set_nonblocking(true).unwrap();
+ let endpoint = format!("http://{}", listener.local_addr().unwrap());
+ let (send, requests) = mpsc::channel();
+ thread::spawn(move || {
+ for response in responses {
+ let deadline = Instant::now() + Duration::from_secs(10);
+ let mut socket = loop {
+ match listener.accept() {
+ Ok((socket, _)) => break socket,
+ Err(error) if error.kind() == std::io::ErrorKind::WouldBlock => {
+ if Instant::now() >= deadline {
+ return;
+ }
+ thread::sleep(Duration::from_millis(5));
+ }
+ Err(error) => panic!("mock accept failed: {error}"),
+ }
+ };
+ socket
+ .set_read_timeout(Some(Duration::from_secs(5)))
+ .unwrap();
+ let mut request = Vec::new();
+ loop {
+ let mut buffer = [0u8; 4096];
+ let count = socket.read(&mut buffer).unwrap();
+ assert_ne!(count, 0);
+ request.extend_from_slice(&buffer[..count]);
+ let text = String::from_utf8_lossy(&request);
+ if let Some(end) = text.find("\r\n\r\n") {
+ let length = text[..end]
+ .lines()
+ .find_map(|line| {
+ let (name, value) = line.split_once(':')?;
+ name.eq_ignore_ascii_case("content-length")
+ .then(|| value.trim().parse::<usize>().unwrap())
+ })
+ .unwrap_or(0);
+ if request.len() >= end + 4 + length {
+ break;
+ }
+ }
+ }
+ let _ = send.send(String::from_utf8(request).unwrap());
+ socket.write_all(response.as_bytes()).unwrap();
+ }
+ });
+ (endpoint, requests)
+}
diff --git a/crates/slack_integration/tests/fixtures/corporate-export.json b/crates/slack_integration/tests/fixtures/corporate-export.json
new file mode 100644
index 0000000000..75fafff6e2
--- /dev/null
+++ b/crates/slack_integration/tests/fixtures/corporate-export.json
@@ -0,0 +1,18 @@
+{
+ "fixture_notice": "Synthetic corporate export roots; no customer data.",
+ "groups.json": [
+ {"id":"C200","name":"synthetic-private","created":"1700000001","creator":"U200","members":["U100","U200"],"is_archived":true}
+ ],
+ "dms.json": [
+ {"id":"D100","members":["U100","U200"],"created":1700000002},
+ {"id":"D101","members":["U100","U400"],"created":null},
+ {"id":"D102","members":["U100","U300"]}
+ ],
+ "mpims.json": [
+ {"id":"G100","name":"mpdm-synthetic","members":["U100","U200","U400"],"created":"1700000003.000001"}
+ ],
+ "users.json": [
+ {"id":"U400","name":"no-email","profile":{"display_name":"External Example"}},
+ {"id":"U500","name":"integration","is_bot":true,"profile":{"email":"[email protected]"}}
+ ]
+}
diff --git a/crates/slack_integration/tests/fixtures/enterprise-export.json b/crates/slack_integration/tests/fixtures/enterprise-export.json
new file mode 100644
index 0000000000..f9be342bb4
--- /dev/null
+++ b/crates/slack_integration/tests/fixtures/enterprise-export.json
@@ -0,0 +1,9 @@
+{
+ "fixture_notice": "Synthetic enterprise export roots; no customer data.",
+ "channels.json": [{"id":"C300","name":"enterprise-synthetic","members":["W100","W200","U999"],"created":1700000004}],
+ "org_users.json": [
+ {"id":"W100","name":"enterprise-alice","profile":{"real_name":"Enterprise Example","email":"[email protected]"},"enterprise_user":{"id":"W100","teams":["T100"]}},
+ {"id":"W200","name":"phantom","profile":null,"is_stranger":true},
+ {"id":"U999","name":"connect-guest","profile":{"display_name":"Connect Guest"},"team_id":"T999","is_stranger":true}
+ ]
+}
diff --git a/crates/slack_integration/tests/fixtures/message-events.ndjson b/crates/slack_integration/tests/fixtures/message-events.ndjson
new file mode 100644
index 0000000000..ab88000cc3
--- /dev/null
+++ b/crates/slack_integration/tests/fixtures/message-events.ndjson
@@ -0,0 +1,11 @@
+{"type":"message","ts":"1700000000.000001","thread_ts":"1700000000.000001","user":"U100","text":"Synthetic root","replies":[{"user":"U200","ts":"1700086400.000003"}],"files":[{"id":"F100"}],"attachments":[{"text":"Ignored synthetic attachment"}]}
+{"type":"message","ts":"1700000000.000002","subtype":"future_slack_subtype","user":"U200","text":"Adjacent microsecond","unknown":{"field":true}}
+{"type":"message","ts":"1700086400.000003","thread_ts":"1700000000.000001","subtype":"thread_broadcast","user":"U200","text":"Next-day reply"}
+{"type":"message","subtype":"message_changed","ts":"1700090000.000004","event_ts":"1700090000.000004","message":{"type":"message","ts":"1700086400.000003","thread_ts":"1700000000.000001","user":"U200","text":"Edited next-day reply"},"previous_message":{"ts":"1700086400.000003","text":"Old text"}}
+{"type":"message","subtype":"message_deleted","deleted_ts":"1700000000.000001","previous_message":{"ts":"1700000000.000001","text":"Do not import previous content"}}
+{"type":"message","subtype":"tombstone","ts":"1700000000.000005","text":"This message was deleted"}
+{"type":"message","ts":"1700172800.000001","thread_ts":"1699999999.999999","user":"W200","user_profile":{"display_name":"Phantom Example"},"text":"Missing root"}
+{"type":"message","subtype":"bot_message","ts":"1700172800.000002","user":"U100","username":"Synthetic Build Bot","user_profile":{"email":"[email protected]"},"text":"Bot attribution stays a bot"}
+{"type":"message","ts":"1700172800.000003","user":"U999","user_profile":{"display_name":"Connect Example","email":"[email protected]"},"text":"Synthetic external author"}
+{"type":"message","ts":"1700172800.000004","user":"USLACKBOT","text":"Synthetic Slackbot"}
+{"type":"reaction_added","event_ts":"1700172800.000005","user":"U100"}
diff --git a/crates/slack_integration/tests/fixtures/native-message-links.json b/crates/slack_integration/tests/fixtures/native-message-links.json
new file mode 100644
index 0000000000..362f1d7d79
--- /dev/null
+++ b/crates/slack_integration/tests/fixtures/native-message-links.json
@@ -0,0 +1,108 @@
+[
+ {
+ "name": "external",
+ "kind": "link",
+ "source": "<https://example.com/a(b)?x=1&amp;y=2|Example>",
+ "native": "<m-link>{\"url\":\"https://example.com/a(b)?x=1&y=2\",\"text\":\"Example\",\"title\":\"\"}</m-link>",
+ "payload": {"url":"https://example.com/a(b)?x=1&y=2","text":"Example","title":""}
+ },
+ {
+ "name": "bare-url",
+ "kind": "link",
+ "source": "https://example.com/a_path",
+ "native": "<m-link>{\"url\":\"https://example.com/a_path\",\"text\":\"https://example.com/a_path\",\"title\":\"\"}</m-link>",
+ "payload": {"url":"https://example.com/a_path","text":"https://example.com/a_path","title":""}
+ },
+ {
+ "name": "unlabeled-angle-url",
+ "kind": "link",
+ "source": "<https://example.com>",
+ "native": "<m-link>{\"url\":\"https://example.com\",\"text\":\"https://example.com\",\"title\":\"\"}</m-link>",
+ "payload": {"url":"https://example.com","text":"https://example.com","title":""}
+ },
+ {
+ "name": "mailto",
+ "kind": "link",
+ "source": "<mailto:[email protected]>",
+ "native": "<m-link>{\"url\":\"mailto:[email protected]\",\"text\":\"[email protected]\",\"title\":\"\"}</m-link>",
+ "payload": {"url":"mailto:[email protected]","text":"[email protected]","title":""}
+ },
+ {
+ "name": "untrusted-label",
+ "kind": "link",
+ "native": "<m-link>{\"url\":\"https://example.com\",\"text\":\"quote \\\" slash \\\\ newline\\n&amp; 世界 \\u003c/m-link\\u003e\\u003cm-user-mention\\u003enested\\u003c/m-user-mention\\u003e\",\"title\":\"\"}</m-link>",
+ "payload": {"url":"https://example.com","text":"quote \" slash \\ newline\n&amp; 世界 </m-link><m-user-mention>nested</m-user-mention>","title":""}
+ },
+ {
+ "name": "encoded-source-label",
+ "kind": "link",
+ "source": "<https://example.com|&lt;/m-link&gt;&lt;m-user-mention&gt;not a mention>",
+ "native": "<m-link>{\"url\":\"https://example.com\",\"text\":\"\\u003c/m-link\\u003e\\u003cm-user-mention\\u003enot a mention\",\"title\":\"\"}</m-link>",
+ "payload": {"url":"https://example.com","text":"</m-link><m-user-mention>not a mention","title":""}
+ },
+ {
+ "name": "user",
+ "kind": "user-mention",
+ "source": "<@U1>",
+ "native": "<m-user-mention>{\"userId\":\"macro|[email protected]\",\"email\":\"[email protected]\"}</m-user-mention>",
+ "payload": {"userId":"macro|[email protected]","email":"[email protected]"}
+ },
+ {
+ "name": "channel",
+ "kind": "document-mention",
+ "source": "<#C1|source label>",
+ "native": "<m-document-mention>{\"documentId\":\"019a0000-0000-7000-8000-000000000001\",\"blockName\":\"channel\",\"documentName\":\"general\",\"blockParams\":{},\"collapsed\":false}</m-document-mention>",
+ "payload": {"documentId":"019a0000-0000-7000-8000-000000000001","blockName":"channel","documentName":"general","blockParams":{},"collapsed":false}
+ },
+ {
+ "name": "root",
+ "kind": "document-mention",
+ "source": "<https://example.slack.com/archives/C1/p1700000000000001>",
+ "native": "<m-document-mention>{\"documentId\":\"019a0000-0000-7000-8000-000000000001\",\"blockName\":\"channel\",\"documentName\":\"general\",\"blockParams\":{\"channel_message_id\":\"019a0000-0000-7000-8000-000000000002\"},\"collapsed\":false}</m-document-mention>",
+ "payload": {"documentId":"019a0000-0000-7000-8000-000000000001","blockName":"channel","documentName":"general","blockParams":{"channel_message_id":"019a0000-0000-7000-8000-000000000002"},"collapsed":false}
+ },
+ {
+ "name": "reply",
+ "kind": "document-mention",
+ "source": "<https://example.slack.com/archives/C1/p1700000000000002?thread_ts=1700000000.000001&amp;cid=C1|reply>",
+ "native": "<m-document-mention>{\"documentId\":\"019a0000-0000-7000-8000-000000000001\",\"blockName\":\"channel\",\"documentName\":\"general\",\"blockParams\":{\"channel_message_id\":\"019a0000-0000-7000-8000-000000000003\",\"channel_thread_id\":\"019a0000-0000-7000-8000-000000000002\"},\"collapsed\":false}</m-document-mention>",
+ "payload": {"documentId":"019a0000-0000-7000-8000-000000000001","blockName":"channel","documentName":"general","blockParams":{"channel_message_id":"019a0000-0000-7000-8000-000000000003","channel_thread_id":"019a0000-0000-7000-8000-000000000002"},"collapsed":false}
+ },
+ {
+ "name": "channel-untrusted-name",
+ "kind": "document-mention",
+ "source": "<#C1>",
+ "native": "<m-document-mention>{\"documentId\":\"019a0000-0000-7000-8000-000000000001\",\"blockName\":\"channel\",\"documentName\":\"quote \\\" slash \\\\ newline\\n&amp; 世界 \\u003c/m-document-mention\\u003e\",\"blockParams\":{},\"collapsed\":false}</m-document-mention>",
+ "payload": {"documentId":"019a0000-0000-7000-8000-000000000001","blockName":"channel","documentName":"quote \" slash \\ newline\n&amp; 世界 </m-document-mention>","blockParams":{},"collapsed":false}
+ },
+ {
+ "name": "inline-code",
+ "kind": "inert",
+ "source": "`<m-user-mention>{\"userId\":\"macro|[email protected]\",\"email\":\"[email protected]\"}</m-user-mention>`",
+ "native": "`<m-user-mention>{\"userId\":\"macro|[email protected]\",\"email\":\"[email protected]\"}</m-user-mention>`"
+ },
+ {
+ "name": "fenced-code",
+ "kind": "inert",
+ "source": "```\n<m-link>{\"url\":\"https://example.com\",\"text\":\"link\",\"title\":\"\"}</m-link>\n```",
+ "native": "```\n<m-link>{\"url\":\"https://example.com\",\"text\":\"link\",\"title\":\"\"}</m-link>\n```"
+ },
+ {
+ "name": "unsafe-scheme",
+ "kind": "inert",
+ "source": "<javascript:alert(1)|not clickable>",
+ "native": "not clickable"
+ },
+ {
+ "name": "source-link-tag",
+ "kind": "inert",
+ "source": "<m-link>{\"url\":\"https://example.com\",\"text\":\"label\",\"title\":\"\"}</m-link>",
+ "native": "&lt;m\\-link&gt;{\"url\":\"https://example\\.com\",\"text\":\"label\",\"title\":\"\"}&lt;/m\\-link&gt;"
+ },
+ {
+ "name": "source-tag",
+ "kind": "inert",
+ "source": "<m-user-mention>{\"userId\":\"macro|[email protected]\",\"email\":\"[email protected]\"}</m-user-mention>",
+ "native": "&lt;m\\-user\\-mention&gt;{\"userId\":\"macro\\|fake@example\\.com\",\"email\":\"fake@example\\.com\"}&lt;/m\\-user\\-mention&gt;"
+ }
+]
diff --git a/crates/slack_integration/tests/fixtures/standard-export.json b/crates/slack_integration/tests/fixtures/standard-export.json
new file mode 100644
index 0000000000..060dbb6734
--- /dev/null
+++ b/crates/slack_integration/tests/fixtures/standard-export.json
@@ -0,0 +1,13 @@
+{
+ "fixture_notice": "Synthetic export roots; all identities and content are invented.",
+ "channels.json": [
+ {"id":"C100","name":"synthetic-general","created":1700000000,"creator":"U100","members":["U100","U200","U300"],"is_archived":false,"purpose":{"value":"Synthetic"},"future_field":true},
+ {"id":"C101","name":"missing-creation","members":[],"is_archived":true}
+ ],
+ "users.json": [
+ {"id":"U100","name":"alice","real_name":"Alice Example","profile":{"display_name":"Alice","email":"[email protected]"}},
+ {"id":"U200","name":"bob","profile":{"display_name":"Bob","email":"[email protected]"}},
+ {"id":"U300","name":"duplicate-alice","profile":{"email":"[email protected]"}},
+ {"id":"USLACKBOT","name":"slackbot","profile":{"email":"[email protected]"},"future_field":{}}
+ ]
+}
diff --git a/crates/slack_integration/tests/schema.rs b/crates/slack_integration/tests/schema.rs
new file mode 100644
index 0000000000..b7cbef6e0b
--- /dev/null
+++ b/crates/slack_integration/tests/schema.rs
@@ -0,0 +1,487 @@
+//! Migration-level contract tests. Raw SQL is intentional here: these scripts
+//! exercise DDL, deferred constraints and invalid SQL shapes, not adapter queries.
+#![cfg(feature = "postgres")]
+
+use sqlx::{PgPool, Postgres, Transaction};
+
+const TEAM: &str = "019a0000-0000-7000-8000-000000000001";
+const JOB: &str = "019a0000-0000-7000-8000-000000000002";
+const CHANNEL: &str = "019a0000-0000-7000-8000-000000000003";
+const MESSAGE: &str = "019a0000-0000-7000-8000-000000000004";
+const CANDIDATE: &str = "019a0000-0000-7000-8000-000000000005";
+const EMPTY_HASH: &str = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";
+
+async fn execute(tx: &mut Transaction<'_, Postgres>, script: &str) {
+ sqlx::raw_sql(script)
+ .execute(&mut **tx)
+ .await
+ .unwrap_or_else(|error| panic!("SQL failed: {error}\n{script}"));
+}
+
+async fn rejects(tx: &mut Transaction<'_, Postgres>, script: &str, code: &str) {
+ execute(tx, "SAVEPOINT expected_failure").await;
+ let error = sqlx::raw_sql(script)
+ .execute(&mut **tx)
+ .await
+ .expect_err("invalid schema mutation must fail");
+ assert_eq!(
+ error.as_database_error().unwrap().code().as_deref(),
+ Some(code),
+ "{error}: {script}"
+ );
+ execute(
+ tx,
+ "ROLLBACK TO SAVEPOINT expected_failure; RELEASE SAVEPOINT expected_failure",
+ )
+ .await;
+}
+
+async fn fixture(pool: &PgPool) -> Transaction<'_, Postgres> {
+ fixture_with_history(pool, true).await
+}
+
+async fn fixture_with_history(pool: &PgPool, history: bool) -> Transaction<'_, Postgres> {
+ let mut tx = pool.begin().await.unwrap();
+ execute(&mut tx, &format!(
+ r#"
+ INSERT INTO macro_user (id, username, email, stripe_customer_id)
+ VALUES ('{TEAM}', 'schema', '[email protected]', 'schema-customer');
+ INSERT INTO "User" (id, email, macro_user_id)
+ VALUES ('macro|[email protected]', '[email protected]', '{TEAM}');
+ INSERT INTO team (id, name, owner_id) VALUES ('{TEAM}', 'Schema', 'macro|[email protected]');
+ INSERT INTO import_source_binding (team_id, confirmed_unknown_at) VALUES ('{TEAM}', now());
+ INSERT INTO slack_import_job
+ (id, team_id, user_id, idempotency_token, request_sha256, confirmed_unknown, include_message_history)
+ VALUES ('{JOB}', '{TEAM}', 'macro|[email protected]', '{JOB}', repeat('a', 64), true, {history});
+ INSERT INTO slack_import_conversation
+ (job_id, slack_channel_id, kind, name, folder, member_ids, archived)
+ VALUES ('{JOB}', 'C123', 'public_channel', 'general', 'general', ARRAY['U123'], false);
+ INSERT INTO comms_channels (id, name, channel_type, owner_id)
+ VALUES ('{CHANNEL}', 'Imported private', 'private', 'macro|[email protected]');
+ INSERT INTO import_target_reservation (team_id, source, foreign_id, candidate_channel_id)
+ VALUES ('{TEAM}', 'slack', 'C123', '{CANDIDATE}');
+ "#
+ )).await;
+ tx
+}
+
+fn users_upload() -> String {
+ format!(
+ "INSERT INTO slack_import_upload (job_id, object_key, sha256, byte_length)
+ VALUES ('{JOB}', 'slack-import/users.json', repeat('a', 64), 10)"
+ )
+}
+
+fn part_upload(index: u32) -> String {
+ format!("INSERT INTO slack_import_upload
+ (job_id, slack_channel_id, part_index, object_key, sha256, byte_length, record_count)
+ VALUES ('{JOB}', 'C123', {index}, 'slack-import/{index}.ndjson', repeat('b', 64), 20, 1)")
+}
+
+fn mapping() -> String {
+ format!("INSERT INTO slack_import_message_map (team_id, slack_channel_id, slack_ts, message_id, channel_id)
+ VALUES ('{TEAM}', 'C123', 1700000000000001, '{MESSAGE}', '{CHANNEL}')")
+}
+
+fn message() -> String {
+ format!(
+ "INSERT INTO comms_messages (id, channel_id, sender_id, content)
+ VALUES ('{MESSAGE}', '{CHANNEL}', 'macro|[email protected]', 'Historical message')"
+ )
+}
+
+#[sqlx::test(migrations = "../macro_db_client/migrations")]
+async fn natural_keys_and_old_ledger_writers_remain_compatible(pool: PgPool) {
+ let mut tx = fixture(&pool).await;
+ for initiator in ["onboarding", "chat", "archive"] {
+ execute(
+ &mut tx,
+ &format!(
+ "INSERT INTO import_entity (user_id, source, foreign_id, initiator)
+ VALUES ('macro|[email protected]', 'slack', '{initiator}', '{initiator}')"
+ ),
+ )
+ .await;
+ }
+ let duplicates = [
+ format!("INSERT INTO import_source_binding (team_id, slack_workspace_id) VALUES ('{TEAM}', 'T123')"),
+ format!("INSERT INTO import_target_reservation (team_id, source, foreign_id, candidate_channel_id)
+ VALUES ('{TEAM}', 'slack', 'C123', '{CHANNEL}')"),
+ format!("INSERT INTO slack_import_job
+ (id, team_id, user_id, idempotency_token, request_sha256, confirmed_unknown, include_message_history)
+ VALUES ('{MESSAGE}', '{TEAM}', 'macro|[email protected]', '{JOB}', repeat('a', 64), true, true)"),
+ format!("INSERT INTO slack_import_conversation
+ (job_id, slack_channel_id, kind, name, folder, member_ids, archived)
+ VALUES ('{JOB}', 'C123', 'public_channel', 'other', 'other', '{{}}', false)"),
+ ];
+ for statement in duplicates {
+ rejects(&mut tx, &statement, "23505").await;
+ }
+ execute(&mut tx, &users_upload()).await;
+ rejects(&mut tx, &users_upload(), "23505").await;
+ execute(&mut tx, &part_upload(0)).await;
+ rejects(&mut tx, &part_upload(0), "23505").await;
+ execute(&mut tx, &mapping()).await;
+ execute(&mut tx, &message()).await;
+ rejects(&mut tx, &mapping(), "23505").await;
+ execute(
+ &mut tx,
+ &format!(
+ "INSERT INTO slack_import_outbox (job_id, slack_channel_id, kind, generation)
+ VALUES ('{JOB}', 'C123', 'import', 1), ('{JOB}', 'C123', 'search', 1)"
+ ),
+ )
+ .await;
+ rejects(
+ &mut tx,
+ &format!(
+ "INSERT INTO slack_import_outbox (job_id, slack_channel_id, kind, generation)
+ VALUES ('{JOB}', 'C123', 'import', 1)"
+ ),
+ "23505",
+ )
+ .await;
+ tx.commit().await.unwrap();
+}
+
+#[sqlx::test(migrations = "../macro_db_client/migrations")]
+async fn upload_descriptors_verification_and_seals_are_immutable(pool: PgPool) {
+ let mut tx = fixture(&pool).await;
+ execute(&mut tx, &users_upload()).await;
+ execute(&mut tx, &part_upload(0)).await;
+ for change in [
+ "sha256 = repeat('c', 64)",
+ "byte_length = 21",
+ "part_index = 1",
+ "object_key = 'changed'",
+ ] {
+ rejects(
+ &mut tx,
+ &format!("UPDATE slack_import_upload SET {change} WHERE slack_channel_id = 'C123'"),
+ "23514",
+ )
+ .await;
+ }
+ rejects(
+ &mut tx,
+ "UPDATE slack_import_upload SET verified_at = now()",
+ "23514",
+ )
+ .await;
+ execute(
+ &mut tx,
+ "UPDATE slack_import_upload SET verified_at = now(), verified_etag = 'pinned-etag'",
+ )
+ .await;
+ rejects(
+ &mut tx,
+ "UPDATE slack_import_upload SET verified_etag = 'replacement'",
+ "23514",
+ )
+ .await;
+ rejects(&mut tx, "DELETE FROM slack_import_upload", "23514").await;
+ rejects(&mut tx, &format!(
+ "UPDATE slack_import_conversation SET sealed_at = now(), part_count = 0, manifest_sha256 = '{EMPTY_HASH}'"
+ ), "23514").await;
+ execute(
+ &mut tx,
+ "UPDATE slack_import_conversation SET sealed_at = now(), part_count = 1,
+ manifest_sha256 = encode(digest('0:' || repeat('b', 64) || E':20:1\n', 'sha256'), 'hex')",
+ )
+ .await;
+ rejects(&mut tx, &part_upload(1), "23514").await;
+ rejects(&mut tx, "UPDATE slack_import_conversation SET sealed_at = NULL, part_count = NULL, manifest_sha256 = NULL", "23514").await;
+ rejects(
+ &mut tx,
+ "UPDATE slack_import_conversation SET member_ids = ARRAY['U999']",
+ "23514",
+ )
+ .await;
+ execute(
+ &mut tx,
+ "UPDATE slack_import_job SET registration_closed_at = now(), finalized_at = now()",
+ )
+ .await;
+ rejects(
+ &mut tx,
+ "UPDATE slack_import_job SET registration_closed_at = NULL",
+ "23514",
+ )
+ .await;
+ // Exact verification retries can still observe the pinned identity after closure.
+ execute(
+ &mut tx,
+ "UPDATE slack_import_upload SET verified_etag = verified_etag",
+ )
+ .await;
+ tx.commit().await.unwrap();
+}
+
+#[sqlx::test(migrations = "../macro_db_client/migrations")]
+async fn shape_only_empty_seals_gaps_and_closed_registration(pool: PgPool) {
+ let mut tx = fixture(&pool).await;
+ execute(&mut tx, &part_upload(1)).await;
+ rejects(
+ &mut tx,
+ "UPDATE slack_import_conversation SET sealed_at = now(), part_count = 1,
+ manifest_sha256 = encode(digest('1:' || repeat('b', 64) || E':20:1\n', 'sha256'), 'hex')",
+ "23514",
+ )
+ .await;
+ tx.rollback().await.unwrap();
+
+ let mut tx = fixture_with_history(&pool, false).await;
+ rejects(&mut tx, &part_upload(0), "23514").await;
+ execute(&mut tx, &users_upload()).await;
+ execute(&mut tx, &format!(
+ "UPDATE slack_import_conversation SET sealed_at = now(), part_count = 0, manifest_sha256 = '{EMPTY_HASH}'"
+ )).await;
+ execute(
+ &mut tx,
+ "UPDATE slack_import_job SET registration_closed_at = now(), finalized_at = now()",
+ )
+ .await;
+ rejects(
+ &mut tx,
+ "UPDATE slack_import_upload SET verified_at = now(), verified_version_id = 'version'",
+ "23514",
+ )
+ .await;
+ rejects(&mut tx, &part_upload(0), "23514").await;
+ tx.commit().await.unwrap();
+}
+
+#[sqlx::test(migrations = "../macro_db_client/migrations")]
+async fn leases_counters_search_and_reservations_have_valid_shapes(pool: PgPool) {
+ let mut tx = fixture(&pool).await;
+ for change in [
+ "status = 'invalid'",
+ "status = 'importing'",
+ "processed = 1",
+ "attempts = -1",
+ "part_count = 0",
+ "checkpoint_record = -1",
+ "search_state = 'submitted'",
+ "search_submitted_generation = 1",
+ "lease_generation = -1",
+ ] {
+ rejects(
+ &mut tx,
+ &format!("UPDATE slack_import_conversation SET {change}"),
+ "23514",
+ )
+ .await;
+ }
+ execute(&mut tx, &format!(
+ "UPDATE slack_import_conversation SET status = 'importing', lease_owner = '{JOB}', lease_token = '{MESSAGE}',
+ lease_generation = 1, lease_expires_at = now() + interval '5 minutes', heartbeat_at = now(), attempts = 1,
+ processed = 3, imported = 1, duplicates = 1, skipped = 1, reactions = 2,
+ search_state = 'submitted', search_dirty_generation = 1, search_submitted_generation = 1, search_receipt_id = '{JOB}'"
+ )).await;
+ rejects(
+ &mut tx,
+ &format!("UPDATE import_target_reservation SET candidate_channel_id = '{CHANNEL}'"),
+ "23514",
+ )
+ .await;
+ rejects(
+ &mut tx,
+ "UPDATE import_target_reservation SET state = 'ready'",
+ "23514",
+ )
+ .await;
+ execute(
+ &mut tx,
+ &format!("UPDATE import_target_reservation SET state = 'ready', channel_id = '{CHANNEL}'"),
+ )
+ .await;
+ rejects(
+ &mut tx,
+ "UPDATE import_target_reservation SET state = 'pending', channel_id = NULL",
+ "23514",
+ )
+ .await;
+ execute(
+ &mut tx,
+ "UPDATE import_source_binding SET slack_workspace_id = 'T123'",
+ )
+ .await;
+ rejects(
+ &mut tx,
+ "UPDATE import_source_binding SET slack_workspace_id = 'T456'",
+ "23514",
+ )
+ .await;
+ rejects(
+ &mut tx,
+ "UPDATE import_source_binding SET slack_workspace_id = NULL",
+ "23514",
+ )
+ .await;
+ rejects(
+ &mut tx,
+ "UPDATE slack_import_job SET include_message_history = false",
+ "23514",
+ )
+ .await;
+ rejects(&mut tx, &format!(
+ "INSERT INTO slack_import_conversation
+ (job_id, slack_channel_id, kind, name, folder, member_ids, archived, sealed_at, part_count, manifest_sha256)
+ VALUES ('{JOB}', 'C456', 'public_channel', 'other', 'other', '{{}}', false, now(), 0, '{EMPTY_HASH}')"
+ ), "23514").await;
+ rejects(
+ &mut tx,
+ "UPDATE slack_import_job SET status = 'cancelled'",
+ "23514",
+ )
+ .await;
+ // Private channels must still have no team_id. Provenance is import-owned.
+ rejects(
+ &mut tx,
+ &format!("UPDATE comms_channels SET team_id = '{TEAM}'"),
+ "23514",
+ )
+ .await;
+ tx.commit().await.unwrap();
+}
+
+#[sqlx::test(migrations = "../macro_db_client/migrations")]
+async fn message_fk_is_deferred_and_checks_parent_identity(pool: PgPool) {
+ let mut tx = fixture(&pool).await;
+ execute(&mut tx, &mapping()).await;
+ rejects(&mut tx, "SET CONSTRAINTS ALL IMMEDIATE", "23503").await;
+ execute(&mut tx, &message()).await;
+ execute(&mut tx, "SET CONSTRAINTS ALL IMMEDIATE").await;
+ rejects(
+ &mut tx,
+ &format!("UPDATE slack_import_message_map SET channel_id = '{CANDIDATE}'"),
+ "23503",
+ )
+ .await;
+ rejects(
+ &mut tx,
+ "UPDATE slack_import_message_map SET slack_ts = -1",
+ "23514",
+ )
+ .await;
+ // A failed enclosing batch leaves neither history nor its dedupe key behind.
+ tx.rollback().await.unwrap();
+ let mut tx = pool.begin().await.unwrap();
+ execute(
+ &mut tx,
+ "DO $$ BEGIN
+ ASSERT NOT EXISTS (SELECT FROM slack_import_message_map);
+ ASSERT NOT EXISTS (SELECT FROM comms_messages);
+ END $$",
+ )
+ .await;
+ tx.commit().await.unwrap();
+}
+
+#[sqlx::test(migrations = "../macro_db_client/migrations")]
+async fn job_cleanup_preserves_history_and_provenance_channel_cleanup_is_deliberate(pool: PgPool) {
+ let mut tx = fixture(&pool).await;
+ execute(&mut tx, &users_upload()).await;
+ execute(&mut tx, &part_upload(0)).await;
+ execute(&mut tx, &mapping()).await;
+ execute(&mut tx, &message()).await;
+ execute(&mut tx, &format!(
+ "UPDATE import_target_reservation SET state = 'ready', channel_id = '{CHANNEL}';
+ INSERT INTO slack_import_outbox (job_id, slack_channel_id, kind, generation) VALUES ('{JOB}', 'C123', 'search', 1);
+ INSERT INTO slack_import_message_reference
+ (job_id, slack_channel_id, message_id, channel_id, importer_version, template)
+ VALUES ('{JOB}', 'C123', '{MESSAGE}', '{CHANNEL}', 1,
+ jsonb_build_object('body', '#source', 'references', jsonb_build_array(jsonb_build_object('kind', 'channel'))));
+ DELETE FROM slack_import_job WHERE id = '{JOB}';"
+ )).await;
+ execute(
+ &mut tx,
+ "DO $$ BEGIN
+ ASSERT NOT EXISTS (SELECT FROM slack_import_conversation);
+ ASSERT NOT EXISTS (SELECT FROM slack_import_upload);
+ ASSERT NOT EXISTS (SELECT FROM slack_import_outbox);
+ ASSERT NOT EXISTS (SELECT FROM slack_import_message_reference);
+ ASSERT (SELECT count(*) = 1 FROM slack_import_message_map);
+ ASSERT (SELECT count(*) = 1 FROM import_source_binding);
+ ASSERT (SELECT count(*) = 1 FROM import_target_reservation);
+ ASSERT (SELECT count(*) = 1 FROM comms_messages);
+ END $$",
+ )
+ .await;
+ execute(
+ &mut tx,
+ &format!("DELETE FROM comms_channels WHERE id = '{CHANNEL}'"),
+ )
+ .await;
+ execute(
+ &mut tx,
+ "DO $$ BEGIN
+ ASSERT NOT EXISTS (SELECT FROM slack_import_message_map);
+ ASSERT NOT EXISTS (SELECT FROM import_target_reservation);
+ ASSERT (SELECT count(*) = 1 FROM import_source_binding);
+ END $$",
+ )
+ .await;
+ tx.commit().await.unwrap();
+}
+
+#[sqlx::test(migrations = "../macro_db_client/migrations")]
+async fn reference_templates_are_bounded_and_completion_releases_payload(pool: PgPool) {
+ let mut tx = fixture(&pool).await;
+ execute(&mut tx, &format!(
+ "INSERT INTO slack_import_message_reference
+ (job_id, slack_channel_id, message_id, channel_id, importer_version, template)
+ VALUES ('{JOB}', 'C123', '{MESSAGE}', '{CHANNEL}', 1,
+ jsonb_build_object('body', '#source', 'references', jsonb_build_array(jsonb_build_object('kind', 'channel'))))"
+ )).await;
+ for change in [
+ "importer_version = 0",
+ "template = NULL",
+ "completed_at = now()",
+ "template = jsonb_build_object('references', '[]'::jsonb)",
+ "template = jsonb_build_object('references', (SELECT jsonb_agg(i) FROM generate_series(1, 257) i))",
+ "template = jsonb_build_object('references', '[1]'::jsonb, 'body', repeat('x', 4194304))",
+ ] {
+ rejects(
+ &mut tx,
+ &format!("UPDATE slack_import_message_reference SET {change}"),
+ "23514",
+ )
+ .await;
+ }
+ execute(
+ &mut tx,
+ "UPDATE slack_import_message_reference SET completed_at = now(), template = NULL",
+ )
+ .await;
+ rejects(
+ &mut tx,
+ "UPDATE slack_import_message_reference SET completed_at = NULL",
+ "23514",
+ )
+ .await;
+ tx.commit().await.unwrap();
+}
+
+#[sqlx::test(migrations = "../macro_db_client/migrations")]
+async fn team_cleanup_does_not_delete_private_history(pool: PgPool) {
+ let mut tx = fixture(&pool).await;
+ execute(&mut tx, &mapping()).await;
+ execute(&mut tx, &message()).await;
+ execute(&mut tx, &users_upload()).await;
+ execute(&mut tx, &format!("DELETE FROM team WHERE id = '{TEAM}'")).await;
+ execute(
+ &mut tx,
+ "DO $$ BEGIN
+ ASSERT NOT EXISTS (SELECT FROM slack_import_job);
+ ASSERT NOT EXISTS (SELECT FROM slack_import_upload);
+ ASSERT NOT EXISTS (SELECT FROM slack_import_message_map);
+ ASSERT NOT EXISTS (SELECT FROM import_source_binding);
+ ASSERT NOT EXISTS (SELECT FROM import_target_reservation);
+ ASSERT (SELECT count(*) = 1 FROM comms_channels);
+ ASSERT (SELECT count(*) = 1 FROM comms_messages);
+ END $$",
+ )
+ .await;
+ tx.commit().await.unwrap();
+}
diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml
index 84af9008b3..febdbab3cc 100644
--- a/docker/docker-compose.yml
+++ b/docker/docker-compose.yml
@@ -172,6 +172,28 @@ services:
retries: 3
start_period: 10s
+ # ============================================================================
+ # slack_import_worker (portless, opt-in profile: slack-import)
+ # Build separately; never part of the default or run-dev bundle.
+ # ============================================================================
+ slack_import_worker:
+ <<: *common-env
+ profiles:
+ - slack-import
+ build:
+ context: .
+ dockerfile: docker/Dockerfile
+ args:
+ SERVICE_NAME: slack_import_worker
+ stop_grace_period: 120s
+ depends_on:
+ - postgres
+ - search_processing_service
+ - connection_gateway
+ networks:
+ - databases
+ - services
+
# ============================================================================
# document_upload_finalizer (local worker)
# LocalStack S3 ObjectCreated -> SQS consumer for document upload finalization.
diff --git a/docs/AGENT_GUIDE/channels.md b/docs/AGENT_GUIDE/channels.md
index dd79a44dd6..e21e1705dd 100644
--- a/docs/AGENT_GUIDE/channels.md
+++ b/docs/AGENT_GUIDE/channels.md
@@ -10,6 +10,37 @@
Team channels are always discoverable to the team. Private channels can only be viewed or joined by invitation. A DM is a channel between two users.
An external email can be selected as a channel participant. For an unregistered recipient, clicking `Add` sends an email invite.
+## Imported Slack history
+
+Import starts in **Settings → Team → Connections → Import from Slack**, not the
+channel composer. Only explicitly selected Slack IDs create work. Slack public
+channels become Team channels with automatic team joining off; private channels
+and group DMs become Private. Two-person DMs keep exactly their mapped pair and
+never gain the importing admin as a third participant. Existing names, roles,
+leavers and newer activity are preserved. Imported history keeps source dates and
+does not produce live message notifications, invitations or bot invocations.
+Unknown authors display their Slack name through the system bot. Attachments and
+attachment-only messages are not imported. Cancellation keeps committed history;
+repeating an archive deduplicates source messages rather than applying later edits.
+
+For verification, use synthetic local history with a root, replies and reactions.
+Search for an imported author name absent from the body. Verify the result for a
+participant and its absence for an unrelated viewer after indexing has caught up.
+Job completion means required search publication finished, not immediate index
+refresh. Test existing channel live activity is not moved backward.
+
+Hover and click imported external links, channel mentions, root-message and reply
+references in the actual message body. Check destination channel/message/root
+UUIDs and that the correct message is revealed, including offscreen replies.
+Forward references must work regardless of conversation processing order. Prior
+same-source imports can resolve without being selected again; unselected targets
+must not be auto-created. Unknown Slack domains remain external links. Missing,
+skipped, deleted and inaccessible targets retain safe source-only fallbacks, and
+another viewer must not gain access through a rendered mention. Code examples
+remain literal; source-controlled tags must not inject entities. Test live edits
+and deletion before deferred reconciliation: neither may be overwritten or revived.
+See the [rollout/recovery gates](../SLACK_ARCHIVE_IMPORT_RUNBOOK.md).
+
## Collapsed reply chains
Collapsed threads show the first three complete message groups. Consecutive
diff --git a/docs/AGENT_GUIDE/surfaces.md b/docs/AGENT_GUIDE/surfaces.md
index b413b3605f..3109cc848f 100644
--- a/docs/AGENT_GUIDE/surfaces.md
+++ b/docs/AGENT_GUIDE/surfaces.md
@@ -1667,6 +1667,55 @@ has Max still sees Max named as its active plan.
## Settings — `/app/settings/<section>`
+### Slack archive import
+
+In **Team → Connections**, **Import from Slack** appears only for team admins
+and owners with `enable-slack-archive-import` enabled. Production rollout stays
+off until explicitly enabled. Select an export ZIP, filter the grouped conversation
+picker, optionally show archived conversations, and use Space to toggle focused
+checkboxes. **Select all visible conversations** preserves selections hidden by
+filters; unchecking it or **Clear visible selection** clears only visible selections.
+Rows show stable Slack IDs
+so duplicate names remain distinguishable. Counts are advisory when available,
+otherwise “not yet counted.” Discovery moves keyboard focus to the filter.
+Slack file/canvas discussion folders (`FC:<file-id>:<title>`) are ignored, not
+imported as channels; their presence must not prevent channel discovery.
+
+Review email attribution and external membership implications, choose whether to
+include history, and confirm the immutable Slack workspace binding before
+**Import selected channels (N)**. At least one selection is required. File selection
+alone makes no server writes. Close/Escape or cancel before confirmation discards
+the archive and selection without creating a job; reopening requires choosing a
+file again. After confirmation the selected IDs/options are frozen. Closing and
+reopening then retains the upload session; keep Team Settings open until it finishes.
+The dialog restores focus to its opener, and polling should not blank Settings.
+
+Use **Job history** (including older pages) for server progress after a reload.
+It displays the persisted selected names/IDs/kinds, history option and selected
+failures/skips without the ZIP. Unselected channels are not skipped work. Reload
+recovers server tracking, **not interrupted local uploads**; finalize verified work
+with skips or cancel and explicitly confirm a new import/token.
+**Cancel import (keep partial results)** does not delete committed messages or
+channels; running work may finish. An interrupted upload offers **Recover job
+receipt**, then **Finalize with skips** or cancellation. Channel links appear only
+when the server confirms current participation and the viewer's channel list
+confirms access. Verify counters, errors and skip
+reasons at narrow widths, using synthetic exports and a test backend rather than
+importing customer data into hosted dev.
+
+For a release check, inspect the actual create request and then reload the server
+receipt: both must contain exactly the selected Slack IDs and history option.
+Select two rows with the same display name, filter both out, and confirm the
+selected total remains unchanged. Cancel before confirmation and verify there
+were no create, registration or PUT requests. An open job must reject tampered
+registration/sealing of an unselected ID; a client-only picker test is insufficient.
+Check desktop web and Tauri: opening Settings alone creates no ZIP worker, and
+signed PUTs work without dropping checksum or conditional headers. A 412 upload
+retry verifies the existing object instead of overwriting it. Keep an active job
+open through websocket disconnect/reconnect to verify polling remains usable.
+The [runbook](../SLACK_ARCHIVE_IMPORT_RUNBOOK.md) distinguishes fixture tests from
+live-backend release gates and documents staging retention and recovery.
+
### Email signatures
In Integrations, **Edit signature** beside an owned inbox expands its editor.
diff --git a/docs/RUNNING_LOCALLY.md b/docs/RUNNING_LOCALLY.md
index 3c5960b98a..7848659fba 100644
--- a/docs/RUNNING_LOCALLY.md
+++ b/docs/RUNNING_LOCALLY.md
@@ -178,6 +178,9 @@ alone does not prove kickstart ran. Do not disable SELinux, change host permissi
or delete the FusionAuth database. Diagnose other bind-mount denials separately;
private labels must not be applied to directories shared by several containers.
+For the synthetic Slack recovery harness and separate browser/native coverage,
+see [the Slack import runbook](SLACK_ARCHIVE_IMPORT_RUNBOOK.md).
+
### Seeding sample data (recommended)
A bare stack has no content to click through. The seed CLI creates a realistic
diff --git a/docs/SLACK_ARCHIVE_IMPORT_CONTRACT.md b/docs/SLACK_ARCHIVE_IMPORT_CONTRACT.md
new file mode 100644
index 0000000000..0a229beb2e
--- /dev/null
+++ b/docs/SLACK_ARCHIVE_IMPORT_CONTRACT.md
@@ -0,0 +1,596 @@
+# Slack archive import contract (v1)
+
+This is repository interface documentation, not an execution plan. The Rust
+source of truth is `crates/slack_integration/src/domain/{models,ports}.rs`.
+The browser, administrator API, PostgreSQL repository and independent worker share
+these contracts. Persisted selection, not browser state or queue input, owns job scope.
+
+## Boundaries and authorization
+
+- `ImportService` is the administrator-facing interface. Every operation takes
+ `EntityAccessReceipt<AdminTeamRole>`; derive team and actor from it. Request
+ JSON contains no team or requester override. Creation requires a real human
+ user, not a team-bot identity. Feature disablement rejects new creation/uploads
+ but leaves existing receipts readable and cancellable.
+- `ImportWorker` is a separate set of queue/maintenance use cases. Queue identity
+ is never authorization. Revalidate the persisted requesting admin before each
+ start/reclaim. Check target type, team, access and import provenance separately.
+- Policy and authorization orchestration live in domain services. Inbound
+ handlers forward receipts/commands; outbound adapters implement narrow ports.
+ Domain code contains no AWS, SQLx, HTTP framework or environment types.
+- A job belonging to another team is indistinguishable from a missing job.
+ An inaccessible reused target is skipped, with `target_unavailable` and no
+ target ID. Never add the importing admin to a DM to bypass access checks.
+- Ports use `rootcause::Report<ImportError>` internally to retain diagnostic
+ causes. Only the closed `ImportError`/`ImportWarning` codes cross the public
+ boundary. Never serialize raw provider errors, keys, tokens, emails or content.
+
+Default features expose models only. `ports` adds capability interfaces;
+`inbound`, `outbound`, `postgres`, `s3`, `sqs`, and `worker` enable their respective
+integration dependencies. Versions of every
+third-party dependency are inherited from the root workspace; none require a
+new third-party version or library. Both packages participate in Hakari and test
+environment setup. The default model dependency graph still includes the shared
+native `workspace-hack`, as in `reminders`.
+
+## Identities and encoding
+
+JSON struct fields use camelCase. Enum tags/status values use snake_case.
+Commands reject unknown fields where defined, rather than silently accepting
+keys/team overrides. Transport adapters must also enforce bounded body sizes;
+Serde parsing by itself does not enforce collection or string quotas.
+
+- `JobId`, `TeamId`, `CreateToken`, `WorkerId`, `LeaseToken` are distinct non-nil,
+ non-max UUID types. Generate new persisted IDs as UUIDv7 in application code.
+ A lease token is internal, fresh for every claim, and never in progress/queues.
+- `ConversationId` accepts uppercase alphanumeric Slack IDs beginning C/G/D;
+ `SlackUserId` accepts U/W (including `USLACKBOT`); `SourceId` accepts T workspace
+ identities. IDs are 2–64 ASCII bytes. An enterprise E ID alone is insufficient
+ to establish a single workspace. If workspace identity cannot be established,
+ require unknown-source confirmation.
+- A `KeySegment` is one nonempty segment, at most 255 UTF-8 bytes, not `.` or
+ `..`, with no slash, backslash, percent escape or control character. It permits
+ source folder spaces/Unicode, but is not an authorization proof. `ObjectKey`
+ validates a sequence of safe segments, at most 1024 bytes; canonical key layout
+ is additionally enforced by the storage adapter.
+- `SlackTimestamp` is an exact nonnegative Unix microsecond value. Parse decimal
+ **strings**, never floats. Accept 1–6 fractional digits, normalize to exactly
+ six, reject higher precision rather than rounding. Bound seconds to
+ `253402300799` (year 9999). Integer Slack metadata creation seconds are converted
+ to this representation with zero microseconds by the parser.
+- `Sha256Digest` is exactly 64 lowercase hexadecimal characters. It hashes the
+ actual uploaded UTF-8 bytes, not an ETag or an object metadata field.
+
+## Source binding and creation
+
+V1 supports one Slack workspace per Macro team. Source binding is durable and
+shared with onboarding. Known mismatches fail with `source_mismatch`. A missing
+workspace identity requires the explicit `confirmed_unknown` variant on every
+such create, including when prior unscoped onboarding mappings exist. Omission
+is not confirmation. Promoting a confirmed-unknown binding to a known identity
+must preserve the existing source namespace; never silently replace a known
+binding. Multi-workspace imports require a later scoped-ledger migration.
+
+`POST /slack/imports` accepts `CreateImport` (team supplied by the authenticated
+team context, not this body):
+
+```json
+{
+ "idempotencyToken": "019a0000-0000-7000-8000-000000000001",
+ "source": { "kind": "known", "sourceId": "T012ABC" },
+ "includeMessageHistory": true,
+ "conversations": [{
+ "slackChannelId": "C012ABC",
+ "kind": "public_channel",
+ "name": "general",
+ "folder": "general",
+ "memberIds": ["U012ABC", "U045DEF"],
+ "creatorId": "U012ABC",
+ "createdAt": "1700000000.000000",
+ "archived": false,
+ "messageCount": null
+ }]
+}
+```
+
+`source: {"kind":"confirmed_unknown"}` is the affirmative unknown-source option.
+Creation persists **all** selected metadata, not just counts. Conversations must
+be unique, nonempty and within the selection limit; folder resolution must be
+unambiguous. Complete source member lists are required even when history is off.
+Unknown advisory counts are null, not zero. Missing creator/time are explicit
+nulls. Missing creation time is resolved once from earliest source message when
+available, otherwise the persisted job creation time; record the corresponding
+warning. Never change a reused target's creation time.
+
+The client generates and retains the create token with its frozen in-memory
+confirmation snapshot before sending.
+Uniqueness is `(team, requesting human admin, token)`. An identical semantic
+payload returns the original job, even after it settles. A different payload
+with the same scoped token conflicts; it never replaces the original selection.
+The repository canonicalizes conversation/member ordering (including duplicate
+members) for semantic comparison; clients retry the exact snapshot rather than
+rebuilding it from mutable UI state. A lost response is retried with the same token,
+not a second create. The browser snapshot is not persisted across a page reload;
+job history can recover existing server jobs but cannot resume local uploads.
+
+### Popup confirmation and immutable scope
+
+Choosing a ZIP discovers metadata locally before any create, grant or upload.
+The existing dialog then presents grouped searchable checkboxes keyed by Slack ID,
+including the ID beside duplicate names, archived state and available advisory
+counts. Keyboard focus moves from the disabled file input to the filter. Bulk
+selection/clearing applies only to visible supported rows; filtering and hiding
+archived rows preserve hidden selections. Unsupported DMs are unavailable choices,
+not skipped selected work.
+
+**Import selected channels (N)** requires a nonempty selection and explicit source
+confirmation; history defaults on. This action freezes full selected metadata and
+options in `CreateImport.conversations`, allocates one token, and disables further
+review edits. Repeated clicks cannot create another request. Preparation, bounded
+parts and seals use only that snapshot, while shared users metadata remains required.
+Close/Escape or cancel before confirmation disposes scratch state without server
+writes. Closing after confirmation retains the active session while Settings stays
+mounted. Changing selection requires choosing an archive and confirming a new token.
+
+Only persisted conversation rows authorize registration, completion/sealing and
+worker claims. Unselected IDs have no manifest, outbox event or imported messages;
+links to them never reserve targets or implicitly import them (see reference policy).
+Shape-only imports retain the same selected rows with zero-part seals plus users.
+
+## Registration and immutable uploads
+
+Operations under `/slack/imports/{jobId}`:
+
+| Operation | Domain input | Result |
+| --- | --- | --- |
+| `POST uploads` | `RegisterUploads` | array of `UploadGrant` |
+| `POST uploads/complete` | `CompleteUploads` | `ImportProgress` |
+| `POST finalize` | path `JobCommand`, no body fields | `ImportProgress` |
+| `POST cancel` | path `JobCommand`, no body fields | `ImportProgress` |
+| `GET` | path `JobCommand` | `ImportProgress` |
+
+`GET /slack/imports` accepts an optional exclusive `before` job-ID cursor and
+returns `ImportPage`: up to 50 newest receipts, `nextCursor`, effective `limits`
+and `sourceBinding` (`unbound`, `confirmed_unknown`, or `known` with `sourceId`).
+Create returns the same `ImportProgress` shape as lifecycle/detail operations.
+
+Register at most 50 descriptors per call, with no duplicate upload identities:
+
+```json
+{
+ "descriptors": [{
+ "upload": {"kind":"users"},
+ "sha256": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
+ "byteLength": 123,
+ "recordCount": null
+ }, {
+ "upload": {"kind":"conversation_part","slackChannelId":"C012ABC","partIndex":0},
+ "sha256": "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
+ "byteLength": 456,
+ "recordCount": 2
+ }]
+}
+```
+
+The hashes above are illustrative. There is exactly one normalized users JSON
+payload per job, accepting source data from `users.json` and `org_users.json`.
+Its recordCount is null. Message parts contain one normalized JSON object per
+line, UTF-8 with LF terminators, counted including the trailing LF. They have
+positive byte/record counts. Empty history uses **no parts**, not a zero-byte
+part. Browser/parser normalization owns the Slack record/user schemas; it strips
+attachment payloads and sorts/deduplicates by exact source timestamp before
+staging. Registering never uploads ZIPs to the backend.
+
+The repository allocates keys and persists immutable descriptors before signing:
+
+```text
+slack-import/{team}/{job}/users.json
+slack-import/{team}/{job}/{slackChannelId}/{partIndex}.ndjson
+```
+
+Canonical UUID strings and canonical decimal indices are server generated.
+Neither requests nor queue bodies contain object keys. Every subsequent key
+lookup must go through the job-owned manifest; prefix matching is insufficient.
+Exact registration retries may renew grants while registration is open,
+including for already sealed descriptors. Descriptor changes always conflict;
+new indices conflict once that conversation is sealed. Job-wide aggregate byte
+accounting includes users and every unique descriptor, not duplicate retries.
+History-off jobs reject all conversation-part descriptors.
+
+Each `UploadGrant` contains `descriptor`, `url`, `requiredHeaders`, `expiresAt`.
+The storage adapter signs the exact checksum, length, derived content type and
+`If-None-Match: *` create-only condition. Content types are `application/json`
+(users) and `application/x-ndjson` (parts). The required-header map includes
+`Content-Type`, `x-amz-checksum-sha256` (base64 encoding of the digest bytes), and
+`If-None-Match`. Browser code sets the returned allowed headers; it does not set
+forbidden `Content-Length`, which follows from the exact Blob length. Provider
+adapter additions to the signed header map must be honored. Never log grants.
+URLs are requested immediately before PUT. A retry returning 412 means verify
+that existing object via completion, **not** obtain overwrite permission.
+
+Completion reads actual object checksum and length with storage checksum
+retrieval enabled. ETag and user-controlled object metadata are not evidence.
+Persist the verified version or entity-tag identity; worker reads pin that version
+or issue a conditional read against the verified ETag, then independently
+revalidate the streamed SHA-256 digest/length. An ETag is a read validator, never
+a substitute for SHA-256. `VerifiedUpload` cannot represent an unpinned read.
+Objects may never be overwritten, including after verification. IAM permission
+for S3 HeadObject is `s3:GetObject`, not a fictitious `s3:HeadObject` action.
+
+## Completion and seals
+
+```json
+{
+ "uploads": [{"kind":"conversation_part","slackChannelId":"C012ABC","partIndex":0}],
+ "seal": {
+ "slackChannelId": "C012ABC",
+ "partCount": 1,
+ "manifestSha256": "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc"
+ }
+}
+```
+
+`uploads` identifies at most 50 previously registered objects to verify. The
+optional seal covers the **entire** registered descriptor set for its selected
+conversation, not just the objects in this call. An empty uploads list permits
+a seal-only call. Null/absent seal verifies early parts without declaring the
+conversation ready. Identical verification/seal retries are idempotent; changed
+seals or descriptor sets conflict. Reject gaps, duplicates, extra parts and
+foreign-conversation descriptors. Once sealed, the expected set is immutable.
+
+### Canonical manifest digest
+
+Sort the conversation descriptors by ascending part index. For each, concatenate
+this ASCII line (decimal integers without leading zeros, lowercase hex digest):
+
+```text
+{partIndex}:{sha256}:{byteLength}:{recordCount}\n
+```
+
+Here `\n` denotes one LF byte. Hash the concatenated bytes using SHA-256.
+Do not hash JSON, registration order, object keys, URLs, or the users descriptor.
+Indices must equal `0..partCount`. Conversation identity is checked separately
+against the selected conversation. `ConversationSeal::from_descriptors` is the
+reference algorithm and test vector source for browser implementations.
+
+For **zero parts**, partCount is 0 and manifestSha256 is the empty byte hash:
+
+```text
+e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
+```
+
+A missing seal is not equivalent to a sealed empty set. Shape-only imports and
+actually empty history both seal zero parts. Both still need verified users for
+membership/author resolution. Readiness requires all three: verified users,
+valid immutable seal, and every required verified part. Users may be completed
+last, making multiple conversations ready in one transaction.
+
+## Lifecycle, recovery and progress
+
+Readiness and a unique `(job, conversation, event generation)` outbox event commit
+in one transaction. API completion/finalization never directly sends SQS. A
+maintenance publisher sends pending events then marks that generation published;
+a crash between those actions duplicates delivery but cannot lose work.
+
+`ImportEvent` contains exactly `jobId`, `slackChannelId`, `generation`. Unknown
+fields are rejected. No team, keys, users, lease tokens or authorization claims
+are trusted from a delivery. Reload full persisted state before execution.
+
+Conversation states:
+
+- `awaiting_uploads` → `queued` only after readiness; → `skipped` at finalize or
+ cancellation if never ready.
+- `queued` → `importing` under an atomic claim; → `skipped` on cancellation.
+- `importing` → `completed`, `failed` or `skipped` under the matching fence.
+ Expired leases can be reclaimed unless cancellation has stopped work.
+- Terminal conversations cannot be resurrected by completion/finalization or old
+ queue deliveries. Search state is tracked independently.
+
+Persist lease owner/token, monotonically increasing execution generation,
+expiry, heartbeat time, attempts, part/record checkpoint and committed counters.
+Execution generation is distinct from outbox publication generation. Every batch
+and final transition verifies the lease inside the transaction using database
+time. Heartbeat the database and extend SQS visibility every 60 seconds, on a
+loop independent of slow object/database processing. Losing either fencing
+ownership or reliable delivery coverage stops writes. A stale worker cannot
+commit. `ClaimOutcome`/`WorkerOutcome` distinguish an active duplicate from
+settled/obsolete work: **do not acknowledge** a duplicate of an active lease.
+
+Finalization is idempotent, closes registration, skips never-ready conversations
+and leaves queued/importing conversations alone. It does not manufacture seals
+or publish to SQS. Exact completion retries may observe already applied state
+after closure; no new verification/seal mutation can revive skipped work.
+
+Cancellation closes registration and atomically stops future claims/publication,
+skips unclaimed work, and enters `cancelling`. Running fenced workers may finish.
+If a running worker dies after cancellation, settle its expired lease without
+restarting; retain committed history and schedule its indexing. `cancelled` is
+terminal only when active leases, committed reference intents and required search
+publication settle. Cancellation is **not rollback**.
+Finalize, cancel and completion share one job lock/CAS. A publication already in
+flight can still arrive; claim rejects it. Repeated cancel/finalize of a terminal
+job returns its existing receipt, not a new lifecycle.
+
+Job states are `uploading` while registration remains open, then `processing`
+while queued/importing work, body reconciliation or required search publication
+remains. Once registration is closed and all work is settled: `completed` if no failures (including deliberate skips),
+`completed_with_errors` if failures coexist with completed conversations, or
+`failed` if failures exist without a completed conversation. Cancellation takes
+precedence while settling and becomes `cancelled`. Failed/cancelled imports may
+still have committed messages; counters must reflect them honestly.
+
+Maintenance also reconciles abandoned upload jobs, expired leases, stale search
+receipts and identifiable DLQ deliveries in bounded pages. Acknowledge exhausted
+work only after persisting failure and recomputing the job. A stale dead-letter
+generation must not fail newer work or a current active lease.
+
+`ImportProgress` exposes job ID/status/revision, creation/update/registration
+closure times, users verification, limits, immutable `source` confirmation and
+`includeMessageHistory`, and bounded per-selected-conversation progress.
+Conversation progress includes persisted source ID, `name`, `kind`, `archived`,
+target ID or null, seal
+partCount (null = not sealed), verified parts, committed counters, sanitized
+error/warnings and search state. No internal keys, grants, leases or raw errors.
+Counters are cumulative committed `processed`, `imported`, `duplicates`, `skipped`
+and `reactions`; processed equals imported + duplicates + skipped, not reactions.
+Replays/checkpoint retries cannot inflate them. Selected failures/skips remain in
+receipts; unselected channels are never counted as skipped. Names/kinds come from
+persisted source metadata, not inaccessible Macro targets or locally retained ZIPs.
+Polling, reconnect and job history render this same immutable server snapshot.
+The administrator service independently gates target disclosure through a read-only
+port using the **viewing** admin, not the original requester or import-write provenance.
+IDs are conservatively returned only for current active participants (even for
+team-visible channels), never for skipped work. Checks are deduplicated in batches
+of 500; a disclosure failure hides target IDs without failing an already committed
+mutation or hiding source receipts. No access grants or membership changes occur.
+
+`slack_import_updated` notifications carry only team/job/revision/status hints to
+the requesting administrator's gateway entity after access revalidation. Other
+admins poll. Notification failure never fails committed work. Websocket messages
+invalidate cached receipts; polling is authoritative. Terminal UI observation
+refreshes channel lists once; inaccessible target links are never rendered.
+
+## Historical persistence, ledger and search
+
+`ImportLedger` reserves a stable candidate UUIDv7 for `(team, Slack, foreign ID)`
+**before** channel creation; both onboarding and archive paths use it. Idempotent
+explicit-ID creation plus ledger completion recovers crashes between stages.
+Reservation states are pending/ready/conflict; ambiguous legacy mappings fail
+closed. Preserve the first mapping. Private/DM provenance is import-owned and
+must not leak through team-wide onboarding mapping lists.
+
+| Slack kind | Macro target |
+| --- | --- |
+| Public channel | Team, explicit members, `auto_join_team=false`; never Public |
+| Private channel | Private, `team_id=NULL`, provenance in reservation |
+| Two-person DM | Exactly two distinct mapped email identities; otherwise skip |
+| Group DM | Private, source-member display-name-based name |
+
+New non-DM channels include the requesting owner. Reused targets preserve names,
+creation time, settings, participant roles and joined/left times; silent inserts
+must not demote owners or reactivate leavers. DM lookup/create shares an atomic
+pair-locked primitive with live creation and requires exact cardinality. Reject
+cross-team Team targets, incompatible types, and private/DM targets without
+requester access or valid same-team import provenance. Reused non-DM private
+targets additionally need participant-management authority before adding members.
+
+Map ordinary authors/members using lowercase **raw email** to `macro|email`,
+without account/roster lookup or alias canonicalization. No-email/unknown/bot
+authors use `MACRO_SYSTEM_BOT_ID` plus Slack-name `imported_author`. No-email DM
+participants cause a skip; the admin is never injected as a third participant.
+
+`HistoricalSink::commit` is a worker composition-root coordinator invoking
+transaction-aware helpers exposed by each owning crate. In **one transaction**,
+fence the lease and persist messages, reactions, mentions, source mappings,
+checkpoint/counters and a dirty-search marker. A failure anywhere rolls back all
+of them. No SQL transaction type crosses a domain port; adapters never import
+another domain's outbound adapter. `(team, Slack conversation, exact timestamp)`
+is the long-lived first-commit-wins dedupe key. It is not global across teams,
+and reimports do not overwrite live edits, delete history or reattribute authors.
+
+Keep original timestamps, exact `thread_ts` parent identity and import metadata.
+Roots precede replies; resolve parents from durable mappings plus the bounded
+current batch. Preserve `orphaned_thread_ts` when missing/skipped, without a later
+repair promise. Convert reactions, dedupe mapped reactors, and use source message
+time when reaction time is absent. Ignore files/attachments and skip empty
+converted messages. No live-message notifications, activity rows, Kafka, bots,
+contacts, sharing changes or realtime sends. Historical activity timestamps use
+`GREATEST` so newer live activity never regresses.
+
+`SearchBackfillClient` submits only affected channel IDs (empty scope means no
+channels). A 202-style receipt is `submitted`, **not** indexed. Persist/poll the
+receipt and distinguish `not_needed`, `pending`, `submitted`, `completed`,
+`failed`. Dirty generation fencing prevents an older receipt clearing newer
+writes. Retry search independently, including committed history from failed or
+cancelled imports. Conversation completion means durable historical persistence
+and recoverable search work, not synchronous indexing. Imported author display
+names require an explicit searchable text mapping; the sender keyword alone is
+not sufficient. Existing search authorization still applies.
+
+Staging may expire after 14 days. Job/staging cleanup must not delete user-visible
+history, long-lived dedupe mappings, canonical reservations or source bindings.
+Team/channel deletion semantics belong to owning schema migrations.
+
+## Native links and deferred source references
+
+The pure `slack::mrkdwn::MrkdwnConverter` retains its existing `message`/`convert`
+interfaces. The additive `convert_with_references(source, italic)` returns
+`ConvertedText`: an initially safe `body`, explicit `user_mentions` (occurrences
+actively emitted outside code), and typed `ReferenceIntent` occurrences. It accepts
+at most 1 MiB source text, 1 MiB cumulative generated token/fallback bytes,
+256 references, and 256 emitted user mentions per body. Metadata expansion and
+occurrence budgets are checked as tokens are emitted; excess is an explicit
+`LimitExceeded`, not silent truncation. `italic` is used for
+Slack `me_message`. The importer uses `message_with_references` and carries this
+evidence to its atomic historical sink.
+
+Native representations match Lexical `INTERNAL_TRANSFORMERS`:
+
+```text
+<m-link>{"url":"https://example.com","text":"Example","title":""}</m-link>
+<m-user-mention>{"userId":"macro|[email protected]","email":"[email protected]"}</m-user-mention>
+<m-document-mention>{"documentId":"<channel UUID>","blockName":"channel","documentName":"general","blockParams":{},"collapsed":false}</m-document-mention>
+<m-document-mention>{"documentId":"<channel UUID>","blockName":"channel","documentName":"general","blockParams":{"channel_message_id":"<message UUID>","channel_thread_id":"<persisted root UUID>"},"collapsed":false}</m-document-mention>
+```
+
+`channel_thread_id` is optional; `channel_message_id` is the canonical message UUID,
+never a Slack timestamp. `documentId` is always the channel UUID. There is no
+`m-channel-mention`. The renderer may show the accessible entity name, not the
+original arbitrary link label. Shared typed serializers live in `mention_utils`;
+the existing markdown-document serializer's API and behavior remain unchanged.
+New native serialization JSON-escapes literal `<`/`>` delimiters, restoring labels
+exactly on JSON decoding (including quotes, backslashes, newlines and Unicode).
+This prevents closing/nested tag injection without HTML-encoding decoded labels.
+URL scheme policy (HTTP/HTTPS/mailto only) is validated separately from URL syntax
+using the URL parser; malformed authorities/ports and literal controls, whitespace,
+backslashes or tag delimiters are rejected. Original accepted URLs are retained,
+not rewritten to a hardcoded Macro origin.
+
+Safe Slack labeled/unlabeled angle links and protocol-prefixed bare HTTP(S)/mailto
+URLs outside code become `m-link`. Unlabeled mailto displays its address. Bare URL
+boundaries follow the frontend's protocol-mode autolink convention: terminal
+punctuation and unmatched closing parentheses are outside the URL; balanced path
+parentheses remain inside. Host-only/fuzzy links are not guessed. Entities decode
+once. Formatting is applied around generated nodes, never recursively through
+serialized JSON. Source-supplied Macro tags are escaped display text; recognized
+inline/fenced code remains literal. Never feed the entire output to the XML-only
+Rust parser to extract mention rows: it is not code-aware. Use `user_mentions`.
+Channel/group broadcasts and subteams are always inert text.
+
+Reference evidence and fallback policy:
+
+- Channel tokens carry a validated exact Slack channel ID. Fallback is the escaped
+ original `#label`, archive-provided channel name, or original Slack ID; no Macro
+ metadata is added to a fallback.
+- Supported permalinks are HTTPS `<workspace>.slack.com/archives/<C/G/D ID>/p<seconds><six microseconds>`
+ root links and reply links with optional `thread_ts=<seconds>.<six microseconds>`
+ and matching `cid`. Message identity always comes from the path, not `thread_ts`.
+ Credentials, non-default ports, fragments, extra path segments, duplicate/unknown
+ query keys, malformed IDs/timestamps and other Slack URL forms stay external links.
+- A parsed hostname is only source evidence to check, **not proof** of the bound
+ workspace. Resolution requires team + bound source + exact channel/message
+ identity and independently established matching source/domain evidence. Unknown
+ source confirmation supplies no domain evidence. Do not infer associations,
+ fetch URLs, call Slack, reserve IDs, create channels, or invent message mappings.
+- `ResolvedTarget` distinguishes authorized canonical channels from messages and
+ carries UUIDs (plus an optional persisted root). The resolver must require
+ requester **read** access, separate from import-write provenance. Prior same-source
+ imports may resolve even if not selected. Per-viewer runtime access checks remain.
+- Missing, unauthorized, skipped, deleted, incompatible or source-mismatched targets
+ retain their original safe fallback: external `m-link` for a permalink, source-only
+ display text for a channel. A message can never downgrade to a channel mention.
+
+### Read-only resolver boundary
+
+`references::resolve::resolve_batch` returns typed `Resolved`, `Pending`, or
+`Fallback` outcomes, in record/occurrence order. Pending/fallback contain no target
+IDs or labels. It deduplicates exact source identities within a batch, caps each
+record at 256 reference occurrences, caps records and serialized template bytes at
+the configured database ceilings (never above 500/4 MiB), and chunks lookup calls
+at that same record ceiling. No per-token queries or archive-wide reference map.
+
+`ImportTargetReader` reads canonical reservations and unambiguous explicitly
+team-scoped legacy mappings without reserving, locking, or deriving scope from a
+user's current team. It validates binding, existence, type and team compatibility;
+name-only legacy mappings are not evidence. Prior compatible imports and archived
+source channels remain eligible. Pending reservations expose no candidate UUID.
+`SourceMessageReader` reads exact integer-microsecond mappings; `HistoricalMessageReader`
+checks live message/root state and actual channel/parent ownership. A persisted
+orphan-imported reply links to its own root, not a guessed Slack parent.
+
+The worker's `WorkerReferenceLookup::context` loads the requester and source from
+the team-owned job. V1 has **no persisted domain-association evidence**, so its
+`domains` is empty and all Slack permalinks remain external, even with a known T ID.
+The domain resolver supports independently established exact workspace/hostname
+pairs, but there is no public/archive input or implicit alias-binding flow for
+those pairs. URL userinfo (including empty userinfo), contradictory `cid`, and a
+`thread_ts` later than the path message are not accepted source references.
+
+Disclosure requires current active participation for Private/DM, or current
+membership in the owning team for Team channels. Import provenance and admin role
+alone do not grant read access. Missing mappings in selected nonterminal work stay
+pending; deleted/inconsistent mapped messages do not become speculative targets.
+The sink persists user mention rows only from converter-emitted `user_mentions`,
+never by parsing tags in the resulting body. Code-contained tags remain inert.
+
+The resolver is a read-only capability for deferred reconciliation; persistence,
+settlement-time retries, guarded body updates and search publication remain the
+separate reconciliation boundary described below.
+
+Each intent stores its exact fallback and UTF-8 byte range in the immutable initial
+body. `ConvertedText::render` reconstructs by occurrence with typed targets; it
+validates ordered, non-overlapping ranges/fallback equality and never searches or
+regex-replaces serialized bodies. Duplicate, self, forward and cyclic occurrences
+are independent of import order. Persist the template/intents atomically with the
+imported body/mapping/checkpoint. Subsequent reconciliation must fence body updates
+against live edits/deletion and atomically complete intents with search-dirty/outbox
+work. Close unresolved intents to fallback when the job's opportunity ends, including
+cancellation, without restarting imports or adding members/mention effects. This is
+within-job reconciliation, not cross-job edit synchronization.
+
+The sink records one job-owned `slack_import_message_reference` template per newly
+won source mapping, plus `slack_import_job` and `body_version` in message import
+metadata. Duplicate-import losers never write intents or replace the winning body.
+No speculative UUID is rendered: all body references, including same-batch links,
+wait until canonical arbitration and selected-conversation settlement.
+
+Maintenance uses a transaction-held job row lock (`SKIP LOCKED`) as its fence,
+not a reclaimed conversation lease. It processes at most 50 templates per tick,
+one bounded template (at most 4 MiB) per transaction. A process crash rolls back
+body, completion checkpoint and search writes together; a competing process cannot
+patch the same job concurrently. No S3 object is needed to finish. The read-only
+resolver rechecks source proof and current disclosure access immediately before a
+message-owned compare-and-set. Body, owning job/version, unchanged timestamps,
+absence of edits and deletion must still match. Live edits/deletions are skipped;
+a successful patch changes only content, not author, timestamps, counters,
+reactions, thread structure, membership or live-message effects.
+
+Missing/pending targets at this final opportunity retain source-only fallbacks.
+Unsupported template versions close without a rewrite. Expired/abandoned/failed
+and cancelled partial imports follow the same rule after their conversations
+settle, without reclaiming cancelled import work. Completion clears bulky template
+data and retains a small checkpoint until job cleanup. Later exports or restored
+access never reopen it. Job deletion cascades only into these temporary intents,
+never into messages or long-lived dedupe mappings.
+
+Changed bodies increment the conversation's durable search generation in the same
+transaction. Scoped search publication waits for the job's pending templates to
+close, then uses the existing receipt/retry path. Job completion waits for required
+publication (not eventual OpenSearch refresh); terminal jobs never oscillate. These
+are internal settlement rules, with no new public progress enum.
+
+`tests/fixtures/native-message-links.json` in `slack_integration` is shared with
+`packages/lexical-core/tests/slack-import-links.test.ts`. Rust checks converter/shared
+serializer output with the real `mention_utils` parser; frontend tests import using
+real `INTERNAL_TRANSFORMERS` and round-trip Lexical state, including hostile labels
+and code. Ordinary mentions/mailto also round-trip native markdown. Lexical's own
+markdown export normalizes URL parentheses and is not the import serializer.
+
+## Bounds and browser obligations
+
+`ImportLimits::default()` defines these initial configurable ceilings:
+
+| Resource | Default |
+| --- | --- |
+| NDJSON part | 16 MiB and 20,000 records |
+| NDJSON record, including LF | 1 MiB |
+| Users/root metadata/individual day JSON | 32 MiB |
+| Total selected temporary upload bytes | 2 GiB |
+| ZIP entries | 100,000 |
+| Selected conversations | 2,000 |
+| Registration/completion call | 50 descriptors/identities |
+| Historical database batch | 500 messages and 4 MiB |
+
+Use byte limits as well as counts, including UTF-8 multibyte text. Reject explicit
+limit violations; do not silently truncate imports. Server configuration must be
+validated at startup. Route body/metadata text bounds complement these limits.
+
+The browser shows the picker before uploads, scans the ZIP at most twice total
+(first metadata/index, second all selected folders), and uses bounded IndexedDB
+sorting/deduplication rather than retaining whole conversations. History-off
+skips the history pass. Apply backpressure and clean scratch data on every exit.
+The worker streams bytes and rechecks total bytes, line length, record count,
+digest and monotonic timestamp ordering across part boundaries. No server ZIP
+extraction, attachment downloads, multipart upload, live Slack bot/contact sync,
+blocks converter, multi-workspace binding, rollback, SDK publishing or automatic
+orphan repair is part of this contract.
diff --git a/docs/SLACK_ARCHIVE_IMPORT_RUNBOOK.md b/docs/SLACK_ARCHIVE_IMPORT_RUNBOOK.md
new file mode 100644
index 0000000000..ef8bcfc548
--- /dev/null
+++ b/docs/SLACK_ARCHIVE_IMPORT_RUNBOOK.md
@@ -0,0 +1,319 @@
+# Slack archive import: rollout and recovery
+
+This is an operator checklist, **not production rollout approval**. Use synthetic
+archives and isolated local resources for verification. Do not deploy, import
+customer archives, reset databases, purge queues, or delete staging/history to
+troubleshoot without separate approval.
+
+See [the contract](SLACK_ARCHIVE_IMPORT_CONTRACT.md),
+[local setup](RUNNING_LOCALLY.md), [database workflows](DATABASE_DEVELOPMENT.md),
+and [the Settings interaction guide](AGENT_GUIDE/surfaces.md#slack-archive-import).
+
+## Deployment order and configuration
+
+1. Apply additive migrations, including source bindings, canonical target
+ reservations, manifests/outbox/leases, and deferred message references.
+ Do not remove older schema or mappings when rolling services back.
+2. Deploy `archive`-aware ledger readers and canonical-reservation **onboarding**
+ writers before archive admission. Both paths must reserve the same target;
+ updating only the archive worker leaves a duplicate-channel race.
+3. Add the `imported_author` OpenSearch mapping **before writers/backfills**.
+ The channel index uses `dynamic: false`; `_source` alone is not searchable.
+ Inspect the intended endpoint/alias first. From
+ `infra/stacks/opensearch/helpers`, run
+ `DRY_RUN=true bun scripts/add_imported_author.ts`. With separate deployment
+ approval, `DRY_RUN=false` applies it; `INDEX` optionally selects a physical
+ index. This adds a `content_text` text field copied into `content`; it does not
+ recreate an index or swap aliases. Backfill affected channels afterward.
+4. Provision `slack-import-queue`/DLQ, the independent Fargate worker, staging
+ lifecycle/CORS and scoped IAM. The main queue's redrive count is 5; configured
+ queue visibility is 900 seconds, while the consumer requests 180 seconds and
+ extends visibility every 60 seconds. Do not derive lease health from the queue
+ default. ECS gives 120 seconds to stop; the driver drains for up to 110 seconds.
+5. Register/verify the exact Doppler names below in the intended environment.
+ Keep DSS and worker admission disabled while deploying search and HTTP changes.
+6. Complete the release gates below against the local stack, then an explicitly
+ approved controlled dev team. Enable backend/worker admission before the
+ frontend flag. The current backend flag is service-wide, not a team allowlist:
+ restrict access operationally or use an isolated environment for the canary.
+
+| Setting | Owner / requirement |
+| --- | --- |
+| `SLACK_IMPORT_ENABLED` | DSS admits creates/registration/completion; worker admits new claims/publication. Default false. Set in both configurations deliberately. |
+| `SLACK_IMPORT_CONCURRENCY` | Worker, default 1; only 1 or 2 accepted. Start at 1. |
+| `DATABASE_URL` | Worker MacroDB; role needs the owning-domain historical transaction tables. |
+| `INTERNAL_API_KEY` | Worker and search processing must agree; never log or include it in tickets. |
+| `UPLOAD_STAGING_BUCKET` | DSS/worker same bucket. Worker infrastructure supplies this value. |
+| `OVERRIDE_SLACK_IMPORT_QUEUE`, `OVERRIDE_SLACK_IMPORT_DLQ` | Typed queue names/URLs; worker infrastructure supplies them. Resolve both at startup. |
+| `OVERRIDE_SEARCH_PROCESSING_SERVICE_URL` | Worker internal scoped-backfill endpoint; supplied by infrastructure. |
+| `enable-slack-archive-import` | Frontend feature flag. Local override: `VITE_ENABLE_SLACK_ARCHIVE_IMPORT=true`. It grants no server authority. |
+
+The worker stack uses Doppler project `slack-import-worker` and selects
+`DATABASE_URL`, `INTERNAL_API_KEY`, `SLACK_IMPORT_ENABLED`, and
+`SLACK_IMPORT_CONCURRENCY` from its synchronized secret. Verify registration and
+restart tasks when changing values. `APP_SECRETS_JSON` can override ordinary
+configuration: do not introduce contradictory values there. No secrets or Doppler
+changes are supplied by this runbook. Worker provisioning is separately gated by
+Pulumi's `deploy_slack_import_worker` (default `false`); enable it only after the
+dedicated secret exists, following the [worker rollout guide](../infra/stacks/cloud-storage-service/SLACK_IMPORT_ROLLOUT.md).
+Keep that deployment gate enabled once provisioned; use `SLACK_IMPORT_ENABLED`
+to pause intake without deleting the worker.
+
+DSS may PutObject/GetObject only under `slack-import/*`. HeadObject uses
+**GetObject**, not an invented `s3:HeadObject` permission. The worker needs
+GetObject there, SendMessage on main, and GetQueueUrl/ReceiveMessage/DeleteMessage/
+ChangeMessageVisibility on main and DLQ. Check pinned version reads if enabling
+bucket versioning; version-specific permissions must be reviewed separately.
+
+Test CORS from the real web and Tauri origins. PUT must preserve signed
+`content-type`, `x-amz-checksum-sha256`, `if-none-match: *`, and exact byte length.
+Browser code does not set the forbidden Content-Length header itself. A 412 means
+verify the existing object through completion, **never remove the condition and
+retry an overwrite**. Do not treat ETag as a SHA-256 checksum.
+
+## Source and data semantics
+
+- V1 binds one Slack workspace to one Macro team. Known mismatches fail; unknown
+ exports need explicit admin confirmation. Never clear a binding to bypass a
+ mismatch. Existing onboarding mappings belong to that binding.
+- Unknown-source confirmation is **not** Slack domain evidence. Unproven Slack
+ permalinks remain external links. There is no domain-association UI in v1.
+- Selection is the immutable create request's full `conversations` set. Names
+ are not identities. Same-token retries must preserve metadata/options; widening
+ selection needs another explicit confirmation and token. Linked unselected
+ conversations are never automatically imported.
+- Public Slack channels become **Team**, with `auto_join_team=false` and explicit
+ source members. Private/group DMs become Private. A DM has exactly two distinct
+ mapped email identities; no importing-admin third member. Unauthorized reused
+ private/DM targets are skipped without exposing their IDs.
+- Emails are lowercased as exported, without roster/alias matching. Unknown users
+ use the system bot with an imported Slack author label. There is no contact
+ synchronization. Files, attachments and attachment-only messages are ignored.
+- Source timestamp identity wins on first commit within the bound team. Repeat
+ and overlapping/date-split exports dedupe; they do not synchronize later edits
+ or deletes or repair earlier orphan threads. Existing names, roles, leavers,
+ creation times and newer live activity must survive imports.
+- Historical writes must not generate live notifications, bot invocations,
+ contact invitations or per-message activity/realtime fan-out.
+
+## Monitor before enabling admission
+
+The current worker emits structured `operation`, `outcome`, and sanitized `code`
+logs. Operation names include `receive`, `delivery`, `delete`, `outbox`, `reconcile`,
+`search`, `malformed`, and `shutdown`. These are logs, **not a claim that custom
+metrics or every alarm below already exist**. Provision dashboards/alarms and
+validate routing before rollout. Avoid source text, emails, object keys, signed
+URLs and receipt handles as labels or log fields.
+
+| Signal | Initial operational action |
+| --- | --- |
+| Main queue age/depth/in-flight count | Warn on rising age for 15 minutes while enabled; compare throughput and worker health before scaling. |
+| DLQ visible messages or `malformed` events | Alert on any; reconcile identity-bearing failures before acknowledging. |
+| ECS restart/OOM/failed deployment, CPU/RSS | Alert; keep concurrency bounded and inspect batch/record limits. |
+| Expired `importing` leases | Warn when still expired after two maintenance cycles; sustained expiry over 5 minutes warrants investigation. |
+| Oldest due unpublished import outbox row | Warn beyond 5 minutes while enabled; publication can safely repeat. |
+| Pending reference count/age after conversations settle | Warn beyond 5 minutes; do not force job completion to clear it. |
+| Dirty/submitted/failed search generations and receipt age | Warn on no progress for 15 minutes; distinguish publication from indexing/refresh lag. |
+| Conversation imported/duplicate/skipped/failed totals | Track reason codes and changes from canary expectations, not just a green job count. |
+| Staging deadline / abandoned upload count | Inspect jobs approaching expiry; request a new confirmed import if bytes are gone. |
+
+A successful job requires reference reconciliation and required search
+**publication**, not merely a 202 acceptance receipt. OpenSearch consumption and
+refresh are eventually consistent and need a separate authorized search probe.
+Use an imported author name absent from message text; verify the matching message
+is found by a participant and not by a no-access viewer.
+
+## Stale-job inspection (read-only)
+
+Start with the admin receipt/detail API or Settings job history; polling remains
+available if websocket invalidation is missed. For an authorized operator, bind
+`job_id` in psql to the particular job UUID and inspect without archive content:
+
+```sql
+BEGIN READ ONLY;
+SELECT id, team_id, status, revision, include_message_history,
+ created_at, updated_at, registration_closed_at, cancel_requested_at,
+ staging_expires_at
+FROM slack_import_job WHERE id = :'job_id'::uuid;
+
+SELECT slack_channel_id, status, part_count, processed, imported, duplicates,
+ skipped, reactions, checkpoint_part, checkpoint_record, attempts,
+ lease_generation, heartbeat_at, lease_expires_at,
+ search_state, search_dirty_generation, search_submitted_generation,
+ search_updated_at, last_error
+FROM slack_import_conversation WHERE job_id = :'job_id'::uuid;
+
+SELECT slack_channel_id, part_index, byte_length, verified_at
+FROM slack_import_upload WHERE job_id = :'job_id'::uuid;
+
+SELECT kind, generation, available_at, published_at
+FROM slack_import_outbox WHERE job_id = :'job_id'::uuid;
+
+SELECT count(*) AS pending_references
+FROM slack_import_message_reference
+WHERE job_id = :'job_id'::uuid AND completed_at IS NULL;
+COMMIT;
+```
+
+Compare users verification, contiguous part descriptors and the seal; completing
+one part is not readiness. A zero-part seal still needs verified users. Check
+current requester admin status, target authorization, S3 identity availability,
+queue delivery, and search receipt separately. Do not manually change fences,
+checkpoints, generations, source bindings or statuses. Stale owners must remain
+unable to commit. Keep any investigation limited to the authorized team.
+
+## Recovery, cancellation and DLQ precautions
+
+1. Disable **DSS admission** first to stop new uploads if there is a safety issue;
+ preserve progress/finalize/cancel access. Hiding the frontend alone is not a
+ server-side stop. Disable worker new work too if processing is unsafe.
+2. Leave recovery available: a disabled worker still reconciles leases,
+ cancellation, references/search and DLQ messages. Do not scale every worker
+ to zero and expect this maintenance to happen. Already running work may finish.
+3. Fix the cause before replay: check current authorization, source binding,
+ verified bytes, staging deadline, lease generation and whether the event is
+ already obsolete/terminal. An old DLQ event must not fail a newer active lease.
+4. Let the DLQ consumer persist terminal failure before deleting its delivery.
+ Malformed envelopes have no trustworthy job identity: retain sanitized failure
+ evidence, not a guessed job/key from the raw body.
+5. Do not bulk-redrive or fabricate generations. Terminal failure is not reopened
+ by replaying an old SQS message. A new explicitly confirmed same-source job may
+ safely dedupe committed history; inaccessible/expired inputs need correction
+ first. There is no operator "force resume" endpoint.
+6. For a lost publication acknowledgement, let the outbox resend. Duplicate
+ delivery is expected. Do not remove message mappings, reactions or reservations
+ to make a replay appear new.
+
+Cancellation is **not rollback**. It closes registration, prevents future claims
+and skips unclaimed work. `cancelling` waits for active leases and retained-history
+reference/search work. A cancelled worker that dies is settled after lease expiry
+without restarting that conversation. Committed messages remain, are indexed,
+and counters remain visible. Finalize skips never-ready conversations but must
+not discard queued/importing work. Finalize/cancel/complete share the job lock;
+no request may resurrect terminal results.
+
+Staging bytes under `slack-import/` expire after **14 days**. This is not deletion
+of imported channels, messages, idempotency maps, source bindings or reservations.
+Lifecycle expiry is asynchronous, not an upload-resume promise. The worker stops
+starting/recovering imports within one day of the persisted staging deadline, so
+operators must not promise all 14 days for processing. SQL receipts and
+manifests have a separate retention/cleanup policy; never cascade from staging
+cleanup into user-visible history. Reloading Settings recovers receipts, not a
+lost browser ZIP or unfinished local upload session.
+
+## Verification harnesses
+
+Run from the workspace root with a local PostgreSQL role capable of creating
+isolated SQLx test databases. Confirm DATABASE_URL is local before proceeding.
+Do not set SQLX_OFFLINE for tests. Nix is the documented toolchain; an approved
+native toolchain works too.
+
+```sh
+export DATABASE_URL="$(just --evaluate DATABASE_URL)"
+env -u SQLX_OFFLINE cargo test -p slack_import_worker --test archive_import -- --ignored --nocapture
+```
+
+Preload `localstack/localstack:4`. The ignored harness refuses to pull an image,
+starts its own container on an ephemeral loopback port, uses dummy credentials,
+and removes only that container on normal exit or panic unwind (a killed test
+process may need operator cleanup of its own container). SQLx creates a separate migrated test
+DB. It uses real conditional PUTs, storage verification, service completion/seal,
+PostgreSQL writes, SQS and the production queue driver. It tests shape→history→
+repeat ID stability, selection tampering, foreign-team lookup, duplicate
+publication, canonical channel references/threads, unproven-domain fallback,
+the search-completion barrier, actual SQS receive exhaustion/redrive, and durable
+DLQ settlement with worker admission disabled. The redrive scenario uses fresh
+queues so abandoned HTTP long polls from the prior driver cannot reserve its
+messages; its test-only redrive count is 2 rather than production's 5. Its notifier/disclosure and search receipt gate
+are test doubles. Three additional tests launch the actual `slack_import_worker`
+binary and kill it after lease acquisition, committed history, and SQS publication
+before outbox acknowledgement. Test-only PostgreSQL triggers/advisory locks make
+those boundaries deterministic; no production fault switches are added. Restart
+assertions check canonical UUID survival, exactly-once counts and the search
+completion barrier. They advance only isolated test lease/outbox deadlines and
+replay the original envelopes rather than waiting the full visibility timeout.
+This is real process-death coverage, but search publication still uses a controlled
+receipt gate, **not OpenSearch**. HTTP auth, navigation and AWS CORS require the
+separate live probes below.
+
+For a **new disposable** browser instance, use the local launcher and seed an admin
+team against that named instance. For an existing instance, follow
+[non-destructive recovery](RUNNING_LOCALLY.md#recover-an-existing-instance-without-resetting-data)
+instead: neither `run_local` nor `stack up` is a data-preserving restart. Enable
+DSS/worker admission and the frontend override, and use the local E2E backend
+origin/JWT setup without invoking a stack-resetting harness. No-Doppler default
+Macro API signing keys are placeholders: supply a local-only generated RSA key
+pair to the services and token generator, or use local FusionAuth login. Never use
+hosted credentials. Warm the browser WASM builds before Playwright's 60-second
+server-start deadline, and do not run Docker teardown tests concurrently with
+Chromium (network-interface changes can abort module requests). The checked-in Playwright testDir is
+`tests/e2e`, not `tests`:
+
+```sh
+# From apps/web, with LOCAL_E2E_BASE_URL, LOCAL_E2E_BACKEND_ORIGIN and
+# LOCAL_JWT for the isolated stack (never a hosted account):
+SLACK_IMPORT_LIVE_E2E=true bun run local:e2e tests/e2e/slack-import.spec.ts
+```
+
+Without `SLACK_IMPORT_LIVE_E2E=true`, only the existing deterministic picker
+fixture runs; the live HTTP tests are skipped. Do not report those skips as a
+backend pass. Live tests cover cancel-before-confirmation, lazy worker creation,
+duplicate display names and hidden selections, actual create/PUT/registration,
+persisted/reloaded options, and open-job unselected registration/seal rejection.
+They leave synthetic imported history and cancel their extra tamper job; they do
+not reset the database. The worker unit suites cover bounded slices, exactly one
+discovery/history phase, backpressure, part limits and quota cleanup. Bounded
+buffer assertions are not a measured browser/native peak-memory certification.
+
+## Required browser/backend release gates
+
+Tauri is **not** a gate for T27 browser/backend verification. Native WebView,
+platform-specific worker behavior and native-origin CORS remain separate follow-up
+coverage below; browser passes must never be presented as native passes.
+
+- [ ] Standard, corporate and Enterprise exports; shape-only; repeat/date-split;
+ onboarding-before/after archive; raw email attribution, unknown/bot authors,
+ threads/reactions, new-user membership; no live effects.
+- [ ] Actual Lexical rendering, hover and click: external/mailto, mapped channels,
+ canonical root/reply UUID targets, forward/self/cyclic links with reversed queue
+ order and prior same-source imports. Inspect destination IDs, not text snapshots.
+- [ ] Private/no-access viewers, unproven/mismatched domains, missing/skipped/deleted
+ targets, closing-tag injection and literal inline/fenced examples retain safe
+ fallbacks and create no unintended mention rows.
+- [ ] Reconciliation failure/rollback/retry, cancellation, edits/deletion before
+ patch and overlapping-job ID races; body patch/search dirty work atomic;
+ successful completion waits for reconciliation and search publication.
+- [x] Kill the actual worker process after lease acquisition, batch commit and
+ queue send-before-ack. Restart from durable state; assert counts and UUIDs.
+ Exhaust real SQS receives into DLQ and verify terminal reconciliation.
+ Verified by the four opt-in `archive_import` tests on 2026-10-01, with local
+ PostgreSQL/LocalStack and native Cargo; search is a receipt-gate double.
+- [ ] Concurrent imports of the same channel/DM; unauthorized reused DM;
+ foreign-team jobs/keys; immutable PUT; finalize/cancel races.
+- [ ] Large synthetic ZIP on desktop: two passes, bounded parts and measured
+ memory, low disk quota failure/cleanup, no eager worker construction,
+ signed-header/CORS behavior. Unit buffer bounds are not a peak-RSS measurement.
+- [ ] Real Settings subset test with live backend, polling/reconnect/reload;
+ selected failures remain visible and unselected IDs produce no work.
+- [ ] Real authorized author-only search and denied-viewer search after publication
+ and index refresh; reindex after reconciled body changes.
+- [ ] Package tests: `slack_integration --all-features`, `slack_import_worker`,
+ `messages`, `channels`, `import`, `document_storage_service` (each via cargo test).
+- [ ] `just check full`, `just rust-check`, `cargo run -p xtask -- deps --check`,
+ web `bun run gen-api cloud-storage --check`, SDK `just coverage && just check`.
+- [ ] Confirm deployed IAM/CORS/lifecycle, Doppler values, alarms and additive
+ mapping. Obtain explicit approval before any production or customer-data run.
+
+### Separate native/platform follow-ups
+
+- [ ] Repeat selection/upload/navigation in the native WebView, including lazy
+ worker startup, quota behavior and signed PUT headers/CORS from native origins.
+- [ ] Verify Linux GTK/WebKit through `apps/web/tests/native/README.md` using the
+ installed native toolchain when available. Nix is not required on the approved
+ Fedora verification host.
+- [ ] Verify iOS/WKWebView and other supported platforms on their actual runtimes.
+ Chromium desktop results do not establish any of these passes.
+
+Rollback admission, not data: disable new imports, retain queues/outbox/mappings
+and imported history, and continue safe reconciliation. Never use a destructive
+reset as a recovery procedure.
diff --git a/infra/stacks/bulk-upload/upload-bucket.test.ts b/infra/stacks/bulk-upload/upload-bucket.test.ts
new file mode 100644
index 0000000000..d668ad93c0
--- /dev/null
+++ b/infra/stacks/bulk-upload/upload-bucket.test.ts
@@ -0,0 +1,136 @@
+import { beforeAll, describe, expect, mock, test } from 'bun:test';
+import * as pulumi from '@pulumi/pulumi';
+
+const resources: pulumi.runtime.MockResourceArgs[] = [];
+const bucketArn = 'arn:aws:s3:::bulk-upload-staging-dev';
+const roleArn = 'arn:aws:iam::123456789012:role/cloud-storage-service-role-dev';
+
+beforeAll(async () => {
+ pulumi.runtime.setAllConfig({ 'aws:region': 'us-east-1' });
+ pulumi.runtime.setMocks(
+ {
+ newResource(args) {
+ resources.push(args);
+ return {
+ id: args.inputs.bucket ?? args.name,
+ state: { ...args.inputs, arn: bucketArn },
+ };
+ },
+ call(args) {
+ return { ...args.inputs, secretString: 'test-only' };
+ },
+ },
+ 'slack-import-tests',
+ 'dev',
+ false
+ );
+ // The shared barrel has a pre-existing ESM cycle; isolate stack constants
+ // while exercising the real bucket helper and AWS resource constructors.
+ mock.module('../../packages/shared', () => ({
+ stack: 'dev',
+ CLOUD_TRAIL_SNS_TOPIC_ARN: 'arn:aws:sns:us-east-1:123456789012:alerts',
+ }));
+ const { BulkUploadBucket } = await import('./upload-bucket');
+ new BulkUploadBucket('bulk-upload-bucket', {
+ cloudStorageServiceRoleArn: roleArn,
+ tags: {},
+ });
+ await pulumi.runtime.waitForRPCs();
+});
+
+function resource(type: string): pulumi.runtime.MockResourceArgs {
+ const matches = resources.filter((item) => item.type === type);
+ expect(matches).toHaveLength(1);
+ return matches[0];
+}
+
+describe('Slack import staging protections', () => {
+ test('one helper-owned lifecycle rule expires only the Slack prefix after 14 days', () => {
+ const lifecycle = resource(
+ 'aws:s3/bucketLifecycleConfigurationV2:BucketLifecycleConfigurationV2'
+ );
+ expect(lifecycle.name).toBe('bulk-upload-bucket-lifecycle');
+ expect(lifecycle.inputs.rules).toEqual([
+ {
+ id: 'slack-import-cleanup',
+ status: 'Enabled',
+ filter: { prefix: 'slack-import/' },
+ expiration: { days: 14 },
+ },
+ ]);
+ });
+
+ test('one existing CORS owner permits conditional checksum PUTs and legacy requests', () => {
+ const cors = resource(
+ 'aws:s3/bucketCorsConfigurationV2:BucketCorsConfigurationV2'
+ );
+ expect(cors.name).toBe('bulk-upload-bucket-cors');
+ expect(cors.inputs.corsRules).toHaveLength(1);
+ const rule = cors.inputs.corsRules[0];
+ // Wildcard covers Content-Type, If-None-Match and x-amz-checksum-sha256.
+ expect(rule.allowedHeaders).toEqual(['*']);
+ expect(rule.allowedMethods).toEqual([
+ 'GET',
+ 'PUT',
+ 'POST',
+ 'DELETE',
+ 'HEAD',
+ ]);
+ expect(rule.allowedOrigins).toContain('https://macro.com');
+ expect(rule.allowedOrigins).toContain('http://localhost:3000');
+ expect(rule.allowedOrigins).not.toContain('*');
+ expect(rule.exposeHeaders).toContain('ETag');
+ });
+
+ test('conditional write deny is prefix-only and legacy bucket access is unchanged', () => {
+ const policy = JSON.parse(
+ resource('aws:s3/bucketPolicy:BucketPolicy').inputs.policy
+ );
+ expect(policy).toEqual({
+ Version: '2012-10-17',
+ Statement: [
+ {
+ Effect: 'Allow',
+ Principal: { AWS: roleArn },
+ Action: [
+ 's3:GetObject',
+ 's3:PutObject',
+ 's3:GetObjectAttributes',
+ 's3:ListBucket',
+ ],
+ Resource: [bucketArn, `${bucketArn}/*`],
+ },
+ {
+ Sid: 'RequireImmutableSlackImports',
+ Effect: 'Deny',
+ Principal: '*',
+ Action: ['s3:PutObject'],
+ Resource: [`${bucketArn}/slack-import/*`],
+ Condition: { StringNotEquals: { 's3:if-none-match': '*' } },
+ },
+ ],
+ });
+ expect(JSON.stringify(policy)).not.toContain('s3:HeadObject');
+ });
+
+ test('existing EventBridge notification remains the sole notification resource', () => {
+ const notification = resource(
+ 'aws:s3/bucketNotification:BucketNotification'
+ );
+ expect(notification.name).toBe('bulk-upload-staging-dev-notification');
+ expect(notification.inputs).toEqual({
+ bucket: 'bulk-upload-staging-dev',
+ eventbridge: true,
+ });
+ expect(resource('aws:s3/bucketObjectv2:BucketObjectv2').inputs.key).toBe(
+ 'extract/'
+ );
+ const access = resource(
+ 'aws:s3/bucketPublicAccessBlock:BucketPublicAccessBlock'
+ ).inputs;
+ expect(access.blockPublicAcls).toBe(true);
+ expect(access.blockPublicPolicy).toBe(true);
+ expect(access.ignorePublicAcls).toBe(true);
+ expect(access.restrictPublicBuckets).toBe(true);
+ });
+});
diff --git a/infra/stacks/bulk-upload/upload-bucket.ts b/infra/stacks/bulk-upload/upload-bucket.ts
index 8ea81dc567..0bd96abd34 100644
--- a/infra/stacks/bulk-upload/upload-bucket.ts
+++ b/infra/stacks/bulk-upload/upload-bucket.ts
@@ -1,6 +1,6 @@
import * as aws from '@pulumi/aws';
import * as pulumi from '@pulumi/pulumi';
-import { createBucketV2 } from '../../packages/resources';
+import { createBucketV2 } from '../../packages/resources/src/resources/bucket';
import { stack } from '../../packages/shared';
const isLocal = stack === 'local';
@@ -38,6 +38,16 @@ export class BulkUploadBucket extends pulumi.ComponentResource {
id: 'bulk-upload-bucket',
bucketName,
transferAcceleration: false,
+ lifecycleRules: [
+ {
+ id: 'slack-import-cleanup',
+ status: 'Enabled',
+ filter: { prefix: 'slack-import/' },
+ expiration: { days: 14 },
+ },
+ ],
+ // The helper owns CORS: PUT and allowedHeaders ['*'] already permit
+ // If-None-Match and x-amz-checksum-sha256 without a second CORS resource.
tags,
},
{ parent: this }
@@ -55,35 +65,39 @@ export class BulkUploadBucket extends pulumi.ComponentResource {
{ parent: this }
);
- // Only grant access to the document storage service role if it exists
- const allowAccessPolicy = pulumi
- .all([this.bucket.arn, cloudStorageServiceRoleArn])
- .apply(([bucketArn, roleArn]) => {
- const principals = roleArn
- ? [{ type: 'AWS', identifiers: [roleArn] }]
- : [{ type: 'AWS', identifiers: ['*'] }];
-
- return aws.iam.getPolicyDocumentOutput({
- statements: [
- {
- principals,
- actions: [
- 's3:GetObject',
- 's3:PutObject',
- 's3:GetObjectAttributes',
- 's3:ListBucket',
- ],
- resources: [bucketArn, pulumi.interpolate`${bucketArn}/*`],
- },
+ const bucketPolicy = pulumi.jsonStringify({
+ Version: '2012-10-17',
+ Statement: [
+ {
+ // Preserve existing bulk-upload access, including the local stack.
+ Effect: 'Allow',
+ Principal: { AWS: cloudStorageServiceRoleArn ?? '*' },
+ Action: [
+ 's3:GetObject',
+ 's3:PutObject',
+ 's3:GetObjectAttributes',
+ 's3:ListBucket',
],
- });
- });
+ Resource: [this.bucket.arn, pulumi.interpolate`${this.bucket.arn}/*`],
+ },
+ {
+ Sid: 'RequireImmutableSlackImports',
+ Effect: 'Deny',
+ Principal: '*',
+ Action: ['s3:PutObject'],
+ Resource: [pulumi.interpolate`${this.bucket.arn}/slack-import/*`],
+ // Negated matching also denies a missing header. This does not
+ // affect extract/ or any other existing bulk-upload keys.
+ Condition: { StringNotEquals: { 's3:if-none-match': '*' } },
+ },
+ ],
+ });
new aws.s3.BucketPolicy(
'bulk-upload-bucket-policy',
{
bucket: this.bucket.bucket,
- policy: allowAccessPolicy.apply((policy) => policy.json),
+ policy: bucketPolicy,
},
{ parent: this }
);
diff --git a/infra/stacks/cloud-storage-service/SLACK_IMPORT_ROLLOUT.md b/infra/stacks/cloud-storage-service/SLACK_IMPORT_ROLLOUT.md
new file mode 100644
index 0000000000..0a9ada1e0f
--- /dev/null
+++ b/infra/stacks/cloud-storage-service/SLACK_IMPORT_ROLLOUT.md
@@ -0,0 +1,114 @@
+# Slack archive worker deployment
+
+The cloud-storage-service stack owns the queues and a **separate**
+`slack-import-worker` ECS service, image, task role and execution role. It is not
+part of the DSS process and has no listener, ingress rule or load balancer.
+CI registers `slack_import_worker` under the existing `document-storage-service`
+stack entry (like email's pubsub worker); do not register a second concurrent
+Pulumi deployment for the same stack.
+
+## Operator prerequisites (before deploying the worker)
+
+The worker is **not provisioned by default**. The stack's
+`deploy_slack_import_worker` Pulumi config defaults to `false`, so initial
+previews/deployments can create the queues and DSS upload policy without looking
+up a not-yet-provisioned worker Doppler secret. This is a deployment gate, separate
+from the runtime `SLACK_IMPORT_ENABLED` intake flag.
+
+1. Apply the Slack import schema migrations and deploy the scoped search backfill
+ API/index changes first. The stack reads `searchProcessingServiceUrl` from
+ `macro-inc/search-processing-service/<env>`. This is the **processing** API at
+ `/search-processing`, not the search query service used by `SearchServiceClient`.
+2. Register the `slack-import-worker` Doppler project/configs for dev and prod and
+ configure the existing ECS sync convention to AWS Secrets Manager:
+ `/doppler-sync/slack-import-worker/<env>/doppler`.
+3. Register these exact keys in each sync's JSON, with raw configuration values
+ (not names of other Secrets Manager secrets):
+
+ | Key | Initial value |
+ | --- | --- |
+ | `DATABASE_URL` | Environment's MacroDB proxy URL with the worker's database access |
+ | `INTERNAL_API_KEY` | Same raw internal auth credential accepted by search-processing's backfill API |
+ | `SLACK_IMPORT_ENABLED` | `false` |
+ | `SLACK_IMPORT_CONCURRENCY` | `1` |
+
+ ECS selects these JSON keys individually using the shared Doppler execution
+ role. It intentionally does **not** inject `APP_SECRETS_JSON`: MacroConfig
+ prioritizes that JSON over plain env vars and would shadow infrastructure
+ wiring. Updating Doppler alone does not update a running task's environment;
+ restart/deploy the worker to pick up changes.
+4. Infrastructure supplies `ENVIRONMENT`, `UPLOAD_STAGING_BUCKET` (bulk-upload's
+ bucket output), `OVERRIDE_SLACK_IMPORT_QUEUE`, `OVERRIDE_SLACK_IMPORT_DLQ`,
+ `OVERRIDE_CONNECTION_GATEWAY_URL`, `OVERRIDE_SEARCH_PROCESSING_SERVICE_URL`,
+ `DD_SERVICE` and `DD_ENV`. Document/register those names in Doppler's service
+ configuration inventory, but do not add competing overrides. There is no
+ `SEARCH_SERVICE_URL` dependency. No Slack OAuth/bot credentials are needed.
+5. Ensure private-subnet egress can reach MacroDB, AWS endpoints and the gateway.
+ The task role only receives the staging-read and main/DLQ policy from
+ `SlackImportQueue`; it does not inherit DSS's permissions. The execution role
+ reads the worker's Doppler sync, not the task role.
+
+No remote secrets are created by the source change itself. After completing the
+above prerequisites, opt in from `infra/stacks/cloud-storage-service/`:
+
+```sh
+pulumi config set deploy_slack_import_worker true --stack <dev-or-prod>
+pulumi preview --stack <dev-or-prod>
+```
+
+Deploy the reviewed change through the normal deployment workflow. Once the
+worker is deployed, keep this gate enabled: setting it to false removes its ECS
+service and associated resources. Pause intake with `SLACK_IMPORT_ENABLED=false`
+instead, so maintenance and DLQ reconciliation continue.
+
+## Rollout values and checks
+
+- When provisioned, desired count: **1**; concurrency: **1** (runtime rejects values above 2).
+- Task: **1024 CPU units / 2048 MiB**. Worker hard limit: 1536 MiB; log router:
+ 128 MiB; Datadog: 384 MiB. Do not allocate the full task budget to the worker.
+- Worker stop timeout: **120 seconds**. SIGTERM stops intake and drains work.
+- Main queue: `slack-import-queue-<env>`; visibility: **900 seconds**; redrive
+ after **5 receives** to `slack-import-dlq-<env>`; DLQ retention: **14 days**.
+ These match `macro_queues::SlackImportQueue` / `SlackImportDlq`.
+- Keep UI/import admission gated while verifying permissions, search publication,
+ a small synthetic import, cancellation, worker restart and DLQ reconciliation.
+ Then set `SLACK_IMPORT_ENABLED=true` and redeploy the worker before opening
+ admission. This flag pauses claims/outbox sends, **not** maintenance/DLQ
+ reconciliation; even paused tasks require working DB/queue/search configuration.
+- Monitor ECS deployment failure alarms, task OOM/restarts, queue age, the existing
+ DLQ alarm, import leases and search receipts. Pause admission and set the worker
+ flag false to stop new imports; do not purge queues or delete imported data.
+
+## Local execution (explicit opt-in)
+
+Start a local stack using [the local guide](../../../docs/RUNNING_LOCALLY.md),
+never `run_dev` or a shared-dev environment. The inventory deliberately excludes
+this worker from default builds/startup. Local provisioning creates both queues
+and reapplies visibility/redrive/retention settings after all queues exist.
+Existing queues do not need to be deleted. There is no staging S3 event trigger.
+
+For the default `macro` instance, put `SLACK_IMPORT_ENABLED=true` and
+`SLACK_IMPORT_CONCURRENCY=1` in an untracked env file, then start with
+`just run_local --no-doppler --env-file <file>`. Once that stack is ready, run
+from the repository root in another terminal:
+
+```sh
+export MACRO_ENV_FILE="$PWD/infra/local/generated/macro/local.generated.env"
+docker compose --project-directory . -p macro \
+ -f docker/docker-compose.yml \
+ -f infra/local/generated/macro/docker-compose.override.yml \
+ --env-file "$MACRO_ENV_FILE" --profile slack-import \
+ up -d --build --no-deps slack_import_worker
+```
+
+The opt-in image builds `slack_import_worker` using the shared Dockerfile. For a
+named instance use its generated directory and Compose project name
+`macro-<instance>` instead. The already-running local stack supplies Postgres,
+LocalStack, connection-gateway and search-processing; `--no-deps` avoids rebuilding
+or restarting them. Stop this worker before stopping the rest of the stack.
+
+Local env wiring uses `bulk-upload-staging`, full LocalStack queue URL overrides,
+`http://connection-gateway:8080`, and
+`http://search-processing-service:8080`. The local `INTERNAL_API_KEY` matches
+search-processing's internal auth key. The default worker flag is false unless
+explicitly overridden. Do not inject a deployed `APP_SECRETS_JSON` locally.
diff --git a/infra/stacks/cloud-storage-service/cloud-storage-service.ts b/infra/stacks/cloud-storage-service/cloud-storage-service.ts
index 1e1e69cd3d..9e497bbbbf 100644
--- a/infra/stacks/cloud-storage-service/cloud-storage-service.ts
+++ b/infra/stacks/cloud-storage-service/cloud-storage-service.ts
@@ -41,6 +41,7 @@ type CreateCloudStorageServiceServiceArgs = {
healthCheckPath: string;
secretKeyArns: (pulumi.Output<string> | string)[];
queueArns: (pulumi.Output<string> | string)[];
+ slackImportUploadPolicyArn: pulumi.Output<string> | string;
callRecordingCrudPolicyArn: pulumi.Output<string> | string;
snsPlatformArns: (pulumi.Output<string> | string)[];
tags: { [key: string]: string };
@@ -69,6 +70,7 @@ export class CloudStorageService extends pulumi.ComponentResource {
cloudStorageClusterName,
secretKeyArns,
queueArns,
+ slackImportUploadPolicyArn,
callRecordingCrudPolicyArn,
snsPlatformArns,
tags,
@@ -245,6 +247,15 @@ export class CloudStorageService extends pulumi.ComponentResource {
{ parent: this, dependsOn: [queuePolicy, this.role] }
);
+ new aws.iam.RolePolicyAttachment(
+ `${BASE_NAME}-role-slack-import-upload-att-${stack}`,
+ {
+ role: this.role,
+ policyArn: slackImportUploadPolicyArn,
+ },
+ { parent: this }
+ );
+
new aws.iam.RolePolicyAttachment(
`${BASE_NAME}-role-call-recording-att-${stack}`,
{
diff --git a/infra/stacks/cloud-storage-service/index.ts b/infra/stacks/cloud-storage-service/index.ts
index a36213c7be..3b17063b2a 100644
--- a/infra/stacks/cloud-storage-service/index.ts
+++ b/infra/stacks/cloud-storage-service/index.ts
@@ -26,6 +26,8 @@ import {
} from './document-upload-finalizer-lambda';
import { CalendarReminderDispatchQueue } from './calendar-reminder-dispatch-queue';
import { ReminderDispatchQueue } from './reminder-dispatch-queue';
+import { SlackImportQueue } from './slack-import-queue';
+import { deploySlackImportWorker } from './slack-import-worker';
import { WafObservability } from './waf-observability';
const tags = {
@@ -192,6 +194,47 @@ export const bulkUploadLambdaRoleArn = bulkUploadStack
.getOutput('uploadExtractHandlerLambdaRoleArn')
.apply((arn) => arn as string);
+const bulkUploadBucketName = bulkUploadStack
+ .requireOutput('bulkUploadBucketName')
+ .apply((name) => name as string);
+
+const slackImportQueue = new SlackImportQueue(`slack-import-${stack}`, {
+ stagingBucketArn: pulumi.interpolate`arn:aws:s3:::${bulkUploadBucketName}`,
+ tags,
+});
+
+export const slackImportQueueArn = slackImportQueue.queue.arn;
+export const slackImportQueueName = slackImportQueue.queue.name;
+export const slackImportDlqArn = slackImportQueue.dlq.arn;
+export const slackImportDlqName = slackImportQueue.dlq.name;
+export const slackImportWorkerPolicyArn = slackImportQueue.workerPolicy.arn;
+
+const searchProcessingStack = new pulumi.StackReference(
+ 'slack-import-search-processing',
+ { name: `macro-inc/search-processing-service/${stack}` }
+);
+const slackImportWorker = deploySlackImportWorker(
+ `slack-import-worker-${stack}`,
+ {
+ ecsClusterArn: cloudStorageClusterArn,
+ vpc: coparse_api_vpc,
+ workerPolicyArn: slackImportQueue.workerPolicy.arn,
+ stagingBucketName: bulkUploadBucketName,
+ queueName: slackImportQueue.queue.name,
+ dlqName: slackImportQueue.dlq.name,
+ gatewayUrl: getServiceUrl(ServiceUrl.CONNECTION_GATEWAY_URL),
+ // This is the processing backfill API, not SearchServiceClient's query API.
+ searchProcessingUrl: searchProcessingStack.requireOutput(
+ 'searchProcessingServiceUrl'
+ ),
+ tags,
+ }
+);
+export const slackImportWorkerRoleArn = slackImportWorker?.role.arn;
+export const slackImportWorkerServiceName =
+ slackImportWorker?.service.service.name;
+export const slackImportWorkerSgId = slackImportWorker?.serviceSg.id;
+
export const docxUploadBucketArn = docxUploadBucket.arn;
export const docxUploadBucketName = docxUploadBucket.id;
@@ -312,6 +355,7 @@ const cloudStorageService = new CloudStorageService(
calWebhookSecretKeyArn,
calEventTypeContentNamesKeyArn,
],
+ slackImportUploadPolicyArn: slackImportQueue.uploadPolicy.arn,
callRecordingCrudPolicyArn,
snsPlatformArns: [snsApnsVoipPlatformArn],
containerEnvVars: [
diff --git a/infra/stacks/cloud-storage-service/slack-import-queue.test.ts b/infra/stacks/cloud-storage-service/slack-import-queue.test.ts
new file mode 100644
index 0000000000..0e0a4c5456
--- /dev/null
+++ b/infra/stacks/cloud-storage-service/slack-import-queue.test.ts
@@ -0,0 +1,131 @@
+import { beforeAll, describe, expect, mock, test } from 'bun:test';
+import * as pulumi from '@pulumi/pulumi';
+
+const resources: pulumi.runtime.MockResourceArgs[] = [];
+const bucketArn = 'arn:aws:s3:::bulk-upload-staging-dev';
+const mainArn = 'arn:aws:sqs:us-east-1:123456789012:slack-import-queue-dev';
+const dlqArn = 'arn:aws:sqs:us-east-1:123456789012:slack-import-dlq-dev';
+
+beforeAll(async () => {
+ pulumi.runtime.setAllConfig({ 'aws:region': 'us-east-1' });
+ pulumi.runtime.setMocks(
+ {
+ newResource(args) {
+ resources.push(args);
+ return {
+ id: args.name,
+ state: {
+ ...args.inputs,
+ arn: `arn:aws:sqs:us-east-1:123456789012:${args.inputs.name ?? args.name}`,
+ },
+ };
+ },
+ call(args) {
+ return { ...args.inputs, secretString: 'test-only' };
+ },
+ },
+ 'slack-import-tests',
+ 'dev',
+ false
+ );
+ // Avoid the shared barrel's pre-existing ESM cycle, not the Queue component.
+ mock.module('../../packages/shared', () => ({
+ stack: 'dev',
+ CLOUD_TRAIL_SNS_TOPIC_ARN: 'arn:aws:sns:us-east-1:123456789012:alerts',
+ }));
+ const { SlackImportQueue } = await import('./slack-import-queue');
+ new SlackImportQueue('slack-import-dev', {
+ stagingBucketArn: bucketArn,
+ tags: {},
+ });
+ await pulumi.runtime.waitForRPCs();
+});
+
+function resource(type: string, name: string): pulumi.runtime.MockResourceArgs {
+ const result = resources.find(
+ (item) => item.type === type && item.name === name
+ );
+ if (!result) throw new Error(`Missing resource ${type} ${name}`);
+ return result;
+}
+
+describe('Slack import queue and IAM', () => {
+ test('shared Queue creates the actual typed main/DLQ names, redrive and alarm', () => {
+ const main = resource(
+ 'aws:sqs/queue:Queue',
+ 'slack-import-queue-dev'
+ ).inputs;
+ const dlq = resource('aws:sqs/queue:Queue', 'slack-import-dlq-dev').inputs;
+ expect(main.name).toBe('slack-import-queue-dev');
+ expect(main.visibilityTimeoutSeconds).toBe(900);
+ expect(main.fifoQueue).toBe(false);
+ expect(JSON.parse(main.redrivePolicy)).toEqual({
+ deadLetterTargetArn: dlqArn,
+ maxReceiveCount: 5,
+ });
+ expect(dlq.name).toBe('slack-import-dlq-dev');
+ expect(dlq.messageRetentionSeconds).toBe(14 * 24 * 60 * 60);
+ expect(
+ resources.filter((item) => item.type === 'aws:sqs/queue:Queue')
+ ).toHaveLength(2);
+ expect(
+ resource(
+ 'aws:cloudwatch/metricAlarm:MetricAlarm',
+ 'slack-import-dlq-alarm'
+ ).inputs.dimensions
+ ).toEqual({
+ QueueName: dlq.name,
+ });
+ });
+
+ test('DSS can only put and verify Slack staging objects, not operate the queue', () => {
+ const policy = resource(
+ 'aws:iam/policy:Policy',
+ 'slack-import-dev-upload-policy'
+ ).inputs.policy;
+ expect(policy).toEqual({
+ Version: '2012-10-17',
+ Statement: [
+ {
+ Effect: 'Allow',
+ Action: ['s3:PutObject', 's3:GetObject'],
+ Resource: [`${bucketArn}/slack-import/*`],
+ },
+ ],
+ });
+ });
+
+ test('worker can read staging, reconcile both queues and publish only to main', () => {
+ const policy = resource(
+ 'aws:iam/policy:Policy',
+ 'slack-import-dev-worker-policy'
+ ).inputs.policy;
+ expect(policy).toEqual({
+ Version: '2012-10-17',
+ Statement: [
+ {
+ Effect: 'Allow',
+ Action: ['s3:GetObject'],
+ Resource: [`${bucketArn}/slack-import/*`],
+ },
+ {
+ Effect: 'Allow',
+ Action: [
+ 'sqs:GetQueueUrl',
+ 'sqs:ReceiveMessage',
+ 'sqs:DeleteMessage',
+ 'sqs:ChangeMessageVisibility',
+ ],
+ Resource: [mainArn, dlqArn],
+ },
+ {
+ Effect: 'Allow',
+ Action: ['sqs:SendMessage'],
+ Resource: [mainArn],
+ },
+ ],
+ });
+ expect(JSON.stringify(policy)).not.toContain('sqs:*');
+ expect(JSON.stringify(policy)).not.toContain('s3:HeadObject');
+ });
+});
diff --git a/infra/stacks/cloud-storage-service/slack-import-queue.ts b/infra/stacks/cloud-storage-service/slack-import-queue.ts
new file mode 100644
index 0000000000..8348ecfa41
--- /dev/null
+++ b/infra/stacks/cloud-storage-service/slack-import-queue.ts
@@ -0,0 +1,87 @@
+import * as aws from '@pulumi/aws';
+import * as pulumi from '@pulumi/pulumi';
+import { Queue } from '../../packages/resources/src/resources/queue';
+
+interface SlackImportQueueArgs {
+ stagingBucketArn: pulumi.Input<string>;
+ tags: { [key: string]: string };
+}
+
+/** Independent worker queue and policies; DSS only signs and verifies uploads. */
+export class SlackImportQueue extends pulumi.ComponentResource {
+ readonly queue: aws.sqs.Queue;
+ readonly dlq: aws.sqs.Queue;
+ readonly uploadPolicy: aws.iam.Policy;
+ readonly workerPolicy: aws.iam.Policy;
+
+ constructor(
+ name: string,
+ { stagingBucketArn, tags }: SlackImportQueueArgs,
+ opts?: pulumi.ComponentResourceOptions
+ ) {
+ super('my:components:SlackImportQueue', name, {}, opts);
+
+ const queue = new Queue(
+ 'slack-import',
+ { tags, maxReceiveCount: 5, visibilityTimeoutSeconds: 900 },
+ { parent: this }
+ );
+ this.queue = queue.queue;
+ this.dlq = queue.dlq;
+
+ const stagingObjects = pulumi.interpolate`${stagingBucketArn}/slack-import/*`;
+ this.uploadPolicy = new aws.iam.Policy(
+ `${name}-upload-policy`,
+ {
+ policy: {
+ Version: '2012-10-17',
+ Statement: [
+ {
+ Effect: 'Allow',
+ // HeadObject verification is authorized by GetObject, not HeadObject.
+ Action: ['s3:PutObject', 's3:GetObject'],
+ Resource: [stagingObjects],
+ },
+ ],
+ },
+ tags,
+ },
+ { parent: this }
+ );
+
+ // Attach this policy to the independent Fargate worker's task role.
+ this.workerPolicy = new aws.iam.Policy(
+ `${name}-worker-policy`,
+ {
+ policy: {
+ Version: '2012-10-17',
+ Statement: [
+ {
+ Effect: 'Allow',
+ Action: ['s3:GetObject'],
+ Resource: [stagingObjects],
+ },
+ {
+ Effect: 'Allow',
+ Action: [
+ 'sqs:GetQueueUrl',
+ 'sqs:ReceiveMessage',
+ 'sqs:DeleteMessage',
+ 'sqs:ChangeMessageVisibility',
+ ],
+ Resource: [this.queue.arn, this.dlq.arn],
+ },
+ {
+ Effect: 'Allow',
+ // Also authorizes SendMessageBatch for the durable outbox.
+ Action: ['sqs:SendMessage'],
+ Resource: [this.queue.arn],
+ },
+ ],
+ },
+ tags,
+ },
+ { parent: this }
+ );
+ }
+}
diff --git a/infra/stacks/cloud-storage-service/slack-import-worker.test.ts b/infra/stacks/cloud-storage-service/slack-import-worker.test.ts
new file mode 100644
index 0000000000..9e01f9b8eb
--- /dev/null
+++ b/infra/stacks/cloud-storage-service/slack-import-worker.test.ts
@@ -0,0 +1,229 @@
+import { beforeAll, describe, expect, mock, test } from 'bun:test';
+import * as pulumi from '@pulumi/pulumi';
+import servicesConfig from '../../../.github/services-config.json';
+
+const resources: pulumi.runtime.MockResourceArgs[] = [];
+const calls: pulumi.runtime.MockCallArgs[] = [];
+const secretArn =
+ 'arn:aws:secretsmanager:us-east-1:123456789012:secret:doppler';
+
+beforeAll(async () => {
+ pulumi.runtime.setAllConfig({
+ 'aws:region': 'us-east-1',
+ 'cloud-storage-service:deploy_slack_import_worker': 'true',
+ });
+ pulumi.runtime.setMocks(
+ {
+ newResource(args) {
+ resources.push(args);
+ return {
+ id: args.name,
+ state: {
+ ...args.inputs,
+ name: args.inputs.name ?? args.name,
+ arn: `arn:aws:mock:us-east-1:123456789012:${args.name}`,
+ url: 'example.invalid/worker',
+ imageUri: 'example.invalid/worker:latest',
+ repository: { id: args.name },
+ service: { name: args.name, arn: 'arn:aws:ecs:service/worker' },
+ },
+ };
+ },
+ call(args) {
+ calls.push(args);
+ return { ...args.inputs, arn: secretArn, secretString: 'test-only' };
+ },
+ },
+ 'slack-import-tests',
+ 'dev',
+ false
+ );
+ // Isolate the shared barrel's ESM cycle, keeping the real Doppler/image helpers.
+ mock.module('../../packages/shared', () => ({
+ stack: 'dev',
+ config: new pulumi.Config('cloud-storage-service'),
+ CLOUD_TRAIL_SNS_TOPIC_ARN: 'arn:aws:sns:us-east-1:123456789012:alerts',
+ }));
+ const { deploySlackImportWorker } = await import('./slack-import-worker');
+ expect(
+ deploySlackImportWorker('slack-import-worker-dev', {
+ ecsClusterArn: 'cluster',
+ vpc: { vpcId: 'vpc', privateSubnetIds: ['private-subnet'] },
+ workerPolicyArn: 'scoped-worker-policy',
+ stagingBucketName: 'bulk-upload-staging-dev',
+ queueName: 'slack-import-queue-dev',
+ dlqName: 'slack-import-dlq-dev',
+ gatewayUrl: 'https://dev-gateway.macro.com/connection-gateway',
+ searchProcessingUrl: 'https://dev-gateway.macro.com/search-processing',
+ tags: {},
+ })
+ ).toBeDefined();
+ await pulumi.runtime.waitForRPCs();
+});
+
+function resource(type: string): pulumi.runtime.MockResourceArgs {
+ const result = resources.find((item) => item.type === type);
+ if (!result) throw new Error(`Missing resource ${type}`);
+ return result;
+}
+
+describe('Slack import Fargate worker', () => {
+ test('default and explicit opt-out create no worker resources or secret lookups', async () => {
+ const { deploySlackImportWorker } = await import('./slack-import-worker');
+ for (const value of [undefined, 'false']) {
+ pulumi.runtime.setAllConfig({
+ 'aws:region': 'us-east-1',
+ ...(value === undefined
+ ? {}
+ : { 'cloud-storage-service:deploy_slack_import_worker': value }),
+ });
+ const resourceCount = resources.length;
+ const callCount = calls.length;
+ expect(
+ deploySlackImportWorker('disabled-worker', {
+ ecsClusterArn: 'cluster',
+ vpc: { vpcId: 'vpc', privateSubnetIds: [] },
+ workerPolicyArn: 'policy',
+ stagingBucketName: 'staging',
+ queueName: 'queue',
+ dlqName: 'dlq',
+ gatewayUrl: 'https://gateway.invalid',
+ searchProcessingUrl: 'https://search.invalid',
+ tags: {},
+ })
+ ).toBeUndefined();
+ await pulumi.runtime.waitForRPCs();
+ expect(resources.length).toBe(resourceCount);
+ expect(calls.length).toBe(callCount);
+ }
+ });
+
+ test('CI builds the worker and detects import changes in the owning stack', () => {
+ const service = servicesConfig.services['document-storage-service'];
+ expect(service.stack_path).toBe('infra/stacks/cloud-storage-service/**');
+ expect(service.deploy_binaries).toContain('slack_import_worker');
+ for (const path of [
+ 'services/slack_import_worker/**',
+ 'crates/slack_integration/**',
+ 'crates/channels/**',
+ 'crates/messages/**',
+ 'crates/import/**',
+ 'crates/macro_queues/**',
+ 'crates/macro_service_urls/**',
+ ]) {
+ expect(service.source_paths).toContain(path);
+ }
+ });
+
+ test('independent no-port service has bounded resources including sidecars', () => {
+ const service = resource('awsx:ecs:FargateService').inputs;
+ expect(service.desiredCount).toBe(1);
+ expect(service.loadBalancers).toBeUndefined();
+ expect(service.networkConfiguration).toEqual({
+ subnets: ['private-subnet'],
+ securityGroups: ['slack-import-worker-dev-sg'],
+ assignPublicIp: false,
+ });
+ const task = service.taskDefinitionArgs;
+ expect(task.cpu).toBe('1024');
+ expect(task.memory).toBe('2048');
+ expect(task.containers.worker.stopTimeout).toBe(120);
+ expect(task.containers.worker.memory).toBe(1536);
+ let memory = 0;
+ for (const container of Object.values(task.containers) as {
+ memory: number;
+ memoryReservation?: number;
+ portMappings?: unknown[];
+ }[]) {
+ memory += container.memory;
+ expect(container.memoryReservation ?? 0).toBeLessThanOrEqual(
+ container.memory
+ );
+ expect(container.portMappings ?? []).toEqual([]);
+ }
+ expect(memory).toBe(2048);
+ expect(service.deploymentCircuitBreaker).toEqual({
+ enable: true,
+ rollback: true,
+ });
+ });
+
+ test('dedicated task role only receives the scoped import policy, with no ingress', () => {
+ expect(
+ resource('aws:iam/rolePolicyAttachment:RolePolicyAttachment').inputs
+ ).toMatchObject({
+ role: 'slack-import-worker-dev-role',
+ policyArn: 'scoped-worker-policy',
+ });
+ const sg = resource('aws:ec2/securityGroup:SecurityGroup').inputs;
+ expect(sg.ingress ?? []).toEqual([]);
+ expect(
+ resources.some((item) =>
+ /IngressRule|TargetGroup|ListenerRule/.test(item.type)
+ )
+ ).toBe(false);
+ expect(
+ resource('aws:vpc/securityGroupEgressRule:SecurityGroupEgressRule').inputs
+ ).toMatchObject({
+ cidrIpv4: '0.0.0.0/0',
+ ipProtocol: '-1',
+ });
+ });
+
+ test('uses the worker binary and its own Doppler sync, not the DSS image/config', () => {
+ expect(resource('awsx:ecr:Image').inputs.args).toEqual({
+ SERVICE_NAME: 'slack_import_worker',
+ });
+ expect(resource('awsx:ecr:Repository').inputs.name).toBe(
+ 'slack-import-worker-dev'
+ );
+ const task = resource('awsx:ecs:FargateService').inputs.taskDefinitionArgs;
+ expect(task.taskRole.roleArn).not.toBe(task.executionRole.roleArn);
+ expect(task.containers.worker.secrets).toEqual(
+ [
+ 'DATABASE_URL',
+ 'INTERNAL_API_KEY',
+ 'SLACK_IMPORT_ENABLED',
+ 'SLACK_IMPORT_CONCURRENCY',
+ ].map((key) => ({
+ name: key,
+ valueFrom: `${secretArn}:${key}::`,
+ }))
+ );
+ expect(
+ resources.some(
+ (item) => item.name === 'slack-import-worker-execution-role'
+ )
+ ).toBe(true);
+ expect(
+ calls.some(
+ (call) =>
+ call.inputs.secretId ===
+ '/doppler-sync/slack-import-worker/dev/doppler'
+ )
+ ).toBe(true);
+ });
+
+ test('wires queue names, staging and the processing API rather than search queries', () => {
+ const worker = resource('awsx:ecs:FargateService').inputs.taskDefinitionArgs
+ .containers.worker;
+ const env = Object.fromEntries(
+ worker.environment.map((entry: { name: string; value: string }) => [
+ entry.name,
+ entry.value,
+ ])
+ );
+ expect(env).toMatchObject({
+ ENVIRONMENT: 'dev',
+ UPLOAD_STAGING_BUCKET: 'bulk-upload-staging-dev',
+ OVERRIDE_SLACK_IMPORT_QUEUE: 'slack-import-queue-dev',
+ OVERRIDE_SLACK_IMPORT_DLQ: 'slack-import-dlq-dev',
+ OVERRIDE_CONNECTION_GATEWAY_URL:
+ 'https://dev-gateway.macro.com/connection-gateway',
+ OVERRIDE_SEARCH_PROCESSING_SERVICE_URL:
+ 'https://dev-gateway.macro.com/search-processing',
+ });
+ expect(env.SEARCH_SERVICE_URL).toBeUndefined();
+ expect(env.OVERRIDE_SEARCH_SERVICE_URL).toBeUndefined();
+ });
+});
diff --git a/infra/stacks/cloud-storage-service/slack-import-worker.ts b/infra/stacks/cloud-storage-service/slack-import-worker.ts
new file mode 100644
index 0000000000..3c4253ab35
--- /dev/null
+++ b/infra/stacks/cloud-storage-service/slack-import-worker.ts
@@ -0,0 +1,197 @@
+import * as aws from '@pulumi/aws';
+import * as awsx from '@pulumi/awsx';
+import * as pulumi from '@pulumi/pulumi';
+import {
+ DATADOG_API_KEY,
+ datadogAgentContainer,
+ fargateLogRouterSidecarContainer,
+} from '../../packages/resources/src/resources/datadog';
+import { DEFAULT_CONTINUE_BEFORE_STEADY_STATE } from '../../packages/resources/src/resources/ecs_deployment_defaults';
+import { EcsDeploymentFailureAlarm } from '../../packages/resources/src/resources/ecs_deployment_failure_alarm';
+import { EcrImage } from '../../packages/service/src/ecr';
+import { config, stack } from '../../packages/shared';
+import { DopplerEcsEnvironment } from '../../packages/shared/src/doppler_environment';
+
+interface SlackImportWorkerArgs {
+ ecsClusterArn: pulumi.Input<string>;
+ vpc: {
+ vpcId: pulumi.Input<string>;
+ privateSubnetIds: pulumi.Input<string[]>;
+ };
+ workerPolicyArn: pulumi.Input<string>;
+ stagingBucketName: pulumi.Input<string>;
+ queueName: pulumi.Input<string>;
+ dlqName: pulumi.Input<string>;
+ gatewayUrl: pulumi.Input<string>;
+ searchProcessingUrl: pulumi.Input<string>;
+ tags: { [key: string]: string };
+}
+
+const SERVICE_NAME = 'slack-import-worker';
+
+/** Opt in only after provisioning the worker's dedicated Doppler sync. */
+export function deploySlackImportWorker(
+ name: string,
+ args: SlackImportWorkerArgs
+): SlackImportWorker | undefined {
+ if (!(config.getBoolean('deploy_slack_import_worker') ?? false)) {
+ return undefined;
+ }
+ return new SlackImportWorker(name, args);
+}
+
+/** Independent, portless ECS consumer; no DSS task role or load balancer. */
+export class SlackImportWorker extends pulumi.ComponentResource {
+ readonly role: aws.iam.Role;
+ readonly serviceSg: aws.ec2.SecurityGroup;
+ readonly service: awsx.ecs.FargateService;
+
+ constructor(
+ name: string,
+ args: SlackImportWorkerArgs,
+ opts?: pulumi.ComponentResourceOptions
+ ) {
+ super('my:components:SlackImportWorker', name, {}, opts);
+ const { tags } = args;
+ const child = { parent: this };
+ this.role = new aws.iam.Role(
+ `${name}-role`,
+ {
+ assumeRolePolicy: aws.iam.assumeRolePolicyForPrincipal({
+ Service: 'ecs-tasks.amazonaws.com',
+ }),
+ tags,
+ },
+ child
+ );
+ const policyAttachment = new aws.iam.RolePolicyAttachment(
+ `${name}-policy`,
+ { role: this.role.name, policyArn: args.workerPolicyArn },
+ child
+ );
+ this.serviceSg = new aws.ec2.SecurityGroup(
+ `${name}-sg`,
+ {
+ vpcId: args.vpc.vpcId,
+ description: 'Slack import worker egress only',
+ tags,
+ },
+ child
+ );
+ const egress = new aws.vpc.SecurityGroupEgressRule(
+ `${name}-egress`,
+ {
+ securityGroupId: this.serviceSg.id,
+ cidrIpv4: '0.0.0.0/0',
+ ipProtocol: '-1',
+ tags,
+ },
+ child
+ );
+ const image = new EcrImage(
+ `${name}-image`,
+ {
+ repositoryId: `${name}-ecr`,
+ repositoryName: name,
+ imageId: `${name}-image`,
+ imagePath: '../../..',
+ dockerfile: 'docker/Dockerfile',
+ buildArgs: { SERVICE_NAME: 'slack_import_worker' },
+ platform: { family: 'linux', architecture: 'amd64' },
+ tags,
+ },
+ child
+ );
+ const doppler = new DopplerEcsEnvironment(SERVICE_NAME, { tags }, child);
+ // Select keys from the shared Doppler sync instead of injecting APP_SECRETS_JSON:
+ // MacroConfig gives that JSON precedence over infrastructure-owned env values.
+ const secrets = [
+ 'DATABASE_URL',
+ 'INTERNAL_API_KEY',
+ 'SLACK_IMPORT_ENABLED',
+ 'SLACK_IMPORT_CONCURRENCY',
+ ].map((key) => ({
+ name: key,
+ valueFrom: pulumi.interpolate`${doppler.containerSecrets[0].valueFrom}:${key}::`,
+ }));
+ this.service = new awsx.ecs.FargateService(
+ SERVICE_NAME,
+ {
+ cluster: args.ecsClusterArn,
+ desiredCount: 1,
+ continueBeforeSteadyState: DEFAULT_CONTINUE_BEFORE_STEADY_STATE,
+ deploymentCircuitBreaker: { enable: true, rollback: true },
+ networkConfiguration: {
+ subnets: args.vpc.privateSubnetIds,
+ securityGroups: [this.serviceSg.id],
+ assignPublicIp: false,
+ },
+ taskDefinitionArgs: {
+ cpu: '1024',
+ memory: '2048',
+ taskRole: { roleArn: this.role.arn },
+ executionRole: { roleArn: doppler.executionRole.arn },
+ runtimePlatform: {
+ operatingSystemFamily: 'LINUX',
+ cpuArchitecture: 'X86_64',
+ },
+ containers: {
+ // Hard limits leave 1536 MiB for import work and 512 MiB for telemetry.
+ log_router: { ...fargateLogRouterSidecarContainer, memory: 128 },
+ datadog_agent: {
+ ...datadogAgentContainer,
+ memory: 384,
+ portMappings: [],
+ },
+ worker: {
+ name: SERVICE_NAME,
+ image: image.image.imageUri,
+ essential: true,
+ memory: 1536,
+ stopTimeout: 120,
+ secrets,
+ environment: [
+ { name: 'ENVIRONMENT', value: stack },
+ {
+ name: 'UPLOAD_STAGING_BUCKET',
+ value: args.stagingBucketName,
+ },
+ { name: 'OVERRIDE_SLACK_IMPORT_QUEUE', value: args.queueName },
+ { name: 'OVERRIDE_SLACK_IMPORT_DLQ', value: args.dlqName },
+ {
+ name: 'OVERRIDE_CONNECTION_GATEWAY_URL',
+ value: args.gatewayUrl,
+ },
+ {
+ name: 'OVERRIDE_SEARCH_PROCESSING_SERVICE_URL',
+ value: args.searchProcessingUrl,
+ },
+ { name: 'DD_SERVICE', value: SERVICE_NAME },
+ { name: 'DD_ENV', value: stack },
+ ],
+ logConfiguration: {
+ logDriver: 'awsfirelens',
+ options: {
+ Name: 'datadog',
+ Host: 'http-intake.logs.us5.datadoghq.com',
+ apikey: DATADOG_API_KEY,
+ dd_service: SERVICE_NAME,
+ dd_source: 'fargate',
+ dd_tags: `env:${stack}`,
+ provider: 'ecs',
+ },
+ },
+ },
+ },
+ },
+ tags,
+ },
+ { parent: this, dependsOn: [policyAttachment, egress] }
+ );
+ new EcsDeploymentFailureAlarm(
+ `${name}-deployment-failure-alarm`,
+ { serviceName: SERVICE_NAME, serviceArn: this.service.service.arn, tags },
+ child
+ );
+ }
+}
diff --git a/infra/stacks/opensearch/helpers/scripts/add_imported_author.test.ts b/infra/stacks/opensearch/helpers/scripts/add_imported_author.test.ts
new file mode 100644
index 0000000000..8fca5e925f
--- /dev/null
+++ b/infra/stacks/opensearch/helpers/scripts/add_imported_author.test.ts
@@ -0,0 +1,99 @@
+import { Client } from '@opensearch-project/opensearch';
+import { serve, type Server } from 'bun';
+import { describe, expect, test } from 'bun:test';
+import {
+ addImportedAuthor,
+ IMPORTED_AUTHOR_MAPPING,
+} from './add_imported_author';
+
+type MappingServerOptions = {
+ exists?: boolean;
+ acknowledged?: boolean;
+ reject?: boolean;
+};
+type MappingRequest = { method: string; path: string; body: unknown };
+
+function mappingServer(options: MappingServerOptions = {}): {
+ client: Client;
+ server: Server<undefined>;
+ requests: MappingRequest[];
+} {
+ const requests: MappingRequest[] = [];
+ const server = serve({
+ port: 0,
+ async fetch(request) {
+ const path = new URL(request.url).pathname;
+ const body = request.method === 'HEAD' ? null : await request.json();
+ requests.push({ method: request.method, path, body });
+ if (request.method === 'HEAD') {
+ return new Response(null, {
+ status: options.exists === false ? 404 : 200,
+ });
+ }
+ if (options.reject) {
+ return Response.json({ error: 'conflicting mapping' }, { status: 400 });
+ }
+ return Response.json({ acknowledged: options.acknowledged ?? true });
+ },
+ });
+ const client = new Client({ node: server.url.toString() });
+ return { client, server, requests };
+}
+
+describe('addImportedAuthor', () => {
+ test('adds only author mapping to the live alias and can be rerun', async () => {
+ const { client, server, requests } = mappingServer();
+ try {
+ await addImportedAuthor(client, false);
+ await addImportedAuthor(client, false);
+ expect(requests.filter((request) => request.method !== 'HEAD')).toEqual([
+ {
+ method: 'POST',
+ path: '/channels/_mapping',
+ body: { properties: { imported_author: IMPORTED_AUTHOR_MAPPING } },
+ },
+ {
+ method: 'POST',
+ path: '/channels/_mapping',
+ body: { properties: { imported_author: IMPORTED_AUTHOR_MAPPING } },
+ },
+ ]);
+ } finally {
+ await client.close();
+ server.stop();
+ }
+ });
+
+ test('dry run never writes', async () => {
+ const { client, server, requests } = mappingServer();
+ try {
+ await addImportedAuthor(client, true, 'channels_v2');
+ expect(requests).toEqual([
+ { method: 'HEAD', path: '/channels_v2', body: null },
+ ]);
+ } finally {
+ await client.close();
+ server.stop();
+ }
+ });
+
+ const failureCases: MappingServerOptions[] = [
+ { exists: false },
+ { acknowledged: false },
+ { reject: true },
+ ];
+ for (const options of failureCases) {
+ test(`fails clearly: ${JSON.stringify(options)}`, async () => {
+ const { client, server, requests } = mappingServer(options);
+ try {
+ await expect(addImportedAuthor(client, false)).rejects.toThrow();
+ if (options.exists === false) {
+ expect(requests).toHaveLength(1);
+ }
+ } finally {
+ await client.close();
+ server.stop();
+ }
+ });
+ }
+});
diff --git a/infra/stacks/opensearch/helpers/scripts/add_imported_author.ts b/infra/stacks/opensearch/helpers/scripts/add_imported_author.ts
new file mode 100644
index 0000000000..09fa455fc2
--- /dev/null
+++ b/infra/stacks/opensearch/helpers/scripts/add_imported_author.ts
@@ -0,0 +1,46 @@
+import type { Client } from '@opensearch-project/opensearch';
+import { client } from '../client';
+import { CHANNELS_ALIAS, IS_DRY_RUN } from '../constants';
+
+export const IMPORTED_AUTHOR_MAPPING = {
+ type: 'text' as const,
+ analyzer: 'content_text',
+ copy_to: 'content',
+};
+
+/**
+ * Add the field to the live alias (including legacy physical `channels` indices).
+ * Run before deploying the writer, then backfill affected channels: mapping
+ * updates do not index fields already present in old documents' _source.
+ * No index creation, alias swap, or message-content rewrite is performed.
+ * From helpers/: DRY_RUN=false bun scripts/add_imported_author.ts
+ * Optional INDEX targets a physical backfill index instead of the live alias.
+ */
+export async function addImportedAuthor(
+ opensearchClient: Pick<Client, 'indices'>,
+ dryRun: boolean,
+ index: string = CHANNELS_ALIAS
+): Promise<void> {
+ const exists = await opensearchClient.indices.exists({ index });
+ if (!exists.body) {
+ throw new Error(`Channel index "${index}" does not exist`);
+ }
+
+ if (dryRun) {
+ console.log(`[DRY-RUN] Would add imported_author to ${index}`);
+ return;
+ }
+
+ const response = await opensearchClient.indices.putMapping({
+ index,
+ body: { properties: { imported_author: IMPORTED_AUTHOR_MAPPING } },
+ });
+ if (!response.body.acknowledged) {
+ throw new Error(`Failed to add imported_author mapping to ${index}`);
+ }
+ console.log(`${index}: imported_author mapped; backfill affected channels`);
+}
+
+if (import.meta.main) {
+ await addImportedAuthor(client(), IS_DRY_RUN, process.env.INDEX);
+}
diff --git a/infra/stacks/opensearch/helpers/scripts/create_indices.test.ts b/infra/stacks/opensearch/helpers/scripts/create_indices.test.ts
index d92bd98453..6ed335b6c6 100644
--- a/infra/stacks/opensearch/helpers/scripts/create_indices.test.ts
+++ b/infra/stacks/opensearch/helpers/scripts/create_indices.test.ts
@@ -6,6 +6,24 @@ import {
selectIndexSpecs,
} from './create_indices';
+test('channel fallback authors are searchable content, not sender identities', () => {
+ const spec = INDEX_SPECS.find((spec) => spec.aliasName === 'channels');
+ expect(spec?.body).toMatchObject({
+ mappings: {
+ dynamic: 'false',
+ properties: {
+ imported_author: {
+ type: 'text',
+ analyzer: 'content_text',
+ copy_to: 'content',
+ },
+ content: { type: 'text', analyzer: 'content_text' },
+ sender_id: { type: 'keyword', index: true },
+ },
+ },
+ });
+});
+
describe('planCreateIndex', () => {
test('fresh env — index missing, alias free → create with alias', () => {
const plan = planCreateIndex({
diff --git a/infra/stacks/opensearch/helpers/scripts/create_indices.ts b/infra/stacks/opensearch/helpers/scripts/create_indices.ts
index 647c45a32f..9879873c03 100644
--- a/infra/stacks/opensearch/helpers/scripts/create_indices.ts
+++ b/infra/stacks/opensearch/helpers/scripts/create_indices.ts
@@ -21,6 +21,7 @@ import {
SHARD_SETTINGS,
SLOWLOG_SETTINGS,
} from '../constants';
+import { IMPORTED_AUTHOR_MAPPING } from './add_imported_author';
export type CreateIndexArgs = {
indexName: string;
@@ -424,6 +425,8 @@ const CHANNEL_BODY = {
type: 'keyword',
index: true,
},
+ // copy_to indexes the name without modifying the message's _source.content.
+ imported_author: IMPORTED_AUTHOR_MAPPING,
mentions: {
type: 'keyword',
index: true,
diff --git a/packages/lexical-core/tests/slack-import-links.test.ts b/packages/lexical-core/tests/slack-import-links.test.ts
new file mode 100644
index 0000000000..75473fb547
--- /dev/null
+++ b/packages/lexical-core/tests/slack-import-links.test.ts
@@ -0,0 +1,123 @@
+import { $isLinkNode } from '@lexical/link';
+import {
+ $convertFromMarkdownString,
+ $convertToMarkdownString,
+} from '@lexical/markdown';
+import {
+ $getRoot,
+ $isElementNode,
+ createEditor,
+ type LexicalEditor,
+ type LexicalNode,
+} from 'lexical';
+import { describe, expect, it } from 'vitest';
+import fixtures from '../../../crates/slack_integration/tests/fixtures/native-message-links.json';
+import { SupportedNodeTypes } from '../node-list';
+import { INTERNAL_TRANSFORMERS } from '../transformers';
+
+function editor(): LexicalEditor {
+ return createEditor({
+ nodes: SupportedNodeTypes,
+ onError(error) {
+ throw error;
+ },
+ });
+}
+
+function importMarkdown(markdown: string): LexicalEditor {
+ const result = editor();
+ result.update(
+ () => $convertFromMarkdownString(markdown, INTERNAL_TRANSFORMERS),
+ { discrete: true }
+ );
+ return result;
+}
+
+function activeNodes(node: LexicalNode): LexicalNode[] {
+ const children = $isElementNode(node) ? node.getChildren() : [];
+ const active = ['link', 'user-mention', 'document-mention'].includes(
+ node.getType()
+ );
+ return [...(active ? [node] : []), ...children.flatMap(activeNodes)];
+}
+
+function assertFixture(
+ result: LexicalEditor,
+ fixture: (typeof fixtures)[number],
+ expectedUrl = fixture.payload?.url
+): void {
+ result.getEditorState().read(() => {
+ const nodes = activeNodes($getRoot());
+ if (fixture.kind === 'inert') {
+ expect(nodes).toHaveLength(0);
+ return;
+ }
+ expect(nodes).toHaveLength(1);
+ const node = nodes[0];
+ expect(node.getType()).toBe(fixture.kind);
+ if ($isLinkNode(node)) {
+ expect(node.getURL()).toBe(expectedUrl);
+ expect(node.getTitle() ?? '').toBe('');
+ expect(node.getTextContent()).toBe(fixture.payload?.text);
+ } else {
+ expect(node.exportJSON()).toMatchObject(fixture.payload ?? {});
+ }
+ });
+}
+
+describe('Rust Slack import native message fixtures', () => {
+ it.each(fixtures)(
+ '$name imports with the real internal transformers',
+ (fixture) => {
+ const imported = importMarkdown(fixture.native);
+ assertFixture(imported, fixture);
+
+ // Exercise actual Lexical state serialization/deserialization, including
+ // labels containing newlines, closing/nested tags, quotes and backslashes.
+ const restored = editor();
+ restored.setEditorState(
+ restored.parseEditorState(
+ JSON.stringify(imported.getEditorState().toJSON())
+ )
+ );
+ assertFixture(restored, fixture);
+ }
+ );
+
+ it.each(['mailto', 'user', 'channel', 'root', 'reply'])(
+ '%s also round-trips through native markdown export',
+ (name) => {
+ const fixture = fixtures.find((entry) => entry.name === name);
+ if (!fixture) throw new Error(`Missing fixture ${name}`);
+ const imported = importMarkdown(fixture.native);
+ const markdown = imported
+ .getEditorState()
+ .read(() => $convertToMarkdownString(INTERNAL_TRANSFORMERS));
+ assertFixture(importMarkdown(markdown), fixture);
+ }
+ );
+
+ it('round-trips external links through markdown with Lexical URL escaping', () => {
+ const fixture = fixtures.find((entry) => entry.name === 'external');
+ if (!fixture?.payload?.url) throw new Error('Missing external fixture');
+ const imported = importMarkdown(fixture.native);
+ const markdown = imported
+ .getEditorState()
+ .read(() => $convertToMarkdownString(INTERNAL_TRANSFORMERS));
+ assertFixture(
+ importMarkdown(markdown),
+ fixture,
+ fixture.payload.url.replaceAll('(', '%28').replaceAll(')', '%29')
+ );
+ });
+
+ it('keeps emphasis around generated links without formatting their JSON labels', () => {
+ const fixture = fixtures.find((entry) => entry.name === 'external');
+ if (!fixture) throw new Error('Missing external fixture');
+ const imported = importMarkdown(`**${fixture.native}**`);
+ assertFixture(imported, fixture);
+ imported.getEditorState().read(() => {
+ expect($getRoot().getAllTextNodes()[0].hasFormat('bold')).toBe(true);
+ });
+ });
+});
diff --git a/packages/sdk/generated/cognition/types.gen.ts b/packages/sdk/generated/cognition/types.gen.ts
index 1b706e8ab7..652ac32f47 100644
--- a/packages/sdk/generated/cognition/types.gen.ts
+++ b/packages/sdk/generated/cognition/types.gen.ts
@@ -787,7 +787,7 @@ export type ImportStatus = 'staged' | 'importing' | 'imported' | 'discarded';
* Where an import entity was first staged from. Provenance only — never a
* visibility filter.
*/
-export type Initiator = 'onboarding' | 'chat';
+export type Initiator = 'onboarding' | 'chat' | 'archive';
export type JwtPayload = {
token: string;
diff --git a/packages/sdk/generated/storage/index.ts b/packages/sdk/generated/storage/index.ts
index 6b47d30d8b..6836f64108 100644
--- a/packages/sdk/generated/storage/index.ts
+++ b/packages/sdk/generated/storage/index.ts
@@ -1,4 +1,4 @@
// This file is auto-generated by @hey-api/openapi-ts
export { type Options, Sdk } from './sdk.gen';
-export type { AccessLevel, ActiveCallsResponse, ActiveCallSummary, ActiveMeeting, ActiveMeetingsResponse, ActivityType, AddFavoriteData, AddFavoriteError, AddFavoriteErrors, AddFavoriteRequest, AddFavoriteResponse, AddFavoriteResponses, AddParticipantsData, AddParticipantsError, AddParticipantsErrors, AddParticipantsRequest, AddParticipantsResponses, AddPinHandlerData, AddPinHandlerError, AddPinHandlerErrors, AddPinHandlerResponse, AddPinHandlerResponses, AddPinRequest, Agent, AgentActionId, AgentChannelScope, AgentMcpServer, AgentMcpServers, AgentPullRequestState, AgentSessionFilters, AgentSessionLifecycleEvent, Anchor, AnchorId, AnchorResponse, AnnotationIncrementalUpdate, ApiActivity, ApiAttachmentChannelReference, ApiAttachmentEntityReference, ApiAttachmentGenericReference, ApiChannelAttachment, ApiChannelAttachmentsPage, ApiChannelDetail, ApiChannelListMessage, ApiChannelListPage, ApiChannelListParticipant, ApiChannelListType, ApiChannelParticipant, ApiChannelWithLatest, ApiEntityFilterAst, ApiGroupByField, ApiGroupMeta, ApiParticipantListRole, ApiParticipantRole, ApiPropertyEntityType, ApproveHarnessPairingData, ApproveHarnessPairingError, ApproveHarnessPairingErrors, ApproveHarnessPairingResponse, ApproveHarnessPairingResponses, ApprovePairingRequest, AssignInitiativeTasksData, AssignInitiativeTasksError, AssignInitiativeTasksErrors, AssignInitiativeTasksResponse, AssignInitiativeTasksResponses, AssignTasksRequest, AssignTasksResponse, AssignTasksResult, AssignTaskStatus, AttachmentChange, AttendeeResponseStatus, BasicDocument, BasicDocumentSubType, BomPart, Bot, BotChannel, BotChannelType, BotId, BotKind, BotOwner, BotOwnerProfile, BotSenderProfile, BotToken, BranchNameResponse, BTreeMap, BulkWakeupRequest, BulkWakeupResponse, BulkWakeupSyncServiceDocumentsData, BulkWakeupSyncServiceDocumentsErrors, BulkWakeupSyncServiceDocumentsResponse, BulkWakeupSyncServiceDocumentsResponses, CalendarAttendee, CalendarEvent, CalendarEventFilters, CalendarEventSourceContent, CalendarMentionEvent, CalendarMentionPreviewItem, CalendarMentionPreviewKind, CalendarMentionPreviewRequest, CalendarMentionPreviewRequestItem, CalendarMentionPreviewResponse, CalendarOccurrence, CalendarOccurrenceItem, CalendarOccurrenceResponse, CalendarSyncStatus, CallActiveResponse, CallFilters, CallRecord, CallRecordGuest, CallRecordParticipant, CallRecordPreview, CallRecordPreviewData, CallRecordTranscriptSegment, CallStatus, CallTokenResponse, CallWebhookData, CallWebhookError, CallWebhookErrors, CallWebhookResponses, Channel, ChannelCreatedMetadata, ChannelDeletedMetadata, ChannelFilters, ChannelJoinCodeResponse, ChannelLabel, ChannelLabelRule, ChannelLabelsList, ChannelMessage, ChannelParticipant, ChannelParticipantAddedMetadata, ChannelParticipantRemovedMetadata, ChannelPreview, ChannelPreviewData, ChannelSender, ChannelSharePermission, ChannelThreadFilters, ChannelTopicEvent, ChannelType, ChannelUpdatedMetadata, ChannelWebhookRequest, ChannelWebhookResponse, ChannelWithParticipants, Chat, ChatFilters, CheckActiveCallData, CheckActiveCallError, CheckActiveCallErrors, CheckActiveCallResponse, CheckActiveCallResponses, ClaimedPairing, ClaimHarnessPairingData, ClaimHarnessPairingError, ClaimHarnessPairingErrors, ClaimHarnessPairingResponse, ClaimHarnessPairingResponses, ClaimPairingRequest, ClientOptions, CloudStorageItemType, CollabSurfaceResponse, CollabSurfaceTokenResponse, CommandFailure, CommandRejectedMetadata, ConferenceProvider, CopyDocumentData, CopyDocumentError, CopyDocumentErrors, CopyDocumentQueryParams, CopyDocumentRequest, CopyDocumentResponse, CopyDocumentResponse2, CopyDocumentResponses, CountedReaction, CreateAgentData, CreateAgentError, CreateAgentErrors, CreateAgentRequest, CreateAgentResponse, CreateAgentResponses, CreateAnchorData, CreateAnchorError, CreateAnchorErrors, CreateAnchorResponse, CreateAnchorResponses, CreateBotRequest, CreateBulkDocumentResponse, CreateBulkDocumentResponseData, CreateChannelData, CreateChannelError, CreateChannelErrors, CreateChannelLabelData, CreateChannelLabelError, CreateChannelLabelErrors, CreateChannelLabelRequest, CreateChannelLabelResponse, CreateChannelLabelResponses, CreateChannelRequest, CreateChannelResponse, CreateChannelResponse2, CreateChannelResponses, CreateChannelScopedBotData, CreateChannelScopedBotError, CreateChannelScopedBotErrors, CreateChannelScopedBotRequest, CreateChannelScopedBotResponse, CreateChannelScopedBotResponse2, CreateChannelScopedBotResponses, CreateCollabSurfaceTokenData, CreateCollabSurfaceTokenError, CreateCollabSurfaceTokenErrors, CreateCollabSurfaceTokenResponse, CreateCollabSurfaceTokenResponses, CreateCrmCompanyData, CreateCrmCompanyError, CreateCrmCompanyErrors, CreateCrmCompanyRequest, CreateCrmCompanyResponse, CreateCrmCompanyResponses, CreateCrmContactData, CreateCrmContactError, CreateCrmContactErrors, CreateCrmContactRequest, CreateCrmContactResponse, CreateCrmContactResponses, CreateDocumentData, CreateDocumentError, CreateDocumentErrors, CreateDocumentRequest, CreateDocumentResponse, CreateDocumentResponseData, CreateDocumentResponses, CreatedPairing, CreatedUserApiKey, CreateEntityMentionData, CreateEntityMentionError, CreateEntityMentionErrors, CreateEntityMentionRequest, CreateEntityMentionResponse, CreateEntityMentionResponse2, CreateEntityMentionResponses, CreateHarnessPairingData, CreateHarnessPairingError, CreateHarnessPairingErrors, CreateHarnessPairingResponse, CreateHarnessPairingResponses, CreateInitiativeData, CreateInitiativeError, CreateInitiativeErrors, CreateInitiativeRequest, CreateInitiativeResponse, CreateInitiativeResponses, CreateInstructionsDocumentResponse, CreateInstructionsHandlerData, CreateInstructionsHandlerError, CreateInstructionsHandlerErrors, CreateInstructionsHandlerResponse, CreateInstructionsHandlerResponses, CreateMarkdownDocumentRequest, CreateMarkdownDocumentResponse, CreateMarkdownHandlerData, CreateMarkdownHandlerError, CreateMarkdownHandlerErrors, CreateMarkdownHandlerResponse, CreateMarkdownHandlerResponses, CreateMeetingRequest, CreatePairingRequest, CreateProjectHandlerData, CreateProjectHandlerError, CreateProjectHandlerErrors, CreateProjectHandlerResponse, CreateProjectHandlerResponses, CreateProjectRequest, CreateProjectResponse, CreateReminderData, CreateReminderError, CreateReminderErrors, CreateReminderRequest, CreateReminderResponse, CreateReminderResponses, CreateSkillHandlerData, CreateSkillHandlerError, CreateSkillHandlerErrors, CreateSkillHandlerResponse, CreateSkillHandlerResponses, CreateSkillRequest, CreateSkillResponse, CreateSnippetHandlerData, CreateSnippetHandlerError, CreateSnippetHandlerErrors, CreateSnippetHandlerResponse, CreateSnippetHandlerResponses, CreateSnippetRequest, CreateSnippetResponse, CreateTaskHandlerData, CreateTaskHandlerError, CreateTaskHandlerErrors, CreateTaskHandlerResponse, CreateTaskHandlerResponses, CreateTaskRequest, CreateTaskResponse, CreateUnthreadedAnchorRequest, CreateUnthreadedAnchorResponse, CreateUnthreadedPdfAnchorRequest, CreateUserApiKeyData, CreateUserApiKeyError, CreateUserApiKeyErrors, CreateUserApiKeyRequest, CreateUserApiKeyResponse, CreateUserApiKeyResponses, CreateViewHandlerData, CreateViewHandlerError, CreateViewHandlerErrors, CreateViewHandlerResponse, CreateViewHandlerResponses, CreateViewRequest, CreateWebhookData, CreateWebhookError, CreateWebhookErrors, CreateWebhookRequest, CreateWebhookResponse, CreateWebhookResponse2, CreateWebhookResponses, CrmCompanyFilters, CrmCompanyResponse, CrmContactResponse, CrmDomainResponse, CrmPermissionRole, CrmStageInput, CrmStageResponse, CrmStagesResponse, CrmTeamSettingsResponse, CustomSpeakerAssignment, DataType, DeleteAnchorData, DeleteAnchorError, DeleteAnchorErrors, DeleteAnchorResponse, DeleteAnchorResponses, DeleteCallRecordData, DeleteCallRecordError, DeleteCallRecordErrors, DeleteCallRecordResponse, DeleteCallRecordResponses, DeleteChannelData, DeleteChannelError, DeleteChannelErrors, DeleteChannelLabelData, DeleteChannelLabelError, DeleteChannelLabelErrors, DeleteChannelLabelResponse, DeleteChannelLabelResponses, DeleteChannelResponse, DeleteChannelResponses, DeleteCollabSurfaceData, DeleteCollabSurfaceError, DeleteCollabSurfaceErrors, DeleteCollabSurfaceResponse, DeleteCollabSurfaceResponses, DeleteDocumentData, DeleteDocumentError, DeleteDocumentErrors, DeleteDocumentResponse, DeleteDocumentResponses, DeleteEntityMentionData, DeleteEntityMentionError, DeleteEntityMentionErrors, DeleteEntityMentionResponse, DeleteEntityMentionResponse2, DeleteEntityMentionResponses, DeleteHarnessData, DeleteHarnessError, DeleteHarnessErrors, DeleteHarnessResponse, DeleteHarnessResponses, DeleteHistoryHandlerData, DeleteHistoryHandlerError, DeleteHistoryHandlerErrors, DeleteHistoryHandlerResponse, DeleteHistoryHandlerResponses, DeleteInitiativeData, DeleteInitiativeError, DeleteInitiativeErrors, DeleteInitiativeResponse, DeleteInitiativeResponses, DeleteProjectHandlerData, DeleteProjectHandlerError, DeleteProjectHandlerErrors, DeleteProjectHandlerResponse, DeleteProjectHandlerResponses, DeleteReminderData, DeleteReminderError, DeleteReminderErrors, DeleteReminderResponse, DeleteReminderResponses, DeleteSelfHarnessData, DeleteSelfHarnessError, DeleteSelfHarnessErrors, DeleteSelfHarnessResponse, DeleteSelfHarnessResponses, DeleteUnthreadedAnchorRequest, DeleteUnthreadedAnchorResponse, DeleteUnthreadedPdfAnchorRequest, DeleteUserApiKeyData, DeleteUserApiKeyError, DeleteUserApiKeyErrors, DeleteUserApiKeyResponse, DeleteUserApiKeyResponses, DeleteUserDocumentViewLocationData, DeleteUserDocumentViewLocationError, DeleteUserDocumentViewLocationErrors, DeleteUserDocumentViewLocationResponse, DeleteUserDocumentViewLocationResponses, DeleteViewHandlerData, DeleteViewHandlerError, DeleteViewHandlerErrors, DeleteViewHandlerResponses, DeleteWebhookData, DeleteWebhookError, DeleteWebhookErrors, DeleteWebhookResponse, DeleteWebhookResponses, DescriptionDocumentId, DocumentBasic, DocumentContent, DocumentContentLocation, DocumentContentState, DocumentContentUploadedMetadata, DocumentCopiedMetadata, DocumentCreatedMetadata, DocumentDeletedMetadata, DocumentFilters, DocumentId, DocumentInteractionMetadata, DocumentMetadata, DocumentMetadataWithContent, DocumentPermissionsToken, DocumentPermissionsTokenRequest, DocumentPermissionsTokenResponse, DocumentPreview, DocumentPreviewData, DocumentPreviewDataSubType, DocumentPurgedMetadata, DocumentResponse, DocumentResponseMetadata, DocumentResponseMetadataWithContent, DocumentStorageServiceApiVersion, DocumentSubType, DocumentSyncContentUpdatedMetadata, DocumentSyncEditor, DocumentTeamShareResponse, DocumentTopicEvent, DocumentUpdatedMetadata, EditAnchorData, EditAnchorError, EditAnchorErrors, EditAnchorRequest, EditAnchorResponse, EditAnchorResponse2, EditAnchorResponses, EditCallRecordData, EditCallRecordError, EditCallRecordErrors, EditCallRecordRequest, EditCallRecordResponse, EditCallRecordResponses, EditCallTranscriptData, EditCallTranscriptError, EditCallTranscriptErrors, EditCallTranscriptRequest, EditCallTranscriptResponse, EditCallTranscriptResponses, EditDocumentData, EditDocumentError, EditDocumentErrors, EditDocumentResponse, EditDocumentResponse2, EditDocumentResponses, EditDocumentServiceArgs, EditPdfAnchorRequest, EditPdfPlaceableCommentAnchorRequest, EditProjectV2Data, EditProjectV2Error, EditProjectV2Errors, EditProjectV2Response, EditProjectV2Responses, EditThreadV2Data, EditThreadV2Error, EditThreadV2Errors, EditThreadV2Response, EditThreadV2Responses, EmailFilters, EmailFollowup, EmailFollowupCommand, EmailFollowupResponse, EmailReminderCondition, EmptyResponse, EnsureCollabSurfaceData, EnsureCollabSurfaceError, EnsureCollabSurfaceErrors, EnsureCollabSurfaceRequest, EnsureCollabSurfaceResponse, EnsureCollabSurfaceResponses, EntityFilters, EntityMessageCreateData, EntityMessageCreateResponse, EntityMessageCreateResponses, EntityMessageDeleteMessageData, EntityMessageDeleteMessageResponse, EntityMessageDeleteMessageResponses, EntityMessageDeleteThreadData, EntityMessageDeleteThreadResponse, EntityMessageDeleteThreadResponses, EntityMessageEditData, EntityMessageEditResponse, EntityMessageEditResponses, EntityMessageGetMessageData, EntityMessageGetMessageResponse, EntityMessageGetMessageResponses, EntityMessageGetThreadData, EntityMessageGetThreadResponse, EntityMessageGetThreadResponses, EntityMessageLegacyData, EntityMessageLegacyResponse, EntityMessageLegacyResponses, EntityMessagePatchThreadData, EntityMessagePatchThreadResponse, EntityMessagePatchThreadResponses, EntityMessageReactData, EntityMessageReactResponse, EntityMessageReactResponses, EntityMessageTypingData, EntityMessageTypingResponse, EntityMessageTypingResponses, EntityPermission, EntityPermissionResponse, EntityReference, EntityType, ErrorResponse, EventReminderOverride, EventReminders, EventStatus, EventTime, EventTransparency, EventType, EventVisibility, ExcludedDefaultView, ExcludeDefaultViewHandlerData, ExcludeDefaultViewHandlerError, ExcludeDefaultViewHandlerErrors, ExcludeDefaultViewHandlerResponses, ExcludeDefaultViewRequest, ExportDocumentData, ExportDocumentError, ExportDocumentErrors, ExportDocumentResponse, ExportDocumentResponse2, ExportDocumentResponses, Favorite, FavoriteEntityRef, FavoritesList, FileSystemNodeWithIds, FileType, FileTypeUpdate, FolderItem, FollowupState, ForeignEntity, ForeignEntityFilters, GenericErrorResponse, GenericResponse, GenericSuccessResponse, GetActiveCallsData, GetActiveCallsError, GetActiveCallsErrors, GetActiveCallsResponse, GetActiveCallsResponses, GetActivityData, GetActivityError, GetActivityErrors, GetActivityResponse, GetActivityResponses, GetAttachmentReferencesData, GetAttachmentReferencesError, GetAttachmentReferencesErrors, GetAttachmentReferencesResponse, GetAttachmentReferencesResponse2, GetAttachmentReferencesResponses, GetBatchCallRecordPreviewData, GetBatchCallRecordPreviewError, GetBatchCallRecordPreviewErrors, GetBatchCallRecordPreviewRequest, GetBatchCallRecordPreviewResponse, GetBatchCallRecordPreviewResponse2, GetBatchCallRecordPreviewResponses, GetBatchChannelPreviewData, GetBatchChannelPreviewError, GetBatchChannelPreviewErrors, GetBatchChannelPreviewRequest, GetBatchChannelPreviewResponse, GetBatchChannelPreviewResponse2, GetBatchChannelPreviewResponses, GetBatchPreviewHandlerData, GetBatchPreviewHandlerError, GetBatchPreviewHandlerErrors, GetBatchPreviewHandlerResponse, GetBatchPreviewHandlerResponses, GetBatchPreviewRequest, GetBatchPreviewResponse, GetBatchProjectPreviewData, GetBatchProjectPreviewError, GetBatchProjectPreviewErrors, GetBatchProjectPreviewRequest, GetBatchProjectPreviewResponse, GetBatchProjectPreviewResponse2, GetBatchProjectPreviewResponses, GetBotOwnerProfilesData, GetBotOwnerProfilesError, GetBotOwnerProfilesErrors, GetBotOwnerProfilesResponse, GetBotOwnerProfilesResponses, GetCallRecordData, GetCallRecordError, GetCallRecordErrors, GetCallRecordResponse, GetCallRecordResponses, GetChannelAttachmentsData, GetChannelAttachmentsError, GetChannelAttachmentsErrors, GetChannelAttachmentsResponse, GetChannelAttachmentsResponses, GetChannelData, GetChannelError, GetChannelErrors, GetChannelJoinLinkData, GetChannelJoinLinkError, GetChannelJoinLinkErrors, GetChannelJoinLinkResponse, GetChannelJoinLinkResponses, GetChannelParticipantsData, GetChannelParticipantsError, GetChannelParticipantsErrors, GetChannelParticipantsResponse, GetChannelParticipantsResponses, GetChannelResponse, GetChannelResponses, GetChannelsData, GetChannelsError, GetChannelsErrors, GetChannelsResponse, GetChannelsResponses, GetCollabSurfaceData, GetCollabSurfaceError, GetCollabSurfaceErrors, GetCollabSurfaceResponse, GetCollabSurfaceResponses, GetCompanyData, GetCompanyError, GetCompanyErrors, GetCompanyResponse, GetCompanyResponses, GetContactByEmailData, GetContactByEmailError, GetContactByEmailErrors, GetContactByEmailResponse, GetContactByEmailResponse2, GetContactByEmailResponses, GetContactData, GetContactError, GetContactErrors, GetContactResponse, GetContactResponses, GetCrmTeamSettingsData, GetCrmTeamSettingsError, GetCrmTeamSettingsErrors, GetCrmTeamSettingsResponse, GetCrmTeamSettingsResponses, GetDocumentAnchorsData, GetDocumentAnchorsError, GetDocumentAnchorsErrors, GetDocumentAnchorsResponse, GetDocumentAnchorsResponses, GetDocumentBranchNameData, GetDocumentBranchNameError, GetDocumentBranchNameErrors, GetDocumentBranchNameResponse, GetDocumentBranchNameResponses, GetDocumentByTeamSlugData, GetDocumentByTeamSlugError, GetDocumentByTeamSlugErrors, GetDocumentByTeamSlugResponse, GetDocumentByTeamSlugResponses, GetDocumentData, GetDocumentError, GetDocumentErrors, GetDocumentGithubPullRequestsData, GetDocumentGithubPullRequestsError, GetDocumentGithubPullRequestsErrors, GetDocumentGithubPullRequestsResponse, GetDocumentGithubPullRequestsResponses, GetDocumentKeyResponse, GetDocumentKeyResponseData, GetDocumentListHandlerData, GetDocumentListHandlerError, GetDocumentListHandlerErrors, GetDocumentListHandlerResponse, GetDocumentListHandlerResponses, GetDocumentListResult, GetDocumentLocationV3Data, GetDocumentLocationV3Error, GetDocumentLocationV3Errors, GetDocumentLocationV3Response, GetDocumentLocationV3Responses, GetDocumentPermissionsResponseDataV2, GetDocumentPermissionsTokenData, GetDocumentPermissionsTokenError, GetDocumentPermissionsTokenErrors, GetDocumentPermissionsTokenResponse, GetDocumentPermissionsTokenResponses, GetDocumentPermissionsV2Data, GetDocumentPermissionsV2Error, GetDocumentPermissionsV2Errors, GetDocumentPermissionsV2Response, GetDocumentPermissionsV2Responses, GetDocumentProcessingResult, GetDocumentProcessingResultData, GetDocumentProcessingResultError, GetDocumentProcessingResultErrors, GetDocumentProcessingResultResponse, GetDocumentProcessingResultResponse2, GetDocumentProcessingResultResponses, GetDocumentResponse, GetDocumentResponse2, GetDocumentResponseData, GetDocumentResponses, GetDocumentSearchResponse, GetDocumentShortIdData, GetDocumentShortIdError, GetDocumentShortIdErrors, GetDocumentShortIdResponse, GetDocumentShortIdResponses, GetDocumentsResponse, GetDocumentTeamShareData, GetDocumentTeamShareError, GetDocumentTeamShareErrors, GetDocumentTeamShareResponse, GetDocumentTeamShareResponses, GetDocumentUserAccessLevelResponse, GetDocumentVersionData, GetDocumentVersionError, GetDocumentVersionErrors, GetDocumentVersionResponse, GetDocumentVersionResponses, GetDocumentViewsHandlerData, GetDocumentViewsHandlerError, GetDocumentViewsHandlerErrors, GetDocumentViewsHandlerResponse, GetDocumentViewsHandlerResponses, GetEmailFollowupData, GetEmailFollowupError, GetEmailFollowupErrors, GetEmailFollowupResponse, GetEmailFollowupResponses, GetEntityPermissionData, GetEntityPermissionErrors, GetEntityPermissionResponse, GetEntityPermissionResponses, GetForeignEntityBySourceData, GetForeignEntityBySourceError, GetForeignEntityBySourceErrors, GetForeignEntityBySourceResponse, GetForeignEntityBySourceResponses, GetForeignEntityData, GetForeignEntityError, GetForeignEntityErrors, GetForeignEntityResponse, GetForeignEntityResponses, GetHarnessPairingData, GetHarnessPairingError, GetHarnessPairingErrors, GetHarnessPairingResponse, GetHarnessPairingResponses, GetHistoryHandlerData, GetHistoryHandlerError, GetHistoryHandlerErrors, GetHistoryHandlerResponse, GetHistoryHandlerResponses, GetInitiativeData, GetInitiativeError, GetInitiativeErrors, GetInitiativeResponse, GetInitiativeResponses, GetInstructionsDocumentResponse, GetInstructionsHandlerData, GetInstructionsHandlerError, GetInstructionsHandlerErrors, GetInstructionsHandlerResponse, GetInstructionsHandlerResponses, GetItemsSoupData, GetItemsSoupError, GetItemsSoupErrors, GetItemsSoupResponse, GetItemsSoupResponses, GetLocationHandlerData, GetLocationHandlerError, GetLocationHandlerErrors, GetLocationHandlerResponse, GetLocationHandlerResponses, GetOrCreateAction, GetOrCreateCallData, GetOrCreateCallError, GetOrCreateCallErrors, GetOrCreateCallResponse, GetOrCreateCallResponses, GetOrCreateChannelResponse, GetOrCreateDmData, GetOrCreateDmError, GetOrCreateDmErrors, GetOrCreateDmRequest, GetOrCreateDmResponse, GetOrCreateDmResponses, GetOrCreatePrivateData, GetOrCreatePrivateError, GetOrCreatePrivateErrors, GetOrCreatePrivateRequest, GetOrCreatePrivateResponse, GetOrCreatePrivateResponses, GetPendingProjectsHandlerData, GetPendingProjectsHandlerError, GetPendingProjectsHandlerErrors, GetPendingProjectsHandlerResponse, GetPendingProjectsHandlerResponses, GetPinsHandlerData, GetPinsHandlerError, GetPinsHandlerErrors, GetPinsHandlerResponse, GetPinsHandlerResponses, GetPinsResponse, GetProjectContentHandlerData, GetProjectContentHandlerError, GetProjectContentHandlerErrors, GetProjectContentHandlerResponse, GetProjectContentHandlerResponses, GetProjectContentResponse, GetProjectHandlerData, GetProjectHandlerError, GetProjectHandlerErrors, GetProjectHandlerResponse, GetProjectHandlerResponses, GetProjectPermissionsV2Data, GetProjectPermissionsV2Error, GetProjectPermissionsV2Errors, GetProjectPermissionsV2Response, GetProjectPermissionsV2Responses, GetProjectResponse, GetProjectResponseData, GetProjectsHandlerData, GetProjectsHandlerError, GetProjectsHandlerErrors, GetProjectsHandlerResponse, GetProjectsHandlerResponses, GetProjectsResponse, GetProjectUserAccessLevelData, GetProjectUserAccessLevelError, GetProjectUserAccessLevelErrors, GetProjectUserAccessLevelResponse, GetProjectUserAccessLevelResponses, GetReminderData, GetReminderError, GetReminderErrors, GetReminderResponse, GetReminderResponses, GetRingStatusData, GetRingStatusError, GetRingStatusErrors, GetRingStatusResponse, GetRingStatusResponses, GetSelfBotData, GetSelfBotError, GetSelfBotErrors, GetSelfBotResponse, GetSelfBotResponses, GetSelfHarnessData, GetSelfHarnessError, GetSelfHarnessErrors, GetSelfHarnessResponse, GetSelfHarnessResponses, GetSystemSkillsHandlerData, GetSystemSkillsHandlerError, GetSystemSkillsHandlerErrors, GetSystemSkillsHandlerResponse, GetSystemSkillsHandlerResponses, GetUserDocumentsHandlerData, GetUserDocumentsHandlerError, GetUserDocumentsHandlerErrors, GetUserDocumentsHandlerResponse, GetUserDocumentsHandlerResponses, GetUserDocumentViewLocationData, GetUserDocumentViewLocationError, GetUserDocumentViewLocationErrors, GetUserDocumentViewLocationResponse, GetUserDocumentViewLocationResponses, GetUserHistoryResponse, GetViewsHandlerData, GetViewsHandlerError, GetViewsHandlerErrors, GetViewsHandlerResponse, GetViewsHandlerResponses, GetWebhookData, GetWebhookError, GetWebhookErrors, GetWebhookResponse, GetWebhookResponses, GithubPullRequest, GithubPullRequestCheckRun, GithubPullRequestComment, GithubPullRequestsResponse, GroupedInitialParams, GroupedPageParams, GroupedSoupGroupPage, GroupedSoupInitialPage, GroupedSoupPage, GroupedSoupSort, GuestId, GuestJoinRequest, HandlerData, HandlerError, HandlerErrors, HandlerResponse, HandlerResponses, Harness, HarnessAgent, HarnessId, HarnessOwner, HarnessSession, HashMap, HealthHandlerData, HealthHandlerResponse, HealthHandlerResponses, HighlightType, ImportedAuthor, InFlightTurnSummary, IngestTranscriptData, IngestTranscriptError, IngestTranscriptErrors, IngestTranscriptResponses, InitializeUserDocumentsData, InitializeUserDocumentsError, InitializeUserDocumentsErrors, InitializeUserDocumentsResponse, InitializeUserDocumentsResponses, InitialPropertyValue, InitiativeDetail, InitiativeFilters, InitiativeId, InitiativeList, InitiativeSummary, InputReceivedMetadata, InstallSyncData, InstallSyncErrors, InteractionReason, InviteMeetingRequest, InviteMeetingUsersRequest, Item, ItemWithUserAccessLevel, JobProcessingResultHandlerData, JobProcessingResultHandlerError, JobProcessingResultHandlerErrors, JobProcessingResultHandlerResponse, JobProcessingResultHandlerResponses, JoinChannelByCodeData, JoinChannelByCodeError, JoinChannelByCodeErrors, JoinChannelByCodeResponses, JoinChannelData, JoinChannelError, JoinChannelErrors, JoinChannelResponses, LatestMessage, LeaveCallResponse, LeaveChannelData, LeaveChannelError, LeaveChannelErrors, LeaveChannelResponses, LeaveOrEndCallData, LeaveOrEndCallError, LeaveOrEndCallErrors, LeaveOrEndCallResponse, LeaveOrEndCallResponses, LinkShare, ListAgentsData, ListAgentsError, ListAgentsErrors, ListAgentsResponse, ListAgentsResponses, ListBotChannelsData, ListBotChannelsError, ListBotChannelsErrors, ListBotChannelsResponse, ListBotChannelsResponses, ListChannelLabelsData, ListChannelLabelsError, ListChannelLabelsErrors, ListChannelLabelsResponse, ListChannelLabelsResponses, ListCompanyContactsData, ListCompanyContactsError, ListCompanyContactsErrors, ListCompanyContactsResponse, ListCompanyContactsResponses, ListFavoritesData, ListFavoritesError, ListFavoritesErrors, ListFavoritesResponse, ListFavoritesResponses, ListHarnessAgentsData, ListHarnessAgentsError, ListHarnessAgentsErrors, ListHarnessAgentsResponse, ListHarnessAgentsResponses, ListHarnessesData, ListHarnessesError, ListHarnessesErrors, ListHarnessesResponse, ListHarnessesResponses, ListHarnessSessionsData, ListHarnessSessionsError, ListHarnessSessionsErrors, ListHarnessSessionsResponse, ListHarnessSessionsResponses, ListInitiativesData, ListInitiativesError, ListInitiativesErrors, ListInitiativesResponse, ListInitiativesResponses, ListOccurrencesData, ListOccurrencesErrors, ListOccurrencesResponse, ListOccurrencesResponses, ListReminderCollectionData, ListReminderCollectionError, ListReminderCollectionErrors, ListReminderCollectionResponse, ListReminderCollectionResponses, ListRemindersData, ListRemindersError, ListRemindersErrors, ListRemindersResponse, ListRemindersResponses, ListTeamOutOfOfficeData, ListTeamOutOfOfficeErrors, ListTeamOutOfOfficeResponse, ListTeamOutOfOfficeResponses, ListUserApiKeysData, ListUserApiKeysError, ListUserApiKeysErrors, ListUserApiKeysResponse, ListUserApiKeysResponses, ListWebhooksData, ListWebhooksError, ListWebhooksErrors, ListWebhooksResponse, ListWebhooksResponse2, ListWebhooksResponses, LocationResponseData, LocationResponseV3, MacroUserIdStr, Meeting, MeetingCancelData, MeetingCancelError, MeetingCancelErrors, MeetingCancelPreparationData, MeetingCancelPreparationError, MeetingCancelPreparationErrors, MeetingCancelPreparationResponse, MeetingCancelPreparationResponses, MeetingCancelResponse, MeetingCancelResponses, MeetingCreateData, MeetingCreateError, MeetingCreateErrors, MeetingCreateResponse, MeetingCreateResponses, MeetingGuestJoinData, MeetingGuestJoinError, MeetingGuestJoinErrors, MeetingGuestJoinResponse, MeetingGuestJoinResponses, MeetingGuestParticipantsData, MeetingGuestParticipantsError, MeetingGuestParticipantsErrors, MeetingGuestParticipantsResponse, MeetingGuestParticipantsResponses, MeetingInviteData, MeetingInviteError, MeetingInviteErrors, MeetingInvitePermissions, MeetingInvitePermissionsData, MeetingInvitePermissionsError, MeetingInvitePermissionsErrors, MeetingInvitePermissionsResponse, MeetingInvitePermissionsResponses, MeetingInviteResponse, MeetingInviteResponses, MeetingInviteUsersData, MeetingInviteUsersError, MeetingInviteUsersErrors, MeetingInviteUsersResponse, MeetingInviteUsersResponses, MeetingJoinData, MeetingJoinError, MeetingJoinErrors, MeetingJoinResponse, MeetingJoinResponses, MeetingLeaveData, MeetingLeaveError, MeetingLeaveErrors, MeetingLeaveResponse, MeetingLeaveResponses, MeetingListActiveData, MeetingListActiveError, MeetingListActiveErrors, MeetingListActiveResponse, MeetingListActiveResponses, MeetingListData, MeetingListError, MeetingListErrors, MeetingListResponse, MeetingListResponses, MeetingLookupData, MeetingLookupError, MeetingLookupErrors, MeetingLookupResponse, MeetingLookupResponses, MeetingParticipant, MeetingParticipants, MeetingParticipantsData, MeetingParticipantsError, MeetingParticipantsErrors, MeetingParticipantsResponse, MeetingParticipantsResponses, MeetingPreparation, MeetingPrepareData, MeetingPrepareError, MeetingPrepareErrors, MeetingPrepareResponse, MeetingPrepareResponses, MeetingShareData, MeetingShareError, MeetingShareErrors, MeetingShareResponse, MeetingShareResponses, MeetingsResponse, MeetingToken, MeetingUpdateData, MeetingUpdateError, MeetingUpdateErrors, MeetingUpdateResponse, MeetingUpdateResponses, MentionPreviewsData, MentionPreviewsErrors, MentionPreviewsResponse, MentionPreviewsResponses, Message, MessageAttachment, MessageAttachmentCreatedMetadata, MessageAttachmentRemovedMetadata, MessageChange, MessageCursor, MessageDeletedMetadata, MessageDirection, MessageEvent, MessageEventAttachment, MessageListItem, MessageMentionedMetadata, MessagePage, MessageParent, MessagePatch, MessagePatchedMetadata, MessagePostedMetadata, MessageThread, MessageThreadPreview, MessageTimelineData, MessageTimelineQuery, MessageTimelineResponse, MessageTimelineResponses, MessageTopicEvent, NewAttachment, NewThreadAnchor, NotificationFilters, NotificationState, PairingDetails, Params, ParticipantRole, PatchBotRequest, PatchChannelData, PatchChannelError, PatchChannelErrors, PatchChannelRequest, PatchChannelResponse, PatchChannelResponses, PatchMessageNotificationPolicy, PatchProjectRequestV2, PatchThreadRequestV2, PatchViewHandlerData, PatchViewHandlerError, PatchViewHandlerErrors, PatchViewHandlerResponses, PatchWebhookData, PatchWebhookError, PatchWebhookErrors, PatchWebhookRequest, PatchWebhookResponse, PatchWebhookResponses, PdfAnchor, PdfAnchorId, PdfHighlightAnchor, PdfHighlightAnchorRequest, PdfHighlightRect, PdfHighlightRectAnchorRequest, PdfPlaceableCommentAnchor, PendingClaimResponse, PermanentlyDeleteDocumentData, PermanentlyDeleteDocumentError, PermanentlyDeleteDocumentErrors, PermanentlyDeleteDocumentResponse, PermanentlyDeleteDocumentResponses, PermanentlyDeleteProjectData, PermanentlyDeleteProjectError, PermanentlyDeleteProjectErrors, PermanentlyDeleteProjectResponse, PermanentlyDeleteProjectResponses, PinnedItem, PinRequest, PostActivityData, PostActivityError, PostActivityErrors, PostActivityRequest, PostActivityResponse, PostActivityResponses, PostChannelBotWebhookData, PostChannelBotWebhookError, PostChannelBotWebhookErrors, PostChannelBotWebhookResponse, PostChannelBotWebhookResponses, PostGroupedSoupAstGroupPageRequest, PostGroupedSoupAstInitialRequest, PostGroupedSoupAstRequest, PostItemsSoupAstData, PostItemsSoupAstError, PostItemsSoupAstErrors, PostItemsSoupAstGroupedData, PostItemsSoupAstGroupedError, PostItemsSoupAstGroupedErrors, PostItemsSoupAstGroupedResponse, PostItemsSoupAstGroupedResponses, PostItemsSoupAstResponse, PostItemsSoupAstResponses, PostItemsSoupData, PostItemsSoupError, PostItemsSoupErrors, PostItemsSoupResponse, PostItemsSoupResponses, PostMessage, PostMessageNotificationPolicy, PostSoupAstRequest, PostSoupRequest, PresaveDocumentHandlerData, PresaveDocumentHandlerError, PresaveDocumentHandlerErrors, PresaveDocumentHandlerResponse, PresaveDocumentHandlerResponses, PreSaveDocumentRequest, PreSaveDocumentResponse, PreSaveDocumentResponseData, PresignedUrl, PreviewSmartTagData, PreviewSmartTagError, PreviewSmartTagErrors, PreviewSmartTagResponse, PreviewSmartTagResponses, Project, ProjectDeleteResponseData, ProjectDocumentStatus, ProjectFilters, ProjectPreview, ProjectPreviewData, PropertyDefinition, PropertyFilter, PropertyInput, PropertyOwner, PropertyValue, PutCrmTeamSettingsData, PutCrmTeamSettingsError, PutCrmTeamSettingsErrors, PutCrmTeamSettingsResponse, PutCrmTeamSettingsResponses, PutCrmTeamStagesData, PutCrmTeamStagesError, PutCrmTeamStagesErrors, PutCrmTeamStagesResponse, PutCrmTeamStagesResponses, ReactionInput, RecentlyDeletedData, RecentlyDeletedError, RecentlyDeletedErrors, RecentlyDeletedResponse, RecentlyDeletedResponseData, RecentlyDeletedResponses, Reminder, ReminderCollectionPage, ReminderCollectionRow, ReminderFilters, ReminderReference, ReminderSchedule, RemindersList, RemoveBotFromChannelByBotData, RemoveBotFromChannelByBotError, RemoveBotFromChannelByBotErrors, RemoveBotFromChannelByBotResponse, RemoveBotFromChannelByBotResponses, RemoveFavoriteByEntityData, RemoveFavoriteByEntityError, RemoveFavoriteByEntityErrors, RemoveFavoriteByEntityResponses, RemoveParticipantsData, RemoveParticipantsError, RemoveParticipantsErrors, RemoveParticipantsRequest, RemoveParticipantsResponses, RemovePinHandlerData, RemovePinHandlerError, RemovePinHandlerErrors, RemovePinHandlerResponse, RemovePinHandlerResponses, RenameChannelLabelData, RenameChannelLabelError, RenameChannelLabelErrors, RenameChannelLabelRequest, RenameChannelLabelResponse, RenameChannelLabelResponses, ReorderFavoritesData, ReorderFavoritesError, ReorderFavoritesErrors, ReorderFavoritesRequest, ReorderFavoritesResponses, ReorderPinRequest, ReorderPinsHandlerData, ReorderPinsHandlerError, ReorderPinsHandlerErrors, ReorderPinsHandlerResponse, ReorderPinsHandlerResponses, ReplaceCrmStagesRequest, RequestedHarnessScope, ResetCrmTeamStagesData, ResetCrmTeamStagesError, ResetCrmTeamStagesErrors, ResetCrmTeamStagesResponse, ResetCrmTeamStagesResponses, RevertDeleteDocumentData, RevertDeleteDocumentError, RevertDeleteDocumentErrors, RevertDeleteDocumentResponse, RevertDeleteDocumentResponses, RevertDeleteProjectData, RevertDeleteProjectError, RevertDeleteProjectErrors, RevertDeleteProjectResponse, RevertDeleteProjectResponses, RingStatus, RingStatusResponse, S3ObjectInfo, SaveBomPart, SaveDocumentHandlerData, SaveDocumentHandlerError, SaveDocumentHandlerErrors, SaveDocumentHandlerResponse, SaveDocumentHandlerResponses, SaveDocumentRequest, SaveDocumentResponse, SaveDocumentResponseData, SearchContactsData, SearchContactsError, SearchContactsErrors, SearchContactsResponse, SearchContactsResponse2, SearchContactsResponses, SessionDeletedMetadata, SessionIdentity, SessionMentionedMetadata, SessionOpenedMetadata, SessionRenamedMetadata, SessionSettledMetadata, SessionStoppedMetadata, SetChannelLabelData, SetChannelLabelError, SetChannelLabelErrors, SetChannelLabelRequest, SetChannelLabelResponse, SetChannelLabelResponses, SetChannelPictureData, SetChannelPictureError, SetChannelPictureErrors, SetChannelPictureRequest, SetChannelPictureResponse, SetChannelPictureResponses, SetCompanyHiddenData, SetCompanyHiddenError, SetCompanyHiddenErrors, SetCompanyHiddenRequest, SetCompanyHiddenResponse, SetCompanyHiddenResponses, SetCompanyNameRequest, SetContactHiddenData, SetContactHiddenError, SetContactHiddenErrors, SetContactHiddenRequest, SetContactHiddenResponse, SetContactHiddenResponses, SetContactNameRequest, SetCrmCompanyNameData, SetCrmCompanyNameError, SetCrmCompanyNameErrors, SetCrmCompanyNameResponse, SetCrmCompanyNameResponses, SetCrmContactNameData, SetCrmContactNameError, SetCrmContactNameErrors, SetCrmContactNameResponse, SetCrmContactNameResponses, SetDocumentTeamShareData, SetDocumentTeamShareError, SetDocumentTeamShareErrors, SetDocumentTeamShareRequest, SetDocumentTeamShareResponse, SetDocumentTeamShareResponses, SetEmailFollowupData, SetEmailFollowupError, SetEmailFollowupErrors, SetEmailFollowupResponse, SetEmailFollowupResponses, SetEmailSyncData, SetEmailSyncError, SetEmailSyncErrors, SetEmailSyncRequest, SetEmailSyncResponse, SetEmailSyncResponses, SetPropertyValue, SharedEmailFilter, SharePermissionV2, ShortIdResponse, SimpleMention, SimpleSaveData, SimpleSaveError, SimpleSaveErrors, SimpleSaveResponse, SimpleSaveResponses, SmartTagChannelMatch, SmartTagPreview, SoupAgentSessionSoupPropertiesField, SoupApiItem, SoupApiSort, SoupApiSortDirection, SoupAttachment, SoupCalendarEventSoupPropertiesField, SoupCalendarEventTime, SoupCallRecordGuest, SoupCallRecordParticipant, SoupCallRecordSoupPropertiesField, SoupChannel, SoupChannelThread, SoupChatSoupPropertiesField, SoupContact, SoupCountedReaction, SoupCrmCompanySoupPropertiesField, SoupCrmDomain, SoupDocumentSoupPropertiesField, SoupDocumentSubType, SoupEmailThreadPreview, SoupEnrichedEmailThreadPreviewSoupPropertiesField, SoupForeignEntity, SoupInitiativeSoupPropertiesField, SoupItem, SoupLabel, SoupLabelListVisibility, SoupLabelType, SoupMessageAttachment, SoupMessageListVisibility, SoupMessageSender, SoupMessageSenderType, SoupPage, SoupProjectSoupPropertiesField, SoupPropertiesField, SoupProperty, SoupReminderReference, SoupReminderSchedule, SoupReminderSoupPropertiesField, SoupThreadInfo, SoupThreadReply, StarterDocumentsResponse, StreamEventsData, StreamEventsError, StreamEventsErrors, StreamEventsResponse, StreamEventsResponses, String, SuccessResponse, SurfaceState, SyncServiceVersionId, SystemSkillSummary, TagFilterMode, TaskFilters, TeamOutOfOfficeItem, TeamOutOfOfficeResponse, TeamRole, ThreadAnchor, ThreadOrigin, ThreadPatch, ThreadState, ToggleShareWithTeamData, ToggleShareWithTeamError, ToggleShareWithTeamErrors, ToggleShareWithTeamResponse, ToggleShareWithTeamResponses, TranscribeDictationData, TranscribeDictationError, TranscribeDictationErrors, TranscribeDictationResponse, TranscribeDictationResponses, TranscribeResponse, TranscriptSegmentRequest, TurnEndedMetadata, TurnStartedMetadata, TurnSummary, TypedSuccessResponse, TypedSuccessResponseGetDocumentResponseData, TypingInput, UnassignInitiativeTaskData, UnassignInitiativeTaskError, UnassignInitiativeTaskErrors, UnassignInitiativeTaskResponse, UnassignInitiativeTaskResponses, UpdateAgentData, UpdateAgentError, UpdateAgentErrors, UpdateAgentRequest, UpdateAgentResponse, UpdateAgentResponses, UpdateChannelSharePermission, UpdateCrmTeamSettingsRequest, UpdateInitiativeData, UpdateInitiativeError, UpdateInitiativeErrors, UpdateInitiativeRequest, UpdateInitiativeResponse, UpdateInitiativeResponses, UpdateMeetingRequest, UpdateOperation, UpdateReminderData, UpdateReminderError, UpdateReminderErrors, UpdateReminderRequest, UpdateReminderResponse, UpdateReminderResponses, UpdateSharePermissionRequestV2, UploadDocumentStatus, UploadExtractFolderHandlerData, UploadExtractFolderHandlerError, UploadExtractFolderHandlerErrors, UploadExtractFolderHandlerResponse, UploadExtractFolderHandlerResponses, UploadExtractFolderRequest, UploadFolderHandlerData, UploadFolderHandlerError, UploadFolderHandlerErrors, UploadFolderHandlerResponse, UploadFolderHandlerResponses, UploadFolderRequest, UpsertHistoryHandlerData, UpsertHistoryHandlerError, UpsertHistoryHandlerErrors, UpsertHistoryHandlerResponse, UpsertHistoryHandlerResponses, UpsertUserDocumentViewLocationData, UpsertUserDocumentViewLocationError, UpsertUserDocumentViewLocationErrors, UpsertUserDocumentViewLocationRequest, UpsertUserDocumentViewLocationResponse, UpsertUserDocumentViewLocationResponses, UserApiKeyId, UserApiKeyInfo, UserApiKeysList, UserDocumentsResponse, UserDocumentViewLocation, UserDocumentViewLocationResponse, UserPinsResponse, UserViewsResponse, ValidateDocumentPermissionsTokenData, ValidateDocumentPermissionsTokenError, ValidateDocumentPermissionsTokenErrors, ValidateDocumentPermissionsTokenResponse, ValidateDocumentPermissionsTokenResponses, ValidateWebhookData, ValidateWebhookError, ValidateWebhookErrors, ValidateWebhookResponse, ValidateWebhookResponse2, ValidateWebhookResponses, Vec, View, ViewPatch, ViewsResponse, WaitingForInputMetadata, Webhook, WebhookEvent, WebhookFilter, WebhookScope, WebhookStatus, WebhookValidationTestEvent, WithCallId, WithChannelId, WithDocumentId, WithProjectId } from './types.gen';
+export type { AccessLevel, ActiveCallsResponse, ActiveCallSummary, ActiveMeeting, ActiveMeetingsResponse, ActivityType, AddFavoriteData, AddFavoriteError, AddFavoriteErrors, AddFavoriteRequest, AddFavoriteResponse, AddFavoriteResponses, AddParticipantsData, AddParticipantsError, AddParticipantsErrors, AddParticipantsRequest, AddParticipantsResponses, AddPinHandlerData, AddPinHandlerError, AddPinHandlerErrors, AddPinHandlerResponse, AddPinHandlerResponses, AddPinRequest, Agent, AgentActionId, AgentChannelScope, AgentMcpServer, AgentMcpServers, AgentPullRequestState, AgentSessionFilters, AgentSessionLifecycleEvent, Anchor, AnchorId, AnchorResponse, AnnotationIncrementalUpdate, ApiActivity, ApiAttachmentChannelReference, ApiAttachmentEntityReference, ApiAttachmentGenericReference, ApiChannelAttachment, ApiChannelAttachmentsPage, ApiChannelDetail, ApiChannelListMessage, ApiChannelListPage, ApiChannelListParticipant, ApiChannelListType, ApiChannelParticipant, ApiChannelWithLatest, ApiEntityFilterAst, ApiGroupByField, ApiGroupMeta, ApiParticipantListRole, ApiParticipantRole, ApiPropertyEntityType, ApproveHarnessPairingData, ApproveHarnessPairingError, ApproveHarnessPairingErrors, ApproveHarnessPairingResponse, ApproveHarnessPairingResponses, ApprovePairingRequest, AssignInitiativeTasksData, AssignInitiativeTasksError, AssignInitiativeTasksErrors, AssignInitiativeTasksResponse, AssignInitiativeTasksResponses, AssignTasksRequest, AssignTasksResponse, AssignTasksResult, AssignTaskStatus, AttachmentChange, AttendeeResponseStatus, BasicDocument, BasicDocumentSubType, BomPart, Bot, BotChannel, BotChannelType, BotId, BotKind, BotOwner, BotOwnerProfile, BotSenderProfile, BotToken, BranchNameResponse, BTreeMap, BulkWakeupRequest, BulkWakeupResponse, BulkWakeupSyncServiceDocumentsData, BulkWakeupSyncServiceDocumentsErrors, BulkWakeupSyncServiceDocumentsResponse, BulkWakeupSyncServiceDocumentsResponses, CalendarAttendee, CalendarEvent, CalendarEventFilters, CalendarEventSourceContent, CalendarMentionEvent, CalendarMentionPreviewItem, CalendarMentionPreviewKind, CalendarMentionPreviewRequest, CalendarMentionPreviewRequestItem, CalendarMentionPreviewResponse, CalendarOccurrence, CalendarOccurrenceItem, CalendarOccurrenceResponse, CalendarSyncStatus, CallActiveResponse, CallFilters, CallRecord, CallRecordGuest, CallRecordParticipant, CallRecordPreview, CallRecordPreviewData, CallRecordTranscriptSegment, CallStatus, CallTokenResponse, CallWebhookData, CallWebhookError, CallWebhookErrors, CallWebhookResponses, CancelSlackImportData, CancelSlackImportErrors, CancelSlackImportResponse, CancelSlackImportResponses, Channel, ChannelCreatedMetadata, ChannelDeletedMetadata, ChannelFilters, ChannelJoinCodeResponse, ChannelLabel, ChannelLabelRule, ChannelLabelsList, ChannelMessage, ChannelParticipant, ChannelParticipantAddedMetadata, ChannelParticipantRemovedMetadata, ChannelPreview, ChannelPreviewData, ChannelSender, ChannelSharePermission, ChannelThreadFilters, ChannelTopicEvent, ChannelType, ChannelUpdatedMetadata, ChannelWebhookRequest, ChannelWebhookResponse, ChannelWithParticipants, Chat, ChatFilters, CheckActiveCallData, CheckActiveCallError, CheckActiveCallErrors, CheckActiveCallResponse, CheckActiveCallResponses, ClaimedPairing, ClaimHarnessPairingData, ClaimHarnessPairingError, ClaimHarnessPairingErrors, ClaimHarnessPairingResponse, ClaimHarnessPairingResponses, ClaimPairingRequest, ClientOptions, CloudStorageItemType, CollabSurfaceResponse, CollabSurfaceTokenResponse, CommandFailure, CommandRejectedMetadata, CompleteSlackImportUploadsData, CompleteSlackImportUploadsErrors, CompleteSlackImportUploadsResponse, CompleteSlackImportUploadsResponses, CompleteUploads, ConferenceProvider, ConversationId, ConversationKind, ConversationMetadata, ConversationProgress, ConversationSeal, ConversationStatus, CopyDocumentData, CopyDocumentError, CopyDocumentErrors, CopyDocumentQueryParams, CopyDocumentRequest, CopyDocumentResponse, CopyDocumentResponse2, CopyDocumentResponses, CountedReaction, CreateAgentData, CreateAgentError, CreateAgentErrors, CreateAgentRequest, CreateAgentResponse, CreateAgentResponses, CreateAnchorData, CreateAnchorError, CreateAnchorErrors, CreateAnchorResponse, CreateAnchorResponses, CreateBotRequest, CreateBulkDocumentResponse, CreateBulkDocumentResponseData, CreateChannelData, CreateChannelError, CreateChannelErrors, CreateChannelLabelData, CreateChannelLabelError, CreateChannelLabelErrors, CreateChannelLabelRequest, CreateChannelLabelResponse, CreateChannelLabelResponses, CreateChannelRequest, CreateChannelResponse, CreateChannelResponse2, CreateChannelResponses, CreateChannelScopedBotData, CreateChannelScopedBotError, CreateChannelScopedBotErrors, CreateChannelScopedBotRequest, CreateChannelScopedBotResponse, CreateChannelScopedBotResponse2, CreateChannelScopedBotResponses, CreateCollabSurfaceTokenData, CreateCollabSurfaceTokenError, CreateCollabSurfaceTokenErrors, CreateCollabSurfaceTokenResponse, CreateCollabSurfaceTokenResponses, CreateCrmCompanyData, CreateCrmCompanyError, CreateCrmCompanyErrors, CreateCrmCompanyRequest, CreateCrmCompanyResponse, CreateCrmCompanyResponses, CreateCrmContactData, CreateCrmContactError, CreateCrmContactErrors, CreateCrmContactRequest, CreateCrmContactResponse, CreateCrmContactResponses, CreateDocumentData, CreateDocumentError, CreateDocumentErrors, CreateDocumentRequest, CreateDocumentResponse, CreateDocumentResponseData, CreateDocumentResponses, CreatedPairing, CreatedUserApiKey, CreateEntityMentionData, CreateEntityMentionError, CreateEntityMentionErrors, CreateEntityMentionRequest, CreateEntityMentionResponse, CreateEntityMentionResponse2, CreateEntityMentionResponses, CreateHarnessPairingData, CreateHarnessPairingError, CreateHarnessPairingErrors, CreateHarnessPairingResponse, CreateHarnessPairingResponses, CreateImport, CreateInitiativeData, CreateInitiativeError, CreateInitiativeErrors, CreateInitiativeRequest, CreateInitiativeResponse, CreateInitiativeResponses, CreateInstructionsDocumentResponse, CreateInstructionsHandlerData, CreateInstructionsHandlerError, CreateInstructionsHandlerErrors, CreateInstructionsHandlerResponse, CreateInstructionsHandlerResponses, CreateMarkdownDocumentRequest, CreateMarkdownDocumentResponse, CreateMarkdownHandlerData, CreateMarkdownHandlerError, CreateMarkdownHandlerErrors, CreateMarkdownHandlerResponse, CreateMarkdownHandlerResponses, CreateMeetingRequest, CreatePairingRequest, CreateProjectHandlerData, CreateProjectHandlerError, CreateProjectHandlerErrors, CreateProjectHandlerResponse, CreateProjectHandlerResponses, CreateProjectRequest, CreateProjectResponse, CreateReminderData, CreateReminderError, CreateReminderErrors, CreateReminderRequest, CreateReminderResponse, CreateReminderResponses, CreateSkillHandlerData, CreateSkillHandlerError, CreateSkillHandlerErrors, CreateSkillHandlerResponse, CreateSkillHandlerResponses, CreateSkillRequest, CreateSkillResponse, CreateSlackImportData, CreateSlackImportErrors, CreateSlackImportResponse, CreateSlackImportResponses, CreateSnippetHandlerData, CreateSnippetHandlerError, CreateSnippetHandlerErrors, CreateSnippetHandlerResponse, CreateSnippetHandlerResponses, CreateSnippetRequest, CreateSnippetResponse, CreateTaskHandlerData, CreateTaskHandlerError, CreateTaskHandlerErrors, CreateTaskHandlerResponse, CreateTaskHandlerResponses, CreateTaskRequest, CreateTaskResponse, CreateToken, CreateUnthreadedAnchorRequest, CreateUnthreadedAnchorResponse, CreateUnthreadedPdfAnchorRequest, CreateUserApiKeyData, CreateUserApiKeyError, CreateUserApiKeyErrors, CreateUserApiKeyRequest, CreateUserApiKeyResponse, CreateUserApiKeyResponses, CreateViewHandlerData, CreateViewHandlerError, CreateViewHandlerErrors, CreateViewHandlerResponse, CreateViewHandlerResponses, CreateViewRequest, CreateWebhookData, CreateWebhookError, CreateWebhookErrors, CreateWebhookRequest, CreateWebhookResponse, CreateWebhookResponse2, CreateWebhookResponses, CrmCompanyFilters, CrmCompanyResponse, CrmContactResponse, CrmDomainResponse, CrmPermissionRole, CrmStageInput, CrmStageResponse, CrmStagesResponse, CrmTeamSettingsResponse, CustomSpeakerAssignment, DataType, DeleteAnchorData, DeleteAnchorError, DeleteAnchorErrors, DeleteAnchorResponse, DeleteAnchorResponses, DeleteCallRecordData, DeleteCallRecordError, DeleteCallRecordErrors, DeleteCallRecordResponse, DeleteCallRecordResponses, DeleteChannelData, DeleteChannelError, DeleteChannelErrors, DeleteChannelLabelData, DeleteChannelLabelError, DeleteChannelLabelErrors, DeleteChannelLabelResponse, DeleteChannelLabelResponses, DeleteChannelResponse, DeleteChannelResponses, DeleteCollabSurfaceData, DeleteCollabSurfaceError, DeleteCollabSurfaceErrors, DeleteCollabSurfaceResponse, DeleteCollabSurfaceResponses, DeleteDocumentData, DeleteDocumentError, DeleteDocumentErrors, DeleteDocumentResponse, DeleteDocumentResponses, DeleteEntityMentionData, DeleteEntityMentionError, DeleteEntityMentionErrors, DeleteEntityMentionResponse, DeleteEntityMentionResponse2, DeleteEntityMentionResponses, DeleteHarnessData, DeleteHarnessError, DeleteHarnessErrors, DeleteHarnessResponse, DeleteHarnessResponses, DeleteHistoryHandlerData, DeleteHistoryHandlerError, DeleteHistoryHandlerErrors, DeleteHistoryHandlerResponse, DeleteHistoryHandlerResponses, DeleteInitiativeData, DeleteInitiativeError, DeleteInitiativeErrors, DeleteInitiativeResponse, DeleteInitiativeResponses, DeleteProjectHandlerData, DeleteProjectHandlerError, DeleteProjectHandlerErrors, DeleteProjectHandlerResponse, DeleteProjectHandlerResponses, DeleteReminderData, DeleteReminderError, DeleteReminderErrors, DeleteReminderResponse, DeleteReminderResponses, DeleteSelfHarnessData, DeleteSelfHarnessError, DeleteSelfHarnessErrors, DeleteSelfHarnessResponse, DeleteSelfHarnessResponses, DeleteUnthreadedAnchorRequest, DeleteUnthreadedAnchorResponse, DeleteUnthreadedPdfAnchorRequest, DeleteUserApiKeyData, DeleteUserApiKeyError, DeleteUserApiKeyErrors, DeleteUserApiKeyResponse, DeleteUserApiKeyResponses, DeleteUserDocumentViewLocationData, DeleteUserDocumentViewLocationError, DeleteUserDocumentViewLocationErrors, DeleteUserDocumentViewLocationResponse, DeleteUserDocumentViewLocationResponses, DeleteViewHandlerData, DeleteViewHandlerError, DeleteViewHandlerErrors, DeleteViewHandlerResponses, DeleteWebhookData, DeleteWebhookError, DeleteWebhookErrors, DeleteWebhookResponse, DeleteWebhookResponses, DescriptionDocumentId, DocumentBasic, DocumentContent, DocumentContentLocation, DocumentContentState, DocumentContentUploadedMetadata, DocumentCopiedMetadata, DocumentCreatedMetadata, DocumentDeletedMetadata, DocumentFilters, DocumentId, DocumentInteractionMetadata, DocumentMetadata, DocumentMetadataWithContent, DocumentPermissionsToken, DocumentPermissionsTokenRequest, DocumentPermissionsTokenResponse, DocumentPreview, DocumentPreviewData, DocumentPreviewDataSubType, DocumentPurgedMetadata, DocumentResponse, DocumentResponseMetadata, DocumentResponseMetadataWithContent, DocumentStorageServiceApiVersion, DocumentSubType, DocumentSyncContentUpdatedMetadata, DocumentSyncEditor, DocumentTeamShareResponse, DocumentTopicEvent, DocumentUpdatedMetadata, EditAnchorData, EditAnchorError, EditAnchorErrors, EditAnchorRequest, EditAnchorResponse, EditAnchorResponse2, EditAnchorResponses, EditCallRecordData, EditCallRecordError, EditCallRecordErrors, EditCallRecordRequest, EditCallRecordResponse, EditCallRecordResponses, EditCallTranscriptData, EditCallTranscriptError, EditCallTranscriptErrors, EditCallTranscriptRequest, EditCallTranscriptResponse, EditCallTranscriptResponses, EditDocumentData, EditDocumentError, EditDocumentErrors, EditDocumentResponse, EditDocumentResponse2, EditDocumentResponses, EditDocumentServiceArgs, EditPdfAnchorRequest, EditPdfPlaceableCommentAnchorRequest, EditProjectV2Data, EditProjectV2Error, EditProjectV2Errors, EditProjectV2Response, EditProjectV2Responses, EditThreadV2Data, EditThreadV2Error, EditThreadV2Errors, EditThreadV2Response, EditThreadV2Responses, EmailFilters, EmailFollowup, EmailFollowupCommand, EmailFollowupResponse, EmailReminderCondition, EmptyResponse, EnsureCollabSurfaceData, EnsureCollabSurfaceError, EnsureCollabSurfaceErrors, EnsureCollabSurfaceRequest, EnsureCollabSurfaceResponse, EnsureCollabSurfaceResponses, EntityFilters, EntityMessageCreateData, EntityMessageCreateResponse, EntityMessageCreateResponses, EntityMessageDeleteMessageData, EntityMessageDeleteMessageResponse, EntityMessageDeleteMessageResponses, EntityMessageDeleteThreadData, EntityMessageDeleteThreadResponse, EntityMessageDeleteThreadResponses, EntityMessageEditData, EntityMessageEditResponse, EntityMessageEditResponses, EntityMessageGetMessageData, EntityMessageGetMessageResponse, EntityMessageGetMessageResponses, EntityMessageGetThreadData, EntityMessageGetThreadResponse, EntityMessageGetThreadResponses, EntityMessageLegacyData, EntityMessageLegacyResponse, EntityMessageLegacyResponses, EntityMessagePatchThreadData, EntityMessagePatchThreadResponse, EntityMessagePatchThreadResponses, EntityMessageReactData, EntityMessageReactResponse, EntityMessageReactResponses, EntityMessageTypingData, EntityMessageTypingResponse, EntityMessageTypingResponses, EntityPermission, EntityPermissionResponse, EntityReference, EntityType, ErrorResponse, EventReminderOverride, EventReminders, EventStatus, EventTime, EventTransparency, EventType, EventVisibility, ExcludedDefaultView, ExcludeDefaultViewHandlerData, ExcludeDefaultViewHandlerError, ExcludeDefaultViewHandlerErrors, ExcludeDefaultViewHandlerResponses, ExcludeDefaultViewRequest, ExportDocumentData, ExportDocumentError, ExportDocumentErrors, ExportDocumentResponse, ExportDocumentResponse2, ExportDocumentResponses, Favorite, FavoriteEntityRef, FavoritesList, FileSystemNodeWithIds, FileType, FileTypeUpdate, FinalizeSlackImportData, FinalizeSlackImportErrors, FinalizeSlackImportResponse, FinalizeSlackImportResponses, FolderItem, FollowupState, ForeignEntity, ForeignEntityFilters, GenericErrorResponse, GenericResponse, GenericSuccessResponse, GetActiveCallsData, GetActiveCallsError, GetActiveCallsErrors, GetActiveCallsResponse, GetActiveCallsResponses, GetActivityData, GetActivityError, GetActivityErrors, GetActivityResponse, GetActivityResponses, GetAttachmentReferencesData, GetAttachmentReferencesError, GetAttachmentReferencesErrors, GetAttachmentReferencesResponse, GetAttachmentReferencesResponse2, GetAttachmentReferencesResponses, GetBatchCallRecordPreviewData, GetBatchCallRecordPreviewError, GetBatchCallRecordPreviewErrors, GetBatchCallRecordPreviewRequest, GetBatchCallRecordPreviewResponse, GetBatchCallRecordPreviewResponse2, GetBatchCallRecordPreviewResponses, GetBatchChannelPreviewData, GetBatchChannelPreviewError, GetBatchChannelPreviewErrors, GetBatchChannelPreviewRequest, GetBatchChannelPreviewResponse, GetBatchChannelPreviewResponse2, GetBatchChannelPreviewResponses, GetBatchPreviewHandlerData, GetBatchPreviewHandlerError, GetBatchPreviewHandlerErrors, GetBatchPreviewHandlerResponse, GetBatchPreviewHandlerResponses, GetBatchPreviewRequest, GetBatchPreviewResponse, GetBatchProjectPreviewData, GetBatchProjectPreviewError, GetBatchProjectPreviewErrors, GetBatchProjectPreviewRequest, GetBatchProjectPreviewResponse, GetBatchProjectPreviewResponse2, GetBatchProjectPreviewResponses, GetBotOwnerProfilesData, GetBotOwnerProfilesError, GetBotOwnerProfilesErrors, GetBotOwnerProfilesResponse, GetBotOwnerProfilesResponses, GetCallRecordData, GetCallRecordError, GetCallRecordErrors, GetCallRecordResponse, GetCallRecordResponses, GetChannelAttachmentsData, GetChannelAttachmentsError, GetChannelAttachmentsErrors, GetChannelAttachmentsResponse, GetChannelAttachmentsResponses, GetChannelData, GetChannelError, GetChannelErrors, GetChannelJoinLinkData, GetChannelJoinLinkError, GetChannelJoinLinkErrors, GetChannelJoinLinkResponse, GetChannelJoinLinkResponses, GetChannelParticipantsData, GetChannelParticipantsError, GetChannelParticipantsErrors, GetChannelParticipantsResponse, GetChannelParticipantsResponses, GetChannelResponse, GetChannelResponses, GetChannelsData, GetChannelsError, GetChannelsErrors, GetChannelsResponse, GetChannelsResponses, GetCollabSurfaceData, GetCollabSurfaceError, GetCollabSurfaceErrors, GetCollabSurfaceResponse, GetCollabSurfaceResponses, GetCompanyData, GetCompanyError, GetCompanyErrors, GetCompanyResponse, GetCompanyResponses, GetContactByEmailData, GetContactByEmailError, GetContactByEmailErrors, GetContactByEmailResponse, GetContactByEmailResponse2, GetContactByEmailResponses, GetContactData, GetContactError, GetContactErrors, GetContactResponse, GetContactResponses, GetCrmTeamSettingsData, GetCrmTeamSettingsError, GetCrmTeamSettingsErrors, GetCrmTeamSettingsResponse, GetCrmTeamSettingsResponses, GetDocumentAnchorsData, GetDocumentAnchorsError, GetDocumentAnchorsErrors, GetDocumentAnchorsResponse, GetDocumentAnchorsResponses, GetDocumentBranchNameData, GetDocumentBranchNameError, GetDocumentBranchNameErrors, GetDocumentBranchNameResponse, GetDocumentBranchNameResponses, GetDocumentByTeamSlugData, GetDocumentByTeamSlugError, GetDocumentByTeamSlugErrors, GetDocumentByTeamSlugResponse, GetDocumentByTeamSlugResponses, GetDocumentData, GetDocumentError, GetDocumentErrors, GetDocumentGithubPullRequestsData, GetDocumentGithubPullRequestsError, GetDocumentGithubPullRequestsErrors, GetDocumentGithubPullRequestsResponse, GetDocumentGithubPullRequestsResponses, GetDocumentKeyResponse, GetDocumentKeyResponseData, GetDocumentListHandlerData, GetDocumentListHandlerError, GetDocumentListHandlerErrors, GetDocumentListHandlerResponse, GetDocumentListHandlerResponses, GetDocumentListResult, GetDocumentLocationV3Data, GetDocumentLocationV3Error, GetDocumentLocationV3Errors, GetDocumentLocationV3Response, GetDocumentLocationV3Responses, GetDocumentPermissionsResponseDataV2, GetDocumentPermissionsTokenData, GetDocumentPermissionsTokenError, GetDocumentPermissionsTokenErrors, GetDocumentPermissionsTokenResponse, GetDocumentPermissionsTokenResponses, GetDocumentPermissionsV2Data, GetDocumentPermissionsV2Error, GetDocumentPermissionsV2Errors, GetDocumentPermissionsV2Response, GetDocumentPermissionsV2Responses, GetDocumentProcessingResult, GetDocumentProcessingResultData, GetDocumentProcessingResultError, GetDocumentProcessingResultErrors, GetDocumentProcessingResultResponse, GetDocumentProcessingResultResponse2, GetDocumentProcessingResultResponses, GetDocumentResponse, GetDocumentResponse2, GetDocumentResponseData, GetDocumentResponses, GetDocumentSearchResponse, GetDocumentShortIdData, GetDocumentShortIdError, GetDocumentShortIdErrors, GetDocumentShortIdResponse, GetDocumentShortIdResponses, GetDocumentsResponse, GetDocumentTeamShareData, GetDocumentTeamShareError, GetDocumentTeamShareErrors, GetDocumentTeamShareResponse, GetDocumentTeamShareResponses, GetDocumentUserAccessLevelResponse, GetDocumentVersionData, GetDocumentVersionError, GetDocumentVersionErrors, GetDocumentVersionResponse, GetDocumentVersionResponses, GetDocumentViewsHandlerData, GetDocumentViewsHandlerError, GetDocumentViewsHandlerErrors, GetDocumentViewsHandlerResponse, GetDocumentViewsHandlerResponses, GetEmailFollowupData, GetEmailFollowupError, GetEmailFollowupErrors, GetEmailFollowupResponse, GetEmailFollowupResponses, GetEntityPermissionData, GetEntityPermissionErrors, GetEntityPermissionResponse, GetEntityPermissionResponses, GetForeignEntityBySourceData, GetForeignEntityBySourceError, GetForeignEntityBySourceErrors, GetForeignEntityBySourceResponse, GetForeignEntityBySourceResponses, GetForeignEntityData, GetForeignEntityError, GetForeignEntityErrors, GetForeignEntityResponse, GetForeignEntityResponses, GetHarnessPairingData, GetHarnessPairingError, GetHarnessPairingErrors, GetHarnessPairingResponse, GetHarnessPairingResponses, GetHistoryHandlerData, GetHistoryHandlerError, GetHistoryHandlerErrors, GetHistoryHandlerResponse, GetHistoryHandlerResponses, GetInitiativeData, GetInitiativeError, GetInitiativeErrors, GetInitiativeResponse, GetInitiativeResponses, GetInstructionsDocumentResponse, GetInstructionsHandlerData, GetInstructionsHandlerError, GetInstructionsHandlerErrors, GetInstructionsHandlerResponse, GetInstructionsHandlerResponses, GetItemsSoupData, GetItemsSoupError, GetItemsSoupErrors, GetItemsSoupResponse, GetItemsSoupResponses, GetLocationHandlerData, GetLocationHandlerError, GetLocationHandlerErrors, GetLocationHandlerResponse, GetLocationHandlerResponses, GetOrCreateAction, GetOrCreateCallData, GetOrCreateCallError, GetOrCreateCallErrors, GetOrCreateCallResponse, GetOrCreateCallResponses, GetOrCreateChannelResponse, GetOrCreateDmData, GetOrCreateDmError, GetOrCreateDmErrors, GetOrCreateDmRequest, GetOrCreateDmResponse, GetOrCreateDmResponses, GetOrCreatePrivateData, GetOrCreatePrivateError, GetOrCreatePrivateErrors, GetOrCreatePrivateRequest, GetOrCreatePrivateResponse, GetOrCreatePrivateResponses, GetPendingProjectsHandlerData, GetPendingProjectsHandlerError, GetPendingProjectsHandlerErrors, GetPendingProjectsHandlerResponse, GetPendingProjectsHandlerResponses, GetPinsHandlerData, GetPinsHandlerError, GetPinsHandlerErrors, GetPinsHandlerResponse, GetPinsHandlerResponses, GetPinsResponse, GetProjectContentHandlerData, GetProjectContentHandlerError, GetProjectContentHandlerErrors, GetProjectContentHandlerResponse, GetProjectContentHandlerResponses, GetProjectContentResponse, GetProjectHandlerData, GetProjectHandlerError, GetProjectHandlerErrors, GetProjectHandlerResponse, GetProjectHandlerResponses, GetProjectPermissionsV2Data, GetProjectPermissionsV2Error, GetProjectPermissionsV2Errors, GetProjectPermissionsV2Response, GetProjectPermissionsV2Responses, GetProjectResponse, GetProjectResponseData, GetProjectsHandlerData, GetProjectsHandlerError, GetProjectsHandlerErrors, GetProjectsHandlerResponse, GetProjectsHandlerResponses, GetProjectsResponse, GetProjectUserAccessLevelData, GetProjectUserAccessLevelError, GetProjectUserAccessLevelErrors, GetProjectUserAccessLevelResponse, GetProjectUserAccessLevelResponses, GetReminderData, GetReminderError, GetReminderErrors, GetReminderResponse, GetReminderResponses, GetRingStatusData, GetRingStatusError, GetRingStatusErrors, GetRingStatusResponse, GetRingStatusResponses, GetSelfBotData, GetSelfBotError, GetSelfBotErrors, GetSelfBotResponse, GetSelfBotResponses, GetSelfHarnessData, GetSelfHarnessError, GetSelfHarnessErrors, GetSelfHarnessResponse, GetSelfHarnessResponses, GetSlackImportData, GetSlackImportErrors, GetSlackImportResponse, GetSlackImportResponses, GetSystemSkillsHandlerData, GetSystemSkillsHandlerError, GetSystemSkillsHandlerErrors, GetSystemSkillsHandlerResponse, GetSystemSkillsHandlerResponses, GetUserDocumentsHandlerData, GetUserDocumentsHandlerError, GetUserDocumentsHandlerErrors, GetUserDocumentsHandlerResponse, GetUserDocumentsHandlerResponses, GetUserDocumentViewLocationData, GetUserDocumentViewLocationError, GetUserDocumentViewLocationErrors, GetUserDocumentViewLocationResponse, GetUserDocumentViewLocationResponses, GetUserHistoryResponse, GetViewsHandlerData, GetViewsHandlerError, GetViewsHandlerErrors, GetViewsHandlerResponse, GetViewsHandlerResponses, GetWebhookData, GetWebhookError, GetWebhookErrors, GetWebhookResponse, GetWebhookResponses, GithubPullRequest, GithubPullRequestCheckRun, GithubPullRequestComment, GithubPullRequestsResponse, GroupedInitialParams, GroupedPageParams, GroupedSoupGroupPage, GroupedSoupInitialPage, GroupedSoupPage, GroupedSoupSort, GuestId, GuestJoinRequest, HandlerData, HandlerError, HandlerErrors, HandlerResponse, HandlerResponses, Harness, HarnessAgent, HarnessId, HarnessOwner, HarnessSession, HashMap, HealthHandlerData, HealthHandlerResponse, HealthHandlerResponses, HighlightType, ImportCounters, ImportedAuthor, ImportError, ImportLimits, ImportPage, ImportProgress, ImportWarning, InFlightTurnSummary, IngestTranscriptData, IngestTranscriptError, IngestTranscriptErrors, IngestTranscriptResponses, InitializeUserDocumentsData, InitializeUserDocumentsError, InitializeUserDocumentsErrors, InitializeUserDocumentsResponse, InitializeUserDocumentsResponses, InitialPropertyValue, InitiativeDetail, InitiativeFilters, InitiativeId, InitiativeList, InitiativeSummary, InputReceivedMetadata, InstallSyncData, InstallSyncErrors, InteractionReason, InviteMeetingRequest, InviteMeetingUsersRequest, Item, ItemWithUserAccessLevel, JobId, JobProcessingResultHandlerData, JobProcessingResultHandlerError, JobProcessingResultHandlerErrors, JobProcessingResultHandlerResponse, JobProcessingResultHandlerResponses, JobStatus, JoinChannelByCodeData, JoinChannelByCodeError, JoinChannelByCodeErrors, JoinChannelByCodeResponses, JoinChannelData, JoinChannelError, JoinChannelErrors, JoinChannelResponses, KeySegment, LatestMessage, LeaveCallResponse, LeaveChannelData, LeaveChannelError, LeaveChannelErrors, LeaveChannelResponses, LeaveOrEndCallData, LeaveOrEndCallError, LeaveOrEndCallErrors, LeaveOrEndCallResponse, LeaveOrEndCallResponses, LinkShare, ListAgentsData, ListAgentsError, ListAgentsErrors, ListAgentsResponse, ListAgentsResponses, ListBotChannelsData, ListBotChannelsError, ListBotChannelsErrors, ListBotChannelsResponse, ListBotChannelsResponses, ListChannelLabelsData, ListChannelLabelsError, ListChannelLabelsErrors, ListChannelLabelsResponse, ListChannelLabelsResponses, ListCompanyContactsData, ListCompanyContactsError, ListCompanyContactsErrors, ListCompanyContactsResponse, ListCompanyContactsResponses, ListFavoritesData, ListFavoritesError, ListFavoritesErrors, ListFavoritesResponse, ListFavoritesResponses, ListHarnessAgentsData, ListHarnessAgentsError, ListHarnessAgentsErrors, ListHarnessAgentsResponse, ListHarnessAgentsResponses, ListHarnessesData, ListHarnessesError, ListHarnessesErrors, ListHarnessesResponse, ListHarnessesResponses, ListHarnessSessionsData, ListHarnessSessionsError, ListHarnessSessionsErrors, ListHarnessSessionsResponse, ListHarnessSessionsResponses, ListInitiativesData, ListInitiativesError, ListInitiativesErrors, ListInitiativesResponse, ListInitiativesResponses, ListOccurrencesData, ListOccurrencesErrors, ListOccurrencesResponse, ListOccurrencesResponses, ListReminderCollectionData, ListReminderCollectionError, ListReminderCollectionErrors, ListReminderCollectionResponse, ListReminderCollectionResponses, ListRemindersData, ListRemindersError, ListRemindersErrors, ListRemindersResponse, ListRemindersResponses, ListSlackImportsData, ListSlackImportsResponse, ListSlackImportsResponses, ListTeamOutOfOfficeData, ListTeamOutOfOfficeErrors, ListTeamOutOfOfficeResponse, ListTeamOutOfOfficeResponses, ListUserApiKeysData, ListUserApiKeysError, ListUserApiKeysErrors, ListUserApiKeysResponse, ListUserApiKeysResponses, ListWebhooksData, ListWebhooksError, ListWebhooksErrors, ListWebhooksResponse, ListWebhooksResponse2, ListWebhooksResponses, LocationResponseData, LocationResponseV3, MacroUserIdStr, Meeting, MeetingCancelData, MeetingCancelError, MeetingCancelErrors, MeetingCancelPreparationData, MeetingCancelPreparationError, MeetingCancelPreparationErrors, MeetingCancelPreparationResponse, MeetingCancelPreparationResponses, MeetingCancelResponse, MeetingCancelResponses, MeetingCreateData, MeetingCreateError, MeetingCreateErrors, MeetingCreateResponse, MeetingCreateResponses, MeetingGuestJoinData, MeetingGuestJoinError, MeetingGuestJoinErrors, MeetingGuestJoinResponse, MeetingGuestJoinResponses, MeetingGuestParticipantsData, MeetingGuestParticipantsError, MeetingGuestParticipantsErrors, MeetingGuestParticipantsResponse, MeetingGuestParticipantsResponses, MeetingInviteData, MeetingInviteError, MeetingInviteErrors, MeetingInvitePermissions, MeetingInvitePermissionsData, MeetingInvitePermissionsError, MeetingInvitePermissionsErrors, MeetingInvitePermissionsResponse, MeetingInvitePermissionsResponses, MeetingInviteResponse, MeetingInviteResponses, MeetingInviteUsersData, MeetingInviteUsersError, MeetingInviteUsersErrors, MeetingInviteUsersResponse, MeetingInviteUsersResponses, MeetingJoinData, MeetingJoinError, MeetingJoinErrors, MeetingJoinResponse, MeetingJoinResponses, MeetingLeaveData, MeetingLeaveError, MeetingLeaveErrors, MeetingLeaveResponse, MeetingLeaveResponses, MeetingListActiveData, MeetingListActiveError, MeetingListActiveErrors, MeetingListActiveResponse, MeetingListActiveResponses, MeetingListData, MeetingListError, MeetingListErrors, MeetingListResponse, MeetingListResponses, MeetingLookupData, MeetingLookupError, MeetingLookupErrors, MeetingLookupResponse, MeetingLookupResponses, MeetingParticipant, MeetingParticipants, MeetingParticipantsData, MeetingParticipantsError, MeetingParticipantsErrors, MeetingParticipantsResponse, MeetingParticipantsResponses, MeetingPreparation, MeetingPrepareData, MeetingPrepareError, MeetingPrepareErrors, MeetingPrepareResponse, MeetingPrepareResponses, MeetingShareData, MeetingShareError, MeetingShareErrors, MeetingShareResponse, MeetingShareResponses, MeetingsResponse, MeetingToken, MeetingUpdateData, MeetingUpdateError, MeetingUpdateErrors, MeetingUpdateResponse, MeetingUpdateResponses, MentionPreviewsData, MentionPreviewsErrors, MentionPreviewsResponse, MentionPreviewsResponses, Message, MessageAttachment, MessageAttachmentCreatedMetadata, MessageAttachmentRemovedMetadata, MessageChange, MessageCursor, MessageDeletedMetadata, MessageDirection, MessageEvent, MessageEventAttachment, MessageListItem, MessageMentionedMetadata, MessagePage, MessageParent, MessagePatch, MessagePatchedMetadata, MessagePostedMetadata, MessageThread, MessageThreadPreview, MessageTimelineData, MessageTimelineQuery, MessageTimelineResponse, MessageTimelineResponses, MessageTopicEvent, NewAttachment, NewThreadAnchor, NotificationFilters, NotificationState, PairingDetails, Params, ParticipantRole, PatchBotRequest, PatchChannelData, PatchChannelError, PatchChannelErrors, PatchChannelRequest, PatchChannelResponse, PatchChannelResponses, PatchMessageNotificationPolicy, PatchProjectRequestV2, PatchThreadRequestV2, PatchViewHandlerData, PatchViewHandlerError, PatchViewHandlerErrors, PatchViewHandlerResponses, PatchWebhookData, PatchWebhookError, PatchWebhookErrors, PatchWebhookRequest, PatchWebhookResponse, PatchWebhookResponses, PdfAnchor, PdfAnchorId, PdfHighlightAnchor, PdfHighlightAnchorRequest, PdfHighlightRect, PdfHighlightRectAnchorRequest, PdfPlaceableCommentAnchor, PendingClaimResponse, PermanentlyDeleteDocumentData, PermanentlyDeleteDocumentError, PermanentlyDeleteDocumentErrors, PermanentlyDeleteDocumentResponse, PermanentlyDeleteDocumentResponses, PermanentlyDeleteProjectData, PermanentlyDeleteProjectError, PermanentlyDeleteProjectErrors, PermanentlyDeleteProjectResponse, PermanentlyDeleteProjectResponses, PinnedItem, PinRequest, PostActivityData, PostActivityError, PostActivityErrors, PostActivityRequest, PostActivityResponse, PostActivityResponses, PostChannelBotWebhookData, PostChannelBotWebhookError, PostChannelBotWebhookErrors, PostChannelBotWebhookResponse, PostChannelBotWebhookResponses, PostGroupedSoupAstGroupPageRequest, PostGroupedSoupAstInitialRequest, PostGroupedSoupAstRequest, PostItemsSoupAstData, PostItemsSoupAstError, PostItemsSoupAstErrors, PostItemsSoupAstGroupedData, PostItemsSoupAstGroupedError, PostItemsSoupAstGroupedErrors, PostItemsSoupAstGroupedResponse, PostItemsSoupAstGroupedResponses, PostItemsSoupAstResponse, PostItemsSoupAstResponses, PostItemsSoupData, PostItemsSoupError, PostItemsSoupErrors, PostItemsSoupResponse, PostItemsSoupResponses, PostMessage, PostMessageNotificationPolicy, PostSoupAstRequest, PostSoupRequest, PresaveDocumentHandlerData, PresaveDocumentHandlerError, PresaveDocumentHandlerErrors, PresaveDocumentHandlerResponse, PresaveDocumentHandlerResponses, PreSaveDocumentRequest, PreSaveDocumentResponse, PreSaveDocumentResponseData, PresignedUrl, PreviewSmartTagData, PreviewSmartTagError, PreviewSmartTagErrors, PreviewSmartTagResponse, PreviewSmartTagResponses, Project, ProjectDeleteResponseData, ProjectDocumentStatus, ProjectFilters, ProjectPreview, ProjectPreviewData, PropertyDefinition, PropertyFilter, PropertyInput, PropertyOwner, PropertyValue, PutCrmTeamSettingsData, PutCrmTeamSettingsError, PutCrmTeamSettingsErrors, PutCrmTeamSettingsResponse, PutCrmTeamSettingsResponses, PutCrmTeamStagesData, PutCrmTeamStagesError, PutCrmTeamStagesErrors, PutCrmTeamStagesResponse, PutCrmTeamStagesResponses, ReactionInput, RecentlyDeletedData, RecentlyDeletedError, RecentlyDeletedErrors, RecentlyDeletedResponse, RecentlyDeletedResponseData, RecentlyDeletedResponses, RegisterSlackImportUploadsData, RegisterSlackImportUploadsErrors, RegisterSlackImportUploadsResponse, RegisterSlackImportUploadsResponses, RegisterUploads, Reminder, ReminderCollectionPage, ReminderCollectionRow, ReminderFilters, ReminderReference, ReminderSchedule, RemindersList, RemoveBotFromChannelByBotData, RemoveBotFromChannelByBotError, RemoveBotFromChannelByBotErrors, RemoveBotFromChannelByBotResponse, RemoveBotFromChannelByBotResponses, RemoveFavoriteByEntityData, RemoveFavoriteByEntityError, RemoveFavoriteByEntityErrors, RemoveFavoriteByEntityResponses, RemoveParticipantsData, RemoveParticipantsError, RemoveParticipantsErrors, RemoveParticipantsRequest, RemoveParticipantsResponses, RemovePinHandlerData, RemovePinHandlerError, RemovePinHandlerErrors, RemovePinHandlerResponse, RemovePinHandlerResponses, RenameChannelLabelData, RenameChannelLabelError, RenameChannelLabelErrors, RenameChannelLabelRequest, RenameChannelLabelResponse, RenameChannelLabelResponses, ReorderFavoritesData, ReorderFavoritesError, ReorderFavoritesErrors, ReorderFavoritesRequest, ReorderFavoritesResponses, ReorderPinRequest, ReorderPinsHandlerData, ReorderPinsHandlerError, ReorderPinsHandlerErrors, ReorderPinsHandlerResponse, ReorderPinsHandlerResponses, ReplaceCrmStagesRequest, RequestedHarnessScope, ResetCrmTeamStagesData, ResetCrmTeamStagesError, ResetCrmTeamStagesErrors, ResetCrmTeamStagesResponse, ResetCrmTeamStagesResponses, RevertDeleteDocumentData, RevertDeleteDocumentError, RevertDeleteDocumentErrors, RevertDeleteDocumentResponse, RevertDeleteDocumentResponses, RevertDeleteProjectData, RevertDeleteProjectError, RevertDeleteProjectErrors, RevertDeleteProjectResponse, RevertDeleteProjectResponses, RingStatus, RingStatusResponse, S3ObjectInfo, SaveBomPart, SaveDocumentHandlerData, SaveDocumentHandlerError, SaveDocumentHandlerErrors, SaveDocumentHandlerResponse, SaveDocumentHandlerResponses, SaveDocumentRequest, SaveDocumentResponse, SaveDocumentResponseData, SearchContactsData, SearchContactsError, SearchContactsErrors, SearchContactsResponse, SearchContactsResponse2, SearchContactsResponses, SearchState, SessionDeletedMetadata, SessionIdentity, SessionMentionedMetadata, SessionOpenedMetadata, SessionRenamedMetadata, SessionSettledMetadata, SessionStoppedMetadata, SetChannelLabelData, SetChannelLabelError, SetChannelLabelErrors, SetChannelLabelRequest, SetChannelLabelResponse, SetChannelLabelResponses, SetChannelPictureData, SetChannelPictureError, SetChannelPictureErrors, SetChannelPictureRequest, SetChannelPictureResponse, SetChannelPictureResponses, SetCompanyHiddenData, SetCompanyHiddenError, SetCompanyHiddenErrors, SetCompanyHiddenRequest, SetCompanyHiddenResponse, SetCompanyHiddenResponses, SetCompanyNameRequest, SetContactHiddenData, SetContactHiddenError, SetContactHiddenErrors, SetContactHiddenRequest, SetContactHiddenResponse, SetContactHiddenResponses, SetContactNameRequest, SetCrmCompanyNameData, SetCrmCompanyNameError, SetCrmCompanyNameErrors, SetCrmCompanyNameResponse, SetCrmCompanyNameResponses, SetCrmContactNameData, SetCrmContactNameError, SetCrmContactNameErrors, SetCrmContactNameResponse, SetCrmContactNameResponses, SetDocumentTeamShareData, SetDocumentTeamShareError, SetDocumentTeamShareErrors, SetDocumentTeamShareRequest, SetDocumentTeamShareResponse, SetDocumentTeamShareResponses, SetEmailFollowupData, SetEmailFollowupError, SetEmailFollowupErrors, SetEmailFollowupResponse, SetEmailFollowupResponses, SetEmailSyncData, SetEmailSyncError, SetEmailSyncErrors, SetEmailSyncRequest, SetEmailSyncResponse, SetEmailSyncResponses, SetPropertyValue, Sha256Digest, SharedEmailFilter, SharePermissionV2, ShortIdResponse, SimpleMention, SimpleSaveData, SimpleSaveError, SimpleSaveErrors, SimpleSaveResponse, SimpleSaveResponses, SlackCompleteRequest, SlackCreateRequest, SlackRegisterRequest, SlackTimestamp, SlackUserId, SmartTagChannelMatch, SmartTagPreview, SoupAgentSessionSoupPropertiesField, SoupApiItem, SoupApiSort, SoupApiSortDirection, SoupAttachment, SoupCalendarEventSoupPropertiesField, SoupCalendarEventTime, SoupCallRecordGuest, SoupCallRecordParticipant, SoupCallRecordSoupPropertiesField, SoupChannel, SoupChannelThread, SoupChatSoupPropertiesField, SoupContact, SoupCountedReaction, SoupCrmCompanySoupPropertiesField, SoupCrmDomain, SoupDocumentSoupPropertiesField, SoupDocumentSubType, SoupEmailThreadPreview, SoupEnrichedEmailThreadPreviewSoupPropertiesField, SoupForeignEntity, SoupInitiativeSoupPropertiesField, SoupItem, SoupLabel, SoupLabelListVisibility, SoupLabelType, SoupMessageAttachment, SoupMessageListVisibility, SoupMessageSender, SoupMessageSenderType, SoupPage, SoupProjectSoupPropertiesField, SoupPropertiesField, SoupProperty, SoupReminderReference, SoupReminderSchedule, SoupReminderSoupPropertiesField, SoupThreadInfo, SoupThreadReply, SourceBinding, SourceId, SourceIdentity, StarterDocumentsResponse, StreamEventsData, StreamEventsError, StreamEventsErrors, StreamEventsResponse, StreamEventsResponses, String, SuccessResponse, SurfaceState, SyncServiceVersionId, SystemSkillSummary, TagFilterMode, TaskFilters, TeamOutOfOfficeItem, TeamOutOfOfficeResponse, TeamRole, ThreadAnchor, ThreadOrigin, ThreadPatch, ThreadState, ToggleShareWithTeamData, ToggleShareWithTeamError, ToggleShareWithTeamErrors, ToggleShareWithTeamResponse, ToggleShareWithTeamResponses, TranscribeDictationData, TranscribeDictationError, TranscribeDictationErrors, TranscribeDictationResponse, TranscribeDictationResponses, TranscribeResponse, TranscriptSegmentRequest, TurnEndedMetadata, TurnStartedMetadata, TurnSummary, TypedSuccessResponse, TypedSuccessResponseGetDocumentResponseData, TypingInput, UnassignInitiativeTaskData, UnassignInitiativeTaskError, UnassignInitiativeTaskErrors, UnassignInitiativeTaskResponse, UnassignInitiativeTaskResponses, UpdateAgentData, UpdateAgentError, UpdateAgentErrors, UpdateAgentRequest, UpdateAgentResponse, UpdateAgentResponses, UpdateChannelSharePermission, UpdateCrmTeamSettingsRequest, UpdateInitiativeData, UpdateInitiativeError, UpdateInitiativeErrors, UpdateInitiativeRequest, UpdateInitiativeResponse, UpdateInitiativeResponses, UpdateMeetingRequest, UpdateOperation, UpdateReminderData, UpdateReminderError, UpdateReminderErrors, UpdateReminderRequest, UpdateReminderResponse, UpdateReminderResponses, UpdateSharePermissionRequestV2, UploadDescriptor, UploadDocumentStatus, UploadExtractFolderHandlerData, UploadExtractFolderHandlerError, UploadExtractFolderHandlerErrors, UploadExtractFolderHandlerResponse, UploadExtractFolderHandlerResponses, UploadExtractFolderRequest, UploadFolderHandlerData, UploadFolderHandlerError, UploadFolderHandlerErrors, UploadFolderHandlerResponse, UploadFolderHandlerResponses, UploadFolderRequest, UploadGrant, UploadId, UpsertHistoryHandlerData, UpsertHistoryHandlerError, UpsertHistoryHandlerErrors, UpsertHistoryHandlerResponse, UpsertHistoryHandlerResponses, UpsertUserDocumentViewLocationData, UpsertUserDocumentViewLocationError, UpsertUserDocumentViewLocationErrors, UpsertUserDocumentViewLocationRequest, UpsertUserDocumentViewLocationResponse, UpsertUserDocumentViewLocationResponses, UserApiKeyId, UserApiKeyInfo, UserApiKeysList, UserDocumentsResponse, UserDocumentViewLocation, UserDocumentViewLocationResponse, UserPinsResponse, UserViewsResponse, ValidateDocumentPermissionsTokenData, ValidateDocumentPermissionsTokenError, ValidateDocumentPermissionsTokenErrors, ValidateDocumentPermissionsTokenResponse, ValidateDocumentPermissionsTokenResponses, ValidateWebhookData, ValidateWebhookError, ValidateWebhookErrors, ValidateWebhookResponse, ValidateWebhookResponse2, ValidateWebhookResponses, Vec, View, ViewPatch, ViewsResponse, WaitingForInputMetadata, Webhook, WebhookEvent, WebhookFilter, WebhookScope, WebhookStatus, WebhookValidationTestEvent, WithCallId, WithChannelId, WithDocumentId, WithProjectId } from './types.gen';
diff --git a/packages/sdk/generated/storage/sdk.gen.ts b/packages/sdk/generated/storage/sdk.gen.ts
index 980b6045de..1d9aa15fed 100644
--- a/packages/sdk/generated/storage/sdk.gen.ts
+++ b/packages/sdk/generated/storage/sdk.gen.ts
@@ -2,7 +2,7 @@
import type { Client, ClientMeta, Options as Options2, RequestResult, ServerSentEventsResult, TDataShape } from './client';
import { client } from './client.gen';
-import type { AddFavoriteData, AddFavoriteErrors, AddFavoriteResponses, AddParticipantsData, AddParticipantsErrors, AddParticipantsResponses, AddPinHandlerData, AddPinHandlerErrors, AddPinHandlerResponses, ApproveHarnessPairingData, ApproveHarnessPairingErrors, ApproveHarnessPairingResponses, AssignInitiativeTasksData, AssignInitiativeTasksErrors, AssignInitiativeTasksResponses, BulkWakeupSyncServiceDocumentsData, BulkWakeupSyncServiceDocumentsErrors, BulkWakeupSyncServiceDocumentsResponses, CallWebhookData, CallWebhookErrors, CallWebhookResponses, CheckActiveCallData, CheckActiveCallErrors, CheckActiveCallResponses, ClaimHarnessPairingData, ClaimHarnessPairingErrors, ClaimHarnessPairingResponses, CopyDocumentData, CopyDocumentErrors, CopyDocumentResponses, CreateAgentData, CreateAgentErrors, CreateAgentResponses, CreateAnchorData, CreateAnchorErrors, CreateAnchorResponses, CreateChannelData, CreateChannelErrors, CreateChannelLabelData, CreateChannelLabelErrors, CreateChannelLabelResponses, CreateChannelResponses, CreateChannelScopedBotData, CreateChannelScopedBotErrors, CreateChannelScopedBotResponses, CreateCollabSurfaceTokenData, CreateCollabSurfaceTokenErrors, CreateCollabSurfaceTokenResponses, CreateCrmCompanyData, CreateCrmCompanyErrors, CreateCrmCompanyResponses, CreateCrmContactData, CreateCrmContactErrors, CreateCrmContactResponses, CreateDocumentData, CreateDocumentErrors, CreateDocumentResponses, CreateEntityMentionData, CreateEntityMentionErrors, CreateEntityMentionResponses, CreateHarnessPairingData, CreateHarnessPairingErrors, CreateHarnessPairingResponses, CreateInitiativeData, CreateInitiativeErrors, CreateInitiativeResponses, CreateInstructionsHandlerData, CreateInstructionsHandlerErrors, CreateInstructionsHandlerResponses, CreateMarkdownHandlerData, CreateMarkdownHandlerErrors, CreateMarkdownHandlerResponses, CreateProjectHandlerData, CreateProjectHandlerErrors, CreateProjectHandlerResponses, CreateReminderData, CreateReminderErrors, CreateReminderResponses, CreateSkillHandlerData, CreateSkillHandlerErrors, CreateSkillHandlerResponses, CreateSnippetHandlerData, CreateSnippetHandlerErrors, CreateSnippetHandlerResponses, CreateTaskHandlerData, CreateTaskHandlerErrors, CreateTaskHandlerResponses, CreateUserApiKeyData, CreateUserApiKeyErrors, CreateUserApiKeyResponses, CreateViewHandlerData, CreateViewHandlerErrors, CreateViewHandlerResponses, CreateWebhookData, CreateWebhookErrors, CreateWebhookResponses, DeleteAnchorData, DeleteAnchorErrors, DeleteAnchorResponses, DeleteCallRecordData, DeleteCallRecordErrors, DeleteCallRecordResponses, DeleteChannelData, DeleteChannelErrors, DeleteChannelLabelData, DeleteChannelLabelErrors, DeleteChannelLabelResponses, DeleteChannelResponses, DeleteCollabSurfaceData, DeleteCollabSurfaceErrors, DeleteCollabSurfaceResponses, DeleteDocumentData, DeleteDocumentErrors, DeleteDocumentResponses, DeleteEntityMentionData, DeleteEntityMentionErrors, DeleteEntityMentionResponses, DeleteHarnessData, DeleteHarnessErrors, DeleteHarnessResponses, DeleteHistoryHandlerData, DeleteHistoryHandlerErrors, DeleteHistoryHandlerResponses, DeleteInitiativeData, DeleteInitiativeErrors, DeleteInitiativeResponses, DeleteProjectHandlerData, DeleteProjectHandlerErrors, DeleteProjectHandlerResponses, DeleteReminderData, DeleteReminderErrors, DeleteReminderResponses, DeleteSelfHarnessData, DeleteSelfHarnessErrors, DeleteSelfHarnessResponses, DeleteUserApiKeyData, DeleteUserApiKeyErrors, DeleteUserApiKeyResponses, DeleteUserDocumentViewLocationData, DeleteUserDocumentViewLocationErrors, DeleteUserDocumentViewLocationResponses, DeleteViewHandlerData, DeleteViewHandlerErrors, DeleteViewHandlerResponses, DeleteWebhookData, DeleteWebhookErrors, DeleteWebhookResponses, EditAnchorData, EditAnchorErrors, EditAnchorResponses, EditCallRecordData, EditCallRecordErrors, EditCallRecordResponses, EditCallTranscriptData, EditCallTranscriptErrors, EditCallTranscriptResponses, EditDocumentData, EditDocumentErrors, EditDocumentResponses, EditProjectV2Data, EditProjectV2Errors, EditProjectV2Responses, EditThreadV2Data, EditThreadV2Errors, EditThreadV2Responses, EnsureCollabSurfaceData, EnsureCollabSurfaceErrors, EnsureCollabSurfaceResponses, EntityMessageCreateData, EntityMessageCreateResponses, EntityMessageDeleteMessageData, EntityMessageDeleteMessageResponses, EntityMessageDeleteThreadData, EntityMessageDeleteThreadResponses, EntityMessageEditData, EntityMessageEditResponses, EntityMessageGetMessageData, EntityMessageGetMessageResponses, EntityMessageGetThreadData, EntityMessageGetThreadResponses, EntityMessageLegacyData, EntityMessageLegacyResponses, EntityMessagePatchThreadData, EntityMessagePatchThreadResponses, EntityMessageReactData, EntityMessageReactResponses, EntityMessageTypingData, EntityMessageTypingResponses, ExcludeDefaultViewHandlerData, ExcludeDefaultViewHandlerErrors, ExcludeDefaultViewHandlerResponses, ExportDocumentData, ExportDocumentErrors, ExportDocumentResponses, GetActiveCallsData, GetActiveCallsErrors, GetActiveCallsResponses, GetActivityData, GetActivityErrors, GetActivityResponses, GetAttachmentReferencesData, GetAttachmentReferencesErrors, GetAttachmentReferencesResponses, GetBatchCallRecordPreviewData, GetBatchCallRecordPreviewErrors, GetBatchCallRecordPreviewResponses, GetBatchChannelPreviewData, GetBatchChannelPreviewErrors, GetBatchChannelPreviewResponses, GetBatchPreviewHandlerData, GetBatchPreviewHandlerErrors, GetBatchPreviewHandlerResponses, GetBatchProjectPreviewData, GetBatchProjectPreviewErrors, GetBatchProjectPreviewResponses, GetBotOwnerProfilesData, GetBotOwnerProfilesErrors, GetBotOwnerProfilesResponses, GetCallRecordData, GetCallRecordErrors, GetCallRecordResponses, GetChannelAttachmentsData, GetChannelAttachmentsErrors, GetChannelAttachmentsResponses, GetChannelData, GetChannelErrors, GetChannelJoinLinkData, GetChannelJoinLinkErrors, GetChannelJoinLinkResponses, GetChannelParticipantsData, GetChannelParticipantsErrors, GetChannelParticipantsResponses, GetChannelResponses, GetChannelsData, GetChannelsErrors, GetChannelsResponses, GetCollabSurfaceData, GetCollabSurfaceErrors, GetCollabSurfaceResponses, GetCompanyData, GetCompanyErrors, GetCompanyResponses, GetContactByEmailData, GetContactByEmailErrors, GetContactByEmailResponses, GetContactData, GetContactErrors, GetContactResponses, GetCrmTeamSettingsData, GetCrmTeamSettingsErrors, GetCrmTeamSettingsResponses, GetDocumentAnchorsData, GetDocumentAnchorsErrors, GetDocumentAnchorsResponses, GetDocumentBranchNameData, GetDocumentBranchNameErrors, GetDocumentBranchNameResponses, GetDocumentByTeamSlugData, GetDocumentByTeamSlugErrors, GetDocumentByTeamSlugResponses, GetDocumentData, GetDocumentErrors, GetDocumentGithubPullRequestsData, GetDocumentGithubPullRequestsErrors, GetDocumentGithubPullRequestsResponses, GetDocumentListHandlerData, GetDocumentListHandlerErrors, GetDocumentListHandlerResponses, GetDocumentLocationV3Data, GetDocumentLocationV3Errors, GetDocumentLocationV3Responses, GetDocumentPermissionsTokenData, GetDocumentPermissionsTokenErrors, GetDocumentPermissionsTokenResponses, GetDocumentPermissionsV2Data, GetDocumentPermissionsV2Errors, GetDocumentPermissionsV2Responses, GetDocumentProcessingResultData, GetDocumentProcessingResultErrors, GetDocumentProcessingResultResponses, GetDocumentResponses, GetDocumentShortIdData, GetDocumentShortIdErrors, GetDocumentShortIdResponses, GetDocumentTeamShareData, GetDocumentTeamShareErrors, GetDocumentTeamShareResponses, GetDocumentVersionData, GetDocumentVersionErrors, GetDocumentVersionResponses, GetDocumentViewsHandlerData, GetDocumentViewsHandlerErrors, GetDocumentViewsHandlerResponses, GetEmailFollowupData, GetEmailFollowupErrors, GetEmailFollowupResponses, GetEntityPermissionData, GetEntityPermissionErrors, GetEntityPermissionResponses, GetForeignEntityBySourceData, GetForeignEntityBySourceErrors, GetForeignEntityBySourceResponses, GetForeignEntityData, GetForeignEntityErrors, GetForeignEntityResponses, GetHarnessPairingData, GetHarnessPairingErrors, GetHarnessPairingResponses, GetHistoryHandlerData, GetHistoryHandlerErrors, GetHistoryHandlerResponses, GetInitiativeData, GetInitiativeErrors, GetInitiativeResponses, GetInstructionsHandlerData, GetInstructionsHandlerErrors, GetInstructionsHandlerResponses, GetItemsSoupData, GetItemsSoupErrors, GetItemsSoupResponses, GetLocationHandlerData, GetLocationHandlerErrors, GetLocationHandlerResponses, GetOrCreateCallData, GetOrCreateCallErrors, GetOrCreateCallResponses, GetOrCreateDmData, GetOrCreateDmErrors, GetOrCreateDmResponses, GetOrCreatePrivateData, GetOrCreatePrivateErrors, GetOrCreatePrivateResponses, GetPendingProjectsHandlerData, GetPendingProjectsHandlerErrors, GetPendingProjectsHandlerResponses, GetPinsHandlerData, GetPinsHandlerErrors, GetPinsHandlerResponses, GetProjectContentHandlerData, GetProjectContentHandlerErrors, GetProjectContentHandlerResponses, GetProjectHandlerData, GetProjectHandlerErrors, GetProjectHandlerResponses, GetProjectPermissionsV2Data, GetProjectPermissionsV2Errors, GetProjectPermissionsV2Responses, GetProjectsHandlerData, GetProjectsHandlerErrors, GetProjectsHandlerResponses, GetProjectUserAccessLevelData, GetProjectUserAccessLevelErrors, GetProjectUserAccessLevelResponses, GetReminderData, GetReminderErrors, GetReminderResponses, GetRingStatusData, GetRingStatusErrors, GetRingStatusResponses, GetSelfBotData, GetSelfBotErrors, GetSelfBotResponses, GetSelfHarnessData, GetSelfHarnessErrors, GetSelfHarnessResponses, GetSystemSkillsHandlerData, GetSystemSkillsHandlerErrors, GetSystemSkillsHandlerResponses, GetUserDocumentsHandlerData, GetUserDocumentsHandlerErrors, GetUserDocumentsHandlerResponses, GetUserDocumentViewLocationData, GetUserDocumentViewLocationErrors, GetUserDocumentViewLocationResponses, GetViewsHandlerData, GetViewsHandlerErrors, GetViewsHandlerResponses, GetWebhookData, GetWebhookErrors, GetWebhookResponses, HandlerData, HandlerErrors, HandlerResponses, HealthHandlerData, HealthHandlerResponses, IngestTranscriptData, IngestTranscriptErrors, IngestTranscriptResponses, InitializeUserDocumentsData, InitializeUserDocumentsErrors, InitializeUserDocumentsResponses, InstallSyncData, InstallSyncErrors, JobProcessingResultHandlerData, JobProcessingResultHandlerErrors, JobProcessingResultHandlerResponses, JoinChannelByCodeData, JoinChannelByCodeErrors, JoinChannelByCodeResponses, JoinChannelData, JoinChannelErrors, JoinChannelResponses, LeaveChannelData, LeaveChannelErrors, LeaveChannelResponses, LeaveOrEndCallData, LeaveOrEndCallErrors, LeaveOrEndCallResponses, ListAgentsData, ListAgentsErrors, ListAgentsResponses, ListBotChannelsData, ListBotChannelsErrors, ListBotChannelsResponses, ListChannelLabelsData, ListChannelLabelsErrors, ListChannelLabelsResponses, ListCompanyContactsData, ListCompanyContactsErrors, ListCompanyContactsResponses, ListFavoritesData, ListFavoritesErrors, ListFavoritesResponses, ListHarnessAgentsData, ListHarnessAgentsErrors, ListHarnessAgentsResponses, ListHarnessesData, ListHarnessesErrors, ListHarnessesResponses, ListHarnessSessionsData, ListHarnessSessionsErrors, ListHarnessSessionsResponses, ListInitiativesData, ListInitiativesErrors, ListInitiativesResponses, ListOccurrencesData, ListOccurrencesErrors, ListOccurrencesResponses, ListReminderCollectionData, ListReminderCollectionErrors, ListReminderCollectionResponses, ListRemindersData, ListRemindersErrors, ListRemindersResponses, ListTeamOutOfOfficeData, ListTeamOutOfOfficeErrors, ListTeamOutOfOfficeResponses, ListUserApiKeysData, ListUserApiKeysErrors, ListUserApiKeysResponses, ListWebhooksData, ListWebhooksErrors, ListWebhooksResponses, MeetingCancelData, MeetingCancelErrors, MeetingCancelPreparationData, MeetingCancelPreparationErrors, MeetingCancelPreparationResponses, MeetingCancelResponses, MeetingCreateData, MeetingCreateErrors, MeetingCreateResponses, MeetingGuestJoinData, MeetingGuestJoinErrors, MeetingGuestJoinResponses, MeetingGuestParticipantsData, MeetingGuestParticipantsErrors, MeetingGuestParticipantsResponses, MeetingInviteData, MeetingInviteErrors, MeetingInvitePermissionsData, MeetingInvitePermissionsErrors, MeetingInvitePermissionsResponses, MeetingInviteResponses, MeetingInviteUsersData, MeetingInviteUsersErrors, MeetingInviteUsersResponses, MeetingJoinData, MeetingJoinErrors, MeetingJoinResponses, MeetingLeaveData, MeetingLeaveErrors, MeetingLeaveResponses, MeetingListActiveData, MeetingListActiveErrors, MeetingListActiveResponses, MeetingListData, MeetingListErrors, MeetingListResponses, MeetingLookupData, MeetingLookupErrors, MeetingLookupResponses, MeetingParticipantsData, MeetingParticipantsErrors, MeetingParticipantsResponses, MeetingPrepareData, MeetingPrepareErrors, MeetingPrepareResponses, MeetingShareData, MeetingShareErrors, MeetingShareResponses, MeetingUpdateData, MeetingUpdateErrors, MeetingUpdateResponses, MentionPreviewsData, MentionPreviewsErrors, MentionPreviewsResponses, MessageTimelineData, MessageTimelineResponses, PatchChannelData, PatchChannelErrors, PatchChannelResponses, PatchViewHandlerData, PatchViewHandlerErrors, PatchViewHandlerResponses, PatchWebhookData, PatchWebhookErrors, PatchWebhookResponses, PermanentlyDeleteDocumentData, PermanentlyDeleteDocumentErrors, PermanentlyDeleteDocumentResponses, PermanentlyDeleteProjectData, PermanentlyDeleteProjectErrors, PermanentlyDeleteProjectResponses, PostActivityData, PostActivityErrors, PostActivityResponses, PostChannelBotWebhookData, PostChannelBotWebhookErrors, PostChannelBotWebhookResponses, PostItemsSoupAstData, PostItemsSoupAstErrors, PostItemsSoupAstGroupedData, PostItemsSoupAstGroupedErrors, PostItemsSoupAstGroupedResponses, PostItemsSoupAstResponses, PostItemsSoupData, PostItemsSoupErrors, PostItemsSoupResponses, PresaveDocumentHandlerData, PresaveDocumentHandlerErrors, PresaveDocumentHandlerResponses, PreviewSmartTagData, PreviewSmartTagErrors, PreviewSmartTagResponses, PutCrmTeamSettingsData, PutCrmTeamSettingsErrors, PutCrmTeamSettingsResponses, PutCrmTeamStagesData, PutCrmTeamStagesErrors, PutCrmTeamStagesResponses, RecentlyDeletedData, RecentlyDeletedErrors, RecentlyDeletedResponses, RemoveBotFromChannelByBotData, RemoveBotFromChannelByBotErrors, RemoveBotFromChannelByBotResponses, RemoveFavoriteByEntityData, RemoveFavoriteByEntityErrors, RemoveFavoriteByEntityResponses, RemoveParticipantsData, RemoveParticipantsErrors, RemoveParticipantsResponses, RemovePinHandlerData, RemovePinHandlerErrors, RemovePinHandlerResponses, RenameChannelLabelData, RenameChannelLabelErrors, RenameChannelLabelResponses, ReorderFavoritesData, ReorderFavoritesErrors, ReorderFavoritesResponses, ReorderPinsHandlerData, ReorderPinsHandlerErrors, ReorderPinsHandlerResponses, ResetCrmTeamStagesData, ResetCrmTeamStagesErrors, ResetCrmTeamStagesResponses, RevertDeleteDocumentData, RevertDeleteDocumentErrors, RevertDeleteDocumentResponses, RevertDeleteProjectData, RevertDeleteProjectErrors, RevertDeleteProjectResponses, SaveDocumentHandlerData, SaveDocumentHandlerErrors, SaveDocumentHandlerResponses, SearchContactsData, SearchContactsErrors, SearchContactsResponses, SetChannelLabelData, SetChannelLabelErrors, SetChannelLabelResponses, SetChannelPictureData, SetChannelPictureErrors, SetChannelPictureResponses, SetCompanyHiddenData, SetCompanyHiddenErrors, SetCompanyHiddenResponses, SetContactHiddenData, SetContactHiddenErrors, SetContactHiddenResponses, SetCrmCompanyNameData, SetCrmCompanyNameErrors, SetCrmCompanyNameResponses, SetCrmContactNameData, SetCrmContactNameErrors, SetCrmContactNameResponses, SetDocumentTeamShareData, SetDocumentTeamShareErrors, SetDocumentTeamShareResponses, SetEmailFollowupData, SetEmailFollowupErrors, SetEmailFollowupResponses, SetEmailSyncData, SetEmailSyncErrors, SetEmailSyncResponses, SimpleSaveData, SimpleSaveErrors, SimpleSaveResponses, StreamEventsData, StreamEventsErrors, StreamEventsResponse, StreamEventsResponses, ToggleShareWithTeamData, ToggleShareWithTeamErrors, ToggleShareWithTeamResponses, TranscribeDictationData, TranscribeDictationErrors, TranscribeDictationResponses, UnassignInitiativeTaskData, UnassignInitiativeTaskErrors, UnassignInitiativeTaskResponses, UpdateAgentData, UpdateAgentErrors, UpdateAgentResponses, UpdateInitiativeData, UpdateInitiativeErrors, UpdateInitiativeResponses, UpdateReminderData, UpdateReminderErrors, UpdateReminderResponses, UploadExtractFolderHandlerData, UploadExtractFolderHandlerErrors, UploadExtractFolderHandlerResponses, UploadFolderHandlerData, UploadFolderHandlerErrors, UploadFolderHandlerResponses, UpsertHistoryHandlerData, UpsertHistoryHandlerErrors, UpsertHistoryHandlerResponses, UpsertUserDocumentViewLocationData, UpsertUserDocumentViewLocationErrors, UpsertUserDocumentViewLocationResponses, ValidateDocumentPermissionsTokenData, ValidateDocumentPermissionsTokenErrors, ValidateDocumentPermissionsTokenResponses, ValidateWebhookData, ValidateWebhookErrors, ValidateWebhookResponses } from './types.gen';
+import type { AddFavoriteData, AddFavoriteErrors, AddFavoriteResponses, AddParticipantsData, AddParticipantsErrors, AddParticipantsResponses, AddPinHandlerData, AddPinHandlerErrors, AddPinHandlerResponses, ApproveHarnessPairingData, ApproveHarnessPairingErrors, ApproveHarnessPairingResponses, AssignInitiativeTasksData, AssignInitiativeTasksErrors, AssignInitiativeTasksResponses, BulkWakeupSyncServiceDocumentsData, BulkWakeupSyncServiceDocumentsErrors, BulkWakeupSyncServiceDocumentsResponses, CallWebhookData, CallWebhookErrors, CallWebhookResponses, CancelSlackImportData, CancelSlackImportErrors, CancelSlackImportResponses, CheckActiveCallData, CheckActiveCallErrors, CheckActiveCallResponses, ClaimHarnessPairingData, ClaimHarnessPairingErrors, ClaimHarnessPairingResponses, CompleteSlackImportUploadsData, CompleteSlackImportUploadsErrors, CompleteSlackImportUploadsResponses, CopyDocumentData, CopyDocumentErrors, CopyDocumentResponses, CreateAgentData, CreateAgentErrors, CreateAgentResponses, CreateAnchorData, CreateAnchorErrors, CreateAnchorResponses, CreateChannelData, CreateChannelErrors, CreateChannelLabelData, CreateChannelLabelErrors, CreateChannelLabelResponses, CreateChannelResponses, CreateChannelScopedBotData, CreateChannelScopedBotErrors, CreateChannelScopedBotResponses, CreateCollabSurfaceTokenData, CreateCollabSurfaceTokenErrors, CreateCollabSurfaceTokenResponses, CreateCrmCompanyData, CreateCrmCompanyErrors, CreateCrmCompanyResponses, CreateCrmContactData, CreateCrmContactErrors, CreateCrmContactResponses, CreateDocumentData, CreateDocumentErrors, CreateDocumentResponses, CreateEntityMentionData, CreateEntityMentionErrors, CreateEntityMentionResponses, CreateHarnessPairingData, CreateHarnessPairingErrors, CreateHarnessPairingResponses, CreateInitiativeData, CreateInitiativeErrors, CreateInitiativeResponses, CreateInstructionsHandlerData, CreateInstructionsHandlerErrors, CreateInstructionsHandlerResponses, CreateMarkdownHandlerData, CreateMarkdownHandlerErrors, CreateMarkdownHandlerResponses, CreateProjectHandlerData, CreateProjectHandlerErrors, CreateProjectHandlerResponses, CreateReminderData, CreateReminderErrors, CreateReminderResponses, CreateSkillHandlerData, CreateSkillHandlerErrors, CreateSkillHandlerResponses, CreateSlackImportData, CreateSlackImportErrors, CreateSlackImportResponses, CreateSnippetHandlerData, CreateSnippetHandlerErrors, CreateSnippetHandlerResponses, CreateTaskHandlerData, CreateTaskHandlerErrors, CreateTaskHandlerResponses, CreateUserApiKeyData, CreateUserApiKeyErrors, CreateUserApiKeyResponses, CreateViewHandlerData, CreateViewHandlerErrors, CreateViewHandlerResponses, CreateWebhookData, CreateWebhookErrors, CreateWebhookResponses, DeleteAnchorData, DeleteAnchorErrors, DeleteAnchorResponses, DeleteCallRecordData, DeleteCallRecordErrors, DeleteCallRecordResponses, DeleteChannelData, DeleteChannelErrors, DeleteChannelLabelData, DeleteChannelLabelErrors, DeleteChannelLabelResponses, DeleteChannelResponses, DeleteCollabSurfaceData, DeleteCollabSurfaceErrors, DeleteCollabSurfaceResponses, DeleteDocumentData, DeleteDocumentErrors, DeleteDocumentResponses, DeleteEntityMentionData, DeleteEntityMentionErrors, DeleteEntityMentionResponses, DeleteHarnessData, DeleteHarnessErrors, DeleteHarnessResponses, DeleteHistoryHandlerData, DeleteHistoryHandlerErrors, DeleteHistoryHandlerResponses, DeleteInitiativeData, DeleteInitiativeErrors, DeleteInitiativeResponses, DeleteProjectHandlerData, DeleteProjectHandlerErrors, DeleteProjectHandlerResponses, DeleteReminderData, DeleteReminderErrors, DeleteReminderResponses, DeleteSelfHarnessData, DeleteSelfHarnessErrors, DeleteSelfHarnessResponses, DeleteUserApiKeyData, DeleteUserApiKeyErrors, DeleteUserApiKeyResponses, DeleteUserDocumentViewLocationData, DeleteUserDocumentViewLocationErrors, DeleteUserDocumentViewLocationResponses, DeleteViewHandlerData, DeleteViewHandlerErrors, DeleteViewHandlerResponses, DeleteWebhookData, DeleteWebhookErrors, DeleteWebhookResponses, EditAnchorData, EditAnchorErrors, EditAnchorResponses, EditCallRecordData, EditCallRecordErrors, EditCallRecordResponses, EditCallTranscriptData, EditCallTranscriptErrors, EditCallTranscriptResponses, EditDocumentData, EditDocumentErrors, EditDocumentResponses, EditProjectV2Data, EditProjectV2Errors, EditProjectV2Responses, EditThreadV2Data, EditThreadV2Errors, EditThreadV2Responses, EnsureCollabSurfaceData, EnsureCollabSurfaceErrors, EnsureCollabSurfaceResponses, EntityMessageCreateData, EntityMessageCreateResponses, EntityMessageDeleteMessageData, EntityMessageDeleteMessageResponses, EntityMessageDeleteThreadData, EntityMessageDeleteThreadResponses, EntityMessageEditData, EntityMessageEditResponses, EntityMessageGetMessageData, EntityMessageGetMessageResponses, EntityMessageGetThreadData, EntityMessageGetThreadResponses, EntityMessageLegacyData, EntityMessageLegacyResponses, EntityMessagePatchThreadData, EntityMessagePatchThreadResponses, EntityMessageReactData, EntityMessageReactResponses, EntityMessageTypingData, EntityMessageTypingResponses, ExcludeDefaultViewHandlerData, ExcludeDefaultViewHandlerErrors, ExcludeDefaultViewHandlerResponses, ExportDocumentData, ExportDocumentErrors, ExportDocumentResponses, FinalizeSlackImportData, FinalizeSlackImportErrors, FinalizeSlackImportResponses, GetActiveCallsData, GetActiveCallsErrors, GetActiveCallsResponses, GetActivityData, GetActivityErrors, GetActivityResponses, GetAttachmentReferencesData, GetAttachmentReferencesErrors, GetAttachmentReferencesResponses, GetBatchCallRecordPreviewData, GetBatchCallRecordPreviewErrors, GetBatchCallRecordPreviewResponses, GetBatchChannelPreviewData, GetBatchChannelPreviewErrors, GetBatchChannelPreviewResponses, GetBatchPreviewHandlerData, GetBatchPreviewHandlerErrors, GetBatchPreviewHandlerResponses, GetBatchProjectPreviewData, GetBatchProjectPreviewErrors, GetBatchProjectPreviewResponses, GetBotOwnerProfilesData, GetBotOwnerProfilesErrors, GetBotOwnerProfilesResponses, GetCallRecordData, GetCallRecordErrors, GetCallRecordResponses, GetChannelAttachmentsData, GetChannelAttachmentsErrors, GetChannelAttachmentsResponses, GetChannelData, GetChannelErrors, GetChannelJoinLinkData, GetChannelJoinLinkErrors, GetChannelJoinLinkResponses, GetChannelParticipantsData, GetChannelParticipantsErrors, GetChannelParticipantsResponses, GetChannelResponses, GetChannelsData, GetChannelsErrors, GetChannelsResponses, GetCollabSurfaceData, GetCollabSurfaceErrors, GetCollabSurfaceResponses, GetCompanyData, GetCompanyErrors, GetCompanyResponses, GetContactByEmailData, GetContactByEmailErrors, GetContactByEmailResponses, GetContactData, GetContactErrors, GetContactResponses, GetCrmTeamSettingsData, GetCrmTeamSettingsErrors, GetCrmTeamSettingsResponses, GetDocumentAnchorsData, GetDocumentAnchorsErrors, GetDocumentAnchorsResponses, GetDocumentBranchNameData, GetDocumentBranchNameErrors, GetDocumentBranchNameResponses, GetDocumentByTeamSlugData, GetDocumentByTeamSlugErrors, GetDocumentByTeamSlugResponses, GetDocumentData, GetDocumentErrors, GetDocumentGithubPullRequestsData, GetDocumentGithubPullRequestsErrors, GetDocumentGithubPullRequestsResponses, GetDocumentListHandlerData, GetDocumentListHandlerErrors, GetDocumentListHandlerResponses, GetDocumentLocationV3Data, GetDocumentLocationV3Errors, GetDocumentLocationV3Responses, GetDocumentPermissionsTokenData, GetDocumentPermissionsTokenErrors, GetDocumentPermissionsTokenResponses, GetDocumentPermissionsV2Data, GetDocumentPermissionsV2Errors, GetDocumentPermissionsV2Responses, GetDocumentProcessingResultData, GetDocumentProcessingResultErrors, GetDocumentProcessingResultResponses, GetDocumentResponses, GetDocumentShortIdData, GetDocumentShortIdErrors, GetDocumentShortIdResponses, GetDocumentTeamShareData, GetDocumentTeamShareErrors, GetDocumentTeamShareResponses, GetDocumentVersionData, GetDocumentVersionErrors, GetDocumentVersionResponses, GetDocumentViewsHandlerData, GetDocumentViewsHandlerErrors, GetDocumentViewsHandlerResponses, GetEmailFollowupData, GetEmailFollowupErrors, GetEmailFollowupResponses, GetEntityPermissionData, GetEntityPermissionErrors, GetEntityPermissionResponses, GetForeignEntityBySourceData, GetForeignEntityBySourceErrors, GetForeignEntityBySourceResponses, GetForeignEntityData, GetForeignEntityErrors, GetForeignEntityResponses, GetHarnessPairingData, GetHarnessPairingErrors, GetHarnessPairingResponses, GetHistoryHandlerData, GetHistoryHandlerErrors, GetHistoryHandlerResponses, GetInitiativeData, GetInitiativeErrors, GetInitiativeResponses, GetInstructionsHandlerData, GetInstructionsHandlerErrors, GetInstructionsHandlerResponses, GetItemsSoupData, GetItemsSoupErrors, GetItemsSoupResponses, GetLocationHandlerData, GetLocationHandlerErrors, GetLocationHandlerResponses, GetOrCreateCallData, GetOrCreateCallErrors, GetOrCreateCallResponses, GetOrCreateDmData, GetOrCreateDmErrors, GetOrCreateDmResponses, GetOrCreatePrivateData, GetOrCreatePrivateErrors, GetOrCreatePrivateResponses, GetPendingProjectsHandlerData, GetPendingProjectsHandlerErrors, GetPendingProjectsHandlerResponses, GetPinsHandlerData, GetPinsHandlerErrors, GetPinsHandlerResponses, GetProjectContentHandlerData, GetProjectContentHandlerErrors, GetProjectContentHandlerResponses, GetProjectHandlerData, GetProjectHandlerErrors, GetProjectHandlerResponses, GetProjectPermissionsV2Data, GetProjectPermissionsV2Errors, GetProjectPermissionsV2Responses, GetProjectsHandlerData, GetProjectsHandlerErrors, GetProjectsHandlerResponses, GetProjectUserAccessLevelData, GetProjectUserAccessLevelErrors, GetProjectUserAccessLevelResponses, GetReminderData, GetReminderErrors, GetReminderResponses, GetRingStatusData, GetRingStatusErrors, GetRingStatusResponses, GetSelfBotData, GetSelfBotErrors, GetSelfBotResponses, GetSelfHarnessData, GetSelfHarnessErrors, GetSelfHarnessResponses, GetSlackImportData, GetSlackImportErrors, GetSlackImportResponses, GetSystemSkillsHandlerData, GetSystemSkillsHandlerErrors, GetSystemSkillsHandlerResponses, GetUserDocumentsHandlerData, GetUserDocumentsHandlerErrors, GetUserDocumentsHandlerResponses, GetUserDocumentViewLocationData, GetUserDocumentViewLocationErrors, GetUserDocumentViewLocationResponses, GetViewsHandlerData, GetViewsHandlerErrors, GetViewsHandlerResponses, GetWebhookData, GetWebhookErrors, GetWebhookResponses, HandlerData, HandlerErrors, HandlerResponses, HealthHandlerData, HealthHandlerResponses, IngestTranscriptData, IngestTranscriptErrors, IngestTranscriptResponses, InitializeUserDocumentsData, InitializeUserDocumentsErrors, InitializeUserDocumentsResponses, InstallSyncData, InstallSyncErrors, JobProcessingResultHandlerData, JobProcessingResultHandlerErrors, JobProcessingResultHandlerResponses, JoinChannelByCodeData, JoinChannelByCodeErrors, JoinChannelByCodeResponses, JoinChannelData, JoinChannelErrors, JoinChannelResponses, LeaveChannelData, LeaveChannelErrors, LeaveChannelResponses, LeaveOrEndCallData, LeaveOrEndCallErrors, LeaveOrEndCallResponses, ListAgentsData, ListAgentsErrors, ListAgentsResponses, ListBotChannelsData, ListBotChannelsErrors, ListBotChannelsResponses, ListChannelLabelsData, ListChannelLabelsErrors, ListChannelLabelsResponses, ListCompanyContactsData, ListCompanyContactsErrors, ListCompanyContactsResponses, ListFavoritesData, ListFavoritesErrors, ListFavoritesResponses, ListHarnessAgentsData, ListHarnessAgentsErrors, ListHarnessAgentsResponses, ListHarnessesData, ListHarnessesErrors, ListHarnessesResponses, ListHarnessSessionsData, ListHarnessSessionsErrors, ListHarnessSessionsResponses, ListInitiativesData, ListInitiativesErrors, ListInitiativesResponses, ListOccurrencesData, ListOccurrencesErrors, ListOccurrencesResponses, ListReminderCollectionData, ListReminderCollectionErrors, ListReminderCollectionResponses, ListRemindersData, ListRemindersErrors, ListRemindersResponses, ListSlackImportsData, ListSlackImportsResponses, ListTeamOutOfOfficeData, ListTeamOutOfOfficeErrors, ListTeamOutOfOfficeResponses, ListUserApiKeysData, ListUserApiKeysErrors, ListUserApiKeysResponses, ListWebhooksData, ListWebhooksErrors, ListWebhooksResponses, MeetingCancelData, MeetingCancelErrors, MeetingCancelPreparationData, MeetingCancelPreparationErrors, MeetingCancelPreparationResponses, MeetingCancelResponses, MeetingCreateData, MeetingCreateErrors, MeetingCreateResponses, MeetingGuestJoinData, MeetingGuestJoinErrors, MeetingGuestJoinResponses, MeetingGuestParticipantsData, MeetingGuestParticipantsErrors, MeetingGuestParticipantsResponses, MeetingInviteData, MeetingInviteErrors, MeetingInvitePermissionsData, MeetingInvitePermissionsErrors, MeetingInvitePermissionsResponses, MeetingInviteResponses, MeetingInviteUsersData, MeetingInviteUsersErrors, MeetingInviteUsersResponses, MeetingJoinData, MeetingJoinErrors, MeetingJoinResponses, MeetingLeaveData, MeetingLeaveErrors, MeetingLeaveResponses, MeetingListActiveData, MeetingListActiveErrors, MeetingListActiveResponses, MeetingListData, MeetingListErrors, MeetingListResponses, MeetingLookupData, MeetingLookupErrors, MeetingLookupResponses, MeetingParticipantsData, MeetingParticipantsErrors, MeetingParticipantsResponses, MeetingPrepareData, MeetingPrepareErrors, MeetingPrepareResponses, MeetingShareData, MeetingShareErrors, MeetingShareResponses, MeetingUpdateData, MeetingUpdateErrors, MeetingUpdateResponses, MentionPreviewsData, MentionPreviewsErrors, MentionPreviewsResponses, MessageTimelineData, MessageTimelineResponses, PatchChannelData, PatchChannelErrors, PatchChannelResponses, PatchViewHandlerData, PatchViewHandlerErrors, PatchViewHandlerResponses, PatchWebhookData, PatchWebhookErrors, PatchWebhookResponses, PermanentlyDeleteDocumentData, PermanentlyDeleteDocumentErrors, PermanentlyDeleteDocumentResponses, PermanentlyDeleteProjectData, PermanentlyDeleteProjectErrors, PermanentlyDeleteProjectResponses, PostActivityData, PostActivityErrors, PostActivityResponses, PostChannelBotWebhookData, PostChannelBotWebhookErrors, PostChannelBotWebhookResponses, PostItemsSoupAstData, PostItemsSoupAstErrors, PostItemsSoupAstGroupedData, PostItemsSoupAstGroupedErrors, PostItemsSoupAstGroupedResponses, PostItemsSoupAstResponses, PostItemsSoupData, PostItemsSoupErrors, PostItemsSoupResponses, PresaveDocumentHandlerData, PresaveDocumentHandlerErrors, PresaveDocumentHandlerResponses, PreviewSmartTagData, PreviewSmartTagErrors, PreviewSmartTagResponses, PutCrmTeamSettingsData, PutCrmTeamSettingsErrors, PutCrmTeamSettingsResponses, PutCrmTeamStagesData, PutCrmTeamStagesErrors, PutCrmTeamStagesResponses, RecentlyDeletedData, RecentlyDeletedErrors, RecentlyDeletedResponses, RegisterSlackImportUploadsData, RegisterSlackImportUploadsErrors, RegisterSlackImportUploadsResponses, RemoveBotFromChannelByBotData, RemoveBotFromChannelByBotErrors, RemoveBotFromChannelByBotResponses, RemoveFavoriteByEntityData, RemoveFavoriteByEntityErrors, RemoveFavoriteByEntityResponses, RemoveParticipantsData, RemoveParticipantsErrors, RemoveParticipantsResponses, RemovePinHandlerData, RemovePinHandlerErrors, RemovePinHandlerResponses, RenameChannelLabelData, RenameChannelLabelErrors, RenameChannelLabelResponses, ReorderFavoritesData, ReorderFavoritesErrors, ReorderFavoritesResponses, ReorderPinsHandlerData, ReorderPinsHandlerErrors, ReorderPinsHandlerResponses, ResetCrmTeamStagesData, ResetCrmTeamStagesErrors, ResetCrmTeamStagesResponses, RevertDeleteDocumentData, RevertDeleteDocumentErrors, RevertDeleteDocumentResponses, RevertDeleteProjectData, RevertDeleteProjectErrors, RevertDeleteProjectResponses, SaveDocumentHandlerData, SaveDocumentHandlerErrors, SaveDocumentHandlerResponses, SearchContactsData, SearchContactsErrors, SearchContactsResponses, SetChannelLabelData, SetChannelLabelErrors, SetChannelLabelResponses, SetChannelPictureData, SetChannelPictureErrors, SetChannelPictureResponses, SetCompanyHiddenData, SetCompanyHiddenErrors, SetCompanyHiddenResponses, SetContactHiddenData, SetContactHiddenErrors, SetContactHiddenResponses, SetCrmCompanyNameData, SetCrmCompanyNameErrors, SetCrmCompanyNameResponses, SetCrmContactNameData, SetCrmContactNameErrors, SetCrmContactNameResponses, SetDocumentTeamShareData, SetDocumentTeamShareErrors, SetDocumentTeamShareResponses, SetEmailFollowupData, SetEmailFollowupErrors, SetEmailFollowupResponses, SetEmailSyncData, SetEmailSyncErrors, SetEmailSyncResponses, SimpleSaveData, SimpleSaveErrors, SimpleSaveResponses, StreamEventsData, StreamEventsErrors, StreamEventsResponse, StreamEventsResponses, ToggleShareWithTeamData, ToggleShareWithTeamErrors, ToggleShareWithTeamResponses, TranscribeDictationData, TranscribeDictationErrors, TranscribeDictationResponses, UnassignInitiativeTaskData, UnassignInitiativeTaskErrors, UnassignInitiativeTaskResponses, UpdateAgentData, UpdateAgentErrors, UpdateAgentResponses, UpdateInitiativeData, UpdateInitiativeErrors, UpdateInitiativeResponses, UpdateReminderData, UpdateReminderErrors, UpdateReminderResponses, UploadExtractFolderHandlerData, UploadExtractFolderHandlerErrors, UploadExtractFolderHandlerResponses, UploadFolderHandlerData, UploadFolderHandlerErrors, UploadFolderHandlerResponses, UpsertHistoryHandlerData, UpsertHistoryHandlerErrors, UpsertHistoryHandlerResponses, UpsertUserDocumentViewLocationData, UpsertUserDocumentViewLocationErrors, UpsertUserDocumentViewLocationResponses, ValidateDocumentPermissionsTokenData, ValidateDocumentPermissionsTokenErrors, ValidateDocumentPermissionsTokenResponses, ValidateWebhookData, ValidateWebhookErrors, ValidateWebhookResponses } from './types.gen';
export type Options<TData extends TDataShape = TDataShape, ThrowOnError extends boolean = boolean, TResponse = unknown> = Options2<TData, ThrowOnError, TResponse> & {
/**
@@ -2247,6 +2247,81 @@ export class Sdk extends HeyApiClient {
});
}
+ /**
+ * List this administrator's team receipts, effective server limits and source binding.
+ */
+ public listSlackImports<ThrowOnError extends boolean = false>(options?: Options<ListSlackImportsData, ThrowOnError>): RequestResult<ListSlackImportsResponses, unknown, ThrowOnError> {
+ return (options?.client ?? this.client).get<ListSlackImportsResponses, unknown, ThrowOnError>({ url: '/slack/imports', ...options });
+ }
+
+ /**
+ * Create or replay a job using a scoped idempotency token and full selected metadata.
+ * Missing creation timestamps and unknown message counts are represented as null.
+ */
+ public createSlackImport<ThrowOnError extends boolean = false>(options: Options<CreateSlackImportData, ThrowOnError>): RequestResult<CreateSlackImportResponses, CreateSlackImportErrors, ThrowOnError> {
+ return (options.client ?? this.client).post<CreateSlackImportResponses, CreateSlackImportErrors, ThrowOnError>({
+ url: '/slack/imports',
+ ...options,
+ headers: {
+ 'Content-Type': 'application/json',
+ ...options.headers
+ }
+ });
+ }
+
+ /**
+ * Read sanitized progress; foreign jobs are indistinguishable from missing jobs.
+ */
+ public getSlackImport<ThrowOnError extends boolean = false>(options: Options<GetSlackImportData, ThrowOnError>): RequestResult<GetSlackImportResponses, GetSlackImportErrors, ThrowOnError> {
+ return (options.client ?? this.client).get<GetSlackImportResponses, GetSlackImportErrors, ThrowOnError>({ url: '/slack/imports/{job_id}', ...options });
+ }
+
+ /**
+ * Stop unclaimed work. Active leases may settle; cancellation does not roll back history.
+ */
+ public cancelSlackImport<ThrowOnError extends boolean = false>(options: Options<CancelSlackImportData, ThrowOnError>): RequestResult<CancelSlackImportResponses, CancelSlackImportErrors, ThrowOnError> {
+ return (options.client ?? this.client).post<CancelSlackImportResponses, CancelSlackImportErrors, ThrowOnError>({ url: '/slack/imports/{job_id}/cancel', ...options });
+ }
+
+ /**
+ * Close registration idempotently; never-ready conversations are skipped, not queued work.
+ */
+ public finalizeSlackImport<ThrowOnError extends boolean = false>(options: Options<FinalizeSlackImportData, ThrowOnError>): RequestResult<FinalizeSlackImportResponses, FinalizeSlackImportErrors, ThrowOnError> {
+ return (options.client ?? this.client).post<FinalizeSlackImportResponses, FinalizeSlackImportErrors, ThrowOnError>({ url: '/slack/imports/{job_id}/finalize', ...options });
+ }
+
+ /**
+ * Issue short-lived, checksum/length-bound, create-only PUT grants. Do not log or persist URLs.
+ * Retry a PUT returning 412 by completing/verifying the existing object, not overwriting it.
+ */
+ public registerSlackImportUploads<ThrowOnError extends boolean = false>(options: Options<RegisterSlackImportUploadsData, ThrowOnError>): RequestResult<RegisterSlackImportUploadsResponses, RegisterSlackImportUploadsErrors, ThrowOnError> {
+ return (options.client ?? this.client).post<RegisterSlackImportUploadsResponses, RegisterSlackImportUploadsErrors, ThrowOnError>({
+ url: '/slack/imports/{job_id}/uploads',
+ ...options,
+ headers: {
+ 'Content-Type': 'application/json',
+ ...options.headers
+ }
+ });
+ }
+
+ /**
+ * Verify storage checksums and lengths for registered objects. A seal fixes the ENTIRE
+ * descriptor set (contiguous indices, sizes and hashes), not just this completion batch.
+ * Empty uploads with a zero-part seal support shape-only or empty-history imports.
+ * Work becomes ready only after verified users metadata, a seal and every required part.
+ */
+ public completeSlackImportUploads<ThrowOnError extends boolean = false>(options: Options<CompleteSlackImportUploadsData, ThrowOnError>): RequestResult<CompleteSlackImportUploadsResponses, CompleteSlackImportUploadsErrors, ThrowOnError> {
+ return (options.client ?? this.client).post<CompleteSlackImportUploadsResponses, CompleteSlackImportUploadsErrors, ThrowOnError>({
+ url: '/slack/imports/{job_id}/uploads/complete',
+ ...options,
+ headers: {
+ 'Content-Type': 'application/json',
+ ...options.headers
+ }
+ });
+ }
+
public bulkWakeupSyncServiceDocuments<ThrowOnError extends boolean = false>(options: Options<BulkWakeupSyncServiceDocumentsData, ThrowOnError>): RequestResult<BulkWakeupSyncServiceDocumentsResponses, BulkWakeupSyncServiceDocumentsErrors, ThrowOnError> {
return (options.client ?? this.client).post<BulkWakeupSyncServiceDocumentsResponses, BulkWakeupSyncServiceDocumentsErrors, ThrowOnError>({
url: '/sync_service/wakeup',
diff --git a/packages/sdk/generated/storage/types.gen.ts b/packages/sdk/generated/storage/types.gen.ts
index a27e8d8f12..5bd398f842 100644
--- a/packages/sdk/generated/storage/types.gen.ts
+++ b/packages/sdk/generated/storage/types.gen.ts
@@ -2619,6 +2619,17 @@ export type CommandRejectedMetadata = {
identity: SessionIdentity;
};
+/**
+ * Explicit upload completion; a seal can be submitted separately with an empty list.
+ */
+export type CompleteUploads = {
+ seal?: null | ConversationSeal;
+ /**
+ * Registered identities to verify against storage (at most 50).
+ */
+ uploads: Array<UploadId>;
+};
+
/**
* The conferencing system backing an event's join URL.
*
@@ -2635,6 +2646,126 @@ export type CommandRejectedMetadata = {
*/
export type ConferenceProvider = 'google_meet' | 'other';
+/**
+ * Source Slack conversation ID (C, G or D prefix); unique only within a source.
+ */
+export type ConversationId = string;
+
+/**
+ * Slack conversation kind. Public Slack channels map to Macro Team, never Public.
+ */
+export type ConversationKind = 'public_channel' | 'private_channel' | 'direct_message' | 'group_direct_message';
+
+/**
+ * Full selected conversation metadata, persisted before granting any uploads.
+ */
+export type ConversationMetadata = {
+ /**
+ * Source archived flag (does not silently archive a reused Macro target).
+ */
+ archived: boolean;
+ createdAt?: null | SlackTimestamp;
+ creatorId?: null | SlackUserId;
+ /**
+ * Single archive folder name, not a backend object key.
+ */
+ folder: KeySegment;
+ /**
+ * Source conversation kind.
+ */
+ kind: ConversationKind;
+ /**
+ * Complete source member list, including members unknown to Macro.
+ */
+ memberIds: Array<SlackUserId>;
+ /**
+ * Advisory only; null means not yet counted, not zero.
+ */
+ messageCount?: number | null;
+ /**
+ * Original display name; not a storage key.
+ */
+ name: string;
+ /**
+ * Stable Slack identity.
+ */
+ slackChannelId: ConversationId;
+};
+
+/**
+ * Admin-visible progress for one selected conversation.
+ */
+export type ConversationProgress = {
+ /**
+ * Whether the selected source conversation was archived.
+ */
+ archived: boolean;
+ /**
+ * Authorized target only; absent for inaccessible reused targets.
+ */
+ channelId?: string | null;
+ /**
+ * Durably committed record counts.
+ */
+ counters: ImportCounters;
+ error?: null | ImportError;
+ /**
+ * Persisted source kind.
+ */
+ kind: ConversationKind;
+ /**
+ * Persisted source display name, never the reused Macro target's name.
+ */
+ name: string;
+ /**
+ * Null until sealed; zero is a valid sealed empty manifest.
+ */
+ partCount?: number | null;
+ /**
+ * Independent indexing state.
+ */
+ search: SearchState;
+ /**
+ * Source identity (always visible to the importing team's administrator).
+ */
+ slackChannelId: ConversationId;
+ /**
+ * Durable state.
+ */
+ status: ConversationStatus;
+ /**
+ * Number of parts whose object identity has been verified.
+ */
+ verifiedParts: number;
+ /**
+ * Non-fatal metadata/skip explanations.
+ */
+ warnings: Array<ImportWarning>;
+};
+
+/**
+ * Complete immutable expected part set for one conversation, including zero parts.
+ */
+export type ConversationSeal = {
+ /**
+ * SHA-256 of canonical descriptor lines; zero parts hash the empty byte string.
+ */
+ manifestSha256: Sha256Digest;
+ /**
+ * Exact number of registered parts, starting at index zero.
+ */
+ partCount: number;
+ /**
+ * Selected source conversation.
+ */
+ slackChannelId: ConversationId;
+};
+
+/**
+ * Durable per-conversation lifecycle.
+ */
+export type ConversationStatus = 'awaiting_uploads' | 'queued' | 'importing' | 'completed' | 'skipped' | 'failed';
+
/**
* Query parameters for the copy document endpoint.
*/
@@ -3066,6 +3197,28 @@ export type CreateEntityMentionResponse = {
user_id?: string | null;
};
+/**
+ * Create command. Team identity is deliberately absent.
+ */
+export type CreateImport = {
+ /**
+ * Full, unique selected conversations (at most the configured bound).
+ */
+ conversations: Array<ConversationMetadata>;
+ /**
+ * Replay with the same semantic payload returns the original job; changes conflict.
+ */
+ idempotencyToken: CreateToken;
+ /**
+ * Default true at the client; false still requires users and zero-part seals.
+ */
+ includeMessageHistory: boolean;
+ /**
+ * Binding or explicit unknown-source confirmation.
+ */
+ source: SourceIdentity;
+};
+
/**
* Create-initiative HTTP body.
*/
@@ -3348,6 +3501,11 @@ export type CreateTaskResponse = {
token: string;
};
+/**
+ * Client-generated create idempotency token, scoped to team and administrator.
+ */
+export type CreateToken = string;
+
export type CreateUnthreadedAnchorRequest = CreateUnthreadedPdfAnchorRequest & {
fileType: 'pdf';
};
@@ -5984,6 +6142,157 @@ export type HashMap = {
export type HighlightType = 1 | 2 | 3;
+/**
+ * Committed counters, never optimistic browser or uncommitted worker counts.
+ */
+export type ImportCounters = {
+ /**
+ * Previously committed source identities.
+ */
+ duplicates: number;
+ /**
+ * Newly persisted messages.
+ */
+ imported: number;
+ /**
+ * Source records examined through the committed checkpoint.
+ */
+ processed: number;
+ /**
+ * Newly persisted reactions.
+ */
+ reactions: number;
+ /**
+ * Unsupported, empty or otherwise deliberately skipped records.
+ */
+ skipped: number;
+};
+
+/**
+ * Public error codes; never carry raw provider errors, keys, emails or source text.
+ */
+export type ImportError = 'disabled' | 'invalid_input' | 'limit_exceeded' | 'unavailable' | 'admin_required' | 'source_mismatch' | 'conflict' | 'upload_mismatch' | 'lease_lost' | 'retryable' | 'internal';
+
+/**
+ * Configurable bounds shared with browser staging and enforced again by the worker.
+ */
+export type ImportLimits = {
+ /**
+ * Maximum selected conversations.
+ */
+ conversations: number;
+ /**
+ * Maximum historical batch payload bytes.
+ */
+ databaseBatchBytes: number;
+ /**
+ * Maximum messages in a historical batch.
+ */
+ databaseBatchMessages: number;
+ /**
+ * Maximum users, root metadata or individual day JSON bytes.
+ */
+ jsonBytes: number;
+ /**
+ * Maximum NDJSON part bytes.
+ */
+ partBytes: number;
+ /**
+ * Maximum records per part.
+ */
+ partRecords: number;
+ /**
+ * Maximum bytes per NDJSON record, including newline.
+ */
+ recordBytes: number;
+ /**
+ * Maximum descriptors or completion identities per call.
+ */
+ registrationBatch: number;
+ /**
+ * Maximum selected temporary data bytes across all uploads.
+ */
+ selectedBytes: number;
+ /**
+ * Maximum ZIP entries scanned by the browser.
+ */
+ zipEntries: number;
+};
+
+/**
+ * Admin list result, also supplying upload limits before a job exists.
+ */
+export type ImportPage = {
+ /**
+ * At most 50 job receipts, newest first.
+ */
+ jobs: Array<ImportProgress>;
+ /**
+ * Effective server limits.
+ */
+ limits: ImportLimits;
+ nextCursor?: null | JobId;
+ /**
+ * Binding summary used by the picker confirmation.
+ */
+ sourceBinding: SourceBinding;
+};
+
+/**
+ * Common receipt returned by create, completion, finalize, cancel and progress.
+ */
+export type ImportProgress = {
+ /**
+ * All selected conversations, bounded by the create limit.
+ */
+ conversations: Array<ConversationProgress>;
+ /**
+ * Persisted creation time, also the final historical-time fallback.
+ */
+ createdAt: string;
+ /**
+ * Immutable history option confirmed when creating this job.
+ */
+ includeMessageHistory: boolean;
+ /**
+ * Stable job identity.
+ */
+ jobId: JobId;
+ /**
+ * Server bounds for staging and registration.
+ */
+ limits: ImportLimits;
+ /**
+ * Registration closure time; cancellation also closes registration.
+ */
+ registrationClosedAt?: string | null;
+ /**
+ * Monotonic job revision for polling/websocket invalidation.
+ */
+ revision: number;
+ /**
+ * Immutable source confirmation from this job, not the current team binding.
+ */
+ source: SourceIdentity;
+ /**
+ * Durable job lifecycle.
+ */
+ status: JobStatus;
+ /**
+ * Last durable lifecycle/progress update.
+ */
+ updatedAt: string;
+ /**
+ * Whether users metadata is verified and pinned.
+ */
+ usersVerified: boolean;
+};
+
+/**
+ * Non-fatal, sanitized explanations displayed in admin progress.
+ */
+export type ImportWarning = 'creation_time_from_message' | 'creation_time_from_job' | 'unresolvable_direct_message' | 'target_unavailable' | 'uploads_incomplete';
+
/**
* Display attribution for a comment imported from an external document.
*/
@@ -6200,6 +6509,21 @@ export type ItemWithUserAccessLevel = {
userAccessLevel: AccessLevel;
};
+/**
+ * Import job identity. Generate UUIDv7 in application code.
+ */
+export type JobId = string;
+
+/**
+ * Job lifecycle. Cancellation remains in progress while any lease is active.
+ */
+export type JobStatus = 'uploading' | 'processing' | 'completed' | 'completed_with_errors' | 'failed' | 'cancelling' | 'cancelled';
+
+/**
+ * Single safe source folder/key segment; never a path or authorization proof.
+ */
+export type KeySegment = string;
+
/**
* Latest-message bundle for soup payloads.
*/
@@ -7778,6 +8102,16 @@ export type RecentlyDeletedResponseData = {
items: Array<Item>;
};
+/**
+ * Batched registration, also used to renew an unchanged descriptor's grant.
+ */
+export type RegisterUploads = {
+ /**
+ * At most 50 descriptors; no duplicate identities in a call.
+ */
+ descriptors: Array<UploadDescriptor>;
+};
+
/**
* A reminder belonging to a user.
*
@@ -8099,6 +8433,26 @@ export type SearchContactsResponse = {
contacts: Array<CrmContactResponse>;
};
+/**
+ * Search publication/receipt state. Acceptance is not completed publication;
+ * even completed publication still awaits consumer indexing and refresh.
+ */
+export type SearchState = {
+ status: 'not_needed';
+} | {
+ status: 'pending';
+} | {
+ /**
+ * Receipt from the search service.
+ */
+ receiptId: string;
+ status: 'submitted';
+} | {
+ status: 'completed';
+} | {
+ status: 'failed';
+};
+
/**
* The session was deleted.
*/
@@ -8359,6 +8713,11 @@ export type SetPropertyValue = {
urls: Array<string>;
};
+/**
+ * SHA-256 encoded as exactly 64 lowercase hexadecimal characters.
+ */
+export type Sha256Digest = string;
+
export type SharePermissionV2 = {
/**
* The channel share permissions for the item
@@ -8406,6 +8765,32 @@ export type SimpleMention = {
entity_type: string;
};
+/**
+ * At most fifty registered identities to verify and an optional immutable conversation seal.
+ */
+export type SlackCompleteRequest = CompleteUploads;
+
+/**
+ * Bounded full-metadata create payload. Domain validation applies effective limits again.
+ */
+export type SlackCreateRequest = CreateImport;
+
+/**
+ * At most fifty immutable upload descriptors, without client-supplied storage keys.
+ */
+export type SlackRegisterRequest = RegisterUploads;
+
+/**
+ * Slack time represented exactly as nonnegative Unix microseconds.
+ * JSON is a string with six fractional digits, never a floating-point number.
+ */
+export type SlackTimestamp = string;
+
+/**
+ * Slack member identity (U or W prefix), including USLACKBOT.
+ */
+export type SlackUserId = string;
+
/**
* A channel visible to the caller that matches a smart tag rule.
*/
@@ -9768,6 +10153,39 @@ export type SoupThreadReply = {
updated_at: string;
};
+/**
+ * Team's durable single-source binding, including confirmed unidentified archives.
+ */
+export type SourceBinding = {
+ kind: 'unbound';
+} | {
+ kind: 'confirmed_unknown';
+} | {
+ kind: 'known';
+ /**
+ * Bound source identity.
+ */
+ sourceId: SourceId;
+};
+
+/**
+ * Known Slack workspace identity (T prefix); an enterprise ID alone is insufficient.
+ */
+export type SourceId = string;
+
+/**
+ * Source identity supplied when creating a job. One binding per Macro team in v1.
+ */
+export type SourceIdentity = {
+ kind: 'known';
+ /**
+ * Workspace identifier in the archive.
+ */
+ sourceId: SourceId;
+} | {
+ kind: 'confirmed_unknown';
+};
+
/**
* The deterministic starter document ids for the current user.
*/
@@ -10385,6 +10803,28 @@ export type UpdateSharePermissionRequestV2 = {
teamShareAccessLevel?: null | AccessLevel;
};
+/**
+ * Immutable expected object properties, registered before signing an upload.
+ */
+export type UploadDescriptor = {
+ /**
+ * Exact byte length including delimiters.
+ */
+ byteLength: number;
+ /**
+ * Exact NDJSON record count; null for the users JSON payload.
+ */
+ recordCount?: number | null;
+ /**
+ * SHA-256 of the exact uploaded bytes, not the ETag.
+ */
+ sha256: Sha256Digest;
+ /**
+ * Manifest identity within this job.
+ */
+ upload: UploadId;
+};
+
/**
* The upload status of the document
*/
@@ -10417,6 +10857,48 @@ export type UploadFolderRequest = {
uploadRequestId: string;
};
+/**
+ * Signed upload permission returned just before PUT; never persisted as an identity.
+ */
+export type UploadGrant = {
+ /**
+ * Exact immutable descriptor being signed.
+ */
+ descriptor: UploadDescriptor;
+ /**
+ * Grant expiry; callers renew by re-registering the identical descriptor.
+ */
+ expiresAt: string;
+ /**
+ * Required signed upload headers (including checksum, content type and create-only
+ * condition). Browser-forbidden Content-Length is derived from the exact Blob bytes.
+ */
+ requiredHeaders: {
+ [key: string]: string;
+ };
+ /**
+ * Short-lived signed destination; must not be logged.
+ */
+ url: string;
+};
+
+/**
+ * Identity in a job's persisted manifest. No user-supplied object keys.
+ */
+export type UploadId = {
+ kind: 'users';
+} | {
+ kind: 'conversation_part';
+ /**
+ * Zero-based part index; sealed manifests must be contiguous.
+ */
+ partIndex: number;
+ /**
+ * Selected Slack conversation.
+ */
+ slackChannelId: ConversationId;
+};
+
export type UpsertUserDocumentViewLocationRequest = {
location: string;
};
@@ -16251,6 +16733,187 @@ export type PatchViewHandlerResponses = {
200: unknown;
};
+export type ListSlackImportsData = {
+ body?: never;
+ path?: never;
+ query?: {
+ before?: JobId;
+ };
+ url: '/slack/imports';
+};
+
+export type ListSlackImportsResponses = {
+ 200: ImportPage;
+};
+
+export type ListSlackImportsResponse = ListSlackImportsResponses[keyof ListSlackImportsResponses];
+
+export type CreateSlackImportData = {
+ body: SlackCreateRequest;
+ path?: never;
+ query?: never;
+ url: '/slack/imports';
+};
+
+export type CreateSlackImportErrors = {
+ /**
+ * Authentication or team administrator access required
+ */
+ 401: unknown;
+ /**
+ * Team administrator required
+ */
+ 403: unknown;
+ /**
+ * Metadata limit exceeded
+ */
+ 413: unknown;
+ /**
+ * Slack imports disabled or temporarily unavailable
+ */
+ 503: unknown;
+};
+
+export type CreateSlackImportResponses = {
+ 200: ImportProgress;
+};
+
+export type CreateSlackImportResponse = CreateSlackImportResponses[keyof CreateSlackImportResponses];
+
+export type GetSlackImportData = {
+ body?: never;
+ path: {
+ job_id: JobId;
+ };
+ query?: never;
+ url: '/slack/imports/{job_id}';
+};
+
+export type GetSlackImportErrors = {
+ /**
+ * Unknown or inaccessible job
+ */
+ 404: unknown;
+};
+
+export type GetSlackImportResponses = {
+ 200: ImportProgress;
+};
+
+export type GetSlackImportResponse = GetSlackImportResponses[keyof GetSlackImportResponses];
+
+export type CancelSlackImportData = {
+ body?: never;
+ path: {
+ job_id: JobId;
+ };
+ query?: never;
+ url: '/slack/imports/{job_id}/cancel';
+};
+
+export type CancelSlackImportErrors = {
+ /**
+ * Unknown or inaccessible job
+ */
+ 404: unknown;
+};
+
+export type CancelSlackImportResponses = {
+ 200: ImportProgress;
+};
+
+export type CancelSlackImportResponse = CancelSlackImportResponses[keyof CancelSlackImportResponses];
+
+export type FinalizeSlackImportData = {
+ body?: never;
+ path: {
+ job_id: JobId;
+ };
+ query?: never;
+ url: '/slack/imports/{job_id}/finalize';
+};
+
+export type FinalizeSlackImportErrors = {
+ /**
+ * Unknown or inaccessible job
+ */
+ 404: unknown;
+};
+
+export type FinalizeSlackImportResponses = {
+ 200: ImportProgress;
+};
+
+export type FinalizeSlackImportResponse = FinalizeSlackImportResponses[keyof FinalizeSlackImportResponses];
+
+export type RegisterSlackImportUploadsData = {
+ body: SlackRegisterRequest;
+ path: {
+ job_id: JobId;
+ };
+ query?: never;
+ url: '/slack/imports/{job_id}/uploads';
+};
+
+export type RegisterSlackImportUploadsErrors = {
+ /**
+ * Unknown or inaccessible job
+ */
+ 404: unknown;
+ /**
+ * Body limit exceeded
+ */
+ 413: unknown;
+ /**
+ * Slack imports disabled
+ */
+ 503: unknown;
+};
+
+export type RegisterSlackImportUploadsResponses = {
+ 200: Array<UploadGrant>;
+};
+
+export type RegisterSlackImportUploadsResponse = RegisterSlackImportUploadsResponses[keyof RegisterSlackImportUploadsResponses];
+
+export type CompleteSlackImportUploadsData = {
+ body: SlackCompleteRequest;
+ path: {
+ job_id: JobId;
+ };
+ query?: never;
+ url: '/slack/imports/{job_id}/uploads/complete';
+};
+
+export type CompleteSlackImportUploadsErrors = {
+ /**
+ * Unknown or inaccessible job
+ */
+ 404: unknown;
+ /**
+ * Immutable manifest conflict
+ */
+ 409: unknown;
+ /**
+ * Body limit exceeded
+ */
+ 413: unknown;
+ /**
+ * Object verification failed
+ */
+ 422: unknown;
+ /**
+ * Slack imports disabled
+ */
+ 503: unknown;
+};
+
+export type CompleteSlackImportUploadsResponses = {
+ 200: ImportProgress;
+};
+
+export type CompleteSlackImportUploadsResponse = CompleteSlackImportUploadsResponses[keyof CompleteSlackImportUploadsResponses];
+
export type BulkWakeupSyncServiceDocumentsData = {
body: BulkWakeupRequest;
path?: never;
diff --git a/packages/sdk/specs/cognition.json b/packages/sdk/specs/cognition.json
index a97e82efa5..734087bc99 100644
--- a/packages/sdk/specs/cognition.json
+++ b/packages/sdk/specs/cognition.json
@@ -3811,7 +3811,7 @@
"Initiator": {
"type": "string",
"description": "Where an import entity was first staged from. Provenance only — never a\nvisibility filter.",
- "enum": ["onboarding", "chat"]
+ "enum": ["onboarding", "chat", "archive"]
},
"JwtPayload": {
"type": "object",
diff --git a/packages/sdk/specs/storage.json b/packages/sdk/specs/storage.json
index 73a22bb2b4..ad3f56f920 100644
--- a/packages/sdk/specs/storage.json
+++ b/packages/sdk/specs/storage.json
@@ -13352,6 +13352,275 @@
}
}
},
+ "/slack/imports": {
+ "get": {
+ "tags": ["slack"],
+ "summary": "List this administrator's team receipts, effective server limits and source binding.",
+ "operationId": "list_slack_imports",
+ "parameters": [
+ {
+ "name": "before",
+ "in": "query",
+ "required": false,
+ "schema": {
+ "$ref": "#/components/schemas/JobId"
+ }
+ }
+ ],
+ "responses": {
+ "200": {
+ "description": "",
+ "content": {
+ "application/json": {
+ "schema": {
+ "$ref": "#/components/schemas/ImportPage"
+ }
+ }
+ }
+ }
+ }
+ },
+ "post": {
+ "tags": ["slack"],
+ "summary": "Create or replay a job using a scoped idempotency token and full selected metadata.\nMissing creation timestamps and unknown message counts are represented as null.",
+ "operationId": "create_slack_import",
+ "requestBody": {
+ "content": {
+ "application/json": {
+ "schema": {
+ "$ref": "#/components/schemas/SlackCreateRequest"
+ }
+ }
+ },
+ "required": true
+ },
+ "responses": {
+ "200": {
+ "description": "",
+ "content": {
+ "application/json": {
+ "schema": {
+ "$ref": "#/components/schemas/ImportProgress"
+ }
+ }
+ }
+ },
+ "401": {
+ "description": "Authentication or team administrator access required"
+ },
+ "403": {
+ "description": "Team administrator required"
+ },
+ "413": {
+ "description": "Metadata limit exceeded"
+ },
+ "503": {
+ "description": "Slack imports disabled or temporarily unavailable"
+ }
+ }
+ }
+ },
+ "/slack/imports/{job_id}": {
+ "get": {
+ "tags": ["slack"],
+ "summary": "Read sanitized progress; foreign jobs are indistinguishable from missing jobs.",
+ "operationId": "get_slack_import",
+ "parameters": [
+ {
+ "name": "job_id",
+ "in": "path",
+ "required": true,
+ "schema": {
+ "$ref": "#/components/schemas/JobId"
+ }
+ }
+ ],
+ "responses": {
+ "200": {
+ "description": "",
+ "content": {
+ "application/json": {
+ "schema": {
+ "$ref": "#/components/schemas/ImportProgress"
+ }
+ }
+ }
+ },
+ "404": {
+ "description": "Unknown or inaccessible job"
+ }
+ }
+ }
+ },
+ "/slack/imports/{job_id}/cancel": {
+ "post": {
+ "tags": ["slack"],
+ "summary": "Stop unclaimed work. Active leases may settle; cancellation does not roll back history.",
+ "operationId": "cancel_slack_import",
+ "parameters": [
+ {
+ "name": "job_id",
+ "in": "path",
+ "required": true,
+ "schema": {
+ "$ref": "#/components/schemas/JobId"
+ }
+ }
+ ],
+ "responses": {
+ "200": {
+ "description": "",
+ "content": {
+ "application/json": {
+ "schema": {
+ "$ref": "#/components/schemas/ImportProgress"
+ }
+ }
+ }
+ },
+ "404": {
+ "description": "Unknown or inaccessible job"
+ }
+ }
+ }
+ },
+ "/slack/imports/{job_id}/finalize": {
+ "post": {
+ "tags": ["slack"],
+ "summary": "Close registration idempotently; never-ready conversations are skipped, not queued work.",
+ "operationId": "finalize_slack_import",
+ "parameters": [
+ {
+ "name": "job_id",
+ "in": "path",
+ "required": true,
+ "schema": {
+ "$ref": "#/components/schemas/JobId"
+ }
+ }
+ ],
+ "responses": {
+ "200": {
+ "description": "",
+ "content": {
+ "application/json": {
+ "schema": {
+ "$ref": "#/components/schemas/ImportProgress"
+ }
+ }
+ }
+ },
+ "404": {
+ "description": "Unknown or inaccessible job"
+ }
+ }
+ }
+ },
+ "/slack/imports/{job_id}/uploads": {
+ "post": {
+ "tags": ["slack"],
+ "summary": "Issue short-lived, checksum/length-bound, create-only PUT grants. Do not log or persist URLs.\nRetry a PUT returning 412 by completing/verifying the existing object, not overwriting it.",
+ "operationId": "register_slack_import_uploads",
+ "parameters": [
+ {
+ "name": "job_id",
+ "in": "path",
+ "required": true,
+ "schema": {
+ "$ref": "#/components/schemas/JobId"
+ }
+ }
+ ],
+ "requestBody": {
+ "content": {
+ "application/json": {
+ "schema": {
+ "$ref": "#/components/schemas/SlackRegisterRequest"
+ }
+ }
+ },
+ "required": true
+ },
+ "responses": {
+ "200": {
+ "description": "",
+ "content": {
+ "application/json": {
+ "schema": {
+ "type": "array",
+ "items": {
+ "$ref": "#/components/schemas/UploadGrant"
+ }
+ }
+ }
+ }
+ },
+ "404": {
+ "description": "Unknown or inaccessible job"
+ },
+ "413": {
+ "description": "Body limit exceeded"
+ },
+ "503": {
+ "description": "Slack imports disabled"
+ }
+ }
+ }
+ },
+ "/slack/imports/{job_id}/uploads/complete": {
+ "post": {
+ "tags": ["slack"],
+ "summary": "Verify storage checksums and lengths for registered objects. A seal fixes the ENTIRE\ndescriptor set (contiguous indices, sizes and hashes), not just this completion batch.\nEmpty uploads with a zero-part seal support shape-only or empty-history imports.\nWork becomes ready only after verified users metadata, a seal and every required part.",
+ "operationId": "complete_slack_import_uploads",
+ "parameters": [
+ {
+ "name": "job_id",
+ "in": "path",
+ "required": true,
+ "schema": {
+ "$ref": "#/components/schemas/JobId"
+ }
+ }
+ ],
+ "requestBody": {
+ "content": {
+ "application/json": {
+ "schema": {
+ "$ref": "#/components/schemas/SlackCompleteRequest"
+ }
+ }
+ },
+ "required": true
+ },
+ "responses": {
+ "200": {
+ "description": "",
+ "content": {
+ "application/json": {
+ "schema": {
+ "$ref": "#/components/schemas/ImportProgress"
+ }
+ }
+ }
+ },
+ "404": {
+ "description": "Unknown or inaccessible job"
+ },
+ "409": {
+ "description": "Immutable manifest conflict"
+ },
+ "413": {
+ "description": "Body limit exceeded"
+ },
+ "422": {
+ "description": "Object verification failed"
+ },
+ "503": {
+ "description": "Slack imports disabled"
+ }
+ }
+ }
+ },
"/sync_service/wakeup": {
"post": {
"tags": ["sync_service"],
@@ -18056,40 +18325,265 @@
"description": "An accepted command was refused before it could start a runtime turn.",
"required": ["identity", "action_id", "failure"],
"properties": {
- "action_id": {
- "$ref": "#/components/schemas/AgentActionId",
- "description": "The command that will not execute."
+ "action_id": {
+ "$ref": "#/components/schemas/AgentActionId",
+ "description": "The command that will not execute."
+ },
+ "actor": {
+ "oneOf": [
+ {
+ "type": "null"
+ },
+ {
+ "$ref": "#/components/schemas/MacroUserIdStr",
+ "description": "Who submitted the command, if acting on a user's behalf."
+ }
+ ]
+ },
+ "announcement_message_id": {
+ "type": ["string", "null"],
+ "format": "uuid",
+ "description": "The command's thread announcement, when one was created."
+ },
+ "failure": {
+ "$ref": "#/components/schemas/CommandFailure",
+ "description": "Safe details for clients and downstream consumers."
+ },
+ "identity": {
+ "$ref": "#/components/schemas/SessionIdentity",
+ "description": "The session."
+ }
+ }
+ },
+ "CompleteUploads": {
+ "type": "object",
+ "description": "Explicit upload completion; a seal can be submitted separately with an empty list.",
+ "required": ["uploads"],
+ "properties": {
+ "seal": {
+ "oneOf": [
+ {
+ "type": "null"
+ },
+ {
+ "$ref": "#/components/schemas/ConversationSeal",
+ "description": "Optional immutable expected part set; identical retries are idempotent."
+ }
+ ]
+ },
+ "uploads": {
+ "type": "array",
+ "items": {
+ "$ref": "#/components/schemas/UploadId"
+ },
+ "description": "Registered identities to verify against storage (at most 50).",
+ "maxItems": 50
+ }
+ },
+ "additionalProperties": false
+ },
+ "ConferenceProvider": {
+ "type": "string",
+ "description": "The conferencing system backing an event's join URL.\n\nMacro generates only Google Meet conferences, so this distinguishes one it\ncreated from a third party's — Zoom and friends arriving as `addOn`\nconference data, or a legacy classic Hangout. Clients use it to label the\nconference and to tell whether the Meet toggle reflects a Macro-managed\nconference.\n\nIt does not gate mutation. An explicit request replaces or detaches any\nconference, third-party included, exactly as deleting the event would;\nwhat protects a conference is that omitting the field leaves it untouched,\nso an unrelated edit never disturbs it.",
+ "enum": ["google_meet", "other"]
+ },
+ "ConversationId": {
+ "type": "string",
+ "description": "Source Slack conversation ID (C, G or D prefix); unique only within a source."
+ },
+ "ConversationKind": {
+ "type": "string",
+ "description": "Slack conversation kind. Public Slack channels map to Macro Team, never Public.",
+ "enum": [
+ "public_channel",
+ "private_channel",
+ "direct_message",
+ "group_direct_message"
+ ]
+ },
+ "ConversationMetadata": {
+ "type": "object",
+ "description": "Full selected conversation metadata, persisted before granting any uploads.",
+ "required": [
+ "slackChannelId",
+ "kind",
+ "name",
+ "folder",
+ "memberIds",
+ "archived"
+ ],
+ "properties": {
+ "archived": {
+ "type": "boolean",
+ "description": "Source archived flag (does not silently archive a reused Macro target)."
+ },
+ "createdAt": {
+ "oneOf": [
+ {
+ "type": "null"
+ },
+ {
+ "$ref": "#/components/schemas/SlackTimestamp",
+ "description": "Missing time is resolved once from earliest message or persisted job time."
+ }
+ ]
+ },
+ "creatorId": {
+ "oneOf": [
+ {
+ "type": "null"
+ },
+ {
+ "$ref": "#/components/schemas/SlackUserId",
+ "description": "Source creator when available."
+ }
+ ]
+ },
+ "folder": {
+ "$ref": "#/components/schemas/KeySegment",
+ "description": "Single archive folder name, not a backend object key."
+ },
+ "kind": {
+ "$ref": "#/components/schemas/ConversationKind",
+ "description": "Source conversation kind."
+ },
+ "memberIds": {
+ "type": "array",
+ "items": {
+ "$ref": "#/components/schemas/SlackUserId"
+ },
+ "description": "Complete source member list, including members unknown to Macro."
+ },
+ "messageCount": {
+ "type": ["integer", "null"],
+ "format": "int64",
+ "description": "Advisory only; null means not yet counted, not zero.",
+ "minimum": 0
+ },
+ "name": {
+ "type": "string",
+ "description": "Original display name; not a storage key."
+ },
+ "slackChannelId": {
+ "$ref": "#/components/schemas/ConversationId",
+ "description": "Stable Slack identity."
+ }
+ },
+ "additionalProperties": false
+ },
+ "ConversationProgress": {
+ "type": "object",
+ "description": "Admin-visible progress for one selected conversation.",
+ "required": [
+ "slackChannelId",
+ "name",
+ "kind",
+ "archived",
+ "status",
+ "verifiedParts",
+ "counters",
+ "search",
+ "warnings"
+ ],
+ "properties": {
+ "archived": {
+ "type": "boolean",
+ "description": "Whether the selected source conversation was archived."
},
- "actor": {
+ "channelId": {
+ "type": ["string", "null"],
+ "format": "uuid",
+ "description": "Authorized target only; absent for inaccessible reused targets."
+ },
+ "counters": {
+ "$ref": "#/components/schemas/ImportCounters",
+ "description": "Durably committed record counts."
+ },
+ "error": {
"oneOf": [
{
"type": "null"
},
{
- "$ref": "#/components/schemas/MacroUserIdStr",
- "description": "Who submitted the command, if acting on a user's behalf."
+ "$ref": "#/components/schemas/ImportError",
+ "description": "Sanitized terminal or retry diagnostic."
}
]
},
- "announcement_message_id": {
- "type": ["string", "null"],
- "format": "uuid",
- "description": "The command's thread announcement, when one was created."
+ "kind": {
+ "$ref": "#/components/schemas/ConversationKind",
+ "description": "Persisted source kind."
},
- "failure": {
- "$ref": "#/components/schemas/CommandFailure",
- "description": "Safe details for clients and downstream consumers."
+ "name": {
+ "type": "string",
+ "description": "Persisted source display name, never the reused Macro target's name."
},
- "identity": {
- "$ref": "#/components/schemas/SessionIdentity",
- "description": "The session."
+ "partCount": {
+ "type": ["integer", "null"],
+ "format": "int32",
+ "description": "Null until sealed; zero is a valid sealed empty manifest.",
+ "minimum": 0
+ },
+ "search": {
+ "$ref": "#/components/schemas/SearchState",
+ "description": "Independent indexing state."
+ },
+ "slackChannelId": {
+ "$ref": "#/components/schemas/ConversationId",
+ "description": "Source identity (always visible to the importing team's administrator)."
+ },
+ "status": {
+ "$ref": "#/components/schemas/ConversationStatus",
+ "description": "Durable state."
+ },
+ "verifiedParts": {
+ "type": "integer",
+ "format": "int32",
+ "description": "Number of parts whose object identity has been verified.",
+ "minimum": 0
+ },
+ "warnings": {
+ "type": "array",
+ "items": {
+ "$ref": "#/components/schemas/ImportWarning"
+ },
+ "description": "Non-fatal metadata/skip explanations."
}
}
},
- "ConferenceProvider": {
+ "ConversationSeal": {
+ "type": "object",
+ "description": "Complete immutable expected part set for one conversation, including zero parts.",
+ "required": ["slackChannelId", "partCount", "manifestSha256"],
+ "properties": {
+ "manifestSha256": {
+ "$ref": "#/components/schemas/Sha256Digest",
+ "description": "SHA-256 of canonical descriptor lines; zero parts hash the empty byte string."
+ },
+ "partCount": {
+ "type": "integer",
+ "format": "int32",
+ "description": "Exact number of registered parts, starting at index zero.",
+ "minimum": 0
+ },
+ "slackChannelId": {
+ "$ref": "#/components/schemas/ConversationId",
+ "description": "Selected source conversation."
+ }
+ },
+ "additionalProperties": false
+ },
+ "ConversationStatus": {
"type": "string",
- "description": "The conferencing system backing an event's join URL.\n\nMacro generates only Google Meet conferences, so this distinguishes one it\ncreated from a third party's — Zoom and friends arriving as `addOn`\nconference data, or a legacy classic Hangout. Clients use it to label the\nconference and to tell whether the Meet toggle reflects a Macro-managed\nconference.\n\nIt does not gate mutation. An explicit request replaces or detaches any\nconference, third-party included, exactly as deleting the event would;\nwhat protects a conference is that omitting the field leaves it untouched,\nso an unrelated edit never disturbs it.",
- "enum": ["google_meet", "other"]
+ "description": "Durable per-conversation lifecycle.",
+ "enum": [
+ "awaiting_uploads",
+ "queued",
+ "importing",
+ "completed",
+ "skipped",
+ "failed"
+ ]
},
"CopyDocumentQueryParams": {
"type": "object",
@@ -18649,6 +19143,40 @@
}
}
},
+ "CreateImport": {
+ "type": "object",
+ "description": "Create command. Team identity is deliberately absent.",
+ "required": [
+ "idempotencyToken",
+ "source",
+ "includeMessageHistory",
+ "conversations"
+ ],
+ "properties": {
+ "conversations": {
+ "type": "array",
+ "items": {
+ "$ref": "#/components/schemas/ConversationMetadata"
+ },
+ "description": "Full, unique selected conversations (at most the configured bound).",
+ "maxItems": 2000,
+ "minItems": 1
+ },
+ "idempotencyToken": {
+ "$ref": "#/components/schemas/CreateToken",
+ "description": "Replay with the same semantic payload returns the original job; changes conflict."
+ },
+ "includeMessageHistory": {
+ "type": "boolean",
+ "description": "Default true at the client; false still requires users and zero-part seals."
+ },
+ "source": {
+ "$ref": "#/components/schemas/SourceIdentity",
+ "description": "Binding or explicit unknown-source confirmation."
+ }
+ },
+ "additionalProperties": false
+ },
"CreateInitiativeRequest": {
"type": "object",
"description": "Create-initiative HTTP body.",
@@ -19006,6 +19534,11 @@
}
}
},
+ "CreateToken": {
+ "type": "string",
+ "format": "uuid",
+ "description": "Client-generated create idempotency token, scoped to team and administrator."
+ },
"CreateUnthreadedAnchorRequest": {
"oneOf": [
{
@@ -23408,68 +23941,320 @@
"type": "string",
"description": "The session's display name."
},
- "owner_id": {
- "type": "string",
- "description": "The user the session belongs to."
+ "owner_id": {
+ "type": "string",
+ "description": "The user the session belongs to."
+ },
+ "session_id": {
+ "type": "string",
+ "format": "uuid",
+ "description": "The session id."
+ },
+ "status": {
+ "type": "string",
+ "description": "Session lifecycle status, e.g. `no_messages`, `active`."
+ }
+ }
+ },
+ "HashMap": {
+ "type": "object",
+ "additionalProperties": {
+ "oneOf": [
+ {
+ "allOf": [
+ {
+ "$ref": "#/components/schemas/PresignedUrl"
+ },
+ {
+ "type": "object",
+ "required": ["type"],
+ "properties": {
+ "type": {
+ "type": "string",
+ "enum": ["external"]
+ }
+ }
+ }
+ ]
+ },
+ {
+ "allOf": [
+ {
+ "$ref": "#/components/schemas/S3ObjectInfo"
+ },
+ {
+ "type": "object",
+ "required": ["type"],
+ "properties": {
+ "type": {
+ "type": "string",
+ "enum": ["internal"]
+ }
+ }
+ }
+ ]
+ }
+ ]
+ },
+ "propertyNames": {
+ "type": "string"
+ }
+ },
+ "HighlightType": {
+ "type": "integer",
+ "enum": [1, 2, 3]
+ },
+ "ImportCounters": {
+ "type": "object",
+ "description": "Committed counters, never optimistic browser or uncommitted worker counts.",
+ "required": [
+ "processed",
+ "imported",
+ "duplicates",
+ "skipped",
+ "reactions"
+ ],
+ "properties": {
+ "duplicates": {
+ "type": "integer",
+ "format": "int64",
+ "description": "Previously committed source identities.",
+ "minimum": 0
+ },
+ "imported": {
+ "type": "integer",
+ "format": "int64",
+ "description": "Newly persisted messages.",
+ "minimum": 0
+ },
+ "processed": {
+ "type": "integer",
+ "format": "int64",
+ "description": "Source records examined through the committed checkpoint.",
+ "minimum": 0
+ },
+ "reactions": {
+ "type": "integer",
+ "format": "int64",
+ "description": "Newly persisted reactions.",
+ "minimum": 0
+ },
+ "skipped": {
+ "type": "integer",
+ "format": "int64",
+ "description": "Unsupported, empty or otherwise deliberately skipped records.",
+ "minimum": 0
+ }
+ }
+ },
+ "ImportError": {
+ "type": "string",
+ "description": "Public error codes; never carry raw provider errors, keys, emails or source text.",
+ "enum": [
+ "disabled",
+ "invalid_input",
+ "limit_exceeded",
+ "unavailable",
+ "admin_required",
+ "source_mismatch",
+ "conflict",
+ "upload_mismatch",
+ "lease_lost",
+ "retryable",
+ "internal"
+ ]
+ },
+ "ImportLimits": {
+ "type": "object",
+ "description": "Configurable bounds shared with browser staging and enforced again by the worker.",
+ "required": [
+ "partBytes",
+ "partRecords",
+ "recordBytes",
+ "jsonBytes",
+ "selectedBytes",
+ "zipEntries",
+ "conversations",
+ "registrationBatch",
+ "databaseBatchBytes",
+ "databaseBatchMessages"
+ ],
+ "properties": {
+ "conversations": {
+ "type": "integer",
+ "format": "int32",
+ "description": "Maximum selected conversations.",
+ "minimum": 0
+ },
+ "databaseBatchBytes": {
+ "type": "integer",
+ "format": "int64",
+ "description": "Maximum historical batch payload bytes.",
+ "minimum": 0
+ },
+ "databaseBatchMessages": {
+ "type": "integer",
+ "format": "int32",
+ "description": "Maximum messages in a historical batch.",
+ "minimum": 0
+ },
+ "jsonBytes": {
+ "type": "integer",
+ "format": "int64",
+ "description": "Maximum users, root metadata or individual day JSON bytes.",
+ "minimum": 0
+ },
+ "partBytes": {
+ "type": "integer",
+ "format": "int64",
+ "description": "Maximum NDJSON part bytes.",
+ "minimum": 0
+ },
+ "partRecords": {
+ "type": "integer",
+ "format": "int32",
+ "description": "Maximum records per part.",
+ "minimum": 0
+ },
+ "recordBytes": {
+ "type": "integer",
+ "format": "int64",
+ "description": "Maximum bytes per NDJSON record, including newline.",
+ "minimum": 0
+ },
+ "registrationBatch": {
+ "type": "integer",
+ "format": "int32",
+ "description": "Maximum descriptors or completion identities per call.",
+ "minimum": 0
+ },
+ "selectedBytes": {
+ "type": "integer",
+ "format": "int64",
+ "description": "Maximum selected temporary data bytes across all uploads.",
+ "minimum": 0
+ },
+ "zipEntries": {
+ "type": "integer",
+ "format": "int32",
+ "description": "Maximum ZIP entries scanned by the browser.",
+ "minimum": 0
+ }
+ }
+ },
+ "ImportPage": {
+ "type": "object",
+ "description": "Admin list result, also supplying upload limits before a job exists.",
+ "required": ["jobs", "limits", "sourceBinding"],
+ "properties": {
+ "jobs": {
+ "type": "array",
+ "items": {
+ "$ref": "#/components/schemas/ImportProgress"
+ },
+ "description": "At most 50 job receipts, newest first."
+ },
+ "limits": {
+ "$ref": "#/components/schemas/ImportLimits",
+ "description": "Effective server limits."
+ },
+ "nextCursor": {
+ "oneOf": [
+ {
+ "type": "null"
+ },
+ {
+ "$ref": "#/components/schemas/JobId",
+ "description": "Exclusive cursor for the next page; null when exhausted."
+ }
+ ]
+ },
+ "sourceBinding": {
+ "$ref": "#/components/schemas/SourceBinding",
+ "description": "Binding summary used by the picker confirmation."
+ }
+ }
+ },
+ "ImportProgress": {
+ "type": "object",
+ "description": "Common receipt returned by create, completion, finalize, cancel and progress.",
+ "required": [
+ "source",
+ "includeMessageHistory",
+ "jobId",
+ "status",
+ "revision",
+ "createdAt",
+ "updatedAt",
+ "usersVerified",
+ "limits",
+ "conversations"
+ ],
+ "properties": {
+ "conversations": {
+ "type": "array",
+ "items": {
+ "$ref": "#/components/schemas/ConversationProgress"
+ },
+ "description": "All selected conversations, bounded by the create limit."
+ },
+ "createdAt": {
+ "type": "string",
+ "format": "date-time",
+ "description": "Persisted creation time, also the final historical-time fallback."
+ },
+ "includeMessageHistory": {
+ "type": "boolean",
+ "description": "Immutable history option confirmed when creating this job."
+ },
+ "jobId": {
+ "$ref": "#/components/schemas/JobId",
+ "description": "Stable job identity."
+ },
+ "limits": {
+ "$ref": "#/components/schemas/ImportLimits",
+ "description": "Server bounds for staging and registration."
+ },
+ "registrationClosedAt": {
+ "type": ["string", "null"],
+ "format": "date-time",
+ "description": "Registration closure time; cancellation also closes registration."
},
- "session_id": {
- "type": "string",
- "format": "uuid",
- "description": "The session id."
+ "revision": {
+ "type": "integer",
+ "format": "int64",
+ "description": "Monotonic job revision for polling/websocket invalidation.",
+ "minimum": 0
+ },
+ "source": {
+ "$ref": "#/components/schemas/SourceIdentity",
+ "description": "Immutable source confirmation from this job, not the current team binding."
},
"status": {
+ "$ref": "#/components/schemas/JobStatus",
+ "description": "Durable job lifecycle."
+ },
+ "updatedAt": {
"type": "string",
- "description": "Session lifecycle status, e.g. `no_messages`, `active`."
+ "format": "date-time",
+ "description": "Last durable lifecycle/progress update."
+ },
+ "usersVerified": {
+ "type": "boolean",
+ "description": "Whether users metadata is verified and pinned."
}
}
},
- "HashMap": {
- "type": "object",
- "additionalProperties": {
- "oneOf": [
- {
- "allOf": [
- {
- "$ref": "#/components/schemas/PresignedUrl"
- },
- {
- "type": "object",
- "required": ["type"],
- "properties": {
- "type": {
- "type": "string",
- "enum": ["external"]
- }
- }
- }
- ]
- },
- {
- "allOf": [
- {
- "$ref": "#/components/schemas/S3ObjectInfo"
- },
- {
- "type": "object",
- "required": ["type"],
- "properties": {
- "type": {
- "type": "string",
- "enum": ["internal"]
- }
- }
- }
- ]
- }
- ]
- },
- "propertyNames": {
- "type": "string"
- }
- },
- "HighlightType": {
- "type": "integer",
- "enum": [1, 2, 3]
+ "ImportWarning": {
+ "type": "string",
+ "description": "Non-fatal, sanitized explanations displayed in admin progress.",
+ "enum": [
+ "creation_time_from_message",
+ "creation_time_from_job",
+ "unresolvable_direct_message",
+ "target_unavailable",
+ "uploads_incomplete"
+ ]
},
"ImportedAuthor": {
"type": "object",
@@ -23762,6 +24547,28 @@
}
}
},
+ "JobId": {
+ "type": "string",
+ "format": "uuid",
+ "description": "Import job identity. Generate UUIDv7 in application code."
+ },
+ "JobStatus": {
+ "type": "string",
+ "description": "Job lifecycle. Cancellation remains in progress while any lease is active.",
+ "enum": [
+ "uploading",
+ "processing",
+ "completed",
+ "completed_with_errors",
+ "failed",
+ "cancelling",
+ "cancelled"
+ ]
+ },
+ "KeySegment": {
+ "type": "string",
+ "description": "Single safe source folder/key segment; never a path or authorization proof."
+ },
"LatestMessage": {
"type": "object",
"description": "Latest-message bundle for soup payloads.",
@@ -26543,6 +27350,22 @@
}
}
},
+ "RegisterUploads": {
+ "type": "object",
+ "description": "Batched registration, also used to renew an unchanged descriptor's grant.",
+ "required": ["descriptors"],
+ "properties": {
+ "descriptors": {
+ "type": "array",
+ "items": {
+ "$ref": "#/components/schemas/UploadDescriptor"
+ },
+ "description": "At most 50 descriptors; no duplicate identities in a call.",
+ "maxItems": 50
+ }
+ },
+ "additionalProperties": false
+ },
"Reminder": {
"type": "object",
"description": "A reminder belonging to a user.\n\n`user_id` is deliberately absent: a reminder is only ever read by its owner,\nso the field would be redundant on the wire.",
@@ -26978,6 +27801,71 @@
}
}
},
+ "SearchState": {
+ "oneOf": [
+ {
+ "type": "object",
+ "description": "No committed history requires indexing.",
+ "required": ["status"],
+ "properties": {
+ "status": {
+ "type": "string",
+ "enum": ["not_needed"]
+ }
+ }
+ },
+ {
+ "type": "object",
+ "description": "Committed history requires an outbox-backed scoped submission.",
+ "required": ["status"],
+ "properties": {
+ "status": {
+ "type": "string",
+ "enum": ["pending"]
+ }
+ }
+ },
+ {
+ "type": "object",
+ "description": "Search service accepted the request, but has not completed it.",
+ "required": ["receiptId", "status"],
+ "properties": {
+ "receiptId": {
+ "type": "string",
+ "format": "uuid",
+ "description": "Receipt from the search service."
+ },
+ "status": {
+ "type": "string",
+ "enum": ["submitted"]
+ }
+ }
+ },
+ {
+ "type": "object",
+ "description": "Search service durably published the scope; eventual indexing is separate.",
+ "required": ["status"],
+ "properties": {
+ "status": {
+ "type": "string",
+ "enum": ["completed"]
+ }
+ }
+ },
+ {
+ "type": "object",
+ "description": "Publication failed; retained history must be retried independently.",
+ "required": ["status"],
+ "properties": {
+ "status": {
+ "type": "string",
+ "enum": ["failed"]
+ }
+ }
+ }
+ ],
+ "description": "Search publication/receipt state. Acceptance is not completed publication;\neven completed publication still awaits consumer indexing and refresh."
+ },
"SessionDeletedMetadata": {
"type": "object",
"description": "The session was deleted.",
@@ -27403,6 +28291,10 @@
],
"description": "Type-safe enum for setting entity property values - provides compile-time validation."
},
+ "Sha256Digest": {
+ "type": "string",
+ "description": "SHA-256 encoded as exactly 64 lowercase hexadecimal characters."
+ },
"SharePermissionV2": {
"type": "object",
"required": ["id", "owner"],
@@ -27488,6 +28380,26 @@
}
}
},
+ "SlackCompleteRequest": {
+ "$ref": "#/components/schemas/CompleteUploads",
+ "description": "At most fifty registered identities to verify and an optional immutable conversation seal."
+ },
+ "SlackCreateRequest": {
+ "$ref": "#/components/schemas/CreateImport",
+ "description": "Bounded full-metadata create payload. Domain validation applies effective limits again."
+ },
+ "SlackRegisterRequest": {
+ "$ref": "#/components/schemas/RegisterUploads",
+ "description": "At most fifty immutable upload descriptors, without client-supplied storage keys."
+ },
+ "SlackTimestamp": {
+ "type": "string",
+ "description": "Slack time represented exactly as nonnegative Unix microseconds.\nJSON is a string with six fractional digits, never a floating-point number."
+ },
+ "SlackUserId": {
+ "type": "string",
+ "description": "Slack member identity (U or W prefix), including USLACKBOT."
+ },
"SmartTagChannelMatch": {
"type": "object",
"description": "A channel visible to the caller that matches a smart tag rule.",
@@ -29505,6 +30417,83 @@
}
}
},
+ "SourceBinding": {
+ "oneOf": [
+ {
+ "type": "object",
+ "description": "No import has bound this team yet.",
+ "required": ["kind"],
+ "properties": {
+ "kind": {
+ "type": "string",
+ "enum": ["unbound"]
+ }
+ }
+ },
+ {
+ "type": "object",
+ "description": "Legacy/onboarding or explicitly confirmed unidentified source.",
+ "required": ["kind"],
+ "properties": {
+ "kind": {
+ "type": "string",
+ "enum": ["confirmed_unknown"]
+ }
+ }
+ },
+ {
+ "type": "object",
+ "description": "Known Slack workspace; reject mismatches.",
+ "required": ["sourceId", "kind"],
+ "properties": {
+ "kind": {
+ "type": "string",
+ "enum": ["known"]
+ },
+ "sourceId": {
+ "$ref": "#/components/schemas/SourceId",
+ "description": "Bound source identity."
+ }
+ }
+ }
+ ],
+ "description": "Team's durable single-source binding, including confirmed unidentified archives."
+ },
+ "SourceId": {
+ "type": "string",
+ "description": "Known Slack workspace identity (T prefix); an enterprise ID alone is insufficient."
+ },
+ "SourceIdentity": {
+ "oneOf": [
+ {
+ "type": "object",
+ "description": "Archive names its Slack workspace; reject a conflicting team binding.",
+ "required": ["sourceId", "kind"],
+ "properties": {
+ "kind": {
+ "type": "string",
+ "enum": ["known"]
+ },
+ "sourceId": {
+ "$ref": "#/components/schemas/SourceId",
+ "description": "Workspace identifier in the archive."
+ }
+ }
+ },
+ {
+ "type": "object",
+ "description": "Explicit confirmation that an unidentified archive belongs to this team's source.\nChoosing this variant is affirmative confirmation, not an inferred default.",
+ "required": ["kind"],
+ "properties": {
+ "kind": {
+ "type": "string",
+ "enum": ["confirmed_unknown"]
+ }
+ }
+ }
+ ],
+ "description": "Source identity supplied when creating a job. One binding per Macro team in v1."
+ },
"StarterDocumentsResponse": {
"type": "object",
"description": "The deterministic starter document ids for the current user.",
@@ -30375,6 +31364,34 @@
}
}
},
+ "UploadDescriptor": {
+ "type": "object",
+ "description": "Immutable expected object properties, registered before signing an upload.",
+ "required": ["upload", "sha256", "byteLength"],
+ "properties": {
+ "byteLength": {
+ "type": "integer",
+ "format": "int64",
+ "description": "Exact byte length including delimiters.",
+ "minimum": 0
+ },
+ "recordCount": {
+ "type": ["integer", "null"],
+ "format": "int32",
+ "description": "Exact NDJSON record count; null for the users JSON payload.",
+ "minimum": 0
+ },
+ "sha256": {
+ "$ref": "#/components/schemas/Sha256Digest",
+ "description": "SHA-256 of the exact uploaded bytes, not the ETag."
+ },
+ "upload": {
+ "$ref": "#/components/schemas/UploadId",
+ "description": "Manifest identity within this job."
+ }
+ },
+ "additionalProperties": false
+ },
"UploadDocumentStatus": {
"type": "string",
"description": "The upload status of the document",
@@ -30420,6 +31437,73 @@
}
}
},
+ "UploadGrant": {
+ "type": "object",
+ "description": "Signed upload permission returned just before PUT; never persisted as an identity.",
+ "required": ["descriptor", "url", "requiredHeaders", "expiresAt"],
+ "properties": {
+ "descriptor": {
+ "$ref": "#/components/schemas/UploadDescriptor",
+ "description": "Exact immutable descriptor being signed."
+ },
+ "expiresAt": {
+ "type": "string",
+ "format": "date-time",
+ "description": "Grant expiry; callers renew by re-registering the identical descriptor."
+ },
+ "requiredHeaders": {
+ "type": "object",
+ "description": "Required signed upload headers (including checksum, content type and create-only\ncondition). Browser-forbidden Content-Length is derived from the exact Blob bytes.",
+ "additionalProperties": {
+ "type": "string"
+ },
+ "propertyNames": {
+ "type": "string"
+ }
+ },
+ "url": {
+ "type": "string",
+ "description": "Short-lived signed destination; must not be logged."
+ }
+ }
+ },
+ "UploadId": {
+ "oneOf": [
+ {
+ "type": "object",
+ "description": "One normalized users payload from users.json and/or org_users.json.",
+ "required": ["kind"],
+ "properties": {
+ "kind": {
+ "type": "string",
+ "enum": ["users"]
+ }
+ }
+ },
+ {
+ "type": "object",
+ "description": "One NDJSON part in a selected conversation.",
+ "required": ["slackChannelId", "partIndex", "kind"],
+ "properties": {
+ "kind": {
+ "type": "string",
+ "enum": ["conversation_part"]
+ },
+ "partIndex": {
+ "type": "integer",
+ "format": "int32",
+ "description": "Zero-based part index; sealed manifests must be contiguous.",
+ "minimum": 0
+ },
+ "slackChannelId": {
+ "$ref": "#/components/schemas/ConversationId",
+ "description": "Selected Slack conversation."
+ }
+ }
+ }
+ ],
+ "description": "Identity in a job's persisted manifest. No user-supplied object keys."
+ },
"UpsertUserDocumentViewLocationRequest": {
"type": "object",
"required": ["location"],
diff --git a/packages/sdk/src/coverage/skipped.ts b/packages/sdk/src/coverage/skipped.ts
index 86b50226f5..750889a717 100644
--- a/packages/sdk/src/coverage/skipped.ts
+++ b/packages/sdk/src/coverage/skipped.ts
@@ -287,6 +287,14 @@ export const staticFilesBacklog =
[] as const satisfies readonly (keyof StaticFilesSdk)[];
export const storageExcluded = [
+ // Slack archive imports are browser-admin workflows, not SDK surface in v1.
+ 'cancelSlackImport',
+ 'completeSlackImportUploads',
+ 'createSlackImport',
+ 'finalizeSlackImport',
+ 'getSlackImport',
+ 'listSlackImports',
+ 'registerSlackImportUploads',
'bulkWakeupSyncServiceDocuments',
'callWebhook',
'checkActiveCall',
diff --git a/rules/ast-grep/ts-feature-components-presentational.yml b/rules/ast-grep/ts-feature-components-presentational.yml
index 812d87d17f..e7f4f56b29 100644
--- a/rules/ast-grep/ts-feature-components-presentational.yml
+++ b/rules/ast-grep/ts-feature-components-presentational.yml
@@ -14,6 +14,7 @@ note: |-
the presentation layer and `component/` is the composition layer.
files:
- apps/web/src/features/crm/components/**
+ - apps/web/src/features/slack-import/components/**
- apps/web/src/features/channels-view/components/ChannelDetailView*
- apps/web/src/features/channel/components/channel-invite*
- apps/web/src/features/projects/components/**
diff --git a/rules/ast-grep/ts-feature-core-pure.yml b/rules/ast-grep/ts-feature-core-pure.yml
index f10e58304d..813e205856 100644
--- a/rules/ast-grep/ts-feature-core-pure.yml
+++ b/rules/ast-grep/ts-feature-core-pure.yml
@@ -16,6 +16,7 @@ note: |-
Block-agent names its rendering layers `ui/` and `component/`.
files:
- apps/web/src/features/crm/core/**
+ - apps/web/src/features/slack-import/core/**
- apps/web/src/features/channel/core/channel-invite*
- apps/web/src/features/projects/core/**
- apps/web/src/features/channel/Call/chat/core/**
diff --git a/rules/ast-grep/ts-feature-data-no-ui.yml b/rules/ast-grep/ts-feature-data-no-ui.yml
index 41eda0a7c1..e681252339 100644
--- a/rules/ast-grep/ts-feature-data-no-ui.yml
+++ b/rules/ast-grep/ts-feature-data-no-ui.yml
@@ -13,6 +13,8 @@ note: |-
files:
- apps/web/src/features/crm/queries/**
- apps/web/src/features/crm/primitives/**
+ - apps/web/src/features/slack-import/queries/**
+ - apps/web/src/features/slack-import/primitives/**
- apps/web/src/features/channels-view/queries/channel-detail-source*
- apps/web/src/features/channels-view/primitives/create-channel-detail*
- apps/web/src/features/channel/queries/channel-invite*
diff --git a/rules/ast-grep/ts-feature-layers-use-context.yml b/rules/ast-grep/ts-feature-layers-use-context.yml
index 9c18a3cd20..92f3769faf 100644
--- a/rules/ast-grep/ts-feature-layers-use-context.yml
+++ b/rules/ast-grep/ts-feature-layers-use-context.yml
@@ -20,6 +20,10 @@ files:
- apps/web/src/features/crm/components/**
- apps/web/src/features/crm/views/**
- apps/web/src/features/crm/context/**
+ - apps/web/src/features/slack-import/queries/**
+ - apps/web/src/features/slack-import/primitives/**
+ - apps/web/src/features/slack-import/components/**
+ - apps/web/src/features/slack-import/views/**
- apps/web/src/features/channels-view/queries/channel-detail-source*
- apps/web/src/features/channels-view/primitives/create-channel-detail*
- apps/web/src/features/channels-view/context/channel-detail-source*
diff --git a/rules/ast-grep/tsx-feature-components-presentational.yml b/rules/ast-grep/tsx-feature-components-presentational.yml
index ba3b21e4cc..baa0987caa 100644
--- a/rules/ast-grep/tsx-feature-components-presentational.yml
+++ b/rules/ast-grep/tsx-feature-components-presentational.yml
@@ -14,6 +14,7 @@ note: |-
the presentation layer and `component/` is the composition layer.
files:
- apps/web/src/features/crm/components/**
+ - apps/web/src/features/slack-import/components/**
- apps/web/src/features/channels-view/components/ChannelDetailView*
- apps/web/src/features/channel/components/channel-invite*
- apps/web/src/features/projects/components/**
diff --git a/rules/ast-grep/tsx-feature-core-pure.yml b/rules/ast-grep/tsx-feature-core-pure.yml
index 9b94b7f03f..67c9a87dfe 100644
--- a/rules/ast-grep/tsx-feature-core-pure.yml
+++ b/rules/ast-grep/tsx-feature-core-pure.yml
@@ -16,6 +16,7 @@ note: |-
Block-agent names its rendering layers `ui/` and `component/`.
files:
- apps/web/src/features/crm/core/**
+ - apps/web/src/features/slack-import/core/**
- apps/web/src/features/channel/core/channel-invite*
- apps/web/src/features/projects/core/**
- apps/web/src/features/channel/Call/chat/core/**
diff --git a/rules/ast-grep/tsx-feature-data-no-ui.yml b/rules/ast-grep/tsx-feature-data-no-ui.yml
index e0763cbc45..38add60fee 100644
--- a/rules/ast-grep/tsx-feature-data-no-ui.yml
+++ b/rules/ast-grep/tsx-feature-data-no-ui.yml
@@ -13,6 +13,8 @@ note: |-
files:
- apps/web/src/features/crm/queries/**
- apps/web/src/features/crm/primitives/**
+ - apps/web/src/features/slack-import/queries/**
+ - apps/web/src/features/slack-import/primitives/**
- apps/web/src/features/channels-view/queries/channel-detail-source*
- apps/web/src/features/channels-view/primitives/create-channel-detail*
- apps/web/src/features/channel/queries/channel-invite*
diff --git a/rules/ast-grep/tsx-feature-layers-use-context.yml b/rules/ast-grep/tsx-feature-layers-use-context.yml
index 77963787cb..3f2d807b52 100644
--- a/rules/ast-grep/tsx-feature-layers-use-context.yml
+++ b/rules/ast-grep/tsx-feature-layers-use-context.yml
@@ -20,6 +20,10 @@ files:
- apps/web/src/features/crm/components/**
- apps/web/src/features/crm/views/**
- apps/web/src/features/crm/context/**
+ - apps/web/src/features/slack-import/queries/**
+ - apps/web/src/features/slack-import/primitives/**
+ - apps/web/src/features/slack-import/components/**
+ - apps/web/src/features/slack-import/views/**
- apps/web/src/features/channels-view/queries/channel-detail-source*
- apps/web/src/features/channels-view/primitives/create-channel-detail*
- apps/web/src/features/channels-view/context/channel-detail-source*
diff --git a/services/document_storage_service/Cargo.toml b/services/document_storage_service/Cargo.toml
index a72d1dbf40..68ec2a19fa 100644
--- a/services/document_storage_service/Cargo.toml
+++ b/services/document_storage_service/Cargo.toml
@@ -130,6 +130,7 @@ graphql_scheduled_action = { path = "../../crates/graphql_scheduled_action" }
harnesses = { path = "../../crates/harnesses" }
hex = { workspace = true }
http-body-util = { workspace = true }
+import = { path = "../../crates/import" }
initiative = { path = "../../crates/initiative", features = ["inbound", "postgres", "resources"] }
jsonwebtoken = { workspace = true }
last_online_tracker = { path = "../../crates/last_online_tracker" }
@@ -206,6 +207,12 @@ search_service = { path = "../../crates/search_service" }
secretsmanager_client = { path = "../../crates/secretsmanager_client" }
serde = { workspace = true }
serde_json = { workspace = true }
+slack_integration = { path = "../../crates/slack_integration", features = [
+ "gateway",
+ "inbound",
+ "postgres",
+ "s3",
+] }
soup = { path = "../../crates/soup", features = [
"axum",
"inbound",
diff --git a/services/document_storage_service/src/api.rs b/services/document_storage_service/src/api.rs
index 7eab67903c..85e65dd1a5 100644
--- a/services/document_storage_service/src/api.rs
+++ b/services/document_storage_service/src/api.rs
@@ -251,6 +251,10 @@ fn api_router(state: ApiContext) -> Router {
state.user_api_key_state.clone(),
),
)
+ .nest(
+ "/slack",
+ slack_integration::inbound::axum_router::slack_router(state.slack_state.clone()),
+ )
.nest(
"/reminders",
reminders::inbound::axum_router::reminders_router(state.reminders_state.clone()),
diff --git a/services/document_storage_service/src/api/context.rs b/services/document_storage_service/src/api/context.rs
index 8a2233e989..2bfdd0166e 100644
--- a/services/document_storage_service/src/api/context.rs
+++ b/services/document_storage_service/src/api/context.rs
@@ -509,6 +509,80 @@ pub(crate) type UserApiKeyServiceType = UserApiKeyServiceImpl<PgUserApiKeysRepo>
pub(crate) type DssUserApiKeyState =
UserApiKeyRouterState<UserApiKeyServiceType, AuthorizationService>;
+/// HTTP-only authorization adapter: revalidates notification recipients. DSS
+/// never authorizes worker targets; that capability fails closed here.
+pub(crate) struct SlackAdminAuthorizer(
+ pub Arc<EntityAccessService>,
+ pub entity_access::outbound::PgAccessRepository,
+);
+
+impl slack_integration::domain::ports::ImportProgressAccess for SlackAdminAuthorizer {
+ async fn participating_targets(
+ &self,
+ viewer: &macro_user_id::user_id::MacroUserIdStr<'_>,
+ targets: &[uuid::Uuid],
+ ) -> slack_integration::domain::ports::PortResult<Vec<uuid::Uuid>> {
+ use entity_access::domain::ports::AccessRepository as _;
+ self.1
+ .check_user_channel_membership(Some(&viewer.0), targets)
+ .await
+ .map_err(|error| {
+ rootcause::Report::new(error)
+ .context(slack_integration::domain::models::ImportError::Retryable)
+ })
+ }
+}
+
+impl slack_integration::domain::ports::ImportAuthorizer for SlackAdminAuthorizer {
+ async fn require_admin(
+ &self,
+ team: slack_integration::domain::models::TeamId,
+ user: &macro_user_id::user_id::MacroUserIdStr<'_>,
+ ) -> slack_integration::domain::ports::PortResult<()> {
+ use entity_access::domain::{
+ models::{AdminTeamRole, EntityType},
+ ports::EntityAccessService as _,
+ };
+ self.0
+ .generate_entity_access_receipt::<AdminTeamRole>(
+ &user.0,
+ None,
+ &team.to_string(),
+ EntityType::Team,
+ )
+ .await
+ .map_err(|_| slack_integration::domain::models::ImportError::AdminRequired)?;
+ Ok(())
+ }
+
+ async fn require_target(
+ &self,
+ _: slack_integration::domain::models::TeamId,
+ _: &macro_user_id::user_id::MacroUserIdStr<'_>,
+ _: &slack_integration::domain::models::ConversationMetadata,
+ _: uuid::Uuid,
+ ) -> slack_integration::domain::ports::PortResult<()> {
+ Err(slack_integration::domain::models::ImportError::Unavailable.into())
+ }
+}
+
+pub(crate) type SlackService = slack_integration::domain::service::SlackImportService<
+ slack_integration::outbound::pg_slack_import_repo::PgSlackImportRepo,
+ slack_integration::outbound::s3_storage::S3ImportStorage,
+ slack_integration::outbound::import_ledger::CanonicalImportLedger<
+ import::outbound::pg_import_repo::PgImportRepo,
+ >,
+ slack_integration::outbound::gateway_notifier::GatewayImportNotifier,
+ SlackAdminAuthorizer,
+ Box<dyn Fn(slack_integration::domain::models::TeamId) -> bool + Send + Sync>,
+>;
+
+pub(crate) type DssSlackState = slack_integration::inbound::axum_router::SlackRouterState<
+ SlackService,
+ EntityAccessService,
+ AuthorizationService,
+>;
+
/// Type alias for the reminders service.
pub(crate) type RemindersServiceType =
reminders::domain::email_followup::reminder_service::EmailRemindersService<
@@ -625,6 +699,7 @@ pub(crate) struct ApiContext {
pub channel_labels_state: DssChannelLabelsState,
pub user_api_key_state: DssUserApiKeyState,
pub reminders_state: DssRemindersState,
+ pub slack_state: DssSlackState,
pub initiative_state: DssInitiativeState,
pub graphql_initiative_context: graphql_initiative::InitiativeGraphqlContext,
pub graphql_scheduled_action_context: graphql_scheduled_action::ScheduledActionGraphqlContext,
diff --git a/services/document_storage_service/src/api/swagger.rs b/services/document_storage_service/src/api/swagger.rs
index bffbab2538..f084a27a44 100644
--- a/services/document_storage_service/src/api/swagger.rs
+++ b/services/document_storage_service/src/api/swagger.rs
@@ -368,6 +368,15 @@ use utoipa::OpenApi;
user_api_key::inbound::axum_router::list_user_api_keys_handler,
user_api_key::inbound::axum_router::delete_user_api_key_handler,
+ // Slack archive imports
+ slack_integration::inbound::axum_router::create::create,
+ slack_integration::inbound::axum_router::uploads::register,
+ slack_integration::inbound::axum_router::uploads::complete,
+ slack_integration::inbound::axum_router::jobs::list,
+ slack_integration::inbound::axum_router::jobs::progress,
+ slack_integration::inbound::axum_router::jobs::finalize,
+ slack_integration::inbound::axum_router::jobs::cancel,
+
// reminders
reminders::inbound::axum_router::get_email_followup_handler,
reminders::inbound::axum_router::set_email_followup_handler,
@@ -811,3 +820,6 @@ use utoipa::OpenApi;
)
)]
pub struct ApiDoc;
+
+#[cfg(test)]
+mod test;
diff --git a/services/document_storage_service/src/api/swagger/test.rs b/services/document_storage_service/src/api/swagger/test.rs
new file mode 100644
index 0000000000..127f5cfb1d
--- /dev/null
+++ b/services/document_storage_service/src/api/swagger/test.rs
@@ -0,0 +1,15 @@
+use super::ApiDoc;
+use slack_integration::inbound::axum_router::SlackApiDoc;
+use utoipa::OpenApi;
+
+#[test]
+fn dss_registers_all_slack_operations_and_schemas() {
+ let dss = serde_json::to_value(ApiDoc::openapi()).unwrap();
+ let slack = serde_json::to_value(SlackApiDoc::openapi()).unwrap();
+ for (path, operations) in slack["paths"].as_object().unwrap() {
+ assert_eq!(&dss["paths"][path], operations, "{path}");
+ }
+ for (name, schema) in slack["components"]["schemas"].as_object().unwrap() {
+ assert_eq!(&dss["components"]["schemas"][name], schema, "{name}");
+ }
+}
diff --git a/services/document_storage_service/src/config.rs b/services/document_storage_service/src/config.rs
index 79f58b14bf..a60ea21415 100644
--- a/services/document_storage_service/src/config.rs
+++ b/services/document_storage_service/src/config.rs
@@ -139,6 +139,11 @@ pub struct Config {
#[macro_config_default(false)]
pub calendar_search_enabled: bool,
+ /// Enable Slack import creation and uploads. Existing receipts remain
+ /// readable, finalizable and cancellable when this switch is off.
+ #[macro_config_default(false)]
+ pub slack_import_enabled: bool,
+
/// Maximum number of SQS messages to receive per poll for the delete document worker
#[macro_config_default(10)]
pub queue_max_messages: i32,
diff --git a/services/document_storage_service/src/main.rs b/services/document_storage_service/src/main.rs
index 32d3e9ddb0..8049df03a4 100644
--- a/services/document_storage_service/src/main.rs
+++ b/services/document_storage_service/src/main.rs
@@ -1280,6 +1280,38 @@ async fn run() -> anyhow::Result<()> {
authorization_state.clone(),
);
+ let slack_limits = slack_integration::domain::models::ImportLimits::default();
+ let slack_enabled = config.slack_import_enabled;
+ let slack_service = api::context::SlackService::new(
+ slack_integration::outbound::pg_slack_import_repo::PgSlackImportRepo::new(
+ db.clone(),
+ slack_limits,
+ ),
+ slack_integration::outbound::s3_storage::S3ImportStorage::new(
+ macro_aws_config::s3_client().await,
+ config.upload_staging_bucket.as_ref().to_owned(),
+ slack_limits,
+ )
+ .map_err(|_| anyhow::anyhow!("invalid Slack import storage configuration"))?,
+ slack_integration::outbound::import_ledger::CanonicalImportLedger::new(
+ import::outbound::pg_import_repo::PgImportRepo::new(db.clone()),
+ ),
+ slack_integration::outbound::gateway_notifier::GatewayImportNotifier::new(
+ conn_gateway_client.clone(),
+ ),
+ api::context::SlackAdminAuthorizer(
+ entity_access_service.clone(),
+ entity_access::outbound::PgAccessRepository::new(db.clone()),
+ ),
+ Box::new(move |_| slack_enabled),
+ slack_limits,
+ )?;
+ let slack_state = slack_integration::inbound::axum_router::SlackRouterState {
+ service: Arc::new(slack_service),
+ entity_access_service: entity_access_service.clone(),
+ authorization_state: authorization_state.clone(),
+ };
+
// Held by value here and behind an `Arc` in the router state: the impl is a
// pool handle, so cloning is cheap and `SoupImpl` needs an owned service.
// Additional connections for guards spanning email I/O. HTTP requests and
@@ -1711,6 +1743,7 @@ async fn run() -> anyhow::Result<()> {
user_api_key_service,
authorization_state.clone(),
),
+ slack_state,
reminders_state: RemindersRouterState::new(
Arc::new(reminders_service),
entity_access_service.clone(),
diff --git a/services/search_processing_service/src/api/test.rs b/services/search_processing_service/src/api/test.rs
index 3da7f89632..0ffee0314e 100644
--- a/services/search_processing_service/src/api/test.rs
+++ b/services/search_processing_service/src/api/test.rs
@@ -5,6 +5,28 @@ use axum::{
};
use tower::ServiceExt;
+#[test]
+fn channel_backfill_wire_scope_distinguishes_missing_null_empty_and_explicit() {
+ use crate::domain::models::{ChannelBackfillRequest, DeletionFilter};
+ for json in ["{}", r#"{"channel_ids":null}"#] {
+ let request: ChannelBackfillRequest = serde_json::from_str(json).unwrap();
+ assert_eq!(request.channel_ids, None);
+ }
+ let empty: ChannelBackfillRequest = serde_json::from_str(r#"{"channel_ids":[]}"#).unwrap();
+ assert_eq!(empty.channel_ids, Some(vec![]));
+ let id = uuid::Uuid::now_v7();
+ let request: ChannelBackfillRequest = serde_json::from_value(serde_json::json!({
+ "channel_ids": [id], "deletion_filter": "active", "index_override": "channels_repair"
+ }))
+ .unwrap();
+ assert_eq!(request.channel_ids, Some(vec![id]));
+ assert_eq!(request.deletion_filter, DeletionFilter::Active);
+ assert_eq!(request.index_override.as_deref(), Some("channels_repair"));
+ assert!(
+ serde_json::from_str::<ChannelBackfillRequest>(r#"{"channel_ids":["invalid"]}"#).is_err()
+ );
+}
+
#[tokio::test]
async fn health_is_reachable_at_root_and_gateway_prefix() {
for path in ["/health", "/search-processing/health"] {
diff --git a/services/search_processing_service/src/domain/models.rs b/services/search_processing_service/src/domain/models.rs
index 95991c605d..621f62f65e 100644
--- a/services/search_processing_service/src/domain/models.rs
+++ b/services/search_processing_service/src/domain/models.rs
@@ -5,6 +5,10 @@ use sqs_client::search::SearchQueueMessage;
use thiserror::Error;
/// Reply returned by every backfill port.
+///
+/// For queue-backed backfills, completion means source publication only, not
+/// consumption, indexing, or OpenSearch refresh. Failed/stale jobs can be retried
+/// with the same scope; search upserts are idempotent.
#[derive(Debug, Clone, Copy, Serialize)]
pub struct BackfillReceipt {
/// Total number of source rows the backfill processed.
@@ -144,16 +148,26 @@ pub struct ChatBackfillCursor {
pub message_id: String,
}
-/// Channel-message backfill filter. No scoping knobs yet — reserved so adding
-/// one later doesn't break the request shape.
+/// Channel-message backfill filter. Explicit scopes read from the primary so
+/// just-committed imported history is not missed due to replica lag.
#[derive(Debug, Clone, Default, Deserialize)]
#[serde(default)]
pub struct ChannelBackfillRequest {
+ /// `None` scans all channels; `Some([])` does no work.
+ pub channel_ids: Option<Vec<uuid::Uuid>>,
pub deletion_filter: DeletionFilter,
/// Override the OpenSearch target index for upserts (e.g. blue/green swap).
pub index_override: Option<String>,
}
+/// Channel messages are enumerated by `(created_at ASC, id ASC)`. The UUID
+/// breaks timestamp ties, and edits do not move messages across page boundaries.
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct ChannelBackfillCursor {
+ pub created_at: DateTime<Utc>,
+ pub message_id: uuid::Uuid,
+}
+
/// Keyset (seek-method) pagination cursor for document backfills.
///
/// `get_documents_for_search` walks `"Document"` in
diff --git a/services/search_processing_service/src/domain/ports.rs b/services/search_processing_service/src/domain/ports.rs
index 20b54d0287..d09e0c6dc6 100644
--- a/services/search_processing_service/src/domain/ports.rs
+++ b/services/search_processing_service/src/domain/ports.rs
@@ -21,9 +21,10 @@ use sqs_client::search::SearchQueueMessage;
use super::models::{
BackfillError, CalendarEventBackfillCursor, CalendarEventBackfillRequest, CallBackfillCursor,
- CallBackfillRequest, ChannelBackfillRequest, ChatBackfillCursor, ChatBackfillRequest,
- DocumentBackfillCursor, DocumentBackfillRequest, EmailBackfillRequest, ProjectBackfillCursor,
- ProjectBackfillRequest, PropertiesBackfillRequest, PropertySourcePage, SourcePage,
+ CallBackfillRequest, ChannelBackfillCursor, ChannelBackfillRequest, ChatBackfillCursor,
+ ChatBackfillRequest, DocumentBackfillCursor, DocumentBackfillRequest, EmailBackfillRequest,
+ ProjectBackfillCursor, ProjectBackfillRequest, PropertiesBackfillRequest, PropertySourcePage,
+ SourcePage,
};
/// Publishes batches of search-event messages.
@@ -76,11 +77,14 @@ pub trait BackfillSource: Send + Sync + 'static {
cursor: Option<ChatBackfillCursor>,
) -> impl Future<Output = Result<(SourcePage, Option<ChatBackfillCursor>), BackfillError>> + Send;
+ /// Enumerate only the requested channels with a stable creation-time/ID
+ /// cursor. Explicit scopes require primary-consistent reads; an empty scope
+ /// must never fall back to a global scan. An empty page ends the scan.
fn fetch_channels(
&self,
req: &ChannelBackfillRequest,
- offset: usize,
- ) -> impl Future<Output = Result<SourcePage, BackfillError>> + Send;
+ cursor: Option<ChannelBackfillCursor>,
+ ) -> impl Future<Output = Result<(SourcePage, Option<ChannelBackfillCursor>), BackfillError>> + Send;
/// Documents paginate by keyset cursor: each call passes the cursor
/// of the last row from the previous page (or `None` for the first
diff --git a/services/search_processing_service/src/domain/service.rs b/services/search_processing_service/src/domain/service.rs
index c7678896b6..1ea869bf9c 100644
--- a/services/search_processing_service/src/domain/service.rs
+++ b/services/search_processing_service/src/domain/service.rs
@@ -263,8 +263,11 @@ where
progress: Arc<JobProgress>,
cancel: CancellationToken,
) -> Result<BackfillReceipt, BackfillError> {
- drain_source(&self.publisher, &progress, &cancel, |offset| {
- self.source.fetch_channels(&req, offset)
+ if req.channel_ids.as_ref().is_some_and(Vec::is_empty) {
+ return Ok(BackfillReceipt { enqueued: 0 });
+ }
+ drain_source_with_cursor(&self.publisher, &progress, &cancel, |cursor| {
+ self.source.fetch_channels(&req, cursor)
})
.await
}
diff --git a/services/search_processing_service/src/domain/service/test.rs b/services/search_processing_service/src/domain/service/test.rs
index 058047e0a3..9f6b3151ab 100644
--- a/services/search_processing_service/src/domain/service/test.rs
+++ b/services/search_processing_service/src/domain/service/test.rs
@@ -387,6 +387,34 @@ async fn cursor_drain_stops_when_non_empty_page_returns_no_cursor() {
assert_eq!(source.observed_cursors(), vec![None]);
}
+#[tokio::test]
+async fn cursor_drain_publish_failure_counts_only_completed_pages() {
+ struct FailSecondPublish(AtomicUsize);
+ impl SearchEventPublisher for FailSecondPublish {
+ async fn publish(&self, _: Vec<SearchQueueMessage>) -> Result<(), BackfillError> {
+ if self.0.fetch_add(1, Ordering::Relaxed) == 1 {
+ return Err(BackfillError::Publish(anyhow::anyhow!("queue unavailable")));
+ }
+ Ok(())
+ }
+ }
+ let source = CursorFakeSource::new(vec![
+ (page(vec![msg("first")]), Some(1)),
+ (page(vec![msg("failed")]), Some(2)),
+ (page(vec![msg("not-fetched")]), Some(3)),
+ ]);
+ let publisher = FailSecondPublish(AtomicUsize::new(0));
+ let (progress, cancel) = detached();
+ let error = drain_source_with_cursor(&publisher, &progress, &cancel, |cursor| {
+ source.fetch_page(cursor)
+ })
+ .await
+ .unwrap_err();
+ assert!(matches!(error, BackfillError::Publish(_)));
+ assert_eq!(progress.local_count(), 1);
+ assert_eq!(source.observed_cursors(), vec![None, Some(1)]);
+}
+
#[tokio::test]
async fn cursor_drain_stops_on_empty_page() {
let source = CursorFakeSource::new(vec![]);
diff --git a/services/search_processing_service/src/main.rs b/services/search_processing_service/src/main.rs
index 4157513bca..5db8bcb084 100644
--- a/services/search_processing_service/src/main.rs
+++ b/services/search_processing_service/src/main.rs
@@ -212,10 +212,8 @@ async fn main() -> anyhow::Result<()> {
return Err(e);
}
- // Backfills run against the read-replica when available so they don't
- // contend with writes on the primary. Queue workers always read from the
- // primary because replica lag would cause them to miss rows they are
- // meant to index.
+ // Global backfills use the replica when available. Explicit channel scopes
+ // and queue workers use the primary so replica lag cannot hide imports.
let backfill_db = match resolve_readonly_pool(config.database_url_readonly.clone()).await {
Some(pool) => {
tracing::info!("using read-replica pool for backfill reads");
@@ -253,7 +251,7 @@ async fn main() -> anyhow::Result<()> {
));
let backfill_service = Arc::new(BackfillOrchestrator::new(
- PgBackfillSource::new(backfill_db, config.backfill_page_sizes()?),
+ PgBackfillSource::new(backfill_db, db.clone(), config.backfill_page_sizes()?),
SqsSearchEventPublisher::new(sqs_client.clone()),
DirectPropertyBackfillIndexer::new(db.clone(), opensearch_client.clone()),
));
diff --git a/services/search_processing_service/src/outbound/source.rs b/services/search_processing_service/src/outbound/source.rs
index 3292403b99..2cfa880f2b 100644
--- a/services/search_processing_service/src/outbound/source.rs
+++ b/services/search_processing_service/src/outbound/source.rs
@@ -13,9 +13,10 @@ use sqs_client::search::{
use crate::config::BackfillPageSizes;
use crate::domain::models::{
BackfillError, CalendarEventBackfillCursor, CalendarEventBackfillRequest, CallBackfillCursor,
- CallBackfillRequest, ChannelBackfillRequest, ChatBackfillCursor, ChatBackfillRequest,
- DocumentBackfillCursor, DocumentBackfillRequest, EmailBackfillRequest, ProjectBackfillCursor,
- ProjectBackfillRequest, PropertiesBackfillRequest, PropertySourcePage, SourcePage,
+ CallBackfillRequest, ChannelBackfillCursor, ChannelBackfillRequest, ChatBackfillCursor,
+ ChatBackfillRequest, DocumentBackfillCursor, DocumentBackfillRequest, EmailBackfillRequest,
+ ProjectBackfillCursor, ProjectBackfillRequest, PropertiesBackfillRequest, PropertySourcePage,
+ SourcePage,
};
use crate::domain::ports::BackfillSource;
@@ -26,17 +27,21 @@ const DEFAULT_EMAIL_BATCH_SIZE: usize = 50;
/// reindexed directly.
const PROPERTIES_PAGE_SIZE: usize = 5000;
-/// Postgres-backed [`BackfillSource`] for every search-indexed entity. One
-/// struct, one DB pool, per-entity page sizes — collapses what used to be
-/// five parallel adapters. New entity types just add a method here.
+/// Postgres-backed [`BackfillSource`]. Global scans may use a replica, but
+/// explicit channel scopes use the primary to include just-committed imports.
pub struct PgBackfillSource {
db: PgPool,
+ primary_db: PgPool,
page_sizes: BackfillPageSizes,
}
impl PgBackfillSource {
- pub fn new(db: PgPool, page_sizes: BackfillPageSizes) -> Self {
- Self { db, page_sizes }
+ pub fn new(db: PgPool, primary_db: PgPool, page_sizes: BackfillPageSizes) -> Self {
+ Self {
+ db,
+ primary_db,
+ page_sizes,
+ }
}
}
@@ -187,33 +192,51 @@ impl BackfillSource for PgBackfillSource {
async fn fetch_channels(
&self,
req: &ChannelBackfillRequest,
- offset: usize,
- ) -> Result<SourcePage, BackfillError> {
- let batch = comms_db_client::messages::get_messages::get_channel_messages(
- &self.db,
- self.page_sizes.channels as i64,
- offset as i64,
- req.deletion_filter.as_only_deleted(),
- )
- .await
- .map_err(BackfillError::Source)?;
+ cursor: Option<ChannelBackfillCursor>,
+ ) -> Result<(SourcePage, Option<ChannelBackfillCursor>), BackfillError> {
+ if req.channel_ids.as_ref().is_some_and(Vec::is_empty) {
+ return Ok((SourcePage::empty(), None));
+ }
+ let db = if req.channel_ids.is_some() {
+ &self.primary_db
+ } else {
+ &self.db
+ };
+ let db_cursor = cursor.map(|c| (c.created_at, c.message_id));
+ let batch =
+ comms_db_client::messages::get_messages::get_channel_messages_for_search_backfill(
+ db,
+ self.page_sizes.channels as i64,
+ db_cursor,
+ req.channel_ids.as_deref(),
+ req.deletion_filter.as_only_deleted(),
+ )
+ .await
+ .map_err(BackfillError::Source)?;
+ let next_cursor = batch.last().map(|row| ChannelBackfillCursor {
+ created_at: row.created_at,
+ message_id: row.message_id,
+ });
let rows_consumed = batch.len();
- let messages: Vec<SearchQueueMessage> = batch
+ let messages = batch
.into_iter()
- .map(|(channel_id, message_id)| {
+ .map(|row| {
SearchQueueMessage::ChannelMessageUpdate(ChannelMessageUpdate {
- channel_id: channel_id.to_string(),
- message_id: message_id.to_string(),
+ channel_id: row.channel_id.to_string(),
+ message_id: row.message_id.to_string(),
index_override: req.index_override.clone(),
})
})
.collect();
- Ok(SourcePage {
- messages,
- rows_consumed,
- })
+ Ok((
+ SourcePage {
+ messages,
+ rows_consumed,
+ },
+ next_cursor,
+ ))
}
async fn fetch_documents(
diff --git a/services/search_processing_service/src/outbound/source/test.rs b/services/search_processing_service/src/outbound/source/test.rs
index 4b8344ac1f..f8bc063052 100644
--- a/services/search_processing_service/src/outbound/source/test.rs
+++ b/services/search_processing_service/src/outbound/source/test.rs
@@ -1,4 +1,150 @@
use super::*;
+use crate::domain::jobs::JobProgress;
+use crate::domain::ports::{PropertyBackfillIndexer, SearchEventPublisher};
+use crate::domain::service::{BackfillOrchestrator, BackfillService};
+use std::sync::{Arc, Mutex};
+use tokio_util::sync::CancellationToken;
+
+fn source(db: PgPool, primary_db: PgPool) -> PgBackfillSource {
+ PgBackfillSource::new(
+ db,
+ primary_db,
+ BackfillPageSizes {
+ calls: 1,
+ chats: 1,
+ channels: 1,
+ documents: 1,
+ emails: 1,
+ projects: 1,
+ calendar_events: 1,
+ },
+ )
+}
+
+async fn closed_pool() -> PgPool {
+ let pool = sqlx::postgres::PgPoolOptions::new()
+ .connect_lazy("postgres://unused:unused@localhost/unused")
+ .unwrap();
+ pool.close().await;
+ pool
+}
+
+#[derive(Clone, Default)]
+struct ChannelPublisher(Arc<Mutex<Vec<(String, String)>>>);
+
+impl SearchEventPublisher for ChannelPublisher {
+ async fn publish(&self, messages: Vec<SearchQueueMessage>) -> Result<(), BackfillError> {
+ for message in messages {
+ let SearchQueueMessage::ChannelMessageUpdate(update) = message else {
+ panic!("expected channel update");
+ };
+ assert_eq!(update.index_override.as_deref(), Some("channels_repair"));
+ self.0
+ .lock()
+ .unwrap()
+ .push((update.channel_id, update.message_id));
+ }
+ Ok(())
+ }
+}
+
+struct UnusedPropertyIndexer;
+
+impl PropertyBackfillIndexer for UnusedPropertyIndexer {
+ async fn reindex(&self, _: &str, _: EntityType) -> Result<(), BackfillError> {
+ panic!("channel publication must not call a direct indexer");
+ }
+}
+
+#[tokio::test]
+async fn empty_channel_scope_never_reads_or_publishes() {
+ let db = closed_pool().await;
+ let source = source(db.clone(), db);
+ let request = ChannelBackfillRequest {
+ channel_ids: Some(vec![]),
+ ..Default::default()
+ };
+ let (page, cursor) = source.fetch_channels(&request, None).await.unwrap();
+ assert!(page.messages.is_empty());
+ assert_eq!(page.rows_consumed, 0);
+ assert!(cursor.is_none());
+
+ let publisher = ChannelPublisher::default();
+ let service = BackfillOrchestrator::new(source, publisher.clone(), UnusedPropertyIndexer);
+ let progress = Arc::new(JobProgress::detached());
+ let receipt = service
+ .backfill_channels(request, progress.clone(), CancellationToken::new())
+ .await
+ .unwrap();
+ assert_eq!(receipt.enqueued, 0);
+ assert_eq!(progress.local_count(), 0);
+ assert!(publisher.0.lock().unwrap().is_empty());
+}
+
+#[sqlx::test(
+ migrations = "../../crates/macro_db_client/migrations",
+ fixtures(
+ path = "../../../../../crates/comms_db_client/fixtures",
+ scripts("channels")
+ )
+)]
+async fn scoped_backfill_publishes_only_primary_channel_history_and_can_replay(pool: PgPool) {
+ use comms_db_client::messages::create_message::{CreateMessageOptions, create_message};
+ let channel = uuid::Uuid::from_u128(0x11111111_1111_1111_1111_111111111111);
+ let other = uuid::Uuid::from_u128(0x33333333_3333_3333_3333_333333333333);
+ let mut expected = Vec::new();
+ for channel_id in [channel, other, channel] {
+ let message = create_message(
+ &pool,
+ CreateMessageOptions {
+ channel_id,
+ sender_id: "macro|[email protected]".into(),
+ content: "history".into(),
+ thread_id: None,
+ },
+ )
+ .await
+ .unwrap();
+ if channel_id == channel {
+ expected.push((channel.to_string(), message.id.to_string()));
+ }
+ }
+ let replica = closed_pool().await;
+ let backfill_source = source(replica.clone(), pool.clone());
+ let publisher = ChannelPublisher::default();
+ let service =
+ BackfillOrchestrator::new(backfill_source, publisher.clone(), UnusedPropertyIndexer);
+ let request = ChannelBackfillRequest {
+ channel_ids: Some(vec![channel]),
+ index_override: Some("channels_repair".into()),
+ ..Default::default()
+ };
+ // A lost receipt or stale job can restart the same scope; publication is
+ // repeatable and downstream upserts deduplicate by message ID.
+ for _ in 0..2 {
+ let progress = Arc::new(JobProgress::detached());
+ let receipt = service
+ .backfill_channels(request.clone(), progress.clone(), CancellationToken::new())
+ .await
+ .unwrap();
+ assert_eq!(receipt.enqueued, 2);
+ assert_eq!(progress.local_count(), 2);
+ assert_eq!(std::mem::take(&mut *publisher.0.lock().unwrap()), expected);
+ }
+
+ // Conversely, global scans retain the configured backfill pool. A closed
+ // primary cannot interfere with an unscoped request.
+ let global = source(pool, replica);
+ let (page, _) = global
+ .fetch_channels(&ChannelBackfillRequest::default(), None)
+ .await
+ .unwrap();
+ assert_eq!(page.rows_consumed, 1);
+ assert!(matches!(
+ global.fetch_channels(&request, None).await,
+ Err(BackfillError::Source(_))
+ ));
+}
fn uuid(n: u128) -> uuid::Uuid {
uuid::Uuid::from_u128(n)
diff --git a/services/search_processing_service/src/process/channel.rs b/services/search_processing_service/src/process/channel.rs
index f4101cd0c1..df791f4771 100644
--- a/services/search_processing_service/src/process/channel.rs
+++ b/services/search_processing_service/src/process/channel.rs
@@ -1,4 +1,5 @@
use anyhow::Context;
+use comms_db_client::messages::get_channel_message::ChannelMessageForSearch;
use mention_utils::parse::{ParsedXmlText, PlainTextFormatter, XmlFormatter};
use opensearch_client::{
OpensearchClient, date_format::EpochMillis, upsert::channel_message::UpsertChannelMessageArgs,
@@ -22,7 +23,7 @@ pub async fn process_channel_message_update(
.await
.context("unable to get channel message")?;
- if channel_message_info.channel_message.deleted_at.is_some() {
+ if channel_message_info.message.deleted_at.is_some() {
tracing::trace!("channel message is deleted, removing from search index");
let channel_id = channel_id.to_string();
let message_id = message_id.to_string();
@@ -32,7 +33,20 @@ pub async fn process_channel_message_update(
return Ok(());
}
- let raw_content = &channel_message_info.channel_message.content;
+ let args = channel_message_upsert_args(channel_message_info)?;
+ opensearch_client
+ .upsert_channel_message(&args, index_override)
+ .await?;
+
+ Ok(())
+}
+
+fn channel_message_upsert_args(
+ ChannelMessageForSearch { message, mentions }: ChannelMessageForSearch,
+) -> anyhow::Result<UpsertChannelMessageArgs> {
+ let channel_id = message.channel_id;
+ let message_id = message.message_id;
+ let raw_content = &message.content;
let transformed_content = match ParsedXmlText::parse(raw_content) {
Ok(parsed) => PlainTextFormatter::format_xml_text(parsed).0,
Err(e) => {
@@ -41,40 +55,24 @@ pub async fn process_channel_message_update(
}
};
- let upsert_channel_message_args = UpsertChannelMessageArgs {
- channel_id: channel_message_info.channel_id.to_string(),
- channel_type: channel_message_info.channel_type.to_string(),
- org_id: channel_message_info.org_id,
- message_id: channel_message_info.channel_message.message_id.to_string(),
- thread_id: channel_message_info
- .channel_message
- .thread_id
- .unwrap_or(channel_message_info.channel_message.message_id)
- .to_string(),
- sender_id: channel_message_info.channel_message.sender_id,
- mentions: channel_message_info.channel_message.mentions,
+ Ok(UpsertChannelMessageArgs {
+ channel_id: channel_id.to_string(),
+ channel_type: message.channel_type.to_string(),
+ org_id: message.org_id,
+ message_id: message_id.to_string(),
+ thread_id: message.thread_id.unwrap_or(message_id).to_string(),
+ sender_id: message.sender_id,
+ imported_author: message.imported_author,
+ mentions,
content: transformed_content.trim().to_string(),
- created_at_millis: EpochMillis::new(
- channel_message_info
- .channel_message
- .created_at
- .timestamp_millis(),
- )?,
- updated_at_millis: EpochMillis::new(
- channel_message_info
- .channel_message
- .updated_at
- .timestamp_millis(),
- )?,
- };
-
- opensearch_client
- .upsert_channel_message(&upsert_channel_message_args, index_override)
- .await?;
-
- Ok(())
+ created_at_millis: EpochMillis::new(message.created_at.timestamp_millis())?,
+ updated_at_millis: EpochMillis::new(message.updated_at.timestamp_millis())?,
+ })
}
+#[cfg(test)]
+mod test;
+
pub async fn process_remove_channel_message(
opensearch_client: &OpensearchClient,
channel_id: Uuid,
diff --git a/services/search_processing_service/src/process/channel/test.rs b/services/search_processing_service/src/process/channel/test.rs
new file mode 100644
index 0000000000..fcb6f291b3
--- /dev/null
+++ b/services/search_processing_service/src/process/channel/test.rs
@@ -0,0 +1,37 @@
+use super::*;
+use channels::domain::models::ChannelType;
+use comms_db_client::messages::get_channel_message::ChannelMessageInfo;
+
+#[test]
+fn imported_author_is_indexed_separately_from_content_and_identity() {
+ let message_id = Uuid::now_v7();
+ let thread_id = Uuid::now_v7();
+ for (author, thread) in [(Some("unique-archive-bot"), None), (None, Some(thread_id))] {
+ let args = channel_message_upsert_args(ChannelMessageForSearch {
+ message: ChannelMessageInfo {
+ channel_id: Uuid::now_v7(),
+ name: Some("Archive".into()),
+ channel_type: ChannelType::Private,
+ org_id: None,
+ message_id,
+ thread_id: thread,
+ sender_id: "macro|[email protected]".into(),
+ imported_author: author.map(str::to_owned),
+ content: " Historical content ".into(),
+ created_at: chrono::DateTime::from_timestamp_millis(1_600_000_000_123).unwrap(),
+ updated_at: chrono::DateTime::from_timestamp_millis(1_600_000_000_456).unwrap(),
+ deleted_at: None,
+ },
+ mentions: vec!["user|macro|[email protected]".into()],
+ })
+ .unwrap();
+ assert_eq!(args.imported_author.as_deref(), author);
+ assert_eq!(args.sender_id, "macro|[email protected]");
+ assert_eq!(args.content, "Historical content");
+ assert_eq!(args.thread_id, thread.unwrap_or(message_id).to_string());
+ assert_eq!(args.mentions, ["user|macro|[email protected]"]);
+ let json = serde_json::to_value(args).unwrap();
+ assert_eq!(json["created_at_millis"], 1_600_000_000_123_i64);
+ assert_eq!(json["updated_at_millis"], 1_600_000_000_456_i64);
+ }
+}
diff --git a/services/slack_import_worker/Cargo.toml b/services/slack_import_worker/Cargo.toml
new file mode 100644
index 0000000000..a1ca896ef3
--- /dev/null
+++ b/services/slack_import_worker/Cargo.toml
@@ -0,0 +1,37 @@
+[package]
+edition = "2024"
+name = "slack_import_worker"
+publish = false
+version = "0.1.0"
+
+[dependencies]
+aws-sdk-sqs = { workspace = true }
+bot_id = { path = "../../crates/bot_id" }
+channel_sender = { path = "../../crates/channel_sender" }
+channels = { path = "../../crates/channels", features = ["outbound"] }
+chrono = { workspace = true }
+database_env_vars = { path = "../../crates/database_env_vars" }
+entity_access = { path = "../../crates/entity_access", default-features = false, features = ["outbound"] }
+import = { path = "../../crates/import" }
+macro_aws_config = { path = "../../crates/macro_aws_config", features = ["s3"] }
+macro_config = { path = "../../crates/macro_config" }
+macro_entrypoint = { path = "../../crates/macro_entrypoint" }
+macro_queues = { path = "../../crates/macro_queues" }
+macro_service_urls = { path = "../../crates/macro_service_urls" }
+macro_user_id = { path = "../../crates/macro_user_id" }
+mention_utils = { path = "../../crates/mention_utils" }
+messages = { path = "../../crates/messages", features = ["outbound"] }
+rootcause = { workspace = true }
+serde = { workspace = true }
+serde_json = { workspace = true }
+slack_integration = { path = "../../crates/slack_integration", features = ["postgres", "s3", "sqs", "search", "worker"] }
+sqlx = { workspace = true }
+tokio = { workspace = true }
+tokio-util = { workspace = true }
+uuid = { workspace = true }
+workspace-hack = { version = "0.1", path = "../../crates/workspace-hack" }
+
+[dev-dependencies]
+aws-sdk-s3 = { workspace = true }
+reqwest = { workspace = true }
+sha2 = { workspace = true }
diff --git a/services/slack_import_worker/src/composition.rs b/services/slack_import_worker/src/composition.rs
new file mode 100644
index 0000000000..16c48631a3
--- /dev/null
+++ b/services/slack_import_worker/src/composition.rs
@@ -0,0 +1,44 @@
+//! Cross-domain transaction coordination belongs here, never in another domain's adapter.
+
+pub mod authorizer;
+pub mod channel_sink;
+pub mod reference_reconciliation;
+pub mod references;
+
+#[cfg(test)]
+mod test;
+
+use import::domain::models::{ImportTargetKey, ImportTargetKind, SlackConversationId};
+use slack_integration::domain::{models::*, ports::PortResult};
+
+fn target_key(team: TeamId, source: &ConversationId) -> PortResult<ImportTargetKey> {
+ Ok(ImportTargetKey {
+ team_id: team.into(),
+ foreign_id: SlackConversationId::new(source.as_str()).ok_or(ImportError::InvalidInput)?,
+ })
+}
+
+fn target_kind(kind: ConversationKind) -> ImportTargetKind {
+ match kind {
+ ConversationKind::PublicChannel => ImportTargetKind::Team,
+ ConversationKind::PrivateChannel | ConversationKind::GroupDirectMessage => {
+ ImportTargetKind::Private
+ }
+ ConversationKind::DirectMessage => ImportTargetKind::DirectMessage,
+ }
+}
+
+fn retry(
+ error: impl std::fmt::Display + std::fmt::Debug + Send + Sync + 'static,
+) -> rootcause::Report<ImportError> {
+ rootcause::Report::new_custom::<rootcause::handlers::Display>(error)
+ .context(ImportError::Retryable)
+}
+
+fn ledger_error(error: import::domain::ports::ImportError) -> rootcause::Report<ImportError> {
+ use import::domain::ports::ImportError as E;
+ match error {
+ E::TargetConflict | E::TargetNotReserved => ImportError::Unavailable.into(),
+ other => retry(other),
+ }
+}
diff --git a/services/slack_import_worker/src/composition/authorizer.rs b/services/slack_import_worker/src/composition/authorizer.rs
new file mode 100644
index 0000000000..7f8f208d3f
--- /dev/null
+++ b/services/slack_import_worker/src/composition/authorizer.rs
@@ -0,0 +1,149 @@
+//! Translate owning-domain facts and access receipts into the import domain policy.
+
+use std::collections::HashSet;
+
+use channels::{
+ domain::models::{ChannelType, ParticipantRole},
+ outbound::pg_channels_repo::historical,
+};
+use entity_access::domain::{
+ models::{AccessError, AdminTeamRole, EntityType},
+ ports::EntityAccessService,
+};
+use import::{domain::models::ImportTargetKey, outbound::pg_import_repo::targets};
+use macro_user_id::{cowlike::CowLike, user_id::MacroUserIdStr};
+use slack_integration::domain::{
+ importer::targets::{TargetAccess, TargetFacts, TargetKind, authorize_target},
+ models::*,
+ ports::{ImportAuthorizer, PortResult},
+};
+use sqlx::{PgPool, Postgres, Transaction};
+use uuid::Uuid;
+
+use super::{ledger_error, retry, target_key, target_kind};
+
+/// Authorization through current team-admin receipts and locked owning-crate facts.
+#[derive(Clone)]
+pub struct WorkerAuthorizer<A> {
+ pool: PgPool,
+ access: A,
+}
+
+impl<A: EntityAccessService> WorkerAuthorizer<A> {
+ /// The access service is constructed by the application's composition root.
+ pub fn new(pool: PgPool, access: A) -> Self {
+ Self { pool, access }
+ }
+
+ pub(super) async fn target_in(
+ &self,
+ tx: &mut Transaction<'_, Postgres>,
+ key: &ImportTargetKey,
+ user: &MacroUserIdStr<'_>,
+ kind: ConversationKind,
+ channel: Uuid,
+ required_reservation: bool,
+ ) -> PortResult<TargetFacts> {
+ let team = key.team_id.try_into().map_err(retry)?;
+ let prior =
+ targets::lock_target_in(tx, key, channel, target_kind(kind), required_reservation)
+ .await
+ .map_err(ledger_error)?;
+ self.require_admin(team, user).await?;
+ let (facts, access) = inspect(tx, channel, Some(user))
+ .await?
+ .ok_or(ImportError::Unavailable)?;
+ authorize_target(team, kind, &facts, prior, access)?;
+ Ok(facts)
+ }
+}
+
+impl<A: EntityAccessService> ImportAuthorizer for WorkerAuthorizer<A> {
+ async fn require_admin(&self, team: TeamId, user: &MacroUserIdStr<'_>) -> PortResult<()> {
+ self.access
+ .generate_entity_access_receipt::<AdminTeamRole>(
+ &user.0,
+ None,
+ &Uuid::from(team).to_string(),
+ EntityType::Team,
+ )
+ .await
+ .map_err(|error| match error {
+ AccessError::Unauthorized => ImportError::Unavailable.into(),
+ other => retry(other),
+ })?;
+ Ok(())
+ }
+
+ async fn require_target(
+ &self,
+ team: TeamId,
+ user: &MacroUserIdStr<'_>,
+ metadata: &ConversationMetadata,
+ target: Uuid,
+ ) -> PortResult<()> {
+ let mut tx = self.pool.begin().await.map_err(retry)?;
+ self.target_in(
+ &mut tx,
+ &target_key(team, &metadata.slack_channel_id)?,
+ user,
+ metadata.kind,
+ target,
+ false,
+ )
+ .await?;
+ tx.commit().await.map_err(retry)
+ }
+}
+
+pub(super) async fn inspect(
+ tx: &mut Transaction<'_, Postgres>,
+ channel: Uuid,
+ requester: Option<&MacroUserIdStr<'_>>,
+) -> PortResult<Option<(TargetFacts, TargetAccess)>> {
+ let Some(facts) = historical::inspect_in(tx, channel, requester)
+ .await
+ .map_err(retry)?
+ else {
+ return Ok(None);
+ };
+ let kind = match facts.kind {
+ ChannelType::Team => TargetKind::Team(
+ facts
+ .team
+ .ok_or(ImportError::Unavailable)?
+ .try_into()
+ .map_err(retry)?,
+ ),
+ ChannelType::Private => TargetKind::Private,
+ ChannelType::DirectMessage => TargetKind::DirectMessage,
+ ChannelType::Public => TargetKind::Public,
+ };
+ let mut access = TargetAccess::None;
+ let mut dm_members = HashSet::new();
+ for participant in facts.participants {
+ if requester.is_some_and(|user| participant.user_id == user.as_ref())
+ && participant.left_at.is_none()
+ {
+ access = match participant.role {
+ ParticipantRole::Owner | ParticipantRole::Admin => TargetAccess::ManageParticipants,
+ ParticipantRole::Member => TargetAccess::Participant,
+ };
+ }
+ if kind == TargetKind::DirectMessage {
+ dm_members.insert(
+ MacroUserIdStr::parse_from_str(&participant.user_id)
+ .map_err(retry)?
+ .into_owned(),
+ );
+ }
+ }
+ Ok(Some((
+ TargetFacts {
+ id: channel,
+ kind,
+ dm_members,
+ },
+ access,
+ )))
+}
diff --git a/services/slack_import_worker/src/composition/channel_sink.rs b/services/slack_import_worker/src/composition/channel_sink.rs
new file mode 100644
index 0000000000..18e3d6ba7d
--- /dev/null
+++ b/services/slack_import_worker/src/composition/channel_sink.rs
@@ -0,0 +1,459 @@
+//! One transaction per historical batch, composing only owning-crate SQL helpers.
+
+#[cfg(test)]
+mod test;
+
+use std::collections::{HashMap, HashSet};
+
+use channel_sender::ChannelSender;
+use channels::{
+ domain::{
+ dm::DmPair,
+ historical::{DmCreation, HistoricalChannel, HistoricalChannelKind},
+ },
+ outbound::pg_channels_repo::{PgChannelsRepo, historical as channels_pg},
+};
+use chrono::{DateTime, Utc};
+use entity_access::domain::ports::EntityAccessService;
+use import::outbound::pg_import_repo::targets as ledger;
+use macro_user_id::user_id::MacroUserIdStr;
+use messages::{
+ domain::historical as messages_model, outbound::pg_message_repo::PgMessageRepository,
+};
+use slack_integration::{
+ domain::{
+ importer::{
+ historical_message_bytes,
+ targets::{ImportTargets, TargetFacts, TargetPlan},
+ },
+ models::*,
+ ports::{HistoricalSink, ImportAuthorizer, PortResult},
+ reference_reconciliation::IMPORTER_BODY_VERSION,
+ },
+ outbound::pg_slack_import_repo::{
+ batches::{self, BatchStart, WriteContext},
+ references,
+ },
+};
+use sqlx::PgPool;
+use uuid::Uuid;
+
+use super::{
+ authorizer::{WorkerAuthorizer, inspect},
+ ledger_error, retry, target_key, target_kind,
+};
+
+/// Concrete atomic sink and silent channel adapter for `ConversationImporter`.
+#[derive(Clone)]
+pub struct ChannelImportSink<A> {
+ pool: PgPool,
+ authorizer: WorkerAuthorizer<A>,
+ limits: ImportLimits,
+}
+
+impl<A: EntityAccessService> ChannelImportSink<A> {
+ /// Share one pool across all participating owning-crate helpers.
+ pub fn new(
+ pool: PgPool,
+ authorizer: WorkerAuthorizer<A>,
+ limits: ImportLimits,
+ ) -> Result<Self, ImportError> {
+ if limits.database_batch_messages == 0
+ || limits.database_batch_messages > ImportLimits::default().database_batch_messages
+ || limits.database_batch_bytes == 0
+ || limits.database_batch_bytes > ImportLimits::default().database_batch_bytes
+ {
+ return Err(ImportError::InvalidInput);
+ }
+ Ok(Self {
+ pool,
+ authorizer,
+ limits,
+ })
+ }
+}
+
+impl<A: EntityAccessService> HistoricalSink for ChannelImportSink<A> {
+ async fn lookup(
+ &self,
+ sources: &[SourceMessageId],
+ ) -> PortResult<Vec<(SourceMessageId, Uuid)>> {
+ batches::lookup(&mut *self.pool.acquire().await.map_err(retry)?, sources).await
+ }
+
+ async fn commit(&self, mut batch: HistoricalBatch) -> PortResult<ImportCounters> {
+ let records = (batch.messages.len() as u64)
+ .checked_add(batch.skipped)
+ .ok_or(ImportError::LimitExceeded)?;
+ if records > u64::from(self.limits.database_batch_messages) {
+ return Err(ImportError::LimitExceeded.into());
+ }
+ let mut bytes = 0_u64;
+ for message in &batch.messages {
+ bytes = bytes
+ .checked_add(historical_message_bytes(message)?)
+ .ok_or(ImportError::LimitExceeded)?;
+ }
+ if bytes > self.limits.database_batch_bytes {
+ return Err(ImportError::LimitExceeded.into());
+ }
+ let mut tx = self.pool.begin().await.map_err(retry)?;
+ let context = batches::write_context(&mut tx, &batch.lease).await?;
+ let channel = context.channel.ok_or(ImportError::Unavailable)?;
+ self.authorizer
+ .target_in(
+ &mut tx,
+ &target_key(context.team, &batch.lease.event.slack_channel_id)?,
+ &context.requester,
+ context.kind,
+ channel,
+ true,
+ )
+ .await?;
+ let start = batches::begin_batch(
+ &mut tx,
+ &batch.lease,
+ context.checkpoint,
+ batch.checkpoint,
+ Some(channel),
+ )
+ .await?;
+ let mut fenced = match start {
+ BatchStart::Replayed(counters) => return Ok(counters),
+ BatchStart::Ready(fenced) => fenced,
+ };
+ // Speculative IDs from domain parent resolution are not durable authority.
+ // Resolve again after the canonical source lock; generate only missing IDs.
+ let sources: Vec<_> = batch.messages.iter().map(|m| m.source.clone()).collect();
+ let existing: HashMap<_, _> = batches::lookup(fenced.transaction(), &sources)
+ .await?
+ .into_iter()
+ .collect();
+ let original_ids: Vec<_> = batch.messages.iter().map(|message| message.id).collect();
+ for message in &mut batch.messages {
+ let id = existing
+ .get(&message.source)
+ .copied()
+ .unwrap_or_else(Uuid::now_v7);
+ message.id = id;
+ }
+ let mappings = fenced.reserve_mappings(&batch.messages).await?;
+ // Reservation arbitration, not the earlier read, determines final IDs.
+ let remapped: HashMap<_, _> = original_ids
+ .into_iter()
+ .zip(mappings.iter().map(|mapping| mapping.message_id))
+ .collect();
+ let missing_parents: Vec<_> = batch
+ .messages
+ .iter()
+ .filter_map(|m| {
+ m.orphaned_thread_ts.map(|ts| SourceMessageId {
+ ts,
+ ..m.source.clone()
+ })
+ })
+ .collect();
+ let mut parents: HashMap<_, _> = batches::lookup(fenced.transaction(), &missing_parents)
+ .await?
+ .into_iter()
+ .collect();
+ parents.extend(mappings.iter().map(|m| (m.source.clone(), m.message_id)));
+ let mut messages = Vec::new();
+ let mut reactions = 0;
+ for (mut message, mapping) in batch.messages.into_iter().zip(mappings) {
+ if !mapping.inserted {
+ continue;
+ } // First commit wins, including reactions and live edits.
+ message.id = mapping.message_id;
+ if let Some(parent) = message.parent_id {
+ message.parent_id = Some(remapped.get(&parent).copied().unwrap_or(parent));
+ }
+ if let Some(ts) = message.orphaned_thread_ts
+ && let Some(parent) = parents.get(&SourceMessageId {
+ ts,
+ ..message.source.clone()
+ })
+ {
+ message.parent_id = Some(*parent);
+ message.orphaned_thread_ts = None;
+ }
+ references::insert_in(fenced.transaction(), &batch.lease, &message).await?;
+ let message = convert(message, batch.lease.event.job_id)?;
+ reactions += message.reactions.len() as u64;
+ messages.push(message);
+ }
+ let activity = messages.iter().map(|m| m.created_at).max();
+ PgMessageRepository::insert_historical_in(
+ fenced.transaction(),
+ &messages_model::HistoricalBatch {
+ channel_id: channel,
+ messages,
+ },
+ )
+ .await
+ .map_err(|error| {
+ use messages::domain::ports::MessageError;
+ match error {
+ MessageError::Invalid(_) | MessageError::Conflict => {
+ ImportError::InvalidInput.into()
+ }
+ other => retry(other),
+ }
+ })?;
+ if let Some(activity) = activity {
+ channels_pg::advance_historical_activity(fenced.transaction(), channel, activity)
+ .await
+ .map_err(retry)?;
+ }
+ let counters = fenced.finish(batch.skipped, reactions).await?;
+ tx.commit().await.map_err(retry)?;
+ Ok(counters)
+ }
+}
+
+impl<A: EntityAccessService> ImportTargets for ChannelImportSink<A> {
+ async fn find_dm(
+ &self,
+ pair: &[MacroUserIdStr<'static>; 2],
+ ) -> PortResult<Option<TargetFacts>> {
+ let pair =
+ DmPair::new(pair[0].clone(), pair[1].clone()).map_err(|_| ImportError::InvalidInput)?;
+ let mut tx = self.pool.begin().await.map_err(retry)?;
+ let Some(id) = channels_pg::find_dm(&mut tx, &pair).await.map_err(retry)? else {
+ return Ok(None);
+ };
+ Ok(inspect(&mut tx, id, None).await?.map(|(facts, _)| facts))
+ }
+
+ async fn inspect(&self, channel: Uuid) -> PortResult<Option<TargetFacts>> {
+ let mut tx = self.pool.begin().await.map_err(retry)?;
+ // Discovery returns no authority; every mutation checks the real requester.
+ Ok(inspect(&mut tx, channel, None)
+ .await?
+ .map(|(facts, _)| facts))
+ }
+
+ async fn create(&self, lease: &Lease, plan: &TargetPlan) -> PortResult<(TargetFacts, bool)> {
+ let mut tx = self.pool.begin().await.map_err(retry)?;
+ let context = batches::write_context(&mut tx, lease).await?;
+ validate_plan(&context, lease, plan)?;
+ self.authorizer
+ .require_admin(context.team, &context.requester)
+ .await?;
+ let key = target_key(context.team, &lease.event.slack_channel_id)?;
+ ledger::lock_target_in(
+ &mut tx,
+ &key,
+ plan.channel_id,
+ target_kind(context.kind),
+ true,
+ )
+ .await
+ .map_err(ledger_error)?;
+ if inspect(&mut tx, plan.channel_id, Some(&context.requester))
+ .await?
+ .is_some()
+ {
+ let facts = self
+ .authorizer
+ .target_in(
+ &mut tx,
+ &key,
+ &context.requester,
+ context.kind,
+ plan.channel_id,
+ true,
+ )
+ .await?;
+ validate_pair(plan, &facts)?;
+ batches::write_context(&mut tx, lease).await?;
+ tx.commit().await.map_err(retry)?;
+ return Ok((facts, false));
+ }
+ let created = match context.kind {
+ ConversationKind::DirectMessage => {
+ let mut members = plan.members.iter();
+ if plan.members.len() != 2 {
+ return Err(ImportError::InvalidInput.into());
+ }
+ let pair = DmPair::new(
+ members.next().unwrap().clone(),
+ members.next().unwrap().clone(),
+ )
+ .map_err(|_| ImportError::InvalidInput)?;
+ channels_pg::ensure_dm_in(
+ &mut tx,
+ pair,
+ DmCreation::Historical {
+ id: plan.channel_id,
+ created_at: plan.created_at,
+ },
+ )
+ .await
+ .map_err(retry)?
+ }
+ kind => PgChannelsRepo::ensure_reserved_channel_in(
+ &mut tx,
+ &HistoricalChannel {
+ id: plan.channel_id,
+ name: plan.name.clone(),
+ owner: plan.owner.clone(),
+ participants: plan.members.clone(),
+ created_at: plan.created_at,
+ kind: if kind == ConversationKind::PublicChannel {
+ HistoricalChannelKind::Team(context.team.into())
+ } else {
+ HistoricalChannelKind::Private
+ },
+ },
+ false,
+ )
+ .await
+ .map_err(retry)?,
+ };
+ if created.id != plan.channel_id || !created.created {
+ return Err(ImportError::Unavailable.into());
+ }
+ // Creation and provenance commit together. A retry also recovers older
+ // pending reservations with an existing authorized channel via bind/complete.
+ ledger::complete_target_in(&mut tx, &key, plan.channel_id, target_kind(context.kind))
+ .await
+ .map_err(ledger_error)?;
+ let facts = self
+ .authorizer
+ .target_in(
+ &mut tx,
+ &key,
+ &context.requester,
+ context.kind,
+ plan.channel_id,
+ true,
+ )
+ .await?;
+ validate_pair(plan, &facts)?;
+ batches::write_context(&mut tx, lease).await?;
+ tx.commit().await.map_err(retry)?;
+ Ok((facts, true))
+ }
+
+ async fn bind(
+ &self,
+ lease: &Lease,
+ plan: &TargetPlan,
+ warnings: &[ImportWarning],
+ ) -> PortResult<()> {
+ let mut tx = self.pool.begin().await.map_err(retry)?;
+ let context = batches::write_context(&mut tx, lease).await?;
+ validate_plan(&context, lease, plan)?;
+ let facts = self
+ .authorizer
+ .target_in(
+ &mut tx,
+ &target_key(context.team, &lease.event.slack_channel_id)?,
+ &context.requester,
+ context.kind,
+ plan.channel_id,
+ true,
+ )
+ .await?;
+ validate_pair(plan, &facts)?;
+ if context.kind != ConversationKind::DirectMessage {
+ channels_pg::insert_members(
+ &mut tx,
+ plan.channel_id,
+ &plan.members.iter().cloned().collect::<Vec<_>>(),
+ plan.created_at,
+ )
+ .await
+ .map_err(retry)?;
+ }
+ batches::bind_target(&mut tx, lease, plan.channel_id).await?;
+ batches::record_warnings(&mut tx, lease, warnings).await?;
+ tx.commit().await.map_err(retry)
+ }
+
+ async fn warn(&self, lease: &Lease, warning: ImportWarning) -> PortResult<()> {
+ let mut tx = self.pool.begin().await.map_err(retry)?;
+ batches::record_warnings(&mut tx, lease, &[warning]).await?;
+ tx.commit().await.map_err(retry)
+ }
+}
+
+fn validate_plan(context: &WriteContext, lease: &Lease, plan: &TargetPlan) -> PortResult<()> {
+ if context.team != plan.team_id
+ || context.requester != plan.requested_by
+ || context.kind != plan.metadata.kind
+ || lease.event.slack_channel_id != plan.metadata.slack_channel_id
+ || context.channel.is_some_and(|id| id != plan.channel_id)
+ {
+ return Err(ImportError::Unavailable.into());
+ }
+ Ok(())
+}
+
+fn validate_pair(plan: &TargetPlan, facts: &TargetFacts) -> PortResult<()> {
+ if plan.metadata.kind == ConversationKind::DirectMessage && facts.dm_members != plan.members {
+ return Err(ImportError::Unavailable.into());
+ }
+ Ok(())
+}
+
+fn timestamp(ts: SlackTimestamp) -> PortResult<DateTime<Utc>> {
+ DateTime::from_timestamp_micros(ts.unix_micros())
+ .ok_or_else(|| ImportError::InvalidInput.into())
+}
+
+fn convert(
+ message: HistoricalMessage,
+ job: JobId,
+) -> PortResult<messages_model::HistoricalMessage> {
+ let created_at = timestamp(message.source.ts)?;
+ let mut seen = HashSet::new();
+ let mut reactions = Vec::new();
+ for reaction in message.reactions {
+ if seen.insert((reaction.user_id.clone(), reaction.emoji.clone())) {
+ reactions.push(messages_model::HistoricalReaction {
+ user_id: reaction.user_id,
+ emoji: reaction.emoji,
+ created_at: timestamp(reaction.created_at)?,
+ });
+ }
+ }
+ if message.user_mentions.len() > slack_integration::domain::slack::references::MAX_REFERENCES {
+ return Err(ImportError::LimitExceeded.into());
+ }
+ let mut users = HashSet::new();
+ let mentions = message
+ .user_mentions
+ .into_iter()
+ .filter(|user| users.insert(user.clone()))
+ .map(|user_id| messages_model::HistoricalUserMention {
+ id: Uuid::now_v7(),
+ user_id,
+ })
+ .collect();
+ Ok(messages_model::HistoricalMessage {
+ id: message.id,
+ thread_id: message.parent_id,
+ sender: match message.sender {
+ HistoricalSender::User(user) => ChannelSender::new_from_user(user),
+ HistoricalSender::SystemBot => ChannelSender::new_from_bot(bot_id::MACRO_SYSTEM_BOT_ID),
+ },
+ imported_author: message.imported_author,
+ content: message.content,
+ created_at,
+ updated_at: created_at,
+ edited_at: None,
+ import_metadata: serde_json::json!({
+ "source": "slack",
+ "slack_channel_id": message.source.slack_channel_id,
+ "slack_ts": message.source.ts,
+ "orphaned_thread_ts": message.orphaned_thread_ts,
+ "slack_import_job": Uuid::from(job),
+ "body_version": IMPORTER_BODY_VERSION,
+ }),
+ import_order: i64::try_from(message.import_order).map_err(|_| ImportError::InvalidInput)?,
+ reactions,
+ mentions,
+ })
+}
diff --git a/services/slack_import_worker/src/composition/channel_sink/test.rs b/services/slack_import_worker/src/composition/channel_sink/test.rs
new file mode 100644
index 0000000000..b41c5a4b63
--- /dev/null
+++ b/services/slack_import_worker/src/composition/channel_sink/test.rs
@@ -0,0 +1,29 @@
+use super::*;
+
+#[test]
+fn fallback_attribution_uses_the_canonical_bot_not_a_fabricated_user() {
+ let message = HistoricalMessage {
+ id: Uuid::now_v7(),
+ source: SourceMessageId {
+ team_id: Uuid::now_v7().try_into().unwrap(),
+ slack_channel_id: "C123".parse().unwrap(),
+ ts: "1.000001".parse().unwrap(),
+ },
+ channel_id: Uuid::now_v7(),
+ parent_id: None,
+ orphaned_thread_ts: None,
+ sender: HistoricalSender::SystemBot,
+ imported_author: Some("Archive author".into()),
+ content: "historical".into(),
+ user_mentions: vec![],
+ body_references: vec![],
+ import_order: 0,
+ reactions: vec![],
+ };
+ let stored = convert(message, Uuid::now_v7().try_into().unwrap()).unwrap();
+ assert_eq!(
+ stored.sender,
+ ChannelSender::new_from_bot(bot_id::MACRO_SYSTEM_BOT_ID)
+ );
+ assert_eq!(stored.imported_author.as_deref(), Some("Archive author"));
+}
diff --git a/services/slack_import_worker/src/composition/fail_inserts.sql b/services/slack_import_worker/src/composition/fail_inserts.sql
new file mode 100644
index 0000000000..a80fbbb549
--- /dev/null
+++ b/services/slack_import_worker/src/composition/fail_inserts.sql
@@ -0,0 +1,7 @@
+-- Installed only in each SQLx test's isolated database. Test-selected AFTER
+-- triggers prove that even inserts which succeeded inside an owning helper roll back.
+CREATE FUNCTION fail_import_insert() RETURNS trigger LANGUAGE plpgsql AS $$
+BEGIN
+ RAISE EXCEPTION 'injected failure after % insertion', TG_TABLE_NAME;
+END;
+$$;
diff --git a/services/slack_import_worker/src/composition/reference_reconciliation.rs b/services/slack_import_worker/src/composition/reference_reconciliation.rs
new file mode 100644
index 0000000000..d5a7fec95e
--- /dev/null
+++ b/services/slack_import_worker/src/composition/reference_reconciliation.rs
@@ -0,0 +1,83 @@
+//! Cross-domain atomic reconciliation: the import owner holds the job fence,
+//! domain policy resolves disclosure, and the message owner guards body mutation.
+
+use messages::{
+ domain::historical::HistoricalBodyPatch, outbound::pg_message_repo::PgMessageRepository,
+};
+use slack_integration::{
+ domain::{
+ models::*,
+ ports::{PortResult, ReferenceReconciler},
+ reference_reconciliation::final_body,
+ },
+ outbound::pg_slack_import_repo::references,
+};
+use sqlx::PgPool;
+use uuid::Uuid;
+
+use super::{references::WorkerReferenceLookup, retry};
+
+/// Bounded maintenance capability. No uploads, import claims or live edit services.
+pub struct WorkerReferenceReconciler {
+ pool: PgPool,
+ lookup: WorkerReferenceLookup,
+ limits: ImportLimits,
+}
+
+impl WorkerReferenceReconciler {
+ /// Use the same database as historical batch persistence.
+ pub fn new(pool: PgPool, limits: ImportLimits) -> Self {
+ Self {
+ lookup: WorkerReferenceLookup::new(pool.clone(), limits),
+ pool,
+ limits,
+ }
+ }
+
+ async fn reconcile_one(&self) -> PortResult<bool> {
+ let mut tx = self.pool.begin().await.map_err(retry)?;
+ let Some(mut fenced) = references::next_in(&mut tx).await? else {
+ return Ok(false);
+ };
+ let work = fenced.work();
+ let context = self.lookup.context(work.team, work.job).await?;
+ let patch = match (&work.template, context) {
+ (Some(template), Some(context)) => {
+ final_body(&self.lookup, &context, template, work.version, &self.limits)
+ .await?
+ .map(|body| HistoricalBodyPatch {
+ message_id: work.message,
+ channel_id: work.channel,
+ job_id: Uuid::from(work.job),
+ importer_version: work.version,
+ expected_body: template.body.clone(),
+ body,
+ })
+ }
+ _ => None,
+ };
+ let changed = if let Some(patch) = patch {
+ // Resolution above is a fresh disclosure check immediately before this
+ // compare-and-set. Never trust authorization from the original import.
+ PgMessageRepository::patch_historical_body_in(fenced.transaction(), &patch)
+ .await
+ .map_err(retry)?
+ } else {
+ false
+ };
+ fenced.finish(changed).await?;
+ tx.commit().await.map_err(retry)?;
+ Ok(true)
+ }
+}
+
+impl ReferenceReconciler for WorkerReferenceReconciler {
+ async fn reconcile_references(&self, limit: u32) -> PortResult<()> {
+ for _ in 0..limit.min(50) {
+ if !self.reconcile_one().await? {
+ break;
+ }
+ }
+ Ok(())
+ }
+}
diff --git a/services/slack_import_worker/src/composition/references.rs b/services/slack_import_worker/src/composition/references.rs
new file mode 100644
index 0000000000..41fc9ed0b7
--- /dev/null
+++ b/services/slack_import_worker/src/composition/references.rs
@@ -0,0 +1,151 @@
+//! Read-only cross-domain reference wiring. No channel reservation, row locks,
+//! membership changes, Slack calls, or body writes occur in this composition.
+
+use std::collections::HashMap;
+
+use entity_access::{domain::ports::AccessRepository, outbound::PgAccessRepository};
+use import::outbound::pg_import_repo::PgImportRepo;
+use messages::{
+ domain::ports::HistoricalMessageReader, outbound::pg_message_repo::PgMessageRepository,
+};
+use slack_integration::{
+ domain::{
+ models::*,
+ ports::{ImportLedger, PortResult, ReferenceLookup, SourceMessageReader},
+ slack::references::resolve::*,
+ },
+ outbound::{import_ledger::CanonicalImportLedger, pg_slack_import_repo::PgSlackImportRepo},
+};
+use sqlx::PgPool;
+use uuid::Uuid;
+
+use super::retry;
+
+/// Concrete read-only capabilities, also usable by deferred job reconciliation.
+pub struct WorkerReferenceLookup {
+ ledger: CanonicalImportLedger<PgImportRepo>,
+ imports: PgSlackImportRepo,
+ messages: PgMessageRepository,
+ access: PgAccessRepository,
+}
+
+impl WorkerReferenceLookup {
+ /// Construct only at the application composition boundary.
+ pub fn new(pool: PgPool, limits: ImportLimits) -> Self {
+ Self {
+ ledger: CanonicalImportLedger::new(PgImportRepo::new(pool.clone())),
+ imports: PgSlackImportRepo::new(pool.clone(), limits),
+ messages: PgMessageRepository::new(pool.clone()),
+ access: PgAccessRepository::new(pool),
+ }
+ }
+
+ /// Build trusted context from an explicit team-owned job. No v1 persistence
+ /// establishes workspace domains, so permalink domain evidence stays empty.
+ pub async fn context(&self, team: TeamId, job: JobId) -> PortResult<Option<ReferenceContext>> {
+ let Some((requester, source)) = self.imports.reference_job(team, job).await? else {
+ return Ok(None);
+ };
+ Ok(Some(ReferenceContext {
+ team,
+ job,
+ requester,
+ source,
+ binding: self.ledger.source_binding(team).await?,
+ domains: vec![],
+ }))
+ }
+}
+
+impl ReferenceLookup for WorkerReferenceLookup {
+ async fn channels(
+ &self,
+ context: &ReferenceContext,
+ channels: &[ConversationId],
+ ) -> PortResult<Vec<ChannelMapping>> {
+ self.ledger
+ .reference_channels(context.team, &context.binding, channels)
+ .await
+ }
+
+ async fn pending_channels(
+ &self,
+ context: &ReferenceContext,
+ channels: &[ConversationId],
+ ) -> PortResult<Vec<ConversationId>> {
+ self.imports
+ .pending_reference_channels(context.team, context.job, channels)
+ .await
+ }
+
+ async fn messages(
+ &self,
+ context: &ReferenceContext,
+ sources: &[SourceMessageId],
+ ) -> PortResult<Vec<MessageMapping>> {
+ let mappings = self
+ .imports
+ .reference_mappings(context.team, sources)
+ .await?;
+ let ids: Vec<_> = mappings
+ .iter()
+ .map(|m| m.message)
+ .collect::<std::collections::HashSet<_>>()
+ .into_iter()
+ .collect();
+ let actual: HashMap<_, _> = self
+ .messages
+ .lookup_historical_targets(&ids)
+ .await
+ .map_err(retry)?
+ .into_iter()
+ .map(|message| (message.message_id, message))
+ .collect();
+ let mappings: HashMap<_, _> = mappings
+ .into_iter()
+ .map(|mapping| (mapping.source.clone(), mapping))
+ .collect();
+ Ok(sources
+ .iter()
+ .map(|source| {
+ let Some(mapping) = mappings.get(source) else {
+ return MessageMapping::Missing;
+ };
+ match actual.get(&mapping.message) {
+ Some(message) if message.channel_id == mapping.channel => {
+ MessageMapping::Ready {
+ channel: mapping.channel,
+ message: message.message_id,
+ root: message.root_id,
+ }
+ }
+ _ => MessageMapping::Invalid,
+ }
+ })
+ .collect())
+ }
+
+ async fn disclosure_access(
+ &self,
+ context: &ReferenceContext,
+ channels: &[Uuid],
+ ) -> PortResult<DisclosureAccess> {
+ if channels.len() > ImportLimits::default().database_batch_messages as usize {
+ return Err(ImportError::LimitExceeded.into());
+ }
+ let team = self
+ .access
+ .get_user_team(&context.requester.0)
+ .await
+ .map_err(retry)?;
+ let participants = self
+ .access
+ .check_user_channel_membership(Some(&context.requester.0), channels)
+ .await
+ .map_err(retry)?;
+ Ok(DisclosureAccess {
+ team_member: team.is_some_and(|team| team.team_id == Uuid::from(context.team)),
+ participant_channels: participants.into_iter().collect(),
+ })
+ }
+}
diff --git a/services/slack_import_worker/src/composition/test.rs b/services/slack_import_worker/src/composition/test.rs
new file mode 100644
index 0000000000..4711be37f0
--- /dev/null
+++ b/services/slack_import_worker/src/composition/test.rs
@@ -0,0 +1,785 @@
+mod reference_reconciliation;
+
+use std::collections::HashSet;
+
+use channels::domain::historical::{HistoricalChannel, HistoricalChannelKind};
+use channels::outbound::pg_channels_repo::PgChannelsRepo;
+use entity_access::{domain::service::EntityAccessServiceImpl, outbound::PgAccessRepository};
+use import::{domain::ports::CanonicalImportRepo, outbound::pg_import_repo::PgImportRepo};
+use macro_user_id::user_id::MacroUserIdStr;
+use slack_integration::{
+ domain::{
+ importer::targets::{ImportTargets, TargetPlan},
+ models::*,
+ ports::{ExecutionRepo, HistoricalSink, ImportAuthorizer, ImportLedger, ImportRepo},
+ },
+ outbound::{import_ledger::CanonicalImportLedger, pg_slack_import_repo::PgSlackImportRepo},
+};
+use sqlx::{Executor, PgPool};
+use uuid::Uuid;
+
+use super::{
+ authorizer::WorkerAuthorizer, channel_sink::ChannelImportSink, target_key, target_kind,
+};
+
+#[sqlx::test(migrations = "../../crates/macro_db_client/migrations")]
+async fn code_tags_never_create_mentions_and_reference_reads_recheck_access(pool: PgPool) {
+ check_read_access_and_mentions(pool, ConversationKind::PrivateChannel).await;
+}
+
+#[sqlx::test(migrations = "../../crates/macro_db_client/migrations")]
+async fn dm_reference_disclosure_is_revoked_without_removing_import_provenance(pool: PgPool) {
+ check_read_access_and_mentions(pool, ConversationKind::DirectMessage).await;
+}
+
+async fn check_read_access_and_mentions(pool: PgPool, kind: ConversationKind) {
+ use slack_integration::domain::{
+ ports::ReferenceLookup,
+ slack::{mrkdwn::MrkdwnConverter, references::resolve::*, users::UserDirectory},
+ };
+ let team = team(&pool).await;
+ let context = claim(&pool, team, kind, 2).await;
+ let target = target(&pool, &context).await;
+ let directory = UserDirectory::new(
+ serde_json::from_value::<Vec<slack_integration::domain::slack::users::ExportUser>>(
+ serde_json::json!([
+ {"id": "U1", "profile": {"email": "[email protected]"}}
+ ]),
+ )
+ .unwrap(),
+ )
+ .unwrap();
+ let names = Default::default();
+ let converter = MrkdwnConverter {
+ users: &directory,
+ channels: &names,
+ };
+ let literal = r#"`<m-user-mention>{"userId":"macro|[email protected]"}</m-user-mention>`"#;
+ let converted = converter.convert_with_references(literal, false).unwrap();
+ assert!(converted.user_mentions.is_empty());
+ let mut batch = batch(
+ &context,
+ target.channel_id,
+ &[1, 2],
+ Checkpoint {
+ part_index: 1,
+ record_index: 0,
+ },
+ );
+ batch.messages[0].content = converted.body;
+ batch.messages[0].user_mentions = converted.user_mentions;
+ let converted = converter
+ .convert_with_references(&format!("{literal} <@U1>"), false)
+ .unwrap();
+ batch.messages[1].content = converted.body;
+ batch.messages[1].user_mentions = converted.user_mentions;
+ let sources: Vec<_> = batch.messages.iter().map(|m| m.source.clone()).collect();
+ sink(&pool).commit(batch).await.unwrap();
+ assert_eq!(
+ sqlx::query_scalar!("SELECT count(*) FROM comms_entity_mentions")
+ .fetch_one(&pool)
+ .await
+ .unwrap(),
+ Some(1)
+ );
+ let lookup =
+ super::references::WorkerReferenceLookup::new(pool.clone(), ImportLimits::default());
+ let scope = lookup
+ .context(team, context.lease.event.job_id)
+ .await
+ .unwrap()
+ .unwrap();
+ assert!(
+ scope.domains.is_empty(),
+ "unknown-source confirmation is not domain evidence"
+ );
+ let token = converter
+ .convert_with_references("<#C1|untrusted>", false)
+ .unwrap();
+ let resolved = resolve_batch(&lookup, &scope, &[token.clone()], &ImportLimits::default())
+ .await
+ .unwrap();
+ assert!(matches!(resolved[0][0], ReferenceOutcome::Resolved(_)));
+ let mapped = lookup.messages(&scope, &sources).await.unwrap();
+ assert!(mapped.iter().all(|m| matches!(m, MessageMapping::Ready { channel, message, root } if *channel == target.channel_id && message == root)));
+ let requester = user();
+ sqlx::query!("UPDATE comms_channel_participants SET left_at = now() WHERE channel_id = $1 AND user_id = $2", target.channel_id, requester.as_ref()).execute(&pool).await.unwrap();
+ assert_eq!(
+ resolve_batch(&lookup, &scope, &[token], &ImportLimits::default())
+ .await
+ .unwrap(),
+ vec![vec![ReferenceOutcome::Fallback]]
+ );
+ // The ready private import provenance remains, but is not read permission.
+ assert!(matches!(
+ lookup
+ .channels(&scope, &["C1".parse().unwrap()])
+ .await
+ .unwrap()[0],
+ ChannelMapping::Ready { .. }
+ ));
+ sqlx::query!(
+ "UPDATE comms_messages SET deleted_at = now() WHERE channel_id = $1",
+ target.channel_id
+ )
+ .execute(&pool)
+ .await
+ .unwrap();
+ assert_eq!(
+ lookup.messages(&scope, &sources).await.unwrap(),
+ vec![MessageMapping::Invalid; 2]
+ );
+}
+
+#[sqlx::test(migrations = "../../crates/macro_db_client/migrations")]
+async fn prior_archived_unselected_channels_resolve_but_selected_absent_targets_wait(pool: PgPool) {
+ use slack_integration::domain::slack::{
+ mrkdwn::MrkdwnConverter,
+ references::{ResolvedTarget, resolve::*},
+ users::UserDirectory,
+ };
+ let team = team(&pool).await;
+ let first =
+ claim_with_archive_state(&pool, team, ConversationKind::PublicChannel, 0, true).await;
+ let target = target(&pool, &first).await;
+ let repo = repo(&pool);
+ repo.settle(&first.lease, ConversationStatus::Completed, None)
+ .await
+ .unwrap();
+ let mut metadata = first.metadata.clone();
+ metadata.slack_channel_id = "C2".parse().unwrap();
+ let second = repo
+ .create(
+ team,
+ &user(),
+ &CreateImport {
+ idempotency_token: Uuid::now_v7().try_into().unwrap(),
+ source: SourceIdentity::ConfirmedUnknown,
+ include_message_history: false,
+ conversations: vec![metadata],
+ },
+ &ImportLimits::default(),
+ )
+ .await
+ .unwrap();
+ let lookup =
+ super::references::WorkerReferenceLookup::new(pool.clone(), ImportLimits::default());
+ let scope = lookup.context(team, second.job_id).await.unwrap().unwrap();
+ let users = UserDirectory::new(vec![]).unwrap();
+ let names = Default::default();
+ let record = MrkdwnConverter {
+ users: &users,
+ channels: &names,
+ }
+ .convert_with_references("<#C1|same> <#C2|same> <#C3|same>", false)
+ .unwrap();
+ let before = repo.progress(team, second.job_id).await.unwrap();
+ let outcomes = resolve_batch(&lookup, &scope, &[record], &ImportLimits::default())
+ .await
+ .unwrap();
+ assert_eq!(
+ outcomes[0],
+ vec![
+ ReferenceOutcome::Resolved(ResolvedTarget::Channel {
+ channel_id: target.channel_id,
+ name: target.name
+ }),
+ ReferenceOutcome::Pending,
+ ReferenceOutcome::Fallback,
+ ]
+ );
+ assert_eq!(before, repo.progress(team, second.job_id).await.unwrap());
+}
+
+type Access = EntityAccessServiceImpl<PgAccessRepository>;
+type Sink = ChannelImportSink<Access>;
+
+fn user() -> MacroUserIdStr<'static> {
+ MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap()
+}
+fn other() -> MacroUserIdStr<'static> {
+ MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap()
+}
+fn authorizer(pool: &PgPool) -> WorkerAuthorizer<Access> {
+ WorkerAuthorizer::new(
+ pool.clone(),
+ EntityAccessServiceImpl::new(PgAccessRepository::new(pool.clone())),
+ )
+}
+fn sink(pool: &PgPool) -> Sink {
+ ChannelImportSink::new(pool.clone(), authorizer(pool), ImportLimits::default()).unwrap()
+}
+fn repo(pool: &PgPool) -> PgSlackImportRepo {
+ PgSlackImportRepo::new(pool.clone(), ImportLimits::default())
+}
+
+async fn team(pool: &PgPool) -> TeamId {
+ let account = Uuid::now_v7();
+ let team = Uuid::now_v7();
+ sqlx::query!("INSERT INTO macro_user (id, username, email, stripe_customer_id) VALUES ($1, 't17', '[email protected]', 't17-customer')", account).execute(pool).await.unwrap();
+ sqlx::query!(r#"INSERT INTO "User" (id, email, macro_user_id) VALUES ('macro|[email protected]', '[email protected]', $1)"#, account).execute(pool).await.unwrap();
+ sqlx::query!("INSERT INTO team (id, name, owner_id) VALUES ($1, 'Atomic history', 'macro|[email protected]')", team).execute(pool).await.unwrap();
+ sqlx::query!("INSERT INTO team_user (team_id, user_id, team_role) VALUES ($1, 'macro|[email protected]', 'owner')", team).execute(pool).await.unwrap();
+ team.try_into().unwrap()
+}
+
+async fn claim(
+ pool: &PgPool,
+ team: TeamId,
+ kind: ConversationKind,
+ records: u32,
+) -> ClaimedConversation {
+ claim_with_archive_state(pool, team, kind, records, false).await
+}
+
+async fn claim_with_archive_state(
+ pool: &PgPool,
+ team: TeamId,
+ kind: ConversationKind,
+ records: u32,
+ archived: bool,
+) -> ClaimedConversation {
+ let repo = repo(pool);
+ let job = repo
+ .create(
+ team,
+ &user(),
+ &CreateImport {
+ idempotency_token: Uuid::now_v7().try_into().unwrap(),
+ source: SourceIdentity::ConfirmedUnknown,
+ include_message_history: records > 0,
+ conversations: vec![ConversationMetadata {
+ slack_channel_id: "C1".parse().unwrap(),
+ kind,
+ name: "historical".into(),
+ folder: "historical".parse().unwrap(),
+ member_ids: vec![],
+ creator_id: None,
+ created_at: Some("1.000001".parse().unwrap()),
+ archived,
+ message_count: None,
+ }],
+ },
+ &ImportLimits::default(),
+ )
+ .await
+ .unwrap()
+ .job_id;
+ let mut descriptors = vec![UploadDescriptor {
+ upload: UploadId::Users,
+ sha256: "a".repeat(64).parse().unwrap(),
+ byte_length: 10,
+ record_count: None,
+ }];
+ if records > 0 {
+ descriptors.push(UploadDescriptor {
+ upload: UploadId::ConversationPart {
+ slack_channel_id: "C1".parse().unwrap(),
+ part_index: 0,
+ },
+ sha256: "b".repeat(64).parse().unwrap(),
+ byte_length: 100,
+ record_count: Some(records),
+ });
+ }
+ let seal = ConversationSeal::from_descriptors(
+ "C1".parse().unwrap(),
+ &descriptors[1..],
+ &ImportLimits::default(),
+ )
+ .unwrap();
+ let uploads: Vec<_> = repo
+ .register(team, job, &RegisterUploads { descriptors })
+ .await
+ .unwrap()
+ .into_iter()
+ .map(|registered| VerifiedUpload {
+ registered,
+ identity: ObjectIdentity::Version("v1".parse().unwrap()),
+ })
+ .collect();
+ repo.complete(team, job, &uploads, Some(&seal))
+ .await
+ .unwrap();
+ let ClaimOutcome::Claimed(context) = repo
+ .claim(
+ &ImportEvent {
+ job_id: job,
+ slack_channel_id: "C1".parse().unwrap(),
+ generation: 1,
+ },
+ Uuid::now_v7().try_into().unwrap(),
+ )
+ .await
+ .unwrap()
+ else {
+ panic!("expected claim")
+ };
+ *context
+}
+
+async fn plan(pool: &PgPool, context: &ClaimedConversation) -> TargetPlan {
+ let ledger = CanonicalImportLedger::new(PgImportRepo::new(pool.clone()));
+ let reserved = ledger
+ .reserve(context.team_id, &user(), &context.metadata, None)
+ .await
+ .unwrap();
+ TargetPlan {
+ channel_id: reserved.channel_id,
+ metadata: context.metadata.clone(),
+ team_id: context.team_id,
+ requested_by: user(),
+ owner: user(),
+ members: HashSet::from([user(), other()]),
+ name: "original".into(),
+ created_at: chrono::DateTime::from_timestamp(1, 1000).unwrap(),
+ }
+}
+async fn target(pool: &PgPool, context: &ClaimedConversation) -> TargetPlan {
+ let plan = plan(pool, context).await;
+ let sink = sink(pool);
+ let (_, created) = sink.create(&context.lease, &plan).await.unwrap();
+ assert!(created);
+ sink.bind(&context.lease, &plan, &[ImportWarning::CreationTimeFromJob])
+ .await
+ .unwrap();
+ plan
+}
+fn message(context: &ClaimedConversation, channel: Uuid, seconds: u32) -> HistoricalMessage {
+ let ts: SlackTimestamp = format!("{seconds}.000001").parse().unwrap();
+ HistoricalMessage {
+ id: Uuid::now_v7(),
+ source: SourceMessageId {
+ team_id: context.team_id,
+ slack_channel_id: context.metadata.slack_channel_id.clone(),
+ ts,
+ },
+ channel_id: channel,
+ parent_id: None,
+ orphaned_thread_ts: None,
+ sender: HistoricalSender::User(user()),
+ imported_author: Some("Source name".into()),
+ content: "history".into(),
+ user_mentions: vec![],
+ body_references: vec![],
+ import_order: u64::from(seconds),
+ reactions: vec![
+ HistoricalReaction {
+ user_id: other(),
+ emoji: "x".into(),
+ created_at: ts
+ };
+ 2
+ ],
+ }
+}
+fn batch(
+ context: &ClaimedConversation,
+ channel: Uuid,
+ seconds: &[u32],
+ checkpoint: Checkpoint,
+) -> HistoricalBatch {
+ HistoricalBatch {
+ lease: context.lease.clone(),
+ messages: seconds
+ .iter()
+ .map(|s| message(context, channel, *s))
+ .collect(),
+ checkpoint,
+ skipped: 0,
+ }
+}
+fn end() -> Checkpoint {
+ Checkpoint {
+ part_index: 1,
+ record_index: 0,
+ }
+}
+async fn counts(pool: &PgPool) -> (i64, i64, i64, i64) {
+ let row = sqlx::query!(r#"SELECT (SELECT count(*) FROM comms_messages) AS "messages!", (SELECT count(*) FROM comms_reactions) AS "reactions!", (SELECT count(*) FROM slack_import_message_map) AS "mappings!", (SELECT count(*) FROM slack_import_outbox WHERE kind = 'search') AS "search!""#).fetch_one(pool).await.unwrap();
+ (row.messages, row.reactions, row.mappings, row.search)
+}
+
+#[sqlx::test(migrations = "../../crates/macro_db_client/migrations")]
+async fn failures_after_each_insert_roll_back_every_batch_effect(pool: PgPool) {
+ let team = team(&pool).await;
+ let context = claim(&pool, team, ConversationKind::PublicChannel, 1).await;
+ let plan = target(&pool, &context).await;
+ let before = counts(&pool).await;
+ pool.execute(include_str!("fail_inserts.sql"))
+ .await
+ .unwrap();
+ for table in [
+ "slack_import_message_map",
+ "slack_import_message_reference",
+ "slack_import_outbox",
+ "comms_messages",
+ "comms_reactions",
+ ] {
+ // Identifiers are this test's closed list, not archive input. DDL is dynamic.
+ pool.execute(format!("CREATE TRIGGER fail_insert AFTER INSERT ON {table} FOR EACH ROW EXECUTE FUNCTION fail_import_insert()").as_str()).await.unwrap();
+ let mut linked = batch(&context, plan.channel_id, &[2], end());
+ reference_reconciliation::linked(&mut linked.messages[0], "<#C1|source>");
+ assert!(sink(&pool).commit(linked).await.is_err(), "{table}");
+ assert_eq!(counts(&pool).await, before, "{table}");
+ assert_eq!(
+ sqlx::query_scalar!("SELECT count(*) FROM slack_import_message_reference")
+ .fetch_one(&pool)
+ .await
+ .unwrap(),
+ Some(0)
+ );
+ let progress = repo(&pool)
+ .progress(team, context.lease.event.job_id)
+ .await
+ .unwrap()
+ .unwrap();
+ assert_eq!(
+ progress.conversations[0].counters,
+ ImportCounters::default()
+ );
+ assert_eq!(progress.conversations[0].search, SearchState::NotNeeded);
+ pool.execute(format!("DROP TRIGGER fail_insert ON {table}").as_str())
+ .await
+ .unwrap();
+ }
+ let counters = sink(&pool)
+ .commit(batch(&context, plan.channel_id, &[2], end()))
+ .await
+ .unwrap();
+ assert_eq!(counters.imported, 1);
+ assert_eq!(counters.reactions, 1);
+ assert_eq!(
+ counts(&pool).await,
+ (before.0 + 1, before.1 + 1, before.2 + 1, before.3 + 1)
+ );
+}
+
+#[sqlx::test(migrations = "../../crates/macro_db_client/migrations")]
+async fn overlapping_jobs_remap_speculative_parents_without_duplicate_reactions(pool: PgPool) {
+ let team = team(&pool).await;
+ let first = claim(&pool, team, ConversationKind::PublicChannel, 2).await;
+ let plan = target(&pool, &first).await;
+ let second = claim(&pool, team, ConversationKind::PublicChannel, 2).await;
+ sink(&pool).bind(&second.lease, &plan, &[]).await.unwrap();
+ let mut a = batch(&first, plan.channel_id, &[2, 3], end());
+ a.messages[1].parent_id = Some(a.messages[0].id);
+ let mut b = batch(&second, plan.channel_id, &[2, 4], end());
+ b.messages[1].parent_id = Some(b.messages[0].id);
+ let sink = sink(&pool);
+ let (a, b) = tokio::join!(sink.commit(a), sink.commit(b));
+ let (a, b) = (a.unwrap(), b.unwrap());
+ assert_eq!(a.imported + b.imported, 3);
+ assert_eq!(a.duplicates + b.duplicates, 1);
+ assert_eq!(a.reactions + b.reactions, 3);
+ let root = sink
+ .lookup(&[message(&first, plan.channel_id, 2).source])
+ .await
+ .unwrap()[0]
+ .1;
+ let replies = sqlx::query_scalar!(
+ "SELECT thread_id FROM comms_messages WHERE channel_id = $1 AND id <> $2",
+ plan.channel_id,
+ root
+ )
+ .fetch_all(&pool)
+ .await
+ .unwrap();
+ assert_eq!(replies, vec![Some(root), Some(root)]);
+ // Replaying a committed checkpoint never increments any counter.
+ let replay = sink
+ .commit(batch(&first, plan.channel_id, &[2, 3], end()))
+ .await
+ .unwrap();
+ assert_eq!(replay, a);
+}
+
+#[sqlx::test(migrations = "../../crates/macro_db_client/migrations")]
+async fn stale_lease_and_cancellation_keep_committed_search_work(pool: PgPool) {
+ let team = team(&pool).await;
+ let context = claim(&pool, team, ConversationKind::PublicChannel, 2).await;
+ let plan = target(&pool, &context).await;
+ let first = sink(&pool)
+ .commit(batch(
+ &context,
+ plan.channel_id,
+ &[2],
+ Checkpoint {
+ part_index: 0,
+ record_index: 1,
+ },
+ ))
+ .await
+ .unwrap();
+ assert_eq!(first.imported, 1);
+ repo(&pool)
+ .cancel(team, context.lease.event.job_id)
+ .await
+ .unwrap();
+ sqlx::query!("UPDATE slack_import_conversation SET lease_expires_at = clock_timestamp() - interval '1 second' WHERE job_id = $1", Uuid::from(context.lease.event.job_id)).execute(&pool).await.unwrap();
+ let before = counts(&pool).await;
+ let error = sink(&pool)
+ .commit(batch(&context, plan.channel_id, &[3], end()))
+ .await
+ .unwrap_err();
+ assert_eq!(*error.current_context(), ImportError::LeaseLost);
+ repo(&pool).reconcile(10).await.unwrap();
+ assert_eq!(counts(&pool).await, before);
+ let progress = repo(&pool)
+ .progress(team, context.lease.event.job_id)
+ .await
+ .unwrap()
+ .unwrap();
+ assert_eq!(progress.status, JobStatus::Cancelling);
+ assert_eq!(progress.conversations[0].counters, first);
+ let search = repo(&pool).pending_search(10).await.unwrap();
+ assert_eq!(search.len(), 1);
+ assert_eq!(search[0].channel_ids, vec![plan.channel_id]);
+ repo(&pool)
+ .record_search(&search[0], SearchState::Completed)
+ .await
+ .unwrap();
+ assert_eq!(
+ repo(&pool)
+ .progress(team, context.lease.event.job_id)
+ .await
+ .unwrap()
+ .unwrap()
+ .status,
+ JobStatus::Cancelled
+ );
+}
+
+#[sqlx::test(migrations = "../../crates/macro_db_client/migrations")]
+async fn reclaimed_generation_and_admin_revocation_block_writes(pool: PgPool) {
+ let team = team(&pool).await;
+ let context = claim(&pool, team, ConversationKind::PublicChannel, 2).await;
+ let plan = target(&pool, &context).await;
+ sqlx::query!("UPDATE slack_import_conversation SET lease_expires_at = clock_timestamp() - interval '1 second' WHERE job_id = $1", Uuid::from(context.lease.event.job_id)).execute(&pool).await.unwrap();
+ let ClaimOutcome::Claimed(reclaimed) = repo(&pool)
+ .claim(&context.lease.event, Uuid::now_v7().try_into().unwrap())
+ .await
+ .unwrap()
+ else {
+ panic!("reclaim")
+ };
+ let sink = sink(&pool);
+ let next = Checkpoint {
+ part_index: 0,
+ record_index: 1,
+ };
+ let (stale, fresh) = tokio::join!(
+ sink.commit(batch(&context, plan.channel_id, &[2], next)),
+ sink.commit(batch(&reclaimed, plan.channel_id, &[2], next))
+ );
+ assert_eq!(
+ *stale.unwrap_err().current_context(),
+ ImportError::LeaseLost
+ );
+ assert_eq!(fresh.unwrap().imported, 1);
+ let before = counts(&pool).await;
+ sqlx::query!(
+ "UPDATE team_user SET team_role = 'member' WHERE team_id = $1",
+ Uuid::from(team)
+ )
+ .execute(&pool)
+ .await
+ .unwrap();
+ assert_eq!(
+ *sink
+ .commit(batch(&reclaimed, plan.channel_id, &[3], end()))
+ .await
+ .unwrap_err()
+ .current_context(),
+ ImportError::Unavailable
+ );
+ assert!(sink.bind(&reclaimed.lease, &plan, &[]).await.is_err());
+ assert_eq!(counts(&pool).await, before);
+}
+
+#[sqlx::test(migrations = "../../crates/macro_db_client/migrations")]
+async fn new_dm_has_exact_pair_and_no_importing_admin(pool: PgPool) {
+ let team = team(&pool).await;
+ let context = claim(&pool, team, ConversationKind::DirectMessage, 0).await;
+ let mut plan = plan(&pool, &context).await;
+ let stranger = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();
+ plan.owner = other();
+ plan.members = HashSet::from([other(), stranger]);
+ let sink = sink(&pool);
+ let (facts, created) = sink.create(&context.lease, &plan).await.unwrap();
+ assert!(created);
+ assert_eq!(facts.dm_members, plan.members);
+ assert!(!facts.dm_members.contains(&user()));
+ sink.bind(&context.lease, &plan, &[]).await.unwrap();
+ let mut invalid = plan.clone();
+ invalid.members.insert(user());
+ assert!(sink.bind(&context.lease, &invalid, &[]).await.is_err());
+ assert_eq!(
+ sink.inspect(plan.channel_id)
+ .await
+ .unwrap()
+ .unwrap()
+ .dm_members,
+ plan.members
+ );
+}
+
+#[sqlx::test(migrations = "../../crates/macro_db_client/migrations")]
+async fn stable_reservation_recovers_creation_and_preserves_existing_state(pool: PgPool) {
+ let team = team(&pool).await;
+ let context = claim(&pool, team, ConversationKind::PrivateChannel, 0).await;
+ let mut plan = plan(&pool, &context).await;
+ let other = other();
+ let sink = sink(&pool);
+ let (first, created) = sink.create(&context.lease, &plan).await.unwrap();
+ assert!(created);
+ // Crash before ledger completion/bind: reserved ID and ready provenance survive.
+ let again = PgImportRepo::new(pool.clone())
+ .reserve_target(
+ &user(),
+ &target_key(team, &context.metadata.slack_channel_id).unwrap(),
+ target_kind(context.metadata.kind),
+ None,
+ )
+ .await
+ .unwrap();
+ assert_eq!(again.channel_id, first.id);
+ assert!(again.ready);
+ plan.name = "must not overwrite".into();
+ plan.created_at = chrono::Utc::now();
+ let (_, created) = sink.create(&context.lease, &plan).await.unwrap();
+ assert!(!created);
+ sqlx::query!("UPDATE comms_channel_participants SET left_at = now(), role = 'admin' WHERE channel_id = $1 AND user_id = $2", plan.channel_id, other.as_ref()).execute(&pool).await.unwrap();
+ sink.bind(&context.lease, &plan, &[ImportWarning::CreationTimeFromJob])
+ .await
+ .unwrap();
+ sink.bind(&context.lease, &plan, &[ImportWarning::CreationTimeFromJob])
+ .await
+ .unwrap();
+ let channel = sqlx::query!(
+ "SELECT name, created_at, auto_join_team, team_id FROM comms_channels WHERE id = $1",
+ plan.channel_id
+ )
+ .fetch_one(&pool)
+ .await
+ .unwrap();
+ assert_eq!(channel.name.as_deref(), Some("original"));
+ assert_eq!(channel.created_at.timestamp(), 1);
+ assert!(!channel.auto_join_team);
+ assert!(channel.team_id.is_none());
+ let member = sqlx::query!(r#"SELECT role::text AS "role!", left_at FROM comms_channel_participants WHERE channel_id = $1 AND user_id = $2"#, plan.channel_id, other.as_ref()).fetch_one(&pool).await.unwrap();
+ assert_eq!(member.role, "admin");
+ assert!(member.left_at.is_some());
+ let progress = repo(&pool)
+ .progress(team, context.lease.event.job_id)
+ .await
+ .unwrap()
+ .unwrap();
+ assert_eq!(progress.conversations[0].warnings.len(), 1);
+}
+
+#[sqlx::test(migrations = "../../crates/macro_db_client/migrations")]
+async fn private_reuse_requires_management_or_compatible_prior_provenance(pool: PgPool) {
+ let team = team(&pool).await;
+ let context = claim(&pool, team, ConversationKind::PrivateChannel, 1).await;
+ let channel = Uuid::now_v7();
+ let mut tx = pool.begin().await.unwrap();
+ PgChannelsRepo::ensure_reserved_channel_in(
+ &mut tx,
+ &HistoricalChannel {
+ id: channel,
+ name: "private".into(),
+ kind: HistoricalChannelKind::Private,
+ owner: other(),
+ participants: HashSet::from([user()]),
+ created_at: chrono::Utc::now(),
+ },
+ false,
+ )
+ .await
+ .unwrap();
+ tx.commit().await.unwrap();
+ let auth = authorizer(&pool);
+ let requester = user();
+ assert!(
+ auth.require_target(team, &user(), &context.metadata, channel)
+ .await
+ .is_err()
+ );
+ sqlx::query!("UPDATE comms_channel_participants SET role = 'admin' WHERE channel_id = $1 AND user_id = $2", channel, requester.as_ref()).execute(&pool).await.unwrap();
+ auth.require_target(team, &user(), &context.metadata, channel)
+ .await
+ .unwrap();
+ let ledger = CanonicalImportLedger::new(PgImportRepo::new(pool.clone()));
+ let reservation = ledger
+ .reserve(team, &user(), &context.metadata, Some(channel))
+ .await
+ .unwrap();
+ ledger
+ .complete(&reservation, context.metadata.kind)
+ .await
+ .unwrap();
+ sqlx::query!("UPDATE comms_channel_participants SET left_at = now() WHERE channel_id = $1 AND user_id = $2", channel, requester.as_ref()).execute(&pool).await.unwrap();
+ auth.require_target(team, &user(), &context.metadata, channel)
+ .await
+ .unwrap();
+ // Provenance is not permission to keep writing after visibility/type changes.
+ sqlx::query!(
+ "UPDATE comms_channels SET channel_type = 'public' WHERE id = $1",
+ channel
+ )
+ .execute(&pool)
+ .await
+ .unwrap();
+ assert!(
+ auth.require_target(team, &user(), &context.metadata, channel)
+ .await
+ .is_err()
+ );
+}
+
+#[sqlx::test(migrations = "../../crates/macro_db_client/migrations")]
+async fn unrelated_dm_pair_is_not_granted_provenance_and_pair_races_fail_closed(pool: PgPool) {
+ use channels::{
+ domain::{dm::DmPair, historical::DmCreation},
+ outbound::pg_channels_repo::historical::ensure_dm_in,
+ };
+ let team = team(&pool).await;
+ let context = claim(&pool, team, ConversationKind::DirectMessage, 0).await;
+ let mut plan = plan(&pool, &context).await;
+ let stranger = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();
+ plan.owner = other();
+ plan.members = HashSet::from([other(), stranger.clone()]);
+ let pair = DmPair::new(other(), stranger.clone()).unwrap();
+ let mut tx = pool.begin().await.unwrap();
+ let winner = ensure_dm_in(
+ &mut tx,
+ pair,
+ DmCreation::Historical {
+ id: Uuid::now_v7(),
+ created_at: plan.created_at,
+ },
+ )
+ .await
+ .unwrap();
+ tx.commit().await.unwrap();
+ let auth = authorizer(&pool);
+ assert!(
+ auth.require_target(team, &user(), &context.metadata, winner.id)
+ .await
+ .is_err()
+ );
+ // Discovery raced after the canonical candidate was reserved: do not substitute
+ // the winner or create a duplicate DM, and never add the admin as a third member.
+ assert!(sink(&pool).create(&context.lease, &plan).await.is_err());
+ assert!(
+ sink(&pool)
+ .inspect(plan.channel_id)
+ .await
+ .unwrap()
+ .is_none()
+ );
+ let winner = sink(&pool).inspect(winner.id).await.unwrap().unwrap();
+ assert_eq!(winner.dm_members, plan.members);
+ assert!(!winner.dm_members.contains(&user()));
+}
diff --git a/services/slack_import_worker/src/composition/test/reference_reconciliation.rs b/services/slack_import_worker/src/composition/test/reference_reconciliation.rs
new file mode 100644
index 0000000000..89d386dd41
--- /dev/null
+++ b/services/slack_import_worker/src/composition/test/reference_reconciliation.rs
@@ -0,0 +1,417 @@
+use super::*;
+use crate::composition::reference_reconciliation::WorkerReferenceReconciler;
+use slack_integration::domain::{
+ ports::ReferenceReconciler,
+ slack::{mrkdwn::MrkdwnConverter, users::UserDirectory},
+};
+
+pub(super) fn linked(message: &mut HistoricalMessage, source: &str) {
+ let text = MrkdwnConverter {
+ users: &UserDirectory::default(),
+ channels: &Default::default(),
+ }
+ .convert_with_references(source, false)
+ .unwrap();
+ message.content = text.body;
+ message.user_mentions = text.user_mentions;
+ message.body_references = text.references;
+}
+fn reconciler(pool: &PgPool) -> WorkerReferenceReconciler {
+ WorkerReferenceReconciler::new(pool.clone(), ImportLimits::default())
+}
+
+#[sqlx::test(migrations = "../../crates/macro_db_client/migrations")]
+async fn patches_intents_and_search_rollback_at_every_boundary(pool: PgPool) {
+ let team = team(&pool).await;
+ let context = claim(&pool, team, ConversationKind::PublicChannel, 1).await;
+ let plan = target(&pool, &context).await;
+ let mut batch = batch(&context, plan.channel_id, &[2], end());
+ linked(&mut batch.messages[0], "<#C1|self>");
+ let fallback = batch.messages[0].content.clone();
+ let candidate = batch.messages[0].id;
+ sink(&pool).commit(batch.clone()).await.unwrap();
+ // Redelivery cannot create a second intent or count history twice.
+ sink(&pool).commit(batch).await.unwrap();
+ let row = sqlx::query!("SELECT message_id FROM slack_import_message_reference")
+ .fetch_one(&pool)
+ .await
+ .unwrap();
+ assert_ne!(row.message_id, candidate);
+ let job = context.lease.event.job_id;
+ let repo = repo(&pool);
+ repo.finalize(team, job).await.unwrap();
+ repo.settle(&context.lease, ConversationStatus::Completed, None)
+ .await
+ .unwrap();
+ assert_eq!(
+ repo.progress(team, job).await.unwrap().unwrap().status,
+ JobStatus::Processing
+ );
+ pool.execute(include_str!("../fail_inserts.sql"))
+ .await
+ .unwrap();
+ let before = counts(&pool).await;
+ for (table, operation) in [
+ ("comms_messages", "UPDATE"),
+ ("slack_import_message_reference", "UPDATE"),
+ ("slack_import_outbox", "INSERT"),
+ ] {
+ // Closed test-only identifiers for fault injection DDL.
+ pool.execute(format!("CREATE TRIGGER fail_reference AFTER {operation} ON {table} FOR EACH ROW EXECUTE FUNCTION fail_import_insert()").as_str()).await.unwrap();
+ assert!(reconciler(&pool).reconcile_references(50).await.is_err());
+ assert_eq!(counts(&pool).await, before);
+ let body = sqlx::query_scalar!(
+ "SELECT content FROM comms_messages WHERE id = $1",
+ row.message_id
+ )
+ .fetch_one(&pool)
+ .await
+ .unwrap();
+ assert_eq!(body, fallback);
+ assert!(repo.pending_search(50).await.unwrap().is_empty());
+ assert!(
+ sqlx::query_scalar!(
+ "SELECT completed_at IS NULL AS \"pending!\" FROM slack_import_message_reference"
+ )
+ .fetch_one(&pool)
+ .await
+ .unwrap()
+ );
+ pool.execute(format!("DROP TRIGGER fail_reference ON {table}").as_str())
+ .await
+ .unwrap();
+ }
+ // A new process object resumes only durable evidence, without S3 or archive data.
+ reconciler(&pool).reconcile_references(1).await.unwrap();
+ let body = sqlx::query!(
+ "SELECT content, created_at, updated_at, edited_at FROM comms_messages WHERE id = $1",
+ row.message_id
+ )
+ .fetch_one(&pool)
+ .await
+ .unwrap();
+ assert!(body.content.contains(&plan.channel_id.to_string()));
+ assert_eq!(body.created_at, body.updated_at);
+ assert!(body.edited_at.is_none());
+ assert_eq!(counts(&pool).await.0, before.0);
+ let progress = repo.progress(team, job).await.unwrap().unwrap();
+ assert_eq!(progress.conversations[0].counters.imported, 1);
+ assert_eq!(progress.conversations[0].counters.duplicates, 0);
+ assert_eq!(progress.status, JobStatus::Processing);
+ let search = repo.pending_search(50).await.unwrap().pop().unwrap();
+ assert_eq!(search.generation, 2);
+ repo.record_search(&search, SearchState::Completed)
+ .await
+ .unwrap();
+ reconciler(&pool).reconcile_references(50).await.unwrap();
+ assert_eq!(
+ repo.progress(team, job).await.unwrap().unwrap().status,
+ JobStatus::Completed
+ );
+ // Cleanup cannot delete historical messages or dedupe mappings.
+ sqlx::query!(
+ "DELETE FROM slack_import_job WHERE id = $1",
+ Uuid::from(job)
+ )
+ .execute(&pool)
+ .await
+ .unwrap();
+ assert_eq!(counts(&pool).await.0, 1);
+ assert_eq!(counts(&pool).await.2, 1);
+}
+
+#[sqlx::test(migrations = "../../crates/macro_db_client/migrations")]
+async fn cancelled_partial_import_rechecks_access_and_never_reopens_fallbacks(pool: PgPool) {
+ let team = team(&pool).await;
+ let context = claim(&pool, team, ConversationKind::PrivateChannel, 2).await;
+ let plan = target(&pool, &context).await;
+ let mut first = batch(
+ &context,
+ plan.channel_id,
+ &[1],
+ Checkpoint {
+ part_index: 0,
+ record_index: 1,
+ },
+ );
+ linked(&mut first.messages[0], "<#C1|source> <#C404|missing>");
+ let fallback = first.messages[0].content.clone();
+ sink(&pool).commit(first).await.unwrap();
+ let repo = repo(&pool);
+ let job = context.lease.event.job_id;
+ repo.cancel(team, job).await.unwrap();
+ sqlx::query!("UPDATE slack_import_conversation SET lease_expires_at = now() - interval '1 minute' WHERE job_id = $1", Uuid::from(job)).execute(&pool).await.unwrap();
+ repo.reconcile(50).await.unwrap();
+ sqlx::query!(
+ "UPDATE comms_channel_participants SET left_at = now() WHERE channel_id = $1",
+ plan.channel_id
+ )
+ .execute(&pool)
+ .await
+ .unwrap();
+ reconciler(&pool).reconcile_references(50).await.unwrap();
+ assert_eq!(
+ sqlx::query_scalar!("SELECT content FROM comms_messages")
+ .fetch_one(&pool)
+ .await
+ .unwrap(),
+ fallback
+ );
+ assert!(matches!(
+ repo.claim(&context.lease.event, Uuid::now_v7().try_into().unwrap())
+ .await
+ .unwrap(),
+ ClaimOutcome::Obsolete
+ ));
+ let search = repo.pending_search(50).await.unwrap().pop().unwrap();
+ assert_eq!(search.generation, 1, "fallback did not dirty search again");
+ repo.record_search(&search, SearchState::Completed)
+ .await
+ .unwrap();
+ assert_eq!(
+ repo.progress(team, job).await.unwrap().unwrap().status,
+ JobStatus::Cancelled
+ );
+ sqlx::query!(
+ "UPDATE comms_channel_participants SET left_at = NULL WHERE channel_id = $1",
+ plan.channel_id
+ )
+ .execute(&pool)
+ .await
+ .unwrap();
+ reconciler(&pool).reconcile_references(50).await.unwrap();
+ assert_eq!(
+ sqlx::query_scalar!("SELECT content FROM comms_messages")
+ .fetch_one(&pool)
+ .await
+ .unwrap(),
+ fallback
+ );
+}
+
+#[sqlx::test(migrations = "../../crates/macro_db_client/migrations")]
+async fn forward_conversation_order_and_split_parts(pool: PgPool) {
+ linked_conversations(pool, ["C1", "C2"]).await;
+}
+
+#[sqlx::test(migrations = "../../crates/macro_db_client/migrations")]
+async fn reverse_conversation_order_and_split_parts(pool: PgPool) {
+ linked_conversations(pool, ["C2", "C1"]).await;
+}
+
+async fn linked_conversations(pool: PgPool, order: [&str; 2]) {
+ let team = team(&pool).await;
+ let repo = repo(&pool);
+ let limits = ImportLimits::default();
+ let conversations = ["C1", "C2"]
+ .into_iter()
+ .map(|id| ConversationMetadata {
+ slack_channel_id: id.parse().unwrap(),
+ kind: ConversationKind::PublicChannel,
+ name: id.into(),
+ folder: id.parse().unwrap(),
+ member_ids: vec![],
+ creator_id: None,
+ created_at: Some("1.000001".parse().unwrap()),
+ archived: false,
+ message_count: None,
+ })
+ .collect();
+ let job = repo
+ .create(
+ team,
+ &user(),
+ &CreateImport {
+ idempotency_token: Uuid::now_v7().try_into().unwrap(),
+ source: SourceIdentity::ConfirmedUnknown,
+ include_message_history: true,
+ conversations,
+ },
+ &limits,
+ )
+ .await
+ .unwrap()
+ .job_id;
+ let mut descriptors = vec![UploadDescriptor {
+ upload: UploadId::Users,
+ sha256: "a".repeat(64).parse().unwrap(),
+ byte_length: 10,
+ record_count: None,
+ }];
+ for id in ["C1", "C2"] {
+ for part_index in 0..2 {
+ descriptors.push(UploadDescriptor {
+ upload: UploadId::ConversationPart {
+ slack_channel_id: id.parse().unwrap(),
+ part_index,
+ },
+ sha256: "b".repeat(64).parse().unwrap(),
+ byte_length: 100,
+ record_count: Some(1),
+ });
+ }
+ }
+ let uploads: Vec<_> = repo
+ .register(
+ team,
+ job,
+ &RegisterUploads {
+ descriptors: descriptors.clone(),
+ },
+ )
+ .await
+ .unwrap()
+ .into_iter()
+ .map(|registered| VerifiedUpload {
+ registered,
+ identity: ObjectIdentity::Version("v1".parse().unwrap()),
+ })
+ .collect();
+ repo.complete(team, job, &uploads, None).await.unwrap();
+ for (index, id) in ["C1", "C2"].into_iter().enumerate() {
+ let seal = ConversationSeal::from_descriptors(
+ id.parse().unwrap(),
+ &descriptors[1 + index * 2..3 + index * 2],
+ &limits,
+ )
+ .unwrap();
+ repo.complete(team, job, &[], Some(&seal)).await.unwrap();
+ }
+ repo.finalize(team, job).await.unwrap();
+ let mut channels = std::collections::HashMap::new();
+ for id in order {
+ let event = ImportEvent {
+ job_id: job,
+ slack_channel_id: id.parse().unwrap(),
+ generation: 1,
+ };
+ let ClaimOutcome::Claimed(context) = repo
+ .claim(&event, Uuid::now_v7().try_into().unwrap())
+ .await
+ .unwrap()
+ else {
+ panic!("claim");
+ };
+ let plan = target(&pool, &context).await;
+ channels.insert(id, plan.channel_id);
+ for part in 0..2 {
+ let mut batch = batch(
+ &context,
+ plan.channel_id,
+ &[part + 1],
+ Checkpoint {
+ part_index: part + 1,
+ record_index: 0,
+ },
+ );
+ linked(
+ &mut batch.messages[0],
+ "<#C1|one> <#C2|two> <#C404|missing> https://example.slack.com/archives/C2/p1000001",
+ );
+ sink(&pool).commit(batch).await.unwrap();
+ reconciler(&pool).reconcile_references(50).await.unwrap();
+ assert_eq!(sqlx::query_scalar!("SELECT count(*) FROM slack_import_message_reference WHERE completed_at IS NOT NULL").fetch_one(&pool).await.unwrap(), Some(0));
+ }
+ repo.settle(&context.lease, ConversationStatus::Completed, None)
+ .await
+ .unwrap();
+ }
+ reconciler(&pool).reconcile_references(50).await.unwrap();
+ let rows = sqlx::query!("SELECT content FROM comms_messages")
+ .fetch_all(&pool)
+ .await
+ .unwrap();
+ assert_eq!(rows.len(), 4);
+ for row in rows {
+ assert!(row.content.contains(&channels["C1"].to_string()));
+ assert!(row.content.contains(&channels["C2"].to_string()));
+ assert!(row.content.contains("#missing"));
+ // Production has no trusted workspace-domain evidence; keep this external.
+ assert!(row.content.contains("<m-link>"));
+ assert!(!row.content.contains("channel_message_id"));
+ }
+ let progress = repo.progress(team, job).await.unwrap().unwrap();
+ assert_eq!(progress.status, JobStatus::Processing);
+ assert!(
+ progress
+ .conversations
+ .iter()
+ .all(|c| c.counters.imported == 2 && c.counters.processed == 2)
+ );
+ for search in repo.pending_search(50).await.unwrap() {
+ repo.record_search(&search, SearchState::Completed)
+ .await
+ .unwrap();
+ }
+ assert_eq!(
+ repo.progress(team, job).await.unwrap().unwrap().status,
+ JobStatus::Completed
+ );
+}
+
+#[sqlx::test(migrations = "../../crates/macro_db_client/migrations")]
+async fn later_duplicate_job_cannot_patch_first_committed_body(pool: PgPool) {
+ let team = team(&pool).await;
+ let first = claim(&pool, team, ConversationKind::PublicChannel, 1).await;
+ let plan = target(&pool, &first).await;
+ let second = claim(&pool, team, ConversationKind::PublicChannel, 1).await;
+ sink(&pool)
+ .bind(
+ &second.lease,
+ &TargetPlan {
+ metadata: second.metadata.clone(),
+ ..plan.clone()
+ },
+ &[],
+ )
+ .await
+ .unwrap();
+ let mut a = batch(&first, plan.channel_id, &[1], end());
+ let mut b = batch(&second, plan.channel_id, &[1], end());
+ linked(&mut a.messages[0], "first <#C1|source>");
+ linked(&mut b.messages[0], "second <#C1|source>");
+ let sink = sink(&pool);
+ let (a, b) = tokio::join!(sink.commit(a), sink.commit(b));
+ assert_eq!(a.unwrap().imported + b.unwrap().imported, 1);
+ assert_eq!(
+ sqlx::query_scalar!("SELECT count(*) FROM slack_import_message_reference")
+ .fetch_one(&pool)
+ .await
+ .unwrap(),
+ Some(1)
+ );
+ let intent = sqlx::query!("SELECT job_id, message_id FROM slack_import_message_reference")
+ .fetch_one(&pool)
+ .await
+ .unwrap();
+ let map = sqlx::query_scalar!("SELECT message_id FROM slack_import_message_map")
+ .fetch_one(&pool)
+ .await
+ .unwrap();
+ assert_eq!(intent.message_id, map);
+ let repo = repo(&pool);
+ for context in [&first, &second] {
+ repo.finalize(team, context.lease.event.job_id)
+ .await
+ .unwrap();
+ repo.settle(&context.lease, ConversationStatus::Completed, None)
+ .await
+ .unwrap();
+ }
+ // A live edit after the winning commit is never overwritten by either job.
+ sqlx::query!(
+ "UPDATE comms_messages SET content = 'live', edited_at = now() WHERE id = $1",
+ map
+ )
+ .execute(&pool)
+ .await
+ .unwrap();
+ reconciler(&pool).reconcile_references(50).await.unwrap();
+ assert_eq!(
+ sqlx::query_scalar!("SELECT content FROM comms_messages")
+ .fetch_one(&pool)
+ .await
+ .unwrap(),
+ "live"
+ );
+}
diff --git a/services/slack_import_worker/src/config.rs b/services/slack_import_worker/src/config.rs
new file mode 100644
index 0000000000..f3aad08361
--- /dev/null
+++ b/services/slack_import_worker/src/config.rs
@@ -0,0 +1,32 @@
+//! Environment-backed startup configuration. Queue/URL overrides use shared types.
+
+use database_env_vars::DatabaseUrl;
+use slack_integration::{domain::models::ImportError, inbound::worker::WorkerConfig};
+
+#[cfg(test)]
+mod test;
+
+/// Settings supplied by the deployment, never by an import archive.
+#[derive(macro_config::MacroConfig)]
+#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
+pub struct Config {
+ /// MacroDB connection URL.
+ pub database_url: DatabaseUrl,
+ /// Immutable temporary archive object bucket.
+ pub upload_staging_bucket: String,
+ /// Credential for scoped search backfill requests.
+ pub internal_api_key: String,
+ /// Pause new claims and outbox sends, not reconciliation or queues.
+ #[macro_config_default(false)]
+ pub slack_import_enabled: bool,
+ /// Initially one; fail startup above two rather than silently widening work.
+ #[macro_config_default(1)]
+ pub slack_import_concurrency: u8,
+}
+
+impl Config {
+ /// Fail fast before polling queues.
+ pub fn worker(&self) -> Result<WorkerConfig, ImportError> {
+ WorkerConfig::new(self.slack_import_enabled, self.slack_import_concurrency, 5)
+ }
+}
diff --git a/services/slack_import_worker/src/config/test.rs b/services/slack_import_worker/src/config/test.rs
new file mode 100644
index 0000000000..2c96d709a3
--- /dev/null
+++ b/services/slack_import_worker/src/config/test.rs
@@ -0,0 +1,29 @@
+use super::*;
+
+#[test]
+fn rollout_defaults_are_disabled_and_single_slot() {
+ let config: Config = serde_json::from_value(serde_json::json!({
+ "DATABASE_URL": "postgres://localhost/test",
+ "UPLOAD_STAGING_BUCKET": "test-staging",
+ "INTERNAL_API_KEY": "test-only"
+ }))
+ .unwrap();
+ assert!(!config.slack_import_enabled);
+ assert_eq!(config.slack_import_concurrency, 1);
+ assert!(config.worker().is_ok());
+}
+
+#[test]
+fn rejects_unbounded_or_zero_concurrency_at_startup() {
+ for concurrency in [0, 3, 255] {
+ let config: Config = serde_json::from_value(serde_json::json!({
+ "DATABASE_URL": "postgres://localhost/test",
+ "UPLOAD_STAGING_BUCKET": "test-staging",
+ "INTERNAL_API_KEY": "test-only",
+ "SLACK_IMPORT_ENABLED": true,
+ "SLACK_IMPORT_CONCURRENCY": concurrency
+ }))
+ .unwrap();
+ assert!(config.worker().is_err());
+ }
+}
diff --git a/services/slack_import_worker/src/lib.rs b/services/slack_import_worker/src/lib.rs
new file mode 100644
index 0000000000..e605b0f1d6
--- /dev/null
+++ b/services/slack_import_worker/src/lib.rs
@@ -0,0 +1,4 @@
+#![deny(missing_docs)]
+//! Composition for side-effect-free, fenced Slack archive persistence.
+
+pub mod composition;
diff --git a/services/slack_import_worker/src/main.rs b/services/slack_import_worker/src/main.rs
new file mode 100644
index 0000000000..6511e2ba6d
--- /dev/null
+++ b/services/slack_import_worker/src/main.rs
@@ -0,0 +1,119 @@
+#![deny(missing_docs)]
+// Independent heartbeat/select futures wrap the composed importer and SDK streams.
+#![recursion_limit = "256"]
+//! Independent Slack archive queue worker and durable recovery composition root.
+
+mod config;
+
+use chrono::{DateTime, Utc};
+use entity_access::{domain::service::EntityAccessServiceImpl, outbound::PgAccessRepository};
+use import::outbound::pg_import_repo::PgImportRepo;
+use macro_entrypoint::{MacroEntrypoint, shutdown_signal};
+use macro_queues::{SlackImportDlq, SlackImportQueue};
+use macro_service_urls::SearchProcessingServiceUrl;
+use slack_import_worker::composition::{
+ authorizer::WorkerAuthorizer, channel_sink::ChannelImportSink,
+ reference_reconciliation::WorkerReferenceReconciler,
+};
+use slack_integration::{
+ domain::{
+ importer::{ConversationImporter, ImporterConfig},
+ maintenance::ImportMaintenance,
+ models::{ImportError, ImportLimits},
+ ports::Clock,
+ },
+ inbound::worker,
+ outbound::{
+ import_ledger::CanonicalImportLedger, pg_slack_import_repo::PgSlackImportRepo,
+ s3_storage::S3ImportStorage, search_backfill::HttpSearchBackfill,
+ sqs_queue::SqsImportQueue,
+ },
+};
+use sqlx::postgres::PgPoolOptions;
+use tokio_util::sync::CancellationToken;
+use uuid::Uuid;
+
+struct SystemClock;
+
+impl Clock for SystemClock {
+ fn now(&self) -> DateTime<Utc> {
+ Utc::now()
+ }
+}
+
+#[tokio::main]
+async fn main() -> Result<(), rootcause::Report> {
+ let entrypoint = MacroEntrypoint::default().init();
+ let result = run().await;
+ entrypoint.shutdown();
+ result
+}
+
+async fn run() -> Result<(), rootcause::Report> {
+ let config = macro_config::ConfigLoader::load::<config::Config>()?;
+ let worker_config = config.worker()?;
+ let limits = ImportLimits::default();
+ let pool = PgPoolOptions::new()
+ // Leave capacity for lease heartbeats while imports hold transactions.
+ .max_connections(12)
+ .acquire_timeout(std::time::Duration::from_secs(20))
+ .connect(config.database_url.as_ref())
+ .await
+ .map_err(|_| ImportError::Retryable)?;
+ let repo = PgSlackImportRepo::new(pool.clone(), limits);
+ let authorizer = WorkerAuthorizer::new(
+ pool.clone(),
+ EntityAccessServiceImpl::new(PgAccessRepository::new(pool.clone())),
+ );
+ let sink = ChannelImportSink::new(pool.clone(), authorizer.clone(), limits)?;
+ let storage = S3ImportStorage::new(
+ macro_aws_config::s3_client().await,
+ config.upload_staging_bucket,
+ limits,
+ )?;
+ let queue = SqsImportQueue::new(
+ aws_sdk_sqs::Client::new(&macro_aws_config::get_macro_aws_config().await),
+ &SlackImportQueue::new(),
+ &SlackImportDlq::new(),
+ )
+ .await
+ .map_err(|error| *error.current_context())?;
+ let search = HttpSearchBackfill::new(
+ SearchProcessingServiceUrl::new()?.as_ref(),
+ &config.internal_api_key,
+ )?;
+ let importer = ConversationImporter::new(
+ repo.clone(),
+ storage,
+ CanonicalImportLedger::new(PgImportRepo::new(pool.clone())),
+ sink.clone(),
+ sink,
+ authorizer,
+ ImporterConfig { limits },
+ )?;
+ let maintenance = ImportMaintenance::new(
+ repo,
+ queue.clone(),
+ search,
+ SystemClock,
+ WorkerReferenceReconciler::new(pool, limits),
+ );
+ let stop = CancellationToken::new();
+ let signal = async {
+ shutdown_signal().await;
+ stop.cancel();
+ };
+ let owner = Uuid::now_v7().try_into()?;
+ tokio::join!(
+ worker::run(
+ &importer,
+ &maintenance,
+ &queue,
+ owner,
+ worker_config,
+ stop.clone()
+ ),
+ signal
+ );
+ Ok(())
+}
diff --git a/services/slack_import_worker/tests/archive_import.rs b/services/slack_import_worker/tests/archive_import.rs
new file mode 100644
index 0000000000..dabacb200b
--- /dev/null
+++ b/services/slack_import_worker/tests/archive_import.rs
@@ -0,0 +1,853 @@
+//! Opt-in real PostgreSQL/S3/SQS contract test. No hosted endpoints or customer data.
+//! SQLx creates a database per test; Docker creates a private LocalStack container.
+//! Search acceptance is deliberately controlled, not an OpenSearch end-to-end test.
+
+use std::{
+ process::Command,
+ sync::{
+ Arc,
+ atomic::{AtomicBool, Ordering},
+ },
+ time::Duration,
+};
+
+use aws_sdk_s3::config::{BehaviorVersion, Credentials, Region};
+use chrono::{DateTime, Utc};
+use entity_access::{
+ domain::{models::*, service::EntityAccessServiceImpl},
+ outbound::PgAccessRepository,
+};
+use import::outbound::pg_import_repo::PgImportRepo;
+use macro_queues::{SlackImportDlq, SlackImportQueue};
+use macro_user_id::user_id::MacroUserIdStr;
+use sha2::{Digest, Sha256};
+use slack_import_worker::composition::{
+ authorizer::WorkerAuthorizer, channel_sink::ChannelImportSink,
+ reference_reconciliation::WorkerReferenceReconciler,
+};
+use slack_integration::{
+ domain::{
+ importer::{ConversationImporter, ImporterConfig},
+ maintenance::{ImportMaintenance, Maintenance},
+ models::*,
+ ports::*,
+ service::SlackImportService,
+ },
+ inbound::worker::{self, WorkerConfig},
+ outbound::{
+ import_ledger::CanonicalImportLedger, pg_slack_import_repo::PgSlackImportRepo,
+ s3_storage::S3ImportStorage, sqs_queue::SqsImportQueue,
+ },
+};
+use sqlx::PgPool;
+use tokio::time::{sleep, timeout};
+use tokio_util::sync::CancellationToken;
+use uuid::Uuid;
+
+#[path = "archive_import/process_recovery.rs"]
+mod process_recovery;
+
+type Access = EntityAccessServiceImpl<PgAccessRepository>;
+
+/// Only this test's container is removed, including on assertion failure. The image
+/// must already exist: an ignored test must not silently pull arbitrary images.
+struct LocalStack(String);
+
+impl LocalStack {
+ async fn start() -> (Self, String) {
+ let container = Self(docker(&[
+ "run",
+ "--detach",
+ "--rm",
+ "--pull=never",
+ "--publish",
+ "127.0.0.1::4566",
+ "--env",
+ "SERVICES=s3,sqs",
+ "--env",
+ "SQS_ENDPOINT_STRATEGY=path",
+ "localstack/localstack:4",
+ ]));
+ let address = docker(&["port", &container.0, "4566/tcp"]);
+ let endpoint = format!("http://{address}");
+ let client = reqwest::Client::new();
+ timeout(Duration::from_secs(60), async {
+ loop {
+ if let Ok(response) = client
+ .get(format!("{endpoint}/_localstack/health"))
+ .send()
+ .await
+ && response.status().is_success()
+ {
+ break;
+ }
+ sleep(Duration::from_millis(250)).await;
+ }
+ })
+ .await
+ .expect("isolated LocalStack did not start");
+ (container, endpoint)
+ }
+}
+
+impl Drop for LocalStack {
+ fn drop(&mut self) {
+ let _ = Command::new("docker")
+ .args(["rm", "--force", &self.0])
+ .output();
+ }
+}
+
+fn docker(args: &[&str]) -> String {
+ let output = Command::new("docker")
+ .args(args)
+ .output()
+ .expect("Docker is required");
+ assert!(
+ output.status.success(),
+ "docker failed: {}",
+ String::from_utf8_lossy(&output.stderr)
+ );
+ String::from_utf8(output.stdout).unwrap().trim().to_owned()
+}
+
+async fn resources(
+ endpoint: &str,
+) -> (
+ S3ImportStorage,
+ SqsImportQueue,
+ process_recovery::ProcessConfig,
+) {
+ let suffix = Uuid::now_v7().simple().to_string();
+ let bucket = format!("archive-test-{suffix}");
+ let main = format!("archive-main-{suffix}");
+ let dlq = format!("archive-dlq-{suffix}");
+ let s3 = aws_sdk_s3::Client::from_conf(
+ aws_sdk_s3::Config::builder()
+ .behavior_version(BehaviorVersion::latest())
+ .region(Region::new("us-east-1"))
+ .credentials_provider(Credentials::new(
+ "test",
+ "test",
+ None,
+ None,
+ "isolated-test",
+ ))
+ .endpoint_url(endpoint)
+ .force_path_style(true)
+ .build(),
+ );
+ s3.create_bucket().bucket(&bucket).send().await.unwrap();
+ let sqs = aws_sdk_sqs::Client::from_conf(
+ aws_sdk_sqs::Config::builder()
+ .behavior_version(BehaviorVersion::latest())
+ .region(Region::new("us-east-1"))
+ .credentials_provider(Credentials::new(
+ "test",
+ "test",
+ None,
+ None,
+ "isolated-test",
+ ))
+ .endpoint_url(endpoint)
+ .build(),
+ );
+ for name in [&main, &dlq] {
+ sqs.create_queue().queue_name(name).send().await.unwrap();
+ }
+ use aws_sdk_sqs::types::QueueAttributeName;
+ let main_url = sqs
+ .get_queue_url()
+ .queue_name(&main)
+ .send()
+ .await
+ .unwrap()
+ .queue_url
+ .unwrap();
+ let dlq_url = sqs
+ .get_queue_url()
+ .queue_name(&dlq)
+ .send()
+ .await
+ .unwrap()
+ .queue_url
+ .unwrap();
+ let attributes = sqs
+ .get_queue_attributes()
+ .queue_url(dlq_url)
+ .attribute_names(QueueAttributeName::QueueArn)
+ .send()
+ .await
+ .unwrap();
+ let arn = &attributes.attributes.unwrap()[&QueueAttributeName::QueueArn];
+ sqs.set_queue_attributes()
+ .queue_url(main_url)
+ .attributes(
+ QueueAttributeName::RedrivePolicy,
+ serde_json::json!({"deadLetterTargetArn": arn, "maxReceiveCount": 2}).to_string(),
+ )
+ .send()
+ .await
+ .unwrap();
+ let queue = SqsImportQueue::new(
+ sqs,
+ &SlackImportQueue::from_owned(main.clone()),
+ &SlackImportDlq::from_owned(dlq.clone()),
+ )
+ .await
+ .unwrap();
+ let process = process_recovery::ProcessConfig {
+ endpoint: endpoint.to_owned(),
+ bucket: bucket.clone(),
+ main,
+ dlq,
+ };
+ let storage = S3ImportStorage::new(s3, bucket, ImportLimits::default()).unwrap();
+ (storage, queue, process)
+}
+
+fn user() -> MacroUserIdStr<'static> {
+ MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap()
+}
+
+async fn team(pool: &PgPool) -> TeamId {
+ let account = Uuid::now_v7();
+ let team = Uuid::now_v7();
+ sqlx::query!("INSERT INTO macro_user (id, username, email, stripe_customer_id) VALUES ($1, 't17', '[email protected]', 't17-customer')", account).execute(pool).await.unwrap();
+ sqlx::query!(r#"INSERT INTO "User" (id, email, macro_user_id) VALUES ('macro|[email protected]', '[email protected]', $1)"#, account).execute(pool).await.unwrap();
+ sqlx::query!("INSERT INTO team (id, name, owner_id) VALUES ($1, 'Atomic history', 'macro|[email protected]')", team).execute(pool).await.unwrap();
+ sqlx::query!("INSERT INTO team_user (team_id, user_id, team_role) VALUES ($1, 'macro|[email protected]', 'owner')", team).execute(pool).await.unwrap();
+ team.try_into().unwrap()
+}
+
+fn access(team: TeamId) -> EntityAccessReceipt<AdminTeamRole> {
+ EntityAccessReceipt::try_new(
+ EntityAccessAuth::Authenticated(user()),
+ Entity {
+ entity_id: team.to_string(),
+ entity_type: EntityType::Team,
+ },
+ EntityPermission::TeamRole {
+ role: TeamRole::Admin,
+ },
+ )
+ .unwrap()
+}
+
+// Production write authorization is retained. This harness intentionally hides
+// all target IDs in public receipts; reference disclosure uses the real lookup.
+struct Admin(WorkerAuthorizer<Access>);
+impl ImportAuthorizer for Admin {
+ async fn require_admin(&self, team: TeamId, user: &MacroUserIdStr<'_>) -> PortResult<()> {
+ self.0.require_admin(team, user).await
+ }
+ async fn require_target(
+ &self,
+ team: TeamId,
+ user: &MacroUserIdStr<'_>,
+ metadata: &ConversationMetadata,
+ target: Uuid,
+ ) -> PortResult<()> {
+ self.0.require_target(team, user, metadata, target).await
+ }
+}
+impl ImportProgressAccess for Admin {
+ async fn participating_targets(
+ &self,
+ _: &MacroUserIdStr<'_>,
+ _: &[Uuid],
+ ) -> PortResult<Vec<Uuid>> {
+ Ok(vec![])
+ }
+}
+struct NoGateway;
+impl ImportNotifier for NoGateway {
+ async fn invalidate(
+ &self,
+ _: &MacroUserIdStr<'_>,
+ _: TeamId,
+ _: JobId,
+ _: u64,
+ _: JobStatus,
+ ) -> PortResult<()> {
+ Ok(())
+ }
+}
+struct SystemClock;
+impl Clock for SystemClock {
+ fn now(&self) -> DateTime<Utc> {
+ Utc::now()
+ }
+}
+#[derive(Clone, Default)]
+struct SearchGate(Arc<AtomicBool>);
+impl SearchBackfillClient for SearchGate {
+ async fn submit(&self, _: &SearchBackfill) -> PortResult<Uuid> {
+ Ok(Uuid::now_v7())
+ }
+ async fn progress(&self, receipt_id: Uuid) -> PortResult<SearchState> {
+ if self.0.load(Ordering::SeqCst) {
+ Ok(SearchState::Completed)
+ } else {
+ Ok(SearchState::Submitted { receipt_id })
+ }
+ }
+}
+
+fn command(history: bool) -> CreateImport {
+ CreateImport {
+ idempotency_token: Uuid::now_v7().try_into().unwrap(),
+ source: SourceIdentity::ConfirmedUnknown,
+ include_message_history: history,
+ conversations: ["C1", "C2"]
+ .map(|id| ConversationMetadata {
+ slack_channel_id: id.parse().unwrap(),
+ kind: ConversationKind::PublicChannel,
+ name: "duplicate display name".into(),
+ folder: id.parse().unwrap(),
+ member_ids: vec!["U1".parse().unwrap(), "U3".parse().unwrap()],
+ creator_id: None,
+ created_at: Some("1.000001".parse().unwrap()),
+ archived: false,
+ message_count: None,
+ })
+ .to_vec(),
+ }
+}
+
+fn descriptor(upload: UploadId, bytes: &[u8]) -> UploadDescriptor {
+ let record_count = match upload {
+ UploadId::Users => None,
+ UploadId::ConversationPart { .. } => {
+ Some(bytes.iter().filter(|byte| **byte == b'\n').count() as u32)
+ }
+ };
+ UploadDescriptor {
+ upload,
+ sha256: format!("{:x}", Sha256::digest(bytes)).parse().unwrap(),
+ byte_length: bytes.len() as u64,
+ record_count,
+ }
+}
+
+async fn put(grant: &UploadGrant, bytes: &[u8]) -> reqwest::StatusCode {
+ let mut request = reqwest::Client::new().put(&grant.url).body(bytes.to_vec());
+ for (name, value) in &grant.required_headers {
+ request = request.header(name, value);
+ }
+ request.send().await.unwrap().status()
+}
+
+async fn upload_job(service: &impl ImportService, team: TeamId, history: bool) -> JobId {
+ let request = command(history);
+ let job = service.create(access(team), request.clone()).await.unwrap();
+ assert_eq!(
+ service
+ .create(access(team), request.clone())
+ .await
+ .unwrap()
+ .job_id,
+ job.job_id
+ );
+ let mut changed = request;
+ changed.conversations.pop();
+ assert_eq!(
+ service.create(access(team), changed).await.unwrap_err(),
+ ImportError::Conflict
+ );
+ let users = serde_json::to_vec(&serde_json::json!([
+ {"id":"U1", "name":"admin", "profile":{"email":"[email protected]"}},
+ {"id":"U2", "name":"Unknown author"},
+ {"id":"U3", "name":"External member", "profile":{"email":"[email protected]"}}
+ ]))
+ .unwrap();
+ let users_descriptor = descriptor(UploadId::Users, &users);
+ let grant = service
+ .register_uploads(
+ access(team),
+ job.job_id,
+ RegisterUploads {
+ descriptors: vec![users_descriptor],
+ },
+ )
+ .await
+ .unwrap()
+ .pop()
+ .unwrap();
+ assert!(put(&grant, &users).await.is_success());
+ assert_eq!(
+ put(&grant, &users).await,
+ reqwest::StatusCode::PRECONDITION_FAILED
+ );
+ service
+ .complete_uploads(
+ access(team),
+ job.job_id,
+ CompleteUploads {
+ uploads: vec![UploadId::Users],
+ seal: None,
+ },
+ )
+ .await
+ .unwrap();
+ // Registration and sealing cannot widen the persisted selection.
+ let foreign = "CUNSELECTED".parse().unwrap();
+ assert!(
+ service
+ .register_uploads(
+ access(team),
+ job.job_id,
+ RegisterUploads {
+ descriptors: vec![descriptor(
+ UploadId::ConversationPart {
+ slack_channel_id: foreign,
+ part_index: 0
+ },
+ b"{}\n"
+ )]
+ }
+ )
+ .await
+ .is_err()
+ );
+ let seal = ConversationSeal::from_descriptors(
+ "CUNSELECTED".parse().unwrap(),
+ &[],
+ &ImportLimits::default(),
+ )
+ .unwrap();
+ assert!(
+ service
+ .complete_uploads(
+ access(team),
+ job.job_id,
+ CompleteUploads {
+ uploads: vec![],
+ seal: Some(seal)
+ }
+ )
+ .await
+ .is_err()
+ );
+ assert!(
+ service
+ .progress(
+ access(Uuid::now_v7().try_into().unwrap()),
+ JobCommand { job_id: job.job_id }
+ )
+ .await
+ .is_err()
+ );
+ // Seal in reverse order. No message can rely on queue order for its links.
+ for id in ["C2", "C1"] {
+ let mut descriptors = vec![];
+ if history {
+ let other = if id == "C1" { "C2" } else { "C1" };
+ let records = [
+ serde_json::json!({"type":"message", "user":"U2", "ts":"2.000001", "text": format!("<@U1> <!channel> See <#{other}|other> and <https://unproven.slack.com/archives/{other}/p0000000002000001|original>"), "reactions":[{"name":"thumbsup", "users":["U1"], "count":1}]}),
+ serde_json::json!({"type":"message", "user":"U1", "ts":"3.000001", "thread_ts":"2.000001", "text":"historical reply"}),
+ ];
+ let bytes = records
+ .iter()
+ .map(|record| format!("{record}\n"))
+ .collect::<String>()
+ .into_bytes();
+ let part = descriptor(
+ UploadId::ConversationPart {
+ slack_channel_id: id.parse().unwrap(),
+ part_index: 0,
+ },
+ &bytes,
+ );
+ let grant = service
+ .register_uploads(
+ access(team),
+ job.job_id,
+ RegisterUploads {
+ descriptors: vec![part.clone()],
+ },
+ )
+ .await
+ .unwrap()
+ .pop()
+ .unwrap();
+ assert!(put(&grant, &bytes).await.is_success());
+ service
+ .complete_uploads(
+ access(team),
+ job.job_id,
+ CompleteUploads {
+ uploads: vec![part.upload.clone()],
+ seal: None,
+ },
+ )
+ .await
+ .unwrap();
+ descriptors.push(part);
+ }
+ let receipt = service
+ .progress(access(team), JobCommand { job_id: job.job_id })
+ .await
+ .unwrap();
+ assert_eq!(
+ receipt
+ .conversations
+ .iter()
+ .find(|conversation| conversation.slack_channel_id.as_str() == id)
+ .unwrap()
+ .status,
+ ConversationStatus::AwaitingUploads
+ );
+ let seal = ConversationSeal::from_descriptors(
+ id.parse().unwrap(),
+ &descriptors,
+ &ImportLimits::default(),
+ )
+ .unwrap();
+ service
+ .complete_uploads(
+ access(team),
+ job.job_id,
+ CompleteUploads {
+ uploads: vec![],
+ seal: Some(seal),
+ },
+ )
+ .await
+ .unwrap();
+ }
+ let receipt = service
+ .finalize(access(team), JobCommand { job_id: job.job_id })
+ .await
+ .unwrap();
+ assert_eq!(receipt.conversations.len(), 2);
+ assert_eq!(receipt.include_message_history, history);
+ job.job_id
+}
+
+async fn progress(repo: &PgSlackImportRepo, team: TeamId, job: JobId) -> ImportProgress {
+ repo.progress(team, job).await.unwrap().unwrap()
+}
+
+/// The historical composition has no live notification/event publisher. Check its
+/// real database effects too: mentions, reactions, external members and replies
+/// must not produce notifications, invitation emails or unread activity.
+async fn assert_backfill_is_silent(pool: &PgPool) {
+ let counts = sqlx::query!(
+ r#"
+ SELECT
+ (SELECT count(*) FROM notification) AS "notifications!",
+ (SELECT count(*) FROM user_notification) AS "user_notifications!",
+ (SELECT count(*) FROM notification_email_sent) AS "emails!",
+ (SELECT count(*) FROM channel_notification_email_sent) AS "channel_emails!",
+ (SELECT count(*) FROM comms_activity) AS "channel_activity!",
+ (SELECT count(*) FROM activity_events) AS "activity_events!"
+ "#
+ )
+ .fetch_one(pool)
+ .await
+ .unwrap();
+ assert_eq!(
+ counts.notifications, 0,
+ "history must not create notifications"
+ );
+ assert_eq!(
+ counts.user_notifications, 0,
+ "history must not create unread notifications"
+ );
+ assert_eq!(counts.emails, 0, "history must not send emails");
+ assert_eq!(
+ counts.channel_emails, 0,
+ "history must not send channel invitations"
+ );
+ assert_eq!(
+ counts.channel_activity, 0,
+ "history must not create per-user activity"
+ );
+ assert_eq!(
+ counts.activity_events, 0,
+ "history must not create live activity events"
+ );
+}
+
+#[ignore = "requires Docker with localstack/localstack:4 and a local PostgreSQL test role"]
+#[sqlx::test(migrations = "../../crates/macro_db_client/migrations")]
+async fn sealed_uploads_queue_replay_and_search_completion_barrier(pool: PgPool) {
+ let (_container, endpoint) = LocalStack::start().await;
+ let (storage, queue, _) = resources(&endpoint).await;
+ let team = team(&pool).await;
+ let limits = ImportLimits::default();
+ let repo = PgSlackImportRepo::new(pool.clone(), limits);
+ let authorizer = WorkerAuthorizer::new(
+ pool.clone(),
+ Access::new(PgAccessRepository::new(pool.clone())),
+ );
+ let ledger = CanonicalImportLedger::new(PgImportRepo::new(pool.clone()));
+ let service = SlackImportService::new(
+ repo.clone(),
+ storage.clone(),
+ ledger.clone(),
+ NoGateway,
+ Admin(authorizer.clone()),
+ |_| true,
+ limits,
+ )
+ .unwrap();
+ let sink = ChannelImportSink::new(pool.clone(), authorizer.clone(), limits).unwrap();
+ let search = SearchGate::default();
+ let maintenance = Arc::new(ImportMaintenance::new(
+ repo.clone(),
+ queue.clone(),
+ search.clone(),
+ SystemClock,
+ WorkerReferenceReconciler::new(pool.clone(), limits),
+ ));
+ let importer = Arc::new(
+ ConversationImporter::new(
+ repo.clone(),
+ storage,
+ ledger,
+ sink.clone(),
+ sink.clone(),
+ authorizer,
+ ImporterConfig { limits },
+ )
+ .unwrap(),
+ );
+ assert_backfill_is_silent(&pool).await;
+ // Shape creation followed by history and an exact repeat reuse the same IDs.
+ let mut canonical = None;
+ for history in [false, true, true] {
+ search.0.store(false, Ordering::SeqCst);
+ let job = upload_job(&service, team, history).await;
+ let events = repo.pending_events(50).await.unwrap();
+ assert_eq!(events.len(), 2);
+ // Fault boundary: SQS accepted the send, but the process died before the
+ // PostgreSQL publication acknowledgement. The real outbox republishes it.
+ for event in &events {
+ queue.publish(event).await.unwrap();
+ }
+ // Expire only this test job's publication reservation, as wall time would
+ // after a publisher crash. pending_events is a reservation, not a read.
+ sqlx::query!("UPDATE slack_import_outbox SET available_at = clock_timestamp() - interval '1 second' WHERE job_id = $1", Uuid::from(job)).execute(&pool).await.unwrap();
+ maintenance.publish().await.unwrap();
+ assert!(repo.pending_events(50).await.unwrap().is_empty());
+ let stop = CancellationToken::new();
+ let driver = {
+ let (importer, maintenance, queue, stop) = (
+ importer.clone(),
+ maintenance.clone(),
+ queue.clone(),
+ stop.clone(),
+ );
+ tokio::spawn(async move {
+ worker::run(
+ importer.as_ref(),
+ maintenance.as_ref(),
+ &queue,
+ Uuid::now_v7().try_into().unwrap(),
+ WorkerConfig::new(true, 2, 5).unwrap(),
+ stop,
+ )
+ .await;
+ })
+ };
+ timeout(Duration::from_secs(60), async {
+ loop {
+ let receipt = progress(&repo, team, job).await;
+ if receipt
+ .conversations
+ .iter()
+ .all(|c| c.status == ConversationStatus::Completed)
+ {
+ break;
+ }
+ sleep(Duration::from_millis(100)).await;
+ }
+ })
+ .await
+ .expect("queue driver did not settle selected conversations");
+ stop.cancel();
+ timeout(Duration::from_secs(35), driver)
+ .await
+ .unwrap()
+ .unwrap();
+ // Reconcile links and publish search without a competing maintenance loop.
+ maintenance.reconcile().await.unwrap();
+ let receipt = progress(&repo, team, job).await;
+ let targets: Vec<_> = receipt
+ .conversations
+ .iter()
+ .map(|c| c.channel_id.unwrap())
+ .collect();
+ if let Some(ref expected) = canonical {
+ assert_eq!(&targets, expected);
+ } else {
+ canonical = Some(targets.clone());
+ }
+ if history
+ && receipt
+ .conversations
+ .iter()
+ .any(|c| c.counters.imported > 0)
+ {
+ assert_eq!(
+ receipt.status,
+ JobStatus::Processing,
+ "search acceptance is not completion"
+ );
+ for (index, id) in ["C1", "C2"].iter().enumerate() {
+ let sources = ["2.000001", "3.000001"].map(|ts| SourceMessageId {
+ team_id: team,
+ slack_channel_id: id.parse().unwrap(),
+ ts: ts.parse().unwrap(),
+ });
+ let mapped = sink.lookup(&sources).await.unwrap();
+ assert_eq!(mapped.len(), 2);
+ let root = mapped
+ .iter()
+ .find(|(source, _)| source.ts == sources[0].ts)
+ .unwrap()
+ .1;
+ let body =
+ sqlx::query_scalar!("SELECT content FROM comms_messages WHERE id = $1", root)
+ .fetch_one(&pool)
+ .await
+ .unwrap();
+ assert!(
+ body.contains(&targets[1 - index].to_string()),
+ "reference must contain final canonical target UUID"
+ );
+ assert!(body.contains("<m-link>"));
+ assert!(
+ body.contains("unproven.slack.com"),
+ "unproven domain must stay external"
+ );
+ let replies = sqlx::query_scalar!(
+ "SELECT thread_id FROM comms_messages WHERE channel_id = $1 AND id <> $2",
+ targets[index],
+ root
+ )
+ .fetch_all(&pool)
+ .await
+ .unwrap();
+ assert_eq!(replies, vec![Some(root)]);
+ }
+ }
+ search.0.store(true, Ordering::SeqCst);
+ // Make the 60-second search receipt poll due in this isolated fixture.
+ sqlx::query!("UPDATE slack_import_outbox SET available_at = clock_timestamp() - interval '1 second' WHERE job_id = $1", Uuid::from(job)).execute(&pool).await.unwrap();
+ maintenance.reconcile().await.unwrap();
+ assert_eq!(
+ progress(&repo, team, job).await.status,
+ JobStatus::Completed
+ );
+ assert_backfill_is_silent(&pool).await;
+ }
+ assert_eq!(
+ sqlx::query_scalar!("SELECT count(*) FROM comms_messages")
+ .fetch_one(&pool)
+ .await
+ .unwrap(),
+ Some(4)
+ );
+
+ // Exhaust real receives (without a worker deleting them) into the configured
+ // DLQ. Disabling admission must still run the production DLQ driver.
+ // Cancelled HTTP long polls can still reserve messages server-side until
+ // their timeout. A fresh queue keeps this redrive scenario independent.
+ let (storage, queue, _) = resources(&endpoint).await;
+ let authorizer = WorkerAuthorizer::new(
+ pool.clone(),
+ Access::new(PgAccessRepository::new(pool.clone())),
+ );
+ let service = SlackImportService::new(
+ repo.clone(),
+ storage,
+ CanonicalImportLedger::new(PgImportRepo::new(pool.clone())),
+ NoGateway,
+ Admin(authorizer),
+ |_| true,
+ limits,
+ )
+ .unwrap();
+ let maintenance = Arc::new(ImportMaintenance::new(
+ repo.clone(),
+ queue.clone(),
+ search,
+ SystemClock,
+ WorkerReferenceReconciler::new(pool.clone(), limits),
+ ));
+ let exhausted = upload_job(&service, team, false).await;
+ maintenance.publish().await.unwrap();
+ let mut twice = std::collections::HashSet::new();
+ timeout(Duration::from_secs(60), async {
+ while twice.len() != 2 {
+ let Some(delivery) = ImportConsumer::receive(&queue, false).await.unwrap() else {
+ continue;
+ };
+ let (event, count) = SqsImportQueue::envelope(&delivery);
+ let event = event.unwrap();
+ if event.job_id != exhausted {
+ ImportConsumer::delete(&queue, &delivery).await.unwrap();
+ continue;
+ }
+ if count >= 2 {
+ twice.insert(event.slack_channel_id);
+ }
+ queue.extend_visibility(&delivery, 0).await.unwrap();
+ }
+ // The next main receive triggers redrive, rather than fabricating a DLQ send.
+ assert!(
+ ImportConsumer::receive(&queue, false)
+ .await
+ .unwrap()
+ .is_none()
+ );
+ })
+ .await
+ .expect("main queue did not exhaust into the DLQ");
+ let stop = CancellationToken::new();
+ let driver = {
+ let (importer, maintenance, queue, stop) = (
+ importer.clone(),
+ maintenance.clone(),
+ queue.clone(),
+ stop.clone(),
+ );
+ tokio::spawn(async move {
+ worker::run(
+ importer.as_ref(),
+ maintenance.as_ref(),
+ &queue,
+ Uuid::now_v7().try_into().unwrap(),
+ WorkerConfig::new(false, 1, 5).unwrap(),
+ stop,
+ )
+ .await;
+ })
+ };
+ timeout(Duration::from_secs(60), async {
+ loop {
+ let receipt = progress(&repo, team, exhausted).await;
+ if receipt.status == JobStatus::Failed {
+ assert!(
+ receipt
+ .conversations
+ .iter()
+ .all(|conversation| conversation.status == ConversationStatus::Failed)
+ );
+ break;
+ }
+ sleep(Duration::from_millis(100)).await;
+ }
+ })
+ .await
+ .expect("disabled worker did not persist exhausted work as Failed");
+ assert_backfill_is_silent(&pool).await;
+ stop.cancel();
+ timeout(Duration::from_secs(35), driver)
+ .await
+ .unwrap()
+ .unwrap();
+}
diff --git a/services/slack_import_worker/tests/archive_import/kill_boundaries.sql b/services/slack_import_worker/tests/archive_import/kill_boundaries.sql
new file mode 100644
index 0000000000..5cb23947e6
--- /dev/null
+++ b/services/slack_import_worker/tests/archive_import/kill_boundaries.sql
@@ -0,0 +1,29 @@
+-- Installed only in a SQLx-created isolated test database. Each trigger pauses
+-- the real worker at a durable boundary; the parent holds advisory lock 27001.
+CREATE FUNCTION pause_archive_worker() RETURNS trigger LANGUAGE plpgsql AS $$
+BEGIN
+ CASE TG_TABLE_NAME
+ WHEN 'comms_channels' THEN
+ IF current_setting('application_name') = 'kill-after-lease' THEN
+ PERFORM pg_advisory_xact_lock(27001);
+ END IF;
+ WHEN 'slack_import_conversation' THEN
+ IF current_setting('application_name') = 'kill-after-batch'
+ AND NEW.status = 'completed' THEN
+ PERFORM pg_advisory_xact_lock(27001);
+ END IF;
+ WHEN 'slack_import_outbox' THEN
+ IF current_setting('application_name') = 'kill-after-publication'
+ AND NEW.published_at IS NOT NULL THEN
+ PERFORM pg_advisory_xact_lock(27001);
+ END IF;
+ END CASE;
+ RETURN NEW;
+END;
+$$;
+CREATE TRIGGER pause_channel BEFORE INSERT ON comms_channels
+ FOR EACH ROW EXECUTE FUNCTION pause_archive_worker();
+CREATE TRIGGER pause_completion BEFORE UPDATE ON slack_import_conversation
+ FOR EACH ROW EXECUTE FUNCTION pause_archive_worker();
+CREATE TRIGGER pause_publication BEFORE UPDATE ON slack_import_outbox
+ FOR EACH ROW EXECUTE FUNCTION pause_archive_worker();
diff --git a/services/slack_import_worker/tests/archive_import/process_recovery.rs b/services/slack_import_worker/tests/archive_import/process_recovery.rs
new file mode 100644
index 0000000000..a74a3ce439
--- /dev/null
+++ b/services/slack_import_worker/tests/archive_import/process_recovery.rs
@@ -0,0 +1,263 @@
+//! SIGKILL the actual worker binary, not an in-process future or mock importer.
+//! PostgreSQL triggers in this isolated database make the kill boundaries exact.
+
+use std::process::{Child, Stdio};
+
+use sqlx::ConnectOptions;
+
+use super::*;
+
+pub(super) struct ProcessConfig {
+ pub endpoint: String,
+ pub bucket: String,
+ pub main: String,
+ pub dlq: String,
+}
+
+struct WorkerProcess(Child);
+
+impl WorkerProcess {
+ fn start(config: &ProcessConfig, pool: &PgPool, boundary: &str, search_url: &str) -> Self {
+ let mut database = pool.connect_options().to_url_lossy();
+ database
+ .query_pairs_mut()
+ .append_pair("application_name", boundary);
+ Self(
+ Command::new(env!("CARGO_BIN_EXE_slack_import_worker"))
+ .env_clear()
+ .env("DATABASE_URL", database.as_str())
+ .env("LOCAL_AWS_URL", &config.endpoint)
+ .env("UPLOAD_STAGING_BUCKET", &config.bucket)
+ .env("OVERRIDE_SLACK_IMPORT_QUEUE", &config.main)
+ .env("OVERRIDE_SLACK_IMPORT_DLQ", &config.dlq)
+ .env("OVERRIDE_SEARCH_PROCESSING_SERVICE_URL", search_url)
+ .env("INTERNAL_API_KEY", "isolated-process-test")
+ .env("SLACK_IMPORT_ENABLED", "true")
+ .env("SLACK_IMPORT_CONCURRENCY", "1")
+ .env("RUST_LOG", "off")
+ .stdin(Stdio::null())
+ .stdout(Stdio::null())
+ .stderr(Stdio::inherit())
+ .spawn()
+ .unwrap(),
+ )
+ }
+
+ fn kill(&mut self) {
+ self.0.kill().unwrap();
+ let status = self.0.wait().unwrap();
+ assert!(
+ !status.success(),
+ "fault injection must terminate the process"
+ );
+ #[cfg(unix)]
+ {
+ use std::os::unix::process::ExitStatusExt;
+ assert_eq!(
+ status.signal(),
+ Some(9),
+ "recovery must not depend on graceful shutdown"
+ );
+ }
+ }
+}
+
+impl Drop for WorkerProcess {
+ fn drop(&mut self) {
+ let _ = self.0.kill();
+ let _ = self.0.wait();
+ }
+}
+
+async fn recover(pool: PgPool, boundary: &str) {
+ let (_container, endpoint) = LocalStack::start().await;
+ let (storage, queue, config) = resources(&endpoint).await;
+ let team = team(&pool).await;
+ let limits = ImportLimits::default();
+ let repo = PgSlackImportRepo::new(pool.clone(), limits);
+ let authorizer = WorkerAuthorizer::new(
+ pool.clone(),
+ Access::new(PgAccessRepository::new(pool.clone())),
+ );
+ let service = SlackImportService::new(
+ repo.clone(),
+ storage,
+ CanonicalImportLedger::new(PgImportRepo::new(pool.clone())),
+ NoGateway,
+ Admin(authorizer),
+ |_| true,
+ limits,
+ )
+ .unwrap();
+ let job = upload_job(&service, team, true).await;
+ let events = repo.pending_events(50).await.unwrap();
+ sqlx::query!("UPDATE slack_import_outbox SET available_at = clock_timestamp() - interval '1 second' WHERE job_id = $1", Uuid::from(job)).execute(&pool).await.unwrap();
+ // DDL is test-local and depends on the runtime SQLx database, not a migration.
+ sqlx::raw_sql(include_str!("kill_boundaries.sql"))
+ .execute(&pool)
+ .await
+ .unwrap();
+ let mut barrier = pool.acquire().await.unwrap();
+ sqlx::query!("SELECT pg_advisory_lock(27001)")
+ .execute(&mut *barrier)
+ .await
+ .unwrap();
+ // Reserve an unused local port so search cannot accidentally hit a real service.
+ // Completion is verified below through the explicitly controlled receipt gate.
+ let search_listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
+ let search_url = format!("http://{}", search_listener.local_addr().unwrap());
+ let mut process = WorkerProcess::start(&config, &pool, boundary, &search_url);
+ timeout(Duration::from_secs(60), async {
+ loop {
+ assert!(process.0.try_wait().unwrap().is_none(), "worker exited before fault");
+ let waiting = sqlx::query_scalar!(
+ "SELECT EXISTS(SELECT 1 FROM pg_stat_activity WHERE application_name = $1 AND wait_event = 'advisory')",
+ boundary
+ )
+ .fetch_one(&pool)
+ .await
+ .unwrap();
+ if waiting == Some(true) {
+ break;
+ }
+ sleep(Duration::from_millis(50)).await;
+ }
+ })
+ .await
+ .expect("worker did not reach the durable kill boundary");
+
+ // Receipts take a shared job lock. Kill first so the deliberately blocked
+ // transaction rolls back; otherwise reading a receipt would deadlock here.
+ process.kill();
+ // PostgreSQL may still be waiting on our advisory lock after socket closure.
+ // Release it so the dead connection's transaction can unwind before reading.
+ sqlx::query!("SELECT pg_advisory_unlock(27001)")
+ .fetch_one(&mut *barrier)
+ .await
+ .unwrap();
+ drop(barrier);
+ let before = progress(&repo, team, job).await;
+ let message_ids = sqlx::query_scalar!("SELECT id FROM comms_messages ORDER BY id")
+ .fetch_all(&pool)
+ .await
+ .unwrap();
+ match boundary {
+ "kill-after-lease" => {
+ assert!(
+ before
+ .conversations
+ .iter()
+ .any(|c| c.status == ConversationStatus::Importing)
+ );
+ assert!(message_ids.is_empty());
+ }
+ "kill-after-batch" => {
+ assert_eq!(message_ids.len(), 2);
+ assert_eq!(
+ before
+ .conversations
+ .iter()
+ .map(|c| c.counters.imported)
+ .sum::<u64>(),
+ 2
+ );
+ }
+ "kill-after-publication" => {
+ // A blocked published_at UPDATE is after SQS accepted the send. The
+ // transaction is still uncommitted, so the outbox must survive death.
+ assert_eq!(sqlx::query_scalar!(
+ "SELECT count(*) FROM slack_import_outbox WHERE job_id = $1 AND kind = 'import' AND published_at IS NULL",
+ Uuid::from(job)
+ ).fetch_one(&pool).await.unwrap(), Some(2));
+ }
+ _ => unreachable!(),
+ }
+ // Advance only this test job's durable deadlines; no hosted leases are touched.
+ sqlx::query!("UPDATE slack_import_conversation SET lease_expires_at = clock_timestamp() - interval '1 second' WHERE job_id = $1 AND status = 'importing'", Uuid::from(job)).execute(&pool).await.unwrap();
+ sqlx::query!("UPDATE slack_import_outbox SET available_at = clock_timestamp() - interval '1 second' WHERE job_id = $1", Uuid::from(job)).execute(&pool).await.unwrap();
+ // Duplicate delivery after a lost worker is legal. It avoids waiting the full
+ // production SQS visibility timeout while still exercising fenced reclamation.
+ for event in &events {
+ queue.publish(event).await.unwrap();
+ }
+ let mut restarted = WorkerProcess::start(&config, &pool, "restarted-worker", &search_url);
+ timeout(Duration::from_secs(60), async {
+ loop {
+ assert!(
+ restarted.0.try_wait().unwrap().is_none(),
+ "restarted worker exited"
+ );
+ if progress(&repo, team, job)
+ .await
+ .conversations
+ .iter()
+ .all(|c| c.status == ConversationStatus::Completed)
+ {
+ break;
+ }
+ sleep(Duration::from_millis(100)).await;
+ }
+ })
+ .await
+ .expect("restarted worker did not recover committed history");
+ restarted.kill();
+ let final_ids = sqlx::query_scalar!("SELECT id FROM comms_messages ORDER BY id")
+ .fetch_all(&pool)
+ .await
+ .unwrap();
+ assert_eq!(final_ids.len(), 4);
+ assert!(
+ message_ids.iter().all(|id| final_ids.contains(id)),
+ "committed UUIDs must survive replay"
+ );
+ let receipt = progress(&repo, team, job).await;
+ assert_eq!(
+ receipt
+ .conversations
+ .iter()
+ .map(|c| c.counters.imported)
+ .sum::<u64>(),
+ 4
+ );
+ assert_ne!(
+ receipt.status,
+ JobStatus::Completed,
+ "unpublished search cannot complete a job"
+ );
+ let search = SearchGate::default();
+ search.0.store(true, Ordering::SeqCst);
+ let maintenance = ImportMaintenance::new(
+ repo.clone(),
+ queue,
+ search,
+ SystemClock,
+ WorkerReferenceReconciler::new(pool.clone(), limits),
+ );
+ // First reconciliation submits; a second due poll observes publication.
+ for _ in 0..2 {
+ sqlx::query!("UPDATE slack_import_outbox SET available_at = clock_timestamp() - interval '1 second' WHERE job_id = $1", Uuid::from(job)).execute(&pool).await.unwrap();
+ maintenance.reconcile().await.unwrap();
+ }
+ assert_eq!(
+ progress(&repo, team, job).await.status,
+ JobStatus::Completed
+ );
+}
+
+#[ignore = "requires local PostgreSQL and preloaded localstack/localstack:4"]
+#[sqlx::test(migrations = "../../crates/macro_db_client/migrations")]
+async fn process_death_after_lease(pool: PgPool) {
+ recover(pool, "kill-after-lease").await;
+}
+
+#[ignore = "requires local PostgreSQL and preloaded localstack/localstack:4"]
+#[sqlx::test(migrations = "../../crates/macro_db_client/migrations")]
+async fn process_death_after_committed_batch(pool: PgPool) {
+ recover(pool, "kill-after-batch").await;
+}
+
+#[ignore = "requires local PostgreSQL and preloaded localstack/localstack:4"]
+#[sqlx::test(migrations = "../../crates/macro_db_client/migrations")]
+async fn process_death_after_queue_publication(pool: PgPool) {
+ recover(pool, "kill-after-publication").await;
+}
diff --git a/tooling/just/rust.just b/tooling/just/rust.just
index ec01ba9846..97f929f3f2 100644
--- a/tooling/just/rust.just
+++ b/tooling/just/rust.just
@@ -86,6 +86,8 @@ setup_test_envs:
echo "DATABASE_URL={{ DATABASE_URL }}" >> ./crates/frecency/.env
echo "DATABASE_URL={{ DATABASE_URL }}" >> ./crates/favorites/.env
echo "DATABASE_URL={{ DATABASE_URL }}" >> ./crates/reminders/.env
+ echo "DATABASE_URL={{ DATABASE_URL }}" >> ./crates/slack_integration/.env
+ echo "DATABASE_URL={{ DATABASE_URL }}" >> ./services/slack_import_worker/.env
echo "DATABASE_URL={{ DATABASE_URL }}" >> ./crates/user_api_key/.env
echo "DATABASE_URL={{ DATABASE_URL }}" >> ./crates/soup/.env
echo "DATABASE_URL={{ DATABASE_URL }}" >> ./crates/email/.env
diff --git a/tooling/xtask/crates/xtask_local/src/local/inventory.rs b/tooling/xtask/crates/xtask_local/src/local/inventory.rs
index 693c9dd2ed..9e8ef00621 100644
--- a/tooling/xtask/crates/xtask_local/src/local/inventory.rs
+++ b/tooling/xtask/crates/xtask_local/src/local/inventory.rs
@@ -91,6 +91,18 @@ impl RustService {
/// The full service inventory of the local service binaries.
pub const RUST_SERVICES: &[RustService] = &[
+ RustService {
+ compose_name: "slack_import_worker",
+ cargo_bin: "slack_import_worker",
+ package: "slack_import_worker",
+ host_port: None,
+ path_prefix: None,
+ is_websocket: false,
+ // Explicit local Compose profile only; never consume shared-dev queues.
+ modes: &[],
+ opt_in: true,
+ no_default_features: false,
+ },
RustService {
compose_name: "preview_gateway",
cargo_bin: "preview_gateway",
diff --git a/tooling/xtask/crates/xtask_local/src/local/inventory/test.rs b/tooling/xtask/crates/xtask_local/src/local/inventory/test.rs
index ced0978422..2ccfd5f513 100644
--- a/tooling/xtask/crates/xtask_local/src/local/inventory/test.rs
+++ b/tooling/xtask/crates/xtask_local/src/local/inventory/test.rs
@@ -32,7 +32,11 @@ fn nonobvious_crate_mappings() {
#[test]
fn workers_are_portless() {
- for name in ["document_upload_finalizer", "email_pubsub_workers"] {
+ for name in [
+ "document_upload_finalizer",
+ "email_pubsub_workers",
+ "slack_import_worker",
+ ] {
let svc = RUST_SERVICES
.iter()
.find(|s| s.compose_name == name)
@@ -41,6 +45,31 @@ fn workers_are_portless() {
}
}
+#[test]
+fn slack_import_worker_is_opt_in() {
+ let worker = RUST_SERVICES
+ .iter()
+ .find(|svc| svc.compose_name == "slack_import_worker")
+ .unwrap();
+ assert_eq!(worker.package, "slack_import_worker");
+ assert_eq!(worker.cargo_bin, "slack_import_worker");
+ assert!(worker.path_prefix.is_none());
+ assert!(worker.is_opt_in());
+ assert!(!local_binaries().contains(&worker.cargo_bin));
+ for mode in [Mode::Local, Mode::Dev] {
+ assert!(!services_for_mode(mode).any(|svc| svc.compose_name == worker.compose_name));
+ }
+ let compose: serde_yaml::Value = serde_yaml::from_str(include_str!(
+ "../../../../../../../docker/docker-compose.yml"
+ ))
+ .unwrap();
+ let service = &compose["services"][worker.compose_name];
+ assert_eq!(service["profiles"][0], "slack-import");
+ assert!(service["ports"].is_null());
+ assert_eq!(service["stop_grace_period"], "120s");
+ assert_eq!(service["build"]["args"]["SERVICE_NAME"], worker.cargo_bin);
+}
+
#[test]
fn agent_harness_has_an_instance_port() {
let svc = RUST_SERVICES
diff --git a/tooling/xtask/crates/xtask_local/src/local/local_env.rs b/tooling/xtask/crates/xtask_local/src/local/local_env.rs
index 601a1a33eb..b51e71c93d 100644
--- a/tooling/xtask/crates/xtask_local/src/local/local_env.rs
+++ b/tooling/xtask/crates/xtask_local/src/local/local_env.rs
@@ -277,6 +277,12 @@ impl InfraEnv {
"OVERRIDE_DOCUMENT_STORAGE_SERVICE_URL".into(),
"http://document-storage-service:8080".into(),
);
+ // Historical indexing calls the processing service's scoped backfill API,
+ // not the search query service. Both run on the local Compose network.
+ env.insert(
+ "OVERRIDE_SEARCH_PROCESSING_SERVICE_URL".into(),
+ "http://search-processing-service:8080".into(),
+ );
// Account deletion awaits both owning services from the auth container.
env.insert(
"OVERRIDE_AGENT_HARNESS_SERVICE_URL".into(),
@@ -648,6 +654,9 @@ struct BootStubEnv;
impl BootStubEnv {
fn write(&self, env: &mut BTreeMap<String, String>) {
+ // The worker is opt-in and ships paused; an explicit env-file can enable it.
+ env.insert("SLACK_IMPORT_ENABLED".into(), "false".into());
+ env.insert("SLACK_IMPORT_CONCURRENCY".into(), "1".into());
// connection_gateway config reads `REDIS_HOST` (a Redis URL, not a
// hostname — see `redis::Client::open`).
env.insert("REDIS_HOST".into(), "redis://redis:6379".into());
diff --git a/tooling/xtask/crates/xtask_local/src/local/local_env/test.rs b/tooling/xtask/crates/xtask_local/src/local/local_env/test.rs
index 58cb881087..73ffcad669 100644
--- a/tooling/xtask/crates/xtask_local/src/local/local_env/test.rs
+++ b/tooling/xtask/crates/xtask_local/src/local/local_env/test.rs
@@ -14,6 +14,31 @@ fn local_env() -> BTreeMap<String, String> {
/// Every key a local service relies on must be present — this is the test that
/// replaces "someone remembers to update defaults.env".
+#[test]
+fn slack_import_worker_uses_local_resources_and_starts_paused() {
+ let env = local_env();
+ assert_eq!(
+ env["OVERRIDE_SLACK_IMPORT_QUEUE"],
+ resources::queue_url(macro_queues::SlackImportQueue::LOCAL)
+ );
+ assert_eq!(
+ env["OVERRIDE_SLACK_IMPORT_DLQ"],
+ resources::queue_url(macro_queues::SlackImportDlq::LOCAL)
+ );
+ assert_eq!(env["UPLOAD_STAGING_BUCKET"], "bulk-upload-staging");
+ assert_eq!(
+ env["OVERRIDE_SEARCH_PROCESSING_SERVICE_URL"],
+ "http://search-processing-service:8080"
+ );
+ assert_eq!(
+ env["OVERRIDE_CONNECTION_GATEWAY_URL"],
+ "http://connection-gateway:8080"
+ );
+ assert_eq!(env["INTERNAL_API_KEY"], env["INTERNAL_API_SECRET_KEY"]);
+ assert_eq!(env["SLACK_IMPORT_ENABLED"], "false");
+ assert_eq!(env["SLACK_IMPORT_CONCURRENCY"], "1");
+}
+
#[test]
fn emits_required_keys() {
let env = local_env();
diff --git a/tooling/xtask/crates/xtask_local/src/local/localstack.rs b/tooling/xtask/crates/xtask_local/src/local/localstack.rs
index 283b303abc..b170af9c44 100644
--- a/tooling/xtask/crates/xtask_local/src/local/localstack.rs
+++ b/tooling/xtask/crates/xtask_local/src/local/localstack.rs
@@ -124,6 +124,19 @@ async fn create_queues(sqs: &aws_sdk_sqs::Client) -> Result<()> {
}
ignore_exists(request.send().await.map(|_| ()), &format!("queue {name}"))?;
}
+ // Update existing queues too; do not rely on CreateQueue accepting changed attributes.
+ for queue in resources::QUEUES {
+ let attributes = queue.attributes();
+ if attributes.is_empty() {
+ continue;
+ }
+ sqs.set_queue_attributes()
+ .queue_url(resources::queue_url(queue.name))
+ .set_attributes(Some(attributes))
+ .send()
+ .await
+ .with_context(|| format!("setting attributes on {}", queue.name))?;
+ }
Ok(())
}
diff --git a/tooling/xtask/crates/xtask_local/src/local/resources.rs b/tooling/xtask/crates/xtask_local/src/local/resources.rs
index 6fa39290c3..cdef681428 100644
--- a/tooling/xtask/crates/xtask_local/src/local/resources.rs
+++ b/tooling/xtask/crates/xtask_local/src/local/resources.rs
@@ -83,6 +83,36 @@ pub struct Queue {
pub bindings: &'static [(&'static str, QueueForm)],
}
+impl Queue {
+ /// Non-default attributes, applied after all queues exist so redrive targets resolve.
+ pub fn attributes(
+ &self,
+ ) -> std::collections::HashMap<aws_sdk_sqs::types::QueueAttributeName, String> {
+ use aws_sdk_sqs::types::QueueAttributeName;
+
+ match self.name {
+ macro_queues::SlackImportQueue::LOCAL => [
+ (QueueAttributeName::VisibilityTimeout, "900".to_string()),
+ (
+ QueueAttributeName::RedrivePolicy,
+ serde_json::json!({
+ "deadLetterTargetArn": queue_arn(macro_queues::SlackImportDlq::LOCAL),
+ "maxReceiveCount": 5,
+ })
+ .to_string(),
+ ),
+ ]
+ .into(),
+ macro_queues::SlackImportDlq::LOCAL => [(
+ QueueAttributeName::MessageRetentionPeriod,
+ (14 * 24 * 60 * 60).to_string(),
+ )]
+ .into(),
+ _ => Default::default(),
+ }
+ }
+}
+
/// An S3 bucket: the name created in LocalStack and the env var pointing at it.
pub struct Bucket {
/// The bucket name created in LocalStack.
@@ -104,6 +134,14 @@ use QueueForm::{Name, Url};
/// Every local SQS queue and the env var(s) that reference it.
pub const QUEUES: &[Queue] = &[
+ Queue {
+ name: macro_queues::SlackImportQueue::LOCAL,
+ bindings: &[(macro_queues::SlackImportQueue::OVERRIDE_ENV_VAR_NAME, Url)],
+ },
+ Queue {
+ name: macro_queues::SlackImportDlq::LOCAL,
+ bindings: &[(macro_queues::SlackImportDlq::OVERRIDE_ENV_VAR_NAME, Url)],
+ },
Queue {
name: macro_queues::NotificationQueue::LOCAL,
bindings: &[("NOTIFICATION_QUEUE", Url)],
diff --git a/tooling/xtask/crates/xtask_local/src/local/resources/test.rs b/tooling/xtask/crates/xtask_local/src/local/resources/test.rs
index b58711add5..0734330a5c 100644
--- a/tooling/xtask/crates/xtask_local/src/local/resources/test.rs
+++ b/tooling/xtask/crates/xtask_local/src/local/resources/test.rs
@@ -48,6 +48,47 @@ fn every_queue_is_bound() {
}
}
+#[test]
+fn slack_import_queues_match_deployment_and_have_redrive() {
+ use aws_sdk_sqs::types::QueueAttributeName;
+
+ let main = QUEUES
+ .iter()
+ .find(|queue| queue.name == macro_queues::SlackImportQueue::LOCAL)
+ .unwrap();
+ let dlq = QUEUES
+ .iter()
+ .find(|queue| queue.name == macro_queues::SlackImportDlq::LOCAL)
+ .unwrap();
+ assert_eq!(main.name, "slack-import-queue");
+ assert_eq!(dlq.name, "slack-import-dlq");
+ for queue in [main, dlq] {
+ assert!(matches!(queue.bindings[0].1, QueueForm::Url));
+ assert_eq!(queue.bindings[0].1.value(queue.name), queue_url(queue.name));
+ }
+ let attrs = main.attributes();
+ assert_eq!(attrs[&QueueAttributeName::VisibilityTimeout], "900");
+ let redrive: serde_json::Value =
+ serde_json::from_str(&attrs[&QueueAttributeName::RedrivePolicy]).unwrap();
+ assert_eq!(
+ redrive,
+ serde_json::json!({
+ "deadLetterTargetArn": queue_arn(dlq.name),
+ "maxReceiveCount": 5,
+ })
+ );
+ assert_eq!(
+ dlq.attributes()[&QueueAttributeName::MessageRetentionPeriod],
+ "1209600"
+ );
+ assert!(
+ QUEUES
+ .iter()
+ .filter(|queue| queue.name != main.name && queue.name != dlq.name)
+ .all(|queue| queue.attributes().is_empty())
+ );
+}
+
#[test]
fn webhook_fifo_queue_uses_a_full_url_override() {
let queue = QUEUES