Skip to content

armgit / macro

Public Imported from GitHub
0

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

feat(routines): support Kafka event filters (MACRO-3553) (#6830)

Will Hutchinson8dc5e4f15206
diff --git a/.sqlx/query-00df0e15a1d75b4d9749ebf590a63ed438a7569c6b3d4ac7ed90049559c174b1.json b/.sqlx/query-00df0e15a1d75b4d9749ebf590a63ed438a7569c6b3d4ac7ed90049559c174b1.json
new file mode 100644
index 0000000000..65be2c5af6
--- /dev/null
+++ b/.sqlx/query-00df0e15a1d75b4d9749ebf590a63ed438a7569c6b3d4ac7ed90049559c174b1.json
@@ -0,0 +1,29 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n INSERT INTO action_execution_record\n (id, action_id, resource_id, start_time, end_time, is_success, result, created_at)\n VALUES ($1, $2, $3, $4, $5, $6, $7, $8)\n ON CONFLICT (id) DO NOTHING RETURNING id\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "id",
+ "type_info": "Uuid"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Uuid",
+ "Text",
+ "Timestamptz",
+ "Timestamptz",
+ "Bool",
+ "Jsonb",
+ "Timestamptz"
+ ]
+ },
+ "nullable": [
+ false
+ ]
+ },
+ "hash": "00df0e15a1d75b4d9749ebf590a63ed438a7569c6b3d4ac7ed90049559c174b1"
+}
diff --git a/.sqlx/query-0a3baa89d91ac0fd02d0562bffdf06f44686bd5d0fbf5d8ea9c2301876b3ae1d.json b/.sqlx/query-0a3baa89d91ac0fd02d0562bffdf06f44686bd5d0fbf5d8ea9c2301876b3ae1d.json
new file mode 100644
index 0000000000..c1dcf0c660
--- /dev/null
+++ b/.sqlx/query-0a3baa89d91ac0fd02d0562bffdf06f44686bd5d0fbf5d8ea9c2301876b3ae1d.json
@@ -0,0 +1,15 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n UPDATE scheduled_action\n SET next_run_at = $1, updated_at = now()\n WHERE id = $2 AND trigger_type = 'cron'\n ",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Timestamptz",
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "0a3baa89d91ac0fd02d0562bffdf06f44686bd5d0fbf5d8ea9c2301876b3ae1d"
+}
diff --git a/.sqlx/query-0ae0717aadb34404195940418891ac60590c1a0f47983618d821c33b7ecdcfd5.json b/.sqlx/query-0ae0717aadb34404195940418891ac60590c1a0f47983618d821c33b7ecdcfd5.json
new file mode 100644
index 0000000000..c87d88c192
--- /dev/null
+++ b/.sqlx/query-0ae0717aadb34404195940418891ac60590c1a0f47983618d821c33b7ecdcfd5.json
@@ -0,0 +1,124 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n UPDATE scheduled_action\n SET name = $1,\n schedule = $2,\n kind = $3,\n timezone = $4,\n task = $5,\n next_run_at = $6,\n enabled = $7,\n trigger_type = $8,\n event_filters = $9,\n configuration_revision = $10,\n event_activated_at = $11,\n updated_at = now()\n WHERE id = $12 AND owner = $13\n RETURNING id, owner, name, schedule, kind, timezone, task, claimed, created_at,\n updated_at, next_run_at, enabled, trigger_type, event_filters,\n configuration_revision, event_activated_at\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "id",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 1,
+ "name": "owner",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 2,
+ "name": "name",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 3,
+ "name": "schedule",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 4,
+ "name": "kind",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 5,
+ "name": "timezone",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 6,
+ "name": "task",
+ "type_info": "Jsonb"
+ },
+ {
+ "ordinal": 7,
+ "name": "claimed",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 8,
+ "name": "created_at",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 9,
+ "name": "updated_at",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 10,
+ "name": "next_run_at",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 11,
+ "name": "enabled",
+ "type_info": "Bool"
+ },
+ {
+ "ordinal": 12,
+ "name": "trigger_type",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 13,
+ "name": "event_filters",
+ "type_info": "Jsonb"
+ },
+ {
+ "ordinal": 14,
+ "name": "configuration_revision",
+ "type_info": "Int8"
+ },
+ {
+ "ordinal": 15,
+ "name": "event_activated_at",
+ "type_info": "Timestamptz"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Text",
+ "Text",
+ "Text",
+ "Text",
+ "Jsonb",
+ "Timestamptz",
+ "Bool",
+ "Text",
+ "Jsonb",
+ "Int8",
+ "Timestamptz",
+ "Uuid",
+ "Text"
+ ]
+ },
+ "nullable": [
+ false,
+ false,
+ false,
+ true,
+ false,
+ true,
+ false,
+ true,
+ false,
+ false,
+ true,
+ false,
+ false,
+ true,
+ false,
+ true
+ ]
+ },
+ "hash": "0ae0717aadb34404195940418891ac60590c1a0f47983618d821c33b7ecdcfd5"
+}
diff --git a/.sqlx/query-0cc3b33f09455637437947f04dec4e4ddbbd9fefc4ccf3e5877d8e4162da0b9e.json b/.sqlx/query-0cc3b33f09455637437947f04dec4e4ddbbd9fefc4ccf3e5877d8e4162da0b9e.json
new file mode 100644
index 0000000000..77cced870e
--- /dev/null
+++ b/.sqlx/query-0cc3b33f09455637437947f04dec4e4ddbbd9fefc4ccf3e5877d8e4162da0b9e.json
@@ -0,0 +1,14 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "INSERT INTO \"User\" (id, email, macro_user_id)\n SELECT $1, $1, id FROM macro_user WHERE email = $1 ON CONFLICT DO NOTHING",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Text"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "0cc3b33f09455637437947f04dec4e4ddbbd9fefc4ccf3e5877d8e4162da0b9e"
+}
diff --git a/.sqlx/query-0e77131ec7a8afa9f8e58dbe401234ae26915f7b7a401c1778534acaac3ca3a0.json b/.sqlx/query-0e77131ec7a8afa9f8e58dbe401234ae26915f7b7a401c1778534acaac3ca3a0.json
new file mode 100644
index 0000000000..624d6ec1ed
--- /dev/null
+++ b/.sqlx/query-0e77131ec7a8afa9f8e58dbe401234ae26915f7b7a401c1778534acaac3ca3a0.json
@@ -0,0 +1,53 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n INSERT INTO scheduled_action (owner, name, schedule, kind, timezone, task, next_run_at, enabled)\n VALUES ($1, $2, $3, $4, $5, $6, $7, $8)\n RETURNING id, trigger_type, configuration_revision, event_filters, event_activated_at\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "id",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 1,
+ "name": "trigger_type",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 2,
+ "name": "configuration_revision",
+ "type_info": "Int8"
+ },
+ {
+ "ordinal": 3,
+ "name": "event_filters",
+ "type_info": "Jsonb"
+ },
+ {
+ "ordinal": 4,
+ "name": "event_activated_at",
+ "type_info": "Timestamptz"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Text",
+ "Text",
+ "Text",
+ "Text",
+ "Text",
+ "Jsonb",
+ "Timestamptz",
+ "Bool"
+ ]
+ },
+ "nullable": [
+ false,
+ false,
+ false,
+ true,
+ true
+ ]
+ },
+ "hash": "0e77131ec7a8afa9f8e58dbe401234ae26915f7b7a401c1778534acaac3ca3a0"
+}
diff --git a/.sqlx/query-0f1fbb26939196a6023a96fafef0b1370fd5ce57595340bef54eabbfc9011ace.json b/.sqlx/query-0f1fbb26939196a6023a96fafef0b1370fd5ce57595340bef54eabbfc9011ace.json
new file mode 100644
index 0000000000..1880fb2845
--- /dev/null
+++ b/.sqlx/query-0f1fbb26939196a6023a96fafef0b1370fd5ce57595340bef54eabbfc9011ace.json
@@ -0,0 +1,17 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n UPDATE scheduled_action_event_run SET state = 'finished', finished_at = now(), outcome = $4\n WHERE action_id = $1 AND event_id = $2 AND configuration_revision = $3 AND state = 'pending'\n ",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Uuid",
+ "Int8",
+ "Jsonb"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "0f1fbb26939196a6023a96fafef0b1370fd5ce57595340bef54eabbfc9011ace"
+}
diff --git a/.sqlx/query-11aaeb83da5085bcd5e67566537f1d780a08b7a15856b2d50226b7e024211aa2.json b/.sqlx/query-11aaeb83da5085bcd5e67566537f1d780a08b7a15856b2d50226b7e024211aa2.json
new file mode 100644
index 0000000000..4b131d811b
--- /dev/null
+++ b/.sqlx/query-11aaeb83da5085bcd5e67566537f1d780a08b7a15856b2d50226b7e024211aa2.json
@@ -0,0 +1,47 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n SELECT r.action_id, r.event_id, r.configuration_revision, r.event_context, r.admitted_at\n FROM scheduled_action a\n CROSS JOIN LATERAL (\n SELECT action_id, event_id, configuration_revision, event_context, admitted_at, admission_order\n FROM scheduled_action_event_run\n WHERE action_id = a.id AND state = 'pending'\n ORDER BY admission_order LIMIT 1\n ) r\n WHERE a.enabled AND a.trigger_type = 'events'\n AND a.configuration_revision = r.configuration_revision\n AND (a.claimed IS NULL OR a.claimed < $1)\n AND NOT EXISTS (SELECT 1 FROM scheduled_action_event_run s WHERE s.action_id = a.id AND s.state = 'started')\n ORDER BY r.admission_order\n LIMIT $2 FOR UPDATE OF a SKIP LOCKED\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "action_id",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 1,
+ "name": "event_id",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 2,
+ "name": "configuration_revision",
+ "type_info": "Int8"
+ },
+ {
+ "ordinal": 3,
+ "name": "event_context",
+ "type_info": "Jsonb"
+ },
+ {
+ "ordinal": 4,
+ "name": "admitted_at",
+ "type_info": "Timestamptz"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Timestamptz",
+ "Int8"
+ ]
+ },
+ "nullable": [
+ false,
+ false,
+ false,
+ false,
+ false
+ ]
+ },
+ "hash": "11aaeb83da5085bcd5e67566537f1d780a08b7a15856b2d50226b7e024211aa2"
+}
diff --git a/.sqlx/query-1c9f7e8a6e0c50dee802223e3df95237dc608c33310d2d17048b340604cb602d.json b/.sqlx/query-1c9f7e8a6e0c50dee802223e3df95237dc608c33310d2d17048b340604cb602d.json
new file mode 100644
index 0000000000..341f61daa9
--- /dev/null
+++ b/.sqlx/query-1c9f7e8a6e0c50dee802223e3df95237dc608c33310d2d17048b340604cb602d.json
@@ -0,0 +1,15 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "INSERT INTO \"User\" (id, email, macro_user_id)\n SELECT $1, $1, id FROM macro_user WHERE email = $2",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Text",
+ "Text"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "1c9f7e8a6e0c50dee802223e3df95237dc608c33310d2d17048b340604cb602d"
+}
diff --git a/.sqlx/query-230e59f6b2343c770363b67cacf66af58498dac9cb8c4fe63df1a78d38680b76.json b/.sqlx/query-230e59f6b2343c770363b67cacf66af58498dac9cb8c4fe63df1a78d38680b76.json
new file mode 100644
index 0000000000..ca02a83551
--- /dev/null
+++ b/.sqlx/query-230e59f6b2343c770363b67cacf66af58498dac9cb8c4fe63df1a78d38680b76.json
@@ -0,0 +1,78 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n SELECT id, owner, name, kind, task, created_at, updated_at, enabled,\n event_filters AS \"event_filters!\", event_activated_at AS \"event_activated_at!\"\n FROM scheduled_action\n WHERE id = $1 AND enabled AND trigger_type = 'events' AND configuration_revision = $2\n AND (claimed IS NULL OR claimed < $3)\n AND NOT EXISTS (SELECT 1 FROM scheduled_action_event_run WHERE action_id = $1 AND state = 'started')\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "id",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 1,
+ "name": "owner",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 2,
+ "name": "name",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 3,
+ "name": "kind",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 4,
+ "name": "task",
+ "type_info": "Jsonb"
+ },
+ {
+ "ordinal": 5,
+ "name": "created_at",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 6,
+ "name": "updated_at",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 7,
+ "name": "enabled",
+ "type_info": "Bool"
+ },
+ {
+ "ordinal": 8,
+ "name": "event_filters!",
+ "type_info": "Jsonb"
+ },
+ {
+ "ordinal": 9,
+ "name": "event_activated_at!",
+ "type_info": "Timestamptz"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Int8",
+ "Timestamptz"
+ ]
+ },
+ "nullable": [
+ false,
+ false,
+ false,
+ false,
+ false,
+ false,
+ false,
+ false,
+ true,
+ true
+ ]
+ },
+ "hash": "230e59f6b2343c770363b67cacf66af58498dac9cb8c4fe63df1a78d38680b76"
+}
diff --git a/.sqlx/query-27385b4ff354563a2cc18f745cb98b859e73b7bb1932d2cafcd5177a435e35db.json b/.sqlx/query-27385b4ff354563a2cc18f745cb98b859e73b7bb1932d2cafcd5177a435e35db.json
new file mode 100644
index 0000000000..24c380322d
--- /dev/null
+++ b/.sqlx/query-27385b4ff354563a2cc18f745cb98b859e73b7bb1932d2cafcd5177a435e35db.json
@@ -0,0 +1,22 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT COUNT(*) AS \"count!\" FROM scheduled_action WHERE id = $1",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "count!",
+ "type_info": "Int8"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ null
+ ]
+ },
+ "hash": "27385b4ff354563a2cc18f745cb98b859e73b7bb1932d2cafcd5177a435e35db"
+}
diff --git a/.sqlx/query-284c0e3e8f0f53e13fce4a7f67d6d9d7eaf77fac610f2275facf75c3a9f62cfe.json b/.sqlx/query-284c0e3e8f0f53e13fce4a7f67d6d9d7eaf77fac610f2275facf75c3a9f62cfe.json
new file mode 100644
index 0000000000..c9c4920508
--- /dev/null
+++ b/.sqlx/query-284c0e3e8f0f53e13fce4a7f67d6d9d7eaf77fac610f2275facf75c3a9f62cfe.json
@@ -0,0 +1,52 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n SELECT id AS action_id, owner, enabled, configuration_revision,\n event_filters AS \"event_filters!\", event_activated_at AS \"event_activated_at!\"\n FROM scheduled_action WHERE id = $1 AND trigger_type = 'events'\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "action_id",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 1,
+ "name": "owner",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 2,
+ "name": "enabled",
+ "type_info": "Bool"
+ },
+ {
+ "ordinal": 3,
+ "name": "configuration_revision",
+ "type_info": "Int8"
+ },
+ {
+ "ordinal": 4,
+ "name": "event_filters!",
+ "type_info": "Jsonb"
+ },
+ {
+ "ordinal": 5,
+ "name": "event_activated_at!",
+ "type_info": "Timestamptz"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ false,
+ false,
+ false,
+ false,
+ true,
+ true
+ ]
+ },
+ "hash": "284c0e3e8f0f53e13fce4a7f67d6d9d7eaf77fac610f2275facf75c3a9f62cfe"
+}
diff --git a/.sqlx/query-2f93274eec14c8c17d59251484b537247921f54e4bc87384e132668d8aa618e2.json b/.sqlx/query-2f93274eec14c8c17d59251484b537247921f54e4bc87384e132668d8aa618e2.json
new file mode 100644
index 0000000000..dc31ec9d13
--- /dev/null
+++ b/.sqlx/query-2f93274eec14c8c17d59251484b537247921f54e4bc87384e132668d8aa618e2.json
@@ -0,0 +1,56 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n SELECT id AS action_id, owner, enabled, configuration_revision,\n event_filters AS \"event_filters!\", event_activated_at AS \"event_activated_at!\"\n FROM scheduled_action\n WHERE enabled AND trigger_type = 'events'\n AND ($1::uuid IS NULL OR id > $1)\n AND event_activated_at <= $2\n AND event_filters @> jsonb_build_array(jsonb_build_object('events', jsonb_build_array($3::text)))\n AND jsonb_path_exists(event_filters,\n '$[*] ? (@.events[*] == $event && (!exists (@.ids) || @.ids == null || @.ids[*] == $id))',\n jsonb_build_object('event', $3::text, 'id', $4::text))\n ORDER BY id\n LIMIT $5\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "action_id",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 1,
+ "name": "owner",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 2,
+ "name": "enabled",
+ "type_info": "Bool"
+ },
+ {
+ "ordinal": 3,
+ "name": "configuration_revision",
+ "type_info": "Int8"
+ },
+ {
+ "ordinal": 4,
+ "name": "event_filters!",
+ "type_info": "Jsonb"
+ },
+ {
+ "ordinal": 5,
+ "name": "event_activated_at!",
+ "type_info": "Timestamptz"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Timestamptz",
+ "Text",
+ "Text",
+ "Int8"
+ ]
+ },
+ "nullable": [
+ false,
+ false,
+ false,
+ false,
+ true,
+ true
+ ]
+ },
+ "hash": "2f93274eec14c8c17d59251484b537247921f54e4bc87384e132668d8aa618e2"
+}
diff --git a/.sqlx/query-370930b952ebdcfb0cf03c781b1a90ce35b2b3449af1547b2e232e2bd61dd3d8.json b/.sqlx/query-370930b952ebdcfb0cf03c781b1a90ce35b2b3449af1547b2e232e2bd61dd3d8.json
new file mode 100644
index 0000000000..a14c8a9844
--- /dev/null
+++ b/.sqlx/query-370930b952ebdcfb0cf03c781b1a90ce35b2b3449af1547b2e232e2bd61dd3d8.json
@@ -0,0 +1,22 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT claim_token FROM scheduled_action WHERE id = $1 FOR UPDATE",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "claim_token",
+ "type_info": "Uuid"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ true
+ ]
+ },
+ "hash": "370930b952ebdcfb0cf03c781b1a90ce35b2b3449af1547b2e232e2bd61dd3d8"
+}
diff --git a/.sqlx/query-40ecb68cdcd633aefd35d32bcdb7349efe4ffcc5cac507df1afd7481a344708a.json b/.sqlx/query-40ecb68cdcd633aefd35d32bcdb7349efe4ffcc5cac507df1afd7481a344708a.json
new file mode 100644
index 0000000000..2e3bb4d8ae
--- /dev/null
+++ b/.sqlx/query-40ecb68cdcd633aefd35d32bcdb7349efe4ffcc5cac507df1afd7481a344708a.json
@@ -0,0 +1,26 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n SELECT id FROM scheduled_action\n WHERE id = $1 AND enabled AND trigger_type = 'events'\n AND configuration_revision = $2 AND event_activated_at <= $3\n AND jsonb_path_exists(event_filters,\n '$[*] ? (@.events[*] == $event && (!exists (@.ids) || @.ids == null || @.ids[*] == $id))',\n jsonb_build_object('event', $4::text, 'id', $5::text))\n FOR UPDATE\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "id",
+ "type_info": "Uuid"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Int8",
+ "Timestamptz",
+ "Text",
+ "Text"
+ ]
+ },
+ "nullable": [
+ false
+ ]
+ },
+ "hash": "40ecb68cdcd633aefd35d32bcdb7349efe4ffcc5cac507df1afd7481a344708a"
+}
diff --git a/.sqlx/query-41f74379907081bd7bca8a2453f51312eca5cd57176f615f72fc0fbee455bbb2.json b/.sqlx/query-41f74379907081bd7bca8a2453f51312eca5cd57176f615f72fc0fbee455bbb2.json
new file mode 100644
index 0000000000..b74f32c6ca
--- /dev/null
+++ b/.sqlx/query-41f74379907081bd7bca8a2453f51312eca5cd57176f615f72fc0fbee455bbb2.json
@@ -0,0 +1,16 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "UPDATE scheduled_action SET claimed = $2, claim_token = $3 WHERE id = $1",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Timestamptz",
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "41f74379907081bd7bca8a2453f51312eca5cd57176f615f72fc0fbee455bbb2"
+}
diff --git a/.sqlx/query-42b88973ab31145895857f5bcae7ad85f50146513f21b3ce12efd4a295e2e3af.json b/.sqlx/query-42b88973ab31145895857f5bcae7ad85f50146513f21b3ce12efd4a295e2e3af.json
new file mode 100644
index 0000000000..2d1a717019
--- /dev/null
+++ b/.sqlx/query-42b88973ab31145895857f5bcae7ad85f50146513f21b3ce12efd4a295e2e3af.json
@@ -0,0 +1,15 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "UPDATE scheduled_action SET claim_token = $2 WHERE id = $1",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "42b88973ab31145895857f5bcae7ad85f50146513f21b3ce12efd4a295e2e3af"
+}
diff --git a/.sqlx/query-4460ad8f431c3bd2d23106bba737743a674adec629cd79ebcda148fc6d39daed.json b/.sqlx/query-4460ad8f431c3bd2d23106bba737743a674adec629cd79ebcda148fc6d39daed.json
new file mode 100644
index 0000000000..24601c7e75
--- /dev/null
+++ b/.sqlx/query-4460ad8f431c3bd2d23106bba737743a674adec629cd79ebcda148fc6d39daed.json
@@ -0,0 +1,14 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "UPDATE scheduled_action SET claimed = NULL WHERE id = $1",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "4460ad8f431c3bd2d23106bba737743a674adec629cd79ebcda148fc6d39daed"
+}
diff --git a/.sqlx/query-46d2ff64b6b9a3b0a6806b50c9e039b33687b8c60bd59f1495a6f727c9493be4.json b/.sqlx/query-46d2ff64b6b9a3b0a6806b50c9e039b33687b8c60bd59f1495a6f727c9493be4.json
new file mode 100644
index 0000000000..bb3b0c0e29
--- /dev/null
+++ b/.sqlx/query-46d2ff64b6b9a3b0a6806b50c9e039b33687b8c60bd59f1495a6f727c9493be4.json
@@ -0,0 +1,15 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "UPDATE scheduled_action SET event_activated_at = $2 WHERE id = $1",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Timestamptz"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "46d2ff64b6b9a3b0a6806b50c9e039b33687b8c60bd59f1495a6f727c9493be4"
+}
diff --git a/.sqlx/query-4889cf7b6100a28da3f99b43b9264f7607636456460db845b8abfe3814b7b7f7.json b/.sqlx/query-4889cf7b6100a28da3f99b43b9264f7607636456460db845b8abfe3814b7b7f7.json
new file mode 100644
index 0000000000..c893416058
--- /dev/null
+++ b/.sqlx/query-4889cf7b6100a28da3f99b43b9264f7607636456460db845b8abfe3814b7b7f7.json
@@ -0,0 +1,125 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n UPDATE scheduled_action\n SET name = $1,\n schedule = $2,\n kind = $3,\n timezone = $4,\n task = $5,\n next_run_at = $6,\n enabled = $7,\n trigger_type = $8,\n event_filters = $9,\n configuration_revision = $10,\n event_activated_at = $11,\n updated_at = now()\n WHERE id = $12 AND owner = $13\n AND configuration_revision = $10::bigint - 1\n AND (\n claimed IS NULL OR claimed < $14\n OR (\n NOT $7 AND name = $1 AND kind = $3 AND task = $5\n AND trigger_type = $8\n AND schedule IS NOT DISTINCT FROM $2\n AND timezone IS NOT DISTINCT FROM $4\n AND event_filters IS NOT DISTINCT FROM $9\n )\n )\n RETURNING id, owner, name, schedule, kind, timezone, task, claimed, created_at,\n updated_at, next_run_at, enabled, trigger_type, event_filters,\n configuration_revision, event_activated_at\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "id",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 1,
+ "name": "owner",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 2,
+ "name": "name",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 3,
+ "name": "schedule",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 4,
+ "name": "kind",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 5,
+ "name": "timezone",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 6,
+ "name": "task",
+ "type_info": "Jsonb"
+ },
+ {
+ "ordinal": 7,
+ "name": "claimed",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 8,
+ "name": "created_at",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 9,
+ "name": "updated_at",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 10,
+ "name": "next_run_at",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 11,
+ "name": "enabled",
+ "type_info": "Bool"
+ },
+ {
+ "ordinal": 12,
+ "name": "trigger_type",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 13,
+ "name": "event_filters",
+ "type_info": "Jsonb"
+ },
+ {
+ "ordinal": 14,
+ "name": "configuration_revision",
+ "type_info": "Int8"
+ },
+ {
+ "ordinal": 15,
+ "name": "event_activated_at",
+ "type_info": "Timestamptz"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Text",
+ "Text",
+ "Text",
+ "Text",
+ "Jsonb",
+ "Timestamptz",
+ "Bool",
+ "Text",
+ "Jsonb",
+ "Int8",
+ "Timestamptz",
+ "Uuid",
+ "Text",
+ "Timestamptz"
+ ]
+ },
+ "nullable": [
+ false,
+ false,
+ false,
+ true,
+ false,
+ true,
+ false,
+ true,
+ false,
+ false,
+ true,
+ false,
+ false,
+ true,
+ false,
+ true
+ ]
+ },
+ "hash": "4889cf7b6100a28da3f99b43b9264f7607636456460db845b8abfe3814b7b7f7"
+}
diff --git a/.sqlx/query-49b2a83cc752ab42d51103daddb4d89f536d535ce45c7a3f3bbfee368799d2d8.json b/.sqlx/query-49b2a83cc752ab42d51103daddb4d89f536d535ce45c7a3f3bbfee368799d2d8.json
new file mode 100644
index 0000000000..58d326e6ff
--- /dev/null
+++ b/.sqlx/query-49b2a83cc752ab42d51103daddb4d89f536d535ce45c7a3f3bbfee368799d2d8.json
@@ -0,0 +1,16 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "UPDATE scheduled_action SET claimed = $2, claim_token = $3, updated_at = now() WHERE id = $1",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Timestamptz",
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "49b2a83cc752ab42d51103daddb4d89f536d535ce45c7a3f3bbfee368799d2d8"
+}
diff --git a/.sqlx/query-4b04c62d7d0a3b20e37e05ff0c391888b910e5523923798f0eef7817ae2bca80.json b/.sqlx/query-4b04c62d7d0a3b20e37e05ff0c391888b910e5523923798f0eef7817ae2bca80.json
new file mode 100644
index 0000000000..587f80736a
--- /dev/null
+++ b/.sqlx/query-4b04c62d7d0a3b20e37e05ff0c391888b910e5523923798f0eef7817ae2bca80.json
@@ -0,0 +1,18 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n UPDATE scheduled_action_event_run\n SET state = 'started', claim_token = $3, started_at = $4, deadline = $5\n WHERE action_id = $1 AND event_id = $2 AND state = 'pending'\n ",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Uuid",
+ "Uuid",
+ "Timestamptz",
+ "Timestamptz"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "4b04c62d7d0a3b20e37e05ff0c391888b910e5523923798f0eef7817ae2bca80"
+}
diff --git a/.sqlx/query-5021113af6bb370e324911c7b2e4a5bd772764f9b0649504b998465a8aba81f1.json b/.sqlx/query-5021113af6bb370e324911c7b2e4a5bd772764f9b0649504b998465a8aba81f1.json
new file mode 100644
index 0000000000..194e7ec561
--- /dev/null
+++ b/.sqlx/query-5021113af6bb370e324911c7b2e4a5bd772764f9b0649504b998465a8aba81f1.json
@@ -0,0 +1,22 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT claim_token FROM scheduled_action WHERE id = $1",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "claim_token",
+ "type_info": "Uuid"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ true
+ ]
+ },
+ "hash": "5021113af6bb370e324911c7b2e4a5bd772764f9b0649504b998465a8aba81f1"
+}
diff --git a/.sqlx/query-59517bccf29ea6e96b86f6a7f28c861c0db7709ab72c68f7a5c27b275f96bc65.json b/.sqlx/query-59517bccf29ea6e96b86f6a7f28c861c0db7709ab72c68f7a5c27b275f96bc65.json
new file mode 100644
index 0000000000..ad3ff9550a
--- /dev/null
+++ b/.sqlx/query-59517bccf29ea6e96b86f6a7f28c861c0db7709ab72c68f7a5c27b275f96bc65.json
@@ -0,0 +1,35 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT state, outcome, execution_record_id FROM scheduled_action_event_run WHERE action_id = $1 AND event_id = $2",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "state",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 1,
+ "name": "outcome",
+ "type_info": "Jsonb"
+ },
+ {
+ "ordinal": 2,
+ "name": "execution_record_id",
+ "type_info": "Uuid"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ false,
+ true,
+ true
+ ]
+ },
+ "hash": "59517bccf29ea6e96b86f6a7f28c861c0db7709ab72c68f7a5c27b275f96bc65"
+}
diff --git a/.sqlx/query-61feae64855668289766559613c2fd6f395fedfaf78f43ac5d601b0cff4e75d9.json b/.sqlx/query-61feae64855668289766559613c2fd6f395fedfaf78f43ac5d601b0cff4e75d9.json
new file mode 100644
index 0000000000..d25c32856c
--- /dev/null
+++ b/.sqlx/query-61feae64855668289766559613c2fd6f395fedfaf78f43ac5d601b0cff4e75d9.json
@@ -0,0 +1,29 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n SELECT r.action_id, r.event_id FROM scheduled_action_event_run r\n JOIN scheduled_action a ON a.id = r.action_id\n WHERE (r.state = 'started' AND r.deadline <= $1)\n OR (r.state = 'pending' AND (NOT a.enabled OR a.trigger_type <> 'events'\n OR a.configuration_revision <> r.configuration_revision))\n ORDER BY r.admission_order LIMIT $2 FOR UPDATE OF a SKIP LOCKED\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "action_id",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 1,
+ "name": "event_id",
+ "type_info": "Uuid"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Timestamptz",
+ "Int8"
+ ]
+ },
+ "nullable": [
+ false,
+ false
+ ]
+ },
+ "hash": "61feae64855668289766559613c2fd6f395fedfaf78f43ac5d601b0cff4e75d9"
+}
diff --git a/.sqlx/query-6698a34f1ed6f260bf83aee8dd6b50ac6af61a813d540a5e60942ead60de42e5.json b/.sqlx/query-6698a34f1ed6f260bf83aee8dd6b50ac6af61a813d540a5e60942ead60de42e5.json
new file mode 100644
index 0000000000..acb6bf88aa
--- /dev/null
+++ b/.sqlx/query-6698a34f1ed6f260bf83aee8dd6b50ac6af61a813d540a5e60942ead60de42e5.json
@@ -0,0 +1,15 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n DELETE FROM scheduled_action\n WHERE id = $1 AND owner = $2\n ",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Text"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "6698a34f1ed6f260bf83aee8dd6b50ac6af61a813d540a5e60942ead60de42e5"
+}
diff --git a/.sqlx/query-6975627d7250479d94dbd98e14bc0643dfb9b9e4f3700dd6d54f34c677c5f385.json b/.sqlx/query-6975627d7250479d94dbd98e14bc0643dfb9b9e4f3700dd6d54f34c677c5f385.json
new file mode 100644
index 0000000000..4680e732cf
--- /dev/null
+++ b/.sqlx/query-6975627d7250479d94dbd98e14bc0643dfb9b9e4f3700dd6d54f34c677c5f385.json
@@ -0,0 +1,124 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n INSERT INTO scheduled_action\n (id, owner, name, schedule, kind, timezone, task, next_run_at, enabled,\n trigger_type, event_filters, configuration_revision, event_activated_at)\n VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)\n RETURNING id, owner, name, schedule, kind, timezone, task, claimed, created_at,\n updated_at, next_run_at, enabled, trigger_type, event_filters,\n configuration_revision, event_activated_at\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "id",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 1,
+ "name": "owner",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 2,
+ "name": "name",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 3,
+ "name": "schedule",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 4,
+ "name": "kind",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 5,
+ "name": "timezone",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 6,
+ "name": "task",
+ "type_info": "Jsonb"
+ },
+ {
+ "ordinal": 7,
+ "name": "claimed",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 8,
+ "name": "created_at",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 9,
+ "name": "updated_at",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 10,
+ "name": "next_run_at",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 11,
+ "name": "enabled",
+ "type_info": "Bool"
+ },
+ {
+ "ordinal": 12,
+ "name": "trigger_type",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 13,
+ "name": "event_filters",
+ "type_info": "Jsonb"
+ },
+ {
+ "ordinal": 14,
+ "name": "configuration_revision",
+ "type_info": "Int8"
+ },
+ {
+ "ordinal": 15,
+ "name": "event_activated_at",
+ "type_info": "Timestamptz"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Text",
+ "Text",
+ "Text",
+ "Text",
+ "Text",
+ "Jsonb",
+ "Timestamptz",
+ "Bool",
+ "Text",
+ "Jsonb",
+ "Int8",
+ "Timestamptz"
+ ]
+ },
+ "nullable": [
+ false,
+ false,
+ false,
+ true,
+ false,
+ true,
+ false,
+ true,
+ false,
+ false,
+ true,
+ false,
+ false,
+ true,
+ false,
+ true
+ ]
+ },
+ "hash": "6975627d7250479d94dbd98e14bc0643dfb9b9e4f3700dd6d54f34c677c5f385"
+}
diff --git a/.sqlx/query-6baf01caee63a634cf1fcd5a3667c61cc68a867af310783113d83cd5ed57974d.json b/.sqlx/query-6baf01caee63a634cf1fcd5a3667c61cc68a867af310783113d83cd5ed57974d.json
new file mode 100644
index 0000000000..747734ca0d
--- /dev/null
+++ b/.sqlx/query-6baf01caee63a634cf1fcd5a3667c61cc68a867af310783113d83cd5ed57974d.json
@@ -0,0 +1,14 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "UPDATE scheduled_action SET configuration_revision = configuration_revision + 1 WHERE id = $1",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "6baf01caee63a634cf1fcd5a3667c61cc68a867af310783113d83cd5ed57974d"
+}
diff --git a/.sqlx/query-7615dbd23b600f4e15c45024d84a0366c3ef228e00417b272922c94b66285685.json b/.sqlx/query-7615dbd23b600f4e15c45024d84a0366c3ef228e00417b272922c94b66285685.json
new file mode 100644
index 0000000000..beddf1a164
--- /dev/null
+++ b/.sqlx/query-7615dbd23b600f4e15c45024d84a0366c3ef228e00417b272922c94b66285685.json
@@ -0,0 +1,14 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "UPDATE scheduled_action SET enabled = false WHERE id = $1",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "7615dbd23b600f4e15c45024d84a0366c3ef228e00417b272922c94b66285685"
+}
diff --git a/.sqlx/query-7e496227f6eb0829a95ccc6b6e4b4dc4402f0c119bac82ef9b136b354a6466e3.json b/.sqlx/query-7e496227f6eb0829a95ccc6b6e4b4dc4402f0c119bac82ef9b136b354a6466e3.json
new file mode 100644
index 0000000000..2d4791f800
--- /dev/null
+++ b/.sqlx/query-7e496227f6eb0829a95ccc6b6e4b4dc4402f0c119bac82ef9b136b354a6466e3.json
@@ -0,0 +1,22 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT COUNT(*) AS \"count!\" FROM entity WHERE id = $1",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "count!",
+ "type_info": "Int8"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ null
+ ]
+ },
+ "hash": "7e496227f6eb0829a95ccc6b6e4b4dc4402f0c119bac82ef9b136b354a6466e3"
+}
diff --git a/.sqlx/query-81cfa29d3ef15ee3afd3eecfa95cd0672b36eed05d7ab9d8ca1342bb6ee4a2b4.json b/.sqlx/query-81cfa29d3ef15ee3afd3eecfa95cd0672b36eed05d7ab9d8ca1342bb6ee4a2b4.json
new file mode 100644
index 0000000000..17de52602e
--- /dev/null
+++ b/.sqlx/query-81cfa29d3ef15ee3afd3eecfa95cd0672b36eed05d7ab9d8ca1342bb6ee4a2b4.json
@@ -0,0 +1,17 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n UPDATE scheduled_action_event_run SET started_at = $3, deadline = $4\n WHERE action_id = $1 AND event_id = $2\n ",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Uuid",
+ "Timestamptz",
+ "Timestamptz"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "81cfa29d3ef15ee3afd3eecfa95cd0672b36eed05d7ab9d8ca1342bb6ee4a2b4"
+}
diff --git a/.sqlx/query-a2b1be64dc00ad01197ffa76bdd8c92829439094f9dfa529b1d4fa2da6ad20cc.json b/.sqlx/query-a2b1be64dc00ad01197ffa76bdd8c92829439094f9dfa529b1d4fa2da6ad20cc.json
new file mode 100644
index 0000000000..a0f976d8fc
--- /dev/null
+++ b/.sqlx/query-a2b1be64dc00ad01197ffa76bdd8c92829439094f9dfa529b1d4fa2da6ad20cc.json
@@ -0,0 +1,40 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n SELECT\n owner_type::text AS \"owner_type!\",\n owner_id,\n entity_type,\n deleted_at\n FROM entity\n WHERE id = $1\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "owner_type!",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 1,
+ "name": "owner_id",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 2,
+ "name": "entity_type",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 3,
+ "name": "deleted_at",
+ "type_info": "Timestamptz"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ null,
+ false,
+ false,
+ true
+ ]
+ },
+ "hash": "a2b1be64dc00ad01197ffa76bdd8c92829439094f9dfa529b1d4fa2da6ad20cc"
+}
diff --git a/.sqlx/query-a34a48a95114b19e13dcb7756ab596c7b8f1ae34dec6e1d5cb9f1cf85c76d04e.json b/.sqlx/query-a34a48a95114b19e13dcb7756ab596c7b8f1ae34dec6e1d5cb9f1cf85c76d04e.json
new file mode 100644
index 0000000000..717e1fde17
--- /dev/null
+++ b/.sqlx/query-a34a48a95114b19e13dcb7756ab596c7b8f1ae34dec6e1d5cb9f1cf85c76d04e.json
@@ -0,0 +1,46 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n SELECT action_id, event_id, configuration_revision, event_context, admitted_at\n FROM scheduled_action_event_run\n WHERE action_id = $1 AND state = 'pending'\n ORDER BY admission_order LIMIT 1 FOR UPDATE\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "action_id",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 1,
+ "name": "event_id",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 2,
+ "name": "configuration_revision",
+ "type_info": "Int8"
+ },
+ {
+ "ordinal": 3,
+ "name": "event_context",
+ "type_info": "Jsonb"
+ },
+ {
+ "ordinal": 4,
+ "name": "admitted_at",
+ "type_info": "Timestamptz"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ false,
+ false,
+ false,
+ false,
+ false
+ ]
+ },
+ "hash": "a34a48a95114b19e13dcb7756ab596c7b8f1ae34dec6e1d5cb9f1cf85c76d04e"
+}
diff --git a/.sqlx/query-a4e1faa9186a0579d9dc4307d158019eea1904a2e412df235f603f5b80986ec3.json b/.sqlx/query-a4e1faa9186a0579d9dc4307d158019eea1904a2e412df235f603f5b80986ec3.json
new file mode 100644
index 0000000000..cf6bad164e
--- /dev/null
+++ b/.sqlx/query-a4e1faa9186a0579d9dc4307d158019eea1904a2e412df235f603f5b80986ec3.json
@@ -0,0 +1,14 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "DELETE FROM entity WHERE id = $1",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "a4e1faa9186a0579d9dc4307d158019eea1904a2e412df235f603f5b80986ec3"
+}
diff --git a/.sqlx/query-b323ec9f47fdfc1afde86254d097b43c650e1937ea88b234ba2d1ff86f55f2e8.json b/.sqlx/query-b323ec9f47fdfc1afde86254d097b43c650e1937ea88b234ba2d1ff86f55f2e8.json
new file mode 100644
index 0000000000..b8fd3d9c34
--- /dev/null
+++ b/.sqlx/query-b323ec9f47fdfc1afde86254d097b43c650e1937ea88b234ba2d1ff86f55f2e8.json
@@ -0,0 +1,17 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n INSERT INTO scheduled_action_event_run (action_id, event_id, configuration_revision, event_context)\n VALUES ($1, $2, $3, $4) ON CONFLICT (action_id, event_id) DO NOTHING\n ",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Uuid",
+ "Int8",
+ "Jsonb"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "b323ec9f47fdfc1afde86254d097b43c650e1937ea88b234ba2d1ff86f55f2e8"
+}
diff --git a/.sqlx/query-b3a0afcda1f0ab33f36dda316b1ac763c805d90083c1b0d91635729b74fb4b92.json b/.sqlx/query-b3a0afcda1f0ab33f36dda316b1ac763c805d90083c1b0d91635729b74fb4b92.json
new file mode 100644
index 0000000000..1e3904ee22
--- /dev/null
+++ b/.sqlx/query-b3a0afcda1f0ab33f36dda316b1ac763c805d90083c1b0d91635729b74fb4b92.json
@@ -0,0 +1,15 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "INSERT INTO macro_user (id, username, email, stripe_customer_id) VALUES ($1, $2, $2, $2) ON CONFLICT DO NOTHING",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Text"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "b3a0afcda1f0ab33f36dda316b1ac763c805d90083c1b0d91635729b74fb4b92"
+}
diff --git a/.sqlx/query-b5e7846dd013917d956ba81fa63ac5ced31845cdfcf3ac964086e2504d27da9c.json b/.sqlx/query-b5e7846dd013917d956ba81fa63ac5ced31845cdfcf3ac964086e2504d27da9c.json
new file mode 100644
index 0000000000..c03c2500e0
--- /dev/null
+++ b/.sqlx/query-b5e7846dd013917d956ba81fa63ac5ced31845cdfcf3ac964086e2504d27da9c.json
@@ -0,0 +1,22 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT id FROM scheduled_action WHERE id = $1 FOR UPDATE SKIP LOCKED",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "id",
+ "type_info": "Uuid"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ false
+ ]
+ },
+ "hash": "b5e7846dd013917d956ba81fa63ac5ced31845cdfcf3ac964086e2504d27da9c"
+}
diff --git a/.sqlx/query-ba729b8cb47ea097fd61d047b968b5de0a47e7955becb6183d2636fabf801429.json b/.sqlx/query-ba729b8cb47ea097fd61d047b968b5de0a47e7955becb6183d2636fabf801429.json
new file mode 100644
index 0000000000..baa382e597
--- /dev/null
+++ b/.sqlx/query-ba729b8cb47ea097fd61d047b968b5de0a47e7955becb6183d2636fabf801429.json
@@ -0,0 +1,113 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n SELECT id, owner, name, schedule, kind, timezone, task, claimed, created_at,\n updated_at, next_run_at, enabled, trigger_type, event_filters,\n configuration_revision, event_activated_at\n FROM scheduled_action\n WHERE id = $1 AND owner = $2\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "id",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 1,
+ "name": "owner",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 2,
+ "name": "name",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 3,
+ "name": "schedule",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 4,
+ "name": "kind",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 5,
+ "name": "timezone",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 6,
+ "name": "task",
+ "type_info": "Jsonb"
+ },
+ {
+ "ordinal": 7,
+ "name": "claimed",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 8,
+ "name": "created_at",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 9,
+ "name": "updated_at",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 10,
+ "name": "next_run_at",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 11,
+ "name": "enabled",
+ "type_info": "Bool"
+ },
+ {
+ "ordinal": 12,
+ "name": "trigger_type",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 13,
+ "name": "event_filters",
+ "type_info": "Jsonb"
+ },
+ {
+ "ordinal": 14,
+ "name": "configuration_revision",
+ "type_info": "Int8"
+ },
+ {
+ "ordinal": 15,
+ "name": "event_activated_at",
+ "type_info": "Timestamptz"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Text"
+ ]
+ },
+ "nullable": [
+ false,
+ false,
+ false,
+ true,
+ false,
+ true,
+ false,
+ true,
+ false,
+ false,
+ true,
+ false,
+ false,
+ true,
+ false,
+ true
+ ]
+ },
+ "hash": "ba729b8cb47ea097fd61d047b968b5de0a47e7955becb6183d2636fabf801429"
+}
diff --git a/.sqlx/query-bac7dcac5151e27205a782204dd3f6d0bc4e10250215f559adcccf0b1fd3a95d.json b/.sqlx/query-bac7dcac5151e27205a782204dd3f6d0bc4e10250215f559adcccf0b1fd3a95d.json
new file mode 100644
index 0000000000..e11c78e811
--- /dev/null
+++ b/.sqlx/query-bac7dcac5151e27205a782204dd3f6d0bc4e10250215f559adcccf0b1fd3a95d.json
@@ -0,0 +1,20 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT count(*) FROM scheduled_action_event_run",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "count",
+ "type_info": "Int8"
+ }
+ ],
+ "parameters": {
+ "Left": []
+ },
+ "nullable": [
+ null
+ ]
+ },
+ "hash": "bac7dcac5151e27205a782204dd3f6d0bc4e10250215f559adcccf0b1fd3a95d"
+}
diff --git a/.sqlx/query-bd66513846d48bd5d00e9bdbf94e276aad7c335492918b5013666d53e359bea2.json b/.sqlx/query-bd66513846d48bd5d00e9bdbf94e276aad7c335492918b5013666d53e359bea2.json
new file mode 100644
index 0000000000..7695e648f7
--- /dev/null
+++ b/.sqlx/query-bd66513846d48bd5d00e9bdbf94e276aad7c335492918b5013666d53e359bea2.json
@@ -0,0 +1,29 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n SELECT state, claim_token FROM scheduled_action_event_run\n WHERE action_id = $1 AND event_id = $2 FOR UPDATE\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "state",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 1,
+ "name": "claim_token",
+ "type_info": "Uuid"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ false,
+ true
+ ]
+ },
+ "hash": "bd66513846d48bd5d00e9bdbf94e276aad7c335492918b5013666d53e359bea2"
+}
diff --git a/.sqlx/query-c7d3ce19850ed84be244b079bffa60a027155c3181743ec171d51d0864a20d55.json b/.sqlx/query-c7d3ce19850ed84be244b079bffa60a027155c3181743ec171d51d0864a20d55.json
new file mode 100644
index 0000000000..02ba949697
--- /dev/null
+++ b/.sqlx/query-c7d3ce19850ed84be244b079bffa60a027155c3181743ec171d51d0864a20d55.json
@@ -0,0 +1,14 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "UPDATE scheduled_action SET claimed = now() WHERE id = $1",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "c7d3ce19850ed84be244b079bffa60a027155c3181743ec171d51d0864a20d55"
+}
diff --git a/.sqlx/query-cdc548895d477a0862934b40476b12321741fa0ad2ef33c5ce371727686788fa.json b/.sqlx/query-cdc548895d477a0862934b40476b12321741fa0ad2ef33c5ce371727686788fa.json
new file mode 100644
index 0000000000..e2b00ab71e
--- /dev/null
+++ b/.sqlx/query-cdc548895d477a0862934b40476b12321741fa0ad2ef33c5ce371727686788fa.json
@@ -0,0 +1,15 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n UPDATE scheduled_action\n SET claimed = NULL, claim_token = NULL, updated_at = now()\n WHERE id = $1 AND claim_token = $2\n ",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "cdc548895d477a0862934b40476b12321741fa0ad2ef33c5ce371727686788fa"
+}
diff --git a/.sqlx/query-d33f587b2e81db6c6d6c6db12629590b62c522e229149217f4106ff7ba16b101.json b/.sqlx/query-d33f587b2e81db6c6d6c6db12629590b62c522e229149217f4106ff7ba16b101.json
new file mode 100644
index 0000000000..f8f5535922
--- /dev/null
+++ b/.sqlx/query-d33f587b2e81db6c6d6c6db12629590b62c522e229149217f4106ff7ba16b101.json
@@ -0,0 +1,17 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n UPDATE scheduled_action\n SET claimed = $1, claim_token = $4, updated_at = now()\n WHERE id = $2\n AND (claimed IS NULL OR claimed < $3)\n ",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Timestamptz",
+ "Uuid",
+ "Timestamptz",
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "d33f587b2e81db6c6d6c6db12629590b62c522e229149217f4106ff7ba16b101"
+}
diff --git a/.sqlx/query-d3deeec48ec97b044da7805a496fe585d0e24ffd87eb8dfd102414d2254ad76e.json b/.sqlx/query-d3deeec48ec97b044da7805a496fe585d0e24ffd87eb8dfd102414d2254ad76e.json
new file mode 100644
index 0000000000..3940abb4f4
--- /dev/null
+++ b/.sqlx/query-d3deeec48ec97b044da7805a496fe585d0e24ffd87eb8dfd102414d2254ad76e.json
@@ -0,0 +1,22 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT id FROM scheduled_action WHERE id = $1 FOR UPDATE",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "id",
+ "type_info": "Uuid"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ false
+ ]
+ },
+ "hash": "d3deeec48ec97b044da7805a496fe585d0e24ffd87eb8dfd102414d2254ad76e"
+}
diff --git a/.sqlx/query-d7254f05e4df78081e857ad99eefdf8477002e1f87e23d8c0d4e5021b1fa0430.json b/.sqlx/query-d7254f05e4df78081e857ad99eefdf8477002e1f87e23d8c0d4e5021b1fa0430.json
new file mode 100644
index 0000000000..927d886266
--- /dev/null
+++ b/.sqlx/query-d7254f05e4df78081e857ad99eefdf8477002e1f87e23d8c0d4e5021b1fa0430.json
@@ -0,0 +1,15 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "UPDATE scheduled_action SET owner = $2 WHERE id = $1",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Text"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "d7254f05e4df78081e857ad99eefdf8477002e1f87e23d8c0d4e5021b1fa0430"
+}
diff --git a/.sqlx/query-dac4a76e5ffb0692966932c8e3a711b42af43bc937b89145d0046e1f508ba209.json b/.sqlx/query-dac4a76e5ffb0692966932c8e3a711b42af43bc937b89145d0046e1f508ba209.json
new file mode 100644
index 0000000000..259a1df871
--- /dev/null
+++ b/.sqlx/query-dac4a76e5ffb0692966932c8e3a711b42af43bc937b89145d0046e1f508ba209.json
@@ -0,0 +1,113 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n SELECT id, owner, name, schedule, kind, timezone, task, claimed, created_at,\n updated_at, next_run_at, enabled, trigger_type, event_filters,\n configuration_revision, event_activated_at\n FROM scheduled_action\n WHERE enabled AND trigger_type = 'cron'\n AND (claimed IS NULL OR claimed < $1)\n ORDER BY next_run_at ASC, id ASC\n LIMIT $2\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "id",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 1,
+ "name": "owner",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 2,
+ "name": "name",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 3,
+ "name": "schedule",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 4,
+ "name": "kind",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 5,
+ "name": "timezone",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 6,
+ "name": "task",
+ "type_info": "Jsonb"
+ },
+ {
+ "ordinal": 7,
+ "name": "claimed",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 8,
+ "name": "created_at",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 9,
+ "name": "updated_at",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 10,
+ "name": "next_run_at",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 11,
+ "name": "enabled",
+ "type_info": "Bool"
+ },
+ {
+ "ordinal": 12,
+ "name": "trigger_type",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 13,
+ "name": "event_filters",
+ "type_info": "Jsonb"
+ },
+ {
+ "ordinal": 14,
+ "name": "configuration_revision",
+ "type_info": "Int8"
+ },
+ {
+ "ordinal": 15,
+ "name": "event_activated_at",
+ "type_info": "Timestamptz"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Timestamptz",
+ "Int8"
+ ]
+ },
+ "nullable": [
+ false,
+ false,
+ false,
+ true,
+ false,
+ true,
+ false,
+ true,
+ false,
+ false,
+ true,
+ false,
+ false,
+ true,
+ false,
+ true
+ ]
+ },
+ "hash": "dac4a76e5ffb0692966932c8e3a711b42af43bc937b89145d0046e1f508ba209"
+}
diff --git a/.sqlx/query-de4f63dbbf1a6b451b4060195c385d3e810c63d8b993b6788f52975ed31794e8.json b/.sqlx/query-de4f63dbbf1a6b451b4060195c385d3e810c63d8b993b6788f52975ed31794e8.json
new file mode 100644
index 0000000000..5fbf3d5708
--- /dev/null
+++ b/.sqlx/query-de4f63dbbf1a6b451b4060195c385d3e810c63d8b993b6788f52975ed31794e8.json
@@ -0,0 +1,18 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n UPDATE scheduled_action_event_run\n SET state = 'finished', finished_at = $3, outcome = $4, execution_record_id = $5\n WHERE action_id = $1 AND event_id = $2\n ",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Uuid",
+ "Timestamptz",
+ "Jsonb",
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "de4f63dbbf1a6b451b4060195c385d3e810c63d8b993b6788f52975ed31794e8"
+}
diff --git a/.sqlx/query-e320109ff316b46278dbefa1039a37d9109a68551860e895e8d705017aca41e0.json b/.sqlx/query-e320109ff316b46278dbefa1039a37d9109a68551860e895e8d705017aca41e0.json
new file mode 100644
index 0000000000..cac4573e16
--- /dev/null
+++ b/.sqlx/query-e320109ff316b46278dbefa1039a37d9109a68551860e895e8d705017aca41e0.json
@@ -0,0 +1,112 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n SELECT id, owner, name, schedule, kind, timezone, task, claimed, created_at,\n updated_at, next_run_at, enabled, trigger_type, event_filters,\n configuration_revision, event_activated_at\n FROM scheduled_action\n WHERE owner = $1\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "id",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 1,
+ "name": "owner",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 2,
+ "name": "name",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 3,
+ "name": "schedule",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 4,
+ "name": "kind",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 5,
+ "name": "timezone",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 6,
+ "name": "task",
+ "type_info": "Jsonb"
+ },
+ {
+ "ordinal": 7,
+ "name": "claimed",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 8,
+ "name": "created_at",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 9,
+ "name": "updated_at",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 10,
+ "name": "next_run_at",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 11,
+ "name": "enabled",
+ "type_info": "Bool"
+ },
+ {
+ "ordinal": 12,
+ "name": "trigger_type",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 13,
+ "name": "event_filters",
+ "type_info": "Jsonb"
+ },
+ {
+ "ordinal": 14,
+ "name": "configuration_revision",
+ "type_info": "Int8"
+ },
+ {
+ "ordinal": 15,
+ "name": "event_activated_at",
+ "type_info": "Timestamptz"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Text"
+ ]
+ },
+ "nullable": [
+ false,
+ false,
+ false,
+ true,
+ false,
+ true,
+ false,
+ true,
+ false,
+ false,
+ true,
+ false,
+ false,
+ true,
+ false,
+ true
+ ]
+ },
+ "hash": "e320109ff316b46278dbefa1039a37d9109a68551860e895e8d705017aca41e0"
+}
diff --git a/.sqlx/query-e455ea2b92eaf6a6a7eaf70c459d6ba56f4190ab4490c753e41c31da4c5c4aa8.json b/.sqlx/query-e455ea2b92eaf6a6a7eaf70c459d6ba56f4190ab4490c753e41c31da4c5c4aa8.json
new file mode 100644
index 0000000000..bb34828a08
--- /dev/null
+++ b/.sqlx/query-e455ea2b92eaf6a6a7eaf70c459d6ba56f4190ab4490c753e41c31da4c5c4aa8.json
@@ -0,0 +1,20 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT count(*) FROM action_execution_record",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "count",
+ "type_info": "Int8"
+ }
+ ],
+ "parameters": {
+ "Left": []
+ },
+ "nullable": [
+ null
+ ]
+ },
+ "hash": "e455ea2b92eaf6a6a7eaf70c459d6ba56f4190ab4490c753e41c31da4c5c4aa8"
+}
diff --git a/.sqlx/query-e7582dfc1a5a859c0b372847c021ae5ebd9623624fefeec79196782a6f334eec.json b/.sqlx/query-e7582dfc1a5a859c0b372847c021ae5ebd9623624fefeec79196782a6f334eec.json
new file mode 100644
index 0000000000..8093418c67
--- /dev/null
+++ b/.sqlx/query-e7582dfc1a5a859c0b372847c021ae5ebd9623624fefeec79196782a6f334eec.json
@@ -0,0 +1,14 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "DELETE FROM scheduled_action WHERE id = $1",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "e7582dfc1a5a859c0b372847c021ae5ebd9623624fefeec79196782a6f334eec"
+}
diff --git a/.sqlx/query-ebba8e0b0f74d635c5c8a8e47c37b64ea486c0ba825bdd16b011472ea576827f.json b/.sqlx/query-ebba8e0b0f74d635c5c8a8e47c37b64ea486c0ba825bdd16b011472ea576827f.json
new file mode 100644
index 0000000000..b68e79c91d
--- /dev/null
+++ b/.sqlx/query-ebba8e0b0f74d635c5c8a8e47c37b64ea486c0ba825bdd16b011472ea576827f.json
@@ -0,0 +1,17 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n INSERT INTO scheduled_action\n (id, owner, name, kind, task, enabled, trigger_type, event_filters, event_activated_at)\n VALUES ($1, $2, 'event run test', 'Agent', '{}', true, 'events', $3, $4)\n ON CONFLICT DO NOTHING\n ",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Text",
+ "Jsonb",
+ "Timestamptz"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "ebba8e0b0f74d635c5c8a8e47c37b64ea486c0ba825bdd16b011472ea576827f"
+}
diff --git a/.sqlx/query-ebc6ea2316723aca58d21ffa1e8ce226a66a8beeb3adf5adace18422d905c716.json b/.sqlx/query-ebc6ea2316723aca58d21ffa1e8ce226a66a8beeb3adf5adace18422d905c716.json
new file mode 100644
index 0000000000..0959af9b34
--- /dev/null
+++ b/.sqlx/query-ebc6ea2316723aca58d21ffa1e8ce226a66a8beeb3adf5adace18422d905c716.json
@@ -0,0 +1,27 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n UPDATE scheduled_action_event_run r\n SET state = 'finished', finished_at = $3,\n outcome = CASE WHEN r.state = 'started' THEN $4::jsonb\n WHEN NOT a.enabled THEN $5::jsonb ELSE $6::jsonb END\n FROM scheduled_action a\n WHERE r.action_id = $1 AND r.event_id = $2 AND a.id = r.action_id\n AND ((r.state = 'started' AND r.deadline <= $3)\n OR (r.state = 'pending' AND (NOT a.enabled OR a.trigger_type <> 'events'\n OR a.configuration_revision <> r.configuration_revision)))\n RETURNING r.claim_token\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "claim_token",
+ "type_info": "Uuid"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Uuid",
+ "Timestamptz",
+ "Jsonb",
+ "Jsonb",
+ "Jsonb"
+ ]
+ },
+ "nullable": [
+ true
+ ]
+ },
+ "hash": "ebc6ea2316723aca58d21ffa1e8ce226a66a8beeb3adf5adace18422d905c716"
+}
diff --git a/.sqlx/query-f19e1ae90e534281d96af475a3b60f5fd4e8ffd5d34dc3e5bc012784c68726f2.json b/.sqlx/query-f19e1ae90e534281d96af475a3b60f5fd4e8ffd5d34dc3e5bc012784c68726f2.json
new file mode 100644
index 0000000000..fbfcc9d70b
--- /dev/null
+++ b/.sqlx/query-f19e1ae90e534281d96af475a3b60f5fd4e8ffd5d34dc3e5bc012784c68726f2.json
@@ -0,0 +1,22 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n INSERT INTO scheduled_action\n (id, owner, name, kind, task, enabled, trigger_type, schedule, timezone,\n next_run_at, event_filters, event_activated_at, configuration_revision)\n VALUES ($1, $2, 'invalid', 'Agent', '{}', true, $3, $4, $5, $6, $7, $8, $9)\n ",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Text",
+ "Text",
+ "Text",
+ "Text",
+ "Timestamptz",
+ "Jsonb",
+ "Timestamptz",
+ "Int8"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "f19e1ae90e534281d96af475a3b60f5fd4e8ffd5d34dc3e5bc012784c68726f2"
+}
diff --git a/.sqlx/query-f3f684621199cc45e6ab23c82bc63ad350584599dabef64862f38dfaab559cb4.json b/.sqlx/query-f3f684621199cc45e6ab23c82bc63ad350584599dabef64862f38dfaab559cb4.json
new file mode 100644
index 0000000000..7b8444520f
--- /dev/null
+++ b/.sqlx/query-f3f684621199cc45e6ab23c82bc63ad350584599dabef64862f38dfaab559cb4.json
@@ -0,0 +1,15 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n UPDATE scheduled_action SET claimed = NULL, claim_token = NULL, updated_at = now()\n WHERE id = $1 AND claim_token = $2\n ",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "f3f684621199cc45e6ab23c82bc63ad350584599dabef64862f38dfaab559cb4"
+}
diff --git a/.sqlx/query-fe74b0a1d240c9ca0472d60285189bb83374d4cad48ebcf2285729746544597e.json b/.sqlx/query-fe74b0a1d240c9ca0472d60285189bb83374d4cad48ebcf2285729746544597e.json
new file mode 100644
index 0000000000..faf05eaa25
--- /dev/null
+++ b/.sqlx/query-fe74b0a1d240c9ca0472d60285189bb83374d4cad48ebcf2285729746544597e.json
@@ -0,0 +1,34 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n SELECT schedule, timezone, trigger_type\n FROM scheduled_action\n WHERE id = $1\n FOR UPDATE\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "schedule",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 1,
+ "name": "timezone",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 2,
+ "name": "trigger_type",
+ "type_info": "Text"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ true,
+ true,
+ false
+ ]
+ },
+ "hash": "fe74b0a1d240c9ca0472d60285189bb83374d4cad48ebcf2285729746544597e"
+}
diff --git a/Cargo.lock b/Cargo.lock
index ed0446bbd8..8e59713f67 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -14823,6 +14823,7 @@ dependencies = [
name = "scheduled_action"
version = "0.1.0"
dependencies = [
+ "activity",
"agent",
"ai_tools",
"ai_toolset",
@@ -14831,15 +14832,20 @@ dependencies = [
"aws-sdk-secretsmanager",
"aws-sdk-sqs",
"axum",
+ "channel_sender",
+ "channels",
"chat",
"chrono",
"chrono-tz",
"connection_gateway_client",
"cron",
"database_env_vars",
+ "documents",
"doppler-config",
+ "entity_access",
"entity_registry_db_utils",
"futures",
+ "kafka_util",
"macro_auth",
"macro_authorization",
"macro_aws_config",
@@ -14850,6 +14856,8 @@ dependencies = [
"macro_entrypoint",
"macro_env",
"macro_env_var",
+ "macro_event_broker",
+ "macro_event_topics",
"macro_queues",
"macro_service_urls",
"macro_user_id",
@@ -14861,11 +14869,14 @@ dependencies = [
"model_owner",
"models_permissions",
"notification",
+ "rdkafka",
+ "rootcause",
"secretsmanager_client",
"serde",
"serde_json",
"share_permission_db_utils",
"sqlx",
+ "thiserror 2.0.18",
"tokio",
"tokio-util",
"tower 0.5.3",
diff --git a/apps/web/src/features/block-automation/component/Automation.test.tsx b/apps/web/src/features/block-automation/component/Automation.test.tsx
new file mode 100644
index 0000000000..a9dbd94fd9
--- /dev/null
+++ b/apps/web/src/features/block-automation/component/Automation.test.tsx
@@ -0,0 +1,311 @@
+import type { ScheduledAction } from '@service-scheduled-action/generated/schemas';
+import { cleanup, fireEvent, render, screen } from '@solidjs/testing-library';
+import { createSignal, type JSX, type Setter } from 'solid-js';
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+import { Automation } from './Automation';
+
+const mocks = vi.hoisted(() => ({
+ readSchedules: (): ScheduledAction[] => [],
+ status: (): string => 'success',
+ update: vi.fn(),
+ create: vi.fn(),
+ run: vi.fn(),
+ openWithSplit: vi.fn(),
+ setDisplayName: vi.fn(),
+ changePrompt: (_value: string): void => {},
+ rename: (_value: string): void => {},
+ duplicate: (): void => {},
+}));
+vi.mock('@core/block', () => ({ useBlockId: () => 'routine-id' }));
+vi.mock('@core/component/AI/constant', () => ({
+ DEFAULT_MODEL: 'claude-sonnet-4-6',
+}));
+vi.mock('@core/constant/allBlocks', () => ({
+ blockNameToDefaultFile: () => 'New automation',
+}));
+vi.mock('@core/component/EntityIcon', () => ({ EntityIcon: () => null }));
+vi.mock('@core/component/Toast/Toast', () => ({
+ toast: { alert: vi.fn(), success: vi.fn() },
+}));
+vi.mock('@entity', () => ({ formatDateAndTime: (value: string) => value }));
+vi.mock('@app/features/entity/bulk-edit/BulkEditEntityModal', () => ({
+ openBulkEditModal: vi.fn(),
+}));
+vi.mock('@components/app/split-layout/layout', () => ({
+ useSplitLayout: () => ({
+ openWithSplit: mocks.openWithSplit,
+ replaceOrInsertSplit: vi.fn(),
+ }),
+}));
+vi.mock('@components/app/split-layout/layoutUtils', () => ({
+ useSplitPanelOrThrow: () => ({
+ handle: { setDisplayName: mocks.setDisplayName },
+ setTitleFileMenuRef: vi.fn(),
+ }),
+ returnSplitToRecentListView: vi.fn(),
+}));
+vi.mock('@components/app/split-layout/components/HeaderIsland', () => ({
+ HeaderIsland: (props: { children: JSX.Element }) => props.children,
+}));
+vi.mock('@components/app/split-layout/components/SplitHeader', () => ({
+ SplitHeaderLeft: (props: { children: JSX.Element }) => props.children,
+}));
+vi.mock('@components/app/split-layout/components/SplitLabel', () => ({
+ SplitTitleFileMenu: (props: { children: JSX.Element }) => props.children,
+}));
+vi.mock('@components/app/split-layout/components/SplitFileMenu', () => ({
+ BlockSplitFileMenu: (props: {
+ tools: { label: string; action: () => void }[];
+ }) => {
+ mocks.duplicate = props.tools.find(
+ (tool) => tool.label === 'Duplicate'
+ )!.action;
+ return <button onClick={mocks.duplicate}>Duplicate</button>;
+ },
+}));
+vi.mock('./AutomationRenameModal', () => ({
+ AutomationRenameModal: (props: { onRename: (value: string) => void }) => {
+ mocks.rename = props.onRename;
+ return null;
+ },
+}));
+vi.mock('./AutomationPromptEditor', () => ({
+ AutomationPromptEditor: (props: {
+ initialValue: string;
+ onChange: (value: string) => void;
+ }) => {
+ mocks.changePrompt = props.onChange;
+ return (
+ <textarea
+ aria-label="Instructions"
+ value={props.initialValue}
+ onInput={(event) => props.onChange(event.currentTarget.value)}
+ />
+ );
+ },
+}));
+vi.mock('./AutomationTimePicker', () => ({ AutomationTimePicker: () => null }));
+vi.mock('@ui', () => ({
+ Button: (props: JSX.ButtonHTMLAttributes<HTMLButtonElement>) => (
+ <button {...props} />
+ ),
+ cn: (...classes: string[]) => classes.join(' '),
+}));
+vi.mock('@queries/chat', () => ({
+ useChatQuery: () => ({ data: { chat: { name: 'Run transcript' } } }),
+}));
+vi.mock('@queries/agent-schedule/schedules', () => ({
+ useSchedulesQuery: () => ({
+ get data() {
+ return mocks.readSchedules();
+ },
+ get isSuccess() {
+ return mocks.status() === 'success';
+ },
+ get isPending() {
+ return mocks.status() === 'pending';
+ },
+ get isError() {
+ return mocks.status() === 'error';
+ },
+ get error() {
+ return mocks.status() === 'error' ? new Error('Unavailable') : null;
+ },
+ }),
+ useScheduleHistoryQuery: () => ({
+ isSuccess: true,
+ isPending: false,
+ data: [
+ {
+ id: 'run-id',
+ resource_id: 'chat-id',
+ start_time: '2026-09-22T12:00:00Z',
+ is_success: true,
+ },
+ ],
+ }),
+ useUpdateScheduleMutation: () => ({ mutate: mocks.update }),
+ useCreateScheduleMutation: () => ({ mutate: mocks.create, isPending: false }),
+ useRunScheduleNowMutation: () => ({ mutate: mocks.run, isPending: false }),
+ invalidateSchedules: vi.fn(),
+}));
+
+const cron: ScheduledAction = {
+ id: 'routine-id',
+ owner: 'macro|[email protected]',
+ name: 'Summary',
+ kind: 'Agent',
+ trigger: {
+ type: 'cron',
+ schedule: '0 0 9 * * 2',
+ timezone: 'America/New_York',
+ },
+ task: {
+ model: 'claude-sonnet-4-6',
+ user_prompt: 'Summarize updates',
+ prompt: '',
+ },
+ enabled: true,
+ configuration_revision: 1,
+ created_at: '2026-09-22T12:00:00Z',
+ updated_at: '2026-09-22T12:00:00Z',
+ next_run_at: '2026-09-28T09:00:00Z',
+};
+const events: ScheduledAction = {
+ ...cron,
+ next_run_at: null,
+ trigger: { type: 'events', filters: [{ events: ['document.updated'] }] },
+};
+let setSchedules: Setter<ScheduledAction[]>;
+let setStatus: Setter<string>;
+beforeEach(() => {
+ vi.clearAllMocks();
+ vi.useFakeTimers();
+ [mocks.status, setStatus] = createSignal('success');
+ [mocks.readSchedules, setSchedules] = createSignal([cron]);
+});
+afterEach(() => {
+ cleanup();
+ vi.useRealTimers();
+});
+
+describe('automation editor trigger guards', () => {
+ it('shows an explicit backend-managed state on a direct event route', async () => {
+ setSchedules([events]);
+ render(() => <Automation />);
+ expect(screen.getByText('Backend-managed routine')).toBeTruthy();
+ expect(
+ screen.getByText(/Manage this routine through the API/)
+ ).toBeTruthy();
+ expect(screen.queryByText('Loading…')).toBeNull();
+ expect(screen.queryByRole('textbox')).toBeNull();
+ expect(screen.queryByText('Duplicate')).toBeNull();
+ expect(screen.queryByText('Run Now')).toBeNull();
+ await vi.advanceTimersByTimeAsync(500);
+ expect(mocks.update).not.toHaveBeenCalled();
+ expect(mocks.create).not.toHaveBeenCalled();
+ });
+
+ it('retains cron editing, duplication, run-now, and history navigation', async () => {
+ render(() => <Automation />);
+ expect(screen.getByText(/America\/New_York/)).toBeTruthy();
+ fireEvent.input(screen.getByRole('textbox', { name: 'Instructions' }), {
+ target: { value: 'New instructions' },
+ });
+ await vi.advanceTimersByTimeAsync(300);
+ expect(mocks.update).toHaveBeenCalledExactlyOnceWith({
+ scheduleId: 'routine-id',
+ body: {
+ name: 'Summary',
+ kind: 'Agent',
+ enabled: true,
+ trigger: cron.trigger,
+ task: {
+ model: 'claude-sonnet-4-6',
+ user_prompt: 'New instructions',
+ prompt: '',
+ },
+ },
+ });
+ fireEvent.click(screen.getByText('Duplicate'));
+ expect(mocks.create).toHaveBeenCalledExactlyOnceWith({
+ name: 'Summary copy',
+ kind: 'Agent',
+ enabled: true,
+ task: cron.task,
+ trigger: cron.trigger,
+ });
+ fireEvent.click(screen.getByText('Run Now'));
+ expect(mocks.run).toHaveBeenCalledExactlyOnceWith({
+ scheduleId: 'routine-id',
+ });
+ fireEvent.click(screen.getByText('Run transcript'));
+ expect(mocks.openWithSplit).toHaveBeenCalledWith(
+ { type: 'chat', id: 'chat-id' },
+ { activate: true, preferNewSplit: false }
+ );
+ });
+
+ it('blocks queued autosave and stale edit/duplicate callbacks after a trigger changes to events', async () => {
+ render(() => <Automation />);
+ mocks.changePrompt('Queued edit');
+ setSchedules([events]);
+ expect(screen.getByText('Backend-managed routine')).toBeTruthy();
+ mocks.rename('Stale rename');
+ mocks.changePrompt('Stale prompt');
+ mocks.duplicate();
+ await vi.advanceTimersByTimeAsync(500);
+ expect(mocks.update).not.toHaveBeenCalled();
+ expect(mocks.create).not.toHaveBeenCalled();
+ expect(mocks.setDisplayName).not.toHaveBeenCalledWith('Stale rename');
+ });
+
+ it('cancels pending saves when the editor unmounts', async () => {
+ const { unmount } = render(() => <Automation />);
+ mocks.changePrompt('Unsaved');
+ unmount();
+ await vi.advanceTimersByTimeAsync(500);
+ expect(mocks.update).not.toHaveBeenCalled();
+ });
+
+ it('does not read pending resource data', () => {
+ setStatus('pending');
+ mocks.readSchedules = () => {
+ throw new Error('Pending data read');
+ };
+ render(() => <Automation />);
+ expect(screen.getByText('Loading…')).toBeTruthy();
+ });
+
+ it('renders a load error rather than spinning forever', () => {
+ setStatus('error');
+ setSchedules([]);
+ render(() => <Automation />);
+ expect(
+ screen.getByText('Unable to load automation. Please try again.')
+ ).toBeTruthy();
+ });
+
+ it('preserves the cron editor and queued save after a background refetch error', async () => {
+ render(() => <Automation />);
+ const editor = screen.getByRole('textbox', { name: 'Instructions' });
+ fireEvent.input(editor, { target: { value: 'Queued during refetch' } });
+ setStatus('error');
+ expect(screen.getByRole('textbox', { name: 'Instructions' })).toBe(editor);
+ expect(screen.queryByText(/Unable to load automation/)).toBeNull();
+ await vi.advanceTimersByTimeAsync(300);
+ expect(mocks.update).toHaveBeenCalledExactlyOnceWith({
+ scheduleId: 'routine-id',
+ body: expect.objectContaining({
+ trigger: cron.trigger,
+ task: expect.objectContaining({ user_prompt: 'Queued during refetch' }),
+ }),
+ });
+ });
+
+ it('initializes the editor from cached data even when mounted after a refetch error', () => {
+ setStatus('error');
+ render(() => <Automation />);
+ expect(screen.getByRole('textbox', { name: 'Instructions' })).toBeTruthy();
+ });
+
+ it('keeps cached event routines backend-managed after a refetch error', async () => {
+ render(() => <Automation />);
+ mocks.changePrompt('Queued edit');
+ setSchedules([events]);
+ setStatus('error');
+ expect(screen.getByText('Backend-managed routine')).toBeTruthy();
+ expect(screen.queryByRole('textbox')).toBeNull();
+ mocks.rename('Stale rename');
+ mocks.duplicate();
+ await vi.advanceTimersByTimeAsync(500);
+ expect(mocks.update).not.toHaveBeenCalled();
+ expect(mocks.create).not.toHaveBeenCalled();
+ });
+
+ it('distinguishes a missing action from a backend-managed event', () => {
+ setSchedules([]);
+ render(() => <Automation />);
+ expect(screen.getByText('Automation not found.')).toBeTruthy();
+ });
+});
diff --git a/apps/web/src/features/block-automation/component/Automation.tsx b/apps/web/src/features/block-automation/component/Automation.tsx
index e43b5bdbf7..e53dd4ae53 100644
--- a/apps/web/src/features/block-automation/component/Automation.tsx
+++ b/apps/web/src/features/block-automation/component/Automation.tsx
@@ -26,10 +26,19 @@ import {
useSchedulesQuery,
useUpdateScheduleMutation,
} from '@queries/agent-schedule/schedules';
+import { getCronTrigger } from '@queries/agent-schedule/triggers';
import { useChatQuery } from '@queries/chat';
import { debounce } from '@solid-primitives/scheduled';
import { Button, cn } from '@ui';
-import { createMemo, createSignal, For, onMount, Show } from 'solid-js';
+import {
+ createMemo,
+ createSignal,
+ For,
+ Match,
+ onMount,
+ Show,
+ Switch,
+} from 'solid-js';
import { AutomationPromptEditor } from './AutomationPromptEditor';
import { AutomationRenameModal } from './AutomationRenameModal';
import { AutomationTimePicker } from './AutomationTimePicker';
@@ -42,6 +51,7 @@ import {
getErrorMessage,
INPUT_CLASS,
isValidTime,
+ scheduleToDuplicateBody,
WEEKDAY_OPTIONS,
} from './automationUtils';
import type { ScheduleDraft } from './types';
@@ -135,8 +145,14 @@ export function Automation() {
const schedulesQuery = useSchedulesQuery(() => true);
const schedule = createMemo(() =>
- schedulesQuery.data?.find((item) => item.id === scheduleId)
+ !schedulesQuery.isPending
+ ? schedulesQuery.data?.find((item) => item.id === scheduleId)
+ : undefined
);
+ const cronTrigger = () => {
+ const current = schedule();
+ return current ? getCronTrigger(current) : undefined;
+ };
const scheduleEntity = () => {
const current = schedule();
return current ? scheduleToEntity(current) : undefined;
@@ -147,7 +163,7 @@ export function Automation() {
const currentSummary = createMemo(() => {
const d = state();
if (!d) return '';
- return describeSchedule(d, getDefaultTimezone());
+ return describeSchedule(d, cronTrigger()?.timezone ?? getDefaultTimezone());
});
const formError = createMemo(() => {
@@ -179,17 +195,16 @@ export function Automation() {
const d = state();
const previous = schedule();
if (!d || !previous) return;
- updateMutation.mutate({
- scheduleId,
- body: draftToUpdateBody(d, previous),
- });
+ const body = draftToUpdateBody(d, previous);
+ if (!body) return;
+ updateMutation.mutate({ scheduleId, body });
};
const debouncedSave = debounce(save, 300);
const setState = (update: (prev: ScheduleDraft) => ScheduleDraft) => {
const current = state();
- if (!current) return;
+ if (!current || !cronTrigger()) return;
const next = update(current);
setRawState(next);
if (next.name !== current.name) {
@@ -198,18 +213,22 @@ export function Automation() {
debouncedSave();
};
- whenSettled(schedulesQuery, () => {
+ function initializeDraft(): void {
const current = schedule();
if (!current) return;
setRawState(draftFromSchedule(current));
panel.handle.setDisplayName(current.name);
- });
+ }
+
+ whenSettled(schedulesQuery, initializeDraft, initializeDraft);
const historyQuery = useScheduleHistoryQuery(
() => scheduleId,
- () => true
+ () => Boolean(cronTrigger())
+ );
+ const history = createMemo(() =>
+ historyQuery.isSuccess ? (historyQuery.data ?? []) : []
);
- const history = createMemo(() => historyQuery.data ?? []);
const [renameOpen, setRenameOpen] = createSignal(false);
@@ -237,14 +256,9 @@ export function Automation() {
const duplicateAutomation = () => {
const current = schedule();
if (!current || duplicateMutation.isPending) return;
- duplicateMutation.mutate({
- enabled: current.enabled,
- kind: current.kind,
- name: `${current.name} copy`,
- schedule: current.schedule,
- task: current.task,
- timezone: current.timezone,
- });
+ const body = scheduleToDuplicateBody(current);
+ if (!body) return;
+ duplicateMutation.mutate(body);
};
// Confirms via the shared bulk-delete modal, which routes automations to
@@ -276,25 +290,29 @@ export function Automation() {
});
onMount(() => {
- invalidateSchedules();
+ void invalidateSchedules();
});
return (
<Show
- when={state()}
+ when={cronTrigger() && state()}
fallback={
- <Show
- when={!schedulesQuery.isPending && !schedule()}
- fallback={
- <div class="flex size-full items-center justify-center text-xs text-ink-muted">
- Loading…
- </div>
- }
- >
- <div class="flex size-full items-center justify-center text-xs text-ink-muted">
- Automation not found.
- </div>
- </Show>
+ <div class="flex size-full flex-col items-center justify-center gap-2 p-3 text-center text-sm text-ink-muted">
+ <Switch fallback={<>Automation not found.</>}>
+ <Match when={schedulesQuery.isError && !schedule()}>
+ Unable to load automation. Please try again.
+ </Match>
+ <Match when={schedulesQuery.isPending}>Loading…</Match>
+ <Match when={schedule() && !cronTrigger()}>
+ <h1 class="font-semibold text-ink">Backend-managed routine</h1>
+ <p>
+ Event-triggered routines cannot be edited or duplicated here.
+ Manage this routine through the API. This editor only supports
+ cron schedules.
+ </p>
+ </Match>
+ </Switch>
+ </div>
}
>
{(d) => (
diff --git a/apps/web/src/features/block-automation/component/automationUtils.test.ts b/apps/web/src/features/block-automation/component/automationUtils.test.ts
new file mode 100644
index 0000000000..c2fb3c79ae
--- /dev/null
+++ b/apps/web/src/features/block-automation/component/automationUtils.test.ts
@@ -0,0 +1,128 @@
+import type { ScheduledAction } from '@service-scheduled-action/generated/schemas';
+import { describe, expect, it, vi } from 'vitest';
+import {
+ createEmptyDraft,
+ draftFromSchedule,
+ draftToCreateBody,
+ draftToUpdateBody,
+ scheduleToDuplicateBody,
+} from './automationUtils';
+
+vi.mock('@core/component/AI/constant', () => ({
+ DEFAULT_MODEL: 'claude-sonnet-4-6',
+}));
+vi.mock('@core/constant/allBlocks', () => ({
+ blockNameToDefaultFile: () => 'New automation',
+}));
+
+const cron: ScheduledAction = {
+ id: 'cron-id',
+ owner: 'macro|[email protected]',
+ name: 'Weekly summary',
+ kind: 'Agent',
+ trigger: {
+ type: 'cron',
+ schedule: '0 30 10 * * 2,4',
+ timezone: 'America/New_York',
+ },
+ task: {
+ model: 'claude-sonnet-4-6',
+ user_prompt: 'Summarize updates',
+ prompt: '',
+ },
+ enabled: true,
+ created_at: '2026-09-22T12:00:00Z',
+ updated_at: '2026-09-22T12:00:00Z',
+ configuration_revision: 1,
+};
+const events: ScheduledAction = {
+ ...cron,
+ trigger: { type: 'events', filters: [{ events: ['document.updated'] }] },
+};
+
+// Cached responses from before tagged triggers were introduced.
+const { trigger: _trigger, ...common } = cron;
+const legacy = {
+ ...common,
+ schedule: '0 30 10 * * 2,4',
+ timezone: 'America/New_York',
+} as unknown as ScheduledAction;
+
+function draft() {
+ return {
+ ...createEmptyDraft(),
+ name: ' Summary ',
+ prompt: ' Summarize updates ',
+ };
+}
+
+describe('cron automation payloads', () => {
+ it('creates a canonical trigger without legacy fields', () => {
+ const body = draftToCreateBody(draft());
+ expect(body).toMatchObject({
+ name: 'Summary',
+ trigger: { type: 'cron', schedule: '0 0 9 * * 2,3,4,5,6' },
+ task: { user_prompt: 'Summarize updates' },
+ });
+ expect(body).not.toHaveProperty('schedule');
+ expect(body).not.toHaveProperty('timezone');
+ });
+
+ it.each([cron, legacy])(
+ 'loads and updates cron while preserving its timezone',
+ (action) => {
+ expect(draftFromSchedule(action)).toMatchObject({
+ time: '10:30',
+ daysOfWeek: ['2', '4'],
+ prompt: 'Summarize updates',
+ });
+ const body = draftToUpdateBody(draft(), action);
+ expect(body).toMatchObject({
+ trigger: { type: 'cron', timezone: 'America/New_York' },
+ });
+ expect(body).not.toHaveProperty('schedule');
+ expect(body).not.toHaveProperty('timezone');
+ }
+ );
+
+ it.each([cron, legacy])(
+ 'duplicates cron without changing its expression or task',
+ (action) => {
+ expect(scheduleToDuplicateBody(action)).toEqual({
+ name: 'Weekly summary copy',
+ enabled: true,
+ kind: 'Agent',
+ task: cron.task,
+ trigger: cron.trigger,
+ });
+ }
+ );
+
+ it('duplicates API-written cron expressions losslessly', () => {
+ const trigger = {
+ type: 'cron' as const,
+ schedule: '0 */15 * * * * 2027',
+ timezone: 'UTC',
+ };
+ expect(scheduleToDuplicateBody({ ...cron, trigger })).toMatchObject({
+ trigger,
+ });
+ });
+
+ it.each([events, { ...events, schedule: '0 0 9 * * 2', timezone: 'UTC' }])(
+ 'never parses, updates, or duplicates an event trigger, even with stale legacy fields',
+ (action) => {
+ expect(draftFromSchedule(action)).toBeUndefined();
+ expect(draftToUpdateBody(draft(), action)).toBeUndefined();
+ expect(scheduleToDuplicateBody(action)).toBeUndefined();
+ }
+ );
+
+ it('prefers canonical cron fields over deprecated aliases', () => {
+ const action = { ...cron, schedule: '0 0 1 * * 1', timezone: 'UTC' };
+ expect(draftFromSchedule(action)?.time).toBe('10:30');
+ expect(scheduleToDuplicateBody(action)).toMatchObject({
+ trigger: cron.trigger,
+ });
+ });
+});
diff --git a/apps/web/src/features/block-automation/component/automationUtils.ts b/apps/web/src/features/block-automation/component/automationUtils.ts
index f1cb1c8c00..6433fea350 100644
--- a/apps/web/src/features/block-automation/component/automationUtils.ts
+++ b/apps/web/src/features/block-automation/component/automationUtils.ts
@@ -11,6 +11,7 @@ import {
parseCron as parseCronParts,
} from '@core/util/cron';
import { ThrownResultError } from '@core/util/result';
+import { getCronTrigger } from '@queries/agent-schedule/triggers';
import type {
AgentTask,
CreateScheduledAction,
@@ -104,8 +105,12 @@ function getAgentTask(schedule: ScheduledAction): AgentTask {
return schedule.task as unknown as AgentTask;
}
-export function draftFromSchedule(schedule: ScheduledAction): ScheduleDraft {
- const parsed = parseCron(schedule.schedule);
+export function draftFromSchedule(
+ schedule: ScheduledAction
+): ScheduleDraft | undefined {
+ const trigger = getCronTrigger(schedule);
+ if (!trigger) return undefined;
+ const parsed = parseCron(trigger.schedule);
const task = getAgentTask(schedule);
return {
@@ -132,9 +137,12 @@ function buildAgentTask(draft: ScheduleDraft): AgentTask {
export function draftToCreateBody(draft: ScheduleDraft): CreateScheduledAction {
return {
name: draft.name.trim() || deriveScheduleName(draft.prompt),
- schedule: buildCron(draft),
+ trigger: {
+ type: 'cron',
+ schedule: buildCron(draft),
+ timezone: getDefaultTimezone(),
+ },
kind: 'Agent',
- timezone: getDefaultTimezone(),
task: buildAgentTask(draft) as unknown as CreateScheduledAction['task'],
enabled: draft.enabled,
};
@@ -143,17 +151,32 @@ export function draftToCreateBody(draft: ScheduleDraft): CreateScheduledAction {
export function draftToUpdateBody(
draft: ScheduleDraft,
previous: ScheduledAction
-): UpdateScheduledAction {
+): UpdateScheduledAction | undefined {
+ const trigger = getCronTrigger(previous);
+ if (!trigger) return undefined;
return {
name: draft.name.trim() || deriveScheduleName(draft.prompt),
- schedule: buildCron(draft),
+ trigger: { ...trigger, schedule: buildCron(draft) },
kind: 'Agent',
- timezone: previous.timezone || getDefaultTimezone(),
task: buildAgentTask(draft) as unknown as UpdateScheduledAction['task'],
enabled: draft.enabled,
};
}
+export function scheduleToDuplicateBody(
+ schedule: ScheduledAction
+): CreateScheduledAction | undefined {
+ const trigger = getCronTrigger(schedule);
+ if (!trigger) return undefined;
+ return {
+ enabled: schedule.enabled,
+ kind: schedule.kind,
+ name: `${schedule.name} copy`,
+ trigger,
+ task: schedule.task,
+ };
+}
+
export function formatDateTime(value: string | null | undefined) {
if (!value) return 'Never';
diff --git a/apps/web/src/lib/queries/agent-schedule/entities.query.test.tsx b/apps/web/src/lib/queries/agent-schedule/entities.query.test.tsx
new file mode 100644
index 0000000000..5c0056eaa7
--- /dev/null
+++ b/apps/web/src/lib/queries/agent-schedule/entities.query.test.tsx
@@ -0,0 +1,107 @@
+import type { ScheduledAction } from '@service-scheduled-action/generated/schemas';
+import { cleanup, render, screen, waitFor } from '@solidjs/testing-library';
+import { QueryClient, type UseQueryResult } from '@tanstack/solid-query';
+import { type JSX, Suspense } from 'solid-js';
+import { afterEach, describe, expect, it, vi } from 'vitest';
+import { useAutomationEntities } from './entities';
+
+const mocks = vi.hoisted(() => ({
+ query: undefined as UseQueryResult<ScheduledAction[]> | undefined,
+ client: undefined as QueryClient | undefined,
+ fetch: vi.fn<() => Promise<ScheduledAction[]>>(),
+}));
+vi.mock('./schedules', async () => {
+ const { useQuery } = await import('@tanstack/solid-query');
+ return {
+ useSchedulesQuery: () => {
+ mocks.query = useQuery(
+ () => ({
+ queryKey: ['schedules'],
+ queryFn: mocks.fetch,
+ retry: false,
+ reconcile: 'id',
+ placeholderData: (previous: ScheduledAction[] | undefined) =>
+ previous,
+ }),
+ () => mocks.client!
+ );
+ return mocks.query;
+ },
+ };
+});
+
+function Entities(): JSX.Element {
+ const entities = useAutomationEntities();
+ return (
+ <div data-testid="entities">
+ {entities()
+ .map((entity) => entity.name)
+ .join(',')}
+ </div>
+ );
+}
+
+function mount(): void {
+ mocks.client = new QueryClient();
+ render(() => (
+ <Suspense fallback={<div>Suspended</div>}>
+ <Entities />
+ </Suspense>
+ ));
+}
+
+afterEach(() => {
+ cleanup();
+ mocks.client?.clear();
+ vi.resetAllMocks();
+});
+
+describe('installed Solid Query resource behavior', () => {
+ it('does not suspend pending entities and reads undefined safely after an initial error', async () => {
+ let reject!: (error: Error) => void;
+ mocks.fetch.mockReturnValue(
+ new Promise((_, fail) => {
+ reject = fail;
+ })
+ );
+ mount();
+ expect(mocks.query?.isPending).toBe(true);
+ expect(screen.getByTestId('entities').textContent).toBe('');
+ expect(screen.queryByText('Suspended')).toBeNull();
+ reject(new Error('Unavailable'));
+ await waitFor(() => expect(mocks.query?.isError).toBe(true));
+ expect(mocks.query?.data).toBeUndefined();
+ expect(screen.getByTestId('entities').textContent).toBe('');
+ });
+
+ it('retains actual cached data and entity nodes after a failed background refetch', async () => {
+ const action: ScheduledAction = {
+ id: 'cron',
+ name: 'Cached cron',
+ owner: 'macro|[email protected]',
+ kind: 'Agent',
+ trigger: { type: 'cron', schedule: '0 0 9 * * 2', timezone: 'UTC' },
+ task: {},
+ enabled: true,
+ configuration_revision: 1,
+ created_at: '2026-09-22T12:00:00Z',
+ updated_at: '2026-09-22T12:00:00Z',
+ next_run_at: null,
+ };
+ mocks.fetch
+ .mockResolvedValueOnce([action])
+ .mockRejectedValue(new Error('Unavailable'));
+ mount();
+ await waitFor(() => expect(mocks.query?.isSuccess).toBe(true));
+ const node = screen.getByTestId('entities');
+ expect(node.textContent).toBe('Cached cron');
+ await mocks.query!.refetch();
+ await waitFor(() => expect(mocks.query?.isRefetchError).toBe(true));
+ expect(mocks.query?.isSuccess).toBe(false);
+ expect(mocks.query?.isPending).toBe(false);
+ expect(mocks.query?.data).toEqual([action]);
+ expect(screen.getByTestId('entities')).toBe(node);
+ expect(node.textContent).toBe('Cached cron');
+ expect(screen.queryByText('Suspended')).toBeNull();
+ });
+});
diff --git a/apps/web/src/lib/queries/agent-schedule/entities.test.ts b/apps/web/src/lib/queries/agent-schedule/entities.test.ts
new file mode 100644
index 0000000000..c618fc6ef0
--- /dev/null
+++ b/apps/web/src/lib/queries/agent-schedule/entities.test.ts
@@ -0,0 +1,122 @@
+import type { ScheduledAction } from '@service-scheduled-action/generated/schemas';
+import { createRoot } from 'solid-js';
+import { describe, expect, it, vi } from 'vitest';
+import { scheduleToEntity, useAutomationEntities } from './entities';
+import { getCronTrigger } from './triggers';
+
+const query = vi.hoisted(() => ({
+ isSuccess: true,
+ isPending: false,
+ items: [] as ScheduledAction[],
+ get data() {
+ return this.items;
+ },
+}));
+vi.mock('./schedules', () => ({ useSchedulesQuery: () => query }));
+
+const cron: ScheduledAction = {
+ id: 'cron-id',
+ owner: 'macro|[email protected]',
+ name: 'Summary',
+ kind: 'Agent',
+ trigger: { type: 'cron', schedule: '0 0 9 * * 2', timezone: 'UTC' },
+ task: {},
+ enabled: true,
+ configuration_revision: 1,
+ created_at: '2026-09-22T12:00:00Z',
+ updated_at: '2026-09-22T12:00:00Z',
+ next_run_at: '2026-09-28T09:00:00Z',
+};
+const events: ScheduledAction = {
+ ...cron,
+ id: 'event-id',
+ next_run_at: null,
+ trigger: { type: 'events', filters: [{ events: ['document.updated'] }] },
+};
+
+describe('cron-only automation entities', () => {
+ it('converts canonical cron actions and preserves run state', () => {
+ expect(
+ scheduleToEntity({ ...cron, claimed: new Date().toISOString() })
+ ).toMatchObject({
+ id: 'cron-id',
+ type: 'automation',
+ cron: '0 0 9 * * 2',
+ nextRunAt: cron.next_run_at,
+ isRunning: true,
+ });
+ expect(
+ scheduleToEntity({ ...cron, claimed: '2020-01-01T00:00:00Z' })?.isRunning
+ ).toBe(false);
+ });
+
+ it('omits events and actions without an id', () => {
+ expect(scheduleToEntity(events)).toBeUndefined();
+ expect(scheduleToEntity({ ...cron, id: null })).toBeUndefined();
+ expect(
+ scheduleToEntity({
+ ...events,
+ schedule: '0 0 9 * * 2',
+ timezone: 'UTC',
+ } as ScheduledAction)
+ ).toBeUndefined();
+ });
+
+ it('accepts legacy cached cron actions through the compatibility helper', () => {
+ const { trigger: _trigger, ...common } = cron;
+ const legacy = { ...common, schedule: '0 0 9 * * 2', timezone: 'UTC' };
+ expect(getCronTrigger(legacy)).toEqual(cron.trigger);
+ expect(scheduleToEntity(legacy as unknown as ScheduledAction)?.cron).toBe(
+ legacy.schedule
+ );
+ expect(getCronTrigger({ schedule: legacy.schedule })).toBeUndefined();
+ expect(getCronTrigger({ timezone: 'UTC' })).toBeUndefined();
+ expect(getCronTrigger({ schedule: null, timezone: null })).toBeUndefined();
+ });
+
+ it('filters events from mixed API/cache lists', () => {
+ query.isSuccess = true;
+ query.items = [events, cron];
+ createRoot((dispose) => {
+ expect(useAutomationEntities()().map((entity) => entity.id)).toEqual([
+ 'cron-id',
+ ]);
+ dispose();
+ });
+ });
+
+ it('does not read pending query data or suspend a list', () => {
+ query.isSuccess = false;
+ query.isPending = true;
+ const data = vi.spyOn(query, 'data', 'get').mockImplementation(() => {
+ throw new Error('Pending resource read');
+ });
+ createRoot((dispose) => {
+ expect(useAutomationEntities()()).toEqual([]);
+ dispose();
+ });
+ data.mockRestore();
+ query.isSuccess = true;
+ query.isPending = false;
+ });
+
+ it('retains cached cron entities after a refetch error, excluding event routines', () => {
+ query.isSuccess = false;
+ query.items = [events, cron];
+ createRoot((dispose) => {
+ expect(useAutomationEntities()().map((entity) => entity.id)).toEqual([
+ 'cron-id',
+ ]);
+ dispose();
+ });
+ });
+
+ it('returns no entities after an initial failure without cached data', () => {
+ query.isSuccess = false;
+ query.items = [];
+ createRoot((dispose) => {
+ expect(useAutomationEntities()()).toEqual([]);
+ dispose();
+ });
+ });
+});
diff --git a/apps/web/src/lib/queries/agent-schedule/entities.ts b/apps/web/src/lib/queries/agent-schedule/entities.ts
index b1fd21a306..5bc4a54f74 100644
--- a/apps/web/src/lib/queries/agent-schedule/entities.ts
+++ b/apps/web/src/lib/queries/agent-schedule/entities.ts
@@ -2,6 +2,7 @@ import type { AutomationEntity } from '@entity';
import type { ScheduledAction } from '@service-scheduled-action/generated/schemas';
import { createMemo } from 'solid-js';
import { useSchedulesQuery } from './schedules';
+import { getCronTrigger } from './triggers';
// Must match `MAX_ACTION_TIME` on the backend
// (services/scheduled_action/src/domain/models.rs). After this window
@@ -17,7 +18,8 @@ function isClaimActive(claimed: string | undefined | null): boolean {
export function scheduleToEntity(
schedule: ScheduledAction
): AutomationEntity | undefined {
- if (!schedule.id) return undefined;
+ const trigger = getCronTrigger(schedule);
+ if (!schedule.id || !trigger) return undefined;
return {
id: schedule.id,
type: 'automation',
@@ -25,7 +27,7 @@ export function scheduleToEntity(
ownerId: schedule.owner,
createdAt: schedule.created_at,
updatedAt: schedule.updated_at,
- cron: schedule.schedule,
+ cron: trigger.schedule,
enabled: schedule.enabled,
nextRunAt: schedule.next_run_at,
isRunning: isClaimActive(schedule.claimed),
@@ -40,7 +42,7 @@ export function scheduleToEntity(
export function useAutomationEntities() {
const schedulesQuery = useSchedulesQuery(() => true);
return createMemo<AutomationEntity[]>(() => {
- const data = schedulesQuery.data;
+ const data = schedulesQuery.isPending ? undefined : schedulesQuery.data;
if (!data) return [];
const out: AutomationEntity[] = [];
for (const schedule of data) {
diff --git a/apps/web/src/lib/queries/agent-schedule/triggers.ts b/apps/web/src/lib/queries/agent-schedule/triggers.ts
new file mode 100644
index 0000000000..c19a6af0be
--- /dev/null
+++ b/apps/web/src/lib/queries/agent-schedule/triggers.ts
@@ -0,0 +1,24 @@
+import type { ActionTrigger } from '@service-scheduled-action/generated/schemas';
+
+type CronCompatibleAction = {
+ trigger?: ActionTrigger;
+ schedule?: string | null;
+ timezone?: string | null;
+};
+
+type CronTrigger = Extract<ActionTrigger, { type: 'cron' }>;
+
+/** Accept pre-trigger cached cron responses only when no tagged trigger exists. */
+export function getCronTrigger(
+ action: CronCompatibleAction
+): CronTrigger | undefined {
+ if (action.trigger !== undefined) {
+ return action.trigger?.type === 'cron' ? action.trigger : undefined;
+ }
+ if (!action.schedule || !action.timezone) return undefined;
+ return {
+ type: 'cron',
+ schedule: action.schedule,
+ timezone: action.timezone,
+ };
+}
diff --git a/apps/web/src/lib/service-clients/service-scheduled-action/client.ts b/apps/web/src/lib/service-clients/service-scheduled-action/client.ts
index 9b4a3288af..d7bc610570 100644
--- a/apps/web/src/lib/service-clients/service-scheduled-action/client.ts
+++ b/apps/web/src/lib/service-clients/service-scheduled-action/client.ts
@@ -34,10 +34,15 @@ function scheduledActionFetch<T extends ObjectLike = never>(
}
export const scheduledActionClient = {
+ // Include backend-managed routines so direct routes can identify them.
+ // Cron-only entity lists filter these out before rendering.
listSchedules: async () =>
- scheduledActionFetch<ScheduledAction[]>('/scheduled-actions', {
- method: 'GET',
- }),
+ scheduledActionFetch<ScheduledAction[]>(
+ '/scheduled-actions?include_events=true',
+ {
+ method: 'GET',
+ }
+ ),
createSchedule: async (body: CreateScheduledAction) =>
scheduledActionFetch<ScheduledAction>('/scheduled-actions', {
diff --git a/apps/web/src/lib/service-clients/service-scheduled-action/generated/client.ts b/apps/web/src/lib/service-clients/service-scheduled-action/generated/client.ts
index 7859a8ea46..a811f2f0d7 100644
--- a/apps/web/src/lib/service-clients/service-scheduled-action/generated/client.ts
+++ b/apps/web/src/lib/service-clients/service-scheduled-action/generated/client.ts
@@ -10,7 +10,8 @@ import type {
CreateScheduledAction,
EmptyResponse,
InProgressExecution,
- ScheduledAction,
+ ListScheduledActionsParams,
+ ScheduledActionResponse,
UpdateScheduledAction,
} from './schemas';
@@ -52,10 +53,15 @@ export const scheduledActionHealth = async (
};
export type listScheduledActionsResponse200 = {
- data: ScheduledAction[];
+ data: ScheduledActionResponse[];
status: 200;
};
+export type listScheduledActionsResponse400 = {
+ data: string;
+ status: 400;
+};
+
export type listScheduledActionsResponse401 = {
data: string;
status: 401;
@@ -71,6 +77,7 @@ export type listScheduledActionsResponseSuccess =
headers: Headers;
};
export type listScheduledActionsResponseError = (
+ | listScheduledActionsResponse400
| listScheduledActionsResponse401
| listScheduledActionsResponse500
) & {
@@ -81,14 +88,29 @@ export type listScheduledActionsResponse =
| listScheduledActionsResponseSuccess
| listScheduledActionsResponseError;
-export const getListScheduledActionsUrl = () => {
- return `/scheduled-actions`;
+export const getListScheduledActionsUrl = (
+ params?: ListScheduledActionsParams
+) => {
+ const normalizedParams = new URLSearchParams();
+
+ Object.entries(params || {}).forEach(([key, value]) => {
+ if (value !== undefined) {
+ normalizedParams.append(key, value === null ? 'null' : value.toString());
+ }
+ });
+
+ const stringifiedParams = normalizedParams.toString();
+
+ return stringifiedParams.length > 0
+ ? `/scheduled-actions?${stringifiedParams}`
+ : `/scheduled-actions`;
};
export const listScheduledActions = async (
+ params?: ListScheduledActionsParams,
options?: RequestInit
): Promise<listScheduledActionsResponse> => {
- const res = await fetch(getListScheduledActionsUrl(), {
+ const res = await fetch(getListScheduledActionsUrl(params), {
...options,
method: 'GET',
});
@@ -106,10 +128,15 @@ export const listScheduledActions = async (
};
export type createScheduledActionResponse201 = {
- data: ScheduledAction;
+ data: ScheduledActionResponse;
status: 201;
};
+export type createScheduledActionResponse400 = {
+ data: string;
+ status: 400;
+};
+
export type createScheduledActionResponse401 = {
data: string;
status: 401;
@@ -125,6 +152,7 @@ export type createScheduledActionResponseSuccess =
headers: Headers;
};
export type createScheduledActionResponseError = (
+ | createScheduledActionResponse400
| createScheduledActionResponse401
| createScheduledActionResponse500
) & {
@@ -163,10 +191,15 @@ export const createScheduledAction = async (
};
export type updateScheduledActionResponse200 = {
- data: ScheduledAction;
+ data: ScheduledActionResponse;
status: 200;
};
+export type updateScheduledActionResponse400 = {
+ data: string;
+ status: 400;
+};
+
export type updateScheduledActionResponse401 = {
data: string;
status: 401;
@@ -177,6 +210,11 @@ export type updateScheduledActionResponse404 = {
status: 404;
};
+export type updateScheduledActionResponse409 = {
+ data: string;
+ status: 409;
+};
+
export type updateScheduledActionResponse500 = {
data: string;
status: 500;
@@ -187,8 +225,10 @@ export type updateScheduledActionResponseSuccess =
headers: Headers;
};
export type updateScheduledActionResponseError = (
+ | updateScheduledActionResponse400
| updateScheduledActionResponse401
| updateScheduledActionResponse404
+ | updateScheduledActionResponse409
| updateScheduledActionResponse500
) & {
headers: Headers;
@@ -292,6 +332,11 @@ export type executeScheduledActionNowResponse200 = {
status: 200;
};
+export type executeScheduledActionNowResponse400 = {
+ data: string;
+ status: 400;
+};
+
export type executeScheduledActionNowResponse401 = {
data: string;
status: 401;
@@ -317,6 +362,7 @@ export type executeScheduledActionNowResponseSuccess =
headers: Headers;
};
export type executeScheduledActionNowResponseError = (
+ | executeScheduledActionNowResponse400
| executeScheduledActionNowResponse401
| executeScheduledActionNowResponse404
| executeScheduledActionNowResponse409
diff --git a/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/actionConfiguration.ts b/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/actionConfiguration.ts
new file mode 100644
index 0000000000..fd7a8e45f1
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/actionConfiguration.ts
@@ -0,0 +1,22 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * scheduled_action
+ * API for managing scheduled actions
+ * OpenAPI spec version: 0.1.0
+ */
+
+import type { ActionConfigurationTask } from './actionConfigurationTask';
+import type { ActionKind } from './actionKind';
+import type { ActionTrigger } from './actionTrigger';
+
+/**
+ * Canonical client configuration. Ownership and execution state are server-owned.
+ */
+export interface ActionConfiguration {
+ enabled: boolean;
+ kind: ActionKind;
+ name: string;
+ task: ActionConfigurationTask;
+ trigger: ActionTrigger;
+}
diff --git a/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/createScheduledActionTask.ts b/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/actionConfigurationTask.ts
similarity index 69%
rename from apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/createScheduledActionTask.ts
rename to apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/actionConfigurationTask.ts
index 47a73ffbbd..4506970ce7 100644
--- a/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/createScheduledActionTask.ts
+++ b/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/actionConfigurationTask.ts
@@ -6,4 +6,4 @@
* OpenAPI spec version: 0.1.0
*/
-export type CreateScheduledActionTask = { [key: string]: unknown };
+export type ActionConfigurationTask = { [key: string]: unknown };
diff --git a/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/actionTrigger.ts b/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/actionTrigger.ts
new file mode 100644
index 0000000000..033b08b4bb
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/actionTrigger.ts
@@ -0,0 +1,14 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * scheduled_action
+ * API for managing scheduled actions
+ * OpenAPI spec version: 0.1.0
+ */
+import type { ActionTriggerOneOf } from './actionTriggerOneOf';
+import type { ActionTriggerOneOfThree } from './actionTriggerOneOfThree';
+
+/**
+ * Exactly one trigger per action. Existing cron validation is reused.
+ */
+export type ActionTrigger = ActionTriggerOneOf | ActionTriggerOneOfThree;
diff --git a/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/actionTriggerOneOf.ts b/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/actionTriggerOneOf.ts
new file mode 100644
index 0000000000..3df5c4aa3f
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/actionTriggerOneOf.ts
@@ -0,0 +1,16 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * scheduled_action
+ * API for managing scheduled actions
+ * OpenAPI spec version: 0.1.0
+ */
+
+import type { ActionTriggerOneOfType } from './actionTriggerOneOfType';
+import type { Schedule } from './schedule';
+
+export type ActionTriggerOneOf = {
+ schedule: Schedule;
+ timezone: string;
+ type: ActionTriggerOneOfType;
+};
diff --git a/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/actionTriggerOneOfThree.ts b/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/actionTriggerOneOfThree.ts
new file mode 100644
index 0000000000..c2b4cd1ffe
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/actionTriggerOneOfThree.ts
@@ -0,0 +1,15 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * scheduled_action
+ * API for managing scheduled actions
+ * OpenAPI spec version: 0.1.0
+ */
+
+import type { ActionTriggerOneOfThreeType } from './actionTriggerOneOfThreeType';
+import type { EventFilters } from './eventFilters';
+
+export type ActionTriggerOneOfThree = {
+ filters: EventFilters;
+ type: ActionTriggerOneOfThreeType;
+};
diff --git a/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/actionTriggerOneOfThreeType.ts b/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/actionTriggerOneOfThreeType.ts
new file mode 100644
index 0000000000..005f37782c
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/actionTriggerOneOfThreeType.ts
@@ -0,0 +1,15 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * scheduled_action
+ * API for managing scheduled actions
+ * OpenAPI spec version: 0.1.0
+ */
+
+export type ActionTriggerOneOfThreeType =
+ (typeof ActionTriggerOneOfThreeType)[keyof typeof ActionTriggerOneOfThreeType];
+
+// eslint-disable-next-line @typescript-eslint/no-redeclare
+export const ActionTriggerOneOfThreeType = {
+ events: 'events',
+} as const;
diff --git a/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/actionTriggerOneOfType.ts b/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/actionTriggerOneOfType.ts
new file mode 100644
index 0000000000..a58782ef32
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/actionTriggerOneOfType.ts
@@ -0,0 +1,15 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * scheduled_action
+ * API for managing scheduled actions
+ * OpenAPI spec version: 0.1.0
+ */
+
+export type ActionTriggerOneOfType =
+ (typeof ActionTriggerOneOfType)[keyof typeof ActionTriggerOneOfType];
+
+// eslint-disable-next-line @typescript-eslint/no-redeclare
+export const ActionTriggerOneOfType = {
+ cron: 'cron',
+} as const;
diff --git a/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/createScheduledAction.ts b/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/createScheduledAction.ts
index a1cdf63dfd..a3a9adf4b1 100644
--- a/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/createScheduledAction.ts
+++ b/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/createScheduledAction.ts
@@ -5,20 +5,12 @@
* API for managing scheduled actions
* OpenAPI spec version: 0.1.0
*/
-import type { ActionKind } from './actionKind';
-import type { CreateScheduledActionTask } from './createScheduledActionTask';
-import type { Schedule } from './schedule';
+import type { ActionConfiguration } from './actionConfiguration';
+import type { LegacyActionConfiguration } from './legacyActionConfiguration';
/**
- * Client-supplied payload for creating a scheduled action. The server fills
-in `id`, `owner` (from the authenticated user), timestamps, `claimed`, and
-`next_run_at` (derived from the cron).
+ * Exactly one representation is accepted, even if mixed fields agree or are null.
*/
-export interface CreateScheduledAction {
- enabled: boolean;
- kind: ActionKind;
- name: string;
- schedule: Schedule;
- task: CreateScheduledActionTask;
- timezone: string;
-}
+export type CreateScheduledAction =
+ | ActionConfiguration
+ | LegacyActionConfiguration;
diff --git a/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/eventFilter.ts b/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/eventFilter.ts
new file mode 100644
index 0000000000..8dadcd07af
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/eventFilter.ts
@@ -0,0 +1,18 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * scheduled_action
+ * API for managing scheduled actions
+ * OpenAPI spec version: 0.1.0
+ */
+
+import type { EventFilterIds } from './eventFilterIds';
+import type { EventName } from './eventName';
+
+/**
+ * An event name AND an entity ID must match within the same filter.
+ */
+export interface EventFilter {
+ events: EventName[];
+ ids?: EventFilterIds;
+}
diff --git a/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/updateScheduledActionTask.ts b/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/eventFilterIds.ts
similarity index 69%
rename from apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/updateScheduledActionTask.ts
rename to apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/eventFilterIds.ts
index ff1184393f..db81cd2a72 100644
--- a/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/updateScheduledActionTask.ts
+++ b/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/eventFilterIds.ts
@@ -6,4 +6,4 @@
* OpenAPI spec version: 0.1.0
*/
-export type UpdateScheduledActionTask = { [key: string]: unknown };
+export type EventFilterIds = string[] | null;
diff --git a/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/eventFilters.ts b/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/eventFilters.ts
new file mode 100644
index 0000000000..7f8b9be9d5
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/eventFilters.ts
@@ -0,0 +1,13 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * scheduled_action
+ * API for managing scheduled actions
+ * OpenAPI spec version: 0.1.0
+ */
+import type { EventFilter } from './eventFilter';
+
+/**
+ * Bounded nonempty OR of filters, canonicalized to remove duplicates.
+ */
+export type EventFilters = EventFilter[];
diff --git a/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/eventName.ts b/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/eventName.ts
new file mode 100644
index 0000000000..b4695c0ce5
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/eventName.ts
@@ -0,0 +1,24 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * scheduled_action
+ * API for managing scheduled actions
+ * OpenAPI spec version: 0.1.0
+ */
+
+/**
+ * Closed allowlist: unknown names, deletions and ambiguous attribution are not
+selectors. Adding a broker variant does not automatically enable routines.
+ */
+export type EventName = (typeof EventName)[keyof typeof EventName];
+
+// eslint-disable-next-line @typescript-eslint/no-redeclare
+export const EventName = {
+ documentcreated: 'document.created',
+ documentupdated: 'document.updated',
+ channelcreated: 'channel.created',
+ channelmessage_posted: 'channel.message_posted',
+ channelmentioned: 'channel.mentioned',
+ channelmessage_patched: 'channel.message_patched',
+ channelmessage_attachment_created: 'channel.message_attachment_created',
+} as const;
diff --git a/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/index.ts b/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/index.ts
index cd33772993..8aacc79453 100644
--- a/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/index.ts
+++ b/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/index.ts
@@ -6,21 +6,40 @@
* OpenAPI spec version: 0.1.0
*/
+export * from './actionConfiguration';
+export * from './actionConfigurationTask';
export * from './actionExecutionRecord';
export * from './actionExecutionRecordId';
export * from './actionExecutionRecordResourceId';
export * from './actionExecutionRecordResult';
export * from './actionKind';
+export * from './actionTrigger';
+export * from './actionTriggerOneOf';
+export * from './actionTriggerOneOfThree';
+export * from './actionTriggerOneOfThreeType';
+export * from './actionTriggerOneOfType';
export * from './agentTask';
export * from './createScheduledAction';
-export * from './createScheduledActionTask';
export * from './emptyResponse';
+export * from './eventFilter';
+export * from './eventFilterIds';
+export * from './eventFilters';
+export * from './eventName';
export * from './inProgressExecution';
export * from './inProgressExecutionChatId';
+export * from './legacyActionConfiguration';
+export * from './legacyActionConfigurationTask';
+export * from './listScheduledActionsParams';
export * from './schedule';
export * from './scheduledAction';
export * from './scheduledActionClaimed';
+export * from './scheduledActionEventActivatedAt';
export * from './scheduledActionId';
+export * from './scheduledActionNextRunAt';
+export * from './scheduledActionResponse';
+export * from './scheduledActionResponseAllOf';
+export * from './scheduledActionResponseAllOfSchedule';
+export * from './scheduledActionResponseAllOfTimezone';
export * from './scheduledActionTask';
export * from './scheduledActionUpdate';
export * from './scheduledActionUpdateOneOf';
@@ -28,4 +47,3 @@ export * from './scheduledActionUpdateOneOfThree';
export * from './scheduledActionUpdateOneOfThreeType';
export * from './scheduledActionUpdateOneOfType';
export * from './updateScheduledAction';
-export * from './updateScheduledActionTask';
diff --git a/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/legacyActionConfiguration.ts b/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/legacyActionConfiguration.ts
new file mode 100644
index 0000000000..75132984f0
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/legacyActionConfiguration.ts
@@ -0,0 +1,22 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * scheduled_action
+ * API for managing scheduled actions
+ * OpenAPI spec version: 0.1.0
+ */
+import type { ActionKind } from './actionKind';
+import type { LegacyActionConfigurationTask } from './legacyActionConfigurationTask';
+import type { Schedule } from './schedule';
+
+/**
+ * Deprecated cron-only input, accepted during the compatibility rollout.
+ */
+export interface LegacyActionConfiguration {
+ enabled: boolean;
+ kind: ActionKind;
+ name: string;
+ schedule: Schedule;
+ task: LegacyActionConfigurationTask;
+ timezone: string;
+}
diff --git a/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/legacyActionConfigurationTask.ts b/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/legacyActionConfigurationTask.ts
new file mode 100644
index 0000000000..d43e163879
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/legacyActionConfigurationTask.ts
@@ -0,0 +1,9 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * scheduled_action
+ * API for managing scheduled actions
+ * OpenAPI spec version: 0.1.0
+ */
+
+export type LegacyActionConfigurationTask = { [key: string]: unknown };
diff --git a/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/listScheduledActionsParams.ts b/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/listScheduledActionsParams.ts
new file mode 100644
index 0000000000..0dca6d3800
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/listScheduledActionsParams.ts
@@ -0,0 +1,14 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * scheduled_action
+ * API for managing scheduled actions
+ * OpenAPI spec version: 0.1.0
+ */
+
+export type ListScheduledActionsParams = {
+ /**
+ * Backend clients must opt in to event actions; defaults to false (cron-only).
+ */
+ include_events?: boolean | null;
+};
diff --git a/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/scheduledAction.ts b/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/scheduledAction.ts
index cc82517069..759730f5ef 100644
--- a/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/scheduledAction.ts
+++ b/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/scheduledAction.ts
@@ -7,30 +7,34 @@
*/
import type { ActionKind } from './actionKind';
-import type { Schedule } from './schedule';
+import type { ActionTrigger } from './actionTrigger';
import type { ScheduledActionClaimed } from './scheduledActionClaimed';
+import type { ScheduledActionEventActivatedAt } from './scheduledActionEventActivatedAt';
import type { ScheduledActionId } from './scheduledActionId';
+import type { ScheduledActionNextRunAt } from './scheduledActionNextRunAt';
import type { ScheduledActionTask } from './scheduledActionTask';
export interface ScheduledAction {
claimed?: ScheduledActionClaimed;
+ /** Independent of execution bookkeeping in `updated_at`. */
+ configuration_revision: number;
created_at: string;
- /** When false, the cron dispatcher skips this schedule. `run_now` remains
+ /** When false, automatic dispatch skips this action. `run_now` remains
available regardless. */
enabled: boolean;
+ /** Event publication boundary; absent for cron actions. */
+ event_activated_at?: ScheduledActionEventActivatedAt;
id?: ScheduledActionId;
kind: ActionKind;
name: string;
- /** Time of the next scheduled firing (derived from the cron on write). UI
-uses this to render "next run" without having to parse the cron itself. */
- next_run_at: string;
+ /** Next cron firing, absent for event-triggered actions. */
+ next_run_at?: ScheduledActionNextRunAt;
/** Who the action belongs to. Every action is user-owned today, but the
type no longer says so: the principal string on the wire and in the
`owner` column is the same, and a bot- or team-owned row decodes
rather than failing to parse. */
owner: string;
- schedule: Schedule;
task: ScheduledActionTask;
- timezone: string;
+ trigger: ActionTrigger;
updated_at: string;
}
diff --git a/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/scheduledActionEventActivatedAt.ts b/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/scheduledActionEventActivatedAt.ts
new file mode 100644
index 0000000000..bf6de407d2
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/scheduledActionEventActivatedAt.ts
@@ -0,0 +1,12 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * scheduled_action
+ * API for managing scheduled actions
+ * OpenAPI spec version: 0.1.0
+ */
+
+/**
+ * Event publication boundary; absent for cron actions.
+ */
+export type ScheduledActionEventActivatedAt = string | null;
diff --git a/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/scheduledActionNextRunAt.ts b/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/scheduledActionNextRunAt.ts
new file mode 100644
index 0000000000..8838bd1590
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/scheduledActionNextRunAt.ts
@@ -0,0 +1,12 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * scheduled_action
+ * API for managing scheduled actions
+ * OpenAPI spec version: 0.1.0
+ */
+
+/**
+ * Next cron firing, absent for event-triggered actions.
+ */
+export type ScheduledActionNextRunAt = string | null;
diff --git a/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/scheduledActionResponse.ts b/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/scheduledActionResponse.ts
new file mode 100644
index 0000000000..ba8fedfea8
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/scheduledActionResponse.ts
@@ -0,0 +1,16 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * scheduled_action
+ * API for managing scheduled actions
+ * OpenAPI spec version: 0.1.0
+ */
+import type { ScheduledAction } from './scheduledAction';
+import type { ScheduledActionResponseAllOf } from './scheduledActionResponseAllOf';
+
+/**
+ * Canonical trigger plus deprecated cron fields for existing clients. Event
+responses omit legacy fields rather than inventing a schedule or timezone.
+ */
+export type ScheduledActionResponse = ScheduledAction &
+ ScheduledActionResponseAllOf;
diff --git a/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/scheduledActionResponseAllOf.ts b/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/scheduledActionResponseAllOf.ts
new file mode 100644
index 0000000000..2c820bbed8
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/scheduledActionResponseAllOf.ts
@@ -0,0 +1,22 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * scheduled_action
+ * API for managing scheduled actions
+ * OpenAPI spec version: 0.1.0
+ */
+import type { ScheduledActionResponseAllOfSchedule } from './scheduledActionResponseAllOfSchedule';
+import type { ScheduledActionResponseAllOfTimezone } from './scheduledActionResponseAllOfTimezone';
+
+export type ScheduledActionResponseAllOf = {
+ /**
+ * Deprecated: use `trigger.schedule`. Present only for cron actions.
+ * @deprecated
+ */
+ schedule?: ScheduledActionResponseAllOfSchedule;
+ /**
+ * Deprecated: use `trigger.timezone`. Present only for cron actions.
+ * @deprecated
+ */
+ timezone?: ScheduledActionResponseAllOfTimezone;
+};
diff --git a/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/scheduledActionResponseAllOfSchedule.ts b/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/scheduledActionResponseAllOfSchedule.ts
new file mode 100644
index 0000000000..cf5c3bf028
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/scheduledActionResponseAllOfSchedule.ts
@@ -0,0 +1,13 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * scheduled_action
+ * API for managing scheduled actions
+ * OpenAPI spec version: 0.1.0
+ */
+
+/**
+ * Deprecated: use `trigger.schedule`. Present only for cron actions.
+ * @deprecated
+ */
+export type ScheduledActionResponseAllOfSchedule = string | null;
diff --git a/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/scheduledActionResponseAllOfTimezone.ts b/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/scheduledActionResponseAllOfTimezone.ts
new file mode 100644
index 0000000000..34b94a4d00
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/scheduledActionResponseAllOfTimezone.ts
@@ -0,0 +1,13 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * scheduled_action
+ * API for managing scheduled actions
+ * OpenAPI spec version: 0.1.0
+ */
+
+/**
+ * Deprecated: use `trigger.timezone`. Present only for cron actions.
+ * @deprecated
+ */
+export type ScheduledActionResponseAllOfTimezone = string | null;
diff --git a/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/updateScheduledAction.ts b/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/updateScheduledAction.ts
index 44c7691a1d..578a3417b2 100644
--- a/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/updateScheduledAction.ts
+++ b/apps/web/src/lib/service-clients/service-scheduled-action/generated/schemas/updateScheduledAction.ts
@@ -5,20 +5,12 @@
* API for managing scheduled actions
* OpenAPI spec version: 0.1.0
*/
-import type { ActionKind } from './actionKind';
-import type { Schedule } from './schedule';
-import type { UpdateScheduledActionTask } from './updateScheduledActionTask';
+import type { ActionConfiguration } from './actionConfiguration';
+import type { LegacyActionConfiguration } from './legacyActionConfiguration';
/**
- * Client-supplied payload for updating a scheduled action. Mirrors the fields
-the repository actually writes — `id`/`owner`/timestamps/`claimed`/
-`next_run_at` are not client-mutable.
+ * Full replacement of client configuration, not of server-owned action state.
*/
-export interface UpdateScheduledAction {
- enabled: boolean;
- kind: ActionKind;
- name: string;
- schedule: Schedule;
- task: UpdateScheduledActionTask;
- timezone: string;
-}
+export type UpdateScheduledAction =
+ | ActionConfiguration
+ | LegacyActionConfiguration;
diff --git a/apps/web/src/lib/service-clients/service-scheduled-action/openapi.json b/apps/web/src/lib/service-clients/service-scheduled-action/openapi.json
index 0d4e972330..ad9f4f69b0 100644
--- a/apps/web/src/lib/service-clients/service-scheduled-action/openapi.json
+++ b/apps/web/src/lib/service-clients/service-scheduled-action/openapi.json
@@ -32,6 +32,18 @@
"get": {
"tags": ["scheduled actions"],
"operationId": "list_scheduled_actions",
+ "parameters": [
+ {
+ "name": "include_events",
+ "in": "query",
+ "description": "Backend clients must opt in to event actions; defaults to false (cron-only).",
+ "required": false,
+ "schema": {
+ "type": ["boolean", "null"],
+ "default": false
+ }
+ }
+ ],
"responses": {
"200": {
"description": "",
@@ -40,12 +52,22 @@
"schema": {
"type": "array",
"items": {
- "$ref": "#/components/schemas/ScheduledAction"
+ "$ref": "#/components/schemas/ScheduledActionResponse"
}
}
}
}
},
+ "400": {
+ "description": "",
+ "content": {
+ "text/plain": {
+ "schema": {
+ "type": "string"
+ }
+ }
+ }
+ },
"401": {
"description": "",
"content": {
@@ -87,7 +109,17 @@
"content": {
"application/json": {
"schema": {
- "$ref": "#/components/schemas/ScheduledAction"
+ "$ref": "#/components/schemas/ScheduledActionResponse"
+ }
+ }
+ }
+ },
+ "400": {
+ "description": "",
+ "content": {
+ "text/plain": {
+ "schema": {
+ "type": "string"
}
}
}
@@ -146,7 +178,17 @@
"content": {
"application/json": {
"schema": {
- "$ref": "#/components/schemas/ScheduledAction"
+ "$ref": "#/components/schemas/ScheduledActionResponse"
+ }
+ }
+ }
+ },
+ "400": {
+ "description": "",
+ "content": {
+ "text/plain": {
+ "schema": {
+ "type": "string"
}
}
}
@@ -171,6 +213,16 @@
}
}
},
+ "409": {
+ "description": "Configuration changed or execution is active",
+ "content": {
+ "text/plain": {
+ "schema": {
+ "type": "string"
+ }
+ }
+ }
+ },
"500": {
"description": "",
"content": {
@@ -260,6 +312,16 @@
}
}
},
+ "400": {
+ "description": "",
+ "content": {
+ "text/plain": {
+ "schema": {
+ "type": "string"
+ }
+ }
+ }
+ },
"401": {
"description": "",
"content": {
@@ -368,6 +430,29 @@
},
"components": {
"schemas": {
+ "ActionConfiguration": {
+ "type": "object",
+ "description": "Canonical client configuration. Ownership and execution state are server-owned.",
+ "required": ["name", "trigger", "kind", "task", "enabled"],
+ "properties": {
+ "enabled": {
+ "type": "boolean"
+ },
+ "kind": {
+ "$ref": "#/components/schemas/ActionKind"
+ },
+ "name": {
+ "type": "string"
+ },
+ "task": {
+ "type": "object"
+ },
+ "trigger": {
+ "$ref": "#/components/schemas/ActionTrigger"
+ }
+ },
+ "additionalProperties": false
+ },
"ActionExecutionRecord": {
"type": "object",
"required": [
@@ -415,6 +500,40 @@
"type": "string",
"enum": ["Agent"]
},
+ "ActionTrigger": {
+ "oneOf": [
+ {
+ "type": "object",
+ "required": ["schedule", "timezone", "type"],
+ "properties": {
+ "schedule": {
+ "$ref": "#/components/schemas/Schedule"
+ },
+ "timezone": {
+ "type": "string"
+ },
+ "type": {
+ "type": "string",
+ "enum": ["cron"]
+ }
+ }
+ },
+ {
+ "type": "object",
+ "required": ["filters", "type"],
+ "properties": {
+ "filters": {
+ "$ref": "#/components/schemas/EventFilters"
+ },
+ "type": {
+ "type": "string",
+ "enum": ["events"]
+ }
+ }
+ }
+ ],
+ "description": "Exactly one trigger per action. Existing cron validation is reused."
+ },
"AgentTask": {
"type": "object",
"required": ["model", "prompt", "user_prompt"],
@@ -431,8 +550,75 @@
}
},
"CreateScheduledAction": {
+ "oneOf": [
+ {
+ "$ref": "#/components/schemas/ActionConfiguration"
+ },
+ {
+ "$ref": "#/components/schemas/LegacyActionConfiguration"
+ }
+ ],
+ "description": "Exactly one representation is accepted, even if mixed fields agree or are null."
+ },
+ "EmptyResponse": {
+ "type": "object",
+ "description": "Empty response is required due to custom fetch forcing `response.json()`"
+ },
+ "EventFilter": {
+ "type": "object",
+ "description": "An event name AND an entity ID must match within the same filter.",
+ "required": ["events"],
+ "properties": {
+ "events": {
+ "type": "array",
+ "items": {
+ "$ref": "#/components/schemas/EventName"
+ }
+ },
+ "ids": {
+ "type": ["array", "null"],
+ "items": {
+ "type": "string"
+ }
+ }
+ }
+ },
+ "EventFilters": {
+ "type": "array",
+ "items": {
+ "$ref": "#/components/schemas/EventFilter"
+ },
+ "description": "Bounded nonempty OR of filters, canonicalized to remove duplicates."
+ },
+ "EventName": {
+ "type": "string",
+ "description": "Closed allowlist: unknown names, deletions and ambiguous attribution are not\nselectors. Adding a broker variant does not automatically enable routines.",
+ "enum": [
+ "document.created",
+ "document.updated",
+ "channel.created",
+ "channel.message_posted",
+ "channel.mentioned",
+ "channel.message_patched",
+ "channel.message_attachment_created"
+ ]
+ },
+ "InProgressExecution": {
+ "type": "object",
+ "required": ["action_id"],
+ "properties": {
+ "action_id": {
+ "type": "string",
+ "format": "uuid"
+ },
+ "chat_id": {
+ "type": ["string", "null"]
+ }
+ }
+ },
+ "LegacyActionConfiguration": {
"type": "object",
- "description": "Client-supplied payload for creating a scheduled action. The server fills\nin `id`, `owner` (from the authenticated user), timestamps, `claimed`, and\n`next_run_at` (derived from the cron).",
+ "description": "Deprecated cron-only input, accepted during the compatibility rollout.",
"required": ["name", "schedule", "kind", "timezone", "task", "enabled"],
"properties": {
"enabled": {
@@ -453,24 +639,8 @@
"timezone": {
"type": "string"
}
- }
- },
- "EmptyResponse": {
- "type": "object",
- "description": "Empty response is required due to custom fetch forcing `response.json()`"
- },
- "InProgressExecution": {
- "type": "object",
- "required": ["action_id"],
- "properties": {
- "action_id": {
- "type": "string",
- "format": "uuid"
- },
- "chat_id": {
- "type": ["string", "null"]
- }
- }
+ },
+ "additionalProperties": false
},
"Schedule": {
"type": "string"
@@ -480,13 +650,12 @@
"required": [
"owner",
"name",
- "schedule",
+ "trigger",
"kind",
"created_at",
"updated_at",
- "timezone",
+ "configuration_revision",
"task",
- "next_run_at",
"enabled"
],
"properties": {
@@ -494,13 +663,23 @@
"type": ["string", "null"],
"format": "date-time"
},
+ "configuration_revision": {
+ "type": "integer",
+ "format": "int64",
+ "description": "Independent of execution bookkeeping in `updated_at`."
+ },
"created_at": {
"type": "string",
"format": "date-time"
},
"enabled": {
"type": "boolean",
- "description": "When false, the cron dispatcher skips this schedule. `run_now` remains\navailable regardless."
+ "description": "When false, automatic dispatch skips this action. `run_now` remains\navailable regardless."
+ },
+ "event_activated_at": {
+ "type": ["string", "null"],
+ "format": "date-time",
+ "description": "Event publication boundary; absent for cron actions."
},
"id": {
"type": ["string", "null"],
@@ -513,22 +692,19 @@
"type": "string"
},
"next_run_at": {
- "type": "string",
+ "type": ["string", "null"],
"format": "date-time",
- "description": "Time of the next scheduled firing (derived from the cron on write). UI\nuses this to render \"next run\" without having to parse the cron itself."
+ "description": "Next cron firing, absent for event-triggered actions."
},
"owner": {
"type": "string",
"description": "Who the action belongs to. Every action is user-owned today, but the\ntype no longer says so: the principal string on the wire and in the\n`owner` column is the same, and a bot- or team-owned row decodes\nrather than failing to parse."
},
- "schedule": {
- "$ref": "#/components/schemas/Schedule"
- },
"task": {
"type": "object"
},
- "timezone": {
- "type": "string"
+ "trigger": {
+ "$ref": "#/components/schemas/ActionTrigger"
},
"updated_at": {
"type": "string",
@@ -536,6 +712,29 @@
}
}
},
+ "ScheduledActionResponse": {
+ "allOf": [
+ {
+ "$ref": "#/components/schemas/ScheduledAction"
+ },
+ {
+ "type": "object",
+ "properties": {
+ "schedule": {
+ "type": ["string", "null"],
+ "description": "Deprecated: use `trigger.schedule`. Present only for cron actions.",
+ "deprecated": true
+ },
+ "timezone": {
+ "type": ["string", "null"],
+ "description": "Deprecated: use `trigger.timezone`. Present only for cron actions.",
+ "deprecated": true
+ }
+ }
+ }
+ ],
+ "description": "Canonical trigger plus deprecated cron fields for existing clients. Event\nresponses omit legacy fields rather than inventing a schedule or timezone."
+ },
"ScheduledActionUpdate": {
"oneOf": [
{
@@ -585,29 +784,15 @@
"description": "Live status update for a scheduled-action run, broadcast via the connection\ngateway to the owner. Clients use the `chat_id` to navigate to the run\ntranscript and the variant tag to toggle the running indicator.\n\nSerialized with a `type` tag (`started`/`stopped`) and delivered over the\nsingle `scheduled_action_update` message type on the gateway."
},
"UpdateScheduledAction": {
- "type": "object",
- "description": "Client-supplied payload for updating a scheduled action. Mirrors the fields\nthe repository actually writes — `id`/`owner`/timestamps/`claimed`/\n`next_run_at` are not client-mutable.",
- "required": ["name", "schedule", "kind", "timezone", "task", "enabled"],
- "properties": {
- "enabled": {
- "type": "boolean"
- },
- "kind": {
- "$ref": "#/components/schemas/ActionKind"
- },
- "name": {
- "type": "string"
- },
- "schedule": {
- "$ref": "#/components/schemas/Schedule"
- },
- "task": {
- "type": "object"
+ "oneOf": [
+ {
+ "$ref": "#/components/schemas/ActionConfiguration"
},
- "timezone": {
- "type": "string"
+ {
+ "$ref": "#/components/schemas/LegacyActionConfiguration"
}
- }
+ ],
+ "description": "Full replacement of client configuration, not of server-owned action state."
}
}
},
diff --git a/crates/macro_db_client/migrations/20260922181603_add_scheduled_action_event_triggers.sql b/crates/macro_db_client/migrations/20260922181603_add_scheduled_action_event_triggers.sql
new file mode 100644
index 0000000000..e83550336e
--- /dev/null
+++ b/crates/macro_db_client/migrations/20260922181603_add_scheduled_action_event_triggers.sql
@@ -0,0 +1,32 @@
+-- Defaults preserve inserts from the deployed cron-only service, including its
+-- database-generated ID. New service writers supply UUIDv7 IDs themselves.
+ALTER TABLE scheduled_action
+ ADD COLUMN trigger_type TEXT NOT NULL DEFAULT 'cron',
+ ADD COLUMN event_filters JSONB,
+ ADD COLUMN configuration_revision BIGINT NOT NULL DEFAULT 1,
+ ADD COLUMN event_activated_at TIMESTAMPTZ,
+ ALTER COLUMN schedule DROP NOT NULL,
+ ALTER COLUMN timezone DROP NOT NULL,
+ ALTER COLUMN next_run_at DROP NOT NULL,
+ ADD CONSTRAINT scheduled_action_configuration_revision_check
+ CHECK (configuration_revision > 0),
+ ADD CONSTRAINT scheduled_action_trigger_shape_check CHECK (
+ (
+ (trigger_type = 'cron'
+ AND schedule IS NOT NULL
+ AND timezone IS NOT NULL
+ AND next_run_at IS NOT NULL
+ AND event_filters IS NULL
+ AND event_activated_at IS NULL)
+ OR
+ (trigger_type = 'events'
+ AND schedule IS NULL
+ AND timezone IS NULL
+ AND next_run_at IS NULL
+ AND event_filters IS NOT NULL
+ AND CASE WHEN jsonb_typeof(event_filters) = 'array'
+ THEN jsonb_array_length(event_filters) BETWEEN 1 AND 32
+ ELSE FALSE END
+ AND event_activated_at IS NOT NULL)
+ ) IS TRUE
+ );
diff --git a/crates/macro_db_client/migrations/20260922182949_add_scheduled_action_event_runs.sql b/crates/macro_db_client/migrations/20260922182949_add_scheduled_action_event_runs.sql
new file mode 100644
index 0000000000..fc94ee43bd
--- /dev/null
+++ b/crates/macro_db_client/migrations/20260922182949_add_scheduled_action_event_runs.sql
@@ -0,0 +1,49 @@
+-- Nullable for deployed cron/manual writers. New writers fence every claim.
+ALTER TABLE scheduled_action ADD COLUMN claim_token UUID;
+
+CREATE TABLE scheduled_action_event_run (
+ action_id UUID NOT NULL REFERENCES scheduled_action (id) ON DELETE CASCADE,
+ event_id UUID NOT NULL,
+ configuration_revision BIGINT NOT NULL CHECK (configuration_revision > 0),
+ -- Only event name, entity ID and optional message ID; never broker content.
+ event_context JSONB NOT NULL CHECK (jsonb_typeof(event_context) = 'object'),
+ admission_order BIGINT GENERATED ALWAYS AS IDENTITY,
+ admitted_at TIMESTAMPTZ NOT NULL DEFAULT now(),
+ state TEXT NOT NULL DEFAULT 'pending' CHECK (state IN ('pending', 'started', 'finished')),
+ claim_token UUID,
+ started_at TIMESTAMPTZ,
+ deadline TIMESTAMPTZ,
+ finished_at TIMESTAMPTZ,
+ outcome JSONB,
+ execution_record_id UUID REFERENCES action_execution_record (id) ON DELETE SET NULL,
+ PRIMARY KEY (action_id, event_id),
+ CONSTRAINT scheduled_action_event_run_lifecycle CHECK (
+ (state = 'pending' AND claim_token IS NULL AND started_at IS NULL
+ AND deadline IS NULL AND finished_at IS NULL AND outcome IS NULL
+ AND execution_record_id IS NULL)
+ OR (state = 'started' AND claim_token IS NOT NULL AND started_at IS NOT NULL
+ AND deadline IS NOT NULL AND deadline > started_at AND finished_at IS NULL AND outcome IS NULL
+ AND execution_record_id IS NULL)
+ OR (state = 'finished' AND finished_at IS NOT NULL AND outcome IS NOT NULL
+ AND ((claim_token IS NULL AND started_at IS NULL AND deadline IS NULL)
+ OR (claim_token IS NOT NULL AND started_at IS NOT NULL
+ AND deadline IS NOT NULL AND deadline > started_at)))
+ ),
+ CONSTRAINT scheduled_action_event_run_outcome CHECK (
+ outcome IS NULL OR (outcome ->> 'type' IN ('succeeded', 'failed', 'cancelled', 'interrupted')) IS TRUE
+ )
+);
+
+-- Serialize one action's queue; admission takes the action lock before allocating
+-- its sequence number, so sequence order is also committed admission order.
+CREATE INDEX scheduled_action_event_run_pending_idx
+ ON scheduled_action_event_run (action_id, admission_order) WHERE state = 'pending';
+CREATE UNIQUE INDEX scheduled_action_event_run_started_idx
+ ON scheduled_action_event_run (action_id) WHERE state = 'started';
+CREATE INDEX scheduled_action_event_run_deadline_idx
+ ON scheduled_action_event_run (deadline, action_id) WHERE state = 'started';
+CREATE INDEX scheduled_action_event_filters_idx
+ ON scheduled_action USING GIN (event_filters jsonb_path_ops)
+ WHERE trigger_type = 'events' AND enabled;
+
+-- Finished rows are deduplication tombstones, retained until action deletion.
diff --git a/docs/AGENT_GUIDE/navigation.md b/docs/AGENT_GUIDE/navigation.md
index c18ad603a4..b798d90e30 100644
--- a/docs/AGENT_GUIDE/navigation.md
+++ b/docs/AGENT_GUIDE/navigation.md
@@ -33,6 +33,7 @@
| `/app/<document-type>/<uuid>` | Legacy document URL (including `md`, `pdf`, `canvas`, `spreadsheet`, and the other Drive document types); redirects to `/app/drive/<document-type>/<uuid>` |
| `/app/documents`, `/app/files` | Legacy Files views; redirect to `/app/drive` |
| `/app/chat/<uuid>` | A standalone AI chat |
+| `/app/automation/<uuid>` | Cron routine editor; event routines show a backend-managed notice |
| `/app/agent/<uuid>` | An agent session (opened from `@macro-new` / `@coder` / `@cursor`) |
| `/app/md/<doc>/chat/<chat>` | Doc + doc-scoped chat in a split |
| `/app/md/<doc>/channel/<channel>` | Doc + channel in a split |
@@ -351,6 +352,23 @@ Release, cancellation, or dragging outside
restores the surface. Disabled controls stay still; reduced motion keeps only
the static highlight.
+## Routines (automations)
+
+Create → Automation creates a cron-scheduled routine. Cron routines support
+editing instructions and schedule, Rename, Pause/Resume, Duplicate, Run Now,
+and History links to run chats. Edits autosave; Run Now also works while paused.
+If a background refresh fails, cached cron routines stay listed and their editor
+and queued autosave remain available. An initial load failure without cached data
+shows **Unable to load automation** instead. Cached event routines remain
+backend-managed even after a refresh failure.
+
+Event-triggered routines are backend-managed through the scheduled-action API.
+They do not appear in the frontend's cron-only automation lists. Opening an
+API-created event routine at `/app/automation/<uuid>` shows **Backend-managed
+routine**, not a cron editor. This surface offers no event editing, duplication,
+or run/history controls; manage those through the API. It never replaces an
+event trigger with a cron schedule. There is no event-filter composer yet.
+
## Command menu (Ctrl+K)
Opens a dialog with a focused `Search...` textbox and bubble-style category radios
diff --git a/packages/sdk/generated/scheduled-action/index.ts b/packages/sdk/generated/scheduled-action/index.ts
index e8ea86d4fe..656a232412 100644
--- a/packages/sdk/generated/scheduled-action/index.ts
+++ b/packages/sdk/generated/scheduled-action/index.ts
@@ -1,4 +1,4 @@
// This file is auto-generated by @hey-api/openapi-ts
export { type Options, Sdk } from './sdk.gen';
-export type { ActionExecutionRecord, ActionKind, AgentTask, ClientOptions, CreateScheduledAction, CreateScheduledActionData, CreateScheduledActionError, CreateScheduledActionErrors, CreateScheduledActionResponse, CreateScheduledActionResponses, DeleteScheduledActionData, DeleteScheduledActionError, DeleteScheduledActionErrors, DeleteScheduledActionResponse, DeleteScheduledActionResponses, EmptyResponse, ExecuteScheduledActionNowData, ExecuteScheduledActionNowError, ExecuteScheduledActionNowErrors, ExecuteScheduledActionNowResponse, ExecuteScheduledActionNowResponses, InProgressExecution, ListScheduledActionHistoryData, ListScheduledActionHistoryError, ListScheduledActionHistoryErrors, ListScheduledActionHistoryResponse, ListScheduledActionHistoryResponses, ListScheduledActionsData, ListScheduledActionsError, ListScheduledActionsErrors, ListScheduledActionsResponse, ListScheduledActionsResponses, Schedule, ScheduledAction, ScheduledActionHealthData, ScheduledActionHealthResponse, ScheduledActionHealthResponses, ScheduledActionUpdate, UpdateScheduledAction, UpdateScheduledActionData, UpdateScheduledActionError, UpdateScheduledActionErrors, UpdateScheduledActionResponse, UpdateScheduledActionResponses } from './types.gen';
+export type { ActionConfiguration, ActionExecutionRecord, ActionKind, ActionTrigger, AgentTask, ClientOptions, CreateScheduledAction, CreateScheduledActionData, CreateScheduledActionError, CreateScheduledActionErrors, CreateScheduledActionResponse, CreateScheduledActionResponses, DeleteScheduledActionData, DeleteScheduledActionError, DeleteScheduledActionErrors, DeleteScheduledActionResponse, DeleteScheduledActionResponses, EmptyResponse, EventFilter, EventFilters, EventName, ExecuteScheduledActionNowData, ExecuteScheduledActionNowError, ExecuteScheduledActionNowErrors, ExecuteScheduledActionNowResponse, ExecuteScheduledActionNowResponses, InProgressExecution, LegacyActionConfiguration, ListScheduledActionHistoryData, ListScheduledActionHistoryError, ListScheduledActionHistoryErrors, ListScheduledActionHistoryResponse, ListScheduledActionHistoryResponses, ListScheduledActionsData, ListScheduledActionsError, ListScheduledActionsErrors, ListScheduledActionsResponse, ListScheduledActionsResponses, Schedule, ScheduledAction, ScheduledActionHealthData, ScheduledActionHealthResponse, ScheduledActionHealthResponses, ScheduledActionResponse, ScheduledActionUpdate, UpdateScheduledAction, UpdateScheduledActionData, UpdateScheduledActionError, UpdateScheduledActionErrors, UpdateScheduledActionResponse, UpdateScheduledActionResponses } from './types.gen';
diff --git a/packages/sdk/generated/scheduled-action/types.gen.ts b/packages/sdk/generated/scheduled-action/types.gen.ts
index 6b9e8ff370..9e67a77573 100644
--- a/packages/sdk/generated/scheduled-action/types.gen.ts
+++ b/packages/sdk/generated/scheduled-action/types.gen.ts
@@ -4,6 +4,19 @@ export type ClientOptions = {
baseUrl: `${string}://${string}` | (string & {});
};
+/**
+ * Canonical client configuration. Ownership and execution state are server-owned.
+ */
+export type ActionConfiguration = {
+ enabled: boolean;
+ kind: ActionKind;
+ name: string;
+ task: {
+ [key: string]: unknown;
+ };
+ trigger: ActionTrigger;
+};
+
export type ActionExecutionRecord = {
action_id: string;
created_at: string;
@@ -23,6 +36,18 @@ export type ActionExecutionRecord = {
export type ActionKind = 'Agent';
+/**
+ * Exactly one trigger per action. Existing cron validation is reused.
+ */
+export type ActionTrigger = {
+ schedule: Schedule;
+ timezone: string;
+ type: 'cron';
+} | {
+ filters: EventFilters;
+ type: 'events';
+};
+
export type AgentTask = {
model: string;
prompt: string;
@@ -30,20 +55,9 @@ export type AgentTask = {
};
/**
- * Client-supplied payload for creating a scheduled action. The server fills
- * in `id`, `owner` (from the authenticated user), timestamps, `claimed`, and
- * `next_run_at` (derived from the cron).
+ * Exactly one representation is accepted, even if mixed fields agree or are null.
*/
-export type CreateScheduledAction = {
- enabled: boolean;
- kind: ActionKind;
- name: string;
- schedule: Schedule;
- task: {
- [key: string]: unknown;
- };
- timezone: string;
-};
+export type CreateScheduledAction = ActionConfiguration | LegacyActionConfiguration;
/**
* Empty response is required due to custom fetch forcing `response.json()`
@@ -52,29 +66,69 @@ export type EmptyResponse = {
[key: string]: unknown;
};
+/**
+ * An event name AND an entity ID must match within the same filter.
+ */
+export type EventFilter = {
+ events: Array<EventName>;
+ ids?: Array<string> | null;
+};
+
+/**
+ * Bounded nonempty OR of filters, canonicalized to remove duplicates.
+ */
+export type EventFilters = Array<EventFilter>;
+
+/**
+ * Closed allowlist: unknown names, deletions and ambiguous attribution are not
+ * selectors. Adding a broker variant does not automatically enable routines.
+ */
+export type EventName = 'document.created' | 'document.updated' | 'channel.created' | 'channel.message_posted' | 'channel.mentioned' | 'channel.message_patched' | 'channel.message_attachment_created';
+
export type InProgressExecution = {
action_id: string;
chat_id?: string | null;
};
+/**
+ * Deprecated cron-only input, accepted during the compatibility rollout.
+ */
+export type LegacyActionConfiguration = {
+ enabled: boolean;
+ kind: ActionKind;
+ name: string;
+ schedule: Schedule;
+ task: {
+ [key: string]: unknown;
+ };
+ timezone: string;
+};
+
export type Schedule = string;
export type ScheduledAction = {
claimed?: string | null;
+ /**
+ * Independent of execution bookkeeping in `updated_at`.
+ */
+ configuration_revision: number;
created_at: string;
/**
- * When false, the cron dispatcher skips this schedule. `run_now` remains
+ * When false, automatic dispatch skips this action. `run_now` remains
* available regardless.
*/
enabled: boolean;
+ /**
+ * Event publication boundary; absent for cron actions.
+ */
+ event_activated_at?: string | null;
id?: string | null;
kind: ActionKind;
name: string;
/**
- * Time of the next scheduled firing (derived from the cron on write). UI
- * uses this to render "next run" without having to parse the cron itself.
+ * Next cron firing, absent for event-triggered actions.
*/
- next_run_at: string;
+ next_run_at?: string | null;
/**
* Who the action belongs to. Every action is user-owned today, but the
* type no longer says so: the principal string on the wire and in the
@@ -82,14 +136,32 @@ export type ScheduledAction = {
* rather than failing to parse.
*/
owner: string;
- schedule: Schedule;
task: {
[key: string]: unknown;
};
- timezone: string;
+ trigger: ActionTrigger;
updated_at: string;
};
+/**
+ * Canonical trigger plus deprecated cron fields for existing clients. Event
+ * responses omit legacy fields rather than inventing a schedule or timezone.
+ */
+export type ScheduledActionResponse = ScheduledAction & {
+ /**
+ * Deprecated: use `trigger.schedule`. Present only for cron actions.
+ *
+ * @deprecated
+ */
+ schedule?: string | null;
+ /**
+ * Deprecated: use `trigger.timezone`. Present only for cron actions.
+ *
+ * @deprecated
+ */
+ timezone?: string | null;
+};
+
/**
* Live status update for a scheduled-action run, broadcast via the connection
* gateway to the owner. Clients use the `chat_id` to navigate to the run
@@ -112,20 +184,9 @@ export type ScheduledActionUpdate = {
};
/**
- * Client-supplied payload for updating a scheduled action. Mirrors the fields
- * the repository actually writes — `id`/`owner`/timestamps/`claimed`/
- * `next_run_at` are not client-mutable.
+ * Full replacement of client configuration, not of server-owned action state.
*/
-export type UpdateScheduledAction = {
- enabled: boolean;
- kind: ActionKind;
- name: string;
- schedule: Schedule;
- task: {
- [key: string]: unknown;
- };
- timezone: string;
-};
+export type UpdateScheduledAction = ActionConfiguration | LegacyActionConfiguration;
export type ScheduledActionHealthData = {
body?: never;
@@ -146,11 +207,17 @@ export type ScheduledActionHealthResponse = ScheduledActionHealthResponses[keyof
export type ListScheduledActionsData = {
body?: never;
path?: never;
- query?: never;
+ query?: {
+ /**
+ * Backend clients must opt in to event actions; defaults to false (cron-only).
+ */
+ include_events?: boolean | null;
+ };
url: '/scheduled-actions';
};
export type ListScheduledActionsErrors = {
+ 400: string;
401: string;
500: string;
};
@@ -158,7 +225,7 @@ export type ListScheduledActionsErrors = {
export type ListScheduledActionsError = ListScheduledActionsErrors[keyof ListScheduledActionsErrors];
export type ListScheduledActionsResponses = {
- 200: Array<ScheduledAction>;
+ 200: Array<ScheduledActionResponse>;
};
export type ListScheduledActionsResponse = ListScheduledActionsResponses[keyof ListScheduledActionsResponses];
@@ -171,6 +238,7 @@ export type CreateScheduledActionData = {
};
export type CreateScheduledActionErrors = {
+ 400: string;
401: string;
500: string;
};
@@ -178,7 +246,7 @@ export type CreateScheduledActionErrors = {
export type CreateScheduledActionError = CreateScheduledActionErrors[keyof CreateScheduledActionErrors];
export type CreateScheduledActionResponses = {
- 201: ScheduledAction;
+ 201: ScheduledActionResponse;
};
export type CreateScheduledActionResponse = CreateScheduledActionResponses[keyof CreateScheduledActionResponses];
@@ -222,15 +290,20 @@ export type UpdateScheduledActionData = {
};
export type UpdateScheduledActionErrors = {
+ 400: string;
401: string;
404: string;
+ /**
+ * Configuration changed or execution is active
+ */
+ 409: string;
500: string;
};
export type UpdateScheduledActionError = UpdateScheduledActionErrors[keyof UpdateScheduledActionErrors];
export type UpdateScheduledActionResponses = {
- 200: ScheduledAction;
+ 200: ScheduledActionResponse;
};
export type UpdateScheduledActionResponse = UpdateScheduledActionResponses[keyof UpdateScheduledActionResponses];
@@ -248,6 +321,7 @@ export type ExecuteScheduledActionNowData = {
};
export type ExecuteScheduledActionNowErrors = {
+ 400: string;
401: string;
404: string;
/**
diff --git a/packages/sdk/specs/scheduled-action.json b/packages/sdk/specs/scheduled-action.json
index 0d4e972330..ad9f4f69b0 100644
--- a/packages/sdk/specs/scheduled-action.json
+++ b/packages/sdk/specs/scheduled-action.json
@@ -32,6 +32,18 @@
"get": {
"tags": ["scheduled actions"],
"operationId": "list_scheduled_actions",
+ "parameters": [
+ {
+ "name": "include_events",
+ "in": "query",
+ "description": "Backend clients must opt in to event actions; defaults to false (cron-only).",
+ "required": false,
+ "schema": {
+ "type": ["boolean", "null"],
+ "default": false
+ }
+ }
+ ],
"responses": {
"200": {
"description": "",
@@ -40,12 +52,22 @@
"schema": {
"type": "array",
"items": {
- "$ref": "#/components/schemas/ScheduledAction"
+ "$ref": "#/components/schemas/ScheduledActionResponse"
}
}
}
}
},
+ "400": {
+ "description": "",
+ "content": {
+ "text/plain": {
+ "schema": {
+ "type": "string"
+ }
+ }
+ }
+ },
"401": {
"description": "",
"content": {
@@ -87,7 +109,17 @@
"content": {
"application/json": {
"schema": {
- "$ref": "#/components/schemas/ScheduledAction"
+ "$ref": "#/components/schemas/ScheduledActionResponse"
+ }
+ }
+ }
+ },
+ "400": {
+ "description": "",
+ "content": {
+ "text/plain": {
+ "schema": {
+ "type": "string"
}
}
}
@@ -146,7 +178,17 @@
"content": {
"application/json": {
"schema": {
- "$ref": "#/components/schemas/ScheduledAction"
+ "$ref": "#/components/schemas/ScheduledActionResponse"
+ }
+ }
+ }
+ },
+ "400": {
+ "description": "",
+ "content": {
+ "text/plain": {
+ "schema": {
+ "type": "string"
}
}
}
@@ -171,6 +213,16 @@
}
}
},
+ "409": {
+ "description": "Configuration changed or execution is active",
+ "content": {
+ "text/plain": {
+ "schema": {
+ "type": "string"
+ }
+ }
+ }
+ },
"500": {
"description": "",
"content": {
@@ -260,6 +312,16 @@
}
}
},
+ "400": {
+ "description": "",
+ "content": {
+ "text/plain": {
+ "schema": {
+ "type": "string"
+ }
+ }
+ }
+ },
"401": {
"description": "",
"content": {
@@ -368,6 +430,29 @@
},
"components": {
"schemas": {
+ "ActionConfiguration": {
+ "type": "object",
+ "description": "Canonical client configuration. Ownership and execution state are server-owned.",
+ "required": ["name", "trigger", "kind", "task", "enabled"],
+ "properties": {
+ "enabled": {
+ "type": "boolean"
+ },
+ "kind": {
+ "$ref": "#/components/schemas/ActionKind"
+ },
+ "name": {
+ "type": "string"
+ },
+ "task": {
+ "type": "object"
+ },
+ "trigger": {
+ "$ref": "#/components/schemas/ActionTrigger"
+ }
+ },
+ "additionalProperties": false
+ },
"ActionExecutionRecord": {
"type": "object",
"required": [
@@ -415,6 +500,40 @@
"type": "string",
"enum": ["Agent"]
},
+ "ActionTrigger": {
+ "oneOf": [
+ {
+ "type": "object",
+ "required": ["schedule", "timezone", "type"],
+ "properties": {
+ "schedule": {
+ "$ref": "#/components/schemas/Schedule"
+ },
+ "timezone": {
+ "type": "string"
+ },
+ "type": {
+ "type": "string",
+ "enum": ["cron"]
+ }
+ }
+ },
+ {
+ "type": "object",
+ "required": ["filters", "type"],
+ "properties": {
+ "filters": {
+ "$ref": "#/components/schemas/EventFilters"
+ },
+ "type": {
+ "type": "string",
+ "enum": ["events"]
+ }
+ }
+ }
+ ],
+ "description": "Exactly one trigger per action. Existing cron validation is reused."
+ },
"AgentTask": {
"type": "object",
"required": ["model", "prompt", "user_prompt"],
@@ -431,8 +550,75 @@
}
},
"CreateScheduledAction": {
+ "oneOf": [
+ {
+ "$ref": "#/components/schemas/ActionConfiguration"
+ },
+ {
+ "$ref": "#/components/schemas/LegacyActionConfiguration"
+ }
+ ],
+ "description": "Exactly one representation is accepted, even if mixed fields agree or are null."
+ },
+ "EmptyResponse": {
+ "type": "object",
+ "description": "Empty response is required due to custom fetch forcing `response.json()`"
+ },
+ "EventFilter": {
+ "type": "object",
+ "description": "An event name AND an entity ID must match within the same filter.",
+ "required": ["events"],
+ "properties": {
+ "events": {
+ "type": "array",
+ "items": {
+ "$ref": "#/components/schemas/EventName"
+ }
+ },
+ "ids": {
+ "type": ["array", "null"],
+ "items": {
+ "type": "string"
+ }
+ }
+ }
+ },
+ "EventFilters": {
+ "type": "array",
+ "items": {
+ "$ref": "#/components/schemas/EventFilter"
+ },
+ "description": "Bounded nonempty OR of filters, canonicalized to remove duplicates."
+ },
+ "EventName": {
+ "type": "string",
+ "description": "Closed allowlist: unknown names, deletions and ambiguous attribution are not\nselectors. Adding a broker variant does not automatically enable routines.",
+ "enum": [
+ "document.created",
+ "document.updated",
+ "channel.created",
+ "channel.message_posted",
+ "channel.mentioned",
+ "channel.message_patched",
+ "channel.message_attachment_created"
+ ]
+ },
+ "InProgressExecution": {
+ "type": "object",
+ "required": ["action_id"],
+ "properties": {
+ "action_id": {
+ "type": "string",
+ "format": "uuid"
+ },
+ "chat_id": {
+ "type": ["string", "null"]
+ }
+ }
+ },
+ "LegacyActionConfiguration": {
"type": "object",
- "description": "Client-supplied payload for creating a scheduled action. The server fills\nin `id`, `owner` (from the authenticated user), timestamps, `claimed`, and\n`next_run_at` (derived from the cron).",
+ "description": "Deprecated cron-only input, accepted during the compatibility rollout.",
"required": ["name", "schedule", "kind", "timezone", "task", "enabled"],
"properties": {
"enabled": {
@@ -453,24 +639,8 @@
"timezone": {
"type": "string"
}
- }
- },
- "EmptyResponse": {
- "type": "object",
- "description": "Empty response is required due to custom fetch forcing `response.json()`"
- },
- "InProgressExecution": {
- "type": "object",
- "required": ["action_id"],
- "properties": {
- "action_id": {
- "type": "string",
- "format": "uuid"
- },
- "chat_id": {
- "type": ["string", "null"]
- }
- }
+ },
+ "additionalProperties": false
},
"Schedule": {
"type": "string"
@@ -480,13 +650,12 @@
"required": [
"owner",
"name",
- "schedule",
+ "trigger",
"kind",
"created_at",
"updated_at",
- "timezone",
+ "configuration_revision",
"task",
- "next_run_at",
"enabled"
],
"properties": {
@@ -494,13 +663,23 @@
"type": ["string", "null"],
"format": "date-time"
},
+ "configuration_revision": {
+ "type": "integer",
+ "format": "int64",
+ "description": "Independent of execution bookkeeping in `updated_at`."
+ },
"created_at": {
"type": "string",
"format": "date-time"
},
"enabled": {
"type": "boolean",
- "description": "When false, the cron dispatcher skips this schedule. `run_now` remains\navailable regardless."
+ "description": "When false, automatic dispatch skips this action. `run_now` remains\navailable regardless."
+ },
+ "event_activated_at": {
+ "type": ["string", "null"],
+ "format": "date-time",
+ "description": "Event publication boundary; absent for cron actions."
},
"id": {
"type": ["string", "null"],
@@ -513,22 +692,19 @@
"type": "string"
},
"next_run_at": {
- "type": "string",
+ "type": ["string", "null"],
"format": "date-time",
- "description": "Time of the next scheduled firing (derived from the cron on write). UI\nuses this to render \"next run\" without having to parse the cron itself."
+ "description": "Next cron firing, absent for event-triggered actions."
},
"owner": {
"type": "string",
"description": "Who the action belongs to. Every action is user-owned today, but the\ntype no longer says so: the principal string on the wire and in the\n`owner` column is the same, and a bot- or team-owned row decodes\nrather than failing to parse."
},
- "schedule": {
- "$ref": "#/components/schemas/Schedule"
- },
"task": {
"type": "object"
},
- "timezone": {
- "type": "string"
+ "trigger": {
+ "$ref": "#/components/schemas/ActionTrigger"
},
"updated_at": {
"type": "string",
@@ -536,6 +712,29 @@
}
}
},
+ "ScheduledActionResponse": {
+ "allOf": [
+ {
+ "$ref": "#/components/schemas/ScheduledAction"
+ },
+ {
+ "type": "object",
+ "properties": {
+ "schedule": {
+ "type": ["string", "null"],
+ "description": "Deprecated: use `trigger.schedule`. Present only for cron actions.",
+ "deprecated": true
+ },
+ "timezone": {
+ "type": ["string", "null"],
+ "description": "Deprecated: use `trigger.timezone`. Present only for cron actions.",
+ "deprecated": true
+ }
+ }
+ }
+ ],
+ "description": "Canonical trigger plus deprecated cron fields for existing clients. Event\nresponses omit legacy fields rather than inventing a schedule or timezone."
+ },
"ScheduledActionUpdate": {
"oneOf": [
{
@@ -585,29 +784,15 @@
"description": "Live status update for a scheduled-action run, broadcast via the connection\ngateway to the owner. Clients use the `chat_id` to navigate to the run\ntranscript and the variant tag to toggle the running indicator.\n\nSerialized with a `type` tag (`started`/`stopped`) and delivered over the\nsingle `scheduled_action_update` message type on the gateway."
},
"UpdateScheduledAction": {
- "type": "object",
- "description": "Client-supplied payload for updating a scheduled action. Mirrors the fields\nthe repository actually writes — `id`/`owner`/timestamps/`claimed`/\n`next_run_at` are not client-mutable.",
- "required": ["name", "schedule", "kind", "timezone", "task", "enabled"],
- "properties": {
- "enabled": {
- "type": "boolean"
- },
- "kind": {
- "$ref": "#/components/schemas/ActionKind"
- },
- "name": {
- "type": "string"
- },
- "schedule": {
- "$ref": "#/components/schemas/Schedule"
- },
- "task": {
- "type": "object"
+ "oneOf": [
+ {
+ "$ref": "#/components/schemas/ActionConfiguration"
},
- "timezone": {
- "type": "string"
+ {
+ "$ref": "#/components/schemas/LegacyActionConfiguration"
}
- }
+ ],
+ "description": "Full replacement of client configuration, not of server-owned action state."
}
}
},
diff --git a/services/scheduled_action/Cargo.toml b/services/scheduled_action/Cargo.toml
index f498a7ab77..fd3feb54eb 100644
--- a/services/scheduled_action/Cargo.toml
+++ b/services/scheduled_action/Cargo.toml
@@ -21,6 +21,17 @@ default = []
local_auth = ["macro_authorization/local_auth"]
[dependencies]
+activity = { path = "../../crates/activity", default-features = false }
+channel_sender = { path = "../../crates/channel_sender" }
+channels = { path = "../../crates/channels", default-features = false }
+documents = { path = "../../crates/documents", default-features = false }
+entity_access = { path = "../../crates/entity_access", default-features = false, features = ["ports"] }
+kafka_util = { path = "../../crates/kafka_util" }
+macro_event_broker = { path = "../../crates/macro_event_broker", features = ["outbound"] }
+macro_event_topics = { path = "../../crates/macro_event_topics" }
+rdkafka = { workspace = true }
+rootcause = { workspace = true }
+thiserror = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
macro_env_var = { path = "../../crates/macro_env_var" }
diff --git a/services/scheduled_action/src/bins/service.rs b/services/scheduled_action/src/bins/service.rs
index f8ccc92c9f..ec37373055 100644
--- a/services/scheduled_action/src/bins/service.rs
+++ b/services/scheduled_action/src/bins/service.rs
@@ -1,10 +1,12 @@
#![recursion_limit = "256"]
-use std::{sync::Arc, time::Duration};
+use std::{future::Future, sync::Arc, time::Duration};
-use ai_tools::build_tool_service_context_from_env;
+use ai_tools::{AiHost, build_tool_service_context_from_env, tools_for};
use anyhow::{Context, Result};
use axum::Router;
+use chat::outbound::postgres::PgChatRepo;
use connection_gateway_client::client::ConnectionGatewayClient;
+use entity_access::{domain::service::EntityAccessServiceImpl, outbound::PgAccessRepository};
use macro_auth::middleware::decode_jwt::JwtValidationArgs;
use macro_authorization::{
InternalAuthConfig, MacroAuthJwtValidator, MacroAuthorizationServiceImpl,
@@ -12,16 +14,27 @@ use macro_authorization::{
};
use macro_entrypoint::MacroEntrypoint;
use macro_service_urls::ConnectionGatewayUrl;
+use memory::domain::service::MemoryServiceImpl;
+use memory::outbound::pg_memory_repo::PgMemoryRepo;
use notification::domain::service::SqsNotificationIngress;
use notification::outbound::queue::SqsQueue;
use scheduled_action::config::Config;
+use scheduled_action::domain::event_runs::{
+ PageSize, admission::EventAdmissionService, dispatch::EventDispatchService,
+};
use scheduled_action::domain::ports::ScheduledActionDispatcher;
use scheduled_action::domain::service::ScheduledActionServiceImpl;
use scheduled_action::inbound::axum_router::{
ScheduledActionRouterState, health, scheduled_action_router,
};
+use scheduled_action::inbound::event_run_worker::run_event_worker;
+use scheduled_action::inbound::kafka_consumer::run_scheduled_action_event_consumer;
use scheduled_action::outbound::conn_gateway_live_updates::ConnGatewayLiveUpdates;
-use scheduled_action::outbound::inprocess_executor::InProcessExecutor;
+use scheduled_action::outbound::event_access::EventAccessAdapter;
+use scheduled_action::outbound::inprocess_executor::{
+ InProcessExecutor, agent_task::AgentTaskRunner,
+};
+use scheduled_action::outbound::pg_event_run_repo::PgEventRunRepo;
use scheduled_action::outbound::pg_polling_dispatcher::{
PgPollingDispatcher, PgPollingDispatcherLifecycle,
};
@@ -35,7 +48,14 @@ use utoipa_swagger_ui::SwaggerUi;
#[cfg(test)]
mod test;
-const EVENT_BROKER_DRAIN_TIMEOUT: Duration = Duration::from_secs(10);
+// ECS stopTimeout is ten seconds. One shared budget includes HTTP, all
+// dispatch/execution bookkeeping, and final broker publishes, leaving two seconds.
+const SHUTDOWN_TIMEOUT: Duration = Duration::from_secs(8);
+const CONSUMER_RESTART_DELAY: Duration = Duration::from_secs(5);
+// Keep event agent work well below the shared pool's ten connections, leaving
+// headroom for HTTP/cron and tool calls (not a per-tool connection reservation).
+const EVENT_CONCURRENCY: u16 = 2;
+const ADMISSION_PAGE_SIZE: u16 = 100;
const GATEWAY_PATH_PREFIX: &str = "/scheduled-action";
#[tokio::main]
@@ -53,11 +73,10 @@ async fn main() -> Result<()> {
.await
.context("failed to connect to macrodb")?;
- let event_broker_tracker = TaskTracker::new();
- let tool_context =
- build_tool_service_context_from_env(db.clone(), event_broker_tracker.clone())
- .await
- .context("failed to build tool service context")?;
+ let lifecycle = ServiceLifecycle::default();
+ let tool_context = build_tool_service_context_from_env(db.clone(), lifecycle.publishes.clone())
+ .await
+ .context("failed to build tool service context")?;
let aws_config = macro_aws_config::get_macro_aws_config().await;
let notification_ingress = Arc::new(SqsNotificationIngress {
@@ -80,39 +99,30 @@ async fn main() -> Result<()> {
let repo = Arc::new(PgScheduledActionRepo::new(db.clone()));
- // The dispatcher consumes its executor, so build a second executor for the
- // service to use when handling execute-now requests. Both executors share
- // the underlying repo/pool/tool-context via cheap Arc/PgPool clones.
- let dispatcher_executor = InProcessExecutor::new(
- Arc::clone(&repo),
- db.clone(),
+ let event_repo = Arc::new(PgEventRunRepo::new(db.clone()));
+ let event_access = Arc::new(EventAccessAdapter::new(EntityAccessServiceImpl::new(
+ PgAccessRepository::new(db.clone()),
+ )));
+ let memory = MemoryServiceImpl::new(
+ PgMemoryRepo::new(db.clone()),
tool_context.clone(),
- Arc::clone(&notification_ingress),
- Arc::clone(&live_updates),
+ tools_for(AiHost::Chat),
);
- let service_executor = Arc::new(InProcessExecutor::new(
- Arc::clone(&repo),
- db.clone(),
+ let runner = Arc::new(AgentTaskRunner::new(
+ Arc::clone(&tool_context.chat_tool_context.service),
+ PgChatRepo::new(db.clone()),
+ memory,
tool_context,
notification_ingress,
- live_updates,
));
-
- let dispatcher_cancellation_token = CancellationToken::new();
- let dispatcher_tracker = TaskTracker::new();
- let dispatcher_lifecycle = PgPollingDispatcherLifecycle::new(
- dispatcher_cancellation_token.clone(),
- dispatcher_tracker.clone(),
- );
- let dispatcher = PgPollingDispatcher::new(Arc::clone(&repo), dispatcher_executor)
- .with_lifecycle(dispatcher_lifecycle);
- let (dispatcher_tx, _execution_rx) = dispatcher.begin_dispatch_loop();
-
- let service = Arc::new(ScheduledActionServiceImpl::new(
+ let dispatcher_executor = InProcessExecutor::new(
Arc::clone(&repo),
- service_executor,
- dispatcher_tx,
- ));
+ runner,
+ live_updates,
+ lifecycle.executions.clone(),
+ lifecycle.stop_executions.clone(),
+ );
+ let service_executor = Arc::new(dispatcher_executor.clone());
let jwt_args = JwtValidationArgs::new_with_secret_manager(environment, &secretsmanager_client)
.await
@@ -129,6 +139,56 @@ async fn main() -> Result<()> {
);
let authorization_state = MacroAuthorizationState::new(Arc::new(authorization_service));
+ // Finish fallible startup before launching intake or claiming any work.
+ let addr = format!("0.0.0.0:{}", config.port);
+ let listener = tokio::net::TcpListener::bind(&addr)
+ .await
+ .with_context(|| format!("failed to bind {addr}"))?;
+
+ let dispatcher = PgPollingDispatcher::new(Arc::clone(&repo), dispatcher_executor)
+ .with_lifecycle(PgPollingDispatcherLifecycle::new(
+ lifecycle.stop_workers.clone(),
+ lifecycle.workers.clone(),
+ ));
+ let (dispatcher_tx, execution_rx) = dispatcher.begin_dispatch_loop();
+ // No subscriber consumes these notifications; don't let a full channel
+ // eventually block cron dispatch or its shutdown.
+ drop(execution_rx);
+
+ let event_dispatch = EventDispatchService::new(
+ Arc::clone(&event_repo),
+ Arc::clone(&event_access),
+ Arc::clone(&service_executor),
+ );
+ let brokers = config.kafka_brokers.to_string();
+ let intake_shutdown = lifecycle.stop_consumers.clone();
+ start_event_tasks(
+ config.event_routines_enabled,
+ &lifecycle,
+ move || {
+ let admission = EventAdmissionService::new(
+ Arc::clone(&event_repo),
+ Arc::clone(&event_access),
+ PageSize::try_from(ADMISSION_PAGE_SIZE).expect("valid admission bound"),
+ );
+ let brokers = brokers.clone();
+ let shutdown = intake_shutdown.clone();
+ async move {
+ run_scheduled_action_event_consumer(&brokers, admission, shutdown.cancelled()).await
+ }
+ },
+ run_event_worker(
+ event_dispatch,
+ lifecycle.stop_workers.clone(),
+ lifecycle.executions.clone(),
+ PageSize::try_from(EVENT_CONCURRENCY).expect("valid worker bound"),
+ ),
+ );
+
+ let service = Arc::new(
+ ScheduledActionServiceImpl::new(Arc::clone(&repo), service_executor, dispatcher_tx)
+ .with_event_management_enabled(config.event_routines_enabled),
+ );
let state = ScheduledActionRouterState {
service,
authorization_state,
@@ -144,39 +204,137 @@ async fn main() -> Result<()> {
.merge(mount_docs_at_root_and_prefix())
.layer(macro_cors::cors_layer());
- let port = config.port;
- let addr = format!("0.0.0.0:{port}");
- let listener = tokio::net::TcpListener::bind(&addr)
- .await
- .with_context(|| format!("failed to bind {addr}"))?;
+ tracing::info!(
+ event_routines_enabled = config.event_routines_enabled,
+ "scheduled_action service listening on {addr}"
+ );
+
+ let http_shutdown = lifecycle.stop_http.clone();
+ let server = async move {
+ axum::serve(listener, router.into_make_service())
+ .with_graceful_shutdown(http_shutdown.cancelled_owned())
+ .await
+ .context("server closed")
+ };
+ serve_until_shutdown(
+ server,
+ macro_entrypoint::shutdown_signal(),
+ &lifecycle,
+ SHUTDOWN_TIMEOUT,
+ )
+ .await
+}
- tracing::info!("scheduled_action service listening on {addr}");
+#[derive(Default)]
+struct ServiceLifecycle {
+ stop_http: CancellationToken,
+ stop_consumers: CancellationToken,
+ stop_workers: CancellationToken,
+ stop_executions: CancellationToken,
+ consumers: TaskTracker,
+ workers: TaskTracker,
+ executions: TaskTracker,
+ publishes: TaskTracker,
+}
- let server_result = axum::serve(listener, router.into_make_service())
- .with_graceful_shutdown(macro_entrypoint::shutdown_signal())
- .await
- .context("server closed");
-
- tracing::info!("stopping scheduled action dispatcher");
- dispatcher_cancellation_token.cancel();
- dispatcher_tracker.close();
- dispatcher_tracker.wait().await;
- tracing::info!("scheduled action dispatcher stopped");
-
- 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 {
- Ok(()) => tracing::info!("event broker publishes drained"),
- Err(error) => {
- tracing::warn!(
- error=?error,
- timeout_seconds = EVENT_BROKER_DRAIN_TIMEOUT.as_secs(),
- "timed out waiting for event broker publishes to drain"
- );
+impl ServiceLifecycle {
+ fn stop(&self) {
+ self.stop_http.cancel();
+ self.stop_consumers.cancel();
+ self.stop_workers.cancel();
+ self.stop_executions.cancel();
+ self.consumers.close();
+ self.workers.close();
+ self.executions.close();
+ }
+
+ async fn drain(&self) {
+ // Workers may still be completing a committed claim. Wait for them
+ // before considering the execution tracker permanently empty.
+ tokio::join!(self.consumers.wait(), self.workers.wait());
+ self.executions.wait().await;
+ self.publishes.close();
+ self.publishes.wait().await;
+ }
+}
+
+fn start_event_tasks<C, F>(
+ enabled: bool,
+ lifecycle: &ServiceLifecycle,
+ consumer: C,
+ worker: impl Future<Output = ()> + Send + 'static,
+) where
+ C: FnMut() -> F + Send + 'static,
+ F: Future<Output = Result<(), rootcause::Report>> + Send + 'static,
+{
+ if !enabled {
+ return;
+ }
+ lifecycle.consumers.spawn(supervise_consumer(
+ consumer,
+ lifecycle.stop_consumers.clone(),
+ CONSUMER_RESTART_DELAY,
+ ));
+ lifecycle.workers.spawn(worker);
+}
+
+async fn supervise_consumer<F: Future<Output = Result<(), rootcause::Report>>>(
+ mut consume: impl FnMut() -> F,
+ shutdown: CancellationToken,
+ restart_delay: Duration,
+) {
+ loop {
+ tokio::select! {
+ biased;
+ _ = shutdown.cancelled() => return,
+ result = consume() => {
+ // Do not log admission reports, which can contain event data.
+ tracing::warn!(failed = result.is_err(), "event consumer exited; restarting with a fresh consumer");
+ }
+ }
+ // Even an unexpected successful exit must not silently disable intake.
+ // A new consumer restores committed offsets; never reuse its position.
+ tokio::select! {
+ biased;
+ _ = shutdown.cancelled() => return,
+ _ = tokio::time::sleep(restart_delay) => {},
}
}
+}
- server_result
+async fn serve_until_shutdown(
+ server: impl Future<Output = Result<()>>,
+ signal: impl Future<Output = ()>,
+ lifecycle: &ServiceLifecycle,
+ timeout: Duration,
+) -> Result<()> {
+ let mut server = std::pin::pin!(server);
+ let mut result = tokio::select! {
+ result = &mut server => Some(result),
+ _ = signal => None,
+ };
+ lifecycle.stop();
+ let drain = async {
+ // All cancellation is already signalled, so work drains concurrently.
+ // Join HTTP before trusting an empty execution tracker: a request may
+ // have passed the executor's shutdown check just before cancellation.
+ if result.is_none() {
+ result = Some(server.await);
+ }
+ lifecycle.drain().await;
+ };
+ if tokio::time::timeout(timeout, drain).await.is_err() {
+ tracing::warn!(
+ consumers = lifecycle.consumers.len(),
+ workers = lifecycle.workers.len(),
+ executions = lifecycle.executions.len(),
+ publishes = lifecycle.publishes.len(),
+ "shutdown deadline reached; unresolved started event runs remain non-retryable"
+ );
+ // The process exits next. Never turn uncertain started work back into
+ // pending work; reconciliation marks it interrupted after claim expiry.
+ }
+ result.unwrap_or(Ok(()))
}
fn mount_at_root_and_prefix(inner: Router) -> Router {
diff --git a/services/scheduled_action/src/bins/test.rs b/services/scheduled_action/src/bins/test.rs
index 1849c6f3f8..026ec3a84a 100644
--- a/services/scheduled_action/src/bins/test.rs
+++ b/services/scheduled_action/src/bins/test.rs
@@ -4,8 +4,159 @@ use axum::{
body::Body,
http::{Request, StatusCode},
};
+use std::sync::atomic::{AtomicUsize, Ordering};
use tower::ServiceExt;
+#[tokio::test]
+async fn disabled_startup_launches_neither_consumer_nor_worker() {
+ let lifecycle = ServiceLifecycle::default();
+ start_event_tasks(
+ false,
+ &lifecycle,
+ || async { panic!("disabled consumer was polled") },
+ async { panic!("disabled worker was polled") },
+ );
+ assert!(lifecycle.consumers.is_empty());
+ assert!(lifecycle.workers.is_empty());
+ lifecycle.stop();
+ lifecycle.drain().await;
+}
+
+#[tokio::test]
+async fn enabled_startup_tracks_and_stops_both_background_tasks() {
+ let lifecycle = ServiceLifecycle::default();
+ let worker_shutdown = lifecycle.stop_workers.clone();
+ start_event_tasks(true, &lifecycle, std::future::pending, async move {
+ worker_shutdown.cancelled().await
+ });
+ assert_eq!(lifecycle.consumers.len(), 1);
+ assert_eq!(lifecycle.workers.len(), 1);
+ lifecycle.stop();
+ tokio::time::timeout(Duration::from_secs(1), lifecycle.drain())
+ .await
+ .unwrap();
+ assert!(lifecycle.consumers.is_empty());
+ assert!(lifecycle.workers.is_empty());
+}
+
+#[tokio::test]
+async fn consumer_failure_and_unexpected_exit_restart_from_a_fresh_factory() {
+ let attempts = Arc::new(AtomicUsize::new(0));
+ let shutdown = CancellationToken::new();
+ let calls = Arc::clone(&attempts);
+ let stop = shutdown.clone();
+ supervise_consumer(
+ move || {
+ let call = calls.fetch_add(1, Ordering::SeqCst);
+ let stop = stop.clone();
+ async move {
+ match call {
+ 0 => Err(rootcause::report!("transient retries exhausted")),
+ 1 => Ok(()),
+ _ => {
+ stop.cancel();
+ Ok(())
+ }
+ }
+ }
+ },
+ shutdown,
+ Duration::ZERO,
+ )
+ .await;
+ assert_eq!(attempts.load(Ordering::SeqCst), 3);
+}
+
+#[tokio::test]
+async fn shutdown_interrupts_consumer_restart_backoff() {
+ let shutdown = CancellationToken::new();
+ let stop = shutdown.clone();
+ tokio::time::timeout(
+ Duration::from_secs(1),
+ supervise_consumer(
+ move || {
+ stop.cancel();
+ async { Err(rootcause::report!("consumer failed")) }
+ },
+ shutdown,
+ Duration::from_secs(60),
+ ),
+ )
+ .await
+ .unwrap();
+}
+
+#[tokio::test]
+async fn shutdown_drains_executions_before_final_publishes() {
+ let lifecycle = ServiceLifecycle::default();
+ let stop = lifecycle.stop_executions.clone();
+ let publishes = lifecycle.publishes.clone();
+ let finished = Arc::new(AtomicUsize::new(0));
+ let published = Arc::clone(&finished);
+ lifecycle.executions.spawn(async move {
+ stop.cancelled().await;
+ assert!(!publishes.is_closed());
+ publishes.spawn(async move {
+ published.fetch_add(1, Ordering::SeqCst);
+ });
+ });
+ let stop_http = lifecycle.stop_http.clone();
+ serve_until_shutdown(
+ async move {
+ stop_http.cancelled().await;
+ Ok(())
+ },
+ std::future::ready(()),
+ &lifecycle,
+ Duration::from_secs(1),
+ )
+ .await
+ .unwrap();
+ assert_eq!(finished.load(Ordering::SeqCst), 1);
+ assert!(lifecycle.executions.is_empty());
+ assert!(lifecycle.publishes.is_empty());
+}
+
+#[tokio::test]
+async fn one_deadline_bounds_stalled_http_and_background_work() {
+ let lifecycle = ServiceLifecycle::default();
+ let task = lifecycle.executions.spawn(std::future::pending::<()>());
+ tokio::time::timeout(
+ Duration::from_secs(1),
+ serve_until_shutdown(
+ std::future::pending(),
+ std::future::ready(()),
+ &lifecycle,
+ Duration::from_millis(10),
+ ),
+ )
+ .await
+ .unwrap()
+ .unwrap();
+ assert!(lifecycle.stop_consumers.is_cancelled());
+ assert!(lifecycle.stop_workers.is_cancelled());
+ assert!(lifecycle.stop_executions.is_cancelled());
+ task.abort();
+ assert!(task.await.unwrap_err().is_cancelled());
+}
+
+#[tokio::test]
+async fn server_failure_also_stops_background_work() {
+ let lifecycle = ServiceLifecycle::default();
+ let error = serve_until_shutdown(
+ async { anyhow::bail!("server failed") },
+ std::future::pending(),
+ &lifecycle,
+ Duration::from_secs(1),
+ )
+ .await
+ .unwrap_err();
+ assert_eq!(error.to_string(), "server failed");
+ assert!(lifecycle.stop_consumers.is_cancelled());
+ assert!(lifecycle.stop_workers.is_cancelled());
+ assert!(lifecycle.stop_executions.is_cancelled());
+}
+
fn docs_router() -> Router {
Router::new()
.merge(mount_at_root_and_prefix(
diff --git a/services/scheduled_action/src/config.rs b/services/scheduled_action/src/config.rs
index ec5d19a0e6..5dce51eae8 100644
--- a/services/scheduled_action/src/config.rs
+++ b/services/scheduled_action/src/config.rs
@@ -16,6 +16,9 @@ use macro_auth::InternalApiKey;
pub use macro_env::Environment;
use macro_env_var::env_vars;
+#[cfg(test)]
+mod test;
+
env_vars! {
/// Auth key used by the document storage / search / lexical clients.
pub struct DocumentStorageServiceAuthKey;
@@ -58,6 +61,10 @@ pub struct Config {
pub document_storage_service_cloudfront_signer_private_key_secret_name:
DocumentStorageServiceCloudfrontSignerPrivateKeySecretName,
pub kafka_brokers: KafkaBrokers,
+ /// Enables event-trigger management, Kafka intake, and pending dispatch together.
+ /// Register EVENT_ROUTINES_ENABLED as a raw boolean in Doppler before rollout.
+ #[macro_config_default(false)]
+ pub event_routines_enabled: bool,
/// The internal api key
pub internal_api_key: InternalApiKey,
}
diff --git a/services/scheduled_action/src/config/test.rs b/services/scheduled_action/src/config/test.rs
new file mode 100644
index 0000000000..9ae9c9d1df
--- /dev/null
+++ b/services/scheduled_action/src/config/test.rs
@@ -0,0 +1,38 @@
+use super::*;
+use serde_json::{Value, json};
+
+fn config_values() -> Value {
+ json!({
+ "ENVIRONMENT": "local",
+ "DATABASE_URL": "postgres://localhost/macro",
+ "DOCUMENT_STORAGE_SERVICE_AUTH_KEY": "test",
+ "SYNC_SERVICE_AUTH_KEY": "test",
+ "DOCUMENT_STORAGE_BUCKET": "test",
+ "DOCX_DOCUMENT_UPLOAD_BUCKET": "test",
+ "DOCUMENT_STORAGE_SERVICE_CLOUDFRONT_DISTRIBUTION_URL": "test",
+ "DOCUMENT_STORAGE_SERVICE_CLOUDFRONT_SIGNER_PUBLIC_KEY_ID": "test",
+ "DOCUMENT_STORAGE_SERVICE_CLOUDFRONT_SIGNER_PRIVATE_KEY_SECRET_NAME": "test",
+ "KAFKA_BROKERS": "existing-broker:9092",
+ "INTERNAL_API_KEY": "test"
+ })
+}
+
+#[test]
+fn event_routines_default_off_and_reuse_existing_brokers() {
+ let config: Config = serde_json::from_value(config_values()).unwrap();
+ assert!(!config.event_routines_enabled);
+ assert_eq!(config.kafka_brokers.to_string(), "existing-broker:9092");
+}
+
+#[test]
+fn event_routines_require_an_explicit_boolean() {
+ for enabled in [false, true] {
+ let mut values = config_values();
+ values["EVENT_ROUTINES_ENABLED"] = json!(enabled);
+ let config: Config = serde_json::from_value(values).unwrap();
+ assert_eq!(config.event_routines_enabled, enabled);
+ }
+ let mut values = config_values();
+ values["EVENT_ROUTINES_ENABLED"] = json!("enabled");
+ assert!(serde_json::from_value::<Config>(values).is_err());
+}
diff --git a/services/scheduled_action/src/domain.rs b/services/scheduled_action/src/domain.rs
index 901e625fda..be67b98ec2 100644
--- a/services/scheduled_action/src/domain.rs
+++ b/services/scheduled_action/src/domain.rs
@@ -1,3 +1,5 @@
+pub mod event_runs;
+pub mod event_trigger;
pub mod models;
pub mod ports;
pub mod service;
diff --git a/services/scheduled_action/src/domain/event_runs.rs b/services/scheduled_action/src/domain/event_runs.rs
new file mode 100644
index 0000000000..884e2435b6
--- /dev/null
+++ b/services/scheduled_action/src/domain/event_runs.rs
@@ -0,0 +1,435 @@
+//! Durable event-run contracts. Database transactions, Kafka acknowledgments and
+//! agent transport are adapter concerns; admission and authorization policy are
+//! domain concerns.
+
+use chrono::{DateTime, Utc};
+use entity_access::domain::models::{
+ EntityAccessReceipt, EntityType, RequiredPermission, ViewAccessLevel, ViewOnly,
+};
+use macro_user_id::user_id::MacroUserIdStr;
+use macro_uuid::{Uuid, generate_uuid_v7};
+use model_owner::Owner;
+use rootcause::Report;
+use serde::{Deserialize, Serialize};
+
+use super::event_trigger::{
+ EventEntityType, EventFilters, EventId, EventReference, EventRejection, IncomingEvent,
+};
+use super::models::{ActionExecutionRecord, ScheduledAction};
+
+pub mod admission;
+pub mod dispatch;
+
+#[cfg(test)]
+mod test_support;
+
+/// Independent of `updated_at`, which execution bookkeeping also changes.
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
+#[serde(try_from = "i64", into = "i64")]
+pub struct ConfigurationRevision(i64);
+
+impl ConfigurationRevision {
+ pub const INITIAL: Self = Self(1);
+
+ pub fn next(self) -> Result<Self, RunContractError> {
+ self.0
+ .checked_add(1)
+ .map(Self)
+ .ok_or(RunContractError::RevisionOverflow)
+ }
+
+ pub fn get(self) -> i64 {
+ self.0
+ }
+}
+
+impl TryFrom<i64> for ConfigurationRevision {
+ type Error = RunContractError;
+
+ fn try_from(value: i64) -> Result<Self, Self::Error> {
+ if value < 1 {
+ return Err(RunContractError::InvalidRevision);
+ }
+ Ok(Self(value))
+ }
+}
+
+impl From<ConfigurationRevision> for i64 {
+ fn from(revision: ConfigurationRevision) -> Self {
+ revision.0
+ }
+}
+
+/// A fresh token for each execution, including cron/manual claims. Release and
+/// finalization must compare it, so an expired worker cannot release a new run.
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
+#[serde(try_from = "Uuid", into = "Uuid")]
+pub struct ClaimToken(Uuid);
+
+impl ClaimToken {
+ pub fn generate() -> Self {
+ Self(generate_uuid_v7())
+ }
+ pub fn as_uuid(self) -> Uuid {
+ self.0
+ }
+}
+
+impl TryFrom<Uuid> for ClaimToken {
+ type Error = RunContractError;
+
+ fn try_from(value: Uuid) -> Result<Self, Self::Error> {
+ EventId::try_from(value).map_err(|_| RunContractError::InvalidClaimToken)?;
+ Ok(Self(value))
+ }
+}
+
+impl From<ClaimToken> for Uuid {
+ fn from(token: ClaimToken) -> Self {
+ token.0
+ }
+}
+
+/// Every repository scan and maintenance pass is explicitly bounded.
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub struct PageSize(u16);
+
+impl PageSize {
+ pub const MAX: u16 = 100;
+ pub fn get(self) -> u16 {
+ self.0
+ }
+}
+
+impl TryFrom<u16> for PageSize {
+ type Error = RunContractError;
+
+ fn try_from(value: u16) -> Result<Self, Self::Error> {
+ if value == 0 || value > Self::MAX {
+ return Err(RunContractError::InvalidPageSize);
+ }
+ Ok(Self(value))
+ }
+}
+
+#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
+pub enum RunContractError {
+ #[error("configuration revisions must be positive")]
+ InvalidRevision,
+ #[error("configuration revision overflow")]
+ RevisionOverflow,
+ #[error("claim tokens must be UUIDv7")]
+ InvalidClaimToken,
+ #[error("page sizes must be between 1 and 100")]
+ InvalidPageSize,
+}
+
+/// Natural deduplication key. A matching event has at most one run per action,
+/// even if several filters matched or the source is redelivered.
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
+pub struct EventRunKey {
+ pub action_id: Uuid,
+ pub event_id: EventId,
+}
+
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
+#[serde(rename_all = "snake_case")]
+pub enum CancellationReason {
+ AccessDenied,
+ Disabled,
+ Superseded,
+ NotUserOwned,
+}
+
+/// All outcomes are terminal. In particular, failure and interruption never
+/// return a started run to pending, even if no agent side effect is known.
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
+#[serde(tag = "type", rename_all = "snake_case")]
+pub enum EventRunOutcome {
+ Succeeded,
+ Failed,
+ Cancelled { reason: CancellationReason },
+ Interrupted,
+}
+
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+#[serde(tag = "state", rename_all = "snake_case")]
+pub enum EventRunState {
+ Pending,
+ Started {
+ token: ClaimToken,
+ started_at: DateTime<Utc>,
+ deadline: DateTime<Utc>,
+ },
+ Finished {
+ outcome: EventRunOutcome,
+ finished_at: DateTime<Utc>,
+ execution_record_id: Option<Uuid>,
+ },
+}
+
+/// Current configuration, reloaded before dispatch. A selector is not an access
+/// grant; the action's owner (not the event actor) must be authorized.
+#[derive(Debug, Clone)]
+pub struct EventActionConfiguration {
+ pub action_id: Uuid,
+ pub owner: Owner,
+ pub enabled: bool,
+ pub revision: ConfigurationRevision,
+ pub filters: EventFilters,
+ pub activated_at: DateTime<Utc>,
+}
+
+#[derive(Debug, Clone)]
+pub struct PendingEventRun {
+ pub action_id: Uuid,
+ pub revision: ConfigurationRevision,
+ pub event: EventReference,
+ pub admitted_at: DateTime<Utc>,
+}
+
+impl PendingEventRun {
+ pub fn key(&self) -> EventRunKey {
+ EventRunKey {
+ action_id: self.action_id,
+ event_id: self.event.event_id(),
+ }
+ }
+}
+
+/// Entity-specific view capability. Channel permission semantics are distinct
+/// from item access levels and must not be converted into document permissions.
+#[derive(Debug, Clone)]
+pub enum EventAccessCapability {
+ Document(EntityAccessReceipt<ViewAccessLevel>),
+ Channel(EntityAccessReceipt<ViewOnly>),
+}
+
+impl EventAccessCapability {
+ /// Require a directly authenticated owner, the exact entity, and its kind.
+ /// In particular, a bot acting on behalf of the owner is not the owner.
+ pub fn authorizes(&self, owner: &MacroUserIdStr<'static>, event: &EventReference) -> bool {
+ match (self, event.entity_type()) {
+ (Self::Document(receipt), EventEntityType::Document) => {
+ receipt_matches(receipt, owner, event, EntityType::Document)
+ }
+ (Self::Channel(receipt), EventEntityType::Channel) => {
+ receipt_matches(receipt, owner, event, EntityType::Channel)
+ }
+ _ => false,
+ }
+ }
+}
+
+fn receipt_matches<T: RequiredPermission>(
+ receipt: &EntityAccessReceipt<T>,
+ owner: &MacroUserIdStr<'static>,
+ event: &EventReference,
+ kind: EntityType,
+) -> bool {
+ receipt
+ .get_authenticated_user()
+ .is_ok_and(|user| user == owner)
+ && receipt.entity().entity_type == kind
+ && Uuid::parse_str(&receipt.entity().entity_id).ok() == Some(event.entity_id())
+}
+
+/// Receipt minted for the current action owner and triggering entity.
+pub struct AuthorizedEventRun {
+ pub pending: PendingEventRun,
+ pub access: EventAccessCapability,
+}
+
+impl AuthorizedEventRun {
+ /// Validate current configuration and receipt binding before the fenced
+ /// started transition. A revision race is checked again by the repository.
+ pub fn prepare(
+ pending: PendingEventRun,
+ configuration: &EventActionConfiguration,
+ access: EventAccessCapability,
+ ) -> Result<Self, CancellationReason> {
+ configuration.check_pending(&pending)?;
+ let Owner::User(owner) = &configuration.owner else {
+ return Err(CancellationReason::NotUserOwned);
+ };
+ if !access.authorizes(owner, &pending.event) {
+ return Err(CancellationReason::AccessDenied);
+ }
+ Ok(Self { pending, access })
+ }
+}
+
+impl EventActionConfiguration {
+ fn check_pending(&self, pending: &PendingEventRun) -> Result<(), CancellationReason> {
+ if !self.enabled {
+ return Err(CancellationReason::Disabled);
+ }
+ if self.action_id != pending.action_id
+ || self.revision != pending.revision
+ || !self.filters.matches(&pending.event, self.activated_at)
+ {
+ return Err(CancellationReason::Superseded);
+ }
+ if !matches!(self.owner, Owner::User(_)) {
+ return Err(CancellationReason::NotUserOwned);
+ }
+ Ok(())
+ }
+}
+
+/// Claim is the start linearization point. Configuration changes after this
+/// transition do not retroactively undo a run. Execution must not claim again.
+pub struct ClaimedEventRun {
+ pub run: AuthorizedEventRun,
+ pub action: ScheduledAction,
+ pub token: ClaimToken,
+ pub started_at: DateTime<Utc>,
+ pub deadline: DateTime<Utc>,
+}
+
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub enum AdmissionResult {
+ Admitted,
+ AlreadyPresent,
+ /// Deleted, disabled, changed trigger/revision or outside activation.
+ Ineligible,
+}
+
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub enum FinalizationResult {
+ Finalized,
+ AlreadyFinalized,
+ /// No writes allowed with a stale/foreign token (including claim release).
+ StaleClaim,
+}
+
+/// Runner result is data, not a transport error. Infrastructure failure after
+/// start is a failed/interrupted attempt, not permission to execute again.
+pub struct EventExecutionResult {
+ pub outcome: EventRunOutcome,
+ pub record: Option<ActionExecutionRecord>,
+}
+
+pub struct FinalizeEventRun {
+ pub key: EventRunKey,
+ pub token: ClaimToken,
+ pub finished_at: DateTime<Utc>,
+ pub execution: EventExecutionResult,
+}
+
+/// A bounded raw page, with invalid persisted configurations omitted.
+pub struct CandidateActionPage {
+ pub configurations: Vec<EventActionConfiguration>,
+ /// Last raw action ID, including invalid rows. None only at end of scan.
+ pub next_after: Option<Uuid>,
+}
+
+/// Durable queue port. Implementations own locking, deduplication and fencing,
+/// but not user authorization or event attribution policy.
+pub trait EventRunRepository: Send + Sync + 'static {
+ /// Keyset page ordered by action ID. May return indexed coarse candidates;
+ /// the domain must recheck exact filters and the activation boundary.
+ /// Advance by the raw-page cursor even when every configuration is invalid.
+ fn candidate_actions(
+ &self,
+ event: &EventReference,
+ after: Option<Uuid>,
+ limit: PageSize,
+ ) -> impl Future<Output = Result<CandidateActionPage, Report>> + Send;
+
+ /// None also covers actions that are no longer event-triggered.
+ fn current_configuration(
+ &self,
+ action_id: Uuid,
+ ) -> impl Future<Output = Result<Option<EventActionConfiguration>, Report>> + Send;
+
+ /// Atomically recheck enabled/event trigger/revision/activation and insert
+ /// ON CONFLICT DO NOTHING. Redelivery after partial fan-out is safe.
+ fn admit(
+ &self,
+ action_id: Uuid,
+ revision: ConfigurationRevision,
+ event: &EventReference,
+ ) -> impl Future<Output = Result<AdmissionResult, Report>> + Send;
+
+ /// Oldest pending run per available action, in durable admission order.
+ /// Busy actions must not starve independent actions out of this page.
+ fn pending_runs(
+ &self,
+ limit: PageSize,
+ ) -> impl Future<Output = Result<Vec<PendingEventRun>, Report>> + Send;
+
+ /// Atomically claim action + oldest pending run and transition to started.
+ /// Recheck revision, enabled/event trigger and no other active execution
+ /// (including cron/manual). None means the claim race was lost/ineligible.
+ fn claim(
+ &self,
+ run: AuthorizedEventRun,
+ token: ClaimToken,
+ started_at: DateTime<Utc>,
+ deadline: DateTime<Utc>,
+ ) -> impl Future<Output = Result<Option<ClaimedEventRun>, Report>> + Send;
+
+ /// Idempotent bookkeeping only: history, terminal outcome and fenced action
+ /// release must commit together. Retain the deduplication row until deletion.
+ fn finalize(
+ &self,
+ run: FinalizeEventRun,
+ ) -> impl Future<Output = Result<FinalizationResult, Report>> + Send;
+
+ /// Compare pending state and revision; never cancel a newer/started run.
+ fn cancel_pending(
+ &self,
+ key: EventRunKey,
+ revision: ConfigurationRevision,
+ reason: CancellationReason,
+ ) -> impl Future<Output = Result<(), Report>> + Send;
+
+ /// Bounded cleanup of disabled/superseded pending rows, and expired started
+ /// rows. Expired started rows become interrupted, never pending. Release
+ /// only their own claim tokens. Return the number of affected rows.
+ fn reconcile(
+ &self,
+ now: DateTime<Utc>,
+ limit: PageSize,
+ ) -> impl Future<Output = Result<u16, Report>> + Send;
+}
+
+/// None is a definitive denial/missing entity. Infrastructure unavailability is
+/// Err, never None. Implement with the owning entity-access receipt service.
+pub trait CurrentOwnerAccess: Send + Sync + 'static {
+ fn authorize(
+ &self,
+ owner: &MacroUserIdStr<'static>,
+ event: &EventReference,
+ ) -> impl Future<Output = Result<Option<EventAccessCapability>, Report>> + Send;
+}
+
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub enum EventIngestionResult {
+ /// All eligible candidates were durably handled (including no matches).
+ Admitted {
+ inserted: u64,
+ },
+ Rejected(EventRejection),
+}
+
+/// Kafka calls only this port. Ok permits acknowledgment; Err is transient and
+/// must not advance the source offset, including after partial fan-out.
+pub trait EventIngestion: Send + Sync + 'static {
+ fn ingest(
+ &self,
+ event: &IncomingEvent,
+ ) -> impl Future<Output = Result<EventIngestionResult, Report>> + Send;
+}
+
+/// Await completion of an already-started run; do not spawn untracked work.
+/// Enforce the claim deadline and propagate cancellation through agent/tools.
+/// Shutdown/timeouts are terminal interruption/failure, never scheduled retries.
+pub trait EventExecutor: Send + Sync + 'static {
+ fn execute(
+ &self,
+ run: &ClaimedEventRun,
+ cancellation: impl Future<Output = ()> + Send,
+ ) -> impl Future<Output = EventExecutionResult> + Send;
+}
diff --git a/services/scheduled_action/src/domain/event_runs/admission.rs b/services/scheduled_action/src/domain/event_runs/admission.rs
new file mode 100644
index 0000000000..573b0d6d95
--- /dev/null
+++ b/services/scheduled_action/src/domain/event_runs/admission.rs
@@ -0,0 +1,89 @@
+//! Normalize once, then durably admit each eligible user-owned action.
+
+use std::sync::Arc;
+
+use super::*;
+
+#[cfg(test)]
+mod test;
+
+/// Keyset paging bounds memory without truncating an event's fan-out. A failed
+/// page or access lookup leaves intake unacknowledged; redelivery deduplicates
+/// any earlier inserts.
+pub struct EventAdmissionService<R, A> {
+ repository: Arc<R>,
+ access: Arc<A>,
+ page_size: PageSize,
+}
+
+impl<R, A> EventAdmissionService<R, A> {
+ pub fn new(repository: Arc<R>, access: Arc<A>, page_size: PageSize) -> Self {
+ Self {
+ repository,
+ access,
+ page_size,
+ }
+ }
+}
+
+impl<R: EventRunRepository, A: CurrentOwnerAccess> EventIngestion for EventAdmissionService<R, A> {
+ async fn ingest(&self, incoming: &IncomingEvent) -> Result<EventIngestionResult, Report> {
+ let event = match incoming.normalize() {
+ Ok(event) => event,
+ Err(reason) => return Ok(EventIngestionResult::Rejected(reason)),
+ };
+ let mut after = None;
+ let mut inserted = 0;
+ loop {
+ let candidates = self
+ .repository
+ .candidate_actions(&event, after, self.page_size)
+ .await?;
+ let Some(next_after) = candidates.next_after else {
+ if !candidates.configurations.is_empty() {
+ return Err(rootcause::report!("event candidate page has no cursor"));
+ }
+ return Ok(EventIngestionResult::Admitted { inserted });
+ };
+ if after.is_some_and(|id| next_after <= id) {
+ return Err(rootcause::report!("event candidate cursor did not advance"));
+ }
+ for configuration in candidates.configurations {
+ // Enforce forward progress even for a broken repository adapter.
+ if after.is_some_and(|id| configuration.action_id <= id)
+ || configuration.action_id > next_after
+ {
+ return Err(rootcause::report!("event candidate page is not ordered"));
+ }
+ after = Some(configuration.action_id);
+ let pending = PendingEventRun {
+ action_id: configuration.action_id,
+ revision: configuration.revision,
+ event: event.clone(),
+ admitted_at: Utc::now(),
+ };
+ if configuration.check_pending(&pending).is_err() {
+ continue;
+ }
+ let Owner::User(owner) = &configuration.owner else {
+ continue;
+ };
+ let Some(access) = self.access.authorize(owner, &event).await? else {
+ continue;
+ };
+ if AuthorizedEventRun::prepare(pending, &configuration, access).is_err() {
+ continue;
+ }
+ if self
+ .repository
+ .admit(configuration.action_id, configuration.revision, &event)
+ .await?
+ == AdmissionResult::Admitted
+ {
+ inserted += 1;
+ }
+ }
+ after = Some(next_after);
+ }
+ }
+}
diff --git a/services/scheduled_action/src/domain/event_runs/admission/test.rs b/services/scheduled_action/src/domain/event_runs/admission/test.rs
new file mode 100644
index 0000000000..bba8ae2e13
--- /dev/null
+++ b/services/scheduled_action/src/domain/event_runs/admission/test.rs
@@ -0,0 +1,126 @@
+use super::super::test_support::*;
+use super::*;
+use serde_json::json;
+
+#[tokio::test]
+async fn pages_all_candidates_and_redelivery_deduplicates_using_owner_not_actor() {
+ let repo = Arc::new(Repo::default());
+ repo.0.lock().unwrap().configurations = (0..5).map(|_| configuration()).collect();
+ let access = Arc::new(Access::default());
+ let service = EventAdmissionService::new(repo.clone(), access.clone(), 2.try_into().unwrap());
+ let event = incoming();
+ assert_eq!(
+ service.ingest(&event).await.unwrap(),
+ EventIngestionResult::Admitted { inserted: 5 }
+ );
+ assert_eq!(repo.0.lock().unwrap().pages.len(), 4);
+ assert_eq!(
+ service.ingest(&event).await.unwrap(),
+ EventIngestionResult::Admitted { inserted: 0 }
+ );
+ assert_eq!(repo.0.lock().unwrap().pending.len(), 5);
+ assert!(
+ access
+ .calls
+ .lock()
+ .unwrap()
+ .iter()
+ .all(|owner| owner == &user())
+ );
+}
+
+#[tokio::test]
+async fn partial_fanout_failure_is_not_acknowledged_and_replay_is_safe() {
+ let repo = Arc::new(Repo::default());
+ repo.0.lock().unwrap().configurations = vec![configuration(), configuration()];
+ repo.0.lock().unwrap().fail_page = Some(2);
+ let service = EventAdmissionService::new(
+ repo.clone(),
+ Arc::new(Access::default()),
+ 1.try_into().unwrap(),
+ );
+ let event = incoming();
+ assert!(service.ingest(&event).await.is_err());
+ assert_eq!(repo.0.lock().unwrap().pending.len(), 1);
+ assert_eq!(
+ service.ingest(&event).await.unwrap(),
+ EventIngestionResult::Admitted { inserted: 1 }
+ );
+ assert_eq!(repo.0.lock().unwrap().pending.len(), 2);
+}
+
+#[tokio::test]
+async fn coarse_candidates_are_rechecked_before_access_or_admission() {
+ let repo = Arc::new(Repo::default());
+ let mut configs: Vec<_> = (0..5).map(|_| configuration()).collect();
+ configs[0].enabled = false;
+ configs[1].activated_at = Utc::now() + chrono::Duration::days(1);
+ configs[2].filters =
+ serde_json::from_value(json!([{"events":["document.updated"],"ids":[]}])).unwrap();
+ configs[3].owner = Owner::Team(generate_uuid_v7());
+ configs[4].owner =
+ Owner::from_principal_str("bot|01900000-0000-7000-8000-000000000004").unwrap();
+ repo.0.lock().unwrap().configurations = configs;
+ let access = Arc::new(Access::default());
+ let service = EventAdmissionService::new(repo, access.clone(), 100.try_into().unwrap());
+ assert_eq!(
+ service.ingest(&incoming()).await.unwrap(),
+ EventIngestionResult::Admitted { inserted: 0 }
+ );
+ assert!(access.calls.lock().unwrap().is_empty());
+}
+
+#[tokio::test]
+async fn permanent_rejections_do_not_query_candidates() {
+ let repo = Arc::new(Repo::default());
+ let service = EventAdmissionService::new(
+ repo.clone(),
+ Arc::new(Access::default()),
+ 10.try_into().unwrap(),
+ );
+ let mut event = incoming();
+ event.schema_version = 2;
+ assert_eq!(
+ service.ingest(&event).await.unwrap(),
+ EventIngestionResult::Rejected(EventRejection::UnsupportedSchema)
+ );
+ event.schema_version = 1;
+ let super::super::super::event_trigger::EventPayload::Document(ref mut payload) = event.payload
+ else {
+ unreachable!()
+ };
+ *payload = serde_json::from_value(json!({"event_type":"document.updated", "metadata": {
+ "document_id":generate_uuid_v7(),"share_permission_updated":false,
+ "actor":"bot|01900000-0000-7000-8000-000000000004",
+ "actor_user_id":"macro|[email protected]", "owner":"macro|[email protected]"
+ }}))
+ .unwrap();
+ assert_eq!(
+ service.ingest(&event).await.unwrap(),
+ EventIngestionResult::Rejected(EventRejection::UnsafeAttribution)
+ );
+ assert!(repo.0.lock().unwrap().pages.is_empty());
+}
+
+#[tokio::test]
+async fn denied_and_mismatched_receipts_skip_but_unavailable_access_defers_intake() {
+ let repo = Arc::new(Repo::default());
+ repo.0.lock().unwrap().configurations.push(configuration());
+ let access = Arc::new(Access::default());
+ let service = EventAdmissionService::new(repo.clone(), access.clone(), 10.try_into().unwrap());
+ *access.denied.lock().unwrap() = true;
+ assert_eq!(
+ service.ingest(&incoming()).await.unwrap(),
+ EventIngestionResult::Admitted { inserted: 0 }
+ );
+ *access.denied.lock().unwrap() = false;
+ *access.override_receipt.lock().unwrap() =
+ Some(capability(user(), &incoming().normalize().unwrap()));
+ assert_eq!(
+ service.ingest(&incoming()).await.unwrap(),
+ EventIngestionResult::Admitted { inserted: 0 }
+ );
+ *access.unavailable.lock().unwrap() = true;
+ assert!(service.ingest(&incoming()).await.is_err());
+ assert!(repo.0.lock().unwrap().pending.is_empty());
+}
diff --git a/services/scheduled_action/src/domain/event_runs/dispatch.rs b/services/scheduled_action/src/domain/event_runs/dispatch.rs
new file mode 100644
index 0000000000..ef3522873b
--- /dev/null
+++ b/services/scheduled_action/src/domain/event_runs/dispatch.rs
@@ -0,0 +1,141 @@
+//! Current-owner preparation, one fenced start, and terminal bookkeeping.
+
+use std::sync::Arc;
+
+use futures::FutureExt;
+
+use super::*;
+use crate::domain::models::MAX_ACTION_TIME;
+
+#[cfg(test)]
+mod test;
+
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub enum DispatchResult {
+ Cancelled(CancellationReason),
+ /// Another worker won, the action is busy, or configuration changed.
+ NotStarted,
+ Finished(FinalizationResult),
+}
+
+/// Worker-facing use cases; transport scheduling does not decide eligibility.
+pub trait EventRunDispatch: Send + Sync + 'static {
+ fn pending(
+ &self,
+ limit: PageSize,
+ ) -> impl Future<Output = Result<Vec<PendingEventRun>, Report>> + Send;
+ fn reconcile(&self, limit: PageSize) -> impl Future<Output = Result<u16, Report>> + Send;
+ /// Errors before claim leave work pending. Errors after claim leave it
+ /// started for deadline reconciliation, never available for execution again.
+ fn dispatch(
+ &self,
+ pending: PendingEventRun,
+ cancellation: impl Future<Output = ()> + Send,
+ ) -> impl Future<Output = Result<DispatchResult, Report>> + Send;
+}
+
+pub struct EventDispatchService<R, A, E> {
+ repository: Arc<R>,
+ access: Arc<A>,
+ executor: Arc<E>,
+}
+
+impl<R, A, E> EventDispatchService<R, A, E> {
+ pub fn new(repository: Arc<R>, access: Arc<A>, executor: Arc<E>) -> Self {
+ Self {
+ repository,
+ access,
+ executor,
+ }
+ }
+}
+
+impl<R: EventRunRepository, A: CurrentOwnerAccess, E: EventExecutor> EventDispatchService<R, A, E> {
+ async fn cancel(
+ &self,
+ pending: &PendingEventRun,
+ reason: CancellationReason,
+ ) -> Result<DispatchResult, Report> {
+ self.repository
+ .cancel_pending(pending.key(), pending.revision, reason)
+ .await?;
+ Ok(DispatchResult::Cancelled(reason))
+ }
+}
+
+impl<R: EventRunRepository, A: CurrentOwnerAccess, E: EventExecutor> EventRunDispatch
+ for EventDispatchService<R, A, E>
+{
+ async fn pending(&self, limit: PageSize) -> Result<Vec<PendingEventRun>, Report> {
+ self.repository.pending_runs(limit).await
+ }
+
+ async fn reconcile(&self, limit: PageSize) -> Result<u16, Report> {
+ self.repository.reconcile(Utc::now(), limit).await
+ }
+
+ async fn dispatch(
+ &self,
+ pending: PendingEventRun,
+ cancellation: impl Future<Output = ()> + Send,
+ ) -> Result<DispatchResult, Report> {
+ let Some(configuration) = self
+ .repository
+ .current_configuration(pending.action_id)
+ .await?
+ else {
+ return self.cancel(&pending, CancellationReason::Superseded).await;
+ };
+ if let Err(reason) = configuration.check_pending(&pending) {
+ return self.cancel(&pending, reason).await;
+ }
+ let Owner::User(owner) = &configuration.owner else {
+ return self
+ .cancel(&pending, CancellationReason::NotUserOwned)
+ .await;
+ };
+ // Unavailability propagates without cancelling or claiming queued work.
+ let Some(access) = self.access.authorize(owner, &pending.event).await? else {
+ return self
+ .cancel(&pending, CancellationReason::AccessDenied)
+ .await;
+ };
+ let authorized = match AuthorizedEventRun::prepare(pending.clone(), &configuration, access)
+ {
+ Ok(run) => run,
+ Err(reason) => return self.cancel(&pending, reason).await,
+ };
+ let mut cancellation = std::pin::pin!(cancellation);
+ // Authorization may have been in flight when intake stopped. Do not
+ // start a fresh claim after shutdown; already-started claims are terminal.
+ if cancellation.as_mut().now_or_never().is_some() {
+ return Ok(DispatchResult::NotStarted);
+ }
+ let started_at = Utc::now();
+ let Some(run) = self
+ .repository
+ .claim(
+ authorized,
+ ClaimToken::generate(),
+ started_at,
+ started_at + MAX_ACTION_TIME,
+ )
+ .await?
+ else {
+ return Ok(DispatchResult::NotStarted);
+ };
+ // The committed claim is the linearization point. Do not reload config
+ // or undo execution for a disable/update after this point.
+ let execution = self.executor.execute(&run, cancellation).await;
+ let finalized = self
+ .repository
+ .finalize(FinalizeEventRun {
+ key: run.run.pending.key(),
+ token: run.token,
+ finished_at: Utc::now(),
+ execution,
+ })
+ .await?;
+ Ok(DispatchResult::Finished(finalized))
+ }
+}
diff --git a/services/scheduled_action/src/domain/event_runs/dispatch/test.rs b/services/scheduled_action/src/domain/event_runs/dispatch/test.rs
new file mode 100644
index 0000000000..8c54bc185c
--- /dev/null
+++ b/services/scheduled_action/src/domain/event_runs/dispatch/test.rs
@@ -0,0 +1,336 @@
+use super::super::test_support::*;
+use super::*;
+use entity_access::domain::models::{
+ AccessLevel, BotReceiptAuth, BotReceiptScope, Entity, EntityAccessAuth, EntityPermission,
+};
+use serde_json::json;
+
+async fn dispatch(
+ service: &impl EventRunDispatch,
+ pending: PendingEventRun,
+) -> Result<DispatchResult, Report> {
+ service.dispatch(pending, std::future::pending()).await
+}
+
+#[tokio::test]
+async fn shutdown_before_claim_leaves_work_pending() {
+ let (repo, access, executor, pending) = setup();
+ let service = EventDispatchService::new(repo.clone(), access, executor.clone());
+ assert_eq!(
+ service
+ .dispatch(pending, std::future::ready(()))
+ .await
+ .unwrap(),
+ DispatchResult::NotStarted
+ );
+ assert!(repo.0.lock().unwrap().started.is_empty());
+ assert_eq!(repo.0.lock().unwrap().pending.len(), 1);
+ assert!(executor.calls.lock().unwrap().is_empty());
+}
+
+#[tokio::test]
+async fn access_revoked_after_admission_cancels_without_execution() {
+ let (repo, access, executor, pending) = setup();
+ *access.denied.lock().unwrap() = true;
+ let service = EventDispatchService::new(repo.clone(), access, executor.clone());
+ assert_eq!(
+ dispatch(&service, pending).await.unwrap(),
+ DispatchResult::Cancelled(CancellationReason::AccessDenied)
+ );
+ assert!(executor.calls.lock().unwrap().is_empty());
+ assert!(repo.0.lock().unwrap().started.is_empty());
+}
+
+#[tokio::test]
+async fn non_user_owner_never_reaches_authorization_or_execution() {
+ let (repo, access, executor, pending) = setup();
+ repo.0.lock().unwrap().configurations[0].owner = Owner::Team(generate_uuid_v7());
+ let service = EventDispatchService::new(repo, access.clone(), executor.clone());
+ assert_eq!(
+ dispatch(&service, pending).await.unwrap(),
+ DispatchResult::Cancelled(CancellationReason::NotUserOwned)
+ );
+ assert!(access.calls.lock().unwrap().is_empty());
+ assert!(executor.calls.lock().unwrap().is_empty());
+}
+
+#[tokio::test]
+async fn deletion_trigger_change_disable_and_reenable_cancel_obsolete_work() {
+ for change in 0..5 {
+ let (repo, access, executor, pending) = setup();
+ let reason = {
+ let mut state = repo.0.lock().unwrap();
+ match change {
+ // Deletion or a switch to cron returns no event configuration.
+ 0 | 1 => {
+ state.configurations.clear();
+ CancellationReason::Superseded
+ }
+ 2 => {
+ state.configurations[0].enabled = false;
+ CancellationReason::Disabled
+ }
+ 3 => {
+ // Re-enabled actions have a new revision even with identical filters.
+ state.configurations[0].revision = pending.revision.next().unwrap();
+ CancellationReason::Superseded
+ }
+ _ => {
+ state.configurations[0].filters =
+ serde_json::from_value(json!([{"events":["channel.created"]}])).unwrap();
+ CancellationReason::Superseded
+ }
+ }
+ };
+ let service = EventDispatchService::new(repo, access, executor.clone());
+ assert_eq!(
+ dispatch(&service, pending).await.unwrap(),
+ DispatchResult::Cancelled(reason)
+ );
+ assert!(executor.calls.lock().unwrap().is_empty());
+ }
+}
+
+#[tokio::test]
+async fn transient_prestart_errors_leave_pending_but_failed_execution_is_terminal() {
+ let (repo, access, executor, pending) = setup();
+ let service = EventDispatchService::new(repo.clone(), access.clone(), executor.clone());
+ *access.unavailable.lock().unwrap() = true;
+ assert!(dispatch(&service, pending.clone()).await.is_err());
+ *access.unavailable.lock().unwrap() = false;
+ repo.0.lock().unwrap().fail_claim = true;
+ assert!(dispatch(&service, pending.clone()).await.is_err());
+ assert_eq!(repo.0.lock().unwrap().pending.len(), 1);
+ assert!(executor.calls.lock().unwrap().is_empty());
+ repo.0.lock().unwrap().fail_claim = false;
+ executor
+ .outcomes
+ .lock()
+ .unwrap()
+ .push_back(EventRunOutcome::Failed);
+ assert_eq!(
+ dispatch(&service, pending.clone()).await.unwrap(),
+ DispatchResult::Finished(FinalizationResult::Finalized)
+ );
+ assert_eq!(
+ dispatch(&service, pending).await.unwrap(),
+ DispatchResult::NotStarted
+ );
+ assert_eq!(executor.calls.lock().unwrap().len(), 1);
+ assert_eq!(
+ repo.0.lock().unwrap().finished[0].1,
+ EventRunOutcome::Failed
+ );
+}
+
+#[tokio::test]
+async fn poststart_bookkeeping_failure_is_reconciled_without_reexecution() {
+ let (repo, access, executor, pending) = setup();
+ repo.0.lock().unwrap().fail_finalize = true;
+ let service = EventDispatchService::new(repo.clone(), access, executor.clone());
+ assert!(dispatch(&service, pending.clone()).await.is_err());
+ assert!(
+ service
+ .pending(10.try_into().unwrap())
+ .await
+ .unwrap()
+ .is_empty()
+ );
+ assert_eq!(
+ dispatch(&service, pending.clone()).await.unwrap(),
+ DispatchResult::NotStarted
+ );
+ assert_eq!(service.reconcile(10.try_into().unwrap()).await.unwrap(), 0);
+ repo.0.lock().unwrap().started[0].2 = Utc::now() - chrono::Duration::seconds(1);
+ assert_eq!(service.reconcile(10.try_into().unwrap()).await.unwrap(), 1);
+ assert_eq!(
+ repo.0.lock().unwrap().finished[0].1,
+ EventRunOutcome::Interrupted
+ );
+ assert_eq!(
+ dispatch(&service, pending).await.unwrap(),
+ DispatchResult::NotStarted
+ );
+ assert_eq!(executor.calls.lock().unwrap().len(), 1);
+}
+
+#[tokio::test]
+async fn claim_race_never_executes_but_disable_after_start_does_not_undo_run() {
+ let (repo, access, executor, pending) = setup();
+ let service = EventDispatchService::new(repo.clone(), access, executor.clone());
+ repo.0.lock().unwrap().lose_claim = true;
+ assert_eq!(
+ dispatch(&service, pending.clone()).await.unwrap(),
+ DispatchResult::NotStarted
+ );
+ assert!(executor.calls.lock().unwrap().is_empty());
+ repo.0.lock().unwrap().lose_claim = false;
+ repo.0.lock().unwrap().disable_after_claim = true;
+ assert_eq!(
+ dispatch(&service, pending).await.unwrap(),
+ DispatchResult::Finished(FinalizationResult::Finalized)
+ );
+ assert!(!repo.0.lock().unwrap().configurations[0].enabled);
+ assert_eq!(executor.calls.lock().unwrap().len(), 1);
+}
+
+#[tokio::test]
+async fn two_events_queue_during_execution_then_failure_does_not_block_next() {
+ let (repo, access, executor, first) = setup();
+ let configuration = repo.0.lock().unwrap().configurations[0].clone();
+ let authorized = AuthorizedEventRun::prepare(
+ first.clone(),
+ &configuration,
+ capability(user(), &first.event),
+ )
+ .unwrap();
+ let now = Utc::now();
+ let started = repo
+ .claim(
+ authorized,
+ ClaimToken::generate(),
+ now,
+ now + MAX_ACTION_TIME,
+ )
+ .await
+ .unwrap()
+ .unwrap();
+ let second = pending(&configuration);
+ let third = pending(&configuration);
+ repo.admit(second.action_id, second.revision, &second.event)
+ .await
+ .unwrap();
+ repo.admit(third.action_id, third.revision, &third.event)
+ .await
+ .unwrap();
+ let service = EventDispatchService::new(repo.clone(), access, executor.clone());
+ assert!(
+ service
+ .pending(10.try_into().unwrap())
+ .await
+ .unwrap()
+ .is_empty()
+ );
+ repo.finalize(FinalizeEventRun {
+ key: first.key(),
+ token: started.token,
+ finished_at: now,
+ execution: EventExecutionResult {
+ outcome: EventRunOutcome::Failed,
+ record: None,
+ },
+ })
+ .await
+ .unwrap();
+ executor
+ .outcomes
+ .lock()
+ .unwrap()
+ .push_back(EventRunOutcome::Failed);
+ let page = service.pending(10.try_into().unwrap()).await.unwrap();
+ assert_eq!(page[0].key(), second.key());
+ dispatch(&service, page[0].clone()).await.unwrap();
+ let page = service.pending(10.try_into().unwrap()).await.unwrap();
+ assert_eq!(page[0].key(), third.key());
+ dispatch(&service, page[0].clone()).await.unwrap();
+ assert_eq!(
+ *executor.calls.lock().unwrap(),
+ vec![second.key(), third.key()]
+ );
+ assert_eq!(
+ repo.0.lock().unwrap().finished[2].1,
+ EventRunOutcome::Succeeded
+ );
+}
+
+#[test]
+fn preparation_binds_receipt_to_authenticated_owner_entity_and_kind() {
+ let configuration = configuration();
+ let pending = pending(&configuration);
+ for auth in [
+ EntityAccessAuth::Internal,
+ EntityAccessAuth::Unauthenticated,
+ EntityAccessAuth::Authenticated(
+ MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap(),
+ ),
+ EntityAccessAuth::Bot(BotReceiptAuth::new(
+ entity_access::domain::models::BotIdStr::parse_from_str(
+ "bot|01900000-0000-7000-8000-000000000004",
+ )
+ .unwrap(),
+ BotReceiptScope::User {
+ acting_user: user(),
+ },
+ )),
+ ] {
+ let receipt = EntityAccessReceipt::try_new(
+ auth,
+ Entity {
+ entity_id: pending.event.entity_id().to_string(),
+ entity_type: EntityType::Document,
+ },
+ EntityPermission::AccessLevel {
+ access_level: AccessLevel::View,
+ },
+ )
+ .unwrap();
+ assert!(matches!(
+ AuthorizedEventRun::prepare(
+ pending.clone(),
+ &configuration,
+ EventAccessCapability::Document(receipt)
+ ),
+ Err(CancellationReason::AccessDenied)
+ ));
+ }
+ for (id, kind) in [
+ (generate_uuid_v7(), EntityType::Document),
+ (pending.event.entity_id(), EntityType::Chat),
+ ] {
+ let receipt = EntityAccessReceipt::try_new_authenticated_user(
+ user(),
+ Entity {
+ entity_id: id.to_string(),
+ entity_type: kind,
+ },
+ EntityPermission::AccessLevel {
+ access_level: AccessLevel::View,
+ },
+ )
+ .unwrap();
+ assert!(
+ AuthorizedEventRun::prepare(
+ pending.clone(),
+ &configuration,
+ EventAccessCapability::Document(receipt)
+ )
+ .is_err()
+ );
+ }
+ assert!(
+ AuthorizedEventRun::prepare(
+ pending.clone(),
+ &configuration,
+ capability(user(), &pending.event)
+ )
+ .is_ok()
+ );
+ let mut channel = pending.clone();
+ channel.event = serde_json::from_value(json!({"event_id": generate_uuid_v7(), "event_name":"channel.created", "entity_id": pending.event.entity_id(), "message_id":null})).unwrap();
+ assert!(
+ AuthorizedEventRun::prepare(
+ channel.clone(),
+ &configuration,
+ capability(user(), &pending.event)
+ )
+ .is_err()
+ );
+ assert!(
+ AuthorizedEventRun::prepare(
+ channel.clone(),
+ &configuration,
+ capability(user(), &channel.event)
+ )
+ .is_ok()
+ );
+}
diff --git a/services/scheduled_action/src/domain/event_runs/test_support.rs b/services/scheduled_action/src/domain/event_runs/test_support.rs
new file mode 100644
index 0000000000..31c2a0edd5
--- /dev/null
+++ b/services/scheduled_action/src/domain/event_runs/test_support.rs
@@ -0,0 +1,378 @@
+//! In-memory ports for domain policy tests. PostgreSQL locking/fencing has its
+//! own integration coverage in the repository adapter.
+
+use super::*;
+use crate::domain::{
+ event_trigger::{ActionTrigger, EventPayload},
+ models::{ActionKind, ScheduledAction},
+};
+use entity_access::domain::models::{AccessLevel, Entity, EntityPermission};
+use serde_json::json;
+use std::collections::VecDeque;
+use std::sync::{Arc, Mutex};
+
+pub fn user() -> MacroUserIdStr<'static> {
+ MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap()
+}
+
+pub fn incoming() -> IncomingEvent {
+ IncomingEvent {
+ event_id: generate_uuid_v7(),
+ schema_version: 1,
+ payload: EventPayload::Document(
+ serde_json::from_value(json!({
+ "event_type":"document.updated", "metadata": {
+ "document_id": generate_uuid_v7(), "owner":"macro|[email protected]",
+ "actor_user_id":"macro|[email protected]", "share_permission_updated":false
+ }
+ }))
+ .unwrap(),
+ ),
+ }
+}
+
+pub fn configuration() -> EventActionConfiguration {
+ EventActionConfiguration {
+ action_id: generate_uuid_v7(),
+ owner: Owner::User(user()),
+ enabled: true,
+ revision: ConfigurationRevision::INITIAL,
+ filters: serde_json::from_value(
+ json!([{"events":["document.updated", "channel.created"]}]),
+ )
+ .unwrap(),
+ activated_at: Utc::now() - chrono::Duration::days(1),
+ }
+}
+
+pub fn pending(configuration: &EventActionConfiguration) -> PendingEventRun {
+ PendingEventRun {
+ action_id: configuration.action_id,
+ revision: configuration.revision,
+ event: incoming().normalize().unwrap(),
+ admitted_at: Utc::now(),
+ }
+}
+
+pub fn capability(owner: MacroUserIdStr<'static>, event: &EventReference) -> EventAccessCapability {
+ let entity = Entity {
+ entity_id: event.entity_id().to_string(),
+ entity_type: match event.entity_type() {
+ EventEntityType::Document => EntityType::Document,
+ EventEntityType::Channel => EntityType::Channel,
+ },
+ };
+ match event.entity_type() {
+ EventEntityType::Document => EventAccessCapability::Document(
+ EntityAccessReceipt::try_new_authenticated_user(
+ owner,
+ entity,
+ EntityPermission::AccessLevel {
+ access_level: AccessLevel::View,
+ },
+ )
+ .unwrap(),
+ ),
+ EventEntityType::Channel => EventAccessCapability::Channel(
+ EntityAccessReceipt::try_new_authenticated_user(
+ owner,
+ entity,
+ EntityPermission::ChannelViewOnly,
+ )
+ .unwrap(),
+ ),
+ }
+}
+
+#[derive(Default)]
+pub struct Access {
+ pub denied: Mutex<bool>,
+ pub unavailable: Mutex<bool>,
+ pub override_receipt: Mutex<Option<EventAccessCapability>>,
+ pub calls: Mutex<Vec<MacroUserIdStr<'static>>>,
+}
+impl CurrentOwnerAccess for Access {
+ async fn authorize(
+ &self,
+ owner: &MacroUserIdStr<'static>,
+ event: &EventReference,
+ ) -> Result<Option<EventAccessCapability>, Report> {
+ self.calls.lock().unwrap().push(owner.clone());
+ if *self.unavailable.lock().unwrap() {
+ return Err(rootcause::report!("access unavailable"));
+ }
+ if *self.denied.lock().unwrap() {
+ return Ok(None);
+ }
+ Ok(Some(
+ self.override_receipt
+ .lock()
+ .unwrap()
+ .clone()
+ .unwrap_or_else(|| capability(owner.clone(), event)),
+ ))
+ }
+}
+
+#[derive(Default)]
+pub struct State {
+ pub configurations: Vec<EventActionConfiguration>,
+ pub pending: Vec<PendingEventRun>,
+ pub started: Vec<(PendingEventRun, ClaimToken, DateTime<Utc>)>,
+ pub finished: Vec<(EventRunKey, EventRunOutcome)>,
+ pub pages: Vec<Option<Uuid>>,
+ pub fail_page: Option<usize>,
+ pub fail_claim: bool,
+ pub lose_claim: bool,
+ pub fail_finalize: bool,
+ pub disable_after_claim: bool,
+}
+#[derive(Default)]
+pub struct Repo(pub Mutex<State>);
+impl EventRunRepository for Repo {
+ async fn candidate_actions(
+ &self,
+ _: &EventReference,
+ after: Option<Uuid>,
+ limit: PageSize,
+ ) -> Result<CandidateActionPage, Report> {
+ let mut state = self.0.lock().unwrap();
+ state.pages.push(after);
+ if state.fail_page == Some(state.pages.len()) {
+ return Err(rootcause::report!("page unavailable"));
+ }
+ let mut configs = state.configurations.clone();
+ configs.sort_by_key(|config| config.action_id);
+ let configurations: Vec<_> = configs
+ .into_iter()
+ .filter(|config| after.is_none_or(|id| config.action_id > id))
+ .take(limit.get().into())
+ .collect();
+ Ok(CandidateActionPage {
+ next_after: configurations.last().map(|config| config.action_id),
+ configurations,
+ })
+ }
+ async fn current_configuration(
+ &self,
+ id: Uuid,
+ ) -> Result<Option<EventActionConfiguration>, Report> {
+ Ok(self
+ .0
+ .lock()
+ .unwrap()
+ .configurations
+ .iter()
+ .find(|c| c.action_id == id)
+ .cloned())
+ }
+ async fn admit(
+ &self,
+ id: Uuid,
+ revision: ConfigurationRevision,
+ event: &EventReference,
+ ) -> Result<AdmissionResult, Report> {
+ let mut state = self.0.lock().unwrap();
+ let pending = PendingEventRun {
+ action_id: id,
+ revision,
+ event: event.clone(),
+ admitted_at: Utc::now(),
+ };
+ if state.pending.iter().any(|p| p.key() == pending.key())
+ || state
+ .started
+ .iter()
+ .any(|(p, _, _)| p.key() == pending.key())
+ || state.finished.iter().any(|(key, _)| *key == pending.key())
+ {
+ return Ok(AdmissionResult::AlreadyPresent);
+ }
+ state.pending.push(pending);
+ Ok(AdmissionResult::Admitted)
+ }
+ async fn pending_runs(&self, limit: PageSize) -> Result<Vec<PendingEventRun>, Report> {
+ let state = self.0.lock().unwrap();
+ let mut actions = Vec::new();
+ Ok(state
+ .pending
+ .iter()
+ .filter(|p| {
+ if actions.contains(&p.action_id)
+ || state
+ .started
+ .iter()
+ .any(|(s, _, _)| s.action_id == p.action_id)
+ {
+ return false;
+ }
+ actions.push(p.action_id);
+ true
+ })
+ .take(limit.get().into())
+ .cloned()
+ .collect())
+ }
+ async fn claim(
+ &self,
+ run: AuthorizedEventRun,
+ token: ClaimToken,
+ started_at: DateTime<Utc>,
+ deadline: DateTime<Utc>,
+ ) -> Result<Option<ClaimedEventRun>, Report> {
+ let mut state = self.0.lock().unwrap();
+ if state.fail_claim {
+ return Err(rootcause::report!("claim unavailable"));
+ }
+ if state.lose_claim
+ || state
+ .started
+ .iter()
+ .any(|(p, _, _)| p.action_id == run.pending.action_id)
+ {
+ return Ok(None);
+ }
+ let Some(config) = state
+ .configurations
+ .iter()
+ .find(|c| c.action_id == run.pending.action_id)
+ .cloned()
+ else {
+ return Ok(None);
+ };
+ if config.check_pending(&run.pending).is_err() {
+ return Ok(None);
+ }
+ let Some(index) = state
+ .pending
+ .iter()
+ .position(|p| p.action_id == run.pending.action_id)
+ else {
+ return Ok(None);
+ };
+ if state.pending[index].key() != run.pending.key() {
+ return Ok(None);
+ }
+ state.pending.remove(index);
+ state.started.push((run.pending.clone(), token, deadline));
+ if state.disable_after_claim {
+ state
+ .configurations
+ .iter_mut()
+ .find(|c| c.action_id == config.action_id)
+ .unwrap()
+ .enabled = false;
+ }
+ Ok(Some(ClaimedEventRun {
+ run,
+ token,
+ started_at,
+ deadline,
+ action: ScheduledAction {
+ id: Some(config.action_id),
+ owner: config.owner,
+ name: "routine".into(),
+ trigger: ActionTrigger::Events {
+ filters: config.filters,
+ },
+ kind: ActionKind::Agent,
+ task: json!({}),
+ created_at: started_at,
+ updated_at: started_at,
+ enabled: true,
+ configuration_revision: config.revision,
+ event_activated_at: Some(config.activated_at),
+ next_run_at: None,
+ claimed: Some(started_at),
+ },
+ }))
+ }
+ async fn finalize(&self, run: FinalizeEventRun) -> Result<FinalizationResult, Report> {
+ let mut state = self.0.lock().unwrap();
+ if state.fail_finalize {
+ return Err(rootcause::report!("bookkeeping unavailable"));
+ }
+ let Some(index) = state
+ .started
+ .iter()
+ .position(|(p, t, _)| p.key() == run.key && *t == run.token)
+ else {
+ return Ok(FinalizationResult::StaleClaim);
+ };
+ state.started.remove(index);
+ state.finished.push((run.key, run.execution.outcome));
+ Ok(FinalizationResult::Finalized)
+ }
+ async fn cancel_pending(
+ &self,
+ key: EventRunKey,
+ revision: ConfigurationRevision,
+ reason: CancellationReason,
+ ) -> Result<(), Report> {
+ let mut state = self.0.lock().unwrap();
+ if let Some(index) = state
+ .pending
+ .iter()
+ .position(|p| p.key() == key && p.revision == revision)
+ {
+ state.pending.remove(index);
+ state
+ .finished
+ .push((key, EventRunOutcome::Cancelled { reason }));
+ }
+ Ok(())
+ }
+ async fn reconcile(&self, now: DateTime<Utc>, limit: PageSize) -> Result<u16, Report> {
+ let mut state = self.0.lock().unwrap();
+ let mut count = 0;
+ while count < limit.get() {
+ let Some(index) = state
+ .started
+ .iter()
+ .position(|(_, _, deadline)| *deadline <= now)
+ else {
+ break;
+ };
+ let (pending, _, _) = state.started.remove(index);
+ state
+ .finished
+ .push((pending.key(), EventRunOutcome::Interrupted));
+ count += 1;
+ }
+ Ok(count)
+ }
+}
+
+#[derive(Default)]
+pub struct Executor {
+ pub calls: Mutex<Vec<EventRunKey>>,
+ pub outcomes: Mutex<VecDeque<EventRunOutcome>>,
+}
+impl EventExecutor for Executor {
+ async fn execute(
+ &self,
+ run: &ClaimedEventRun,
+ _: impl Future<Output = ()> + Send,
+ ) -> EventExecutionResult {
+ self.calls.lock().unwrap().push(run.run.pending.key());
+ assert!(run.run.access.authorizes(&user(), &run.run.pending.event));
+ EventExecutionResult {
+ outcome: self
+ .outcomes
+ .lock()
+ .unwrap()
+ .pop_front()
+ .unwrap_or(EventRunOutcome::Succeeded),
+ record: None,
+ }
+ }
+}
+
+pub fn setup() -> (Arc<Repo>, Arc<Access>, Arc<Executor>, PendingEventRun) {
+ let config = configuration();
+ let pending = pending(&config);
+ let repo = Arc::new(Repo::default());
+ repo.0.lock().unwrap().configurations.push(config);
+ repo.0.lock().unwrap().pending.push(pending.clone());
+ (repo, Arc::default(), Arc::default(), pending)
+}
diff --git a/services/scheduled_action/src/domain/event_trigger.rs b/services/scheduled_action/src/domain/event_trigger.rs
new file mode 100644
index 0000000000..79e08981f8
--- /dev/null
+++ b/services/scheduled_action/src/domain/event_trigger.rs
@@ -0,0 +1,433 @@
+//! Routine-owned event eligibility and exact selector matching.
+//!
+//! Adapters strip the broker envelope into [`IncomingEvent`]. Only explicitly
+//! allowlisted, human-authored events can become compact [`EventReference`]s.
+
+use activity::Actor;
+use channels::domain::broker_events::ChannelTopicEvent;
+use chrono::{DateTime, Utc};
+use chrono_tz::Tz;
+use documents::domain::events::DocumentTopicEvent;
+use macro_user_id::user_id::MacroUserIdStr;
+use macro_uuid::Uuid;
+use serde::{Deserialize, Serialize};
+use utoipa::ToSchema;
+
+use super::models::Schedule;
+
+#[cfg(test)]
+mod test;
+
+/// Limits apply before deduplication, including repeated values.
+pub const MAX_FILTERS: usize = 32;
+pub const MAX_EVENTS_PER_FILTER: usize = 7;
+pub const MAX_IDS_PER_FILTER: usize = 100;
+
+/// Exactly one trigger per action. Existing cron validation is reused.
+#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
+#[serde(tag = "type", rename_all = "snake_case", deny_unknown_fields)]
+pub enum ActionTrigger {
+ Cron {
+ schedule: Schedule,
+ #[schema(value_type = String)]
+ timezone: Tz,
+ },
+ Events {
+ filters: EventFilters,
+ },
+}
+
+/// Closed allowlist: unknown names, deletions and ambiguous attribution are not
+/// selectors. Adding a broker variant does not automatically enable routines.
+#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, ToSchema)]
+pub enum EventName {
+ #[serde(rename = "document.created")]
+ DocumentCreated,
+ #[serde(rename = "document.updated")]
+ DocumentUpdated,
+ #[serde(rename = "channel.created")]
+ ChannelCreated,
+ #[serde(rename = "channel.message_posted")]
+ ChannelMessagePosted,
+ #[serde(rename = "channel.mentioned")]
+ ChannelMentioned,
+ #[serde(rename = "channel.message_patched")]
+ ChannelMessagePatched,
+ #[serde(rename = "channel.message_attachment_created")]
+ ChannelMessageAttachmentCreated,
+}
+
+impl EventName {
+ pub const fn as_str(self) -> &'static str {
+ match self {
+ Self::DocumentCreated => "document.created",
+ Self::DocumentUpdated => "document.updated",
+ Self::ChannelCreated => "channel.created",
+ Self::ChannelMessagePosted => "channel.message_posted",
+ Self::ChannelMentioned => "channel.mentioned",
+ Self::ChannelMessagePatched => "channel.message_patched",
+ Self::ChannelMessageAttachmentCreated => "channel.message_attachment_created",
+ }
+ }
+
+ pub const fn entity_type(self) -> EventEntityType {
+ match self {
+ Self::DocumentCreated | Self::DocumentUpdated => EventEntityType::Document,
+ _ => EventEntityType::Channel,
+ }
+ }
+
+ const fn has_message(self) -> bool {
+ !matches!(
+ self,
+ Self::DocumentCreated | Self::DocumentUpdated | Self::ChannelCreated
+ )
+ }
+}
+
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
+#[serde(rename_all = "snake_case")]
+pub enum EventEntityType {
+ Document,
+ Channel,
+}
+
+#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
+pub enum FilterValidationError {
+ #[error("expected between 1 and 32 filters")]
+ FilterCount,
+ #[error("expected between 1 and 7 event names per filter")]
+ EventCount,
+ #[error("expected at most 100 entity IDs per filter")]
+ IdCount,
+}
+
+/// An event name AND an entity ID must match within the same filter.
+#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, ToSchema)]
+#[serde(try_from = "FilterInput")]
+pub struct EventFilter {
+ events: Vec<EventName>,
+ #[serde(skip_serializing_if = "Option::is_none")]
+ #[schema(value_type = Option<Vec<String>>)]
+ ids: Option<Vec<Uuid>>,
+}
+
+#[derive(Deserialize)]
+#[serde(deny_unknown_fields)]
+struct FilterInput {
+ events: Vec<EventName>,
+ #[serde(default)]
+ ids: Option<Vec<Uuid>>,
+}
+
+impl TryFrom<FilterInput> for EventFilter {
+ type Error = FilterValidationError;
+
+ fn try_from(input: FilterInput) -> Result<Self, Self::Error> {
+ Self::new(input.events, input.ids)
+ }
+}
+
+impl EventFilter {
+ pub fn new(
+ mut events: Vec<EventName>,
+ mut ids: Option<Vec<Uuid>>,
+ ) -> Result<Self, FilterValidationError> {
+ if events.is_empty() || events.len() > MAX_EVENTS_PER_FILTER {
+ return Err(FilterValidationError::EventCount);
+ }
+ if ids
+ .as_ref()
+ .is_some_and(|ids| ids.len() > MAX_IDS_PER_FILTER)
+ {
+ return Err(FilterValidationError::IdCount);
+ }
+ events.sort_unstable();
+ events.dedup();
+ if let Some(ids) = &mut ids {
+ ids.sort_unstable();
+ ids.dedup();
+ }
+ Ok(Self { events, ids })
+ }
+
+ pub fn events(&self) -> &[EventName] {
+ &self.events
+ }
+
+ /// `None` matches every ID; `Some([])` matches nothing.
+ pub fn ids(&self) -> Option<&[Uuid]> {
+ self.ids.as_deref()
+ }
+
+ pub fn accepts(&self, name: EventName, entity_id: Uuid) -> bool {
+ self.events.contains(&name) && self.ids.as_ref().is_none_or(|ids| ids.contains(&entity_id))
+ }
+}
+
+/// Bounded nonempty OR of filters, canonicalized to remove duplicates.
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
+#[serde(try_from = "Vec<EventFilter>", into = "Vec<EventFilter>")]
+pub struct EventFilters(Vec<EventFilter>);
+
+impl TryFrom<Vec<EventFilter>> for EventFilters {
+ type Error = FilterValidationError;
+
+ fn try_from(mut filters: Vec<EventFilter>) -> Result<Self, Self::Error> {
+ if filters.is_empty() || filters.len() > MAX_FILTERS {
+ return Err(FilterValidationError::FilterCount);
+ }
+ filters.sort_unstable();
+ filters.dedup();
+ Ok(Self(filters))
+ }
+}
+
+impl From<EventFilters> for Vec<EventFilter> {
+ fn from(filters: EventFilters) -> Self {
+ filters.0
+ }
+}
+
+impl EventFilters {
+ pub fn as_slice(&self) -> &[EventFilter] {
+ &self.0
+ }
+
+ /// Publication time comes only from the UUIDv7, never ingestion or metadata
+ /// timestamps. The activation boundary is inclusive.
+ pub fn matches(&self, event: &EventReference, activated_at: DateTime<Utc>) -> bool {
+ event.published_at() >= activated_at
+ && self
+ .0
+ .iter()
+ .any(|filter| filter.accepts(event.event_name, event.entity_id))
+ }
+}
+
+/// A verified RFC UUIDv7 whose embedded publication timestamp is representable.
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
+#[serde(try_from = "Uuid", into = "Uuid")]
+pub struct EventId(Uuid);
+
+impl TryFrom<Uuid> for EventId {
+ type Error = EventRejection;
+
+ fn try_from(id: Uuid) -> Result<Self, Self::Error> {
+ // Check the RFC variant as well as the version nibble.
+ if id.get_version_num() != 7 || id.as_bytes()[8] & 0xc0 != 0x80 {
+ return Err(EventRejection::InvalidIdentity);
+ }
+ let timestamp = id.get_timestamp().ok_or(EventRejection::InvalidIdentity)?;
+ let (seconds, nanos) = timestamp.to_unix();
+ let seconds = i64::try_from(seconds).map_err(|_| EventRejection::InvalidIdentity)?;
+ DateTime::<Utc>::from_timestamp(seconds, nanos).ok_or(EventRejection::InvalidIdentity)?;
+ Ok(Self(id))
+ }
+}
+
+impl From<EventId> for Uuid {
+ fn from(id: EventId) -> Self {
+ id.0
+ }
+}
+
+impl EventId {
+ pub fn as_uuid(self) -> Uuid {
+ self.0
+ }
+
+ pub fn published_at(self) -> DateTime<Utc> {
+ let (seconds, nanos) = self.0.get_timestamp().expect("validated UUIDv7").to_unix();
+ DateTime::from_timestamp(seconds as i64, nanos).expect("validated publication timestamp")
+ }
+}
+
+/// Minimal durable context. No content, actor, owner or full envelope is kept.
+/// The entity kind and publication timestamp derive from the name and event ID.
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+#[serde(try_from = "EventReferenceInput")]
+pub struct EventReference {
+ event_id: EventId,
+ event_name: EventName,
+ entity_id: Uuid,
+ message_id: Option<Uuid>,
+}
+
+#[derive(Deserialize)]
+#[serde(deny_unknown_fields)]
+struct EventReferenceInput {
+ event_id: EventId,
+ event_name: EventName,
+ entity_id: Uuid,
+ message_id: Option<Uuid>,
+}
+
+impl TryFrom<EventReferenceInput> for EventReference {
+ type Error = EventRejection;
+
+ fn try_from(input: EventReferenceInput) -> Result<Self, Self::Error> {
+ if input.event_name.has_message() != input.message_id.is_some() {
+ return Err(EventRejection::InvalidContext);
+ }
+ Ok(Self {
+ event_id: input.event_id,
+ event_name: input.event_name,
+ entity_id: input.entity_id,
+ message_id: input.message_id,
+ })
+ }
+}
+
+impl EventReference {
+ pub fn event_id(&self) -> EventId {
+ self.event_id
+ }
+ pub fn event_name(&self) -> EventName {
+ self.event_name
+ }
+ pub fn entity_type(&self) -> EventEntityType {
+ self.event_name.entity_type()
+ }
+ pub fn entity_id(&self) -> Uuid {
+ self.entity_id
+ }
+ pub fn message_id(&self) -> Option<Uuid> {
+ self.message_id
+ }
+ pub fn published_at(&self) -> DateTime<Utc> {
+ self.event_id.published_at()
+ }
+}
+
+/// Domain event payloads only; no broker keys, offsets, messages or clients.
+#[derive(Debug, Clone)]
+pub enum EventPayload {
+ Document(DocumentTopicEvent),
+ Channel(ChannelTopicEvent),
+}
+
+#[derive(Debug, Clone)]
+pub struct IncomingEvent {
+ pub event_id: Uuid,
+ pub schema_version: u8,
+ pub payload: EventPayload,
+}
+
+/// Permanent rejection: consumers may advance after recording this disposition.
+#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
+pub enum EventRejection {
+ #[error("unsupported event schema")]
+ UnsupportedSchema,
+ #[error("unsupported event name")]
+ UnsupportedEvent,
+ #[error("event attribution is not unambiguously human")]
+ UnsafeAttribution,
+ #[error("event identity is not a verifiable UUIDv7")]
+ InvalidIdentity,
+ #[error("invalid entity UUID")]
+ InvalidEntityId,
+ #[error("event context does not match its event name")]
+ InvalidContext,
+}
+
+fn require_human(actor: Option<&Actor<'static>>, delegated: bool) -> Result<(), EventRejection> {
+ if !delegated && actor.is_some_and(|actor| actor.as_user().is_some()) {
+ Ok(())
+ } else {
+ Err(EventRejection::UnsafeAttribution)
+ }
+}
+
+fn document_actor(
+ actor: Option<&Actor<'static>>,
+ fallback: Option<&MacroUserIdStr<'static>>,
+ delegated: bool,
+) -> Result<(), EventRejection> {
+ if actor.is_some() || delegated {
+ require_human(actor, delegated)
+ } else if fallback.is_some() {
+ Ok(())
+ } else {
+ Err(EventRejection::UnsafeAttribution)
+ }
+}
+
+impl IncomingEvent {
+ /// Fail closed on attribution, identity and schema. Wildcard arms are
+ /// deliberate: future source variants remain non-triggering by default.
+ pub fn normalize(&self) -> Result<EventReference, EventRejection> {
+ // Both currently supported source schemas are version 1. A producer
+ // version change requires reviewing eligibility before accepting it.
+ if self.schema_version != 1 {
+ return Err(EventRejection::UnsupportedSchema);
+ }
+ let event_id = EventId::try_from(self.event_id)?;
+ let (event_name, entity_id, message_id) = match &self.payload {
+ EventPayload::Document(event) => {
+ let (name, id) = match event {
+ DocumentTopicEvent::Created(data) => {
+ require_human(data.actor.as_ref(), data.on_behalf_of.is_some())?;
+ (EventName::DocumentCreated, &data.document_id)
+ }
+ DocumentTopicEvent::Updated(data) => {
+ document_actor(
+ data.actor.as_ref(),
+ data.actor_user_id.as_ref(),
+ data.on_behalf_of.is_some(),
+ )?;
+ (EventName::DocumentUpdated, &data.document_id)
+ }
+ _ => return Err(EventRejection::UnsupportedEvent),
+ };
+ let id = Uuid::parse_str(id).map_err(|_| EventRejection::InvalidEntityId)?;
+ (name, id, None)
+ }
+ EventPayload::Channel(event) => match event {
+ ChannelTopicEvent::Created(data) => {
+ require_human(Some(&data.actor), data.on_behalf_of.is_some())?;
+ (EventName::ChannelCreated, data.channel_id, None)
+ }
+ ChannelTopicEvent::MessagePosted(data) => {
+ require_human(Some(&data.sender), data.triggered_by.is_some())?;
+ (
+ EventName::ChannelMessagePosted,
+ data.channel_id,
+ Some(data.message_id),
+ )
+ }
+ ChannelTopicEvent::Mentioned(data) => {
+ require_human(Some(&data.sender), false)?;
+ (
+ EventName::ChannelMentioned,
+ data.channel_id,
+ Some(data.message_id),
+ )
+ }
+ ChannelTopicEvent::MessagePatched(data) => {
+ require_human(Some(&data.actor), false)?;
+ (
+ EventName::ChannelMessagePatched,
+ data.channel_id,
+ Some(data.message_id),
+ )
+ }
+ ChannelTopicEvent::MessageAttachmentCreated(data) => {
+ require_human(Some(&data.actor), false)?;
+ (
+ EventName::ChannelMessageAttachmentCreated,
+ data.channel_id,
+ Some(data.message_id),
+ )
+ }
+ _ => return Err(EventRejection::UnsupportedEvent),
+ },
+ };
+ Ok(EventReference {
+ event_id,
+ event_name,
+ entity_id,
+ message_id,
+ })
+ }
+}
diff --git a/services/scheduled_action/src/domain/event_trigger/test.rs b/services/scheduled_action/src/domain/event_trigger/test.rs
new file mode 100644
index 0000000000..b4c6aeddea
--- /dev/null
+++ b/services/scheduled_action/src/domain/event_trigger/test.rs
@@ -0,0 +1,326 @@
+use super::*;
+use serde_json::{Value, json};
+
+const ENTITY_ID: &str = "01900000-0000-7000-8000-000000000001";
+const OTHER_ID: &str = "01900000-0000-7000-8000-000000000002";
+const EVENT_ID: &str = "01900000-0000-7000-8000-000000000003";
+const HUMAN: &str = "macro|[email protected]";
+const BOT: &str = "bot|01900000-0000-7000-8000-000000000004";
+
+fn metadata() -> Value {
+ json!({
+ "document_id": ENTITY_ID, "owner": HUMAN, "actor": HUMAN,
+ "actor_user_id": HUMAN, "document_name": "Example", "file_type": "md",
+ "share_permission_updated": false, "source_document_id": OTHER_ID,
+ "reason": "edited", "channel_id": ENTITY_ID, "message_id": OTHER_ID,
+ "sender": HUMAN, "channel_type": "public", "participant_user_ids": [HUMAN],
+ "content": "Untrusted content must not survive normalization",
+ "mentions": [], "attachments": [],
+ "mentioned": {"entity_type": "user", "entity_id": HUMAN},
+ "created_at": "2024-01-01T00:00:00Z", "updated_at": "2024-01-01T00:00:00Z",
+ "added_by": HUMAN, "added_user_ids": [HUMAN],
+ "removed_by": HUMAN, "removed_user_ids": [HUMAN]
+ })
+}
+
+fn incoming(name: &str, mut metadata: Value) -> IncomingEvent {
+ // Updates encode a FileTypeUpdate, unlike the other document events.
+ if name == "document.updated" {
+ metadata["file_type"] = Value::Null;
+ }
+ let value = json!({"event_type": name, "metadata": metadata});
+ let payload = if name.starts_with("document.") {
+ EventPayload::Document(serde_json::from_value(value).unwrap())
+ } else {
+ EventPayload::Channel(serde_json::from_value(value).unwrap())
+ };
+ IncomingEvent {
+ event_id: Uuid::parse_str(EVENT_ID).unwrap(),
+ schema_version: 1,
+ payload,
+ }
+}
+
+fn filters(value: Value) -> EventFilters {
+ serde_json::from_value(value).unwrap()
+}
+
+#[test]
+fn all_allowlisted_human_events_normalize_to_minimal_context() {
+ for name in [
+ "document.created",
+ "document.updated",
+ "channel.created",
+ "channel.message_posted",
+ "channel.mentioned",
+ "channel.message_patched",
+ "channel.message_attachment_created",
+ ] {
+ let event = incoming(name, metadata()).normalize().unwrap();
+ assert_eq!(event.event_name().as_str(), name);
+ assert_eq!(event.entity_id().to_string(), ENTITY_ID);
+ assert_eq!(
+ event.message_id().is_some(),
+ name.starts_with("channel.") && name != "channel.created"
+ );
+ let encoded = serde_json::to_value(&event).unwrap();
+ assert!(encoded.get("content").is_none());
+ assert!(encoded.get("actor").is_none());
+ assert_eq!(
+ serde_json::from_value::<EventReference>(encoded).unwrap(),
+ event
+ );
+ }
+}
+
+#[test]
+fn explicit_bots_never_fall_back_to_human_owner_actor_or_subject() {
+ for (name, field) in [
+ ("document.created", "actor"),
+ ("document.updated", "actor"),
+ ("channel.created", "actor"),
+ ("channel.message_posted", "sender"),
+ ("channel.mentioned", "sender"),
+ ("channel.message_patched", "actor"),
+ ("channel.message_attachment_created", "actor"),
+ ] {
+ for delegated in [false, true] {
+ let mut data = metadata();
+ data[field] = json!(BOT);
+ if delegated {
+ data["on_behalf_of"] = json!(HUMAN);
+ }
+ assert_eq!(
+ incoming(name, data).normalize(),
+ Err(EventRejection::UnsafeAttribution)
+ );
+ }
+ }
+}
+
+#[test]
+fn missing_creation_attribution_and_delegation_are_not_human_authorship() {
+ let mut data = metadata();
+ data.as_object_mut().unwrap().remove("actor");
+ assert_eq!(
+ incoming("document.created", data.clone()).normalize(),
+ Err(EventRejection::UnsafeAttribution)
+ );
+ assert!(
+ incoming("document.updated", data.clone())
+ .normalize()
+ .is_ok()
+ );
+ data["on_behalf_of"] = json!(HUMAN);
+ assert_eq!(
+ incoming("document.updated", data).normalize(),
+ Err(EventRejection::UnsafeAttribution)
+ );
+ for name in ["document.created", "document.updated", "channel.created"] {
+ let mut data = metadata();
+ data["on_behalf_of"] = json!(HUMAN);
+ assert_eq!(
+ incoming(name, data).normalize(),
+ Err(EventRejection::UnsafeAttribution)
+ );
+ }
+ let mut data = metadata();
+ data["triggered_by"] = json!(HUMAN);
+ assert_eq!(
+ incoming("channel.message_posted", data).normalize(),
+ Err(EventRejection::UnsafeAttribution)
+ );
+}
+
+#[test]
+fn update_prefers_explicit_actor_and_requires_a_fallback_when_absent() {
+ let mut data = metadata();
+ data["actor_user_id"] = Value::Null;
+ assert!(
+ incoming("document.updated", data.clone())
+ .normalize()
+ .is_ok()
+ );
+ data["actor"] = Value::Null;
+ assert_eq!(
+ incoming("document.updated", data).normalize(),
+ Err(EventRejection::UnsafeAttribution)
+ );
+ let mut data = metadata();
+ data["actor_user_id"] = json!("macro|[email protected]");
+ assert!(incoming("document.updated", data).normalize().is_ok());
+}
+
+#[test]
+fn every_non_allowlisted_variant_is_rejected() {
+ for name in [
+ "document.deleted",
+ "document.content_uploaded",
+ "document.sync_content_updated",
+ "document.purged",
+ "document.copied",
+ "document.interaction",
+ "channel.updated",
+ "channel.deleted",
+ "channel.message_deleted",
+ "channel.message_attachment_removed",
+ "channel.participant_added",
+ "channel.participant_removed",
+ ] {
+ assert_eq!(
+ incoming(name, metadata()).normalize(),
+ Err(EventRejection::UnsupportedEvent)
+ );
+ assert!(serde_json::from_value::<EventName>(json!(name)).is_err());
+ }
+ assert!(serde_json::from_value::<EventName>(json!("document.future_event")).is_err());
+ assert!(serde_json::from_value::<EventName>(json!("document.*")).is_err());
+}
+
+#[test]
+fn validates_schema_identity_and_document_uuid() {
+ let mut event = incoming("document.updated", metadata());
+ for version in [0, 2, 255] {
+ event.schema_version = version;
+ assert_eq!(event.normalize(), Err(EventRejection::UnsupportedSchema));
+ }
+ event.schema_version = 1;
+ for id in [
+ "00000000-0000-0000-0000-000000000000",
+ "01900000-0000-4000-8000-000000000003",
+ "01900000-0000-7000-0000-000000000003",
+ ] {
+ event.event_id = Uuid::parse_str(id).unwrap();
+ assert_eq!(event.normalize(), Err(EventRejection::InvalidIdentity));
+ }
+ let mut data = metadata();
+ data["document_id"] = json!("not-a-uuid");
+ assert_eq!(
+ incoming("document.created", data).normalize(),
+ Err(EventRejection::InvalidEntityId)
+ );
+}
+
+#[test]
+fn matching_is_same_filter_and_empty_ids_match_nothing() {
+ let event = incoming("document.created", metadata())
+ .normalize()
+ .unwrap();
+ let activation = event.published_at();
+ let separate = filters(json!([
+ {"events": ["document.created"], "ids": [OTHER_ID]},
+ {"events": ["document.updated"], "ids": [ENTITY_ID]}
+ ]));
+ assert!(!separate.matches(&event, activation));
+ for ids in [Value::Null, json!([ENTITY_ID])] {
+ assert!(
+ filters(json!([{"events": ["document.created"], "ids": ids}]))
+ .matches(&event, activation)
+ );
+ }
+ assert!(filters(json!([{"events": ["document.created"]}])).matches(&event, activation));
+ assert!(
+ !filters(json!([{"events": ["document.created"], "ids": []}])).matches(&event, activation)
+ );
+}
+
+#[test]
+fn duplicates_do_not_multiply_matches_and_pre_activation_events_do_not_match() {
+ let event = incoming("document.created", metadata())
+ .normalize()
+ .unwrap();
+ let filter =
+ json!({"events": ["document.created", "document.created"], "ids": [ENTITY_ID, ENTITY_ID]});
+ let filters = filters(json!([filter, filter]));
+ assert_eq!(filters.as_slice().len(), 1);
+ assert_eq!(filters.as_slice()[0].events().len(), 1);
+ assert_eq!(filters.as_slice()[0].ids().unwrap().len(), 1);
+ assert!(filters.matches(&event, event.published_at()));
+ assert!(filters.matches(
+ &event,
+ event.published_at() - chrono::Duration::milliseconds(1)
+ ));
+ assert!(!filters.matches(
+ &event,
+ event.published_at() + chrono::Duration::milliseconds(1)
+ ));
+}
+
+#[test]
+fn filter_validation_cannot_be_bypassed_by_deserialization() {
+ for value in [
+ json!([]),
+ json!([{"events": []}]),
+ json!([{"events": ["document.copied"]}]),
+ json!([{"events": ["document.created"], "ids": ["invalid"]}]),
+ json!([{"events": ["document.created"], "unexpected": true}]),
+ json!(vec![
+ json!({"events": ["document.created"]});
+ MAX_FILTERS + 1
+ ]),
+ json!([{"events": vec!["document.created"; MAX_EVENTS_PER_FILTER + 1]}]),
+ json!([{"events": ["document.created"], "ids": vec![ENTITY_ID; MAX_IDS_PER_FILTER + 1]}]),
+ ] {
+ assert!(serde_json::from_value::<EventFilters>(value).is_err());
+ }
+ assert!(serde_json::from_value::<EventFilters>(json!([
+ {"events": vec!["document.created"; MAX_EVENTS_PER_FILTER], "ids": vec![ENTITY_ID; MAX_IDS_PER_FILTER]}
+ ])).is_ok());
+}
+
+#[test]
+fn stored_context_cannot_bypass_identity_or_message_shape_validation() {
+ let event = incoming("channel.message_posted", metadata())
+ .normalize()
+ .unwrap();
+ let mut stored = serde_json::to_value(event).unwrap();
+ stored["message_id"] = Value::Null;
+ assert!(serde_json::from_value::<EventReference>(stored.clone()).is_err());
+ stored["event_name"] = json!("document.created");
+ assert!(serde_json::from_value::<EventReference>(stored.clone()).is_ok());
+ stored["event_id"] = json!("01900000-0000-4000-8000-000000000003");
+ assert!(serde_json::from_value::<EventReference>(stored).is_err());
+}
+
+#[test]
+fn run_revisions_claim_tokens_and_page_sizes_are_validated() {
+ use crate::domain::event_runs::{ClaimToken, ConfigurationRevision, EventRunState, PageSize};
+
+ assert_eq!(ConfigurationRevision::INITIAL.next().unwrap().get(), 2);
+ for value in [0, -1] {
+ assert!(serde_json::from_value::<ConfigurationRevision>(json!(value)).is_err());
+ }
+ assert!(
+ ConfigurationRevision::try_from(i64::MAX)
+ .unwrap()
+ .next()
+ .is_err()
+ );
+ let first = ClaimToken::generate();
+ let second = ClaimToken::generate();
+ assert_ne!(first, second);
+ assert_eq!(
+ serde_json::from_value::<ClaimToken>(serde_json::to_value(first).unwrap()).unwrap(),
+ first
+ );
+ assert!(ClaimToken::try_from(Uuid::nil()).is_err());
+ assert!(PageSize::try_from(0).is_err());
+ assert!(PageSize::try_from(PageSize::MAX + 1).is_err());
+ assert_eq!(
+ PageSize::try_from(PageSize::MAX).unwrap().get(),
+ PageSize::MAX
+ );
+ assert!(serde_json::from_value::<EventRunState>(json!({"state": "retry_scheduled"})).is_err());
+}
+
+#[test]
+fn tagged_trigger_round_trips_and_rejects_conflicting_fields() {
+ for value in [
+ json!({"type": "cron", "schedule": "0 0 9 * * *", "timezone": "UTC"}),
+ json!({"type": "events", "filters": [{"events": ["document.created"]}]}),
+ ] {
+ let trigger: ActionTrigger = serde_json::from_value(value.clone()).unwrap();
+ assert_eq!(serde_json::to_value(trigger).unwrap(), value);
+ }
+ assert!(serde_json::from_value::<ActionTrigger>(json!({"type": "events", "filters": [{"events": ["document.created"]}], "schedule": "0 0 9 * * *"})).is_err());
+}
diff --git a/services/scheduled_action/src/domain/models.rs b/services/scheduled_action/src/domain/models.rs
index b96c42ef63..7e2a4f1de4 100644
--- a/services/scheduled_action/src/domain/models.rs
+++ b/services/scheduled_action/src/domain/models.rs
@@ -10,6 +10,9 @@ use serde_json::Value;
use std::str::FromStr;
use utoipa::ToSchema;
+use super::event_runs::ConfigurationRevision;
+use super::event_trigger::ActionTrigger;
+
#[cfg(test)]
mod test;
@@ -53,7 +56,7 @@ impl<'de> Deserialize<'de> for Schedule {
}
}
-#[derive(Debug, Serialize, Deserialize, Clone, ToSchema)]
+#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, ToSchema)]
pub enum ActionKind {
Agent,
}
@@ -65,26 +68,22 @@ pub struct AgentTask {
pub user_prompt: String,
}
-/// Client-supplied payload for creating a scheduled action. The server fills
-/// in `id`, `owner` (from the authenticated user), timestamps, `claimed`, and
-/// `next_run_at` (derived from the cron).
+/// Canonical client configuration. Ownership and execution state are server-owned.
#[derive(Serialize, Deserialize, Debug, Clone, ToSchema)]
-pub struct CreateScheduledAction {
+#[serde(deny_unknown_fields)]
+pub struct ActionConfiguration {
pub name: String,
- pub schedule: Schedule,
+ pub trigger: ActionTrigger,
pub kind: ActionKind,
- #[schema(value_type = String)]
- pub timezone: Tz,
#[schema(value_type = Object)]
pub task: Value,
pub enabled: bool,
}
-/// Client-supplied payload for updating a scheduled action. Mirrors the fields
-/// the repository actually writes — `id`/`owner`/timestamps/`claimed`/
-/// `next_run_at` are not client-mutable.
+/// Deprecated cron-only input, accepted during the compatibility rollout.
#[derive(Serialize, Deserialize, Debug, Clone, ToSchema)]
-pub struct UpdateScheduledAction {
+#[serde(deny_unknown_fields)]
+pub struct LegacyActionConfiguration {
pub name: String,
pub schedule: Schedule,
pub kind: ActionKind,
@@ -95,6 +94,68 @@ pub struct UpdateScheduledAction {
pub enabled: bool,
}
+impl From<LegacyActionConfiguration> for ActionConfiguration {
+ fn from(input: LegacyActionConfiguration) -> Self {
+ Self {
+ name: input.name,
+ trigger: ActionTrigger::Cron {
+ schedule: input.schedule,
+ timezone: input.timezone,
+ },
+ kind: input.kind,
+ task: input.task,
+ enabled: input.enabled,
+ }
+ }
+}
+
+/// Exactly one representation is accepted, even if mixed fields agree or are null.
+#[derive(Serialize, Deserialize, Debug, Clone, ToSchema)]
+#[serde(untagged)]
+pub enum CreateScheduledAction {
+ Canonical(ActionConfiguration),
+ Legacy(LegacyActionConfiguration),
+}
+
+/// Full replacement of client configuration, not of server-owned action state.
+#[derive(Serialize, Deserialize, Debug, Clone, ToSchema)]
+#[serde(untagged)]
+pub enum UpdateScheduledAction {
+ Canonical(ActionConfiguration),
+ Legacy(LegacyActionConfiguration),
+}
+
+impl From<CreateScheduledAction> for ActionConfiguration {
+ fn from(input: CreateScheduledAction) -> Self {
+ match input {
+ CreateScheduledAction::Canonical(input) => input,
+ CreateScheduledAction::Legacy(input) => input.into(),
+ }
+ }
+}
+
+impl From<UpdateScheduledAction> for ActionConfiguration {
+ fn from(input: UpdateScheduledAction) -> Self {
+ match input {
+ UpdateScheduledAction::Canonical(input) => input,
+ UpdateScheduledAction::Legacy(input) => input.into(),
+ }
+ }
+}
+
+/// Expected management failures; adapters map these without exposing internals.
+#[derive(Debug, thiserror::Error)]
+pub enum ActionPolicyError {
+ #[error("scheduled action not found")]
+ NotFound,
+ #[error("schedule has no future firings")]
+ NoFutureFirings,
+ #[error("event-trigger management is not enabled")]
+ EventManagementDisabled,
+ #[error("scheduled action changed or is running; reload before updating")]
+ UpdateConflict,
+}
+
#[derive(Serialize, Deserialize, Debug, Clone, ToSchema)]
pub struct ScheduledAction {
#[schema(value_type = Option<String>, format = Uuid)]
@@ -106,19 +167,21 @@ pub struct ScheduledAction {
#[schema(value_type = String)]
pub owner: Owner,
pub name: String,
- pub schedule: Schedule,
+ pub trigger: ActionTrigger,
pub kind: ActionKind,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
- #[schema(value_type = String)]
- pub timezone: Tz,
+ /// Independent of execution bookkeeping in `updated_at`.
+ #[schema(value_type = i64)]
+ pub configuration_revision: ConfigurationRevision,
+ /// Event publication boundary; absent for cron actions.
+ pub event_activated_at: Option<DateTime<Utc>>,
#[schema(value_type = Object)]
pub task: Value,
pub claimed: Option<DateTime<Utc>>,
- /// Time of the next scheduled firing (derived from the cron on write). UI
- /// uses this to render "next run" without having to parse the cron itself.
- pub next_run_at: DateTime<Utc>,
- /// When false, the cron dispatcher skips this schedule. `run_now` remains
+ /// Next cron firing, absent for event-triggered actions.
+ pub next_run_at: Option<DateTime<Utc>>,
+ /// When false, automatic dispatch skips this action. `run_now` remains
/// available regardless.
pub enabled: bool,
}
diff --git a/services/scheduled_action/src/domain/models/test.rs b/services/scheduled_action/src/domain/models/test.rs
index f4e86ce418..cd87e2b22a 100644
--- a/services/scheduled_action/src/domain/models/test.rs
+++ b/services/scheduled_action/src/domain/models/test.rs
@@ -3,7 +3,37 @@ use macro_uuid::{Uuid, generate_uuid_v7};
use model_owner::{Owner, OwnerType};
use serde_json::json;
-use super::{ActionKind, Schedule, ScheduledAction};
+use super::{
+ ActionConfiguration, ActionKind, CreateScheduledAction, Schedule, ScheduledAction,
+ UpdateScheduledAction,
+};
+use crate::domain::event_runs::ConfigurationRevision;
+use crate::domain::event_trigger::ActionTrigger;
+
+#[test]
+fn both_request_representations_normalize_to_the_same_configuration() {
+ let legacy = json!({"name":"routine", "kind":"Agent", "task":{}, "enabled":true,
+ "schedule":"0 0 9 * * *", "timezone":"UTC"});
+ let canonical = json!({"name":"routine", "kind":"Agent", "task":{}, "enabled":true,
+ "trigger":{"type":"cron", "schedule":"0 0 9 * * *", "timezone":"UTC"}});
+ for input in [legacy, canonical] {
+ let create: CreateScheduledAction = serde_json::from_value(input.clone()).unwrap();
+ let update: UpdateScheduledAction = serde_json::from_value(input).unwrap();
+ let create = serde_json::to_value(ActionConfiguration::from(create)).unwrap();
+ let update = serde_json::to_value(ActionConfiguration::from(update)).unwrap();
+ assert_eq!(create, update);
+ assert_eq!(create["trigger"]["type"], "cron");
+ }
+}
+
+#[test]
+fn mixed_representations_are_rejected_even_when_the_values_agree() {
+ let input = json!({"name":"routine", "kind":"Agent", "task":{}, "enabled":true,
+ "schedule":"0 0 9 * * *", "timezone":"UTC",
+ "trigger":{"type":"cron", "schedule":"0 0 9 * * *", "timezone":"UTC"}});
+ assert!(serde_json::from_value::<CreateScheduledAction>(input.clone()).is_err());
+ assert!(serde_json::from_value::<UpdateScheduledAction>(input).is_err());
+}
const DAILY_9AM: &str = "0 0 9 * * *";
const USER_PRINCIPAL: &str = "macro|[email protected]";
@@ -19,14 +49,15 @@ fn action_owned_by(owner: Owner) -> ScheduledAction {
id: Some(generate_uuid_v7()),
owner,
name: "standup".to_string(),
- schedule,
+ trigger: ActionTrigger::Cron { schedule, timezone },
kind: ActionKind::Agent,
created_at: now,
updated_at: now,
- timezone,
+ configuration_revision: ConfigurationRevision::INITIAL,
+ event_activated_at: None,
task: json!({}),
claimed: None,
- next_run_at,
+ next_run_at: Some(next_run_at),
enabled: true,
}
}
@@ -97,4 +128,32 @@ fn owner_serializes_as_the_bare_principal_string() {
let decoded: ScheduledAction =
serde_json::from_value(encoded).expect("action should round-trip");
assert_eq!(decoded.owner, action.owner);
+ assert!(matches!(decoded.trigger, ActionTrigger::Cron { .. }));
+ assert!(decoded.next_run_at.is_some());
+ assert_eq!(decoded.event_activated_at, None);
+}
+
+#[test]
+fn event_action_round_trips_without_cron_fields() {
+ let mut action = action_owned_by(Owner::from_principal_str(USER_PRINCIPAL).unwrap());
+ action.trigger = serde_json::from_value(json!({
+ "type": "events",
+ "filters": [{ "events": ["document.created"] }]
+ }))
+ .unwrap();
+ action.next_run_at = None;
+ action.event_activated_at = Some(Utc::now());
+ let encoded = serde_json::to_value(&action).unwrap();
+ assert_eq!(encoded["trigger"]["type"], "events");
+ assert_eq!(encoded["next_run_at"], json!(null));
+ assert!(encoded.get("schedule").is_none());
+ assert!(encoded.get("timezone").is_none());
+ let decoded: ScheduledAction = serde_json::from_value(encoded).unwrap();
+ assert!(matches!(decoded.trigger, ActionTrigger::Events { .. }));
+ assert_eq!(decoded.next_run_at, None);
+ assert_eq!(decoded.event_activated_at, action.event_activated_at);
+ assert_eq!(
+ decoded.configuration_revision,
+ ConfigurationRevision::INITIAL
+ );
}
diff --git a/services/scheduled_action/src/domain/ports.rs b/services/scheduled_action/src/domain/ports.rs
index 57c79e88e1..3f34a76295 100644
--- a/services/scheduled_action/src/domain/ports.rs
+++ b/services/scheduled_action/src/domain/ports.rs
@@ -1,6 +1,8 @@
+use super::event_runs::ClaimToken;
+use super::event_trigger::EventReference;
use super::models::{
- ActionExecutionRecord, DispatchEvent, InProgressExecution, ScheduledAction,
- ScheduledActionUpdate,
+ ActionExecutionRecord, CreateScheduledAction, DispatchEvent, InProgressExecution,
+ ScheduledAction, ScheduledActionUpdate, UpdateScheduledAction,
};
use anyhow::Result;
use chrono::{DateTime, Utc};
@@ -19,7 +21,14 @@ pub trait ScheduledActionRepo: Send + Sync + 'static {
user_id: MacroUserIdStr<'static>,
) -> impl Future<Output = Result<Vec<ScheduledAction>>> + Send;
- /// Return the next `limit` enabled actions ordered by `next_run_at` ASC,
+ /// Look up one action owned by the caller, independently of list filtering.
+ fn get_action(
+ &self,
+ id: &Uuid,
+ user_id: MacroUserIdStr<'static>,
+ ) -> impl Future<Output = Result<Option<ScheduledAction>>> + Send;
+
+ /// Return the next `limit` enabled cron actions ordered by `next_run_at` ASC,
/// filtering out those currently claimed by another worker (i.e. claimed
/// within `MAX_ACTION_TIME`). Used by the polling dispatcher to find work.
fn get_next_unclaimed_actions(
@@ -27,6 +36,10 @@ pub trait ScheduledActionRepo: Send + Sync + 'static {
limit: i64,
) -> impl Future<Output = Result<Vec<ScheduledAction>>> + Send;
+ /// Atomically replace configuration only when the stored revision is the
+ /// predecessor of the supplied revision. While claimed, only disabling with
+ /// otherwise identical configuration is allowed. Return UpdateConflict on
+ /// stale revisions or a concurrent claim; never overwrite execution state.
fn update_action(
&self,
action: ScheduledAction,
@@ -38,9 +51,14 @@ pub trait ScheduledActionRepo: Send + Sync + 'static {
macro_user_id: MacroUserIdStr<'static>,
) -> impl Future<Output = Result<()>> + Send;
- fn claim_action(&self, id: &Uuid) -> impl Future<Output = Result<()>> + Send;
+ fn claim_action(&self, id: &Uuid) -> impl Future<Output = Result<ClaimToken>> + Send;
- fn release_action(&self, id: &Uuid) -> impl Future<Output = Result<()>> + Send;
+ /// Release only this execution's claim; stale tokens must not mutate a newer run.
+ fn release_action(
+ &self,
+ id: &Uuid,
+ token: ClaimToken,
+ ) -> impl Future<Output = Result<()>> + Send;
fn create_execution_record(
&self,
@@ -52,6 +70,7 @@ pub trait ScheduledActionRepo: Send + Sync + 'static {
action_id: &Uuid,
) -> impl Future<Output = Result<Vec<ActionExecutionRecord>>> + Send;
+ /// Advance the cron firing time; event actions are left unchanged.
fn update_next_run_at(&self, id: &Uuid) -> impl Future<Output = Result<()>> + Send;
fn update_last_executed(
@@ -64,17 +83,22 @@ pub trait ScheduledActionRepo: Send + Sync + 'static {
pub trait ScheduledActionService: Send + Sync + 'static {
fn create_action(
&self,
- action: ScheduledAction,
+ input: CreateScheduledAction,
+ user_id: MacroUserIdStr<'static>,
) -> impl Future<Output = Result<ScheduledAction>> + Send;
+ /// Legacy clients see cron actions only. Backend clients opt into events.
+ /// ID-based operations and workers must never authorize through this list.
fn get_actions(
&self,
user_id: MacroUserIdStr<'static>,
+ include_events: bool,
) -> impl Future<Output = Result<Vec<ScheduledAction>>> + Send;
fn update_action(
&self,
- action: ScheduledAction,
+ id: &Uuid,
+ input: UpdateScheduledAction,
macro_user_id: MacroUserIdStr<'static>,
) -> impl Future<Output = Result<ScheduledAction>> + Send;
@@ -108,6 +132,19 @@ pub trait ScheduledActionExecutor {
) -> impl Future<Output = Result<InProgressExecution>> + Send;
}
+/// Agent execution dependencies, separate from claim/history orchestration.
+/// Dropping `run` must cancel its agent session and tool request context.
+pub trait ScheduledAgentRunner: Send + Sync + 'static {
+ fn create_chat(&self, action: &ScheduledAction) -> impl Future<Output = Result<String>> + Send;
+
+ fn run(
+ &self,
+ action: &ScheduledAction,
+ chat_id: &str,
+ event: Option<&EventReference>,
+ ) -> impl Future<Output = Result<()>> + Send;
+}
+
pub trait ScheduledActionLiveUpdate: Send + Sync + 'static {
fn publish_update(&self, update: ScheduledActionUpdate) -> impl Future<Output = ()> + Send;
}
diff --git a/services/scheduled_action/src/domain/service.rs b/services/scheduled_action/src/domain/service.rs
index 430aa4bcaa..8750a818ca 100644
--- a/services/scheduled_action/src/domain/service.rs
+++ b/services/scheduled_action/src/domain/service.rs
@@ -1,36 +1,92 @@
use std::sync::Arc;
-use anyhow::{Result, bail};
+use anyhow::Result;
+use chrono::{DateTime, Utc};
use macro_user_id::user_id::MacroUserIdStr;
use macro_uuid::Uuid;
+use model_owner::Owner;
use tokio::sync::mpsc::Sender;
-use super::models::{ActionExecutionRecord, DispatchEvent, InProgressExecution, ScheduledAction};
+use super::event_runs::ConfigurationRevision;
+use super::event_trigger::ActionTrigger;
+use super::models::{
+ ActionConfiguration, ActionExecutionRecord, ActionPolicyError, CreateScheduledAction,
+ DispatchEvent, InProgressExecution, MAX_ACTION_TIME, ScheduledAction, UpdateScheduledAction,
+};
use super::ports::{ScheduledActionExecutor, ScheduledActionRepo, ScheduledActionService};
-/// True when `action` is the caller's own action.
-///
-/// The repository already filters by the caller's principal, so this is the
-/// second half of the same statement: the row is only theirs when its owner is
-/// that user rather than a bot or a team that happens to share the string.
-fn is_owned_by(action: &ScheduledAction, caller: &MacroUserIdStr<'static>) -> bool {
- action.owner.is_user(caller)
-}
+#[cfg(test)]
+pub(crate) mod test;
pub struct ScheduledActionServiceImpl<Rpo, Exe> {
repo: Arc<Rpo>,
executor: Arc<Exe>,
dispatcher_tx: Sender<DispatchEvent>,
+ event_management_enabled: bool,
}
-impl<Rpo, Exe> ScheduledActionServiceImpl<Rpo, Exe> {
+impl<Rpo: ScheduledActionRepo, Exe> ScheduledActionServiceImpl<Rpo, Exe> {
+ /// Event management defaults off until explicitly enabled by composition.
pub fn new(repo: Arc<Rpo>, executor: Arc<Exe>, dispatcher_tx: Sender<DispatchEvent>) -> Self {
Self {
repo,
executor,
dispatcher_tx,
+ event_management_enabled: false,
+ }
+ }
+
+ pub fn with_event_management_enabled(mut self, enabled: bool) -> Self {
+ self.event_management_enabled = enabled;
+ self
+ }
+
+ async fn owned_action(
+ &self,
+ id: &Uuid,
+ caller: &MacroUserIdStr<'static>,
+ ) -> Result<ScheduledAction> {
+ match self.repo.get_action(id, caller.clone()).await? {
+ Some(action) if action.owner.is_user(caller) => Ok(action),
+ _ => Err(ActionPolicyError::NotFound.into()),
}
}
+
+ fn check_event_management(&self, trigger: &ActionTrigger) -> Result<()> {
+ if matches!(trigger, ActionTrigger::Events { .. }) && !self.event_management_enabled {
+ return Err(ActionPolicyError::EventManagementDisabled.into());
+ }
+ Ok(())
+ }
+}
+
+fn next_run(trigger: &ActionTrigger) -> Result<Option<DateTime<Utc>>> {
+ match trigger {
+ ActionTrigger::Cron { schedule, timezone } => schedule
+ .next_run_after_now(*timezone)
+ .map(Some)
+ .ok_or_else(|| ActionPolicyError::NoFutureFirings.into()),
+ // Filters are validated value objects: neither deserialization nor Rust
+ // constructors can supply unbounded, empty or unsupported selectors.
+ ActionTrigger::Events { .. } => Ok(None),
+ }
+}
+
+fn same_trigger(left: &ActionTrigger, right: &ActionTrigger) -> bool {
+ match (left, right) {
+ (
+ ActionTrigger::Cron {
+ schedule: a,
+ timezone: at,
+ },
+ ActionTrigger::Cron {
+ schedule: b,
+ timezone: bt,
+ },
+ ) => a.as_str() == b.as_str() && at == bt,
+ (ActionTrigger::Events { filters: a }, ActionTrigger::Events { filters: b }) => a == b,
+ _ => false,
+ }
}
impl<Rpo, Exe> ScheduledActionService for ScheduledActionServiceImpl<Rpo, Exe>
@@ -38,8 +94,37 @@ where
Rpo: ScheduledActionRepo,
Exe: ScheduledActionExecutor + Send + Sync + 'static,
{
- async fn create_action(&self, action: ScheduledAction) -> Result<ScheduledAction> {
- let created = self.repo.create_action(action).await?;
+ async fn create_action(
+ &self,
+ input: CreateScheduledAction,
+ user_id: MacroUserIdStr<'static>,
+ ) -> Result<ScheduledAction> {
+ let input = ActionConfiguration::from(input);
+ self.check_event_management(&input.trigger)?;
+ let now = Utc::now();
+ let next_run_at = next_run(&input.trigger)?;
+ let event_activated_at = match &input.trigger {
+ ActionTrigger::Events { .. } => Some(now),
+ ActionTrigger::Cron { .. } => None,
+ };
+ let created = self
+ .repo
+ .create_action(ScheduledAction {
+ id: None,
+ owner: Owner::User(user_id),
+ name: input.name,
+ trigger: input.trigger,
+ kind: input.kind,
+ task: input.task,
+ enabled: input.enabled,
+ created_at: now,
+ updated_at: now,
+ configuration_revision: ConfigurationRevision::INITIAL,
+ event_activated_at,
+ next_run_at,
+ claimed: None,
+ })
+ .await?;
self.dispatcher_tx
.send(DispatchEvent::Create(created.clone()))
.await
@@ -47,25 +132,68 @@ where
Ok(created)
}
- async fn get_actions(&self, user_id: MacroUserIdStr<'static>) -> Result<Vec<ScheduledAction>> {
- self.repo.get_actions(user_id).await
+ async fn get_actions(
+ &self,
+ user_id: MacroUserIdStr<'static>,
+ include_events: bool,
+ ) -> Result<Vec<ScheduledAction>> {
+ Ok(self
+ .repo
+ .get_actions(user_id.clone())
+ .await?
+ .into_iter()
+ .filter(|action| action.owner.is_user(&user_id))
+ .filter(|action| include_events || matches!(action.trigger, ActionTrigger::Cron { .. }))
+ .collect())
}
async fn update_action(
&self,
- action: ScheduledAction,
+ id: &Uuid,
+ input: UpdateScheduledAction,
macro_user_id: MacroUserIdStr<'static>,
) -> Result<ScheduledAction> {
- let Some(id) = action.id else {
- bail!("cannot update action without id");
- };
- let actions = self.repo.get_actions(macro_user_id.clone()).await?;
- if !actions
- .iter()
- .any(|a| a.id.as_ref() == Some(&id) && is_owned_by(a, &macro_user_id))
+ let mut action = self.owned_action(id, &macro_user_id).await?;
+ let input = ActionConfiguration::from(input);
+ let trigger_changed = !same_trigger(&action.trigger, &input.trigger);
+ let configuration_changed = trigger_changed
+ || action.name != input.name
+ || action.kind != input.kind
+ || action.task != input.task;
+ let disable_only = !input.enabled && !configuration_changed;
+ // A rollout gate must not prevent an owner from stopping an existing
+ // event action. Deletion, history and explicit manual runs also remain available.
+ if !disable_only {
+ self.check_event_management(&action.trigger)?;
+ self.check_event_management(&input.trigger)?;
+ }
+ let now = Utc::now();
+ if action
+ .claimed
+ .is_some_and(|claimed| claimed >= now - MAX_ACTION_TIME)
+ && !disable_only
{
- bail!("scheduled action {id} not found for user");
+ return Err(ActionPolicyError::UpdateConflict.into());
}
+ if !disable_only {
+ action.next_run_at = next_run(&input.trigger)?;
+ }
+ action.event_activated_at = match &input.trigger {
+ ActionTrigger::Cron { .. } => None,
+ ActionTrigger::Events { .. }
+ if trigger_changed || (!action.enabled && input.enabled) =>
+ {
+ Some(now)
+ }
+ ActionTrigger::Events { .. } => action.event_activated_at,
+ };
+ action.configuration_revision = action.configuration_revision.next()?;
+ action.name = input.name;
+ action.trigger = input.trigger;
+ action.kind = input.kind;
+ action.task = input.task;
+ action.enabled = input.enabled;
+ action.updated_at = now;
let updated = self.repo.update_action(action).await?;
self.dispatcher_tx
@@ -76,14 +204,7 @@ where
}
async fn delete_action(&self, id: &Uuid, macro_user_id: MacroUserIdStr<'static>) -> Result<()> {
- let actions = self.repo.get_actions(macro_user_id.clone()).await?;
- let Some(action) = actions
- .into_iter()
- .find(|a| a.id.as_ref() == Some(id) && is_owned_by(a, &macro_user_id))
- else {
- bail!("scheduled action {id} not found for user");
- };
-
+ let action = self.owned_action(id, &macro_user_id).await?;
self.repo.delete_action(id, macro_user_id).await?;
self.dispatcher_tx
.send(DispatchEvent::Delete(action))
@@ -97,14 +218,9 @@ where
id: &Uuid,
macro_user_id: MacroUserIdStr<'static>,
) -> Result<InProgressExecution> {
- let actions = self.repo.get_actions(macro_user_id.clone()).await?;
- let Some(action) = actions
- .into_iter()
- .find(|a| a.id.as_ref() == Some(id) && is_owned_by(a, &macro_user_id))
- else {
- bail!("scheduled action {id} not found for user");
- };
-
+ let action = self.owned_action(id, &macro_user_id).await?;
+ action.owner_user()?;
+ // Manual execution deliberately has no event reference or event-run ID.
self.executor.execute_action(action).await
}
@@ -113,14 +229,7 @@ where
id: &Uuid,
macro_user_id: MacroUserIdStr<'static>,
) -> Result<Vec<ActionExecutionRecord>> {
- let actions = self.repo.get_actions(macro_user_id.clone()).await?;
- if !actions
- .iter()
- .any(|a| a.id.as_ref() == Some(id) && is_owned_by(a, &macro_user_id))
- {
- bail!("scheduled action {id} not found for user");
- }
-
+ self.owned_action(id, &macro_user_id).await?;
self.repo.get_execution_records(id).await
}
}
diff --git a/services/scheduled_action/src/domain/service/test.rs b/services/scheduled_action/src/domain/service/test.rs
new file mode 100644
index 0000000000..160f07b4e3
--- /dev/null
+++ b/services/scheduled_action/src/domain/service/test.rs
@@ -0,0 +1,462 @@
+use super::*;
+use crate::domain::event_runs::ClaimToken;
+use crate::domain::models::ActionKind;
+use macro_uuid::generate_uuid_v7;
+use serde_json::json;
+use std::sync::Mutex;
+
+pub(crate) const USER: &str = "macro|[email protected]";
+const FOREIGN_USER: &str = "macro|[email protected]";
+
+#[derive(Default)]
+pub(crate) struct FakeRepo {
+ actions: Mutex<Vec<ScheduledAction>>,
+}
+
+impl ScheduledActionRepo for FakeRepo {
+ async fn create_action(&self, mut action: ScheduledAction) -> Result<ScheduledAction> {
+ action.id = Some(generate_uuid_v7());
+ self.actions.lock().unwrap().push(action.clone());
+ Ok(action)
+ }
+
+ async fn get_actions(&self, _user: MacroUserIdStr<'static>) -> Result<Vec<ScheduledAction>> {
+ // Deliberately return all owners to exercise the service's owner check.
+ Ok(self.actions.lock().unwrap().clone())
+ }
+
+ async fn get_action(
+ &self,
+ id: &Uuid,
+ _user: MacroUserIdStr<'static>,
+ ) -> Result<Option<ScheduledAction>> {
+ Ok(self
+ .actions
+ .lock()
+ .unwrap()
+ .iter()
+ .find(|a| a.id == Some(*id))
+ .cloned())
+ }
+
+ async fn update_action(&self, action: ScheduledAction) -> Result<ScheduledAction> {
+ let mut actions = self.actions.lock().unwrap();
+ let stored = actions.iter_mut().find(|a| a.id == action.id).unwrap();
+ if stored.configuration_revision.next()? != action.configuration_revision {
+ return Err(ActionPolicyError::UpdateConflict.into());
+ }
+ *stored = action.clone();
+ Ok(action)
+ }
+
+ async fn delete_action(&self, id: &Uuid, _user: MacroUserIdStr<'static>) -> Result<()> {
+ self.actions.lock().unwrap().retain(|a| a.id != Some(*id));
+ Ok(())
+ }
+
+ async fn get_execution_records(&self, _id: &Uuid) -> Result<Vec<ActionExecutionRecord>> {
+ Ok(vec![])
+ }
+
+ async fn get_next_unclaimed_actions(&self, _limit: i64) -> Result<Vec<ScheduledAction>> {
+ unimplemented!()
+ }
+ async fn claim_action(&self, _id: &Uuid) -> Result<ClaimToken> {
+ unimplemented!()
+ }
+ async fn release_action(&self, _id: &Uuid, _token: ClaimToken) -> Result<()> {
+ unimplemented!()
+ }
+ async fn create_execution_record(&self, _record: ActionExecutionRecord) -> Result<()> {
+ unimplemented!()
+ }
+ async fn update_next_run_at(&self, _id: &Uuid) -> Result<()> {
+ unimplemented!()
+ }
+ async fn update_last_executed(&self, _id: &Uuid, _at: DateTime<Utc>) -> Result<()> {
+ unimplemented!()
+ }
+}
+
+#[derive(Default)]
+pub(crate) struct FakeExecutor {
+ calls: Mutex<Vec<ScheduledAction>>,
+}
+
+impl ScheduledActionExecutor for FakeExecutor {
+ async fn execute_action(&self, action: ScheduledAction) -> Result<InProgressExecution> {
+ let action_id = action.id.unwrap();
+ self.calls.lock().unwrap().push(action);
+ Ok(InProgressExecution {
+ action_id,
+ chat_id: Some("manual-chat".into()),
+ })
+ }
+}
+
+pub(crate) type TestService = ScheduledActionServiceImpl<FakeRepo, FakeExecutor>;
+
+pub(crate) fn service(enabled: bool) -> Arc<TestService> {
+ let (tx, mut rx) = tokio::sync::mpsc::channel(32);
+ tokio::spawn(async move { while rx.recv().await.is_some() {} });
+ Arc::new(
+ TestService::new(
+ Arc::new(FakeRepo::default()),
+ Arc::new(FakeExecutor::default()),
+ tx,
+ )
+ .with_event_management_enabled(enabled),
+ )
+}
+
+pub(crate) fn user() -> MacroUserIdStr<'static> {
+ MacroUserIdStr::try_from(USER.to_owned()).unwrap()
+}
+
+pub(crate) fn configuration(events: bool) -> ActionConfiguration {
+ let trigger = if events {
+ json!({"type": "events", "filters": [{"events": ["document.created"]}]})
+ } else {
+ json!({"type": "cron", "schedule": "0 0 9 * * *", "timezone": "UTC"})
+ };
+ ActionConfiguration {
+ name: "routine".into(),
+ trigger: serde_json::from_value(trigger).unwrap(),
+ kind: ActionKind::Agent,
+ task: json!({"prompt": "summarize"}),
+ enabled: true,
+ }
+}
+
+fn assert_policy<T: std::fmt::Debug>(result: Result<T>, expected: ActionPolicyError) {
+ assert_eq!(
+ result
+ .unwrap_err()
+ .downcast_ref::<ActionPolicyError>()
+ .unwrap()
+ .to_string(),
+ expected.to_string()
+ );
+}
+
+#[tokio::test]
+async fn creates_both_triggers_with_server_owned_state_and_legacy_input() {
+ let svc = service(true);
+ for events in [false, true] {
+ let before = Utc::now();
+ let created = svc
+ .create_action(
+ CreateScheduledAction::Canonical(configuration(events)),
+ user(),
+ )
+ .await
+ .unwrap();
+ assert!(created.owner.is_user(&user()));
+ assert!(created.claimed.is_none());
+ assert_eq!(
+ created.configuration_revision,
+ ConfigurationRevision::INITIAL
+ );
+ assert_eq!(created.next_run_at.is_none(), events);
+ assert_eq!(created.event_activated_at.is_some(), events);
+ assert!(created.created_at >= before);
+ }
+ let legacy = serde_json::from_value(json!({
+ "name": "legacy", "schedule": "0 0 9 * * *", "timezone": "UTC",
+ "kind": "Agent", "task": {}, "enabled": true
+ }))
+ .unwrap();
+ assert!(
+ svc.create_action(legacy, user())
+ .await
+ .unwrap()
+ .next_run_at
+ .is_some()
+ );
+}
+
+#[tokio::test]
+async fn default_gate_rejects_event_creation_and_transition_but_allows_cron() {
+ let (tx, _rx) = tokio::sync::mpsc::channel(8);
+ let svc = TestService::new(
+ Arc::new(FakeRepo::default()),
+ Arc::new(FakeExecutor::default()),
+ tx,
+ );
+ assert_policy(
+ svc.create_action(
+ CreateScheduledAction::Canonical(configuration(true)),
+ user(),
+ )
+ .await,
+ ActionPolicyError::EventManagementDisabled,
+ );
+ let cron = svc
+ .create_action(
+ CreateScheduledAction::Canonical(configuration(false)),
+ user(),
+ )
+ .await
+ .unwrap();
+ assert_policy(
+ svc.update_action(
+ &cron.id.unwrap(),
+ UpdateScheduledAction::Canonical(configuration(true)),
+ user(),
+ )
+ .await,
+ ActionPolicyError::EventManagementDisabled,
+ );
+}
+
+#[tokio::test]
+async fn gate_off_rejects_enabling_an_existing_event_action() {
+ let enabled = service(true);
+ let mut config = configuration(true);
+ config.enabled = false;
+ let action = enabled
+ .create_action(CreateScheduledAction::Canonical(config.clone()), user())
+ .await
+ .unwrap();
+ let disabled = TestService::new(
+ enabled.repo.clone(),
+ enabled.executor.clone(),
+ enabled.dispatcher_tx.clone(),
+ )
+ .with_event_management_enabled(false);
+ config.enabled = true;
+ assert_policy(
+ disabled
+ .update_action(
+ &action.id.unwrap(),
+ UpdateScheduledAction::Canonical(config),
+ user(),
+ )
+ .await,
+ ActionPolicyError::EventManagementDisabled,
+ );
+ assert!(!enabled.repo.actions.lock().unwrap()[0].enabled);
+}
+
+#[tokio::test]
+async fn cron_without_future_firings_is_bad_input() {
+ let svc = service(true);
+ let mut input = configuration(false);
+ input.trigger = serde_json::from_value(
+ json!({"type":"cron", "schedule":"0 0 0 1 1 * 2000", "timezone":"UTC"}),
+ )
+ .unwrap();
+ assert_policy(
+ svc.create_action(CreateScheduledAction::Canonical(input), user())
+ .await,
+ ActionPolicyError::NoFutureFirings,
+ );
+}
+
+#[tokio::test]
+async fn list_is_cron_only_by_default_but_id_operations_reach_events() {
+ let svc = service(true);
+ let cron = svc
+ .create_action(
+ CreateScheduledAction::Canonical(configuration(false)),
+ user(),
+ )
+ .await
+ .unwrap();
+ let event = svc
+ .create_action(
+ CreateScheduledAction::Canonical(configuration(true)),
+ user(),
+ )
+ .await
+ .unwrap();
+ assert_eq!(svc.get_actions(user(), false).await.unwrap()[0].id, cron.id);
+ assert_eq!(svc.get_actions(user(), false).await.unwrap().len(), 1);
+ assert_eq!(svc.get_actions(user(), true).await.unwrap().len(), 2);
+ svc.get_execution_records(&event.id.unwrap(), user())
+ .await
+ .unwrap();
+ svc.delete_action(&event.id.unwrap(), user()).await.unwrap();
+ assert_eq!(svc.get_actions(user(), true).await.unwrap().len(), 1);
+}
+
+#[tokio::test]
+async fn foreign_and_non_user_owners_cannot_be_managed_or_executed() {
+ let svc = service(true);
+ for owner in [
+ Owner::from_principal_str(FOREIGN_USER).unwrap(),
+ Owner::from_principal_str(&format!("bot|{}", generate_uuid_v7())).unwrap(),
+ Owner::from_principal_str(&generate_uuid_v7().to_string()).unwrap(),
+ ] {
+ let created = svc
+ .create_action(
+ CreateScheduledAction::Canonical(configuration(true)),
+ user(),
+ )
+ .await
+ .unwrap();
+ let id = created.id.unwrap();
+ svc.repo.actions.lock().unwrap().last_mut().unwrap().owner = owner;
+ assert_policy(
+ svc.update_action(
+ &id,
+ UpdateScheduledAction::Canonical(configuration(true)),
+ user(),
+ )
+ .await,
+ ActionPolicyError::NotFound,
+ );
+ assert_policy(
+ svc.delete_action(&id, user()).await,
+ ActionPolicyError::NotFound,
+ );
+ assert_policy(
+ svc.get_execution_records(&id, user()).await,
+ ActionPolicyError::NotFound,
+ );
+ assert_policy(
+ svc.execute_action_now(&id, user()).await,
+ ActionPolicyError::NotFound,
+ );
+ }
+ assert!(svc.get_actions(user(), true).await.unwrap().is_empty());
+ assert!(svc.executor.calls.lock().unwrap().is_empty());
+ assert_policy(
+ svc.execute_action_now(&generate_uuid_v7(), user()).await,
+ ActionPolicyError::NotFound,
+ );
+}
+
+#[tokio::test]
+async fn disabled_manual_runs_work_for_both_variants_even_with_gate_off() {
+ let enabled = service(true);
+ let svc = TestService::new(
+ enabled.repo.clone(),
+ enabled.executor.clone(),
+ enabled.dispatcher_tx.clone(),
+ );
+ for events in [false, true] {
+ let mut config = configuration(events);
+ config.enabled = false;
+ let action = enabled
+ .create_action(CreateScheduledAction::Canonical(config), user())
+ .await
+ .unwrap();
+ let id = action.id.unwrap();
+ svc.execute_action_now(&id, user()).await.unwrap();
+ let after = svc.owned_action(&id, &user()).await.unwrap();
+ assert_eq!(after.configuration_revision, action.configuration_revision);
+ assert_eq!(after.event_activated_at, action.event_activated_at);
+ assert!(!after.enabled);
+ }
+ assert_eq!(svc.executor.calls.lock().unwrap().len(), 2);
+}
+
+#[tokio::test]
+async fn updates_advance_revision_and_only_trigger_changes_or_enabling_reset_activation() {
+ let svc = service(true);
+ let mut config = configuration(true);
+ let action = svc
+ .create_action(CreateScheduledAction::Canonical(config.clone()), user())
+ .await
+ .unwrap();
+ let id = action.id.unwrap();
+ let past = Utc::now() - chrono::Duration::days(1);
+ svc.repo.actions.lock().unwrap()[0].event_activated_at = Some(past);
+ config.task = json!({"prompt": "changed"});
+ let updated = svc
+ .update_action(
+ &id,
+ UpdateScheduledAction::Canonical(config.clone()),
+ user(),
+ )
+ .await
+ .unwrap();
+ assert_eq!(updated.configuration_revision.get(), 2);
+ assert_eq!(updated.created_at, action.created_at);
+ assert_eq!(updated.event_activated_at, Some(past));
+ config.enabled = false;
+ let disabled = svc
+ .update_action(
+ &id,
+ UpdateScheduledAction::Canonical(config.clone()),
+ user(),
+ )
+ .await
+ .unwrap();
+ assert_eq!(disabled.configuration_revision.get(), 3);
+ assert_eq!(disabled.event_activated_at, Some(past));
+ config.enabled = true;
+ let enabled = svc
+ .update_action(
+ &id,
+ UpdateScheduledAction::Canonical(config.clone()),
+ user(),
+ )
+ .await
+ .unwrap();
+ assert_eq!(enabled.configuration_revision.get(), 4);
+ assert!(enabled.event_activated_at.unwrap() > past);
+ svc.repo.actions.lock().unwrap()[0].event_activated_at = Some(past);
+ config.trigger = serde_json::from_value(
+ json!({"type":"events", "filters":[{"events":["document.updated"]}]}),
+ )
+ .unwrap();
+ let changed = svc
+ .update_action(&id, UpdateScheduledAction::Canonical(config), user())
+ .await
+ .unwrap();
+ assert!(changed.event_activated_at.unwrap() > past);
+ let cron = svc
+ .update_action(
+ &id,
+ UpdateScheduledAction::Canonical(configuration(false)),
+ user(),
+ )
+ .await
+ .unwrap();
+ assert!(cron.event_activated_at.is_none());
+ assert!(cron.next_run_at.is_some());
+}
+
+#[tokio::test]
+async fn active_execution_blocks_replacement_but_allows_disabling_even_with_gate_off() {
+ let enabled = service(true);
+ let mut config = configuration(true);
+ let action = enabled
+ .create_action(CreateScheduledAction::Canonical(config.clone()), user())
+ .await
+ .unwrap();
+ let id = action.id.unwrap();
+ enabled.repo.actions.lock().unwrap()[0].claimed = Some(Utc::now());
+ let mut replacement = config.clone();
+ replacement.name = "new name".into();
+ assert_policy(
+ enabled
+ .update_action(&id, UpdateScheduledAction::Canonical(replacement), user())
+ .await,
+ ActionPolicyError::UpdateConflict,
+ );
+ let svc = TestService::new(
+ enabled.repo.clone(),
+ enabled.executor.clone(),
+ enabled.dispatcher_tx.clone(),
+ );
+ assert_policy(
+ svc.update_action(
+ &id,
+ UpdateScheduledAction::Canonical(config.clone()),
+ user(),
+ )
+ .await,
+ ActionPolicyError::EventManagementDisabled,
+ );
+ config.enabled = false;
+ let disabled = svc
+ .update_action(&id, UpdateScheduledAction::Canonical(config), user())
+ .await
+ .unwrap();
+ assert!(!disabled.enabled);
+ assert!(disabled.claimed.is_some());
+ assert_eq!(disabled.event_activated_at, action.event_activated_at);
+}
diff --git a/services/scheduled_action/src/inbound.rs b/services/scheduled_action/src/inbound.rs
index 2021071c60..548b1282da 100644
--- a/services/scheduled_action/src/inbound.rs
+++ b/services/scheduled_action/src/inbound.rs
@@ -1 +1,3 @@
pub mod axum_router;
+pub mod event_run_worker;
+pub mod kafka_consumer;
diff --git a/services/scheduled_action/src/inbound/axum_router.rs b/services/scheduled_action/src/inbound/axum_router.rs
index cd16b0cca7..9df44766ca 100644
--- a/services/scheduled_action/src/inbound/axum_router.rs
+++ b/services/scheduled_action/src/inbound/axum_router.rs
@@ -1,23 +1,67 @@
use std::sync::Arc;
-use axum::extract::{FromRef, Path, State};
+use crate::domain::event_trigger::ActionTrigger;
+use crate::domain::models::{
+ ActionExecutionRecord, ActionPolicyError, AlreadyRunningError, CreateScheduledAction,
+ InProgressExecution, OwnerNotUserError, Schedule, ScheduledAction, UpdateScheduledAction,
+};
+use crate::domain::ports::ScheduledActionService;
+use axum::extract::{FromRef, Path, Query, State, rejection::JsonRejection};
use axum::http::StatusCode;
use axum::response::IntoResponse;
use axum::routing::{get, post, put};
use axum::{Json, Router};
-use chrono::Utc;
+use chrono_tz::Tz;
use macro_authorization::{
MacroAuthorizationExtractor, MacroAuthorizationService, MacroAuthorizationState, UserOrInternal,
};
use macro_uuid::Uuid;
use model::response::EmptyResponse;
-use model_owner::Owner;
+use serde::{Deserialize, Serialize};
+use utoipa::{IntoParams, ToSchema};
-use crate::domain::models::{
- ActionExecutionRecord, AlreadyRunningError, CreateScheduledAction, InProgressExecution,
- OwnerNotUserError, ScheduledAction, UpdateScheduledAction,
-};
-use crate::domain::ports::ScheduledActionService;
+#[cfg(test)]
+mod test;
+
+/// Canonical trigger plus deprecated cron fields for existing clients. Event
+/// responses omit legacy fields rather than inventing a schedule or timezone.
+#[derive(Debug, Serialize, ToSchema)]
+pub struct ScheduledActionResponse {
+ #[serde(flatten)]
+ pub action: ScheduledAction,
+ /// Deprecated: use `trigger.schedule`. Present only for cron actions.
+ #[deprecated(note = "use trigger.schedule")]
+ #[serde(skip_serializing_if = "Option::is_none")]
+ #[schema(value_type = Option<String>)]
+ pub schedule: Option<Schedule>,
+ /// Deprecated: use `trigger.timezone`. Present only for cron actions.
+ #[deprecated(note = "use trigger.timezone")]
+ #[serde(skip_serializing_if = "Option::is_none")]
+ #[schema(value_type = Option<String>)]
+ pub timezone: Option<Tz>,
+}
+
+impl From<ScheduledAction> for ScheduledActionResponse {
+ #[expect(deprecated, reason = "compatibility response populates legacy fields")]
+ fn from(action: ScheduledAction) -> Self {
+ let (schedule, timezone) = match &action.trigger {
+ ActionTrigger::Cron { schedule, timezone } => (Some(schedule.clone()), Some(*timezone)),
+ ActionTrigger::Events { .. } => (None, None),
+ };
+ Self {
+ action,
+ schedule,
+ timezone,
+ }
+ }
+}
+
+#[derive(Debug, Default, Deserialize, IntoParams)]
+pub struct ListActionsQuery {
+ /// Backend clients must opt in to event actions; defaults to false (cron-only).
+ #[param(default = false)]
+ pub include_events: Option<bool>,
+}
pub struct ScheduledActionRouterState<S, Auth> {
pub service: Arc<S>,
@@ -87,7 +131,8 @@ pub async fn health() -> impl IntoResponse {
operation_id = "create_scheduled_action",
request_body = CreateScheduledAction,
responses(
- (status = 201, body = ScheduledAction),
+ (status = 201, body = ScheduledActionResponse),
+ (status = 400, body = String),
(status = 401, body = String),
(status = 500, body = String),
)
@@ -98,29 +143,17 @@ pub async fn create_action<
>(
State(state): State<ScheduledActionRouterState<S, Auth>>,
user: MacroAuthorizationExtractor<Auth, UserOrInternal>,
- Json(req): Json<CreateScheduledAction>,
+ body: Result<Json<CreateScheduledAction>, JsonRejection>,
) -> Result<impl IntoResponse, ScheduledActionApiError> {
- let now = Utc::now();
- let next_run_at = req
- .schedule
- .next_run_after_now(req.timezone)
- .ok_or_else(|| anyhow::anyhow!("schedule has no future firings"))?;
- let action = ScheduledAction {
- id: None,
- owner: Owner::User(user.authorization.user.macro_user_id.clone()),
- name: req.name,
- schedule: req.schedule,
- kind: req.kind,
- created_at: now,
- updated_at: now,
- timezone: req.timezone,
- task: req.task,
- claimed: None,
- next_run_at,
- enabled: req.enabled,
- };
- let created = state.service.create_action(action).await?;
- Ok((StatusCode::CREATED, Json(created)))
+ let Json(req) = body.map_err(ScheduledActionApiError::InvalidRequest)?;
+ let created = state
+ .service
+ .create_action(req, user.authorization.user.macro_user_id.clone())
+ .await?;
+ Ok((
+ StatusCode::CREATED,
+ Json(ScheduledActionResponse::from(created)),
+ ))
}
#[utoipa::path(
@@ -128,8 +161,10 @@ pub async fn create_action<
path = "/scheduled-actions",
tag = "scheduled actions",
operation_id = "list_scheduled_actions",
+ params(ListActionsQuery),
responses(
- (status = 200, body = Vec<ScheduledAction>),
+ (status = 200, body = Vec<ScheduledActionResponse>),
+ (status = 400, body = String),
(status = 401, body = String),
(status = 500, body = String),
)
@@ -140,12 +175,21 @@ pub async fn list_actions<
>(
State(state): State<ScheduledActionRouterState<S, Auth>>,
user: MacroAuthorizationExtractor<Auth, UserOrInternal>,
+ Query(query): Query<ListActionsQuery>,
) -> Result<impl IntoResponse, ScheduledActionApiError> {
let actions = state
.service
- .get_actions(user.authorization.user.macro_user_id.clone())
+ .get_actions(
+ user.authorization.user.macro_user_id.clone(),
+ query.include_events.unwrap_or(false),
+ )
.await?;
- Ok(Json(actions))
+ Ok(Json(
+ actions
+ .into_iter()
+ .map(ScheduledActionResponse::from)
+ .collect::<Vec<_>>(),
+ ))
}
#[utoipa::path(
@@ -156,7 +200,9 @@ pub async fn list_actions<
params(("id" = String, Path, description = "ID of the scheduled action")),
request_body = UpdateScheduledAction,
responses(
- (status = 200, body = ScheduledAction),
+ (status = 200, body = ScheduledActionResponse),
+ (status = 400, body = String),
+ (status = 409, body = String, description = "Configuration changed or execution is active"),
(status = 401, body = String),
(status = 404, body = String),
(status = 500, body = String),
@@ -169,32 +215,14 @@ pub async fn update_action<
State(state): State<ScheduledActionRouterState<S, Auth>>,
user: MacroAuthorizationExtractor<Auth, UserOrInternal>,
Path(id): Path<Uuid>,
- Json(req): Json<UpdateScheduledAction>,
+ body: Result<Json<UpdateScheduledAction>, JsonRejection>,
) -> Result<impl IntoResponse, ScheduledActionApiError> {
- let now = Utc::now();
- let next_run_at = req
- .schedule
- .next_run_after_now(req.timezone)
- .ok_or_else(|| anyhow::anyhow!("schedule has no future firings"))?;
- let action = ScheduledAction {
- id: Some(id),
- owner: Owner::User(user.authorization.user.macro_user_id.clone()),
- name: req.name,
- schedule: req.schedule,
- kind: req.kind,
- created_at: now,
- updated_at: now,
- timezone: req.timezone,
- task: req.task,
- claimed: None,
- next_run_at,
- enabled: req.enabled,
- };
+ let Json(req) = body.map_err(ScheduledActionApiError::InvalidRequest)?;
let updated = state
.service
- .update_action(action, user.authorization.user.macro_user_id.clone())
+ .update_action(&id, req, user.authorization.user.macro_user_id.clone())
.await?;
- Ok(Json(updated))
+ Ok(Json(ScheduledActionResponse::from(updated)))
}
#[utoipa::path(
@@ -233,6 +261,7 @@ pub async fn delete_action<
params(("id" = String, Path, description = "ID of the scheduled action")),
responses(
(status = 200, body = InProgressExecution),
+ (status = 400, body = String),
(status = 401, body = String),
(status = 404, body = String),
(status = 409, body = String, description = "Action is already running"),
@@ -282,25 +311,49 @@ pub async fn list_history<
Ok(Json(records))
}
-pub struct ScheduledActionApiError(anyhow::Error);
+pub enum ScheduledActionApiError {
+ InvalidRequest(JsonRejection),
+ Service(anyhow::Error),
+}
impl From<anyhow::Error> for ScheduledActionApiError {
fn from(err: anyhow::Error) -> Self {
- Self(err)
+ Self::Service(err)
}
}
impl IntoResponse for ScheduledActionApiError {
fn into_response(self) -> axum::response::Response {
- if let Some(already_running) = self.0.downcast_ref::<AlreadyRunningError>() {
+ let error = match self {
+ Self::InvalidRequest(rejection) => {
+ let status = if rejection.status() == StatusCode::PAYLOAD_TOO_LARGE {
+ StatusCode::PAYLOAD_TOO_LARGE
+ } else {
+ StatusCode::BAD_REQUEST
+ };
+ return (status, "invalid scheduled action request").into_response();
+ }
+ Self::Service(error) => error,
+ };
+ if let Some(policy) = error.downcast_ref::<ActionPolicyError>() {
+ let status = match policy {
+ ActionPolicyError::NotFound => StatusCode::NOT_FOUND,
+ ActionPolicyError::UpdateConflict => StatusCode::CONFLICT,
+ ActionPolicyError::NoFutureFirings | ActionPolicyError::EventManagementDisabled => {
+ StatusCode::BAD_REQUEST
+ }
+ };
+ return (status, policy.to_string()).into_response();
+ }
+ if let Some(already_running) = error.downcast_ref::<AlreadyRunningError>() {
tracing::info!(error=%already_running, "scheduled action already running");
return (StatusCode::CONFLICT, already_running.to_string()).into_response();
}
- if let Some(owner_not_user) = self.0.downcast_ref::<OwnerNotUserError>() {
+ if let Some(owner_not_user) = error.downcast_ref::<OwnerNotUserError>() {
tracing::warn!(error=%owner_not_user, "scheduled action owner is not a user");
return (StatusCode::BAD_REQUEST, owner_not_user.to_string()).into_response();
}
- tracing::error!(error=?self.0, "scheduled action api error");
- (StatusCode::INTERNAL_SERVER_ERROR, self.0.to_string()).into_response()
+ tracing::error!(error=?error, "scheduled action api error");
+ (StatusCode::INTERNAL_SERVER_ERROR, "internal server error").into_response()
}
}
diff --git a/services/scheduled_action/src/inbound/axum_router/test.rs b/services/scheduled_action/src/inbound/axum_router/test.rs
new file mode 100644
index 0000000000..f00487051e
--- /dev/null
+++ b/services/scheduled_action/src/inbound/axum_router/test.rs
@@ -0,0 +1,347 @@
+use super::*;
+use crate::domain::service::test::{USER, configuration, service};
+use axum::{
+ body::{Body, to_bytes},
+ http::{Request, header},
+};
+use macro_authorization::{InternalIdentityClaims, MacroAuthorizationError};
+use model::user::UserContext;
+use rootcause::Report;
+use serde_json::{Value, json};
+use tower::ServiceExt;
+use utoipa::OpenApi;
+
+#[derive(Clone)]
+struct FakeAuth;
+
+impl MacroAuthorizationService for FakeAuth {
+ async fn authorize(&self, jwt: &str) -> Result<UserContext, Report<MacroAuthorizationError>> {
+ match jwt {
+ "owner" => Ok(UserContext {
+ user_id: USER.into(),
+ ..Default::default()
+ }),
+ "other" => Ok(UserContext {
+ user_id: "macro|[email protected]".into(),
+ ..Default::default()
+ }),
+ _ => Err(Report::new(MacroAuthorizationError::InvalidCredentials)),
+ }
+ }
+
+ async fn authorize_internal(
+ &self,
+ key: &str,
+ claims: InternalIdentityClaims,
+ ) -> Result<Option<UserContext>, Report<MacroAuthorizationError>> {
+ if key != "test-internal-key" {
+ return Err(Report::new(MacroAuthorizationError::InvalidCredentials));
+ }
+ Ok(claims.user_id.map(|user_id| UserContext {
+ user_id,
+ ..Default::default()
+ }))
+ }
+}
+
+fn router(events: bool) -> Router {
+ scheduled_action_router(ScheduledActionRouterState {
+ service: service(events),
+ authorization_state: MacroAuthorizationState::new(Arc::new(FakeAuth)),
+ })
+}
+
+async fn request(
+ router: &Router,
+ method: &str,
+ uri: &str,
+ token: &str,
+ body: Value,
+) -> (StatusCode, Value) {
+ let response = router
+ .clone()
+ .oneshot(
+ Request::builder()
+ .method(method)
+ .uri(uri)
+ .header(header::AUTHORIZATION, format!("Bearer {token}"))
+ .header(header::CONTENT_TYPE, "application/json")
+ .body(Body::from(body.to_string()))
+ .unwrap(),
+ )
+ .await
+ .unwrap();
+ let status = response.status();
+ let bytes = to_bytes(response.into_body(), usize::MAX).await.unwrap();
+ let value =
+ serde_json::from_slice(&bytes).unwrap_or_else(|_| json!(String::from_utf8_lossy(&bytes)));
+ (status, value)
+}
+
+fn legacy() -> Value {
+ json!({"name":"legacy", "kind":"Agent", "schedule":"0 0 9 * * *", "timezone":"UTC", "task":{}, "enabled":true})
+}
+
+#[tokio::test]
+async fn legacy_and_canonical_cron_create_update_return_compatibility_fields() {
+ let app = router(false);
+ for input in [
+ legacy(),
+ serde_json::to_value(configuration(false)).unwrap(),
+ ] {
+ let (status, created) =
+ request(&app, "POST", "/scheduled-actions", "owner", input.clone()).await;
+ assert_eq!(status, StatusCode::CREATED);
+ assert_eq!(created["owner"], USER);
+ assert_eq!(created["trigger"]["type"], "cron");
+ assert_eq!(created["schedule"], created["trigger"]["schedule"]);
+ assert_eq!(created["timezone"], created["trigger"]["timezone"]);
+ let url = format!("/scheduled-actions/{}", created["id"].as_str().unwrap());
+ let (status, updated) = request(&app, "PUT", &url, "owner", input).await;
+ assert_eq!(status, StatusCode::OK);
+ assert_eq!(updated["configuration_revision"], 2);
+ assert_eq!(updated["created_at"], created["created_at"]);
+ }
+}
+
+#[tokio::test]
+async fn event_responses_and_opt_in_lists_do_not_invent_cron_fields() {
+ let app = router(true);
+ let mut input = serde_json::to_value(configuration(true)).unwrap();
+ input["enabled"] = json!(false);
+ let (status, event) = request(&app, "POST", "/scheduled-actions", "owner", input).await;
+ assert_eq!(status, StatusCode::CREATED);
+ assert!(event.get("schedule").is_none());
+ assert!(event.get("timezone").is_none());
+ assert!(event["next_run_at"].is_null());
+ let (_, list) = request(&app, "GET", "/scheduled-actions", "owner", Value::Null).await;
+ assert_eq!(list, json!([]));
+ let (_, list) = request(
+ &app,
+ "GET",
+ "/scheduled-actions?include_events=true",
+ "owner",
+ Value::Null,
+ )
+ .await;
+ assert_eq!(list.as_array().unwrap().len(), 1);
+ let url = format!(
+ "/scheduled-actions/{}/execute",
+ event["id"].as_str().unwrap()
+ );
+ assert_eq!(
+ request(&app, "POST", &url, "owner", Value::Null).await.0,
+ StatusCode::OK
+ );
+}
+
+#[tokio::test]
+async fn mixed_unknown_and_server_owned_input_is_bad_request_for_create_and_update() {
+ let app = router(true);
+ let (_, created) = request(&app, "POST", "/scheduled-actions", "owner", legacy()).await;
+ let url = format!("/scheduled-actions/{}", created["id"].as_str().unwrap());
+ let mut invalid = vec![];
+ for field in [
+ "owner",
+ "id",
+ "claimed",
+ "configuration_revision",
+ "event_activated_at",
+ "next_run_at",
+ "created_at",
+ "updated_at",
+ ] {
+ let mut input = legacy();
+ input[field] = Value::Null;
+ invalid.push(input);
+ }
+ for extra in [
+ Value::Null,
+ json!({"type":"cron", "schedule":"0 0 9 * * *", "timezone":"UTC"}),
+ json!({"type":"events", "filters":[{"events":["document.created"]}]}),
+ ] {
+ let mut input = legacy();
+ input["trigger"] = extra;
+ invalid.push(input);
+ }
+ let mut canonical = serde_json::to_value(configuration(true)).unwrap();
+ canonical["schedule"] = Value::Null;
+ invalid.push(canonical);
+ let mut bad_filter = serde_json::to_value(configuration(true)).unwrap();
+ bad_filter["trigger"]["filters"] = json!([]);
+ invalid.push(bad_filter);
+ for input in invalid {
+ assert_eq!(
+ request(&app, "POST", "/scheduled-actions", "owner", input.clone())
+ .await
+ .0,
+ StatusCode::BAD_REQUEST
+ );
+ assert_eq!(
+ request(&app, "PUT", &url, "owner", input).await.0,
+ StatusCode::BAD_REQUEST
+ );
+ }
+}
+
+#[tokio::test]
+async fn foreign_owner_operations_return_not_found_and_list_is_empty() {
+ let app = router(true);
+ let (_, action) = request(
+ &app,
+ "POST",
+ "/scheduled-actions",
+ "owner",
+ serde_json::to_value(configuration(true)).unwrap(),
+ )
+ .await;
+ let url = format!("/scheduled-actions/{}", action["id"].as_str().unwrap());
+ for (method, path, body) in [
+ ("PUT", url.clone(), legacy()),
+ ("DELETE", url.clone(), Value::Null),
+ ("POST", format!("{url}/execute"), Value::Null),
+ ("GET", format!("{url}/history"), Value::Null),
+ ] {
+ assert_eq!(
+ request(&app, method, &path, "other", body).await.0,
+ StatusCode::NOT_FOUND
+ );
+ }
+ assert_eq!(
+ request(
+ &app,
+ "GET",
+ "/scheduled-actions?include_events=true",
+ "other",
+ Value::Null
+ )
+ .await
+ .1,
+ json!([])
+ );
+}
+
+#[tokio::test]
+async fn event_management_off_and_expired_cron_are_bad_requests() {
+ let app = router(false);
+ assert_eq!(
+ request(
+ &app,
+ "POST",
+ "/scheduled-actions",
+ "owner",
+ serde_json::to_value(configuration(true)).unwrap()
+ )
+ .await
+ .0,
+ StatusCode::BAD_REQUEST
+ );
+ let mut input = legacy();
+ input["schedule"] = json!("0 0 0 1 1 * 2000");
+ assert_eq!(
+ request(&app, "POST", "/scheduled-actions", "owner", input)
+ .await
+ .0,
+ StatusCode::BAD_REQUEST
+ );
+}
+
+#[tokio::test]
+async fn authenticates_user_and_internal_requests_and_rejects_missing_credentials() {
+ let app = router(false);
+ let no_auth = Request::builder()
+ .uri("/scheduled-actions")
+ .body(Body::empty())
+ .unwrap();
+ assert_eq!(
+ app.clone().oneshot(no_auth).await.unwrap().status(),
+ StatusCode::UNAUTHORIZED
+ );
+ let internal = Request::builder()
+ .uri("/scheduled-actions")
+ .header(
+ macro_authorization::INTERNAL_API_KEY_HEADER,
+ "test-internal-key",
+ )
+ .header(macro_authorization::INTERNAL_MACRO_USER_ID_HEADER, USER)
+ .body(Body::empty())
+ .unwrap();
+ assert_eq!(
+ app.oneshot(internal).await.unwrap().status(),
+ StatusCode::OK
+ );
+}
+
+#[tokio::test]
+async fn maps_typed_errors_and_sanitizes_internal_failures() {
+ for (error, expected) in [
+ (
+ anyhow::Error::from(ActionPolicyError::NotFound),
+ StatusCode::NOT_FOUND,
+ ),
+ (
+ anyhow::Error::from(ActionPolicyError::UpdateConflict),
+ StatusCode::CONFLICT,
+ ),
+ (
+ anyhow::Error::from(AlreadyRunningError {
+ action_id: macro_uuid::generate_uuid_v7(),
+ }),
+ StatusCode::CONFLICT,
+ ),
+ (
+ anyhow::Error::from(OwnerNotUserError {
+ owner_type: model_owner::OwnerType::Bot,
+ }),
+ StatusCode::BAD_REQUEST,
+ ),
+ (
+ anyhow::anyhow!("secret database details"),
+ StatusCode::INTERNAL_SERVER_ERROR,
+ ),
+ ] {
+ let response = ScheduledActionApiError::from(error).into_response();
+ assert_eq!(response.status(), expected);
+ let bytes = to_bytes(response.into_body(), usize::MAX).await.unwrap();
+ assert!(!String::from_utf8_lossy(&bytes).contains("secret"));
+ }
+}
+
+#[test]
+fn openapi_documents_canonical_legacy_and_event_opt_in_contracts() {
+ let spec = serde_json::to_value(crate::swagger::ApiDoc::openapi()).unwrap();
+ let schemas = &spec["components"]["schemas"];
+ for name in [
+ "ActionConfiguration",
+ "LegacyActionConfiguration",
+ "ActionTrigger",
+ "EventFilters",
+ "EventName",
+ "ScheduledActionResponse",
+ "CreateScheduledAction",
+ "UpdateScheduledAction",
+ ] {
+ assert!(!schemas[name].is_null(), "missing {name}");
+ }
+ let legacy_properties = &schemas["ScheduledActionResponse"]["allOf"][1]["properties"];
+ assert_eq!(legacy_properties["schedule"]["deprecated"], true);
+ assert_eq!(legacy_properties["timezone"]["deprecated"], true);
+ assert_eq!(schemas["EventFilters"]["type"], "array");
+ assert_eq!(
+ schemas["ActionConfiguration"]["additionalProperties"],
+ false
+ );
+ assert_eq!(
+ schemas["LegacyActionConfiguration"]["additionalProperties"],
+ false
+ );
+ let params = spec["paths"]["/scheduled-actions"]["get"]["parameters"]
+ .as_array()
+ .unwrap();
+ assert!(
+ params
+ .iter()
+ .any(|p| p["name"] == "include_events" && p["required"] == false)
+ );
+ assert!(!spec["paths"]["/scheduled-actions/{id}"]["put"]["responses"]["409"].is_null());
+}
diff --git a/services/scheduled_action/src/inbound/event_run_worker.rs b/services/scheduled_action/src/inbound/event_run_worker.rs
new file mode 100644
index 0000000000..89d1306475
--- /dev/null
+++ b/services/scheduled_action/src/inbound/event_run_worker.rs
@@ -0,0 +1,84 @@
+//! Bounded continuous event dispatch. Shutdown signals active executors; the
+//! lifecycle drains the worker, then tracked executions and terminal bookkeeping.
+
+use std::{sync::Arc, time::Duration};
+
+use tokio::sync::Semaphore;
+use tokio_util::{sync::CancellationToken, task::TaskTracker};
+
+use crate::domain::event_runs::{PageSize, dispatch::EventRunDispatch};
+
+#[cfg(test)]
+mod test;
+
+const POLL_INTERVAL: Duration = Duration::from_secs(1);
+
+/// Track dispatch futures separately from the worker without detaching them.
+/// The composition root budgets capacity alongside HTTP and cron work.
+pub async fn run_event_worker(
+ service: impl EventRunDispatch,
+ shutdown: CancellationToken,
+ executions: TaskTracker,
+ concurrency: PageSize,
+) {
+ run(service, shutdown, executions, concurrency, POLL_INTERVAL).await;
+}
+
+async fn run(
+ service: impl EventRunDispatch,
+ shutdown: CancellationToken,
+ executions: TaskTracker,
+ limit: PageSize,
+ interval: Duration,
+) {
+ let service = Arc::new(service);
+ let permits = Arc::new(Semaphore::new(usize::from(limit.get())));
+ loop {
+ if shutdown.is_cancelled() {
+ return;
+ }
+ if service.reconcile(limit).await.is_err() {
+ tracing::warn!(
+ outcome = "reconciliation_unavailable",
+ "event maintenance deferred"
+ );
+ }
+ let free = u16::try_from(permits.available_permits()).expect("capacity fits PageSize");
+ if let Ok(page) = PageSize::try_from(free) {
+ match service.pending(page).await {
+ Ok(pending) => {
+ for pending in pending {
+ let Ok(permit) = permits.clone().try_acquire_owned() else {
+ break;
+ };
+ let service = Arc::clone(&service);
+ let shutdown = shutdown.clone();
+ executions.spawn(async move {
+ let _permit = permit;
+ if shutdown.is_cancelled() {
+ return;
+ }
+ // Do not select/drop dispatch on shutdown: a claim may
+ // already have committed. The executor owns cancellation.
+ if service
+ .dispatch(pending, shutdown.cancelled())
+ .await
+ .is_err()
+ {
+ tracing::warn!(
+ outcome = "dispatch_unavailable",
+ "event dispatch or bookkeeping deferred"
+ );
+ }
+ });
+ }
+ }
+ Err(_) => tracing::warn!(outcome = "queue_unavailable", "event polling deferred"),
+ }
+ }
+ tokio::select! {
+ _ = shutdown.cancelled() => return,
+ _ = tokio::time::sleep(interval) => {},
+ }
+ }
+}
diff --git a/services/scheduled_action/src/inbound/event_run_worker/test.rs b/services/scheduled_action/src/inbound/event_run_worker/test.rs
new file mode 100644
index 0000000000..1947cce137
--- /dev/null
+++ b/services/scheduled_action/src/inbound/event_run_worker/test.rs
@@ -0,0 +1,218 @@
+use super::*;
+use crate::domain::event_runs::{ConfigurationRevision, PendingEventRun, dispatch::DispatchResult};
+use chrono::Utc;
+use macro_uuid::generate_uuid_v7;
+use rootcause::Report;
+use serde_json::json;
+use std::sync::{
+ Arc, Mutex,
+ atomic::{AtomicUsize, Ordering},
+};
+
+const CONCURRENT_RUNS: u16 = 2;
+
+#[derive(Clone)]
+struct Service {
+ state: Arc<State>,
+ shutdown: CancellationToken,
+ wait_for_shutdown: bool,
+ slow_first: bool,
+}
+#[derive(Default)]
+struct State {
+ pending: Mutex<Vec<PendingEventRun>>,
+ active: AtomicUsize,
+ maximum: AtomicUsize,
+ started: AtomicUsize,
+ completed: AtomicUsize,
+ maintenance: AtomicUsize,
+ finishing: AtomicUsize,
+ finish: tokio::sync::Notify,
+}
+impl EventRunDispatch for Service {
+ async fn pending(&self, limit: PageSize) -> Result<Vec<PendingEventRun>, Report> {
+ let mut pending = self.state.pending.lock().unwrap();
+ let count = pending.len().min(limit.get().into());
+ Ok(pending.drain(..count).collect())
+ }
+ async fn reconcile(&self, limit: PageSize) -> Result<u16, Report> {
+ assert_eq!(limit.get(), CONCURRENT_RUNS);
+ self.state.maintenance.fetch_add(1, Ordering::SeqCst);
+ Ok(0)
+ }
+ async fn dispatch(
+ &self,
+ _: PendingEventRun,
+ cancellation: impl Future<Output = ()> + Send,
+ ) -> Result<DispatchResult, Report> {
+ let call = self.state.started.fetch_add(1, Ordering::SeqCst);
+ let active = self.state.active.fetch_add(1, Ordering::SeqCst) + 1;
+ self.state.maximum.fetch_max(active, Ordering::SeqCst);
+ if self.wait_for_shutdown || (self.slow_first && call == 0) {
+ cancellation.await;
+ let finish = self.state.finish.notified();
+ tokio::pin!(finish);
+ finish.as_mut().enable();
+ self.state.finishing.fetch_add(1, Ordering::SeqCst);
+ finish.await;
+ } else {
+ tokio::task::yield_now().await;
+ }
+ self.state.active.fetch_sub(1, Ordering::SeqCst);
+ let completed = self.state.completed.fetch_add(1, Ordering::SeqCst) + 1;
+ if completed == 25 {
+ self.shutdown.cancel();
+ }
+ if call == 0 {
+ return Err(rootcause::report!("first dispatch unavailable"));
+ }
+ Ok(DispatchResult::NotStarted)
+ }
+}
+fn service(count: usize, wait_for_shutdown: bool) -> Service {
+ let state = Arc::new(State::default());
+ *state.pending.lock().unwrap() = (0..count).map(|_| PendingEventRun {
+ action_id: generate_uuid_v7(), revision: ConfigurationRevision::INITIAL,
+ event: serde_json::from_value(json!({"event_id":generate_uuid_v7(), "event_name":"document.updated", "entity_id":generate_uuid_v7(), "message_id":null})).unwrap(),
+ admitted_at: Utc::now(),
+ }).collect();
+ Service {
+ state,
+ shutdown: CancellationToken::new(),
+ wait_for_shutdown,
+ slow_first: false,
+ }
+}
+
+#[tokio::test]
+async fn bounded_dispatch_continues_after_failed_run() {
+ let service = service(25, false);
+ tokio::time::timeout(
+ Duration::from_secs(2),
+ run(
+ service.clone(),
+ service.shutdown.clone(),
+ TaskTracker::new(),
+ PageSize::try_from(CONCURRENT_RUNS).unwrap(),
+ Duration::ZERO,
+ ),
+ )
+ .await
+ .unwrap();
+ assert_eq!(service.state.completed.load(Ordering::SeqCst), 25);
+ assert_eq!(
+ service.state.maximum.load(Ordering::SeqCst),
+ usize::from(CONCURRENT_RUNS)
+ );
+ assert!(service.state.maintenance.load(Ordering::SeqCst) >= 13);
+ assert_eq!(service.state.active.load(Ordering::SeqCst), 0);
+}
+
+async fn wait_until(condition: impl Fn() -> bool) {
+ tokio::time::timeout(Duration::from_secs(2), async {
+ while !condition() {
+ tokio::task::yield_now().await;
+ }
+ })
+ .await
+ .unwrap();
+}
+
+#[tokio::test]
+async fn slow_run_does_not_block_third_run_or_reconciliation() {
+ let mut service = service(3, false);
+ service.slow_first = true;
+ let executions = TaskTracker::new();
+ let worker = tokio::spawn(run(
+ service.clone(),
+ service.shutdown.clone(),
+ executions.clone(),
+ page_size(),
+ Duration::from_millis(1),
+ ));
+ wait_until(|| service.state.completed.load(Ordering::SeqCst) == 2).await;
+ assert_eq!(service.state.started.load(Ordering::SeqCst), 3);
+ assert_eq!(service.state.active.load(Ordering::SeqCst), 1);
+ assert_eq!(service.state.maximum.load(Ordering::SeqCst), 2);
+ let maintenance = service.state.maintenance.load(Ordering::SeqCst);
+ wait_until(|| service.state.maintenance.load(Ordering::SeqCst) > maintenance).await;
+ service.shutdown.cancel();
+ worker.await.unwrap();
+ wait_until(|| service.state.finishing.load(Ordering::SeqCst) == 1).await;
+ service.state.finish.notify_waiters();
+ executions.close();
+ tokio::time::timeout(Duration::from_secs(2), executions.wait())
+ .await
+ .unwrap();
+ assert_eq!(service.state.completed.load(Ordering::SeqCst), 3);
+}
+
+fn page_size() -> PageSize {
+ CONCURRENT_RUNS.try_into().unwrap()
+}
+
+#[tokio::test]
+async fn cancelled_worker_does_not_poll_or_dispatch() {
+ let service = service(3, false);
+ service.shutdown.cancel();
+ let executions = TaskTracker::new();
+ run(
+ service.clone(),
+ service.shutdown.clone(),
+ executions.clone(),
+ page_size(),
+ Duration::ZERO,
+ )
+ .await;
+ assert_eq!(service.state.maintenance.load(Ordering::SeqCst), 0);
+ assert_eq!(service.state.pending.lock().unwrap().len(), 3);
+ assert!(executions.is_empty());
+}
+
+#[tokio::test]
+async fn shutdown_signals_active_runs_and_awaits_completion_without_claiming_more() {
+ let service = service(25, true);
+ let executions = TaskTracker::new();
+ let worker = tokio::spawn(run(
+ service.clone(),
+ service.shutdown.clone(),
+ executions.clone(),
+ PageSize::try_from(CONCURRENT_RUNS).unwrap(),
+ Duration::ZERO,
+ ));
+ tokio::time::timeout(Duration::from_secs(2), async {
+ while service.state.started.load(Ordering::SeqCst) < usize::from(CONCURRENT_RUNS) {
+ tokio::task::yield_now().await;
+ }
+ })
+ .await
+ .unwrap();
+ assert_eq!(executions.len(), usize::from(CONCURRENT_RUNS));
+ let maintenance = service.state.maintenance.load(Ordering::SeqCst);
+ wait_until(|| service.state.maintenance.load(Ordering::SeqCst) > maintenance).await;
+ assert_eq!(service.state.pending.lock().unwrap().len(), 23);
+ executions.close();
+ service.shutdown.cancel();
+ tokio::time::timeout(Duration::from_secs(2), worker)
+ .await
+ .unwrap()
+ .unwrap();
+ wait_until(|| service.state.finishing.load(Ordering::SeqCst) == 2).await;
+ assert_eq!(executions.len(), 2);
+ assert!(
+ tokio::time::timeout(Duration::from_millis(10), executions.wait())
+ .await
+ .is_err()
+ );
+ service.state.finish.notify_waiters();
+ tokio::time::timeout(Duration::from_secs(2), executions.wait())
+ .await
+ .unwrap();
+ assert_eq!(
+ service.state.completed.load(Ordering::SeqCst),
+ usize::from(CONCURRENT_RUNS)
+ );
+ assert_eq!(service.state.active.load(Ordering::SeqCst), 0);
+ assert_eq!(service.state.pending.lock().unwrap().len(), 23);
+ assert!(executions.is_empty());
+}
diff --git a/services/scheduled_action/src/inbound/kafka_consumer.rs b/services/scheduled_action/src/inbound/kafka_consumer.rs
new file mode 100644
index 0000000000..28332682bd
--- /dev/null
+++ b/services/scheduled_action/src/inbound/kafka_consumer.rs
@@ -0,0 +1,216 @@
+//! Durable event intake, independent of routine execution. Records are processed
+//! serially: no later offset can be committed past an unfinished admission.
+
+#[cfg(test)]
+mod test;
+
+use std::future::Future;
+use std::time::Duration;
+
+use channels::domain::broker_events::ChannelMacroEvent;
+use documents::domain::events::DocumentMacroEvent;
+use kafka_util::{GroupName, KafkaEventConsumer};
+use macro_event_broker::{
+ EventBrokerError, EventConsumer, KafkaConsumerAdapter, MacroEvent as _,
+ MacroEventCollection as _, MacroEventConsumerService, MessageWrapper,
+};
+use rdkafka::consumer::CommitMode;
+use rdkafka::message::{BorrowedMessage, Message};
+use rootcause::Report;
+use tracing::Instrument as _;
+
+use crate::domain::event_runs::{EventIngestion, EventIngestionResult};
+use crate::domain::event_trigger::{EventPayload, IncomingEvent};
+
+macro_event_broker::declare_topics!(DeclaredMacroEvent: DocumentMacroEvent, ChannelMacroEvent);
+
+struct ScheduledActionEventIngestionGroup;
+
+impl GroupName for ScheduledActionEventIngestionGroup {
+ const GROUP_NAME: &'static str = "scheduled-action-event-ingestion";
+}
+
+type ScheduledActionKafkaAdapter =
+ KafkaConsumerAdapter<ScheduledActionEventIngestionGroup, DeclaredMacroEvent>;
+
+const MAX_INGEST_ATTEMPTS: u8 = 5;
+// Four delays (1+2+4+8 seconds) before the fifth and final attempt.
+const INGEST_RETRY_BASE_DELAY: Duration = Duration::from_secs(1);
+
+// Transport-only seam: tests exercise the same receive/admit/commit loop without
+// a broker. The domain sees only EventIngestion, never Kafka delivery controls.
+trait CommittingConsumer: EventConsumer<DeclaredMacroEvent> {
+ fn commit(&self, message: &Self::MessageType<'_>) -> Result<(), Report>;
+}
+
+impl CommittingConsumer for ScheduledActionKafkaAdapter {
+ fn commit(&self, message: &BorrowedMessage<'_>) -> Result<(), Report> {
+ // Async commits only report enqueueing, not broker acknowledgment. Stop
+ // on a synchronous commit failure and let a fresh consumer redeliver.
+ self.commit_message(message, CommitMode::Sync)
+ }
+}
+
+fn incoming_event(event: DeclaredMacroEvent) -> IncomingEvent {
+ match event {
+ DeclaredMacroEvent::DocumentMacroEvent(event) => {
+ let envelope = event.event();
+ IncomingEvent {
+ event_id: envelope.event_id,
+ schema_version: envelope.schema_version,
+ payload: EventPayload::Document(envelope.event.clone()),
+ }
+ }
+ DeclaredMacroEvent::ChannelMacroEvent(event) => {
+ let envelope = event.event();
+ IncomingEvent {
+ event_id: envelope.event_id,
+ schema_version: envelope.schema_version,
+ payload: EventPayload::Channel(envelope.event.clone()),
+ }
+ }
+ }
+}
+
+fn decode_rejection(error: &EventBrokerError) -> &'static str {
+ match error {
+ EventBrokerError::UnsupportedSchemaVersion { .. } => "unsupported_schema",
+ EventBrokerError::MissingMessageKey => "missing_key",
+ EventBrokerError::MissingMessagePayload => "missing_payload",
+ EventBrokerError::UnknownTopic(_) => "unsupported_topic",
+ _ => "malformed_or_unsupported_payload",
+ }
+}
+
+async fn ingest_with_retry(
+ service: &impl EventIngestion,
+ event: &IncomingEvent,
+ base_delay: Duration,
+) -> Result<(), Report> {
+ for attempt in 1..=MAX_INGEST_ATTEMPTS {
+ if attempt > 1 {
+ tokio::time::sleep(base_delay * (1 << (attempt - 2))).await;
+ }
+ match service.ingest(event).await {
+ Ok(EventIngestionResult::Admitted { inserted }) => {
+ tracing::info!(attempt, inserted, outcome = "admitted", "event admitted");
+ return Ok(());
+ }
+ Ok(EventIngestionResult::Rejected(reason)) => {
+ tracing::info!(
+ attempt,
+ reason = %reason,
+ outcome = "permanent_reject",
+ "event rejected by ingestion"
+ );
+ return Ok(());
+ }
+ Err(_) => {
+ // Reports can contain payloads or database values. Only emit
+ // the classification, never the underlying admission error.
+ tracing::warn!(
+ attempt,
+ outcome = "transient_failure",
+ "event admission failed"
+ );
+ }
+ }
+ }
+ Err(rootcause::report!("event admission retries exhausted"))
+}
+
+async fn process_message<C: CommittingConsumer>(
+ consumer: &C,
+ service: &impl EventIngestion,
+ message: &MessageWrapper<C::MessageType<'_>, DeclaredMacroEvent>,
+ base_delay: Duration,
+) -> Result<(), Report> {
+ match message.decode_payload() {
+ Ok(event) => {
+ let event = incoming_event(event);
+ tracing::Span::current()
+ .record("macro.event.id", tracing::field::display(event.event_id));
+ ingest_with_retry(service, &event, base_delay).await?;
+ }
+ Err(error) => {
+ // Serde errors can include attacker-controlled strings. Do not log
+ // the error itself, message key, or raw payload.
+ tracing::warn!(
+ reason = decode_rejection(&error),
+ outcome = "permanent_reject",
+ "broker record rejected"
+ );
+ }
+ }
+ consumer
+ .commit(message.inner())
+ .map_err(|_| rootcause::report!("event offset commit failed"))?;
+ tracing::info!(outcome = "committed", records = 1, "event offset committed");
+ Ok(())
+}
+
+async fn consume<C>(
+ consumer: C,
+ service: impl EventIngestion,
+ shutdown: impl Future<Output = ()> + Send,
+ base_delay: Duration,
+) -> Result<(), Report>
+where
+ C: CommittingConsumer,
+ for<'a> C::MessageType<'a>: Message,
+{
+ let consumer = MacroEventConsumerService::new(consumer);
+ let mut shutdown = std::pin::pin!(shutdown);
+ loop {
+ let message = tokio::select! {
+ biased;
+ _ = &mut shutdown => return Ok(()),
+ result = consumer.recv() => {
+ result.map_err(|_| rootcause::report!("event consumer receive failed"))?
+ }
+ };
+ let span = kafka_util::consumer_span(
+ message.inner(),
+ ScheduledActionEventIngestionGroup::GROUP_NAME,
+ );
+ let result = tokio::select! {
+ biased;
+ _ = &mut shutdown => {
+ tracing::info!(parent: &span, outcome = "cancelled_uncommitted", "event intake stopped");
+ // Dropping admission can leave partial durable fan-out. Exit,
+ // never continue this consumer; redelivery deduplicates it.
+ return Ok(());
+ }
+ result = process_message(consumer.inner(), &service, &message, base_delay)
+ .instrument(span.clone()) => result,
+ };
+ result.inspect_err(|_| {
+ kafka_util::record_span_error(&span, "event intake stopped without advancing");
+ tracing::error!(parent: &span, outcome = "fatal", "event intake stopped without advancing");
+ })?;
+ }
+}
+
+/// Consume exactly document and channel topics under a dedicated durable group.
+///
+/// Only durable admission or a permanent rejection permits committing. Admission
+/// errors get five attempts with bounded exponential backoff. Exhausted retries,
+/// receive errors, and commit failures are fatal: restart with a fresh consumer,
+/// not the old in-memory position. Shutdown also drops the consumer without
+/// committing unfinished admission. Partial fan-out is safe to redeliver through
+/// the ingestion port's durable deduplication contract.
+pub async fn run_scheduled_action_event_consumer(
+ brokers: &str,
+ service: impl EventIngestion,
+ shutdown: impl Future<Output = ()> + Send,
+) -> Result<(), Report> {
+ let consumer = KafkaEventConsumer::<ScheduledActionEventIngestionGroup>::from_env(brokers)?;
+ let consumer = KafkaConsumerAdapter::<ScheduledActionEventIngestionGroup, ()>::new(consumer)
+ .subscribe::<DeclaredMacroEvent>()?;
+ tracing::info!(
+ topics = ?DeclaredMacroEvent::topics(),
+ group = ScheduledActionEventIngestionGroup::GROUP_NAME,
+ "scheduled action event consumer listening"
+ );
+ consume(consumer, service, shutdown, INGEST_RETRY_BASE_DELAY).await
+}
diff --git a/services/scheduled_action/src/inbound/kafka_consumer/test.rs b/services/scheduled_action/src/inbound/kafka_consumer/test.rs
new file mode 100644
index 0000000000..737a0d47fa
--- /dev/null
+++ b/services/scheduled_action/src/inbound/kafka_consumer/test.rs
@@ -0,0 +1,386 @@
+use super::*;
+use crate::domain::event_trigger::EventRejection;
+use macro_event_broker::Event;
+use macro_uuid::Uuid;
+use rdkafka::message::{OwnedMessage, Timestamp};
+use serde_json::{Value, json};
+use std::collections::{HashSet, VecDeque};
+use std::sync::{Arc, Mutex};
+use tokio_util::sync::CancellationToken;
+
+const EVENT_ID: &str = "01900000-0000-7000-8000-000000000003";
+const ENTITY_ID: &str = "01900000-0000-7000-8000-000000000001";
+
+#[derive(Default)]
+struct State {
+ messages: VecDeque<OwnedMessage>,
+ received: Vec<i64>,
+ operations: Vec<&'static str>,
+ committed: Vec<i64>,
+ commit_attempts: Vec<i64>,
+ fail_commit: bool,
+ calls: Vec<Uuid>,
+ admitted: HashSet<(u8, Uuid)>,
+ inserted: Vec<u64>,
+ steps: VecDeque<Step>,
+}
+
+enum Step {
+ Admit,
+ PartialFailure,
+ Reject(EventRejection),
+ ShutdownDuringAdmission,
+ ShutdownBeforeBackoff,
+}
+
+#[derive(Clone, Default)]
+struct FakeConsumer {
+ state: Arc<Mutex<State>>,
+ shutdown: CancellationToken,
+}
+
+impl EventConsumer<DeclaredMacroEvent> for FakeConsumer {
+ type MessageType<'a> = OwnedMessage;
+
+ async fn recv<'a>(
+ &'a self,
+ ) -> Result<MessageWrapper<Self::MessageType<'a>, DeclaredMacroEvent>, Report> {
+ let mut state = self.state.lock().unwrap();
+ let message = state.messages.pop_front().expect("unexpected receive");
+ state.received.push(message.offset());
+ Ok(MessageWrapper::new(message))
+ }
+}
+
+impl CommittingConsumer for FakeConsumer {
+ fn commit(&self, message: &OwnedMessage) -> Result<(), Report> {
+ let mut state = self.state.lock().unwrap();
+ state.operations.push("commit");
+ state.commit_attempts.push(message.offset());
+ if state.fail_commit {
+ return Err(rootcause::report!("fake commit failure"));
+ }
+ state.committed.push(message.offset());
+ if state.messages.is_empty() {
+ self.shutdown.cancel();
+ }
+ Ok(())
+ }
+}
+
+// This fake models durable admission's idempotency contract, not matching or
+// execution. No repositories, agent runners, or model clients are constructed.
+#[derive(Clone)]
+struct FakeIngestion(FakeConsumer);
+
+impl EventIngestion for FakeIngestion {
+ async fn ingest(&self, event: &IncomingEvent) -> Result<EventIngestionResult, Report> {
+ let step = {
+ let mut state = self.0.state.lock().unwrap();
+ state.operations.push("ingest");
+ state.calls.push(event.event_id);
+ let step = state.steps.pop_front().unwrap_or(Step::Admit);
+ match step {
+ Step::Admit => {
+ let mut inserted = 0;
+ for action_id in [1, 2] {
+ inserted += u64::from(state.admitted.insert((action_id, event.event_id)));
+ }
+ state.inserted.push(inserted);
+ return Ok(EventIngestionResult::Admitted { inserted });
+ }
+ Step::Reject(reason) => return Ok(EventIngestionResult::Rejected(reason)),
+ _ => {
+ state.admitted.insert((1, event.event_id));
+ step
+ }
+ }
+ };
+ match step {
+ Step::ShutdownDuringAdmission => {
+ self.0.shutdown.cancel();
+ std::future::pending().await
+ }
+ Step::ShutdownBeforeBackoff => {
+ self.0.shutdown.cancel();
+ Err(rootcause::report!("fake transient failure"))
+ }
+ _ => Err(rootcause::report!("fake failure after partial fan-out")),
+ }
+ }
+}
+
+fn envelope(name: &str) -> Value {
+ json!({
+ "event_id": EVENT_ID,
+ "schema_version": 1,
+ "event_type": name,
+ "metadata": {
+ "document_id": ENTITY_ID, "owner": "macro|[email protected]",
+ "actor": "macro|[email protected]", "document_name": "Private title",
+ "file_type": "md", "channel_id": ENTITY_ID, "channel_type": "public",
+ "participant_user_ids": ["macro|[email protected]"]
+ }
+ })
+}
+
+fn message(
+ topic: &str,
+ payload: Option<Vec<u8>>,
+ key: Option<Vec<u8>>,
+ offset: i64,
+) -> OwnedMessage {
+ OwnedMessage::new(
+ payload,
+ key,
+ topic.to_owned(),
+ Timestamp::NotAvailable,
+ 0,
+ offset,
+ None,
+ )
+}
+
+fn document(offset: i64) -> OwnedMessage {
+ message(
+ "macro.documents",
+ Some(serde_json::to_vec(&envelope("document.created")).unwrap()),
+ Some(ENTITY_ID.as_bytes().to_vec()),
+ offset,
+ )
+}
+
+fn fixture(messages: Vec<OwnedMessage>, steps: Vec<Step>) -> FakeConsumer {
+ FakeConsumer {
+ state: Arc::new(Mutex::new(State {
+ messages: messages.into(),
+ steps: steps.into(),
+ ..State::default()
+ })),
+ shutdown: CancellationToken::new(),
+ }
+}
+
+async fn run(consumer: &FakeConsumer) -> Result<(), Report> {
+ tokio::time::timeout(
+ Duration::from_secs(1),
+ consume(
+ consumer.clone(),
+ FakeIngestion(consumer.clone()),
+ consumer.shutdown.cancelled(),
+ Duration::ZERO,
+ ),
+ )
+ .await
+ .expect("consumer did not terminate")
+}
+
+#[test]
+fn subscriptions_are_exact_and_group_is_dedicated() {
+ assert_eq!(
+ DeclaredMacroEvent::topics(),
+ &["macro.documents", "macro.channels"]
+ );
+ assert_eq!(
+ ScheduledActionEventIngestionGroup::GROUP_NAME,
+ "scheduled-action-event-ingestion"
+ );
+}
+
+#[tokio::test]
+async fn admits_both_topics_before_committing() {
+ let channel = message(
+ "macro.channels",
+ Some(serde_json::to_vec(&envelope("channel.created")).unwrap()),
+ Some(ENTITY_ID.as_bytes().to_vec()),
+ 11,
+ );
+ // Confirm transport conversion preserves identity, schema and typed payload.
+ let decoded = DeclaredMacroEvent::decode(&channel).unwrap();
+ let incoming = incoming_event(decoded);
+ assert_eq!(incoming.event_id.to_string(), EVENT_ID);
+ assert_eq!(incoming.schema_version, 1);
+ assert!(matches!(incoming.payload, EventPayload::Channel(_)));
+ let consumer = fixture(vec![document(10), channel], vec![]);
+ run(&consumer).await.unwrap();
+ let state = consumer.state.lock().unwrap();
+ assert_eq!(state.calls.len(), 2);
+ assert_eq!(state.committed, [10, 11]);
+}
+
+#[tokio::test]
+async fn partial_fan_out_is_retried_and_redelivery_is_deduplicated() {
+ let consumer = fixture(vec![document(10), document(11)], vec![Step::PartialFailure]);
+ run(&consumer).await.unwrap();
+ let state = consumer.state.lock().unwrap();
+ assert_eq!(state.calls, vec![Uuid::parse_str(EVENT_ID).unwrap(); 3]);
+ assert_eq!(state.admitted.len(), 2);
+ assert_eq!(state.inserted, [1, 0]);
+ assert_eq!(state.committed, [10, 11]);
+ assert_eq!(
+ state.operations,
+ ["ingest", "ingest", "commit", "ingest", "commit"]
+ );
+}
+
+#[tokio::test]
+async fn permanent_domain_rejections_commit_without_retry() {
+ for reason in [
+ EventRejection::UnsupportedEvent,
+ EventRejection::UnsafeAttribution,
+ ] {
+ let consumer = fixture(vec![document(10)], vec![Step::Reject(reason)]);
+ run(&consumer).await.unwrap();
+ let state = consumer.state.lock().unwrap();
+ assert_eq!(state.calls.len(), 1);
+ assert!(state.admitted.is_empty());
+ assert_eq!(state.committed, [10]);
+ }
+}
+
+#[tokio::test]
+async fn poison_missing_and_future_payloads_are_rejected_without_ingestion() {
+ let mut unknown = envelope("document.future_event");
+ unknown["metadata"]["content"] = json!("secret content");
+ let payloads = [
+ None,
+ Some(vec![0xff, 0xfe]),
+ Some(b"{invalid private payload".to_vec()),
+ Some(serde_json::to_vec(&unknown).unwrap()),
+ Some(b"{}".to_vec()),
+ ];
+ for payload in payloads {
+ let consumer = fixture(
+ vec![message("macro.documents", payload, Some(vec![]), 10)],
+ vec![],
+ );
+ run(&consumer).await.unwrap();
+ let state = consumer.state.lock().unwrap();
+ assert!(state.calls.is_empty());
+ assert_eq!(state.committed, [10]);
+ }
+ let consumer = fixture(
+ vec![message(
+ "macro.documents",
+ document(10).payload().map(<[u8]>::to_vec),
+ None,
+ 10,
+ )],
+ vec![],
+ );
+ run(&consumer).await.unwrap();
+ assert!(consumer.state.lock().unwrap().calls.is_empty());
+}
+
+#[tokio::test]
+async fn schema_mismatch_is_an_observable_permanent_reject() {
+ let mut payload = envelope("document.created");
+ payload["schema_version"] = json!(2);
+ let record = message(
+ "macro.documents",
+ Some(serde_json::to_vec(&payload).unwrap()),
+ Some(vec![]),
+ 10,
+ );
+ let error = DeclaredMacroEvent::decode(&record).err().unwrap();
+ assert_eq!(decode_rejection(&error), "unsupported_schema");
+ let consumer = fixture(vec![record, document(11)], vec![]);
+ run(&consumer).await.unwrap();
+ let state = consumer.state.lock().unwrap();
+ assert_eq!(state.calls.len(), 1);
+ assert_eq!(state.committed, [10, 11]);
+}
+
+#[tokio::test]
+async fn exhausted_transient_errors_never_advance_to_the_next_record() {
+ let consumer = fixture(
+ vec![document(10), document(11)],
+ (0..MAX_INGEST_ATTEMPTS)
+ .map(|_| Step::PartialFailure)
+ .collect(),
+ );
+ assert!(run(&consumer).await.is_err());
+ {
+ let state = consumer.state.lock().unwrap();
+ assert_eq!(state.calls.len(), usize::from(MAX_INGEST_ATTEMPTS));
+ assert_eq!(state.received, [10]);
+ assert!(state.commit_attempts.is_empty());
+ assert_eq!(state.admitted.len(), 1);
+ }
+ // A fresh consumer restarts from the uncommitted offset with the same
+ // durable rows. The already-admitted first action must not run twice.
+ consumer
+ .state
+ .lock()
+ .unwrap()
+ .messages
+ .push_front(document(10));
+ run(&consumer).await.unwrap();
+ let state = consumer.state.lock().unwrap();
+ assert_eq!(state.admitted.len(), 2);
+ assert_eq!(state.inserted, [1, 0]);
+ assert_eq!(state.committed, [10, 11]);
+}
+
+#[tokio::test]
+async fn commit_failure_is_fatal_even_for_a_poison_record() {
+ for record in [
+ document(10),
+ message("macro.documents", None, Some(vec![]), 10),
+ ] {
+ let consumer = fixture(vec![record, document(11)], vec![]);
+ consumer.state.lock().unwrap().fail_commit = true;
+ assert!(run(&consumer).await.is_err());
+ let state = consumer.state.lock().unwrap();
+ assert_eq!(state.received, [10]);
+ assert_eq!(state.commit_attempts, [10]);
+ assert!(state.committed.is_empty());
+ }
+}
+
+#[tokio::test]
+async fn shutdown_during_admission_or_backoff_never_commits_or_receives_later_work() {
+ for step in [Step::ShutdownDuringAdmission, Step::ShutdownBeforeBackoff] {
+ let consumer = fixture(vec![document(10), document(11)], vec![step]);
+ // Use real backoff here: cancellation must interrupt the delay rather
+ // than merely finish after the zero-delay retries used by other tests.
+ tokio::time::timeout(
+ Duration::from_secs(1),
+ consume(
+ consumer.clone(),
+ FakeIngestion(consumer.clone()),
+ consumer.shutdown.cancelled(),
+ INGEST_RETRY_BASE_DELAY,
+ ),
+ )
+ .await
+ .expect("shutdown did not interrupt admission/backoff")
+ .unwrap();
+ let state = consumer.state.lock().unwrap();
+ assert_eq!(state.calls.len(), 1);
+ assert_eq!(state.admitted.len(), 1);
+ assert_eq!(state.received, [10]);
+ assert!(state.commit_attempts.is_empty());
+ }
+}
+
+#[tokio::test]
+async fn already_cancelled_shutdown_does_not_receive() {
+ let consumer = fixture(vec![document(10)], vec![]);
+ consumer.shutdown.cancel();
+ run(&consumer).await.unwrap();
+ assert!(consumer.state.lock().unwrap().received.is_empty());
+}
+
+#[test]
+fn document_conversion_preserves_the_envelope() {
+ let event = incoming_event(DeclaredMacroEvent::decode(&document(10)).unwrap());
+ let EventPayload::Document(payload) = event.payload else {
+ panic!("expected document payload");
+ };
+ let original: Event<documents::domain::events::DocumentTopicEvent> =
+ serde_json::from_value(envelope("document.created")).unwrap();
+ assert_eq!(event.event_id, original.event_id);
+ assert_eq!(event.schema_version, original.schema_version);
+ assert_eq!(payload, original.event);
+}
diff --git a/services/scheduled_action/src/outbound.rs b/services/scheduled_action/src/outbound.rs
index 8ca29def91..2915cecbe1 100644
--- a/services/scheduled_action/src/outbound.rs
+++ b/services/scheduled_action/src/outbound.rs
@@ -1,5 +1,7 @@
pub mod conn_gateway_live_updates;
+pub mod event_access;
pub mod inprocess_executor;
+pub mod pg_event_run_repo;
pub mod pg_polling_dispatcher;
pub mod pg_scheduled_action_repo;
pub mod tokio_dispatcher;
diff --git a/services/scheduled_action/src/outbound/event_access.rs b/services/scheduled_action/src/outbound/event_access.rs
new file mode 100644
index 0000000000..1909c37544
--- /dev/null
+++ b/services/scheduled_action/src/outbound/event_access.rs
@@ -0,0 +1,68 @@
+//! Translate the owning entity-access service's typed receipts, not permission
+//! tables or event actors, into the routine domain's narrow capability port.
+
+use entity_access::domain::{
+ models::{AccessError, EntityType, ViewAccessLevel, ViewOnly},
+ ports::EntityAccessService,
+};
+use macro_user_id::user_id::MacroUserIdStr;
+use rootcause::Report;
+
+use crate::domain::{
+ event_runs::{CurrentOwnerAccess, EventAccessCapability},
+ event_trigger::{EventEntityType, EventReference},
+};
+
+#[cfg(test)]
+mod test;
+
+pub struct EventAccessAdapter<S> {
+ service: S,
+}
+
+impl<S> EventAccessAdapter<S> {
+ pub fn new(service: S) -> Self {
+ Self { service }
+ }
+}
+
+fn access_result<T>(result: Result<T, AccessError>) -> Result<Option<T>, Report> {
+ match result {
+ Ok(receipt) => Ok(Some(receipt)),
+ Err(
+ AccessError::Unauthorized
+ | AccessError::UnauthorizedWithMessage(_)
+ | AccessError::NotFound(_),
+ ) => Ok(None),
+ // Never turn infrastructure or adapter-contract errors into denial.
+ Err(error) => Err(Report::new(error).into_dynamic()),
+ }
+}
+
+impl<S: EntityAccessService> CurrentOwnerAccess for EventAccessAdapter<S> {
+ async fn authorize(
+ &self,
+ owner: &MacroUserIdStr<'static>,
+ event: &EventReference,
+ ) -> Result<Option<EventAccessCapability>, Report> {
+ let id = event.entity_id().to_string();
+ let result = match event.entity_type() {
+ EventEntityType::Document => self
+ .service
+ .generate_entity_access_receipt::<ViewAccessLevel>(
+ owner,
+ None,
+ &id,
+ EntityType::Document,
+ )
+ .await
+ .map(EventAccessCapability::Document),
+ EventEntityType::Channel => self
+ .service
+ .generate_entity_access_receipt::<ViewOnly>(owner, None, &id, EntityType::Channel)
+ .await
+ .map(EventAccessCapability::Channel),
+ };
+ access_result(result)
+ }
+}
diff --git a/services/scheduled_action/src/outbound/event_access/test.rs b/services/scheduled_action/src/outbound/event_access/test.rs
new file mode 100644
index 0000000000..d7add28658
--- /dev/null
+++ b/services/scheduled_action/src/outbound/event_access/test.rs
@@ -0,0 +1,189 @@
+use super::*;
+use entity_access::domain::models::{
+ AccessLevel, BotAccessScope, BotId, CallChannelInfo, Entity, EntityAccessReceipt,
+ EntityPermission, RequiredPermission, TeamRole, UserTeamInfo,
+};
+use macro_user_id::{lowercased::Lowercase, user_id::MacroUserId};
+use macro_uuid::{Uuid, generate_uuid_v7};
+use serde_json::json;
+use std::sync::{Arc, Mutex};
+
+#[derive(Clone)]
+struct AccessService {
+ permission: EntityPermission,
+ calls: Arc<Mutex<Vec<(String, String, EntityType)>>>,
+}
+impl EntityAccessService for AccessService {
+ async fn generate_entity_access_receipt<T: RequiredPermission>(
+ &self,
+ user: &MacroUserId<Lowercase<'_>>,
+ org: Option<i64>,
+ id: &str,
+ kind: EntityType,
+ ) -> Result<EntityAccessReceipt<T>, AccessError> {
+ assert_eq!(org, None);
+ self.calls
+ .lock()
+ .unwrap()
+ .push((user.as_ref().to_string(), id.into(), kind));
+ EntityAccessReceipt::try_new_authenticated_user(
+ MacroUserIdStr::try_from(user.as_ref().to_string()).unwrap(),
+ Entity {
+ entity_id: id.into(),
+ entity_type: kind,
+ },
+ self.permission,
+ )
+ }
+ async fn generate_bot_entity_access_receipt<T: RequiredPermission>(
+ &self,
+ _: BotId,
+ _: BotAccessScope,
+ _: &str,
+ _: EntityType,
+ ) -> Result<EntityAccessReceipt<T>, AccessError> {
+ unreachable!()
+ }
+ async fn get_access_level(
+ &self,
+ _: Option<&MacroUserId<Lowercase<'_>>>,
+ _: &str,
+ _: EntityType,
+ ) -> Result<Option<AccessLevel>, AccessError> {
+ unreachable!()
+ }
+ async fn check_access(
+ &self,
+ _: Option<&MacroUserId<Lowercase<'_>>>,
+ _: &str,
+ _: EntityType,
+ _: AccessLevel,
+ ) -> Result<AccessLevel, AccessError> {
+ unreachable!()
+ }
+ async fn check_public_access(
+ &self,
+ _: &str,
+ _: EntityType,
+ _: AccessLevel,
+ ) -> Result<AccessLevel, AccessError> {
+ unreachable!()
+ }
+ async fn get_entity_permission(
+ &self,
+ _: Option<&MacroUserId<Lowercase<'_>>>,
+ _: &str,
+ _: EntityType,
+ _: Option<i64>,
+ ) -> Result<EntityPermission, AccessError> {
+ unreachable!()
+ }
+ async fn get_crm_entity_permission_with_team(
+ &self,
+ _: Option<&MacroUserId<Lowercase<'_>>>,
+ _: &str,
+ _: EntityType,
+ ) -> Result<(EntityPermission, Uuid, TeamRole), AccessError> {
+ unreachable!()
+ }
+ async fn get_users_by_entity(
+ &self,
+ _: &str,
+ _: EntityType,
+ ) -> Result<Vec<MacroUserIdStr<'static>>, AccessError> {
+ unreachable!()
+ }
+ async fn get_call_channel(&self, _: &Uuid) -> Result<Option<CallChannelInfo>, AccessError> {
+ unreachable!()
+ }
+ async fn get_call_channel_by_channel_id(
+ &self,
+ _: &Uuid,
+ ) -> Result<Option<CallChannelInfo>, AccessError> {
+ unreachable!()
+ }
+ async fn get_user_team(
+ &self,
+ _: &MacroUserId<Lowercase<'_>>,
+ ) -> Result<Option<UserTeamInfo>, AccessError> {
+ unreachable!()
+ }
+}
+
+#[tokio::test]
+async fn mints_entity_specific_receipts_for_exact_owner_and_entity() {
+ let owner = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();
+ for (name, kind, permission) in [
+ (
+ "document.updated",
+ EntityType::Document,
+ EntityPermission::AccessLevel {
+ access_level: AccessLevel::View,
+ },
+ ),
+ (
+ "channel.created",
+ EntityType::Channel,
+ EntityPermission::ChannelViewOnly,
+ ),
+ (
+ "channel.created",
+ EntityType::Channel,
+ EntityPermission::ChannelRole {
+ role: entity_access::domain::models::ParticipantRole::Member,
+ },
+ ),
+ ] {
+ let service = AccessService {
+ permission,
+ calls: Arc::default(),
+ };
+ let adapter = EventAccessAdapter::new(service.clone());
+ let event: EventReference = serde_json::from_value(json!({"event_id":generate_uuid_v7(), "event_name":name, "entity_id":generate_uuid_v7(), "message_id":null})).unwrap();
+ let receipt = adapter.authorize(&owner, &event).await.unwrap().unwrap();
+ assert!(receipt.authorizes(&owner, &event));
+ assert_eq!(
+ *service.calls.lock().unwrap(),
+ vec![(owner.to_string(), event.entity_id().to_string(), kind)]
+ );
+ }
+}
+
+#[tokio::test]
+async fn wrong_permission_family_is_denied_not_reinterpreted() {
+ let owner = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();
+ for (name, permission) in [
+ ("document.updated", EntityPermission::ChannelViewOnly),
+ (
+ "channel.created",
+ EntityPermission::AccessLevel {
+ access_level: AccessLevel::Owner,
+ },
+ ),
+ ] {
+ let adapter = EventAccessAdapter::new(AccessService {
+ permission,
+ calls: Arc::default(),
+ });
+ let event = serde_json::from_value(json!({"event_id":generate_uuid_v7(), "event_name":name, "entity_id":generate_uuid_v7(), "message_id":null})).unwrap();
+ assert!(adapter.authorize(&owner, &event).await.unwrap().is_none());
+ }
+}
+
+#[test]
+fn denial_and_missing_entities_are_distinct_from_infrastructure_failures() {
+ for error in [
+ AccessError::Unauthorized,
+ AccessError::UnauthorizedWithMessage("denied"),
+ AccessError::NotFound("missing"),
+ ] {
+ assert!(access_result::<()>(Err(error)).unwrap().is_none());
+ }
+ for error in [
+ AccessError::Unavailable(rootcause::report!("offline")),
+ AccessError::internal("broken"),
+ AccessError::BadRequest("invalid"),
+ ] {
+ assert!(access_result::<()>(Err(error)).is_err());
+ }
+}
diff --git a/services/scheduled_action/src/outbound/inprocess_executor.rs b/services/scheduled_action/src/outbound/inprocess_executor.rs
index f4c844611c..8e353a88ec 100644
--- a/services/scheduled_action/src/outbound/inprocess_executor.rs
+++ b/services/scheduled_action/src/outbound/inprocess_executor.rs
@@ -1,175 +1,282 @@
-mod agent_task;
+pub mod agent_task;
mod notify;
-use std::sync::Arc;
+#[cfg(test)]
+mod test;
-use ai_tools::ToolServiceContext;
-use anyhow::Result;
-use chrono::Utc;
-use notification::domain::service::SqsNotificationIngress;
-use notification::outbound::queue::SqsQueue;
+use std::{sync::Arc, time::Duration};
+
+use anyhow::{Context, Result};
+use chrono::{DateTime, Utc};
use serde_json::Value;
-use sqlx::PgPool;
+use tokio_util::{sync::CancellationToken, task::TaskTracker};
+use crate::domain::event_runs::{
+ ClaimedEventRun, EventExecutionResult, EventExecutor, EventRunOutcome,
+};
use crate::domain::models::{
- ActionExecutionRecord, ActionKind, AlreadyRunningError, InProgressExecution, MAX_ACTION_TIME,
- ScheduledAction, ScheduledActionUpdate,
+ ActionExecutionRecord, InProgressExecution, MAX_ACTION_TIME, ScheduledAction,
+ ScheduledActionUpdate,
};
use crate::domain::ports::{
- ScheduledActionExecutor, ScheduledActionLiveUpdate, ScheduledActionRepo,
+ ScheduledActionExecutor, ScheduledActionLiveUpdate, ScheduledActionRepo, ScheduledAgentRunner,
};
-pub struct InProcessExecutor<Rpo: ScheduledActionRepo, Live: ScheduledActionLiveUpdate> {
+/// Shared execution path. Cron/manual calls return after tracked chat creation;
+/// event workers await it and own atomic event/history finalization and release.
+pub struct InProcessExecutor<Rpo, Live, Runner> {
repo: Arc<Rpo>,
- db: PgPool,
- tool_context: ToolServiceContext,
- notification_ingress: Arc<SqsNotificationIngress<SqsQueue>>,
live_updates: Arc<Live>,
+ runner: Arc<Runner>,
+ tracker: TaskTracker,
+ cancellation: CancellationToken,
+}
+
+impl<Rpo, Live, Runner> Clone for InProcessExecutor<Rpo, Live, Runner> {
+ fn clone(&self) -> Self {
+ Self {
+ repo: self.repo.clone(),
+ live_updates: self.live_updates.clone(),
+ runner: self.runner.clone(),
+ tracker: self.tracker.clone(),
+ cancellation: self.cancellation.clone(),
+ }
+ }
}
-impl<Rpo: ScheduledActionRepo, Live: ScheduledActionLiveUpdate> InProcessExecutor<Rpo, Live> {
+impl<Rpo, Live, Runner> InProcessExecutor<Rpo, Live, Runner>
+where
+ Rpo: ScheduledActionRepo,
+ Live: ScheduledActionLiveUpdate,
+ Runner: ScheduledAgentRunner,
+{
pub fn new(
repo: Arc<Rpo>,
- db: PgPool,
- tool_context: ToolServiceContext,
- notification_ingress: Arc<SqsNotificationIngress<SqsQueue>>,
+ runner: Arc<Runner>,
live_updates: Arc<Live>,
+ tracker: TaskTracker,
+ cancellation: CancellationToken,
) -> Self {
Self {
repo,
- db,
- tool_context,
- notification_ingress,
+ runner,
live_updates,
+ tracker,
+ cancellation,
}
}
-}
-fn try_claim(action: &ScheduledAction) -> Result<()> {
- if let Some(claimed_at) = action.claimed {
- let elapsed = Utc::now() - claimed_at;
- if elapsed < MAX_ACTION_TIME {
- return Err(anyhow::Error::new(AlreadyRunningError {
- action_id: *action.id.as_ref().unwrap(),
- }));
+ async fn prepare(
+ &self,
+ action: &ScheduledAction,
+ chat_id: &mut Option<String>,
+ ) -> Result<String> {
+ let owner = action.owner_user()?.clone();
+ let action_id = action.id.context("persisted action required")?;
+ let created = self.runner.create_chat(action).await?;
+ // Retain the chat link even if publishing Started stalls or is cancelled.
+ *chat_id = Some(created.clone());
+ self.live_updates
+ .publish_update(ScheduledActionUpdate::Started {
+ owner,
+ action_id,
+ chat_id: created.clone(),
+ })
+ .await;
+ Ok(created)
+ }
+
+ async fn stopped(&self, action: &ScheduledAction, chat_id: &str, is_success: bool) {
+ if let (Ok(owner), Some(action_id)) = (action.owner_user(), action.id) {
+ self.live_updates
+ .publish_update(ScheduledActionUpdate::Stopped {
+ owner: owner.clone(),
+ action_id,
+ chat_id: chat_id.to_owned(),
+ is_success,
+ })
+ .await;
}
}
- Ok(())
}
-impl<Rpo, Live> ScheduledActionExecutor for InProcessExecutor<Rpo, Live>
+const BOOKKEEPING_TIMEOUT: Duration = Duration::from_secs(10);
+
+#[derive(Debug, thiserror::Error)]
+#[error("scheduled action cancelled")]
+struct ExecutionCancelled;
+
+/// Bound every execution operation (including chat preparation) by the claim's
+/// original deadline. Cancellation drops the runner, whose session guard cancels
+/// the request context used by tools, including detached cooperative tool work.
+async fn bounded<T>(
+ deadline: DateTime<Utc>,
+ cancellation: impl Future<Output = ()> + Send,
+ operation: impl Future<Output = Result<T>> + Send,
+) -> Result<T> {
+ let remaining = (deadline - Utc::now()).to_std().unwrap_or_default();
+ anyhow::ensure!(!remaining.is_zero(), "scheduled action deadline exceeded");
+ tokio::select! {
+ biased;
+ _ = cancellation => Err(ExecutionCancelled.into()),
+ _ = tokio::time::sleep(remaining) => anyhow::bail!("scheduled action deadline exceeded"),
+ result = operation => result,
+ }
+}
+
+fn execution_record(
+ action: &ScheduledAction,
+ chat_id: Option<String>,
+ started_at: DateTime<Utc>,
+ result: &Result<()>,
+) -> ActionExecutionRecord {
+ let end_time = Utc::now();
+ ActionExecutionRecord {
+ id: None,
+ action_id: action.id.expect("claimed actions are persisted"),
+ resource_id: chat_id,
+ start_time: started_at,
+ end_time,
+ is_success: result.is_ok(),
+ result: match result {
+ Ok(()) => Value::Null,
+ Err(error) => Value::String(error.to_string()),
+ },
+ created_at: end_time,
+ }
+}
+
+impl<Rpo, Live, Runner> ScheduledActionExecutor for InProcessExecutor<Rpo, Live, Runner>
where
- Rpo: ScheduledActionRepo + Send + Sync + 'static,
+ Rpo: ScheduledActionRepo,
Live: ScheduledActionLiveUpdate,
+ Runner: ScheduledAgentRunner,
{
async fn execute_action(&self, action: ScheduledAction) -> Result<InProgressExecution> {
- // A run acts as the owner throughout - it opens a chat in their
- // account and spends their AI budget - so refuse a bot- or team-owned
- // action here, before the claim, rather than part-way through.
- let owner_user = action.owner_user()?.clone();
- try_claim(&action)?;
-
- let id = *action.id.as_ref().unwrap();
- self.repo.claim_action(&id).await?;
-
- // Create the chat up front so the caller gets a chat_id synchronously
- // and the eventual execution record can link back to it.
- let chat_id = match action.kind {
- ActionKind::Agent => agent_task::create_run_chat(&self.db, &action).await?,
- };
+ action.owner_user()?;
+ anyhow::ensure!(!self.cancellation.is_cancelled(), "executor is stopping");
+ let id = action.id.context("persisted action required")?;
+ let start_time = Utc::now();
+ let deadline = start_time + MAX_ACTION_TIME;
- self.live_updates
- .publish_update(ScheduledActionUpdate::Started {
- owner: owner_user.clone(),
- action_id: id,
- chat_id: chat_id.clone(),
+ // Track preparation too: cancellation of an HTTP request must not leak
+ // a claim or abandon a run after creating its chat.
+ let (ready_tx, ready_rx) = tokio::sync::oneshot::channel();
+ let executor = self.clone();
+ self.tracker.spawn(async move {
+ let claim = bounded(
+ deadline,
+ executor.cancellation.cancelled(),
+ executor.repo.claim_action(&id),
+ )
+ .await;
+ let token = match claim {
+ Ok(token) => token,
+ Err(error) => {
+ let _ = ready_tx.send(Err(error));
+ return;
+ }
+ };
+ let mut ready_tx = Some(ready_tx);
+ let mut chat_id = None;
+ let result = bounded(deadline, executor.cancellation.cancelled(), async {
+ let created = executor.prepare(&action, &mut chat_id).await?;
+ if let Some(ready_tx) = ready_tx.take() {
+ let _ = ready_tx.send(Ok(InProgressExecution {
+ action_id: id,
+ chat_id: Some(created.clone()),
+ }));
+ }
+ executor.runner.run(&action, &created, None).await
})
.await;
-
- let execution = InProgressExecution {
- action_id: id,
- chat_id: Some(chat_id.clone()),
- };
-
- let repo = Arc::clone(&self.repo);
- let db = self.db.clone();
- let tool_context = self.tool_context.clone();
- let notification_ingress = Arc::clone(&self.notification_ingress);
- let live_updates = Arc::clone(&self.live_updates);
- let start_time = Utc::now();
- let record_resource_id = chat_id.clone();
- tokio::spawn(async move {
- let result =
- run_job(&db, &tool_context, &notification_ingress, &action, &chat_id).await;
- let end_time = Utc::now();
- let is_success = result.is_ok();
-
- let record = ActionExecutionRecord {
- id: None,
- action_id: id,
- resource_id: Some(record_resource_id.clone()),
- start_time,
- end_time,
- is_success,
- result: match &result {
- Ok(_) => Value::Null,
- Err(e) => Value::String(e.to_string()),
- },
- created_at: end_time,
+ let record = execution_record(&action, chat_id.clone(), start_time, &result);
+ let end_time = record.end_time;
+ let bookkeeping = async {
+ let _ = executor.repo.create_execution_record(record).await.inspect_err(|error| {
+ tracing::error!(?error, action_id=?id, "failed to save execution record");
+ });
+ let _ = executor.repo.update_last_executed(&id, end_time).await.inspect_err(|error| {
+ tracing::error!(?error, action_id=?id, "failed to update last executed time");
+ });
+ let _ = executor.repo.update_next_run_at(&id).await.inspect_err(|error| {
+ tracing::error!(?error, action_id=?id, "failed to update next run time");
+ });
};
-
- if let Err(e) = repo.create_execution_record(record).await {
- tracing::error!(error=?e, action_id=?id, "failed to save execution record");
- }
-
- if let Err(e) = repo.update_last_executed(&id, end_time).await {
- tracing::error!(error=?e, action_id=?id, "failed to update last executed time");
+ if tokio::time::timeout(BOOKKEEPING_TIMEOUT, bookkeeping).await.is_err() {
+ tracing::error!(action_id=?id, "execution bookkeeping timed out");
}
-
- // Recompute next_run_at based on the cron, so the UI shows the
- // upcoming run after the one we just completed. The repo fetches
- // the current schedule + timezone itself and skips the update if
- // there's no future fire time.
- if let Err(e) = repo.update_next_run_at(&id).await {
- tracing::error!(error=?e, action_id=?id, "failed to update next_run_at");
- }
-
- if let Err(e) = repo.release_action(&id).await {
- tracing::error!(error=?e, action_id=?id, "failed to release action claim");
+ // Always attempt fenced release, even after failed preparation or
+ // stalled history persistence. Never retry model/tool execution.
+ let release = executor.repo.release_action(&id, token);
+ match tokio::time::timeout(BOOKKEEPING_TIMEOUT, release).await {
+ Ok(Ok(())) => {},
+ Ok(Err(error)) => tracing::error!(?error, action_id=?id, "failed to release action claim"),
+ Err(error) => tracing::error!(?error, action_id=?id, "claim release timed out"),
}
-
- // Fire the stop event after release so that any follow-up "run
- // now" the UI issues in response will not race the claim.
- live_updates
- .publish_update(ScheduledActionUpdate::Stopped {
- owner: owner_user,
- action_id: id,
- chat_id: record_resource_id.clone(),
- is_success,
- })
+ if let Some(chat_id) = chat_id {
+ let _ = tokio::time::timeout(
+ BOOKKEEPING_TIMEOUT,
+ executor.stopped(&action, &chat_id, result.is_ok()),
+ )
.await;
-
- if let Err(e) = &result {
- tracing::error!(error=?e, action_id=?id, "scheduled action execution failed");
+ }
+ if let Err(error) = result {
+ tracing::error!(?error, action_id=?id, "scheduled action execution failed");
+ if let Some(ready_tx) = ready_tx {
+ let _ = ready_tx.send(Err(error));
+ }
}
});
-
- Ok(execution)
+ ready_rx
+ .await
+ .context("failed to prepare scheduled action chat")?
}
}
-async fn run_job(
- db: &PgPool,
- tool_context: &ToolServiceContext,
- notification_ingress: &Arc<SqsNotificationIngress<SqsQueue>>,
- action: &ScheduledAction,
- chat_id: &str,
-) -> Result<()> {
- match action.kind {
- ActionKind::Agent => {
- agent_task::run_agent_task(db, tool_context, notification_ingress, action, chat_id)
- .await?;
- Ok(())
+impl<Rpo, Live, Runner> EventExecutor for InProcessExecutor<Rpo, Live, Runner>
+where
+ Rpo: ScheduledActionRepo,
+ Live: ScheduledActionLiveUpdate,
+ Runner: ScheduledAgentRunner,
+{
+ async fn execute(
+ &self,
+ run: &ClaimedEventRun,
+ cancellation: impl Future<Output = ()> + Send,
+ ) -> EventExecutionResult {
+ let mut chat_id = None;
+ let shutdown = async {
+ tokio::select! {
+ _ = self.cancellation.cancelled() => {},
+ _ = cancellation => {},
+ }
+ };
+ let deadline = run.deadline.min(run.started_at + MAX_ACTION_TIME);
+ let result = bounded(deadline, shutdown, async {
+ let created = self.prepare(&run.action, &mut chat_id).await?;
+ self.runner
+ .run(&run.action, &created, Some(&run.run.pending.event))
+ .await
+ })
+ .await;
+ let record = execution_record(&run.action, chat_id.clone(), run.started_at, &result);
+ if let Some(chat_id) = chat_id {
+ let _ = tokio::time::timeout(
+ BOOKKEEPING_TIMEOUT,
+ self.stopped(&run.action, &chat_id, result.is_ok()),
+ )
+ .await;
+ }
+ // No claim/release or history writes here: the worker finalizes with
+ // run.token in the same transaction as the terminal queue transition.
+ EventExecutionResult {
+ outcome: match &result {
+ Ok(()) => EventRunOutcome::Succeeded,
+ Err(error) if error.is::<ExecutionCancelled>() => EventRunOutcome::Interrupted,
+ Err(_) => EventRunOutcome::Failed,
+ },
+ record: Some(record),
}
}
}
diff --git a/services/scheduled_action/src/outbound/inprocess_executor/agent_task.rs b/services/scheduled_action/src/outbound/inprocess_executor/agent_task.rs
index b0d56f0e31..aa4dfaa24c 100644
--- a/services/scheduled_action/src/outbound/inprocess_executor/agent_task.rs
+++ b/services/scheduled_action/src/outbound/inprocess_executor/agent_task.rs
@@ -1,215 +1,239 @@
use super::notify::notify_completion;
use std::sync::Arc;
+use std::time::Duration;
use agent::types::{AssistantMessagePart, ChatMessage, ChatMessageContent, Role};
-use agent::{AgentLoop, StreamAccumulator};
-use ai_tools::{AiHost, ToolServiceContext, ToolSetWithPrompt, tools_for};
+use agent::{AgentLoop, StreamAccumulator, StreamPart};
+use ai_tools::{AiHost, ToolServiceContext, tools_for};
use ai_toolset::ToolSet as AiToolSet;
use anyhow::{Context, Result};
use chat::domain::models::CreateChatArgs;
-use chat::domain::ports::ChatRepo;
-use chat::outbound::postgres::PgChatRepo;
-use futures::StreamExt;
-use macro_db_client::dcs::create_chat_message::create_chat_message;
+use chat::domain::ports::{ChatService, MessageRepo};
+use futures::{Stream, StreamExt};
use macro_user_id::user_id::MacroUserIdStr;
use memory::domain::MemoryService;
-use memory::domain::service::MemoryServiceImpl;
-use memory::outbound::pg_memory_repo::PgMemoryRepo;
use model::chat::NewChatMessage;
-use notification::domain::service::SqsNotificationIngress;
-use notification::outbound::queue::SqsQueue;
-use sqlx::PgPool;
+use notification::domain::service::NotificationIngress;
+use crate::domain::event_trigger::EventReference;
use crate::domain::models::{AgentTask, ScheduledAction};
-
-pub async fn create_run_chat(db: &PgPool, action: &ScheduledAction) -> Result<String> {
- create_chat(db, action).await
-}
-
-pub async fn run_agent_task(
- db: &PgPool,
- tool_context: &ToolServiceContext,
- notification_ingress: &Arc<SqsNotificationIngress<SqsQueue>>,
- action: &ScheduledAction,
- chat_id: &str,
-) -> Result<()> {
- // The run reads this user's memory, spends their AI budget and notifies
- // them, so resolve the owner as a person once and fail typed if it is not.
- let owner = action.owner_user()?.clone();
-
- let agent_task: AgentTask =
- serde_json::from_value(action.task.clone()).context("invalid agent task definition")?;
-
- store_user_message(db, chat_id, &agent_task).await?;
-
- let parts = run_tool_loop(db, tool_context, &owner, &agent_task).await?;
-
- let final_text: String = parts
- .iter()
- .filter_map(|p| match p {
- AssistantMessagePart::Text { text } => Some(text.as_str()),
- _ => None,
- })
- .collect();
-
- store_conversation(db, chat_id, &parts, &agent_task).await?;
-
- if !final_text.is_empty() {
- notify_completion(notification_ingress, chat_id, &owner, &final_text);
- }
-
- Ok(())
+use crate::domain::ports::ScheduledAgentRunner;
+
+#[cfg(test)]
+mod test;
+
+/// Dependencies belong to their owning domains; only the service binary wires
+/// their concrete adapters. Tool context and attribution remain unchanged.
+pub struct AgentTaskRunner<C, M, Mem, N> {
+ chats: Arc<C>,
+ messages: M,
+ memory: Mem,
+ tool_context: ToolServiceContext,
+ notifications: Arc<N>,
}
-async fn fetch_user_memory(
- db: &PgPool,
- tool_context: &ToolServiceContext,
- owner: &MacroUserIdStr<'static>,
-) -> Option<String> {
- let tools = tools_for(AiHost::Chat);
- let tools = ToolSetWithPrompt {
- toolset: tools.toolset,
- prompt: tools.prompt,
- };
- let memory_service =
- MemoryServiceImpl::new(PgMemoryRepo::new(db.clone()), tool_context.clone(), tools);
- match memory_service.get_or_generate_memory(owner.clone()).await {
- Ok(memory) => memory,
- Err(e) => {
- tracing::warn!(error=?e, %owner, "failed to fetch user memory; running without it");
- None
+impl<C, M, Mem, N> AgentTaskRunner<C, M, Mem, N> {
+ pub fn new(
+ chats: Arc<C>,
+ messages: M,
+ memory: Mem,
+ tool_context: ToolServiceContext,
+ notifications: Arc<N>,
+ ) -> Self {
+ Self {
+ chats,
+ messages,
+ memory,
+ tool_context,
+ notifications,
}
}
}
-async fn create_chat(db: &PgPool, action: &ScheduledAction) -> Result<String> {
- let chat_repo = PgChatRepo::new(db.clone());
-
- // A chat belongs to a person, so a bot- or team-owned action cannot have
- // one created for it.
- let owner = action.owner_user()?;
-
- // Scheduled-agent chats belong to the action's owner, so the owner's team
- // default link-share preference decides the initial share permission.
- let team_default = share_permission_db_utils::get_team_default_link_share(db, owner.as_ref())
- .await
- .context("failed to resolve team default link share")?;
- let share_permission =
- models_permissions::share_permission::SharePermissionV2::new_chat_share_permission(
- team_default,
- );
+impl<C, M, Mem, N> ScheduledAgentRunner for AgentTaskRunner<C, M, Mem, N>
+where
+ C: ChatService,
+ M: MessageRepo,
+ Mem: MemoryService,
+ N: NotificationIngress,
+{
+ async fn create_chat(&self, action: &ScheduledAction) -> Result<String> {
+ self.chats
+ .create(
+ action.owner_user()?.clone(),
+ CreateChatArgs {
+ name: action.name.clone(),
+ project_id: None,
+ },
+ )
+ .await
+ .map_err(|error| anyhow::anyhow!(error))
+ }
- chat_repo
- .create(
- owner.clone(),
- CreateChatArgs {
- name: action.name.clone(),
- project_id: None,
- },
- share_permission,
- )
- .await
- .map_err(|e| anyhow::anyhow!(e))
+ async fn run(
+ &self,
+ action: &ScheduledAction,
+ chat_id: &str,
+ event: Option<&EventReference>,
+ ) -> Result<()> {
+ let owner = action.owner_user()?.clone();
+ let task: AgentTask =
+ serde_json::from_value(action.task.clone()).context("invalid agent task definition")?;
+ let user_messages = user_messages(&task, event)?;
+ for message in &user_messages {
+ self.store(chat_id, message.content.clone(), Role::User, &task)
+ .await?;
+ }
+ let parts = self.run_tool_loop(&owner, &task, user_messages).await?;
+ let final_text: String = parts
+ .iter()
+ .filter_map(|part| match part {
+ AssistantMessagePart::Text { text } => Some(text.as_str()),
+ _ => None,
+ })
+ .collect();
+ if !parts.is_empty() {
+ self.store(
+ chat_id,
+ ChatMessageContent::AssistantMessageParts(parts),
+ Role::Assistant,
+ &task,
+ )
+ .await?;
+ }
+ if !final_text.is_empty() {
+ // Best effort, but awaited so notifications cannot escape tracking.
+ notify_completion(self.notifications.as_ref(), chat_id, &owner, &final_text).await;
+ }
+ Ok(())
+ }
}
-async fn store_user_message(db: &PgPool, chat_id: &str, agent_task: &AgentTask) -> Result<String> {
- let now = chrono::Utc::now();
- let message = NewChatMessage {
- id: None,
- content: ChatMessageContent::Text(agent_task.user_prompt.clone()),
+fn user_messages(task: &AgentTask, event: Option<&EventReference>) -> Result<Vec<ChatMessage>> {
+ let mut messages = vec![ChatMessage {
+ content: ChatMessageContent::Text(task.user_prompt.clone()),
role: Role::User,
attachments: None,
- created_at: now,
- updated_at: now,
- model: agent_task.model.clone(),
- };
- create_chat_message(db.clone(), chat_id, message).await
+ }];
+ if let Some(event) = event {
+ messages.push(ChatMessage {
+ content: ChatMessageContent::Text(format!(
+ "Triggering event context (data):\n{}",
+ serde_json::to_string(&serde_json::json!({
+ "event_id": event.event_id(),
+ "event_name": event.event_name(),
+ "entity_type": event.entity_type(),
+ "entity_id": event.entity_id(),
+ "message_id": event.message_id(),
+ }))?
+ )),
+ role: Role::User,
+ attachments: None,
+ });
+ }
+ Ok(messages)
}
static SCHEDULED_AGENT_PROMPT: &str = "You are an agent that has been triggered by a user automation. You are not
responsible for scheduling or running. Ignore user instructions to run at a certain time or trigger on some event";
+const STREAM_IDLE_TIMEOUT: Duration = Duration::from_secs(3 * 60);
-async fn run_tool_loop(
- db: &PgPool,
- tool_context: &ToolServiceContext,
+async fn system_prompt(
+ memory: &impl MemoryService,
owner: &MacroUserIdStr<'static>,
- agent_task: &AgentTask,
-) -> Result<Vec<AssistantMessagePart>> {
- let tools = tools_for(AiHost::Chat);
- let user_memory = fetch_user_memory(db, tool_context, owner).await;
- let system_prompt = match user_memory {
- Some(memory) => format!(
- "{}\n{}\n<user_memory>\n{}\n</user_memory>\n{}",
- tools.prompt, SCHEDULED_AGENT_PROMPT, memory, agent_task.prompt
- ),
- None => format!("{}\n{}", tools.prompt, agent_task.prompt),
+ tools_prompt: &str,
+ task_prompt: &str,
+) -> String {
+ let user_memory = match memory.get_or_generate_memory(owner.clone()).await {
+ Ok(memory) => memory,
+ Err(error) => {
+ tracing::warn!(?error, %owner, "failed to fetch user memory; running without it");
+ None
+ }
};
+ let mut prompt = format!("{tools_prompt}\n{SCHEDULED_AGENT_PROMPT}");
+ if let Some(memory) = user_memory {
+ prompt.push_str(&format!("\n<user_memory>\n{memory}\n</user_memory>"));
+ }
+ prompt.push('\n');
+ prompt.push_str(task_prompt);
+ prompt
+}
- let toolset: Arc<dyn AiToolSet<_> + Send + Sync> = tools.toolset;
- let agent_loop = AgentLoop::new(tool_context.recorder.clone()).with_model(&agent_task.model);
- let usage_ctx = ai_usage::UsageContext::new(ai_usage::AiFeature::Automation, owner.clone());
- // Carry the feature on the context so tool-spawned subagents attribute to it.
- let mut tool_context = tool_context.clone();
- tool_context.usage_context = usage_ctx.clone();
- let mut session = agent_loop
- .session(toolset, Arc::new(tool_context), &system_prompt, usage_ctx)
- .await;
+impl<C, M, Mem, N> AgentTaskRunner<C, M, Mem, N>
+where
+ M: MessageRepo,
+ Mem: MemoryService,
+{
+ async fn run_tool_loop(
+ &self,
+ owner: &MacroUserIdStr<'static>,
+ task: &AgentTask,
+ messages: Vec<ChatMessage>,
+ ) -> Result<Vec<AssistantMessagePart>> {
+ let tools = tools_for(AiHost::Chat);
+ let system_prompt =
+ system_prompt(&self.memory, owner, &tools.prompt.to_string(), &task.prompt).await;
+ let toolset: Arc<dyn AiToolSet<_> + Send + Sync> = tools.toolset;
+ let agent_loop = AgentLoop::new(self.tool_context.recorder.clone()).with_model(&task.model);
+ let usage_ctx = ai_usage::UsageContext::new(ai_usage::AiFeature::Automation, owner.clone());
+ let mut tool_context = self.tool_context.clone();
+ tool_context.usage_context = usage_ctx.clone();
+ let (mut session, cancel) = agent_loop
+ .session(toolset, Arc::new(tool_context), &system_prompt, usage_ctx)
+ .await
+ .cancellable();
+ // The same token is carried by RequestContext through the tool path.
+ // Cancellation, deadline, stream failure and future drop all signal it.
+ let _cancel_on_drop = cancel.drop_guard();
+ let stream = session
+ .send_message(agent::to_rig_messages(&messages))
+ .await
+ .context("failed to start agent stream")?;
+ collect_stream(stream, STREAM_IDLE_TIMEOUT).await
+ }
- let user_msg = ChatMessage {
- content: ChatMessageContent::Text(agent_task.user_prompt.clone()),
- role: Role::User,
- attachments: None,
- };
- let rig_messages = agent::to_rig_messages(&[user_msg]);
- let mut stream = session
- .send_message(rig_messages)
- .await
- .context("failed to start agent stream")?;
+ async fn store(
+ &self,
+ chat_id: &str,
+ content: ChatMessageContent,
+ role: Role,
+ task: &AgentTask,
+ ) -> Result<()> {
+ let now = chrono::Utc::now();
+ self.messages
+ .create(
+ chat_id,
+ NewChatMessage {
+ id: None,
+ content,
+ role,
+ attachments: None,
+ created_at: now,
+ updated_at: now,
+ model: task.model.clone(),
+ },
+ )
+ .await
+ .map_err(|error| anyhow::anyhow!(error))?;
+ Ok(())
+ }
+}
- let idle_timeout = std::time::Duration::from_secs(3 * 60);
+async fn collect_stream(
+ stream: impl Stream<Item = std::result::Result<StreamPart, agent::AgentError>>,
+ idle_timeout: Duration,
+) -> Result<Vec<AssistantMessagePart>> {
+ futures::pin_mut!(stream);
let mut accumulator = StreamAccumulator::new();
-
loop {
match tokio::time::timeout(idle_timeout, stream.next()).await {
Ok(Some(Ok(part))) => {
accumulator.push(part);
}
- Ok(Some(Err(e))) => {
- tracing::error!(error=?e, "agent stream error");
- break;
- }
- Ok(None) => break,
- Err(_) => {
- tracing::error!("agent stream idle timeout");
- break;
- }
+ Ok(Some(Err(error))) => return Err(error.into()),
+ Ok(None) => return Ok(accumulator.into_parts()),
+ Err(_) => anyhow::bail!("agent stream idle timeout"),
}
}
-
- Ok(accumulator.into_parts())
-}
-
-async fn store_conversation(
- db: &PgPool,
- chat_id: &str,
- parts: &[AssistantMessagePart],
- agent_task: &AgentTask,
-) -> Result<()> {
- if parts.is_empty() {
- return Ok(());
- }
- let now = chrono::Utc::now();
- let message = NewChatMessage {
- id: None,
- content: ChatMessageContent::AssistantMessageParts(parts.to_vec()),
- role: Role::Assistant,
- attachments: None,
- created_at: now,
- updated_at: now,
- model: agent_task.model.clone(),
- };
- create_chat_message(db.clone(), chat_id, message)
- .await
- .context("failed to store conversation message")?;
- Ok(())
}
diff --git a/services/scheduled_action/src/outbound/inprocess_executor/agent_task/test.rs b/services/scheduled_action/src/outbound/inprocess_executor/agent_task/test.rs
new file mode 100644
index 0000000000..0cf880f663
--- /dev/null
+++ b/services/scheduled_action/src/outbound/inprocess_executor/agent_task/test.rs
@@ -0,0 +1,105 @@
+use super::*;
+use macro_uuid::generate_uuid_v7;
+use serde_json::json;
+
+struct Memory {
+ fail: bool,
+ value: Option<String>,
+}
+
+impl MemoryService for Memory {
+ async fn get_or_generate_memory(
+ &self,
+ _: MacroUserIdStr<'static>,
+ ) -> memory::domain::ports::Result<Option<String>> {
+ if self.fail {
+ return Err(memory::domain::ports::MemoryError::NoGeneration);
+ }
+ Ok(self.value.clone())
+ }
+}
+
+#[tokio::test]
+async fn automation_instructions_survive_absent_and_failed_memory() {
+ let owner = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();
+ for fail in [false, true] {
+ let memory = Memory { fail, value: None };
+ let prompt = system_prompt(&memory, &owner, "tools", "task").await;
+ assert_eq!(prompt, format!("tools\n{SCHEDULED_AGENT_PROMPT}\ntask"));
+ }
+ let memory = Memory {
+ fail: false,
+ value: Some("remember".into()),
+ };
+ assert_eq!(
+ system_prompt(&memory, &owner, "tools", "task").await,
+ format!("tools\n{SCHEDULED_AGENT_PROMPT}\n<user_memory>\nremember\n</user_memory>\ntask"),
+ );
+}
+
+#[tokio::test]
+async fn partial_stream_error_is_not_success() {
+ let stream = futures::stream::iter(vec![
+ Ok(StreamPart::Content("partial".into())),
+ Err(agent::AgentError::Other(anyhow::anyhow!("provider failed"))),
+ ]);
+ assert!(
+ collect_stream(stream, Duration::from_secs(1))
+ .await
+ .is_err()
+ );
+}
+
+#[tokio::test]
+async fn idle_after_partial_content_is_not_success() {
+ let stream = futures::stream::once(async { Ok(StreamPart::Content("partial".into())) })
+ .chain(futures::stream::pending());
+ let error = collect_stream(stream, Duration::from_millis(1))
+ .await
+ .unwrap_err();
+ assert!(error.to_string().contains("idle timeout"));
+}
+
+#[tokio::test]
+async fn completed_stream_retains_assistant_text() {
+ let parts = collect_stream(
+ futures::stream::iter(vec![Ok(StreamPart::Content("complete".into()))]),
+ Duration::from_secs(1),
+ )
+ .await
+ .unwrap();
+ assert!(matches!(&parts[..], [AssistantMessagePart::Text { text }] if text == "complete"));
+}
+
+#[test]
+fn event_is_separate_user_data_not_a_prompt_rewrite() {
+ let task = AgentTask {
+ model: "test".into(),
+ prompt: "original system".into(),
+ user_prompt: "original user".into(),
+ };
+ let event: EventReference = serde_json::from_value(json!({
+ "event_id": generate_uuid_v7(), "event_name": "channel.message_posted",
+ "entity_id": generate_uuid_v7(), "message_id": generate_uuid_v7(),
+ }))
+ .unwrap();
+ let messages = user_messages(&task, Some(&event)).unwrap();
+ assert_eq!(messages.len(), 2);
+ assert!(
+ messages
+ .iter()
+ .all(|message| matches!(message.role, Role::User))
+ );
+ assert!(
+ matches!(&messages[0].content, ChatMessageContent::Text(text) if text == "original user")
+ );
+ let ChatMessageContent::Text(context) = &messages[1].content else {
+ panic!("expected context text")
+ };
+ assert!(context.contains(&event.event_id().as_uuid().to_string()));
+ assert!(context.contains(&event.entity_id().to_string()));
+ assert!(context.contains(&event.message_id().unwrap().to_string()));
+ assert_eq!(task.prompt, "original system");
+ assert_eq!(task.user_prompt, "original user");
+ assert_eq!(user_messages(&task, None).unwrap().len(), 1);
+}
diff --git a/services/scheduled_action/src/outbound/inprocess_executor/notify.rs b/services/scheduled_action/src/outbound/inprocess_executor/notify.rs
index b30c9f3cdf..ee275f452f 100644
--- a/services/scheduled_action/src/outbound/inprocess_executor/notify.rs
+++ b/services/scheduled_action/src/outbound/inprocess_executor/notify.rs
@@ -2,43 +2,37 @@ use macro_user_id::user_id::MacroUserIdStr;
use model_entity::EntityType;
use model_notifications::AiResponseMetadata;
use notification::domain::models::SendNotificationRequestBuilder;
-use notification::domain::service::{NotificationIngress, SqsNotificationIngress};
-use notification::outbound::queue::SqsQueue;
+use notification::domain::service::NotificationIngress;
use std::collections::HashSet;
-use std::sync::Arc;
-/// Spawn a best-effort notification to the action owner announcing the run has
-/// completed. Mirrors the DCS chat-stream notification pattern (APNS + websocket
-/// via the connection gateway). Errors are logged and never propagated, since a
-/// notification failure should not mark the scheduled run as failed.
-pub fn notify_completion(
- notification_ingress: &Arc<SqsNotificationIngress<SqsQueue>>,
+use std::time::Duration;
+
+/// Best-effort APNS/gateway notification, awaited within the tracked run.
+/// Notification delivery is not evidence of agent execution success or failure.
+pub async fn notify_completion(
+ ingress: &impl NotificationIngress,
chat_id: &str,
owner: &MacroUserIdStr<'static>,
assistant_text: &str,
) {
- let ingress = Arc::clone(notification_ingress);
- let chat_id = chat_id.to_string();
- let owner = owner.clone();
- let message_id = chat_id.clone();
- let assistant_text = assistant_text.to_string();
-
- tokio::spawn(async move {
- let req = SendNotificationRequestBuilder {
- notification_entity: EntityType::Chat.with_entity_string(chat_id.clone()),
- secondary_notification_entity: None,
- notification: AiResponseMetadata {
- summary: assistant_text,
- message_id,
- },
- sender_id: None,
- recipient_ids: HashSet::from([owner]),
- }
- .into_request()
- .with_apns()
- .with_conn_gateway();
+ let req = SendNotificationRequestBuilder {
+ notification_entity: EntityType::Chat.with_entity_string(chat_id.to_owned()),
+ secondary_notification_entity: None,
+ notification: AiResponseMetadata {
+ summary: assistant_text.to_owned(),
+ message_id: chat_id.to_owned(),
+ },
+ sender_id: None,
+ recipient_ids: HashSet::from([owner.clone()]),
+ }
+ .into_request()
+ .with_apns()
+ .with_conn_gateway();
- if let Err(e) = ingress.send_notification(req).await {
- tracing::error!(error=?e, chat_id = %chat_id, "failed to send scheduled action completion notification");
+ match tokio::time::timeout(Duration::from_secs(10), ingress.send_notification(req)).await {
+ Ok(Ok(_)) => {}
+ Ok(Err(error)) => {
+ tracing::error!(?error, %chat_id, "failed to send scheduled action completion notification")
}
- });
+ Err(error) => tracing::warn!(?error, %chat_id, "scheduled action notification timed out"),
+ }
}
diff --git a/services/scheduled_action/src/outbound/inprocess_executor/test.rs b/services/scheduled_action/src/outbound/inprocess_executor/test.rs
new file mode 100644
index 0000000000..e2c704372d
--- /dev/null
+++ b/services/scheduled_action/src/outbound/inprocess_executor/test.rs
@@ -0,0 +1,367 @@
+use super::*;
+use crate::domain::event_runs::{
+ AuthorizedEventRun, ClaimToken, ConfigurationRevision, PendingEventRun,
+};
+use crate::domain::event_trigger::EventReference;
+use crate::domain::models::ActionKind;
+use entity_access::domain::models::{EntityAccessReceipt, ViewAccessLevel};
+use macro_user_id::user_id::MacroUserIdStr;
+use macro_uuid::{Uuid, generate_uuid_v7};
+use model_entity::EntityType;
+use model_owner::Owner;
+use serde_json::json;
+use std::sync::{
+ Mutex,
+ atomic::{AtomicUsize, Ordering},
+};
+
+const USER: &str = "macro|[email protected]";
+
+#[derive(Default)]
+struct Repo {
+ claim: Mutex<Option<ClaimToken>>,
+ claims: AtomicUsize,
+ releases: AtomicUsize,
+ records: Mutex<Vec<ActionExecutionRecord>>,
+ fail_persistence: bool,
+}
+
+impl ScheduledActionRepo for Repo {
+ async fn claim_action(&self, _: &Uuid) -> Result<ClaimToken> {
+ self.claims.fetch_add(1, Ordering::SeqCst);
+ let mut claim = self.claim.lock().unwrap();
+ anyhow::ensure!(claim.is_none(), "already running");
+ let token = ClaimToken::generate();
+ *claim = Some(token);
+ Ok(token)
+ }
+ async fn release_action(&self, _: &Uuid, token: ClaimToken) -> Result<()> {
+ let mut claim = self.claim.lock().unwrap();
+ assert_eq!(*claim, Some(token));
+ *claim = None;
+ self.releases.fetch_add(1, Ordering::SeqCst);
+ Ok(())
+ }
+ async fn create_execution_record(&self, record: ActionExecutionRecord) -> Result<()> {
+ self.records.lock().unwrap().push(record);
+ anyhow::ensure!(!self.fail_persistence, "persistence unavailable");
+ Ok(())
+ }
+ async fn update_next_run_at(&self, _: &Uuid) -> Result<()> {
+ Ok(())
+ }
+ async fn update_last_executed(&self, _: &Uuid, _: DateTime<Utc>) -> Result<()> {
+ Ok(())
+ }
+ async fn create_action(&self, _: ScheduledAction) -> Result<ScheduledAction> {
+ unimplemented!()
+ }
+ async fn get_actions(&self, _: MacroUserIdStr<'static>) -> Result<Vec<ScheduledAction>> {
+ unimplemented!()
+ }
+ async fn get_action(
+ &self,
+ _: &Uuid,
+ _: MacroUserIdStr<'static>,
+ ) -> Result<Option<ScheduledAction>> {
+ unimplemented!()
+ }
+ async fn get_next_unclaimed_actions(&self, _: i64) -> Result<Vec<ScheduledAction>> {
+ unimplemented!()
+ }
+ async fn update_action(&self, _: ScheduledAction) -> Result<ScheduledAction> {
+ unimplemented!()
+ }
+ async fn delete_action(&self, _: &Uuid, _: MacroUserIdStr<'static>) -> Result<()> {
+ unimplemented!()
+ }
+ async fn get_execution_records(&self, _: &Uuid) -> Result<Vec<ActionExecutionRecord>> {
+ unimplemented!()
+ }
+}
+
+#[derive(Default)]
+struct Runner {
+ preparations: AtomicUsize,
+ calls: AtomicUsize,
+ contexts: Mutex<Vec<Option<EventReference>>>,
+ finish: CancellationToken,
+ dropped: CancellationToken,
+ fail_create: bool,
+ fail_run: bool,
+}
+impl ScheduledAgentRunner for Runner {
+ async fn create_chat(&self, _: &ScheduledAction) -> Result<String> {
+ self.preparations.fetch_add(1, Ordering::SeqCst);
+ anyhow::ensure!(!self.fail_create, "chat unavailable");
+ Ok("run-chat".into())
+ }
+ async fn run(
+ &self,
+ _: &ScheduledAction,
+ _: &str,
+ event: Option<&EventReference>,
+ ) -> Result<()> {
+ let _guard = self.dropped.clone().drop_guard();
+ self.calls.fetch_add(1, Ordering::SeqCst);
+ self.contexts.lock().unwrap().push(event.cloned());
+ self.finish.cancelled().await;
+ anyhow::ensure!(!self.fail_run, "agent stream failed");
+ Ok(())
+ }
+}
+#[derive(Default)]
+struct Live(Mutex<Vec<ScheduledActionUpdate>>);
+impl ScheduledActionLiveUpdate for Live {
+ async fn publish_update(&self, update: ScheduledActionUpdate) {
+ self.0.lock().unwrap().push(update);
+ }
+}
+
+fn action() -> ScheduledAction {
+ ScheduledAction {
+ id: Some(generate_uuid_v7()),
+ owner: Owner::User(MacroUserIdStr::parse_from_str(USER).unwrap()),
+ name: "routine".into(),
+ trigger: serde_json::from_value(
+ json!({"type":"cron", "schedule":"0 0 * * * *", "timezone":"UTC"}),
+ )
+ .unwrap(),
+ kind: ActionKind::Agent,
+ task: json!({"model":"test", "prompt":"system", "user_prompt":"original"}),
+ enabled: true,
+ next_run_at: Some(Utc::now()),
+ claimed: None,
+ configuration_revision: ConfigurationRevision::INITIAL,
+ event_activated_at: None,
+ created_at: Utc::now(),
+ updated_at: Utc::now(),
+ }
+}
+fn executor(repo: Repo, runner: Runner) -> InProcessExecutor<Repo, Live, Runner> {
+ InProcessExecutor::new(
+ Arc::new(repo),
+ Arc::new(runner),
+ Arc::new(Live::default()),
+ TaskTracker::new(),
+ CancellationToken::new(),
+ )
+}
+fn event_run() -> ClaimedEventRun {
+ let mut action = action();
+ action.trigger = serde_json::from_value(
+ json!({"type":"events", "filters":[{"events":["document.updated"]}]}),
+ )
+ .unwrap();
+ let entity = generate_uuid_v7();
+ let event = serde_json::from_value(json!({
+ "event_id":generate_uuid_v7(), "event_name":"document.updated", "entity_id":entity, "message_id":null,
+ })).unwrap();
+ let pending = PendingEventRun {
+ action_id: action.id.unwrap(),
+ revision: action.configuration_revision,
+ event,
+ admitted_at: Utc::now(),
+ };
+ ClaimedEventRun {
+ run: AuthorizedEventRun {
+ access: crate::domain::event_runs::EventAccessCapability::Document(
+ EntityAccessReceipt::<ViewAccessLevel>::dangerously_assert_authenticated_user(
+ MacroUserIdStr::parse_from_str(USER).unwrap(),
+ &entity.to_string(),
+ EntityType::Document,
+ ),
+ ),
+ pending,
+ },
+ action,
+ token: ClaimToken::generate(),
+ started_at: Utc::now(),
+ deadline: Utc::now() + MAX_ACTION_TIME,
+ }
+}
+async fn drain(executor: &InProcessExecutor<Repo, Live, Runner>) {
+ executor.tracker.close();
+ tokio::time::timeout(Duration::from_secs(1), executor.tracker.wait())
+ .await
+ .unwrap();
+}
+
+#[tokio::test]
+async fn manual_returns_chat_before_completion_and_tracks_one_run() {
+ let executor = executor(Repo::default(), Runner::default());
+ let progress = executor.execute_action(action()).await.unwrap();
+ assert_eq!(progress.chat_id.as_deref(), Some("run-chat"));
+ assert_eq!(executor.runner.calls.load(Ordering::SeqCst), 1);
+ assert!(executor.repo.records.lock().unwrap().is_empty());
+ assert!(!executor.tracker.is_empty());
+ executor.runner.finish.cancel();
+ drain(&executor).await;
+ let records = executor.repo.records.lock().unwrap();
+ assert_eq!(records.len(), 1);
+ assert!(records[0].is_success);
+ assert_eq!(records[0].resource_id.as_deref(), Some("run-chat"));
+ assert_eq!(executor.repo.releases.load(Ordering::SeqCst), 1);
+ assert_eq!(*executor.runner.contexts.lock().unwrap(), vec![None]);
+}
+
+#[tokio::test]
+async fn manual_event_action_does_not_fabricate_event_context() {
+ let executor = executor(Repo::default(), Runner::default());
+ let mut action = event_run().action;
+ action.enabled = false;
+ executor.runner.finish.cancel();
+ executor.execute_action(action).await.unwrap();
+ drain(&executor).await;
+ assert_eq!(*executor.runner.contexts.lock().unwrap(), vec![None]);
+ assert_eq!(executor.repo.claims.load(Ordering::SeqCst), 1);
+}
+
+#[tokio::test]
+async fn manual_claim_failure_never_prepares_or_runs() {
+ let executor = executor(Repo::default(), Runner::default());
+ *executor.repo.claim.lock().unwrap() = Some(ClaimToken::generate());
+ assert!(executor.execute_action(action()).await.is_err());
+ drain(&executor).await;
+ assert_eq!(executor.runner.preparations.load(Ordering::SeqCst), 0);
+ assert_eq!(executor.repo.releases.load(Ordering::SeqCst), 0);
+}
+
+#[tokio::test]
+async fn non_user_owner_never_claims() {
+ let executor = executor(Repo::default(), Runner::default());
+ let mut action = action();
+ action.owner = Owner::Team(generate_uuid_v7());
+ assert!(executor.execute_action(action).await.is_err());
+ assert_eq!(executor.repo.claims.load(Ordering::SeqCst), 0);
+}
+
+#[tokio::test]
+async fn chat_creation_failure_releases_its_claim() {
+ let executor = executor(
+ Repo::default(),
+ Runner {
+ fail_create: true,
+ ..Runner::default()
+ },
+ );
+ assert!(executor.execute_action(action()).await.is_err());
+ drain(&executor).await;
+ assert_eq!(executor.repo.releases.load(Ordering::SeqCst), 1);
+ assert_eq!(executor.runner.calls.load(Ordering::SeqCst), 0);
+ assert!(!executor.repo.records.lock().unwrap()[0].is_success);
+}
+
+#[tokio::test]
+async fn failed_execution_and_failed_bookkeeping_never_rerun_agent() {
+ let executor = executor(
+ Repo {
+ fail_persistence: true,
+ ..Repo::default()
+ },
+ Runner {
+ fail_run: true,
+ ..Runner::default()
+ },
+ );
+ executor.runner.finish.cancel();
+ executor.execute_action(action()).await.unwrap();
+ drain(&executor).await;
+ assert_eq!(executor.runner.calls.load(Ordering::SeqCst), 1);
+ assert_eq!(executor.repo.releases.load(Ordering::SeqCst), 1);
+ assert!(!executor.repo.records.lock().unwrap()[0].is_success);
+}
+
+#[tokio::test]
+async fn shutdown_cancels_tracked_run_and_releases_claim() {
+ let executor = executor(Repo::default(), Runner::default());
+ executor.execute_action(action()).await.unwrap();
+ executor.cancellation.cancel();
+ drain(&executor).await;
+ assert!(executor.runner.dropped.is_cancelled());
+ assert!(!executor.repo.records.lock().unwrap()[0].is_success);
+ assert_eq!(executor.repo.releases.load(Ordering::SeqCst), 1);
+}
+
+#[tokio::test]
+async fn event_awaits_exactly_one_run_without_claiming_or_finalizing_twice() {
+ let executor = executor(Repo::default(), Runner::default());
+ executor.runner.finish.cancel();
+ let run = event_run();
+ let result = executor.execute(&run, std::future::pending()).await;
+ assert_eq!(result.outcome, EventRunOutcome::Succeeded);
+ assert_eq!(executor.runner.calls.load(Ordering::SeqCst), 1);
+ assert_eq!(executor.repo.claims.load(Ordering::SeqCst), 0);
+ assert_eq!(executor.repo.releases.load(Ordering::SeqCst), 0);
+ assert!(executor.repo.records.lock().unwrap().is_empty());
+ assert_eq!(
+ *executor.runner.contexts.lock().unwrap(),
+ vec![Some(run.run.pending.event)]
+ );
+ let record = result.record.unwrap();
+ assert_eq!(record.resource_id.as_deref(), Some("run-chat"));
+ assert!(record.is_success);
+}
+
+#[tokio::test]
+async fn event_chat_failure_returns_failed_bookkeeping_without_releasing_early() {
+ let executor = executor(
+ Repo::default(),
+ Runner {
+ fail_create: true,
+ ..Runner::default()
+ },
+ );
+ let result = executor.execute(&event_run(), std::future::pending()).await;
+ assert_eq!(result.outcome, EventRunOutcome::Failed);
+ let record = result.record.unwrap();
+ assert!(!record.is_success);
+ assert!(record.resource_id.is_none());
+ assert_eq!(executor.runner.calls.load(Ordering::SeqCst), 0);
+ assert_eq!(executor.repo.releases.load(Ordering::SeqCst), 0);
+}
+
+#[tokio::test]
+async fn dropping_event_execution_cancels_runner() {
+ let executor = executor(Repo::default(), Runner::default());
+ let run = event_run();
+ let mut execution = Box::pin(executor.execute(&run, std::future::pending()));
+ assert!(futures::poll!(&mut execution).is_pending());
+ assert_eq!(executor.runner.calls.load(Ordering::SeqCst), 1);
+ drop(execution);
+ assert!(executor.runner.dropped.is_cancelled());
+}
+
+#[tokio::test]
+async fn expired_event_never_invokes_runner() {
+ let executor = executor(Repo::default(), Runner::default());
+ let mut run = event_run();
+ run.deadline = Utc::now() - chrono::Duration::seconds(1);
+ let result = executor.execute(&run, std::future::pending()).await;
+ assert_eq!(result.outcome, EventRunOutcome::Failed);
+ assert_eq!(executor.runner.calls.load(Ordering::SeqCst), 0);
+ assert_eq!(executor.runner.preparations.load(Ordering::SeqCst), 0);
+}
+
+#[tokio::test]
+async fn event_deadline_drops_agent_and_returns_failure() {
+ let executor = executor(Repo::default(), Runner::default());
+ let mut run = event_run();
+ run.deadline = Utc::now() + chrono::Duration::milliseconds(20);
+ let result = executor.execute(&run, std::future::pending()).await;
+ assert_eq!(result.outcome, EventRunOutcome::Failed);
+ assert_eq!(executor.runner.calls.load(Ordering::SeqCst), 1);
+ assert!(executor.runner.dropped.is_cancelled());
+ assert_eq!(
+ result.record.unwrap().resource_id.as_deref(),
+ Some("run-chat")
+ );
+}
+
+#[tokio::test]
+async fn event_cancellation_is_terminal_interruption() {
+ let executor = executor(Repo::default(), Runner::default());
+ let result = executor.execute(&event_run(), async {}).await;
+ assert_eq!(result.outcome, EventRunOutcome::Interrupted);
+ assert_eq!(executor.runner.calls.load(Ordering::SeqCst), 0);
+}
diff --git a/services/scheduled_action/src/outbound/pg_event_run_repo.rs b/services/scheduled_action/src/outbound/pg_event_run_repo.rs
new file mode 100644
index 0000000000..b2627b0a5e
--- /dev/null
+++ b/services/scheduled_action/src/outbound/pg_event_run_repo.rs
@@ -0,0 +1,566 @@
+//! Durable event admission and fenced execution bookkeeping.
+//!
+//! Every mutation locks the action before its runs. This serializes admission,
+//! configuration changes and manual/event claims without locking other actions.
+
+use chrono::{DateTime, Utc};
+use macro_uuid::{Uuid, generate_uuid_v7};
+use model_owner::Owner;
+use rootcause::{Report, bail};
+use serde_json::{Value, json};
+use sqlx::{PgPool, Postgres, Transaction};
+
+use crate::domain::event_runs::{
+ AdmissionResult, AuthorizedEventRun, CancellationReason, CandidateActionPage, ClaimToken,
+ ClaimedEventRun, ConfigurationRevision, EventActionConfiguration, EventRunKey, EventRunOutcome,
+ EventRunRepository, FinalizationResult, FinalizeEventRun, PageSize, PendingEventRun,
+};
+use crate::domain::event_trigger::{ActionTrigger, EventReference};
+use crate::domain::models::{MAX_ACTION_TIME, ScheduledAction};
+
+#[cfg(test)]
+mod test;
+
+/// PostgreSQL implementation of the durable queue port. Authorization is the
+/// caller's responsibility; selectors and queue entries are not access grants.
+#[derive(Clone)]
+pub struct PgEventRunRepo {
+ pool: PgPool,
+}
+
+impl PgEventRunRepo {
+ /// Construct at the composition root with the shared MacroDB pool.
+ pub fn new(pool: PgPool) -> Self {
+ Self { pool }
+ }
+}
+
+struct ConfigurationRow {
+ action_id: Uuid,
+ owner: String,
+ enabled: bool,
+ configuration_revision: i64,
+ event_filters: Value,
+ event_activated_at: DateTime<Utc>,
+}
+
+/// Deliberately excludes persisted content and underlying parser errors.
+#[derive(Debug, thiserror::Error)]
+enum InvalidConfiguration {
+ #[error("invalid_owner")]
+ Owner,
+ #[error("invalid_revision")]
+ Revision,
+ #[error("invalid_filters")]
+ Filters,
+}
+
+impl TryFrom<ConfigurationRow> for EventActionConfiguration {
+ type Error = InvalidConfiguration;
+
+ fn try_from(row: ConfigurationRow) -> Result<Self, Self::Error> {
+ Ok(Self {
+ action_id: row.action_id,
+ owner: Owner::from_principal_str(&row.owner)
+ .map_err(|_| InvalidConfiguration::Owner)?,
+ enabled: row.enabled,
+ revision: row
+ .configuration_revision
+ .try_into()
+ .map_err(|_| InvalidConfiguration::Revision)?,
+ filters: serde_json::from_value(row.event_filters)
+ .map_err(|_| InvalidConfiguration::Filters)?,
+ activated_at: row.event_activated_at,
+ })
+ }
+}
+
+struct PendingRow {
+ action_id: Uuid,
+ event_id: Uuid,
+ configuration_revision: i64,
+ event_context: Value,
+ admitted_at: DateTime<Utc>,
+}
+
+impl TryFrom<PendingRow> for PendingEventRun {
+ type Error = Report;
+
+ fn try_from(mut row: PendingRow) -> Result<Self, Report> {
+ row.event_context["event_id"] = json!(row.event_id);
+ Ok(Self {
+ action_id: row.action_id,
+ revision: row.configuration_revision.try_into()?,
+ event: serde_json::from_value(row.event_context)?,
+ admitted_at: row.admitted_at,
+ })
+ }
+}
+
+/// All paths use this lock order, including cancellation and maintenance. Skip
+/// locked actions rather than holding a worker behind another routine's work.
+async fn lock_action(tx: &mut Transaction<'_, Postgres>, action_id: Uuid) -> Result<bool, Report> {
+ Ok(sqlx::query_scalar!(
+ "SELECT id FROM scheduled_action WHERE id = $1 FOR UPDATE SKIP LOCKED",
+ action_id,
+ )
+ .fetch_optional(&mut **tx)
+ .await?
+ .is_some())
+}
+
+impl EventRunRepository for PgEventRunRepo {
+ async fn candidate_actions(
+ &self,
+ event: &EventReference,
+ after: Option<Uuid>,
+ limit: PageSize,
+ ) -> Result<CandidateActionPage, Report> {
+ let rows = sqlx::query_as!(
+ ConfigurationRow,
+ r#"
+ SELECT id AS action_id, owner, enabled, configuration_revision,
+ event_filters AS "event_filters!", event_activated_at AS "event_activated_at!"
+ FROM scheduled_action
+ WHERE enabled AND trigger_type = 'events'
+ AND ($1::uuid IS NULL OR id > $1)
+ AND event_activated_at <= $2
+ AND event_filters @> jsonb_build_array(jsonb_build_object('events', jsonb_build_array($3::text)))
+ AND jsonb_path_exists(event_filters,
+ '$[*] ? (@.events[*] == $event && (!exists (@.ids) || @.ids == null || @.ids[*] == $id))',
+ jsonb_build_object('event', $3::text, 'id', $4::text))
+ ORDER BY id
+ LIMIT $5
+ "#,
+ after,
+ event.published_at(),
+ event.event_name().as_str(),
+ event.entity_id().to_string(),
+ i64::from(limit.get()),
+ )
+ .fetch_all(&self.pool)
+ .await?;
+ let next_after = rows.last().map(|row| row.action_id);
+ let mut configurations = Vec::with_capacity(rows.len());
+ for row in rows {
+ let action_id = row.action_id;
+ match row.try_into() {
+ Ok(configuration) => configurations.push(configuration),
+ Err(error) => tracing::warn!(
+ %action_id,
+ classification = %error,
+ "invalid event configuration skipped"
+ ),
+ }
+ }
+ Ok(CandidateActionPage {
+ configurations,
+ next_after,
+ })
+ }
+
+ async fn current_configuration(
+ &self,
+ action_id: Uuid,
+ ) -> Result<Option<EventActionConfiguration>, Report> {
+ Ok(sqlx::query_as!(
+ ConfigurationRow,
+ r#"
+ SELECT id AS action_id, owner, enabled, configuration_revision,
+ event_filters AS "event_filters!", event_activated_at AS "event_activated_at!"
+ FROM scheduled_action WHERE id = $1 AND trigger_type = 'events'
+ "#,
+ action_id,
+ )
+ .fetch_optional(&self.pool)
+ .await?
+ .map(TryInto::try_into)
+ .transpose()?)
+ }
+
+ async fn admit(
+ &self,
+ action_id: Uuid,
+ revision: ConfigurationRevision,
+ event: &EventReference,
+ ) -> Result<AdmissionResult, Report> {
+ let mut tx = self.pool.begin().await?;
+ // Unlike dispatch, intake must wait: skipping a locked row would permit
+ // Kafka acknowledgment without durable admission.
+ let eligible = sqlx::query_scalar!(
+ r#"
+ SELECT id FROM scheduled_action
+ WHERE id = $1 AND enabled AND trigger_type = 'events'
+ AND configuration_revision = $2 AND event_activated_at <= $3
+ AND jsonb_path_exists(event_filters,
+ '$[*] ? (@.events[*] == $event && (!exists (@.ids) || @.ids == null || @.ids[*] == $id))',
+ jsonb_build_object('event', $4::text, 'id', $5::text))
+ FOR UPDATE
+ "#,
+ action_id,
+ revision.get(),
+ event.published_at(),
+ event.event_name().as_str(),
+ event.entity_id().to_string(),
+ )
+ .fetch_optional(&mut *tx)
+ .await?;
+ if eligible.is_none() {
+ return Ok(AdmissionResult::Ineligible);
+ }
+ let context = json!({
+ "event_name": event.event_name(),
+ "entity_id": event.entity_id(),
+ "message_id": event.message_id(),
+ });
+ let inserted = sqlx::query!(
+ r#"
+ INSERT INTO scheduled_action_event_run (action_id, event_id, configuration_revision, event_context)
+ VALUES ($1, $2, $3, $4) ON CONFLICT (action_id, event_id) DO NOTHING
+ "#,
+ action_id,
+ event.event_id().as_uuid(),
+ revision.get(),
+ context,
+ )
+ .execute(&mut *tx)
+ .await?;
+ tx.commit().await?;
+ if inserted.rows_affected() == 0 {
+ Ok(AdmissionResult::AlreadyPresent)
+ } else {
+ Ok(AdmissionResult::Admitted)
+ }
+ }
+
+ async fn pending_runs(&self, limit: PageSize) -> Result<Vec<PendingEventRun>, Report> {
+ // Expired manual claims may be replaced, but an event claim must first
+ // be reconciled to interrupted, even after its action claim expires.
+ let rows = sqlx::query_as!(
+ PendingRow,
+ r#"
+ SELECT r.action_id, r.event_id, r.configuration_revision, r.event_context, r.admitted_at
+ FROM scheduled_action a
+ CROSS JOIN LATERAL (
+ SELECT action_id, event_id, configuration_revision, event_context, admitted_at, admission_order
+ FROM scheduled_action_event_run
+ WHERE action_id = a.id AND state = 'pending'
+ ORDER BY admission_order LIMIT 1
+ ) r
+ WHERE a.enabled AND a.trigger_type = 'events'
+ AND a.configuration_revision = r.configuration_revision
+ AND (a.claimed IS NULL OR a.claimed < $1)
+ AND NOT EXISTS (SELECT 1 FROM scheduled_action_event_run s WHERE s.action_id = a.id AND s.state = 'started')
+ ORDER BY r.admission_order
+ LIMIT $2 FOR UPDATE OF a SKIP LOCKED
+ "#,
+ Utc::now() - MAX_ACTION_TIME,
+ i64::from(limit.get()),
+ )
+ .fetch_all(&self.pool)
+ .await?;
+ rows.into_iter().map(TryInto::try_into).collect()
+ }
+
+ async fn claim(
+ &self,
+ run: AuthorizedEventRun,
+ token: ClaimToken,
+ started_at: DateTime<Utc>,
+ deadline: DateTime<Utc>,
+ ) -> Result<Option<ClaimedEventRun>, Report> {
+ if deadline <= started_at
+ || deadline <= Utc::now()
+ || deadline > started_at + MAX_ACTION_TIME
+ {
+ bail!(
+ "event execution deadline must be in the future and within the action claim lifetime"
+ );
+ }
+ let mut tx = self.pool.begin().await?;
+ let key = run.pending.key();
+ if !lock_action(&mut tx, key.action_id).await? {
+ return Ok(None);
+ }
+ let action = sqlx::query!(
+ r#"
+ SELECT id, owner, name, kind, task, created_at, updated_at, enabled,
+ event_filters AS "event_filters!", event_activated_at AS "event_activated_at!"
+ FROM scheduled_action
+ WHERE id = $1 AND enabled AND trigger_type = 'events' AND configuration_revision = $2
+ AND (claimed IS NULL OR claimed < $3)
+ AND NOT EXISTS (SELECT 1 FROM scheduled_action_event_run WHERE action_id = $1 AND state = 'started')
+ "#,
+ key.action_id,
+ run.pending.revision.get(),
+ Utc::now() - MAX_ACTION_TIME,
+ )
+ .fetch_optional(&mut *tx)
+ .await?;
+ let Some(action) = action else {
+ return Ok(None);
+ };
+ let pending = sqlx::query_as!(
+ PendingRow,
+ r#"
+ SELECT action_id, event_id, configuration_revision, event_context, admitted_at
+ FROM scheduled_action_event_run
+ WHERE action_id = $1 AND state = 'pending'
+ ORDER BY admission_order LIMIT 1 FOR UPDATE
+ "#,
+ key.action_id,
+ )
+ .fetch_optional(&mut *tx)
+ .await?;
+ let Some(pending) = pending else {
+ return Ok(None);
+ };
+ let pending = PendingEventRun::try_from(pending)?;
+ // Bind the authorized snapshot to the actual durable head, not merely
+ // to an action ID supplied by a caller.
+ if pending.key() != key
+ || pending.revision != run.pending.revision
+ || pending.event != run.pending.event
+ {
+ return Ok(None);
+ }
+ let action = ScheduledAction {
+ id: Some(action.id),
+ owner: Owner::from_principal_str(&action.owner)?,
+ name: action.name,
+ trigger: ActionTrigger::Events {
+ filters: serde_json::from_value(action.event_filters)?,
+ },
+ kind: serde_json::from_value(json!(action.kind))?,
+ task: action.task,
+ created_at: action.created_at,
+ updated_at: action.updated_at,
+ enabled: action.enabled,
+ configuration_revision: pending.revision,
+ event_activated_at: Some(action.event_activated_at),
+ next_run_at: None,
+ claimed: Some(started_at),
+ };
+ sqlx::query!(
+ "UPDATE scheduled_action SET claimed = $2, claim_token = $3, updated_at = now() WHERE id = $1",
+ key.action_id,
+ started_at,
+ token.as_uuid(),
+ )
+ .execute(&mut *tx)
+ .await?;
+ sqlx::query!(
+ r#"
+ UPDATE scheduled_action_event_run
+ SET state = 'started', claim_token = $3, started_at = $4, deadline = $5
+ WHERE action_id = $1 AND event_id = $2 AND state = 'pending'
+ "#,
+ key.action_id,
+ key.event_id.as_uuid(),
+ token.as_uuid(),
+ started_at,
+ deadline,
+ )
+ .execute(&mut *tx)
+ .await?;
+ tx.commit().await?;
+ Ok(Some(ClaimedEventRun {
+ run: AuthorizedEventRun {
+ pending,
+ access: run.access,
+ },
+ action,
+ token,
+ started_at,
+ deadline,
+ }))
+ }
+
+ async fn finalize(&self, run: FinalizeEventRun) -> Result<FinalizationResult, Report> {
+ let mut tx = self.pool.begin().await?;
+ // Bookkeeping retries wait for other bookkeeping, rather than reporting
+ // a spurious stale token just because its action is locked.
+ let action = sqlx::query!(
+ "SELECT claim_token FROM scheduled_action WHERE id = $1 FOR UPDATE",
+ run.key.action_id,
+ )
+ .fetch_optional(&mut *tx)
+ .await?;
+ let Some(action) = action else {
+ return Ok(FinalizationResult::StaleClaim);
+ };
+ let stored = sqlx::query!(
+ r#"
+ SELECT state, claim_token FROM scheduled_action_event_run
+ WHERE action_id = $1 AND event_id = $2 FOR UPDATE
+ "#,
+ run.key.action_id,
+ run.key.event_id.as_uuid(),
+ )
+ .fetch_optional(&mut *tx)
+ .await?;
+ let Some(stored) = stored else {
+ return Ok(FinalizationResult::StaleClaim);
+ };
+ if stored.claim_token != Some(run.token.as_uuid()) {
+ return Ok(FinalizationResult::StaleClaim);
+ }
+ if stored.state == "finished" {
+ return Ok(FinalizationResult::AlreadyFinalized);
+ }
+ // Delayed bookkeeping may still finish its own claim. Reconciliation
+ // uses the same lock: whichever terminal transition commits first wins.
+ if stored.state != "started" || action.claim_token != stored.claim_token {
+ return Ok(FinalizationResult::StaleClaim);
+ }
+ let mut execution_record_id = None;
+ if let Some(record) = run.execution.record {
+ if record.action_id != run.key.action_id {
+ bail!("execution record belongs to another action");
+ }
+ let id = generate_uuid_v7();
+ execution_record_id = Some(sqlx::query_scalar!(
+ r#"
+ INSERT INTO action_execution_record
+ (id, action_id, resource_id, start_time, end_time, is_success, result, created_at)
+ VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
+ ON CONFLICT (id) DO NOTHING RETURNING id
+ "#,
+ id,
+ run.key.action_id,
+ record.resource_id,
+ record.start_time,
+ record.end_time,
+ matches!(run.execution.outcome, EventRunOutcome::Succeeded),
+ record.result,
+ record.created_at,
+ )
+ .fetch_one(&mut *tx)
+ .await?);
+ }
+ sqlx::query!(
+ r#"
+ UPDATE scheduled_action_event_run
+ SET state = 'finished', finished_at = $3, outcome = $4, execution_record_id = $5
+ WHERE action_id = $1 AND event_id = $2
+ "#,
+ run.key.action_id,
+ run.key.event_id.as_uuid(),
+ run.finished_at,
+ serde_json::to_value(run.execution.outcome)?,
+ execution_record_id,
+ )
+ .execute(&mut *tx)
+ .await?;
+ release_claim(&mut tx, run.key.action_id, run.token.as_uuid()).await?;
+ tx.commit().await?;
+ Ok(FinalizationResult::Finalized)
+ }
+
+ async fn cancel_pending(
+ &self,
+ key: EventRunKey,
+ revision: ConfigurationRevision,
+ reason: CancellationReason,
+ ) -> Result<(), Report> {
+ let mut tx = self.pool.begin().await?;
+ sqlx::query_scalar!(
+ "SELECT id FROM scheduled_action WHERE id = $1 FOR UPDATE",
+ key.action_id,
+ )
+ .fetch_optional(&mut *tx)
+ .await?;
+ sqlx::query!(
+ r#"
+ UPDATE scheduled_action_event_run SET state = 'finished', finished_at = now(), outcome = $4
+ WHERE action_id = $1 AND event_id = $2 AND configuration_revision = $3 AND state = 'pending'
+ "#,
+ key.action_id,
+ key.event_id.as_uuid(),
+ revision.get(),
+ serde_json::to_value(EventRunOutcome::Cancelled { reason })?,
+ )
+ .execute(&mut *tx)
+ .await?;
+ tx.commit().await?;
+ Ok(())
+ }
+
+ async fn reconcile(&self, now: DateTime<Utc>, limit: PageSize) -> Result<u16, Report> {
+ let candidates = sqlx::query!(
+ r#"
+ SELECT r.action_id, r.event_id FROM scheduled_action_event_run r
+ JOIN scheduled_action a ON a.id = r.action_id
+ WHERE (r.state = 'started' AND r.deadline <= $1)
+ OR (r.state = 'pending' AND (NOT a.enabled OR a.trigger_type <> 'events'
+ OR a.configuration_revision <> r.configuration_revision))
+ ORDER BY r.admission_order LIMIT $2 FOR UPDATE OF a SKIP LOCKED
+ "#,
+ now,
+ i64::from(limit.get()),
+ )
+ .fetch_all(&self.pool)
+ .await?;
+ let mut affected = 0;
+ for candidate in candidates {
+ let mut tx = self.pool.begin().await?;
+ if !lock_action(&mut tx, candidate.action_id).await? {
+ continue;
+ }
+ // Recheck under the action lock. A cancellation cannot race past
+ // start, and reconciliation never overwrites a completed outcome.
+ let updated = sqlx::query!(
+ r#"
+ UPDATE scheduled_action_event_run r
+ SET state = 'finished', finished_at = $3,
+ outcome = CASE WHEN r.state = 'started' THEN $4::jsonb
+ WHEN NOT a.enabled THEN $5::jsonb ELSE $6::jsonb END
+ FROM scheduled_action a
+ WHERE r.action_id = $1 AND r.event_id = $2 AND a.id = r.action_id
+ AND ((r.state = 'started' AND r.deadline <= $3)
+ OR (r.state = 'pending' AND (NOT a.enabled OR a.trigger_type <> 'events'
+ OR a.configuration_revision <> r.configuration_revision)))
+ RETURNING r.claim_token
+ "#,
+ candidate.action_id,
+ candidate.event_id,
+ now,
+ serde_json::to_value(EventRunOutcome::Interrupted)?,
+ serde_json::to_value(EventRunOutcome::Cancelled {
+ reason: CancellationReason::Disabled
+ })?,
+ serde_json::to_value(EventRunOutcome::Cancelled {
+ reason: CancellationReason::Superseded
+ })?,
+ )
+ .fetch_optional(&mut *tx)
+ .await?;
+ if let Some(updated) = updated {
+ if let Some(token) = updated.claim_token {
+ release_claim(&mut tx, candidate.action_id, token).await?;
+ }
+ affected += 1;
+ }
+ tx.commit().await?;
+ }
+ Ok(affected)
+ }
+}
+
+async fn release_claim(
+ tx: &mut Transaction<'_, Postgres>,
+ action_id: Uuid,
+ token: Uuid,
+) -> Result<(), Report> {
+ sqlx::query!(
+ r#"
+ UPDATE scheduled_action SET claimed = NULL, claim_token = NULL, updated_at = now()
+ WHERE id = $1 AND claim_token = $2
+ "#,
+ action_id,
+ token,
+ )
+ .execute(&mut **tx)
+ .await?;
+ Ok(())
+}
diff --git a/services/scheduled_action/src/outbound/pg_event_run_repo/test.rs b/services/scheduled_action/src/outbound/pg_event_run_repo/test.rs
new file mode 100644
index 0000000000..b4a550559f
--- /dev/null
+++ b/services/scheduled_action/src/outbound/pg_event_run_repo/test.rs
@@ -0,0 +1,846 @@
+use entity_access::domain::models::{EntityAccessReceipt, ViewAccessLevel};
+use macro_db_migrator::MACRO_DB_MIGRATIONS;
+use macro_user_id::user_id::MacroUserIdStr;
+use model_entity::EntityType;
+
+use super::*;
+use crate::domain::event_runs::EventExecutionResult;
+use crate::domain::models::ActionExecutionRecord;
+
+const USER: &str = "macro|[email protected]";
+
+fn page(size: u16) -> PageSize {
+ size.try_into().unwrap()
+}
+
+fn new_event(entity_id: Uuid) -> EventReference {
+ serde_json::from_value(json!({
+ "event_id": generate_uuid_v7(),
+ "event_name": "document.updated",
+ "entity_id": entity_id,
+ "message_id": null,
+ }))
+ .unwrap()
+}
+
+async fn action(pool: &PgPool, filters: Value) -> Uuid {
+ let user_id = generate_uuid_v7();
+ sqlx::query!(
+ "INSERT INTO macro_user (id, username, email, stripe_customer_id) VALUES ($1, $2, $2, $2) ON CONFLICT DO NOTHING",
+ user_id,
+ USER,
+ )
+ .execute(pool)
+ .await
+ .unwrap();
+ sqlx::query!(
+ r#"INSERT INTO "User" (id, email, macro_user_id)
+ SELECT $1, $1, id FROM macro_user WHERE email = $1 ON CONFLICT DO NOTHING"#,
+ USER,
+ )
+ .execute(pool)
+ .await
+ .unwrap();
+ let id = generate_uuid_v7();
+ sqlx::query!(
+ r#"
+ INSERT INTO scheduled_action
+ (id, owner, name, kind, task, enabled, trigger_type, event_filters, event_activated_at)
+ VALUES ($1, $2, 'event run test', 'Agent', '{}', true, 'events', $3, $4)
+ ON CONFLICT DO NOTHING
+ "#,
+ id,
+ USER,
+ filters,
+ Utc::now() - chrono::Duration::days(1),
+ )
+ .execute(pool)
+ .await
+ .unwrap();
+ id
+}
+
+async fn setup(pool: &PgPool) -> (PgEventRunRepo, Uuid, EventReference) {
+ let id = action(pool, json!([{"events": ["document.updated"]}])).await;
+ (
+ PgEventRunRepo::new(pool.clone()),
+ id,
+ new_event(generate_uuid_v7()),
+ )
+}
+
+async fn admit(repo: &PgEventRunRepo, action_id: Uuid, event: &EventReference) {
+ assert_eq!(
+ repo.admit(action_id, ConfigurationRevision::INITIAL, event)
+ .await
+ .unwrap(),
+ AdmissionResult::Admitted,
+ );
+}
+
+fn authorized(pending: PendingEventRun) -> AuthorizedEventRun {
+ let access = EntityAccessReceipt::<ViewAccessLevel>::dangerously_assert_authenticated_user(
+ MacroUserIdStr::parse_from_str(USER).unwrap(),
+ &pending.event.entity_id().to_string(),
+ EntityType::Document,
+ );
+ AuthorizedEventRun {
+ pending,
+ access: crate::domain::event_runs::EventAccessCapability::Document(access),
+ }
+}
+
+async fn claim(repo: &PgEventRunRepo, pending: PendingEventRun) -> Option<ClaimedEventRun> {
+ let now = Utc::now();
+ repo.claim(
+ authorized(pending),
+ ClaimToken::generate(),
+ now,
+ now + MAX_ACTION_TIME,
+ )
+ .await
+ .unwrap()
+}
+
+fn completion(run: &ClaimedEventRun, outcome: EventRunOutcome) -> FinalizeEventRun {
+ FinalizeEventRun {
+ key: run.run.pending.key(),
+ token: run.token,
+ finished_at: Utc::now(),
+ execution: EventExecutionResult {
+ outcome,
+ record: Some(ActionExecutionRecord {
+ id: None,
+ action_id: run.run.pending.action_id,
+ resource_id: Some("run-chat".into()),
+ start_time: run.started_at,
+ end_time: Utc::now(),
+ is_success: matches!(outcome, EventRunOutcome::Succeeded),
+ result: json!({}),
+ created_at: Utc::now(),
+ }),
+ },
+ }
+}
+
+async fn state(pool: &PgPool, key: EventRunKey) -> (String, Option<Value>, Option<Uuid>) {
+ let row = sqlx::query!(
+ "SELECT state, outcome, execution_record_id FROM scheduled_action_event_run WHERE action_id = $1 AND event_id = $2",
+ key.action_id,
+ key.event_id.as_uuid(),
+ )
+ .fetch_one(pool)
+ .await
+ .unwrap();
+ (row.state, row.outcome, row.execution_record_id)
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn concurrent_duplicate_admission_and_cascade_cleanup(pool: PgPool) {
+ let (repo, id, event) = setup(&pool).await;
+ let (a, b) = tokio::join!(
+ repo.admit(id, ConfigurationRevision::INITIAL, &event),
+ repo.admit(id, ConfigurationRevision::INITIAL, &event),
+ );
+ let results = [a.unwrap(), b.unwrap()];
+ assert!(results.contains(&AdmissionResult::Admitted));
+ assert!(results.contains(&AdmissionResult::AlreadyPresent));
+ let pending = repo.pending_runs(page(100)).await.unwrap();
+ assert_eq!(pending.len(), 1);
+ let started = claim(&repo, pending[0].clone()).await.unwrap();
+ repo.finalize(completion(&started, EventRunOutcome::Succeeded))
+ .await
+ .unwrap();
+ assert_eq!(
+ repo.admit(id, ConfigurationRevision::INITIAL, &event)
+ .await
+ .unwrap(),
+ AdmissionResult::AlreadyPresent
+ );
+ sqlx::query!("DELETE FROM scheduled_action WHERE id = $1", id)
+ .execute(&pool)
+ .await
+ .unwrap();
+ assert_eq!(
+ sqlx::query_scalar!("SELECT count(*) FROM scheduled_action_event_run")
+ .fetch_one(&pool)
+ .await
+ .unwrap(),
+ Some(0)
+ );
+ assert_eq!(
+ sqlx::query_scalar!("SELECT count(*) FROM action_execution_record")
+ .fetch_one(&pool)
+ .await
+ .unwrap(),
+ Some(0)
+ );
+ assert_eq!(
+ repo.admit(id, ConfigurationRevision::INITIAL, &event)
+ .await
+ .unwrap(),
+ AdmissionResult::Ineligible
+ );
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn candidates_exact_recheck_activation_and_keyset_pages(pool: PgPool) {
+ let entity_id = generate_uuid_v7();
+ let event = new_event(entity_id);
+ // Containment matches the name, but an ID from a different filter cannot
+ // satisfy that filter. Empty IDs must not turn into unrestricted selectors.
+ action(
+ &pool,
+ json!([
+ {"events": ["document.updated"], "ids": [generate_uuid_v7()]},
+ {"events": ["document.created"], "ids": [entity_id]},
+ ]),
+ )
+ .await;
+ action(&pool, json!([{"events": ["document.updated"], "ids": []}])).await;
+ let null_ids = action(
+ &pool,
+ json!([{"events": ["document.updated"], "ids": null}]),
+ )
+ .await;
+ let exact = action(
+ &pool,
+ json!([
+ {"events": ["document.updated"], "ids": [entity_id]},
+ {"events": ["document.updated"]},
+ ]),
+ )
+ .await;
+ let repo = PgEventRunRepo::new(pool.clone());
+ let first = repo.candidate_actions(&event, None, page(1)).await.unwrap();
+ let second = repo
+ .candidate_actions(&event, first.next_after, page(1))
+ .await
+ .unwrap();
+ assert_eq!(
+ [
+ first.configurations[0].action_id,
+ second.configurations[0].action_id
+ ],
+ [null_ids, exact]
+ );
+ assert!(
+ repo.candidate_actions(&event, Some(exact), page(1))
+ .await
+ .unwrap()
+ .next_after
+ .is_none()
+ );
+ admit(&repo, exact, &event).await;
+ assert_eq!(repo.pending_runs(page(100)).await.unwrap().len(), 1);
+ sqlx::query!(
+ "UPDATE scheduled_action SET event_activated_at = $2 WHERE id = $1",
+ null_ids,
+ Utc::now() + chrono::Duration::days(1)
+ )
+ .execute(&pool)
+ .await
+ .unwrap();
+ assert_eq!(
+ repo.admit(null_ids, ConfigurationRevision::INITIAL, &event)
+ .await
+ .unwrap(),
+ AdmissionResult::Ineligible
+ );
+ assert_eq!(
+ repo.candidate_actions(&event, None, page(100))
+ .await
+ .unwrap()
+ .configurations
+ .len(),
+ 1
+ );
+}
+
+struct AllowAccess;
+
+impl crate::domain::event_runs::CurrentOwnerAccess for AllowAccess {
+ async fn authorize(
+ &self,
+ owner: &MacroUserIdStr<'static>,
+ event: &EventReference,
+ ) -> Result<Option<crate::domain::event_runs::EventAccessCapability>, Report> {
+ Ok(Some(
+ crate::domain::event_runs::EventAccessCapability::Document(
+ EntityAccessReceipt::<ViewAccessLevel>::dangerously_assert_authenticated_user(
+ owner.clone(),
+ &event.entity_id().to_string(),
+ EntityType::Document,
+ ),
+ ),
+ ))
+ }
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn admission_skips_invalid_first_and_middle_pages_without_truncating_fanout(pool: PgPool) {
+ use crate::domain::event_runs::{
+ EventIngestion, EventIngestionResult, admission::EventAdmissionService,
+ };
+ use crate::domain::event_trigger::{EventPayload, IncomingEvent};
+ use std::sync::Arc;
+
+ let valid = json!([{"events": ["document.updated"]}]);
+ let malformed_owner = action(&pool, valid.clone()).await;
+ sqlx::query!(
+ r#"INSERT INTO "User" (id, email, macro_user_id)
+ SELECT $1, $1, id FROM macro_user WHERE email = $2"#,
+ "invalid private owner",
+ USER,
+ )
+ .execute(&pool)
+ .await
+ .unwrap();
+ sqlx::query!(
+ "UPDATE scheduled_action SET owner = $2 WHERE id = $1",
+ malformed_owner,
+ "invalid private owner"
+ )
+ .execute(&pool)
+ .await
+ .unwrap();
+ let too_many_events = action(&pool, json!([{"events": vec!["document.updated"; 8]}])).await;
+ let first_valid = action(&pool, valid.clone()).await;
+ let second_valid = action(&pool, valid.clone()).await;
+ // Another entirely invalid page after valid candidates have been admitted.
+ action(&pool, json!([{"events": ["document.updated"], "ids": vec![generate_uuid_v7().to_string(); 101]}, {"events": ["document.updated"]}])).await;
+ action(
+ &pool,
+ json!([{"events": ["document.updated", "unknown.event"]}]),
+ )
+ .await;
+ let last_valid = action(&pool, valid).await;
+ let incoming = IncomingEvent {
+ event_id: generate_uuid_v7(),
+ schema_version: 1,
+ payload: EventPayload::Document(
+ serde_json::from_value(json!({
+ "event_type": "document.updated", "metadata": {
+ "document_id": generate_uuid_v7(), "owner": USER,
+ "actor_user_id": USER, "share_permission_updated": false,
+ }
+ }))
+ .unwrap(),
+ ),
+ };
+ let repo = Arc::new(PgEventRunRepo::new(pool.clone()));
+ let first = repo
+ .candidate_actions(&incoming.normalize().unwrap(), None, page(2))
+ .await
+ .unwrap();
+ assert!(first.configurations.is_empty());
+ assert_eq!(first.next_after, Some(too_many_events));
+ let service = EventAdmissionService::new(repo.clone(), Arc::new(AllowAccess), page(2));
+ assert_eq!(
+ service.ingest(&incoming).await.unwrap(),
+ EventIngestionResult::Admitted { inserted: 3 }
+ );
+ let mut admitted: Vec<_> = repo
+ .pending_runs(page(100))
+ .await
+ .unwrap()
+ .into_iter()
+ .map(|run| run.action_id)
+ .collect();
+ admitted.sort();
+ assert_eq!(admitted, vec![first_valid, second_valid, last_valid]);
+ assert_eq!(
+ service.ingest(&incoming).await.unwrap(),
+ EventIngestionResult::Admitted { inserted: 0 }
+ );
+ // A repository outage is not a malformed configuration and must defer intake.
+ pool.close().await;
+ assert!(service.ingest(&incoming).await.is_err());
+}
+
+#[test]
+fn invalid_configuration_classifications_do_not_include_persisted_content() {
+ fn row() -> ConfigurationRow {
+ ConfigurationRow {
+ action_id: generate_uuid_v7(),
+ owner: USER.into(),
+ enabled: true,
+ configuration_revision: 1,
+ event_filters: json!([{"events": ["document.updated"]}]),
+ event_activated_at: Utc::now(),
+ }
+ }
+ let mut owner = row();
+ owner.owner = "private malformed owner".into();
+ assert_eq!(
+ EventActionConfiguration::try_from(owner)
+ .unwrap_err()
+ .to_string(),
+ "invalid_owner"
+ );
+ let mut revision = row();
+ revision.configuration_revision = 0;
+ assert_eq!(
+ EventActionConfiguration::try_from(revision)
+ .unwrap_err()
+ .to_string(),
+ "invalid_revision"
+ );
+ let mut filters = row();
+ filters.event_filters = json!([{"events": ["private malformed event"]}]);
+ assert_eq!(
+ EventActionConfiguration::try_from(filters)
+ .unwrap_err()
+ .to_string(),
+ "invalid_filters"
+ );
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn two_workers_fifo_and_independent_actions(pool: PgPool) {
+ let (repo, id, earlier_event) = setup(&pool).await;
+ let later_event = new_event(earlier_event.entity_id());
+ // Admission order, deliberately opposite publication order.
+ admit(&repo, id, &later_event).await;
+ admit(&repo, id, &earlier_event).await;
+ let other_id = action(&pool, json!([{"events": ["document.updated"]}])).await;
+ admit(&repo, other_id, &earlier_event).await;
+ let pending = repo.pending_runs(page(100)).await.unwrap();
+ assert_eq!(pending.len(), 2);
+ assert_eq!(pending[0].event, later_event);
+ let mut not_head = pending[0].clone();
+ not_head.event = earlier_event.clone();
+ assert!(claim(&repo, not_head).await.is_none());
+ let (a, b) = tokio::join!(
+ claim(&repo, pending[0].clone()),
+ claim(&repo, pending[0].clone())
+ );
+ assert_eq!(usize::from(a.is_some()) + usize::from(b.is_some()), 1);
+ let started = a.or(b).unwrap();
+ let remaining = repo.pending_runs(page(1)).await.unwrap();
+ assert_eq!(remaining[0].action_id, other_id);
+ assert!(claim(&repo, remaining[0].clone()).await.is_some());
+ repo.finalize(completion(&started, EventRunOutcome::Failed))
+ .await
+ .unwrap();
+ let next = repo.pending_runs(page(1)).await.unwrap();
+ assert_eq!(next[0].event, earlier_event);
+ assert!(claim(&repo, next[0].clone()).await.is_some());
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn manual_claim_blocks_events_and_foreign_tokens_cannot_finalize(pool: PgPool) {
+ let (repo, id, event) = setup(&pool).await;
+ admit(&repo, id, &event).await;
+ let pending = repo.pending_runs(page(1)).await.unwrap().remove(0);
+ // Deployed writers only set claimed; fenced manual writers set both.
+ sqlx::query!(
+ "UPDATE scheduled_action SET claimed = now() WHERE id = $1",
+ id
+ )
+ .execute(&pool)
+ .await
+ .unwrap();
+ assert!(repo.pending_runs(page(1)).await.unwrap().is_empty());
+ assert!(claim(&repo, pending.clone()).await.is_none());
+ sqlx::query!(
+ "UPDATE scheduled_action SET claimed = NULL WHERE id = $1",
+ id
+ )
+ .execute(&pool)
+ .await
+ .unwrap();
+ let started = claim(&repo, pending).await.unwrap();
+ let mut foreign = completion(&started, EventRunOutcome::Succeeded);
+ foreign.token = ClaimToken::generate();
+ assert_eq!(
+ repo.finalize(foreign).await.unwrap(),
+ FinalizationResult::StaleClaim
+ );
+ assert_eq!(state(&pool, started.run.pending.key()).await.0, "started");
+ assert_eq!(
+ repo.finalize(completion(&started, EventRunOutcome::Succeeded))
+ .await
+ .unwrap(),
+ FinalizationResult::Finalized
+ );
+ let finished = state(&pool, started.run.pending.key()).await;
+ assert!(finished.2.is_some());
+ assert_eq!(
+ repo.finalize(completion(&started, EventRunOutcome::Failed))
+ .await
+ .unwrap(),
+ FinalizationResult::AlreadyFinalized
+ );
+ assert_eq!(state(&pool, started.run.pending.key()).await, finished);
+ assert_eq!(
+ sqlx::query_scalar!("SELECT count(*) FROM action_execution_record")
+ .fetch_one(&pool)
+ .await
+ .unwrap(),
+ Some(1)
+ );
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn expired_started_is_interrupted_and_never_requeued(pool: PgPool) {
+ let (repo, id, first) = setup(&pool).await;
+ let second = new_event(first.entity_id());
+ admit(&repo, id, &first).await;
+ admit(&repo, id, &second).await;
+ let pending = repo.pending_runs(page(1)).await.unwrap().remove(0);
+ let started = claim(&repo, pending).await.unwrap();
+ assert_eq!(repo.reconcile(started.deadline, page(1)).await.unwrap(), 1);
+ assert_eq!(repo.reconcile(started.deadline, page(1)).await.unwrap(), 0);
+ assert_eq!(
+ state(&pool, started.run.pending.key()).await.1,
+ Some(json!({"type": "interrupted"}))
+ );
+ let pending = repo.pending_runs(page(1)).await.unwrap();
+ assert_eq!(pending[0].event, second);
+ let newer = claim(&repo, pending[0].clone()).await.unwrap();
+ assert_eq!(
+ repo.finalize(completion(&started, EventRunOutcome::Succeeded))
+ .await
+ .unwrap(),
+ FinalizationResult::AlreadyFinalized
+ );
+ let token = sqlx::query_scalar!("SELECT claim_token FROM scheduled_action WHERE id = $1", id)
+ .fetch_one(&pool)
+ .await
+ .unwrap();
+ assert_eq!(token, Some(newer.token.as_uuid()));
+ assert_eq!(
+ repo.admit(id, ConfigurationRevision::INITIAL, &first)
+ .await
+ .unwrap(),
+ AdmissionResult::AlreadyPresent
+ );
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn reconcile_does_not_release_a_replaced_token(pool: PgPool) {
+ let (repo, id, event) = setup(&pool).await;
+ admit(&repo, id, &event).await;
+ let pending = repo.pending_runs(page(1)).await.unwrap().remove(0);
+ let started = claim(&repo, pending).await.unwrap();
+ let replacement = ClaimToken::generate();
+ sqlx::query!(
+ "UPDATE scheduled_action SET claim_token = $2 WHERE id = $1",
+ id,
+ replacement.as_uuid()
+ )
+ .execute(&pool)
+ .await
+ .unwrap();
+ assert_eq!(
+ repo.finalize(completion(&started, EventRunOutcome::Succeeded))
+ .await
+ .unwrap(),
+ FinalizationResult::StaleClaim
+ );
+ assert_eq!(repo.reconcile(started.deadline, page(1)).await.unwrap(), 1);
+ assert_eq!(
+ sqlx::query_scalar!("SELECT claim_token FROM scheduled_action WHERE id = $1", id)
+ .fetch_one(&pool)
+ .await
+ .unwrap(),
+ Some(replacement.as_uuid())
+ );
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn cancellation_claim_race_has_one_terminal_direction(pool: PgPool) {
+ let (repo, id, event) = setup(&pool).await;
+ admit(&repo, id, &event).await;
+ let pending = repo.pending_runs(page(1)).await.unwrap().remove(0);
+ let key = pending.key();
+ let (started, cancelled) = tokio::join!(
+ claim(&repo, pending),
+ repo.cancel_pending(
+ key,
+ ConfigurationRevision::INITIAL,
+ CancellationReason::AccessDenied
+ ),
+ );
+ cancelled.unwrap();
+ let stored = state(&pool, key).await;
+ if started.is_some() {
+ assert_eq!(stored.0, "started");
+ assert!(stored.1.is_none());
+ } else {
+ assert_eq!(stored.0, "finished");
+ assert_eq!(
+ stored.1,
+ Some(json!({"type": "cancelled", "reason": "access_denied"}))
+ );
+ }
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn revision_disable_and_cancellation_rechecks(pool: PgPool) {
+ let (repo, id, event) = setup(&pool).await;
+ admit(&repo, id, &event).await;
+ let pending = repo.pending_runs(page(1)).await.unwrap().remove(0);
+ repo.cancel_pending(
+ pending.key(),
+ ConfigurationRevision::INITIAL.next().unwrap(),
+ CancellationReason::Disabled,
+ )
+ .await
+ .unwrap();
+ assert_eq!(state(&pool, pending.key()).await.0, "pending");
+ sqlx::query!("UPDATE scheduled_action SET configuration_revision = configuration_revision + 1 WHERE id = $1", id).execute(&pool).await.unwrap();
+ assert!(claim(&repo, pending.clone()).await.is_none());
+ assert_eq!(
+ repo.admit(id, ConfigurationRevision::INITIAL, &event)
+ .await
+ .unwrap(),
+ AdmissionResult::Ineligible
+ );
+ assert_eq!(repo.reconcile(Utc::now(), page(1)).await.unwrap(), 1);
+ assert_eq!(
+ state(&pool, pending.key()).await.1,
+ Some(json!({"type": "cancelled", "reason": "superseded"}))
+ );
+ let next_event = new_event(event.entity_id());
+ assert_eq!(
+ repo.admit(
+ id,
+ ConfigurationRevision::INITIAL.next().unwrap(),
+ &next_event
+ )
+ .await
+ .unwrap(),
+ AdmissionResult::Admitted
+ );
+ let next = repo.pending_runs(page(1)).await.unwrap().remove(0);
+ sqlx::query!(
+ "UPDATE scheduled_action SET enabled = false WHERE id = $1",
+ id
+ )
+ .execute(&pool)
+ .await
+ .unwrap();
+ assert!(claim(&repo, next.clone()).await.is_none());
+ assert_eq!(repo.reconcile(Utc::now(), page(1)).await.unwrap(), 1);
+ assert_eq!(
+ state(&pool, next.key()).await.1,
+ Some(json!({"type": "cancelled", "reason": "disabled"}))
+ );
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn locked_actions_do_not_block_independent_dispatch(pool: PgPool) {
+ let (repo, id, event) = setup(&pool).await;
+ admit(&repo, id, &event).await;
+ let other = action(&pool, json!([{"events": ["document.updated"]}])).await;
+ admit(&repo, other, &event).await;
+ let pending = repo.pending_runs(page(100)).await.unwrap();
+ let mut tx = pool.begin().await.unwrap();
+ assert!(lock_action(&mut tx, id).await.unwrap());
+ let available = repo.pending_runs(page(1)).await.unwrap();
+ assert_eq!(available[0].action_id, other);
+ let result = tokio::time::timeout(
+ std::time::Duration::from_secs(2),
+ claim(&repo, pending[0].clone()),
+ )
+ .await
+ .unwrap();
+ assert!(result.is_none());
+ assert!(claim(&repo, pending[1].clone()).await.is_some());
+ tx.rollback().await.unwrap();
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn failed_bookkeeping_rolls_back_history_and_claim(pool: PgPool) {
+ let (repo, id, event) = setup(&pool).await;
+ admit(&repo, id, &event).await;
+ let pending = repo.pending_runs(page(1)).await.unwrap().remove(0);
+ let started = claim(&repo, pending).await.unwrap();
+ let mut invalid = completion(&started, EventRunOutcome::Succeeded);
+ invalid.execution.record.as_mut().unwrap().action_id = generate_uuid_v7();
+ assert!(repo.finalize(invalid).await.is_err());
+ assert_eq!(state(&pool, started.run.pending.key()).await.0, "started");
+ assert_eq!(
+ sqlx::query_scalar!("SELECT count(*) FROM action_execution_record")
+ .fetch_one(&pool)
+ .await
+ .unwrap(),
+ Some(0)
+ );
+ let (a, b) = tokio::join!(
+ repo.finalize(completion(&started, EventRunOutcome::Succeeded)),
+ repo.finalize(completion(&started, EventRunOutcome::Succeeded)),
+ );
+ let results = [a.unwrap(), b.unwrap()];
+ assert!(results.contains(&FinalizationResult::Finalized));
+ assert!(results.contains(&FinalizationResult::AlreadyFinalized));
+ assert_eq!(
+ sqlx::query_scalar!("SELECT count(*) FROM action_execution_record")
+ .fetch_one(&pool)
+ .await
+ .unwrap(),
+ Some(1)
+ );
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn expired_manual_claim_can_be_replaced_without_stale_release(pool: PgPool) {
+ let (repo, id, event) = setup(&pool).await;
+ admit(&repo, id, &event).await;
+ let manual_token = ClaimToken::generate();
+ sqlx::query!(
+ "UPDATE scheduled_action SET claimed = $2, claim_token = $3 WHERE id = $1",
+ id,
+ Utc::now() - MAX_ACTION_TIME - chrono::Duration::seconds(1),
+ manual_token.as_uuid(),
+ )
+ .execute(&pool)
+ .await
+ .unwrap();
+ let pending = repo.pending_runs(page(1)).await.unwrap().remove(0);
+ let started = claim(&repo, pending).await.unwrap();
+ let mut tx = pool.begin().await.unwrap();
+ release_claim(&mut tx, id, manual_token.as_uuid())
+ .await
+ .unwrap();
+ tx.commit().await.unwrap();
+ assert_eq!(
+ sqlx::query_scalar!("SELECT claim_token FROM scheduled_action WHERE id = $1", id)
+ .fetch_one(&pool)
+ .await
+ .unwrap(),
+ Some(started.token.as_uuid())
+ );
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn admission_waits_for_configuration_lock_and_rechecks_disable(pool: PgPool) {
+ let (repo, id, event) = setup(&pool).await;
+ let mut tx = pool.begin().await.unwrap();
+ sqlx::query!(
+ "UPDATE scheduled_action SET enabled = false WHERE id = $1",
+ id
+ )
+ .execute(&mut *tx)
+ .await
+ .unwrap();
+ let admitting_repo = repo.clone();
+ let mut admitting = tokio::spawn(async move {
+ admitting_repo
+ .admit(id, ConfigurationRevision::INITIAL, &event)
+ .await
+ });
+ assert!(
+ tokio::time::timeout(std::time::Duration::from_millis(100), &mut admitting)
+ .await
+ .is_err()
+ );
+ tx.commit().await.unwrap();
+ assert_eq!(
+ admitting.await.unwrap().unwrap(),
+ AdmissionResult::Ineligible
+ );
+ assert!(repo.pending_runs(page(1)).await.unwrap().is_empty());
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn reconcile_is_bounded_and_stale_configuration_does_not_cancel_started(pool: PgPool) {
+ let (repo, id, first) = setup(&pool).await;
+ let second = new_event(first.entity_id());
+ let third = new_event(first.entity_id());
+ admit(&repo, id, &first).await;
+ admit(&repo, id, &second).await;
+ admit(&repo, id, &third).await;
+ let pending = repo.pending_runs(page(1)).await.unwrap().remove(0);
+ let started = claim(&repo, pending).await.unwrap();
+ sqlx::query!(
+ "UPDATE scheduled_action SET enabled = false WHERE id = $1",
+ id
+ )
+ .execute(&pool)
+ .await
+ .unwrap();
+ assert_eq!(repo.reconcile(Utc::now(), page(1)).await.unwrap(), 1);
+ assert_eq!(repo.reconcile(Utc::now(), page(1)).await.unwrap(), 1);
+ assert_eq!(repo.reconcile(Utc::now(), page(1)).await.unwrap(), 0);
+ assert_eq!(state(&pool, started.run.pending.key()).await.0, "started");
+ assert_eq!(
+ repo.finalize(completion(&started, EventRunOutcome::Succeeded))
+ .await
+ .unwrap(),
+ FinalizationResult::Finalized
+ );
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn invalid_deadlines_leave_the_run_pending(pool: PgPool) {
+ let (repo, id, event) = setup(&pool).await;
+ admit(&repo, id, &event).await;
+ let pending = repo.pending_runs(page(1)).await.unwrap().remove(0);
+ let now = Utc::now();
+ for (start, deadline) in [
+ (now, now),
+ (now, now + MAX_ACTION_TIME + chrono::Duration::seconds(1)),
+ (now - MAX_ACTION_TIME, now - chrono::Duration::seconds(1)),
+ ] {
+ assert!(
+ repo.claim(
+ authorized(pending.clone()),
+ ClaimToken::generate(),
+ start,
+ deadline
+ )
+ .await
+ .is_err()
+ );
+ }
+ assert_eq!(state(&pool, pending.key()).await.0, "pending");
+ let configuration = repo.current_configuration(id).await.unwrap().unwrap();
+ assert_eq!(configuration.action_id, id);
+ assert_eq!(configuration.revision, pending.revision);
+ assert!(
+ configuration
+ .filters
+ .matches(&event, configuration.activated_at)
+ );
+ assert!(
+ repo.current_configuration(generate_uuid_v7())
+ .await
+ .unwrap()
+ .is_none()
+ );
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn delayed_bookkeeping_can_finish_before_reconciliation(pool: PgPool) {
+ let (repo, id, event) = setup(&pool).await;
+ admit(&repo, id, &event).await;
+ let pending = repo.pending_runs(page(1)).await.unwrap().remove(0);
+ let started = claim(&repo, pending).await.unwrap();
+ // Simulate persistence becoming available after the execution deadline.
+ sqlx::query!(
+ r#"
+ UPDATE scheduled_action_event_run SET started_at = $3, deadline = $4
+ WHERE action_id = $1 AND event_id = $2
+ "#,
+ id,
+ event.event_id().as_uuid(),
+ Utc::now() - MAX_ACTION_TIME,
+ Utc::now() - chrono::Duration::seconds(1),
+ )
+ .execute(&pool)
+ .await
+ .unwrap();
+ assert_eq!(
+ repo.finalize(completion(&started, EventRunOutcome::Failed))
+ .await
+ .unwrap(),
+ FinalizationResult::Finalized
+ );
+ assert_eq!(repo.reconcile(Utc::now(), page(1)).await.unwrap(), 0);
+ let finished = state(&pool, started.run.pending.key()).await;
+ assert_eq!(finished.1, Some(json!({"type": "failed"})));
+ assert!(finished.2.is_some());
+}
diff --git a/services/scheduled_action/src/outbound/pg_polling_dispatcher.rs b/services/scheduled_action/src/outbound/pg_polling_dispatcher.rs
index b097803255..d567c56fe5 100644
--- a/services/scheduled_action/src/outbound/pg_polling_dispatcher.rs
+++ b/services/scheduled_action/src/outbound/pg_polling_dispatcher.rs
@@ -9,6 +9,7 @@ use tokio_util::task::TaskTracker;
#[cfg(test)]
mod test;
+use crate::domain::event_trigger::ActionTrigger;
use crate::domain::models::{DispatchEvent, InProgressExecution};
use crate::domain::ports::{
ScheduledActionDispatcher, ScheduledActionExecutor, ScheduledActionRepo,
@@ -144,10 +145,16 @@ where
Ok(candidates) => {
let now = Utc::now();
for action in candidates {
+ if !action.enabled || !matches!(action.trigger, ActionTrigger::Cron { .. }) {
+ continue;
+ }
+ let Some(next_run_at) = action.next_run_at else {
+ continue;
+ };
// Candidates come back sorted by next_run_at ASC.
// The first non-due one ends the batch — anything
// after it is also not yet due.
- if action.next_run_at > now {
+ if next_run_at > now {
break;
}
diff --git a/services/scheduled_action/src/outbound/pg_polling_dispatcher/test.rs b/services/scheduled_action/src/outbound/pg_polling_dispatcher/test.rs
index 5a4f3c90fc..f3b58cebde 100644
--- a/services/scheduled_action/src/outbound/pg_polling_dispatcher/test.rs
+++ b/services/scheduled_action/src/outbound/pg_polling_dispatcher/test.rs
@@ -13,6 +13,8 @@ use tokio_util::sync::CancellationToken;
use tokio_util::task::TaskTracker;
use super::{PgPollingDispatcher, PgPollingDispatcherLifecycle};
+use crate::domain::event_runs::ConfigurationRevision;
+use crate::domain::event_trigger::ActionTrigger;
use crate::domain::models::{
ActionExecutionRecord, ActionKind, InProgressExecution, Schedule, ScheduledAction,
};
@@ -64,6 +66,14 @@ impl ScheduledActionRepo for FakeRepository {
Ok(Vec::new())
}
+ async fn get_action(
+ &self,
+ _id: &Uuid,
+ _user_id: MacroUserIdStr<'static>,
+ ) -> Result<Option<ScheduledAction>> {
+ Ok(None)
+ }
+
async fn get_next_unclaimed_actions(&self, _limit: i64) -> Result<Vec<ScheduledAction>> {
self.poll_started.add_permits(1);
if let Some(cancellation_token) = &self.cancellation_token {
@@ -84,11 +94,15 @@ impl ScheduledActionRepo for FakeRepository {
Ok(())
}
- async fn claim_action(&self, _id: &Uuid) -> Result<()> {
- Ok(())
+ async fn claim_action(&self, _id: &Uuid) -> Result<crate::domain::event_runs::ClaimToken> {
+ Ok(crate::domain::event_runs::ClaimToken::generate())
}
- async fn release_action(&self, _id: &Uuid) -> Result<()> {
+ async fn release_action(
+ &self,
+ _id: &Uuid,
+ _token: crate::domain::event_runs::ClaimToken,
+ ) -> Result<()> {
Ok(())
}
@@ -157,15 +171,19 @@ fn due_action() -> ScheduledAction {
owner: Owner::from_principal_str("macro|[email protected]")
.expect("test owner should be valid"),
name: "test action".to_string(),
- schedule: Schedule::from_cron("0 * * * * *".to_string())
- .expect("test schedule should be valid"),
+ trigger: ActionTrigger::Cron {
+ schedule: Schedule::from_cron("0 * * * * *".to_string())
+ .expect("test schedule should be valid"),
+ timezone: chrono_tz::UTC,
+ },
kind: ActionKind::Agent,
created_at: now,
updated_at: now,
- timezone: chrono_tz::UTC,
+ configuration_revision: ConfigurationRevision::INITIAL,
+ event_activated_at: None,
task: json!({}),
claimed: None,
- next_run_at: now - ChronoDuration::seconds(1),
+ next_run_at: Some(now - ChronoDuration::seconds(1)),
enabled: true,
}
}
@@ -259,3 +277,45 @@ async fn cancellation_allows_started_execution_to_finish_without_starting_anothe
assert!(first_execution_finished.load(Ordering::SeqCst));
assert_eq!(execution_count.load(Ordering::SeqCst), 1);
}
+
+#[tokio::test]
+async fn only_enabled_due_cron_candidates_reach_executor() {
+ let mut event = due_action();
+ event.trigger = serde_json::from_value(json!({
+ "type": "events", "filters": [{"events": ["document.created"]}]
+ }))
+ .unwrap();
+ event.next_run_at = None;
+ event.event_activated_at = Some(Utc::now());
+ let mut disabled = due_action();
+ disabled.enabled = false;
+ let mut missing_next_run = due_action();
+ missing_next_run.next_run_at = None;
+ let due = due_action();
+ let due_id = due.id.unwrap();
+ let mut future = due_action();
+ future.next_run_at = Some(Utc::now() + ChronoDuration::hours(1));
+ let repository = Arc::new(FakeRepository::returning(vec![
+ event,
+ disabled,
+ missing_next_run,
+ due,
+ future,
+ ]));
+ let execution_count = Arc::new(AtomicUsize::new(0));
+ let executor = RecordingExecutor {
+ execution_count: Arc::clone(&execution_count),
+ };
+ let (cancellation_token, tracker, lifecycle) = lifecycle();
+ let dispatcher = PgPollingDispatcher::new(repository, executor).with_lifecycle(lifecycle);
+ let (_dispatch_tx, mut execution_rx) = dispatcher.begin_dispatch_loop();
+ let execution = tokio::time::timeout(Duration::from_secs(1), execution_rx.recv())
+ .await
+ .unwrap()
+ .unwrap();
+ assert_eq!(execution.action_id, due_id);
+ cancellation_token.cancel();
+ tracker.close();
+ wait_for_tracker(&tracker).await;
+ assert_eq!(execution_count.load(Ordering::SeqCst), 1);
+}
diff --git a/services/scheduled_action/src/outbound/pg_scheduled_action_repo.rs b/services/scheduled_action/src/outbound/pg_scheduled_action_repo.rs
index 7471d6976f..e3836820bd 100644
--- a/services/scheduled_action/src/outbound/pg_scheduled_action_repo.rs
+++ b/services/scheduled_action/src/outbound/pg_scheduled_action_repo.rs
@@ -1,4 +1,4 @@
-use anyhow::{Result, bail};
+use anyhow::{Context, Result, bail};
use chrono::{DateTime, Utc};
use chrono_tz::Tz;
use entity_registry_db_utils::{
@@ -6,14 +6,17 @@ use entity_registry_db_utils::{
insert_entity,
};
use macro_user_id::user_id::MacroUserIdStr;
-use macro_uuid::Uuid;
+use macro_uuid::{Uuid, generate_uuid_v7};
use model_owner::Owner;
+use serde_json::Value;
use sqlx::PgPool;
use std::str::FromStr;
+use crate::domain::event_runs::{ClaimToken, ConfigurationRevision};
+use crate::domain::event_trigger::ActionTrigger;
use crate::domain::models::{
- ActionExecutionRecord, ActionKind, AlreadyRunningError, MAX_ACTION_TIME, Schedule,
- ScheduledAction,
+ ActionExecutionRecord, ActionKind, ActionPolicyError, AlreadyRunningError, MAX_ACTION_TIME,
+ Schedule, ScheduledAction,
};
use crate::domain::ports::ScheduledActionRepo;
@@ -47,49 +50,129 @@ fn kind_to_str(kind: &ActionKind) -> &'static str {
}
}
+/// One mapping for every action query, including management and dispatch.
+struct ActionRow {
+ id: Uuid,
+ owner: String,
+ name: String,
+ schedule: Option<String>,
+ kind: String,
+ timezone: Option<String>,
+ task: Value,
+ claimed: Option<DateTime<Utc>>,
+ created_at: DateTime<Utc>,
+ updated_at: DateTime<Utc>,
+ next_run_at: Option<DateTime<Utc>>,
+ enabled: bool,
+ trigger_type: String,
+ event_filters: Option<Value>,
+ configuration_revision: i64,
+ event_activated_at: Option<DateTime<Utc>>,
+}
+
+impl TryFrom<ActionRow> for ScheduledAction {
+ type Error = anyhow::Error;
+
+ fn try_from(row: ActionRow) -> Result<Self> {
+ let trigger = match row.trigger_type.as_str() {
+ "cron" => ActionTrigger::Cron {
+ schedule: Schedule::from_cron(row.schedule.context("cron schedule missing")?)?,
+ timezone: parse_timezone(&row.timezone.context("cron timezone missing")?)?,
+ },
+ "events" => ActionTrigger::Events {
+ filters: serde_json::from_value(
+ row.event_filters.context("event filters missing")?,
+ )?,
+ },
+ other => bail!("unknown action trigger: {other}"),
+ };
+ Ok(Self {
+ id: Some(row.id),
+ owner: Owner::from_principal_str(&row.owner)?,
+ name: row.name,
+ trigger,
+ kind: parse_kind(&row.kind)?,
+ created_at: row.created_at,
+ updated_at: row.updated_at,
+ task: row.task,
+ claimed: row.claimed,
+ next_run_at: row.next_run_at,
+ enabled: row.enabled,
+ configuration_revision: ConfigurationRevision::try_from(row.configuration_revision)?,
+ event_activated_at: row.event_activated_at,
+ })
+ }
+}
+
+struct TriggerColumns {
+ trigger_type: &'static str,
+ schedule: Option<String>,
+ timezone: Option<String>,
+ event_filters: Option<Value>,
+}
+
+impl TryFrom<&ActionTrigger> for TriggerColumns {
+ type Error = anyhow::Error;
+
+ fn try_from(trigger: &ActionTrigger) -> Result<Self> {
+ match trigger {
+ ActionTrigger::Cron { schedule, timezone } => Ok(Self {
+ trigger_type: "cron",
+ schedule: Some(schedule.as_str().to_owned()),
+ timezone: Some(timezone.to_string()),
+ event_filters: None,
+ }),
+ ActionTrigger::Events { filters } => Ok(Self {
+ trigger_type: "events",
+ schedule: None,
+ timezone: None,
+ event_filters: Some(serde_json::to_value(filters)?),
+ }),
+ }
+ }
+}
+
impl ScheduledActionRepo for PgScheduledActionRepo {
async fn create_action(&self, action: ScheduledAction) -> Result<ScheduledAction> {
+ let id = generate_uuid_v7();
let owner = action.owner.principal_id();
- let timezone = action.timezone.to_string();
+ let trigger = TriggerColumns::try_from(&action.trigger)?;
let kind = kind_to_str(&action.kind);
let mut tx = self.pool.begin().await?;
- let row = sqlx::query!(
+ let row = sqlx::query_as!(
+ ActionRow,
r#"
- INSERT INTO scheduled_action (owner, name, schedule, kind, timezone, task, next_run_at, enabled)
- VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
- RETURNING id, owner, name, schedule, kind, timezone, task, claimed, created_at, updated_at, next_run_at, enabled
+ INSERT INTO scheduled_action
+ (id, owner, name, schedule, kind, timezone, task, next_run_at, enabled,
+ trigger_type, event_filters, configuration_revision, event_activated_at)
+ VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)
+ RETURNING id, owner, name, schedule, kind, timezone, task, claimed, created_at,
+ updated_at, next_run_at, enabled, trigger_type, event_filters,
+ configuration_revision, event_activated_at
"#,
+ id,
owner,
action.name,
- action.schedule.as_str(),
+ trigger.schedule,
kind,
- timezone,
+ trigger.timezone,
action.task,
action.next_run_at,
action.enabled,
+ trigger.trigger_type,
+ trigger.event_filters,
+ action.configuration_revision.get(),
+ action.event_activated_at,
)
.fetch_one(&mut *tx)
.await?;
- let created = ScheduledAction {
- id: Some(row.id),
- owner: Owner::from_principal_str(&row.owner)?,
- name: row.name,
- schedule: Schedule::from_cron(row.schedule)?,
- kind: parse_kind(&row.kind)?,
- created_at: row.created_at,
- updated_at: row.updated_at,
- timezone: parse_timezone(&row.timezone)?,
- task: row.task,
- claimed: row.claimed,
- next_run_at: row.next_run_at,
- enabled: row.enabled,
- };
+ let created = ScheduledAction::try_from(row)?;
match insert_entity(
&mut tx,
NewEntityRecord::new(
- row.id,
+ id,
RegisteredEntityType::ScheduledAction,
created.owner.clone(),
),
@@ -104,10 +187,12 @@ impl ScheduledActionRepo for PgScheduledActionRepo {
async fn get_actions(&self, user_id: MacroUserIdStr<'static>) -> Result<Vec<ScheduledAction>> {
let owner = user_id.to_string();
-
- let rows = sqlx::query!(
+ let rows = sqlx::query_as!(
+ ActionRow,
r#"
- SELECT id, owner, name, schedule, kind, timezone, task, claimed, created_at, updated_at, next_run_at, enabled
+ SELECT id, owner, name, schedule, kind, timezone, task, claimed, created_at,
+ updated_at, next_run_at, enabled, trigger_type, event_filters,
+ configuration_revision, event_activated_at
FROM scheduled_action
WHERE owner = $1
"#,
@@ -115,37 +200,45 @@ impl ScheduledActionRepo for PgScheduledActionRepo {
)
.fetch_all(&self.pool)
.await?;
+ rows.into_iter().map(ScheduledAction::try_from).collect()
+ }
- rows.into_iter()
- .map(|row| {
- Ok(ScheduledAction {
- id: Some(row.id),
- owner: Owner::from_principal_str(&row.owner)?,
- name: row.name,
- schedule: Schedule::from_cron(row.schedule)?,
- kind: parse_kind(&row.kind)?,
- created_at: row.created_at,
- updated_at: row.updated_at,
- timezone: parse_timezone(&row.timezone)?,
- task: row.task,
- claimed: row.claimed,
- next_run_at: row.next_run_at,
- enabled: row.enabled,
- })
- })
- .collect()
+ async fn get_action(
+ &self,
+ id: &Uuid,
+ user_id: MacroUserIdStr<'static>,
+ ) -> Result<Option<ScheduledAction>> {
+ let owner = user_id.to_string();
+ sqlx::query_as!(
+ ActionRow,
+ r#"
+ SELECT id, owner, name, schedule, kind, timezone, task, claimed, created_at,
+ updated_at, next_run_at, enabled, trigger_type, event_filters,
+ configuration_revision, event_activated_at
+ FROM scheduled_action
+ WHERE id = $1 AND owner = $2
+ "#,
+ *id,
+ owner,
+ )
+ .fetch_optional(&self.pool)
+ .await?
+ .map(ScheduledAction::try_from)
+ .transpose()
}
async fn get_next_unclaimed_actions(&self, limit: i64) -> Result<Vec<ScheduledAction>> {
let stale_threshold = Utc::now() - MAX_ACTION_TIME;
-
- let rows = sqlx::query!(
+ let rows = sqlx::query_as!(
+ ActionRow,
r#"
- SELECT id, owner, name, schedule, kind, timezone, task, claimed, created_at, updated_at, next_run_at, enabled
+ SELECT id, owner, name, schedule, kind, timezone, task, claimed, created_at,
+ updated_at, next_run_at, enabled, trigger_type, event_filters,
+ configuration_revision, event_activated_at
FROM scheduled_action
- WHERE enabled
+ WHERE enabled AND trigger_type = 'cron'
AND (claimed IS NULL OR claimed < $1)
- ORDER BY next_run_at ASC
+ ORDER BY next_run_at ASC, id ASC
LIMIT $2
"#,
stale_threshold,
@@ -153,35 +246,18 @@ impl ScheduledActionRepo for PgScheduledActionRepo {
)
.fetch_all(&self.pool)
.await?;
-
- rows.into_iter()
- .map(|row| {
- Ok(ScheduledAction {
- id: Some(row.id),
- owner: Owner::from_principal_str(&row.owner)?,
- name: row.name,
- schedule: Schedule::from_cron(row.schedule)?,
- kind: parse_kind(&row.kind)?,
- created_at: row.created_at,
- updated_at: row.updated_at,
- timezone: parse_timezone(&row.timezone)?,
- task: row.task,
- claimed: row.claimed,
- next_run_at: row.next_run_at,
- enabled: row.enabled,
- })
- })
- .collect()
+ rows.into_iter().map(ScheduledAction::try_from).collect()
}
async fn update_action(&self, action: ScheduledAction) -> Result<ScheduledAction> {
let Some(id) = action.id else {
bail!("cannot update action without id");
};
- let timezone = action.timezone.to_string();
+ let owner = action.owner.principal_id();
+ let trigger = TriggerColumns::try_from(&action.trigger)?;
let kind = kind_to_str(&action.kind);
-
- let row = sqlx::query!(
+ let row = sqlx::query_as!(
+ ActionRow,
r#"
UPDATE scheduled_action
SET name = $1,
@@ -191,75 +267,87 @@ impl ScheduledActionRepo for PgScheduledActionRepo {
task = $5,
next_run_at = $6,
enabled = $7,
+ trigger_type = $8,
+ event_filters = $9,
+ configuration_revision = $10,
+ event_activated_at = $11,
updated_at = now()
- WHERE id = $8
- RETURNING id, owner, name, schedule, kind, timezone, task, claimed, created_at, updated_at, next_run_at, enabled
+ WHERE id = $12 AND owner = $13
+ AND configuration_revision = $10::bigint - 1
+ AND (
+ claimed IS NULL OR claimed < $14
+ OR (
+ NOT $7 AND name = $1 AND kind = $3 AND task = $5
+ AND trigger_type = $8
+ AND schedule IS NOT DISTINCT FROM $2
+ AND timezone IS NOT DISTINCT FROM $4
+ AND event_filters IS NOT DISTINCT FROM $9
+ )
+ )
+ RETURNING id, owner, name, schedule, kind, timezone, task, claimed, created_at,
+ updated_at, next_run_at, enabled, trigger_type, event_filters,
+ configuration_revision, event_activated_at
"#,
action.name,
- action.schedule.as_str(),
+ trigger.schedule,
kind,
- timezone,
+ trigger.timezone,
action.task,
action.next_run_at,
action.enabled,
+ trigger.trigger_type,
+ trigger.event_filters,
+ action.configuration_revision.get(),
+ action.event_activated_at,
id,
+ owner,
+ Utc::now() - MAX_ACTION_TIME,
)
- .fetch_one(&self.pool)
- .await?;
-
- Ok(ScheduledAction {
- id: Some(row.id),
- owner: Owner::from_principal_str(&row.owner)?,
- name: row.name,
- schedule: Schedule::from_cron(row.schedule)?,
- kind: parse_kind(&row.kind)?,
- created_at: row.created_at,
- updated_at: row.updated_at,
- timezone: parse_timezone(&row.timezone)?,
- task: row.task,
- claimed: row.claimed,
- next_run_at: row.next_run_at,
- enabled: row.enabled,
- })
+ .fetch_optional(&self.pool)
+ .await?
+ .ok_or(ActionPolicyError::UpdateConflict)?;
+ ScheduledAction::try_from(row)
}
- async fn delete_action(
- &self,
- id: &Uuid,
- _macro_user_id: MacroUserIdStr<'static>,
- ) -> Result<()> {
+ async fn delete_action(&self, id: &Uuid, macro_user_id: MacroUserIdStr<'static>) -> Result<()> {
+ let owner = macro_user_id.to_string();
let mut tx = self.pool.begin().await?;
- sqlx::query!(
+ let deleted = sqlx::query!(
r#"
DELETE FROM scheduled_action
- WHERE id = $1
+ WHERE id = $1 AND owner = $2
"#,
- *id
+ *id,
+ owner,
)
.execute(&mut *tx)
.await?;
- match delete_entity(&mut tx, *id).await? {
- WriteOutcome::Applied | WriteOutcome::NotFound => {}
+ if deleted.rows_affected() != 0 {
+ match delete_entity(&mut tx, *id).await? {
+ WriteOutcome::Applied | WriteOutcome::NotFound => {}
+ }
}
tx.commit().await?;
Ok(())
}
- async fn claim_action(&self, id: &Uuid) -> Result<()> {
+ async fn claim_action(&self, id: &Uuid) -> Result<ClaimToken> {
+ let token = ClaimToken::generate();
let now = Utc::now();
let stale_threshold = now - MAX_ACTION_TIME;
let result = sqlx::query!(
r#"
UPDATE scheduled_action
- SET claimed = $1, updated_at = now()
+ SET claimed = $1, claim_token = $4, updated_at = now()
WHERE id = $2
AND (claimed IS NULL OR claimed < $3)
"#,
now,
*id,
stale_threshold,
+ token.as_uuid(),
)
.execute(&self.pool)
.await?;
@@ -268,17 +356,18 @@ impl ScheduledActionRepo for PgScheduledActionRepo {
return Err(anyhow::Error::new(AlreadyRunningError { action_id: *id }));
}
- Ok(())
+ Ok(token)
}
- async fn release_action(&self, id: &Uuid) -> Result<()> {
+ async fn release_action(&self, id: &Uuid, token: ClaimToken) -> Result<()> {
sqlx::query!(
r#"
UPDATE scheduled_action
- SET claimed = NULL, updated_at = now()
- WHERE id = $1
+ SET claimed = NULL, claim_token = NULL, updated_at = now()
+ WHERE id = $1 AND claim_token = $2
"#,
*id,
+ token.as_uuid(),
)
.execute(&self.pool)
.await?;
@@ -334,35 +423,39 @@ impl ScheduledActionRepo for PgScheduledActionRepo {
}
async fn update_next_run_at(&self, id: &Uuid) -> Result<()> {
+ // Lock through the update so a concurrent trigger replacement cannot
+ // receive a firing time derived from the old schedule.
+ let mut tx = self.pool.begin().await?;
let row = sqlx::query!(
r#"
- SELECT schedule, timezone
+ SELECT schedule, timezone, trigger_type
FROM scheduled_action
WHERE id = $1
+ FOR UPDATE
"#,
*id,
)
- .fetch_one(&self.pool)
- .await?;
-
- let tz = parse_timezone(&row.timezone)?;
- let schedule = Schedule::from_cron(row.schedule)?;
- let Some(next_run_at) = schedule.next_run_after_now(tz) else {
- return Ok(());
- };
-
- sqlx::query!(
- r#"
- UPDATE scheduled_action
- SET next_run_at = $1, updated_at = now()
- WHERE id = $2
- "#,
- next_run_at,
- *id,
- )
- .execute(&self.pool)
+ .fetch_one(&mut *tx)
.await?;
+ if row.trigger_type == "cron" {
+ let tz = parse_timezone(&row.timezone.context("cron timezone missing")?)?;
+ let schedule = Schedule::from_cron(row.schedule.context("cron schedule missing")?)?;
+ if let Some(next_run_at) = schedule.next_run_after_now(tz) {
+ sqlx::query!(
+ r#"
+ UPDATE scheduled_action
+ SET next_run_at = $1, updated_at = now()
+ WHERE id = $2 AND trigger_type = 'cron'
+ "#,
+ next_run_at,
+ *id,
+ )
+ .execute(&mut *tx)
+ .await?;
+ }
+ }
+ tx.commit().await?;
Ok(())
}
diff --git a/services/scheduled_action/src/outbound/pg_scheduled_action_repo/test.rs b/services/scheduled_action/src/outbound/pg_scheduled_action_repo/test.rs
index ffdea9f3c8..d369312145 100644
--- a/services/scheduled_action/src/outbound/pg_scheduled_action_repo/test.rs
+++ b/services/scheduled_action/src/outbound/pg_scheduled_action_repo/test.rs
@@ -1,12 +1,14 @@
-use chrono::{DateTime, Utc};
+use chrono::Utc;
use macro_db_migrator::MACRO_DB_MIGRATIONS;
use macro_user_id::user_id::MacroUserIdStr;
use macro_uuid::Uuid;
use model_owner::Owner;
use serde_json::json;
-use sqlx::{PgPool, Row};
+use sqlx::PgPool;
use super::*;
+use crate::domain::event_runs::ConfigurationRevision;
+use crate::domain::event_trigger::{ActionTrigger, EventFilter, EventFilters, EventName};
use crate::domain::models::{ActionKind, AlreadyRunningError, Schedule, ScheduledAction};
use crate::domain::ports::ScheduledActionRepo;
@@ -24,20 +26,22 @@ fn user_owner(id: &'static str) -> Owner {
async fn insert_user(pool: &PgPool, id: &str) {
let macro_user_id = macro_uuid::generate_uuid_v7();
- sqlx::query(
+ sqlx::query!(
r#"INSERT INTO macro_user (id, username, email, stripe_customer_id) VALUES ($1, $2, $2, $2)"#,
+ macro_user_id,
+ id,
)
- .bind(macro_user_id)
- .bind(id)
.execute(pool)
.await
.expect("macro_user should insert");
- sqlx::query(r#"INSERT INTO "User" (id, email, macro_user_id) VALUES ($1, $1, $2)"#)
- .bind(id)
- .bind(macro_user_id)
- .execute(pool)
- .await
- .expect("user should insert");
+ sqlx::query!(
+ r#"INSERT INTO "User" (id, email, macro_user_id) VALUES ($1, $1, $2)"#,
+ id,
+ macro_user_id,
+ )
+ .execute(pool)
+ .await
+ .expect("user should insert");
}
fn sample_action(owner: Owner, name: &str) -> ScheduledAction {
@@ -51,50 +55,37 @@ fn sample_action(owner: Owner, name: &str) -> ScheduledAction {
id: None,
owner,
name: name.to_string(),
- schedule,
+ trigger: ActionTrigger::Cron { schedule, timezone },
kind: ActionKind::Agent,
created_at: now,
updated_at: now,
- timezone,
+ configuration_revision: ConfigurationRevision::INITIAL,
+ event_activated_at: None,
task: json!({}),
claimed: None,
- next_run_at,
+ next_run_at: Some(next_run_at),
enabled: true,
}
}
-async fn fetch_entity_row(pool: &PgPool, id: Uuid) -> sqlx::postgres::PgRow {
- sqlx::query(
- r#"
- SELECT
- owner_type::text AS owner_type,
- owner_id,
- entity_type,
- deleted_at
- FROM entity
- WHERE id = $1
- "#,
+async fn entity_row_count(pool: &PgPool, id: Uuid) -> i64 {
+ sqlx::query_scalar!(
+ r#"SELECT COUNT(*) AS "count!" FROM entity WHERE id = $1"#,
+ id
)
- .bind(id)
.fetch_one(pool)
.await
.unwrap()
}
-async fn entity_row_count(pool: &PgPool, id: Uuid) -> i64 {
- sqlx::query_scalar("SELECT COUNT(*) FROM entity WHERE id = $1")
- .bind(id)
- .fetch_one(pool)
- .await
- .unwrap()
-}
-
async fn scheduled_action_row_count(pool: &PgPool, id: Uuid) -> i64 {
- sqlx::query_scalar("SELECT COUNT(*) FROM scheduled_action WHERE id = $1")
- .bind(id)
- .fetch_one(pool)
- .await
- .unwrap()
+ sqlx::query_scalar!(
+ r#"SELECT COUNT(*) AS "count!" FROM scheduled_action WHERE id = $1"#,
+ id,
+ )
+ .fetch_one(pool)
+ .await
+ .unwrap()
}
#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
@@ -106,7 +97,7 @@ async fn create_action_returns_id_and_is_listable_by_owner(pool: PgPool) {
.create_action(sample_action(user_owner(USER_A), "standup"))
.await
.expect("create should succeed");
- assert!(created.id.is_some());
+ assert_eq!(created.id.unwrap().get_version_num(), 7);
assert_eq!(created.owner, user_owner(USER_A));
let listed = repo
@@ -158,20 +149,69 @@ async fn update_action_changes_name_schedule_and_enabled(pool: PgPool) {
.expect("schedule has a future firing");
let updated = repo
.update_action(ScheduledAction {
+ configuration_revision: created.configuration_revision.next().unwrap(),
name: "evening standup".to_string(),
- schedule,
+ trigger: ActionTrigger::Cron {
+ schedule,
+ timezone: chrono_tz::UTC,
+ },
enabled: false,
- next_run_at,
+ next_run_at: Some(next_run_at),
..created
})
.await
.expect("update should succeed");
assert_eq!(updated.name, "evening standup");
- assert_eq!(updated.schedule.as_str(), "0 0 18 * * *");
+ let ActionTrigger::Cron { schedule, .. } = updated.trigger else {
+ panic!("expected cron trigger");
+ };
+ assert_eq!(schedule.as_str(), "0 0 18 * * *");
assert!(!updated.enabled);
}
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn replacement_is_fenced_against_claims_and_stale_revisions(pool: PgPool) {
+ insert_user(&pool, USER_A).await;
+ let repo = PgScheduledActionRepo::new(pool);
+ let action = repo.create_action(event_action()).await.unwrap();
+ let id = action.id.unwrap();
+ let mut replacement = action.clone();
+ replacement.configuration_revision = action.configuration_revision.next().unwrap();
+ replacement.name = "replacement".into();
+
+ // Claim after management read, before its write.
+ let token = repo.claim_action(&id).await.unwrap();
+ let error = repo.update_action(replacement.clone()).await.unwrap_err();
+ assert!(matches!(
+ error.downcast_ref(),
+ Some(ActionPolicyError::UpdateConflict)
+ ));
+ let disabled = repo
+ .update_action(ScheduledAction {
+ enabled: false,
+ configuration_revision: replacement.configuration_revision,
+ ..action
+ })
+ .await
+ .unwrap();
+ assert!(disabled.claimed.is_some());
+ repo.release_action(&id, token).await.unwrap();
+
+ // The earlier replacement is stale even after execution has finished.
+ let error = repo.update_action(replacement).await.unwrap_err();
+ assert!(matches!(
+ error.downcast_ref(),
+ Some(ActionPolicyError::UpdateConflict)
+ ));
+ let current = repo.get_action(&id, user(USER_A)).await.unwrap().unwrap();
+ assert!(!current.enabled);
+ assert_eq!(
+ current.configuration_revision,
+ disabled.configuration_revision
+ );
+}
+
#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn second_claim_returns_already_running(pool: PgPool) {
insert_user(&pool, USER_A).await;
@@ -193,6 +233,29 @@ async fn second_claim_returns_already_running(pool: PgPool) {
assert!(error.downcast_ref::<AlreadyRunningError>().is_some());
}
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn release_is_fenced_to_its_own_execution(pool: PgPool) {
+ insert_user(&pool, USER_A).await;
+ let repo = PgScheduledActionRepo::new(pool);
+ let action = repo
+ .create_action(sample_action(user_owner(USER_A), "fenced"))
+ .await
+ .unwrap();
+ let id = action.id.unwrap();
+ let old = repo.claim_action(&id).await.unwrap();
+ repo.release_action(&id, crate::domain::event_runs::ClaimToken::generate())
+ .await
+ .unwrap();
+ assert!(repo.claim_action(&id).await.is_err());
+ repo.release_action(&id, old).await.unwrap();
+ let current = repo.claim_action(&id).await.unwrap();
+ assert_ne!(old, current);
+ repo.release_action(&id, old).await.unwrap();
+ assert!(repo.claim_action(&id).await.is_err());
+ repo.release_action(&id, current).await.unwrap();
+ assert!(repo.claim_action(&id).await.is_ok());
+}
+
#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn delete_action_removes_row_from_owner_list(pool: PgPool) {
insert_user(&pool, USER_A).await;
@@ -226,11 +289,25 @@ async fn create_action_registers_entity_row(pool: PgPool) {
.expect("create should succeed");
let id = created.id.expect("create returns Some(id)");
- let row = fetch_entity_row(&pool, id).await;
- assert_eq!(row.get::<String, _>("entity_type"), "scheduled_action");
- assert_eq!(row.get::<String, _>("owner_type"), "user");
- assert_eq!(row.get::<String, _>("owner_id"), USER_A);
- assert_eq!(row.get::<Option<DateTime<Utc>>, _>("deleted_at"), None);
+ let row = sqlx::query!(
+ r#"
+ SELECT
+ owner_type::text AS "owner_type!",
+ owner_id,
+ entity_type,
+ deleted_at
+ FROM entity
+ WHERE id = $1
+ "#,
+ id,
+ )
+ .fetch_one(&pool)
+ .await
+ .unwrap();
+ assert_eq!(row.entity_type, "scheduled_action");
+ assert_eq!(row.owner_type, "user");
+ assert_eq!(row.owner_id, USER_A);
+ assert_eq!(row.deleted_at, None);
}
#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
@@ -262,8 +339,7 @@ async fn delete_action_succeeds_when_entity_row_is_missing(pool: PgPool) {
.expect("create should succeed");
let id = created.id.expect("create returns Some(id)");
- sqlx::query("DELETE FROM entity WHERE id = $1")
- .bind(id)
+ sqlx::query!("DELETE FROM entity WHERE id = $1", id)
.execute(&pool)
.await
.expect("entity row should delete");
@@ -275,3 +351,366 @@ async fn delete_action_succeeds_when_entity_row_is_missing(pool: PgPool) {
assert_eq!(entity_row_count(&pool, id).await, 0);
assert_eq!(scheduled_action_row_count(&pool, id).await, 0);
}
+
+fn event_action() -> ScheduledAction {
+ let filters = EventFilters::try_from(vec![
+ EventFilter::new(vec![EventName::DocumentCreated], None).unwrap(),
+ ])
+ .unwrap();
+ ScheduledAction {
+ trigger: ActionTrigger::Events { filters },
+ next_run_at: None,
+ event_activated_at: Some(Utc::now()),
+ ..sample_action(user_owner(USER_A), "document routine")
+ }
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn event_round_trip_and_bookkeeping_does_not_advance_configuration(pool: PgPool) {
+ insert_user(&pool, USER_A).await;
+ let repo = PgScheduledActionRepo::new(pool);
+ let created = repo.create_action(event_action()).await.unwrap();
+ let id = created.id.unwrap();
+ assert_eq!(id.get_version_num(), 7);
+ let before = repo.get_action(&id, user(USER_A)).await.unwrap().unwrap();
+ assert!(matches!(before.trigger, ActionTrigger::Events { .. }));
+ assert_eq!(before.next_run_at, None);
+ assert!(before.event_activated_at.is_some());
+ repo.update_next_run_at(&id).await.unwrap();
+ let after = repo.get_action(&id, user(USER_A)).await.unwrap().unwrap();
+ assert_eq!(after.updated_at, before.updated_at);
+ assert_eq!(after.next_run_at, None);
+ let token = repo.claim_action(&id).await.unwrap();
+ repo.release_action(&id, token).await.unwrap();
+ repo.update_last_executed(&id, Utc::now()).await.unwrap();
+ let after = repo.get_action(&id, user(USER_A)).await.unwrap().unwrap();
+ assert_eq!(after.configuration_revision, before.configuration_revision);
+ assert_eq!(after.event_activated_at, before.event_activated_at);
+ assert_eq!(
+ serde_json::to_value(&after.trigger).unwrap(),
+ serde_json::to_value(&before.trigger).unwrap()
+ );
+
+ let filters = EventFilters::try_from(vec![
+ EventFilter::new(
+ vec![EventName::ChannelMessagePosted],
+ Some(vec![macro_uuid::generate_uuid_v7()]),
+ )
+ .unwrap(),
+ ])
+ .unwrap();
+ let updated = repo
+ .update_action(ScheduledAction {
+ trigger: ActionTrigger::Events {
+ filters: filters.clone(),
+ },
+ enabled: false,
+ configuration_revision: after.configuration_revision.next().unwrap(),
+ ..after
+ })
+ .await
+ .unwrap();
+ let ActionTrigger::Events {
+ filters: stored_filters,
+ } = updated.trigger
+ else {
+ panic!("expected event trigger");
+ };
+ assert_eq!(stored_filters, filters);
+ assert!(!updated.enabled);
+ assert_eq!(updated.next_run_at, None);
+ assert_eq!(updated.configuration_revision.get(), 2);
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn lookup_update_and_delete_are_owner_scoped(pool: PgPool) {
+ insert_user(&pool, USER_A).await;
+ insert_user(&pool, USER_B).await;
+ let repo = PgScheduledActionRepo::new(pool.clone());
+ for action in [sample_action(user_owner(USER_A), "cron"), event_action()] {
+ let created = repo.create_action(action).await.unwrap();
+ let id = created.id.unwrap();
+ assert!(repo.get_action(&id, user(USER_B)).await.unwrap().is_none());
+ let mut foreign_update = created.clone();
+ foreign_update.owner = user_owner(USER_B);
+ foreign_update.configuration_revision = created.configuration_revision.next().unwrap();
+ assert!(repo.update_action(foreign_update).await.is_err());
+ repo.delete_action(&id, user(USER_B)).await.unwrap();
+ assert!(repo.get_action(&id, user(USER_A)).await.unwrap().is_some());
+ assert_eq!(entity_row_count(&pool, id).await, 1);
+ repo.delete_action(&id, user(USER_A)).await.unwrap();
+ assert!(repo.get_action(&id, user(USER_A)).await.unwrap().is_none());
+ assert_eq!(entity_row_count(&pool, id).await, 0);
+ }
+ assert!(
+ repo.get_action(&macro_uuid::generate_uuid_v7(), user(USER_A))
+ .await
+ .unwrap()
+ .is_none()
+ );
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn trigger_transitions_replace_all_trigger_columns(pool: PgPool) {
+ insert_user(&pool, USER_A).await;
+ let repo = PgScheduledActionRepo::new(pool);
+ let cron = repo
+ .create_action(sample_action(user_owner(USER_A), "cron"))
+ .await
+ .unwrap();
+ let events = event_action();
+ let event = repo
+ .update_action(ScheduledAction {
+ trigger: events.trigger,
+ next_run_at: None,
+ event_activated_at: events.event_activated_at,
+ configuration_revision: cron.configuration_revision.next().unwrap(),
+ ..cron
+ })
+ .await
+ .unwrap();
+ assert!(matches!(event.trigger, ActionTrigger::Events { .. }));
+ assert_eq!(event.configuration_revision.get(), 2);
+ assert!(
+ repo.get_next_unclaimed_actions(10)
+ .await
+ .unwrap()
+ .is_empty()
+ );
+ let cron_config = sample_action(user_owner(USER_A), "cron again");
+ let cron = repo
+ .update_action(ScheduledAction {
+ trigger: cron_config.trigger,
+ next_run_at: cron_config.next_run_at,
+ event_activated_at: None,
+ configuration_revision: event.configuration_revision.next().unwrap(),
+ ..event
+ })
+ .await
+ .unwrap();
+ assert!(matches!(cron.trigger, ActionTrigger::Cron { .. }));
+ assert_eq!(cron.configuration_revision.get(), 3);
+ assert_eq!(cron.event_activated_at, None);
+ assert!(cron.next_run_at.is_some());
+ assert_eq!(repo.get_next_unclaimed_actions(10).await.unwrap().len(), 1);
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn polling_returns_only_enabled_unclaimed_cron_rows(pool: PgPool) {
+ insert_user(&pool, USER_A).await;
+ let repo = PgScheduledActionRepo::new(pool);
+ repo.create_action(event_action()).await.unwrap();
+ let disabled = ScheduledAction {
+ enabled: false,
+ ..sample_action(user_owner(USER_A), "disabled")
+ };
+ repo.create_action(disabled).await.unwrap();
+ let claimed = repo
+ .create_action(sample_action(user_owner(USER_A), "claimed"))
+ .await
+ .unwrap();
+ repo.claim_action(&claimed.id.unwrap()).await.unwrap();
+ let cron = repo
+ .create_action(sample_action(user_owner(USER_A), "cron"))
+ .await
+ .unwrap();
+ let candidates = repo.get_next_unclaimed_actions(1).await.unwrap();
+ assert_eq!(candidates.len(), 1);
+ assert_eq!(candidates[0].id, cron.id);
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn deployed_cron_insert_remains_valid(pool: PgPool) {
+ insert_user(&pool, USER_A).await;
+ let next_run_at = Utc::now();
+ // Exactly the deployed writer's columns: no new discriminator, revision,
+ // activation, filters or application-generated ID.
+ let row = sqlx::query!(
+ r#"
+ INSERT INTO scheduled_action (owner, name, schedule, kind, timezone, task, next_run_at, enabled)
+ VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
+ RETURNING id, trigger_type, configuration_revision, event_filters, event_activated_at
+ "#,
+ USER_A, "legacy", DAILY_9AM, "Agent", "UTC", json!({}), next_run_at, true,
+ ).fetch_one(&pool).await.unwrap();
+ assert_eq!(row.trigger_type, "cron");
+ assert_eq!(row.configuration_revision, 1);
+ assert_eq!(row.event_filters, None);
+ assert_eq!(row.event_activated_at, None);
+ let repo = PgScheduledActionRepo::new(pool);
+ let action = repo
+ .get_action(&row.id, user(USER_A))
+ .await
+ .unwrap()
+ .unwrap();
+ assert!(matches!(action.trigger, ActionTrigger::Cron { .. }));
+ assert!(action.next_run_at.is_some());
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn database_rejects_invalid_trigger_shapes(pool: PgPool) {
+ insert_user(&pool, USER_A).await;
+ let now = Utc::now();
+ let filters = json!([{ "events": ["document.created"] }]);
+ // SQL CHECK must reject NULL/unknown discriminators, mixed triggers, absent
+ // cron fields, absent event activation/filters, and non-array/empty filters.
+ let cases = [
+ (
+ Some("unknown"),
+ Some(DAILY_9AM),
+ Some("UTC"),
+ Some(now),
+ None,
+ None,
+ 1,
+ ),
+ (Some("cron"), None, Some("UTC"), Some(now), None, None, 1),
+ (
+ Some("cron"),
+ Some(DAILY_9AM),
+ None,
+ Some(now),
+ None,
+ None,
+ 1,
+ ),
+ (
+ Some("cron"),
+ Some(DAILY_9AM),
+ Some("UTC"),
+ None,
+ None,
+ None,
+ 1,
+ ),
+ (
+ Some("cron"),
+ Some(DAILY_9AM),
+ Some("UTC"),
+ Some(now),
+ Some(filters.clone()),
+ None,
+ 1,
+ ),
+ (
+ Some("cron"),
+ Some(DAILY_9AM),
+ Some("UTC"),
+ Some(now),
+ None,
+ Some(now),
+ 1,
+ ),
+ (Some("events"), None, None, None, None, Some(now), 1),
+ (
+ Some("events"),
+ None,
+ None,
+ None,
+ Some(filters.clone()),
+ None,
+ 1,
+ ),
+ (
+ Some("events"),
+ Some(DAILY_9AM),
+ None,
+ None,
+ Some(filters.clone()),
+ Some(now),
+ 1,
+ ),
+ (
+ Some("events"),
+ None,
+ Some("UTC"),
+ None,
+ Some(filters.clone()),
+ Some(now),
+ 1,
+ ),
+ (
+ Some("events"),
+ None,
+ None,
+ Some(now),
+ Some(filters.clone()),
+ Some(now),
+ 1,
+ ),
+ (
+ Some("events"),
+ None,
+ None,
+ None,
+ Some(json!(null)),
+ Some(now),
+ 1,
+ ),
+ (
+ Some("events"),
+ None,
+ None,
+ None,
+ Some(json!({})),
+ Some(now),
+ 1,
+ ),
+ (
+ Some("events"),
+ None,
+ None,
+ None,
+ Some(json!([])),
+ Some(now),
+ 1,
+ ),
+ (
+ Some("events"),
+ None,
+ None,
+ None,
+ Some(json!(vec![filters.clone(); 33])),
+ Some(now),
+ 1,
+ ),
+ (
+ Some("events"),
+ None,
+ None,
+ None,
+ Some(filters),
+ Some(now),
+ 0,
+ ),
+ (None, Some(DAILY_9AM), Some("UTC"), Some(now), None, None, 1),
+ ];
+ for (trigger_type, schedule, timezone, next_run_at, filters, activated_at, revision) in cases {
+ let id = macro_uuid::generate_uuid_v7();
+ let error = sqlx::query!(
+ r#"
+ INSERT INTO scheduled_action
+ (id, owner, name, kind, task, enabled, trigger_type, schedule, timezone,
+ next_run_at, event_filters, event_activated_at, configuration_revision)
+ VALUES ($1, $2, 'invalid', 'Agent', '{}', true, $3, $4, $5, $6, $7, $8, $9)
+ "#,
+ id,
+ USER_A,
+ trigger_type,
+ schedule,
+ timezone,
+ next_run_at,
+ filters,
+ activated_at,
+ revision,
+ )
+ .execute(&pool)
+ .await
+ .expect_err("invalid shape must fail");
+ let code = error.as_database_error().unwrap().code().unwrap();
+ assert!(
+ code == "23514" || code == "23502",
+ "expected constraint violation: {error}"
+ );
+ }
+}
diff --git a/services/scheduled_action/src/outbound/tokio_dispatcher.rs b/services/scheduled_action/src/outbound/tokio_dispatcher.rs
index 21d4fd3c74..8387d09f1e 100644
--- a/services/scheduled_action/src/outbound/tokio_dispatcher.rs
+++ b/services/scheduled_action/src/outbound/tokio_dispatcher.rs
@@ -7,9 +7,13 @@ use futures::stream::FuturesUnordered;
use macro_uuid::Uuid;
use tokio::sync::mpsc::{Receiver, Sender};
+use crate::domain::event_trigger::ActionTrigger;
use crate::domain::models::{DispatchEvent, InProgressExecution, ScheduledAction};
use crate::domain::ports::{ScheduledActionDispatcher, ScheduledActionExecutor};
+#[cfg(test)]
+mod test;
+
const BUFFER_SIZE: usize = 1024;
type SleepFuture = Pin<Box<dyn Future<Output = (Uuid, u64)> + Send>>;
@@ -34,9 +38,12 @@ fn action_sleep(id: Uuid, action: &ScheduledAction, generation: u64) -> Option<S
return None;
}
- let cron = action.schedule.as_cron();
- let next = cron.upcoming(action.timezone).next()?;
- let now = Utc::now().with_timezone(&action.timezone);
+ let ActionTrigger::Cron { schedule, timezone } = &action.trigger else {
+ return None;
+ };
+ let cron = schedule.as_cron();
+ let next = cron.upcoming(*timezone).next()?;
+ let now = Utc::now().with_timezone(timezone);
let duration = (next - now).to_std().unwrap_or(std::time::Duration::ZERO);
Some(Box::pin(async move {
diff --git a/services/scheduled_action/src/outbound/tokio_dispatcher/test.rs b/services/scheduled_action/src/outbound/tokio_dispatcher/test.rs
new file mode 100644
index 0000000000..6384e6de52
--- /dev/null
+++ b/services/scheduled_action/src/outbound/tokio_dispatcher/test.rs
@@ -0,0 +1,40 @@
+use super::*;
+use crate::domain::event_runs::ConfigurationRevision;
+use crate::domain::models::{ActionKind, Schedule};
+use model_owner::Owner;
+use serde_json::json;
+
+#[test]
+fn only_enabled_cron_actions_get_a_timer() {
+ let now = Utc::now();
+ let id = macro_uuid::generate_uuid_v7();
+ let mut action = ScheduledAction {
+ id: Some(id),
+ owner: Owner::from_principal_str("macro|[email protected]").unwrap(),
+ name: "routine".into(),
+ trigger: ActionTrigger::Cron {
+ schedule: Schedule::from_cron("* * * * * *".into()).unwrap(),
+ timezone: chrono_tz::UTC,
+ },
+ kind: ActionKind::Agent,
+ created_at: now,
+ updated_at: now,
+ configuration_revision: ConfigurationRevision::INITIAL,
+ event_activated_at: None,
+ task: json!({}),
+ claimed: None,
+ next_run_at: Some(now),
+ enabled: true,
+ };
+ assert!(action_sleep(id, &action, 0).is_some());
+ action.enabled = false;
+ assert!(action_sleep(id, &action, 1).is_none());
+ action.enabled = true;
+ action.trigger = serde_json::from_value(json!({
+ "type": "events", "filters": [{"events": ["document.created"]}]
+ }))
+ .unwrap();
+ action.next_run_at = None;
+ action.event_activated_at = Some(now);
+ assert!(action_sleep(id, &action, 2).is_none());
+}
diff --git a/services/scheduled_action/src/swagger.rs b/services/scheduled_action/src/swagger.rs
index 51178b8aaf..8bb54357ad 100644
--- a/services/scheduled_action/src/swagger.rs
+++ b/services/scheduled_action/src/swagger.rs
@@ -1,17 +1,19 @@
use utoipa::OpenApi;
-#[allow(
+#[expect(
unused_imports,
reason = "utoipa path macros require these generated symbols in scope"
)]
use crate::inbound::axum_router::{
__path_create_action, __path_delete_action, __path_execute_action, __path_health,
- __path_list_actions, __path_list_history, __path_update_action,
+ __path_list_actions, __path_list_history, __path_update_action, ScheduledActionResponse,
};
+use crate::domain::event_trigger::{ActionTrigger, EventFilter, EventFilters, EventName};
use crate::domain::models::{
- ActionExecutionRecord, ActionKind, AgentTask, CreateScheduledAction, InProgressExecution,
- Schedule, ScheduledAction, ScheduledActionUpdate, UpdateScheduledAction,
+ ActionConfiguration, ActionExecutionRecord, ActionKind, AgentTask, CreateScheduledAction,
+ InProgressExecution, LegacyActionConfiguration, Schedule, ScheduledAction,
+ ScheduledActionUpdate, UpdateScheduledAction,
};
use model::response::EmptyResponse;
@@ -34,6 +36,13 @@ use model::response::EmptyResponse;
components(
schemas(
ScheduledAction,
+ ScheduledActionResponse,
+ ActionConfiguration,
+ LegacyActionConfiguration,
+ ActionTrigger,
+ EventFilter,
+ EventFilters,
+ EventName,
CreateScheduledAction,
UpdateScheduledAction,
Schedule,
diff --git a/services/scheduled_action/tests/event_routines.rs b/services/scheduled_action/tests/event_routines.rs
new file mode 100644
index 0000000000..1e84ceba56
--- /dev/null
+++ b/services/scheduled_action/tests/event_routines.rs
@@ -0,0 +1,718 @@
+//! API -> PostgreSQL admission -> worker -> shared executor regressions.
+//! Access, agent side effects, and broker redelivery are simulated; no model or
+//! Kafka connection is made. Each test owns an isolated migrated database.
+
+use std::{
+ future::pending,
+ sync::{Arc, Mutex},
+ time::Duration,
+};
+
+use axum::{
+ Router,
+ body::{Body, to_bytes},
+ http::{Request, StatusCode, header},
+};
+use chrono::Utc;
+use entity_access::domain::models::{
+ AccessLevel, Entity, EntityAccessReceipt, EntityPermission, EntityType,
+};
+use macro_authorization::{
+ InternalIdentityClaims, MacroAuthorizationError, MacroAuthorizationService,
+ MacroAuthorizationState,
+};
+use macro_db_migrator::MACRO_DB_MIGRATIONS;
+use macro_user_id::user_id::MacroUserIdStr;
+use macro_uuid::{Uuid, generate_uuid_v7};
+use model::user::UserContext;
+use rootcause::Report;
+use scheduled_action::{
+ domain::{
+ event_runs::{
+ AuthorizedEventRun, ClaimToken, ClaimedEventRun, CurrentOwnerAccess,
+ EventAccessCapability, EventExecutor, EventIngestion, EventIngestionResult,
+ EventRunKey, EventRunOutcome, EventRunRepository, FinalizationResult, PageSize,
+ PendingEventRun,
+ admission::EventAdmissionService,
+ dispatch::{DispatchResult, EventDispatchService, EventRunDispatch},
+ },
+ event_trigger::{
+ EventEntityType, EventId, EventPayload, EventReference, EventRejection, IncomingEvent,
+ },
+ models::{MAX_ACTION_TIME, ScheduledAction, ScheduledActionUpdate},
+ ports::{ScheduledActionLiveUpdate, ScheduledActionRepo, ScheduledAgentRunner},
+ service::ScheduledActionServiceImpl,
+ },
+ inbound::{
+ axum_router::{ScheduledActionRouterState, scheduled_action_router},
+ event_run_worker::run_event_worker,
+ },
+ outbound::{
+ inprocess_executor::InProcessExecutor, pg_event_run_repo::PgEventRunRepo,
+ pg_scheduled_action_repo::PgScheduledActionRepo,
+ },
+};
+use serde_json::{Value, json};
+use sqlx::PgPool;
+use tokio::sync::{Semaphore, mpsc};
+use tokio_util::{sync::CancellationToken, task::TaskTracker};
+use tower::ServiceExt;
+
+const USER: &str = "macro|[email protected]";
+const ACTOR: &str = "macro|[email protected]";
+const BOT: &str = "bot|01900000-0000-7000-8000-000000000004";
+const TEST_TIMEOUT: Duration = Duration::from_secs(10);
+
+fn page() -> PageSize {
+ PageSize::try_from(100).unwrap()
+}
+
+#[derive(Clone)]
+struct FakeAuth;
+impl MacroAuthorizationService for FakeAuth {
+ async fn authorize(&self, jwt: &str) -> Result<UserContext, Report<MacroAuthorizationError>> {
+ if jwt != "owner" {
+ return Err(Report::new(MacroAuthorizationError::InvalidCredentials));
+ }
+ Ok(UserContext {
+ user_id: USER.into(),
+ ..Default::default()
+ })
+ }
+ async fn authorize_internal(
+ &self,
+ _: &str,
+ _: InternalIdentityClaims,
+ ) -> Result<Option<UserContext>, Report<MacroAuthorizationError>> {
+ Err(Report::new(MacroAuthorizationError::InvalidCredentials))
+ }
+}
+
+struct FakeAccess;
+impl CurrentOwnerAccess for FakeAccess {
+ async fn authorize(
+ &self,
+ owner: &MacroUserIdStr<'static>,
+ event: &EventReference,
+ ) -> Result<Option<EventAccessCapability>, Report> {
+ // The event actor is intentionally different from the routine owner.
+ assert_eq!(owner.as_ref(), USER);
+ let entity = Entity {
+ entity_id: event.entity_id().to_string(),
+ entity_type: match event.entity_type() {
+ EventEntityType::Document => EntityType::Document,
+ EventEntityType::Channel => EntityType::Channel,
+ },
+ };
+ let access = match event.entity_type() {
+ EventEntityType::Document => EventAccessCapability::Document(
+ EntityAccessReceipt::try_new_authenticated_user(
+ owner.clone(),
+ entity,
+ EntityPermission::AccessLevel {
+ access_level: AccessLevel::View,
+ },
+ )
+ .unwrap(),
+ ),
+ EventEntityType::Channel => EventAccessCapability::Channel(
+ EntityAccessReceipt::try_new_authenticated_user(
+ owner.clone(),
+ entity,
+ EntityPermission::ChannelViewOnly,
+ )
+ .unwrap(),
+ ),
+ };
+ Ok(Some(access))
+ }
+}
+
+#[derive(Default)]
+struct FakeRunner {
+ calls: Mutex<Vec<(Uuid, Option<EventReference>)>>,
+ fail_event: Mutex<Option<EventId>>,
+ permits: Option<Semaphore>,
+ outputs: Mutex<Vec<IncomingEvent>>,
+ emit_bot_outputs: bool,
+}
+impl ScheduledAgentRunner for FakeRunner {
+ async fn create_chat(&self, _: &ScheduledAction) -> anyhow::Result<String> {
+ Ok(generate_uuid_v7().to_string())
+ }
+ async fn run(
+ &self,
+ action: &ScheduledAction,
+ _: &str,
+ event: Option<&EventReference>,
+ ) -> anyhow::Result<()> {
+ self.calls
+ .lock()
+ .unwrap()
+ .push((action.id.unwrap(), event.cloned()));
+ if let Some(permits) = &self.permits {
+ permits.acquire().await.unwrap().forget();
+ }
+ if self.emit_bot_outputs {
+ let mut outputs = self.outputs.lock().unwrap();
+ outputs.push(incoming("document.updated", BOT));
+ outputs.push(incoming("channel.message_posted", BOT));
+ }
+ if event.is_some_and(|event| Some(event.event_id()) == *self.fail_event.lock().unwrap()) {
+ anyhow::bail!("simulated agent failure after side effects");
+ }
+ Ok(())
+ }
+}
+
+struct NoLiveUpdates;
+impl ScheduledActionLiveUpdate for NoLiveUpdates {
+ async fn publish_update(&self, _: ScheduledActionUpdate) {}
+}
+
+type Executor = InProcessExecutor<PgScheduledActionRepo, NoLiveUpdates, FakeRunner>;
+type Admission = EventAdmissionService<PgEventRunRepo, FakeAccess>;
+type Dispatch = EventDispatchService<PgEventRunRepo, FakeAccess, Executor>;
+
+struct Harness {
+ pool: PgPool,
+ app: Router,
+ actions: Arc<PgScheduledActionRepo>,
+ runs: Arc<PgEventRunRepo>,
+ runner: Arc<FakeRunner>,
+ executor: Arc<Executor>,
+ tracker: TaskTracker,
+ // Keep the cron notification receiver alive; these tests drive dispatch explicitly.
+ _notifications: mpsc::Receiver<scheduled_action::domain::models::DispatchEvent>,
+}
+impl Harness {
+ async fn new(pool: PgPool, runner: FakeRunner) -> Self {
+ let user_id = generate_uuid_v7();
+ sqlx::query!(
+ "INSERT INTO macro_user (id, username, email, stripe_customer_id) VALUES ($1, $2, $2, $2) ON CONFLICT DO NOTHING",
+ user_id, USER,
+ ).execute(&pool).await.unwrap();
+ sqlx::query!(
+ r#"INSERT INTO "User" (id, email, macro_user_id)
+ SELECT $1, $1, id FROM macro_user WHERE email = $1 ON CONFLICT DO NOTHING"#,
+ USER,
+ )
+ .execute(&pool)
+ .await
+ .unwrap();
+ let actions = Arc::new(PgScheduledActionRepo::new(pool.clone()));
+ let runs = Arc::new(PgEventRunRepo::new(pool.clone()));
+ let runner = Arc::new(runner);
+ let tracker = TaskTracker::new();
+ let executor = Arc::new(InProcessExecutor::new(
+ actions.clone(),
+ runner.clone(),
+ Arc::new(NoLiveUpdates),
+ tracker.clone(),
+ CancellationToken::new(),
+ ));
+ let (tx, notifications) = mpsc::channel(100);
+ let service = ScheduledActionServiceImpl::new(actions.clone(), executor.clone(), tx)
+ .with_event_management_enabled(true);
+ let app = scheduled_action_router(ScheduledActionRouterState {
+ service: Arc::new(service),
+ authorization_state: MacroAuthorizationState::new(Arc::new(FakeAuth)),
+ });
+ Self {
+ pool,
+ app,
+ actions,
+ runs,
+ runner,
+ executor,
+ tracker,
+ _notifications: notifications,
+ }
+ }
+ fn admission(&self) -> Admission {
+ // Fresh adapters model restarted intake using only durable state.
+ EventAdmissionService::new(
+ Arc::new(PgEventRunRepo::new(self.pool.clone())),
+ Arc::new(FakeAccess),
+ PageSize::try_from(1).unwrap(),
+ )
+ }
+ fn dispatch(&self) -> Dispatch {
+ EventDispatchService::new(
+ Arc::new(PgEventRunRepo::new(self.pool.clone())),
+ Arc::new(FakeAccess),
+ self.executor.clone(),
+ )
+ }
+ async fn create(&self, body: Value) -> Value {
+ let (status, action) = request(&self.app, "POST", "/scheduled-actions", body).await;
+ assert_eq!(status, StatusCode::CREATED, "{action}");
+ // UUIDv7 publication times have millisecond precision, while API activation
+ // uses PostgreSQL timestamps. Publish strictly after the activation tick.
+ let activation = Utc::now().timestamp_millis();
+ while Utc::now().timestamp_millis() <= activation {
+ tokio::time::sleep(Duration::from_millis(1)).await;
+ }
+ action
+ }
+ async fn ingest(&self, event: &IncomingEvent, inserted: u64) {
+ assert_eq!(
+ self.admission().ingest(event).await.unwrap(),
+ EventIngestionResult::Admitted { inserted }
+ );
+ }
+ async fn head(&self) -> PendingEventRun {
+ self.runs.pending_runs(page()).await.unwrap().remove(0)
+ }
+ async fn dispatch_head(&self) {
+ assert_eq!(
+ self.dispatch()
+ .dispatch(self.head().await, pending())
+ .await
+ .unwrap(),
+ DispatchResult::Finished(FinalizationResult::Finalized)
+ );
+ }
+ async fn count(&self) -> i64 {
+ sqlx::query_scalar!("SELECT count(*) FROM scheduled_action_event_run")
+ .fetch_one(&self.pool)
+ .await
+ .unwrap()
+ .unwrap()
+ }
+ async fn state(&self, key: EventRunKey) -> (String, Option<Value>, Option<Uuid>) {
+ let row = sqlx::query!(
+ "SELECT state, outcome, execution_record_id FROM scheduled_action_event_run WHERE action_id = $1 AND event_id = $2",
+ key.action_id, key.event_id.as_uuid(),
+ ).fetch_one(&self.pool).await.unwrap();
+ (row.state, row.outcome, row.execution_record_id)
+ }
+ async fn assert_terminal(&self, key: EventRunKey, outcome: EventRunOutcome, history: bool) {
+ let (state, stored, record) = self.state(key).await;
+ assert_eq!(state, "finished");
+ assert_eq!(stored, Some(serde_json::to_value(outcome).unwrap()));
+ assert_eq!(record.is_some(), history);
+ }
+ async fn claim_head(&self) -> ClaimedEventRun {
+ let head = self.head().await;
+ let configuration = self
+ .runs
+ .current_configuration(head.action_id)
+ .await
+ .unwrap()
+ .unwrap();
+ let access = FakeAccess
+ .authorize(configuration.owner.as_user().unwrap(), &head.event)
+ .await
+ .unwrap()
+ .unwrap();
+ let authorized = AuthorizedEventRun::prepare(head, &configuration, access).unwrap();
+ let now = Utc::now();
+ self.runs
+ .claim(
+ authorized,
+ ClaimToken::generate(),
+ now,
+ now + MAX_ACTION_TIME,
+ )
+ .await
+ .unwrap()
+ .unwrap()
+ }
+ fn event_calls(&self) -> Vec<EventRunKey> {
+ self.runner
+ .calls
+ .lock()
+ .unwrap()
+ .iter()
+ .map(|(action_id, event)| EventRunKey {
+ action_id: *action_id,
+ event_id: event.as_ref().unwrap().event_id(),
+ })
+ .collect()
+ }
+}
+
+async fn request(app: &Router, method: &str, uri: &str, body: Value) -> (StatusCode, Value) {
+ let response = app
+ .clone()
+ .oneshot(
+ Request::builder()
+ .method(method)
+ .uri(uri)
+ .header(header::AUTHORIZATION, "Bearer owner")
+ .header(header::CONTENT_TYPE, "application/json")
+ .body(Body::from(body.to_string()))
+ .unwrap(),
+ )
+ .await
+ .unwrap();
+ let status = response.status();
+ let bytes = to_bytes(response.into_body(), usize::MAX).await.unwrap();
+ (
+ status,
+ serde_json::from_slice(&bytes).unwrap_or(Value::Null),
+ )
+}
+fn legacy() -> Value {
+ json!({"name":"legacy", "kind":"Agent", "schedule":"0 0 9 * * *", "timezone":"UTC", "task":{}, "enabled":true})
+}
+fn event_action() -> Value {
+ json!({"name":"events", "kind":"Agent", "trigger":{"type":"events", "filters":[{"events":["document.updated", "channel.message_posted"]}]}, "task":{}, "enabled":true})
+}
+fn action_id(action: &Value) -> Uuid {
+ action["id"].as_str().unwrap().parse().unwrap()
+}
+fn key(action_id: Uuid, event: &IncomingEvent) -> EventRunKey {
+ EventRunKey {
+ action_id,
+ event_id: EventId::try_from(event.event_id).unwrap(),
+ }
+}
+fn incoming(name: &str, actor: &str) -> IncomingEvent {
+ let envelope = json!({"event_type":name, "metadata":{
+ "document_id":generate_uuid_v7(), "owner":USER, "actor":actor,
+ "actor_user_id":ACTOR, "share_permission_updated":false,
+ "channel_id":generate_uuid_v7(), "message_id":generate_uuid_v7(),
+ "sender":actor, "channel_type":"public", "content":"simulated tool output", "mentions":[], "attachments":[],
+ "created_at":Utc::now(), "updated_at":Utc::now()
+ }});
+ IncomingEvent {
+ event_id: generate_uuid_v7(),
+ schema_version: 1,
+ payload: if name.starts_with("document.") {
+ EventPayload::Document(serde_json::from_value(envelope).unwrap())
+ } else {
+ EventPayload::Channel(serde_json::from_value(envelope).unwrap())
+ },
+ }
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn legacy_cron_and_canonical_events_share_crud_and_manual_execution(pool: PgPool) {
+ let h = Harness::new(pool, FakeRunner::default()).await;
+ let cron = h.create(legacy()).await;
+ let events = h.create(event_action()).await;
+ assert_eq!(cron["schedule"], cron["trigger"]["schedule"]);
+ assert!(events.get("schedule").is_none());
+ assert!(events["next_run_at"].is_null());
+ let (status, list) = request(&h.app, "GET", "/scheduled-actions", Value::Null).await;
+ assert_eq!(status, StatusCode::OK);
+ assert_eq!(list.as_array().unwrap().len(), 1);
+ assert_eq!(list[0]["id"], cron["id"]);
+ let (_, all) = request(
+ &h.app,
+ "GET",
+ "/scheduled-actions?include_events=true",
+ Value::Null,
+ )
+ .await;
+ assert_eq!(all.as_array().unwrap().len(), 2);
+
+ for (created, mut input) in [(cron, legacy()), (events, event_action())] {
+ let id = action_id(&created);
+ let url = format!("/scheduled-actions/{id}");
+ input["name"] = json!("updated");
+ let (status, updated) = request(&h.app, "PUT", &url, input).await;
+ assert_eq!(status, StatusCode::OK);
+ assert_eq!(updated["configuration_revision"], 2);
+ assert_eq!(updated["name"], "updated");
+ let (status, execution) =
+ request(&h.app, "POST", &format!("{url}/execute"), Value::Null).await;
+ assert_eq!(status, StatusCode::OK);
+ h.tracker.close();
+ tokio::time::timeout(TEST_TIMEOUT, h.tracker.wait())
+ .await
+ .unwrap();
+ let (status, history) =
+ request(&h.app, "GET", &format!("{url}/history"), Value::Null).await;
+ assert_eq!(status, StatusCode::OK);
+ assert_eq!(history.as_array().unwrap().len(), 1);
+ assert_eq!(history[0]["resource_id"], execution["chat_id"]);
+ assert_eq!(history[0]["is_success"], true);
+ assert_eq!(
+ request(&h.app, "DELETE", &url, Value::Null).await.0,
+ StatusCode::NO_CONTENT
+ );
+ assert!(
+ h.actions
+ .get_action(&id, MacroUserIdStr::parse_from_str(USER).unwrap())
+ .await
+ .unwrap()
+ .is_none()
+ );
+ }
+ assert_eq!(h.count().await, 0);
+ let calls = h.runner.calls.lock().unwrap();
+ assert_eq!(calls.len(), 2);
+ assert!(calls.iter().all(|(_, context)| context.is_none()));
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn two_worker_replicas_queue_two_arrivals_while_active_without_rerunning_failure(
+ pool: PgPool,
+) {
+ let h = Harness::new(
+ pool,
+ FakeRunner {
+ permits: Some(Semaphore::new(0)),
+ ..Default::default()
+ },
+ )
+ .await;
+ let id = action_id(&h.create(event_action()).await);
+ let first = incoming("document.updated", ACTOR);
+ *h.runner.fail_event.lock().unwrap() = Some(key(id, &first).event_id);
+ h.ingest(&first, 1).await;
+ let shutdown = CancellationToken::new();
+ let executions = TaskTracker::new();
+ let mut workers = Vec::new();
+ for _ in 0..2 {
+ workers.push(tokio::spawn(run_event_worker(
+ h.dispatch(),
+ shutdown.clone(),
+ executions.clone(),
+ PageSize::try_from(2).unwrap(),
+ )));
+ }
+ tokio::time::timeout(TEST_TIMEOUT, async {
+ while h.event_calls().is_empty() {
+ tokio::time::sleep(Duration::from_millis(10)).await;
+ }
+ })
+ .await
+ .unwrap();
+ let second = incoming("document.updated", ACTOR);
+ let third = incoming("document.updated", ACTOR);
+ h.ingest(&second, 1).await;
+ h.ingest(&third, 1).await;
+ h.ingest(&second, 0).await;
+ h.ingest(&first, 0).await;
+ assert_eq!(h.count().await, 3);
+ assert_eq!(h.event_calls(), vec![key(id, &first)]);
+ assert!(h.runs.pending_runs(page()).await.unwrap().is_empty());
+ assert_eq!(h.state(key(id, &second)).await.0, "pending");
+ assert_eq!(h.state(key(id, &third)).await.0, "pending");
+ h.runner.permits.as_ref().unwrap().add_permits(3);
+ tokio::time::timeout(TEST_TIMEOUT, async {
+ while h.actions.get_execution_records(&id).await.unwrap().len() < 3 {
+ tokio::time::sleep(Duration::from_millis(10)).await;
+ }
+ })
+ .await
+ .unwrap();
+ shutdown.cancel();
+ for worker in workers {
+ tokio::time::timeout(TEST_TIMEOUT, worker)
+ .await
+ .unwrap()
+ .unwrap();
+ }
+ executions.close();
+ tokio::time::timeout(TEST_TIMEOUT, executions.wait())
+ .await
+ .unwrap();
+ assert_eq!(
+ h.event_calls(),
+ vec![key(id, &first), key(id, &second), key(id, &third)]
+ );
+ h.assert_terminal(key(id, &first), EventRunOutcome::Failed, true)
+ .await;
+ for event in [&second, &third] {
+ h.assert_terminal(key(id, event), EventRunOutcome::Succeeded, true)
+ .await;
+ }
+ h.ingest(&first, 0).await;
+ assert!(h.dispatch().pending(page()).await.unwrap().is_empty());
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn overlapping_filters_and_multiple_routines_deduplicate_concurrent_redelivery(pool: PgPool) {
+ let h = Harness::new(pool, FakeRunner::default()).await;
+ let mut input = event_action();
+ input["trigger"]["filters"]
+ .as_array_mut()
+ .unwrap()
+ .push(json!({"events":["document.updated"]}));
+ let a = action_id(&h.create(input.clone()).await);
+ let b = action_id(&h.create(input).await);
+ let event = incoming("document.updated", ACTOR);
+ let intake_a = h.admission();
+ let intake_b = h.admission();
+ let (left, right) = tokio::join!(intake_a.ingest(&event), intake_b.ingest(&event));
+ let inserted = [left.unwrap(), right.unwrap()]
+ .into_iter()
+ .map(|result| match result {
+ EventIngestionResult::Admitted { inserted } => inserted,
+ other => panic!("unexpected rejection: {other:?}"),
+ })
+ .sum::<u64>();
+ assert_eq!(inserted, 2);
+ assert_eq!(h.count().await, 2);
+ let heads = h.runs.pending_runs(page()).await.unwrap();
+ assert_eq!(heads.len(), 2);
+ let worker_a = h.dispatch();
+ let worker_b = h.dispatch();
+ let (left, right) = tokio::join!(
+ worker_a.dispatch(heads[0].clone(), pending()),
+ worker_b.dispatch(heads[0].clone(), pending())
+ );
+ let results = [left.unwrap(), right.unwrap()];
+ assert!(results.contains(&DispatchResult::Finished(FinalizationResult::Finalized)));
+ assert!(results.contains(&DispatchResult::NotStarted));
+ h.dispatch_head().await;
+ h.ingest(&event, 0).await;
+ for id in [a, b] {
+ h.assert_terminal(key(id, &event), EventRunOutcome::Succeeded, true)
+ .await;
+ assert_eq!(
+ h.event_calls()
+ .iter()
+ .filter(|run| run.action_id == id)
+ .count(),
+ 1
+ );
+ }
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn crash_before_admission_commit_and_after_admission_before_offset_commit(pool: PgPool) {
+ let h = Harness::new(pool, FakeRunner::default()).await;
+ let id = action_id(&h.create(event_action()).await);
+ let event = incoming("document.updated", ACTOR);
+ let mut lock = h.pool.begin().await.unwrap();
+ sqlx::query_scalar!(
+ "SELECT id FROM scheduled_action WHERE id = $1 FOR UPDATE SKIP LOCKED",
+ id
+ )
+ .fetch_one(&mut *lock)
+ .await
+ .unwrap();
+ // Hold the admission transaction before it can insert/commit, then drop the
+ // intake future as a process crash would. No acknowledgment is simulated.
+ assert!(
+ tokio::time::timeout(Duration::from_millis(100), h.admission().ingest(&event))
+ .await
+ .is_err()
+ );
+ assert_eq!(h.count().await, 0);
+ lock.rollback().await.unwrap();
+ h.ingest(&event, 1).await;
+ // Crash after durable admission but before Kafka offset commit: a fresh
+ // intake instance receives the identical event ID and must insert nothing.
+ h.ingest(&event, 0).await;
+ assert_eq!(h.count().await, 1);
+ h.dispatch_head().await;
+ h.assert_terminal(key(id, &event), EventRunOutcome::Succeeded, true)
+ .await;
+ assert_eq!(h.event_calls(), vec![key(id, &event)]);
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn crashes_after_start_and_after_side_effects_are_interrupted_without_reexecution(
+ pool: PgPool,
+) {
+ let h = Harness::new(
+ pool,
+ FakeRunner {
+ emit_bot_outputs: true,
+ ..Default::default()
+ },
+ )
+ .await;
+ let id = action_id(&h.create(event_action()).await);
+ let before_runner = incoming("document.updated", ACTOR);
+ let after_runner = incoming("document.updated", ACTOR);
+ let later = incoming("document.updated", ACTOR);
+ for event in [&before_runner, &after_runner, &later] {
+ h.ingest(event, 1).await;
+ }
+
+ let claimed = h.claim_head().await;
+ let deadline = claimed.deadline;
+ drop(claimed); // Started committed, but runner was never invoked.
+ assert!(h.event_calls().is_empty());
+ assert!(h.runner.outputs.lock().unwrap().is_empty());
+ assert!(h.dispatch().pending(page()).await.unwrap().is_empty());
+ assert_eq!(h.runs.reconcile(deadline, page()).await.unwrap(), 1);
+ h.assert_terminal(key(id, &before_runner), EventRunOutcome::Interrupted, false)
+ .await;
+ h.ingest(&before_runner, 0).await;
+
+ let claimed = h.claim_head().await;
+ assert_eq!(claimed.run.pending.key(), key(id, &after_runner));
+ let execution = h.executor.execute(&claimed, pending()).await;
+ assert_eq!(execution.outcome, EventRunOutcome::Succeeded);
+ assert_eq!(h.runner.outputs.lock().unwrap().len(), 2);
+ let deadline = claimed.deadline;
+ drop(execution); // Side effect happened; terminal transaction never ran.
+ drop(claimed);
+ assert_eq!(h.state(key(id, &after_runner)).await.0, "started");
+ assert!(
+ h.actions
+ .get_execution_records(&id)
+ .await
+ .unwrap()
+ .is_empty()
+ );
+ assert!(h.dispatch().pending(page()).await.unwrap().is_empty());
+ assert_eq!(h.runs.reconcile(deadline, page()).await.unwrap(), 1);
+ assert_eq!(h.runs.reconcile(deadline, page()).await.unwrap(), 0);
+ h.assert_terminal(key(id, &after_runner), EventRunOutcome::Interrupted, false)
+ .await;
+ h.ingest(&after_runner, 0).await;
+
+ let stale_pending = h.head().await;
+ h.dispatch_head().await;
+ h.assert_terminal(key(id, &later), EventRunOutcome::Succeeded, true)
+ .await;
+ // Restart after terminal finalization: both redelivery and a stale worker's
+ // pending snapshot must leave the already-finalized execution alone.
+ h.ingest(&later, 0).await;
+ assert_eq!(
+ h.dispatch()
+ .dispatch(stale_pending, pending())
+ .await
+ .unwrap(),
+ DispatchResult::NotStarted
+ );
+ assert_eq!(
+ h.event_calls(),
+ vec![key(id, &after_runner), key(id, &later)]
+ );
+ assert_eq!(h.actions.get_execution_records(&id).await.unwrap().len(), 1);
+ assert_eq!(h.runner.outputs.lock().unwrap().len(), 4);
+ assert_eq!(h.count().await, 3);
+ assert!(h.dispatch().pending(page()).await.unwrap().is_empty());
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn simulated_bot_document_and_channel_outputs_cannot_start_a_routine_loop(pool: PgPool) {
+ let h = Harness::new(
+ pool,
+ FakeRunner {
+ emit_bot_outputs: true,
+ ..Default::default()
+ },
+ )
+ .await;
+ h.create(event_action()).await;
+ let event = incoming("channel.message_posted", ACTOR);
+ h.ingest(&event, 1).await;
+ h.dispatch_head().await;
+ let outputs = std::mem::take(&mut *h.runner.outputs.lock().unwrap());
+ assert_eq!(outputs.len(), 2);
+ for output in outputs {
+ // Human ownership/legacy actor metadata must not override explicit bot
+ // authorship. Deliver each output twice, as Kafka may do on recovery.
+ for _ in 0..2 {
+ assert_eq!(
+ h.admission().ingest(&output).await.unwrap(),
+ EventIngestionResult::Rejected(EventRejection::UnsafeAttribution)
+ );
+ }
+ }
+ assert_eq!(h.count().await, 1);
+ assert_eq!(h.event_calls().len(), 1);
+ assert!(h.dispatch().pending(page()).await.unwrap().is_empty());
+}