Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
feat(import): agent-guided Notion workspace import (#8367)
Wolf Mermelstein552c0afb0f60
diff --git a/.github/workspace-dep-closures.json b/.github/workspace-dep-closures.jsonindex 4205aab0b0..bc32882712 100644--- a/.github/workspace-dep-closures.json+++ b/.github/workspace-dep-closures.json@@ -680,6 +680,7 @@"crates/harnesses","crates/image_generation","crates/import",+ "crates/import_notion","crates/initiative","crates/initiative_description","crates/invite_email",@@ -865,6 +866,7 @@"crates/harness_token","crates/image_generation","crates/import",+ "crates/import_notion","crates/initiative","crates/initiative_description","crates/invite_email",@@ -1722,6 +1724,7 @@"crates/harness_token","crates/image_generation","crates/import",+ "crates/import_notion","crates/initiative","crates/initiative_description","crates/invite_email",@@ -1889,6 +1892,7 @@"crates/harness_token","crates/image_generation","crates/import",+ "crates/import_notion","crates/initiative","crates/initiative_description","crates/invite_email",@@ -2192,7 +2196,6 @@"crates/gtm_invite","crates/harness_id","crates/harness_token",- "crates/import","crates/initiative","crates/invite_email","crates/ip_extractor",@@ -2225,8 +2228,6 @@"crates/macro_uuid","crates/maybe_send","crates/mcp_client",- "crates/mcp_select",- "crates/mcp_toolset","crates/mention_utils","crates/messages","crates/microsoft_oauth_grant_db_utils",@@ -2255,7 +2256,6 @@"crates/notification","crates/notification_state","crates/onboarding",- "crates/pipedream_mcp","crates/pptx_engine","crates/prompt","crates/properties",@@ -2989,6 +2989,7 @@"crates/harness_token","crates/image_generation","crates/import",+ "crates/import_notion","crates/initiative","crates/initiative_description","crates/invite_email",@@ -5084,6 +5085,7 @@"crates/harnesses","crates/image_generation","crates/import",+ "crates/import_notion","crates/initiative","crates/initiative_description","crates/invite_email",@@ -8772,6 +8774,7 @@"crates/activity","crates/agent","crates/agent_runtime_protocol",+ "crates/agent_session","crates/ai_billing","crates/ai_toolset","crates/ai_usage",@@ -8780,6 +8783,7 @@"crates/authentication_service_client","crates/bot_id","crates/bot_token",+ "crates/bots","crates/broadcast","crates/calendar_events","crates/call",@@ -8803,6 +8807,8 @@"crates/entity_access_db_utils","crates/entity_access_management","crates/entity_mutation",+ "crates/entity_registry/entity_registry",+ "crates/entity_registry/entity_registry_db_utils","crates/entity_registry/shared_entity_registry","crates/filter_ast","crates/folds/agent_fold",@@ -8814,6 +8820,7 @@"crates/harness_id","crates/harness_token","crates/import",+ "crates/import_notion","crates/initiative","crates/invite_email","crates/ip_extractor",@@ -8875,6 +8882,27 @@"crates/unfurl","crates/workspace-hack"],+ "import_notion": [+ "crates/ai_toolset",+ "crates/bot_id",+ "crates/bot_token",+ "crates/cowlike",+ "crates/genai_telemetry",+ "crates/harness_id",+ "crates/harness_token",+ "crates/import_notion",+ "crates/macro_auth",+ "crates/macro_authorization",+ "crates/macro_env",+ "crates/macro_env_var",+ "crates/macro_user_id",+ "crates/mcp_toolset",+ "crates/model-error-response",+ "crates/model_user",+ "crates/pipedream_mcp",+ "crates/remote_env_var",+ "crates/workspace-hack"+ ],"initiative": ["crates/activity","crates/agent",@@ -9786,6 +9814,7 @@"crates/harness_token","crates/image_generation","crates/import",+ "crates/import_notion","crates/initiative","crates/initiative_description","crates/invite_email",@@ -10629,111 +10658,32 @@"crates/notification_state"],"onboarding": [- "crates/activity","crates/agent",- "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/database_sql",- "crates/databases",- "crates/document_sub_type",- "crates/email",- "crates/email_api_client",- "crates/email_db_client",- "crates/email_utils",- "crates/entity_access",- "crates/entity_access_db_utils",- "crates/entity_access_management",- "crates/entity_mutation",- "crates/entity_registry/shared_entity_registry",- "crates/filter_ast",- "crates/folds/agent_fold",- "crates/frecency","crates/genai_telemetry","crates/generic_email_domains",- "crates/gmail_client",- "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_service_urls",- "crates/macro_tower_layers","crates/macro_user_id","crates/macro_uuid",- "crates/maybe_send","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_databases",- "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/onboarding",- "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/sqs_client",- "crates/sqs_worker",- "crates/static_file_service_client",- "crates/system_properties",- "crates/teams",- "crates/trigger_context",- "crates/unfurl","crates/workspace-hack"],"opensearch_client": [@@ -12217,6 +12167,7 @@"crates/activity","crates/agent","crates/agent_runtime_protocol",+ "crates/agent_session","crates/ai_billing","crates/ai_toolset","crates/ai_usage",@@ -12225,6 +12176,7 @@"crates/authentication_service_client","crates/bot_id","crates/bot_token",+ "crates/bots","crates/broadcast","crates/calendar_events","crates/call",@@ -12248,6 +12200,8 @@"crates/entity_access_db_utils","crates/entity_access_management","crates/entity_mutation",+ "crates/entity_registry/entity_registry",+ "crates/entity_registry/entity_registry_db_utils","crates/entity_registry/shared_entity_registry","crates/filter_ast","crates/folds/agent_fold",@@ -12259,6 +12213,7 @@"crates/harness_id","crates/harness_token","crates/import",+ "crates/import_notion","crates/initiative","crates/invite_email","crates/ip_extractor",@@ -12329,6 +12284,7 @@"crates/activity","crates/agent","crates/agent_runtime_protocol",+ "crates/agent_session","crates/ai_billing","crates/ai_toolset","crates/ai_usage",@@ -12337,6 +12293,7 @@"crates/authentication_service_client","crates/bot_id","crates/bot_token",+ "crates/bots","crates/broadcast","crates/calendar_events","crates/call",@@ -12360,6 +12317,8 @@"crates/entity_access_db_utils","crates/entity_access_management","crates/entity_mutation",+ "crates/entity_registry/entity_registry",+ "crates/entity_registry/entity_registry_db_utils","crates/entity_registry/shared_entity_registry","crates/filter_ast","crates/folds/agent_fold",@@ -12371,6 +12330,7 @@"crates/harness_id","crates/harness_token","crates/import",+ "crates/import_notion","crates/initiative","crates/invite_email","crates/ip_extractor",diff --git a/.sqlx/query-093097077c78d0b124f160bbcf1222f5b95f4b4a66622a8fcbb8c46b64fb4886.json b/.sqlx/query-093097077c78d0b124f160bbcf1222f5b95f4b4a66622a8fcbb8c46b64fb4886.jsonnew file mode 100644index 0000000000..9866a86a1e--- /dev/null+++ b/.sqlx/query-093097077c78d0b124f160bbcf1222f5b95f4b4a66622a8fcbb8c46b64fb4886.json@@ -0,0 +1,22 @@+{+ "db_name": "PostgreSQL",+ "query": "\n SELECT id FROM import_entity\n WHERE id = $1 AND status = 'importing' AND team_id IS NOT NULL\n FOR SHARE\n ",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "id",+ "type_info": "Uuid"+ }+ ],+ "parameters": {+ "Left": [+ "Uuid"+ ]+ },+ "nullable": [+ false+ ]+ },+ "hash": "093097077c78d0b124f160bbcf1222f5b95f4b4a66622a8fcbb8c46b64fb4886"+}diff --git a/.sqlx/query-093d3a1cb9d0823a742ef48849ca114cfc2a2d2c64c7758a225c08aa085332dd.json b/.sqlx/query-093d3a1cb9d0823a742ef48849ca114cfc2a2d2c64c7758a225c08aa085332dd.jsonnew file mode 100644index 0000000000..2d3d1b8a2e--- /dev/null+++ b/.sqlx/query-093d3a1cb9d0823a742ef48849ca114cfc2a2d2c64c7758a225c08aa085332dd.json@@ -0,0 +1,16 @@+{+ "db_name": "PostgreSQL",+ "query": "\n INSERT INTO import_run (user_id, source, status)\n VALUES ($1, $2, 'running')\n ON CONFLICT (user_id, source) DO UPDATE\n SET status = 'running', error = NULL, updated_at = NOW(),\n scanned_pages = NULL, scanned_data_sources = NULL,\n lease_owner = NULL, lease_expires_at = NULL, adoptions = 0,\n review_session_id = NULL\n WHERE import_run.status::text = ANY($3::text[])\n ",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Text",+ "Text",+ "TextArray"+ ]+ },+ "nullable": []+ },+ "hash": "093d3a1cb9d0823a742ef48849ca114cfc2a2d2c64c7758a225c08aa085332dd"+}diff --git a/.sqlx/query-0a41e84a695b39ddf490db802922c943f6499b14467fd7a0d0b83b04af8abf9c.json b/.sqlx/query-0a41e84a695b39ddf490db802922c943f6499b14467fd7a0d0b83b04af8abf9c.jsondeleted file mode 100644index 6d1c68b86e..0000000000--- a/.sqlx/query-0a41e84a695b39ddf490db802922c943f6499b14467fd7a0d0b83b04af8abf9c.json+++ /dev/null@@ -1,17 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "\n INSERT INTO import_run (user_id, source, status, auto_import)\n VALUES ($1, $2, 'running', $4)\n ON CONFLICT (user_id, source) DO UPDATE\n SET status = 'running', error = NULL, updated_at = NOW()\n WHERE import_run.status::text = ANY($3::text[])\n ",- "describe": {- "columns": [],- "parameters": {- "Left": [- "Text",- "Text",- "TextArray",- "Bool"- ]- },- "nullable": []- },- "hash": "0a41e84a695b39ddf490db802922c943f6499b14467fd7a0d0b83b04af8abf9c"-}diff --git a/.sqlx/query-90f114af5728d942b2ad3bf11f4adc3f5f0575df8012246175675fe0842584e9.json b/.sqlx/query-0a8ea9d2caf5d555cf0eeb7da0284d8b3ed4f7472d97dc2656fb829cb5225ec5.jsonsimilarity index 65%rename from .sqlx/query-90f114af5728d942b2ad3bf11f4adc3f5f0575df8012246175675fe0842584e9.jsonrename to .sqlx/query-0a8ea9d2caf5d555cf0eeb7da0284d8b3ed4f7472d97dc2656fb829cb5225ec5.jsonindex c5d2799508..b6eecd7347 100644--- a/.sqlx/query-90f114af5728d942b2ad3bf11f4adc3f5f0575df8012246175675fe0842584e9.json+++ b/.sqlx/query-0a8ea9d2caf5d555cf0eeb7da0284d8b3ed4f7472d97dc2656fb829cb5225ec5.json@@ -1,6 +1,6 @@{"db_name": "PostgreSQL",- "query": "\n SELECT id, user_id, team_id, source, foreign_id, status, initiator,\n metadata, entity_id, entity_type, last_error, created_at, updated_at\n FROM import_entity\n WHERE (\n user_id = $1\n OR (status = 'imported' AND team_id IS NOT NULL\n AND team_id = (SELECT team_id FROM team_user WHERE user_id = $1))\n )\n AND ($2::text IS NULL OR source = $2)\n AND ($3::text IS NULL OR status = $3)\n ORDER BY (user_id = $1) DESC, created_at DESC\n ",+ "query": "\n SELECT id, user_id, team_id, source, kind, foreign_id, parent_id, review_id, status,\n initiator, metadata, entity_id, entity_type, last_error, waiting_on, created_at, updated_at\n FROM import_entity\n WHERE user_id = $1 AND source = $2 AND foreign_id = $3\n ","describe": {"columns": [{@@ -25,46 +25,66 @@},{"ordinal": 4,- "name": "foreign_id",+ "name": "kind","type_info": "Text"},{"ordinal": 5,- "name": "status",+ "name": "foreign_id","type_info": "Text"},{"ordinal": 6,+ "name": "parent_id",+ "type_info": "Uuid"+ },+ {+ "ordinal": 7,+ "name": "review_id",+ "type_info": "Uuid"+ },+ {+ "ordinal": 8,+ "name": "status",+ "type_info": "Text"+ },+ {+ "ordinal": 9,"name": "initiator","type_info": "Text"},{- "ordinal": 7,+ "ordinal": 10,"name": "metadata","type_info": "Jsonb"},{- "ordinal": 8,+ "ordinal": 11,"name": "entity_id","type_info": "Text"},{- "ordinal": 9,+ "ordinal": 12,"name": "entity_type","type_info": "Text"},{- "ordinal": 10,+ "ordinal": 13,"name": "last_error","type_info": "Text"},{- "ordinal": 11,+ "ordinal": 14,+ "name": "waiting_on",+ "type_info": "Uuid"+ },+ {+ "ordinal": 15,"name": "created_at","type_info": "Timestamptz"},{- "ordinal": 12,+ "ordinal": 16,"name": "updated_at","type_info": "Timestamptz"}@@ -81,16 +101,20 @@false,true,false,+ true,false,+ true,+ true,false,false,false,true,true,true,+ true,false,false]},- "hash": "90f114af5728d942b2ad3bf11f4adc3f5f0575df8012246175675fe0842584e9"+ "hash": "0a8ea9d2caf5d555cf0eeb7da0284d8b3ed4f7472d97dc2656fb829cb5225ec5"}diff --git a/.sqlx/query-0f26a254b504bfb5cfb0119ad26934c125f38adde695d96aa3f30a0cdfd01f9b.json b/.sqlx/query-0f26a254b504bfb5cfb0119ad26934c125f38adde695d96aa3f30a0cdfd01f9b.jsonnew file mode 100644index 0000000000..e495ef7b15--- /dev/null+++ b/.sqlx/query-0f26a254b504bfb5cfb0119ad26934c125f38adde695d96aa3f30a0cdfd01f9b.json@@ -0,0 +1,29 @@+{+ "db_name": "PostgreSQL",+ "query": "\n SELECT foreign_id, team_id FROM import_entity\n WHERE user_id = $1 AND id = $2 AND status = 'importing'\n FOR UPDATE\n ",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "foreign_id",+ "type_info": "Text"+ },+ {+ "ordinal": 1,+ "name": "team_id",+ "type_info": "Uuid"+ }+ ],+ "parameters": {+ "Left": [+ "Text",+ "Uuid"+ ]+ },+ "nullable": [+ false,+ true+ ]+ },+ "hash": "0f26a254b504bfb5cfb0119ad26934c125f38adde695d96aa3f30a0cdfd01f9b"+}diff --git a/.sqlx/query-102b0fc31f8c2f28fa12bbb66d249b6dc743b1cf7f83cc54b8b6ea521d03c631.json b/.sqlx/query-102b0fc31f8c2f28fa12bbb66d249b6dc743b1cf7f83cc54b8b6ea521d03c631.jsonnew file mode 100644index 0000000000..6771ce7145--- /dev/null+++ b/.sqlx/query-102b0fc31f8c2f28fa12bbb66d249b6dc743b1cf7f83cc54b8b6ea521d03c631.json@@ -0,0 +1,124 @@+{+ "db_name": "PostgreSQL",+ "query": "\n INSERT INTO import_entity\n (user_id, source, kind, foreign_id, status, initiator, metadata, parent_id,\n review_id)\n VALUES ($1, $2, $3, $4, 'staged', 'chat', $5, $6, $7)\n ON CONFLICT (user_id, source, foreign_id) DO UPDATE\n SET kind = EXCLUDED.kind, metadata = EXCLUDED.metadata,\n parent_id = EXCLUDED.parent_id, status = 'staged',\n review_id = EXCLUDED.review_id, last_error = NULL, updated_at = NOW()\n WHERE import_entity.status IN ('staged', 'discarded')\n RETURNING id, user_id, team_id, source, kind, foreign_id, parent_id, review_id,\n status, initiator, metadata, entity_id, entity_type, last_error,\n waiting_on, created_at, updated_at\n ",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "id",+ "type_info": "Uuid"+ },+ {+ "ordinal": 1,+ "name": "user_id",+ "type_info": "Text"+ },+ {+ "ordinal": 2,+ "name": "team_id",+ "type_info": "Uuid"+ },+ {+ "ordinal": 3,+ "name": "source",+ "type_info": "Text"+ },+ {+ "ordinal": 4,+ "name": "kind",+ "type_info": "Text"+ },+ {+ "ordinal": 5,+ "name": "foreign_id",+ "type_info": "Text"+ },+ {+ "ordinal": 6,+ "name": "parent_id",+ "type_info": "Uuid"+ },+ {+ "ordinal": 7,+ "name": "review_id",+ "type_info": "Uuid"+ },+ {+ "ordinal": 8,+ "name": "status",+ "type_info": "Text"+ },+ {+ "ordinal": 9,+ "name": "initiator",+ "type_info": "Text"+ },+ {+ "ordinal": 10,+ "name": "metadata",+ "type_info": "Jsonb"+ },+ {+ "ordinal": 11,+ "name": "entity_id",+ "type_info": "Text"+ },+ {+ "ordinal": 12,+ "name": "entity_type",+ "type_info": "Text"+ },+ {+ "ordinal": 13,+ "name": "last_error",+ "type_info": "Text"+ },+ {+ "ordinal": 14,+ "name": "waiting_on",+ "type_info": "Uuid"+ },+ {+ "ordinal": 15,+ "name": "created_at",+ "type_info": "Timestamptz"+ },+ {+ "ordinal": 16,+ "name": "updated_at",+ "type_info": "Timestamptz"+ }+ ],+ "parameters": {+ "Left": [+ "Text",+ "Text",+ "Text",+ "Text",+ "Jsonb",+ "Uuid",+ "Uuid"+ ]+ },+ "nullable": [+ false,+ false,+ true,+ false,+ true,+ false,+ true,+ true,+ false,+ false,+ false,+ true,+ true,+ true,+ true,+ false,+ false+ ]+ },+ "hash": "102b0fc31f8c2f28fa12bbb66d249b6dc743b1cf7f83cc54b8b6ea521d03c631"+}diff --git a/.sqlx/query-15a436b5bb997d61383861968f1f4d2b6a46103cef869f8303223b5499e92c7a.json b/.sqlx/query-15a436b5bb997d61383861968f1f4d2b6a46103cef869f8303223b5499e92c7a.jsonnew file mode 100644index 0000000000..92ee5cac18--- /dev/null+++ b/.sqlx/query-15a436b5bb997d61383861968f1f4d2b6a46103cef869f8303223b5499e92c7a.json@@ -0,0 +1,15 @@+{+ "db_name": "PostgreSQL",+ "query": "\n DELETE FROM import_entity\n WHERE user_id = $1 AND source = $2 AND status = 'staged'\n AND metadata->'plan' IS NOT NULL\n ",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Text",+ "Text"+ ]+ },+ "nullable": []+ },+ "hash": "15a436b5bb997d61383861968f1f4d2b6a46103cef869f8303223b5499e92c7a"+}diff --git a/.sqlx/query-168a4a9273e26ac21e208f6f76327b879ddf76f93f411d0ae47dd792f84c2a3a.json b/.sqlx/query-168a4a9273e26ac21e208f6f76327b879ddf76f93f411d0ae47dd792f84c2a3a.jsonnew file mode 100644index 0000000000..a3390740a7--- /dev/null+++ b/.sqlx/query-168a4a9273e26ac21e208f6f76327b879ddf76f93f411d0ae47dd792f84c2a3a.json@@ -0,0 +1,15 @@+{+ "db_name": "PostgreSQL",+ "query": "\n UPDATE import_entity SET waiting_on = $2, updated_at = NOW()\n WHERE waiting_on = $1 AND status = 'importing'\n ",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Uuid",+ "Uuid"+ ]+ },+ "nullable": []+ },+ "hash": "168a4a9273e26ac21e208f6f76327b879ddf76f93f411d0ae47dd792f84c2a3a"+}diff --git a/.sqlx/query-18b28d89f92ce787c86984be5c68b5577218c7000631fdec7b70dfcf5de516a6.json b/.sqlx/query-18b28d89f92ce787c86984be5c68b5577218c7000631fdec7b70dfcf5de516a6.jsondeleted file mode 100644index 1af4dcfd7e..0000000000--- a/.sqlx/query-18b28d89f92ce787c86984be5c68b5577218c7000631fdec7b70dfcf5de516a6.json+++ /dev/null@@ -1,15 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "\n DELETE FROM import_entity\n WHERE user_id = $1 AND initiator = $2 AND status = 'staged'\n AND NOT (\n $2 = 'onboarding'\n AND EXISTS (\n SELECT 1\n FROM import_run\n WHERE import_run.user_id = import_entity.user_id\n AND import_run.source = import_entity.source\n AND import_run.auto_import\n AND import_run.status IN ('running', 'ready', 'importing', 'failed')\n )\n )\n ",- "describe": {- "columns": [],- "parameters": {- "Left": [- "Text",- "Text"- ]- },- "nullable": []- },- "hash": "18b28d89f92ce787c86984be5c68b5577218c7000631fdec7b70dfcf5de516a6"-}diff --git a/.sqlx/query-192b53847ed98ebf7e5506cf99eff776fed05fb7926d8fba33b31ff8b49fa1c3.json b/.sqlx/query-192b53847ed98ebf7e5506cf99eff776fed05fb7926d8fba33b31ff8b49fa1c3.jsondeleted file mode 100644index 9772ba66d2..0000000000--- a/.sqlx/query-192b53847ed98ebf7e5506cf99eff776fed05fb7926d8fba33b31ff8b49fa1c3.json+++ /dev/null@@ -1,46 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "\n SELECT source, status::text AS \"status!\", auto_import, error, updated_at\n FROM import_run WHERE user_id = $1\n ORDER BY source\n ",- "describe": {- "columns": [- {- "ordinal": 0,- "name": "source",- "type_info": "Text"- },- {- "ordinal": 1,- "name": "status!",- "type_info": "Text"- },- {- "ordinal": 2,- "name": "auto_import",- "type_info": "Bool"- },- {- "ordinal": 3,- "name": "error",- "type_info": "Text"- },- {- "ordinal": 4,- "name": "updated_at",- "type_info": "Timestamptz"- }- ],- "parameters": {- "Left": [- "Text"- ]- },- "nullable": [- false,- null,- false,- true,- false- ]- },- "hash": "192b53847ed98ebf7e5506cf99eff776fed05fb7926d8fba33b31ff8b49fa1c3"-}diff --git a/.sqlx/query-199c90308d4dd5a6288847f517f013fc00fdc93ca071598c7cbb2dcb9683b308.json b/.sqlx/query-199c90308d4dd5a6288847f517f013fc00fdc93ca071598c7cbb2dcb9683b308.jsonnew file mode 100644index 0000000000..36ea26464e--- /dev/null+++ b/.sqlx/query-199c90308d4dd5a6288847f517f013fc00fdc93ca071598c7cbb2dcb9683b308.json@@ -0,0 +1,56 @@+{+ "db_name": "PostgreSQL",+ "query": "\n WITH stalled AS (\n SELECT user_id FROM import_run\n WHERE source = 'notion' AND status IN ('running', 'importing')\n AND ($1::text IS NULL OR user_id = $1)\n AND (lease_expires_at < NOW()\n OR (lease_expires_at IS NULL\n AND updated_at < NOW() - ($4::double precision * interval '1 second')))\n ORDER BY updated_at\n LIMIT $5\n FOR UPDATE SKIP LOCKED\n )\n UPDATE import_run AS run\n SET lease_owner = $2,\n lease_expires_at = NOW() + ($3::double precision * interval '1 second'),\n adoptions = run.adoptions + 1,\n updated_at = NOW()\n FROM stalled\n WHERE run.user_id = stalled.user_id AND run.source = 'notion'\n RETURNING run.user_id, run.status::text AS \"status!\", run.scope, run.sharing,\n run.review_id,\n run.adoptions\n ",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "user_id",+ "type_info": "Text"+ },+ {+ "ordinal": 1,+ "name": "status!",+ "type_info": "Text"+ },+ {+ "ordinal": 2,+ "name": "scope",+ "type_info": "Jsonb"+ },+ {+ "ordinal": 3,+ "name": "sharing",+ "type_info": "Text"+ },+ {+ "ordinal": 4,+ "name": "review_id",+ "type_info": "Uuid"+ },+ {+ "ordinal": 5,+ "name": "adoptions",+ "type_info": "Int2"+ }+ ],+ "parameters": {+ "Left": [+ "Text",+ "Uuid",+ "Float8",+ "Float8",+ "Int8"+ ]+ },+ "nullable": [+ false,+ null,+ true,+ true,+ true,+ false+ ]+ },+ "hash": "199c90308d4dd5a6288847f517f013fc00fdc93ca071598c7cbb2dcb9683b308"+}diff --git a/.sqlx/query-1f632dca1f9936c1fe418219f03744955d587014e1015ff4616e05aa8491e70d.json b/.sqlx/query-1f632dca1f9936c1fe418219f03744955d587014e1015ff4616e05aa8491e70d.jsondeleted file mode 100644index a86c575272..0000000000--- a/.sqlx/query-1f632dca1f9936c1fe418219f03744955d587014e1015ff4616e05aa8491e70d.json+++ /dev/null@@ -1,17 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "\n UPDATE import_run\n SET status = $4::text::import_run_status, updated_at = NOW()\n WHERE user_id = $1 AND source = $2\n AND status::text = ANY($3::text[])\n ",- "describe": {- "columns": [],- "parameters": {- "Left": [- "Text",- "Text",- "TextArray",- "Text"- ]- },- "nullable": []- },- "hash": "1f632dca1f9936c1fe418219f03744955d587014e1015ff4616e05aa8491e70d"-}diff --git a/.sqlx/query-dbdf6afe127d8dd8f6e98f9b6ea98d9bbf3af9f9067524af4837f53af55bc593.json b/.sqlx/query-30d3dbc58a4bcfd1a5c52c2e37038da0e6470833930700ae06f176d8d449eeb3.jsonsimilarity index 62%rename from .sqlx/query-dbdf6afe127d8dd8f6e98f9b6ea98d9bbf3af9f9067524af4837f53af55bc593.jsonrename to .sqlx/query-30d3dbc58a4bcfd1a5c52c2e37038da0e6470833930700ae06f176d8d449eeb3.jsonindex 66c20cb92f..ecf80cb305 100644--- a/.sqlx/query-dbdf6afe127d8dd8f6e98f9b6ea98d9bbf3af9f9067524af4837f53af55bc593.json+++ b/.sqlx/query-30d3dbc58a4bcfd1a5c52c2e37038da0e6470833930700ae06f176d8d449eeb3.json@@ -1,6 +1,6 @@{"db_name": "PostgreSQL",- "query": "\n UPDATE import_entity\n SET status = 'staged',\n last_error = 'the import was interrupted — select it again to retry',\n updated_at = NOW()\n WHERE user_id = $1 AND status = 'importing'\n AND updated_at < NOW() - ($2::double precision * interval '1 second')\n ",+ "query": "\n UPDATE import_entity\n SET status = 'staged',\n last_error = 'the import was interrupted — select it again to retry',\n updated_at = NOW()\n WHERE user_id = $1 AND status = 'importing'\n AND NOT (source = 'notion' AND metadata->'plan' IS NOT NULL)\n AND updated_at < NOW() - ($2::double precision * interval '1 second')\n ","describe": {"columns": [],"parameters": {@@ -11,5 +11,5 @@},"nullable": []},- "hash": "dbdf6afe127d8dd8f6e98f9b6ea98d9bbf3af9f9067524af4837f53af55bc593"+ "hash": "30d3dbc58a4bcfd1a5c52c2e37038da0e6470833930700ae06f176d8d449eeb3"}diff --git a/.sqlx/query-343e49ebf372e14f2ab7fd3b7b36edf53b6c444aab2d2a618712594b1670a764.json b/.sqlx/query-343e49ebf372e14f2ab7fd3b7b36edf53b6c444aab2d2a618712594b1670a764.jsonnew file mode 100644index 0000000000..b46a428e33--- /dev/null+++ b/.sqlx/query-343e49ebf372e14f2ab7fd3b7b36edf53b6c444aab2d2a618712594b1670a764.json@@ -0,0 +1,19 @@+{+ "db_name": "PostgreSQL",+ "query": "\n UPDATE import_run\n SET lease_owner = $2,\n lease_expires_at = NOW() + ($3::double precision * interval '1 second'),\n scope = $4, review_id = $5, sharing = $6, updated_at = NOW()\n WHERE user_id = $1 AND source = 'notion' AND status IN ('running', 'importing')\n AND lease_owner IS NULL AND lease_expires_at IS NULL\n ",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Text",+ "Uuid",+ "Float8",+ "Jsonb",+ "Uuid",+ "Text"+ ]+ },+ "nullable": []+ },+ "hash": "343e49ebf372e14f2ab7fd3b7b36edf53b6c444aab2d2a618712594b1670a764"+}diff --git a/.sqlx/query-3452a0b8cbc5ff8b2a1373286a080b72d248c2ef74d27233f6ba3033b835bbbc.json b/.sqlx/query-3452a0b8cbc5ff8b2a1373286a080b72d248c2ef74d27233f6ba3033b835bbbc.jsonnew file mode 100644index 0000000000..32979aa61a--- /dev/null+++ b/.sqlx/query-3452a0b8cbc5ff8b2a1373286a080b72d248c2ef74d27233f6ba3033b835bbbc.json@@ -0,0 +1,43 @@+{+ "db_name": "PostgreSQL",+ "query": "\n UPDATE import_run\n SET status = CASE WHEN status = 'dismissed' THEN status\n ELSE $2::text::import_run_status END,\n error = CASE WHEN status = 'dismissed' THEN error ELSE $3 END,\n updated_at = NOW(), lease_owner = NULL, lease_expires_at = NULL\n WHERE user_id = $1 AND source = 'notion' AND status::text = ANY($4::text[])\n RETURNING status::text AS \"status!\", error, review_id, review_session_id\n ",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "status!",+ "type_info": "Text"+ },+ {+ "ordinal": 1,+ "name": "error",+ "type_info": "Text"+ },+ {+ "ordinal": 2,+ "name": "review_id",+ "type_info": "Uuid"+ },+ {+ "ordinal": 3,+ "name": "review_session_id",+ "type_info": "Uuid"+ }+ ],+ "parameters": {+ "Left": [+ "Text",+ "Text",+ "Text",+ "TextArray"+ ]+ },+ "nullable": [+ null,+ true,+ true,+ true+ ]+ },+ "hash": "3452a0b8cbc5ff8b2a1373286a080b72d248c2ef74d27233f6ba3033b835bbbc"+}diff --git a/.sqlx/query-060c162e3dd450c4f72b35b19b98cbf20bc65ee4f5775ea342801a25b60e5aaa.json b/.sqlx/query-3d15956b862c39a1c9b3b75aa38714e844308d503f8e95cefa3114904d43ac52.jsonsimilarity index 61%rename from .sqlx/query-060c162e3dd450c4f72b35b19b98cbf20bc65ee4f5775ea342801a25b60e5aaa.jsonrename to .sqlx/query-3d15956b862c39a1c9b3b75aa38714e844308d503f8e95cefa3114904d43ac52.jsonindex 3ef3b8416e..4a5e5e3bed 100644--- a/.sqlx/query-060c162e3dd450c4f72b35b19b98cbf20bc65ee4f5775ea342801a25b60e5aaa.json+++ b/.sqlx/query-3d15956b862c39a1c9b3b75aa38714e844308d503f8e95cefa3114904d43ac52.json@@ -1,6 +1,6 @@{"db_name": "PostgreSQL",- "query": "\n INSERT INTO import_entity (user_id, source, foreign_id, status, initiator, metadata,\n entity_id, entity_type, team_id)\n VALUES ($1, $2, $3, 'imported', $4, $5, $6, $7, $8)\n ON CONFLICT (user_id, source, foreign_id) DO UPDATE\n SET status = 'imported', metadata = EXCLUDED.metadata,\n entity_id = EXCLUDED.entity_id, entity_type = EXCLUDED.entity_type,\n team_id = EXCLUDED.team_id, last_error = NULL, updated_at = NOW()\n WHERE import_entity.status <> 'imported'\n RETURNING id, user_id, team_id, source, foreign_id, status, initiator,\n metadata, entity_id, entity_type, last_error, created_at, updated_at\n ",+ "query": "\n INSERT INTO import_entity (user_id, source, kind, foreign_id, status, initiator, metadata)\n VALUES ($1, $2, $6, $3, 'staged', $4, $5)\n ON CONFLICT (user_id, source, foreign_id) DO UPDATE\n SET metadata = EXCLUDED.metadata, updated_at = NOW()\n WHERE import_entity.status = 'staged'\n RETURNING id, user_id, team_id, source, kind, foreign_id, parent_id, review_id, status,\n initiator, metadata, entity_id, entity_type, last_error, waiting_on, created_at,\n updated_at\n ","describe": {"columns": [{@@ -25,46 +25,66 @@},{"ordinal": 4,- "name": "foreign_id",+ "name": "kind","type_info": "Text"},{"ordinal": 5,- "name": "status",+ "name": "foreign_id","type_info": "Text"},{"ordinal": 6,+ "name": "parent_id",+ "type_info": "Uuid"+ },+ {+ "ordinal": 7,+ "name": "review_id",+ "type_info": "Uuid"+ },+ {+ "ordinal": 8,+ "name": "status",+ "type_info": "Text"+ },+ {+ "ordinal": 9,"name": "initiator","type_info": "Text"},{- "ordinal": 7,+ "ordinal": 10,"name": "metadata","type_info": "Jsonb"},{- "ordinal": 8,+ "ordinal": 11,"name": "entity_id","type_info": "Text"},{- "ordinal": 9,+ "ordinal": 12,"name": "entity_type","type_info": "Text"},{- "ordinal": 10,+ "ordinal": 13,"name": "last_error","type_info": "Text"},{- "ordinal": 11,+ "ordinal": 14,+ "name": "waiting_on",+ "type_info": "Uuid"+ },+ {+ "ordinal": 15,"name": "created_at","type_info": "Timestamptz"},{- "ordinal": 12,+ "ordinal": 16,"name": "updated_at","type_info": "Timestamptz"}@@ -76,9 +96,7 @@"Text","Text","Jsonb",- "Text",- "Text",- "Uuid"+ "Text"]},"nullable": [@@ -86,16 +104,20 @@false,true,false,+ true,false,+ true,+ true,false,false,false,true,true,true,+ true,false,false]},- "hash": "060c162e3dd450c4f72b35b19b98cbf20bc65ee4f5775ea342801a25b60e5aaa"+ "hash": "3d15956b862c39a1c9b3b75aa38714e844308d503f8e95cefa3114904d43ac52"}diff --git a/.sqlx/query-44d85152c83b14c481ab790e2454a5ab5a99258257df8a0f0fe4dfad3790cf06.json b/.sqlx/query-44d85152c83b14c481ab790e2454a5ab5a99258257df8a0f0fe4dfad3790cf06.jsonnew file mode 100644index 0000000000..543b27d04e--- /dev/null+++ b/.sqlx/query-44d85152c83b14c481ab790e2454a5ab5a99258257df8a0f0fe4dfad3790cf06.json@@ -0,0 +1,23 @@+{+ "db_name": "PostgreSQL",+ "query": "\n UPDATE import_entity AS mine\n SET status = 'staged', last_error = $2, team_id = NULL, waiting_on = NULL,\n updated_at = NOW()\n WHERE mine.user_id = $1 AND mine.source = 'notion' AND mine.status = 'importing'\n AND mine.metadata->'plan' IS NOT NULL\n AND NOT EXISTS (\n SELECT 1 FROM import_run\n WHERE user_id = $1 AND source = 'notion' AND status = 'importing'\n )\n AND NOT EXISTS (\n SELECT 1 FROM import_entity AS holder\n WHERE holder.id = mine.waiting_on AND holder.status = 'importing'\n )\n RETURNING mine.id\n ",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "id",+ "type_info": "Uuid"+ }+ ],+ "parameters": {+ "Left": [+ "Text",+ "Text"+ ]+ },+ "nullable": [+ false+ ]+ },+ "hash": "44d85152c83b14c481ab790e2454a5ab5a99258257df8a0f0fe4dfad3790cf06"+}diff --git a/.sqlx/query-48167394182b4fa5b3290de7acbcc848c0f73177b0cf6d0adafbdb275d07a752.json b/.sqlx/query-48167394182b4fa5b3290de7acbcc848c0f73177b0cf6d0adafbdb275d07a752.jsondeleted file mode 100644index 7a4344669f..0000000000--- a/.sqlx/query-48167394182b4fa5b3290de7acbcc848c0f73177b0cf6d0adafbdb275d07a752.json+++ /dev/null@@ -1,16 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "\n UPDATE import_entity\n SET status = 'staged', last_error = $3, updated_at = NOW()\n WHERE user_id = $1 AND id = $2 AND status = 'importing'\n ",- "describe": {- "columns": [],- "parameters": {- "Left": [- "Text",- "Uuid",- "Text"- ]- },- "nullable": []- },- "hash": "48167394182b4fa5b3290de7acbcc848c0f73177b0cf6d0adafbdb275d07a752"-}diff --git a/.sqlx/query-4c72e8328ca3fa4f934e4788f76ece74ba01b5e636fa056854ff55f0334c6ffb.json b/.sqlx/query-4c72e8328ca3fa4f934e4788f76ece74ba01b5e636fa056854ff55f0334c6ffb.jsonnew file mode 100644index 0000000000..76915e1d6d--- /dev/null+++ b/.sqlx/query-4c72e8328ca3fa4f934e4788f76ece74ba01b5e636fa056854ff55f0334c6ffb.json@@ -0,0 +1,16 @@+{+ "db_name": "PostgreSQL",+ "query": "\n UPDATE import_entity SET waiting_on = $3, updated_at = NOW()\n WHERE user_id = $1 AND id = $2 AND status = 'importing'\n ",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Text",+ "Uuid",+ "Uuid"+ ]+ },+ "nullable": []+ },+ "hash": "4c72e8328ca3fa4f934e4788f76ece74ba01b5e636fa056854ff55f0334c6ffb"+}diff --git a/.sqlx/query-4cd2ab841670fcca8f3d4037c0c3b82c7e6b9cda5fea3fc22f4197c782b0558f.json b/.sqlx/query-4cd2ab841670fcca8f3d4037c0c3b82c7e6b9cda5fea3fc22f4197c782b0558f.jsonnew file mode 100644index 0000000000..891b0943a2--- /dev/null+++ b/.sqlx/query-4cd2ab841670fcca8f3d4037c0c3b82c7e6b9cda5fea3fc22f4197c782b0558f.json@@ -0,0 +1,15 @@+{+ "db_name": "PostgreSQL",+ "query": "\n UPDATE import_run\n SET lease_owner = NULL, lease_expires_at = NOW()\n WHERE user_id = $1 AND source = 'notion' AND lease_owner = $2\n ",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Text",+ "Uuid"+ ]+ },+ "nullable": []+ },+ "hash": "4cd2ab841670fcca8f3d4037c0c3b82c7e6b9cda5fea3fc22f4197c782b0558f"+}diff --git a/.sqlx/query-0fca1dabefce12acb121ed6ca71a63dfffc2df9ef70b51421f9adf07ed71f3ac.json b/.sqlx/query-625a6f71f314544d768b1b9cb73a41cc772efba18e986e78cc6acbab9816a11c.jsonsimilarity index 59%rename from .sqlx/query-0fca1dabefce12acb121ed6ca71a63dfffc2df9ef70b51421f9adf07ed71f3ac.jsonrename to .sqlx/query-625a6f71f314544d768b1b9cb73a41cc772efba18e986e78cc6acbab9816a11c.jsonindex 34ef982fa0..7be3917a7c 100644--- a/.sqlx/query-0fca1dabefce12acb121ed6ca71a63dfffc2df9ef70b51421f9adf07ed71f3ac.json+++ b/.sqlx/query-625a6f71f314544d768b1b9cb73a41cc772efba18e986e78cc6acbab9816a11c.json@@ -1,6 +1,6 @@{"db_name": "PostgreSQL",- "query": "\n INSERT INTO import_entity (user_id, source, foreign_id, status, initiator, metadata)\n VALUES ($1, $2, $3, 'staged', $4, $5)\n ON CONFLICT (user_id, source, foreign_id) DO UPDATE\n SET metadata = EXCLUDED.metadata, updated_at = NOW()\n WHERE import_entity.status = 'staged'\n RETURNING id, user_id, team_id, source, foreign_id, status, initiator,\n metadata, entity_id, entity_type, last_error, created_at, updated_at\n ",+ "query": "\n SELECT id, user_id, team_id, source, kind, foreign_id, parent_id, review_id, status,\n initiator, metadata, entity_id, entity_type, last_error, waiting_on, created_at,\n updated_at\n FROM import_entity\n WHERE source = 'notion' AND foreign_id = ANY($2::text[])\n AND status IN ('importing', 'imported')\n AND user_id <> $1\n AND team_id IS NOT NULL\n AND team_id = (SELECT team_id FROM team_user WHERE user_id = $1)\n ","describe": {"columns": [{@@ -25,46 +25,66 @@},{"ordinal": 4,- "name": "foreign_id",+ "name": "kind","type_info": "Text"},{"ordinal": 5,- "name": "status",+ "name": "foreign_id","type_info": "Text"},{"ordinal": 6,+ "name": "parent_id",+ "type_info": "Uuid"+ },+ {+ "ordinal": 7,+ "name": "review_id",+ "type_info": "Uuid"+ },+ {+ "ordinal": 8,+ "name": "status",+ "type_info": "Text"+ },+ {+ "ordinal": 9,"name": "initiator","type_info": "Text"},{- "ordinal": 7,+ "ordinal": 10,"name": "metadata","type_info": "Jsonb"},{- "ordinal": 8,+ "ordinal": 11,"name": "entity_id","type_info": "Text"},{- "ordinal": 9,+ "ordinal": 12,"name": "entity_type","type_info": "Text"},{- "ordinal": 10,+ "ordinal": 13,"name": "last_error","type_info": "Text"},{- "ordinal": 11,+ "ordinal": 14,+ "name": "waiting_on",+ "type_info": "Uuid"+ },+ {+ "ordinal": 15,"name": "created_at","type_info": "Timestamptz"},{- "ordinal": 12,+ "ordinal": 16,"name": "updated_at","type_info": "Timestamptz"}@@ -72,10 +92,7 @@"parameters": {"Left": ["Text",- "Text",- "Text",- "Text",- "Jsonb"+ "TextArray"]},"nullable": [@@ -83,16 +100,20 @@false,true,false,+ true,false,+ true,+ true,false,false,false,true,true,true,+ true,false,false]},- "hash": "0fca1dabefce12acb121ed6ca71a63dfffc2df9ef70b51421f9adf07ed71f3ac"+ "hash": "625a6f71f314544d768b1b9cb73a41cc772efba18e986e78cc6acbab9816a11c"}diff --git a/.sqlx/query-6335355244b0d3b14781c6983127a65b3ef3e669b837ffa239d5b8d74dc7ef8c.json b/.sqlx/query-6335355244b0d3b14781c6983127a65b3ef3e669b837ffa239d5b8d74dc7ef8c.jsondeleted file mode 100644index 31282a5aa7..0000000000--- a/.sqlx/query-6335355244b0d3b14781c6983127a65b3ef3e669b837ffa239d5b8d74dc7ef8c.json+++ /dev/null@@ -1,24 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "\n WITH outcome AS (\n SELECT\n COUNT(*) FILTER (WHERE status = 'imported')\n = CARDINALITY($3::uuid[])::bigint AS succeeded\n FROM import_entity\n WHERE user_id = $1 AND id = ANY($3::uuid[])\n )\n UPDATE import_run\n SET status = (CASE\n WHEN outcome.succeeded THEN 'completed'\n ELSE 'failed'\n END)::import_run_status,\n error = CASE\n WHEN outcome.succeeded THEN NULL\n ELSE 'one or more automatic imports failed'\n END,\n updated_at = NOW()\n FROM outcome\n WHERE user_id = $1 AND source = $2 AND status = 'importing'\n RETURNING import_run.status::text AS \"status!\"\n ",- "describe": {- "columns": [- {- "ordinal": 0,- "name": "status!",- "type_info": "Text"- }- ],- "parameters": {- "Left": [- "Text",- "Text",- "UuidArray"- ]- },- "nullable": [- null- ]- },- "hash": "6335355244b0d3b14781c6983127a65b3ef3e669b837ffa239d5b8d74dc7ef8c"-}diff --git a/.sqlx/query-6a12634cf2e318118c1ae8d87152664544a970f8d050ba87baef16e0880e8646.json b/.sqlx/query-6a12634cf2e318118c1ae8d87152664544a970f8d050ba87baef16e0880e8646.jsonnew file mode 100644index 0000000000..6fd4d6b288--- /dev/null+++ b/.sqlx/query-6a12634cf2e318118c1ae8d87152664544a970f8d050ba87baef16e0880e8646.json@@ -0,0 +1,15 @@+{+ "db_name": "PostgreSQL",+ "query": "UPDATE import_entity SET team_id = $2, updated_at = NOW() WHERE id = $1",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Uuid",+ "Uuid"+ ]+ },+ "nullable": []+ },+ "hash": "6a12634cf2e318118c1ae8d87152664544a970f8d050ba87baef16e0880e8646"+}diff --git a/.sqlx/query-6e32fde757f7c6f6b0217d8909d07cb6be1da01bc1ad99c8eae654057cc17f78.json b/.sqlx/query-6e32fde757f7c6f6b0217d8909d07cb6be1da01bc1ad99c8eae654057cc17f78.jsonnew file mode 100644index 0000000000..7c4cdc213f--- /dev/null+++ b/.sqlx/query-6e32fde757f7c6f6b0217d8909d07cb6be1da01bc1ad99c8eae654057cc17f78.json@@ -0,0 +1,16 @@+{+ "db_name": "PostgreSQL",+ "query": "\n UPDATE import_entity\n SET review_id = $3, updated_at = NOW()\n WHERE user_id = $1 AND source = $2 AND status = 'staged'\n AND metadata->'plan' IS NOT NULL AND review_id IS NULL\n ",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Text",+ "Text",+ "Uuid"+ ]+ },+ "nullable": []+ },+ "hash": "6e32fde757f7c6f6b0217d8909d07cb6be1da01bc1ad99c8eae654057cc17f78"+}diff --git a/.sqlx/query-7006b7abe5555f98383a98493c7a50910cec55ec8e5970c2323a6e58b484177d.json b/.sqlx/query-7006b7abe5555f98383a98493c7a50910cec55ec8e5970c2323a6e58b484177d.jsonnew file mode 100644index 0000000000..10023b25a1--- /dev/null+++ b/.sqlx/query-7006b7abe5555f98383a98493c7a50910cec55ec8e5970c2323a6e58b484177d.json@@ -0,0 +1,16 @@+{+ "db_name": "PostgreSQL",+ "query": "\n UPDATE import_entity\n SET status = 'imported', entity_id = $2, entity_type = $3, waiting_on = NULL,\n last_error = NULL, updated_at = NOW()\n WHERE waiting_on = $1 AND status = 'importing'\n ",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Uuid",+ "Text",+ "Text"+ ]+ },+ "nullable": []+ },+ "hash": "7006b7abe5555f98383a98493c7a50910cec55ec8e5970c2323a6e58b484177d"+}diff --git a/.sqlx/query-70fcf43dc8c7e898f3601f27ff3cffd81d39299ee35140adfe19120a165d61d2.json b/.sqlx/query-70fcf43dc8c7e898f3601f27ff3cffd81d39299ee35140adfe19120a165d61d2.jsonnew file mode 100644index 0000000000..d18bc2130c--- /dev/null+++ b/.sqlx/query-70fcf43dc8c7e898f3601f27ff3cffd81d39299ee35140adfe19120a165d61d2.json@@ -0,0 +1,48 @@+{+ "db_name": "PostgreSQL",+ "query": "\n SELECT holder.id, holder.status, holder.entity_id, holder.entity_type,\n EXISTS (\n SELECT 1 FROM import_run AS run\n WHERE run.user_id = holder.user_id AND run.source = 'notion'\n AND run.status = 'importing' AND run.lease_expires_at > NOW()\n ) AS \"live!\"\n FROM import_entity AS holder\n WHERE holder.source = 'notion' AND holder.team_id = $2 AND holder.foreign_id = $3\n AND holder.status IN ('importing', 'imported') AND holder.user_id <> $1\n FOR UPDATE OF holder\n ",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "id",+ "type_info": "Uuid"+ },+ {+ "ordinal": 1,+ "name": "status",+ "type_info": "Text"+ },+ {+ "ordinal": 2,+ "name": "entity_id",+ "type_info": "Text"+ },+ {+ "ordinal": 3,+ "name": "entity_type",+ "type_info": "Text"+ },+ {+ "ordinal": 4,+ "name": "live!",+ "type_info": "Bool"+ }+ ],+ "parameters": {+ "Left": [+ "Text",+ "Uuid",+ "Text"+ ]+ },+ "nullable": [+ false,+ false,+ true,+ true,+ null+ ]+ },+ "hash": "70fcf43dc8c7e898f3601f27ff3cffd81d39299ee35140adfe19120a165d61d2"+}diff --git a/.sqlx/query-71cfcfd36d1fba1d5f3319def96b75378b854b92c648ab3eb37f79cea1d10002.json b/.sqlx/query-71cfcfd36d1fba1d5f3319def96b75378b854b92c648ab3eb37f79cea1d10002.jsonnew file mode 100644index 0000000000..5931cdf307--- /dev/null+++ b/.sqlx/query-71cfcfd36d1fba1d5f3319def96b75378b854b92c648ab3eb37f79cea1d10002.json@@ -0,0 +1,16 @@+{+ "db_name": "PostgreSQL",+ "query": "\n UPDATE import_entity\n SET metadata = jsonb_set(metadata, '{plan,shared}', to_jsonb(id = ANY($2::uuid[]))),\n updated_at = NOW()\n WHERE user_id = $1 AND status = 'staged' AND metadata->'plan' IS NOT NULL\n AND (id = ANY($2::uuid[]) OR id = ANY($3::uuid[]))\n ",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Text",+ "UuidArray",+ "UuidArray"+ ]+ },+ "nullable": []+ },+ "hash": "71cfcfd36d1fba1d5f3319def96b75378b854b92c648ab3eb37f79cea1d10002"+}diff --git a/.sqlx/query-84e3222ba49449bc2d82c15b3d87a171837a15bf6aa4731c784c2d419309da2f.json b/.sqlx/query-84e3222ba49449bc2d82c15b3d87a171837a15bf6aa4731c784c2d419309da2f.jsondeleted file mode 100644index 6f2dfb1b36..0000000000--- a/.sqlx/query-84e3222ba49449bc2d82c15b3d87a171837a15bf6aa4731c784c2d419309da2f.json+++ /dev/null@@ -1,16 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "\n INSERT INTO import_run (user_id, source, status, auto_import)\n VALUES ($1, $2, 'running', false)\n ON CONFLICT (user_id, source) DO UPDATE\n SET status = 'running', auto_import = false, error = NULL, updated_at = NOW()\n WHERE import_run.status::text = ANY($3::text[])\n ",- "describe": {- "columns": [],- "parameters": {- "Left": [- "Text",- "Text",- "TextArray"- ]- },- "nullable": []- },- "hash": "84e3222ba49449bc2d82c15b3d87a171837a15bf6aa4731c784c2d419309da2f"-}diff --git a/.sqlx/query-8c382919183ad7f85e8f9a4ce9572eb5e9a06ffae89d6f5c5819ac5ab627c935.json b/.sqlx/query-8c382919183ad7f85e8f9a4ce9572eb5e9a06ffae89d6f5c5819ac5ab627c935.jsonnew file mode 100644index 0000000000..0e651497c5--- /dev/null+++ b/.sqlx/query-8c382919183ad7f85e8f9a4ce9572eb5e9a06ffae89d6f5c5819ac5ab627c935.json@@ -0,0 +1,14 @@+{+ "db_name": "PostgreSQL",+ "query": "UPDATE import_entity SET team_id = NULL, updated_at = NOW() WHERE id = $1",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Uuid"+ ]+ },+ "nullable": []+ },+ "hash": "8c382919183ad7f85e8f9a4ce9572eb5e9a06ffae89d6f5c5819ac5ab627c935"+}diff --git a/.sqlx/query-8e011849907decbe84eabe0e674610c9da25f8676e931edf8efbdf956deff1ba.json b/.sqlx/query-8e011849907decbe84eabe0e674610c9da25f8676e931edf8efbdf956deff1ba.jsonnew file mode 100644index 0000000000..d9d31ea86c--- /dev/null+++ b/.sqlx/query-8e011849907decbe84eabe0e674610c9da25f8676e931edf8efbdf956deff1ba.json@@ -0,0 +1,41 @@+{+ "db_name": "PostgreSQL",+ "query": "\n SELECT written.import_id, written.kind, written.object, written.macro_id\n FROM import_entity_written AS written\n JOIN import_entity AS entity ON entity.id = written.import_id\n WHERE entity.user_id = $1 AND written.import_id = ANY($2::uuid[])\n ",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "import_id",+ "type_info": "Uuid"+ },+ {+ "ordinal": 1,+ "name": "kind",+ "type_info": "Text"+ },+ {+ "ordinal": 2,+ "name": "object",+ "type_info": "Text"+ },+ {+ "ordinal": 3,+ "name": "macro_id",+ "type_info": "Uuid"+ }+ ],+ "parameters": {+ "Left": [+ "Text",+ "UuidArray"+ ]+ },+ "nullable": [+ false,+ false,+ false,+ false+ ]+ },+ "hash": "8e011849907decbe84eabe0e674610c9da25f8676e931edf8efbdf956deff1ba"+}diff --git a/.sqlx/query-621f908816dd99749329e39e1f4b895d791e5f29b573156b9cea88dcbbfcfab3.json b/.sqlx/query-8eed1c0cb0d9cbcc4848a9762c8c96bda2f4b481094d777ae8b3f3d5b10eabc0.jsonsimilarity index 62%rename from .sqlx/query-621f908816dd99749329e39e1f4b895d791e5f29b573156b9cea88dcbbfcfab3.jsonrename to .sqlx/query-8eed1c0cb0d9cbcc4848a9762c8c96bda2f4b481094d777ae8b3f3d5b10eabc0.jsonindex 72cad1ee0f..ce4f604491 100644--- a/.sqlx/query-621f908816dd99749329e39e1f4b895d791e5f29b573156b9cea88dcbbfcfab3.json+++ b/.sqlx/query-8eed1c0cb0d9cbcc4848a9762c8c96bda2f4b481094d777ae8b3f3d5b10eabc0.json@@ -1,6 +1,6 @@{"db_name": "PostgreSQL",- "query": "\n UPDATE import_entity\n SET status = 'imported', entity_id = $3, entity_type = $4, team_id = $5,\n last_error = NULL, updated_at = NOW()\n WHERE user_id = $1 AND id = $2 AND status = 'importing'\n RETURNING id, user_id, team_id, source, foreign_id, status, initiator,\n metadata, entity_id, entity_type, last_error, created_at, updated_at\n ",+ "query": "\n UPDATE import_entity\n SET status = 'imported', entity_id = $3, entity_type = $4,\n team_id = COALESCE($5, team_id), waiting_on = NULL,\n last_error = NULL, updated_at = NOW()\n WHERE user_id = $1 AND id = $2 AND status = 'importing'\n RETURNING id, user_id, team_id, source, kind, foreign_id, parent_id, review_id, status,\n initiator, metadata, entity_id, entity_type, last_error, waiting_on, created_at,\n updated_at\n ","describe": {"columns": [{@@ -25,46 +25,66 @@},{"ordinal": 4,- "name": "foreign_id",+ "name": "kind","type_info": "Text"},{"ordinal": 5,- "name": "status",+ "name": "foreign_id","type_info": "Text"},{"ordinal": 6,+ "name": "parent_id",+ "type_info": "Uuid"+ },+ {+ "ordinal": 7,+ "name": "review_id",+ "type_info": "Uuid"+ },+ {+ "ordinal": 8,+ "name": "status",+ "type_info": "Text"+ },+ {+ "ordinal": 9,"name": "initiator","type_info": "Text"},{- "ordinal": 7,+ "ordinal": 10,"name": "metadata","type_info": "Jsonb"},{- "ordinal": 8,+ "ordinal": 11,"name": "entity_id","type_info": "Text"},{- "ordinal": 9,+ "ordinal": 12,"name": "entity_type","type_info": "Text"},{- "ordinal": 10,+ "ordinal": 13,"name": "last_error","type_info": "Text"},{- "ordinal": 11,+ "ordinal": 14,+ "name": "waiting_on",+ "type_info": "Uuid"+ },+ {+ "ordinal": 15,"name": "created_at","type_info": "Timestamptz"},{- "ordinal": 12,+ "ordinal": 16,"name": "updated_at","type_info": "Timestamptz"}@@ -83,16 +103,20 @@false,true,false,+ true,false,+ true,+ true,false,false,false,true,true,true,+ true,false,false]},- "hash": "621f908816dd99749329e39e1f4b895d791e5f29b573156b9cea88dcbbfcfab3"+ "hash": "8eed1c0cb0d9cbcc4848a9762c8c96bda2f4b481094d777ae8b3f3d5b10eabc0"}diff --git a/.sqlx/query-90d114a94cf73a9961b7feb439f9768d44e230ffd1dec01305b78fe87829875b.json b/.sqlx/query-90d114a94cf73a9961b7feb439f9768d44e230ffd1dec01305b78fe87829875b.jsonnew file mode 100644index 0000000000..9d7e4072ff--- /dev/null+++ b/.sqlx/query-90d114a94cf73a9961b7feb439f9768d44e230ffd1dec01305b78fe87829875b.json@@ -0,0 +1,22 @@+{+ "db_name": "PostgreSQL",+ "query": "SELECT root_folder_id FROM import_run WHERE user_id = $1 AND source = 'notion'",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "root_folder_id",+ "type_info": "Uuid"+ }+ ],+ "parameters": {+ "Left": [+ "Text"+ ]+ },+ "nullable": [+ true+ ]+ },+ "hash": "90d114a94cf73a9961b7feb439f9768d44e230ffd1dec01305b78fe87829875b"+}diff --git a/.sqlx/query-928a3b85689abec8e96bfbdf6539f71966262848a6f3d3372dd12df60de0a1b9.json b/.sqlx/query-928a3b85689abec8e96bfbdf6539f71966262848a6f3d3372dd12df60de0a1b9.jsondeleted file mode 100644index b69e3eb5ff..0000000000--- a/.sqlx/query-928a3b85689abec8e96bfbdf6539f71966262848a6f3d3372dd12df60de0a1b9.json+++ /dev/null@@ -1,15 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "\n UPDATE import_entity\n SET status = 'discarded', updated_at = NOW()\n WHERE user_id = $1 AND initiator = $2 AND status = 'staged'\n ",- "describe": {- "columns": [],- "parameters": {- "Left": [- "Text",- "Text"- ]- },- "nullable": []- },- "hash": "928a3b85689abec8e96bfbdf6539f71966262848a6f3d3372dd12df60de0a1b9"-}diff --git a/.sqlx/query-93bce01c816527dc7babad15fbcc9823387a1db6b82016bd3a7640e48c6cc542.json b/.sqlx/query-93bce01c816527dc7babad15fbcc9823387a1db6b82016bd3a7640e48c6cc542.jsonnew file mode 100644index 0000000000..83421b9e72--- /dev/null+++ b/.sqlx/query-93bce01c816527dc7babad15fbcc9823387a1db6b82016bd3a7640e48c6cc542.json@@ -0,0 +1,58 @@+{+ "db_name": "PostgreSQL",+ "query": "\n SELECT source, status::text AS \"status!\", error, updated_at,\n CASE WHEN status = 'running' THEN scanned_pages END AS scanned_pages,\n CASE WHEN status = 'running' THEN scanned_data_sources END\n AS scanned_data_sources,\n review_id\n FROM import_run WHERE user_id = $1\n ORDER BY source\n ",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "source",+ "type_info": "Text"+ },+ {+ "ordinal": 1,+ "name": "status!",+ "type_info": "Text"+ },+ {+ "ordinal": 2,+ "name": "error",+ "type_info": "Text"+ },+ {+ "ordinal": 3,+ "name": "updated_at",+ "type_info": "Timestamptz"+ },+ {+ "ordinal": 4,+ "name": "scanned_pages",+ "type_info": "Int4"+ },+ {+ "ordinal": 5,+ "name": "scanned_data_sources",+ "type_info": "Int4"+ },+ {+ "ordinal": 6,+ "name": "review_id",+ "type_info": "Uuid"+ }+ ],+ "parameters": {+ "Left": [+ "Text"+ ]+ },+ "nullable": [+ false,+ null,+ true,+ false,+ null,+ null,+ true+ ]+ },+ "hash": "93bce01c816527dc7babad15fbcc9823387a1db6b82016bd3a7640e48c6cc542"+}diff --git a/.sqlx/query-977c64b15ccd01c07d53c8d006f3cd9fae920259500706151d075ef0f9b2dce8.json b/.sqlx/query-977c64b15ccd01c07d53c8d006f3cd9fae920259500706151d075ef0f9b2dce8.jsonnew file mode 100644index 0000000000..cb124b42ac--- /dev/null+++ b/.sqlx/query-977c64b15ccd01c07d53c8d006f3cd9fae920259500706151d075ef0f9b2dce8.json@@ -0,0 +1,24 @@+{+ "db_name": "PostgreSQL",+ "query": "\n SELECT id FROM import_entity\n WHERE user_id = $1 AND source = $2 AND foreign_id = $3\n ",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "id",+ "type_info": "Uuid"+ }+ ],+ "parameters": {+ "Left": [+ "Text",+ "Text",+ "Text"+ ]+ },+ "nullable": [+ false+ ]+ },+ "hash": "977c64b15ccd01c07d53c8d006f3cd9fae920259500706151d075ef0f9b2dce8"+}diff --git a/.sqlx/query-99a97f3fc8c49d8e045c22aa724896e30afe473662ff91dd2f6e5af1a3c2e26b.json b/.sqlx/query-99a97f3fc8c49d8e045c22aa724896e30afe473662ff91dd2f6e5af1a3c2e26b.jsonnew file mode 100644index 0000000000..1785182d87--- /dev/null+++ b/.sqlx/query-99a97f3fc8c49d8e045c22aa724896e30afe473662ff91dd2f6e5af1a3c2e26b.json@@ -0,0 +1,18 @@+{+ "db_name": "PostgreSQL",+ "query": "\n UPDATE import_run\n SET status = $3::text::import_run_status, error = $4, updated_at = NOW(),\n lease_owner = NULL, lease_expires_at = NULL\n WHERE user_id = $1 AND source = $2 AND status::text = ANY($5::text[])\n ",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Text",+ "Text",+ "Text",+ "Text",+ "TextArray"+ ]+ },+ "nullable": []+ },+ "hash": "99a97f3fc8c49d8e045c22aa724896e30afe473662ff91dd2f6e5af1a3c2e26b"+}diff --git a/.sqlx/query-c0459fd55d7b0f642eb71773dc19deca937df172ead20864de84998cb280d8a3.json b/.sqlx/query-9d57ea07fdaee19d0f9eabc3b2e566c62c0848d4ea53d4e4fe535de9cb447563.jsonsimilarity index 65%rename from .sqlx/query-c0459fd55d7b0f642eb71773dc19deca937df172ead20864de84998cb280d8a3.jsonrename to .sqlx/query-9d57ea07fdaee19d0f9eabc3b2e566c62c0848d4ea53d4e4fe535de9cb447563.jsonindex 00ab44a4d4..4763e63020 100644--- a/.sqlx/query-c0459fd55d7b0f642eb71773dc19deca937df172ead20864de84998cb280d8a3.json+++ b/.sqlx/query-9d57ea07fdaee19d0f9eabc3b2e566c62c0848d4ea53d4e4fe535de9cb447563.json@@ -1,6 +1,6 @@{"db_name": "PostgreSQL",- "query": "\n SELECT id, user_id, team_id, source, foreign_id, status, initiator,\n metadata, entity_id, entity_type, last_error, created_at, updated_at\n FROM import_entity\n WHERE user_id = $1 AND id = $2\n ",+ "query": "\n SELECT id, user_id, team_id, source, kind, foreign_id, parent_id, review_id, status,\n initiator, metadata, entity_id, entity_type, last_error, waiting_on, created_at, updated_at\n FROM import_entity\n WHERE user_id = $1 AND id = $2\n ","describe": {"columns": [{@@ -25,46 +25,66 @@},{"ordinal": 4,- "name": "foreign_id",+ "name": "kind","type_info": "Text"},{"ordinal": 5,- "name": "status",+ "name": "foreign_id","type_info": "Text"},{"ordinal": 6,+ "name": "parent_id",+ "type_info": "Uuid"+ },+ {+ "ordinal": 7,+ "name": "review_id",+ "type_info": "Uuid"+ },+ {+ "ordinal": 8,+ "name": "status",+ "type_info": "Text"+ },+ {+ "ordinal": 9,"name": "initiator","type_info": "Text"},{- "ordinal": 7,+ "ordinal": 10,"name": "metadata","type_info": "Jsonb"},{- "ordinal": 8,+ "ordinal": 11,"name": "entity_id","type_info": "Text"},{- "ordinal": 9,+ "ordinal": 12,"name": "entity_type","type_info": "Text"},{- "ordinal": 10,+ "ordinal": 13,"name": "last_error","type_info": "Text"},{- "ordinal": 11,+ "ordinal": 14,+ "name": "waiting_on",+ "type_info": "Uuid"+ },+ {+ "ordinal": 15,"name": "created_at","type_info": "Timestamptz"},{- "ordinal": 12,+ "ordinal": 16,"name": "updated_at","type_info": "Timestamptz"}@@ -80,16 +100,20 @@false,true,false,+ true,false,+ true,+ true,false,false,false,true,true,true,+ true,false,false]},- "hash": "c0459fd55d7b0f642eb71773dc19deca937df172ead20864de84998cb280d8a3"+ "hash": "9d57ea07fdaee19d0f9eabc3b2e566c62c0848d4ea53d4e4fe535de9cb447563"}diff --git a/.sqlx/query-a2bd28bc1ef78191532d9df2f61576a872c592dd2d9db2c6075c6e12e180a993.json b/.sqlx/query-a2bd28bc1ef78191532d9df2f61576a872c592dd2d9db2c6075c6e12e180a993.jsonnew file mode 100644index 0000000000..7403725f27--- /dev/null+++ b/.sqlx/query-a2bd28bc1ef78191532d9df2f61576a872c592dd2d9db2c6075c6e12e180a993.json@@ -0,0 +1,121 @@+{+ "db_name": "PostgreSQL",+ "query": "\n UPDATE import_entity AS mine\n SET status = 'importing', last_error = NULL, waiting_on = NULL, updated_at = NOW(),\n team_id = CASE\n WHEN $4::uuid IS NOT NULL AND mine.id = ANY($3::uuid[]) AND NOT EXISTS (\n SELECT 1 FROM import_entity AS other\n WHERE other.source = mine.source AND other.team_id = $4\n AND other.foreign_id = mine.foreign_id\n AND other.status IN ('importing', 'imported')\n AND other.id <> mine.id\n ) THEN $4\n ELSE NULL\n END\n WHERE mine.user_id = $1 AND mine.id = ANY($2::uuid[]) AND mine.status = 'staged'\n RETURNING mine.id, mine.user_id, mine.team_id, mine.source, mine.kind,\n mine.foreign_id, mine.parent_id, mine.review_id, mine.status,\n mine.initiator, mine.metadata, mine.entity_id, mine.entity_type,\n mine.last_error, mine.waiting_on, mine.created_at, mine.updated_at\n ",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "id",+ "type_info": "Uuid"+ },+ {+ "ordinal": 1,+ "name": "user_id",+ "type_info": "Text"+ },+ {+ "ordinal": 2,+ "name": "team_id",+ "type_info": "Uuid"+ },+ {+ "ordinal": 3,+ "name": "source",+ "type_info": "Text"+ },+ {+ "ordinal": 4,+ "name": "kind",+ "type_info": "Text"+ },+ {+ "ordinal": 5,+ "name": "foreign_id",+ "type_info": "Text"+ },+ {+ "ordinal": 6,+ "name": "parent_id",+ "type_info": "Uuid"+ },+ {+ "ordinal": 7,+ "name": "review_id",+ "type_info": "Uuid"+ },+ {+ "ordinal": 8,+ "name": "status",+ "type_info": "Text"+ },+ {+ "ordinal": 9,+ "name": "initiator",+ "type_info": "Text"+ },+ {+ "ordinal": 10,+ "name": "metadata",+ "type_info": "Jsonb"+ },+ {+ "ordinal": 11,+ "name": "entity_id",+ "type_info": "Text"+ },+ {+ "ordinal": 12,+ "name": "entity_type",+ "type_info": "Text"+ },+ {+ "ordinal": 13,+ "name": "last_error",+ "type_info": "Text"+ },+ {+ "ordinal": 14,+ "name": "waiting_on",+ "type_info": "Uuid"+ },+ {+ "ordinal": 15,+ "name": "created_at",+ "type_info": "Timestamptz"+ },+ {+ "ordinal": 16,+ "name": "updated_at",+ "type_info": "Timestamptz"+ }+ ],+ "parameters": {+ "Left": [+ "Text",+ "UuidArray",+ "UuidArray",+ "Uuid"+ ]+ },+ "nullable": [+ false,+ false,+ true,+ false,+ true,+ false,+ true,+ true,+ false,+ false,+ false,+ true,+ true,+ true,+ true,+ false,+ false+ ]+ },+ "hash": "a2bd28bc1ef78191532d9df2f61576a872c592dd2d9db2c6075c6e12e180a993"+}diff --git a/.sqlx/query-a4919a7db5b008116c78b12f00d5910a39acdb5a57e0cfa0a0e5c0c8f21c38aa.json b/.sqlx/query-a4919a7db5b008116c78b12f00d5910a39acdb5a57e0cfa0a0e5c0c8f21c38aa.jsonnew file mode 100644index 0000000000..0e82c39838--- /dev/null+++ b/.sqlx/query-a4919a7db5b008116c78b12f00d5910a39acdb5a57e0cfa0a0e5c0c8f21c38aa.json@@ -0,0 +1,15 @@+{+ "db_name": "PostgreSQL",+ "query": "\n UPDATE import_entity\n SET status = 'staged', waiting_on = NULL, last_error = $2, updated_at = NOW()\n WHERE waiting_on = ANY($1::uuid[]) AND status = 'importing'\n ",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "UuidArray",+ "Text"+ ]+ },+ "nullable": []+ },+ "hash": "a4919a7db5b008116c78b12f00d5910a39acdb5a57e0cfa0a0e5c0c8f21c38aa"+}diff --git a/.sqlx/query-a53c213e1d4304506ece3fb431c15a14575a6c9eee0ff348f4b01eee0eacd2be.json b/.sqlx/query-a53c213e1d4304506ece3fb431c15a14575a6c9eee0ff348f4b01eee0eacd2be.jsondeleted file mode 100644index 56be6b6bd9..0000000000--- a/.sqlx/query-a53c213e1d4304506ece3fb431c15a14575a6c9eee0ff348f4b01eee0eacd2be.json+++ /dev/null@@ -1,23 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "\n UPDATE import_run\n SET status = 'importing', error = NULL, updated_at = NOW()\n WHERE user_id = $1 AND source = $2\n AND status = 'ready' AND auto_import\n RETURNING source\n ",- "describe": {- "columns": [- {- "ordinal": 0,- "name": "source",- "type_info": "Text"- }- ],- "parameters": {- "Left": [- "Text",- "Text"- ]- },- "nullable": [- false- ]- },- "hash": "a53c213e1d4304506ece3fb431c15a14575a6c9eee0ff348f4b01eee0eacd2be"-}diff --git a/.sqlx/query-b0621b4c8c02e424eb4ae0ab0c1145ca4debea3e22e1c892bad017c9322579ae.json b/.sqlx/query-b0621b4c8c02e424eb4ae0ab0c1145ca4debea3e22e1c892bad017c9322579ae.jsonnew file mode 100644index 0000000000..4c465fd14e--- /dev/null+++ b/.sqlx/query-b0621b4c8c02e424eb4ae0ab0c1145ca4debea3e22e1c892bad017c9322579ae.json@@ -0,0 +1,16 @@+{+ "db_name": "PostgreSQL",+ "query": "\n UPDATE import_run\n SET lease_expires_at = NOW() + ($3::double precision * interval '1 second'),\n updated_at = NOW()\n WHERE user_id = $1 AND source = 'notion' AND status IN ('running', 'importing')\n AND lease_owner = $2\n ",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Text",+ "Uuid",+ "Float8"+ ]+ },+ "nullable": []+ },+ "hash": "b0621b4c8c02e424eb4ae0ab0c1145ca4debea3e22e1c892bad017c9322579ae"+}diff --git a/.sqlx/query-b4e5da0493950b2287b5a485eb5aeb432a9224220af852809741ec5b2a650fec.json b/.sqlx/query-b4e5da0493950b2287b5a485eb5aeb432a9224220af852809741ec5b2a650fec.jsonnew file mode 100644index 0000000000..46e7c2956a--- /dev/null+++ b/.sqlx/query-b4e5da0493950b2287b5a485eb5aeb432a9224220af852809741ec5b2a650fec.json@@ -0,0 +1,44 @@+{+ "db_name": "PostgreSQL",+ "query": "\n WITH input AS (\n SELECT * FROM UNNEST($2::uuid[], $3::text[], $4::text[], $5::uuid[])\n AS input(import_id, kind, object, macro_id)\n ),\n inserted AS (\n INSERT INTO import_entity_written (import_id, kind, object, macro_id)\n SELECT input.import_id, input.kind, input.object, input.macro_id\n FROM input\n JOIN import_entity AS entity ON entity.id = input.import_id AND entity.user_id = $1\n ON CONFLICT (import_id, kind, object) DO NOTHING\n RETURNING import_id, kind, object, macro_id\n )\n SELECT import_id AS \"import_id!\", kind AS \"kind!\", object AS \"object!\",\n macro_id AS \"macro_id!\"\n FROM inserted\n UNION ALL\n SELECT written.import_id, written.kind, written.object, written.macro_id\n FROM import_entity_written AS written\n JOIN input USING (import_id, kind, object)\n JOIN import_entity AS entity ON entity.id = written.import_id AND entity.user_id = $1\n ",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "import_id!",+ "type_info": "Uuid"+ },+ {+ "ordinal": 1,+ "name": "kind!",+ "type_info": "Text"+ },+ {+ "ordinal": 2,+ "name": "object!",+ "type_info": "Text"+ },+ {+ "ordinal": 3,+ "name": "macro_id!",+ "type_info": "Uuid"+ }+ ],+ "parameters": {+ "Left": [+ "Text",+ "UuidArray",+ "TextArray",+ "TextArray",+ "UuidArray"+ ]+ },+ "nullable": [+ null,+ null,+ null,+ null+ ]+ },+ "hash": "b4e5da0493950b2287b5a485eb5aeb432a9224220af852809741ec5b2a650fec"+}diff --git a/.sqlx/query-ba22ddfde9ce9dd4cbbe6fded2652d7ca1665d2919c07a1d7f37624fe3d2584d.json b/.sqlx/query-ba22ddfde9ce9dd4cbbe6fded2652d7ca1665d2919c07a1d7f37624fe3d2584d.jsonnew file mode 100644index 0000000000..4d6d64853e--- /dev/null+++ b/.sqlx/query-ba22ddfde9ce9dd4cbbe6fded2652d7ca1665d2919c07a1d7f37624fe3d2584d.json@@ -0,0 +1,17 @@+{+ "db_name": "PostgreSQL",+ "query": "\n UPDATE import_entity AS entity\n SET metadata = decided.metadata, updated_at = NOW()\n FROM UNNEST($3::text[], $4::jsonb[]) AS decided(foreign_id, metadata)\n WHERE entity.user_id = $1 AND entity.source = $2\n AND entity.foreign_id = decided.foreign_id\n AND entity.status = 'staged' AND entity.metadata->'plan' IS NOT NULL\n ",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Text",+ "Text",+ "TextArray",+ "JsonbArray"+ ]+ },+ "nullable": []+ },+ "hash": "ba22ddfde9ce9dd4cbbe6fded2652d7ca1665d2919c07a1d7f37624fe3d2584d"+}diff --git a/.sqlx/query-c00ffb94c8b6b3c21a28360c1e7d5026d39f79645826bcb2e8136d530ebc358b.json b/.sqlx/query-c00ffb94c8b6b3c21a28360c1e7d5026d39f79645826bcb2e8136d530ebc358b.jsondeleted file mode 100644index 430fd63cfa..0000000000--- a/.sqlx/query-c00ffb94c8b6b3c21a28360c1e7d5026d39f79645826bcb2e8136d530ebc358b.json+++ /dev/null@@ -1,14 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "\n WITH settled AS (\n SELECT\n run.source,\n EXISTS (\n SELECT 1\n FROM import_entity entity\n WHERE entity.user_id = run.user_id\n AND entity.source = run.source\n AND entity.initiator = 'onboarding'\n AND entity.status = 'staged'\n AND entity.last_error IS NOT NULL\n ) AS failed\n FROM import_run run\n WHERE run.user_id = $1\n AND run.status = 'importing'\n AND run.auto_import\n AND NOT EXISTS (\n SELECT 1\n FROM import_entity entity\n WHERE entity.user_id = run.user_id\n AND entity.source = run.source\n AND entity.initiator = 'onboarding'\n AND entity.status = 'importing'\n )\n )\n UPDATE import_run run\n SET status = (\n CASE WHEN settled.failed THEN 'failed' ELSE 'completed' END\n )::import_run_status,\n error = CASE\n WHEN settled.failed THEN 'one or more automatic imports failed'\n ELSE NULL\n END,\n updated_at = NOW()\n FROM settled\n WHERE run.user_id = $1\n AND run.source = settled.source\n AND run.status = 'importing'\n ",- "describe": {- "columns": [],- "parameters": {- "Left": [- "Text"- ]- },- "nullable": []- },- "hash": "c00ffb94c8b6b3c21a28360c1e7d5026d39f79645826bcb2e8136d530ebc358b"-}diff --git a/.sqlx/query-c0fe1c613874dab111b153260fb8aaf5e66456ad93083622eb5a4cc54fb6fa05.json b/.sqlx/query-c0fe1c613874dab111b153260fb8aaf5e66456ad93083622eb5a4cc54fb6fa05.jsonnew file mode 100644index 0000000000..112a7ff692--- /dev/null+++ b/.sqlx/query-c0fe1c613874dab111b153260fb8aaf5e66456ad93083622eb5a4cc54fb6fa05.json@@ -0,0 +1,16 @@+{+ "db_name": "PostgreSQL",+ "query": "\n UPDATE import_entity\n SET status = 'staged', last_error = $3, team_id = NULL, waiting_on = NULL,\n updated_at = NOW()\n WHERE user_id = $1 AND id = $2 AND status = 'importing'\n ",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Text",+ "Uuid",+ "Text"+ ]+ },+ "nullable": []+ },+ "hash": "c0fe1c613874dab111b153260fb8aaf5e66456ad93083622eb5a4cc54fb6fa05"+}diff --git a/.sqlx/query-166dcdc916a1f95150cf1254f078ac08b9eafbfaa1f3cca4b68127ea2baeb05b.json b/.sqlx/query-ca4e4fd8600fb284c1566906a7f5ff46b1d53b9e79ae435bacda1764026a3fbd.jsonsimilarity index 58%rename from .sqlx/query-166dcdc916a1f95150cf1254f078ac08b9eafbfaa1f3cca4b68127ea2baeb05b.jsonrename to .sqlx/query-ca4e4fd8600fb284c1566906a7f5ff46b1d53b9e79ae435bacda1764026a3fbd.jsonindex 678d913d7d..63d4bae16a 100644--- a/.sqlx/query-166dcdc916a1f95150cf1254f078ac08b9eafbfaa1f3cca4b68127ea2baeb05b.json+++ b/.sqlx/query-ca4e4fd8600fb284c1566906a7f5ff46b1d53b9e79ae435bacda1764026a3fbd.json@@ -1,6 +1,6 @@{"db_name": "PostgreSQL",- "query": "\n SELECT id, user_id, team_id, source, foreign_id, status, initiator,\n metadata, entity_id, entity_type, last_error, created_at, updated_at\n FROM import_entity\n WHERE user_id = $1 AND source = $2 AND foreign_id = $3\n ",+ "query": "\n SELECT id, user_id, team_id, source, kind, foreign_id, parent_id, review_id, status,\n initiator, metadata, entity_id, entity_type, last_error, waiting_on, created_at, updated_at\n FROM import_entity\n WHERE (\n user_id = $1\n OR (status = 'imported' AND team_id IS NOT NULL\n AND team_id = (SELECT team_id FROM team_user WHERE user_id = $1))\n )\n AND ($2::text IS NULL OR source = $2)\n AND ($3::text IS NULL OR status = $3)\n ORDER BY (user_id = $1) DESC, created_at DESC\n ","describe": {"columns": [{@@ -25,46 +25,66 @@},{"ordinal": 4,- "name": "foreign_id",+ "name": "kind","type_info": "Text"},{"ordinal": 5,- "name": "status",+ "name": "foreign_id","type_info": "Text"},{"ordinal": 6,+ "name": "parent_id",+ "type_info": "Uuid"+ },+ {+ "ordinal": 7,+ "name": "review_id",+ "type_info": "Uuid"+ },+ {+ "ordinal": 8,+ "name": "status",+ "type_info": "Text"+ },+ {+ "ordinal": 9,"name": "initiator","type_info": "Text"},{- "ordinal": 7,+ "ordinal": 10,"name": "metadata","type_info": "Jsonb"},{- "ordinal": 8,+ "ordinal": 11,"name": "entity_id","type_info": "Text"},{- "ordinal": 9,+ "ordinal": 12,"name": "entity_type","type_info": "Text"},{- "ordinal": 10,+ "ordinal": 13,"name": "last_error","type_info": "Text"},{- "ordinal": 11,+ "ordinal": 14,+ "name": "waiting_on",+ "type_info": "Uuid"+ },+ {+ "ordinal": 15,"name": "created_at","type_info": "Timestamptz"},{- "ordinal": 12,+ "ordinal": 16,"name": "updated_at","type_info": "Timestamptz"}@@ -81,16 +101,20 @@false,true,false,+ true,false,+ true,+ true,false,false,false,true,true,true,+ true,false,false]},- "hash": "166dcdc916a1f95150cf1254f078ac08b9eafbfaa1f3cca4b68127ea2baeb05b"+ "hash": "ca4e4fd8600fb284c1566906a7f5ff46b1d53b9e79ae435bacda1764026a3fbd"}diff --git a/.sqlx/query-a232d6e9add612435b2aa53371d3f6bf8698340072f9a181b462a7c4669dc9dc.json b/.sqlx/query-d08131d609eb600f1e662246ccf63985f7380e6363af866178afb96c679bd6cb.jsonsimilarity index 72%rename from .sqlx/query-a232d6e9add612435b2aa53371d3f6bf8698340072f9a181b462a7c4669dc9dc.jsonrename to .sqlx/query-d08131d609eb600f1e662246ccf63985f7380e6363af866178afb96c679bd6cb.jsonindex 497b06b82b..d67d6e4d52 100644--- a/.sqlx/query-a232d6e9add612435b2aa53371d3f6bf8698340072f9a181b462a7c4669dc9dc.json+++ b/.sqlx/query-d08131d609eb600f1e662246ccf63985f7380e6363af866178afb96c679bd6cb.json@@ -1,6 +1,6 @@{"db_name": "PostgreSQL",- "query": "\n UPDATE import_entity\n SET status = 'importing', last_error = NULL, updated_at = NOW()\n WHERE user_id = $1 AND id = ANY($2::uuid[]) AND status = 'staged'\n RETURNING id, user_id, team_id, source, foreign_id, status, initiator,\n metadata, entity_id, entity_type, last_error, created_at, updated_at\n ",+ "query": "\n UPDATE import_entity\n SET status = 'importing', last_error = NULL, updated_at = NOW()\n WHERE user_id = $1 AND id = ANY($2::uuid[]) AND status = 'staged'\n RETURNING id, user_id, team_id, source, kind, foreign_id, parent_id, review_id, status,\n initiator, metadata, entity_id, entity_type, last_error, waiting_on, created_at,\n updated_at\n ","describe": {"columns": [{@@ -25,46 +25,66 @@},{"ordinal": 4,- "name": "foreign_id",+ "name": "kind","type_info": "Text"},{"ordinal": 5,- "name": "status",+ "name": "foreign_id","type_info": "Text"},{"ordinal": 6,+ "name": "parent_id",+ "type_info": "Uuid"+ },+ {+ "ordinal": 7,+ "name": "review_id",+ "type_info": "Uuid"+ },+ {+ "ordinal": 8,+ "name": "status",+ "type_info": "Text"+ },+ {+ "ordinal": 9,"name": "initiator","type_info": "Text"},{- "ordinal": 7,+ "ordinal": 10,"name": "metadata","type_info": "Jsonb"},{- "ordinal": 8,+ "ordinal": 11,"name": "entity_id","type_info": "Text"},{- "ordinal": 9,+ "ordinal": 12,"name": "entity_type","type_info": "Text"},{- "ordinal": 10,+ "ordinal": 13,"name": "last_error","type_info": "Text"},{- "ordinal": 11,+ "ordinal": 14,+ "name": "waiting_on",+ "type_info": "Uuid"+ },+ {+ "ordinal": 15,"name": "created_at","type_info": "Timestamptz"},{- "ordinal": 12,+ "ordinal": 16,"name": "updated_at","type_info": "Timestamptz"}@@ -80,16 +100,20 @@false,true,false,+ true,false,+ true,+ true,false,false,false,true,true,true,+ true,false,false]},- "hash": "a232d6e9add612435b2aa53371d3f6bf8698340072f9a181b462a7c4669dc9dc"+ "hash": "d08131d609eb600f1e662246ccf63985f7380e6363af866178afb96c679bd6cb"}diff --git a/.sqlx/query-e529e3d4409b2205dcf28984af197d715c9f713b5ae43b6e7de87d5f54c3b852.json b/.sqlx/query-e529e3d4409b2205dcf28984af197d715c9f713b5ae43b6e7de87d5f54c3b852.jsonnew file mode 100644index 0000000000..e11ca150a9--- /dev/null+++ b/.sqlx/query-e529e3d4409b2205dcf28984af197d715c9f713b5ae43b6e7de87d5f54c3b852.json@@ -0,0 +1,15 @@+{+ "db_name": "PostgreSQL",+ "query": "\n UPDATE import_run\n SET status = 'failed',\n error = 'discovery was interrupted — try again',\n updated_at = NOW()\n WHERE user_id = $1 AND status IN ('running', 'importing') AND source <> 'notion'\n AND updated_at < NOW() - ($2::double precision * interval '1 second')\n ",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Text",+ "Float8"+ ]+ },+ "nullable": []+ },+ "hash": "e529e3d4409b2205dcf28984af197d715c9f713b5ae43b6e7de87d5f54c3b852"+}diff --git a/.sqlx/query-e6c0457cac880c2e1fd2d152a747420a08ecf98bbcae6e46c39973ce9f5baeb6.json b/.sqlx/query-e6c0457cac880c2e1fd2d152a747420a08ecf98bbcae6e46c39973ce9f5baeb6.jsonnew file mode 100644index 0000000000..c13741177b--- /dev/null+++ b/.sqlx/query-e6c0457cac880c2e1fd2d152a747420a08ecf98bbcae6e46c39973ce9f5baeb6.json@@ -0,0 +1,17 @@+{+ "db_name": "PostgreSQL",+ "query": "\n UPDATE import_run\n SET scanned_pages = $3, scanned_data_sources = $4\n WHERE user_id = $1 AND source = $2 AND status = 'running'\n ",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Text",+ "Text",+ "Int4",+ "Int4"+ ]+ },+ "nullable": []+ },+ "hash": "e6c0457cac880c2e1fd2d152a747420a08ecf98bbcae6e46c39973ce9f5baeb6"+}diff --git a/.sqlx/query-eb6c30961698d4ab7c9c0d77db32dc166551575fd6bc51567cca0242b06ff0e2.json b/.sqlx/query-eb6c30961698d4ab7c9c0d77db32dc166551575fd6bc51567cca0242b06ff0e2.jsonnew file mode 100644index 0000000000..b4e6aac58f--- /dev/null+++ b/.sqlx/query-eb6c30961698d4ab7c9c0d77db32dc166551575fd6bc51567cca0242b06ff0e2.json@@ -0,0 +1,23 @@+{+ "db_name": "PostgreSQL",+ "query": "\n SELECT review_id FROM import_run\n WHERE user_id = $1 AND source = $2 AND status = 'running'\n FOR UPDATE\n ",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "review_id",+ "type_info": "Uuid"+ }+ ],+ "parameters": {+ "Left": [+ "Text",+ "Text"+ ]+ },+ "nullable": [+ true+ ]+ },+ "hash": "eb6c30961698d4ab7c9c0d77db32dc166551575fd6bc51567cca0242b06ff0e2"+}diff --git a/.sqlx/query-eba1acf141a51c1e9ba85101b866d8207933d3e393bad2a7bad7b6bd453b7709.json b/.sqlx/query-eba1acf141a51c1e9ba85101b866d8207933d3e393bad2a7bad7b6bd453b7709.jsonnew file mode 100644index 0000000000..a6f49cead7--- /dev/null+++ b/.sqlx/query-eba1acf141a51c1e9ba85101b866d8207933d3e393bad2a7bad7b6bd453b7709.json@@ -0,0 +1,22 @@+{+ "db_name": "PostgreSQL",+ "query": "SELECT record AS \"record: Json<ReviewRecord>\" FROM agent_session_review WHERE id = $1",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "record: Json<ReviewRecord>",+ "type_info": "Jsonb"+ }+ ],+ "parameters": {+ "Left": [+ "Uuid"+ ]+ },+ "nullable": [+ false+ ]+ },+ "hash": "eba1acf141a51c1e9ba85101b866d8207933d3e393bad2a7bad7b6bd453b7709"+}diff --git a/.sqlx/query-ed041c2746913e1250e2b14c9c1037e60b292e39c679b6461cbb9330a62d69cd.json b/.sqlx/query-ed041c2746913e1250e2b14c9c1037e60b292e39c679b6461cbb9330a62d69cd.jsonnew file mode 100644index 0000000000..05483d299f--- /dev/null+++ b/.sqlx/query-ed041c2746913e1250e2b14c9c1037e60b292e39c679b6461cbb9330a62d69cd.json@@ -0,0 +1,17 @@+{+ "db_name": "PostgreSQL",+ "query": "\n UPDATE import_run\n SET review_id = CASE WHEN status = 'running' THEN $3 ELSE review_id END,\n review_session_id = CASE WHEN status = 'running' THEN $4\n ELSE COALESCE(review_session_id, $4) END\n WHERE user_id = $1 AND source = $2 AND status IN ('running', 'ready')\n ",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Text",+ "Text",+ "Uuid",+ "Uuid"+ ]+ },+ "nullable": []+ },+ "hash": "ed041c2746913e1250e2b14c9c1037e60b292e39c679b6461cbb9330a62d69cd"+}diff --git a/.sqlx/query-f576ecd02eefb30acb0a6a7cb2763e875c69972a612e23ea583cea16bb63d469.json b/.sqlx/query-f576ecd02eefb30acb0a6a7cb2763e875c69972a612e23ea583cea16bb63d469.jsonnew file mode 100644index 0000000000..7889a484d2--- /dev/null+++ b/.sqlx/query-f576ecd02eefb30acb0a6a7cb2763e875c69972a612e23ea583cea16bb63d469.json@@ -0,0 +1,15 @@+{+ "db_name": "PostgreSQL",+ "query": "UPDATE import_run SET root_folder_id = $2 WHERE user_id = $1 AND source = 'notion'",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Text",+ "Uuid"+ ]+ },+ "nullable": []+ },+ "hash": "f576ecd02eefb30acb0a6a7cb2763e875c69972a612e23ea583cea16bb63d469"+}diff --git a/.sqlx/query-ff8e627d8fcb9bbcc5751ede083004cfd6f9f4ccb817a23ec8cc868946fcb276.json b/.sqlx/query-ff8e627d8fcb9bbcc5751ede083004cfd6f9f4ccb817a23ec8cc868946fcb276.jsondeleted file mode 100644index 3181be44f2..0000000000--- a/.sqlx/query-ff8e627d8fcb9bbcc5751ede083004cfd6f9f4ccb817a23ec8cc868946fcb276.json+++ /dev/null@@ -1,95 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "\n UPDATE import_entity\n SET status = 'importing', last_error = NULL, updated_at = NOW()\n WHERE user_id = $1 AND source = $2\n AND initiator = 'onboarding' AND status = 'staged'\n RETURNING id, user_id, team_id, source, foreign_id, status, initiator,\n metadata, entity_id, entity_type, last_error, created_at, updated_at\n ",- "describe": {- "columns": [- {- "ordinal": 0,- "name": "id",- "type_info": "Uuid"- },- {- "ordinal": 1,- "name": "user_id",- "type_info": "Text"- },- {- "ordinal": 2,- "name": "team_id",- "type_info": "Uuid"- },- {- "ordinal": 3,- "name": "source",- "type_info": "Text"- },- {- "ordinal": 4,- "name": "foreign_id",- "type_info": "Text"- },- {- "ordinal": 5,- "name": "status",- "type_info": "Text"- },- {- "ordinal": 6,- "name": "initiator",- "type_info": "Text"- },- {- "ordinal": 7,- "name": "metadata",- "type_info": "Jsonb"- },- {- "ordinal": 8,- "name": "entity_id",- "type_info": "Text"- },- {- "ordinal": 9,- "name": "entity_type",- "type_info": "Text"- },- {- "ordinal": 10,- "name": "last_error",- "type_info": "Text"- },- {- "ordinal": 11,- "name": "created_at",- "type_info": "Timestamptz"- },- {- "ordinal": 12,- "name": "updated_at",- "type_info": "Timestamptz"- }- ],- "parameters": {- "Left": [- "Text",- "Text"- ]- },- "nullable": [- false,- false,- true,- false,- false,- false,- false,- false,- true,- true,- true,- false,- false- ]- },- "hash": "ff8e627d8fcb9bbcc5751ede083004cfd6f9f4ccb817a23ec8cc868946fcb276"-}diff --git a/.sqlx/query-ffa2ceefdeb2a64a1578356fba177a37e07a364381274987e848f565a5af736d.json b/.sqlx/query-ffa2ceefdeb2a64a1578356fba177a37e07a364381274987e848f565a5af736d.jsonnew file mode 100644index 0000000000..25a7511e5b--- /dev/null+++ b/.sqlx/query-ffa2ceefdeb2a64a1578356fba177a37e07a364381274987e848f565a5af736d.json@@ -0,0 +1,126 @@+{+ "db_name": "PostgreSQL",+ "query": "\n INSERT INTO import_entity (user_id, source, kind, foreign_id, status, initiator,\n metadata, entity_id, entity_type, team_id)\n VALUES ($1, $2, $9, $3, 'imported', $4, $5, $6, $7, $8)\n ON CONFLICT (user_id, source, foreign_id) DO UPDATE\n SET status = 'imported', metadata = EXCLUDED.metadata,\n entity_id = EXCLUDED.entity_id, entity_type = EXCLUDED.entity_type,\n team_id = EXCLUDED.team_id, last_error = NULL, updated_at = NOW()\n WHERE import_entity.status <> 'imported'\n RETURNING id, user_id, team_id, source, kind, foreign_id, parent_id, review_id, status,\n initiator, metadata, entity_id, entity_type, last_error, waiting_on, created_at,\n updated_at\n ",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "id",+ "type_info": "Uuid"+ },+ {+ "ordinal": 1,+ "name": "user_id",+ "type_info": "Text"+ },+ {+ "ordinal": 2,+ "name": "team_id",+ "type_info": "Uuid"+ },+ {+ "ordinal": 3,+ "name": "source",+ "type_info": "Text"+ },+ {+ "ordinal": 4,+ "name": "kind",+ "type_info": "Text"+ },+ {+ "ordinal": 5,+ "name": "foreign_id",+ "type_info": "Text"+ },+ {+ "ordinal": 6,+ "name": "parent_id",+ "type_info": "Uuid"+ },+ {+ "ordinal": 7,+ "name": "review_id",+ "type_info": "Uuid"+ },+ {+ "ordinal": 8,+ "name": "status",+ "type_info": "Text"+ },+ {+ "ordinal": 9,+ "name": "initiator",+ "type_info": "Text"+ },+ {+ "ordinal": 10,+ "name": "metadata",+ "type_info": "Jsonb"+ },+ {+ "ordinal": 11,+ "name": "entity_id",+ "type_info": "Text"+ },+ {+ "ordinal": 12,+ "name": "entity_type",+ "type_info": "Text"+ },+ {+ "ordinal": 13,+ "name": "last_error",+ "type_info": "Text"+ },+ {+ "ordinal": 14,+ "name": "waiting_on",+ "type_info": "Uuid"+ },+ {+ "ordinal": 15,+ "name": "created_at",+ "type_info": "Timestamptz"+ },+ {+ "ordinal": 16,+ "name": "updated_at",+ "type_info": "Timestamptz"+ }+ ],+ "parameters": {+ "Left": [+ "Text",+ "Text",+ "Text",+ "Text",+ "Jsonb",+ "Text",+ "Text",+ "Uuid",+ "Text"+ ]+ },+ "nullable": [+ false,+ false,+ true,+ false,+ true,+ false,+ true,+ true,+ false,+ false,+ false,+ true,+ true,+ true,+ true,+ false,+ false+ ]+ },+ "hash": "ffa2ceefdeb2a64a1578356fba177a37e07a364381274987e848f565a5af736d"+}diff --git a/.sqlx/query-ca24a6a5985263b41ffecec6d8481191a492610fcb5c161cf75428187705bf91.json b/.sqlx/query-ffef700487dd8f7bbffa4580b8415da90b37ce3f9001e5ca36073baeda7d6226.jsonsimilarity index 60%rename from .sqlx/query-ca24a6a5985263b41ffecec6d8481191a492610fcb5c161cf75428187705bf91.jsonrename to .sqlx/query-ffef700487dd8f7bbffa4580b8415da90b37ce3f9001e5ca36073baeda7d6226.jsonindex 841f9667ad..ed5c037a7d 100644--- a/.sqlx/query-ca24a6a5985263b41ffecec6d8481191a492610fcb5c161cf75428187705bf91.json+++ b/.sqlx/query-ffef700487dd8f7bbffa4580b8415da90b37ce3f9001e5ca36073baeda7d6226.json@@ -1,6 +1,6 @@{"db_name": "PostgreSQL",- "query": "\n SELECT id, user_id, team_id, source, foreign_id, status, initiator,\n metadata, entity_id, entity_type, last_error, created_at, updated_at\n FROM import_entity\n WHERE source = $2 AND foreign_id = $3\n AND status = 'imported'\n AND user_id <> $1\n AND team_id IS NOT NULL\n AND team_id = (SELECT team_id FROM team_user WHERE user_id = $1)\n LIMIT 1\n ",+ "query": "\n SELECT id, user_id, team_id, source, kind, foreign_id, parent_id, review_id, status,\n initiator, metadata, entity_id, entity_type, last_error, waiting_on, created_at, updated_at\n FROM import_entity\n WHERE source = $2 AND foreign_id = $3\n AND status = 'imported'\n AND user_id <> $1\n AND team_id IS NOT NULL\n AND team_id = (SELECT team_id FROM team_user WHERE user_id = $1)\n LIMIT 1\n ","describe": {"columns": [{@@ -25,46 +25,66 @@},{"ordinal": 4,- "name": "foreign_id",+ "name": "kind","type_info": "Text"},{"ordinal": 5,- "name": "status",+ "name": "foreign_id","type_info": "Text"},{"ordinal": 6,+ "name": "parent_id",+ "type_info": "Uuid"+ },+ {+ "ordinal": 7,+ "name": "review_id",+ "type_info": "Uuid"+ },+ {+ "ordinal": 8,+ "name": "status",+ "type_info": "Text"+ },+ {+ "ordinal": 9,"name": "initiator","type_info": "Text"},{- "ordinal": 7,+ "ordinal": 10,"name": "metadata","type_info": "Jsonb"},{- "ordinal": 8,+ "ordinal": 11,"name": "entity_id","type_info": "Text"},{- "ordinal": 9,+ "ordinal": 12,"name": "entity_type","type_info": "Text"},{- "ordinal": 10,+ "ordinal": 13,"name": "last_error","type_info": "Text"},{- "ordinal": 11,+ "ordinal": 14,+ "name": "waiting_on",+ "type_info": "Uuid"+ },+ {+ "ordinal": 15,"name": "created_at","type_info": "Timestamptz"},{- "ordinal": 12,+ "ordinal": 16,"name": "updated_at","type_info": "Timestamptz"}@@ -81,16 +101,20 @@false,true,false,+ true,false,+ true,+ true,false,false,false,true,true,true,+ true,false,false]},- "hash": "ca24a6a5985263b41ffecec6d8481191a492610fcb5c161cf75428187705bf91"+ "hash": "ffef700487dd8f7bbffa4580b8415da90b37ce3f9001e5ca36073baeda7d6226"}diff --git a/Cargo.lock b/Cargo.lockindex 916778ec0e..4de95741b9 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -560,6 +560,7 @@ dependencies = ["http 1.4.0","http-body-util","import",+ "import_notion","initiative","jev","kafka_util",@@ -1127,6 +1128,7 @@ dependencies = ["github_pull_requests","image_generation","import",+ "import_notion","initiative","initiative_description","item_filters",@@ -1151,6 +1153,7 @@ dependencies = ["model_notifications","model_owner","model_user",+ "models_databases","models_dcs","models_email","models_pagination",@@ -10408,6 +10411,8 @@ name = "import"version = "0.1.0"dependencies = ["agent",+ "agent_runtime_protocol",+ "agent_session","ai_billing","ai_toolset","ai_usage",@@ -10418,12 +10423,14 @@ dependencies = ["connection_gateway_client","dateparser","futures",+ "import_notion","macro_authorization","macro_db_migrator","macro_user_id","mcp_select","model-entity","model_user",+ "models_databases","models_properties","properties","regex",@@ -10435,6 +10442,7 @@ dependencies = ["strum 0.27.2","thiserror 2.0.18","tokio",+ "tokio-util","tower 0.5.3","tracing","utoipa",@@ -10442,6 +10450,28 @@ dependencies = ["workspace-hack",]+[[package]]+name = "import_notion"+version = "0.1.0"+dependencies = [+ "anyhow",+ "chrono",+ "futures",+ "http 1.4.0",+ "indexmap 2.14.0",+ "lazy-regex",+ "macro_user_id",+ "pipedream_mcp",+ "rand 0.9.4",+ "rootcause",+ "serde",+ "serde_json",+ "thiserror 2.0.18",+ "tokio",+ "tracing",+ "workspace-hack",+]+[[package]]name = "include_dir"version = "0.7.4"@@ -13999,12 +14029,10 @@ dependencies = ["axum","chrono","generic_email_domains",- "import","macro_authorization","macro_db_migrator","macro_user_id","mcp_client",- "mcp_select","rootcause","serde","serde_json",diff --git a/Cargo.toml b/Cargo.tomlindex 8f471bcb4a..7f73c2b0bd 100644--- a/Cargo.toml+++ b/Cargo.toml@@ -84,6 +84,7 @@ members = ["crates/harness_token","crates/harnesses","crates/import",+ "crates/import_notion","crates/initiative","crates/initiative_description","crates/item_filter_index",diff --git a/apps/docs/AI/mcp/tools/import-from-notion.mdx b/apps/docs/AI/mcp/tools/import-from-notion.mdxnew file mode 100644index 0000000000..5c27f771c6--- /dev/null+++ b/apps/docs/AI/mcp/tools/import-from-notion.mdx@@ -0,0 +1,20 @@+---+title: ImportFromNotion+description: >-+ Import the user's Notion workspace (or part of it) into Macro: pages become+ documents in folders mirroring Notion's page tree, databases become Macro+ databases with their rows and relations, and task-tracker databases become+ Macro tasks.+---++# ImportFromNotion++Import the user's Notion workspace (or part of it) into Macro: pages become documents in folders mirroring Notion's page tree, databases become Macro databases with their rows and relations, and task-tracker databases become Macro tasks. `plan` reads the workspace through the user's Notion connection, decides per page and database what to do (and, with `sharing` "ai", who sees it), links anything a teammate already imported and shared, and stages a plan without writing anything. Who sees the import: unless the user already said, call `plan` without `sharing` first. A user who is not on a team gets a personal import straight away; tell them so and do not offer team sharing. For a user on a team it answers that `sharing` is required: then ask them in one short question with three options (shared with their team, only them, or let AI decide per page), do not guess, and plan again with their answer. `plan` answers within seconds while planning goes on in the background: present the plan right away with PresentReview (kind "import", drafts [\{"source":"notion"}]). The review fills in as Notion is read and each object is decided, and the user approves it there once it is ready; do not wait for planning or check `status` in a loop. Approving that review writes the plan; never import plan items with other tools. End your message after presenting it by asking "Should I kick it off?"; if the user says yes, approve it yourself with DecideReview, submitting the reviewId PresentReview returned. `status` reports progress; `cancel` stops planning or importing. Use this for anything bigger than a single page instead of ImportNotionPage.++## Parameters++| Parameter | Type | Required | Description |+| --- | --- | --- | --- |+| `action` | `"plan"` \| `"status"` \| `"cancel"` | Yes | What the tool is asked to do. |+| `roots` | string[] | No | For `plan`: Notion page or database URLs (or ids) to import, each with everything under it. Leave empty to plan everything the connection can see. |+| `sharing` | `"team"` \| `"personal"` \| `"ai"` | No | For `plan`: who sees what the import writes, as the user answered when asked: `team` shares everything with their team, `personal` keeps everything visible only to them, `ai` lets the decision model choose per page. Leave it out for a user who is not on a team. |diff --git a/apps/docs/AI/mcp/tools/index.mdx b/apps/docs/AI/mcp/tools/index.mdxindex 6a015c921e..16b2d16a66 100644--- a/apps/docs/AI/mcp/tools/index.mdx+++ b/apps/docs/AI/mcp/tools/index.mdx@@ -75,6 +75,7 @@ These pages are generated from Macro's Rust MCP tool registry.- [GetTeamAvailability](/AI/mcp/tools/get-team-availability)- [GetThread](/AI/mcp/tools/get-thread)- [ImportCompaniesCsv](/AI/mcp/tools/import-companies-csv)+- [ImportFromNotion](/AI/mcp/tools/import-from-notion)- [ImportNotionPage](/AI/mcp/tools/import-notion-page)- [ImportPipelineCsv](/AI/mcp/tools/import-pipeline-csv)- [IssueBotCredential](/AI/mcp/tools/issue-bot-credential)diff --git a/apps/docs/config/tool-pages.json b/apps/docs/config/tool-pages.jsonindex df78af9a1c..7deb90a4c3 100644--- a/apps/docs/config/tool-pages.json+++ b/apps/docs/config/tool-pages.json@@ -66,6 +66,7 @@"AI/mcp/tools/get-team-availability","AI/mcp/tools/get-thread","AI/mcp/tools/import-companies-csv",+ "AI/mcp/tools/import-from-notion","AI/mcp/tools/import-notion-page","AI/mcp/tools/import-pipeline-csv","AI/mcp/tools/issue-bot-credential",diff --git a/apps/docs/docs.json b/apps/docs/docs.jsonindex 6c4922f406..fc147158bc 100644--- a/apps/docs/docs.json+++ b/apps/docs/docs.json@@ -170,6 +170,7 @@"AI/mcp/tools/get-team-availability","AI/mcp/tools/get-thread","AI/mcp/tools/import-companies-csv",+ "AI/mcp/tools/import-from-notion","AI/mcp/tools/import-notion-page","AI/mcp/tools/import-pipeline-csv","AI/mcp/tools/issue-bot-credential",diff --git a/apps/marketing/src/features/setup/core/importPreview.ts b/apps/marketing/src/features/setup/core/importPreview.tsindex ac3d2c9ec7..839a5fbc2d 100644--- a/apps/marketing/src/features/setup/core/importPreview.ts+++ b/apps/marketing/src/features/setup/core/importPreview.ts@@ -10,7 +10,6 @@ export interface ImportRun {| 'completed'| 'failed'| 'dismissed';- auto_import: boolean;updated_at: string;}diff --git a/apps/web/src/features/agent-reviews/components/import-plan-card.tsx b/apps/web/src/features/agent-reviews/components/import-plan-card.tsxnew file mode 100644index 0000000000..d2d694a5f0--- /dev/null+++ b/apps/web/src/features/agent-reviews/components/import-plan-card.tsx@@ -0,0 +1,464 @@+import CaretDown from '@phosphor/caret-down.svg';+import { ActionDialogShell } from '@ui/components/ActionDialogShell';+import { Button } from '@ui/components/Button';+import { Card } from '@ui/components/Card';+import { Dialog } from '@ui/components/Dialog';+import { cn } from '@ui/utils/classname';+import { createMemo, createSignal, For, type JSX, Show } from 'solid-js';+import { match } from 'ts-pattern';+import {+ type ImportedCopy,+ type ImportPlanEntry,+ type ImportPlanNode,+ type ImportProgress,+ type ImportReviewPhase,+ type ImportTotal,+ type ImportVolume,+ volumeLabel,+} from '../core/import-plan';+import { ImportPlanRow } from './import-plan-row';++function plural(count: number, noun: string): string {+ return `${count} ${noun}${count === 1 ? '' : 's'}`;+}++function ProgressBar(props: { done: number; total: number }) {+ const percent = () =>+ props.total > 0 ? Math.round((props.done / props.total) * 100) : 0;+ return (+ <div+ role="progressbar"+ aria-label="Import progress"+ aria-valuemin={0}+ aria-valuemax={props.total}+ aria-valuenow={props.done}+ class="h-1.5 w-full overflow-hidden rounded-full bg-edge-muted"+ >+ <div+ class="h-full rounded-full bg-accent transition-[width]"+ style={{ width: `${percent()}%` }}+ />+ </div>+ );+}++function PhaseStatus(props: {+ phase: ImportReviewPhase;+ progress: ImportProgress | undefined;+}) {+ const imported = () => props.progress?.imported ?? 0;+ const total = () =>+ imported() ++ (props.progress?.waiting ?? 0) ++ (props.progress?.writing ?? 0);+ // Rebuild only when the phase changes, not on every progress tick.+ const kind = createMemo(() => props.phase.kind);+ const error = () =>+ props.phase.kind === 'failed' || props.phase.kind === 'unknown'+ ? props.phase.error+ : null;+ return (+ <>+ {match(kind())+ .returnType<JSX.Element>()+ .with('preparing', () => (+ <p role="status" class="text-xs text-ink-muted">+ Preparing the import plan…+ </p>+ ))+ .with('planning', 'plan', () => null)+ .with('starting', () => (+ <p role="status" class="text-xs text-ink-muted">+ Starting the import…+ </p>+ ))+ .with('importing', () => (+ <div class="flex flex-col gap-1.5">+ <ProgressBar done={imported()} total={total()} />+ <p role="status" class="text-xs text-ink-muted">+ {imported()} of {total()} imported+ <Show when={(props.progress?.writing ?? 0) > 0}>+ {' '}+ · {props.progress?.writing} writing+ </Show>+ </p>+ </div>+ ))+ .with('completed', () => (+ <p role="status" class="text-sm text-ink">+ Imported {plural(imported(), 'object')}+ <Show when={props.progress?.error}>+ {(error) => <span class="text-failure"> — {error()}</span>}+ </Show>+ </p>+ ))+ .with('stopped', () => (+ <p role="status" class="text-sm text-ink">+ Import stopped — {plural(imported(), 'imported object')}{' '}+ {imported() === 1 ? 'stays' : 'stay'}+ </p>+ ))+ .with('failed', () => (+ <p role="alert" class="text-sm text-failure">+ {error()}+ </p>+ ))+ .with('dismissed', () => (+ <p role="status" class="text-sm text-ink-muted">+ Dismissed — nothing was imported+ </p>+ ))+ .with('unknown', () => (+ <p role="alert" class="text-sm text-failure">+ {error() ??+ 'The import may not have started. Check Macro before trying again.'}+ </p>+ ))+ .exhaustive()}+ </>+ );+}++/** Objects a teammate already imported; this import links to their copies. */+function AlreadyInMacro(props: {+ summary: string;+ entries: (ImportPlanEntry & { alreadyImported: ImportedCopy })[];+ entityHref: (copy: ImportedCopy) => string | undefined;+ onOpenEntity: (copy: ImportedCopy, event: MouseEvent) => void;+}) {+ const [expanded, setExpanded] = createSignal(false);+ return (+ <section aria-label="Already in Macro" class="flex min-w-0 flex-col gap-1">+ <button+ type="button"+ class="flex min-w-0 items-center gap-1.5 self-start rounded-sm text-left text-xs text-ink-muted touch:min-h-[44px] not-touch:hover:text-ink"+ aria-expanded={expanded()}+ onClick={() => setExpanded(!expanded())}+ >+ <CaretDown+ class={cn(+ 'size-3 shrink-0 transition-transform',+ !expanded() && '-rotate-90'+ )}+ />+ <span class="min-w-0 wrap-break-word">{props.summary}</span>+ </button>+ <Show when={expanded()}>+ <ul class="flex min-w-0 flex-col gap-0.5 pl-4.5">+ <For each={props.entries}>+ {(entry) => {+ const title = () => entry.title || 'Untitled';+ const href = () => props.entityHref(entry.alreadyImported);+ return (+ <li class="flex min-w-0 flex-wrap items-baseline gap-x-1.5 py-1 text-sm touch:min-h-[44px]">+ <Show+ when={href()}+ fallback={<span class="min-w-0 truncate">{title()}</span>}+ >+ {(link) => (+ <a+ href={link()}+ class="min-w-0 truncate text-ink underline decoration-edge underline-offset-2 not-touch:hover:decoration-ink"+ onMouseDown={(event) => event.preventDefault()}+ onClick={(event) => {+ event.preventDefault();+ props.onOpenEntity(entry.alreadyImported, event);+ }}+ >+ {title()}+ </a>+ )}+ </Show>+ <span class="min-w-0 truncate text-xs text-ink-muted">+ by {entry.alreadyImported.by}+ </span>+ </li>+ );+ }}+ </For>+ </ul>+ </Show>+ </section>+ );+}++type ImportPlanCardProps = {+ title: string;+ summary: string;+ /** e.g. "12 shared with your team · 3 only you". */+ sharingSummary: string;+ /** What the plan holds per kind. */+ totals: ImportTotal[];+ /** What planning is doing, while the plan is still being made. */+ planningStatus: string;+ alreadyInMacroSummary: string;+ /** Objects a teammate already imported, left out of the plan tree. */+ alreadyInMacro: (ImportPlanEntry & { alreadyImported: ImportedCopy })[];+ /** Where a teammate's copy opens; `undefined` when it cannot be linked. */+ entityHref: (copy: ImportedCopy) => string | undefined;+ onOpenEntity: (copy: ImportedCopy, event: MouseEvent) => void;+ phase: ImportReviewPhase;+ progress: ImportProgress | undefined;+ nodes: ImportPlanNode[];+ hasPlan: boolean;+ isCollapsed: (id: string) => boolean;+ isShowingRows: (id: string) => boolean;+ isExcluded: (id: string) => boolean;+ isShared: (id: string) => boolean;+ /** The viewer may trim, approve, and stop this import. */+ canAct: boolean;+ /** Trimming and approving are open. */+ editable: boolean;+ /** The agent approved it because the owner told it to in chat. */+ startedByAgent: boolean;+ working: boolean;+ stopping: boolean;+ count: number;+ volume: ImportVolume;+ alert?: string;+ onReload?: () => void;+ onToggleExpanded: (id: string) => void;+ onToggleRows: (id: string) => void;+ onIncludedChange: (id: string, included: boolean) => void;+ onSharedChange: (id: string, shared: boolean) => void;+ onApprove: () => void;+ onDismiss: () => void;+ onStop: () => void;+};++/** What the plan holds per kind, e.g. "Databases 18 (405 rows)". */+function PlanTotals(props: { totals: ImportTotal[] }) {+ return (+ <Show when={props.totals.length > 0}>+ <dl+ aria-label="Plan totals"+ class="flex min-w-0 flex-wrap gap-x-4 gap-y-1 text-xs"+ >+ <For each={props.totals}>+ {(total) => (+ <div class="flex items-baseline gap-1">+ <dt class="text-ink-muted">{total.label}</dt>+ <dd class="text-ink">{total.value}</dd>+ </div>+ )}+ </For>+ </dl>+ </Show>+ );+}++/**+ * The plan and its progress. The card shows it in a short scrolling list;+ * `full` shows the whole tree in the full review.+ */+function PlanContents(+ props: ImportPlanCardProps & { full: boolean; onOpenFull?: () => void }+) {+ const choosing = () => props.phase.kind === 'plan';+ const planning = () => props.phase.kind === 'planning';+ const running = () =>+ props.phase.kind === 'starting' || props.phase.kind === 'importing';+ const failures = () => props.progress?.failures ?? [];+ return (+ <>+ <header class="flex min-w-0 flex-col gap-0.5">+ <Show+ when={props.full}+ fallback={+ <h3 class="text-sm font-semibold text-ink">{props.title}</h3>+ }+ >+ <ActionDialogShell.Title>{props.title}</ActionDialogShell.Title>+ </Show>+ <Show when={props.summary}>+ <p class="text-xs text-ink-muted">{props.summary}</p>+ </Show>+ <Show when={(choosing() || planning()) && props.sharingSummary}>+ <p class="text-xs text-ink-muted">{props.sharingSummary}</p>+ </Show>+ </header>+ <PlanTotals totals={props.totals} />+ <Show when={props.alert}>+ <div class="flex flex-wrap items-center gap-2">+ <p role="alert" class="text-sm text-failure">+ {props.alert}+ </p>+ <Show when={props.onReload}>+ {(reload) => (+ <Button+ variant="outline"+ size="sm"+ disabled={props.working}+ onClick={() => reload()()}+ >+ Reload saved review+ </Button>+ )}+ </Show>+ </div>+ </Show>+ <Show+ when={planning()}+ fallback={<PhaseStatus phase={props.phase} progress={props.progress} />}+ >+ <p role="status" class="text-xs text-ink-muted">+ {props.planningStatus}+ </p>+ </Show>+ <Show when={props.startedByAgent}>+ <p class="text-xs text-ink-muted">+ {props.canAct+ ? 'Started by the agent at your request'+ : "Started by the agent at the owner's request"}+ </p>+ </Show>+ <Show+ when={props.hasPlan}+ fallback={+ <Show when={!planning()}>+ <p class="text-xs text-ink-muted">+ {props.alreadyInMacro.length > 0+ ? 'Everything Notion shared is already in Macro.'+ : 'The plan has no objects to show.'}+ </p>+ </Show>+ }+ >+ <ul+ aria-label="Import plan"+ class={cn(+ 'min-w-0 overflow-x-hidden',+ props.full+ ? 'min-h-0 flex-1 overflow-y-auto'+ : 'max-h-80 overflow-y-auto'+ )}+ >+ <For each={props.nodes}>+ {(node) => (+ <ImportPlanRow+ node={node}+ collapsed={props.isCollapsed(node.id)}+ showingRows={props.isShowingRows(node.id)}+ excluded={props.isExcluded(node.id)}+ shared={props.isShared(node.id)}+ choosing={(choosing() || planning()) && props.canAct}+ disabled={!props.editable}+ showStatus={+ !choosing() && !planning() && props.phase.kind !== 'preparing'+ }+ onToggleExpanded={() => props.onToggleExpanded(node.id)}+ onToggleRows={() => props.onToggleRows(node.id)}+ onIncludedChange={(included) =>+ props.onIncludedChange(node.id, included)+ }+ onSharedChange={(shared) =>+ props.onSharedChange(node.id, shared)+ }+ />+ )}+ </For>+ </ul>+ <Show when={props.onOpenFull}>+ {(open) => (+ <Button+ variant="ghost"+ size="sm"+ class="self-start"+ onClick={() => open()()}+ >+ Open full review+ </Button>+ )}+ </Show>+ </Show>+ <Show when={props.alreadyInMacro.length > 0}>+ <AlreadyInMacro+ summary={props.alreadyInMacroSummary}+ entries={props.alreadyInMacro}+ entityHref={props.entityHref}+ onOpenEntity={props.onOpenEntity}+ />+ </Show>+ <Show when={failures().length > 0}>+ <section aria-label="Failures" class="flex flex-col gap-1">+ <h4 class="text-xs font-medium text-ink-muted">+ {plural(failures().length, 'object')} couldn't be imported+ </h4>+ <ul class="flex flex-col gap-0.5">+ <For each={failures()}>+ {(failure) => (+ <li class="min-w-0 text-xs text-ink-muted wrap-break-word">+ <span class="text-ink">{failure.title || 'Untitled'}</span>+ <Show when={failure.reason}> — {failure.reason}</Show>+ </li>+ )}+ </For>+ </ul>+ </section>+ </Show>+ <Show when={props.canAct && (choosing() || planning())}>+ <footer class="flex flex-wrap items-center justify-end gap-2">+ <p class="mr-auto min-w-0 text-xs text-ink-muted">+ You can stop the import while it runs; anything already imported+ stays.+ </p>+ <Button+ variant="ghost"+ size="sm"+ disabled={!props.editable}+ onClick={() => props.onDismiss()}+ >+ Dismiss+ </Button>+ <Button+ variant="cta"+ size="sm"+ disabled={!props.editable || props.count === 0}+ onClick={() => props.onApprove()}+ >+ {planning() ? 'Import' : `Import ${volumeLabel(props.volume)}`}+ </Button>+ </footer>+ </Show>+ <Show when={props.canAct && running()}>+ <footer class="flex justify-end">+ <Button+ variant="danger"+ size="sm"+ disabled={props.stopping}+ onClick={() => props.onStop()}+ >+ {props.stopping ? 'Stopping…' : 'Stop'}+ </Button>+ </footer>+ </Show>+ </>+ );+}++/**+ * A Notion workspace import plan: filled in while it is planned, trimmed+ * and approved, then its progress. "Open full review" shows the whole plan+ * in a dialog, a full-height sheet on phones.+ */+export function ImportPlanCard(props: ImportPlanCardProps) {+ const [full, setFull] = createSignal(false);+ return (+ <>+ <Card class="my-3 flex min-w-0 flex-col gap-3 p-3">+ <PlanContents+ {...props}+ full={false}+ onOpenFull={() => setFull(true)}+ />+ </Card>+ <Dialog open={full()} onOpenChange={setFull}>+ <ActionDialogShell class="h-[85dvh] max-h-[85dvh] mobile:h-auto mobile:max-h-none">+ <ActionDialogShell.Body class="flex min-h-0 flex-1 flex-col gap-3 space-y-0">+ <PlanContents {...props} full />+ </ActionDialogShell.Body>+ </ActionDialogShell>+ </Dialog>+ </>+ );+}diff --git a/apps/web/src/features/agent-reviews/components/import-plan-row.tsx b/apps/web/src/features/agent-reviews/components/import-plan-row.tsxnew file mode 100644index 0000000000..fe7a3a474e--- /dev/null+++ b/apps/web/src/features/agent-reviews/components/import-plan-row.tsx@@ -0,0 +1,323 @@+import { EntityIcon } from '@core/component/EntityIcon';+import CaretDown from '@phosphor/caret-down.svg';+import { Badge, badgeTriggerClasses } from '@ui/components/Badge';+import { Checkbox } from '@ui/components/Checkbox';+import { Tooltip } from '@ui/components/Tooltip';+import { cn } from '@ui/utils/classname';+import { Show } from 'solid-js';+import { match } from 'ts-pattern';+import {+ type ImportPlanDecision,+ type ImportPlanNode,+ type ImportTaskFields,+ isWritten,+} from '../core/import-plan';++/** Deeper levels share the last indent so rows fit a phone-width transcript. */+const MAX_INDENT_LEVEL = 4;++function decisionLabel(decision: ImportPlanDecision | null): string {+ return match(decision)+ .with('copy', () => 'Copy')+ .with('tasks', () => 'Tasks')+ .with('skip', () => 'Skip')+ .with('hand', () => 'Needs you')+ .with(null, () => 'Deciding…')+ .exhaustive();+}++/** e.g. "2,365 tasks — Fix login redirect, Q3 pricing page, …". */+function rowsLine(node: ImportPlanNode): string {+ const count = (node.rows ?? node.sampleTitles.length).toLocaleString('en-US');+ const noun = node.decision === 'tasks' ? 'tasks' : 'rows';+ const more =+ (node.rows ?? 0) > node.sampleTitles.length && node.sampleTitles.length > 0+ ? ', …'+ : '';+ const titles = node.sampleTitles.map((title) => title || 'Untitled');+ return titles.length > 0+ ? `${count} ${noun} — ${titles.join(', ')}${more}`+ : `${count} ${noun}`;+}++/** e.g. "Status from “Stage” · Due from “Deadline”". */+function taskFieldsLine(fields: ImportTaskFields): string {+ return [+ fields.status ? `Status from “${fields.status}”` : '',+ fields.priority ? `Priority from “${fields.priority}”` : '',+ fields.due ? `Due from “${fields.due}”` : '',+ fields.assignee ? `Assignee from “${fields.assignee}”` : '',+ ]+ .filter(Boolean)+ .join(' · ');+}++function DecisionBadge(props: {+ decision: ImportPlanDecision | null;+ reason: string | null;+}) {+ const badge = () => (+ <Badge+ size="xs"+ variant="outline"+ class={cn(+ props.decision === 'tasks' && 'text-accent',+ props.decision === 'hand' && 'text-warning',+ (props.decision === 'skip' || props.decision === null) &&+ 'text-ink-extra-muted'+ )}+ >+ {decisionLabel(props.decision)}+ </Badge>+ );+ return (+ <Show when={props.reason} fallback={badge()}>+ {(reason) => (+ <Tooltip label={reason()} as="span" tabIndex={0}>+ {badge()}+ </Tooltip>+ )}+ </Show>+ );+}++function SharingBadge(props: {+ title: string;+ shared: boolean;+ reason: string | null;+ /** The owner can flip it here. */+ toggleable: boolean;+ muted: boolean;+ onSharedChange: (shared: boolean) => void;+}) {+ const label = () => (props.shared ? 'Team' : 'Only you');+ const tone = () =>+ cn(+ props.shared && !props.muted ? 'text-accent' : 'text-ink-muted',+ props.muted && 'opacity-60'+ );+ const badge = () => (+ <Show+ when={props.toggleable}+ fallback={+ <Badge size="xs" variant="outline" class={tone()}>+ {label()}+ </Badge>+ }+ >+ <button+ type="button"+ aria-pressed={props.shared}+ aria-label={+ props.shared+ ? `Make ${props.title} only visible to you`+ : `Share ${props.title} with your team`+ }+ class={badgeTriggerClasses({+ variant: 'outline',+ size: 'xs',+ class: cn('touch:h-8 touch:px-3', tone()),+ })}+ onClick={() => props.onSharedChange(!props.shared)}+ >+ {label()}+ </button>+ </Show>+ );+ return (+ <Show when={props.reason} fallback={badge()}>+ {(reason) => (+ <Tooltip+ label={reason()}+ as="span"+ tabIndex={props.toggleable ? undefined : 0}+ >+ {badge()}+ </Tooltip>+ )}+ </Show>+ );+}++function RowStatus(props: { node: ImportPlanNode; excluded: boolean }) {+ return (+ <Show+ when={props.node.error}+ fallback={+ <span class="shrink-0 text-xs text-ink-muted">+ {match(props.node.status)+ .with('imported', () => 'Imported')+ .with('importing', () => 'Writing…')+ .with('discarded', () => 'Left out')+ .with('staged', () =>+ props.node.decision === 'hand'+ ? 'Left for the agent'+ : isWritten(props.node.decision) && !props.excluded+ ? 'Waiting'+ : ''+ )+ .exhaustive()}+ </span>+ }+ >+ <span class="shrink-0 text-xs text-failure">Failed</span>+ </Show>+ );+}++/** One Notion object in the plan tree. */+export function ImportPlanRow(props: {+ node: ImportPlanNode;+ collapsed: boolean;+ /** A database's sample rows and task columns are showing. */+ showingRows: boolean;+ excluded: boolean;+ /** Shared with the owner's team when written. */+ shared: boolean;+ /** Show the checkbox that leaves the object out, and the sharing toggle. */+ choosing: boolean;+ disabled: boolean;+ /** Show how the write went instead of the plan. */+ showStatus: boolean;+ onToggleExpanded: () => void;+ onToggleRows: () => void;+ onIncludedChange: (included: boolean) => void;+ onSharedChange: (shared: boolean) => void;+}) {+ const written = () => isWritten(props.node.decision);+ const included = () => written() && !props.excluded;+ const hasRows = () =>+ props.node.kind === 'database' &&+ (props.node.sampleTitles.length > 0 || props.node.taskFields !== null);+ const indent = () => `${Math.min(props.node.depth, MAX_INDENT_LEVEL) * 12}px`;+ return (+ <li+ class={cn(+ 'flex min-w-0 flex-col',+ !included() && !props.showStatus && 'text-ink-muted',+ props.showStatus && props.node.status === 'discarded' && 'opacity-60'+ )}+ >+ <div+ class="flex min-w-0 items-center gap-1.5 py-1 pr-1 touch:min-h-[44px]"+ style={{ 'padding-left': indent() }}+ >+ <Show+ when={props.node.childIds.length > 0}+ fallback={+ <Show+ when={hasRows()}+ fallback={+ <span class="size-4 shrink-0 touch:size-8" aria-hidden="true" />+ }+ >+ <button+ type="button"+ class="flex size-4 shrink-0 items-center justify-center rounded-sm text-ink-muted touch:size-8 not-touch:hover:text-ink"+ aria-expanded={props.showingRows}+ aria-label={`${props.showingRows ? 'Hide' : 'Show'} rows of ${props.node.title || 'Untitled'}`}+ onClick={() => props.onToggleRows()}+ >+ <CaretDown+ class={cn(+ 'size-3 transition-transform',+ !props.showingRows && '-rotate-90'+ )}+ />+ </button>+ </Show>+ }+ >+ <button+ type="button"+ class="flex size-4 shrink-0 items-center justify-center rounded-sm text-ink-muted touch:size-8 not-touch:hover:text-ink"+ aria-expanded={!props.collapsed}+ aria-label={`${props.collapsed ? 'Show' : 'Hide'} what is inside ${props.node.title}`}+ onClick={() => props.onToggleExpanded()}+ >+ <CaretDown+ class={cn(+ 'size-3 transition-transform',+ props.collapsed && '-rotate-90'+ )}+ />+ </button>+ </Show>+ <Show when={props.choosing}>+ <Checkbox+ checked={included()}+ disabled={props.disabled || !written()}+ onChange={(checked) => props.onIncludedChange(checked)}+ >+ <Checkbox.Control />+ <Checkbox.Label class="sr-only">+ Import {props.node.title}+ </Checkbox.Label>+ </Checkbox>+ </Show>+ <EntityIcon+ targetType={props.node.kind === 'database' ? 'database' : 'md'}+ size="xs"+ />+ <span class="flex min-w-0 flex-1 flex-col">+ <span class="truncate text-sm" title={props.node.title}>+ {props.node.title || 'Untitled'}+ </span>+ {/* The agent picks these up, so their reason is worth reading. */}+ <Show when={props.node.decision === 'hand' && props.node.reason}>+ <span class="truncate text-xs text-ink-muted">+ {props.node.reason}+ </span>+ </Show>+ </span>+ <Show when={props.node.kind === 'database' && props.node.rows !== null}>+ <span class="shrink-0 text-xs text-ink-muted">+ {props.node.rows} rows+ </span>+ </Show>+ <Show when={written()}>+ <SharingBadge+ title={props.node.title || 'Untitled'}+ shared={props.shared}+ reason={props.node.sharedReason}+ toggleable={+ props.node.shareable && props.choosing && !props.disabled+ }+ muted={props.excluded}+ onSharedChange={(shared) => props.onSharedChange(shared)}+ />+ </Show>+ <Show+ when={props.showStatus}+ fallback={+ <DecisionBadge+ decision={props.node.decision}+ reason={props.node.reason}+ />+ }+ >+ <RowStatus node={props.node} excluded={props.excluded} />+ </Show>+ </div>+ <Show when={hasRows() && props.showingRows}>+ <div+ class="flex min-w-0 flex-col gap-0.5 pb-1.5 pr-1 text-xs text-ink-muted"+ style={{ 'padding-left': `calc(${indent()} + 1.375rem)` }}+ >+ <p class="wrap-break-word">{rowsLine(props.node)}</p>+ <Show+ when={+ props.node.decision === 'tasks' ? props.node.taskFields : null+ }+ >+ {(fields) => (+ <Show when={taskFieldsLine(fields())}>+ {(line) => <p class="wrap-break-word">{line()}</p>}+ </Show>+ )}+ </Show>+ </div>+ </Show>+ </li>+ );+}diff --git a/apps/web/src/features/agent-reviews/core/import-plan.test.ts b/apps/web/src/features/agent-reviews/core/import-plan.test.tsnew file mode 100644index 0000000000..2898b8846b--- /dev/null+++ b/apps/web/src/features/agent-reviews/core/import-plan.test.ts@@ -0,0 +1,435 @@+import { describe, expect, it } from 'vitest';+import {+ alreadyInMacroSummary,+ buildPlanTree,+ EMPTY_IMPORT_DRAFT,+ type ImportPlanEntry,+ type ImportProgress,+ importCount,+ importReviewPhase,+ importVolume,+ initiallyCollapsed,+ isShared,+ planCounts,+ planningStatus,+ planSummary,+ planTotals,+ planTreeEntries,+ setExcluded,+ setShared,+ sharingSummary,+ visiblePlanNodes,+ volumeLabel,+} from './import-plan';++const unshared = {+ sampleTitles: [],+ taskFields: null,+ shared: false,+ sharedReason: null,+ shareable: false,+ alreadyImported: null,+};++const entries: ImportPlanEntry[] = [+ {+ id: 'wiki',+ parentId: null,+ kind: 'page',+ title: 'Wiki',+ decision: 'copy',+ reason: null,+ rows: null,+ status: 'staged',+ error: null,+ ...unshared,+ },+ {+ id: 'roadmap',+ parentId: 'wiki',+ kind: 'database',+ title: 'Roadmap',+ decision: 'tasks',+ reason: 'Rows have a status and an owner',+ rows: 40,+ status: 'staged',+ error: null,+ ...unshared,+ },+ {+ id: 'q3',+ parentId: 'roadmap',+ kind: 'page',+ title: 'Q3',+ decision: 'copy',+ reason: null,+ rows: null,+ status: 'staged',+ error: null,+ ...unshared,+ },+ {+ id: 'scratch',+ parentId: 'wiki',+ kind: 'page',+ title: 'Scratch',+ decision: 'skip',+ reason: 'Empty page',+ rows: null,+ status: 'staged',+ error: null,+ ...unshared,+ },+ {+ id: 'billing',+ parentId: 'gone',+ kind: 'page',+ title: 'Billing',+ decision: 'hand',+ reason: 'Holds credentials',+ rows: null,+ status: 'staged',+ error: null,+ ...unshared,+ },+];++describe('buildPlanTree', () => {+ it('orders entries depth-first and treats a missing parent as a root', () => {+ const nodes = buildPlanTree(entries);+ expect(nodes.map((node) => [node.id, node.depth, node.childIds])).toEqual([+ ['wiki', 0, ['roadmap', 'scratch']],+ ['roadmap', 1, ['q3']],+ ['q3', 2, []],+ ['scratch', 1, []],+ ['billing', 0, []],+ ]);+ });++ it('places every entry of a parent cycle exactly once', () => {+ const nodes = buildPlanTree([+ { ...entries[0], id: 'a', parentId: 'b' },+ { ...entries[0], id: 'b', parentId: 'a' },+ ]);+ expect(nodes.map((node) => [node.id, node.depth])).toEqual([+ ['a', 0],+ ['b', 1],+ ]);+ });+});++describe('visiblePlanNodes', () => {+ it('hides everything under a collapsed node', () => {+ const nodes = buildPlanTree(entries);+ expect(+ visiblePlanNodes(nodes, new Set(['wiki'])).map((node) => node.id)+ ).toEqual(['wiki', 'billing']);+ expect(+ visiblePlanNodes(nodes, initiallyCollapsed(nodes)).map((node) => node.id)+ ).toEqual(['wiki', 'roadmap', 'scratch', 'billing']);+ });+});++describe('setExcluded', () => {+ it('leaves out the written objects below a node and brings them back', () => {+ const nodes = buildPlanTree(entries);+ const out = setExcluded(EMPTY_IMPORT_DRAFT, nodes, 'wiki', true);+ expect(out).toEqual({+ ...EMPTY_IMPORT_DRAFT,+ excluded: ['wiki', 'roadmap', 'q3'],+ });+ expect(importCount(entries, out.excluded)).toBe(0);+ const back = setExcluded(out, nodes, 'roadmap', false);+ expect(back).toEqual({ ...EMPTY_IMPORT_DRAFT, excluded: ['wiki'] });+ expect(importCount(entries, back.excluded)).toBe(2);+ });+});++describe('setShared', () => {+ const sharing = entries.map(+ (entry): ImportPlanEntry =>+ entry.kind === 'page'+ ? { ...entry, shared: true, shareable: true }+ : {+ ...entry,+ sharedReason: 'Personal notes',+ }+ );++ it('records only the choices that differ from the plan', () => {+ const nodes = buildPlanTree(sharing);+ const personal = setShared(EMPTY_IMPORT_DRAFT, nodes, 'wiki', false);+ // Roadmap is not shareable and Scratch is never written.+ expect(personal).toEqual({+ ...EMPTY_IMPORT_DRAFT,+ personal: ['wiki', 'q3'],+ });+ expect(nodes.map((node) => [node.id, isShared(node, personal)])).toEqual([+ ['wiki', false],+ ['roadmap', false],+ ['q3', false],+ ['scratch', true],+ ['billing', true],+ ]);+ expect(sharingSummary(sharing, personal, true)).toBe('3 only you');+ const back = setShared(personal, nodes, 'q3', true);+ expect(back).toEqual({ ...EMPTY_IMPORT_DRAFT, personal: ['wiki'] });+ expect(sharingSummary(sharing, back, true)).toBe(+ '1 shared with your team · 2 only you'+ );+ expect(setShared(back, nodes, 'wiki', true)).toEqual(EMPTY_IMPORT_DRAFT);+ });++ it('switches private rows to the team and leaves out excluded rows', () => {+ const shareable = entries.map((entry) => ({ ...entry, shareable: true }));+ const nodes = buildPlanTree(shareable);+ const shared = setShared(EMPTY_IMPORT_DRAFT, nodes, 'roadmap', true);+ expect(shared).toEqual({+ ...EMPTY_IMPORT_DRAFT,+ shared: ['roadmap', 'q3'],+ });+ expect(+ sharingSummary(shareable, { ...shared, excluded: ['wiki'] }, true)+ ).toBe('2 shared with your team');+ expect(+ sharingSummary(+ shareable,+ {+ ...shared,+ excluded: ['wiki', 'roadmap', 'q3'],+ },+ true+ )+ ).toBe('');+ });+});++describe('already in Macro', () => {+ const copy = (by: string) => ({+ by,+ entityId: `doc-${by}`,+ entityType: 'md',+ });+ const withCopies = entries.map(+ (entry): ImportPlanEntry =>+ entry.id === 'wiki'+ ? { ...entry, alreadyImported: copy('[email protected]') }+ : entry.id === 'q3'+ ? { ...entry, alreadyImported: copy('[email protected]') }+ : entry+ );++ it('leaves copies out of the tree and moves their children up', () => {+ const nodes = buildPlanTree(planTreeEntries(withCopies));+ expect(nodes.map((node) => [node.id, node.depth])).toEqual([+ ['roadmap', 0],+ ['scratch', 0],+ ['billing', 0],+ ]);+ const nested = buildPlanTree(+ planTreeEntries(+ entries.map((entry) =>+ entry.id === 'roadmap'+ ? { ...entry, alreadyImported: copy('[email protected]') }+ : entry+ )+ )+ );+ expect(nested.map((node) => [node.id, node.depth, node.parentId])).toEqual([+ ['wiki', 0, null],+ ['q3', 1, 'wiki'],+ ['scratch', 1, 'wiki'],+ ['billing', 0, 'gone'],+ ]);+ });++ it('counts only new objects in the import, and Notion still shared them all', () => {+ expect(importCount(withCopies, [])).toBe(1);+ expect(volumeLabel(importVolume(withCopies, []))).toBe('40 tasks');+ expect(planCounts(withCopies).pages).toBe(4);+ expect(alreadyInMacroSummary(withCopies)).toBe(+ '2 already in Macro (imported by [email protected], [email protected])'+ );+ expect(alreadyInMacroSummary(entries)).toBe('');+ });+});++describe('planSummary', () => {+ it('reads the plan size and decisions', () => {+ expect(planSummary(planCounts(entries), importVolume(entries, []))).toBe(+ 'Notion shared 4 pages and 1 database — importing 2 pages, 40 tasks · 1 skipped, 1 needs you'+ );+ });+});++describe('importVolume', () => {+ it('counts every row a database or task list writes, without what was left out', () => {+ const database: ImportPlanEntry = {+ id: 'bugs',+ parentId: null,+ kind: 'database',+ title: 'Bugs',+ decision: 'copy',+ reason: null,+ rows: 405,+ status: 'staged',+ error: null,+ ...unshared,+ };+ const all = [...entries, database];+ expect(importVolume(all, [])).toEqual({+ documents: 2,+ databases: 1,+ rows: 405,+ taskLists: 1,+ tasks: 40,+ });+ expect(volumeLabel(importVolume(all, []))).toBe(+ '2 pages, 1 database (405 rows), 40 tasks'+ );+ expect(volumeLabel(importVolume(all, ['roadmap', 'bugs']))).toBe('2 pages');+ expect(volumeLabel(importVolume([], []))).toBe('nothing');+ });+});++describe('importReviewPhase', () => {+ const progress: ImportProgress = {+ reviewId: 'review',+ status: 'importing',+ error: null,+ scanned: null,+ onTeam: true,+ counts: planCounts(entries),+ imported: 1,+ waiting: 1,+ writing: 1,+ failures: [],+ };++ it('follows the review item until the plan is approved', () => {+ expect(+ importReviewPhase({ presentedAt: null }, undefined, undefined)+ ).toEqual({ kind: 'preparing' });+ expect(+ importReviewPhase({ presentedAt: 1 }, { status: 'pending' }, progress)+ ).toEqual({ kind: 'plan' });+ expect(+ importReviewPhase(+ { presentedAt: 1 },+ { status: 'pending' },+ { ...progress, status: 'running' }+ )+ ).toEqual({ kind: 'planning' });+ expect(+ importReviewPhase({ presentedAt: 1 }, { status: 'dismissed' }, undefined)+ ).toEqual({ kind: 'dismissed' });+ });++ it('follows the run once the plan is approved', () => {+ const completed = { status: 'completed' as const };+ expect(importReviewPhase({ presentedAt: 1 }, completed, progress)).toEqual({+ kind: 'importing',+ });+ expect(+ importReviewPhase({ presentedAt: 1 }, completed, {+ ...progress,+ status: 'dismissed',+ error: 'Cancelled',+ })+ ).toEqual({ kind: 'stopped' });+ expect(+ importReviewPhase({ presentedAt: 1 }, completed, {+ ...progress,+ status: 'failed',+ error: 'Notion is unreachable',+ })+ ).toEqual({ kind: 'failed', error: 'Notion is unreachable' });+ expect(+ importReviewPhase({ presentedAt: 1 }, completed, {+ ...progress,+ status: null,+ })+ ).toEqual({ kind: 'completed' });+ expect(importReviewPhase({ presentedAt: 1 }, completed, undefined)).toEqual(+ { kind: 'importing' }+ );+ });+});++describe('planning', () => {+ const running: ImportProgress = {+ reviewId: 'review',+ status: 'running',+ error: null,+ scanned: { pages: 40, databases: 12 },+ onTeam: true,+ counts: planCounts([]),+ imported: 0,+ waiting: 0,+ writing: 0,+ failures: [],+ };++ it('reads Notion until the plan is staged, then counts what is decided', () => {+ expect(planningStatus(running, [])).toBe(+ 'Reading Notion: 40 pages, 12 databases'+ );+ expect(planningStatus({ ...running, scanned: null }, [])).toBe(+ 'Reading Notion…'+ );+ const staged = entries.map((entry, index) =>+ index < 2 ? entry : { ...entry, decision: null }+ );+ expect(planningStatus(running, staged)).toBe(+ 'Deciding what to import: 2 of 5 decided'+ );+ expect(planCounts(staged).deciding).toBe(3);+ expect(importVolume(staged, [])).toEqual({+ documents: 1,+ databases: 0,+ rows: 0,+ taskLists: 1,+ tasks: 40,+ });+ });++ it('says why nothing is shared without a team', () => {+ expect(sharingSummary(entries, EMPTY_IMPORT_DRAFT, false)).toBe(+ "3 only you — you're not on a team, so nothing is shared"+ );+ });+});++describe('planTotals', () => {+ it('totals the plan per kind and leaves empty kinds out', () => {+ const database: ImportPlanEntry = {+ id: 'bugs',+ parentId: null,+ kind: 'database',+ title: 'Bugs',+ decision: 'copy',+ reason: null,+ rows: 405,+ status: 'staged',+ error: null,+ ...unshared,+ };+ const copied: ImportPlanEntry = {+ ...database,+ id: 'crm',+ title: 'CRM',+ alreadyImported: {+ by: '[email protected]',+ entityId: 'db',+ entityType: 'database',+ },+ };+ expect(planTotals([...entries, database, copied], [])).toEqual([+ { label: 'Documents', value: '2' },+ { label: 'Databases', value: '1 (405 rows)' },+ { label: 'Task lists', value: '1 (40 tasks)' },+ { label: 'Skipped', value: '1' },+ { label: 'Already in Macro', value: '1' },+ ]);+ });+});diff --git a/apps/web/src/features/agent-reviews/core/import-plan.ts b/apps/web/src/features/agent-reviews/core/import-plan.tsnew file mode 100644index 0000000000..bb51335cb4--- /dev/null+++ b/apps/web/src/features/agent-reviews/core/import-plan.ts@@ -0,0 +1,579 @@+import { match } from 'ts-pattern';+import type { Review, ReviewItem } from './review';++/** What a workspace import does with one Notion object. */+export type ImportPlanDecision = 'copy' | 'tasks' | 'skip' | 'hand';++/** Which Notion columns become a task list's task fields. */+export type ImportTaskFields = {+ status: string | null;+ priority: string | null;+ due: string | null;+ assignee: string | null;+};++/** One Notion object in an import plan. */+export type ImportPlanEntry = {+ id: string;+ parentId: string | null;+ kind: 'page' | 'database';+ title: string;+ /** `null` while planning has not decided it yet. */+ decision: ImportPlanDecision | null;+ reason: string | null;+ rows: number | null;+ /** A few of a database's row titles. */+ sampleTitles: string[];+ /** For a task list: the columns its task fields come from. */+ taskFields: ImportTaskFields | null;+ status: 'staged' | 'importing' | 'imported' | 'discarded';+ error: string | null;+ /** The plan shares the written object with the owner's team. */+ shared: boolean;+ /** Why it is shared or kept private. */+ sharedReason: string | null;+ /** The owner may change `shared`. */+ shareable: boolean;+ /** A teammate's copy, already shared with the team, that this row links to. */+ alreadyImported: ImportedCopy | null;+};++/** A teammate's import of the same Notion object. */+export type ImportedCopy = {+ /** The teammate's email. */+ by: string;+ entityId: string;+ /** A Macro entity type, e.g. `md`, `project`, or `database`. */+ entityType: string;+};++/** A plan entry placed in its tree, in depth-first order. */+export type ImportPlanNode = ImportPlanEntry & {+ depth: number;+ childIds: string[];+};++export type ImportPlanCounts = {+ pages: number;+ databases: number;+ copy: number;+ tasks: number;+ skip: number;+ hand: number;+ /** Not decided yet. */+ deciding: number;+};++export type ImportReportItem = { id: string; title: string; reason: string };++/** Where the import run stands, as the server reports it. */+export type ImportProgress = {+ reviewId: string | null;+ status:+ | 'running'+ | 'ready'+ | 'importing'+ | 'completed'+ | 'failed'+ | 'dismissed'+ | null;+ error: string | null;+ /** What planning has found in Notion so far, while it reads it. */+ scanned: { pages: number; databases: number } | null;+ /** Without a team, everything stays with the owner. */+ onTeam: boolean;+ counts: ImportPlanCounts;+ imported: number;+ waiting: number;+ writing: number;+ failures: ImportReportItem[];+};++export type ImportReviewPhase =+ | { kind: 'preparing' }+ | { kind: 'planning' }+ | { kind: 'plan' }+ | { kind: 'starting' }+ | { kind: 'importing' }+ | { kind: 'completed' }+ | { kind: 'stopped' }+ | { kind: 'failed'; error: string }+ | { kind: 'dismissed' }+ | { kind: 'unknown'; error: string | null };++/**+ * The saved draft of an import review. `shared` and `personal` hold the rows+ * the owner switched to or from team sharing, against the plan's choice.+ */+export type ImportDraft = {+ source: 'notion';+ excluded: string[];+ shared: string[];+ personal: string[];+};++export const EMPTY_IMPORT_DRAFT: ImportDraft = {+ source: 'notion',+ excluded: [],+ shared: [],+ personal: [],+};++/** Whether a decision writes anything, and so can be left out. */+export function isWritten(decision: ImportPlanDecision | null): boolean {+ return match(decision)+ .with('copy', 'tasks', () => true)+ .with('skip', 'hand', null, () => false)+ .exhaustive();+}++/** Whether an entry becomes a new Macro object when the plan is approved. */+function isNew(entry: ImportPlanEntry): boolean {+ return isWritten(entry.decision) && entry.alreadyImported === null;+}++/**+ * The entries the plan tree shows: everything not already in Macro. A child+ * of an already-imported entry moves up to its nearest ancestor still shown.+ */+export function planTreeEntries(entries: ImportPlanEntry[]): ImportPlanEntry[] {+ const byId = new Map(entries.map((entry) => [entry.id, entry]));+ const shownParent = (parentId: string | null): string | null => {+ const seen = new Set<string>();+ let current = parentId;+ while (current !== null && !seen.has(current)) {+ seen.add(current);+ const parent = byId.get(current);+ if (!parent || parent.alreadyImported === null) return current;+ current = parent.parentId;+ }+ return null;+ };+ return entries+ .filter((entry) => entry.alreadyImported === null)+ .map((entry) => ({ ...entry, parentId: shownParent(entry.parentId) }));+}++/** Entries a teammate already imported and shared with the team. */+export function alreadyInMacro(+ entries: ImportPlanEntry[]+): (ImportPlanEntry & { alreadyImported: ImportedCopy })[] {+ return entries.flatMap((entry) =>+ entry.alreadyImported+ ? [{ ...entry, alreadyImported: entry.alreadyImported }]+ : []+ );+}++/**+ * Orders entries depth-first under their parents. An entry whose parent is+ * not in the plan is a root; a cycle is broken where it is first revisited.+ */+export function buildPlanTree(entries: ImportPlanEntry[]): ImportPlanNode[] {+ const ids = new Set(entries.map((entry) => entry.id));+ const children = new Map<string, ImportPlanEntry[]>();+ const roots: ImportPlanEntry[] = [];+ for (const entry of entries) {+ if (+ entry.parentId &&+ entry.parentId !== entry.id &&+ ids.has(entry.parentId)+ ) {+ const siblings = children.get(entry.parentId) ?? [];+ siblings.push(entry);+ children.set(entry.parentId, siblings);+ } else {+ roots.push(entry);+ }+ }+ const placed = new Set<string>();+ const nodes: ImportPlanNode[] = [];+ const place = (entry: ImportPlanEntry, depth: number) => {+ if (placed.has(entry.id)) return;+ placed.add(entry.id);+ const below = (children.get(entry.id) ?? []).filter(+ (child) => !placed.has(child.id)+ );+ nodes.push({ ...entry, depth, childIds: below.map((child) => child.id) });+ for (const child of below) place(child, depth + 1);+ };+ for (const root of roots) place(root, 0);+ // Entries only reachable through a cycle become roots.+ for (const entry of entries) place(entry, 0);+ return nodes;+}++/** The nodes left showing when the given nodes are collapsed. */+export function visiblePlanNodes(+ nodes: ImportPlanNode[],+ collapsed: ReadonlySet<string>+): ImportPlanNode[] {+ const visible: ImportPlanNode[] = [];+ let hiddenBelow: number | null = null;+ for (const node of nodes) {+ if (hiddenBelow !== null && node.depth > hiddenBelow) continue;+ hiddenBelow = null;+ visible.push(node);+ if (collapsed.has(node.id) && node.childIds.length > 0)+ hiddenBelow = node.depth;+ }+ return visible;+}++/** Nodes with children below the top level start collapsed. */+export function initiallyCollapsed(nodes: ImportPlanNode[]): Set<string> {+ return new Set(+ nodes+ .filter((node) => node.depth > 0 && node.childIds.length > 0)+ .map((node) => node.id)+ );+}++/** The node and everything below it. */+function subtree(nodes: ImportPlanNode[], id: string): string[] {+ const byId = new Map(nodes.map((node) => [node.id, node]));+ const out: string[] = [];+ const visit = (current: string) => {+ const node = byId.get(current);+ if (!node || out.includes(current)) return;+ out.push(current);+ for (const child of node.childIds) visit(child);+ };+ visit(id);+ return out;+}++/**+ * Leaves an object (and the written objects below it) out of the import, or+ * brings them back. Objects that are never written are not recorded.+ */+export function setExcluded(+ draft: ImportDraft,+ nodes: ImportPlanNode[],+ id: string,+ excluded: boolean+): ImportDraft {+ const written = new Set(+ nodes.filter((node) => isWritten(node.decision)).map((node) => node.id)+ );+ const affected = subtree(nodes, id).filter((entry) => written.has(entry));+ const next = draft.excluded.filter((entry) => !affected.includes(entry));+ return {+ ...draft,+ excluded: excluded ? [...next, ...affected] : next,+ };+}++/** Whether the object is shared with the team when it is written. */+export function isShared(entry: ImportPlanEntry, draft: ImportDraft): boolean {+ if (!entry.shareable) return entry.shared;+ if (draft.shared.includes(entry.id)) return true;+ if (draft.personal.includes(entry.id)) return false;+ return entry.shared;+}++/**+ * Shares an object (and the shareable written objects below it) with the+ * team, or keeps them to the owner. Only choices that differ from the plan+ * are recorded.+ */+export function setShared(+ draft: ImportDraft,+ nodes: ImportPlanNode[],+ id: string,+ shared: boolean+): ImportDraft {+ const changeable = new Map(+ nodes+ .filter((node) => node.shareable && isNew(node))+ .map((node) => [node.id, node])+ );+ const affected = subtree(nodes, id).flatMap((entry) => {+ const node = changeable.get(entry);+ return node ? [node] : [];+ });+ const ids = new Set(affected.map((node) => node.id));+ const overrides = affected.filter((node) => node.shared !== shared);+ const added = overrides.map((node) => node.id);+ const sharedIds = draft.shared.filter((entry) => !ids.has(entry));+ const personalIds = draft.personal.filter((entry) => !ids.has(entry));+ return {+ ...draft,+ shared: shared ? [...sharedIds, ...added] : sharedIds,+ personal: shared ? personalIds : [...personalIds, ...added],+ };+}++/**+ * e.g. "12 shared with your team · 3 only you", over the objects approving+ * the plan writes; empty when it writes nothing. Without a team, says why+ * everything stays with the owner.+ */+export function sharingSummary(+ entries: ImportPlanEntry[],+ draft: ImportDraft,+ onTeam: boolean+): string {+ let team = 0;+ let personal = 0;+ for (const entry of entries) {+ if (!isNew(entry) || draft.excluded.includes(entry.id)) continue;+ if (isShared(entry, draft)) team += 1;+ else personal += 1;+ }+ if (!onTeam) {+ return personal > 0+ ? `${personal} only you — you're not on a team, so nothing is shared`+ : '';+ }+ return [+ team > 0 ? `${team} shared with your team` : '',+ personal > 0 ? `${personal} only you` : '',+ ]+ .filter(Boolean)+ .join(' · ');+}++/** e.g. "4 already in Macro (imported by [email protected], [email protected])". */+export function alreadyInMacroSummary(entries: ImportPlanEntry[]): string {+ const copies = alreadyInMacro(entries);+ if (copies.length === 0) return '';+ const by = [...new Set(copies.map((entry) => entry.alreadyImported.by))];+ return `${copies.length} already in Macro (imported by ${by.join(', ')})`;+}++/** Objects the import will write once approved. */+export function importCount(+ entries: ImportPlanEntry[],+ excluded: readonly string[]+): number {+ return entries.filter((entry) => isNew(entry) && !excluded.includes(entry.id))+ .length;+}++/** What approving the plan writes, in real numbers: every row is counted. */+export type ImportVolume = {+ documents: number;+ databases: number;+ rows: number;+ taskLists: number;+ tasks: number;+};++/** The volume the plan writes, leaving out `excluded` entries. */+export function importVolume(+ entries: ImportPlanEntry[],+ excluded: readonly string[]+): ImportVolume {+ const volume: ImportVolume = {+ documents: 0,+ databases: 0,+ rows: 0,+ taskLists: 0,+ tasks: 0,+ };+ for (const entry of entries) {+ if (excluded.includes(entry.id) || entry.alreadyImported) continue;+ match(entry.decision)+ .with('copy', () => {+ if (entry.kind === 'database') {+ volume.databases += 1;+ volume.rows += entry.rows ?? 0;+ } else {+ volume.documents += 1;+ }+ })+ .with('tasks', () => {+ volume.taskLists += 1;+ volume.tasks += entry.rows ?? 0;+ })+ .with('skip', 'hand', null, () => {})+ .exhaustive();+ }+ return volume;+}++/** One line of the plan's totals, e.g. "Databases 18 (405 rows)". */+export type ImportTotal = { label: string; value: string };++/** What the plan holds per kind, leaving out empty kinds. */+export function planTotals(+ entries: ImportPlanEntry[],+ excluded: readonly string[]+): ImportTotal[] {+ const volume = importVolume(entries, excluded);+ const skipped = entries.filter(+ (entry) => entry.decision === 'skip' && entry.alreadyImported === null+ ).length;+ const already = alreadyInMacro(entries).length;+ return [+ volume.documents > 0+ ? { label: 'Documents', value: `${volume.documents}` }+ : null,+ volume.databases > 0+ ? {+ label: 'Databases',+ value: `${volume.databases} (${plural(volume.rows, 'row', 'rows')})`,+ }+ : null,+ volume.taskLists > 0+ ? {+ label: 'Task lists',+ value: `${volume.taskLists} (${plural(volume.tasks, 'task', 'tasks')})`,+ }+ : null,+ skipped > 0 ? { label: 'Skipped', value: `${skipped}` } : null,+ already > 0 ? { label: 'Already in Macro', value: `${already}` } : null,+ ].filter((total): total is ImportTotal => total !== null);+}++/**+ * What planning is doing: reading Notion until the plan is staged, then+ * deciding each object, e.g. "Deciding what to import: 40 of 84 decided".+ */+export function planningStatus(+ progress: ImportProgress | undefined,+ entries: ImportPlanEntry[]+): string {+ if (entries.length === 0) {+ const scanned = progress?.scanned;+ return scanned+ ? `Reading Notion: ${plural(scanned.pages, 'page', 'pages')}, ${plural(scanned.databases, 'database', 'databases')}`+ : 'Reading Notion…';+ }+ const decided = entries.filter((entry) => entry.decision !== null).length;+ return `Deciding what to import: ${decided} of ${entries.length} decided`;+}++/** e.g. "54 pages, 18 databases (405 rows), 33 tasks". */+export function volumeLabel(volume: ImportVolume): string {+ const parts = [+ volume.documents > 0 ? plural(volume.documents, 'page', 'pages') : '',+ volume.databases > 0+ ? `${plural(volume.databases, 'database', 'databases')} (${plural(volume.rows, 'row', 'rows')})`+ : '',+ volume.taskLists > 0 ? plural(volume.tasks, 'task', 'tasks') : '',+ ].filter(Boolean);+ return parts.length > 0 ? parts.join(', ') : 'nothing';+}++export function planCounts(entries: ImportPlanEntry[]): ImportPlanCounts {+ const counts: ImportPlanCounts = {+ pages: 0,+ databases: 0,+ copy: 0,+ tasks: 0,+ skip: 0,+ hand: 0,+ deciding: 0,+ };+ for (const entry of entries) {+ if (entry.kind === 'database') counts.databases += 1;+ else counts.pages += 1;+ counts[entry.decision ?? 'deciding'] += 1;+ }+ return counts;+}++function plural(count: number, one: string, many: string): string {+ return `${count} ${count === 1 ? one : many}`;+}++/**+ * e.g. "Notion shared 56 pages and 24 databases — importing 54 pages,+ * 18 databases (405 rows), 33 tasks · 6 skipped, 1 needs you".+ */+export function planSummary(+ counts: ImportPlanCounts,+ volume: ImportVolume+): string {+ const shared = [+ counts.pages > 0 ? plural(counts.pages, 'page', 'pages') : '',+ counts.databases > 0+ ? plural(counts.databases, 'database', 'databases')+ : '',+ ]+ .filter(Boolean)+ .join(' and ');+ const left = [+ counts.skip > 0 ? `${counts.skip} skipped` : '',+ counts.hand > 0+ ? `${counts.hand} ${counts.hand === 1 ? 'needs' : 'need'} you`+ : '',+ ]+ .filter(Boolean)+ .join(', ');+ return [+ shared ? `Notion shared ${shared}` : '',+ `importing ${volumeLabel(volume)}`,+ ]+ .filter(Boolean)+ .join(' — ')+ .concat(left ? ` · ${left}` : '');+}++/** Progress counted from the plan rows, when the run reports another review. */+export function progressFromEntries(+ reviewId: string,+ entries: ImportPlanEntry[]+): ImportProgress {+ const pending = entries.filter(+ (entry) => entry.status === 'staged' && isWritten(entry.decision)+ );+ return {+ reviewId,+ status: null,+ error: null,+ scanned: null,+ onTeam: true,+ counts: planCounts(entries),+ imported: entries.filter((entry) => entry.status === 'imported').length,+ waiting: pending.length,+ writing: entries.filter((entry) => entry.status === 'importing').length,+ failures: pending+ .filter((entry) => entry.error)+ .map((entry) => ({+ id: entry.id,+ title: entry.title,+ reason: entry.error ?? '',+ })),+ };+}++/**+ * The review item decides whether the plan was approved; once it was, the+ * run's status says how the write is going.+ */+export function importReviewPhase(+ review: Pick<Review, 'presentedAt'>,+ item: Pick<ReviewItem, 'status' | 'error'> | undefined,+ progress: ImportProgress | undefined+): ImportReviewPhase {+ if (review.presentedAt == null || !item) return { kind: 'preparing' };+ return match(item.status)+ .returnType<ImportReviewPhase>()+ .with('pending', () =>+ progress?.status === 'running' ? { kind: 'planning' } : { kind: 'plan' }+ )+ .with('submitting', () => ({ kind: 'starting' }))+ .with('dismissed', () => ({ kind: 'dismissed' }))+ .with('unknown', () => ({ kind: 'unknown', error: item.error ?? null }))+ .with('completed', () =>+ match(progress?.status ?? null)+ .returnType<ImportReviewPhase>()+ .with('completed', () => ({ kind: 'completed' }))+ .with('dismissed', () => ({ kind: 'stopped' }))+ .with('failed', () => ({+ kind: 'failed',+ error: progress?.error ?? 'The import failed.',+ }))+ .with('importing', 'running', 'ready', () => ({ kind: 'importing' }))+ // Progress without a run status was counted from the rows because a+ // newer run replaced this one, so this run has finished.+ .with(null, () =>+ progress ? { kind: 'completed' } : { kind: 'importing' }+ )+ .exhaustive()+ )+ .exhaustive();+}diff --git a/apps/web/src/features/agent-reviews/core/review.ts b/apps/web/src/features/agent-reviews/core/review.tsindex d2b72e0661..89bee4e765 100644--- a/apps/web/src/features/agent-reviews/core/review.ts+++ b/apps/web/src/features/agent-reviews/core/review.ts@@ -4,7 +4,7 @@ export type Review = {sessionId: string;actingUser?: string | null;executionId?: string | null;- kind: 'email' | 'calendar';+ kind: 'email' | 'calendar' | 'import';title: string;revision: number;updatedAt: number;@@ -27,3 +27,15 @@ export type ReviewActions = {reload: () => Promise<Review>;decide: (decision: ReviewDecision) => Promise<Review>;};++/**+ * Whether the agent answers a decision as soon as it is made. Approving an+ * import only starts its write; the agent answers when the write settles.+ */+export function decisionWakesAgent(+ kind: Review['kind'],+ decision: ReviewDecision+): boolean {+ if (decision.action === 'edit') return false;+ return !(kind === 'import' && decision.action === 'submit');+}diff --git a/apps/web/src/features/agent-reviews/primitives/import-review.ts b/apps/web/src/features/agent-reviews/primitives/import-review.tsnew file mode 100644index 0000000000..86def179f5--- /dev/null+++ b/apps/web/src/features/agent-reviews/primitives/import-review.ts@@ -0,0 +1,193 @@+import { type Accessor, createMemo, createSignal } from 'solid-js';+import {+ alreadyInMacro,+ alreadyInMacroSummary,+ buildPlanTree,+ EMPTY_IMPORT_DRAFT,+ type ImportDraft,+ type ImportPlanEntry,+ type ImportProgress,+ importCount,+ importReviewPhase,+ importVolume,+ initiallyCollapsed,+ isShared,+ planCounts,+ planningStatus,+ planSummary,+ planTotals,+ planTreeEntries,+ progressFromEntries,+ setExcluded,+ setShared,+ sharingSummary,+ visiblePlanNodes,+} from '../core/import-plan';+import type { Review, ReviewActions } from '../core/review';+import { decodeImportDraft } from '../queries/import-plan';+import { createReviewController } from './review-controller';+import { validateReviewDraft } from './review-draft-validation';++export type ImportReviewSources = {+ review: Accessor<Review>;+ actions: ReviewActions;+ /** The plan rows this review presents. */+ entries: Accessor<ImportPlanEntry[]>;+ /** The run's latest report; `undefined` until it has loaded. */+ report: Accessor<ImportProgress | undefined>;+ canAct: Accessor<boolean>;+ /** Stops the run; resolves `false` when the request failed. */+ stop: () => Promise<boolean>;+};++/**+ * One import review: the plan the owner trims and approves, then the run's+ * progress. Edits go through the review controller, which serializes saves.+ */+export function createImportReview(sources: ImportReviewSources) {+ const controller = createReviewController(+ sources.review,+ sources.actions,+ validateReviewDraft+ );+ const record = controller.record;+ const item = () => record().items[0];+ // Shown until the review is reloaded; the controller saves it in order.+ const [editedDraft, setEditedDraft] = createSignal<ImportDraft>();+ const draft = (): ImportDraft =>+ editedDraft() ?? decodeImportDraft(item()?.draft) ?? EMPTY_IMPORT_DRAFT;+ const nodes = createMemo(() =>+ buildPlanTree(planTreeEntries(sources.entries()))+ );+ const entriesById = createMemo(+ () => new Map(sources.entries().map((entry) => [entry.id, entry]))+ );+ const [collapsed, setCollapsed] = createSignal<ReadonlySet<string>>();+ // Databases whose sample rows are showing; kept here so a refreshed plan+ // keeps them open.+ const [showingRows, setShowingRows] = createSignal<ReadonlySet<string>>(+ new Set()+ );+ const collapsedIds = () => collapsed() ?? initiallyCollapsed(nodes());+ const [stopping, setStopping] = createSignal(false);+ const [stopFailed, setStopFailed] = createSignal(false);++ const progress = (): ImportProgress | undefined => {+ const report = sources.report();+ if (!report) return undefined;+ if (report.reviewId === record().id) return report;+ // A newer run replaced this one; its rows still say how it ended.+ return item()?.status === 'completed'+ ? progressFromEntries(record().id, sources.entries())+ : undefined;+ };+ const phase = () => importReviewPhase(record(), item(), progress());+ const locked = () =>+ !sources.canAct() ||+ controller.busy() ||+ Boolean(controller.error()) ||+ controller.stale() ||+ record().presentedAt == null;+ const editable = () => !locked() && phase().kind === 'plan';+ const count = () => importCount(sources.entries(), draft().excluded);+ const volume = () => importVolume(sources.entries(), draft().excluded);++ function setIncluded(id: string, included: boolean) {+ const current = item();+ if (!current || !editable()) return;+ const next = setExcluded(draft(), nodes(), id, !included);+ setEditedDraft(next);+ controller.edit(current.id, next);+ }+ function setSharedWithTeam(id: string, shared: boolean) {+ const current = item();+ if (!current || !editable()) return;+ const next = setShared(draft(), nodes(), id, shared);+ setEditedDraft(next);+ controller.edit(current.id, next);+ }+ function toggleRows(id: string) {+ const next = new Set(showingRows());+ if (next.has(id)) next.delete(id);+ else next.add(id);+ setShowingRows(next);+ }+ function toggleExpanded(id: string) {+ const next = new Set(collapsedIds());+ if (next.has(id)) next.delete(id);+ else next.add(id);+ setCollapsed(next);+ }+ async function approve(): Promise<boolean> {+ if (!editable() || count() === 0) return false;+ return controller.decide('submit', []);+ }+ async function dismiss(): Promise<boolean> {+ if (!editable()) return false;+ return controller.decide('dismiss', []);+ }+ async function stop(): Promise<boolean> {+ if (!sources.canAct() || stopping()) return false;+ setStopping(true);+ setStopFailed(false);+ try {+ const stopped = await sources.stop();+ setStopFailed(!stopped);+ return stopped;+ } finally {+ setStopping(false);+ }+ }+ async function reload(): Promise<boolean> {+ const reloaded = await controller.reload();+ if (reloaded) setEditedDraft(undefined);+ return reloaded;+ }++ return {+ title: () => record().title,+ phase,+ progress,+ summary: () =>+ planSummary(+ progress()?.counts ?? planCounts(sources.entries()),+ volume()+ ),+ sharingSummary: () =>+ sharingSummary(+ sources.entries(),+ draft(),+ sources.report()?.onTeam ?? true+ ),+ totals: () => planTotals(sources.entries(), draft().excluded),+ planningStatus: () => planningStatus(progress(), sources.entries()),+ alreadyInMacroSummary: () => alreadyInMacroSummary(sources.entries()),+ alreadyInMacro: () => alreadyInMacro(sources.entries()),+ nodes: () => visiblePlanNodes(nodes(), collapsedIds()),+ hasPlan: () => nodes().length > 0,+ isCollapsed: (id: string) => collapsedIds().has(id),+ isShowingRows: (id: string) => showingRows().has(id),+ isExcluded: (id: string) => draft().excluded.includes(id),+ isShared: (id: string) => {+ const entry = entriesById().get(id);+ return entry ? isShared(entry, draft()) : false;+ },+ count,+ volume,+ editable,+ working: controller.busy,+ stopping,+ stopFailed,+ error: controller.error,+ validation: () => controller.validation()?.message,+ stale: controller.stale,+ setIncluded,+ setShared: setSharedWithTeam,+ toggleExpanded,+ toggleRows,+ approve,+ dismiss,+ stop,+ reload,+ };+}diff --git a/apps/web/src/features/agent-reviews/primitives/review-draft-validation.ts b/apps/web/src/features/agent-reviews/primitives/review-draft-validation.tsindex e1d4b51191..7bdb11d119 100644--- a/apps/web/src/features/agent-reviews/primitives/review-draft-validation.ts+++ b/apps/web/src/features/agent-reviews/primitives/review-draft-validation.ts@@ -4,52 +4,66 @@ import {SendEmail,} from '@service-cognition/generated/tools/schemas';import * as EmailValidator from 'email-validator';+import { match } from 'ts-pattern';import type { Review, ReviewItem } from '../core/review';+import { decodeImportDraft } from '../queries/import-plan';/** Preflight every selected draft before a batch can start external actions. */export function validateReviewDraft(item: ReviewItem,kind: Review['kind']): string | undefined {- if (kind === 'email') {- const parsed = SendEmail.safeParse(item.draft);- if (!parsed.success)- return 'This email has invalid fields. Review it before sending.';- const draft = parsed.data;- if (draft.to.length === 0) return 'Add at least one recipient.';- if (- [...draft.to, ...(draft.cc ?? []), ...(draft.bcc ?? [])].some(- (recipient) => !EmailValidator.validate(recipient.email)- )+ return match(kind)+ .with('email', () => validateEmailDraft(item.draft))+ .with('calendar', () => validateCalendarDraft(item.draft))+ .with('import', () =>+ decodeImportDraft(item.draft)+ ? undefined+ : 'This import plan cannot be read. Reload it before importing.')- return 'Correct the email addresses before sending.';- const decoded = decodeBase64Utf8(draft.body);- const content =- decoded !== draft.body && /^\s*</.test(decoded)- ? new DOMParser().parseFromString(decoded, 'text/html').body.textContent- : draft.body;- if (!content?.trim()) return 'Write a message before sending.';- } else if (kind === 'calendar') {- const parsed = CreateCalendarEvent.safeParse(item.draft);- if (!parsed.success)- return 'This event has invalid fields. Review it before creating.';- const draft = parsed.data;- if (!draft.title.trim()) return 'Give this event a title.';- const time = draft.time;- if (!time) return 'Choose the event start and end.';- const start = Date.parse(- time.kind === 'timed' ? time.startsAt : time.startDate- );- const end = Date.parse(time.kind === 'timed' ? time.endsAt : time.endDate);- if (!Number.isFinite(start) || !Number.isFinite(end) || end <= start)- return 'The event must end after it starts.';- if (draft.addMacroCall && draft.addConference)- return 'Choose either a Macro call or a video meeting.';- if (- (draft.attendees ?? []).some(- (guest) => !EmailValidator.validate(guest.email)- )+ .exhaustive();+}++function validateEmailDraft(value: unknown): string | undefined {+ const parsed = SendEmail.safeParse(value);+ if (!parsed.success)+ return 'This email has invalid fields. Review it before sending.';+ const draft = parsed.data;+ if (draft.to.length === 0) return 'Add at least one recipient.';+ if (+ [...draft.to, ...(draft.cc ?? []), ...(draft.bcc ?? [])].some(+ (recipient) => !EmailValidator.validate(recipient.email)+ )+ )+ return 'Correct the email addresses before sending.';+ const decoded = decodeBase64Utf8(draft.body);+ const content =+ decoded !== draft.body && /^\s*</.test(decoded)+ ? new DOMParser().parseFromString(decoded, 'text/html').body.textContent+ : draft.body;+ if (!content?.trim()) return 'Write a message before sending.';+}++function validateCalendarDraft(value: unknown): string | undefined {+ const parsed = CreateCalendarEvent.safeParse(value);+ if (!parsed.success)+ return 'This event has invalid fields. Review it before creating.';+ const draft = parsed.data;+ if (!draft.title.trim()) return 'Give this event a title.';+ const time = draft.time;+ if (!time) return 'Choose the event start and end.';+ const start = Date.parse(+ time.kind === 'timed' ? time.startsAt : time.startDate+ );+ const end = Date.parse(time.kind === 'timed' ? time.endsAt : time.endDate);+ if (!Number.isFinite(start) || !Number.isFinite(end) || end <= start)+ return 'The event must end after it starts.';+ if (draft.addMacroCall && draft.addConference)+ return 'Choose either a Macro call or a video meeting.';+ if (+ (draft.attendees ?? []).some(+ (guest) => !EmailValidator.validate(guest.email))- return 'Correct the guest email addresses before creating.';- }+ )+ return 'Correct the guest email addresses before creating.';}diff --git a/apps/web/src/features/agent-reviews/queries/import-plan.test.ts b/apps/web/src/features/agent-reviews/queries/import-plan.test.tsnew file mode 100644index 0000000000..78fec93616--- /dev/null+++ b/apps/web/src/features/agent-reviews/queries/import-plan.test.ts@@ -0,0 +1,154 @@+import type { ImportEntity } from '@queries/import';+import { describe, expect, it } from 'vitest';+import { decodeImportDraft, decodePlanEntries } from './import-plan';++const row: ImportEntity = {+ id: 'roadmap',+ user_id: 'owner',+ source: 'notion',+ kind: 'data_source',+ foreign_id: 'notion-roadmap',+ parent_id: 'wiki',+ review_id: 'review',+ status: 'staged',+ initiator: 'chat',+ metadata: {+ title: 'Roadmap',+ url: 'https://notion.so/roadmap',+ plan: {+ decision: 'tasks',+ reason: 'Rows have a status',+ path: ['Wiki'],+ rows: 40,+ sampleTitles: ['Fix login', 'Ship billing'],+ taskFields: { status: 'Status' },+ },+ },+ last_error: 'Rate limited',+ created_at: '2026-10-19T00:00:00Z',+ updated_at: '2026-10-19T00:00:00Z',+};++describe('decodePlanEntries', () => {+ it("keeps this review's Notion plan rows", () => {+ expect(+ decodePlanEntries(+ [+ row,+ { ...row, id: 'older', review_id: 'older-review' },+ { ...row, id: 'single', metadata: { title: 'Staged alone' } },+ { ...row, id: 'slack', source: 'slack' },+ ],+ 'review'+ )+ ).toEqual([+ {+ id: 'roadmap',+ parentId: 'wiki',+ kind: 'database',+ title: 'Roadmap',+ decision: 'tasks',+ reason: 'Rows have a status',+ rows: 40,+ sampleTitles: ['Fix login', 'Ship billing'],+ taskFields: {+ status: 'Status',+ priority: null,+ due: null,+ assignee: null,+ },+ status: 'staged',+ error: 'Rate limited',+ shared: false,+ sharedReason: null,+ shareable: false,+ alreadyImported: null,+ },+ ]);+ });++ it('reads sharing and a teammate copy', () => {+ expect(+ decodePlanEntries(+ [+ {+ ...row,+ kind: 'page',+ metadata: {+ title: 'Wiki',+ plan: {+ decision: 'copy',+ path: [],+ shared: true,+ sharedReason: 'Team handbook',+ shareable: true,+ alreadyImported: {+ by: '[email protected]',+ entityId: 'doc-1',+ entityType: 'md',+ },+ },+ },+ },+ ],+ 'review'+ )+ ).toMatchObject([+ {+ id: 'roadmap',+ shared: true,+ sharedReason: 'Team handbook',+ shareable: true,+ alreadyImported: {+ by: '[email protected]',+ entityId: 'doc-1',+ entityType: 'md',+ },+ },+ ]);+ });+});++describe('decodePlanEntries while planning', () => {+ it('reads a row the decision model has not answered for yet', () => {+ expect(+ decodePlanEntries(+ [+ {+ ...row,+ metadata: {+ title: 'Roadmap',+ plan: { path: [], rows: 40, shared: false, shareable: false },+ },+ },+ ],+ 'review'+ )+ ).toMatchObject([{ id: 'roadmap', decision: null, rows: 40 }]);+ });+});++describe('decodeImportDraft', () => {+ it('reads the excluded and re-shared rows and rejects other drafts', () => {+ expect(decodeImportDraft({ source: 'notion' })).toEqual({+ source: 'notion',+ excluded: [],+ shared: [],+ personal: [],+ });+ expect(+ decodeImportDraft({+ source: 'notion',+ excluded: ['roadmap'],+ shared: ['wiki'],+ personal: ['q3'],+ })+ ).toEqual({+ source: 'notion',+ excluded: ['roadmap'],+ shared: ['wiki'],+ personal: ['q3'],+ });+ expect(decodeImportDraft({ subject: 'Hi' })).toBeUndefined();+ });+});diff --git a/apps/web/src/features/agent-reviews/queries/import-plan.ts b/apps/web/src/features/agent-reviews/queries/import-plan.tsnew file mode 100644index 0000000000..55d3a3df4c--- /dev/null+++ b/apps/web/src/features/agent-reviews/queries/import-plan.ts@@ -0,0 +1,128 @@+import type { ImportEntity, NotionImportReport } from '@queries/import';+import { z } from 'zod';+import type {+ ImportDraft,+ ImportPlanEntry,+ ImportProgress,+} from '../core/import-plan';++const importDraftSchema = z.object({+ source: z.literal('notion'),+ excluded: z.array(z.string()).optional(),+ shared: z.array(z.string()).optional(),+ personal: z.array(z.string()).optional(),+});++/** The saved draft of an import review; `undefined` when it is not one. */+export function decodeImportDraft(draft: unknown): ImportDraft | undefined {+ const parsed = importDraftSchema.safeParse(draft);+ if (!parsed.success) return undefined;+ return {+ source: 'notion',+ excluded: parsed.data.excluded ?? [],+ shared: parsed.data.shared ?? [],+ personal: parsed.data.personal ?? [],+ };+}++const planMetadataSchema = z.object({+ title: z.string(),+ plan: z.object({+ // Absent while planning has not decided the object yet.+ decision: z.enum(['copy', 'tasks', 'skip', 'hand']).nullish(),+ reason: z.string().nullish(),+ rows: z.number().nullish(),+ sampleTitles: z.array(z.string()).nullish(),+ taskFields: z+ .object({+ status: z.string().nullish(),+ priority: z.string().nullish(),+ due: z.string().nullish(),+ assignee: z.string().nullish(),+ })+ .nullish(),+ shared: z.boolean().nullish(),+ sharedReason: z.string().nullish(),+ shareable: z.boolean().nullish(),+ alreadyImported: z+ .object({+ by: z.string(),+ entityId: z.string(),+ entityType: z.string(),+ })+ .nullish(),+ }),+});++/** The Notion plan rows presented by one review, in ledger order. */+export function decodePlanEntries(+ entities: ImportEntity[],+ reviewId: string+): ImportPlanEntry[] {+ return entities.flatMap((entity): ImportPlanEntry[] => {+ if (entity.source !== 'notion' || entity.review_id !== reviewId) return [];+ const metadata = planMetadataSchema.safeParse(entity.metadata);+ if (!metadata.success) return [];+ const plan = metadata.data.plan;+ return [+ {+ id: entity.id,+ parentId: entity.parent_id ?? null,+ kind: entity.kind === 'data_source' ? 'database' : 'page',+ title: metadata.data.title,+ decision: plan.decision ?? null,+ reason: plan.reason ?? null,+ rows: plan.rows ?? null,+ sampleTitles: plan.sampleTitles ?? [],+ taskFields: plan.taskFields+ ? {+ status: plan.taskFields.status ?? null,+ priority: plan.taskFields.priority ?? null,+ due: plan.taskFields.due ?? null,+ assignee: plan.taskFields.assignee ?? null,+ }+ : null,+ status: entity.status,+ error: entity.last_error ?? null,+ shared: plan.shared ?? false,+ sharedReason: plan.sharedReason ?? null,+ shareable: plan.shareable ?? false,+ alreadyImported: plan.alreadyImported ?? null,+ },+ ];+ });+}++export function decodeImportProgress(+ report: NotionImportReport+): ImportProgress {+ return {+ reviewId: report.reviewId ?? null,+ status: report.status ?? null,+ error: report.error ?? null,+ scanned: report.scanned+ ? {+ pages: report.scanned.pages,+ databases: report.scanned.dataSources,+ }+ : null,+ onTeam: report.onTeam ?? false,+ counts: {+ pages: report.pages,+ databases: report.dataSources,+ copy: report.copy,+ tasks: report.tasks,+ skip: report.skip,+ hand: report.hand,+ deciding: report.deciding ?? 0,+ },+ imported: report.imported,+ waiting: report.waiting,+ writing: report.writing,+ failures: (report.failures ?? []).map((item) => ({+ id: item.importId,+ title: item.title,+ reason: item.reason ?? '',+ })),+ };+}diff --git a/apps/web/src/features/agent-reviews/views/import-review.test.tsx b/apps/web/src/features/agent-reviews/views/import-review.test.tsxnew file mode 100644index 0000000000..8942c3f059--- /dev/null+++ b/apps/web/src/features/agent-reviews/views/import-review.test.tsx@@ -0,0 +1,612 @@+/** @vitest-environment jsdom */+import type { ImportEntity, NotionImportReport } from '@queries/import';+import {+ cleanup,+ fireEvent,+ render,+ screen,+ waitFor,+ within,+} from '@solidjs/testing-library';+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';+import type { Review, ReviewDecision, ReviewItem } from '../core/review';+import { ImportReview } from './import-review';++const imports = vi.hoisted(() => ({+ entities: [] as ImportEntity[],+ report: undefined as NotionImportReport | undefined,+ cancel: vi.fn(),+ open: vi.fn(),+}));+vi.mock('@components/app/split-layout/layout', () => ({+ useSplitLayout: () => ({ openWithSplit: imports.open }),+}));+vi.mock('@core/mobile/isMobile', () => ({ isMobile: () => false }));+// The real icon table loads every block, which opens app connections.+vi.mock('@core/component/EntityIcon', () => ({+ EntityIcon: (props: { targetType: string }) => (+ <span data-icon={props.targetType} />+ ),+}));+vi.mock('@queries/import', () => ({+ useImportQuery: () => ({+ isPending: false,+ isPlaceholderData: false,+ data: { runs: [], entities: imports.entities },+ }),+ useNotionImportReportQuery: () => ({+ get isPending() {+ return imports.report === undefined;+ },+ get data() {+ return imports.report;+ },+ }),+ useCancelNotionImportMutation: () => ({ mutateAsync: imports.cancel }),+}));++function entity(+ id: string,+ parentId: string | null,+ title: string,+ plan: {+ decision?: string;+ reason?: string;+ rows?: number;+ sampleTitles?: string[];+ taskFields?: { status?: string; due?: string };+ shared?: boolean;+ sharedReason?: string;+ shareable?: boolean;+ alreadyImported?: { by: string; entityId: string; entityType: string };+ },+ overrides: Partial<ImportEntity> = {}+): ImportEntity {+ return {+ id,+ user_id: 'owner',+ source: 'notion',+ kind: plan.rows === undefined ? 'page' : 'data_source',+ foreign_id: `notion-${id}`,+ parent_id: parentId,+ review_id: 'review',+ status: 'staged',+ initiator: 'chat',+ metadata: {+ title,+ url: `https://notion.so/${id}`,+ plan: { path: [], ...plan },+ },+ created_at: '2026-10-19T00:00:00Z',+ updated_at: '2026-10-19T00:00:00Z',+ ...overrides,+ };+}++beforeEach(() => {+ imports.entities = [+ entity('wiki', null, 'Wiki', { decision: 'copy' }),+ entity('roadmap', 'wiki', 'Roadmap', {+ decision: 'tasks',+ reason: 'Rows have a status and an owner',+ rows: 40,+ }),+ entity('scratch', 'wiki', 'Scratch', {+ decision: 'skip',+ reason: 'Empty',+ }),+ entity('billing', null, 'Billing', {+ decision: 'hand',+ reason: 'Holds credentials',+ }),+ entity(+ 'other',+ null,+ 'Other review',+ { decision: 'copy' },+ {+ review_id: 'older-review',+ }+ ),+ ];+ imports.report = undefined;+ imports.cancel.mockReset();+ imports.open.mockReset();+ vi.stubGlobal('scrollTo', vi.fn());+ vi.stubGlobal(+ 'ResizeObserver',+ class {+ observe() {}+ unobserve() {}+ disconnect() {}+ }+ );+});+afterEach(() => {+ cleanup();+ vi.unstubAllGlobals();+});++function setup(+ options: {+ item?: Partial<ReviewItem>;+ canAct?: boolean;+ review?: Partial<Review>;+ } = {}+) {+ let record: Review = {+ id: 'review',+ sessionId: 'session',+ kind: 'import',+ title: 'Import from Notion',+ revision: 0,+ updatedAt: 0,+ presentedAt: 1,+ items: [+ {+ id: 'plan',+ status: 'pending',+ draft: { source: 'notion' },+ ...options.item,+ },+ ],+ ...options.review,+ };+ const decide = vi.fn(async (decision: ReviewDecision) => {+ if (decision.revision !== record.revision) throw new Error('conflict');+ record = {+ ...record,+ revision: record.revision + 1,+ items: record.items.map((item) =>+ decision.action === 'edit'+ ? { ...item, draft: decision.draft }+ : {+ ...item,+ status: decision.action === 'submit' ? 'submitting' : 'dismissed',+ }+ ),+ };+ return record;+ });+ render(() => (+ <ImportReview+ review={record}+ canAct={options.canAct ?? true}+ actions={{ decide, reload: async () => record }}+ />+ ));+ return decide;+}++describe('import review', () => {+ it('saves what the owner leaves out and imports the rest', async () => {+ const decide = setup();+ expect(+ screen.getByText(+ 'Notion shared 3 pages and 1 database — importing 1 page, 40 tasks · 1 skipped, 1 needs you'+ )+ ).toBeTruthy();+ expect(screen.queryByText('Other review')).toBeNull();+ expect(screen.getByText('Holds credentials')).toBeTruthy();+ expect(+ screen.getByRole<HTMLInputElement>('checkbox', { name: 'Import Scratch' })+ .disabled+ ).toBe(true);++ fireEvent.click(screen.getByRole('checkbox', { name: 'Import Roadmap' }));+ expect(screen.getByRole('button', { name: 'Import 1 page' })).toBeTruthy();+ await waitFor(() =>+ expect(decide).toHaveBeenCalledWith({+ action: 'edit',+ revision: 0,+ item: 'plan',+ draft: {+ source: 'notion',+ excluded: ['roadmap'],+ shared: [],+ personal: [],+ },+ })+ );++ // The card's button approves; there is no confirmation step.+ fireEvent.click(screen.getByRole('button', { name: 'Import 1 page' }));+ await waitFor(() =>+ expect(decide).toHaveBeenLastCalledWith({+ action: 'submit',+ revision: 1,+ items: [],+ })+ );+ expect(await screen.findByText('Starting the import…')).toBeTruthy();+ expect(screen.getByRole('button', { name: 'Stop' })).toBeTruthy();+ expect(screen.queryByRole('checkbox')).toBeNull();+ });++ it('dismisses the plan without importing', async () => {+ const decide = setup();+ fireEvent.click(screen.getByRole('button', { name: 'Dismiss' }));+ await waitFor(() =>+ expect(decide).toHaveBeenCalledWith({+ action: 'dismiss',+ revision: 0,+ items: [],+ })+ );+ });++ it('hides the choices from viewers who cannot act', () => {+ setup({ canAct: false });+ expect(screen.getByText('Roadmap')).toBeTruthy();+ expect(screen.queryByRole('checkbox')).toBeNull();+ expect(screen.queryByRole('button', { name: /Import \d/ })).toBeNull();+ expect(screen.queryByRole('button', { name: 'Dismiss' })).toBeNull();+ });++ it('shows progress and failures once approved, and stops the run', async () => {+ imports.report = {+ status: 'importing',+ pages: 3,+ dataSources: 1,+ copy: 1,+ tasks: 1,+ skip: 1,+ hand: 1,+ documents: 1,+ databases: 1,+ databaseRows: 0,+ taskRows: 0,+ imported: 1,+ waiting: 2,+ writing: 1,+ reviewId: 'review',+ handItems: [],+ failures: [+ {+ importId: 'roadmap',+ title: 'Roadmap',+ kind: 'data_source',+ reason: 'Notion rate limited the request',+ },+ ],+ };+ imports.cancel.mockResolvedValue({+ ...imports.report,+ status: 'dismissed',+ });+ setup({ item: { status: 'completed' } });++ expect(screen.getByRole('progressbar').getAttribute('aria-valuenow')).toBe(+ '1'+ );+ expect(screen.getByText(/1 of 4 imported/)).toBeTruthy();+ expect(screen.getByText(/1 writing/)).toBeTruthy();+ expect(screen.getByText(/Notion rate limited the request/)).toBeTruthy();+ expect(screen.queryByRole('checkbox')).toBeNull();++ fireEvent.click(screen.getByRole('button', { name: 'Stop' }));+ await waitFor(() => expect(imports.cancel).toHaveBeenCalledTimes(1));+ });++ it("says when the agent started the import at the owner's request", () => {+ setup({+ item: { status: 'submitting' },+ review: { decision: { via: 'chat' } },+ });+ expect(+ screen.getByText('Started by the agent at your request')+ ).toBeTruthy();+ cleanup();++ setup({ item: { status: 'submitting' } });+ expect(screen.queryByText(/Started by the agent/)).toBeNull();+ });++ it('says what stays after the run was stopped', () => {+ imports.report = {+ status: 'dismissed',+ error: 'Cancelled',+ pages: 3,+ dataSources: 1,+ copy: 1,+ tasks: 1,+ skip: 1,+ hand: 1,+ documents: 1,+ databases: 1,+ databaseRows: 0,+ taskRows: 0,+ imported: 2,+ waiting: 1,+ writing: 0,+ reviewId: 'review',+ handItems: [],+ failures: [],+ };+ setup({ item: { status: 'completed' } });+ expect(+ screen.getByText('Import stopped — 2 imported objects stay')+ ).toBeTruthy();+ expect(screen.queryByRole('button', { name: 'Stop' })).toBeNull();+ });++ it('saves who each object is shared with, down the tree', async () => {+ imports.entities = [+ entity('wiki', null, 'Wiki', {+ decision: 'copy',+ shared: true,+ shareable: true,+ sharedReason: 'Team handbook',+ }),+ entity('guide', 'wiki', 'Guide', {+ decision: 'copy',+ shared: true,+ shareable: true,+ }),+ entity('roadmap', 'wiki', 'Roadmap', {+ decision: 'tasks',+ rows: 40,+ shared: false,+ shareable: false,+ sharedReason: 'Personal notes',+ }),+ entity('billing', null, 'Billing', {+ decision: 'hand',+ reason: 'Holds credentials',+ }),+ ];+ const decide = setup();+ expect(+ screen.getByText('2 shared with your team · 1 only you')+ ).toBeTruthy();+ const plan = screen.getByRole('list', { name: 'Import plan' });+ expect(within(plan).getByText('Only you')).toBeTruthy();+ expect(+ screen.queryByRole('button', { name: /Roadmap with your team/ })+ ).toBeNull();++ const wiki = screen.getByRole('button', {+ name: 'Make Wiki only visible to you',+ });+ expect(wiki.getAttribute('aria-pressed')).toBe('true');+ fireEvent.click(wiki);+ await waitFor(() =>+ expect(decide).toHaveBeenLastCalledWith({+ action: 'edit',+ revision: 0,+ item: 'plan',+ draft: {+ source: 'notion',+ excluded: [],+ shared: [],+ personal: ['wiki', 'guide'],+ },+ })+ );+ expect(screen.getByText('3 only you')).toBeTruthy();+ expect(+ screen+ .getByRole('button', { name: 'Share Guide with your team' })+ .getAttribute('aria-pressed')+ ).toBe('false');++ fireEvent.click(+ screen.getByRole('button', { name: 'Share Guide with your team' })+ );+ await waitFor(() =>+ expect(decide).toHaveBeenLastCalledWith({+ action: 'edit',+ revision: 1,+ item: 'plan',+ draft: {+ source: 'notion',+ excluded: [],+ shared: [],+ personal: ['wiki'],+ },+ })+ );++ fireEvent.click(+ screen.getByRole('button', { name: 'Share Wiki with your team' })+ );+ await waitFor(() =>+ expect(decide).toHaveBeenLastCalledWith({+ action: 'edit',+ revision: 2,+ item: 'plan',+ draft: { source: 'notion', excluded: [], shared: [], personal: [] },+ })+ );++ expect(+ screen.getByText('2 shared with your team · 1 only you')+ ).toBeTruthy();+ });++ it('lists what teammates already imported instead of importing it again', async () => {+ imports.entities = [+ entity('wiki', null, 'Wiki', {+ decision: 'copy',+ shared: true,+ shareable: true,+ alreadyImported: {+ by: '[email protected]',+ entityId: 'doc-wiki',+ entityType: 'md',+ },+ }),+ entity('notes', 'wiki', 'Notes', { decision: 'copy' }),+ entity('crm', null, 'CRM', {+ decision: 'copy',+ rows: 12,+ alreadyImported: {+ by: '[email protected]',+ entityId: 'db-crm',+ entityType: 'database',+ },+ }),+ entity('old', null, 'Old', {+ decision: 'copy',+ alreadyImported: {+ by: '[email protected]',+ entityId: 'legacy',+ entityType: 'no-such-type',+ },+ }),+ ];+ setup();+ const plan = screen.getByRole('list', { name: 'Import plan' });+ expect(within(plan).getByText('Notes')).toBeTruthy();+ expect(within(plan).queryByText('Wiki')).toBeNull();+ expect(screen.getByRole('button', { name: 'Import 1 page' })).toBeTruthy();++ const disclosure = screen.getByRole('button', {+ name: '3 already in Macro (imported by [email protected], [email protected])',+ });+ expect(disclosure.getAttribute('aria-expanded')).toBe('false');+ expect(screen.queryByRole('link', { name: 'Wiki' })).toBeNull();++ fireEvent.click(disclosure);+ expect(disclosure.getAttribute('aria-expanded')).toBe('true');+ const section = screen.getByRole('region', { name: 'Already in Macro' });+ expect(within(section).getAllByText('by [email protected]')).toHaveLength(2);+ expect(within(section).getByText('Old')).toBeTruthy();+ expect(within(section).queryByRole('link', { name: 'Old' })).toBeNull();+ expect(+ within(section).getByRole('link', { name: 'CRM' }).getAttribute('href')+ ).toMatch(/\/app\/database\/db-crm$/);+ const link = within(section).getByRole('link', { name: 'Wiki' });+ expect(link.getAttribute('href')).toMatch(/\/app\/md\/doc-wiki$/);+ fireEvent.click(link);+ expect(imports.open).toHaveBeenCalledWith(+ { type: 'md', id: 'doc-wiki' },+ expect.anything()+ );+ });++ it('fills in while it is planned and opens for approval once ready', () => {+ imports.entities = [];+ const planning: NotionImportReport = {+ status: 'running',+ reviewId: 'review',+ onTeam: true,+ scanned: { pages: 40, dataSources: 12 },+ pages: 0,+ dataSources: 0,+ copy: 0,+ tasks: 0,+ skip: 0,+ hand: 0,+ documents: 0,+ databases: 0,+ databaseRows: 0,+ taskRows: 0,+ imported: 0,+ waiting: 0,+ writing: 0,+ };+ imports.report = planning;+ setup();+ expect(+ screen.getByText('Reading Notion: 40 pages, 12 databases')+ ).toBeTruthy();+ expect(+ screen.getByRole<HTMLButtonElement>('button', { name: 'Import' }).disabled+ ).toBe(true);+ cleanup();++ // Staged, and one of two decided.+ imports.entities = [+ entity('wiki', null, 'Wiki', { decision: 'copy' }),+ entity('roadmap', 'wiki', 'Roadmap', { rows: 40 }),+ ];+ imports.report = { ...planning, pages: 1, dataSources: 1 };+ setup();+ expect(+ screen.getByText('Deciding what to import: 1 of 2 decided')+ ).toBeTruthy();+ expect(screen.getByText('Deciding…')).toBeTruthy();+ expect(+ screen.getByRole<HTMLButtonElement>('button', { name: 'Import' }).disabled+ ).toBe(true);+ expect(+ screen.getByRole<HTMLInputElement>('checkbox', { name: 'Import Wiki' })+ .disabled+ ).toBe(true);+ cleanup();++ imports.entities = [+ entity('wiki', null, 'Wiki', { decision: 'copy' }),+ entity('roadmap', 'wiki', 'Roadmap', { decision: 'skip', rows: 40 }),+ ];+ imports.report = { ...planning, pages: 1, dataSources: 1, status: 'ready' };+ setup();+ expect(screen.queryByText(/Deciding/)).toBeNull();+ expect(+ screen.getByRole<HTMLButtonElement>('button', { name: 'Import 1 page' })+ .disabled+ ).toBe(false);+ });++ it("says nothing is shared when the owner isn't on a team", () => {+ imports.report = {+ status: 'ready',+ reviewId: 'review',+ onTeam: false,+ pages: 3,+ dataSources: 1,+ copy: 1,+ tasks: 1,+ skip: 1,+ hand: 1,+ documents: 1,+ databases: 0,+ databaseRows: 0,+ taskRows: 40,+ imported: 0,+ waiting: 2,+ writing: 0,+ };+ setup();+ expect(+ screen.getByText(+ "2 only you — you're not on a team, so nothing is shared"+ )+ ).toBeTruthy();+ });++ it("shows a database's sample rows and task columns, and the whole plan", async () => {+ imports.entities = [+ entity('roadmap', null, 'Roadmap', {+ decision: 'tasks',+ rows: 2365,+ sampleTitles: ['Fix login redirect', 'Q3 pricing page'],+ taskFields: { status: 'Stage', due: 'Deadline' },+ }),+ entity('crm', null, 'CRM', {+ decision: 'copy',+ rows: 12,+ sharedReason: 'Personal notes',+ }),+ ];+ setup();+ expect(screen.getByText('Task lists')).toBeTruthy();+ fireEvent.click(+ screen.getByRole('button', { name: 'Show rows of Roadmap' })+ );+ expect(+ screen.getByText('2,365 tasks — Fix login redirect, Q3 pricing page, …')+ ).toBeTruthy();+ expect(+ screen.getByText('Status from “Stage” · Due from “Deadline”')+ ).toBeTruthy();++ fireEvent.click(screen.getByRole('button', { name: 'Open full review' }));+ const dialog = await screen.findByRole('dialog');+ expect(+ within(dialog).getByRole('list', { name: 'Import plan' })+ ).toBeTruthy();+ expect(within(dialog).getByText('CRM')).toBeTruthy();+ });+});diff --git a/apps/web/src/features/agent-reviews/views/import-review.tsx b/apps/web/src/features/agent-reviews/views/import-review.tsxnew file mode 100644index 0000000000..618d1087a6--- /dev/null+++ b/apps/web/src/features/agent-reviews/views/import-review.tsx@@ -0,0 +1,119 @@+import { useSplitLayout } from '@components/app/split-layout/layout';+import { fileTypeToBlockName } from '@core/constant/allBlocks';+import { fireAndForget } from '@core/util/fireAndForget';+import { openInNewSplitForMention } from '@core/util/openInNewSplit';+import { buildSimpleEntityUrl } from '@core/util/url';+import { queryReadyGate } from '@queries/gate';+import {+ useCancelNotionImportMutation,+ useImportQuery,+ useNotionImportReportQuery,+} from '@queries/import';+import { ResultAsync } from 'neverthrow';+import { ImportPlanCard } from '../components/import-plan-card';+import type { ImportedCopy } from '../core/import-plan';+import type { Review, ReviewActions } from '../core/review';+import { createImportReview } from '../primitives/import-review';+import {+ decodeImportProgress,+ decodePlanEntries,+} from '../queries/import-plan';++/** The block a teammate's copy opens in; `undefined` when it has none. */+function copyBlock(copy: ImportedCopy) {+ const block = fileTypeToBlockName(copy.entityType);+ return block === 'unknown' ? undefined : block;+}++/** An import review shows its plan, then the run it started. */+export function ImportReview(props: {+ review: Review;+ canAct: boolean;+ actions: ReviewActions;+}) {+ const state = useImportQuery();+ const report = useNotionImportReportQuery();+ const cancel = useCancelNotionImportMutation();+ const layout = useSplitLayout();+ const review = createImportReview({+ review: () => props.review,+ actions: props.actions,+ entries: () =>+ queryReadyGate(state)+ ? decodePlanEntries(state.data.entities, props.review.id)+ : [],+ report: () =>+ queryReadyGate(report) ? decodeImportProgress(report.data) : undefined,+ canAct: () => props.canAct,+ stop: async () =>+ (+ await ResultAsync.fromPromise(cancel.mutateAsync(), () => ({+ kind: 'cancel-failed' as const,+ }))+ ).isOk(),+ });+ const alert = () =>+ review.error() ??+ review.validation() ??+ (review.stopFailed()+ ? 'The import could not be stopped. Try again.'+ : undefined) ??+ (review.stale()+ ? 'This review changed elsewhere. Reload it to see the saved plan.'+ : undefined);+ return (+ <section aria-label={review.title()} class="min-w-0">+ <ImportPlanCard+ title={review.title()}+ summary={review.summary()}+ sharingSummary={review.sharingSummary()}+ totals={review.totals()}+ planningStatus={review.planningStatus()}+ alreadyInMacroSummary={review.alreadyInMacroSummary()}+ alreadyInMacro={review.alreadyInMacro()}+ entityHref={(copy) => {+ const block = copyBlock(copy);+ return block+ ? buildSimpleEntityUrl({ type: block, id: copy.entityId })+ : undefined;+ }}+ onOpenEntity={(copy, event) => {+ const block = copyBlock(copy);+ if (!block) return;+ layout.openWithSplit(+ { type: block, id: copy.entityId },+ { preferNewSplit: openInNewSplitForMention(event.shiftKey, true) }+ );+ }}+ phase={review.phase()}+ progress={review.progress()}+ nodes={review.nodes()}+ hasPlan={review.hasPlan() || state.isPlaceholderData}+ isCollapsed={review.isCollapsed}+ isShowingRows={review.isShowingRows}+ isExcluded={review.isExcluded}+ isShared={review.isShared}+ canAct={props.canAct}+ editable={review.editable()}+ startedByAgent={props.review.decision?.via === 'chat'}+ working={review.working()}+ stopping={review.stopping()}+ count={review.count()}+ volume={review.volume()}+ alert={alert()}+ onReload={+ review.error() || review.stale()+ ? () => fireAndForget(review.reload(), 'agent-reviews')+ : undefined+ }+ onToggleExpanded={review.toggleExpanded}+ onToggleRows={review.toggleRows}+ onIncludedChange={review.setIncluded}+ onSharedChange={review.setShared}+ onApprove={() => fireAndForget(review.approve(), 'agent-reviews')}+ onDismiss={() => fireAndForget(review.dismiss(), 'agent-reviews')}+ onStop={() => fireAndForget(review.stop(), 'agent-reviews')}+ />+ </section>+ );+}diff --git a/apps/web/src/features/agent-reviews/views/persisted-review.test.tsx b/apps/web/src/features/agent-reviews/views/persisted-review.test.tsxnew file mode 100644index 0000000000..a599afb645--- /dev/null+++ b/apps/web/src/features/agent-reviews/views/persisted-review.test.tsx@@ -0,0 +1,120 @@+/** @vitest-environment jsdom */+import { cleanup, fireEvent, render, screen } from '@solidjs/testing-library';+import { afterEach, describe, expect, it, vi } from 'vitest';+import type { Review, ReviewActions } from '../core/review';+import { PersistedReview } from './persisted-review';++const reviews = vi.hoisted(() => ({+ record: undefined as Review | undefined,+ decide: vi.fn(),+}));+vi.mock('../queries/reviews', () => ({+ useReview: () => ({+ isError: false,+ isSuccess: true,+ data: reviews.record,+ refetch: vi.fn(),+ }),+ useReviewActions: () => ({ reload: vi.fn(), decide: reviews.decide }),+}));+vi.mock('@core/context/user', () => ({+ useUserId: () => () => 'macro|[email protected]',+}));+function Card(props: { review: Review; actions: ReviewActions }) {+ return (+ <>+ <button+ type="button"+ onClick={() =>+ props.actions.decide({+ action: 'submit',+ revision: props.review.revision,+ items: [],+ })+ }+ >+ Approve+ </button>+ <button+ type="button"+ onClick={() =>+ props.actions.decide({+ action: 'dismiss',+ revision: props.review.revision,+ items: [],+ })+ }+ >+ Dismiss+ </button>+ </>+ );+}+vi.mock('./import-review', () => ({+ ImportReview: (props: { review: Review; actions: ReviewActions }) =>+ Card(props),+}));+vi.mock('./review-stack', () => ({+ ReviewStack: (props: { review: Review; actions: ReviewActions }) =>+ Card(props),+}));++function review(kind: Review['kind']): Review {+ return {+ id: 'review',+ sessionId: 'session',+ kind,+ title: 'Review',+ revision: 1,+ updatedAt: 1,+ presentedAt: 1,+ items: [{ id: 'item', draft: {}, status: 'pending' }],+ };+}++afterEach(() => {+ cleanup();+ reviews.decide.mockReset();+});++describe('PersistedReview', () => {+ it('expects no answer until an approved import settles', () => {+ reviews.record = review('import');+ reviews.decide.mockResolvedValue(review('import'));+ const onDecision = vi.fn();+ render(() => (+ <PersistedReview+ sessionId="session"+ reviewId="review"+ canAct+ ownerId="macro|[email protected]"+ onDecision={onDecision}+ />+ ));++ fireEvent.click(screen.getByRole('button', { name: 'Approve' }));+ expect(reviews.decide).toHaveBeenCalledTimes(1);+ expect(onDecision).not.toHaveBeenCalled();++ fireEvent.click(screen.getByRole('button', { name: 'Dismiss' }));+ expect(onDecision).toHaveBeenCalledTimes(1);+ });++ it('expects the agent to answer an approved email stack at once', () => {+ reviews.record = review('email');+ reviews.decide.mockResolvedValue(review('email'));+ const onDecision = vi.fn();+ render(() => (+ <PersistedReview+ sessionId="session"+ reviewId="review"+ canAct+ ownerId="macro|[email protected]"+ onDecision={onDecision}+ />+ ));++ fireEvent.click(screen.getByRole('button', { name: 'Approve' }));+ expect(onDecision).toHaveBeenCalledTimes(1);+ });+});diff --git a/apps/web/src/features/agent-reviews/views/persisted-review.tsx b/apps/web/src/features/agent-reviews/views/persisted-review.tsxindex f1623ff6bb..00e5acfada 100644--- a/apps/web/src/features/agent-reviews/views/persisted-review.tsx+++ b/apps/web/src/features/agent-reviews/views/persisted-review.tsx@@ -1,7 +1,10 @@import { useUserId } from '@core/context/user';import { fireAndForget } from '@core/util/fireAndForget';import { createEffect, on, Show, Suspense } from 'solid-js';+import { match } from 'ts-pattern';+import { decisionWakesAgent, type Review } from '../core/review';import { useReview, useReviewActions } from '../queries/reviews';+import { ImportReview } from './import-review';import { ReviewStack } from './review-stack';/** Every agent's ACP chain references the same authenticated, persisted review. */@@ -31,18 +34,19 @@ export function PersistedReview(props: {}));- const decisions = {+ const decisions = (kind: Review['kind']) => ({reload: actions.reload,decide: async (decision: Parameters<typeof actions.decide>[0]) => {- if (decision.action !== 'edit') props.onDecision?.();+ const wakes = decisionWakesAgent(kind, decision);+ if (wakes) props.onDecision?.();try {return await actions.decide(decision);} catch (error) {- if (decision.action !== 'edit') props.onDecisionFailed?.();+ if (wakes) props.onDecisionFailed?.();throw error;}},- };+ });return (<Suspense fallback={<p class="text-xs text-ink-muted">Loading review…</p>}><Show when={query.isError}>@@ -58,16 +62,27 @@ export function PersistedReview(props: {</p></Show><Show when={query.isSuccess ? query.data : undefined}>- {(record) => (- <ReviewStack- review={record()}- canAct={- props.canAct &&- userId() === (record().actingUser ?? props.ownerId)- }- actions={decisions}- />- )}+ {(record) => {+ const canAct = () =>+ props.canAct && userId() === (record().actingUser ?? props.ownerId);+ // A review's kind is fixed when it is saved.+ return match(record().kind)+ .with('import', () => (+ <ImportReview+ review={record()}+ canAct={canAct()}+ actions={decisions('import')}+ />+ ))+ .with('email', 'calendar', (kind) => (+ <ReviewStack+ review={record()}+ canAct={canAct()}+ actions={decisions(kind)}+ />+ ))+ .exhaustive();+ }}</Show></Suspense>);diff --git a/apps/web/src/features/agent-reviews/views/review-stack.tsx b/apps/web/src/features/agent-reviews/views/review-stack.tsxindex 91742c3e36..7e01e601b7 100644--- a/apps/web/src/features/agent-reviews/views/review-stack.tsx+++ b/apps/web/src/features/agent-reviews/views/review-stack.tsx@@ -9,6 +9,7 @@ import {import { Button } from '@ui/components/Button';import { ConfirmDialog } from '@ui/components/ConfirmDialog';import { createEffect, createSignal, Match, on, Show, Switch } from 'solid-js';+import { match } from 'ts-pattern';import { ReviewDeck } from '../components/review-deck';import {ReviewNavigation,@@ -183,7 +184,7 @@ export function ReviewStack(props: {const progress = () =>[completed() > 0- ? `${completed()} ${record().kind === 'email' ? 'sent' : record().kind === 'calendar' ? 'created' : 'answered'}`+ ? `${completed()} ${reviewWording(record().kind).done.toLowerCase()}`: '',dismissed() > 0 ? `${dismissed()} dismissed` : '',needsAttention() > 0@@ -322,38 +323,29 @@ export function ReviewStack(props: {<span class="min-w-0 text-xs text-ink-muted" role="status">{progress()}</span>- <Show- when={record().kind === 'email' || record().kind === 'calendar'}+ <Button+ variant="strong"+ size="sm"+ class="ml-auto"+ disabled={locked() || pending() === 0}+ onClick={() => {+ setConfirmingBatch(+ record()+ .items.filter((item) => item.status === 'pending')+ .map((item) => item.id)+ );+ setConfirmationOpen(true);+ }}>- <Button- variant="strong"- size="sm"- class="ml-auto"- disabled={locked() || pending() === 0}- onClick={() => {- setConfirmingBatch(- record()- .items.filter((item) => item.status === 'pending')- .map((item) => item.id)- );- setConfirmationOpen(true);- }}- >- {record().kind === 'email' ? 'Send all' : 'Create all'} (- {pending()})- </Button>- </Show>+ {reviewWording(record().kind).verb} all ({pending()})+ </Button></footer><ConfirmDialogopen={confirmationOpen()}onOpenChange={setConfirmationOpen}- title={`${record().kind === 'email' ? 'Send' : 'Create'} ${confirmingBatch().length} ${record().kind === 'email' ? 'email' : 'event'}${confirmingBatch().length === 1 ? '' : 's'}?`}- body={- record().kind === 'email'- ? 'Each email will be sent to the recipients shown in its draft.'- : 'These events will be created and guests will receive invitations.'- }- confirmLabel={`${record().kind === 'email' ? 'Send' : 'Create'} ${confirmingBatch().length} ${record().kind === 'email' ? 'email' : 'event'}${confirmingBatch().length === 1 ? '' : 's'}`}+ title={`${batchLabel(record().kind, confirmingBatch().length)}?`}+ body={reviewWording(record().kind).batchBody}+ confirmLabel={batchLabel(record().kind, confirmingBatch().length)}onConfirm={() => {const items = confirmingBatch();if (items.length === 0 || !confirmationOpen()) return;@@ -374,17 +366,51 @@ function startedByAgent(review: Review, item: ReviewItem | undefined) {function neverDraft(): never {throw new Error('Invalid review draft');}+function reviewWording(kind: Review['kind']) {+ return match(kind)+ .with('email', () => ({+ verb: 'Send',+ noun: 'email',+ working: 'Sending…',+ done: 'Sent',+ batchBody:+ 'Each email will be sent to the recipients shown in its draft.',+ }))+ .with('calendar', () => ({+ verb: 'Create',+ noun: 'event',+ working: 'Creating…',+ done: 'Created',+ batchBody:+ 'These events will be created and guests will receive invitations.',+ }))+ .with('import', () => ({+ verb: 'Import',+ noun: 'import',+ working: 'Importing…',+ done: 'Imported',+ batchBody: 'These imports will start right away.',+ }))+ .exhaustive();+}+function batchLabel(kind: Review['kind'], count: number): string {+ const wording = reviewWording(kind);+ return `${wording.verb} ${count} ${wording.noun}${count === 1 ? '' : 's'}`;+}function statusLabel(status: ReviewItem['status'] | undefined,kind: Review['kind']): string {- if (status === 'submitting')- return kind === 'email' ? 'Sending…' : 'Creating…';- if (status === 'completed') return kind === 'email' ? 'Sent' : 'Created';- if (status === 'dismissed') return 'Dismissed';- if (status === 'unknown')- return 'Outcome unknown — check the destination before trying again.';- return 'Draft';+ return match(status)+ .with('submitting', () => reviewWording(kind).working)+ .with('completed', () => reviewWording(kind).done)+ .with('dismissed', () => 'Dismissed')+ .with(+ 'unknown',+ () => 'Outcome unknown — check the destination before trying again.'+ )+ .with('pending', undefined, () => 'Draft')+ .exhaustive();}function reviewSummary(item: ReviewItem, kind: Review['kind']): ReviewSummary {const summary = {@@ -398,41 +424,48 @@ function reviewSummary(item: ReviewItem, kind: Review['kind']): ReviewSummary {: statusLabel(item.status, kind),needsAttention: Boolean(item.error) || item.status === 'unknown',};- if (kind === 'email') {- const email = SendEmail.safeParse(item.draft);- if (!email.success) return summary;- return {- ...summary,- title: email.data.subject || '(No subject)',- description:- email.data.to- ?.map((recipient) =>- recipient.name- ? `${recipient.name} <${recipient.email}>`- : recipient.email- )- .join(', ') || 'No recipients',- };- }- if (kind === 'calendar') {- const calendar = CreateCalendarEvent.safeParse(item.draft);- if (!calendar.success) return summary;- const time = calendar.data.time;- const description =- time.kind === 'allDay'- ? `${time.startDate} · All day`- : new Date(time.startsAt).toLocaleString(undefined, {- month: 'short',- day: 'numeric',- hour: 'numeric',- minute: '2-digit',- timeZoneName: 'short',- });- return {- ...summary,- title: calendar.data.title || '(Untitled event)',- description,- };- }- return summary;+ return match(kind)+ .with('email', () => emailSummary(item, summary))+ .with('calendar', () => calendarSummary(item, summary))+ .with('import', () => ({ ...summary, title: 'Import plan' }))+ .exhaustive();+}+function emailSummary(item: ReviewItem, summary: ReviewSummary): ReviewSummary {+ const email = SendEmail.safeParse(item.draft);+ if (!email.success) return summary;+ return {+ ...summary,+ title: email.data.subject || '(No subject)',+ description:+ email.data.to+ ?.map((recipient) =>+ recipient.name+ ? `${recipient.name} <${recipient.email}>`+ : recipient.email+ )+ .join(', ') || 'No recipients',+ };+}+function calendarSummary(+ item: ReviewItem,+ summary: ReviewSummary+): ReviewSummary {+ const calendar = CreateCalendarEvent.safeParse(item.draft);+ if (!calendar.success) return summary;+ const time = calendar.data.time;+ const description =+ time.kind === 'allDay'+ ? `${time.startDate} · All day`+ : new Date(time.startsAt).toLocaleString(undefined, {+ month: 'short',+ day: 'numeric',+ hour: 'numeric',+ minute: '2-digit',+ timeZoneName: 'short',+ });+ return {+ ...summary,+ title: calendar.data.title || '(Untitled event)',+ description,+ };}diff --git a/apps/web/src/features/agents-view/components/AgentSessionPane.tsx b/apps/web/src/features/agents-view/components/AgentSessionPane.tsxindex b5751da715..3f502f242a 100644--- a/apps/web/src/features/agents-view/components/AgentSessionPane.tsx+++ b/apps/web/src/features/agents-view/components/AgentSessionPane.tsx@@ -1,5 +1,6 @@import { AgentSessionProvider } from '@app/features/block-agent/agent-session-provider';import { AgentComposer } from '@app/features/block-agent/component/AgentComposer';+import { AgentConnections } from '@app/features/block-agent/component/AgentConnections';import { AgentPreviewBanner } from '@app/features/block-agent/component/AgentPreviewBanner';import { AgentPullRequestChip } from '@app/features/block-agent/component/AgentPullRequestChip';import { AgentSessionReadMarker } from '@app/features/block-agent/component/AgentSessionReadMarker';@@ -270,7 +271,9 @@ function SessionContent(props: {><AgentPreviewBanner /><div class="transcript-host">- <Transcript searchTarget={searchTarget()} />+ <AgentConnections>+ <Transcript searchTarget={searchTarget()} />+ </AgentConnections></div>{/* The pane's last row on desktop, where the corner prompt sits. */}<divdiff --git a/apps/web/src/features/block-agent/component/AgentConnections.test.tsx b/apps/web/src/features/block-agent/component/AgentConnections.test.tsxindex 56393b969e..728defdd92 100644--- a/apps/web/src/features/block-agent/component/AgentConnections.test.tsx+++ b/apps/web/src/features/block-agent/component/AgentConnections.test.tsx@@ -17,6 +17,7 @@ const mocks = vi.hoisted(() => ({revision: 0,connect: vi.fn(),issue: vi.fn(),+ reportConnected: vi.fn(),notify: vi.fn(),}));vi.mock('@queries/pipedream-connectors', () => ({@@ -25,8 +26,8 @@ vi.mock('@queries/pipedream-connectors', () => ({vi.mock('@core/component/Toast/Toast', () => ({toast: { alert: mocks.notify },}));-vi.mock('./prompt-action', () => ({- promptActionOf: (prompt: string) => ({ type: 'prompt', prompt }),+vi.mock('@queries/agent-session/app-connections', () => ({+ reportAgentSessionAppConnected: mocks.reportConnected,}));vi.mock('../context/AgentSessionContext', () => ({useAgentSession: () => ({@@ -58,11 +59,11 @@ beforeEach(() => {mocks.viewer = 'owner';mocks.turn = 'idle';mocks.revision = 0;- mocks.issue.mockResolvedValue({ isErr: () => false });+ mocks.reportConnected.mockResolvedValue({ isErr: () => false });});afterEach(cleanup);-it('registers then issues exactly one agent prompt without touching the composer', async () => {+it('reports the connection once, without prompting or touching the composer', async () => {mocks.connect.mockResolvedValue('connected');render(() => (<AgentConnections>@@ -75,12 +76,12 @@ it('registers then issues exactly one agent prompt without touching the composer});fireEvent.click(screen.getByRole('button'));await waitFor(() =>- expect(mocks.issue).toHaveBeenCalledExactlyOnceWith({- type: 'prompt',- prompt:- 'I connected Notion. Continue my previous request using the newly available tools.',- })+ expect(mocks.reportConnected).toHaveBeenCalledExactlyOnceWith(+ 'agent-session',+ 'notion'+ ));+ expect(mocks.issue).not.toHaveBeenCalled();expect((screen.getByRole('textbox') as HTMLTextAreaElement).value).toBe('unsent draft');@@ -98,7 +99,7 @@ it('cancellation never prompts the agent', async () => {false));- expect(mocks.issue).not.toHaveBeenCalled();+ expect(mocks.reportConnected).not.toHaveBeenCalled();});it('collaborators cannot connect their account on behalf of the session owner', () => {mocks.viewer = 'collaborator';@@ -111,7 +112,7 @@ it('collaborators cannot connect their account on behalf of the session owner',});it('surfaces a rejected continuation', async () => {mocks.connect.mockResolvedValue('connected');- mocks.issue.mockResolvedValue({ isErr: () => true });+ mocks.reportConnected.mockResolvedValue({ isErr: () => true });render(() => (<AgentConnections><Button />diff --git a/apps/web/src/features/block-agent/component/AgentConnections.tsx b/apps/web/src/features/block-agent/component/AgentConnections.tsxindex 32a353a88c..c931032771 100644--- a/apps/web/src/features/block-agent/component/AgentConnections.tsx+++ b/apps/web/src/features/block-agent/component/AgentConnections.tsx@@ -1,10 +1,10 @@import { toast } from '@core/component/Toast/Toast';import { AppConnectionContext } from '@core/pipedream/connection-context';+import { reportAgentSessionAppConnected } from '@queries/agent-session/app-connections';import { connectPipedreamApp } from '@queries/pipedream-connectors';import type { ParentProps } from 'solid-js';import { useAgentSession } from '../context/AgentSessionContext';import { createConnectorContinuation } from '../primitives/create-connector-continuation';-import { promptActionOf } from './prompt-action';/** Authorization belongs to the session transcript, independently of its draft. */export function AgentConnections(props: ParentProps) {@@ -25,14 +25,14 @@ export function AgentConnections(props: ParentProps) {.map((message) => `${message.turn}:${message.author.kind}`).join(',')}`,connect: connectPipedreamApp,- resume: async (name) => {- const result = await state.issue(- promptActionOf(- `I connected ${name}. Continue my previous request using the newly available tools.`,- []- )+ resume: async (app) => {+ const sessionId = state.sessionId();+ if (!sessionId) throw new Error('No agent session to continue');+ const result = await reportAgentSessionAppConnected(+ sessionId,+ app.appSlug);- if (!result || result.isErr())+ if (result.isErr())throw new Error('Could not continue the agent session');},notify: (message) => toast.alert(message),diff --git a/apps/web/src/features/block-agent/components/session-event-row.test.tsx b/apps/web/src/features/block-agent/components/session-event-row.test.tsxindex 75e47066e9..b0dd1c4e05 100644--- a/apps/web/src/features/block-agent/components/session-event-row.test.tsx+++ b/apps/web/src/features/block-agent/components/session-event-row.test.tsx@@ -126,3 +126,60 @@ it('keeps a safe fallback for events from a newer server', () => {const view = render(() => <SessionEventRow event={event} />);expect(view.getByRole('status').textContent).toContain('System event');});++it('announces a ready import plan with its counts and opens its review', async () => {+ const open = vi.fn();+ const view = render(() => (+ <SessionEventRow+ onOpenReview={open}+ event={{+ ...base,+ kind: 'import.planned',+ payload: { copy: 12, taskLists: 1, skipped: 3, byHand: 2 },+ }}+ />+ ));+ expect(view.getByRole('status').textContent).toContain(+ 'Import plan ready · 12 to copy · 1 task list · 2 by hand'+ );+ await fireEvent.click(view.getByRole('button', { name: 'View review' }));+ expect(open).toHaveBeenCalledExactlyOnceWith('review-id');+});++it('reports a settled import from its counts, never its error text', () => {+ const view = render(() => (+ <SessionEventRow+ event={{+ ...base,+ kind: 'review.settled',+ payload: {+ import: {+ outcome: 'failed',+ error: 'Notion said: Secret roadmap',+ imported: 4,+ planned: 10,+ documents: 8,+ databases: 1,+ databaseRows: 20,+ taskLists: 1,+ taskRows: 5,+ skipped: 2,+ notWritten: 6,+ sharedWithTeam: 0,+ alreadyInMacro: 0,+ waitingOnTeammates: 0,+ rootFolderId: null,+ failures: [{ title: 'Roadmap', url: null, reason: null }],+ handItems: [],+ },+ },+ }}+ />+ ));+ const status = view.getByRole('status');+ expect(status.textContent).toContain(+ 'Import failed · 4 of 10 imported · 1 failed'+ );+ expect(status.textContent).not.toContain('Secret roadmap');+ expect(status.classList.contains('text-failure')).toBe(true);+});diff --git a/apps/web/src/features/block-agent/components/session-event-row.tsx b/apps/web/src/features/block-agent/components/session-event-row.tsxindex 9c2f9e242c..3bcae5f3be 100644--- a/apps/web/src/features/block-agent/components/session-event-row.tsx+++ b/apps/web/src/features/block-agent/components/session-event-row.tsx@@ -1,9 +1,12 @@import { formatCompactRelativeTimestamp } from '@entity/utils/timestamp';import CheckCircle from '@phosphor/check-circle.svg';import ClipboardText from '@phosphor/clipboard-text.svg';+import DownloadSimple from '@phosphor/download-simple.svg';import Info from '@phosphor/info.svg';import Plug from '@phosphor/plug.svg';import type {+ ImportPlan,+ ImportSettlement,ReviewOutcome,SessionNotification,} from '@service-agent-fold/generated/types';@@ -46,6 +49,35 @@ function reviewSummary(return { summary, failed: failed > 0 || uncertain > 0 };}+function countLabel(count: number, one: string, many: string) {+ return `${count} ${count === 1 ? one : many}`;+}++function importPlanSummary(plan: ImportPlan) {+ let summary = `Import plan ready · ${plan.copy} to copy`;+ if (plan.taskLists)+ summary += ` · ${countLabel(plan.taskLists, 'task list', 'task lists')}`;+ if (plan.byHand) summary += ` · ${plan.byHand} by hand`;+ return { summary, failed: false };+}++/** Counts only: an import report's error text can quote source content. */+function importSettlementSummary(report: ImportSettlement) {+ const heading = match(report.outcome)+ .with('completed', () => 'Import finished')+ .with('failed', () => 'Import failed')+ .with('cancelled', () => 'Import cancelled')+ .exhaustive();+ let summary = `${heading} · ${report.imported} of ${report.planned} imported`;+ if (report.failures.length) summary += ` · ${report.failures.length} failed`;+ if (report.handItems.length)+ summary += ` · ${report.handItems.length} by hand`;+ return {+ summary,+ failed: report.outcome === 'failed' || report.failures.length > 0,+ };+}+/** System facts stay on one line on desktop and mobile, outside messages. */export function SessionEventRow(props: {event: SessionNotification;@@ -68,7 +100,14 @@ export function SessionEventRow(props: {})).with({ kind: 'review.settled' }, (event) => ({icon: CheckCircle,- ...reviewSummary(event.payload, true, props.viewerId),+ ...('import' in event.payload+ ? importSettlementSummary(event.payload.import)+ : reviewSummary(event.payload, true, props.viewerId)),+ review: true,+ }))+ .with({ kind: 'import.planned' }, (event) => ({+ icon: DownloadSimple,+ ...importPlanSummary(event.payload),review: true,})).exhaustive((_unknown) => ({diff --git a/apps/web/src/features/block-agent/primitives/create-connector-continuation.test.ts b/apps/web/src/features/block-agent/primitives/create-connector-continuation.test.tsindex 20bf750a3b..9077398e16 100644--- a/apps/web/src/features/block-agent/primitives/create-connector-continuation.test.ts+++ b/apps/web/src/features/block-agent/primitives/create-connector-continuation.test.ts@@ -33,7 +33,7 @@ describe('connector continuation', () => {expect(deps.resume).not.toHaveBeenCalled();finish('connected');await pending;- expect(deps.resume).toHaveBeenCalledExactlyOnceWith('Linear');+ expect(deps.resume).toHaveBeenCalledExactlyOnceWith(app);expect(flow.disabled()).toBe(false);dispose();});diff --git a/apps/web/src/features/block-agent/primitives/create-connector-continuation.ts b/apps/web/src/features/block-agent/primitives/create-connector-continuation.tsindex 09855de107..3ab0fcd2ac 100644--- a/apps/web/src/features/block-agent/primitives/create-connector-continuation.ts+++ b/apps/web/src/features/block-agent/primitives/create-connector-continuation.ts@@ -9,7 +9,7 @@ export function createConnectorContinuation(deps: {appSlug: string;serverName: string;}) => Promise<PipedreamConnectOutcome>;- resume: (name: string) => Promise<void>;+ resume: (app: { appSlug: string; name: string }) => Promise<void>;notify: (message: string) => void;}) {const [pending, setPending] = createSignal(false);@@ -40,7 +40,7 @@ export function createConnectorContinuation(deps: {);return;}- await deps.resume(app.name);+ await deps.resume(app);} catch {if (!disposed)deps.notify(diff --git a/apps/web/src/features/block-agent/state/tool-approval-wording.ts b/apps/web/src/features/block-agent/state/tool-approval-wording.tsindex 08e5eac312..a067ba2df9 100644--- a/apps/web/src/features/block-agent/state/tool-approval-wording.ts+++ b/apps/web/src/features/block-agent/state/tool-approval-wording.ts@@ -85,6 +85,8 @@ const MACRO_TOOLS: Record<MacroToolName, Wording | null> = {GetTeamAvailability: (whose) => `check availability for ${whose} team`,GetThread: (whose) => `read ${whose} email`,ImportCompaniesCsv: (whose) => `import companies into ${whose} CRM`,+ ImportFromNotion: (whose) =>+ `import a Notion workspace into ${whose} workspace`,ImportNotionPage: (whose) => `import a Notion page into ${whose} workspace`,ImportPipelineCsv: (whose) => `import deals into ${whose} pipelines`,IssueBotCredential: (whose) => `issue credentials for ${whose} bots`,diff --git a/apps/web/src/features/connector-import/core/agent-handoff.test.ts b/apps/web/src/features/connector-import/core/agent-handoff.test.tsindex 9921dad789..ebe31b084f 100644--- a/apps/web/src/features/connector-import/core/agent-handoff.test.ts+++ b/apps/web/src/features/connector-import/core/agent-handoff.test.ts@@ -15,12 +15,13 @@ describe('agent import handoff', () => {expect(handoff.instructions).toContain('never edit or delete');});- it("imports Notion pages through Macro's importer", () => {+ it("imports the Notion workspace through Macro's importer", () => {const files = IMPORT_CATEGORIES.files;const notion = files.providers.find((p) => p.appSlug === 'notion')!;- const { instructions } = agentImportHandoff(files, notion);- expect(instructions).toContain('ImportNotionPage');- expect(instructions).not.toContain('CreateDocument, converting');+ const handoff = agentImportHandoff(files, notion);+ expect(handoff.input).toBe('Import my Notion workspace into Macro.');+ expect(handoff.instructions).toContain('ImportFromNotion');+ expect(handoff.instructions).not.toContain('CreateImportEntity');});it('records ledger sources so nothing imports twice', () => {diff --git a/apps/web/src/features/connector-import/core/agent-handoff.ts b/apps/web/src/features/connector-import/core/agent-handoff.tsindex 16c8b8278a..46725070bc 100644--- a/apps/web/src/features/connector-import/core/agent-handoff.ts+++ b/apps/web/src/features/connector-import/core/agent-handoff.ts@@ -9,13 +9,7 @@ export type AgentImportHandoff = {instructions: string;};-function createWith(- config: ImportCategoryConfig,- provider: ImportProvider-): string {- // Macro's Notion importer converts and dedupes pages itself.- if (provider.ledger === 'notion')- return 'Import each chosen page with ImportNotionPage; never recreate a page with CreateDocument.';+function createWith(config: ImportCategoryConfig): string {return match(config.category).with('files',@@ -42,17 +36,31 @@ function createWith(/** Ledger bookkeeping, so neither the user nor a teammate imports twice. */function ledgerSteps(provider: ImportProvider): string[] {- if (!provider.ledger || provider.ledger === 'notion') return [];+ if (!provider.ledger) return [];return [`Check ListImportEntities for source ${provider.ledger} first and leave out anything already imported or declined.`,`After creating each item, record it with CreateImportEntity: source ${provider.ledger}, status imported, and the new entityId.`,];}+/**+ * Notion runs Macro's workspace importer: the agent asks who should see the+ * import, plans it with ImportFromNotion, and presents the plan for review.+ */+const NOTION_WORKSPACE_HANDOFF: AgentImportHandoff = {+ input: 'Import my Notion workspace into Macro.',+ instructions: [+ "The user started this from Macro's import dialog and has just connected Notion.",+ 'Import their workspace with ImportFromNotion: ask who should see it if the tool says to, plan it, and present the plan for review right away.',+ 'Never recreate Notion pages with CreateDocument or ImportNotionPage, and never edit or delete anything in Notion.',+ ].join(' '),+};+export function agentImportHandoff(config: ImportCategoryConfig,provider: ImportProvider): AgentImportHandoff {+ if (provider.ledger === 'notion') return NOTION_WORKSPACE_HANDOFF;return {input: `Import my most recent ${config.items} from ${provider.name} into ${config.destination}.`,instructions: [@@ -60,7 +68,7 @@ export function agentImportHandoff(`Use the ${provider.name} connector tools to find their most relevant recent ${config.items}.`,...ledgerSteps(provider),`Before creating anything, show a short numbered list and ask which ones to bring over.`,- createWith(config, provider),+ createWith(config),`Only read from ${provider.name}; never edit or delete anything there.`,'Finish with links to everything you created.',].join(' '),diff --git a/apps/web/src/features/connector-import/views/import-dialog.test.tsx b/apps/web/src/features/connector-import/views/import-dialog.test.tsxindex 04a7e56a26..0758c42760 100644--- a/apps/web/src/features/connector-import/views/import-dialog.test.tsx+++ b/apps/web/src/features/connector-import/views/import-dialog.test.tsx@@ -114,15 +114,14 @@ describe('connector import dialog', () => {);});- it("hands a ledger source to the agent too, with Macro's importer", async () => {+ it("hands Notion to the agent's workspace importer", async () => {const { context, open } = setup('files', { connected: ['notion'] });fireEvent.click(screen.getByRole('button', { name: 'Import from Notion' }));await waitFor(() => expect(open()).toBe(false));expect(context.startAgentImport).toHaveBeenCalledWith(expect.objectContaining({- input:- 'Import my most recent documents and files from Notion into my Macro Drive.',- instructions: expect.stringContaining('ImportNotionPage'),+ input: 'Import my Notion workspace into Macro.',+ instructions: expect.stringContaining('ImportFromNotion'),}));});diff --git a/apps/web/src/features/onboarding/onboarding.tsx b/apps/web/src/features/onboarding/onboarding.tsxindex 92897d6512..d02f63c23e 100644--- a/apps/web/src/features/onboarding/onboarding.tsx+++ b/apps/web/src/features/onboarding/onboarding.tsx@@ -1,5 +1,4 @@import { InviteOfferPanel } from '@app/features/gtm-invite/InviteOfferPanel';-import { useImportQuery } from '@queries/import';import { useNavigate, useSearchParams } from '@solidjs/router';import type { JSX } from 'solid-js';import { OnboardingProvider } from './context/onboarding-context';@@ -13,14 +12,6 @@ export function Onboarding() {const context = createAppOnboardingContext();const navigate = useNavigate();const [params] = useSearchParams();- // Polling the import state keeps gather runs starting while the user is in- // the flow; the steps themselves never read it.- useImportQuery({- enabled: () => {- const viewer = context.viewer();- return viewer.t === 'signed-in' && !viewer.viewer.tutorialComplete;- },- });return (<OnboardingProvider value={context}><OnboardingFlowViewdiff --git a/apps/web/src/features/paywall/ai-usage-limit-handling.test.ts b/apps/web/src/features/paywall/ai-usage-limit-handling.test.tsindex 5c2daadea0..664c2d5c26 100644--- a/apps/web/src/features/paywall/ai-usage-limit-handling.test.ts+++ b/apps/web/src/features/paywall/ai-usage-limit-handling.test.ts@@ -72,7 +72,6 @@ const spendingActions = ['import',() => importClient.runImport({ import_ids: ['id'], discard_ids: [] }),],- ['gather retry', () => importClient.retryGather('linear')],['run schedule',() => scheduledActionClient.runNow({ scheduleId: 'schedule' }),diff --git a/apps/web/src/features/settings/connections/integrations-view.test.tsx b/apps/web/src/features/settings/connections/integrations-view.test.tsxindex 2799e8ab43..023654f423 100644--- a/apps/web/src/features/settings/connections/integrations-view.test.tsx+++ b/apps/web/src/features/settings/connections/integrations-view.test.tsx@@ -142,6 +142,7 @@ function provideChannels() {id: name,foreign_id: `C${name}`,source: 'slack',+ kind: 'channel',status: 'staged',initiator: 'manual',user_id: 'me',diff --git a/apps/web/src/features/settings/connections/slack-channel-import/SlackChannelImportCard.test.tsx b/apps/web/src/features/settings/connections/slack-channel-import/SlackChannelImportCard.test.tsxindex 95d873bc22..818ffebeab 100644--- a/apps/web/src/features/settings/connections/slack-channel-import/SlackChannelImportCard.test.tsx+++ b/apps/web/src/features/settings/connections/slack-channel-import/SlackChannelImportCard.test.tsx@@ -60,6 +60,7 @@ function channel(id,foreign_id: `C${id}`,source: 'slack',+ kind: 'channel',status: 'staged',initiator: 'manual',user_id: 'me',@@ -90,7 +91,6 @@ describe('Slack channel import card', () => {{source: 'slack',status: 'running',- auto_import: false,updated_at: '',},],@@ -194,7 +194,6 @@ describe('Slack channel import card', () => {{source: 'slack',status: 'ready',- auto_import: false,updated_at: '',},],@@ -208,7 +207,6 @@ describe('Slack channel import card', () => {source: 'slack',status: 'failed',error: 'Slack is not connected',- auto_import: false,updated_at: '',},],diff --git a/apps/web/src/features/settings/connections/slack-channel-import/model.test.ts b/apps/web/src/features/settings/connections/slack-channel-import/model.test.tsindex 00a5327300..18837f1efd 100644--- a/apps/web/src/features/settings/connections/slack-channel-import/model.test.ts+++ b/apps/web/src/features/settings/connections/slack-channel-import/model.test.ts@@ -18,6 +18,7 @@ function entity(overrides: Partial<ImportEntity> = {}): ImportEntity {id: 'ledger-1',user_id: 'me',source: 'slack',+ kind: 'channel',foreign_id: 'C123',status: 'staged',initiator: 'manual',@@ -37,7 +38,7 @@ function entity(overrides: Partial<ImportEntity> = {}): ImportEntity {}function run(status: ImportRun['status']): ImportRun {- return { source: 'slack', status, auto_import: false, updated_at: '' };+ return { source: 'slack', status, updated_at: '' };}function state(entities: ImportEntity[], runs: ImportRun[] = []): ImportState {diff --git a/apps/web/src/lib/core/component/AI/component/tool/ImportTools.tsx b/apps/web/src/lib/core/component/AI/component/tool/ImportTools.tsxindex 181b3b61da..5bfbdf4a20 100644--- a/apps/web/src/lib/core/component/AI/component/tool/ImportTools.tsx+++ b/apps/web/src/lib/core/component/AI/component/tool/ImportTools.tsx@@ -1,5 +1,6 @@import TrayArrowDown from '@phosphor-icons/core/regular/tray-arrow-down.svg';-import { createSignal } from 'solid-js';+import { useNotionImportReportQuery } from '@queries/import';+import { createSignal, Show } from 'solid-js';import { match } from 'ts-pattern';import { BaseTool } from './BaseTool';import { Tool } from './Tool';@@ -7,8 +8,8 @@ import { createToolRenderer } from './ToolRenderer';/** Renderers for the import-ledger tools the chat agent can call- * (CreateImportEntity / ImportNotionPage / DeleteImportEntity /- * ListImportEntities).+ * (CreateImportEntity / ImportNotionPage / ImportFromNotion /+ * DeleteImportEntity / ListImportEntities).*//** Human label for the item a call is about, from its source metadata. */@@ -85,6 +86,60 @@ export const importNotionPageHandler = createToolRenderer({},});+/** What planning has found in Notion so far, while a plan call waits. */+function NotionScanProgress() {+ const report = useNotionImportReportQuery();+ return (+ <Show when={report.data?.scanned}>+ {(scanned) => (+ <span class="shrink-0 text-ink-muted">+ {scanned().pages} pages, {scanned().dataSources} databases found+ </span>+ )}+ </Show>+ );+}++export const importFromNotionHandler = createToolRenderer({+ name: 'ImportFromNotion',+ render: (ctx) => {+ const [isExpanded, setIsExpanded] = createSignal(false);+ const hasResponse = () => ctx.response !== undefined;+ const label = () =>+ match(ctx.tool.data.action)+ .with('plan', () => 'Plan a Notion import')+ .with('status', () => 'Check the Notion import')+ .with('cancel', () => 'Stop the Notion import')+ .exhaustive();+ return (+ <BaseTool+ icon={TrayArrowDown}+ renderContext={ctx.renderContext}+ type="call"+ response={+ hasResponse() && isExpanded() ? (+ <div class="rounded-md bg-panel px-3 py-2 text-xs text-ink-muted">+ {ctx.response?.data.message}+ </div>+ ) : undefined+ }+ >+ <div class="flex min-w-0 flex-1 items-center justify-between gap-3 overflow-hidden">+ <div class="min-w-0 flex-1 truncate">{label()}</div>+ <Show when={ctx.tool.data.action === 'plan' && !hasResponse()}>+ <NotionScanProgress />+ </Show>+ <Tool.ResultToggle+ expanded={isExpanded()}+ onToggle={() => setIsExpanded((expanded) => !expanded)}+ showToggle={hasResponse()}+ />+ </div>+ </BaseTool>+ );+ },+});+export const deleteImportEntityHandler = createToolRenderer({name: 'DeleteImportEntity',render: (ctx) => (diff --git a/apps/web/src/lib/core/component/AI/component/tool/handler.tsx b/apps/web/src/lib/core/component/AI/component/tool/handler.tsxindex 2b7fb600e1..f6082a8c81 100644--- a/apps/web/src/lib/core/component/AI/component/tool/handler.tsx+++ b/apps/web/src/lib/core/component/AI/component/tool/handler.tsx@@ -120,6 +120,7 @@ import { getThreadHandler } from './GetThread';import {createImportEntityHandler,deleteImportEntityHandler,+ importFromNotionHandler,importNotionPageHandler,listImportEntitiesHandler,} from './ImportTools';@@ -253,6 +254,7 @@ const toolHandlers: ToolHandlerMap<RenderContext> = {GetTeamAvailability: getTeamAvailabilityHandler,CreateImportEntity: createImportEntityHandler,DeleteImportEntity: deleteImportEntityHandler,+ ImportFromNotion: importFromNotionHandler,ImportNotionPage: importNotionPageHandler,GetCompany: getCompanyHandler,CreateCompany: createCompanyHandler,diff --git a/apps/web/src/lib/queries/agent-session/app-connections.ts b/apps/web/src/lib/queries/agent-session/app-connections.tsnew file mode 100644index 0000000000..e24944249b--- /dev/null+++ b/apps/web/src/lib/queries/agent-session/app-connections.ts@@ -0,0 +1,14 @@+import { agentHarnessServiceClient } from '@service-agent-harness/client';++/**+ * Tell the session its owner connected `appSlug`, so it continues the request+ * that asked for the app. The harness writes the continuation; nothing shows+ * in the transcript but the agent's reply, which every viewer gets through the+ * session's log, so this is not a cached mutation.+ */+export function reportAgentSessionAppConnected(+ sessionId: string,+ appSlug: string+) {+ return agentHarnessServiceClient.reportAppConnected(sessionId, appSlug);+}diff --git a/apps/web/src/lib/queries/import.ts b/apps/web/src/lib/queries/import.tsindex 075794a7be..0496d20773 100644--- a/apps/web/src/lib/queries/import.ts+++ b/apps/web/src/lib/queries/import.ts@@ -1,9 +1,9 @@/*** Queries and mutations for the import pipeline.*- * The server owns everything: gather jobs stage candidates, `POST- * /import/run` accepts/declines them, and import jobs flip rows to- * `imported`. Row flips are pushed over the connection gateway as+ * The server owns everything: Slack discovery and chat tools stage+ * candidates, `POST /import/run` accepts/declines them, and import jobs flip+ * rows to `imported`. Row flips are pushed over the connection gateway as* `import_updated`; polling remains as the backstop (pushes can be missed* while reconnecting), tightening while anything is in flight.*/@@ -16,6 +16,7 @@ import {type ImportSource,type ImportState,importClient,+ type NotionImportReport,type SlackChannelMeta,} from '@service-cognition/import';import { createConnectionWebsocketEffect } from '@service-connection/websocket';@@ -25,22 +26,21 @@ import {useMutation,useQuery,} from '@tanstack/solid-query';+import { importKeys } from './import/keys';export type {ImportEntity,ImportEntityStatus,+ ImportKind,ImportRun,ImportRunStatus,ImportSource,ImportState,+ NotionImportReport,SlackChannelMeta,} from '@service-cognition/import';-const KEYS = {- state: ['import', 'state'] as const,-};--/** Gateway push sent when an import row or gather run changes server-side. */+/** Gateway push sent when an import row or discovery run changes server-side. */const IMPORT_UPDATED_MESSAGE_TYPE = 'import_updated';/**@@ -54,9 +54,8 @@ const PENDING_IMPORT_STATE: ImportState = { runs: [], entities: [] };function anythingInFlight(state: ImportState | undefined): boolean {if (!state) return false;return (- state.runs.some(- (run) => run.status === 'running' || run.status === 'importing'- ) || state.entities.some((entity) => entity.status === 'importing')+ state.runs.some((run) => run.status === 'running') ||+ state.entities.some((entity) => entity.status === 'importing'));}@@ -65,31 +64,30 @@ function fetchImportStateFromServer() {}// Cached on the query past unmount; module scope keeps hook state out of it.-function importStateQueryOptions(enabled: boolean) {+function importStateQueryOptions() {return queryOptions({- queryKey: KEYS.state,+ queryKey: importKeys.state.queryKey,queryFn: fetchImportStateFromServer,- enabled,refetchInterval: (query) =>anythingInFlight(query.state.data) ? 3_000 : 15_000,placeholderData: PENDING_IMPORT_STATE,});}-/** The import aggregate: gather runs plus visible ledger rows. */-export function useImportQuery(options?: { enabled?: () => boolean }) {+/** The import aggregate: discovery runs plus visible ledger rows. */+export function useImportQuery() {createConnectionWebsocketEffect((message) => {if (message.type !== IMPORT_UPDATED_MESSAGE_TYPE) return;fireAndForget(invalidateImportState(), 'import-state');});- return useQuery(() =>- importStateQueryOptions(options?.enabled ? options.enabled() : true)- );+ return useQuery(() => importStateQueryOptions());}function invalidateImportState() {- return queryClient.invalidateQueries({ queryKey: KEYS.state });+ return queryClient.invalidateQueries({+ queryKey: importKeys.state.queryKey,+ });}/**@@ -130,3 +128,49 @@ export function slackChannelMeta(if (entity.source !== 'slack') return null;return entity.metadata as SlackChannelMeta;}++function notionImportInFlight(report: NotionImportReport | undefined) {+ return report?.status === 'running' || report?.status === 'importing';+}++function fetchNotionReportFromServer() {+ return throwOnErr(() => importClient.getNotionReport());+}++// Cached on the query past unmount; module scope keeps hook state out of it.+function notionImportReportQueryOptions() {+ return queryOptions({+ queryKey: importKeys.notion.queryKey,+ queryFn: fetchNotionReportFromServer,+ // `import_updated` pushes drive refreshes; polling covers missed pushes+ // while the run is moving.+ refetchInterval: (query) =>+ notionImportInFlight(query.state.data) ? 2_000 : false,+ });+}++/** The Notion workspace import report, refreshed on `import_updated`. */+export function useNotionImportReportQuery() {+ createConnectionWebsocketEffect((message) => {+ if (message.type !== IMPORT_UPDATED_MESSAGE_TYPE) return;+ queryClient.invalidateQueries({+ queryKey: importKeys.notion.queryKey,+ });+ });++ return useQuery(() => notionImportReportQueryOptions());+}++/**+ * Stop the Notion workspace import. Objects already imported stay; the+ * returned report replaces the cached one.+ */+export function useCancelNotionImportMutation() {+ return useMutation(() => ({+ mutationFn: () => throwOnErr(() => importClient.cancelNotion()),+ onSuccess: (report: NotionImportReport) => {+ queryClient.setQueryData(importKeys.notion.queryKey, report);+ fireAndForget(invalidateImportState(), 'import-state');+ },+ }));+}diff --git a/apps/web/src/lib/queries/import/keys.ts b/apps/web/src/lib/queries/import/keys.tsnew file mode 100644index 0000000000..534df09a92--- /dev/null+++ b/apps/web/src/lib/queries/import/keys.ts@@ -0,0 +1,8 @@+import { createQueryKeys } from '@lukemorales/query-key-factory';++export const importKeys = createQueryKeys('import', {+ /** Discovery runs plus visible ledger rows. */+ state: null,+ /** The Notion workspace import report. */+ notion: null,+});diff --git a/apps/web/src/lib/service-clients/service-agent-fold/generated/types.ts b/apps/web/src/lib/service-clients/service-agent-fold/generated/types.tsindex 1f8fd32b36..a346720138 100644--- a/apps/web/src/lib/service-clients/service-agent-fold/generated/types.ts+++ b/apps/web/src/lib/service-clients/service-agent-fold/generated/types.ts@@ -666,6 +666,131 @@ export type Harness =/** Not announced, or an agent this fold does not know. */| 'unknown';+/** One object an import report calls out. */+export type ImportItem = ImportItem_Serialize | ImportItem_Deserialize;++/** One object an import report calls out. */+export type ImportItem_Deserialize = {+ /** The object's title. */+ title: string;+ /** Its URL in the source workspace. */+ url: string | null;+ /** Why it is called out. */+ reason: string | null;+};++/** One object an import report calls out. */+export type ImportItem_Serialize = {+ /** The object's title. */+ title: string;+ /** Its URL in the source workspace. */+ url?: string | null;+ /** Why it is called out. */+ reason?: string | null;+};++/** How an approved import's write ended. */+export type ImportOutcome =+ /** Every planned object was written or accounted for. */+ | 'completed'+ /** The write stopped on an error. */+ | 'failed'+ /** The owner cancelled it. */+ | 'cancelled';++/** What an import plan proposes, once it is ready to approve. */+export type ImportPlan = {+ /** Objects to copy as they are. */+ copy: number;+ /** Data sources whose rows become tasks. */+ taskLists: number;+ /** Objects to leave in the source workspace. */+ skipped: number;+ /** Objects that need handling by hand. */+ byHand: number;+};++/** The final report of an approved workspace import, with no page content. */+export type ImportSettlement =+ | ImportSettlement_Serialize+ | ImportSettlement_Deserialize;++/** The final report of an approved workspace import, with no page content. */+export type ImportSettlement_Deserialize = {+ /** How the write ended. */+ outcome: ImportOutcome;+ /** Why it failed, or what went wrong in a completed write. */+ error: string | null;+ /** Objects written into Macro. */+ imported: number;+ /** Objects the plan copies, as they are or as task lists. */+ planned: number;+ /** Planned documents. */+ documents: number;+ /** Planned databases. */+ databases: number;+ /** Rows those databases hold. */+ databaseRows: number;+ /** Planned task lists. */+ taskLists: number;+ /** Tasks those task lists become. */+ taskRows: number;+ /** Objects left in Notion. */+ skipped: number;+ /** Planned objects that were not written. */+ notWritten: number;+ /** Written objects shared with the owner's team. */+ sharedWithTeam: number;+ /** Objects teammates had already imported, linked instead of copied. */+ alreadyInMacro: number;+ /** Objects a teammate is writing at the same time, linked once written. */+ waitingOnTeammates: number;+ /** The Macro folder the import writes into. */+ rootFolderId: string | null;+ /** Objects that could not be imported, at most [`MAX_IMPORT_ITEMS`]. */+ failures: ImportItem_Deserialize[];+ /** Objects left for the owner to handle by hand, at most [`MAX_IMPORT_ITEMS`]. */+ handItems: ImportItem_Deserialize[];+};++/** The final report of an approved workspace import, with no page content. */+export type ImportSettlement_Serialize = {+ /** How the write ended. */+ outcome: ImportOutcome;+ /** Why it failed, or what went wrong in a completed write. */+ error?: string | null;+ /** Objects written into Macro. */+ imported: number;+ /** Objects the plan copies, as they are or as task lists. */+ planned: number;+ /** Planned documents. */+ documents: number;+ /** Planned databases. */+ databases: number;+ /** Rows those databases hold. */+ databaseRows: number;+ /** Planned task lists. */+ taskLists: number;+ /** Tasks those task lists become. */+ taskRows: number;+ /** Objects left in Notion. */+ skipped: number;+ /** Planned objects that were not written. */+ notWritten: number;+ /** Written objects shared with the owner's team. */+ sharedWithTeam: number;+ /** Objects teammates had already imported, linked instead of copied. */+ alreadyInMacro: number;+ /** Objects a teammate is writing at the same time, linked once written. */+ waitingOnTeammates: number;+ /** The Macro folder the import writes into. */+ rootFolderId?: string | null;+ /** Objects that could not be imported, at most [`MAX_IMPORT_ITEMS`]. */+ failures: ImportItem_Serialize[];+ /** Objects left for the owner to handle by hand, at most [`MAX_IMPORT_ITEMS`]. */+ handItems: ImportItem_Serialize[];+};+/** Review item state, shared with notification consumers. */export type ItemStatus =/** Still awaiting a decision. */@@ -854,7 +979,9 @@ export type NotificationEvent_Deserialize =/** Owner decision and its confirmed results. */| { kind: 'review.decided'; payload: ReviewOutcome_Deserialize }/** Completion of work initiated by a prior review decision. */- | { kind: 'review.settled'; payload: ReviewOutcome_Deserialize }+ | { kind: 'review.settled'; payload: ReviewSettlement_Deserialize }+ /** The import plan a presented review shows finished planning. */+ | { kind: 'import.planned'; payload: ImportPlan }/** An app connection became available to the session. */| {kind: 'app.connected';@@ -869,7 +996,9 @@ export type NotificationEvent_Serialize =/** Owner decision and its confirmed results. */| { kind: 'review.decided'; payload: ReviewOutcome_Serialize }/** Completion of work initiated by a prior review decision. */- | { kind: 'review.settled'; payload: ReviewOutcome_Serialize }+ | { kind: 'review.settled'; payload: ReviewSettlement_Serialize }+ /** The import plan a presented review shows finished planning. */+ | { kind: 'import.planned'; payload: ImportPlan }/** An app connection became available to the session. */| {kind: 'app.connected';@@ -1094,6 +1223,40 @@ export type ReviewOutcome_Serialize = {items: ReviewItemOutcome_Serialize[];};+/**+ * How the work a review decision started ended. Untagged so settlements+ * stored before imports existed, bare [`ReviewOutcome`]s, still read.+ */+export type ReviewSettlement =+ | ReviewSettlement_Serialize+ | ReviewSettlement_Deserialize;++/**+ * How the work a review decision started ended. Untagged so settlements+ * stored before imports existed, bare [`ReviewOutcome`]s, still read.+ */+export type ReviewSettlement_Deserialize =+ /** An approved import's background write ended. */+ | {+ /** The import's final report. */+ import: ImportSettlement_Deserialize;+ }+ /** The decided actions' confirmed results. */+ | ReviewOutcome_Deserialize;++/**+ * How the work a review decision started ended. Untagged so settlements+ * stored before imports existed, bare [`ReviewOutcome`]s, still read.+ */+export type ReviewSettlement_Serialize =+ /** An approved import's background write ended. */+ | {+ /** The import's final report. */+ import: ImportSettlement_Serialize;+ }+ /** The decided actions' confirmed results. */+ | ReviewOutcome_Serialize;+/** A run of a reply's parts that reads as one unit. */export type Segment = {/** Position among the reply's segments, from zero. */diff --git a/apps/web/src/lib/service-clients/service-agent-harness/client.ts b/apps/web/src/lib/service-clients/service-agent-harness/client.tsindex 43cf9289fa..4dd2828982 100644--- a/apps/web/src/lib/service-clients/service-agent-harness/client.ts+++ b/apps/web/src/lib/service-clients/service-agent-harness/client.ts@@ -14,6 +14,7 @@ import type {AgentSessionQueueResponse,AgentSessionResponse,AnswerToolApprovalResponse,+ AppConnectedRequest,CodingPreferencesBody,ControlRequest,ControlResponse,@@ -432,6 +433,23 @@ export const agentHarnessServiceClient = {);},+ /**+ * Continue the session now that its owner connected `appSlug`. The+ * harness wakes it with an `app.connected` system event, so the transcript+ * shows no prompt. 422 when the app is not actually connected.+ */+ reportAppConnected(sessionId: string, appSlug: string) {+ return fetchWithToken<Record<string, never>, typeof AI_USAGE_LIMIT_ERROR>(+ `${agentHarnessHost}/agent-sessions/${sessionId}/app-connections`,+ {+ method: 'POST',+ headers: { 'Content-Type': 'application/json' },+ body: JSON.stringify({ appSlug } satisfies AppConnectedRequest),+ errorResponseHandler: sessionAiError,+ }+ );+ },+setSessionSandboxSize(sessionId: string, size: SandboxSize) {return fetchWithToken<SandboxSizeBody>(`${agentHarnessHost}/agent-sessions/${sessionId}/sandbox-size`,diff --git a/apps/web/src/lib/service-clients/service-agent-harness/generated/client.ts b/apps/web/src/lib/service-clients/service-agent-harness/generated/client.tsindex 861b8dcc9d..e8be301c3f 100644--- a/apps/web/src/lib/service-clients/service-agent-harness/generated/client.ts+++ b/apps/web/src/lib/service-clients/service-agent-harness/generated/client.ts@@ -17,6 +17,7 @@ import type {AiAdmissionErrorBody,AnswerToolApprovalRequest,AnswerToolApprovalResponse,+ AppConnectedRequest,CodingPreferencesBody,ControlRequest,ControlResponse,@@ -1265,6 +1266,87 @@ export const deleteAgentSession = async (} as deleteAgentSessionResponse;};+/**+ * The harness produces an `app.connected` system event, which the transcript+shows as no prompt; it starts a turn once the session is idle.+ * @summary Continue the session now that its owner connected an app it asked for.+ */+export type reportAgentSessionAppConnectedResponse202 = {+ data: void;+ status: 202;+};++export type reportAgentSessionAppConnectedResponse401 = {+ data: string;+ status: 401;+};++export type reportAgentSessionAppConnectedResponse403 = {+ data: string;+ status: 403;+};++export type reportAgentSessionAppConnectedResponse409 = {+ data: string;+ status: 409;+};++export type reportAgentSessionAppConnectedResponse422 = {+ data: string;+ status: 422;+};++export type reportAgentSessionAppConnectedResponse500 = {+ data: string;+ status: 500;+};++export type reportAgentSessionAppConnectedResponseSuccess =+ reportAgentSessionAppConnectedResponse202 & {+ headers: Headers;+ };+export type reportAgentSessionAppConnectedResponseError = (+ | reportAgentSessionAppConnectedResponse401+ | reportAgentSessionAppConnectedResponse403+ | reportAgentSessionAppConnectedResponse409+ | reportAgentSessionAppConnectedResponse422+ | reportAgentSessionAppConnectedResponse500+) & {+ headers: Headers;+};++export type reportAgentSessionAppConnectedResponse =+ | reportAgentSessionAppConnectedResponseSuccess+ | reportAgentSessionAppConnectedResponseError;++export const getReportAgentSessionAppConnectedUrl = (sessionId: string) => {+ return `/agent-sessions/${sessionId}/app-connections`;+};++export const reportAgentSessionAppConnected = async (+ sessionId: string,+ appConnectedRequest: AppConnectedRequest,+ options?: RequestInit+): Promise<reportAgentSessionAppConnectedResponse> => {+ const res = await fetch(getReportAgentSessionAppConnectedUrl(sessionId), {+ ...options,+ method: 'POST',+ headers: { 'Content-Type': 'application/json', ...options?.headers },+ body: JSON.stringify(appConnectedRequest),+ });++ const body = [204, 205, 304].includes(res.status) ? null : await res.text();++ const data: reportAgentSessionAppConnectedResponse['data'] = body+ ? JSON.parse(body)+ : {};+ return {+ data,+ status: res.status,+ headers: res.headers,+ } as reportAgentSessionAppConnectedResponse;+};+/*** @summary Archive or unarchive an agent session.*/diff --git a/apps/web/src/lib/service-clients/service-agent-harness/generated/schemas/appConnectedRequest.ts b/apps/web/src/lib/service-clients/service-agent-harness/generated/schemas/appConnectedRequest.tsnew file mode 100644index 0000000000..1d657c5518--- /dev/null+++ b/apps/web/src/lib/service-clients/service-agent-harness/generated/schemas/appConnectedRequest.ts@@ -0,0 +1,16 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * agent_harness_service+ * OpenAPI spec version: 0.1.0+ */+import type { AppConnectedRequestActionId } from './appConnectedRequestActionId';++/**+ * Request body for reporting a connected app.+ */+export interface AppConnectedRequest {+ actionId?: AppConnectedRequestActionId;+ /** The Pipedream app slug the owner connected, e.g. `notion`. */+ appSlug: string;+}diff --git a/apps/web/src/lib/service-clients/service-agent-harness/generated/schemas/appConnectedRequestActionId.ts b/apps/web/src/lib/service-clients/service-agent-harness/generated/schemas/appConnectedRequestActionId.tsnew file mode 100644index 0000000000..5c9840cb44--- /dev/null+++ b/apps/web/src/lib/service-clients/service-agent-harness/generated/schemas/appConnectedRequestActionId.ts@@ -0,0 +1,9 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * agent_harness_service+ * OpenAPI spec version: 0.1.0+ */+import type { AgentActionId } from './agentActionId';++export type AppConnectedRequestActionId = null | AgentActionId;diff --git a/apps/web/src/lib/service-clients/service-agent-harness/generated/schemas/createReview.ts b/apps/web/src/lib/service-clients/service-agent-harness/generated/schemas/createReview.tsindex 91e9795ba4..160af9d0b4 100644--- a/apps/web/src/lib/service-clients/service-agent-harness/generated/schemas/createReview.ts+++ b/apps/web/src/lib/service-clients/service-agent-harness/generated/schemas/createReview.ts@@ -17,7 +17,8 @@ replyingToId?, attachments?: [{documentId}]}. Calendar: {title,time: {kind: "timed", startsAt: ISO datetime, endsAt: ISO datetime,timeZone?: IANA zone} or {kind: "allDay", startDate: ISO date,endDate: exclusive ISO date}, description?, location?, attendees?:-[{email, isOptional?}]}.+[{email, isOptional?}]}. Import: exactly one draft {source: "notion"},+presenting the plan ImportFromNotion staged.Creating any review only presents these drafts; the user acts separately. */drafts: unknown[];executionId?: CreateReviewExecutionId;diff --git a/apps/web/src/lib/service-clients/service-agent-harness/generated/schemas/index.ts b/apps/web/src/lib/service-clients/service-agent-harness/generated/schemas/index.tsindex 129757125c..6917ba9e2d 100644--- a/apps/web/src/lib/service-clients/service-agent-harness/generated/schemas/index.ts+++ b/apps/web/src/lib/service-clients/service-agent-harness/generated/schemas/index.ts@@ -97,6 +97,8 @@ export * from './agentSetModelAction';export * from './aiAdmissionErrorBody';export * from './answerToolApprovalRequest';export * from './answerToolApprovalResponse';+export * from './appConnectedRequest';+export * from './appConnectedRequestActionId';export * from './botId';export * from './capabilityHarnessDto';export * from './captureAttemptDto';diff --git a/apps/web/src/lib/service-clients/service-agent-harness/generated/schemas/reviewKind.ts b/apps/web/src/lib/service-clients/service-agent-harness/generated/schemas/reviewKind.tsindex 469dbbd0b1..4d1cc935ea 100644--- a/apps/web/src/lib/service-clients/service-agent-harness/generated/schemas/reviewKind.ts+++ b/apps/web/src/lib/service-clients/service-agent-harness/generated/schemas/reviewKind.ts@@ -14,4 +14,5 @@ export type ReviewKind = (typeof ReviewKind)[keyof typeof ReviewKind];export const ReviewKind = {email: 'email',calendar: 'calendar',+ import: 'import',} as const;diff --git a/apps/web/src/lib/service-clients/service-agent-harness/openapi.json b/apps/web/src/lib/service-clients/service-agent-harness/openapi.jsonindex 9242325de7..8c34272290 100644--- a/apps/web/src/lib/service-clients/service-agent-harness/openapi.json+++ b/apps/web/src/lib/service-clients/service-agent-harness/openapi.json@@ -956,6 +956,91 @@}}},+ "/agent-sessions/{session_id}/app-connections": {+ "post": {+ "tags": ["agent-sessions"],+ "summary": "Continue the session now that its owner connected an app it asked for.",+ "description": "The harness produces an `app.connected` system event, which the transcript\nshows as no prompt; it starts a turn once the session is idle.",+ "operationId": "report_agent_session_app_connected",+ "parameters": [+ {+ "name": "session_id",+ "in": "path",+ "description": "ID of the agent session",+ "required": true,+ "schema": {+ "type": "string",+ "format": "uuid"+ }+ }+ ],+ "requestBody": {+ "content": {+ "application/json": {+ "schema": {+ "$ref": "#/components/schemas/AppConnectedRequest"+ }+ }+ },+ "required": true+ },+ "responses": {+ "202": {+ "description": "The session continues once it is idle"+ },+ "401": {+ "description": "",+ "content": {+ "text/plain": {+ "schema": {+ "type": "string"+ }+ }+ }+ },+ "403": {+ "description": "Not the session's owner, or no edit access",+ "content": {+ "text/plain": {+ "schema": {+ "type": "string"+ }+ }+ }+ },+ "409": {+ "description": "The session is archived",+ "content": {+ "text/plain": {+ "schema": {+ "type": "string"+ }+ }+ }+ },+ "422": {+ "description": "The owner has not connected that app",+ "content": {+ "text/plain": {+ "schema": {+ "type": "string"+ }+ }+ }+ },+ "500": {+ "description": "",+ "content": {+ "text/plain": {+ "schema": {+ "type": "string"+ }+ }+ }+ }+ }+ }+ },"/agent-sessions/{session_id}/archived": {"put": {"tags": ["agent-sessions"],@@ -3531,6 +3616,28 @@}}},+ "AppConnectedRequest": {+ "type": "object",+ "description": "Request body for reporting a connected app.",+ "required": ["appSlug"],+ "properties": {+ "actionId": {+ "oneOf": [+ {+ "type": "null"+ },+ {+ "$ref": "#/components/schemas/AgentActionId",+ "description": "Names the report, so a retried one wakes the session once."+ }+ ]+ },+ "appSlug": {+ "type": "string",+ "description": "The Pipedream app slug the owner connected, e.g. `notion`."+ }+ }+ },"BotId": {"type": "string"},@@ -3865,7 +3972,7 @@"drafts": {"type": "array","items": {},- "description": "One to one hundred independent drafts. Email: {subject, body: Markdown,\nto: [{email, name?}], cc?: [{email, name?}], bcc?: [{email, name?}],\nreplyingToId?, attachments?: [{documentId}]}. Calendar: {title,\ntime: {kind: \"timed\", startsAt: ISO datetime, endsAt: ISO datetime,\ntimeZone?: IANA zone} or {kind: \"allDay\", startDate: ISO date,\nendDate: exclusive ISO date}, description?, location?, attendees?:\n[{email, isOptional?}]}.\nCreating any review only presents these drafts; the user acts separately."+ "description": "One to one hundred independent drafts. Email: {subject, body: Markdown,\nto: [{email, name?}], cc?: [{email, name?}], bcc?: [{email, name?}],\nreplyingToId?, attachments?: [{documentId}]}. Calendar: {title,\ntime: {kind: \"timed\", startsAt: ISO datetime, endsAt: ISO datetime,\ntimeZone?: IANA zone} or {kind: \"allDay\", startDate: ISO date,\nendDate: exclusive ISO date}, description?, location?, attendees?:\n[{email, isOptional?}]}. Import: exactly one draft {source: \"notion\"},\npresenting the plan ImportFromNotion staged.\nCreating any review only presents these drafts; the user acts separately."},"executionId": {"oneOf": [@@ -4815,7 +4922,7 @@"ReviewKind": {"type": "string","description": "The component and action used to finish a review.",- "enum": ["email", "calendar"]+ "enum": ["email", "calendar", "import"]},"ReviewRecord": {"type": "object",diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/client.ts b/apps/web/src/lib/service-clients/service-cognition/generated/client.tsindex cc87c21f48..e6d9019c94 100644--- a/apps/web/src/lib/service-clients/service-cognition/generated/client.ts+++ b/apps/web/src/lib/service-clients/service-cognition/generated/client.ts@@ -6,7 +6,6 @@*/import type {AddServerRequest,- AiAdmissionErrorBody,BrowsePipedreamMcpCatalogParams,CallToolRequest,CallToolResponse,@@ -33,6 +32,7 @@ import type {McpAuthCallbackParams,MemoryErrorBody,MemoryResponse,+ NotionImportReport,OnboardingRow,OnboardingState,PatchChatRequest,@@ -1315,236 +1315,212 @@ export const healthHandler = async (};/**- * @summary Accept staged imports (starts import jobs) and/or discard staged rows.+ * @summary Where the user's Notion workspace import stands.*/-export type runImportHandlerResponse200 = {- data: RunImportOutcome;+export type notionReportHandlerResponse200 = {+ data: NotionImportReport;status: 200;};-export type runImportHandlerResponse500 = {+export type notionReportHandlerResponse500 = {data: void;status: 500;};-export type runImportHandlerResponseSuccess = runImportHandlerResponse200 & {- headers: Headers;-};-export type runImportHandlerResponseError = runImportHandlerResponse500 & {- headers: Headers;-};+export type notionReportHandlerResponseSuccess =+ notionReportHandlerResponse200 & {+ headers: Headers;+ };+export type notionReportHandlerResponseError =+ notionReportHandlerResponse500 & {+ headers: Headers;+ };-export type runImportHandlerResponse =- | runImportHandlerResponseSuccess- | runImportHandlerResponseError;+export type notionReportHandlerResponse =+ | notionReportHandlerResponseSuccess+ | notionReportHandlerResponseError;-export const getRunImportHandlerUrl = () => {- return `/import/run`;+export const getNotionReportHandlerUrl = () => {+ return `/import/notion`;};-export const runImportHandler = async (- runImportRequest: RunImportRequest,+export const notionReportHandler = async (options?: RequestInit-): Promise<runImportHandlerResponse> => {- const res = await fetch(getRunImportHandlerUrl(), {+): Promise<notionReportHandlerResponse> => {+ const res = await fetch(getNotionReportHandlerUrl(), {...options,- method: 'POST',- headers: { 'Content-Type': 'application/json', ...options?.headers },- body: JSON.stringify(runImportRequest),+ method: 'GET',});const body = [204, 205, 304].includes(res.status) ? null : await res.text();- const data: runImportHandlerResponse['data'] = body ? JSON.parse(body) : {};+ const data: notionReportHandlerResponse['data'] = body+ ? JSON.parse(body)+ : {};return {data,status: res.status,headers: res.headers,- } as runImportHandlerResponse;+ } as notionReportHandlerResponse;};/**- * @summary Discover candidates for user selection without auto-importing them.+ * @summary Stop planning or writing the user's Notion import. Objects already+imported stay.*/-export type discoverHandlerResponse204 = {- data: void;- status: 204;-};--export type discoverHandlerResponse400 = {- data: void;- status: 400;+export type cancelNotionHandlerResponse200 = {+ data: NotionImportReport;+ status: 200;};-export type discoverHandlerResponse500 = {+export type cancelNotionHandlerResponse500 = {data: void;status: 500;};-export type discoverHandlerResponseSuccess = discoverHandlerResponse204 & {- headers: Headers;-};-export type discoverHandlerResponseError = (- | discoverHandlerResponse400- | discoverHandlerResponse500-) & {- headers: Headers;-};+export type cancelNotionHandlerResponseSuccess =+ cancelNotionHandlerResponse200 & {+ headers: Headers;+ };+export type cancelNotionHandlerResponseError =+ cancelNotionHandlerResponse500 & {+ headers: Headers;+ };-export type discoverHandlerResponse =- | discoverHandlerResponseSuccess- | discoverHandlerResponseError;+export type cancelNotionHandlerResponse =+ | cancelNotionHandlerResponseSuccess+ | cancelNotionHandlerResponseError;-export const getDiscoverHandlerUrl = (source: string) => {- return `/import/runs/${source}/discover`;+export const getCancelNotionHandlerUrl = () => {+ return `/import/notion/cancel`;};-export const discoverHandler = async (- source: string,+export const cancelNotionHandler = async (options?: RequestInit-): Promise<discoverHandlerResponse> => {- const res = await fetch(getDiscoverHandlerUrl(source), {+): Promise<cancelNotionHandlerResponse> => {+ const res = await fetch(getCancelNotionHandlerUrl(), {...options,method: 'POST',});const body = [204, 205, 304].includes(res.status) ? null : await res.text();- const data: discoverHandlerResponse['data'] = body ? JSON.parse(body) : {};+ const data: cancelNotionHandlerResponse['data'] = body+ ? JSON.parse(body)+ : {};return {data,status: res.status,headers: res.headers,- } as discoverHandlerResponse;+ } as cancelNotionHandlerResponse;};/**- * @summary Dismiss one source's import section.+ * @summary Accept staged imports (starts import jobs) and/or discard staged rows.*/-export type dismissRunHandlerResponse204 = {- data: void;- status: 204;-};--export type dismissRunHandlerResponse400 = {- data: void;- status: 400;+export type runImportHandlerResponse200 = {+ data: RunImportOutcome;+ status: 200;};-export type dismissRunHandlerResponse500 = {+export type runImportHandlerResponse500 = {data: void;status: 500;};-export type dismissRunHandlerResponseSuccess = dismissRunHandlerResponse204 & {+export type runImportHandlerResponseSuccess = runImportHandlerResponse200 & {headers: Headers;};-export type dismissRunHandlerResponseError = (- | dismissRunHandlerResponse400- | dismissRunHandlerResponse500-) & {+export type runImportHandlerResponseError = runImportHandlerResponse500 & {headers: Headers;};-export type dismissRunHandlerResponse =- | dismissRunHandlerResponseSuccess- | dismissRunHandlerResponseError;+export type runImportHandlerResponse =+ | runImportHandlerResponseSuccess+ | runImportHandlerResponseError;-export const getDismissRunHandlerUrl = (source: string) => {- return `/import/runs/${source}/dismiss`;+export const getRunImportHandlerUrl = () => {+ return `/import/run`;};-export const dismissRunHandler = async (- source: string,+export const runImportHandler = async (+ runImportRequest: RunImportRequest,options?: RequestInit-): Promise<dismissRunHandlerResponse> => {- const res = await fetch(getDismissRunHandlerUrl(source), {+): Promise<runImportHandlerResponse> => {+ const res = await fetch(getRunImportHandlerUrl(), {...options,method: 'POST',+ headers: { 'Content-Type': 'application/json', ...options?.headers },+ body: JSON.stringify(runImportRequest),});const body = [204, 205, 304].includes(res.status) ? null : await res.text();- const data: dismissRunHandlerResponse['data'] = body ? JSON.parse(body) : {};+ const data: runImportHandlerResponse['data'] = body ? JSON.parse(body) : {};return {data,status: res.status,headers: res.headers,- } as dismissRunHandlerResponse;+ } as runImportHandlerResponse;};/**- * @summary Restart a failed (or dismissed) gather run for one source.+ * @summary Discover candidates for user selection. Only Slack supports discovery.*/-export type retryGatherHandlerResponse204 = {+export type discoverHandlerResponse204 = {data: void;status: 204;};-export type retryGatherHandlerResponse400 = {+export type discoverHandlerResponse400 = {data: void;status: 400;};-export type retryGatherHandlerResponse402 = {- data: AiAdmissionErrorBody;- status: 402;-};--export type retryGatherHandlerResponse500 = {+export type discoverHandlerResponse500 = {data: void;status: 500;};-export type retryGatherHandlerResponse503 = {- data: AiAdmissionErrorBody;- status: 503;+export type discoverHandlerResponseSuccess = discoverHandlerResponse204 & {+ headers: Headers;};--export type retryGatherHandlerResponseSuccess =- retryGatherHandlerResponse204 & {- headers: Headers;- };-export type retryGatherHandlerResponseError = (- | retryGatherHandlerResponse400- | retryGatherHandlerResponse402- | retryGatherHandlerResponse500- | retryGatherHandlerResponse503+export type discoverHandlerResponseError = (+ | discoverHandlerResponse400+ | discoverHandlerResponse500) & {headers: Headers;};-export type retryGatherHandlerResponse =- | retryGatherHandlerResponseSuccess- | retryGatherHandlerResponseError;+export type discoverHandlerResponse =+ | discoverHandlerResponseSuccess+ | discoverHandlerResponseError;-export const getRetryGatherHandlerUrl = (source: string) => {- return `/import/runs/${source}/retry`;+export const getDiscoverHandlerUrl = (source: string) => {+ return `/import/runs/${source}/discover`;};-export const retryGatherHandler = async (+export const discoverHandler = async (source: string,options?: RequestInit-): Promise<retryGatherHandlerResponse> => {- const res = await fetch(getRetryGatherHandlerUrl(source), {+): Promise<discoverHandlerResponse> => {+ const res = await fetch(getDiscoverHandlerUrl(source), {...options,method: 'POST',});const body = [204, 205, 304].includes(res.status) ? null : await res.text();- const data: retryGatherHandlerResponse['data'] = body ? JSON.parse(body) : {};+ const data: discoverHandlerResponse['data'] = body ? JSON.parse(body) : {};return {data,status: res.status,headers: res.headers,- } as retryGatherHandlerResponse;+ } as discoverHandlerResponse;};/**- * @summary Get the authenticated user's onboarding state. While the flow is active-this also starts any gather runs that are due, so polling this endpoint-is what keeps onboarding moving.+ * @summary Get the authenticated user's onboarding state.*/export type getStateHandlerResponse200 = {data: OnboardingState;diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/aiAdmissionErrorBody.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/aiAdmissionErrorBody.tsdeleted file mode 100644index 0b3f7709bb..0000000000--- a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/aiAdmissionErrorBody.ts+++ /dev/null@@ -1,17 +0,0 @@-/**- * Generated by orval v7.21.0 🍺- * Do not edit manually.- * Document Cognition Service- * OpenAPI spec version: 1.0.0- */--/**- * Public admission error payload. Handlers with additional fields can reuse the-domain error's code and message and [`admission_status`].- */-export interface AiAdmissionErrorBody {- /** Stable denial or unavailability code. */- code: string;- /** Human-readable explanation, without internal billing diagnostics. */- error: string;-}diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/importEntity.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/importEntity.tsindex be9e440b23..c80292f1a5 100644--- a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/importEntity.ts+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/importEntity.ts@@ -7,7 +7,11 @@import type { ImportEntityEntityId } from './importEntityEntityId';import type { ImportEntityEntityType } from './importEntityEntityType';import type { ImportEntityLastError } from './importEntityLastError';+import type { ImportEntityParentId } from './importEntityParentId';+import type { ImportEntityReviewId } from './importEntityReviewId';import type { ImportEntityTeamId } from './importEntityTeamId';+import type { ImportEntityWaitingOn } from './importEntityWaitingOn';+import type { ImportKind } from './importKind';import type { ImportSource } from './importSource';import type { ImportStatus } from './importStatus';import type { Initiator } from './initiator';@@ -29,11 +33,19 @@ export interface ImportEntity {id: string;/** Where it was first staged from. */initiator: Initiator;+ /** What the item is in its source system. */+ kind: ImportKind;/** Failure detail from the last import attempt, when one failed. */last_error?: ImportEntityLastError;/** Per-source metadata (shape depends on `source`; see[`LinearIssueMeta`], [`NotionDocMeta`], [`SlackChannelMeta`]). */metadata: unknown;+ /** The import row this item was imported under (a subpage's parent page,+a row's database), when it was imported as part of a tree. */+ parent_id?: ImportEntityParentId;+ /** The agent session review that approved importing this item, when it+came from one. */+ review_id?: ImportEntityReviewId;/** The external system the item comes from. */source: ImportSource;/** Where the row is in its lifecycle. */@@ -46,4 +58,8 @@ export interface ImportEntity {are visible with their owner's id, so clients can say "created by ateammate". */user_id: string;+ /** The teammate's row this one waits on: that teammate is writing the+same object for the team, and this row links to their copy once it+is imported. */+ waiting_on?: ImportEntityWaitingOn;}diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/importEntityParentId.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/importEntityParentId.tsnew file mode 100644index 0000000000..882cfe02a6--- /dev/null+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/importEntityParentId.ts@@ -0,0 +1,12 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * Document Cognition Service+ * OpenAPI spec version: 1.0.0+ */++/**+ * The import row this item was imported under (a subpage's parent page,+a row's database), when it was imported as part of a tree.+ */+export type ImportEntityParentId = string | null;diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/importEntityReviewId.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/importEntityReviewId.tsnew file mode 100644index 0000000000..f7ce8c89bc--- /dev/null+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/importEntityReviewId.ts@@ -0,0 +1,12 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * Document Cognition Service+ * OpenAPI spec version: 1.0.0+ */++/**+ * The agent session review that approved importing this item, when it+came from one.+ */+export type ImportEntityReviewId = string | null;diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/importEntityWaitingOn.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/importEntityWaitingOn.tsnew file mode 100644index 0000000000..b5048da0aa--- /dev/null+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/importEntityWaitingOn.ts@@ -0,0 +1,13 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * Document Cognition Service+ * OpenAPI spec version: 1.0.0+ */++/**+ * The teammate's row this one waits on: that teammate is writing the+same object for the team, and this row links to their copy once it+is imported.+ */+export type ImportEntityWaitingOn = string | null;diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/importKind.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/importKind.tsnew file mode 100644index 0000000000..02b21902c7--- /dev/null+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/importKind.ts@@ -0,0 +1,25 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * Document Cognition Service+ * OpenAPI spec version: 1.0.0+ */++/**+ * What an imported item is in its source system. Each source has its own+kinds; a Notion database and a Notion page, or a Linear project and a+Linear issue, are tracked and deduplicated separately.+ */+export type ImportKind = (typeof ImportKind)[keyof typeof ImportKind];++// eslint-disable-next-line @typescript-eslint/no-redeclare+export const ImportKind = {+ issue: 'issue',+ project: 'project',+ document: 'document',+ page: 'page',+ database: 'database',+ data_source: 'data_source',+ row: 'row',+ channel: 'channel',+} as const;diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/importRun.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/importRun.tsindex 91f8541d0f..67e303d436 100644--- a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/importRun.ts+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/importRun.ts@@ -5,19 +5,22 @@* OpenAPI spec version: 1.0.0*/import type { ImportRunError } from './importRunError';+import type { ImportRunReviewId } from './importRunReviewId';+import type { ImportRunScanned } from './importRunScanned';import type { ImportSource } from './importSource';import type { RunStatus } from './runStatus';/**- * Gather-run state for one user × source.+ * Discovery-run state for one user × source.*/export interface ImportRun {- /** Whether this run should import its onboarding-staged candidates as-soon as gathering finishes. */- auto_import: boolean;- /** Gather or automatic-import failure detail, when the run failed. */+ /** Discovery failure detail, when the run failed. */error?: ImportRunError;- /** The source this run gathered from. */+ /** The review presenting the run's plan, once one does. A Notion plan is+presented while it is still `running`; rows staged after that take it. */+ review_id?: ImportRunReviewId;+ scanned?: ImportRunScanned;+ /** The source this run discovered from. */source: ImportSource;/** Where the run is in its lifecycle. */status: RunStatus;diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/importRunError.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/importRunError.tsindex 9b7ec14e05..218c7d904d 100644--- a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/importRunError.ts+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/importRunError.ts@@ -6,6 +6,6 @@*//**- * Gather or automatic-import failure detail, when the run failed.+ * Discovery failure detail, when the run failed.*/export type ImportRunError = string | null;diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/importRunReviewId.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/importRunReviewId.tsnew file mode 100644index 0000000000..eb0ec54e95--- /dev/null+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/importRunReviewId.ts@@ -0,0 +1,12 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * Document Cognition Service+ * OpenAPI spec version: 1.0.0+ */++/**+ * The review presenting the run's plan, once one does. A Notion plan is+presented while it is still `running`; rows staged after that take it.+ */+export type ImportRunReviewId = string | null;diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/importRunScanned.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/importRunScanned.tsnew file mode 100644index 0000000000..2ca7f6e2de--- /dev/null+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/importRunScanned.ts@@ -0,0 +1,9 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * Document Cognition Service+ * OpenAPI spec version: 1.0.0+ */+import type { ScanCounts } from './scanCounts';++export type ImportRunScanned = null | ScanCounts;diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/importState.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/importState.tsindex 1eaf4c95f4..44b466c45a 100644--- a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/importState.ts+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/importState.ts@@ -8,12 +8,12 @@ import type { ImportEntity } from './importEntity';import type { ImportRun } from './importRun';/**- * The full import aggregate for a user: gather runs plus visible ledger+ * The full import aggregate for a user: discovery runs plus visible ledgerrows (their own, and teammates' team-imported rows).*/export interface ImportState {/** Visible import entities, newest first. */entities: ImportEntity[];- /** Gather runs, one per source that ever gathered. */+ /** Discovery runs, one per source that ever ran discovery. */runs: ImportRun[];}diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/index.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/index.tsindex aa77045836..5cc96f0056 100644--- a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/index.ts+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/index.ts@@ -7,7 +7,6 @@export * from './accessLevel';export * from './addServerRequest';-export * from './aiAdmissionErrorBody';export * from './aiFeature';export * from './assistantMessagePart';export * from './assistantMessagePartOneOf';@@ -140,9 +139,15 @@ export * from './importEntity';export * from './importEntityEntityId';export * from './importEntityEntityType';export * from './importEntityLastError';+export * from './importEntityParentId';+export * from './importEntityReviewId';export * from './importEntityTeamId';+export * from './importEntityWaitingOn';+export * from './importKind';export * from './importRun';export * from './importRunError';+export * from './importRunReviewId';+export * from './importRunScanned';export * from './importSource';export * from './importState';export * from './importStatus';@@ -169,9 +174,33 @@ export * from './newChatMessage';export * from './newChatMessageAttachments';export * from './newChatMessageId';export * from './newMessageAttachment';+export * from './notionAlreadyImported';+export * from './notionDecision';export * from './notionDocMeta';+export * from './notionDocMetaPlan';export * from './notionDocMetaSummary';export * from './notionDocMetaUrl';+export * from './notionImporter';+export * from './notionImportReport';+export * from './notionImportReportError';+export * from './notionImportReportReviewId';+export * from './notionImportReportScanned';+export * from './notionImportReportStatus';+export * from './notionPlanItem';+export * from './notionPlanItemAlreadyImported';+export * from './notionPlanItemDecision';+export * from './notionPlanItemReason';+export * from './notionPlanItemRows';+export * from './notionPlanItemSharedReason';+export * from './notionPlanItemTaskFields';+export * from './notionReportItem';+export * from './notionReportItemReason';+export * from './notionReportItemUrl';+export * from './notionTaskFields';+export * from './notionTaskFieldsAssignee';+export * from './notionTaskFieldsDue';+export * from './notionTaskFieldsPriority';+export * from './notionTaskFieldsStatus';export * from './onboardingRow';export * from './onboardingRowCompletedAt';export * from './onboardingState';@@ -209,6 +238,7 @@ export * from './role';export * from './runImportOutcome';export * from './runImportRequest';export * from './runStatus';+export * from './scanCounts';export * from './sendChatMessagePayload';export * from './sendChatMessagePayloadAllOf';export * from './sendChatMessagePayloadAllOfAdditionalInstructions';diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionAlreadyImported.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionAlreadyImported.tsnew file mode 100644index 0000000000..9afc27a090--- /dev/null+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionAlreadyImported.ts@@ -0,0 +1,18 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * Document Cognition Service+ * OpenAPI spec version: 1.0.0+ */++/**+ * A teammate's copy of a Notion object, shared with the user's team.+ */+export interface NotionAlreadyImported {+ /** The teammate who imported it, by email. */+ by: string;+ /** The Macro entity it became. */+ entityId: string;+ /** That entity's type (`md`, `project`). */+ entityType: string;+}diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionDecision.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionDecision.tsnew file mode 100644index 0000000000..58c7f5a308--- /dev/null+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionDecision.ts@@ -0,0 +1,20 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * Document Cognition Service+ * OpenAPI spec version: 1.0.0+ */++/**+ * What a workspace import does with one Notion object.+ */+export type NotionDecision =+ (typeof NotionDecision)[keyof typeof NotionDecision];++// eslint-disable-next-line @typescript-eslint/no-redeclare+export const NotionDecision = {+ copy: 'copy',+ tasks: 'tasks',+ skip: 'skip',+ hand: 'hand',+} as const;diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionDocMeta.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionDocMeta.tsindex 0ae6f375af..2b9d81faf0 100644--- a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionDocMeta.ts+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionDocMeta.ts@@ -4,6 +4,7 @@* Document Cognition Service* OpenAPI spec version: 1.0.0*/+import type { NotionDocMetaPlan } from './notionDocMetaPlan';import type { NotionDocMetaSummary } from './notionDocMetaSummary';import type { NotionDocMetaUrl } from './notionDocMetaUrl';@@ -12,6 +13,7 @@ import type { NotionDocMetaUrl } from './notionDocMetaUrl';page bodies are fetched at import time, for accepted pages only.*/export interface NotionDocMeta {+ plan?: NotionDocMetaPlan;/** One-line summary of what the page contains. */summary?: NotionDocMetaSummary;/** Page title. */diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionDocMetaPlan.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionDocMetaPlan.tsnew file mode 100644index 0000000000..d46fe1d69e--- /dev/null+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionDocMetaPlan.ts@@ -0,0 +1,9 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * Document Cognition Service+ * OpenAPI spec version: 1.0.0+ */+import type { NotionPlanItem } from './notionPlanItem';++export type NotionDocMetaPlan = null | NotionPlanItem;diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionImportReport.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionImportReport.tsnew file mode 100644index 0000000000..3a5a52a3f5--- /dev/null+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionImportReport.ts@@ -0,0 +1,121 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * Document Cognition Service+ * OpenAPI spec version: 1.0.0+ */+import type { NotionImporter } from './notionImporter';+import type { NotionImportReportError } from './notionImportReportError';+import type { NotionImportReportReviewId } from './notionImportReportReviewId';+import type { NotionImportReportScanned } from './notionImportReportScanned';+import type { NotionImportReportStatus } from './notionImportReportStatus';+import type { NotionReportItem } from './notionReportItem';++/**+ * Where a Notion workspace import stands.+ */+export interface NotionImportReport {+ /** Who imported the objects already in Macro, most first. */+ alreadyImportedBy?: NotionImporter[];+ /**+ * Plan objects a teammate already imported and shared with the team;+they link to that copy and are not counted as copies.+ * @minimum 0+ */+ alreadyInMacro?: number;+ /**+ * Objects to copy as they are.+ * @minimum 0+ */+ copy: number;+ /**+ * Data sources (database tables) in the plan.+ * @minimum 0+ */+ dataSources: number;+ /**+ * Rows those databases hold, every one counted while planning.+ * @minimum 0+ */+ databaseRows: number;+ /**+ * Data sources written as databases.+ * @minimum 0+ */+ databases: number;+ /**+ * Plan objects the decision model has not answered for yet, while+planning is still `running`.+ * @minimum 0+ */+ deciding?: number;+ /**+ * Pages written as documents.+ * @minimum 0+ */+ documents: number;+ /** Why the run failed, when it did. */+ error?: NotionImportReportError;+ /** Objects that failed to import, with the failure. */+ failures?: NotionReportItem[];+ /**+ * Objects that need handling by hand.+ * @minimum 0+ */+ hand: number;+ /** Objects that need handling by hand, with reasons. */+ handItems?: NotionReportItem[];+ /**+ * Plan objects already imported.+ * @minimum 0+ */+ imported: number;+ /** Whether the user is on a team. Without one, everything the import+writes stays visible only to them. */+ onTeam?: boolean;+ /**+ * Pages in the plan.+ * @minimum 0+ */+ pages: number;+ /** The review the plan is tied to, once presented. */+ reviewId?: NotionImportReportReviewId;+ scanned?: NotionImportReportScanned;+ /**+ * Written plan objects shared with the user's team.+ * @minimum 0+ */+ sharedWithTeam?: number;+ /**+ * Objects left in Notion.+ * @minimum 0+ */+ skip: number;+ status?: NotionImportReportStatus;+ /**+ * Tasks the task lists become, one per row.+ * @minimum 0+ */+ taskRows: number;+ /**+ * Data sources whose rows become tasks.+ * @minimum 0+ */+ tasks: number;+ /**+ * Plan objects still waiting to be written.+ * @minimum 0+ */+ waiting: number;+ /**+ * Plan objects waiting for a teammate who is writing the same object+for the team; they link to the teammate's copy once it is written.+ * @minimum 0+ */+ waitingOnTeammates?: number;+ /**+ * Plan objects being written now.+ * @minimum 0+ */+ writing: number;+}diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionImportReportError.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionImportReportError.tsnew file mode 100644index 0000000000..dd8efb0a8d--- /dev/null+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionImportReportError.ts@@ -0,0 +1,11 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * Document Cognition Service+ * OpenAPI spec version: 1.0.0+ */++/**+ * Why the run failed, when it did.+ */+export type NotionImportReportError = string | null;diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionImportReportReviewId.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionImportReportReviewId.tsnew file mode 100644index 0000000000..da2d4af282--- /dev/null+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionImportReportReviewId.ts@@ -0,0 +1,11 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * Document Cognition Service+ * OpenAPI spec version: 1.0.0+ */++/**+ * The review the plan is tied to, once presented.+ */+export type NotionImportReportReviewId = string | null;diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionImportReportScanned.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionImportReportScanned.tsnew file mode 100644index 0000000000..86ab26cbab--- /dev/null+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionImportReportScanned.ts@@ -0,0 +1,9 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * Document Cognition Service+ * OpenAPI spec version: 1.0.0+ */+import type { ScanCounts } from './scanCounts';++export type NotionImportReportScanned = null | ScanCounts;diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionImportReportStatus.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionImportReportStatus.tsnew file mode 100644index 0000000000..8bbb37f560--- /dev/null+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionImportReportStatus.ts@@ -0,0 +1,9 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * Document Cognition Service+ * OpenAPI spec version: 1.0.0+ */+import type { RunStatus } from './runStatus';++export type NotionImportReportStatus = null | RunStatus;diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionImporter.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionImporter.tsnew file mode 100644index 0000000000..31928d9a67--- /dev/null+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionImporter.ts@@ -0,0 +1,19 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * Document Cognition Service+ * OpenAPI spec version: 1.0.0+ */++/**+ * A teammate who already imported some of a plan's objects.+ */+export interface NotionImporter {+ /** The teammate, by email. */+ by: string;+ /**+ * How many of the plan's objects they imported.+ * @minimum 0+ */+ count: number;+}diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionPlanItem.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionPlanItem.tsnew file mode 100644index 0000000000..395e6cbcfd--- /dev/null+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionPlanItem.ts@@ -0,0 +1,39 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * Document Cognition Service+ * OpenAPI spec version: 1.0.0+ */+import type { NotionPlanItemAlreadyImported } from './notionPlanItemAlreadyImported';+import type { NotionPlanItemDecision } from './notionPlanItemDecision';+import type { NotionPlanItemReason } from './notionPlanItemReason';+import type { NotionPlanItemRows } from './notionPlanItemRows';+import type { NotionPlanItemSharedReason } from './notionPlanItemSharedReason';+import type { NotionPlanItemTaskFields } from './notionPlanItemTaskFields';++/**+ * One object's place in a workspace import plan.+ */+export interface NotionPlanItem {+ alreadyImported?: NotionPlanItemAlreadyImported;+ decision?: NotionPlanItemDecision;+ /** Titles of the pages above it, outermost first. */+ path?: string[];+ /** Why, in a short sentence, when the decision model gave one. */+ reason?: NotionPlanItemReason;+ /**+ * For a data source: rows counted while planning.+ * @minimum 0+ */+ rows?: NotionPlanItemRows;+ /** For a data source: the titles of a few of its rows. */+ sampleTitles?: string[];+ /** Whether the user may change `shared`: false for users without a team. */+ shareable?: boolean;+ /** Whether what it becomes is shared with the user's team. */+ shared?: boolean;+ /** Why it is (or is not) shared, when the decision model chose or the+import could not share it. */+ sharedReason?: NotionPlanItemSharedReason;+ taskFields?: NotionPlanItemTaskFields;+}diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionPlanItemAlreadyImported.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionPlanItemAlreadyImported.tsnew file mode 100644index 0000000000..453e639078--- /dev/null+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionPlanItemAlreadyImported.ts@@ -0,0 +1,9 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * Document Cognition Service+ * OpenAPI spec version: 1.0.0+ */+import type { NotionAlreadyImported } from './notionAlreadyImported';++export type NotionPlanItemAlreadyImported = null | NotionAlreadyImported;diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionPlanItemDecision.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionPlanItemDecision.tsnew file mode 100644index 0000000000..dbe47befcc--- /dev/null+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionPlanItemDecision.ts@@ -0,0 +1,9 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * Document Cognition Service+ * OpenAPI spec version: 1.0.0+ */+import type { NotionDecision } from './notionDecision';++export type NotionPlanItemDecision = null | NotionDecision;diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionPlanItemReason.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionPlanItemReason.tsnew file mode 100644index 0000000000..81e03ac420--- /dev/null+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionPlanItemReason.ts@@ -0,0 +1,11 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * Document Cognition Service+ * OpenAPI spec version: 1.0.0+ */++/**+ * Why, in a short sentence, when the decision model gave one.+ */+export type NotionPlanItemReason = string | null;diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionPlanItemRows.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionPlanItemRows.tsnew file mode 100644index 0000000000..24c65623d8--- /dev/null+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionPlanItemRows.ts@@ -0,0 +1,12 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * Document Cognition Service+ * OpenAPI spec version: 1.0.0+ */++/**+ * For a data source: rows counted while planning.+ * @minimum 0+ */+export type NotionPlanItemRows = number | null;diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionPlanItemSharedReason.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionPlanItemSharedReason.tsnew file mode 100644index 0000000000..131381b2f4--- /dev/null+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionPlanItemSharedReason.ts@@ -0,0 +1,12 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * Document Cognition Service+ * OpenAPI spec version: 1.0.0+ */++/**+ * Why it is (or is not) shared, when the decision model chose or the+import could not share it.+ */+export type NotionPlanItemSharedReason = string | null;diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionPlanItemTaskFields.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionPlanItemTaskFields.tsnew file mode 100644index 0000000000..0d704bb508--- /dev/null+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionPlanItemTaskFields.ts@@ -0,0 +1,9 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * Document Cognition Service+ * OpenAPI spec version: 1.0.0+ */+import type { NotionTaskFields } from './notionTaskFields';++export type NotionPlanItemTaskFields = null | NotionTaskFields;diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionReportItem.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionReportItem.tsnew file mode 100644index 0000000000..39397af8dc--- /dev/null+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionReportItem.ts@@ -0,0 +1,25 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * Document Cognition Service+ * OpenAPI spec version: 1.0.0+ */+import type { ImportKind } from './importKind';+import type { NotionReportItemReason } from './notionReportItemReason';+import type { NotionReportItemUrl } from './notionReportItemUrl';++/**+ * One object a workspace import report calls out.+ */+export interface NotionReportItem {+ /** The ledger row. */+ importId: string;+ /** What the object is. */+ kind: ImportKind;+ /** Why it is called out: the decision model's reason, or the failure. */+ reason?: NotionReportItemReason;+ /** The object's title. */+ title: string;+ /** Its Notion URL. */+ url?: NotionReportItemUrl;+}diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionReportItemReason.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionReportItemReason.tsnew file mode 100644index 0000000000..7493b59ce8--- /dev/null+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionReportItemReason.ts@@ -0,0 +1,11 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * Document Cognition Service+ * OpenAPI spec version: 1.0.0+ */++/**+ * Why it is called out: the decision model's reason, or the failure.+ */+export type NotionReportItemReason = string | null;diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionReportItemUrl.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionReportItemUrl.tsnew file mode 100644index 0000000000..dc834c1455--- /dev/null+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionReportItemUrl.ts@@ -0,0 +1,11 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * Document Cognition Service+ * OpenAPI spec version: 1.0.0+ */++/**+ * Its Notion URL.+ */+export type NotionReportItemUrl = string | null;diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionTaskFields.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionTaskFields.tsnew file mode 100644index 0000000000..ab98f8d9f9--- /dev/null+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionTaskFields.ts@@ -0,0 +1,24 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * Document Cognition Service+ * OpenAPI spec version: 1.0.0+ */+import type { NotionTaskFieldsAssignee } from './notionTaskFieldsAssignee';+import type { NotionTaskFieldsDue } from './notionTaskFieldsDue';+import type { NotionTaskFieldsPriority } from './notionTaskFieldsPriority';+import type { NotionTaskFieldsStatus } from './notionTaskFieldsStatus';++/**+ * Which Notion columns hold a task list's task fields, by property name.+ */+export interface NotionTaskFields {+ /** The assignee column. */+ assignee?: NotionTaskFieldsAssignee;+ /** The due date column. */+ due?: NotionTaskFieldsDue;+ /** The priority column. */+ priority?: NotionTaskFieldsPriority;+ /** The status column. */+ status?: NotionTaskFieldsStatus;+}diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionTaskFieldsAssignee.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionTaskFieldsAssignee.tsnew file mode 100644index 0000000000..6e212ba420--- /dev/null+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionTaskFieldsAssignee.ts@@ -0,0 +1,11 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * Document Cognition Service+ * OpenAPI spec version: 1.0.0+ */++/**+ * The assignee column.+ */+export type NotionTaskFieldsAssignee = string | null;diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionTaskFieldsDue.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionTaskFieldsDue.tsnew file mode 100644index 0000000000..ae8d602a7e--- /dev/null+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionTaskFieldsDue.ts@@ -0,0 +1,11 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * Document Cognition Service+ * OpenAPI spec version: 1.0.0+ */++/**+ * The due date column.+ */+export type NotionTaskFieldsDue = string | null;diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionTaskFieldsPriority.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionTaskFieldsPriority.tsnew file mode 100644index 0000000000..20f59efe1b--- /dev/null+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionTaskFieldsPriority.ts@@ -0,0 +1,11 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * Document Cognition Service+ * OpenAPI spec version: 1.0.0+ */++/**+ * The priority column.+ */+export type NotionTaskFieldsPriority = string | null;diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionTaskFieldsStatus.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionTaskFieldsStatus.tsnew file mode 100644index 0000000000..9e9aa1c005--- /dev/null+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionTaskFieldsStatus.ts@@ -0,0 +1,11 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * Document Cognition Service+ * OpenAPI spec version: 1.0.0+ */++/**+ * The status column.+ */+export type NotionTaskFieldsStatus = string | null;diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/runStatus.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/runStatus.tsindex 9976265c9a..e83c5bb8a9 100644--- a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/runStatus.ts+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/runStatus.ts@@ -6,7 +6,7 @@*//**- * Lifecycle of a gather run (one per user × source).+ * Lifecycle of a discovery run (one per user × source).*/export type RunStatus = (typeof RunStatus)[keyof typeof RunStatus];diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/scanCounts.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/scanCounts.tsnew file mode 100644index 0000000000..bdd48b6229--- /dev/null+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/scanCounts.ts@@ -0,0 +1,22 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * Document Cognition Service+ * OpenAPI spec version: 1.0.0+ */++/**+ * What a planning run has found in the source so far.+ */+export interface ScanCounts {+ /**+ * Data sources (database tables) found.+ * @minimum 0+ */+ dataSources: number;+ /**+ * Pages found.+ * @minimum 0+ */+ pages: number;+}diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/tools/schemas.ts b/apps/web/src/lib/service-clients/service-cognition/generated/tools/schemas.tsindex 1031b30dca..d74d57f305 100644--- a/apps/web/src/lib/service-clients/service-cognition/generated/tools/schemas.ts+++ b/apps/web/src/lib/service-clients/service-cognition/generated/tools/schemas.ts@@ -9170,6 +9170,203 @@ export const ImportCompaniesCsvResponse = z.object({summary: z.string(),});+export const ImportFromNotion = z.object({+ action: z.any().superRefine((x, ctx) => {+ const schemas = [+ z.literal('plan'),+ z.literal('status'),+ z.literal('cancel'),+ ];+ const errors = schemas.reduce<z.ZodError[]>(+ (errors, schema) =>+ ((result) => (result.error ? [...errors, result.error] : errors))(+ schema.safeParse(x)+ ),+ []+ );+ if (schemas.length - errors.length !== 1) {+ ctx.addIssue({+ path: ctx.path,+ code: 'invalid_union',+ unionErrors: errors,+ message: 'Invalid input: Should pass single schema',+ });+ }+ }),+ roots: z.array(z.string()).optional(),+ sharing: z+ .union([+ z.any().superRefine((x, ctx) => {+ const schemas = [+ z.literal('team'),+ z.literal('personal'),+ z.literal('ai'),+ ];+ const errors = schemas.reduce<z.ZodError[]>(+ (errors, schema) =>+ ((result) => (result.error ? [...errors, result.error] : errors))(+ schema.safeParse(x)+ ),+ []+ );+ if (schemas.length - errors.length !== 1) {+ ctx.addIssue({+ path: ctx.path,+ code: 'invalid_union',+ unionErrors: errors,+ message: 'Invalid input: Should pass single schema',+ });+ }+ }),+ z.null(),+ ])+ .optional(),+});++export const ImportFromNotionResponse = z.object({+ report: z.object({+ status: z+ .union([+ z.any().superRefine((x, ctx) => {+ const schemas = [+ z.literal('running'),+ z.literal('ready'),+ z.literal('importing'),+ z.literal('completed'),+ z.literal('failed'),+ z.literal('dismissed'),+ ];+ const errors = schemas.reduce<z.ZodError[]>(+ (errors, schema) =>+ ((result) => (result.error ? [...errors, result.error] : errors))(+ schema.safeParse(x)+ ),+ []+ );+ if (schemas.length - errors.length !== 1) {+ ctx.addIssue({+ path: ctx.path,+ code: 'invalid_union',+ unionErrors: errors,+ message: 'Invalid input: Should pass single schema',+ });+ }+ }),+ z.null(),+ ])+ .optional(),+ error: z.union([z.string(), z.null()]).optional(),+ scanned: z+ .union([+ z.object({+ pages: z.number().int().gte(0),+ dataSources: z.number().int().gte(0),+ }),+ z.null(),+ ])+ .optional(),+ pages: z.number().int().gte(0),+ dataSources: z.number().int().gte(0),+ deciding: z.number().int().gte(0).optional(),+ onTeam: z.boolean().optional(),+ copy: z.number().int().gte(0),+ tasks: z.number().int().gte(0),+ skip: z.number().int().gte(0),+ hand: z.number().int().gte(0),+ documents: z.number().int().gte(0),+ databases: z.number().int().gte(0),+ databaseRows: z.number().int().gte(0),+ taskRows: z.number().int().gte(0),+ imported: z.number().int().gte(0),+ waiting: z.number().int().gte(0),+ writing: z.number().int().gte(0),+ waitingOnTeammates: z.number().int().gte(0).optional(),+ sharedWithTeam: z.number().int().gte(0).optional(),+ alreadyInMacro: z.number().int().gte(0).optional(),+ alreadyImportedBy: z+ .array(z.object({ by: z.string(), count: z.number().int().gte(0) }))+ .optional(),+ reviewId: z.union([z.string().uuid(), z.null()]).optional(),+ handItems: z+ .array(+ z.object({+ importId: z.string().uuid(),+ title: z.string(),+ kind: z.any().superRefine((x, ctx) => {+ const schemas = [+ z.literal('issue'),+ z.literal('project'),+ z.literal('document'),+ z.literal('page'),+ z.literal('database'),+ z.literal('data_source'),+ z.literal('row'),+ z.literal('channel'),+ ];+ const errors = schemas.reduce<z.ZodError[]>(+ (errors, schema) =>+ ((result) =>+ result.error ? [...errors, result.error] : errors)(+ schema.safeParse(x)+ ),+ []+ );+ if (schemas.length - errors.length !== 1) {+ ctx.addIssue({+ path: ctx.path,+ code: 'invalid_union',+ unionErrors: errors,+ message: 'Invalid input: Should pass single schema',+ });+ }+ }),+ url: z.union([z.string(), z.null()]).optional(),+ reason: z.union([z.string(), z.null()]).optional(),+ })+ )+ .optional(),+ failures: z+ .array(+ z.object({+ importId: z.string().uuid(),+ title: z.string(),+ kind: z.any().superRefine((x, ctx) => {+ const schemas = [+ z.literal('issue'),+ z.literal('project'),+ z.literal('document'),+ z.literal('page'),+ z.literal('database'),+ z.literal('data_source'),+ z.literal('row'),+ z.literal('channel'),+ ];+ const errors = schemas.reduce<z.ZodError[]>(+ (errors, schema) =>+ ((result) =>+ result.error ? [...errors, result.error] : errors)(+ schema.safeParse(x)+ ),+ []+ );+ if (schemas.length - errors.length !== 1) {+ ctx.addIssue({+ path: ctx.path,+ code: 'invalid_union',+ unionErrors: errors,+ message: 'Invalid input: Should pass single schema',+ });+ }+ }),+ url: z.union([z.string(), z.null()]).optional(),+ reason: z.union([z.string(), z.null()]).optional(),+ })+ )+ .optional(),+ }),+ message: z.string(),+});+export const ImportNotionPage = z.object({ pageUrl: z.string() });export const ImportNotionPageResponse = z.object({diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/tools/tool.ts b/apps/web/src/lib/service-clients/service-cognition/generated/tools/tool.tsindex 90dde95bf5..33df89cf16 100644--- a/apps/web/src/lib/service-clients/service-cognition/generated/tools/tool.ts+++ b/apps/web/src/lib/service-clients/service-cognition/generated/tools/tool.ts@@ -235,6 +235,10 @@ type ToolParserMap = {call: types.ImportCompaniesCsv;response: types.ImportCompaniesCsvResponse;};+ ImportFromNotion: {+ call: types.ImportFromNotion;+ response: types.ImportFromNotionResponse;+ };ImportNotionPage: {call: types.ImportNotionPage;response: types.ImportNotionPageResponse;@@ -775,6 +779,10 @@ const toolParserMap = {call: schemas.ImportCompaniesCsv,response: schemas.ImportCompaniesCsvResponse,},+ ImportFromNotion: {+ call: schemas.ImportFromNotion,+ response: schemas.ImportFromNotionResponse,+ },ImportNotionPage: {call: schemas.ImportNotionPage,response: schemas.ImportNotionPageResponse,@@ -1353,6 +1361,10 @@ type ToolDataMap = {call: types.ImportCompaniesCsv;response: types.ImportCompaniesCsvResponse;};+ ImportFromNotion: {+ call: types.ImportFromNotion;+ response: types.ImportFromNotionResponse;+ };ImportNotionPage: {call: types.ImportNotionPage;response: types.ImportNotionPageResponse;diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/tools/types.ts b/apps/web/src/lib/service-clients/service-cognition/generated/tools/types.tsindex 5459840cb3..af57e9f563 100644--- a/apps/web/src/lib/service-clients/service-cognition/generated/tools/types.ts+++ b/apps/web/src/lib/service-clients/service-cognition/generated/tools/types.ts@@ -3597,6 +3597,38 @@ export type AvailabilityUnknownReason =| 'invalid_time_zone'| 'invalid_interval'| 'conflicting_copies';+/**+ * What the tool is asked to do.+ */+export type NotionImportAction = 'plan' | 'status' | 'cancel';+/**+ * Who a Notion workspace import shares what it writes with.+ */+export type NotionSharing = 'team' | 'personal' | 'ai';+/**+ * Lifecycle of a discovery run (one per user × source).+ */+export type RunStatus =+ | 'running'+ | 'ready'+ | 'importing'+ | 'completed'+ | 'failed'+ | 'dismissed';+/**+ * What an imported item is in its source system. Each source has its own+ * kinds; a Notion database and a Notion page, or a Linear project and a+ * Linear issue, are tracked and deduplicated separately.+ */+export type ImportKind =+ | 'issue'+ | 'project'+ | 'document'+ | 'page'+ | 'database'+ | 'data_source'+ | 'row'+ | 'channel';/*** Provider selected from the persisted calendar account and email binding.*/@@ -10728,6 +10760,194 @@ export interface ToolAddedOptions {*/labels: string[];}+/**+ * Import the user's Notion workspace (or part of it) into Macro: pages become documents in folders mirroring Notion's page tree, databases become Macro databases with their rows and relations, and task-tracker databases become Macro tasks. `plan` reads the workspace through the user's Notion connection, decides per page and database what to do (and, with `sharing` "ai", who sees it), links anything a teammate already imported and shared, and stages a plan without writing anything. Who sees the import: unless the user already said, call `plan` without `sharing` first. A user who is not on a team gets a personal import straight away; tell them so and do not offer team sharing. For a user on a team it answers that `sharing` is required: then ask them in one short question with three options (shared with their team, only them, or let AI decide per page), do not guess, and plan again with their answer. `plan` answers within seconds while planning goes on in the background: present the plan right away with PresentReview (kind "import", drafts [{"source":"notion"}]). The review fills in as Notion is read and each object is decided, and the user approves it there once it is ready; do not wait for planning or check `status` in a loop. Approving that review writes the plan; never import plan items with other tools. End your message after presenting it by asking "Should I kick it off?"; if the user says yes, approve it yourself with DecideReview, submitting the reviewId PresentReview returned. `status` reports progress; `cancel` stops planning or importing. Use this for anything bigger than a single page instead of ImportNotionPage.+ */+export interface ImportFromNotion {+ action: NotionImportAction;+ /**+ * For `plan`: Notion page or database URLs (or ids) to import, each+ * with everything under it. Leave empty to plan everything the+ * connection can see.+ */+ roots?: string[];+ /**+ * For `plan`: who sees what the import writes, as the user answered+ * when asked: `team` shares everything with their team, `personal`+ * keeps everything visible only to them, `ai` lets the decision model+ * choose per page. Leave it out for a user who is not on a team.+ */+ sharing?: NotionSharing | null;+}+/**+ * The tool's answer.+ */+export interface ImportFromNotionResponse {+ report: NotionImportReport;+ /**+ * What happened and what to do next.+ */+ message: string;+}+/**+ * Where a Notion workspace import stands.+ */+export interface NotionImportReport {+ /**+ * The run's state: `running` while planning, `ready` once a plan is+ * staged, `importing` while writing, `completed`, `failed`, or+ * `dismissed` when cancelled. Absent before the first run.+ */+ status?: RunStatus | null;+ /**+ * Why the run failed, when it did.+ */+ error?: string | null;+ /**+ * What planning has found in Notion so far, while it is `running`.+ */+ scanned?: ScanCounts | null;+ /**+ * Pages in the plan.+ */+ pages: number;+ /**+ * Data sources (database tables) in the plan.+ */+ dataSources: number;+ /**+ * Plan objects the decision model has not answered for yet, while+ * planning is still `running`.+ */+ deciding?: number;+ /**+ * Whether the user is on a team. Without one, everything the import+ * writes stays visible only to them.+ */+ onTeam?: boolean;+ /**+ * Objects to copy as they are.+ */+ copy: number;+ /**+ * Data sources whose rows become tasks.+ */+ tasks: number;+ /**+ * Objects left in Notion.+ */+ skip: number;+ /**+ * Objects that need handling by hand.+ */+ hand: number;+ /**+ * Pages written as documents.+ */+ documents: number;+ /**+ * Data sources written as databases.+ */+ databases: number;+ /**+ * Rows those databases hold, every one counted while planning.+ */+ databaseRows: number;+ /**+ * Tasks the task lists become, one per row.+ */+ taskRows: number;+ /**+ * Plan objects already imported.+ */+ imported: number;+ /**+ * Plan objects still waiting to be written.+ */+ waiting: number;+ /**+ * Plan objects being written now.+ */+ writing: number;+ /**+ * Plan objects waiting for a teammate who is writing the same object+ * for the team; they link to the teammate's copy once it is written.+ */+ waitingOnTeammates?: number;+ /**+ * Written plan objects shared with the user's team.+ */+ sharedWithTeam?: number;+ /**+ * Plan objects a teammate already imported and shared with the team;+ * they link to that copy and are not counted as copies.+ */+ alreadyInMacro?: number;+ /**+ * Who imported the objects already in Macro, most first.+ */+ alreadyImportedBy?: NotionImporter[];+ /**+ * The review the plan is tied to, once presented.+ */+ reviewId?: string | null;+ /**+ * Objects that need handling by hand, with reasons.+ */+ handItems?: NotionReportItem[];+ /**+ * Objects that failed to import, with the failure.+ */+ failures?: NotionReportItem[];+}+/**+ * What a planning run has found in the source so far.+ */+export interface ScanCounts {+ /**+ * Pages found.+ */+ pages: number;+ /**+ * Data sources (database tables) found.+ */+ dataSources: number;+}+/**+ * A teammate who already imported some of a plan's objects.+ */+export interface NotionImporter {+ /**+ * The teammate, by email.+ */+ by: string;+ /**+ * How many of the plan's objects they imported.+ */+ count: number;+}+/**+ * One object a workspace import report calls out.+ */+export interface NotionReportItem {+ /**+ * The ledger row.+ */+ importId: string;+ /**+ * The object's title.+ */+ title: string;+ kind: ImportKind;+ /**+ * Its Notion URL.+ */+ url?: string | null;+ /**+ * Why it is called out: the decision model's reason, or the failure.+ */+ reason?: string | null;+}/*** Import one specific Notion page through Macro's canonical Notion importer. Use this when the user explicitly asks to import a page URL or id. The tool performs deduplication, fetches through the user's connected Notion MCP, normalizes the page, creates the Macro markdown document, and returns its entity id. Do not fetch and recreate the page manually with generic document tools. Notion databases and database-first pages are intentionally not imported.*/diff --git a/apps/web/src/lib/service-clients/service-cognition/import.ts b/apps/web/src/lib/service-clients/service-cognition/import.tsindex 1066c7220e..fbd64e87f7 100644--- a/apps/web/src/lib/service-clients/service-cognition/import.ts+++ b/apps/web/src/lib/service-clients/service-cognition/import.ts@@ -17,6 +17,9 @@ import {type AI_USAGE_LIMIT_ERROR,aiUsageErrorResponseHandler,} from '../ai-usage-limit';+import type { NotionImportReport } from './generated/schemas';++export type { NotionImportReport } from './generated/schemas';const dcsHost: string = SERVER_HOSTS['cognition-service'];@@ -39,6 +42,17 @@ function importFetch<T extends ObjectLike = never>(export type ImportSource = 'linear' | 'notion' | 'slack';+/** What an imported item is in its source system. */+export type ImportKind =+ | 'issue'+ | 'project'+ | 'document'+ | 'page'+ | 'database'+ | 'data_source'+ | 'row'+ | 'channel';+export type ImportEntityStatus =| 'staged'| 'importing'@@ -97,7 +111,12 @@ export type ImportEntity = {user_id: string;team_id?: string | null;source: ImportSource;+ kind: ImportKind;foreign_id: string;+ /** The row this item was imported under, when it was part of a tree. */+ parent_id?: string | null;+ /** The agent session review that approved importing it, if any. */+ review_id?: string | null;status: ImportEntityStatus;initiator: ImportInitiator;/** Per-source metadata; shape keyed by `source` (see the Meta types). */@@ -109,11 +128,10 @@ export type ImportEntity = {updated_at: string;};-/** Gather-run state for one source. */+/** Discovery-run state for one source. */export type ImportRun = {source: ImportSource;status: ImportRunStatus;- auto_import: boolean;error?: string | null;updated_at: string;};@@ -129,7 +147,7 @@ export type RunImportOutcome = {};export const importClient = {- /** Gather runs plus visible ledger rows for the authenticated user. */+ /** Discovery runs plus visible ledger rows for the authenticated user. */async getState() {return await importFetch<ImportState>('/import/state', { method: 'GET' });},@@ -153,20 +171,16 @@ export const importClient = {});},- /** Restart a failed gather run for one source. */- async retryGather(source: ImportSource) {- return await fetchWithToken<{}, typeof AI_USAGE_LIMIT_ERROR>(- `${dcsHost}/import/runs/${source}/retry`,- {- method: 'POST',- errorResponseHandler: aiUsageErrorResponseHandler,- }- );+ /** Where the Notion workspace import stands. */+ async getNotionReport() {+ return await importFetch<NotionImportReport>('/import/notion', {+ method: 'GET',+ });},- /** Dismiss one source's import section. */- async dismissRun(source: ImportSource) {- return await importFetch(`/import/runs/${source}/dismiss`, {+ /** Stop planning or writing the Notion workspace import. */+ async cancelNotion() {+ return await importFetch<NotionImportReport>('/import/notion/cancel', {method: 'POST',});},diff --git a/apps/web/src/lib/service-clients/service-cognition/openapi.json b/apps/web/src/lib/service-clients/service-cognition/openapi.jsonindex 53e220375c..de7b85ca6b 100644--- a/apps/web/src/lib/service-clients/service-cognition/openapi.json+++ b/apps/web/src/lib/service-clients/service-cognition/openapi.json@@ -1159,28 +1159,18 @@}}},- "/import/run": {- "post": {+ "/import/notion": {+ "get": {"tags": ["import"],- "summary": "Accept staged imports (starts import jobs) and/or discard staged rows.",- "operationId": "run_import_handler",- "requestBody": {- "content": {- "application/json": {- "schema": {- "$ref": "#/components/schemas/RunImportRequest"- }- }- },- "required": true- },+ "summary": "Where the user's Notion workspace import stands.",+ "operationId": "notion_report_handler","responses": {"200": {- "description": "Import run outcome",+ "description": "","content": {"application/json": {"schema": {- "$ref": "#/components/schemas/RunImportOutcome"+ "$ref": "#/components/schemas/NotionImportReport"}}}@@ -1191,28 +1181,21 @@}}},- "/import/runs/{source}/discover": {+ "/import/notion/cancel": {"post": {"tags": ["import"],- "summary": "Discover candidates for user selection without auto-importing them.",- "operationId": "discover_handler",- "parameters": [- {- "name": "source",- "in": "path",- "description": "Import source",- "required": true,- "schema": {- "type": "string"- }- }- ],+ "summary": "Stop planning or writing the user's Notion import. Objects already\nimported stay.",+ "operationId": "cancel_notion_handler","responses": {- "204": {- "description": "Discovery accepted (idempotent)"- },- "400": {- "description": "Unknown import source"+ "200": {+ "description": "Where the import stands after cancelling",+ "content": {+ "application/json": {+ "schema": {+ "$ref": "#/components/schemas/NotionImportReport"+ }+ }+ }},"500": {"description": "Internal server error"@@ -1220,28 +1203,31 @@}}},- "/import/runs/{source}/dismiss": {+ "/import/run": {"post": {"tags": ["import"],- "summary": "Dismiss one source's import section.",- "operationId": "dismiss_run_handler",- "parameters": [- {- "name": "source",- "in": "path",- "description": "Import source",- "required": true,- "schema": {- "type": "string"+ "summary": "Accept staged imports (starts import jobs) and/or discard staged rows.",+ "operationId": "run_import_handler",+ "requestBody": {+ "content": {+ "application/json": {+ "schema": {+ "$ref": "#/components/schemas/RunImportRequest"+ }}- }- ],- "responses": {- "204": {- "description": "Dismissed"},- "400": {- "description": "Unknown import source"+ "required": true+ },+ "responses": {+ "200": {+ "description": "Import run outcome",+ "content": {+ "application/json": {+ "schema": {+ "$ref": "#/components/schemas/RunImportOutcome"+ }+ }+ }},"500": {"description": "Internal server error"@@ -1249,11 +1235,11 @@}}},- "/import/runs/{source}/retry": {+ "/import/runs/{source}/discover": {"post": {"tags": ["import"],- "summary": "Restart a failed (or dismissed) gather run for one source.",- "operationId": "retry_gather_handler",+ "summary": "Discover candidates for user selection. Only Slack supports discovery.",+ "operationId": "discover_handler","parameters": [{"name": "source",@@ -1267,33 +1253,13 @@],"responses": {"204": {- "description": "Retry accepted (idempotent)"+ "description": "Discovery accepted (idempotent)"},"400": {- "description": "Unknown import source"- },- "402": {- "description": "AI allowance exhausted",- "content": {- "application/json": {- "schema": {- "$ref": "#/components/schemas/AiAdmissionErrorBody"- }- }- }+ "description": "Unknown import source or discovery not supported"},"500": {"description": "Internal server error"- },- "503": {- "description": "AI usage validation unavailable; retry later",- "content": {- "application/json": {- "schema": {- "$ref": "#/components/schemas/AiAdmissionErrorBody"- }- }- }}}}@@ -1301,7 +1267,7 @@"/import/state": {"get": {"tags": ["import"],- "summary": "Get the authenticated user's import state: gather runs plus visible\nledger rows.",+ "summary": "Get the authenticated user's import state: discovery runs plus visible\nledger rows.","operationId": "get_state_handler","responses": {"200": {@@ -1674,7 +1640,7 @@"/onboarding": {"get": {"tags": ["onboarding"],- "summary": "Get the authenticated user's onboarding state. While the flow is active\nthis also starts any gather runs that are due, so polling this endpoint\nis what keeps onboarding moving.",+ "summary": "Get the authenticated user's onboarding state.","operationId": "get_state_handler","responses": {"200": {@@ -2392,21 +2358,6 @@}}},- "AiAdmissionErrorBody": {- "type": "object",- "description": "Public admission error payload. Handlers with additional fields can reuse the\ndomain error's code and message and [`admission_status`].",- "required": ["error", "code"],- "properties": {- "code": {- "type": "string",- "description": "Stable denial or unavailability code."- },- "error": {- "type": "string",- "description": "Human-readable explanation, without internal billing diagnostics."- }- }- },"AiFeature": {"type": "string","description": "Everything we use AI for. The wire / DB form of each variant is its\n`snake_case` name.",@@ -4006,6 +3957,7 @@"id","user_id","source",+ "kind","foreign_id","status","initiator",@@ -4040,6 +3992,10 @@"$ref": "#/components/schemas/Initiator","description": "Where it was first staged from."},+ "kind": {+ "$ref": "#/components/schemas/ImportKind",+ "description": "What the item is in its source system."+ },"last_error": {"type": ["string", "null"],"description": "Failure detail from the last import attempt, when one failed."@@ -4047,6 +4003,16 @@"metadata": {"description": "Per-source metadata (shape depends on `source`; see\n[`LinearIssueMeta`], [`NotionDocMeta`], [`SlackChannelMeta`])."},+ "parent_id": {+ "type": ["string", "null"],+ "format": "uuid",+ "description": "The import row this item was imported under (a subpage's parent page,\na row's database), when it was imported as part of a tree."+ },+ "review_id": {+ "type": ["string", "null"],+ "format": "uuid",+ "description": "The agent session review that approved importing this item, when it\ncame from one."+ },"source": {"$ref": "#/components/schemas/ImportSource","description": "The external system the item comes from."@@ -4068,25 +4034,56 @@"user_id": {"type": "string","description": "The user who staged/imported it. Team-imported rows from teammates\nare visible with their owner's id, so clients can say \"created by a\nteammate\"."+ },+ "waiting_on": {+ "type": ["string", "null"],+ "format": "uuid",+ "description": "The teammate's row this one waits on: that teammate is writing the\nsame object for the team, and this row links to their copy once it\nis imported."}}},+ "ImportKind": {+ "type": "string",+ "description": "What an imported item is in its source system. Each source has its own\nkinds; a Notion database and a Notion page, or a Linear project and a\nLinear issue, are tracked and deduplicated separately.",+ "enum": [+ "issue",+ "project",+ "document",+ "page",+ "database",+ "data_source",+ "row",+ "channel"+ ]+ },"ImportRun": {"type": "object",- "description": "Gather-run state for one user × source.",- "required": ["source", "status", "auto_import", "updated_at"],+ "description": "Discovery-run state for one user × source.",+ "required": ["source", "status", "updated_at"],"properties": {- "auto_import": {- "type": "boolean",- "description": "Whether this run should import its onboarding-staged candidates as\nsoon as gathering finishes."- },"error": {"type": ["string", "null"],- "description": "Gather or automatic-import failure detail, when the run failed."+ "description": "Discovery failure detail, when the run failed."+ },+ "review_id": {+ "type": ["string", "null"],+ "format": "uuid",+ "description": "The review presenting the run's plan, once one does. A Notion plan is\npresented while it is still `running`; rows staged after that take it."+ },+ "scanned": {+ "oneOf": [+ {+ "type": "null"+ },+ {+ "$ref": "#/components/schemas/ScanCounts",+ "description": "What the run has found in the source so far, while it is `running`."+ }+ ]},"source": {"$ref": "#/components/schemas/ImportSource",- "description": "The source this run gathered from."+ "description": "The source this run discovered from."},"status": {"$ref": "#/components/schemas/RunStatus",@@ -4106,7 +4103,7 @@},"ImportState": {"type": "object",- "description": "The full import aggregate for a user: gather runs plus visible ledger\nrows (their own, and teammates' team-imported rows).",+ "description": "The full import aggregate for a user: discovery runs plus visible ledger\nrows (their own, and teammates' team-imported rows).","required": ["runs", "entities"],"properties": {"entities": {@@ -4121,7 +4118,7 @@"items": {"$ref": "#/components/schemas/ImportRun"},- "description": "Gather runs, one per source that ever gathered."+ "description": "Discovery runs, one per source that ever ran discovery."}}},@@ -4304,11 +4301,46 @@}}},+ "NotionAlreadyImported": {+ "type": "object",+ "description": "A teammate's copy of a Notion object, shared with the user's team.",+ "required": ["by", "entityId", "entityType"],+ "properties": {+ "by": {+ "type": "string",+ "description": "The teammate who imported it, by email."+ },+ "entityId": {+ "type": "string",+ "description": "The Macro entity it became."+ },+ "entityType": {+ "type": "string",+ "description": "That entity's type (`md`, `project`)."+ }+ }+ },+ "NotionDecision": {+ "type": "string",+ "description": "What a workspace import does with one Notion object.",+ "enum": ["copy", "tasks", "skip", "hand"]+ },"NotionDocMeta": {"type": "object","description": "Metadata for one staged Notion page. Deliberately has NO content field:\npage bodies are fetched at import time, for accepted pages only.","required": ["title"],"properties": {+ "plan": {+ "oneOf": [+ {+ "type": "null"+ },+ {+ "$ref": "#/components/schemas/NotionPlanItem",+ "description": "Where this object sits in a workspace import plan; absent for pages\nstaged one at a time. Written by the workspace import, never by a\nmodel, so it is left out of the tool schema."+ }+ ]+ },"summary": {"type": ["string", "null"],"description": "One-line summary of what the page contains."@@ -4323,6 +4355,327 @@}}},+ "NotionImportReport": {+ "type": "object",+ "description": "Where a Notion workspace import stands.",+ "required": [+ "pages",+ "dataSources",+ "copy",+ "tasks",+ "skip",+ "hand",+ "documents",+ "databases",+ "databaseRows",+ "taskRows",+ "imported",+ "waiting",+ "writing"+ ],+ "properties": {+ "alreadyImportedBy": {+ "type": "array",+ "items": {+ "$ref": "#/components/schemas/NotionImporter"+ },+ "description": "Who imported the objects already in Macro, most first."+ },+ "alreadyInMacro": {+ "type": "integer",+ "format": "int32",+ "description": "Plan objects a teammate already imported and shared with the team;\nthey link to that copy and are not counted as copies.",+ "minimum": 0+ },+ "copy": {+ "type": "integer",+ "format": "int32",+ "description": "Objects to copy as they are.",+ "minimum": 0+ },+ "dataSources": {+ "type": "integer",+ "format": "int32",+ "description": "Data sources (database tables) in the plan.",+ "minimum": 0+ },+ "databaseRows": {+ "type": "integer",+ "format": "int32",+ "description": "Rows those databases hold, every one counted while planning.",+ "minimum": 0+ },+ "databases": {+ "type": "integer",+ "format": "int32",+ "description": "Data sources written as databases.",+ "minimum": 0+ },+ "deciding": {+ "type": "integer",+ "format": "int32",+ "description": "Plan objects the decision model has not answered for yet, while\nplanning is still `running`.",+ "minimum": 0+ },+ "documents": {+ "type": "integer",+ "format": "int32",+ "description": "Pages written as documents.",+ "minimum": 0+ },+ "error": {+ "type": ["string", "null"],+ "description": "Why the run failed, when it did."+ },+ "failures": {+ "type": "array",+ "items": {+ "$ref": "#/components/schemas/NotionReportItem"+ },+ "description": "Objects that failed to import, with the failure."+ },+ "hand": {+ "type": "integer",+ "format": "int32",+ "description": "Objects that need handling by hand.",+ "minimum": 0+ },+ "handItems": {+ "type": "array",+ "items": {+ "$ref": "#/components/schemas/NotionReportItem"+ },+ "description": "Objects that need handling by hand, with reasons."+ },+ "imported": {+ "type": "integer",+ "format": "int32",+ "description": "Plan objects already imported.",+ "minimum": 0+ },+ "onTeam": {+ "type": "boolean",+ "description": "Whether the user is on a team. Without one, everything the import\nwrites stays visible only to them."+ },+ "pages": {+ "type": "integer",+ "format": "int32",+ "description": "Pages in the plan.",+ "minimum": 0+ },+ "reviewId": {+ "type": ["string", "null"],+ "format": "uuid",+ "description": "The review the plan is tied to, once presented."+ },+ "scanned": {+ "oneOf": [+ {+ "type": "null"+ },+ {+ "$ref": "#/components/schemas/ScanCounts",+ "description": "What planning has found in Notion so far, while it is `running`."+ }+ ]+ },+ "sharedWithTeam": {+ "type": "integer",+ "format": "int32",+ "description": "Written plan objects shared with the user's team.",+ "minimum": 0+ },+ "skip": {+ "type": "integer",+ "format": "int32",+ "description": "Objects left in Notion.",+ "minimum": 0+ },+ "status": {+ "oneOf": [+ {+ "type": "null"+ },+ {+ "$ref": "#/components/schemas/RunStatus",+ "description": "The run's state: `running` while planning, `ready` once a plan is\nstaged, `importing` while writing, `completed`, `failed`, or\n`dismissed` when cancelled. Absent before the first run."+ }+ ]+ },+ "taskRows": {+ "type": "integer",+ "format": "int32",+ "description": "Tasks the task lists become, one per row.",+ "minimum": 0+ },+ "tasks": {+ "type": "integer",+ "format": "int32",+ "description": "Data sources whose rows become tasks.",+ "minimum": 0+ },+ "waiting": {+ "type": "integer",+ "format": "int32",+ "description": "Plan objects still waiting to be written.",+ "minimum": 0+ },+ "waitingOnTeammates": {+ "type": "integer",+ "format": "int32",+ "description": "Plan objects waiting for a teammate who is writing the same object\nfor the team; they link to the teammate's copy once it is written.",+ "minimum": 0+ },+ "writing": {+ "type": "integer",+ "format": "int32",+ "description": "Plan objects being written now.",+ "minimum": 0+ }+ }+ },+ "NotionImporter": {+ "type": "object",+ "description": "A teammate who already imported some of a plan's objects.",+ "required": ["by", "count"],+ "properties": {+ "by": {+ "type": "string",+ "description": "The teammate, by email."+ },+ "count": {+ "type": "integer",+ "format": "int32",+ "description": "How many of the plan's objects they imported.",+ "minimum": 0+ }+ }+ },+ "NotionPlanItem": {+ "type": "object",+ "description": "One object's place in a workspace import plan.",+ "properties": {+ "alreadyImported": {+ "oneOf": [+ {+ "type": "null"+ },+ {+ "$ref": "#/components/schemas/NotionAlreadyImported",+ "description": "A teammate's copy shared with the team, which this object links to\ninstead of being imported again."+ }+ ]+ },+ "decision": {+ "oneOf": [+ {+ "type": "null"+ },+ {+ "$ref": "#/components/schemas/NotionDecision",+ "description": "What the import does with it; absent while planning has staged the\nobject but the decision model has not answered for it yet."+ }+ ]+ },+ "path": {+ "type": "array",+ "items": {+ "type": "string"+ },+ "description": "Titles of the pages above it, outermost first."+ },+ "reason": {+ "type": ["string", "null"],+ "description": "Why, in a short sentence, when the decision model gave one."+ },+ "rows": {+ "type": ["integer", "null"],+ "format": "int32",+ "description": "For a data source: rows counted while planning.",+ "minimum": 0+ },+ "sampleTitles": {+ "type": "array",+ "items": {+ "type": "string"+ },+ "description": "For a data source: the titles of a few of its rows."+ },+ "shareable": {+ "type": "boolean",+ "description": "Whether the user may change `shared`: false for users without a team."+ },+ "shared": {+ "type": "boolean",+ "description": "Whether what it becomes is shared with the user's team."+ },+ "sharedReason": {+ "type": ["string", "null"],+ "description": "Why it is (or is not) shared, when the decision model chose or the\nimport could not share it."+ },+ "taskFields": {+ "oneOf": [+ {+ "type": "null"+ },+ {+ "$ref": "#/components/schemas/NotionTaskFields",+ "description": "For a task list: which columns hold the task fields."+ }+ ]+ }+ }+ },+ "NotionReportItem": {+ "type": "object",+ "description": "One object a workspace import report calls out.",+ "required": ["importId", "title", "kind"],+ "properties": {+ "importId": {+ "type": "string",+ "format": "uuid",+ "description": "The ledger row."+ },+ "kind": {+ "$ref": "#/components/schemas/ImportKind",+ "description": "What the object is."+ },+ "reason": {+ "type": ["string", "null"],+ "description": "Why it is called out: the decision model's reason, or the failure."+ },+ "title": {+ "type": "string",+ "description": "The object's title."+ },+ "url": {+ "type": ["string", "null"],+ "description": "Its Notion URL."+ }+ }+ },+ "NotionTaskFields": {+ "type": "object",+ "description": "Which Notion columns hold a task list's task fields, by property name.",+ "properties": {+ "assignee": {+ "type": ["string", "null"],+ "description": "The assignee column."+ },+ "due": {+ "type": ["string", "null"],+ "description": "The due date column."+ },+ "priority": {+ "type": ["string", "null"],+ "description": "The priority column."+ },+ "status": {+ "type": ["string", "null"],+ "description": "The status column."+ }+ }+ },"OnboardingRow": {"type": "object","description": "One user's onboarding row.",@@ -4663,7 +5016,7 @@},"RunStatus": {"type": "string",- "description": "Lifecycle of a gather run (one per user × source).",+ "description": "Lifecycle of a discovery run (one per user × source).","enum": ["running","ready",@@ -4673,6 +5026,25 @@"dismissed"]},+ "ScanCounts": {+ "type": "object",+ "description": "What a planning run has found in the source so far.",+ "required": ["pages", "dataSources"],+ "properties": {+ "dataSources": {+ "type": "integer",+ "format": "int32",+ "description": "Data sources (database tables) found.",+ "minimum": 0+ },+ "pages": {+ "type": "integer",+ "format": "int32",+ "description": "Pages found.",+ "minimum": 0+ }+ }+ },"SendChatMessagePayload": {"allOf": [{diff --git a/crates/agent_code_mode/AGENTS.md b/crates/agent_code_mode/AGENTS.mdindex e6b93216e2..9e25b936f7 100644--- a/crates/agent_code_mode/AGENTS.md+++ b/crates/agent_code_mode/AGENTS.md@@ -23,6 +23,13 @@ stacks. Process isolation belongs to- `src/domain/user.rs`: `UserCodeMode`, runs as an authenticated user with nosession, journal or reviews; shares outcome validation with the session path.- `src/domain/reviews.rs`: review validation, revisions, claims and outcomes.+ `decide_for_session` (the `DecideReview` tool, `inbound/reviews.rs`) is the+ agent deciding on the account holder's word in chat: a turn they prompted,+ never a System turn (per `ReviewSession::prompter`), and a Submit only once+ the action is `approvable`.+ An import review only starts its write: its `review.decided` is quiet, and+ its `review.settled` comes from the import when the write ends; only an+ import that could not start settles here.- `src/domain/documentation.rs`: Jev selection of generated SDK docs, renderedinto the per-turn prompt (top four, 48 KiB) or returned for a`DescribeCodeTools` `task` (every relevant entity, 32 KiB).diff --git a/crates/agent_code_mode/SKILL.md b/crates/agent_code_mode/SKILL.mdindex 5e372ea939..220611d1b7 100644--- a/crates/agent_code_mode/SKILL.md+++ b/crates/agent_code_mode/SKILL.md@@ -29,7 +29,7 @@ macro, session and execution are real SDK handles. You can instead import {Macro- Collect a bounded recent-email batch, fetch relevant content, then map macro.ai.decide over it with bounded parallelism. Retain every error and distinguish probability estimates from facts. The ai guide has a complete example.- Generate structured extraction with macro.ai.structuredCompletion and toolset:{type:'none'}, using source text already fetched; check the returned outcome rather than assuming success.- Use Handlebars to personalize many drafts, persist a single email review stack, and return its reference. The templates guide shows the full module. Chat continues while the user edits and sends later.-- Persist calendar drafts with the same review entity, and let the user finish in the reused component. Reviews survive the Deno process and page reload. Creating one immediately presents a completed PresentReview tool call in the shared session stream; it does not wait for approval. The card can appear while main is still running. "Presented to the user" never means sent or approved. Submissions queue a separate outcome into the original session. The direct PresentReview tool offers the same UI outside code mode. After presenting, end by asking "Should I kick it off?"; when the person whose account the review uses says yes, submit it with the direct DecideReview tool, which approves it as the card would. It refuses in a turn someone else prompted or a system event started, and while the review is not ready.+- Persist calendar drafts with the same review entity, and let the user finish in the reused component. Reviews survive the Deno process and page reload. Creating one immediately presents a completed PresentReview tool call in the shared session stream; it does not wait for approval. The card can appear while main is still running. "Presented to the user" never means sent or approved. Submissions queue a separate outcome into the original session. The direct PresentReview tool offers the same UI outside code mode. After presenting, end by asking "Should I kick it off?"; when the person whose account the review uses says yes, submit it with the direct DecideReview tool, which approves it as the card would. It refuses in a turn someone else prompted or a system event started, and while the review is not ready. An approved import reports its end later as a review.settled system event with the final report.## Correctness and boundsdiff --git a/crates/agent_code_mode/src/domain/documentation.json b/crates/agent_code_mode/src/domain/documentation.jsonindex 88bc616442..efae42b428 100644--- a/crates/agent_code_mode/src/domain/documentation.json+++ b/crates/agent_code_mode/src/domain/documentation.json@@ -3,7 +3,7 @@"name": "agentSessions","description": "Create and inspect agent sessions, read any viewable session's history as folded messages (`session.foldedLog()`), saved code executions, and editable email or calendar review stacks.","overview": "<!-- src/entities/agent-sessions/agent-session/type-aliases/CreateManagedSessionOptions.md -->\n# Type Alias: CreateManagedSessionOptions\n\n> **CreateManagedSessionOptions** = \\{ `instructions?`: `string`; `model?`: `string`; `prompt?`: `string`; `repoBranch?`: `string`; `repoUrl?`: `string`; \\}\n\nWhat a managed session is created with.\n\n## Properties\n\n| Property | Type | Description |\n| ------ | ------ | ------ |\n| <a id=\"property-instructions\"></a> `instructions?` | `string` | Instructions the session's runtime works under, fixed for its life. |\n| <a id=\"property-model\"></a> `model?` | `string` | The model the session runs on, instead of its persona's. The session's model from creation, read back as [AgentSession.model](../classes/AgentSession.md#property-model). |\n| <a id=\"property-prompt\"></a> `prompt?` | `string` | First prompt to deliver once the session is running. |\n| <a id=\"property-repobranch\"></a> `repoBranch?` | `string` | The branch the session starts on; needs `repoUrl`. Omitted, the repository's default branch. |\n| <a id=\"property-repourl\"></a> `repoUrl?` | `string` | The repository the session works on, as one of the URLs [AgentSession.repositories](../classes/AgentSession.md#repositories) lists for the caller. Omitted, the runtime chooses from the prompt. Honored for Cursor sessions. |\n\n\n<!-- src/entities/agent-sessions/agent-session/type-aliases/SelectableRepository.md -->\n# Type Alias: SelectableRepository\n\n> **SelectableRepository** = \\{ `defaultBranch?`: `string`; `url`: `string`; \\}\n\nA GitHub repository the caller can point a managed session at.\n\n## Properties\n\n| Property | Type | Description |\n| ------ | ------ | ------ |\n| <a id=\"property-defaultbranch\"></a> `defaultBranch?` | `string` | The branch its clones check out, and where a session starts unless `createManaged` names another. Absent for a repository with no commits. |\n| <a id=\"property-url\"></a> `url` | `string` | The canonical `https://github.com/owner/name` URL, as `createManaged` takes it. |\n\n\n<!-- src/entities/agent-sessions/namespace/classes/AgentSessionNamespace.md -->\n# Class: AgentSessionNamespace\n\nEntry point for coding-agent sessions.\n\n## Constructors\n\n### Constructor\n\n> **new AgentSessionNamespace**(`client`): `AgentSessionNamespace`\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `client` | `MacroClient` |\n\n#### Returns\n\n`AgentSessionNamespace`\n\n## Methods\n\n### byId()\n\n> **byId**(`id`): [`AgentSession`](../../agent-session/classes/AgentSession.md)\n\nA handle to an agent session by id. Details load on first access.\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `id` | `string` |\n\n#### Returns\n\n[`AgentSession`](../../agent-session/classes/AgentSession.md)\n\n***\n\n### codingPreferences()\n\n> **codingPreferences**(): `Promise`\\<[`CodingPreferencesBody`](../../../../../generated/agent-harness/types.gen/type-aliases/CodingPreferencesBody.md)\\>\n\nWhat the caller's new coding sessions are told to do beyond their assignment.\n\n#### Returns\n\n`Promise`\\<[`CodingPreferencesBody`](../../../../../generated/agent-harness/types.gen/type-aliases/CodingPreferencesBody.md)\\>\n\n***\n\n### createManaged()\n\n> **createManaged**(`opts?`): `Promise`\\<[`AgentSession`](../../agent-session/classes/AgentSession.md)\\>\n\nCreate a managed agent session.\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `opts?` | [`CreateManagedSessionOptions`](../../agent-session/type-aliases/CreateManagedSessionOptions.md) |\n\n#### Returns\n\n`Promise`\\<[`AgentSession`](../../agent-session/classes/AgentSession.md)\\>\n\n***\n\n### defaultSandboxSize()\n\n> **defaultSandboxSize**(): `Promise`\\<[`SandboxSize`](../../../../../generated/agent-harness/types.gen/type-aliases/SandboxSize.md)\\>\n\nThe caller's default sandbox size for new `@coder` sessions.\n\n#### Returns\n\n`Promise`\\<[`SandboxSize`](../../../../../generated/agent-harness/types.gen/type-aliases/SandboxSize.md)\\>\n\n***\n\n### forPullRequest()\n\n> **forPullRequest**(`url`): `Promise`\\<[`AgentSession`](../../agent-session/classes/AgentSession.md)[]\\>\n\nThe sessions linked to the GitHub pull request at `url` that the caller\ncan view.\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `url` | `string` |\n\n#### Returns\n\n`Promise`\\<[`AgentSession`](../../agent-session/classes/AgentSession.md)[]\\>\n\n***\n\n### repositories()\n\n> **repositories**(): `Promise`\\<[`SelectableRepository`](../../agent-session/type-aliases/SelectableRepository.md)[]\\>\n\nThe GitHub repositories the caller can hand a managed session, with the\nbranch each one's sessions start on by default.\n\n#### Returns\n\n`Promise`\\<[`SelectableRepository`](../../agent-session/type-aliases/SelectableRepository.md)[]\\>\n\n***\n\n### repositoryBranches()\n\n> **repositoryBranches**(`repoUrl`): `Promise`\\<`string`[]\\>\n\nBranch names on one repository the caller can start a managed session\nfrom. `repoUrl` is one of the URLs [AgentSessionNamespace.repositories](#repositories)\nlists.\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `repoUrl` | `string` |\n\n#### Returns\n\n`Promise`\\<`string`[]\\>\n\n***\n\n### setCodingPreferences()\n\n> **setCodingPreferences**(`preferences`): `Promise`\\<[`CodingPreferencesBody`](../../../../../generated/agent-harness/types.gen/type-aliases/CodingPreferencesBody.md)\\>\n\nReplace the caller's coding preferences.\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `preferences` | [`CodingPreferencesBody`](../../../../../generated/agent-harness/types.gen/type-aliases/CodingPreferencesBody.md) |\n\n#### Returns\n\n`Promise`\\<[`CodingPreferencesBody`](../../../../../generated/agent-harness/types.gen/type-aliases/CodingPreferencesBody.md)\\>\n\n***\n\n### setDefaultSandboxSize()\n\n> **setDefaultSandboxSize**(`size`): `Promise`\\<[`SandboxSize`](../../../../../generated/agent-harness/types.gen/type-aliases/SandboxSize.md)\\>\n\nSet the caller's default sandbox size for the next `@coder` mention.\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `size` | [`SandboxSize`](../../../../../generated/agent-harness/types.gen/type-aliases/SandboxSize.md) |\n\n#### Returns\n\n`Promise`\\<[`SandboxSize`](../../../../../generated/agent-harness/types.gen/type-aliases/SandboxSize.md)\\>\n",- "documentation": "# Macro SDK: agentSessions\n\nGenerated by TypeDoc. Paths below refer to the canonical Markdown hierarchy. Other entity handles are documented in their named section; request core for shared SDK helpers.\n\n<!-- generated/agent-harness/types.gen/type-aliases/AccessLevel.md -->\n# Type Alias: AccessLevel\n\n> **AccessLevel** = `\"view\"` \\| `\"comment\"` \\| `\"edit\"` \\| `\"owner\"`\n\nOrdered from least to most access top -> bottom\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/AgentAction.md -->\n# Type Alias: AgentAction\n\n> **AgentAction** = [`AgentPromptAction`](AgentPromptAction.md) & \\{ `type`: `\"prompt\"`; \\} \\| [`AgentSetModelAction`](AgentSetModelAction.md) & \\{ `type`: `\"setModel\"`; \\} \\| [`AgentSetConfigOptionAction`](AgentSetConfigOptionAction.md) & \\{ `type`: `\"setConfigOption\"`; \\} \\| \\{ `type`: `\"compact\"`; \\} \\| \\{ `type`: `\"stop\"`; \\} \\| [`AgentRespondElicitationAction`](AgentRespondElicitationAction.md) & \\{ `type`: `\"respondElicitation\"`; \\} \\| [`AgentPermissionAction`](AgentPermissionAction.md) & \\{ `type`: `\"respondToPermission\"`; \\}\n\nOne thing a caller wants an agent to do.\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/AgentActionId.md -->\n# Type Alias: AgentActionId\n\n> **AgentActionId** = `string`\n\nIdentifies one accepted [`AgentAction`] end to end: returned by the\ncontrol endpoint, written as the JSON-RPC request id on the action's wire\nframe, and read back off that frame as `request_id` on the folded message\nit derives.\n\nA v7 uuid, so ids sort by mint time. Minted by the server at accept time,\nor by a client that speculated the action and named it in the control\nrequest - either way the server is the only writer of runtime-bound\nframes, so a uuid-shaped request id remains the whole ownership test. The\nmachine's own handshake request ids (`agent_session:{session}:{n}`) are\nnot uuids and stay `None`.\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/AgentConversationDto.md -->\n# Type Alias: AgentConversationDto\n\n> **AgentConversationDto** = \\{ `available`: `boolean`; `avatarUrl?`: `string` \\| `null`; `botId`: `string`; `channelId`: `string`; `name`: `string`; `sessions`: [`AgentConversationSessionDto`](AgentConversationSessionDto.md)[]; `settingsChanged`: `boolean`; `turns`: [`AgentConversationTurnDto`](AgentConversationTurnDto.md)[]; \\}\n\nAn agent's conversation in a channel, as the person it is with sees it.\n\n## Properties\n\n| Property | Type | Description |\n| ------ | ------ | ------ |\n| <a id=\"property-available\"></a> `available` | `boolean` | Whether the caller may currently send a new prompt. |\n| <a id=\"property-avatarurl\"></a> `avatarUrl?` | `string` \\| `null` | Optional persona avatar. |\n| <a id=\"property-botid\"></a> `botId` | `string` | Persona id (without the bot principal prefix). |\n| <a id=\"property-channelid\"></a> `channelId` | `string` | The channel containing the visible transcript. |\n| <a id=\"property-name\"></a> `name` | `string` | Persona's historical display name. |\n| <a id=\"property-sessions\"></a> `sessions` | [`AgentConversationSessionDto`](AgentConversationSessionDto.md)[] | The sessions the conversation has run on, oldest first. |\n| <a id=\"property-settingschanged\"></a> `settingsChanged` | `boolean` | Whether new persona settings are available to adopt explicitly. |\n| <a id=\"property-turns\"></a> `turns` | [`AgentConversationTurnDto`](AgentConversationTurnDto.md)[] | Persistent state of each accepted message and its current attempt. |\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/AgentConversationSessionDto.md -->\n# Type Alias: AgentConversationSessionDto\n\n> **AgentConversationSessionDto** = \\{ `createdAt`: `string`; `isCurrent`: `boolean`; `sessionId`: `string`; \\}\n\nOne session a conversation has run on; every channel message stays visible.\n\n## Properties\n\n| Property | Type | Description |\n| ------ | ------ | ------ |\n| <a id=\"property-createdat\"></a> `createdAt` | `string` | When the session began. |\n| <a id=\"property-iscurrent\"></a> `isCurrent` | `boolean` | Whether this is the current agent context. |\n| <a id=\"property-sessionid\"></a> `sessionId` | `string` | Session whose controls and live events belong to this context. |\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/AgentConversationTurnDto.md -->\n# Type Alias: AgentConversationTurnDto\n\n> **AgentConversationTurnDto** = \\{ `actionId`: `string`; `createdAt`: `string`; `replyMessageId?`: `string` \\| `null`; `retryable`: `boolean`; `sessionId`: `string`; `sourceMessageId`: `string`; `state`: [`ConversationTurnState`](ConversationTurnState.md); \\}\n\nExecution status without exposing the persona prompt or stored credentials.\n\n## Properties\n\n| Property | Type | Description |\n| ------ | ------ | ------ |\n| <a id=\"property-actionid\"></a> `actionId` | `string` | Current runtime attempt identity. |\n| <a id=\"property-createdat\"></a> `createdAt` | `string` | When the source message was first accepted. |\n| <a id=\"property-replymessageid\"></a> `replyMessageId?` | `string` \\| `null` | The associated agent reply, if it has been posted. |\n| <a id=\"property-retryable\"></a> `retryable` | `boolean` | Whether Retry would be accepted now. A failed attempt is not retryable until its reply says how it ended. |\n| <a id=\"property-sessionid\"></a> `sessionId` | `string` | Session carrying this turn. |\n| <a id=\"property-sourcemessageid\"></a> `sourceMessageId` | `string` | The user's message in the channel timeline. |\n| <a id=\"property-state\"></a> `state` | [`ConversationTurnState`](ConversationTurnState.md) | Durable execution state. |\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/AgentPermissionAction.md -->\n# Type Alias: AgentPermissionAction\n\n> **AgentPermissionAction** = \\{ `answer`: [`PermissionAnswer`](PermissionAnswer.md); `requestId`: `unknown`; \\}\n\nA user's answer to the agent's `session/request_permission`.\n\nUnlike every other action this is not a new request but the reply to one\nthe agent made, so it carries the agent's own request id rather than\nreceiving a server-minted one.\n\n## Properties\n\n| Property | Type | Description |\n| ------ | ------ | ------ |\n| <a id=\"property-answer\"></a> `answer` | [`PermissionAnswer`](PermissionAnswer.md) | What the user decided. |\n| <a id=\"property-requestid\"></a> `requestId` | `unknown` | The agent's JSON-RPC request id, echoed verbatim from the folded permission part. A string or a number on the wire; agents mint both, and `7` does not answer `\"7\"`. |\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/AgentPromptAction.md -->\n# Type Alias: AgentPromptAction\n\n> **AgentPromptAction** = \\{ `attachments?`: [`PromptAttachment`](PromptAttachment.md)[]; `prompt`: `string`; \\}\n\nAsk the agent to work on something.\n\n## Properties\n\n| Property | Type | Description |\n| ------ | ------ | ------ |\n| <a id=\"property-attachments\"></a> `attachments?` | [`PromptAttachment`](PromptAttachment.md)[] | Files the prompt refers to, in the order the user attached them. Delivered after the text as one `resource_link` block each. |\n| <a id=\"property-prompt\"></a> `prompt` | `string` | What to tell the agent. |\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/AgentRespondElicitationAction.md -->\n# Type Alias: AgentRespondElicitationAction\n\n> **AgentRespondElicitationAction** = [`ElicitationAnswer`](ElicitationAnswer.md) & \\{ `requestId`: [`ElicitationRequestId`](ElicitationRequestId.md); \\}\n\nAnswer an elicitation the agent is waiting on.\n\n## Type Declaration\n\n| Name | Type | Description |\n| ------ | ------ | ------ |\n| `requestId` | [`ElicitationRequestId`](ElicitationRequestId.md) | The agent's `elicitation/create` request id - not an [`AgentActionId`], because the agent minted it. |\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/AgentSessionChangesResponse.md -->\n# Type Alias: AgentSessionChangesResponse\n\n> **AgentSessionChangesResponse** = \\{ `attempt?`: `null` \\| [`CaptureAttemptDto`](CaptureAttemptDto.md); `capturing`: `boolean`; `changeset?`: `null` \\| [`ChangesetDto`](ChangesetDto.md); \\}\n\nResponse body for `GET /agent-sessions/{session_id}/changes`.\n\nClients deserialize this, so both derives are used.\n\n## Properties\n\n| Property | Type | Description |\n| ------ | ------ | ------ |\n| <a id=\"property-attempt\"></a> `attempt?` | `null` \\| [`CaptureAttemptDto`](CaptureAttemptDto.md) | - |\n| <a id=\"property-capturing\"></a> `capturing` | `boolean` | A capture is running right now. |\n| <a id=\"property-changeset\"></a> `changeset?` | `null` \\| [`ChangesetDto`](ChangesetDto.md) | - |\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/AgentSessionLogEntryDto.md -->\n# Type Alias: AgentSessionLogEntryDto\n\n> **AgentSessionLogEntryDto** = [`LogFrameDto`](LogFrameDto.md) & \\{ `createdAt`: `string`; `id`: `string`; `userId?`: `string` \\| `null`; \\}\n\nOne entry of a session's protocol log.\n\nSerializes as `{\"userId\": ..., \"direction\": ..., \"content\": ...}` - the\nframe's own two fields, flattened in beside the attribution, which is the\nsame shape a recorded session's JSONL carries. A reader can deserialize the\n`direction`/`content` pair straight back into the fold's own log type\nrather than through a transport vocabulary of its own.\n\n`agentSessionId` is not repeated per entry: every entry in a response\nbelongs to the session named once at the top.\n\n`Deserialize` is for the wire-contract tests only - nothing server-side\ndecodes its own response type.\n\n## Type Declaration\n\n| Name | Type | Description |\n| ------ | ------ | ------ |\n| `createdAt` | `string` | When the log recorded the frame. The frame itself carries no time, so this comes from the log row. It is what a reader has to order these against anything else it is showing beside them - the fold derives an order among the messages of one session and nothing more. |\n| `id` | `string` | Durable transport row identity; together with `createdAt`, its order cursor. |\n| `userId?` | `string` \\| `null` | The user whose action produced the frame, absent when no user did. Only prompts carry one, and only when the frame was attributed at the time - a replayed or recorded session's are anonymous. |\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/AgentSessionLogResponse.md -->\n# Type Alias: AgentSessionLogResponse\n\n> **AgentSessionLogResponse** = \\{ `bot`: [`SessionBot`](SessionBot.md); `entries`: [`AgentSessionLogEntryDto`](AgentSessionLogEntryDto.md)[]; \\}\n\nResponse body for one session's raw protocol log.\n\nA wrapper rather than a bare array so that anything which is about the\nresponse rather than about a frame has somewhere to go later without\nbreaking every client.\n\n## Properties\n\n| Property | Type | Description |\n| ------ | ------ | ------ |\n| <a id=\"property-bot\"></a> `bot` | [`SessionBot`](SessionBot.md) | The agent whose messages the log derives. Here because a client renders those messages and cannot otherwise work out who sent them: the sender of an agent message is this session's bot, and nothing else names it. |\n| <a id=\"property-entries\"></a> `entries` | [`AgentSessionLogEntryDto`](AgentSessionLogEntryDto.md)[] | Effective history in ascending `(createdAt, id)` order. The first row is the inclusive history boundary cursor: buffered rows before it are obsolete. Reconcile snapshot overlap by row ID, never content. An empty history has no boundary or overlapping rows. |\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/AgentSessionResponse.md -->\n# Type Alias: AgentSessionResponse\n\n> **AgentSessionResponse** = \\{ `acpSessionId?`: `string` \\| `null`; `botId`: `string`; `canEdit`: `boolean`; `createdAt`: `string`; `external?`: `null` \\| [`ExternalSessionResponse`](ExternalSessionResponse.md); `harness`: `string`; `id`: `string`; `instructions?`: `string` \\| `null`; `isArchived`: `boolean`; `model`: `string`; `modifiedAt`: `string`; `name`: `string`; `originatingMessageId?`: `string` \\| `null`; `ownerId`: `string`; `pullRequestUrl?`: `string` \\| `null`; `repoUrl?`: `string` \\| `null`; `sandboxSize`: [`SandboxSize`](SandboxSize.md); `status`: [`SessionStatusDto`](SessionStatusDto.md); `taskId?`: `string` \\| `null`; `threadChannelId?`: `string` \\| `null`; `threadId?`: `string` \\| `null`; `threadParent?`: `null` \\| [`MessageParent`](MessageParent.md); `workspace`: `string`; \\}\n\nResponse body describing an agent session.\n\nClients deserialize this, so both derives are used.\n\n## Properties\n\n| Property | Type | Description |\n| ------ | ------ | ------ |\n| <a id=\"property-acpsessionid\"></a> `acpSessionId?` | `string` \\| `null` | The ACP session id, if one exists. |\n| <a id=\"property-botid\"></a> `botId` | `string` | The bot running the agent. |\n| <a id=\"property-canedit\"></a> `canEdit` | `boolean` | Whether the caller may drive the session - prompt it, answer its questions, stop it - rather than only watch. Edit access; the creator owns the session, so a create response always says so. |\n| <a id=\"property-createdat\"></a> `createdAt` | `string` | When the session was created. |\n| <a id=\"property-external\"></a> `external?` | `null` \\| [`ExternalSessionResponse`](ExternalSessionResponse.md) | - |\n| <a id=\"property-harness\"></a> `harness` | `string` | Harness slug. |\n| <a id=\"property-id\"></a> `id` | `string` | The session id. |\n| <a id=\"property-instructions\"></a> `instructions?` | `string` \\| `null` | Instructions the session's runtime works under, when any were stated at creation. Absent otherwise, so existing payloads are unchanged. |\n| <a id=\"property-isarchived\"></a> `isArchived` | `boolean` | Whether the session is archived and read-only. |\n| <a id=\"property-model\"></a> `model` | `string` | Model slug. |\n| <a id=\"property-modifiedat\"></a> `modifiedAt` | `string` | When the session was last modified. |\n| <a id=\"property-name\"></a> `name` | `string` | User-facing session name. |\n| <a id=\"property-originatingmessageid\"></a> `originatingMessageId?` | `string` \\| `null` | The exact message that invoked the bot, if any. |\n| <a id=\"property-ownerid\"></a> `ownerId` | `string` | The user who created and owns the session. |\n| <a id=\"property-pullrequesturl\"></a> `pullRequestUrl?` | `string` \\| `null` | The session's linked pull request. |\n| <a id=\"property-repourl\"></a> `repoUrl?` | `string` \\| `null` | The repository the session works with, when one was stated. |\n| <a id=\"property-sandboxsize\"></a> `sandboxSize` | [`SandboxSize`](SandboxSize.md) | Compute tier of the managed sandbox. |\n| <a id=\"property-status\"></a> `status` | [`SessionStatusDto`](SessionStatusDto.md) | The session's status. |\n| <a id=\"property-taskid\"></a> `taskId?` | `string` \\| `null` | The Macro task the session was linked to. |\n| <a id=\"property-threadchannelid\"></a> `threadChannelId?` | `string` \\| `null` | The channel `thread_id` lives in, when the session was spawned from a channel thread. Derived from `thread_parent`. |\n| <a id=\"property-threadid\"></a> `threadId?` | `string` \\| `null` | The root message of the thread the session was created from, if any. |\n| <a id=\"property-threadparent\"></a> `threadParent?` | `null` \\| [`MessageParent`](MessageParent.md) | - |\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/AgentSetConfigOptionAction.md -->\n# Type Alias: AgentSetConfigOptionAction\n\n> **AgentSetConfigOptionAction** = \\{ `configId`: `string`; `value`: `string`; \\}\n\nAsk the agent to change one advertised select-style session setting.\n\n## Properties\n\n| Property | Type | Description |\n| ------ | ------ | ------ |\n| <a id=\"property-configid\"></a> `configId` | `string` | Opaque ACP config id advertised by the agent. |\n| <a id=\"property-value\"></a> `value` | `string` | Opaque select value advertised for that config option. |\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/AgentSetModelAction.md -->\n# Type Alias: AgentSetModelAction\n\n> **AgentSetModelAction** = \\{ `model`: `string`; \\}\n\nAsk the agent to run on a different model from here on.\n\n## Properties\n\n| Property | Type | Description |\n| ------ | ------ | ------ |\n| <a id=\"property-model\"></a> `model` | `string` | The model slug the agent should switch to. |\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/AnswerToolApprovalResponse.md -->\n# Type Alias: AnswerToolApprovalResponse\n\n> **AnswerToolApprovalResponse** = \\{ `status`: [`ToolApprovalStatusDto`](ToolApprovalStatusDto.md); \\}\n\nResponse body for answering a held tool call.\n\n## Properties\n\n| Property | Type | Description |\n| ------ | ------ | ------ |\n| <a id=\"property-status\"></a> `status` | [`ToolApprovalStatusDto`](ToolApprovalStatusDto.md) | Where the call stands now. |\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/BotId.md -->\n# Type Alias: BotId\n\n> **BotId** = `string`\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/CaptureAttemptDto.md -->\n# Type Alias: CaptureAttemptDto\n\n> **CaptureAttemptDto** = \\{ `error?`: `string` \\| `null`; `finishedAt?`: `string` \\| `null`; `outcome?`: `null` \\| [`CaptureOutcomeDto`](CaptureOutcomeDto.md); `startedAt`: `string`; \\}\n\nThe latest capture attempt.\n\nClients deserialize this, so both derives are used.\n\n## Properties\n\n| Property | Type | Description |\n| ------ | ------ | ------ |\n| <a id=\"property-error\"></a> `error?` | `string` \\| `null` | Why it did not capture, in a sentence the user can read. |\n| <a id=\"property-finishedat\"></a> `finishedAt?` | `string` \\| `null` | When it ended; absent while it runs. |\n| <a id=\"property-outcome\"></a> `outcome?` | `null` \\| [`CaptureOutcomeDto`](CaptureOutcomeDto.md) | - |\n| <a id=\"property-startedat\"></a> `startedAt` | `string` | When it started. |\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/CaptureOutcomeDto.md -->\n# Type Alias: CaptureOutcomeDto\n\n> **CaptureOutcomeDto** = `\"captured\"` \\| `\"not_ready\"` \\| `\"failed\"`\n\nHow the latest capture attempt ended, on the wire.\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/ChangedFileDto.md -->\n# Type Alias: ChangedFileDto\n\n> **ChangedFileDto** = \\{ `additions`: `number`; `binary`: `boolean`; `deletions`: `number`; `kind`: [`FileChangeKindDto`](FileChangeKindDto.md); `patchOmitted`: `boolean`; `path`: `string`; `previousPath?`: `string` \\| `null`; \\}\n\nOne changed file.\n\nClients deserialize this, so both derives are used.\n\n## Properties\n\n| Property | Type | Description |\n| ------ | ------ | ------ |\n| <a id=\"property-additions\"></a> `additions` | `number` | Lines added. |\n| <a id=\"property-binary\"></a> `binary` | `boolean` | The diff carries no text for this file. |\n| <a id=\"property-deletions\"></a> `deletions` | `number` | Lines removed. |\n| <a id=\"property-kind\"></a> `kind` | [`FileChangeKindDto`](FileChangeKindDto.md) | What happened to the file. |\n| <a id=\"property-patchomitted\"></a> `patchOmitted` | `boolean` | The file's hunks were left out of the patch to fit the size budget. |\n| <a id=\"property-path\"></a> `path` | `string` | The file's path after the change, or before it for a deletion. |\n| <a id=\"property-previouspath\"></a> `previousPath?` | `string` \\| `null` | Where a renamed file came from. |\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/ChangesetDto.md -->\n# Type Alias: ChangesetDto\n\n> **ChangesetDto** = \\{ `additions`: `number`; `base`: [`GitRefDto`](GitRefDto.md); `capturedAt`: `string`; `deletions`: `number`; `files`: [`ChangedFileDto`](ChangedFileDto.md)[]; `head`: [`GitRefDto`](GitRefDto.md); `id`: `string`; `patchBytes`: `number`; `repository?`: `string` \\| `null`; `source`: [`ChangesetSourceDto`](ChangesetSourceDto.md); `truncated`: `boolean`; \\}\n\nOne changeset: the files a patch touches and what happened to each.\n\nClients deserialize this, so both derives are used.\n\n## Properties\n\n| Property | Type | Description |\n| ------ | ------ | ------ |\n| <a id=\"property-additions\"></a> `additions` | `number` | Lines added across all files. |\n| <a id=\"property-base\"></a> `base` | [`GitRefDto`](GitRefDto.md) | The side the work started from. |\n| <a id=\"property-capturedat\"></a> `capturedAt` | `string` | When the diff was taken. |\n| <a id=\"property-deletions\"></a> `deletions` | `number` | Lines removed across all files. |\n| <a id=\"property-files\"></a> `files` | [`ChangedFileDto`](ChangedFileDto.md)[] | Every changed file, in patch order. |\n| <a id=\"property-head\"></a> `head` | [`GitRefDto`](GitRefDto.md) | The side carrying the work. |\n| <a id=\"property-id\"></a> `id` | `string` | The changeset's id; a different id means different changes. |\n| <a id=\"property-patchbytes\"></a> `patchBytes` | `number` | Size of the patch the matching patch route serves; zero when nothing changed. |\n| <a id=\"property-repository\"></a> `repository?` | `string` \\| `null` | `https://github.com/owner/name`, when known. |\n| <a id=\"property-source\"></a> `source` | [`ChangesetSourceDto`](ChangesetSourceDto.md) | Where the diff was read from. |\n| <a id=\"property-truncated\"></a> `truncated` | `boolean` | Some files' hunks were left out of the patch. |\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/ChangesetSourceDto.md -->\n# Type Alias: ChangesetSourceDto\n\n> **ChangesetSourceDto** = `\"github_pull_request\"`\n\nThe source of a changeset's diff, on the wire.\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/ChannelSharePermission.md -->\n# Type Alias: ChannelSharePermission\n\n> **ChannelSharePermission** = \\{ `access_level`: [`AccessLevel`](AccessLevel.md); `channel_id`: `string`; \\}\n\nThe channel share permission\n\n## Properties\n\n| Property | Type | Description |\n| ------ | ------ | ------ |\n| <a id=\"property-access_level\"></a> `access_level` | [`AccessLevel`](AccessLevel.md) | The access level for the channel |\n| <a id=\"property-channel_id\"></a> `channel_id` | `string` | The channel id |\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/CodingPreferencesBody.md -->\n# Type Alias: CodingPreferencesBody\n\n> **CodingPreferencesBody** = \\{ `createTasks`: `boolean`; `openPullRequests`: `boolean`; \\}\n\nRequest or response body for the caller's coding preferences.\n\n## Properties\n\n| Property | Type | Description |\n| ------ | ------ | ------ |\n| <a id=\"property-createtasks\"></a> `createTasks` | `boolean` | Whether new coding sessions research prior work and link a Macro task. |\n| <a id=\"property-openpullrequests\"></a> `openPullRequests` | `boolean` | Whether new coding sessions deliver their work as a pull request. |\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/ControlResponse.md -->\n# Type Alias: ControlResponse\n\n> **ControlResponse** = \\{ `actionId`: [`AgentActionId`](AgentActionId.md); `status`: [`ControlStatusDto`](ControlStatusDto.md); \\}\n\nResponse body for a control operation.\n\nClients deserialize this, so both derives are used.\n\n## Properties\n\n| Property | Type | Description |\n| ------ | ------ | ------ |\n| <a id=\"property-actionid\"></a> `actionId` | [`AgentActionId`](AgentActionId.md) | Matches `requestId` on the folded message this action derives once it dispatches, and names the queue entry until then. The caller's own `actionId` when it supplied one; a freshly minted id otherwise. |\n| <a id=\"property-status\"></a> `status` | [`ControlStatusDto`](ControlStatusDto.md) | Whether the action went out or waits in the queue. |\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/ControlStatusDto.md -->\n# Type Alias: ControlStatusDto\n\n> **ControlStatusDto** = `\"sent\"` \\| `\"queued\"`\n\nWhat accepting a control operation did with it, on the wire.\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/ConversationTurnState.md -->\n# Type Alias: ConversationTurnState\n\n> **ConversationTurnState** = `\"queued\"` \\| `\"running\"` \\| `\"succeeded\"` \\| `\"failed\"` \\| `\"stopped\"` \\| `\"interrupted\"`\n\nThe state of one source message, retained after completion for deduplication.\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/DecisionVia.md -->\n# Type Alias: DecisionVia\n\n> **DecisionVia** = `\"card\"` \\| `\"chat\"` \\| `\"inbox\"`\n\nSurface of a review decision.\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/DocumentId.md -->\n# Type Alias: DocumentId\n\n> **DocumentId** = `string`\n\nA validated document identifier. Historical document ids need not be UUIDs.\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/ElicitationAnswer.md -->\n# Type Alias: ElicitationAnswer\n\n> **ElicitationAnswer** = \\{ `action`: `\"accept\"`; `content?`: \\{\\[`key`: `string`\\]: [`ElicitationContentValue`](ElicitationContentValue.md); \\} \\| `null`; \\} \\| \\{ `action`: `\"decline\"`; \\} \\| \\{ `action`: `\"cancel\"`; \\}\n\nWhat the user decided about an elicitation. Mirrors ACP's three actions;\nthere is no `Other` because we never originate an action we do not know.\n\n## Union Members\n\n### Type Literal\n\n\\{ `action`: `\"accept\"`; `content?`: \\{\\[`key`: `string`\\]: [`ElicitationContentValue`](ElicitationContentValue.md); \\} \\| `null`; \\}\n\n| Name | Type | Description |\n| ------ | ------ | ------ |\n| `action` | `\"accept\"` | - |\n| `content?` | \\{\\[`key`: `string`\\]: [`ElicitationContentValue`](ElicitationContentValue.md); \\} \\| `null` | Form: the submitted values keyed by property. URL: omitted. |\n\n***\n\n### Type Literal\n\n\\{ `action`: `\"decline\"`; \\}\n\n***\n\n### Type Literal\n\n\\{ `action`: `\"cancel\"`; \\}\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/ElicitationContentValue.md -->\n# Type Alias: ElicitationContentValue\n\n> **ElicitationContentValue** = `string` \\| `boolean` \\| `number` \\| `number` \\| `string`[]\n\nA value ACP accepts in an elicitation answer.\n\nMirrors ACP's `ElicitationContentValue` so that the contract a caller\nanswers against is the closed union ACP will accept, rather than arbitrary\nJSON narrowed on the way out. An object, a null, or a mixed array is\nrefused when the request is deserialized - where the caller learns of it -\ninstead of at send time, when the elicitation slot has already been\nreleased.\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/ElicitationRequestId.md -->\n# Type Alias: ElicitationRequestId\n\n> **ElicitationRequestId** = `number` \\| `string`\n\nThe JSON-RPC id of an agent's `elicitation/create` request, carried whole\nso the answer echoes exactly what the agent sent.\n\nAgents pick these, not us: Claude Code counts from `0`, others use\nstrings. `null` is not a legal id for a request that expects a response,\nso it is not representable here.\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/ExecutionActivity.md -->\n# Type Alias: ExecutionActivity\n\n> **ExecutionActivity** = \\{ `id`: `number`; `label`: `string`; `status`: [`RecordedCallStatus`](RecordedCallStatus.md); \\}\n\nA bounded SDK operation observed during execution; not an authorization or write receipt.\n\n## Properties\n\n| Property | Type | Description |\n| ------ | ------ | ------ |\n| <a id=\"property-id\"></a> `id` | `number` | Request identity within the execution. |\n| <a id=\"property-label\"></a> `label` | `string` | Server-selected label from the generated operation catalog. |\n| <a id=\"property-status\"></a> `status` | [`RecordedCallStatus`](RecordedCallStatus.md) | Observed request state; unfinished operations have unknown outcomes. |\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/ExecutionId.md -->\n# Type Alias: ExecutionId\n\n> **ExecutionId** = `string`\n\nCaller-selected UUID persisted in tool input before execution begins.\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/ExecutionRecord.md -->\n# Type Alias: ExecutionRecord\n\n> **ExecutionRecord** = \\{ `actingAs?`: `null` \\| [`ToolActor`](ToolActor.md); `activity?`: [`ExecutionActivity`](ExecutionActivity.md)[]; `activityTruncated?`: `boolean`; `calls`: [`RecordedToolCall`](RecordedToolCall.md)[]; `error?`: `string` \\| `null`; `executionId`: [`ExecutionId`](ExecutionId.md); `result?`: `unknown`; `source`: `string`; `status`: [`ExecutionStatus`](ExecutionStatus.md); `turnId?`: `string` \\| `null`; \\}\n\nDurable source and final outcome, with historical bridge calls when present.\nThe full journal is not included in the model's tool result.\n\n## Properties\n\n| Property | Type | Description |\n| ------ | ------ | ------ |\n| <a id=\"property-actingas\"></a> `actingAs?` | `null` \\| [`ToolActor`](ToolActor.md) | - |\n| <a id=\"property-activity\"></a> `activity?` | [`ExecutionActivity`](ExecutionActivity.md)[] | Advisory SDK operation activity, without arguments or response contents. |\n| <a id=\"property-activitytruncated\"></a> `activityTruncated?` | `boolean` | Additional request activity was omitted at the bounded observation limit. |\n| <a id=\"property-calls\"></a> `calls` | [`RecordedToolCall`](RecordedToolCall.md)[] | Historical bridge calls. Direct HTTP executions leave this empty. |\n| <a id=\"property-error\"></a> `error?` | `string` \\| `null` | Safe execution failure text. |\n| <a id=\"property-executionid\"></a> `executionId` | [`ExecutionId`](ExecutionId.md) | Execution identity. |\n| <a id=\"property-result\"></a> `result?` | `unknown` | JSON returned by the program, if successful. |\n| <a id=\"property-source\"></a> `source` | `string` | The submitted JavaScript module. |\n| <a id=\"property-status\"></a> `status` | [`ExecutionStatus`](ExecutionStatus.md) | Overall execution state. |\n| <a id=\"property-turnid\"></a> `turnId?` | `string` \\| `null` | Original turn, preventing another requester from replaying a saved result. |\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/ExecutionStatus.md -->\n# Type Alias: ExecutionStatus\n\n> **ExecutionStatus** = `\"running\"` \\| `\"succeeded\"` \\| `\"failed\"` \\| `\"cancelled\"` \\| `\"timed_out\"`\n\nThe recorded outcome of a program.\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/ExternalSessionResponse.md -->\n# Type Alias: ExternalSessionResponse\n\n> **ExternalSessionResponse** = \\{ `name?`: `string` \\| `null`; `provider`: `string`; `url?`: `string` \\| `null`; \\}\n\nThe provider-side identity of an externally-served session.\n\n## Properties\n\n| Property | Type | Description |\n| ------ | ------ | ------ |\n| <a id=\"property-name\"></a> `name?` | `string` \\| `null` | The provider's display name for the agent, when it reported one. |\n| <a id=\"property-provider\"></a> `provider` | `string` | Which provider serves the session, e.g. `cursor`. |\n| <a id=\"property-url\"></a> `url?` | `string` \\| `null` | The agent's page on the provider's site, for a client to link out to. |\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/FileChangeKindDto.md -->\n# Type Alias: FileChangeKindDto\n\n> **FileChangeKindDto** = `\"added\"` \\| `\"modified\"` \\| `\"deleted\"` \\| `\"renamed\"`\n\nWhat happened to a file, on the wire.\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/GitRefDto.md -->\n# Type Alias: GitRefDto\n\n> **GitRefDto** = \\{ `name?`: `string` \\| `null`; `sha?`: `string` \\| `null`; \\}\n\nOne end of the compared range.\n\n## Properties\n\n| Property | Type | Description |\n| ------ | ------ | ------ |\n| <a id=\"property-name\"></a> `name?` | `string` \\| `null` | The branch name, when known. |\n| <a id=\"property-sha\"></a> `sha?` | `string` \\| `null` | The commit, when known. |\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/LinkShare.md -->\n# Type Alias: LinkShare\n\n> **LinkShare** = `\"PUBLIC\"` \\| `\"TEAM\"`\n\nDefines who can access an item through its share link.\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/LogDirectionDto.md -->\n# Type Alias: LogDirectionDto\n\n> **LogDirectionDto** = `\"notification\"` \\| `\"to_server\"` \\| `\"to_runtime\"`\n\nWhich way a logged frame travelled, mirroring [`Message`]'s discriminant.\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/LogFrameDto.md -->\n# Type Alias: LogFrameDto\n\n> **LogFrameDto** = \\{ `content`: \\{\\[`key`: `string`\\]: `unknown`; \\}; `direction`: [`LogDirectionDto`](LogDirectionDto.md); \\}\n\nThe two fields [`AgentSessionLogEntryDto`] flattens in.\n\nSchema only. Nothing constructs one: the entry serializes through\n[`Message`], and this exists so the generated clients see `direction` and\n`content` as named fields instead of an open map. A hand-built copy could\ndrift from the fold's wire format, and the point of the endpoint is that it\ncannot - so this describes that format without being able to produce it.\n\n## Properties\n\n| Property | Type | Description |\n| ------ | ------ | ------ |\n| <a id=\"property-content\"></a> `content` | \\{\\[`key`: `string`\\]: `unknown`; \\} | The protocol envelope, verbatim. Opaque here: it is Agent Runtime Protocol, whose shape belongs to the fold rather than this endpoint. |\n| <a id=\"property-direction\"></a> `direction` | [`LogDirectionDto`](LogDirectionDto.md) | Which way the frame travelled. |\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/MessageParent.md -->\n# Type Alias: MessageParent\n\n> **MessageParent** = \\{ `id`: `string`; `type`: `\"channel\"`; \\} \\| \\{ `id`: [`DocumentId`](DocumentId.md); `type`: `\"document\"`; \\} \\| \\{ `id`: `string`; `type`: `\"initiative\"`; \\} \\| \\{ `id`: `string`; `type`: `\"crm_company\"`; \\} \\| \\{ `id`: `string`; `type`: `\"crm_contact\"`; \\} \\| \\{ `id`: `string`; `type`: `\"call\"`; \\}\n\nThe entity whose permissions and lifecycle govern a message.\n\n## Union Members\n\n### Type Literal\n\n\\{ `id`: `string`; `type`: `\"channel\"`; \\}\n\n| Name | Type | Description |\n| ------ | ------ | ------ |\n| `id` | `string` | A channel, including direct messages. |\n| `type` | `\"channel\"` | - |\n\n***\n\n### Type Literal\n\n\\{ `id`: [`DocumentId`](DocumentId.md); `type`: `\"document\"`; \\}\n\n| Name | Type | Description |\n| ------ | ------ | ------ |\n| `id` | [`DocumentId`](DocumentId.md) | A document, including tasks and PDFs. |\n| `type` | `\"document\"` | - |\n\n***\n\n### Type Literal\n\n\\{ `id`: `string`; `type`: `\"initiative\"`; \\}\n\n| Name | Type | Description |\n| ------ | ------ | ------ |\n| `id` | `string` | An initiative, presented as a project in the application. |\n| `type` | `\"initiative\"` | - |\n\n***\n\n### Type Literal\n\n\\{ `id`: `string`; `type`: `\"crm_company\"`; \\}\n\n| Name | Type | Description |\n| ------ | ------ | ------ |\n| `id` | `string` | A CRM company. |\n| `type` | `\"crm_company\"` | - |\n\n***\n\n### Type Literal\n\n\\{ `id`: `string`; `type`: `\"crm_contact\"`; \\}\n\n| Name | Type | Description |\n| ------ | ------ | ------ |\n| `id` | `string` | A CRM contact. |\n| `type` | `\"crm_contact\"` | - |\n\n***\n\n### Type Literal\n\n\\{ `id`: `string`; `type`: `\"call\"`; \\}\n\n| Name | Type | Description |\n| ------ | ------ | ------ |\n| `id` | `string` | A video call and its persistent chat thread. |\n| `type` | `\"call\"` | - |\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/PermissionAnswer.md -->\n# Type Alias: PermissionAnswer\n\n> **PermissionAnswer** = \\{ `kind`: `\"selected\"`; `optionId`: `string`; \\} \\| \\{ `kind`: `\"cancelled\"`; \\}\n\nThe decision carried by an [`AgentPermissionAction`].\n\n## Union Members\n\n### Type Literal\n\n\\{ `kind`: `\"selected\"`; `optionId`: `string`; \\}\n\n| Name | Type | Description |\n| ------ | ------ | ------ |\n| `kind` | `\"selected\"` | - |\n| `optionId` | `string` | The chosen option's id, as the agent listed it. |\n\n***\n\n### Type Literal\n\n\\{ `kind`: `\"cancelled\"`; \\}\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/PromptAttachment.md -->\n# Type Alias: PromptAttachment\n\n> **PromptAttachment** = \\{ `mimeType?`: `string` \\| `null`; `name`: `string`; `size?`: `number` \\| `null`; `uri`: `string`; \\}\n\nA file the prompt refers to, by where the agent can fetch it.\n\nMirrors ACP's `resource_link` content block, which every agent must\naccept: bytes never ride the prompt, only a URI (a static file service URL\nin practice) with enough metadata for a client to render a chip and for\nthe agent to decide whether to fetch it. Keeping the wire shape ACP-native\nmeans the harness translates without resolving anything, and the fold\nreads the same fields back off the logged frame.\n\n## Properties\n\n| Property | Type | Description |\n| ------ | ------ | ------ |\n| <a id=\"property-mimetype\"></a> `mimeType?` | `string` \\| `null` | The file's media type, when known. |\n| <a id=\"property-name\"></a> `name` | `string` | Display name, typically the original file name. |\n| <a id=\"property-size\"></a> `size?` | `number` \\| `null` | Size in bytes, when known. |\n| <a id=\"property-uri\"></a> `uri` | `string` | Where the agent can fetch the file. |\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/PullRequestLinkSource.md -->\n# Type Alias: PullRequestLinkSource\n\n> **PullRequestLinkSource** = `\"agent\"` \\| `\"user\"`\n\nWho associated a pull request with a session.\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/QueuedActionDto.md -->\n# Type Alias: QueuedActionDto\n\n> **QueuedActionDto** = \\{ `actionId`: [`AgentActionId`](AgentActionId.md); `actorUserId?`: `string` \\| `null`; `attachments?`: [`PromptAttachment`](PromptAttachment.md)[]; `createdAt`: `string`; `kind`: `string`; `prompt?`: `string` \\| `null`; \\}\n\nOne action waiting in a session's queue.\n\nClients deserialize this, so both derives are used.\n\n## Properties\n\n| Property | Type | Description |\n| ------ | ------ | ------ |\n| <a id=\"property-actionid\"></a> `actionId` | [`AgentActionId`](AgentActionId.md) | The id the action was accepted under. |\n| <a id=\"property-actoruserid\"></a> `actorUserId?` | `string` \\| `null` | The user who queued it, absent when a bot acted on nobody's behalf. |\n| <a id=\"property-attachments\"></a> `attachments?` | [`PromptAttachment`](PromptAttachment.md)[] | Files the prompt refers to, for prompts only. Kept through an edit, which replaces the text alone. |\n| <a id=\"property-createdat\"></a> `createdAt` | `string` | When it was accepted. |\n| <a id=\"property-kind\"></a> `kind` | `string` | What kind of action waits - `prompt` or `compact`; only turn-occupying actions are ever queued. |\n| <a id=\"property-prompt\"></a> `prompt?` | `string` \\| `null` | The prompt's raw text, present for prompts only. What an edit replaces. |\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/RecordedCallStatus.md -->\n# Type Alias: RecordedCallStatus\n\n> **RecordedCallStatus** = `\"running\"` \\| `\"completed\"` \\| `\"failed\"` \\| `\"unknown\"`\n\nA dispatch result; unfinished work is never reported as success.\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/RecordedToolCall.md -->\n# Type Alias: RecordedToolCall\n\n> **RecordedToolCall** = \\{ `error?`: `string` \\| `null`; `id`: `string`; `input`: `unknown`; `name`: `string`; `output?`: `unknown`; `outputOmitted`: `boolean`; `status`: [`RecordedCallStatus`](RecordedCallStatus.md); \\}\n\nA real inner call, recorded by trusted dispatch for the existing renderers.\n\n## Properties\n\n| Property | Type | Description |\n| ------ | ------ | ------ |\n| <a id=\"property-error\"></a> `error?` | `string` \\| `null` | Safe tool failure, if one was returned. |\n| <a id=\"property-id\"></a> `id` | `string` | Stable, execution-scoped component identity. |\n| <a id=\"property-input\"></a> `input` | `unknown` | Arguments passed to the actual tool. |\n| <a id=\"property-name\"></a> `name` | `string` | Canonical Macro tool name from the allowlisted registry. |\n| <a id=\"property-output\"></a> `output?` | `unknown` | Original tool output, retained independently of the program's return value. |\n| <a id=\"property-outputomitted\"></a> `outputOmitted` | `boolean` | Whether the response was omitted to keep the durable journal bounded. |\n| <a id=\"property-status\"></a> `status` | [`RecordedCallStatus`](RecordedCallStatus.md) | A started call that never replied has an unknown side-effect outcome. |\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/ReviewDecisionContext.md -->\n# Type Alias: ReviewDecisionContext\n\n> **ReviewDecisionContext** = \\{ `by`: `string`; `items`: `string`[]; `revision`: `number`; `via`: [`DecisionVia`](DecisionVia.md); \\}\n\nFacts about the most recent account-holder decision.\n\n## Properties\n\n| Property | Type | Description |\n| ------ | ------ | ------ |\n| <a id=\"property-by\"></a> `by` | `string` | Authenticated account holder. |\n| <a id=\"property-items\"></a> `items` | `string`[] | Items selected by this decision. |\n| <a id=\"property-revision\"></a> `revision` | `number` | Revision claimed by this batch, used in notification deduplication. |\n| <a id=\"property-via\"></a> `via` | [`DecisionVia`](DecisionVia.md) | Entry surface. |\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/ReviewId.md -->\n# Type Alias: ReviewId\n\n> **ReviewId** = `string`\n\nPersisted review identity.\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/ReviewItem.md -->\n# Type Alias: ReviewItem\n\n> **ReviewItem** = \\{ `draft`: `unknown`; `edited?`: `boolean`; `error?`: `string` \\| `null`; `id`: `string`; `result?`: `unknown`; `status`: [`ReviewItemStatus`](ReviewItemStatus.md); \\}\n\nOne editable item and its durable outcome.\n\n## Properties\n\n| Property | Type | Description |\n| ------ | ------ | ------ |\n| <a id=\"property-draft\"></a> `draft` | `unknown` | Validated arguments for the existing composer/action. |\n| <a id=\"property-edited\"></a> `edited?` | `boolean` | Whether the account holder edited this draft before deciding. |\n| <a id=\"property-error\"></a> `error?` | `string` \\| `null` | Failure or uncertainty description. |\n| <a id=\"property-id\"></a> `id` | `string` | Stable item identity across stack navigation and edits. |\n| <a id=\"property-result\"></a> `result?` | `unknown` | Actual backend response, never a model assertion. |\n| <a id=\"property-status\"></a> `status` | [`ReviewItemStatus`](ReviewItemStatus.md) | Current outcome. |\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/ReviewItemStatus.md -->\n# Type Alias: ReviewItemStatus\n\n> **ReviewItemStatus** = `\"pending\"` \\| `\"submitting\"` \\| `\"completed\"` \\| `\"dismissed\"` \\| `\"unknown\"`\n\nReview item state, shared with notification consumers.\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/ReviewKind.md -->\n# Type Alias: ReviewKind\n\n> **ReviewKind** = `\"email\"` \\| `\"calendar\"`\n\nThe component and action used to finish a review.\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/ReviewRecord.md -->\n# Type Alias: ReviewRecord\n\n> **ReviewRecord** = \\{ `actingUser?`: `string` \\| `null`; `decision?`: `null` \\| [`ReviewDecisionContext`](ReviewDecisionContext.md); `executionId?`: `null` \\| [`ExecutionId`](ExecutionId.md); `id`: [`ReviewId`](ReviewId.md); `items`: [`ReviewItem`](ReviewItem.md)[]; `kind`: [`ReviewKind`](ReviewKind.md); `presentedAt?`: `number` \\| `null`; `revision`: `number`; `sessionId`: `string`; `title`: `string`; `updatedAt`: `number`; \\}\n\nFull UI record, fetched outside the model context.\n\n## Properties\n\n| Property | Type | Description |\n| ------ | ------ | ------ |\n| <a id=\"property-actinguser\"></a> `actingUser?` | `string` \\| `null` | Account whose email/calendar the drafts use; historical records use the session owner. |\n| <a id=\"property-decision\"></a> `decision?` | `null` \\| [`ReviewDecisionContext`](ReviewDecisionContext.md) | - |\n| <a id=\"property-executionid\"></a> `executionId?` | `null` \\| [`ExecutionId`](ExecutionId.md) | - |\n| <a id=\"property-id\"></a> `id` | [`ReviewId`](ReviewId.md) | Review identity. |\n| <a id=\"property-items\"></a> `items` | [`ReviewItem`](ReviewItem.md)[] | Drafts and per-item outcomes. |\n| <a id=\"property-kind\"></a> `kind` | [`ReviewKind`](ReviewKind.md) | Component kind. |\n| <a id=\"property-presentedat\"></a> `presentedAt?` | `number` \\| `null` | Durable presentation acknowledgment, separate from user decisions. |\n| <a id=\"property-revision\"></a> `revision` | `number` | Compare-and-swap revision for edits and submissions. |\n| <a id=\"property-sessionid\"></a> `sessionId` | `string` | Owning session. |\n| <a id=\"property-title\"></a> `title` | `string` | Stack label. |\n| <a id=\"property-updatedat\"></a> `updatedAt` | `number` | Milliseconds since epoch of the last state change. |\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/SandboxSize.md -->\n# Type Alias: SandboxSize\n\n> **SandboxSize** = `\"small\"` \\| `\"default\"` \\| `\"large\"`\n\nNamed compute tier for a managed sandbox.\n\nThe API and database store the name. CPU, RAM, and disk live in\n`crates/agent_harness/sandbox_sizes.json`.\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/SessionBot.md -->\n# Type Alias: SessionBot\n\n> **SessionBot** = \\{ `avatarUrl?`: `string` \\| `null`; `handle`: `string`; `id`: [`BotId`](BotId.md); `name`: `string`; \\}\n\nThe agent behind a session, as much of it as rendering a message needs.\n\n## Properties\n\n| Property | Type | Description |\n| ------ | ------ | ------ |\n| <a id=\"property-avatarurl\"></a> `avatarUrl?` | `string` \\| `null` | Avatar, when it has one. |\n| <a id=\"property-handle\"></a> `handle` | `string` | Stable `@` handle, without a leading `@`. |\n| <a id=\"property-id\"></a> `id` | [`BotId`](BotId.md) | The bot's id. A message it sent has `\"bot\\|{id}\"` as its sender. |\n| <a id=\"property-name\"></a> `name` | `string` | Display name. |\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/SessionStatusDto.md -->\n# Type Alias: SessionStatusDto\n\n> **SessionStatusDto** = \\{ `kind`: `\"no_messages\"`; \\} \\| \\{ `event`: `string`; `kind`: `\"event\"`; \\} \\| \\{ `kind`: `\"disconnected\"`; \\}\n\nTransport representation of a session's status, mirroring\n[`SessionStatus`].\n\n## Union Members\n\n### Type Literal\n\n\\{ `kind`: `\"no_messages\"`; \\}\n\n***\n\n### Type Literal\n\n\\{ `event`: `string`; `kind`: `\"event\"`; \\}\n\n| Name | Type | Description |\n| ------ | ------ | ------ |\n| `event` | `string` | The wire name of the system event, e.g. `acp_ready`. |\n| `kind` | `\"event\"` | - |\n\n***\n\n### Type Literal\n\n\\{ `kind`: `\"disconnected\"`; \\}\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/SharePermissionV2.md -->\n# Type Alias: SharePermissionV2\n\n> **SharePermissionV2** = \\{ `channelSharePermissions?`: [`ChannelSharePermission`](ChannelSharePermission.md)[] \\| `null`; `id`: `string`; `linkShare?`: `null` \\| [`LinkShare`](LinkShare.md); `linkShareAccessLevel?`: `null` \\| [`AccessLevel`](AccessLevel.md); `owner`: `string`; `teamShareAccessLevel?`: `null` \\| [`AccessLevel`](AccessLevel.md); \\}\n\n## Properties\n\n| Property | Type | Description |\n| ------ | ------ | ------ |\n| <a id=\"property-channelsharepermissions\"></a> `channelSharePermissions?` | [`ChannelSharePermission`](ChannelSharePermission.md)[] \\| `null` | The channel share permissions for the item |\n| <a id=\"property-id\"></a> `id` | `string` | The share permission id |\n| <a id=\"property-linkshare\"></a> `linkShare?` | `null` \\| [`LinkShare`](LinkShare.md) | - |\n| <a id=\"property-linkshareaccesslevel\"></a> `linkShareAccessLevel?` | `null` \\| [`AccessLevel`](AccessLevel.md) | - |\n| <a id=\"property-owner\"></a> `owner` | `string` | The owner of the item |\n| <a id=\"property-teamshareaccesslevel\"></a> `teamShareAccessLevel?` | `null` \\| [`AccessLevel`](AccessLevel.md) | - |\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/ToolActor.md -->\n# Type Alias: ToolActor\n\n> **ToolActor** = `\"requester\"` \\| `\"owner\"`\n\nWhich authenticated participant's access a tool call requests.\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/ToolApprovalAnswerDto.md -->\n# Type Alias: ToolApprovalAnswerDto\n\n> **ToolApprovalAnswerDto** = `\"approve\"` \\| `\"approve_and_remember\"` \\| `\"deny\"` \\| `\"cancel\"`\n\nHow a person answers a held tool call.\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/ToolApprovalStatusDto.md -->\n# Type Alias: ToolApprovalStatusDto\n\n> **ToolApprovalStatusDto** = `\"pending\"` \\| `\"approved\"` \\| `\"denied\"` \\| `\"cancelled\"` \\| `\"expired\"`\n\nWhere a held tool call stands once answered.\n\n\n<!-- generated/calendar/types.gen/type-aliases/EventTime.md -->\n# Type Alias: EventTime\n\n> **EventTime** = \\{ `endsAt`: `string`; `kind`: `\"timed\"`; `startsAt`: `string`; `timeZone?`: `string` \\| `null`; \\} \\| \\{ `endDate`: `string`; `kind`: `\"allDay\"`; `startDate`: `string`; \\}\n\nThe mutually exclusive time shape of a calendar event.\n\nFields are renamed per variant rather than with `rename_all_fields`\nbecause utoipa only honors variant-level serde renames when it\nderives the OpenAPI schema.\n\n## Union Members\n\n### Type Literal\n\n\\{ `endsAt`: `string`; `kind`: `\"timed\"`; `startsAt`: `string`; `timeZone?`: `string` \\| `null`; \\}\n\n| Name | Type | Description |\n| ------ | ------ | ------ |\n| `endsAt` | `string` | Exclusive end instant; equal to the start for an imported point event. |\n| `kind` | `\"timed\"` | - |\n| `startsAt` | `string` | Inclusive start instant. |\n| `timeZone?` | `string` \\| `null` | Original IANA time-zone identifier, when supplied. |\n\n***\n\n### Type Literal\n\n\\{ `endDate`: `string`; `kind`: `\"allDay\"`; `startDate`: `string`; \\}\n\n| Name | Type | Description |\n| ------ | ------ | ------ |\n| `endDate` | `string` | Exclusive local end date. |\n| `kind` | `\"allDay\"` | - |\n| `startDate` | `string` | Inclusive local start date. |\n\n\n<!-- src/entities/agent-sessions/agent-session/classes/AgentSession.md -->\n# Class: AgentSession\n\nA managed or externally hosted coding-agent session.\n\n## Extends\n\n- [`MacroEntity`](../../../entity/classes/MacroEntity.md)\\<[`AgentSessionResponse`](../../../../../generated/agent-harness/types.gen/type-aliases/AgentSessionResponse.md)\\>\n\n## Properties\n\n| Property | Modifier | Type | Description | Inherited from |\n| ------ | ------ | ------ | ------ | ------ |\n| <a id=\"property-access\"></a> `access` | `readonly` | [`EntityAccess`](../../../sharing/access/classes/EntityAccess.md) | Access requests and direct user grants. | - |\n| <a id=\"property-codeexecutions\"></a> `codeExecutions` | `readonly` | \\{ `byId`: (`id`) => [`CodeExecution`](../../code-execution/classes/CodeExecution.md); \\} | Saved code executions scoped to this session. Handle construction makes no request. | - |\n| `codeExecutions.byId` | `public` | (`id`) => [`CodeExecution`](../../code-execution/classes/CodeExecution.md) | - | - |\n| <a id=\"property-createdat\"></a> `createdAt` | `readonly` | () => `Promise`\\<`string`\\> | When the session was created. | - |\n| <a id=\"property-harness\"></a> `harness` | `readonly` | () => `Promise`\\<`string`\\> | The agent harness implementation serving the session. | - |\n| <a id=\"property-id\"></a> `id` | `public readonly` | `string` | - | [`MacroEntity`](../../../entity/classes/MacroEntity.md).[`id`](../../../entity/classes/MacroEntity.md#property-id) |\n| <a id=\"property-instructions\"></a> `instructions` | `readonly` | () => `Promise`\\<`string` \\| `undefined`\\> | Instructions the session's runtime works under, when any were stated at creation. Fixed for the session's life. | - |\n| <a id=\"property-isarchived\"></a> `isArchived` | `readonly` | () => `Promise`\\<`boolean`\\> | Whether the session is archived and read-only. | - |\n| <a id=\"property-model\"></a> `model` | `readonly` | () => `Promise`\\<`string`\\> | The model currently configured for the session. | - |\n| <a id=\"property-modifiedat\"></a> `modifiedAt` | `readonly` | () => `Promise`\\<`string`\\> | When the session was last modified. | - |\n| <a id=\"property-name\"></a> `name` | `readonly` | () => `Promise`\\<`string`\\> | The session's user-facing display name. | - |\n| <a id=\"property-repourl\"></a> `repoUrl` | `readonly` | () => `Promise`\\<`string` \\| `undefined`\\> | The repository the agent works with, when one was supplied. | - |\n| <a id=\"property-reviews\"></a> `reviews` | `readonly` | \\{ `byId`: (`id`) => [`SessionReview`](../../review/classes/SessionReview.md); `create`: (`input`) => `Promise`\\<[`SessionReview`](../../review/classes/SessionReview.md)\\>; \\} | Persisted reviews scoped to this session, with or without a code execution. | - |\n| `reviews.byId` | `public` | (`id`) => [`SessionReview`](../../review/classes/SessionReview.md) | - | - |\n| `reviews.create` | `public` | (`input`) => `Promise`\\<[`SessionReview`](../../review/classes/SessionReview.md)\\> | - | - |\n| <a id=\"property-sandboxsize\"></a> `sandboxSize` | `readonly` | () => `Promise`\\<[`SandboxSize`](../../../../../generated/agent-harness/types.gen/type-aliases/SandboxSize.md)\\> | Compute tier of the managed sandbox. | - |\n| <a id=\"property-status\"></a> `status` | `readonly` | () => `Promise`\\<[`SessionStatusDto`](../../../../../generated/agent-harness/types.gen/type-aliases/SessionStatusDto.md)\\> | The session's latest runtime status. | - |\n\n## Methods\n\n### archive()\n\n> **archive**(): `Promise`\\<`void`\\>\n\nArchive this session, making it read-only until unarchived.\n\n#### Returns\n\n`Promise`\\<`void`\\>\n\n***\n\n### changes()\n\n> **changes**(): `Promise`\\<[`AgentSessionChangesResponse`](../../../../../generated/agent-harness/types.gen/type-aliases/AgentSessionChangesResponse.md)\\>\n\nRead the latest captured GitHub pull request changes and capture status.\n\n#### Returns\n\n`Promise`\\<[`AgentSessionChangesResponse`](../../../../../generated/agent-harness/types.gen/type-aliases/AgentSessionChangesResponse.md)\\>\n\n***\n\n### changesPatch()\n\n> **changesPatch**(): `Promise`\\<`string`\\>\n\nRead the unified diff of the latest captured changeset.\n\n#### Returns\n\n`Promise`\\<`string`\\>\n\n***\n\n### control()\n\n> **control**(`action`): `Promise`\\<[`ControlResponse`](../../../../../generated/agent-harness/types.gen/type-aliases/ControlResponse.md)\\>\n\nSend a prompt or lifecycle operation to the live agent session.\n\nThe returned `actionId` matches `requestId` on the folded message the\naction derives once it dispatches. A `queued` status means a turn was\nrunning: the action waits in the session's queue ([queue](#queue)) and\ndispatches when that turn ends.\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `action` | [`AgentAction`](../../../../../generated/agent-harness/types.gen/type-aliases/AgentAction.md) |\n\n#### Returns\n\n`Promise`\\<[`ControlResponse`](../../../../../generated/agent-harness/types.gen/type-aliases/ControlResponse.md)\\>\n\n***\n\n### delete()\n\n> **delete**(): `Promise`\\<`void`\\>\n\nDelete this session and any live resources it owns.\n\n#### Returns\n\n`Promise`\\<`void`\\>\n\n***\n\n### foldedLog()\n\n> **foldedLog**(): `Promise`\\<[`FoldedAgentSessionLog`](../type-aliases/FoldedAgentSessionLog.md)\\>\n\nRead this session's whole history as the conversation the app renders:\neach user prompt and agent reply, oldest first, with tool calls and their\nresults, plans, permission requests and how each turn ended. Works on any\nsession the caller can view, live or finished, so it is the way to read\nwhat another agent session did. Prefer it to [log](#log), which returns\nraw protocol frames.\n\n#### Returns\n\n`Promise`\\<[`FoldedAgentSessionLog`](../type-aliases/FoldedAgentSessionLog.md)\\>\n\n***\n\n### linkPullRequest()\n\n> **linkPullRequest**(`url`): `Promise`\\<`void`\\>\n\nLink the GitHub pull request at `url` to this session. Linking one that\nis already linked changes nothing.\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `url` | `string` |\n\n#### Returns\n\n`Promise`\\<`void`\\>\n\n***\n\n### log()\n\n> **log**(): `Promise`\\<[`AgentSessionLogResponse`](../../../../../generated/agent-harness/types.gen/type-aliases/AgentSessionLogResponse.md)\\>\n\nRead the complete raw protocol log for this session.\n\n#### Returns\n\n`Promise`\\<[`AgentSessionLogResponse`](../../../../../generated/agent-harness/types.gen/type-aliases/AgentSessionLogResponse.md)\\>\n\n***\n\n### prompt()\n\n> **prompt**(`text`, `attachments?`): `Promise`\\<[`ControlResponse`](../../../../../generated/agent-harness/types.gen/type-aliases/ControlResponse.md)\\>\n\nSend a prompt to the session — sugar over [control](#control).\n\n`attachments` are files the prompt refers to, each by a URL the agent\ncan fetch (a static file service URL in practice); they reach the agent\nas ACP `resource_link` blocks after the text.\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `text` | `string` |\n| `attachments?` | [`PromptAttachment`](../../../../../generated/agent-harness/types.gen/type-aliases/PromptAttachment.md)[] |\n\n#### Returns\n\n`Promise`\\<[`ControlResponse`](../../../../../generated/agent-harness/types.gen/type-aliases/ControlResponse.md)\\>\n\n***\n\n### pullRequests()\n\n> **pullRequests**(): `Promise`\\<[`SessionPullRequest`](../type-aliases/SessionPullRequest.md)[]\\>\n\nThe GitHub pull requests linked to this session: the one its agent\nopened and any a person linked, oldest first.\n\n#### Returns\n\n`Promise`\\<[`SessionPullRequest`](../type-aliases/SessionPullRequest.md)[]\\>\n\n***\n\n### queue()\n\n> **queue**(): `Promise`\\<[`QueuedAction`](../../queued-action/classes/QueuedAction.md)[]\\>\n\nThe actions waiting to dispatch in this session, oldest first. Each can\nbe edited or removed until it dispatches.\n\n#### Returns\n\n`Promise`\\<[`QueuedAction`](../../queued-action/classes/QueuedAction.md)[]\\>\n\n***\n\n### refreshChanges()\n\n> **refreshChanges**(): `Promise`\\<[`AgentSessionChangesResponse`](../../../../../generated/agent-harness/types.gen/type-aliases/AgentSessionChangesResponse.md)\\>\n\nRequest a fresh capture and return the current state while it runs.\n\n#### Returns\n\n`Promise`\\<[`AgentSessionChangesResponse`](../../../../../generated/agent-harness/types.gen/type-aliases/AgentSessionChangesResponse.md)\\>\n\n***\n\n### rename()\n\n> **rename**(`name`): `Promise`\\<`void`\\>\n\nRename this session.\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `name` | `string` |\n\n#### Returns\n\n`Promise`\\<`void`\\>\n\n***\n\n### setSandboxSize()\n\n> **setSandboxSize**(`size`): `Promise`\\<[`SandboxSize`](../../../../../generated/agent-harness/types.gen/type-aliases/SandboxSize.md)\\>\n\nResize this session's sandbox and remember the size as the owner's default.\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `size` | [`SandboxSize`](../../../../../generated/agent-harness/types.gen/type-aliases/SandboxSize.md) |\n\n#### Returns\n\n`Promise`\\<[`SandboxSize`](../../../../../generated/agent-harness/types.gen/type-aliases/SandboxSize.md)\\>\n\n***\n\n### sharePermissions()\n\n> **sharePermissions**(): `Promise`\\<[`SharePermissionV2`](../../../../../generated/agent-harness/types.gen/type-aliases/SharePermissionV2.md)\\>\n\nSharing configuration.\n\n#### Returns\n\n`Promise`\\<[`SharePermissionV2`](../../../../../generated/agent-harness/types.gen/type-aliases/SharePermissionV2.md)\\>\n\n***\n\n### toJSON()\n\n> **toJSON**(): \\{ `detail?`: [`AgentSessionResponse`](../../../../../generated/agent-harness/types.gen/type-aliases/AgentSessionResponse.md); `id`: `string`; \\}\n\n#### Returns\n\n\\{ `detail?`: [`AgentSessionResponse`](../../../../../generated/agent-harness/types.gen/type-aliases/AgentSessionResponse.md); `id`: `string`; \\}\n\n| Name | Type |\n| ------ | ------ |\n| `detail?` | [`AgentSessionResponse`](../../../../../generated/agent-harness/types.gen/type-aliases/AgentSessionResponse.md) |\n| `id` | `string` |\n\n#### Inherited from\n\n[`MacroEntity`](../../../entity/classes/MacroEntity.md).[`toJSON`](../../../entity/classes/MacroEntity.md#tojson)\n\n***\n\n### toolApprovalById()\n\n> **toolApprovalById**(`id`): [`ToolApproval`](../../tool-approval/classes/ToolApproval.md)\n\nConstruct an approval handle scoped to this session.\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `id` | `string` |\n\n#### Returns\n\n[`ToolApproval`](../../tool-approval/classes/ToolApproval.md)\n\n***\n\n### unarchive()\n\n> **unarchive**(): `Promise`\\<`void`\\>\n\nRestore an archived session so it can be edited and prompted again.\n\n#### Returns\n\n`Promise`\\<`void`\\>\n\n***\n\n### unlinkPullRequest()\n\n> **unlinkPullRequest**(`url`): `Promise`\\<`void`\\>\n\nUnlink a GitHub pull request a person linked to this session. The pull\nrequest the session's agent opened stays linked.\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `url` | `string` |\n\n#### Returns\n\n`Promise`\\<`void`\\>\n\n***\n\n### updateSharePermissions()\n\n> **updateSharePermissions**(`options`): `Promise`\\<[`SharePermissionV2`](../../../../../generated/agent-harness/types.gen/type-aliases/SharePermissionV2.md)\\>\n\nReplace sharing grants.\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `options` | [`SharePermissionOptions`](../../../sharing/permissions/type-aliases/SharePermissionOptions.md) |\n\n#### Returns\n\n`Promise`\\<[`SharePermissionV2`](../../../../../generated/agent-harness/types.gen/type-aliases/SharePermissionV2.md)\\>\n\n***\n\n### byId()\n\n> `static` **byId**(`client`, `id`): `AgentSession`\n\nA handle to an agent session by id. Details load on first access.\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `client` | `MacroClient` |\n| `id` | `string` |\n\n#### Returns\n\n`AgentSession`\n\n***\n\n### codingPreferences()\n\n> `static` **codingPreferences**(`client`): `Promise`\\<[`CodingPreferencesBody`](../../../../../generated/agent-harness/types.gen/type-aliases/CodingPreferencesBody.md)\\>\n\nWhat the caller's new coding sessions are told to do beyond their assignment.\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `client` | `MacroClient` |\n\n#### Returns\n\n`Promise`\\<[`CodingPreferencesBody`](../../../../../generated/agent-harness/types.gen/type-aliases/CodingPreferencesBody.md)\\>\n\n***\n\n### createManaged()\n\n> `static` **createManaged**(`client`, `opts?`): `Promise`\\<`AgentSession`\\>\n\nCreate a managed session, optionally delivering its first prompt.\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `client` | `MacroClient` |\n| `opts?` | [`CreateManagedSessionOptions`](../type-aliases/CreateManagedSessionOptions.md) |\n\n#### Returns\n\n`Promise`\\<`AgentSession`\\>\n\n***\n\n### defaultSandboxSize()\n\n> `static` **defaultSandboxSize**(`client`): `Promise`\\<[`SandboxSize`](../../../../../generated/agent-harness/types.gen/type-aliases/SandboxSize.md)\\>\n\nThe caller's default sandbox size for new `@coder` sessions.\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `client` | `MacroClient` |\n\n#### Returns\n\n`Promise`\\<[`SandboxSize`](../../../../../generated/agent-harness/types.gen/type-aliases/SandboxSize.md)\\>\n\n***\n\n### forPullRequest()\n\n> `static` **forPullRequest**(`client`, `url`): `Promise`\\<`AgentSession`[]\\>\n\nThe sessions linked to the GitHub pull request at `url` that the caller\ncan view, oldest link first.\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `client` | `MacroClient` |\n| `url` | `string` |\n\n#### Returns\n\n`Promise`\\<`AgentSession`[]\\>\n\n***\n\n### repositories()\n\n> `static` **repositories**(`client`): `Promise`\\<[`SelectableRepository`](../type-aliases/SelectableRepository.md)[]\\>\n\nThe GitHub repositories the caller can hand a managed session: every\nrepository under an installation of Macro's GitHub App they or their\nteams made, sorted by `owner/name`. Empty when the App is installed\nnowhere they reach.\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `client` | `MacroClient` |\n\n#### Returns\n\n`Promise`\\<[`SelectableRepository`](../type-aliases/SelectableRepository.md)[]\\>\n\n***\n\n### repositoryBranches()\n\n> `static` **repositoryBranches**(`client`, `repoUrl`): `Promise`\\<`string`[]\\>\n\nBranch names on one repository the caller can start a managed session\nfrom, in the order GitHub listed them. Empty when the repository has\nno commits yet. `repoUrl` is one of the URLs [AgentSession.repositories](#repositories)\nlists.\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `client` | `MacroClient` |\n| `repoUrl` | `string` |\n\n#### Returns\n\n`Promise`\\<`string`[]\\>\n\n***\n\n### setCodingPreferences()\n\n> `static` **setCodingPreferences**(`client`, `preferences`): `Promise`\\<[`CodingPreferencesBody`](../../../../../generated/agent-harness/types.gen/type-aliases/CodingPreferencesBody.md)\\>\n\nReplace the caller's coding preferences.\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `client` | `MacroClient` |\n| `preferences` | [`CodingPreferencesBody`](../../../../../generated/agent-harness/types.gen/type-aliases/CodingPreferencesBody.md) |\n\n#### Returns\n\n`Promise`\\<[`CodingPreferencesBody`](../../../../../generated/agent-harness/types.gen/type-aliases/CodingPreferencesBody.md)\\>\n\n***\n\n### setDefaultSandboxSize()\n\n> `static` **setDefaultSandboxSize**(`client`, `size`): `Promise`\\<[`SandboxSize`](../../../../../generated/agent-harness/types.gen/type-aliases/SandboxSize.md)\\>\n\nSet the caller's default sandbox size for the next `@coder` mention.\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `client` | `MacroClient` |\n| `size` | [`SandboxSize`](../../../../../generated/agent-harness/types.gen/type-aliases/SandboxSize.md) |\n\n#### Returns\n\n`Promise`\\<[`SandboxSize`](../../../../../generated/agent-harness/types.gen/type-aliases/SandboxSize.md)\\>\n\n\n<!-- src/entities/agent-sessions/agent-session/type-aliases/CreateManagedSessionOptions.md -->\n# Type Alias: CreateManagedSessionOptions\n\n> **CreateManagedSessionOptions** = \\{ `instructions?`: `string`; `model?`: `string`; `prompt?`: `string`; `repoBranch?`: `string`; `repoUrl?`: `string`; \\}\n\nWhat a managed session is created with.\n\n## Properties\n\n| Property | Type | Description |\n| ------ | ------ | ------ |\n| <a id=\"property-instructions\"></a> `instructions?` | `string` | Instructions the session's runtime works under, fixed for its life. |\n| <a id=\"property-model\"></a> `model?` | `string` | The model the session runs on, instead of its persona's. The session's model from creation, read back as [AgentSession.model](../classes/AgentSession.md#property-model). |\n| <a id=\"property-prompt\"></a> `prompt?` | `string` | First prompt to deliver once the session is running. |\n| <a id=\"property-repobranch\"></a> `repoBranch?` | `string` | The branch the session starts on; needs `repoUrl`. Omitted, the repository's default branch. |\n| <a id=\"property-repourl\"></a> `repoUrl?` | `string` | The repository the session works on, as one of the URLs [AgentSession.repositories](../classes/AgentSession.md#repositories) lists for the caller. Omitted, the runtime chooses from the prompt. Honored for Cursor sessions. |\n\n\n<!-- src/entities/agent-sessions/agent-session/type-aliases/FoldedAgentSessionLog.md -->\n# Type Alias: FoldedAgentSessionLog\n\n> **FoldedAgentSessionLog** = \\{ `bot`: [`SessionBot`](../../../../../generated/agent-harness/types.gen/type-aliases/SessionBot.md); `messages`: `FoldedMessage`[]; \\}\n\nA session's history folded into readable messages, oldest first.\n\n## Properties\n\n| Property | Type | Description |\n| ------ | ------ | ------ |\n| <a id=\"property-bot\"></a> `bot` | [`SessionBot`](../../../../../generated/agent-harness/types.gen/type-aliases/SessionBot.md) | The agent that sent every agent message. |\n| <a id=\"property-messages\"></a> `messages` | `FoldedMessage`[] | Prompts and replies with their tool calls, results, plans, permission requests and how each turn ended. |\n\n\n<!-- src/entities/agent-sessions/agent-session/type-aliases/SelectableRepository.md -->\n# Type Alias: SelectableRepository\n\n> **SelectableRepository** = \\{ `defaultBranch?`: `string`; `url`: `string`; \\}\n\nA GitHub repository the caller can point a managed session at.\n\n## Properties\n\n| Property | Type | Description |\n| ------ | ------ | ------ |\n| <a id=\"property-defaultbranch\"></a> `defaultBranch?` | `string` | The branch its clones check out, and where a session starts unless `createManaged` names another. Absent for a repository with no commits. |\n| <a id=\"property-url\"></a> `url` | `string` | The canonical `https://github.com/owner/name` URL, as `createManaged` takes it. |\n\n\n<!-- src/entities/agent-sessions/agent-session/type-aliases/SessionPullRequest.md -->\n# Type Alias: SessionPullRequest\n\n> **SessionPullRequest** = \\{ `createdAt`: `string`; `githubKey`: `string`; `linkedBy?`: [`User`](../../../users/user/classes/User.md); `source`: [`PullRequestLinkSource`](../../../../../generated/agent-harness/types.gen/type-aliases/PullRequestLinkSource.md); `url`: `string`; \\}\n\nA GitHub pull request linked to a session.\n\n## Properties\n\n| Property | Type | Description |\n| ------ | ------ | ------ |\n| <a id=\"property-createdat\"></a> `createdAt` | `string` | When it was linked. |\n| <a id=\"property-githubkey\"></a> `githubKey` | `string` | The pull request's `owner/repo/pull/number` key. |\n| <a id=\"property-linkedby\"></a> `linkedBy?` | [`User`](../../../users/user/classes/User.md) | Who linked it, for pull requests a person linked. |\n| <a id=\"property-source\"></a> `source` | [`PullRequestLinkSource`](../../../../../generated/agent-harness/types.gen/type-aliases/PullRequestLinkSource.md) | Whether the session's agent opened it or a person linked it. |\n| <a id=\"property-url\"></a> `url` | `string` | The pull request's GitHub URL. |\n\n\n<!-- src/entities/agent-sessions/code-execution/classes/CodeExecution.md -->\n# Class: CodeExecution\n\nA saved code execution belonging to one agent session.\n\n## Extends\n\n- [`MacroEntity`](../../../entity/classes/MacroEntity.md)\\<[`ExecutionRecord`](../../../../../generated/agent-harness/types.gen/type-aliases/ExecutionRecord.md)\\>\n\n## Properties\n\n| Property | Modifier | Type | Description | Inherited from |\n| ------ | ------ | ------ | ------ | ------ |\n| <a id=\"property-calls\"></a> `calls` | `readonly` | () => `Promise`\\<[`RecordedToolCall`](../../../../../generated/agent-harness/types.gen/type-aliases/RecordedToolCall.md)[]\\> | Legacy bridge calls; direct HTTP executions leave this empty. | - |\n| <a id=\"property-error\"></a> `error` | `readonly` | () => `Promise`\\<`string` \\| `undefined`\\> | Execution failure text, when present. | - |\n| <a id=\"property-id\"></a> `id` | `public readonly` | `string` | - | [`MacroEntity`](../../../entity/classes/MacroEntity.md).[`id`](../../../entity/classes/MacroEntity.md#property-id) |\n| <a id=\"property-reviews\"></a> `reviews` | `readonly` | \\{ `create`: (`input`) => `Promise`\\<[`SessionReview`](../../review/classes/SessionReview.md)\\>; `list`: () => `Promise`\\<[`SessionReview`](../../review/classes/SessionReview.md)[]\\>; \\} | Persistent UI blocks, independent of the execution lifetime. | - |\n| `reviews.create` | `public` | (`input`) => `Promise`\\<[`SessionReview`](../../review/classes/SessionReview.md)\\> | - | - |\n| `reviews.list` | `public` | () => `Promise`\\<[`SessionReview`](../../review/classes/SessionReview.md)[]\\> | - | - |\n| <a id=\"property-source\"></a> `source` | `readonly` | () => `Promise`\\<`string`\\> | The submitted JavaScript module source. | - |\n| <a id=\"property-status\"></a> `status` | `readonly` | () => `Promise`\\<[`ExecutionStatus`](../../../../../generated/agent-harness/types.gen/type-aliases/ExecutionStatus.md)\\> | Saved execution state. Call refresh to observe subsequent changes. | - |\n\n## Methods\n\n### refresh()\n\n> **refresh**(): `Promise`\\<`CodeExecution`\\>\n\nReload the saved state, for example after a running execution completes.\n\n#### Returns\n\n`Promise`\\<`CodeExecution`\\>\n\n***\n\n### result()\n\n> **result**(): `Promise`\\<`unknown`\\>\n\nThe program's returned JSON; preserves an explicit null result.\n\n#### Returns\n\n`Promise`\\<`unknown`\\>\n\n***\n\n### session()\n\n> **session**(): [`AgentSession`](../../agent-session/classes/AgentSession.md)\n\nThe session this execution belongs to.\n\n#### Returns\n\n[`AgentSession`](../../agent-session/classes/AgentSession.md)\n\n***\n\n### toJSON()\n\n> **toJSON**(): \\{ `detail?`: [`ExecutionRecord`](../../../../../generated/agent-harness/types.gen/type-aliases/ExecutionRecord.md); `id`: `string`; \\}\n\n#### Returns\n\n\\{ `detail?`: [`ExecutionRecord`](../../../../../generated/agent-harness/types.gen/type-aliases/ExecutionRecord.md); `id`: `string`; \\}\n\n| Name | Type |\n| ------ | ------ |\n| `detail?` | [`ExecutionRecord`](../../../../../generated/agent-harness/types.gen/type-aliases/ExecutionRecord.md) |\n| `id` | `string` |\n\n#### Inherited from\n\n[`MacroEntity`](../../../entity/classes/MacroEntity.md).[`toJSON`](../../../entity/classes/MacroEntity.md#tojson)\n\n***\n\n### byId()\n\n> `static` **byId**(`client`, `session`, `id`): `CodeExecution`\n\nConstruct a lazy execution handle within its owning session.\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `client` | `MacroClient` |\n| `session` | [`AgentSession`](../../agent-session/classes/AgentSession.md) |\n| `id` | `string` |\n\n#### Returns\n\n`CodeExecution`\n\n\n<!-- src/entities/agent-sessions/code-execution/type-aliases/CodeResult.md -->\n# Type Alias: CodeResult\n\n> **CodeResult** = \\{ `data`: [`JsonValue`](JsonValue.md); `reviews`: \\{ `id`: `string`; `kind`: `\"email\"` \\| `\"calendar\"`; \\}[]; \\}\n\nJSON data returned to the model, plus references to independently saved UI.\n\n## Properties\n\n| Property | Type |\n| ------ | ------ |\n| <a id=\"property-data\"></a> `data` | [`JsonValue`](JsonValue.md) |\n| <a id=\"property-reviews\"></a> `reviews` | \\{ `id`: `string`; `kind`: `\"email\"` \\| `\"calendar\"`; \\}[] |\n\n\n<!-- src/entities/agent-sessions/code-execution/type-aliases/JsonValue.md -->\n# Type Alias: JsonValue\n\n> **JsonValue** = `null` \\| `boolean` \\| `number` \\| `string` \\| `JsonValue`[] \\| \\{\\[`key`: `string`\\]: `JsonValue`; \\}\n\n\n<!-- src/entities/agent-sessions/conversation/classes/AgentConversation.md -->\n# Class: AgentConversation\n\nAn agent's conversation in one channel.\n\n## Constructors\n\n### Constructor\n\n> **new AgentConversation**(`client`, `channel`, `record`): `AgentConversation`\n\n#### Parameters\n\n| Parameter | Type | Description |\n| ------ | ------ | ------ |\n| `client` | `MacroClient` | - |\n| `channel` | [`Channel`](../../../channels/channel/classes/Channel.md) | Parent channel. |\n| `record` | [`AgentConversationDto`](../../../../../generated/agent-harness/types.gen/type-aliases/AgentConversationDto.md) | - |\n\n#### Returns\n\n`AgentConversation`\n\n## Properties\n\n| Property | Modifier | Type | Description |\n| ------ | ------ | ------ | ------ |\n| <a id=\"property-channel\"></a> `channel` | `readonly` | [`Channel`](../../../channels/channel/classes/Channel.md) | Parent channel. |\n\n## Methods\n\n### agent()\n\n> **agent**(): [`Agent`](../../../agents/agent/classes/Agent.md)\n\nThe agent taking part.\n\n#### Returns\n\n[`Agent`](../../../agents/agent/classes/Agent.md)\n\n***\n\n### sessions()\n\n> **sessions**(): \\{ `createdAt`: `string`; `isCurrent`: `boolean`; `session`: [`AgentSession`](../../agent-session/classes/AgentSession.md); \\}[]\n\nConversation sessions with their current flag.\n\n#### Returns\n\n\\{ `createdAt`: `string`; `isCurrent`: `boolean`; `session`: [`AgentSession`](../../agent-session/classes/AgentSession.md); \\}[]\n\n***\n\n### startFresh()\n\n> **startFresh**(`session`): `Promise`\\<`void`\\>\n\nStart a fresh conversation from one of its sessions.\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `session` | [`AgentSession`](../../agent-session/classes/AgentSession.md) |\n\n#### Returns\n\n`Promise`\\<`void`\\>\n\n***\n\n### toJSON()\n\n> **toJSON**(): \\{ `agentId`: `string`; `channelId`: `string`; \\}\n\nSerialize scoped identity.\n\n#### Returns\n\n\\{ `agentId`: `string`; `channelId`: `string`; \\}\n\n| Name | Type |\n| ------ | ------ |\n| `agentId` | `string` |\n| `channelId` | `string` |\n\n***\n\n### turns()\n\n> **turns**(): [`ConversationTurn`](ConversationTurn.md)[]\n\nRecorded conversation turns.\n\n#### Returns\n\n[`ConversationTurn`](ConversationTurn.md)[]\n\n\n<!-- src/entities/agent-sessions/conversation/classes/ConversationTurn.md -->\n# Class: ConversationTurn\n\nOne agent conversation turn, identified by its source message.\n\n## Constructors\n\n### Constructor\n\n> **new ConversationTurn**(`client`, `conversation`, `record`): `ConversationTurn`\n\n#### Parameters\n\n| Parameter | Type | Description |\n| ------ | ------ | ------ |\n| `client` | `MacroClient` | - |\n| `conversation` | [`AgentConversation`](AgentConversation.md) | Parent conversation. |\n| `record` | [`AgentConversationTurnDto`](../../../../../generated/agent-harness/types.gen/type-aliases/AgentConversationTurnDto.md) | - |\n\n#### Returns\n\n`ConversationTurn`\n\n## Properties\n\n| Property | Modifier | Type | Description |\n| ------ | ------ | ------ | ------ |\n| <a id=\"property-conversation\"></a> `conversation` | `readonly` | [`AgentConversation`](AgentConversation.md) | Parent conversation. |\n\n## Methods\n\n### retry()\n\n> **retry**(`actionId`): `Promise`\\<`void`\\>\n\nRetry with a caller persisted action nonce. Never retries automatically.\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `actionId` | `string` |\n\n#### Returns\n\n`Promise`\\<`void`\\>\n\n***\n\n### session()\n\n> **session**(): [`AgentSession`](../../agent-session/classes/AgentSession.md)\n\nSession that handled this turn.\n\n#### Returns\n\n[`AgentSession`](../../agent-session/classes/AgentSession.md)\n\n***\n\n### source()\n\n> **source**(): [`Message`](../../../channels/message/classes/Message.md)\n\nOriginating message.\n\n#### Returns\n\n[`Message`](../../../channels/message/classes/Message.md)\n\n***\n\n### toJSON()\n\n> **toJSON**(): \\{ `channelId`: `string`; `sourceMessageId`: `string`; \\}\n\nSerialize scoped identity.\n\n#### Returns\n\n\\{ `channelId`: `string`; `sourceMessageId`: `string`; \\}\n\n| Name | Type |\n| ------ | ------ |\n| `channelId` | `string` |\n| `sourceMessageId` | `string` |\n\n\n<!-- src/entities/agent-sessions/namespace/classes/AgentSessionNamespace.md -->\n# Class: AgentSessionNamespace\n\nEntry point for coding-agent sessions.\n\n## Constructors\n\n### Constructor\n\n> **new AgentSessionNamespace**(`client`): `AgentSessionNamespace`\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `client` | `MacroClient` |\n\n#### Returns\n\n`AgentSessionNamespace`\n\n## Methods\n\n### byId()\n\n> **byId**(`id`): [`AgentSession`](../../agent-session/classes/AgentSession.md)\n\nA handle to an agent session by id. Details load on first access.\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `id` | `string` |\n\n#### Returns\n\n[`AgentSession`](../../agent-session/classes/AgentSession.md)\n\n***\n\n### codingPreferences()\n\n> **codingPreferences**(): `Promise`\\<[`CodingPreferencesBody`](../../../../../generated/agent-harness/types.gen/type-aliases/CodingPreferencesBody.md)\\>\n\nWhat the caller's new coding sessions are told to do beyond their assignment.\n\n#### Returns\n\n`Promise`\\<[`CodingPreferencesBody`](../../../../../generated/agent-harness/types.gen/type-aliases/CodingPreferencesBody.md)\\>\n\n***\n\n### createManaged()\n\n> **createManaged**(`opts?`): `Promise`\\<[`AgentSession`](../../agent-session/classes/AgentSession.md)\\>\n\nCreate a managed agent session.\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `opts?` | [`CreateManagedSessionOptions`](../../agent-session/type-aliases/CreateManagedSessionOptions.md) |\n\n#### Returns\n\n`Promise`\\<[`AgentSession`](../../agent-session/classes/AgentSession.md)\\>\n\n***\n\n### defaultSandboxSize()\n\n> **defaultSandboxSize**(): `Promise`\\<[`SandboxSize`](../../../../../generated/agent-harness/types.gen/type-aliases/SandboxSize.md)\\>\n\nThe caller's default sandbox size for new `@coder` sessions.\n\n#### Returns\n\n`Promise`\\<[`SandboxSize`](../../../../../generated/agent-harness/types.gen/type-aliases/SandboxSize.md)\\>\n\n***\n\n### forPullRequest()\n\n> **forPullRequest**(`url`): `Promise`\\<[`AgentSession`](../../agent-session/classes/AgentSession.md)[]\\>\n\nThe sessions linked to the GitHub pull request at `url` that the caller\ncan view.\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `url` | `string` |\n\n#### Returns\n\n`Promise`\\<[`AgentSession`](../../agent-session/classes/AgentSession.md)[]\\>\n\n***\n\n### repositories()\n\n> **repositories**(): `Promise`\\<[`SelectableRepository`](../../agent-session/type-aliases/SelectableRepository.md)[]\\>\n\nThe GitHub repositories the caller can hand a managed session, with the\nbranch each one's sessions start on by default.\n\n#### Returns\n\n`Promise`\\<[`SelectableRepository`](../../agent-session/type-aliases/SelectableRepository.md)[]\\>\n\n***\n\n### repositoryBranches()\n\n> **repositoryBranches**(`repoUrl`): `Promise`\\<`string`[]\\>\n\nBranch names on one repository the caller can start a managed session\nfrom. `repoUrl` is one of the URLs [AgentSessionNamespace.repositories](#repositories)\nlists.\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `repoUrl` | `string` |\n\n#### Returns\n\n`Promise`\\<`string`[]\\>\n\n***\n\n### setCodingPreferences()\n\n> **setCodingPreferences**(`preferences`): `Promise`\\<[`CodingPreferencesBody`](../../../../../generated/agent-harness/types.gen/type-aliases/CodingPreferencesBody.md)\\>\n\nReplace the caller's coding preferences.\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `preferences` | [`CodingPreferencesBody`](../../../../../generated/agent-harness/types.gen/type-aliases/CodingPreferencesBody.md) |\n\n#### Returns\n\n`Promise`\\<[`CodingPreferencesBody`](../../../../../generated/agent-harness/types.gen/type-aliases/CodingPreferencesBody.md)\\>\n\n***\n\n### setDefaultSandboxSize()\n\n> **setDefaultSandboxSize**(`size`): `Promise`\\<[`SandboxSize`](../../../../../generated/agent-harness/types.gen/type-aliases/SandboxSize.md)\\>\n\nSet the caller's default sandbox size for the next `@coder` mention.\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `size` | [`SandboxSize`](../../../../../generated/agent-harness/types.gen/type-aliases/SandboxSize.md) |\n\n#### Returns\n\n`Promise`\\<[`SandboxSize`](../../../../../generated/agent-harness/types.gen/type-aliases/SandboxSize.md)\\>\n\n\n<!-- src/entities/agent-sessions/queued-action/classes/QueuedAction.md -->\n# Class: QueuedAction\n\nOne action waiting in an agent session's server-side queue: a prompt or a\ncompact accepted while a turn was running, dispatching when that turn\nends. Until then it can be edited (prompts only), steered, or removed; once\nit has dispatched, these actions answer 404 — there is no un-sending.\n\nList-sourced only - there is no per-action GET, only the queue snapshot -\nso this wraps whatever DTO the caller already has, the same shape\n`CrmComment` wraps a list-sourced record, rather than `MacroEntity`'s\nlazy-fetch-by-id.\n\n## Accessors\n\n### actionId\n\n#### Get Signature\n\n> **get** **actionId**(): `string`\n\nThe id the action was accepted under. Once it dispatches, this equals\n`requestId` on the folded message the action derives.\n\n##### Returns\n\n`string`\n\n***\n\n### actorUserId\n\n#### Get Signature\n\n> **get** **actorUserId**(): `string` \\| `undefined`\n\nThe user who queued it, absent when a bot acted on nobody's behalf.\n\n##### Returns\n\n`string` \\| `undefined`\n\n***\n\n### attachments\n\n#### Get Signature\n\n> **get** **attachments**(): [`PromptAttachment`](../../../../../generated/agent-harness/types.gen/type-aliases/PromptAttachment.md)[]\n\nFiles the prompt refers to; empty for a compact. Kept through an edit.\n\n##### Returns\n\n[`PromptAttachment`](../../../../../generated/agent-harness/types.gen/type-aliases/PromptAttachment.md)[]\n\n***\n\n### createdAt\n\n#### Get Signature\n\n> **get** **createdAt**(): `string`\n\nWhen the action was accepted, as an RFC 3339 timestamp.\n\n##### Returns\n\n`string`\n\n***\n\n### kind\n\n#### Get Signature\n\n> **get** **kind**(): `string`\n\n`prompt` or `compact`; only turn-occupying actions are ever queued.\n\n##### Returns\n\n`string`\n\n***\n\n### prompt\n\n#### Get Signature\n\n> **get** **prompt**(): `string` \\| `undefined`\n\nThe prompt's raw text, present for prompts only. What an edit replaces.\n\n##### Returns\n\n`string` \\| `undefined`\n\n## Methods\n\n### edit()\n\n> **edit**(`prompt`): `Promise`\\<`QueuedAction`\\>\n\nReplace this queued prompt's text before it dispatches. Returns this\nhandle for chaining; the edit endpoint answers 204, so `prompt` is\napplied locally rather than reread from a response body.\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `prompt` | `string` |\n\n#### Returns\n\n`Promise`\\<`QueuedAction`\\>\n\n***\n\n### remove()\n\n> **remove**(): `Promise`\\<`void`\\>\n\nRemove this action from the queue before it dispatches.\n\n#### Returns\n\n`Promise`\\<`void`\\>\n\n***\n\n### steer()\n\n> **steer**(): `Promise`\\<`void`\\>\n\nMove this action to the front and interrupt the current turn to run it next.\n\n#### Returns\n\n`Promise`\\<`void`\\>\n\n***\n\n### from()\n\n> `static` **from**(`client`, `sessionId`, `dto`): `QueuedAction`\n\nWrap a queue entry already in hand (e.g. from `AgentSession.queue()`).\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `client` | `MacroClient` |\n| `sessionId` | `string` |\n| `dto` | [`QueuedActionDto`](../../../../../generated/agent-harness/types.gen/type-aliases/QueuedActionDto.md) |\n\n#### Returns\n\n`QueuedAction`\n\n\n<!-- src/entities/agent-sessions/review/classes/ReviewItem.md -->\n# Class: ReviewItem\n\nAn item handle scopes edits and decisions to its persisted review.\n\n## Constructors\n\n### Constructor\n\n> **new ReviewItem**(`snapshot`, `actions`): `ReviewItem`\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `snapshot` | [`ReviewItem`](../../../../../generated/agent-harness/types.gen/type-aliases/ReviewItem.md) |\n| `actions` | \\{ `dismiss`: () => `Promise`\\<`void`\\>; `edit`: (`draft`) => `Promise`\\<`void`\\>; `submit`: () => `Promise`\\<`void`\\>; \\} |\n| `actions.dismiss` | () => `Promise`\\<`void`\\> |\n| `actions.edit` | (`draft`) => `Promise`\\<`void`\\> |\n| `actions.submit` | () => `Promise`\\<`void`\\> |\n\n#### Returns\n\n`ReviewItem`\n\n## Accessors\n\n### draft\n\n#### Get Signature\n\n> **get** **draft**(): `unknown`\n\n##### Returns\n\n`unknown`\n\n***\n\n### id\n\n#### Get Signature\n\n> **get** **id**(): `string`\n\n##### Returns\n\n`string`\n\n***\n\n### result\n\n#### Get Signature\n\n> **get** **result**(): `unknown`\n\n##### Returns\n\n`unknown`\n\n***\n\n### status\n\n#### Get Signature\n\n> **get** **status**(): [`ReviewItemStatus`](../../../../../generated/agent-harness/types.gen/type-aliases/ReviewItemStatus.md)\n\n##### Returns\n\n[`ReviewItemStatus`](../../../../../generated/agent-harness/types.gen/type-aliases/ReviewItemStatus.md)\n\n## Methods\n\n### dismiss()\n\n> **dismiss**(): `Promise`\\<`void`\\>\n\n#### Returns\n\n`Promise`\\<`void`\\>\n\n***\n\n### edit()\n\n> **edit**(`draft`): `Promise`\\<`void`\\>\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `draft` | `unknown` |\n\n#### Returns\n\n`Promise`\\<`void`\\>\n\n***\n\n### submit()\n\n> **submit**(): `Promise`\\<`void`\\>\n\n#### Returns\n\n`Promise`\\<`void`\\>\n\n\n<!-- src/entities/agent-sessions/review/classes/SessionReview.md -->\n# Class: SessionReview\n\nA persistent review remains actionable after the code execution ends.\n\n## Extends\n\n- [`MacroEntity`](../../../entity/classes/MacroEntity.md)\\<[`ReviewRecord`](../../../../../generated/agent-harness/types.gen/type-aliases/ReviewRecord.md)\\>\n\n## Properties\n\n| Property | Modifier | Type | Inherited from |\n| ------ | ------ | ------ | ------ |\n| <a id=\"property-id\"></a> `id` | `public readonly` | `string` | [`MacroEntity`](../../../entity/classes/MacroEntity.md).[`id`](../../../entity/classes/MacroEntity.md#property-id) |\n| <a id=\"property-kind\"></a> `kind` | `readonly` | () => `Promise`\\<[`ReviewKind`](../../../../../generated/agent-harness/types.gen/type-aliases/ReviewKind.md)\\> | - |\n| <a id=\"property-title\"></a> `title` | `readonly` | () => `Promise`\\<`string`\\> | - |\n\n## Methods\n\n### dismissAll()\n\n> **dismissAll**(): `Promise`\\<`void`\\>\n\n#### Returns\n\n`Promise`\\<`void`\\>\n\n***\n\n### items()\n\n> **items**(): `Promise`\\<[`ReviewItem`](ReviewItem.md)[]\\>\n\n#### Returns\n\n`Promise`\\<[`ReviewItem`](ReviewItem.md)[]\\>\n\n***\n\n### reference()\n\n> **reference**(): `Promise`\\<\\{ `id`: `string`; `kind`: [`ReviewKind`](../../../../../generated/agent-harness/types.gen/type-aliases/ReviewKind.md); \\}\\>\n\nCompact reference for main()'s returned reviews array.\n\n#### Returns\n\n`Promise`\\<\\{ `id`: `string`; `kind`: [`ReviewKind`](../../../../../generated/agent-harness/types.gen/type-aliases/ReviewKind.md); \\}\\>\n\n***\n\n### refresh()\n\n> **refresh**(): `Promise`\\<`SessionReview`\\>\n\n#### Returns\n\n`Promise`\\<`SessionReview`\\>\n\n***\n\n### session()\n\n> **session**(): [`AgentSession`](../../agent-session/classes/AgentSession.md)\n\n#### Returns\n\n[`AgentSession`](../../agent-session/classes/AgentSession.md)\n\n***\n\n### submitAll()\n\n> **submitAll**(): `Promise`\\<`void`\\>\n\nSubmit the saved pending drafts. Never retries a conflict or ambiguous send.\n\n#### Returns\n\n`Promise`\\<`void`\\>\n\n***\n\n### toJSON()\n\n> **toJSON**(): \\{ `detail?`: [`ReviewRecord`](../../../../../generated/agent-harness/types.gen/type-aliases/ReviewRecord.md); `id`: `string`; \\}\n\n#### Returns\n\n\\{ `detail?`: [`ReviewRecord`](../../../../../generated/agent-harness/types.gen/type-aliases/ReviewRecord.md); `id`: `string`; \\}\n\n| Name | Type |\n| ------ | ------ |\n| `detail?` | [`ReviewRecord`](../../../../../generated/agent-harness/types.gen/type-aliases/ReviewRecord.md) |\n| `id` | `string` |\n\n#### Inherited from\n\n[`MacroEntity`](../../../entity/classes/MacroEntity.md).[`toJSON`](../../../entity/classes/MacroEntity.md#tojson)\n\n***\n\n### byId()\n\n> `static` **byId**(`client`, `session`, `id`): `SessionReview`\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `client` | `MacroClient` |\n| `session` | [`AgentSession`](../../agent-session/classes/AgentSession.md) |\n| `id` | `string` |\n\n#### Returns\n\n`SessionReview`\n\n***\n\n### create()\n\n> `static` **create**(`client`, `execution`, `input`): `Promise`\\<`SessionReview`\\>\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `client` | `MacroClient` |\n| `execution` | [`CodeExecution`](../../code-execution/classes/CodeExecution.md) |\n| `input` | [`CreateReviewOptions`](../type-aliases/CreateReviewOptions.md) |\n\n#### Returns\n\n`Promise`\\<`SessionReview`\\>\n\n***\n\n### createForSession()\n\n> `static` **createForSession**(`client`, `session`, `input`, `execution?`): `Promise`\\<`SessionReview`\\>\n\nPresent a review in a session without requiring a code execution.\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `client` | `MacroClient` |\n| `session` | [`AgentSession`](../../agent-session/classes/AgentSession.md) |\n| `input` | [`CreateReviewOptions`](../type-aliases/CreateReviewOptions.md) |\n| `execution?` | [`CodeExecution`](../../code-execution/classes/CodeExecution.md) |\n\n#### Returns\n\n`Promise`\\<`SessionReview`\\>\n\n***\n\n### fromRecord()\n\n> `static` **fromRecord**(`client`, `session`, `record`): `SessionReview`\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `client` | `MacroClient` |\n| `session` | [`AgentSession`](../../agent-session/classes/AgentSession.md) |\n| `record` | [`ReviewRecord`](../../../../../generated/agent-harness/types.gen/type-aliases/ReviewRecord.md) |\n\n#### Returns\n\n`SessionReview`\n\n***\n\n### list()\n\n> `static` **list**(`client`, `execution`): `Promise`\\<`SessionReview`[]\\>\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `client` | `MacroClient` |\n| `execution` | [`CodeExecution`](../../code-execution/classes/CodeExecution.md) |\n\n#### Returns\n\n`Promise`\\<`SessionReview`[]\\>\n\n\n<!-- src/entities/agent-sessions/review/type-aliases/CreateReviewOptions.md -->\n# Type Alias: CreateReviewOptions\n\n> **CreateReviewOptions** = \\{ `drafts`: [`ReviewEmailDraft`](ReviewEmailDraft.md)[]; `kind`: `\"email\"`; `title`: `string`; \\} \\| \\{ `drafts`: [`ReviewCalendarDraft`](ReviewCalendarDraft.md)[]; `kind`: `\"calendar\"`; `title`: `string`; \\}\n\n\n<!-- src/entities/agent-sessions/review/type-aliases/ReviewCalendarDraft.md -->\n# Type Alias: ReviewCalendarDraft\n\n> **ReviewCalendarDraft** = \\{ `addGoogleMeet?`: `boolean`; `addMacroCall?`: `boolean`; `attendees?`: \\{ `email`: `string`; `isOptional?`: `boolean`; \\}[]; `calendarId?`: `string` \\| `null`; `description?`: `string` \\| `null`; `eventType?`: `\"default\"` \\| `\"out_of_office\"`; `location?`: `string` \\| `null`; `outOfOffice?`: \\{ `autoDeclineMode?`: `\"decline_none\"` \\| `\"decline_all\"` \\| `\"decline_new_only\"`; `declineMessage?`: `string` \\| `null`; \\} \\| `null`; `recurrenceLines?`: `string`[]; `reminders?`: \\{ `overrides?`: \\{ `method`: `string`; `minutes`: `number`; \\}[]; `useDefault`: `boolean`; \\} \\| `null`; `time`: [`EventTime`](../../../../../generated/calendar/types.gen/type-aliases/EventTime.md); `title`: `string`; \\}\n\nCalendar drafts use the existing calendar composer and creation action.\n\n## Properties\n\n| Property | Type |\n| ------ | ------ |\n| <a id=\"property-addgooglemeet\"></a> `addGoogleMeet?` | `boolean` |\n| <a id=\"property-addmacrocall\"></a> `addMacroCall?` | `boolean` |\n| <a id=\"property-attendees\"></a> `attendees?` | \\{ `email`: `string`; `isOptional?`: `boolean`; \\}[] |\n| <a id=\"property-calendarid\"></a> `calendarId?` | `string` \\| `null` |\n| <a id=\"property-description\"></a> `description?` | `string` \\| `null` |\n| <a id=\"property-eventtype\"></a> `eventType?` | `\"default\"` \\| `\"out_of_office\"` |\n| <a id=\"property-location\"></a> `location?` | `string` \\| `null` |\n| <a id=\"property-outofoffice\"></a> `outOfOffice?` | \\{ `autoDeclineMode?`: `\"decline_none\"` \\| `\"decline_all\"` \\| `\"decline_new_only\"`; `declineMessage?`: `string` \\| `null`; \\} \\| `null` |\n| <a id=\"property-recurrencelines\"></a> `recurrenceLines?` | `string`[] |\n| <a id=\"property-reminders\"></a> `reminders?` | \\{ `overrides?`: \\{ `method`: `string`; `minutes`: `number`; \\}[]; `useDefault`: `boolean`; \\} \\| `null` |\n| <a id=\"property-time\"></a> `time` | [`EventTime`](../../../../../generated/calendar/types.gen/type-aliases/EventTime.md) |\n| <a id=\"property-title\"></a> `title` | `string` |\n\n\n<!-- src/entities/agent-sessions/review/type-aliases/ReviewEmailDraft.md -->\n# Type Alias: ReviewEmailDraft\n\n> **ReviewEmailDraft** = \\{ `attachments?`: \\{ `documentId`: `string`; \\}[]; `bcc?`: \\{ `email`: `string`; `name?`: `string` \\| `null`; \\}[]; `body`: `string`; `cc?`: \\{ `email`: `string`; `name?`: `string` \\| `null`; \\}[]; `includeSignature?`: `boolean` \\| `null`; `replyingToId?`: `string` \\| `null`; `subject`: `string`; `to`: \\{ `email`: `string`; `name?`: `string` \\| `null`; \\}[]; \\}\n\nMarkdown is rendered by the ordinary email action; the UI can edit each draft.\n\n## Properties\n\n| Property | Type |\n| ------ | ------ |\n| <a id=\"property-attachments\"></a> `attachments?` | \\{ `documentId`: `string`; \\}[] |\n| <a id=\"property-bcc\"></a> `bcc?` | \\{ `email`: `string`; `name?`: `string` \\| `null`; \\}[] |\n| <a id=\"property-body\"></a> `body` | `string` |\n| <a id=\"property-cc\"></a> `cc?` | \\{ `email`: `string`; `name?`: `string` \\| `null`; \\}[] |\n| <a id=\"property-includesignature\"></a> `includeSignature?` | `boolean` \\| `null` |\n| <a id=\"property-replyingtoid\"></a> `replyingToId?` | `string` \\| `null` |\n| <a id=\"property-subject\"></a> `subject` | `string` |\n| <a id=\"property-to\"></a> `to` | \\{ `email`: `string`; `name?`: `string` \\| `null`; \\}[] |\n\n\n<!-- src/entities/agent-sessions/tool-approval/classes/ToolApproval.md -->\n# Class: ToolApproval\n\nA permission request scoped to its originating agent session.\n\n## Properties\n\n| Property | Modifier | Type | Description |\n| ------ | ------ | ------ | ------ |\n| <a id=\"property-id\"></a> `id` | `readonly` | `string` | Approval identity. |\n| <a id=\"property-session\"></a> `session` | `readonly` | [`AgentSession`](../../agent-session/classes/AgentSession.md) | Owning session. |\n\n## Methods\n\n### answer()\n\n> **answer**(`answer`): `Promise`\\<[`AnswerToolApprovalResponse`](../../../../../generated/agent-harness/types.gen/type-aliases/AnswerToolApprovalResponse.md)\\>\n\nAnswer the approval using the generated discriminated answer contract.\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `answer` | [`ToolApprovalAnswerDto`](../../../../../generated/agent-harness/types.gen/type-aliases/ToolApprovalAnswerDto.md) |\n\n#### Returns\n\n`Promise`\\<[`AnswerToolApprovalResponse`](../../../../../generated/agent-harness/types.gen/type-aliases/AnswerToolApprovalResponse.md)\\>\n\n***\n\n### toJSON()\n\n> **toJSON**(): \\{ `id`: `string`; `sessionId`: `string`; \\}\n\nSerialize scoped identity only.\n\n#### Returns\n\n\\{ `id`: `string`; `sessionId`: `string`; \\}\n\n| Name | Type |\n| ------ | ------ |\n| `id` | `string` |\n| `sessionId` | `string` |\n\n***\n\n### byId()\n\n> `static` **byId**(`client`, `session`, `id`): `ToolApproval`\n\nConstruct a scoped approval handle.\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `client` | `MacroClient` |\n| `session` | [`AgentSession`](../../agent-session/classes/AgentSession.md) |\n| `id` | `string` |\n\n#### Returns\n\n`ToolApproval`\n"+ "documentation": "# Macro SDK: agentSessions\n\nGenerated by TypeDoc. Paths below refer to the canonical Markdown hierarchy. Other entity handles are documented in their named section; request core for shared SDK helpers.\n\n<!-- generated/agent-harness/types.gen/type-aliases/AccessLevel.md -->\n# Type Alias: AccessLevel\n\n> **AccessLevel** = `\"view\"` \\| `\"comment\"` \\| `\"edit\"` \\| `\"owner\"`\n\nOrdered from least to most access top -> bottom\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/AgentAction.md -->\n# Type Alias: AgentAction\n\n> **AgentAction** = [`AgentPromptAction`](AgentPromptAction.md) & \\{ `type`: `\"prompt\"`; \\} \\| [`AgentSetModelAction`](AgentSetModelAction.md) & \\{ `type`: `\"setModel\"`; \\} \\| [`AgentSetConfigOptionAction`](AgentSetConfigOptionAction.md) & \\{ `type`: `\"setConfigOption\"`; \\} \\| \\{ `type`: `\"compact\"`; \\} \\| \\{ `type`: `\"stop\"`; \\} \\| [`AgentRespondElicitationAction`](AgentRespondElicitationAction.md) & \\{ `type`: `\"respondElicitation\"`; \\} \\| [`AgentPermissionAction`](AgentPermissionAction.md) & \\{ `type`: `\"respondToPermission\"`; \\}\n\nOne thing a caller wants an agent to do.\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/AgentActionId.md -->\n# Type Alias: AgentActionId\n\n> **AgentActionId** = `string`\n\nIdentifies one accepted [`AgentAction`] end to end: returned by the\ncontrol endpoint, written as the JSON-RPC request id on the action's wire\nframe, and read back off that frame as `request_id` on the folded message\nit derives.\n\nA v7 uuid, so ids sort by mint time. Minted by the server at accept time,\nor by a client that speculated the action and named it in the control\nrequest - either way the server is the only writer of runtime-bound\nframes, so a uuid-shaped request id remains the whole ownership test. The\nmachine's own handshake request ids (`agent_session:{session}:{n}`) are\nnot uuids and stay `None`.\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/AgentConversationDto.md -->\n# Type Alias: AgentConversationDto\n\n> **AgentConversationDto** = \\{ `available`: `boolean`; `avatarUrl?`: `string` \\| `null`; `botId`: `string`; `channelId`: `string`; `name`: `string`; `sessions`: [`AgentConversationSessionDto`](AgentConversationSessionDto.md)[]; `settingsChanged`: `boolean`; `turns`: [`AgentConversationTurnDto`](AgentConversationTurnDto.md)[]; \\}\n\nAn agent's conversation in a channel, as the person it is with sees it.\n\n## Properties\n\n| Property | Type | Description |\n| ------ | ------ | ------ |\n| <a id=\"property-available\"></a> `available` | `boolean` | Whether the caller may currently send a new prompt. |\n| <a id=\"property-avatarurl\"></a> `avatarUrl?` | `string` \\| `null` | Optional persona avatar. |\n| <a id=\"property-botid\"></a> `botId` | `string` | Persona id (without the bot principal prefix). |\n| <a id=\"property-channelid\"></a> `channelId` | `string` | The channel containing the visible transcript. |\n| <a id=\"property-name\"></a> `name` | `string` | Persona's historical display name. |\n| <a id=\"property-sessions\"></a> `sessions` | [`AgentConversationSessionDto`](AgentConversationSessionDto.md)[] | The sessions the conversation has run on, oldest first. |\n| <a id=\"property-settingschanged\"></a> `settingsChanged` | `boolean` | Whether new persona settings are available to adopt explicitly. |\n| <a id=\"property-turns\"></a> `turns` | [`AgentConversationTurnDto`](AgentConversationTurnDto.md)[] | Persistent state of each accepted message and its current attempt. |\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/AgentConversationSessionDto.md -->\n# Type Alias: AgentConversationSessionDto\n\n> **AgentConversationSessionDto** = \\{ `createdAt`: `string`; `isCurrent`: `boolean`; `sessionId`: `string`; \\}\n\nOne session a conversation has run on; every channel message stays visible.\n\n## Properties\n\n| Property | Type | Description |\n| ------ | ------ | ------ |\n| <a id=\"property-createdat\"></a> `createdAt` | `string` | When the session began. |\n| <a id=\"property-iscurrent\"></a> `isCurrent` | `boolean` | Whether this is the current agent context. |\n| <a id=\"property-sessionid\"></a> `sessionId` | `string` | Session whose controls and live events belong to this context. |\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/AgentConversationTurnDto.md -->\n# Type Alias: AgentConversationTurnDto\n\n> **AgentConversationTurnDto** = \\{ `actionId`: `string`; `createdAt`: `string`; `replyMessageId?`: `string` \\| `null`; `retryable`: `boolean`; `sessionId`: `string`; `sourceMessageId`: `string`; `state`: [`ConversationTurnState`](ConversationTurnState.md); \\}\n\nExecution status without exposing the persona prompt or stored credentials.\n\n## Properties\n\n| Property | Type | Description |\n| ------ | ------ | ------ |\n| <a id=\"property-actionid\"></a> `actionId` | `string` | Current runtime attempt identity. |\n| <a id=\"property-createdat\"></a> `createdAt` | `string` | When the source message was first accepted. |\n| <a id=\"property-replymessageid\"></a> `replyMessageId?` | `string` \\| `null` | The associated agent reply, if it has been posted. |\n| <a id=\"property-retryable\"></a> `retryable` | `boolean` | Whether Retry would be accepted now. A failed attempt is not retryable until its reply says how it ended. |\n| <a id=\"property-sessionid\"></a> `sessionId` | `string` | Session carrying this turn. |\n| <a id=\"property-sourcemessageid\"></a> `sourceMessageId` | `string` | The user's message in the channel timeline. |\n| <a id=\"property-state\"></a> `state` | [`ConversationTurnState`](ConversationTurnState.md) | Durable execution state. |\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/AgentPermissionAction.md -->\n# Type Alias: AgentPermissionAction\n\n> **AgentPermissionAction** = \\{ `answer`: [`PermissionAnswer`](PermissionAnswer.md); `requestId`: `unknown`; \\}\n\nA user's answer to the agent's `session/request_permission`.\n\nUnlike every other action this is not a new request but the reply to one\nthe agent made, so it carries the agent's own request id rather than\nreceiving a server-minted one.\n\n## Properties\n\n| Property | Type | Description |\n| ------ | ------ | ------ |\n| <a id=\"property-answer\"></a> `answer` | [`PermissionAnswer`](PermissionAnswer.md) | What the user decided. |\n| <a id=\"property-requestid\"></a> `requestId` | `unknown` | The agent's JSON-RPC request id, echoed verbatim from the folded permission part. A string or a number on the wire; agents mint both, and `7` does not answer `\"7\"`. |\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/AgentPromptAction.md -->\n# Type Alias: AgentPromptAction\n\n> **AgentPromptAction** = \\{ `attachments?`: [`PromptAttachment`](PromptAttachment.md)[]; `prompt`: `string`; \\}\n\nAsk the agent to work on something.\n\n## Properties\n\n| Property | Type | Description |\n| ------ | ------ | ------ |\n| <a id=\"property-attachments\"></a> `attachments?` | [`PromptAttachment`](PromptAttachment.md)[] | Files the prompt refers to, in the order the user attached them. Delivered after the text as one `resource_link` block each. |\n| <a id=\"property-prompt\"></a> `prompt` | `string` | What to tell the agent. |\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/AgentRespondElicitationAction.md -->\n# Type Alias: AgentRespondElicitationAction\n\n> **AgentRespondElicitationAction** = [`ElicitationAnswer`](ElicitationAnswer.md) & \\{ `requestId`: [`ElicitationRequestId`](ElicitationRequestId.md); \\}\n\nAnswer an elicitation the agent is waiting on.\n\n## Type Declaration\n\n| Name | Type | Description |\n| ------ | ------ | ------ |\n| `requestId` | [`ElicitationRequestId`](ElicitationRequestId.md) | The agent's `elicitation/create` request id - not an [`AgentActionId`], because the agent minted it. |\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/AgentSessionChangesResponse.md -->\n# Type Alias: AgentSessionChangesResponse\n\n> **AgentSessionChangesResponse** = \\{ `attempt?`: `null` \\| [`CaptureAttemptDto`](CaptureAttemptDto.md); `capturing`: `boolean`; `changeset?`: `null` \\| [`ChangesetDto`](ChangesetDto.md); \\}\n\nResponse body for `GET /agent-sessions/{session_id}/changes`.\n\nClients deserialize this, so both derives are used.\n\n## Properties\n\n| Property | Type | Description |\n| ------ | ------ | ------ |\n| <a id=\"property-attempt\"></a> `attempt?` | `null` \\| [`CaptureAttemptDto`](CaptureAttemptDto.md) | - |\n| <a id=\"property-capturing\"></a> `capturing` | `boolean` | A capture is running right now. |\n| <a id=\"property-changeset\"></a> `changeset?` | `null` \\| [`ChangesetDto`](ChangesetDto.md) | - |\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/AgentSessionLogEntryDto.md -->\n# Type Alias: AgentSessionLogEntryDto\n\n> **AgentSessionLogEntryDto** = [`LogFrameDto`](LogFrameDto.md) & \\{ `createdAt`: `string`; `id`: `string`; `userId?`: `string` \\| `null`; \\}\n\nOne entry of a session's protocol log.\n\nSerializes as `{\"userId\": ..., \"direction\": ..., \"content\": ...}` - the\nframe's own two fields, flattened in beside the attribution, which is the\nsame shape a recorded session's JSONL carries. A reader can deserialize the\n`direction`/`content` pair straight back into the fold's own log type\nrather than through a transport vocabulary of its own.\n\n`agentSessionId` is not repeated per entry: every entry in a response\nbelongs to the session named once at the top.\n\n`Deserialize` is for the wire-contract tests only - nothing server-side\ndecodes its own response type.\n\n## Type Declaration\n\n| Name | Type | Description |\n| ------ | ------ | ------ |\n| `createdAt` | `string` | When the log recorded the frame. The frame itself carries no time, so this comes from the log row. It is what a reader has to order these against anything else it is showing beside them - the fold derives an order among the messages of one session and nothing more. |\n| `id` | `string` | Durable transport row identity; together with `createdAt`, its order cursor. |\n| `userId?` | `string` \\| `null` | The user whose action produced the frame, absent when no user did. Only prompts carry one, and only when the frame was attributed at the time - a replayed or recorded session's are anonymous. |\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/AgentSessionLogResponse.md -->\n# Type Alias: AgentSessionLogResponse\n\n> **AgentSessionLogResponse** = \\{ `bot`: [`SessionBot`](SessionBot.md); `entries`: [`AgentSessionLogEntryDto`](AgentSessionLogEntryDto.md)[]; \\}\n\nResponse body for one session's raw protocol log.\n\nA wrapper rather than a bare array so that anything which is about the\nresponse rather than about a frame has somewhere to go later without\nbreaking every client.\n\n## Properties\n\n| Property | Type | Description |\n| ------ | ------ | ------ |\n| <a id=\"property-bot\"></a> `bot` | [`SessionBot`](SessionBot.md) | The agent whose messages the log derives. Here because a client renders those messages and cannot otherwise work out who sent them: the sender of an agent message is this session's bot, and nothing else names it. |\n| <a id=\"property-entries\"></a> `entries` | [`AgentSessionLogEntryDto`](AgentSessionLogEntryDto.md)[] | Effective history in ascending `(createdAt, id)` order. The first row is the inclusive history boundary cursor: buffered rows before it are obsolete. Reconcile snapshot overlap by row ID, never content. An empty history has no boundary or overlapping rows. |\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/AgentSessionResponse.md -->\n# Type Alias: AgentSessionResponse\n\n> **AgentSessionResponse** = \\{ `acpSessionId?`: `string` \\| `null`; `botId`: `string`; `canEdit`: `boolean`; `createdAt`: `string`; `external?`: `null` \\| [`ExternalSessionResponse`](ExternalSessionResponse.md); `harness`: `string`; `id`: `string`; `instructions?`: `string` \\| `null`; `isArchived`: `boolean`; `model`: `string`; `modifiedAt`: `string`; `name`: `string`; `originatingMessageId?`: `string` \\| `null`; `ownerId`: `string`; `pullRequestUrl?`: `string` \\| `null`; `repoUrl?`: `string` \\| `null`; `sandboxSize`: [`SandboxSize`](SandboxSize.md); `status`: [`SessionStatusDto`](SessionStatusDto.md); `taskId?`: `string` \\| `null`; `threadChannelId?`: `string` \\| `null`; `threadId?`: `string` \\| `null`; `threadParent?`: `null` \\| [`MessageParent`](MessageParent.md); `workspace`: `string`; \\}\n\nResponse body describing an agent session.\n\nClients deserialize this, so both derives are used.\n\n## Properties\n\n| Property | Type | Description |\n| ------ | ------ | ------ |\n| <a id=\"property-acpsessionid\"></a> `acpSessionId?` | `string` \\| `null` | The ACP session id, if one exists. |\n| <a id=\"property-botid\"></a> `botId` | `string` | The bot running the agent. |\n| <a id=\"property-canedit\"></a> `canEdit` | `boolean` | Whether the caller may drive the session - prompt it, answer its questions, stop it - rather than only watch. Edit access; the creator owns the session, so a create response always says so. |\n| <a id=\"property-createdat\"></a> `createdAt` | `string` | When the session was created. |\n| <a id=\"property-external\"></a> `external?` | `null` \\| [`ExternalSessionResponse`](ExternalSessionResponse.md) | - |\n| <a id=\"property-harness\"></a> `harness` | `string` | Harness slug. |\n| <a id=\"property-id\"></a> `id` | `string` | The session id. |\n| <a id=\"property-instructions\"></a> `instructions?` | `string` \\| `null` | Instructions the session's runtime works under, when any were stated at creation. Absent otherwise, so existing payloads are unchanged. |\n| <a id=\"property-isarchived\"></a> `isArchived` | `boolean` | Whether the session is archived and read-only. |\n| <a id=\"property-model\"></a> `model` | `string` | Model slug. |\n| <a id=\"property-modifiedat\"></a> `modifiedAt` | `string` | When the session was last modified. |\n| <a id=\"property-name\"></a> `name` | `string` | User-facing session name. |\n| <a id=\"property-originatingmessageid\"></a> `originatingMessageId?` | `string` \\| `null` | The exact message that invoked the bot, if any. |\n| <a id=\"property-ownerid\"></a> `ownerId` | `string` | The user who created and owns the session. |\n| <a id=\"property-pullrequesturl\"></a> `pullRequestUrl?` | `string` \\| `null` | The session's linked pull request. |\n| <a id=\"property-repourl\"></a> `repoUrl?` | `string` \\| `null` | The repository the session works with, when one was stated. |\n| <a id=\"property-sandboxsize\"></a> `sandboxSize` | [`SandboxSize`](SandboxSize.md) | Compute tier of the managed sandbox. |\n| <a id=\"property-status\"></a> `status` | [`SessionStatusDto`](SessionStatusDto.md) | The session's status. |\n| <a id=\"property-taskid\"></a> `taskId?` | `string` \\| `null` | The Macro task the session was linked to. |\n| <a id=\"property-threadchannelid\"></a> `threadChannelId?` | `string` \\| `null` | The channel `thread_id` lives in, when the session was spawned from a channel thread. Derived from `thread_parent`. |\n| <a id=\"property-threadid\"></a> `threadId?` | `string` \\| `null` | The root message of the thread the session was created from, if any. |\n| <a id=\"property-threadparent\"></a> `threadParent?` | `null` \\| [`MessageParent`](MessageParent.md) | - |\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/AgentSetConfigOptionAction.md -->\n# Type Alias: AgentSetConfigOptionAction\n\n> **AgentSetConfigOptionAction** = \\{ `configId`: `string`; `value`: `string`; \\}\n\nAsk the agent to change one advertised select-style session setting.\n\n## Properties\n\n| Property | Type | Description |\n| ------ | ------ | ------ |\n| <a id=\"property-configid\"></a> `configId` | `string` | Opaque ACP config id advertised by the agent. |\n| <a id=\"property-value\"></a> `value` | `string` | Opaque select value advertised for that config option. |\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/AgentSetModelAction.md -->\n# Type Alias: AgentSetModelAction\n\n> **AgentSetModelAction** = \\{ `model`: `string`; \\}\n\nAsk the agent to run on a different model from here on.\n\n## Properties\n\n| Property | Type | Description |\n| ------ | ------ | ------ |\n| <a id=\"property-model\"></a> `model` | `string` | The model slug the agent should switch to. |\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/AnswerToolApprovalResponse.md -->\n# Type Alias: AnswerToolApprovalResponse\n\n> **AnswerToolApprovalResponse** = \\{ `status`: [`ToolApprovalStatusDto`](ToolApprovalStatusDto.md); \\}\n\nResponse body for answering a held tool call.\n\n## Properties\n\n| Property | Type | Description |\n| ------ | ------ | ------ |\n| <a id=\"property-status\"></a> `status` | [`ToolApprovalStatusDto`](ToolApprovalStatusDto.md) | Where the call stands now. |\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/BotId.md -->\n# Type Alias: BotId\n\n> **BotId** = `string`\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/CaptureAttemptDto.md -->\n# Type Alias: CaptureAttemptDto\n\n> **CaptureAttemptDto** = \\{ `error?`: `string` \\| `null`; `finishedAt?`: `string` \\| `null`; `outcome?`: `null` \\| [`CaptureOutcomeDto`](CaptureOutcomeDto.md); `startedAt`: `string`; \\}\n\nThe latest capture attempt.\n\nClients deserialize this, so both derives are used.\n\n## Properties\n\n| Property | Type | Description |\n| ------ | ------ | ------ |\n| <a id=\"property-error\"></a> `error?` | `string` \\| `null` | Why it did not capture, in a sentence the user can read. |\n| <a id=\"property-finishedat\"></a> `finishedAt?` | `string` \\| `null` | When it ended; absent while it runs. |\n| <a id=\"property-outcome\"></a> `outcome?` | `null` \\| [`CaptureOutcomeDto`](CaptureOutcomeDto.md) | - |\n| <a id=\"property-startedat\"></a> `startedAt` | `string` | When it started. |\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/CaptureOutcomeDto.md -->\n# Type Alias: CaptureOutcomeDto\n\n> **CaptureOutcomeDto** = `\"captured\"` \\| `\"not_ready\"` \\| `\"failed\"`\n\nHow the latest capture attempt ended, on the wire.\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/ChangedFileDto.md -->\n# Type Alias: ChangedFileDto\n\n> **ChangedFileDto** = \\{ `additions`: `number`; `binary`: `boolean`; `deletions`: `number`; `kind`: [`FileChangeKindDto`](FileChangeKindDto.md); `patchOmitted`: `boolean`; `path`: `string`; `previousPath?`: `string` \\| `null`; \\}\n\nOne changed file.\n\nClients deserialize this, so both derives are used.\n\n## Properties\n\n| Property | Type | Description |\n| ------ | ------ | ------ |\n| <a id=\"property-additions\"></a> `additions` | `number` | Lines added. |\n| <a id=\"property-binary\"></a> `binary` | `boolean` | The diff carries no text for this file. |\n| <a id=\"property-deletions\"></a> `deletions` | `number` | Lines removed. |\n| <a id=\"property-kind\"></a> `kind` | [`FileChangeKindDto`](FileChangeKindDto.md) | What happened to the file. |\n| <a id=\"property-patchomitted\"></a> `patchOmitted` | `boolean` | The file's hunks were left out of the patch to fit the size budget. |\n| <a id=\"property-path\"></a> `path` | `string` | The file's path after the change, or before it for a deletion. |\n| <a id=\"property-previouspath\"></a> `previousPath?` | `string` \\| `null` | Where a renamed file came from. |\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/ChangesetDto.md -->\n# Type Alias: ChangesetDto\n\n> **ChangesetDto** = \\{ `additions`: `number`; `base`: [`GitRefDto`](GitRefDto.md); `capturedAt`: `string`; `deletions`: `number`; `files`: [`ChangedFileDto`](ChangedFileDto.md)[]; `head`: [`GitRefDto`](GitRefDto.md); `id`: `string`; `patchBytes`: `number`; `repository?`: `string` \\| `null`; `source`: [`ChangesetSourceDto`](ChangesetSourceDto.md); `truncated`: `boolean`; \\}\n\nOne changeset: the files a patch touches and what happened to each.\n\nClients deserialize this, so both derives are used.\n\n## Properties\n\n| Property | Type | Description |\n| ------ | ------ | ------ |\n| <a id=\"property-additions\"></a> `additions` | `number` | Lines added across all files. |\n| <a id=\"property-base\"></a> `base` | [`GitRefDto`](GitRefDto.md) | The side the work started from. |\n| <a id=\"property-capturedat\"></a> `capturedAt` | `string` | When the diff was taken. |\n| <a id=\"property-deletions\"></a> `deletions` | `number` | Lines removed across all files. |\n| <a id=\"property-files\"></a> `files` | [`ChangedFileDto`](ChangedFileDto.md)[] | Every changed file, in patch order. |\n| <a id=\"property-head\"></a> `head` | [`GitRefDto`](GitRefDto.md) | The side carrying the work. |\n| <a id=\"property-id\"></a> `id` | `string` | The changeset's id; a different id means different changes. |\n| <a id=\"property-patchbytes\"></a> `patchBytes` | `number` | Size of the patch the matching patch route serves; zero when nothing changed. |\n| <a id=\"property-repository\"></a> `repository?` | `string` \\| `null` | `https://github.com/owner/name`, when known. |\n| <a id=\"property-source\"></a> `source` | [`ChangesetSourceDto`](ChangesetSourceDto.md) | Where the diff was read from. |\n| <a id=\"property-truncated\"></a> `truncated` | `boolean` | Some files' hunks were left out of the patch. |\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/ChangesetSourceDto.md -->\n# Type Alias: ChangesetSourceDto\n\n> **ChangesetSourceDto** = `\"github_pull_request\"`\n\nThe source of a changeset's diff, on the wire.\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/ChannelSharePermission.md -->\n# Type Alias: ChannelSharePermission\n\n> **ChannelSharePermission** = \\{ `access_level`: [`AccessLevel`](AccessLevel.md); `channel_id`: `string`; \\}\n\nThe channel share permission\n\n## Properties\n\n| Property | Type | Description |\n| ------ | ------ | ------ |\n| <a id=\"property-access_level\"></a> `access_level` | [`AccessLevel`](AccessLevel.md) | The access level for the channel |\n| <a id=\"property-channel_id\"></a> `channel_id` | `string` | The channel id |\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/CodingPreferencesBody.md -->\n# Type Alias: CodingPreferencesBody\n\n> **CodingPreferencesBody** = \\{ `createTasks`: `boolean`; `openPullRequests`: `boolean`; \\}\n\nRequest or response body for the caller's coding preferences.\n\n## Properties\n\n| Property | Type | Description |\n| ------ | ------ | ------ |\n| <a id=\"property-createtasks\"></a> `createTasks` | `boolean` | Whether new coding sessions research prior work and link a Macro task. |\n| <a id=\"property-openpullrequests\"></a> `openPullRequests` | `boolean` | Whether new coding sessions deliver their work as a pull request. |\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/ControlResponse.md -->\n# Type Alias: ControlResponse\n\n> **ControlResponse** = \\{ `actionId`: [`AgentActionId`](AgentActionId.md); `status`: [`ControlStatusDto`](ControlStatusDto.md); \\}\n\nResponse body for a control operation.\n\nClients deserialize this, so both derives are used.\n\n## Properties\n\n| Property | Type | Description |\n| ------ | ------ | ------ |\n| <a id=\"property-actionid\"></a> `actionId` | [`AgentActionId`](AgentActionId.md) | Matches `requestId` on the folded message this action derives once it dispatches, and names the queue entry until then. The caller's own `actionId` when it supplied one; a freshly minted id otherwise. |\n| <a id=\"property-status\"></a> `status` | [`ControlStatusDto`](ControlStatusDto.md) | Whether the action went out or waits in the queue. |\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/ControlStatusDto.md -->\n# Type Alias: ControlStatusDto\n\n> **ControlStatusDto** = `\"sent\"` \\| `\"queued\"`\n\nWhat accepting a control operation did with it, on the wire.\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/ConversationTurnState.md -->\n# Type Alias: ConversationTurnState\n\n> **ConversationTurnState** = `\"queued\"` \\| `\"running\"` \\| `\"succeeded\"` \\| `\"failed\"` \\| `\"stopped\"` \\| `\"interrupted\"`\n\nThe state of one source message, retained after completion for deduplication.\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/DecisionVia.md -->\n# Type Alias: DecisionVia\n\n> **DecisionVia** = `\"card\"` \\| `\"chat\"` \\| `\"inbox\"`\n\nSurface of a review decision.\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/DocumentId.md -->\n# Type Alias: DocumentId\n\n> **DocumentId** = `string`\n\nA validated document identifier. Historical document ids need not be UUIDs.\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/ElicitationAnswer.md -->\n# Type Alias: ElicitationAnswer\n\n> **ElicitationAnswer** = \\{ `action`: `\"accept\"`; `content?`: \\{\\[`key`: `string`\\]: [`ElicitationContentValue`](ElicitationContentValue.md); \\} \\| `null`; \\} \\| \\{ `action`: `\"decline\"`; \\} \\| \\{ `action`: `\"cancel\"`; \\}\n\nWhat the user decided about an elicitation. Mirrors ACP's three actions;\nthere is no `Other` because we never originate an action we do not know.\n\n## Union Members\n\n### Type Literal\n\n\\{ `action`: `\"accept\"`; `content?`: \\{\\[`key`: `string`\\]: [`ElicitationContentValue`](ElicitationContentValue.md); \\} \\| `null`; \\}\n\n| Name | Type | Description |\n| ------ | ------ | ------ |\n| `action` | `\"accept\"` | - |\n| `content?` | \\{\\[`key`: `string`\\]: [`ElicitationContentValue`](ElicitationContentValue.md); \\} \\| `null` | Form: the submitted values keyed by property. URL: omitted. |\n\n***\n\n### Type Literal\n\n\\{ `action`: `\"decline\"`; \\}\n\n***\n\n### Type Literal\n\n\\{ `action`: `\"cancel\"`; \\}\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/ElicitationContentValue.md -->\n# Type Alias: ElicitationContentValue\n\n> **ElicitationContentValue** = `string` \\| `boolean` \\| `number` \\| `number` \\| `string`[]\n\nA value ACP accepts in an elicitation answer.\n\nMirrors ACP's `ElicitationContentValue` so that the contract a caller\nanswers against is the closed union ACP will accept, rather than arbitrary\nJSON narrowed on the way out. An object, a null, or a mixed array is\nrefused when the request is deserialized - where the caller learns of it -\ninstead of at send time, when the elicitation slot has already been\nreleased.\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/ElicitationRequestId.md -->\n# Type Alias: ElicitationRequestId\n\n> **ElicitationRequestId** = `number` \\| `string`\n\nThe JSON-RPC id of an agent's `elicitation/create` request, carried whole\nso the answer echoes exactly what the agent sent.\n\nAgents pick these, not us: Claude Code counts from `0`, others use\nstrings. `null` is not a legal id for a request that expects a response,\nso it is not representable here.\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/ExecutionActivity.md -->\n# Type Alias: ExecutionActivity\n\n> **ExecutionActivity** = \\{ `id`: `number`; `label`: `string`; `status`: [`RecordedCallStatus`](RecordedCallStatus.md); \\}\n\nA bounded SDK operation observed during execution; not an authorization or write receipt.\n\n## Properties\n\n| Property | Type | Description |\n| ------ | ------ | ------ |\n| <a id=\"property-id\"></a> `id` | `number` | Request identity within the execution. |\n| <a id=\"property-label\"></a> `label` | `string` | Server-selected label from the generated operation catalog. |\n| <a id=\"property-status\"></a> `status` | [`RecordedCallStatus`](RecordedCallStatus.md) | Observed request state; unfinished operations have unknown outcomes. |\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/ExecutionId.md -->\n# Type Alias: ExecutionId\n\n> **ExecutionId** = `string`\n\nCaller-selected UUID persisted in tool input before execution begins.\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/ExecutionRecord.md -->\n# Type Alias: ExecutionRecord\n\n> **ExecutionRecord** = \\{ `actingAs?`: `null` \\| [`ToolActor`](ToolActor.md); `activity?`: [`ExecutionActivity`](ExecutionActivity.md)[]; `activityTruncated?`: `boolean`; `calls`: [`RecordedToolCall`](RecordedToolCall.md)[]; `error?`: `string` \\| `null`; `executionId`: [`ExecutionId`](ExecutionId.md); `result?`: `unknown`; `source`: `string`; `status`: [`ExecutionStatus`](ExecutionStatus.md); `turnId?`: `string` \\| `null`; \\}\n\nDurable source and final outcome, with historical bridge calls when present.\nThe full journal is not included in the model's tool result.\n\n## Properties\n\n| Property | Type | Description |\n| ------ | ------ | ------ |\n| <a id=\"property-actingas\"></a> `actingAs?` | `null` \\| [`ToolActor`](ToolActor.md) | - |\n| <a id=\"property-activity\"></a> `activity?` | [`ExecutionActivity`](ExecutionActivity.md)[] | Advisory SDK operation activity, without arguments or response contents. |\n| <a id=\"property-activitytruncated\"></a> `activityTruncated?` | `boolean` | Additional request activity was omitted at the bounded observation limit. |\n| <a id=\"property-calls\"></a> `calls` | [`RecordedToolCall`](RecordedToolCall.md)[] | Historical bridge calls. Direct HTTP executions leave this empty. |\n| <a id=\"property-error\"></a> `error?` | `string` \\| `null` | Safe execution failure text. |\n| <a id=\"property-executionid\"></a> `executionId` | [`ExecutionId`](ExecutionId.md) | Execution identity. |\n| <a id=\"property-result\"></a> `result?` | `unknown` | JSON returned by the program, if successful. |\n| <a id=\"property-source\"></a> `source` | `string` | The submitted JavaScript module. |\n| <a id=\"property-status\"></a> `status` | [`ExecutionStatus`](ExecutionStatus.md) | Overall execution state. |\n| <a id=\"property-turnid\"></a> `turnId?` | `string` \\| `null` | Original turn, preventing another requester from replaying a saved result. |\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/ExecutionStatus.md -->\n# Type Alias: ExecutionStatus\n\n> **ExecutionStatus** = `\"running\"` \\| `\"succeeded\"` \\| `\"failed\"` \\| `\"cancelled\"` \\| `\"timed_out\"`\n\nThe recorded outcome of a program.\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/ExternalSessionResponse.md -->\n# Type Alias: ExternalSessionResponse\n\n> **ExternalSessionResponse** = \\{ `name?`: `string` \\| `null`; `provider`: `string`; `url?`: `string` \\| `null`; \\}\n\nThe provider-side identity of an externally-served session.\n\n## Properties\n\n| Property | Type | Description |\n| ------ | ------ | ------ |\n| <a id=\"property-name\"></a> `name?` | `string` \\| `null` | The provider's display name for the agent, when it reported one. |\n| <a id=\"property-provider\"></a> `provider` | `string` | Which provider serves the session, e.g. `cursor`. |\n| <a id=\"property-url\"></a> `url?` | `string` \\| `null` | The agent's page on the provider's site, for a client to link out to. |\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/FileChangeKindDto.md -->\n# Type Alias: FileChangeKindDto\n\n> **FileChangeKindDto** = `\"added\"` \\| `\"modified\"` \\| `\"deleted\"` \\| `\"renamed\"`\n\nWhat happened to a file, on the wire.\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/GitRefDto.md -->\n# Type Alias: GitRefDto\n\n> **GitRefDto** = \\{ `name?`: `string` \\| `null`; `sha?`: `string` \\| `null`; \\}\n\nOne end of the compared range.\n\n## Properties\n\n| Property | Type | Description |\n| ------ | ------ | ------ |\n| <a id=\"property-name\"></a> `name?` | `string` \\| `null` | The branch name, when known. |\n| <a id=\"property-sha\"></a> `sha?` | `string` \\| `null` | The commit, when known. |\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/LinkShare.md -->\n# Type Alias: LinkShare\n\n> **LinkShare** = `\"PUBLIC\"` \\| `\"TEAM\"`\n\nDefines who can access an item through its share link.\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/LogDirectionDto.md -->\n# Type Alias: LogDirectionDto\n\n> **LogDirectionDto** = `\"notification\"` \\| `\"to_server\"` \\| `\"to_runtime\"`\n\nWhich way a logged frame travelled, mirroring [`Message`]'s discriminant.\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/LogFrameDto.md -->\n# Type Alias: LogFrameDto\n\n> **LogFrameDto** = \\{ `content`: \\{\\[`key`: `string`\\]: `unknown`; \\}; `direction`: [`LogDirectionDto`](LogDirectionDto.md); \\}\n\nThe two fields [`AgentSessionLogEntryDto`] flattens in.\n\nSchema only. Nothing constructs one: the entry serializes through\n[`Message`], and this exists so the generated clients see `direction` and\n`content` as named fields instead of an open map. A hand-built copy could\ndrift from the fold's wire format, and the point of the endpoint is that it\ncannot - so this describes that format without being able to produce it.\n\n## Properties\n\n| Property | Type | Description |\n| ------ | ------ | ------ |\n| <a id=\"property-content\"></a> `content` | \\{\\[`key`: `string`\\]: `unknown`; \\} | The protocol envelope, verbatim. Opaque here: it is Agent Runtime Protocol, whose shape belongs to the fold rather than this endpoint. |\n| <a id=\"property-direction\"></a> `direction` | [`LogDirectionDto`](LogDirectionDto.md) | Which way the frame travelled. |\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/MessageParent.md -->\n# Type Alias: MessageParent\n\n> **MessageParent** = \\{ `id`: `string`; `type`: `\"channel\"`; \\} \\| \\{ `id`: [`DocumentId`](DocumentId.md); `type`: `\"document\"`; \\} \\| \\{ `id`: `string`; `type`: `\"initiative\"`; \\} \\| \\{ `id`: `string`; `type`: `\"crm_company\"`; \\} \\| \\{ `id`: `string`; `type`: `\"crm_contact\"`; \\} \\| \\{ `id`: `string`; `type`: `\"call\"`; \\}\n\nThe entity whose permissions and lifecycle govern a message.\n\n## Union Members\n\n### Type Literal\n\n\\{ `id`: `string`; `type`: `\"channel\"`; \\}\n\n| Name | Type | Description |\n| ------ | ------ | ------ |\n| `id` | `string` | A channel, including direct messages. |\n| `type` | `\"channel\"` | - |\n\n***\n\n### Type Literal\n\n\\{ `id`: [`DocumentId`](DocumentId.md); `type`: `\"document\"`; \\}\n\n| Name | Type | Description |\n| ------ | ------ | ------ |\n| `id` | [`DocumentId`](DocumentId.md) | A document, including tasks and PDFs. |\n| `type` | `\"document\"` | - |\n\n***\n\n### Type Literal\n\n\\{ `id`: `string`; `type`: `\"initiative\"`; \\}\n\n| Name | Type | Description |\n| ------ | ------ | ------ |\n| `id` | `string` | An initiative, presented as a project in the application. |\n| `type` | `\"initiative\"` | - |\n\n***\n\n### Type Literal\n\n\\{ `id`: `string`; `type`: `\"crm_company\"`; \\}\n\n| Name | Type | Description |\n| ------ | ------ | ------ |\n| `id` | `string` | A CRM company. |\n| `type` | `\"crm_company\"` | - |\n\n***\n\n### Type Literal\n\n\\{ `id`: `string`; `type`: `\"crm_contact\"`; \\}\n\n| Name | Type | Description |\n| ------ | ------ | ------ |\n| `id` | `string` | A CRM contact. |\n| `type` | `\"crm_contact\"` | - |\n\n***\n\n### Type Literal\n\n\\{ `id`: `string`; `type`: `\"call\"`; \\}\n\n| Name | Type | Description |\n| ------ | ------ | ------ |\n| `id` | `string` | A video call and its persistent chat thread. |\n| `type` | `\"call\"` | - |\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/PermissionAnswer.md -->\n# Type Alias: PermissionAnswer\n\n> **PermissionAnswer** = \\{ `kind`: `\"selected\"`; `optionId`: `string`; \\} \\| \\{ `kind`: `\"cancelled\"`; \\}\n\nThe decision carried by an [`AgentPermissionAction`].\n\n## Union Members\n\n### Type Literal\n\n\\{ `kind`: `\"selected\"`; `optionId`: `string`; \\}\n\n| Name | Type | Description |\n| ------ | ------ | ------ |\n| `kind` | `\"selected\"` | - |\n| `optionId` | `string` | The chosen option's id, as the agent listed it. |\n\n***\n\n### Type Literal\n\n\\{ `kind`: `\"cancelled\"`; \\}\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/PromptAttachment.md -->\n# Type Alias: PromptAttachment\n\n> **PromptAttachment** = \\{ `mimeType?`: `string` \\| `null`; `name`: `string`; `size?`: `number` \\| `null`; `uri`: `string`; \\}\n\nA file the prompt refers to, by where the agent can fetch it.\n\nMirrors ACP's `resource_link` content block, which every agent must\naccept: bytes never ride the prompt, only a URI (a static file service URL\nin practice) with enough metadata for a client to render a chip and for\nthe agent to decide whether to fetch it. Keeping the wire shape ACP-native\nmeans the harness translates without resolving anything, and the fold\nreads the same fields back off the logged frame.\n\n## Properties\n\n| Property | Type | Description |\n| ------ | ------ | ------ |\n| <a id=\"property-mimetype\"></a> `mimeType?` | `string` \\| `null` | The file's media type, when known. |\n| <a id=\"property-name\"></a> `name` | `string` | Display name, typically the original file name. |\n| <a id=\"property-size\"></a> `size?` | `number` \\| `null` | Size in bytes, when known. |\n| <a id=\"property-uri\"></a> `uri` | `string` | Where the agent can fetch the file. |\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/PullRequestLinkSource.md -->\n# Type Alias: PullRequestLinkSource\n\n> **PullRequestLinkSource** = `\"agent\"` \\| `\"user\"`\n\nWho associated a pull request with a session.\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/QueuedActionDto.md -->\n# Type Alias: QueuedActionDto\n\n> **QueuedActionDto** = \\{ `actionId`: [`AgentActionId`](AgentActionId.md); `actorUserId?`: `string` \\| `null`; `attachments?`: [`PromptAttachment`](PromptAttachment.md)[]; `createdAt`: `string`; `kind`: `string`; `prompt?`: `string` \\| `null`; \\}\n\nOne action waiting in a session's queue.\n\nClients deserialize this, so both derives are used.\n\n## Properties\n\n| Property | Type | Description |\n| ------ | ------ | ------ |\n| <a id=\"property-actionid\"></a> `actionId` | [`AgentActionId`](AgentActionId.md) | The id the action was accepted under. |\n| <a id=\"property-actoruserid\"></a> `actorUserId?` | `string` \\| `null` | The user who queued it, absent when a bot acted on nobody's behalf. |\n| <a id=\"property-attachments\"></a> `attachments?` | [`PromptAttachment`](PromptAttachment.md)[] | Files the prompt refers to, for prompts only. Kept through an edit, which replaces the text alone. |\n| <a id=\"property-createdat\"></a> `createdAt` | `string` | When it was accepted. |\n| <a id=\"property-kind\"></a> `kind` | `string` | What kind of action waits - `prompt` or `compact`; only turn-occupying actions are ever queued. |\n| <a id=\"property-prompt\"></a> `prompt?` | `string` \\| `null` | The prompt's raw text, present for prompts only. What an edit replaces. |\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/RecordedCallStatus.md -->\n# Type Alias: RecordedCallStatus\n\n> **RecordedCallStatus** = `\"running\"` \\| `\"completed\"` \\| `\"failed\"` \\| `\"unknown\"`\n\nA dispatch result; unfinished work is never reported as success.\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/RecordedToolCall.md -->\n# Type Alias: RecordedToolCall\n\n> **RecordedToolCall** = \\{ `error?`: `string` \\| `null`; `id`: `string`; `input`: `unknown`; `name`: `string`; `output?`: `unknown`; `outputOmitted`: `boolean`; `status`: [`RecordedCallStatus`](RecordedCallStatus.md); \\}\n\nA real inner call, recorded by trusted dispatch for the existing renderers.\n\n## Properties\n\n| Property | Type | Description |\n| ------ | ------ | ------ |\n| <a id=\"property-error\"></a> `error?` | `string` \\| `null` | Safe tool failure, if one was returned. |\n| <a id=\"property-id\"></a> `id` | `string` | Stable, execution-scoped component identity. |\n| <a id=\"property-input\"></a> `input` | `unknown` | Arguments passed to the actual tool. |\n| <a id=\"property-name\"></a> `name` | `string` | Canonical Macro tool name from the allowlisted registry. |\n| <a id=\"property-output\"></a> `output?` | `unknown` | Original tool output, retained independently of the program's return value. |\n| <a id=\"property-outputomitted\"></a> `outputOmitted` | `boolean` | Whether the response was omitted to keep the durable journal bounded. |\n| <a id=\"property-status\"></a> `status` | [`RecordedCallStatus`](RecordedCallStatus.md) | A started call that never replied has an unknown side-effect outcome. |\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/ReviewDecisionContext.md -->\n# Type Alias: ReviewDecisionContext\n\n> **ReviewDecisionContext** = \\{ `by`: `string`; `items`: `string`[]; `revision`: `number`; `via`: [`DecisionVia`](DecisionVia.md); \\}\n\nFacts about the most recent account-holder decision.\n\n## Properties\n\n| Property | Type | Description |\n| ------ | ------ | ------ |\n| <a id=\"property-by\"></a> `by` | `string` | Authenticated account holder. |\n| <a id=\"property-items\"></a> `items` | `string`[] | Items selected by this decision. |\n| <a id=\"property-revision\"></a> `revision` | `number` | Revision claimed by this batch, used in notification deduplication. |\n| <a id=\"property-via\"></a> `via` | [`DecisionVia`](DecisionVia.md) | Entry surface. |\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/ReviewId.md -->\n# Type Alias: ReviewId\n\n> **ReviewId** = `string`\n\nPersisted review identity.\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/ReviewItem.md -->\n# Type Alias: ReviewItem\n\n> **ReviewItem** = \\{ `draft`: `unknown`; `edited?`: `boolean`; `error?`: `string` \\| `null`; `id`: `string`; `result?`: `unknown`; `status`: [`ReviewItemStatus`](ReviewItemStatus.md); \\}\n\nOne editable item and its durable outcome.\n\n## Properties\n\n| Property | Type | Description |\n| ------ | ------ | ------ |\n| <a id=\"property-draft\"></a> `draft` | `unknown` | Validated arguments for the existing composer/action. |\n| <a id=\"property-edited\"></a> `edited?` | `boolean` | Whether the account holder edited this draft before deciding. |\n| <a id=\"property-error\"></a> `error?` | `string` \\| `null` | Failure or uncertainty description. |\n| <a id=\"property-id\"></a> `id` | `string` | Stable item identity across stack navigation and edits. |\n| <a id=\"property-result\"></a> `result?` | `unknown` | Actual backend response, never a model assertion. |\n| <a id=\"property-status\"></a> `status` | [`ReviewItemStatus`](ReviewItemStatus.md) | Current outcome. |\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/ReviewItemStatus.md -->\n# Type Alias: ReviewItemStatus\n\n> **ReviewItemStatus** = `\"pending\"` \\| `\"submitting\"` \\| `\"completed\"` \\| `\"dismissed\"` \\| `\"unknown\"`\n\nReview item state, shared with notification consumers.\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/ReviewKind.md -->\n# Type Alias: ReviewKind\n\n> **ReviewKind** = `\"email\"` \\| `\"calendar\"` \\| `\"import\"`\n\nThe component and action used to finish a review.\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/ReviewRecord.md -->\n# Type Alias: ReviewRecord\n\n> **ReviewRecord** = \\{ `actingUser?`: `string` \\| `null`; `decision?`: `null` \\| [`ReviewDecisionContext`](ReviewDecisionContext.md); `executionId?`: `null` \\| [`ExecutionId`](ExecutionId.md); `id`: [`ReviewId`](ReviewId.md); `items`: [`ReviewItem`](ReviewItem.md)[]; `kind`: [`ReviewKind`](ReviewKind.md); `presentedAt?`: `number` \\| `null`; `revision`: `number`; `sessionId`: `string`; `title`: `string`; `updatedAt`: `number`; \\}\n\nFull UI record, fetched outside the model context.\n\n## Properties\n\n| Property | Type | Description |\n| ------ | ------ | ------ |\n| <a id=\"property-actinguser\"></a> `actingUser?` | `string` \\| `null` | Account whose email/calendar the drafts use; historical records use the session owner. |\n| <a id=\"property-decision\"></a> `decision?` | `null` \\| [`ReviewDecisionContext`](ReviewDecisionContext.md) | - |\n| <a id=\"property-executionid\"></a> `executionId?` | `null` \\| [`ExecutionId`](ExecutionId.md) | - |\n| <a id=\"property-id\"></a> `id` | [`ReviewId`](ReviewId.md) | Review identity. |\n| <a id=\"property-items\"></a> `items` | [`ReviewItem`](ReviewItem.md)[] | Drafts and per-item outcomes. |\n| <a id=\"property-kind\"></a> `kind` | [`ReviewKind`](ReviewKind.md) | Component kind. |\n| <a id=\"property-presentedat\"></a> `presentedAt?` | `number` \\| `null` | Durable presentation acknowledgment, separate from user decisions. |\n| <a id=\"property-revision\"></a> `revision` | `number` | Compare-and-swap revision for edits and submissions. |\n| <a id=\"property-sessionid\"></a> `sessionId` | `string` | Owning session. |\n| <a id=\"property-title\"></a> `title` | `string` | Stack label. |\n| <a id=\"property-updatedat\"></a> `updatedAt` | `number` | Milliseconds since epoch of the last state change. |\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/SandboxSize.md -->\n# Type Alias: SandboxSize\n\n> **SandboxSize** = `\"small\"` \\| `\"default\"` \\| `\"large\"`\n\nNamed compute tier for a managed sandbox.\n\nThe API and database store the name. CPU, RAM, and disk live in\n`crates/agent_harness/sandbox_sizes.json`.\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/SessionBot.md -->\n# Type Alias: SessionBot\n\n> **SessionBot** = \\{ `avatarUrl?`: `string` \\| `null`; `handle`: `string`; `id`: [`BotId`](BotId.md); `name`: `string`; \\}\n\nThe agent behind a session, as much of it as rendering a message needs.\n\n## Properties\n\n| Property | Type | Description |\n| ------ | ------ | ------ |\n| <a id=\"property-avatarurl\"></a> `avatarUrl?` | `string` \\| `null` | Avatar, when it has one. |\n| <a id=\"property-handle\"></a> `handle` | `string` | Stable `@` handle, without a leading `@`. |\n| <a id=\"property-id\"></a> `id` | [`BotId`](BotId.md) | The bot's id. A message it sent has `\"bot\\|{id}\"` as its sender. |\n| <a id=\"property-name\"></a> `name` | `string` | Display name. |\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/SessionStatusDto.md -->\n# Type Alias: SessionStatusDto\n\n> **SessionStatusDto** = \\{ `kind`: `\"no_messages\"`; \\} \\| \\{ `event`: `string`; `kind`: `\"event\"`; \\} \\| \\{ `kind`: `\"disconnected\"`; \\}\n\nTransport representation of a session's status, mirroring\n[`SessionStatus`].\n\n## Union Members\n\n### Type Literal\n\n\\{ `kind`: `\"no_messages\"`; \\}\n\n***\n\n### Type Literal\n\n\\{ `event`: `string`; `kind`: `\"event\"`; \\}\n\n| Name | Type | Description |\n| ------ | ------ | ------ |\n| `event` | `string` | The wire name of the system event, e.g. `acp_ready`. |\n| `kind` | `\"event\"` | - |\n\n***\n\n### Type Literal\n\n\\{ `kind`: `\"disconnected\"`; \\}\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/SharePermissionV2.md -->\n# Type Alias: SharePermissionV2\n\n> **SharePermissionV2** = \\{ `channelSharePermissions?`: [`ChannelSharePermission`](ChannelSharePermission.md)[] \\| `null`; `id`: `string`; `linkShare?`: `null` \\| [`LinkShare`](LinkShare.md); `linkShareAccessLevel?`: `null` \\| [`AccessLevel`](AccessLevel.md); `owner`: `string`; `teamShareAccessLevel?`: `null` \\| [`AccessLevel`](AccessLevel.md); \\}\n\n## Properties\n\n| Property | Type | Description |\n| ------ | ------ | ------ |\n| <a id=\"property-channelsharepermissions\"></a> `channelSharePermissions?` | [`ChannelSharePermission`](ChannelSharePermission.md)[] \\| `null` | The channel share permissions for the item |\n| <a id=\"property-id\"></a> `id` | `string` | The share permission id |\n| <a id=\"property-linkshare\"></a> `linkShare?` | `null` \\| [`LinkShare`](LinkShare.md) | - |\n| <a id=\"property-linkshareaccesslevel\"></a> `linkShareAccessLevel?` | `null` \\| [`AccessLevel`](AccessLevel.md) | - |\n| <a id=\"property-owner\"></a> `owner` | `string` | The owner of the item |\n| <a id=\"property-teamshareaccesslevel\"></a> `teamShareAccessLevel?` | `null` \\| [`AccessLevel`](AccessLevel.md) | - |\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/ToolActor.md -->\n# Type Alias: ToolActor\n\n> **ToolActor** = `\"requester\"` \\| `\"owner\"`\n\nWhich authenticated participant's access a tool call requests.\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/ToolApprovalAnswerDto.md -->\n# Type Alias: ToolApprovalAnswerDto\n\n> **ToolApprovalAnswerDto** = `\"approve\"` \\| `\"approve_and_remember\"` \\| `\"deny\"` \\| `\"cancel\"`\n\nHow a person answers a held tool call.\n\n\n<!-- generated/agent-harness/types.gen/type-aliases/ToolApprovalStatusDto.md -->\n# Type Alias: ToolApprovalStatusDto\n\n> **ToolApprovalStatusDto** = `\"pending\"` \\| `\"approved\"` \\| `\"denied\"` \\| `\"cancelled\"` \\| `\"expired\"`\n\nWhere a held tool call stands once answered.\n\n\n<!-- generated/calendar/types.gen/type-aliases/EventTime.md -->\n# Type Alias: EventTime\n\n> **EventTime** = \\{ `endsAt`: `string`; `kind`: `\"timed\"`; `startsAt`: `string`; `timeZone?`: `string` \\| `null`; \\} \\| \\{ `endDate`: `string`; `kind`: `\"allDay\"`; `startDate`: `string`; \\}\n\nThe mutually exclusive time shape of a calendar event.\n\nFields are renamed per variant rather than with `rename_all_fields`\nbecause utoipa only honors variant-level serde renames when it\nderives the OpenAPI schema.\n\n## Union Members\n\n### Type Literal\n\n\\{ `endsAt`: `string`; `kind`: `\"timed\"`; `startsAt`: `string`; `timeZone?`: `string` \\| `null`; \\}\n\n| Name | Type | Description |\n| ------ | ------ | ------ |\n| `endsAt` | `string` | Exclusive end instant; equal to the start for an imported point event. |\n| `kind` | `\"timed\"` | - |\n| `startsAt` | `string` | Inclusive start instant. |\n| `timeZone?` | `string` \\| `null` | Original IANA time-zone identifier, when supplied. |\n\n***\n\n### Type Literal\n\n\\{ `endDate`: `string`; `kind`: `\"allDay\"`; `startDate`: `string`; \\}\n\n| Name | Type | Description |\n| ------ | ------ | ------ |\n| `endDate` | `string` | Exclusive local end date. |\n| `kind` | `\"allDay\"` | - |\n| `startDate` | `string` | Inclusive local start date. |\n\n\n<!-- src/entities/agent-sessions/agent-session/classes/AgentSession.md -->\n# Class: AgentSession\n\nA managed or externally hosted coding-agent session.\n\n## Extends\n\n- [`MacroEntity`](../../../entity/classes/MacroEntity.md)\\<[`AgentSessionResponse`](../../../../../generated/agent-harness/types.gen/type-aliases/AgentSessionResponse.md)\\>\n\n## Properties\n\n| Property | Modifier | Type | Description | Inherited from |\n| ------ | ------ | ------ | ------ | ------ |\n| <a id=\"property-access\"></a> `access` | `readonly` | [`EntityAccess`](../../../sharing/access/classes/EntityAccess.md) | Access requests and direct user grants. | - |\n| <a id=\"property-codeexecutions\"></a> `codeExecutions` | `readonly` | \\{ `byId`: (`id`) => [`CodeExecution`](../../code-execution/classes/CodeExecution.md); \\} | Saved code executions scoped to this session. Handle construction makes no request. | - |\n| `codeExecutions.byId` | `public` | (`id`) => [`CodeExecution`](../../code-execution/classes/CodeExecution.md) | - | - |\n| <a id=\"property-createdat\"></a> `createdAt` | `readonly` | () => `Promise`\\<`string`\\> | When the session was created. | - |\n| <a id=\"property-harness\"></a> `harness` | `readonly` | () => `Promise`\\<`string`\\> | The agent harness implementation serving the session. | - |\n| <a id=\"property-id\"></a> `id` | `public readonly` | `string` | - | [`MacroEntity`](../../../entity/classes/MacroEntity.md).[`id`](../../../entity/classes/MacroEntity.md#property-id) |\n| <a id=\"property-instructions\"></a> `instructions` | `readonly` | () => `Promise`\\<`string` \\| `undefined`\\> | Instructions the session's runtime works under, when any were stated at creation. Fixed for the session's life. | - |\n| <a id=\"property-isarchived\"></a> `isArchived` | `readonly` | () => `Promise`\\<`boolean`\\> | Whether the session is archived and read-only. | - |\n| <a id=\"property-model\"></a> `model` | `readonly` | () => `Promise`\\<`string`\\> | The model currently configured for the session. | - |\n| <a id=\"property-modifiedat\"></a> `modifiedAt` | `readonly` | () => `Promise`\\<`string`\\> | When the session was last modified. | - |\n| <a id=\"property-name\"></a> `name` | `readonly` | () => `Promise`\\<`string`\\> | The session's user-facing display name. | - |\n| <a id=\"property-repourl\"></a> `repoUrl` | `readonly` | () => `Promise`\\<`string` \\| `undefined`\\> | The repository the agent works with, when one was supplied. | - |\n| <a id=\"property-reviews\"></a> `reviews` | `readonly` | \\{ `byId`: (`id`) => [`SessionReview`](../../review/classes/SessionReview.md); `create`: (`input`) => `Promise`\\<[`SessionReview`](../../review/classes/SessionReview.md)\\>; \\} | Persisted reviews scoped to this session, with or without a code execution. | - |\n| `reviews.byId` | `public` | (`id`) => [`SessionReview`](../../review/classes/SessionReview.md) | - | - |\n| `reviews.create` | `public` | (`input`) => `Promise`\\<[`SessionReview`](../../review/classes/SessionReview.md)\\> | - | - |\n| <a id=\"property-sandboxsize\"></a> `sandboxSize` | `readonly` | () => `Promise`\\<[`SandboxSize`](../../../../../generated/agent-harness/types.gen/type-aliases/SandboxSize.md)\\> | Compute tier of the managed sandbox. | - |\n| <a id=\"property-status\"></a> `status` | `readonly` | () => `Promise`\\<[`SessionStatusDto`](../../../../../generated/agent-harness/types.gen/type-aliases/SessionStatusDto.md)\\> | The session's latest runtime status. | - |\n\n## Methods\n\n### archive()\n\n> **archive**(): `Promise`\\<`void`\\>\n\nArchive this session, making it read-only until unarchived.\n\n#### Returns\n\n`Promise`\\<`void`\\>\n\n***\n\n### changes()\n\n> **changes**(): `Promise`\\<[`AgentSessionChangesResponse`](../../../../../generated/agent-harness/types.gen/type-aliases/AgentSessionChangesResponse.md)\\>\n\nRead the latest captured GitHub pull request changes and capture status.\n\n#### Returns\n\n`Promise`\\<[`AgentSessionChangesResponse`](../../../../../generated/agent-harness/types.gen/type-aliases/AgentSessionChangesResponse.md)\\>\n\n***\n\n### changesPatch()\n\n> **changesPatch**(): `Promise`\\<`string`\\>\n\nRead the unified diff of the latest captured changeset.\n\n#### Returns\n\n`Promise`\\<`string`\\>\n\n***\n\n### control()\n\n> **control**(`action`): `Promise`\\<[`ControlResponse`](../../../../../generated/agent-harness/types.gen/type-aliases/ControlResponse.md)\\>\n\nSend a prompt or lifecycle operation to the live agent session.\n\nThe returned `actionId` matches `requestId` on the folded message the\naction derives once it dispatches. A `queued` status means a turn was\nrunning: the action waits in the session's queue ([queue](#queue)) and\ndispatches when that turn ends.\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `action` | [`AgentAction`](../../../../../generated/agent-harness/types.gen/type-aliases/AgentAction.md) |\n\n#### Returns\n\n`Promise`\\<[`ControlResponse`](../../../../../generated/agent-harness/types.gen/type-aliases/ControlResponse.md)\\>\n\n***\n\n### delete()\n\n> **delete**(): `Promise`\\<`void`\\>\n\nDelete this session and any live resources it owns.\n\n#### Returns\n\n`Promise`\\<`void`\\>\n\n***\n\n### foldedLog()\n\n> **foldedLog**(): `Promise`\\<[`FoldedAgentSessionLog`](../type-aliases/FoldedAgentSessionLog.md)\\>\n\nRead this session's whole history as the conversation the app renders:\neach user prompt and agent reply, oldest first, with tool calls and their\nresults, plans, permission requests and how each turn ended. Works on any\nsession the caller can view, live or finished, so it is the way to read\nwhat another agent session did. Prefer it to [log](#log), which returns\nraw protocol frames.\n\n#### Returns\n\n`Promise`\\<[`FoldedAgentSessionLog`](../type-aliases/FoldedAgentSessionLog.md)\\>\n\n***\n\n### linkPullRequest()\n\n> **linkPullRequest**(`url`): `Promise`\\<`void`\\>\n\nLink the GitHub pull request at `url` to this session. Linking one that\nis already linked changes nothing.\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `url` | `string` |\n\n#### Returns\n\n`Promise`\\<`void`\\>\n\n***\n\n### log()\n\n> **log**(): `Promise`\\<[`AgentSessionLogResponse`](../../../../../generated/agent-harness/types.gen/type-aliases/AgentSessionLogResponse.md)\\>\n\nRead the complete raw protocol log for this session.\n\n#### Returns\n\n`Promise`\\<[`AgentSessionLogResponse`](../../../../../generated/agent-harness/types.gen/type-aliases/AgentSessionLogResponse.md)\\>\n\n***\n\n### prompt()\n\n> **prompt**(`text`, `attachments?`): `Promise`\\<[`ControlResponse`](../../../../../generated/agent-harness/types.gen/type-aliases/ControlResponse.md)\\>\n\nSend a prompt to the session — sugar over [control](#control).\n\n`attachments` are files the prompt refers to, each by a URL the agent\ncan fetch (a static file service URL in practice); they reach the agent\nas ACP `resource_link` blocks after the text.\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `text` | `string` |\n| `attachments?` | [`PromptAttachment`](../../../../../generated/agent-harness/types.gen/type-aliases/PromptAttachment.md)[] |\n\n#### Returns\n\n`Promise`\\<[`ControlResponse`](../../../../../generated/agent-harness/types.gen/type-aliases/ControlResponse.md)\\>\n\n***\n\n### pullRequests()\n\n> **pullRequests**(): `Promise`\\<[`SessionPullRequest`](../type-aliases/SessionPullRequest.md)[]\\>\n\nThe GitHub pull requests linked to this session: the one its agent\nopened and any a person linked, oldest first.\n\n#### Returns\n\n`Promise`\\<[`SessionPullRequest`](../type-aliases/SessionPullRequest.md)[]\\>\n\n***\n\n### queue()\n\n> **queue**(): `Promise`\\<[`QueuedAction`](../../queued-action/classes/QueuedAction.md)[]\\>\n\nThe actions waiting to dispatch in this session, oldest first. Each can\nbe edited or removed until it dispatches.\n\n#### Returns\n\n`Promise`\\<[`QueuedAction`](../../queued-action/classes/QueuedAction.md)[]\\>\n\n***\n\n### refreshChanges()\n\n> **refreshChanges**(): `Promise`\\<[`AgentSessionChangesResponse`](../../../../../generated/agent-harness/types.gen/type-aliases/AgentSessionChangesResponse.md)\\>\n\nRequest a fresh capture and return the current state while it runs.\n\n#### Returns\n\n`Promise`\\<[`AgentSessionChangesResponse`](../../../../../generated/agent-harness/types.gen/type-aliases/AgentSessionChangesResponse.md)\\>\n\n***\n\n### rename()\n\n> **rename**(`name`): `Promise`\\<`void`\\>\n\nRename this session.\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `name` | `string` |\n\n#### Returns\n\n`Promise`\\<`void`\\>\n\n***\n\n### setSandboxSize()\n\n> **setSandboxSize**(`size`): `Promise`\\<[`SandboxSize`](../../../../../generated/agent-harness/types.gen/type-aliases/SandboxSize.md)\\>\n\nResize this session's sandbox and remember the size as the owner's default.\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `size` | [`SandboxSize`](../../../../../generated/agent-harness/types.gen/type-aliases/SandboxSize.md) |\n\n#### Returns\n\n`Promise`\\<[`SandboxSize`](../../../../../generated/agent-harness/types.gen/type-aliases/SandboxSize.md)\\>\n\n***\n\n### sharePermissions()\n\n> **sharePermissions**(): `Promise`\\<[`SharePermissionV2`](../../../../../generated/agent-harness/types.gen/type-aliases/SharePermissionV2.md)\\>\n\nSharing configuration.\n\n#### Returns\n\n`Promise`\\<[`SharePermissionV2`](../../../../../generated/agent-harness/types.gen/type-aliases/SharePermissionV2.md)\\>\n\n***\n\n### toJSON()\n\n> **toJSON**(): \\{ `detail?`: [`AgentSessionResponse`](../../../../../generated/agent-harness/types.gen/type-aliases/AgentSessionResponse.md); `id`: `string`; \\}\n\n#### Returns\n\n\\{ `detail?`: [`AgentSessionResponse`](../../../../../generated/agent-harness/types.gen/type-aliases/AgentSessionResponse.md); `id`: `string`; \\}\n\n| Name | Type |\n| ------ | ------ |\n| `detail?` | [`AgentSessionResponse`](../../../../../generated/agent-harness/types.gen/type-aliases/AgentSessionResponse.md) |\n| `id` | `string` |\n\n#### Inherited from\n\n[`MacroEntity`](../../../entity/classes/MacroEntity.md).[`toJSON`](../../../entity/classes/MacroEntity.md#tojson)\n\n***\n\n### toolApprovalById()\n\n> **toolApprovalById**(`id`): [`ToolApproval`](../../tool-approval/classes/ToolApproval.md)\n\nConstruct an approval handle scoped to this session.\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `id` | `string` |\n\n#### Returns\n\n[`ToolApproval`](../../tool-approval/classes/ToolApproval.md)\n\n***\n\n### unarchive()\n\n> **unarchive**(): `Promise`\\<`void`\\>\n\nRestore an archived session so it can be edited and prompted again.\n\n#### Returns\n\n`Promise`\\<`void`\\>\n\n***\n\n### unlinkPullRequest()\n\n> **unlinkPullRequest**(`url`): `Promise`\\<`void`\\>\n\nUnlink a GitHub pull request a person linked to this session. The pull\nrequest the session's agent opened stays linked.\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `url` | `string` |\n\n#### Returns\n\n`Promise`\\<`void`\\>\n\n***\n\n### updateSharePermissions()\n\n> **updateSharePermissions**(`options`): `Promise`\\<[`SharePermissionV2`](../../../../../generated/agent-harness/types.gen/type-aliases/SharePermissionV2.md)\\>\n\nReplace sharing grants.\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `options` | [`SharePermissionOptions`](../../../sharing/permissions/type-aliases/SharePermissionOptions.md) |\n\n#### Returns\n\n`Promise`\\<[`SharePermissionV2`](../../../../../generated/agent-harness/types.gen/type-aliases/SharePermissionV2.md)\\>\n\n***\n\n### byId()\n\n> `static` **byId**(`client`, `id`): `AgentSession`\n\nA handle to an agent session by id. Details load on first access.\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `client` | `MacroClient` |\n| `id` | `string` |\n\n#### Returns\n\n`AgentSession`\n\n***\n\n### codingPreferences()\n\n> `static` **codingPreferences**(`client`): `Promise`\\<[`CodingPreferencesBody`](../../../../../generated/agent-harness/types.gen/type-aliases/CodingPreferencesBody.md)\\>\n\nWhat the caller's new coding sessions are told to do beyond their assignment.\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `client` | `MacroClient` |\n\n#### Returns\n\n`Promise`\\<[`CodingPreferencesBody`](../../../../../generated/agent-harness/types.gen/type-aliases/CodingPreferencesBody.md)\\>\n\n***\n\n### createManaged()\n\n> `static` **createManaged**(`client`, `opts?`): `Promise`\\<`AgentSession`\\>\n\nCreate a managed session, optionally delivering its first prompt.\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `client` | `MacroClient` |\n| `opts?` | [`CreateManagedSessionOptions`](../type-aliases/CreateManagedSessionOptions.md) |\n\n#### Returns\n\n`Promise`\\<`AgentSession`\\>\n\n***\n\n### defaultSandboxSize()\n\n> `static` **defaultSandboxSize**(`client`): `Promise`\\<[`SandboxSize`](../../../../../generated/agent-harness/types.gen/type-aliases/SandboxSize.md)\\>\n\nThe caller's default sandbox size for new `@coder` sessions.\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `client` | `MacroClient` |\n\n#### Returns\n\n`Promise`\\<[`SandboxSize`](../../../../../generated/agent-harness/types.gen/type-aliases/SandboxSize.md)\\>\n\n***\n\n### forPullRequest()\n\n> `static` **forPullRequest**(`client`, `url`): `Promise`\\<`AgentSession`[]\\>\n\nThe sessions linked to the GitHub pull request at `url` that the caller\ncan view, oldest link first.\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `client` | `MacroClient` |\n| `url` | `string` |\n\n#### Returns\n\n`Promise`\\<`AgentSession`[]\\>\n\n***\n\n### repositories()\n\n> `static` **repositories**(`client`): `Promise`\\<[`SelectableRepository`](../type-aliases/SelectableRepository.md)[]\\>\n\nThe GitHub repositories the caller can hand a managed session: every\nrepository under an installation of Macro's GitHub App they or their\nteams made, sorted by `owner/name`. Empty when the App is installed\nnowhere they reach.\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `client` | `MacroClient` |\n\n#### Returns\n\n`Promise`\\<[`SelectableRepository`](../type-aliases/SelectableRepository.md)[]\\>\n\n***\n\n### repositoryBranches()\n\n> `static` **repositoryBranches**(`client`, `repoUrl`): `Promise`\\<`string`[]\\>\n\nBranch names on one repository the caller can start a managed session\nfrom, in the order GitHub listed them. Empty when the repository has\nno commits yet. `repoUrl` is one of the URLs [AgentSession.repositories](#repositories)\nlists.\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `client` | `MacroClient` |\n| `repoUrl` | `string` |\n\n#### Returns\n\n`Promise`\\<`string`[]\\>\n\n***\n\n### setCodingPreferences()\n\n> `static` **setCodingPreferences**(`client`, `preferences`): `Promise`\\<[`CodingPreferencesBody`](../../../../../generated/agent-harness/types.gen/type-aliases/CodingPreferencesBody.md)\\>\n\nReplace the caller's coding preferences.\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `client` | `MacroClient` |\n| `preferences` | [`CodingPreferencesBody`](../../../../../generated/agent-harness/types.gen/type-aliases/CodingPreferencesBody.md) |\n\n#### Returns\n\n`Promise`\\<[`CodingPreferencesBody`](../../../../../generated/agent-harness/types.gen/type-aliases/CodingPreferencesBody.md)\\>\n\n***\n\n### setDefaultSandboxSize()\n\n> `static` **setDefaultSandboxSize**(`client`, `size`): `Promise`\\<[`SandboxSize`](../../../../../generated/agent-harness/types.gen/type-aliases/SandboxSize.md)\\>\n\nSet the caller's default sandbox size for the next `@coder` mention.\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `client` | `MacroClient` |\n| `size` | [`SandboxSize`](../../../../../generated/agent-harness/types.gen/type-aliases/SandboxSize.md) |\n\n#### Returns\n\n`Promise`\\<[`SandboxSize`](../../../../../generated/agent-harness/types.gen/type-aliases/SandboxSize.md)\\>\n\n\n<!-- src/entities/agent-sessions/agent-session/type-aliases/CreateManagedSessionOptions.md -->\n# Type Alias: CreateManagedSessionOptions\n\n> **CreateManagedSessionOptions** = \\{ `instructions?`: `string`; `model?`: `string`; `prompt?`: `string`; `repoBranch?`: `string`; `repoUrl?`: `string`; \\}\n\nWhat a managed session is created with.\n\n## Properties\n\n| Property | Type | Description |\n| ------ | ------ | ------ |\n| <a id=\"property-instructions\"></a> `instructions?` | `string` | Instructions the session's runtime works under, fixed for its life. |\n| <a id=\"property-model\"></a> `model?` | `string` | The model the session runs on, instead of its persona's. The session's model from creation, read back as [AgentSession.model](../classes/AgentSession.md#property-model). |\n| <a id=\"property-prompt\"></a> `prompt?` | `string` | First prompt to deliver once the session is running. |\n| <a id=\"property-repobranch\"></a> `repoBranch?` | `string` | The branch the session starts on; needs `repoUrl`. Omitted, the repository's default branch. |\n| <a id=\"property-repourl\"></a> `repoUrl?` | `string` | The repository the session works on, as one of the URLs [AgentSession.repositories](../classes/AgentSession.md#repositories) lists for the caller. Omitted, the runtime chooses from the prompt. Honored for Cursor sessions. |\n\n\n<!-- src/entities/agent-sessions/agent-session/type-aliases/FoldedAgentSessionLog.md -->\n# Type Alias: FoldedAgentSessionLog\n\n> **FoldedAgentSessionLog** = \\{ `bot`: [`SessionBot`](../../../../../generated/agent-harness/types.gen/type-aliases/SessionBot.md); `messages`: `FoldedMessage`[]; \\}\n\nA session's history folded into readable messages, oldest first.\n\n## Properties\n\n| Property | Type | Description |\n| ------ | ------ | ------ |\n| <a id=\"property-bot\"></a> `bot` | [`SessionBot`](../../../../../generated/agent-harness/types.gen/type-aliases/SessionBot.md) | The agent that sent every agent message. |\n| <a id=\"property-messages\"></a> `messages` | `FoldedMessage`[] | Prompts and replies with their tool calls, results, plans, permission requests and how each turn ended. |\n\n\n<!-- src/entities/agent-sessions/agent-session/type-aliases/SelectableRepository.md -->\n# Type Alias: SelectableRepository\n\n> **SelectableRepository** = \\{ `defaultBranch?`: `string`; `url`: `string`; \\}\n\nA GitHub repository the caller can point a managed session at.\n\n## Properties\n\n| Property | Type | Description |\n| ------ | ------ | ------ |\n| <a id=\"property-defaultbranch\"></a> `defaultBranch?` | `string` | The branch its clones check out, and where a session starts unless `createManaged` names another. Absent for a repository with no commits. |\n| <a id=\"property-url\"></a> `url` | `string` | The canonical `https://github.com/owner/name` URL, as `createManaged` takes it. |\n\n\n<!-- src/entities/agent-sessions/agent-session/type-aliases/SessionPullRequest.md -->\n# Type Alias: SessionPullRequest\n\n> **SessionPullRequest** = \\{ `createdAt`: `string`; `githubKey`: `string`; `linkedBy?`: [`User`](../../../users/user/classes/User.md); `source`: [`PullRequestLinkSource`](../../../../../generated/agent-harness/types.gen/type-aliases/PullRequestLinkSource.md); `url`: `string`; \\}\n\nA GitHub pull request linked to a session.\n\n## Properties\n\n| Property | Type | Description |\n| ------ | ------ | ------ |\n| <a id=\"property-createdat\"></a> `createdAt` | `string` | When it was linked. |\n| <a id=\"property-githubkey\"></a> `githubKey` | `string` | The pull request's `owner/repo/pull/number` key. |\n| <a id=\"property-linkedby\"></a> `linkedBy?` | [`User`](../../../users/user/classes/User.md) | Who linked it, for pull requests a person linked. |\n| <a id=\"property-source\"></a> `source` | [`PullRequestLinkSource`](../../../../../generated/agent-harness/types.gen/type-aliases/PullRequestLinkSource.md) | Whether the session's agent opened it or a person linked it. |\n| <a id=\"property-url\"></a> `url` | `string` | The pull request's GitHub URL. |\n\n\n<!-- src/entities/agent-sessions/code-execution/classes/CodeExecution.md -->\n# Class: CodeExecution\n\nA saved code execution belonging to one agent session.\n\n## Extends\n\n- [`MacroEntity`](../../../entity/classes/MacroEntity.md)\\<[`ExecutionRecord`](../../../../../generated/agent-harness/types.gen/type-aliases/ExecutionRecord.md)\\>\n\n## Properties\n\n| Property | Modifier | Type | Description | Inherited from |\n| ------ | ------ | ------ | ------ | ------ |\n| <a id=\"property-calls\"></a> `calls` | `readonly` | () => `Promise`\\<[`RecordedToolCall`](../../../../../generated/agent-harness/types.gen/type-aliases/RecordedToolCall.md)[]\\> | Legacy bridge calls; direct HTTP executions leave this empty. | - |\n| <a id=\"property-error\"></a> `error` | `readonly` | () => `Promise`\\<`string` \\| `undefined`\\> | Execution failure text, when present. | - |\n| <a id=\"property-id\"></a> `id` | `public readonly` | `string` | - | [`MacroEntity`](../../../entity/classes/MacroEntity.md).[`id`](../../../entity/classes/MacroEntity.md#property-id) |\n| <a id=\"property-reviews\"></a> `reviews` | `readonly` | \\{ `create`: (`input`) => `Promise`\\<[`SessionReview`](../../review/classes/SessionReview.md)\\>; `list`: () => `Promise`\\<[`SessionReview`](../../review/classes/SessionReview.md)[]\\>; \\} | Persistent UI blocks, independent of the execution lifetime. | - |\n| `reviews.create` | `public` | (`input`) => `Promise`\\<[`SessionReview`](../../review/classes/SessionReview.md)\\> | - | - |\n| `reviews.list` | `public` | () => `Promise`\\<[`SessionReview`](../../review/classes/SessionReview.md)[]\\> | - | - |\n| <a id=\"property-source\"></a> `source` | `readonly` | () => `Promise`\\<`string`\\> | The submitted JavaScript module source. | - |\n| <a id=\"property-status\"></a> `status` | `readonly` | () => `Promise`\\<[`ExecutionStatus`](../../../../../generated/agent-harness/types.gen/type-aliases/ExecutionStatus.md)\\> | Saved execution state. Call refresh to observe subsequent changes. | - |\n\n## Methods\n\n### refresh()\n\n> **refresh**(): `Promise`\\<`CodeExecution`\\>\n\nReload the saved state, for example after a running execution completes.\n\n#### Returns\n\n`Promise`\\<`CodeExecution`\\>\n\n***\n\n### result()\n\n> **result**(): `Promise`\\<`unknown`\\>\n\nThe program's returned JSON; preserves an explicit null result.\n\n#### Returns\n\n`Promise`\\<`unknown`\\>\n\n***\n\n### session()\n\n> **session**(): [`AgentSession`](../../agent-session/classes/AgentSession.md)\n\nThe session this execution belongs to.\n\n#### Returns\n\n[`AgentSession`](../../agent-session/classes/AgentSession.md)\n\n***\n\n### toJSON()\n\n> **toJSON**(): \\{ `detail?`: [`ExecutionRecord`](../../../../../generated/agent-harness/types.gen/type-aliases/ExecutionRecord.md); `id`: `string`; \\}\n\n#### Returns\n\n\\{ `detail?`: [`ExecutionRecord`](../../../../../generated/agent-harness/types.gen/type-aliases/ExecutionRecord.md); `id`: `string`; \\}\n\n| Name | Type |\n| ------ | ------ |\n| `detail?` | [`ExecutionRecord`](../../../../../generated/agent-harness/types.gen/type-aliases/ExecutionRecord.md) |\n| `id` | `string` |\n\n#### Inherited from\n\n[`MacroEntity`](../../../entity/classes/MacroEntity.md).[`toJSON`](../../../entity/classes/MacroEntity.md#tojson)\n\n***\n\n### byId()\n\n> `static` **byId**(`client`, `session`, `id`): `CodeExecution`\n\nConstruct a lazy execution handle within its owning session.\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `client` | `MacroClient` |\n| `session` | [`AgentSession`](../../agent-session/classes/AgentSession.md) |\n| `id` | `string` |\n\n#### Returns\n\n`CodeExecution`\n\n\n<!-- src/entities/agent-sessions/code-execution/type-aliases/CodeResult.md -->\n# Type Alias: CodeResult\n\n> **CodeResult** = \\{ `data`: [`JsonValue`](JsonValue.md); `reviews`: \\{ `id`: `string`; `kind`: `\"email\"` \\| `\"calendar\"`; \\}[]; \\}\n\nJSON data returned to the model, plus references to independently saved UI.\n\n## Properties\n\n| Property | Type |\n| ------ | ------ |\n| <a id=\"property-data\"></a> `data` | [`JsonValue`](JsonValue.md) |\n| <a id=\"property-reviews\"></a> `reviews` | \\{ `id`: `string`; `kind`: `\"email\"` \\| `\"calendar\"`; \\}[] |\n\n\n<!-- src/entities/agent-sessions/code-execution/type-aliases/JsonValue.md -->\n# Type Alias: JsonValue\n\n> **JsonValue** = `null` \\| `boolean` \\| `number` \\| `string` \\| `JsonValue`[] \\| \\{\\[`key`: `string`\\]: `JsonValue`; \\}\n\n\n<!-- src/entities/agent-sessions/conversation/classes/AgentConversation.md -->\n# Class: AgentConversation\n\nAn agent's conversation in one channel.\n\n## Constructors\n\n### Constructor\n\n> **new AgentConversation**(`client`, `channel`, `record`): `AgentConversation`\n\n#### Parameters\n\n| Parameter | Type | Description |\n| ------ | ------ | ------ |\n| `client` | `MacroClient` | - |\n| `channel` | [`Channel`](../../../channels/channel/classes/Channel.md) | Parent channel. |\n| `record` | [`AgentConversationDto`](../../../../../generated/agent-harness/types.gen/type-aliases/AgentConversationDto.md) | - |\n\n#### Returns\n\n`AgentConversation`\n\n## Properties\n\n| Property | Modifier | Type | Description |\n| ------ | ------ | ------ | ------ |\n| <a id=\"property-channel\"></a> `channel` | `readonly` | [`Channel`](../../../channels/channel/classes/Channel.md) | Parent channel. |\n\n## Methods\n\n### agent()\n\n> **agent**(): [`Agent`](../../../agents/agent/classes/Agent.md)\n\nThe agent taking part.\n\n#### Returns\n\n[`Agent`](../../../agents/agent/classes/Agent.md)\n\n***\n\n### sessions()\n\n> **sessions**(): \\{ `createdAt`: `string`; `isCurrent`: `boolean`; `session`: [`AgentSession`](../../agent-session/classes/AgentSession.md); \\}[]\n\nConversation sessions with their current flag.\n\n#### Returns\n\n\\{ `createdAt`: `string`; `isCurrent`: `boolean`; `session`: [`AgentSession`](../../agent-session/classes/AgentSession.md); \\}[]\n\n***\n\n### startFresh()\n\n> **startFresh**(`session`): `Promise`\\<`void`\\>\n\nStart a fresh conversation from one of its sessions.\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `session` | [`AgentSession`](../../agent-session/classes/AgentSession.md) |\n\n#### Returns\n\n`Promise`\\<`void`\\>\n\n***\n\n### toJSON()\n\n> **toJSON**(): \\{ `agentId`: `string`; `channelId`: `string`; \\}\n\nSerialize scoped identity.\n\n#### Returns\n\n\\{ `agentId`: `string`; `channelId`: `string`; \\}\n\n| Name | Type |\n| ------ | ------ |\n| `agentId` | `string` |\n| `channelId` | `string` |\n\n***\n\n### turns()\n\n> **turns**(): [`ConversationTurn`](ConversationTurn.md)[]\n\nRecorded conversation turns.\n\n#### Returns\n\n[`ConversationTurn`](ConversationTurn.md)[]\n\n\n<!-- src/entities/agent-sessions/conversation/classes/ConversationTurn.md -->\n# Class: ConversationTurn\n\nOne agent conversation turn, identified by its source message.\n\n## Constructors\n\n### Constructor\n\n> **new ConversationTurn**(`client`, `conversation`, `record`): `ConversationTurn`\n\n#### Parameters\n\n| Parameter | Type | Description |\n| ------ | ------ | ------ |\n| `client` | `MacroClient` | - |\n| `conversation` | [`AgentConversation`](AgentConversation.md) | Parent conversation. |\n| `record` | [`AgentConversationTurnDto`](../../../../../generated/agent-harness/types.gen/type-aliases/AgentConversationTurnDto.md) | - |\n\n#### Returns\n\n`ConversationTurn`\n\n## Properties\n\n| Property | Modifier | Type | Description |\n| ------ | ------ | ------ | ------ |\n| <a id=\"property-conversation\"></a> `conversation` | `readonly` | [`AgentConversation`](AgentConversation.md) | Parent conversation. |\n\n## Methods\n\n### retry()\n\n> **retry**(`actionId`): `Promise`\\<`void`\\>\n\nRetry with a caller persisted action nonce. Never retries automatically.\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `actionId` | `string` |\n\n#### Returns\n\n`Promise`\\<`void`\\>\n\n***\n\n### session()\n\n> **session**(): [`AgentSession`](../../agent-session/classes/AgentSession.md)\n\nSession that handled this turn.\n\n#### Returns\n\n[`AgentSession`](../../agent-session/classes/AgentSession.md)\n\n***\n\n### source()\n\n> **source**(): [`Message`](../../../channels/message/classes/Message.md)\n\nOriginating message.\n\n#### Returns\n\n[`Message`](../../../channels/message/classes/Message.md)\n\n***\n\n### toJSON()\n\n> **toJSON**(): \\{ `channelId`: `string`; `sourceMessageId`: `string`; \\}\n\nSerialize scoped identity.\n\n#### Returns\n\n\\{ `channelId`: `string`; `sourceMessageId`: `string`; \\}\n\n| Name | Type |\n| ------ | ------ |\n| `channelId` | `string` |\n| `sourceMessageId` | `string` |\n\n\n<!-- src/entities/agent-sessions/namespace/classes/AgentSessionNamespace.md -->\n# Class: AgentSessionNamespace\n\nEntry point for coding-agent sessions.\n\n## Constructors\n\n### Constructor\n\n> **new AgentSessionNamespace**(`client`): `AgentSessionNamespace`\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `client` | `MacroClient` |\n\n#### Returns\n\n`AgentSessionNamespace`\n\n## Methods\n\n### byId()\n\n> **byId**(`id`): [`AgentSession`](../../agent-session/classes/AgentSession.md)\n\nA handle to an agent session by id. Details load on first access.\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `id` | `string` |\n\n#### Returns\n\n[`AgentSession`](../../agent-session/classes/AgentSession.md)\n\n***\n\n### codingPreferences()\n\n> **codingPreferences**(): `Promise`\\<[`CodingPreferencesBody`](../../../../../generated/agent-harness/types.gen/type-aliases/CodingPreferencesBody.md)\\>\n\nWhat the caller's new coding sessions are told to do beyond their assignment.\n\n#### Returns\n\n`Promise`\\<[`CodingPreferencesBody`](../../../../../generated/agent-harness/types.gen/type-aliases/CodingPreferencesBody.md)\\>\n\n***\n\n### createManaged()\n\n> **createManaged**(`opts?`): `Promise`\\<[`AgentSession`](../../agent-session/classes/AgentSession.md)\\>\n\nCreate a managed agent session.\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `opts?` | [`CreateManagedSessionOptions`](../../agent-session/type-aliases/CreateManagedSessionOptions.md) |\n\n#### Returns\n\n`Promise`\\<[`AgentSession`](../../agent-session/classes/AgentSession.md)\\>\n\n***\n\n### defaultSandboxSize()\n\n> **defaultSandboxSize**(): `Promise`\\<[`SandboxSize`](../../../../../generated/agent-harness/types.gen/type-aliases/SandboxSize.md)\\>\n\nThe caller's default sandbox size for new `@coder` sessions.\n\n#### Returns\n\n`Promise`\\<[`SandboxSize`](../../../../../generated/agent-harness/types.gen/type-aliases/SandboxSize.md)\\>\n\n***\n\n### forPullRequest()\n\n> **forPullRequest**(`url`): `Promise`\\<[`AgentSession`](../../agent-session/classes/AgentSession.md)[]\\>\n\nThe sessions linked to the GitHub pull request at `url` that the caller\ncan view.\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `url` | `string` |\n\n#### Returns\n\n`Promise`\\<[`AgentSession`](../../agent-session/classes/AgentSession.md)[]\\>\n\n***\n\n### repositories()\n\n> **repositories**(): `Promise`\\<[`SelectableRepository`](../../agent-session/type-aliases/SelectableRepository.md)[]\\>\n\nThe GitHub repositories the caller can hand a managed session, with the\nbranch each one's sessions start on by default.\n\n#### Returns\n\n`Promise`\\<[`SelectableRepository`](../../agent-session/type-aliases/SelectableRepository.md)[]\\>\n\n***\n\n### repositoryBranches()\n\n> **repositoryBranches**(`repoUrl`): `Promise`\\<`string`[]\\>\n\nBranch names on one repository the caller can start a managed session\nfrom. `repoUrl` is one of the URLs [AgentSessionNamespace.repositories](#repositories)\nlists.\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `repoUrl` | `string` |\n\n#### Returns\n\n`Promise`\\<`string`[]\\>\n\n***\n\n### setCodingPreferences()\n\n> **setCodingPreferences**(`preferences`): `Promise`\\<[`CodingPreferencesBody`](../../../../../generated/agent-harness/types.gen/type-aliases/CodingPreferencesBody.md)\\>\n\nReplace the caller's coding preferences.\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `preferences` | [`CodingPreferencesBody`](../../../../../generated/agent-harness/types.gen/type-aliases/CodingPreferencesBody.md) |\n\n#### Returns\n\n`Promise`\\<[`CodingPreferencesBody`](../../../../../generated/agent-harness/types.gen/type-aliases/CodingPreferencesBody.md)\\>\n\n***\n\n### setDefaultSandboxSize()\n\n> **setDefaultSandboxSize**(`size`): `Promise`\\<[`SandboxSize`](../../../../../generated/agent-harness/types.gen/type-aliases/SandboxSize.md)\\>\n\nSet the caller's default sandbox size for the next `@coder` mention.\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `size` | [`SandboxSize`](../../../../../generated/agent-harness/types.gen/type-aliases/SandboxSize.md) |\n\n#### Returns\n\n`Promise`\\<[`SandboxSize`](../../../../../generated/agent-harness/types.gen/type-aliases/SandboxSize.md)\\>\n\n\n<!-- src/entities/agent-sessions/queued-action/classes/QueuedAction.md -->\n# Class: QueuedAction\n\nOne action waiting in an agent session's server-side queue: a prompt or a\ncompact accepted while a turn was running, dispatching when that turn\nends. Until then it can be edited (prompts only), steered, or removed; once\nit has dispatched, these actions answer 404 — there is no un-sending.\n\nList-sourced only - there is no per-action GET, only the queue snapshot -\nso this wraps whatever DTO the caller already has, the same shape\n`CrmComment` wraps a list-sourced record, rather than `MacroEntity`'s\nlazy-fetch-by-id.\n\n## Accessors\n\n### actionId\n\n#### Get Signature\n\n> **get** **actionId**(): `string`\n\nThe id the action was accepted under. Once it dispatches, this equals\n`requestId` on the folded message the action derives.\n\n##### Returns\n\n`string`\n\n***\n\n### actorUserId\n\n#### Get Signature\n\n> **get** **actorUserId**(): `string` \\| `undefined`\n\nThe user who queued it, absent when a bot acted on nobody's behalf.\n\n##### Returns\n\n`string` \\| `undefined`\n\n***\n\n### attachments\n\n#### Get Signature\n\n> **get** **attachments**(): [`PromptAttachment`](../../../../../generated/agent-harness/types.gen/type-aliases/PromptAttachment.md)[]\n\nFiles the prompt refers to; empty for a compact. Kept through an edit.\n\n##### Returns\n\n[`PromptAttachment`](../../../../../generated/agent-harness/types.gen/type-aliases/PromptAttachment.md)[]\n\n***\n\n### createdAt\n\n#### Get Signature\n\n> **get** **createdAt**(): `string`\n\nWhen the action was accepted, as an RFC 3339 timestamp.\n\n##### Returns\n\n`string`\n\n***\n\n### kind\n\n#### Get Signature\n\n> **get** **kind**(): `string`\n\n`prompt` or `compact`; only turn-occupying actions are ever queued.\n\n##### Returns\n\n`string`\n\n***\n\n### prompt\n\n#### Get Signature\n\n> **get** **prompt**(): `string` \\| `undefined`\n\nThe prompt's raw text, present for prompts only. What an edit replaces.\n\n##### Returns\n\n`string` \\| `undefined`\n\n## Methods\n\n### edit()\n\n> **edit**(`prompt`): `Promise`\\<`QueuedAction`\\>\n\nReplace this queued prompt's text before it dispatches. Returns this\nhandle for chaining; the edit endpoint answers 204, so `prompt` is\napplied locally rather than reread from a response body.\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `prompt` | `string` |\n\n#### Returns\n\n`Promise`\\<`QueuedAction`\\>\n\n***\n\n### remove()\n\n> **remove**(): `Promise`\\<`void`\\>\n\nRemove this action from the queue before it dispatches.\n\n#### Returns\n\n`Promise`\\<`void`\\>\n\n***\n\n### steer()\n\n> **steer**(): `Promise`\\<`void`\\>\n\nMove this action to the front and interrupt the current turn to run it next.\n\n#### Returns\n\n`Promise`\\<`void`\\>\n\n***\n\n### from()\n\n> `static` **from**(`client`, `sessionId`, `dto`): `QueuedAction`\n\nWrap a queue entry already in hand (e.g. from `AgentSession.queue()`).\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `client` | `MacroClient` |\n| `sessionId` | `string` |\n| `dto` | [`QueuedActionDto`](../../../../../generated/agent-harness/types.gen/type-aliases/QueuedActionDto.md) |\n\n#### Returns\n\n`QueuedAction`\n\n\n<!-- src/entities/agent-sessions/review/classes/ReviewItem.md -->\n# Class: ReviewItem\n\nAn item handle scopes edits and decisions to its persisted review.\n\n## Constructors\n\n### Constructor\n\n> **new ReviewItem**(`snapshot`, `actions`): `ReviewItem`\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `snapshot` | [`ReviewItem`](../../../../../generated/agent-harness/types.gen/type-aliases/ReviewItem.md) |\n| `actions` | \\{ `dismiss`: () => `Promise`\\<`void`\\>; `edit`: (`draft`) => `Promise`\\<`void`\\>; `submit`: () => `Promise`\\<`void`\\>; \\} |\n| `actions.dismiss` | () => `Promise`\\<`void`\\> |\n| `actions.edit` | (`draft`) => `Promise`\\<`void`\\> |\n| `actions.submit` | () => `Promise`\\<`void`\\> |\n\n#### Returns\n\n`ReviewItem`\n\n## Accessors\n\n### draft\n\n#### Get Signature\n\n> **get** **draft**(): `unknown`\n\n##### Returns\n\n`unknown`\n\n***\n\n### id\n\n#### Get Signature\n\n> **get** **id**(): `string`\n\n##### Returns\n\n`string`\n\n***\n\n### result\n\n#### Get Signature\n\n> **get** **result**(): `unknown`\n\n##### Returns\n\n`unknown`\n\n***\n\n### status\n\n#### Get Signature\n\n> **get** **status**(): [`ReviewItemStatus`](../../../../../generated/agent-harness/types.gen/type-aliases/ReviewItemStatus.md)\n\n##### Returns\n\n[`ReviewItemStatus`](../../../../../generated/agent-harness/types.gen/type-aliases/ReviewItemStatus.md)\n\n## Methods\n\n### dismiss()\n\n> **dismiss**(): `Promise`\\<`void`\\>\n\n#### Returns\n\n`Promise`\\<`void`\\>\n\n***\n\n### edit()\n\n> **edit**(`draft`): `Promise`\\<`void`\\>\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `draft` | `unknown` |\n\n#### Returns\n\n`Promise`\\<`void`\\>\n\n***\n\n### submit()\n\n> **submit**(): `Promise`\\<`void`\\>\n\n#### Returns\n\n`Promise`\\<`void`\\>\n\n\n<!-- src/entities/agent-sessions/review/classes/SessionReview.md -->\n# Class: SessionReview\n\nA persistent review remains actionable after the code execution ends.\n\n## Extends\n\n- [`MacroEntity`](../../../entity/classes/MacroEntity.md)\\<[`ReviewRecord`](../../../../../generated/agent-harness/types.gen/type-aliases/ReviewRecord.md)\\>\n\n## Properties\n\n| Property | Modifier | Type | Inherited from |\n| ------ | ------ | ------ | ------ |\n| <a id=\"property-id\"></a> `id` | `public readonly` | `string` | [`MacroEntity`](../../../entity/classes/MacroEntity.md).[`id`](../../../entity/classes/MacroEntity.md#property-id) |\n| <a id=\"property-kind\"></a> `kind` | `readonly` | () => `Promise`\\<[`ReviewKind`](../../../../../generated/agent-harness/types.gen/type-aliases/ReviewKind.md)\\> | - |\n| <a id=\"property-title\"></a> `title` | `readonly` | () => `Promise`\\<`string`\\> | - |\n\n## Methods\n\n### dismissAll()\n\n> **dismissAll**(): `Promise`\\<`void`\\>\n\n#### Returns\n\n`Promise`\\<`void`\\>\n\n***\n\n### items()\n\n> **items**(): `Promise`\\<[`ReviewItem`](ReviewItem.md)[]\\>\n\n#### Returns\n\n`Promise`\\<[`ReviewItem`](ReviewItem.md)[]\\>\n\n***\n\n### reference()\n\n> **reference**(): `Promise`\\<\\{ `id`: `string`; `kind`: [`ReviewKind`](../../../../../generated/agent-harness/types.gen/type-aliases/ReviewKind.md); \\}\\>\n\nCompact reference for main()'s returned reviews array.\n\n#### Returns\n\n`Promise`\\<\\{ `id`: `string`; `kind`: [`ReviewKind`](../../../../../generated/agent-harness/types.gen/type-aliases/ReviewKind.md); \\}\\>\n\n***\n\n### refresh()\n\n> **refresh**(): `Promise`\\<`SessionReview`\\>\n\n#### Returns\n\n`Promise`\\<`SessionReview`\\>\n\n***\n\n### session()\n\n> **session**(): [`AgentSession`](../../agent-session/classes/AgentSession.md)\n\n#### Returns\n\n[`AgentSession`](../../agent-session/classes/AgentSession.md)\n\n***\n\n### submitAll()\n\n> **submitAll**(): `Promise`\\<`void`\\>\n\nSubmit the saved pending drafts. Never retries a conflict or ambiguous send.\n\n#### Returns\n\n`Promise`\\<`void`\\>\n\n***\n\n### toJSON()\n\n> **toJSON**(): \\{ `detail?`: [`ReviewRecord`](../../../../../generated/agent-harness/types.gen/type-aliases/ReviewRecord.md); `id`: `string`; \\}\n\n#### Returns\n\n\\{ `detail?`: [`ReviewRecord`](../../../../../generated/agent-harness/types.gen/type-aliases/ReviewRecord.md); `id`: `string`; \\}\n\n| Name | Type |\n| ------ | ------ |\n| `detail?` | [`ReviewRecord`](../../../../../generated/agent-harness/types.gen/type-aliases/ReviewRecord.md) |\n| `id` | `string` |\n\n#### Inherited from\n\n[`MacroEntity`](../../../entity/classes/MacroEntity.md).[`toJSON`](../../../entity/classes/MacroEntity.md#tojson)\n\n***\n\n### byId()\n\n> `static` **byId**(`client`, `session`, `id`): `SessionReview`\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `client` | `MacroClient` |\n| `session` | [`AgentSession`](../../agent-session/classes/AgentSession.md) |\n| `id` | `string` |\n\n#### Returns\n\n`SessionReview`\n\n***\n\n### create()\n\n> `static` **create**(`client`, `execution`, `input`): `Promise`\\<`SessionReview`\\>\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `client` | `MacroClient` |\n| `execution` | [`CodeExecution`](../../code-execution/classes/CodeExecution.md) |\n| `input` | [`CreateReviewOptions`](../type-aliases/CreateReviewOptions.md) |\n\n#### Returns\n\n`Promise`\\<`SessionReview`\\>\n\n***\n\n### createForSession()\n\n> `static` **createForSession**(`client`, `session`, `input`, `execution?`): `Promise`\\<`SessionReview`\\>\n\nPresent a review in a session without requiring a code execution.\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `client` | `MacroClient` |\n| `session` | [`AgentSession`](../../agent-session/classes/AgentSession.md) |\n| `input` | [`CreateReviewOptions`](../type-aliases/CreateReviewOptions.md) |\n| `execution?` | [`CodeExecution`](../../code-execution/classes/CodeExecution.md) |\n\n#### Returns\n\n`Promise`\\<`SessionReview`\\>\n\n***\n\n### fromRecord()\n\n> `static` **fromRecord**(`client`, `session`, `record`): `SessionReview`\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `client` | `MacroClient` |\n| `session` | [`AgentSession`](../../agent-session/classes/AgentSession.md) |\n| `record` | [`ReviewRecord`](../../../../../generated/agent-harness/types.gen/type-aliases/ReviewRecord.md) |\n\n#### Returns\n\n`SessionReview`\n\n***\n\n### list()\n\n> `static` **list**(`client`, `execution`): `Promise`\\<`SessionReview`[]\\>\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `client` | `MacroClient` |\n| `execution` | [`CodeExecution`](../../code-execution/classes/CodeExecution.md) |\n\n#### Returns\n\n`Promise`\\<`SessionReview`[]\\>\n\n\n<!-- src/entities/agent-sessions/review/type-aliases/CreateReviewOptions.md -->\n# Type Alias: CreateReviewOptions\n\n> **CreateReviewOptions** = \\{ `drafts`: [`ReviewEmailDraft`](ReviewEmailDraft.md)[]; `kind`: `\"email\"`; `title`: `string`; \\} \\| \\{ `drafts`: [`ReviewCalendarDraft`](ReviewCalendarDraft.md)[]; `kind`: `\"calendar\"`; `title`: `string`; \\}\n\n\n<!-- src/entities/agent-sessions/review/type-aliases/ReviewCalendarDraft.md -->\n# Type Alias: ReviewCalendarDraft\n\n> **ReviewCalendarDraft** = \\{ `addGoogleMeet?`: `boolean`; `addMacroCall?`: `boolean`; `attendees?`: \\{ `email`: `string`; `isOptional?`: `boolean`; \\}[]; `calendarId?`: `string` \\| `null`; `description?`: `string` \\| `null`; `eventType?`: `\"default\"` \\| `\"out_of_office\"`; `location?`: `string` \\| `null`; `outOfOffice?`: \\{ `autoDeclineMode?`: `\"decline_none\"` \\| `\"decline_all\"` \\| `\"decline_new_only\"`; `declineMessage?`: `string` \\| `null`; \\} \\| `null`; `recurrenceLines?`: `string`[]; `reminders?`: \\{ `overrides?`: \\{ `method`: `string`; `minutes`: `number`; \\}[]; `useDefault`: `boolean`; \\} \\| `null`; `time`: [`EventTime`](../../../../../generated/calendar/types.gen/type-aliases/EventTime.md); `title`: `string`; \\}\n\nCalendar drafts use the existing calendar composer and creation action.\n\n## Properties\n\n| Property | Type |\n| ------ | ------ |\n| <a id=\"property-addgooglemeet\"></a> `addGoogleMeet?` | `boolean` |\n| <a id=\"property-addmacrocall\"></a> `addMacroCall?` | `boolean` |\n| <a id=\"property-attendees\"></a> `attendees?` | \\{ `email`: `string`; `isOptional?`: `boolean`; \\}[] |\n| <a id=\"property-calendarid\"></a> `calendarId?` | `string` \\| `null` |\n| <a id=\"property-description\"></a> `description?` | `string` \\| `null` |\n| <a id=\"property-eventtype\"></a> `eventType?` | `\"default\"` \\| `\"out_of_office\"` |\n| <a id=\"property-location\"></a> `location?` | `string` \\| `null` |\n| <a id=\"property-outofoffice\"></a> `outOfOffice?` | \\{ `autoDeclineMode?`: `\"decline_none\"` \\| `\"decline_all\"` \\| `\"decline_new_only\"`; `declineMessage?`: `string` \\| `null`; \\} \\| `null` |\n| <a id=\"property-recurrencelines\"></a> `recurrenceLines?` | `string`[] |\n| <a id=\"property-reminders\"></a> `reminders?` | \\{ `overrides?`: \\{ `method`: `string`; `minutes`: `number`; \\}[]; `useDefault`: `boolean`; \\} \\| `null` |\n| <a id=\"property-time\"></a> `time` | [`EventTime`](../../../../../generated/calendar/types.gen/type-aliases/EventTime.md) |\n| <a id=\"property-title\"></a> `title` | `string` |\n\n\n<!-- src/entities/agent-sessions/review/type-aliases/ReviewEmailDraft.md -->\n# Type Alias: ReviewEmailDraft\n\n> **ReviewEmailDraft** = \\{ `attachments?`: \\{ `documentId`: `string`; \\}[]; `bcc?`: \\{ `email`: `string`; `name?`: `string` \\| `null`; \\}[]; `body`: `string`; `cc?`: \\{ `email`: `string`; `name?`: `string` \\| `null`; \\}[]; `includeSignature?`: `boolean` \\| `null`; `replyingToId?`: `string` \\| `null`; `subject`: `string`; `to`: \\{ `email`: `string`; `name?`: `string` \\| `null`; \\}[]; \\}\n\nMarkdown is rendered by the ordinary email action; the UI can edit each draft.\n\n## Properties\n\n| Property | Type |\n| ------ | ------ |\n| <a id=\"property-attachments\"></a> `attachments?` | \\{ `documentId`: `string`; \\}[] |\n| <a id=\"property-bcc\"></a> `bcc?` | \\{ `email`: `string`; `name?`: `string` \\| `null`; \\}[] |\n| <a id=\"property-body\"></a> `body` | `string` |\n| <a id=\"property-cc\"></a> `cc?` | \\{ `email`: `string`; `name?`: `string` \\| `null`; \\}[] |\n| <a id=\"property-includesignature\"></a> `includeSignature?` | `boolean` \\| `null` |\n| <a id=\"property-replyingtoid\"></a> `replyingToId?` | `string` \\| `null` |\n| <a id=\"property-subject\"></a> `subject` | `string` |\n| <a id=\"property-to\"></a> `to` | \\{ `email`: `string`; `name?`: `string` \\| `null`; \\}[] |\n\n\n<!-- src/entities/agent-sessions/tool-approval/classes/ToolApproval.md -->\n# Class: ToolApproval\n\nA permission request scoped to its originating agent session.\n\n## Properties\n\n| Property | Modifier | Type | Description |\n| ------ | ------ | ------ | ------ |\n| <a id=\"property-id\"></a> `id` | `readonly` | `string` | Approval identity. |\n| <a id=\"property-session\"></a> `session` | `readonly` | [`AgentSession`](../../agent-session/classes/AgentSession.md) | Owning session. |\n\n## Methods\n\n### answer()\n\n> **answer**(`answer`): `Promise`\\<[`AnswerToolApprovalResponse`](../../../../../generated/agent-harness/types.gen/type-aliases/AnswerToolApprovalResponse.md)\\>\n\nAnswer the approval using the generated discriminated answer contract.\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `answer` | [`ToolApprovalAnswerDto`](../../../../../generated/agent-harness/types.gen/type-aliases/ToolApprovalAnswerDto.md) |\n\n#### Returns\n\n`Promise`\\<[`AnswerToolApprovalResponse`](../../../../../generated/agent-harness/types.gen/type-aliases/AnswerToolApprovalResponse.md)\\>\n\n***\n\n### toJSON()\n\n> **toJSON**(): \\{ `id`: `string`; `sessionId`: `string`; \\}\n\nSerialize scoped identity only.\n\n#### Returns\n\n\\{ `id`: `string`; `sessionId`: `string`; \\}\n\n| Name | Type |\n| ------ | ------ |\n| `id` | `string` |\n| `sessionId` | `string` |\n\n***\n\n### byId()\n\n> `static` **byId**(`client`, `session`, `id`): `ToolApproval`\n\nConstruct a scoped approval handle.\n\n#### Parameters\n\n| Parameter | Type |\n| ------ | ------ |\n| `client` | `MacroClient` |\n| `session` | [`AgentSession`](../../agent-session/classes/AgentSession.md) |\n| `id` | `string` |\n\n#### Returns\n\n`ToolApproval`\n"},{"name": "agents",diff --git a/crates/agent_code_mode/src/domain/operations.json b/crates/agent_code_mode/src/domain/operations.jsonindex e69028d967..e2b7768735 100644--- a/crates/agent_code_mode/src/domain/operations.json+++ b/crates/agent_code_mode/src/domain/operations.json@@ -17,6 +17,7 @@"agent-harness.warmAgentSessionHandler": "Warm Agent Session","agent-harness.deleteAgentSession": "Delete Agent Session","agent-harness.getAgentSession": "Get Agent Session",+ "agent-harness.reportAgentSessionAppConnected": "Report Agent Session App Connected","agent-harness.setAgentSessionArchived": "Set Agent Session Archived","agent-harness.getAgentSessionChanges": "Get Agent Session Changes","agent-harness.getAgentSessionChangesPatch": "Get Agent Session Changes Patch",@@ -185,10 +186,10 @@"cognition.getCitationHandler": "Get Citation","cognition.decisions": "Classify content","cognition.healthHandler": "Health",+ "cognition.notionReportHandler": "Notion Report",+ "cognition.cancelNotionHandler": "Cancel Notion","cognition.runImportHandler": "Run Import","cognition.discoverHandler": "Discover",- "cognition.dismissRunHandler": "Dismiss Run",- "cognition.retryGatherHandler": "Retry Gather","cognition.getStateHandler": "Get State","cognition.deleteMcpServer": "Delete Mcp Server","cognition.listMcpServers": "List Mcp Servers",diff --git a/crates/agent_code_mode/src/domain/reviews.rs b/crates/agent_code_mode/src/domain/reviews.rsindex 336b4bb2b7..ad08051958 100644--- a/crates/agent_code_mode/src/domain/reviews.rs+++ b/crates/agent_code_mode/src/domain/reviews.rs@@ -2,6 +2,7 @@use super::{CodeModeError, ExecutionId, ExecutionStore};use agent_runtime_protocol::domain::notification::{DecisionOutcome, DecisionVia, ItemStatus, NotificationEvent, ReviewItemOutcome, ReviewOutcome,+ ReviewSettlement,};use agent_session::domain::model::{AgentSessionId, TurnPrompter};use agent_session::domain::notifications::{PendingNotification, ProduceNotification};@@ -33,7 +34,7 @@ const MAX_RESULT_BYTES: usize = 8192;Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, ToSchema, schemars::JsonSchema,)]#[serde(transparent)]-pub struct ReviewId(pub Uuid);+pub struct ReviewId(#[schemars(with = "String")] pub Uuid);/// The component and action used to finish a review.#[derive(@@ -45,6 +46,8 @@ pub enum ReviewKind {Email,/// Editable calendar event stack.Calendar,+ /// A staged workspace import plan, approved as one item.+ Import,}/// A reference returned by generated code; it confers no authorization.@@ -73,7 +76,8 @@ pub struct CreateReview {/// time: {kind: "timed", startsAt: ISO datetime, endsAt: ISO datetime,/// timeZone?: IANA zone} or {kind: "allDay", startDate: ISO date,/// endDate: exclusive ISO date}, description?, location?, attendees?:- /// [{email, isOptional?}]}.+ /// [{email, isOptional?}]}. Import: exactly one draft {source: "notion"},+ /// presenting the plan ImportFromNotion staged./// Creating any review only presents these drafts; the user acts separately.pub drafts: Vec<Value>,}@@ -232,11 +236,13 @@ pub trait ReviewActions: Send + Sync + 'static {) -> Result<bool, CodeModeError> {Ok(true)}- /// Execute the reviewed draft as its saved, authenticated account holder.+ /// Execute the reviewed draft of `record` as its saved, authenticated+ /// account holder. An import only starts here: its background write+ /// reports its own end as a `review.settled` event once it stops.async fn execute(&self,owner: &MacroUserIdStr<'static>,- kind: ReviewKind,+ record: &ReviewRecord,draft: Value,) -> Result<Value, CodeModeError>;/// Persist and stream a completed presentation without waiting for user action.@@ -446,10 +452,9 @@ impl ReviewService {);}}- let event = record- .decision- .as_ref()- .map(|_| NotificationEvent::ReviewSettled(review_outcome(&record)));+ let event = record.decision.as_ref().map(|_| {+ NotificationEvent::ReviewSettled(ReviewSettlement::Actions(review_outcome(&record)))+ });match self.save(&mut record, event).await {Ok(()) => {}Err(CodeModeError::Conflict) => {@@ -479,7 +484,10 @@ impl ReviewService {record.updated_at = chrono::Utc::now().timestamp_millis();let pending = event.map(|event| {- let wake = true;+ // An approved import only started: its write settles later, and+ // that wakes the session instead.+ let wake = !(record.kind == ReviewKind::Import+ && matches!(&event, NotificationEvent::ReviewDecided(outcome) if outcome.outcome != DecisionOutcome::Dismissed));PendingNotification::new(ProduceNotification {session_id: record.session_id,subject_id: record.id.0.to_string(),@@ -672,7 +680,7 @@ impl ReviewService {}service.actions- .execute(&actor, record.kind, record.items[index].draft.clone())+ .execute(&actor, &record, record.items[index].draft.clone()).await}).await;@@ -695,8 +703,16 @@ impl ReviewService {item.error = Some("The submission outcome could not be confirmed. Check the destination before retrying.".into());}}- let event = (Some(index) == last)- .then(|| NotificationEvent::ReviewSettled(review_outcome(&record)));+ // A started import settles when its write ends; only one that+ // could not start settles here.+ let event = (Some(index) == last+ && !(record.kind == ReviewKind::Import+ && record.items[index].status == ReviewItemStatus::Completed))+ .then(|| {+ NotificationEvent::ReviewSettled(ReviewSettlement::Actions(+ review_outcome(&record),+ ))+ });if service.save(&mut record, event).await.is_err() {return;}diff --git a/crates/agent_code_mode/src/domain/reviews/test.rs b/crates/agent_code_mode/src/domain/reviews/test.rsindex 024682ec57..10b49724dc 100644--- a/crates/agent_code_mode/src/domain/reviews/test.rs+++ b/crates/agent_code_mode/src/domain/reviews/test.rs@@ -158,7 +158,7 @@ impl ReviewActions for Actions {async fn execute(&self,owner: &MacroUserIdStr<'static>,- _: ReviewKind,+ _: &ReviewRecord,draft: Value,) -> Result<Value, CodeModeError> {self.actors.lock().unwrap().push(owner.to_string());@@ -659,10 +659,111 @@ async fn a_system_turn_cannot_approve_even_with_owner_credentials() {assert_eq!(events.len(), 2);assert!(events.iter().all(|event| event.wake));assert!(- matches!(&events[1].notification.event, NotificationEvent::ReviewSettled(outcome) if outcome.by == f.actions.owner.as_ref() && outcome.via == DecisionVia::Chat)+ matches!(&events[1].notification.event, NotificationEvent::ReviewSettled(ReviewSettlement::Actions(outcome)) if outcome.by == f.actions.owner.as_ref() && outcome.via == DecisionVia::Chat));}+#[tokio::test]+async fn a_started_import_waits_quietly_for_its_write_to_settle() {+ let f = Fixture::new();+ let mut input = f.input(vec![json!({"subject":"plan"})]);+ input.kind = ReviewKind::Import;+ let row = f+ .service+ .create(&f.edit(), &f.actions.owner, input)+ .await+ .unwrap();++ f.service+ .decide(+ &f.edit(),+ &f.actions.owner,+ row.id,+ ReviewDecision::Submit {+ revision: row.revision,+ items: vec![],+ },+ )+ .await+ .unwrap();+ let started = f.settled(row.id).await;++ assert_eq!(started.items[0].status, ReviewItemStatus::Completed);+ let events = f.store.2.lock().unwrap();+ assert_eq!(events.len(), 1);+ assert!(matches!(+ events[0].notification.event,+ NotificationEvent::ReviewDecided(_)+ ));+ assert!(!events[0].wake);+}++#[tokio::test]+async fn an_import_that_could_not_start_settles_and_wakes() {+ let f = Fixture::new();+ let mut input = f.input(vec![json!({"subject":"plan", "fail": true})]);+ input.kind = ReviewKind::Import;+ let row = f+ .service+ .create(&f.edit(), &f.actions.owner, input)+ .await+ .unwrap();++ f.service+ .decide(+ &f.edit(),+ &f.actions.owner,+ row.id,+ ReviewDecision::Submit {+ revision: row.revision,+ items: vec![],+ },+ )+ .await+ .unwrap();+ let ended = f.settled(row.id).await;++ assert_eq!(ended.items[0].status, ReviewItemStatus::Unknown);+ let events = f.store.2.lock().unwrap();+ assert_eq!(events.len(), 2);+ assert!(!events[0].wake);+ assert!(matches!(+ events[1].notification.event,+ NotificationEvent::ReviewSettled(ReviewSettlement::Actions(_))+ ));+ assert!(events[1].wake);+}++#[tokio::test]+async fn dismissing_an_import_wakes_the_session() {+ let f = Fixture::new();+ let mut input = f.input(vec![json!({"subject":"plan"})]);+ input.kind = ReviewKind::Import;+ let row = f+ .service+ .create(&f.edit(), &f.actions.owner, input)+ .await+ .unwrap();++ f.service+ .decide(+ &f.edit(),+ &f.actions.owner,+ row.id,+ ReviewDecision::Dismiss {+ revision: row.revision,+ items: vec![],+ },+ )+ .await+ .unwrap();++ let events = f.store.2.lock().unwrap();+ assert_eq!(events.len(), 1);+ assert!(events[0].wake);+ assert!(f.actions.calls.lock().unwrap().is_empty());+}+#[tokio::test]async fn requester_reviews_are_decided_and_executed_only_with_their_account() {let f = Fixture::new();diff --git a/crates/agent_code_mode/src/inbound/reviews.rs b/crates/agent_code_mode/src/inbound/reviews.rsindex 1fa9ce2ab6..95ff851fd3 100644--- a/crates/agent_code_mode/src/inbound/reviews.rs+++ b/crates/agent_code_mode/src/inbound/reviews.rs@@ -32,7 +32,7 @@ pub fn toolset() -> AsyncToolCollection<ReviewToolContext> {#[serde(transparent)]#[schemars(title = "PresentReview",- description = "Present an editable email stack or calendar event stack to the user without pausing the conversation. Returns immediately after the saved review is displayed; it does not send email, create an event, or wait for approval. User decisions later deliver their recorded outcomes as typed system events. Use the existing ACP question tool for questions. Supply kind (email, calendar), title, and 1–100 drafts. Email drafts use SendEmail arguments (Markdown body); calendar drafts use CreateCalendarEvent arguments. executionId is optional and should be omitted outside ExecuteCode. Do not automatically submit these drafts or duplicate completed actions. After presenting, end your message by asking \"Should I kick it off?\"; if the person whose account the review uses says yes, submit it with DecideReview."+ description = "Present an editable email stack, calendar event stack, or import plan to the user without pausing the conversation. Returns immediately after the saved review is displayed; it does not send email, create an event, import anything, or wait for approval. User decisions later deliver their recorded outcomes as typed system events. Use the existing ACP question tool for questions. Supply kind (email, calendar, import), title, and 1–100 drafts. Email drafts use SendEmail arguments (Markdown body); calendar drafts use CreateCalendarEvent arguments; an import review has exactly one draft {\"source\":\"notion\"} and presents the plan ImportFromNotion staged. executionId is optional and should be omitted outside ExecuteCode. Do not automatically submit these drafts or duplicate completed actions. After presenting, end your message by asking \"Should I kick it off?\"; if the person whose account the review uses says yes, submit it with DecideReview.")]pub struct PresentReview(CreateReview);diff --git a/crates/agent_harness/src/domain/app_connections.rs b/crates/agent_harness/src/domain/app_connections.rsnew file mode 100644index 0000000000..1d0cce5e7c--- /dev/null+++ b/crates/agent_harness/src/domain/app_connections.rs@@ -0,0 +1,172 @@+//! Picking a session back up once its owner connects the app it was waiting on.+//!+//! An agent that needs an app the owner has not connected asks for it with a+//! connect chip. When the owner finishes connecting, the harness produces an+//! `app.connected` system event that wakes the session: the agent is told the+//! app is there, and the transcript shows no message the owner did not type.++use std::future::Future;+use std::sync::Arc;++use agent_runtime_protocol::domain::action::AgentActionId;+use agent_runtime_protocol::domain::notification::NotificationEvent;+use agent_session::domain::error::AgentSessionError;+use agent_session::domain::model::AgentSessionId;+use agent_session::domain::notifications::{NotificationService, ProduceNotification};+use agent_session::domain::ports::AgentSessionRepo;+use macro_user_id::user_id::MacroUserIdStr;++#[cfg(test)]+mod test;++/// Who owns a session, and whether it can still take a turn.+#[derive(Debug, Clone, PartialEq, Eq)]+pub struct SessionStanding {+ /// The owning user; `None` for a session a bot or team owns.+ pub owner: Option<MacroUserIdStr<'static>>,+ /// Archived sessions take no new turns.+ pub archived: bool,+}++/// Reads a session's [`SessionStanding`].+pub trait SessionStandings: Send + Sync + 'static {+ /// The standing of `session_id`.+ fn standing(+ &self,+ session_id: AgentSessionId,+ ) -> impl Future<Output = Result<SessionStanding, AgentSessionError>> + Send;+}++impl<Repo: AgentSessionRepo> SessionStandings for Repo {+ async fn standing(+ &self,+ session_id: AgentSessionId,+ ) -> Result<SessionStanding, AgentSessionError> {+ let session = self.get(session_id).await?;+ Ok(SessionStanding {+ owner: session.owner_user().ok().cloned(),+ archived: session.is_archived,+ })+ }+}++/// Produces system events for a session.+pub trait SessionNotifications: Send + Sync + 'static {+ /// Persist `notification` idempotently.+ fn produce(+ &self,+ notification: ProduceNotification,+ ) -> impl Future<Output = Result<(), AgentSessionError>> + Send;+}++impl SessionNotifications for NotificationService {+ fn produce(+ &self,+ notification: ProduceNotification,+ ) -> impl Future<Output = Result<(), AgentSessionError>> + Send {+ NotificationService::produce(self, notification)+ }+}++/// The apps a user has connected.+pub trait ConnectedApps: Send + Sync + 'static {+ /// Error reading the user's connections.+ type Err: std::error::Error + Send + Sync + 'static;++ /// The display name of `app_slug` when `user` has it connected and+ /// enabled for tool use.+ fn connected_app_name(+ &self,+ user: &MacroUserIdStr<'static>,+ app_slug: &str,+ ) -> impl Future<Output = Result<Option<String>, Self::Err>> + Send;+}++/// Why a session could not be continued after a connection.+#[derive(Debug, thiserror::Error)]+pub enum AppConnectedError {+ /// Only the owner's connections reach the session's tools, so only the+ /// owner may say one was made.+ #[error("only the session's owner can continue it after connecting an app")]+ NotOwner,+ /// The session is archived.+ #[error("the session is archived")]+ Archived,+ /// The owner has no enabled connection to the app.+ #[error("{0} is not connected")]+ NotConnected(String),+ /// Reading or prompting the session failed.+ #[error(transparent)]+ Session(#[from] AgentSessionError),+ /// The owner's connections could not be read.+ #[error("could not read the owner's connections")]+ Internal(rootcause::Report),+}++/// Wakes a session with an `app.connected` event once its owner connects an app.+pub struct AppConnectedContinuation<Sessions, Apps, Notifications> {+ sessions: Arc<Sessions>,+ apps: Arc<Apps>,+ notifications: Arc<Notifications>,+}++impl<Sessions, Apps, Notifications> AppConnectedContinuation<Sessions, Apps, Notifications>+where+ Sessions: SessionStandings,+ Apps: ConnectedApps,+ Notifications: SessionNotifications,+{+ /// Build the service.+ pub fn new(+ sessions: Arc<Sessions>,+ apps: Arc<Apps>,+ notifications: Arc<Notifications>,+ ) -> Self {+ Self {+ sessions,+ apps,+ notifications,+ }+ }++ /// Tell `session_id` that `user` has connected `app_slug`.+ ///+ /// The event starts a turn once the session is idle, after any running+ /// one. `report_id` names the report, so a retried request produces the+ /// event once.+ #[tracing::instrument(skip(self), fields(%session_id, %app_slug), err)]+ pub async fn continue_session(+ &self,+ session_id: AgentSessionId,+ user: &MacroUserIdStr<'static>,+ app_slug: &str,+ report_id: Option<AgentActionId>,+ ) -> Result<(), AppConnectedError> {+ let standing = self.sessions.standing(session_id).await?;+ if standing.owner.as_ref() != Some(user) {+ return Err(AppConnectedError::NotOwner);+ }+ if standing.archived {+ return Err(AppConnectedError::Archived);+ }+ let app_name = self+ .apps+ .connected_app_name(user, app_slug)+ .await+ .map_err(|error| {+ AppConnectedError::Internal(rootcause::Report::new(error).into_dynamic())+ })?+ .ok_or_else(|| AppConnectedError::NotConnected(app_slug.to_owned()))?;+ let report_id = report_id.unwrap_or_else(AgentActionId::mint);+ self.notifications+ .produce(ProduceNotification {+ session_id,+ dedupe_key: Some(format!("app.connected:{app_slug}:{report_id}")),+ subject_id: app_slug.to_owned(),+ event: NotificationEvent::AppConnected { app: app_name },+ wake: true,+ })+ .await?;+ Ok(())+ }+}diff --git a/crates/agent_harness/src/domain/app_connections/test.rs b/crates/agent_harness/src/domain/app_connections/test.rsnew file mode 100644index 0000000000..8aef18affe--- /dev/null+++ b/crates/agent_harness/src/domain/app_connections/test.rs@@ -0,0 +1,189 @@+use std::sync::{Arc, Mutex};++use agent_runtime_protocol::domain::action::AgentActionId;+use agent_runtime_protocol::domain::notification::NotificationEvent;+use agent_session::domain::error::AgentSessionError;+use agent_session::domain::model::AgentSessionId;+use agent_session::domain::notifications::ProduceNotification;+use macro_user_id::user_id::MacroUserIdStr;++use super::*;++struct FixedStanding(SessionStanding);++impl SessionStandings for FixedStanding {+ async fn standing(+ &self,+ _session_id: AgentSessionId,+ ) -> Result<SessionStanding, AgentSessionError> {+ Ok(self.0.clone())+ }+}++/// Connections as `(user, slug, display name)`.+struct Connections(Vec<(&'static str, &'static str, &'static str)>);++#[derive(Debug, thiserror::Error)]+#[error("unreachable")]+struct NeverFails;++impl ConnectedApps for Connections {+ type Err = NeverFails;++ async fn connected_app_name(+ &self,+ user: &MacroUserIdStr<'static>,+ app_slug: &str,+ ) -> Result<Option<String>, NeverFails> {+ Ok(self+ .0+ .iter()+ .find(|(owner, slug, _)| *owner == user.as_ref() && *slug == app_slug)+ .map(|(_, _, name)| (*name).to_owned()))+ }+}++#[derive(Default)]+struct RecordedNotifications(Mutex<Vec<ProduceNotification>>);++impl SessionNotifications for RecordedNotifications {+ async fn produce(&self, notification: ProduceNotification) -> Result<(), AgentSessionError> {+ self.0.lock().unwrap().push(notification);+ Ok(())+ }+}++fn user(email: &str) -> MacroUserIdStr<'static> {+ MacroUserIdStr::try_from_email(email).expect("a valid user id")+}++#[tokio::test]+async fn the_owner_connecting_notion_wakes_the_session_with_an_app_connected_event() {+ let session_id = AgentSessionId::new();+ let report_id = AgentActionId::mint();+ let notifications = Arc::new(RecordedNotifications::default());+ let service = AppConnectedContinuation::new(+ Arc::new(FixedStanding(SessionStanding {+ owner: Some(user("[email protected]")),+ archived: false,+ })),+ Arc::new(Connections(vec![(+ "macro|[email protected]",+ "notion",+ "Notion",+ )])),+ Arc::clone(¬ifications),+ );++ for _ in 0..2 {+ service+ .continue_session(+ session_id,+ &user("[email protected]"),+ "notion",+ Some(report_id),+ )+ .await+ .unwrap();+ }++ let produced = notifications.0.lock().unwrap();+ assert_eq!(produced.len(), 2);+ let event = &produced[0];+ assert_eq!(event.session_id, session_id);+ assert_eq!(event.subject_id, "notion");+ assert!(event.wake);+ assert_eq!(+ event.event,+ NotificationEvent::AppConnected {+ app: "Notion".into()+ }+ );+ assert_eq!(+ event.dedupe_key, produced[1].dedupe_key,+ "a retried report produces the event once"+ );+}++#[tokio::test]+async fn someone_other_than_the_owner_cannot_continue_the_session() {+ let notifications = Arc::new(RecordedNotifications::default());+ let service = AppConnectedContinuation::new(+ Arc::new(FixedStanding(SessionStanding {+ owner: Some(user("[email protected]")),+ archived: false,+ })),+ Arc::new(Connections(vec![(+ "macro|[email protected]",+ "notion",+ "Notion",+ )])),+ Arc::clone(¬ifications),+ );++ let result = service+ .continue_session(+ AgentSessionId::new(),+ &user("[email protected]"),+ "notion",+ None,+ )+ .await;++ assert!(matches!(result, Err(AppConnectedError::NotOwner)));+ assert!(notifications.0.lock().unwrap().is_empty());+}++#[tokio::test]+async fn an_app_the_owner_has_not_connected_sends_nothing() {+ let notifications = Arc::new(RecordedNotifications::default());+ let service = AppConnectedContinuation::new(+ Arc::new(FixedStanding(SessionStanding {+ owner: Some(user("[email protected]")),+ archived: false,+ })),+ Arc::new(Connections(vec![])),+ Arc::clone(¬ifications),+ );++ let result = service+ .continue_session(+ AgentSessionId::new(),+ &user("[email protected]"),+ "notion",+ None,+ )+ .await;++ assert!(matches!(result, Err(AppConnectedError::NotConnected(slug)) if slug == "notion"));+ assert!(notifications.0.lock().unwrap().is_empty());+}++#[tokio::test]+async fn an_archived_session_is_not_continued() {+ let notifications = Arc::new(RecordedNotifications::default());+ let service = AppConnectedContinuation::new(+ Arc::new(FixedStanding(SessionStanding {+ owner: Some(user("[email protected]")),+ archived: true,+ })),+ Arc::new(Connections(vec![(+ "macro|[email protected]",+ "notion",+ "Notion",+ )])),+ Arc::clone(¬ifications),+ );++ let result = service+ .continue_session(+ AgentSessionId::new(),+ &user("[email protected]"),+ "notion",+ None,+ )+ .await;++ assert!(matches!(result, Err(AppConnectedError::Archived)));+ assert!(notifications.0.lock().unwrap().is_empty());+}diff --git a/crates/agent_harness/src/domain/mod.rs b/crates/agent_harness/src/domain/mod.rsindex 099852d95a..d9d45e8347 100644--- a/crates/agent_harness/src/domain/mod.rs+++ b/crates/agent_harness/src/domain/mod.rs@@ -1,3 +1,5 @@+/// Continuing a session once its owner connects the app it waited on.+pub mod app_connections;/// Fresh ACP capability discovery without creating an agent session.pub mod capability_discovery;pub mod conversation_turns;diff --git a/crates/agent_harness/src/domain/service/test/notifications.rs b/crates/agent_harness/src/domain/service/test/notifications.rsindex bf1212455a..dbc2dfc298 100644--- a/crates/agent_harness/src/domain/service/test/notifications.rs+++ b/crates/agent_harness/src/domain/service/test/notifications.rs@@ -727,3 +727,76 @@ async fn a_blocked_worker_does_not_block_discovery_or_renewal_or_queue_duplicatecomplete_handshake(&containers.container(blocked).unwrap()).await;blocked_open.await.unwrap();}++#[tokio::test]+async fn a_settled_import_wakes_an_idle_session_with_its_final_report() {+ use agent_runtime_protocol::domain::notification::{+ ImportOutcome, ImportSettlement, ReviewSettlement,+ };+ let outbox = Arc::new(Outbox::default());+ let ((service, repo, containers, _, _), mut turns) = harness_with_outbox(outbox.clone());+ let id = AgentSessionId::new();+ let container = live_session(&service, &containers, id).await;+ turns.settled(id).await;+ let review = Uuid::now_v7();+ outbox+ .enqueue(+ PendingNotification::new(ProduceNotification {+ session_id: id,+ subject_id: review.to_string(),+ event: NotificationEvent::ReviewSettled(ReviewSettlement::Import {+ import: ImportSettlement {+ outcome: ImportOutcome::Completed,+ error: None,+ imported: 3,+ planned: 3,+ documents: 3,+ databases: 0,+ database_rows: 0,+ task_lists: 0,+ task_rows: 0,+ skipped: 1,+ not_written: 0,+ shared_with_team: 0,+ already_in_macro: 0,+ waiting_on_teammates: 0,+ root_folder_id: None,+ failures: Vec::new(),+ hand_items: Vec::new(),+ },+ }),+ dedupe_key: None,+ wake: true,+ })+ .unwrap(),+ )+ .await+ .unwrap();++ service.recover_notifications().await.unwrap();+ container.agent().wait_for_requests(4).await;++ let prompt = prompts(&container.agent());+ let ContentBlock::Text(context) = &prompt.last().unwrap()[0] else {+ panic!("the events lead the prompt");+ };+ assert!(+ context.text.contains(&format!(+ "<m-notification kind=\"review.settled\" subject=\"{review}\""+ )),+ "{}",+ context.text+ );+ assert!(+ context+ .text+ .contains(""outcome":"completed""),+ "{}",+ context.text+ );+ let prompter = repo.turn_prompter(id).await.unwrap().unwrap();+ assert_eq!(+ prompter.kind,+ agent_session::domain::model::PrompterKind::System+ );+}diff --git a/crates/agent_harness/src/inbound/app_connections.rs b/crates/agent_harness/src/inbound/app_connections.rsnew file mode 100644index 0000000000..5659748e9c--- /dev/null+++ b/crates/agent_harness/src/inbound/app_connections.rs@@ -0,0 +1,185 @@+//! Telling a session its owner connected an app, mounted under+//! `/agent-sessions`.+//!+//! The browser reports the connection; the harness checks it and produces the+//! `app.connected` event that continues the session, so the caller can+//! neither choose the words nor slip anything else into someone's session.++use std::sync::Arc;++use agent_runtime_protocol::domain::action::AgentActionId;+use agent_session::domain::model::AgentSessionId;+use agent_session::inbound::axum_router::AgentSessionApiError;+use axum::Router;+use axum::extract::{FromRef, Json, Path, State};+use axum::http::StatusCode;+use axum::response::{IntoResponse, Response};+use axum::routing::post;+use entity_access::domain::models::EditAccessLevel;+use entity_access::domain::ports::EntityAccessService;+use entity_access::inbound::axum_extractors::AgentSessionAccessLevelExtractor;+use macro_authorization::{MacroAuthorizationService, MacroAuthorizationState};+use macro_uuid::Uuid;+use serde::Deserialize;+use utoipa::ToSchema;++use crate::domain::app_connections::{+ AppConnectedContinuation, AppConnectedError, ConnectedApps, SessionNotifications,+ SessionStandings,+};++/// Request body for reporting a connected app.+#[derive(Debug, Deserialize, ToSchema)]+#[serde(rename_all = "camelCase")]+pub struct AppConnectedRequest {+ /// The Pipedream app slug the owner connected, e.g. `notion`.+ pub app_slug: String,+ /// Names the report, so a retried one wakes the session once.+ pub action_id: Option<AgentActionId>,+}++/// Router state: the continuation, plus what the access extractor resolves+/// grants and identity through.+pub struct AppConnectionsRouterState<Sessions, Apps, Notifications, Access, Auth> {+ continuation: Arc<AppConnectedContinuation<Sessions, Apps, Notifications>>,+ entity_access: Arc<Access>,+ authorization: MacroAuthorizationState<Auth>,+}++impl<Sessions, Apps, Notifications, Access, Auth>+ AppConnectionsRouterState<Sessions, Apps, Notifications, Access, Auth>+{+ /// Build the state.+ pub fn new(+ continuation: Arc<AppConnectedContinuation<Sessions, Apps, Notifications>>,+ entity_access: Arc<Access>,+ authorization: MacroAuthorizationState<Auth>,+ ) -> Self {+ Self {+ continuation,+ entity_access,+ authorization,+ }+ }+}++impl<Sessions, Apps, Notifications, Access, Auth> Clone+ for AppConnectionsRouterState<Sessions, Apps, Notifications, Access, Auth>+{+ fn clone(&self) -> Self {+ Self {+ continuation: Arc::clone(&self.continuation),+ entity_access: Arc::clone(&self.entity_access),+ authorization: self.authorization.clone(),+ }+ }+}++impl<Sessions, Apps, Notifications, Access, Auth>+ FromRef<AppConnectionsRouterState<Sessions, Apps, Notifications, Access, Auth>>+ for MacroAuthorizationState<Auth>+{+ fn from_ref(+ state: &AppConnectionsRouterState<Sessions, Apps, Notifications, Access, Auth>,+ ) -> Self {+ state.authorization.clone()+ }+}++impl<Sessions, Apps, Notifications, Access, Auth>+ FromRef<AppConnectionsRouterState<Sessions, Apps, Notifications, Access, Auth>>+ for Arc<Access>+{+ fn from_ref(+ state: &AppConnectionsRouterState<Sessions, Apps, Notifications, Access, Auth>,+ ) -> Self {+ Arc::clone(&state.entity_access)+ }+}++/// Build the router. Mount it under `/agent-sessions`.+pub fn app_connections_router<Sessions, Apps, Notifications, Access, Auth, S>(+ state: AppConnectionsRouterState<Sessions, Apps, Notifications, Access, Auth>,+) -> Router<S>+where+ Sessions: SessionStandings,+ Apps: ConnectedApps,+ Notifications: SessionNotifications,+ Access: EntityAccessService,+ Auth: MacroAuthorizationService,+ S: Clone + Send + Sync + 'static,+{+ Router::new()+ .route(+ "/{session_id}/app-connections",+ post(app_connected_handler::<Sessions, Apps, Notifications, Access, Auth>),+ )+ .with_state(state)+}++fn error_response(error: AppConnectedError) -> Response {+ match error {+ AppConnectedError::NotOwner => (StatusCode::FORBIDDEN, error.to_string()).into_response(),+ AppConnectedError::Archived => (StatusCode::CONFLICT, error.to_string()).into_response(),+ AppConnectedError::NotConnected(_) => {+ (StatusCode::UNPROCESSABLE_ENTITY, error.to_string()).into_response()+ }+ AppConnectedError::Session(error) => AgentSessionApiError::from(error).into_response(),+ AppConnectedError::Internal(error) => {+ tracing::error!(error = ?error, "continuing a session after a connection failed");+ (StatusCode::INTERNAL_SERVER_ERROR, "internal error").into_response()+ }+ }+}++#[utoipa::path(+ post,+ path = "/agent-sessions/{session_id}/app-connections",+ tag = "agent-sessions",+ operation_id = "report_agent_session_app_connected",+ params(("session_id" = Uuid, Path, description = "ID of the agent session")),+ request_body = AppConnectedRequest,+ responses(+ (status = 202, description = "The session continues once it is idle"),+ (status = 401, body = String),+ (status = 403, body = String, description = "Not the session's owner, or no edit access"),+ (status = 409, body = String, description = "The session is archived"),+ (status = 422, body = String, description = "The owner has not connected that app"),+ (status = 500, body = String),+ )+)]+/// Continue the session now that its owner connected an app it asked for.+///+/// The harness produces an `app.connected` system event, which the transcript+/// shows as no prompt; it starts a turn once the session is idle.+#[tracing::instrument(skip_all, fields(%session_id, app_slug = %request.app_slug), err(Debug))]+pub async fn app_connected_handler<Sessions, Apps, Notifications, Access, Auth>(+ access: AgentSessionAccessLevelExtractor<EditAccessLevel, Access, Auth>,+ State(state): State<AppConnectionsRouterState<Sessions, Apps, Notifications, Access, Auth>>,+ Path(session_id): Path<Uuid>,+ Json(request): Json<AppConnectedRequest>,+) -> Result<StatusCode, Response>+where+ Sessions: SessionStandings,+ Apps: ConnectedApps,+ Notifications: SessionNotifications,+ Access: EntityAccessService,+ Auth: MacroAuthorizationService,+{+ let user = access+ .entity_access_receipt+ .get_authenticated_user()+ .map_err(|_| (StatusCode::FORBIDDEN, "a person must connect the app").into_response())?+ .clone();+ state+ .continuation+ .continue_session(+ AgentSessionId::new_from_uuid(session_id),+ &user,+ &request.app_slug,+ request.action_id,+ )+ .await+ .map_err(error_response)?;+ Ok(StatusCode::ACCEPTED)+}diff --git a/crates/agent_harness/src/inbound/mod.rs b/crates/agent_harness/src/inbound/mod.rsindex b5bb8849b9..86d4eb5822 100644--- a/crates/agent_harness/src/inbound/mod.rs+++ b/crates/agent_harness/src/inbound/mod.rs@@ -3,6 +3,8 @@//! Thin by construction - decode the broker message, ask the domain whether it//! is addressed to us, hand one value inward. No policy lives here.+/// Continuing a session after its owner connects an app.+pub mod app_connections;/// Authenticated ACP capability discovery.pub mod capability_discovery;/// Authenticated agent conversation metadata and controls.diff --git a/crates/agent_harness/src/outbound/connected_apps.rs b/crates/agent_harness/src/outbound/connected_apps.rsnew file mode 100644index 0000000000..8834151520--- /dev/null+++ b/crates/agent_harness/src/outbound/connected_apps.rs@@ -0,0 +1,44 @@+//! The apps a user has connected, read from their Pipedream connections.++use std::sync::Arc;++use macro_user_id::user_id::MacroUserIdStr;+use pipedream_mcp::domain::ports::ConnectionStore;++use crate::domain::app_connections::ConnectedApps;++/// The connection store could not be read.+#[derive(Debug, thiserror::Error)]+#[error("could not read the Pipedream connection: {0}")]+pub struct ConnectionLookupError(String);++/// [`ConnectedApps`] over the Pipedream connection store.+pub struct PipedreamConnectedApps<Connections> {+ connections: Arc<Connections>,+}++impl<Connections> PipedreamConnectedApps<Connections> {+ /// Read connections from `connections`.+ pub fn new(connections: Arc<Connections>) -> Self {+ Self { connections }+ }+}++impl<Connections: ConnectionStore> ConnectedApps for PipedreamConnectedApps<Connections> {+ type Err = ConnectionLookupError;++ async fn connected_app_name(+ &self,+ user: &MacroUserIdStr<'static>,+ app_slug: &str,+ ) -> Result<Option<String>, ConnectionLookupError> {+ let connection = self+ .connections+ .load(user, app_slug)+ .await+ .map_err(|error| ConnectionLookupError(format!("{error:?}")))?;+ Ok(connection+ .filter(|connection| connection.enabled)+ .map(|connection| connection.server_name))+ }+}diff --git a/crates/agent_harness/src/outbound/mod.rs b/crates/agent_harness/src/outbound/mod.rsindex 7c4125903d..7dbe5ff669 100644--- a/crates/agent_harness/src/outbound/mod.rs+++ b/crates/agent_harness/src/outbound/mod.rs@@ -4,6 +4,7 @@pub mod agent_prompt_composer;pub mod channel_announcer;pub mod channel_prompt_context;+pub mod connected_apps;pub mod containers;/// Durable agent conversation turn journal.pub mod conversation_turns;diff --git a/crates/agent_runtime_protocol/src/domain/notification.rs b/crates/agent_runtime_protocol/src/domain/notification.rsindex 8f5ca699fb..607369db03 100644--- a/crates/agent_runtime_protocol/src/domain/notification.rs+++ b/crates/agent_runtime_protocol/src/domain/notification.rs@@ -29,7 +29,10 @@ pub enum NotificationEvent {ReviewDecided(ReviewOutcome),/// Completion of work initiated by a prior review decision.#[serde(rename = "review.settled")]- ReviewSettled(ReviewOutcome),+ ReviewSettled(ReviewSettlement),+ /// The import plan a presented review shows finished planning.+ #[serde(rename = "import.planned")]+ ImportPlanned(ImportPlan),/// An app connection became available to the session.#[serde(rename = "app.connected")]AppConnected {@@ -44,6 +47,7 @@ impl NotificationEvent {match self {Self::ReviewDecided(_) => "review.decided",Self::ReviewSettled(_) => "review.settled",+ Self::ImportPlanned(_) => "import.planned",Self::AppConnected { .. } => "app.connected",}}@@ -63,6 +67,105 @@ pub struct ReviewOutcome {pub items: Vec<ReviewItemOutcome>,}+/// How the work a review decision started ended. Untagged so settlements+/// stored before imports existed, bare [`ReviewOutcome`]s, still read.+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, specta::Type)]+#[serde(untagged)]+pub enum ReviewSettlement {+ /// An approved import's background write ended.+ Import {+ /// The import's final report.+ import: ImportSettlement,+ },+ /// The decided actions' confirmed results.+ Actions(ReviewOutcome),+}++/// The final report of an approved workspace import, with no page content.+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, specta::Type)]+#[serde(rename_all = "camelCase")]+pub struct ImportSettlement {+ /// How the write ended.+ pub outcome: ImportOutcome,+ /// Why it failed, or what went wrong in a completed write.+ #[serde(skip_serializing_if = "Option::is_none")]+ pub error: Option<String>,+ /// Objects written into Macro.+ pub imported: u32,+ /// Objects the plan copies, as they are or as task lists.+ pub planned: u32,+ /// Planned documents.+ pub documents: u32,+ /// Planned databases.+ pub databases: u32,+ /// Rows those databases hold.+ pub database_rows: u32,+ /// Planned task lists.+ pub task_lists: u32,+ /// Tasks those task lists become.+ pub task_rows: u32,+ /// Objects left in Notion.+ pub skipped: u32,+ /// Planned objects that were not written.+ pub not_written: u32,+ /// Written objects shared with the owner's team.+ pub shared_with_team: u32,+ /// Objects teammates had already imported, linked instead of copied.+ pub already_in_macro: u32,+ /// Objects a teammate is writing at the same time, linked once written.+ pub waiting_on_teammates: u32,+ /// The Macro folder the import writes into.+ #[serde(skip_serializing_if = "Option::is_none")]+ #[specta(type = Option<String>)]+ pub root_folder_id: Option<Uuid>,+ /// Objects that could not be imported, at most [`MAX_IMPORT_ITEMS`].+ pub failures: Vec<ImportItem>,+ /// Objects left for the owner to handle by hand, at most [`MAX_IMPORT_ITEMS`].+ pub hand_items: Vec<ImportItem>,+}++/// Most items an [`ImportSettlement`] lists per kind, keeping it bounded.+pub const MAX_IMPORT_ITEMS: usize = 50;++/// How an approved import's write ended.+#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, specta::Type)]+#[serde(rename_all = "snake_case")]+pub enum ImportOutcome {+ /// Every planned object was written or accounted for.+ Completed,+ /// The write stopped on an error.+ Failed,+ /// The owner cancelled it.+ Cancelled,+}++/// One object an import report calls out.+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, specta::Type)]+pub struct ImportItem {+ /// The object's title.+ pub title: String,+ /// Its URL in the source workspace.+ #[serde(skip_serializing_if = "Option::is_none")]+ pub url: Option<String>,+ /// Why it is called out.+ #[serde(skip_serializing_if = "Option::is_none")]+ pub reason: Option<String>,+}++/// What an import plan proposes, once it is ready to approve.+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, specta::Type)]+#[serde(rename_all = "camelCase")]+pub struct ImportPlan {+ /// Objects to copy as they are.+ pub copy: u32,+ /// Data sources whose rows become tasks.+ pub task_lists: u32,+ /// Objects to leave in the source workspace.+ pub skipped: u32,+ /// Objects that need handling by hand.+ pub by_hand: u32,+}+/// Overall review decision.#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, specta::Type)]#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]diff --git a/crates/agent_session/src/outbound/notifications/test.rs b/crates/agent_session/src/outbound/notifications/test.rsindex ce080e3447..95f82051a5 100644--- a/crates/agent_session/src/outbound/notifications/test.rs+++ b/crates/agent_session/src/outbound/notifications/test.rs@@ -438,3 +438,71 @@ async fn unreadable_bound_events_keep_the_entire_original_action_for_a_compatiblassert_eq!(recovered.action_id, original.action_id);assert_eq!(recovered.notifications.len(), 2);}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn stored_settlements_read_back_as_the_kind_they_were_written(pool: PgPool) {+ use agent_runtime_protocol::domain::notification::{+ DecisionOutcome, DecisionVia, ImportOutcome, ImportSettlement, ReviewOutcome,+ ReviewSettlement,+ };+ sqlx::raw_sql("INSERT INTO agent_session (id, owner_id, bot_id, model, harness, workspace) VALUES ('00000000-0000-0000-0000-000000000001','macro|[email protected]','00000000-0000-0000-0000-00000000a9e7','test','macrod','/workspace')").execute(&pool).await.unwrap();+ let session = AgentSessionId::new_from_uuid(Uuid::from_u128(1));+ let store = PgNotificationStore(pool.clone());+ let import = ReviewSettlement::Import {+ import: ImportSettlement {+ outcome: ImportOutcome::Cancelled,+ error: Some("Cancelled".into()),+ imported: 1,+ planned: 4,+ documents: 4,+ databases: 0,+ database_rows: 0,+ task_lists: 0,+ task_rows: 0,+ skipped: 0,+ not_written: 3,+ shared_with_team: 0,+ already_in_macro: 0,+ waiting_on_teammates: 0,+ root_folder_id: Some(Uuid::from_u128(9)),+ failures: Vec::new(),+ hand_items: Vec::new(),+ },+ };+ let actions = ReviewSettlement::Actions(ReviewOutcome {+ outcome: DecisionOutcome::Approved,+ by: "macro|[email protected]".into(),+ via: DecisionVia::Card,+ items: Vec::new(),+ });+ for (subject, settlement) in [("import", &import), ("email", &actions)] {+ store+ .enqueue(+ PendingNotification::new(ProduceNotification {+ session_id: session,+ subject_id: subject.into(),+ event: NotificationEvent::ReviewSettled(settlement.clone()),+ dedupe_key: None,+ wake: true,+ })+ .unwrap(),+ )+ .await+ .unwrap();+ }++ let batch = store.claim(session, false).await.unwrap().unwrap();++ let read: Vec<_> = batch+ .notifications+ .iter()+ .map(|notification| notification.event.clone())+ .collect();+ assert_eq!(+ read,+ [+ NotificationEvent::ReviewSettled(import),+ NotificationEvent::ReviewSettled(actions)+ ]+ );+}diff --git a/crates/ai_tools/AGENTS.md b/crates/ai_tools/AGENTS.mdindex 9d945cc67b..1304997b7e 100644--- a/crates/ai_tools/AGENTS.md+++ b/crates/ai_tools/AGENTS.md@@ -19,6 +19,21 @@ execution, display) that don't belong to a single domain.- `src/deferred.rs`: tools whose result arrives after the turn (long-runningwork).- `src/tool_context.rs` + `src/tool_context/`: per-tool context extraction.+- `src/import_workspace.rs`: `ToolEntityCreator` as the import crate's+ `WorkspaceWriter` (folders, documents, tasks, databases as Macro AI for the+ user), sharing its document/task paths with `EntityCreator`; Notion bodies+ go through `DocumentCreator::create_notion_markdown`. `share_with_team`+ shares a folder through `ProjectService::edit_project` (edit level) and a+ document through `DocumentService::set_team_share`; Notion tasks are created+ unshared and reached through their shared folder, while single tracked tasks+ (Linear) stay team-shared at creation.+- `src/import_jobs.rs`: `ImportJobs`, the shutdown token and tracker every+ host passes to `build_tool_service_context_from_env` (or its own import+ service) and calls `hand_over` on at shutdown, so running Notion imports+ release their runs to another process. Those hosts build the import ledger+ with `build_import_repo`, which wires agent-session notifications so the+ session presenting an import's review hears when its plan is ready and+ when its write ends.- `src/bin/gen_tool_schemas.rs`, `src/bin/gen_ai_request_schemas.rs`:schema-generation binaries the frontend consumes.diff --git a/crates/ai_tools/Cargo.toml b/crates/ai_tools/Cargo.tomlindex 72cbec9f70..b2ad7b9eab 100644--- a/crates/ai_tools/Cargo.toml+++ b/crates/ai_tools/Cargo.toml@@ -64,6 +64,7 @@ frecency = { path = "../frecency", features = ["outbound", "postgres"] }futures.workspace = trueimage_generation = { path = "../image_generation" }import = { path = "../import" }+import_notion = { path = "../import_notion" }item_filters = { path = "../item_filters" }lazy_static.workspace = truelexical_client = { path = "../lexical_client" }@@ -82,6 +83,7 @@ model = { path = "../model" }model-entity = { path = "../model-entity" }model_notifications = { path = "../model_notifications" }model_owner = { path = "../model_owner" }+models_databases = { path = "../models_databases" }models_dcs = { path = "../models_dcs" }models_email = { path = "../models_email" }models_pagination = { path = "../models_pagination" }diff --git a/crates/ai_tools/src/build_context.rs b/crates/ai_tools/src/build_context.rsindex d98c9de041..969095f9e3 100644--- a/crates/ai_tools/src/build_context.rs+++ b/crates/ai_tools/src/build_context.rs@@ -166,10 +166,15 @@ pub fn build_cognition_tool_context() -> anyhow::Result<crate::ToolCognitionTool/// `event_task_tracker` tracks event publishes started by the context. Callers/// must retain the original tracker, pass a clone here, and close and drain the/// original after the host stops broker-backed work.-#[tracing::instrument(skip(pool, event_task_tracker, recorder), err)]+///+/// `import_jobs` runs the context's Notion workspace imports; the host calls+/// [`crate::ImportJobs::hand_over`] on shutdown so they move to another+/// process instead of waiting for their leases to lapse.+#[tracing::instrument(skip(pool, event_task_tracker, import_jobs, recorder), err)]pub async fn build_tool_service_context_from_env(pool: sqlx::PgPool,event_task_tracker: TaskTracker,+ import_jobs: crate::ImportJobs,enforcement: ai_usage::AiUsageEnforcement,pricing: ai_billing::AiPricing,recorder: Arc<dyn ai_usage::UsageRecorder>,@@ -567,19 +572,20 @@ pub async fn build_tool_service_context_from_env(let mcp_encryption_key =mcp_client::domain::models::AesKey::try_from(mcp_credentials_key.as_ref()).context("invalid MCP credentials encryption key")?;+ let pipedream_connections = Arc::new(+ pipedream_mcp::outbound::pg_connection_repo::PgConnectionRepo::new(pool.clone()),+ );let mcp_selector = Arc::new(mcp_select::McpToolSelector::new(Arc::new(mcp_client::outbound::pg_server_repo::PgServerRepo::new(pool.clone(),mcp_encryption_key,)),- Arc::new(- pipedream_mcp::outbound::pg_connection_repo::PgConnectionRepo::new(pool.clone()),- ),+ pipedream_connections.clone(),Arc::new(pipedream_client.clone()),));let import_service = Arc::new(import::domain::service::ImportServiceImpl::new(- import::outbound::pg_import_repo::PgImportRepo::new(pool.clone()),+ crate::tool_context::build_import_repo(pool.clone()),mcp_selector.clone(),Arc::new(crate::ToolEntityCreator {document_creator: document_tool_context.creator.clone(),@@ -594,16 +600,26 @@ pub async fn build_tool_service_context_from_env(properties_service.clone(),),team_repository: crate::tool_context::build_team_repository(pool.clone()),+ project_service: project_tool_context.service.clone(),+ databases_service: databases_tool_context.service.clone(),+ document_service: document_tool_context.service.clone(),}),recorder.clone(),).with_slack_source(Arc::new(import::outbound::mcp_slack_source::McpSlackSource::new(mcp_selector),))+ .with_notion_connections(Arc::new(+ import_notion::outbound::pipedream::PipedreamNotionConnections::new(+ pipedream_connections,+ pipedream_client.clone(),+ ),+ )).with_admission(admission.clone()).with_notifier(import::outbound::gateway_notifier::gateway_import_notify(side_effect_clients.connection_gateway.clone(),- )),+ ))+ .with_job_shutdown(import_jobs.shutdown, import_jobs.tracker),);ToolImportToolContext::wired(import_service)} else {diff --git a/crates/ai_tools/src/import_channels/test.rs b/crates/ai_tools/src/import_channels/test.rsindex 5279814276..c20609042a 100644--- a/crates/ai_tools/src/import_channels/test.rs+++ b/crates/ai_tools/src/import_channels/test.rs@@ -12,12 +12,12 @@ use channels::{use import::{domain::{models::{- ImportEntity, ImportSource, ImportStatus, ImportTargetKey, ImportTargetKind,- ImportTargetReservation, Initiator, SlackConversationId,+ ImportEntity, ImportKind, ImportSource, ImportStatus, ImportTargetKey,+ ImportTargetKind, ImportTargetReservation, Initiator, SlackConversationId,},ports::{CanonicalImportRepo, EntityCreator, ImportRepo, ImportedDocumentProperties,- ImportedTaskProperties,+ ImportedTaskProperties, NotionLinks,},service::{ImportService, ImportServiceImpl},},@@ -73,11 +73,12 @@ impl EntityCreator for ReservedCreator {) -> anyhow::Result<String> {unreachable!()}- async fn create_markdown_doc(+ async fn create_notion_document(&self,_: &MacroUserIdStr<'static>,_: &str,_: &str,+ _: &NotionLinks,_: &ImportedDocumentProperties,) -> anyhow::Result<String> {unreachable!()@@ -123,7 +124,8 @@ async fn stage(repo: &PgImportRepo) -> ImportEntity {repo.upsert_staged(&user(),ImportSource::Slack,- Initiator::Onboarding,+ ImportKind::Channel,+ Initiator::Manual,key().foreign_id.as_str(),&serde_json::json!({"name": "engineering", "channel_id": key().foreign_id.as_str()}),)diff --git a/crates/ai_tools/src/import_jobs.rs b/crates/ai_tools/src/import_jobs.rsnew file mode 100644index 0000000000..8b5d9bf3bb--- /dev/null+++ b/crates/ai_tools/src/import_jobs.rs@@ -0,0 +1,44 @@+//! Where a host runs Notion workspace imports, so they outlive its shutdown.++use std::time::Duration;++use tokio_util::sync::CancellationToken;+use tokio_util::task::TaskTracker;++/// How long a host waits for its running imports to hand their runs over.+/// Past it, their leases lapse and another process adopts them anyway.+pub const IMPORT_JOBS_HANDOVER_TIMEOUT: Duration = Duration::from_secs(10);++/// The shutdown signal and tracker a host's import service runs its jobs+/// on. On shutdown the host calls [`ImportJobs::hand_over`]: each running+/// planning or write job stops at its next checkpoint and releases its run,+/// which another process adopts at once.+#[derive(Clone, Default)]+pub struct ImportJobs {+ /// Cancelled when the host shuts down.+ pub shutdown: CancellationToken,+ /// Every import job the host runs.+ pub tracker: TaskTracker,+}++impl ImportJobs {+ /// Jobs for a new host.+ pub fn new() -> Self {+ Self::default()+ }++ /// Stop running imports and wait for them to hand their runs over, up to+ /// [`IMPORT_JOBS_HANDOVER_TIMEOUT`].+ pub async fn hand_over(&self) {+ self.shutdown.cancel();+ self.tracker.close();+ match tokio::time::timeout(IMPORT_JOBS_HANDOVER_TIMEOUT, self.tracker.wait()).await {+ Ok(()) => tracing::info!("import jobs handed over"),+ Err(error) => tracing::warn!(+ error = ?error,+ timeout_seconds = IMPORT_JOBS_HANDOVER_TIMEOUT.as_secs(),+ "timed out handing import jobs over; their leases lapse instead"+ ),+ }+ }+}diff --git a/crates/ai_tools/src/import_workspace.rs b/crates/ai_tools/src/import_workspace.rsnew file mode 100644index 0000000000..b38252eef3--- /dev/null+++ b/crates/ai_tools/src/import_workspace.rs@@ -0,0 +1,366 @@+//! Writes a workspace import (folders, documents, tasks, databases) through+//! the same services the AI tools run on, as Macro AI for the user.++use crate::ToolEntityCreator;+use crate::tool_context::ImportedContent;+use databases::domain::models::{CreateDatabase, DatabaseDetail, DatabaseId, OpBatch, Viewer};+use databases::domain::ports::DatabasesService as _;+use databases::domain::receipt::database_receipt;+use databases::domain::sharing::DatabaseSharingService as _;+use documents::domain::models::DocumentError;+use documents::domain::notion_markdown::{NotionMarkdown, NotionPageLink, NotionUserLink};+use documents::domain::ports::DocumentService as _;+use entity_access::domain::models::{EditAccessLevel, OwnerAccessLevel, ViewAccessLevel};+use entity_access::domain::ports::EntityAccessService as _;+use import::domain::ports::{+ ImportedDocumentProperties, ImportedTaskProperties, NewDatabase, NotionLinks, SharedEntity,+ TeamMember,+};+use macro_user_id::user_id::MacroUserIdStr;+use model::project::request::{CreateProjectRequest, PatchProjectRequestV2};+use models_permissions::share_permission::UpdateSharePermissionRequestV2;+use models_permissions::share_permission::access_level::AccessLevel;+use projects::domain::ports::ProjectService as _;+use uuid::Uuid;++#[cfg(test)]+mod test;++/// The import acts as Macro AI on the user's behalf, like the agent's tools.+fn viewer(user: &MacroUserIdStr<'static>) -> Viewer {+ Viewer {+ user_id: user.clone(),+ acting_bot: Some(bot_id::MACRO_AI_BOT_ID),+ }+}++/// Edit access for the owner's team, and nothing else changed.+fn team_share() -> UpdateSharePermissionRequestV2 {+ UpdateSharePermissionRequestV2 {+ link_share: None,+ link_share_access_level: None,+ team_share_access_level: Some(Some(AccessLevel::Edit)),+ channel_share_permissions: None,+ }+}++/// A Notion page body with the import's links, in the documents vocabulary.+pub(crate) fn notion_markdown(markdown: &str, links: &NotionLinks) -> NotionMarkdown {+ NotionMarkdown {+ markdown: markdown.to_string(),+ pages: links+ .pages+ .iter()+ .map(|(id, page)| {+ let page = NotionPageLink {+ document_id: page.document_id,+ name: page.name.clone(),+ };+ (id.as_str().to_string(), page)+ })+ .collect(),+ users: links+ .users+ .iter()+ .map(|(id, user)| {+ let user = NotionUserLink {+ user_id: user.user_id.as_ref().to_string(),+ email: user.email.clone(),+ };+ (id.as_str().to_string(), user)+ })+ .collect(),+ }+}++/// A document or task the import names by id that exists already was made+/// by an interrupted earlier attempt: it stands, as the starter documents'+/// retries treat it.+fn already_created(created: anyhow::Result<String>, id: Uuid) -> anyhow::Result<String> {+ match created {+ Err(error)+ if matches!(+ error.downcast_ref::<DocumentError>(),+ Some(DocumentError::Conflict(_))+ ) =>+ {+ tracing::info!(document_id = %id, "imported document already exists");+ Ok(id.to_string())+ }+ other => other,+ }+}++/// The starter table a new database comes with and its title column, the+/// only column that table starts with.+fn new_database(detail: &DatabaseDetail) -> anyhow::Result<NewDatabase> {+ let database_id = *detail.database.id.as_uuid();+ let Some(table) = detail.tables.first() else {+ anyhow::bail!("new database {database_id} has no table");+ };+ let Some(title_column) = table.columns.first() else {+ anyhow::bail!("new database {database_id}'s starter table has no column");+ };+ Ok(NewDatabase {+ database_id,+ table: table.table.id,+ title_column: title_column.column.id,+ })+}++impl import::domain::ports::WorkspaceWriter for ToolEntityCreator {+ #[tracing::instrument(skip(self, user, name), err)]+ async fn create_folder(+ &self,+ user: &MacroUserIdStr<'static>,+ name: &str,+ parent: Option<Uuid>,+ ) -> anyhow::Result<Uuid> {+ // Mirrors the CreateProject tool: creating inside a parent requires+ // edit access to it.+ if let Some(parent) = parent {+ self.authorize_folder(user, parent).await?;+ }+ let project = self+ .project_service+ .create_project(+ &Self::creation_principal(user),+ CreateProjectRequest {+ name: name.to_string(),+ project_parent_id: parent,+ },+ )+ .await+ .map_err(|error| anyhow::anyhow!("failed to create imported folder: {error:?}"))?;+ Ok(project.id.parse()?)+ }++ #[tracing::instrument(skip(self, user, name, notion_markdown, links, properties), err)]+ async fn create_document(+ &self,+ user: &MacroUserIdStr<'static>,+ id: Uuid,+ name: &str,+ notion_markdown: &str,+ links: &NotionLinks,+ properties: &ImportedDocumentProperties,+ folder: Option<Uuid>,+ ) -> anyhow::Result<String> {+ let created = self+ .create_imported_document(+ user,+ Some(id),+ name,+ self::notion_markdown(notion_markdown, links),+ properties,+ folder,+ )+ .await;+ already_created(created, id)+ }++ #[tracing::instrument(skip(self, user, name, notion_markdown, links, properties), err)]+ async fn create_task(+ &self,+ user: &MacroUserIdStr<'static>,+ id: Uuid,+ name: &str,+ notion_markdown: &str,+ links: &NotionLinks,+ properties: &ImportedTaskProperties,+ folder: Option<Uuid>,+ ) -> anyhow::Result<String> {+ let created = self+ .create_imported_task(+ user,+ Some(id),+ name,+ ImportedContent::Notion(self::notion_markdown(notion_markdown, links)),+ properties,+ folder,+ // A shared task list shares its folder, which reaches its tasks.+ false,+ )+ .await;+ already_created(created, id)+ }++ #[tracing::instrument(skip(self, user), err)]+ async fn folder_exists(+ &self,+ user: &MacroUserIdStr<'static>,+ folder: Uuid,+ ) -> anyhow::Result<bool> {+ let Ok(receipt) = self+ .entity_access_service+ .generate_entity_access_receipt::<ViewAccessLevel>(+ user,+ None,+ &folder.to_string(),+ model_entity::EntityType::Project,+ )+ .await+ else {+ return Ok(false);+ };+ match self.project_service.get_project(receipt).await {+ Ok(project) => Ok(project.project_metadata.deleted_at.is_none()),+ Err(error) => {+ tracing::warn!(error = ?error, %folder, "imported root folder was not read");+ Ok(false)+ }+ }+ }++ #[tracing::instrument(skip(self, user, name), err)]+ async fn create_database(+ &self,+ user: &MacroUserIdStr<'static>,+ name: &str,+ ) -> anyhow::Result<NewDatabase> {+ let viewer = viewer(user);+ let database = self+ .databases_service+ .create_database(CreateDatabase {+ name: name.to_string(),+ owner_id: user.clone(),+ acting_bot: viewer.acting_bot,+ })+ .await?;+ let receipt = database_receipt::<ViewAccessLevel, _>(+ self.entity_access_service.as_ref(),+ &viewer,+ database.id,+ )+ .await+ .map_err(|error| {+ anyhow::anyhow!("no view access to new database {}: {error:?}", database.id)+ })?;+ let detail = self.databases_service.get_database(receipt).await?;+ new_database(&detail)+ }++ #[tracing::instrument(skip(self, user, ops), fields(op_count = ops.len()), err)]+ async fn apply_database_ops(+ &self,+ user: &MacroUserIdStr<'static>,+ database_id: Uuid,+ ops: Vec<models_databases::DatabaseOp>,+ ) -> anyhow::Result<Vec<models_databases::OpResult>> {+ let viewer = viewer(user);+ let receipt = database_receipt::<EditAccessLevel, _>(+ self.entity_access_service.as_ref(),+ &viewer,+ DatabaseId::from_uuid(database_id),+ )+ .await+ .map_err(|error| anyhow::anyhow!("no edit access to database {database_id}: {error:?}"))?;+ Ok(self+ .databases_service+ .apply_ops(receipt, viewer, OpBatch::from(ops))+ .await?)+ }++ #[tracing::instrument(skip(self, user), err)]+ async fn share_with_team(+ &self,+ user: &MacroUserIdStr<'static>,+ entity: SharedEntity,+ ) -> anyhow::Result<()> {+ match entity {+ SharedEntity::Folder(folder) => {+ let receipt = self+ .entity_access_service+ .generate_entity_access_receipt::<EditAccessLevel>(+ user,+ None,+ &folder.to_string(),+ model_entity::EntityType::Project,+ )+ .await+ .map_err(|error| {+ anyhow::anyhow!("no edit access to folder {folder}: {error:?}")+ })?;+ let project = self+ .project_service+ .internal_get_basic_project(&folder.to_string())+ .await+ .map_err(|error| {+ anyhow::anyhow!("imported folder {folder} was not read: {error:?}")+ })?;+ self.project_service+ .edit_project(+ receipt,+ project,+ PatchProjectRequestV2 {+ name: None,+ project_parent_id: None,+ share_permission: Some(team_share()),+ },+ )+ .await+ .map_err(|error| {+ anyhow::anyhow!("imported folder {folder} was not shared: {error:?}")+ })+ }+ SharedEntity::Document(document) => {+ let receipt = self+ .entity_access_service+ .generate_entity_access_receipt::<EditAccessLevel>(+ user,+ None,+ &document.to_string(),+ model_entity::EntityType::Document,+ )+ .await+ .map_err(|error| {+ anyhow::anyhow!("no edit access to document {document}: {error:?}")+ })?;+ self.document_service+ .set_team_share(receipt, true)+ .await+ .map_err(|error| {+ anyhow::anyhow!("imported document {document} was not shared: {error:?}")+ })?;+ Ok(())+ }+ SharedEntity::Database(database) => {+ let receipt = self+ .entity_access_service+ .generate_entity_access_receipt::<OwnerAccessLevel>(+ user,+ None,+ &database.to_string(),+ model_entity::EntityType::Database,+ )+ .await+ .map_err(|error| {+ anyhow::anyhow!("no owner access to database {database}: {error:?}")+ })?;+ self.databases_service+ .update_share_permissions(receipt, team_share())+ .await+ .map_err(|error| {+ anyhow::anyhow!("imported database {database} was not shared: {error:?}")+ })?;+ Ok(())+ }+ }+ }++ #[tracing::instrument(skip(self, user), err)]+ async fn team_members(+ &self,+ user: &MacroUserIdStr<'static>,+ ) -> anyhow::Result<Vec<TeamMember>> {+ let (_, roster) = self.team_roster(user).await;+ Ok(roster+ .into_iter()+ .map(|user_id| TeamMember {+ // User ids are lowercased when parsed, so their email is too.+ email: user_id.email_str().to_string(),+ user_id,+ })+ .collect())+ }+}diff --git a/crates/ai_tools/src/import_workspace/test.rs b/crates/ai_tools/src/import_workspace/test.rsnew file mode 100644index 0000000000..6521f088d8--- /dev/null+++ b/crates/ai_tools/src/import_workspace/test.rs@@ -0,0 +1,130 @@+use chrono::Utc;+use databases::domain::models::{+ Column, ColumnDetail, Database, DatabaseDetail, Table, TableDetail, TableVersion,+};+use entity_access::domain::models::AccessLevel;+use models_databases::{ColumnId, DatabaseId, TableId};+use models_properties::service::property_definition::PropertyDefinition;+use models_properties::service::property_definition_with_options::PropertyDefinitionWithOptions;+use models_properties::shared::{DataType, PropertyOwner};+use uuid::Uuid;++use super::new_database;++#[test]+fn a_new_database_is_written_into_its_starter_table_and_title_column() {+ let database_id = Uuid::from_u128(0x0dbb_0000_0000_0000_0000_0000_0000_0001);+ let table_id = Uuid::from_u128(0x7ab1_0000_0000_0000_0000_0000_0000_0001);+ let column_id = Uuid::from_u128(0xc01a_0000_0000_0000_0000_0000_0000_0001);+ let detail = DatabaseDetail {+ database: Database {+ id: DatabaseId::from_uuid(database_id),+ name: "Roadmap".to_string(),+ owner_id: "macro|[email protected]".to_string(),+ created_at: Utc::now(),+ trashed_at: None,+ },+ grant: AccessLevel::Owner,+ tables: vec![TableDetail {+ table: Table {+ id: TableId::from_uuid(table_id),+ database_id: DatabaseId::from_uuid(database_id),+ name: "Table 1".to_string(),+ position: "80".parse().expect("valid position"),+ version: TableVersion(1),+ },+ sql_name: "\"Roadmap\".\"Table 1\"".to_string(),+ columns: vec![ColumnDetail {+ column: Column {+ id: ColumnId::from_uuid(column_id),+ table_id: TableId::from_uuid(table_id),+ property_definition_id: Uuid::nil(),+ position: "80".parse().expect("valid position"),+ config: None,+ display_name: None,+ infer_type: true,+ protections: vec![],+ nullable: true,+ },+ sql_name: "\"Name\"".to_string(),+ definition: PropertyDefinitionWithOptions {+ definition: PropertyDefinition {+ id: Uuid::nil(),+ owner: PropertyOwner::Database { database_id },+ display_name: "Name".to_string(),+ data_type: DataType::String,+ is_multi_select: false,+ specific_entity_type: None,+ created_at: Utc::now(),+ updated_at: Utc::now(),+ is_system: false,+ is_metadata: false,+ },+ property_options: vec![],+ },+ writable: true,+ shared_outside_database: false,+ }],+ views: vec![],+ }],+ };++ let created = new_database(&detail).expect("a starter table with a title column");++ assert_eq!(+ created,+ import::domain::ports::NewDatabase {+ database_id,+ table: TableId::from_uuid(table_id),+ title_column: ColumnId::from_uuid(column_id),+ }+ );+}++#[test]+fn a_new_database_without_a_starter_table_is_refused() {+ let database_id = Uuid::from_u128(0x0dbb_0000_0000_0000_0000_0000_0000_0002);+ let detail = DatabaseDetail {+ database: Database {+ id: DatabaseId::from_uuid(database_id),+ name: "Roadmap".to_string(),+ owner_id: "macro|[email protected]".to_string(),+ created_at: Utc::now(),+ trashed_at: None,+ },+ grant: AccessLevel::Owner,+ tables: vec![],+ };++ assert!(new_database(&detail).is_err());+}++#[test]+fn a_new_database_whose_starter_table_has_no_column_is_refused() {+ let database_id = Uuid::from_u128(0x0dbb_0000_0000_0000_0000_0000_0000_0003);+ let table_id = Uuid::from_u128(0x7ab1_0000_0000_0000_0000_0000_0000_0003);+ let detail = DatabaseDetail {+ database: Database {+ id: DatabaseId::from_uuid(database_id),+ name: "Roadmap".to_string(),+ owner_id: "macro|[email protected]".to_string(),+ created_at: Utc::now(),+ trashed_at: None,+ },+ grant: AccessLevel::Owner,+ tables: vec![TableDetail {+ table: Table {+ id: TableId::from_uuid(table_id),+ database_id: DatabaseId::from_uuid(database_id),+ name: "Table 1".to_string(),+ position: "80".parse().expect("valid position"),+ version: TableVersion(1),+ },+ sql_name: "\"Roadmap\".\"Table 1\"".to_string(),+ columns: vec![],+ views: vec![],+ }],+ };++ assert!(new_database(&detail).is_err());+}diff --git a/crates/ai_tools/src/lib.rs b/crates/ai_tools/src/lib.rsindex 856b3144cf..a55a039824 100644--- a/crates/ai_tools/src/lib.rs+++ b/crates/ai_tools/src/lib.rs@@ -17,6 +17,8 @@ mod deferred;mod display_results;pub mod email_attachments;mod import_channels;+mod import_jobs;+mod import_workspace;mod mcp_app_catalog;mod schemas;pub mod search;@@ -67,6 +69,7 @@ pub use build_context::{build_tool_service_context_from_env,};pub use deferred::{DeferredToolSet, deferred_tools};+pub use import_jobs::{IMPORT_JOBS_HANDOVER_TIMEOUT, ImportJobs};pub use mcp_app_catalog::{PipedreamMcpAppCatalog, pipedream_client_from_env};pub use search::search_toolset;pub use tool_context::{@@ -101,11 +104,11 @@ pub use tool_context::{build_channel_tool_context_without_side_effects, build_coding_agent_tool_context,build_crm_tool_context, build_crm_transfer_tool_context, build_databases_sql_tool_context,build_databases_tool_context, build_favorites_service, build_favorites_tool_context,- build_forms_tool_context, build_image_generation_tool_context, build_initiative_tool_context,- build_item_notification_preference_tool_context, build_message_service_with_side_effects,- build_message_service_without_side_effects, build_message_tool_context,- build_pipeline_tool_context, build_project_tool_context, build_properties_service,- build_properties_service_with_broker, build_properties_tool_context,+ build_forms_tool_context, build_image_generation_tool_context, build_import_repo,+ build_initiative_tool_context, build_item_notification_preference_tool_context,+ build_message_service_with_side_effects, build_message_service_without_side_effects,+ build_message_tool_context, build_pipeline_tool_context, build_project_tool_context,+ build_properties_service, build_properties_service_with_broker, build_properties_tool_context,build_read_agent_session_context, build_routine_tool_context, build_skill_tool_context,build_task_properties_adapter, build_team_calendar_tool_context, build_team_repository,build_team_tool_context,diff --git a/crates/ai_tools/src/tool_context.rs b/crates/ai_tools/src/tool_context.rsindex 186d74cbad..16b00c86e8 100644--- a/crates/ai_tools/src/tool_context.rs+++ b/crates/ai_tools/src/tool_context.rs@@ -737,6 +737,17 @@ pub fn build_team_repository(pool: sqlx::PgPool) -> Arc<ToolTeamService> {Arc::new(TeamRepositoryImpl::new(pool))}+/// Build the import ledger for hosts that run Notion workspace imports: it+/// tells the agent session presenting a plan's review when the plan is ready+/// and when its write ends.+pub fn build_import_repo(pool: sqlx::PgPool) -> import::outbound::pg_import_repo::PgImportRepo {+ import::outbound::pg_import_repo::PgImportRepo::new(pool.clone()).with_session_notifications(+ Arc::new(agent_session::outbound::notifications::PgNotificationStore(+ pool,+ )),+ )+}+/// Build the team AI tool context from a Postgres pool.pub fn build_team_tool_context(pool: sqlx::PgPool) -> ToolTeamToolContext {TeamToolContext::new(@@ -1465,6 +1476,14 @@ pub fn build_favorites_tool_context(/// Type alias for the chat tool contextpub type ToolChatToolContext = ChatToolContext<ToolChatService, ToolEntityAccessService>;+/// What an imported document holds.+pub(crate) enum ImportedContent {+ /// Macro markdown.+ Markdown(String),+ /// A Notion page, converted by lexical-service.+ Notion(documents::domain::notion_markdown::NotionMarkdown),+}+/// Creates the Macro entities the import pipeline's fixed mapping calls for/// (linear → task, notion → md, slack → channel), reusing the same document/// creator and channel service the other AI tools run on. Task system@@ -1486,37 +1505,93 @@ pub struct ToolEntityCreator {pub document_properties: ToolDocumentPropertiesApplicator,/// Team roster lookups, to resolve source-tool emails to teammates.pub team_repository: Arc<ToolTeamService>,+ /// Folder creation for workspace imports (shared with project tools).+ pub project_service: Arc<ToolProjectService>,+ /// Database creation and writes for workspace imports (shared with+ /// database tools).+ pub databases_service: Arc<ToolDatabasesService>,+ /// Team sharing of documents workspace imports write (shared with+ /// document tools).+ pub document_service: Arc<ToolDocumentService>,}impl ToolEntityCreator {- fn creation_principal(user: &MacroUserIdStr<'static>) -> model_owner::CreationPrincipal {+ pub(crate) fn creation_principal(+ user: &MacroUserIdStr<'static>,+ ) -> model_owner::CreationPrincipal {model_owner::CreationPrincipal::BotForUser {bot: bot_id::MACRO_AI_BOT_ID,user: user.clone(),}}+ /// Require `user` to hold edit access on `folder`, as the project and+ /// document tools do before creating inside one.+ pub(crate) async fn authorize_folder(+ &self,+ user: &MacroUserIdStr<'static>,+ folder: uuid::Uuid,+ ) -> anyhow::Result<()> {+ self.entity_access_service+ .generate_entity_access_receipt::<EditAccessLevel>(+ user,+ None,+ &folder.to_string(),+ model_entity::EntityType::Project,+ )+ .await+ .map_err(|error| anyhow::anyhow!("no edit access to folder {folder}: {error:?}"))?;+ Ok(())+ }++ #[allow(clippy::too_many_arguments)]async fn create_doc(&self,user: &MacroUserIdStr<'static>,+ id: Option<uuid::Uuid>,name: &str,- markdown: &str,- is_task: bool,- team_id: Option<uuid::Uuid>,+ content: ImportedContent,+ subtype: documents::domain::create::MarkdownSubtype,+ folder: Option<uuid::Uuid>,) -> anyhow::Result<String> {use std::str::FromStr as _;- let document = documents::domain::create::NewPlainTextDocument::builder(- documents::domain::create::NewDocumentMetadata::new(name.to_string()),- )- .file_type(model::document::FileType::from_str("md").expect("md is a valid file type"))- .text(markdown.to_string())- .task_flag(is_task, team_id)- .build()?;+ let mut metadata = documents::domain::create::NewDocumentMetadata::builder(name);+ if let Some(id) = id {+ metadata = metadata.id(id);+ }+ if let Some(folder) = folder {+ self.authorize_folder(user, folder).await?;+ metadata = metadata.project_id(folder);+ }let ai_for_user = Self::creation_principal(user);- let created = self- .document_creator- .create_plain_text(&ai_for_user, document)- .await?;+ let created = match content {+ ImportedContent::Markdown(markdown) => {+ let document =+ documents::domain::create::NewPlainTextDocument::builder(metadata.build())+ .file_type(+ model::document::FileType::from_str("md")+ .expect("md is a valid file type"),+ )+ .text(markdown)+ .markdown_subtype(subtype)+ .build()?;+ self.document_creator+ .create_plain_text(&ai_for_user, document)+ .await?+ }+ ImportedContent::Notion(notion) => {+ self.document_creator+ .create_notion_markdown(+ &ai_for_user,+ documents::domain::create::NewNotionDocument {+ metadata: metadata.build(),+ notion,+ subtype,+ },+ )+ .await?+ }+ };Ok(created.into_response().document_response@@ -1527,7 +1602,7 @@ impl ToolEntityCreator {}/// The user's team id (when they have one) and its member ids.- async fn team_roster(+ pub(crate) async fn team_roster(&self,user: &MacroUserIdStr<'static>,) -> (Option<uuid::Uuid>, Vec<MacroUserIdStr<'static>>) {@@ -1557,6 +1632,87 @@ impl ToolEntityCreator {}}+ /// Create an imported task (with `id` when given), in `folder` when+ /// given, numbered within the user's team and shared with it when+ /// `share_with_team`, then apply its properties best-effort.+ #[expect(+ clippy::too_many_arguments,+ reason = "a task carries its id, content, properties, folder and sharing"+ )]+ pub(crate) async fn create_imported_task(+ &self,+ user: &MacroUserIdStr<'static>,+ id: Option<uuid::Uuid>,+ name: &str,+ content: ImportedContent,+ properties: &import::domain::ports::ImportedTaskProperties,+ folder: Option<uuid::Uuid>,+ share_with_team: bool,+ ) -> anyhow::Result<String> {+ // Same team resolution as the CreateDocument tool, so imported tasks+ // number correctly within the user's team; the roster doubles as the+ // assignee-email lookup. A teamless user gets a private task.+ let (team_id, roster) = self.team_roster(user).await;+ let subtype = documents::domain::create::MarkdownSubtype::Task {+ property_values: None,+ share_with_team: share_with_team && team_id.is_some(),+ team_id,+ };+ let task_id = self+ .create_doc(user, id, name, content, subtype, folder)+ .await?;+ self.apply_task_properties(user, &task_id, properties, &roster)+ .await;+ Ok(task_id)+ }++ /// Create an imported markdown document, in `folder` when given, then+ /// apply its properties best-effort.+ pub(crate) async fn create_imported_document(+ &self,+ user: &MacroUserIdStr<'static>,+ id: Option<uuid::Uuid>,+ name: &str,+ notion: documents::domain::notion_markdown::NotionMarkdown,+ properties: &import::domain::ports::ImportedDocumentProperties,+ folder: Option<uuid::Uuid>,+ ) -> anyhow::Result<String> {+ let document_id = self+ .create_doc(+ user,+ id,+ name,+ ImportedContent::Notion(notion),+ documents::domain::create::MarkdownSubtype::Note,+ folder,+ )+ .await?;+ let access = match self+ .entity_access_service+ .generate_entity_access_receipt::<EditAccessLevel>(+ user,+ None,+ &document_id,+ model_entity::EntityType::Document,+ )+ .await+ {+ Ok(access) => access,+ Err(error) => {+ tracing::warn!(+ document_id,+ error = ?error,+ "failed to authorize imported document properties"+ );+ return Ok(document_id);+ }+ };+ self.document_properties+ .apply(user, &access, &document_id, properties)+ .await;+ Ok(document_id)+ }+/// Apply imported task properties best-effort: each one logs and is/// skipped on failure so a bad label or unknown assignee never sinks/// the import.@@ -1672,48 +1828,37 @@ impl import::domain::ports::EntityCreator for ToolEntityCreator {markdown: &str,properties: &import::domain::ports::ImportedTaskProperties,) -> anyhow::Result<String> {- // Same team resolution as the CreateDocument tool, so imported tasks- // number correctly within the user's team; the roster doubles as the- // assignee-email lookup.- let (team_id, roster) = self.team_roster(user).await;- let task_id = self.create_doc(user, name, markdown, true, team_id).await?;- self.apply_task_properties(user, &task_id, properties, &roster)- .await;- Ok(task_id)+ self.create_imported_task(+ user,+ None,+ name,+ ImportedContent::Markdown(markdown.to_string()),+ properties,+ None,+ // Single tracked tasks (Linear) are shared with the team, as+ // tasks the user makes are.+ true,+ )+ .await}- async fn create_markdown_doc(+ async fn create_notion_document(&self,user: &MacroUserIdStr<'static>,name: &str,- markdown: &str,+ notion_markdown: &str,+ links: &import::domain::ports::NotionLinks,properties: &import::domain::ports::ImportedDocumentProperties,) -> anyhow::Result<String> {- let document_id = self.create_doc(user, name, markdown, false, None).await?;- let access = match self- .entity_access_service- .generate_entity_access_receipt::<EditAccessLevel>(- user,- None,- &document_id,- model_entity::EntityType::Document,- )- .await- {- Ok(access) => access,- Err(error) => {- tracing::warn!(- document_id,- error = ?error,- "failed to authorize imported document properties"- );- return Ok(document_id);- }- };- self.document_properties- .apply(user, &access, &document_id, properties)- .await;- Ok(document_id)+ self.create_imported_document(+ user,+ None,+ name,+ crate::import_workspace::notion_markdown(notion_markdown, links),+ properties,+ None,+ )+ .await}async fn create_channel(@@ -1757,6 +1902,10 @@ pub type ToolImportService = import::domain::service::ImportServiceImpl<ToolMcpSelector,ToolEntityCreator,import::outbound::mcp_slack_source::McpSlackSource<ToolMcpSelector>,+ import_notion::outbound::pipedream::PipedreamNotionConnections<+ pipedream_mcp::outbound::pg_connection_repo::PgConnectionRepo,+ pipedream_mcp::outbound::api::PipedreamClient,+ >,>;/// Type alias for the import tool context, wired by hosts with connector access.diff --git a/crates/code_execution/src/outbound/deno/sdk.mjs b/crates/code_execution/src/outbound/deno/sdk.mjsindex a3aa1798af..4ab3ea1cee 100644--- a/crates/code_execution/src/outbound/deno/sdk.mjs+++ b/crates/code_execution/src/outbound/deno/sdk.mjs@@ -4,7 +4,7 @@ async function gO($,O,j){if(!Number.isInteger(O)||O<1)throw RangeError(`concurre`);N=k.pop()??"";for(let r of k){let p=r.split(``),F=[],y;for(let R of p)if(R.startsWith("data:"))F.push(R.replace(/^data:\s*/,""));else if(R.startsWith("event:"))y=R.replace(/^event:\s*/,"");else if(R.startsWith("id:"))J=R.replace(/^id:\s*/,"");else if(R.startsWith("retry:")){let h=Number.parseInt(R.replace(/^retry:\s*/,""),10);if(!Number.isNaN(h))V=h}let _,v=!1;if(F.length){let R=F.join(`-`);try{_=JSON.parse(R),v=!0}catch{_=R}}if(v){if(S)await S(_);if(x)_=await x(_)}if(j?.({data:_,event:y,id:J,retry:V}),F.length)yield _}}}finally{T.removeEventListener("abort",g),H.releaseLock()}break}catch(D){if(O?.(D),Z!==void 0&&B>=Z)break;let C=Math.min(V*2**(B-1),X??30000);await I(C)}}}()}}var j7=($)=>{switch($){case"label":return".";case"matrix":return";";case"simple":return",";default:return"&"}},x7=($)=>{switch($){case"form":return",";case"pipeDelimited":return"|";case"spaceDelimited":return"%20";default:return","}},S7=($)=>{switch($){case"label":return".";case"matrix":return";";case"simple":return",";default:return"&"}},Cj=({allowReserved:$,explode:O,name:j,style:x,value:S})=>{if(!O){let X=($?S:S.map((G)=>encodeURIComponent(G))).join(x7(x));switch(x){case"label":return`.${X}`;case"matrix":return`;${j}=${X}`;case"simple":return X;default:return`${j}=${X}`}}let K=j7(x),Z=S.map((X)=>{if(x==="label"||x==="simple")return $?X:encodeURIComponent(X);return TO({allowReserved:$,name:j,value:X})}).join(K);return x==="label"||x==="matrix"?K+Z:Z},TO=({allowReserved:$,name:O,value:j})=>{if(j===void 0||j===null)return"";if(typeof j==="object")throw Error("Deeply-nested arrays/objects aren’t supported. Provide your own `querySerializer()` to handle these.");return`${O}=${$?j:encodeURIComponent(j)}`},_j=({allowReserved:$,explode:O,name:j,style:x,value:S,valueOnly:K})=>{if(S instanceof Date)return K?S.toISOString():`${j}=${S.toISOString()}`;if(x!=="deepObject"&&!O){let G=[];Object.entries(S).forEach(([Y,J])=>{G=[...G,Y,$?J:encodeURIComponent(J)]});let L=G.join(",");switch(x){case"form":return`${j}=${L}`;case"label":return`.${L}`;case"matrix":return`;${j}=${L}`;default:return L}}let Z=S7(x),X=Object.entries(S).map(([G,L])=>TO({allowReserved:$,name:x==="deepObject"?`${j}[${G}]`:G,value:L})).join(Z);return x==="label"||x==="matrix"?Z+X:X};var Z7=/\{[^{}]+\}/g,X7=({path:$,url:O})=>{let j=O,x=O.match(Z7);if(x)for(let S of x){let K=!1,Z=S.substring(1,S.length-1),X="simple";if(Z.endsWith("*"))K=!0,Z=Z.substring(0,Z.length-1);if(Z.startsWith("."))Z=Z.substring(1),X="label";else if(Z.startsWith(";"))Z=Z.substring(1),X="matrix";let G=$[Z];if(G===void 0||G===null)continue;if(Array.isArray(G)){j=j.replace(S,Cj({explode:K,name:Z,style:X,value:G}));continue}if(typeof G==="object"){j=j.replace(S,_j({explode:K,name:Z,style:X,value:G,valueOnly:!0}));continue}if(X==="matrix"){j=j.replace(S,`;${TO({name:Z,value:G})}`);continue}let L=encodeURIComponent(X==="label"?`.${G}`:G);j=j.replace(S,L)}return j},u1=({baseUrl:$,path:O,query:j,querySerializer:x,url:S})=>{let K=S.startsWith("/")?S:`/${S}`,Z=($??"")+K;if(O)Z=X7({path:O,url:Z});let X=j?x(j):"";if(X.startsWith("?"))X=X.substring(1);if(X)Z+=`?${X}`;return Z};function F0($){let O=$.body!==void 0;if(O&&$.bodySerializer){if("serializedBody"in $)return $.serializedBody!==void 0&&$.serializedBody!==""?$.serializedBody:null;return $.body!==""?$.body:null}if(O)return $.body;return}var d1=async($,O)=>{let j=typeof O==="function"?await O($):O;if(!j)return;if($.scheme==="bearer")return`Bearer ${j}`;if($.scheme==="basic")return`Basic ${btoa(j)}`;return j};var l1={bodySerializer:($)=>JSON.stringify($,(O,j)=>typeof j==="bigint"?j.toString():j)};var r1=({parameters:$={},...O}={})=>{return(x)=>{let S=[];if(x&&typeof x==="object")for(let K in x){let Z=x[K];if(Z===void 0||Z===null)continue;let X=$[K]||O;if(Array.isArray(Z)){let G=Cj({allowReserved:X.allowReserved,explode:!0,name:K,style:"form",value:Z,...X.array});if(G)S.push(G)}else if(typeof Z==="object"){let G=_j({allowReserved:X.allowReserved,explode:!0,name:K,style:"deepObject",value:Z,...X.object});if(G)S.push(G)}else{let G=TO({allowReserved:X.allowReserved,name:K,value:Z});if(G)S.push(G)}}return S.join("&")}},p1=($)=>{if(!$)return"stream";let O=$.split(";")[0]?.trim();if(!O)return;if(O.startsWith("application/json")||O.endsWith("+json"))return"json";if(O==="multipart/form-data")return"formData";if(["application/","audio/","image/","video/"].some((j)=>O.startsWith(j)))return"blob";if(O.startsWith("text/"))return"text";return},G7=($,O)=>{if(!O)return!1;if($.headers.has(O)||$.query?.[O]||$.headers.get("Cookie")?.includes(`${O}=`))return!0;return!1};async function a1($){for(let O of $.security??[]){if(G7($,O.name))continue;let j=await d1(O,$.auth);if(!j)continue;let x=O.name??"Authorization";switch(O.in){case"query":if(!$.query)$.query={};$.query[x]=j;break;case"cookie":$.headers.append("Cookie",`${x}=${j}`);break;case"header":default:$.headers.set(x,j);break}}}var E0=($)=>u1({baseUrl:$.baseUrl,path:$.path,query:$.query,querySerializer:typeof $.querySerializer==="function"?$.querySerializer:r1($.querySerializer),url:$.url}),w0=($,O)=>{let j={...$,...O};if(j.baseUrl?.endsWith("/"))j.baseUrl=j.baseUrl.substring(0,j.baseUrl.length-1);return j.headers=Mj($.headers,O.headers),j},K7=($)=>{let O=[];return $.forEach((j,x)=>{O.push([x,j])}),O},Mj=(...$)=>{let O=new Headers;for(let j of $){if(!j)continue;let x=j instanceof Headers?K7(j):Object.entries(j);for(let[S,K]of x)if(K===null)O.delete(S);else if(Array.isArray(K))for(let Z of K)O.append(S,Z);else if(K!==void 0)O.set(S,typeof K==="object"?JSON.stringify(K):K)}return O};class Aj{fns=[];clear(){this.fns=[]}eject($){let O=this.getInterceptorIndex($);if(this.fns[O])this.fns[O]=null}exists($){let O=this.getInterceptorIndex($);return Boolean(this.fns[O])}getInterceptorIndex($){if(typeof $==="number")return this.fns[$]?$:-1;return this.fns.indexOf($)}update($,O){let j=this.getInterceptorIndex($);if(this.fns[j])return this.fns[j]=O,$;return!1}use($){return this.fns.push($),this.fns.length-1}}var i1=()=>({error:new Aj,request:new Aj,response:new Aj}),Y7=r1({allowReserved:!1,array:{explode:!0,style:"form"},object:{explode:!0,style:"deepObject"}}),Q7={"Content-Type":"application/json"},iO=($={})=>({...l1,headers:Q7,parseAs:"auto",querySerializer:Y7,...$});var q0=($={})=>{let O=w0(iO(),$),j=()=>({...O}),x=(Y)=>{return O=w0(O,Y),j()},S=i1(),K=async(Y)=>{let J={...O,...Y,fetch:Y.fetch??O.fetch??globalThis.fetch,headers:Mj(O.headers,Y.headers),serializedBody:void 0};if(J.security)await a1(J);if(J.requestValidator)await J.requestValidator(J);if(J.body!==void 0&&J.bodySerializer)J.serializedBody=J.bodySerializer(J.body);if(J.body===void 0||J.serializedBody==="")J.headers.delete("Content-Type");let I=J,z=E0(I);return{opts:I,url:z}},Z=async(Y)=>{let J=Y.throwOnError??O.throwOnError,I=Y.responseStyle??O.responseStyle,z,W;try{let{opts:V,url:B}=await K(Y),T={redirect:"follow",...V,body:F0(V)};z=new Request(B,T);for(let U of S.request.fns)if(U)z=await U(z,V);let P=V.fetch;W=await P(z);for(let U of S.response.fns)if(U)W=await U(W,z,V);let D={request:z,response:W};if(W.ok){let U=(V.parseAs==="auto"?p1(W.headers.get("Content-Type")):V.parseAs)??"json";if(W.status===204||W.headers.get("Content-Length")==="0"){let N;switch(U){case"arrayBuffer":case"blob":case"text":N=await W[U]();break;case"formData":N=new FormData;break;case"stream":N=W.body;break;case"json":default:N={};break}return V.responseStyle==="data"?N:{data:N,...D}}let H;switch(U){case"arrayBuffer":case"blob":case"formData":case"text":H=await W[U]();break;case"json":{let N=await W.text();H=N?JSON.parse(N):{};break}case"stream":return V.responseStyle==="data"?W.body:{data:W.body,...D}}if(U==="json"){if(V.responseValidator)await V.responseValidator(H);if(V.responseTransformer)H=await V.responseTransformer(H)}return V.responseStyle==="data"?H:{data:H,...D}}let C=await W.text(),w;try{w=JSON.parse(C)}catch{}throw w??C}catch(V){let B=V;for(let T of S.error.fns)if(T)B=await T(B,W,z,Y);if(B=B||{},J)throw B;return I==="data"?void 0:{error:B,request:z,response:W}}},X=(Y)=>(J)=>Z({...J,method:Y}),G=(Y)=>async(J)=>{let{opts:I,url:z}=await K(J);return c1({...I,body:I.body,method:Y,onRequest:async(W,V)=>{let B=new Request(W,V);for(let T of S.request.fns)if(T)B=await T(B,I);return B},serializedBody:F0(I),url:z})};return{buildUrl:(Y)=>E0({...O,...Y}),connect:X("CONNECT"),delete:X("DELETE"),get:X("GET"),getConfig:j,head:X("HEAD"),interceptors:S,options:X("OPTIONS"),patch:X("PATCH"),post:X("POST"),put:X("PUT"),request:Z,setConfig:x,sse:{connect:G("CONNECT"),delete:G("DELETE"),get:G("GET"),head:G("HEAD"),options:G("OPTIONS"),patch:G("PATCH"),post:G("POST"),put:G("PUT"),trace:G("TRACE")},trace:X("TRACE")}};var n1=q0(iO());class s1{client;constructor($){this.client=$?.client??n1}}class t1{defaultKey="default";instances=new Map;get($){let O=this.instances.get($??this.defaultKey);if(!O)throw Error('No SDK client found. Create one with "new Sdk()" to fix this error.');return O}set($,O){this.instances.set(O??this.defaultKey,$)}}class Pj extends s1{static __registry=new t1;constructor($){super($);Pj.__registry.set(this,$?.key)}discoverAgentCapabilitiesHandler($){return($.client??this.client).post({security:[{scheme:"bearer",type:"http"}],url:"/agent-capabilities/discover",...$,headers:{"Content-Type":"application/json",...$.headers}})}getAgentCodingPreferences($){return($?.client??this.client).get({url:"/agent-coding-preferences",...$})}putAgentCodingPreferences($){return($.client??this.client).put({url:"/agent-coding-preferences",...$,headers:{"Content-Type":"application/json",...$.headers}})}listAgentConversations($){return($.client??this.client).get({security:[{scheme:"bearer",type:"http"}],url:"/agent-conversations/{channel_id}",...$})}startFreshAgentConversation($){return($.client??this.client).post({security:[{scheme:"bearer",type:"http"}],url:"/agent-conversations/{channel_id}/{bot_id}/start-fresh",...$,headers:{"Content-Type":"application/json",...$.headers}})}retryAgentConversationTurn($){return($.client??this.client).post({security:[{scheme:"bearer",type:"http"}],url:"/agent-conversations/{channel_id}/{bot_id}/turns/{source}/retry",...$,headers:{"Content-Type":"application/json",...$.headers}})}loadAgentModelsHandler($){return($.client??this.client).post({security:[{scheme:"bearer",type:"http"}],url:"/agent-models/load",...$,headers:{"Content-Type":"application/json",...$.headers}})}listAgentRepositories($){return($?.client??this.client).get({security:[{scheme:"bearer",type:"http"}],url:"/agent-repositories",...$})}listAgentRepositoryBranches($){return($.client??this.client).get({security:[{scheme:"bearer",type:"http"}],url:"/agent-repositories/branches",...$})}getAgentSandboxSize($){return($?.client??this.client).get({url:"/agent-sandbox-size",...$})}putAgentSandboxSize($){return($.client??this.client).put({url:"/agent-sandbox-size",...$,headers:{"Content-Type":"application/json",...$.headers}})}createAgentSession($){return($.client??this.client).post({url:"/agent-sessions",...$,headers:{"Content-Type":"application/json",...$.headers}})}agentSessionsForPullRequest($){return($.client??this.client).post({url:"/agent-sessions/by-pull-request",...$,headers:{"Content-Type":"application/json",...$.headers}})}agentSessionsForPullRequests($){return($.client??this.client).post({url:"/agent-sessions/by-pull-requests",...$,headers:{"Content-Type":"application/json",...$.headers}})}previewAgentSessions($){return($.client??this.client).post({url:"/agent-sessions/preview",...$,headers:{"Content-Type":"application/json",...$.headers}})}warmAgentSessionHandler($){return($.client??this.client).post({url:"/agent-sessions/warm",...$,headers:{"Content-Type":"application/json",...$.headers}})}deleteAgentSession($){return($.client??this.client).delete({url:"/agent-sessions/{session_id}",...$})}getAgentSession($){return($.client??this.client).get({url:"/agent-sessions/{session_id}",...$})}setAgentSessionArchived($){return($.client??this.client).put({url:"/agent-sessions/{session_id}/archived",...$,headers:{"Content-Type":"application/json",...$.headers}})}getAgentSessionChanges($){return($.client??this.client).get({url:"/agent-sessions/{session_id}/changes",...$})}getAgentSessionChangesPatch($){return($.client??this.client).get({url:"/agent-sessions/{session_id}/changes/patch",...$})}refreshAgentSessionChanges($){return($.client??this.client).post({url:"/agent-sessions/{session_id}/changes/refresh",...$})}getCodeExecution($){return($.client??this.client).get({url:"/agent-sessions/{session_id}/code-executions/{execution_id}",...$})}listExecutionReviews($){return($.client??this.client).get({url:"/agent-sessions/{session_id}/code-executions/{execution_id}/reviews",...$})}controlAgentSession($){return($.client??this.client).post({url:"/agent-sessions/{session_id}/control",...$,headers:{"Content-Type":"application/json",...$.headers}})}getAgentSessionLog($){return($.client??this.client).get({url:"/agent-sessions/{session_id}/log",...$})}getAgentSessionFoldedLog($){return($.client??this.client).get({url:"/agent-sessions/{session_id}/log/folded",...$})}renameAgentSession($){return($.client??this.client).put({url:"/agent-sessions/{session_id}/name",...$,headers:{"Content-Type":"application/json",...$.headers}})}getAgentSessionPermissions($){return($.client??this.client).get({url:"/agent-sessions/{session_id}/permissions",...$})}updateAgentSessionPermissions($){return($.client??this.client).patch({url:"/agent-sessions/{session_id}/permissions",...$,headers:{"Content-Type":"application/json",...$.headers}})}unlinkAgentSessionPullRequest($){return($.client??this.client).delete({url:"/agent-sessions/{session_id}/pull-requests",...$})}listAgentSessionPullRequests($){return($.client??this.client).get({url:"/agent-sessions/{session_id}/pull-requests",...$})}linkAgentSessionPullRequest($){return($.client??this.client).put({url:"/agent-sessions/{session_id}/pull-requests",...$,headers:{"Content-Type":"application/json",...$.headers}})}getAgentSessionQueue($){return($.client??this.client).get({url:"/agent-sessions/{session_id}/queue",...$})}removeQueuedAction($){return($.client??this.client).delete({url:"/agent-sessions/{session_id}/queue/{action_id}",...$})}editQueuedAction($){return($.client??this.client).put({url:"/agent-sessions/{session_id}/queue/{action_id}",...$,headers:{"Content-Type":"application/json",...$.headers}})}steerQueuedAction($){return($.client??this.client).post({url:"/agent-sessions/{session_id}/queue/{action_id}/steer",...$})}createSessionReview($){return($.client??this.client).post({url:"/agent-sessions/{session_id}/reviews",...$,headers:{"Content-Type":"application/json",...$.headers}})}getSessionReview($){return($.client??this.client).get({url:"/agent-sessions/{session_id}/reviews/{review_id}",...$})}decideSessionReview($){return($.client??this.client).post({url:"/agent-sessions/{session_id}/reviews/{review_id}",...$,headers:{"Content-Type":"application/json",...$.headers}})}putAgentSessionSandboxSize($){return($.client??this.client).put({url:"/agent-sessions/{session_id}/sandbox-size",...$,headers:{"Content-Type":"application/json",...$.headers}})}answerAgentSessionToolApproval($){return($.client??this.client).post({url:"/agent-sessions/{session_id}/tool-approvals/{approval_id}",...$,headers:{"Content-Type":"application/json",...$.headers}})}}function e1({onRequest:$,onSseError:O,onSseEvent:j,responseTransformer:x,responseValidator:S,sseDefaultRetryDelay:K,sseMaxRetryAttempts:Z,sseMaxRetryDelay:X,sseSleepFn:G,url:L,...Y}){let J,I=G??((V)=>new Promise((B)=>setTimeout(B,V)));return{stream:async function*(){let V=K??3000,B=0,T=Y.signal??new AbortController().signal;while(!0){if(T.aborted)break;B++;let P=Y.headers instanceof Headers?Y.headers:new Headers(Y.headers);if(J!==void 0)P.set("Last-Event-ID",J);try{let D={redirect:"follow",...Y,body:Y.serializedBody,headers:P,signal:T},C=new Request(L,D);if($)C=await $(L,D);let U=await(Y.fetch??globalThis.fetch)(C);if(!U.ok)throw Error(`SSE failed: ${U.status} ${U.statusText}`);if(!U.body)throw Error("No body in SSE response");let H=U.body.pipeThrough(new TextDecoderStream).getReader(),N="",g=()=>{try{H.cancel()}catch{}};T.addEventListener("abort",g);try{while(!0){let{done:d,value:l}=await H.read();if(d)break;N+=l,N=N.replace(/\r\n?/g,`+`);try{_=JSON.parse(R),v=!0}catch{_=R}}if(v){if(S)await S(_);if(x)_=await x(_)}if(j?.({data:_,event:y,id:J,retry:V}),F.length)yield _}}}finally{T.removeEventListener("abort",g),H.releaseLock()}break}catch(D){if(O?.(D),Z!==void 0&&B>=Z)break;let C=Math.min(V*2**(B-1),X??30000);await I(C)}}}()}}var j7=($)=>{switch($){case"label":return".";case"matrix":return";";case"simple":return",";default:return"&"}},x7=($)=>{switch($){case"form":return",";case"pipeDelimited":return"|";case"spaceDelimited":return"%20";default:return","}},S7=($)=>{switch($){case"label":return".";case"matrix":return";";case"simple":return",";default:return"&"}},Cj=({allowReserved:$,explode:O,name:j,style:x,value:S})=>{if(!O){let X=($?S:S.map((G)=>encodeURIComponent(G))).join(x7(x));switch(x){case"label":return`.${X}`;case"matrix":return`;${j}=${X}`;case"simple":return X;default:return`${j}=${X}`}}let K=j7(x),Z=S.map((X)=>{if(x==="label"||x==="simple")return $?X:encodeURIComponent(X);return TO({allowReserved:$,name:j,value:X})}).join(K);return x==="label"||x==="matrix"?K+Z:Z},TO=({allowReserved:$,name:O,value:j})=>{if(j===void 0||j===null)return"";if(typeof j==="object")throw Error("Deeply-nested arrays/objects aren’t supported. Provide your own `querySerializer()` to handle these.");return`${O}=${$?j:encodeURIComponent(j)}`},_j=({allowReserved:$,explode:O,name:j,style:x,value:S,valueOnly:K})=>{if(S instanceof Date)return K?S.toISOString():`${j}=${S.toISOString()}`;if(x!=="deepObject"&&!O){let G=[];Object.entries(S).forEach(([Y,J])=>{G=[...G,Y,$?J:encodeURIComponent(J)]});let L=G.join(",");switch(x){case"form":return`${j}=${L}`;case"label":return`.${L}`;case"matrix":return`;${j}=${L}`;default:return L}}let Z=S7(x),X=Object.entries(S).map(([G,L])=>TO({allowReserved:$,name:x==="deepObject"?`${j}[${G}]`:G,value:L})).join(Z);return x==="label"||x==="matrix"?Z+X:X};var Z7=/\{[^{}]+\}/g,X7=({path:$,url:O})=>{let j=O,x=O.match(Z7);if(x)for(let S of x){let K=!1,Z=S.substring(1,S.length-1),X="simple";if(Z.endsWith("*"))K=!0,Z=Z.substring(0,Z.length-1);if(Z.startsWith("."))Z=Z.substring(1),X="label";else if(Z.startsWith(";"))Z=Z.substring(1),X="matrix";let G=$[Z];if(G===void 0||G===null)continue;if(Array.isArray(G)){j=j.replace(S,Cj({explode:K,name:Z,style:X,value:G}));continue}if(typeof G==="object"){j=j.replace(S,_j({explode:K,name:Z,style:X,value:G,valueOnly:!0}));continue}if(X==="matrix"){j=j.replace(S,`;${TO({name:Z,value:G})}`);continue}let L=encodeURIComponent(X==="label"?`.${G}`:G);j=j.replace(S,L)}return j},u1=({baseUrl:$,path:O,query:j,querySerializer:x,url:S})=>{let K=S.startsWith("/")?S:`/${S}`,Z=($??"")+K;if(O)Z=X7({path:O,url:Z});let X=j?x(j):"";if(X.startsWith("?"))X=X.substring(1);if(X)Z+=`?${X}`;return Z};function F0($){let O=$.body!==void 0;if(O&&$.bodySerializer){if("serializedBody"in $)return $.serializedBody!==void 0&&$.serializedBody!==""?$.serializedBody:null;return $.body!==""?$.body:null}if(O)return $.body;return}var d1=async($,O)=>{let j=typeof O==="function"?await O($):O;if(!j)return;if($.scheme==="bearer")return`Bearer ${j}`;if($.scheme==="basic")return`Basic ${btoa(j)}`;return j};var l1={bodySerializer:($)=>JSON.stringify($,(O,j)=>typeof j==="bigint"?j.toString():j)};var r1=({parameters:$={},...O}={})=>{return(x)=>{let S=[];if(x&&typeof x==="object")for(let K in x){let Z=x[K];if(Z===void 0||Z===null)continue;let X=$[K]||O;if(Array.isArray(Z)){let G=Cj({allowReserved:X.allowReserved,explode:!0,name:K,style:"form",value:Z,...X.array});if(G)S.push(G)}else if(typeof Z==="object"){let G=_j({allowReserved:X.allowReserved,explode:!0,name:K,style:"deepObject",value:Z,...X.object});if(G)S.push(G)}else{let G=TO({allowReserved:X.allowReserved,name:K,value:Z});if(G)S.push(G)}}return S.join("&")}},p1=($)=>{if(!$)return"stream";let O=$.split(";")[0]?.trim();if(!O)return;if(O.startsWith("application/json")||O.endsWith("+json"))return"json";if(O==="multipart/form-data")return"formData";if(["application/","audio/","image/","video/"].some((j)=>O.startsWith(j)))return"blob";if(O.startsWith("text/"))return"text";return},G7=($,O)=>{if(!O)return!1;if($.headers.has(O)||$.query?.[O]||$.headers.get("Cookie")?.includes(`${O}=`))return!0;return!1};async function a1($){for(let O of $.security??[]){if(G7($,O.name))continue;let j=await d1(O,$.auth);if(!j)continue;let x=O.name??"Authorization";switch(O.in){case"query":if(!$.query)$.query={};$.query[x]=j;break;case"cookie":$.headers.append("Cookie",`${x}=${j}`);break;case"header":default:$.headers.set(x,j);break}}}var E0=($)=>u1({baseUrl:$.baseUrl,path:$.path,query:$.query,querySerializer:typeof $.querySerializer==="function"?$.querySerializer:r1($.querySerializer),url:$.url}),w0=($,O)=>{let j={...$,...O};if(j.baseUrl?.endsWith("/"))j.baseUrl=j.baseUrl.substring(0,j.baseUrl.length-1);return j.headers=Mj($.headers,O.headers),j},K7=($)=>{let O=[];return $.forEach((j,x)=>{O.push([x,j])}),O},Mj=(...$)=>{let O=new Headers;for(let j of $){if(!j)continue;let x=j instanceof Headers?K7(j):Object.entries(j);for(let[S,K]of x)if(K===null)O.delete(S);else if(Array.isArray(K))for(let Z of K)O.append(S,Z);else if(K!==void 0)O.set(S,typeof K==="object"?JSON.stringify(K):K)}return O};class Aj{fns=[];clear(){this.fns=[]}eject($){let O=this.getInterceptorIndex($);if(this.fns[O])this.fns[O]=null}exists($){let O=this.getInterceptorIndex($);return Boolean(this.fns[O])}getInterceptorIndex($){if(typeof $==="number")return this.fns[$]?$:-1;return this.fns.indexOf($)}update($,O){let j=this.getInterceptorIndex($);if(this.fns[j])return this.fns[j]=O,$;return!1}use($){return this.fns.push($),this.fns.length-1}}var i1=()=>({error:new Aj,request:new Aj,response:new Aj}),Y7=r1({allowReserved:!1,array:{explode:!0,style:"form"},object:{explode:!0,style:"deepObject"}}),Q7={"Content-Type":"application/json"},iO=($={})=>({...l1,headers:Q7,parseAs:"auto",querySerializer:Y7,...$});var q0=($={})=>{let O=w0(iO(),$),j=()=>({...O}),x=(Y)=>{return O=w0(O,Y),j()},S=i1(),K=async(Y)=>{let J={...O,...Y,fetch:Y.fetch??O.fetch??globalThis.fetch,headers:Mj(O.headers,Y.headers),serializedBody:void 0};if(J.security)await a1(J);if(J.requestValidator)await J.requestValidator(J);if(J.body!==void 0&&J.bodySerializer)J.serializedBody=J.bodySerializer(J.body);if(J.body===void 0||J.serializedBody==="")J.headers.delete("Content-Type");let I=J,z=E0(I);return{opts:I,url:z}},Z=async(Y)=>{let J=Y.throwOnError??O.throwOnError,I=Y.responseStyle??O.responseStyle,z,W;try{let{opts:V,url:B}=await K(Y),T={redirect:"follow",...V,body:F0(V)};z=new Request(B,T);for(let U of S.request.fns)if(U)z=await U(z,V);let P=V.fetch;W=await P(z);for(let U of S.response.fns)if(U)W=await U(W,z,V);let D={request:z,response:W};if(W.ok){let U=(V.parseAs==="auto"?p1(W.headers.get("Content-Type")):V.parseAs)??"json";if(W.status===204||W.headers.get("Content-Length")==="0"){let N;switch(U){case"arrayBuffer":case"blob":case"text":N=await W[U]();break;case"formData":N=new FormData;break;case"stream":N=W.body;break;case"json":default:N={};break}return V.responseStyle==="data"?N:{data:N,...D}}let H;switch(U){case"arrayBuffer":case"blob":case"formData":case"text":H=await W[U]();break;case"json":{let N=await W.text();H=N?JSON.parse(N):{};break}case"stream":return V.responseStyle==="data"?W.body:{data:W.body,...D}}if(U==="json"){if(V.responseValidator)await V.responseValidator(H);if(V.responseTransformer)H=await V.responseTransformer(H)}return V.responseStyle==="data"?H:{data:H,...D}}let C=await W.text(),w;try{w=JSON.parse(C)}catch{}throw w??C}catch(V){let B=V;for(let T of S.error.fns)if(T)B=await T(B,W,z,Y);if(B=B||{},J)throw B;return I==="data"?void 0:{error:B,request:z,response:W}}},X=(Y)=>(J)=>Z({...J,method:Y}),G=(Y)=>async(J)=>{let{opts:I,url:z}=await K(J);return c1({...I,body:I.body,method:Y,onRequest:async(W,V)=>{let B=new Request(W,V);for(let T of S.request.fns)if(T)B=await T(B,I);return B},serializedBody:F0(I),url:z})};return{buildUrl:(Y)=>E0({...O,...Y}),connect:X("CONNECT"),delete:X("DELETE"),get:X("GET"),getConfig:j,head:X("HEAD"),interceptors:S,options:X("OPTIONS"),patch:X("PATCH"),post:X("POST"),put:X("PUT"),request:Z,setConfig:x,sse:{connect:G("CONNECT"),delete:G("DELETE"),get:G("GET"),head:G("HEAD"),options:G("OPTIONS"),patch:G("PATCH"),post:G("POST"),put:G("PUT"),trace:G("TRACE")},trace:X("TRACE")}};var n1=q0(iO());class s1{client;constructor($){this.client=$?.client??n1}}class t1{defaultKey="default";instances=new Map;get($){let O=this.instances.get($??this.defaultKey);if(!O)throw Error('No SDK client found. Create one with "new Sdk()" to fix this error.');return O}set($,O){this.instances.set(O??this.defaultKey,$)}}class Pj extends s1{static __registry=new t1;constructor($){super($);Pj.__registry.set(this,$?.key)}discoverAgentCapabilitiesHandler($){return($.client??this.client).post({security:[{scheme:"bearer",type:"http"}],url:"/agent-capabilities/discover",...$,headers:{"Content-Type":"application/json",...$.headers}})}getAgentCodingPreferences($){return($?.client??this.client).get({url:"/agent-coding-preferences",...$})}putAgentCodingPreferences($){return($.client??this.client).put({url:"/agent-coding-preferences",...$,headers:{"Content-Type":"application/json",...$.headers}})}listAgentConversations($){return($.client??this.client).get({security:[{scheme:"bearer",type:"http"}],url:"/agent-conversations/{channel_id}",...$})}startFreshAgentConversation($){return($.client??this.client).post({security:[{scheme:"bearer",type:"http"}],url:"/agent-conversations/{channel_id}/{bot_id}/start-fresh",...$,headers:{"Content-Type":"application/json",...$.headers}})}retryAgentConversationTurn($){return($.client??this.client).post({security:[{scheme:"bearer",type:"http"}],url:"/agent-conversations/{channel_id}/{bot_id}/turns/{source}/retry",...$,headers:{"Content-Type":"application/json",...$.headers}})}loadAgentModelsHandler($){return($.client??this.client).post({security:[{scheme:"bearer",type:"http"}],url:"/agent-models/load",...$,headers:{"Content-Type":"application/json",...$.headers}})}listAgentRepositories($){return($?.client??this.client).get({security:[{scheme:"bearer",type:"http"}],url:"/agent-repositories",...$})}listAgentRepositoryBranches($){return($.client??this.client).get({security:[{scheme:"bearer",type:"http"}],url:"/agent-repositories/branches",...$})}getAgentSandboxSize($){return($?.client??this.client).get({url:"/agent-sandbox-size",...$})}putAgentSandboxSize($){return($.client??this.client).put({url:"/agent-sandbox-size",...$,headers:{"Content-Type":"application/json",...$.headers}})}createAgentSession($){return($.client??this.client).post({url:"/agent-sessions",...$,headers:{"Content-Type":"application/json",...$.headers}})}agentSessionsForPullRequest($){return($.client??this.client).post({url:"/agent-sessions/by-pull-request",...$,headers:{"Content-Type":"application/json",...$.headers}})}agentSessionsForPullRequests($){return($.client??this.client).post({url:"/agent-sessions/by-pull-requests",...$,headers:{"Content-Type":"application/json",...$.headers}})}previewAgentSessions($){return($.client??this.client).post({url:"/agent-sessions/preview",...$,headers:{"Content-Type":"application/json",...$.headers}})}warmAgentSessionHandler($){return($.client??this.client).post({url:"/agent-sessions/warm",...$,headers:{"Content-Type":"application/json",...$.headers}})}deleteAgentSession($){return($.client??this.client).delete({url:"/agent-sessions/{session_id}",...$})}getAgentSession($){return($.client??this.client).get({url:"/agent-sessions/{session_id}",...$})}reportAgentSessionAppConnected($){return($.client??this.client).post({url:"/agent-sessions/{session_id}/app-connections",...$,headers:{"Content-Type":"application/json",...$.headers}})}setAgentSessionArchived($){return($.client??this.client).put({url:"/agent-sessions/{session_id}/archived",...$,headers:{"Content-Type":"application/json",...$.headers}})}getAgentSessionChanges($){return($.client??this.client).get({url:"/agent-sessions/{session_id}/changes",...$})}getAgentSessionChangesPatch($){return($.client??this.client).get({url:"/agent-sessions/{session_id}/changes/patch",...$})}refreshAgentSessionChanges($){return($.client??this.client).post({url:"/agent-sessions/{session_id}/changes/refresh",...$})}getCodeExecution($){return($.client??this.client).get({url:"/agent-sessions/{session_id}/code-executions/{execution_id}",...$})}listExecutionReviews($){return($.client??this.client).get({url:"/agent-sessions/{session_id}/code-executions/{execution_id}/reviews",...$})}controlAgentSession($){return($.client??this.client).post({url:"/agent-sessions/{session_id}/control",...$,headers:{"Content-Type":"application/json",...$.headers}})}getAgentSessionLog($){return($.client??this.client).get({url:"/agent-sessions/{session_id}/log",...$})}getAgentSessionFoldedLog($){return($.client??this.client).get({url:"/agent-sessions/{session_id}/log/folded",...$})}renameAgentSession($){return($.client??this.client).put({url:"/agent-sessions/{session_id}/name",...$,headers:{"Content-Type":"application/json",...$.headers}})}getAgentSessionPermissions($){return($.client??this.client).get({url:"/agent-sessions/{session_id}/permissions",...$})}updateAgentSessionPermissions($){return($.client??this.client).patch({url:"/agent-sessions/{session_id}/permissions",...$,headers:{"Content-Type":"application/json",...$.headers}})}unlinkAgentSessionPullRequest($){return($.client??this.client).delete({url:"/agent-sessions/{session_id}/pull-requests",...$})}listAgentSessionPullRequests($){return($.client??this.client).get({url:"/agent-sessions/{session_id}/pull-requests",...$})}linkAgentSessionPullRequest($){return($.client??this.client).put({url:"/agent-sessions/{session_id}/pull-requests",...$,headers:{"Content-Type":"application/json",...$.headers}})}getAgentSessionQueue($){return($.client??this.client).get({url:"/agent-sessions/{session_id}/queue",...$})}removeQueuedAction($){return($.client??this.client).delete({url:"/agent-sessions/{session_id}/queue/{action_id}",...$})}editQueuedAction($){return($.client??this.client).put({url:"/agent-sessions/{session_id}/queue/{action_id}",...$,headers:{"Content-Type":"application/json",...$.headers}})}steerQueuedAction($){return($.client??this.client).post({url:"/agent-sessions/{session_id}/queue/{action_id}/steer",...$})}createSessionReview($){return($.client??this.client).post({url:"/agent-sessions/{session_id}/reviews",...$,headers:{"Content-Type":"application/json",...$.headers}})}getSessionReview($){return($.client??this.client).get({url:"/agent-sessions/{session_id}/reviews/{review_id}",...$})}decideSessionReview($){return($.client??this.client).post({url:"/agent-sessions/{session_id}/reviews/{review_id}",...$,headers:{"Content-Type":"application/json",...$.headers}})}putAgentSessionSandboxSize($){return($.client??this.client).put({url:"/agent-sessions/{session_id}/sandbox-size",...$,headers:{"Content-Type":"application/json",...$.headers}})}answerAgentSessionToolApproval($){return($.client??this.client).post({url:"/agent-sessions/{session_id}/tool-approvals/{approval_id}",...$,headers:{"Content-Type":"application/json",...$.headers}})}}function e1({onRequest:$,onSseError:O,onSseEvent:j,responseTransformer:x,responseValidator:S,sseDefaultRetryDelay:K,sseMaxRetryAttempts:Z,sseMaxRetryDelay:X,sseSleepFn:G,url:L,...Y}){let J,I=G??((V)=>new Promise((B)=>setTimeout(B,V)));return{stream:async function*(){let V=K??3000,B=0,T=Y.signal??new AbortController().signal;while(!0){if(T.aborted)break;B++;let P=Y.headers instanceof Headers?Y.headers:new Headers(Y.headers);if(J!==void 0)P.set("Last-Event-ID",J);try{let D={redirect:"follow",...Y,body:Y.serializedBody,headers:P,signal:T},C=new Request(L,D);if($)C=await $(L,D);let U=await(Y.fetch??globalThis.fetch)(C);if(!U.ok)throw Error(`SSE failed: ${U.status} ${U.statusText}`);if(!U.body)throw Error("No body in SSE response");let H=U.body.pipeThrough(new TextDecoderStream).getReader(),N="",g=()=>{try{H.cancel()}catch{}};T.addEventListener("abort",g);try{while(!0){let{done:d,value:l}=await H.read();if(d)break;N+=l,N=N.replace(/\r\n?/g,``);let k=N.split(``);N=k.pop()??"";for(let r of k){let p=r.split(`@@ -19,7 +19,7 @@ async function gO($,O,j){if(!Number.isInteger(O)||O<1)throw RangeError(`concurre`);N=k.pop()??"";for(let r of k){let p=r.split(``),F=[],y;for(let R of p)if(R.startsWith("data:"))F.push(R.replace(/^data:\s*/,""));else if(R.startsWith("event:"))y=R.replace(/^event:\s*/,"");else if(R.startsWith("id:"))J=R.replace(/^id:\s*/,"");else if(R.startsWith("retry:")){let h=Number.parseInt(R.replace(/^retry:\s*/,""),10);if(!Number.isNaN(h))V=h}let _,v=!1;if(F.length){let R=F.join(`-`);try{_=JSON.parse(R),v=!0}catch{_=R}}if(v){if(S)await S(_);if(x)_=await x(_)}if(j?.({data:_,event:y,id:J,retry:V}),F.length)yield _}}}finally{T.removeEventListener("abort",g),H.releaseLock()}break}catch(D){if(O?.(D),Z!==void 0&&B>=Z)break;let C=Math.min(V*2**(B-1),X??30000);await I(C)}}}()}}var F7=($)=>{switch($){case"label":return".";case"matrix":return";";case"simple":return",";default:return"&"}},E7=($)=>{switch($){case"form":return",";case"pipeDelimited":return"|";case"spaceDelimited":return"%20";default:return","}},w7=($)=>{switch($){case"label":return".";case"matrix":return";";case"simple":return",";default:return"&"}},hj=({allowReserved:$,explode:O,name:j,style:x,value:S})=>{if(!O){let X=($?S:S.map((G)=>encodeURIComponent(G))).join(E7(x));switch(x){case"label":return`.${X}`;case"matrix":return`;${j}=${X}`;case"simple":return X;default:return`${j}=${X}`}}let K=F7(x),Z=S.map((X)=>{if(x==="label"||x==="simple")return $?X:encodeURIComponent(X);return DO({allowReserved:$,name:j,value:X})}).join(K);return x==="label"||x==="matrix"?K+Z:Z},DO=({allowReserved:$,name:O,value:j})=>{if(j===void 0||j===null)return"";if(typeof j==="object")throw Error("Deeply-nested arrays/objects aren’t supported. Provide your own `querySerializer()` to handle these.");return`${O}=${$?j:encodeURIComponent(j)}`},mj=({allowReserved:$,explode:O,name:j,style:x,value:S,valueOnly:K})=>{if(S instanceof Date)return K?S.toISOString():`${j}=${S.toISOString()}`;if(x!=="deepObject"&&!O){let G=[];Object.entries(S).forEach(([Y,J])=>{G=[...G,Y,$?J:encodeURIComponent(J)]});let L=G.join(",");switch(x){case"form":return`${j}=${L}`;case"label":return`.${L}`;case"matrix":return`;${j}=${L}`;default:return L}}let Z=w7(x),X=Object.entries(S).map(([G,L])=>DO({allowReserved:$,name:x==="deepObject"?`${j}[${G}]`:G,value:L})).join(Z);return x==="label"||x==="matrix"?Z+X:X};var q7=/\{[^{}]+\}/g,f7=({path:$,url:O})=>{let j=O,x=O.match(q7);if(x)for(let S of x){let K=!1,Z=S.substring(1,S.length-1),X="simple";if(Z.endsWith("*"))K=!0,Z=Z.substring(0,Z.length-1);if(Z.startsWith("."))Z=Z.substring(1),X="label";else if(Z.startsWith(";"))Z=Z.substring(1),X="matrix";let G=$[Z];if(G===void 0||G===null)continue;if(Array.isArray(G)){j=j.replace(S,hj({explode:K,name:Z,style:X,value:G}));continue}if(typeof G==="object"){j=j.replace(S,mj({explode:K,name:Z,style:X,value:G,valueOnly:!0}));continue}if(X==="matrix"){j=j.replace(S,`;${DO({name:Z,value:G})}`);continue}let L=encodeURIComponent(X==="label"?`.${G}`:G);j=j.replace(S,L)}return j},HS=({baseUrl:$,path:O,query:j,querySerializer:x,url:S})=>{let K=S.startsWith("/")?S:`/${S}`,Z=($??"")+K;if(O)Z=f7({path:O,url:Z});let X=j?x(j):"";if(X.startsWith("?"))X=X.substring(1);if(X)Z+=`?${X}`;return Z};function c0($){let O=$.body!==void 0;if(O&&$.bodySerializer){if("serializedBody"in $)return $.serializedBody!==void 0&&$.serializedBody!==""?$.serializedBody:null;return $.body!==""?$.body:null}if(O)return $.body;return}var RS=async($,O)=>{let j=typeof O==="function"?await O($):O;if(!j)return;if($.scheme==="bearer")return`Bearer ${j}`;if($.scheme==="basic")return`Basic ${btoa(j)}`;return j};var DS={bodySerializer:($)=>JSON.stringify($,(O,j)=>typeof j==="bigint"?j.toString():j)};var CS=({parameters:$={},...O}={})=>{return(x)=>{let S=[];if(x&&typeof x==="object")for(let K in x){let Z=x[K];if(Z===void 0||Z===null)continue;let X=$[K]||O;if(Array.isArray(Z)){let G=hj({allowReserved:X.allowReserved,explode:!0,name:K,style:"form",value:Z,...X.array});if(G)S.push(G)}else if(typeof Z==="object"){let G=mj({allowReserved:X.allowReserved,explode:!0,name:K,style:"deepObject",value:Z,...X.object});if(G)S.push(G)}else{let G=DO({allowReserved:X.allowReserved,name:K,value:Z});if(G)S.push(G)}}return S.join("&")}},_S=($)=>{if(!$)return"stream";let O=$.split(";")[0]?.trim();if(!O)return;if(O.startsWith("application/json")||O.endsWith("+json"))return"json";if(O==="multipart/form-data")return"formData";if(["application/","audio/","image/","video/"].some((j)=>O.startsWith(j)))return"blob";if(O.startsWith("text/"))return"text";return},b7=($,O)=>{if(!O)return!1;if($.headers.has(O)||$.query?.[O]||$.headers.get("Cookie")?.includes(`${O}=`))return!0;return!1};async function AS($){for(let O of $.security??[]){if(b7($,O.name))continue;let j=await RS(O,$.auth);if(!j)continue;let x=O.name??"Authorization";switch(O.in){case"query":if(!$.query)$.query={};$.query[x]=j;break;case"cookie":$.headers.append("Cookie",`${x}=${j}`);break;case"header":default:$.headers.set(x,j);break}}}var u0=($)=>HS({baseUrl:$.baseUrl,path:$.path,query:$.query,querySerializer:typeof $.querySerializer==="function"?$.querySerializer:CS($.querySerializer),url:$.url}),d0=($,O)=>{let j={...$,...O};if(j.baseUrl?.endsWith("/"))j.baseUrl=j.baseUrl.substring(0,j.baseUrl.length-1);return j.headers=uj($.headers,O.headers),j},g7=($)=>{let O=[];return $.forEach((j,x)=>{O.push([x,j])}),O},uj=(...$)=>{let O=new Headers;for(let j of $){if(!j)continue;let x=j instanceof Headers?g7(j):Object.entries(j);for(let[S,K]of x)if(K===null)O.delete(S);else if(Array.isArray(K))for(let Z of K)O.append(S,Z);else if(K!==void 0)O.set(S,typeof K==="object"?JSON.stringify(K):K)}return O};class cj{fns=[];clear(){this.fns=[]}eject($){let O=this.getInterceptorIndex($);if(this.fns[O])this.fns[O]=null}exists($){let O=this.getInterceptorIndex($);return Boolean(this.fns[O])}getInterceptorIndex($){if(typeof $==="number")return this.fns[$]?$:-1;return this.fns.indexOf($)}update($,O){let j=this.getInterceptorIndex($);if(this.fns[j])return this.fns[j]=O,$;return!1}use($){return this.fns.push($),this.fns.length-1}}var MS=()=>({error:new cj,request:new cj,response:new cj}),k7=CS({allowReserved:!1,array:{explode:!0,style:"form"},object:{explode:!0,style:"deepObject"}}),y7={"Content-Type":"application/json"},tO=($={})=>({...DS,headers:y7,parseAs:"auto",querySerializer:k7,...$});var l0=($={})=>{let O=d0(tO(),$),j=()=>({...O}),x=(Y)=>{return O=d0(O,Y),j()},S=MS(),K=async(Y)=>{let J={...O,...Y,fetch:Y.fetch??O.fetch??globalThis.fetch,headers:uj(O.headers,Y.headers),serializedBody:void 0};if(J.security)await AS(J);if(J.requestValidator)await J.requestValidator(J);if(J.body!==void 0&&J.bodySerializer)J.serializedBody=J.bodySerializer(J.body);if(J.body===void 0||J.serializedBody==="")J.headers.delete("Content-Type");let I=J,z=u0(I);return{opts:I,url:z}},Z=async(Y)=>{let J=Y.throwOnError??O.throwOnError,I=Y.responseStyle??O.responseStyle,z,W;try{let{opts:V,url:B}=await K(Y),T={redirect:"follow",...V,body:c0(V)};z=new Request(B,T);for(let U of S.request.fns)if(U)z=await U(z,V);let P=V.fetch;W=await P(z);for(let U of S.response.fns)if(U)W=await U(W,z,V);let D={request:z,response:W};if(W.ok){let U=(V.parseAs==="auto"?_S(W.headers.get("Content-Type")):V.parseAs)??"json";if(W.status===204||W.headers.get("Content-Length")==="0"){let N;switch(U){case"arrayBuffer":case"blob":case"text":N=await W[U]();break;case"formData":N=new FormData;break;case"stream":N=W.body;break;case"json":default:N={};break}return V.responseStyle==="data"?N:{data:N,...D}}let H;switch(U){case"arrayBuffer":case"blob":case"formData":case"text":H=await W[U]();break;case"json":{let N=await W.text();H=N?JSON.parse(N):{};break}case"stream":return V.responseStyle==="data"?W.body:{data:W.body,...D}}if(U==="json"){if(V.responseValidator)await V.responseValidator(H);if(V.responseTransformer)H=await V.responseTransformer(H)}return V.responseStyle==="data"?H:{data:H,...D}}let C=await W.text(),w;try{w=JSON.parse(C)}catch{}throw w??C}catch(V){let B=V;for(let T of S.error.fns)if(T)B=await T(B,W,z,Y);if(B=B||{},J)throw B;return I==="data"?void 0:{error:B,request:z,response:W}}},X=(Y)=>(J)=>Z({...J,method:Y}),G=(Y)=>async(J)=>{let{opts:I,url:z}=await K(J);return TS({...I,body:I.body,method:Y,onRequest:async(W,V)=>{let B=new Request(W,V);for(let T of S.request.fns)if(T)B=await T(B,I);return B},serializedBody:c0(I),url:z})};return{buildUrl:(Y)=>u0({...O,...Y}),connect:X("CONNECT"),delete:X("DELETE"),get:X("GET"),getConfig:j,head:X("HEAD"),interceptors:S,options:X("OPTIONS"),patch:X("PATCH"),post:X("POST"),put:X("PUT"),request:Z,setConfig:x,sse:{connect:G("CONNECT"),delete:G("DELETE"),get:G("GET"),head:G("HEAD"),options:G("OPTIONS"),patch:G("PATCH"),post:G("POST"),put:G("PUT"),trace:G("TRACE")},trace:X("TRACE")}};var PS=l0(tO());class FS{client;constructor($){this.client=$?.client??PS}}class ES{defaultKey="default";instances=new Map;get($){let O=this.instances.get($??this.defaultKey);if(!O)throw Error('No SDK client found. Create one with "new Sdk()" to fix this error.');return O}set($,O){this.instances.set(O??this.defaultKey,$)}}class dj extends FS{static __registry=new ES;constructor($){super($);dj.__registry.set(this,$?.key)}setPricingHandler($){return($.client??this.client).post({url:"/ai-cost/pricing",...$,headers:{"Content-Type":"application/json",...$.headers}})}getUsageHandler($){return($.client??this.client).post({url:"/ai-cost/usage",...$,headers:{"Content-Type":"application/json",...$.headers}})}upsertAiProjection($){return($.client??this.client).post({url:"/ai-projections",...$,headers:{"Content-Type":"application/json",...$.headers}})}getChatsForAttachmentHandler($){return($.client??this.client).get({url:"/attachments/{attachment_id}/chats",...$})}deleteChat($){return($.client??this.client).delete({url:"/chat/{chat_id}",...$})}patchChat($){return($.client??this.client).patch({url:"/chat/{chat_id}",...$,headers:{"Content-Type":"application/json",...$.headers}})}permanentlyDeleteChat($){return($.client??this.client).delete({url:"/chat/{chat_id}/permanent",...$})}createChat($){return($.client??this.client).post({url:"/chats",...$,headers:{"Content-Type":"application/json",...$.headers}})}getChatHistoryHandler($){return($.client??this.client).get({url:"/chats/history/{chat_id}",...$})}getChatHistoryBatchMessagesHandler($){return($.client??this.client).post({url:"/chats/history_batch_messages",...$,headers:{"Content-Type":"application/json",...$.headers}})}getChat($){return($.client??this.client).get({url:"/chats/{chat_id}",...$})}copyChat($){return($.client??this.client).post({url:"/chats/{chat_id}/copy",...$})}getChatPermissions($){return($.client??this.client).get({url:"/chats/{chat_id}/permissions",...$})}revertDeleteChat($){return($.client??this.client).put({url:"/chats/{chat_id}/revert_delete",...$})}callTool($){return($.client??this.client).post({url:"/chats/{chat_id}/tool/call",...$,headers:{"Content-Type":"application/json",...$.headers}})}rejectToolCall($){return($.client??this.client).post({url:"/chats/{chat_id}/tool/reject",...$,headers:{"Content-Type":"application/json",...$.headers}})}updateToolResponse($){return($.client??this.client).post({url:"/chats/{chat_id}/tool/response/update",...$,headers:{"Content-Type":"application/json",...$.headers}})}updateToolCall($){return($.client??this.client).post({url:"/chats/{chat_id}/tool/update",...$,headers:{"Content-Type":"application/json",...$.headers}})}getCitationHandler($){return($.client??this.client).get({url:"/citations/{id}",...$})}decisions($){return($.client??this.client).post({url:"/decisions",...$,headers:{"Content-Type":"application/json",...$.headers}})}healthHandler($){return($?.client??this.client).get({url:"/health",...$})}runImportHandler($){return($.client??this.client).post({url:"/import/run",...$,headers:{"Content-Type":"application/json",...$.headers}})}discoverHandler($){return($.client??this.client).post({url:"/import/runs/{source}/discover",...$})}dismissRunHandler($){return($.client??this.client).post({url:"/import/runs/{source}/dismiss",...$})}retryGatherHandler($){return($.client??this.client).post({url:"/import/runs/{source}/retry",...$})}getStateHandler($){return($?.client??this.client).get({url:"/import/state",...$})}deleteMcpServer($){return($.client??this.client).delete({url:"/mcp/servers",...$})}listMcpServers($){return($?.client??this.client).get({url:"/mcp/servers",...$})}addMcpServer($){return($.client??this.client).post({url:"/mcp/servers",...$,headers:{"Content-Type":"application/json",...$.headers}})}updateMcpServer($){return($.client??this.client).put({url:"/mcp/servers",...$,headers:{"Content-Type":"application/json",...$.headers}})}mcpAuthCallback($){return($?.client??this.client).get({url:"/mcp/servers/auth/callback",...$})}mcpOauthClientMetadata($){return($?.client??this.client).get({url:"/mcp/servers/auth/client-metadata",...$})}startMcpAuth($){return($.client??this.client).post({url:"/mcp/servers/auth/start",...$,headers:{"Content-Type":"application/json",...$.headers}})}getMemoryHandler($){return($?.client??this.client).get({url:"/memory",...$})}getStateHandler2($){return($?.client??this.client).get({url:"/onboarding",...$})}completeHandler($){return($.client??this.client).post({url:"/onboarding/complete",...$,headers:{"Content-Type":"application/json",...$.headers}})}dismissedPromptsHandler($){return($?.client??this.client).get({url:"/onboarding/prompts",...$})}dismissPromptHandler($){return($.client??this.client).post({url:"/onboarding/prompts/{prompt_id}/dismiss",...$})}browsePipedreamMcpCatalog($){return($?.client??this.client).get({url:"/pipedream/mcp/catalog",...$})}completePipedreamMcpConnection($){return($.client??this.client).post({url:"/pipedream/mcp/complete",...$,headers:{"Content-Type":"application/json",...$.headers}})}deletePipedreamMcpConnection($){return($.client??this.client).delete({url:"/pipedream/mcp/connections",...$})}listPipedreamMcpConnections($){return($?.client??this.client).get({url:"/pipedream/mcp/connections",...$})}updatePipedreamMcpConnection($){return($.client??this.client).put({url:"/pipedream/mcp/connections",...$,headers:{"Content-Type":"application/json",...$.headers}})}createPipedreamMcpToken($){return($?.client??this.client).post({url:"/pipedream/mcp/token",...$})}getBatchPreview($){return($.client??this.client).post({url:"/preview",...$,headers:{"Content-Type":"application/json",...$.headers}})}sendChatMessage($){return($.client??this.client).post({url:"/stream/chat/message",...$,headers:{"Content-Type":"application/json",...$.headers}})}stopChatStream($){return($.client??this.client).post({url:"/stream/chat/message/stop",...$,headers:{"Content-Type":"application/json",...$.headers}})}structuredCompletion($){return($.client??this.client).post({url:"/structured-completion",...$,headers:{"Content-Type":"application/json",...$.headers}})}}function wS({onRequest:$,onSseError:O,onSseEvent:j,responseTransformer:x,responseValidator:S,sseDefaultRetryDelay:K,sseMaxRetryAttempts:Z,sseMaxRetryDelay:X,sseSleepFn:G,url:L,...Y}){let J,I=G??((V)=>new Promise((B)=>setTimeout(B,V)));return{stream:async function*(){let V=K??3000,B=0,T=Y.signal??new AbortController().signal;while(!0){if(T.aborted)break;B++;let P=Y.headers instanceof Headers?Y.headers:new Headers(Y.headers);if(J!==void 0)P.set("Last-Event-ID",J);try{let D={redirect:"follow",...Y,body:Y.serializedBody,headers:P,signal:T},C=new Request(L,D);if($)C=await $(L,D);let U=await(Y.fetch??globalThis.fetch)(C);if(!U.ok)throw Error(`SSE failed: ${U.status} ${U.statusText}`);if(!U.body)throw Error("No body in SSE response");let H=U.body.pipeThrough(new TextDecoderStream).getReader(),N="",g=()=>{try{H.cancel()}catch{}};T.addEventListener("abort",g);try{while(!0){let{done:d,value:l}=await H.read();if(d)break;N+=l,N=N.replace(/\r\n?/g,`+`);try{_=JSON.parse(R),v=!0}catch{_=R}}if(v){if(S)await S(_);if(x)_=await x(_)}if(j?.({data:_,event:y,id:J,retry:V}),F.length)yield _}}}finally{T.removeEventListener("abort",g),H.releaseLock()}break}catch(D){if(O?.(D),Z!==void 0&&B>=Z)break;let C=Math.min(V*2**(B-1),X??30000);await I(C)}}}()}}var F7=($)=>{switch($){case"label":return".";case"matrix":return";";case"simple":return",";default:return"&"}},E7=($)=>{switch($){case"form":return",";case"pipeDelimited":return"|";case"spaceDelimited":return"%20";default:return","}},w7=($)=>{switch($){case"label":return".";case"matrix":return";";case"simple":return",";default:return"&"}},hj=({allowReserved:$,explode:O,name:j,style:x,value:S})=>{if(!O){let X=($?S:S.map((G)=>encodeURIComponent(G))).join(E7(x));switch(x){case"label":return`.${X}`;case"matrix":return`;${j}=${X}`;case"simple":return X;default:return`${j}=${X}`}}let K=F7(x),Z=S.map((X)=>{if(x==="label"||x==="simple")return $?X:encodeURIComponent(X);return DO({allowReserved:$,name:j,value:X})}).join(K);return x==="label"||x==="matrix"?K+Z:Z},DO=({allowReserved:$,name:O,value:j})=>{if(j===void 0||j===null)return"";if(typeof j==="object")throw Error("Deeply-nested arrays/objects aren’t supported. Provide your own `querySerializer()` to handle these.");return`${O}=${$?j:encodeURIComponent(j)}`},mj=({allowReserved:$,explode:O,name:j,style:x,value:S,valueOnly:K})=>{if(S instanceof Date)return K?S.toISOString():`${j}=${S.toISOString()}`;if(x!=="deepObject"&&!O){let G=[];Object.entries(S).forEach(([Y,J])=>{G=[...G,Y,$?J:encodeURIComponent(J)]});let L=G.join(",");switch(x){case"form":return`${j}=${L}`;case"label":return`.${L}`;case"matrix":return`;${j}=${L}`;default:return L}}let Z=w7(x),X=Object.entries(S).map(([G,L])=>DO({allowReserved:$,name:x==="deepObject"?`${j}[${G}]`:G,value:L})).join(Z);return x==="label"||x==="matrix"?Z+X:X};var q7=/\{[^{}]+\}/g,f7=({path:$,url:O})=>{let j=O,x=O.match(q7);if(x)for(let S of x){let K=!1,Z=S.substring(1,S.length-1),X="simple";if(Z.endsWith("*"))K=!0,Z=Z.substring(0,Z.length-1);if(Z.startsWith("."))Z=Z.substring(1),X="label";else if(Z.startsWith(";"))Z=Z.substring(1),X="matrix";let G=$[Z];if(G===void 0||G===null)continue;if(Array.isArray(G)){j=j.replace(S,hj({explode:K,name:Z,style:X,value:G}));continue}if(typeof G==="object"){j=j.replace(S,mj({explode:K,name:Z,style:X,value:G,valueOnly:!0}));continue}if(X==="matrix"){j=j.replace(S,`;${DO({name:Z,value:G})}`);continue}let L=encodeURIComponent(X==="label"?`.${G}`:G);j=j.replace(S,L)}return j},HS=({baseUrl:$,path:O,query:j,querySerializer:x,url:S})=>{let K=S.startsWith("/")?S:`/${S}`,Z=($??"")+K;if(O)Z=f7({path:O,url:Z});let X=j?x(j):"";if(X.startsWith("?"))X=X.substring(1);if(X)Z+=`?${X}`;return Z};function c0($){let O=$.body!==void 0;if(O&&$.bodySerializer){if("serializedBody"in $)return $.serializedBody!==void 0&&$.serializedBody!==""?$.serializedBody:null;return $.body!==""?$.body:null}if(O)return $.body;return}var RS=async($,O)=>{let j=typeof O==="function"?await O($):O;if(!j)return;if($.scheme==="bearer")return`Bearer ${j}`;if($.scheme==="basic")return`Basic ${btoa(j)}`;return j};var DS={bodySerializer:($)=>JSON.stringify($,(O,j)=>typeof j==="bigint"?j.toString():j)};var CS=({parameters:$={},...O}={})=>{return(x)=>{let S=[];if(x&&typeof x==="object")for(let K in x){let Z=x[K];if(Z===void 0||Z===null)continue;let X=$[K]||O;if(Array.isArray(Z)){let G=hj({allowReserved:X.allowReserved,explode:!0,name:K,style:"form",value:Z,...X.array});if(G)S.push(G)}else if(typeof Z==="object"){let G=mj({allowReserved:X.allowReserved,explode:!0,name:K,style:"deepObject",value:Z,...X.object});if(G)S.push(G)}else{let G=DO({allowReserved:X.allowReserved,name:K,value:Z});if(G)S.push(G)}}return S.join("&")}},_S=($)=>{if(!$)return"stream";let O=$.split(";")[0]?.trim();if(!O)return;if(O.startsWith("application/json")||O.endsWith("+json"))return"json";if(O==="multipart/form-data")return"formData";if(["application/","audio/","image/","video/"].some((j)=>O.startsWith(j)))return"blob";if(O.startsWith("text/"))return"text";return},b7=($,O)=>{if(!O)return!1;if($.headers.has(O)||$.query?.[O]||$.headers.get("Cookie")?.includes(`${O}=`))return!0;return!1};async function AS($){for(let O of $.security??[]){if(b7($,O.name))continue;let j=await RS(O,$.auth);if(!j)continue;let x=O.name??"Authorization";switch(O.in){case"query":if(!$.query)$.query={};$.query[x]=j;break;case"cookie":$.headers.append("Cookie",`${x}=${j}`);break;case"header":default:$.headers.set(x,j);break}}}var u0=($)=>HS({baseUrl:$.baseUrl,path:$.path,query:$.query,querySerializer:typeof $.querySerializer==="function"?$.querySerializer:CS($.querySerializer),url:$.url}),d0=($,O)=>{let j={...$,...O};if(j.baseUrl?.endsWith("/"))j.baseUrl=j.baseUrl.substring(0,j.baseUrl.length-1);return j.headers=uj($.headers,O.headers),j},g7=($)=>{let O=[];return $.forEach((j,x)=>{O.push([x,j])}),O},uj=(...$)=>{let O=new Headers;for(let j of $){if(!j)continue;let x=j instanceof Headers?g7(j):Object.entries(j);for(let[S,K]of x)if(K===null)O.delete(S);else if(Array.isArray(K))for(let Z of K)O.append(S,Z);else if(K!==void 0)O.set(S,typeof K==="object"?JSON.stringify(K):K)}return O};class cj{fns=[];clear(){this.fns=[]}eject($){let O=this.getInterceptorIndex($);if(this.fns[O])this.fns[O]=null}exists($){let O=this.getInterceptorIndex($);return Boolean(this.fns[O])}getInterceptorIndex($){if(typeof $==="number")return this.fns[$]?$:-1;return this.fns.indexOf($)}update($,O){let j=this.getInterceptorIndex($);if(this.fns[j])return this.fns[j]=O,$;return!1}use($){return this.fns.push($),this.fns.length-1}}var MS=()=>({error:new cj,request:new cj,response:new cj}),k7=CS({allowReserved:!1,array:{explode:!0,style:"form"},object:{explode:!0,style:"deepObject"}}),y7={"Content-Type":"application/json"},tO=($={})=>({...DS,headers:y7,parseAs:"auto",querySerializer:k7,...$});var l0=($={})=>{let O=d0(tO(),$),j=()=>({...O}),x=(Y)=>{return O=d0(O,Y),j()},S=MS(),K=async(Y)=>{let J={...O,...Y,fetch:Y.fetch??O.fetch??globalThis.fetch,headers:uj(O.headers,Y.headers),serializedBody:void 0};if(J.security)await AS(J);if(J.requestValidator)await J.requestValidator(J);if(J.body!==void 0&&J.bodySerializer)J.serializedBody=J.bodySerializer(J.body);if(J.body===void 0||J.serializedBody==="")J.headers.delete("Content-Type");let I=J,z=u0(I);return{opts:I,url:z}},Z=async(Y)=>{let J=Y.throwOnError??O.throwOnError,I=Y.responseStyle??O.responseStyle,z,W;try{let{opts:V,url:B}=await K(Y),T={redirect:"follow",...V,body:c0(V)};z=new Request(B,T);for(let U of S.request.fns)if(U)z=await U(z,V);let P=V.fetch;W=await P(z);for(let U of S.response.fns)if(U)W=await U(W,z,V);let D={request:z,response:W};if(W.ok){let U=(V.parseAs==="auto"?_S(W.headers.get("Content-Type")):V.parseAs)??"json";if(W.status===204||W.headers.get("Content-Length")==="0"){let N;switch(U){case"arrayBuffer":case"blob":case"text":N=await W[U]();break;case"formData":N=new FormData;break;case"stream":N=W.body;break;case"json":default:N={};break}return V.responseStyle==="data"?N:{data:N,...D}}let H;switch(U){case"arrayBuffer":case"blob":case"formData":case"text":H=await W[U]();break;case"json":{let N=await W.text();H=N?JSON.parse(N):{};break}case"stream":return V.responseStyle==="data"?W.body:{data:W.body,...D}}if(U==="json"){if(V.responseValidator)await V.responseValidator(H);if(V.responseTransformer)H=await V.responseTransformer(H)}return V.responseStyle==="data"?H:{data:H,...D}}let C=await W.text(),w;try{w=JSON.parse(C)}catch{}throw w??C}catch(V){let B=V;for(let T of S.error.fns)if(T)B=await T(B,W,z,Y);if(B=B||{},J)throw B;return I==="data"?void 0:{error:B,request:z,response:W}}},X=(Y)=>(J)=>Z({...J,method:Y}),G=(Y)=>async(J)=>{let{opts:I,url:z}=await K(J);return TS({...I,body:I.body,method:Y,onRequest:async(W,V)=>{let B=new Request(W,V);for(let T of S.request.fns)if(T)B=await T(B,I);return B},serializedBody:c0(I),url:z})};return{buildUrl:(Y)=>u0({...O,...Y}),connect:X("CONNECT"),delete:X("DELETE"),get:X("GET"),getConfig:j,head:X("HEAD"),interceptors:S,options:X("OPTIONS"),patch:X("PATCH"),post:X("POST"),put:X("PUT"),request:Z,setConfig:x,sse:{connect:G("CONNECT"),delete:G("DELETE"),get:G("GET"),head:G("HEAD"),options:G("OPTIONS"),patch:G("PATCH"),post:G("POST"),put:G("PUT"),trace:G("TRACE")},trace:X("TRACE")}};var PS=l0(tO());class FS{client;constructor($){this.client=$?.client??PS}}class ES{defaultKey="default";instances=new Map;get($){let O=this.instances.get($??this.defaultKey);if(!O)throw Error('No SDK client found. Create one with "new Sdk()" to fix this error.');return O}set($,O){this.instances.set(O??this.defaultKey,$)}}class dj extends FS{static __registry=new ES;constructor($){super($);dj.__registry.set(this,$?.key)}setPricingHandler($){return($.client??this.client).post({url:"/ai-cost/pricing",...$,headers:{"Content-Type":"application/json",...$.headers}})}getUsageHandler($){return($.client??this.client).post({url:"/ai-cost/usage",...$,headers:{"Content-Type":"application/json",...$.headers}})}upsertAiProjection($){return($.client??this.client).post({url:"/ai-projections",...$,headers:{"Content-Type":"application/json",...$.headers}})}getChatsForAttachmentHandler($){return($.client??this.client).get({url:"/attachments/{attachment_id}/chats",...$})}deleteChat($){return($.client??this.client).delete({url:"/chat/{chat_id}",...$})}patchChat($){return($.client??this.client).patch({url:"/chat/{chat_id}",...$,headers:{"Content-Type":"application/json",...$.headers}})}permanentlyDeleteChat($){return($.client??this.client).delete({url:"/chat/{chat_id}/permanent",...$})}createChat($){return($.client??this.client).post({url:"/chats",...$,headers:{"Content-Type":"application/json",...$.headers}})}getChatHistoryHandler($){return($.client??this.client).get({url:"/chats/history/{chat_id}",...$})}getChatHistoryBatchMessagesHandler($){return($.client??this.client).post({url:"/chats/history_batch_messages",...$,headers:{"Content-Type":"application/json",...$.headers}})}getChat($){return($.client??this.client).get({url:"/chats/{chat_id}",...$})}copyChat($){return($.client??this.client).post({url:"/chats/{chat_id}/copy",...$})}getChatPermissions($){return($.client??this.client).get({url:"/chats/{chat_id}/permissions",...$})}revertDeleteChat($){return($.client??this.client).put({url:"/chats/{chat_id}/revert_delete",...$})}callTool($){return($.client??this.client).post({url:"/chats/{chat_id}/tool/call",...$,headers:{"Content-Type":"application/json",...$.headers}})}rejectToolCall($){return($.client??this.client).post({url:"/chats/{chat_id}/tool/reject",...$,headers:{"Content-Type":"application/json",...$.headers}})}updateToolResponse($){return($.client??this.client).post({url:"/chats/{chat_id}/tool/response/update",...$,headers:{"Content-Type":"application/json",...$.headers}})}updateToolCall($){return($.client??this.client).post({url:"/chats/{chat_id}/tool/update",...$,headers:{"Content-Type":"application/json",...$.headers}})}getCitationHandler($){return($.client??this.client).get({url:"/citations/{id}",...$})}decisions($){return($.client??this.client).post({url:"/decisions",...$,headers:{"Content-Type":"application/json",...$.headers}})}healthHandler($){return($?.client??this.client).get({url:"/health",...$})}notionReportHandler($){return($?.client??this.client).get({url:"/import/notion",...$})}cancelNotionHandler($){return($?.client??this.client).post({url:"/import/notion/cancel",...$})}runImportHandler($){return($.client??this.client).post({url:"/import/run",...$,headers:{"Content-Type":"application/json",...$.headers}})}discoverHandler($){return($.client??this.client).post({url:"/import/runs/{source}/discover",...$})}getStateHandler($){return($?.client??this.client).get({url:"/import/state",...$})}deleteMcpServer($){return($.client??this.client).delete({url:"/mcp/servers",...$})}listMcpServers($){return($?.client??this.client).get({url:"/mcp/servers",...$})}addMcpServer($){return($.client??this.client).post({url:"/mcp/servers",...$,headers:{"Content-Type":"application/json",...$.headers}})}updateMcpServer($){return($.client??this.client).put({url:"/mcp/servers",...$,headers:{"Content-Type":"application/json",...$.headers}})}mcpAuthCallback($){return($?.client??this.client).get({url:"/mcp/servers/auth/callback",...$})}mcpOauthClientMetadata($){return($?.client??this.client).get({url:"/mcp/servers/auth/client-metadata",...$})}startMcpAuth($){return($.client??this.client).post({url:"/mcp/servers/auth/start",...$,headers:{"Content-Type":"application/json",...$.headers}})}getMemoryHandler($){return($?.client??this.client).get({url:"/memory",...$})}getStateHandler2($){return($?.client??this.client).get({url:"/onboarding",...$})}completeHandler($){return($.client??this.client).post({url:"/onboarding/complete",...$,headers:{"Content-Type":"application/json",...$.headers}})}dismissedPromptsHandler($){return($?.client??this.client).get({url:"/onboarding/prompts",...$})}dismissPromptHandler($){return($.client??this.client).post({url:"/onboarding/prompts/{prompt_id}/dismiss",...$})}browsePipedreamMcpCatalog($){return($?.client??this.client).get({url:"/pipedream/mcp/catalog",...$})}completePipedreamMcpConnection($){return($.client??this.client).post({url:"/pipedream/mcp/complete",...$,headers:{"Content-Type":"application/json",...$.headers}})}deletePipedreamMcpConnection($){return($.client??this.client).delete({url:"/pipedream/mcp/connections",...$})}listPipedreamMcpConnections($){return($?.client??this.client).get({url:"/pipedream/mcp/connections",...$})}updatePipedreamMcpConnection($){return($.client??this.client).put({url:"/pipedream/mcp/connections",...$,headers:{"Content-Type":"application/json",...$.headers}})}createPipedreamMcpToken($){return($?.client??this.client).post({url:"/pipedream/mcp/token",...$})}getBatchPreview($){return($.client??this.client).post({url:"/preview",...$,headers:{"Content-Type":"application/json",...$.headers}})}sendChatMessage($){return($.client??this.client).post({url:"/stream/chat/message",...$,headers:{"Content-Type":"application/json",...$.headers}})}stopChatStream($){return($.client??this.client).post({url:"/stream/chat/message/stop",...$,headers:{"Content-Type":"application/json",...$.headers}})}structuredCompletion($){return($.client??this.client).post({url:"/structured-completion",...$,headers:{"Content-Type":"application/json",...$.headers}})}}function wS({onRequest:$,onSseError:O,onSseEvent:j,responseTransformer:x,responseValidator:S,sseDefaultRetryDelay:K,sseMaxRetryAttempts:Z,sseMaxRetryDelay:X,sseSleepFn:G,url:L,...Y}){let J,I=G??((V)=>new Promise((B)=>setTimeout(B,V)));return{stream:async function*(){let V=K??3000,B=0,T=Y.signal??new AbortController().signal;while(!0){if(T.aborted)break;B++;let P=Y.headers instanceof Headers?Y.headers:new Headers(Y.headers);if(J!==void 0)P.set("Last-Event-ID",J);try{let D={redirect:"follow",...Y,body:Y.serializedBody,headers:P,signal:T},C=new Request(L,D);if($)C=await $(L,D);let U=await(Y.fetch??globalThis.fetch)(C);if(!U.ok)throw Error(`SSE failed: ${U.status} ${U.statusText}`);if(!U.body)throw Error("No body in SSE response");let H=U.body.pipeThrough(new TextDecoderStream).getReader(),N="",g=()=>{try{H.cancel()}catch{}};T.addEventListener("abort",g);try{while(!0){let{done:d,value:l}=await H.read();if(d)break;N+=l,N=N.replace(/\r\n?/g,``);let k=N.split(``);N=k.pop()??"";for(let r of k){let p=r.split(`diff --git a/crates/folds/agent_fold/src/domain/test/notifications.rs b/crates/folds/agent_fold/src/domain/test/notifications.rsindex 5229b69eb7..b35d57a3e7 100644--- a/crates/folds/agent_fold/src/domain/test/notifications.rs+++ b/crates/folds/agent_fold/src/domain/test/notifications.rs@@ -160,7 +160,9 @@ fn interrupted_delivery_keeps_one_event_attached_to_the_retry_turn() {#[test]fn stored_flat_notification_rows_keep_their_typed_payloads() {use crate::domain::model::SessionNotification;- use agent_runtime_protocol::domain::notification::{DecisionOutcome, DecisionVia, ItemStatus};+ use agent_runtime_protocol::domain::notification::{+ DecisionOutcome, DecisionVia, ItemStatus, ReviewSettlement,+ };// These are the flat kind/payload rows emitted before the typed fold model.let stored = r#"[@@ -177,7 +179,8 @@ fn stored_flat_notification_rows_keep_their_typed_payloads() {assert_eq!(decided.via, DecisionVia::Card);assert_eq!(decided.items[0].status, ItemStatus::Submitting);assert!(decided.items[0].edited);- let NotificationEvent::ReviewSettled(settled) = &rows[2].event else {+ let NotificationEvent::ReviewSettled(ReviewSettlement::Actions(settled)) = &rows[2].event+ else {panic!("stored settlement lost its kind");};assert_eq!(settled.items[0].error.as_deref(), Some("Provider rejected"));diff --git a/crates/import/AGENTS.md b/crates/import/AGENTS.mdindex 06953e1436..49512beabf 100644--- a/crates/import/AGENTS.md+++ b/crates/import/AGENTS.md@@ -2,52 +2,173 @@## What it is-Belongs to onboarding and AI chat: the import pipeline tracking external-items (Linear issues, Notion pages, Slack channels) as they're discovered,-accepted, and copied into Macro entities. Host-agnostic — onboarding-(`/setup`) and AI chat both drive the same primitives, distinguished only by-the `initiator` recorded on each row.+Belongs to AI chat and Settings: the import pipeline tracking external items+(Linear issues, Notion pages, Slack channels) as they're discovered,+accepted, and copied into Macro entities. Host-agnostic — chat tools,+Settings → Slack discovery, and the Slack archive import drive the same+primitives, distinguished only by the `initiator` recorded on each row.Not to be confused with `crates/foreign_entity`: foreign entities stay live inthe external system; an `ImportEntity` here is a ledger row for an item being-copied in, moving `staged` → `importing` → `imported` (or `discarded`).+copied in, moving `staged` → `importing` → `imported` (or `discarded`). Rows+carry their source `kind`, an optional `parent_id` (the row they were imported+under), and an optional `review_id`; a team keeps one `importing`/`imported`+row per Notion or Linear object (`import_entity_team_object_key`).++Notion workspace imports go through a reviewed plan: `ImportFromNotion`+(`plan`) reads the workspace over Notion's REST API (`import_notion`, through+the user's Pipedream grant), asks the decision model what to do with each page+and data source (copy, tasks, skip, or by hand), and stages the plan as ledger+rows. The agent presents it with `PresentReview` (kind `import`) and asks+"Should I kick it off?"; the owner approves on the card or by saying yes, which+has the agent submit it with `DecideReview`. `plan` returns after a short wait+(`PLAN_WAIT`), usually while the run is still `running`, and the agent+presents the review at once: the harness ties the staged rows and the+`running` run (`import_run.review_id`) to the review, so rows staged later+take it too. Planning stages the whole tree as soon as the inventory is read,+each row without a `decision` yet, then writes each decision-model batch's+verdicts onto its rows as it lands (`update_plan_rows`); the report counts+undecided rows as `deciding`, and the write cannot start until the run is+`ready`. A resumed plan stages the tree again, which is idempotent. On+approval the harness starts the write:+folders mirroring the page tree, documents, databases with rows, row bodies and+relations, and task lists as Macro tasks. The Notion run row tracks it+(`running` → `ready` → `importing` → `completed`/`failed`/`dismissed`).++Every plan carries the user's `sharing` (`team`, `personal`, or `ai`, where+the decision model also answers `shared` per object), stored on the run so an+adopted plan keeps it. A user on a team must choose it+(`ImportError::NotionSharingRequired` tells the agent to ask); a user without+one plans `personal` without being asked, and the report's `onTeam` says+which. Each+plan item records `shared`, `sharedReason` and `shareable`+(`notion_workspace/sharing.rs`). The owner can flip items on the review card;+the draft's `shared`/`personal` lists are written onto the rows before the+write starts. Users without a team keep everything personal. The write+shares documents, databases (with their row bodies' folder) and task-list+folders it makes (`WorkspaceWriter::share_with_team`), and a page's folder only+when everything written inside it is shared, since sharing a folder shares its+contents. A teammate's shared database is linked like a page, but relations+from this import's other databases into it are not linked.## Start here- `src/domain/models.rs`: `ImportEntity` and its state machine — read themodule doc comment for the staged/importing/imported/discarded distinction.-- `src/domain/service.rs` + `src/domain/service/admission.rs`: discovery and- acceptance logic.+- `src/domain/service.rs` + `src/domain/service/admission.rs`: Slack+ discovery, staging, and acceptance logic.- `src/outbound/mcp_slack_source.rs`: Slack discovery via MCP tool calls, with`fixtures/*.json` backing its tests.-- `src/inbound/toolset.rs`: the AI-chat-facing import tools.-- `src/inbound/axum_router.rs`: the `/setup` onboarding-facing routes.+- `src/domain/service/notion_workspace.rs` (+ `plan`, `triage`, `sharing`,+ `object`, `write`, `values`, `report`): the Notion workspace import —+ planning, the decision model, who sees what, writing one object (claim,+ link or write), cancel and the report.+- `src/inbound/toolset.rs`: the AI-chat-facing import tools+ (`toolset/import_from_notion.rs` is the workspace import tool).+- `src/inbound/axum_router.rs`: the `/import/*` routes (state, accept/decline,+ Slack discovery, the Notion import report and cancel).## Scope-- Owns: the import ledger, discovery/admission logic, and both the- onboarding and AI-chat entry points into it.+- Owns: the import ledger, Slack discovery, admission logic, and the AI-chat+ and HTTP entry points into it.- Not here: live references to external items that never get copied in →`crates/foreign_entity`.## Boundaries-- Depends on: `ai_toolset`, `ai_usage`, `ai_billing`, `connection_gateway_client`,- `mcp_select`, `properties`.-- Used by: `onboarding`, `ai_tools`, `slack_integration`.+- Depends on: `agent_session` (system events for the session presenting a+ review), `ai_toolset`, `ai_usage`, `ai_billing`, `connection_gateway_client`,+ `mcp_select`, `properties`, `import_notion` (Notion REST), `models_databases`+ (database ops the workspace import writes).+- Used by: `ai_tools`, `slack_integration`, `services/agent_harness_service`+ (cognition `/import/*` routes, the Notion sweeper, import reviews).## Recipes- Add an import source: `src/outbound/<source>.rs` (implement the source port)→ `src/source_binding.rs` → `src/domain/service.rs`.- Add a toolset action: `src/inbound/toolset.rs` → `src/domain/service.rs`.-- Add an onboarding route: `src/inbound/axum_router.rs` →- `src/domain/service.rs`.+- Add a route: `src/inbound/axum_router.rs` → `src/domain/service.rs`.## Checks- `cargo test -p import` (`SQLX_OFFLINE` unset).+## Gotchas++- Workspace imports write through the host's `WorkspaceWriter` (the+ `ai_tools` entity creator), which also implements `EntityCreator`; the+ import service's `N` type parameter is the Notion connection factory, and+ hosts that never call `.with_notion_connections` report Notion as not+ connected.+- Notion page bodies are never parsed here: they go to lexical-service+ (`POST /snapshot/notion`) as Notion markdown with a `NotionLinks` map. The+ workspace write knows every planned page's document id up front+ (`write::notion_links`, derived from the ledger row) so pages mention each+ other; only the import guards+ (`check_notion_markdown`: tool-result wrappers, database-only pages) stay in+ Rust.+- Planning and writing run detached on the host's `TaskTracker`+ (`with_job_shutdown`) and survive their process (`notion_workspace/run.rs`):+ the running process holds a lease on the run row (`lease_owner`,+ `lease_expires_at`, renewed by the heartbeat, recording the job: `scope` or+ `review_id`). A cancel from any process flips the run to `dismissed`; on+ shutdown the job stops at its next checkpoint and releases the lease. A run+ whose lease lapsed is adopted (`FOR UPDATE SKIP LOCKED`) by the next report+ read for that user or by the harness's sweeper (`agent_harness_service::cognition`): planning+ restarts, writing continues with the rows still `importing`. After five+ adoptions the run fails. Stale-run and stale-row reapers skip Notion+ workspace imports for this reason.+- A resumed write never creates anything twice: documents, row bodies and+ tasks get UUIDv5 ids from their ledger row (`WorkspaceWriter` treats an id+ in use as already written), database columns and options UUIDv5 ids from+ their database, and folders, databases, inserted rows, the Page column and+ relation columns are recorded in `import_entity_written` as they are made.+ Every import reuses the user's "Notion" folder (`import_run.root_folder_id`)+ while it exists. Left open: a crash between creating a folder, a database+ or a row batch and recording it duplicates that one object or batch, and a+ crash between applying a schema op and recording it fails that database on+ resume (Macro refuses the column id).+- `team_id` on a Notion row means "the team's copy": only shared objects take+ it, at `claim_importing` (staged → importing) before anything is written, so+ `import_entity_team_object_key` makes a second teammate's claim fail. A+ personal copy never takes it, so teammates never see or link to it and+ import their own copy. Planning marks objects a teammate already imported+ and shared as `alreadyImported` (no decision-model call, not counted as+ copies); the write links them. At write time `claim_team_object` decides per+ shared object: hold the claim and write, link to a finished copy, take over a+ claim whose holder's run has no live lease, or wait. A waiting row is+ checked again after the rest is written, then parks with `waiting_on` the+ holder's row: `mark_imported` links waiters in a second statement (the+ waiter holds the holder's row `FOR SHARE`, so neither misses the other), and+ a holder going back to staged releases its waiters to staged. Waiting rows+ outlive their own run; `settle_importing` leaves them while the holder is+ still being written. Left open: a takeover racing the original holder's own+ resume can leave both copies.+- The agent session presenting a plan's review hears what becomes of it as+ system events (`notion_workspace/events.rs`), each produced in the+ transaction that moves the run (`NotionRunRepo::transition_reviewed_run`,+ through `agent_session`'s `NotificationWriter`): `import.planned` once a+ plan presented while planning is `ready`, and `review.settled` with the+ final report (outcome, counts, failures, by-hand items, root folder) once+ its write ends however it ended, including a resumed write and a stalled+ one given up on. A cancel from another process keeps the run `dismissed`,+ so the write's end reports it cancelled. `attach_review` records the+ session (`import_run.review_session_id`); hosts that run Notion imports+ build the ledger with `ai_tools::build_import_repo`, which wires the+ writer. Left open: a writer whose process dies after a cancel never ends,+ so that cancel is not reported.+- The inventory counts each data source's rows, and takes its sample titles,+ from the pages `/v1/search` already returned (every row is a page there);+ only a data source search showed no rows for (a root fetched by id, or an+ empty one) is queried. Plan rows carry the sample titles (`sampleTitles`)+ and a task list's `taskFields` for the review card.+- While planning, the inventory's counts go over a `watch` channel to the run+ row (`scanned_pages`/`scanned_data_sources`, at most every 2s and once at the+ end, `notion_workspace/scan.rs`); `list_runs` and the report show them only+ while the run is `running`.+## Related- Rules: `.agents/rules/rust/architecture.md`diff --git a/crates/import/Cargo.toml b/crates/import/Cargo.tomlindex 45af7fae75..69f8df9e0c 100644--- a/crates/import/Cargo.toml+++ b/crates/import/Cargo.toml@@ -5,6 +5,8 @@ publish = falseversion = "0.1.0"[dependencies]+agent_runtime_protocol = { path = "../agent_runtime_protocol", default-features = false }+agent_session = { path = "../agent_session", default-features = false }ai_billing = { path = "../ai_billing" }rootcause = { workspace = true }agent = { path = "../agent" }@@ -23,6 +25,8 @@ macro_authorization = { path = "../macro_authorization", default-features = falsmacro_user_id = { path = "../macro_user_id" }mcp_select = { path = "../mcp_select" }model-entity = { path = "../model-entity" }+import_notion = { path = "../import_notion" }+models_databases = { path = "../models_databases" }models_properties = { path = "../models_properties" }properties = { path = "../properties", default-features = false, features = ["ports",@@ -35,6 +39,7 @@ sqlx = { workspace = true }strum = { workspace = true }thiserror = { workspace = true }tokio = { workspace = true }+tokio-util = { workspace = true, features = ["rt"] }tracing = { workspace = true }utoipa = { workspace = true }uuid = { workspace = true }diff --git a/crates/import/src/domain/models.rs b/crates/import/src/domain/models.rsindex 81e67c512d..cbc4aa7cc3 100644--- a/crates/import/src/domain/models.rs+++ b/crates/import/src/domain/models.rs@@ -8,6 +8,17 @@ use serde::{Deserialize, Serialize};use utoipa::ToSchema;use uuid::Uuid;+mod notion_run;+mod notion_sharing;+pub use notion_run::{+ AdoptedRun, NotionJob, ReviewedRun, RunReview, TEAMMATE_STOPPED, TeamClaim, WrittenKind,+ WrittenObject,+};++pub use notion_sharing::{+ NotionAlreadyImported, NotionImporter, NotionSharing, NotionWriteChoices,+};+#[cfg(test)]mod test;@@ -76,6 +87,16 @@ impl ImportSource {}}+ /// The kind of item this source's current import paths stage: Linear+ /// issues, Notion pages, and Slack channels.+ pub fn default_kind(self) -> ImportKind {+ match self {+ ImportSource::Linear => ImportKind::Issue,+ ImportSource::Notion => ImportKind::Page,+ ImportSource::Slack => ImportKind::Channel,+ }+ }+/// The Macro entity type items from this source become. The mapping is/// fixed — importers have latitude on content, never on shape.pub fn entity_type(self) -> &'static str {@@ -116,6 +137,56 @@ impl ImportSource {}}+/// What an imported item is in its source system. Each source has its own+/// kinds; a Notion database and a Notion page, or a Linear project and a+/// Linear issue, are tracked and deduplicated separately.+#[derive(+ Debug,+ Clone,+ Copy,+ PartialEq,+ Eq,+ Serialize,+ Deserialize,+ JsonSchema,+ ToSchema,+ strum::EnumString,+ strum::AsRefStr,+)]+#[serde(rename_all = "snake_case")]+#[strum(serialize_all = "snake_case")]+pub enum ImportKind {+ /// A Linear issue.+ Issue,+ /// A Linear project.+ Project,+ /// A Linear document.+ Document,+ /// A Notion page.+ Page,+ /// A Notion database.+ Database,+ /// One data source (table) of a Notion database.+ DataSource,+ /// One row of a Notion data source.+ Row,+ /// A Slack channel.+ Channel,+}++impl ImportKind {+ /// The source this kind of item comes from.+ pub fn source(self) -> ImportSource {+ match self {+ ImportKind::Issue | ImportKind::Project | ImportKind::Document => ImportSource::Linear,+ ImportKind::Page | ImportKind::Database | ImportKind::DataSource | ImportKind::Row => {+ ImportSource::Notion+ }+ ImportKind::Channel => ImportSource::Slack,+ }+ }+}+/// Lifecycle of one import entity.#[derive(Debug,@@ -139,7 +210,7 @@ pub enum ImportStatus {Importing,/// A Macro entity exists for it (`entity_id`/`entity_type` are set).Imported,- /// The user declined it. Remembered so re-gathers don't re-stage it.+ /// The user declined it. Remembered so later discovery doesn't re-stage it.Discarded,}@@ -161,7 +232,7 @@ pub enum ImportStatus {#[serde(rename_all = "snake_case")]#[strum(serialize_all = "snake_case")]pub enum Initiator {- /// Staged by the onboarding gather job (`/setup`).+ /// Legacy: staged by the removed onboarding gather. No longer written.Onboarding,/// Staged by an AI chat session.Chat,@@ -171,7 +242,7 @@ pub enum Initiator {Manual,}-/// Lifecycle of a gather run (one per user × source).+/// Lifecycle of a discovery run (one per user × source).#[derive(Debug,Clone,@@ -181,24 +252,27 @@ pub enum Initiator {Serialize,Deserialize,ToSchema,+ JsonSchema,strum::EnumString,strum::AsRefStr,)]#[serde(rename_all = "snake_case")]#[strum(serialize_all = "snake_case")]pub enum RunStatus {- /// A gather job is discovering candidates.+ /// Slack discovery is staging candidates, or a Notion workspace import is+ /// being planned.Running,- /// The gather finished; staged rows (if any) are ready.+ /// Discovery or planning finished; staged rows (if any) are ready.Ready,- /// The gather finished and its configured automatic import is running.+ /// An approved Notion workspace import plan is being written.Importing,- /// The configured automatic import finished successfully.+ /// A Notion workspace import finished writing; `error` notes objects+ /// that could not be imported.Completed,- /// Gathering or automatic importing failed; `error` has details.- /// Retryable.+ /// Discovery, planning or writing failed; `error` has details. Retryable.Failed,- /// The user dismissed this source's import section.+ /// A Notion workspace import was cancelled. Older rows may hold it from+ /// the removed onboarding import, where it meant a dismissed section.Dismissed,}@@ -477,18 +551,144 @@ pub struct NotionDocMeta {pub url: Option<String>,/// One-line summary of what the page contains.pub summary: Option<String>,+ /// Where this object sits in a workspace import plan; absent for pages+ /// staged one at a time. Written by the workspace import, never by a+ /// model, so it is left out of the tool schema.+ #[serde(default, skip_serializing_if = "Option::is_none")]+ #[schemars(skip)]+ pub plan: Option<NotionPlanItem>,}impl NotionDocMeta {+ /// Metadata for one object of a workspace import plan, capped like+ /// everything else the ledger stores.+ pub fn for_plan(title: String, url: String, plan: NotionPlanItem) -> Self {+ Self {+ title,+ url: Some(url),+ summary: None,+ plan: Some(plan),+ }+ .capped()+ }+fn capped(self) -> Self {Self {title: truncated(self.title, MAX_TEXT),url: truncate_opt(self.url, MAX_TEXT),summary: truncate_opt(self.summary, MAX_SUMMARY),+ plan: self.plan.map(NotionPlanItem::capped),+ }+ }+}++/// What a workspace import does with one Notion object.+#[derive(+ Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema, ToSchema, strum::Display,+)]+#[serde(rename_all = "snake_case")]+#[strum(serialize_all = "snake_case")]+pub enum NotionDecision {+ /// Bring it over as it is: a page becomes a document, a data source a+ /// database.+ Copy,+ /// A data source that is really a task list: its rows become Macro tasks.+ Tasks,+ /// Leave it in Notion.+ Skip,+ /// Needs judgment the import cannot make; the agent handles it by hand.+ Hand,+}++/// Which Notion columns hold a task list's task fields, by property name.+#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema, ToSchema)]+#[serde(rename_all = "camelCase")]+pub struct NotionTaskFields {+ /// The status column.+ #[serde(default, skip_serializing_if = "Option::is_none")]+ pub status: Option<String>,+ /// The priority column.+ #[serde(default, skip_serializing_if = "Option::is_none")]+ pub priority: Option<String>,+ /// The due date column.+ #[serde(default, skip_serializing_if = "Option::is_none")]+ pub due: Option<String>,+ /// The assignee column.+ #[serde(default, skip_serializing_if = "Option::is_none")]+ pub assignee: Option<String>,+}++/// One object's place in a workspace import plan.+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema, ToSchema)]+#[serde(rename_all = "camelCase")]+pub struct NotionPlanItem {+ /// What the import does with it; absent while planning has staged the+ /// object but the decision model has not answered for it yet.+ #[serde(default, skip_serializing_if = "Option::is_none")]+ pub decision: Option<NotionDecision>,+ /// Why, in a short sentence, when the decision model gave one.+ #[serde(default, skip_serializing_if = "Option::is_none")]+ pub reason: Option<String>,+ /// Titles of the pages above it, outermost first.+ #[serde(default)]+ pub path: Vec<String>,+ /// For a data source: rows counted while planning.+ #[serde(default, skip_serializing_if = "Option::is_none")]+ pub rows: Option<u32>,+ /// For a data source: the titles of a few of its rows.+ #[serde(default, skip_serializing_if = "Vec::is_empty")]+ pub sample_titles: Vec<String>,+ /// For a task list: which columns hold the task fields.+ #[serde(default, skip_serializing_if = "Option::is_none")]+ pub task_fields: Option<NotionTaskFields>,+ /// Whether what it becomes is shared with the user's team.+ #[serde(default)]+ pub shared: bool,+ /// Why it is (or is not) shared, when the decision model chose or the+ /// import could not share it.+ #[serde(default, skip_serializing_if = "Option::is_none")]+ pub shared_reason: Option<String>,+ /// Whether the user may change `shared`: false for users without a team.+ #[serde(default)]+ pub shareable: bool,+ /// A teammate's copy shared with the team, which this object links to+ /// instead of being imported again.+ #[serde(default, skip_serializing_if = "Option::is_none")]+ pub already_imported: Option<NotionAlreadyImported>,+}++impl NotionPlanItem {+ fn capped(self) -> Self {+ Self {+ reason: truncate_opt(self.reason, MAX_SUMMARY),+ shared_reason: truncate_opt(self.shared_reason, MAX_SUMMARY),+ already_imported: self.already_imported.map(|copy| NotionAlreadyImported {+ by: truncated(copy.by, MAX_TEXT),+ ..copy+ }),+ path: self+ .path+ .into_iter()+ .take(MAX_PLAN_DEPTH)+ .map(|title| truncated(title, MAX_TEXT))+ .collect(),+ sample_titles: self+ .sample_titles+ .into_iter()+ .take(MAX_SAMPLE_TITLES)+ .map(|title| truncated(title, MAX_TEXT))+ .collect(),+ ..self}}}+/// Row titles a plan item keeps for a data source.+const MAX_SAMPLE_TITLES: usize = 5;++/// Deepest ancestor path a plan item records.+const MAX_PLAN_DEPTH: usize = 12;+/// A person active in a staged Slack channel.#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema, ToSchema)]pub struct SlackParticipant {@@ -618,9 +818,17 @@ pub struct ImportEntity {pub team_id: Option<Uuid>,/// The external system the item comes from.pub source: ImportSource,+ /// What the item is in its source system.+ pub kind: ImportKind,/// Stable id in the source system (see/// [`ImportSource::normalize_foreign_id`]).pub foreign_id: String,+ /// The import row this item was imported under (a subpage's parent page,+ /// a row's database), when it was imported as part of a tree.+ pub parent_id: Option<Uuid>,+ /// The agent session review that approved importing this item, when it+ /// came from one.+ pub review_id: Option<Uuid>,/// Where the row is in its lifecycle.pub status: ImportStatus,/// Where it was first staged from.@@ -634,34 +842,147 @@ pub struct ImportEntity {pub entity_type: Option<String>,/// Failure detail from the last import attempt, when one failed.pub last_error: Option<String>,+ /// The teammate's row this one waits on: that teammate is writing the+ /// same object for the team, and this row links to their copy once it+ /// is imported.+ #[serde(default, skip_serializing_if = "Option::is_none")]+ pub waiting_on: Option<Uuid>,/// When the row was first staged.pub created_at: DateTime<Utc>,/// When the row last changed.pub updated_at: DateTime<Utc>,}-/// Gather-run state for one user × source.+/// Discovery-run state for one user × source.#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, ToSchema)]pub struct ImportRun {- /// The source this run gathered from.+ /// The source this run discovered from.pub source: ImportSource,/// Where the run is in its lifecycle.pub status: RunStatus,- /// Whether this run should import its onboarding-staged candidates as- /// soon as gathering finishes.- pub auto_import: bool,- /// Gather or automatic-import failure detail, when the run failed.+ /// Discovery failure detail, when the run failed.pub error: Option<String>,/// When the run state last changed.pub updated_at: DateTime<Utc>,+ /// What the run has found in the source so far, while it is `running`.+ #[serde(default, skip_serializing_if = "Option::is_none")]+ pub scanned: Option<ScanCounts>,+ /// The review presenting the run's plan, once one does. A Notion plan is+ /// presented while it is still `running`; rows staged after that take it.+ #[serde(default, skip_serializing_if = "Option::is_none")]+ pub review_id: Option<Uuid>,+}++/// What a planning run has found in the source so far.+#[derive(+ Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema, ToSchema,+)]+#[serde(rename_all = "camelCase")]+pub struct ScanCounts {+ /// Pages found.+ pub pages: u32,+ /// Data sources (database tables) found.+ pub data_sources: u32,}-/// The full import aggregate for a user: gather runs plus visible ledger+/// The full import aggregate for a user: discovery runs plus visible ledger/// rows (their own, and teammates' team-imported rows).#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, ToSchema)]pub struct ImportState {- /// Gather runs, one per source that ever gathered.+ /// Discovery runs, one per source that ever ran discovery.pub runs: Vec<ImportRun>,/// Visible import entities, newest first.pub entities: Vec<ImportEntity>,}++/// One object a workspace import report calls out.+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema, ToSchema)]+#[serde(rename_all = "camelCase")]+pub struct NotionReportItem {+ /// The ledger row.+ pub import_id: Uuid,+ /// The object's title.+ pub title: String,+ /// What the object is.+ pub kind: ImportKind,+ /// Its Notion URL.+ #[serde(default, skip_serializing_if = "Option::is_none")]+ pub url: Option<String>,+ /// Why it is called out: the decision model's reason, or the failure.+ #[serde(default, skip_serializing_if = "Option::is_none")]+ pub reason: Option<String>,+}++/// Where a Notion workspace import stands.+#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema, ToSchema)]+#[serde(rename_all = "camelCase")]+pub struct NotionImportReport {+ /// The run's state: `running` while planning, `ready` once a plan is+ /// staged, `importing` while writing, `completed`, `failed`, or+ /// `dismissed` when cancelled. Absent before the first run.+ #[serde(default, skip_serializing_if = "Option::is_none")]+ pub status: Option<RunStatus>,+ /// Why the run failed, when it did.+ #[serde(default, skip_serializing_if = "Option::is_none")]+ pub error: Option<String>,+ /// What planning has found in Notion so far, while it is `running`.+ #[serde(default, skip_serializing_if = "Option::is_none")]+ pub scanned: Option<ScanCounts>,+ /// Pages in the plan.+ pub pages: u32,+ /// Data sources (database tables) in the plan.+ pub data_sources: u32,+ /// Plan objects the decision model has not answered for yet, while+ /// planning is still `running`.+ #[serde(default)]+ pub deciding: u32,+ /// Whether the user is on a team. Without one, everything the import+ /// writes stays visible only to them.+ #[serde(default)]+ pub on_team: bool,+ /// Objects to copy as they are.+ pub copy: u32,+ /// Data sources whose rows become tasks.+ pub tasks: u32,+ /// Objects left in Notion.+ pub skip: u32,+ /// Objects that need handling by hand.+ pub hand: u32,+ /// Pages written as documents.+ pub documents: u32,+ /// Data sources written as databases.+ pub databases: u32,+ /// Rows those databases hold, every one counted while planning.+ pub database_rows: u32,+ /// Tasks the task lists become, one per row.+ pub task_rows: u32,+ /// Plan objects already imported.+ pub imported: u32,+ /// Plan objects still waiting to be written.+ pub waiting: u32,+ /// Plan objects being written now.+ pub writing: u32,+ /// Plan objects waiting for a teammate who is writing the same object+ /// for the team; they link to the teammate's copy once it is written.+ #[serde(default)]+ pub waiting_on_teammates: u32,+ /// Written plan objects shared with the user's team.+ #[serde(default)]+ pub shared_with_team: u32,+ /// Plan objects a teammate already imported and shared with the team;+ /// they link to that copy and are not counted as copies.+ #[serde(default)]+ pub already_in_macro: u32,+ /// Who imported the objects already in Macro, most first.+ #[serde(default)]+ pub already_imported_by: Vec<NotionImporter>,+ /// The review the plan is tied to, once presented.+ #[serde(default, skip_serializing_if = "Option::is_none")]+ pub review_id: Option<Uuid>,+ /// Objects that need handling by hand, with reasons.+ #[serde(default)]+ pub hand_items: Vec<NotionReportItem>,+ /// Objects that failed to import, with the failure.+ #[serde(default)]+ pub failures: Vec<NotionReportItem>,+}diff --git a/crates/import/src/domain/models/notion_run.rs b/crates/import/src/domain/models/notion_run.rsnew file mode 100644index 0000000000..4f7ca9d815--- /dev/null+++ b/crates/import/src/domain/models/notion_run.rs@@ -0,0 +1,164 @@+//! What makes a Notion workspace import resumable: the job recorded on its+//! run, and what a write already created.++use agent_session::domain::model::AgentSessionId;+use import_notion::domain::model::{ImportScope, NotionId};+use macro_user_id::user_id::MacroUserIdStr;+use uuid::Uuid;++use super::{NotionSharing, RunStatus};++/// The job a Notion run is doing, recorded on the run so another process can+/// pick it up.+#[derive(Debug, Clone, PartialEq, Eq)]+pub enum NotionJob {+ /// Planning `scope`. Planning only reads, so it restarts from the top.+ Plan {+ /// What part of the workspace the plan covers.+ scope: ImportScope,+ /// Who the plan shares what it writes with.+ sharing: NotionSharing,+ },+ /// Writing the plan tied to `review_id`.+ Write {+ /// The review that approved the plan.+ review_id: Uuid,+ },+}++/// A Notion run whose process stopped holding it, now held by the caller.+#[derive(Debug, Clone, PartialEq, Eq)]+pub struct AdoptedRun {+ /// Whose import it is.+ pub user: MacroUserIdStr<'static>,+ /// `running` (planning) or `importing` (writing).+ pub status: RunStatus,+ /// The job to resume; `None` when its process stopped before recording it.+ pub job: Option<NotionJob>,+ /// How many times the run has been adopted, this time included.+ pub adoptions: u16,+}++/// A Notion run as a transition left it, with the review presenting it.+#[derive(Debug, Clone, PartialEq, Eq)]+pub struct ReviewedRun {+ /// The run's new status.+ pub status: RunStatus,+ /// The run's error, when it has one.+ pub error: Option<String>,+ /// The review presenting the run and its session, once both are known.+ pub review: Option<RunReview>,+}++/// The review a Notion run is tied to, and the agent session presenting it.+#[derive(Debug, Clone, Copy, PartialEq, Eq)]+pub struct RunReview {+ /// The review.+ pub review_id: Uuid,+ /// The session the review is presented in.+ pub session_id: AgentSessionId,+}++/// One thing a write created for a ledger row, recorded as it is created so+/// a resumed write reuses it.+#[derive(+ Debug, Clone, Copy, PartialEq, Eq, Hash, strum::EnumString, strum::AsRefStr, strum::Display,+)]+#[strum(serialize_all = "snake_case")]+pub enum WrittenKind {+ /// The folder a page with children became.+ Folder,+ /// The document a page became; minted before it is written.+ Document,+ /// The database a data source became.+ Database,+ /// That database's starter table.+ Table,+ /// That table's title column.+ TitleColumn,+ /// The database's columns were created (the id is the database).+ Schema,+ /// A data source row, by its Notion id, and the database row it became.+ Row,+ /// The folder holding a database's row bodies.+ BodiesFolder,+ /// The column linking each database row to its body.+ PageColumn,+ /// A row body, by the row's Notion id; minted before it is written.+ Body,+ /// The folder a task list became.+ TasksFolder,+ /// A task, by its row's Notion id; minted before it is written.+ Task,+ /// A relation column, by its Notion property name.+ Relation,+ /// A relation column whose rows are all linked, by property name.+ RelationLinked,+}++/// One thing a write created: `kind` for ledger row `import_id` (and, for+/// per-row kinds, the Notion `object` it stands for) is Macro's `macro_id`.+#[derive(Debug, Clone, PartialEq, Eq)]+pub struct WrittenObject {+ /// The ledger row it was written for.+ pub import_id: Uuid,+ /// What it is.+ pub kind: WrittenKind,+ /// The Notion row or property it is for; empty for the row's own object.+ pub object: String,+ /// The Macro id.+ pub macro_id: Uuid,+}++impl WrittenObject {+ /// The ledger row's own object of `kind`.+ pub fn own(import_id: Uuid, kind: WrittenKind, macro_id: Uuid) -> Self {+ Self {+ import_id,+ kind,+ object: String::new(),+ macro_id,+ }+ }++ /// The object of `kind` for Notion row `row`.+ pub fn for_row(import_id: Uuid, kind: WrittenKind, row: &NotionId, macro_id: Uuid) -> Self {+ Self {+ import_id,+ kind,+ object: row.as_str().to_string(),+ macro_id,+ }+ }+}++/// What a row waiting on a teammate's copy says once that teammate's import+/// stopped before writing it.+pub const TEAMMATE_STOPPED: &str =+ "A teammate's import of this stopped before it finished. Import it again.";++/// Who writes a Notion object for the team, as seen by one ledger row.+///+/// The team keeps one copy of each object (`import_entity_team_object_key`):+/// the row holding the team's claim writes it, and every other teammate's row+/// links to that copy.+#[derive(Debug, Clone, PartialEq, Eq)]+pub enum TeamClaim {+ /// This row holds the claim: write the object and share it.+ Held,+ /// Nobody holds a claim and this row does not take one: write a copy+ /// for the user only.+ Free,+ /// A teammate's copy exists: link to it.+ Linked {+ /// The teammate's entity.+ entity_id: String,+ /// Its type.+ entity_type: String,+ },+ /// A teammate is writing it now: wait for their row.+ Waiting {+ /// The teammate's ledger row.+ holder: Uuid,+ },+}diff --git a/crates/import/src/domain/models/notion_sharing.rs b/crates/import/src/domain/models/notion_sharing.rsnew file mode 100644index 0000000000..ba77283d40--- /dev/null+++ b/crates/import/src/domain/models/notion_sharing.rs@@ -0,0 +1,66 @@+//! Who sees what a Notion workspace import writes, and what teammates+//! already imported.++use schemars::JsonSchema;+use serde::{Deserialize, Serialize};+use utoipa::ToSchema;+use uuid::Uuid;++/// A teammate's copy of a Notion object, shared with the user's team.+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema, ToSchema)]+#[serde(rename_all = "camelCase")]+pub struct NotionAlreadyImported {+ /// The teammate who imported it, by email.+ pub by: String,+ /// The Macro entity it became.+ pub entity_id: String,+ /// That entity's type (`md`, `project`).+ pub entity_type: String,+}++/// Who a Notion workspace import shares what it writes with.+#[derive(+ Debug,+ Clone,+ Copy,+ PartialEq,+ Eq,+ Serialize,+ Deserialize,+ JsonSchema,+ ToSchema,+ strum::EnumString,+ strum::AsRefStr,+)]+#[serde(rename_all = "snake_case")]+#[strum(serialize_all = "snake_case")]+pub enum NotionSharing {+ /// Everything the import writes is shared with the user's team.+ Team,+ /// Everything stays visible to the user only.+ Personal,+ /// The decision model chooses per object.+ Ai,+}++/// What the owner changed in a plan before approving it.+#[derive(Debug, Clone, Default, PartialEq, Eq)]+pub struct NotionWriteChoices {+ /// Plan rows left out of the import.+ pub excluded: Vec<Uuid>,+ /// Plan rows to share with the team although the plan keeps them.+ pub shared: Vec<Uuid>,+ /// Plan rows to keep visible only to the owner although the plan shares+ /// them.+ pub personal: Vec<Uuid>,+}++/// A teammate who already imported some of a plan's objects.+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema, ToSchema)]+#[serde(rename_all = "camelCase")]+pub struct NotionImporter {+ /// The teammate, by email.+ pub by: String,+ /// How many of the plan's objects they imported.+ pub count: u32,+}diff --git a/crates/import/src/domain/ports.rs b/crates/import/src/domain/ports.rsindex 15621997c3..c899df2573 100644--- a/crates/import/src/domain/ports.rs+++ b/crates/import/src/domain/ports.rs@@ -1,11 +1,16 @@//! Ports (traits) the import domain depends on.use super::models::{- ImportEntity, ImportRun, ImportSource, ImportSourceBinding, ImportStatus, ImportTargetKey,- ImportTargetKind, ImportTargetReservation, Initiator, RunStatus, SlackConversationId,- SlackConversationPage, SlackMemberPage, SlackUserPage, SlackWorkspaceId,+ AdoptedRun, ImportEntity, ImportKind, ImportRun, ImportSource, ImportSourceBinding,+ ImportStatus, ImportTargetKey, ImportTargetKind, ImportTargetReservation, Initiator, NotionJob,+ ReviewedRun, RunStatus, ScanCounts, SlackConversationId, SlackConversationPage,+ SlackMemberPage, SlackUserPage, SlackWorkspaceId, TeamClaim, WrittenObject,};+use agent_session::domain::model::AgentSessionId;+use agent_session::domain::notifications::PendingNotification;+use import_notion::domain::model::NotionId;use macro_user_id::user_id::MacroUserIdStr;+use std::collections::HashMap;use std::time::Duration;use thiserror::Error;use uuid::Uuid;@@ -16,6 +21,9 @@ pub enum ImportError {/// AI work was refused or usage validation is temporarily unavailable.#[error(transparent)]Admission(#[from] ai_billing::AiAdmissionError),+ /// This source does not support user-triggered discovery.+ #[error("manual discovery is not supported for {source}", source = .0.as_ref())]+ UnsupportedDiscovery(ImportSource),/// Database failure.#[error("database error: {0}")]Db(rootcause::Report),@@ -34,6 +42,19 @@ pub enum ImportError {/// Completion must follow a committed reservation for exactly this UUID.#[error("import target is not reserved")]TargetNotReserved,+ /// Notion could not be read.+ #[error(transparent)]+ Notion(#[from] import_notion::domain::ports::NotionError),+ /// The user has not connected Notion, or turned it off.+ #[error("Notion is not connected")]+ NotionNotConnected,+ /// The user is on a team and has not said who should see what a Notion+ /// import writes.+ #[error("who should see the Notion import was not chosen")]+ NotionSharingRequired,+ /// The import is not in a state that allows this; the message says why.+ #[error("{0}")]+ NotReady(&'static str),/// Anything else.#[error(transparent)]Other(#[from] anyhow::Error),@@ -67,10 +88,10 @@ pub enum SlackSourceError {/// Opens live Slack workspace sessions for import discovery and membership reads.pub trait SlackWorkspaceSource: Send + Sync + 'static {- /// Workspace reader scoped to one gather or one import batch.+ /// Workspace reader scoped to one discovery or one import batch.type Session: SlackWorkspaceSession;- /// Open a session scoped to one gather or one import batch.+ /// Open a session scoped to one discovery or one import batch./// Returns [`SlackSourceError::NotConnected`] when the user has no Slack/// connector, or [`SlackSourceError::ToolsUnavailable`] when the connector/// lacks a needed capability.@@ -80,7 +101,7 @@ pub trait SlackWorkspaceSource: Send + Sync + 'static {) -> impl Future<Output = std::result::Result<Self::Session, SlackSourceError>> + Send;}-/// Paginated workspace reads scoped to one gather or one import batch.+/// Paginated workspace reads scoped to one discovery or one import batch./// Pass `None` to read the first page, then the returned continuation cursor.pub trait SlackWorkspaceSession: Send + Sync {/// List conversations visible to the connected user.@@ -103,10 +124,10 @@ pub trait SlackWorkspaceSession: Send + Sync {) -> impl Future<Output = std::result::Result<SlackUserPage, SlackSourceError>> + Send;}-/// Persistence for the import ledger and gather runs.+/// Persistence for the import ledger and discovery runs.////// Status writes are compare-and-swaps that report whether they happened, so-/// concurrent gather jobs, import jobs, and user actions interleave without+/// concurrent discovery jobs, import jobs, and user actions interleave without/// clobbering each other.pub trait ImportRepo: Send + Sync + 'static {/// The user's own row for `(source, foreign_id)`, regardless of status.@@ -134,6 +155,7 @@ pub trait ImportRepo: Send + Sync + 'static {&self,user: &MacroUserIdStr<'static>,source: ImportSource,+ kind: ImportKind,initiator: Initiator,foreign_id: &str,metadata: &serde_json::Value,@@ -148,6 +170,7 @@ pub trait ImportRepo: Send + Sync + 'static {&self,user: &MacroUserIdStr<'static>,source: ImportSource,+ kind: ImportKind,initiator: Initiator,foreign_id: &str,metadata: &serde_json::Value,@@ -181,7 +204,10 @@ pub trait ImportRepo: Send + Sync + 'static {ids: &[Uuid],) -> impl Future<Output = Result<Vec<ImportEntity>>> + Send;- /// CAS one `importing` row to `imported` with the entity it became.+ /// CAS one `importing` row to `imported` with the entity it became,+ /// recording `team_id` when given (a row's team claim is kept+ /// otherwise). Rows waiting on it ([`NotionPlanRepo::wait_on`]) link to+ /// the same entity.fn mark_imported(&self,user: &MacroUserIdStr<'static>,@@ -191,7 +217,8 @@ pub trait ImportRepo: Send + Sync + 'static {team_id: Option<Uuid>,) -> impl Future<Output = Result<Option<ImportEntity>>> + Send;- /// CAS one `importing` row back to `staged`, recording the failure.+ /// CAS one `importing` row back to `staged`, recording the failure and+ /// dropping its team claim. Rows waiting on it go back to `staged` too.fn mark_import_failed(&self,user: &MacroUserIdStr<'static>,@@ -210,7 +237,8 @@ pub trait ImportRepo: Send + Sync + 'static {/// CAS the user's `importing` rows whose heartbeat stopped more than/// `older_than_secs` ago back to `staged`. Import jobs are in-process/// tasks: a crash or restart mid-job orphans its rows, and nothing else- /// can move them again. Returns how many were reaped.+ /// can move them again. Notion workspace plan rows are left to the+ /// adopting process ([`NotionRunRepo`]). Returns how many were reaped.fn fail_stale_importing(&self,user: &MacroUserIdStr<'static>,@@ -225,26 +253,6 @@ pub trait ImportRepo: Send + Sync + 'static {id: Uuid,) -> impl Future<Output = Result<bool>> + Send;- /// Discard all of the user's remaining `staged` rows with the given- /// initiator (an explicit "no" — `stage()` refuses to re-stage them).- /// Returns how many flipped.- fn discard_staged_by_initiator(- &self,- user: &MacroUserIdStr<'static>,- initiator: Initiator,- ) -> impl Future<Output = Result<u64>> + Send;-- /// DELETE the user's remaining unreserved `staged` rows with the given- /// initiator. Onboarding candidates reserved by an active or retryable- /// configured auto-import run survive completion cleanup; other- /// candidates the user never acted on vanish and stay re-stageable later.- /// Returns how many were removed.- fn delete_staged_by_initiator(- &self,- user: &MacroUserIdStr<'static>,- initiator: Initiator,- ) -> impl Future<Output = Result<u64>> + Send;-/// The user's team, when they are on one.fn user_team_id(&self,@@ -258,33 +266,31 @@ pub trait ImportRepo: Send + Sync + 'static {team_id: Uuid,) -> impl Future<Output = Result<Vec<MacroUserIdStr<'static>>>> + Send;- /// All gather runs for the user.+ /// All discovery runs for the user.fn list_runs(&self,user: &MacroUserIdStr<'static>,) -> impl Future<Output = Result<Vec<ImportRun>>> + Send;- /// CAS a gather run to `running`. New rows persist `auto_import`- /// atomically; retries preserve the existing run's setting. Wins when no- /// run row exists yet, or when the existing row's status is in `from`.- /// Returns whether this call won.+ /// CAS a discovery run to `running`. Wins when no run exists or the+ /// existing status is in `from`. Returns whether this call won.fn start_run(&self,user: &MacroUserIdStr<'static>,source: ImportSource,from: &[RunStatus],- auto_import: bool,) -> impl Future<Output = Result<bool>> + Send;- /// CAS a run to `running` with `auto_import = false`. Wins when no run- /// exists or the existing status is in `from`. Unlike `start_run`, a- /// successful claim always clears `auto_import`.- fn start_manual_run(+ /// CAS the user's runs stuck in `running` (or the legacy `importing`) for+ /// more than `older_than_secs` to `failed`. Discovery is an in-process+ /// task: a crash or deploy mid-run leaves the row behind, and the UI+ /// cannot start a new run while one looks active. Notion runs are adopted+ /// instead ([`NotionRunRepo`]). Returns how many were reaped.+ fn fail_stale_runs(&self,user: &MacroUserIdStr<'static>,- source: ImportSource,- from: &[RunStatus],- ) -> impl Future<Output = Result<bool>> + Send;+ older_than_secs: i64,+ ) -> impl Future<Output = Result<u64>> + Send;/// CAS the `running` run for `source` to `to` (ready/failed), recording/// `error` when there is one. Returns whether it happened.@@ -295,42 +301,6 @@ pub trait ImportRepo: Send + Sync + 'static {to: RunStatus,error: Option<&str>,) -> impl Future<Output = Result<bool>> + Send;-- /// CAS a run from one of `from` to `to` without touching its error.- fn transition_run(- &self,- user: &MacroUserIdStr<'static>,- source: ImportSource,- from: &[RunStatus],- to: RunStatus,- ) -> impl Future<Output = Result<bool>> + Send;-- /// Atomically claim a ready, auto-import-enabled run and all of its own- /// onboarding-staged candidates. `None` means another caller won or the- /// run is not ready/configured; `Some([])` is a valid empty run.- fn begin_auto_import(- &self,- user: &MacroUserIdStr<'static>,- source: ImportSource,- ) -> impl Future<Output = Result<Option<Vec<ImportEntity>>>> + Send;-- /// Finish an auto-import run after every claimed row has settled. The run- /// becomes `completed` only when all claimed rows are imported; otherwise- /// it becomes `failed`. Returns the terminal status when this call won.- fn finish_auto_import(- &self,- user: &MacroUserIdStr<'static>,- source: ImportSource,- ids: &[Uuid],- ) -> impl Future<Output = Result<Option<RunStatus>>> + Send;-- /// Repair auto-import run rows after process interruption. An `importing`- /// run with no importing onboarding rows is terminal: `failed` when a- /// staged row carries an import error, otherwise `completed`.- fn reconcile_auto_import_runs(- &self,- user: &MacroUserIdStr<'static>,- ) -> impl Future<Output = Result<u64>> + Send;}/// Narrow canonical Slack ledger persistence shared by archive and onboarding.@@ -479,6 +449,34 @@ pub enum ImportedDocumentPropertyValue {},}+/// What a Notion page's references point to in Macro, so the page converts+/// with live mentions instead of links back to Notion.+#[derive(Debug, Clone, Default, PartialEq)]+pub struct NotionLinks {+ /// Notion page → the Macro document it becomes.+ pub pages: HashMap<NotionId, NotionPageLink>,+ /// Notion person → the Macro user they are.+ pub users: HashMap<NotionId, NotionUserLink>,+}++/// The Macro document a Notion page becomes.+#[derive(Debug, Clone, PartialEq)]+pub struct NotionPageLink {+ /// The document id, minted before the document exists.+ pub document_id: Uuid,+ /// Its name.+ pub name: String,+}++/// The Macro user a Notion person is.+#[derive(Debug, Clone, PartialEq)]+pub struct NotionUserLink {+ /// The Macro user id.+ pub user_id: MacroUserIdStr<'static>,+ /// Their email, lowercase.+ pub email: String,+}+/// Creates the Macro entities imports become. Implemented by the host/// service against its real document/channel services; the fixed/// source → entity-type mapping is enforced by the import service, which@@ -494,12 +492,14 @@ pub trait EntityCreator: Send + Sync + 'static {properties: &ImportedTaskProperties,) -> impl Future<Output = anyhow::Result<String>> + Send;- /// Create a markdown document. Returns the new entity id.- fn create_markdown_doc(+ /// Create a document from a Notion page in Notion's enhanced markdown,+ /// its references resolved through `links`. Returns the new entity id.+ fn create_notion_document(&self,user: &MacroUserIdStr<'static>,name: &str,- markdown: &str,+ notion_markdown: &str,+ links: &NotionLinks,properties: &ImportedDocumentProperties,) -> impl Future<Output = anyhow::Result<String>> + Send;@@ -516,3 +516,359 @@ pub trait EntityCreator: Send + Sync + 'static {participant_emails: &[String],) -> impl Future<Output = anyhow::Result<Uuid>> + Send;}++/// A database a workspace import just created, with the starter table and its+/// title column, which the import reuses for the first data source.+#[derive(Debug, Clone, PartialEq, Eq)]+pub struct NewDatabase {+ /// The new database.+ pub database_id: Uuid,+ /// The table every new database starts with.+ pub table: models_databases::TableId,+ /// That table's title column.+ pub title_column: models_databases::ColumnId,+}++/// Something a workspace import shares with the user's team.+#[derive(Debug, Clone, Copy, PartialEq, Eq)]+pub enum SharedEntity {+ /// A folder, and everything in it.+ Folder(Uuid),+ /// A document.+ Document(Uuid),+ /// A database, with its rows.+ Database(Uuid),+}++/// A team member a Notion person can be matched to, by email.+#[derive(Debug, Clone, PartialEq, Eq)]+pub struct TeamMember {+ /// The member's Macro user id.+ pub user_id: MacroUserIdStr<'static>,+ /// Their email, lowercase.+ pub email: String,+}++/// Creates the Macro side of a workspace import: folders mirroring the+/// source's tree, documents and tasks inside them, and databases.+///+/// Every entity is created as `user`, through the same services the app+/// uses, so permissions, search and sync behave as for anything the user+/// made by hand.+pub trait WorkspaceWriter: Send + Sync + 'static {+ /// Create a folder, inside `parent` when given. Returns the folder id.+ fn create_folder(+ &self,+ user: &MacroUserIdStr<'static>,+ name: &str,+ parent: Option<Uuid>,+ ) -> impl Future<Output = anyhow::Result<Uuid>> + Send;++ /// Whether `folder` still exists and `user` can create inside it.+ fn folder_exists(+ &self,+ user: &MacroUserIdStr<'static>,+ folder: Uuid,+ ) -> impl Future<Output = anyhow::Result<bool>> + Send;++ /// Create document `id` from a Notion page (Notion's enhanced markdown,+ /// references resolved through `links`) inside `folder` when given,+ /// applying `properties` best-effort. Returns the document id. Safe to+ /// repeat: when document `id` exists already (an interrupted earlier+ /// attempt), it is left as it is and its id returned.+ #[allow(clippy::too_many_arguments)]+ fn create_document(+ &self,+ user: &MacroUserIdStr<'static>,+ id: Uuid,+ name: &str,+ notion_markdown: &str,+ links: &NotionLinks,+ properties: &ImportedDocumentProperties,+ folder: Option<Uuid>,+ ) -> impl Future<Output = anyhow::Result<String>> + Send;++ /// Create task `id` from a Notion page (as for [`Self::create_document`],+ /// and as safe to repeat) inside `folder` when given, applying+ /// `properties` best-effort. Returns the task id.+ #[expect(+ clippy::too_many_arguments,+ reason = "a task carries its id, content, links, properties and folder"+ )]+ fn create_task(+ &self,+ user: &MacroUserIdStr<'static>,+ id: Uuid,+ name: &str,+ notion_markdown: &str,+ links: &NotionLinks,+ properties: &ImportedTaskProperties,+ folder: Option<Uuid>,+ ) -> impl Future<Output = anyhow::Result<String>> + Send;++ /// Create an empty database named `name`.+ fn create_database(+ &self,+ user: &MacroUserIdStr<'static>,+ name: &str,+ ) -> impl Future<Output = anyhow::Result<NewDatabase>> + Send;++ /// Apply `ops` to a database `user` created, in one transaction. Ops may+ /// refer to ids minted earlier in the same batch.+ fn apply_database_ops(+ &self,+ user: &MacroUserIdStr<'static>,+ database_id: Uuid,+ ops: Vec<models_databases::DatabaseOp>,+ ) -> impl Future<Output = anyhow::Result<Vec<models_databases::OpResult>>> + Send;++ /// Share `entity`, which `user` owns, with `user`'s team at edit level.+ /// Sharing a folder reaches everything inside it, folders included.+ /// Safe to repeat.+ fn share_with_team(+ &self,+ user: &MacroUserIdStr<'static>,+ entity: SharedEntity,+ ) -> impl Future<Output = anyhow::Result<()>> + Send;++ /// The members of `user`'s team (including `user`), for matching Notion+ /// people by email. Empty when the user has no team.+ fn team_members(+ &self,+ user: &MacroUserIdStr<'static>,+ ) -> impl Future<Output = anyhow::Result<Vec<TeamMember>>> + Send;+}++/// One object of a workspace import plan, ready to stage.+#[derive(Debug, Clone, PartialEq)]+pub struct PlanRow {+ /// What the object is.+ pub kind: ImportKind,+ /// The object's id in the source.+ pub foreign_id: String,+ /// The `foreign_id` of the plan row it sits under, if any.+ pub parent_foreign_id: Option<String>,+ /// The row's metadata, already capped.+ pub metadata: serde_json::Value,+}++/// Ledger operations a workspace import needs beyond single-row staging.+pub trait NotionPlanRepo: Send + Sync + 'static {+ /// Replace `user`'s current plan for `source`: drop the staged rows of the+ /// previous plan, then stage `rows` in order (parents before children),+ /// linking each to its parent row and to the review the `running` run+ /// records, if one presents it already. Objects already importing or+ /// imported keep their rows. Returns the rows now staged.+ fn replace_plan(+ &self,+ user: &MacroUserIdStr<'static>,+ source: ImportSource,+ rows: &[PlanRow],+ ) -> impl Future<Output = Result<Vec<ImportEntity>>> + Send;++ /// Rewrite the metadata of the user's staged plan rows for `rows`'+ /// objects (by `foreign_id`), as the decision model answers for them.+ /// Rows no longer staged are left alone. Returns how many changed.+ fn update_plan_rows(+ &self,+ user: &MacroUserIdStr<'static>,+ source: ImportSource,+ rows: &[PlanRow],+ ) -> impl Future<Output = Result<u64>> + Send;++ /// Teammates' rows for the Notion objects `foreign_ids` that hold the+ /// user's team's claim (`importing` or `imported`, shared with the team).+ fn team_copies(+ &self,+ user: &MacroUserIdStr<'static>,+ foreign_ids: &[String],+ ) -> impl Future<Output = Result<Vec<ImportEntity>>> + Send;++ /// Record the sharing the user chose on their staged plan rows: `shared`+ /// become shared with the team, `personal` not. Returns how many changed.+ fn set_plan_sharing(+ &self,+ user: &MacroUserIdStr<'static>,+ shared: &[Uuid],+ personal: &[Uuid],+ ) -> impl Future<Output = Result<u64>> + Send;++ /// CAS the user's own `staged` rows in `ids` to `importing`, claiming+ /// each of `shared` for `team_id` unless a teammate holds the team's+ /// claim on the same object. The team's partial unique index makes the+ /// claim exclusive, so two teammates writing the same object at once+ /// cannot both hold it. Returns the rows that moved, with the claims+ /// taken.+ fn claim_importing(+ &self,+ user: &MacroUserIdStr<'static>,+ ids: &[Uuid],+ shared: &[Uuid],+ team_id: Option<Uuid>,+ ) -> impl Future<Output = Result<Vec<ImportEntity>>> + Send;++ /// Who writes the object of the user's `importing` row `import_id` for+ /// `team_id`. With `claim` set the row takes the claim when nobody holds+ /// it, or when its holder's import stopped (no live lease on a writing+ /// run); otherwise it writes a copy of its own.+ fn claim_team_object(+ &self,+ user: &MacroUserIdStr<'static>,+ import_id: Uuid,+ team_id: Uuid,+ claim: bool,+ ) -> impl Future<Output = Result<TeamClaim>> + Send;++ /// Make the user's `importing` row `import_id` wait on the teammate's row+ /// `holder`, which links it once `holder` is imported. Returns `false`+ /// (and changes nothing) when `holder` is no longer being written.+ fn wait_on(+ &self,+ user: &MacroUserIdStr<'static>,+ import_id: Uuid,+ holder: Uuid,+ ) -> impl Future<Output = Result<bool>> + Send;++ /// Tie the current plan to `review_id`, presented in `session_id`: its+ /// staged rows, and the run while it is still planning (`running`), so+ /// rows staged later are tied too. The first review presenting a plan+ /// keeps its session, which hears when the plan is ready and when its+ /// write ends. Returns how many rows it tied.+ fn attach_review(+ &self,+ user: &MacroUserIdStr<'static>,+ source: ImportSource,+ review_id: Uuid,+ session_id: AgentSessionId,+ ) -> impl Future<Output = Result<u64>> + Send;++ /// Move `user`'s run for `source` to `to` when it is in one of `from`,+ /// dropping any lease on it. Returns whether the run moved.+ fn transition_run(+ &self,+ user: &MacroUserIdStr<'static>,+ source: ImportSource,+ from: &[RunStatus],+ to: RunStatus,+ error: Option<&str>,+ ) -> impl Future<Output = Result<bool>> + Send;++ /// Record what `user`'s `running` run for `source` has found so far.+ /// Returns whether the run was still running.+ fn record_scan(+ &self,+ user: &MacroUserIdStr<'static>,+ source: ImportSource,+ scanned: ScanCounts,+ ) -> impl Future<Output = Result<bool>> + Send;+}++/// Who holds the user's Notion run, so an import outlives the process running+/// it.+///+/// The process planning or writing holds a lease on the run and renews it+/// while it works. Once a lease lapses (a crash or a deploy) any process may+/// adopt the run and resume the job recorded on it.+pub trait NotionRunRepo: Send + Sync + 'static {+ /// Take the user's Notion run for `owner` until `lease` from now and+ /// record `job` on it. Only a run that just started (`running` or+ /// `importing`, held by nobody) can be taken. Returns whether it was.+ fn take_run(+ &self,+ user: &MacroUserIdStr<'static>,+ owner: Uuid,+ lease: Duration,+ job: &NotionJob,+ ) -> impl Future<Output = Result<bool>> + Send;++ /// Extend `owner`'s lease on the user's Notion run to `lease` from now.+ /// Returns `false` once the run stopped or another process holds it.+ fn renew_lease(+ &self,+ user: &MacroUserIdStr<'static>,+ owner: Uuid,+ lease: Duration,+ ) -> impl Future<Output = Result<bool>> + Send;++ /// Give up `owner`'s lease, so another process adopts the run at once.+ /// Returns whether `owner` held it.+ fn release_lease(+ &self,+ user: &MacroUserIdStr<'static>,+ owner: Uuid,+ ) -> impl Future<Output = Result<bool>> + Send;++ /// Adopt up to `limit` Notion runs (only `user`'s, when given) still+ /// planning or writing whose lease lapsed, or which nobody leased and+ /// nobody touched for `stale_after` (runs started by code that predates+ /// leases). Each is held for `owner` until `lease` from now. Runs another+ /// process is adopting at the same moment are skipped.+ fn adopt_stalled_runs(+ &self,+ user: Option<&MacroUserIdStr<'static>>,+ owner: Uuid,+ lease: Duration,+ stale_after: Duration,+ limit: u16,+ ) -> impl Future<Output = Result<Vec<AdoptedRun>>> + Send;++ /// Move the user's Notion run to `to` with `error` when it is in one of+ /// `from`, dropping any lease on it; a run cancelled meanwhile stays+ /// `dismissed` with its own error. In the same transaction, produce the+ /// system event `event` makes of the moved run, so the session presenting+ /// its review hears of the change exactly when it happens. Returns the+ /// moved run, or `None` when it was in none of `from`.+ fn transition_reviewed_run<Event>(+ &self,+ user: &MacroUserIdStr<'static>,+ from: &[RunStatus],+ to: RunStatus,+ error: Option<&str>,+ event: Event,+ ) -> impl Future<Output = Result<Option<ReviewedRun>>> + Send+ where+ Event: FnOnce(&ReviewedRun) -> Option<PendingNotification> + Send;++ /// The user's "Notion" folder, once an import has made one.+ fn root_folder(+ &self,+ user: &MacroUserIdStr<'static>,+ ) -> impl Future<Output = Result<Option<Uuid>>> + Send;++ /// Record `folder` as the user's "Notion" folder.+ fn set_root_folder(+ &self,+ user: &MacroUserIdStr<'static>,+ folder: Uuid,+ ) -> impl Future<Output = Result<()>> + Send;++ /// Send the user's workspace plan rows still `importing` back to staged+ /// with `reason`, unless a write is running for them or they wait on a+ /// teammate's row still being written. Rows waiting on a teammate's row+ /// that stopped go back too. Returns how many moved.+ fn settle_importing(+ &self,+ user: &MacroUserIdStr<'static>,+ reason: &str,+ ) -> impl Future<Output = Result<u64>> + Send;+}++/// What workspace writes created for each ledger row, recorded as they go so+/// a resumed write reuses it instead of creating it again.+pub trait WrittenObjects: Send + Sync + 'static {+ /// Everything recorded for the user's ledger rows `import_ids`.+ fn written(+ &self,+ user: &MacroUserIdStr<'static>,+ import_ids: &[Uuid],+ ) -> impl Future<Output = Result<Vec<WrittenObject>>> + Send;++ /// Record `objects` for the user's ledger rows. An object already+ /// recorded under the same row, kind and Notion object keeps its earlier+ /// id. Returns what is recorded for each of `objects` afterwards.+ fn record_written(+ &self,+ user: &MacroUserIdStr<'static>,+ objects: &[WrittenObject],+ ) -> impl Future<Output = Result<Vec<WrittenObject>>> + Send;+}diff --git a/crates/import/src/domain/service.rs b/crates/import/src/domain/service.rsindex 5024851e20..6ca3134a7e 100644--- a/crates/import/src/domain/service.rs+++ b/crates/import/src/domain/service.rs@@ -1,13 +1,11 @@-//! The import orchestrator: staging (with team-wide dedup), gather jobs,+//! The import orchestrator: staging (with team-wide dedup), Slack discovery,//! and import jobs.//!-//! Slack discovery uses the typed workspace source port to stage public-//! channels and enrich their membership. Manual discovery never uses an agent-//! or auto-imports; onboarding may fall back to an agent session. Other gather-//! jobs use connector tools with `CreateImportEntity` locked to-//! `(user, source, initiator)`. Import jobs copy-//! accepted rows in: Linear tasks and Slack channels are composed-//! deterministically from staged metadata; Notion pages are fetched+//! Slack discovery uses the typed workspace source port to stage every+//! visible public channel for the user to pick from; it never uses an agent+//! or imports on its own. Linear and Notion rows are staged by chat tools.+//! Import jobs copy accepted rows in: Linear tasks and Slack channels are+//! composed deterministically from staged metadata; Notion pages are fetched//! directly through the connector's fetch tool (no model in the content//! path), falling back to a bounded single-page Haiku session that lands//! content through `FinalizeImport` when the direct path can't cope.@@ -15,16 +13,15 @@use super::models::*;use super::ports::{CanonicalImportRepo, EntityCreator, ImportError, ImportRepo, ImportedDocumentProperties,- ImportedDocumentProperty, ImportedDocumentPropertyValue, ImportedTaskProperties, Result,- SlackSourceError, SlackWorkspaceSource,-};-use crate::inbound::toolset::{- ImportToolContext, ToolPolicy, gather_toolset, notion_import_toolset,+ ImportedDocumentProperty, ImportedDocumentPropertyValue, ImportedTaskProperties, NotionLinks,+ Result, SlackSourceError, SlackWorkspaceSource,};+use crate::inbound::toolset::{ImportToolContext, notion_import_toolset};use agent::types::{ChatMessage, ChatMessageContent, Role};use agent::{AgentLoop, PredefinedModel};use ai_toolset::{RequestContext, ToolResult, ToolSet, ToolSetError};use futures::StreamExt;+use import_notion::domain::ports::NotionConnections;use macro_user_id::user_id::MacroUserIdStr;use mcp_select::{ConnectorSelect, UserMcpTools};use std::collections::HashSet;@@ -35,33 +32,18 @@ use std::time::Duration;use uuid::Uuid;mod admission;+mod notion_workspace;mod prompts;mod slack;mod slack_discovery;+pub use notion_workspace::NoNotionConnections;pub use slack_discovery::NoSlackSource;use slack_discovery::SlackBatch;#[cfg(test)]mod test;-/// Model for gather sessions: fast beats maximal while the user watches the-/// section shimmer, and Cerebras serves gpt-oss-120b at interactive latency.-/// A raw provider-prefixed id (not [`PredefinedModel`]) because the registry-/// has no Cerebras tier — the model router resolves the `cerebras/` prefix.-const GATHER_MODEL: &str = "cerebras/gpt-oss-120b";-/// Fallback when the primary gather session fails: Cerebras enforces tight-/// org-wide rate limits, so a burst of concurrent onboardings can 429 every-/// gather at once. gpt-5.4-nano is slower per token but rides OpenAI's much-/// higher limits, and a slower gather beats a failed section.-const GATHER_FALLBACK_MODEL: &str = "openai/gpt-5.4-nano";-/// Turn cap for gather sessions: a couple of searches plus one staging tool-/// call per candidate (providers may batch several per turn).-const GATHER_MAX_TURNS: usize = 24;-/// Hard cap on a gather session. Staged rows land incrementally, so the-/// section fills while this runs; the cap only bounds the shimmer tail.-const GATHER_TIMEOUT: Duration = Duration::from_secs(90);-/// Model for Notion import sessions (normal ai_usage attribution).const NOTION_IMPORT_MODEL: PredefinedModel = PredefinedModel::Fast;/// Hard cap on importing ONE Notion page (fetch + convert + finalize).@@ -73,22 +55,13 @@ const NOTION_PAGE_IMPORT_TIMEOUT: Duration = Duration::from_secs(120);/// enough to stay polite to Notion's MCP.const NOTION_IMPORT_CONCURRENCY: usize = 4;-/// Discovery policy: bounded onboarding suggestions or a user-driven listing.-#[derive(Debug, Clone, Copy, PartialEq, Eq)]-pub enum GatherMode {- /// Suggest the strongest active channels for onboarding.- Onboarding,- /// Stage all visible public channels for explicit selection.- Manual,-}+/// Hard cap on one manual Slack discovery: a full channel listing plus+/// membership reads for every channel.+const SLACK_DISCOVERY_TIMEOUT: Duration = Duration::from_secs(360);-fn gather_timeout(source: ImportSource, mode: GatherMode) -> Duration {- match (source, mode) {- (ImportSource::Slack, GatherMode::Onboarding) => Duration::from_secs(180),- (ImportSource::Slack, GatherMode::Manual) => Duration::from_secs(360),- _ => GATHER_TIMEOUT,- }-}+/// How long a discovery run may stay `running` before the read path declares+/// its process dead. Twice the discovery timeout, so a live run is never reaped.+const STALE_RUN_AFTER: Duration = Duration::from_secs(SLACK_DISCOVERY_TIMEOUT.as_secs() * 2);/// How often a running import batch touches its rows' `updated_at`. The/// heartbeat covers every row the batch owns — queued AND in-flight — so a@@ -162,7 +135,7 @@ pub enum DiscardOutcome {NotDiscardable(ImportStatus),}-/// The API the import router (and host services like onboarding) talk to.+/// The API the import router talks to.pub trait ImportService: Send + Sync + 'static {/// The full import aggregate for the user.fn state(@@ -170,40 +143,14 @@ pub trait ImportService: Send + Sync + 'static {user: MacroUserIdStr<'static>,) -> impl Future<Output = Result<ImportState>> + Send;- /// Start a gather run for `source` if none has ever run (the- /// connector-just-authenticated hook). `auto_import` is persisted with- /// the new run before its background gather starts. Returns whether a- /// run started.- fn start_gather(- &self,- user: MacroUserIdStr<'static>,- source: ImportSource,- auto_import: bool,- ) -> impl Future<Output = Result<bool>> + Send;-- /// User-triggered, non-auto-import gather that stages candidates for- /// explicit selection. Returns whether a run started.+ /// User-triggered discovery that stages everything visible. Only Slack is+ /// supported. Returns whether a run started.fn start_discovery(&self,user: MacroUserIdStr<'static>,source: ImportSource,) -> impl Future<Output = Result<bool>> + Send;- /// Restart a failed (or dismissed) gather run. Returns whether a run- /// started.- fn retry_gather(- &self,- user: MacroUserIdStr<'static>,- source: ImportSource,- ) -> impl Future<Output = Result<bool>> + Send;-- /// Dismiss a source's import section.- fn dismiss_run(- &self,- user: MacroUserIdStr<'static>,- source: ImportSource,- ) -> impl Future<Output = Result<()>> + Send;-/// Accept `import_ids` (flip to `importing` and start import jobs) and/// discard `discard_ids`.fn run_import(@@ -212,26 +159,6 @@ pub trait ImportService: Send + Sync + 'static {import_ids: Vec<Uuid>,discard_ids: Vec<Uuid>,) -> impl Future<Output = Result<RunImportOutcome>> + Send;-- /// Discard all remaining staged rows with `initiator` — an explicit- /// decline; `stage()` refuses to re-stage discarded items. Returns how- /// many were discarded.- fn discard_staged_by_initiator(- &self,- user: MacroUserIdStr<'static>,- initiator: Initiator,- ) -> impl Future<Output = Result<u64>> + Send;-- /// Delete remaining unreserved staged rows with `initiator` (onboarding- /// completion cleanup). Candidates reserved by active or retryable- /// configured auto-import runs survive. Unlike discarding, deleted- /// candidates were never reviewed and stay re-stageable by later gathers- /// or chat. Returns how many were removed.- fn delete_staged_by_initiator(- &self,- user: MacroUserIdStr<'static>,- initiator: Initiator,- ) -> impl Future<Output = Result<u64>> + Send;}/// Staging operations the AI tools drive. Split from [`ImportService`] so@@ -313,7 +240,7 @@ pub enum ImportNotionPageOutcome {/// Workflow used by the interactive agent to import one specific Notion page.////// Unlike [`ImportFinalizer`], this owns the complete operation: deduplication,-/// ledger transitions, connector fetch, content normalization, document+/// ledger transitions, connector fetch, content checks, document/// creation, and final ledger state.pub trait NotionPageImporter: Send + Sync + 'static {/// Import a Notion page URL or page id for `user`.@@ -324,28 +251,104 @@ pub trait NotionPageImporter: Send + Sync + 'static {) -> impl Future<Output = Result<ImportNotionPageOutcome>> + Send;}+/// Importing a whole Notion workspace (or part of it) through a reviewed plan.+///+/// Planning reads the workspace and asks the decision model what to do with+/// each object; the plan is staged as ledger rows and presented for review.+/// Approving the review writes the plan; nothing is written before that.+pub trait NotionWorkspaceImporter: Send + Sync + 'static {+ /// Plan an import of `scope`, replacing any previous plan. Planning runs+ /// in the background; this waits up to `wait` for it and reports where it+ /// got. `sharing` is required of a user on a team+ /// ([`ImportError::NotionSharingRequired`]); without a team everything+ /// is personal.+ fn prepare_notion_import(+ &self,+ user: &MacroUserIdStr<'static>,+ scope: import_notion::domain::model::ImportScope,+ sharing: Option<NotionSharing>,+ wait: Duration,+ ) -> impl Future<Output = Result<NotionImportReport>> + Send;++ /// Where the user's Notion import stands.+ fn notion_import_report(+ &self,+ user: &MacroUserIdStr<'static>,+ ) -> impl Future<Output = Result<NotionImportReport>> + Send;++ /// Tie the staged plan to the review presenting it in agent session+ /// `session_id`, which then hears when the plan is ready and when its+ /// write ends.+ fn attach_notion_review(+ &self,+ user: &MacroUserIdStr<'static>,+ review_id: Uuid,+ session_id: agent_session::domain::model::AgentSessionId,+ ) -> impl Future<Output = Result<u64>> + Send;++ /// Write the plan tied to `review_id` with the owner's `choices`.+ /// Writing runs in the background; this returns once it has started.+ fn start_notion_write(+ &self,+ user: &MacroUserIdStr<'static>,+ review_id: Uuid,+ choices: NotionWriteChoices,+ ) -> impl Future<Output = Result<NotionImportReport>> + Send;++ /// Stop planning or writing. Objects already written stay. Returns+ /// whether anything was running.+ fn cancel_notion_import(+ &self,+ user: &MacroUserIdStr<'static>,+ ) -> impl Future<Output = Result<bool>> + Send;+}+/// Concrete orchestrator wiring the repo, the user's MCP servers, the/// entity creator, and usage recording together.-pub struct ImportServiceImpl<R, S, C, W = NoSlackSource> {+pub struct ImportServiceImpl<R, S, C, W = NoSlackSource, N = NoNotionConnections> {repo: R,+ notion: Arc<N>,slack_source: Arc<W>,mcp_tools: Arc<S>,creator: Arc<C>,recorder: Arc<dyn ai_usage::UsageRecorder>,admission: Arc<dyn ai_billing::AiAdmissionService>,notifier: Option<ImportNotify>,+ jobs: NotionJobs,}-impl<R: Clone, S, C, W: SlackWorkspaceSource> Clone for ImportServiceImpl<R, S, C, W> {+/// Where this process runs Notion workspace imports: its identity on the+/// runs it holds, the shutdown that makes them hand over, and the tracker the+/// host waits on before exiting.+#[derive(Clone)]+struct NotionJobs {+ instance: Uuid,+ shutdown: tokio_util::sync::CancellationToken,+ tracker: tokio_util::task::TaskTracker,+}++impl NotionJobs {+ fn new() -> Self {+ Self {+ instance: Uuid::now_v7(),+ shutdown: tokio_util::sync::CancellationToken::new(),+ tracker: tokio_util::task::TaskTracker::new(),+ }+ }+}++impl<R: Clone, S, C, W: SlackWorkspaceSource, N> Clone for ImportServiceImpl<R, S, C, W, N> {fn clone(&self) -> Self {Self {repo: self.repo.clone(),+ notion: self.notion.clone(),slack_source: self.slack_source.clone(),mcp_tools: self.mcp_tools.clone(),creator: self.creator.clone(),recorder: self.recorder.clone(),admission: self.admission.clone(),notifier: self.notifier.clone(),+ jobs: self.jobs.clone(),}}}@@ -360,29 +363,33 @@ impl<R, S, C> ImportServiceImpl<R, S, C> {) -> Self {Self {repo,+ notion: Arc::new(NoNotionConnections),slack_source: Arc::new(NoSlackSource),mcp_tools,creator,recorder,admission: Arc::new(ai_billing::DisabledAiAdmissionService),notifier: None,+ jobs: NotionJobs::new(),}}}-impl<R, S, C, W: SlackWorkspaceSource> ImportServiceImpl<R, S, C, W> {+impl<R, S, C, W: SlackWorkspaceSource, N: Send + Sync + 'static> ImportServiceImpl<R, S, C, W, N> {/// Attach a live Slack workspace reader, preserving the service configuration.pub fn with_slack_source<W2: SlackWorkspaceSource>(self,source: Arc<W2>,- ) -> ImportServiceImpl<R, S, C, W2> {+ ) -> ImportServiceImpl<R, S, C, W2, N> {ImportServiceImpl {repo: self.repo,+ notion: self.notion,mcp_tools: self.mcp_tools,creator: self.creator,recorder: self.recorder,admission: self.admission,notifier: self.notifier,+ jobs: self.jobs,slack_source: source,}}@@ -393,6 +400,39 @@ impl<R, S, C, W: SlackWorkspaceSource> ImportServiceImpl<R, S, C, W> {self}+ /// Run Notion workspace imports on `tracker`, handing them over to+ /// another process once `shutdown` is cancelled: a job stops at its next+ /// checkpoint and gives up its run, which the next process adopts. The+ /// host waits on `tracker` before exiting.+ pub fn with_job_shutdown(+ mut self,+ shutdown: tokio_util::sync::CancellationToken,+ tracker: tokio_util::task::TaskTracker,+ ) -> Self {+ self.jobs.shutdown = shutdown;+ self.jobs.tracker = tracker;+ self+ }++ /// Read users' Notion workspaces through `connections` for workspace+ /// imports.+ pub fn with_notion_connections<N2: NotionConnections>(+ self,+ connections: Arc<N2>,+ ) -> ImportServiceImpl<R, S, C, W, N2> {+ ImportServiceImpl {+ repo: self.repo,+ notion: connections,+ mcp_tools: self.mcp_tools,+ creator: self.creator,+ recorder: self.recorder,+ admission: self.admission,+ notifier: self.notifier,+ jobs: self.jobs,+ slack_source: self.slack_source,+ }+ }+async fn notify(&self, user: &MacroUserIdStr<'static>) {if let Some(notifier) = &self.notifier {notifier(user.clone()).await;@@ -400,194 +440,66 @@ impl<R, S, C, W: SlackWorkspaceSource> ImportServiceImpl<R, S, C, W> {}}-impl<R, S, C, W: SlackWorkspaceSource> ImportServiceImpl<R, S, C, W>+impl<R, S, C, W: SlackWorkspaceSource, N: Send + Sync + 'static> ImportServiceImpl<R, S, C, W, N>whereR: ImportRepo + CanonicalImportRepo + Clone,S: ConnectorSelect,C: EntityCreator,{- /// Spawn the gather session for one source; finishes the run row either- /// way and nudges the client.- fn spawn_gather(&self, user: MacroUserIdStr<'static>, source: ImportSource, mode: GatherMode) {+ /// Fail discovery runs that outlived their own timeout: their process died+ /// (crash or deploy) and the run would otherwise block a new discovery+ /// forever. Best-effort, so a read or a restart never fails because of it.+ async fn reap_stale_runs(&self, user: &MacroUserIdStr<'static>) {+ match self+ .repo+ .fail_stale_runs(user, STALE_RUN_AFTER.as_secs() as i64)+ .await+ {+ Ok(0) => {}+ Ok(reaped) => tracing::warn!(reaped, "reaped orphaned discovery runs"),+ Err(e) => tracing::warn!(error = ?e, "failed to reap stale discovery runs"),+ }+ }++ /// Spawn Slack discovery; finishes the run row either way and nudges the+ /// client.+ fn spawn_slack_discovery(&self, user: MacroUserIdStr<'static>) {let service = self.clone();tokio::spawn(async move {let outcome = tokio::time::timeout(- gather_timeout(source, mode),- service.run_gather_session(&user, source, mode),+ SLACK_DISCOVERY_TIMEOUT,+ slack_discovery::discover_slack(&service, &user),).await- .unwrap_or_else(|_| Err(anyhow::anyhow!("gather session timed out")));- let gather_succeeded = outcome.is_ok();-+ .unwrap_or_else(|_| Err(anyhow::anyhow!("Slack discovery timed out").into()));let finished = match outcome {- Ok(()) => {+ Ok(_) => {service.repo- .finish_run(&user, source, RunStatus::Ready, None)+ .finish_run(&user, ImportSource::Slack, RunStatus::Ready, None).await}Err(e) => {- tracing::warn!(source = source.as_ref(), error = ?e, "gather session failed");+ tracing::warn!(error = ?e, "Slack discovery failed");+ let reason = match e {+ SlackSourceError::ToolsUnavailable(_) => {+ "Your Slack connection does not expose channel tools".to_string()+ }+ e => e.to_string(),+ };service.repo- .finish_run(- &user,- source,- RunStatus::Failed,- Some(&admission::failure_reason(&e)),- )+ .finish_run(&user, ImportSource::Slack, RunStatus::Failed, Some(&reason)).await}};- match finished {- Ok(true) if gather_succeeded => {- let _ = service- .maybe_start_auto_import(&user, source)- .await- .inspect_err(|e| {- tracing::error!(source = source.as_ref(), error = ?e, "failed to start automatic import");- });- }- Ok(_) => {}- Err(e) => {- tracing::error!(source = source.as_ref(), error = ?e, "failed to persist gather outcome");- }- }+ let _ = finished.inspect_err(|e| {+ tracing::error!(error = ?e, "failed to persist Slack discovery outcome");+ });service.notify(&user).await;});}- /// Claim and spawn a configured automatic import once gathering is ready.- /// The repository CAS makes this safe when completion, configuration, and- /// read-path reconciliation race across service replicas.- async fn maybe_start_auto_import(- &self,- user: &MacroUserIdStr<'static>,- source: ImportSource,- ) -> Result<bool> {- let Some(rows) = self.repo.begin_auto_import(user, source).await? else {- return Ok(false);- };- let ids: Vec<Uuid> = rows.iter().map(|row| row.id).collect();- self.notify(user).await;- if rows.is_empty() {- self.finish_auto_import_batch(user, source, &ids).await;- } else {- self.spawn_import_batch(user.clone(), rows, Some(source));- }- Ok(true)- }-- async fn finish_auto_import_batch(- &self,- user: &MacroUserIdStr<'static>,- source: ImportSource,- ids: &[Uuid],- ) {- match self.repo.finish_auto_import(user, source, ids).await {- Ok(Some(status)) => {- tracing::info!(- source = source.as_ref(),- status = status.as_ref(),- "automatic import finished"- );- self.notify(user).await;- }- Ok(None) => {}- Err(e) => {- tracing::error!(source = source.as_ref(), error = ?e, "failed to persist automatic import outcome");- }- }- }-- /// Run typed Slack discovery or an agent gather with locked staging tools.- /// Candidates land incrementally; any final agent text is ignored.- #[tracing::instrument(skip(self, user), err)]- async fn run_gather_session(- &self,- user: &MacroUserIdStr<'static>,- source: ImportSource,- mode: GatherMode,- ) -> anyhow::Result<()> {- // Recheck queued AI work before loading tools. Slack stays deterministic- // until its onboarding fallback actually needs an agent.- self.admit_gather(user, source).await?;- self.gather_with_tools(user, source, mode).await- }-- async fn gather_with_tools(- &self,- user: &MacroUserIdStr<'static>,- source: ImportSource,- mode: GatherMode,- ) -> anyhow::Result<()> {- // Typed Slack discovery runs before loading agent tools. Only- // onboarding may fall back to an agent when workspace reads fail.- if source == ImportSource::Slack {- match slack_discovery::gather_slack(self, user, mode).await {- Ok(_) => return Ok(()),- Err(SlackSourceError::ToolsUnavailable(_)) if mode == GatherMode::Manual => {- anyhow::bail!("Your Slack connection does not expose channel tools");- }- Err(e) if mode == GatherMode::Manual => return Err(e.into()),- Err(e) => {- tracing::warn!(error = ?e, "direct slack gather failed; trying the agent");- }- }- }-- let mcp_tools = self.connector_tools(user, source).await?;-- // Staging is idempotent (the ledger dedups already-staged rows), so- // rerunning the whole session on the fallback model is safe even- // when the primary died mid-way through staging.- match self- .gather_agent_session(user, source, mode, GATHER_MODEL, mcp_tools.clone())- .await- {- Ok(()) => Ok(()),- Err(e) if e.is::<ai_billing::AiAdmissionError>() => Err(e),- Err(e) => {- tracing::warn!(model = GATHER_MODEL, error = ?e, "gather session failed; retrying on the fallback model");- self.gather_agent_session(user, source, mode, GATHER_FALLBACK_MODEL, mcp_tools)- .await- }- }- }-- /// One agent gather session on a specific model.- #[tracing::instrument(skip(self, user, mcp_tools), err)]- async fn gather_agent_session<M: ToolSet<ImportToolContext<Self>> + 'static>(- &self,- user: &MacroUserIdStr<'static>,- source: ImportSource,- mode: GatherMode,- model: &str,- mcp_tools: Arc<M>,- ) -> anyhow::Result<()> {- let native = gather_toolset::<Self>();- let toolset = NativePlusMcp::new(native, mcp_tools);- let initiator = match mode {- GatherMode::Onboarding => Initiator::Onboarding,- GatherMode::Manual => Initiator::Manual,- };- let context = ImportToolContext {- service: Some(Arc::new(self.clone())),- policy: ToolPolicy::gather(source, initiator),- };-- self.drive_session(- user,- model,- GATHER_MAX_TURNS,- toolset,- context,- &prompts::gather_system(source),- prompts::gather_prompt(source),- )- .await- }-/// Copy one accepted Notion page in WITHOUT a model: call the/// connector's fetch tool directly, take the markdown it returns, and/// finalize. The fallback Haiku session exists for pages this can't@@ -641,9 +553,7 @@ where.map(str::to_string)}).unwrap_or_else(|| "Untitled".to_string());- let markdown = prepare_notion_markdown(&fetched.body)?;-- self.finalize_document(user, row.id, &name, &markdown, &fetched.properties)+ self.finalize_document(user, row.id, &name, &fetched.body, &fetched.properties).await.map_err(|e| anyhow::anyhow!("finalize failed: {e}"))?;Ok(())@@ -660,10 +570,7 @@ where) -> anyhow::Result<()> {let native = notion_import_toolset::<Self>();let toolset = NativePlusMcp::new(native, mcp_tools);- let context = ImportToolContext {- service: Some(Arc::new(self.clone())),- policy: ToolPolicy::import_job(),- };+ let context = ImportToolContext::wired(Arc::new(self.clone()));self.drive_session(user,@@ -870,15 +777,8 @@ whereoutcome}- /// Run a claimed import batch in the background. Manual batches only- /// update their entity rows; automatic batches also settle their owning- /// run after every row reaches a terminal state.- fn spawn_import_batch(- &self,- user: MacroUserIdStr<'static>,- rows: Vec<ImportEntity>,- auto_run: Option<ImportSource>,- ) {+ /// Run a claimed import batch in the background.+ fn spawn_import_batch(&self, user: MacroUserIdStr<'static>, rows: Vec<ImportEntity>) {let service = self.clone();tokio::spawn(async move {let batch_ids: Vec<Uuid> = rows.iter().map(|row| row.id).collect();@@ -927,11 +827,6 @@ where.fail_unfinished(&user, &ids, &format!("notion unavailable: {e}")).await;service.notify(&user).await;- if let Some(source) = auto_run {- service- .finish_auto_import_batch(&user, source, &batch_ids)- .await;- }return;}};@@ -948,17 +843,12 @@ where.await;service.notify(&user).await;}-- if let Some(source) = auto_run {- service- .finish_auto_import_batch(&user, source, &batch_ids)- .await;- }});}}-impl<R, S, C, W: SlackWorkspaceSource> ImportService for ImportServiceImpl<R, S, C, W>+impl<R, S, C, W: SlackWorkspaceSource, N: Send + Sync + 'static> ImportService+ for ImportServiceImpl<R, S, C, W, N>whereR: ImportRepo + CanonicalImportRepo + Clone,S: ConnectorSelect,@@ -984,128 +874,36 @@ whereErr(e) => tracing::warn!(error = ?e, "failed to reap stale imports"),}- match self.repo.reconcile_auto_import_runs(&user).await {- Ok(0) => {}- Ok(reconciled) => {- tracing::warn!(reconciled, "reconciled interrupted automatic import runs");- self.notify(&user).await;- }- // A read must never fail because recovery did.- Err(e) => tracing::warn!(error = ?e, "failed to reconcile automatic import runs"),- }-- // Self-heal the small window between persisting gather completion and- // spawning its configured follow-on batch. The begin CAS makes this- // safe across concurrent readers and replicas.- let mut runs = self.repo.list_runs(&user).await?;- let auto_ready: Vec<ImportSource> = runs- .iter()- .filter(|run| run.auto_import && run.status == RunStatus::Ready)- .map(|run| run.source)- .collect();- let mut started = false;- for source in auto_ready {- match self.maybe_start_auto_import(&user, source).await {- Ok(did_start) => started |= did_start,- Err(e) => {- tracing::warn!(source = source.as_ref(), error = ?e, "failed to self-heal automatic import start");- }- }- }- if started {- runs = self.repo.list_runs(&user).await?;- }+ self.reap_stale_runs(&user).await;+ let runs = self.repo.list_runs(&user).await?;let entities = self.repo.list(&user, None, None).await?;Ok(ImportState { runs, entities })}- #[tracing::instrument(skip(self, user), err)]- async fn start_gather(- &self,- user: MacroUserIdStr<'static>,- source: ImportSource,- auto_import: bool,- ) -> Result<bool> {- if !self.prepare_gather(&user, source, &[]).await? {- return Ok(false);- }- // The CAS still decides the winner if another request raced admission.- let won = self.repo.start_run(&user, source, &[], auto_import).await?;- if won {- self.spawn_gather(user.clone(), source, GatherMode::Onboarding);- self.notify(&user).await;- }- Ok(won)- }-#[tracing::instrument(skip(self, user), err)]async fn start_discovery(&self,user: MacroUserIdStr<'static>,source: ImportSource,) -> Result<bool> {+ if source != ImportSource::Slack {+ return Err(ImportError::UnsupportedDiscovery(source));+ }let from = [RunStatus::Ready,RunStatus::Completed,RunStatus::Failed,RunStatus::Dismissed,];- if !self.prepare_gather(&user, source, &from).await? {- return Ok(false);- }- let won = self.repo.start_manual_run(&user, source, &from).await?;- if won {- self.spawn_gather(user.clone(), source, GatherMode::Manual);- self.notify(&user).await;- }- Ok(won)- }-- #[tracing::instrument(skip(self, user), err)]- async fn retry_gather(- &self,- user: MacroUserIdStr<'static>,- source: ImportSource,- ) -> Result<bool> {- if !self- .prepare_gather(&user, source, &[RunStatus::Failed, RunStatus::Dismissed])- .await?- {- return Ok(false);- }- let won = self- .repo- .start_run(- &user,- source,- &[RunStatus::Failed, RunStatus::Dismissed],- false,- )- .await?;+ self.reap_stale_runs(&user).await;+ let won = self.repo.start_run(&user, source, &from).await?;if won {- self.spawn_gather(user.clone(), source, GatherMode::Onboarding);+ self.spawn_slack_discovery(user.clone());self.notify(&user).await;}Ok(won)}- #[tracing::instrument(skip(self, user), err)]- async fn dismiss_run(&self, user: MacroUserIdStr<'static>, source: ImportSource) -> Result<()> {- let dismissed = self- .repo- .transition_run(- &user,- source,- &[RunStatus::Ready, RunStatus::Failed],- RunStatus::Dismissed,- )- .await?;- if dismissed {- self.notify(&user).await;- }- Ok(())- }-#[tracing::instrument(skip(self, user, import_ids, discard_ids), fields(imports = import_ids.len(), discards = discard_ids.len()), err)]async fn run_import(&self,@@ -1128,7 +926,7 @@ whereself.notify(&user).await;if !rows.is_empty() {- self.spawn_import_batch(user.clone(), rows, None);+ self.spawn_import_batch(user.clone(), rows);}Ok(RunImportOutcome {@@ -1136,41 +934,9 @@ whereimporting,})}-- #[tracing::instrument(skip(self, user), err)]- async fn discard_staged_by_initiator(- &self,- user: MacroUserIdStr<'static>,- initiator: Initiator,- ) -> Result<u64> {- let discarded = self- .repo- .discard_staged_by_initiator(&user, initiator)- .await?;- if discarded > 0 {- self.notify(&user).await;- }- Ok(discarded)- }-- #[tracing::instrument(skip(self, user), err)]- async fn delete_staged_by_initiator(- &self,- user: MacroUserIdStr<'static>,- initiator: Initiator,- ) -> Result<u64> {- let removed = self- .repo- .delete_staged_by_initiator(&user, initiator)- .await?;- if removed > 0 {- self.notify(&user).await;- }- Ok(removed)- }}-impl<R, S, C, W: SlackWorkspaceSource> ImportServiceImpl<R, S, C, W>+impl<R, S, C, W: SlackWorkspaceSource, N: Send + Sync + 'static> ImportServiceImpl<R, S, C, W, N>whereR: ImportRepo + CanonicalImportRepo + Clone,S: ConnectorSelect,@@ -1221,7 +987,14 @@ wherematch self.repo- .upsert_staged(user, source, initiator, &foreign_id, &metadata)+ .upsert_staged(+ user,+ source,+ source.default_kind(),+ initiator,+ &foreign_id,+ &metadata,+ ).await?{Some(row) => {@@ -1253,7 +1026,8 @@ where}}-impl<R, S, C, W: SlackWorkspaceSource> ImportStager for ImportServiceImpl<R, S, C, W>+impl<R, S, C, W: SlackWorkspaceSource, N: Send + Sync + 'static> ImportStager+ for ImportServiceImpl<R, S, C, W, N>whereR: ImportRepo + CanonicalImportRepo + Clone,S: ConnectorSelect,@@ -1297,6 +1071,7 @@ where.upsert_imported(user,source,+ source.default_kind(),initiator,&foreign_id,&metadata,@@ -1335,7 +1110,8 @@ where}}-impl<R, S, C, W: SlackWorkspaceSource> NotionPageImporter for ImportServiceImpl<R, S, C, W>+impl<R, S, C, W: SlackWorkspaceSource, N: Send + Sync + 'static> NotionPageImporter+ for ImportServiceImpl<R, S, C, W, N>whereR: ImportRepo + CanonicalImportRepo + Clone,S: ConnectorSelect,@@ -1470,7 +1246,8 @@ where}}-impl<R, S, C, W: SlackWorkspaceSource> ImportFinalizer for ImportServiceImpl<R, S, C, W>+impl<R, S, C, W: SlackWorkspaceSource, N: Send + Sync + 'static> ImportFinalizer+ for ImportServiceImpl<R, S, C, W, N>whereR: ImportRepo + CanonicalImportRepo + Clone,S: ConnectorSelect,@@ -1509,10 +1286,17 @@ where.await}ImportSource::Notion => {- let content_markdown =- prepare_notion_markdown(content_markdown).map_err(ImportError::Other)?;+ check_notion_markdown(content_markdown).map_err(ImportError::Other)?;+ // A single page knows nothing else of the import, so its+ // references link back to Notion.self.creator- .create_markdown_doc(user, name, &content_markdown, properties)+ .create_notion_document(+ user,+ name,+ content_markdown,+ &NotionLinks::default(),+ properties,+ ).await}ImportSource::Slack => {@@ -1874,48 +1658,6 @@ fn dedupe_strings(values: &mut Vec<String>) {values.retain(|value| seen.insert(value.to_ascii_lowercase()));}-static PAIRED_NOTION_PAGE_REFS: LazyLock<Vec<regex::Regex>> = LazyLock::new(|| {- ["page", "mention-page"]- .into_iter()- .map(|tag| {- regex::Regex::new(&format!(- r#"(?s)<{tag}\b(?P<attrs>[^>]*)>(?P<label>.*?)</{tag}>"#- ))- .expect("valid notion page reference regex")- })- .collect()-});-static SELF_CLOSING_NOTION_PAGE_REF: LazyLock<regex::Regex> = LazyLock::new(|| {- regex::Regex::new(r#"<(?:ancestor-\d+-)?page\b(?P<attrs>[^>]*)/?>"#)- .expect("valid self-closing notion page reference regex")-});-static PAIRED_NOTION_TEXT_MENTIONS: LazyLock<Vec<regex::Regex>> = LazyLock::new(|| {- ["mention-user", "mention-agent"]- .into_iter()- .map(|tag| {- regex::Regex::new(&format!(r#"(?s)<{tag}\b[^>]*>(?P<label>.*?)</{tag}>"#))- .expect("valid notion text mention regex")- })- .collect()-});-static NOTION_DATE_MENTION: LazyLock<regex::Regex> = LazyLock::new(|| {- regex::Regex::new(r#"<mention-date\b(?P<attrs>[^>]*)/?>"#)- .expect("valid notion date mention regex")-});-static PAIRED_NOTION_MEDIA: LazyLock<Vec<regex::Regex>> = LazyLock::new(|| {- ["audio", "video", "file", "pdf"]- .into_iter()- .map(|tag| {- regex::Regex::new(&format!(- r#"(?s)<{tag}\b(?P<attrs>[^>]*)>(?P<label>.*?)</{tag}>"#- ))- .expect("valid notion media regex")- })- .collect()-});-static NOTION_UNKNOWN_BLOCK: LazyLock<regex::Regex> = LazyLock::new(|| {- regex::Regex::new(r#"<unknown\b(?P<attrs>[^>]*)/?>"#).expect("valid notion unknown-block regex")-});static PAIRED_NOTION_DATABASES: LazyLock<Vec<regex::Regex>> = LazyLock::new(|| {["database", "mention-database", "mention-data-source"].into_iter()@@ -1929,28 +1671,6 @@ static SELF_CLOSING_NOTION_DATABASE: LazyLock<regex::Regex> = LazyLock::new(|| {regex::Regex::new(r#"(?s)<(?:database|mention-database|mention-data-source)\b[^>]*/?>"#).expect("valid self-closing notion database regex")});-static NOTION_TOGGLE_MARKER: LazyLock<regex::Regex> = LazyLock::new(|| {- regex::Regex::new(r#"[ \t]*\{toggle\s*=\s*"true"\}"#).expect("valid notion toggle marker regex")-});-static NOTION_BLOCK_ATTRIBUTES: LazyLock<regex::Regex> = LazyLock::new(|| {- regex::Regex::new(r#"[ \t]*\{(?:(?:toggle|color)\s*=\s*"[^"]*"[ \t]*)+\}[ \t]*$"#)- .expect("valid notion block-attribute regex")-});-static PAIRED_NOTION_SPAN: LazyLock<regex::Regex> = LazyLock::new(|| {- regex::Regex::new(r#"(?s)<span\b[^>]*>(?P<label>.*?)</span>"#).expect("valid notion span regex")-});-static NOTION_SUMMARY: LazyLock<regex::Regex> = LazyLock::new(|| {- regex::Regex::new(r#"(?s)<summary>(?P<label>.*?)</summary>"#)- .expect("valid notion summary regex")-});-static NOTION_EMPTY_BLOCK: LazyLock<regex::Regex> = LazyLock::new(|| {- regex::Regex::new(r#"(?m)^[\t ]*<(?:empty-block|table_of_contents)\b[^>]*/?>[\t ]*$"#)- .expect("valid empty Notion block regex")-});-static ESCAPED_NOTION_TODO: LazyLock<regex::Regex> = LazyLock::new(|| {- regex::Regex::new(r#"^(?P<indent>[\t ]*)(?:-\s+)?\\\[(?P<state>[ xX])\\\](?P<rest>.*)$"#)- .expect("valid escaped Notion todo regex")-});static NOTION_TOOL_RESULT_PREAMBLE: LazyLock<regex::Regex> = LazyLock::new(|| {regex::Regex::new(r#"(?im)^\s*Here is the result of "(?:view|fetch)"(?:\s|:)"#).expect("valid notion tool-result preamble regex")@@ -1959,120 +1679,24 @@ static NOTION_SERIALIZED_TITLE_WRAPPER: LazyLock<regex::Regex> = LazyLock::new(|regex::Regex::new(r#"(?m)^\s*(?:\[\s*)?\{\s*"[^"]+"\s*:"#).expect("valid notion serialized title wrapper regex")});-static UNHANDLED_NOTION_MARKUP: LazyLock<regex::Regex> = LazyLock::new(|| {- regex::Regex::new(- r#"(?i)</?(?:page|database|mention-[\w-]+|ancestor-\d+-page|details|summary|callout|columns?|synced_block(?:_reference)?|table|tr|td|th|colgroup|col|properties|content)\b"#,- )- .expect("valid unsupported Notion markup regex")-});-static NOTION_ATTR_RE: LazyLock<regex::Regex> = LazyLock::new(|| {- regex::Regex::new(r#"(?P<name>[\w-]+)="(?P<value>[^"]*)""#)- .expect("valid notion attribute regex")-});static INLINE_TAG_RE: LazyLock<regex::Regex> =LazyLock::new(|| regex::Regex::new(r"(?s)<[^>]+>").expect("valid inline tag regex"));-static BREAK_TAG_RE: LazyLock<regex::Regex> =- LazyLock::new(|| regex::Regex::new(r"(?i)<br\s*/?>").expect("valid HTML break regex"));-static TABLE_ROW_RE: LazyLock<regex::Regex> = LazyLock::new(|| {- regex::Regex::new(r"(?s)<tr\b[^>]*>(?P<body>.*?)</tr>").expect("valid notion table row regex")-});-static TABLE_CELL_RE: LazyLock<regex::Regex> = LazyLock::new(|| {- regex::Regex::new(r"(?s)<t[dh]\b[^>]*>(?P<body>.*?)</t[dh]>")- .expect("valid notion table cell regex")-});--/// Convert Notion's enhanced-markdown extensions into the markdown dialect-/// consumed by Macro's Lexical service. In particular, pipe tables become-/// native Lexical tables and Notion page references remain clickable external-/// links even when the referenced page was not part of the import.-fn normalize_notion_markdown(input: &str) -> String {- transform_outside_fenced_code(input, normalize_notion_markdown_fragment)-}-fn normalize_notion_markdown_fragment(input: &str) -> String {- let mut markdown = remove_notion_databases_fragment(input);- for regex in PAIRED_NOTION_PAGE_REFS.iter() {- markdown = regex- .replace_all(&markdown, |captures: ®ex::Captures<'_>| {- notion_page_link(- &captures["attrs"],- Some(INLINE_TAG_RE.replace_all(&captures["label"], "")),- )- })- .into_owned();- }- markdown = SELF_CLOSING_NOTION_PAGE_REF- .replace_all(&markdown, |captures: ®ex::Captures<'_>| {- notion_page_link(&captures["attrs"], None)- })- .into_owned();- for regex in PAIRED_NOTION_TEXT_MENTIONS.iter() {- markdown = regex- .replace_all(&markdown, |captures: ®ex::Captures<'_>| {- let label = INLINE_TAG_RE.replace_all(&captures["label"], "");- let label = label.trim();- if label.is_empty() {- String::new()- } else {- format!("@{label}")- }- })- .into_owned();- }- markdown = NOTION_DATE_MENTION- .replace_all(&markdown, |captures: ®ex::Captures<'_>| {- notion_date_mention(&captures["attrs"])- })- .into_owned();- for regex in PAIRED_NOTION_MEDIA.iter() {- markdown = regex- .replace_all(&markdown, |captures: ®ex::Captures<'_>| {- notion_media_link(&captures["attrs"], &captures["label"])- })- .into_owned();- }- markdown = NOTION_UNKNOWN_BLOCK- .replace_all(&markdown, |captures: ®ex::Captures<'_>| {- notion_unknown_block(&captures["attrs"])- })- .into_owned();- markdown = PAIRED_NOTION_SPAN- .replace_all(&markdown, |captures: ®ex::Captures<'_>| {- captures["label"].to_string()- })- .into_owned();- markdown = NOTION_SUMMARY- .replace_all(&markdown, |captures: ®ex::Captures<'_>| {- captures["label"].to_string()- })- .into_owned();- markdown = markdown- .replace("<ancestor-path>\n", "")- .replace("\n</ancestor-path>", "")- .replace("<ancestor-path>", "")- .replace("</ancestor-path>", "");- markdown = NOTION_TOGGLE_MARKER.replace_all(&markdown, "").into_owned();- markdown = NOTION_BLOCK_ATTRIBUTES- .replace_all(&markdown, "")- .into_owned();- markdown = NOTION_EMPTY_BLOCK.replace_all(&markdown, "").into_owned();- markdown = convert_notion_tables(&markdown);- markdown = flatten_notion_containers(&markdown);- markdown = normalize_escaped_notion_todos(&markdown);- let input_trailing_newlines = input- .chars()- .rev()- .take_while(|value| *value == '\n')- .count();- let output_trailing_newlines = markdown- .chars()- .rev()- .take_while(|value| *value == '\n')- .count();- for _ in output_trailing_newlines..input_trailing_newlines {- markdown.push('\n');- }- markdown+/// Whether a fetched Notion page is worth importing as a document: real body+/// content, not a connector's tool-result wrapper or a page that is mostly a+/// database. Converting it is lexical-service's job.+fn check_notion_markdown(input: &str) -> anyhow::Result<()> {+ anyhow::ensure!(!input.trim().is_empty(), "fetched page has no content");+ anyhow::ensure!(+ !NOTION_TOOL_RESULT_PREAMBLE.is_match(input)+ && !NOTION_SERIALIZED_TITLE_WRAPPER.is_match(input),+ "fetched page contained tool-result metadata instead of body content"+ );+ anyhow::ensure!(+ !notion_page_is_mostly_database(input),+ "fetched page is primarily a Notion database"+ );+ Ok(())}fn remove_notion_databases(input: &str) -> String {@@ -2089,30 +1713,6 @@ fn remove_notion_databases_fragment(input: &str) -> String {.into_owned()}-fn prepare_notion_markdown(input: &str) -> anyhow::Result<String> {- anyhow::ensure!(!input.trim().is_empty(), "fetched page has no content");- anyhow::ensure!(- !NOTION_TOOL_RESULT_PREAMBLE.is_match(input)- && !NOTION_SERIALIZED_TITLE_WRAPPER.is_match(input),- "fetched page contained tool-result metadata instead of body content"- );- anyhow::ensure!(- !notion_page_is_mostly_database(input),- "fetched page is primarily a Notion database"- );-- let markdown = normalize_notion_markdown(input);- anyhow::ensure!(- !markdown.trim().is_empty(),- "fetched page has no supported body content"- );- anyhow::ensure!(- !contains_outside_fenced_code(&markdown, &UNHANDLED_NOTION_MARKUP),- "fetched page contained unsupported Notion markup after normalization"- );- Ok(markdown)-}-fn notion_page_is_mostly_database(input: &str) -> bool {let without_databases = remove_notion_databases(input);if without_databases == input {@@ -2130,228 +1730,6 @@ fn notion_page_is_mostly_database(input: &str) -> bool {substantive_chars < 200}-fn notion_page_link(attrs: &str, body_label: Option<std::borrow::Cow<'_, str>>) -> String {- let mut url = None;- let mut title = None;- for captures in NOTION_ATTR_RE.captures_iter(attrs) {- match &captures["name"] {- "url" => url = Some(captures["value"].to_string()),- "title" => title = Some(captures["value"].to_string()),- _ => {}- }- }- let Some(url) = url else {- return body_label.unwrap_or_default().into_owned();- };- let label = body_label- .as_deref()- .map(str::trim)- .filter(|label| !label.is_empty())- .or_else(|| {- title- .as_deref()- .map(str::trim)- .filter(|label| !label.is_empty())- })- .unwrap_or("Notion page");- format!("[{}]({url})", escape_markdown_link_label(label))-}--fn notion_media_link(attrs: &str, body_label: &str) -> String {- let attributes = notion_attributes(attrs);- let Some(url) = attributes- .get("src")- .or_else(|| attributes.get("url"))- .filter(|url| is_web_url(url))- else {- return INLINE_TAG_RE.replace_all(body_label, "").trim().to_string();- };- let label = INLINE_TAG_RE.replace_all(body_label, "");- let label = label.trim();- let label = if label.is_empty() {- "Notion file"- } else {- label- };- format!("[{}]({url})", escape_markdown_link_label(label))-}--fn notion_unknown_block(attrs: &str) -> String {- let attributes = notion_attributes(attrs);- let Some(url) = attributes.get("url").filter(|url| is_web_url(url)) else {- return String::new();- };- let label = attributes- .get("alt")- .map(String::as_str)- .map(str::trim)- .filter(|label| !label.is_empty())- .unwrap_or("Unsupported Notion content");- format!("[{}]({url})", escape_markdown_link_label(label))-}--fn notion_date_mention(attrs: &str) -> String {- let attributes = notion_attributes(attrs);- let Some(start) = attributes.get("start") else {- return String::new();- };- let mut label = humanize_notion_date(start);- if let Some(time) = attributes.get("startTime").filter(|time| !time.is_empty()) {- label.push(' ');- label.push_str(time);- }- if let Some(end) = attributes- .get("end")- .filter(|end| !end.is_empty() && *end != start)- {- label.push_str(" – ");- label.push_str(&humanize_notion_date(end));- }- label-}--fn humanize_notion_date(value: &str) -> String {- chrono::NaiveDate::parse_from_str(value, "%Y-%m-%d")- .map(|date| date.format("%B %-d, %Y").to_string())- .unwrap_or_else(|_| value.to_string())-}--fn notion_attributes(attrs: &str) -> std::collections::HashMap<String, String> {- NOTION_ATTR_RE- .captures_iter(attrs)- .map(|captures| (captures["name"].to_string(), captures["value"].to_string()))- .collect()-}--fn escape_markdown_link_label(label: &str) -> String {- label- .replace('\\', "\\\\")- .replace('[', "\\[")- .replace(']', "\\]")-}--#[derive(Debug)]-enum DroppedNotionContainer {- Plain,- Callout {- icon: Option<String>,- emitted_icon: bool,- },-}--fn flatten_notion_containers(input: &str) -> String {- let mut output = Vec::new();- let mut containers: Vec<DroppedNotionContainer> = Vec::new();-- for line in input.lines() {- let trimmed = line.trim();- if is_notion_container_close(trimmed) {- containers.pop();- continue;- }- if let Some(container) = notion_container_open(trimmed) {- containers.push(container);- continue;- }-- let mut flattened = drop_notion_container_indentation(line, containers.len()).to_string();- if containers- .iter()- .any(|container| matches!(container, DroppedNotionContainer::Callout { .. }))- {- let icon = containers- .iter_mut()- .rev()- .find_map(|container| match container {- DroppedNotionContainer::Callout { icon, emitted_icon } if !*emitted_icon => {- *emitted_icon = true;- icon.take()- }- _ => None,- });- flattened = match (icon, flattened.trim().is_empty()) {- (Some(icon), false) => format!("> {icon} {flattened}"),- (Some(icon), true) => format!("> {icon}"),- (None, false) => format!("> {flattened}"),- (None, true) => ">".to_string(),- };- }- output.push(flattened);- }-- output.join("\n")-}--fn notion_container_open(line: &str) -> Option<DroppedNotionContainer> {- for tag in [- "details",- "columns",- "column",- "synced_block",- "synced_block_reference",- ] {- if line.starts_with(&format!("<{tag}")) && line.ends_with('>') {- return Some(DroppedNotionContainer::Plain);- }- }- if line.starts_with("<callout") && line.ends_with('>') {- let attrs = line- .strip_prefix("<callout")- .and_then(|line| line.strip_suffix('>'))- .unwrap_or_default();- return Some(DroppedNotionContainer::Callout {- icon: notion_attributes(attrs).remove("icon"),- emitted_icon: false,- });- }- None-}--fn is_notion_container_close(line: &str) -> bool {- [- "</details>",- "</callout>",- "</columns>",- "</column>",- "</synced_block>",- "</synced_block_reference>",- ]- .contains(&line)-}--fn drop_notion_container_indentation(mut line: &str, levels: usize) -> &str {- for _ in 0..levels {- if let Some(rest) = line.strip_prefix('\t') {- line = rest;- } else if let Some(rest) = line.strip_prefix(" ") {- line = rest;- } else {- break;- }- }- line-}--fn normalize_escaped_notion_todos(input: &str) -> String {- input- .lines()- .map(|line| {- ESCAPED_NOTION_TODO- .captures(line)- .map(|captures| {- format!(- "{}- [{}]{}",- &captures["indent"],- captures["state"].to_ascii_lowercase(),- &captures["rest"]- )- })- .unwrap_or_else(|| line.to_string())- })- .collect::<Vec<_>>()- .join("\n")-}-fn transform_outside_fenced_code(input: &str, mut transform: impl FnMut(&str) -> String) -> String {let mut output = String::with_capacity(input.len());let mut fragment = String::new();@@ -2384,83 +1762,6 @@ fn transform_outside_fenced_code(input: &str, mut transform: impl FnMut(&str) ->output}-fn contains_outside_fenced_code(input: &str, pattern: ®ex::Regex) -> bool {- let mut found = false;- let _ = transform_outside_fenced_code(input, |fragment| {- found |= pattern.is_match(fragment);- fragment.to_string()- });- found-}--fn convert_notion_tables(input: &str) -> String {- let mut output = String::with_capacity(input.len());- let mut remaining = input;- while let Some(start) = remaining.find("<table") {- output.push_str(&remaining[..start]);- let table = &remaining[start..];- let Some(close_start) = table.find("</table>") else {- output.push_str(table);- return output;- };- let close_end = close_start + "</table>".len();- let fragment = &table[..close_end];- match notion_table_to_markdown(fragment) {- Some(converted) => output.push_str(&converted),- None => output.push_str(fragment),- }- remaining = &table[close_end..];- }- output.push_str(remaining);- output-}--fn notion_table_to_markdown(table: &str) -> Option<String> {- let mut rows: Vec<Vec<String>> = TABLE_ROW_RE- .captures_iter(table)- .filter_map(|row| {- let cells: Vec<String> = TABLE_CELL_RE- .captures_iter(&row["body"])- .map(|cell| normalize_notion_table_cell(&cell["body"]))- .collect();- (!cells.is_empty()).then_some(cells)- })- .collect();- let column_count = rows.iter().map(Vec::len).max()?;- if column_count == 0 {- return None;- }- for row in &mut rows {- row.resize(column_count, String::new());- }-- let mut lines = Vec::with_capacity(rows.len() + 1);- for (index, row) in rows.into_iter().enumerate() {- lines.push(format!("| {} |", row.join(" | ")));- if index == 0 {- lines.push(format!(- "| {} |",- std::iter::repeat_n("---", column_count)- .collect::<Vec<_>>()- .join(" | ")- ));- }- }- Some(lines.join("\n"))-}--fn normalize_notion_table_cell(cell: &str) -> String {- let with_breaks = BREAK_TAG_RE.replace_all(cell, "\n");- let without_tags = INLINE_TAG_RE.replace_all(&with_breaks, "");- without_tags- .lines()- .map(str::trim)- .filter(|line| !line.is_empty())- .collect::<Vec<_>>()- .join("\\n")- .replace('|', "|")-}-/// Map a Linear workflow status name onto Macro's task status label./// Returns `None` for anything unrecognized (the raw label then stays in/// the task body's footer instead).diff --git a/crates/import/src/domain/service/admission.rs b/crates/import/src/domain/service/admission.rsindex bf4aa3fe11..00961d33b0 100644--- a/crates/import/src/domain/service/admission.rs+++ b/crates/import/src/domain/service/admission.rs@@ -1,13 +1,13 @@//! Admission belongs at AI boundaries, not at deterministic import claims.-use super::{ImportServiceImpl, ImportSource, Result, RunStatus};-use crate::domain::ports::{ImportRepo, SlackWorkspaceSource};+use super::ImportServiceImpl;+use crate::domain::ports::SlackWorkspaceSource;use ai_billing::{AiAdmissionError, AiAdmissionService};use ai_usage::AiFeature;use macro_user_id::user_id::MacroUserIdStr;use std::sync::Arc;-impl<R, S, C, W: SlackWorkspaceSource> ImportServiceImpl<R, S, C, W> {+impl<R, S, C, W: SlackWorkspaceSource, N: Send + Sync + 'static> ImportServiceImpl<R, S, C, W, N> {/// Attach configured shared admission. Production hosts must supply this;/// the source-compatible constructor defaults to disabled enforcement.pub fn with_admission(mut self, admission: Arc<dyn AiAdmissionService>) -> Self {@@ -21,44 +21,9 @@ impl<R, S, C, W: SlackWorkspaceSource> ImportServiceImpl<R, S, C, W> {) -> std::result::Result<(), AiAdmissionError> {self.admission.admit(user, AiFeature::Import).await}-- pub(super) async fn admit_gather(- &self,- user: &MacroUserIdStr<'static>,- source: ImportSource,- ) -> std::result::Result<(), AiAdmissionError> {- match source {- ImportSource::Slack => Ok(()),- ImportSource::Linear | ImportSource::Notion => self.admit_ai(user).await,- }- }-}--impl<R: ImportRepo, S, C, W: SlackWorkspaceSource> ImportServiceImpl<R, S, C, W> {- /// Preserve no-op retries and settings before checking new AI spending.- /// Slack discovery is deterministic until its fallback actually starts.- pub(super) async fn prepare_gather(- &self,- user: &MacroUserIdStr<'static>,- source: ImportSource,- from: &[RunStatus],- ) -> Result<bool> {- if let Some(run) = self- .repo- .list_runs(user)- .await?- .iter()- .find(|run| run.source == source)- && !from.contains(&run.status)- {- return Ok(false);- }- self.admit_gather(user, source).await?;- Ok(true)- }}-/// Persist stable, sanitized admission codes in existing run/row error fields.+/// Persist stable, sanitized admission codes in existing row error fields./// Unavailability is retryable; exhaustion requires a quota/policy change.pub(super) fn failure_reason(error: &anyhow::Error) -> String {match error.downcast_ref::<AiAdmissionError>() {diff --git a/crates/import/src/domain/service/notion_workspace.rs b/crates/import/src/domain/service/notion_workspace.rsnew file mode 100644index 0000000000..e1c67bf532--- /dev/null+++ b/crates/import/src/domain/service/notion_workspace.rs@@ -0,0 +1,856 @@+//! Importing a Notion workspace: inventory, triage, a reviewed plan, and the+//! write that turns the plan into folders, documents, tasks and databases.+//!+//! Planning and writing both run detached from the request that started+//! them, recorded on the user's Notion run row: `running` while planning,+//! `ready` once a plan is staged, `importing` while writing, then+//! `completed`, `failed` or `dismissed` (cancelled). The process running a+//! job holds a lease on the run; when it dies or shuts down, another process+//! adopts the run and resumes the job (`run`), and every step checks the row+//! so a cancel from any process stops the job.++use std::collections::HashMap;+use std::time::{Duration, Instant};++use agent_session::domain::model::AgentSessionId;+use futures::{FutureExt, StreamExt, stream};+use import_notion::domain::inventory::{all_users, inventory};+use import_notion::domain::model::{+ ImportScope, NotionDataSource, NotionId, NotionPage, NotionUser, PageMarkdown, Paged,+ SearchHit, SearchObject,+};+use import_notion::domain::ports::{NotionApi, NotionConnections, NotionError};+use macro_user_id::user_id::MacroUserIdStr;+use tracing::Instrument;+use uuid::Uuid;++use super::{ImportServiceImpl, NOTION_IMPORT_MODEL, NotionWorkspaceImporter};+use crate::domain::models::{+ ImportEntity, ImportKind, ImportSource, ImportStatus, NotionDecision, NotionImportReport,+ NotionJob, NotionSharing, NotionWriteChoices, RunStatus, ScanCounts, WrittenKind,+};+use crate::domain::ports::{+ CanonicalImportRepo, EntityCreator, ImportError, ImportRepo, NotionPlanRepo, NotionRunRepo,+ Result, SharedEntity, SlackWorkspaceSource, WorkspaceWriter, WrittenObjects,+};+use mcp_select::ConnectorSelect;+use object::{Outcome, Target};+use run::{IMPORT_STOPPED, JobControl, Stop, WriteSet};++mod events;+mod object;+mod plan;+mod report;+mod run;+mod scan;+mod sharing;+mod triage;+mod values;+mod write;++pub(super) use report::report;+/// How often a running write checks whether the user cancelled it.+const CANCEL_CHECK_EVERY: Duration = Duration::from_secs(3);+/// How often progress is pushed to the client while a job runs.+const NOTIFY_EVERY: Duration = Duration::from_secs(2);++/// The default: no Notion workspace is reachable, so workspace imports report+/// Notion as not connected.+#[derive(Debug, Clone, Copy, Default)]+pub struct NoNotionConnections;++/// An uninhabited client: [`NoNotionConnections`] never opens one.+pub enum NoNotionApi {}++impl NotionConnections for NoNotionConnections {+ type Api = NoNotionApi;++ async fn for_user(+ &self,+ _user: &MacroUserIdStr<'static>,+ ) -> std::result::Result<Option<Self::Api>, NotionError> {+ Ok(None)+ }+}++impl NotionApi for NoNotionApi {+ async fn search(+ &self,+ _object: SearchObject,+ _cursor: Option<String>,+ ) -> std::result::Result<Paged<SearchHit>, NotionError> {+ match *self {}+ }++ async fn page(&self, _id: &NotionId) -> std::result::Result<NotionPage, NotionError> {+ match *self {}+ }++ async fn page_markdown(+ &self,+ _id: &NotionId,+ ) -> std::result::Result<PageMarkdown, NotionError> {+ match *self {}+ }++ async fn data_source(+ &self,+ _id: &NotionId,+ ) -> std::result::Result<NotionDataSource, NotionError> {+ match *self {}+ }++ async fn database_sources(+ &self,+ _database: &NotionId,+ ) -> std::result::Result<Vec<NotionId>, NotionError> {+ match *self {}+ }++ async fn query_data_source(+ &self,+ _id: &NotionId,+ _cursor: Option<String>,+ ) -> std::result::Result<Paged<NotionPage>, NotionError> {+ match *self {}+ }++ async fn users(+ &self,+ _cursor: Option<String>,+ ) -> std::result::Result<Paged<NotionUser>, NotionError> {+ match *self {}+ }+}++/// Run states a new plan or write may start from.+const SETTLED: [RunStatus; 4] = [+ RunStatus::Ready,+ RunStatus::Completed,+ RunStatus::Failed,+ RunStatus::Dismissed,+];++/// Pushes progress to the client at most every [`NOTIFY_EVERY`].+struct Throttle(std::sync::Mutex<Option<Instant>>);++impl Throttle {+ fn new() -> Self {+ Self(std::sync::Mutex::new(None))+ }++ fn due(&self) -> bool {+ let Ok(mut last) = self.0.lock() else {+ return false;+ };+ let now = Instant::now();+ match *last {+ Some(previous) if now.duration_since(previous) < NOTIFY_EVERY => false,+ _ => {+ *last = Some(now);+ true+ }+ }+ }+}++impl<R, S, C, W, N> NotionWorkspaceImporter for ImportServiceImpl<R, S, C, W, N>+where+ R: ImportRepo + CanonicalImportRepo + NotionPlanRepo + NotionRunRepo + WrittenObjects + Clone,+ S: ConnectorSelect,+ C: EntityCreator + WorkspaceWriter,+ W: SlackWorkspaceSource,+ N: NotionConnections,+{+ async fn prepare_notion_import(+ &self,+ user: &MacroUserIdStr<'static>,+ scope: ImportScope,+ sharing: Option<NotionSharing>,+ wait: Duration,+ ) -> Result<NotionImportReport> {+ let sharing = match (sharing, self.repo.user_team_id(user).await?) {+ (Some(sharing), _) => sharing,+ (None, None) => NotionSharing::Personal,+ (None, Some(_)) => return Err(ImportError::NotionSharingRequired),+ };+ self.reap_stale_runs(user).await;+ self.adopt_stalled(Some(user)).await;+ if !self+ .repo+ .start_run(user, ImportSource::Notion, &SETTLED)+ .await?+ {+ return Err(ImportError::NotReady(+ "A Notion import is already being planned or written. Check its status, or cancel it first.",+ ));+ }+ self.hold_new_run(+ user,+ RunStatus::Running,+ &NotionJob::Plan {+ scope: scope.clone(),+ sharing,+ },+ )+ .await?;+ self.notify(user).await;+ let service = self.clone();+ let owner = user.clone();+ let job = self+ .jobs+ .tracker+ .spawn(async move { service.plan_workspace(&owner, scope, sharing).await });+ // The job keeps going if the wait runs out or the caller goes away.+ let _ = tokio::time::timeout(wait, job).await;+ self.notion_import_report(user).await+ }++ async fn notion_import_report(+ &self,+ user: &MacroUserIdStr<'static>,+ ) -> Result<NotionImportReport> {+ self.reap_stale_runs(user).await;+ // A run whose process stopped is picked up here, and rows a stopped+ // write left claimed go back to staged.+ self.adopt_stalled(Some(user)).await;+ if let Err(error) = self.repo.settle_importing(user, IMPORT_STOPPED).await {+ tracing::warn!(error = ?error, "could not settle stopped notion rows");+ }+ self.current_report(user).await+ }++ async fn attach_notion_review(+ &self,+ user: &MacroUserIdStr<'static>,+ review_id: Uuid,+ session_id: AgentSessionId,+ ) -> Result<u64> {+ let attached = self+ .repo+ .attach_review(user, ImportSource::Notion, review_id, session_id)+ .await?;+ self.notify(user).await;+ Ok(attached)+ }++ async fn start_notion_write(+ &self,+ user: &MacroUserIdStr<'static>,+ review_id: Uuid,+ choices: NotionWriteChoices,+ ) -> Result<NotionImportReport> {+ let staged = self+ .repo+ .list(user, Some(ImportSource::Notion), Some(ImportStatus::Staged))+ .await?;+ let mut planned: Vec<(ImportEntity, write::WriteItem)> = staged+ .into_iter()+ .filter(|row| row.review_id == Some(review_id))+ .filter_map(|row| write::WriteItem::from_entity(&row).map(|item| (row, item)))+ .collect();+ if planned.is_empty() {+ return Err(ImportError::NotReady(+ "This plan has nothing left to import.",+ ));+ }+ // The owner's sharing changes are recorded on the rows first, so a+ // resumed write shares exactly what they approved.+ let items: Vec<write::WriteItem> = planned.iter().map(|(_, item)| item.clone()).collect();+ let (to_share, to_keep) =+ sharing::sharing_changes(&items, &choices.shared, &choices.personal);+ self.repo+ .set_plan_sharing(user, &to_share, &to_keep)+ .await?;+ for (_, item) in &mut planned {+ if to_share.contains(&item.import_id) {+ item.shared = true;+ } else if to_keep.contains(&item.import_id) {+ item.shared = false;+ }+ }+ let team_id = self.repo.user_team_id(user).await?;+ if !self+ .repo+ .transition_run(+ user,+ ImportSource::Notion,+ &SETTLED,+ RunStatus::Importing,+ None,+ )+ .await?+ {+ return Err(ImportError::NotReady(+ "A Notion import is already being planned or written.",+ ));+ }+ self.hold_new_run(user, RunStatus::Importing, &NotionJob::Write { review_id })+ .await?;++ let mut to_write = Vec::new();+ for (row, item) in planned {+ let left_out = choices.excluded.contains(&row.id);+ match (left_out, item.decision) {+ (true, _) | (false, NotionDecision::Skip) => {+ self.repo.discard(user, row.id).await?;+ }+ // Left staged for the agent, which handles it by hand.+ (false, NotionDecision::Hand) => {}+ (false, NotionDecision::Copy | NotionDecision::Tasks) => to_write.push(item),+ }+ }+ let ids: Vec<Uuid> = to_write.iter().map(|item| item.import_id).collect();+ // Shared objects are claimed for the team now, before anything is+ // written, so a teammate writing the same object links to this copy.+ let shared: Vec<Uuid> = to_write+ .iter()+ .filter(|item| item.shared && item.writes())+ .map(|item| item.import_id)+ .collect();+ let claimed: std::collections::HashSet<Uuid> = self+ .repo+ .claim_importing(user, &ids, &shared, team_id)+ .await?+ .into_iter()+ .map(|row| row.id)+ .collect();+ to_write.retain(|item| claimed.contains(&item.import_id));+ self.notify(user).await;++ let service = self.clone();+ let owner = user.clone();+ let set = WriteSet {+ review_id,+ all: to_write.clone(),+ pending: to_write,+ };+ self.jobs+ .tracker+ .spawn(async move { service.write_plan(&owner, set).await });+ self.notion_import_report(user).await+ }++ async fn cancel_notion_import(&self, user: &MacroUserIdStr<'static>) -> Result<bool> {+ let cancelled = self+ .repo+ .transition_run(+ user,+ ImportSource::Notion,+ &[RunStatus::Running, RunStatus::Importing],+ RunStatus::Dismissed,+ Some("Cancelled"),+ )+ .await?;+ self.notify(user).await;+ Ok(cancelled)+ }+}++impl<R, S, C, W, N> ImportServiceImpl<R, S, C, W, N>+where+ R: ImportRepo + CanonicalImportRepo + NotionPlanRepo + NotionRunRepo + WrittenObjects + Clone,+ S: ConnectorSelect,+ C: EntityCreator + WorkspaceWriter,+ W: SlackWorkspaceSource,+ N: NotionConnections,+{+ /// Hold the run that just moved to `status` and record its job, so it+ /// survives this process; fails the run when that is not possible.+ async fn hold_new_run(+ &self,+ user: &MacroUserIdStr<'static>,+ status: RunStatus,+ job: &NotionJob,+ ) -> Result<()> {+ if self.take_run(user, job).await? {+ return Ok(());+ }+ self.repo+ .transition_run(+ user,+ ImportSource::Notion,+ &[status],+ RunStatus::Failed,+ Some(IMPORT_STOPPED),+ )+ .await?;+ Err(ImportError::NotReady(+ "The Notion import could not start. Try it again.",+ ))+ }++ /// Plan and record the outcome on the run.+ fn plan_workspace<'a>(+ &'a self,+ user: &'a MacroUserIdStr<'static>,+ scope: ImportScope,+ sharing: NotionSharing,+ ) -> futures::future::BoxFuture<'a, ()> {+ let span = tracing::info_span!(+ "notion_import.plan",+ %user,+ sharing = sharing.as_ref(),+ already_in_macro = tracing::field::Empty,+ stopped = tracing::field::Empty,+ );+ async move {+ let (control, _held) = self.hold_run(user);+ let outcome = tokio::select! {+ outcome = self.try_plan_workspace(user, scope, sharing, &control) => Some(outcome),+ () = control.stopped() => None,+ };+ if let Some(reason) = control.reason() {+ tracing::Span::current().record("stopped", reason.as_ref());+ }+ let outcome = match outcome {+ Some(outcome) => outcome,+ // Planning only reads, so the next process plans again.+ None if control.handed_over() => {+ if control.reason() == Some(Stop::Shutdown) {+ self.release_run(user).await;+ }+ return;+ }+ // Cancelled: the run is `dismissed` already.+ None => Ok(()),+ };+ let (to, error) = match &outcome {+ Ok(()) => (RunStatus::Ready, None),+ Err(error) => {+ tracing::warn!(error = ?error, "notion import planning failed");+ (RunStatus::Failed, Some(failure_text(error)))+ }+ };+ // A cancelled run is `dismissed` already, so this moves nothing.+ if let Err(error) = self.end_plan(user, to, error.as_deref()).await {+ tracing::warn!(error = ?error, "could not record the notion planning outcome");+ }+ self.notify(user).await;+ }+ .instrument(span)+ .boxed()+ }++ async fn try_plan_workspace(+ &self,+ user: &MacroUserIdStr<'static>,+ scope: ImportScope,+ sharing: NotionSharing,+ control: &JobControl,+ ) -> Result<()> {+ let api = self+ .notion+ .for_user(user)+ .await?+ .ok_or(ImportError::NotionNotConnected)?;+ let (counts, scanned) = tokio::sync::watch::channel(ScanCounts::default());+ let walk = async move {+ let found = inventory(&api, &scope, &|progress| {+ counts.send_replace(ScanCounts {+ pages: u32::try_from(progress.pages).unwrap_or(u32::MAX),+ data_sources: u32::try_from(progress.data_sources).unwrap_or(u32::MAX),+ });+ })+ .await;+ // Dropping `counts` here lets the recorder write the final counts.+ drop(counts);+ found+ };+ let (found, ()) = tokio::join!(walk, scan::record_scan(&self.repo, user, scanned));+ let found = found?;+ if control.reason().is_some() {+ return Ok(());+ }+ let tree = plan::PlanTree::new(&found);+ let team = self.repo.user_team_id(user).await?.is_some();+ let already_imported = self.already_in_macro(user, &tree).await?;+ tracing::Span::current().record("already_in_macro", already_imported.len());+ // The whole tree is staged before the decision model answers, so the+ // review shows it while each batch is decided.+ self.repo+ .replace_plan(+ user,+ ImportSource::Notion,+ &tree.staged_rows(sharing, team, &already_imported),+ )+ .await?;+ self.notify(user).await;+ // Objects already in Macro need no decision, so they cost no model call.+ let items = tree.triage_items(&already_imported);+ let usage = ai_usage::UsageContext::new(ai_usage::AiFeature::Import, user.clone());+ let batches = triage::decide(+ &items,+ sharing,+ || {+ let service = self.clone();+ let user = user.clone();+ async move { service.admit_ai(&user).await.map_err(anyhow::Error::from) }+ },+ self.recorder.as_ref(),+ usage,+ NOTION_IMPORT_MODEL,+ );+ self.stage_verdicts(user, &tree, &items, batches, sharing, team, control)+ .await+ }++ /// Write each batch of verdicts onto the staged plan as it arrives, then+ /// give anything the model never answered for the default verdict, so no+ /// row is left undecided once planning is done. Writing a verdict twice+ /// writes the same row, so a resumed plan is safe to stage again.+ #[expect(+ clippy::too_many_arguments,+ reason = "the planning run's state, passed once"+ )]+ async fn stage_verdicts(+ &self,+ user: &MacroUserIdStr<'static>,+ tree: &plan::PlanTree,+ items: &[triage::TriageItem],+ batches: impl futures::Stream<Item = HashMap<String, triage::Verdict>>,+ sharing: NotionSharing,+ team: bool,+ control: &JobControl,+ ) -> Result<()> {+ let mut batches = std::pin::pin!(batches);+ let mut decided = std::collections::HashSet::new();+ while let Some(verdicts) = batches.next().await {+ if control.reason().is_some() {+ return Ok(());+ }+ decided.extend(verdicts.keys().cloned());+ self.repo+ .update_plan_rows(+ user,+ ImportSource::Notion,+ &tree.decided_rows(&verdicts, sharing, team),+ )+ .await?;+ self.notify(user).await;+ }+ let unanswered: HashMap<String, triage::Verdict> = items+ .iter()+ .filter(|item| !decided.contains(&item.key))+ .map(|item| {+ (+ item.key.clone(),+ triage::default_verdict(item, "The decision model gave no answer"),+ )+ })+ .collect();+ if !unanswered.is_empty() {+ self.repo+ .update_plan_rows(+ user,+ ImportSource::Notion,+ &tree.decided_rows(&unanswered, sharing, team),+ )+ .await?;+ }+ Ok(())+ }++ /// Write the plan's pending objects and record the outcome on the run.+ #[tracing::instrument(+ skip(self, set),+ fields(+ %user,+ review_id = %set.review_id,+ items = set.all.len(),+ pending = set.pending.len(),+ stopped = tracing::field::Empty,+ waiting_on_teammates = tracing::field::Empty,+ ),+ name = "notion_import.write"+ )]+ async fn write_plan(&self, user: &MacroUserIdStr<'static>, set: WriteSet) {+ let (control, _held) = self.hold_run(user);+ let outcome = self.try_write_plan(user, &set, &control).await;+ if let Some(reason) = control.reason() {+ tracing::Span::current().record("stopped", reason.as_ref());+ }+ // The rows still `importing` belong to whoever resumes the write.+ if control.handed_over() {+ if control.reason() == Some(Stop::Shutdown) {+ self.release_run(user).await;+ }+ return;+ }+ let (to, error) = match &outcome {+ Ok(0) => (RunStatus::Completed, None),+ Ok(failed) => (+ RunStatus::Completed,+ Some(format!(+ "{failed} of {} objects could not be imported",+ set.pending.len()+ )),+ ),+ Err(error) => {+ tracing::warn!(error = ?error, "notion import write failed");+ (RunStatus::Failed, Some(failure_text(error)))+ }+ };+ // Anything still claimed (a cancel, or a failure part way) goes back+ // to staged so it can be imported later.+ // Rows waiting on a teammate's copy stay, and link once it is written.+ for item in &set.pending {+ if let Ok(Some(row)) = self.repo.get(user, item.import_id).await+ && row.status == ImportStatus::Importing+ && row.waiting_on.is_none()+ {+ let reason = if outcome.is_ok() {+ "Cancelled"+ } else {+ "The import stopped"+ };+ let _ = self+ .repo+ .mark_import_failed(user, item.import_id, reason)+ .await;+ }+ }+ if let Err(error) = self.end_write(user, to, error.as_deref()).await {+ tracing::warn!(error = ?error, "could not record the notion write outcome");+ }+ self.notify(user).await;+ }++ /// Write every pending object, reusing what earlier attempts wrote;+ /// returns how many failed.+ async fn try_write_plan(+ &self,+ user: &MacroUserIdStr<'static>,+ set: &WriteSet,+ control: &JobControl,+ ) -> Result<usize> {+ let api = self+ .notion+ .for_user(user)+ .await?+ .ok_or(ImportError::NotionNotConnected)?;+ let team_id = self.repo.user_team_id(user).await?;+ let people = self.people(&api, user).await;+ let ids: Vec<Uuid> = set.all.iter().map(|item| item.import_id).collect();+ let written = self.repo.written(user, &ids).await?;+ let links = self.links(user, &set.all, &people).await?;+ let root = self.root_folder(user).await?;++ // Folders first, parents before children. A folder is shared only+ // when everything written inside it is; it is shared again on every+ // attempt, which is harmless, so a stop between creating and sharing+ // it heals on resume.+ let layout = write::folder_layout(&set.all);+ let shared_folders = match team_id {+ Some(_) => sharing::shared_folders(&set.all, &layout),+ None => std::collections::HashSet::new(),+ };+ let by_id: HashMap<Uuid, &write::WriteItem> =+ set.all.iter().map(|item| (item.import_id, item)).collect();+ let parents: HashMap<Uuid, Option<Uuid>> = set+ .all+ .iter()+ .map(|item| (item.import_id, item.parent))+ .collect();+ let mut folders: HashMap<Uuid, Uuid> = HashMap::new();+ for folder_row in &layout.folders {+ let Some(item) = by_id.get(folder_row) else {+ continue;+ };+ let progress = write::Progress::new(&self.repo, user, item.import_id, &written);+ let folder = match progress.get(WrittenKind::Folder) {+ Some(folder) => folder,+ None => {+ let parent = write::folder_for(item, false, &folders, &parents, root);+ let folder = self+ .creator+ .create_folder(user, &item.title, Some(parent))+ .await?;+ progress.record(WrittenKind::Folder, folder).await?;+ folder+ }+ };+ if shared_folders.contains(folder_row) {+ self.creator+ .share_with_team(user, SharedEntity::Folder(folder))+ .await?;+ }+ folders.insert(*folder_row, folder);+ }++ let cancel = control.cancel.as_ref();+ let throttle = Throttle::new();+ let databases: tokio::sync::Mutex<HashMap<NotionId, write::WrittenDatabase>> =+ tokio::sync::Mutex::new(HashMap::new());+ let target = Target {+ api: &api,+ team_id,+ place: Place {+ folders: &folders,+ parents: &parents,+ root,+ },+ people: &people,+ links: &links,+ databases: &databases,+ written: &written,+ cancel,+ };+ let outcomes: Vec<Outcome> = stream::iter(set.pending.iter().cloned())+ .map(|item| {+ let target = ⌖+ let throttle = &throttle;+ async move {+ if cancel.stopped() {+ return Outcome::Done;+ }+ let outcome = self.write_or_fail(user, &item, target, control).await;+ if throttle.due() {+ self.notify(user).await;+ }+ outcome+ }+ .boxed()+ })+ .buffer_unordered(write::WRITE_CONCURRENCY)+ .collect()+ .await;++ // Objects a teammate was writing: link to what they finished, take+ // over what they gave up, and wait on the rest.+ let mut failed = 0;+ let mut waiting = Vec::new();+ for (item, outcome) in set.pending.iter().zip(outcomes) {+ match outcome {+ Outcome::Done => {}+ Outcome::Failed => failed += 1,+ Outcome::Waiting(_) => waiting.push(item),+ }+ }+ tracing::Span::current().record("waiting_on_teammates", waiting.len());+ for item in waiting {+ if cancel.stopped() {+ break;+ }+ if self.settle_waiting(user, item, &target, control).await == Outcome::Failed {+ failed += 1;+ }+ }++ // Databases earlier attempts finished still take part in relations.+ let mut databases = databases.into_inner();+ let finished: Vec<&write::WriteItem> = set+ .all+ .iter()+ .filter(|item| {+ item.kind == ImportKind::DataSource+ && item.decision == NotionDecision::Copy+ && !databases.contains_key(&item.notion_id)+ })+ .collect();+ for item in finished {+ let progress = write::Progress::new(&self.repo, user, item.import_id, &written);+ match write::recorded_database(&api, item, &progress).await {+ Ok(Some(database)) => {+ databases.insert(item.notion_id.clone(), database);+ }+ Ok(None) => {}+ Err(error) => {+ tracing::warn!(error = ?error, "a written notion database was not reread");+ }+ }+ }+ if !databases.is_empty()+ && !cancel.stopped()+ && let Err(error) =+ write::link_relations(self.creator.as_ref(), &self.repo, user, &databases, cancel)+ .await+ {+ tracing::warn!(error = ?error, "notion relations were not linked");+ }+ Ok(failed)+ }++ /// The user's "Notion" folder: the one earlier imports made while it+ /// still exists, otherwise a new one.+ async fn root_folder(&self, user: &MacroUserIdStr<'static>) -> Result<Uuid> {+ if let Some(folder) = self.repo.root_folder(user).await?+ && self.creator.folder_exists(user, folder).await?+ {+ return Ok(folder);+ }+ let folder = self+ .creator+ .create_folder(user, write::ROOT_FOLDER, None)+ .await?;+ self.repo.set_root_folder(user, folder).await?;+ Ok(folder)+ }++ /// Notion users matched to the user's team by email. Empty when Notion+ /// will not share emails; people columns then stay empty.+ async fn people(&self, api: &N::Api, user: &MacroUserIdStr<'static>) -> values::PeopleMap {+ let notion_users = match all_users(api).await {+ Ok(users) => users,+ Err(error) => {+ tracing::warn!(error = ?error, "notion users were not read; people stay empty");+ return values::PeopleMap::new();+ }+ };+ let members = self+ .creator+ .team_members(user)+ .await+ .unwrap_or_else(|error| {+ tracing::warn!(error = ?error, "team members were not read; people stay unmatched");+ Vec::new()+ });+ notion_users+ .into_iter()+ .map(|notion_user| {+ let email = notion_user.email.map(|email| email.to_lowercase());+ let macro_user = email.as_ref().and_then(|email| {+ members+ .iter()+ .find(|member| &member.email == email)+ .map(|member| member.user_id.clone())+ });+ (notion_user.id, values::Person { email, macro_user })+ })+ .collect()+ }++ /// Whether the user cancelled the import.+ async fn cancelled(&self, user: &MacroUserIdStr<'static>) -> bool {+ match self.repo.list_runs(user).await {+ Ok(runs) => runs.iter().any(|run| {+ run.source == ImportSource::Notion && run.status == RunStatus::Dismissed+ }),+ Err(error) => {+ tracing::warn!(error = ?error, "could not read the notion run");+ false+ }+ }+ }+}++/// Where written objects go: the folders made so far and the import's root.+struct Place<'a> {+ folders: &'a HashMap<Uuid, Uuid>,+ parents: &'a HashMap<Uuid, Option<Uuid>>,+ root: Uuid,+}++impl Place<'_> {+ fn folder_for(&self, item: &write::WriteItem, own: bool) -> Uuid {+ write::folder_for(item, own, self.folders, self.parents, self.root)+ }+}++/// A failure as the user reads it on the run or the row.+fn failure_text(error: &ImportError) -> String {+ match error {+ ImportError::Other(error) => super::admission::failure_reason(error),+ other => other.to_string(),+ }+}diff --git a/crates/import/src/domain/service/notion_workspace/events.rs b/crates/import/src/domain/service/notion_workspace/events.rsnew file mode 100644index 0000000000..1e914d9790--- /dev/null+++ b/crates/import/src/domain/service/notion_workspace/events.rs@@ -0,0 +1,201 @@+//! Telling the agent session that presented a plan's review what became of+//! it: `import.planned` once the plan is ready to approve, and+//! `review.settled` with the final report once its write ends, however it+//! ended.+//!+//! Each event is produced in the transaction that moves the run (see+//! [`NotionRunRepo::transition_reviewed_run`]), so the session hears of every+//! change exactly when it happens, from whichever process made it.++use agent_runtime_protocol::domain::notification::{+ ImportItem, ImportOutcome, ImportPlan, ImportSettlement, MAX_IMPORT_ITEMS, NotificationEvent,+ ReviewSettlement,+};+use agent_session::domain::notifications::{PendingNotification, ProduceNotification};+use import_notion::domain::ports::NotionConnections;+use macro_user_id::user_id::MacroUserIdStr;+use mcp_select::ConnectorSelect;+use uuid::Uuid;++use super::super::ImportServiceImpl;+use super::report;+use crate::domain::models::{+ ImportSource, NotionImportReport, NotionReportItem, ReviewedRun, RunStatus,+};+use crate::domain::ports::{+ CanonicalImportRepo, EntityCreator, ImportRepo, NotionPlanRepo, NotionRunRepo, Result,+ SlackWorkspaceSource, WorkspaceWriter, WrittenObjects,+};++/// Longest title, error or reason a settlement carries, in characters.+const MAX_TEXT_CHARS: usize = 300;++impl<R, S, C, W, N> ImportServiceImpl<R, S, C, W, N>+where+ R: ImportRepo + CanonicalImportRepo + NotionPlanRepo + NotionRunRepo + WrittenObjects + Clone,+ S: ConnectorSelect,+ C: EntityCreator + WorkspaceWriter,+ W: SlackWorkspaceSource,+ N: NotionConnections,+{+ /// End the user's write: move the run from `importing` to `to` (a+ /// cancelled run stays `dismissed`) and tell the session presenting its+ /// review how it went. Returns whether the run moved.+ pub(super) async fn end_write(+ &self,+ user: &MacroUserIdStr<'static>,+ to: RunStatus,+ error: Option<&str>,+ ) -> Result<bool> {+ // Read before the run moves: once it has, the rows still claimed go+ // back to staged and no longer say they were part of this write.+ let report = self.current_report(user).await?;+ let root_folder = self.repo.root_folder(user).await?;+ let moved = self+ .repo+ .transition_reviewed_run(+ user,+ &[RunStatus::Importing, RunStatus::Dismissed],+ to,+ error,+ |run| {+ let review = run.review?;+ let settlement = settlement(&report, root_folder, run)?;+ produce(ProduceNotification {+ session_id: review.session_id,+ subject_id: review.review_id.to_string(),+ event: NotificationEvent::ReviewSettled(ReviewSettlement::Import {+ import: settlement,+ }),+ dedupe_key: None,+ wake: true,+ })+ },+ )+ .await?;+ Ok(moved.is_some())+ }++ /// Finish planning: move the run from `running` to `to` and, once the+ /// plan is ready, tell the session presenting its review. Returns+ /// whether the run moved.+ pub(super) async fn end_plan(+ &self,+ user: &MacroUserIdStr<'static>,+ to: RunStatus,+ error: Option<&str>,+ ) -> Result<bool> {+ let report = self.current_report(user).await?;+ let moved = self+ .repo+ .transition_reviewed_run(user, &[RunStatus::Running], to, error, |run| {+ let review = run.review?;+ if run.status != RunStatus::Ready {+ return None;+ }+ produce(ProduceNotification {+ session_id: review.session_id,+ subject_id: review.review_id.to_string(),+ event: NotificationEvent::ImportPlanned(plan(&report)),+ dedupe_key: None,+ wake: true,+ })+ })+ .await?;+ Ok(moved.is_some())+ }++ /// The report on the user's current plan as the ledger has it now,+ /// without adopting or settling anything.+ pub(super) async fn current_report(+ &self,+ user: &MacroUserIdStr<'static>,+ ) -> Result<NotionImportReport> {+ let run = self+ .repo+ .list_runs(user)+ .await?+ .into_iter()+ .find(|run| run.source == ImportSource::Notion);+ let rows = self+ .repo+ .list(user, Some(ImportSource::Notion), None)+ .await?;+ let mut report = report(run.as_ref(), &rows);+ report.on_team = self.repo.user_team_id(user).await?.is_some();+ Ok(report)+ }+}++/// Validate an event and capture the producing span; a malformed event is+/// dropped rather than holding the run in its old state.+fn produce(input: ProduceNotification) -> Option<PendingNotification> {+ PendingNotification::new(input)+ .inspect_err(|error| tracing::warn!(error = ?error, "could not produce an import event"))+ .ok()+}++/// What the plan in `report` proposes.+fn plan(report: &NotionImportReport) -> ImportPlan {+ ImportPlan {+ copy: report.copy,+ task_lists: report.tasks,+ skipped: report.skip,+ by_hand: report.hand,+ }+}++/// The final report of the write `report` describes, now that `run` ended+/// it; `None` while the run has not ended.+fn settlement(+ report: &NotionImportReport,+ root_folder: Option<Uuid>,+ run: &ReviewedRun,+) -> Option<ImportSettlement> {+ let outcome = match run.status {+ RunStatus::Completed => ImportOutcome::Completed,+ RunStatus::Failed => ImportOutcome::Failed,+ RunStatus::Dismissed => ImportOutcome::Cancelled,+ RunStatus::Running | RunStatus::Ready | RunStatus::Importing => return None,+ };+ Some(ImportSettlement {+ outcome,+ error: run.error.as_deref().map(bounded),+ imported: report.imported,+ planned: report.copy + report.tasks,+ documents: report.documents,+ databases: report.databases,+ database_rows: report.database_rows,+ task_lists: report.tasks,+ task_rows: report.task_rows,+ skipped: report.skip,+ not_written: report.waiting + report.writing,+ shared_with_team: report.shared_with_team,+ already_in_macro: report.already_in_macro,+ waiting_on_teammates: report.waiting_on_teammates,+ root_folder_id: root_folder,+ failures: items(&report.failures),+ hand_items: items(&report.hand_items),+ })+}++fn items(items: &[NotionReportItem]) -> Vec<ImportItem> {+ items+ .iter()+ .take(MAX_IMPORT_ITEMS)+ .map(|item| ImportItem {+ title: bounded(&item.title),+ url: item.url.clone(),+ reason: item.reason.as_deref().map(bounded),+ })+ .collect()+}++fn bounded(text: &str) -> String {+ let mut chars = text.chars();+ let mut bounded: String = chars.by_ref().take(MAX_TEXT_CHARS).collect();+ if chars.next().is_some() {+ bounded.push('…');+ }+ bounded+}diff --git a/crates/import/src/domain/service/notion_workspace/object.rs b/crates/import/src/domain/service/notion_workspace/object.rsnew file mode 100644index 0000000000..6de3c47d79--- /dev/null+++ b/crates/import/src/domain/service/notion_workspace/object.rs@@ -0,0 +1,336 @@+//! Writing one object of a plan: claiming it for the team, linking it to a+//! teammate's copy, or writing it and sharing what it became.++use std::collections::HashMap;++use import_notion::domain::inventory::full_markdown;+use import_notion::domain::model::NotionId;+use import_notion::domain::ports::{NotionApi, NotionConnections};+use macro_user_id::user_id::MacroUserIdStr;+use mcp_select::ConnectorSelect;+use uuid::Uuid;++use super::super::ImportServiceImpl;+use super::run::JobControl;+use super::{Place, failure_text, plan, values, write};+use crate::domain::models::{+ ImportEntity, ImportKind, ImportStatus, NotionAlreadyImported, NotionDecision, TeamClaim,+ WrittenObject,+};+use crate::domain::ports::{+ CanonicalImportRepo, EntityCreator, ImportError, ImportRepo, NotionLinks, NotionPlanRepo,+ NotionRunRepo, Result, SharedEntity, SlackWorkspaceSource, WorkspaceWriter, WrittenObjects,+};++/// Times a waiting object is checked again before it is given up on: each+/// check finds the teammate's copy finished, taken over, or still writing.+const WAIT_CHECKS: u8 = 3;+/// What an object says when its team copy kept changing hands.+const KEPT_MOVING: &str =+ "A teammate's import of this kept stopping and starting. Import it again.";++impl<R, S, C, W, N> ImportServiceImpl<R, S, C, W, N>+where+ R: ImportRepo + CanonicalImportRepo + NotionPlanRepo + NotionRunRepo + WrittenObjects + Clone,+ S: ConnectorSelect,+ C: EntityCreator + WorkspaceWriter,+ W: SlackWorkspaceSource,+ N: NotionConnections,+{+ /// An object a teammate was writing when the write reached it, checked+ /// again once the rest is written: linked to their copy when it is done,+ /// written when they stopped, otherwise left waiting on their row, which+ /// links it once written.+ pub(super) async fn settle_waiting(+ &self,+ user: &MacroUserIdStr<'static>,+ item: &write::WriteItem,+ target: &Target<'_, N::Api>,+ control: &JobControl,+ ) -> Outcome {+ for _ in 0..WAIT_CHECKS {+ let holder = match self.write_or_fail(user, item, target, control).await {+ Outcome::Waiting(holder) => holder,+ done => return done,+ };+ match self.repo.wait_on(user, item.import_id, holder).await {+ Ok(true) => return Outcome::Done,+ // The teammate finished or stopped meanwhile: check again.+ Ok(false) => {}+ Err(error) => {+ tracing::warn!(error = ?error, "could not wait on a teammate's notion import");+ break;+ }+ }+ }+ let _ = self+ .repo+ .mark_import_failed(user, item.import_id, KEPT_MOVING)+ .await;+ Outcome::Failed+ }++ /// Write one object, recording a failure on its row.+ pub(super) async fn write_or_fail(+ &self,+ user: &MacroUserIdStr<'static>,+ item: &write::WriteItem,+ target: &Target<'_, N::Api>,+ control: &JobControl,+ ) -> Outcome {+ let progress = write::Progress::new(&self.repo, user, item.import_id, target.written);+ match self.write_item(user, item, target, &progress).await {+ Ok(Written::Done) => Outcome::Done,+ Ok(Written::Waiting(holder)) => Outcome::Waiting(holder),+ // Stopped part way for the next process: the row stays+ // `importing` for it to finish.+ Err(_) if control.handed_over() => Outcome::Done,+ Err(error) => {+ tracing::warn!(error = ?error, kind = item.kind.as_ref(), "notion object was not imported");+ let _ = self+ .repo+ .mark_import_failed(user, item.import_id, &failure_text(&error))+ .await;+ Outcome::Failed+ }+ }+ }++ /// Write one object, or link it to the team's copy. A shared object is+ /// written only by the row holding the team's claim.+ async fn write_item(+ &self,+ user: &MacroUserIdStr<'static>,+ item: &write::WriteItem,+ target: &Target<'_, N::Api>,+ progress: &write::Progress<'_, R>,+ ) -> Result<Written> {+ let claim = match target.team_id {+ Some(team_id) if item.shared || item.already_imported => {+ self.repo+ .claim_team_object(user, item.import_id, team_id, item.shared)+ .await?+ }+ _ => TeamClaim::Free,+ };+ let share = match claim {+ TeamClaim::Linked {+ entity_id,+ entity_type,+ } => {+ self.repo+ .mark_imported(user, item.import_id, &entity_id, &entity_type, None)+ .await?;+ return Ok(Written::Done);+ }+ TeamClaim::Waiting { holder } => return Ok(Written::Waiting(holder)),+ TeamClaim::Held => true,+ TeamClaim::Free => false,+ };+ let (api, cancel) = (target.api, target.cancel);+ let (entity_id, entity_type) = match (item.kind, item.decision) {+ (ImportKind::DataSource, NotionDecision::Tasks) => {+ let folder = target.place.folder_for(item, false);+ let tasks = write::write_tasks(+ api,+ self.creator.as_ref(),+ user,+ item,+ folder,+ share,+ target.people,+ target.links,+ progress,+ cancel,+ )+ .await?;+ (tasks.to_string(), write::FOLDER_ENTITY)+ }+ (ImportKind::DataSource, _) => {+ let folder = target.place.folder_for(item, false);+ let written = write::write_database(+ api,+ self.creator.as_ref(),+ user,+ item,+ folder,+ share,+ target.people,+ target.links,+ progress,+ cancel,+ )+ .await?;+ let id = written.database_id.to_string();+ target+ .databases+ .lock()+ .await+ .insert(item.notion_id.clone(), written);+ (id, write::DATABASE_ENTITY)+ }+ _ => {+ let folder = target.place.folder_for(item, true);+ let markdown = full_markdown(api, &item.notion_id).await?;+ let id = target+ .links+ .pages+ .get(&item.notion_id)+ .map(|page| page.document_id)+ .ok_or_else(|| {+ ImportError::Other(anyhow::anyhow!("page {} has no id", item.notion_id))+ })?;+ let properties = match api.page(&item.notion_id).await {+ Ok(page) => values::document_properties(&page),+ Err(error) => {+ tracing::warn!(error = ?error, "notion page properties were not read");+ Default::default()+ }+ };+ let document = self+ .creator+ .create_document(+ user,+ id,+ &item.title,+ &markdown,+ target.links,+ &properties,+ Some(folder),+ )+ .await?;+ if share {+ self.creator+ .share_with_team(user, SharedEntity::Document(id))+ .await?;+ }+ (document, write::DOCUMENT_ENTITY)+ }+ };+ // The row's team claim, taken before writing, stays on it.+ self.repo+ .mark_imported(user, item.import_id, &entity_id, entity_type, None)+ .await?;+ Ok(Written::Done)+ }++ /// Where the plan's pages and people resolve in Macro, known before+ /// anything is written: the user's own document when it exists, the+ /// team's copy of a shared page (written, or being written by a+ /// teammate), otherwise the document the page becomes.+ pub(super) async fn links(+ &self,+ user: &MacroUserIdStr<'static>,+ items: &[write::WriteItem],+ people: &values::PeopleMap,+ ) -> Result<NotionLinks> {+ let pages: Vec<&write::WriteItem> = items+ .iter()+ .filter(|item| item.kind != ImportKind::DataSource)+ .collect();+ let foreign_ids: Vec<String> = pages+ .iter()+ .filter(|item| item.shared || item.already_imported)+ .map(|item| item.notion_id.as_str().to_string())+ .collect();+ let copies: HashMap<String, ImportEntity> = self+ .repo+ .team_copies(user, &foreign_ids)+ .await?+ .into_iter()+ .map(|copy| (copy.foreign_id.clone(), copy))+ .collect();+ let mut imported = HashMap::new();+ for item in pages {+ let own = self+ .repo+ .get(user, item.import_id)+ .await?+ .filter(|row| row.status == ImportStatus::Imported);+ let document = match (own, copies.get(item.notion_id.as_str())) {+ (Some(row), _) => imported_document(&row),+ (None, Some(copy)) if copy.status == ImportStatus::Importing => {+ Some(write::document_id(copy.id))+ }+ (None, Some(copy)) => imported_document(copy),+ (None, None) => None,+ };+ if let Some(document) = document {+ imported.insert(item.notion_id.clone(), document);+ }+ }+ Ok(write::notion_links(items, &imported, people))+ }++ /// Teammates' copies shared with the team of the planned objects, by+ /// Notion id. Only finished copies count.+ pub(super) async fn already_in_macro(+ &self,+ user: &MacroUserIdStr<'static>,+ tree: &plan::PlanTree,+ ) -> Result<HashMap<String, NotionAlreadyImported>> {+ let foreign_ids: Vec<String> = tree+ .nodes+ .iter()+ .map(|node| node.id.as_str().to_string())+ .collect();+ let copies = self.repo.team_copies(user, &foreign_ids).await?;+ Ok(copies+ .into_iter()+ .filter(|copy| copy.status == ImportStatus::Imported)+ .filter_map(|copy| {+ let by = MacroUserIdStr::parse_from_str(©.user_id)+ .ok()?+ .email_str()+ .to_string();+ Some((+ copy.foreign_id,+ NotionAlreadyImported {+ by,+ entity_id: copy.entity_id?,+ entity_type: copy.entity_type?,+ },+ ))+ })+ .collect())+ }+}++/// The document an imported row became, when it became one.+fn imported_document(row: &ImportEntity) -> Option<Uuid> {+ if row.entity_type.as_deref() != Some(write::DOCUMENT_ENTITY) {+ return None;+ }+ row.entity_id.as_deref()?.parse().ok()+}++/// How writing one object ended.+#[derive(Debug, Clone, Copy, PartialEq, Eq)]+pub(super) enum Outcome {+ /// Written, linked, or left for the next process.+ Done,+ /// It failed; the row records why.+ Failed,+ /// A teammate is writing it for the team: the holder's row.+ Waiting(Uuid),+}++/// What one object's write did.+enum Written {+ /// It was written, or linked to the team's copy.+ Done,+ /// A teammate is writing it for the team; nothing was written.+ Waiting(Uuid),+}++/// Everything one object's write needs from the write around it.+pub(super) struct Target<'a, Api> {+ pub api: &'a Api,+ pub team_id: Option<Uuid>,+ pub place: Place<'a>,+ pub people: &'a values::PeopleMap,+ pub links: &'a NotionLinks,+ pub databases: &'a tokio::sync::Mutex<HashMap<NotionId, write::WrittenDatabase>>,+ pub written: &'a [WrittenObject],+ pub cancel: &'a write::Cancel,+}diff --git a/crates/import/src/domain/service/notion_workspace/plan.rs b/crates/import/src/domain/service/notion_workspace/plan.rsnew file mode 100644index 0000000000..4806dc411d--- /dev/null+++ b/crates/import/src/domain/service/notion_workspace/plan.rs@@ -0,0 +1,354 @@+//! The plan's shape: the workspace as a tree of pages and data sources, the+//! items the decision model sees, and the rows the ledger stages.++use std::collections::{HashMap, HashSet, VecDeque};++use import_notion::domain::model::{+ DataSourceSummary, Inventory, NotionId, NotionParent, NotionPropertyKind,+};++use crate::domain::models::{+ ImportKind, NotionAlreadyImported, NotionDecision, NotionDocMeta, NotionPlanItem, NotionSharing,+};+use crate::domain::ports::PlanRow;++use super::sharing::plan_sharing;+use super::triage::{TriageItem, TriageTable, Verdict, default_verdict};++#[cfg(test)]+mod test;++/// Titles given to objects Notion has none for.+pub(super) const UNTITLED_PAGE: &str = "Untitled";+pub(super) const UNTITLED_DATABASE: &str = "Untitled database";++/// One object in the plan's tree.+#[derive(Debug, Clone, PartialEq)]+pub(super) struct Node {+ /// The object's id.+ pub id: NotionId,+ /// A page or a data source.+ pub kind: ImportKind,+ /// The title, never empty.+ pub title: String,+ /// Its Notion URL.+ pub url: String,+ /// The node it sits under, when that node is in scope.+ pub parent: Option<NotionId>,+ /// For a data source: what planning learned about its rows.+ pub table: Option<TriageTable>,+}++/// The objects in scope, parents before children.+#[derive(Debug, Clone, PartialEq, Default)]+pub(super) struct PlanTree {+ /// Every node, in breadth-first order from the roots.+ pub nodes: Vec<Node>,+}++impl PlanTree {+ /// Arrange an inventory into a tree. A page under a page or a data source+ /// under a page hangs off that page when it is in scope; everything else+ /// is a root.+ pub fn new(inventory: &Inventory) -> Self {+ let mut nodes: Vec<Node> = inventory+ .pages+ .iter()+ .map(|page| Node {+ id: page.id.clone(),+ kind: ImportKind::Page,+ title: titled(&page.title, UNTITLED_PAGE),+ url: page.url.clone(),+ parent: parent_page(&page.parent),+ table: None,+ })+ .collect();+ nodes.extend(inventory.data_sources.iter().map(data_source_node));++ let known: HashSet<NotionId> = nodes.iter().map(|node| node.id.clone()).collect();+ for node in &mut nodes {+ if node+ .parent+ .as_ref()+ .is_some_and(|parent| !known.contains(parent))+ {+ node.parent = None;+ }+ }+ Self {+ nodes: breadth_first(nodes),+ }+ }++ /// The titles above `id`, outermost first.+ fn path(&self, id: &NotionId, index: &HashMap<&NotionId, &Node>) -> Vec<String> {+ let mut path = Vec::new();+ let mut seen = HashSet::new();+ let mut current = index.get(id).and_then(|node| node.parent.as_ref());+ while let Some(parent) = current {+ if !seen.insert(parent) {+ break;+ }+ let Some(node) = index.get(parent) else { break };+ path.push(node.title.clone());+ current = node.parent.as_ref();+ }+ path.reverse();+ path+ }++ /// What the decision model sees of each node, leaving out those already+ /// in Macro (keyed by Notion id), which need no decision.+ pub fn triage_items(+ &self,+ already_imported: &HashMap<String, NotionAlreadyImported>,+ ) -> Vec<TriageItem> {+ let index = self.index();+ let children = self.child_counts();+ self.nodes+ .iter()+ .filter(|node| !already_imported.contains_key(node.id.as_str()))+ .map(|node| TriageItem {+ key: node.id.to_string(),+ kind: node.kind,+ title: node.title.clone(),+ path: self.path(&node.id, &index),+ children: children.get(&node.id).copied().unwrap_or(0),+ table: node.table.clone(),+ })+ .collect()+ }++ /// The rows planning stages before the decision model answers, parents+ /// first: every node, waiting for its decision, except those already in+ /// Macro (keyed by Notion id), which are decided already: they link to+ /// the teammate's copy. `team` says whether the user has a team.+ pub fn staged_rows(+ &self,+ sharing: NotionSharing,+ team: bool,+ already_imported: &HashMap<String, NotionAlreadyImported>,+ ) -> Vec<PlanRow> {+ let index = self.index();+ self.nodes+ .iter()+ .map(|node| {+ let key = node.id.to_string();+ let copy = already_imported.get(&key).cloned();+ let verdict = copy+ .as_ref()+ .map(|_| default_verdict(&triage_item(node, &key), ALREADY_IN_MACRO));+ self.row(node, verdict, copy, sharing, team, &index)+ })+ .collect()+ }++ /// The rows for the nodes `verdicts` decides, with their sharing under+ /// `sharing`, parents first.+ pub fn decided_rows(+ &self,+ verdicts: &HashMap<String, Verdict>,+ sharing: NotionSharing,+ team: bool,+ ) -> Vec<PlanRow> {+ let index = self.index();+ self.nodes+ .iter()+ .filter_map(|node| {+ let verdict = verdicts.get(node.id.as_str())?.clone();+ Some(self.row(node, Some(verdict), None, sharing, team, &index))+ })+ .collect()+ }++ /// One node's row: decided by `verdict`, or waiting for the decision+ /// model without one; `copy` is the teammate's copy standing in for it.+ fn row(+ &self,+ node: &Node,+ verdict: Option<Verdict>,+ copy: Option<NotionAlreadyImported>,+ sharing: NotionSharing,+ team: bool,+ index: &HashMap<&NotionId, &Node>,+ ) -> PlanRow {+ let placed = verdict+ .as_ref()+ .map(|verdict| plan_sharing(sharing, verdict, team));+ let plan = NotionPlanItem {+ decision: verdict.as_ref().map(|verdict| verdict.decision),+ reason: verdict.as_ref().and_then(|verdict| verdict.reason.clone()),+ path: self.path(&node.id, index),+ rows: node+ .table+ .as_ref()+ .map(|table| u32::try_from(table.rows).unwrap_or(u32::MAX)),+ sample_titles: node+ .table+ .as_ref()+ .map(|table| table.sample_titles.clone())+ .unwrap_or_default(),+ task_fields: verdict+ .as_ref()+ .filter(|verdict| verdict.decision == NotionDecision::Tasks)+ .and_then(|verdict| verdict.task_fields.clone()),+ shared: placed.as_ref().is_some_and(|placed| placed.shared),+ shared_reason: placed.as_ref().and_then(|placed| placed.reason.clone()),+ shareable: placed.as_ref().is_some_and(|placed| placed.shareable) && copy.is_none(),+ already_imported: copy,+ };+ let metadata = NotionDocMeta::for_plan(node.title.clone(), node.url.clone(), plan);+ PlanRow {+ kind: node.kind,+ foreign_id: node.id.to_string(),+ parent_foreign_id: node.parent.as_ref().map(NotionId::to_string),+ metadata: serde_json::to_value(metadata).unwrap_or_default(),+ }+ }++ fn index(&self) -> HashMap<&NotionId, &Node> {+ self.nodes.iter().map(|node| (&node.id, node)).collect()+ }++ fn child_counts(&self) -> HashMap<NotionId, usize> {+ let mut counts = HashMap::new();+ for parent in self.nodes.iter().filter_map(|node| node.parent.clone()) {+ *counts.entry(parent).or_insert(0) += 1;+ }+ counts+ }+}++/// Why an object a teammate already imported is not copied.+const ALREADY_IN_MACRO: &str =+ "Already in Macro: a teammate imported it and shared it with the team.";++fn triage_item(node: &Node, key: &str) -> TriageItem {+ TriageItem {+ key: key.to_string(),+ kind: node.kind,+ title: node.title.clone(),+ path: Vec::new(),+ children: 0,+ table: node.table.clone(),+ }+}++fn data_source_node(summary: &DataSourceSummary) -> Node {+ let source = &summary.source;+ let columns = source+ .properties+ .iter()+ .map(|property| format!("{} ({})", property.name, kind_name(&property.kind)))+ .collect();+ Node {+ id: source.id.clone(),+ kind: ImportKind::DataSource,+ title: titled(&source.title, UNTITLED_DATABASE),+ url: source.database_id.url(),+ parent: parent_page(&source.database_parent),+ table: Some(TriageTable {+ columns,+ rows: summary.row_count,+ sample_titles: summary.sample_titles.clone(),+ }),+ }+}++/// The page an object sits directly under, when it sits under one.+fn parent_page(parent: &NotionParent) -> Option<NotionId> {+ match parent {+ NotionParent::Page { id } => Some(id.clone()),+ NotionParent::Workspace+ | NotionParent::Database { .. }+ | NotionParent::DataSource { .. }+ | NotionParent::Block { .. }+ | NotionParent::Other => None,+ }+}++fn titled(title: &str, fallback: &str) -> String {+ let trimmed = title.trim();+ if trimmed.is_empty() {+ fallback.to_string()+ } else {+ trimmed.to_string()+ }+}++fn kind_name(kind: &NotionPropertyKind) -> &str {+ match kind {+ NotionPropertyKind::Title => "title",+ NotionPropertyKind::RichText => "text",+ NotionPropertyKind::Number => "number",+ NotionPropertyKind::Select { .. } => "select",+ NotionPropertyKind::MultiSelect { .. } => "multi_select",+ NotionPropertyKind::Status { .. } => "status",+ NotionPropertyKind::Date => "date",+ NotionPropertyKind::People => "people",+ NotionPropertyKind::Files => "files",+ NotionPropertyKind::Checkbox => "checkbox",+ NotionPropertyKind::Url => "url",+ NotionPropertyKind::Email => "email",+ NotionPropertyKind::PhoneNumber => "phone",+ NotionPropertyKind::Formula => "formula",+ NotionPropertyKind::Relation { .. } => "relation",+ NotionPropertyKind::Rollup => "rollup",+ NotionPropertyKind::CreatedTime => "created_time",+ NotionPropertyKind::CreatedBy => "created_by",+ NotionPropertyKind::LastEditedTime => "last_edited_time",+ NotionPropertyKind::LastEditedBy => "last_edited_by",+ NotionPropertyKind::UniqueId { .. } => "unique_id",+ NotionPropertyKind::Unsupported { notion_type } => notion_type,+ }+}++/// Nodes reordered so every parent comes before its children. A cycle+/// (which Notion should never report) leaves its members as roots.+fn breadth_first(nodes: Vec<Node>) -> Vec<Node> {+ let mut children: HashMap<NotionId, Vec<usize>> = HashMap::new();+ let mut roots = VecDeque::new();+ for (position, node) in nodes.iter().enumerate() {+ match &node.parent {+ Some(parent) => children.entry(parent.clone()).or_default().push(position),+ None => roots.push_back(position),+ }+ }+ let mut ordered = Vec::with_capacity(nodes.len());+ let mut visited = vec![false; nodes.len()];+ let mut queue = roots;+ loop {+ while let Some(position) = queue.pop_front() {+ if std::mem::replace(&mut visited[position], true) {+ continue;+ }+ ordered.push(position);+ if let Some(below) = children.get(&nodes[position].id) {+ queue.extend(below.iter().copied());+ }+ }+ // Anything unvisited is in a cycle: start again from one of them.+ match visited.iter().position(|seen| !seen) {+ Some(position) => queue.push_back(position),+ None => break,+ }+ }+ let mut slots: Vec<Option<Node>> = nodes.into_iter().map(Some).collect();+ let mut emitted: HashSet<NotionId> = HashSet::with_capacity(slots.len());+ ordered+ .into_iter()+ .filter_map(|position| {+ let mut node = slots[position].take()?;+ // A cycle member emitted before its parent becomes a root.+ if node+ .parent+ .as_ref()+ .is_some_and(|parent| !emitted.contains(parent))+ {+ node.parent = None;+ }+ emitted.insert(node.id.clone());+ Some(node)+ })+ .collect()+}diff --git a/crates/import/src/domain/service/notion_workspace/plan/test.rs b/crates/import/src/domain/service/notion_workspace/plan/test.rsnew file mode 100644index 0000000000..267dec7afe--- /dev/null+++ b/crates/import/src/domain/service/notion_workspace/plan/test.rs@@ -0,0 +1,221 @@+use chrono::{TimeZone, Utc};+use import_notion::domain::model::{NotionDataSource, NotionPage, NotionPropertySchema};++use super::*;++fn id(hex: char) -> NotionId {+ NotionId::parse(&hex.to_string().repeat(32)).unwrap()+}++fn page(hex: char, title: &str, parent: NotionParent) -> NotionPage {+ NotionPage {+ id: id(hex),+ title: title.to_string(),+ url: id(hex).url(),+ parent,+ last_edited: Utc.with_ymd_and_hms(2026, 10, 1, 9, 0, 0).unwrap(),+ created: Utc.with_ymd_and_hms(2026, 9, 1, 9, 0, 0).unwrap(),+ in_trash: false,+ properties: Default::default(),+ }+}++#[test]+fn the_tree_puts_parents_first_and_plans_every_object_under_its_page() {+ let inventory = Inventory {+ pages: vec![+ page('b', "Q3 plan", NotionParent::Page { id: id('a') }),+ page('a', "Engineering", NotionParent::Workspace),+ page('c', "", NotionParent::Page { id: id('b') }),+ // Its parent was not shared with the connection, so it is a root.+ page('d', "Orphan", NotionParent::Page { id: id('9') }),+ ],+ data_sources: vec![DataSourceSummary {+ source: NotionDataSource {+ id: id('e'),+ database_id: id('f'),+ title: "Bugs".to_string(),+ database_parent: NotionParent::Page { id: id('a') },+ in_trash: false,+ properties: vec![+ NotionPropertySchema {+ id: "title".to_string(),+ name: "Name".to_string(),+ kind: NotionPropertyKind::Title,+ },+ NotionPropertySchema {+ id: "stat".to_string(),+ name: "Status".to_string(),+ kind: NotionPropertyKind::Status {+ options: Vec::new(),+ },+ },+ ],+ },+ row_count: 12,+ sample_titles: vec!["Fix login".to_string()],+ }],+ };++ let tree = PlanTree::new(&inventory);++ let order: Vec<&str> = tree.nodes.iter().map(|node| node.title.as_str()).collect();+ assert_eq!(+ order,+ ["Engineering", "Orphan", "Q3 plan", "Bugs", UNTITLED_PAGE]+ );++ let items = tree.triage_items(&HashMap::new());+ let engineering = items+ .iter()+ .find(|item| item.title == "Engineering")+ .unwrap();+ assert_eq!(engineering.children, 2);+ let untitled = items+ .iter()+ .find(|item| item.title == UNTITLED_PAGE)+ .unwrap();+ assert_eq!(untitled.path, ["Engineering", "Q3 plan"]);+ let bugs = items.iter().find(|item| item.title == "Bugs").unwrap();+ assert_eq!(+ bugs.table.as_ref().map(|table| table.columns.clone()),+ Some(vec![+ "Name (title)".to_string(),+ "Status (status)".to_string()+ ])+ );++ let verdicts: HashMap<String, Verdict> = [(+ id('e').to_string(),+ Verdict {+ key: id('e').to_string(),+ decision: NotionDecision::Tasks,+ reason: Some("A bug tracker".to_string()),+ task_fields: Some(crate::domain::models::NotionTaskFields {+ status: Some("Status".to_string()),+ ..Default::default()+ }),+ shared: None,+ shared_reason: None,+ },+ )]+ .into_iter()+ .collect();+ let staged = tree.staged_rows(NotionSharing::Personal, true, &HashMap::new());+ assert_eq!(staged.len(), 5);+ let waiting: Vec<Option<NotionDecision>> = staged+ .iter()+ .map(|row| {+ serde_json::from_value::<NotionDocMeta>(row.metadata.clone())+ .unwrap()+ .plan+ .unwrap()+ .decision+ })+ .collect();+ assert_eq!(waiting, [None, None, None, None, None]);++ let rows = tree.decided_rows(&verdicts, NotionSharing::Personal, true);++ assert_eq!(rows.len(), 1);+ let bugs_row = rows+ .iter()+ .find(|row| row.foreign_id == id('e').to_string())+ .unwrap();+ assert_eq!(bugs_row.kind, ImportKind::DataSource);+ assert_eq!(bugs_row.parent_foreign_id, Some(id('a').to_string()));+ let meta: NotionDocMeta = serde_json::from_value(bugs_row.metadata.clone()).unwrap();+ let plan = meta.plan.unwrap();+ assert_eq!(plan.decision, Some(NotionDecision::Tasks));+ assert_eq!(plan.rows, Some(12));+ assert_eq!(plan.sample_titles, ["Fix login"]);+ assert_eq!(+ plan.task_fields.and_then(|fields| fields.status).as_deref(),+ Some("Status")+ );+ assert_eq!(plan.path, ["Engineering"]);+ assert_eq!(meta.url.as_deref(), Some(id('f').url().as_str()));+}++#[test]+fn a_parent_cycle_cannot_hide_its_members() {+ let inventory = Inventory {+ pages: vec![+ page('a', "One", NotionParent::Page { id: id('b') }),+ page('b', "Two", NotionParent::Page { id: id('a') }),+ ],+ data_sources: Vec::new(),+ };++ let tree = PlanTree::new(&inventory);++ assert_eq!(tree.nodes.len(), 2);+ assert_eq!(tree.nodes[0].parent, None);+ assert_eq!(tree.nodes[1].parent, Some(tree.nodes[0].id.clone()));+}++#[test]+fn objects_a_teammate_already_imported_skip_the_model_and_link_to_their_copy() {+ let inventory = Inventory {+ pages: vec![+ page('a', "Engineering", NotionParent::Workspace),+ page('b', "Handbook", NotionParent::Page { id: id('a') }),+ ],+ data_sources: Vec::new(),+ };+ let tree = PlanTree::new(&inventory);+ let copy = NotionAlreadyImported {+ by: "[email protected]".to_string(),+ entity_id: "doc-handbook".to_string(),+ entity_type: "md".to_string(),+ };+ let already: HashMap<String, NotionAlreadyImported> =+ [(id('b').to_string(), copy.clone())].into_iter().collect();++ let items = tree.triage_items(&already);+ assert_eq!(+ items+ .iter()+ .map(|item| item.title.as_str())+ .collect::<Vec<_>>(),+ ["Engineering"],+ "the decision model never sees the Handbook"+ );+ let engineering = Verdict {+ key: id('a').to_string(),+ decision: NotionDecision::Copy,+ reason: None,+ task_fields: None,+ shared: Some(true),+ shared_reason: Some("The team's space".to_string()),+ };+ let verdicts: HashMap<String, Verdict> =+ [(id('a').to_string(), engineering)].into_iter().collect();++ let staged = tree.staged_rows(NotionSharing::Ai, true, &already);+ let decided = tree.decided_rows(&verdicts, NotionSharing::Ai, true);++ let plans: Vec<NotionPlanItem> = [&decided[0], &staged[1]]+ .into_iter()+ .map(|row| {+ serde_json::from_value::<NotionDocMeta>(row.metadata.clone())+ .unwrap()+ .plan+ .unwrap()+ })+ .collect();+ assert!(plans[0].shared);+ assert_eq!(plans[0].shared_reason.as_deref(), Some("The team's space"));+ assert_eq!(plans[0].already_imported, None);+ assert_eq!(plans[1].already_imported, Some(copy));+ assert!(+ !plans[1].shareable,+ "a teammate's copy is not the user's to share"+ );+ assert_eq!(plans[1].reason.as_deref(), Some(ALREADY_IN_MACRO));+ assert_eq!(+ plans[1].decision,+ Some(NotionDecision::Copy),+ "decided as soon as it is staged"+ );+}diff --git a/crates/import/src/domain/service/notion_workspace/report.rs b/crates/import/src/domain/service/notion_workspace/report.rsnew file mode 100644index 0000000000..8218adefe6--- /dev/null+++ b/crates/import/src/domain/service/notion_workspace/report.rs@@ -0,0 +1,144 @@+//! The report on a workspace import: what the current plan holds and how far+//! writing it got.++use std::collections::HashMap;++use crate::domain::models::{+ ImportEntity, ImportKind, ImportRun, ImportStatus, NotionDecision, NotionDocMeta,+ NotionImportReport, NotionImporter, NotionReportItem, RunStatus,+};++#[cfg(test)]+mod test;++/// Failures and by-hand items a report lists, at most.+const REPORT_LIST_LIMIT: usize = 50;++/// Summarize the ledger for the current plan: the rows tied to the newest+/// review, plus staged rows not yet tied to one. Objects a teammate already+/// imported are counted apart: they link to that copy, so they are neither+/// copies nor progress.+pub(in crate::domain::service) fn report(+ run: Option<&ImportRun>,+ rows: &[ImportEntity],+) -> NotionImportReport {+ let planned: Vec<(&ImportEntity, NotionDocMeta)> = rows+ .iter()+ .filter_map(|row| {+ let meta: NotionDocMeta = serde_json::from_value(row.metadata.clone()).ok()?;+ meta.plan.as_ref()?;+ Some((row, meta))+ })+ .collect();+ // A plan still being made is presented by the review its run records;+ // rows left from earlier plans belong to earlier reviews.+ let review_id = match run {+ Some(run) if run.status == RunStatus::Running => run.review_id,+ _ => planned+ .iter()+ .filter(|(row, _)| row.review_id.is_some())+ .max_by_key(|(row, _)| row.updated_at)+ .and_then(|(row, _)| row.review_id),+ };+ let current: Vec<&(&ImportEntity, NotionDocMeta)> = planned+ .iter()+ .filter(|(row, _)| match review_id {+ Some(review) => {+ row.review_id == Some(review)+ || (row.review_id.is_none() && row.status == ImportStatus::Staged)+ }+ None => true,+ })+ .collect();++ let mut report = NotionImportReport {+ status: run.map(|run| run.status),+ error: run.and_then(|run| run.error.clone()),+ scanned: run+ .filter(|run| run.status == RunStatus::Running)+ .and_then(|run| run.scanned),+ review_id,+ ..NotionImportReport::default()+ };+ let mut importers: HashMap<&str, u32> = HashMap::new();+ for (row, meta) in ¤t {+ let Some(plan) = &meta.plan else { continue };+ match row.kind {+ ImportKind::DataSource => report.data_sources += 1,+ _ => report.pages += 1,+ }+ if let Some(copy) = &plan.already_imported {+ report.already_in_macro += 1;+ *importers.entry(copy.by.as_str()).or_insert(0) += 1;+ continue;+ }+ let Some(decision) = plan.decision else {+ report.deciding += 1;+ continue;+ };+ let rows = plan.rows.unwrap_or(0);+ match (decision, row.kind) {+ (NotionDecision::Copy, ImportKind::DataSource) => {+ report.copy += 1;+ report.databases += 1;+ report.database_rows += rows;+ }+ (NotionDecision::Copy, _) => {+ report.copy += 1;+ report.documents += 1;+ }+ (NotionDecision::Tasks, _) => {+ report.tasks += 1;+ report.task_rows += rows;+ }+ (NotionDecision::Skip, _) => report.skip += 1,+ (NotionDecision::Hand, _) => report.hand += 1,+ }+ let written = matches!(decision, NotionDecision::Copy | NotionDecision::Tasks);+ if written && plan.shared && row.status != ImportStatus::Discarded {+ report.shared_with_team += 1;+ }+ let reported = || NotionReportItem {+ import_id: row.id,+ title: meta.title.clone(),+ kind: row.kind,+ url: meta.url.clone(),+ reason: plan.reason.clone(),+ };+ match row.status {+ ImportStatus::Imported => report.imported += 1,+ ImportStatus::Importing if row.waiting_on.is_some() => {+ report.waiting_on_teammates += 1;+ }+ ImportStatus::Importing => report.writing += 1,+ ImportStatus::Staged => {+ if decision == NotionDecision::Hand {+ if report.hand_items.len() < REPORT_LIST_LIMIT {+ report.hand_items.push(reported());+ }+ } else if decision != NotionDecision::Skip {+ report.waiting += 1;+ }+ if let Some(error) = &row.last_error+ && report.failures.len() < REPORT_LIST_LIMIT+ {+ report.failures.push(NotionReportItem {+ reason: Some(error.clone()),+ ..reported()+ });+ }+ }+ ImportStatus::Discarded => {}+ }+ }+ let mut importers: Vec<NotionImporter> = importers+ .into_iter()+ .map(|(by, count)| NotionImporter {+ by: by.to_string(),+ count,+ })+ .collect();+ importers.sort_by(|a, b| b.count.cmp(&a.count).then_with(|| a.by.cmp(&b.by)));+ report.already_imported_by = importers;+ report+}diff --git a/crates/import/src/domain/service/notion_workspace/report/test.rs b/crates/import/src/domain/service/notion_workspace/report/test.rsnew file mode 100644index 0000000000..8fed5f7043--- /dev/null+++ b/crates/import/src/domain/service/notion_workspace/report/test.rs@@ -0,0 +1,301 @@+use chrono::{TimeZone, Utc};++use uuid::Uuid;++use super::*;+use crate::domain::models::{+ ImportSource, Initiator, NotionAlreadyImported, NotionPlanItem, ScanCounts,+};++fn row(+ title: &str,+ kind: ImportKind,+ status: ImportStatus,+ decision: NotionDecision,+ review: Option<Uuid>,+ last_error: Option<&str>,+) -> ImportEntity {+ let plan = NotionPlanItem {+ decision: Some(decision),+ reason: (decision == NotionDecision::Hand).then(|| "Mixed records".to_string()),+ path: Vec::new(),+ rows: (kind == ImportKind::DataSource).then_some(10),+ sample_titles: Vec::new(),+ task_fields: None,+ shared: false,+ shared_reason: None,+ shareable: true,+ already_imported: None,+ };+ ImportEntity {+ id: Uuid::now_v7(),+ user_id: "macro|[email protected]".to_string(),+ team_id: None,+ source: ImportSource::Notion,+ kind,+ foreign_id: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa".to_string(),+ parent_id: None,+ review_id: review,+ status,+ initiator: Initiator::Chat,+ metadata: serde_json::to_value(NotionDocMeta::for_plan(+ title.to_string(),+ "https://www.notion.so/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa".to_string(),+ plan,+ ))+ .unwrap(),+ entity_id: (status == ImportStatus::Imported).then(|| "doc-1".to_string()),+ entity_type: (status == ImportStatus::Imported).then(|| "md".to_string()),+ last_error: last_error.map(str::to_string),+ waiting_on: None,+ created_at: Utc.with_ymd_and_hms(2026, 10, 19, 12, 0, 0).unwrap(),+ updated_at: Utc.with_ymd_and_hms(2026, 10, 19, 12, 0, 0).unwrap(),+ }+}++#[test]+fn the_report_counts_the_current_plan_and_lists_what_needs_attention() {+ let review = Uuid::now_v7();+ let older_review = Uuid::now_v7();+ let mut older = row(+ "Old import",+ ImportKind::Page,+ ImportStatus::Imported,+ NotionDecision::Copy,+ Some(older_review),+ None,+ );+ older.updated_at = Utc.with_ymd_and_hms(2026, 10, 1, 12, 0, 0).unwrap();+ let rows = vec![+ older,+ row(+ "Roadmap",+ ImportKind::Page,+ ImportStatus::Imported,+ NotionDecision::Copy,+ Some(review),+ None,+ ),+ row(+ "Bugs",+ ImportKind::DataSource,+ ImportStatus::Importing,+ NotionDecision::Tasks,+ Some(review),+ None,+ ),+ row(+ "Specs",+ ImportKind::Page,+ ImportStatus::Staged,+ NotionDecision::Copy,+ Some(review),+ Some("Notion request failed"),+ ),+ row(+ "Untitled",+ ImportKind::Page,+ ImportStatus::Staged,+ NotionDecision::Skip,+ Some(review),+ None,+ ),+ row(+ "CRM",+ ImportKind::DataSource,+ ImportStatus::Staged,+ NotionDecision::Hand,+ Some(review),+ None,+ ),+ ];+ let run = ImportRun {+ source: ImportSource::Notion,+ status: RunStatus::Importing,+ error: None,+ updated_at: Utc.with_ymd_and_hms(2026, 10, 19, 12, 0, 0).unwrap(),+ scanned: None,+ review_id: None,+ };++ let report = report(Some(&run), &rows);++ assert_eq!(report.status, Some(RunStatus::Importing));+ assert_eq!(report.review_id, Some(review));+ assert_eq!((report.pages, report.data_sources), (3, 2));+ assert_eq!(+ (report.copy, report.tasks, report.skip, report.hand),+ (2, 1, 1, 1)+ );+ assert_eq!((report.imported, report.writing, report.waiting), (1, 1, 1));+ // Exact numbers: Bugs (tasks, 10 rows); CRM is by hand, so no database+ // is copied.+ assert_eq!(+ (+ report.documents,+ report.databases,+ report.database_rows,+ report.task_rows+ ),+ (2, 0, 0, 10)+ );+ assert_eq!(report.hand_items.len(), 1);+ assert_eq!(report.hand_items[0].title, "CRM");+ assert_eq!(+ report.hand_items[0].reason.as_deref(),+ Some("Mixed records")+ );+ assert_eq!(report.failures.len(), 1);+ assert_eq!(report.failures[0].title, "Specs");+ assert_eq!(+ report.failures[0].reason.as_deref(),+ Some("Notion request failed")+ );+}++#[test]+fn before_any_plan_the_report_is_empty() {+ assert_eq!(report(None, &[]), NotionImportReport::default());+}++#[test]+fn while_planning_the_report_carries_what_notion_has_shown_so_far() {+ let mut run = ImportRun {+ source: ImportSource::Notion,+ status: RunStatus::Running,+ error: None,+ updated_at: Utc.with_ymd_and_hms(2026, 10, 19, 12, 0, 0).unwrap(),+ scanned: Some(ScanCounts {+ pages: 40,+ data_sources: 12,+ }),+ review_id: None,+ };++ assert_eq!(+ report(Some(&run), &[]).scanned,+ Some(ScanCounts {+ pages: 40,+ data_sources: 12,+ })+ );++ run.status = RunStatus::Ready;+ assert_eq!(report(Some(&run), &[]).scanned, None);+}++#[test]+fn objects_already_in_macro_are_counted_apart_with_who_imported_them() {+ let review = Uuid::now_v7();+ let mut handbook = row(+ "Handbook",+ ImportKind::Page,+ ImportStatus::Staged,+ NotionDecision::Copy,+ Some(review),+ None,+ );+ let mut meta: NotionDocMeta = serde_json::from_value(handbook.metadata.clone()).unwrap();+ if let Some(plan) = meta.plan.as_mut() {+ plan.already_imported = Some(NotionAlreadyImported {+ by: "[email protected]".to_string(),+ entity_id: "doc-handbook".to_string(),+ entity_type: "md".to_string(),+ });+ }+ handbook.metadata = serde_json::to_value(meta).unwrap();+ let mut wiki = row(+ "Wiki",+ ImportKind::Page,+ ImportStatus::Importing,+ NotionDecision::Copy,+ Some(review),+ None,+ );+ let mut meta: NotionDocMeta = serde_json::from_value(wiki.metadata.clone()).unwrap();+ if let Some(plan) = meta.plan.as_mut() {+ plan.shared = true;+ }+ wiki.metadata = serde_json::to_value(meta).unwrap();+ wiki.waiting_on = Some(Uuid::now_v7());+ let notes = row(+ "Notes",+ ImportKind::Page,+ ImportStatus::Staged,+ NotionDecision::Copy,+ Some(review),+ None,+ );++ let report = report(None, &[handbook, wiki, notes]);++ assert_eq!(report.pages, 3, "Notion shared all three");+ assert_eq!(report.already_in_macro, 1);+ assert_eq!(+ report.already_imported_by,+ [NotionImporter {+ by: "[email protected]".to_string(),+ count: 1+ }]+ );+ assert_eq!(+ (report.copy, report.documents),+ (2, 2),+ "the Handbook is not copied"+ );+ assert_eq!(report.shared_with_team, 1);+ assert_eq!(report.waiting_on_teammates, 1);+ assert_eq!((report.writing, report.waiting), (0, 1));+}++#[test]+fn a_plan_presented_while_it_is_decided_counts_what_is_undecided_under_the_runs_review() {+ let earlier_review = Uuid::now_v7();+ let review = Uuid::now_v7();+ // Imported by an earlier plan and review.+ let imported = row(+ "Old notes",+ ImportKind::Page,+ ImportStatus::Imported,+ NotionDecision::Copy,+ Some(earlier_review),+ None,+ );+ let decided = row(+ "Handbook",+ ImportKind::Page,+ ImportStatus::Staged,+ NotionDecision::Copy,+ Some(review),+ None,+ );+ let mut undecided = row(+ "Roadmap",+ ImportKind::DataSource,+ ImportStatus::Staged,+ NotionDecision::Copy,+ Some(review),+ None,+ );+ let mut meta: NotionDocMeta = serde_json::from_value(undecided.metadata.clone()).unwrap();+ if let Some(plan) = meta.plan.as_mut() {+ plan.decision = None;+ }+ undecided.metadata = serde_json::to_value(meta).unwrap();+ let run = ImportRun {+ source: ImportSource::Notion,+ status: RunStatus::Running,+ error: None,+ updated_at: Utc.with_ymd_and_hms(2026, 10, 19, 12, 0, 0).unwrap(),+ scanned: None,+ review_id: Some(review),+ };++ let report = report(Some(&run), &[imported, decided, undecided]);++ assert_eq!(report.review_id, Some(review));+ assert_eq!((report.pages, report.data_sources), (1, 1));+ assert_eq!(report.deciding, 1);+ assert_eq!((report.copy, report.documents, report.databases), (1, 1, 0));+ assert_eq!((report.imported, report.waiting), (0, 1));+}diff --git a/crates/import/src/domain/service/notion_workspace/run.rs b/crates/import/src/domain/service/notion_workspace/run.rsnew file mode 100644index 0000000000..dcac96824a--- /dev/null+++ b/crates/import/src/domain/service/notion_workspace/run.rs@@ -0,0 +1,360 @@+//! Keeping a Notion import alive across processes.+//!+//! The process running a plan or a write holds a lease on the user's run and+//! renews it with the heartbeat. A job stops early when the user cancels,+//! when its process shuts down (it then gives the lease up so another+//! process adopts the run at once), or when another process took the run+//! over (its lease lapsed). Runs whose lease lapsed are adopted by the next+//! report read for that user, or by the host's sweeper, and resumed:+//! planning restarts, writing continues from the ledger.++use std::sync::{Arc, OnceLock};+use std::time::Duration;++use futures::FutureExt;+use import_notion::domain::ports::NotionConnections;+use macro_user_id::user_id::MacroUserIdStr;+use mcp_select::ConnectorSelect;+use tokio_util::sync::CancellationToken;+use tracing::Instrument;+use uuid::Uuid;++use super::super::{AbortOnDrop, IMPORT_HEARTBEAT, ImportServiceImpl};+use super::{CANCEL_CHECK_EVERY, write};+use crate::domain::models::{+ AdoptedRun, ImportSource, ImportStatus, NotionDecision, NotionJob, RunStatus,+};+use crate::domain::ports::{+ CanonicalImportRepo, EntityCreator, ImportRepo, NotionPlanRepo, NotionRunRepo, Result,+ SlackWorkspaceSource, WorkspaceWriter, WrittenObjects,+};++#[cfg(test)]+mod test;++/// How long a run stays held without a heartbeat: three missed beats.+pub(super) const LEASE: Duration = Duration::from_secs(IMPORT_HEARTBEAT.as_secs() * 3);+/// How long a run nobody leased (started by code that predates leases) may+/// go untouched before it is adopted.+const STALE_UNLEASED: Duration = Duration::from_secs(LEASE.as_secs() * 3);+/// How often the sweeper looks for runs to adopt.+const SWEEP_EVERY: Duration = Duration::from_secs(60);+/// Runs one sweep adopts at most.+const ADOPT_PER_SWEEP: u16 = 10;+/// Adoptions after which a run is given up on: a job that keeps stopping its+/// process would otherwise restart forever.+const MAX_ADOPTIONS: u16 = 5;+/// What a run given up on, or a write row left behind, says.+pub(super) const IMPORT_STOPPED: &str =+ "The Notion import stopped before it finished. Start it again to pick up what is left.";++/// Why a job stopped before finishing.+#[derive(Debug, Clone, Copy, PartialEq, Eq, strum::AsRefStr)]+#[strum(serialize_all = "snake_case")]+pub(super) enum Stop {+ /// The user cancelled the import.+ Cancelled,+ /// This process is shutting down; the run was handed over.+ Shutdown,+ /// Another process holds the run now.+ LeaseLost,+}++/// A running job's view of its run: whether, and why, it must stop.+pub(super) struct JobControl {+ reason: OnceLock<Stop>,+ shutdown: CancellationToken,+ stopped: CancellationToken,+ /// Set together with `stopped`, for the write's checkpoints.+ pub cancel: Arc<write::Cancel>,+}++impl JobControl {+ fn new(shutdown: CancellationToken) -> Self {+ Self {+ reason: OnceLock::new(),+ stopped: shutdown.child_token(),+ shutdown,+ cancel: Arc::new(write::Cancel::default()),+ }+ }++ fn stop(&self, reason: Stop) {+ if self.reason.set(reason).is_ok() {+ tracing::info!(reason = reason.as_ref(), "notion import job stopping");+ self.cancel.stop();+ self.stopped.cancel();+ }+ }++ /// Why the job must stop, once it must. Shutdown counts from the moment+ /// it is signalled, before the watcher notices.+ pub fn reason(&self) -> Option<Stop> {+ self.reason+ .get()+ .copied()+ .or_else(|| self.shutdown.is_cancelled().then_some(Stop::Shutdown))+ }++ /// Whether the job stopped without the user asking: what it was doing+ /// belongs to the next process, so it leaves the run and rows alone.+ pub fn handed_over(&self) -> bool {+ matches!(self.reason(), Some(Stop::Shutdown | Stop::LeaseLost))+ }++ /// Resolves once the job must stop.+ pub async fn stopped(&self) {+ self.stopped.cancelled().await;+ }+}++/// A Notion write to resume or start: every object of the plan, for the+/// folder tree and links, and those still to write.+#[derive(Debug, Clone, Default)]+pub(super) struct WriteSet {+ /// The review that approved the plan.+ pub review_id: Uuid,+ /// Every planned object being written, done or not.+ pub all: Vec<write::WriteItem>,+ /// The objects still to write.+ pub pending: Vec<write::WriteItem>,+}++impl<R, S, C, W, N> ImportServiceImpl<R, S, C, W, N>+where+ R: ImportRepo + CanonicalImportRepo + NotionPlanRepo + NotionRunRepo + WrittenObjects + Clone,+ S: ConnectorSelect,+ C: EntityCreator + WorkspaceWriter,+ W: SlackWorkspaceSource,+ N: NotionConnections,+{+ /// Hold the user's run for this process, recording `job` on it.+ pub(super) async fn take_run(+ &self,+ user: &MacroUserIdStr<'static>,+ job: &NotionJob,+ ) -> Result<bool> {+ self.repo+ .take_run(user, self.jobs.instance, LEASE, job)+ .await+ }++ /// Watch the held run for as long as the returned guard lives: renew the+ /// lease, notice a cancel within seconds, and stop on shutdown.+ pub(super) fn hold_run(+ &self,+ user: &MacroUserIdStr<'static>,+ ) -> (Arc<JobControl>, AbortOnDrop) {+ let control = Arc::new(JobControl::new(self.jobs.shutdown.clone()));+ let service = self.clone();+ let user = user.clone();+ let watched = Arc::clone(&control);+ let shutdown = self.jobs.shutdown.clone();+ let watch = self.jobs.tracker.spawn(+ async move {+ let start = tokio::time::Instant::now();+ let mut beat = tokio::time::interval_at(start + IMPORT_HEARTBEAT, IMPORT_HEARTBEAT);+ let mut check =+ tokio::time::interval_at(start + CANCEL_CHECK_EVERY, CANCEL_CHECK_EVERY);+ loop {+ tokio::select! {+ () = shutdown.cancelled() => {+ watched.stop(Stop::Shutdown);+ return;+ }+ _ = check.tick() => {+ if service.cancelled(&user).await {+ watched.stop(Stop::Cancelled);+ return;+ }+ }+ _ = beat.tick() => {+ match service.repo.renew_lease(&user, service.jobs.instance, LEASE).await {+ Ok(true) => {}+ Ok(false) => {+ let reason = if service.cancelled(&user).await {+ Stop::Cancelled+ } else {+ tracing::warn!("notion import lease was taken over");+ Stop::LeaseLost+ };+ watched.stop(reason);+ return;+ }+ Err(error) => {+ tracing::warn!(error = ?error, "notion import lease was not renewed");+ }+ }+ }+ }+ }+ }+ .in_current_span(),+ );+ (control, AbortOnDrop(watch))+ }++ /// Give the run up so another process adopts it straight away.+ pub(super) async fn release_run(&self, user: &MacroUserIdStr<'static>) {+ match self.repo.release_lease(user, self.jobs.instance).await {+ Ok(released) => tracing::info!(released, "notion import handed over"),+ Err(error) => tracing::warn!(error = ?error, "notion import lease was not released"),+ }+ }++ /// Adopt and resume the stalled Notion runs, only `user`'s when given.+ /// Best-effort: a failure is logged and adopts nothing. Returns how many+ /// runs were adopted.+ pub(super) async fn adopt_stalled(&self, user: Option<&MacroUserIdStr<'static>>) -> usize {+ let limit = if user.is_some() { 1 } else { ADOPT_PER_SWEEP };+ let adopted = match self+ .repo+ .adopt_stalled_runs(user, self.jobs.instance, LEASE, STALE_UNLEASED, limit)+ .await+ {+ Ok(adopted) => adopted,+ Err(error) => {+ tracing::warn!(error = ?error, "could not adopt stalled notion imports");+ return 0;+ }+ };+ let count = adopted.len();+ for run in adopted {+ self.resume(run).await;+ }+ count+ }++ /// Adopt stalled Notion imports every [`SWEEP_EVERY`] until `shutdown`,+ /// so an import whose process died finishes even if nobody looks at it.+ pub async fn sweep_stalled_notion_imports(&self, shutdown: CancellationToken) {+ let mut sweep = tokio::time::interval(SWEEP_EVERY);+ loop {+ tokio::select! {+ () = shutdown.cancelled() => return,+ _ = sweep.tick() => {+ let span = tracing::info_span!("notion_import.sweep", adopted = tracing::field::Empty);+ let adopted = self.adopt_stalled(None).instrument(span.clone()).await;+ span.record("adopted", adopted);+ }+ }+ }+ }++ /// Resume an adopted run in the background, or give it up when it has no+ /// job to resume or kept stopping. Boxed: giving up follows up on the+ /// review, which reads the report, which adopts.+ fn resume(&self, run: AdoptedRun) -> futures::future::BoxFuture<'_, ()> {+ let job = match &run.job {+ Some(NotionJob::Plan { .. }) => "plan",+ Some(NotionJob::Write { .. }) => "write",+ None => "none",+ };+ let span = tracing::info_span!(+ "notion_import.adopt",+ user = %run.user,+ status = run.status.as_ref(),+ adoptions = run.adoptions,+ job,+ review_id = tracing::field::Empty,+ pending = tracing::field::Empty,+ done = tracing::field::Empty,+ );+ let user = run.user.clone();+ async move {+ match run.job {+ Some(NotionJob::Plan { scope, sharing }) if run.adoptions <= MAX_ADOPTIONS => {+ let service = self.clone();+ self.jobs.tracker.spawn(+ async move { service.plan_workspace(&user, scope, sharing).await }+ .in_current_span(),+ );+ }+ Some(NotionJob::Write { review_id }) if run.adoptions <= MAX_ADOPTIONS => {+ tracing::Span::current().record("review_id", tracing::field::display(review_id));+ match self.resumable_write(&user, review_id).await {+ Ok(set) => {+ let span = tracing::Span::current();+ span.record("pending", set.pending.len());+ span.record("done", set.all.len() - set.pending.len());+ let service = self.clone();+ self.jobs.tracker.spawn(+ async move { service.write_plan(&user, set).await }+ .in_current_span(),+ );+ }+ Err(error) => {+ tracing::warn!(error = ?error, "could not read the notion write to resume");+ self.give_up(&user, run.status).await;+ }+ }+ }+ _ => self.give_up(&user, run.status).await,+ }+ }+ .instrument(span)+ .boxed()+ }++ /// Fail a run that cannot be resumed and send its rows back to staged.+ /// A write given up on ends like any other, so its session hears of it.+ async fn give_up(&self, user: &MacroUserIdStr<'static>, status: RunStatus) {+ tracing::warn!("giving up on a stalled notion import");+ let failed = match status {+ RunStatus::Importing => {+ self.end_write(user, RunStatus::Failed, Some(IMPORT_STOPPED))+ .await+ }+ _ => {+ self.repo+ .transition_run(+ user,+ ImportSource::Notion,+ &[status],+ RunStatus::Failed,+ Some(IMPORT_STOPPED),+ )+ .await+ }+ };+ if let Err(error) = failed {+ tracing::warn!(error = ?error, "could not fail the stalled notion import");+ }+ let _ = self.repo.settle_importing(user, IMPORT_STOPPED).await;+ self.notify(user).await;+ }++ /// The write for `review_id` as the ledger has it: the plan's copied+ /// objects, and those still `importing`.+ async fn resumable_write(+ &self,+ user: &MacroUserIdStr<'static>,+ review_id: Uuid,+ ) -> Result<WriteSet> {+ let rows = self+ .repo+ .list(user, Some(ImportSource::Notion), None)+ .await?;+ let mut set = WriteSet {+ review_id,+ ..WriteSet::default()+ };+ for row in rows.iter().filter(|row| {+ row.review_id == Some(review_id)+ && row.user_id == user.as_ref()+ && row.status != ImportStatus::Discarded+ }) {+ let Some(item) = write::WriteItem::from_entity(row) else {+ continue;+ };+ if !matches!(item.decision, NotionDecision::Copy | NotionDecision::Tasks) {+ continue;+ }+ if row.status == ImportStatus::Importing {+ set.pending.push(item.clone());+ }+ set.all.push(item);+ }+ Ok(set)+ }+}diff --git a/crates/import/src/domain/service/notion_workspace/run/test.rs b/crates/import/src/domain/service/notion_workspace/run/test.rsnew file mode 100644index 0000000000..aa61394866--- /dev/null+++ b/crates/import/src/domain/service/notion_workspace/run/test.rs@@ -0,0 +1,1188 @@+//! A Notion write outliving its process, against the real ledger: handing+//! the run over on shutdown, and adopting and finishing a stalled write.++use std::sync::{Arc, Mutex};++use agent_runtime_protocol::domain::notification::{+ ImportOutcome, NotificationEvent, ReviewSettlement,+};+use agent_session::domain::model::AgentSessionId;+use agent_session::domain::notifications::{+ NotificationFuture, NotificationWriter, PendingNotification,+};+use import_notion::domain::model::{+ NotionDataSource, NotionId, NotionPage, NotionUser, PageMarkdown, Paged, SearchHit,+ SearchObject,+};+use import_notion::domain::ports::{NotionApi, NotionError};+use macro_db_migrator::MACRO_DB_MIGRATIONS;+use macro_user_id::cowlike::CowLike;+use sqlx::{PgConnection, PgPool};+use tokio::sync::Notify;+use tokio_util::task::TaskTracker;++use super::*;+use crate::domain::models::{+ ImportKind, ImportTargetReservation, NotionDocMeta, NotionPlanItem, NotionSharing,+ NotionWriteChoices,+};+use crate::domain::ports::{+ ImportedDocumentProperties, ImportedTaskProperties, NewDatabase, NotionLinks, PlanRow,+ SharedEntity, TeamMember,+};+use crate::domain::service::NotionWorkspaceImporter;+use crate::domain::service::test::admission::NoConnector;+use crate::outbound::pg_import_repo::PgImportRepo;++fn page_id(hex: char) -> NotionId {+ NotionId::parse(&hex.to_string().repeat(32)).unwrap()+}++/// A workspace of plain pages, each with a body.+struct Pages;++impl NotionApi for Pages {+ async fn search(+ &self,+ _: SearchObject,+ _: Option<String>,+ ) -> std::result::Result<Paged<SearchHit>, NotionError> {+ unimplemented!("writing never searches")+ }++ async fn page(&self, _: &NotionId) -> std::result::Result<NotionPage, NotionError> {+ Err(NotionError::NotFound)+ }++ async fn page_markdown(&self, id: &NotionId) -> std::result::Result<PageMarkdown, NotionError> {+ Ok(PageMarkdown {+ markdown: format!("Body of {id}"),+ unknown_block_ids: Vec::new(),+ })+ }++ async fn data_source(+ &self,+ _: &NotionId,+ ) -> std::result::Result<NotionDataSource, NotionError> {+ unimplemented!("only pages here")+ }++ async fn database_sources(+ &self,+ _: &NotionId,+ ) -> std::result::Result<Vec<NotionId>, NotionError> {+ unimplemented!("only pages here")+ }++ async fn query_data_source(+ &self,+ _: &NotionId,+ _: Option<String>,+ ) -> std::result::Result<Paged<NotionPage>, NotionError> {+ unimplemented!("only pages here")+ }++ async fn users(+ &self,+ _: Option<String>,+ ) -> std::result::Result<Paged<NotionUser>, NotionError> {+ Ok(Paged {+ results: Vec::new(),+ next_cursor: None,+ })+ }+}++struct Connected;++impl NotionConnections for Connected {+ type Api = Pages;++ async fn for_user(+ &self,+ _: &MacroUserIdStr<'static>,+ ) -> std::result::Result<Option<Pages>, NotionError> {+ Ok(Some(Pages))+ }+}++/// Records the documents and folders a write makes. With `hold` set, a+/// document write waits until the process is shutting down and then fails,+/// as a write cut off by a deploy does.+#[derive(Default)]+struct Writer {+ folders: Mutex<Vec<String>>,+ documents: Mutex<Vec<(Uuid, String, NotionLinks)>>,+ shares: Mutex<Vec<SharedEntity>>,+ hold: Option<(Arc<Notify>, tokio_util::sync::CancellationToken)>,+}++impl WorkspaceWriter for Writer {+ async fn create_folder(+ &self,+ _: &MacroUserIdStr<'static>,+ name: &str,+ _: Option<Uuid>,+ ) -> anyhow::Result<Uuid> {+ self.folders.lock().unwrap().push(name.to_string());+ Ok(Uuid::now_v7())+ }++ async fn folder_exists(&self, _: &MacroUserIdStr<'static>, _: Uuid) -> anyhow::Result<bool> {+ Ok(true)+ }++ async fn share_with_team(+ &self,+ _: &MacroUserIdStr<'static>,+ entity: SharedEntity,+ ) -> anyhow::Result<()> {+ self.shares.lock().unwrap().push(entity);+ Ok(())+ }++ async fn create_document(+ &self,+ _: &MacroUserIdStr<'static>,+ id: Uuid,+ name: &str,+ _: &str,+ links: &NotionLinks,+ _: &ImportedDocumentProperties,+ _: Option<Uuid>,+ ) -> anyhow::Result<String> {+ if let Some((entered, shutdown)) = &self.hold {+ entered.notify_one();+ shutdown.cancelled().await;+ anyhow::bail!("the process stopped");+ }+ self.documents+ .lock()+ .unwrap()+ .push((id, name.to_string(), links.clone()));+ Ok(id.to_string())+ }++ async fn create_task(+ &self,+ _: &MacroUserIdStr<'static>,+ _: Uuid,+ _: &str,+ _: &str,+ _: &NotionLinks,+ _: &ImportedTaskProperties,+ _: Option<Uuid>,+ ) -> anyhow::Result<String> {+ unimplemented!("only pages here")+ }++ async fn create_database(+ &self,+ _: &MacroUserIdStr<'static>,+ _: &str,+ ) -> anyhow::Result<NewDatabase> {+ unimplemented!("only pages here")+ }++ async fn apply_database_ops(+ &self,+ _: &MacroUserIdStr<'static>,+ _: Uuid,+ _: Vec<models_databases::DatabaseOp>,+ ) -> anyhow::Result<Vec<models_databases::OpResult>> {+ unimplemented!("only pages here")+ }++ async fn team_members(&self, _: &MacroUserIdStr<'static>) -> anyhow::Result<Vec<TeamMember>> {+ Ok(Vec::new())+ }+}++impl EntityCreator for Writer {+ async fn create_task(+ &self,+ _: &MacroUserIdStr<'static>,+ _: &str,+ _: &str,+ _: &ImportedTaskProperties,+ ) -> anyhow::Result<String> {+ unimplemented!("workspace imports write through WorkspaceWriter")+ }++ async fn create_notion_document(+ &self,+ _: &MacroUserIdStr<'static>,+ _: &str,+ _: &str,+ _: &NotionLinks,+ _: &ImportedDocumentProperties,+ ) -> anyhow::Result<String> {+ unimplemented!("workspace imports write through WorkspaceWriter")+ }++ async fn create_channel(+ &self,+ _: &MacroUserIdStr<'static>,+ _: &str,+ _: &ImportTargetReservation,+ _: &[String],+ ) -> anyhow::Result<Uuid> {+ unimplemented!("workspace imports write no channels")+ }+}++type Service = ImportServiceImpl<+ PgImportRepo,+ NoConnector,+ Writer,+ crate::domain::service::NoSlackSource,+ Connected,+>;++fn service(+ pool: &PgPool,+ writer: Writer,+ shutdown: tokio_util::sync::CancellationToken,+ tracker: TaskTracker,+) -> Service {+ ImportServiceImpl::new(+ PgImportRepo::new(pool.clone()),+ Arc::new(NoConnector),+ Arc::new(writer),+ Arc::new(ai_usage::NoOpUsageRecorder),+ )+ .with_notion_connections(Arc::new(Connected))+ .with_job_shutdown(shutdown, tracker)+}++fn user(label: &str) -> MacroUserIdStr<'static> {+ MacroUserIdStr::parse_from_str(&format!("macro|{label}@example.com"))+ .unwrap()+ .into_owned()+}++fn page(hex: char, title: &str) -> PlanRow {+ planned_page(hex, title, false)+}++/// A page planned as a document, shared with the team or not.+fn planned_page(hex: char, title: &str, shared: bool) -> PlanRow {+ let plan = NotionPlanItem {+ decision: Some(NotionDecision::Copy),+ reason: None,+ path: Vec::new(),+ rows: None,+ sample_titles: Vec::new(),+ task_fields: None,+ shared,+ shared_reason: None,+ shareable: true,+ already_imported: None,+ };+ PlanRow {+ kind: ImportKind::Page,+ foreign_id: page_id(hex).as_str().to_string(),+ parent_foreign_id: None,+ metadata: serde_json::to_value(NotionDocMeta::for_plan(+ title.to_string(),+ page_id(hex).url(),+ plan,+ ))+ .unwrap(),+ }+}++/// A user with an approved two-page plan.+async fn approved_plan(pool: &PgPool, owner: &MacroUserIdStr<'static>) -> (Uuid, Vec<Uuid>) {+ approved(pool, owner, &[page('a', "Roadmap"), page('b', "Spec")]).await+}++/// A user with an approved plan of `rows`.+async fn approved(+ pool: &PgPool,+ owner: &MacroUserIdStr<'static>,+ rows: &[PlanRow],+) -> (Uuid, Vec<Uuid>) {+ let repo = PgImportRepo::new(pool.clone());+ let staged = repo+ .replace_plan(owner, ImportSource::Notion, rows)+ .await+ .unwrap();+ let session = Uuid::now_v7();+ sqlx::query!(+ r#"INSERT INTO agent_session (id, owner_id, bot_id, harness, model, workspace, for_warming) VALUES ($1, $2, '00000000-0000-0000-0000-00000000a2a2', 'macro-inmem', 'test-model', '/workspace', false)"#,+ session,+ owner.as_ref(),+ )+ .execute(pool)+ .await+ .unwrap();+ let review = Uuid::now_v7();+ sqlx::query!(+ r#"INSERT INTO agent_session_review (id, agent_session_id, record) VALUES ($1, $2, '{}'::jsonb)"#,+ review,+ session,+ )+ .execute(pool)+ .await+ .unwrap();+ repo.attach_review(+ owner,+ ImportSource::Notion,+ review,+ AgentSessionId::new_from_uuid(session),+ )+ .await+ .unwrap();+ (review, staged.iter().map(|row| row.id).collect())+}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn a_write_cut_off_by_shutdown_hands_its_run_over(pool: PgPool) {+ let owner = user("handover");+ let (review, rows) = approved_plan(&pool, &owner).await;+ let shutdown = tokio_util::sync::CancellationToken::new();+ let tracker = TaskTracker::new();+ let entered = Arc::new(Notify::new());+ let writer = Writer {+ hold: Some((Arc::clone(&entered), shutdown.clone())),+ ..Writer::default()+ };+ let service = service(&pool, writer, shutdown.clone(), tracker.clone());+ let repo = PgImportRepo::new(pool.clone());+ // Planned: the run is ready.+ repo.start_run(&owner, ImportSource::Notion, &[])+ .await+ .unwrap();+ repo.transition_run(+ &owner,+ ImportSource::Notion,+ &[RunStatus::Running],+ RunStatus::Ready,+ None,+ )+ .await+ .unwrap();++ service+ .start_notion_write(&owner, review, NotionWriteChoices::default())+ .await+ .unwrap();+ entered.notified().await;+ shutdown.cancel();+ tracker.close();+ tracker.wait().await;++ let runs = repo.list_runs(&owner).await.unwrap();+ assert_eq!(runs[0].status, RunStatus::Importing, "the run is not over");+ for row in &rows {+ let row = repo.get(&owner, *row).await.unwrap().unwrap();+ assert_eq!(+ row.status,+ ImportStatus::Importing,+ "left for the next process"+ );+ assert_eq!(row.last_error, None);+ }+ let adopted = repo+ .adopt_stalled_runs(Some(&owner), Uuid::now_v7(), LEASE, STALE_UNLEASED, 1)+ .await+ .unwrap();+ assert_eq!(+ adopted.len(),+ 1,+ "the lease was given up, so it is adopted at once"+ );+ assert_eq!(adopted[0].job, Some(NotionJob::Write { review_id: review }));+}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn a_stalled_write_is_adopted_on_read_and_finishes_without_duplicates(pool: PgPool) {+ let owner = user("adopt");+ let (review, rows) = approved_plan(&pool, &owner).await;+ let repo = PgImportRepo::new(pool.clone());+ // The first process wrote Roadmap and died while writing Spec; its+ // lease has lapsed.+ repo.start_run(&owner, ImportSource::Notion, &[])+ .await+ .unwrap();+ repo.transition_run(+ &owner,+ ImportSource::Notion,+ &[RunStatus::Running],+ RunStatus::Importing,+ None,+ )+ .await+ .unwrap();+ repo.take_run(+ &owner,+ Uuid::now_v7(),+ std::time::Duration::ZERO,+ &NotionJob::Write { review_id: review },+ )+ .await+ .unwrap();+ let root = Uuid::now_v7();+ repo.set_root_folder(&owner, root).await.unwrap();+ repo.mark_importing(&owner, &rows).await.unwrap();+ let roadmap = write::document_id(rows[0]);+ repo.mark_imported(&owner, rows[0], &roadmap.to_string(), "md", None)+ .await+ .unwrap();++ let tracker = TaskTracker::new();+ let writer = Arc::new(Writer::default());+ let service = ImportServiceImpl::new(+ PgImportRepo::new(pool.clone()),+ Arc::new(NoConnector),+ Arc::clone(&writer),+ Arc::new(ai_usage::NoOpUsageRecorder),+ )+ .with_notion_connections(Arc::new(Connected))+ .with_job_shutdown(tokio_util::sync::CancellationToken::new(), tracker.clone());++ service.notion_import_report(&owner).await.unwrap();+ tracker.close();+ tracker.wait().await;++ let documents = writer.documents.lock().unwrap();+ assert_eq!(documents.len(), 1, "only Spec is written: {documents:?}");+ let (spec, name, links) = &documents[0];+ assert_eq!(name, "Spec");+ assert_eq!(*spec, write::document_id(rows[1]));+ assert_eq!(+ links.pages[&page_id('a')].document_id,+ roadmap,+ "Spec still mentions the Roadmap written before"+ );+ assert!(+ writer.folders.lock().unwrap().is_empty(),+ "the Notion folder is reused"+ );+ let report = service.notion_import_report(&owner).await.unwrap();+ assert_eq!(report.status, Some(RunStatus::Completed));+ assert_eq!(report.imported, 2);+ assert_eq!(report.writing, 0);+ let spec_row = repo.get(&owner, rows[1]).await.unwrap().unwrap();+ assert_eq!(spec_row.entity_id, Some(spec.to_string()));+}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn a_run_that_keeps_stopping_is_given_up_with_its_rows_back_in_staged(pool: PgPool) {+ let owner = user("give-up");+ let (review, rows) = approved_plan(&pool, &owner).await;+ let repo = PgImportRepo::new(pool.clone());+ repo.start_run(&owner, ImportSource::Notion, &[])+ .await+ .unwrap();+ repo.transition_run(+ &owner,+ ImportSource::Notion,+ &[RunStatus::Running],+ RunStatus::Importing,+ None,+ )+ .await+ .unwrap();+ repo.take_run(+ &owner,+ Uuid::now_v7(),+ std::time::Duration::ZERO,+ &NotionJob::Write { review_id: review },+ )+ .await+ .unwrap();+ repo.mark_importing(&owner, &rows).await.unwrap();+ sqlx::query!(+ "UPDATE import_run SET adoptions = $2 WHERE user_id = $1",+ owner.as_ref(),+ i16::try_from(MAX_ADOPTIONS).unwrap(),+ )+ .execute(&pool)+ .await+ .unwrap();++ let tracker = TaskTracker::new();+ let service = service(+ &pool,+ Writer::default(),+ tokio_util::sync::CancellationToken::new(),+ tracker.clone(),+ );+ let report = service.notion_import_report(&owner).await.unwrap();+ tracker.close();+ tracker.wait().await;++ assert_eq!(report.status, Some(RunStatus::Failed));+ assert_eq!(report.error.as_deref(), Some(IMPORT_STOPPED));+ assert_eq!(report.writing, 0);+ assert_eq!(report.waiting, 2, "both can be imported again");+}++/// A Macro user on `team`, which is made on first use.+async fn teammate(pool: &PgPool, label: &str, team: Uuid) -> 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();+ sqlx::query!(+ "INSERT INTO team (id, name, owner_id) VALUES ($1, 'Sharing', $2) ON CONFLICT (id) DO NOTHING",+ team,+ user_id,+ )+ .execute(pool)+ .await+ .unwrap();+ sqlx::query!(+ "INSERT INTO team_user (user_id, team_id, team_role) VALUES ($1, $2, 'member')",+ user_id,+ team,+ )+ .execute(pool)+ .await+ .unwrap();+ MacroUserIdStr::parse_from_str(&user_id)+ .unwrap()+ .into_owned()+}++/// Move `owner`'s run to `ready`, as planning leaves it.+async fn planned(repo: &PgImportRepo, owner: &MacroUserIdStr<'static>) {+ repo.start_run(owner, ImportSource::Notion, &[])+ .await+ .unwrap();+ repo.transition_run(+ owner,+ ImportSource::Notion,+ &[RunStatus::Running],+ RunStatus::Ready,+ None,+ )+ .await+ .unwrap();+}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn a_write_shares_what_the_plan_shares_and_keeps_the_rest_with_the_owner(pool: PgPool) {+ let team = Uuid::now_v7();+ let owner = teammate(&pool, "share-owner", team).await;+ let (review, rows) = approved(+ &pool,+ &owner,+ &[+ planned_page('a', "Wiki", true),+ planned_page('b', "Journal", false),+ planned_page('c', "Roadmap", true),+ ],+ )+ .await;+ let repo = PgImportRepo::new(pool.clone());+ planned(&repo, &owner).await;+ let tracker = TaskTracker::new();+ let writer = Arc::new(Writer::default());+ let service = ImportServiceImpl::new(+ PgImportRepo::new(pool.clone()),+ Arc::new(NoConnector),+ Arc::clone(&writer),+ Arc::new(ai_usage::NoOpUsageRecorder),+ )+ .with_notion_connections(Arc::new(Connected))+ .with_job_shutdown(tokio_util::sync::CancellationToken::new(), tracker.clone());++ // The owner keeps the Roadmap to themselves on the card.+ service+ .start_notion_write(+ &owner,+ review,+ NotionWriteChoices {+ personal: vec![rows[2]],+ ..NotionWriteChoices::default()+ },+ )+ .await+ .unwrap();+ tracker.close();+ tracker.wait().await;++ assert_eq!(+ *writer.shares.lock().unwrap(),+ [SharedEntity::Document(write::document_id(rows[0]))],+ "only the Wiki is shared"+ );+ let wiki = repo.get(&owner, rows[0]).await.unwrap().unwrap();+ assert_eq!(wiki.status, ImportStatus::Imported);+ assert_eq!(wiki.team_id, Some(team), "the team's copy of the Wiki");+ let roadmap = repo.get(&owner, rows[2]).await.unwrap().unwrap();+ assert_eq!(roadmap.team_id, None);+ assert_eq!(+ roadmap.metadata["plan"]["shared"], false,+ "the owner's choice is on the row, for a resumed write"+ );+ assert_eq!(writer.documents.lock().unwrap().len(), 3);+}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn a_teammate_writing_the_same_page_waits_and_links_to_the_first_copy(pool: PgPool) {+ let team = Uuid::now_v7();+ let ana = teammate(&pool, "first-ana", team).await;+ let bo = teammate(&pool, "second-bo", team).await;+ let repo = PgImportRepo::new(pool.clone());+ // Ana claimed the Wiki and is writing it in a live process.+ let (_, anas) = approved(&pool, &ana, &[planned_page('a', "Wiki", true)]).await;+ repo.start_run(&ana, ImportSource::Notion, &[])+ .await+ .unwrap();+ repo.transition_run(+ &ana,+ ImportSource::Notion,+ &[RunStatus::Running],+ RunStatus::Importing,+ None,+ )+ .await+ .unwrap();+ repo.take_run(+ &ana,+ Uuid::now_v7(),+ LEASE,+ &NotionJob::Plan {+ scope: import_notion::domain::model::ImportScope::Everything,+ sharing: NotionSharing::Team,+ },+ )+ .await+ .unwrap();+ repo.claim_importing(&ana, &anas, &anas, Some(team))+ .await+ .unwrap();+ // Bo approves the same page, shared, while Ana is still writing.+ let (review, bos) = approved(&pool, &bo, &[planned_page('a', "Wiki", true)]).await;+ planned(&repo, &bo).await;+ let tracker = TaskTracker::new();+ let writer = Arc::new(Writer::default());+ let service = ImportServiceImpl::new(+ PgImportRepo::new(pool.clone()),+ Arc::new(NoConnector),+ Arc::clone(&writer),+ Arc::new(ai_usage::NoOpUsageRecorder),+ )+ .with_notion_connections(Arc::new(Connected))+ .with_job_shutdown(tokio_util::sync::CancellationToken::new(), tracker.clone());++ service+ .start_notion_write(&bo, review, NotionWriteChoices::default())+ .await+ .unwrap();+ tracker.close();+ tracker.wait().await;++ assert!(+ writer.documents.lock().unwrap().is_empty(),+ "Bo writes no second copy"+ );+ let waiting = repo.get(&bo, bos[0]).await.unwrap().unwrap();+ assert_eq!(waiting.status, ImportStatus::Importing);+ assert_eq!(waiting.waiting_on, Some(anas[0]));+ let report = service.notion_import_report(&bo).await.unwrap();+ assert_eq!(report.status, Some(RunStatus::Completed));+ assert_eq!(report.waiting_on_teammates, 1);++ // Ana finishes: Bo's row links to her copy.+ let wiki = write::document_id(anas[0]).to_string();+ repo.mark_imported(&ana, anas[0], &wiki, "md", None)+ .await+ .unwrap();+ let linked = repo.get(&bo, bos[0]).await.unwrap().unwrap();+ assert_eq!(linked.status, ImportStatus::Imported);+ assert_eq!(linked.entity_id, Some(wiki));+}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn a_page_a_teammate_already_shared_is_linked_without_writing(pool: PgPool) {+ let team = Uuid::now_v7();+ let ana = teammate(&pool, "done-ana", team).await;+ let bo = teammate(&pool, "linking-bo", team).await;+ let repo = PgImportRepo::new(pool.clone());+ let (_, anas) = approved(&pool, &ana, &[planned_page('a', "Wiki", true)]).await;+ repo.claim_importing(&ana, &anas, &anas, Some(team))+ .await+ .unwrap();+ repo.mark_imported(&ana, anas[0], "doc-wiki", "md", None)+ .await+ .unwrap();+ // Bo planned the Wiki privately: the team's copy still stands in for it.+ let (review, bos) = approved(&pool, &bo, &[planned_page('a', "Wiki", false)]).await;+ let row = repo.get(&bo, bos[0]).await.unwrap().unwrap();+ let mut meta: NotionDocMeta = serde_json::from_value(row.metadata).unwrap();+ meta.plan.as_mut().unwrap().already_imported =+ Some(crate::domain::models::NotionAlreadyImported {+ by: "[email protected]".to_string(),+ entity_id: "doc-wiki".to_string(),+ entity_type: "md".to_string(),+ });+ sqlx::query!(+ "UPDATE import_entity SET metadata = $2 WHERE id = $1",+ bos[0],+ serde_json::to_value(meta).unwrap(),+ )+ .execute(&pool)+ .await+ .unwrap();+ planned(&repo, &bo).await;+ let tracker = TaskTracker::new();+ let writer = Arc::new(Writer::default());+ let service = ImportServiceImpl::new(+ PgImportRepo::new(pool.clone()),+ Arc::new(NoConnector),+ Arc::clone(&writer),+ Arc::new(ai_usage::NoOpUsageRecorder),+ )+ .with_notion_connections(Arc::new(Connected))+ .with_job_shutdown(tokio_util::sync::CancellationToken::new(), tracker.clone());++ service+ .start_notion_write(&bo, review, NotionWriteChoices::default())+ .await+ .unwrap();+ tracker.close();+ tracker.wait().await;++ assert!(writer.documents.lock().unwrap().is_empty());+ let linked = repo.get(&bo, bos[0]).await.unwrap().unwrap();+ assert_eq!(linked.status, ImportStatus::Imported);+ assert_eq!(linked.entity_id.as_deref(), Some("doc-wiki"));+ assert_eq!(linked.team_id, None);+}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn a_plan_presented_while_planning_fills_in_batch_by_batch_and_restages_on_resume(+ pool: PgPool,+) {+ use crate::domain::service::notion_workspace::plan::PlanTree;+ use crate::domain::service::notion_workspace::triage::Verdict;+ use import_notion::domain::model::{Inventory, NotionParent};++ let owner = user("filling");+ let service = service(+ &pool,+ Writer::default(),+ tokio_util::sync::CancellationToken::new(),+ TaskTracker::new(),+ );+ let repo = PgImportRepo::new(pool.clone());+ repo.start_run(&owner, ImportSource::Notion, &[])+ .await+ .unwrap();+ assert!(+ service+ .take_run(+ &owner,+ &NotionJob::Plan {+ scope: import_notion::domain::model::ImportScope::Everything,+ sharing: NotionSharing::Personal,+ },+ )+ .await+ .unwrap()+ );+ // The agent presents the plan before planning has staged anything.+ let (review, _) = approved(&pool, &owner, &[]).await;+ let report = service.notion_import_report(&owner).await.unwrap();+ assert_eq!(report.status, Some(RunStatus::Running));+ assert_eq!(report.review_id, Some(review));++ let page = |hex: char, title: &str, parent: NotionParent| NotionPage {+ id: page_id(hex),+ title: title.to_string(),+ url: page_id(hex).url(),+ parent,+ last_edited: chrono::Utc::now(),+ created: chrono::Utc::now(),+ in_trash: false,+ properties: Default::default(),+ };+ let tree = PlanTree::new(&Inventory {+ pages: vec![+ page('a', "Engineering", NotionParent::Workspace),+ page('b', "Spec", NotionParent::Page { id: page_id('a') }),+ ],+ data_sources: Vec::new(),+ });+ let items = tree.triage_items(&std::collections::HashMap::new());+ let verdict = |hex: char, decision: NotionDecision| {+ (+ page_id(hex).to_string(),+ Verdict {+ key: page_id(hex).to_string(),+ decision,+ reason: None,+ task_fields: None,+ shared: None,+ shared_reason: None,+ },+ )+ };+ let control = JobControl::new(tokio_util::sync::CancellationToken::new());++ // Planning: the tree is staged undecided, then one batch at a time.+ for attempt in ["first", "resumed"] {+ repo.replace_plan(+ &owner,+ ImportSource::Notion,+ &tree.staged_rows(+ NotionSharing::Personal,+ false,+ &std::collections::HashMap::new(),+ ),+ )+ .await+ .unwrap();+ let staged = service.notion_import_report(&owner).await.unwrap();+ assert_eq!(staged.review_id, Some(review), "{attempt}");+ assert_eq!((staged.pages, staged.deciding), (2, 2), "{attempt}");++ let between = Mutex::new(None);+ let batches = futures::stream::unfold(0, |batch| {+ let service = &service;+ let owner = &owner;+ let between = ≬+ async move {+ match batch {+ 0 => Some((+ [verdict('a', NotionDecision::Copy)].into_iter().collect(),+ 1,+ )),+ 1 => {+ // Read once the first batch is written.+ *between.lock().unwrap() =+ Some(service.notion_import_report(owner).await.unwrap());+ Some((+ [verdict('b', NotionDecision::Skip)].into_iter().collect(),+ 2,+ ))+ }+ _ => None,+ }+ }+ });+ service+ .stage_verdicts(+ &owner,+ &tree,+ &items,+ batches,+ NotionSharing::Personal,+ false,+ &control,+ )+ .await+ .unwrap();++ let between = between.into_inner().unwrap().unwrap();+ assert_eq!(+ (between.deciding, between.copy, between.skip),+ (1, 1, 0),+ "{attempt}: the first batch shows before the second is answered"+ );+ let decided = service.notion_import_report(&owner).await.unwrap();+ assert_eq!(+ (decided.pages, decided.deciding, decided.copy, decided.skip),+ (2, 0, 1, 1),+ "{attempt}"+ );+ let rows = repo+ .list(&owner, Some(ImportSource::Notion), None)+ .await+ .unwrap();+ assert_eq!(rows.len(), 2, "{attempt}: staging again duplicates nothing");+ assert!(+ rows.iter().all(|row| row.review_id == Some(review)),+ "{attempt}: rows staged after the review was presented are tied to it"+ );+ }+}++/// The session events the import produces, in order.+#[derive(Default)]+struct Sessions(Mutex<Vec<PendingNotification>>);++impl NotificationWriter<PgConnection> for Sessions {+ fn produce<'a>(+ &'a self,+ _: &'a mut PgConnection,+ notification: &'a PendingNotification,+ ) -> NotificationFuture<'a, ()> {+ self.0.lock().unwrap().push(notification.clone());+ Box::pin(async { Ok(()) })+ }+}++/// A repository telling `sessions` what becomes of reviewed runs.+fn notifying_repo(pool: &PgPool, sessions: &Arc<Sessions>) -> PgImportRepo {+ PgImportRepo::new(pool.clone()).with_session_notifications(Arc::clone(sessions) as _)+}++/// A user with a two-page plan presented in a session while it was being+/// planned, and the run still `running`.+async fn presented_while_planning(+ pool: &PgPool,+ owner: &MacroUserIdStr<'static>,+) -> (Uuid, AgentSessionId, Vec<Uuid>) {+ let (review, rows) = approved_plan(pool, owner).await;+ let session = sqlx::query_scalar!(+ "SELECT agent_session_id FROM agent_session_review WHERE id = $1",+ review+ )+ .fetch_one(pool)+ .await+ .unwrap();+ let session = AgentSessionId::new_from_uuid(session);+ let repo = PgImportRepo::new(pool.clone());+ repo.start_run(owner, ImportSource::Notion, &[])+ .await+ .unwrap();+ repo.attach_review(owner, ImportSource::Notion, review, session)+ .await+ .unwrap();+ (review, session, rows)+}++fn settled_import(+ notification: &PendingNotification,+) -> &agent_runtime_protocol::domain::notification::ImportSettlement {+ match ¬ification.notification.event {+ NotificationEvent::ReviewSettled(ReviewSettlement::Import { import }) => import,+ other => panic!("expected a settled import, got {other:?}"),+ }+}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn a_plan_presented_while_planning_tells_its_session_once_it_is_ready(pool: PgPool) {+ let owner = user("planned");+ let (review, session, _) = presented_while_planning(&pool, &owner).await;+ let sessions = Arc::new(Sessions::default());+ let service = ImportServiceImpl::new(+ notifying_repo(&pool, &sessions),+ Arc::new(NoConnector),+ Arc::new(Writer::default()),+ Arc::new(ai_usage::NoOpUsageRecorder),+ )+ .with_notion_connections(Arc::new(Connected));++ assert!(+ service+ .end_plan(&owner, RunStatus::Ready, None)+ .await+ .unwrap()+ );+ assert!(+ !service+ .end_plan(&owner, RunStatus::Ready, None)+ .await+ .unwrap(),+ "a plan is ready once"+ );++ let produced = sessions.0.lock().unwrap();+ assert_eq!(produced.len(), 1);+ assert_eq!(produced[0].session_id, session);+ assert_eq!(produced[0].notification.subject_id, review.to_string());+ assert!(produced[0].wake);+ let NotificationEvent::ImportPlanned(plan) = &produced[0].notification.event else {+ panic!("expected a ready plan");+ };+ assert_eq!(plan.copy, 2);+}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn a_plan_that_fails_tells_no_session_it_is_ready(pool: PgPool) {+ let owner = user("plan-failed");+ presented_while_planning(&pool, &owner).await;+ let sessions = Arc::new(Sessions::default());+ let service = ImportServiceImpl::new(+ notifying_repo(&pool, &sessions),+ Arc::new(NoConnector),+ Arc::new(Writer::default()),+ Arc::new(ai_usage::NoOpUsageRecorder),+ );++ assert!(+ service+ .end_plan(&owner, RunStatus::Failed, Some("Notion was unreachable"))+ .await+ .unwrap()+ );++ assert!(sessions.0.lock().unwrap().is_empty());+}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn a_finished_write_tells_its_session_once_with_the_final_report(pool: PgPool) {+ let owner = user("settled");+ let (review, session, _) = presented_while_planning(&pool, &owner).await;+ let repo = PgImportRepo::new(pool.clone());+ repo.transition_run(+ &owner,+ ImportSource::Notion,+ &[RunStatus::Running],+ RunStatus::Ready,+ None,+ )+ .await+ .unwrap();+ let sessions = Arc::new(Sessions::default());+ let tracker = TaskTracker::new();+ let service = ImportServiceImpl::new(+ notifying_repo(&pool, &sessions),+ Arc::new(NoConnector),+ Arc::new(Writer::default()),+ Arc::new(ai_usage::NoOpUsageRecorder),+ )+ .with_notion_connections(Arc::new(Connected))+ .with_job_shutdown(tokio_util::sync::CancellationToken::new(), tracker.clone());++ service+ .start_notion_write(&owner, review, NotionWriteChoices::default())+ .await+ .unwrap();+ tracker.close();+ tracker.wait().await;+ service.notion_import_report(&owner).await.unwrap();++ let produced = sessions.0.lock().unwrap();+ assert_eq!(produced.len(), 1, "{produced:?}");+ assert_eq!(produced[0].session_id, session);+ assert_eq!(produced[0].notification.subject_id, review.to_string());+ assert!(produced[0].wake);+ let settled = settled_import(&produced[0]);+ assert_eq!(settled.outcome, ImportOutcome::Completed);+ assert_eq!(settled.error, None);+ assert_eq!(settled.imported, 2);+ assert_eq!(settled.planned, 2);+ assert_eq!(settled.not_written, 0);+ assert!(settled.root_folder_id.is_some());+}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn an_adopted_write_tells_its_session_once_when_it_finishes(pool: PgPool) {+ let owner = user("adopted-settled");+ let (review, session, rows) = presented_while_planning(&pool, &owner).await;+ let repo = PgImportRepo::new(pool.clone());+ // The first process started writing and died; its lease has lapsed.+ repo.transition_run(+ &owner,+ ImportSource::Notion,+ &[RunStatus::Running],+ RunStatus::Importing,+ None,+ )+ .await+ .unwrap();+ repo.take_run(+ &owner,+ Uuid::now_v7(),+ std::time::Duration::ZERO,+ &NotionJob::Write { review_id: review },+ )+ .await+ .unwrap();+ repo.mark_importing(&owner, &rows).await.unwrap();+ let sessions = Arc::new(Sessions::default());+ let tracker = TaskTracker::new();+ let service = ImportServiceImpl::new(+ notifying_repo(&pool, &sessions),+ Arc::new(NoConnector),+ Arc::new(Writer::default()),+ Arc::new(ai_usage::NoOpUsageRecorder),+ )+ .with_notion_connections(Arc::new(Connected))+ .with_job_shutdown(tokio_util::sync::CancellationToken::new(), tracker.clone());++ service.notion_import_report(&owner).await.unwrap();+ tracker.close();+ tracker.wait().await;+ service.notion_import_report(&owner).await.unwrap();++ let produced = sessions.0.lock().unwrap();+ assert_eq!(produced.len(), 1, "{produced:?}");+ assert_eq!(produced[0].session_id, session);+ let settled = settled_import(&produced[0]);+ assert_eq!(settled.outcome, ImportOutcome::Completed);+ assert_eq!(settled.imported, 2);+}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn a_write_given_up_on_tells_its_session_it_failed(pool: PgPool) {+ let owner = user("given-up-settled");+ let (review, session, rows) = presented_while_planning(&pool, &owner).await;+ let repo = PgImportRepo::new(pool.clone());+ repo.transition_run(+ &owner,+ ImportSource::Notion,+ &[RunStatus::Running],+ RunStatus::Importing,+ None,+ )+ .await+ .unwrap();+ repo.take_run(+ &owner,+ Uuid::now_v7(),+ std::time::Duration::ZERO,+ &NotionJob::Write { review_id: review },+ )+ .await+ .unwrap();+ repo.mark_importing(&owner, &rows).await.unwrap();+ sqlx::query!(+ "UPDATE import_run SET adoptions = $2 WHERE user_id = $1",+ owner.as_ref(),+ i16::try_from(MAX_ADOPTIONS).unwrap(),+ )+ .execute(&pool)+ .await+ .unwrap();+ let sessions = Arc::new(Sessions::default());+ let tracker = TaskTracker::new();+ let service = ImportServiceImpl::new(+ notifying_repo(&pool, &sessions),+ Arc::new(NoConnector),+ Arc::new(Writer::default()),+ Arc::new(ai_usage::NoOpUsageRecorder),+ )+ .with_notion_connections(Arc::new(Connected))+ .with_job_shutdown(tokio_util::sync::CancellationToken::new(), tracker.clone());++ service.notion_import_report(&owner).await.unwrap();+ tracker.close();+ tracker.wait().await;+ service.notion_import_report(&owner).await.unwrap();++ let produced = sessions.0.lock().unwrap();+ assert_eq!(produced.len(), 1, "{produced:?}");+ assert_eq!(produced[0].session_id, session);+ let settled = settled_import(&produced[0]);+ assert_eq!(settled.outcome, ImportOutcome::Failed);+ assert_eq!(settled.error.as_deref(), Some(IMPORT_STOPPED));+ assert_eq!(settled.imported, 0);+ assert_eq!(settled.not_written, 2);+}diff --git a/crates/import/src/domain/service/notion_workspace/scan.rs b/crates/import/src/domain/service/notion_workspace/scan.rsnew file mode 100644index 0000000000..4f3603cefc--- /dev/null+++ b/crates/import/src/domain/service/notion_workspace/scan.rs@@ -0,0 +1,49 @@+//! Recording what a planning run has found in Notion so far, so the user+//! sees the scan grow while the inventory walks the workspace.++use macro_user_id::user_id::MacroUserIdStr;+use tokio::sync::watch;+use tokio::time::Instant;++use super::NOTIFY_EVERY;+use crate::domain::models::{ImportSource, ScanCounts};+use crate::domain::ports::NotionPlanRepo;++#[cfg(test)]+mod test;++/// Write the newest counts from `counts` to the run, at most every+/// [`NOTIFY_EVERY`], and the last ones as soon as the sender is dropped.+/// Returns once the sender is gone and everything sent is written.+pub(super) async fn record_scan<R: NotionPlanRepo>(+ repo: &R,+ user: &MacroUserIdStr<'static>,+ mut counts: watch::Receiver<ScanCounts>,+) {+ let mut unwritten = false;+ let mut next_write = Instant::now();+ loop {+ tokio::select! {+ changed = counts.changed() => match changed {+ Ok(()) => unwritten = true,+ Err(_) => break,+ },+ () = tokio::time::sleep_until(next_write), if unwritten => {+ let latest = *counts.borrow();+ write(repo, user, latest).await;+ unwritten = false;+ next_write = Instant::now() + NOTIFY_EVERY;+ }+ }+ }+ if unwritten {+ let latest = *counts.borrow();+ write(repo, user, latest).await;+ }+}++async fn write<R: NotionPlanRepo>(repo: &R, user: &MacroUserIdStr<'static>, scanned: ScanCounts) {+ if let Err(error) = repo.record_scan(user, ImportSource::Notion, scanned).await {+ tracing::warn!(error = ?error, "could not record the notion scan progress");+ }+}diff --git a/crates/import/src/domain/service/notion_workspace/scan/test.rs b/crates/import/src/domain/service/notion_workspace/scan/test.rsnew file mode 100644index 0000000000..98053d8efe--- /dev/null+++ b/crates/import/src/domain/service/notion_workspace/scan/test.rs@@ -0,0 +1,171 @@+use std::time::Duration;++use macro_user_id::cowlike::CowLike;+use tokio::sync::mpsc;+use uuid::Uuid;++use super::*;+use crate::domain::models::{ImportEntity, RunStatus};+use crate::domain::ports::{PlanRow, Result};++/// Hands every recorded scan to the test, with when it was written.+struct Recorder(mpsc::UnboundedSender<(Instant, ScanCounts)>);++impl NotionPlanRepo for Recorder {+ async fn replace_plan(+ &self,+ _: &MacroUserIdStr<'static>,+ _: ImportSource,+ _: &[PlanRow],+ ) -> Result<Vec<ImportEntity>> {+ unreachable!("recording a scan never replaces the plan")+ }++ async fn update_plan_rows(+ &self,+ _: &MacroUserIdStr<'static>,+ _: ImportSource,+ _: &[PlanRow],+ ) -> Result<u64> {+ unreachable!("recording a scan never decides plan rows")+ }++ async fn attach_review(+ &self,+ _: &MacroUserIdStr<'static>,+ _: ImportSource,+ _: Uuid,+ _: agent_session::domain::model::AgentSessionId,+ ) -> Result<u64> {+ unreachable!("recording a scan never attaches a review")+ }++ async fn transition_run(+ &self,+ _: &MacroUserIdStr<'static>,+ _: ImportSource,+ _: &[RunStatus],+ _: RunStatus,+ _: Option<&str>,+ ) -> Result<bool> {+ unreachable!("recording a scan never moves the run")+ }++ async fn team_copies(+ &self,+ _: &MacroUserIdStr<'static>,+ _: &[String],+ ) -> Result<Vec<ImportEntity>> {+ unreachable!("recording a scan never reads team copies")+ }++ async fn set_plan_sharing(+ &self,+ _: &MacroUserIdStr<'static>,+ _: &[Uuid],+ _: &[Uuid],+ ) -> Result<u64> {+ unreachable!("recording a scan never changes sharing")+ }++ async fn claim_importing(+ &self,+ _: &MacroUserIdStr<'static>,+ _: &[Uuid],+ _: &[Uuid],+ _: Option<Uuid>,+ ) -> Result<Vec<ImportEntity>> {+ unreachable!("recording a scan never claims rows")+ }++ async fn claim_team_object(+ &self,+ _: &MacroUserIdStr<'static>,+ _: Uuid,+ _: Uuid,+ _: bool,+ ) -> Result<crate::domain::models::TeamClaim> {+ unreachable!("recording a scan never claims objects")+ }++ async fn wait_on(&self, _: &MacroUserIdStr<'static>, _: Uuid, _: Uuid) -> Result<bool> {+ unreachable!("recording a scan never waits")+ }++ async fn record_scan(+ &self,+ _: &MacroUserIdStr<'static>,+ source: ImportSource,+ scanned: ScanCounts,+ ) -> Result<bool> {+ assert_eq!(source, ImportSource::Notion);+ self.0.send((Instant::now(), scanned)).unwrap();+ Ok(true)+ }+}++#[tokio::test(start_paused = true)]+async fn scan_counts_are_written_at_most_every_two_seconds_and_once_at_the_end() {+ let user = MacroUserIdStr::parse_from_str("macro|[email protected]")+ .unwrap()+ .into_owned();+ let (writes, mut written) = mpsc::unbounded_channel();+ let repo = Recorder(writes);+ let (counts, scanned) = watch::channel(ScanCounts::default());++ let walk = async {+ counts.send_replace(ScanCounts {+ pages: 100,+ data_sources: 0,+ });+ let (first_at, first) = written.recv().await.unwrap();+ assert_eq!(+ first,+ ScanCounts {+ pages: 100,+ data_sources: 0,+ }+ );++ // Two search pages inside the throttle window: only the newest is+ // written, once the window has passed.+ counts.send_replace(ScanCounts {+ pages: 200,+ data_sources: 0,+ });+ counts.send_replace(ScanCounts {+ pages: 200,+ data_sources: 7,+ });+ let (second_at, second) = written.recv().await.unwrap();+ assert_eq!(+ second,+ ScanCounts {+ pages: 200,+ data_sources: 7,+ }+ );+ assert_eq!(second_at - first_at, Duration::from_secs(2));++ // The final counts are written as soon as the inventory ends, without+ // waiting out the window.+ counts.send_replace(ScanCounts {+ pages: 180,+ data_sources: 7,+ });+ drop(counts);+ second_at+ };+ let (second_at, ()) = tokio::join!(walk, record_scan(&repo, &user, scanned));++ let (last_at, last) = written.recv().await.unwrap();+ assert_eq!(+ last,+ ScanCounts {+ pages: 180,+ data_sources: 7,+ }+ );+ assert_eq!(last_at, second_at);+ assert!(written.try_recv().is_err());+}diff --git a/crates/import/src/domain/service/notion_workspace/sharing.rs b/crates/import/src/domain/service/notion_workspace/sharing.rsnew file mode 100644index 0000000000..e8faed9664--- /dev/null+++ b/crates/import/src/domain/service/notion_workspace/sharing.rs@@ -0,0 +1,112 @@+//! Who sees what a workspace import writes: the plan's per-object sharing,+//! the owner's changes to it, and which folders can be shared.+//!+//! Sharing a folder shares everything inside it, so a folder is shared only+//! when everything written inside it is.++use std::collections::{HashMap, HashSet};++use uuid::Uuid;++use crate::domain::models::NotionSharing;++use super::triage::Verdict;+use super::write::{FolderLayout, WriteItem};++#[cfg(test)]+mod test;++/// Why a user without a team keeps everything.+pub(super) const NO_TEAM: &str = "You're not on a team, so this stays visible only to you.";+/// Why an object is not shared when the decision model gave no answer.+const NO_ANSWER: &str = "Kept visible only to you: the decision model gave no answer.";++/// What the plan says about one object's sharing.+#[derive(Debug, Clone, PartialEq, Eq)]+pub(super) struct PlanSharing {+ /// Shared with the team.+ pub shared: bool,+ /// Why, when there is something to say.+ pub reason: Option<String>,+ /// Whether the user may change it.+ pub shareable: bool,+}++/// The plan's sharing for one object under the user's choice.+pub(super) fn plan_sharing(sharing: NotionSharing, verdict: &Verdict, team: bool) -> PlanSharing {+ let personal = |reason: &str| PlanSharing {+ shared: false,+ reason: Some(reason.to_string()),+ shareable: false,+ };+ if !team {+ return personal(NO_TEAM);+ }+ let (shared, reason) = match sharing {+ NotionSharing::Team => (true, None),+ NotionSharing::Personal => (false, None),+ NotionSharing::Ai => match verdict.shared {+ Some(shared) => (shared, verdict.shared_reason.clone()),+ None => (false, Some(NO_ANSWER.to_string())),+ },+ };+ PlanSharing {+ shared,+ reason,+ shareable: true,+ }+}++/// The owner's changes to the plan's sharing that the import accepts: rows+/// that can be shared, set to a value other than the plan's. Returns the+/// rows to share and the rows to keep.+pub(super) fn sharing_changes(+ items: &[WriteItem],+ shared: &[Uuid],+ personal: &[Uuid],+) -> (Vec<Uuid>, Vec<Uuid>) {+ let mut to_share = Vec::new();+ let mut to_keep = Vec::new();+ for item in items.iter().filter(|item| item.shareable) {+ if shared.contains(&item.import_id) && !item.shared {+ to_share.push(item.import_id);+ } else if personal.contains(&item.import_id) && item.shared {+ to_keep.push(item.import_id);+ }+ }+ (to_share, to_keep)+}++/// The folders to share: those whose every written object, the folder's+/// own page included, is shared. A folder holding a private page or+/// database stays with the user, since sharing it would share them.+pub(super) fn shared_folders(items: &[WriteItem], layout: &FolderLayout) -> HashSet<Uuid> {+ let parents: HashMap<Uuid, Option<Uuid>> = items+ .iter()+ .map(|item| (item.import_id, item.parent))+ .collect();+ let mut private: HashSet<Uuid> = HashSet::new();+ let mut holds_written: HashSet<Uuid> = HashSet::new();+ for item in items.iter().filter(|item| item.writes()) {+ // The item's own folder (when it became one) and every folder above.+ let mut current = Some(item.import_id);+ let mut guard = 0;+ while let Some(folder) = current {+ guard += 1;+ if guard > items.len() + 1 {+ break;+ }+ holds_written.insert(folder);+ if !item.shared {+ private.insert(folder);+ }+ current = parents.get(&folder).copied().flatten();+ }+ }+ layout+ .folders+ .iter()+ .copied()+ .filter(|folder| holds_written.contains(folder) && !private.contains(folder))+ .collect()+}diff --git a/crates/import/src/domain/service/notion_workspace/sharing/test.rs b/crates/import/src/domain/service/notion_workspace/sharing/test.rsnew file mode 100644index 0000000000..0b96aad7c9--- /dev/null+++ b/crates/import/src/domain/service/notion_workspace/sharing/test.rs@@ -0,0 +1,189 @@+use import_notion::domain::model::NotionId;++use super::*;+use crate::domain::models::{ImportKind, NotionDecision, NotionTaskFields};+use crate::domain::service::notion_workspace::write::folder_layout;++fn verdict(decision: NotionDecision, shared: Option<bool>) -> Verdict {+ Verdict {+ key: "a".repeat(32),+ decision,+ reason: None,+ task_fields: None,+ shared,+ shared_reason: shared.map(|_| "Team wiki".to_string()),+ }+}++fn item(import: u128, parent: Option<u128>, kind: ImportKind, shared: bool) -> WriteItem {+ WriteItem {+ import_id: Uuid::from_u128(import),+ parent: parent.map(Uuid::from_u128),+ notion_id: NotionId::parse(&"a".repeat(32)).unwrap(),+ kind,+ title: format!("object {import}"),+ decision: NotionDecision::Copy,+ task_fields: NotionTaskFields::default(),+ shared,+ shareable: true,+ already_imported: false,+ }+}++#[test]+fn the_users_choice_decides_sharing_except_for_users_without_a_team() {+ let page = verdict(NotionDecision::Copy, Some(true));+ assert_eq!(+ plan_sharing(NotionSharing::Team, &page, true),+ PlanSharing {+ shared: true,+ reason: None,+ shareable: true+ }+ );+ assert_eq!(+ plan_sharing(NotionSharing::Personal, &page, true),+ PlanSharing {+ shared: false,+ reason: None,+ shareable: true+ }+ );+ assert_eq!(+ plan_sharing(NotionSharing::Ai, &page, true),+ PlanSharing {+ shared: true,+ reason: Some("Team wiki".to_string()),+ shareable: true+ }+ );+ assert_eq!(+ plan_sharing(+ NotionSharing::Ai,+ &verdict(NotionDecision::Copy, None),+ true+ ),+ PlanSharing {+ shared: false,+ reason: Some(NO_ANSWER.to_string()),+ shareable: true+ },+ "no answer keeps it private"+ );+ assert_eq!(+ plan_sharing(+ NotionSharing::Team,+ &verdict(NotionDecision::Tasks, None),+ true+ ),+ PlanSharing {+ shared: true,+ reason: None,+ shareable: true+ },+ "a task list is a shareable folder of tasks"+ );+ assert_eq!(+ plan_sharing(NotionSharing::Team, &page, false),+ PlanSharing {+ shared: false,+ reason: Some(NO_TEAM.to_string()),+ shareable: false+ }+ );+}++#[test]+fn a_database_is_shared_like_a_page() {+ let plan = item(1, None, ImportKind::DataSource, false);+ assert_eq!(+ plan_sharing(+ NotionSharing::Team,+ &verdict(NotionDecision::Copy, None),+ true+ ),+ PlanSharing {+ shared: true,+ reason: None,+ shareable: true+ }+ );+ assert_eq!(+ plan_sharing(+ NotionSharing::Ai,+ &verdict(NotionDecision::Copy, Some(false)),+ true+ ),+ PlanSharing {+ shared: false,+ reason: Some("Team wiki".to_string()),+ shareable: true+ }+ );+ assert_eq!(+ sharing_changes(&[plan], &[Uuid::from_u128(1)], &[]),+ (vec![Uuid::from_u128(1)], vec![]),+ "the owner may share a database the plan keeps"+ );+}++#[test]+fn only_changes_to_shareable_rows_that_differ_from_the_plan_are_accepted() {+ let mut personal = item(3, None, ImportKind::Page, false);+ personal.shareable = false;+ let items = vec![+ item(1, None, ImportKind::Page, false),+ item(2, None, ImportKind::Page, true),+ personal,+ item(4, None, ImportKind::Page, true),+ ];+ let (share, keep) = sharing_changes(+ &items,+ &[Uuid::from_u128(1), Uuid::from_u128(3), Uuid::from_u128(4)],+ &[Uuid::from_u128(2)],+ );++ assert_eq!(+ share,+ [Uuid::from_u128(1)],+ "4 is shared already; 3 cannot be shared"+ );+ assert_eq!(keep, [Uuid::from_u128(2)]);+}++#[test]+fn a_folder_is_shared_only_when_everything_written_inside_it_is() {+ // Wiki (shared) > Handbook (shared) > Onboarding (shared)+ // Projects (shared) > Notes (private)+ // Planning (shared) > Budget (private database)+ // Roadmap (shared) > Releases (shared database)+ let items = vec![+ item(1, None, ImportKind::Page, true),+ item(2, Some(1), ImportKind::Page, true),+ item(3, Some(2), ImportKind::Page, true),+ item(4, None, ImportKind::Page, true),+ item(5, Some(4), ImportKind::Page, false),+ item(6, None, ImportKind::Page, true),+ item(7, Some(6), ImportKind::DataSource, false),+ item(8, None, ImportKind::Page, true),+ item(9, Some(8), ImportKind::DataSource, true),+ ];++ let shared = shared_folders(&items, &folder_layout(&items));++ assert_eq!(+ shared,+ HashSet::from([Uuid::from_u128(1), Uuid::from_u128(2), Uuid::from_u128(8)])+ );+}++#[test]+fn a_teammates_copy_inside_a_folder_does_not_keep_the_folder_private() {+ let mut linked = item(2, Some(1), ImportKind::Page, false);+ linked.already_imported = true;+ let items = vec![item(1, None, ImportKind::Page, true), linked];++ let shared = shared_folders(&items, &folder_layout(&items));++ assert_eq!(shared, HashSet::from([Uuid::from_u128(1)]));+}diff --git a/crates/import/src/domain/service/notion_workspace/triage.rs b/crates/import/src/domain/service/notion_workspace/triage.rsnew file mode 100644index 0000000000..5f7bdf52db--- /dev/null+++ b/crates/import/src/domain/service/notion_workspace/triage.rs@@ -0,0 +1,252 @@+//! The decision model: what to do with each object in scope, decided in+//! batches so a large workspace costs a handful of calls.++use std::collections::HashMap;++use agent::Message;+use agent::structured_output::{DynamicSchema, dynamic_structured_completion};+use futures::{FutureExt, Stream, StreamExt, stream};+use serde::{Deserialize, Serialize};+use serde_json::json;++use crate::domain::models::{ImportKind, NotionDecision, NotionSharing, NotionTaskFields};++#[cfg(test)]+mod test;++/// Objects per decision-model call.+pub(super) const TRIAGE_BATCH: usize = 40;+/// Decision-model calls in flight at once.+pub(super) const TRIAGE_CONCURRENCY: usize = 4;++/// One object, as the decision model sees it.+#[derive(Debug, Clone, PartialEq, Serialize)]+#[serde(rename_all = "camelCase")]+pub(super) struct TriageItem {+ /// The object's Notion id; the model answers by it.+ pub key: String,+ /// `page` or `data_source`.+ pub kind: ImportKind,+ /// The title.+ pub title: String,+ /// Titles of the pages above it, outermost first.+ pub path: Vec<String>,+ /// How many planned objects sit directly under it.+ pub children: usize,+ /// For a data source: its columns and rows.+ #[serde(skip_serializing_if = "Option::is_none")]+ pub table: Option<TriageTable>,+}++/// What the decision model sees of a data source.+#[derive(Debug, Clone, PartialEq, Serialize)]+#[serde(rename_all = "camelCase")]+pub(super) struct TriageTable {+ /// Column names with their Notion types, such as `Status (status)`.+ pub columns: Vec<String>,+ /// Rows counted.+ pub rows: usize,+ /// The first few row titles.+ pub sample_titles: Vec<String>,+}++/// The decision model's answer for one object.+#[derive(Debug, Clone, PartialEq, Deserialize)]+#[serde(rename_all = "camelCase")]+pub(super) struct Verdict {+ /// The object's key, echoed back.+ pub key: String,+ /// What to do with it.+ pub decision: NotionDecision,+ /// Why, in a short sentence.+ #[serde(default)]+ pub reason: Option<String>,+ /// For a task list: which columns hold the task fields.+ #[serde(default)]+ pub task_fields: Option<NotionTaskFields>,+ /// Whether to share it with the user's team; asked only when the user+ /// left sharing to the model.+ #[serde(default)]+ pub shared: Option<bool>,+ /// Why, in a short sentence.+ #[serde(default)]+ pub shared_reason: Option<String>,+}++#[derive(Debug, Deserialize)]+struct TriageOutput {+ verdicts: Vec<Verdict>,+}++pub(super) const TRIAGE_SYSTEM: &str = include_str!("triage_system.md");+/// Added to [`TRIAGE_SYSTEM`] when the model also decides sharing.+pub(super) const TRIAGE_SHARING: &str = include_str!("triage_sharing.md");++/// The system prompt: sharing is the model's to decide only when the user+/// left it to the model.+pub(super) fn triage_system(sharing: NotionSharing) -> String {+ match sharing {+ NotionSharing::Ai => format!("{TRIAGE_SYSTEM}\n{TRIAGE_SHARING}"),+ NotionSharing::Team | NotionSharing::Personal => TRIAGE_SYSTEM.to_string(),+ }+}++/// The schema every decision-model answer is validated against; with+/// sharing left to the model, every verdict also says whether to share.+pub(super) fn triage_schema(sharing: NotionSharing) -> DynamicSchema {+ let mut schema = verdicts_schema();+ if sharing == NotionSharing::Ai {+ let verdict = &mut schema["properties"]["verdicts"]["items"];+ verdict["required"] = json!(["key", "decision", "shared", "sharedReason"]);+ verdict["properties"]["shared"] = json!({ "type": "boolean" });+ verdict["properties"]["sharedReason"] = json!({ "type": "string", "maxLength": 200 });+ }+ DynamicSchema {+ name: "NotionImportVerdicts".to_string(),+ description: Some("What to do with each Notion object in an import.".to_string()),+ schema,+ }+}++fn verdicts_schema() -> serde_json::Value {+ json!({+ "type": "object",+ "additionalProperties": false,+ "required": ["verdicts"],+ "properties": {+ "verdicts": {+ "type": "array",+ "items": {+ "type": "object",+ "additionalProperties": false,+ "required": ["key", "decision"],+ "properties": {+ "key": { "type": "string" },+ "decision": { "type": "string", "enum": ["copy", "tasks", "skip", "hand"] },+ "reason": { "type": "string", "maxLength": 200 },+ "taskFields": {+ "type": "object",+ "additionalProperties": false,+ "properties": {+ "status": { "type": "string" },+ "priority": { "type": "string" },+ "due": { "type": "string" },+ "assignee": { "type": "string" }+ }+ }+ }+ }+ }+ }+ })+}++/// The verdict an object gets when the decision model gave none: bring it+/// over as it is, which loses nothing.+pub(super) fn default_verdict(item: &TriageItem, reason: &str) -> Verdict {+ Verdict {+ key: item.key.clone(),+ decision: NotionDecision::Copy,+ reason: Some(reason.to_string()),+ task_fields: None,+ shared: None,+ shared_reason: None,+ }+}++/// Decide every item, `TRIAGE_BATCH` at a time, yielding each batch's+/// verdicts as it is answered so planning can stage them straight away. A+/// batch the model fails on (or answers badly) falls back to+/// [`default_verdict`] for its items, so planning never fails because the+/// model did.+pub(super) fn decide<'a, Admit, AdmitFuture>(+ items: &'a [TriageItem],+ sharing: NotionSharing,+ admit: Admit,+ recorder: &'a dyn ai_usage::UsageRecorder,+ usage: ai_usage::UsageContext,+ model: agent::PredefinedModel,+) -> impl Stream<Item = HashMap<String, Verdict>> + Send + 'a+where+ Admit: Fn() -> AdmitFuture + Send + Sync + 'a,+ AdmitFuture: Future<Output = anyhow::Result<()>> + Send + 'a,+{+ stream::iter(items.chunks(TRIAGE_BATCH))+ .map(move |batch| {+ let admitted = admit();+ let usage = usage.clone();+ async move {+ let outcome = match admitted.await {+ Ok(()) => decide_batch(batch, sharing, recorder, usage, model).await,+ Err(error) => Err(error),+ };+ match outcome {+ Ok(verdicts) => fill_missing(batch, verdicts),+ Err(error) => {+ tracing::warn!(error = ?error, items = batch.len(), "notion triage batch failed; copying its objects as they are");+ batch+ .iter()+ .map(|item| {+ (item.key.clone(), default_verdict(item, "Planned without the decision model"))+ })+ .collect()+ }+ }+ }+ .boxed()+ })+ .buffer_unordered(TRIAGE_CONCURRENCY)+}++async fn decide_batch(+ batch: &[TriageItem],+ sharing: NotionSharing,+ recorder: &dyn ai_usage::UsageRecorder,+ usage: ai_usage::UsageContext,+ model: agent::PredefinedModel,+) -> anyhow::Result<Vec<Verdict>> {+ let prompt = format!(+ "Decide what to do with each of these {} Notion objects.\n\n```json\n{}\n```",+ batch.len(),+ serde_json::to_string_pretty(batch)?+ );+ let value = dynamic_structured_completion(+ model,+ &triage_system(sharing),+ vec![Message::user(prompt)],+ triage_schema(sharing),+ recorder,+ usage,+ )+ .await?;+ let output: TriageOutput = serde_json::from_value(value)+ .map_err(|error| anyhow::anyhow!("notion triage returned malformed output: {error}"))?;+ Ok(output.verdicts)+}++/// Keep verdicts for keys in the batch, apply rules the model cannot+/// override, and give anything the model skipped the default.+pub(super) fn fill_missing(+ batch: &[TriageItem],+ verdicts: Vec<Verdict>,+) -> HashMap<String, Verdict> {+ let mut by_key: HashMap<String, Verdict> = verdicts+ .into_iter()+ .filter(|verdict| batch.iter().any(|item| item.key == verdict.key))+ .map(|verdict| (verdict.key.clone(), verdict))+ .collect();+ for item in batch {+ let verdict = by_key+ .entry(item.key.clone())+ .or_insert_with(|| default_verdict(item, "The decision model gave no answer"));+ // Only a data source can become tasks.+ if verdict.decision == NotionDecision::Tasks && item.table.is_none() {+ verdict.decision = NotionDecision::Copy;+ verdict.task_fields = None;+ }+ if verdict.decision != NotionDecision::Tasks {+ verdict.task_fields = None;+ }+ }+ by_key+}diff --git a/crates/import/src/domain/service/notion_workspace/triage/test.rs b/crates/import/src/domain/service/notion_workspace/triage/test.rsnew file mode 100644index 0000000000..e605adbfc3--- /dev/null+++ b/crates/import/src/domain/service/notion_workspace/triage/test.rs@@ -0,0 +1,144 @@+use super::*;++fn page(key: &str, title: &str) -> TriageItem {+ TriageItem {+ key: key.to_string(),+ kind: ImportKind::Page,+ title: title.to_string(),+ path: Vec::new(),+ children: 0,+ table: None,+ }+}++fn table(key: &str, title: &str) -> TriageItem {+ TriageItem {+ table: Some(TriageTable {+ columns: vec!["Name (title)".to_string(), "Status (status)".to_string()],+ rows: 12,+ sample_titles: vec!["Fix login".to_string()],+ }),+ kind: ImportKind::DataSource,+ ..page(key, title)+ }+}++#[test]+fn verdicts_keep_the_models_answers_and_fill_in_what_it_left_out() {+ let batch = vec![+ page("a", "Roadmap"),+ page("b", "Untitled"),+ table("c", "Bugs"),+ page("d", "Team wiki"),+ ];+ let verdicts = vec![+ Verdict {+ key: "b".to_string(),+ decision: NotionDecision::Skip,+ reason: Some("Empty page".to_string()),+ task_fields: None,+ shared: None,+ shared_reason: None,+ },+ Verdict {+ key: "c".to_string(),+ decision: NotionDecision::Tasks,+ reason: Some("A bug tracker".to_string()),+ task_fields: Some(NotionTaskFields {+ status: Some("Status".to_string()),+ ..NotionTaskFields::default()+ }),+ shared: None,+ shared_reason: None,+ },+ // Only a data source can become tasks.+ Verdict {+ key: "d".to_string(),+ decision: NotionDecision::Tasks,+ reason: None,+ task_fields: Some(NotionTaskFields::default()),+ shared: None,+ shared_reason: None,+ },+ // A key the batch never had is dropped.+ Verdict {+ key: "zz".to_string(),+ decision: NotionDecision::Skip,+ reason: None,+ task_fields: None,+ shared: None,+ shared_reason: None,+ },+ ];++ let decided = fill_missing(&batch, verdicts);++ assert_eq!(decided.len(), 4);+ assert_eq!(decided["a"].decision, NotionDecision::Copy);+ assert_eq!(+ decided["a"].reason.as_deref(),+ Some("The decision model gave no answer")+ );+ assert_eq!(decided["b"].decision, NotionDecision::Skip);+ assert_eq!(decided["c"].decision, NotionDecision::Tasks);+ assert_eq!(+ decided["c"]+ .task_fields+ .as_ref()+ .and_then(|fields| fields.status.as_deref()),+ Some("Status")+ );+ assert_eq!(decided["d"].decision, NotionDecision::Copy);+ assert_eq!(decided["d"].task_fields, None);+}++#[test]+fn the_verdict_schema_is_one_the_structured_output_validator_accepts() {+ let validator =+ agent::structured_output::validate_schema(&triage_schema(NotionSharing::Personal)).unwrap();+ let answer = serde_json::json!({+ "verdicts": [+ { "key": "c", "decision": "tasks", "reason": "A bug tracker", "taskFields": { "status": "Status" } },+ { "key": "a", "decision": "copy" }+ ]+ });+ assert!(validator.is_valid(&answer));+ let output: TriageOutput = serde_json::from_value(answer).unwrap();+ assert_eq!(output.verdicts[0].decision, NotionDecision::Tasks);+ assert!(!validator.is_valid(&serde_json::json!({+ "verdicts": [{ "key": "a", "decision": "delete" }]+ })));+}++#[test]+fn sharing_is_asked_of_the_model_only_when_the_user_left_it_to_the_model() {+ let verdict = serde_json::json!({+ "verdicts": [{ "key": "a", "decision": "copy" }]+ });+ let with_sharing = serde_json::json!({+ "verdicts": [{ "key": "a", "decision": "copy", "shared": true, "sharedReason": "The team wiki" }]+ });+ let personal =+ agent::structured_output::validate_schema(&triage_schema(NotionSharing::Team)).unwrap();+ let ai = agent::structured_output::validate_schema(&triage_schema(NotionSharing::Ai)).unwrap();++ assert!(personal.is_valid(&verdict));+ assert!(+ !personal.is_valid(&with_sharing),+ "sharing is the user's when they chose it"+ );+ assert!(+ !ai.is_valid(&verdict),+ "every verdict says whether to share"+ );+ assert!(ai.is_valid(&with_sharing));+ assert!(triage_system(NotionSharing::Ai).contains(TRIAGE_SHARING));+ assert!(!triage_system(NotionSharing::Personal).contains(TRIAGE_SHARING));++ let parsed: TriageOutput = serde_json::from_value(with_sharing).unwrap();+ assert_eq!(parsed.verdicts[0].shared, Some(true));+ assert_eq!(+ parsed.verdicts[0].shared_reason.as_deref(),+ Some("The team wiki")+ );+}diff --git a/crates/import/src/domain/service/notion_workspace/triage_sharing.md b/crates/import/src/domain/service/notion_workspace/triage_sharing.mdnew file mode 100644index 0000000000..d12a4b6224--- /dev/null+++ b/crates/import/src/domain/service/notion_workspace/triage_sharing.md@@ -0,0 +1,5 @@+Sharing: the user asked you to decide, for each object, whether what it becomes is shared with their team in Macro ("shared": true) or stays visible only to them ("shared": false).+- Share what the whole team works from: team wikis, handbooks, processes, project and product docs, specs, roadmaps, meeting notes of team meetings, shared task trackers and backlogs.+- Keep private what belongs to one person: personal notes, journals, drafts, to-do lists for oneself, private reflections, 1:1 notes, anything about compensation, hiring candidates, performance or health, credentials, and pages whose path sits under a personal page (for example "My notes" or a person's name).+- When unsure, keep it private: sharing later is easy, unsharing something already read is not.+Give "shared" and a one-sentence "sharedReason" for every object.diff --git a/crates/import/src/domain/service/notion_workspace/triage_system.md b/crates/import/src/domain/service/notion_workspace/triage_system.mdnew file mode 100644index 0000000000..4115a271b6--- /dev/null+++ b/crates/import/src/domain/service/notion_workspace/triage_system.md@@ -0,0 +1,14 @@+You plan an import of a Notion workspace into Macro. For every Notion object you are given, decide what the import does with it. You only decide; nothing is written until the user reviews and approves the plan.++Macro has:+- Documents: rich markdown pages, organized in folders. Notion pages become documents, and a page with pages under it becomes a folder holding its own document and its children.+- Databases: tables with typed columns (text, number, checkbox, date, URL, single/multi select, people, relations to other tables). A Notion data source (the table behind a Notion database) can be copied as a Macro database, row for row.+- Tasks: first-class work items with status (Not Started, In Progress, In Review, Completed, Canceled), priority (Low, Medium, High, Urgent), a due date and an assignee. They live in the team's task list, not in a database.++Decisions:+- "copy": bring it over as it is. Pages become documents, data sources become databases. This is the default; choose it unless another decision is clearly better.+- "tasks": only for a data source that is a task tracker, to-do list, backlog, sprint board, bug list or similar, where each row is a piece of work someone does. Its rows become Macro tasks. Set "taskFields" to the exact column names that hold the status, priority, due date and assignee (omit the ones the table does not have). Choose "copy" instead for tables of things that are not work items (contacts, meeting notes, docs, inventories, CRM records), even when they have a status column.+- "skip": leave it in Notion. Only for objects with no value to the user in Macro: empty "Untitled" pages, Notion's own onboarding and template pages ("Getting Started", "Quick Note" with Notion's sample text, "Template" pages), and obvious duplicates. When in doubt, copy.+- "hand": the import cannot do it well and a person or agent should handle it by hand. Use it rarely: for example a data source that mixes several unrelated kinds of records, or a page that is clearly a live embed or a dashboard of other databases with no content of its own.++Give every object a verdict, answering by its "key". Keep "reason" to one short sentence a user would understand, and give one for every decision other than "copy".diff --git a/crates/import/src/domain/service/notion_workspace/values.rs b/crates/import/src/domain/service/notion_workspace/values.rsnew file mode 100644index 0000000000..e49e8acb4c--- /dev/null+++ b/crates/import/src/domain/service/notion_workspace/values.rs@@ -0,0 +1,516 @@+//! Notion values in Macro's terms: document properties, database cells and+//! task fields.++use std::collections::HashMap;++use chrono::{DateTime, NaiveDate, Utc};+use import_notion::domain::model::{+ NotionId, NotionPage, NotionPropertyKind, NotionPropertySchema, NotionValue,+};+use macro_user_id::user_id::MacroUserIdStr;+use models_databases::{+ CellValue, ColumnId, ColumnKind, EntityKind, EntityRef, OptionId, OptionRef,+};+use uuid::Uuid;++use crate::domain::models::NotionTaskFields;+use crate::domain::ports::{+ ImportedDocumentProperties, ImportedDocumentProperty, ImportedDocumentPropertyValue,+ ImportedTaskProperties,+};++use super::super::{is_tag_property_name, map_linear_priority, map_linear_status};++#[cfg(test)]+mod test;++/// Longest column or option name a Macro database accepts.+const MAX_NAME: usize = 200;++/// Notion users matched to Macro team members, by Notion user id.+pub(super) type PeopleMap = HashMap<NotionId, Person>;++/// A Notion user as the import knows them.+#[derive(Debug, Clone, PartialEq, Eq)]+pub(super) struct Person {+ /// Their email, when Notion shared it.+ pub email: Option<String>,+ /// Their Macro account, when the email matched a team member.+ pub macro_user: Option<MacroUserIdStr<'static>>,+}++/// A Macro column planned for a Notion property.+#[derive(Debug, Clone, PartialEq)]+pub(super) struct PlannedColumn {+ /// The column id, minted before the column is created.+ pub id: ColumnId,+ /// The Notion property it holds.+ pub property: String,+ /// The column name.+ pub name: String,+ /// How the column stores values.+ pub shape: ColumnShape,+}++/// How a planned column stores values.+#[derive(Debug, Clone, PartialEq)]+pub(super) enum ColumnShape {+ /// Plain text; also where values Macro has no type for are snapshotted.+ Text,+ /// A number.+ Number,+ /// A checkbox.+ Boolean,+ /// A date.+ Date,+ /// URLs.+ Link,+ /// Options declared up front.+ Select {+ /// Whether a cell holds several options.+ multi: bool,+ /// The options, by label.+ options: Vec<(OptionId, String)>,+ },+ /// Macro users.+ People {+ /// Whether a cell holds several people.+ multi: bool,+ },+}++impl ColumnShape {+ /// The Macro column kind.+ pub fn kind(&self) -> ColumnKind {+ match self {+ Self::Text => ColumnKind::Text,+ Self::Number => ColumnKind::Number,+ Self::Boolean => ColumnKind::Boolean,+ Self::Date => ColumnKind::Date,+ Self::Link => ColumnKind::Link,+ Self::Select { multi, .. } => ColumnKind::Select { multi: *multi },+ Self::People { multi } => ColumnKind::Entity {+ target: EntityKind::User,+ multi: *multi,+ },+ }+ }+}++/// The column a Notion property becomes, or `None` for the title (which+/// fills the table's title column), relations (added once every table+/// exists), and types with nothing to keep.+/// The column `property` becomes in `database`. Its ids derive from the+/// database and the property, so a resumed write names the same columns.+pub(super) fn planned_column(+ property: &NotionPropertySchema,+ names: &ColumnNames,+ database: Uuid,+) -> Option<PlannedColumn> {+ let shape = match &property.kind {+ NotionPropertyKind::Title+ | NotionPropertyKind::Relation { .. }+ | NotionPropertyKind::Unsupported { .. } => return None,+ NotionPropertyKind::RichText+ | NotionPropertyKind::Email+ | NotionPropertyKind::PhoneNumber+ | NotionPropertyKind::Formula+ | NotionPropertyKind::Rollup+ | NotionPropertyKind::UniqueId { .. }+ | NotionPropertyKind::Files => ColumnShape::Text,+ NotionPropertyKind::Number => ColumnShape::Number,+ NotionPropertyKind::Checkbox => ColumnShape::Boolean,+ NotionPropertyKind::Date+ | NotionPropertyKind::CreatedTime+ | NotionPropertyKind::LastEditedTime => ColumnShape::Date,+ NotionPropertyKind::Url => ColumnShape::Link,+ NotionPropertyKind::Select { options } | NotionPropertyKind::Status { options } => {+ ColumnShape::Select {+ multi: false,+ options: planned_options(+ database,+ &property.id,+ options.iter().map(|option| option.name.as_str()),+ ),+ }+ }+ NotionPropertyKind::MultiSelect { options } => ColumnShape::Select {+ multi: true,+ options: planned_options(+ database,+ &property.id,+ options.iter().map(|option| option.name.as_str()),+ ),+ },+ NotionPropertyKind::People => ColumnShape::People { multi: true },+ NotionPropertyKind::CreatedBy | NotionPropertyKind::LastEditedBy => {+ ColumnShape::People { multi: false }+ }+ };+ Some(PlannedColumn {+ id: ColumnId::from_uuid(derived_id(database, &format!("column/{}", property.id))),+ property: property.name.clone(),+ name: names.of(property),+ shape,+ })+}++/// The column name each property of one data source gets, by property id.+/// Macro refuses an empty column name, so a blank one becomes "Untitled" (or+/// "Untitled 2", ...), distinct from the data source's other names. It+/// depends on the schema alone, so a resumed write picks the same names.+#[derive(Debug, Clone, PartialEq, Eq)]+pub(super) struct ColumnNames(HashMap<String, String>);++impl ColumnNames {+ const UNTITLED: &str = "Untitled";++ pub fn new(properties: &[NotionPropertySchema]) -> Self {+ let mut taken: std::collections::HashSet<String> = properties+ .iter()+ .map(|property| capped_name(&property.name).to_lowercase())+ .collect();+ let names = properties+ .iter()+ .map(|property| {+ let mut name = capped_name(&property.name);+ if name.is_empty() {+ name = std::iter::once(Self::UNTITLED.to_string())+ .chain((2..).map(|index| format!("{} {index}", Self::UNTITLED)))+ .find(|candidate| !taken.contains(&candidate.to_lowercase()))+ .unwrap_or_else(|| Self::UNTITLED.to_string());+ taken.insert(name.to_lowercase());+ }+ (property.id.clone(), name)+ })+ .collect();+ Self(names)+ }++ /// `property`'s column name.+ pub fn of(&self, property: &NotionPropertySchema) -> String {+ self.0+ .get(&property.id)+ .cloned()+ .unwrap_or_else(|| capped_name(&property.name))+ }+}++/// Options with usable, distinct labels, ids minted.+fn planned_options<'a>(+ database: Uuid,+ property: &str,+ labels: impl Iterator<Item = &'a str>,+) -> Vec<(OptionId, String)> {+ let mut seen = std::collections::HashSet::new();+ labels+ .map(capped_name)+ .filter(|label| !label.is_empty() && seen.insert(label.to_lowercase()))+ .map(|label| {+ let id = derived_id(database, &format!("option/{property}/{label}"));+ (OptionId::from_uuid(id), label)+ })+ .collect()+}++/// A stable id for `name` within `namespace` (UUIDv5), so writing the same+/// object again names the same thing: Macro refuses an id already in use+/// instead of creating a duplicate.+pub(super) fn derived_id(namespace: Uuid, name: &str) -> Uuid {+ Uuid::new_v5(&namespace, name.as_bytes())+}++/// A name trimmed to what Macro accepts.+pub(super) fn capped_name(name: &str) -> String {+ name.trim().chars().take(MAX_NAME).collect()+}++/// The cell a value becomes in `column`, or `None` to leave it empty.+pub(super) fn cell(+ value: &NotionValue,+ column: &PlannedColumn,+ people: &PeopleMap,+) -> Option<CellValue> {+ match &column.shape {+ ColumnShape::Text => text(value).map(CellValue::Text),+ ColumnShape::Number => number(value).map(CellValue::Number),+ ColumnShape::Boolean => match value {+ NotionValue::Checkbox(checked) => Some(CellValue::Boolean(*checked)),+ _ => None,+ },+ ColumnShape::Date => date(value).map(CellValue::Date),+ ColumnShape::Link => match value {+ NotionValue::Url(url) if is_web_url(url) => Some(CellValue::Link(vec![url.clone()])),+ _ => None,+ },+ ColumnShape::Select { multi, options } => {+ let labels: Vec<&str> = match value {+ NotionValue::Option(label) => vec![label.as_str()],+ NotionValue::Options(labels) => labels.iter().map(String::as_str).collect(),+ _ => Vec::new(),+ };+ let refs: Vec<OptionRef> = labels+ .into_iter()+ .filter_map(|label| {+ let label = capped_name(label);+ options+ .iter()+ .find(|(_, known)| known.eq_ignore_ascii_case(&label))+ .map(|(id, _)| OptionRef::Id(*id))+ })+ .take(if *multi { usize::MAX } else { 1 })+ .collect();+ (!refs.is_empty()).then_some(CellValue::Options(refs))+ }+ ColumnShape::People { multi } => {+ let ids: Vec<&NotionId> = match value {+ NotionValue::People(ids) => ids.iter().collect(),+ _ => Vec::new(),+ };+ let users: Vec<EntityRef> = ids+ .into_iter()+ .filter_map(|id| people.get(id)?.macro_user.clone())+ .map(|user| EntityRef {+ entity_type: EntityKind::User,+ entity_id: user.as_ref().to_string(),+ })+ .take(if *multi { usize::MAX } else { 1 })+ .collect();+ (!users.is_empty()).then_some(CellValue::Entities(users))+ }+ }+}++/// A value as text: what a text column snapshots, and what a task body lists.+pub(super) fn text(value: &NotionValue) -> Option<String> {+ let rendered = match value {+ NotionValue::Text(text)+ | NotionValue::Option(text)+ | NotionValue::Url(text)+ | NotionValue::Email(text)+ | NotionValue::Phone(text)+ | NotionValue::Timestamp(text)+ | NotionValue::UniqueId(text) => text.clone(),+ NotionValue::Number(number) => format_number(*number),+ NotionValue::Options(labels) => labels.join(", "),+ NotionValue::Date { start, end } => match end {+ Some(end) => format!("{start} → {end}"),+ None => start.clone(),+ },+ NotionValue::Checkbox(checked) => if *checked { "Yes" } else { "No" }.to_string(),+ NotionValue::Files(files) => files+ .iter()+ .map(|file| file.name.as_str())+ .collect::<Vec<_>>()+ .join(", "),+ NotionValue::List(values) => values+ .iter()+ .filter_map(text)+ .collect::<Vec<_>>()+ .join(", "),+ NotionValue::People(_) | NotionValue::Relation(_) | NotionValue::Empty => String::new(),+ };+ let trimmed = rendered.trim();+ (!trimmed.is_empty()).then(|| trimmed.to_string())+}++fn number(value: &NotionValue) -> Option<f64> {+ match value {+ NotionValue::Number(number) if number.is_finite() => Some(*number),+ NotionValue::List(values) => values.iter().find_map(number),+ _ => None,+ }+}++/// A Notion date or timestamp as a UTC instant; a bare date is midnight UTC.+pub(super) fn date(value: &NotionValue) -> Option<DateTime<Utc>> {+ let raw = match value {+ NotionValue::Date { start, .. } => start.as_str(),+ NotionValue::Timestamp(timestamp) => timestamp.as_str(),+ NotionValue::List(values) => return values.iter().find_map(date),+ _ => return None,+ };+ parse_instant(raw)+}++fn parse_instant(raw: &str) -> Option<DateTime<Utc>> {+ DateTime::parse_from_rfc3339(raw)+ .map(|instant| instant.with_timezone(&Utc))+ .ok()+ .or_else(|| {+ NaiveDate::parse_from_str(raw, "%Y-%m-%d")+ .ok()?+ .and_hms_opt(0, 0, 0)+ .map(|midnight| midnight.and_utc())+ })+}++fn format_number(number: f64) -> String {+ if number.fract() == 0.0 && number.abs() < 1e15 {+ format!("{number:.0}")+ } else {+ number.to_string()+ }+}++fn is_web_url(value: &str) -> bool {+ value.starts_with("https://") || value.starts_with("http://")+}++/// The properties a standalone page carries over to its document.+pub(super) fn document_properties(page: &NotionPage) -> ImportedDocumentProperties {+ let mut imported = ImportedDocumentProperties::default();+ for (name, value) in &page.properties {+ let name = name.trim();+ if name.is_empty() || name.len() > 100 {+ continue;+ }+ if is_tag_property_name(name) {+ match value {+ NotionValue::Options(labels) => imported.tags.extend(labels.iter().cloned()),+ NotionValue::Option(label) | NotionValue::Text(label) => {+ imported.tags.push(label.clone())+ }+ _ => {}+ }+ continue;+ }+ let converted = match value {+ NotionValue::Checkbox(checked) => {+ Some(ImportedDocumentPropertyValue::Boolean { value: *checked })+ }+ NotionValue::Number(number) if number.is_finite() => {+ Some(ImportedDocumentPropertyValue::Number { value: *number })+ }+ NotionValue::Date { start, .. } => Some(ImportedDocumentPropertyValue::Date {+ value: start.clone(),+ }),+ NotionValue::Option(label) => Some(ImportedDocumentPropertyValue::Select {+ values: vec![label.clone()],+ multi: false,+ }),+ NotionValue::Options(labels) if !labels.is_empty() => {+ Some(ImportedDocumentPropertyValue::Select {+ values: labels.clone(),+ multi: true,+ })+ }+ NotionValue::Url(url) if is_web_url(url) => Some(ImportedDocumentPropertyValue::Link {+ urls: vec![url.clone()],+ multi: false,+ }),+ NotionValue::Text(_)+ | NotionValue::Email(_)+ | NotionValue::Phone(_)+ | NotionValue::UniqueId(_) => {+ text(value).map(|value| ImportedDocumentPropertyValue::String { value })+ }+ _ => None,+ };+ if let Some(value) = converted {+ imported.values.push(ImportedDocumentProperty {+ name: name.to_string(),+ value,+ });+ }+ }+ imported.tags.retain(|tag| !tag.trim().is_empty());+ imported.tags.sort();+ imported.tags.dedup();+ imported+}++/// A task list row's task fields, read from the columns `fields` names.+pub(super) fn task_properties(+ row: &NotionPage,+ fields: &NotionTaskFields,+ people: &PeopleMap,+) -> ImportedTaskProperties {+ let field = |name: &Option<String>| name.as_ref().and_then(|name| row.properties.get(name));+ ImportedTaskProperties {+ status: field(&fields.status)+ .and_then(text)+ .and_then(|label| map_task_status(&label))+ .map(String::from),+ priority: field(&fields.priority)+ .and_then(text)+ .and_then(|label| map_linear_priority(&label))+ .map(String::from),+ due_date: field(&fields.due)+ .and_then(date)+ .map(|due| due.format("%Y-%m-%d").to_string()),+ assignee_email: field(&fields.assignee).and_then(|value| match value {+ NotionValue::People(ids) => ids+ .iter()+ .find_map(|id| people.get(id).and_then(|person| person.email.clone())),+ NotionValue::Email(email) => Some(email.clone()),+ _ => None,+ }),+ }+}++/// A Notion status label in Macro's status vocabulary.+fn map_task_status(label: &str) -> Option<&'static str> {+ map_linear_status(label).or_else(|| {+ let label = label.trim().to_ascii_lowercase();+ if label.contains("progress") || label.contains("doing") {+ Some("In Progress")+ } else if label.contains("review") {+ Some("In Review")+ } else if label.contains("done") || label.contains("complete") || label.contains("shipped")+ {+ Some("Completed")+ } else if label.contains("cancel") || label.contains("archived") {+ Some("Canceled")+ } else if label.contains("not started") || label.contains("to do") || label.contains("todo")+ {+ Some("Not Started")+ } else {+ None+ }+ })+}++/// A task body: the row's own content, then the properties the task fields+/// did not take, so nothing in the row is lost.+pub(super) fn task_body(+ row: &NotionPage,+ content: &str,+ fields: &NotionTaskFields,+ people: &PeopleMap,+) -> String {+ let used = [+ &fields.status,+ &fields.priority,+ &fields.due,+ &fields.assignee,+ ];+ let lines: Vec<String> = row+ .properties+ .iter()+ .filter(|(name, _)| {+ !used+ .iter()+ .any(|field| field.as_deref() == Some(name.as_str()))+ })+ .filter_map(|(name, value)| {+ let rendered = match value {+ NotionValue::People(ids) => {+ let names: Vec<String> = ids+ .iter()+ .filter_map(|id| people.get(id).and_then(|person| person.email.clone()))+ .collect();+ (!names.is_empty()).then(|| names.join(", "))+ }+ other => text(other),+ }?;+ Some(format!("- **{name}:** {rendered}"))+ })+ .collect();+ let content = content.trim();+ match (content.is_empty(), lines.is_empty()) {+ (_, true) => content.to_string(),+ (true, false) => lines.join("\n"),+ (false, false) => format!("{content}\n\n---\n\n{}", lines.join("\n")),+ }+}diff --git a/crates/import/src/domain/service/notion_workspace/values/test.rs b/crates/import/src/domain/service/notion_workspace/values/test.rsnew file mode 100644index 0000000000..0ff74c91ed--- /dev/null+++ b/crates/import/src/domain/service/notion_workspace/values/test.rs@@ -0,0 +1,398 @@+use std::collections::BTreeMap;++use chrono::TimeZone;+use import_notion::domain::model::{NotionOption, NotionParent};+use macro_user_id::cowlike::CowLike;++use super::*;++fn option(name: &str) -> NotionOption {+ NotionOption {+ id: format!("id-{name}"),+ name: name.to_string(),+ color: "blue".to_string(),+ }+}++fn row(properties: Vec<(&str, NotionValue)>) -> NotionPage {+ NotionPage {+ id: NotionId::parse("3fb607323e2e80629886fc0c028d50a7").unwrap(),+ title: "Ship the importer".to_string(),+ url: "https://www.notion.so/3fb607323e2e80629886fc0c028d50a7".to_string(),+ parent: NotionParent::Workspace,+ last_edited: Utc.with_ymd_and_hms(2026, 10, 1, 9, 0, 0).unwrap(),+ created: Utc.with_ymd_and_hms(2026, 9, 1, 9, 0, 0).unwrap(),+ in_trash: false,+ properties: properties+ .into_iter()+ .map(|(name, value)| (name.to_string(), value))+ .collect::<BTreeMap<_, _>>(),+ }+}++#[test]+fn a_status_column_becomes_a_select_and_its_cells_point_at_the_planned_options() {+ let property = NotionPropertySchema {+ id: "stat".to_string(),+ name: "Status".to_string(),+ kind: NotionPropertyKind::Status {+ options: vec![option("Not started"), option("In progress"), option("Done")],+ },+ };++ let column = planned_column(+ &property,+ &ColumnNames::new(std::slice::from_ref(&property)),+ Uuid::from_u128(500),+ )+ .unwrap();+ assert_eq!(+ planned_column(+ &property,+ &ColumnNames::new(std::slice::from_ref(&property)),+ Uuid::from_u128(500)+ )+ .unwrap(),+ column,+ "writing the same database again names the same column and options"+ );+ assert_ne!(+ planned_column(+ &property,+ &ColumnNames::new(std::slice::from_ref(&property)),+ Uuid::from_u128(501)+ )+ .unwrap()+ .id,+ column.id+ );++ let ColumnShape::Select { multi, options } = &column.shape else {+ panic!("expected a select, got {:?}", column.shape);+ };+ assert!(!multi);+ let labels: Vec<&str> = options.iter().map(|(_, label)| label.as_str()).collect();+ assert_eq!(labels, ["Not started", "In progress", "Done"]);+ assert_eq!(column.name, "Status");+ assert_eq!(column.shape.kind(), ColumnKind::Select { multi: false });++ let in_progress = options[1].0;+ assert_eq!(+ cell(+ &NotionValue::Option("in progress".to_string()),+ &column,+ &PeopleMap::new()+ ),+ Some(CellValue::Options(vec![OptionRef::Id(in_progress)]))+ );+ assert_eq!(+ cell(+ &NotionValue::Option("Blocked".to_string()),+ &column,+ &PeopleMap::new()+ ),+ None,+ "a label Notion never declared has no option to point at"+ );+}++#[test]+fn columns_take_the_closest_macro_type_and_titles_and_relations_wait() {+ let kinds = [+ (NotionPropertyKind::Title, None),+ (+ NotionPropertyKind::Relation {+ data_source_id: None,+ },+ None,+ ),+ (+ NotionPropertyKind::Unsupported {+ notion_type: "button".to_string(),+ },+ None,+ ),+ (NotionPropertyKind::RichText, Some(ColumnKind::Text)),+ (NotionPropertyKind::Formula, Some(ColumnKind::Text)),+ (NotionPropertyKind::Email, Some(ColumnKind::Text)),+ (NotionPropertyKind::Files, Some(ColumnKind::Text)),+ (NotionPropertyKind::Number, Some(ColumnKind::Number)),+ (NotionPropertyKind::Checkbox, Some(ColumnKind::Boolean)),+ (NotionPropertyKind::Date, Some(ColumnKind::Date)),+ (NotionPropertyKind::CreatedTime, Some(ColumnKind::Date)),+ (NotionPropertyKind::Url, Some(ColumnKind::Link)),+ (+ NotionPropertyKind::MultiSelect {+ options: vec![option("a")],+ },+ Some(ColumnKind::Select { multi: true }),+ ),+ (+ NotionPropertyKind::People,+ Some(ColumnKind::Entity {+ target: EntityKind::User,+ multi: true,+ }),+ ),+ (+ NotionPropertyKind::CreatedBy,+ Some(ColumnKind::Entity {+ target: EntityKind::User,+ multi: false,+ }),+ ),+ ];+ for (kind, expected) in kinds {+ let property = NotionPropertySchema {+ id: "p".to_string(),+ name: "Column".to_string(),+ kind: kind.clone(),+ };+ assert_eq!(+ planned_column(+ &property,+ &ColumnNames::new(std::slice::from_ref(&property)),+ Uuid::from_u128(500)+ )+ .map(|column| column.shape.kind()),+ expected,+ "{kind:?}"+ );+ }+}++#[test]+fn people_cells_hold_only_notion_users_matched_to_team_members() {+ let alice = NotionId::parse("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").unwrap();+ let outsider = NotionId::parse("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb").unwrap();+ let alice_user = MacroUserIdStr::parse_from_str("macro|[email protected]")+ .unwrap()+ .into_owned();+ let people: PeopleMap = [+ (+ alice.clone(),+ Person {+ email: Some("[email protected]".to_string()),+ macro_user: Some(alice_user),+ },+ ),+ (+ outsider.clone(),+ Person {+ email: Some("[email protected]".to_string()),+ macro_user: None,+ },+ ),+ ]+ .into_iter()+ .collect();+ let owner = NotionPropertySchema {+ id: "own".to_string(),+ name: "Owner".to_string(),+ kind: NotionPropertyKind::People,+ };+ let column = planned_column(+ &owner,+ &ColumnNames::new(std::slice::from_ref(&owner)),+ Uuid::from_u128(500),+ )+ .unwrap();++ assert_eq!(+ cell(+ &NotionValue::People(vec![alice, outsider]),+ &column,+ &people+ ),+ Some(CellValue::Entities(vec![EntityRef {+ entity_type: EntityKind::User,+ entity_id: "macro|[email protected]".to_string(),+ }]))+ );+}++#[test]+fn dates_and_timestamps_become_instants_and_bare_dates_are_midnight_utc() {+ assert_eq!(+ date(&NotionValue::Date {+ start: "2026-09-15".to_string(),+ end: None,+ }),+ Some(Utc.with_ymd_and_hms(2026, 9, 15, 0, 0, 0).unwrap())+ );+ assert_eq!(+ date(&NotionValue::Timestamp(+ "2026-09-15T14:30:00.000Z".to_string()+ )),+ Some(Utc.with_ymd_and_hms(2026, 9, 15, 14, 30, 0).unwrap())+ );+ assert_eq!(date(&NotionValue::Text("next week".to_string())), None);+}++#[test]+fn text_snapshots_render_every_value_a_reader_would_recognize() {+ assert_eq!(text(&NotionValue::Number(16.0)), Some("16".to_string()));+ assert_eq!(text(&NotionValue::Number(2.5)), Some("2.5".to_string()));+ assert_eq!(+ text(&NotionValue::Options(vec![+ "Q3".to_string(),+ "Infra".to_string()+ ])),+ Some("Q3, Infra".to_string())+ );+ assert_eq!(+ text(&NotionValue::Date {+ start: "2026-09-01".to_string(),+ end: Some("2026-09-05".to_string()),+ }),+ Some("2026-09-01 → 2026-09-05".to_string())+ );+ assert_eq!(text(&NotionValue::Checkbox(true)), Some("Yes".to_string()));+ assert_eq!(text(&NotionValue::Text(" ".to_string())), None);+ assert_eq!(text(&NotionValue::Empty), None);+}++#[test]+fn a_page_keeps_its_tags_and_typed_properties_as_document_properties() {+ let page = row(vec![+ (+ "Tags",+ NotionValue::Options(vec!["roadmap".to_string(), "q3".to_string()]),+ ),+ ("Reviewed", NotionValue::Checkbox(true)),+ ("Effort", NotionValue::Number(3.0)),+ (+ "Due",+ NotionValue::Date {+ start: "2026-10-01".to_string(),+ end: None,+ },+ ),+ ("Area", NotionValue::Option("Infra".to_string())),+ (+ "Spec",+ NotionValue::Url("https://acme.test/spec".to_string()),+ ),+ ("Notes", NotionValue::Text("Ask legal".to_string())),+ ("Related", NotionValue::Relation(Vec::new())),+ ]);++ let properties = document_properties(&page);++ assert_eq!(properties.tags, ["q3", "roadmap"]);+ assert_eq!(+ properties.values,+ vec![+ ImportedDocumentProperty {+ name: "Area".to_string(),+ value: ImportedDocumentPropertyValue::Select {+ values: vec!["Infra".to_string()],+ multi: false,+ },+ },+ ImportedDocumentProperty {+ name: "Due".to_string(),+ value: ImportedDocumentPropertyValue::Date {+ value: "2026-10-01".to_string(),+ },+ },+ ImportedDocumentProperty {+ name: "Effort".to_string(),+ value: ImportedDocumentPropertyValue::Number { value: 3.0 },+ },+ ImportedDocumentProperty {+ name: "Notes".to_string(),+ value: ImportedDocumentPropertyValue::String {+ value: "Ask legal".to_string(),+ },+ },+ ImportedDocumentProperty {+ name: "Reviewed".to_string(),+ value: ImportedDocumentPropertyValue::Boolean { value: true },+ },+ ImportedDocumentProperty {+ name: "Spec".to_string(),+ value: ImportedDocumentPropertyValue::Link {+ urls: vec!["https://acme.test/spec".to_string()],+ multi: false,+ },+ },+ ]+ );+}++#[test]+fn a_task_list_row_maps_its_named_columns_onto_task_fields_and_keeps_the_rest_in_the_body() {+ let alice = NotionId::parse("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").unwrap();+ let people: PeopleMap = [(+ alice.clone(),+ Person {+ email: Some("[email protected]".to_string()),+ macro_user: None,+ },+ )]+ .into_iter()+ .collect();+ let task = row(vec![+ ("Stage", NotionValue::Option("In progress".to_string())),+ ("Urgency", NotionValue::Option("High".to_string())),+ (+ "Deadline",+ NotionValue::Date {+ start: "2026-11-02T17:00:00.000Z".to_string(),+ end: None,+ },+ ),+ ("Owner", NotionValue::People(vec![alice])),+ ("Sprint", NotionValue::Option("Sprint 12".to_string())),+ ]);+ let fields = NotionTaskFields {+ status: Some("Stage".to_string()),+ priority: Some("Urgency".to_string()),+ due: Some("Deadline".to_string()),+ assignee: Some("Owner".to_string()),+ };++ assert_eq!(+ task_properties(&task, &fields, &people),+ ImportedTaskProperties {+ status: Some("In Progress".to_string()),+ priority: Some("High".to_string()),+ due_date: Some("2026-11-02".to_string()),+ assignee_email: Some("[email protected]".to_string()),+ }+ );+ assert_eq!(+ task_body(&task, "Pair with Bo on the parser.", &fields, &people),+ "Pair with Bo on the parser.\n\n---\n\n- **Sprint:** Sprint 12"+ );+}++#[test]+fn blank_property_names_become_untitled_columns_distinct_from_the_others() {+ let property = |id: &str, name: &str, kind| NotionPropertySchema {+ id: id.to_string(),+ name: name.to_string(),+ kind,+ };+ let properties = vec![+ property("title", "Name", NotionPropertyKind::Title),+ property("empty", "", NotionPropertyKind::RichText),+ property("spaces", " ", NotionPropertyKind::Number),+ property("taken", "untitled", NotionPropertyKind::Checkbox),+ ];++ let names = ColumnNames::new(&properties);+ let columns: Vec<String> = properties+ .iter()+ .filter_map(|property| planned_column(property, &names, Uuid::from_u128(500)))+ .map(|column| column.name)+ .collect();++ assert_eq!(columns, ["Untitled 2", "Untitled 3", "untitled"]);+ assert_eq!(+ ColumnNames::new(&properties),+ names,+ "a resumed write names the blank columns the same way"+ );+}diff --git a/crates/import/src/domain/service/notion_workspace/write.rs b/crates/import/src/domain/service/notion_workspace/write.rsnew file mode 100644index 0000000000..339a2f297a--- /dev/null+++ b/crates/import/src/domain/service/notion_workspace/write.rs@@ -0,0 +1,444 @@+//! Writing an approved plan: folders mirroring the page tree, documents,+//! databases with their rows and relations, and task lists.+//!+//! A write can stop part way (a crash, a deploy) and resume in another+//! process, so it never creates the same thing twice: documents and tasks+//! get ids derived from their ledger row and Notion object, which Macro+//! refuses to create twice, and everything else is recorded as it is made+//! ([`Progress`]) and reused when the write resumes.++use std::collections::{HashMap, HashSet};+use std::sync::Mutex;++use futures::{FutureExt, StreamExt, stream};+use import_notion::domain::inventory::{all_rows, full_markdown};+use import_notion::domain::model::NotionId;+use import_notion::domain::ports::NotionApi;+use macro_user_id::user_id::MacroUserIdStr;+use uuid::Uuid;++use crate::domain::models::{+ ImportEntity, ImportKind, NotionDecision, NotionDocMeta, NotionTaskFields, WrittenKind,+ WrittenObject,+};+use crate::domain::ports::{+ NotionLinks, NotionPageLink, NotionUserLink, SharedEntity, WorkspaceWriter, WrittenObjects,+};++use super::values::{PeopleMap, capped_name, derived_id, task_body, task_properties};++mod database;+pub(super) use database::{WrittenDatabase, link_relations, recorded_database, write_database};++#[cfg(test)]+mod test;++/// Objects written at once.+pub(super) const WRITE_CONCURRENCY: usize = 4;+/// Row bodies fetched at once within one database.+const BODY_CONCURRENCY: usize = 3;+/// Name of the folder every workspace import writes into.+pub(super) const ROOT_FOLDER: &str = "Notion";++/// Ledger entity types for what an import creates.+pub(super) const DOCUMENT_ENTITY: &str = "md";+pub(super) const DATABASE_ENTITY: &str = "database";+pub(super) const FOLDER_ENTITY: &str = "project";++/// Set once the write must stop (the user cancelled, the process is shutting+/// down, or another process took the run over); long writes check it between+/// batches, rows and tasks so a stop takes effect within seconds.+#[derive(Debug, Default)]+pub(super) struct Cancel(std::sync::atomic::AtomicBool);++impl Cancel {+ /// Mark the import cancelled.+ pub fn stop(&self) {+ self.0.store(true, std::sync::atomic::Ordering::Relaxed);+ }++ /// Whether the import was cancelled.+ pub fn stopped(&self) -> bool {+ self.0.load(std::sync::atomic::Ordering::Relaxed)+ }++ /// Fail with "Cancelled" once the import was cancelled.+ pub fn ensure_running(&self) -> anyhow::Result<()> {+ anyhow::ensure!(!self.stopped(), "Cancelled");+ Ok(())+ }+}++/// A plan row ready to write.+#[derive(Debug, Clone, PartialEq)]+pub(super) struct WriteItem {+ /// The ledger row.+ pub import_id: Uuid,+ /// The ledger row it sits under, if any.+ pub parent: Option<Uuid>,+ /// The Notion object.+ pub notion_id: NotionId,+ /// A page or a data source.+ pub kind: ImportKind,+ /// The title.+ pub title: String,+ /// What to do with it.+ pub decision: NotionDecision,+ /// For a task list: which columns hold the task fields.+ pub task_fields: NotionTaskFields,+ /// Shared with the user's team once written.+ pub shared: bool,+ /// Whether the owner may change `shared`.+ pub shareable: bool,+ /// A teammate's copy shared with the team stands in for it.+ pub already_imported: bool,+}++impl WriteItem {+ /// Whether writing the plan creates something for this object, rather+ /// than linking a teammate's copy.+ pub fn writes(&self) -> bool {+ matches!(self.decision, NotionDecision::Copy | NotionDecision::Tasks)+ && !self.already_imported+ }++ /// Read a staged ledger row; `None` for rows that are not plan items, or+ /// not decided yet.+ pub fn from_entity(entity: &ImportEntity) -> Option<Self> {+ let meta: NotionDocMeta = serde_json::from_value(entity.metadata.clone()).ok()?;+ let plan = meta.plan?;+ Some(Self {+ import_id: entity.id,+ parent: entity.parent_id,+ notion_id: NotionId::parse(&entity.foreign_id)?,+ kind: entity.kind,+ title: meta.title,+ decision: plan.decision?,+ task_fields: plan.task_fields.unwrap_or_default(),+ shared: plan.shared,+ shareable: plan.shareable,+ already_imported: plan.already_imported.is_some(),+ })+ }+}++/// Which folder each written object goes in.+#[derive(Debug, Clone, PartialEq, Default)]+pub(super) struct FolderLayout {+ /// Pages that become folders (they have something under them), in the+ /// order to create them: parents first.+ pub folders: Vec<Uuid>,+}++/// Plan rows that have something being written under them, in an order+/// where every such row's parents come first.+pub(super) fn folder_layout(items: &[WriteItem]) -> FolderLayout {+ let by_id: HashMap<Uuid, &WriteItem> =+ items.iter().map(|item| (item.import_id, item)).collect();+ let mut needs_folder: HashSet<Uuid> = HashSet::new();+ for item in items {+ // Every page above a written object becomes a folder, so the tree+ // survives even when a page in the middle is not written itself.+ let mut current = item.parent;+ let mut guard = 0;+ while let Some(parent) = current {+ guard += 1;+ if guard > items.len() || !needs_folder.insert(parent) {+ break;+ }+ current = by_id.get(&parent).and_then(|parent| parent.parent);+ }+ }+ let mut ordered: Vec<Uuid> = needs_folder.into_iter().collect();+ ordered.sort_by_key(|id| depth(*id, &by_id));+ FolderLayout { folders: ordered }+}++fn depth(id: Uuid, by_id: &HashMap<Uuid, &WriteItem>) -> usize {+ let mut depth = 0;+ let mut current = by_id.get(&id).and_then(|item| item.parent);+ while let Some(parent) = current {+ depth += 1;+ if depth > by_id.len() {+ break;+ }+ current = by_id.get(&parent).and_then(|item| item.parent);+ }+ depth+}++/// The folder `item` goes in: its own when it became one, otherwise its+/// nearest folder above, otherwise the import's root.+pub(super) fn folder_for(+ item: &WriteItem,+ own: bool,+ folders: &HashMap<Uuid, Uuid>,+ parents: &HashMap<Uuid, Option<Uuid>>,+ root: Uuid,+) -> Uuid {+ if own && let Some(folder) = folders.get(&item.import_id) {+ return *folder;+ }+ let mut current = item.parent;+ let mut guard = 0;+ while let Some(parent) = current {+ if let Some(folder) = folders.get(&parent) {+ return *folder;+ }+ guard += 1;+ if guard > parents.len() {+ break;+ }+ current = parents.get(&parent).copied().flatten();+ }+ root+}++/// Where the plan's pages and people resolve in Macro: every page gets the+/// id of the document it becomes (`imported` when it already exists,+/// otherwise [`document_id`], known before anything is written), and every+/// person matched to a teammate their account.+pub(super) fn notion_links(+ items: &[WriteItem],+ imported: &HashMap<NotionId, Uuid>,+ people: &PeopleMap,+) -> NotionLinks {+ let pages = items+ .iter()+ .filter(|item| item.kind != ImportKind::DataSource)+ .map(|item| {+ let link = NotionPageLink {+ document_id: imported+ .get(&item.notion_id)+ .copied()+ .unwrap_or_else(|| document_id(item.import_id)),+ name: item.title.clone(),+ };+ (item.notion_id.clone(), link)+ })+ .collect();+ let users = people+ .iter()+ .filter_map(|(id, person)| {+ let link = NotionUserLink {+ user_id: person.macro_user.clone()?,+ email: person.email.clone()?,+ };+ Some((id.clone(), link))+ })+ .collect();+ NotionLinks { pages, users }+}++/// The document plan row `import_id` becomes, the same on every attempt.+pub(super) fn document_id(import_id: Uuid) -> Uuid {+ derived_id(import_id, "document")+}++/// What earlier attempts created for one ledger row, and the record of what+/// this one creates.+pub(super) struct Progress<'a, Store> {+ store: &'a Store,+ user: &'a MacroUserIdStr<'static>,+ import_id: Uuid,+ known: Mutex<HashMap<(WrittenKind, String), Uuid>>,+}++impl<'a, Store: WrittenObjects> Progress<'a, Store> {+ /// Progress for ledger row `import_id`, from everything recorded so far.+ pub fn new(+ store: &'a Store,+ user: &'a MacroUserIdStr<'static>,+ import_id: Uuid,+ written: &[WrittenObject],+ ) -> Self {+ let known = written+ .iter()+ .filter(|object| object.import_id == import_id)+ .map(|object| ((object.kind, object.object.clone()), object.macro_id))+ .collect();+ Self {+ store,+ user,+ import_id,+ known: Mutex::new(known),+ }+ }++ /// The row's own object of `kind`, when an earlier attempt made it.+ pub fn get(&self, kind: WrittenKind) -> Option<Uuid> {+ self.get_for(kind, "")+ }++ /// The object of `kind` for Notion `object`, when an earlier attempt+ /// made it.+ pub fn get_for(&self, kind: WrittenKind, object: &str) -> Option<Uuid> {+ self.known+ .lock()+ .ok()?+ .get(&(kind, object.to_string()))+ .copied()+ }++ /// Every object of `kind` made so far, by Notion object.+ pub fn all(&self, kind: WrittenKind) -> HashMap<String, Uuid> {+ self.known+ .lock()+ .map(|known| {+ known+ .iter()+ .filter(|((known_kind, _), _)| *known_kind == kind)+ .map(|((_, object), id)| (object.clone(), *id))+ .collect()+ })+ .unwrap_or_default()+ }++ /// Record the row's own object of `kind`.+ pub async fn record(&self, kind: WrittenKind, macro_id: Uuid) -> anyhow::Result<()> {+ self.record_all(vec![WrittenObject::own(self.import_id, kind, macro_id)])+ .await+ }++ /// Record the object of `kind` for Notion row `row`.+ pub async fn record_for(+ &self,+ kind: WrittenKind,+ row: &NotionId,+ macro_id: Uuid,+ ) -> anyhow::Result<()> {+ self.record_all(vec![WrittenObject::for_row(+ self.import_id,+ kind,+ row,+ macro_id,+ )])+ .await+ }++ /// Record `objects`, which must belong to this row.+ pub async fn record_all(&self, objects: Vec<WrittenObject>) -> anyhow::Result<()> {+ let recorded = self.store.record_written(self.user, &objects).await?;+ if let Ok(mut known) = self.known.lock() {+ known.extend(+ recorded+ .into_iter()+ .map(|object| ((object.kind, object.object), object.macro_id)),+ );+ }+ Ok(())+ }+}++/// A folder made once for the row: reused when an earlier attempt made it,+/// otherwise created and recorded.+pub(super) async fn folder_once<Writer: WorkspaceWriter, Store: WrittenObjects>(+ writer: &Writer,+ user: &MacroUserIdStr<'static>,+ progress: &Progress<'_, Store>,+ kind: WrittenKind,+ name: &str,+ parent: Uuid,+) -> anyhow::Result<Uuid> {+ if let Some(folder) = progress.get(kind) {+ return Ok(folder);+ }+ let folder = writer.create_folder(user, name, Some(parent)).await?;+ progress.record(kind, folder).await?;+ Ok(folder)+}++/// Write a task list: a folder of Macro tasks, one per row, shared with the+/// team with `share`. Returns the folder id. Rows an earlier attempt already+/// wrote are skipped.+#[expect(+ clippy::too_many_arguments,+ reason = "a task list needs its source, destination, people, links and progress"+)]+pub(super) async fn write_tasks<Api: NotionApi, Writer: WorkspaceWriter, Store: WrittenObjects>(+ api: &Api,+ writer: &Writer,+ user: &MacroUserIdStr<'static>,+ item: &WriteItem,+ folder: Uuid,+ share: bool,+ people: &PeopleMap,+ links: &NotionLinks,+ progress: &Progress<'_, Store>,+ cancel: &Cancel,+) -> anyhow::Result<Uuid> {+ let rows = all_rows(api, &item.notion_id).await?;+ let tasks_folder = folder_once(+ writer,+ user,+ progress,+ WrittenKind::TasksFolder,+ &capped_name(&item.title),+ folder,+ )+ .await?;+ // Shared before the tasks are made, so each is shared as it lands in+ // the folder; shared again on resume, which is harmless.+ if share {+ writer+ .share_with_team(user, SharedEntity::Folder(tasks_folder))+ .await?;+ }+ let failures = Mutex::new(0usize);+ stream::iter(rows.iter().cloned())+ .map(|row| {+ let failures = &failures;+ async move {+ if cancel.stopped()+ || progress+ .get_for(WrittenKind::Task, row.id.as_str())+ .is_some()+ {+ return;+ }+ let outcome: anyhow::Result<()> = async {+ let content = full_markdown(api, &row.id).await?;+ let body = task_body(&row, &content, &item.task_fields, people);+ let properties = task_properties(&row, &item.task_fields, people);+ let title = if row.title.trim().is_empty() {+ super::plan::UNTITLED_PAGE+ } else {+ row.title.trim()+ };+ let id = derived_id(item.import_id, &format!("task/{}", row.id.as_str()));+ writer+ .create_task(+ user,+ id,+ title,+ &body,+ links,+ &properties,+ Some(tasks_folder),+ )+ .await?;+ progress.record_for(WrittenKind::Task, &row.id, id).await?;+ Ok(())+ }+ .await;+ if let Err(error) = outcome {+ tracing::warn!(error = ?error, "notion task row was not imported");+ if let Ok(mut failed) = failures.lock() {+ *failed += 1;+ }+ }+ }+ .boxed()+ })+ .buffer_unordered(BODY_CONCURRENCY)+ .collect::<Vec<()>>()+ .await;+ cancel.ensure_running()?;+ let failed = failures.into_inner().unwrap_or_default();+ anyhow::ensure!(+ failed < rows.len() || rows.is_empty(),+ "none of the {} task rows could be imported",+ rows.len()+ );+ Ok(tasks_folder)+}diff --git a/crates/import/src/domain/service/notion_workspace/write/database.rs b/crates/import/src/domain/service/notion_workspace/write/database.rsnew file mode 100644index 0000000000..1f388dcfac--- /dev/null+++ b/crates/import/src/domain/service/notion_workspace/write/database.rs@@ -0,0 +1,787 @@+//! Writing a data source as a database: its schema, rows, row bodies, and+//! relations once every database exists. Every step is recorded as it+//! completes, so a resumed write picks up where the last attempt stopped.++use std::collections::{HashMap, HashSet};++use futures::{FutureExt, StreamExt, stream};+use import_notion::domain::inventory::{all_rows, full_markdown};+use import_notion::domain::model::{+ NotionDataSource, NotionId, NotionPage, NotionPropertyKind, NotionPropertySchema, NotionValue,+};+use import_notion::domain::ports::NotionApi;+use macro_user_id::user_id::MacroUserIdStr;+use models_databases::{+ CellValue, CellWrite, ColumnChange, ColumnId, ColumnKind, DatabaseId, DatabaseOp, EntityKind,+ EntityRef, NewColumn, NewOption, OpResult, RowChange, RowChanges, RowId, RowsChange,+ RowsResult, TableChange, TableId,+};+use uuid::Uuid;++use super::{BODY_CONCURRENCY, Cancel, Progress, WriteItem, folder_once};+use crate::domain::models::{WrittenKind, WrittenObject};+use crate::domain::ports::{+ ImportedDocumentProperties, NewDatabase, NotionLinks, SharedEntity, WorkspaceWriter,+ WrittenObjects,+};++use super::super::values::{+ ColumnNames, ColumnShape, PeopleMap, PlannedColumn, capped_name, cell, derived_id,+ planned_column,+};++#[cfg(test)]+mod test;++/// Rows per insert batch, well under the 10,000-row batch limit.+const ROWS_PER_BATCH: usize = 500;+/// Name of the column linking a database row to its page body.+const PAGE_COLUMN: &str = "Page";++/// One row of a written database, for linking relations afterwards.+#[derive(Debug, Clone, Copy, PartialEq, Eq)]+pub(in super::super) struct WrittenRow {+ /// The database.+ pub database: DatabaseId,+ /// Its table.+ pub table: TableId,+ /// The row.+ pub row: RowId,+}++/// A relation column waiting until every table exists.+#[derive(Debug, Clone, PartialEq)]+pub(in super::super) struct PendingRelation {+ /// The database holding the column.+ pub database: Uuid,+ /// Its table.+ pub table: TableId,+ /// The column name.+ pub name: String,+ /// The data source the relation points at.+ pub target: Option<NotionId>,+ /// Each written row's related Notion pages.+ pub links: Vec<(RowId, Vec<NotionId>)>,+}++/// What writing one data source produced.+#[derive(Debug, Clone, PartialEq, Default)]+pub(in super::super) struct WrittenDatabase {+ /// The ledger row it was written for.+ pub import_id: Uuid,+ /// The Macro database.+ pub database_id: Uuid,+ /// The data source's table in it.+ pub table: Option<TableId>,+ /// Notion row id → written row.+ pub rows: HashMap<NotionId, WrittenRow>,+ /// Relations to link once every table exists.+ pub relations: Vec<PendingRelation>,+ /// Relation columns an earlier attempt created, by name.+ pub relations_created: HashSet<String>,+ /// Relation columns an earlier attempt finished linking, by name.+ pub relations_linked: HashSet<String>,+}++/// Write one data source as a database: columns from its schema, every row,+/// each row's body as a linked document, and relations left pending. With+/// `share`, the database and its row bodies' folder are shared with the+/// team. What an earlier attempt wrote (per `progress`) is reused, not+/// written again.+#[expect(+ clippy::too_many_arguments,+ reason = "a database needs its source, destination, people, links and progress"+)]+pub(in super::super) async fn write_database<+ Api: NotionApi,+ Writer: WorkspaceWriter,+ Store: WrittenObjects,+>(+ api: &Api,+ writer: &Writer,+ user: &MacroUserIdStr<'static>,+ item: &WriteItem,+ folder: Uuid,+ share: bool,+ people: &PeopleMap,+ links: &NotionLinks,+ progress: &Progress<'_, Store>,+ cancel: &Cancel,+) -> anyhow::Result<WrittenDatabase> {+ let source = api.data_source(&item.notion_id).await?;+ let created = database_once(writer, user, item, progress).await?;+ // Shared before its rows are written; shared again on resume, which is+ // harmless.+ if share {+ writer+ .share_with_team(user, SharedEntity::Database(created.database_id))+ .await?;+ }+ let table = created.table;+ let names = ColumnNames::new(&source.properties);+ let columns: Vec<PlannedColumn> = source+ .properties+ .iter()+ .filter_map(|property| planned_column(property, &names, created.database_id))+ .collect();+ let page_column = PageColumn {+ id: ColumnId::from_uuid(derived_id(created.database_id, "column/page")),+ name: page_column_name(&source.properties),+ // First, then the title and the data source's own columns.+ order: std::iter::once(created.title_column)+ .chain(columns.iter().map(|column| column.id))+ .collect(),+ };++ if progress.get(WrittenKind::Schema).is_none() {+ writer+ .apply_database_ops(+ user,+ created.database_id,+ schema_ops(item, &source, &names, &created, &columns),+ )+ .await?;+ progress+ .record(WrittenKind::Schema, created.database_id)+ .await?;+ }++ let rows = all_rows(api, &item.notion_id).await?;+ let database = DatabaseId::from_uuid(created.database_id);+ let mut written = WrittenDatabase {+ import_id: item.import_id,+ database_id: created.database_id,+ table: Some(table),+ rows: recorded_rows(progress, database, table),+ ..WrittenDatabase::default()+ };+ let unwritten: Vec<NotionPage> = rows+ .iter()+ .filter(|row| !written.rows.contains_key(&row.id))+ .cloned()+ .collect();+ for batch in owned_batches(&unwritten) {+ cancel.ensure_running()?;+ let cells: Vec<Vec<CellWrite>> = batch+ .iter()+ .map(|row| row_cells(row, created.title_column, &columns, people))+ .collect();+ let results = writer+ .apply_database_ops(+ user,+ created.database_id,+ vec![DatabaseOp::Rows {+ table,+ change: RowsChange::Insert { rows: cells },+ }],+ )+ .await?;+ let ids = inserted_rows(&results);+ anyhow::ensure!(+ ids.len() == batch.len(),+ "database insert returned {} rows for {} written",+ ids.len(),+ batch.len()+ );+ progress+ .record_all(+ batch+ .iter()+ .zip(&ids)+ .map(|(row, id)| {+ WrittenObject::for_row(+ item.import_id,+ WrittenKind::Row,+ &row.id,+ id.into_uuid(),+ )+ })+ .collect(),+ )+ .await?;+ for (row, id) in batch.iter().zip(ids) {+ written.rows.insert(+ row.id.clone(),+ WrittenRow {+ database,+ table,+ row: id,+ },+ );+ }+ }++ written.relations = pending_relations(&source, &rows, &written);++ write_row_bodies(+ api,+ writer,+ user,+ item,+ folder,+ share,+ &rows,+ &written,+ page_column,+ links,+ progress,+ cancel,+ )+ .await?;+ Ok(written)+}++/// The database an earlier attempt created, or a new one, recorded before+/// anything is written into it.+async fn database_once<Writer: WorkspaceWriter, Store: WrittenObjects>(+ writer: &Writer,+ user: &MacroUserIdStr<'static>,+ item: &WriteItem,+ progress: &Progress<'_, Store>,+) -> anyhow::Result<NewDatabase> {+ if let (Some(database_id), Some(table), Some(title_column)) = (+ progress.get(WrittenKind::Database),+ progress.get(WrittenKind::Table),+ progress.get(WrittenKind::TitleColumn),+ ) {+ return Ok(NewDatabase {+ database_id,+ table: TableId::from_uuid(table),+ title_column: ColumnId::from_uuid(title_column),+ });+ }+ let created = writer+ .create_database(user, &capped_name(&item.title))+ .await?;+ progress+ .record_all(vec![+ WrittenObject::own(item.import_id, WrittenKind::Database, created.database_id),+ WrittenObject::own(+ item.import_id,+ WrittenKind::Table,+ created.table.into_uuid(),+ ),+ WrittenObject::own(+ item.import_id,+ WrittenKind::TitleColumn,+ created.title_column.into_uuid(),+ ),+ ])+ .await?;+ Ok(created)+}++/// Rename the starter table and title column and create the data source's+/// columns, in order.+fn schema_ops(+ item: &WriteItem,+ source: &NotionDataSource,+ names: &ColumnNames,+ created: &NewDatabase,+ columns: &[PlannedColumn],+) -> Vec<DatabaseOp> {+ let table = created.table;+ let mut ops = vec![DatabaseOp::Table {+ table,+ change: TableChange::Rename {+ name: capped_name(&item.title),+ previous_name: None,+ },+ }];+ if let Some(title) = source+ .properties+ .iter()+ .find(|property| property.kind == NotionPropertyKind::Title)+ {+ ops.push(DatabaseOp::Column {+ table,+ column: created.title_column,+ change: ColumnChange::Rename {+ name: names.of(title),+ previous_name: None,+ },+ });+ }+ let mut after = created.title_column;+ for column in columns {+ ops.push(DatabaseOp::Column {+ table,+ column: column.id,+ change: ColumnChange::Create {+ definition: NewColumn::New {+ name: column.name.clone(),+ kind: column.shape.kind(),+ options: match &column.shape {+ ColumnShape::Select { options, .. } => options+ .iter()+ .map(|(id, label)| NewOption {+ id: *id,+ label: label.clone(),+ })+ .collect(),+ _ => Vec::new(),+ },+ infer_type: false,+ },+ after: Some(after),+ },+ });+ after = column.id;+ }+ ops+}++/// The rows earlier attempts inserted, by Notion row.+fn recorded_rows<Store: WrittenObjects>(+ progress: &Progress<'_, Store>,+ database: DatabaseId,+ table: TableId,+) -> HashMap<NotionId, WrittenRow> {+ progress+ .all(WrittenKind::Row)+ .into_iter()+ .filter_map(|(notion, row)| {+ let written = WrittenRow {+ database,+ table,+ row: RowId::from_uuid(row),+ };+ Some((NotionId::parse(¬ion)?, written))+ })+ .collect()+}++/// Each relation property, with the written rows' related Notion pages.+fn pending_relations(+ source: &NotionDataSource,+ rows: &[NotionPage],+ written: &WrittenDatabase,+) -> Vec<PendingRelation> {+ let Some(table) = written.table else {+ return Vec::new();+ };+ let names = ColumnNames::new(&source.properties);+ source+ .properties+ .iter()+ .filter_map(|property| {+ let NotionPropertyKind::Relation { data_source_id } = &property.kind else {+ return None;+ };+ let links = rows+ .iter()+ .filter_map(|row| {+ let related = match row.properties.get(&property.name)? {+ NotionValue::Relation(ids) => ids.clone(),+ _ => return None,+ };+ let written_row = written.rows.get(&row.id)?;+ (!related.is_empty()).then_some((written_row.row, related))+ })+ .collect();+ Some(PendingRelation {+ database: written.database_id,+ table,+ name: names.of(property),+ target: data_source_id.clone(),+ links,+ })+ })+ .collect()+}++/// A database an earlier attempt finished writing, rebuilt from what it+/// recorded so relations to it (and its own, if not yet linked) can still+/// be linked. `None` when it was never created.+pub(in super::super) async fn recorded_database<Api: NotionApi, Store: WrittenObjects>(+ api: &Api,+ item: &WriteItem,+ progress: &Progress<'_, Store>,+) -> anyhow::Result<Option<WrittenDatabase>> {+ let (Some(database_id), Some(table)) = (+ progress.get(WrittenKind::Database),+ progress.get(WrittenKind::Table),+ ) else {+ return Ok(None);+ };+ let table = TableId::from_uuid(table);+ let mut written = WrittenDatabase {+ import_id: item.import_id,+ database_id,+ table: Some(table),+ rows: recorded_rows(progress, DatabaseId::from_uuid(database_id), table),+ relations_created: progress.all(WrittenKind::Relation).into_keys().collect(),+ relations_linked: progress+ .all(WrittenKind::RelationLinked)+ .into_keys()+ .collect(),+ ..WrittenDatabase::default()+ };+ let source = api.data_source(&item.notion_id).await?;+ let names = ColumnNames::new(&source.properties);+ let unlinked = source.properties.iter().any(|property| {+ matches!(property.kind, NotionPropertyKind::Relation { .. })+ && !written.relations_linked.contains(&names.of(property))+ });+ // Only a database with relations left to link needs its rows again.+ if unlinked {+ let rows = all_rows(api, &item.notion_id).await?;+ written.relations = pending_relations(&source, &rows, &written);+ }+ Ok(Some(written))+}++fn row_cells(+ row: &NotionPage,+ title_column: ColumnId,+ columns: &[PlannedColumn],+ people: &PeopleMap,+) -> Vec<CellWrite> {+ let mut cells = Vec::with_capacity(columns.len() + 1);+ if !row.title.trim().is_empty() {+ cells.push(CellWrite {+ column: title_column,+ value: CellValue::Text(row.title.trim().to_string()),+ });+ }+ for column in columns {+ if let Some(value) = row+ .properties+ .get(&column.property)+ .and_then(|value| cell(value, column, people))+ {+ cells.push(CellWrite {+ column: column.id,+ value,+ });+ }+ }+ cells+}++fn inserted_rows(results: &[OpResult]) -> Vec<RowId> {+ results+ .iter()+ .filter_map(|result| match result {+ OpResult::Rows {+ change: RowsResult::Inserted { rows },+ ..+ } => Some(rows.clone()),+ _ => None,+ })+ .flatten()+ .collect()+}++/// Rows with page content get a document in a folder beside the database,+/// linked from the row's Page column, and shared with the database. Bodies+/// an earlier attempt wrote are linked without fetching them again.+#[expect(+ clippy::too_many_arguments,+ reason = "row bodies need the rows, their database, links and progress"+)]+async fn write_row_bodies<Api: NotionApi, Writer: WorkspaceWriter, Store: WrittenObjects>(+ api: &Api,+ writer: &Writer,+ user: &MacroUserIdStr<'static>,+ item: &WriteItem,+ folder: Uuid,+ share: bool,+ rows: &[NotionPage],+ written: &WrittenDatabase,+ page_column: PageColumn,+ links: &NotionLinks,+ progress: &Progress<'_, Store>,+ cancel: &Cancel,+) -> anyhow::Result<()> {+ let bodies_folder: tokio::sync::OnceCell<Uuid> = tokio::sync::OnceCell::new();+ let bodies_name = capped_name(&format!("{} pages", item.title));+ let linked: Vec<anyhow::Result<Option<RowChange>>> = stream::iter(rows.iter().cloned())+ .map(|row| {+ let bodies_folder = &bodies_folder;+ let bodies_name = &bodies_name;+ async move {+ cancel.ensure_running()?;+ let Some(written_row) = written.rows.get(&row.id) else {+ return Ok(None);+ };+ let change = |document: Uuid| RowChange {+ row: written_row.row,+ cells: vec![CellWrite {+ column: page_column.id,+ value: CellValue::Entities(vec![EntityRef {+ entity_type: EntityKind::Document,+ entity_id: document.to_string(),+ }]),+ }],+ };+ if let Some(document) = progress.get_for(WrittenKind::Body, row.id.as_str()) {+ return Ok(Some(change(document)));+ }+ let markdown = full_markdown(api, &row.id).await?;+ if markdown.trim().is_empty() {+ return Ok(None);+ }+ let folder = *bodies_folder+ .get_or_try_init(|| async {+ let bodies = folder_once(+ writer,+ user,+ progress,+ WrittenKind::BodiesFolder,+ bodies_name,+ folder,+ )+ .await?;+ // A shared database's row bodies are shared with it,+ // before any lands in the folder.+ if share {+ writer+ .share_with_team(user, SharedEntity::Folder(bodies))+ .await?;+ }+ anyhow::Ok(bodies)+ })+ .await?;+ let title = if row.title.trim().is_empty() {+ super::super::plan::UNTITLED_PAGE.to_string()+ } else {+ row.title.trim().to_string()+ };+ let id = derived_id(item.import_id, &format!("body/{}", row.id.as_str()));+ let document: Uuid = writer+ .create_document(+ user,+ id,+ &title,+ &markdown,+ links,+ &ImportedDocumentProperties::default(),+ Some(folder),+ )+ .await?+ .parse()?;+ progress+ .record_for(WrittenKind::Body, &row.id, document)+ .await?;+ Ok(Some(change(document)))+ }+ .boxed()+ })+ .buffer_unordered(BODY_CONCURRENCY)+ .collect()+ .await;+ let mut changes = Vec::new();+ for outcome in linked {+ match outcome {+ Ok(Some(change)) => changes.push(change),+ Ok(None) => {}+ // One unreadable row body never loses the row itself.+ Err(error) => tracing::warn!(error = ?error, "notion row body was not imported"),+ }+ }+ cancel.ensure_running()?;+ let Some(table) = written.table else {+ return Ok(());+ };+ if changes.is_empty() {+ return Ok(());+ }+ // Only a database whose rows have content gets the column, and it goes+ // first so the row's page is one click away.+ if progress.get(WrittenKind::PageColumn).is_none() {+ writer+ .apply_database_ops(+ user,+ written.database_id,+ vec![+ DatabaseOp::Column {+ table,+ column: page_column.id,+ change: ColumnChange::Create {+ definition: NewColumn::New {+ name: page_column.name.clone(),+ kind: ColumnKind::Entity {+ target: EntityKind::Document,+ multi: false,+ },+ options: Vec::new(),+ infer_type: false,+ },+ after: None,+ },+ },+ DatabaseOp::Table {+ table,+ change: TableChange::ReorderColumns {+ order: std::iter::once(page_column.id)+ .chain(page_column.order.iter().copied())+ .collect(),+ },+ },+ ],+ )+ .await?;+ progress+ .record(WrittenKind::PageColumn, page_column.id.into_uuid())+ .await?;+ }+ // Setting a cell again is harmless, so a resumed write fills them all.+ for batch in owned_batches(&changes) {+ cancel.ensure_running()?;+ writer+ .apply_database_ops(+ user,+ written.database_id,+ vec![DatabaseOp::Rows {+ table,+ change: RowsChange::Update {+ changes: RowChanges::PerRow { rows: batch },+ },+ }],+ )+ .await?;+ }+ Ok(())+}++/// The column linking each row to its page document.+#[derive(Debug, Clone)]+pub(in super::super) struct PageColumn {+ /// The column id, derived from the database so every attempt names it.+ id: ColumnId,+ /// Its name, distinct from the data source's own columns.+ name: String,+ /// The table's other columns, in order; the page column goes before them.+ order: Vec<ColumnId>,+}++/// "Page", or the first variant no Notion column of the same data source is+/// already called (case-insensitively).+pub(in super::super) fn page_column_name(properties: &[NotionPropertySchema]) -> String {+ let taken = |name: &str| {+ properties+ .iter()+ .any(|property| property.name.trim().eq_ignore_ascii_case(name))+ };+ std::iter::once(PAGE_COLUMN.to_string())+ .chain(std::iter::once(format!("{PAGE_COLUMN} (Notion)")))+ .chain((2..).map(|index| format!("{PAGE_COLUMN} {index}")))+ .find(|name| !taken(name))+ .unwrap_or_else(|| PAGE_COLUMN.to_string())+}++/// `items` in insert-sized batches, owned so no borrow is held across the+/// writes.+fn owned_batches<Item: Clone>(items: &[Item]) -> Vec<Vec<Item>> {+ items.chunks(ROWS_PER_BATCH).map(<[Item]>::to_vec).collect()+}++/// Link relations once every database exists: each relation whose target+/// data source was written becomes a relation column filled with the written+/// rows. A relation to something left in Notion is dropped. Columns an+/// earlier attempt created are reused, and finished ones skipped.+pub(in super::super) async fn link_relations<Writer: WorkspaceWriter, Store: WrittenObjects>(+ writer: &Writer,+ store: &Store,+ user: &MacroUserIdStr<'static>,+ databases: &HashMap<NotionId, WrittenDatabase>,+ cancel: &Cancel,+) -> anyhow::Result<()> {+ let rows: HashMap<&NotionId, WrittenRow> = databases+ .values()+ .flat_map(|database| database.rows.iter().map(|(id, row)| (id, *row)))+ .collect();+ for database in databases.values() {+ for relation in &database.relations {+ cancel.ensure_running()?;+ if database.relations_linked.contains(&relation.name) {+ continue;+ }+ let Some(target) = relation+ .target+ .as_ref()+ .and_then(|target| databases.get(target))+ else {+ continue;+ };+ let Some(target_table) = target.table else {+ continue;+ };+ let column = ColumnId::from_uuid(derived_id(+ relation.database,+ &format!("relation/{}", relation.name),+ ));+ let record = |kind| {+ let object = WrittenObject {+ import_id: database.import_id,+ kind,+ object: relation.name.clone(),+ macro_id: column.into_uuid(),+ };+ async move { store.record_written(user, &[object]).await }+ };+ if !database.relations_created.contains(&relation.name) {+ writer+ .apply_database_ops(+ user,+ relation.database,+ vec![DatabaseOp::Column {+ table: relation.table,+ column,+ change: ColumnChange::Create {+ definition: NewColumn::New {+ name: relation.name.clone(),+ kind: ColumnKind::Relation {+ database: DatabaseId::from_uuid(target.database_id),+ table: target_table,+ },+ options: Vec::new(),+ infer_type: false,+ },+ after: None,+ },+ }],+ )+ .await?;+ record(WrittenKind::Relation).await?;+ }+ let changes: Vec<RowChange> = relation+ .links+ .iter()+ .filter_map(|(row, related)| {+ let linked: Vec<RowId> = related+ .iter()+ .filter_map(|id| rows.get(id))+ .filter(|written| written.table == target_table)+ .map(|written| written.row)+ .collect();+ (!linked.is_empty()).then(|| RowChange {+ row: *row,+ cells: vec![CellWrite {+ column,+ value: CellValue::Rows(linked),+ }],+ })+ })+ .collect();+ for batch in owned_batches(&changes) {+ cancel.ensure_running()?;+ writer+ .apply_database_ops(+ user,+ relation.database,+ vec![DatabaseOp::Rows {+ table: relation.table,+ change: RowsChange::Update {+ changes: RowChanges::PerRow { rows: batch },+ },+ }],+ )+ .await?;+ }+ record(WrittenKind::RelationLinked).await?;+ }+ }+ Ok(())+}diff --git a/crates/import/src/domain/service/notion_workspace/write/database/test.rs b/crates/import/src/domain/service/notion_workspace/write/database/test.rsnew file mode 100644index 0000000000..35ca93ab2f--- /dev/null+++ b/crates/import/src/domain/service/notion_workspace/write/database/test.rs@@ -0,0 +1,887 @@+use std::collections::BTreeMap;+use std::sync::Mutex;++use chrono::{TimeZone, Utc};+use import_notion::domain::model::{+ NotionOption, NotionParent, NotionUser, PageMarkdown, Paged, SearchHit, SearchObject,+};+use import_notion::domain::ports::NotionError;+use macro_user_id::cowlike::CowLike;++use super::*;+use crate::domain::models::{ImportKind, NotionDecision, NotionTaskFields};+use crate::domain::ports::{ImportedTaskProperties, NotionPageLink, TeamMember};++fn id(hex: char) -> NotionId {+ NotionId::parse(&hex.to_string().repeat(32)).unwrap()+}++fn user() -> MacroUserIdStr<'static> {+ MacroUserIdStr::parse_from_str("macro|[email protected]")+ .unwrap()+ .into_owned()+}++fn source_item(title: &str) -> WriteItem {+ WriteItem {+ import_id: Uuid::from_u128(1),+ parent: None,+ notion_id: id('e'),+ kind: ImportKind::DataSource,+ title: title.to_string(),+ decision: NotionDecision::Copy,+ task_fields: NotionTaskFields::default(),+ shared: false,+ shareable: true,+ already_imported: false,+ }+}++/// What earlier attempts recorded, kept the way the ledger keeps it: the+/// first record of an object wins.+#[derive(Default)]+struct FakeStore(Mutex<Vec<WrittenObject>>);++impl WrittenObjects for FakeStore {+ async fn written(+ &self,+ _user: &MacroUserIdStr<'static>,+ import_ids: &[Uuid],+ ) -> crate::domain::ports::Result<Vec<WrittenObject>> {+ Ok(self+ .0+ .lock()+ .unwrap()+ .iter()+ .filter(|object| import_ids.contains(&object.import_id))+ .cloned()+ .collect())+ }++ async fn record_written(+ &self,+ _user: &MacroUserIdStr<'static>,+ objects: &[WrittenObject],+ ) -> crate::domain::ports::Result<Vec<WrittenObject>> {+ let mut stored = self.0.lock().unwrap();+ Ok(objects+ .iter()+ .map(|object| {+ let existing = stored.iter().find(|known| {+ known.import_id == object.import_id+ && known.kind == object.kind+ && known.object == object.object+ });+ match existing {+ Some(known) => known.clone(),+ None => {+ stored.push(object.clone());+ object.clone()+ }+ }+ })+ .collect())+ }+}++/// A Notion workspace holding one data source with two rows.+struct FakeNotion {+ source: NotionDataSource,+ rows: Vec<NotionPage>,+ bodies: HashMap<NotionId, String>,+ /// How many page bodies were read.+ fetched: Mutex<usize>,+}++impl NotionApi for FakeNotion {+ async fn search(+ &self,+ _object: SearchObject,+ _cursor: Option<String>,+ ) -> std::result::Result<Paged<SearchHit>, NotionError> {+ unimplemented!("writing never searches")+ }++ async fn page(&self, _id: &NotionId) -> std::result::Result<NotionPage, NotionError> {+ unimplemented!("rows come from the query")+ }++ async fn page_markdown(&self, id: &NotionId) -> std::result::Result<PageMarkdown, NotionError> {+ *self.fetched.lock().unwrap() += 1;+ Ok(PageMarkdown {+ markdown: self.bodies.get(id).cloned().unwrap_or_default(),+ unknown_block_ids: Vec::new(),+ })+ }++ async fn data_source(+ &self,+ _id: &NotionId,+ ) -> std::result::Result<NotionDataSource, NotionError> {+ Ok(self.source.clone())+ }++ async fn database_sources(+ &self,+ _database: &NotionId,+ ) -> std::result::Result<Vec<NotionId>, NotionError> {+ unimplemented!("writing never expands databases")+ }++ async fn query_data_source(+ &self,+ _id: &NotionId,+ _cursor: Option<String>,+ ) -> std::result::Result<Paged<NotionPage>, NotionError> {+ Ok(Paged {+ results: self.rows.clone(),+ next_cursor: None,+ })+ }++ async fn users(+ &self,+ _cursor: Option<String>,+ ) -> std::result::Result<Paged<NotionUser>, NotionError> {+ unimplemented!("people come in as a map")+ }+}++/// A document the import asked for.+struct WrittenDocument {+ id: Uuid,+ name: String,+ markdown: String,+ links: NotionLinks,+}++/// Records what the import asks Macro to create. `fail_at_batch` makes that+/// database write (counting from 0) fail, the way a crash part way would+/// leave it.+#[derive(Default)]+struct FakeWriter {+ databases: Mutex<usize>,+ folders: Mutex<Vec<(String, Option<Uuid>)>>,+ documents: Mutex<Vec<WrittenDocument>>,+ batches: Mutex<Vec<Vec<DatabaseOp>>>,+ next_row: Mutex<u128>,+ fail_at_batch: Mutex<Option<usize>>,+ shares: Mutex<Vec<SharedEntity>>,+}++const DATABASE: Uuid = Uuid::from_u128(500);+const TABLE: Uuid = Uuid::from_u128(501);+const TITLE_COLUMN: Uuid = Uuid::from_u128(502);++impl WorkspaceWriter for FakeWriter {+ async fn create_folder(+ &self,+ _user: &MacroUserIdStr<'static>,+ name: &str,+ parent: Option<Uuid>,+ ) -> anyhow::Result<Uuid> {+ let mut folders = self.folders.lock().unwrap();+ folders.push((name.to_string(), parent));+ Ok(Uuid::from_u128(900 + folders.len() as u128))+ }++ async fn folder_exists(+ &self,+ _user: &MacroUserIdStr<'static>,+ _folder: Uuid,+ ) -> anyhow::Result<bool> {+ Ok(true)+ }++ async fn share_with_team(+ &self,+ _user: &MacroUserIdStr<'static>,+ entity: SharedEntity,+ ) -> anyhow::Result<()> {+ self.shares.lock().unwrap().push(entity);+ Ok(())+ }++ async fn create_document(+ &self,+ _user: &MacroUserIdStr<'static>,+ id: Uuid,+ name: &str,+ markdown: &str,+ links: &NotionLinks,+ _properties: &ImportedDocumentProperties,+ _folder: Option<Uuid>,+ ) -> anyhow::Result<String> {+ let mut documents = self.documents.lock().unwrap();+ // Like Macro: a document id already in use is the earlier attempt's.+ if !documents.iter().any(|document| document.id == id) {+ documents.push(WrittenDocument {+ id,+ name: name.to_string(),+ markdown: markdown.to_string(),+ links: links.clone(),+ });+ }+ Ok(id.to_string())+ }++ async fn create_task(+ &self,+ _user: &MacroUserIdStr<'static>,+ _id: Uuid,+ _name: &str,+ _markdown: &str,+ _links: &NotionLinks,+ _properties: &ImportedTaskProperties,+ _folder: Option<Uuid>,+ ) -> anyhow::Result<String> {+ unimplemented!("a copied database writes no tasks")+ }++ async fn create_database(+ &self,+ _user: &MacroUserIdStr<'static>,+ _name: &str,+ ) -> anyhow::Result<NewDatabase> {+ *self.databases.lock().unwrap() += 1;+ Ok(NewDatabase {+ database_id: DATABASE,+ table: TableId::from_uuid(TABLE),+ title_column: ColumnId::from_uuid(TITLE_COLUMN),+ })+ }++ async fn apply_database_ops(+ &self,+ _user: &MacroUserIdStr<'static>,+ _database_id: Uuid,+ ops: Vec<DatabaseOp>,+ ) -> anyhow::Result<Vec<OpResult>> {+ let mut batches = self.batches.lock().unwrap();+ if *self.fail_at_batch.lock().unwrap() == Some(batches.len()) {+ anyhow::bail!("the process stopped");+ }+ let mut next_row = self.next_row.lock().unwrap();+ let results = ops+ .iter()+ .filter_map(|op| match op {+ DatabaseOp::Rows {+ change: RowsChange::Insert { rows },+ ..+ } => Some(OpResult::Rows {+ table: TableId::from_uuid(TABLE),+ table_version: models_databases::TableVersion(1),+ change: RowsResult::Inserted {+ rows: rows+ .iter()+ .map(|_| {+ *next_row += 1;+ RowId::from_uuid(Uuid::from_u128(699 + *next_row))+ })+ .collect(),+ },+ }),+ _ => None,+ })+ .collect();+ batches.push(ops);+ Ok(results)+ }++ async fn team_members(+ &self,+ _user: &MacroUserIdStr<'static>,+ ) -> anyhow::Result<Vec<TeamMember>> {+ Ok(Vec::new())+ }+}++fn notion_row(hex: char, title: &str, properties: Vec<(&str, NotionValue)>) -> NotionPage {+ NotionPage {+ id: id(hex),+ title: title.to_string(),+ url: id(hex).url(),+ parent: NotionParent::DataSource {+ data_source: id('e'),+ database: id('f'),+ },+ last_edited: Utc.with_ymd_and_hms(2026, 10, 1, 9, 0, 0).unwrap(),+ created: Utc.with_ymd_and_hms(2026, 9, 1, 9, 0, 0).unwrap(),+ in_trash: false,+ properties: properties+ .into_iter()+ .map(|(name, value)| (name.to_string(), value))+ .collect::<BTreeMap<_, _>>(),+ }+}++#[tokio::test]+async fn a_data_source_becomes_a_database_with_typed_columns_rows_bodies_and_pending_relations() {+ let notion = FakeNotion {+ fetched: Mutex::new(0),+ source: NotionDataSource {+ id: id('e'),+ database_id: id('f'),+ title: "Bugs".to_string(),+ database_parent: NotionParent::Workspace,+ in_trash: false,+ properties: vec![+ NotionPropertySchema {+ id: "title".to_string(),+ name: "Bug".to_string(),+ kind: NotionPropertyKind::Title,+ },+ NotionPropertySchema {+ id: "sev".to_string(),+ name: "Severity".to_string(),+ kind: NotionPropertyKind::Select {+ options: vec![NotionOption {+ id: "o1".to_string(),+ name: "High".to_string(),+ color: "red".to_string(),+ }],+ },+ },+ NotionPropertySchema {+ id: "proj".to_string(),+ name: "Project".to_string(),+ kind: NotionPropertyKind::Relation {+ data_source_id: Some(id('9')),+ },+ },+ ],+ },+ rows: vec![+ notion_row(+ '1',+ "Login fails",+ vec![+ ("Severity", NotionValue::Option("High".to_string())),+ ("Project", NotionValue::Relation(vec![id('8')])),+ ],+ ),+ notion_row('2', "Typo on pricing", vec![]),+ ],+ bodies: [(id('1'), "Steps to reproduce:\n\n1. Sign in".to_string())]+ .into_iter()+ .collect(),+ };+ let writer = FakeWriter::default();+ let folder = Uuid::from_u128(42);+ let links = NotionLinks {+ pages: [(+ id('7'),+ NotionPageLink {+ document_id: Uuid::from_u128(70),+ name: "Roadmap".to_string(),+ },+ )]+ .into(),+ users: HashMap::new(),+ };+ let source_item = source_item("Bugs");+ let store = FakeStore::default();+ let user = user();+ let progress = Progress::new(&store, &user, source_item.import_id, &[]);++ let written = write_database(+ ¬ion,+ &writer,+ &user,+ &source_item,+ folder,+ false,+ &PeopleMap::new(),+ &links,+ &progress,+ &Cancel::default(),+ )+ .await+ .unwrap();++ let batches = writer.batches.lock().unwrap();+ // Schema first: rename the starter table and title and add the select;+ // the relation waits, and the Page column comes once a row has content.+ let schema = &batches[0];+ assert!(matches!(+ &schema[0],+ DatabaseOp::Table { change: TableChange::Rename { name, .. }, .. } if name == "Bugs"+ ));+ assert!(matches!(+ &schema[1],+ DatabaseOp::Column { change: ColumnChange::Rename { name, .. }, .. } if name == "Bug"+ ));+ let created: Vec<(String, ColumnKind)> = schema+ .iter()+ .filter_map(|op| match op {+ DatabaseOp::Column {+ change:+ ColumnChange::Create {+ definition: NewColumn::New { name, kind, .. },+ ..+ },+ ..+ } => Some((name.clone(), *kind)),+ _ => None,+ })+ .collect();+ assert_eq!(+ created,+ [("Severity".to_string(), ColumnKind::Select { multi: false })]+ );++ // Then the rows, title first.+ let DatabaseOp::Rows {+ change: RowsChange::Insert { rows },+ ..+ } = &batches[1][0]+ else {+ panic!("expected a row insert, got {:?}", batches[1]);+ };+ assert_eq!(rows.len(), 2);+ assert_eq!(+ rows[0][0],+ CellWrite {+ column: ColumnId::from_uuid(TITLE_COLUMN),+ value: CellValue::Text("Login fails".to_string()),+ }+ );+ assert!(matches!(rows[0][1].value, CellValue::Options(_)));+ assert_eq!(rows[1].len(), 1, "an empty row keeps only its title");++ // The row with a body got a document in a folder beside the database,+ // linked from its Page column.+ assert_eq!(+ writer.folders.lock().unwrap().as_slice(),+ [("Bugs pages".to_string(), Some(folder))]+ );+ let documents = writer.documents.lock().unwrap();+ assert_eq!(documents.len(), 1);+ assert_eq!(documents[0].name, "Login fails");+ assert!(documents[0].markdown.contains("Steps to reproduce"));+ assert_eq!(+ documents[0].links, links,+ "row bodies resolve the import's pages"+ );+ // The Page column is created first in the table...+ let DatabaseOp::Column {+ column: page_column,+ change:+ ColumnChange::Create {+ definition: NewColumn::New { name, kind, .. },+ ..+ },+ ..+ } = &batches[2][0]+ else {+ panic!("expected the Page column, got {:?}", batches[2]);+ };+ assert_eq!(name, "Page");+ assert_eq!(+ *kind,+ ColumnKind::Entity {+ target: EntityKind::Document,+ multi: false+ }+ );+ let DatabaseOp::Table {+ change: TableChange::ReorderColumns { order },+ ..+ } = &batches[2][1]+ else {+ panic!("expected the column order, got {:?}", batches[2]);+ };+ assert_eq!(order[0], *page_column);+ assert_eq!(order[1], ColumnId::from_uuid(TITLE_COLUMN));+ assert_eq!(order.len(), 3);+ // ...then filled with the row's document.+ let DatabaseOp::Rows {+ change:+ RowsChange::Update {+ changes: RowChanges::PerRow { rows: linked },+ },+ ..+ } = &batches[3][0]+ else {+ panic!("expected the Page links, got {:?}", batches[3]);+ };+ assert_eq!(linked.len(), 1);+ assert_eq!(linked[0].row, RowId::from_uuid(Uuid::from_u128(700)));+ assert_eq!(+ linked[0].cells[0].value,+ CellValue::Entities(vec![EntityRef {+ entity_type: EntityKind::Document,+ entity_id: derived_id(Uuid::from_u128(1), &format!("body/{}", id('1').as_str()))+ .to_string(),+ }])+ );++ assert_eq!(written.database_id, DATABASE);+ assert!(+ writer.shares.lock().unwrap().is_empty(),+ "a personal database shares nothing"+ );+ assert_eq!(written.rows.len(), 2);+ assert_eq!(written.relations.len(), 1);+ assert_eq!(written.relations[0].name, "Project");+ assert_eq!(written.relations[0].target, Some(id('9')));+ assert_eq!(+ written.relations[0].links,+ [(RowId::from_uuid(Uuid::from_u128(700)), vec![id('8')])]+ );+}++#[tokio::test]+async fn a_shared_database_is_shared_with_its_row_bodies_folder() {+ let notion = FakeNotion {+ fetched: Mutex::new(0),+ source: NotionDataSource {+ id: id('e'),+ database_id: id('f'),+ title: "Bugs".to_string(),+ database_parent: NotionParent::Workspace,+ in_trash: false,+ properties: Vec::new(),+ },+ rows: vec![+ notion_row('1', "Login fails", vec![]),+ notion_row('2', "Typo on pricing", vec![]),+ ],+ bodies: [(id('1'), "Steps to reproduce".to_string())]+ .into_iter()+ .collect(),+ };+ let writer = FakeWriter::default();+ let source_item = source_item("Bugs");+ let store = FakeStore::default();+ let user = user();+ let progress = Progress::new(&store, &user, source_item.import_id, &[]);++ write_database(+ ¬ion,+ &writer,+ &user,+ &source_item,+ Uuid::from_u128(42),+ true,+ &PeopleMap::new(),+ &NotionLinks::default(),+ &progress,+ &Cancel::default(),+ )+ .await+ .unwrap();++ assert_eq!(+ writer.folders.lock().unwrap().as_slice(),+ [("Bugs pages".to_string(), Some(Uuid::from_u128(42)))]+ );+ assert_eq!(+ writer.shares.lock().unwrap().as_slice(),+ [+ SharedEntity::Database(DATABASE),+ SharedEntity::Folder(Uuid::from_u128(901)),+ ]+ );+}++#[tokio::test]+async fn a_cancelled_import_stops_before_writing_rows() {+ let notion = FakeNotion {+ fetched: Mutex::new(0),+ source: NotionDataSource {+ id: id('e'),+ database_id: id('f'),+ title: "Bugs".to_string(),+ database_parent: NotionParent::Workspace,+ in_trash: false,+ properties: Vec::new(),+ },+ rows: vec![notion_row('1', "Login fails", vec![])],+ bodies: HashMap::new(),+ };+ let writer = FakeWriter::default();+ let cancel = Cancel::default();+ cancel.stop();+ let source_item = source_item("Bugs");+ let store = FakeStore::default();+ let user = user();+ let progress = Progress::new(&store, &user, source_item.import_id, &[]);++ let outcome = write_database(+ ¬ion,+ &writer,+ &user,+ &source_item,+ Uuid::from_u128(42),+ false,+ &PeopleMap::new(),+ &NotionLinks::default(),+ &progress,+ &cancel,+ )+ .await;++ assert_eq!(outcome.unwrap_err().to_string(), "Cancelled");+ let batches = writer.batches.lock().unwrap();+ assert_eq!(batches.len(), 1, "only the schema was written");+ assert!(+ !batches[0]+ .iter()+ .any(|op| matches!(op, DatabaseOp::Rows { .. }))+ );+}++#[test]+fn the_page_column_never_takes_a_name_the_data_source_already_uses() {+ let property = |name: &str| NotionPropertySchema {+ id: name.to_string(),+ name: name.to_string(),+ kind: NotionPropertyKind::RichText,+ };+ assert_eq!(page_column_name(&[property("Status")]), "Page");+ assert_eq!(page_column_name(&[property("page")]), "Page (Notion)");+ assert_eq!(+ page_column_name(&[property("Page"), property("Page (Notion)")]),+ "Page 2"+ );+}++#[tokio::test]+async fn a_database_whose_rows_have_no_content_gets_no_page_column() {+ let notion = FakeNotion {+ fetched: Mutex::new(0),+ source: NotionDataSource {+ id: id('e'),+ database_id: id('f'),+ title: "Partners".to_string(),+ database_parent: NotionParent::Workspace,+ in_trash: false,+ properties: Vec::new(),+ },+ rows: vec![notion_row('1', "Acme", vec![])],+ bodies: HashMap::new(),+ };+ let writer = FakeWriter::default();+ let source_item = source_item("Partners");+ let store = FakeStore::default();+ let user = user();+ let progress = Progress::new(&store, &user, source_item.import_id, &[]);++ write_database(+ ¬ion,+ &writer,+ &user,+ &source_item,+ Uuid::from_u128(42),+ false,+ &PeopleMap::new(),+ &NotionLinks::default(),+ &progress,+ &Cancel::default(),+ )+ .await+ .unwrap();++ let batches = writer.batches.lock().unwrap();+ assert_eq!(batches.len(), 2, "schema and rows only");+ assert!(writer.folders.lock().unwrap().is_empty());+}++fn numbered_row(number: u128) -> NotionPage {+ let id = NotionId::parse(&format!("{number:032x}")).unwrap();+ NotionPage {+ id: id.clone(),+ title: format!("Row {number}"),+ url: id.url(),+ parent: NotionParent::DataSource {+ data_source: self::id('e'),+ database: self::id('f'),+ },+ last_edited: Utc.with_ymd_and_hms(2026, 10, 1, 9, 0, 0).unwrap(),+ created: Utc.with_ymd_and_hms(2026, 9, 1, 9, 0, 0).unwrap(),+ in_trash: false,+ properties: BTreeMap::new(),+ }+}++fn inserted(batches: &[Vec<DatabaseOp>]) -> usize {+ batches+ .iter()+ .flatten()+ .map(|op| match op {+ DatabaseOp::Rows {+ change: RowsChange::Insert { rows },+ ..+ } => rows.len(),+ _ => 0,+ })+ .sum()+}++#[tokio::test]+async fn a_database_stopped_part_way_through_its_rows_resumes_without_duplicates() {+ // 600 rows: two insert batches, the second lost with its process.+ let notion = FakeNotion {+ fetched: Mutex::new(0),+ source: NotionDataSource {+ id: id('e'),+ database_id: id('f'),+ title: "Accounts".to_string(),+ database_parent: NotionParent::Workspace,+ in_trash: false,+ properties: Vec::new(),+ },+ rows: (1..=600).map(numbered_row).collect(),+ bodies: HashMap::new(),+ };+ let writer = FakeWriter::default();+ *writer.fail_at_batch.lock().unwrap() = Some(2);+ let store = FakeStore::default();+ let user = user();+ let item = source_item("Accounts");++ let first = write_database(+ ¬ion,+ &writer,+ &user,+ &item,+ Uuid::from_u128(42),+ false,+ &PeopleMap::new(),+ &NotionLinks::default(),+ &Progress::new(&store, &user, item.import_id, &[]),+ &Cancel::default(),+ )+ .await;+ assert_eq!(first.unwrap_err().to_string(), "the process stopped");+ assert_eq!(inserted(&writer.batches.lock().unwrap()), 500);++ // Another process resumes from what was recorded.+ *writer.fail_at_batch.lock().unwrap() = None;+ let recorded = store.written(&user, &[item.import_id]).await.unwrap();+ let written = write_database(+ ¬ion,+ &writer,+ &user,+ &item,+ Uuid::from_u128(42),+ false,+ &PeopleMap::new(),+ &NotionLinks::default(),+ &Progress::new(&store, &user, item.import_id, &recorded),+ &Cancel::default(),+ )+ .await+ .unwrap();++ assert_eq!(*writer.databases.lock().unwrap(), 1, "one database");+ let batches = writer.batches.lock().unwrap();+ let schemas = batches+ .iter()+ .filter(|batch| {+ batch.iter().any(|op| {+ matches!(+ op,+ DatabaseOp::Table {+ change: TableChange::Rename { .. },+ ..+ }+ )+ })+ })+ .count();+ assert_eq!(schemas, 1, "the schema is written once");+ assert_eq!(inserted(&batches), 600, "every row once");+ assert_eq!(written.rows.len(), 600);+ let distinct: HashSet<RowId> = written.rows.values().map(|row| row.row).collect();+ assert_eq!(distinct.len(), 600);+ assert_eq!(written.database_id, DATABASE);+}++#[tokio::test]+async fn resumed_row_bodies_are_neither_read_nor_created_again() {+ let notion = FakeNotion {+ fetched: Mutex::new(0),+ source: NotionDataSource {+ id: id('e'),+ database_id: id('f'),+ title: "Bugs".to_string(),+ database_parent: NotionParent::Workspace,+ in_trash: false,+ properties: Vec::new(),+ },+ rows: vec![+ notion_row('1', "Login fails", vec![]),+ notion_row('2', "Typo", vec![]),+ ],+ bodies: [+ (id('1'), "Steps".to_string()),+ (id('2'), "Where".to_string()),+ ]+ .into_iter()+ .collect(),+ };+ let writer = FakeWriter::default();+ // Schema, rows, then the Page column, which the stop takes down.+ *writer.fail_at_batch.lock().unwrap() = Some(2);+ let store = FakeStore::default();+ let user = user();+ let item = source_item("Bugs");++ write_database(+ ¬ion,+ &writer,+ &user,+ &item,+ Uuid::from_u128(42),+ false,+ &PeopleMap::new(),+ &NotionLinks::default(),+ &Progress::new(&store, &user, item.import_id, &[]),+ &Cancel::default(),+ )+ .await+ .unwrap_err();+ assert_eq!(*notion.fetched.lock().unwrap(), 2);+ assert_eq!(writer.documents.lock().unwrap().len(), 2);++ *writer.fail_at_batch.lock().unwrap() = None;+ let recorded = store.written(&user, &[item.import_id]).await.unwrap();+ write_database(+ ¬ion,+ &writer,+ &user,+ &item,+ Uuid::from_u128(42),+ false,+ &PeopleMap::new(),+ &NotionLinks::default(),+ &Progress::new(&store, &user, item.import_id, &recorded),+ &Cancel::default(),+ )+ .await+ .unwrap();++ assert_eq!(*notion.fetched.lock().unwrap(), 2, "no body is read twice");+ assert_eq!(writer.documents.lock().unwrap().len(), 2);+ assert_eq!(+ writer.folders.lock().unwrap().len(),+ 1,+ "the bodies folder is reused"+ );+ let batches = writer.batches.lock().unwrap();+ assert_eq!(inserted(&batches), 2);+ // The Page column, then its two links.+ let DatabaseOp::Rows {+ change:+ RowsChange::Update {+ changes: RowChanges::PerRow { rows: linked },+ },+ ..+ } = &batches[batches.len() - 1][0]+ else {+ panic!("expected the Page links, got {:?}", batches.last());+ };+ assert_eq!(linked.len(), 2);+}diff --git a/crates/import/src/domain/service/notion_workspace/write/test.rs b/crates/import/src/domain/service/notion_workspace/write/test.rsnew file mode 100644index 0000000000..a5dc7b05a8--- /dev/null+++ b/crates/import/src/domain/service/notion_workspace/write/test.rs@@ -0,0 +1,124 @@+use macro_user_id::cowlike::CowLike;++use super::super::values::Person;+use super::*;++fn id(hex: char) -> NotionId {+ NotionId::parse(&hex.to_string().repeat(32)).unwrap()+}++fn item(import: u128, parent: Option<u128>, title: &str) -> WriteItem {+ WriteItem {+ import_id: Uuid::from_u128(import),+ parent: parent.map(Uuid::from_u128),+ notion_id: id('a'),+ kind: ImportKind::Page,+ title: title.to_string(),+ decision: NotionDecision::Copy,+ task_fields: NotionTaskFields::default(),+ shared: false,+ shareable: true,+ already_imported: false,+ }+}++#[test]+fn every_page_above_a_written_object_becomes_a_folder_parents_first() {+ // Engineering > Q3 plan > Launch checklist, and a lone page.+ let items = vec![+ item(3, Some(2), "Launch checklist"),+ item(1, None, "Engineering"),+ item(2, Some(1), "Q3 plan"),+ item(4, None, "Scratchpad"),+ ];++ let layout = folder_layout(&items);++ assert_eq!(layout.folders, [Uuid::from_u128(1), Uuid::from_u128(2)]);++ let root = Uuid::from_u128(100);+ let folders: HashMap<Uuid, Uuid> = [+ (Uuid::from_u128(1), Uuid::from_u128(101)),+ (Uuid::from_u128(2), Uuid::from_u128(102)),+ ]+ .into_iter()+ .collect();+ let parents: HashMap<Uuid, Option<Uuid>> = items+ .iter()+ .map(|item| (item.import_id, item.parent))+ .collect();+ // A page with children goes in its own folder.+ assert_eq!(+ folder_for(&items[1], true, &folders, &parents, root),+ Uuid::from_u128(101)+ );+ // The folder itself sits in its parent's folder.+ assert_eq!(+ folder_for(&items[2], false, &folders, &parents, root),+ Uuid::from_u128(101)+ );+ assert_eq!(+ folder_for(&items[0], true, &folders, &parents, root),+ Uuid::from_u128(102)+ );+ assert_eq!(folder_for(&items[3], true, &folders, &parents, root), root);+}++#[test]+fn every_planned_page_gets_a_document_id_before_anything_is_written() {+ let mut fresh = item(1, None, "Roadmap");+ fresh.notion_id = id('1');+ let mut teammates = item(2, None, "Spec");+ teammates.notion_id = id('2');+ let mut source = item(3, None, "Bugs");+ source.kind = ImportKind::DataSource;+ source.notion_id = id('3');+ let imported: HashMap<NotionId, Uuid> = [(id('2'), Uuid::from_u128(77))].into();+ let people: PeopleMap = [+ (+ id('4'),+ Person {+ email: Some("[email protected]".to_string()),+ macro_user: Some(+ MacroUserIdStr::parse_from_str("macro|[email protected]")+ .unwrap()+ .into_owned(),+ ),+ },+ ),+ (+ id('5'),+ Person {+ email: Some("[email protected]".to_string()),+ macro_user: None,+ },+ ),+ ]+ .into();++ let links = notion_links(&[fresh, teammates, source], &imported, &people);++ assert_eq!(+ links.pages.len(),+ 2,+ "data sources become databases, not documents"+ );+ assert_eq!(links.pages[&id('1')].name, "Roadmap");+ assert_eq!(+ links.pages[&id('1')].document_id,+ document_id(Uuid::from_u128(1)),+ "a resumed write names the same document"+ );+ assert_eq!(+ links.pages[&id('2')],+ NotionPageLink {+ document_id: Uuid::from_u128(77),+ name: "Spec".to_string(),+ }+ );+ assert_eq!(+ links.users.keys().collect::<Vec<_>>(),+ [&id('4')],+ "only people matched to a teammate become mentions"+ );+}diff --git a/crates/import/src/domain/service/prompts.rs b/crates/import/src/domain/service/prompts.rsindex c541ed1296..4dd33818ef 100644--- a/crates/import/src/domain/service/prompts.rs+++ b/crates/import/src/domain/service/prompts.rs@@ -1,87 +1,9 @@-//! Prompts for the gather and import agent sessions.-//!-//! Gather sessions stage candidates through the `CreateImportEntity` tool —-//! there is no structured output to parse; the tool IS the output channel.-//! Import sessions (Notion only — Linear tasks and Slack channels are-//! composed deterministically from staged metadata) fetch page content and-//! land it through `FinalizeImport`.+//! Prompts for the Notion import agent session, the fallback when a page+//! can't be copied directly. It fetches page content and lands it through+//! `FinalizeImport`; Linear tasks and Slack channels are composed+//! deterministically from staged metadata.-use crate::domain::models::{ImportEntity, ImportSource, metadata_label};--/// System prompt for a gather session over one connector.-pub fn gather_system(source: ImportSource) -> String {- let (what, foreign_id_rule, metadata_hint, search_strategy) = match source {- ImportSource::Linear => (- "the user's most relevant recent Linear issues",- "the Linear issue identifier (e.g. `ENG-142`); fall back to the issue URL only when \- no identifier exists",- "identifier, title, description (short markdown), status, priority, assignee, \- assignee_email, due_date (ISO date, when set), url",- "Always pass a non-empty query to search tools.",- ),- ImportSource::Notion => (- "the user's most recently updated, substantive Notion pages",- "the page URL (it is normalized to the stable page id server-side)",- "title, url, summary (one line). Do NOT fetch or include page content — content is \- fetched later, only for pages the user accepts",- "Always pass a non-empty query to search tools.",- ),- ImportSource::Slack => (- "the Slack channels the user is most active in",- "the Slack channel id (e.g. `C0123456789`); fall back to the channel name",- "name (without the leading #), channel_id, purpose, participants (name + email when \- available)",- "Slack-specific discovery strategy:\n\- - FIRST call `Search channels` with an explicitly empty search string: \- `{\"query\": \"\"}`. For this Slack MCP tool, an empty query lists all channels the \- connected user can see. Do not substitute `active`, `recent`, `all`, or `*`.\n\- - Follow the result's pagination cursor when one is present until you have enough \- candidates or there are no more pages.\n\- - Prefer channels whose names, topics, or purposes indicate substantive work. You \- may use one broad `Search messages & files` call afterward to rank the enumerated \- channels by recent activity, but do not use message search as a prerequisite for \- discovering that a channel exists.\n\- - Participant details are optional. Do not drop a channel merely because member \- names or emails are unavailable.",- ),- };- format!(- "You are discovering {what} so they can be imported into Macro, the user's new \- workspace.\n\- \n\- Use the connected tools to find 8-15 strong candidates: recently active, substantive, \- and clearly relevant to the user's own work. Prefer one or two broad searches/list \- calls over many narrow ones. Pass only parameters the tool's schema supports; if a call \- fails validation, fix the arguments and retry once.\n\- {search_strategy}\n\- \n\- For EACH candidate, call `CreateImportEntity` once with:\n\- - `foreign_id`: {foreign_id_rule}\n\- - `metadata`: {metadata_hint}\n\- \n\- The tool response tells you when an item was already imported by the user or a \- teammate, or previously declined — do not re-stage those, just move on.\n\- \n\- When you are done staging, reply with one short plain-text sentence summarizing what \- you staged. Do not output JSON."- )-}--/// User message opening a gather session.-pub fn gather_prompt(source: ImportSource) -> &'static str {- match source {- ImportSource::Linear => {- "Find my most relevant recent Linear issues and stage them for import."- }- ImportSource::Notion => {- "Find my most recently updated Notion pages worth bringing over and stage them for \- import."- }- ImportSource::Slack => {- "Find the Slack channels I'm most active in and stage them for import."- }- }-}+use crate::domain::models::{ImportEntity, metadata_label};/// System prompt for the Notion import session.pub const NOTION_IMPORT_SYSTEM: &str = "You are importing Notion pages the user accepted into \diff --git a/crates/import/src/domain/service/slack/test.rs b/crates/import/src/domain/service/slack/test.rsindex c64dccb096..99b58cecbd 100644--- a/crates/import/src/domain/service/slack/test.rs+++ b/crates/import/src/domain/service/slack/test.rs@@ -1,10 +1,10 @@use super::*;use crate::domain::{models::{- ImportSource, ImportSourceBinding, ImportStatus, ImportTargetReservation, Initiator,- SlackWorkspaceId,+ ImportKind, ImportSource, ImportSourceBinding, ImportStatus, ImportTargetReservation,+ Initiator, SlackWorkspaceId,},- ports::{ImportError, ImportedDocumentProperties, ImportedTaskProperties, Result},+ ports::{ImportError, ImportedDocumentProperties, ImportedTaskProperties, NotionLinks, Result},};use std::{collections::HashSet, sync::Mutex};@@ -99,11 +99,12 @@ impl EntityCreator for Harness {unreachable!()}- async fn create_markdown_doc(+ async fn create_notion_document(&self,_: &MacroUserIdStr<'static>,_: &str,_: &str,+ _: &NotionLinks,_: &ImportedDocumentProperties,) -> anyhow::Result<String> {unreachable!()@@ -149,13 +150,17 @@ fn fixture() -> (MacroUserIdStr<'static>, ImportEntity, ImportTargetKey) {user_id: user.to_string(),team_id: None,source: ImportSource::Slack,+ kind: ImportKind::Channel,foreign_id: key.foreign_id.as_str().to_string(),+ parent_id: None,+ review_id: None,status: ImportStatus::Importing,- initiator: Initiator::Onboarding,+ initiator: Initiator::Manual,metadata: serde_json::json!({"name": "engineering", "channel_id": key.foreign_id.as_str()}),entity_id: None,entity_type: None,last_error: None,+ waiting_on: None,created_at: chrono::Utc::now(),updated_at: chrono::Utc::now(),};diff --git a/crates/import/src/domain/service/slack_discovery.rs b/crates/import/src/domain/service/slack_discovery.rsindex 5a53766869..ab7df9947b 100644--- a/crates/import/src/domain/service/slack_discovery.rs+++ b/crates/import/src/domain/service/slack_discovery.rs@@ -1,10 +1,10 @@//! Bounded Slack discovery and roster policy over typed workspace reads.-use super::{GatherMode, ImportServiceImpl, StageOutcome, gather_timeout};+use super::{ImportServiceImpl, SLACK_DISCOVERY_TIMEOUT, StageOutcome};use crate::domain::models::{- ImportSource, Initiator, SlackChannelMeta, SlackConversation, SlackConversationId,- SlackConversationKind, SlackConversationPage, SlackMemberPage, SlackParticipant, SlackUser,- SlackUserId, SlackUserPage,+ ImportSource, Initiator, SlackChannelMeta, SlackConversationId, SlackConversationKind,+ SlackConversationPage, SlackMemberPage, SlackParticipant, SlackUser, SlackUserId,+ SlackUserPage,};use crate::domain::ports::{CanonicalImportRepo, EntityCreator, ImportRepo, SlackSourceError, SlackWorkspaceSession,@@ -23,10 +23,10 @@ mod test;const MAX_CONVERSATION_PAGES: usize = 10;const MAX_USER_PAGES: usize = 10;const MAX_MEMBER_PAGES: usize = 5;-const MANUAL_MEMBER_BUDGET: usize = 100;+const MEMBER_ENRICHMENT_BUDGET: usize = 100;const NOTIFY_EVERY: usize = 10;const RATE_LIMIT_MAX_SLEEP: Duration = Duration::from_secs(30);-// Finish best-effort work before spawn_gather's hard timeout can fail usable rows.+// Finish best-effort work before spawn_slack_discovery's hard timeout can fail usable rows.const ENRICHMENT_TIMEOUT_RESERVE: Duration = Duration::from_secs(10);/// Default for hosts that have not attached a live Slack workspace reader.@@ -115,9 +115,9 @@ impl<Sess> Default for SlackBatch<Sess> {}impl<Sess: SlackWorkspaceSession> SlackBatch<Sess> {- pub(super) async fn resolve_emails<R, S, C, W>(+ pub(super) async fn resolve_emails<R, S, C, W, N: Send + Sync + 'static>(&mut self,- service: &ImportServiceImpl<R, S, C, W>,+ service: &ImportServiceImpl<R, S, C, W, N>,user: &MacroUserIdStr<'static>,team_id: Uuid,meta: &SlackChannelMeta,@@ -223,21 +223,11 @@ pub(super) async fn resolve_members(Ok(resolved)}-/// Prefer larger active channels, retaining listing order among ties.-fn select_slack_candidates(- mut channels: Vec<SlackConversation>,- cap: usize,-) -> Vec<SlackConversation> {- channels.retain(|channel| !channel.archived);- channels.sort_by_key(|channel| std::cmp::Reverse(channel.member_count.unwrap_or(0)));- channels.truncate(cap);- channels-}--pub(super) async fn gather_slack<R, S, C, W>(- service: &ImportServiceImpl<R, S, C, W>,+/// Stage every visible public Slack channel for the user to pick from, then+/// enrich staged rows with matched members until the deadline.+pub(super) async fn discover_slack<R, S, C, W, N: Send + Sync + 'static>(+ service: &ImportServiceImpl<R, S, C, W, N>,user: &MacroUserIdStr<'static>,- mode: GatherMode,) -> Result<usize, SlackSourceError>whereR: ImportRepo + CanonicalImportRepo + Clone,@@ -246,9 +236,7 @@ whereW: SlackWorkspaceSource,{// Anchor before opening/listing: discovery time consumes the enrichment budget.- // Slack admission before this call does not perform asynchronous work.- let enrichment_deadline =- Instant::now() + gather_timeout(ImportSource::Slack, mode) - ENRICHMENT_TIMEOUT_RESERVE;+ let enrichment_deadline = Instant::now() + SLACK_DISCOVERY_TIMEOUT - ENRICHMENT_TIMEOUT_RESERVE;let session = service.slack_source.open(user).await?;let mut channels = Vec::new();let mut seen = HashSet::new();@@ -263,13 +251,6 @@ wherebreak;}}- let initiator = match mode {- GatherMode::Onboarding => {- channels = select_slack_candidates(channels, 15);- Initiator::Onboarding- }- GatherMode::Manual => Initiator::Manual,- };let roster = match service.repo.user_team_id(user)@@ -300,7 +281,7 @@ wherelet outcome = service.stage_inner(user,- initiator,+ Initiator::Manual,ImportSource::Slack,channel.id.as_str(),serde_json::to_value(&metadata).map_err(|e| SlackSourceError::Other(e.into()))?,@@ -325,16 +306,12 @@ whereif count == 0 || Instant::now() >= enrichment_deadline {return Ok(count);}- let budget = match mode {- GatherMode::Onboarding => count,- GatherMode::Manual => MANUAL_MEMBER_BUDGET,- };// Dropping this future stops reads/retries and notifications. Each metadata// upsert is atomic and only updates staged rows: cancellation leaves either// the original or enriched metadata, never an import claim to clean up.match timeout_at(enrichment_deadline,- enrich_slack(service, user, &session, &roster, staged, initiator, budget),+ enrich_slack(service, user, &session, &roster, staged),).await{@@ -349,14 +326,12 @@ whereOk(count)}-async fn enrich_slack<R, S, C, W>(- service: &ImportServiceImpl<R, S, C, W>,+async fn enrich_slack<R, S, C, W, N: Send + Sync + 'static>(+ service: &ImportServiceImpl<R, S, C, W, N>,user: &MacroUserIdStr<'static>,session: &W::Session,roster: &[MacroUserIdStr<'static>],mut staged: Vec<(SlackConversationId, SlackChannelMeta)>,- initiator: Initiator,- budget: usize,) -> Result<(), SlackSourceError>whereR: ImportRepo + CanonicalImportRepo + Clone,@@ -373,7 +348,7 @@ where};staged.sort_by_key(|(_, metadata)| std::cmp::Reverse(metadata.member_count.unwrap_or(0)));let mut enriched = 0;- for (channel, mut metadata) in staged.into_iter().take(budget) {+ for (channel, mut metadata) in staged.into_iter().take(MEMBER_ENRICHMENT_BUDGET) {let members = match resolve_members(session, &directory, &channel, roster).await {Ok(members) => members,Err(error) => {@@ -387,7 +362,7 @@ wherematch service.stage_inner(user,- initiator,+ Initiator::Manual,ImportSource::Slack,channel.as_str(),serde_json::to_value(metadata).map_err(|e| SlackSourceError::Other(e.into()))?,diff --git a/crates/import/src/domain/service/slack_discovery/test.rs b/crates/import/src/domain/service/slack_discovery/test.rsindex 7e046f58b6..d7641f21c9 100644--- a/crates/import/src/domain/service/slack_discovery/test.rs+++ b/crates/import/src/domain/service/slack_discovery/test.rs@@ -1,7 +1,8 @@use super::*;-use crate::domain::models::{ImportStatus, RunStatus};+use crate::domain::models::{ImportStatus, RunStatus, SlackConversation};+use crate::domain::ports::ImportError;use crate::domain::service::test::admission::{Creator, NoConnector, Repo, user};-use crate::domain::service::{ImportService, ImportStager, gather_timeout};+use crate::domain::service::{ImportService, ImportStager, SLACK_DISCOVERY_TIMEOUT};use std::sync::atomic::{AtomicUsize, Ordering};use std::sync::{Arc, Mutex};@@ -218,9 +219,7 @@ async fn pagination_follows_cursors_and_stops_at_each_bound() {};let service = service(Repo::default(), workspace);assert_eq!(- gather_slack(&service, &user(), GatherMode::Manual)- .await- .unwrap(),+ discover_slack(&service, &user()).await.unwrap(),MAX_CONVERSATION_PAGES);let calls = &service.slack_source.0.0;@@ -242,51 +241,6 @@ async fn pagination_follows_cursors_and_stops_at_each_bound() {}}-#[tokio::test]-async fn onboarding_ranks_caps_and_excludes_archived_and_unsupported_kinds() {- let mut channels: Vec<_> = (0..20).map(channel).collect();- channels[19].archived = true;- channels.push(channel(18)); // duplicate across the listing- for (index, kind) in [- SlackConversationKind::PrivateChannel,- SlackConversationKind::DirectMessage,- SlackConversationKind::GroupDirectMessage,- ]- .into_iter()- .enumerate()- {- let mut unsupported = channel(30 + index);- unsupported.kind = kind;- channels.push(unsupported);- }- let repo = Repo::default();- let service = service(- repo.clone(),- Workspace {- observe_staged: Some(repo),- ..workspace(channels)- },- );- assert_eq!(- gather_slack(&service, &user(), GatherMode::Onboarding)- .await- .unwrap(),- 15- );- let rows = service.repo.list(&user(), None, None).await.unwrap();- assert_eq!(rows.len(), 15);- assert_eq!(rows[0].foreign_id, channel(18).id.as_str());- assert_eq!(rows[14].foreign_id, channel(4).id.as_str());- assert!(- rows.iter()- .all(|row| row.initiator == Initiator::Onboarding && row.metadata["archived"] == false)- );- assert_eq!(- service.slack_source.0.0.member_calls.lock().unwrap().len(),- 15- );-}-#[tokio::test]async fn manual_stages_all_public_channels_including_archived_but_bounds_enrichment() {let mut channels: Vec<_> = (0..102).map(channel).collect();@@ -302,12 +256,7 @@ async fn manual_stages_all_public_channels_including_archived_but_bounds_enrichmchannels.push(unsupported);}let service = service(Repo::default(), workspace(channels));- assert_eq!(- gather_slack(&service, &user(), GatherMode::Manual)- .await- .unwrap(),- 102- );+ assert_eq!(discover_slack(&service, &user()).await.unwrap(), 102);let rows = service.repo.list(&user(), None, None).await.unwrap();assert_eq!(rows.len(), 102);assert!(rows.iter().all(|row| row.initiator == Initiator::Manual));@@ -320,7 +269,7 @@ async fn manual_stages_all_public_channels_including_archived_but_bounds_enrichm.all(|row| row.metadata["members_resolved"] == true));let calls = service.slack_source.0.0.member_calls.lock().unwrap();- assert_eq!(calls.len(), MANUAL_MEMBER_BUDGET);+ assert_eq!(calls.len(), MEMBER_ENRICHMENT_BUDGET);assert_eq!(calls[0].0, channel(101).id.as_str());}@@ -351,9 +300,7 @@ async fn resolves_only_roster_matches_and_counts_all_known_humans() {..workspace(vec![channel(0)])},);- gather_slack(&service, &user(), GatherMode::Manual)- .await- .unwrap();+ discover_slack(&service, &user()).await.unwrap();let rows = service.repo.list(&user(), None, None).await.unwrap();let metadata: SlackChannelMeta = serde_json::from_value(rows[0].metadata.clone()).unwrap();assert_eq!(metadata.member_count, Some(3));@@ -368,7 +315,7 @@ async fn resolves_only_roster_matches_and_counts_all_known_humans() {}#[tokio::test]-async fn rate_limited_channels_retry_once_and_persistent_errors_do_not_fail_gather() {+async fn rate_limited_channels_retry_once_and_persistent_errors_do_not_fail_discovery() {let service = service(Repo::default(),Workspace {@@ -379,12 +326,7 @@ async fn rate_limited_channels_retry_once_and_persistent_errors_do_not_fail_gath..workspace(vec![channel(0), channel(1), channel(2)])},);- assert_eq!(- gather_slack(&service, &user(), GatherMode::Manual)- .await- .unwrap(),- 3- );+ assert_eq!(discover_slack(&service, &user()).await.unwrap(), 3);let rows = service.repo.list(&user(), None, None).await.unwrap();assert_eq!(rows[0].metadata["members_resolved"], true);assert_eq!(rows[1].metadata["members_resolved"], false);@@ -410,12 +352,7 @@ async fn directory_failure_preserves_unresolved_candidates() {..workspace(vec![channel(0)])},);- assert_eq!(- gather_slack(&service, &user(), GatherMode::Manual)- .await- .unwrap(),- 1- );+ assert_eq!(discover_slack(&service, &user()).await.unwrap(), 1);let rows = service.repo.list(&user(), None, None).await.unwrap();assert_eq!(rows[0].metadata["members_resolved"], false);assert!(@@ -431,82 +368,77 @@ async fn directory_failure_preserves_unresolved_candidates() {}#[tokio::test(start_paused = true)]-async fn slow_directory_retains_staged_rows_before_both_outer_timeouts() {- for mode in [GatherMode::Onboarding, GatherMode::Manual] {- let outer_timeout = gather_timeout(ImportSource::Slack, mode);- let repo = Repo::default();- let service = service(- repo.clone(),- Workspace {- listing_delay: Duration::from_secs(30),- directory_delay: outer_timeout,- observe_staged: Some(repo),- ..workspace(vec![channel(0)])- },- );- let start = Instant::now();- assert_eq!(- tokio::time::timeout(outer_timeout, gather_slack(&service, &user(), mode))- .await- .expect("best-effort directory must not hit the outer timeout")- .unwrap(),- 1- );- assert_eq!(start.elapsed(), outer_timeout - ENRICHMENT_TIMEOUT_RESERVE);- assert!(start.elapsed() < outer_timeout);- let rows = service.repo.list(&user(), None, None).await.unwrap();- assert_eq!(rows.len(), 1);- assert_eq!(rows[0].status, ImportStatus::Staged);- assert_eq!(rows[0].metadata["members_resolved"], false);- let calls = &service.slack_source.0.0;- assert_eq!(calls.user_calls.lock().unwrap().len(), 1);- assert!(calls.member_calls.lock().unwrap().is_empty());- }+async fn slow_directory_retains_staged_rows_before_the_outer_timeout() {+ let outer_timeout = SLACK_DISCOVERY_TIMEOUT;+ let repo = Repo::default();+ let service = service(+ repo.clone(),+ Workspace {+ listing_delay: Duration::from_secs(30),+ directory_delay: outer_timeout,+ observe_staged: Some(repo),+ ..workspace(vec![channel(0)])+ },+ );+ let start = Instant::now();+ assert_eq!(+ tokio::time::timeout(outer_timeout, discover_slack(&service, &user()))+ .await+ .expect("best-effort directory must not hit the outer timeout")+ .unwrap(),+ 1+ );+ assert_eq!(start.elapsed(), outer_timeout - ENRICHMENT_TIMEOUT_RESERVE);+ assert!(start.elapsed() < outer_timeout);+ let rows = service.repo.list(&user(), None, None).await.unwrap();+ assert_eq!(rows.len(), 1);+ assert_eq!(rows[0].status, ImportStatus::Staged);+ assert_eq!(rows[0].metadata["members_resolved"], false);+ let calls = &service.slack_source.0.0;+ assert_eq!(calls.user_calls.lock().unwrap().len(), 1);+ assert!(calls.member_calls.lock().unwrap().is_empty());}#[tokio::test(start_paused = true)]async fn listing_and_directory_share_member_deadline_and_preserve_completed_enrichment() {- for mode in [GatherMode::Onboarding, GatherMode::Manual] {- let outer_timeout = gather_timeout(ImportSource::Slack, mode);- let service = service(- Repo::default(),- Workspace {- listing_delay: Duration::from_secs(30),- directory_delay: Duration::from_secs(40),- // Channel 1 is enriched first; channel 0 never finishes.- member_delays: HashMap::from([(channel(0).id.as_str().into(), outer_timeout)]),- ..workspace(vec![channel(0), channel(1)])- },- );- let start = Instant::now();- assert_eq!(- tokio::time::timeout(outer_timeout, gather_slack(&service, &user(), mode))- .await- .expect("best-effort membership must not hit the outer timeout")- .unwrap(),- 2- );- assert_eq!(start.elapsed(), outer_timeout - ENRICHMENT_TIMEOUT_RESERVE);- let rows = service.repo.list(&user(), None, None).await.unwrap();- assert_eq!(rows.len(), 2);- for row in rows {- assert_eq!(row.status, ImportStatus::Staged);- assert_eq!(- row.metadata["members_resolved"],- row.foreign_id == channel(1).id.as_str()- );- }+ let outer_timeout = SLACK_DISCOVERY_TIMEOUT;+ let service = service(+ Repo::default(),+ Workspace {+ listing_delay: Duration::from_secs(30),+ directory_delay: Duration::from_secs(40),+ // Channel 1 is enriched first; channel 0 never finishes.+ member_delays: HashMap::from([(channel(0).id.as_str().into(), outer_timeout)]),+ ..workspace(vec![channel(0), channel(1)])+ },+ );+ let start = Instant::now();+ assert_eq!(+ tokio::time::timeout(outer_timeout, discover_slack(&service, &user()))+ .await+ .expect("best-effort membership must not hit the outer timeout")+ .unwrap(),+ 2+ );+ assert_eq!(start.elapsed(), outer_timeout - ENRICHMENT_TIMEOUT_RESERVE);+ let rows = service.repo.list(&user(), None, None).await.unwrap();+ assert_eq!(rows.len(), 2);+ for row in rows {+ assert_eq!(row.status, ImportStatus::Staged);assert_eq!(- service.slack_source.0.0.member_calls.lock().unwrap().len(),- 2+ row.metadata["members_resolved"],+ row.foreign_id == channel(1).id.as_str());}+ assert_eq!(+ service.slack_source.0.0.member_calls.lock().unwrap().len(),+ 2+ );}#[tokio::test(start_paused = true)]async fn exhausted_listing_budget_skips_enrichment_without_failing_staged_rows() {- let mode = GatherMode::Manual;- let outer_timeout = gather_timeout(ImportSource::Slack, mode);+ let outer_timeout = SLACK_DISCOVERY_TIMEOUT;let service = service(Repo::default(),Workspace {@@ -514,7 +446,7 @@ async fn exhausted_listing_budget_skips_enrichment_without_failing_staged_rows()..workspace(vec![channel(0)])},);- assert_eq!(gather_slack(&service, &user(), mode).await.unwrap(), 1);+ assert_eq!(discover_slack(&service, &user()).await.unwrap(), 1);let calls = &service.slack_source.0.0;assert!(calls.user_calls.lock().unwrap().is_empty());assert!(calls.member_calls.lock().unwrap().is_empty());@@ -526,8 +458,7 @@ async fn exhausted_listing_budget_skips_enrichment_without_failing_staged_rows()async fn enrichment_notifications_cannot_extend_the_deadline() {for notify_at in [1, 2, 3] {let calls = Arc::new(AtomicUsize::new(0));- let mode = GatherMode::Manual;- let outer_timeout = gather_timeout(ImportSource::Slack, mode);+ let outer_timeout = SLACK_DISCOVERY_TIMEOUT;let service = service(Repo::default(), workspace((0..10).map(channel).collect())).with_notifier(Arc::new({let calls = calls.clone();@@ -543,7 +474,7 @@ async fn enrichment_notifications_cannot_extend_the_deadline() {}));let start = Instant::now();assert_eq!(- tokio::time::timeout(outer_timeout, gather_slack(&service, &user(), mode))+ tokio::time::timeout(outer_timeout, discover_slack(&service, &user())).await.expect("notifications must not hit the outer timeout").unwrap(),@@ -565,12 +496,10 @@ async fn listing_errors_still_propagate_before_staging() {..workspace(vec![channel(0)])},);- for mode in [GatherMode::Onboarding, GatherMode::Manual] {- assert!(matches!(- gather_slack(&service, &user(), mode).await,- Err(SlackSourceError::MissingScope(scope)) if scope == "channels:read"- ));- }+ assert!(matches!(+ discover_slack(&service, &user()).await,+ Err(SlackSourceError::MissingScope(scope)) if scope == "channels:read"+ ));assert!(service.repo@@ -601,12 +530,7 @@ async fn enrichment_does_not_overwrite_a_concurrent_import() {..workspace(vec![channel(0)])},);- assert_eq!(- gather_slack(&service, &user(), GatherMode::Manual)- .await- .unwrap(),- 1- );+ assert_eq!(discover_slack(&service, &user()).await.unwrap(), 1);let rows = service.repo.list(&user(), None, None).await.unwrap();assert_eq!(rows[0].status, ImportStatus::Importing);assert_eq!(rows[0].metadata["members_resolved"], false);@@ -646,9 +570,7 @@ async fn silent_staging_and_discovery_notifications_are_batched() {.await.unwrap();assert_eq!(calls.swap(0, Ordering::SeqCst), 1);- gather_slack(&service, &user(), GatherMode::Manual)- .await- .unwrap();+ discover_slack(&service, &user()).await.unwrap();// Two batches and a final notification for each of staging and enrichment.assert_eq!(calls.load(Ordering::SeqCst), 6);}@@ -676,38 +598,33 @@ async fn finished_run<W: SlackWorkspaceSource>(}#[tokio::test(start_paused = true)]-async fn spawn_gather_marks_slow_enrichment_ready_for_onboarding_and_manual() {- for mode in [GatherMode::Onboarding, GatherMode::Manual] {- for slow_directory in [true, false] {- let outer_timeout = gather_timeout(ImportSource::Slack, mode);- let mut workspace = workspace(vec![channel(0)]);- if slow_directory {- workspace.directory_delay = outer_timeout;- } else {- workspace.member_delays =- HashMap::from([(channel(0).id.as_str().into(), outer_timeout)]);- }- let service = service(Repo::default(), workspace);- let started = match mode {- GatherMode::Onboarding => {- service- .start_gather(user(), ImportSource::Slack, false)- .await- }- GatherMode::Manual => service.start_discovery(user(), ImportSource::Slack).await,- };- assert!(started.unwrap());- // Let the spawned gather start, then advance virtual (not wall) time.- tokio::task::yield_now().await;- tokio::time::sleep(outer_timeout - ENRICHMENT_TIMEOUT_RESERVE).await;- let run = finished_run(&service).await;- assert_eq!(run.status, RunStatus::Ready);- assert!(run.error.is_none());- let rows = service.repo.list(&user(), None, None).await.unwrap();- assert_eq!(rows.len(), 1);- assert_eq!(rows[0].status, ImportStatus::Staged);- assert_eq!(rows[0].metadata["members_resolved"], false);+async fn spawned_discovery_marks_slow_enrichment_ready() {+ for slow_directory in [true, false] {+ let outer_timeout = SLACK_DISCOVERY_TIMEOUT;+ let mut workspace = workspace(vec![channel(0)]);+ if slow_directory {+ workspace.directory_delay = outer_timeout;+ } else {+ workspace.member_delays =+ HashMap::from([(channel(0).id.as_str().into(), outer_timeout)]);}+ let service = service(Repo::default(), workspace);+ assert!(+ service+ .start_discovery(user(), ImportSource::Slack)+ .await+ .unwrap()+ );+ // Let the spawned discovery start, then advance virtual (not wall) time.+ tokio::task::yield_now().await;+ tokio::time::sleep(outer_timeout - ENRICHMENT_TIMEOUT_RESERVE).await;+ let run = finished_run(&service).await;+ assert_eq!(run.status, RunStatus::Ready);+ assert!(run.error.is_none());+ let rows = service.repo.list(&user(), None, None).await.unwrap();+ assert_eq!(rows.len(), 1);+ assert_eq!(rows[0].status, ImportStatus::Staged);+ assert_eq!(rows[0].metadata["members_resolved"], false);}}@@ -724,7 +641,6 @@ async fn manual_without_connection_fails_readably_without_loading_connector_toollet run = finished_run(&service).await;assert_eq!(run.status, RunStatus::Failed);assert_eq!(run.error.as_deref(), Some("Slack is not connected"));- assert!(!run.auto_import);}struct MissingTools;@@ -754,26 +670,23 @@ async fn manual_without_tools_fails_readably() {}#[tokio::test]-async fn manual_accepts_agent_sources_and_does_not_restart_running_runs() {+async fn manual_rejects_other_sources_and_does_not_restart_running_runs() {let service = service(Repo::default(), Workspace::default());- assert!(- service- .start_discovery(user(), ImportSource::Linear)- .await- .unwrap()- );- let linear = service- .repo- .list_runs(&user())+ let error = service+ .start_discovery(user(), ImportSource::Linear).await- .unwrap()- .into_iter()- .find(|run| run.source == ImportSource::Linear)- .unwrap();- assert!(!linear.auto_import);+ .unwrap_err();+ assert!(matches!(+ error,+ ImportError::UnsupportedDiscovery(ImportSource::Linear)+ ));+ assert_eq!(+ error.to_string(),+ "manual discovery is not supported for linear"+ );service.repo- .start_run(&user(), ImportSource::Slack, &[], true)+ .start_run(&user(), ImportSource::Slack, &[]).await.unwrap();assert!(@@ -782,15 +695,6 @@ async fn manual_accepts_agent_sources_and_does_not_restart_running_runs() {.await.unwrap());- let slack = service- .repo- .list_runs(&user())- .await- .unwrap()- .into_iter()- .find(|run| run.source == ImportSource::Slack)- .unwrap();- assert!(slack.auto_import);assert!(service.slack_source@@ -804,7 +708,7 @@ async fn manual_accepts_agent_sources_and_does_not_restart_running_runs() {}#[tokio::test]-async fn manual_restarts_terminal_runs_without_auto_import() {+async fn manual_restarts_terminal_runs() {for status in [RunStatus::Ready,RunStatus::Completed,@@ -814,7 +718,7 @@ async fn manual_restarts_terminal_runs_without_auto_import() {let service = service(Repo::default(), workspace(vec![channel(0)]));service.repo- .start_run(&user(), ImportSource::Slack, &[], true)+ .start_run(&user(), ImportSource::Slack, &[]).await.unwrap();service@@ -830,7 +734,6 @@ async fn manual_restarts_terminal_runs_without_auto_import() {);let run = finished_run(&service).await;assert_eq!(run.status, RunStatus::Ready);- assert!(!run.auto_import);}}@@ -1016,19 +919,3 @@ async fn batch_missing_or_invalid_channel_id_falls_back_without_poisoning_sessio);assert_eq!(calls.open_calls.load(Ordering::SeqCst), 1);}--#[test]-fn gather_deadlines_depend_on_source_and_mode() {- assert_eq!(- gather_timeout(ImportSource::Slack, GatherMode::Onboarding),- Duration::from_secs(180)- );- assert_eq!(- gather_timeout(ImportSource::Slack, GatherMode::Manual),- Duration::from_secs(360)- );- assert_eq!(- gather_timeout(ImportSource::Notion, GatherMode::Onboarding),- Duration::from_secs(90)- );-}diff --git a/crates/import/src/domain/service/test.rs b/crates/import/src/domain/service/test.rsindex 63d691c751..61564ed4f8 100644--- a/crates/import/src/domain/service/test.rs+++ b/crates/import/src/domain/service/test.rs@@ -115,17 +115,6 @@ fn notion_import_failure_reason_preserves_the_actual_error() {);}-#[test]-fn slack_gather_lists_channels_with_an_explicit_empty_query() {- let prompt = prompts::gather_system(ImportSource::Slack);-- assert!(prompt.contains("FIRST call `Search channels`"));- assert!(prompt.contains(r#"{"query": ""}"#));- assert!(prompt.contains("an empty query lists all channels"));- assert!(!prompt.contains("Always pass a non-empty query"));- assert!(prompt.contains("Participant details are optional"));-}-#[test]fn notion_fetch_text_parses_the_fetch_document_shape() {let parsed = parse_notion_fetch_result(serde_json::Value::String(@@ -286,26 +275,6 @@ fn notion_fetch_recognizes_documented_tool_aliases() {assert!(!is_notion_fetch_tool_name("mcp__Notion__search"));}-#[test]-fn notion_page_references_become_external_markdown_links() {- let input = r#"<ancestor-path>-<ancestor-3-page url="https://app.notion.com/p/93fa70f914eb477f89049c38912f9bb1" title=""/>-<ancestor-4-page url="https://app.notion.com/p/dcff3c11ce9847f9b17b4a10eafa4410" title="Parent"/>-</ancestor-path>-See <mention-page url="https://notion.so/roadmap">Roadmap</mention-page>.-<page url="https://notion.so/spec">Product spec</page>-<page url="https://notion.so/draft">Plan \ [draft]</page>"#;-- assert_eq!(- normalize_notion_markdown(input),- "[Notion page](https://app.notion.com/p/93fa70f914eb477f89049c38912f9bb1)\n\-[Parent](https://app.notion.com/p/dcff3c11ce9847f9b17b4a10eafa4410)\n\-See [Roadmap](https://notion.so/roadmap).\n\-[Product spec](https://notion.so/spec)\n\-[Plan \\\\ \\[draft\\]](https://notion.so/draft)"- );-}-#[test]fn notion_prompt_imports_only_source_backed_body_content() {let prompt = prompts::NOTION_IMPORT_SYSTEM;@@ -331,76 +300,9 @@ fn notion_prompt_imports_only_source_backed_body_content() {fn notion_tool_result_wrappers_are_rejected_as_content() {let view_wrapper = r#"Here is the result of "view" for the Page with URL https://app.notion.com/p/39dd7bb70863806ab8afe6d96ecf9701 as of 2026-07-14T21:50:42.234Z:[{"title":""}]"#;- assert!(prepare_notion_markdown(view_wrapper).is_err());- assert!(prepare_notion_markdown(r#"[{"title":"Content team"}]"#).is_err());- assert!(prepare_notion_markdown("# Actual page content").is_ok());-}--#[test]-fn notion_toggles_and_databases_are_not_imported_as_markup() {- let input = r#"# Useful notes {toggle="true"}-Keep this text.-<database url="https://notion.so/database">Content calendar</database>-<mention-database url="https://notion.so/roadmap">Roadmap</mention-database>-End."#;-- assert_eq!(- normalize_notion_markdown(input),- "# Useful notes\nKeep this text.\n\n\nEnd."- );-}--#[test]-fn notion_enhanced_markdown_becomes_macro_markdown() {- let input = r#"<details color="Gray">-<summary>Coming in Q2 (starts <mention-date start="2024-04-01"/>)</summary>- ### Revamp Java PDF Parser- - [ ] Open task- - [x] Assigned to <mention-user url="https://notion.so/user">Teo Nys</mention-user>- <callout icon="🔢" color="Gray">- Numbers are estimates in **weeks**.- </callout>-</details>--<file src="https://notion.so/organizing-polish.docx">Organizing-polish.docx</file>-<span underline="true">Readable text</span>-<unknown url="https://notion.so/unsupported" alt="Linked preview"/>-<empty-block/>"#;-- assert_eq!(- normalize_notion_markdown(input),- "Coming in Q2 (starts April 1, 2024)\n\-### Revamp Java PDF Parser\n\-- [ ] Open task\n\-- [x] Assigned to @Teo Nys\n\-> 🔢 Numbers are estimates in **weeks**.\n\-\n\-[Organizing-polish.docx](https://notion.so/organizing-polish.docx)\n\-Readable text\n\-[Linked preview](https://notion.so/unsupported)"- );-}--#[test]-fn notion_escaped_todos_are_repaired_but_code_examples_are_untouched() {- let input = r#"\[x\] Completed- \[ \] Nested--```text-\[x\] This is an example-<page url="https://notion.so/example">also an example</page>-```"#;-- assert_eq!(- normalize_notion_markdown(input),- "- [x] Completed\n\-\t- [ ] Nested\n\-\n\-```text\n\-\\[x\\] This is an example\n\-<page url=\"https://notion.so/example\">also an example</page>\n\-```"- );+ assert!(check_notion_markdown(view_wrapper).is_err());+ assert!(check_notion_markdown(r#"[{"title":"Content team"}]"#).is_err());+ assert!(check_notion_markdown("# Actual page content").is_ok());}#[test]@@ -422,36 +324,3 @@ fn notion_database_heavy_pages_are_detected() {);assert!(!notion_page_is_mostly_database(&substantive));}--#[test]-fn notion_tables_become_rectangular_macro_pipe_tables() {- let input = r#"<table fit-page-width="true" header-row="true">-<colgroup>-<col width="379">-<col width="230">-</colgroup>-<tr>-<td>AI PDF Editors</td>-<td>Features</td>-<td>Pricing</td>-</tr>-<tr>-lc-</tr>-<tr>-<td>PDFelement</td>-<td>• Automatically bookmarks important pages-• Use Lumi AI to chat with PDFs</td>-<td>Paid plans start at $79.99 per year.</td>-</tr>-<tr><td>Forma | Pro</td><td>Draft with AI</td></tr>-</table>"#;-- assert_eq!(- normalize_notion_markdown(input),- "| AI PDF Editors | Features | Pricing |\n\-| --- | --- | --- |\n\-| PDFelement | • Automatically bookmarks important pages\\n• Use Lumi AI to chat with PDFs | Paid plans start at $79.99 per year. |\n\-| Forma | Pro | Draft with AI | |"- );-}diff --git a/crates/import/src/domain/service/test/admission.rs b/crates/import/src/domain/service/test/admission.rsindex 42cd002282..f5944bd008 100644--- a/crates/import/src/domain/service/test/admission.rs+++ b/crates/import/src/domain/service/test/admission.rs@@ -14,7 +14,6 @@ fn denied() -> AiAdmissionError {struct Admission {error: Mutex<Option<AiAdmissionError>>,calls: AtomicUsize,- allow_first: bool,}impl Admission {@@ -22,7 +21,6 @@ impl Admission {Arc::new(Self {error: Mutex::new(Some(error)),calls: AtomicUsize::new(0),- allow_first: false,})}}@@ -35,14 +33,9 @@ impl AiAdmissionService for Admission {) -> ai_billing::domain::admission::AdmissionFuture<'a> {assert_eq!(caller, &user());assert_eq!(feature, ai_usage::AiFeature::Import);- let call = self.calls.fetch_add(1, Ordering::SeqCst);+ self.calls.fetch_add(1, Ordering::SeqCst);let error = *self.error.lock().unwrap();- Box::pin(async move {- if self.allow_first && call == 0 {- return Ok(());- }- error.map_or(Ok(()), Err)- })+ Box::pin(async move { error.map_or(Ok(()), Err) })}}@@ -73,17 +66,21 @@ impl Repo {user_id: user().to_string(),team_id: None,source,+ kind: source.default_kind(),foreign_id: if source == ImportSource::Slack {"C0123456789".into()} else {Uuid::now_v7().to_string()},+ parent_id: None,+ review_id: None,status: ImportStatus::Staged,- initiator: Initiator::Onboarding,+ initiator: Initiator::Chat,metadata: serde_json::json!({"title": "A page", "name": "general"}),entity_id: None,entity_type: None,last_error: None,+ waiting_on: None,created_at: chrono::Utc::now(),updated_at: chrono::Utc::now(),};@@ -157,7 +154,6 @@ impl ImportRepo for Repo {_: &MacroUserIdStr<'static>,source: ImportSource,from: &[RunStatus],- auto_import: bool,) -> Result<bool> {let mut ledger = self.0.lock().unwrap();if let Some(run) = ledger.runs.iter_mut().find(|run| run.source == source) {@@ -170,32 +166,14 @@ impl ImportRepo for Repo {ledger.runs.push(ImportRun {source,status: RunStatus::Running,- auto_import,error: None,updated_at: chrono::Utc::now(),+ scanned: None,+ review_id: None,});}Ok(true)}- async fn start_manual_run(- &self,- user: &MacroUserIdStr<'static>,- source: ImportSource,- from: &[RunStatus],- ) -> Result<bool> {- let won = self.start_run(user, source, from, false).await?;- if won {- self.0- .lock()- .unwrap()- .runs- .iter_mut()- .find(|run| run.source == source)- .unwrap()- .auto_import = false;- }- Ok(won)- }async fn finish_run(&self,_: &MacroUserIdStr<'static>,@@ -216,25 +194,6 @@ impl ImportRepo for Repo {run.error = error.map(str::to_string);Ok(true)}- async fn transition_run(- &self,- _: &MacroUserIdStr<'static>,- source: ImportSource,- from: &[RunStatus],- to: RunStatus,- ) -> Result<bool> {- let mut ledger = self.0.lock().unwrap();- let run = ledger- .runs- .iter_mut()- .find(|run| run.source == source)- .unwrap();- if !from.contains(&run.status) {- return Ok(false);- }- run.status = to;- Ok(true)- }async fn get(&self,caller: &MacroUserIdStr<'static>,@@ -326,7 +285,7 @@ impl ImportRepo for Repo {async fn fail_stale_importing(&self, _: &MacroUserIdStr<'static>, _: i64) -> Result<u64> {Ok(0)}- async fn reconcile_auto_import_runs(&self, _: &MacroUserIdStr<'static>) -> Result<u64> {+ async fn fail_stale_runs(&self, _: &MacroUserIdStr<'static>, _: i64) -> Result<u64> {Ok(0)}async fn touch_importing(&self, _: &MacroUserIdStr<'static>, _: &[Uuid]) -> Result<u64> {@@ -359,6 +318,7 @@ impl ImportRepo for Repo {&self,_: &MacroUserIdStr<'static>,source: ImportSource,+ _: ImportKind,initiator: Initiator,foreign_id: &str,metadata: &serde_json::Value,@@ -388,6 +348,7 @@ impl ImportRepo for Repo {&self,_: &MacroUserIdStr<'static>,_: ImportSource,+ _: ImportKind,_: Initiator,_: &str,_: &serde_json::Value,@@ -397,35 +358,6 @@ impl ImportRepo for Repo {) -> Result<ImportEntity> {unreachable!()}- async fn discard_staged_by_initiator(- &self,- _: &MacroUserIdStr<'static>,- _: Initiator,- ) -> Result<u64> {- unreachable!()- }- async fn delete_staged_by_initiator(- &self,- _: &MacroUserIdStr<'static>,- _: Initiator,- ) -> Result<u64> {- unreachable!()- }- async fn begin_auto_import(- &self,- _: &MacroUserIdStr<'static>,- _: ImportSource,- ) -> Result<Option<Vec<ImportEntity>>> {- Ok(None)- }- async fn finish_auto_import(- &self,- _: &MacroUserIdStr<'static>,- _: ImportSource,- _: &[Uuid],- ) -> Result<Option<RunStatus>> {- unreachable!()- }}pub(in crate::domain::service) struct NoConnector;@@ -462,11 +394,12 @@ impl EntityCreator for Creator {self.0.fetch_add(1, Ordering::SeqCst);Ok(Uuid::now_v7().to_string())}- async fn create_markdown_doc(+ async fn create_notion_document(&self,_: &MacroUserIdStr<'static>,_: &str,_: &str,+ _: &NotionLinks,_: &ImportedDocumentProperties,) -> anyhow::Result<String> {self.0.fetch_add(1, Ordering::SeqCst);@@ -534,166 +467,6 @@ fn notion_tools(truncated: bool) -> Arc<Tools> {})}-#[tokio::test]-async fn initial_gather_and_retry_refuse_before_running_or_connector_calls() {- for error in [denied(), AiAdmissionError::Unavailable] {- let admission = Admission::refusing(error);- let service = service(admission.clone());- for source in [ImportSource::Linear, ImportSource::Notion] {- assert!(- matches!(service.start_gather(user(), source, true).await, Err(ImportError::Admission(e)) if e == error)- );- assert!(service.repo.list_runs(&user()).await.unwrap().is_empty());- assert!(- matches!(service.retry_gather(user(), source).await, Err(ImportError::Admission(e)) if e == error)- );- }- service- .repo- .start_run(&user(), ImportSource::Linear, &[], true)- .await- .unwrap();- service- .repo- .finish_run(- &user(),- ImportSource::Linear,- RunStatus::Failed,- Some("old failure"),- )- .await- .unwrap();- assert!(- matches!(service.retry_gather(user(), ImportSource::Linear).await, Err(ImportError::Admission(e)) if e == error)- );- let run = service- .repo- .list_runs(&user())- .await- .unwrap()- .pop()- .unwrap();- assert_eq!(run.status, RunStatus::Failed);- assert!(run.auto_import);- assert_eq!(run.error.as_deref(), Some("old failure"));- service- .dismiss_run(user(), ImportSource::Linear)- .await- .unwrap();- let calls = admission.calls.load(Ordering::SeqCst);- assert!(- !service- .start_gather(user(), ImportSource::Linear, false)- .await- .unwrap()- );- assert_eq!(admission.calls.load(Ordering::SeqCst), calls);- assert_eq!(- service.state(user()).await.unwrap().runs[0].status,- RunStatus::Dismissed- );- }-}--#[tokio::test]-async fn queued_gather_rechecks_and_finishes_failed_without_loading_tools() {- for error in [denied(), AiAdmissionError::Unavailable] {- let admission = Arc::new(Admission {- error: Mutex::new(Some(error)),- calls: AtomicUsize::new(0),- allow_first: true,- });- let service = service(admission.clone());- assert!(- service- .start_gather(user(), ImportSource::Notion, true)- .await- .unwrap()- );- tokio::time::timeout(Duration::from_secs(2), async {- loop {- let run = service- .repo- .list_runs(&user())- .await- .unwrap()- .pop()- .unwrap();- if run.status == RunStatus::Failed {- assert!(run.error.unwrap().starts_with(error.code()));- assert!(run.auto_import);- break;- }- tokio::task::yield_now().await;- }- })- .await- .unwrap();- assert_eq!(admission.calls.load(Ordering::SeqCst), 2);- }-}--#[tokio::test]-async fn active_gather_retries_are_noops_and_failed_retries_preserve_configuration() {- let admission = Admission::refusing(AiAdmissionError::Unavailable);- let service = service(admission.clone());- service- .repo- .start_run(&user(), ImportSource::Linear, &[], true)- .await- .unwrap();- assert!(- !service- .retry_gather(user(), ImportSource::Linear)- .await- .unwrap()- );- assert_eq!(admission.calls.load(Ordering::SeqCst), 0);- service- .repo- .finish_run(- &user(),- ImportSource::Linear,- RunStatus::Failed,- Some("interrupted"),- )- .await- .unwrap();-- // Admission recovers for the retry, then fails again when the task starts.- let admission = Arc::new(Admission {- error: Mutex::new(Some(AiAdmissionError::Unavailable)),- calls: AtomicUsize::new(0),- allow_first: true,- });- let service = service.with_admission(admission);- assert!(- service- .retry_gather(user(), ImportSource::Linear)- .await- .unwrap()- );- tokio::time::timeout(Duration::from_secs(2), async {- loop {- let run = service- .repo- .list_runs(&user())- .await- .unwrap()- .pop()- .unwrap();- if run.status == RunStatus::Failed {- assert!(run.auto_import);- assert!(run.error.unwrap().starts_with("ai_billing_unavailable"));- break;- }- tokio::task::yield_now().await;- }- })- .await- .unwrap();-}-#[tokio::test]async fn default_constructor_keeps_admission_disabled() {let service = ImportServiceImpl::new(@@ -703,43 +476,6 @@ async fn default_constructor_keeps_admission_disabled() {Arc::new(ai_usage::NoOpUsageRecorder),);service.admit_ai(&user()).await.unwrap();- assert!(- service- .prepare_gather(&user(), ImportSource::Notion, &[])- .await- .unwrap()- );-}--#[tokio::test]-async fn slack_agent_fallback_still_requires_admission() {- let admission = Admission::refusing(denied());- let service = service(admission.clone());- assert!(- service- .prepare_gather(&user(), ImportSource::Slack, &[])- .await- .unwrap()- );- assert_eq!(admission.calls.load(Ordering::SeqCst), 0);- let tools = Arc::new(Tools {- name: "unused",- result: serde_json::json!({}),- calls: AtomicUsize::new(0),- });- let error = service- .gather_agent_session(- &user(),- ImportSource::Slack,- GatherMode::Onboarding,- GATHER_MODEL,- tools.clone(),- )- .await- .unwrap_err();- assert_eq!(error.downcast_ref::<AiAdmissionError>(), Some(&denied()));- assert_eq!(admission.calls.load(Ordering::SeqCst), 1);- assert_eq!(tools.calls.load(Ordering::SeqCst), 0);}#[tokio::test]diff --git a/crates/import/src/inbound/axum_router.rs b/crates/import/src/inbound/axum_router.rsindex ca40e17a20..3607f7255d 100644--- a/crates/import/src/inbound/axum_router.rs+++ b/crates/import/src/inbound/axum_router.rs@@ -1,8 +1,8 @@//! HTTP surface for the import pipeline.-use crate::domain::models::{ImportSource, ImportState};+use crate::domain::models::{ImportSource, ImportState, NotionImportReport};use crate::domain::ports::ImportError;-use crate::domain::service::{ImportService, RunImportOutcome};+use crate::domain::service::{ImportService, NotionWorkspaceImporter, RunImportOutcome};use axum::{Json, Router,extract::{FromRef, Path, State},@@ -68,28 +68,75 @@ impl<T, Auth> FromRef<ImportRouterState<T, Auth>> for MacroAuthorizationState<Au/// Build the import router.pub fn import_router<T, Auth, S>(state: ImportRouterState<T, Auth>) -> Router<S>where- T: ImportService,+ T: ImportService + NotionWorkspaceImporter,Auth: MacroAuthorizationService,S: Send + Sync + Clone + 'static,{Router::new().route("/import/state", get(get_state_handler::<T, Auth>)).route("/import/run", post(run_import_handler::<T, Auth>))- .route(- "/import/runs/{source}/retry",- post(retry_gather_handler::<T, Auth>),- ).route("/import/runs/{source}/discover",post(discover_handler::<T, Auth>),)+ .route("/import/notion", get(notion_report_handler::<T, Auth>)).route(- "/import/runs/{source}/dismiss",- post(dismiss_run_handler::<T, Auth>),+ "/import/notion/cancel",+ post(cancel_notion_handler::<T, Auth>),).with_state(state)}+/// Where the user's Notion workspace import stands.+#[utoipa::path(+ get,+ path = "/import/notion",+ responses(+ (status = 200, body = NotionImportReport),+ (status = 500, description = "Internal server error"),+ ),+ tag = "import"+)]+#[tracing::instrument(skip(service, user), fields(user_id = %user.authorization.user.macro_user_id))]+pub async fn notion_report_handler<T: NotionWorkspaceImporter, Auth: MacroAuthorizationService>(+ State(service): State<Arc<T>>,+ user: MacroAuthorizationExtractor<Auth, UserOrInternal>,+) -> Response {+ match service+ .notion_import_report(&user.authorization.user.macro_user_id)+ .await+ {+ Ok(report) => Json(report).into_response(),+ Err(e) => error_response(e),+ }+}++/// Stop planning or writing the user's Notion import. Objects already+/// imported stay.+#[utoipa::path(+ post,+ path = "/import/notion/cancel",+ responses(+ (status = 200, body = NotionImportReport, description = "Where the import stands after cancelling"),+ (status = 500, description = "Internal server error"),+ ),+ tag = "import"+)]+#[tracing::instrument(skip(service, user), fields(user_id = %user.authorization.user.macro_user_id))]+pub async fn cancel_notion_handler<T: NotionWorkspaceImporter, Auth: MacroAuthorizationService>(+ State(service): State<Arc<T>>,+ user: MacroAuthorizationExtractor<Auth, UserOrInternal>,+) -> Response {+ let user = user.authorization.user.macro_user_id;+ if let Err(e) = service.cancel_notion_import(&user).await {+ return error_response(e);+ }+ match service.notion_import_report(&user).await {+ Ok(report) => Json(report).into_response(),+ Err(e) => error_response(e),+ }+}+fn error_response(e: ImportError) -> Response {if let ImportError::Admission(error) = e {return error.into_response();@@ -106,7 +153,7 @@ fn unknown_source_response(source: &str) -> Response {.into_response()}-/// Get the authenticated user's import state: gather runs plus visible+/// Get the authenticated user's import state: discovery runs plus visible/// ledger rows.#[utoipa::path(get,@@ -158,46 +205,14 @@ pub async fn run_import_handler<T: ImportService, Auth: MacroAuthorizationServic}}-/// Restart a failed (or dismissed) gather run for one source.-#[utoipa::path(- post,- path = "/import/runs/{source}/retry",- params(("source" = String, Path, description = "Import source")),- responses(- (status = 204, description = "Retry accepted (idempotent)"),- (status = 400, description = "Unknown import source"),- (status = 402, description = "AI allowance exhausted", body = ai_billing::inbound::admission::AiAdmissionErrorBody),- (status = 503, description = "AI usage validation unavailable; retry later", body = ai_billing::inbound::admission::AiAdmissionErrorBody),- (status = 500, description = "Internal server error"),- ),- tag = "import"-)]-#[tracing::instrument(skip(service, user), fields(user_id = %user.authorization.user.macro_user_id))]-pub async fn retry_gather_handler<T: ImportService, Auth: MacroAuthorizationService>(- State(service): State<Arc<T>>,- user: MacroAuthorizationExtractor<Auth, UserOrInternal>,- Path(source): Path<String>,-) -> Response {- let Ok(source) = ImportSource::from_str(&source) else {- return unknown_source_response(&source);- };- match service- .retry_gather(user.authorization.user.macro_user_id, source)- .await- {- Ok(_) => StatusCode::NO_CONTENT.into_response(),- Err(e) => error_response(e),- }-}--/// Discover candidates for user selection without auto-importing them.+/// Discover candidates for user selection. Only Slack supports discovery.#[utoipa::path(post,path = "/import/runs/{source}/discover",params(("source" = String, Path, description = "Import source")),responses((status = 204, description = "Discovery accepted (idempotent)"),- (status = 400, description = "Unknown import source"),+ (status = 400, description = "Unknown import source or discovery not supported"),(status = 500, description = "Internal server error"),),tag = "import"@@ -216,36 +231,9 @@ pub async fn discover_handler<T: ImportService, Auth: MacroAuthorizationService>.await{Ok(_) => StatusCode::NO_CONTENT.into_response(),- Err(e) => error_response(e),- }-}--/// Dismiss one source's import section.-#[utoipa::path(- post,- path = "/import/runs/{source}/dismiss",- params(("source" = String, Path, description = "Import source")),- responses(- (status = 204, description = "Dismissed"),- (status = 400, description = "Unknown import source"),- (status = 500, description = "Internal server error"),- ),- tag = "import"-)]-#[tracing::instrument(skip(service, user), fields(user_id = %user.authorization.user.macro_user_id))]-pub async fn dismiss_run_handler<T: ImportService, Auth: MacroAuthorizationService>(- State(service): State<Arc<T>>,- user: MacroAuthorizationExtractor<Auth, UserOrInternal>,- Path(source): Path<String>,-) -> Response {- let Ok(source) = ImportSource::from_str(&source) else {- return unknown_source_response(&source);- };- match service- .dismiss_run(user.authorization.user.macro_user_id, source)- .await- {- Ok(()) => StatusCode::NO_CONTENT.into_response(),+ Err(e @ ImportError::UnsupportedDiscovery(_)) => {+ (StatusCode::BAD_REQUEST, e.to_string()).into_response()+ }Err(e) => error_response(e),}}diff --git a/crates/import/src/inbound/axum_router/test.rs b/crates/import/src/inbound/axum_router/test.rsindex 44d088b5f6..0c8e4f322a 100644--- a/crates/import/src/inbound/axum_router/test.rs+++ b/crates/import/src/inbound/axum_router/test.rs@@ -2,13 +2,14 @@ use super::*;use ai_billing::{AiAdmissionError, DenyReason};use axum::{body::Body, http::Request};use macro_authorization::{InternalIdentityClaims, MacroAuthorizationError};+use macro_user_id::cowlike::CowLike;use macro_user_id::user_id::MacroUserIdStr;use model_user::UserContext;use rootcause::Report;use std::sync::Mutex;use tower::ServiceExt;-use crate::domain::{models::Initiator, ports::Result};+use crate::domain::ports::Result;const TEST_USER: &str = "macro|[email protected]";@@ -43,45 +44,72 @@ impl MacroAuthorizationService for FakeAuthorization {struct FakeImportService {calls: Mutex<Vec<(MacroUserIdStr<'static>, ImportSource)>>,outcome: Mutex<Option<Result<bool>>>,+ cancels: Mutex<Vec<MacroUserIdStr<'static>>>,}-impl ImportService for FakeImportService {- async fn start_discovery(+impl NotionWorkspaceImporter for FakeImportService {+ async fn prepare_notion_import(&self,- user: MacroUserIdStr<'static>,- source: ImportSource,- ) -> Result<bool> {- self.calls.lock().unwrap().push((user, source));- self.outcome.lock().unwrap().take().unwrap()+ _user: &MacroUserIdStr<'static>,+ _scope: import_notion::domain::model::ImportScope,+ _sharing: Option<crate::domain::models::NotionSharing>,+ _wait: std::time::Duration,+ ) -> Result<NotionImportReport> {+ unimplemented!("routes never plan")}- async fn state(&self, _user: MacroUserIdStr<'static>) -> Result<ImportState> {- unimplemented!("discovery must not read state")+ async fn notion_import_report(+ &self,+ _user: &MacroUserIdStr<'static>,+ ) -> Result<NotionImportReport> {+ let cancelled = !self.cancels.lock().unwrap().is_empty();+ Ok(NotionImportReport {+ status: Some(if cancelled {+ crate::domain::models::RunStatus::Dismissed+ } else {+ crate::domain::models::RunStatus::Importing+ }),+ imported: 3,+ ..NotionImportReport::default()+ })}- async fn start_gather(+ async fn attach_notion_review(&self,- _user: MacroUserIdStr<'static>,- _source: ImportSource,- _auto_import: bool,- ) -> Result<bool> {- unimplemented!("discovery must not start an onboarding gather")+ _user: &MacroUserIdStr<'static>,+ _review_id: Uuid,+ _session_id: agent_session::domain::model::AgentSessionId,+ ) -> Result<u64> {+ unimplemented!("routes never attach reviews")}- async fn retry_gather(+ async fn start_notion_write(&self,- _user: MacroUserIdStr<'static>,- _source: ImportSource,- ) -> Result<bool> {- unimplemented!("discovery must not retry a gather")+ _user: &MacroUserIdStr<'static>,+ _review_id: Uuid,+ _choices: crate::domain::models::NotionWriteChoices,+ ) -> Result<NotionImportReport> {+ unimplemented!("routes never write")}- async fn dismiss_run(+ async fn cancel_notion_import(&self, user: &MacroUserIdStr<'static>) -> Result<bool> {+ self.cancels.lock().unwrap().push(user.clone());+ Ok(true)+ }+}++impl ImportService for FakeImportService {+ async fn start_discovery(&self,- _user: MacroUserIdStr<'static>,- _source: ImportSource,- ) -> Result<()> {- unimplemented!()+ user: MacroUserIdStr<'static>,+ source: ImportSource,+ ) -> Result<bool> {+ self.calls.lock().unwrap().push((user, source));+ self.outcome.lock().unwrap().take().unwrap()+ }++ async fn state(&self, _user: MacroUserIdStr<'static>) -> Result<ImportState> {+ unimplemented!("discovery must not read state")}async fn run_import(@@ -92,28 +120,13 @@ impl ImportService for FakeImportService {) -> Result<RunImportOutcome> {unimplemented!("discovery must not import candidates")}-- async fn discard_staged_by_initiator(- &self,- _user: MacroUserIdStr<'static>,- _initiator: Initiator,- ) -> Result<u64> {- unimplemented!()- }-- async fn delete_staged_by_initiator(- &self,- _user: MacroUserIdStr<'static>,- _initiator: Initiator,- ) -> Result<u64> {- unimplemented!()- }}fn discovery_router(outcome: Result<bool>) -> (Router, Arc<FakeImportService>) {let service = Arc::new(FakeImportService {calls: Mutex::new(Vec::new()),outcome: Mutex::new(Some(outcome)),+ cancels: Mutex::new(Vec::new()),});let router = import_router(ImportRouterState {service: service.clone(),@@ -149,17 +162,24 @@ async fn discovery_rejects_unknown_source_without_calling_service() {}#[tokio::test]-async fn discovery_accepts_agent_gathered_sources() {- for (path, source) in [- ("linear", ImportSource::Linear),- ("notion", ImportSource::Notion),- ] {- let (router, service) = discovery_router(Ok(true));- let response = router.oneshot(discovery_request(path, true)).await.unwrap();+async fn discovery_returns_unsupported_source_message() {+ let (router, service) =+ discovery_router(Err(ImportError::UnsupportedDiscovery(ImportSource::Linear)));+ let response = router+ .oneshot(discovery_request("linear", true))+ .await+ .unwrap();- assert_eq!(response.status(), StatusCode::NO_CONTENT);- assert_eq!(service.calls.lock().unwrap()[0].1, source);- }+ assert_eq!(response.status(), StatusCode::BAD_REQUEST);+ let body = axum::body::to_bytes(response.into_body(), 4096)+ .await+ .unwrap();+ assert!(+ std::str::from_utf8(&body)+ .unwrap()+ .contains("not supported")+ );+ assert_eq!(service.calls.lock().unwrap()[0].1, ImportSource::Linear);}#[tokio::test]@@ -243,3 +263,33 @@ fn other_errors_remain_internal() {let response = error_response(ImportError::Other(anyhow::anyhow!("private diagnostic")));assert_eq!(response.status(), StatusCode::INTERNAL_SERVER_ERROR);}++#[tokio::test]+async fn cancelling_a_notion_import_cancels_it_for_the_caller_and_reports_where_it_stopped() {+ let (router, service) = discovery_router(Ok(true));+ let response = router+ .oneshot(+ Request::builder()+ .method("POST")+ .uri("/import/notion/cancel")+ .header("authorization", "Bearer valid")+ .body(Body::empty())+ .unwrap(),+ )+ .await+ .unwrap();++ assert_eq!(response.status(), StatusCode::OK);+ assert_eq!(+ service.cancels.lock().unwrap().as_slice(),+ [MacroUserIdStr::parse_from_str(TEST_USER)+ .unwrap()+ .into_owned()]+ );+ let body = axum::body::to_bytes(response.into_body(), 4096)+ .await+ .unwrap();+ let report: serde_json::Value = serde_json::from_slice(&body).unwrap();+ assert_eq!(report["status"], "dismissed");+ assert_eq!(report["imported"], 3);+}diff --git a/crates/import/src/inbound/toolset.rs b/crates/import/src/inbound/toolset.rsindex 18339c36f8..20a8079221 100644--- a/crates/import/src/inbound/toolset.rs+++ b/crates/import/src/inbound/toolset.rs@@ -1,21 +1,15 @@//! AI tools over the import ledger.//!-//! One tool surface serves three callers, differing only in the-//! [`ToolPolicy`] baked into the context:-//! - chat sessions get the ledger tools plus the single-page Notion import-//! workflow, staging as `initiator = chat`;-//! - gather jobs get only `CreateImportEntity`, locked to one source and-//! `initiator = onboarding`, staging only;-//! - Notion import jobs get only `FinalizeImport`.-//!-//! The policy lives server-side in the tool context — the model can never-//! spoof another user, source, or initiator.+//! Chat sessions get the ledger tools plus the single-page Notion import+//! workflow, staging as `initiator = chat`; Notion import jobs get only+//! `FinalizeImport`. The user comes from the request context, so the model+//! can never stage on another user's behalf.use crate::domain::models::{ImportEntity, ImportSource, ImportStatus, Initiator, metadata_label};use crate::domain::ports::{ImportError, ImportedDocumentProperties, ImportedDocumentProperty};use crate::domain::service::{DiscardOutcome, ImportFinalizer, ImportNotionPageOutcome, ImportStager, NotionPageImporter,- StageOutcome,+ NotionWorkspaceImporter, StageOutcome,};use ai_toolset::{AsyncTool, AsyncToolCollection, RequestContext, ServiceContext, ToolCallError, ToolResult,@@ -27,51 +21,12 @@ use serde::{Deserialize, Serialize};use std::sync::Arc;use uuid::Uuid;-#[cfg(test)]-mod test;--/// What a given toolset instance is allowed to do; baked in server-side.-#[derive(Debug, Clone, Copy)]-pub struct ToolPolicy {- /// The initiator recorded on rows staged through this context.- pub initiator: Initiator,- /// When set, staging is locked to this source (gather jobs).- pub forced_source: Option<ImportSource>,- /// Whether `status = imported` writes are allowed (chat agents that- /// already created the entity).- pub allow_imported: bool,-}--impl ToolPolicy {- /// Policy for interactive chat sessions.- pub fn chat() -> Self {- Self {- initiator: Initiator::Chat,- forced_source: None,- allow_imported: true,- }- }+mod import_from_notion;- /// Policy for a gather job over one connector, recording `initiator`- /// (onboarding suggestions or a user-started discovery).- pub fn gather(source: ImportSource, initiator: Initiator) -> Self {- Self {- initiator,- forced_source: Some(source),- allow_imported: false,- }- }+pub use import_from_notion::{ImportFromNotion, ImportFromNotionResponse, NotionImportAction};- /// Policy for a Notion import job (only `FinalizeImport` is registered,- /// which ignores staging policy).- pub fn import_job() -> Self {- Self {- initiator: Initiator::Onboarding,- forced_source: Some(ImportSource::Notion),- allow_imported: false,- }- }-}+#[cfg(test)]+mod test;/// Service context for import AI tools.///@@ -82,36 +37,28 @@ impl ToolPolicy {pub struct ImportToolContext<T> {/// The import service, where the host wired one.pub service: Option<Arc<T>>,- /// What this toolset instance may do.- pub policy: ToolPolicy,}impl<T> Clone for ImportToolContext<T> {fn clone(&self) -> Self {Self {service: self.service.clone(),- policy: self.policy,}}}impl<T> ImportToolContext<T> {- /// A chat-policy context backed by a real import service.+ /// A context backed by a real import service.pub fn wired(service: Arc<T>) -> Self {Self {service: Some(service),- policy: ToolPolicy::chat(),}}- /// A chat-policy context with no import service: every tool call fails- /// with a clear error. For hosts that share the toolset but cannot- /// build the service.+ /// A context with no import service: every tool call fails with a clear+ /// error. For hosts that share the toolset but cannot build the service.pub fn unwired() -> Self {- Self {- service: None,- policy: ToolPolicy::chat(),- }+ Self { service: None }}fn require_service(&self) -> ToolResult<&Arc<T>> {@@ -123,20 +70,16 @@ impl<T> ImportToolContext<T> {}/// The import toolset for interactive chat.-pub fn import_toolset<T: ImportStager + NotionPageImporter>()+pub fn import_toolset<T: ImportStager + NotionPageImporter + NotionWorkspaceImporter>()-> AsyncToolCollection<ImportToolContext<T>> {AsyncToolCollection::new().add_tool::<CreateImportEntity, ImportToolContext<T>>().add_tool::<ImportNotionPage, ImportToolContext<T>>()+ .add_tool::<ImportFromNotion, ImportToolContext<T>>().add_tool::<DeleteImportEntity, ImportToolContext<T>>().add_tool::<ListImportEntities, ImportToolContext<T>>()}-/// The staging-only toolset for gather jobs.-pub fn gather_toolset<T: ImportStager>() -> AsyncToolCollection<ImportToolContext<T>> {- AsyncToolCollection::new().add_tool::<CreateImportEntity, ImportToolContext<T>>()-}-/// The finalize-only toolset for Notion import jobs.pub fn notion_import_toolset<T: ImportFinalizer>() -> AsyncToolCollection<ImportToolContext<T>> {AsyncToolCollection::new().add_tool::<FinalizeImport, ImportToolContext<T>>()@@ -270,20 +213,6 @@ impl<T: ImportStager> AsyncTool<ImportToolContext<T>> for CreateImportEntity {request_context: RequestContext,) -> ToolResult<Self::Output> {let user = request_context.user_id.clone();- let policy = service_context.policy;-- if let Some(forced) = policy.forced_source- && self.source != forced- {- return Err(tool_error(- format!(- "This session only stages {} items; got {}.",- forced.as_ref(),- self.source.as_ref()- ),- anyhow::anyhow!("forced-source policy violation"),- ));- }match self.status {CreateImportStatus::Staged => {@@ -291,7 +220,7 @@ impl<T: ImportStager> AsyncTool<ImportToolContext<T>> for CreateImportEntity {.require_service()?.stage(&user,- policy.initiator,+ Initiator::Chat,self.source,&self.foreign_id,self.metadata.clone(),@@ -301,14 +230,6 @@ impl<T: ImportStager> AsyncTool<ImportToolContext<T>> for CreateImportEntity {Ok(stage_response(outcome, user.as_ref()))}CreateImportStatus::Imported => {- if !policy.allow_imported {- return Err(tool_error(- "This session can only stage candidates (status `staged`); it cannot \- record imported entities."- .to_string(),- anyhow::anyhow!("imported-status policy violation"),- ));- }let Some(entity_id) = self.entity_id.as_deref()@@ -326,7 +247,7 @@ impl<T: ImportStager> AsyncTool<ImportToolContext<T>> for CreateImportEntity {.require_service()?.record_imported(&user,- policy.initiator,+ Initiator::Chat,self.source,&self.foreign_id,self.metadata.clone(),diff --git a/crates/import/src/inbound/toolset/import_from_notion.rs b/crates/import/src/inbound/toolset/import_from_notion.rsnew file mode 100644index 0000000000..d75018973b--- /dev/null+++ b/crates/import/src/inbound/toolset/import_from_notion.rs@@ -0,0 +1,311 @@+//! The agent's one tool for importing a Notion workspace.++use std::time::Duration;++use ai_toolset::{+ AsyncTool, RequestContext, ServiceContext, ToolAnnotated, ToolAnnotations, ToolResult,+};+use async_trait::async_trait;+use import_notion::domain::model::{ImportScope, NotionId};+use schemars::JsonSchema;+use serde::{Deserialize, Serialize};++use super::{ImportToolContext, import_error, tool_error};+use crate::domain::models::{NotionImportReport, NotionSharing, RunStatus};+use crate::domain::ports::ImportError;+use crate::domain::service::NotionWorkspaceImporter;++#[cfg(test)]+mod test;++/// How long `plan` waits for planning before handing back a plan that is+/// still being made: long enough for a tiny workspace to come back ready,+/// short enough that the agent presents the review while it fills in.+const PLAN_WAIT: Duration = Duration::from_secs(5);++/// What the tool is asked to do.+#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, JsonSchema)]+#[serde(rename_all = "snake_case")]+pub enum NotionImportAction {+ /// Read the workspace and plan the import, replacing any earlier plan.+ Plan,+ /// Report where planning or importing stands.+ Status,+ /// Stop planning or importing; what is already imported stays.+ Cancel,+}++/// Plan, check on, or cancel an import of the user's Notion workspace.+#[derive(Debug, Clone, Deserialize, JsonSchema)]+#[serde(rename_all = "camelCase")]+#[schemars(+ title = "ImportFromNotion",+ description = "Import the user's Notion workspace (or part of it) into Macro: pages become documents in folders mirroring Notion's page tree, databases become Macro databases with their rows and relations, and task-tracker databases become Macro tasks. `plan` reads the workspace through the user's Notion connection, decides per page and database what to do (and, with `sharing` \"ai\", who sees it), links anything a teammate already imported and shared, and stages a plan without writing anything. Who sees the import: unless the user already said, call `plan` without `sharing` first. A user who is not on a team gets a personal import straight away; tell them so and do not offer team sharing. For a user on a team it answers that `sharing` is required: then ask them in one short question with three options (shared with their team, only them, or let AI decide per page), do not guess, and plan again with their answer. `plan` answers within seconds while planning goes on in the background: present the plan right away with PresentReview (kind \"import\", drafts [{\"source\":\"notion\"}]). The review fills in as Notion is read and each object is decided, and the user approves it there once it is ready; do not wait for planning or check `status` in a loop. Approving that review writes the plan; never import plan items with other tools. End your message after presenting it by asking \"Should I kick it off?\"; if the user says yes, approve it yourself with DecideReview, submitting the reviewId PresentReview returned. `status` reports progress; `cancel` stops planning or importing. Use this for anything bigger than a single page instead of ImportNotionPage."+)]+pub struct ImportFromNotion {+ /// What to do.+ pub action: NotionImportAction,+ /// For `plan`: Notion page or database URLs (or ids) to import, each+ /// with everything under it. Leave empty to plan everything the+ /// connection can see.+ #[serde(default)]+ pub roots: Vec<String>,+ /// For `plan`: who sees what the import writes, as the user answered+ /// when asked: `team` shares everything with their team, `personal`+ /// keeps everything visible only to them, `ai` lets the decision model+ /// choose per page. Leave it out for a user who is not on a team.+ #[serde(default)]+ pub sharing: Option<NotionSharing>,+}++/// The tool's answer.+#[derive(Debug, Serialize, JsonSchema)]+#[serde(rename_all = "camelCase")]+pub struct ImportFromNotionResponse {+ /// Where the import stands.+ pub report: NotionImportReport,+ /// What happened and what to do next.+ pub message: String,+}++impl ToolAnnotated for ImportFromNotion {+ const ANNOTATIONS: ToolAnnotations =+ ToolAnnotations::additive("Import from Notion").with_open_world();+}++#[async_trait]+impl<T: NotionWorkspaceImporter> AsyncTool<ImportToolContext<T>> for ImportFromNotion {+ type Output = ImportFromNotionResponse;++ #[tracing::instrument(skip_all, fields(user_id = %request_context.user_id, action = ?self.action, roots = self.roots.len(), sharing = ?self.sharing), err)]+ async fn call(+ &self,+ service_context: ServiceContext<ImportToolContext<T>>,+ request_context: RequestContext,+ ) -> ToolResult<Self::Output> {+ let service = service_context.require_service()?;+ let user = request_context.user_id.clone();+ let report = match self.action {+ NotionImportAction::Plan => {+ let scope = scope(&self.roots)?;+ let planned = service.prepare_notion_import(&user, scope, self.sharing, PLAN_WAIT);+ tokio::select! {+ report = planned => report,+ // Planning carries on in the background; report where it is.+ () = request_context.cancel.cancelled() => service.notion_import_report(&user).await,+ }+ }+ NotionImportAction::Status => service.notion_import_report(&user).await,+ NotionImportAction::Cancel => match service.cancel_notion_import(&user).await {+ Ok(_) => service.notion_import_report(&user).await,+ Err(error) => Err(error),+ },+ }+ .map_err(|error| match error {+ ImportError::NotionSharingRequired => tool_error(+ "The user is on a team, so `sharing` is required for `plan`. Ask them whether the import should be shared with their team, kept to themselves, or left to AI to decide per page, then plan again with their answer.".to_string(),+ error,+ ),+ ImportError::NotionNotConnected => tool_error(+ "Notion is not connected. Ask the user to connect Notion, then plan again."+ .to_string(),+ error,+ ),+ other => import_error("Notion import failed", other),+ })?;+ let message = message(self.action, &report);+ Ok(ImportFromNotionResponse { report, message })+ }+}++fn scope(roots: &[String]) -> ToolResult<ImportScope> {+ if roots.iter().all(|root| root.trim().is_empty()) {+ return Ok(ImportScope::Everything);+ }+ let ids = roots+ .iter()+ .filter(|root| !root.trim().is_empty())+ .map(|root| {+ NotionId::parse(root).ok_or_else(|| {+ tool_error(+ format!("`{root}` is not a Notion page or database URL or id."),+ anyhow::anyhow!("unparseable Notion root"),+ )+ })+ })+ .collect::<ToolResult<Vec<_>>>()?;+ Ok(ImportScope::Roots { ids })+}++/// What the model should know and do next.+pub(super) fn message(action: NotionImportAction, report: &NotionImportReport) -> String {+ let planned = planned_summary(report);+ let already = already_in_macro(report);+ let hand = if report.hand_items.is_empty() {+ String::new()+ } else {+ format!(+ " Objects to handle by hand (with your own tools, after the import): {}.",+ report+ .hand_items+ .iter()+ .map(|item| match &item.reason {+ Some(reason) => format!("\"{}\" ({reason})", item.title),+ None => format!("\"{}\"", item.title),+ })+ .collect::<Vec<_>>()+ .join(", ")+ )+ };+ let personal = if report.on_team {+ ""+ } else {+ " The user is not on a team, so everything this import writes is visible only to them; do not offer to share it with a team."+ };+ let present = "PresentReview (kind \"import\", a short title such as \"Import from Notion\", drafts [{\"source\":\"notion\"}])";+ match (action, report.status) {+ (NotionImportAction::Cancel, _) => format!(+ "The Notion import is stopped. {} objects were already imported and stay in Macro.",+ report.imported+ ),+ (_, Some(RunStatus::Running)) => {+ let progress = planning_progress(report);+ match report.review_id {+ None => format!(+ "Planning has started and goes on in the background{progress}.{personal} Present the plan now with {present} and tell the user in a sentence that the review fills in as Notion is read and each object is decided, and that they approve it there once it is ready. Do not wait for planning or check `status` in a loop. End your message by asking \"Should I kick it off once it's ready?\". If they say yes, approve it with DecideReview (a submit decision); while the plan is still being made it refuses, so tell them it is not ready yet and that they can say so again, or approve it on the card, once it is."+ ),+ Some(_) => format!(+ "The plan is presented and still being made{progress}.{personal} The review fills in as planning goes, and the user approves it there once it is ready. Do not wait for planning or check `status` in a loop."+ ),+ }+ }+ (_, Some(RunStatus::Ready)) if report.review_id.is_none() => {+ let choose = if report.on_team {+ "; they can change who sees each object on the card"+ } else {+ ""+ };+ format!(+ "The plan is ready: {planned}.{already}{personal} Tell the user these numbers as they are, then present the plan now with {present}. Nothing is written until they approve it{choose}. End your message by asking \"Should I kick it off?\"; if they say yes, approve it with DecideReview (a submit decision).{hand}"+ )+ }+ (_, Some(RunStatus::Ready)) => {+ format!(+ "The plan ({planned}) is presented and waiting for the user's approval. If they tell you to go ahead, approve it with DecideReview (a submit decision).{already}{hand}"+ )+ }+ (_, Some(RunStatus::Importing)) => format!(+ "Importing: {} done, {} being written, {} waiting.{}",+ report.imported,+ report.writing,+ report.waiting,+ waiting_on_teammates(report)+ ),+ (_, Some(RunStatus::Completed)) => format!(+ "The import finished: {} objects imported{}.{}{hand}",+ report.imported,+ report+ .error+ .as_deref()+ .map(|error| format!(" ({error})"))+ .unwrap_or_default(),+ waiting_on_teammates(report)+ ),+ (_, Some(RunStatus::Failed)) => format!(+ "The Notion import failed: {}. Fix the cause and plan again.",+ report.error.as_deref().unwrap_or("unknown error")+ ),+ (_, Some(RunStatus::Dismissed)) => "The last Notion import was cancelled.".to_string(),+ (_, None) => "No Notion import has been planned yet. Use action `plan`.".to_string(),+ }+}++/// How far planning has got: what Notion has shown so far while it is read,+/// then how many staged objects the decision model has answered for.+fn planning_progress(report: &NotionImportReport) -> String {+ let staged = report.pages + report.data_sources;+ if staged > 0 {+ return format!(+ " ({} of {staged} objects decided so far)",+ staged.saturating_sub(report.deciding)+ );+ }+ report+ .scanned+ .map(|scanned| {+ format!(+ " ({} pages and {} databases found in Notion so far)",+ scanned.pages, scanned.data_sources+ )+ })+ .unwrap_or_default()+}++/// The plan in exact numbers, so the agent repeats what will really be+/// written rather than a count of top-level objects.+pub(super) fn planned_summary(report: &NotionImportReport) -> String {+ let mut parts = Vec::new();+ if report.documents > 0 {+ parts.push(format!("{} pages as documents", report.documents));+ }+ if report.databases > 0 {+ parts.push(format!(+ "{} databases with {} rows",+ report.databases, report.database_rows+ ));+ }+ if report.tasks > 0 {+ parts.push(format!(+ "{} task lists as {} tasks",+ report.tasks, report.task_rows+ ));+ }+ let writes = if parts.is_empty() {+ "nothing".to_string()+ } else {+ parts.join(", ")+ };+ let shared = if report.copy + report.tasks == 0 {+ String::new()+ } else if !report.on_team {+ "; all of it visible only to the user".to_string()+ } else {+ let personal = (report.copy + report.tasks).saturating_sub(report.shared_with_team);+ format!(+ "; {} of them shared with the user's team, {personal} visible only to the user",+ report.shared_with_team+ )+ };+ format!(+ "of the {} pages and {} databases Notion shared, it imports {writes}{shared}; {} skipped, {} to handle by hand",+ report.pages, report.data_sources, report.skip, report.hand+ )+}++/// What teammates already imported, by whom: these link to the teammates'+/// copies and are not imported again.+pub(super) fn already_in_macro(report: &NotionImportReport) -> String {+ if report.already_in_macro == 0 {+ return String::new();+ }+ let by = report+ .already_imported_by+ .iter()+ .map(|importer| format!("{} ({})", importer.by, importer.count))+ .collect::<Vec<_>>()+ .join(", ");+ format!(+ " {} more are already in Macro, imported and shared with the team by {by}; they link to those copies and are not imported again.",+ report.already_in_macro+ )+}++fn waiting_on_teammates(report: &NotionImportReport) -> String {+ if report.waiting_on_teammates == 0 {+ return String::new();+ }+ format!(+ " {} are being imported by a teammate at the same time and link to their copies once those are written.",+ report.waiting_on_teammates+ )+}diff --git a/crates/import/src/inbound/toolset/import_from_notion/test.rs b/crates/import/src/inbound/toolset/import_from_notion/test.rsnew file mode 100644index 0000000000..e387ecb256--- /dev/null+++ b/crates/import/src/inbound/toolset/import_from_notion/test.rs@@ -0,0 +1,142 @@+use super::*;++#[test]+fn a_ready_plan_tells_the_agent_to_present_it_for_review() {+ let report = NotionImportReport {+ status: Some(RunStatus::Ready),+ pages: 40,+ data_sources: 3,+ copy: 38,+ tasks: 1,+ skip: 3,+ hand: 1,+ documents: 36,+ databases: 2,+ database_rows: 412,+ task_rows: 33,+ shared_with_team: 30,+ on_team: true,+ hand_items: vec![crate::domain::models::NotionReportItem {+ import_id: uuid::Uuid::nil(),+ title: "CRM".to_string(),+ kind: crate::domain::models::ImportKind::DataSource,+ url: None,+ reason: Some("Mixed records".to_string()),+ }],+ ..NotionImportReport::default()+ };++ assert_eq!(+ message(NotionImportAction::Plan, &report),+ "The plan is ready: of the 40 pages and 3 databases Notion shared, it imports 36 pages as documents, 2 databases with 412 rows, 1 task lists as 33 tasks; 30 of them shared with the user's team, 9 visible only to the user; 3 skipped, 1 to handle by hand. Tell the user these numbers as they are, then present the plan now with PresentReview (kind \"import\", a short title such as \"Import from Notion\", drafts [{\"source\":\"notion\"}]). Nothing is written until they approve it; they can change who sees each object on the card. End your message by asking \"Should I kick it off?\"; if they say yes, approve it with DecideReview (a submit decision). Objects to handle by hand (with your own tools, after the import): \"CRM\" (Mixed records)."+ );+}++#[test]+fn roots_parse_from_notion_urls_and_an_empty_list_means_everything() {+ assert_eq!(scope(&[]).unwrap(), ImportScope::Everything);+ assert_eq!(scope(&[" ".to_string()]).unwrap(), ImportScope::Everything);+ assert_eq!(+ scope(&["https://www.notion.so/Q3-plan-3fb607323e2e80629886fc0c028d50a7".to_string()])+ .unwrap(),+ ImportScope::Roots {+ ids: vec![NotionId::parse("3fb607323e2e80629886fc0c028d50a7").unwrap()],+ }+ );+ assert!(scope(&["not a page".to_string()]).is_err());+}++#[test]+fn while_notion_is_read_the_agent_presents_the_plan_at_once_with_what_is_found_so_far() {+ let report = NotionImportReport {+ status: Some(RunStatus::Running),+ on_team: true,+ scanned: Some(crate::domain::models::ScanCounts {+ pages: 40,+ data_sources: 12,+ }),+ ..NotionImportReport::default()+ };++ assert_eq!(+ message(NotionImportAction::Plan, &report),+ "Planning has started and goes on in the background (40 pages and 12 databases found in Notion so far). Present the plan now with PresentReview (kind \"import\", a short title such as \"Import from Notion\", drafts [{\"source\":\"notion\"}]) and tell the user in a sentence that the review fills in as Notion is read and each object is decided, and that they approve it there once it is ready. Do not wait for planning or check `status` in a loop. End your message by asking \"Should I kick it off once it's ready?\". If they say yes, approve it with DecideReview (a submit decision); while the plan is still being made it refuses, so tell them it is not ready yet and that they can say so again, or approve it on the card, once it is."+ );+}++#[test]+fn a_presented_plan_still_being_decided_says_how_far_it_got_and_not_to_wait() {+ let report = NotionImportReport {+ status: Some(RunStatus::Running),+ on_team: true,+ review_id: Some(uuid::Uuid::nil()),+ pages: 60,+ data_sources: 24,+ deciding: 44,+ ..NotionImportReport::default()+ };++ assert_eq!(+ message(NotionImportAction::Status, &report),+ "The plan is presented and still being made (40 of 84 objects decided so far). The review fills in as planning goes, and the user approves it there once it is ready. Do not wait for planning or check `status` in a loop."+ );+}++#[test]+fn without_a_team_the_agent_hears_the_import_is_personal_and_offers_no_team_sharing() {+ let report = NotionImportReport {+ status: Some(RunStatus::Ready),+ pages: 2,+ copy: 2,+ documents: 2,+ ..NotionImportReport::default()+ };++ assert_eq!(+ message(NotionImportAction::Plan, &report),+ "The plan is ready: of the 2 pages and 0 databases Notion shared, it imports 2 pages as documents; all of it visible only to the user; 0 skipped, 0 to handle by hand. The user is not on a team, so everything this import writes is visible only to them; do not offer to share it with a team. Tell the user these numbers as they are, then present the plan now with PresentReview (kind \"import\", a short title such as \"Import from Notion\", drafts [{\"source\":\"notion\"}]). Nothing is written until they approve it. End your message by asking \"Should I kick it off?\"; if they say yes, approve it with DecideReview (a submit decision)."+ );+}++#[test]+fn the_agent_hears_how_much_is_already_in_macro_and_by_whom() {+ let report = NotionImportReport {+ status: Some(RunStatus::Ready),+ on_team: true,+ pages: 12,+ copy: 2,+ documents: 2,+ already_in_macro: 10,+ already_imported_by: vec![+ crate::domain::models::NotionImporter {+ by: "[email protected]".to_string(),+ count: 7,+ },+ crate::domain::models::NotionImporter {+ by: "[email protected]".to_string(),+ count: 3,+ },+ ],+ ..NotionImportReport::default()+ };++ assert_eq!(+ message(NotionImportAction::Plan, &report),+ "The plan is ready: of the 12 pages and 0 databases Notion shared, it imports 2 pages as documents; 0 of them shared with the user's team, 2 visible only to the user; 0 skipped, 0 to handle by hand. 10 more are already in Macro, imported and shared with the team by [email protected] (7), [email protected] (3); they link to those copies and are not imported again. Tell the user these numbers as they are, then present the plan now with PresentReview (kind \"import\", a short title such as \"Import from Notion\", drafts [{\"source\":\"notion\"}]). Nothing is written until they approve it; they can change who sees each object on the card. End your message by asking \"Should I kick it off?\"; if they say yes, approve it with DecideReview (a submit decision)."+ );+}++#[test]+fn objects_waiting_on_a_teammate_are_explained_while_and_after_importing() {+ let report = NotionImportReport {+ status: Some(RunStatus::Completed),+ imported: 4,+ waiting_on_teammates: 2,+ ..NotionImportReport::default()+ };++ assert_eq!(+ message(NotionImportAction::Status, &report),+ "The import finished: 4 objects imported. 2 are being imported by a teammate at the same time and link to their copies once those are written."+ );+}diff --git a/crates/import/src/inbound/toolset/test.rs b/crates/import/src/inbound/toolset/test.rsindex 743885fc39..8b21c9c297 100644--- a/crates/import/src/inbound/toolset/test.rs+++ b/crates/import/src/inbound/toolset/test.rs@@ -15,14 +15,18 @@ fn row(source: ImportSource, status: ImportStatus) -> ImportEntity {user_id: user().as_ref().to_string(),team_id: None,source,+ kind: source.default_kind(),foreign_id: "ENG-1".into(),+ parent_id: None,+ review_id: None,status,- initiator: Initiator::Onboarding,+ initiator: Initiator::Chat,metadata: serde_json::json!({"title": "T"}),entity_id: matches!(status, ImportStatus::Imported).then(|| "doc-1".to_string()),entity_type: matches!(status, ImportStatus::Imported).then(|| source.entity_type().to_string()),last_error: None,+ waiting_on: None,created_at: chrono::Utc::now(),updated_at: chrono::Utc::now(),}@@ -103,56 +107,20 @@ impl NotionPageImporter for MockStager {}}-fn contexts(- policy: ToolPolicy,-) -> (+fn contexts() -> (ServiceContext<ImportToolContext<MockStager>>,RequestContext,Arc<MockStager>,) {let stager = Arc::new(MockStager::default());- let context = ImportToolContext {- service: Some(stager.clone()),- policy,- };+ let context = ImportToolContext::wired(stager.clone());(ServiceContext(context), RequestContext::new(user()), stager)}#[tokio::test]-async fn gather_policy_locks_source_and_initiator() {- let (service_context, request_context, stager) = contexts(ToolPolicy::gather(- ImportSource::Linear,- Initiator::Onboarding,- ));-- // Wrong source is refused before touching the service.- let err = CreateImportEntity {- source: ImportSource::Notion,- foreign_id: "https://notion.so/x".into(),- status: CreateImportStatus::Staged,- metadata: serde_json::json!({"title": "T"}),- entity_id: None,- }- .call(service_context.clone(), request_context.clone())- .await- .expect_err("forced source must reject other sources");- assert!(err.description.contains("only stages linear"), "{err:?}");- assert!(stager.staged_calls.lock().unwrap().is_empty());-- // status=imported is refused under a staging-only policy.- let err = CreateImportEntity {- source: ImportSource::Linear,- foreign_id: "ENG-1".into(),- status: CreateImportStatus::Imported,- metadata: serde_json::json!({"title": "T"}),- entity_id: Some("doc-1".into()),- }- .call(service_context.clone(), request_context.clone())- .await- .expect_err("staging-only policy must reject imported writes");- assert!(err.description.contains("only stage"), "{err:?}");+async fn staging_records_the_chat_initiator() {+ let (service_context, request_context, stager) = contexts();- // The right source stages with the locked initiator.let response = CreateImportEntity {source: ImportSource::Linear,foreign_id: "ENG-1".into(),@@ -163,38 +131,17 @@ async fn gather_policy_locks_source_and_initiator() {.call(service_context, request_context).await.expect("staging succeeds");+assert_eq!(response.outcome, "staged");assert_eq!(stager.staged_calls.lock().unwrap().as_slice(),- &[(- Initiator::Onboarding,- ImportSource::Linear,- "ENG-1".to_string()- )]+ &[(Initiator::Chat, ImportSource::Linear, "ENG-1".to_string())]);}#[tokio::test]-async fn manual_gather_policy_records_manual_initiator() {- let (service_context, request_context, stager) =- contexts(ToolPolicy::gather(ImportSource::Notion, Initiator::Manual));-- CreateImportEntity {- source: ImportSource::Notion,- foreign_id: "https://notion.so/Plan-0123456789abcdef0123456789abcdef".into(),- status: CreateImportStatus::Staged,- metadata: serde_json::json!({"title": "Plan"}),- entity_id: None,- }- .call(service_context, request_context)- .await- .expect("staging succeeds");- assert_eq!(stager.staged_calls.lock().unwrap()[0].0, Initiator::Manual);-}--#[tokio::test]-async fn chat_policy_requires_entity_id_for_imported_writes() {- let (service_context, request_context, _) = contexts(ToolPolicy::chat());+async fn imported_writes_require_an_entity_id() {+ let (service_context, request_context, _) = contexts();let err = CreateImportEntity {source: ImportSource::Linear,foreign_id: "ENG-1".into(),@@ -221,10 +168,7 @@ async fn teammate_dedup_response_tells_the_agent_not_to_duplicate() {}),..Default::default()});- let context = ImportToolContext {- service: Some(stager),- policy: ToolPolicy::chat(),- };+ let context = ImportToolContext::wired(stager);let response = CreateImportEntity {source: ImportSource::Linear,foreign_id: "ENG-1".into(),@@ -242,7 +186,7 @@ async fn teammate_dedup_response_tells_the_agent_not_to_duplicate() {#[tokio::test]async fn import_notion_page_runs_the_workflow_and_returns_the_document() {- let (service_context, request_context, stager) = contexts(ToolPolicy::chat());+ let (service_context, request_context, stager) = contexts();let page_url = "https://app.notion.com/p/4d005e63b2fd4d079df4e376e35b7519";let response = ImportNotionPage {@@ -263,7 +207,7 @@ async fn import_notion_page_runs_the_workflow_and_returns_the_document() {#[tokio::test]async fn import_notion_page_requires_a_page_url() {- let (service_context, request_context, stager) = contexts(ToolPolicy::chat());+ let (service_context, request_context, stager) = contexts();let error = ImportNotionPage {page_url: " ".to_string(),@@ -278,7 +222,7 @@ async fn import_notion_page_requires_a_page_url() {#[tokio::test]async fn delete_reports_non_discardable_status() {- let (service_context, request_context, _) = contexts(ToolPolicy::chat());+ let (service_context, request_context, _) = contexts();let err = DeleteImportEntity { id: Uuid::nil() }.call(service_context, request_context).awaitdiff --git a/crates/import/src/lib.rs b/crates/import/src/lib.rsindex 48850ebd8b..7402e37c26 100644--- a/crates/import/src/lib.rs+++ b/crates/import/src/lib.rs@@ -7,11 +7,13 @@//! GitHub PR). An [`domain::models::ImportEntity`] is a ledger row for an//! item being COPIED into Macro — it tracks the item from `staged`//! (discovered, not yet accepted) through `importing` to `imported` (a Macro-//! entity now exists), or `discarded` (the user declined it).+//! entity now exists), or `discarded` (the user declined it). Each row also+//! records what the item is in its source (`kind`), the row it was imported+//! under (`parent_id`), and the review that approved it (`review_id`).//!-//! The crate is host-agnostic: onboarding (`/setup`) and AI chat both drive-//! the same primitives, distinguished only by the `initiator` recorded on-//! each row.+//! The crate is host-agnostic: AI chat, Settings (Slack channel discovery),+//! and the Slack archive import drive the same primitives, distinguished+//! only by the `initiator` recorded on each row.#![deny(missing_docs)]diff --git a/crates/import/src/outbound/mcp_slack_source.rs b/crates/import/src/outbound/mcp_slack_source.rsindex 8e61a9ea3e..25b5eabe07 100644--- a/crates/import/src/outbound/mcp_slack_source.rs+++ b/crates/import/src/outbound/mcp_slack_source.rs@@ -58,7 +58,7 @@ impl<S: ConnectorSelect> SlackWorkspaceSource for McpSlackSource<S> {}}-/// Connector tools and schemas loaded once for a gather or import batch.+/// Connector tools and schemas loaded once for a discovery or import batch.pub struct McpSlackSession {tools: Arc<UserMcpTools>,user: MacroUserIdStr<'static>,diff --git a/crates/import/src/outbound/pg_import_repo.rs b/crates/import/src/outbound/pg_import_repo.rsindex 8a3497470d..d3bcfd4309 100644--- a/crates/import/src/outbound/pg_import_repo.rs+++ b/crates/import/src/outbound/pg_import_repo.rs@@ -5,17 +5,22 @@//! reporting whether it happened via `RETURNING` / rows-affected.use crate::domain::models::{- ImportEntity, ImportRun, ImportSource, ImportStatus, Initiator, RunStatus,+ ImportEntity, ImportKind, ImportRun, ImportSource, ImportStatus, Initiator, RunStatus,+ ScanCounts, TEAMMATE_STOPPED,};use crate::domain::ports::{ImportError, ImportRepo, Result};+use agent_session::domain::notifications::NotificationWriter;use chrono::{DateTime, Utc};use macro_user_id::cowlike::CowLike;use macro_user_id::user_id::MacroUserIdStr;-use sqlx::PgPool;+use sqlx::{PgConnection, PgPool};use std::collections::HashSet;use std::str::FromStr;+use std::sync::Arc;use uuid::Uuid;+mod notion_run;+mod plan;/// Canonical target transaction helpers for composition roots.pub mod targets;@@ -26,12 +31,27 @@ mod test;#[derive(Clone)]pub struct PgImportRepo {pool: PgPool,+ notifications: Option<Arc<dyn NotificationWriter<PgConnection>>>,}impl PgImportRepo {/// Build the repository on the MacroDB pool.pub fn new(pool: PgPool) -> Self {- Self { pool }+ Self {+ pool,+ notifications: None,+ }+ }++ /// Produce agent-session system events through `notifications`, in the+ /// transactions that move a reviewed Notion run. Hosts that run Notion+ /// workspace imports set it; without it, sessions are not told.+ pub fn with_session_notifications(+ mut self,+ notifications: Arc<dyn NotificationWriter<PgConnection>>,+ ) -> Self {+ self.notifications = Some(notifications);+ self}}@@ -40,13 +60,17 @@ struct ImportEntityDbRow {user_id: String,team_id: Option<Uuid>,source: String,+ kind: Option<String>,foreign_id: String,+ parent_id: Option<Uuid>,+ review_id: Option<Uuid>,status: String,initiator: String,metadata: serde_json::Value,entity_id: Option<String>,entity_type: Option<String>,last_error: Option<String>,+ waiting_on: Option<Uuid>,created_at: DateTime<Utc>,updated_at: DateTime<Utc>,}@@ -61,18 +85,30 @@ impl TryFrom<ImportEntityDbRow> for ImportEntity {.map_err(|_| anyhow::anyhow!("unknown import status: {}", row.status))?;let initiator = Initiator::from_str(&row.initiator).map_err(|_| anyhow::anyhow!("unknown import initiator: {}", row.initiator))?;+ // Rows written before `kind` existed (or by a replica still running+ // older code during a deploy) hold NULL: they are always the source's+ // original kind.+ let kind = match row.kind {+ Some(kind) => ImportKind::from_str(&kind)+ .map_err(|_| anyhow::anyhow!("unknown import kind: {kind}"))?,+ None => source.default_kind(),+ };Ok(ImportEntity {id: row.id,user_id: row.user_id,team_id: row.team_id,source,+ kind,foreign_id: row.foreign_id,+ parent_id: row.parent_id,+ review_id: row.review_id,status,initiator,metadata: row.metadata,entity_id: row.entity_id,entity_type: row.entity_type,last_error: row.last_error,+ waiting_on: row.waiting_on,created_at: row.created_at,updated_at: row.updated_at,})@@ -82,9 +118,11 @@ impl TryFrom<ImportEntityDbRow> for ImportEntity {struct ImportRunDbRow {source: String,status: String,- auto_import: bool,error: Option<String>,updated_at: DateTime<Utc>,+ scanned_pages: Option<i32>,+ scanned_data_sources: Option<i32>,+ review_id: Option<Uuid>,}impl TryFrom<ImportRunDbRow> for ImportRun {@@ -95,12 +133,20 @@ impl TryFrom<ImportRunDbRow> for ImportRun {.map_err(|_| anyhow::anyhow!("unknown import source: {}", row.source))?;let status = RunStatus::from_str(&row.status).map_err(|_| anyhow::anyhow!("unknown run status: {}", row.status))?;+ let scanned = match (row.scanned_pages, row.scanned_data_sources) {+ (Some(pages), Some(data_sources)) => Some(ScanCounts {+ pages: u32::try_from(pages).map_err(anyhow::Error::from)?,+ data_sources: u32::try_from(data_sources).map_err(anyhow::Error::from)?,+ }),+ _ => None,+ };Ok(ImportRun {source,status,- auto_import: row.auto_import,error: row.error,updated_at: row.updated_at,+ scanned,+ review_id: row.review_id,})}}@@ -109,6 +155,27 @@ fn run_status_strings(statuses: &[RunStatus]) -> Vec<String> {statuses.iter().map(|s| s.as_ref().to_string()).collect()}+/// Send the rows waiting on `holders`, which stopped being written, back to+/// staged. Run after the update that moved the holders, as its own+/// statement, so it sees waiters that committed meanwhile.+async fn release_waiters(+ transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,+ holders: &[Uuid],+) -> Result<u64> {+ let result = sqlx::query!(+ r#"+ UPDATE import_entity+ SET status = 'staged', waiting_on = NULL, last_error = $2, updated_at = NOW()+ WHERE waiting_on = ANY($1::uuid[]) AND status = 'importing'+ "#,+ holders,+ TEAMMATE_STOPPED,+ )+ .execute(&mut **transaction)+ .await?;+ Ok(result.rows_affected())+}+impl ImportRepo for PgImportRepo {#[tracing::instrument(skip(self), err)]async fn get_own_by_foreign_id(@@ -120,8 +187,8 @@ impl ImportRepo for PgImportRepo {let row = sqlx::query_as!(ImportEntityDbRow,r#"- SELECT id, user_id, team_id, source, foreign_id, status, initiator,- metadata, entity_id, entity_type, last_error, created_at, updated_at+ SELECT id, user_id, team_id, source, kind, foreign_id, parent_id, review_id, status,+ initiator, metadata, entity_id, entity_type, last_error, waiting_on, created_at, updated_atFROM import_entityWHERE user_id = $1 AND source = $2 AND foreign_id = $3"#,@@ -144,8 +211,8 @@ impl ImportRepo for PgImportRepo {let row = sqlx::query_as!(ImportEntityDbRow,r#"- SELECT id, user_id, team_id, source, foreign_id, status, initiator,- metadata, entity_id, entity_type, last_error, created_at, updated_at+ SELECT id, user_id, team_id, source, kind, foreign_id, parent_id, review_id, status,+ initiator, metadata, entity_id, entity_type, last_error, waiting_on, created_at, updated_atFROM import_entityWHERE source = $2 AND foreign_id = $3AND status = 'imported'@@ -168,6 +235,7 @@ impl ImportRepo for PgImportRepo {&self,user: &MacroUserIdStr<'static>,source: ImportSource,+ kind: ImportKind,initiator: Initiator,foreign_id: &str,metadata: &serde_json::Value,@@ -175,19 +243,21 @@ impl ImportRepo for PgImportRepo {let row = sqlx::query_as!(ImportEntityDbRow,r#"- INSERT INTO import_entity (user_id, source, foreign_id, status, initiator, metadata)- VALUES ($1, $2, $3, 'staged', $4, $5)+ INSERT INTO import_entity (user_id, source, kind, foreign_id, status, initiator, metadata)+ VALUES ($1, $2, $6, $3, 'staged', $4, $5)ON CONFLICT (user_id, source, foreign_id) DO UPDATESET metadata = EXCLUDED.metadata, updated_at = NOW()WHERE import_entity.status = 'staged'- RETURNING id, user_id, team_id, source, foreign_id, status, initiator,- metadata, entity_id, entity_type, last_error, created_at, updated_at+ RETURNING id, user_id, team_id, source, kind, foreign_id, parent_id, review_id, status,+ initiator, metadata, entity_id, entity_type, last_error, waiting_on, created_at,+ updated_at"#,user.as_ref(),source.as_ref(),foreign_id,initiator.as_ref(),metadata,+ kind.as_ref(),).fetch_optional(&self.pool).await?;@@ -199,6 +269,7 @@ impl ImportRepo for PgImportRepo {&self,user: &MacroUserIdStr<'static>,source: ImportSource,+ kind: ImportKind,initiator: Initiator,foreign_id: &str,metadata: &serde_json::Value,@@ -209,16 +280,17 @@ impl ImportRepo for PgImportRepo {let row = sqlx::query_as!(ImportEntityDbRow,r#"- INSERT INTO import_entity (user_id, source, foreign_id, status, initiator, metadata,- entity_id, entity_type, team_id)- VALUES ($1, $2, $3, 'imported', $4, $5, $6, $7, $8)+ INSERT INTO import_entity (user_id, source, kind, foreign_id, status, initiator,+ metadata, entity_id, entity_type, team_id)+ VALUES ($1, $2, $9, $3, 'imported', $4, $5, $6, $7, $8)ON CONFLICT (user_id, source, foreign_id) DO UPDATESET status = 'imported', metadata = EXCLUDED.metadata,entity_id = EXCLUDED.entity_id, entity_type = EXCLUDED.entity_type,team_id = EXCLUDED.team_id, last_error = NULL, updated_at = NOW()WHERE import_entity.status <> 'imported'- RETURNING id, user_id, team_id, source, foreign_id, status, initiator,- metadata, entity_id, entity_type, last_error, created_at, updated_at+ RETURNING id, user_id, team_id, source, kind, foreign_id, parent_id, review_id, status,+ initiator, metadata, entity_id, entity_type, last_error, waiting_on, created_at,+ updated_at"#,user.as_ref(),source.as_ref(),@@ -228,6 +300,7 @@ impl ImportRepo for PgImportRepo {entity_id,entity_type,team_id,+ kind.as_ref(),).fetch_optional(&self.pool).await?;@@ -246,8 +319,8 @@ impl ImportRepo for PgImportRepo {let row = sqlx::query_as!(ImportEntityDbRow,r#"- SELECT id, user_id, team_id, source, foreign_id, status, initiator,- metadata, entity_id, entity_type, last_error, created_at, updated_at+ SELECT id, user_id, team_id, source, kind, foreign_id, parent_id, review_id, status,+ initiator, metadata, entity_id, entity_type, last_error, waiting_on, created_at, updated_atFROM import_entityWHERE user_id = $1 AND id = $2"#,@@ -269,8 +342,8 @@ impl ImportRepo for PgImportRepo {let rows = sqlx::query_as!(ImportEntityDbRow,r#"- SELECT id, user_id, team_id, source, foreign_id, status, initiator,- metadata, entity_id, entity_type, last_error, created_at, updated_at+ SELECT id, user_id, team_id, source, kind, foreign_id, parent_id, review_id, status,+ initiator, metadata, entity_id, entity_type, last_error, waiting_on, created_at, updated_atFROM import_entityWHERE (user_id = $1@@ -333,8 +406,9 @@ impl ImportRepo for PgImportRepo {UPDATE import_entitySET status = 'importing', last_error = NULL, updated_at = NOW()WHERE user_id = $1 AND id = ANY($2::uuid[]) AND status = 'staged'- RETURNING id, user_id, team_id, source, foreign_id, status, initiator,- metadata, entity_id, entity_type, last_error, created_at, updated_at+ RETURNING id, user_id, team_id, source, kind, foreign_id, parent_id, review_id, status,+ initiator, metadata, entity_id, entity_type, last_error, waiting_on, created_at,+ updated_at"#,user.as_ref(),ids,@@ -353,15 +427,18 @@ impl ImportRepo for PgImportRepo {entity_type: &str,team_id: Option<Uuid>,) -> Result<Option<ImportEntity>> {+ let mut transaction = self.pool.begin().await?;let row = sqlx::query_as!(ImportEntityDbRow,r#"UPDATE import_entity- SET status = 'imported', entity_id = $3, entity_type = $4, team_id = $5,+ SET status = 'imported', entity_id = $3, entity_type = $4,+ team_id = COALESCE($5, team_id), waiting_on = NULL,last_error = NULL, updated_at = NOW()WHERE user_id = $1 AND id = $2 AND status = 'importing'- RETURNING id, user_id, team_id, source, foreign_id, status, initiator,- metadata, entity_id, entity_type, last_error, created_at, updated_at+ RETURNING id, user_id, team_id, source, kind, foreign_id, parent_id, review_id, status,+ initiator, metadata, entity_id, entity_type, last_error, waiting_on, created_at,+ updated_at"#,user.as_ref(),id,@@ -369,8 +446,26 @@ impl ImportRepo for PgImportRepo {entity_type,team_id,)- .fetch_optional(&self.pool)+ .fetch_optional(&mut *transaction).await?;+ if row.is_some() {+ // Its own statement, so it sees waiters that committed while the+ // update above waited for their lock on this row.+ sqlx::query!(+ r#"+ UPDATE import_entity+ SET status = 'imported', entity_id = $2, entity_type = $3, waiting_on = NULL,+ last_error = NULL, updated_at = NOW()+ WHERE waiting_on = $1 AND status = 'importing'+ "#,+ id,+ entity_id,+ entity_type,+ )+ .execute(&mut *transaction)+ .await?;+ }+ transaction.commit().await?;row.map(ImportEntity::try_from).transpose()}@@ -381,19 +476,26 @@ impl ImportRepo for PgImportRepo {id: Uuid,error: &str,) -> Result<bool> {+ let mut transaction = self.pool.begin().await?;let result = sqlx::query!(r#"UPDATE import_entity- SET status = 'staged', last_error = $3, updated_at = NOW()+ SET status = 'staged', last_error = $3, team_id = NULL, waiting_on = NULL,+ updated_at = NOW()WHERE user_id = $1 AND id = $2 AND status = 'importing'"#,user.as_ref(),id,error,)- .execute(&self.pool)+ .execute(&mut *transaction).await?;- Ok(result.rows_affected() > 0)+ let moved = result.rows_affected() > 0;+ if moved {+ release_waiters(&mut transaction, &[id]).await?;+ }+ transaction.commit().await?;+ Ok(moved)}#[tracing::instrument(skip(self), err)]@@ -409,6 +511,7 @@ impl ImportRepo for PgImportRepo {last_error = 'the import was interrupted — select it again to retry',updated_at = NOW()WHERE user_id = $1 AND status = 'importing'+ AND NOT (source = 'notion' AND metadata->'plan' IS NOT NULL)AND updated_at < NOW() - ($2::double precision * interval '1 second')"#,user.as_ref(),@@ -451,56 +554,6 @@ impl ImportRepo for PgImportRepo {Ok(result.rows_affected() > 0)}- #[tracing::instrument(skip(self), err)]- async fn discard_staged_by_initiator(- &self,- user: &MacroUserIdStr<'static>,- initiator: Initiator,- ) -> Result<u64> {- let result = sqlx::query!(- r#"- UPDATE import_entity- SET status = 'discarded', updated_at = NOW()- WHERE user_id = $1 AND initiator = $2 AND status = 'staged'- "#,- user.as_ref(),- initiator.as_ref(),- )- .execute(&self.pool)- .await?;- Ok(result.rows_affected())- }-- #[tracing::instrument(skip(self), err)]- async fn delete_staged_by_initiator(- &self,- user: &MacroUserIdStr<'static>,- initiator: Initiator,- ) -> Result<u64> {- let result = sqlx::query!(- r#"- DELETE FROM import_entity- WHERE user_id = $1 AND initiator = $2 AND status = 'staged'- AND NOT (- $2 = 'onboarding'- AND EXISTS (- SELECT 1- FROM import_run- WHERE import_run.user_id = import_entity.user_id- AND import_run.source = import_entity.source- AND import_run.auto_import- AND import_run.status IN ('running', 'ready', 'importing', 'failed')- )- )- "#,- user.as_ref(),- initiator.as_ref(),- )- .execute(&self.pool)- .await?;- Ok(result.rows_affected())- }-#[tracing::instrument(skip(self), err)]async fn user_team_id(&self, user: &MacroUserIdStr<'static>) -> Result<Option<Uuid>> {let team_id = sqlx::query_scalar!(@@ -534,7 +587,11 @@ impl ImportRepo for PgImportRepo {let rows = sqlx::query_as!(ImportRunDbRow,r#"- SELECT source, status::text AS "status!", auto_import, error, updated_at+ SELECT source, status::text AS "status!", error, updated_at,+ CASE WHEN status = 'running' THEN scanned_pages END AS scanned_pages,+ CASE WHEN status = 'running' THEN scanned_data_sources END+ AS scanned_data_sources,+ review_idFROM import_run WHERE user_id = $1ORDER BY source"#,@@ -551,21 +608,22 @@ impl ImportRepo for PgImportRepo {user: &MacroUserIdStr<'static>,source: ImportSource,from: &[RunStatus],- auto_import: bool,) -> Result<bool> {let from = run_status_strings(from);let result = sqlx::query!(r#"- INSERT INTO import_run (user_id, source, status, auto_import)- VALUES ($1, $2, 'running', $4)+ INSERT INTO import_run (user_id, source, status)+ VALUES ($1, $2, 'running')ON CONFLICT (user_id, source) DO UPDATE- SET status = 'running', error = NULL, updated_at = NOW()+ SET status = 'running', error = NULL, updated_at = NOW(),+ scanned_pages = NULL, scanned_data_sources = NULL,+ lease_owner = NULL, lease_expires_at = NULL, adoptions = 0,+ review_session_id = NULLWHERE import_run.status::text = ANY($3::text[])"#,user.as_ref(),source.as_ref(),&from,- auto_import,).execute(&self.pool).await?;@@ -573,28 +631,26 @@ impl ImportRepo for PgImportRepo {}#[tracing::instrument(skip(self), err)]- async fn start_manual_run(+ async fn fail_stale_runs(&self,user: &MacroUserIdStr<'static>,- source: ImportSource,- from: &[RunStatus],- ) -> Result<bool> {- let from = run_status_strings(from);+ older_than_secs: i64,+ ) -> Result<u64> {let result = sqlx::query!(r#"- INSERT INTO import_run (user_id, source, status, auto_import)- VALUES ($1, $2, 'running', false)- ON CONFLICT (user_id, source) DO UPDATE- SET status = 'running', auto_import = false, error = NULL, updated_at = NOW()- WHERE import_run.status::text = ANY($3::text[])+ UPDATE import_run+ SET status = 'failed',+ error = 'discovery was interrupted — try again',+ updated_at = NOW()+ WHERE user_id = $1 AND status IN ('running', 'importing') AND source <> 'notion'+ AND updated_at < NOW() - ($2::double precision * interval '1 second')"#,user.as_ref(),- source.as_ref(),- &from,+ older_than_secs as f64,).execute(&self.pool).await?;- Ok(result.rows_affected() > 0)+ Ok(result.rows_affected())}#[tracing::instrument(skip(self), err)]@@ -622,173 +678,6 @@ impl ImportRepo for PgImportRepo {.await?;Ok(result.rows_affected() > 0)}-- #[tracing::instrument(skip(self), err)]- async fn transition_run(- &self,- user: &MacroUserIdStr<'static>,- source: ImportSource,- from: &[RunStatus],- to: RunStatus,- ) -> Result<bool> {- let from = run_status_strings(from);- let result = sqlx::query!(- r#"- UPDATE import_run- SET status = $4::text::import_run_status, updated_at = NOW()- WHERE user_id = $1 AND source = $2- AND status::text = ANY($3::text[])- "#,- user.as_ref(),- source.as_ref(),- &from,- to.as_ref(),- )- .execute(&self.pool)- .await?;- Ok(result.rows_affected() > 0)- }-- #[tracing::instrument(skip(self), err)]- async fn begin_auto_import(- &self,- user: &MacroUserIdStr<'static>,- source: ImportSource,- ) -> Result<Option<Vec<ImportEntity>>> {- let mut transaction = self.pool.begin().await?;- let claimed = sqlx::query_scalar!(- r#"- UPDATE import_run- SET status = 'importing', error = NULL, updated_at = NOW()- WHERE user_id = $1 AND source = $2- AND status = 'ready' AND auto_import- RETURNING source- "#,- user.as_ref(),- source.as_ref(),- )- .fetch_optional(&mut *transaction)- .await?;- if claimed.is_none() {- return Ok(None);- }-- let rows = sqlx::query_as!(- ImportEntityDbRow,- r#"- UPDATE import_entity- SET status = 'importing', last_error = NULL, updated_at = NOW()- WHERE user_id = $1 AND source = $2- AND initiator = 'onboarding' AND status = 'staged'- RETURNING id, user_id, team_id, source, foreign_id, status, initiator,- metadata, entity_id, entity_type, last_error, created_at, updated_at- "#,- user.as_ref(),- source.as_ref(),- )- .fetch_all(&mut *transaction)- .await?;- transaction.commit().await?;- Ok(Some(- rows.into_iter()- .map(ImportEntity::try_from)- .collect::<Result<Vec<_>>>()?,- ))- }-- #[tracing::instrument(skip(self, ids), fields(count = ids.len()), err)]- async fn finish_auto_import(- &self,- user: &MacroUserIdStr<'static>,- source: ImportSource,- ids: &[Uuid],- ) -> Result<Option<RunStatus>> {- let status = sqlx::query_scalar!(- r#"- WITH outcome AS (- SELECT- COUNT(*) FILTER (WHERE status = 'imported')- = CARDINALITY($3::uuid[])::bigint AS succeeded- FROM import_entity- WHERE user_id = $1 AND id = ANY($3::uuid[])- )- UPDATE import_run- SET status = (CASE- WHEN outcome.succeeded THEN 'completed'- ELSE 'failed'- END)::import_run_status,- error = CASE- WHEN outcome.succeeded THEN NULL- ELSE 'one or more automatic imports failed'- END,- updated_at = NOW()- FROM outcome- WHERE user_id = $1 AND source = $2 AND status = 'importing'- RETURNING import_run.status::text AS "status!"- "#,- user.as_ref(),- source.as_ref(),- ids,- )- .fetch_optional(&self.pool)- .await?;- status- .map(|status| {- RunStatus::from_str(&status)- .map_err(|_| anyhow::anyhow!("unknown run status: {status}").into())- })- .transpose()- }-- #[tracing::instrument(skip(self), err)]- async fn reconcile_auto_import_runs(&self, user: &MacroUserIdStr<'static>) -> Result<u64> {- let result = sqlx::query!(- r#"- WITH settled AS (- SELECT- run.source,- EXISTS (- SELECT 1- FROM import_entity entity- WHERE entity.user_id = run.user_id- AND entity.source = run.source- AND entity.initiator = 'onboarding'- AND entity.status = 'staged'- AND entity.last_error IS NOT NULL- ) AS failed- FROM import_run run- WHERE run.user_id = $1- AND run.status = 'importing'- AND run.auto_import- AND NOT EXISTS (- SELECT 1- FROM import_entity entity- WHERE entity.user_id = run.user_id- AND entity.source = run.source- AND entity.initiator = 'onboarding'- AND entity.status = 'importing'- )- )- UPDATE import_run run- SET status = (- CASE WHEN settled.failed THEN 'failed' ELSE 'completed' END- )::import_run_status,- error = CASE- WHEN settled.failed THEN 'one or more automatic imports failed'- ELSE NULL- END,- updated_at = NOW()- FROM settled- WHERE run.user_id = $1- AND run.source = settled.source- AND run.status = 'importing'- "#,- user.as_ref(),- )- .execute(&self.pool)- .await?;- Ok(result.rows_affected())- }}// The domain error stays sqlx-free; this adapter owns the mapping so `?`diff --git a/crates/import/src/outbound/pg_import_repo/notion_run.rs b/crates/import/src/outbound/pg_import_repo/notion_run.rsnew file mode 100644index 0000000000..26580ea604--- /dev/null+++ b/crates/import/src/outbound/pg_import_repo/notion_run.rs@@ -0,0 +1,384 @@+//! Postgres side of resumable Notion workspace imports: the lease on the+//! user's run, and what a write already created.++use std::str::FromStr;+use std::time::Duration;++use agent_session::domain::model::AgentSessionId;+use agent_session::domain::notifications::PendingNotification;+use import_notion::domain::model::ImportScope;+use macro_user_id::cowlike::CowLike;+use macro_user_id::user_id::MacroUserIdStr;+use uuid::Uuid;++use super::{PgImportRepo, run_status_strings};+use crate::domain::models::{+ AdoptedRun, NotionJob, NotionSharing, ReviewedRun, RunReview, RunStatus, WrittenKind,+ WrittenObject,+};+use crate::domain::ports::{ImportError, NotionRunRepo, Result, WrittenObjects};++#[cfg(test)]+mod test;++fn seconds(duration: Duration) -> f64 {+ duration.as_secs_f64()+}++struct WrittenDbRow {+ import_id: Uuid,+ kind: String,+ object: String,+ macro_id: Uuid,+}++impl TryFrom<WrittenDbRow> for WrittenObject {+ type Error = ImportError;++ fn try_from(row: WrittenDbRow) -> Result<Self> {+ Ok(Self {+ import_id: row.import_id,+ kind: WrittenKind::from_str(&row.kind)+ .map_err(|_| anyhow::anyhow!("unknown written kind: {}", row.kind))?,+ object: row.object,+ macro_id: row.macro_id,+ })+ }+}++impl NotionRunRepo for PgImportRepo {+ #[tracing::instrument(skip(self, job), err)]+ async fn take_run(+ &self,+ user: &MacroUserIdStr<'static>,+ owner: Uuid,+ lease: Duration,+ job: &NotionJob,+ ) -> Result<bool> {+ let (scope, sharing, review_id) = match job {+ NotionJob::Plan { scope, sharing } => (+ Some(serde_json::to_value(scope)?),+ Some(sharing.as_ref()),+ None,+ ),+ NotionJob::Write { review_id } => (None, None, Some(*review_id)),+ };+ let result = sqlx::query!(+ r#"+ UPDATE import_run+ SET lease_owner = $2,+ lease_expires_at = NOW() + ($3::double precision * interval '1 second'),+ scope = $4, review_id = $5, sharing = $6, updated_at = NOW()+ WHERE user_id = $1 AND source = 'notion' AND status IN ('running', 'importing')+ AND lease_owner IS NULL AND lease_expires_at IS NULL+ "#,+ user.as_ref(),+ owner,+ seconds(lease),+ scope,+ review_id,+ sharing,+ )+ .execute(&self.pool)+ .await?;+ Ok(result.rows_affected() > 0)+ }++ #[tracing::instrument(skip(self), err)]+ async fn renew_lease(+ &self,+ user: &MacroUserIdStr<'static>,+ owner: Uuid,+ lease: Duration,+ ) -> Result<bool> {+ let result = sqlx::query!(+ r#"+ UPDATE import_run+ SET lease_expires_at = NOW() + ($3::double precision * interval '1 second'),+ updated_at = NOW()+ WHERE user_id = $1 AND source = 'notion' AND status IN ('running', 'importing')+ AND lease_owner = $2+ "#,+ user.as_ref(),+ owner,+ seconds(lease),+ )+ .execute(&self.pool)+ .await?;+ Ok(result.rows_affected() > 0)+ }++ #[tracing::instrument(skip(self), err)]+ async fn release_lease(&self, user: &MacroUserIdStr<'static>, owner: Uuid) -> Result<bool> {+ // Lapsed as of now, so the next adopter takes it straight away.+ let result = sqlx::query!(+ r#"+ UPDATE import_run+ SET lease_owner = NULL, lease_expires_at = NOW()+ WHERE user_id = $1 AND source = 'notion' AND lease_owner = $2+ "#,+ user.as_ref(),+ owner,+ )+ .execute(&self.pool)+ .await?;+ Ok(result.rows_affected() > 0)+ }++ #[tracing::instrument(skip(self), err)]+ async fn adopt_stalled_runs(+ &self,+ user: Option<&MacroUserIdStr<'static>>,+ owner: Uuid,+ lease: Duration,+ stale_after: Duration,+ limit: u16,+ ) -> Result<Vec<AdoptedRun>> {+ let rows = sqlx::query!(+ r#"+ WITH stalled AS (+ SELECT user_id FROM import_run+ WHERE source = 'notion' AND status IN ('running', 'importing')+ AND ($1::text IS NULL OR user_id = $1)+ AND (lease_expires_at < NOW()+ OR (lease_expires_at IS NULL+ AND updated_at < NOW() - ($4::double precision * interval '1 second')))+ ORDER BY updated_at+ LIMIT $5+ FOR UPDATE SKIP LOCKED+ )+ UPDATE import_run AS run+ SET lease_owner = $2,+ lease_expires_at = NOW() + ($3::double precision * interval '1 second'),+ adoptions = run.adoptions + 1,+ updated_at = NOW()+ FROM stalled+ WHERE run.user_id = stalled.user_id AND run.source = 'notion'+ RETURNING run.user_id, run.status::text AS "status!", run.scope, run.sharing,+ run.review_id,+ run.adoptions+ "#,+ user.map(|user| user.as_ref().to_string()) as Option<String>,+ owner,+ seconds(lease),+ seconds(stale_after),+ i64::from(limit),+ )+ .fetch_all(&self.pool)+ .await?;+ let mut adopted = Vec::with_capacity(rows.len());+ for row in rows {+ let user = MacroUserIdStr::parse_from_str(&row.user_id)+ .map_err(|error| anyhow::anyhow!("unparseable import run user: {error:?}"))?+ .into_owned();+ let status = RunStatus::from_str(&row.status)+ .map_err(|_| anyhow::anyhow!("unknown run status: {}", row.status))?;+ let job = match (status, row.scope, row.sharing, row.review_id) {+ (RunStatus::Running, Some(scope), Some(sharing), _) => Some(NotionJob::Plan {+ scope: serde_json::from_value::<ImportScope>(scope)?,+ sharing: NotionSharing::from_str(&sharing)+ .map_err(|_| anyhow::anyhow!("unknown notion sharing: {sharing}"))?,+ }),+ (RunStatus::Importing, _, _, Some(review_id)) => {+ Some(NotionJob::Write { review_id })+ }+ _ => None,+ };+ adopted.push(AdoptedRun {+ user,+ status,+ job,+ adoptions: u16::try_from(row.adoptions).unwrap_or(0),+ });+ }+ Ok(adopted)+ }++ #[tracing::instrument(skip(self, event), fields(notified = tracing::field::Empty), err)]+ async fn transition_reviewed_run<Event>(+ &self,+ user: &MacroUserIdStr<'static>,+ from: &[RunStatus],+ to: RunStatus,+ error: Option<&str>,+ event: Event,+ ) -> Result<Option<ReviewedRun>>+ where+ Event: FnOnce(&ReviewedRun) -> Option<PendingNotification> + Send,+ {+ let from = run_status_strings(from);+ let mut transaction = self.pool.begin().await?;+ let Some(row) = sqlx::query!(+ r#"+ UPDATE import_run+ SET status = CASE WHEN status = 'dismissed' THEN status+ ELSE $2::text::import_run_status END,+ error = CASE WHEN status = 'dismissed' THEN error ELSE $3 END,+ updated_at = NOW(), lease_owner = NULL, lease_expires_at = NULL+ WHERE user_id = $1 AND source = 'notion' AND status::text = ANY($4::text[])+ RETURNING status::text AS "status!", error, review_id, review_session_id+ "#,+ user.as_ref(),+ to.as_ref(),+ error,+ &from,+ )+ .fetch_optional(&mut *transaction)+ .await?+ else {+ return Ok(None);+ };+ let run = ReviewedRun {+ status: RunStatus::from_str(&row.status)+ .map_err(|_| anyhow::anyhow!("unknown run status: {}", row.status))?,+ error: row.error,+ review: row+ .review_id+ .zip(row.review_session_id)+ .map(|(review_id, session_id)| RunReview {+ review_id,+ session_id: AgentSessionId::new_from_uuid(session_id),+ }),+ };+ let pending = event(&run);+ tracing::Span::current().record("notified", pending.is_some());+ if let (Some(pending), Some(notifications)) = (pending, &self.notifications) {+ notifications+ .produce(&mut *transaction, &pending)+ .await+ .map_err(|error| ImportError::Db(rootcause::report!(error).into_dynamic()))?;+ }+ transaction.commit().await?;+ Ok(Some(run))+ }++ #[tracing::instrument(skip(self), err)]+ async fn root_folder(&self, user: &MacroUserIdStr<'static>) -> Result<Option<Uuid>> {+ let folder = sqlx::query_scalar!(+ r#"SELECT root_folder_id FROM import_run WHERE user_id = $1 AND source = 'notion'"#,+ user.as_ref(),+ )+ .fetch_optional(&self.pool)+ .await?;+ Ok(folder.flatten())+ }++ #[tracing::instrument(skip(self), err)]+ async fn set_root_folder(&self, user: &MacroUserIdStr<'static>, folder: Uuid) -> Result<()> {+ sqlx::query!(+ r#"UPDATE import_run SET root_folder_id = $2 WHERE user_id = $1 AND source = 'notion'"#,+ user.as_ref(),+ folder,+ )+ .execute(&self.pool)+ .await?;+ Ok(())+ }++ #[tracing::instrument(skip(self), err)]+ async fn settle_importing(&self, user: &MacroUserIdStr<'static>, reason: &str) -> Result<u64> {+ let mut transaction = self.pool.begin().await?;+ let settled = sqlx::query_scalar!(+ r#"+ UPDATE import_entity AS mine+ SET status = 'staged', last_error = $2, team_id = NULL, waiting_on = NULL,+ updated_at = NOW()+ WHERE mine.user_id = $1 AND mine.source = 'notion' AND mine.status = 'importing'+ AND mine.metadata->'plan' IS NOT NULL+ AND NOT EXISTS (+ SELECT 1 FROM import_run+ WHERE user_id = $1 AND source = 'notion' AND status = 'importing'+ )+ AND NOT EXISTS (+ SELECT 1 FROM import_entity AS holder+ WHERE holder.id = mine.waiting_on AND holder.status = 'importing'+ )+ RETURNING mine.id+ "#,+ user.as_ref(),+ reason,+ )+ .fetch_all(&mut *transaction)+ .await?;+ super::release_waiters(&mut transaction, &settled).await?;+ transaction.commit().await?;+ Ok(u64::try_from(settled.len()).unwrap_or(u64::MAX))+ }+}++impl WrittenObjects for PgImportRepo {+ #[tracing::instrument(skip(self, import_ids), fields(rows = import_ids.len()), err)]+ async fn written(+ &self,+ user: &MacroUserIdStr<'static>,+ import_ids: &[Uuid],+ ) -> Result<Vec<WrittenObject>> {+ let rows = sqlx::query_as!(+ WrittenDbRow,+ r#"+ SELECT written.import_id, written.kind, written.object, written.macro_id+ FROM import_entity_written AS written+ JOIN import_entity AS entity ON entity.id = written.import_id+ WHERE entity.user_id = $1 AND written.import_id = ANY($2::uuid[])+ "#,+ user.as_ref(),+ import_ids,+ )+ .fetch_all(&self.pool)+ .await?;+ rows.into_iter().map(WrittenObject::try_from).collect()+ }++ #[tracing::instrument(skip(self, objects), fields(objects = objects.len()), err)]+ async fn record_written(+ &self,+ user: &MacroUserIdStr<'static>,+ objects: &[WrittenObject],+ ) -> Result<Vec<WrittenObject>> {+ if objects.is_empty() {+ return Ok(Vec::new());+ }+ let import_ids: Vec<Uuid> = objects.iter().map(|object| object.import_id).collect();+ let kinds: Vec<String> = objects+ .iter()+ .map(|object| object.kind.as_ref().to_string())+ .collect();+ let names: Vec<String> = objects.iter().map(|object| object.object.clone()).collect();+ let macro_ids: Vec<Uuid> = objects.iter().map(|object| object.macro_id).collect();+ // The final SELECT sees the table as it was before the insert, so an+ // object comes back either as inserted now or as recorded earlier.+ let rows = sqlx::query_as!(+ WrittenDbRow,+ r#"+ WITH input AS (+ SELECT * FROM UNNEST($2::uuid[], $3::text[], $4::text[], $5::uuid[])+ AS input(import_id, kind, object, macro_id)+ ),+ inserted AS (+ INSERT INTO import_entity_written (import_id, kind, object, macro_id)+ SELECT input.import_id, input.kind, input.object, input.macro_id+ FROM input+ JOIN import_entity AS entity ON entity.id = input.import_id AND entity.user_id = $1+ ON CONFLICT (import_id, kind, object) DO NOTHING+ RETURNING import_id, kind, object, macro_id+ )+ SELECT import_id AS "import_id!", kind AS "kind!", object AS "object!",+ macro_id AS "macro_id!"+ FROM inserted+ UNION ALL+ SELECT written.import_id, written.kind, written.object, written.macro_id+ FROM import_entity_written AS written+ JOIN input USING (import_id, kind, object)+ JOIN import_entity AS entity ON entity.id = written.import_id AND entity.user_id = $1+ "#,+ user.as_ref(),+ &import_ids,+ &kinds,+ &names,+ ¯o_ids,+ )+ .fetch_all(&self.pool)+ .await?;+ rows.into_iter().map(WrittenObject::try_from).collect()+ }+}diff --git a/crates/import/src/outbound/pg_import_repo/notion_run/test.rs b/crates/import/src/outbound/pg_import_repo/notion_run/test.rsnew file mode 100644index 0000000000..e18ddf57f1--- /dev/null+++ b/crates/import/src/outbound/pg_import_repo/notion_run/test.rs@@ -0,0 +1,490 @@+use std::time::Duration;++use import_notion::domain::model::{ImportScope, NotionId};+use macro_db_migrator::MACRO_DB_MIGRATIONS;+use macro_user_id::cowlike::CowLike;+use sqlx::PgPool;++use super::*;+use crate::domain::models::{ImportKind, ImportSource, ImportStatus};+use crate::domain::ports::{ImportRepo, NotionPlanRepo, PlanRow};++fn user(label: &str) -> MacroUserIdStr<'static> {+ MacroUserIdStr::parse_from_str(&format!("macro|{label}@example.com"))+ .unwrap()+ .into_owned()+}++const LEASE: Duration = Duration::from_secs(90);+const STALE: Duration = Duration::from_secs(270);++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn a_run_is_held_renewed_and_released_by_its_owner_only(pool: PgPool) {+ let repo = PgImportRepo::new(pool);+ let alice = user("lease-owner");+ let owner = Uuid::now_v7();+ let other = Uuid::now_v7();+ let job = NotionJob::Plan {+ sharing: NotionSharing::Ai,+ scope: ImportScope::Everything,+ };++ assert!(+ !repo.take_run(&alice, owner, LEASE, &job).await.unwrap(),+ "no run to take yet"+ );+ repo.start_run(&alice, ImportSource::Notion, &[])+ .await+ .unwrap();+ assert!(repo.take_run(&alice, owner, LEASE, &job).await.unwrap());+ assert!(+ !repo.take_run(&alice, other, LEASE, &job).await.unwrap(),+ "a held run is not taken again"+ );+ assert!(repo.renew_lease(&alice, owner, LEASE).await.unwrap());+ assert!(!repo.renew_lease(&alice, other, LEASE).await.unwrap());+ assert!(+ repo.adopt_stalled_runs(Some(&alice), other, LEASE, STALE, 10)+ .await+ .unwrap()+ .is_empty(),+ "a live lease is never adopted"+ );++ assert!(!repo.release_lease(&alice, other).await.unwrap());+ assert!(repo.release_lease(&alice, owner).await.unwrap());+ assert!(+ !repo.renew_lease(&alice, owner, LEASE).await.unwrap(),+ "a released lease is gone"+ );+ let adopted = repo+ .adopt_stalled_runs(Some(&alice), other, LEASE, STALE, 10)+ .await+ .unwrap();+ assert_eq!(+ adopted,+ [AdoptedRun {+ user: alice.clone(),+ status: RunStatus::Running,+ job: Some(job),+ adoptions: 1,+ }],+ "a released run is adopted at once, with its job"+ );+ assert!(repo.renew_lease(&alice, other, LEASE).await.unwrap());+}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn an_expired_lease_is_adopted_once_and_a_finished_run_never(pool: PgPool) {+ let repo = PgImportRepo::new(pool.clone());+ let alice = user("lease-expired");+ let bob = user("lease-finished");+ let dead = Uuid::now_v7();+ let review_id = review(&pool, &alice).await;+ for owner in [&alice, &bob] {+ repo.start_run(owner, ImportSource::Notion, &[])+ .await+ .unwrap();+ repo.transition_run(+ owner,+ ImportSource::Notion,+ &[RunStatus::Running],+ RunStatus::Importing,+ None,+ )+ .await+ .unwrap();+ }+ // A lease of zero has lapsed by the next statement: the process died.+ assert!(+ repo.take_run(+ &alice,+ dead,+ Duration::ZERO,+ &NotionJob::Write { review_id }+ )+ .await+ .unwrap()+ );+ repo.take_run(&bob, dead, Duration::ZERO, &NotionJob::Write { review_id })+ .await+ .unwrap();+ repo.transition_run(+ &bob,+ ImportSource::Notion,+ &[RunStatus::Importing],+ RunStatus::Completed,+ None,+ )+ .await+ .unwrap();++ let first = Uuid::now_v7();+ let adopted = repo+ .adopt_stalled_runs(None, first, LEASE, STALE, 10)+ .await+ .unwrap();+ let mine: Vec<&AdoptedRun> = adopted+ .iter()+ .filter(|run| run.user == alice || run.user == bob)+ .collect();+ assert_eq!(mine.len(), 1, "only the stalled run: {adopted:?}");+ assert_eq!(mine[0].user, alice);+ assert_eq!(mine[0].status, RunStatus::Importing);+ assert_eq!(mine[0].job, Some(NotionJob::Write { review_id }));+ assert!(+ repo.adopt_stalled_runs(Some(&alice), Uuid::now_v7(), LEASE, STALE, 10)+ .await+ .unwrap()+ .is_empty(),+ "the adopter holds it now"+ );+ assert!(+ !repo.renew_lease(&alice, dead, LEASE).await.unwrap(),+ "the dead owner lost it"+ );+}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn concurrent_adopters_split_stalled_runs_without_overlap(pool: PgPool) {+ let repo = PgImportRepo::new(pool);+ let dead = Uuid::now_v7();+ let users: Vec<MacroUserIdStr<'static>> = (0..6)+ .map(|index| user(&format!("lease-many-{index}")))+ .collect();+ for owner in &users {+ repo.start_run(owner, ImportSource::Notion, &[])+ .await+ .unwrap();+ repo.take_run(+ owner,+ dead,+ Duration::ZERO,+ &NotionJob::Plan {+ sharing: NotionSharing::Ai,+ scope: ImportScope::Everything,+ },+ )+ .await+ .unwrap();+ }++ let (left, right) = tokio::join!(+ repo.adopt_stalled_runs(None, Uuid::now_v7(), LEASE, STALE, 6),+ repo.adopt_stalled_runs(None, Uuid::now_v7(), LEASE, STALE, 6),+ );+ let mut adopted: Vec<String> = left+ .unwrap()+ .into_iter()+ .chain(right.unwrap())+ .map(|run| run.user.as_ref().to_string())+ .filter(|adopted| users.iter().any(|owner| owner.as_ref() == adopted))+ .collect();+ adopted.sort();+ let mut expected: Vec<String> = users+ .iter()+ .map(|owner| owner.as_ref().to_string())+ .collect();+ expected.sort();+ assert_eq!(adopted, expected, "every run adopted exactly once");+}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn an_unleased_run_from_older_code_is_adopted_only_once_it_goes_quiet(pool: PgPool) {+ let repo = PgImportRepo::new(pool.clone());+ let alice = user("lease-legacy");+ repo.start_run(&alice, ImportSource::Notion, &[])+ .await+ .unwrap();+ assert!(+ repo.adopt_stalled_runs(Some(&alice), Uuid::now_v7(), LEASE, STALE, 1)+ .await+ .unwrap()+ .is_empty(),+ "its old heartbeat is still fresh"+ );+ sqlx::query!(+ "UPDATE import_run SET updated_at = NOW() - interval '10 minutes' WHERE user_id = $1",+ alice.as_ref(),+ )+ .execute(&pool)+ .await+ .unwrap();+ let adopted = repo+ .adopt_stalled_runs(Some(&alice), Uuid::now_v7(), LEASE, STALE, 1)+ .await+ .unwrap();+ assert_eq!(adopted.len(), 1);+ assert_eq!(adopted[0].job, None, "no job was recorded");+}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn written_objects_keep_their_first_record_and_stay_with_their_owner(pool: PgPool) {+ let repo = PgImportRepo::new(pool);+ let alice = user("written-owner");+ let mallory = user("written-other");+ let staged = repo+ .replace_plan(+ &alice,+ ImportSource::Notion,+ &[PlanRow {+ kind: ImportKind::DataSource,+ foreign_id: "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee".to_string(),+ parent_foreign_id: None,+ metadata: serde_json::json!({ "title": "Bugs", "url": null, "summary": null }),+ }],+ )+ .await+ .unwrap();+ let row = staged[0].id;+ let notion_row = NotionId::parse("11111111111111111111111111111111").unwrap();+ let first = Uuid::now_v7();+ let second = Uuid::now_v7();++ let recorded = repo+ .record_written(+ &alice,+ &[+ WrittenObject::own(row, WrittenKind::Database, first),+ WrittenObject::for_row(row, WrittenKind::Row, ¬ion_row, first),+ ],+ )+ .await+ .unwrap();+ assert_eq!(recorded.len(), 2);+ let again = repo+ .record_written(+ &alice,+ &[WrittenObject::own(row, WrittenKind::Database, second)],+ )+ .await+ .unwrap();+ assert_eq!(+ again,+ [WrittenObject::own(row, WrittenKind::Database, first)],+ "the earlier record wins"+ );+ assert!(+ repo.record_written(+ &mallory,+ &[WrittenObject::own(row, WrittenKind::Folder, second)]+ )+ .await+ .unwrap()+ .is_empty(),+ "nobody records against another user's row"+ );+ assert!(repo.written(&mallory, &[row]).await.unwrap().is_empty());+ let mut written = repo.written(&alice, &[row]).await.unwrap();+ written.sort_by_key(|object| object.kind.as_ref().to_string());+ assert_eq!(+ written,+ [+ WrittenObject::own(row, WrittenKind::Database, first),+ WrittenObject::for_row(row, WrittenKind::Row, ¬ion_row, first),+ ]+ );+}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn rows_a_stopped_write_left_claimed_are_settled_only_once_no_write_runs(pool: PgPool) {+ let repo = PgImportRepo::new(pool);+ let alice = user("settle-owner");+ let staged = repo+ .replace_plan(+ &alice,+ ImportSource::Notion,+ &[PlanRow {+ kind: ImportKind::Page,+ foreign_id: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa".to_string(),+ parent_foreign_id: None,+ metadata: serde_json::json!({+ "title": "Roadmap", "url": null, "summary": null,+ "plan": { "decision": "copy", "path": [] }+ }),+ }],+ )+ .await+ .unwrap();+ repo.mark_importing(&alice, &[staged[0].id]).await.unwrap();+ repo.start_run(&alice, ImportSource::Notion, &[])+ .await+ .unwrap();+ repo.transition_run(+ &alice,+ ImportSource::Notion,+ &[RunStatus::Running],+ RunStatus::Importing,+ None,+ )+ .await+ .unwrap();++ assert_eq!(repo.settle_importing(&alice, "stopped").await.unwrap(), 0);+ repo.transition_run(+ &alice,+ ImportSource::Notion,+ &[RunStatus::Importing],+ RunStatus::Failed,+ None,+ )+ .await+ .unwrap();+ assert_eq!(repo.settle_importing(&alice, "stopped").await.unwrap(), 1);+ let row = repo.get(&alice, staged[0].id).await.unwrap().unwrap();+ assert_eq!(row.status, ImportStatus::Staged);+ assert_eq!(row.last_error.as_deref(), Some("stopped"));+}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn the_root_folder_outlives_the_run_that_made_it(pool: PgPool) {+ let repo = PgImportRepo::new(pool);+ let alice = user("root-folder");+ repo.start_run(&alice, ImportSource::Notion, &[])+ .await+ .unwrap();+ assert_eq!(repo.root_folder(&alice).await.unwrap(), None);+ let folder = Uuid::now_v7();+ repo.set_root_folder(&alice, folder).await.unwrap();+ repo.transition_run(+ &alice,+ ImportSource::Notion,+ &[RunStatus::Running],+ RunStatus::Ready,+ None,+ )+ .await+ .unwrap();+ repo.start_run(&alice, ImportSource::Notion, &[RunStatus::Ready])+ .await+ .unwrap();+ assert_eq!(repo.root_folder(&alice).await.unwrap(), Some(folder));+}++async fn review(pool: &PgPool, owner: &MacroUserIdStr<'static>) -> Uuid {+ let session = Uuid::now_v7();+ sqlx::query!(+ r#"INSERT INTO agent_session (id, owner_id, bot_id, harness, model, workspace, for_warming) VALUES ($1, $2, '00000000-0000-0000-0000-00000000a2a2', 'macro-inmem', 'test-model', '/workspace', false)"#,+ session,+ owner.as_ref(),+ )+ .execute(pool)+ .await+ .unwrap();+ let review = Uuid::now_v7();+ sqlx::query!(+ r#"INSERT INTO agent_session_review (id, agent_session_id, record) VALUES ($1, $2, '{}'::jsonb)"#,+ review,+ session,+ )+ .execute(pool)+ .await+ .unwrap();+ review+}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn a_reviewed_run_reports_its_review_and_a_cancel_survives_the_write_ending(pool: PgPool) {+ let repo = PgImportRepo::new(pool.clone());+ let alice = user("reviewed-run");+ let review = review(&pool, &alice).await;+ let session = sqlx::query_scalar!(+ "SELECT agent_session_id FROM agent_session_review WHERE id = $1",+ review+ )+ .fetch_one(&pool)+ .await+ .unwrap();+ repo.start_run(&alice, ImportSource::Notion, &[])+ .await+ .unwrap();+ repo.attach_review(+ &alice,+ ImportSource::Notion,+ review,+ AgentSessionId::new_from_uuid(session),+ )+ .await+ .unwrap();+ let to_importing = repo+ .transition_reviewed_run(+ &alice,+ &[RunStatus::Running],+ RunStatus::Importing,+ None,+ |_| None,+ )+ .await+ .unwrap()+ .unwrap();+ assert_eq!(+ to_importing.review,+ Some(RunReview {+ review_id: review,+ session_id: AgentSessionId::new_from_uuid(session),+ })+ );+ // The owner cancels from another process while the write runs.+ repo.transition_run(+ &alice,+ ImportSource::Notion,+ &[RunStatus::Importing],+ RunStatus::Dismissed,+ Some("Cancelled"),+ )+ .await+ .unwrap();++ let mut seen = None;+ let ended = repo+ .transition_reviewed_run(+ &alice,+ &[RunStatus::Importing, RunStatus::Dismissed],+ RunStatus::Completed,+ None,+ |run| {+ seen = Some(run.status);+ None+ },+ )+ .await+ .unwrap()+ .unwrap();++ assert_eq!(seen, Some(RunStatus::Dismissed));+ assert_eq!(ended.status, RunStatus::Dismissed);+ assert_eq!(ended.error.as_deref(), Some("Cancelled"));+ assert!(+ repo.transition_reviewed_run(+ &alice,+ &[RunStatus::Importing],+ RunStatus::Completed,+ None,+ |_| None+ )+ .await+ .unwrap()+ .is_none(),+ "a run in none of `from` does not move"+ );++ sqlx::query!("DELETE FROM agent_session WHERE id = $1", session)+ .execute(&pool)+ .await+ .unwrap();+ let orphaned = repo+ .transition_reviewed_run(+ &alice,+ &[RunStatus::Dismissed],+ RunStatus::Dismissed,+ None,+ |_| None,+ )+ .await+ .unwrap()+ .unwrap();+ assert_eq!(+ orphaned.review, None,+ "a deleted session no longer hears of the run"+ );+}diff --git a/crates/import/src/outbound/pg_import_repo/plan.rs b/crates/import/src/outbound/pg_import_repo/plan.rsnew file mode 100644index 0000000000..4d4eee8caf--- /dev/null+++ b/crates/import/src/outbound/pg_import_repo/plan.rs@@ -0,0 +1,531 @@+//! Postgres side of Notion workspace plans: staging a plan, the user's+//! sharing choices, and the team's claim on each object it writes.++use std::collections::HashMap;++use agent_session::domain::model::AgentSessionId;+use macro_user_id::user_id::MacroUserIdStr;+use uuid::Uuid;++use super::{ImportEntityDbRow, PgImportRepo, run_status_strings};+use crate::domain::models::{ImportEntity, ImportSource, RunStatus, ScanCounts, TeamClaim};+use crate::domain::ports::{NotionPlanRepo, PlanRow, Result};++#[cfg(test)]+mod test;++/// Attempts at a claim before giving up: a teammate claiming the same object+/// at the same moment makes an attempt fail on the team's unique index, and+/// the next attempt sees their claim.+const CLAIM_ATTEMPTS: u8 = 3;++fn is_unique_violation(error: &sqlx::Error) -> bool {+ error+ .as_database_error()+ .is_some_and(|error| error.is_unique_violation())+}++impl NotionPlanRepo for PgImportRepo {+ #[tracing::instrument(skip(self, rows), fields(rows = rows.len()), err)]+ async fn replace_plan(+ &self,+ user: &MacroUserIdStr<'static>,+ source: ImportSource,+ rows: &[PlanRow],+ ) -> Result<Vec<ImportEntity>> {+ let mut transaction = self.pool.begin().await?;+ // Locked until the plan is staged, so a review attached meanwhile+ // waits and then ties these rows too.+ let review_id = sqlx::query_scalar!(+ r#"+ SELECT review_id FROM import_run+ WHERE user_id = $1 AND source = $2 AND status = 'running'+ FOR UPDATE+ "#,+ user.as_ref(),+ source.as_ref(),+ )+ .fetch_optional(&mut *transaction)+ .await?+ .flatten();+ sqlx::query!(+ r#"+ DELETE FROM import_entity+ WHERE user_id = $1 AND source = $2 AND status = 'staged'+ AND metadata->'plan' IS NOT NULL+ "#,+ user.as_ref(),+ source.as_ref(),+ )+ .execute(&mut *transaction)+ .await?;++ let mut ids: HashMap<&str, Uuid> = HashMap::with_capacity(rows.len());+ let mut staged = Vec::with_capacity(rows.len());+ for row in rows {+ let parent_id = row+ .parent_foreign_id+ .as_deref()+ .and_then(|parent| ids.get(parent).copied());+ let inserted = sqlx::query_as!(+ ImportEntityDbRow,+ r#"+ INSERT INTO import_entity+ (user_id, source, kind, foreign_id, status, initiator, metadata, parent_id,+ review_id)+ VALUES ($1, $2, $3, $4, 'staged', 'chat', $5, $6, $7)+ ON CONFLICT (user_id, source, foreign_id) DO UPDATE+ SET kind = EXCLUDED.kind, metadata = EXCLUDED.metadata,+ parent_id = EXCLUDED.parent_id, status = 'staged',+ review_id = EXCLUDED.review_id, last_error = NULL, updated_at = NOW()+ WHERE import_entity.status IN ('staged', 'discarded')+ RETURNING id, user_id, team_id, source, kind, foreign_id, parent_id, review_id,+ status, initiator, metadata, entity_id, entity_type, last_error,+ waiting_on, created_at, updated_at+ "#,+ user.as_ref(),+ source.as_ref(),+ row.kind.as_ref(),+ row.foreign_id,+ row.metadata,+ parent_id,+ review_id,+ )+ .fetch_optional(&mut *transaction)+ .await?;+ let id = match inserted {+ Some(inserted) => {+ let entity = ImportEntity::try_from(inserted)?;+ let id = entity.id;+ staged.push(entity);+ id+ }+ // Already importing or imported: children still hang under it.+ None => {+ sqlx::query_scalar!(+ r#"+ SELECT id FROM import_entity+ WHERE user_id = $1 AND source = $2 AND foreign_id = $3+ "#,+ user.as_ref(),+ source.as_ref(),+ row.foreign_id,+ )+ .fetch_one(&mut *transaction)+ .await?+ }+ };+ ids.insert(row.foreign_id.as_str(), id);+ }+ transaction.commit().await?;+ Ok(staged)+ }++ #[tracing::instrument(skip(self, rows), fields(rows = rows.len()), err)]+ async fn update_plan_rows(+ &self,+ user: &MacroUserIdStr<'static>,+ source: ImportSource,+ rows: &[PlanRow],+ ) -> Result<u64> {+ if rows.is_empty() {+ return Ok(0);+ }+ let foreign_ids: Vec<String> = rows.iter().map(|row| row.foreign_id.clone()).collect();+ let metadata: Vec<serde_json::Value> =+ rows.iter().map(|row| row.metadata.clone()).collect();+ let result = sqlx::query!(+ r#"+ UPDATE import_entity AS entity+ SET metadata = decided.metadata, updated_at = NOW()+ FROM UNNEST($3::text[], $4::jsonb[]) AS decided(foreign_id, metadata)+ WHERE entity.user_id = $1 AND entity.source = $2+ AND entity.foreign_id = decided.foreign_id+ AND entity.status = 'staged' AND entity.metadata->'plan' IS NOT NULL+ "#,+ user.as_ref(),+ source.as_ref(),+ &foreign_ids,+ &metadata,+ )+ .execute(&self.pool)+ .await?;+ Ok(result.rows_affected())+ }++ #[tracing::instrument(skip(self), err)]+ async fn attach_review(+ &self,+ user: &MacroUserIdStr<'static>,+ source: ImportSource,+ review_id: Uuid,+ session_id: AgentSessionId,+ ) -> Result<u64> {+ let mut transaction = self.pool.begin().await?;+ // First, so a plan being staged right now finishes before the rows+ // are tied (it holds the run), or stages them with this review.+ sqlx::query!(+ r#"+ UPDATE import_run+ SET review_id = CASE WHEN status = 'running' THEN $3 ELSE review_id END,+ review_session_id = CASE WHEN status = 'running' THEN $4+ ELSE COALESCE(review_session_id, $4) END+ WHERE user_id = $1 AND source = $2 AND status IN ('running', 'ready')+ "#,+ user.as_ref(),+ source.as_ref(),+ review_id,+ session_id.as_uuid(),+ )+ .execute(&mut *transaction)+ .await?;+ let result = sqlx::query!(+ r#"+ UPDATE import_entity+ SET review_id = $3, updated_at = NOW()+ WHERE user_id = $1 AND source = $2 AND status = 'staged'+ AND metadata->'plan' IS NOT NULL AND review_id IS NULL+ "#,+ user.as_ref(),+ source.as_ref(),+ review_id,+ )+ .execute(&mut *transaction)+ .await?;+ transaction.commit().await?;+ Ok(result.rows_affected())+ }++ #[tracing::instrument(skip(self), err)]+ async fn transition_run(+ &self,+ user: &MacroUserIdStr<'static>,+ source: ImportSource,+ from: &[RunStatus],+ to: RunStatus,+ error: Option<&str>,+ ) -> Result<bool> {+ let from = run_status_strings(from);+ let result = sqlx::query!(+ r#"+ UPDATE import_run+ SET status = $3::text::import_run_status, error = $4, updated_at = NOW(),+ lease_owner = NULL, lease_expires_at = NULL+ WHERE user_id = $1 AND source = $2 AND status::text = ANY($5::text[])+ "#,+ user.as_ref(),+ source.as_ref(),+ to.as_ref(),+ error,+ &from,+ )+ .execute(&self.pool)+ .await?;+ Ok(result.rows_affected() > 0)+ }++ #[tracing::instrument(skip(self), fields(pages = scanned.pages, data_sources = scanned.data_sources), err)]+ async fn record_scan(+ &self,+ user: &MacroUserIdStr<'static>,+ source: ImportSource,+ scanned: ScanCounts,+ ) -> Result<bool> {+ let pages = i32::try_from(scanned.pages).unwrap_or(i32::MAX);+ let data_sources = i32::try_from(scanned.data_sources).unwrap_or(i32::MAX);+ let result = sqlx::query!(+ r#"+ UPDATE import_run+ SET scanned_pages = $3, scanned_data_sources = $4+ WHERE user_id = $1 AND source = $2 AND status = 'running'+ "#,+ user.as_ref(),+ source.as_ref(),+ pages,+ data_sources,+ )+ .execute(&self.pool)+ .await?;+ Ok(result.rows_affected() > 0)+ }++ #[tracing::instrument(skip(self, foreign_ids), fields(objects = foreign_ids.len()), err)]+ async fn team_copies(+ &self,+ user: &MacroUserIdStr<'static>,+ foreign_ids: &[String],+ ) -> Result<Vec<ImportEntity>> {+ if foreign_ids.is_empty() {+ return Ok(Vec::new());+ }+ let rows = sqlx::query_as!(+ ImportEntityDbRow,+ r#"+ SELECT id, user_id, team_id, source, kind, foreign_id, parent_id, review_id, status,+ initiator, metadata, entity_id, entity_type, last_error, waiting_on, created_at,+ updated_at+ FROM import_entity+ WHERE source = 'notion' AND foreign_id = ANY($2::text[])+ AND status IN ('importing', 'imported')+ AND user_id <> $1+ AND team_id IS NOT NULL+ AND team_id = (SELECT team_id FROM team_user WHERE user_id = $1)+ "#,+ user.as_ref(),+ foreign_ids,+ )+ .fetch_all(&self.pool)+ .await?;+ rows.into_iter().map(ImportEntity::try_from).collect()+ }++ #[tracing::instrument(skip(self, shared, personal), fields(shared = shared.len(), personal = personal.len()), err)]+ async fn set_plan_sharing(+ &self,+ user: &MacroUserIdStr<'static>,+ shared: &[Uuid],+ personal: &[Uuid],+ ) -> Result<u64> {+ if shared.is_empty() && personal.is_empty() {+ return Ok(0);+ }+ let result = sqlx::query!(+ r#"+ UPDATE import_entity+ SET metadata = jsonb_set(metadata, '{plan,shared}', to_jsonb(id = ANY($2::uuid[]))),+ updated_at = NOW()+ WHERE user_id = $1 AND status = 'staged' AND metadata->'plan' IS NOT NULL+ AND (id = ANY($2::uuid[]) OR id = ANY($3::uuid[]))+ "#,+ user.as_ref(),+ shared,+ personal,+ )+ .execute(&self.pool)+ .await?;+ Ok(result.rows_affected())+ }++ #[tracing::instrument(skip(self, ids, shared), fields(rows = ids.len(), shared = shared.len()), err)]+ async fn claim_importing(+ &self,+ user: &MacroUserIdStr<'static>,+ ids: &[Uuid],+ shared: &[Uuid],+ team_id: Option<Uuid>,+ ) -> Result<Vec<ImportEntity>> {+ if ids.is_empty() {+ return Ok(Vec::new());+ }+ let mut attempt = 1;+ let rows = loop {+ let claimed = sqlx::query_as!(+ ImportEntityDbRow,+ r#"+ UPDATE import_entity AS mine+ SET status = 'importing', last_error = NULL, waiting_on = NULL, updated_at = NOW(),+ team_id = CASE+ WHEN $4::uuid IS NOT NULL AND mine.id = ANY($3::uuid[]) AND NOT EXISTS (+ SELECT 1 FROM import_entity AS other+ WHERE other.source = mine.source AND other.team_id = $4+ AND other.foreign_id = mine.foreign_id+ AND other.status IN ('importing', 'imported')+ AND other.id <> mine.id+ ) THEN $4+ ELSE NULL+ END+ WHERE mine.user_id = $1 AND mine.id = ANY($2::uuid[]) AND mine.status = 'staged'+ RETURNING mine.id, mine.user_id, mine.team_id, mine.source, mine.kind,+ mine.foreign_id, mine.parent_id, mine.review_id, mine.status,+ mine.initiator, mine.metadata, mine.entity_id, mine.entity_type,+ mine.last_error, mine.waiting_on, mine.created_at, mine.updated_at+ "#,+ user.as_ref(),+ ids,+ shared,+ team_id,+ )+ .fetch_all(&self.pool)+ .await;+ match claimed {+ Ok(rows) => break rows,+ Err(error) if is_unique_violation(&error) && attempt < CLAIM_ATTEMPTS => {+ tracing::info!(+ attempt,+ "a teammate claimed the same notion object; claiming again"+ );+ attempt += 1;+ }+ Err(error) => return Err(error.into()),+ }+ };+ rows.into_iter().map(ImportEntity::try_from).collect()+ }++ #[tracing::instrument(skip(self), err)]+ async fn claim_team_object(+ &self,+ user: &MacroUserIdStr<'static>,+ import_id: Uuid,+ team_id: Uuid,+ claim: bool,+ ) -> Result<TeamClaim> {+ let mut attempt = 1;+ loop {+ match self+ .try_claim_team_object(user, import_id, team_id, claim)+ .await+ {+ Ok(outcome) => return Ok(outcome),+ Err(error) if is_unique_violation(&error) && attempt < CLAIM_ATTEMPTS => {+ tracing::info!(+ attempt,+ "a teammate claimed the same notion object; checking again"+ );+ attempt += 1;+ }+ Err(error) => return Err(error.into()),+ }+ }+ }++ #[tracing::instrument(skip(self), err)]+ async fn wait_on(+ &self,+ user: &MacroUserIdStr<'static>,+ import_id: Uuid,+ holder: Uuid,+ ) -> Result<bool> {+ let mut transaction = self.pool.begin().await?;+ // Holding the holder's row keeps it from being imported or released+ // until this row is waiting, so whichever comes second sees the other.+ let writing = sqlx::query_scalar!(+ r#"+ SELECT id FROM import_entity+ WHERE id = $1 AND status = 'importing' AND team_id IS NOT NULL+ FOR SHARE+ "#,+ holder,+ )+ .fetch_optional(&mut *transaction)+ .await?;+ if writing.is_none() {+ return Ok(false);+ }+ let result = sqlx::query!(+ r#"+ UPDATE import_entity SET waiting_on = $3, updated_at = NOW()+ WHERE user_id = $1 AND id = $2 AND status = 'importing'+ "#,+ user.as_ref(),+ import_id,+ holder,+ )+ .execute(&mut *transaction)+ .await?;+ transaction.commit().await?;+ Ok(result.rows_affected() > 0)+ }+}++impl PgImportRepo {+ /// One attempt at [`NotionPlanRepo::claim_team_object`], in one+ /// transaction holding the row and the team's current holder.+ async fn try_claim_team_object(+ &self,+ user: &MacroUserIdStr<'static>,+ import_id: Uuid,+ team_id: Uuid,+ claim: bool,+ ) -> sqlx::Result<TeamClaim> {+ let mut transaction = self.pool.begin().await?;+ let Some(own) = sqlx::query!(+ r#"+ SELECT foreign_id, team_id FROM import_entity+ WHERE user_id = $1 AND id = $2 AND status = 'importing'+ FOR UPDATE+ "#,+ user.as_ref(),+ import_id,+ )+ .fetch_optional(&mut *transaction)+ .await?+ else {+ // Not being written any more (cancelled, settled): nothing to claim.+ return Ok(TeamClaim::Free);+ };+ if own.team_id == Some(team_id) {+ return Ok(TeamClaim::Held);+ }+ let holder = sqlx::query!(+ r#"+ SELECT holder.id, holder.status, holder.entity_id, holder.entity_type,+ EXISTS (+ SELECT 1 FROM import_run AS run+ WHERE run.user_id = holder.user_id AND run.source = 'notion'+ AND run.status = 'importing' AND run.lease_expires_at > NOW()+ ) AS "live!"+ FROM import_entity AS holder+ WHERE holder.source = 'notion' AND holder.team_id = $2 AND holder.foreign_id = $3+ AND holder.status IN ('importing', 'imported') AND holder.user_id <> $1+ FOR UPDATE OF holder+ "#,+ user.as_ref(),+ team_id,+ own.foreign_id,+ )+ .fetch_optional(&mut *transaction)+ .await?;+ let outcome = match holder {+ Some(holder) if holder.status == "imported" => TeamClaim::Linked {+ entity_id: holder.entity_id.unwrap_or_default(),+ entity_type: holder.entity_type.unwrap_or_default(),+ },+ Some(holder) if holder.live => TeamClaim::Waiting { holder: holder.id },+ // The holder's import stopped: take its claim over, and its+ // waiters with it.+ Some(holder) if claim => {+ sqlx::query!(+ "UPDATE import_entity SET team_id = NULL, updated_at = NOW() WHERE id = $1",+ holder.id,+ )+ .execute(&mut *transaction)+ .await?;+ sqlx::query!(+ r#"+ UPDATE import_entity SET waiting_on = $2, updated_at = NOW()+ WHERE waiting_on = $1 AND status = 'importing'+ "#,+ holder.id,+ import_id,+ )+ .execute(&mut *transaction)+ .await?;+ claim_own(&mut transaction, import_id, team_id).await?;+ TeamClaim::Held+ }+ None if claim => {+ claim_own(&mut transaction, import_id, team_id).await?;+ TeamClaim::Held+ }+ Some(_) | None => TeamClaim::Free,+ };+ transaction.commit().await?;+ Ok(outcome)+ }+}++async fn claim_own(+ transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,+ import_id: Uuid,+ team_id: Uuid,+) -> sqlx::Result<()> {+ sqlx::query!(+ "UPDATE import_entity SET team_id = $2, updated_at = NOW() WHERE id = $1",+ import_id,+ team_id,+ )+ .execute(&mut **transaction)+ .await?;+ Ok(())+}diff --git a/crates/import/src/outbound/pg_import_repo/plan/test.rs b/crates/import/src/outbound/pg_import_repo/plan/test.rsnew file mode 100644index 0000000000..636da464eb--- /dev/null+++ b/crates/import/src/outbound/pg_import_repo/plan/test.rs@@ -0,0 +1,337 @@+//! The team's claim on each Notion object, against the real ledger: one+//! teammate writes an object for the team, and the others link to it.++use std::time::Duration;++use macro_db_migrator::MACRO_DB_MIGRATIONS;+use macro_user_id::cowlike::CowLike;+use sqlx::PgPool;++use super::*;+use crate::domain::models::{ImportKind, ImportStatus, NotionJob, TEAMMATE_STOPPED};+use crate::domain::ports::{ImportRepo, NotionRunRepo};++const ROADMAP: &str = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";++async fn member(pool: &PgPool, label: &str, team: Uuid) -> 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();+ sqlx::query!(+ "INSERT INTO team (id, name, owner_id) VALUES ($1, 'Claims', $2) ON CONFLICT (id) DO NOTHING",+ team,+ user_id,+ )+ .execute(pool)+ .await+ .unwrap();+ sqlx::query!(+ "INSERT INTO team_user (user_id, team_id, team_role) VALUES ($1, $2, 'member')",+ user_id,+ team,+ )+ .execute(pool)+ .await+ .unwrap();+ MacroUserIdStr::parse_from_str(&user_id)+ .unwrap()+ .into_owned()+}++/// `user`'s one-page plan of the Roadmap, staged.+async fn roadmap_plan(repo: &PgImportRepo, user: &MacroUserIdStr<'static>) -> Uuid {+ let staged = repo+ .replace_plan(+ user,+ ImportSource::Notion,+ &[PlanRow {+ kind: ImportKind::Page,+ foreign_id: ROADMAP.to_string(),+ parent_foreign_id: None,+ metadata: serde_json::json!({+ "title": "Roadmap", "url": null, "summary": null,+ "plan": { "decision": "copy", "path": [], "shared": true, "shareable": true }+ }),+ }],+ )+ .await+ .unwrap();+ staged[0].id+}++/// `user`'s run, writing, held by a live process.+async fn writing(repo: &PgImportRepo, user: &MacroUserIdStr<'static>, lease: Duration) {+ repo.start_run(user, ImportSource::Notion, &[])+ .await+ .unwrap();+ repo.transition_run(+ user,+ ImportSource::Notion,+ &[RunStatus::Running],+ RunStatus::Importing,+ None,+ )+ .await+ .unwrap();+ let job = NotionJob::Plan {+ scope: import_notion::domain::model::ImportScope::Everything,+ sharing: crate::domain::models::NotionSharing::Team,+ };+ assert!(+ repo.take_run(user, Uuid::now_v7(), lease, &job)+ .await+ .unwrap()+ );+}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn only_the_first_teammate_to_start_writing_holds_the_teams_claim(pool: PgPool) {+ let repo = PgImportRepo::new(pool.clone());+ let team = Uuid::now_v7();+ let ana = member(&pool, "claim-ana", team).await;+ let bo = member(&pool, "claim-bo", team).await;+ let anas = roadmap_plan(&repo, &ana).await;+ let bos = roadmap_plan(&repo, &bo).await;++ let first = repo+ .claim_importing(&ana, &[anas], &[anas], Some(team))+ .await+ .unwrap();+ let second = repo+ .claim_importing(&bo, &[bos], &[bos], Some(team))+ .await+ .unwrap();++ assert_eq!(first[0].status, ImportStatus::Importing);+ assert_eq!(first[0].team_id, Some(team), "Ana holds the claim");+ assert_eq!(second[0].status, ImportStatus::Importing);+ assert_eq!(second[0].team_id, None, "Bo writes nothing for the team");+}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn a_private_object_takes_no_claim(pool: PgPool) {+ let repo = PgImportRepo::new(pool.clone());+ let team = Uuid::now_v7();+ let ana = member(&pool, "private-ana", team).await;+ let anas = roadmap_plan(&repo, &ana).await;++ let claimed = repo+ .claim_importing(&ana, &[anas], &[], Some(team))+ .await+ .unwrap();++ assert_eq!(claimed[0].team_id, None);+ assert_eq!(+ repo.claim_team_object(&ana, anas, team, false)+ .await+ .unwrap(),+ TeamClaim::Free+ );+}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn a_teammate_waits_on_the_live_holder_and_is_linked_once_it_is_imported(pool: PgPool) {+ let repo = PgImportRepo::new(pool.clone());+ let team = Uuid::now_v7();+ let ana = member(&pool, "wait-ana", team).await;+ let bo = member(&pool, "wait-bo", team).await;+ let anas = roadmap_plan(&repo, &ana).await;+ let bos = roadmap_plan(&repo, &bo).await;+ writing(&repo, &ana, Duration::from_secs(90)).await;+ repo.claim_importing(&ana, &[anas], &[anas], Some(team))+ .await+ .unwrap();+ repo.claim_importing(&bo, &[bos], &[bos], Some(team))+ .await+ .unwrap();++ let claim = repo.claim_team_object(&bo, bos, team, true).await.unwrap();+ assert_eq!(claim, TeamClaim::Waiting { holder: anas });+ assert!(repo.wait_on(&bo, bos, anas).await.unwrap());+ repo.mark_imported(&ana, anas, "doc-roadmap", "md", None)+ .await+ .unwrap();++ let anas = repo.get(&ana, anas).await.unwrap().unwrap();+ assert_eq!(anas.team_id, Some(team), "the claim stays with the copy");+ let bos = repo.get(&bo, bos).await.unwrap().unwrap();+ assert_eq!(bos.status, ImportStatus::Imported);+ assert_eq!(bos.entity_id.as_deref(), Some("doc-roadmap"));+ assert_eq!(bos.waiting_on, None);+ assert_eq!(bos.team_id, None);+ assert_eq!(+ repo.team_copies(&bo, &[ROADMAP.to_string()])+ .await+ .unwrap()+ .len(),+ 1,+ "Ana's copy is the team's"+ );+}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn a_finished_copy_is_linked_and_a_stopped_holder_is_taken_over(pool: PgPool) {+ let repo = PgImportRepo::new(pool.clone());+ let team = Uuid::now_v7();+ let ana = member(&pool, "over-ana", team).await;+ let bo = member(&pool, "over-bo", team).await;+ let cy = member(&pool, "over-cy", team).await;+ let anas = roadmap_plan(&repo, &ana).await;+ let bos = roadmap_plan(&repo, &bo).await;+ let cys = roadmap_plan(&repo, &cy).await;+ // Ana's process died: her run's lease lapsed with her row still claimed.+ writing(&repo, &ana, Duration::ZERO).await;+ repo.claim_importing(&ana, &[anas], &[anas], Some(team))+ .await+ .unwrap();+ repo.claim_importing(&bo, &[bos], &[bos], Some(team))+ .await+ .unwrap();+ repo.claim_importing(&cy, &[cys], &[cys], Some(team))+ .await+ .unwrap();+ // Cy was already waiting on Ana.+ assert!(repo.wait_on(&cy, cys, anas).await.unwrap());++ let claim = repo.claim_team_object(&bo, bos, team, true).await.unwrap();++ assert_eq!(claim, TeamClaim::Held, "Bo takes the stopped claim over");+ assert_eq!(repo.get(&ana, anas).await.unwrap().unwrap().team_id, None);+ assert_eq!(+ repo.get(&cy, cys).await.unwrap().unwrap().waiting_on,+ Some(bos),+ "Cy now waits on Bo"+ );+ // Ana's write, resumed while Bo's is running, finds her claim gone and+ // waits on Bo instead of writing a second copy.+ writing(&repo, &bo, Duration::from_secs(90)).await;+ assert_eq!(+ repo.claim_team_object(&ana, anas, team, true)+ .await+ .unwrap(),+ TeamClaim::Waiting { holder: bos }+ );+}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn a_teammate_linking_a_finished_copy_never_claims_it(pool: PgPool) {+ let repo = PgImportRepo::new(pool.clone());+ let team = Uuid::now_v7();+ let ana = member(&pool, "link-ana", team).await;+ let bo = member(&pool, "link-bo", team).await;+ let anas = roadmap_plan(&repo, &ana).await;+ let bos = roadmap_plan(&repo, &bo).await;+ repo.claim_importing(&ana, &[anas], &[anas], Some(team))+ .await+ .unwrap();+ repo.mark_imported(&ana, anas, "doc-roadmap", "md", None)+ .await+ .unwrap();+ repo.claim_importing(&bo, &[bos], &[bos], Some(team))+ .await+ .unwrap();++ assert_eq!(+ repo.claim_team_object(&bo, bos, team, false).await.unwrap(),+ TeamClaim::Linked {+ entity_id: "doc-roadmap".to_string(),+ entity_type: "md".to_string(),+ }+ );+ assert!(+ !repo.wait_on(&bo, bos, anas).await.unwrap(),+ "nothing to wait for once it is imported"+ );+}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn a_holder_that_fails_gives_up_its_claim_and_releases_its_waiters(pool: PgPool) {+ let repo = PgImportRepo::new(pool.clone());+ let team = Uuid::now_v7();+ let ana = member(&pool, "fail-ana", team).await;+ let bo = member(&pool, "fail-bo", team).await;+ let anas = roadmap_plan(&repo, &ana).await;+ let bos = roadmap_plan(&repo, &bo).await;+ repo.claim_importing(&ana, &[anas], &[anas], Some(team))+ .await+ .unwrap();+ repo.claim_importing(&bo, &[bos], &[bos], Some(team))+ .await+ .unwrap();+ assert!(repo.wait_on(&bo, bos, anas).await.unwrap());++ assert!(+ repo.mark_import_failed(&ana, anas, "Notion request failed")+ .await+ .unwrap()+ );++ let anas = repo.get(&ana, anas).await.unwrap().unwrap();+ assert_eq!(anas.status, ImportStatus::Staged);+ assert_eq!(anas.team_id, None);+ let bos = repo.get(&bo, bos).await.unwrap().unwrap();+ assert_eq!(bos.status, ImportStatus::Staged);+ assert_eq!(bos.waiting_on, None);+ assert_eq!(bos.last_error.as_deref(), Some(TEAMMATE_STOPPED));+}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn a_waiting_row_outlives_its_own_write_until_the_holder_settles(pool: PgPool) {+ let repo = PgImportRepo::new(pool.clone());+ let team = Uuid::now_v7();+ let ana = member(&pool, "settle-ana", team).await;+ let bo = member(&pool, "settle-bo", team).await;+ let anas = roadmap_plan(&repo, &ana).await;+ let bos = roadmap_plan(&repo, &bo).await;+ repo.claim_importing(&ana, &[anas], &[anas], Some(team))+ .await+ .unwrap();+ repo.claim_importing(&bo, &[bos], &[bos], Some(team))+ .await+ .unwrap();+ assert!(repo.wait_on(&bo, bos, anas).await.unwrap());++ // Bo's run is over; his row waits on Ana's, which is still being written.+ assert_eq!(repo.settle_importing(&bo, "stopped").await.unwrap(), 0);+ // Ana's run is over too, without her row written: both go back.+ assert_eq!(repo.settle_importing(&ana, "stopped").await.unwrap(), 1);++ let bos = repo.get(&bo, bos).await.unwrap().unwrap();+ assert_eq!(bos.status, ImportStatus::Staged);+ assert_eq!(bos.last_error.as_deref(), Some(TEAMMATE_STOPPED));+}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn the_owners_sharing_changes_are_recorded_on_their_staged_rows(pool: PgPool) {+ let repo = PgImportRepo::new(pool.clone());+ let team = Uuid::now_v7();+ let ana = member(&pool, "share-ana", team).await;+ let anas = roadmap_plan(&repo, &ana).await;++ assert_eq!(repo.set_plan_sharing(&ana, &[], &[anas]).await.unwrap(), 1);+ let row = repo.get(&ana, anas).await.unwrap().unwrap();+ assert_eq!(row.metadata["plan"]["shared"], false);+ assert_eq!(row.metadata["title"], "Roadmap");++ assert_eq!(repo.set_plan_sharing(&ana, &[anas], &[]).await.unwrap(), 1);+ let row = repo.get(&ana, anas).await.unwrap().unwrap();+ assert_eq!(row.metadata["plan"]["shared"], true);+}diff --git a/crates/import/src/outbound/pg_import_repo/test.rs b/crates/import/src/outbound/pg_import_repo/test.rsindex a30ea65f35..dfd209fd45 100644--- a/crates/import/src/outbound/pg_import_repo/test.rs+++ b/crates/import/src/outbound/pg_import_repo/test.rs@@ -2,10 +2,9 @@ use super::*;use crate::domain::models::{ImportTargetKey, ImportTargetKind, SlackConversationId, SlackWorkspaceId,};-use crate::domain::ports::CanonicalImportRepo;+use crate::domain::ports::{CanonicalImportRepo, NotionPlanRepo, PlanRow};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 team_members_returns_only_the_requested_roster(pool: PgPool) {@@ -25,12 +24,12 @@ async fn team_members_returns_only_the_requested_roster(pool: PgPool) {}#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]-async fn manual_run_inserts_without_auto_import(pool: PgPool) {- let actor = ledger_actor(&pool, "manual-insert").await;+async fn run_inserts_running_without_an_error(pool: PgPool) {+ let actor = ledger_actor(&pool, "run-insert").await;let repo = PgImportRepo::new(pool);assert!(repo.list_runs(&actor).await.unwrap().is_empty());assert!(- repo.start_manual_run(&actor, ImportSource::Slack, &[])+ repo.start_run(&actor, ImportSource::Slack, &[]).await.unwrap());@@ -39,12 +38,11 @@ async fn manual_run_inserts_without_auto_import(pool: PgPool) {assert_eq!(runs.len(), 1);assert_eq!(runs[0].source, ImportSource::Slack);assert_eq!(runs[0].status, RunStatus::Running);- assert!(!runs[0].auto_import);assert!(runs[0].error.is_none());}#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]-async fn manual_run_clears_auto_import_and_error_only_when_claimed(pool: PgPool) {+async fn run_restarts_terminal_runs_and_clears_their_error(pool: PgPool) {let repo = PgImportRepo::new(pool.clone());let from = [RunStatus::Ready,@@ -55,7 +53,7 @@ async fn manual_run_clears_auto_import_and_error_only_when_claimed(pool: PgPool)for status in from {let actor = ledger_actor(&pool, status.as_ref()).await;assert!(- repo.start_run(&actor, ImportSource::Slack, &[], true)+ repo.start_run(&actor, ImportSource::Slack, &[]).await.unwrap());@@ -64,32 +62,22 @@ async fn manual_run_clears_auto_import_and_error_only_when_claimed(pool: PgPool).await.unwrap());- assert!(repo.list_runs(&actor).await.unwrap()[0].auto_import);assert!(- repo.start_manual_run(&actor, ImportSource::Slack, &from)+ repo.start_run(&actor, ImportSource::Slack, &from).await.unwrap());let runs = repo.list_runs(&actor).await.unwrap();assert_eq!(runs.len(), 1);assert_eq!(runs[0].status, RunStatus::Running);- assert!(!runs[0].auto_import);assert!(runs[0].error.is_none());-- // A subsequent initial gather must not re-enable automatic import.- assert!(- repo.start_run(&actor, ImportSource::Slack, &[RunStatus::Running], true)- .await- .unwrap()- );- assert!(!repo.list_runs(&actor).await.unwrap()[0].auto_import);}}#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]-async fn manual_run_does_not_take_over_active_runs(pool: PgPool) {- let actor = ledger_actor(&pool, "manual-active").await;+async fn run_does_not_take_over_a_running_run(pool: PgPool) {+ let actor = ledger_actor(&pool, "run-active").await;let repo = PgImportRepo::new(pool);let from = [RunStatus::Ready,@@ -98,31 +86,74 @@ async fn manual_run_does_not_take_over_active_runs(pool: PgPool) {RunStatus::Dismissed,];assert!(- repo.start_run(&actor, ImportSource::Slack, &[], true)+ repo.start_run(&actor, ImportSource::Slack, &[]).await.unwrap());- for status in [RunStatus::Running, RunStatus::Importing] {- if status == RunStatus::Importing {- assert!(- repo.transition_run(&actor, ImportSource::Slack, &[RunStatus::Running], status)- .await- .unwrap()- );- }- let before = repo.list_runs(&actor).await.unwrap().pop().unwrap();+ let before = repo.list_runs(&actor).await.unwrap().pop().unwrap();+ assert!(+ !repo+ .start_run(&actor, ImportSource::Slack, &from)+ .await+ .unwrap()+ );+ let after = repo.list_runs(&actor).await.unwrap().pop().unwrap();+ assert_eq!(after.status, RunStatus::Running);+ assert_eq!(after.updated_at, before.updated_at);+}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn runs_left_running_or_importing_are_reaped_once_stale(pool: PgPool) {+ let stale = ledger_actor(&pool, "stale-run").await;+ let legacy = ledger_actor(&pool, "legacy-run").await;+ let fresh = ledger_actor(&pool, "fresh-run").await;+ let repo = PgImportRepo::new(pool.clone());+ for actor in [&stale, &legacy, &fresh] {assert!(- !repo- .start_manual_run(&actor, ImportSource::Slack, &from)+ repo.start_run(actor, ImportSource::Slack, &[]).await.unwrap());- let after = repo.list_runs(&actor).await.unwrap().pop().unwrap();- assert_eq!(after.status, status);- assert!(after.auto_import);- assert_eq!(after.error, before.error);- assert_eq!(after.updated_at, before.updated_at);}+ sqlx::query!(+ "UPDATE import_run SET updated_at = NOW() - interval '1 hour' WHERE user_id = $1",+ stale.as_ref(),+ )+ .execute(&pool)+ .await+ .unwrap();+ sqlx::query!(+ r#"+ UPDATE import_run SET status = 'importing', updated_at = NOW() - interval '1 hour'+ WHERE user_id = $1+ "#,+ legacy.as_ref(),+ )+ .execute(&pool)+ .await+ .unwrap();++ for actor in [&stale, &legacy] {+ assert_eq!(repo.fail_stale_runs(actor, 600).await.unwrap(), 1);+ let run = repo.list_runs(actor).await.unwrap().pop().unwrap();+ assert_eq!(run.status, RunStatus::Failed);+ assert_eq!(+ run.error.as_deref(),+ Some("discovery was interrupted — try again")+ );+ // A reaped run is terminal, so discovery can start again.+ assert!(+ repo.start_run(actor, ImportSource::Slack, &[RunStatus::Failed])+ .await+ .unwrap()+ );+ }++ assert_eq!(repo.fail_stale_runs(&fresh, 600).await.unwrap(), 0);+ assert_eq!(+ repo.list_runs(&fresh).await.unwrap()[0].status,+ RunStatus::Running+ );}#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]@@ -134,6 +165,7 @@ async fn staged_manual_entity_round_trips(pool: PgPool) {.upsert_staged(&actor,ImportSource::Slack,+ ImportKind::Channel,Initiator::Manual,"C123",&metadata,@@ -230,208 +262,138 @@ async fn read_only_targets_are_exact_scoped_and_never_reserve(pool: PgPool) {);}-fn user() -> MacroUserIdStr<'static> {- MacroUserIdStr::parse_from_str("macro|[email protected]").expect("valid user id")-}--async fn ready_run(repo: &PgImportRepo, auto_import: bool) {- assert!(- repo.start_run(&user(), ImportSource::Linear, &[], auto_import)- .await- .expect("start run")- );- assert!(- repo.finish_run(&user(), ImportSource::Linear, RunStatus::Ready, None)- .await- .expect("finish gather")- );-}--async fn stage(- repo: &PgImportRepo,- source: ImportSource,- initiator: Initiator,- foreign_id: &str,-) -> ImportEntity {- let metadata = match source {- ImportSource::Linear => serde_json::json!({ "title": foreign_id }),- ImportSource::Notion => serde_json::json!({ "title": foreign_id }),- ImportSource::Slack => serde_json::json!({ "name": foreign_id }),- };- repo.upsert_staged(&user(), source, initiator, foreign_id, &metadata)- .await- .expect("stage query")- .expect("staged row")-}-#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]-async fn auto_import_claims_only_its_onboarding_source(pool: Pool<Postgres>) {- let repo = PgImportRepo::new(pool);- ready_run(&repo, true).await;-- let claimed = stage(&repo, ImportSource::Linear, Initiator::Onboarding, "LIN-1").await;- let chat = stage(&repo, ImportSource::Linear, Initiator::Chat, "LIN-CHAT").await;- let notion = stage(- &repo,- ImportSource::Notion,- Initiator::Onboarding,- "NOTION-1",- )- .await;-- assert_eq!(- repo.delete_staged_by_initiator(&user(), Initiator::Onboarding)- .await- .expect("onboarding cleanup"),- 1,- "only the unrelated Notion row should be removed"- );- assert!(- repo.get(&user(), notion.id)- .await- .expect("notion row lookup")- .is_none()- );-- let rows = repo- .begin_auto_import(&user(), ImportSource::Linear)+async fn rows_record_their_kind_and_legacy_rows_read_as_the_source_default(pool: PgPool) {+ let actor = ledger_actor(&pool, "kinds").await;+ let repo = PgImportRepo::new(pool.clone());+ let database = repo+ .upsert_staged(+ &actor,+ ImportSource::Notion,+ ImportKind::Database,+ Initiator::Chat,+ "0123456789abcdef0123456789abcdef",+ &serde_json::json!({"title": "Roadmap"}),+ ).await- .expect("begin auto import")- .expect("claimed run");- assert_eq!(rows.len(), 1);- assert_eq!(rows[0].id, claimed.id);- assert_eq!(rows[0].status, ImportStatus::Importing);+ .unwrap()+ .unwrap();+ assert_eq!(database.kind, ImportKind::Database);+ assert_eq!(database.parent_id, None);+ assert_eq!(database.review_id, None);+ // A replica still running code from before `kind` existed writes NULL.+ let legacy = sqlx::query_scalar!(+ r#"+ INSERT INTO import_entity (user_id, source, foreign_id, status, initiator, metadata)+ VALUES ($1, 'linear', 'ENG-1', 'staged', 'chat', '{"title": "Legacy"}')+ RETURNING id+ "#,+ actor.as_ref(),+ )+ .fetch_one(&pool)+ .await+ .unwrap();assert_eq!(- repo.get(&user(), chat.id)- .await- .expect("chat row")- .expect("chat row exists")- .status,- ImportStatus::Staged- );- assert!(- repo.begin_auto_import(&user(), ImportSource::Linear)- .await- .expect("second begin")- .is_none(),- "the run claim is a CAS"+ repo.get(&actor, legacy).await.unwrap().unwrap().kind,+ ImportKind::Issue);}#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]-async fn auto_import_run_reports_completed_or_failed(pool: Pool<Postgres>) {+async fn a_kind_from_another_source_is_rejected(pool: PgPool) {+ let actor = ledger_actor(&pool, "wrong-kind").await;let repo = PgImportRepo::new(pool);- ready_run(&repo, true).await;- let row = stage(&repo, ImportSource::Linear, Initiator::Onboarding, "LIN-1").await;- let claimed = repo- .begin_auto_import(&user(), ImportSource::Linear)- .await- .expect("begin")- .expect("claimed");- assert_eq!(claimed.len(), 1);- repo.mark_imported(&user(), row.id, "task-1", "task", None)- .await- .expect("mark imported")- .expect("updated row");-- assert_eq!(- repo.finish_auto_import(&user(), ImportSource::Linear, &[row.id])- .await- .expect("finish")- .expect("won finish"),- RunStatus::Completed- );- let run = repo- .list_runs(&user())- .await- .expect("runs")- .pop()- .expect("run");- assert_eq!(run.status, RunStatus::Completed);- assert!(run.auto_import);-- // A retry preserves the run's configuration and can report an item- // failure on its next automatic batch.- assert!(- repo.start_run(- &user(),- ImportSource::Linear,- &[RunStatus::Completed],- false,+ let result = repo+ .upsert_staged(+ &actor,+ ImportSource::Notion,+ ImportKind::Issue,+ Initiator::Chat,+ "0123456789abcdef0123456789abcdef",+ &serde_json::json!({"title": "Roadmap"}),)- .await- .expect("restart for test")- );- assert!(- repo.finish_run(&user(), ImportSource::Linear, RunStatus::Ready, None)- .await- .expect("ready again")- );- let failed_row = stage(&repo, ImportSource::Linear, Initiator::Onboarding, "LIN-2").await;- repo.begin_auto_import(&user(), ImportSource::Linear)- .await- .expect("begin failed batch")- .expect("claimed failed batch");+ .await;+ let error = result.expect_err("a Linear kind cannot describe a Notion row");assert!(- repo.mark_import_failed(&user(), failed_row.id, "creator failed")- .await- .expect("mark failed")- );- assert_eq!(- repo.finish_auto_import(&user(), ImportSource::Linear, &[failed_row.id])- .await- .expect("finish failed")- .expect("won failed finish"),- RunStatus::Failed- );- assert_eq!(- repo.delete_staged_by_initiator(&user(), Initiator::Onboarding)- .await- .expect("cleanup after failure"),- 0,- "a retryable automatic-import failure must survive onboarding cleanup"- );- assert!(- repo.get(&user(), failed_row.id)- .await- .expect("failed row lookup")- .is_some()+ error.to_string().contains("import_entity_kind_check"),+ "{error}");}#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]-async fn interrupted_auto_import_run_is_reconciled(pool: Pool<Postgres>) {+async fn a_team_keeps_one_copy_of_each_notion_object(pool: PgPool) {+ let owner = ledger_actor(&pool, "notion-owner").await;+ let teammate = ledger_actor(&pool, "notion-teammate").await;+ let team = ledger_team(&pool, &owner).await;+ join_ledger_team(&pool, &teammate, team).await;let repo = PgImportRepo::new(pool);- ready_run(&repo, true).await;- let row = stage(&repo, ImportSource::Linear, Initiator::Onboarding, "LIN-1").await;- repo.begin_auto_import(&user(), ImportSource::Linear)- .await- .expect("begin")- .expect("claimed");+ let page = "0123456789abcdef0123456789abcdef";++ repo.upsert_imported(+ &owner,+ ImportSource::Notion,+ ImportKind::Page,+ Initiator::Chat,+ page,+ &serde_json::json!({"title": "Handbook"}),+ "doc-1",+ "md",+ Some(team),+ )+ .await+ .unwrap();+ let duplicate = repo+ .upsert_imported(+ &teammate,+ ImportSource::Notion,+ ImportKind::Page,+ Initiator::Chat,+ page,+ &serde_json::json!({"title": "Handbook"}),+ "doc-2",+ "md",+ Some(team),+ )+ .await;+ let error = duplicate.expect_err("a teammate cannot import a second copy");assert!(- repo.mark_import_failed(&user(), row.id, "process interrupted")- .await- .expect("mark interrupted")+ error.to_string().contains("import_entity_team_object_key"),+ "{error}");- assert_eq!(- repo.reconcile_auto_import_runs(&user())- .await- .expect("reconcile"),- 1- );- let run = repo- .list_runs(&user())+ // Without a team scope each user keeps their own row, as before.+ repo.upsert_imported(+ &teammate,+ ImportSource::Notion,+ ImportKind::Page,+ Initiator::Chat,+ page,+ &serde_json::json!({"title": "Handbook"}),+ "doc-3",+ "md",+ None,+ )+ .await+ .unwrap();++ // Slack rows carry team_id but keep their own reservations, so teammates'+ // rows for one channel are not constrained.+ for actor in [&owner, &teammate] {+ repo.upsert_imported(+ actor,+ ImportSource::Slack,+ ImportKind::Channel,+ Initiator::Manual,+ "C0123456789",+ &serde_json::json!({"name": "general"}),+ "channel-1",+ "channel",+ Some(team),+ ).await- .expect("runs")- .pop()- .expect("run");- assert_eq!(run.status, RunStatus::Failed);- assert_eq!(- run.error.as_deref(),- Some("one or more automatic imports failed")- );+ .unwrap();+ }}async fn ledger_actor(pool: &PgPool, label: &str) -> MacroUserIdStr<'static> {@@ -517,7 +479,8 @@ async fn legacy_mapping(repo.upsert_imported(user,ImportSource::Slack,- Initiator::Onboarding,+ ImportKind::Channel,+ Initiator::Manual,key.foreign_id.as_str(),&serde_json::json!({"name": "Historical"}),&channel_id.to_string(),@@ -676,6 +639,7 @@ async fn conflicting_historical_rows_fail_closed(pool: PgPool) {repo.upsert_imported(&admin,ImportSource::Slack,+ ImportKind::Channel,Initiator::Chat,non_channel.foreign_id.as_str(),&serde_json::json!({}),@@ -702,7 +666,8 @@ async fn names_cannot_claim_slack_ids_and_pending_ids_cannot_be_replaced(pool: Prepo.upsert_imported(&admin,ImportSource::Slack,- Initiator::Onboarding,+ ImportKind::Channel,+ Initiator::Manual,"#C123",&serde_json::json!({"name": "C123", "channel_id": "C123"}),&channel.to_string(),@@ -849,6 +814,7 @@ async fn private_legacy_mapping_requires_authorized_claim(pool: PgPool) {repo.upsert_imported(&admin,ImportSource::Slack,+ ImportKind::Channel,Initiator::Chat,"G123",&serde_json::json!({}),@@ -932,3 +898,245 @@ async fn concurrent_different_workspace_bindings_have_one_winner(pool: PgPool) {assert_eq!(confirmed.workspace_id, known.workspace_id);assert!(confirmed.confirmed_unknown_at.is_some());}++fn plan_row(foreign_id: &str, parent: Option<&str>, title: &str) -> PlanRow {+ PlanRow {+ kind: ImportKind::Page,+ foreign_id: foreign_id.to_string(),+ parent_foreign_id: parent.map(str::to_string),+ metadata: serde_json::json!({+ "title": title,+ "url": null,+ "summary": null,+ "plan": { "decision": "copy", "path": [] }+ }),+ }+}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn a_new_plan_replaces_the_staged_one_links_parents_and_keeps_imported_rows(pool: PgPool) {+ let actor = ledger_actor(&pool, "plan-replace").await;+ let repo = PgImportRepo::new(pool);+ let first = repo+ .replace_plan(+ &actor,+ ImportSource::Notion,+ &[+ plan_row("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", None, "Engineering"),+ plan_row(+ "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",+ Some("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"),+ "Q3 plan",+ ),+ plan_row("cccccccccccccccccccccccccccccccc", None, "Dropped later"),+ ],+ )+ .await+ .unwrap();+ assert_eq!(first.len(), 3);+ let engineering = first[0].id;+ assert_eq!(first[1].parent_id, Some(engineering));++ // Engineering gets imported before the next plan.+ repo.mark_importing(&actor, &[engineering]).await.unwrap();+ repo.mark_imported(&actor, engineering, "doc-1", "md", None)+ .await+ .unwrap();++ let second = repo+ .replace_plan(+ &actor,+ ImportSource::Notion,+ &[+ plan_row("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", None, "Engineering"),+ plan_row(+ "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",+ Some("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"),+ "Q3 plan, renamed",+ ),+ ],+ )+ .await+ .unwrap();++ // The imported row stays imported and is not restaged; its child still+ // hangs under it.+ assert_eq!(second.len(), 1);+ assert_eq!(second[0].parent_id, Some(engineering));+ assert_eq!(second[0].metadata["title"], "Q3 plan, renamed");+ let all = repo+ .list(&actor, Some(ImportSource::Notion), None)+ .await+ .unwrap();+ assert_eq!(all.len(), 2, "the dropped row of the old plan is gone");+ assert!(+ all.iter()+ .any(|row| row.id == engineering && row.status == ImportStatus::Imported)+ );+}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn a_review_ties_only_the_staged_plan_and_runs_move_only_from_the_given_states(pool: PgPool) {+ let actor = ledger_actor(&pool, "plan-review").await;+ let repo = PgImportRepo::new(pool.clone());+ let staged = repo+ .replace_plan(+ &actor,+ ImportSource::Notion,+ &[plan_row(+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",+ None,+ "Engineering",+ )],+ )+ .await+ .unwrap();+ // A page staged one at a time is not part of the plan.+ repo.upsert_staged(+ &actor,+ ImportSource::Notion,+ ImportKind::Page,+ Initiator::Chat,+ "dddddddddddddddddddddddddddddddd",+ &serde_json::json!({ "title": "Single page", "url": null, "summary": null }),+ )+ .await+ .unwrap();+ let session = Uuid::now_v7();+ sqlx::query!(+ r#"INSERT INTO agent_session (id, owner_id, bot_id, harness, model, workspace, for_warming) VALUES ($1, $2, '00000000-0000-0000-0000-00000000a2a2', 'macro-inmem', 'test-model', '/workspace', false)"#,+ session,+ actor.as_ref(),+ )+ .execute(&pool)+ .await+ .unwrap();+ let review = Uuid::now_v7();+ sqlx::query!(+ r#"INSERT INTO agent_session_review (id, agent_session_id, record) VALUES ($1, $2, '{}'::jsonb)"#,+ review,+ session,+ )+ .execute(&pool)+ .await+ .unwrap();++ assert_eq!(+ repo.attach_review(+ &actor,+ ImportSource::Notion,+ review,+ agent_session::domain::model::AgentSessionId::new_from_uuid(session),+ )+ .await+ .unwrap(),+ 1+ );+ let tied = repo.get(&actor, staged[0].id).await.unwrap().unwrap();+ assert_eq!(tied.review_id, Some(review));++ assert!(+ repo.start_run(&actor, ImportSource::Notion, &[])+ .await+ .unwrap()+ );+ assert!(+ !repo+ .transition_run(+ &actor,+ ImportSource::Notion,+ &[RunStatus::Ready],+ RunStatus::Importing,+ None,+ )+ .await+ .unwrap(),+ "a running plan cannot start writing"+ );+ assert!(+ repo.transition_run(+ &actor,+ ImportSource::Notion,+ &[RunStatus::Running],+ RunStatus::Dismissed,+ Some("Cancelled"),+ )+ .await+ .unwrap()+ );+ let runs = repo.list_runs(&actor).await.unwrap();+ assert_eq!(runs[0].status, RunStatus::Dismissed);+ assert_eq!(runs[0].error.as_deref(), Some("Cancelled"));+}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn scan_counts_show_only_while_the_run_is_running_and_reset_on_restart(pool: PgPool) {+ let actor = ledger_actor(&pool, "run-scan").await;+ let repo = PgImportRepo::new(pool);+ assert!(+ !repo+ .record_scan(+ &actor,+ ImportSource::Notion,+ ScanCounts {+ pages: 1,+ data_sources: 1+ }+ )+ .await+ .unwrap()+ );++ repo.start_run(&actor, ImportSource::Notion, &[])+ .await+ .unwrap();+ assert_eq!(repo.list_runs(&actor).await.unwrap()[0].scanned, None);+ assert!(+ repo.record_scan(+ &actor,+ ImportSource::Notion,+ ScanCounts {+ pages: 40,+ data_sources: 12+ }+ )+ .await+ .unwrap()+ );+ assert_eq!(+ repo.list_runs(&actor).await.unwrap()[0].scanned,+ Some(ScanCounts {+ pages: 40,+ data_sources: 12+ })+ );++ repo.transition_run(+ &actor,+ ImportSource::Notion,+ &[RunStatus::Running],+ RunStatus::Ready,+ None,+ )+ .await+ .unwrap();+ assert_eq!(repo.list_runs(&actor).await.unwrap()[0].scanned, None);+ assert!(+ !repo+ .record_scan(+ &actor,+ ImportSource::Notion,+ ScanCounts {+ pages: 41,+ data_sources: 12+ }+ )+ .await+ .unwrap()+ );++ repo.start_run(&actor, ImportSource::Notion, &[RunStatus::Ready])+ .await+ .unwrap();+ assert_eq!(repo.list_runs(&actor).await.unwrap()[0].scanned, None);+}diff --git a/crates/import_notion/AGENTS.md b/crates/import_notion/AGENTS.mdnew file mode 100644index 0000000000..42a220c8bc--- /dev/null+++ b/crates/import_notion/AGENTS.md@@ -0,0 +1,56 @@+# import_notion++## What it is++Part of the Notion import feature. Reads a user's Notion workspace (pages,+data sources and their rows, members, page bodies as markdown) into a typed+model for the `import` crate to turn into Macro documents and databases.++## Start here++- `src/domain/model.rs` + `src/domain/ports.rs`: the contract (`NotionApi`,+ `NotionConnections`, `NotionError`).+- `src/domain/inventory.rs`: walking the workspace; scope, row draining,+ markdown splicing.+- `src/outbound/rest.rs`: requests, retries, pacing; `rest/wire.rs` maps+ Notion's wire shapes to the model.+- `src/outbound/pipedream.rs`: reaching Notion through Pipedream's Connect+ proxy with the user's `notion` connection.++## Scope++- Owns: reading Notion over REST (`Notion-Version: 2026-03-11`, data sources+ API).+- Not here: import planning, writing Macro entities → `crates/import`;+ Pipedream connections and the proxy port → `crates/pipedream_mcp`.++## Boundaries++- Depends on: `pipedream_mcp` (`ConnectionStore`, `ConnectedAppProxy`).+- Used by: `import`.++## Checks++- `cargo test -p import_notion`++## Gotchas++- Read-only by construction: the REST client can only send a `NotionRead`+ (GETs, plus the read POSTs `/v1/search` and `/v1/data_sources/{id}/query`);+ never add a variant that writes to the user's Notion.+- One `RequestPacer` per Pipedream account (~3 requests/second), shared by+ every client of it; a `Retry-After` holds every request on the account.+- `query_data_source` cursors are this crate's own JSON, not Notion's: past+ Notion's 10,000-row cap the query restarts from the last row's+ `created_time`, so drain with `inventory::all_rows`, which de-duplicates.+- `inventory` counts rows and samples titles from the search results (rows are+ pages there) and queries a data source only when search showed none of its+ rows; the ~3 requests/second pacing makes every extra request count.+- Decoding skips (and warns on) a single unreadable search/query item rather+ than failing the page; fixtures in `fixtures/` cover every property type.+- Unknown property and value types map to `Unsupported` / `Empty`, never an+ error.++## Related++- Rules: `.agents/rules/rust/architecture.md`, `.agents/rules/observability.md`diff --git a/crates/import_notion/CLAUDE.md b/crates/import_notion/CLAUDE.mdnew file mode 120000index 0000000000..47dc3e3d86--- /dev/null+++ b/crates/import_notion/CLAUDE.md@@ -0,0 +1 @@+AGENTS.md\ No newline at end of filediff --git a/crates/import_notion/Cargo.toml b/crates/import_notion/Cargo.tomlnew file mode 100644index 0000000000..43ded82537--- /dev/null+++ b/crates/import_notion/Cargo.toml@@ -0,0 +1,26 @@+[package]+name = "import_notion"+version = "0.1.0"+edition = "2024"+publish = false++[dependencies]+chrono = { workspace = true }+futures = { workspace = true }+http = { workspace = true }+indexmap = { workspace = true, features = ["serde"] }+lazy-regex = { workspace = true }+macro_user_id = { path = "../macro_user_id" }+pipedream_mcp = { path = "../pipedream_mcp" }+rand = { workspace = true }+rootcause = { workspace = true }+serde = { workspace = true }+serde_json = { workspace = true }+thiserror = { workspace = true }+tokio = { workspace = true, features = ["time"] }+tracing = { workspace = true }+workspace-hack = { version = "0.1", path = "../workspace-hack" }++[dev-dependencies]+anyhow = { workspace = true }+tokio = { workspace = true, features = ["test-util"] }diff --git a/crates/import_notion/fixtures/data_source.json b/crates/import_notion/fixtures/data_source.jsonnew file mode 100644index 0000000000..846fd48e56--- /dev/null+++ b/crates/import_notion/fixtures/data_source.json@@ -0,0 +1,32 @@+{+ "object": "data_source",+ "id": "1a44be12-0953-4631-a1d6-7d4c2b1b4a2f",+ "created_time": "2026-01-10T08:00:00.000Z",+ "last_edited_time": "2026-03-02T10:15:00.000Z",+ "title": [{"type": "text", "plain_text": "Roadmap"}],+ "description": [],+ "parent": {"type": "database_id", "database_id": "d9824bdc-8445-4327-be8b-5b47500af6ce"},+ "database_parent": {"type": "page_id", "page_id": "c7f0aa50-1f0e-4a52-9a17-e2f6f3a3b0c1"},+ "archived": false,+ "in_trash": false,+ "properties": {+ "Name": {"id": "title", "name": "Name", "type": "title", "title": {}},+ "Priority": {"id": "pR", "name": "Priority", "type": "select", "select": {"options": [+ {"id": "o1", "name": "High", "color": "red"},+ {"id": "o2", "name": "Low", "color": "green"}+ ]}},+ "Status": {"id": "sT", "name": "Status", "type": "status", "status": {+ "options": [{"id": "s1", "name": "In progress", "color": "blue"}],+ "groups": [{"id": "g1", "name": "In progress", "color": "blue", "option_ids": ["s1"]}]+ }},+ "Projects": {"id": "pJ", "name": "Projects", "type": "relation", "relation": {+ "database_id": "aaaa0000-0000-4000-8000-000000000000",+ "data_source_id": "bbbb0000-0000-4000-8000-000000000000",+ "type": "dual_property",+ "dual_property": {"synced_property_name": "Tasks", "synced_property_id": "xY"}+ }},+ "Ticket": {"id": "tK", "name": "Ticket", "type": "unique_id", "unique_id": {"prefix": "TASK"}},+ "Budget": {"id": "bU", "name": "Budget", "type": "number", "number": {"format": "dollar"}},+ "Run": {"id": "rN", "name": "Run", "type": "button", "button": {}}+ }+}diff --git a/crates/import_notion/fixtures/page.json b/crates/import_notion/fixtures/page.jsonnew file mode 100644index 0000000000..b487937280--- /dev/null+++ b/crates/import_notion/fixtures/page.json@@ -0,0 +1,58 @@+{+ "object": "page",+ "id": "59833787-2cf9-4fdf-8782-e53db20768a5",+ "created_time": "2026-03-01T09:30:00.000Z",+ "last_edited_time": "2026-03-02T10:15:00.000Z",+ "created_by": {"object": "user", "id": "ee5f0f84-409a-440f-983a-a5315961c6e4"},+ "last_edited_by": {"object": "user", "id": "ee5f0f84-409a-440f-983a-a5315961c6e4"},+ "cover": null,+ "icon": {"type": "emoji", "emoji": "🚀"},+ "parent": {+ "type": "data_source_id",+ "data_source_id": "1a44be12-0953-4631-a1d6-7d4c2b1b4a2f",+ "database_id": "d9824bdc-8445-4327-be8b-5b47500af6ce"+ },+ "archived": false,+ "in_trash": false,+ "url": "https://www.notion.so/Launch-plan-598337872cf94fdf8782e53db20768a5",+ "public_url": null,+ "properties": {+ "Name": {"id": "title", "type": "title", "title": [+ {"type": "text", "text": {"content": "Launch ", "link": null}, "plain_text": "Launch ", "href": null},+ {"type": "text", "text": {"content": "plan", "link": null}, "plain_text": "plan", "href": null}+ ]},+ "Notes": {"id": "%3AUPp", "type": "rich_text", "rich_text": [{"type": "text", "plain_text": "Ship it", "href": null}]},+ "Empty notes": {"id": "eN", "type": "rich_text", "rich_text": []},+ "Budget": {"id": "bU", "type": "number", "number": 1250.5},+ "No budget": {"id": "nB", "type": "number", "number": null},+ "Priority": {"id": "pR", "type": "select", "select": {"id": "o1", "name": "High", "color": "red"}},+ "No priority": {"id": "nP", "type": "select", "select": null},+ "Tags": {"id": "tG", "type": "multi_select", "multi_select": [{"id": "t1", "name": "web", "color": "blue"}, {"id": "t2", "name": "ios", "color": "gray"}]},+ "Status": {"id": "sT", "type": "status", "status": {"id": "s1", "name": "In progress", "color": "blue"}},+ "Due": {"id": "dU", "type": "date", "date": {"start": "2026-04-01", "end": "2026-04-03", "time_zone": null}},+ "Owner": {"id": "oW", "type": "people", "people": [{"object": "user", "id": "ee5f0f84-409a-440f-983a-a5315961c6e4"}]},+ "Attachments": {"id": "aT", "type": "files", "files": [+ {"name": "spec.pdf", "type": "file", "file": {"url": "https://prod-files.notion-static.com/spec.pdf", "expiry_time": "2026-03-02T11:15:00.000Z"}},+ {"name": "mock", "type": "external", "external": {"url": "https://example.com/mock.png"}}+ ]},+ "Done": {"id": "dN", "type": "checkbox", "checkbox": true},+ "Link": {"id": "lK", "type": "url", "url": "https://macro.com"},+ "Contact": {"id": "cT", "type": "email", "email": "[email protected]"},+ "Phone": {"id": "pH", "type": "phone_number", "phone_number": null},+ "Projects": {"id": "pJ", "type": "relation", "relation": [{"id": "a1b2c3d4-0000-4000-8000-000000000001"}], "has_more": false},+ "Score": {"id": "sC", "type": "formula", "formula": {"type": "number", "number": 42}},+ "Label": {"id": "lB", "type": "formula", "formula": {"type": "string", "string": "high-42"}},+ "Total": {"id": "tT", "type": "rollup", "rollup": {"type": "number", "number": 7, "function": "sum"}},+ "Project names": {"id": "pN", "type": "rollup", "rollup": {"type": "array", "function": "show_original", "array": [+ {"type": "title", "title": [{"plain_text": "Alpha"}]},+ {"type": "title", "title": []}+ ]}},+ "Created": {"id": "cR", "type": "created_time", "created_time": "2026-03-01T09:30:00.000Z"},+ "Created by": {"id": "cB", "type": "created_by", "created_by": {"object": "user", "id": "ee5f0f84-409a-440f-983a-a5315961c6e4"}},+ "Edited": {"id": "eD", "type": "last_edited_time", "last_edited_time": "2026-03-02T10:15:00.000Z"},+ "Edited by": {"id": "eB", "type": "last_edited_by", "last_edited_by": {"object": "user", "id": "ee5f0f84-409a-440f-983a-a5315961c6e4"}},+ "Ticket": {"id": "tK", "type": "unique_id", "unique_id": {"number": 42, "prefix": "TASK"}},+ "Run": {"id": "rN", "type": "button", "button": {}},+ "Verified": {"id": "vR", "type": "verification", "verification": {"state": "unverified"}}+ }+}diff --git a/crates/import_notion/src/domain.rs b/crates/import_notion/src/domain.rsnew file mode 100644index 0000000000..cea9ab8f7a--- /dev/null+++ b/crates/import_notion/src/domain.rs@@ -0,0 +1,5 @@+//! The importer's view of Notion.++pub mod inventory;+pub mod model;+pub mod ports;diff --git a/crates/import_notion/src/domain/inventory.rs b/crates/import_notion/src/domain/inventory.rsnew file mode 100644index 0000000000..2d7ceab3cd--- /dev/null+++ b/crates/import_notion/src/domain/inventory.rs@@ -0,0 +1,437 @@+//! Walking a Notion workspace: everything in scope, every row of a data+//! source, every member, and a page's whole body.++use std::collections::{HashMap, HashSet};++use futures::{StreamExt, TryStreamExt, stream};+use lazy_regex::regex;++use super::model::{+ DataSourceSummary, ImportScope, Inventory, InventoryProgress, NotionDataSource, NotionId,+ NotionPage, NotionParent, NotionUser, SearchHit, SearchObject,+};+use super::ports::{NotionApi, NotionError};++/// How many data sources are sampled at once.+const SAMPLE_CONCURRENCY: usize = 4;++/// How many row titles a summary keeps.+const SAMPLE_TITLES: usize = 5;++/// How many rounds of unknown blocks [`full_markdown`] follows.+const MARKDOWN_DEPTH: u8 = 8;++/// How many unknown blocks are fetched at once.+const MARKDOWN_CONCURRENCY: usize = 4;++/// Everything in `scope`: standalone pages (rows and trashed objects+/// excluded) and data sources with their rows counted.+///+/// `progress` is called as the counts grow, and once more with the final,+/// scoped counts. Roots that are not shared with the connection are skipped,+/// as are data sources whose rows cannot be read. Rows are counted from the+/// search results; a data source is queried only when search showed none+/// of its rows.+#[tracing::instrument(skip_all, err, fields(pages, data_sources, queried_data_sources))]+pub async fn inventory<Api: NotionApi>(+ api: &Api,+ scope: &ImportScope,+ progress: &(dyn Fn(InventoryProgress) + Send + Sync),+) -> Result<Inventory, NotionError> {+ // Every live page, rows included: rows are not documents, but pages+ // nested under a row walk up through it.+ let mut pages: Vec<NotionPage> = Vec::new();+ let mut data_sources: Vec<NotionDataSource> = Vec::new();+ let mut documents = 0;++ for object in [SearchObject::Page, SearchObject::DataSource] {+ let mut cursor = None;+ loop {+ let page = api.search(object, cursor).await?;+ for hit in page.results {+ match hit {+ SearchHit::Page(page) if !page.in_trash => {+ if !is_row(&page) {+ documents += 1;+ }+ pages.push(page);+ }+ SearchHit::DataSource(source) if !source.in_trash => data_sources.push(source),+ SearchHit::Page(_) | SearchHit::DataSource(_) => {}+ }+ }+ progress(InventoryProgress {+ pages: documents,+ data_sources: data_sources.len(),+ });+ match page.next_cursor {+ Some(next) => cursor = Some(next),+ None => break,+ }+ }+ }+ dedupe_pages(&mut pages);+ dedupe_sources(&mut data_sources);+ // Search lists every row as a page, so rows are counted from it; only a+ // data source search did not show rows for is queried.+ let searched_rows = rows_by_data_source(&pages, &data_sources);++ if let ImportScope::Roots { ids } = scope {+ let roots: HashSet<NotionId> = ids.iter().cloned().collect();+ add_missing_roots(api, &roots, &mut pages, &mut data_sources).await?;+ let tree = Tree::new(&pages, &data_sources);+ pages.retain(|page| tree.is_under(&page.id, &roots));+ data_sources.retain(|source| tree.is_under(&source.id, &roots));+ }+ pages.retain(|page| !is_row(page));++ tracing::Span::current().record(+ "queried_data_sources",+ data_sources+ .iter()+ .filter(|source| !searched_rows.contains_key(&source.id))+ .count(),+ );+ let summaries: Vec<Option<DataSourceSummary>> = stream::iter(data_sources)+ .map(|source| {+ let rows = searched_rows.get(&source.id);+ async move {+ match rows {+ Some(titles) => Ok(Some(DataSourceSummary {+ row_count: titles.len(),+ sample_titles: titles.iter().take(SAMPLE_TITLES).cloned().collect(),+ source,+ })),+ None => summarize(api, source).await,+ }+ }+ })+ .buffered(SAMPLE_CONCURRENCY)+ .try_collect()+ .await?;+ let data_sources: Vec<DataSourceSummary> = summaries.into_iter().flatten().collect();++ progress(InventoryProgress {+ pages: pages.len(),+ data_sources: data_sources.len(),+ });+ let span = tracing::Span::current();+ span.record("pages", pages.len());+ span.record("data_sources", data_sources.len());+ Ok(Inventory {+ pages,+ data_sources,+ })+}++/// Every row of a data source, oldest first, each once.+#[tracing::instrument(skip(api), err, fields(rows))]+pub async fn all_rows<Api: NotionApi>(+ api: &Api,+ data_source: &NotionId,+) -> Result<Vec<NotionPage>, NotionError> {+ let mut rows: Vec<NotionPage> = Vec::new();+ let mut seen: HashSet<NotionId> = HashSet::new();+ let mut cursor = None;+ loop {+ let page = api.query_data_source(data_source, cursor).await?;+ // A query that restarts past Notion's result cap repeats the rows+ // created in the same instant as the last one it returned.+ rows.extend(+ page.results+ .into_iter()+ .filter(|row| seen.insert(row.id.clone())),+ );+ match page.next_cursor {+ Some(next) => cursor = Some(next),+ None => break,+ }+ }+ tracing::Span::current().record("rows", rows.len());+ Ok(rows)+}++/// Every workspace member the connection may list, bots excluded.+#[tracing::instrument(skip(api), err, fields(users))]+pub async fn all_users<Api: NotionApi>(api: &Api) -> Result<Vec<NotionUser>, NotionError> {+ let mut users = Vec::new();+ let mut cursor = None;+ loop {+ let page = api.users(cursor).await?;+ users.extend(page.results);+ match page.next_cursor {+ Some(next) => cursor = Some(next),+ None => break,+ }+ }+ tracing::Span::current().record("users", users.len());+ Ok(users)+}++/// A page's whole body as markdown, with the subtrees Notion left out of a+/// large page fetched and spliced back in.+///+/// Each left-out block replaces the `<unknown ...>` placeholder that names+/// it, or is appended when no placeholder does. Blocks the connection cannot+/// read keep their placeholder.+#[tracing::instrument(skip(api), err, fields(unknown_blocks))]+pub async fn full_markdown<Api: NotionApi>(+ api: &Api,+ page: &NotionId,+) -> Result<String, NotionError> {+ let root = api.page_markdown(page).await?;+ let mut markdown = root.markdown;+ let mut seen: HashSet<NotionId> = HashSet::from([page.clone()]);+ let mut pending = root.unknown_block_ids;+ let mut fetched = 0;++ for _ in 0..MARKDOWN_DEPTH {+ pending.retain(|id| seen.insert(id.clone()));+ if pending.is_empty() {+ break;+ }+ fetched += pending.len();+ let blocks: Vec<(NotionId, Option<_>)> = stream::iter(std::mem::take(&mut pending))+ .map(|id| async move {+ match api.page_markdown(&id).await {+ Ok(block) => Ok((id, Some(block))),+ // Not readable as markdown: unshared, gone, or not a page+ // at all (an embedded database answers 400). The page+ // keeps the placeholder rather than failing outright.+ Err(+ NotionError::NotFound+ | NotionError::Forbidden+ | NotionError::Upstream { status: 400, .. },+ ) => {+ tracing::warn!(block = %id, "unknown block is not readable; keeping its placeholder");+ Ok((id, None))+ }+ Err(error) => Err(error),+ }+ })+ .buffered(MARKDOWN_CONCURRENCY)+ .try_collect()+ .await?;+ for (id, block) in blocks {+ let Some(block) = block else { continue };+ markdown = splice(&markdown, &id, &block.markdown);+ pending.extend(block.unknown_block_ids);+ }+ }+ if !pending.is_empty() {+ tracing::warn!(+ remaining = pending.len(),+ "page nests unknown blocks deeper than the importer follows"+ );+ }+ tracing::Span::current().record("unknown_blocks", fetched);+ Ok(markdown)+}++/// `markdown` with the placeholder naming `id` replaced by `block`, or with+/// `block` appended when no placeholder names it.+fn splice(markdown: &str, id: &NotionId, block: &str) -> String {+ // The placeholder names its block somewhere in its attributes, with or+ // without dashes.+ let placeholder = regex!(r"<unknown\b[^>]*>").find_iter(markdown).find(|tag| {+ regex!(r"(?i)[0-9a-f]{8}-?[0-9a-f]{4}-?[0-9a-f]{4}-?[0-9a-f]{4}-?[0-9a-f]{12}")+ .find_iter(tag.as_str())+ .any(|found| NotionId::parse(found.as_str()).as_ref() == Some(id))+ });+ match placeholder {+ Some(tag) => format!(+ "{}{}{}",+ &markdown[..tag.start()],+ block.trim_end(),+ &markdown[tag.end()..]+ ),+ None if markdown.is_empty() => block.to_owned(),+ None => format!("{}\n\n{}", markdown.trim_end(), block),+ }+}++/// The titles of the rows search listed, by the data source they belong to,+/// for each data source search listed with at least one row. A data source+/// search did not list (a root fetched by id), or listed without rows, is+/// left out: its rows have to be queried.+fn rows_by_data_source(+ pages: &[NotionPage],+ data_sources: &[NotionDataSource],+) -> HashMap<NotionId, Vec<String>> {+ let searched: HashSet<&NotionId> = data_sources.iter().map(|source| &source.id).collect();+ let mut rows: HashMap<NotionId, Vec<String>> = HashMap::new();+ for page in pages {+ if let NotionParent::DataSource { data_source, .. } = &page.parent+ && searched.contains(data_source)+ {+ rows.entry(data_source.clone())+ .or_default()+ .push(page.title.clone());+ }+ }+ rows+}++fn is_row(page: &NotionPage) -> bool {+ matches!(page.parent, NotionParent::DataSource { .. })+}++fn dedupe_pages(pages: &mut Vec<NotionPage>) {+ let mut seen = HashSet::new();+ pages.retain(|page| seen.insert(page.id.clone()));+}++fn dedupe_sources(sources: &mut Vec<NotionDataSource>) {+ let mut seen = HashSet::new();+ sources.retain(|source| seen.insert(source.id.clone()));+}++/// Fetch roots the search did not return: a database container's data+/// sources, a data source, or a page.+async fn add_missing_roots<Api: NotionApi>(+ api: &Api,+ roots: &HashSet<NotionId>,+ pages: &mut Vec<NotionPage>,+ data_sources: &mut Vec<NotionDataSource>,+) -> Result<(), NotionError> {+ for root in roots {+ let known = pages.iter().any(|page| &page.id == root)+ || data_sources+ .iter()+ .any(|source| &source.id == root || &source.database_id == root);+ if known {+ continue;+ }+ match api.database_sources(root).await {+ Ok(sources) => {+ for id in sources {+ if data_sources.iter().any(|source| source.id == id) {+ continue;+ }+ match api.data_source(&id).await {+ Ok(source) if !source.in_trash => data_sources.push(source),+ Ok(_) => {}+ Err(NotionError::NotFound | NotionError::Forbidden) => {+ tracing::warn!(data_source = %id, "root's data source is not readable; skipping it");+ }+ Err(error) => return Err(error),+ }+ }+ continue;+ }+ Err(NotionError::NotFound | NotionError::Forbidden) => {}+ Err(error) => return Err(error),+ }+ match api.data_source(root).await {+ Ok(source) => {+ if !source.in_trash {+ data_sources.push(source);+ }+ continue;+ }+ Err(NotionError::NotFound | NotionError::Forbidden) => {}+ Err(error) => return Err(error),+ }+ match api.page(root).await {+ Ok(page) if !page.in_trash => pages.push(page),+ Ok(_) => {}+ Err(NotionError::NotFound | NotionError::Forbidden) => {+ tracing::warn!(root = %root, "import root is not shared with the connection; skipping it");+ }+ Err(error) => return Err(error),+ }+ }+ Ok(())+}++/// Parent links between the collected objects.+struct Tree {+ parents: HashMap<NotionId, NotionId>,+}++impl Tree {+ fn new(pages: &[NotionPage], data_sources: &[NotionDataSource]) -> Self {+ let mut parents = HashMap::new();+ for page in pages {+ if let Some(parent) = parent_id(&page.parent) {+ parents.insert(page.id.clone(), parent);+ }+ }+ for source in data_sources {+ parents.insert(source.id.clone(), source.database_id.clone());+ if let Some(parent) = parent_id(&source.database_parent) {+ parents.insert(source.database_id.clone(), parent);+ }+ }+ Self { parents }+ }++ /// Whether `id` is a root or sits anywhere under one.+ fn is_under(&self, id: &NotionId, roots: &HashSet<NotionId>) -> bool {+ let mut visited = HashSet::new();+ let mut current = id;+ loop {+ if roots.contains(current) {+ return true;+ }+ if !visited.insert(current) {+ return false;+ }+ match self.parents.get(current) {+ Some(parent) => current = parent,+ None => return false,+ }+ }+ }+}++fn parent_id(parent: &NotionParent) -> Option<NotionId> {+ match parent {+ NotionParent::Page { id } | NotionParent::Database { id } | NotionParent::Block { id } => {+ Some(id.clone())+ }+ NotionParent::DataSource { data_source, .. } => Some(data_source.clone()),+ NotionParent::Workspace | NotionParent::Other => None,+ }+}++/// A data source with its rows counted, or `None` when its rows+/// cannot be read.+async fn summarize<Api: NotionApi>(+ api: &Api,+ source: NotionDataSource,+) -> Result<Option<DataSourceSummary>, NotionError> {+ // Every row is counted, so the plan can say how much it will write.+ let mut row_count = 0;+ let mut sample_titles = Vec::new();+ let mut cursor = None;+ loop {+ let page = match api.query_data_source(&source.id, cursor).await {+ Ok(page) => page,+ Err(NotionError::NotFound | NotionError::Forbidden) => {+ tracing::warn!(data_source = %source.id, "data source rows are not readable; leaving it out");+ return Ok(None);+ }+ Err(error) => return Err(error),+ };+ row_count += page.results.len();+ sample_titles.extend(+ page.results+ .iter()+ .take(SAMPLE_TITLES.saturating_sub(sample_titles.len()))+ .map(|row| row.title.clone()),+ );+ match page.next_cursor {+ Some(next) => cursor = Some(next),+ None => break,+ }+ }+ Ok(Some(DataSourceSummary {+ row_count,+ sample_titles,+ source,+ }))+}++#[cfg(test)]+mod test;diff --git a/crates/import_notion/src/domain/inventory/test.rs b/crates/import_notion/src/domain/inventory/test.rsnew file mode 100644index 0000000000..96003e402c--- /dev/null+++ b/crates/import_notion/src/domain/inventory/test.rs@@ -0,0 +1,668 @@+use std::collections::{BTreeMap, HashMap};+use std::sync::Mutex;++use chrono::{TimeZone, Utc};++use super::{full_markdown, inventory};+use crate::domain::model::{+ DataSourceSummary, ImportScope, Inventory, InventoryProgress, NotionDataSource, NotionId,+ NotionPage, NotionParent, NotionUser, PageMarkdown, Paged, SearchHit, SearchObject,+};+use crate::domain::ports::{NotionApi, NotionError};++/// A workspace held in memory. Searches return everything on one page.+#[derive(Default)]+struct FakeWorkspace {+ pages: Vec<NotionPage>,+ data_sources: Vec<NotionDataSource>,+ /// Data sources a database container holds, by database id.+ databases: HashMap<NotionId, Vec<NotionId>>,+ /// Rows by data source id; the first page of a query.+ rows: HashMap<NotionId, Paged<NotionPage>>,+ /// The page after the first, by data source id, for any cursor.+ later_rows: HashMap<NotionId, Vec<NotionPage>>,+ markdown: HashMap<NotionId, PageMarkdown>,+ /// Ids that are databases, which Notion refuses to render as markdown.+ databases_not_pages: Vec<NotionId>,+ markdown_requests: Mutex<Vec<NotionId>>,+ /// Every search, by object type.+ searches: Mutex<Vec<SearchObject>>,+ /// Every data source query, by data source id.+ queries: Mutex<Vec<NotionId>>,+}++impl NotionApi for FakeWorkspace {+ async fn search(+ &self,+ object: SearchObject,+ _cursor: Option<String>,+ ) -> Result<Paged<SearchHit>, NotionError> {+ self.searches.lock().unwrap().push(object);+ let results = match object {+ SearchObject::Page => self.pages.iter().cloned().map(SearchHit::Page).collect(),+ SearchObject::DataSource => self+ .data_sources+ .iter()+ .cloned()+ .map(SearchHit::DataSource)+ .collect(),+ };+ Ok(Paged {+ results,+ next_cursor: None,+ })+ }++ async fn page(&self, id: &NotionId) -> Result<NotionPage, NotionError> {+ self.pages+ .iter()+ .find(|page| &page.id == id)+ .cloned()+ .ok_or(NotionError::NotFound)+ }++ async fn page_markdown(&self, id: &NotionId) -> Result<PageMarkdown, NotionError> {+ self.markdown_requests.lock().unwrap().push(id.clone());+ if self.databases_not_pages.contains(id) {+ return Err(NotionError::Upstream {+ status: 400,+ body: "Provided ID is a database, not a page.".into(),+ });+ }+ self.markdown.get(id).cloned().ok_or(NotionError::NotFound)+ }++ async fn data_source(&self, id: &NotionId) -> Result<NotionDataSource, NotionError> {+ self.data_sources+ .iter()+ .find(|source| &source.id == id)+ .cloned()+ .ok_or(NotionError::NotFound)+ }++ async fn database_sources(&self, database: &NotionId) -> Result<Vec<NotionId>, NotionError> {+ self.databases+ .get(database)+ .cloned()+ .ok_or(NotionError::NotFound)+ }++ async fn query_data_source(+ &self,+ id: &NotionId,+ cursor: Option<String>,+ ) -> Result<Paged<NotionPage>, NotionError> {+ self.queries.lock().unwrap().push(id.clone());+ if cursor.is_some() {+ return Ok(Paged {+ results: self.later_rows.get(id).cloned().unwrap_or_default(),+ next_cursor: None,+ });+ }+ Ok(self.rows.get(id).cloned().unwrap_or(Paged {+ results: Vec::new(),+ next_cursor: None,+ }))+ }++ async fn users(&self, _cursor: Option<String>) -> Result<Paged<NotionUser>, NotionError> {+ Ok(Paged {+ results: Vec::new(),+ next_cursor: None,+ })+ }+}++#[tokio::test]+async fn everything_lists_documents_and_data_sources_with_rows_counted_from_search() {+ let plan = NotionPage {+ id: NotionId::parse("598337872cf94fdf8782e53db20768a5").unwrap(),+ title: "Q3 plan".into(),+ url: "https://www.notion.so/598337872cf94fdf8782e53db20768a5".into(),+ parent: NotionParent::Workspace,+ last_edited: Utc.with_ymd_and_hms(2026, 3, 2, 0, 0, 0).unwrap(),+ created: Utc.with_ymd_and_hms(2026, 3, 1, 0, 0, 0).unwrap(),+ in_trash: false,+ properties: BTreeMap::new(),+ };+ let trashed = NotionPage {+ id: NotionId::parse("c7f0aa501f0e4a529a17e2f6f3a3b0c1").unwrap(),+ title: "Old notes".into(),+ url: "https://www.notion.so/c7f0aa501f0e4a529a17e2f6f3a3b0c1".into(),+ parent: NotionParent::Workspace,+ last_edited: Utc.with_ymd_and_hms(2026, 1, 2, 0, 0, 0).unwrap(),+ created: Utc.with_ymd_and_hms(2026, 1, 1, 0, 0, 0).unwrap(),+ in_trash: true,+ properties: BTreeMap::new(),+ };+ let row = NotionPage {+ id: NotionId::parse("ee5f0f84409a440f983aa5315961c6e4").unwrap(),+ title: "Ship onboarding".into(),+ url: "https://www.notion.so/ee5f0f84409a440f983aa5315961c6e4".into(),+ parent: NotionParent::DataSource {+ data_source: NotionId::parse("1a44be1209534631a1d67d4c2b1b4a2f").unwrap(),+ database: NotionId::parse("d9824bdc84454327be8b5b47500af6ce").unwrap(),+ },+ last_edited: Utc.with_ymd_and_hms(2026, 2, 2, 0, 0, 0).unwrap(),+ created: Utc.with_ymd_and_hms(2026, 2, 1, 0, 0, 0).unwrap(),+ in_trash: false,+ properties: BTreeMap::new(),+ };+ let roadmap = NotionDataSource {+ id: NotionId::parse("1a44be1209534631a1d67d4c2b1b4a2f").unwrap(),+ database_id: NotionId::parse("d9824bdc84454327be8b5b47500af6ce").unwrap(),+ title: "Roadmap".into(),+ database_parent: NotionParent::Page {+ id: NotionId::parse("598337872cf94fdf8782e53db20768a5").unwrap(),+ },+ in_trash: false,+ properties: vec![],+ };+ let workspace = FakeWorkspace {+ pages: vec![plan.clone(), trashed, row.clone()],+ data_sources: vec![roadmap.clone()],+ rows: HashMap::from([(+ roadmap.id.clone(),+ Paged {+ results: vec![row.clone()],+ next_cursor: Some("more".into()),+ },+ )]),+ later_rows: HashMap::from([(+ roadmap.id.clone(),+ vec![NotionPage {+ id: NotionId::parse("7d0c5bd5b4a04f3a9c7d0c5bd5b4a04f").unwrap(),+ title: "Ship billing".into(),+ ..row.clone()+ }],+ )]),+ ..FakeWorkspace::default()+ };+ let reports = Mutex::new(Vec::new());++ let found = inventory(&workspace, &ImportScope::Everything, &|progress| {+ reports.lock().unwrap().push(progress)+ })+ .await+ .unwrap();++ // Search listed the roadmap's one row, so its rows are not queried+ // (a query would have found a second).+ assert_eq!(+ found,+ Inventory {+ pages: vec![plan],+ data_sources: vec![DataSourceSummary {+ source: roadmap,+ row_count: 1,+ sample_titles: vec!["Ship onboarding".into()],+ }],+ }+ );+ assert_eq!(*workspace.queries.lock().unwrap(), Vec::<NotionId>::new());+ assert_eq!(+ reports.into_inner().unwrap(),+ vec![+ InventoryProgress {+ pages: 1,+ data_sources: 0,+ },+ InventoryProgress {+ pages: 1,+ data_sources: 1,+ },+ InventoryProgress {+ pages: 1,+ data_sources: 1,+ },+ ]+ );+}++#[tokio::test]+async fn roots_keep_their_descendants_through_rows_and_databases() {+ // root page+ // └ database d98… / data source 1a4… (Roadmap)+ // └ row ee5…+ // └ page under the row 9a3…+ // └ column block b10…+ // └ page in a column a1b…+ // unrelated page c7f…+ let root = NotionPage {+ id: NotionId::parse("598337872cf94fdf8782e53db20768a5").unwrap(),+ title: "Team".into(),+ url: "https://www.notion.so/598337872cf94fdf8782e53db20768a5".into(),+ parent: NotionParent::Workspace,+ last_edited: Utc.with_ymd_and_hms(2026, 3, 2, 0, 0, 0).unwrap(),+ created: Utc.with_ymd_and_hms(2026, 3, 1, 0, 0, 0).unwrap(),+ in_trash: false,+ properties: BTreeMap::new(),+ };+ let row = NotionPage {+ id: NotionId::parse("ee5f0f84409a440f983aa5315961c6e4").unwrap(),+ title: "Ship onboarding".into(),+ url: "https://www.notion.so/ee5f0f84409a440f983aa5315961c6e4".into(),+ parent: NotionParent::DataSource {+ data_source: NotionId::parse("1a44be1209534631a1d67d4c2b1b4a2f").unwrap(),+ database: NotionId::parse("d9824bdc84454327be8b5b47500af6ce").unwrap(),+ },+ last_edited: Utc.with_ymd_and_hms(2026, 2, 2, 0, 0, 0).unwrap(),+ created: Utc.with_ymd_and_hms(2026, 2, 1, 0, 0, 0).unwrap(),+ in_trash: false,+ properties: BTreeMap::new(),+ };+ let under_row = NotionPage {+ id: NotionId::parse("9a3b5ae0c6e6482db0e1ed315ee6dc57").unwrap(),+ title: "Onboarding spec".into(),+ url: "https://www.notion.so/9a3b5ae0c6e6482db0e1ed315ee6dc57".into(),+ parent: NotionParent::Page {+ id: NotionId::parse("ee5f0f84409a440f983aa5315961c6e4").unwrap(),+ },+ last_edited: Utc.with_ymd_and_hms(2026, 2, 3, 0, 0, 0).unwrap(),+ created: Utc.with_ymd_and_hms(2026, 2, 3, 0, 0, 0).unwrap(),+ in_trash: false,+ properties: BTreeMap::new(),+ };+ let in_column = NotionPage {+ id: NotionId::parse("a1b2c3d4000040008000000000000001").unwrap(),+ title: "Column page".into(),+ url: "https://www.notion.so/a1b2c3d4000040008000000000000001".into(),+ parent: NotionParent::Block {+ id: NotionId::parse("b1000000000040008000000000000000").unwrap(),+ },+ last_edited: Utc.with_ymd_and_hms(2026, 2, 4, 0, 0, 0).unwrap(),+ created: Utc.with_ymd_and_hms(2026, 2, 4, 0, 0, 0).unwrap(),+ in_trash: false,+ properties: BTreeMap::new(),+ };+ let unrelated = NotionPage {+ id: NotionId::parse("c7f0aa501f0e4a529a17e2f6f3a3b0c1").unwrap(),+ title: "Elsewhere".into(),+ url: "https://www.notion.so/c7f0aa501f0e4a529a17e2f6f3a3b0c1".into(),+ parent: NotionParent::Workspace,+ last_edited: Utc.with_ymd_and_hms(2026, 1, 2, 0, 0, 0).unwrap(),+ created: Utc.with_ymd_and_hms(2026, 1, 1, 0, 0, 0).unwrap(),+ in_trash: false,+ properties: BTreeMap::new(),+ };+ let roadmap = NotionDataSource {+ id: NotionId::parse("1a44be1209534631a1d67d4c2b1b4a2f").unwrap(),+ database_id: NotionId::parse("d9824bdc84454327be8b5b47500af6ce").unwrap(),+ title: "Roadmap".into(),+ database_parent: NotionParent::Page {+ id: NotionId::parse("598337872cf94fdf8782e53db20768a5").unwrap(),+ },+ in_trash: false,+ properties: vec![],+ };+ let workspace = FakeWorkspace {+ pages: vec![+ root.clone(),+ row,+ under_row.clone(),+ in_column.clone(),+ unrelated,+ ],+ data_sources: vec![roadmap.clone()],+ ..FakeWorkspace::default()+ };++ let found = inventory(+ &workspace,+ &ImportScope::Roots {+ ids: vec![root.id.clone()],+ },+ &|_| {},+ )+ .await+ .unwrap();++ // The column block's owning page is not knowable from search results.+ let titles: Vec<&str> = found.pages.iter().map(|page| page.title.as_str()).collect();+ assert_eq!(titles, vec!["Team", "Onboarding spec"]);+ assert_eq!(+ found.data_sources,+ vec![DataSourceSummary {+ source: roadmap,+ row_count: 1,+ sample_titles: vec!["Ship onboarding".into()],+ }]+ );+}++#[tokio::test]+async fn a_database_root_missing_from_search_is_expanded_into_its_data_sources() {+ let tasks = NotionDataSource {+ id: NotionId::parse("1a44be1209534631a1d67d4c2b1b4a2f").unwrap(),+ database_id: NotionId::parse("d9824bdc84454327be8b5b47500af6ce").unwrap(),+ title: "Tasks".into(),+ database_parent: NotionParent::Workspace,+ in_trash: false,+ properties: vec![],+ };+ let archive = NotionDataSource {+ id: NotionId::parse("ee5f0f84409a440f983aa5315961c6e4").unwrap(),+ database_id: NotionId::parse("d9824bdc84454327be8b5b47500af6ce").unwrap(),+ title: "Archive".into(),+ database_parent: NotionParent::Workspace,+ in_trash: false,+ properties: vec![],+ };+ let other = NotionDataSource {+ id: NotionId::parse("c7f0aa501f0e4a529a17e2f6f3a3b0c1").unwrap(),+ database_id: NotionId::parse("9a3b5ae0c6e6482db0e1ed315ee6dc57").unwrap(),+ title: "Other".into(),+ database_parent: NotionParent::Workspace,+ in_trash: false,+ properties: vec![],+ };+ // Search only finds the unrelated data source; the root database's+ // sources are reachable by id.+ struct PartialSearch(FakeWorkspace);+ impl NotionApi for PartialSearch {+ async fn search(+ &self,+ object: SearchObject,+ _cursor: Option<String>,+ ) -> Result<Paged<SearchHit>, NotionError> {+ let results = match object {+ SearchObject::Page => vec![],+ SearchObject::DataSource => {+ vec![SearchHit::DataSource(self.0.data_sources[2].clone())]+ }+ };+ Ok(Paged {+ results,+ next_cursor: None,+ })+ }+ async fn page(&self, id: &NotionId) -> Result<NotionPage, NotionError> {+ self.0.page(id).await+ }+ async fn page_markdown(&self, id: &NotionId) -> Result<PageMarkdown, NotionError> {+ self.0.page_markdown(id).await+ }+ async fn data_source(&self, id: &NotionId) -> Result<NotionDataSource, NotionError> {+ self.0.data_source(id).await+ }+ async fn database_sources(+ &self,+ database: &NotionId,+ ) -> Result<Vec<NotionId>, NotionError> {+ self.0.database_sources(database).await+ }+ async fn query_data_source(+ &self,+ id: &NotionId,+ cursor: Option<String>,+ ) -> Result<Paged<NotionPage>, NotionError> {+ self.0.query_data_source(id, cursor).await+ }+ async fn users(&self, cursor: Option<String>) -> Result<Paged<NotionUser>, NotionError> {+ self.0.users(cursor).await+ }+ }+ let workspace = PartialSearch(FakeWorkspace {+ data_sources: vec![tasks.clone(), archive.clone(), other],+ databases: HashMap::from([(+ NotionId::parse("d9824bdc84454327be8b5b47500af6ce").unwrap(),+ vec![tasks.id.clone(), archive.id.clone()],+ )]),+ ..FakeWorkspace::default()+ });++ let found = inventory(+ &workspace,+ &ImportScope::Roots {+ ids: vec![NotionId::parse("d9824bdc84454327be8b5b47500af6ce").unwrap()],+ },+ &|_| {},+ )+ .await+ .unwrap();++ let titles: Vec<&str> = found+ .data_sources+ .iter()+ .map(|summary| summary.source.title.as_str())+ .collect();+ assert_eq!(titles, vec!["Tasks", "Archive"]);+ assert_eq!(found.pages, vec![]);+ // Search never listed them, so only a query can count their rows.+ assert_eq!(+ *workspace.0.queries.lock().unwrap(),+ vec![tasks.id.clone(), archive.id.clone()]+ );+}++#[tokio::test]+async fn a_data_source_search_shows_no_rows_for_is_queried_page_by_page() {+ let roadmap = NotionDataSource {+ id: NotionId::parse("1a44be1209534631a1d67d4c2b1b4a2f").unwrap(),+ database_id: NotionId::parse("d9824bdc84454327be8b5b47500af6ce").unwrap(),+ title: "Roadmap".into(),+ database_parent: NotionParent::Workspace,+ in_trash: false,+ properties: vec![],+ };+ let row = NotionPage {+ id: NotionId::parse("ee5f0f84409a440f983aa5315961c6e4").unwrap(),+ title: "Ship onboarding".into(),+ url: "https://www.notion.so/ee5f0f84409a440f983aa5315961c6e4".into(),+ parent: NotionParent::DataSource {+ data_source: roadmap.id.clone(),+ database: roadmap.database_id.clone(),+ },+ last_edited: Utc.with_ymd_and_hms(2026, 2, 2, 0, 0, 0).unwrap(),+ created: Utc.with_ymd_and_hms(2026, 2, 1, 0, 0, 0).unwrap(),+ in_trash: false,+ properties: BTreeMap::new(),+ };+ let workspace = FakeWorkspace {+ data_sources: vec![roadmap.clone()],+ rows: HashMap::from([(+ roadmap.id.clone(),+ Paged {+ results: vec![row.clone()],+ next_cursor: Some("more".into()),+ },+ )]),+ later_rows: HashMap::from([(+ roadmap.id.clone(),+ vec![NotionPage {+ id: NotionId::parse("7d0c5bd5b4a04f3a9c7d0c5bd5b4a04f").unwrap(),+ title: "Ship billing".into(),+ ..row+ }],+ )]),+ ..FakeWorkspace::default()+ };++ let found = inventory(&workspace, &ImportScope::Everything, &|_| {})+ .await+ .unwrap();++ assert_eq!(+ found.data_sources,+ vec![DataSourceSummary {+ source: roadmap.clone(),+ row_count: 2,+ sample_titles: vec!["Ship onboarding".into(), "Ship billing".into()],+ }]+ );+ assert_eq!(+ *workspace.queries.lock().unwrap(),+ vec![roadmap.id.clone(), roadmap.id]+ );+}++#[tokio::test]+async fn a_workspace_whose_rows_search_lists_costs_two_searches_and_no_row_queries() {+ // Three databases of three rows each, under one page. Counting rows by+ // querying cost a query per data source (and per hundred rows) on top+ // of the searches: 3 here, and 24 data sources with 3,900 rows cost at+ // least 54 on a real workspace.+ let home = NotionPage {+ id: NotionId::parse("598337872cf94fdf8782e53db20768a5").unwrap(),+ title: "Home".into(),+ url: "https://www.notion.so/598337872cf94fdf8782e53db20768a5".into(),+ parent: NotionParent::Workspace,+ last_edited: Utc.with_ymd_and_hms(2026, 3, 2, 0, 0, 0).unwrap(),+ created: Utc.with_ymd_and_hms(2026, 3, 1, 0, 0, 0).unwrap(),+ in_trash: false,+ properties: BTreeMap::new(),+ };+ let sources: Vec<NotionDataSource> = ["1", "2", "3"]+ .into_iter()+ .map(|digit| NotionDataSource {+ id: NotionId::parse(&format!("{digit}a44be1209534631a1d67d4c2b1b4a2f")).unwrap(),+ database_id: NotionId::parse(&format!("{digit}9824bdc84454327be8b5b47500af6ce"))+ .unwrap(),+ title: format!("Tracker {digit}"),+ database_parent: NotionParent::Page {+ id: home.id.clone(),+ },+ in_trash: false,+ properties: vec![],+ })+ .collect();+ let mut pages = vec![home.clone()];+ for (source_index, source) in sources.iter().enumerate() {+ for row_index in 0..3 {+ pages.push(NotionPage {+ id: NotionId::parse(&format!(+ "{source_index}{row_index}5f0f84409a440f983aa5315961c6e4"+ ))+ .unwrap(),+ title: format!("Row {row_index}"),+ parent: NotionParent::DataSource {+ data_source: source.id.clone(),+ database: source.database_id.clone(),+ },+ ..home.clone()+ });+ }+ }+ let workspace = FakeWorkspace {+ pages,+ data_sources: sources.clone(),+ ..FakeWorkspace::default()+ };++ let found = inventory(&workspace, &ImportScope::Everything, &|_| {})+ .await+ .unwrap();++ assert_eq!(found.pages, vec![home]);+ assert_eq!(+ found+ .data_sources+ .iter()+ .map(|summary| (summary.source.title.as_str(), summary.row_count))+ .collect::<Vec<_>>(),+ vec![("Tracker 1", 3), ("Tracker 2", 3), ("Tracker 3", 3)]+ );+ assert_eq!(+ *workspace.searches.lock().unwrap(),+ vec![SearchObject::Page, SearchObject::DataSource]+ );+ assert_eq!(*workspace.queries.lock().unwrap(), Vec::<NotionId>::new());+}++#[tokio::test]+async fn full_markdown_splices_unknown_blocks_into_their_placeholders() {+ let page = NotionId::parse("598337872cf94fdf8782e53db20768a5").unwrap();+ let toggle = NotionId::parse("c7f0aa501f0e4a529a17e2f6f3a3b0c1").unwrap();+ let nested = NotionId::parse("ee5f0f84409a440f983aa5315961c6e4").unwrap();+ let orphan = NotionId::parse("9a3b5ae0c6e6482db0e1ed315ee6dc57").unwrap();+ let workspace = FakeWorkspace {+ markdown: HashMap::from([+ (+ page.clone(),+ PageMarkdown {+ markdown: "# Plan\n\n<unknown url=\"https://www.notion.so/598337872cf94fdf8782e53db20768a5#c7f0aa50-1f0e-4a52-9a17-e2f6f3a3b0c1\" alt=\"toggle\"/>\n\nEnd".into(),+ unknown_block_ids: vec![toggle.clone(), orphan.clone()],+ },+ ),+ (+ toggle,+ PageMarkdown {+ markdown: "<details>\n<summary>More</summary>\n<unknown url=\"https://www.notion.so/x#ee5f0f84409a440f983aa5315961c6e4\" alt=\"table\"/>\n</details>\n".into(),+ unknown_block_ids: vec![nested.clone()],+ },+ ),+ (+ nested,+ PageMarkdown {+ markdown: "| a | b |".into(),+ unknown_block_ids: vec![],+ },+ ),+ (+ orphan,+ PageMarkdown {+ markdown: "Trailing block".into(),+ unknown_block_ids: vec![],+ },+ ),+ ]),+ ..FakeWorkspace::default()+ };++ let markdown = full_markdown(&workspace, &page).await.unwrap();++ assert_eq!(+ markdown,+ "# Plan\n\n<details>\n<summary>More</summary>\n| a | b |\n</details>\n\nEnd\n\nTrailing block"+ );+ assert_eq!(workspace.markdown_requests.lock().unwrap().len(), 4);+}++#[tokio::test]+async fn full_markdown_keeps_the_placeholder_of_an_unreadable_block() {+ let page = NotionId::parse("598337872cf94fdf8782e53db20768a5").unwrap();+ let workspace = FakeWorkspace {+ markdown: HashMap::from([(+ page.clone(),+ PageMarkdown {+ markdown: "Intro <unknown alt=\"c7f0aa501f0e4a529a17e2f6f3a3b0c1\"/>".into(),+ unknown_block_ids: vec![+ NotionId::parse("c7f0aa501f0e4a529a17e2f6f3a3b0c1").unwrap(),+ ],+ },+ )]),+ ..FakeWorkspace::default()+ };++ let markdown = full_markdown(&workspace, &page).await.unwrap();++ assert_eq!(+ markdown,+ "Intro <unknown alt=\"c7f0aa501f0e4a529a17e2f6f3a3b0c1\"/>"+ );+}++#[tokio::test]+async fn a_database_embedded_in_a_page_keeps_its_placeholder_instead_of_failing_the_page() {+ let page = NotionId::parse("598337872cf94fdf8782e53db20768a5").unwrap();+ let embedded = NotionId::parse("e617ab741aa546b19d317930ff338b29").unwrap();+ let workspace = FakeWorkspace {+ markdown: HashMap::from([(+ page.clone(),+ PageMarkdown {+ markdown: "Welcome <unknown alt=\"e617ab741aa546b19d317930ff338b29\"/> Outro"+ .into(),+ unknown_block_ids: vec![embedded.clone()],+ },+ )]),+ databases_not_pages: vec![embedded],+ ..FakeWorkspace::default()+ };++ let markdown = full_markdown(&workspace, &page).await.unwrap();++ assert_eq!(+ markdown,+ "Welcome <unknown alt=\"e617ab741aa546b19d317930ff338b29\"/> Outro"+ );+}diff --git a/crates/import_notion/src/domain/model.rs b/crates/import_notion/src/domain/model.rsnew file mode 100644index 0000000000..8041a9247d--- /dev/null+++ b/crates/import_notion/src/domain/model.rs@@ -0,0 +1,374 @@+//! Notion objects as the importer reads them: typed, flattened from the REST+//! API's wire shapes, and stripped of everything an import never uses.++use std::collections::BTreeMap;+use std::fmt;++use chrono::{DateTime, Utc};+use lazy_regex::regex;+use serde::{Deserialize, Serialize};++/// A Notion object id: 32 lowercase hex digits, without dashes.+///+/// Notion accepts and returns ids both with and without dashes, and URLs end+/// in the dashless form after a slug, so ids are normalized on the way in.+#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]+#[serde(try_from = "String", into = "String")]+pub struct NotionId(String);++impl NotionId {+ /// Parse an id from a raw id (with or without dashes) or a Notion URL+ /// whose last path segment ends in one, such as+ /// `https://www.notion.so/Q3-plan-3fb607323e2e80629886fc0c028d50a7`.+ pub fn parse(input: &str) -> Option<Self> {+ // The id ends the path, with or without its dashes; a query or+ // fragment after it (`?v=…` on a database view) is never the id.+ let captures = regex!(+ r"(?i)^[^?#]*?([0-9a-f]{8})-?([0-9a-f]{4})-?([0-9a-f]{4})-?([0-9a-f]{4})-?([0-9a-f]{12})/?(?:[?#].*)?$"+ )+ .captures(input.trim())?;+ let hex: String = (1..=5)+ .filter_map(|group| captures.get(group))+ .map(|part| part.as_str().to_ascii_lowercase())+ .collect();+ Some(Self(hex))+ }++ /// The dashless form.+ pub fn as_str(&self) -> &str {+ &self.0+ }++ /// A canonical link to the object in Notion.+ pub fn url(&self) -> String {+ format!("https://www.notion.so/{}", self.0)+ }+}++impl fmt::Display for NotionId {+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {+ f.write_str(&self.0)+ }+}++impl TryFrom<String> for NotionId {+ type Error = String;++ fn try_from(value: String) -> Result<Self, Self::Error> {+ Self::parse(&value).ok_or_else(|| format!("not a Notion id: {value}"))+ }+}++impl From<NotionId> for String {+ fn from(id: NotionId) -> Self {+ id.0+ }+}++/// Where a Notion object sits.+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]+#[serde(tag = "type", rename_all = "snake_case")]+pub enum NotionParent {+ /// At the top of the workspace.+ Workspace,+ /// Under a page.+ Page {+ /// The parent page.+ id: NotionId,+ },+ /// Under a database container (for data sources, and wiki databases).+ Database {+ /// The parent database.+ id: NotionId,+ },+ /// A row of a data source.+ DataSource {+ /// The data source the page is a row of.+ data_source: NotionId,+ /// The database holding that data source.+ database: NotionId,+ },+ /// Inside a block, such as a column; the owning page is further up.+ Block {+ /// The parent block.+ id: NotionId,+ },+ /// A parent kind the importer does not model (for example an agent).+ Other,+}++/// A page, either a standalone document or a data source row.+#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]+pub struct NotionPage {+ /// The page id.+ pub id: NotionId,+ /// The plain-text title, empty when the page has none.+ pub title: String,+ /// The page's Notion URL.+ pub url: String,+ /// Where the page sits.+ pub parent: NotionParent,+ /// When the page was last edited.+ pub last_edited: DateTime<Utc>,+ /// When the page was created.+ pub created: DateTime<Utc>,+ /// Whether the page is in Notion's trash.+ pub in_trash: bool,+ /// Property values by property name, title excluded.+ pub properties: BTreeMap<String, NotionValue>,+}++/// A data source: the table behind a Notion database, with its schema.+#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]+pub struct NotionDataSource {+ /// The data source id.+ pub id: NotionId,+ /// The database container holding this data source.+ pub database_id: NotionId,+ /// The plain-text title.+ pub title: String,+ /// Where the database container sits.+ pub database_parent: NotionParent,+ /// Whether the data source is in Notion's trash.+ pub in_trash: bool,+ /// The columns, in the order Notion returns them, title included.+ pub properties: Vec<NotionPropertySchema>,+}++/// One column of a data source.+#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]+pub struct NotionPropertySchema {+ /// Notion's stable property id.+ pub id: String,+ /// The column name.+ pub name: String,+ /// The column's type and its configuration.+ pub kind: NotionPropertyKind,+}++/// A data source column type.+#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]+#[serde(tag = "type", rename_all = "snake_case")]+pub enum NotionPropertyKind {+ /// The row title.+ Title,+ /// Formatted text, read as plain text.+ RichText,+ /// A number.+ Number,+ /// One option.+ Select {+ /// The declared options.+ options: Vec<NotionOption>,+ },+ /// Several options.+ MultiSelect {+ /// The declared options.+ options: Vec<NotionOption>,+ },+ /// Notion's workflow status, an option with groups.+ Status {+ /// The declared options.+ options: Vec<NotionOption>,+ },+ /// A date or date range.+ Date,+ /// Workspace members.+ People,+ /// Attached files.+ Files,+ /// A checkbox.+ Checkbox,+ /// A URL.+ Url,+ /// An email address.+ Email,+ /// A phone number.+ PhoneNumber,+ /// A computed value.+ Formula,+ /// Rows of another data source.+ Relation {+ /// The related data source, when Notion reports it.+ data_source_id: Option<NotionId>,+ },+ /// An aggregate over a relation.+ Rollup,+ /// When the row was created.+ CreatedTime,+ /// Who created the row.+ CreatedBy,+ /// When the row was last edited.+ LastEditedTime,+ /// Who last edited the row.+ LastEditedBy,+ /// An auto-incrementing id, such as `TASK-42`.+ UniqueId {+ /// The id prefix, when one is set.+ prefix: Option<String>,+ },+ /// A type the importer does not model; carries Notion's type name.+ Unsupported {+ /// Notion's type name, such as `button`.+ notion_type: String,+ },+}++/// A select, multi-select or status option.+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]+pub struct NotionOption {+ /// Notion's option id.+ pub id: String,+ /// The label.+ pub name: String,+ /// Notion's color name, such as `blue`.+ pub color: String,+}++/// One property value on a page.+#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]+#[serde(tag = "type", content = "value", rename_all = "snake_case")]+pub enum NotionValue {+ /// Title, rich text, or a formula's string result, as plain text.+ Text(String),+ /// A number.+ Number(f64),+ /// A select or status option label.+ Option(String),+ /// Multi-select labels.+ Options(Vec<String>),+ /// A date, or the start and end of a range, as ISO-8601 strings.+ Date {+ /// The start.+ start: String,+ /// The end, for a range.+ end: Option<String>,+ },+ /// Notion user ids.+ People(Vec<NotionId>),+ /// Attached files.+ Files(Vec<NotionFile>),+ /// A checkbox.+ Checkbox(bool),+ /// A URL.+ Url(String),+ /// An email address.+ Email(String),+ /// A phone number.+ Phone(String),+ /// Ids of related pages.+ Relation(Vec<NotionId>),+ /// A timestamp (created or last-edited time), ISO-8601.+ Timestamp(String),+ /// A unique id rendered with its prefix, such as `TASK-42`.+ UniqueId(String),+ /// Several values, from a rollup.+ List(Vec<NotionValue>),+ /// No value.+ Empty,+}++/// A file attached to a page property.+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]+pub struct NotionFile {+ /// The file's name.+ pub name: String,+ /// Where to download it.+ pub url: String,+ /// When `url` stops working, for files Notion hosts.+ pub expires: Option<DateTime<Utc>>,+}++/// A workspace member.+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]+pub struct NotionUser {+ /// The user id.+ pub id: NotionId,+ /// The display name.+ pub name: Option<String>,+ /// The email, when the connection may read it.+ pub email: Option<String>,+}++/// A page's body as Notion-flavoured markdown.+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]+pub struct PageMarkdown {+ /// The markdown.+ pub markdown: String,+ /// Subtrees Notion left out of `markdown` because the page was too large;+ /// each can be fetched as markdown on its own.+ pub unknown_block_ids: Vec<NotionId>,+}++/// One page of a paginated list.+#[derive(Debug, Clone, PartialEq)]+pub struct Paged<T> {+ /// The items on this page.+ pub results: Vec<T>,+ /// Where the next page starts, when there is one.+ pub next_cursor: Option<String>,+}++/// What a search result is.+#[derive(Debug, Clone, PartialEq)]+pub enum SearchHit {+ /// A page (a document or a row).+ Page(NotionPage),+ /// A data source.+ DataSource(NotionDataSource),+}++/// Which object type a search lists.+#[derive(Debug, Clone, Copy, PartialEq, Eq)]+pub enum SearchObject {+ /// Pages, including data source rows.+ Page,+ /// Data sources.+ DataSource,+}++/// What part of the workspace an import covers.+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]+#[serde(tag = "type", rename_all = "snake_case")]+pub enum ImportScope {+ /// Everything the connection was granted.+ Everything,+ /// These pages and databases, and everything under them.+ Roots {+ /// Page, database or data source ids.+ ids: Vec<NotionId>,+ },+}++/// A data source with what an import plan needs to know about its rows.+#[derive(Debug, Clone, PartialEq)]+pub struct DataSourceSummary {+ /// The data source.+ pub source: NotionDataSource,+ /// Every row the data source holds.+ pub row_count: usize,+ /// The titles of the first few rows.+ pub sample_titles: Vec<String>,+}++/// Everything in scope: the pages that are documents (rows excluded) and the+/// data sources.+#[derive(Debug, Clone, PartialEq, Default)]+pub struct Inventory {+ /// Standalone pages; rows of data sources are not here.+ pub pages: Vec<NotionPage>,+ /// Data sources with row summaries.+ pub data_sources: Vec<DataSourceSummary>,+}++/// How far an inventory has got, for progress reporting.+#[derive(Debug, Clone, Copy, PartialEq, Eq)]+pub struct InventoryProgress {+ /// Pages found so far.+ pub pages: usize,+ /// Data sources found so far.+ pub data_sources: usize,+}++#[cfg(test)]+mod test;diff --git a/crates/import_notion/src/domain/model/test.rs b/crates/import_notion/src/domain/model/test.rsnew file mode 100644index 0000000000..de3490a30d--- /dev/null+++ b/crates/import_notion/src/domain/model/test.rs@@ -0,0 +1,72 @@+use super::NotionId;++#[test]+fn a_dashed_id_is_normalized_to_lowercase_without_dashes() {+ let id = NotionId::parse("59833787-2CF9-4FDF-8782-E53DB20768A5").unwrap();++ assert_eq!(id.as_str(), "598337872cf94fdf8782e53db20768a5");+ assert_eq!(+ id.url(),+ "https://www.notion.so/598337872cf94fdf8782e53db20768a5"+ );+}++#[test]+fn a_dashless_id_parses_as_is() {+ assert_eq!(+ NotionId::parse("598337872cf94fdf8782e53db20768a5")+ .unwrap()+ .as_str(),+ "598337872cf94fdf8782e53db20768a5"+ );+}++#[test]+fn a_page_url_yields_the_id_after_its_slug() {+ assert_eq!(+ NotionId::parse(+ "https://www.notion.so/acme/Q3-plan-3fb607323e2e80629886fc0c028d50a7?pvs=4#heading"+ )+ .unwrap()+ .as_str(),+ "3fb607323e2e80629886fc0c028d50a7"+ );+}++#[test]+fn a_url_with_a_trailing_slash_still_parses() {+ assert_eq!(+ NotionId::parse("https://www.notion.so/3fb607323e2e80629886fc0c028d50a7/")+ .unwrap()+ .as_str(),+ "3fb607323e2e80629886fc0c028d50a7"+ );+}++#[test]+fn text_without_32_hex_digits_is_not_an_id() {+ assert_eq!(NotionId::parse("https://www.notion.so/Q3-plan"), None);+ assert_eq!(NotionId::parse("598337872cf94fdf8782e53db2076"), None);+ assert_eq!(NotionId::parse("zz8337872cf94fdf8782e53db20768a5"), None);+ assert_eq!(NotionId::parse(""), None);+}++#[test]+fn ids_deserialize_from_either_form() {+ let id: NotionId = serde_json::from_str("\"59833787-2cf9-4fdf-8782-e53db20768a5\"").unwrap();++ assert_eq!(id.as_str(), "598337872cf94fdf8782e53db20768a5");+ assert!(serde_json::from_str::<NotionId>("\"not-an-id\"").is_err());+}++#[test]+fn a_database_view_link_yields_the_database_not_the_view_in_its_query() {+ assert_eq!(+ NotionId::parse(+ "https://www.notion.so/acme/1a44be1209534631a1d67d4c2b1b4a2f?v=d9824bdc84454327be8b5b47500af6ce&p=598337872cf94fdf8782e53db20768a5"+ )+ .unwrap()+ .as_str(),+ "1a44be1209534631a1d67d4c2b1b4a2f"+ );+}diff --git a/crates/import_notion/src/domain/ports.rs b/crates/import_notion/src/domain/ports.rsnew file mode 100644index 0000000000..18efd6d435--- /dev/null+++ b/crates/import_notion/src/domain/ports.rs@@ -0,0 +1,104 @@+//! What the importer needs from Notion, and how a user's Notion is reached.++use std::future::Future;+use std::time::Duration;++use macro_user_id::user_id::MacroUserIdStr;++use super::model::{+ NotionDataSource, NotionId, NotionPage, NotionUser, PageMarkdown, Paged, SearchHit,+ SearchObject,+};++/// Why a Notion request failed.+#[derive(Debug, thiserror::Error)]+pub enum NotionError {+ /// Notion (or the proxy) asked us to slow down and retries ran out.+ #[error("Notion rate limited the import; retry after {retry_after:?}")]+ RateLimited {+ /// How long Notion asked to wait, when it said.+ retry_after: Option<Duration>,+ },+ /// The object does not exist or was not shared with the connection.+ #[error("Notion object not found or not shared with the connection")]+ NotFound,+ /// The connection may not read this.+ #[error("the Notion connection may not read this object")]+ Forbidden,+ /// Notion answered with an error the importer does not handle.+ #[error("Notion could not complete the request: {body}")]+ Upstream {+ /// The HTTP status.+ status: u16,+ /// The response body, truncated.+ body: String,+ },+ /// Notion answered with a shape the importer could not read.+ #[error("unexpected Notion response: {0}")]+ Decode(String),+ /// The request did not complete.+ #[error("Notion request failed: {0:?}")]+ Transport(rootcause::Report),+}++/// A user's Notion workspace, read through the REST API.+///+/// Implementations pace themselves to Notion's rate limit and retry rate+/// limited and transient failures, so callers may issue requests+/// concurrently.+pub trait NotionApi: Send + Sync + 'static {+ /// One page of a workspace search for `object`, most recently edited+ /// first.+ fn search(+ &self,+ object: SearchObject,+ cursor: Option<String>,+ ) -> impl Future<Output = Result<Paged<SearchHit>, NotionError>> + Send;++ /// A page and its property values.+ fn page(&self, id: &NotionId) -> impl Future<Output = Result<NotionPage, NotionError>> + Send;++ /// A page's body as markdown.+ fn page_markdown(+ &self,+ id: &NotionId,+ ) -> impl Future<Output = Result<PageMarkdown, NotionError>> + Send;++ /// A data source and its schema.+ fn data_source(+ &self,+ id: &NotionId,+ ) -> impl Future<Output = Result<NotionDataSource, NotionError>> + Send;++ /// The data sources of a database container.+ fn database_sources(+ &self,+ database: &NotionId,+ ) -> impl Future<Output = Result<Vec<NotionId>, NotionError>> + Send;++ /// One page of a data source's rows, oldest first.+ fn query_data_source(+ &self,+ id: &NotionId,+ cursor: Option<String>,+ ) -> impl Future<Output = Result<Paged<NotionPage>, NotionError>> + Send;++ /// One page of workspace members.+ fn users(+ &self,+ cursor: Option<String>,+ ) -> impl Future<Output = Result<Paged<NotionUser>, NotionError>> + Send;+}++/// Opens a user's Notion workspace.+pub trait NotionConnections: Send + Sync + 'static {+ /// The workspace client.+ type Api: NotionApi;++ /// The user's Notion workspace, or `None` when they have not connected+ /// Notion (or turned it off).+ fn for_user(+ &self,+ user: &MacroUserIdStr<'static>,+ ) -> impl Future<Output = Result<Option<Self::Api>, NotionError>> + Send;+}diff --git a/crates/import_notion/src/lib.rs b/crates/import_notion/src/lib.rsnew file mode 100644index 0000000000..253fe0ce73--- /dev/null+++ b/crates/import_notion/src/lib.rs@@ -0,0 +1,11 @@+//! Reading a user's Notion workspace for import into Macro.+//!+//! The domain is the importer's view of Notion: typed pages, data sources and+//! property values, the [`domain::ports::NotionApi`] port, and the+//! inventory that walks everything in scope. The outbound adapter reaches+//! Notion's REST API through the user's Pipedream Connect grant.++#![deny(missing_docs)]++pub mod domain;+pub mod outbound;diff --git a/crates/import_notion/src/outbound.rs b/crates/import_notion/src/outbound.rsnew file mode 100644index 0000000000..55e9f4fb87--- /dev/null+++ b/crates/import_notion/src/outbound.rs@@ -0,0 +1,4 @@+//! Adapters reaching Notion.++pub mod pipedream;+pub mod rest;diff --git a/crates/import_notion/src/outbound/pipedream.rs b/crates/import_notion/src/outbound/pipedream.rsnew file mode 100644index 0000000000..a60a5baf68--- /dev/null+++ b/crates/import_notion/src/outbound/pipedream.rs@@ -0,0 +1,129 @@+//! Reaching a user's Notion through their Pipedream Connect grant: Pipedream+//! injects the account's credentials into proxied REST requests.++use std::collections::HashMap;+use std::sync::{Arc, Mutex, PoisonError, Weak};++use http::header::{CONTENT_TYPE, HeaderMap, HeaderName, HeaderValue};+use macro_user_id::user_id::MacroUserIdStr;+use pipedream_mcp::domain::models::PipedreamConnection;+use pipedream_mcp::domain::ports::{ConnectedAppProxy, ConnectionStore};+use rootcause::compat::IntoRootcause;++use super::rest::{+ NOTION_REQUEST_INTERVAL, NOTION_VERSION, NotionRead, NotionRestClient, NotionTransport,+ RequestPacer, TransportResponse,+};+use crate::domain::ports::{NotionConnections, NotionError};++/// Pipedream's app slug for Notion.+const NOTION_APP: &str = "notion";++/// Notion's REST API origin.+const NOTION_API_URL: &str = "https://api.notion.com";++/// Opens users' Notion workspaces from their Pipedream connections.+///+/// Every client of one Pipedream account shares a [`RequestPacer`], so+/// concurrent imports from the same workspace grant stay under Notion's rate+/// limit together.+pub struct PipedreamNotionConnections<Store, Proxy> {+ store: Arc<Store>,+ proxy: Option<Arc<Proxy>>,+ pacers: Mutex<HashMap<String, Weak<RequestPacer>>>,+}++impl<Store, Proxy> PipedreamNotionConnections<Store, Proxy> {+ /// Connections read from `store`, proxied through `proxy`; with no proxy+ /// (Pipedream unconfigured) nobody has Notion connected.+ pub fn new(store: Arc<Store>, proxy: Option<Arc<Proxy>>) -> Self {+ Self {+ store,+ proxy,+ pacers: Mutex::new(HashMap::new()),+ }+ }++ fn pacer(&self, account_id: &str) -> Arc<RequestPacer> {+ let mut pacers = self.pacers.lock().unwrap_or_else(PoisonError::into_inner);+ if let Some(pacer) = pacers.get(account_id).and_then(Weak::upgrade) {+ return pacer;+ }+ pacers.retain(|_, pacer| pacer.strong_count() > 0);+ let pacer = Arc::new(RequestPacer::new(NOTION_REQUEST_INTERVAL));+ pacers.insert(account_id.to_owned(), Arc::downgrade(&pacer));+ pacer+ }+}++impl<Store, Proxy> NotionConnections for PipedreamNotionConnections<Store, Proxy>+where+ Store: ConnectionStore,+ Proxy: ConnectedAppProxy,+{+ type Api = NotionRestClient<PipedreamTransport<Proxy>>;++ #[tracing::instrument(skip_all, err)]+ async fn for_user(+ &self,+ user: &MacroUserIdStr<'static>,+ ) -> Result<Option<Self::Api>, NotionError> {+ let Some(proxy) = &self.proxy else {+ return Ok(None);+ };+ let connection = self.store.load(user, NOTION_APP).await.map_err(|error| {+ NotionError::Transport(rootcause::report!(+ "loading the Notion connection: {error:?}"+ ))+ })?;+ let Some(connection) = connection.filter(|connection| connection.enabled) else {+ return Ok(None);+ };+ let pacer = self.pacer(&connection.account_id);+ Ok(Some(NotionRestClient::new(+ PipedreamTransport {+ proxy: proxy.clone(),+ connection,+ },+ pacer,+ )))+ }+}++/// Sends Notion REST requests through Pipedream's Connect proxy for one+/// connected account.+pub struct PipedreamTransport<Proxy> {+ proxy: Arc<Proxy>,+ connection: PipedreamConnection,+}++impl<Proxy: ConnectedAppProxy> NotionTransport for PipedreamTransport<Proxy> {+ async fn send(&self, read: &NotionRead) -> Result<TransportResponse, NotionError> {+ let headers = HeaderMap::from_iter([+ (+ HeaderName::from_static("notion-version"),+ HeaderValue::from_static(NOTION_VERSION),+ ),+ (CONTENT_TYPE, HeaderValue::from_static("application/json")),+ ]);+ let response = self+ .proxy+ .proxy(+ &self.connection,+ read.method(),+ &format!("{NOTION_API_URL}{}", read.path_and_query()),+ &headers,+ read.body(),+ )+ .await+ .map_err(|error| NotionError::Transport(error.into_rootcause()))?;+ Ok(TransportResponse {+ status: response.status,+ retry_after: response.retry_after(),+ body: response.body,+ })+ }+}++#[cfg(test)]+mod test;diff --git a/crates/import_notion/src/outbound/pipedream/test.rs b/crates/import_notion/src/outbound/pipedream/test.rsnew file mode 100644index 0000000000..adbecd6942--- /dev/null+++ b/crates/import_notion/src/outbound/pipedream/test.rs@@ -0,0 +1,168 @@+use std::sync::{Arc, Mutex};+use std::time::Duration;++use http::{HeaderMap, Method, StatusCode};+use macro_user_id::user_id::MacroUserIdStr;+use pipedream_mcp::domain::models::PipedreamConnection;+use pipedream_mcp::domain::ports::{ConnectedAppProxy, ConnectionStore, ProxyResponse};+use tokio::time::Instant;++use super::PipedreamNotionConnections;+use crate::domain::model::NotionId;+use crate::domain::ports::{NotionApi, NotionConnections};++struct OneConnection(Option<PipedreamConnection>);++impl ConnectionStore for OneConnection {+ type Err = String;++ async fn save(&self, _record: &PipedreamConnection) -> Result<(), String> {+ unreachable!("connections are never saved by the importer")+ }++ async fn load(+ &self,+ user_id: &MacroUserIdStr<'static>,+ app_slug: &str,+ ) -> Result<Option<PipedreamConnection>, String> {+ assert_eq!(app_slug, "notion");+ Ok(self+ .0+ .clone()+ .filter(|connection| &connection.user_id == user_id))+ }++ async fn delete(+ &self,+ _user_id: &MacroUserIdStr<'static>,+ _app_slug: &str,+ ) -> Result<(), String> {+ unreachable!("connections are never deleted by the importer")+ }++ async fn list(+ &self,+ _user_id: &MacroUserIdStr<'static>,+ ) -> Result<Vec<PipedreamConnection>, String> {+ unreachable!("connections are loaded by app")+ }+}++#[derive(Debug, Clone, PartialEq)]+struct ProxiedRequest {+ account_id: String,+ method: Method,+ url: String,+ headers: HeaderMap,+ at: Instant,+}++#[derive(Default)]+struct RecordingProxy {+ requests: Mutex<Vec<ProxiedRequest>>,+}++impl ConnectedAppProxy for RecordingProxy {+ async fn proxy(+ &self,+ connection: &PipedreamConnection,+ method: Method,+ url: &str,+ headers: &HeaderMap,+ _body: Option<&serde_json::Value>,+ ) -> anyhow::Result<ProxyResponse> {+ self.requests.lock().unwrap().push(ProxiedRequest {+ account_id: connection.account_id.clone(),+ method,+ url: url.to_owned(),+ headers: headers.clone(),+ at: Instant::now(),+ });+ Ok(ProxyResponse {+ status: StatusCode::OK,+ headers: HeaderMap::new(),+ body: r#"{"object":"database","data_sources":[]}"#.into(),+ })+ }+}++#[tokio::test(start_paused = true)]+async fn a_connected_user_reaches_notion_through_the_proxy_with_one_pacer_per_account() {+ let user = MacroUserIdStr::try_from_email("[email protected]").unwrap();+ let store = Arc::new(OneConnection(Some(PipedreamConnection {+ user_id: user.clone(),+ app_slug: "notion".into(),+ server_name: "Notion".into(),+ account_id: "apn_notion".into(),+ enabled: true,+ })));+ let proxy = Arc::new(RecordingProxy::default());+ let connections = PipedreamNotionConnections::new(store, Some(proxy.clone()));+ let started = Instant::now();++ let first = connections.for_user(&user).await.unwrap().unwrap();+ let second = connections.for_user(&user).await.unwrap().unwrap();+ let database = NotionId::parse("d9824bdc84454327be8b5b47500af6ce").unwrap();+ let (a, b) = tokio::join!(+ first.database_sources(&database),+ second.database_sources(&database)+ );++ assert_eq!(a.unwrap(), vec![]);+ assert_eq!(b.unwrap(), vec![]);+ let requests = proxy.requests.lock().unwrap().clone();+ assert_eq!(+ requests[0],+ ProxiedRequest {+ account_id: "apn_notion".into(),+ method: Method::GET,+ url: "https://api.notion.com/v1/databases/d9824bdc84454327be8b5b47500af6ce".into(),+ headers: HeaderMap::from_iter([+ (+ http::HeaderName::from_static("notion-version"),+ http::HeaderValue::from_static("2026-03-11"),+ ),+ (+ http::header::CONTENT_TYPE,+ http::HeaderValue::from_static("application/json"),+ ),+ ]),+ at: started,+ }+ );+ // Two clients of the same account share its pacer.+ assert_eq!(requests[1].at - started, Duration::from_millis(340));+}++#[tokio::test]+async fn no_connection_a_disabled_one_or_no_proxy_means_no_notion() {+ let user = MacroUserIdStr::try_from_email("[email protected]").unwrap();+ let disabled = PipedreamConnection {+ user_id: user.clone(),+ app_slug: "notion".into(),+ server_name: "Notion".into(),+ account_id: "apn_notion".into(),+ enabled: false,+ };+ let enabled = PipedreamConnection {+ enabled: true,+ ..disabled.clone()+ };++ let missing = PipedreamNotionConnections::new(+ Arc::new(OneConnection(None)),+ Some(Arc::new(RecordingProxy::default())),+ );+ let turned_off = PipedreamNotionConnections::new(+ Arc::new(OneConnection(Some(disabled))),+ Some(Arc::new(RecordingProxy::default())),+ );+ let unconfigured = PipedreamNotionConnections::<_, RecordingProxy>::new(+ Arc::new(OneConnection(Some(enabled))),+ None,+ );++ assert!(missing.for_user(&user).await.unwrap().is_none());+ assert!(turned_off.for_user(&user).await.unwrap().is_none());+ assert!(unconfigured.for_user(&user).await.unwrap().is_none());+}diff --git a/crates/import_notion/src/outbound/rest.rs b/crates/import_notion/src/outbound/rest.rsnew file mode 100644index 0000000000..cdb5f4ce61--- /dev/null+++ b/crates/import_notion/src/outbound/rest.rs@@ -0,0 +1,570 @@+//! A [`NotionApi`] over Notion's REST API: request shapes, decoding,+//! pacing and retries, independent of how requests reach Notion.++use std::future::Future;+use std::sync::{Arc, Mutex, PoisonError};+use std::time::Duration;++use chrono::{DateTime, SecondsFormat, Utc};+use http::{Method, StatusCode};+use rand::Rng;+use serde::de::DeserializeOwned;+use serde::{Deserialize, Serialize};+use tokio::time::Instant;++use crate::domain::model::{+ NotionDataSource, NotionId, NotionPage, NotionUser, PageMarkdown, Paged, SearchHit,+ SearchObject,+};+use crate::domain::ports::{NotionApi, NotionError};++mod wire;++use wire::{+ WireDataSource, WireDatabase, WireError, WireList, WireMarkdown, WireObject, WirePage,+ WirePropertyItem, WireUser, WireValue,+};++/// The Notion API version the wire shapes follow (the data sources API).+pub const NOTION_VERSION: &str = "2026-03-11";++/// The spacing between requests on one connection: Notion allows about+/// three requests a second per integration.+pub const NOTION_REQUEST_INTERVAL: Duration = Duration::from_millis(340);++/// Notion's largest page size.+const PAGE_SIZE: u8 = 100;++/// Attempts per request before a rate limit or transient failure is final.+const MAX_ATTEMPTS: u8 = 5;++/// The first retry's delay when Notion does not say how long to wait.+const BASE_BACKOFF: Duration = Duration::from_millis(500);++/// The longest backoff between attempts.+const MAX_BACKOFF: Duration = Duration::from_secs(30);++/// The most random delay added to a backoff, in milliseconds.+const MAX_JITTER_MILLIS: u64 = 250;++/// How much of an error body is kept.+const ERROR_BODY_CHARS: usize = 500;++/// A request the importer makes to Notion. The client can send nothing+/// else: every variant is a read (a `GET`, or one of the two `POST`s Notion+/// uses for reads, search and data source queries), so an import can never+/// modify or delete anything in the user's Notion.+#[derive(Debug, Clone, PartialEq, Eq)]+pub enum NotionRead {+ /// `POST /v1/search` with a search body.+ Search(serde_json::Value),+ /// `GET /v1/pages/{id}`.+ Page(NotionId),+ /// `GET /v1/pages/{id}/markdown`.+ PageMarkdown(NotionId),+ /// `GET /v1/pages/{page}/properties/{property}`, one page of references.+ PageProperty {+ /// The page.+ page: NotionId,+ /// The property's id, as Notion encodes it in paths.+ property: String,+ /// Where the previous page of references ended.+ cursor: Option<String>,+ },+ /// `GET /v1/data_sources/{id}`.+ DataSource(NotionId),+ /// `POST /v1/data_sources/{id}/query` with a query body.+ QueryDataSource {+ /// The data source.+ id: NotionId,+ /// The filter, sorts and cursor.+ body: serde_json::Value,+ },+ /// `GET /v1/databases/{id}`.+ Database(NotionId),+ /// `GET /v1/users`, one page of members.+ Users {+ /// Where the previous page of members ended.+ cursor: Option<String>,+ },+}++impl NotionRead {+ /// The HTTP method Notion's API takes this read with.+ pub fn method(&self) -> Method {+ match self {+ Self::Search(_) | Self::QueryDataSource { .. } => Method::POST,+ Self::Page(_)+ | Self::PageMarkdown(_)+ | Self::PageProperty { .. }+ | Self::DataSource(_)+ | Self::Database(_)+ | Self::Users { .. } => Method::GET,+ }+ }++ /// The path and query, such as `/v1/search`.+ pub fn path_and_query(&self) -> String {+ match self {+ Self::Search(_) => "/v1/search".to_owned(),+ Self::Page(id) => format!("/v1/pages/{id}"),+ Self::PageMarkdown(id) => format!("/v1/pages/{id}/markdown"),+ Self::PageProperty {+ page,+ property,+ cursor,+ } => with_cursor(+ format!("/v1/pages/{page}/properties/{property}?page_size={PAGE_SIZE}"),+ cursor,+ ),+ Self::DataSource(id) => format!("/v1/data_sources/{id}"),+ Self::QueryDataSource { id, .. } => format!("/v1/data_sources/{id}/query"),+ Self::Database(id) => format!("/v1/databases/{id}"),+ Self::Users { cursor } => {+ with_cursor(format!("/v1/users?page_size={PAGE_SIZE}"), cursor)+ }+ }+ }++ /// The JSON body, for the reads that take one.+ pub fn body(&self) -> Option<&serde_json::Value> {+ match self {+ Self::Search(body) | Self::QueryDataSource { body, .. } => Some(body),+ Self::Page(_)+ | Self::PageMarkdown(_)+ | Self::PageProperty { .. }+ | Self::DataSource(_)+ | Self::Database(_)+ | Self::Users { .. } => None,+ }+ }+}++fn with_cursor(mut path: String, cursor: &Option<String>) -> String {+ if let Some(cursor) = cursor {+ path.push_str(&format!("&start_cursor={cursor}"));+ }+ path+}++/// Notion's answer to one request, whatever its status.+#[derive(Debug, Clone, PartialEq, Eq)]+pub struct TransportResponse {+ /// The HTTP status.+ pub status: StatusCode,+ /// How long Notion asked callers to wait, from `Retry-After`.+ pub retry_after: Option<Duration>,+ /// The response body.+ pub body: String,+}++/// Carries one request to Notion's REST API with the connection's+/// credentials.+pub trait NotionTransport: Send + Sync + 'static {+ /// Send `read`. A non-2xx answer is a response; only a request that did+ /// not complete is an error, and it should be [`NotionError::Transport`]+ /// to be retried.+ fn send(+ &self,+ read: &NotionRead,+ ) -> impl Future<Output = Result<TransportResponse, NotionError>> + Send;+}++/// Spaces requests on one Notion connection, shared by every client of that+/// connection so concurrent imports together stay under the rate limit.+#[derive(Debug)]+pub struct RequestPacer {+ interval: Duration,+ next: Mutex<Instant>,+}++impl RequestPacer {+ /// A pacer letting one request through every `interval`.+ pub fn new(interval: Duration) -> Self {+ Self {+ interval,+ next: Mutex::new(Instant::now()),+ }+ }++ /// Wait for this request's turn.+ async fn wait(&self) {+ let slot = {+ let mut next = self.next.lock().unwrap_or_else(PoisonError::into_inner);+ let slot = (*next).max(Instant::now());+ *next = slot + self.interval;+ slot+ };+ tokio::time::sleep_until(slot).await;+ }++ /// Hold every request for `delay`, as Notion asked.+ fn hold(&self, delay: Duration) {+ let mut next = self.next.lock().unwrap_or_else(PoisonError::into_inner);+ *next = (*next).max(Instant::now() + delay);+ }+}++/// A [`NotionApi`] speaking Notion's REST API through a [`NotionTransport`].+///+/// Requests wait their turn on the shared [`RequestPacer`]; rate limits+/// (429) and transient failures (5xx, 529, incomplete requests) are retried+/// with backoff and jitter, honouring `Retry-After` for every request on the+/// connection.+pub struct NotionRestClient<Transport> {+ transport: Transport,+ pacer: Arc<RequestPacer>,+}++impl<Transport: NotionTransport> NotionRestClient<Transport> {+ /// A client sending through `transport`, paced by `pacer`.+ pub fn new(transport: Transport, pacer: Arc<RequestPacer>) -> Self {+ Self { transport, pacer }+ }++ async fn request<Body: DeserializeOwned>(&self, read: NotionRead) -> Result<Body, NotionError> {+ let text = self.send(&read).await?;+ serde_json::from_str(&text)+ .map_err(|error| NotionError::Decode(format!("{}: {error}", read.path_and_query())))+ }++ #[tracing::instrument(+ name = "notion.request",+ skip_all,+ err,+ fields(method = %read.method(), path = %read.path_and_query(), attempts, status)+ )]+ async fn send(&self, read: &NotionRead) -> Result<String, NotionError> {+ let span = tracing::Span::current();+ let mut attempt: u8 = 0;+ loop {+ attempt += 1;+ span.record("attempts", attempt);+ self.pacer.wait().await;+ let (error, retry_after) = match self.transport.send(read).await {+ Ok(response) => {+ span.record("status", response.status.as_u16());+ if response.status.is_success() {+ return Ok(response.body);+ }+ match failure(response) {+ Failure::Final(error) => return Err(error),+ Failure::Transient { error, retry_after } => (error, retry_after),+ }+ }+ Err(NotionError::Transport(report)) => (NotionError::Transport(report), None),+ Err(error) => return Err(error),+ };+ if attempt >= MAX_ATTEMPTS {+ return Err(error);+ }+ tracing::warn!(attempt, error = %error, "retrying Notion request");+ match retry_after {+ Some(delay) => self.pacer.hold(delay),+ None => tokio::time::sleep(backoff(attempt)).await,+ }+ }+ }++ /// `page` with every truncated reference list fetched in full.+ async fn complete(&self, mut page: WirePage) -> Result<NotionPage, NotionError> {+ for property in page.properties.values_mut() {+ if !property.value.truncated() {+ continue;+ }+ let people = matches!(property.value, WireValue::People { .. });+ let references = self.all_references(&page.id, &property.id).await?;+ property.value = if people {+ WireValue::People {+ people: references,+ has_more: false,+ }+ } else {+ WireValue::Relation {+ relation: references,+ has_more: false,+ }+ };+ }+ Ok(page.into_page())+ }++ async fn all_references(+ &self,+ page: &NotionId,+ property: &str,+ ) -> Result<Vec<wire::WireReference>, NotionError> {+ let mut references = Vec::new();+ let mut cursor: Option<String> = None;+ loop {+ let read = NotionRead::PageProperty {+ page: page.clone(),+ property: property.to_owned(),+ cursor: cursor.take(),+ };+ let list: WireList = self.request(read.clone()).await?;+ for item in &list.results {+ let item = WirePropertyItem::deserialize(item).map_err(|error| {+ NotionError::Decode(format!("{}: {error}", read.path_and_query()))+ })?;+ references.extend(item.reference().map(|id| wire::WireReference { id }));+ }+ match list.next() {+ Some(next) => cursor = Some(next),+ None => return Ok(references),+ }+ }+ }++ async fn pages(&self, list: &WireList) -> Result<Vec<NotionPage>, NotionError> {+ let mut pages = Vec::with_capacity(list.results.len());+ for item in &list.results {+ match WireObject::deserialize(item) {+ Ok(WireObject::Page(page)) => pages.push(self.complete(page).await?),+ Ok(WireObject::DataSource(_) | WireObject::Other) => {}+ Err(error) => skip(item, &error),+ }+ }+ Ok(pages)+ }+}++impl<Transport: NotionTransport> NotionApi for NotionRestClient<Transport> {+ async fn search(+ &self,+ object: SearchObject,+ cursor: Option<String>,+ ) -> Result<Paged<SearchHit>, NotionError> {+ let value = match object {+ SearchObject::Page => "page",+ SearchObject::DataSource => "data_source",+ };+ let mut body = serde_json::json!({+ "filter": {"property": "object", "value": value},+ "sort": {"timestamp": "last_edited_time", "direction": "descending"},+ "page_size": PAGE_SIZE,+ });+ if let Some(cursor) = cursor {+ body["start_cursor"] = serde_json::Value::String(cursor);+ }+ let list: WireList = self.request(NotionRead::Search(body)).await?;+ let mut results = Vec::with_capacity(list.results.len());+ for item in &list.results {+ match WireObject::deserialize(item) {+ Ok(WireObject::Page(page)) => results.push(SearchHit::Page(page.into_page())),+ Ok(WireObject::DataSource(source)) => match source.into_data_source() {+ Ok(source) => results.push(SearchHit::DataSource(source)),+ Err(error) => skip(item, &error),+ },+ Ok(WireObject::Other) => {}+ Err(error) => skip(item, &error),+ }+ }+ Ok(Paged {+ results,+ next_cursor: list.next(),+ })+ }++ async fn page(&self, id: &NotionId) -> Result<NotionPage, NotionError> {+ let page: WirePage = self.request(NotionRead::Page(id.clone())).await?;+ self.complete(page).await+ }++ async fn page_markdown(&self, id: &NotionId) -> Result<PageMarkdown, NotionError> {+ let markdown: WireMarkdown = self.request(NotionRead::PageMarkdown(id.clone())).await?;+ Ok(PageMarkdown {+ markdown: markdown.markdown,+ unknown_block_ids: markdown.unknown_block_ids,+ })+ }++ async fn data_source(&self, id: &NotionId) -> Result<NotionDataSource, NotionError> {+ let read = NotionRead::DataSource(id.clone());+ let source: WireDataSource = self.request(read.clone()).await?;+ source+ .into_data_source()+ .map_err(|error| NotionError::Decode(format!("{}: {error}", read.path_and_query())))+ }++ async fn database_sources(&self, database: &NotionId) -> Result<Vec<NotionId>, NotionError> {+ let database: WireDatabase = self.request(NotionRead::Database(database.clone())).await?;+ Ok(database+ .data_sources+ .into_iter()+ .map(|source| source.id)+ .collect())+ }++ /// Rows oldest first. Notion caps one query at 10,000 rows; past the cap+ /// the cursor restarts the query from the last row's creation time, so+ /// rows created in that same instant come again and callers draining the+ /// query de-duplicate by id.+ async fn query_data_source(+ &self,+ id: &NotionId,+ cursor: Option<String>,+ ) -> Result<Paged<NotionPage>, NotionError> {+ let cursor = match cursor {+ Some(cursor) => serde_json::from_str::<QueryCursor>(&cursor)+ .map_err(|error| NotionError::Decode(format!("query cursor: {error}")))?,+ None => QueryCursor::default(),+ };+ let mut body = serde_json::json!({+ "page_size": PAGE_SIZE,+ "sorts": [{"timestamp": "created_time", "direction": "ascending"}],+ });+ if let Some(after) = cursor.created_on_or_after {+ body["filter"] = serde_json::json!({+ "timestamp": "created_time",+ "created_time": {"on_or_after": after.to_rfc3339_opts(SecondsFormat::Millis, true)},+ });+ }+ if let Some(start) = &cursor.start_cursor {+ body["start_cursor"] = serde_json::Value::String(start.clone());+ }+ let list: WireList = self+ .request(NotionRead::QueryDataSource {+ id: id.clone(),+ body,+ })+ .await?;+ let rows = self.pages(&list).await?;++ let next = match list.next() {+ Some(start) => Some(QueryCursor {+ start_cursor: Some(start),+ created_on_or_after: cursor.created_on_or_after,+ }),+ None if list.incomplete() => match rows.last() {+ Some(last) if Some(last.created) != cursor.created_on_or_after => {+ Some(QueryCursor {+ start_cursor: None,+ created_on_or_after: Some(last.created),+ })+ }+ _ => {+ tracing::warn!(data_source = %id, "query stopped at Notion's cap without progress; rows are missing");+ None+ }+ },+ None => None,+ };+ let next_cursor = next+ .map(|next| serde_json::to_string(&next))+ .transpose()+ .map_err(|error| NotionError::Decode(format!("query cursor: {error}")))?;+ Ok(Paged {+ results: rows,+ next_cursor,+ })+ }++ async fn users(&self, cursor: Option<String>) -> Result<Paged<NotionUser>, NotionError> {+ let list: WireList = match self.request(NotionRead::Users { cursor }).await {+ Ok(list) => list,+ // Listing members needs a capability the connection may lack.+ Err(NotionError::Forbidden) => {+ return Ok(Paged {+ results: Vec::new(),+ next_cursor: None,+ });+ }+ Err(error) => return Err(error),+ };+ let mut results = Vec::with_capacity(list.results.len());+ for item in &list.results {+ match WireUser::deserialize(item) {+ Ok(user) => results.extend(user.into_user()),+ Err(error) => skip(item, &error),+ }+ }+ Ok(Paged {+ results,+ next_cursor: list.next(),+ })+ }+}++/// Where a data source query resumes: Notion's own cursor, within the+/// created-time window that restarts a query past Notion's result cap.+#[derive(Debug, Default, Serialize, Deserialize)]+struct QueryCursor {+ #[serde(default, skip_serializing_if = "Option::is_none")]+ start_cursor: Option<String>,+ #[serde(default, skip_serializing_if = "Option::is_none")]+ created_on_or_after: Option<DateTime<Utc>>,+}++enum Failure {+ Transient {+ error: NotionError,+ retry_after: Option<Duration>,+ },+ Final(NotionError),+}++fn failure(response: TransportResponse) -> Failure {+ let code = serde_json::from_str::<WireError>(&response.body)+ .ok()+ .and_then(|error| error.code);+ match (response.status, code.as_deref()) {+ (StatusCode::TOO_MANY_REQUESTS, _) | (_, Some("rate_limited")) => Failure::Transient {+ error: NotionError::RateLimited {+ retry_after: response.retry_after,+ },+ retry_after: response.retry_after,+ },+ (+ StatusCode::INTERNAL_SERVER_ERROR+ | StatusCode::BAD_GATEWAY+ | StatusCode::SERVICE_UNAVAILABLE+ | StatusCode::GATEWAY_TIMEOUT,+ _,+ ) => Failure::Transient {+ retry_after: response.retry_after,+ error: upstream(response),+ },+ // Notion's overloaded status, outside the standard set.+ (status, _) if status.as_u16() == 529 => Failure::Transient {+ retry_after: response.retry_after,+ error: upstream(response),+ },+ (StatusCode::NOT_FOUND, _) | (_, Some("object_not_found")) => {+ Failure::Final(NotionError::NotFound)+ }+ (StatusCode::FORBIDDEN, _) | (_, Some("restricted_resource")) => {+ Failure::Final(NotionError::Forbidden)+ }+ _ => Failure::Final(upstream(response)),+ }+}++fn upstream(response: TransportResponse) -> NotionError {+ // Notion explains itself in `message`; keep that rather than the JSON+ // around it, which is no use to a reader.+ let explained = serde_json::from_str::<WireError>(&response.body)+ .ok()+ .and_then(|error| error.message)+ .filter(|message| !message.trim().is_empty());+ let body = explained.unwrap_or(response.body);+ NotionError::Upstream {+ status: response.status.as_u16(),+ body: body.chars().take(ERROR_BODY_CHARS).collect(),+ }+}++/// The delay before attempt `attempt + 1`: exponential, capped, jittered.+fn backoff(attempt: u8) -> Duration {+ let exponential = BASE_BACKOFF.saturating_mul(1 << u32::from(attempt.saturating_sub(1)));+ let jitter = Duration::from_millis(rand::rng().random_range(0..=MAX_JITTER_MILLIS));+ exponential.min(MAX_BACKOFF) + jitter+}++fn skip(item: &serde_json::Value, error: &dyn std::fmt::Display) {+ let id = item.get("id").and_then(serde_json::Value::as_str);+ tracing::warn!(id, error = %error, "skipping a Notion object the importer cannot read");+}++#[cfg(test)]+mod test;diff --git a/crates/import_notion/src/outbound/rest/test.rs b/crates/import_notion/src/outbound/rest/test.rsnew file mode 100644index 0000000000..c40d57d8cb--- /dev/null+++ b/crates/import_notion/src/outbound/rest/test.rs@@ -0,0 +1,687 @@+use std::collections::{BTreeMap, VecDeque};+use std::sync::{Arc, Mutex};+use std::time::Duration;++use chrono::{TimeZone, Utc};+use serde_json::json;+use tokio::time::Instant;++use http::{Method, StatusCode};++use super::{+ NOTION_REQUEST_INTERVAL, NotionRead, NotionRestClient, NotionTransport, RequestPacer,+ TransportResponse,+};+use crate::domain::model::{+ NotionDataSource, NotionId, NotionPage, NotionParent, NotionUser, NotionValue, PageMarkdown,+ Paged, SearchHit, SearchObject,+};+use crate::domain::ports::{NotionApi, NotionError};++/// Answers requests in order from a script and records what was sent.+struct ScriptedTransport {+ started: Instant,+ responses: Mutex<VecDeque<TransportResponse>>,+ requests: Mutex<Vec<SentRequest>>,+}++#[derive(Debug, Clone, PartialEq)]+struct SentRequest {+ method: Method,+ path: String,+ body: Option<serde_json::Value>,+ after: Duration,+}++impl ScriptedTransport {+ fn new(responses: Vec<TransportResponse>) -> Arc<Self> {+ Arc::new(Self {+ started: Instant::now(),+ responses: Mutex::new(responses.into()),+ requests: Mutex::new(Vec::new()),+ })+ }++ fn requests(&self) -> Vec<SentRequest> {+ self.requests.lock().unwrap().clone()+ }+}++impl NotionTransport for Arc<ScriptedTransport> {+ async fn send(&self, read: &NotionRead) -> Result<TransportResponse, NotionError> {+ self.requests.lock().unwrap().push(SentRequest {+ method: read.method(),+ path: read.path_and_query(),+ body: read.body().cloned(),+ after: self.started.elapsed(),+ });+ let response = self.responses.lock().unwrap().pop_front();+ Ok(response.expect("the script has a response for every request"))+ }+}++fn ok(body: serde_json::Value) -> TransportResponse {+ TransportResponse {+ status: StatusCode::OK,+ retry_after: None,+ body: body.to_string(),+ }+}++#[tokio::test(start_paused = true)]+async fn search_decodes_pages_and_data_sources_and_skips_what_it_cannot_read() {+ let transport = ScriptedTransport::new(vec![ok(json!({+ "object": "list",+ "results": [+ {+ "object": "page",+ "id": "59833787-2cf9-4fdf-8782-e53db20768a5",+ "created_time": "2026-03-01T09:30:00.000Z",+ "last_edited_time": "2026-03-02T10:15:00.000Z",+ "parent": {"type": "workspace", "workspace": true},+ "in_trash": false,+ "url": "https://www.notion.so/Q3-plan-598337872cf94fdf8782e53db20768a5",+ "properties": {+ "title": {"id": "title", "type": "title", "title": [{"plain_text": "Q3 plan"}]}+ }+ },+ {+ "object": "data_source",+ "id": "1a44be12-0953-4631-a1d6-7d4c2b1b4a2f",+ "title": [{"plain_text": "Roadmap"}],+ "parent": {"type": "database_id", "database_id": "d9824bdc-8445-4327-be8b-5b47500af6ce"},+ "database_parent": {"type": "workspace", "workspace": true},+ "in_trash": true,+ "properties": {+ "Name": {"id": "title", "name": "Name", "type": "title", "title": {}}+ }+ },+ {"object": "page", "id": "not-a-notion-id"},+ {"object": "agent", "id": "c7f0aa50-1f0e-4a52-9a17-e2f6f3a3b0c1"}+ ],+ "next_cursor": "c7f0aa50-1f0e-4a52-9a17-e2f6f3a3b0c1",+ "has_more": true,+ "type": "page_or_data_source",+ "page_or_data_source": {}+ }))]);+ let client = NotionRestClient::new(+ transport.clone(),+ Arc::new(RequestPacer::new(NOTION_REQUEST_INTERVAL)),+ );++ let page = client+ .search(SearchObject::Page, Some("first-cursor".into()))+ .await+ .unwrap();++ assert_eq!(+ page,+ Paged {+ results: vec![+ SearchHit::Page(NotionPage {+ id: NotionId::parse("598337872cf94fdf8782e53db20768a5").unwrap(),+ title: "Q3 plan".into(),+ url: "https://www.notion.so/Q3-plan-598337872cf94fdf8782e53db20768a5".into(),+ parent: NotionParent::Workspace,+ last_edited: Utc.with_ymd_and_hms(2026, 3, 2, 10, 15, 0).unwrap(),+ created: Utc.with_ymd_and_hms(2026, 3, 1, 9, 30, 0).unwrap(),+ in_trash: false,+ properties: BTreeMap::new(),+ }),+ SearchHit::DataSource(NotionDataSource {+ id: NotionId::parse("1a44be1209534631a1d67d4c2b1b4a2f").unwrap(),+ database_id: NotionId::parse("d9824bdc84454327be8b5b47500af6ce").unwrap(),+ title: "Roadmap".into(),+ database_parent: NotionParent::Workspace,+ in_trash: true,+ properties: vec![crate::domain::model::NotionPropertySchema {+ id: "title".into(),+ name: "Name".into(),+ kind: crate::domain::model::NotionPropertyKind::Title,+ }],+ }),+ ],+ next_cursor: Some("c7f0aa50-1f0e-4a52-9a17-e2f6f3a3b0c1".into()),+ }+ );+ assert_eq!(+ transport.requests(),+ vec![SentRequest {+ method: Method::POST,+ path: "/v1/search".into(),+ body: Some(json!({+ "filter": {"property": "object", "value": "page"},+ "sort": {"timestamp": "last_edited_time", "direction": "descending"},+ "page_size": 100,+ "start_cursor": "first-cursor"+ })),+ after: Duration::ZERO,+ }]+ );+}++#[tokio::test(start_paused = true)]+async fn the_last_page_of_a_search_has_no_cursor() {+ let transport = ScriptedTransport::new(vec![ok(json!({+ "object": "list",+ "results": [],+ "next_cursor": null,+ "has_more": false+ }))]);+ let client = NotionRestClient::new(+ transport.clone(),+ Arc::new(RequestPacer::new(NOTION_REQUEST_INTERVAL)),+ );++ let page = client.search(SearchObject::DataSource, None).await.unwrap();++ assert_eq!(+ page,+ Paged {+ results: vec![],+ next_cursor: None,+ }+ );+ assert_eq!(+ transport.requests()[0].body,+ Some(json!({+ "filter": {"property": "object", "value": "data_source"},+ "sort": {"timestamp": "last_edited_time", "direction": "descending"},+ "page_size": 100+ }))+ );+}++#[tokio::test(start_paused = true)]+async fn page_markdown_decodes_the_body_and_its_unknown_blocks() {+ let transport = ScriptedTransport::new(vec![ok(json!({+ "object": "page_markdown",+ "id": "59833787-2cf9-4fdf-8782-e53db20768a5",+ "markdown": "# Q3 plan\n\n<unknown url=\"https://www.notion.so/598337872cf94fdf8782e53db20768a5#c7f0aa501f0e4a529a17e2f6f3a3b0c1\" alt=\"toggle\"/>",+ "truncated": true,+ "unknown_block_ids": ["c7f0aa50-1f0e-4a52-9a17-e2f6f3a3b0c1"]+ }))]);+ let client = NotionRestClient::new(+ transport.clone(),+ Arc::new(RequestPacer::new(NOTION_REQUEST_INTERVAL)),+ );++ let markdown = client+ .page_markdown(&NotionId::parse("598337872cf94fdf8782e53db20768a5").unwrap())+ .await+ .unwrap();++ assert_eq!(+ markdown,+ PageMarkdown {+ markdown: "# Q3 plan\n\n<unknown url=\"https://www.notion.so/598337872cf94fdf8782e53db20768a5#c7f0aa501f0e4a529a17e2f6f3a3b0c1\" alt=\"toggle\"/>".into(),+ unknown_block_ids: vec![NotionId::parse("c7f0aa501f0e4a529a17e2f6f3a3b0c1").unwrap()],+ }+ );+ assert_eq!(+ transport.requests()[0].path,+ "/v1/pages/598337872cf94fdf8782e53db20768a5/markdown"+ );+}++#[tokio::test(start_paused = true)]+async fn a_rate_limit_is_retried_after_the_delay_notion_asked_for() {+ let transport = ScriptedTransport::new(vec![+ TransportResponse {+ status: StatusCode::TOO_MANY_REQUESTS,+ retry_after: Some(Duration::from_secs(7)),+ body: r#"{"object":"error","status":429,"code":"rate_limited","message":"slow down"}"#+ .into(),+ },+ ok(+ json!({"object": "database", "data_sources": [{"id": "1a44be12-0953-4631-a1d6-7d4c2b1b4a2f", "name": "Roadmap"}]}),+ ),+ ]);+ let client = NotionRestClient::new(+ transport.clone(),+ Arc::new(RequestPacer::new(NOTION_REQUEST_INTERVAL)),+ );++ let sources = client+ .database_sources(&NotionId::parse("d9824bdc84454327be8b5b47500af6ce").unwrap())+ .await+ .unwrap();++ assert_eq!(+ sources,+ vec![NotionId::parse("1a44be1209534631a1d67d4c2b1b4a2f").unwrap()]+ );+ let requests = transport.requests();+ assert_eq!(requests.len(), 2);+ assert_eq!(+ requests[0].path,+ "/v1/databases/d9824bdc84454327be8b5b47500af6ce"+ );+ assert_eq!(requests[0].after, Duration::ZERO);+ assert_eq!(requests[1].after, Duration::from_secs(7));+}++#[tokio::test(start_paused = true)]+async fn a_rate_limit_that_persists_fails_after_five_attempts() {+ let rate_limited = TransportResponse {+ status: StatusCode::TOO_MANY_REQUESTS,+ retry_after: Some(Duration::from_secs(1)),+ body: r#"{"object":"error","code":"rate_limited"}"#.into(),+ };+ let transport = ScriptedTransport::new(vec![rate_limited; 5]);+ let client = NotionRestClient::new(+ transport.clone(),+ Arc::new(RequestPacer::new(NOTION_REQUEST_INTERVAL)),+ );++ let error = client+ .page(&NotionId::parse("598337872cf94fdf8782e53db20768a5").unwrap())+ .await+ .unwrap_err();++ assert!(+ matches!(error, NotionError::RateLimited { retry_after: Some(delay) } if delay == Duration::from_secs(1)),+ "{error:?}"+ );+ assert_eq!(transport.requests().len(), 5);+}++#[tokio::test(start_paused = true)]+async fn a_transient_server_error_is_retried_with_backoff() {+ let transport = ScriptedTransport::new(vec![+ TransportResponse {+ status: StatusCode::SERVICE_UNAVAILABLE,+ retry_after: None,+ body: "upstream unavailable".into(),+ },+ ok(json!({"object": "page_markdown", "markdown": "done", "unknown_block_ids": []})),+ ]);+ let client = NotionRestClient::new(+ transport.clone(),+ Arc::new(RequestPacer::new(NOTION_REQUEST_INTERVAL)),+ );++ let markdown = client+ .page_markdown(&NotionId::parse("598337872cf94fdf8782e53db20768a5").unwrap())+ .await+ .unwrap();++ assert_eq!(markdown.markdown, "done");+ let requests = transport.requests();+ assert_eq!(requests.len(), 2);+ // The first backoff is half a second plus at most a quarter second of+ // jitter.+ assert!(+ requests[1].after >= Duration::from_millis(500),+ "{requests:?}"+ );+ assert!(+ requests[1].after <= Duration::from_millis(750),+ "{requests:?}"+ );+}++#[tokio::test(start_paused = true)]+async fn not_found_and_forbidden_are_final() {+ let transport = ScriptedTransport::new(vec![+ TransportResponse {+ status: StatusCode::NOT_FOUND,+ retry_after: None,+ body: r#"{"object":"error","status":404,"code":"object_not_found","message":"Could not find page"}"#.into(),+ },+ TransportResponse {+ status: StatusCode::FORBIDDEN,+ retry_after: None,+ body: r#"{"object":"error","status":403,"code":"restricted_resource","message":"no access"}"#.into(),+ },+ TransportResponse {+ status: StatusCode::BAD_REQUEST,+ retry_after: None,+ body: format!(r#"{{"object":"error","code":"validation_error","message":"{}"}}"#, "x".repeat(600)),+ },+ ]);+ let client = NotionRestClient::new(+ transport.clone(),+ Arc::new(RequestPacer::new(NOTION_REQUEST_INTERVAL)),+ );+ let id = NotionId::parse("598337872cf94fdf8782e53db20768a5").unwrap();++ let not_found = client.page(&id).await.unwrap_err();+ let forbidden = client.data_source(&id).await.unwrap_err();+ let invalid = client.page_markdown(&id).await.unwrap_err();++ assert!(matches!(not_found, NotionError::NotFound), "{not_found:?}");+ assert!(matches!(forbidden, NotionError::Forbidden), "{forbidden:?}");+ match invalid {+ NotionError::Upstream { status, body } => {+ assert_eq!(status, 400);+ assert_eq!(body.chars().count(), 500);+ }+ other => panic!("expected an upstream error, got {other:?}"),+ }+ assert_eq!(transport.requests().len(), 3);+}++#[tokio::test(start_paused = true)]+async fn concurrent_requests_on_one_connection_are_spaced_out() {+ let empty_markdown =+ ok(json!({"object": "page_markdown", "markdown": "", "unknown_block_ids": []}));+ let transport = ScriptedTransport::new(vec![empty_markdown; 3]);+ let pacer = Arc::new(RequestPacer::new(NOTION_REQUEST_INTERVAL));+ let first = NotionRestClient::new(transport.clone(), pacer.clone());+ let second = NotionRestClient::new(transport.clone(), pacer);+ let id = NotionId::parse("598337872cf94fdf8782e53db20768a5").unwrap();++ let (a, b, c) = tokio::join!(+ first.page_markdown(&id),+ second.page_markdown(&id),+ first.page_markdown(&id)+ );++ a.unwrap();+ b.unwrap();+ c.unwrap();+ let times: Vec<Duration> = transport+ .requests()+ .into_iter()+ .map(|request| request.after)+ .collect();+ assert_eq!(+ times,+ vec![+ Duration::ZERO,+ NOTION_REQUEST_INTERVAL,+ NOTION_REQUEST_INTERVAL * 2+ ]+ );+}++#[tokio::test(start_paused = true)]+async fn users_skip_bots_and_a_forbidden_listing_is_empty() {+ let transport = ScriptedTransport::new(vec![+ ok(json!({+ "object": "list",+ "results": [+ {"object": "user", "id": "ee5f0f84-409a-440f-983a-a5315961c6e4", "type": "person", "name": "Ada", "person": {"email": "[email protected]"}},+ {"object": "user", "id": "c7f0aa50-1f0e-4a52-9a17-e2f6f3a3b0c1", "type": "person", "name": "Grace", "person": {}},+ {"object": "user", "id": "9a3b5ae0-c6e6-482d-b0e1-ed315ee6dc57", "type": "bot", "name": "Importer", "bot": {}}+ ],+ "next_cursor": "next-users",+ "has_more": true+ })),+ TransportResponse {+ status: StatusCode::FORBIDDEN,+ retry_after: None,+ body: r#"{"object":"error","status":403,"code":"restricted_resource"}"#.into(),+ },+ ]);+ let client = NotionRestClient::new(+ transport.clone(),+ Arc::new(RequestPacer::new(NOTION_REQUEST_INTERVAL)),+ );++ let first = client.users(None).await.unwrap();+ let second = client.users(first.next_cursor.clone()).await.unwrap();++ assert_eq!(+ first,+ Paged {+ results: vec![+ NotionUser {+ id: NotionId::parse("ee5f0f84409a440f983aa5315961c6e4").unwrap(),+ name: Some("Ada".into()),+ email: Some("[email protected]".into()),+ },+ NotionUser {+ id: NotionId::parse("c7f0aa501f0e4a529a17e2f6f3a3b0c1").unwrap(),+ name: Some("Grace".into()),+ email: None,+ },+ ],+ next_cursor: Some("next-users".into()),+ }+ );+ assert_eq!(+ second,+ Paged {+ results: vec![],+ next_cursor: None,+ }+ );+ let paths: Vec<String> = transport+ .requests()+ .into_iter()+ .map(|request| request.path)+ .collect();+ assert_eq!(+ paths,+ vec![+ "/v1/users?page_size=100".to_owned(),+ "/v1/users?page_size=100&start_cursor=next-users".to_owned(),+ ]+ );+}++#[tokio::test(start_paused = true)]+async fn a_query_past_notions_result_cap_restarts_from_the_last_creation_time() {+ let row = |id: &str, created: &str| {+ json!({+ "object": "page",+ "id": id,+ "created_time": created,+ "last_edited_time": created,+ "parent": {"type": "data_source_id", "data_source_id": "1a44be12-0953-4631-a1d6-7d4c2b1b4a2f", "database_id": "d9824bdc-8445-4327-be8b-5b47500af6ce"},+ "url": "https://www.notion.so/row",+ "properties": {}+ })+ };+ let transport = ScriptedTransport::new(vec![+ ok(json!({+ "object": "list",+ "results": [row("598337872cf94fdf8782e53db20768a5", "2026-01-01T00:00:00.000Z")],+ "next_cursor": "notion-cursor",+ "has_more": true+ })),+ ok(json!({+ "object": "list",+ "results": [row("c7f0aa501f0e4a529a17e2f6f3a3b0c1", "2026-02-01T12:00:00.000Z")],+ "next_cursor": null,+ "has_more": false,+ "request_status": {"type": "incomplete", "incomplete_reason": "query_result_limit_reached"}+ })),+ ok(json!({+ "object": "list",+ "results": [+ row("c7f0aa501f0e4a529a17e2f6f3a3b0c1", "2026-02-01T12:00:00.000Z"),+ row("ee5f0f84409a440f983aa5315961c6e4", "2026-02-02T00:00:00.000Z")+ ],+ "next_cursor": null,+ "has_more": false+ })),+ ]);+ let client = NotionRestClient::new(+ transport.clone(),+ Arc::new(RequestPacer::new(NOTION_REQUEST_INTERVAL)),+ );+ let source = NotionId::parse("1a44be1209534631a1d67d4c2b1b4a2f").unwrap();++ let rows = crate::domain::inventory::all_rows(&client, &source)+ .await+ .unwrap();++ let ids: Vec<&str> = rows.iter().map(|row| row.id.as_str()).collect();+ assert_eq!(+ ids,+ vec![+ "598337872cf94fdf8782e53db20768a5",+ "c7f0aa501f0e4a529a17e2f6f3a3b0c1",+ "ee5f0f84409a440f983aa5315961c6e4",+ ]+ );+ let bodies: Vec<Option<serde_json::Value>> = transport+ .requests()+ .into_iter()+ .map(|request| request.body)+ .collect();+ assert_eq!(+ bodies,+ vec![+ Some(json!({+ "page_size": 100,+ "sorts": [{"timestamp": "created_time", "direction": "ascending"}]+ })),+ Some(json!({+ "page_size": 100,+ "sorts": [{"timestamp": "created_time", "direction": "ascending"}],+ "start_cursor": "notion-cursor"+ })),+ Some(json!({+ "page_size": 100,+ "sorts": [{"timestamp": "created_time", "direction": "ascending"}],+ "filter": {"timestamp": "created_time", "created_time": {"on_or_after": "2026-02-01T12:00:00.000Z"}}+ })),+ ]+ );+}++#[tokio::test(start_paused = true)]+async fn a_truncated_relation_is_fetched_in_full() {+ let transport = ScriptedTransport::new(vec![+ ok(json!({+ "object": "page",+ "id": "59833787-2cf9-4fdf-8782-e53db20768a5",+ "created_time": "2026-03-01T09:30:00.000Z",+ "last_edited_time": "2026-03-01T09:30:00.000Z",+ "parent": {"type": "page_id", "page_id": "c7f0aa50-1f0e-4a52-9a17-e2f6f3a3b0c1"},+ "url": "https://www.notion.so/598337872cf94fdf8782e53db20768a5",+ "properties": {+ "Projects": {"id": "pJ%3B", "type": "relation", "relation": [{"id": "a1b2c3d4-0000-4000-8000-000000000001"}], "has_more": true}+ }+ })),+ ok(json!({+ "object": "list",+ "results": [+ {"object": "property_item", "id": "pJ%3B", "type": "relation", "relation": {"id": "a1b2c3d4-0000-4000-8000-000000000001"}}+ ],+ "next_cursor": "more",+ "has_more": true,+ "type": "property_item",+ "property_item": {"id": "pJ%3B", "type": "relation", "relation": {}}+ })),+ ok(json!({+ "object": "list",+ "results": [+ {"object": "property_item", "id": "pJ%3B", "type": "relation", "relation": {"id": "a1b2c3d4-0000-4000-8000-000000000002"}}+ ],+ "next_cursor": null,+ "has_more": false+ })),+ ]);+ let client = NotionRestClient::new(+ transport.clone(),+ Arc::new(RequestPacer::new(NOTION_REQUEST_INTERVAL)),+ );++ let page = client+ .page(&NotionId::parse("598337872cf94fdf8782e53db20768a5").unwrap())+ .await+ .unwrap();++ assert_eq!(+ page.properties.get("Projects"),+ Some(&NotionValue::Relation(vec![+ NotionId::parse("a1b2c3d4000040008000000000000001").unwrap(),+ NotionId::parse("a1b2c3d4000040008000000000000002").unwrap(),+ ]))+ );+ let paths: Vec<String> = transport+ .requests()+ .into_iter()+ .map(|request| request.path)+ .collect();+ assert_eq!(+ paths,+ vec![+ "/v1/pages/598337872cf94fdf8782e53db20768a5".to_owned(),+ "/v1/pages/598337872cf94fdf8782e53db20768a5/properties/pJ%3B?page_size=100".to_owned(),+ "/v1/pages/598337872cf94fdf8782e53db20768a5/properties/pJ%3B?page_size=100&start_cursor=more".to_owned(),+ ]+ );+}++/// Every request the client can build, named once per variant so a new one+/// fails to compile here until it is checked.+fn every_read() -> Vec<NotionRead> {+ let id = NotionId::parse("598337872cf94fdf8782e53db20768a5").unwrap();+ let reads = vec![+ NotionRead::Search(json!({})),+ NotionRead::Page(id.clone()),+ NotionRead::PageMarkdown(id.clone()),+ NotionRead::PageProperty {+ page: id.clone(),+ property: "title".into(),+ cursor: Some("next".into()),+ },+ NotionRead::DataSource(id.clone()),+ NotionRead::QueryDataSource {+ id: id.clone(),+ body: json!({}),+ },+ NotionRead::Database(id.clone()),+ NotionRead::Users { cursor: None },+ ];+ for read in &reads {+ match read {+ NotionRead::Search(_)+ | NotionRead::Page(_)+ | NotionRead::PageMarkdown(_)+ | NotionRead::PageProperty { .. }+ | NotionRead::DataSource(_)+ | NotionRead::QueryDataSource { .. }+ | NotionRead::Database(_)+ | NotionRead::Users { .. } => {}+ }+ }+ reads+}++#[test]+fn every_request_the_client_can_send_only_reads_notion() {+ let sent: Vec<(Method, String)> = every_read()+ .iter()+ .map(|read| (read.method(), read.path_and_query()))+ .collect();++ assert_eq!(+ sent,+ vec![+ (Method::POST, "/v1/search".to_owned()),+ (+ Method::GET,+ "/v1/pages/598337872cf94fdf8782e53db20768a5".to_owned()+ ),+ (+ Method::GET,+ "/v1/pages/598337872cf94fdf8782e53db20768a5/markdown".to_owned()+ ),+ (+ Method::GET,+ "/v1/pages/598337872cf94fdf8782e53db20768a5/properties/title?page_size=100&start_cursor=next"+ .to_owned()+ ),+ (+ Method::GET,+ "/v1/data_sources/598337872cf94fdf8782e53db20768a5".to_owned()+ ),+ (+ Method::POST,+ "/v1/data_sources/598337872cf94fdf8782e53db20768a5/query".to_owned()+ ),+ (+ Method::GET,+ "/v1/databases/598337872cf94fdf8782e53db20768a5".to_owned()+ ),+ (Method::GET, "/v1/users?page_size=100".to_owned()),+ ]+ );+}diff --git a/crates/import_notion/src/outbound/rest/wire.rs b/crates/import_notion/src/outbound/rest/wire.rsnew file mode 100644index 0000000000..e0831dcff0--- /dev/null+++ b/crates/import_notion/src/outbound/rest/wire.rs@@ -0,0 +1,685 @@+//! Notion's REST wire shapes, and how they flatten into the domain model.+//!+//! These differ from the domain types in kind, not just in derives: values+//! are tagged by a `type` field naming a sibling field, text is arrays of+//! rich-text runs, and absence is spelled several ways.++use std::collections::BTreeMap;++use chrono::{DateTime, Utc};+use indexmap::IndexMap;+use serde::Deserialize;+use serde::de::DeserializeOwned;++use crate::domain::model::{+ NotionDataSource, NotionFile, NotionId, NotionOption, NotionPage, NotionParent,+ NotionPropertyKind, NotionPropertySchema, NotionUser, NotionValue,+};++/// A paginated list; items stay raw so one odd item can be skipped.+#[derive(Deserialize)]+pub(super) struct WireList {+ #[serde(default)]+ pub results: Vec<serde_json::Value>,+ pub next_cursor: Option<String>,+ #[serde(default)]+ pub has_more: bool,+ pub request_status: Option<WireRequestStatus>,+}++impl WireList {+ /// The cursor of the next page, when Notion says there is one.+ pub fn next(&self) -> Option<String> {+ self.next_cursor.clone().filter(|_| self.has_more)+ }++ /// Whether Notion stopped short of the full result (its 10,000 row cap).+ pub fn incomplete(&self) -> bool {+ self.request_status+ .as_ref()+ .is_some_and(|status| status.kind == "incomplete")+ }+}++#[derive(Deserialize)]+pub(super) struct WireRequestStatus {+ #[serde(rename = "type")]+ kind: String,+}++/// A search result.+#[derive(Deserialize)]+#[serde(tag = "object", rename_all = "snake_case")]+pub(super) enum WireObject {+ Page(WirePage),+ DataSource(WireDataSource),+ #[serde(other)]+ Other,+}++#[derive(Deserialize)]+pub(super) struct WirePage {+ pub id: NotionId,+ #[serde(default)]+ url: String,+ parent: WireParent,+ created_time: DateTime<Utc>,+ last_edited_time: DateTime<Utc>,+ #[serde(default)]+ in_trash: bool,+ #[serde(default)]+ archived: bool,+ #[serde(default)]+ pub properties: BTreeMap<String, WireProperty>,+}++#[derive(Deserialize)]+pub(super) struct WireProperty {+ pub id: String,+ #[serde(flatten)]+ pub value: WireValue,+}++/// A property value, tagged by `type` with the value in the field it names.+#[derive(Deserialize)]+#[serde(tag = "type", rename_all = "snake_case")]+pub(super) enum WireValue {+ Title {+ #[serde(default)]+ title: Vec<WireText>,+ },+ RichText {+ #[serde(default)]+ rich_text: Vec<WireText>,+ },+ Number {+ number: Option<f64>,+ },+ Select {+ select: Option<WireName>,+ },+ Status {+ status: Option<WireName>,+ },+ MultiSelect {+ #[serde(default)]+ multi_select: Vec<WireName>,+ },+ Date {+ date: Option<WireDate>,+ },+ People {+ #[serde(default)]+ people: Vec<WireReference>,+ #[serde(default)]+ has_more: bool,+ },+ Files {+ #[serde(default)]+ files: Vec<WireFile>,+ },+ Checkbox {+ #[serde(default)]+ checkbox: bool,+ },+ Url {+ url: Option<String>,+ },+ Email {+ email: Option<String>,+ },+ PhoneNumber {+ phone_number: Option<String>,+ },+ Relation {+ #[serde(default)]+ relation: Vec<WireReference>,+ #[serde(default)]+ has_more: bool,+ },+ Formula {+ formula: WireFormula,+ },+ Rollup {+ rollup: WireRollup,+ },+ CreatedTime {+ created_time: String,+ },+ LastEditedTime {+ last_edited_time: String,+ },+ CreatedBy {+ created_by: WireReference,+ },+ LastEditedBy {+ last_edited_by: WireReference,+ },+ UniqueId {+ unique_id: WireUniqueId,+ },+ #[serde(other)]+ Unsupported,+}++impl WireValue {+ /// Whether Notion truncated this value's references (at 25).+ pub fn truncated(&self) -> bool {+ matches!(+ self,+ Self::People { has_more: true, .. } | Self::Relation { has_more: true, .. }+ )+ }+}++#[derive(Deserialize)]+pub(super) struct WireText {+ #[serde(default)]+ plain_text: String,+}++#[derive(Deserialize)]+pub(super) struct WireName {+ name: String,+}++#[derive(Deserialize)]+pub(super) struct WireDate {+ start: String,+ end: Option<String>,+}++#[derive(Deserialize)]+pub(super) struct WireReference {+ pub id: NotionId,+}++#[derive(Deserialize)]+#[serde(tag = "type", rename_all = "snake_case")]+pub(super) enum WireFile {+ File {+ #[serde(default)]+ name: String,+ file: WireHostedFile,+ },+ External {+ #[serde(default)]+ name: String,+ external: WireExternalFile,+ },+ #[serde(other)]+ Other,+}++#[derive(Deserialize)]+pub(super) struct WireHostedFile {+ url: String,+ expiry_time: Option<DateTime<Utc>>,+}++#[derive(Deserialize)]+pub(super) struct WireExternalFile {+ url: String,+}++#[derive(Deserialize)]+#[serde(tag = "type", rename_all = "snake_case")]+pub(super) enum WireFormula {+ String {+ string: Option<String>,+ },+ Number {+ number: Option<f64>,+ },+ Boolean {+ boolean: Option<bool>,+ },+ Date {+ date: Option<WireDate>,+ },+ #[serde(other)]+ Other,+}++#[derive(Deserialize)]+#[serde(tag = "type", rename_all = "snake_case")]+pub(super) enum WireRollup {+ Number {+ number: Option<f64>,+ },+ Date {+ date: Option<WireDate>,+ },+ Array {+ #[serde(default)]+ array: Vec<WireValue>,+ },+ #[serde(other)]+ Other,+}++#[derive(Deserialize)]+pub(super) struct WireUniqueId {+ number: Option<u64>,+ prefix: Option<String>,+}++/// One item of a paginated property-item list, for truncated references.+#[derive(Deserialize)]+#[serde(tag = "type", rename_all = "snake_case")]+pub(super) enum WirePropertyItem {+ Relation {+ relation: WireReference,+ },+ People {+ people: WireReference,+ },+ #[serde(other)]+ Other,+}++impl WirePropertyItem {+ pub fn reference(self) -> Option<NotionId> {+ match self {+ Self::Relation { relation } => Some(relation.id),+ Self::People { people } => Some(people.id),+ Self::Other => None,+ }+ }+}++#[derive(Deserialize)]+#[serde(tag = "type", rename_all = "snake_case")]+pub(super) enum WireParent {+ PageId {+ page_id: NotionId,+ },+ DatabaseId {+ database_id: NotionId,+ },+ DataSourceId {+ data_source_id: NotionId,+ database_id: NotionId,+ },+ Workspace,+ BlockId {+ block_id: NotionId,+ },+ #[serde(other)]+ Other,+}++#[derive(Deserialize)]+pub(super) struct WireDataSource {+ id: NotionId,+ #[serde(default)]+ title: Vec<WireText>,+ parent: WireParent,+ database_parent: Option<WireParent>,+ #[serde(default)]+ in_trash: bool,+ #[serde(default)]+ archived: bool,+ #[serde(default)]+ properties: IndexMap<String, WireSchemaProperty>,+}++#[derive(Deserialize)]+struct WireSchemaProperty {+ id: String,+ name: String,+ #[serde(rename = "type")]+ kind: String,+ #[serde(flatten)]+ config: serde_json::Map<String, serde_json::Value>,+}++#[derive(Deserialize, Default)]+struct WireOptions {+ #[serde(default)]+ options: Vec<WireOption>,+}++#[derive(Deserialize)]+struct WireOption {+ #[serde(default)]+ id: String,+ name: String,+ #[serde(default)]+ color: String,+}++#[derive(Deserialize, Default)]+struct WireRelationConfig {+ data_source_id: Option<NotionId>,+}++#[derive(Deserialize, Default)]+struct WireUniqueIdConfig {+ prefix: Option<String>,+}++#[derive(Deserialize)]+pub(super) struct WireDatabase {+ #[serde(default)]+ pub data_sources: Vec<WireReference>,+}++#[derive(Deserialize)]+pub(super) struct WireMarkdown {+ pub markdown: String,+ #[serde(default)]+ pub unknown_block_ids: Vec<NotionId>,+}++#[derive(Deserialize)]+pub(super) struct WireUser {+ id: NotionId,+ #[serde(rename = "type")]+ kind: Option<String>,+ name: Option<String>,+ person: Option<WirePerson>,+}++#[derive(Deserialize)]+struct WirePerson {+ email: Option<String>,+}++/// Notion's error body.+#[derive(Deserialize)]+pub(super) struct WireError {+ pub code: Option<String>,+ pub message: Option<String>,+}++impl From<WireParent> for NotionParent {+ fn from(parent: WireParent) -> Self {+ match parent {+ WireParent::PageId { page_id } => Self::Page { id: page_id },+ WireParent::DatabaseId { database_id } => Self::Database { id: database_id },+ WireParent::DataSourceId {+ data_source_id,+ database_id,+ } => Self::DataSource {+ data_source: data_source_id,+ database: database_id,+ },+ WireParent::Workspace => Self::Workspace,+ WireParent::BlockId { block_id } => Self::Block { id: block_id },+ WireParent::Other => Self::Other,+ }+ }+}++impl WirePage {+ /// The page, its title lifted out of the property values.+ pub fn into_page(self) -> NotionPage {+ let mut title = String::new();+ let mut properties = BTreeMap::new();+ for (name, property) in self.properties {+ match property.value {+ WireValue::Title { title: runs } => title = plain_text(&runs),+ value => {+ properties.insert(name, value.into_value());+ }+ }+ }+ NotionPage {+ id: self.id,+ title,+ url: self.url,+ parent: self.parent.into(),+ last_edited: self.last_edited_time,+ created: self.created_time,+ in_trash: self.in_trash || self.archived,+ properties,+ }+ }+}++impl WireValue {+ pub fn into_value(self) -> NotionValue {+ match self {+ Self::Title { title: runs } | Self::RichText { rich_text: runs } => {+ non_empty(plain_text(&runs)).map_or(NotionValue::Empty, NotionValue::Text)+ }+ Self::Number { number } => number.map_or(NotionValue::Empty, NotionValue::Number),+ Self::Select { select: option } | Self::Status { status: option } => option+ .map_or(NotionValue::Empty, |option| {+ NotionValue::Option(option.name)+ }),+ Self::MultiSelect { multi_select } if multi_select.is_empty() => NotionValue::Empty,+ Self::MultiSelect { multi_select } => {+ NotionValue::Options(multi_select.into_iter().map(|option| option.name).collect())+ }+ Self::Date { date } => date_value(date),+ Self::People { people, .. } if people.is_empty() => NotionValue::Empty,+ Self::People { people, .. } => {+ NotionValue::People(people.into_iter().map(|person| person.id).collect())+ }+ Self::Files { files } => {+ let files: Vec<NotionFile> = files.into_iter().filter_map(file).collect();+ if files.is_empty() {+ NotionValue::Empty+ } else {+ NotionValue::Files(files)+ }+ }+ Self::Checkbox { checkbox } => NotionValue::Checkbox(checkbox),+ Self::Url { url } => url+ .and_then(non_empty)+ .map_or(NotionValue::Empty, NotionValue::Url),+ Self::Email { email } => email+ .and_then(non_empty)+ .map_or(NotionValue::Empty, NotionValue::Email),+ Self::PhoneNumber { phone_number } => phone_number+ .and_then(non_empty)+ .map_or(NotionValue::Empty, NotionValue::Phone),+ Self::Relation { relation, .. } if relation.is_empty() => NotionValue::Empty,+ Self::Relation { relation, .. } => {+ NotionValue::Relation(relation.into_iter().map(|page| page.id).collect())+ }+ Self::Formula { formula } => match formula {+ WireFormula::String { string } => string+ .and_then(non_empty)+ .map_or(NotionValue::Empty, NotionValue::Text),+ WireFormula::Number { number } => {+ number.map_or(NotionValue::Empty, NotionValue::Number)+ }+ WireFormula::Boolean { boolean } => {+ boolean.map_or(NotionValue::Empty, NotionValue::Checkbox)+ }+ WireFormula::Date { date } => date_value(date),+ WireFormula::Other => NotionValue::Empty,+ },+ Self::Rollup { rollup } => match rollup {+ WireRollup::Number { number } => {+ number.map_or(NotionValue::Empty, NotionValue::Number)+ }+ WireRollup::Date { date } => date_value(date),+ WireRollup::Array { array } => {+ let values: Vec<NotionValue> = array+ .into_iter()+ .map(Self::into_value)+ .filter(|value| *value != NotionValue::Empty)+ .collect();+ if values.is_empty() {+ NotionValue::Empty+ } else {+ NotionValue::List(values)+ }+ }+ WireRollup::Other => NotionValue::Empty,+ },+ Self::CreatedTime { created_time: time }+ | Self::LastEditedTime {+ last_edited_time: time,+ } => NotionValue::Timestamp(time),+ Self::CreatedBy { created_by: user }+ | Self::LastEditedBy {+ last_edited_by: user,+ } => NotionValue::People(vec![user.id]),+ Self::UniqueId { unique_id } => match (unique_id.number, unique_id.prefix) {+ (None, _) => NotionValue::Empty,+ (Some(number), Some(prefix)) if !prefix.is_empty() => {+ NotionValue::UniqueId(format!("{prefix}-{number}"))+ }+ (Some(number), _) => NotionValue::UniqueId(number.to_string()),+ },+ Self::Unsupported => NotionValue::Empty,+ }+ }+}++impl WireDataSource {+ pub fn into_data_source(self) -> Result<NotionDataSource, String> {+ let WireParent::DatabaseId { database_id } = self.parent else {+ return Err(format!("data source {} is not under a database", self.id));+ };+ let properties = self+ .properties+ .into_values()+ .map(|property| {+ let kind = property_kind(&property.kind, &property.config)+ .map_err(|error| format!("property {}: {error}", property.name))?;+ Ok(NotionPropertySchema {+ id: property.id,+ name: property.name,+ kind,+ })+ })+ .collect::<Result<Vec<_>, String>>()?;+ Ok(NotionDataSource {+ id: self.id,+ database_id,+ title: plain_text(&self.title),+ database_parent: self+ .database_parent+ .map_or(NotionParent::Other, NotionParent::from),+ in_trash: self.in_trash || self.archived,+ properties,+ })+ }+}++impl WireUser {+ /// The member, or `None` for a bot.+ pub fn into_user(self) -> Option<NotionUser> {+ if self.kind.as_deref() == Some("bot") {+ return None;+ }+ Some(NotionUser {+ id: self.id,+ name: self.name.and_then(non_empty),+ email: self+ .person+ .and_then(|person| person.email)+ .and_then(non_empty),+ })+ }+}++fn property_kind(+ kind: &str,+ config: &serde_json::Map<String, serde_json::Value>,+) -> Result<NotionPropertyKind, serde_json::Error> {+ Ok(match kind {+ "title" => NotionPropertyKind::Title,+ "rich_text" => NotionPropertyKind::RichText,+ "number" => NotionPropertyKind::Number,+ "select" => NotionPropertyKind::Select {+ options: options(config, kind)?,+ },+ "multi_select" => NotionPropertyKind::MultiSelect {+ options: options(config, kind)?,+ },+ "status" => NotionPropertyKind::Status {+ options: options(config, kind)?,+ },+ "date" => NotionPropertyKind::Date,+ "people" => NotionPropertyKind::People,+ "files" => NotionPropertyKind::Files,+ "checkbox" => NotionPropertyKind::Checkbox,+ "url" => NotionPropertyKind::Url,+ "email" => NotionPropertyKind::Email,+ "phone_number" => NotionPropertyKind::PhoneNumber,+ "formula" => NotionPropertyKind::Formula,+ "relation" => NotionPropertyKind::Relation {+ data_source_id: type_config::<WireRelationConfig>(config, kind)?.data_source_id,+ },+ "rollup" => NotionPropertyKind::Rollup,+ "created_time" => NotionPropertyKind::CreatedTime,+ "created_by" => NotionPropertyKind::CreatedBy,+ "last_edited_time" => NotionPropertyKind::LastEditedTime,+ "last_edited_by" => NotionPropertyKind::LastEditedBy,+ "unique_id" => NotionPropertyKind::UniqueId {+ prefix: type_config::<WireUniqueIdConfig>(config, kind)?+ .prefix+ .and_then(non_empty),+ },+ other => NotionPropertyKind::Unsupported {+ notion_type: other.to_owned(),+ },+ })+}++fn options(+ config: &serde_json::Map<String, serde_json::Value>,+ kind: &str,+) -> Result<Vec<NotionOption>, serde_json::Error> {+ Ok(type_config::<WireOptions>(config, kind)?+ .options+ .into_iter()+ .map(|option| NotionOption {+ id: option.id,+ name: option.name,+ color: option.color,+ })+ .collect())+}++/// The configuration object in the field named after the property's type.+fn type_config<Config: DeserializeOwned + Default>(+ config: &serde_json::Map<String, serde_json::Value>,+ kind: &str,+) -> Result<Config, serde_json::Error> {+ match config.get(kind) {+ None | Some(serde_json::Value::Null) => Ok(Config::default()),+ Some(value) => Config::deserialize(value),+ }+}++fn file(file: WireFile) -> Option<NotionFile> {+ match file {+ WireFile::File { name, file } => Some(NotionFile {+ name,+ url: file.url,+ expires: file.expiry_time,+ }),+ WireFile::External { name, external } => Some(NotionFile {+ name,+ url: external.url,+ expires: None,+ }),+ WireFile::Other => None,+ }+}++fn date_value(date: Option<WireDate>) -> NotionValue {+ date.map_or(NotionValue::Empty, |date| NotionValue::Date {+ start: date.start,+ end: date.end,+ })+}++fn plain_text(runs: &[WireText]) -> String {+ runs.iter().map(|run| run.plain_text.as_str()).collect()+}++fn non_empty(text: String) -> Option<String> {+ (!text.is_empty()).then_some(text)+}++#[cfg(test)]+mod test;diff --git a/crates/import_notion/src/outbound/rest/wire/test.rs b/crates/import_notion/src/outbound/rest/wire/test.rsnew file mode 100644index 0000000000..caa655d84d--- /dev/null+++ b/crates/import_notion/src/outbound/rest/wire/test.rs@@ -0,0 +1,203 @@+use std::collections::BTreeMap;++use chrono::{TimeZone, Utc};++use super::{WireDataSource, WirePage};+use crate::domain::model::{+ NotionDataSource, NotionFile, NotionId, NotionOption, NotionPage, NotionParent,+ NotionPropertyKind, NotionPropertySchema, NotionValue,+};++#[test]+fn a_page_with_every_property_type_flattens_into_values() {+ let page: WirePage =+ serde_json::from_str(include_str!("../../../../fixtures/page.json")).unwrap();++ let owner = NotionId::parse("ee5f0f84-409a-440f-983a-a5315961c6e4").unwrap();+ assert_eq!(+ page.into_page(),+ NotionPage {+ id: NotionId::parse("598337872cf94fdf8782e53db20768a5").unwrap(),+ title: "Launch plan".into(),+ url: "https://www.notion.so/Launch-plan-598337872cf94fdf8782e53db20768a5".into(),+ parent: NotionParent::DataSource {+ data_source: NotionId::parse("1a44be1209534631a1d67d4c2b1b4a2f").unwrap(),+ database: NotionId::parse("d9824bdc84454327be8b5b47500af6ce").unwrap(),+ },+ last_edited: Utc.with_ymd_and_hms(2026, 3, 2, 10, 15, 0).unwrap(),+ created: Utc.with_ymd_and_hms(2026, 3, 1, 9, 30, 0).unwrap(),+ in_trash: false,+ properties: BTreeMap::from([+ ("Notes".into(), NotionValue::Text("Ship it".into())),+ ("Empty notes".into(), NotionValue::Empty),+ ("Budget".into(), NotionValue::Number(1250.5)),+ ("No budget".into(), NotionValue::Empty),+ ("Priority".into(), NotionValue::Option("High".into())),+ ("No priority".into(), NotionValue::Empty),+ (+ "Tags".into(),+ NotionValue::Options(vec!["web".into(), "ios".into()]),+ ),+ ("Status".into(), NotionValue::Option("In progress".into())),+ (+ "Due".into(),+ NotionValue::Date {+ start: "2026-04-01".into(),+ end: Some("2026-04-03".into()),+ },+ ),+ ("Owner".into(), NotionValue::People(vec![owner.clone()])),+ (+ "Attachments".into(),+ NotionValue::Files(vec![+ NotionFile {+ name: "spec.pdf".into(),+ url: "https://prod-files.notion-static.com/spec.pdf".into(),+ expires: Some(Utc.with_ymd_and_hms(2026, 3, 2, 11, 15, 0).unwrap()),+ },+ NotionFile {+ name: "mock".into(),+ url: "https://example.com/mock.png".into(),+ expires: None,+ },+ ]),+ ),+ ("Done".into(), NotionValue::Checkbox(true)),+ ("Link".into(), NotionValue::Url("https://macro.com".into())),+ (+ "Contact".into(),+ NotionValue::Email("[email protected]".into())+ ),+ ("Phone".into(), NotionValue::Empty),+ (+ "Projects".into(),+ NotionValue::Relation(vec![+ NotionId::parse("a1b2c3d4-0000-4000-8000-000000000001").unwrap()+ ]),+ ),+ ("Score".into(), NotionValue::Number(42.0)),+ ("Label".into(), NotionValue::Text("high-42".into())),+ ("Total".into(), NotionValue::Number(7.0)),+ (+ "Project names".into(),+ NotionValue::List(vec![NotionValue::Text("Alpha".into())]),+ ),+ (+ "Created".into(),+ NotionValue::Timestamp("2026-03-01T09:30:00.000Z".into()),+ ),+ (+ "Created by".into(),+ NotionValue::People(vec![owner.clone()])+ ),+ (+ "Edited".into(),+ NotionValue::Timestamp("2026-03-02T10:15:00.000Z".into()),+ ),+ ("Edited by".into(), NotionValue::People(vec![owner])),+ ("Ticket".into(), NotionValue::UniqueId("TASK-42".into())),+ ("Run".into(), NotionValue::Empty),+ ("Verified".into(), NotionValue::Empty),+ ]),+ }+ );+}++#[test]+fn a_data_source_schema_keeps_notion_column_order_and_configuration() {+ let source: WireDataSource =+ serde_json::from_str(include_str!("../../../../fixtures/data_source.json")).unwrap();++ assert_eq!(+ source.into_data_source().unwrap(),+ NotionDataSource {+ id: NotionId::parse("1a44be1209534631a1d67d4c2b1b4a2f").unwrap(),+ database_id: NotionId::parse("d9824bdc84454327be8b5b47500af6ce").unwrap(),+ title: "Roadmap".into(),+ database_parent: NotionParent::Page {+ id: NotionId::parse("c7f0aa501f0e4a529a17e2f6f3a3b0c1").unwrap(),+ },+ in_trash: false,+ properties: vec![+ NotionPropertySchema {+ id: "title".into(),+ name: "Name".into(),+ kind: NotionPropertyKind::Title,+ },+ NotionPropertySchema {+ id: "pR".into(),+ name: "Priority".into(),+ kind: NotionPropertyKind::Select {+ options: vec![+ NotionOption {+ id: "o1".into(),+ name: "High".into(),+ color: "red".into(),+ },+ NotionOption {+ id: "o2".into(),+ name: "Low".into(),+ color: "green".into(),+ },+ ],+ },+ },+ NotionPropertySchema {+ id: "sT".into(),+ name: "Status".into(),+ kind: NotionPropertyKind::Status {+ options: vec![NotionOption {+ id: "s1".into(),+ name: "In progress".into(),+ color: "blue".into(),+ }],+ },+ },+ NotionPropertySchema {+ id: "pJ".into(),+ name: "Projects".into(),+ kind: NotionPropertyKind::Relation {+ data_source_id: Some(+ NotionId::parse("bbbb0000-0000-4000-8000-000000000000").unwrap()+ ),+ },+ },+ NotionPropertySchema {+ id: "tK".into(),+ name: "Ticket".into(),+ kind: NotionPropertyKind::UniqueId {+ prefix: Some("TASK".into()),+ },+ },+ NotionPropertySchema {+ id: "bU".into(),+ name: "Budget".into(),+ kind: NotionPropertyKind::Number,+ },+ NotionPropertySchema {+ id: "rN".into(),+ name: "Run".into(),+ kind: NotionPropertyKind::Unsupported {+ notion_type: "button".into(),+ },+ },+ ],+ }+ );+}++#[test]+fn a_data_source_outside_a_database_is_unreadable() {+ let source: WireDataSource = serde_json::from_value(serde_json::json!({+ "object": "data_source",+ "id": "1a44be12-0953-4631-a1d6-7d4c2b1b4a2f",+ "parent": {"type": "workspace", "workspace": true},+ "properties": {}+ }))+ .unwrap();++ assert_eq!(+ source.into_data_source(),+ Err("data source 1a44be1209534631a1d67d4c2b1b4a2f is not under a database".into())+ );+}diff --git a/crates/macro_db_client/migrations/20261017002451_settle_automatic_import_runs.sql b/crates/macro_db_client/migrations/20261017002451_settle_automatic_import_runs.sqlnew file mode 100644index 0000000000..6717c77766--- /dev/null+++ b/crates/macro_db_client/migrations/20261017002451_settle_automatic_import_runs.sql@@ -0,0 +1,18 @@+-- Onboarding no longer gathers or imports automatically.+--+-- Settle runs it left mid-import: nothing will finish them, and Slack+-- discovery only restarts terminal runs. Remove the candidates it staged that+-- nobody reviewed and no import attempted, as onboarding completion used to;+-- never-reviewed is not declined, so chat or Slack discovery can stage them+-- again. Failed automatic imports (staged with `last_error`) stay retryable.+-- Rows it left `importing` are reset by the service's stale-import reaper.+-- Runs before import_entity gains its self-referencing foreign key, so the+-- delete fires no cascade checks.+SET LOCAL lock_timeout = '5s';++UPDATE import_run+SET status = 'completed', updated_at = NOW()+WHERE status = 'importing';++DELETE FROM import_entity+WHERE initiator = 'onboarding' AND status = 'staged' AND last_error IS NULL;diff --git a/crates/macro_db_client/migrations/20261017002457_import_entity_kind_and_tree.sql b/crates/macro_db_client/migrations/20261017002457_import_entity_kind_and_tree.sqlnew file mode 100644index 0000000000..28025ac595--- /dev/null+++ b/crates/macro_db_client/migrations/20261017002457_import_entity_kind_and_tree.sql@@ -0,0 +1,19 @@+-- What each imported item is in its source system, the row it was imported+-- under, and the review that approved it. All three columns are nullable, so+-- adding them is a catalog-only change. NULL `kind` reads as the source's+-- original kind (Linear issue, Notion page, Slack channel), which is what+-- every existing row and every row written by older code is. The check is+-- validated separately so this migration never scans the table.+SET LOCAL lock_timeout = '5s';++ALTER TABLE import_entity+ ADD COLUMN kind TEXT,+ ADD COLUMN parent_id UUID REFERENCES import_entity (id) ON DELETE CASCADE,+ ADD COLUMN review_id UUID REFERENCES agent_session_review (id) ON DELETE SET NULL;++ALTER TABLE import_entity ADD CONSTRAINT import_entity_kind_check CHECK (+ kind IS NULL+ OR (source = 'linear' AND kind IN ('issue', 'project', 'document'))+ OR (source = 'notion' AND kind IN ('page', 'database', 'data_source', 'row'))+ OR (source = 'slack' AND kind = 'channel')+) NOT VALID;diff --git a/crates/macro_db_client/migrations/20261017002500_import_entity_team_object_key.sql b/crates/macro_db_client/migrations/20261017002500_import_entity_team_object_key.sqlnew file mode 100644index 0000000000..4580dd6918--- /dev/null+++ b/crates/macro_db_client/migrations/20261017002500_import_entity_team_object_key.sql@@ -0,0 +1,14 @@+-- no-transaction+-- One Macro copy per team for each Notion or Linear object: the first+-- teammate to claim an object (`importing`) or finish it (`imported`) owns+-- it, and later importers link to that copy. Existing Notion and Linear rows+-- never set team_id, so this cannot conflict with current data. Slack keeps+-- its own reservations in import_target_reservation. Built CONCURRENTLY so+-- imports keep writing. A failed build leaves an INVALID index that IF NOT+-- EXISTS would skip; validate_import_entity_kind refuses to finish until the+-- index is valid.+CREATE UNIQUE INDEX CONCURRENTLY IF NOT EXISTS import_entity_team_object_key+ ON import_entity (source, team_id, foreign_id)+ WHERE team_id IS NOT NULL+ AND source IN ('notion', 'linear')+ AND status IN ('importing', 'imported');diff --git a/crates/macro_db_client/migrations/20261017002503_import_entity_parent_index.sql b/crates/macro_db_client/migrations/20261017002503_import_entity_parent_index.sqlnew file mode 100644index 0000000000..2c6841cb29--- /dev/null+++ b/crates/macro_db_client/migrations/20261017002503_import_entity_parent_index.sql@@ -0,0 +1,5 @@+-- no-transaction+-- Backs the parent_id foreign key: deleting an import row looks up its+-- children, which is a full scan without this index.+CREATE INDEX CONCURRENTLY IF NOT EXISTS import_entity_parent_idx+ ON import_entity (parent_id) WHERE parent_id IS NOT NULL;diff --git a/crates/macro_db_client/migrations/20261017002505_import_entity_review_index.sql b/crates/macro_db_client/migrations/20261017002505_import_entity_review_index.sqlnew file mode 100644index 0000000000..7d89618b58--- /dev/null+++ b/crates/macro_db_client/migrations/20261017002505_import_entity_review_index.sql@@ -0,0 +1,5 @@+-- no-transaction+-- Backs the review_id foreign key: deleting a review (with its agent+-- session) clears referencing rows, which is a full scan without this index.+CREATE INDEX CONCURRENTLY IF NOT EXISTS import_entity_review_idx+ ON import_entity (review_id) WHERE review_id IS NOT NULL;diff --git a/crates/macro_db_client/migrations/20261017002508_validate_import_entity_kind.sql b/crates/macro_db_client/migrations/20261017002508_validate_import_entity_kind.sqlnew file mode 100644index 0000000000..27bb0f6de8--- /dev/null+++ b/crates/macro_db_client/migrations/20261017002508_validate_import_entity_kind.sql@@ -0,0 +1,22 @@+-- Scan only after the column migration commits and releases its lock.+ALTER TABLE import_entity VALIDATE CONSTRAINT import_entity_kind_check;++-- A cancelled CONCURRENTLY build leaves an INVALID index that the earlier+-- migrations' IF NOT EXISTS would silently keep; fail loudly instead.+DO $$+BEGIN+ IF EXISTS (+ SELECT 1+ FROM pg_index+ JOIN pg_class ON pg_class.oid = pg_index.indexrelid+ WHERE pg_class.relname IN (+ 'import_entity_team_object_key',+ 'import_entity_parent_idx',+ 'import_entity_review_idx'+ )+ AND NOT pg_index.indisvalid+ ) THEN+ RAISE EXCEPTION 'an import_entity index build did not finish; drop the INVALID index and re-run migrations';+ END IF;+END+$$;diff --git a/crates/macro_db_client/migrations/20261020225110_import_run_scan_counts.sql b/crates/macro_db_client/migrations/20261020225110_import_run_scan_counts.sqlnew file mode 100644index 0000000000..a95758483a--- /dev/null+++ b/crates/macro_db_client/migrations/20261020225110_import_run_scan_counts.sql@@ -0,0 +1,7 @@+-- What a planning run has found in the source so far, shown while it is+-- `running`. Nullable, so code deployed before this change keeps working.+SET LOCAL lock_timeout = '5s';++ALTER TABLE import_run+ ADD COLUMN scanned_pages INTEGER,+ ADD COLUMN scanned_data_sources INTEGER;diff --git a/crates/macro_db_client/migrations/20261020233827_notion_import_run_lease.sql b/crates/macro_db_client/migrations/20261020233827_notion_import_run_lease.sqlnew file mode 100644index 0000000000..a780079eaf--- /dev/null+++ b/crates/macro_db_client/migrations/20261020233827_notion_import_run_lease.sql@@ -0,0 +1,39 @@+-- A Notion workspace import survives the process that runs it.+--+-- The process planning or writing holds a lease on the run, renewed by its+-- heartbeat; when the lease lapses (crash, deploy) another process adopts the+-- run and resumes the job recorded on it: planning restarts with `scope`,+-- writing continues the plan tied to `review_id`. `root_folder_id` is the+-- user's "Notion" folder, reused by every import. `adoptions` counts how+-- often the run was picked up after its process stopped, so a job that+-- keeps killing its process is given up on.+--+-- `import_entity_written` is what a write already created for each ledger+-- row (its folder, document, database, rows, row bodies, tasks), so a+-- resumed write reuses them instead of creating them again.+--+-- Every new column is nullable or defaulted, so code deployed before this+-- change keeps working.+SET LOCAL lock_timeout = '5s';++ALTER TABLE import_run+ ADD COLUMN lease_owner UUID,+ ADD COLUMN lease_expires_at TIMESTAMPTZ,+ ADD COLUMN scope JSONB,+ ADD COLUMN review_id UUID REFERENCES agent_session_review (id) ON DELETE SET NULL,+ ADD COLUMN root_folder_id UUID,+ ADD COLUMN adoptions SMALLINT NOT NULL DEFAULT 0;++-- Removed with its ledger row, which a new plan or a user's cleanup deletes.+CREATE TABLE import_entity_written (+ import_id UUID NOT NULL REFERENCES import_entity (id) ON DELETE CASCADE,+ -- 'folder' | 'document' | 'database' | 'table' | 'title_column' |+ -- 'schema' | 'row' | 'bodies_folder' | 'page_column' | 'body' |+ -- 'tasks_folder' | 'task' | 'relation' | 'relation_linked'+ kind TEXT NOT NULL,+ -- The Notion row or property it is for; empty for the object itself.+ object TEXT NOT NULL DEFAULT '',+ macro_id UUID NOT NULL,+ created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),+ PRIMARY KEY (import_id, kind, object)+);diff --git a/crates/macro_db_client/migrations/20261021162646_notion_import_sharing.sql b/crates/macro_db_client/migrations/20261021162646_notion_import_sharing.sqlnew file mode 100644index 0000000000..644b143c46--- /dev/null+++ b/crates/macro_db_client/migrations/20261021162646_notion_import_sharing.sql@@ -0,0 +1,23 @@+-- Who a Notion workspace import shares with, and rows waiting on a+-- teammate's copy.+--+-- `import_run.sharing` is what the user chose when planning: 'team' (share+-- everything the import writes with their team), 'personal', or 'ai' (the+-- decision model chooses per object). An adopted planning run plans again+-- with it.+--+-- `import_entity.waiting_on` is set on a row whose object a teammate is+-- writing for the team at the same moment: the row stays `importing` until+-- that teammate's row (`waiting_on`) becomes `imported`, then links to the+-- same Macro entity. A teammate's row going back to `staged` releases its+-- waiters back to `staged` too.+--+-- Both columns are nullable, so code deployed before this change keeps+-- working; the index backing the foreign key is built CONCURRENTLY next.+SET LOCAL lock_timeout = '5s';++ALTER TABLE import_run+ ADD COLUMN sharing TEXT CHECK (sharing IN ('team', 'personal', 'ai'));++ALTER TABLE import_entity+ ADD COLUMN waiting_on UUID REFERENCES import_entity (id) ON DELETE SET NULL;diff --git a/crates/macro_db_client/migrations/20261021162648_import_entity_waiting_on_index.sql b/crates/macro_db_client/migrations/20261021162648_import_entity_waiting_on_index.sqlnew file mode 100644index 0000000000..6513ad082f--- /dev/null+++ b/crates/macro_db_client/migrations/20261021162648_import_entity_waiting_on_index.sql@@ -0,0 +1,5 @@+-- no-transaction+-- Backs the waiting_on foreign key and the lookup of a row's waiters when it+-- is imported or released.+CREATE INDEX CONCURRENTLY IF NOT EXISTS import_entity_waiting_on_idx+ ON import_entity (waiting_on) WHERE waiting_on IS NOT NULL;diff --git a/crates/macro_db_client/migrations/20261022045845_import_run_review_session.sql b/crates/macro_db_client/migrations/20261022045845_import_run_review_session.sqlnew file mode 100644index 0000000000..85c3c15604--- /dev/null+++ b/crates/macro_db_client/migrations/20261022045845_import_run_review_session.sql@@ -0,0 +1,13 @@+-- The agent session that presented a Notion import's review.+--+-- When the plan the review shows is ready, and when its write ends, the+-- import produces a system event for this session in the same transaction+-- as the run's transition. A deleted session clears it, so the run still+-- finishes; import_run holds one row per user and source, so the foreign+-- key needs no index.+--+-- Nullable, so code deployed before this change keeps working.+SET LOCAL lock_timeout = '5s';++ALTER TABLE import_run+ ADD COLUMN review_session_id UUID REFERENCES agent_session (id) ON DELETE SET NULL;diff --git a/crates/mcp_client/src/inbound/axum_router.rs b/crates/mcp_client/src/inbound/axum_router.rsindex 51d2595885..74571d590d 100644--- a/crates/mcp_client/src/inbound/axum_router.rs+++ b/crates/mcp_client/src/inbound/axum_router.rs@@ -20,20 +20,12 @@ use utoipa::{IntoParams, ToSchema};#[cfg(test)]mod test;-/// Hook invoked after an OAuth flow completes and the credentials are saved.-/// Hosts use this to react to a connection the moment it exists (e.g. start-/// import gather jobs); implementations must be quick or spawn.-pub type McpAuthCompletedHook = Arc<- dyn Fn(McpServerRecord) -> std::pin::Pin<Box<dyn Future<Output = ()> + Send>> + Send + Sync,->;-/// Shared state for the MCP router.pub struct McpRouterState<S, O, Auth> {store: Arc<S>,oauth: Arc<O>,authorization_state: MacroAuthorizationState<Auth>,client_metadata: OAuthClientMetadata,- on_auth_completed: Option<McpAuthCompletedHook>,}impl<S, O, Auth> Clone for McpRouterState<S, O, Auth> {@@ -43,7 +35,6 @@ impl<S, O, Auth> Clone for McpRouterState<S, O, Auth> {oauth: self.oauth.clone(),authorization_state: self.authorization_state.clone(),client_metadata: self.client_metadata.clone(),- on_auth_completed: self.on_auth_completed.clone(),}}}@@ -73,17 +64,9 @@ whereoauth: Arc::new(oauth),authorization_state,client_metadata,- on_auth_completed: None,}}- /// Invoke `hook` whenever an OAuth flow completes (see- /// [`McpAuthCompletedHook`]).- pub fn with_auth_completed_hook(mut self, hook: McpAuthCompletedHook) -> Self {- self.on_auth_completed = Some(hook);- self- }-/// Access the underlying server store.pub fn store(&self) -> Arc<S> {self.store.clone()@@ -529,16 +512,10 @@ where{let (code, csrf_state) = parse_callback_params(params)?;- let record = state+ state.oauth.exchange_authorization_code(&code, &csrf_state).await?;- // Let the host react to the brand-new connection (e.g. kick off import- // gather jobs) before the user even returns to their original tab.- if let Some(hook) = &state.on_auth_completed {- hook(record).await;- }-Ok("Authorization successful. You can close this tab.".to_string())}diff --git a/crates/memory/src/context.rs b/crates/memory/src/context.rsindex c58f20ffbb..756c35fecd 100644--- a/crates/memory/src/context.rs+++ b/crates/memory/src/context.rs@@ -15,12 +15,14 @@ pub async fn build_tool_service_context(pool: sqlx::PgPool,config: &Config,event_task_tracker: TaskTracker,+ import_jobs: ai_tools::ImportJobs,) -> anyhow::Result<ToolServiceContext> {let recorder =ai_usage::pg_recorder_with_enforcement(pool.clone(), config.enable_ai_usage_enforcement);build_tool_service_context_from_env(pool,event_task_tracker,+ import_jobs,config.enable_ai_usage_enforcement,config.ai_pricing,recorder,diff --git a/crates/memory/src/main.rs b/crates/memory/src/main.rsindex 405f2ae476..502245ce5a 100644--- a/crates/memory/src/main.rs+++ b/crates/memory/src/main.rs@@ -28,8 +28,14 @@ async fn main() -> anyhow::Result<()> {.await?;let event_broker_tracker = TaskTracker::new();- let tool_context =- build_tool_service_context(pool.clone(), &config, event_broker_tracker.clone()).await?;+ let import_jobs = ai_tools::ImportJobs::new();+ let tool_context = build_tool_service_context(+ pool.clone(),+ &config,+ event_broker_tracker.clone(),+ import_jobs.clone(),+ )+ .await?;let tools = tools_for(AiHost::Chat);let memory_repo = PgMemoryRepo::new(pool);let memory_service = MemoryServiceImpl::new(memory_repo, tool_context, tools);@@ -37,6 +43,8 @@ async fn main() -> anyhow::Result<()> {tracing::info!("Generating memory for {user}...");let memory_result = memory_service.get_or_generate_memory(user).await;+ import_jobs.hand_over().await;+tracing::info!("waiting for event broker publishes to drain");event_broker_tracker.close();match tokio::time::timeout(EVENT_BROKER_DRAIN_TIMEOUT, event_broker_tracker.wait()).await {diff --git a/crates/onboarding/AGENTS.md b/crates/onboarding/AGENTS.mdindex 6edd15d1c5..a3144ac7a5 100644--- a/crates/onboarding/AGENTS.md+++ b/crates/onboarding/AGENTS.md@@ -3,13 +3,13 @@## What it isPart of the onboarding feature (`/setup`). Tracks one row per user for-whether they are still in the flow, and starts import gather runs when-connectors authenticate during onboarding.+whether they are still in the flow, and assembles the state the flow reads+(connected MCP servers, a suggested team domain). Connecting an app during+onboarding does not start an import.## Start here-- `src/lib.rs`: crate doc comment — read it first, it states the boundary- with `import` precisely.+- `src/lib.rs`: crate doc comment.- `src/domain/service.rs` + `src/domain/ports.rs`: flow state transitions andthe ports they need.- `src/outbound/pg_onboarding_repo.rs`: the single Postgres adapter.@@ -17,15 +17,12 @@ connectors authenticate during onboarding.## Scope-- Owns: the per-user onboarding flag/state and starting auto-import gathers- while onboarding is active.-- Not here: the import machinery itself (staging, dedup, gather/import jobs)- → `import`.+- Owns: the per-user onboarding flag/state.+- Not here: importing from connected apps → `import`.## Boundaries-- Depends on: `import`, `mcp_client`, `mcp_select`, `generic_email_domains`,- `macro_authorization`.+- Depends on: `mcp_client`, `generic_email_domains`, `macro_authorization`.- Used by: `services/agent_harness_service`,`services/authentication_service` (onboarding deletes its row duringaccount deletion).diff --git a/crates/onboarding/Cargo.toml b/crates/onboarding/Cargo.tomlindex 254b1bc3e6..31f29e338f 100644--- a/crates/onboarding/Cargo.toml+++ b/crates/onboarding/Cargo.toml@@ -10,13 +10,11 @@ anyhow = { workspace = true }axum = { workspace = true }chrono = { workspace = true }generic_email_domains = { path = "../generic_email_domains" }-import = { path = "../import" }macro_authorization = { path = "../macro_authorization", default-features = false, features = ["axum",] }macro_user_id = { path = "../macro_user_id" }mcp_client = { path = "../mcp_client" }-mcp_select = { path = "../mcp_select" }serde = { workspace = true }serde_json = { workspace = true }sqlx = { workspace = true }diff --git a/crates/onboarding/src/domain/ports.rs b/crates/onboarding/src/domain/ports.rsindex 123f65ebac..35dc77f838 100644--- a/crates/onboarding/src/domain/ports.rs+++ b/crates/onboarding/src/domain/ports.rs@@ -10,9 +10,6 @@ pub enum OnboardingError {/// Database failure.#[error("database error: {0}")]Db(rootcause::Report),- /// A failure in the import pipeline this flow drives.- #[error(transparent)]- Import(#[from] import::domain::ports::ImportError),/// Anything else.#[error(transparent)]Other(#[from] anyhow::Error),diff --git a/crates/onboarding/src/domain/service.rs b/crates/onboarding/src/domain/service.rsindex a4a3338349..8e90aa3818 100644--- a/crates/onboarding/src/domain/service.rs+++ b/crates/onboarding/src/domain/service.rs@@ -1,19 +1,9 @@-//! The onboarding orchestrator: assembles the aggregate and keeps the-//! import pipeline moving while the flow is active.-//!-//! Reconciliation is level-triggered and idempotent: every tick lists the-//! user's MCP connections and asks the import service to start a gather for-//! each authenticated connector with automatic import enabled.-//! `start_gather` is a CAS that only wins when no run has ever started, so-//! ticking on every read (and on the MCP post-auth hook) is safe.+//! The onboarding orchestrator: assembles the aggregate and records when the+//! flow finishes.-use super::models::{- ConnectedServer, DismissedPrompts, OnboardingRow, OnboardingState, OnboardingStatus, PromptId,-};+use super::models::{ConnectedServer, DismissedPrompts, OnboardingRow, OnboardingState, PromptId};use super::ports::{OnboardingError, OnboardingRepo, Result};use generic_email_domains::is_generic_email_domain;-use import::domain::models::{ImportSource, Initiator};-use import::domain::service::ImportService;use macro_user_id::email::ReadEmailParts;use macro_user_id::user_id::MacroUserIdStr;use mcp_client::domain::ports::McpServerStore;@@ -33,25 +23,13 @@ fn suggested_team_domain(user: &MacroUserIdStr<'_>) -> Option<String> {/// The API the onboarding router (and host-service hooks) talk to.pub trait OnboardingService: Send + Sync + 'static {- /// The full aggregate for the user. While the flow is active this also- /// starts any gather runs that are due, so reading the state is what- /// keeps onboarding moving.+ /// The full aggregate for the user.fn get_state(&self,user: MacroUserIdStr<'static>,) -> impl Future<Output = Result<OnboardingState>> + Send;- /// An explicit reconcile tick, for host hooks that observed a real state- /// change (e.g. an MCP OAuth completion). A no-op for users who never- /// entered the flow or already finished it — connecting an MCP server- /// outside onboarding must not start gathers.- fn reconcile(&self, user: MacroUserIdStr<'static>) -> impl Future<Output = Result<()>> + Send;-- /// Finish the flow (`skipped` records how). Unreserved leftover- /// onboarding-staged import candidates are deleted — never reviewed is- /// not declined, so they stay re-stageable by later gathers or chat.- /// Candidates reserved by configured automatic runs remain until their- /// run settles.+ /// Finish the flow (`skipped` records how).fn complete(&self,user: MacroUserIdStr<'static>,@@ -73,33 +51,23 @@ pub trait OnboardingService: Send + Sync + 'static {) -> impl Future<Output = Result<()>> + Send;}-/// Concrete orchestrator wiring the repo, the user's MCP servers, and the-/// import service together.-pub struct OnboardingServiceImpl<R, S, I, M> {+/// Concrete orchestrator wiring the repo and the user's MCP servers together.+pub struct OnboardingServiceImpl<R, S> {repo: R,mcp_store: Arc<S>,- import: Arc<I>,- mcp_select: Arc<M>,}-impl<R, S, I, M> OnboardingServiceImpl<R, S, I, M> {+impl<R, S> OnboardingServiceImpl<R, S> {/// Build the orchestrator.- pub fn new(repo: R, mcp_store: Arc<S>, import: Arc<I>, mcp_select: Arc<M>) -> Self {- Self {- repo,- mcp_store,- import,- mcp_select,- }+ pub fn new(repo: R, mcp_store: Arc<S>) -> Self {+ Self { repo, mcp_store }}}-impl<R, S, I, M> OnboardingServiceImpl<R, S, I, M>+impl<R, S> OnboardingServiceImpl<R, S>whereR: OnboardingRepo,S: McpServerStore,- I: ImportService,- M: mcp_select::ConnectorSelect,{async fn connected_servers(&self,@@ -119,50 +87,17 @@ where}).collect())}-- /// Start an auto-importing gather run for every authenticated connector- /// that never had one. A failing source must not block the others (or- /// the read).- async fn start_due_gathers(- &self,- user: &MacroUserIdStr<'static>,- _servers: &[ConnectedServer],- ) {- for source in ImportSource::all() {- // Which stack backs the connector is the selector's call- // (enabled native connectors take precedence, see `mcp_select`).- let connected = self- .mcp_select- .connector_connected(user, source.connector_ref())- .await- .inspect_err(- |e| tracing::warn!(source = source.as_ref(), error = ?e, "connected check failed"),- )- .unwrap_or(false);- if !connected {- continue;- }- if let Err(e) = self.import.start_gather(user.clone(), source, true).await {- tracing::warn!(source = source.as_ref(), error = ?e, "failed to start gather run");- }- }- }}-impl<R, S, I, M> OnboardingService for OnboardingServiceImpl<R, S, I, M>+impl<R, S> OnboardingService for OnboardingServiceImpl<R, S>whereR: OnboardingRepo,S: McpServerStore,- I: ImportService,- M: mcp_select::ConnectorSelect,{#[tracing::instrument(skip(self, user), err)]async fn get_state(&self, user: MacroUserIdStr<'static>) -> Result<OnboardingState> {let row = self.repo.ensure_row(&user).await?;let connected_servers = self.connected_servers(&user).await?;- if row.status == OnboardingStatus::Active {- self.start_due_gathers(&user, &connected_servers).await;- }Ok(OnboardingState {row,connected_servers,@@ -170,58 +105,13 @@ where})}- #[tracing::instrument(skip(self, user), err)]- async fn reconcile(&self, user: MacroUserIdStr<'static>) -> Result<()> {- // get_row, NOT ensure_row: this runs from the generic MCP post-auth- // hook, and a user connecting a server outside onboarding must not- // be pulled into the flow (or have gathers started).- let Some(row) = self.repo.get_row(&user).await? else {- return Ok(());- };- if row.status != OnboardingStatus::Active {- // Self-heal the completion race: a reconcile (or a gather- // session it started) can outlive complete()'s cleanup and- // stage rows after it ran. Any later tick for a finished flow- // sweeps those leftovers, best-effort.- let _ = self- .import- .delete_staged_by_initiator(user, Initiator::Onboarding)- .await- .inspect_err(|e| {- tracing::warn!(error = ?e, "failed to sweep post-completion staged rows");- });- return Ok(());- }- let servers = self.connected_servers(&user).await?;- self.start_due_gathers(&user, &servers).await;- Ok(())- }-#[tracing::instrument(skip(self, user), err)]async fn complete(&self,user: MacroUserIdStr<'static>,skipped: bool,) -> Result<OnboardingRow> {- let row = self.repo.complete(&user, skipped).await?;- // Unreserved candidates the user never acted on are removed from the- // ledger, not discarded: discard means "the user said no" and blocks- // re-staging forever, which is wrong for items they never looked at- // ("Set up later" skips the whole review). Candidates reserved by an- // active or retryable configured auto-import run survive this cleanup.- // Explicit declines (a section toggled off) were already discarded by- // run_import.- // Chat-staged rows are untouched. Best-effort: the completion above- // is durable, so a cleanup failure must not fail the call — the- // next reconcile tick sweeps leftovers anyway.- let _ = self- .import- .delete_staged_by_initiator(user, Initiator::Onboarding)- .await- .inspect_err(|e| {- tracing::warn!(error = ?e, "failed to delete leftover onboarding-staged candidates");- });- Ok(row)+ self.repo.complete(&user, skipped).await}#[tracing::instrument(skip(self, user), err)]diff --git a/crates/onboarding/src/domain/service/test.rs b/crates/onboarding/src/domain/service/test.rsindex c1d6dc429c..517ddac0be 100644--- a/crates/onboarding/src/domain/service/test.rs+++ b/crates/onboarding/src/domain/service/test.rs@@ -1,12 +1,7 @@use super::*;-use import::domain::models::ImportState;-use import::domain::ports::Result as ImportResult;-use import::domain::service::RunImportOutcome;+use crate::domain::models::OnboardingStatus;use mcp_client::domain::models::{McpServerRecord, StoredCredentials};-use mcp_client::domain::service::McpToolSet;-use mcp_select::{ConnectorRef, ConnectorSelect, UserMcpTools};use std::sync::Mutex;-use uuid::Uuid;fn user() -> MacroUserIdStr<'static> {MacroUserIdStr::try_from("macro|[email protected]".to_string()).expect("valid test user id")@@ -23,8 +18,6 @@ fn active_row() -> OnboardingRow {struct MockRepo {status: OnboardingStatus,- /// Whether a row exists at all (get_row returns None when false).- exists: bool,completions: Mutex<Vec<bool>>,dismissed: Mutex<Vec<String>>,}@@ -33,18 +26,10 @@ impl MockRepo {fn new(status: OnboardingStatus) -> Self {Self {status,- exists: true,completions: Mutex::new(Vec::new()),dismissed: Mutex::new(Vec::new()),}}-- fn missing() -> Self {- Self {- exists: false,- ..Self::new(OnboardingStatus::Active)- }- }}impl OnboardingRepo for MockRepo {@@ -56,7 +41,7 @@ impl OnboardingRepo for MockRepo {}async fn get_row(&self, _user: &MacroUserIdStr<'static>) -> Result<Option<OnboardingRow>> {- Ok(self.exists.then(|| OnboardingRow {+ Ok(Some(OnboardingRow {status: self.status,..active_row()}))@@ -132,118 +117,6 @@ impl McpServerStore for MockStore {}}-#[derive(Default)]-struct MockImport {- gathers: Mutex<Vec<(ImportSource, bool)>>,- discards: Mutex<Vec<Initiator>>,- deletions: Mutex<Vec<Initiator>>,-}--impl ImportService for MockImport {- async fn state(&self, _user: MacroUserIdStr<'static>) -> ImportResult<ImportState> {- Ok(ImportState {- runs: Vec::new(),- entities: Vec::new(),- })- }-- async fn start_gather(- &self,- _user: MacroUserIdStr<'static>,- source: ImportSource,- auto_import: bool,- ) -> ImportResult<bool> {- self.gathers.lock().unwrap().push((source, auto_import));- Ok(true)- }-- async fn start_discovery(- &self,- _user: MacroUserIdStr<'static>,- _source: ImportSource,- ) -> ImportResult<bool> {- Ok(false)- }-- async fn retry_gather(- &self,- _user: MacroUserIdStr<'static>,- _source: ImportSource,- ) -> ImportResult<bool> {- Ok(false)- }-- async fn dismiss_run(- &self,- _user: MacroUserIdStr<'static>,- _source: ImportSource,- ) -> ImportResult<()> {- Ok(())- }-- async fn run_import(- &self,- _user: MacroUserIdStr<'static>,- _import_ids: Vec<Uuid>,- _discard_ids: Vec<Uuid>,- ) -> ImportResult<RunImportOutcome> {- Ok(RunImportOutcome {- discarded: 0,- importing: 0,- })- }-- async fn discard_staged_by_initiator(- &self,- _user: MacroUserIdStr<'static>,- initiator: Initiator,- ) -> ImportResult<u64> {- self.discards.lock().unwrap().push(initiator);- Ok(1)- }-- async fn delete_staged_by_initiator(- &self,- _user: MacroUserIdStr<'static>,- initiator: Initiator,- ) -> ImportResult<u64> {- self.deletions.lock().unwrap().push(initiator);- Ok(1)- }-}--/// Selector mock mirroring the native-only rule: no Pipedream connectors in-/// these tests, so connectivity is the native `credentials && url` match.-struct MockSelect {- store: Arc<MockStore>,-}--impl ConnectorSelect for MockSelect {- async fn user_toolset(&self, _user: &MacroUserIdStr<'static>) -> UserMcpTools {- UserMcpTools::Native(McpToolSet::new(&[], self.store.clone()).await)- }-- async fn connector_toolset(- &self,- _user: &MacroUserIdStr<'static>,- _connector: ConnectorRef<'_>,- ) -> anyhow::Result<Option<UserMcpTools>> {- Ok(None)- }-- async fn connector_connected(- &self,- _user: &MacroUserIdStr<'static>,- connector: ConnectorRef<'_>,- ) -> anyhow::Result<bool> {- Ok(self- .store- .records- .iter()- .any(|r| r.credentials.is_some() && r.url == connector.native_server_url))- }-}-fn record(url: &str, authenticated: bool) -> McpServerRecord {McpServerRecord {user_id: user(),@@ -255,96 +128,33 @@ fn record(url: &str, authenticated: bool) -> McpServerRecord {}}-fn service(- status: OnboardingStatus,- records: Vec<McpServerRecord>,-) -> (- OnboardingServiceImpl<MockRepo, MockStore, MockImport, MockSelect>,- Arc<MockImport>,-) {- let import = Arc::new(MockImport::default());- (- OnboardingServiceImpl::new(- MockRepo::new(status),- Arc::new(MockStore {- records: records.clone(),- }),- import.clone(),- Arc::new(MockSelect {- store: Arc::new(MockStore { records }),- }),- ),- import,- )-}-#[tokio::test]-async fn active_flow_starts_gathers_for_authenticated_connectors_only() {- let (service, import) = service(- OnboardingStatus::Active,- vec![- record("https://mcp.linear.app/mcp", true),- record("https://mcp.notion.com/mcp", false), // not authed yet- record("https://unrelated.example/mcp", true), // not an import source- ],+async fn get_state_lists_connected_servers_without_starting_imports() {+ let service = OnboardingServiceImpl::new(+ MockRepo::new(OnboardingStatus::Active),+ Arc::new(MockStore {+ records: vec![+ record("https://mcp.linear.app/mcp", true),+ record("https://mcp.notion.com/mcp", false),+ ],+ }),);let state = service.get_state(user()).await.expect("state");- assert_eq!(state.connected_servers.len(), 3);- assert_eq!(- import.gathers.lock().unwrap().as_slice(),- &[(ImportSource::Linear, true)]- );+ assert_eq!(state.connected_servers.len(), 2);+ assert!(state.connected_servers[0].authenticated);+ assert!(!state.connected_servers[1].authenticated);}#[tokio::test]-async fn completed_flow_never_starts_gathers() {- let (service, import) = service(- OnboardingStatus::Completed,- vec![record("https://mcp.linear.app/mcp", true)],- );-- service.get_state(user()).await.expect("state");- service.reconcile(user()).await.expect("reconcile");- assert!(import.gathers.lock().unwrap().is_empty());-}--#[tokio::test]-async fn completing_deletes_leftover_onboarding_staged_rows() {- let (service, import) = service(OnboardingStatus::Active, Vec::new());+async fn completing_records_how_the_flow_ended() {+ let repo = MockRepo::new(OnboardingStatus::Active);+ let service = OnboardingServiceImpl::new(repo, Arc::new(MockStore { records: vec![] }));let row = service.complete(user(), true).await.expect("complete");assert_eq!(row.status, OnboardingStatus::Completed);assert!(row.skipped);- // Deleted, not discarded — never-reviewed candidates must stay- // re-stageable by later gathers or chat.- assert_eq!(- import.deletions.lock().unwrap().as_slice(),- &[Initiator::Onboarding]- );- assert!(import.discards.lock().unwrap().is_empty());-}--#[tokio::test]-async fn reconcile_ignores_users_who_never_entered_the_flow() {- // The MCP post-auth hook reconciles every OAuth completion; a user- // connecting a server outside onboarding must not get gathers (or an- // onboarding row) out of it.- let import = Arc::new(MockImport::default());- let records = vec![record("https://mcp.linear.app/mcp", true)];- let service = OnboardingServiceImpl::new(- MockRepo::missing(),- Arc::new(MockStore {- records: records.clone(),- }),- import.clone(),- Arc::new(MockSelect {- store: Arc::new(MockStore { records }),- }),- );-- service.reconcile(user()).await.expect("reconcile");- assert!(import.gathers.lock().unwrap().is_empty());+ assert_eq!(service.repo.completions.lock().unwrap().as_slice(), &[true]);}#[test]@@ -364,10 +174,6 @@ async fn get_state_carries_the_suggested_team_domain() {let service = OnboardingServiceImpl::new(MockRepo::new(OnboardingStatus::Active),Arc::new(MockStore { records: vec![] }),- Arc::new(MockImport::default()),- Arc::new(MockSelect {- store: Arc::new(MockStore { records: vec![] }),- }),);let state = service.get_state(user()).await.expect("get_state");@@ -398,7 +204,10 @@ fn prompt_ids_accept_card_slugs_and_reject_everything_else() {#[tokio::test]async fn dismissed_prompts_round_trip_without_entering_onboarding() {- let (service, _) = service(OnboardingStatus::Active, Vec::new());+ let service = OnboardingServiceImpl::new(+ MockRepo::new(OnboardingStatus::Active),+ Arc::new(MockStore { records: vec![] }),+ );service.dismiss_prompt(user(), PromptId::parse("import:tasks").unwrap()).awaitdiff --git a/crates/onboarding/src/inbound/axum_router.rs b/crates/onboarding/src/inbound/axum_router.rsindex 6c98d8e298..eddf512c94 100644--- a/crates/onboarding/src/inbound/axum_router.rs+++ b/crates/onboarding/src/inbound/axum_router.rs@@ -83,9 +83,7 @@ fn error_response(e: OnboardingError) -> Response {StatusCode::INTERNAL_SERVER_ERROR.into_response()}-/// Get the authenticated user's onboarding state. While the flow is active-/// this also starts any gather runs that are due, so polling this endpoint-/// is what keeps onboarding moving.+/// Get the authenticated user's onboarding state.#[utoipa::path(get,path = "/onboarding",diff --git a/crates/onboarding/src/lib.rs b/crates/onboarding/src/lib.rsindex c5cc689b47..1c81eb3430 100644--- a/crates/onboarding/src/lib.rs+++ b/crates/onboarding/src/lib.rs@@ -1,12 +1,7 @@//! The onboarding flow (`/setup`): one row per user tracking whether they-//! are still in the flow, plus the hook that starts import gather runs when-//! connectors authenticate.-//!-//! All the actual import machinery — staging, dedup, gather/import jobs —-//! lives in the `import` crate. Onboarding is just a feature that uses it:-//! it starts auto-importing gathers for authenticated connectors while the-//! flow is active, and deletes unreserved onboarding-staged rows when the-//! flow completes.+//! are still in the flow, plus the aggregate the flow reads (connected MCP+//! servers and a suggested team domain). Connecting an app during onboarding+//! only connects it; importing is a separate, explicit step.#![deny(missing_docs)]diff --git a/crates/pipedream_mcp/AGENTS.md b/crates/pipedream_mcp/AGENTS.mdindex dd29944c48..b92f76f224 100644--- a/crates/pipedream_mcp/AGENTS.md+++ b/crates/pipedream_mcp/AGENTS.md@@ -29,8 +29,8 @@ AI loop calls their tools through Pipedream's remote MCP server.- Depends on: `ai_toolset`, `mcp_toolset`, `macro_authorization`,`macro_user_id`.-- Used by: `ai_tools`, `agent_egress`, `agent_harness`, `granola_sync`, `mcp_select`,- `services/agent_harness_service`.+- Used by: `ai_tools`, `import_notion`, `agent_egress`, `agent_harness`,+ `granola_sync`, `mcp_select`, `services/agent_harness_service`.## Checksdiff --git a/crates/pipedream_mcp/src/inbound/axum_router.rs b/crates/pipedream_mcp/src/inbound/axum_router.rsindex f77ae577c1..f073636a9e 100644--- a/crates/pipedream_mcp/src/inbound/axum_router.rs+++ b/crates/pipedream_mcp/src/inbound/axum_router.rs@@ -24,13 +24,6 @@ use utoipa::{IntoParams, ToSchema};#[cfg(test)]mod test;-/// Hook invoked after a connect flow completes and the record is saved.-/// Hosts use this to react to a connection the moment it exists (e.g. start-/// import gather jobs); implementations must be quick or spawn.-pub type PipedreamAuthCompletedHook = Arc<- dyn Fn(PipedreamConnection) -> std::pin::Pin<Box<dyn Future<Output = ()> + Send>> + Send + Sync,->;-/// Shared state for the MCP router.////// `pipedream` is the single connect path. When it is `None` (deployment not@@ -40,7 +33,6 @@ pub struct PipedreamRouterState<S, P, Auth> {store: Arc<S>,pipedream: Option<Arc<P>>,authorization_state: MacroAuthorizationState<Auth>,- on_auth_completed: Option<PipedreamAuthCompletedHook>,}impl<S, P, Auth> Clone for PipedreamRouterState<S, P, Auth> {@@ -49,7 +41,6 @@ impl<S, P, Auth> Clone for PipedreamRouterState<S, P, Auth> {store: self.store.clone(),pipedream: self.pipedream.clone(),authorization_state: self.authorization_state.clone(),- on_auth_completed: self.on_auth_completed.clone(),}}}@@ -78,17 +69,9 @@ wherestore: Arc::new(store),pipedream,authorization_state,- on_auth_completed: None,}}- /// Invoke `hook` whenever a connect flow completes (see- /// [`PipedreamAuthCompletedHook`]).- pub fn with_auth_completed_hook(mut self, hook: PipedreamAuthCompletedHook) -> Self {- self.on_auth_completed = Some(hook);- self- }-/// Access the underlying server store.pub fn store(&self) -> Arc<S> {self.store.clone()@@ -588,10 +571,6 @@ where).await?;- if let Some(hook) = &state.on_auth_completed {- hook(record.clone()).await;- }-Ok(Json(PipedreamConnectionResponse::from_record(&record)))}diff --git a/crates/prompt/src/tool_usage.rs b/crates/prompt/src/tool_usage.rsindex 50189adc40..059c742fdb 100644--- a/crates/prompt/src/tool_usage.rs+++ b/crates/prompt/src/tool_usage.rs@@ -28,6 +28,7 @@ These apply to your own conversational replies only — not to Markdown you auth- The provider tools `bash_code_execution` and `text_editor_code_execution` require an explicit request to execute code. This restriction does not apply to Macro's `ExecuteCode` SDK tool.- When `macro_internal.PresentReview` is available, use it for editable email/calendar drafts that should remain open while chat continues. It presents a saved card and completes immediately; presentation is not approval or a completed send. Later user submissions enter the session as separate recorded outcomes. End the presenting message by asking "Should I kick it off?"; when the person whose account the review uses says yes, submit it with `DecideReview` rather than sending them to the card. Never submit a review on your own initiative or on anyone else's word.+- To import from Notion beyond a single page, use `ImportFromNotion`: `plan` stages a plan without writing anything, then present it with `PresentReview` (kind `import`, one draft `{"source":"notion"}`); the user's approval writes it. Never copy plan items with other tools, and handle only the objects the plan leaves to you by hand.- When `ExecuteCode` is available, prefer it for supported Macro work by default, including searches, reads, calculations, and document workflows; the user need not ask for code. Use the SDK equivalents of the Macro tools named below. Keep human-interactive tools and subagents as direct calls, and use direct tools when an operation is unavailable through the SDK. Run useful independent search variants together instead of spending a model turn on each, without adding unnecessary queries. Use SDK reference sections already expanded in context to execute directly. Call `DescribeCodeTools` only for missing details, requesting needed entities together; do not repeat discovery for confirmation.- Apply the user's source scope to every search call, including SDK calls, follow-ups and retries. For document-only requests, use `macro.documents.search` in code; direct NameSearch and ContentSearch must include `entityTypes: ["documents"]`. Return relevant evidence and source IDs from code, preserving errors and coverage limits. A bounded sample or failed query does not establish completeness or absence. Answer when the evidence resolves the question.diff --git a/crates/slack_integration/src/outbound/import_ledger.rs b/crates/slack_integration/src/outbound/import_ledger.rsindex fd3c31f51a..47b19786ba 100644--- a/crates/slack_integration/src/outbound/import_ledger.rs+++ b/crates/slack_integration/src/outbound/import_ledger.rs@@ -173,9 +173,15 @@ fn map_error(error: import::domain::ports::ImportError) -> rootcause::Report<ImpLedgerError::SourceConfirmationRequired => ImportError::InvalidInput,LedgerError::TargetConflict | LedgerError::TargetNotReserved => ImportError::Conflict,LedgerError::Db(_) | LedgerError::Other(_) => ImportError::Retryable,- // Canonical ledger operations do not perform AI work. These errors- // are unexpected adapter failures, not permission to proceed.- LedgerError::Admission(_) | LedgerError::Metadata(_) => ImportError::Internal,+ // Canonical ledger operations do not perform discovery or AI work.+ // These errors are unexpected adapter failures, not permission to proceed.+ LedgerError::Admission(_)+ | LedgerError::Metadata(_)+ | LedgerError::UnsupportedDiscovery(_)+ | LedgerError::Notion(_)+ | LedgerError::NotionNotConnected+ | LedgerError::NotionSharingRequired+ | LedgerError::NotReady(_) => 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.rsindex c15976f024..5c97fa2c71 100644--- a/crates/slack_integration/src/outbound/import_ledger/test.rs+++ b/crates/slack_integration/src/outbound/import_ledger/test.rs@@ -151,6 +151,17 @@ async fn reservation_passes_requester_explicit_team_and_authorized_target_for_ev}}+#[test]+fn unexpected_discovery_error_fails_closed() {+ assert_eq!(+ map_error(LedgerError::UnsupportedDiscovery(+ ledger::ImportSource::Slack+ ))+ .into_current_context(),+ ImportError::Internal+ );+}+#[test]fn unexpected_ai_admission_error_fails_closed() {let admission = serde_json::from_str(r#""unavailable""#).unwrap();diff --git a/docs/agent-guide/ai-chat.md b/docs/agent-guide/ai-chat.mdindex 43853300ba..4ab9de8d41 100644--- a/docs/agent-guide/ai-chat.md+++ b/docs/agent-guide/ai-chat.md@@ -2066,8 +2066,12 @@ reads the saved version and discards the local edit; it never retries an externasend automatically. If the outcome is unknown, check the inbox/calendar beforeattempting another send elsewhere. Review decisions produce durable system eventsfor the same review ID. The card refreshes in place, with **Responding** below it-until the agent starts its reply. This behaves the same on mobile. These events-create no user bubble; events without an existing card use a quiet timeline row.+until the agent starts its reply. This behaves the same on mobile. Approving an+import plan only starts its write, so no **Responding** shows until the write+ends; the card shows its final state and the agent's reply follows. A plan+still being made when it was presented also wakes the agent once it is ready.+These events create no user bubble; events without an existing card use a+quiet timeline row.The agent can read final drafts and confirmed outcomes through **GetReview**.An exact silent response produces no assistant message or unread notification.A decision in chat from the review’s acting account uses the same saved-revision claim as the card;@@ -2137,4 +2141,8 @@ Agent-session system events appear as a single quiet row with a kind-specificicon, an outcome summary, and relative time (hover for the exact timestamp).Failures and unconfirmed outcomes use the error tone. Review events refreshthe existing card without adding a second timeline row when that card is-already in the thread. The row stays on one line and truncates on phones.+already in the thread. Import reviews add "Import plan ready" with the plan's+counts once planning finishes, and settle as "Import finished", "Import+failed" or "Import cancelled" with imported, failed and by-hand counts; the+row never shows the report's error text. The row stays on one line and+truncates on phones.diff --git a/docs/agent-guide/files.md b/docs/agent-guide/files.mdindex f93a2ddbbd..b85abbb3e5 100644--- a/docs/agent-guide/files.md+++ b/docs/agent-guide/files.md@@ -57,7 +57,37 @@ touch uses the global search overlay and creation uses the dock's New button.The **Folders** pill opens the folder overview and stays highlighted insideany folder; tapping it from inside a folder returns to the overview, andselecting another pill leaves the folder tree. The sidebar contains `New file or folder`, `My Files`, `Recent`,-`Shared with me`, collapsible Favorites, and a searchable folder hierarchy. Filter+`Shared with me`, collapsible Favorites, and a searchable folder hierarchy.+The Drive sidebar's import card (**Import files**, see+[setup cards](setup-cards.md#import-from-connected-apps)) lists Notion; its+**Import** (or a successful **Connect**) opens a new agent chat beside Drive+that sends "Import my Notion workspace into Macro." (the agent plans with ImportFromNotion, or shows the+Notion connect chip first; a user on a team is first asked whether to share+the import with the team, keep it personal, or let AI decide per page, and a+user without a team gets a personal import without being asked). The plan+call returns within seconds and the agent presents the review card at once:+while planning it shows `Reading Notion: 40 pages, 12 databases`, then every+row with a **Deciding…** badge and `Deciding what to import: 40 of 84+decided` as the decisions fill in, with **Import** disabled until the plan is+ready. The card shows totals (Documents, Databases with rows, Task lists with+tasks, Skipped, Already in Macro), a **Team** / **Only you** badge on every+written row (the owner clicks it to flip it, and a folder flips what is below+it; databases stay **Only you**, and the card says "Databases can't be shared+with the team yet, so they stay with you."), a sharing summary line (without+a team: `N only you — you're not on a team, so nothing is shared`), and a+collapsed **Already in Macro** section linking to copies teammates already+imported and shared, which are not imported again. A database or task-list+row's caret (**Show rows of …**) shows its row count with a few sample row+titles and, for a task list, which Notion columns become status, priority,+due date and assignee. **Open full review** shows the whole plan in a dialog+(a full-height sheet on phones). **Import …** approves straight from the+card, with no confirmation step. The agent ends that+message with "Should I kick it off?"; when the owner answers yes in chat, it+approves the review itself (a `DecideReview` submit, the card's own approve with the+card's choices), and the card's run state adds **Started by the agent at your+request** ("at the owner's request" for other viewers). A message from anyone+but the owner never approves, and a plan still being made cannot be approved+yet. Filterby tag from the **Filter** menu's Tags submenu; navigating to another tab or folderclears tag filters. A folder with no files matching the active tagshows the list's no-match state rather than `This folder is empty`.diff --git a/docs/agent-guide/setup-cards.md b/docs/agent-guide/setup-cards.mdindex cbeb942af1..b70083b5f7 100644--- a/docs/agent-guide/setup-cards.md+++ b/docs/agent-guide/setup-cards.md@@ -24,7 +24,8 @@ Every other app opens Pipedream's Connect UI inside the dialog. A connected rowthe current view, with an unsent prompt such as "Import my most recentissues and tasks from Jira into Macro tasks.". Private instructions tell theagent to list candidates and ask before creating anything, and never to- write to the source. Notion pages import through `ImportNotionPage`; Linear+ write to the source. Notion opens the workspace importer instead ("Import my Notion workspace+into Macro." → `ImportFromNotion`, which plans and presents a review); Linearand Slack items are checked against and recorded in the import ledger(`ListImportEntities`, `CreateImportEntity`) so nothing imports twice.- Files also offers **Upload files** and **Upload a folder** (Drive's owndiff --git a/docs/design/agent-session-notifications.md b/docs/design/agent-session-notifications.mdindex af6f6f802a..5d89664c57 100644--- a/docs/design/agent-session-notifications.md+++ b/docs/design/agent-session-notifications.md@@ -7,11 +7,23 @@ through its transaction-aware producer port in the state-change transaction.## ContractA notification has a UUIDv7 id, session, typed event (`review.decided`,-`review.settled`, `app.connected`), subject, payload, dedupe key, wake policy,-creation time and producer trace context. Dedupe is scoped to a session; absent-an explicit key it is kind plus subject. Review decisions include a revision in-their key because a stack can be decided in several batches. Payloads reference-review and item ids; final drafts remain available through GetReview.+`review.settled`, `import.planned`, `app.connected`), subject, payload, dedupe+key, wake policy, creation time and producer trace context. Dedupe is scoped to+a session; absent an explicit key it is kind plus subject. Review decisions+include a revision in their key because a stack can be decided in several+batches. Payloads reference review and item ids; final drafts remain available+through GetReview.++Long-running review kinds settle in their own domain. Approving an import+review only starts its write, so its `review.decided` is quiet; the import+produces one `review.settled` per review, carrying the final report+(`ImportSettlement`: outcome, counts, failures, by-hand items, root folder),+in the transaction that ends the write, including a resumed write and one+given up on. A plan presented while planning also produces `import.planned`+once it is ready to approve. Both wake the session. A settled payload is+either an import report or the review service's own outcome; the enum is+untagged so settlements stored before imports existed still read. Dismissing+an import still wakes, as for other kinds.The harness serializes delivery with all other session commands. It claims abounded batch under a lease using SKIP LOCKED. Waking events start an idle turn;@@ -106,9 +118,8 @@ terminal writes from older replicas during rollout. Queue reads discard theirformer context entries so they cannot restore as empty owner prompts.Old JSON fields are ignored on reads;removing them does not require destructive SQL. Notification rows are visible to all-session viewers. The app-connected context-prompt caller referenced in the-original goals does not exist in this checkout; the typed event remains part-of the producer API for the next integration branch.+session viewers. The harness produces `app.connected` when the owner connects+an app a session asked for (`POST /agent-sessions/{id}/app-connections`, 202).## Browser proofdiff --git a/docs/runbooks/ai-billing-rollout.md b/docs/runbooks/ai-billing-rollout.mdindex 8c7639c659..4b03e0af7a 100644--- a/docs/runbooks/ai-billing-rollout.md+++ b/docs/runbooks/ai-billing-rollout.md@@ -105,7 +105,7 @@ regressions, not live-provider or deployment verification.| DSS channel classifier | Separate configured recorder supplied to `FastModelTriggerClassifier` in DSS `main.rs`; requesting user, ChannelBot | One-shot classification attempts → journal | Shared completion/structured tests. This is distinct from the responder's common context || DSS call summarizer, system task duplicate judge | Independent configured recorder injections in DSS `main.rs`; existing user/feature supplied by each producer | Calls using shared Rust completion APIs now produce journal attempts | Shared agent tests and DSS tests; this change does not change their attribution policy || Scheduled actions | `services/agent_harness_service/src/scheduling.rs` settling recorder for the condition classifier; executor binds stored authenticated owner, Automation feature | AgentLoop attempts + native tools/subagents → journal | Scheduled-action package and shared scope tests; remote execution outside this process remains separate |-| DCS imports | DCS `main.rs` passes wrapped recorder to import service; import `drive_session` binds user, Import feature | Rust gather/Notion AgentLoop attempts → journal | Shared agent tests / DCS tests. Remote connector internals are not local model attempts |+| DCS imports | DCS `main.rs` passes wrapped recorder to import service; import `drive_session` binds user, Import feature | Rust Notion page import AgentLoop attempts → journal | Shared agent tests / DCS tests. Remote connector internals are not local model attempts || Memory and AI projections | Common host recorder (DCS; memory's own context uses the configured common-tool builder), with existing user-specific Memory / AiProjection contexts | Shared completion/AgentLoop attempts → journal, including native tool subcalls where available | Shared agent/tool regressions; no claim of new end-to-end producer deployment tests || Managed harness sandboxes (local/Daytona), external Cursor/Codex/Claude agents | Harness passes provider credentials / external account credentials directly to the runtime | **Not covered** by Rust common-context injection; runtime model calls bypass this HTTP adapter. Rust session naming/selection and calls back to authenticated MCP are the only covered portions | Future runtime-owned producer instrumentation must emit trusted per-attempt provider/model/counter/response IDs and immutable authenticated session attribution to an idempotent journal ingress. Legacy admission now gates Macro-funded managed sessions, but adds no runtime metering or credential/proxy redesign; externally funded runtime execution skips that gate || AI editing worker, dictation/audio, arbitrary raw-provider callers, remote MCP internals | Separate producer ownership; multipart requests explicitly warn when scoped but unsupported | **Not covered by this token-attempt adapter.** Existing aggregate/audio behavior is not per-attempt evidence | Separate worker/audio integrations and trusted ingress required. Do not enable new-policy charging for these paths based on this matrix |diff --git a/docs/runbooks/ai-quota-enforcement.md b/docs/runbooks/ai-quota-enforcement.mdindex 5e2b3a8be1..806de55582 100644--- a/docs/runbooks/ai-quota-enforcement.md+++ b/docs/runbooks/ai-quota-enforcement.md@@ -308,7 +308,7 @@ requests settlement). Admission itself never records usage or calls settlement.| Direct subagent / inherited feature | [operations service](../../crates/ai_tools/src/ai_operations.rs), [subagent](../../crates/ai_tools/src/subagent.rs) | Host common context: T, DCS main, or [MCP context](../../services/agent_harness_service/src/public_mcp/context.rs) using `pg_settling_recorder` | [zero provider calls](../../crates/ai_tools/src/ai_operations/test.rs), [concurrent caller isolation](../../crates/ai_tools/src/subagent/test.rs) || Native Anthropic WebSearch/WebFetch/code tools / inherited feature | [invoke_server_tool](../../crates/anthropic/src/toolset.rs) before both provider branches | Same common contexts via `FromRef`; observation only, **no legacy aggregate producer** | [tool attribution/refusal](../../crates/anthropic/src/toolset/test.rs) || AI document editing / AiEditing | [document orchestration](../../crates/documents/src/domain/ai_editing.rs) after verified edit receipt | Common context's recorder, existing per-model worker usage | [domain](../../crates/documents/src/domain/ai_editing/test.rs), [direct tool and permission precedence](../../crates/documents/src/inbound/toolset/edit_document/test.rs) |-| AI gather/Notion imports / Import | [import admission](../../crates/import/src/domain/service/admission.rs), [execution/rechecks](../../crates/import/src/domain/service.rs) | DCS main, wrapped recorder | [initial/retry/delayed/deterministic cases](../../crates/import/src/domain/service/test/admission.rs), [HTTP](../../crates/import/src/inbound/axum_router/test.rs) |+| Notion page imports / Import | [import admission](../../crates/import/src/domain/service/admission.rs), [execution/rechecks](../../crates/import/src/domain/service.rs) | DCS main, wrapped recorder | [delayed/deterministic cases](../../crates/import/src/domain/service/test/admission.rs), [HTTP](../../crates/import/src/inbound/axum_router/test.rs) || Channel responder / ChannelBot | [bot service](../../crates/channel_bots/src/domain/service.rs) | [DSS main](../../services/document_storage_service/src/main.rs), common context T | [response refusal](../../crates/channel_bots/src/domain/service/tests.rs) || Optional channel classification / ChannelBot | [trigger detector](../../crates/channel_bots/src/domain/trigger_detector.rs) | DSS main, independent R for classifier | [explicit routing and inference skip](../../crates/channel_bots/src/domain/trigger_detector/tests.rs) || Trigger inference and image captions / Automation | [trigger service](../../crates/agent_trigger/src/domain/service.rs) | [standalone trigger main](../../services/agent_trigger_service/src/main.rs) R; [harness trigger](../../services/agent_harness_service/src/trigger.rs) receives the harness's settling recorder from harness main | [classification/caption refusal](../../crates/agent_trigger/src/domain/service/test/admission.rs), [no system fallback](../../crates/agent_trigger/src/outbound/fast_model_judge/test.rs) |diff --git a/docs/runbooks/slack-live-channel-import.md b/docs/runbooks/slack-live-channel-import.mdindex 0f912192a3..ee054255d7 100644--- a/docs/runbooks/slack-live-channel-import.md+++ b/docs/runbooks/slack-live-channel-import.md@@ -215,9 +215,7 @@ gates, not successful observations:3. Find channels again: verify no duplicate rows, refreshed counts on stagedrows, and already-imported rows remaining nonselectable. Check archivedfiltering, empty/error states, and exclusion of private/DM conversations.-4. Onboard a fresh user with Slack connected: verify auto-imported channels- include matched teammates, without changing onboarding to manual selection.-5. Record channel-list, directory, per-channel member, complete discovery, and+4. Record channel-list, directory, per-channel member, complete discovery, andtwo-channel import latency with workspace/channel sizes. **No live latencywas measured**; test durations above are not service latency measurements.@@ -230,9 +228,8 @@ gates, not successful observations:batches attempt fresh resolution with one shared session/directory; failuresfall back to staged participants, potentially leaving only the importer.- Best-effort membership enrichment shares a deadline measured from the start- of discovery: 170 seconds for onboarding, 350 seconds for manual discovery,- leaving 10 seconds before the outer timeout. Directory/member reads, metadata- enrichment, and notifications are bounded; expiry keeps staged channels ready+ of discovery: 350 seconds, leaving 10 seconds before the outer timeout.+ Directory/member reads, metadata enrichment, and notifications are bounded; expiry keeps staged channels readywith unresolved membership instead of failing an otherwise usable discovery.Initial listing or staging can still fail or reach the outer timeout.- Stored matched participants are capped at 100; `member_count` is separate.diff --git a/packages/sdk-docs/reference/generated/agent-harness/types.gen/README.md b/packages/sdk-docs/reference/generated/agent-harness/types.gen/README.mdindex d693d940e7..116b350947 100644--- a/packages/sdk-docs/reference/generated/agent-harness/types.gen/README.md+++ b/packages/sdk-docs/reference/generated/agent-harness/types.gen/README.md@@ -49,6 +49,7 @@- [AnswerAgentSessionToolApprovalResponses](type-aliases/AnswerAgentSessionToolApprovalResponses.md)- [AnswerToolApprovalRequest](type-aliases/AnswerToolApprovalRequest.md)- [AnswerToolApprovalResponse](type-aliases/AnswerToolApprovalResponse.md)+- [AppConnectedRequest](type-aliases/AppConnectedRequest.md)- [BotId](type-aliases/BotId.md)- [CapabilityHarnessDto](type-aliases/CapabilityHarnessDto.md)- [CaptureAttemptDto](type-aliases/CaptureAttemptDto.md)@@ -253,6 +254,10 @@- [RenameAgentSessionRequest](type-aliases/RenameAgentSessionRequest.md)- [RenameAgentSessionResponse](type-aliases/RenameAgentSessionResponse.md)- [RenameAgentSessionResponses](type-aliases/RenameAgentSessionResponses.md)+- [ReportAgentSessionAppConnectedData](type-aliases/ReportAgentSessionAppConnectedData.md)+- [ReportAgentSessionAppConnectedError](type-aliases/ReportAgentSessionAppConnectedError.md)+- [ReportAgentSessionAppConnectedErrors](type-aliases/ReportAgentSessionAppConnectedErrors.md)+- [ReportAgentSessionAppConnectedResponses](type-aliases/ReportAgentSessionAppConnectedResponses.md)- [RetryAgentConversationTurnData](type-aliases/RetryAgentConversationTurnData.md)- [RetryAgentConversationTurnErrors](type-aliases/RetryAgentConversationTurnErrors.md)- [RetryAgentConversationTurnRequest](type-aliases/RetryAgentConversationTurnRequest.md)diff --git a/packages/sdk-docs/reference/generated/agent-harness/types.gen/type-aliases/AppConnectedRequest.md b/packages/sdk-docs/reference/generated/agent-harness/types.gen/type-aliases/AppConnectedRequest.mdnew file mode 100644index 0000000000..4af9441dc8--- /dev/null+++ b/packages/sdk-docs/reference/generated/agent-harness/types.gen/type-aliases/AppConnectedRequest.md@@ -0,0 +1,12 @@+# Type Alias: AppConnectedRequest++> **AppConnectedRequest** = \{ `actionId?`: `null` \| [`AgentActionId`](AgentActionId.md); `appSlug`: `string`; \}++Request body for reporting a connected app.++## Properties++| Property | Type | Description |+| ------ | ------ | ------ |+| <a id="property-actionid"></a> `actionId?` | `null` \| [`AgentActionId`](AgentActionId.md) | - |+| <a id="property-appslug"></a> `appSlug` | `string` | The Pipedream app slug the owner connected, e.g. `notion`. |diff --git a/packages/sdk-docs/reference/generated/agent-harness/types.gen/type-aliases/CreateReview.md b/packages/sdk-docs/reference/generated/agent-harness/types.gen/type-aliases/CreateReview.mdindex f290960355..3a101f7a1c 100644--- a/packages/sdk-docs/reference/generated/agent-harness/types.gen/type-aliases/CreateReview.md+++ b/packages/sdk-docs/reference/generated/agent-harness/types.gen/type-aliases/CreateReview.md@@ -8,7 +8,7 @@ Review creation. Drafts are validated by the action's owning adapter.| Property | Type | Description || ------ | ------ | ------ |-| <a id="property-drafts"></a> `drafts` | `unknown`[] | One to one hundred independent drafts. Email: {subject, body: Markdown, to: [{email, name?}], cc?: [{email, name?}], bcc?: [{email, name?}], replyingToId?, attachments?: [{documentId}]}. Calendar: {title, time: {kind: "timed", startsAt: ISO datetime, endsAt: ISO datetime, timeZone?: IANA zone} or {kind: "allDay", startDate: ISO date, endDate: exclusive ISO date}, description?, location?, attendees?: [{email, isOptional?}]}. Creating any review only presents these drafts; the user acts separately. |+| <a id="property-drafts"></a> `drafts` | `unknown`[] | One to one hundred independent drafts. Email: {subject, body: Markdown, to: [{email, name?}], cc?: [{email, name?}], bcc?: [{email, name?}], replyingToId?, attachments?: [{documentId}]}. Calendar: {title, time: {kind: "timed", startsAt: ISO datetime, endsAt: ISO datetime, timeZone?: IANA zone} or {kind: "allDay", startDate: ISO date, endDate: exclusive ISO date}, description?, location?, attendees?: [{email, isOptional?}]}. Import: exactly one draft {source: "notion"}, presenting the plan ImportFromNotion staged. Creating any review only presents these drafts; the user acts separately. || <a id="property-executionid"></a> `executionId?` | `null` \| [`ExecutionId`](ExecutionId.md) | - || <a id="property-kind"></a> `kind` | [`ReviewKind`](ReviewKind.md) | Existing UI and action to use. || <a id="property-title"></a> `title` | `string` | Stack label. |diff --git a/packages/sdk-docs/reference/generated/agent-harness/types.gen/type-aliases/ReportAgentSessionAppConnectedData.md b/packages/sdk-docs/reference/generated/agent-harness/types.gen/type-aliases/ReportAgentSessionAppConnectedData.mdnew file mode 100644index 0000000000..797811c098--- /dev/null+++ b/packages/sdk-docs/reference/generated/agent-harness/types.gen/type-aliases/ReportAgentSessionAppConnectedData.md@@ -0,0 +1,13 @@+# Type Alias: ReportAgentSessionAppConnectedData++> **ReportAgentSessionAppConnectedData** = \{ `body`: [`AppConnectedRequest`](AppConnectedRequest.md); `path`: \{ `session_id`: `string`; \}; `query?`: `never`; `url`: `"/agent-sessions/{session_id}/app-connections"`; \}++## Properties++| Property | Type | Description |+| ------ | ------ | ------ |+| <a id="property-body"></a> `body` | [`AppConnectedRequest`](AppConnectedRequest.md) | - |+| <a id="property-path"></a> `path` | \{ `session_id`: `string`; \} | - |+| `path.session_id` | `string` | ID of the agent session |+| <a id="property-query"></a> `query?` | `never` | - |+| <a id="property-url"></a> `url` | `"/agent-sessions/{session_id}/app-connections"` | - |diff --git a/packages/sdk-docs/reference/generated/agent-harness/types.gen/type-aliases/ReportAgentSessionAppConnectedError.md b/packages/sdk-docs/reference/generated/agent-harness/types.gen/type-aliases/ReportAgentSessionAppConnectedError.mdnew file mode 100644index 0000000000..6311b2d1d1--- /dev/null+++ b/packages/sdk-docs/reference/generated/agent-harness/types.gen/type-aliases/ReportAgentSessionAppConnectedError.md@@ -0,0 +1,3 @@+# Type Alias: ReportAgentSessionAppConnectedError++> **ReportAgentSessionAppConnectedError** = [`ReportAgentSessionAppConnectedErrors`](ReportAgentSessionAppConnectedErrors.md)\[keyof [`ReportAgentSessionAppConnectedErrors`](ReportAgentSessionAppConnectedErrors.md)\]diff --git a/packages/sdk-docs/reference/generated/agent-harness/types.gen/type-aliases/ReportAgentSessionAppConnectedErrors.md b/packages/sdk-docs/reference/generated/agent-harness/types.gen/type-aliases/ReportAgentSessionAppConnectedErrors.mdnew file mode 100644index 0000000000..f0b8a22861--- /dev/null+++ b/packages/sdk-docs/reference/generated/agent-harness/types.gen/type-aliases/ReportAgentSessionAppConnectedErrors.md@@ -0,0 +1,13 @@+# Type Alias: ReportAgentSessionAppConnectedErrors++> **ReportAgentSessionAppConnectedErrors** = \{ `401`: `string`; `403`: `string`; `409`: `string`; `422`: `string`; `500`: `string`; \}++## Properties++| Property | Type | Description |+| ------ | ------ | ------ |+| <a id="property-401"></a> `401` | `string` | - |+| <a id="property-403"></a> `403` | `string` | Not the session's owner, or no edit access |+| <a id="property-409"></a> `409` | `string` | The session is archived |+| <a id="property-422"></a> `422` | `string` | The owner has not connected that app |+| <a id="property-500"></a> `500` | `string` | - |diff --git a/packages/sdk-docs/reference/generated/agent-harness/types.gen/type-aliases/ReportAgentSessionAppConnectedResponses.md b/packages/sdk-docs/reference/generated/agent-harness/types.gen/type-aliases/ReportAgentSessionAppConnectedResponses.mdnew file mode 100644index 0000000000..b867f62a77--- /dev/null+++ b/packages/sdk-docs/reference/generated/agent-harness/types.gen/type-aliases/ReportAgentSessionAppConnectedResponses.md@@ -0,0 +1,9 @@+# Type Alias: ReportAgentSessionAppConnectedResponses++> **ReportAgentSessionAppConnectedResponses** = \{ `202`: `unknown`; \}++## Properties++| Property | Type | Description |+| ------ | ------ | ------ |+| <a id="property-202"></a> `202` | `unknown` | The session continues once it is idle |diff --git a/packages/sdk-docs/reference/generated/agent-harness/types.gen/type-aliases/ReviewKind.md b/packages/sdk-docs/reference/generated/agent-harness/types.gen/type-aliases/ReviewKind.mdindex 87ee856d01..d39811e1f9 100644--- a/packages/sdk-docs/reference/generated/agent-harness/types.gen/type-aliases/ReviewKind.md+++ b/packages/sdk-docs/reference/generated/agent-harness/types.gen/type-aliases/ReviewKind.md@@ -1,5 +1,5 @@# Type Alias: ReviewKind-> **ReviewKind** = `"email"` \| `"calendar"`+> **ReviewKind** = `"email"` \| `"calendar"` \| `"import"`The component and action used to finish a review.diff --git a/packages/sdk-docs/reference/generated/cognition/types.gen/README.md b/packages/sdk-docs/reference/generated/cognition/types.gen/README.mdindex ba4934ba68..171d218a38 100644--- a/packages/sdk-docs/reference/generated/cognition/types.gen/README.md+++ b/packages/sdk-docs/reference/generated/cognition/types.gen/README.md@@ -9,7 +9,6 @@- [AddMcpServerResponse](type-aliases/AddMcpServerResponse.md)- [AddMcpServerResponses](type-aliases/AddMcpServerResponses.md)- [AddServerRequest](type-aliases/AddServerRequest.md)-- [AiAdmissionErrorBody](type-aliases/AiAdmissionErrorBody.md)- [AiFeature](type-aliases/AiFeature.md)- [AssistantMessagePart](type-aliases/AssistantMessagePart.md)- [AttachmentMetadata](type-aliases/AttachmentMetadata.md)@@ -26,6 +25,10 @@- [CallToolResponse](type-aliases/CallToolResponse.md)- [CallToolResponse2](type-aliases/CallToolResponse2.md)- [CallToolResponses](type-aliases/CallToolResponses.md)+- [CancelNotionHandlerData](type-aliases/CancelNotionHandlerData.md)+- [CancelNotionHandlerErrors](type-aliases/CancelNotionHandlerErrors.md)+- [CancelNotionHandlerResponse](type-aliases/CancelNotionHandlerResponse.md)+- [CancelNotionHandlerResponses](type-aliases/CancelNotionHandlerResponses.md)- [ChannelSharePermission](type-aliases/ChannelSharePermission.md)- [ChannelType](type-aliases/ChannelType.md)- [Chat](type-aliases/Chat.md)@@ -106,10 +109,6 @@- [DismissPromptHandlerErrors](type-aliases/DismissPromptHandlerErrors.md)- [DismissPromptHandlerResponse](type-aliases/DismissPromptHandlerResponse.md)- [DismissPromptHandlerResponses](type-aliases/DismissPromptHandlerResponses.md)-- [DismissRunHandlerData](type-aliases/DismissRunHandlerData.md)-- [DismissRunHandlerErrors](type-aliases/DismissRunHandlerErrors.md)-- [DismissRunHandlerResponse](type-aliases/DismissRunHandlerResponse.md)-- [DismissRunHandlerResponses](type-aliases/DismissRunHandlerResponses.md)- [DocumentCognitionServiceApiVersion](type-aliases/DocumentCognitionServiceApiVersion.md)- [DocumentReference](type-aliases/DocumentReference.md)- [DocumentTextPart](type-aliases/DocumentTextPart.md)@@ -186,6 +185,7 @@- [HealthHandlerResponses](type-aliases/HealthHandlerResponses.md)- [HttpSendChatMessageRequest](type-aliases/HttpSendChatMessageRequest.md)- [ImportEntity](type-aliases/ImportEntity.md)+- [ImportKind](type-aliases/ImportKind.md)- [ImportRun](type-aliases/ImportRun.md)- [ImportSource](type-aliases/ImportSource.md)- [ImportState](type-aliases/ImportState.md)@@ -218,7 +218,18 @@- [NewAttachment](type-aliases/NewAttachment.md)- [NewChatMessage](type-aliases/NewChatMessage.md)- [NewMessageAttachment](type-aliases/NewMessageAttachment.md)+- [NotionAlreadyImported](type-aliases/NotionAlreadyImported.md)+- [NotionDecision](type-aliases/NotionDecision.md)- [NotionDocMeta](type-aliases/NotionDocMeta.md)+- [NotionImporter](type-aliases/NotionImporter.md)+- [NotionImportReport](type-aliases/NotionImportReport.md)+- [NotionPlanItem](type-aliases/NotionPlanItem.md)+- [NotionReportHandlerData](type-aliases/NotionReportHandlerData.md)+- [NotionReportHandlerErrors](type-aliases/NotionReportHandlerErrors.md)+- [NotionReportHandlerResponse](type-aliases/NotionReportHandlerResponse.md)+- [NotionReportHandlerResponses](type-aliases/NotionReportHandlerResponses.md)+- [NotionReportItem](type-aliases/NotionReportItem.md)+- [NotionTaskFields](type-aliases/NotionTaskFields.md)- [OnboardingRow](type-aliases/OnboardingRow.md)- [OnboardingState](type-aliases/OnboardingState.md)- [OnboardingStatus](type-aliases/OnboardingStatus.md)@@ -246,11 +257,6 @@- [RejectToolCallErrors](type-aliases/RejectToolCallErrors.md)- [RejectToolCallRequest](type-aliases/RejectToolCallRequest.md)- [RejectToolCallResponses](type-aliases/RejectToolCallResponses.md)-- [RetryGatherHandlerData](type-aliases/RetryGatherHandlerData.md)-- [RetryGatherHandlerError](type-aliases/RetryGatherHandlerError.md)-- [RetryGatherHandlerErrors](type-aliases/RetryGatherHandlerErrors.md)-- [RetryGatherHandlerResponse](type-aliases/RetryGatherHandlerResponse.md)-- [RetryGatherHandlerResponses](type-aliases/RetryGatherHandlerResponses.md)- [RevertDeleteChatData](type-aliases/RevertDeleteChatData.md)- [RevertDeleteChatError](type-aliases/RevertDeleteChatError.md)- [RevertDeleteChatErrors](type-aliases/RevertDeleteChatErrors.md)@@ -263,6 +269,7 @@- [RunImportOutcome](type-aliases/RunImportOutcome.md)- [RunImportRequest](type-aliases/RunImportRequest.md)- [RunStatus](type-aliases/RunStatus.md)+- [ScanCounts](type-aliases/ScanCounts.md)- [SendChatMessageData](type-aliases/SendChatMessageData.md)- [SendChatMessageError](type-aliases/SendChatMessageError.md)- [SendChatMessageErrors](type-aliases/SendChatMessageErrors.md)diff --git a/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/AiAdmissionErrorBody.md b/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/AiAdmissionErrorBody.mddeleted file mode 100644index c0a52e48ca..0000000000--- a/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/AiAdmissionErrorBody.md+++ /dev/null@@ -1,13 +0,0 @@-# Type Alias: AiAdmissionErrorBody--> **AiAdmissionErrorBody** = \{ `code`: `string`; `error`: `string`; \}--Public admission error payload. Handlers with additional fields can reuse the-domain error's code and message and [`admission_status`].--## Properties--| Property | Type | Description |-| ------ | ------ | ------ |-| <a id="property-code"></a> `code` | `string` | Stable denial or unavailability code. |-| <a id="property-error"></a> `error` | `string` | Human-readable explanation, without internal billing diagnostics. |diff --git a/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/CancelNotionHandlerData.md b/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/CancelNotionHandlerData.mdnew file mode 100644index 0000000000..571e1356d7--- /dev/null+++ b/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/CancelNotionHandlerData.md@@ -0,0 +1,12 @@+# Type Alias: CancelNotionHandlerData++> **CancelNotionHandlerData** = \{ `body?`: `never`; `path?`: `never`; `query?`: `never`; `url`: `"/import/notion/cancel"`; \}++## Properties++| Property | Type |+| ------ | ------ |+| <a id="property-body"></a> `body?` | `never` |+| <a id="property-path"></a> `path?` | `never` |+| <a id="property-query"></a> `query?` | `never` |+| <a id="property-url"></a> `url` | `"/import/notion/cancel"` |diff --git a/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/CancelNotionHandlerErrors.md b/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/CancelNotionHandlerErrors.mdnew file mode 100644index 0000000000..1cbba37fc3--- /dev/null+++ b/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/CancelNotionHandlerErrors.md@@ -0,0 +1,9 @@+# Type Alias: CancelNotionHandlerErrors++> **CancelNotionHandlerErrors** = \{ `500`: `unknown`; \}++## Properties++| Property | Type | Description |+| ------ | ------ | ------ |+| <a id="property-500"></a> `500` | `unknown` | Internal server error |diff --git a/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/CancelNotionHandlerResponse.md b/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/CancelNotionHandlerResponse.mdnew file mode 100644index 0000000000..47b2767c80--- /dev/null+++ b/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/CancelNotionHandlerResponse.md@@ -0,0 +1,3 @@+# Type Alias: CancelNotionHandlerResponse++> **CancelNotionHandlerResponse** = [`CancelNotionHandlerResponses`](CancelNotionHandlerResponses.md)\[keyof [`CancelNotionHandlerResponses`](CancelNotionHandlerResponses.md)\]diff --git a/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/CancelNotionHandlerResponses.md b/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/CancelNotionHandlerResponses.mdnew file mode 100644index 0000000000..f422d16b05--- /dev/null+++ b/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/CancelNotionHandlerResponses.md@@ -0,0 +1,9 @@+# Type Alias: CancelNotionHandlerResponses++> **CancelNotionHandlerResponses** = \{ `200`: [`NotionImportReport`](NotionImportReport.md); \}++## Properties++| Property | Type | Description |+| ------ | ------ | ------ |+| <a id="property-200"></a> `200` | [`NotionImportReport`](NotionImportReport.md) | Where the import stands after cancelling |diff --git a/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/DiscoverHandlerErrors.md b/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/DiscoverHandlerErrors.mdindex 59d162bfc7..892fdf32f5 100644--- a/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/DiscoverHandlerErrors.md+++ b/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/DiscoverHandlerErrors.md@@ -6,5 +6,5 @@| Property | Type | Description || ------ | ------ | ------ |-| <a id="property-400"></a> `400` | `unknown` | Unknown import source |+| <a id="property-400"></a> `400` | `unknown` | Unknown import source or discovery not supported || <a id="property-500"></a> `500` | `unknown` | Internal server error |diff --git a/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/DismissRunHandlerData.md b/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/DismissRunHandlerData.mddeleted file mode 100644index a868070587..0000000000--- a/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/DismissRunHandlerData.md+++ /dev/null@@ -1,13 +0,0 @@-# Type Alias: DismissRunHandlerData--> **DismissRunHandlerData** = \{ `body?`: `never`; `path`: \{ `source`: `string`; \}; `query?`: `never`; `url`: `"/import/runs/{source}/dismiss"`; \}--## Properties--| Property | Type | Description |-| ------ | ------ | ------ |-| <a id="property-body"></a> `body?` | `never` | - |-| <a id="property-path"></a> `path` | \{ `source`: `string`; \} | - |-| `path.source` | `string` | Import source |-| <a id="property-query"></a> `query?` | `never` | - |-| <a id="property-url"></a> `url` | `"/import/runs/{source}/dismiss"` | - |diff --git a/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/DismissRunHandlerErrors.md b/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/DismissRunHandlerErrors.mddeleted file mode 100644index d61f58c584..0000000000--- a/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/DismissRunHandlerErrors.md+++ /dev/null@@ -1,10 +0,0 @@-# Type Alias: DismissRunHandlerErrors--> **DismissRunHandlerErrors** = \{ `400`: `unknown`; `500`: `unknown`; \}--## Properties--| Property | Type | Description |-| ------ | ------ | ------ |-| <a id="property-400"></a> `400` | `unknown` | Unknown import source |-| <a id="property-500"></a> `500` | `unknown` | Internal server error |diff --git a/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/DismissRunHandlerResponse.md b/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/DismissRunHandlerResponse.mddeleted file mode 100644index 78b22e99df..0000000000--- a/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/DismissRunHandlerResponse.md+++ /dev/null@@ -1,3 +0,0 @@-# Type Alias: DismissRunHandlerResponse--> **DismissRunHandlerResponse** = [`DismissRunHandlerResponses`](DismissRunHandlerResponses.md)\[keyof [`DismissRunHandlerResponses`](DismissRunHandlerResponses.md)\]diff --git a/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/DismissRunHandlerResponses.md b/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/DismissRunHandlerResponses.mddeleted file mode 100644index 8f5fbb5c4a..0000000000--- a/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/DismissRunHandlerResponses.md+++ /dev/null@@ -1,9 +0,0 @@-# Type Alias: DismissRunHandlerResponses--> **DismissRunHandlerResponses** = \{ `204`: `void`; \}--## Properties--| Property | Type | Description |-| ------ | ------ | ------ |-| <a id="property-204"></a> `204` | `void` | Dismissed |diff --git a/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/ImportEntity.md b/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/ImportEntity.mdindex d633672167..f73a653d20 100644--- a/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/ImportEntity.md+++ b/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/ImportEntity.md@@ -1,6 +1,6 @@# Type Alias: ImportEntity-> **ImportEntity** = \{ `created_at`: `string`; `entity_id?`: `string` \| `null`; `entity_type?`: `string` \| `null`; `foreign_id`: `string`; `id`: `string`; `initiator`: [`Initiator`](Initiator.md); `last_error?`: `string` \| `null`; `metadata`: `unknown`; `source`: [`ImportSource`](ImportSource.md); `status`: [`ImportStatus`](ImportStatus.md); `team_id?`: `string` \| `null`; `updated_at`: `string`; `user_id`: `string`; \}+> **ImportEntity** = \{ `created_at`: `string`; `entity_id?`: `string` \| `null`; `entity_type?`: `string` \| `null`; `foreign_id`: `string`; `id`: `string`; `initiator`: [`Initiator`](Initiator.md); `kind`: [`ImportKind`](ImportKind.md); `last_error?`: `string` \| `null`; `metadata`: `unknown`; `parent_id?`: `string` \| `null`; `review_id?`: `string` \| `null`; `source`: [`ImportSource`](ImportSource.md); `status`: [`ImportStatus`](ImportStatus.md); `team_id?`: `string` \| `null`; `updated_at`: `string`; `user_id`: `string`; `waiting_on?`: `string` \| `null`; \}One row of the import ledger.@@ -14,10 +14,14 @@ One row of the import ledger.| <a id="property-foreign_id"></a> `foreign_id` | `string` | Stable id in the source system (see [`ImportSource::normalize_foreign_id`]). || <a id="property-id"></a> `id` | `string` | Ledger row id. || <a id="property-initiator"></a> `initiator` | [`Initiator`](Initiator.md) | Where it was first staged from. |+| <a id="property-kind"></a> `kind` | [`ImportKind`](ImportKind.md) | What the item is in its source system. || <a id="property-last_error"></a> `last_error?` | `string` \| `null` | Failure detail from the last import attempt, when one failed. || <a id="property-metadata"></a> `metadata` | `unknown` | Per-source metadata (shape depends on `source`; see [`LinearIssueMeta`], [`NotionDocMeta`], [`SlackChannelMeta`]). |+| <a id="property-parent_id"></a> `parent_id?` | `string` \| `null` | The import row this item was imported under (a subpage's parent page, a row's database), when it was imported as part of a tree. |+| <a id="property-review_id"></a> `review_id?` | `string` \| `null` | The agent session review that approved importing this item, when it came from one. || <a id="property-source"></a> `source` | [`ImportSource`](ImportSource.md) | The external system the item comes from. || <a id="property-status"></a> `status` | [`ImportStatus`](ImportStatus.md) | Where the row is in its lifecycle. || <a id="property-team_id"></a> `team_id?` | `string` \| `null` | The team the created entity was shared with, when it was. || <a id="property-updated_at"></a> `updated_at` | `string` | When the row last changed. || <a id="property-user_id"></a> `user_id` | `string` | The user who staged/imported it. Team-imported rows from teammates are visible with their owner's id, so clients can say "created by a teammate". |+| <a id="property-waiting_on"></a> `waiting_on?` | `string` \| `null` | The teammate's row this one waits on: that teammate is writing the same object for the team, and this row links to their copy once it is imported. |diff --git a/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/ImportKind.md b/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/ImportKind.mdnew file mode 100644index 0000000000..8c1fb0fa60--- /dev/null+++ b/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/ImportKind.md@@ -0,0 +1,7 @@+# Type Alias: ImportKind++> **ImportKind** = `"issue"` \| `"project"` \| `"document"` \| `"page"` \| `"database"` \| `"data_source"` \| `"row"` \| `"channel"`++What an imported item is in its source system. Each source has its own+kinds; a Notion database and a Notion page, or a Linear project and a+Linear issue, are tracked and deduplicated separately.diff --git a/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/ImportRun.md b/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/ImportRun.mdindex 70eaf3b493..de451f0be9 100644--- a/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/ImportRun.md+++ b/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/ImportRun.md@@ -1,15 +1,16 @@# Type Alias: ImportRun-> **ImportRun** = \{ `auto_import`: `boolean`; `error?`: `string` \| `null`; `source`: [`ImportSource`](ImportSource.md); `status`: [`RunStatus`](RunStatus.md); `updated_at`: `string`; \}+> **ImportRun** = \{ `error?`: `string` \| `null`; `review_id?`: `string` \| `null`; `scanned?`: `null` \| [`ScanCounts`](ScanCounts.md); `source`: [`ImportSource`](ImportSource.md); `status`: [`RunStatus`](RunStatus.md); `updated_at`: `string`; \}-Gather-run state for one user × source.+Discovery-run state for one user × source.## Properties| Property | Type | Description || ------ | ------ | ------ |-| <a id="property-auto_import"></a> `auto_import` | `boolean` | Whether this run should import its onboarding-staged candidates as soon as gathering finishes. |-| <a id="property-error"></a> `error?` | `string` \| `null` | Gather or automatic-import failure detail, when the run failed. |-| <a id="property-source"></a> `source` | [`ImportSource`](ImportSource.md) | The source this run gathered from. |+| <a id="property-error"></a> `error?` | `string` \| `null` | Discovery failure detail, when the run failed. |+| <a id="property-review_id"></a> `review_id?` | `string` \| `null` | The review presenting the run's plan, once one does. A Notion plan is presented while it is still `running`; rows staged after that take it. |+| <a id="property-scanned"></a> `scanned?` | `null` \| [`ScanCounts`](ScanCounts.md) | - |+| <a id="property-source"></a> `source` | [`ImportSource`](ImportSource.md) | The source this run discovered from. || <a id="property-status"></a> `status` | [`RunStatus`](RunStatus.md) | Where the run is in its lifecycle. || <a id="property-updated_at"></a> `updated_at` | `string` | When the run state last changed. |diff --git a/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/ImportState.md b/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/ImportState.mdindex f7d4b72fed..6893aa8555 100644--- a/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/ImportState.md+++ b/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/ImportState.md@@ -2,7 +2,7 @@> **ImportState** = \{ `entities`: [`ImportEntity`](ImportEntity.md)[]; `runs`: [`ImportRun`](ImportRun.md)[]; \}-The full import aggregate for a user: gather runs plus visible ledger+The full import aggregate for a user: discovery runs plus visible ledgerrows (their own, and teammates' team-imported rows).## Properties@@ -10,4 +10,4 @@ rows (their own, and teammates' team-imported rows).| Property | Type | Description || ------ | ------ | ------ || <a id="property-entities"></a> `entities` | [`ImportEntity`](ImportEntity.md)[] | Visible import entities, newest first. |-| <a id="property-runs"></a> `runs` | [`ImportRun`](ImportRun.md)[] | Gather runs, one per source that ever gathered. |+| <a id="property-runs"></a> `runs` | [`ImportRun`](ImportRun.md)[] | Discovery runs, one per source that ever ran discovery. |diff --git a/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/NotionAlreadyImported.md b/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/NotionAlreadyImported.mdnew file mode 100644index 0000000000..9051b231f6--- /dev/null+++ b/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/NotionAlreadyImported.md@@ -0,0 +1,13 @@+# Type Alias: NotionAlreadyImported++> **NotionAlreadyImported** = \{ `by`: `string`; `entityId`: `string`; `entityType`: `string`; \}++A teammate's copy of a Notion object, shared with the user's team.++## Properties++| Property | Type | Description |+| ------ | ------ | ------ |+| <a id="property-by"></a> `by` | `string` | The teammate who imported it, by email. |+| <a id="property-entityid"></a> `entityId` | `string` | The Macro entity it became. |+| <a id="property-entitytype"></a> `entityType` | `string` | That entity's type (`md`, `project`). |diff --git a/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/NotionDecision.md b/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/NotionDecision.mdnew file mode 100644index 0000000000..37ed14c3c3--- /dev/null+++ b/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/NotionDecision.md@@ -0,0 +1,5 @@+# Type Alias: NotionDecision++> **NotionDecision** = `"copy"` \| `"tasks"` \| `"skip"` \| `"hand"`++What a workspace import does with one Notion object.diff --git a/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/NotionDocMeta.md b/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/NotionDocMeta.mdindex 94cb0cb8b7..9326405c62 100644--- a/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/NotionDocMeta.md+++ b/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/NotionDocMeta.md@@ -1,6 +1,6 @@# Type Alias: NotionDocMeta-> **NotionDocMeta** = \{ `summary?`: `string` \| `null`; `title`: `string`; `url?`: `string` \| `null`; \}+> **NotionDocMeta** = \{ `plan?`: `null` \| [`NotionPlanItem`](NotionPlanItem.md); `summary?`: `string` \| `null`; `title`: `string`; `url?`: `string` \| `null`; \}Metadata for one staged Notion page. Deliberately has NO content field:page bodies are fetched at import time, for accepted pages only.@@ -9,6 +9,7 @@ page bodies are fetched at import time, for accepted pages only.| Property | Type | Description || ------ | ------ | ------ |+| <a id="property-plan"></a> `plan?` | `null` \| [`NotionPlanItem`](NotionPlanItem.md) | - || <a id="property-summary"></a> `summary?` | `string` \| `null` | One-line summary of what the page contains. || <a id="property-title"></a> `title` | `string` | Page title. || <a id="property-url"></a> `url?` | `string` \| `null` | Deep link back to the page in Notion. |diff --git a/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/NotionImportReport.md b/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/NotionImportReport.mdnew file mode 100644index 0000000000..95aa65917a--- /dev/null+++ b/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/NotionImportReport.md@@ -0,0 +1,35 @@+# Type Alias: NotionImportReport++> **NotionImportReport** = \{ `alreadyImportedBy?`: [`NotionImporter`](NotionImporter.md)[]; `alreadyInMacro?`: `number`; `copy`: `number`; `databaseRows`: `number`; `databases`: `number`; `dataSources`: `number`; `deciding?`: `number`; `documents`: `number`; `error?`: `string` \| `null`; `failures?`: [`NotionReportItem`](NotionReportItem.md)[]; `hand`: `number`; `handItems?`: [`NotionReportItem`](NotionReportItem.md)[]; `imported`: `number`; `onTeam?`: `boolean`; `pages`: `number`; `reviewId?`: `string` \| `null`; `scanned?`: `null` \| [`ScanCounts`](ScanCounts.md); `sharedWithTeam?`: `number`; `skip`: `number`; `status?`: `null` \| [`RunStatus`](RunStatus.md); `taskRows`: `number`; `tasks`: `number`; `waiting`: `number`; `waitingOnTeammates?`: `number`; `writing`: `number`; \}++Where a Notion workspace import stands.++## Properties++| Property | Type | Description |+| ------ | ------ | ------ |+| <a id="property-alreadyimportedby"></a> `alreadyImportedBy?` | [`NotionImporter`](NotionImporter.md)[] | Who imported the objects already in Macro, most first. |+| <a id="property-alreadyinmacro"></a> `alreadyInMacro?` | `number` | Plan objects a teammate already imported and shared with the team; they link to that copy and are not counted as copies. |+| <a id="property-copy"></a> `copy` | `number` | Objects to copy as they are. |+| <a id="property-databaserows"></a> `databaseRows` | `number` | Rows those databases hold, every one counted while planning. |+| <a id="property-databases"></a> `databases` | `number` | Data sources written as databases. |+| <a id="property-datasources"></a> `dataSources` | `number` | Data sources (database tables) in the plan. |+| <a id="property-deciding"></a> `deciding?` | `number` | Plan objects the decision model has not answered for yet, while planning is still `running`. |+| <a id="property-documents"></a> `documents` | `number` | Pages written as documents. |+| <a id="property-error"></a> `error?` | `string` \| `null` | Why the run failed, when it did. |+| <a id="property-failures"></a> `failures?` | [`NotionReportItem`](NotionReportItem.md)[] | Objects that failed to import, with the failure. |+| <a id="property-hand"></a> `hand` | `number` | Objects that need handling by hand. |+| <a id="property-handitems"></a> `handItems?` | [`NotionReportItem`](NotionReportItem.md)[] | Objects that need handling by hand, with reasons. |+| <a id="property-imported"></a> `imported` | `number` | Plan objects already imported. |+| <a id="property-onteam"></a> `onTeam?` | `boolean` | Whether the user is on a team. Without one, everything the import writes stays visible only to them. |+| <a id="property-pages"></a> `pages` | `number` | Pages in the plan. |+| <a id="property-reviewid"></a> `reviewId?` | `string` \| `null` | The review the plan is tied to, once presented. |+| <a id="property-scanned"></a> `scanned?` | `null` \| [`ScanCounts`](ScanCounts.md) | - |+| <a id="property-sharedwithteam"></a> `sharedWithTeam?` | `number` | Written plan objects shared with the user's team. |+| <a id="property-skip"></a> `skip` | `number` | Objects left in Notion. |+| <a id="property-status"></a> `status?` | `null` \| [`RunStatus`](RunStatus.md) | - |+| <a id="property-taskrows"></a> `taskRows` | `number` | Tasks the task lists become, one per row. |+| <a id="property-tasks"></a> `tasks` | `number` | Data sources whose rows become tasks. |+| <a id="property-waiting"></a> `waiting` | `number` | Plan objects still waiting to be written. |+| <a id="property-waitingonteammates"></a> `waitingOnTeammates?` | `number` | Plan objects waiting for a teammate who is writing the same object for the team; they link to the teammate's copy once it is written. |+| <a id="property-writing"></a> `writing` | `number` | Plan objects being written now. |diff --git a/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/NotionImporter.md b/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/NotionImporter.mdnew file mode 100644index 0000000000..6042ea16cc--- /dev/null+++ b/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/NotionImporter.md@@ -0,0 +1,12 @@+# Type Alias: NotionImporter++> **NotionImporter** = \{ `by`: `string`; `count`: `number`; \}++A teammate who already imported some of a plan's objects.++## Properties++| Property | Type | Description |+| ------ | ------ | ------ |+| <a id="property-by"></a> `by` | `string` | The teammate, by email. |+| <a id="property-count"></a> `count` | `number` | How many of the plan's objects they imported. |diff --git a/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/NotionPlanItem.md b/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/NotionPlanItem.mdnew file mode 100644index 0000000000..f4f3d1e157--- /dev/null+++ b/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/NotionPlanItem.md@@ -0,0 +1,20 @@+# Type Alias: NotionPlanItem++> **NotionPlanItem** = \{ `alreadyImported?`: `null` \| [`NotionAlreadyImported`](NotionAlreadyImported.md); `decision?`: `null` \| [`NotionDecision`](NotionDecision.md); `path?`: `string`[]; `reason?`: `string` \| `null`; `rows?`: `number` \| `null`; `sampleTitles?`: `string`[]; `shareable?`: `boolean`; `shared?`: `boolean`; `sharedReason?`: `string` \| `null`; `taskFields?`: `null` \| [`NotionTaskFields`](NotionTaskFields.md); \}++One object's place in a workspace import plan.++## Properties++| Property | Type | Description |+| ------ | ------ | ------ |+| <a id="property-alreadyimported"></a> `alreadyImported?` | `null` \| [`NotionAlreadyImported`](NotionAlreadyImported.md) | - |+| <a id="property-decision"></a> `decision?` | `null` \| [`NotionDecision`](NotionDecision.md) | - |+| <a id="property-path"></a> `path?` | `string`[] | Titles of the pages above it, outermost first. |+| <a id="property-reason"></a> `reason?` | `string` \| `null` | Why, in a short sentence, when the decision model gave one. |+| <a id="property-rows"></a> `rows?` | `number` \| `null` | For a data source: rows counted while planning. |+| <a id="property-sampletitles"></a> `sampleTitles?` | `string`[] | For a data source: the titles of a few of its rows. |+| <a id="property-shareable"></a> `shareable?` | `boolean` | Whether the user may change `shared`: false for users without a team. |+| <a id="property-shared"></a> `shared?` | `boolean` | Whether what it becomes is shared with the user's team. |+| <a id="property-sharedreason"></a> `sharedReason?` | `string` \| `null` | Why it is (or is not) shared, when the decision model chose or the import could not share it. |+| <a id="property-taskfields"></a> `taskFields?` | `null` \| [`NotionTaskFields`](NotionTaskFields.md) | - |diff --git a/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/NotionReportHandlerData.md b/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/NotionReportHandlerData.mdnew file mode 100644index 0000000000..72eb33dcf0--- /dev/null+++ b/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/NotionReportHandlerData.md@@ -0,0 +1,12 @@+# Type Alias: NotionReportHandlerData++> **NotionReportHandlerData** = \{ `body?`: `never`; `path?`: `never`; `query?`: `never`; `url`: `"/import/notion"`; \}++## Properties++| Property | Type |+| ------ | ------ |+| <a id="property-body"></a> `body?` | `never` |+| <a id="property-path"></a> `path?` | `never` |+| <a id="property-query"></a> `query?` | `never` |+| <a id="property-url"></a> `url` | `"/import/notion"` |diff --git a/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/NotionReportHandlerErrors.md b/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/NotionReportHandlerErrors.mdnew file mode 100644index 0000000000..3f2d8c695f--- /dev/null+++ b/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/NotionReportHandlerErrors.md@@ -0,0 +1,9 @@+# Type Alias: NotionReportHandlerErrors++> **NotionReportHandlerErrors** = \{ `500`: `unknown`; \}++## Properties++| Property | Type | Description |+| ------ | ------ | ------ |+| <a id="property-500"></a> `500` | `unknown` | Internal server error |diff --git a/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/NotionReportHandlerResponse.md b/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/NotionReportHandlerResponse.mdnew file mode 100644index 0000000000..ef9af64ef5--- /dev/null+++ b/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/NotionReportHandlerResponse.md@@ -0,0 +1,3 @@+# Type Alias: NotionReportHandlerResponse++> **NotionReportHandlerResponse** = [`NotionReportHandlerResponses`](NotionReportHandlerResponses.md)\[keyof [`NotionReportHandlerResponses`](NotionReportHandlerResponses.md)\]diff --git a/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/NotionReportHandlerResponses.md b/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/NotionReportHandlerResponses.mdnew file mode 100644index 0000000000..25063cc817--- /dev/null+++ b/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/NotionReportHandlerResponses.md@@ -0,0 +1,9 @@+# Type Alias: NotionReportHandlerResponses++> **NotionReportHandlerResponses** = \{ `200`: [`NotionImportReport`](NotionImportReport.md); \}++## Properties++| Property | Type |+| ------ | ------ |+| <a id="property-200"></a> `200` | [`NotionImportReport`](NotionImportReport.md) |diff --git a/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/NotionReportItem.md b/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/NotionReportItem.mdnew file mode 100644index 0000000000..b9026e983e--- /dev/null+++ b/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/NotionReportItem.md@@ -0,0 +1,15 @@+# Type Alias: NotionReportItem++> **NotionReportItem** = \{ `importId`: `string`; `kind`: [`ImportKind`](ImportKind.md); `reason?`: `string` \| `null`; `title`: `string`; `url?`: `string` \| `null`; \}++One object a workspace import report calls out.++## Properties++| Property | Type | Description |+| ------ | ------ | ------ |+| <a id="property-importid"></a> `importId` | `string` | The ledger row. |+| <a id="property-kind"></a> `kind` | [`ImportKind`](ImportKind.md) | What the object is. |+| <a id="property-reason"></a> `reason?` | `string` \| `null` | Why it is called out: the decision model's reason, or the failure. |+| <a id="property-title"></a> `title` | `string` | The object's title. |+| <a id="property-url"></a> `url?` | `string` \| `null` | Its Notion URL. |diff --git a/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/NotionTaskFields.md b/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/NotionTaskFields.mdnew file mode 100644index 0000000000..2062bbe383--- /dev/null+++ b/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/NotionTaskFields.md@@ -0,0 +1,14 @@+# Type Alias: NotionTaskFields++> **NotionTaskFields** = \{ `assignee?`: `string` \| `null`; `due?`: `string` \| `null`; `priority?`: `string` \| `null`; `status?`: `string` \| `null`; \}++Which Notion columns hold a task list's task fields, by property name.++## Properties++| Property | Type | Description |+| ------ | ------ | ------ |+| <a id="property-assignee"></a> `assignee?` | `string` \| `null` | The assignee column. |+| <a id="property-due"></a> `due?` | `string` \| `null` | The due date column. |+| <a id="property-priority"></a> `priority?` | `string` \| `null` | The priority column. |+| <a id="property-status"></a> `status?` | `string` \| `null` | The status column. |diff --git a/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/RetryGatherHandlerData.md b/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/RetryGatherHandlerData.mddeleted file mode 100644index 3566ccf90a..0000000000--- a/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/RetryGatherHandlerData.md+++ /dev/null@@ -1,13 +0,0 @@-# Type Alias: RetryGatherHandlerData--> **RetryGatherHandlerData** = \{ `body?`: `never`; `path`: \{ `source`: `string`; \}; `query?`: `never`; `url`: `"/import/runs/{source}/retry"`; \}--## Properties--| Property | Type | Description |-| ------ | ------ | ------ |-| <a id="property-body"></a> `body?` | `never` | - |-| <a id="property-path"></a> `path` | \{ `source`: `string`; \} | - |-| `path.source` | `string` | Import source |-| <a id="property-query"></a> `query?` | `never` | - |-| <a id="property-url"></a> `url` | `"/import/runs/{source}/retry"` | - |diff --git a/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/RetryGatherHandlerError.md b/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/RetryGatherHandlerError.mddeleted file mode 100644index 8164fbff2b..0000000000--- a/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/RetryGatherHandlerError.md+++ /dev/null@@ -1,3 +0,0 @@-# Type Alias: RetryGatherHandlerError--> **RetryGatherHandlerError** = [`RetryGatherHandlerErrors`](RetryGatherHandlerErrors.md)\[keyof [`RetryGatherHandlerErrors`](RetryGatherHandlerErrors.md)\]diff --git a/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/RetryGatherHandlerErrors.md b/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/RetryGatherHandlerErrors.mddeleted file mode 100644index acb5a912c1..0000000000--- a/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/RetryGatherHandlerErrors.md+++ /dev/null@@ -1,12 +0,0 @@-# Type Alias: RetryGatherHandlerErrors--> **RetryGatherHandlerErrors** = \{ `400`: `unknown`; `402`: [`AiAdmissionErrorBody`](AiAdmissionErrorBody.md); `500`: `unknown`; `503`: [`AiAdmissionErrorBody`](AiAdmissionErrorBody.md); \}--## Properties--| Property | Type | Description |-| ------ | ------ | ------ |-| <a id="property-400"></a> `400` | `unknown` | Unknown import source |-| <a id="property-402"></a> `402` | [`AiAdmissionErrorBody`](AiAdmissionErrorBody.md) | AI allowance exhausted |-| <a id="property-500"></a> `500` | `unknown` | Internal server error |-| <a id="property-503"></a> `503` | [`AiAdmissionErrorBody`](AiAdmissionErrorBody.md) | AI usage validation unavailable; retry later |diff --git a/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/RetryGatherHandlerResponse.md b/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/RetryGatherHandlerResponse.mddeleted file mode 100644index 3d3b409318..0000000000--- a/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/RetryGatherHandlerResponse.md+++ /dev/null@@ -1,3 +0,0 @@-# Type Alias: RetryGatherHandlerResponse--> **RetryGatherHandlerResponse** = [`RetryGatherHandlerResponses`](RetryGatherHandlerResponses.md)\[keyof [`RetryGatherHandlerResponses`](RetryGatherHandlerResponses.md)\]diff --git a/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/RetryGatherHandlerResponses.md b/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/RetryGatherHandlerResponses.mddeleted file mode 100644index fbb0dff13b..0000000000--- a/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/RetryGatherHandlerResponses.md+++ /dev/null@@ -1,9 +0,0 @@-# Type Alias: RetryGatherHandlerResponses--> **RetryGatherHandlerResponses** = \{ `204`: `void`; \}--## Properties--| Property | Type | Description |-| ------ | ------ | ------ |-| <a id="property-204"></a> `204` | `void` | Retry accepted (idempotent) |diff --git a/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/RunStatus.md b/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/RunStatus.mdindex 613fdfa5f0..a614f4971f 100644--- a/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/RunStatus.md+++ b/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/RunStatus.md@@ -2,4 +2,4 @@> **RunStatus** = `"running"` \| `"ready"` \| `"importing"` \| `"completed"` \| `"failed"` \| `"dismissed"`-Lifecycle of a gather run (one per user × source).+Lifecycle of a discovery run (one per user × source).diff --git a/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/ScanCounts.md b/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/ScanCounts.mdnew file mode 100644index 0000000000..22fc3e3d6e--- /dev/null+++ b/packages/sdk-docs/reference/generated/cognition/types.gen/type-aliases/ScanCounts.md@@ -0,0 +1,12 @@+# Type Alias: ScanCounts++> **ScanCounts** = \{ `dataSources`: `number`; `pages`: `number`; \}++What a planning run has found in the source so far.++## Properties++| Property | Type | Description |+| ------ | ------ | ------ |+| <a id="property-datasources"></a> `dataSources` | `number` | Data sources (database tables) found. |+| <a id="property-pages"></a> `pages` | `number` | Pages found. |diff --git a/packages/sdk/generated/agent-harness/index.ts b/packages/sdk/generated/agent-harness/index.tsindex a598eb2ebe..a18c50dba1 100644--- a/packages/sdk/generated/agent-harness/index.ts+++ b/packages/sdk/generated/agent-harness/index.ts@@ -1,4 +1,4 @@// This file is auto-generated by @hey-api/openapi-tsexport { type Options, Sdk } from './sdk.gen';-export type { AccessLevel, AgentAction, AgentActionId, AgentConfigKindDto, AgentConfigOptionDto, AgentConfigSelectOptionDto, AgentConversationDto, AgentConversationsDto, AgentConversationSessionDto, AgentConversationTurnDto, AgentModelDto, AgentModelsStatusDto, AgentPermissionAction, AgentPromptAction, AgentRepositoriesResponse, AgentRepositoryBranchesResponse, AgentRepositoryDto, AgentRespondElicitationAction, AgentSessionChangesPatchResponse, AgentSessionChangesResponse, AgentSessionFoldedLogResponse, AgentSessionLogEntryDto, AgentSessionLogResponse, AgentSessionPreviewData, AgentSessionPreviewDto, AgentSessionQueueResponse, AgentSessionResponse, AgentSessionsForPullRequestData, AgentSessionsForPullRequestError, AgentSessionsForPullRequestErrors, AgentSessionsForPullRequestResponse, AgentSessionsForPullRequestResponses, AgentSessionsForPullRequestsData, AgentSessionsForPullRequestsError, AgentSessionsForPullRequestsErrors, AgentSessionsForPullRequestsResponse, AgentSessionsForPullRequestsResponses, AgentSetConfigOptionAction, AgentSetModelAction, AiAdmissionErrorBody, AnswerAgentSessionToolApprovalData, AnswerAgentSessionToolApprovalError, AnswerAgentSessionToolApprovalErrors, AnswerAgentSessionToolApprovalResponse, AnswerAgentSessionToolApprovalResponses, AnswerToolApprovalRequest, AnswerToolApprovalResponse, BotId, CapabilityHarnessDto, CaptureAttemptDto, CaptureOutcomeDto, ChangedFileDto, ChangesetDto, ChangesetSourceDto, ChannelSharePermission, ClientOptions, CodingPreferencesBody, ControlAgentSessionData, ControlAgentSessionError, ControlAgentSessionErrors, ControlAgentSessionResponse, ControlAgentSessionResponses, ControlRequest, ControlResponse, ControlStatusDto, ConversationTurnState, CreateAgentSessionData, CreateAgentSessionError, CreateAgentSessionErrors, CreateAgentSessionRequest, CreateAgentSessionResponse, CreateAgentSessionResponse2, CreateAgentSessionResponses, CreateReview, CreateSessionReviewData, CreateSessionReviewErrors, CreateSessionReviewResponse, CreateSessionReviewResponses, CreateSessionThread, DecideSessionReviewData, DecideSessionReviewErrors, DecideSessionReviewResponse, DecideSessionReviewResponses, DecisionVia, DeleteAgentSessionData, DeleteAgentSessionError, DeleteAgentSessionErrors, DeleteAgentSessionResponses, DiscoverAgentCapabilitiesHandlerData, DiscoverAgentCapabilitiesHandlerErrors, DiscoverAgentCapabilitiesHandlerResponse, DiscoverAgentCapabilitiesHandlerResponses, DiscoverAgentCapabilitiesRequest, DiscoverAgentCapabilitiesResponse, DocumentId, EditQueuedActionData, EditQueuedActionError, EditQueuedActionErrors, EditQueuedActionRequest, EditQueuedActionResponse, EditQueuedActionResponses, ElicitationAnswer, ElicitationContentValue, ElicitationRequestId, ExecutionActivity, ExecutionId, ExecutionRecord, ExecutionStatus, ExternalSessionResponse, FileChangeKindDto, GetAgentCodingPreferencesData, GetAgentCodingPreferencesError, GetAgentCodingPreferencesErrors, GetAgentCodingPreferencesResponse, GetAgentCodingPreferencesResponses, GetAgentSandboxSizeData, GetAgentSandboxSizeError, GetAgentSandboxSizeErrors, GetAgentSandboxSizeResponse, GetAgentSandboxSizeResponses, GetAgentSessionChangesData, GetAgentSessionChangesError, GetAgentSessionChangesErrors, GetAgentSessionChangesPatchData, GetAgentSessionChangesPatchError, GetAgentSessionChangesPatchErrors, GetAgentSessionChangesPatchResponse, GetAgentSessionChangesPatchResponses, GetAgentSessionChangesResponse, GetAgentSessionChangesResponses, GetAgentSessionData, GetAgentSessionError, GetAgentSessionErrors, GetAgentSessionFoldedLogData, GetAgentSessionFoldedLogError, GetAgentSessionFoldedLogErrors, GetAgentSessionFoldedLogResponse, GetAgentSessionFoldedLogResponses, GetAgentSessionLogData, GetAgentSessionLogError, GetAgentSessionLogErrors, GetAgentSessionLogResponse, GetAgentSessionLogResponses, GetAgentSessionPermissionsData, GetAgentSessionPermissionsError, GetAgentSessionPermissionsErrors, GetAgentSessionPermissionsResponse, GetAgentSessionPermissionsResponses, GetAgentSessionQueueData, GetAgentSessionQueueError, GetAgentSessionQueueErrors, GetAgentSessionQueueResponse, GetAgentSessionQueueResponses, GetAgentSessionResponse, GetAgentSessionResponses, GetCodeExecutionData, GetCodeExecutionErrors, GetCodeExecutionResponse, GetCodeExecutionResponses, GetSessionReviewData, GetSessionReviewErrors, GetSessionReviewResponse, GetSessionReviewResponses, GitRefDto, LinkAgentSessionPullRequestData, LinkAgentSessionPullRequestError, LinkAgentSessionPullRequestErrors, LinkAgentSessionPullRequestResponse, LinkAgentSessionPullRequestResponses, LinkShare, ListAgentConversationsData, ListAgentConversationsErrors, ListAgentConversationsResponse, ListAgentConversationsResponses, ListAgentRepositoriesData, ListAgentRepositoriesErrors, ListAgentRepositoriesResponse, ListAgentRepositoriesResponses, ListAgentRepositoryBranchesData, ListAgentRepositoryBranchesErrors, ListAgentRepositoryBranchesResponse, ListAgentRepositoryBranchesResponses, ListAgentSessionPullRequestsData, ListAgentSessionPullRequestsError, ListAgentSessionPullRequestsErrors, ListAgentSessionPullRequestsResponse, ListAgentSessionPullRequestsResponses, ListExecutionReviewsData, ListExecutionReviewsErrors, ListExecutionReviewsResponse, ListExecutionReviewsResponses, LoadAgentModelsHandlerData, LoadAgentModelsHandlerErrors, LoadAgentModelsHandlerResponse, LoadAgentModelsHandlerResponses, LoadAgentModelsRequest, LoadAgentModelsResponse, LogDirectionDto, LogFrameDto, MessageParent, ModelHarnessDto, PermissionAnswer, PreviewAgentSessionsData, PreviewAgentSessionsError, PreviewAgentSessionsErrors, PreviewAgentSessionsRequest, PreviewAgentSessionsResponse, PreviewAgentSessionsResponse2, PreviewAgentSessionsResponses, PromptAttachment, PullRequestLinkedSession, PullRequestLinkSource, PullRequestSessions, PullRequestSessionsResponse, PullRequestsSessionsResponse, PullRequestUrl, PullRequestUrls, PutAgentCodingPreferencesData, PutAgentCodingPreferencesError, PutAgentCodingPreferencesErrors, PutAgentCodingPreferencesResponse, PutAgentCodingPreferencesResponses, PutAgentSandboxSizeData, PutAgentSandboxSizeError, PutAgentSandboxSizeErrors, PutAgentSandboxSizeResponse, PutAgentSandboxSizeResponses, PutAgentSessionSandboxSizeData, PutAgentSessionSandboxSizeError, PutAgentSessionSandboxSizeErrors, PutAgentSessionSandboxSizeResponse, PutAgentSessionSandboxSizeResponses, QueuedActionDto, RecordedCallStatus, RecordedToolCall, RefreshAgentSessionChangesData, RefreshAgentSessionChangesError, RefreshAgentSessionChangesErrors, RefreshAgentSessionChangesResponse, RefreshAgentSessionChangesResponses, RemoveQueuedActionData, RemoveQueuedActionError, RemoveQueuedActionErrors, RemoveQueuedActionResponse, RemoveQueuedActionResponses, RenameAgentSessionData, RenameAgentSessionError, RenameAgentSessionErrors, RenameAgentSessionRequest, RenameAgentSessionResponse, RenameAgentSessionResponses, RetryAgentConversationTurnData, RetryAgentConversationTurnErrors, RetryAgentConversationTurnRequest, RetryAgentConversationTurnResponse, RetryAgentConversationTurnResponses, ReviewDecision, ReviewDecisionContext, ReviewId, ReviewItem, ReviewItemStatus, ReviewKind, ReviewRecord, SandboxSize, SandboxSizeBody, SessionBot, SessionPullRequestLink, SessionPullRequestsResponse, SessionStatusDto, SetAgentSessionArchivedData, SetAgentSessionArchivedError, SetAgentSessionArchivedErrors, SetAgentSessionArchivedRequest, SetAgentSessionArchivedResponse, SetAgentSessionArchivedResponses, SharePermissionV2, StartFreshAgentConversationData, StartFreshAgentConversationErrors, StartFreshAgentConversationRequest, StartFreshAgentConversationResponse, StartFreshAgentConversationResponses, SteerQueuedActionData, SteerQueuedActionError, SteerQueuedActionErrors, SteerQueuedActionResponse, SteerQueuedActionResponses, ToolActor, ToolApprovalAnswerDto, ToolApprovalStatusDto, UnlinkAgentSessionPullRequestData, UnlinkAgentSessionPullRequestError, UnlinkAgentSessionPullRequestErrors, UnlinkAgentSessionPullRequestResponse, UnlinkAgentSessionPullRequestResponses, UpdateAgentSessionPermissionsData, UpdateAgentSessionPermissionsError, UpdateAgentSessionPermissionsErrors, UpdateAgentSessionPermissionsResponse, UpdateAgentSessionPermissionsResponses, UpdateChannelSharePermission, UpdateOperation, UpdateSharePermissionRequestV2, WarmAgentSessionHandlerData, WarmAgentSessionHandlerResponse, WarmAgentSessionHandlerResponses, WarmAgentSessionRequest, WarmAgentSessionResponse, WithAgentSessionId } from './types.gen';+export type { AccessLevel, AgentAction, AgentActionId, AgentConfigKindDto, AgentConfigOptionDto, AgentConfigSelectOptionDto, AgentConversationDto, AgentConversationsDto, AgentConversationSessionDto, AgentConversationTurnDto, AgentModelDto, AgentModelsStatusDto, AgentPermissionAction, AgentPromptAction, AgentRepositoriesResponse, AgentRepositoryBranchesResponse, AgentRepositoryDto, AgentRespondElicitationAction, AgentSessionChangesPatchResponse, AgentSessionChangesResponse, AgentSessionFoldedLogResponse, AgentSessionLogEntryDto, AgentSessionLogResponse, AgentSessionPreviewData, AgentSessionPreviewDto, AgentSessionQueueResponse, AgentSessionResponse, AgentSessionsForPullRequestData, AgentSessionsForPullRequestError, AgentSessionsForPullRequestErrors, AgentSessionsForPullRequestResponse, AgentSessionsForPullRequestResponses, AgentSessionsForPullRequestsData, AgentSessionsForPullRequestsError, AgentSessionsForPullRequestsErrors, AgentSessionsForPullRequestsResponse, AgentSessionsForPullRequestsResponses, AgentSetConfigOptionAction, AgentSetModelAction, AiAdmissionErrorBody, AnswerAgentSessionToolApprovalData, AnswerAgentSessionToolApprovalError, AnswerAgentSessionToolApprovalErrors, AnswerAgentSessionToolApprovalResponse, AnswerAgentSessionToolApprovalResponses, AnswerToolApprovalRequest, AnswerToolApprovalResponse, AppConnectedRequest, BotId, CapabilityHarnessDto, CaptureAttemptDto, CaptureOutcomeDto, ChangedFileDto, ChangesetDto, ChangesetSourceDto, ChannelSharePermission, ClientOptions, CodingPreferencesBody, ControlAgentSessionData, ControlAgentSessionError, ControlAgentSessionErrors, ControlAgentSessionResponse, ControlAgentSessionResponses, ControlRequest, ControlResponse, ControlStatusDto, ConversationTurnState, CreateAgentSessionData, CreateAgentSessionError, CreateAgentSessionErrors, CreateAgentSessionRequest, CreateAgentSessionResponse, CreateAgentSessionResponse2, CreateAgentSessionResponses, CreateReview, CreateSessionReviewData, CreateSessionReviewErrors, CreateSessionReviewResponse, CreateSessionReviewResponses, CreateSessionThread, DecideSessionReviewData, DecideSessionReviewErrors, DecideSessionReviewResponse, DecideSessionReviewResponses, DecisionVia, DeleteAgentSessionData, DeleteAgentSessionError, DeleteAgentSessionErrors, DeleteAgentSessionResponses, DiscoverAgentCapabilitiesHandlerData, DiscoverAgentCapabilitiesHandlerErrors, DiscoverAgentCapabilitiesHandlerResponse, DiscoverAgentCapabilitiesHandlerResponses, DiscoverAgentCapabilitiesRequest, DiscoverAgentCapabilitiesResponse, DocumentId, EditQueuedActionData, EditQueuedActionError, EditQueuedActionErrors, EditQueuedActionRequest, EditQueuedActionResponse, EditQueuedActionResponses, ElicitationAnswer, ElicitationContentValue, ElicitationRequestId, ExecutionActivity, ExecutionId, ExecutionRecord, ExecutionStatus, ExternalSessionResponse, FileChangeKindDto, GetAgentCodingPreferencesData, GetAgentCodingPreferencesError, GetAgentCodingPreferencesErrors, GetAgentCodingPreferencesResponse, GetAgentCodingPreferencesResponses, GetAgentSandboxSizeData, GetAgentSandboxSizeError, GetAgentSandboxSizeErrors, GetAgentSandboxSizeResponse, GetAgentSandboxSizeResponses, GetAgentSessionChangesData, GetAgentSessionChangesError, GetAgentSessionChangesErrors, GetAgentSessionChangesPatchData, GetAgentSessionChangesPatchError, GetAgentSessionChangesPatchErrors, GetAgentSessionChangesPatchResponse, GetAgentSessionChangesPatchResponses, GetAgentSessionChangesResponse, GetAgentSessionChangesResponses, GetAgentSessionData, GetAgentSessionError, GetAgentSessionErrors, GetAgentSessionFoldedLogData, GetAgentSessionFoldedLogError, GetAgentSessionFoldedLogErrors, GetAgentSessionFoldedLogResponse, GetAgentSessionFoldedLogResponses, GetAgentSessionLogData, GetAgentSessionLogError, GetAgentSessionLogErrors, GetAgentSessionLogResponse, GetAgentSessionLogResponses, GetAgentSessionPermissionsData, GetAgentSessionPermissionsError, GetAgentSessionPermissionsErrors, GetAgentSessionPermissionsResponse, GetAgentSessionPermissionsResponses, GetAgentSessionQueueData, GetAgentSessionQueueError, GetAgentSessionQueueErrors, GetAgentSessionQueueResponse, GetAgentSessionQueueResponses, GetAgentSessionResponse, GetAgentSessionResponses, GetCodeExecutionData, GetCodeExecutionErrors, GetCodeExecutionResponse, GetCodeExecutionResponses, GetSessionReviewData, GetSessionReviewErrors, GetSessionReviewResponse, GetSessionReviewResponses, GitRefDto, LinkAgentSessionPullRequestData, LinkAgentSessionPullRequestError, LinkAgentSessionPullRequestErrors, LinkAgentSessionPullRequestResponse, LinkAgentSessionPullRequestResponses, LinkShare, ListAgentConversationsData, ListAgentConversationsErrors, ListAgentConversationsResponse, ListAgentConversationsResponses, ListAgentRepositoriesData, ListAgentRepositoriesErrors, ListAgentRepositoriesResponse, ListAgentRepositoriesResponses, ListAgentRepositoryBranchesData, ListAgentRepositoryBranchesErrors, ListAgentRepositoryBranchesResponse, ListAgentRepositoryBranchesResponses, ListAgentSessionPullRequestsData, ListAgentSessionPullRequestsError, ListAgentSessionPullRequestsErrors, ListAgentSessionPullRequestsResponse, ListAgentSessionPullRequestsResponses, ListExecutionReviewsData, ListExecutionReviewsErrors, ListExecutionReviewsResponse, ListExecutionReviewsResponses, LoadAgentModelsHandlerData, LoadAgentModelsHandlerErrors, LoadAgentModelsHandlerResponse, LoadAgentModelsHandlerResponses, LoadAgentModelsRequest, LoadAgentModelsResponse, LogDirectionDto, LogFrameDto, MessageParent, ModelHarnessDto, PermissionAnswer, PreviewAgentSessionsData, PreviewAgentSessionsError, PreviewAgentSessionsErrors, PreviewAgentSessionsRequest, PreviewAgentSessionsResponse, PreviewAgentSessionsResponse2, PreviewAgentSessionsResponses, PromptAttachment, PullRequestLinkedSession, PullRequestLinkSource, PullRequestSessions, PullRequestSessionsResponse, PullRequestsSessionsResponse, PullRequestUrl, PullRequestUrls, PutAgentCodingPreferencesData, PutAgentCodingPreferencesError, PutAgentCodingPreferencesErrors, PutAgentCodingPreferencesResponse, PutAgentCodingPreferencesResponses, PutAgentSandboxSizeData, PutAgentSandboxSizeError, PutAgentSandboxSizeErrors, PutAgentSandboxSizeResponse, PutAgentSandboxSizeResponses, PutAgentSessionSandboxSizeData, PutAgentSessionSandboxSizeError, PutAgentSessionSandboxSizeErrors, PutAgentSessionSandboxSizeResponse, PutAgentSessionSandboxSizeResponses, QueuedActionDto, RecordedCallStatus, RecordedToolCall, RefreshAgentSessionChangesData, RefreshAgentSessionChangesError, RefreshAgentSessionChangesErrors, RefreshAgentSessionChangesResponse, RefreshAgentSessionChangesResponses, RemoveQueuedActionData, RemoveQueuedActionError, RemoveQueuedActionErrors, RemoveQueuedActionResponse, RemoveQueuedActionResponses, RenameAgentSessionData, RenameAgentSessionError, RenameAgentSessionErrors, RenameAgentSessionRequest, RenameAgentSessionResponse, RenameAgentSessionResponses, ReportAgentSessionAppConnectedData, ReportAgentSessionAppConnectedError, ReportAgentSessionAppConnectedErrors, ReportAgentSessionAppConnectedResponses, RetryAgentConversationTurnData, RetryAgentConversationTurnErrors, RetryAgentConversationTurnRequest, RetryAgentConversationTurnResponse, RetryAgentConversationTurnResponses, ReviewDecision, ReviewDecisionContext, ReviewId, ReviewItem, ReviewItemStatus, ReviewKind, ReviewRecord, SandboxSize, SandboxSizeBody, SessionBot, SessionPullRequestLink, SessionPullRequestsResponse, SessionStatusDto, SetAgentSessionArchivedData, SetAgentSessionArchivedError, SetAgentSessionArchivedErrors, SetAgentSessionArchivedRequest, SetAgentSessionArchivedResponse, SetAgentSessionArchivedResponses, SharePermissionV2, StartFreshAgentConversationData, StartFreshAgentConversationErrors, StartFreshAgentConversationRequest, StartFreshAgentConversationResponse, StartFreshAgentConversationResponses, SteerQueuedActionData, SteerQueuedActionError, SteerQueuedActionErrors, SteerQueuedActionResponse, SteerQueuedActionResponses, ToolActor, ToolApprovalAnswerDto, ToolApprovalStatusDto, UnlinkAgentSessionPullRequestData, UnlinkAgentSessionPullRequestError, UnlinkAgentSessionPullRequestErrors, UnlinkAgentSessionPullRequestResponse, UnlinkAgentSessionPullRequestResponses, UpdateAgentSessionPermissionsData, UpdateAgentSessionPermissionsError, UpdateAgentSessionPermissionsErrors, UpdateAgentSessionPermissionsResponse, UpdateAgentSessionPermissionsResponses, UpdateChannelSharePermission, UpdateOperation, UpdateSharePermissionRequestV2, WarmAgentSessionHandlerData, WarmAgentSessionHandlerResponse, WarmAgentSessionHandlerResponses, WarmAgentSessionRequest, WarmAgentSessionResponse, WithAgentSessionId } from './types.gen';diff --git a/packages/sdk/generated/agent-harness/sdk.gen.ts b/packages/sdk/generated/agent-harness/sdk.gen.tsindex 12fc6e22fe..dcb198eff3 100644--- a/packages/sdk/generated/agent-harness/sdk.gen.ts+++ b/packages/sdk/generated/agent-harness/sdk.gen.ts@@ -2,7 +2,7 @@import type { Client, ClientMeta, Options as Options2, RequestResult, TDataShape } from './client';import { client } from './client.gen';-import type { AgentSessionsForPullRequestData, AgentSessionsForPullRequestErrors, AgentSessionsForPullRequestResponses, AgentSessionsForPullRequestsData, AgentSessionsForPullRequestsErrors, AgentSessionsForPullRequestsResponses, AnswerAgentSessionToolApprovalData, AnswerAgentSessionToolApprovalErrors, AnswerAgentSessionToolApprovalResponses, ControlAgentSessionData, ControlAgentSessionErrors, ControlAgentSessionResponses, CreateAgentSessionData, CreateAgentSessionErrors, CreateAgentSessionResponses, CreateSessionReviewData, CreateSessionReviewErrors, CreateSessionReviewResponses, DecideSessionReviewData, DecideSessionReviewErrors, DecideSessionReviewResponses, DeleteAgentSessionData, DeleteAgentSessionErrors, DeleteAgentSessionResponses, DiscoverAgentCapabilitiesHandlerData, DiscoverAgentCapabilitiesHandlerErrors, DiscoverAgentCapabilitiesHandlerResponses, EditQueuedActionData, EditQueuedActionErrors, EditQueuedActionResponses, GetAgentCodingPreferencesData, GetAgentCodingPreferencesErrors, GetAgentCodingPreferencesResponses, GetAgentSandboxSizeData, GetAgentSandboxSizeErrors, GetAgentSandboxSizeResponses, GetAgentSessionChangesData, GetAgentSessionChangesErrors, GetAgentSessionChangesPatchData, GetAgentSessionChangesPatchErrors, GetAgentSessionChangesPatchResponses, GetAgentSessionChangesResponses, GetAgentSessionData, GetAgentSessionErrors, GetAgentSessionFoldedLogData, GetAgentSessionFoldedLogErrors, GetAgentSessionFoldedLogResponses, GetAgentSessionLogData, GetAgentSessionLogErrors, GetAgentSessionLogResponses, GetAgentSessionPermissionsData, GetAgentSessionPermissionsErrors, GetAgentSessionPermissionsResponses, GetAgentSessionQueueData, GetAgentSessionQueueErrors, GetAgentSessionQueueResponses, GetAgentSessionResponses, GetCodeExecutionData, GetCodeExecutionErrors, GetCodeExecutionResponses, GetSessionReviewData, GetSessionReviewErrors, GetSessionReviewResponses, LinkAgentSessionPullRequestData, LinkAgentSessionPullRequestErrors, LinkAgentSessionPullRequestResponses, ListAgentConversationsData, ListAgentConversationsErrors, ListAgentConversationsResponses, ListAgentRepositoriesData, ListAgentRepositoriesErrors, ListAgentRepositoriesResponses, ListAgentRepositoryBranchesData, ListAgentRepositoryBranchesErrors, ListAgentRepositoryBranchesResponses, ListAgentSessionPullRequestsData, ListAgentSessionPullRequestsErrors, ListAgentSessionPullRequestsResponses, ListExecutionReviewsData, ListExecutionReviewsErrors, ListExecutionReviewsResponses, LoadAgentModelsHandlerData, LoadAgentModelsHandlerErrors, LoadAgentModelsHandlerResponses, PreviewAgentSessionsData, PreviewAgentSessionsErrors, PreviewAgentSessionsResponses, PutAgentCodingPreferencesData, PutAgentCodingPreferencesErrors, PutAgentCodingPreferencesResponses, PutAgentSandboxSizeData, PutAgentSandboxSizeErrors, PutAgentSandboxSizeResponses, PutAgentSessionSandboxSizeData, PutAgentSessionSandboxSizeErrors, PutAgentSessionSandboxSizeResponses, RefreshAgentSessionChangesData, RefreshAgentSessionChangesErrors, RefreshAgentSessionChangesResponses, RemoveQueuedActionData, RemoveQueuedActionErrors, RemoveQueuedActionResponses, RenameAgentSessionData, RenameAgentSessionErrors, RenameAgentSessionResponses, RetryAgentConversationTurnData, RetryAgentConversationTurnErrors, RetryAgentConversationTurnResponses, SetAgentSessionArchivedData, SetAgentSessionArchivedErrors, SetAgentSessionArchivedResponses, StartFreshAgentConversationData, StartFreshAgentConversationErrors, StartFreshAgentConversationResponses, SteerQueuedActionData, SteerQueuedActionErrors, SteerQueuedActionResponses, UnlinkAgentSessionPullRequestData, UnlinkAgentSessionPullRequestErrors, UnlinkAgentSessionPullRequestResponses, UpdateAgentSessionPermissionsData, UpdateAgentSessionPermissionsErrors, UpdateAgentSessionPermissionsResponses, WarmAgentSessionHandlerData, WarmAgentSessionHandlerResponses } from './types.gen';+import type { AgentSessionsForPullRequestData, AgentSessionsForPullRequestErrors, AgentSessionsForPullRequestResponses, AgentSessionsForPullRequestsData, AgentSessionsForPullRequestsErrors, AgentSessionsForPullRequestsResponses, AnswerAgentSessionToolApprovalData, AnswerAgentSessionToolApprovalErrors, AnswerAgentSessionToolApprovalResponses, ControlAgentSessionData, ControlAgentSessionErrors, ControlAgentSessionResponses, CreateAgentSessionData, CreateAgentSessionErrors, CreateAgentSessionResponses, CreateSessionReviewData, CreateSessionReviewErrors, CreateSessionReviewResponses, DecideSessionReviewData, DecideSessionReviewErrors, DecideSessionReviewResponses, DeleteAgentSessionData, DeleteAgentSessionErrors, DeleteAgentSessionResponses, DiscoverAgentCapabilitiesHandlerData, DiscoverAgentCapabilitiesHandlerErrors, DiscoverAgentCapabilitiesHandlerResponses, EditQueuedActionData, EditQueuedActionErrors, EditQueuedActionResponses, GetAgentCodingPreferencesData, GetAgentCodingPreferencesErrors, GetAgentCodingPreferencesResponses, GetAgentSandboxSizeData, GetAgentSandboxSizeErrors, GetAgentSandboxSizeResponses, GetAgentSessionChangesData, GetAgentSessionChangesErrors, GetAgentSessionChangesPatchData, GetAgentSessionChangesPatchErrors, GetAgentSessionChangesPatchResponses, GetAgentSessionChangesResponses, GetAgentSessionData, GetAgentSessionErrors, GetAgentSessionFoldedLogData, GetAgentSessionFoldedLogErrors, GetAgentSessionFoldedLogResponses, GetAgentSessionLogData, GetAgentSessionLogErrors, GetAgentSessionLogResponses, GetAgentSessionPermissionsData, GetAgentSessionPermissionsErrors, GetAgentSessionPermissionsResponses, GetAgentSessionQueueData, GetAgentSessionQueueErrors, GetAgentSessionQueueResponses, GetAgentSessionResponses, GetCodeExecutionData, GetCodeExecutionErrors, GetCodeExecutionResponses, GetSessionReviewData, GetSessionReviewErrors, GetSessionReviewResponses, LinkAgentSessionPullRequestData, LinkAgentSessionPullRequestErrors, LinkAgentSessionPullRequestResponses, ListAgentConversationsData, ListAgentConversationsErrors, ListAgentConversationsResponses, ListAgentRepositoriesData, ListAgentRepositoriesErrors, ListAgentRepositoriesResponses, ListAgentRepositoryBranchesData, ListAgentRepositoryBranchesErrors, ListAgentRepositoryBranchesResponses, ListAgentSessionPullRequestsData, ListAgentSessionPullRequestsErrors, ListAgentSessionPullRequestsResponses, ListExecutionReviewsData, ListExecutionReviewsErrors, ListExecutionReviewsResponses, LoadAgentModelsHandlerData, LoadAgentModelsHandlerErrors, LoadAgentModelsHandlerResponses, PreviewAgentSessionsData, PreviewAgentSessionsErrors, PreviewAgentSessionsResponses, PutAgentCodingPreferencesData, PutAgentCodingPreferencesErrors, PutAgentCodingPreferencesResponses, PutAgentSandboxSizeData, PutAgentSandboxSizeErrors, PutAgentSandboxSizeResponses, PutAgentSessionSandboxSizeData, PutAgentSessionSandboxSizeErrors, PutAgentSessionSandboxSizeResponses, RefreshAgentSessionChangesData, RefreshAgentSessionChangesErrors, RefreshAgentSessionChangesResponses, RemoveQueuedActionData, RemoveQueuedActionErrors, RemoveQueuedActionResponses, RenameAgentSessionData, RenameAgentSessionErrors, RenameAgentSessionResponses, ReportAgentSessionAppConnectedData, ReportAgentSessionAppConnectedErrors, ReportAgentSessionAppConnectedResponses, RetryAgentConversationTurnData, RetryAgentConversationTurnErrors, RetryAgentConversationTurnResponses, SetAgentSessionArchivedData, SetAgentSessionArchivedErrors, SetAgentSessionArchivedResponses, StartFreshAgentConversationData, StartFreshAgentConversationErrors, StartFreshAgentConversationResponses, SteerQueuedActionData, SteerQueuedActionErrors, SteerQueuedActionResponses, UnlinkAgentSessionPullRequestData, UnlinkAgentSessionPullRequestErrors, UnlinkAgentSessionPullRequestResponses, UpdateAgentSessionPermissionsData, UpdateAgentSessionPermissionsErrors, UpdateAgentSessionPermissionsResponses, WarmAgentSessionHandlerData, WarmAgentSessionHandlerResponses } from './types.gen';export type Options<TData extends TDataShape = TDataShape, ThrowOnError extends boolean = boolean, TResponse = unknown> = Options2<TData, ThrowOnError, TResponse> & {/**@@ -294,6 +294,23 @@ export class Sdk extends HeyApiClient {return (options.client ?? this.client).get<GetAgentSessionResponses, GetAgentSessionErrors, ThrowOnError>({ url: '/agent-sessions/{session_id}', ...options });}+ /**+ * Continue the session now that its owner connected an app it asked for.+ *+ * The harness produces an `app.connected` system event, which the transcript+ * shows as no prompt; it starts a turn once the session is idle.+ */+ public reportAgentSessionAppConnected<ThrowOnError extends boolean = false>(options: Options<ReportAgentSessionAppConnectedData, ThrowOnError>): RequestResult<ReportAgentSessionAppConnectedResponses, ReportAgentSessionAppConnectedErrors, ThrowOnError> {+ return (options.client ?? this.client).post<ReportAgentSessionAppConnectedResponses, ReportAgentSessionAppConnectedErrors, ThrowOnError>({+ url: '/agent-sessions/{session_id}/app-connections',+ ...options,+ headers: {+ 'Content-Type': 'application/json',+ ...options.headers+ }+ });+ }+/*** Archive or unarchive an agent session.*/diff --git a/packages/sdk/generated/agent-harness/types.gen.ts b/packages/sdk/generated/agent-harness/types.gen.tsindex 8e2d12c67a..646fec4be9 100644--- a/packages/sdk/generated/agent-harness/types.gen.ts+++ b/packages/sdk/generated/agent-harness/types.gen.ts@@ -660,6 +660,17 @@ export type AnswerToolApprovalResponse = {status: ToolApprovalStatusDto;};+/**+ * Request body for reporting a connected app.+ */+export type AppConnectedRequest = {+ actionId?: null | AgentActionId;+ /**+ * The Pipedream app slug the owner connected, e.g. `notion`.+ */+ appSlug: string;+};+export type BotId = string;/**@@ -976,7 +987,8 @@ export type CreateReview = {* time: {kind: "timed", startsAt: ISO datetime, endsAt: ISO datetime,* timeZone?: IANA zone} or {kind: "allDay", startDate: ISO date,* endDate: exclusive ISO date}, description?, location?, attendees?:- * [{email, isOptional?}]}.+ * [{email, isOptional?}]}. Import: exactly one draft {source: "notion"},+ * presenting the plan ImportFromNotion staged.* Creating any review only presents these drafts; the user acts separately.*/drafts: Array<unknown>;@@ -1682,7 +1694,7 @@ export type ReviewItemStatus = 'pending' | 'submitting' | 'completed' | 'dismiss/*** The component and action used to finish a review.*/-export type ReviewKind = 'email' | 'calendar';+export type ReviewKind = 'email' | 'calendar' | 'import';/*** Full UI record, fetched outside the model context.@@ -2430,6 +2442,44 @@ export type GetAgentSessionResponses = {export type GetAgentSessionResponse = GetAgentSessionResponses[keyof GetAgentSessionResponses];+export type ReportAgentSessionAppConnectedData = {+ body: AppConnectedRequest;+ path: {+ /**+ * ID of the agent session+ */+ session_id: string;+ };+ query?: never;+ url: '/agent-sessions/{session_id}/app-connections';+};++export type ReportAgentSessionAppConnectedErrors = {+ 401: string;+ /**+ * Not the session's owner, or no edit access+ */+ 403: string;+ /**+ * The session is archived+ */+ 409: string;+ /**+ * The owner has not connected that app+ */+ 422: string;+ 500: string;+};++export type ReportAgentSessionAppConnectedError = ReportAgentSessionAppConnectedErrors[keyof ReportAgentSessionAppConnectedErrors];++export type ReportAgentSessionAppConnectedResponses = {+ /**+ * The session continues once it is idle+ */+ 202: unknown;+};+export type SetAgentSessionArchivedData = {body: SetAgentSessionArchivedRequest;path: {diff --git a/packages/sdk/generated/cognition/index.ts b/packages/sdk/generated/cognition/index.tsindex 106616acb6..fb82f5a9e3 100644--- a/packages/sdk/generated/cognition/index.ts+++ b/packages/sdk/generated/cognition/index.ts@@ -1,4 +1,4 @@// This file is auto-generated by @hey-api/openapi-tsexport { type Options, Sdk } from './sdk.gen';-export type { AccessLevel, AddMcpServerData, AddMcpServerError, AddMcpServerErrors, AddMcpServerResponse, AddMcpServerResponses, AddServerRequest, AiAdmissionErrorBody, AiFeature, AssistantMessagePart, AttachmentMetadata, AttachmentType, BrowsePipedreamMcpCatalogData, BrowsePipedreamMcpCatalogError, BrowsePipedreamMcpCatalogErrors, BrowsePipedreamMcpCatalogResponse, BrowsePipedreamMcpCatalogResponses, CallToolData, CallToolError, CallToolErrors, CallToolRequest, CallToolResponse, CallToolResponse2, CallToolResponses, ChannelSharePermission, ChannelType, Chat, ChatAttachment, ChatHistory, ChatHistoryBatchMessagesRequest, ChatMessage, ChatMessageContent, ChatMessageError, ChatMessageWithAttachments, ChatPreview, ChatPreviewData, ChatResponse, ChatStream, ClientOptions, CompleteHandlerData, CompleteHandlerErrors, CompleteHandlerResponse, CompleteHandlerResponses, CompleteOnboardingRequest, CompletePipedreamMcpConnectionData, CompletePipedreamMcpConnectionError, CompletePipedreamMcpConnectionErrors, CompletePipedreamMcpConnectionResponse, CompletePipedreamMcpConnectionResponses, CompletionUsage, ConnectedServer, ConversationRecord, CopyChatData, CopyChatError, CopyChatErrors, CopyChatResponse, CopyChatResponses, CreateChatData, CreateChatError, CreateChatErrors, CreateChatRequest, CreateChatResponse, CreateChatResponses, CreatePipedreamMcpTokenData, CreatePipedreamMcpTokenError, CreatePipedreamMcpTokenErrors, CreatePipedreamMcpTokenResponse, CreatePipedreamMcpTokenResponses, DatabaseChange, DecisionErrorBody, DecisionRequest, DecisionResponse, DecisionsData, DecisionsError, DecisionsErrors, DecisionsResponse, DecisionsResponses, DeleteChatData, DeleteChatError, DeleteChatErrors, DeleteChatResponses, DeleteMcpServerData, DeleteMcpServerError, DeleteMcpServerErrors, DeleteMcpServerResponse, DeleteMcpServerResponses, DeletePipedreamMcpConnectionData, DeletePipedreamMcpConnectionError, DeletePipedreamMcpConnectionErrors, DeletePipedreamMcpConnectionResponse, DeletePipedreamMcpConnectionResponses, DiscoverHandlerData, DiscoverHandlerErrors, DiscoverHandlerResponse, DiscoverHandlerResponses, DismissedPrompts, DismissedPromptsHandlerData, DismissedPromptsHandlerErrors, DismissedPromptsHandlerResponse, DismissedPromptsHandlerResponses, DismissPromptHandlerData, DismissPromptHandlerErrors, DismissPromptHandlerResponse, DismissPromptHandlerResponses, DismissRunHandlerData, DismissRunHandlerErrors, DismissRunHandlerResponse, DismissRunHandlerResponses, DocumentCognitionServiceApiVersion, DocumentReference, DocumentTextPart, DynamicSchema, Entity, EntityType, ErrorBody, ErrorResponse, Expiry, FeatureUsage, FileType, FreeTierModel, GenericErrorResponse, GetBatchPreviewData, GetBatchPreviewError, GetBatchPreviewErrors, GetBatchPreviewRequest, GetBatchPreviewResponse, GetBatchPreviewResponse2, GetBatchPreviewResponses, GetChatData, GetChatError, GetChatErrors, GetChatHistoryBatchMessagesHandlerData, GetChatHistoryBatchMessagesHandlerError, GetChatHistoryBatchMessagesHandlerErrors, GetChatHistoryBatchMessagesHandlerResponse, GetChatHistoryBatchMessagesHandlerResponses, GetChatHistoryHandlerData, GetChatHistoryHandlerError, GetChatHistoryHandlerErrors, GetChatHistoryHandlerResponse, GetChatHistoryHandlerResponses, GetChatPermissionsData, GetChatPermissionsError, GetChatPermissionsErrors, GetChatPermissionsResponse, GetChatPermissionsResponse2, GetChatPermissionsResponses, GetChatResponse, GetChatResponse2, GetChatResponses, GetChatsForAttachmentHandlerData, GetChatsForAttachmentHandlerError, GetChatsForAttachmentHandlerErrors, GetChatsForAttachmentHandlerResponse, GetChatsForAttachmentHandlerResponses, GetChatsForAttachmentResponse, GetCitationHandlerData, GetCitationHandlerError, GetCitationHandlerErrors, GetCitationHandlerResponse, GetCitationHandlerResponses, GetMemoryHandlerData, GetMemoryHandlerError, GetMemoryHandlerErrors, GetMemoryHandlerResponse, GetMemoryHandlerResponses, GetStateHandler2Data, GetStateHandler2Errors, GetStateHandler2Response, GetStateHandler2Responses, GetStateHandlerData, GetStateHandlerErrors, GetStateHandlerResponse, GetStateHandlerResponses, GetUsageHandlerData, GetUsageHandlerError, GetUsageHandlerErrors, GetUsageHandlerResponse, GetUsageHandlerResponses, HealthHandlerData, HealthHandlerResponse, HealthHandlerResponses, HttpSendChatMessageRequest, ImportEntity, ImportRun, ImportSource, ImportState, ImportStatus, Initiator, JwtPayload, LinearIssueMeta, LinkShare, ListMcpServersData, ListMcpServersError, ListMcpServersErrors, ListMcpServersResponse, ListMcpServersResponses, ListPipedreamMcpConnectionsData, ListPipedreamMcpConnectionsError, ListPipedreamMcpConnectionsErrors, ListPipedreamMcpConnectionsResponse, ListPipedreamMcpConnectionsResponses, MacroUserIdStr, McpAuthCallbackData, McpAuthCallbackError, McpAuthCallbackErrors, McpAuthCallbackResponses, McpOauthClientMetadataData, McpOauthClientMetadataResponses, MemoryErrorBody, MemoryResponse, MessageWithAttachments, ModelSpeed, NewAttachment, NewChatMessage, NewMessageAttachment, NotionDocMeta, OnboardingRow, OnboardingState, OnboardingStatus, PatchChatData, PatchChatError, PatchChatErrors, PatchChatRequest, PatchChatResponses, PermanentlyDeleteChatData, PermanentlyDeleteChatError, PermanentlyDeleteChatErrors, PermanentlyDeleteChatResponses, PipedreamCatalogEntryResponse, PipedreamCatalogResponse, PipedreamCompleteRequest, PipedreamConnectionResponse, PipedreamTokenResponse, PipedreamUpdateRequest, Price, ProjectionStateResponse, ProjectionStatus, RefreshCadence, RejectToolCallData, RejectToolCallError, RejectToolCallErrors, RejectToolCallRequest, RejectToolCallResponses, RetryGatherHandlerData, RetryGatherHandlerError, RetryGatherHandlerErrors, RetryGatherHandlerResponse, RetryGatherHandlerResponses, RevertDeleteChatData, RevertDeleteChatError, RevertDeleteChatErrors, RevertDeleteChatResponses, Role, RunImportHandlerData, RunImportHandlerErrors, RunImportHandlerResponse, RunImportHandlerResponses, RunImportOutcome, RunImportRequest, RunStatus, SendChatMessageData, SendChatMessageError, SendChatMessageErrors, SendChatMessagePayload, SendChatMessageResponse, SendChatMessageResponse2, SendChatMessageResponses, ServerResponse, SetPricingHandlerData, SetPricingHandlerError, SetPricingHandlerErrors, SetPricingHandlerResponses, SetPricingRequest, SharePermissionV2, SlackChannelMeta, SlackParticipant, StartAuthRequest, StartAuthResponse, StartMcpAuthData, StartMcpAuthError, StartMcpAuthErrors, StartMcpAuthResponse, StartMcpAuthResponses, StopChatStreamData, StopChatStreamError, StopChatStreamError2, StopChatStreamErrors, StopChatStreamRequest, StopChatStreamResponse, StopChatStreamResponse2, StopChatStreamResponses, StreamError, StringIdResponse, StructuredCompletionData, StructuredCompletionError, StructuredCompletionError2, StructuredCompletionErrors, StructuredCompletionOutcome, StructuredCompletionRequest, StructuredCompletionResponse, StructuredCompletionResponse2, StructuredCompletionResponses, StructuredToolActivity, TargetType, ToolOutcome, ToolSet, UpdateChannelSharePermission, UpdateMcpServerData, UpdateMcpServerError, UpdateMcpServerErrors, UpdateMcpServerResponse, UpdateMcpServerResponses, UpdateOperation, UpdatePipedreamMcpConnectionData, UpdatePipedreamMcpConnectionError, UpdatePipedreamMcpConnectionErrors, UpdatePipedreamMcpConnectionResponse, UpdatePipedreamMcpConnectionResponses, UpdateServerRequest, UpdateSharePermissionRequestV2, UpdateToolCallData, UpdateToolCallError, UpdateToolCallErrors, UpdateToolCallRequest, UpdateToolCallResponses, UpdateToolResponseData, UpdateToolResponseError, UpdateToolResponseErrors, UpdateToolResponseRequest, UpdateToolResponseResponses, UpsertAiProjectionData, UpsertAiProjectionError, UpsertAiProjectionErrors, UpsertAiProjectionResponse, UpsertAiProjectionResponses, UpsertProjectionRequest, Usage, UsageRequest, UsageSummary, UserPdfRect, UserToolResponseValue, WebCitation, WithChatId } from './types.gen';+export type { AccessLevel, AddMcpServerData, AddMcpServerError, AddMcpServerErrors, AddMcpServerResponse, AddMcpServerResponses, AddServerRequest, AiFeature, AssistantMessagePart, AttachmentMetadata, AttachmentType, BrowsePipedreamMcpCatalogData, BrowsePipedreamMcpCatalogError, BrowsePipedreamMcpCatalogErrors, BrowsePipedreamMcpCatalogResponse, BrowsePipedreamMcpCatalogResponses, CallToolData, CallToolError, CallToolErrors, CallToolRequest, CallToolResponse, CallToolResponse2, CallToolResponses, CancelNotionHandlerData, CancelNotionHandlerErrors, CancelNotionHandlerResponse, CancelNotionHandlerResponses, ChannelSharePermission, ChannelType, Chat, ChatAttachment, ChatHistory, ChatHistoryBatchMessagesRequest, ChatMessage, ChatMessageContent, ChatMessageError, ChatMessageWithAttachments, ChatPreview, ChatPreviewData, ChatResponse, ChatStream, ClientOptions, CompleteHandlerData, CompleteHandlerErrors, CompleteHandlerResponse, CompleteHandlerResponses, CompleteOnboardingRequest, CompletePipedreamMcpConnectionData, CompletePipedreamMcpConnectionError, CompletePipedreamMcpConnectionErrors, CompletePipedreamMcpConnectionResponse, CompletePipedreamMcpConnectionResponses, CompletionUsage, ConnectedServer, ConversationRecord, CopyChatData, CopyChatError, CopyChatErrors, CopyChatResponse, CopyChatResponses, CreateChatData, CreateChatError, CreateChatErrors, CreateChatRequest, CreateChatResponse, CreateChatResponses, CreatePipedreamMcpTokenData, CreatePipedreamMcpTokenError, CreatePipedreamMcpTokenErrors, CreatePipedreamMcpTokenResponse, CreatePipedreamMcpTokenResponses, DatabaseChange, DecisionErrorBody, DecisionRequest, DecisionResponse, DecisionsData, DecisionsError, DecisionsErrors, DecisionsResponse, DecisionsResponses, DeleteChatData, DeleteChatError, DeleteChatErrors, DeleteChatResponses, DeleteMcpServerData, DeleteMcpServerError, DeleteMcpServerErrors, DeleteMcpServerResponse, DeleteMcpServerResponses, DeletePipedreamMcpConnectionData, DeletePipedreamMcpConnectionError, DeletePipedreamMcpConnectionErrors, DeletePipedreamMcpConnectionResponse, DeletePipedreamMcpConnectionResponses, DiscoverHandlerData, DiscoverHandlerErrors, DiscoverHandlerResponse, DiscoverHandlerResponses, DismissedPrompts, DismissedPromptsHandlerData, DismissedPromptsHandlerErrors, DismissedPromptsHandlerResponse, DismissedPromptsHandlerResponses, DismissPromptHandlerData, DismissPromptHandlerErrors, DismissPromptHandlerResponse, DismissPromptHandlerResponses, DocumentCognitionServiceApiVersion, DocumentReference, DocumentTextPart, DynamicSchema, Entity, EntityType, ErrorBody, ErrorResponse, Expiry, FeatureUsage, FileType, FreeTierModel, GenericErrorResponse, GetBatchPreviewData, GetBatchPreviewError, GetBatchPreviewErrors, GetBatchPreviewRequest, GetBatchPreviewResponse, GetBatchPreviewResponse2, GetBatchPreviewResponses, GetChatData, GetChatError, GetChatErrors, GetChatHistoryBatchMessagesHandlerData, GetChatHistoryBatchMessagesHandlerError, GetChatHistoryBatchMessagesHandlerErrors, GetChatHistoryBatchMessagesHandlerResponse, GetChatHistoryBatchMessagesHandlerResponses, GetChatHistoryHandlerData, GetChatHistoryHandlerError, GetChatHistoryHandlerErrors, GetChatHistoryHandlerResponse, GetChatHistoryHandlerResponses, GetChatPermissionsData, GetChatPermissionsError, GetChatPermissionsErrors, GetChatPermissionsResponse, GetChatPermissionsResponse2, GetChatPermissionsResponses, GetChatResponse, GetChatResponse2, GetChatResponses, GetChatsForAttachmentHandlerData, GetChatsForAttachmentHandlerError, GetChatsForAttachmentHandlerErrors, GetChatsForAttachmentHandlerResponse, GetChatsForAttachmentHandlerResponses, GetChatsForAttachmentResponse, GetCitationHandlerData, GetCitationHandlerError, GetCitationHandlerErrors, GetCitationHandlerResponse, GetCitationHandlerResponses, GetMemoryHandlerData, GetMemoryHandlerError, GetMemoryHandlerErrors, GetMemoryHandlerResponse, GetMemoryHandlerResponses, GetStateHandler2Data, GetStateHandler2Errors, GetStateHandler2Response, GetStateHandler2Responses, GetStateHandlerData, GetStateHandlerErrors, GetStateHandlerResponse, GetStateHandlerResponses, GetUsageHandlerData, GetUsageHandlerError, GetUsageHandlerErrors, GetUsageHandlerResponse, GetUsageHandlerResponses, HealthHandlerData, HealthHandlerResponse, HealthHandlerResponses, HttpSendChatMessageRequest, ImportEntity, ImportKind, ImportRun, ImportSource, ImportState, ImportStatus, Initiator, JwtPayload, LinearIssueMeta, LinkShare, ListMcpServersData, ListMcpServersError, ListMcpServersErrors, ListMcpServersResponse, ListMcpServersResponses, ListPipedreamMcpConnectionsData, ListPipedreamMcpConnectionsError, ListPipedreamMcpConnectionsErrors, ListPipedreamMcpConnectionsResponse, ListPipedreamMcpConnectionsResponses, MacroUserIdStr, McpAuthCallbackData, McpAuthCallbackError, McpAuthCallbackErrors, McpAuthCallbackResponses, McpOauthClientMetadataData, McpOauthClientMetadataResponses, MemoryErrorBody, MemoryResponse, MessageWithAttachments, ModelSpeed, NewAttachment, NewChatMessage, NewMessageAttachment, NotionAlreadyImported, NotionDecision, NotionDocMeta, NotionImporter, NotionImportReport, NotionPlanItem, NotionReportHandlerData, NotionReportHandlerErrors, NotionReportHandlerResponse, NotionReportHandlerResponses, NotionReportItem, NotionTaskFields, OnboardingRow, OnboardingState, OnboardingStatus, PatchChatData, PatchChatError, PatchChatErrors, PatchChatRequest, PatchChatResponses, PermanentlyDeleteChatData, PermanentlyDeleteChatError, PermanentlyDeleteChatErrors, PermanentlyDeleteChatResponses, PipedreamCatalogEntryResponse, PipedreamCatalogResponse, PipedreamCompleteRequest, PipedreamConnectionResponse, PipedreamTokenResponse, PipedreamUpdateRequest, Price, ProjectionStateResponse, ProjectionStatus, RefreshCadence, RejectToolCallData, RejectToolCallError, RejectToolCallErrors, RejectToolCallRequest, RejectToolCallResponses, RevertDeleteChatData, RevertDeleteChatError, RevertDeleteChatErrors, RevertDeleteChatResponses, Role, RunImportHandlerData, RunImportHandlerErrors, RunImportHandlerResponse, RunImportHandlerResponses, RunImportOutcome, RunImportRequest, RunStatus, ScanCounts, SendChatMessageData, SendChatMessageError, SendChatMessageErrors, SendChatMessagePayload, SendChatMessageResponse, SendChatMessageResponse2, SendChatMessageResponses, ServerResponse, SetPricingHandlerData, SetPricingHandlerError, SetPricingHandlerErrors, SetPricingHandlerResponses, SetPricingRequest, SharePermissionV2, SlackChannelMeta, SlackParticipant, StartAuthRequest, StartAuthResponse, StartMcpAuthData, StartMcpAuthError, StartMcpAuthErrors, StartMcpAuthResponse, StartMcpAuthResponses, StopChatStreamData, StopChatStreamError, StopChatStreamError2, StopChatStreamErrors, StopChatStreamRequest, StopChatStreamResponse, StopChatStreamResponse2, StopChatStreamResponses, StreamError, StringIdResponse, StructuredCompletionData, StructuredCompletionError, StructuredCompletionError2, StructuredCompletionErrors, StructuredCompletionOutcome, StructuredCompletionRequest, StructuredCompletionResponse, StructuredCompletionResponse2, StructuredCompletionResponses, StructuredToolActivity, TargetType, ToolOutcome, ToolSet, UpdateChannelSharePermission, UpdateMcpServerData, UpdateMcpServerError, UpdateMcpServerErrors, UpdateMcpServerResponse, UpdateMcpServerResponses, UpdateOperation, UpdatePipedreamMcpConnectionData, UpdatePipedreamMcpConnectionError, UpdatePipedreamMcpConnectionErrors, UpdatePipedreamMcpConnectionResponse, UpdatePipedreamMcpConnectionResponses, UpdateServerRequest, UpdateSharePermissionRequestV2, UpdateToolCallData, UpdateToolCallError, UpdateToolCallErrors, UpdateToolCallRequest, UpdateToolCallResponses, UpdateToolResponseData, UpdateToolResponseError, UpdateToolResponseErrors, UpdateToolResponseRequest, UpdateToolResponseResponses, UpsertAiProjectionData, UpsertAiProjectionError, UpsertAiProjectionErrors, UpsertAiProjectionResponse, UpsertAiProjectionResponses, UpsertProjectionRequest, Usage, UsageRequest, UsageSummary, UserPdfRect, UserToolResponseValue, WebCitation, WithChatId } from './types.gen';diff --git a/packages/sdk/generated/cognition/sdk.gen.ts b/packages/sdk/generated/cognition/sdk.gen.tsindex 6dd5f2e5c6..1397a395c4 100644--- a/packages/sdk/generated/cognition/sdk.gen.ts+++ b/packages/sdk/generated/cognition/sdk.gen.ts@@ -2,7 +2,7 @@import type { Client, ClientMeta, Options as Options2, RequestResult, TDataShape } from './client';import { client } from './client.gen';-import type { AddMcpServerData, AddMcpServerErrors, AddMcpServerResponses, BrowsePipedreamMcpCatalogData, BrowsePipedreamMcpCatalogErrors, BrowsePipedreamMcpCatalogResponses, CallToolData, CallToolErrors, CallToolResponses, CompleteHandlerData, CompleteHandlerErrors, CompleteHandlerResponses, CompletePipedreamMcpConnectionData, CompletePipedreamMcpConnectionErrors, CompletePipedreamMcpConnectionResponses, CopyChatData, CopyChatErrors, CopyChatResponses, CreateChatData, CreateChatErrors, CreateChatResponses, CreatePipedreamMcpTokenData, CreatePipedreamMcpTokenErrors, CreatePipedreamMcpTokenResponses, DecisionsData, DecisionsErrors, DecisionsResponses, DeleteChatData, DeleteChatErrors, DeleteChatResponses, DeleteMcpServerData, DeleteMcpServerErrors, DeleteMcpServerResponses, DeletePipedreamMcpConnectionData, DeletePipedreamMcpConnectionErrors, DeletePipedreamMcpConnectionResponses, DiscoverHandlerData, DiscoverHandlerErrors, DiscoverHandlerResponses, DismissedPromptsHandlerData, DismissedPromptsHandlerErrors, DismissedPromptsHandlerResponses, DismissPromptHandlerData, DismissPromptHandlerErrors, DismissPromptHandlerResponses, DismissRunHandlerData, DismissRunHandlerErrors, DismissRunHandlerResponses, GetBatchPreviewData, GetBatchPreviewErrors, GetBatchPreviewResponses, GetChatData, GetChatErrors, GetChatHistoryBatchMessagesHandlerData, GetChatHistoryBatchMessagesHandlerErrors, GetChatHistoryBatchMessagesHandlerResponses, GetChatHistoryHandlerData, GetChatHistoryHandlerErrors, GetChatHistoryHandlerResponses, GetChatPermissionsData, GetChatPermissionsErrors, GetChatPermissionsResponses, GetChatResponses, GetChatsForAttachmentHandlerData, GetChatsForAttachmentHandlerErrors, GetChatsForAttachmentHandlerResponses, GetCitationHandlerData, GetCitationHandlerErrors, GetCitationHandlerResponses, GetMemoryHandlerData, GetMemoryHandlerErrors, GetMemoryHandlerResponses, GetStateHandler2Data, GetStateHandler2Errors, GetStateHandler2Responses, GetStateHandlerData, GetStateHandlerErrors, GetStateHandlerResponses, GetUsageHandlerData, GetUsageHandlerErrors, GetUsageHandlerResponses, HealthHandlerData, HealthHandlerResponses, ListMcpServersData, ListMcpServersErrors, ListMcpServersResponses, ListPipedreamMcpConnectionsData, ListPipedreamMcpConnectionsErrors, ListPipedreamMcpConnectionsResponses, McpAuthCallbackData, McpAuthCallbackErrors, McpAuthCallbackResponses, McpOauthClientMetadataData, McpOauthClientMetadataResponses, PatchChatData, PatchChatErrors, PatchChatResponses, PermanentlyDeleteChatData, PermanentlyDeleteChatErrors, PermanentlyDeleteChatResponses, RejectToolCallData, RejectToolCallErrors, RejectToolCallResponses, RetryGatherHandlerData, RetryGatherHandlerErrors, RetryGatherHandlerResponses, RevertDeleteChatData, RevertDeleteChatErrors, RevertDeleteChatResponses, RunImportHandlerData, RunImportHandlerErrors, RunImportHandlerResponses, SendChatMessageData, SendChatMessageErrors, SendChatMessageResponses, SetPricingHandlerData, SetPricingHandlerErrors, SetPricingHandlerResponses, StartMcpAuthData, StartMcpAuthErrors, StartMcpAuthResponses, StopChatStreamData, StopChatStreamErrors, StopChatStreamResponses, StructuredCompletionData, StructuredCompletionErrors, StructuredCompletionResponses, UpdateMcpServerData, UpdateMcpServerErrors, UpdateMcpServerResponses, UpdatePipedreamMcpConnectionData, UpdatePipedreamMcpConnectionErrors, UpdatePipedreamMcpConnectionResponses, UpdateToolCallData, UpdateToolCallErrors, UpdateToolCallResponses, UpdateToolResponseData, UpdateToolResponseErrors, UpdateToolResponseResponses, UpsertAiProjectionData, UpsertAiProjectionErrors, UpsertAiProjectionResponses } from './types.gen';+import type { AddMcpServerData, AddMcpServerErrors, AddMcpServerResponses, BrowsePipedreamMcpCatalogData, BrowsePipedreamMcpCatalogErrors, BrowsePipedreamMcpCatalogResponses, CallToolData, CallToolErrors, CallToolResponses, CancelNotionHandlerData, CancelNotionHandlerErrors, CancelNotionHandlerResponses, CompleteHandlerData, CompleteHandlerErrors, CompleteHandlerResponses, CompletePipedreamMcpConnectionData, CompletePipedreamMcpConnectionErrors, CompletePipedreamMcpConnectionResponses, CopyChatData, CopyChatErrors, CopyChatResponses, CreateChatData, CreateChatErrors, CreateChatResponses, CreatePipedreamMcpTokenData, CreatePipedreamMcpTokenErrors, CreatePipedreamMcpTokenResponses, DecisionsData, DecisionsErrors, DecisionsResponses, DeleteChatData, DeleteChatErrors, DeleteChatResponses, DeleteMcpServerData, DeleteMcpServerErrors, DeleteMcpServerResponses, DeletePipedreamMcpConnectionData, DeletePipedreamMcpConnectionErrors, DeletePipedreamMcpConnectionResponses, DiscoverHandlerData, DiscoverHandlerErrors, DiscoverHandlerResponses, DismissedPromptsHandlerData, DismissedPromptsHandlerErrors, DismissedPromptsHandlerResponses, DismissPromptHandlerData, DismissPromptHandlerErrors, DismissPromptHandlerResponses, GetBatchPreviewData, GetBatchPreviewErrors, GetBatchPreviewResponses, GetChatData, GetChatErrors, GetChatHistoryBatchMessagesHandlerData, GetChatHistoryBatchMessagesHandlerErrors, GetChatHistoryBatchMessagesHandlerResponses, GetChatHistoryHandlerData, GetChatHistoryHandlerErrors, GetChatHistoryHandlerResponses, GetChatPermissionsData, GetChatPermissionsErrors, GetChatPermissionsResponses, GetChatResponses, GetChatsForAttachmentHandlerData, GetChatsForAttachmentHandlerErrors, GetChatsForAttachmentHandlerResponses, GetCitationHandlerData, GetCitationHandlerErrors, GetCitationHandlerResponses, GetMemoryHandlerData, GetMemoryHandlerErrors, GetMemoryHandlerResponses, GetStateHandler2Data, GetStateHandler2Errors, GetStateHandler2Responses, GetStateHandlerData, GetStateHandlerErrors, GetStateHandlerResponses, GetUsageHandlerData, GetUsageHandlerErrors, GetUsageHandlerResponses, HealthHandlerData, HealthHandlerResponses, ListMcpServersData, ListMcpServersErrors, ListMcpServersResponses, ListPipedreamMcpConnectionsData, ListPipedreamMcpConnectionsErrors, ListPipedreamMcpConnectionsResponses, McpAuthCallbackData, McpAuthCallbackErrors, McpAuthCallbackResponses, McpOauthClientMetadataData, McpOauthClientMetadataResponses, NotionReportHandlerData, NotionReportHandlerErrors, NotionReportHandlerResponses, PatchChatData, PatchChatErrors, PatchChatResponses, PermanentlyDeleteChatData, PermanentlyDeleteChatErrors, PermanentlyDeleteChatResponses, RejectToolCallData, RejectToolCallErrors, RejectToolCallResponses, RevertDeleteChatData, RevertDeleteChatErrors, RevertDeleteChatResponses, RunImportHandlerData, RunImportHandlerErrors, RunImportHandlerResponses, SendChatMessageData, SendChatMessageErrors, SendChatMessageResponses, SetPricingHandlerData, SetPricingHandlerErrors, SetPricingHandlerResponses, StartMcpAuthData, StartMcpAuthErrors, StartMcpAuthResponses, StopChatStreamData, StopChatStreamErrors, StopChatStreamResponses, StructuredCompletionData, StructuredCompletionErrors, StructuredCompletionResponses, UpdateMcpServerData, UpdateMcpServerErrors, UpdateMcpServerResponses, UpdatePipedreamMcpConnectionData, UpdatePipedreamMcpConnectionErrors, UpdatePipedreamMcpConnectionResponses, UpdateToolCallData, UpdateToolCallErrors, UpdateToolCallResponses, UpdateToolResponseData, UpdateToolResponseErrors, UpdateToolResponseResponses, UpsertAiProjectionData, UpsertAiProjectionErrors, UpsertAiProjectionResponses } from './types.gen';export type Options<TData extends TDataShape = TDataShape, ThrowOnError extends boolean = boolean, TResponse = unknown> = Options2<TData, ThrowOnError, TResponse> & {/**@@ -273,6 +273,21 @@ export class Sdk extends HeyApiClient {return (options?.client ?? this.client).get<HealthHandlerResponses, unknown, ThrowOnError>({ url: '/health', ...options });}+ /**+ * Where the user's Notion workspace import stands.+ */+ public notionReportHandler<ThrowOnError extends boolean = false>(options?: Options<NotionReportHandlerData, ThrowOnError>): RequestResult<NotionReportHandlerResponses, NotionReportHandlerErrors, ThrowOnError> {+ return (options?.client ?? this.client).get<NotionReportHandlerResponses, NotionReportHandlerErrors, ThrowOnError>({ url: '/import/notion', ...options });+ }++ /**+ * Stop planning or writing the user's Notion import. Objects already+ * imported stay.+ */+ public cancelNotionHandler<ThrowOnError extends boolean = false>(options?: Options<CancelNotionHandlerData, ThrowOnError>): RequestResult<CancelNotionHandlerResponses, CancelNotionHandlerErrors, ThrowOnError> {+ return (options?.client ?? this.client).post<CancelNotionHandlerResponses, CancelNotionHandlerErrors, ThrowOnError>({ url: '/import/notion/cancel', ...options });+ }+/*** Accept staged imports (starts import jobs) and/or discard staged rows.*/@@ -288,28 +303,14 @@ export class Sdk extends HeyApiClient {}/**- * Discover candidates for user selection without auto-importing them.+ * Discover candidates for user selection. Only Slack supports discovery.*/public discoverHandler<ThrowOnError extends boolean = false>(options: Options<DiscoverHandlerData, ThrowOnError>): RequestResult<DiscoverHandlerResponses, DiscoverHandlerErrors, ThrowOnError> {return (options.client ?? this.client).post<DiscoverHandlerResponses, DiscoverHandlerErrors, ThrowOnError>({ url: '/import/runs/{source}/discover', ...options });}/**- * Dismiss one source's import section.- */- public dismissRunHandler<ThrowOnError extends boolean = false>(options: Options<DismissRunHandlerData, ThrowOnError>): RequestResult<DismissRunHandlerResponses, DismissRunHandlerErrors, ThrowOnError> {- return (options.client ?? this.client).post<DismissRunHandlerResponses, DismissRunHandlerErrors, ThrowOnError>({ url: '/import/runs/{source}/dismiss', ...options });- }-- /**- * Restart a failed (or dismissed) gather run for one source.- */- public retryGatherHandler<ThrowOnError extends boolean = false>(options: Options<RetryGatherHandlerData, ThrowOnError>): RequestResult<RetryGatherHandlerResponses, RetryGatherHandlerErrors, ThrowOnError> {- return (options.client ?? this.client).post<RetryGatherHandlerResponses, RetryGatherHandlerErrors, ThrowOnError>({ url: '/import/runs/{source}/retry', ...options });- }-- /**- * Get the authenticated user's import state: gather runs plus visible+ * Get the authenticated user's import state: discovery runs plus visible* ledger rows.*/public getStateHandler<ThrowOnError extends boolean = false>(options?: Options<GetStateHandlerData, ThrowOnError>): RequestResult<GetStateHandlerResponses, GetStateHandlerErrors, ThrowOnError> {@@ -399,9 +400,7 @@ export class Sdk extends HeyApiClient {}/**- * Get the authenticated user's onboarding state. While the flow is active- * this also starts any gather runs that are due, so polling this endpoint- * is what keeps onboarding moving.+ * Get the authenticated user's onboarding state.*/public getStateHandler2<ThrowOnError extends boolean = false>(options?: Options<GetStateHandler2Data, ThrowOnError>): RequestResult<GetStateHandler2Responses, GetStateHandler2Errors, ThrowOnError> {return (options?.client ?? this.client).get<GetStateHandler2Responses, GetStateHandler2Errors, ThrowOnError>({ url: '/onboarding', ...options });diff --git a/packages/sdk/generated/cognition/types.gen.ts b/packages/sdk/generated/cognition/types.gen.tsindex 645b0f8c4a..3948097a22 100644--- a/packages/sdk/generated/cognition/types.gen.ts+++ b/packages/sdk/generated/cognition/types.gen.ts@@ -23,21 +23,6 @@ export type AddServerRequest = {url: string;};-/**- * Public admission error payload. Handlers with additional fields can reuse the- * domain error's code and message and [`admission_status`].- */-export type AiAdmissionErrorBody = {- /**- * Stable denial or unavailability code.- */- code: string;- /**- * Human-readable explanation, without internal billing diagnostics.- */- error: string;-};-/*** Everything we use AI for. The wire / DB form of each variant is its* `snake_case` name.@@ -772,6 +757,10 @@ export type ImportEntity = {* Where it was first staged from.*/initiator: Initiator;+ /**+ * What the item is in its source system.+ */+ kind: ImportKind;/*** Failure detail from the last import attempt, when one failed.*/@@ -781,6 +770,16 @@ export type ImportEntity = {* [`LinearIssueMeta`], [`NotionDocMeta`], [`SlackChannelMeta`]).*/metadata: unknown;+ /**+ * The import row this item was imported under (a subpage's parent page,+ * a row's database), when it was imported as part of a tree.+ */+ parent_id?: string | null;+ /**+ * The agent session review that approved importing this item, when it+ * came from one.+ */+ review_id?: string | null;/*** The external system the item comes from.*/@@ -803,23 +802,37 @@ export type ImportEntity = {* teammate".*/user_id: string;+ /**+ * The teammate's row this one waits on: that teammate is writing the+ * same object for the team, and this row links to their copy once it+ * is imported.+ */+ waiting_on?: string | null;};/**- * Gather-run state for one user × source.+ * What an imported item is in its source system. Each source has its own+ * kinds; a Notion database and a Notion page, or a Linear project and a+ * Linear issue, are tracked and deduplicated separately.+ */+export type ImportKind = 'issue' | 'project' | 'document' | 'page' | 'database' | 'data_source' | 'row' | 'channel';++/**+ * Discovery-run state for one user × source.*/export type ImportRun = {/**- * Whether this run should import its onboarding-staged candidates as- * soon as gathering finishes.+ * Discovery failure detail, when the run failed.*/- auto_import: boolean;+ error?: string | null;/**- * Gather or automatic-import failure detail, when the run failed.+ * The review presenting the run's plan, once one does. A Notion plan is+ * presented while it is still `running`; rows staged after that take it.*/- error?: string | null;+ review_id?: string | null;+ scanned?: null | ScanCounts;/**- * The source this run gathered from.+ * The source this run discovered from.*/source: ImportSource;/**@@ -838,7 +851,7 @@ export type ImportRun = {export type ImportSource = 'linear' | 'notion' | 'slack';/**- * The full import aggregate for a user: gather runs plus visible ledger+ * The full import aggregate for a user: discovery runs plus visible ledger* rows (their own, and teammates' team-imported rows).*/export type ImportState = {@@ -847,7 +860,7 @@ export type ImportState = {*/entities: Array<ImportEntity>;/**- * Gather runs, one per source that ever gathered.+ * Discovery runs, one per source that ever ran discovery.*/runs: Array<ImportRun>;};@@ -980,11 +993,35 @@ export type NewMessageAttachment = {messageId: string;};+/**+ * A teammate's copy of a Notion object, shared with the user's team.+ */+export type NotionAlreadyImported = {+ /**+ * The teammate who imported it, by email.+ */+ by: string;+ /**+ * The Macro entity it became.+ */+ entityId: string;+ /**+ * That entity's type (`md`, `project`).+ */+ entityType: string;+};++/**+ * What a workspace import does with one Notion object.+ */+export type NotionDecision = 'copy' | 'tasks' | 'skip' | 'hand';+/*** Metadata for one staged Notion page. Deliberately has NO content field:* page bodies are fetched at import time, for accepted pages only.*/export type NotionDocMeta = {+ plan?: null | NotionPlanItem;/*** One-line summary of what the page contains.*/@@ -999,6 +1036,210 @@ export type NotionDocMeta = {url?: string | null;};+/**+ * Where a Notion workspace import stands.+ */+export type NotionImportReport = {+ /**+ * Who imported the objects already in Macro, most first.+ */+ alreadyImportedBy?: Array<NotionImporter>;+ /**+ * Plan objects a teammate already imported and shared with the team;+ * they link to that copy and are not counted as copies.+ */+ alreadyInMacro?: number;+ /**+ * Objects to copy as they are.+ */+ copy: number;+ /**+ * Data sources (database tables) in the plan.+ */+ dataSources: number;+ /**+ * Rows those databases hold, every one counted while planning.+ */+ databaseRows: number;+ /**+ * Data sources written as databases.+ */+ databases: number;+ /**+ * Plan objects the decision model has not answered for yet, while+ * planning is still `running`.+ */+ deciding?: number;+ /**+ * Pages written as documents.+ */+ documents: number;+ /**+ * Why the run failed, when it did.+ */+ error?: string | null;+ /**+ * Objects that failed to import, with the failure.+ */+ failures?: Array<NotionReportItem>;+ /**+ * Objects that need handling by hand.+ */+ hand: number;+ /**+ * Objects that need handling by hand, with reasons.+ */+ handItems?: Array<NotionReportItem>;+ /**+ * Plan objects already imported.+ */+ imported: number;+ /**+ * Whether the user is on a team. Without one, everything the import+ * writes stays visible only to them.+ */+ onTeam?: boolean;+ /**+ * Pages in the plan.+ */+ pages: number;+ /**+ * The review the plan is tied to, once presented.+ */+ reviewId?: string | null;+ scanned?: null | ScanCounts;+ /**+ * Written plan objects shared with the user's team.+ */+ sharedWithTeam?: number;+ /**+ * Objects left in Notion.+ */+ skip: number;+ status?: null | RunStatus;+ /**+ * Tasks the task lists become, one per row.+ */+ taskRows: number;+ /**+ * Data sources whose rows become tasks.+ */+ tasks: number;+ /**+ * Plan objects still waiting to be written.+ */+ waiting: number;+ /**+ * Plan objects waiting for a teammate who is writing the same object+ * for the team; they link to the teammate's copy once it is written.+ */+ waitingOnTeammates?: number;+ /**+ * Plan objects being written now.+ */+ writing: number;+};++/**+ * A teammate who already imported some of a plan's objects.+ */+export type NotionImporter = {+ /**+ * The teammate, by email.+ */+ by: string;+ /**+ * How many of the plan's objects they imported.+ */+ count: number;+};++/**+ * One object's place in a workspace import plan.+ */+export type NotionPlanItem = {+ alreadyImported?: null | NotionAlreadyImported;+ decision?: null | NotionDecision;+ /**+ * Titles of the pages above it, outermost first.+ */+ path?: Array<string>;+ /**+ * Why, in a short sentence, when the decision model gave one.+ */+ reason?: string | null;+ /**+ * For a data source: rows counted while planning.+ */+ rows?: number | null;+ /**+ * For a data source: the titles of a few of its rows.+ */+ sampleTitles?: Array<string>;+ /**+ * Whether the user may change `shared`: false for users without a team.+ */+ shareable?: boolean;+ /**+ * Whether what it becomes is shared with the user's team.+ */+ shared?: boolean;+ /**+ * Why it is (or is not) shared, when the decision model chose or the+ * import could not share it.+ */+ sharedReason?: string | null;+ taskFields?: null | NotionTaskFields;+};++/**+ * One object a workspace import report calls out.+ */+export type NotionReportItem = {+ /**+ * The ledger row.+ */+ importId: string;+ /**+ * What the object is.+ */+ kind: ImportKind;+ /**+ * Why it is called out: the decision model's reason, or the failure.+ */+ reason?: string | null;+ /**+ * The object's title.+ */+ title: string;+ /**+ * Its Notion URL.+ */+ url?: string | null;+};++/**+ * Which Notion columns hold a task list's task fields, by property name.+ */+export type NotionTaskFields = {+ /**+ * The assignee column.+ */+ assignee?: string | null;+ /**+ * The due date column.+ */+ due?: string | null;+ /**+ * The priority column.+ */+ priority?: string | null;+ /**+ * The status column.+ */+ status?: string | null;+};+/*** One user's onboarding row.*/@@ -1294,10 +1535,24 @@ export type RunImportRequest = {};/**- * Lifecycle of a gather run (one per user × source).+ * Lifecycle of a discovery run (one per user × source).*/export type RunStatus = 'running' | 'ready' | 'importing' | 'completed' | 'failed' | 'dismissed';+/**+ * What a planning run has found in the source so far.+ */+export type ScanCounts = {+ /**+ * Data sources (database tables) found.+ */+ dataSources: number;+ /**+ * Pages found.+ */+ pages: number;+};+export type SendChatMessagePayload = JwtPayload & {/*** Additional system instructions appended to the base system prompt@@ -2439,94 +2694,73 @@ export type HealthHandlerResponses = {export type HealthHandlerResponse = HealthHandlerResponses[keyof HealthHandlerResponses];-export type RunImportHandlerData = {- body: RunImportRequest;+export type NotionReportHandlerData = {+ body?: never;path?: never;query?: never;- url: '/import/run';+ url: '/import/notion';};-export type RunImportHandlerErrors = {+export type NotionReportHandlerErrors = {/*** Internal server error*/500: unknown;};-export type RunImportHandlerResponses = {- /**- * Import run outcome- */- 200: RunImportOutcome;+export type NotionReportHandlerResponses = {+ 200: NotionImportReport;};-export type RunImportHandlerResponse = RunImportHandlerResponses[keyof RunImportHandlerResponses];+export type NotionReportHandlerResponse = NotionReportHandlerResponses[keyof NotionReportHandlerResponses];-export type DiscoverHandlerData = {+export type CancelNotionHandlerData = {body?: never;- path: {- /**- * Import source- */- source: string;- };+ path?: never;query?: never;- url: '/import/runs/{source}/discover';+ url: '/import/notion/cancel';};-export type DiscoverHandlerErrors = {- /**- * Unknown import source- */- 400: unknown;+export type CancelNotionHandlerErrors = {/*** Internal server error*/500: unknown;};-export type DiscoverHandlerResponses = {+export type CancelNotionHandlerResponses = {/**- * Discovery accepted (idempotent)+ * Where the import stands after cancelling*/- 204: void;+ 200: NotionImportReport;};-export type DiscoverHandlerResponse = DiscoverHandlerResponses[keyof DiscoverHandlerResponses];+export type CancelNotionHandlerResponse = CancelNotionHandlerResponses[keyof CancelNotionHandlerResponses];-export type DismissRunHandlerData = {- body?: never;- path: {- /**- * Import source- */- source: string;- };+export type RunImportHandlerData = {+ body: RunImportRequest;+ path?: never;query?: never;- url: '/import/runs/{source}/dismiss';+ url: '/import/run';};-export type DismissRunHandlerErrors = {- /**- * Unknown import source- */- 400: unknown;+export type RunImportHandlerErrors = {/*** Internal server error*/500: unknown;};-export type DismissRunHandlerResponses = {+export type RunImportHandlerResponses = {/**- * Dismissed+ * Import run outcome*/- 204: void;+ 200: RunImportOutcome;};-export type DismissRunHandlerResponse = DismissRunHandlerResponses[keyof DismissRunHandlerResponses];+export type RunImportHandlerResponse = RunImportHandlerResponses[keyof RunImportHandlerResponses];-export type RetryGatherHandlerData = {+export type DiscoverHandlerData = {body?: never;path: {/**@@ -2535,38 +2769,28 @@ export type RetryGatherHandlerData = {source: string;};query?: never;- url: '/import/runs/{source}/retry';+ url: '/import/runs/{source}/discover';};-export type RetryGatherHandlerErrors = {+export type DiscoverHandlerErrors = {/**- * Unknown import source+ * Unknown import source or discovery not supported*/400: unknown;- /**- * AI allowance exhausted- */- 402: AiAdmissionErrorBody;/*** Internal server error*/500: unknown;- /**- * AI usage validation unavailable; retry later- */- 503: AiAdmissionErrorBody;};-export type RetryGatherHandlerError = RetryGatherHandlerErrors[keyof RetryGatherHandlerErrors];--export type RetryGatherHandlerResponses = {+export type DiscoverHandlerResponses = {/**- * Retry accepted (idempotent)+ * Discovery accepted (idempotent)*/204: void;};-export type RetryGatherHandlerResponse = RetryGatherHandlerResponses[keyof RetryGatherHandlerResponses];+export type DiscoverHandlerResponse = DiscoverHandlerResponses[keyof DiscoverHandlerResponses];export type GetStateHandlerData = {body?: never;diff --git a/packages/sdk/specs/agent-harness.json b/packages/sdk/specs/agent-harness.jsonindex 9242325de7..8c34272290 100644--- a/packages/sdk/specs/agent-harness.json+++ b/packages/sdk/specs/agent-harness.json@@ -956,6 +956,91 @@}}},+ "/agent-sessions/{session_id}/app-connections": {+ "post": {+ "tags": ["agent-sessions"],+ "summary": "Continue the session now that its owner connected an app it asked for.",+ "description": "The harness produces an `app.connected` system event, which the transcript\nshows as no prompt; it starts a turn once the session is idle.",+ "operationId": "report_agent_session_app_connected",+ "parameters": [+ {+ "name": "session_id",+ "in": "path",+ "description": "ID of the agent session",+ "required": true,+ "schema": {+ "type": "string",+ "format": "uuid"+ }+ }+ ],+ "requestBody": {+ "content": {+ "application/json": {+ "schema": {+ "$ref": "#/components/schemas/AppConnectedRequest"+ }+ }+ },+ "required": true+ },+ "responses": {+ "202": {+ "description": "The session continues once it is idle"+ },+ "401": {+ "description": "",+ "content": {+ "text/plain": {+ "schema": {+ "type": "string"+ }+ }+ }+ },+ "403": {+ "description": "Not the session's owner, or no edit access",+ "content": {+ "text/plain": {+ "schema": {+ "type": "string"+ }+ }+ }+ },+ "409": {+ "description": "The session is archived",+ "content": {+ "text/plain": {+ "schema": {+ "type": "string"+ }+ }+ }+ },+ "422": {+ "description": "The owner has not connected that app",+ "content": {+ "text/plain": {+ "schema": {+ "type": "string"+ }+ }+ }+ },+ "500": {+ "description": "",+ "content": {+ "text/plain": {+ "schema": {+ "type": "string"+ }+ }+ }+ }+ }+ }+ },"/agent-sessions/{session_id}/archived": {"put": {"tags": ["agent-sessions"],@@ -3531,6 +3616,28 @@}}},+ "AppConnectedRequest": {+ "type": "object",+ "description": "Request body for reporting a connected app.",+ "required": ["appSlug"],+ "properties": {+ "actionId": {+ "oneOf": [+ {+ "type": "null"+ },+ {+ "$ref": "#/components/schemas/AgentActionId",+ "description": "Names the report, so a retried one wakes the session once."+ }+ ]+ },+ "appSlug": {+ "type": "string",+ "description": "The Pipedream app slug the owner connected, e.g. `notion`."+ }+ }+ },"BotId": {"type": "string"},@@ -3865,7 +3972,7 @@"drafts": {"type": "array","items": {},- "description": "One to one hundred independent drafts. Email: {subject, body: Markdown,\nto: [{email, name?}], cc?: [{email, name?}], bcc?: [{email, name?}],\nreplyingToId?, attachments?: [{documentId}]}. Calendar: {title,\ntime: {kind: \"timed\", startsAt: ISO datetime, endsAt: ISO datetime,\ntimeZone?: IANA zone} or {kind: \"allDay\", startDate: ISO date,\nendDate: exclusive ISO date}, description?, location?, attendees?:\n[{email, isOptional?}]}.\nCreating any review only presents these drafts; the user acts separately."+ "description": "One to one hundred independent drafts. Email: {subject, body: Markdown,\nto: [{email, name?}], cc?: [{email, name?}], bcc?: [{email, name?}],\nreplyingToId?, attachments?: [{documentId}]}. Calendar: {title,\ntime: {kind: \"timed\", startsAt: ISO datetime, endsAt: ISO datetime,\ntimeZone?: IANA zone} or {kind: \"allDay\", startDate: ISO date,\nendDate: exclusive ISO date}, description?, location?, attendees?:\n[{email, isOptional?}]}. Import: exactly one draft {source: \"notion\"},\npresenting the plan ImportFromNotion staged.\nCreating any review only presents these drafts; the user acts separately."},"executionId": {"oneOf": [@@ -4815,7 +4922,7 @@"ReviewKind": {"type": "string","description": "The component and action used to finish a review.",- "enum": ["email", "calendar"]+ "enum": ["email", "calendar", "import"]},"ReviewRecord": {"type": "object",diff --git a/packages/sdk/specs/cognition.json b/packages/sdk/specs/cognition.jsonindex 53e220375c..de7b85ca6b 100644--- a/packages/sdk/specs/cognition.json+++ b/packages/sdk/specs/cognition.json@@ -1159,28 +1159,18 @@}}},- "/import/run": {- "post": {+ "/import/notion": {+ "get": {"tags": ["import"],- "summary": "Accept staged imports (starts import jobs) and/or discard staged rows.",- "operationId": "run_import_handler",- "requestBody": {- "content": {- "application/json": {- "schema": {- "$ref": "#/components/schemas/RunImportRequest"- }- }- },- "required": true- },+ "summary": "Where the user's Notion workspace import stands.",+ "operationId": "notion_report_handler","responses": {"200": {- "description": "Import run outcome",+ "description": "","content": {"application/json": {"schema": {- "$ref": "#/components/schemas/RunImportOutcome"+ "$ref": "#/components/schemas/NotionImportReport"}}}@@ -1191,28 +1181,21 @@}}},- "/import/runs/{source}/discover": {+ "/import/notion/cancel": {"post": {"tags": ["import"],- "summary": "Discover candidates for user selection without auto-importing them.",- "operationId": "discover_handler",- "parameters": [- {- "name": "source",- "in": "path",- "description": "Import source",- "required": true,- "schema": {- "type": "string"- }- }- ],+ "summary": "Stop planning or writing the user's Notion import. Objects already\nimported stay.",+ "operationId": "cancel_notion_handler","responses": {- "204": {- "description": "Discovery accepted (idempotent)"- },- "400": {- "description": "Unknown import source"+ "200": {+ "description": "Where the import stands after cancelling",+ "content": {+ "application/json": {+ "schema": {+ "$ref": "#/components/schemas/NotionImportReport"+ }+ }+ }},"500": {"description": "Internal server error"@@ -1220,28 +1203,31 @@}}},- "/import/runs/{source}/dismiss": {+ "/import/run": {"post": {"tags": ["import"],- "summary": "Dismiss one source's import section.",- "operationId": "dismiss_run_handler",- "parameters": [- {- "name": "source",- "in": "path",- "description": "Import source",- "required": true,- "schema": {- "type": "string"+ "summary": "Accept staged imports (starts import jobs) and/or discard staged rows.",+ "operationId": "run_import_handler",+ "requestBody": {+ "content": {+ "application/json": {+ "schema": {+ "$ref": "#/components/schemas/RunImportRequest"+ }}- }- ],- "responses": {- "204": {- "description": "Dismissed"},- "400": {- "description": "Unknown import source"+ "required": true+ },+ "responses": {+ "200": {+ "description": "Import run outcome",+ "content": {+ "application/json": {+ "schema": {+ "$ref": "#/components/schemas/RunImportOutcome"+ }+ }+ }},"500": {"description": "Internal server error"@@ -1249,11 +1235,11 @@}}},- "/import/runs/{source}/retry": {+ "/import/runs/{source}/discover": {"post": {"tags": ["import"],- "summary": "Restart a failed (or dismissed) gather run for one source.",- "operationId": "retry_gather_handler",+ "summary": "Discover candidates for user selection. Only Slack supports discovery.",+ "operationId": "discover_handler","parameters": [{"name": "source",@@ -1267,33 +1253,13 @@],"responses": {"204": {- "description": "Retry accepted (idempotent)"+ "description": "Discovery accepted (idempotent)"},"400": {- "description": "Unknown import source"- },- "402": {- "description": "AI allowance exhausted",- "content": {- "application/json": {- "schema": {- "$ref": "#/components/schemas/AiAdmissionErrorBody"- }- }- }+ "description": "Unknown import source or discovery not supported"},"500": {"description": "Internal server error"- },- "503": {- "description": "AI usage validation unavailable; retry later",- "content": {- "application/json": {- "schema": {- "$ref": "#/components/schemas/AiAdmissionErrorBody"- }- }- }}}}@@ -1301,7 +1267,7 @@"/import/state": {"get": {"tags": ["import"],- "summary": "Get the authenticated user's import state: gather runs plus visible\nledger rows.",+ "summary": "Get the authenticated user's import state: discovery runs plus visible\nledger rows.","operationId": "get_state_handler","responses": {"200": {@@ -1674,7 +1640,7 @@"/onboarding": {"get": {"tags": ["onboarding"],- "summary": "Get the authenticated user's onboarding state. While the flow is active\nthis also starts any gather runs that are due, so polling this endpoint\nis what keeps onboarding moving.",+ "summary": "Get the authenticated user's onboarding state.","operationId": "get_state_handler","responses": {"200": {@@ -2392,21 +2358,6 @@}}},- "AiAdmissionErrorBody": {- "type": "object",- "description": "Public admission error payload. Handlers with additional fields can reuse the\ndomain error's code and message and [`admission_status`].",- "required": ["error", "code"],- "properties": {- "code": {- "type": "string",- "description": "Stable denial or unavailability code."- },- "error": {- "type": "string",- "description": "Human-readable explanation, without internal billing diagnostics."- }- }- },"AiFeature": {"type": "string","description": "Everything we use AI for. The wire / DB form of each variant is its\n`snake_case` name.",@@ -4006,6 +3957,7 @@"id","user_id","source",+ "kind","foreign_id","status","initiator",@@ -4040,6 +3992,10 @@"$ref": "#/components/schemas/Initiator","description": "Where it was first staged from."},+ "kind": {+ "$ref": "#/components/schemas/ImportKind",+ "description": "What the item is in its source system."+ },"last_error": {"type": ["string", "null"],"description": "Failure detail from the last import attempt, when one failed."@@ -4047,6 +4003,16 @@"metadata": {"description": "Per-source metadata (shape depends on `source`; see\n[`LinearIssueMeta`], [`NotionDocMeta`], [`SlackChannelMeta`])."},+ "parent_id": {+ "type": ["string", "null"],+ "format": "uuid",+ "description": "The import row this item was imported under (a subpage's parent page,\na row's database), when it was imported as part of a tree."+ },+ "review_id": {+ "type": ["string", "null"],+ "format": "uuid",+ "description": "The agent session review that approved importing this item, when it\ncame from one."+ },"source": {"$ref": "#/components/schemas/ImportSource","description": "The external system the item comes from."@@ -4068,25 +4034,56 @@"user_id": {"type": "string","description": "The user who staged/imported it. Team-imported rows from teammates\nare visible with their owner's id, so clients can say \"created by a\nteammate\"."+ },+ "waiting_on": {+ "type": ["string", "null"],+ "format": "uuid",+ "description": "The teammate's row this one waits on: that teammate is writing the\nsame object for the team, and this row links to their copy once it\nis imported."}}},+ "ImportKind": {+ "type": "string",+ "description": "What an imported item is in its source system. Each source has its own\nkinds; a Notion database and a Notion page, or a Linear project and a\nLinear issue, are tracked and deduplicated separately.",+ "enum": [+ "issue",+ "project",+ "document",+ "page",+ "database",+ "data_source",+ "row",+ "channel"+ ]+ },"ImportRun": {"type": "object",- "description": "Gather-run state for one user × source.",- "required": ["source", "status", "auto_import", "updated_at"],+ "description": "Discovery-run state for one user × source.",+ "required": ["source", "status", "updated_at"],"properties": {- "auto_import": {- "type": "boolean",- "description": "Whether this run should import its onboarding-staged candidates as\nsoon as gathering finishes."- },"error": {"type": ["string", "null"],- "description": "Gather or automatic-import failure detail, when the run failed."+ "description": "Discovery failure detail, when the run failed."+ },+ "review_id": {+ "type": ["string", "null"],+ "format": "uuid",+ "description": "The review presenting the run's plan, once one does. A Notion plan is\npresented while it is still `running`; rows staged after that take it."+ },+ "scanned": {+ "oneOf": [+ {+ "type": "null"+ },+ {+ "$ref": "#/components/schemas/ScanCounts",+ "description": "What the run has found in the source so far, while it is `running`."+ }+ ]},"source": {"$ref": "#/components/schemas/ImportSource",- "description": "The source this run gathered from."+ "description": "The source this run discovered from."},"status": {"$ref": "#/components/schemas/RunStatus",@@ -4106,7 +4103,7 @@},"ImportState": {"type": "object",- "description": "The full import aggregate for a user: gather runs plus visible ledger\nrows (their own, and teammates' team-imported rows).",+ "description": "The full import aggregate for a user: discovery runs plus visible ledger\nrows (their own, and teammates' team-imported rows).","required": ["runs", "entities"],"properties": {"entities": {@@ -4121,7 +4118,7 @@"items": {"$ref": "#/components/schemas/ImportRun"},- "description": "Gather runs, one per source that ever gathered."+ "description": "Discovery runs, one per source that ever ran discovery."}}},@@ -4304,11 +4301,46 @@}}},+ "NotionAlreadyImported": {+ "type": "object",+ "description": "A teammate's copy of a Notion object, shared with the user's team.",+ "required": ["by", "entityId", "entityType"],+ "properties": {+ "by": {+ "type": "string",+ "description": "The teammate who imported it, by email."+ },+ "entityId": {+ "type": "string",+ "description": "The Macro entity it became."+ },+ "entityType": {+ "type": "string",+ "description": "That entity's type (`md`, `project`)."+ }+ }+ },+ "NotionDecision": {+ "type": "string",+ "description": "What a workspace import does with one Notion object.",+ "enum": ["copy", "tasks", "skip", "hand"]+ },"NotionDocMeta": {"type": "object","description": "Metadata for one staged Notion page. Deliberately has NO content field:\npage bodies are fetched at import time, for accepted pages only.","required": ["title"],"properties": {+ "plan": {+ "oneOf": [+ {+ "type": "null"+ },+ {+ "$ref": "#/components/schemas/NotionPlanItem",+ "description": "Where this object sits in a workspace import plan; absent for pages\nstaged one at a time. Written by the workspace import, never by a\nmodel, so it is left out of the tool schema."+ }+ ]+ },"summary": {"type": ["string", "null"],"description": "One-line summary of what the page contains."@@ -4323,6 +4355,327 @@}}},+ "NotionImportReport": {+ "type": "object",+ "description": "Where a Notion workspace import stands.",+ "required": [+ "pages",+ "dataSources",+ "copy",+ "tasks",+ "skip",+ "hand",+ "documents",+ "databases",+ "databaseRows",+ "taskRows",+ "imported",+ "waiting",+ "writing"+ ],+ "properties": {+ "alreadyImportedBy": {+ "type": "array",+ "items": {+ "$ref": "#/components/schemas/NotionImporter"+ },+ "description": "Who imported the objects already in Macro, most first."+ },+ "alreadyInMacro": {+ "type": "integer",+ "format": "int32",+ "description": "Plan objects a teammate already imported and shared with the team;\nthey link to that copy and are not counted as copies.",+ "minimum": 0+ },+ "copy": {+ "type": "integer",+ "format": "int32",+ "description": "Objects to copy as they are.",+ "minimum": 0+ },+ "dataSources": {+ "type": "integer",+ "format": "int32",+ "description": "Data sources (database tables) in the plan.",+ "minimum": 0+ },+ "databaseRows": {+ "type": "integer",+ "format": "int32",+ "description": "Rows those databases hold, every one counted while planning.",+ "minimum": 0+ },+ "databases": {+ "type": "integer",+ "format": "int32",+ "description": "Data sources written as databases.",+ "minimum": 0+ },+ "deciding": {+ "type": "integer",+ "format": "int32",+ "description": "Plan objects the decision model has not answered for yet, while\nplanning is still `running`.",+ "minimum": 0+ },+ "documents": {+ "type": "integer",+ "format": "int32",+ "description": "Pages written as documents.",+ "minimum": 0+ },+ "error": {+ "type": ["string", "null"],+ "description": "Why the run failed, when it did."+ },+ "failures": {+ "type": "array",+ "items": {+ "$ref": "#/components/schemas/NotionReportItem"+ },+ "description": "Objects that failed to import, with the failure."+ },+ "hand": {+ "type": "integer",+ "format": "int32",+ "description": "Objects that need handling by hand.",+ "minimum": 0+ },+ "handItems": {+ "type": "array",+ "items": {+ "$ref": "#/components/schemas/NotionReportItem"+ },+ "description": "Objects that need handling by hand, with reasons."+ },+ "imported": {+ "type": "integer",+ "format": "int32",+ "description": "Plan objects already imported.",+ "minimum": 0+ },+ "onTeam": {+ "type": "boolean",+ "description": "Whether the user is on a team. Without one, everything the import\nwrites stays visible only to them."+ },+ "pages": {+ "type": "integer",+ "format": "int32",+ "description": "Pages in the plan.",+ "minimum": 0+ },+ "reviewId": {+ "type": ["string", "null"],+ "format": "uuid",+ "description": "The review the plan is tied to, once presented."+ },+ "scanned": {+ "oneOf": [+ {+ "type": "null"+ },+ {+ "$ref": "#/components/schemas/ScanCounts",+ "description": "What planning has found in Notion so far, while it is `running`."+ }+ ]+ },+ "sharedWithTeam": {+ "type": "integer",+ "format": "int32",+ "description": "Written plan objects shared with the user's team.",+ "minimum": 0+ },+ "skip": {+ "type": "integer",+ "format": "int32",+ "description": "Objects left in Notion.",+ "minimum": 0+ },+ "status": {+ "oneOf": [+ {+ "type": "null"+ },+ {+ "$ref": "#/components/schemas/RunStatus",+ "description": "The run's state: `running` while planning, `ready` once a plan is\nstaged, `importing` while writing, `completed`, `failed`, or\n`dismissed` when cancelled. Absent before the first run."+ }+ ]+ },+ "taskRows": {+ "type": "integer",+ "format": "int32",+ "description": "Tasks the task lists become, one per row.",+ "minimum": 0+ },+ "tasks": {+ "type": "integer",+ "format": "int32",+ "description": "Data sources whose rows become tasks.",+ "minimum": 0+ },+ "waiting": {+ "type": "integer",+ "format": "int32",+ "description": "Plan objects still waiting to be written.",+ "minimum": 0+ },+ "waitingOnTeammates": {+ "type": "integer",+ "format": "int32",+ "description": "Plan objects waiting for a teammate who is writing the same object\nfor the team; they link to the teammate's copy once it is written.",+ "minimum": 0+ },+ "writing": {+ "type": "integer",+ "format": "int32",+ "description": "Plan objects being written now.",+ "minimum": 0+ }+ }+ },+ "NotionImporter": {+ "type": "object",+ "description": "A teammate who already imported some of a plan's objects.",+ "required": ["by", "count"],+ "properties": {+ "by": {+ "type": "string",+ "description": "The teammate, by email."+ },+ "count": {+ "type": "integer",+ "format": "int32",+ "description": "How many of the plan's objects they imported.",+ "minimum": 0+ }+ }+ },+ "NotionPlanItem": {+ "type": "object",+ "description": "One object's place in a workspace import plan.",+ "properties": {+ "alreadyImported": {+ "oneOf": [+ {+ "type": "null"+ },+ {+ "$ref": "#/components/schemas/NotionAlreadyImported",+ "description": "A teammate's copy shared with the team, which this object links to\ninstead of being imported again."+ }+ ]+ },+ "decision": {+ "oneOf": [+ {+ "type": "null"+ },+ {+ "$ref": "#/components/schemas/NotionDecision",+ "description": "What the import does with it; absent while planning has staged the\nobject but the decision model has not answered for it yet."+ }+ ]+ },+ "path": {+ "type": "array",+ "items": {+ "type": "string"+ },+ "description": "Titles of the pages above it, outermost first."+ },+ "reason": {+ "type": ["string", "null"],+ "description": "Why, in a short sentence, when the decision model gave one."+ },+ "rows": {+ "type": ["integer", "null"],+ "format": "int32",+ "description": "For a data source: rows counted while planning.",+ "minimum": 0+ },+ "sampleTitles": {+ "type": "array",+ "items": {+ "type": "string"+ },+ "description": "For a data source: the titles of a few of its rows."+ },+ "shareable": {+ "type": "boolean",+ "description": "Whether the user may change `shared`: false for users without a team."+ },+ "shared": {+ "type": "boolean",+ "description": "Whether what it becomes is shared with the user's team."+ },+ "sharedReason": {+ "type": ["string", "null"],+ "description": "Why it is (or is not) shared, when the decision model chose or the\nimport could not share it."+ },+ "taskFields": {+ "oneOf": [+ {+ "type": "null"+ },+ {+ "$ref": "#/components/schemas/NotionTaskFields",+ "description": "For a task list: which columns hold the task fields."+ }+ ]+ }+ }+ },+ "NotionReportItem": {+ "type": "object",+ "description": "One object a workspace import report calls out.",+ "required": ["importId", "title", "kind"],+ "properties": {+ "importId": {+ "type": "string",+ "format": "uuid",+ "description": "The ledger row."+ },+ "kind": {+ "$ref": "#/components/schemas/ImportKind",+ "description": "What the object is."+ },+ "reason": {+ "type": ["string", "null"],+ "description": "Why it is called out: the decision model's reason, or the failure."+ },+ "title": {+ "type": "string",+ "description": "The object's title."+ },+ "url": {+ "type": ["string", "null"],+ "description": "Its Notion URL."+ }+ }+ },+ "NotionTaskFields": {+ "type": "object",+ "description": "Which Notion columns hold a task list's task fields, by property name.",+ "properties": {+ "assignee": {+ "type": ["string", "null"],+ "description": "The assignee column."+ },+ "due": {+ "type": ["string", "null"],+ "description": "The due date column."+ },+ "priority": {+ "type": ["string", "null"],+ "description": "The priority column."+ },+ "status": {+ "type": ["string", "null"],+ "description": "The status column."+ }+ }+ },"OnboardingRow": {"type": "object","description": "One user's onboarding row.",@@ -4663,7 +5016,7 @@},"RunStatus": {"type": "string",- "description": "Lifecycle of a gather run (one per user × source).",+ "description": "Lifecycle of a discovery run (one per user × source).","enum": ["running","ready",@@ -4673,6 +5026,25 @@"dismissed"]},+ "ScanCounts": {+ "type": "object",+ "description": "What a planning run has found in the source so far.",+ "required": ["pages", "dataSources"],+ "properties": {+ "dataSources": {+ "type": "integer",+ "format": "int32",+ "description": "Data sources (database tables) found.",+ "minimum": 0+ },+ "pages": {+ "type": "integer",+ "format": "int32",+ "description": "Pages found.",+ "minimum": 0+ }+ }+ },"SendChatMessagePayload": {"allOf": [{diff --git a/packages/sdk/src/coverage/skipped.ts b/packages/sdk/src/coverage/skipped.tsindex a4d6c387dc..a027d04d06 100644--- a/packages/sdk/src/coverage/skipped.ts+++ b/packages/sdk/src/coverage/skipped.ts@@ -32,6 +32,8 @@ export const agentHarnessExcluded = ['agentSessionsForPullRequests',// Speculative page warm-up requires a signed-in user and is app-internal.'warmAgentSessionHandler',+ // The connect chip's continuation after an app connects is app-internal.+ 'reportAgentSessionAppConnected',] as const satisfies readonly (keyof AgentHarnessSdk)[];export const agentHarnessBacklog =@@ -171,8 +173,9 @@ export const cognitionExcluded = [// Channel discovery shares the app-only import workflow below.'discoverHandler','runImportHandler',- 'dismissRunHandler',- 'retryGatherHandler',+ // The Notion import report and cancel back the in-chat import review card.+ 'notionReportHandler',+ 'cancelNotionHandler','getStateHandler','getStateHandler2','completeHandler',diff --git a/services/agent_harness_service/AGENTS.md b/services/agent_harness_service/AGENTS.mdindex 5af5a856e0..0ca7e126cc 100644--- a/services/agent_harness_service/AGENTS.md+++ b/services/agent_harness_service/AGENTS.md@@ -57,6 +57,11 @@ Pulumi stacks; they do not host extra agent processes.## Tool contracts- Internal session tools require explicit `acting_as`, resolved by the shared gate, plus existing owner/edit authorization. SDK executions choose one account for the whole program; review decisions use the saved acting account. Keep their restricted prompt/stop route separate from human approval controls.+- An import review's draft is `{source, excluded, shared, personal}`: the rows the owner left out and the rows whose sharing they flipped, passed to `start_notion_write` as `NotionWriteChoices`.+- One `ai_tools::ImportJobs` serves the whole process: session tools, public MCP, and `cognition::build` (the `/import/*` routes and the only stalled-Notion-import sweeper) share it, and `main.rs` hands running imports over on shutdown (`ImportJobs::hand_over`).+- The agent approves a presented review after its account holder says yes in chat by submitting it with `DecideReview`: `Actions::session` reports the turn's prompter from `agent_session.turn_prompter`, and `approvable` refuses an import whose plan is not `ready` for that review.+- Presenting an import review ties its plan to the review and the session (`attach_notion_review`); the import then produces `import.planned` and `review.settled` for that session itself, in the run's transaction (`crates/import`).+- The owner connecting an app a session asked for produces an `app.connected` event (`agent_harness::domain::app_connections`).The composition root wires `PgExternalReplyStore` into the harness and recoversunfinished external chat replies alongside conversation recovery every ten seconds.diff --git a/services/agent_harness_service/Cargo.toml b/services/agent_harness_service/Cargo.tomlindex e3a7a4e892..67d550abab 100644--- a/services/agent_harness_service/Cargo.toml+++ b/services/agent_harness_service/Cargo.toml@@ -106,6 +106,7 @@ harnesses = { path = "../../crates/harnesses", default-features = false, featurehttp = { workspace = true }http-body-util = { workspace = true }import = { path = "../../crates/import" }+import_notion = { path = "../../crates/import_notion" }initiative = { path = "../../crates/initiative", default-features = false, features = ["postgres"] }jev = { path = "../../crates/jev", features = ["decisions", "outbound"] }kafka_util = { path = "../../crates/kafka_util" }diff --git a/services/agent_harness_service/src/api.rs b/services/agent_harness_service/src/api.rsindex df82ec05c9..0bdeab6257 100644--- a/services/agent_harness_service/src/api.rs+++ b/services/agent_harness_service/src/api.rs@@ -81,6 +81,7 @@ pub struct ApiStates<T, R, Opener, Bots, Requests, Access, Auth, Models, Changesconversations: Router,pull_requests: Router,tool_approvals: Router,+ app_connections: Router,code_executions: Router,notifications: Router,changes: AgentChangesRouterState<Changes, Access, Auth>,@@ -118,6 +119,7 @@ impl<T, R, Opener, Bots, Requests, Access, Auth, Models, Changes>conversations: Router::new(),pull_requests: Router::new(),tool_approvals: Router::new(),+ app_connections: Router::new(),code_executions: Router::new(),notifications: Router::new(),changes,@@ -207,6 +209,12 @@ impl<T, R, Opener, Bots, Requests, Access, Auth, Models, Changes>self.tool_approvals = router;self}++ /// Attach the route continuing a session once its owner connects an app.+ pub fn with_app_connections(mut self, router: Router) -> Self {+ self.app_connections = router;+ self+ }}/// Serve session egress and Macro Internal MCP on the existing egress listener.@@ -325,6 +333,7 @@ where.merge(states.sharing).merge(states.pull_requests).merge(states.tool_approvals)+ .merge(states.app_connections).merge(states.code_executions).merge(states.notifications).merge(agent_changes_router(states.changes));diff --git a/services/agent_harness_service/src/api/swagger.rs b/services/agent_harness_service/src/api/swagger.rsindex 4ab79f1e8a..05e40d5bbd 100644--- a/services/agent_harness_service/src/api/swagger.rs+++ b/services/agent_harness_service/src/api/swagger.rs@@ -5,6 +5,7 @@ use agent_changes::inbound::axum_router::{CaptureAttemptDto, CaptureOutcomeDto, ChangedFileDto, ChangesetDto, ChangesetSourceDto,FileChangeKindDto, GitRefDto,};+use agent_harness::inbound::app_connections::{self, AppConnectedRequest};use agent_harness::inbound::capability_discovery;use agent_harness::inbound::model_load::{self, AgentModelDto, AgentModelsStatusDto, LoadAgentModelsRequest, LoadAgentModelsResponse,@@ -100,6 +101,7 @@ impl Modify for SecurityAddon {changes_router::get_agent_session_changes_patch_handler,changes_router::refresh_agent_session_changes_handler,tool_approvals::answer_tool_approval_handler,+ app_connections::app_connected_handler,),components(schemas(CreateAgentSessionRequest,@@ -159,6 +161,7 @@ impl Modify for SecurityAddon {ChangesetSourceDto,FileChangeKindDto,AnswerToolApprovalRequest,+ AppConnectedRequest,AnswerToolApprovalResponse,ToolApprovalAnswerDto,ToolApprovalStatusDto,diff --git a/services/agent_harness_service/src/code_mode/reviews.rs b/services/agent_harness_service/src/code_mode/reviews.rsindex 6ca817eb51..9d7feb6192 100644--- a/services/agent_harness_service/src/code_mode/reviews.rs+++ b/services/agent_harness_service/src/code_mode/reviews.rs@@ -6,13 +6,38 @@ use agent_session::domain::{};use ai_toolset::RequestContext;use async_trait::async_trait;+use import::domain::models::{NotionImportReport, NotionWriteChoices, RunStatus};+use import::domain::service::NotionWorkspaceImporter;use macro_user_id::user_id::MacroUserIdStr;+use serde::Deserialize;use serde_json::Value;use std::sync::Arc;#[cfg(test)]mod test;+/// The one draft an import review carries: which source's staged plan it+/// presents, the plan rows the user took out of it, and the rows whose+/// sharing they changed: `shared` to share with their team, `personal` to+/// keep to themselves.+#[derive(Debug, Deserialize)]+#[serde(rename_all = "camelCase", deny_unknown_fields)]+struct ImportDraft {+ source: ImportReviewSource,+ #[serde(default)]+ excluded: Vec<macro_uuid::Uuid>,+ #[serde(default)]+ shared: Vec<macro_uuid::Uuid>,+ #[serde(default)]+ personal: Vec<macro_uuid::Uuid>,+}++#[derive(Debug, Deserialize)]+#[serde(rename_all = "snake_case")]+enum ImportReviewSource {+ Notion,+}+pub struct Actions<S, H> {pub sessions: S,pub harness: Arc<H>,@@ -43,15 +68,27 @@ impl<S: AgentSessionRepo, H: AgentSessionNotificationRecipient> ReviewActions fofn validate(&self, kind: ReviewKind, draft: &Value) -> Result<(), CodeModeError> {validate_draft(&self.tools, kind, draft)}+ async fn approvable(+ &self,+ account_holder: &MacroUserIdStr<'static>,+ record: &ReviewRecord,+ ) -> Result<bool, CodeModeError> {+ if record.kind != ReviewKind::Import {+ return Ok(true);+ }+ let report = self.import_report(account_holder).await?;+ Ok(report.review_id == Some(record.id.0) && report.status == Some(RunStatus::Ready))+ }async fn execute(&self,owner: &MacroUserIdStr<'static>,- kind: ReviewKind,+ record: &ReviewRecord,draft: Value,) -> Result<Value, CodeModeError> {- let name = match kind {+ let name = match record.kind {ReviewKind::Email => "SendEmail",ReviewKind::Calendar => "CreateCalendarEvent",+ ReviewKind::Import => return self.start_import(owner, record, draft).await,};let mut context = self.context.clone();context.usage_context =@@ -78,6 +115,20 @@ impl<S: AgentSessionRepo, H: AgentSessionNotificationRecipient> ReviewActions foowner: &MacroUserIdStr<'static>,record: &ReviewRecord,) -> Result<(), CodeModeError> {+ if record.kind == ReviewKind::Import {+ // The plan is tied to the review before the card appears, so the+ // card never shows a plan it cannot approve.+ self.import_service()?+ .attach_notion_review(owner, record.id.0, record.session_id)+ .await+ .map_err(|error| {+ tracing::warn!(error = ?error, review = %record.id.0, "could not tie the import plan to its review");+ CodeModeError::Invalid(format!(+ "Review {} was saved, but its import plan could not be attached. Read this review to retry.",+ record.id.0+ ))+ })?;+ }self.harness.present_tool(record.session_id,owner.clone(),@@ -91,6 +142,59 @@ impl<S: AgentSessionRepo, H: AgentSessionNotificationRecipient> ReviewActions fo}}+impl<S, H> Actions<S, H> {+ fn import_service(&self) -> Result<&ai_tools::ToolImportService, CodeModeError> {+ self.context+ .import_tool_context+ .service+ .as_deref()+ .ok_or_else(|| CodeModeError::Invalid("Imports are not available here.".into()))+ }++ async fn import_report(+ &self,+ owner: &MacroUserIdStr<'static>,+ ) -> Result<NotionImportReport, CodeModeError> {+ self.import_service()?+ .notion_import_report(owner)+ .await+ .map_err(|error| {+ tracing::warn!(error = ?error, "could not read the notion import report");+ CodeModeError::Invalid("The import's report could not be read.".into())+ })+ }++ /// Start writing the approved plan; the write runs in the background,+ /// reports progress through the import state, and tells the session+ /// once it ends.+ async fn start_import(+ &self,+ owner: &MacroUserIdStr<'static>,+ record: &ReviewRecord,+ draft: Value,+ ) -> Result<Value, CodeModeError> {+ let draft: ImportDraft = serde_json::from_value(draft)+ .map_err(|_| CodeModeError::Invalid("Import draft is malformed.".into()))?;+ let ImportReviewSource::Notion = draft.source;+ let report = self+ .import_service()?+ .start_notion_write(+ owner,+ record.id.0,+ NotionWriteChoices {+ excluded: draft.excluded,+ shared: draft.shared,+ personal: draft.personal,+ },+ )+ .await+ .map_err(|error| CodeModeError::Invalid(error.to_string()))?;+ serde_json::to_value(report).map_err(|_| {+ CodeModeError::Invalid("The import started, but its report could not be read.".into())+ })+ }+}+fn validate_draft(tools: &ai_tools::AiToolSet,kind: ReviewKind,@@ -109,6 +213,11 @@ fn validate_draft(return Err(invalid());}}+ ReviewKind::Import => {+ if serde_json::from_value::<ImportDraft>(draft.clone()).is_err() {+ return Err(invalid());+ }+ }}Ok(())}diff --git a/services/agent_harness_service/src/code_mode/reviews/test.rs b/services/agent_harness_service/src/code_mode/reviews/test.rsindex 1a167748b8..6c55f99b62 100644--- a/services/agent_harness_service/src/code_mode/reviews/test.rs+++ b/services/agent_harness_service/src/code_mode/reviews/test.rs@@ -23,3 +23,43 @@ fn review_validation_uses_existing_action_contracts() {.is_err());}++#[test]+fn an_import_review_carries_one_notion_draft_with_its_left_out_and_reshared_rows() {+ let tools = ai_tools::code_mode_tools();+ assert!(validate_draft(&tools, ReviewKind::Import, &json!({"source": "notion"})).is_ok());+ assert!(+ validate_draft(+ &tools,+ ReviewKind::Import,+ &json!({"source": "notion", "excluded": ["01a15623-f76d-7ea0-955d-a9ecb91a520e"]})+ )+ .is_ok()+ );+ assert!(+ validate_draft(+ &tools,+ ReviewKind::Import,+ &json!({+ "source": "notion",+ "excluded": [],+ "shared": ["01a15623-f76d-7ea0-955d-a9ecb91a520e"],+ "personal": ["01a15623-f76d-7ea0-955d-a9ecb91a520f"]+ })+ )+ .is_ok(),+ "the owner's sharing changes travel with the draft"+ );+ for draft in [+ json!({"source": "linear"}),+ json!({"source": "notion", "excluded": ["not a uuid"]}),+ json!({"source": "notion", "personal": ["not a uuid"]}),+ json!({"source": "notion", "write": "everything"}),+ json!({}),+ ] {+ assert!(+ validate_draft(&tools, ReviewKind::Import, &draft).is_err(),+ "{draft}"+ );+ }+}diff --git a/services/agent_harness_service/src/cognition/api/context.rs b/services/agent_harness_service/src/cognition/api/context.rsindex f2321bde8e..550146c17e 100644--- a/services/agent_harness_service/src/cognition/api/context.rs+++ b/services/agent_harness_service/src/cognition/api/context.rs@@ -116,17 +116,15 @@ pub type DcsMcpRouterState = mcp_client::inbound::McpRouterState<DcsAuthorizationService,>;-/// The import pipeline service, shared between the import router, the chat-/// toolset, and the onboarding flow.+/// The import pipeline service, shared between the import router and the+/// chat toolset.pub type DcsImportService = ai_tools::ToolImportService;-/// The onboarding orchestrator wired to the Postgres repo, the MCP server-/// store, and the import service.+/// The onboarding orchestrator wired to the Postgres repo and the MCP server+/// store.pub type DcsOnboardingService = onboarding::domain::service::OnboardingServiceImpl<onboarding::outbound::pg_onboarding_repo::PgOnboardingRepo,mcp_client::outbound::pg_server_repo::PgServerRepo,- DcsImportService,- ai_tools::ToolMcpSelector,>;/// Concrete Pipedream MCP router state for DCS: the Postgres connectiondiff --git a/services/agent_harness_service/src/cognition/api/context/test.rs b/services/agent_harness_service/src/cognition/api/context/test.rsindex d276d4ea02..bbccabac40 100644--- a/services/agent_harness_service/src/cognition/api/context/test.rs+++ b/services/agent_harness_service/src/cognition/api/context/test.rs@@ -610,15 +610,17 @@ pub async fn test_api_context(pool: sqlx::Pool<sqlx::Postgres>) -> std::sync::Arproperties_service.clone(),),team_repository: ai_tools::build_team_repository(pool.clone()),+ project_service: tool_service_context.project_tool_context.service.clone(),+ databases_service: tool_service_context.databases_tool_context.service.clone(),+ document_service: document_tool_context.service.clone(),};+ let pipedream_connections = Arc::new(+ pipedream_mcp::outbound::pg_connection_repo::PgConnectionRepo::new(pool.clone()),+ );let mcp_selector: Arc<ai_tools::ToolMcpSelector> =Arc::new(mcp_select::McpToolSelector::new(Arc::new(mcp_repo.clone()),- Arc::new(- pipedream_mcp::outbound::pg_connection_repo::PgConnectionRepo::new(- pool.clone(),- ),- ),+ pipedream_connections.clone(),Arc::new(None),));let import_service = Arc::new(@@ -631,13 +633,17 @@ pub async fn test_api_context(pool: sqlx::Pool<sqlx::Postgres>) -> std::sync::Ar.with_slack_source(Arc::new(import::outbound::mcp_slack_source::McpSlackSource::new(mcp_selector.clone()),))+ .with_notion_connections(Arc::new(+ import_notion::outbound::pipedream::PipedreamNotionConnections::new(+ pipedream_connections,+ None,+ ),+ )).with_admission(admission.clone()),);let onboarding_service = Arc::new(onboarding::domain::service::OnboardingServiceImpl::new(onboarding::outbound::pg_onboarding_repo::PgOnboardingRepo::new(pool.clone()),Arc::new(mcp_repo),- import_service.clone(),- mcp_selector.clone(),));(import_service, onboarding_service, mcp_selector)};diff --git a/services/agent_harness_service/src/cognition/api/swagger.rs b/services/agent_harness_service/src/cognition/api/swagger.rsindex 882e8f11b1..f8f1d6e9ac 100644--- a/services/agent_harness_service/src/cognition/api/swagger.rs+++ b/services/agent_harness_service/src/cognition/api/swagger.rs@@ -96,9 +96,9 @@ use utoipa::OpenApi;memory_api::get_memory_handler,import_api::get_state_handler,import_api::run_import_handler,- import_api::retry_gather_handler,import_api::discover_handler,- import_api::dismiss_run_handler,+ import_api::notion_report_handler,+ import_api::cancel_notion_handler,onboarding_api::get_state_handler,onboarding_api::complete_handler,onboarding_api::dismissed_prompts_handler,@@ -208,11 +208,17 @@ use utoipa::OpenApi;import::domain::models::ImportEntity,import::domain::models::ImportRun,import::domain::models::ImportSource,+ import::domain::models::ImportKind,import::domain::models::ImportStatus,import::domain::models::Initiator,import::domain::models::RunStatus,import::domain::models::LinearIssueMeta,import::domain::models::NotionDocMeta,+ import::domain::models::NotionPlanItem,+ import::domain::models::NotionDecision,+ import::domain::models::NotionTaskFields,+ import::domain::models::NotionImportReport,+ import::domain::models::NotionReportItem,import::domain::models::SlackChannelMeta,import::domain::models::SlackParticipant,import::domain::service::RunImportOutcome,diff --git a/services/agent_harness_service/src/cognition/server.rs b/services/agent_harness_service/src/cognition/server.rsindex 33baa59a23..6189dfa599 100644--- a/services/agent_harness_service/src/cognition/server.rs+++ b/services/agent_harness_service/src/cognition/server.rs@@ -90,10 +90,15 @@ impl CognitionRuntime {}/// Compose chat, completions, integrations, and workers in the harness process.+///+/// `import_jobs` are the harness's import jobs: this runtime's Notion imports+/// and the stalled-import sweeper run on them, and the harness hands them+/// over on shutdown.pub async fn build(config: Config,db: sqlx::PgPool,event_broker_tracker: TaskTracker,+ import_jobs: ai_tools::ImportJobs,) -> anyhow::Result<CognitionRuntime> {let public_url = macro_service_urls::CognitionPublicUrl::new()?;let aws_config = macro_aws_config::get_macro_aws_config().await;@@ -563,9 +568,9 @@ pub async fn build(),);- // The import pipeline: staged/imported external items, gather jobs over- // the user's connectors, and the Haiku import job. Built before the tool- // service context so the chat toolset gets a wired import context.+ // The import pipeline: staged/imported external items, Slack discovery,+ // and the Notion page import job. Built before the tool service context+ // so the chat toolset gets a wired import context.let mcp_encryption_key = mcp_client::domain::models::AesKey::try_from(config.mcp_credentials_key_secret_name.as_ref(),)@@ -693,33 +698,6 @@ pub async fn build(channels_connection_gateway.clone(),);- let entity_creator = ai_tools::ToolEntityCreator {- document_creator: document_tool_context.creator.clone(),- entity_access_service: entity_access_service.clone(),- channel_service: channel_tool_context.service.clone(),- task_properties: task_properties_for_import,- document_properties: document_properties_for_import,- team_repository: ai_tools::build_team_repository(db.clone()),- };- let import_service = Arc::new(- import::domain::service::ImportServiceImpl::new(- import::outbound::pg_import_repo::PgImportRepo::new(db.clone()),- mcp_selector.clone(),- Arc::new(entity_creator),- recorder.clone(),- )- .with_slack_source(Arc::new(- import::outbound::mcp_slack_source::McpSlackSource::new(mcp_selector.clone()),- ))- .with_admission(admission.clone())- .with_notifier(import_notify),- );-- // No boot-time recovery: running batches heartbeat their rows, and the- // read path reaps rows whose heartbeat stopped — safe with multiple- // replicas, where a boot-time sweep would clobber other instances' jobs.- tracing::info!("initialized import service");-let project_tool_context = ai_tools::build_project_tool_context(db.clone(),macro_event_broker.clone(),@@ -746,6 +724,51 @@ pub async fn build(),ai_tools::MaybeToolEventBroker::Real(macro_event_broker.clone()),);++ let entity_creator = ai_tools::ToolEntityCreator {+ document_creator: document_tool_context.creator.clone(),+ entity_access_service: entity_access_service.clone(),+ channel_service: channel_tool_context.service.clone(),+ task_properties: task_properties_for_import,+ document_properties: document_properties_for_import,+ team_repository: ai_tools::build_team_repository(db.clone()),+ project_service: project_tool_context.service.clone(),+ databases_service: databases_tool_context.service.clone(),+ document_service: document_tool_context.service.clone(),+ };+ let import_service = Arc::new(+ import::domain::service::ImportServiceImpl::new(+ ai_tools::build_import_repo(db.clone()),+ mcp_selector.clone(),+ Arc::new(entity_creator),+ recorder.clone(),+ )+ .with_slack_source(Arc::new(+ import::outbound::mcp_slack_source::McpSlackSource::new(mcp_selector.clone()),+ ))+ .with_notion_connections(Arc::new(+ import_notion::outbound::pipedream::PipedreamNotionConnections::new(+ Arc::new(pipedream_repo.clone()),+ pipedream_client.clone(),+ ),+ ))+ .with_admission(admission.clone())+ .with_notifier(import_notify)+ .with_job_shutdown(import_jobs.shutdown.clone(), import_jobs.tracker.clone()),+ );++ // Notion workspace imports outlive the process running them: on shutdown+ // a running job hands its run over, and the sweeper adopts runs whose+ // process stopped holding them (other replicas' live runs are leased, so+ // it leaves them alone). Slack and single-page jobs heal on read. This is+ // the process's only sweeper.+ import_jobs.tracker.spawn({+ let import_service = import_service.clone();+ let shutdown = import_jobs.shutdown.clone();+ async move { import_service.sweep_stalled_notion_imports(shutdown).await }+ });+ tracing::info!("initialized import service");+let databases_sql_tool_context = ai_tools::build_databases_sql_tool_context(&databases_tool_context,soup_service.clone(),@@ -943,14 +966,9 @@ pub async fn build(tracing::info!("initialized ai projections service");- // The onboarding flow drives the import pipeline: reads/hooks start- // auto-importing gather runs for authenticated connectors, and- // completion deletes unreserved onboarding-staged candidates.let onboarding_service = Arc::new(onboarding::domain::service::OnboardingServiceImpl::new(onboarding::outbound::pg_onboarding_repo::PgOnboardingRepo::new(db.clone()),Arc::new(mcp_server_repo.clone()),- import_service.clone(),- mcp_selector.clone(),));tracing::info!("initialized onboarding service");@@ -970,53 +988,18 @@ pub async fn build(mcp_client_metadata.clone(),mcp_pre_registered,);- // The moment a connector finishes OAuth, reconcile onboarding for that- // user — gather jobs start before the user even returns to their- // original tab. Spawned so the callback response never waits on them.- let onboarding_for_auth_hook = onboarding_service.clone();- let mcp_auth_hook: mcp_client::inbound::axum_router::McpAuthCompletedHook =- Arc::new(move |record: mcp_client::domain::models::McpServerRecord| {- let service = onboarding_for_auth_hook.clone();- Box::pin(async move {- tokio::spawn(async move {- use onboarding::domain::service::OnboardingService;- if let Err(e) = service.reconcile(record.user_id).await {- tracing::warn!(error = ?e, "post-auth onboarding reconcile failed");- }- });- })- });let mcp_state = mcp_client::inbound::McpRouterState::new(mcp_server_repo,mcp_oauth,authorization_state.clone(),mcp_client_metadata,- )- .with_auth_completed_hook(mcp_auth_hook);-- // The Pipedream stack gets the same post-connect reconcile hook as the- // native one: a finished Connect flow starts gather jobs immediately.- let onboarding_for_pipedream_hook = onboarding_service.clone();- let pipedream_auth_hook: pipedream_mcp::inbound::axum_router::PipedreamAuthCompletedHook =- Arc::new(- move |connection: pipedream_mcp::domain::models::PipedreamConnection| {- let service = onboarding_for_pipedream_hook.clone();- Box::pin(async move {- tokio::spawn(async move {- use onboarding::domain::service::OnboardingService;- if let Err(e) = service.reconcile(connection.user_id).await {- tracing::warn!(error = ?e, "post-connect onboarding reconcile failed");- }- });- })- },- );+ );+let pipedream_state = pipedream_mcp::inbound::PipedreamRouterState::new(pipedream_repo,pipedream_client,authorization_state.clone(),- )- .with_auth_completed_hook(pipedream_auth_hook);+ );let cancellation = tokio_util::sync::CancellationToken::new();super::refresh::start(diff --git a/services/agent_harness_service/src/internal_mcp.rs b/services/agent_harness_service/src/internal_mcp.rsindex d1cbcd8f9d..8b3f559154 100644--- a/services/agent_harness_service/src/internal_mcp.rs+++ b/services/agent_harness_service/src/internal_mcp.rs@@ -91,7 +91,7 @@ impl ServerHandler for InternalTools {.with_title("Macro Internal MCP");info.instructions = Some(prompt::pull_requests::internal_mcp_instructions());info.instructions.as_mut().expect("instructions are set").push_str(- " macro_internal.PresentReview presents editable email/calendar draft stacks or questions through the shared session stream. It completes immediately as Presented to the user and never waits for approval. Later account-holder decisions deliver typed review.decided and review.settled system events. GetReview reads saved drafts and outcomes by reviewId. DecideReview uses the same claim/execute path and requires a real request from the review’s account holder in the current turn; system notifications never authorize decisions. These tools are available even when code mode is disabled.",+ " macro_internal.PresentReview presents editable email/calendar draft stacks or questions through the shared session stream. It completes immediately as Presented to the user and never waits for approval. Later account-holder decisions deliver typed review.decided and review.settled system events. GetReview reads saved drafts and outcomes by reviewId. DecideReview uses the same claim/execute path and requires a real request from the review’s account holder in the current turn; system notifications never authorize decisions. After presenting an import plan, ask \"Should I kick it off?\"; when the review’s account holder says yes, submit it with macro_internal.DecideReview. These tools are available even when code mode is disabled.",);if self.code_mode.is_some() {info.instructions.as_mut().expect("instructions are set").push_str(diff --git a/services/agent_harness_service/src/internal_mcp/test.rs b/services/agent_harness_service/src/internal_mcp/test.rsindex 7d1efd2eaa..96fccc860f 100644--- a/services/agent_harness_service/src/internal_mcp/test.rs+++ b/services/agent_harness_service/src/internal_mcp/test.rs@@ -204,6 +204,8 @@ async fn each_harness_gets_only_internal_tools_and_writes_only_its_session() {assert!(instructions.contains("macro_internal.set_pull_request"));assert!(instructions.contains("register its URL"));assert!(instructions.contains(prompt::pull_requests::READY_PULL_REQUEST));+ assert!(instructions.contains("submit it with macro_internal.DecideReview"));+ assert!(instructions.contains("Should I kick it off?"));let (status, listed) = rpc(app.clone(),Some("secret"),@@ -230,6 +232,19 @@ async fn each_harness_gets_only_internal_tools_and_writes_only_its_session() {"task_pull_requests"]);+ let description = |name: &str| {+ listed["result"]["tools"]+ .as_array()+ .unwrap()+ .iter()+ .find(|tool| tool["name"] == name)+ .and_then(|tool| tool["description"].as_str())+ .unwrap()+ .to_owned()+ };+ assert!(description("PresentReview").contains("Should I kick it off?"));+ assert!(description("DecideReview").contains("Should I kick it off?"));+ assert!(description("DecideReview").contains("not ready yet"));let read_review = listed["result"]["tools"].as_array().unwrap()diff --git a/services/agent_harness_service/src/main.rs b/services/agent_harness_service/src/main.rsindex 29a41cf7d0..6402daff50 100644--- a/services/agent_harness_service/src/main.rs+++ b/services/agent_harness_service/src/main.rs@@ -531,9 +531,13 @@ async fn run() -> anyhow::Result<()> {// against it, so the two must be the same string.let egress_base_url = AgentHarnessEgressUrl::new()?.to_string();+ // Notion imports started from sessions, public MCP, and the cognition+ // routes share these jobs and hand their runs over on shutdown.+ let import_jobs = ai_tools::ImportJobs::new();let tool_context = ai_tools::build_tool_service_context_from_env(pool.clone(),event_broker_tracker.clone(),+ import_jobs.clone(),config.enable_ai_usage_enforcement,config.ai_pricing(),recorder.clone(),@@ -544,6 +548,7 @@ async fn run() -> anyhow::Result<()> {agent_harness_service::cognition::config::Config::from_env()?,pool.clone(),event_broker_tracker.clone(),+ import_jobs.clone(),).await.context("failed to compose cognition APIs")?;@@ -1492,14 +1497,32 @@ async fn run() -> anyhow::Result<()> {entity_access.clone(),MacroAuthorizationState::new(Arc::new(authorization_service.clone())),));+ let notification_service = Arc::new(agent_session::domain::notifications::NotificationService(+ Arc::new(agent_session::outbound::notifications::PgNotificationStore(+ pool.clone(),+ )),+ ));+ let app_connections = agent_harness::inbound::app_connections::app_connections_router(+ agent_harness::inbound::app_connections::AppConnectionsRouterState::new(+ Arc::new(+ agent_harness::domain::app_connections::AppConnectedContinuation::new(+ Arc::new(session_repo.clone()),+ Arc::new(+ agent_harness::outbound::connected_apps::PipedreamConnectedApps::new(+ Arc::clone(&mcp_connections),+ ),+ ),+ Arc::clone(¬ification_service),+ ),+ ),+ entity_access.clone(),+ MacroAuthorizationState::new(Arc::new(authorization_service.clone())),+ ),+ );let notification_ingress = agent_session::inbound::notifications::notification_router(agent_session::inbound::notifications::NotificationRouterState {authorization: MacroAuthorizationState::new(Arc::new(authorization_service.clone())),- service: Arc::new(agent_session::domain::notifications::NotificationService(- Arc::new(agent_session::outbound::notifications::PgNotificationStore(- pool.clone(),- )),- )),+ service: notification_service,},);let http_shutdown = service_shutdown.clone();@@ -1521,6 +1544,7 @@ async fn run() -> anyhow::Result<()> {.with_capabilities(capabilities).with_pull_requests(pull_requests).with_tool_approvals(tool_approval_answers)+ .with_app_connections(app_connections).with_code_executions(code_executions).with_notifications(notification_ingress).with_cognition(cognition_router)@@ -1930,6 +1954,9 @@ async fn run() -> anyhow::Result<()> {}}+ // Running imports stop at their next checkpoint and hand their runs over.+ import_jobs.hand_over().await;+service_shutdown.cancel();cognition_runtime.shutdown().await;scheduling_runtime.shutdown().await;diff --git a/services/document_storage_service/src/server.rs b/services/document_storage_service/src/server.rsindex 1fefe51042..f20e82d2bf 100644--- a/services/document_storage_service/src/server.rs+++ b/services/document_storage_service/src/server.rs@@ -342,6 +342,7 @@ pub async fn run() -> anyhow::Result<()> {let consumer_cancellation_token = CancellationToken::new();let consumer_tracker = TaskTracker::new();let event_broker_tracker = TaskTracker::new();+ let import_jobs = ai_tools::ImportJobs::new();let macro_event_broker = MacroEventBrokerService::new(KafkaEventPublisher::new(config.kafka_brokers.as_ref()).context("failed to create kafka event publisher")?,@@ -1369,6 +1370,7 @@ pub async fn run() -> anyhow::Result<()> {let mut macro_agent_tool_context = ai_tools::build_tool_service_context_from_env(db.clone(),event_broker_tracker.clone(),+ import_jobs.clone(),config.enable_ai_usage_enforcement,config.ai_pricing(),ai_usage::pg_recorder_with_enforcement(db.clone(), config.enable_ai_usage_enforcement),@@ -2355,6 +2357,9 @@ pub async fn run() -> anyhow::Result<()> {consumer_tracker.wait().await;tracing::info!("event consumers stopped");+ // Running imports stop at their next checkpoint and hand their runs over.+ import_jobs.hand_over().await;+tracing::info!("waiting for event broker publishes to drain");event_broker_tracker.close();match tokio::time::timeout(EVENT_BROKER_DRAIN_TIMEOUT, event_broker_tracker.wait()).await {