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.

fix(email): keep follow-ups, calendar changes and key mail in Focus (#9517)

teoa72231ca165f
diff --git a/.sqlx/query-07fb7795589565482a7a5637f3a6bc7abecd4b6e5caf4bf48088db8c276a87e3.json b/.sqlx/query-07fb7795589565482a7a5637f3a6bc7abecd4b6e5caf4bf48088db8c276a87e3.json
new file mode 100644
index 0000000000..6008f921d9
--- /dev/null
+++ b/.sqlx/query-07fb7795589565482a7a5637f3a6bc7abecd4b6e5caf4bf48088db8c276a87e3.json
@@ -0,0 +1,29 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n INSERT INTO email_thread_focus (\n thread_id, link_id, classified_message_id, classified_message_ts, is_focus,\n category, importance, needs_reply, needs_follow_up, scores, model, classified_at,\n rule_version, pinned, awaiting_reply, calendar_hold_until\n )\n VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16)\n ON CONFLICT (thread_id) DO UPDATE SET\n link_id = EXCLUDED.link_id,\n classified_message_id = EXCLUDED.classified_message_id,\n classified_message_ts = EXCLUDED.classified_message_ts,\n is_focus = EXCLUDED.is_focus,\n category = EXCLUDED.category,\n importance = EXCLUDED.importance,\n needs_reply = EXCLUDED.needs_reply,\n needs_follow_up = EXCLUDED.needs_follow_up,\n scores = EXCLUDED.scores,\n model = EXCLUDED.model,\n classified_at = EXCLUDED.classified_at,\n rule_version = EXCLUDED.rule_version,\n pinned = EXCLUDED.pinned,\n awaiting_reply = EXCLUDED.awaiting_reply,\n calendar_hold_until = EXCLUDED.calendar_hold_until\n WHERE email_thread_focus.classified_message_ts <= EXCLUDED.classified_message_ts\n ",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Uuid",
+ "Uuid",
+ "Timestamptz",
+ "Bool",
+ "Text",
+ "Int2",
+ "Bool",
+ "Bool",
+ "Jsonb",
+ "Text",
+ "Timestamptz",
+ "Int2",
+ "Bool",
+ "Bool",
+ "Timestamptz"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "07fb7795589565482a7a5637f3a6bc7abecd4b6e5caf4bf48088db8c276a87e3"
+}
diff --git a/.sqlx/query-0b75f58158446630c709f49d5c06165a33d78f048daaf4b5d51e50d0c5b1258f.json b/.sqlx/query-0b75f58158446630c709f49d5c06165a33d78f048daaf4b5d51e50d0c5b1258f.json
new file mode 100644
index 0000000000..51db466273
--- /dev/null
+++ b/.sqlx/query-0b75f58158446630c709f49d5c06165a33d78f048daaf4b5d51e50d0c5b1258f.json
@@ -0,0 +1,73 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n SELECT f.thread_id, f.category, f.importance, f.classified_at,\n now_state.earned OR now_state.waiting OR now_state.calendar_held\n AS \"is_focus!\",\n (now_state.earned OR now_state.calendar_held)\n AND (f.needs_reply OR f.needs_follow_up)\n AND NOT now_state.owner_latest\n AS \"needs_reply!\",\n ((now_state.earned OR now_state.calendar_held)\n AND f.needs_follow_up\n AND now_state.owner_latest)\n OR now_state.waiting\n AS \"needs_follow_up!\",\n now_state.waiting AS \"waiting!\",\n now_state.calendar_held AS \"calendar_held!\"\n FROM email_thread_focus f\n JOIN email_threads t ON t.id = f.thread_id AND t.link_id = f.link_id\n LEFT JOIN LATERAL (\n SELECT COALESCE(m.internal_date_ts, m.created_at) AS at, m.is_sent\n FROM email_messages m\n WHERE m.thread_id = t.id\n AND NOT m.is_draft\n AND EXISTS (\n SELECT 1 FROM email_message_mailbox_facts facts\n WHERE facts.id = m.id AND facts.is_present AND NOT facts.in_trash\n )\n ORDER BY COALESCE(m.internal_date_ts, m.created_at) DESC, m.id DESC\n LIMIT 1\n ) latest ON TRUE\n CROSS JOIN LATERAL (\n SELECT COALESCE(latest.is_sent, false) AS owner_latest,\n f.is_focus AND (t.is_signal OR f.pinned) AS earned,\n f.awaiting_reply\n AND t.inbox_visible\n AND COALESCE(latest.is_sent AND latest.at <= $3, false) AS waiting,\n t.inbox_visible\n AND COALESCE(\n f.calendar_hold_until > $4\n AND latest.at <= f.classified_message_ts,\n false\n ) AS calendar_held\n ) now_state\n WHERE f.thread_id = ANY($1)\n AND f.link_id IN (\n SELECT el.id FROM email_links el WHERE el.macro_id = $2\n UNION\n SELECT mul.link_id FROM macro_user_links mul WHERE mul.primary_macro_id = $2\n )\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "thread_id",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 1,
+ "name": "category",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 2,
+ "name": "importance",
+ "type_info": "Int2"
+ },
+ {
+ "ordinal": 3,
+ "name": "classified_at",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 4,
+ "name": "is_focus!",
+ "type_info": "Bool"
+ },
+ {
+ "ordinal": 5,
+ "name": "needs_reply!",
+ "type_info": "Bool"
+ },
+ {
+ "ordinal": 6,
+ "name": "needs_follow_up!",
+ "type_info": "Bool"
+ },
+ {
+ "ordinal": 7,
+ "name": "waiting!",
+ "type_info": "Bool"
+ },
+ {
+ "ordinal": 8,
+ "name": "calendar_held!",
+ "type_info": "Bool"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "UuidArray",
+ "Text",
+ "Timestamptz",
+ "Timestamptz"
+ ]
+ },
+ "nullable": [
+ false,
+ false,
+ false,
+ false,
+ null,
+ null,
+ null,
+ null,
+ null
+ ]
+ },
+ "hash": "0b75f58158446630c709f49d5c06165a33d78f048daaf4b5d51e50d0c5b1258f"
+}
diff --git a/.sqlx/query-4ea7cf38da5f6527f03cb85e1aea923bffb49d9e90eedf1d7014cb11f2cdfe5b.json b/.sqlx/query-4ea7cf38da5f6527f03cb85e1aea923bffb49d9e90eedf1d7014cb11f2cdfe5b.json
new file mode 100644
index 0000000000..74029d3916
--- /dev/null
+++ b/.sqlx/query-4ea7cf38da5f6527f03cb85e1aea923bffb49d9e90eedf1d7014cb11f2cdfe5b.json
@@ -0,0 +1,26 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n SELECT f.thread_id\n FROM email_thread_focus f\n JOIN email_threads t ON t.id = f.thread_id AND t.link_id = f.link_id\n -- Who wrote last comes from the mail itself: editing a draft moves\n -- the thread's own timestamps. An indexed per-thread top-one probe.\n CROSS JOIN LATERAL (\n SELECT COALESCE(m.internal_date_ts, m.created_at) AS at, m.is_sent\n FROM email_messages m\n WHERE m.thread_id = t.id\n AND NOT m.is_draft\n AND EXISTS (\n SELECT 1 FROM email_message_mailbox_facts facts\n WHERE facts.id = m.id AND facts.is_present AND NOT facts.in_trash\n )\n ORDER BY COALESCE(m.internal_date_ts, m.created_at) DESC, m.id DESC\n LIMIT 1\n ) latest\n WHERE f.link_id IN (\n -- The owner's inboxes, as Soup lists them: their own and linked ones.\n SELECT el.id FROM email_links el WHERE el.macro_id = $1\n UNION\n SELECT mul.link_id FROM macro_user_links mul WHERE mul.primary_macro_id = $1\n )\n -- Every way in, indexed by email_thread_focus_list_idx and\n -- email_thread_focus_read_time_idx.\n AND (f.is_focus OR f.awaiting_reply OR f.calendar_hold_until IS NOT NULL)\n AND t.inbox_visible\n -- Never earlier than the latest mail, so this spares most probes.\n AND GREATEST(t.latest_inbound_message_ts, t.latest_outbound_message_ts) >= $2\n AND latest.at >= $2\n AND (\n (f.is_focus AND (t.is_signal OR f.pinned))\n OR (\n -- Nothing reclassifies a thread as it ages, so the wait is\n -- measured here, and a newer reply ends it before the worker\n -- gets to the thread.\n f.awaiting_reply\n AND latest.is_sent\n AND latest.at <= $3\n )\n -- A calendar change holds a thread until its event is over, and\n -- a newer message ends that before the worker gets to the thread.\n OR (f.calendar_hold_until > $4 AND latest.at <= f.classified_message_ts)\n )\n -- Newest first, so the limit keeps the newest threads.\n ORDER BY latest.at DESC, f.thread_id\n LIMIT $5\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "thread_id",
+ "type_info": "Uuid"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Text",
+ "Timestamptz",
+ "Timestamptz",
+ "Timestamptz",
+ "Int8"
+ ]
+ },
+ "nullable": [
+ false
+ ]
+ },
+ "hash": "4ea7cf38da5f6527f03cb85e1aea923bffb49d9e90eedf1d7014cb11f2cdfe5b"
+}
diff --git a/.sqlx/query-5d67f3eb1f40039f8c892b01dfcdf48969c3943748d447b14b5ac1e497a1d1a9.json b/.sqlx/query-5d67f3eb1f40039f8c892b01dfcdf48969c3943748d447b14b5ac1e497a1d1a9.json
deleted file mode 100644
index dacbb26585..0000000000
--- a/.sqlx/query-5d67f3eb1f40039f8c892b01dfcdf48969c3943748d447b14b5ac1e497a1d1a9.json
+++ /dev/null
@@ -1,30 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n SELECT t.id,\n GREATEST(t.latest_inbound_message_ts, t.latest_outbound_message_ts)\n AS \"latest_at!\"\n FROM email_links el\n JOIN email_threads t ON t.link_id = el.id\n LEFT JOIN email_thread_focus f ON f.thread_id = t.id\n WHERE LOWER(SPLIT_PART(el.email_address, '@', 2)) = ANY($2)\n AND t.inbox_visible\n AND t.is_signal\n AND t.latest_inbound_message_ts IS NOT NULL\n AND t.latest_inbound_message_ts >= $1\n AND (\n f.thread_id IS NULL\n OR f.classified_message_ts\n < GREATEST(t.latest_inbound_message_ts, t.latest_outbound_message_ts)\n )\n ORDER BY t.latest_inbound_message_ts DESC\n LIMIT $3\n ",
- "describe": {
- "columns": [
- {
- "ordinal": 0,
- "name": "id",
- "type_info": "Uuid"
- },
- {
- "ordinal": 1,
- "name": "latest_at!",
- "type_info": "Timestamptz"
- }
- ],
- "parameters": {
- "Left": [
- "Timestamptz",
- "TextArray",
- "Int8"
- ]
- },
- "nullable": [
- false,
- null
- ]
- },
- "hash": "5d67f3eb1f40039f8c892b01dfcdf48969c3943748d447b14b5ac1e497a1d1a9"
-}
diff --git a/.sqlx/query-65114771d7f78f812488196eba0b96b325ac8c8fe9eafb3bba2c58a0f75c2917.json b/.sqlx/query-65114771d7f78f812488196eba0b96b325ac8c8fe9eafb3bba2c58a0f75c2917.json
deleted file mode 100644
index aa4bbfae6b..0000000000
--- a/.sqlx/query-65114771d7f78f812488196eba0b96b325ac8c8fe9eafb3bba2c58a0f75c2917.json
+++ /dev/null
@@ -1,24 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n SELECT f.thread_id\n FROM email_thread_focus f\n -- Joining on the inbox too lets the planner walk each inbox's recent\n -- signal threads (idx_email_threads_signal_view) instead of every\n -- Focus row the inbox ever had.\n JOIN email_threads t ON t.id = f.thread_id AND t.link_id = f.link_id\n WHERE f.link_id IN (\n -- The owner's inboxes, as Soup lists them: their own and linked ones.\n SELECT el.id FROM email_links el WHERE el.macro_id = $1\n UNION\n SELECT mul.link_id FROM macro_user_links mul WHERE mul.primary_macro_id = $1\n )\n AND f.is_focus\n AND t.inbox_visible\n AND t.is_signal\n AND t.latest_inbound_message_ts >= $2\n -- Priority: importance, halved for every 2 days (172800 s) since the\n -- latest inbound mail. As a sum of logs the key doesn't depend on the\n -- current time, so the order only changes when new mail arrives.\n ORDER BY log(2.0, GREATEST(f.importance, 1))\n + extract(epoch FROM t.latest_inbound_message_ts) / 172800 DESC,\n t.latest_inbound_message_ts DESC,\n f.thread_id\n LIMIT $3\n ",
- "describe": {
- "columns": [
- {
- "ordinal": 0,
- "name": "thread_id",
- "type_info": "Uuid"
- }
- ],
- "parameters": {
- "Left": [
- "Text",
- "Timestamptz",
- "Int8"
- ]
- },
- "nullable": [
- false
- ]
- },
- "hash": "65114771d7f78f812488196eba0b96b325ac8c8fe9eafb3bba2c58a0f75c2917"
-}
diff --git a/.sqlx/query-1cf488babaabb9e301eb886c49df39d9fa77a17de10005d6991a9d9e2e23f07e.json b/.sqlx/query-6ae01ede4239a6d05e70b1efd6d912e1ed14a6c575267ad6607bfe654eb3cc20.json
similarity index 57%
rename from .sqlx/query-1cf488babaabb9e301eb886c49df39d9fa77a17de10005d6991a9d9e2e23f07e.json
rename to .sqlx/query-6ae01ede4239a6d05e70b1efd6d912e1ed14a6c575267ad6607bfe654eb3cc20.json
index 495800c714..680cc98a6d 100644
--- a/.sqlx/query-1cf488babaabb9e301eb886c49df39d9fa77a17de10005d6991a9d9e2e23f07e.json
+++ b/.sqlx/query-6ae01ede4239a6d05e70b1efd6d912e1ed14a6c575267ad6607bfe654eb3cc20.json
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
- "query": "\n SELECT t.id, t.link_id, t.is_signal, t.inbox_visible, el.macro_id, el.email_address,\n f.classified_message_id AS \"classified_message_id?\"\n FROM email_threads t\n JOIN email_links el ON el.id = t.link_id\n LEFT JOIN email_thread_focus f ON f.thread_id = t.id\n WHERE t.id = $1\n ",
+ "query": "\n SELECT t.id, t.link_id, t.is_signal, t.inbox_visible, el.macro_id, el.email_address,\n f.classified_message_id AS \"classified_message_id?\",\n f.rule_version AS \"rule_version?\",\n f.scores AS \"scores?\",\n f.model AS \"model?\"\n FROM email_threads t\n JOIN email_links el ON el.id = t.link_id\n LEFT JOIN email_thread_focus f ON f.thread_id = t.id\n WHERE t.id = $1\n ",
"describe": {
"columns": [
{
@@ -37,6 +37,21 @@
"ordinal": 6,
"name": "classified_message_id?",
"type_info": "Uuid"
+ },
+ {
+ "ordinal": 7,
+ "name": "rule_version?",
+ "type_info": "Int2"
+ },
+ {
+ "ordinal": 8,
+ "name": "scores?",
+ "type_info": "Jsonb"
+ },
+ {
+ "ordinal": 9,
+ "name": "model?",
+ "type_info": "Text"
}
],
"parameters": {
@@ -51,8 +66,11 @@
false,
false,
false,
+ false,
+ false,
+ false,
false
]
},
- "hash": "1cf488babaabb9e301eb886c49df39d9fa77a17de10005d6991a9d9e2e23f07e"
+ "hash": "6ae01ede4239a6d05e70b1efd6d912e1ed14a6c575267ad6607bfe654eb3cc20"
}
diff --git a/.sqlx/query-862dc37caafc34b0e704a54a782f2d6d4b949f78a45d3c0bda01c63e1842f5c1.json b/.sqlx/query-862dc37caafc34b0e704a54a782f2d6d4b949f78a45d3c0bda01c63e1842f5c1.json
deleted file mode 100644
index 911880b907..0000000000
--- a/.sqlx/query-862dc37caafc34b0e704a54a782f2d6d4b949f78a45d3c0bda01c63e1842f5c1.json
+++ /dev/null
@@ -1,25 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n INSERT INTO email_thread_focus (\n thread_id, link_id, classified_message_id, classified_message_ts, is_focus,\n category, importance, needs_reply, needs_follow_up, scores, model, classified_at\n )\n VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)\n ON CONFLICT (thread_id) DO UPDATE SET\n link_id = EXCLUDED.link_id,\n classified_message_id = EXCLUDED.classified_message_id,\n classified_message_ts = EXCLUDED.classified_message_ts,\n is_focus = EXCLUDED.is_focus,\n category = EXCLUDED.category,\n importance = EXCLUDED.importance,\n needs_reply = EXCLUDED.needs_reply,\n needs_follow_up = EXCLUDED.needs_follow_up,\n scores = EXCLUDED.scores,\n model = EXCLUDED.model,\n classified_at = EXCLUDED.classified_at\n WHERE email_thread_focus.classified_message_ts <= EXCLUDED.classified_message_ts\n ",
- "describe": {
- "columns": [],
- "parameters": {
- "Left": [
- "Uuid",
- "Uuid",
- "Uuid",
- "Timestamptz",
- "Bool",
- "Text",
- "Int2",
- "Bool",
- "Bool",
- "Jsonb",
- "Text",
- "Timestamptz"
- ]
- },
- "nullable": []
- },
- "hash": "862dc37caafc34b0e704a54a782f2d6d4b949f78a45d3c0bda01c63e1842f5c1"
-}
diff --git a/.sqlx/query-86bb7ac593a43d6168db00f97999c5168b76ba4ca71b729921f9c425298d91aa.json b/.sqlx/query-86bb7ac593a43d6168db00f97999c5168b76ba4ca71b729921f9c425298d91aa.json
new file mode 100644
index 0000000000..4395e68dcc
--- /dev/null
+++ b/.sqlx/query-86bb7ac593a43d6168db00f97999c5168b76ba4ca71b729921f9c425298d91aa.json
@@ -0,0 +1,35 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n SELECT t.id,\n GREATEST(t.latest_inbound_message_ts, t.latest_outbound_message_ts)\n AS \"latest_at!\"\n FROM email_links el\n JOIN email_threads t ON t.link_id = el.id\n LEFT JOIN email_thread_focus f ON f.thread_id = t.id\n WHERE LOWER(SPLIT_PART(el.email_address, '@', 2)) = ANY($2)\n AND t.inbox_visible\n AND t.latest_inbound_message_ts IS NOT NULL\n AND GREATEST(t.latest_inbound_message_ts, t.latest_outbound_message_ts) >= $1\n AND (\n t.is_signal\n OR t.latest_outbound_message_ts > t.latest_inbound_message_ts\n OR f.rule_version < $4\n -- A calendar change. The service judges whether it is the\n -- latest message and its event isn't over; a new invite or an\n -- RSVP never gets this far.\n OR EXISTS (\n SELECT 1\n FROM email_messages notice\n WHERE notice.thread_id = t.id\n AND NOT notice.is_sent\n AND NOT notice.is_draft\n AND (\n LOWER(notice.subject) ~ $6\n OR EXISTS (\n SELECT 1 FROM email_message_calendar_invites i\n WHERE i.message_id = notice.id\n AND (\n i.snapshot->>'method' IN ('cancel', 'counter')\n OR (\n i.snapshot->>'method' = 'request'\n AND i.snapshot->'sequence' > '0'::jsonb\n )\n )\n )\n )\n )\n -- A sender the owner has written to lately. The service judges\n -- whether it really is a recent contact; mail no event covers,\n -- such as an initial sync, only arrives this way.\n OR EXISTS (\n SELECT 1\n FROM email_messages inbound\n JOIN email_message_recipients r ON r.contact_id = inbound.from_contact_id\n JOIN email_messages sent ON sent.id = r.message_id\n WHERE inbound.thread_id = t.id\n AND NOT inbound.is_sent\n AND NOT inbound.is_draft\n AND sent.is_sent\n AND NOT sent.is_draft\n AND sent.internal_date_ts >= $5\n )\n )\n AND (\n f.thread_id IS NULL\n OR f.rule_version < $4\n OR f.classified_message_ts\n < GREATEST(t.latest_inbound_message_ts, t.latest_outbound_message_ts)\n )\n AND (\n $7::timestamptz IS NULL\n OR (GREATEST(t.latest_inbound_message_ts, t.latest_outbound_message_ts), t.id)\n < ($7, $8::uuid)\n )\n ORDER BY GREATEST(t.latest_inbound_message_ts, t.latest_outbound_message_ts) DESC, t.id DESC\n LIMIT $3\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "id",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 1,
+ "name": "latest_at!",
+ "type_info": "Timestamptz"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Timestamptz",
+ "TextArray",
+ "Int8",
+ "Int2",
+ "Timestamptz",
+ "Text",
+ "Timestamptz",
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ false,
+ null
+ ]
+ },
+ "hash": "86bb7ac593a43d6168db00f97999c5168b76ba4ca71b729921f9c425298d91aa"
+}
diff --git a/.sqlx/query-95f7a63d62734055e597dc81a3dd64d7ad41a7b379086980a533f2ff5e5302b7.json b/.sqlx/query-95f7a63d62734055e597dc81a3dd64d7ad41a7b379086980a533f2ff5e5302b7.json
new file mode 100644
index 0000000000..9c7516cddb
--- /dev/null
+++ b/.sqlx/query-95f7a63d62734055e597dc81a3dd64d7ad41a7b379086980a533f2ff5e5302b7.json
@@ -0,0 +1,37 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n SELECT newest.recipient AS \"recipient!\",\n COALESCE(newest.internal_date_ts, m.created_at) AS \"at!\",\n LEFT(m.body_text, $3) AS body\n FROM (\n SELECT LOWER(rc.email_address) AS recipient, m.id, m.internal_date_ts\n FROM email_contacts rc\n JOIN email_message_recipients r ON r.contact_id = rc.id\n JOIN email_messages m ON m.id = r.message_id\n WHERE rc.link_id = $1\n AND LOWER(rc.email_address) = ANY($2)\n AND m.is_sent\n AND NOT m.is_draft\n ORDER BY m.internal_date_ts DESC NULLS LAST\n LIMIT $4\n ) newest\n JOIN email_messages m ON m.id = newest.id\n ORDER BY newest.internal_date_ts DESC NULLS LAST\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "recipient!",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 1,
+ "name": "at!",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 2,
+ "name": "body",
+ "type_info": "Text"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "TextArray",
+ "Int4",
+ "Int8"
+ ]
+ },
+ "nullable": [
+ null,
+ null,
+ null
+ ]
+ },
+ "hash": "95f7a63d62734055e597dc81a3dd64d7ad41a7b379086980a533f2ff5e5302b7"
+}
diff --git a/.sqlx/query-2110d42dd38744a81f045a881557e43847c3fe531c5f473ceb755682534db90a.json b/.sqlx/query-a9de89d42ce460860138106dd395aa23a5d2fc1e187a3ea149e0c9c06d9294ad.json
similarity index 73%
rename from .sqlx/query-2110d42dd38744a81f045a881557e43847c3fe531c5f473ceb755682534db90a.json
rename to .sqlx/query-a9de89d42ce460860138106dd395aa23a5d2fc1e187a3ea149e0c9c06d9294ad.json
index 67e5c9edd3..fc0786fd16 100644
--- a/.sqlx/query-2110d42dd38744a81f045a881557e43847c3fe531c5f473ceb755682534db90a.json
+++ b/.sqlx/query-a9de89d42ce460860138106dd395aa23a5d2fc1e187a3ea149e0c9c06d9294ad.json
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
- "query": "\n SELECT\n m.id,\n COALESCE(m.internal_date_ts, m.created_at) AS \"at!\",\n m.is_sent,\n m.has_attachments,\n m.subject,\n m.snippet,\n LEFT(m.body_text, $2) AS body,\n c.email_address AS \"from_email?\",\n COALESCE(m.from_name, c.name) AS from_name,\n COALESCE((\n SELECT array_agg(rc.email_address ORDER BY rc.email_address)\n FROM email_message_recipients r\n JOIN email_contacts rc ON rc.id = r.contact_id\n WHERE r.message_id = m.id AND r.recipient_type = 'TO'\n ), '{}') AS \"to_emails!\",\n COALESCE((\n SELECT array_agg(rc.email_address ORDER BY rc.email_address)\n FROM email_message_recipients r\n JOIN email_contacts rc ON rc.id = r.contact_id\n WHERE r.message_id = m.id AND r.recipient_type = 'CC'\n ), '{}') AS \"cc_emails!\",\n EXISTS (\n SELECT 1\n FROM jsonb_array_elements(\n CASE WHEN jsonb_typeof(m.headers_jsonb) = 'array'\n THEN m.headers_jsonb ELSE '[]'::jsonb END\n ) h\n WHERE LOWER(h->>'name') IN (\n 'list-unsubscribe', 'list-id', 'feedback-id', 'x-sg-eid',\n 'x-ses-outgoing', 'x-mailgun-tag', 'x-campaign'\n )\n OR (LOWER(h->>'name') = 'precedence'\n AND LOWER(h->>'value') IN ('bulk', 'list', 'junk'))\n ) AS \"bulk!\"\n FROM email_messages m\n LEFT JOIN email_contacts c ON c.id = m.from_contact_id\n WHERE m.thread_id = $1\n AND NOT m.is_draft\n AND NOT EXISTS (\n SELECT 1 FROM email_message_labels ml\n JOIN email_labels l ON l.id = ml.label_id\n WHERE ml.message_id = m.id AND l.name = 'TRASH'\n )\n ORDER BY COALESCE(m.internal_date_ts, m.created_at) DESC, m.id DESC\n LIMIT $3\n ",
+ "query": "\n SELECT\n m.id,\n COALESCE(m.internal_date_ts, m.created_at) AS \"at!\",\n m.is_sent,\n m.has_attachments,\n m.subject,\n m.snippet,\n LEFT(m.body_text, $2) AS body,\n c.email_address AS \"from_email?\",\n COALESCE(m.from_name, c.name) AS from_name,\n COALESCE((\n SELECT array_agg(rc.email_address ORDER BY rc.email_address)\n FROM email_message_recipients r\n JOIN email_contacts rc ON rc.id = r.contact_id\n WHERE r.message_id = m.id AND r.recipient_type = 'TO'\n ), '{}') AS \"to_emails!\",\n COALESCE((\n SELECT array_agg(rc.email_address ORDER BY rc.email_address)\n FROM email_message_recipients r\n JOIN email_contacts rc ON rc.id = r.contact_id\n WHERE r.message_id = m.id AND r.recipient_type = 'CC'\n ), '{}') AS \"cc_emails!\",\n EXISTS (\n SELECT 1\n FROM jsonb_array_elements(\n CASE WHEN jsonb_typeof(m.headers_jsonb) = 'array'\n THEN m.headers_jsonb ELSE '[]'::jsonb END\n ) h\n WHERE LOWER(h->>'name') IN (\n 'list-unsubscribe', 'list-id', 'feedback-id', 'x-sg-eid',\n 'x-ses-outgoing', 'x-mailgun-tag', 'x-campaign'\n )\n OR (LOWER(h->>'name') = 'precedence'\n AND LOWER(h->>'value') IN ('bulk', 'list', 'junk'))\n ) AS \"bulk!\",\n EXISTS (\n SELECT 1 FROM email_message_calendar_invites i\n WHERE i.message_id = m.id\n ) OR EXISTS (\n SELECT 1 FROM email_attachments a\n WHERE a.message_id = m.id\n AND (a.filename ILIKE '%.ics'\n OR a.mime_type IN ('text/calendar', 'application/ics'))\n ) AS \"has_calendar!\"\n FROM email_messages m\n LEFT JOIN email_contacts c ON c.id = m.from_contact_id\n WHERE m.thread_id = $1\n AND NOT m.is_draft\n AND NOT EXISTS (\n SELECT 1 FROM email_message_labels ml\n JOIN email_labels l ON l.id = ml.label_id\n WHERE ml.message_id = m.id AND l.name = 'TRASH'\n )\n ORDER BY COALESCE(m.internal_date_ts, m.created_at) DESC, m.id DESC\n LIMIT $3\n ",
"describe": {
"columns": [
{
@@ -62,6 +62,11 @@
"ordinal": 11,
"name": "bulk!",
"type_info": "Bool"
+ },
+ {
+ "ordinal": 12,
+ "name": "has_calendar!",
+ "type_info": "Bool"
}
],
"parameters": {
@@ -83,8 +88,9 @@
null,
null,
null,
+ null,
null
]
},
- "hash": "2110d42dd38744a81f045a881557e43847c3fe531c5f473ceb755682534db90a"
+ "hash": "a9de89d42ce460860138106dd395aa23a5d2fc1e187a3ea149e0c9c06d9294ad"
}
diff --git a/.sqlx/query-cd9fbd473a524bea0be21c40dc68018d0552b556a93e9c5e8a6a8d1d4a3d9fec.json b/.sqlx/query-cd9fbd473a524bea0be21c40dc68018d0552b556a93e9c5e8a6a8d1d4a3d9fec.json
deleted file mode 100644
index 5497058963..0000000000
--- a/.sqlx/query-cd9fbd473a524bea0be21c40dc68018d0552b556a93e9c5e8a6a8d1d4a3d9fec.json
+++ /dev/null
@@ -1,59 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n SELECT f.thread_id, f.is_focus, f.category, f.importance, f.needs_reply,\n f.needs_follow_up, f.classified_at\n FROM email_thread_focus f\n WHERE f.thread_id = ANY($1)\n AND f.link_id IN (\n SELECT el.id FROM email_links el WHERE el.macro_id = $2\n UNION\n SELECT mul.link_id FROM macro_user_links mul WHERE mul.primary_macro_id = $2\n )\n ",
- "describe": {
- "columns": [
- {
- "ordinal": 0,
- "name": "thread_id",
- "type_info": "Uuid"
- },
- {
- "ordinal": 1,
- "name": "is_focus",
- "type_info": "Bool"
- },
- {
- "ordinal": 2,
- "name": "category",
- "type_info": "Text"
- },
- {
- "ordinal": 3,
- "name": "importance",
- "type_info": "Int2"
- },
- {
- "ordinal": 4,
- "name": "needs_reply",
- "type_info": "Bool"
- },
- {
- "ordinal": 5,
- "name": "needs_follow_up",
- "type_info": "Bool"
- },
- {
- "ordinal": 6,
- "name": "classified_at",
- "type_info": "Timestamptz"
- }
- ],
- "parameters": {
- "Left": [
- "UuidArray",
- "Text"
- ]
- },
- "nullable": [
- false,
- false,
- false,
- false,
- false,
- false,
- false
- ]
- },
- "hash": "cd9fbd473a524bea0be21c40dc68018d0552b556a93e9c5e8a6a8d1d4a3d9fec"
-}
diff --git a/.sqlx/query-e0fb2c92ab5c17b3b872c396babd02295055a9eb318fa968a4db79ba6807d381.json b/.sqlx/query-e0fb2c92ab5c17b3b872c396babd02295055a9eb318fa968a4db79ba6807d381.json
deleted file mode 100644
index e76f5582a1..0000000000
--- a/.sqlx/query-e0fb2c92ab5c17b3b872c396babd02295055a9eb318fa968a4db79ba6807d381.json
+++ /dev/null
@@ -1,31 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n SELECT newest.recipient AS \"recipient!\", LEFT(m.body_text, $3) AS body\n FROM (\n SELECT LOWER(rc.email_address) AS recipient, m.id, m.internal_date_ts\n FROM email_contacts rc\n JOIN email_message_recipients r ON r.contact_id = rc.id\n JOIN email_messages m ON m.id = r.message_id\n WHERE rc.link_id = $1\n AND LOWER(rc.email_address) = ANY($2)\n AND m.is_sent\n AND NOT m.is_draft\n ORDER BY m.internal_date_ts DESC NULLS LAST\n LIMIT $4\n ) newest\n JOIN email_messages m ON m.id = newest.id\n ORDER BY newest.internal_date_ts DESC NULLS LAST\n ",
- "describe": {
- "columns": [
- {
- "ordinal": 0,
- "name": "recipient!",
- "type_info": "Text"
- },
- {
- "ordinal": 1,
- "name": "body",
- "type_info": "Text"
- }
- ],
- "parameters": {
- "Left": [
- "Uuid",
- "TextArray",
- "Int4",
- "Int8"
- ]
- },
- "nullable": [
- null,
- null
- ]
- },
- "hash": "e0fb2c92ab5c17b3b872c396babd02295055a9eb318fa968a4db79ba6807d381"
-}
diff --git a/crates/complete_graph/src/schema.rs b/crates/complete_graph/src/schema.rs
index cfc2e6a332..2d16a7c4ef 100644
--- a/crates/complete_graph/src/schema.rs
+++ b/crates/complete_graph/src/schema.rs
@@ -938,10 +938,10 @@ where
.await
}
- /// The viewer's Focus: signal threads in their own inboxes worth their
- /// attention, highest priority first (importance, halved for every 2 days
- /// since the latest inbound mail), with mail in the last `days` days
- /// (default 30, at most 90). Each thread's `focus` field explains why.
+ /// The viewer's Focus: threads in their own inboxes worth their attention,
+ /// newest first by latest message time in either direction, with mail in
+ /// the last `days` days (default 30, at most 90). Each thread carries a
+ /// `focus` field that explains why.
async fn email_focus(
&self,
ctx: &Context<'_>,
diff --git a/crates/email/AGENTS.md b/crates/email/AGENTS.md
index 5325a92cdc..93dc8e9141 100644
--- a/crates/email/AGENTS.md
+++ b/crates/email/AGENTS.md
@@ -54,6 +54,24 @@ tools — hexagonal, heavily feature-gated.
## Gotchas
+- Focus's `decide` and `signals` are versioned: bump `FOCUS_RULE_VERSION` in
+ `domain/focus/rule.rs` when either changes, or stored rows keep the old verdict
+ until new mail arrives. The worker's sweep re-decides older rows from their
+ stored `scores`, without calling Jev. `rule/experiment.csv` holds the expected
+ verdicts for 643 real threads; report how far a rule change moves them.
+- Nothing reclassifies a Focus thread as it ages, so `focus_pg` applies the wait
+ and the calendar hold when it reads: `awaiting_reply` and `calendar_hold_until`
+ are stored, `FOLLOW_UP_AFTER_DAYS` and the time are passed in. `focus_thread_ids`
+ and `focus_for_threads` restate the same tests; change both.
+- In Focus, Calendar only means a change whose event isn't over: the read sets it
+ from `calendar_hold_until`, and the stored `category` is what the thread reads as
+ afterwards. A stored Calendar is a new invite or RSVP left out, and
+ `NEW_INVITES_STAY_OUT` in `domain/focus/rule.rs` is the switch for new invites.
+ `stale_threads` restates `domain/focus/calendar.rs`'s test for a change in SQL;
+ change both.
+- A thread holding only the owner's sent mail stays out of Focus on purpose:
+ it has `inbox_visible` false, which the client reads as Done, Mark Done cannot
+ change it, and there is no inbound mail for the rule to judge.
- Treat DATE property literals as non-matches; apply ordinary boolean negation and keep non-date email filtering unchanged.
- All Mail excludes present messages in spam/junk or trash before pagination;
canonical cached previews and non-draft baselines use the same rule for Gmail
diff --git a/crates/email/fixtures/email_focus_waiting.sql b/crates/email/fixtures/email_focus_waiting.sql
new file mode 100644
index 0000000000..770a078862
--- /dev/null
+++ b/crates/email/fixtures/email_focus_waiting.sql
@@ -0,0 +1,131 @@
+-- Fixture for the Focus repository (email crate, outbound/focus_pg), loaded
+-- after email_focus. Inbox state comes from each message's labels: tests run
+-- recompute_thread_metadata, so visibility and timestamps are what ingestion
+-- would store, not what a fixture asserts.
+--
+-- Thread f301: Casey wrote 10 days ago, the owner answered 5 days ago.
+-- Thread f302: Casey wrote 3 days ago, the owner answered yesterday.
+-- Thread f303: as f301, but marked Done: the incoming mail lost its INBOX label.
+-- Thread f304: as f301, but the owner's answer only says "Thanks!".
+-- Thread f305: only the owner's own message, sent 5 days ago.
+-- Threads f306 to f309 come from someone the owner never wrote to, so only what
+-- the thread itself is can bring them in:
+-- Thread f306: a reschedule (METHOD:REQUEST past the first revision) filed under
+-- Updates, for an event two days ahead.
+-- Thread f307: an RSVP (METHOD:REPLY) filed under Updates.
+-- Thread f308: plain mail filed under Updates.
+-- Thread f309: a calendar file nothing parsed, filed under Updates.
+-- Thread f30a: mail from Casey, whom the owner wrote to lately, filed under Updates.
+
+INSERT INTO email_contacts (id, link_id, email_address, name, created_at, updated_at)
+VALUES ('00000000-0000-0000-0000-0000000cf006', '00000000-0000-0000-0000-000000000f01', '[email protected]', NULL, NOW(), NOW());
+
+INSERT INTO email_labels (id, link_id, provider_label_id, name, created_at)
+VALUES ('00000000-0000-0000-0000-0000000bf002', '00000000-0000-0000-0000-000000000f01', 'INBOX', 'INBOX', NOW()),
+ ('00000000-0000-0000-0000-0000000bf003', '00000000-0000-0000-0000-000000000f01', 'SENT', 'SENT', NOW()),
+ ('00000000-0000-0000-0000-0000000bf004', '00000000-0000-0000-0000-000000000f01', 'CATEGORY_UPDATES', 'CATEGORY_UPDATES', NOW());
+
+INSERT INTO email_threads (id, link_id, inbox_visible, is_read, is_signal, created_at, updated_at)
+SELECT ('00000000-0000-0000-0000-00000000f30' || n)::uuid, '00000000-0000-0000-0000-000000000f01', false, true, false, NOW(), NOW()
+FROM unnest(ARRAY['1', '2', '3', '4', '5', '6', '7', '8', '9', 'a']) AS n;
+
+INSERT INTO email_messages (id, thread_id, link_id, provider_id, global_id, is_sent, from_contact_id, internal_date_ts,
+ subject, body_text, has_attachments, is_read, is_starred, is_draft, created_at, updated_at)
+VALUES ('00000000-0000-0000-0000-00000000f611', '00000000-0000-0000-0000-00000000f301', '00000000-0000-0000-0000-000000000f01',
+ 'p-f611', 'g-f611', false, '00000000-0000-0000-0000-0000000cf001', NOW() - INTERVAL '10 days',
+ 'Contract', 'Here is the contract.', false, true, false, false, NOW(), NOW()),
+ ('00000000-0000-0000-0000-00000000f612', '00000000-0000-0000-0000-00000000f301', '00000000-0000-0000-0000-000000000f01',
+ 'p-f612', 'g-f612', true, '00000000-0000-0000-0000-0000000cf002', NOW() - INTERVAL '5 days',
+ 'Re: Contract', 'Can you send the signed copy?', false, true, false, false, NOW(), NOW()),
+ ('00000000-0000-0000-0000-00000000f621', '00000000-0000-0000-0000-00000000f302', '00000000-0000-0000-0000-000000000f01',
+ 'p-f621', 'g-f621', false, '00000000-0000-0000-0000-0000000cf001', NOW() - INTERVAL '3 days',
+ 'Pilot', 'How is the pilot going?', false, true, false, false, NOW(), NOW()),
+ ('00000000-0000-0000-0000-00000000f622', '00000000-0000-0000-0000-00000000f302', '00000000-0000-0000-0000-000000000f01',
+ 'p-f622', 'g-f622', true, '00000000-0000-0000-0000-0000000cf002', NOW() - INTERVAL '1 day',
+ 'Re: Pilot', 'Well. When can we talk through the results?', false, true, false, false, NOW(), NOW()),
+ ('00000000-0000-0000-0000-00000000f631', '00000000-0000-0000-0000-00000000f303', '00000000-0000-0000-0000-000000000f01',
+ 'p-f631', 'g-f631', false, '00000000-0000-0000-0000-0000000cf001', NOW() - INTERVAL '10 days',
+ 'Old thread', 'Here is the report.', false, true, false, false, NOW(), NOW()),
+ ('00000000-0000-0000-0000-00000000f632', '00000000-0000-0000-0000-00000000f303', '00000000-0000-0000-0000-000000000f01',
+ 'p-f632', 'g-f632', true, '00000000-0000-0000-0000-0000000cf002', NOW() - INTERVAL '5 days',
+ 'Re: Old thread', 'Can you send last year too?', false, true, false, false, NOW(), NOW()),
+ ('00000000-0000-0000-0000-00000000f641', '00000000-0000-0000-0000-00000000f304', '00000000-0000-0000-0000-000000000f01',
+ 'p-f641', 'g-f641', false, '00000000-0000-0000-0000-0000000cf001', NOW() - INTERVAL '10 days',
+ 'Slides', 'Here are the slides.', false, true, false, false, NOW(), NOW()),
+ ('00000000-0000-0000-0000-00000000f642', '00000000-0000-0000-0000-00000000f304', '00000000-0000-0000-0000-000000000f01',
+ 'p-f642', 'g-f642', true, '00000000-0000-0000-0000-0000000cf002', NOW() - INTERVAL '5 days',
+ 'Re: Slides', 'Thanks!', false, true, false, false, NOW(), NOW()),
+ ('00000000-0000-0000-0000-00000000f651', '00000000-0000-0000-0000-00000000f305', '00000000-0000-0000-0000-000000000f01',
+ 'p-f651', 'g-f651', true, '00000000-0000-0000-0000-0000000cf002', NOW() - INTERVAL '5 days',
+ 'Hello', 'Are you free next week?', false, true, false, false, NOW(), NOW()),
+ ('00000000-0000-0000-0000-00000000f661', '00000000-0000-0000-0000-00000000f306', '00000000-0000-0000-0000-000000000f01',
+ 'p-f661', 'g-f661', false, '00000000-0000-0000-0000-0000000cf006', NOW() - INTERVAL '1 day',
+ 'Moved: Demo', '', false, true, false, false, NOW(), NOW()),
+ ('00000000-0000-0000-0000-00000000f671', '00000000-0000-0000-0000-00000000f307', '00000000-0000-0000-0000-000000000f01',
+ 'p-f671', 'g-f671', false, '00000000-0000-0000-0000-0000000cf006', NOW() - INTERVAL '1 day',
+ 'Aceptada: Demo', '', false, true, false, false, NOW(), NOW()),
+ ('00000000-0000-0000-0000-00000000f681', '00000000-0000-0000-0000-00000000f308', '00000000-0000-0000-0000-000000000f01',
+ 'p-f681', 'g-f681', false, '00000000-0000-0000-0000-0000000cf006', NOW() - INTERVAL '1 day',
+ 'Weekly numbers', 'Up and to the right.', false, true, false, false, NOW(), NOW()),
+ ('00000000-0000-0000-0000-00000000f6a1', '00000000-0000-0000-0000-00000000f30a', '00000000-0000-0000-0000-000000000f01',
+ 'p-f6a1', 'g-f6a1', false, '00000000-0000-0000-0000-0000000cf001', NOW() - INTERVAL '1 day',
+ 'Quarterly summary', 'For your records.', false, true, false, false, NOW(), NOW()),
+ ('00000000-0000-0000-0000-00000000f691', '00000000-0000-0000-0000-00000000f309', '00000000-0000-0000-0000-000000000f01',
+ 'p-f691', 'g-f691', false, '00000000-0000-0000-0000-0000000cf006', NOW() - INTERVAL '1 day',
+ 'See you there', '', true, true, false, false, NOW(), NOW());
+
+INSERT INTO email_message_recipients (message_id, contact_id, recipient_type)
+VALUES ('00000000-0000-0000-0000-00000000f611', '00000000-0000-0000-0000-0000000cf002', 'TO'),
+ ('00000000-0000-0000-0000-00000000f612', '00000000-0000-0000-0000-0000000cf001', 'TO'),
+ ('00000000-0000-0000-0000-00000000f621', '00000000-0000-0000-0000-0000000cf002', 'TO'),
+ ('00000000-0000-0000-0000-00000000f622', '00000000-0000-0000-0000-0000000cf001', 'TO'),
+ ('00000000-0000-0000-0000-00000000f631', '00000000-0000-0000-0000-0000000cf002', 'TO'),
+ ('00000000-0000-0000-0000-00000000f632', '00000000-0000-0000-0000-0000000cf001', 'TO'),
+ ('00000000-0000-0000-0000-00000000f641', '00000000-0000-0000-0000-0000000cf002', 'TO'),
+ ('00000000-0000-0000-0000-00000000f642', '00000000-0000-0000-0000-0000000cf001', 'TO'),
+ ('00000000-0000-0000-0000-00000000f651', '00000000-0000-0000-0000-0000000cf001', 'TO');
+
+INSERT INTO email_message_labels (message_id, label_id)
+VALUES ('00000000-0000-0000-0000-00000000f611', '00000000-0000-0000-0000-0000000bf002'),
+ ('00000000-0000-0000-0000-00000000f612', '00000000-0000-0000-0000-0000000bf003'),
+ ('00000000-0000-0000-0000-00000000f621', '00000000-0000-0000-0000-0000000bf002'),
+ ('00000000-0000-0000-0000-00000000f622', '00000000-0000-0000-0000-0000000bf003'),
+ -- f631 lost INBOX when the thread was marked Done.
+ ('00000000-0000-0000-0000-00000000f632', '00000000-0000-0000-0000-0000000bf003'),
+ ('00000000-0000-0000-0000-00000000f641', '00000000-0000-0000-0000-0000000bf002'),
+ ('00000000-0000-0000-0000-00000000f642', '00000000-0000-0000-0000-0000000bf003'),
+ ('00000000-0000-0000-0000-00000000f651', '00000000-0000-0000-0000-0000000bf003'),
+ ('00000000-0000-0000-0000-00000000f661', '00000000-0000-0000-0000-0000000bf002'),
+ ('00000000-0000-0000-0000-00000000f661', '00000000-0000-0000-0000-0000000bf004'),
+ ('00000000-0000-0000-0000-00000000f671', '00000000-0000-0000-0000-0000000bf002'),
+ ('00000000-0000-0000-0000-00000000f671', '00000000-0000-0000-0000-0000000bf004'),
+ ('00000000-0000-0000-0000-00000000f681', '00000000-0000-0000-0000-0000000bf002'),
+ ('00000000-0000-0000-0000-00000000f681', '00000000-0000-0000-0000-0000000bf004'),
+ ('00000000-0000-0000-0000-00000000f691', '00000000-0000-0000-0000-0000000bf002'),
+ ('00000000-0000-0000-0000-00000000f691', '00000000-0000-0000-0000-0000000bf004'),
+ ('00000000-0000-0000-0000-00000000f6a1', '00000000-0000-0000-0000-0000000bf002'),
+ ('00000000-0000-0000-0000-00000000f6a1', '00000000-0000-0000-0000-0000000bf004');
+
+INSERT INTO email_message_calendar_invites (message_id, component_id, snapshot)
+VALUES ('00000000-0000-0000-0000-00000000f661', 'request-1', jsonb_build_object(
+ 'id', 'request-1', 'uid', '[email protected]', 'method', 'request', 'sequence', 1,
+ 'attendees', '[]'::jsonb, 'recurrence', '[]'::jsonb,
+ 'end', jsonb_build_object(
+ 'kind', 'zoned', 'time_zone', 'UTC',
+ 'value', to_char((NOW() + INTERVAL '2 days') AT TIME ZONE 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS"Z"')
+ )
+ )),
+ ('00000000-0000-0000-0000-00000000f671', 'reply-1', jsonb_build_object(
+ 'id', 'reply-1', 'uid', '[email protected]', 'method', 'reply', 'sequence', 1,
+ 'attendees', '[]'::jsonb, 'recurrence', '[]'::jsonb
+ ));
+
+INSERT INTO email_attachments (id, message_id, filename, mime_type, created_at)
+VALUES ('00000000-0000-0000-0000-00000000f991', '00000000-0000-0000-0000-00000000f691', 'invite.ics', 'text/calendar', NOW());
+
+-- Maintained at ingestion; recompute_thread_metadata leaves it alone.
+UPDATE email_threads
+SET has_calendar_attachment = true
+WHERE id IN ('00000000-0000-0000-0000-00000000f306',
+ '00000000-0000-0000-0000-00000000f307',
+ '00000000-0000-0000-0000-00000000f309');
diff --git a/crates/email/src/domain/focus.rs b/crates/email/src/domain/focus.rs
index c400dbfa0b..fb89a3e8f5 100644
--- a/crates/email/src/domain/focus.rs
+++ b/crates/email/src/domain/focus.rs
@@ -1,12 +1,19 @@
-//! Focus: which signal threads deserve the owner's attention.
+//! Focus: which inbox threads deserve the owner's attention.
//!
-//! Each signal thread is classified on every new message. Jev, a yes/no
-//! classifier, answers a fixed set of questions about the thread with the
-//! owner's stored memory as context; a deterministic rule then turns those
-//! answers and the owner's relationship with the sender into a verdict: in or
-//! out of Focus, a category, reply-needed and follow-up flags, and a 0-100
-//! importance score. Only inboxes on the worker's allowlist are classified.
+//! Each signal thread is classified on every new message, as is key mail outside
+//! the signal inbox: changes to events that aren't over, mail from recent contacts,
+//! and threads the owner answered last. Jev, a yes/no classifier, answers a fixed set of
+//! questions about the thread with the owner's stored memory as context; a
+//! deterministic rule then turns those answers and the owner's relationship with
+//! the sender into a verdict: in or out of Focus, a category, reply-needed and
+//! follow-up flags, and a 0-100 importance score. A thread the owner has waited
+//! on past a threshold joins Focus when it is read, and a calendar change leaves
+//! it when its event is over, since nothing reclassifies a thread as it ages.
+//! Threads holding only the owner's own sent mail stay out:
+//! they are not inbox threads, and nothing marks one Done. Only inboxes on the
+//! worker's allowlist are classified.
+mod calendar;
mod input;
mod models;
mod ports;
@@ -15,12 +22,15 @@ mod rule;
mod service;
mod signals;
+pub use calendar::change_subject_pattern;
pub use input::{is_dismissive_reply, new_text};
pub use models::{
- FocusAnswers, FocusCategory, FocusInbox, FocusMail, FocusMessage, FocusRecord, FocusSignals,
- FocusThread, FocusVerdict, SentNote, StaleThread, ThreadFocus,
+ CalendarNotice, CalendarPart, FocusAnswers, FocusCategory, FocusInbox, FocusMail, FocusMessage,
+ FocusRecord, FocusSignals, FocusThread, FocusVerdict, SentNote, StaleThread,
+ StoredClassification, ThreadFocus,
};
pub use ports::{FocusClassifier, FocusClassifierError, FocusStore, ProfileSource};
pub use questions::{FOCUS_QUESTIONS, FocusQuestion};
-pub use rule::{decide, is_calendar_subject};
+pub use rule::{FOCUS_RULE_VERSION, decide};
pub use service::{ClassifyOutcome, FocusQueries, FocusService, SweepReport};
+pub use signals::RECENT_CONTACT_DAYS;
diff --git a/crates/email/src/domain/focus/calendar.rs b/crates/email/src/domain/focus/calendar.rs
new file mode 100644
index 0000000000..1056bbb3f2
--- /dev/null
+++ b/crates/email/src/domain/focus/calendar.rs
@@ -0,0 +1,174 @@
+//! What a message's saved calendar content and subject say about an event.
+
+use chrono::{DateTime, Duration, NaiveDate, NaiveTime, Utc};
+
+use super::models::{CalendarNotice, CalendarPart, FocusMessage};
+use crate::domain::models::calendar_invitation::{
+ CalendarInvitation, InvitationDateTime, InvitationMethod,
+};
+
+/// Days a change holds a thread when nothing says when its event is over: a
+/// series goes on past any one date, and a subject names no usable time.
+const FALLBACK_HOLD_DAYS: i64 = 7;
+/// Hours after midnight UTC by which a date has begun everywhere on Earth. An
+/// all-day event carries no zone, so its end date only counts once it has.
+const DATE_BEGUN_EVERYWHERE_HOURS: i64 = 12;
+
+/// Subject prefixes Google Calendar, Outlook and scheduling tools put on updates,
+/// reschedules, cancellations and proposed new times.
+const CHANGE_PREFIXES: [&str; 13] = [
+ "updated invitation",
+ "updated invitation with note",
+ "canceled",
+ "cancelled",
+ "canceled event",
+ "cancelled event",
+ "canceled event with note",
+ "event updated",
+ "rescheduled",
+ "event rescheduled",
+ "meeting rescheduled",
+ "proposed new time",
+ "new time proposed",
+];
+
+/// Subject prefixes on first invitations.
+const NEW_INVITE_PREFIXES: [&str; 2] = ["invitation", "new event"];
+
+/// Subject prefixes on attendees' RSVP notices.
+const RSVP_PREFIXES: [&str; 4] = ["accepted", "declined", "tentative", "tentatively accepted"];
+
+fn has_subject_prefix(subject: &str, prefixes: &[&str]) -> bool {
+ prefixes.iter().any(|prefix| {
+ subject
+ .strip_prefix(prefix)
+ .is_some_and(|rest| rest.trim_start().starts_with(':'))
+ })
+}
+
+/// A Postgres regular expression matching a lowercased subject with a change
+/// prefix, so the sweep looks for what [`notice_in_subject`] reads.
+pub fn change_subject_pattern() -> String {
+ format!(r"^({})\s*:", CHANGE_PREFIXES.join("|"))
+}
+
+fn fallback_hold(at: DateTime<Utc>) -> DateTime<Utc> {
+ at + Duration::days(FALLBACK_HOLD_DAYS)
+}
+
+/// The calendar notice a subject reads as, e.g. "Accepted: Demo @ Fri" is an
+/// RSVP. A change holds until `change_until`.
+fn notice_in_subject(subject: &str, change_until: DateTime<Utc>) -> Option<CalendarNotice> {
+ let subject = subject.to_lowercase();
+ if has_subject_prefix(&subject, &RSVP_PREFIXES) {
+ Some(CalendarNotice::Rsvp)
+ } else if has_subject_prefix(&subject, &CHANGE_PREFIXES) {
+ Some(CalendarNotice::Change {
+ until: change_until,
+ })
+ } else if has_subject_prefix(&subject, &NEW_INVITE_PREFIXES) {
+ Some(CalendarNotice::NewInvite)
+ } else {
+ None
+ }
+}
+
+/// The calendar notice one message is. A saved change or RSVP decides. Otherwise
+/// the subject does, since not every tool bumps an event's sequence when it
+/// changes, and a calendar file that says nothing more is a new invite.
+pub(super) fn notice_in_message(message: &FocusMessage) -> Option<CalendarNotice> {
+ let from_subject = |change_until| {
+ message
+ .subject
+ .as_deref()
+ .and_then(|subject| notice_in_subject(subject, change_until))
+ };
+ let fallback = fallback_hold(message.at);
+ match message.calendar_part {
+ Some(CalendarPart::Change { until }) => Some(CalendarNotice::Change { until }),
+ Some(CalendarPart::Rsvp) => Some(CalendarNotice::Rsvp),
+ Some(CalendarPart::NewInvite { until }) => {
+ from_subject(until).or(Some(CalendarNotice::NewInvite))
+ }
+ Some(CalendarPart::Unparsed) => from_subject(fallback).or(Some(CalendarNotice::NewInvite)),
+ None => from_subject(fallback),
+ }
+}
+
+fn is_change(invite: &CalendarInvitation) -> bool {
+ match invite.method {
+ InvitationMethod::Cancel | InvitationMethod::Counter => true,
+ InvitationMethod::Request => invite.sequence > 0,
+ InvitationMethod::Reply | InvitationMethod::Publish | InvitationMethod::Unknown => false,
+ }
+}
+
+fn start_of_date(value: &str) -> Option<DateTime<Utc>> {
+ let date = NaiveDate::parse_from_str(value, "%Y-%m-%d").ok()?;
+ Some(date.and_time(NaiveTime::MIN).and_utc() + Duration::hours(DATE_BEGUN_EVERYWHERE_HOURS))
+}
+
+fn instant(value: &str) -> Option<DateTime<Utc>> {
+ DateTime::parse_from_rfc3339(value)
+ .ok()
+ .map(|at| at.to_utc())
+}
+
+/// When an event is over: its end, else its start. An end date is exclusive, and
+/// an all-day start with no end lasts that day.
+fn event_over_at(invite: &CalendarInvitation) -> Option<DateTime<Utc>> {
+ let end = invite.end.as_ref().and_then(|end| match end {
+ InvitationDateTime::Zoned { value, .. } => instant(value),
+ InvitationDateTime::Date { value } => start_of_date(value),
+ InvitationDateTime::Unresolved { .. } => None,
+ });
+ end.or_else(|| {
+ invite.start.as_ref().and_then(|start| match start {
+ InvitationDateTime::Zoned { value, .. } => instant(value),
+ InvitationDateTime::Date { value } => {
+ start_of_date(value).map(|at| at + Duration::days(1))
+ }
+ InvitationDateTime::Unresolved { .. } => None,
+ })
+ })
+}
+
+/// How long a change holds a thread that received it at `at`. Only a single
+/// event has one end. `recurrence` is `None` on snapshots saved before it was
+/// recorded, which may hide a series, so those wait the fixed while too.
+fn change_hold(invite: &CalendarInvitation, at: DateTime<Utc>) -> DateTime<Utc> {
+ invite
+ .recurrence
+ .as_ref()
+ .filter(|lines| lines.is_empty())
+ .and_then(|_| event_over_at(invite))
+ .unwrap_or_else(|| fallback_hold(at))
+}
+
+impl CalendarPart {
+ /// What the saved components of a message received at `at` say, if it has
+ /// any. Any change makes it one, held until the last changed event is over.
+ pub fn from_saved(invites: &[CalendarInvitation], at: DateTime<Utc>) -> Option<Self> {
+ let hold = |invite: &CalendarInvitation| change_hold(invite, at);
+ if let Some(until) = invites
+ .iter()
+ .filter(|invite| is_change(invite))
+ .map(hold)
+ .max()
+ {
+ return Some(Self::Change { until });
+ }
+ let until = invites.iter().map(hold).max()?;
+ let only_replies = invites
+ .iter()
+ .all(|invite| invite.method == InvitationMethod::Reply);
+ Some(if only_replies {
+ Self::Rsvp
+ } else {
+ Self::NewInvite { until }
+ })
+ }
+}
+
+#[cfg(test)]
+mod test;
diff --git a/crates/email/src/domain/focus/calendar/test.rs b/crates/email/src/domain/focus/calendar/test.rs
new file mode 100644
index 0000000000..4d8df72fac
--- /dev/null
+++ b/crates/email/src/domain/focus/calendar/test.rs
@@ -0,0 +1,346 @@
+use chrono::TimeZone;
+use uuid::Uuid;
+
+use super::*;
+
+fn received() -> DateTime<Utc> {
+ Utc.with_ymd_and_hms(2026, 11, 1, 9, 0, 0).unwrap()
+}
+
+fn fallback() -> DateTime<Utc> {
+ received() + Duration::days(FALLBACK_HOLD_DAYS)
+}
+
+fn zoned(value: &str) -> Option<InvitationDateTime> {
+ Some(InvitationDateTime::Zoned {
+ value: value.to_owned(),
+ time_zone: "America/New_York".to_owned(),
+ })
+}
+
+fn date(value: &str) -> Option<InvitationDateTime> {
+ Some(InvitationDateTime::Date {
+ value: value.to_owned(),
+ })
+}
+
+fn invite(method: InvitationMethod, sequence: u32) -> CalendarInvitation {
+ CalendarInvitation {
+ id: "component".to_owned(),
+ uid: "[email protected]".to_owned(),
+ method,
+ sequence,
+ dtstamp: None,
+ last_modified: None,
+ status: None,
+ recurrence_id: None,
+ recurrence_id_raw: None,
+ title: None,
+ organizer: None,
+ attendees: Vec::new(),
+ comment: None,
+ location: None,
+ description: None,
+ start: zoned("2026-11-05T10:00:00-05:00"),
+ end: zoned("2026-11-05T11:00:00-05:00"),
+ recurrence: Some(Vec::new()),
+ conference_url: None,
+ }
+}
+
+fn message(subject: &str, calendar_part: Option<CalendarPart>) -> FocusMessage {
+ FocusMessage {
+ id: Uuid::new_v4(),
+ at: received(),
+ is_sent: false,
+ from_email: None,
+ from_name: None,
+ to: Vec::new(),
+ cc: Vec::new(),
+ subject: Some(subject.to_owned()),
+ has_attachments: false,
+ bulk: false,
+ calendar_part,
+ body: None,
+ snippet: None,
+ }
+}
+
+fn until(part: Option<CalendarPart>) -> DateTime<Utc> {
+ match part {
+ Some(CalendarPart::Change { until }) => until,
+ other => panic!("expected a change, got {other:?}"),
+ }
+}
+
+#[test]
+fn subjects_tell_changes_from_new_invites_and_rsvps() {
+ for subject in [
+ "Updated invitation: Standup @ Mon",
+ "Updated invitation with note: Scoping/Planning",
+ "Canceled event: Standup @ Fri",
+ "CANCELLED: Prod Deploy",
+ "Rescheduled: Demo @ Thu",
+ "Rescheduled : spaced colon",
+ "Proposed new time: Demo @ Thu",
+ ] {
+ assert_eq!(
+ notice_in_subject(subject, fallback()),
+ Some(CalendarNotice::Change { until: fallback() }),
+ "{subject}"
+ );
+ }
+ for subject in [
+ "Invitation: Standup @ Mon",
+ "Invitation : spaced colon",
+ "NEW EVENT: Offsite",
+ ] {
+ assert_eq!(
+ notice_in_subject(subject, fallback()),
+ Some(CalendarNotice::NewInvite),
+ "{subject}"
+ );
+ }
+ for subject in [
+ "Accepted: Demo @ Fri Aug 7, 2026",
+ "Tentatively Accepted: Sync Team Meet",
+ "Tentative: Standup",
+ "DECLINED: Prod Deploy",
+ ] {
+ assert_eq!(
+ notice_in_subject(subject, fallback()),
+ Some(CalendarNotice::Rsvp),
+ "{subject}"
+ );
+ }
+ for subject in [
+ "Re: Invitation: thoughts on the deck",
+ "Re: Rescheduled: Demo @ Thu",
+ "Invitations for the launch party",
+ "Can we reschedule Tuesday?",
+ "Greg x Teo",
+ "",
+ ] {
+ assert_eq!(notice_in_subject(subject, fallback()), None, "{subject}");
+ }
+}
+
+#[test]
+fn a_saved_change_or_rsvp_decides_whatever_the_subject_says() {
+ let change = CalendarPart::Change { until: fallback() };
+ assert_eq!(
+ notice_in_message(&message("Moved: Demo", Some(change))),
+ Some(CalendarNotice::Change { until: fallback() })
+ );
+ assert_eq!(
+ notice_in_message(&message("Invitation: Demo", Some(change))),
+ Some(CalendarNotice::Change { until: fallback() })
+ );
+ // An RSVP in another language has no subject prefix to match.
+ assert_eq!(
+ notice_in_message(&message("Aceptada: Demo", Some(CalendarPart::Rsvp))),
+ Some(CalendarNotice::Rsvp)
+ );
+ assert_eq!(
+ notice_in_message(&message("Rescheduled: Demo", Some(CalendarPart::Rsvp))),
+ Some(CalendarNotice::Rsvp)
+ );
+}
+
+#[test]
+fn a_new_invite_or_unread_file_reads_as_its_subject_or_as_new() {
+ let event_end = received() + Duration::days(30);
+ for (part, until) in [
+ (CalendarPart::NewInvite { until: event_end }, event_end),
+ (CalendarPart::Unparsed, fallback()),
+ ] {
+ assert_eq!(
+ notice_in_message(&message("See you there", Some(part))),
+ Some(CalendarNotice::NewInvite),
+ "{part:?}"
+ );
+ // Not every tool bumps the sequence; a change subject is enough, and the
+ // saved event's own time still decides how long it holds.
+ assert_eq!(
+ notice_in_message(&message("Rescheduled: Demo", Some(part))),
+ Some(CalendarNotice::Change { until }),
+ "{part:?}"
+ );
+ assert_eq!(
+ notice_in_message(&message("Accepted: Demo", Some(part))),
+ Some(CalendarNotice::Rsvp),
+ "{part:?}"
+ );
+ }
+ assert_eq!(notice_in_message(&message("Plain mail", None)), None);
+ assert_eq!(
+ notice_in_message(&message("Rescheduled: Demo", None)),
+ Some(CalendarNotice::Change { until: fallback() })
+ );
+}
+
+#[test]
+fn saved_methods_tell_changes_from_new_invites_and_rsvps() {
+ let event_end = Utc.with_ymd_and_hms(2026, 11, 5, 16, 0, 0).unwrap();
+ for change in [
+ invite(InvitationMethod::Request, 1),
+ invite(InvitationMethod::Cancel, 0),
+ invite(InvitationMethod::Counter, 0),
+ ] {
+ assert_eq!(
+ CalendarPart::from_saved(&[change], received()),
+ Some(CalendarPart::Change { until: event_end })
+ );
+ }
+ for new in [
+ invite(InvitationMethod::Request, 0),
+ invite(InvitationMethod::Publish, 3),
+ invite(InvitationMethod::Unknown, 3),
+ ] {
+ assert_eq!(
+ CalendarPart::from_saved(&[new], received()),
+ Some(CalendarPart::NewInvite { until: event_end })
+ );
+ }
+ assert_eq!(
+ CalendarPart::from_saved(&[invite(InvitationMethod::Reply, 2)], received()),
+ Some(CalendarPart::Rsvp)
+ );
+ assert_eq!(CalendarPart::from_saved(&[], received()), None);
+ // A reply next to an invite doesn't make the message an RSVP.
+ assert_eq!(
+ CalendarPart::from_saved(
+ &[
+ invite(InvitationMethod::Reply, 0),
+ invite(InvitationMethod::Request, 0)
+ ],
+ received()
+ ),
+ Some(CalendarPart::NewInvite { until: event_end })
+ );
+}
+
+#[test]
+fn a_change_holds_until_the_last_changed_event_is_over() {
+ let mut later = invite(InvitationMethod::Request, 2);
+ later.end = zoned("2026-11-09T11:00:00-05:00");
+ // A new event beside it doesn't count, however far ahead.
+ let mut unchanged = invite(InvitationMethod::Request, 0);
+ unchanged.end = zoned("2027-01-01T00:00:00Z");
+ assert_eq!(
+ until(CalendarPart::from_saved(
+ &[invite(InvitationMethod::Cancel, 0), later, unchanged],
+ received()
+ )),
+ Utc.with_ymd_and_hms(2026, 11, 9, 16, 0, 0).unwrap()
+ );
+}
+
+#[test]
+fn a_change_subject_holds_until_the_last_saved_event_is_over() {
+ let mut later = invite(InvitationMethod::Request, 0);
+ later.end = zoned("2026-11-09T11:00:00-05:00");
+ assert_eq!(
+ CalendarPart::from_saved(&[invite(InvitationMethod::Request, 0), later], received()),
+ Some(CalendarPart::NewInvite {
+ until: Utc.with_ymd_and_hms(2026, 11, 9, 16, 0, 0).unwrap()
+ })
+ );
+}
+
+#[test]
+fn a_change_uses_the_events_end_else_its_start() {
+ let mut no_end = invite(InvitationMethod::Cancel, 0);
+ no_end.end = None;
+ assert_eq!(
+ until(CalendarPart::from_saved(&[no_end], received())),
+ Utc.with_ymd_and_hms(2026, 11, 5, 15, 0, 0).unwrap()
+ );
+
+ let mut unresolved_end = invite(InvitationMethod::Cancel, 0);
+ unresolved_end.end = Some(InvitationDateTime::Unresolved {
+ value: "2026-11-05T11:00:00".to_owned(),
+ time_zone: None,
+ });
+ assert_eq!(
+ until(CalendarPart::from_saved(&[unresolved_end], received())),
+ Utc.with_ymd_and_hms(2026, 11, 5, 15, 0, 0).unwrap()
+ );
+}
+
+#[test]
+fn an_all_day_event_is_over_once_its_day_has_ended_everywhere() {
+ // The end date is exclusive: this event lasts the 5th.
+ let mut all_day = invite(InvitationMethod::Request, 1);
+ all_day.start = date("2026-11-05");
+ all_day.end = date("2026-11-06");
+ assert_eq!(
+ until(CalendarPart::from_saved(&[all_day], received())),
+ Utc.with_ymd_and_hms(2026, 11, 6, 12, 0, 0).unwrap()
+ );
+
+ let mut start_only = invite(InvitationMethod::Request, 1);
+ start_only.start = date("2026-11-05");
+ start_only.end = None;
+ assert_eq!(
+ until(CalendarPart::from_saved(&[start_only], received())),
+ Utc.with_ymd_and_hms(2026, 11, 6, 12, 0, 0).unwrap()
+ );
+}
+
+#[test]
+fn a_change_with_no_single_end_holds_for_the_fallback() {
+ let mut recurring = invite(InvitationMethod::Request, 1);
+ recurring.recurrence = Some(vec!["RRULE:FREQ=WEEKLY".to_owned()]);
+ let mut from_before_recurrence_was_saved = invite(InvitationMethod::Request, 1);
+ from_before_recurrence_was_saved.recurrence = None;
+ let mut unresolved = invite(InvitationMethod::Request, 1);
+ let wall_time = InvitationDateTime::Unresolved {
+ value: "2026-11-05T11:00:00".to_owned(),
+ time_zone: Some("Mars/Olympus".to_owned()),
+ };
+ unresolved.start = Some(wall_time.clone());
+ unresolved.end = Some(wall_time);
+ let mut no_times = invite(InvitationMethod::Cancel, 0);
+ no_times.start = None;
+ no_times.end = None;
+ let mut unreadable = invite(InvitationMethod::Cancel, 0);
+ unreadable.start = zoned("not a time");
+ unreadable.end = date("not a date");
+ for change in [
+ recurring,
+ from_before_recurrence_was_saved,
+ unresolved,
+ no_times,
+ unreadable,
+ ] {
+ assert_eq!(
+ until(CalendarPart::from_saved(&[change], received())),
+ fallback()
+ );
+ }
+
+ // One occurrence of a series has its own end.
+ let mut occurrence = invite(InvitationMethod::Cancel, 0);
+ occurrence.recurrence_id = zoned("2026-11-05T10:00:00-05:00");
+ assert_eq!(
+ until(CalendarPart::from_saved(&[occurrence], received())),
+ Utc.with_ymd_and_hms(2026, 11, 5, 16, 0, 0).unwrap()
+ );
+}
+
+#[test]
+fn the_sweeps_pattern_matches_every_change_prefix_and_nothing_else() {
+ let pattern = change_subject_pattern();
+ assert!(pattern.starts_with("^("));
+ assert!(pattern.ends_with(r")\s*:"));
+ for prefix in CHANGE_PREFIXES {
+ assert!(pattern.contains(prefix), "{prefix}");
+ }
+ for prefix in NEW_INVITE_PREFIXES.iter().chain(&RSVP_PREFIXES) {
+ assert!(
+ !pattern.split(['(', '|', ')']).any(|form| form == *prefix),
+ "{prefix}"
+ );
+ }
+}
diff --git a/crates/email/src/domain/focus/input.rs b/crates/email/src/domain/focus/input.rs
index e7af63fe7e..d3334c79de 100644
--- a/crates/email/src/domain/focus/input.rs
+++ b/crates/email/src/domain/focus/input.rs
@@ -26,6 +26,35 @@ const BRUSH_OFF_PHRASES: [&str; 5] = [
"remove me",
];
+/// Messages that end the exchange ("Thanks!", "Got it"): nobody owes a reply to
+/// one, though unlike a brush-off it says nothing bad about the relationship.
+const CLOSING_PHRASES: [&str; 20] = [
+ "thanks",
+ "thanks so much",
+ "thanks a lot",
+ "thanks very much",
+ "thank you",
+ "thank you so much",
+ "thank you very much",
+ "many thanks",
+ "thx",
+ "ty",
+ "cheers",
+ "got it",
+ "sounds good",
+ "will do",
+ "noted",
+ "great",
+ "perfect",
+ "awesome",
+ "ok",
+ "okay",
+];
+/// Words a closing line can carry after its phrase: a name, as in "Thanks Ava!".
+const CLOSING_LINE_EXTRA_WORDS: usize = 1;
+/// Words a later line can have and still read as a signature.
+const SIGNATURE_LINE_WORDS: usize = 3;
+
/// The part of a plain-text body above the quoted history.
pub fn new_text(body: &str) -> String {
let mut kept = Vec::new();
@@ -63,6 +92,49 @@ pub fn is_dismissive_reply(text: &str) -> bool {
.any(|phrase| contains_words(&lowered, phrase))
}
+/// Whether a line after an acknowledgement reads as a name or title rather than
+/// a request: short, with every word capitalized ("Alex Smith", not "Please
+/// confirm").
+fn reads_as_signature(line: &str) -> bool {
+ line.split_whitespace().count() <= SIGNATURE_LINE_WORDS
+ && line
+ .split_whitespace()
+ .all(|word| !word.starts_with(char::is_lowercase))
+}
+
+/// Whether a message only ends the exchange, so nobody owes a reply to it: a
+/// brush-off, or an acknowledgement and nothing but a signature after it. When
+/// in doubt it says no: a stray follow-up costs less than a missed one.
+pub(super) fn is_closing_reply(text: &str) -> bool {
+ if is_dismissive_reply(text) {
+ return true;
+ }
+ if text.contains('?') {
+ return false;
+ }
+ let mut lines = text.lines().map(str::trim).filter(|line| !line.is_empty());
+ let Some(first_line) = lines.next() else {
+ return false;
+ };
+ let first_line = first_line.to_lowercase();
+ let first_line = first_line.trim_end_matches(['.', '!', ',']);
+ let closes = CLOSING_PHRASES.iter().any(|phrase| {
+ first_line.strip_prefix(phrase).is_some_and(|rest| {
+ let is_whole_phrase = !rest.starts_with(char::is_alphanumeric);
+ let extra_words = rest
+ .trim_start_matches([',', '.', '!', '-', ' '])
+ .split_whitespace()
+ .count();
+ is_whole_phrase && extra_words <= CLOSING_LINE_EXTRA_WORDS
+ })
+ });
+ // Anything past a signature delimiter is signature, whatever it says.
+ closes
+ && lines
+ .take_while(|line| !line.starts_with("--"))
+ .all(reads_as_signature)
+}
+
/// `phrase` appears in `text` as whole words.
fn contains_words(text: &str, phrase: &str) -> bool {
text.match_indices(phrase).any(|(start, matched)| {
diff --git a/crates/email/src/domain/focus/input/test.rs b/crates/email/src/domain/focus/input/test.rs
index 42f7ed4a4a..f9415c536a 100644
--- a/crates/email/src/domain/focus/input/test.rs
+++ b/crates/email/src/domain/focus/input/test.rs
@@ -16,6 +16,7 @@ fn message(body: &str, is_sent: bool) -> FocusMessage {
subject: Some("Contract".to_owned()),
has_attachments: false,
bulk: false,
+ calendar_part: None,
body: Some(body.to_owned()),
snippet: Some("snippet".to_owned()),
}
@@ -30,7 +31,7 @@ fn thread(messages: Vec<FocusMessage>) -> FocusThread {
owner_email: "[email protected]".to_owned(),
is_signal: true,
inbox_visible: true,
- classified_message_id: None,
+ classified: None,
messages,
sent_notes: Vec::new(),
}
@@ -45,6 +46,40 @@ fn new_text_drops_quoted_history() {
assert_eq!(new_text("Hi\n-----Original Message-----\nFrom: x"), "Hi");
}
+#[test]
+fn closing_replies_owe_nobody_an_answer() {
+ for reply in [
+ "Thanks!",
+ "Thank you so much",
+ "thx",
+ "Got it, thanks",
+ "Sounds good.",
+ "Thanks Ava!\n\nOwner\nFounder, Acme",
+ "Thanks!\nAlex Smith",
+ "Thanks,\nBest,\nOwner",
+ "Thanks!\n\n-- \nOwner Name\nFounder, Acme",
+ "No thanks",
+ ] {
+ assert!(is_closing_reply(reply), "{reply}");
+ }
+ for reply in [
+ "",
+ "Thanks, can you send the contract?",
+ "Thanks!\n\nAlso, when can you start?",
+ "Thanks, please send pricing",
+ "Thanks!\nPlease send the signed contract.",
+ "Thanks!\nPlease confirm",
+ "Thanks!\nPlease send pricing",
+ "Ok, send me the contract",
+ "Thanks for the intro. I would love to hear how the pilot went and what you would change.",
+ "Thanksgiving works for us",
+ "Okay to send the draft Monday morning to the whole team",
+ "Here is the contract to sign",
+ ] {
+ assert!(!is_closing_reply(reply), "{reply}");
+ }
+}
+
#[test]
fn brush_offs() {
for reply in [
diff --git a/crates/email/src/domain/focus/models.rs b/crates/email/src/domain/focus/models.rs
index f543c875a9..fcfe675a92 100644
--- a/crates/email/src/domain/focus/models.rs
+++ b/crates/email/src/domain/focus/models.rs
@@ -8,9 +8,10 @@ use uuid::Uuid;
use super::questions::FocusQuestion;
-/// Why a thread is in or out of Focus. The first four are Focus categories;
-/// the rest name the reason a thread was left out. None depends on the owner's
-/// line of work: who the mail is from comes from their own history.
+/// Why a thread is in or out of Focus. The first four, and Calendar for a change
+/// to an event that isn't over, are Focus categories; the rest name the reason a
+/// thread was left out. None depends on the owner's line of work: who the mail is
+/// from comes from their own history.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Display, EnumString)]
#[serde(rename_all = "snake_case")]
#[strum(serialize_all = "snake_case")]
@@ -23,7 +24,8 @@ pub enum FocusCategory {
Known,
/// Relevant mail that fits no narrower category.
Other,
- /// A calendar invite, update or RSVP.
+ /// In Focus: a reschedule, update, cancellation or proposed new time for an
+ /// event that isn't over. Left out: a new invite or an RSVP notice.
Calendar,
/// Unsolicited outreach: a pitch, recruiting or a job application.
Outreach,
@@ -44,7 +46,8 @@ pub struct FocusAnswers {
pub automated: f32,
/// Is it a customer, client or user asking for help or giving feedback?
pub customer: f32,
- /// Is it only a calendar invite, update or RSVP?
+ /// Is it only a calendar invite, update or RSVP? Stored, but the rule doesn't
+ /// read it: the one question can't tell a change from a new invite or an RSVP.
pub calendar_rsvp: f32,
/// Does it ask for a decision only the owner can make?
pub needs_decision: f32,
@@ -80,6 +83,44 @@ impl FocusAnswers {
}
}
+/// The calendar notice a thread's latest message is.
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
+#[serde(tag = "kind", rename_all = "snake_case")]
+pub enum CalendarNotice {
+ /// A reschedule, update, cancellation or proposed new time: the owner's
+ /// time moves.
+ Change {
+ /// When the event is over, or a fixed while after the notice when
+ /// nothing says.
+ until: DateTime<Utc>,
+ },
+ /// A first invitation to an event.
+ NewInvite,
+ /// An attendee's accepted, declined or tentative notice.
+ Rsvp,
+}
+
+/// What a message's calendar content says, before its subject is considered.
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub enum CalendarPart {
+ /// A saved component that cancels an event, proposes a new time, or updates
+ /// one past its first revision.
+ Change {
+ /// When the event is over, or a fixed while after the notice when
+ /// nothing says.
+ until: DateTime<Utc>,
+ },
+ /// Saved components that invite or publish, and change nothing.
+ NewInvite {
+ /// When the event is over, should the message's subject say it changed.
+ until: DateTime<Utc>,
+ },
+ /// Saved components that are only attendees' replies.
+ Rsvp,
+ /// A calendar file with no saved component to read.
+ Unparsed,
+}
+
/// What the owner's history says about a thread, independent of its words.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct FocusSignals {
@@ -87,28 +128,70 @@ pub struct FocusSignals {
/// replied in this thread, or a teammate is on it. Brushed-off senders
/// never count, and neither does a teammate on bulk mail.
pub contact: bool,
+ /// The owner wrote to a sender for real within the recent window. Stricter
+ /// than `contact`: dated, and bulk or automated senders never count, even
+ /// ones the owner once replied to.
+ pub recent_contact: bool,
+ /// The owner has only ever turned the thread's first sender away.
+ pub brushed_off: bool,
/// The owner sent anything in the thread after its first incoming message.
pub replied: bool,
/// A sender shares the owner's company domain.
pub teammate: bool,
/// The thread's latest message is the owner's.
pub latest_from_owner: bool,
- /// The subject reads as a calendar invite or RSVP.
- pub calendar_subject: bool,
+ /// The notice the thread's latest message is. Anyone writing after a notice
+ /// makes the thread a conversation, so it counts only as the latest message.
+ pub calendar: Option<CalendarNotice>,
+ /// Someone wrote to the owner, the owner answered last, and that answer
+ /// expects a reply from someone who can give one. "Thanks!" does not.
+ pub awaiting_reply: bool,
+}
+
+impl FocusSignals {
+ /// When the calendar change that is the thread's latest message stops holding
+ /// it in Focus.
+ pub fn calendar_change_until(&self) -> Option<DateTime<Utc>> {
+ match self.calendar {
+ Some(CalendarNotice::Change { until }) => Some(until),
+ Some(CalendarNotice::NewInvite | CalendarNotice::Rsvp) | None => None,
+ }
+ }
+
+ /// Mail worth classifying even when it isn't in the signal inbox. A new
+ /// invite or a change to an event already over is not.
+ pub fn is_key_mail(&self, now: DateTime<Utc>) -> bool {
+ self.calendar_change_until()
+ .is_some_and(|until| until > now)
+ || self.recent_contact
+ || self.awaiting_reply
+ }
}
/// The rule's decision about one thread.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct FocusVerdict {
- /// Whether the thread belongs in Focus.
+ /// Whether the thread belongs in Focus whatever the time. A thread awaiting
+ /// a reply also joins once it has waited long enough, and a calendar change
+ /// holds one until `calendar_hold_until`.
pub is_focus: bool,
- /// The Focus category, or why the thread was left out.
+ /// Key mail: in Focus even when the thread is not in the signal inbox.
+ pub pinned: bool,
+ /// A calendar change holds the thread in Focus until this time, in the signal
+ /// inbox or not, as long as nobody writes after it.
+ pub calendar_hold_until: Option<DateTime<Utc>>,
+ /// The Focus category, or why the thread was left out, apart from a calendar
+ /// hold: the read calls a thread Calendar while one lasts.
pub category: FocusCategory,
- /// A Focus thread whose latest message waits on the owner's answer.
+ /// A Focus thread whose latest message is someone else's and waits on the
+ /// owner. Never set together with `needs_follow_up`.
pub needs_reply: bool,
- /// A Focus thread with an open loop to come back to.
+ /// A Focus thread whose latest message is the owner's, with an open loop to
+ /// come back to. Never set together with `needs_reply`.
pub needs_follow_up: bool,
- /// Sort key for the Focus list, 0 to 100.
+ /// The owner sent the latest message and expects an answer.
+ pub awaiting_reply: bool,
+ /// How much the thread matters, 0 to 100.
pub importance: u8,
}
@@ -135,6 +218,8 @@ pub struct FocusMessage {
pub has_attachments: bool,
/// Whether its headers mark it as mailing-list or bulk mail.
pub bulk: bool,
+ /// Calendar content the message carries.
+ pub calendar_part: Option<CalendarPart>,
/// Plain-text body, truncated by the store.
pub body: Option<String>,
/// Provider snippet.
@@ -146,12 +231,27 @@ pub struct FocusMessage {
pub struct SentNote {
/// The recipient, lowercased.
pub recipient: String,
+ /// When it was sent.
+ pub at: DateTime<Utc>,
/// Plain-text body, truncated by the store.
pub body: Option<String>,
}
+/// The classification stored for a thread.
+#[derive(Debug, Clone, PartialEq)]
+pub struct StoredClassification {
+ /// The message it is based on.
+ pub message_id: Uuid,
+ /// The rule version that decided it.
+ pub rule_version: i16,
+ /// Jev's answers, when they still decode.
+ pub answers: Option<FocusAnswers>,
+ /// The model that answered.
+ pub model: String,
+}
+
/// Where a thread lives and how it stands; read before any message content.
-#[derive(Debug, Clone, PartialEq, Eq)]
+#[derive(Debug, Clone, PartialEq)]
pub struct FocusInbox {
/// Thread row id.
pub thread_id: Uuid,
@@ -165,8 +265,8 @@ pub struct FocusInbox {
pub is_signal: bool,
/// Whether the thread is still in the inbox.
pub inbox_visible: bool,
- /// Message the stored result is based on, when one exists.
- pub classified_message_id: Option<Uuid>,
+ /// The stored classification, when one exists.
+ pub classified: Option<StoredClassification>,
}
/// A thread's mail and the owner's sent mail to its senders.
@@ -193,8 +293,8 @@ pub struct FocusThread {
pub is_signal: bool,
/// Whether the thread is still in the inbox.
pub inbox_visible: bool,
- /// Message the stored result is based on, when one exists.
- pub classified_message_id: Option<Uuid>,
+ /// The stored classification, when one exists.
+ pub classified: Option<StoredClassification>,
/// Non-draft, non-trashed messages, oldest first.
pub messages: Vec<FocusMessage>,
/// The owner's sent mail to this thread's senders, newest first.
@@ -211,14 +311,14 @@ impl FocusThread {
owner_email: inbox.owner_email,
is_signal: inbox.is_signal,
inbox_visible: inbox.inbox_visible,
- classified_message_id: inbox.classified_message_id,
+ classified: inbox.classified,
messages: mail.messages,
sent_notes: mail.sent_notes,
}
}
}
-/// A signal thread whose stored classification may be out of date.
+/// An inbox thread whose stored classification may be out of date.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct StaleThread {
/// Thread row id.
@@ -248,18 +348,21 @@ pub struct FocusRecord {
pub classified_at: DateTime<Utc>,
}
-/// A stored classification, as the Focus view reads it.
+/// A stored classification as it stands when read: who wrote last, how long the
+/// owner has waited and whether a changed event is over are applied to what was
+/// stored.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ThreadFocus {
/// Whether the thread belongs in Focus.
pub is_focus: bool,
/// The Focus category, or why the thread was left out.
pub category: FocusCategory,
- /// Sort key, 0 to 100.
+ /// How much the thread matters, 0 to 100.
pub importance: u8,
- /// The latest message waits on the owner's answer.
+ /// The latest message is someone else's and waits on the owner's answer.
pub needs_reply: bool,
- /// An open loop to come back to.
+ /// The latest message is the owner's, and the thread has an open loop or
+ /// has waited past the threshold for an answer.
pub needs_follow_up: bool,
/// When the classification was made.
pub classified_at: DateTime<Utc>,
diff --git a/crates/email/src/domain/focus/ports.rs b/crates/email/src/domain/focus/ports.rs
index 873e1cb7dc..de0ddfe5a7 100644
--- a/crates/email/src/domain/focus/ports.rs
+++ b/crates/email/src/domain/focus/ports.rs
@@ -29,31 +29,41 @@ pub trait FocusStore: Send + Sync + 'static {
/// already stored. Returns whether it was stored.
fn save(&self, record: &FocusRecord) -> impl Future<Output = Result<bool, Report>> + Send;
- /// Inbox signal threads with mail since `since` and no classification of
- /// their latest message, newest first, from inboxes whose address is at
- /// one of `domains`.
+ /// Inbox threads that may belong in Focus with mail since `since` and no
+ /// classification of their latest message under the current rule, newest
+ /// first, from inboxes whose address is at one of `domains`. `after` is the
+ /// last thread of the previous page.
fn stale_threads(
&self,
since: DateTime<Utc>,
domains: &[String],
+ after: Option<StaleThread>,
limit: i64,
) -> impl Future<Output = Result<Vec<StaleThread>, Report>> + Send;
/// The owner's Focus threads still in the inbox with mail since `since`,
- /// highest priority first: importance, halved for every 2 days since the
- /// latest inbound mail.
+ /// newest first by latest message time, in either direction. Threads whose
+ /// owner has been awaiting a reply since at or before `waiting_before` count,
+ /// as do threads a calendar change still holds at `now`.
fn focus_thread_ids(
&self,
owner: &MacroUserIdStr<'static>,
since: DateTime<Utc>,
+ waiting_before: DateTime<Utc>,
+ now: DateTime<Utc>,
limit: i64,
) -> impl Future<Output = Result<Vec<Uuid>, Report>> + Send;
- /// Stored classifications of the owner's threads among `thread_ids`.
+ /// Classifications of the owner's threads among `thread_ids`, as they stand
+ /// at `now`: who wrote last settles the labels, a thread whose owner has been
+ /// awaiting a reply since at or before `waiting_before` is in Focus, and one a
+ /// calendar change still holds is in Focus as Calendar.
fn focus_for_threads(
&self,
owner: &MacroUserIdStr<'static>,
thread_ids: &[Uuid],
+ waiting_before: DateTime<Utc>,
+ now: DateTime<Utc>,
) -> impl Future<Output = Result<HashMap<Uuid, ThreadFocus>, Report>> + Send;
}
diff --git a/crates/email/src/domain/focus/rule.rs b/crates/email/src/domain/focus/rule.rs
index ea9f3a4667..5e0582ed32 100644
--- a/crates/email/src/domain/focus/rule.rs
+++ b/crates/email/src/domain/focus/rule.rs
@@ -1,6 +1,11 @@
//! The deterministic rule that turns Jev's answers into a Focus verdict.
-use super::models::{FocusAnswers, FocusCategory, FocusSignals, FocusVerdict};
+use super::models::{CalendarNotice, FocusAnswers, FocusCategory, FocusSignals, FocusVerdict};
+
+/// The rule's version, stored with each verdict. Bump it when [`decide`] or the
+/// signals change: the next sweep re-decides rows stored under an older version
+/// from their stored answers, without asking Jev again.
+pub const FOCUS_RULE_VERSION: i16 = 1;
/// A "yes" from Jev.
const YES: f32 = 0.5;
@@ -10,72 +15,90 @@ const CONTACT_FOCUS: f32 = 0.3;
/// profile that asks for such mail (a recruiter's applications) clears it;
/// on the experiment's founder inbox no stranger outreach scored above 0.5.
const OUTREACH_FOCUS: f32 = 0.6;
-/// Jev's calendar answer only vetoes a thread when it is this sure; below it,
-/// it flagged real conversations that merely mentioned an invite.
-const CALENDAR: f32 = 0.7;
/// The bar for the reply-needed and follow-up flags. With the owner's memory
/// as context Jev leans towards "yes", so a plain yes flagged half of Focus.
const FLAG: f32 = 0.7;
-/// Subject prefixes Google Calendar and Outlook put on invites and RSVPs.
-const CALENDAR_PREFIXES: [&str; 10] = [
- "accepted",
- "declined",
- "tentatively accepted",
- "invitation",
- "updated invitation",
- "updated invitation with note",
- "canceled",
- "canceled event",
- "new event",
- "event updated",
-];
+/// A new invite is out of Focus, like an RSVP. Set this to false and a new invite
+/// takes the normal rule instead, as an ordinary thread.
+const NEW_INVITES_STAY_OUT: bool = true;
-/// Importance weights; they sum to 1.
+/// Importance weights. A thread needs a reply or a follow-up, never both, so the
+/// largest score is 1.
const WEIGHT_FOCUS: f32 = 0.55;
-const WEIGHT_REPLY: f32 = 0.15;
-const WEIGHT_FOLLOW_UP: f32 = 0.10;
+const WEIGHT_REPLY: f32 = 0.25;
+const WEIGHT_FOLLOW_UP: f32 = 0.15;
const WEIGHT_DECISION: f32 = 0.10;
const WEIGHT_CONTACT: f32 = 0.10;
-/// Whether a subject is a calendar invite or RSVP, e.g. "Accepted: Demo @ Fri".
-pub fn is_calendar_subject(subject: &str) -> bool {
- let subject = subject.to_lowercase();
- CALENDAR_PREFIXES.iter().any(|prefix| {
- subject
- .strip_prefix(prefix)
- .is_some_and(|rest| rest.trim_start().starts_with(':'))
- })
-}
+/// Lowest importance by why a thread is in Focus: mail in for a reason Jev's
+/// relevance score doesn't measure must not read as unimportant.
+const REPLY_FLOOR: u8 = 70;
+const FOLLOW_UP_FLOOR: u8 = 55;
+const KEY_FLOOR: u8 = 50;
+const FOCUS_FLOOR: u8 = 30;
/// Decide whether a thread belongs in Focus, why, and how important it is.
///
-/// A thread is left out when it is a calendar notice or automated mail nobody
-/// vouches for. Otherwise it is in when Jev says the owner, as their profile
-/// describes them, would want it. Unsolicited outreach from strangers needs a
-/// firmer yes. Customers, and contacts Jev finds at least somewhat relevant,
-/// need less, unless the mail is outreach.
+/// Key mail is always in: mail waiting on the owner's reply, and mail from recent
+/// contacts, unless it is outreach, automated, or from someone the owner turned
+/// away. Everything else is in when Jev says the owner, as their profile describes
+/// them, would want it, unless it is automated mail nobody vouches for. Unsolicited
+/// outreach from strangers needs a firmer yes. Customers, and contacts Jev finds at
+/// least somewhat relevant, need less, unless the mail is outreach.
+///
+/// Calendar mail counts only when it is the thread's latest message; once anyone
+/// has written after it, the thread is a conversation like any other. A change (a
+/// reschedule, update, cancellation or proposed new time) is held in Focus until
+/// its event is over, whatever else it reads as. A new invite and an RSVP are out.
+/// Jev's calendar answer plays no part: it can't tell these apart, so a thread only
+/// Jev calls calendar mail takes the rules above and reads as its sender's category.
+///
+/// Time is not known here, so `is_focus` leaves out what time decides. A thread the
+/// owner is waiting on joins Focus once it has waited long enough, and a calendar
+/// change holds one until `calendar_hold_until`; the list applies both when it
+/// reads, since nothing reclassifies a thread as it ages. Its labels and importance
+/// are set for those moments here, and the read calls a held thread Calendar, so
+/// Calendar in Focus only ever means a change still ahead of its event.
pub fn decide(answers: &FocusAnswers, signals: &FocusSignals) -> FocusVerdict {
- let calendar = signals.calendar_subject || answers.calendar_rsvp >= CALENDAR;
+ let calendar_hold_until = signals.calendar_change_until();
+ let left_out = match signals.calendar {
+ Some(CalendarNotice::Rsvp) => true,
+ Some(CalendarNotice::NewInvite) => NEW_INVITES_STAY_OUT,
+ Some(CalendarNotice::Change { .. }) | None => false,
+ };
+
let customer = answers.customer >= YES;
let outreach = answers.outreach >= YES;
+ let reads_automated = answers.automated >= YES;
let vouched = signals.contact && answers.focus >= YES;
- let automated = answers.automated >= YES && !customer && !signals.replied && !vouched;
+ let automated = reads_automated && !customer && !signals.replied && !vouched;
let stranger = !signals.contact && !signals.replied;
let focus_bar = if outreach && stranger {
OUTREACH_FOCUS
} else {
YES
};
-
- let is_focus = !calendar
- && !automated
+ let earned = !automated
&& (answers.focus >= focus_bar
|| (!outreach && (customer || (signals.contact && answers.focus >= CONTACT_FOCUS))));
- let needs_reply = is_focus && !signals.latest_from_owner && answers.needs_response >= FLAG;
- let needs_follow_up = is_focus && answers.needs_follow_up >= FLAG;
- let category = if is_focus {
+ let asks_owner = !signals.latest_from_owner && answers.needs_response >= FLAG;
+ let key_sender = !outreach && !reads_automated && !signals.brushed_off;
+ let pinned = !left_out && key_sender && (asks_owner || signals.recent_contact);
+ let is_focus = !left_out && (pinned || earned);
+ let in_focus_while_held = is_focus || calendar_hold_until.is_some();
+
+ // Who wrote last settles the label, whichever way Jev flagged the thread:
+ // nobody follows up on mail still waiting on the owner's own answer.
+ let open_loop = answers.needs_follow_up >= FLAG;
+ let needs_reply =
+ in_focus_while_held && !signals.latest_from_owner && (asks_owner || open_loop);
+ let needs_follow_up = in_focus_while_held && signals.latest_from_owner && open_loop;
+
+ let category = if left_out {
+ FocusCategory::Calendar
+ } else if is_focus || signals.awaiting_reply {
if customer {
FocusCategory::Customer
} else if signals.teammate {
@@ -85,8 +108,6 @@ pub fn decide(answers: &FocusAnswers, signals: &FocusSignals) -> FocusVerdict {
} else {
FocusCategory::Other
}
- } else if calendar {
- FocusCategory::Calendar
} else if outreach {
FocusCategory::Outreach
} else if automated {
@@ -100,14 +121,28 @@ pub fn decide(answers: &FocusAnswers, signals: &FocusSignals) -> FocusVerdict {
+ WEIGHT_FOLLOW_UP * f32::from(u8::from(needs_follow_up))
+ WEIGHT_DECISION * f32::from(u8::from(answers.needs_decision >= YES))
+ WEIGHT_CONTACT * f32::from(u8::from(signals.contact));
+ let floor = if needs_reply {
+ REPLY_FLOOR
+ } else if needs_follow_up || signals.awaiting_reply {
+ FOLLOW_UP_FLOOR
+ } else if pinned || calendar_hold_until.is_some() || (is_focus && customer) {
+ KEY_FLOOR
+ } else if is_focus {
+ FOCUS_FLOOR
+ } else {
+ 0
+ };
// Answers are probabilities, so the score already sits in 0..=1.
- let importance = (score.clamp(0.0, 1.0) * 100.0).round() as u8;
+ let importance = ((score.clamp(0.0, 1.0) * 100.0).round() as u8).max(floor);
FocusVerdict {
is_focus,
+ pinned,
+ calendar_hold_until,
category,
needs_reply,
needs_follow_up,
+ awaiting_reply: signals.awaiting_reply,
importance,
}
}
diff --git a/crates/email/src/domain/focus/rule/experiment.csv b/crates/email/src/domain/focus/rule/experiment.csv
index 5a676119b4..ebfa2016f8 100644
--- a/crates/email/src/domain/focus/rule/experiment.csv
+++ b/crates/email/src/domain/focus/rule/experiment.csv
@@ -1,5 +1,5 @@
focus,outreach,automated,customer,calendar_rsvp,needs_decision,needs_response,needs_follow_up,contact,replied,teammate,latest_from_owner,calendar_subject,expect_is_focus,expect_needs_reply,expect_needs_follow_up
-0.52,0.19,0.08,0.48,0.05,0.3,0.13,0.72,1,1,0,0,0,1,0,1
+0.52,0.19,0.08,0.48,0.05,0.3,0.13,0.72,1,1,0,0,0,1,1,0
0.08,0.95,0.15,0.03,0.01,0.14,0.84,0.07,0,0,0,0,0,0,0,0
0.17,0.95,0.07,0.05,0.01,0.23,0.81,0.16,0,0,0,0,0,0,0,0
0.07,0.96,0.62,0.02,0.02,0.15,0.84,0.06,0,0,0,0,0,0,0,0
@@ -13,7 +13,7 @@ focus,outreach,automated,customer,calendar_rsvp,needs_decision,needs_response,ne
0.07,0.96,0.3,0.03,0.01,0.13,0.83,0.08,0,0,0,0,0,0,0,0
0.09,0.96,0.69,0.03,0.01,0.15,0.83,0.07,0,0,0,0,0,0,0,0
0.07,0.95,0.61,0.02,0.02,0.25,0.79,0.06,0,0,0,0,0,0,0,0
-0.68,0.03,0.06,0.52,0.04,0.14,0.08,0.7,1,0,0,0,0,1,0,1
+0.68,0.03,0.06,0.52,0.04,0.14,0.08,0.7,1,0,0,0,0,1,1,0
0.28,0.06,0.62,0.93,0.02,0.27,0.63,0.29,0,0,0,0,0,1,0,0
0.34,0.11,0.49,0.79,0.02,0.21,0.83,0.4,0,0,0,0,0,1,1,0
0.43,0.12,0.82,0.28,0.02,0.19,0.12,0.19,0,0,0,0,0,0,0,0
@@ -24,10 +24,10 @@ focus,outreach,automated,customer,calendar_rsvp,needs_decision,needs_response,ne
0.08,0.98,0.78,0.02,0.01,0.14,0.75,0.05,0,0,0,0,0,0,0,0
0.08,0.91,0.8,0.03,0.03,0.19,0.53,0.09,0,0,0,0,0,0,0,0
0.07,0.96,0.45,0.04,0.02,0.17,0.87,0.08,0,0,0,0,0,0,0,0
-0.31,0.23,0.14,0.17,0.02,0.29,0.92,0.32,0,0,0,0,0,0,0,0
+0.31,0.23,0.14,0.17,0.02,0.29,0.92,0.32,0,0,0,0,0,1,1,0
0.08,0.96,0.74,0.03,0.02,0.1,0.42,0.06,0,0,0,0,0,0,0,0
-0.76,0.02,0.92,0.03,0.94,0.2,0.06,0.33,1,0,1,0,1,0,0,0
-0.86,0.03,0.19,0.03,0.01,0.42,0.14,0.75,0,0,0,0,0,1,0,1
+0.76,0.02,0.92,0.03,0.94,0.2,0.06,0.33,1,0,1,0,1,1,0,0
+0.86,0.03,0.19,0.03,0.01,0.42,0.14,0.75,0,0,0,0,0,1,1,0
0.2,0.14,0.64,0.08,0.01,0.26,0.57,0.16,0,0,0,0,0,0,0,0
0.32,0.09,0.16,0.67,0.04,0.57,0.87,0.48,0,0,0,0,0,1,1,0
0.07,0.57,0.86,0.02,0.02,0.07,0.6,0.05,0,0,0,0,0,0,0,0
@@ -43,7 +43,7 @@ focus,outreach,automated,customer,calendar_rsvp,needs_decision,needs_response,ne
0.4,0.94,0.11,0.03,0.02,0.4,0.47,0.2,0,0,0,0,0,0,0,0
0.82,0.02,0.64,0.04,0.75,0.26,0.06,0.66,0,0,0,0,0,0,0,0
0.08,0.97,0.56,0.02,0.02,0.15,0.09,0.05,0,0,0,0,0,0,0,0
-0.75,0.02,0.79,0.03,0.89,0.22,0.07,0.28,1,0,1,0,1,0,0,0
+0.75,0.02,0.79,0.03,0.89,0.22,0.07,0.28,1,0,1,0,1,1,0,0
0.06,0.98,0.67,0.02,0.01,0.09,0.82,0.05,0,0,0,0,0,0,0,0
0.08,0.97,0.65,0.02,0.01,0.14,0.83,0.07,0,0,0,0,0,0,0,0
0.07,0.96,0.38,0.03,0.01,0.08,0.59,0.07,0,0,0,0,0,0,0,0
@@ -52,37 +52,37 @@ focus,outreach,automated,customer,calendar_rsvp,needs_decision,needs_response,ne
0.1,0.94,0.27,0.04,0.02,0.31,0.74,0.13,0,1,0,0,0,0,0,0
0.08,0.97,0.9,0.02,0.02,0.1,0.4,0.04,0,0,0,0,0,0,0,0
0.62,0.06,0.07,0.29,0.01,0.28,0.37,0.38,1,0,1,0,0,1,0,0
-0.78,0.03,0.9,0.02,0.95,0.11,0.06,0.45,0,0,1,0,1,0,0,0
-0.68,0.04,0.09,0.05,0.14,0.15,0.07,0.78,1,0,1,0,0,1,0,1
+0.78,0.03,0.9,0.02,0.95,0.11,0.06,0.45,0,0,1,0,1,1,0,0
+0.68,0.04,0.09,0.05,0.14,0.15,0.07,0.78,1,0,1,0,0,1,1,0
0.16,0.97,0.9,0.03,0.01,0.07,0.37,0.05,0,0,0,0,0,0,0,0
0.09,0.97,0.75,0.02,0.01,0.14,0.79,0.05,0,0,0,0,0,0,0,0
0.08,0.96,0.52,0.03,0.02,0.26,0.81,0.13,0,0,0,0,0,0,0,0
-0.87,0.03,0.56,0.03,0.82,0.33,0.11,0.72,1,0,1,0,1,0,0,0
+0.87,0.03,0.56,0.03,0.82,0.33,0.11,0.72,1,0,1,0,1,1,1,0
0.1,0.65,0.39,0.05,0.05,0.45,0.79,0.31,1,0,1,0,0,0,0,0
0.18,0.96,0.12,0.03,0.01,0.28,0.56,0.11,0,0,0,0,0,0,0,0
0.07,0.98,0.75,0.02,0.01,0.15,0.89,0.06,0,0,0,0,0,0,0,0
0.08,0.92,0.61,0.07,0.03,0.21,0.83,0.08,0,0,0,0,0,0,0,0
-0.45,0.1,0.92,0.09,0.95,0.1,0.04,0.17,0,0,0,0,1,0,0,0
-0.78,0.02,0.64,0.02,0.77,0.38,0.09,0.38,1,0,1,0,1,0,0,0
+0.45,0.1,0.92,0.09,0.95,0.1,0.04,0.17,0,0,0,0,1,1,0,0
+0.78,0.02,0.64,0.02,0.77,0.38,0.09,0.38,1,0,1,0,1,1,0,0
0.07,0.98,0.89,0.02,0.01,0.14,0.81,0.06,0,0,0,0,0,0,0,0
0.12,0.73,0.96,0.04,0.01,0.05,0.08,0.07,0,0,0,0,0,0,0,0
0.74,0.03,0.1,0.04,0.02,0.12,0.09,0.24,1,1,1,1,0,1,0,0
0.07,0.93,0.62,0.03,0.02,0.13,0.77,0.07,0,0,0,0,0,0,0,0
-0.73,0.02,0.78,0.03,0.92,0.54,0.1,0.63,0,0,1,0,1,0,0,0
+0.73,0.02,0.78,0.03,0.92,0.54,0.1,0.63,0,0,1,0,1,1,0,0
0.07,0.96,0.45,0.03,0.01,0.21,0.83,0.08,0,0,0,0,0,0,0,0
0.37,0.37,0.05,0.05,0.02,0.13,0.12,0.8,1,1,0,1,0,1,0,1
-0.8,0.03,0.03,0.08,0.11,0.74,0.93,0.79,1,0,1,0,0,1,1,1
+0.8,0.03,0.03,0.08,0.11,0.74,0.93,0.79,1,0,1,0,0,1,1,0
0.09,0.96,0.1,0.04,0.01,0.19,0.82,0.09,0,0,0,0,0,0,0,0
-0.73,0.05,0.73,0.19,0.87,0.36,0.08,0.49,1,0,1,0,1,0,0,0
+0.73,0.05,0.73,0.19,0.87,0.36,0.08,0.49,1,0,1,0,1,1,0,0
0.63,0.09,0.29,0.08,0.24,0.65,0.91,0.69,1,0,0,0,0,1,1,0
-0.9,0.03,0.04,0.04,0.02,0.73,0.6,0.79,1,0,1,0,0,1,0,1
-0.82,0.03,0.74,0.03,0.9,0.27,0.07,0.62,1,0,1,0,1,0,0,0
+0.9,0.03,0.04,0.04,0.02,0.73,0.6,0.79,1,0,1,0,0,1,1,0
+0.82,0.03,0.74,0.03,0.9,0.27,0.07,0.62,1,0,1,0,1,1,0,0
0.08,0.95,0.44,0.06,0.02,0.14,0.57,0.09,0,0,0,0,0,0,0,0
0.26,0.96,0.11,0.03,0.01,0.38,0.73,0.16,0,0,0,0,0,0,0,0
-0.67,0.03,0.94,0.03,0.95,0.08,0.04,0.17,0,0,1,0,1,0,0,0
+0.67,0.03,0.94,0.03,0.95,0.08,0.04,0.17,0,0,1,0,1,1,0,0
0.13,0.8,0.95,0.03,0.01,0.08,0.35,0.04,0,0,0,0,0,0,0,0
0.1,0.78,0.98,0.03,0.02,0.06,0.1,0.05,0,0,0,0,0,0,0,0
-0.7,0.03,0.82,0.02,0.89,0.3,0.08,0.4,1,0,1,0,1,0,0,0
+0.7,0.03,0.82,0.02,0.89,0.3,0.08,0.4,1,0,1,0,1,1,0,0
0.69,0.02,0.05,0.02,0.02,0.04,0.06,0.08,1,0,1,0,0,1,0,0
0.45,0.32,0.46,0.23,0.03,0.31,0.33,0.68,1,0,1,0,0,1,0,0
0.07,0.96,0.53,0.03,0.04,0.26,0.86,0.07,0,0,0,0,0,0,0,0
@@ -90,8 +90,8 @@ focus,outreach,automated,customer,calendar_rsvp,needs_decision,needs_response,ne
0.78,0.02,0.76,0.04,0.78,0.43,0.09,0.77,0,0,0,0,0,0,0,0
0.06,0.97,0.72,0.02,0.02,0.18,0.83,0.07,0,0,0,0,0,0,0,0
0.2,0.96,0.15,0.03,0.01,0.33,0.56,0.16,0,0,0,0,0,0,0,0
-0.88,0.02,0.56,0.03,0.71,0.22,0.06,0.69,1,0,1,0,1,0,0,0
-0.78,0.03,0.35,0.03,0.69,0.31,0.11,0.61,0,0,0,0,1,0,0,0
+0.88,0.02,0.56,0.03,0.71,0.22,0.06,0.69,1,0,1,0,1,1,0,0
+0.78,0.03,0.35,0.03,0.69,0.31,0.11,0.61,0,0,0,0,1,1,0,0
0.32,0.03,0.18,0.04,0.02,0.05,0.05,0.07,1,0,1,0,0,1,0,0
0.76,0.03,0.04,0.45,0.01,0.23,0.84,0.61,0,0,0,0,0,1,1,0
0.08,0.97,0.82,0.02,0.03,0.12,0.59,0.05,0,0,0,0,0,0,0,0
@@ -106,38 +106,38 @@ focus,outreach,automated,customer,calendar_rsvp,needs_decision,needs_response,ne
0.09,0.97,0.65,0.03,0.04,0.12,0.77,0.07,0,0,0,0,0,0,0,0
0.44,0.07,0.09,0.4,0.02,0.11,0.08,0.25,1,0,1,0,0,1,0,0
0.79,0.03,0.05,0.83,0.02,0.17,0.09,0.29,1,1,1,0,0,1,0,0
-0.58,0.04,0.24,0.12,0.02,0.17,0.08,0.85,1,0,1,0,0,1,0,1
+0.58,0.04,0.24,0.12,0.02,0.17,0.08,0.85,1,0,1,0,0,1,1,0
0.37,0.93,0.1,0.03,0.02,0.35,0.38,0.21,0,0,0,0,0,0,0,0
0.07,0.95,0.41,0.04,0.02,0.13,0.83,0.05,0,0,0,0,0,0,0,0
0.1,0.72,0.86,0.03,0.06,0.29,0.43,0.11,0,0,0,0,0,0,0,0
-0.71,0.03,0.92,0.03,0.95,0.11,0.05,0.42,0,0,1,0,1,0,0,0
+0.71,0.03,0.92,0.03,0.95,0.11,0.05,0.42,0,0,1,0,1,1,0,0
0.08,0.98,0.87,0.02,0.02,0.16,0.81,0.06,0,0,0,0,0,0,0,0
0.17,0.16,0.86,0.27,0.02,0.05,0.07,0.1,0,0,0,0,0,0,0,0
0.07,0.96,0.51,0.03,0.01,0.15,0.88,0.09,0,0,0,0,0,0,0,0
-0.73,0.02,0.71,0.02,0.81,0.42,0.11,0.53,1,0,1,0,1,0,0,0
+0.73,0.02,0.71,0.02,0.81,0.42,0.11,0.53,1,0,1,0,1,1,0,0
0.07,0.97,0.74,0.03,0.02,0.19,0.78,0.06,0,0,0,0,0,0,0,0
-0.84,0.02,0.84,0.03,0.92,0.16,0.07,0.52,1,0,1,0,1,0,0,0
+0.84,0.02,0.84,0.03,0.92,0.16,0.07,0.52,1,0,1,0,1,1,0,0
0.13,0.89,0.78,0.27,0.02,0.11,0.08,0.1,0,0,0,0,0,0,0,0
-0.61,0.02,0.94,0.03,0.95,0.06,0.04,0.17,0,0,1,0,1,0,0,0
+0.61,0.02,0.94,0.03,0.95,0.06,0.04,0.17,0,0,1,0,1,1,0,0
0.06,0.98,0.47,0.02,0.01,0.1,0.84,0.05,0,0,0,0,0,0,0,0
0.53,0.06,0.23,0.06,0.04,0.07,0.08,0.1,1,1,1,1,0,1,0,0
0.07,0.98,0.82,0.02,0.02,0.14,0.83,0.06,0,0,0,0,0,0,0,0
0.08,0.96,0.64,0.02,0.02,0.17,0.84,0.07,0,0,0,0,0,0,0,0
-0.81,0.03,0.54,0.04,0.82,0.65,0.14,0.7,1,0,1,0,1,0,0,0
+0.81,0.03,0.54,0.04,0.82,0.65,0.14,0.7,1,0,1,0,1,1,1,0
0.1,0.81,0.91,0.04,0.01,0.14,0.62,0.04,0,0,0,0,0,0,0,0
0.12,0.67,0.98,0.03,0.01,0.08,0.12,0.05,0,0,0,0,0,0,0,0
0.13,0.81,0.26,0.1,0.01,0.18,0.62,0.2,0,0,0,0,0,0,0,0
-0.59,0.03,0.92,0.03,0.95,0.14,0.05,0.26,0,0,1,0,1,0,0,0
-0.62,0.03,0.94,0.03,0.95,0.07,0.05,0.09,0,0,1,0,1,0,0,0
-0.81,0.02,0.83,0.02,0.91,0.2,0.07,0.42,1,0,1,0,1,0,0,0
+0.59,0.03,0.92,0.03,0.95,0.14,0.05,0.26,0,0,1,0,1,1,0,0
+0.62,0.03,0.94,0.03,0.95,0.07,0.05,0.09,0,0,1,0,1,1,0,0
+0.81,0.02,0.83,0.02,0.91,0.2,0.07,0.42,1,0,1,0,1,1,0,0
0.08,0.97,0.64,0.02,0.01,0.25,0.85,0.08,0,0,0,0,0,0,0,0
0.07,0.98,0.78,0.02,0.01,0.19,0.79,0.05,0,0,0,0,0,0,0,0
0.1,0.91,0.97,0.02,0.14,0.07,0.11,0.05,0,0,0,0,0,0,0,0
0.26,0.96,0.09,0.03,0.02,0.22,0.74,0.11,0,0,0,0,0,0,0,0
0.09,0.97,0.74,0.03,0.01,0.16,0.7,0.08,0,0,0,0,0,0,0,0
0.08,0.95,0.89,0.02,0.02,0.06,0.15,0.05,0,0,0,0,0,0,0,0
-0.75,0.04,0.62,0.09,0.91,0.35,0.07,0.5,0,0,0,0,1,0,0,0
-0.8,0.03,0.76,0.02,0.9,0.27,0.07,0.55,1,0,1,0,1,0,0,0
+0.75,0.04,0.62,0.09,0.91,0.35,0.07,0.5,0,0,0,0,1,1,0,0
+0.8,0.03,0.76,0.02,0.9,0.27,0.07,0.55,1,0,1,0,1,1,0,0
0.22,0.61,0.24,0.05,0.09,0.4,0.91,0.43,0,0,0,0,0,0,0,0
0.08,0.95,0.4,0.02,0.02,0.18,0.87,0.08,0,0,0,0,0,0,0,0
0.05,0.98,0.74,0.02,0.01,0.14,0.32,0.06,0,0,0,0,0,0,0,0
@@ -145,7 +145,7 @@ focus,outreach,automated,customer,calendar_rsvp,needs_decision,needs_response,ne
0.06,0.97,0.5,0.02,0.01,0.25,0.78,0.06,0,0,0,0,0,0,0,0
0.07,0.95,0.88,0.02,0.11,0.07,0.19,0.05,0,0,0,0,0,0,0,0
0.18,0.57,0.5,0.05,0.02,0.53,0.87,0.38,0,0,0,0,0,0,0,0
-0.57,0.07,0.08,0.31,0.02,0.34,0.86,0.81,1,0,1,0,0,1,1,1
+0.57,0.07,0.08,0.31,0.02,0.34,0.86,0.81,1,0,1,0,0,1,1,0
0.07,0.91,0.98,0.03,0.02,0.16,0.17,0.05,0,0,0,0,0,0,0,0
0.46,0.92,0.07,0.03,0.01,0.45,0.68,0.28,0,0,0,0,0,0,0,0
0.09,0.91,0.41,0.04,0.02,0.13,0.58,0.08,0,0,0,0,0,0,0,0
@@ -153,18 +153,18 @@ focus,outreach,automated,customer,calendar_rsvp,needs_decision,needs_response,ne
0.08,0.7,0.98,0.03,0.01,0.1,0.09,0.06,0,0,0,0,0,0,0,0
0.68,0.05,0.27,0.93,0.02,0.18,0.07,0.44,1,0,1,0,0,1,0,0
0.32,0.19,0.97,0.07,0.07,0.11,0.06,0.32,0,0,0,0,0,0,0,0
-0.67,0.14,0.18,0.17,0.03,0.68,0.72,0.91,1,0,1,0,0,1,1,1
+0.67,0.14,0.18,0.17,0.03,0.68,0.72,0.91,1,0,1,0,0,1,1,0
0.2,0.95,0.08,0.03,0.01,0.35,0.53,0.15,0,0,0,0,0,0,0,0
0.08,0.97,0.64,0.03,0.01,0.17,0.87,0.06,0,0,0,0,0,0,0,0
0.07,0.97,0.5,0.02,0.01,0.14,0.81,0.05,0,0,0,0,0,0,0,0
0.32,0.09,0.35,0.77,0.02,0.2,0.62,0.31,0,0,0,0,0,1,0,0
0.07,0.97,0.61,0.03,0.02,0.18,0.83,0.07,0,0,0,0,0,0,0,0
-0.49,0.17,0.92,0.12,0.95,0.11,0.04,0.34,0,0,0,0,1,0,0,0
-0.71,0.02,0.91,0.02,0.95,0.11,0.06,0.44,0,0,1,0,1,0,0,0
+0.49,0.17,0.92,0.12,0.95,0.11,0.04,0.34,0,0,0,0,1,1,0,0
+0.71,0.02,0.91,0.02,0.95,0.11,0.06,0.44,0,0,1,0,1,1,0,0
0.08,0.96,0.47,0.02,0.01,0.24,0.91,0.07,0,0,0,0,0,0,0,0
0.08,0.85,0.54,0.03,0.16,0.18,0.51,0.08,0,0,0,0,0,0,0,0
-0.53,0.09,0.04,0.25,0.02,0.73,0.21,0.91,1,1,0,0,0,1,0,1
-0.7,0.13,0.68,0.03,0.89,0.63,0.11,0.62,1,0,1,0,1,0,0,0
+0.53,0.09,0.04,0.25,0.02,0.73,0.21,0.91,1,1,0,0,0,1,1,0
+0.7,0.13,0.68,0.03,0.89,0.63,0.11,0.62,1,0,1,0,1,1,0,0
0.1,0.88,0.56,0.09,0.02,0.1,0.72,0.09,0,0,0,0,0,0,0,0
0.08,0.96,0.49,0.03,0.01,0.16,0.81,0.07,0,0,0,0,0,0,0,0
0.37,0.92,0.14,0.03,0.01,0.36,0.49,0.22,0,0,0,0,0,0,0,0
@@ -173,11 +173,11 @@ focus,outreach,automated,customer,calendar_rsvp,needs_decision,needs_response,ne
0.08,0.96,0.63,0.02,0.02,0.31,0.88,0.08,0,0,0,0,0,0,0,0
0.07,0.97,0.53,0.03,0.01,0.18,0.88,0.08,0,0,0,0,0,0,0,0
0.56,0.09,0.16,0.18,0.01,0.24,0.85,0.5,0,0,0,0,0,1,1,0
-0.63,0.03,0.95,0.03,0.95,0.06,0.04,0.12,0,0,1,0,1,0,0,0
+0.63,0.03,0.95,0.03,0.95,0.06,0.04,0.12,0,0,1,0,1,1,0,0
0.08,0.96,0.27,0.03,0.01,0.19,0.84,0.08,0,0,0,0,0,0,0,0
-0.52,0.03,0.96,0.03,0.95,0.07,0.04,0.09,0,0,1,0,1,0,0,0
+0.52,0.03,0.96,0.03,0.95,0.07,0.04,0.09,0,0,1,0,1,1,0,0
0.07,0.97,0.46,0.02,0.01,0.34,0.88,0.07,0,0,0,0,0,0,0,0
-0.85,0.03,0.57,0.03,0.56,0.35,0.11,0.69,1,0,1,0,1,0,0,0
+0.85,0.03,0.57,0.03,0.56,0.35,0.11,0.69,1,0,1,0,1,1,0,0
0.08,0.96,0.23,0.03,0.01,0.27,0.85,0.08,0,0,0,0,0,0,0,0
0.1,0.87,0.97,0.03,0.11,0.08,0.12,0.05,0,0,0,0,0,0,0,0
0.07,0.97,0.58,0.03,0.03,0.15,0.79,0.08,0,0,0,0,0,0,0,0
@@ -185,40 +185,40 @@ focus,outreach,automated,customer,calendar_rsvp,needs_decision,needs_response,ne
0.79,0.04,0.05,0.95,0.01,0.43,0.91,0.62,0,0,0,0,0,1,1,0
0.11,0.6,0.89,0.21,0.01,0.25,0.6,0.09,0,0,0,0,0,0,0,0
0.09,0.96,0.46,0.03,0.01,0.18,0.86,0.08,0,0,0,0,0,0,0,0
-0.61,0.03,0.94,0.03,0.95,0.06,0.04,0.13,0,0,1,0,1,0,0,0
+0.61,0.03,0.94,0.03,0.95,0.06,0.04,0.13,0,0,1,0,1,1,0,0
0.07,0.97,0.87,0.03,0.02,0.14,0.8,0.06,0,0,0,0,0,0,0,0
-0.42,0.63,0.6,0.04,0.88,0.43,0.09,0.49,0,0,0,0,1,0,0,0
+0.42,0.63,0.6,0.04,0.88,0.43,0.09,0.49,0,0,0,0,1,1,0,0
0.09,0.96,0.34,0.03,0.02,0.36,0.86,0.17,0,0,0,0,0,0,0,0
-0.75,0.03,0.7,0.03,0.9,0.42,0.1,0.51,1,0,1,0,1,0,0,0
+0.75,0.03,0.7,0.03,0.9,0.42,0.1,0.51,1,0,1,0,1,1,0,0
0.09,0.96,0.96,0.03,0.02,0.04,0.08,0.05,0,0,0,0,0,0,0,0
0.07,0.95,0.51,0.04,0.01,0.08,0.41,0.06,0,0,0,0,0,0,0,0
-0.55,0.06,0.04,0.32,0.06,0.34,0.12,0.71,1,0,0,0,0,1,0,1
+0.55,0.06,0.04,0.32,0.06,0.34,0.12,0.71,1,0,0,0,0,1,1,0
0.09,0.97,0.34,0.03,0.01,0.11,0.73,0.05,0,0,0,0,0,0,0,0
0.33,0.03,0.14,0.04,0.02,0.04,0.05,0.07,1,0,1,0,0,1,0,0
0.08,0.93,0.49,0.03,0.01,0.24,0.82,0.11,0,0,0,0,0,0,0,0
-0.77,0.04,0.18,0.04,0.03,0.85,0.52,0.91,1,0,1,0,0,1,0,1
+0.77,0.04,0.18,0.04,0.03,0.85,0.52,0.91,1,0,1,0,0,1,1,0
0.4,0.94,0.1,0.03,0.01,0.37,0.52,0.29,0,0,0,0,0,0,0,0
0.08,0.97,0.62,0.04,0.01,0.12,0.85,0.06,0,0,0,0,0,0,0,0
-0.66,0.03,0.92,0.09,0.95,0.1,0.05,0.25,0,0,0,0,1,0,0,0
+0.66,0.03,0.92,0.09,0.95,0.1,0.05,0.25,0,0,0,0,1,1,0,0
0.08,0.97,0.75,0.02,0.01,0.1,0.75,0.04,0,0,0,0,0,0,0,0
-0.63,0.05,0.1,0.95,0.02,0.34,0.14,0.83,1,0,1,0,0,1,0,1
+0.63,0.05,0.1,0.95,0.02,0.34,0.14,0.83,1,0,1,0,0,1,1,0
0.2,0.96,0.16,0.04,0.02,0.15,0.42,0.12,0,0,0,0,0,0,0,0
-0.86,0.03,0.03,0.29,0.01,0.83,0.91,0.84,1,0,1,0,0,1,1,1
-0.62,0.09,0.21,0.43,0.02,0.7,0.93,0.81,1,0,0,0,0,1,1,1
+0.86,0.03,0.03,0.29,0.01,0.83,0.91,0.84,1,0,1,0,0,1,1,0
+0.62,0.09,0.21,0.43,0.02,0.7,0.93,0.81,1,0,0,0,0,1,1,0
0.1,0.97,0.66,0.03,0.01,0.21,0.74,0.06,0,0,0,0,0,0,0,0
-0.66,0.03,0.82,0.02,0.9,0.17,0.07,0.13,1,0,1,0,1,0,0,0
-0.61,0.03,0.91,0.02,0.95,0.09,0.05,0.17,0,0,1,0,1,0,0,0
+0.66,0.03,0.82,0.02,0.9,0.17,0.07,0.13,1,0,1,0,1,1,0,0
+0.61,0.03,0.91,0.02,0.95,0.09,0.05,0.17,0,0,1,0,1,1,0,0
0.11,0.23,0.94,0.18,0.02,0.61,0.58,0.09,0,0,0,0,0,0,0,0
0.73,0.05,0.08,0.4,0.02,0.1,0.06,0.24,1,0,1,0,0,1,0,0
0.07,0.97,0.72,0.03,0.02,0.12,0.88,0.06,0,0,0,0,0,0,0,0
-0.21,0.36,0.13,0.45,0.01,0.23,0.77,0.23,0,0,0,0,0,0,0,0
+0.21,0.36,0.13,0.45,0.01,0.23,0.77,0.23,0,0,0,0,0,1,1,0
0.08,0.93,0.17,0.04,0.02,0.08,0.59,0.1,0,0,0,0,0,0,0,0
0.09,0.78,0.98,0.03,0.01,0.14,0.11,0.05,0,0,0,0,0,0,0,0
0.3,0.1,0.24,0.76,0.02,0.17,0.7,0.5,1,0,0,0,0,1,1,0
0.08,0.97,0.67,0.03,0.01,0.27,0.87,0.07,0,0,0,0,0,0,0,0
0.09,0.97,0.78,0.02,0.01,0.15,0.75,0.05,0,0,0,0,0,0,0,0
0.08,0.98,0.9,0.02,0.01,0.08,0.63,0.04,0,0,0,0,0,0,0,0
-0.58,0.03,0.81,0.03,0.85,0.08,0.05,0.15,0,0,1,0,1,0,0,0
+0.58,0.03,0.81,0.03,0.85,0.08,0.05,0.15,0,0,1,0,1,1,0,0
0.07,0.97,0.91,0.02,0.02,0.09,0.47,0.05,0,0,0,0,0,0,0,0
0.11,0.47,0.96,0.04,0.01,0.16,0.42,0.06,0,0,0,0,0,0,0,0
0.09,0.93,0.59,0.04,0.01,0.09,0.2,0.07,0,0,0,0,0,0,0,0
@@ -226,7 +226,7 @@ focus,outreach,automated,customer,calendar_rsvp,needs_decision,needs_response,ne
0.07,0.97,0.75,0.04,0.01,0.15,0.77,0.07,0,0,0,0,0,0,0,0
0.38,0.06,0.97,0.07,0.07,0.14,0.32,0.77,0,0,0,0,0,0,0,0
0.07,0.98,0.64,0.02,0.02,0.15,0.84,0.05,0,0,0,0,0,0,0,0
-0.24,0.44,0.12,0.37,0.02,0.17,0.09,0.22,1,0,1,0,0,0,0,0
+0.24,0.44,0.12,0.37,0.02,0.17,0.09,0.22,1,0,1,0,0,1,0,0
0.15,0.98,0.94,0.03,0.01,0.1,0.31,0.05,0,0,0,0,0,0,0,0
0.1,0.57,0.95,0.05,0.02,0.05,0.07,0.06,0,0,0,0,0,0,0,0
0.07,0.96,0.61,0.03,0.02,0.13,0.82,0.05,0,0,0,0,0,0,0,0
@@ -235,20 +235,20 @@ focus,outreach,automated,customer,calendar_rsvp,needs_decision,needs_response,ne
0.12,0.9,0.93,0.04,0.01,0.09,0.33,0.05,0,0,0,0,0,0,0,0
0.36,0.92,0.15,0.03,0.02,0.29,0.4,0.16,0,0,0,0,0,0,0,0
0.11,0.97,0.96,0.03,0.01,0.05,0.15,0.05,0,0,0,0,0,0,0,0
-0.31,0.35,0.42,0.36,0.02,0.67,0.92,0.32,0,0,0,0,0,0,0,0
-0.64,0.04,0.79,0.03,0.58,0.37,0.09,0.43,1,0,1,0,1,0,0,0
-0.71,0.04,0.92,0.08,0.95,0.09,0.04,0.26,0,0,0,0,1,0,0,0
+0.31,0.35,0.42,0.36,0.02,0.67,0.92,0.32,0,0,0,0,0,1,1,0
+0.64,0.04,0.79,0.03,0.58,0.37,0.09,0.43,1,0,1,0,1,1,0,0
+0.71,0.04,0.92,0.08,0.95,0.09,0.04,0.26,0,0,0,0,1,1,0,0
0.07,0.96,0.61,0.03,0.01,0.25,0.84,0.08,0,0,0,0,0,0,0,0
-0.72,0.02,0.84,0.03,0.93,0.73,0.08,0.38,1,0,1,0,0,0,0,0
+0.72,0.02,0.84,0.03,0.93,0.73,0.08,0.38,1,0,1,0,0,1,0,0
0.09,0.86,0.82,0.03,0.06,0.25,0.77,0.12,0,0,0,0,0,0,0,0
-0.53,0.04,0.87,0.04,0.62,0.06,0.05,0.09,1,0,1,0,1,0,0,0
-0.67,0.03,0.92,0.03,0.95,0.08,0.04,0.25,0,0,1,0,1,0,0,0
-0.78,0.02,0.78,0.03,0.72,0.8,0.15,0.52,1,0,1,0,0,0,0,0
-0.63,0.03,0.9,0.03,0.95,0.13,0.05,0.22,0,0,1,0,1,0,0,0
+0.53,0.04,0.87,0.04,0.62,0.06,0.05,0.09,1,0,1,0,1,1,0,0
+0.67,0.03,0.92,0.03,0.95,0.08,0.04,0.25,0,0,1,0,1,1,0,0
+0.78,0.02,0.78,0.03,0.72,0.8,0.15,0.52,1,0,1,0,0,1,0,0
+0.63,0.03,0.9,0.03,0.95,0.13,0.05,0.22,0,0,1,0,1,1,0,0
0.11,0.82,0.96,0.04,0.01,0.04,0.13,0.06,0,0,0,0,0,0,0,0
0.36,0.27,0.88,0.15,0.04,0.39,0.46,0.25,0,0,0,0,0,0,0,0
0.65,0.29,0.05,0.06,0.01,0.9,0.96,0.65,1,0,0,0,0,1,1,0
-0.52,0.03,0.9,0.04,0.68,0.05,0.04,0.08,1,0,1,0,1,0,0,0
+0.52,0.03,0.9,0.04,0.68,0.05,0.04,0.08,1,0,1,0,1,1,0,0
0.8,0.03,0.85,0.03,0.01,0.33,0.07,0.25,1,0,1,0,0,1,0,0
0.08,0.93,0.48,0.03,0.02,0.23,0.84,0.11,0,0,0,0,0,0,0,0
0.12,0.84,0.95,0.04,0.06,0.16,0.13,0.08,0,0,0,0,0,0,0,0
@@ -259,20 +259,20 @@ focus,outreach,automated,customer,calendar_rsvp,needs_decision,needs_response,ne
0.06,0.97,0.59,0.02,0.01,0.23,0.86,0.05,0,0,0,0,0,0,0,0
0.8,0.05,0.05,0.46,0.01,0.26,0.55,0.5,0,0,0,0,0,1,0,0
0.07,0.97,0.6,0.02,0.01,0.12,0.53,0.07,0,0,0,0,0,0,0,0
-0.78,0.03,0.64,0.06,0.86,0.34,0.1,0.58,1,0,1,0,1,0,0,0
+0.78,0.03,0.64,0.06,0.86,0.34,0.1,0.58,1,0,1,0,1,1,0,0
0.58,0.09,0.09,0.92,0.02,0.12,0.1,0.42,1,0,1,0,0,1,0,0
0.48,0.17,0.95,0.11,0.03,0.74,0.46,0.46,0,0,0,0,0,0,0,0
0.34,0.27,0.1,0.73,0.02,0.2,0.13,0.61,1,1,0,1,0,1,0,0
0.08,0.95,0.33,0.03,0.01,0.18,0.81,0.07,0,0,0,0,0,0,0,0
0.14,0.92,0.68,0.04,0.02,0.11,0.32,0.08,0,0,0,0,0,0,0,0
-0.76,0.03,0.78,0.08,0.91,0.28,0.06,0.44,0,0,0,0,1,0,0,0
+0.76,0.03,0.78,0.08,0.91,0.28,0.06,0.44,0,0,0,0,1,1,0,0
0.06,0.97,0.6,0.02,0.01,0.29,0.73,0.07,0,0,0,0,0,0,0,0
0.08,0.93,0.49,0.03,0.02,0.25,0.71,0.09,0,0,0,0,0,0,0,0
0.08,0.93,0.55,0.08,0.02,0.09,0.61,0.07,0,0,0,0,0,0,0,0
0.09,0.86,0.56,0.03,0.02,0.16,0.77,0.07,0,0,0,0,0,0,0,0
0.42,0.04,0.92,0.07,0.04,0.11,0.09,0.13,1,0,1,0,0,0,0,0
0.07,0.97,0.67,0.03,0.02,0.15,0.74,0.05,0,0,0,0,0,0,0,0
-0.59,0.03,0.95,0.03,0.96,0.08,0.04,0.12,0,0,1,0,1,0,0,0
+0.59,0.03,0.95,0.03,0.96,0.08,0.04,0.12,0,0,1,0,1,1,0,0
0.07,0.97,0.71,0.02,0.01,0.13,0.72,0.05,0,0,0,0,0,0,0,0
0.12,0.42,0.97,0.06,0.02,0.1,0.07,0.08,0,0,0,0,0,0,0,0
0.07,0.97,0.51,0.03,0.01,0.11,0.82,0.07,0,0,0,0,0,0,0,0
@@ -283,38 +283,38 @@ focus,outreach,automated,customer,calendar_rsvp,needs_decision,needs_response,ne
0.08,0.97,0.54,0.03,0.02,0.16,0.88,0.06,0,0,0,0,0,0,0,0
0.45,0.05,0.32,0.91,0.02,0.36,0.74,0.61,0,0,0,0,0,1,1,0
0.08,0.93,0.7,0.04,0.02,0.07,0.29,0.06,0,0,0,0,0,0,0,0
-0.42,0.11,0.94,0.08,0.95,0.08,0.04,0.3,0,0,0,0,1,0,0,0
+0.42,0.11,0.94,0.08,0.95,0.08,0.04,0.3,0,0,0,0,1,1,0,0
0.07,0.94,0.16,0.03,0.01,0.08,0.72,0.07,0,0,0,0,0,0,0,0
-0.58,0.02,0.95,0.03,0.96,0.11,0.04,0.18,0,0,1,0,1,0,0,0
+0.58,0.02,0.95,0.03,0.96,0.11,0.04,0.18,0,0,1,0,1,1,0,0
0.06,0.96,0.56,0.02,0.01,0.23,0.83,0.07,0,0,0,0,0,0,0,0
0.07,0.97,0.58,0.03,0.02,0.12,0.19,0.05,0,0,0,0,0,0,0,0
0.11,0.95,0.64,0.05,0.02,0.06,0.2,0.05,0,0,0,0,0,0,0,0
-0.64,0.02,0.68,0.03,0.57,0.38,0.07,0.43,1,0,1,0,1,0,0,0
+0.64,0.02,0.68,0.03,0.57,0.38,0.07,0.43,1,0,1,0,1,1,0,0
0.06,0.97,0.73,0.02,0.01,0.1,0.78,0.05,0,0,0,0,0,0,0,0
0.45,0.29,0.87,0.1,0.93,0.13,0.05,0.2,1,0,0,0,0,0,0,0
-0.64,0.03,0.95,0.03,0.95,0.08,0.05,0.14,0,0,1,0,1,0,0,0
+0.64,0.03,0.95,0.03,0.95,0.08,0.05,0.14,0,0,1,0,1,1,0,0
0.12,0.35,0.3,0.05,0.02,0.04,0.06,0.06,0,0,0,0,0,0,0,0
0.09,0.94,0.2,0.05,0.01,0.16,0.47,0.08,0,0,0,0,0,0,0,0
0.47,0.12,0.97,0.32,0.05,0.14,0.09,0.35,0,0,0,0,0,0,0,0
0.41,0.95,0.11,0.03,0.01,0.3,0.72,0.24,0,0,0,0,0,0,0,0
-0.7,0.04,0.76,0.03,0.88,0.18,0.06,0.28,1,0,1,0,1,0,0,0
-0.65,0.04,0.72,0.03,0.51,0.31,0.08,0.42,1,0,1,0,1,0,0,0
+0.7,0.04,0.76,0.03,0.88,0.18,0.06,0.28,1,0,1,0,1,1,0,0
+0.65,0.04,0.72,0.03,0.51,0.31,0.08,0.42,1,0,1,0,1,1,0,0
0.5,0.89,0.08,0.03,0.01,0.35,0.44,0.27,0,0,0,0,0,0,0,0
0.2,0.8,0.1,0.07,0.02,0.15,0.83,0.23,0,0,0,0,0,0,0,0
0.41,0.1,0.23,0.75,0.02,0.25,0.79,0.47,0,0,0,0,0,1,1,0
0.1,0.71,0.94,0.03,0.1,0.14,0.13,0.06,0,0,0,0,0,0,0,0
-0.76,0.03,0.63,0.03,0.82,0.61,0.11,0.58,1,0,1,0,1,0,0,0
+0.76,0.03,0.63,0.03,0.82,0.61,0.11,0.58,1,0,1,0,1,1,0,0
0.11,0.95,0.56,0.03,0.01,0.1,0.17,0.11,0,0,0,0,0,0,0,0
0.08,0.91,0.98,0.03,0.25,0.08,0.11,0.05,0,0,0,0,0,0,0,0
0.24,0.95,0.3,0.03,0.02,0.21,0.58,0.16,0,0,0,0,0,0,0,0
0.07,0.96,0.22,0.03,0.01,0.1,0.68,0.07,0,0,0,0,0,0,0,0
0.07,0.97,0.54,0.02,0.02,0.12,0.76,0.06,0,0,0,0,0,0,0,0
0.28,0.03,0.98,0.06,0.05,0.1,0.38,0.4,0,0,0,0,0,0,0,0
-0.69,0.03,0.93,0.03,0.93,0.33,0.06,0.32,1,0,1,0,1,0,0,0
+0.69,0.03,0.93,0.03,0.93,0.33,0.06,0.32,1,0,1,0,1,1,0,0
0.12,0.19,0.32,0.46,0.02,0.07,0.25,0.11,0,0,0,0,0,0,0,0
0.15,0.1,0.13,0.57,0.02,0.04,0.07,0.09,0,0,0,0,0,1,0,0
0.43,0.09,0.97,0.09,0.07,0.3,0.55,0.82,0,0,0,0,0,0,0,0
-0.66,0.03,0.94,0.03,0.95,0.07,0.04,0.19,0,0,1,0,1,0,0,0
+0.66,0.03,0.94,0.03,0.95,0.07,0.04,0.19,0,0,1,0,1,1,0,0
0.18,0.89,0.28,0.03,0.03,0.36,0.89,0.19,0,0,0,0,0,0,0,0
0.2,0.36,0.91,0.19,0.02,0.1,0.12,0.19,0,0,0,0,0,0,0,0
0.09,0.96,0.72,0.03,0.02,0.15,0.67,0.06,0,0,0,0,0,0,0,0
@@ -326,11 +326,11 @@ focus,outreach,automated,customer,calendar_rsvp,needs_decision,needs_response,ne
0.1,0.93,0.98,0.02,0.03,0.04,0.08,0.05,0,0,0,0,0,0,0,0
0.06,0.97,0.53,0.02,0.02,0.11,0.88,0.06,0,0,0,0,0,0,0,0
0.07,0.91,0.71,0.05,0.02,0.14,0.57,0.09,0,0,0,0,0,0,0,0
-0.61,0.03,0.94,0.03,0.95,0.07,0.05,0.09,0,0,1,0,1,0,0,0
-0.46,0.43,0.58,0.08,0.9,0.36,0.09,0.58,0,0,0,0,1,0,0,0
+0.61,0.03,0.94,0.03,0.95,0.07,0.05,0.09,0,0,1,0,1,1,0,0
+0.46,0.43,0.58,0.08,0.9,0.36,0.09,0.58,0,0,0,0,1,1,0,0
0.11,0.91,0.47,0.03,0.02,0.24,0.88,0.1,0,0,0,0,0,0,0,0
-0.81,0.09,0.11,0.16,0.03,0.68,0.71,0.94,1,0,1,0,0,1,1,1
-0.67,0.02,0.93,0.03,0.95,0.08,0.04,0.16,0,0,1,0,1,0,0,0
+0.81,0.09,0.11,0.16,0.03,0.68,0.71,0.94,1,0,1,0,0,1,1,0
+0.67,0.02,0.93,0.03,0.95,0.08,0.04,0.16,0,0,1,0,1,1,0,0
0.12,0.91,0.96,0.06,0.02,0.07,0.13,0.06,0,0,0,0,0,0,0,0
0.14,0.06,0.75,0.05,0.06,0.07,0.05,0.09,1,0,1,0,0,0,0,0
0.09,0.96,0.35,0.05,0.01,0.16,0.86,0.09,0,0,0,0,0,0,0,0
@@ -339,34 +339,34 @@ focus,outreach,automated,customer,calendar_rsvp,needs_decision,needs_response,ne
0.1,0.87,0.5,0.04,0.01,0.19,0.85,0.11,0,0,0,0,0,0,0,0
0.23,0.95,0.16,0.04,0.02,0.2,0.2,0.13,0,0,0,0,0,0,0,0
0.06,0.96,0.28,0.02,0.01,0.17,0.86,0.08,0,0,0,0,0,0,0,0
-0.52,0.03,0.9,0.03,0.75,0.05,0.04,0.08,1,0,1,0,1,0,0,0
+0.52,0.03,0.9,0.03,0.75,0.05,0.04,0.08,1,0,1,0,1,1,0,0
0.36,0.15,0.41,0.86,0.02,0.37,0.55,0.52,1,0,1,0,0,1,0,0
-0.82,0.07,0.57,0.06,0.84,0.46,0.08,0.61,0,0,0,0,1,0,0,0
+0.82,0.07,0.57,0.06,0.84,0.46,0.08,0.61,0,0,0,0,1,1,0,0
0.08,0.96,0.54,0.05,0.01,0.14,0.75,0.08,0,0,0,0,0,0,0,0
-0.58,0.03,0.89,0.03,0.94,0.15,0.05,0.18,0,0,1,0,1,0,0,0
+0.58,0.03,0.89,0.03,0.94,0.15,0.05,0.18,0,0,1,0,1,1,0,0
0.44,0.05,0.97,0.1,0.1,0.27,0.4,0.73,0,0,0,0,0,0,0,0
0.07,0.96,0.51,0.04,0.01,0.21,0.87,0.1,0,0,0,0,0,0,0,0
0.09,0.96,0.55,0.03,0.02,0.17,0.51,0.08,0,0,0,0,0,0,0,0
-0.67,0.03,0.94,0.04,0.95,0.09,0.05,0.19,0,0,1,0,1,0,0,0
+0.67,0.03,0.94,0.04,0.95,0.09,0.05,0.19,0,0,1,0,1,1,0,0
0.14,0.95,0.35,0.03,0.02,0.17,0.58,0.09,0,0,0,0,0,0,0,0
-0.71,0.03,0.81,0.03,0.9,0.58,0.09,0.56,0,0,1,0,1,0,0,0
-0.62,0.03,0.82,0.02,0.83,0.09,0.06,0.19,0,0,1,0,1,0,0,0
-0.57,0.02,0.93,0.03,0.96,0.11,0.04,0.12,0,0,1,0,1,0,0,0
+0.71,0.03,0.81,0.03,0.9,0.58,0.09,0.56,0,0,1,0,1,1,0,0
+0.62,0.03,0.82,0.02,0.83,0.09,0.06,0.19,0,0,1,0,1,1,0,0
+0.57,0.02,0.93,0.03,0.96,0.11,0.04,0.12,0,0,1,0,1,1,0,0
0.06,0.97,0.65,0.02,0.01,0.15,0.88,0.06,0,0,0,0,0,0,0,0
-0.64,0.03,0.92,0.06,0.95,0.08,0.04,0.2,0,0,0,0,1,0,0,0
+0.64,0.03,0.92,0.06,0.95,0.08,0.04,0.2,0,0,0,0,1,1,0,0
0.55,0.74,0.08,0.05,0.05,0.53,0.75,0.94,1,1,0,1,0,1,0,1
0.09,0.56,0.98,0.03,0.02,0.04,0.06,0.05,0,0,0,0,0,0,0,0
0.08,0.96,0.56,0.03,0.01,0.06,0.56,0.05,0,0,0,0,0,0,0,0
0.08,0.97,0.78,0.02,0.01,0.11,0.82,0.05,0,0,0,0,0,0,0,0
0.24,0.82,0.17,0.25,0.01,0.18,0.59,0.14,0,0,0,0,0,0,0,0
-0.61,0.03,0.78,0.03,0.89,0.09,0.05,0.12,0,0,1,0,1,0,0,0
+0.61,0.03,0.78,0.03,0.89,0.09,0.05,0.12,0,0,1,0,1,1,0,0
0.13,0.89,0.93,0.04,0.02,0.07,0.09,0.07,0,0,0,0,0,0,0,0
-0.55,0.07,0.91,0.07,0.94,0.1,0.04,0.17,0,0,0,0,1,0,0,0
-0.68,0.02,0.94,0.03,0.95,0.08,0.05,0.18,0,0,1,0,1,0,0,0
+0.55,0.07,0.91,0.07,0.94,0.1,0.04,0.17,0,0,0,0,1,1,0,0
+0.68,0.02,0.94,0.03,0.95,0.08,0.05,0.18,0,0,1,0,1,1,0,0
0.07,0.97,0.6,0.02,0.02,0.16,0.83,0.05,0,0,0,0,0,0,0,0
0.18,0.86,0.1,0.06,0.02,0.35,0.92,0.24,0,0,0,0,0,0,0,0
0.42,0.1,0.96,0.1,0.06,0.65,0.44,0.82,0,0,0,0,0,0,0,0
-0.73,0.15,0.48,0.04,0.86,0.26,0.07,0.58,1,0,0,0,1,0,0,0
+0.73,0.15,0.48,0.04,0.86,0.26,0.07,0.58,1,0,0,0,1,1,0,0
0.32,0.95,0.13,0.04,0.01,0.24,0.55,0.15,0,0,0,0,0,0,0,0
0.06,0.97,0.74,0.03,0.01,0.12,0.74,0.06,0,0,0,0,0,0,0,0
0.39,0.06,0.33,0.89,0.02,0.28,0.79,0.41,0,0,0,0,0,1,1,0
@@ -378,10 +378,10 @@ focus,outreach,automated,customer,calendar_rsvp,needs_decision,needs_response,ne
0.07,0.65,0.97,0.03,0.04,0.05,0.05,0.05,0,0,0,0,0,0,0,0
0.3,0.95,0.09,0.04,0.02,0.38,0.9,0.17,0,0,0,0,0,0,0,0
0.12,0.91,0.11,0.05,0.02,0.14,0.91,0.1,0,0,0,0,0,0,0,0
-0.79,0.05,0.04,0.07,0.26,0.37,0.11,0.84,1,0,1,0,0,1,0,1
+0.79,0.05,0.04,0.07,0.26,0.37,0.11,0.84,1,0,1,0,0,1,1,0
0.07,0.95,0.56,0.03,0.01,0.2,0.75,0.06,0,0,0,0,0,0,0,0
0.08,0.95,0.52,0.03,0.02,0.34,0.86,0.12,0,0,0,0,0,0,0,0
-0.65,0.03,0.95,0.03,0.95,0.07,0.05,0.17,0,0,1,0,1,0,0,0
+0.65,0.03,0.95,0.03,0.95,0.07,0.05,0.17,0,0,1,0,1,1,0,0
0.08,0.98,0.7,0.02,0.01,0.12,0.78,0.05,0,0,0,0,0,0,0,0
0.08,0.78,0.41,0.04,0.02,0.12,0.77,0.07,0,0,0,0,0,0,0,0
0.09,0.97,0.95,0.02,0.02,0.07,0.45,0.05,0,0,0,0,0,0,0,0
@@ -392,13 +392,13 @@ focus,outreach,automated,customer,calendar_rsvp,needs_decision,needs_response,ne
0.07,0.98,0.84,0.02,0.01,0.12,0.82,0.05,0,0,0,0,0,0,0,0
0.34,0.07,0.97,0.16,0.03,0.47,0.43,0.61,0,0,0,0,0,0,0,0
0.31,0.95,0.09,0.04,0.01,0.28,0.61,0.19,0,0,0,0,0,0,0,0
-0.64,0.03,0.95,0.02,0.94,0.08,0.04,0.09,0,0,1,0,1,0,0,0
+0.64,0.03,0.95,0.02,0.94,0.08,0.04,0.09,0,0,1,0,1,1,0,0
0.08,0.93,0.11,0.04,0.01,0.08,0.68,0.07,0,0,0,0,0,0,0,0
0.08,0.96,0.58,0.04,0.01,0.28,0.84,0.09,0,0,0,0,0,0,0,0
0.07,0.97,0.49,0.03,0.01,0.08,0.71,0.06,0,0,0,0,0,0,0,0
0.09,0.92,0.7,0.06,0.02,0.11,0.48,0.09,0,0,0,0,0,0,0,0
0.52,0.04,0.08,0.04,0.02,0.06,0.09,0.06,1,1,1,1,0,1,0,0
-0.77,0.03,0.82,0.03,0.91,0.63,0.08,0.68,0,0,1,0,1,0,0,0
+0.77,0.03,0.82,0.03,0.91,0.63,0.08,0.68,0,0,1,0,1,1,0,0
0.24,0.15,0.68,0.39,0.04,0.09,0.11,0.16,0,0,0,0,0,0,0,0
0.08,0.97,0.5,0.05,0.01,0.14,0.88,0.08,0,0,0,0,0,0,0,0
0.07,0.97,0.6,0.03,0.01,0.08,0.48,0.05,0,0,0,0,0,0,0,0
@@ -406,7 +406,7 @@ focus,outreach,automated,customer,calendar_rsvp,needs_decision,needs_response,ne
0.09,0.92,0.22,0.14,0.01,0.16,0.74,0.09,0,0,0,0,0,0,0,0
0.09,0.96,0.67,0.03,0.01,0.13,0.55,0.05,0,0,0,0,0,0,0,0
0.76,0.22,0.04,0.05,0.55,0.34,0.09,0.34,1,1,0,1,0,1,0,0
-0.77,0.05,0.63,0.1,0.87,0.37,0.08,0.54,0,0,0,0,1,0,0,0
+0.77,0.05,0.63,0.1,0.87,0.37,0.08,0.54,0,0,0,0,1,1,0,0
0.11,0.9,0.22,0.04,0.05,0.2,0.83,0.18,0,0,0,0,0,0,0,0
0.08,0.97,0.45,0.03,0.01,0.16,0.77,0.07,0,0,0,0,0,0,0,0
0.49,0.03,0.22,0.05,0.02,0.16,0.06,0.36,1,0,1,0,0,1,0,0
@@ -415,22 +415,22 @@ focus,outreach,automated,customer,calendar_rsvp,needs_decision,needs_response,ne
0.09,0.96,0.73,0.03,0.01,0.17,0.8,0.07,0,0,0,0,0,0,0,0
0.4,0.94,0.07,0.03,0.01,0.34,0.53,0.24,0,0,0,0,0,0,0,0
0.63,0.04,0.26,0.96,0.02,0.18,0.57,0.59,0,0,0,0,0,1,0,0
-0.59,0.03,0.75,0.03,0.86,0.12,0.06,0.34,0,0,1,0,1,0,0,0
+0.59,0.03,0.75,0.03,0.86,0.12,0.06,0.34,0,0,1,0,1,1,0,0
0.07,0.98,0.79,0.02,0.01,0.14,0.86,0.05,0,0,0,0,0,0,0,0
0.16,0.5,0.22,0.1,0.03,0.06,0.15,0.11,0,0,0,0,0,0,0,0
-0.57,0.02,0.96,0.03,0.96,0.06,0.03,0.08,0,0,1,0,1,0,0,0
-0.71,0.03,0.93,0.03,0.95,0.1,0.04,0.29,0,0,1,0,1,0,0,0
+0.57,0.02,0.96,0.03,0.96,0.06,0.03,0.08,0,0,1,0,1,1,0,0
+0.71,0.03,0.93,0.03,0.95,0.1,0.04,0.29,0,0,1,0,1,1,0,0
0.37,0.19,0.11,0.06,0.05,0.18,0.06,0.24,1,0,0,0,0,1,0,0
-0.54,0.03,0.95,0.03,0.96,0.06,0.04,0.11,0,0,1,0,1,0,0,0
-0.48,0.02,0.96,0.02,0.96,0.09,0.04,0.08,0,0,1,0,1,0,0,0
-0.67,0.03,0.92,0.03,0.95,0.08,0.04,0.2,0,0,1,0,1,0,0,0
+0.54,0.03,0.95,0.03,0.96,0.06,0.04,0.11,0,0,1,0,1,1,0,0
+0.48,0.02,0.96,0.02,0.96,0.09,0.04,0.08,0,0,1,0,1,1,0,0
+0.67,0.03,0.92,0.03,0.95,0.08,0.04,0.2,0,0,1,0,1,1,0,0
0.07,0.97,0.74,0.02,0.01,0.09,0.82,0.05,0,0,0,0,0,0,0,0
0.14,0.96,0.36,0.03,0.02,0.21,0.3,0.08,0,0,0,0,0,0,0,0
0.06,0.97,0.35,0.02,0.02,0.21,0.89,0.07,0,0,0,0,0,0,0,0
0.09,0.93,0.98,0.02,0.04,0.05,0.08,0.05,0,0,0,0,0,0,0,0
0.08,0.95,0.39,0.03,0.01,0.24,0.89,0.1,0,0,0,0,0,0,0,0
-0.66,0.03,0.89,0.03,0.92,0.29,0.07,0.4,1,0,1,0,1,0,0,0
-0.67,0.03,0.91,0.03,0.94,0.08,0.05,0.18,0,0,1,0,1,0,0,0
+0.66,0.03,0.89,0.03,0.92,0.29,0.07,0.4,1,0,1,0,1,1,0,0
+0.67,0.03,0.91,0.03,0.94,0.08,0.05,0.18,0,0,1,0,1,1,0,0
0.11,0.93,0.98,0.02,0.02,0.04,0.06,0.05,0,0,0,0,0,0,0,0
0.07,0.97,0.68,0.02,0.01,0.09,0.8,0.05,0,0,0,0,0,0,0,0
0.1,0.95,0.34,0.05,0.01,0.14,0.36,0.07,0,0,0,0,0,0,0,0
@@ -441,17 +441,17 @@ focus,outreach,automated,customer,calendar_rsvp,needs_decision,needs_response,ne
0.46,0.04,0.18,0.04,0.02,0.04,0.08,0.08,1,1,1,1,0,1,0,0
0.11,0.91,0.95,0.03,0.01,0.06,0.09,0.06,0,0,0,0,0,0,0,0
0.08,0.93,0.21,0.02,0.02,0.15,0.84,0.07,0,0,0,0,0,0,0,0
-0.77,0.03,0.65,0.03,0.8,0.17,0.07,0.42,1,0,1,0,1,0,0,0
+0.77,0.03,0.65,0.03,0.8,0.17,0.07,0.42,1,0,1,0,1,1,0,0
0.07,0.98,0.69,0.03,0.01,0.16,0.8,0.05,0,0,0,0,0,0,0,0
-0.47,0.08,0.92,0.07,0.96,0.09,0.05,0.21,0,0,0,0,1,0,0,0
+0.47,0.08,0.92,0.07,0.96,0.09,0.05,0.21,0,0,0,0,1,1,0,0
0.18,0.95,0.46,0.03,0.01,0.19,0.43,0.13,0,0,0,0,0,0,0,0
-0.53,0.03,0.96,0.03,0.96,0.06,0.04,0.08,0,0,1,0,1,0,0,0
-0.49,0.03,0.09,0.94,0.01,0.35,0.85,0.71,0,0,0,0,0,1,1,1
+0.53,0.03,0.96,0.03,0.96,0.06,0.04,0.08,0,0,1,0,1,1,0,0
+0.49,0.03,0.09,0.94,0.01,0.35,0.85,0.71,0,0,0,0,0,1,1,0
0.86,0.02,0.11,0.06,0.01,0.27,0.08,0.69,1,0,1,0,0,1,0,0
0.51,0.08,0.07,0.21,0.02,0.29,0.17,0.58,1,1,1,1,0,1,0,0
-0.85,0.04,0.77,0.16,0.67,0.82,0.08,0.88,0,0,0,0,1,0,0,0
+0.85,0.04,0.77,0.16,0.67,0.82,0.08,0.88,0,0,0,0,1,1,1,0
0.07,0.92,0.98,0.02,0.18,0.07,0.12,0.05,0,0,0,0,0,0,0,0
-0.06,0.97,0.93,0.02,0.58,0.06,0.18,0.05,0,0,0,0,1,0,0,0
+0.06,0.97,0.93,0.02,0.58,0.06,0.18,0.05,0,0,0,0,1,1,0,0
0.09,0.95,0.56,0.02,0.02,0.33,0.81,0.09,0,0,0,0,0,0,0,0
0.22,0.11,0.76,0.41,0.02,0.07,0.09,0.12,0,0,0,0,0,0,0,0
0.09,0.9,0.47,0.08,0.02,0.21,0.78,0.13,0,0,0,0,0,0,0,0
@@ -459,7 +459,7 @@ focus,outreach,automated,customer,calendar_rsvp,needs_decision,needs_response,ne
0.38,0.94,0.13,0.03,0.02,0.34,0.52,0.37,0,0,0,0,0,0,0,0
0.06,0.97,0.53,0.02,0.01,0.15,0.83,0.06,0,0,0,0,0,0,0,0
0.42,0.92,0.08,0.03,0.01,0.32,0.62,0.21,0,0,0,0,0,0,0,0
-0.68,0.03,0.93,0.03,0.95,0.09,0.05,0.16,0,0,1,0,1,0,0,0
+0.68,0.03,0.93,0.03,0.95,0.09,0.05,0.16,0,0,1,0,1,1,0,0
0.07,0.96,0.61,0.02,0.02,0.16,0.82,0.06,0,0,0,0,0,0,0,0
0.09,0.89,0.15,0.03,0.01,0.17,0.87,0.07,0,0,0,0,0,0,0,0
0.59,0.12,0.23,0.56,0.02,0.24,0.35,0.63,1,0,1,0,0,1,0,0
@@ -469,29 +469,29 @@ focus,outreach,automated,customer,calendar_rsvp,needs_decision,needs_response,ne
0.43,0.93,0.08,0.03,0.01,0.29,0.8,0.32,0,0,0,0,0,0,0,0
0.31,0.05,0.97,0.06,0.06,0.13,0.24,0.41,0,0,0,0,0,0,0,0
0.09,0.37,0.98,0.03,0.03,0.05,0.04,0.05,0,0,0,0,0,0,0,0
-0.72,0.03,0.93,0.03,0.95,0.07,0.04,0.19,0,0,1,0,1,0,0,0
+0.72,0.03,0.93,0.03,0.95,0.07,0.04,0.19,0,0,1,0,1,1,0,0
0.47,0.95,0.06,0.03,0.02,0.36,0.85,0.28,0,0,0,0,0,0,0,0
0.3,0.95,0.22,0.03,0.01,0.28,0.64,0.16,0,0,0,0,0,0,0,0
-0.81,0.02,0.76,0.02,0.91,0.36,0.07,0.51,1,0,1,0,1,0,0,0
+0.81,0.02,0.76,0.02,0.91,0.36,0.07,0.51,1,0,1,0,1,1,0,0
0.07,0.94,0.29,0.03,0.02,0.18,0.88,0.08,0,0,0,0,0,0,0,0
-0.83,0.03,0.5,0.03,0.89,0.21,0.07,0.49,0,0,0,0,1,0,0,0
-0.52,0.03,0.9,0.06,0.91,0.06,0.04,0.12,1,0,0,0,1,0,0,0
-0.26,0.31,0.08,0.21,0.02,0.28,0.9,0.28,0,0,0,0,0,0,0,0
+0.83,0.03,0.5,0.03,0.89,0.21,0.07,0.49,0,0,0,0,1,1,0,0
+0.52,0.03,0.9,0.06,0.91,0.06,0.04,0.12,1,0,0,0,1,1,0,0
+0.26,0.31,0.08,0.21,0.02,0.28,0.9,0.28,0,0,0,0,0,1,1,0
0.08,0.97,0.74,0.02,0.02,0.15,0.89,0.07,0,0,0,0,0,0,0,0
0.41,0.95,0.11,0.03,0.01,0.26,0.82,0.23,0,0,0,0,0,0,0,0
-0.74,0.03,0.75,0.03,0.85,0.23,0.07,0.29,1,0,1,0,1,0,0,0
+0.74,0.03,0.75,0.03,0.85,0.23,0.07,0.29,1,0,1,0,1,1,0,0
0.15,0.69,0.13,0.14,0.02,0.13,0.83,0.12,0,0,0,0,0,0,0,0
0.07,0.98,0.84,0.02,0.01,0.13,0.85,0.05,0,0,0,0,0,0,0,0
0.26,0.96,0.09,0.04,0.02,0.26,0.85,0.13,0,0,0,0,0,0,0,0
0.12,0.8,0.98,0.03,0.01,0.05,0.07,0.05,0,0,0,0,0,0,0,0
0.37,0.95,0.06,0.03,0.01,0.34,0.74,0.23,0,0,0,0,0,0,0,0
0.06,0.96,0.35,0.03,0.01,0.22,0.87,0.07,0,0,0,0,0,0,0,0
-0.78,0.02,0.9,0.03,0.94,0.21,0.06,0.36,1,0,1,0,1,0,0,0
+0.78,0.02,0.9,0.03,0.94,0.21,0.06,0.36,1,0,1,0,1,1,0,0
0.25,0.21,0.76,0.55,0.02,0.07,0.1,0.12,0,0,0,0,0,1,0,0
0.39,0.08,0.08,0.08,0.03,0.09,0.06,0.09,1,0,1,0,0,1,0,0
0.37,0.03,0.99,0.07,0.01,0.09,0.05,0.1,0,0,0,0,0,0,0,0
0.11,0.84,0.71,0.06,0.03,0.31,0.4,0.11,0,0,0,0,0,0,0,0
-0.72,0.12,0.12,0.15,0.02,0.5,0.63,0.71,1,0,1,0,0,1,0,1
+0.72,0.12,0.12,0.15,0.02,0.5,0.63,0.71,1,0,1,0,0,1,1,0
0.36,0.95,0.06,0.04,0.02,0.23,0.77,0.2,0,0,0,0,0,0,0,0
0.1,0.96,0.16,0.03,0.01,0.14,0.53,0.09,0,0,0,0,0,0,0,0
0.07,0.97,0.62,0.02,0.01,0.16,0.8,0.06,0,0,0,0,0,0,0,0
@@ -507,12 +507,12 @@ focus,outreach,automated,customer,calendar_rsvp,needs_decision,needs_response,ne
0.4,0.95,0.07,0.04,0.02,0.28,0.81,0.24,0,0,0,0,0,0,0,0
0.09,0.97,0.65,0.03,0.01,0.16,0.87,0.07,0,0,0,0,0,0,0,0
0.16,0.64,0.17,0.05,0.02,0.15,0.9,0.13,0,0,0,0,0,0,0,0
-0.71,0.04,0.82,0.08,0.9,0.13,0.04,0.6,1,0,0,0,0,0,0,0
+0.71,0.04,0.82,0.08,0.9,0.13,0.04,0.6,1,0,0,0,0,1,0,0
0.17,0.95,0.1,0.05,0.01,0.19,0.48,0.16,0,0,0,0,0,0,0,0
0.08,0.93,0.46,0.03,0.02,0.12,0.8,0.08,0,0,0,0,0,0,0,0
-0.86,0.04,0.57,0.12,0.65,0.81,0.11,0.9,0,0,0,0,1,0,0,0
+0.86,0.04,0.57,0.12,0.65,0.81,0.11,0.9,0,0,0,0,1,1,1,0
0.07,0.97,0.76,0.02,0.01,0.14,0.74,0.05,1,0,0,0,0,0,0,0
-0.73,0.03,0.91,0.03,0.95,0.09,0.05,0.29,0,0,1,0,1,0,0,0
+0.73,0.03,0.91,0.03,0.95,0.09,0.05,0.29,0,0,1,0,1,1,0,0
0.07,0.96,0.57,0.03,0.02,0.18,0.86,0.09,0,0,0,0,0,0,0,0
0.11,0.67,0.11,0.07,0.01,0.23,0.09,0.15,0,0,0,0,0,0,0,0
0.07,0.98,0.54,0.02,0.02,0.15,0.52,0.05,0,0,0,0,0,0,0,0
@@ -524,13 +524,13 @@ focus,outreach,automated,customer,calendar_rsvp,needs_decision,needs_response,ne
0.07,0.96,0.23,0.03,0.01,0.08,0.58,0.06,0,0,0,0,0,0,0,0
0.08,0.96,0.46,0.03,0.02,0.17,0.82,0.06,0,0,0,0,0,0,0,0
0.07,0.97,0.59,0.02,0.01,0.13,0.74,0.04,0,0,0,0,0,0,0,0
-0.71,0.03,0.72,0.02,0.85,0.28,0.07,0.21,1,0,1,0,1,0,0,0
+0.71,0.03,0.72,0.02,0.85,0.28,0.07,0.21,1,0,1,0,1,1,0,0
0.1,0.96,0.11,0.03,0.02,0.23,0.87,0.08,0,0,0,0,0,0,0,0
-0.59,0.38,0.6,0.32,0.86,0.39,0.08,0.62,1,0,0,0,1,0,0,0
-0.76,0.03,0.77,0.02,0.87,0.34,0.07,0.29,1,0,1,0,1,0,0,0
+0.59,0.38,0.6,0.32,0.86,0.39,0.08,0.62,1,0,0,0,1,1,0,0
+0.76,0.03,0.77,0.02,0.87,0.34,0.07,0.29,1,0,1,0,1,1,0,0
0.32,0.95,0.11,0.03,0.02,0.33,0.6,0.19,0,0,0,0,0,0,0,0
0.06,0.95,0.56,0.04,0.02,0.26,0.82,0.08,0,0,0,0,0,0,0,0
-0.62,0.05,0.95,0.09,0.87,0.14,0.07,0.21,1,0,0,0,0,0,0,0
+0.62,0.05,0.95,0.09,0.87,0.14,0.07,0.21,1,0,0,0,0,1,0,0
0.35,0.94,0.07,0.06,0.01,0.35,0.93,0.44,0,0,0,0,0,0,0,0
0.07,0.96,0.78,0.04,0.01,0.13,0.79,0.06,0,0,0,0,0,0,0,0
0.09,0.96,0.36,0.03,0.01,0.17,0.86,0.08,0,0,0,0,0,0,0,0
@@ -541,7 +541,7 @@ focus,outreach,automated,customer,calendar_rsvp,needs_decision,needs_response,ne
0.07,0.96,0.64,0.02,0.02,0.09,0.63,0.06,0,0,0,0,0,0,0,0
0.09,0.97,0.48,0.03,0.01,0.14,0.71,0.06,0,0,0,0,0,0,0,0
0.09,0.95,0.78,0.03,0.03,0.16,0.81,0.07,0,0,0,0,0,0,0,0
-0.7,0.03,0.89,0.02,0.91,0.1,0.06,0.12,1,0,1,0,1,0,0,0
+0.7,0.03,0.89,0.02,0.91,0.1,0.06,0.12,1,0,1,0,1,1,0,0
0.08,0.95,0.47,0.04,0.02,0.13,0.1,0.07,0,1,0,1,0,0,0,0
0.11,0.92,0.98,0.03,0.06,0.13,0.17,0.05,0,0,0,0,0,0,0,0
0.24,0.95,0.08,0.04,0.01,0.23,0.25,0.17,0,0,0,0,0,0,0,0
@@ -550,28 +550,28 @@ focus,outreach,automated,customer,calendar_rsvp,needs_decision,needs_response,ne
0.28,0.37,0.96,0.13,0.05,0.26,0.39,0.64,0,0,0,0,0,0,0,0
0.07,0.97,0.45,0.02,0.01,0.13,0.6,0.06,0,0,0,0,0,0,0,0
0.07,0.98,0.48,0.03,0.02,0.1,0.1,0.07,0,1,0,1,0,0,0,0
-0.78,0.02,0.77,0.03,0.92,0.39,0.08,0.44,1,0,1,0,1,0,0,0
+0.78,0.02,0.77,0.03,0.92,0.39,0.08,0.44,1,0,1,0,1,1,0,0
0.08,0.97,0.94,0.03,0.02,0.08,0.5,0.04,0,0,0,0,0,0,0,0
0.16,0.51,0.26,0.07,0.02,0.2,0.89,0.35,0,0,0,0,0,0,0,0
0.09,0.76,0.97,0.03,0.07,0.05,0.05,0.04,0,0,0,0,0,0,0,0
-0.73,0.02,0.84,0.02,0.9,0.11,0.06,0.13,1,0,1,0,1,0,0,0
+0.73,0.02,0.84,0.02,0.9,0.11,0.06,0.13,1,0,1,0,1,1,0,0
0.24,0.03,0.98,0.06,0.01,0.07,0.06,0.07,0,0,0,0,0,0,0,0
0.38,0.94,0.09,0.04,0.01,0.38,0.88,0.33,0,0,0,0,0,0,0,0
0.06,0.96,0.82,0.04,0.01,0.16,0.85,0.06,0,0,0,0,0,0,0,0
0.07,0.97,0.87,0.02,0.01,0.1,0.62,0.04,0,0,0,0,0,0,0,0
-0.71,0.03,0.92,0.03,0.92,0.1,0.05,0.31,0,0,1,0,1,0,0,0
-0.58,0.02,0.91,0.03,0.93,0.04,0.04,0.07,1,0,1,0,1,0,0,0
+0.71,0.03,0.92,0.03,0.92,0.1,0.05,0.31,0,0,1,0,1,1,0,0
+0.58,0.02,0.91,0.03,0.93,0.04,0.04,0.07,1,0,1,0,1,1,0,0
0.07,0.98,0.88,0.02,0.01,0.1,0.79,0.05,0,0,0,0,0,0,0,0
0.09,0.97,0.59,0.03,0.02,0.13,0.83,0.07,0,0,0,0,0,0,0,0
0.09,0.98,0.59,0.02,0.02,0.3,0.42,0.06,0,0,0,0,0,0,0,0
0.21,0.12,0.09,0.62,0.01,0.16,0.75,0.34,0,0,0,0,0,1,1,0
-0.46,0.06,0.91,0.09,0.95,0.08,0.04,0.15,0,0,0,0,1,0,0,0
+0.46,0.06,0.91,0.09,0.95,0.08,0.04,0.15,0,0,0,0,1,1,0,0
0.08,0.98,0.46,0.02,0.01,0.16,0.78,0.06,0,0,0,0,0,0,0,0
0.07,0.94,0.43,0.02,0.02,0.13,0.86,0.06,0,0,0,0,0,0,0,0
0.08,0.97,0.58,0.02,0.02,0.14,0.81,0.06,0,0,0,0,0,0,0,0
0.17,0.96,0.13,0.04,0.02,0.27,0.59,0.11,0,0,0,0,0,0,0,0
0.09,0.93,0.7,0.04,0.02,0.12,0.66,0.08,0,0,0,0,0,0,0,0
-0.24,0.4,0.15,0.45,0.02,0.16,0.9,0.31,0,0,0,0,0,0,0,0
+0.24,0.4,0.15,0.45,0.02,0.16,0.9,0.31,0,0,0,0,0,1,1,0
0.13,0.96,0.11,0.03,0.02,0.2,0.83,0.1,0,0,0,0,0,0,0,0
0.07,0.97,0.68,0.03,0.01,0.14,0.8,0.06,0,0,0,0,0,0,0,0
0.08,0.97,0.67,0.03,0.01,0.11,0.59,0.05,0,0,0,0,0,0,0,0
@@ -581,19 +581,19 @@ focus,outreach,automated,customer,calendar_rsvp,needs_decision,needs_response,ne
0.27,0.96,0.14,0.03,0.01,0.34,0.72,0.19,0,0,0,0,0,0,0,0
0.37,0.94,0.1,0.04,0.01,0.25,0.61,0.14,0,0,0,0,0,0,0,0
0.08,0.96,0.73,0.03,0.01,0.11,0.63,0.06,0,0,0,0,0,0,0,0
-0.72,0.03,0.88,0.03,0.91,0.12,0.06,0.12,1,0,1,0,1,0,0,0
-0.69,0.03,0.39,0.07,0.3,0.42,0.27,0.52,1,0,0,0,1,0,0,0
+0.72,0.03,0.88,0.03,0.91,0.12,0.06,0.12,1,0,1,0,1,1,0,0
+0.69,0.03,0.39,0.07,0.3,0.42,0.27,0.52,1,0,0,0,1,1,0,0
0.08,0.96,0.59,0.03,0.01,0.21,0.83,0.06,0,0,0,0,0,0,0,0
-0.76,0.02,0.82,0.02,0.92,0.36,0.06,0.31,1,0,1,0,1,0,0,0
+0.76,0.02,0.82,0.02,0.92,0.36,0.06,0.31,1,0,1,0,1,1,0,0
0.08,0.97,0.92,0.02,0.02,0.07,0.65,0.05,0,0,0,0,0,0,0,0
-0.83,0.04,0.34,0.07,0.86,0.32,0.09,0.65,1,0,0,0,1,0,0,0
+0.83,0.04,0.34,0.07,0.86,0.32,0.09,0.65,1,0,0,0,1,1,0,0
0.07,0.97,0.59,0.03,0.02,0.22,0.87,0.07,0,0,0,0,0,0,0,0
-0.8,0.03,0.71,0.03,0.86,0.18,0.08,0.67,1,0,1,0,1,0,0,0
+0.8,0.03,0.71,0.03,0.86,0.18,0.08,0.67,1,0,1,0,1,1,0,0
0.46,0.92,0.08,0.03,0.01,0.37,0.6,0.25,0,0,0,0,0,0,0,0
0.33,0.96,0.08,0.03,0.01,0.24,0.82,0.18,0,0,0,0,0,0,0,0
0.08,0.91,0.36,0.05,0.03,0.14,0.52,0.09,0,0,0,0,0,0,0,0
0.07,0.96,0.54,0.02,0.02,0.15,0.68,0.06,0,0,0,0,0,0,0,0
-0.54,0.05,0.96,0.07,0.79,0.08,0.05,0.14,1,0,0,0,0,0,0,0
+0.54,0.05,0.96,0.07,0.79,0.08,0.05,0.14,1,0,0,0,0,1,0,0
0.35,0.94,0.07,0.04,0.01,0.38,0.83,0.19,0,0,0,0,0,0,0,0
0.36,0.96,0.08,0.04,0.02,0.27,0.72,0.19,0,0,0,0,0,0,0,0
0.1,0.94,0.91,0.02,0.03,0.09,0.26,0.05,0,0,0,0,0,0,0,0
@@ -604,38 +604,38 @@ focus,outreach,automated,customer,calendar_rsvp,needs_decision,needs_response,ne
0.38,0.94,0.09,0.03,0.01,0.4,0.76,0.24,0,0,0,0,0,0,0,0
0.08,0.98,0.47,0.03,0.01,0.16,0.65,0.06,0,0,0,0,0,0,0,0
0.08,0.97,0.46,0.04,0.02,0.14,0.81,0.08,0,0,0,0,0,0,0,0
-0.57,0.03,0.91,0.03,0.93,0.05,0.04,0.08,1,0,1,0,1,0,0,0
+0.57,0.03,0.91,0.03,0.93,0.05,0.04,0.08,1,0,1,0,1,1,0,0
0.07,0.97,0.55,0.02,0.02,0.11,0.75,0.05,0,0,0,0,0,0,0,0
0.07,0.97,0.89,0.02,0.01,0.1,0.7,0.05,0,0,0,0,0,0,0,0
-0.62,0.04,0.92,0.03,0.95,0.11,0.05,0.13,0,0,1,0,1,0,0,0
+0.62,0.04,0.92,0.03,0.95,0.11,0.05,0.13,0,0,1,0,1,1,0,0
0.14,0.8,0.75,0.07,0.01,0.39,0.59,0.11,0,0,0,0,0,0,0,0
-0.79,0.02,0.77,0.03,0.92,0.43,0.08,0.5,1,0,1,0,1,0,0,0
+0.79,0.02,0.77,0.03,0.92,0.43,0.08,0.5,1,0,1,0,1,1,0,0
0.1,0.8,0.97,0.04,0.01,0.06,0.08,0.07,0,0,0,0,0,0,0,0
-0.75,0.03,0.84,0.02,0.9,0.13,0.06,0.15,1,0,1,0,1,0,0,0
+0.75,0.03,0.84,0.02,0.9,0.13,0.06,0.15,1,0,1,0,1,1,0,0
0.07,0.97,0.62,0.02,0.01,0.17,0.86,0.06,0,0,0,0,0,0,0,0
-0.75,0.04,0.91,0.04,0.95,0.13,0.06,0.41,0,0,1,0,1,0,0,0
+0.75,0.04,0.91,0.04,0.95,0.13,0.06,0.41,0,0,1,0,1,1,0,0
0.07,0.97,0.53,0.02,0.01,0.2,0.87,0.07,0,0,0,0,0,0,0,0
0.45,0.05,0.12,0.93,0.02,0.22,0.54,0.46,1,0,0,0,0,1,0,0
0.13,0.96,0.99,0.02,0.01,0.04,0.07,0.04,0,0,0,0,0,0,0,0
-0.29,0.47,0.45,0.08,0.81,0.23,0.07,0.51,1,0,0,0,0,0,0,0
+0.29,0.47,0.45,0.08,0.81,0.23,0.07,0.51,1,0,0,0,0,1,0,0
0.07,0.97,0.75,0.03,0.02,0.09,0.46,0.05,0,0,0,0,0,0,0,0
0.63,0.21,0.22,0.13,0.02,0.67,0.66,0.68,0,0,0,0,0,1,0,0
-0.81,0.04,0.64,0.08,0.92,0.22,0.06,0.55,1,0,0,0,1,0,0,0
+0.81,0.04,0.64,0.08,0.92,0.22,0.06,0.55,1,0,0,0,1,1,0,0
0.08,0.96,0.32,0.02,0.01,0.2,0.72,0.06,0,0,0,0,0,0,0,0
-0.58,0.04,0.92,0.03,0.95,0.13,0.06,0.13,0,0,1,0,1,0,0,0
+0.58,0.04,0.92,0.03,0.95,0.13,0.06,0.13,0,0,1,0,1,1,0,0
0.08,0.97,0.94,0.03,0.01,0.1,0.61,0.05,0,0,0,0,0,0,0,0
0.22,0.03,0.99,0.05,0.01,0.08,0.06,0.06,0,0,0,0,0,0,0,0
-0.5,0.47,0.64,0.26,0.87,0.4,0.08,0.61,1,0,0,0,1,0,0,0
-0.57,0.27,0.11,0.09,0.08,0.65,0.91,0.72,1,1,0,0,0,1,1,1
+0.5,0.47,0.64,0.26,0.87,0.4,0.08,0.61,1,0,0,0,1,1,0,0
+0.57,0.27,0.11,0.09,0.08,0.65,0.91,0.72,1,1,0,0,0,1,1,0
0.1,0.69,0.33,0.03,0.02,0.32,0.88,0.1,0,0,0,0,0,0,0,0
0.07,0.97,0.8,0.03,0.01,0.13,0.73,0.05,0,0,0,0,0,0,0,0
-0.83,0.02,0.75,0.03,0.89,0.53,0.08,0.58,1,0,1,0,1,0,0,0
-0.75,0.03,0.62,0.03,0.85,0.57,0.12,0.53,1,0,1,0,1,0,0,0
-0.71,0.03,0.84,0.02,0.88,0.41,0.09,0.29,1,0,1,0,1,0,0,0
+0.83,0.02,0.75,0.03,0.89,0.53,0.08,0.58,1,0,1,0,1,1,0,0
+0.75,0.03,0.62,0.03,0.85,0.57,0.12,0.53,1,0,1,0,1,1,0,0
+0.71,0.03,0.84,0.02,0.88,0.41,0.09,0.29,1,0,1,0,1,1,0,0
0.07,0.96,0.51,0.02,0.02,0.24,0.84,0.07,0,0,0,0,0,0,0,0
-0.5,0.07,0.17,0.88,0.02,0.27,0.8,0.71,1,0,1,0,0,1,1,1
-0.79,0.03,0.55,0.03,0.84,0.14,0.07,0.38,1,0,1,0,1,0,0,0
-0.76,0.02,0.89,0.03,0.85,0.14,0.05,0.22,1,0,1,0,1,0,0,0
+0.5,0.07,0.17,0.88,0.02,0.27,0.8,0.71,1,0,1,0,0,1,1,0
+0.79,0.03,0.55,0.03,0.84,0.14,0.07,0.38,1,0,1,0,1,1,0,0
+0.76,0.02,0.89,0.03,0.85,0.14,0.05,0.22,1,0,1,0,1,1,0,0
0.42,0.93,0.09,0.04,0.02,0.4,0.53,0.31,0,0,0,0,0,0,0,0
0.08,0.36,0.77,0.06,0.04,0.04,0.05,0.07,0,0,0,0,0,0,0,0
0.06,0.96,0.77,0.03,0.01,0.19,0.84,0.06,0,0,0,0,0,0,0,0
diff --git a/crates/email/src/domain/focus/rule/test.rs b/crates/email/src/domain/focus/rule/test.rs
index 6d519ae082..1c0ae2dea4 100644
--- a/crates/email/src/domain/focus/rule/test.rs
+++ b/crates/email/src/domain/focus/rule/test.rs
@@ -1,10 +1,19 @@
+use chrono::{DateTime, TimeZone, Utc};
+
use super::*;
use crate::domain::focus::FocusQuestion;
/// Jev's recorded answers (the shipped questions, with the owner's memory as
/// context) and the owner's signals for 643 real signal threads from one
-/// founder's inbox, with the verdicts of the experiment's reference
-/// implementation. No email content.
+/// founder's inbox, with the current rule's verdicts. No email content.
+///
+/// The experiment predates some signals, so `parse_row` maps its columns onto
+/// them at their widest: every contact reads as recent since nothing recorded
+/// dates. It also records only whether a thread's first subject looks like
+/// calendar mail, with no method, sequence or event date, so it can't tell a
+/// change from a new invite or an RSVP. The expected verdicts read every such
+/// thread as a change to an event that isn't over, and
+/// `the_experiment_bounds_what_calendar_mail_adds` counts the other extreme.
const EXPERIMENT: &str = include_str!("experiment.csv");
fn flag(value: &str) -> bool {
@@ -15,7 +24,18 @@ fn flag(value: &str) -> bool {
}
}
-fn parse_row(line: &str) -> (FocusAnswers, FocusSignals, (bool, bool, bool)) {
+fn event_end() -> DateTime<Utc> {
+ Utc.with_ymd_and_hms(2026, 11, 10, 17, 0, 0).unwrap()
+}
+
+fn change() -> CalendarNotice {
+ CalendarNotice::Change { until: event_end() }
+}
+
+fn parse_row(
+ line: &str,
+ calendar_subject: CalendarNotice,
+) -> (FocusAnswers, FocusSignals, (bool, bool, bool)) {
let cells = line.split(',').collect::<Vec<_>>();
let answer_count = FocusQuestion::ALL.len();
assert_eq!(cells.len(), answer_count + 8, "bad fixture row: {line}");
@@ -26,12 +46,20 @@ fn parse_row(line: &str) -> (FocusAnswers, FocusSignals, (bool, bool, bool)) {
.collect::<Vec<_>>();
let answers =
FocusAnswers::from_probabilities(&probabilities).expect("one answer per question");
+ let contact = flag(rest[0]);
+ let replied = flag(rest[1]);
+ let latest_from_owner = flag(rest[3]);
let signals = FocusSignals {
- contact: flag(rest[0]),
- replied: flag(rest[1]),
+ contact,
+ recent_contact: contact,
+ // A reply that didn't make the sender a contact only turned them away.
+ brushed_off: replied && !contact,
+ replied,
teammate: flag(rest[2]),
- latest_from_owner: flag(rest[3]),
- calendar_subject: flag(rest[4]),
+ latest_from_owner,
+ // Only a notice nobody wrote after counts.
+ calendar: (flag(rest[4]) && !latest_from_owner).then_some(calendar_subject),
+ awaiting_reply: latest_from_owner && contact,
};
(
answers,
@@ -53,71 +81,175 @@ fn answers(focus: f32) -> FocusAnswers {
}
}
-#[test]
-fn reproduces_the_experiment_verdicts() {
+fn experiment_rows() -> impl Iterator<Item = &'static str> {
let mut rows = EXPERIMENT.lines();
assert!(
rows.next()
.is_some_and(|header| header.starts_with("focus,"))
);
+ rows
+}
+
+/// In Focus for as long as nothing has run out.
+fn in_focus(verdict: &FocusVerdict) -> bool {
+ verdict.is_focus || verdict.calendar_hold_until.is_some()
+}
+
+#[test]
+fn reproduces_the_experiment_verdicts() {
let (mut focus, mut reply, mut follow_up, mut total) = (0, 0, 0, 0);
- for (index, line) in rows.enumerate() {
- let (answers, signals, (expect_focus, expect_reply, expect_follow_up)) = parse_row(line);
+ let mut bands = [0; 5];
+ for (index, line) in experiment_rows().enumerate() {
+ let (answers, signals, (expect_focus, expect_reply, expect_follow_up)) =
+ parse_row(line, change());
let verdict = decide(&answers, &signals);
- assert_eq!(verdict.is_focus, expect_focus, "row {index}: {line}");
+ assert_eq!(in_focus(&verdict), expect_focus, "row {index}: {line}");
assert_eq!(verdict.needs_reply, expect_reply, "row {index}: {line}");
assert_eq!(
verdict.needs_follow_up, expect_follow_up,
"row {index}: {line}"
);
+ assert!(
+ !(verdict.needs_reply && verdict.needs_follow_up),
+ "row {index}: {line}"
+ );
total += 1;
- focus += usize::from(verdict.is_focus);
+ focus += usize::from(in_focus(&verdict));
reply += usize::from(verdict.needs_reply);
follow_up += usize::from(verdict.needs_follow_up);
+ if in_focus(&verdict) {
+ bands[usize::from(verdict.importance.min(99) / 20)] += 1;
+ }
}
- assert_eq!((total, focus, reply, follow_up), (643, 84, 27, 25));
+ assert_eq!((total, focus, reply, follow_up), (643, 218, 48, 4));
+ // Nothing in Focus reads below the 40-59 band on this inbox.
+ assert_eq!(bands, [0, 0, 163, 42, 13]);
}
#[test]
-fn calendar_subjects() {
- for subject in [
- "Accepted: Demo @ Fri Aug 7, 2026",
- "Tentatively Accepted: Sync Team Meet",
- "Updated invitation with note: Scoping/Planning",
- "Canceled event: Standup @ Fri",
- "Invitation : spaced colon",
- "DECLINED: Prod Deploy",
- ] {
- assert!(is_calendar_subject(subject), "{subject}");
- }
- for subject in [
- "Re: Invitation: thoughts on the deck",
- "Invitations for the launch party",
- "Greg x Teo",
- "",
- ] {
- assert!(!is_calendar_subject(subject), "{subject}");
- }
+fn the_experiment_bounds_what_calendar_mail_adds() {
+ let count = |calendar_subject| {
+ let (mut focus, mut reply, mut follow_up) = (0, 0, 0);
+ for line in experiment_rows() {
+ let (answers, signals, _) = parse_row(line, calendar_subject);
+ let verdict = decide(&answers, &signals);
+ focus += usize::from(in_focus(&verdict));
+ reply += usize::from(verdict.needs_reply);
+ follow_up += usize::from(verdict.needs_follow_up);
+ }
+ (focus, reply, follow_up)
+ };
+ // Every thread whose first subject looks like calendar mail is a change to
+ // an event that isn't over, or none is.
+ assert_eq!(count(change()), (218, 48, 4));
+ assert_eq!(count(CalendarNotice::NewInvite), (96, 44, 4));
}
#[test]
-fn calendar_notices_stay_out_even_when_relevant() {
- let mut relevant = answers(0.9);
- let by_subject = decide(
- &relevant,
+fn a_calendar_change_is_held_whatever_else_it_reads_as() {
+ // A reschedule reads as automated, low relevance and from a stranger.
+ let mut reschedule = answers(0.05);
+ reschedule.automated = 0.95;
+ reschedule.outreach = 0.8;
+ let verdict = decide(
+ &reschedule,
+ &FocusSignals {
+ calendar: Some(change()),
+ ..FocusSignals::default()
+ },
+ );
+ assert_eq!(verdict.calendar_hold_until, Some(event_end()));
+ assert_eq!(verdict.importance, 50);
+ // Once the hold ends nothing else keeps it in, and it reads as what it is.
+ assert!(!verdict.is_focus && !verdict.pinned);
+ assert_eq!(verdict.category, FocusCategory::Outreach);
+}
+
+#[test]
+fn a_calendar_change_on_mail_that_earned_focus_stays_in_after_the_hold() {
+ let signals = FocusSignals {
+ calendar: Some(change()),
+ contact: true,
+ ..FocusSignals::default()
+ };
+ let verdict = decide(&answers(0.8), &signals);
+ assert!(verdict.is_focus && verdict.calendar_hold_until.is_some());
+ assert_eq!(verdict.category, FocusCategory::Known);
+}
+
+#[test]
+fn a_held_calendar_change_can_still_need_a_reply() {
+ let mut asked = answers(0.05);
+ asked.automated = 0.95;
+ asked.needs_response = 0.9;
+ let verdict = decide(
+ &asked,
&FocusSignals {
- calendar_subject: true,
- contact: true,
+ calendar: Some(change()),
..FocusSignals::default()
},
);
- assert!(!by_subject.is_focus);
- assert_eq!(by_subject.category, FocusCategory::Calendar);
+ assert!(!verdict.is_focus && verdict.needs_reply && !verdict.needs_follow_up);
+ assert_eq!(verdict.importance, 70);
+}
- relevant.calendar_rsvp = 0.65;
- assert!(decide(&relevant, &FocusSignals::default()).is_focus);
- relevant.calendar_rsvp = 0.7;
- assert!(!decide(&relevant, &FocusSignals::default()).is_focus);
+#[test]
+fn new_invites_and_rsvps_stay_out_even_when_relevant() {
+ let mut relevant = answers(0.9);
+ relevant.needs_response = 0.9;
+ for notice in [CalendarNotice::NewInvite, CalendarNotice::Rsvp] {
+ let verdict = decide(
+ &relevant,
+ &FocusSignals {
+ calendar: Some(notice),
+ contact: true,
+ recent_contact: true,
+ ..FocusSignals::default()
+ },
+ );
+ assert!(!verdict.is_focus && !verdict.pinned, "{notice:?}");
+ assert_eq!(verdict.calendar_hold_until, None, "{notice:?}");
+ assert!(!verdict.needs_reply, "{notice:?}");
+ assert_eq!(verdict.category, FocusCategory::Calendar, "{notice:?}");
+ }
+}
+
+#[test]
+fn jevs_calendar_answer_neither_holds_nor_vetoes() {
+ // Jev alone is sure this is calendar mail: it takes the normal rule either way.
+ let mut unwanted = answers(0.05);
+ unwanted.calendar_rsvp = 0.95;
+ let verdict = decide(&unwanted, &FocusSignals::default());
+ assert!(!verdict.is_focus);
+ assert_eq!(verdict.calendar_hold_until, None);
+ assert_eq!(verdict.category, FocusCategory::LowRelevance);
+
+ let mut wanted = answers(0.8);
+ wanted.calendar_rsvp = 0.95;
+ let contact = FocusSignals {
+ contact: true,
+ ..FocusSignals::default()
+ };
+ let verdict = decide(&wanted, &contact);
+ assert!(verdict.is_focus);
+ assert_eq!(verdict.category, FocusCategory::Known);
+ let verdict = decide(&wanted, &FocusSignals::default());
+ assert!(verdict.is_focus);
+ assert_eq!(verdict.category, FocusCategory::Other);
+}
+
+#[test]
+fn calendar_only_ever_describes_a_change_or_a_thread_left_out() {
+ for line in experiment_rows() {
+ for notice in [change(), CalendarNotice::NewInvite, CalendarNotice::Rsvp] {
+ let (answers, signals, _) = parse_row(line, notice);
+ let verdict = decide(&answers, &signals);
+ if verdict.category == FocusCategory::Calendar {
+ assert!(!verdict.is_focus && !verdict.awaiting_reply, "{line}");
+ assert_eq!(verdict.calendar_hold_until, None, "{line}");
+ }
+ }
+ }
}
#[test]
@@ -151,11 +283,15 @@ fn contacts_get_a_lower_bar_unless_it_is_a_pitch() {
assert!(!decide(&borderline, &FocusSignals::default()).is_focus);
let verdict = decide(&borderline, &contact);
assert!(verdict.is_focus);
+ assert!(!verdict.pinned);
assert_eq!(verdict.category, FocusCategory::Known);
let mut pitch = borderline;
pitch.outreach = 0.8;
assert!(!decide(&pitch, &contact).is_focus);
+
+ // Long-ago correspondence alone doesn't hold a thread in Focus.
+ assert!(!decide(&answers(0.05), &contact).is_focus);
}
#[test]
@@ -179,6 +315,51 @@ fn automated_mail_needs_someone_to_vouch_for_it() {
assert_eq!(verdict.category, FocusCategory::Customer);
}
+#[test]
+fn a_reply_the_owner_owes_stays_in_on_a_low_score() {
+ let mut asked = answers(0.1);
+ asked.needs_response = 0.9;
+ let verdict = decide(&asked, &FocusSignals::default());
+ assert!(verdict.is_focus && verdict.pinned && verdict.needs_reply);
+ assert_eq!(verdict.category, FocusCategory::Other);
+ assert_eq!(verdict.importance, 70);
+
+ let mut pitch = asked;
+ pitch.outreach = 0.8;
+ assert!(!decide(&pitch, &FocusSignals::default()).is_focus);
+
+ let mut newsletter = asked;
+ newsletter.automated = 0.8;
+ assert!(!decide(&newsletter, &FocusSignals::default()).is_focus);
+
+ let turned_away = FocusSignals {
+ brushed_off: true,
+ ..FocusSignals::default()
+ };
+ assert!(!decide(&asked, &turned_away).is_focus);
+}
+
+#[test]
+fn mail_from_recent_contacts_stays_in_on_a_low_score() {
+ let recent = FocusSignals {
+ contact: true,
+ recent_contact: true,
+ ..FocusSignals::default()
+ };
+ let verdict = decide(&answers(0.05), &recent);
+ assert!(verdict.is_focus && verdict.pinned);
+ assert_eq!(verdict.category, FocusCategory::Known);
+ assert_eq!(verdict.importance, 50);
+
+ let mut pitch = answers(0.05);
+ pitch.outreach = 0.8;
+ assert!(!decide(&pitch, &recent).is_focus);
+
+ let mut automated = answers(0.05);
+ automated.automated = 0.8;
+ assert!(!decide(&automated, &recent).is_focus);
+}
+
#[test]
fn flags_need_a_confident_yes() {
let mut maybe = answers(0.8);
@@ -191,15 +372,26 @@ fn flags_need_a_confident_yes() {
}
#[test]
-fn reply_needed_clears_once_the_owner_answers() {
- let mut asked = answers(0.8);
- asked.needs_response = 0.9;
- assert!(decide(&asked, &FocusSignals::default()).needs_reply);
- let answered = FocusSignals {
+fn who_wrote_last_settles_reply_or_follow_up() {
+ let mut flagged = answers(0.8);
+ flagged.needs_response = 0.9;
+ flagged.needs_follow_up = 0.9;
+
+ let theirs = decide(&flagged, &FocusSignals::default());
+ assert!(theirs.needs_reply && !theirs.needs_follow_up);
+
+ let owners = FocusSignals {
latest_from_owner: true,
..FocusSignals::default()
};
- assert!(!decide(&asked, &answered).needs_reply);
+ let ours = decide(&flagged, &owners);
+ assert!(!ours.needs_reply && ours.needs_follow_up);
+
+ // An open loop on someone else's message is still the owner's to answer.
+ let mut open_loop = answers(0.8);
+ open_loop.needs_follow_up = 0.9;
+ let verdict = decide(&open_loop, &FocusSignals::default());
+ assert!(verdict.needs_reply && !verdict.needs_follow_up);
}
#[test]
@@ -216,7 +408,30 @@ fn flags_only_apply_inside_focus() {
}
#[test]
-fn importance_weights_reply_decision_and_relationship() {
+fn a_thread_awaiting_a_reply_waits_for_the_read_to_join_focus() {
+ let waiting = FocusSignals {
+ latest_from_owner: true,
+ awaiting_reply: true,
+ ..FocusSignals::default()
+ };
+ let verdict = decide(&answers(0.05), &waiting);
+ // Age isn't known here: the list decides, and finds it already labeled.
+ assert!(!verdict.is_focus);
+ assert!(verdict.awaiting_reply);
+ assert!(!verdict.needs_follow_up);
+ assert_eq!(verdict.importance, 55);
+ assert_eq!(verdict.category, FocusCategory::Other);
+
+ let mut customer = answers(0.05);
+ customer.customer = 0.8;
+ assert_eq!(
+ decide(&customer, &waiting).category,
+ FocusCategory::Customer
+ );
+}
+
+#[test]
+fn importance_reflects_why_a_thread_is_in() {
let mut everything = answers(1.0);
everything.needs_response = 1.0;
everything.needs_follow_up = 1.0;
@@ -234,6 +449,13 @@ fn importance_weights_reply_decision_and_relationship() {
decide(&answers(0.0), &FocusSignals::default()).importance,
0
);
+
+ // A customer Jev rates low is in Focus, so it doesn't read as noise.
+ let mut customer = answers(0.1);
+ customer.customer = 0.9;
+ assert_eq!(decide(&customer, &FocusSignals::default()).importance, 50);
+ // Neither does a thread that only just cleared the contact bar.
+ assert_eq!(decide(&answers(0.3), &contact).importance, 30);
}
#[test]
diff --git a/crates/email/src/domain/focus/service.rs b/crates/email/src/domain/focus/service.rs
index 457aa0d7dc..c5d5135241 100644
--- a/crates/email/src/domain/focus/service.rs
+++ b/crates/email/src/domain/focus/service.rs
@@ -12,10 +12,13 @@ use uuid::Uuid;
use super::{
input::jev_input,
- models::{FocusAnswers, FocusRecord, FocusThread, FocusVerdict, StaleThread, ThreadFocus},
+ models::{
+ FocusAnswers, FocusMessage, FocusRecord, FocusThread, FocusVerdict, StaleThread,
+ StoredClassification, ThreadFocus,
+ },
ports::{FocusClassifier, FocusClassifierError, FocusStore, ProfileSource},
questions::FOCUS_QUESTIONS,
- rule::decide,
+ rule::{FOCUS_RULE_VERSION, decide},
signals::signals,
};
@@ -23,6 +26,8 @@ use super::{
const MAX_FOCUS_THREADS: i64 = 500;
/// Longest window the Focus list looks back over, in days.
const MAX_WINDOW_DAYS: u16 = 90;
+/// Days the owner waits on an answer before a thread joins Focus to follow up.
+const FOLLOW_UP_AFTER_DAYS: i64 = 3;
/// Rejections in a row that end a sweep: the classifier is refusing every
/// request (a revoked key, say), not one thread.
const MAX_REJECTIONS_IN_A_ROW: usize = 5;
@@ -32,11 +37,15 @@ const MAX_REJECTIONS_IN_A_ROW: usize = 5;
pub enum ClassifyOutcome {
/// Classified and stored.
Classified(FocusVerdict),
- /// Already classified at its latest message.
+ /// The stored answers were decided again under the current rule, without
+ /// calling the classifier.
+ Redecided(FocusVerdict),
+ /// Already classified at its latest message under the current rule.
UpToDate,
/// The thread is gone, or has no message from anyone but the owner.
Missing,
- /// The thread is not in the signal inbox.
+ /// The thread is out of the inbox, or outside the signal inbox without
+ /// being key mail right now.
NotSignal,
/// The inbox is not on the allowlist, so nothing left the system.
NotEnabled,
@@ -53,6 +62,8 @@ pub struct SweepReport {
pub candidates: usize,
/// Threads classified.
pub classified: usize,
+ /// Threads decided again from stored answers, without the classifier.
+ pub redecided: usize,
/// Threads skipped as up to date, missing, not signal or not enabled, or
/// already checked at their latest mail.
pub skipped: usize,
@@ -112,8 +123,22 @@ where
}
/// Classify a thread as it stands now. Safe to call repeatedly: a thread
- /// already classified at its latest message is left alone.
- #[tracing::instrument(skip(self), err)]
+ /// already classified at its latest message under the current rule is left
+ /// alone, and one classified under an older rule is decided again from its
+ /// stored answers without calling the classifier.
+ #[tracing::instrument(
+ skip(self),
+ err,
+ fields(
+ is_focus = tracing::field::Empty,
+ pinned = tracing::field::Empty,
+ calendar_held = tracing::field::Empty,
+ awaiting_reply = tracing::field::Empty,
+ category = tracing::field::Empty,
+ importance = tracing::field::Empty,
+ redecided = tracing::field::Empty,
+ )
+ )]
pub async fn classify_thread(&self, thread_id: Uuid) -> Result<ClassifyOutcome, Report> {
let Some(inbox) = self.store.thread_inbox(thread_id).await? else {
return Ok(ClassifyOutcome::Missing);
@@ -121,7 +146,7 @@ where
if !self.is_enabled(&inbox.owner_email) {
return Ok(ClassifyOutcome::NotEnabled);
}
- if !inbox.is_signal || !inbox.inbox_visible {
+ if !inbox.inbox_visible {
return Ok(ClassifyOutcome::NotSignal);
}
let mail = self.store.thread_mail(&inbox).await?;
@@ -132,9 +157,34 @@ where
if thread.messages.iter().all(|message| message.is_sent) {
return Ok(ClassifyOutcome::Missing);
}
- if thread.classified_message_id == Some(latest.id) {
+ let stored = thread
+ .classified
+ .as_ref()
+ .filter(|stored| stored.message_id == latest.id);
+ if stored.is_some_and(|stored| stored.rule_version == FOCUS_RULE_VERSION) {
return Ok(ClassifyOutcome::UpToDate);
}
+ let signals = signals(&thread);
+ // Same mail, older rule: nothing new for the classifier to read.
+ if let Some(StoredClassification {
+ answers: Some(answers),
+ model,
+ ..
+ }) = stored
+ {
+ let verdict = decide(answers, &signals);
+ let saved = self
+ .save_verdict(&thread, latest, verdict, *answers, model.clone())
+ .await?;
+ if !saved {
+ return Ok(ClassifyOutcome::UpToDate);
+ }
+ record_verdict(&verdict, true);
+ return Ok(ClassifyOutcome::Redecided(verdict));
+ }
+ if !thread.is_signal && !signals.is_key_mail(Utc::now()) {
+ return Ok(ClassifyOutcome::NotSignal);
+ }
// The profile only sharpens the answers; classify without it rather than not at all.
let profile = self
@@ -161,9 +211,28 @@ where
);
return Ok(ClassifyOutcome::Rejected);
};
- let verdict = decide(&answers, &signals(&thread));
- let stored = self
- .store
+ let verdict = decide(&answers, &signals);
+ let model = self.classifier.model().to_owned();
+ let saved = self
+ .save_verdict(&thread, latest, verdict, answers, model)
+ .await?;
+ if !saved {
+ // A concurrent classification of a later message won.
+ return Ok(ClassifyOutcome::UpToDate);
+ }
+ record_verdict(&verdict, false);
+ Ok(ClassifyOutcome::Classified(verdict))
+ }
+
+ async fn save_verdict(
+ &self,
+ thread: &FocusThread,
+ latest: &FocusMessage,
+ verdict: FocusVerdict,
+ answers: FocusAnswers,
+ model: String,
+ ) -> Result<bool, Report> {
+ self.store
.save(&FocusRecord {
thread_id: thread.thread_id,
link_id: thread.link_id,
@@ -171,91 +240,131 @@ where
classified_message_ts: latest.at,
verdict,
answers,
- model: self.classifier.model().to_owned(),
+ model,
classified_at: Utc::now(),
})
- .await?;
- if !stored {
- // A concurrent classification of a later message won.
- return Ok(ClassifyOutcome::UpToDate);
- }
- Ok(ClassifyOutcome::Classified(verdict))
+ .await
}
- /// Classify up to `limit` stale signal threads from the last `window`,
- /// newest first. Stops at the first sign the classifier is unavailable,
- /// or after a run of rejections.
+ /// Check up to `limit` stale threads from the last `window`, newest first,
+ /// and classify those that need it. Stops at the first sign the classifier is
+ /// unavailable, or after a run of rejections.
+ ///
+ /// A thread checked without storing a result stays stale in the store, so the
+ /// sweep pages past those: taking only the newest `limit` would hand every
+ /// sweep the same threads once enough of them pile up, and never reach older
+ /// ones.
#[tracing::instrument(skip(self), err)]
pub async fn sweep(&self, window: Duration, limit: i64) -> Result<SweepReport, Report> {
let mut report = SweepReport::default();
if self.enabled_domains.is_empty() {
return Ok(report);
}
- let candidates = self
- .store
- .stale_threads(Utc::now() - window, &self.enabled_domains, limit)
- .await?;
- report.candidates = candidates.len();
- // Only this sweep's candidates can be skipped, so the map stays as
- // small as one sweep.
- let stale = candidates
- .iter()
- .map(|candidate| candidate.thread_id)
- .collect::<HashSet<_>>();
- self.settled()
- .retain(|thread_id, _| stale.contains(thread_id));
-
+ let since = Utc::now() - window;
+ let mut seen = HashSet::new();
+ let mut after = None;
+ let mut checks = 0;
+ let mut saw_them_all = false;
let mut rejections_in_a_row = 0;
- for StaleThread {
- thread_id,
- latest_at,
- } in candidates
- {
- if self.settled().get(&thread_id) == Some(&latest_at) {
- report.skipped += 1;
- continue;
- }
- let outcome = self.classify_thread(thread_id).await;
- if !matches!(outcome, Ok(ClassifyOutcome::Rejected)) {
- rejections_in_a_row = 0;
+ 'pages: loop {
+ let page = self
+ .store
+ .stale_threads(since, &self.enabled_domains, after, limit)
+ .await?;
+ report.candidates += page.len();
+ let last_page = i64::try_from(page.len()).is_ok_and(|len| len < limit);
+ after = page.last().copied();
+ if after.is_none() {
+ saw_them_all = true;
+ break;
}
- match outcome {
- Ok(ClassifyOutcome::Classified(_)) => {
- report.classified += 1;
- self.settled().remove(&thread_id);
+ for StaleThread {
+ thread_id,
+ latest_at,
+ } in page
+ {
+ seen.insert(thread_id);
+ if self.settled().get(&thread_id) == Some(&latest_at) {
+ report.skipped += 1;
+ continue;
+ }
+ if checks >= limit {
+ break 'pages;
}
- Ok(ClassifyOutcome::Unavailable) => {
- report.failed += 1;
- report.stopped_early = true;
- break;
+ checks += 1;
+ let outcome = self.classify_thread(thread_id).await;
+ if !matches!(outcome, Ok(ClassifyOutcome::Rejected)) {
+ rejections_in_a_row = 0;
}
- Ok(ClassifyOutcome::Rejected) => {
- report.failed += 1;
- self.settled().insert(thread_id, latest_at);
- rejections_in_a_row += 1;
- if rejections_in_a_row >= MAX_REJECTIONS_IN_A_ROW {
- tracing::warn!(
- rejections_in_a_row,
- "focus sweep stopped: the classifier rejects every thread"
- );
+ match outcome {
+ Ok(ClassifyOutcome::Classified(_)) => {
+ report.classified += 1;
+ self.settled().remove(&thread_id);
+ }
+ Ok(ClassifyOutcome::Redecided(_)) => {
+ report.redecided += 1;
+ self.settled().remove(&thread_id);
+ }
+ Ok(ClassifyOutcome::Unavailable) => {
+ report.failed += 1;
report.stopped_early = true;
- break;
+ break 'pages;
+ }
+ Ok(ClassifyOutcome::Rejected) => {
+ report.failed += 1;
+ self.settled().insert(thread_id, latest_at);
+ rejections_in_a_row += 1;
+ if rejections_in_a_row >= MAX_REJECTIONS_IN_A_ROW {
+ tracing::warn!(
+ rejections_in_a_row,
+ "focus sweep stopped: the classifier rejects every thread"
+ );
+ report.stopped_early = true;
+ break 'pages;
+ }
+ }
+ Ok(_) => {
+ report.skipped += 1;
+ self.settled().insert(thread_id, latest_at);
+ }
+ Err(error) => {
+ tracing::warn!(error = ?error, %thread_id, "focus sweep could not classify a thread");
+ report.failed += 1;
}
}
- Ok(_) => {
- report.skipped += 1;
- self.settled().insert(thread_id, latest_at);
- }
- Err(error) => {
- tracing::warn!(error = ?error, %thread_id, "focus sweep could not classify a thread");
- report.failed += 1;
- }
+ }
+ if last_page {
+ saw_them_all = true;
+ break;
}
}
+ // Only stale threads can be skipped, so the map stays as small as one
+ // full sweep. A sweep that stopped short hasn't seen the rest.
+ if saw_them_all {
+ self.settled()
+ .retain(|thread_id, _| seen.contains(thread_id));
+ }
Ok(report)
}
}
+/// Put why a thread landed where it did on the classification's span.
+fn record_verdict(verdict: &FocusVerdict, redecided: bool) {
+ let span = tracing::Span::current();
+ span.record("is_focus", verdict.is_focus);
+ span.record("pinned", verdict.pinned);
+ span.record("calendar_held", verdict.calendar_hold_until.is_some());
+ span.record("awaiting_reply", verdict.awaiting_reply);
+ span.record("category", tracing::field::display(verdict.category));
+ span.record("importance", verdict.importance);
+ span.record("redecided", redecided);
+}
+
+/// The latest time a thread's waiting can have started and still count now.
+fn waiting_before(now: DateTime<Utc>) -> DateTime<Utc> {
+ now - Duration::days(FOLLOW_UP_AFTER_DAYS)
+}
+
/// Reads the Focus view needs.
pub struct FocusQueries<S> {
store: S,
@@ -268,23 +377,28 @@ impl<S: FocusStore> FocusQueries<S> {
}
/// The owner's Focus threads still in the inbox with mail in the last
- /// `window_days` (at most 90), highest priority first.
+ /// `window_days` (at most 90), newest first by latest message time in
+ /// either direction, so the limit keeps the newest threads.
pub async fn focus_thread_ids(
&self,
owner: &MacroUserIdStr<'static>,
window_days: u16,
) -> Result<Vec<Uuid>, Report> {
let days = window_days.clamp(1, MAX_WINDOW_DAYS);
+ let now = Utc::now();
self.store
.focus_thread_ids(
owner,
- Utc::now() - Duration::days(i64::from(days)),
+ now - Duration::days(i64::from(days)),
+ waiting_before(now),
+ now,
MAX_FOCUS_THREADS,
)
.await
}
- /// Stored classifications for the owner's threads among `thread_ids`.
+ /// Classifications of the owner's threads among `thread_ids`, as they stand
+ /// now.
pub async fn focus_for_threads(
&self,
owner: &MacroUserIdStr<'static>,
@@ -293,7 +407,10 @@ impl<S: FocusStore> FocusQueries<S> {
if thread_ids.is_empty() {
return Ok(HashMap::new());
}
- self.store.focus_for_threads(owner, thread_ids).await
+ let now = Utc::now();
+ self.store
+ .focus_for_threads(owner, thread_ids, waiting_before(now), now)
+ .await
}
}
diff --git a/crates/email/src/domain/focus/service/test.rs b/crates/email/src/domain/focus/service/test.rs
index b79c0e4a74..bcb210d078 100644
--- a/crates/email/src/domain/focus/service/test.rs
+++ b/crates/email/src/domain/focus/service/test.rs
@@ -11,7 +11,8 @@ use uuid::Uuid;
use super::*;
use crate::domain::focus::models::{
- FocusCategory, FocusInbox, FocusMail, FocusMessage, FocusThread,
+ CalendarPart, FocusCategory, FocusInbox, FocusMail, FocusMessage, FocusThread, SentNote,
+ StoredClassification,
};
#[derive(Clone, Default)]
@@ -23,6 +24,9 @@ struct FakeStore {
stale: Arc<Mutex<Vec<StaleThread>>>,
stale_domains: Arc<Mutex<Vec<String>>>,
mail_reads: Arc<Mutex<usize>>,
+ /// The cutoff each list read was given.
+ waiting_before: Arc<Mutex<Vec<DateTime<Utc>>>>,
+ read_at: Arc<Mutex<Vec<DateTime<Utc>>>>,
}
impl FocusStore for FakeStore {
@@ -39,7 +43,7 @@ impl FocusStore for FakeStore {
owner_email: thread.owner_email.clone(),
is_signal: thread.is_signal,
inbox_visible: thread.inbox_visible,
- classified_message_id: thread.classified_message_id,
+ classified: thread.classified.clone(),
}))
}
@@ -65,18 +69,35 @@ impl FocusStore for FakeStore {
&self,
_since: DateTime<Utc>,
domains: &[String],
- _limit: i64,
+ after: Option<StaleThread>,
+ limit: i64,
) -> Result<Vec<StaleThread>, Report> {
*self.stale_domains.lock().unwrap() = domains.to_vec();
- Ok(self.stale.lock().unwrap().clone())
+ let stale = self.stale.lock().unwrap();
+ let start = after.map_or(0, |after| {
+ stale
+ .iter()
+ .position(|thread| *thread == after)
+ .map_or(stale.len(), |index| index + 1)
+ });
+ Ok(stale
+ .iter()
+ .skip(start)
+ .take(usize::try_from(limit).unwrap())
+ .copied()
+ .collect())
}
async fn focus_thread_ids(
&self,
_owner: &MacroUserIdStr<'static>,
_since: DateTime<Utc>,
+ waiting_before: DateTime<Utc>,
+ now: DateTime<Utc>,
_limit: i64,
) -> Result<Vec<Uuid>, Report> {
+ self.waiting_before.lock().unwrap().push(waiting_before);
+ self.read_at.lock().unwrap().push(now);
Ok(Vec::new())
}
@@ -84,7 +105,11 @@ impl FocusStore for FakeStore {
&self,
_owner: &MacroUserIdStr<'static>,
_thread_ids: &[Uuid],
+ waiting_before: DateTime<Utc>,
+ now: DateTime<Utc>,
) -> Result<HashMap<Uuid, ThreadFocus>, Report> {
+ self.waiting_before.lock().unwrap().push(waiting_before);
+ self.read_at.lock().unwrap().push(now);
Ok(HashMap::new())
}
}
@@ -143,7 +168,7 @@ fn thread(owner_email: &str) -> FocusThread {
owner_email: owner_email.to_owned(),
is_signal: true,
inbox_visible: true,
- classified_message_id: None,
+ classified: None,
messages: vec![FocusMessage {
id: Uuid::new_v4(),
at: Utc.with_ymd_and_hms(2026, 10, 8, 12, 0, 0).unwrap(),
@@ -155,6 +180,7 @@ fn thread(owner_email: &str) -> FocusThread {
subject: Some("Unpaid invoice".to_owned()),
has_attachments: false,
bulk: false,
+ calendar_part: None,
body: Some("Can you approve the invoice today?".to_owned()),
snippet: None,
}],
@@ -196,6 +222,17 @@ fn noon() -> DateTime<Utc> {
Utc.with_ymd_and_hms(2026, 10, 8, 12, 0, 0).unwrap()
}
+/// A classification of `thread`'s latest message: Focus low, reply needed.
+fn stored_at(thread: &FocusThread, rule_version: i16) -> StoredClassification {
+ let answers = FocusAnswers::from_probabilities(&[0.1, 0.0, 0.0, 0.0, 0.0, 0.0, 0.9, 0.0]);
+ StoredClassification {
+ message_id: thread.messages.last().unwrap().id,
+ rule_version,
+ answers,
+ model: "jev-earlier".to_owned(),
+ }
+}
+
#[tokio::test]
async fn classifies_and_stores_an_enabled_inbox() {
let store = FakeStore::default();
@@ -251,7 +288,7 @@ async fn skips_threads_outside_the_signal_inbox_and_up_to_date_ones() {
archived.inbox_visible = false;
let archived = insert(&store, archived);
let mut current = thread("[email protected]");
- current.classified_message_id = Some(current.messages[0].id);
+ current.classified = Some(stored_at(&current, FOCUS_RULE_VERSION));
let current = insert(&store, current);
let mut only_sent = thread("[email protected]");
only_sent.messages[0].is_sent = true;
@@ -338,6 +375,7 @@ async fn sweep_classifies_every_stale_thread() {
SweepReport {
candidates: 3,
classified: 2,
+ redecided: 0,
skipped: 1,
failed: 0,
stopped_early: false,
@@ -405,6 +443,72 @@ async fn sweep_leaves_rejected_and_settled_threads_until_new_mail() {
assert_eq!(classifier.inputs.lock().unwrap().len(), 2);
}
+#[tokio::test]
+async fn sweeps_reach_threads_behind_ones_that_never_store_a_result() {
+ let store = FakeStore::default();
+ let classifier = FakeClassifier::answering(Ok(relevant()));
+ let service = service(&store, &classifier, &["acme.example"]);
+ // More non-signal threads than one sweep's limit, newest first, then a thread
+ // that is worth classifying.
+ let mut stale = (0..5)
+ .map(|_| {
+ let mut noise = thread("[email protected]");
+ noise.is_signal = false;
+ insert(&store, noise)
+ })
+ .collect::<Vec<_>>();
+ let older = insert(&store, thread("[email protected]"));
+ stale.push(older);
+ set_stale(&store, &stale, noon());
+
+ // Two checks a sweep: the third sweep gets there, past the settled ones.
+ for _ in 0..2 {
+ let report = service.sweep(Duration::days(30), 2).await.unwrap();
+ assert_eq!(report.classified, 0);
+ }
+ let reads_so_far = *store.mail_reads.lock().unwrap();
+ assert_eq!(reads_so_far, 4);
+ let report = service.sweep(Duration::days(30), 2).await.unwrap();
+ assert_eq!(report.classified, 1);
+ assert_eq!(*store.mail_reads.lock().unwrap(), reads_so_far + 2);
+ assert_eq!(classifier.inputs.lock().unwrap().len(), 1);
+
+ // Everything is settled or stored now, however far back the sweep looks.
+ store.stale.lock().unwrap().pop();
+ let report = service.sweep(Duration::days(30), 2).await.unwrap();
+ assert_eq!(report.skipped, 5);
+ assert_eq!(*store.mail_reads.lock().unwrap(), reads_so_far + 2);
+}
+
+#[tokio::test]
+async fn only_a_full_sweep_forgets_what_it_settled() {
+ let store = FakeStore::default();
+ let classifier = FakeClassifier::answering(Err(FocusClassifierError::Unavailable));
+ let service = service(&store, &classifier, &["acme.example"]);
+ let mut noise = thread("[email protected]");
+ noise.is_signal = false;
+ let noise = insert(&store, noise);
+ let down = insert(&store, thread("[email protected]"));
+ set_stale(&store, &[noise], noon());
+ service.sweep(Duration::days(30), 100).await.unwrap();
+ assert_eq!(*store.mail_reads.lock().unwrap(), 1);
+
+ // Cut short before it reached the settled thread: that says nothing about it.
+ set_stale(&store, &[down, noise], noon());
+ let report = service.sweep(Duration::days(30), 100).await.unwrap();
+ assert!(report.stopped_early);
+ assert_eq!(*store.mail_reads.lock().unwrap(), 2);
+ set_stale(&store, &[noise], noon());
+ let report = service.sweep(Duration::days(30), 100).await.unwrap();
+ assert_eq!(report.skipped, 1);
+ assert_eq!(*store.mail_reads.lock().unwrap(), 2);
+
+ // A full sweep that no longer finds it forgets it, so the map stays small.
+ set_stale(&store, &[], noon());
+ service.sweep(Duration::days(30), 100).await.unwrap();
+ assert!(service.settled().is_empty());
+}
+
#[tokio::test]
async fn sweep_stops_when_the_classifier_rejects_everything() {
let store = FakeStore::default();
@@ -425,3 +529,247 @@ async fn sweep_stops_when_the_classifier_rejects_everything() {
MAX_REJECTIONS_IN_A_ROW
);
}
+
+#[tokio::test]
+async fn an_older_rule_is_decided_again_without_the_classifier() {
+ let store = FakeStore::default();
+ let classifier = FakeClassifier::answering(Err(FocusClassifierError::Unavailable));
+ let mut legacy = thread("[email protected]");
+ legacy.classified = Some(stored_at(&legacy, 0));
+ let id = insert(&store, legacy);
+
+ let outcome = service(&store, &classifier, &["acme.example"])
+ .classify_thread(id)
+ .await
+ .unwrap();
+ let ClassifyOutcome::Redecided(verdict) = outcome else {
+ panic!("expected a new decision, got {outcome:?}");
+ };
+ // The stored answers now read as a reply the owner owes, on a low score.
+ assert!(verdict.is_focus && verdict.pinned && verdict.needs_reply);
+ assert!(classifier.inputs.lock().unwrap().is_empty());
+ let saved = store.saved.lock().unwrap();
+ assert_eq!(saved.len(), 1);
+ assert_eq!(saved[0].model, "jev-earlier");
+ assert_eq!(saved[0].answers.needs_response, 0.9);
+}
+
+#[tokio::test]
+async fn sweep_counts_threads_decided_again_apart_from_classified_ones() {
+ let store = FakeStore::default();
+ let classifier = FakeClassifier::answering(Ok(relevant()));
+ let mut legacy = thread("[email protected]");
+ legacy.classified = Some(stored_at(&legacy, 0));
+ let legacy = insert(&store, legacy);
+ let new = insert(&store, thread("[email protected]"));
+ set_stale(&store, &[legacy, new], noon());
+
+ let report = service(&store, &classifier, &["acme.example"])
+ .sweep(Duration::days(30), 100)
+ .await
+ .unwrap();
+ assert_eq!((report.classified, report.redecided), (1, 1));
+ assert_eq!(classifier.inputs.lock().unwrap().len(), 1);
+}
+
+#[tokio::test]
+async fn answers_that_no_longer_decode_go_back_to_the_classifier() {
+ let store = FakeStore::default();
+ let classifier = FakeClassifier::answering(Ok(relevant()));
+ let mut unreadable = thread("[email protected]");
+ unreadable.classified = Some(StoredClassification {
+ answers: None,
+ ..stored_at(&unreadable, 0)
+ });
+ let id = insert(&store, unreadable);
+
+ let outcome = service(&store, &classifier, &["acme.example"])
+ .classify_thread(id)
+ .await
+ .unwrap();
+ assert!(matches!(outcome, ClassifyOutcome::Classified(_)));
+ assert_eq!(classifier.inputs.lock().unwrap().len(), 1);
+}
+
+#[tokio::test]
+async fn new_mail_goes_to_the_classifier_even_under_an_older_rule() {
+ let store = FakeStore::default();
+ let classifier = FakeClassifier::answering(Ok(relevant()));
+ let mut stale = thread("[email protected]");
+ stale.classified = Some(StoredClassification {
+ message_id: Uuid::new_v4(),
+ ..stored_at(&stale, 0)
+ });
+ let id = insert(&store, stale);
+
+ let outcome = service(&store, &classifier, &["acme.example"])
+ .classify_thread(id)
+ .await
+ .unwrap();
+ assert!(matches!(outcome, ClassifyOutcome::Classified(_)));
+ assert_eq!(classifier.inputs.lock().unwrap().len(), 1);
+}
+
+#[tokio::test]
+async fn outside_the_signal_inbox_only_key_mail_reaches_the_classifier() {
+ let store = FakeStore::default();
+ let classifier = FakeClassifier::answering(Ok(relevant()));
+ let service = service(&store, &classifier, &["acme.example"]);
+
+ let mut noise = thread("[email protected]");
+ noise.is_signal = false;
+ let noise = insert(&store, noise);
+ assert_eq!(
+ service.classify_thread(noise).await.unwrap(),
+ ClassifyOutcome::NotSignal
+ );
+ assert!(classifier.inputs.lock().unwrap().is_empty());
+
+ let mut rsvp = thread("[email protected]");
+ rsvp.is_signal = false;
+ rsvp.messages[0].calendar_part = Some(CalendarPart::Rsvp);
+ let rsvp = insert(&store, rsvp);
+ assert_eq!(
+ service.classify_thread(rsvp).await.unwrap(),
+ ClassifyOutcome::NotSignal
+ );
+ assert!(classifier.inputs.lock().unwrap().is_empty());
+
+ // Neither is a new invite, or a change to an event that is already over.
+ let mut new_invite = thread("[email protected]");
+ new_invite.is_signal = false;
+ new_invite.messages[0].calendar_part = Some(CalendarPart::NewInvite {
+ until: Utc::now() + Duration::days(2),
+ });
+ let new_invite = insert(&store, new_invite);
+ assert_eq!(
+ service.classify_thread(new_invite).await.unwrap(),
+ ClassifyOutcome::NotSignal
+ );
+ let mut over = thread("[email protected]");
+ over.is_signal = false;
+ over.messages[0].calendar_part = Some(CalendarPart::Change {
+ until: Utc::now() - Duration::hours(1),
+ });
+ let over = insert(&store, over);
+ assert_eq!(
+ service.classify_thread(over).await.unwrap(),
+ ClassifyOutcome::NotSignal
+ );
+ assert!(classifier.inputs.lock().unwrap().is_empty());
+
+ let until = Utc::now() + Duration::days(2);
+ let mut reschedule = thread("[email protected]");
+ reschedule.is_signal = false;
+ reschedule.messages[0].calendar_part = Some(CalendarPart::Change { until });
+ let reschedule = insert(&store, reschedule);
+ let ClassifyOutcome::Classified(verdict) = service.classify_thread(reschedule).await.unwrap()
+ else {
+ panic!("expected the reschedule to be classified");
+ };
+ assert_eq!(verdict.calendar_hold_until, Some(until));
+
+ let mut contact = thread("[email protected]");
+ contact.is_signal = false;
+ contact.sent_notes = vec![SentNote {
+ recipient: "[email protected]".to_owned(),
+ at: noon() - Duration::days(7),
+ body: Some("Sending the invoice over now".to_owned()),
+ }];
+ let contact = insert(&store, contact);
+ let ClassifyOutcome::Classified(verdict) = service.classify_thread(contact).await.unwrap()
+ else {
+ panic!("expected the recent contact to be classified");
+ };
+ assert!(verdict.is_focus && verdict.pinned);
+}
+
+#[tokio::test]
+async fn a_thread_the_owner_answered_last_is_classified_outside_the_signal_inbox() {
+ let store = FakeStore::default();
+ // Reads as automated, so only the wait can bring it in.
+ let classifier = FakeClassifier::answering(Ok(vec![0.0, 0.0, 0.9, 0.0, 0.0, 0.0, 0.0, 0.0]));
+ let mut waiting = thread("[email protected]");
+ waiting.is_signal = false;
+ let mut reply = waiting.messages[0].clone();
+ reply.id = Uuid::new_v4();
+ reply.at = noon() + Duration::hours(1);
+ reply.is_sent = true;
+ reply.from_email = Some("[email protected]".to_owned());
+ reply.to = vec!["[email protected]".to_owned()];
+ reply.body = Some("Can you resend the invoice?".to_owned());
+ waiting.messages.push(reply);
+ let id = insert(&store, waiting);
+
+ let outcome = service(&store, &classifier, &["acme.example"])
+ .classify_thread(id)
+ .await
+ .unwrap();
+ let ClassifyOutcome::Classified(verdict) = outcome else {
+ panic!("expected a classification, got {outcome:?}");
+ };
+ // The list applies the age; the verdict records that it can.
+ assert!(!verdict.is_focus);
+ assert!(verdict.awaiting_reply);
+}
+
+#[tokio::test]
+async fn a_thread_of_only_sent_mail_never_reaches_the_classifier() {
+ let store = FakeStore::default();
+ let classifier = FakeClassifier::answering(Ok(relevant()));
+ let service = service(&store, &classifier, &["acme.example"]);
+
+ // As the projection stores it: never in the inbox, and so not visible.
+ let mut unanswered = thread("[email protected]");
+ unanswered.inbox_visible = false;
+ unanswered.messages[0].is_sent = true;
+ unanswered.messages[0].from_email = Some("[email protected]".to_owned());
+ unanswered.messages[0].to = vec!["[email protected]".to_owned()];
+ let hidden = insert(&store, unanswered.clone());
+ assert_eq!(
+ service.classify_thread(hidden).await.unwrap(),
+ ClassifyOutcome::NotSignal
+ );
+
+ // Back in the inbox through a reminder, it still has no mail from anyone else.
+ unanswered.thread_id = Uuid::new_v4();
+ unanswered.inbox_visible = true;
+ let returned = insert(&store, unanswered);
+ assert_eq!(
+ service.classify_thread(returned).await.unwrap(),
+ ClassifyOutcome::Missing
+ );
+ assert!(classifier.inputs.lock().unwrap().is_empty());
+ assert!(store.saved.lock().unwrap().is_empty());
+}
+
+#[tokio::test]
+async fn the_lists_apply_a_three_day_wait() {
+ let store = FakeStore::default();
+ let queries = FocusQueries::new(store.clone());
+ let owner = MacroUserIdStr::try_from_email("[email protected]").unwrap();
+
+ let before = Utc::now() - Duration::days(FOLLOW_UP_AFTER_DAYS);
+ queries.focus_thread_ids(&owner, 30).await.unwrap();
+ queries
+ .focus_for_threads(&owner, &[Uuid::new_v4()])
+ .await
+ .unwrap();
+ let after = Utc::now() - Duration::days(FOLLOW_UP_AFTER_DAYS);
+
+ let cutoffs = store.waiting_before.lock().unwrap();
+ assert_eq!(cutoffs.len(), 2);
+ assert!(
+ cutoffs
+ .iter()
+ .all(|cutoff| before <= *cutoff && *cutoff <= after)
+ );
+ assert_eq!(FOLLOW_UP_AFTER_DAYS, 3);
+ let read_at = store.read_at.lock().unwrap();
+ assert_eq!(read_at.len(), 2);
+ assert!(
+ read_at
+ .iter()
+ .all(|now| before + Duration::days(FOLLOW_UP_AFTER_DAYS) <= *now)
+ );
+}
diff --git a/crates/email/src/domain/focus/signals.rs b/crates/email/src/domain/focus/signals.rs
index cd415aba1c..d8d1adf7b8 100644
--- a/crates/email/src/domain/focus/signals.rs
+++ b/crates/email/src/domain/focus/signals.rs
@@ -2,12 +2,18 @@
use std::collections::HashSet;
+use chrono::Duration;
+
use super::{
- input::{is_dismissive_reply, new_text},
+ calendar::notice_in_message,
+ input::{is_closing_reply, is_dismissive_reply, new_text},
models::{FocusSignals, FocusThread},
- rule::is_calendar_subject,
};
+/// How far back from a thread's latest message the owner's writing to someone
+/// still makes them a recent contact.
+pub const RECENT_CONTACT_DAYS: i64 = 90;
+
/// Consumer mail providers; sharing one of these is not being teammates.
const FREEMAIL_DOMAINS: [&str; 11] = [
"gmail.com",
@@ -57,17 +63,32 @@ const AUTOMATED_LOCAL_PARTS: [&str; 30] = [
"mailer-daemon",
];
+/// Local parts of addresses nobody answers from. Narrower than
+/// [`AUTOMATED_LOCAL_PARTS`]: a person does reply from `support@` or `hello@`.
+const UNANSWERED_LOCAL_PARTS: [&str; 8] = [
+ "noreply",
+ "no-reply",
+ "donotreply",
+ "do-not-reply",
+ "donot-reply",
+ "do-notreply",
+ "bounce",
+ "mailer-daemon",
+];
+
fn domain(address: &str) -> &str {
address.rsplit_once('@').map_or("", |(_, domain)| domain)
}
+fn local_part_ends_with(address: &str, parts: &[&str]) -> bool {
+ let local = address.split_once('@').map_or(address, |(local, _)| local);
+ parts.iter().any(|part| local.ends_with(part))
+}
+
/// An address that sends automated mail; matches the end of the local part,
/// so `team@` and `product-updates@` both count.
fn is_automated_sender(address: &str) -> bool {
- let local = address.split_once('@').map_or(address, |(local, _)| local);
- AUTOMATED_LOCAL_PARTS
- .iter()
- .any(|part| local.ends_with(part))
+ local_part_ends_with(address, &AUTOMATED_LOCAL_PARTS)
}
fn is_substantive(body: Option<&str>) -> bool {
@@ -78,6 +99,7 @@ fn is_substantive(body: Option<&str>) -> bool {
/// owner's sent mail.
pub(super) fn signals(thread: &FocusThread) -> FocusSignals {
let owner = thread.owner_email.to_lowercase();
+ let latest = thread.messages.last();
let incoming = thread
.messages
.iter()
@@ -131,19 +153,46 @@ pub(super) fn signals(thread: &FocusThread) -> FocusSignals {
let contact = !brushed_off && (replied_for_real || written_to || (teammate && !bulk));
+ // Measured from the thread's latest message, so re-deciding a thread later
+ // gives the same answer.
+ let recent_since = latest.map(|message| message.at - Duration::days(RECENT_CONTACT_DAYS));
+ let is_recent = |at| recent_since.is_some_and(|since| at >= since);
+ let replied_recently = replies
+ .iter()
+ .any(|message| is_recent(message.at) && is_substantive(message.body.as_deref()));
+ let wrote_recently = notes_to_senders
+ .iter()
+ .any(|note| is_recent(note.at) && is_substantive(note.body.as_deref()));
+ let recent_contact = !brushed_off && !bulk && (replied_recently || wrote_recently);
+
+ // Only a notice nobody has written after: a person replying to one is a
+ // conversation, and the owner answering one has seen it.
+ let calendar = latest
+ .filter(|message| !message.is_sent)
+ .and_then(notice_in_message);
+
+ // Only threads with mail from the other side: the owner's own unanswered
+ // sends are not inbox threads, and nothing here can judge them.
+ let awaiting_reply = !incoming.is_empty()
+ && latest.is_some_and(|message| {
+ message.is_sent
+ && notice_in_message(message).is_none()
+ && !is_closing_reply(&new_text(message.body.as_deref().unwrap_or_default()))
+ && message.to.iter().any(|recipient| {
+ let recipient = recipient.to_lowercase();
+ recipient != owner && !local_part_ends_with(&recipient, &UNANSWERED_LOCAL_PARTS)
+ })
+ });
+
FocusSignals {
contact,
+ recent_contact,
+ brushed_off,
replied,
teammate,
- latest_from_owner: thread
- .messages
- .last()
- .is_some_and(|message| message.is_sent),
- calendar_subject: thread
- .messages
- .first()
- .and_then(|message| message.subject.as_deref())
- .is_some_and(is_calendar_subject),
+ latest_from_owner: latest.is_some_and(|message| message.is_sent),
+ calendar,
+ awaiting_reply,
}
}
diff --git a/crates/email/src/domain/focus/signals/test.rs b/crates/email/src/domain/focus/signals/test.rs
index c827fd7750..4f58475fd0 100644
--- a/crates/email/src/domain/focus/signals/test.rs
+++ b/crates/email/src/domain/focus/signals/test.rs
@@ -1,14 +1,18 @@
-use chrono::{Duration, TimeZone, Utc};
+use chrono::{DateTime, Duration, TimeZone, Utc};
use macro_user_id::user_id::MacroUserIdStr;
use uuid::Uuid;
use super::*;
-use crate::domain::focus::models::{FocusMessage, SentNote};
+use crate::domain::focus::models::{CalendarNotice, CalendarPart, FocusMessage, SentNote};
+
+fn start() -> DateTime<Utc> {
+ Utc.with_ymd_and_hms(2026, 10, 1, 9, 0, 0).unwrap()
+}
fn message(from: &str, minutes: i64, is_sent: bool, body: &str) -> FocusMessage {
FocusMessage {
id: Uuid::new_v4(),
- at: Utc.with_ymd_and_hms(2026, 10, 1, 9, 0, 0).unwrap() + Duration::minutes(minutes),
+ at: start() + Duration::minutes(minutes),
is_sent,
from_email: Some(from.to_owned()),
from_name: None,
@@ -17,11 +21,19 @@ fn message(from: &str, minutes: i64, is_sent: bool, body: &str) -> FocusMessage
subject: Some("Hello".to_owned()),
has_attachments: false,
bulk: false,
+ calendar_part: None,
body: Some(body.to_owned()),
snippet: None,
}
}
+fn reply_to(to: &str, minutes: i64, body: &str) -> FocusMessage {
+ FocusMessage {
+ to: vec![to.to_owned()],
+ ..message("[email protected]", minutes, true, body)
+ }
+}
+
fn thread(messages: Vec<FocusMessage>, sent_notes: Vec<SentNote>) -> FocusThread {
FocusThread {
thread_id: Uuid::new_v4(),
@@ -30,19 +42,39 @@ fn thread(messages: Vec<FocusMessage>, sent_notes: Vec<SentNote>) -> FocusThread
owner_email: "[email protected]".to_owned(),
is_signal: true,
inbox_visible: true,
- classified_message_id: None,
+ classified: None,
messages,
sent_notes,
}
}
-fn note(recipient: &str, body: &str) -> SentNote {
+fn note(recipient: &str, days_ago: i64, body: &str) -> SentNote {
SentNote {
recipient: recipient.to_owned(),
+ at: start() - Duration::days(days_ago),
body: Some(body.to_owned()),
}
}
+fn with_subject(mut message: FocusMessage, subject: &str) -> FocusMessage {
+ message.subject = Some(subject.to_owned());
+ message
+}
+
+fn with_part(mut message: FocusMessage, part: CalendarPart) -> FocusMessage {
+ message.calendar_part = Some(part);
+ message
+}
+
+fn saved_change(message: FocusMessage) -> FocusMessage {
+ let until = message.at + Duration::days(2);
+ with_part(message, CalendarPart::Change { until })
+}
+
+fn calendar(messages: Vec<FocusMessage>) -> Option<CalendarNotice> {
+ signals(&thread(messages, Vec::new())).calendar
+}
+
#[test]
fn a_stranger_is_not_a_contact() {
let signals = signals(&thread(
@@ -61,24 +93,79 @@ fn a_stranger_is_not_a_contact() {
fn someone_the_owner_wrote_to_is_a_contact() {
let signals = signals(&thread(
vec![message("[email protected]", 0, false, "Your invoice")],
- vec![note("[email protected]", "Yes, please send it over!")],
+ vec![note("[email protected]", 1, "Yes, please send it over!")],
));
assert!(signals.contact);
assert!(!signals.replied);
}
+#[test]
+fn a_recent_contact_is_someone_the_owner_wrote_to_within_the_window() {
+ let incoming = message("[email protected]", 0, false, "Your invoice");
+ let inside = signals(&thread(
+ vec![incoming.clone()],
+ vec![note("[email protected]", 89, "Yes, please send it over!")],
+ ));
+ assert!(inside.contact && inside.recent_contact);
+
+ // Older mail still makes a contact, but not a recent one.
+ let outside = signals(&thread(
+ vec![incoming.clone()],
+ vec![note("[email protected]", 91, "Yes, please send it over!")],
+ ));
+ assert!(outside.contact && !outside.recent_contact);
+
+ let brush_off = signals(&thread(
+ vec![incoming],
+ vec![note("[email protected]", 1, "No thanks")],
+ ));
+ assert!(brush_off.brushed_off && !brush_off.recent_contact);
+}
+
+#[test]
+fn bulk_and_automated_senders_are_never_recent_contacts() {
+ let mut newsletter = message("[email protected]", 0, false, "This week");
+ newsletter.bulk = true;
+ let newsletter = signals(&thread(
+ vec![newsletter],
+ vec![note("[email protected]", 1, "Love this, can we talk?")],
+ ));
+ assert!(newsletter.contact && !newsletter.recent_contact);
+
+ let automated = signals(&thread(
+ vec![message("[email protected]", 0, false, "Build failed")],
+ vec![note("[email protected]", 1, "Who owns this alert?")],
+ ));
+ assert!(automated.contact && !automated.recent_contact);
+}
+
+#[test]
+fn a_real_reply_in_the_thread_is_a_recent_contact() {
+ let signals = signals(&thread(
+ vec![
+ message("[email protected]", 0, false, "Application"),
+ reply_to("[email protected]", 5, "Let's chat tomorrow"),
+ message("[email protected]", 10, false, "Yes!"),
+ ],
+ Vec::new(),
+ ));
+ assert!(signals.recent_contact);
+}
+
#[test]
fn a_brushed_off_sender_is_not_a_contact() {
let signals = signals(&thread(
vec![
message("[email protected]", 0, false, "Following up"),
- message("[email protected]", 5, true, "I'm not interested"),
+ reply_to("[email protected]", 5, "I'm not interested"),
],
- vec![note("[email protected]", "No thanks")],
+ vec![note("[email protected]", 1, "No thanks")],
));
assert!(signals.replied);
+ assert!(signals.brushed_off);
assert!(!signals.contact);
assert!(signals.latest_from_owner);
+ assert!(!signals.awaiting_reply);
}
#[test]
@@ -86,10 +173,9 @@ fn a_real_reply_makes_a_contact() {
let signals = signals(&thread(
vec![
message("[email protected]", 0, false, "Application"),
- message(
+ reply_to(
5,
- true,
"Let's chat, are you free tomorrow?",
),
message("[email protected]", 10, false, "Yes!"),
@@ -99,6 +185,101 @@ fn a_real_reply_makes_a_contact() {
assert!(signals.contact);
assert!(signals.replied);
assert!(!signals.latest_from_owner);
+ assert!(!signals.awaiting_reply);
+}
+
+#[test]
+fn the_owner_answering_last_awaits_a_reply() {
+ let incoming = message("[email protected]", 0, false, "Application");
+ let waiting = signals(&thread(
+ vec![
+ incoming.clone(),
+ reply_to("[email protected]", 5, "Are you free tomorrow?"),
+ ],
+ Vec::new(),
+ ));
+ assert!(waiting.latest_from_owner && waiting.awaiting_reply);
+
+ // A person can answer from a shared address.
+ let shared_address = signals(&thread(
+ vec![
+ message("[email protected]", 0, false, "Ticket opened"),
+ reply_to("[email protected]", 5, "Any update on this?"),
+ ],
+ Vec::new(),
+ ));
+ assert!(shared_address.awaiting_reply);
+
+ for (reason, reply) in [
+ (
+ "an acknowledgement",
+ reply_to("[email protected]", 5, "Thanks!"),
+ ),
+ (
+ "a brush-off",
+ reply_to("[email protected]", 5, "No thanks"),
+ ),
+ (
+ "an address nobody reads",
+ reply_to("[email protected]", 5, "Hello?"),
+ ),
+ (
+ "no one else addressed",
+ reply_to("[email protected]", 5, "Note to self"),
+ ),
+ ] {
+ let signals = signals(&thread(vec![incoming.clone(), reply], Vec::new()));
+ assert!(signals.latest_from_owner, "{reason}");
+ assert!(!signals.awaiting_reply, "{reason}");
+ }
+}
+
+#[test]
+fn only_mail_sent_by_the_owner_awaits_nothing() {
+ let signals = signals(&thread(
+ vec![reply_to(
+ 0,
+ "Are you free tomorrow?",
+ )],
+ Vec::new(),
+ ));
+ assert!(signals.latest_from_owner);
+ assert!(!signals.awaiting_reply);
+ assert!(!signals.is_key_mail(start()));
+}
+
+#[test]
+fn a_support_thread_opened_by_a_tool_still_awaits_a_reply() {
+ let mut opened_by_a_tool = message("[email protected]", 0, false, "Ticket opened");
+ opened_by_a_tool.bulk = true;
+ let signals = signals(&thread(
+ vec![
+ opened_by_a_tool,
+ message("[email protected]", 5, false, "Looking into it"),
+ reply_to("[email protected]", 10, "Any update on this?"),
+ ],
+ Vec::new(),
+ ));
+ assert!(signals.latest_from_owner && signals.awaiting_reply);
+}
+
+#[test]
+fn the_owners_own_calendar_notice_awaits_nothing() {
+ let signals = signals(&thread(
+ vec![
+ message("[email protected]", 0, false, "Application"),
+ saved_change(reply_to(
+ 5,
+ "Joining details attached",
+ )),
+ ],
+ Vec::new(),
+ ));
+ assert!(signals.latest_from_owner && !signals.awaiting_reply);
+ // Nor does it make the thread a notice for the owner to read.
+ assert_eq!(signals.calendar, None);
}
#[test]
@@ -109,6 +290,7 @@ fn teammates_count_unless_it_is_bulk_mail() {
));
assert!(teammate.teammate);
assert!(teammate.contact);
+ assert!(!teammate.recent_contact);
let mut bulk = message("[email protected]", 0, false, "Weekly digest");
bulk.bulk = true;
@@ -140,8 +322,91 @@ fn freemail_owners_have_no_teammates() {
}
#[test]
-fn calendar_subject_comes_from_the_first_message() {
- let mut invite = message("[email protected]", 0, false, "");
- invite.subject = Some("Invitation: Standup @ Mon".to_owned());
- assert!(signals(&thread(vec![invite], Vec::new())).calendar_subject);
+fn a_notice_counts_only_as_the_threads_latest_message() {
+ let first = message("[email protected]", 0, false, "Can we meet?");
+ let reschedule = with_subject(
+ message("[email protected]", 5, false, ""),
+ "Rescheduled: Demo @ Thu",
+ );
+ let later = message("[email protected]", 10, false, "Does Friday work instead?");
+ assert!(matches!(
+ calendar(vec![first.clone(), reschedule.clone()]),
+ Some(CalendarNotice::Change { .. })
+ ));
+ // Someone wrote after it: a conversation.
+ assert_eq!(calendar(vec![first, reschedule.clone(), later]), None);
+
+ // The owner answered it: they have seen it, and may be waiting on an answer.
+ let answered = signals(&thread(
+ vec![
+ reschedule.clone(),
+ reply_to("[email protected]", 10, "Does Friday work instead?"),
+ ],
+ Vec::new(),
+ ));
+ assert_eq!(answered.calendar, None);
+ assert!(answered.awaiting_reply);
+
+ // The owner's own RSVP settles it the same way, and awaits nothing.
+ let rsvp = with_part(reply_to("[email protected]", 10, ""), CalendarPart::Rsvp);
+ let accepted = signals(&thread(vec![reschedule, rsvp], Vec::new()));
+ assert_eq!(accepted.calendar, None);
+ assert!(!accepted.awaiting_reply);
+}
+
+#[test]
+fn a_new_invite_or_rsvp_behind_a_later_message_is_a_conversation() {
+ let invite = with_subject(
+ message("[email protected]", 0, false, ""),
+ "Invitation: Demo @ Thu",
+ );
+ let rsvp = with_subject(
+ message("[email protected]", 0, false, ""),
+ "Accepted: Demo @ Thu",
+ );
+ let person = with_subject(
+ message("[email protected]", 5, false, "Can we move it?"),
+ "Re: Invitation: Demo @ Thu",
+ );
+ assert_eq!(
+ calendar(vec![invite.clone()]),
+ Some(CalendarNotice::NewInvite)
+ );
+ assert_eq!(calendar(vec![invite.clone(), person.clone()]), None);
+ assert_eq!(calendar(vec![rsvp.clone(), person]), None);
+
+ // The latest message settles it, whatever came before.
+ assert_eq!(calendar(vec![invite, rsvp]), Some(CalendarNotice::Rsvp));
+}
+
+#[test]
+fn key_mail_is_changes_to_events_ahead_recent_contacts_and_waiting_threads() {
+ let now = start();
+ let change_until = |until| FocusSignals {
+ calendar: Some(CalendarNotice::Change { until }),
+ ..FocusSignals::default()
+ };
+ let recent = FocusSignals {
+ recent_contact: true,
+ ..FocusSignals::default()
+ };
+ let waiting = FocusSignals {
+ awaiting_reply: true,
+ ..FocusSignals::default()
+ };
+ let notice = |notice| FocusSignals {
+ calendar: Some(notice),
+ ..FocusSignals::default()
+ };
+ let contact = FocusSignals {
+ contact: true,
+ ..FocusSignals::default()
+ };
+ assert!(change_until(now + Duration::minutes(1)).is_key_mail(now));
+ assert!(recent.is_key_mail(now) && waiting.is_key_mail(now));
+ assert!(!change_until(now).is_key_mail(now));
+ assert!(!change_until(now - Duration::days(1)).is_key_mail(now));
+ assert!(!notice(CalendarNotice::NewInvite).is_key_mail(now));
+ assert!(!notice(CalendarNotice::Rsvp).is_key_mail(now));
+ assert!(!contact.is_key_mail(now));
}
diff --git a/crates/email/src/outbound/focus_pg.rs b/crates/email/src/outbound/focus_pg.rs
index 41ca279ed7..9870e2b7e3 100644
--- a/crates/email/src/outbound/focus_pg.rs
+++ b/crates/email/src/outbound/focus_pg.rs
@@ -2,15 +2,19 @@
use std::{collections::HashMap, str::FromStr};
-use chrono::{DateTime, Utc};
+use chrono::{DateTime, Duration, Utc};
use macro_user_id::user_id::MacroUserIdStr;
use rootcause::Report;
use sqlx::PgPool;
use uuid::Uuid;
-use crate::domain::focus::{
- FocusCategory, FocusInbox, FocusMail, FocusMessage, FocusRecord, FocusStore, SentNote,
- StaleThread, ThreadFocus,
+use crate::{
+ domain::focus::{
+ CalendarPart, FOCUS_RULE_VERSION, FocusAnswers, FocusCategory, FocusInbox, FocusMail,
+ FocusMessage, FocusRecord, FocusStore, RECENT_CONTACT_DAYS, SentNote, StaleThread,
+ StoredClassification, ThreadFocus, change_subject_pattern,
+ },
+ outbound::invitation_pg,
};
/// Most recent messages read per thread.
@@ -64,7 +68,16 @@ impl FocusPgRepository {
)
OR (LOWER(h->>'name') = 'precedence'
AND LOWER(h->>'value') IN ('bulk', 'list', 'junk'))
- ) AS "bulk!"
+ ) AS "bulk!",
+ EXISTS (
+ SELECT 1 FROM email_message_calendar_invites i
+ WHERE i.message_id = m.id
+ ) OR EXISTS (
+ SELECT 1 FROM email_attachments a
+ WHERE a.message_id = m.id
+ AND (a.filename ILIKE '%.ics'
+ OR a.mime_type IN ('text/calendar', 'application/ics'))
+ ) AS "has_calendar!"
FROM email_messages m
LEFT JOIN email_contacts c ON c.id = m.from_contact_id
WHERE m.thread_id = $1
@@ -83,21 +96,41 @@ impl FocusPgRepository {
)
.fetch_all(&self.0)
.await?;
+ let with_calendar = rows
+ .iter()
+ .filter(|row| row.has_calendar)
+ .map(|row| row.id)
+ .collect::<Vec<_>>();
+ let invites = if with_calendar.is_empty() {
+ HashMap::new()
+ } else {
+ invitation_pg::load(&self.0, &with_calendar).await?
+ };
let mut messages = rows
.into_iter()
- .map(|row| FocusMessage {
- id: row.id,
- at: row.at,
- is_sent: row.is_sent,
- from_email: row.from_email,
- from_name: row.from_name,
- to: row.to_emails,
- cc: row.cc_emails,
- subject: row.subject,
- has_attachments: row.has_attachments,
- bulk: row.bulk,
- body: row.body,
- snippet: row.snippet,
+ .map(|row| {
+ // A calendar file or snapshot nothing could read still is one.
+ let calendar_part = row.has_calendar.then(|| {
+ invites
+ .get(&row.id)
+ .and_then(|invites| CalendarPart::from_saved(invites, row.at))
+ .unwrap_or(CalendarPart::Unparsed)
+ });
+ FocusMessage {
+ id: row.id,
+ at: row.at,
+ is_sent: row.is_sent,
+ from_email: row.from_email,
+ from_name: row.from_name,
+ to: row.to_emails,
+ cc: row.cc_emails,
+ subject: row.subject,
+ has_attachments: row.has_attachments,
+ bulk: row.bulk,
+ calendar_part,
+ body: row.body,
+ snippet: row.snippet,
+ }
})
.collect::<Vec<_>>();
messages.reverse();
@@ -116,7 +149,9 @@ impl FocusPgRepository {
// close correspondent can have thousands of messages.
let rows = sqlx::query!(
r#"
- SELECT newest.recipient AS "recipient!", LEFT(m.body_text, $3) AS body
+ SELECT newest.recipient AS "recipient!",
+ COALESCE(newest.internal_date_ts, m.created_at) AS "at!",
+ LEFT(m.body_text, $3) AS body
FROM (
SELECT LOWER(rc.email_address) AS recipient, m.id, m.internal_date_ts
FROM email_contacts rc
@@ -143,6 +178,7 @@ impl FocusPgRepository {
.into_iter()
.map(|row| SentNote {
recipient: row.recipient,
+ at: row.at,
body: row.body,
})
.collect())
@@ -155,7 +191,10 @@ impl FocusStore for FocusPgRepository {
let Some(row) = sqlx::query!(
r#"
SELECT t.id, t.link_id, t.is_signal, t.inbox_visible, el.macro_id, el.email_address,
- f.classified_message_id AS "classified_message_id?"
+ f.classified_message_id AS "classified_message_id?",
+ f.rule_version AS "rule_version?",
+ f.scores AS "scores?",
+ f.model AS "model?"
FROM email_threads t
JOIN email_links el ON el.id = t.link_id
LEFT JOIN email_thread_focus f ON f.thread_id = t.id
@@ -168,6 +207,28 @@ impl FocusStore for FocusPgRepository {
else {
return Ok(None);
};
+ let classified = match (
+ row.classified_message_id,
+ row.rule_version,
+ row.scores,
+ row.model,
+ ) {
+ (Some(message_id), Some(rule_version), Some(scores), Some(model)) => {
+ // Answers that no longer decode just go back to the classifier.
+ let answers = serde_json::from_value::<FocusAnswers>(scores)
+ .inspect_err(|_| {
+ tracing::warn!(thread_id = %row.id, "undecodable focus answers stored");
+ })
+ .ok();
+ Some(StoredClassification {
+ message_id,
+ rule_version,
+ answers,
+ model,
+ })
+ }
+ _ => None,
+ };
Ok(Some(FocusInbox {
thread_id: row.id,
link_id: row.link_id,
@@ -175,7 +236,7 @@ impl FocusStore for FocusPgRepository {
owner_email: row.email_address.to_lowercase(),
is_signal: row.is_signal,
inbox_visible: row.inbox_visible,
- classified_message_id: row.classified_message_id,
+ classified,
}))
}
@@ -206,9 +267,10 @@ impl FocusStore for FocusPgRepository {
r#"
INSERT INTO email_thread_focus (
thread_id, link_id, classified_message_id, classified_message_ts, is_focus,
- category, importance, needs_reply, needs_follow_up, scores, model, classified_at
+ category, importance, needs_reply, needs_follow_up, scores, model, classified_at,
+ rule_version, pinned, awaiting_reply, calendar_hold_until
)
- VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)
+ VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16)
ON CONFLICT (thread_id) DO UPDATE SET
link_id = EXCLUDED.link_id,
classified_message_id = EXCLUDED.classified_message_id,
@@ -220,7 +282,11 @@ impl FocusStore for FocusPgRepository {
needs_follow_up = EXCLUDED.needs_follow_up,
scores = EXCLUDED.scores,
model = EXCLUDED.model,
- classified_at = EXCLUDED.classified_at
+ classified_at = EXCLUDED.classified_at,
+ rule_version = EXCLUDED.rule_version,
+ pinned = EXCLUDED.pinned,
+ awaiting_reply = EXCLUDED.awaiting_reply,
+ calendar_hold_until = EXCLUDED.calendar_hold_until
WHERE email_thread_focus.classified_message_ts <= EXCLUDED.classified_message_ts
"#,
record.thread_id,
@@ -235,6 +301,10 @@ impl FocusStore for FocusPgRepository {
scores,
record.model,
record.classified_at,
+ FOCUS_RULE_VERSION,
+ record.verdict.pinned,
+ record.verdict.awaiting_reply,
+ record.verdict.calendar_hold_until,
)
.execute(&self.0)
.await?;
@@ -246,6 +316,7 @@ impl FocusStore for FocusPgRepository {
&self,
since: DateTime<Utc>,
domains: &[String],
+ after: Option<StaleThread>,
limit: i64,
) -> Result<Vec<StaleThread>, Report> {
let rows = sqlx::query!(
@@ -258,20 +329,74 @@ impl FocusStore for FocusPgRepository {
LEFT JOIN email_thread_focus f ON f.thread_id = t.id
WHERE LOWER(SPLIT_PART(el.email_address, '@', 2)) = ANY($2)
AND t.inbox_visible
- AND t.is_signal
AND t.latest_inbound_message_ts IS NOT NULL
- AND t.latest_inbound_message_ts >= $1
+ AND GREATEST(t.latest_inbound_message_ts, t.latest_outbound_message_ts) >= $1
+ AND (
+ t.is_signal
+ OR t.latest_outbound_message_ts > t.latest_inbound_message_ts
+ OR f.rule_version < $4
+ -- A calendar change. The service judges whether it is the
+ -- latest message and its event isn't over; a new invite or an
+ -- RSVP never gets this far.
+ OR EXISTS (
+ SELECT 1
+ FROM email_messages notice
+ WHERE notice.thread_id = t.id
+ AND NOT notice.is_sent
+ AND NOT notice.is_draft
+ AND (
+ LOWER(notice.subject) ~ $6
+ OR EXISTS (
+ SELECT 1 FROM email_message_calendar_invites i
+ WHERE i.message_id = notice.id
+ AND (
+ i.snapshot->>'method' IN ('cancel', 'counter')
+ OR (
+ i.snapshot->>'method' = 'request'
+ AND i.snapshot->'sequence' > '0'::jsonb
+ )
+ )
+ )
+ )
+ )
+ -- A sender the owner has written to lately. The service judges
+ -- whether it really is a recent contact; mail no event covers,
+ -- such as an initial sync, only arrives this way.
+ OR EXISTS (
+ SELECT 1
+ FROM email_messages inbound
+ JOIN email_message_recipients r ON r.contact_id = inbound.from_contact_id
+ JOIN email_messages sent ON sent.id = r.message_id
+ WHERE inbound.thread_id = t.id
+ AND NOT inbound.is_sent
+ AND NOT inbound.is_draft
+ AND sent.is_sent
+ AND NOT sent.is_draft
+ AND sent.internal_date_ts >= $5
+ )
+ )
AND (
f.thread_id IS NULL
+ OR f.rule_version < $4
OR f.classified_message_ts
< GREATEST(t.latest_inbound_message_ts, t.latest_outbound_message_ts)
)
- ORDER BY t.latest_inbound_message_ts DESC
+ AND (
+ $7::timestamptz IS NULL
+ OR (GREATEST(t.latest_inbound_message_ts, t.latest_outbound_message_ts), t.id)
+ < ($7, $8::uuid)
+ )
+ ORDER BY GREATEST(t.latest_inbound_message_ts, t.latest_outbound_message_ts) DESC, t.id DESC
LIMIT $3
"#,
since,
domains,
limit,
+ FOCUS_RULE_VERSION,
+ since - Duration::days(RECENT_CONTACT_DAYS),
+ change_subject_pattern(),
+ after.map(|after| after.latest_at),
+ after.map(|after| after.thread_id),
)
.fetch_all(&self.0)
.await?;
@@ -284,60 +409,142 @@ impl FocusStore for FocusPgRepository {
.collect())
}
- #[tracing::instrument(skip(self), err)]
+ #[tracing::instrument(skip(self), fields(threads = tracing::field::Empty), err)]
async fn focus_thread_ids(
&self,
owner: &MacroUserIdStr<'static>,
since: DateTime<Utc>,
+ waiting_before: DateTime<Utc>,
+ now: DateTime<Utc>,
limit: i64,
) -> Result<Vec<Uuid>, Report> {
- // GREATEST floors importance at 1 below: log(0) is an error.
- Ok(sqlx::query_scalar!(
+ // The waiting and calendar hold tests here restate `focus_for_threads`;
+ // change both together.
+ let thread_ids = sqlx::query_scalar!(
r#"
SELECT f.thread_id
FROM email_thread_focus f
- -- Joining on the inbox too lets the planner walk each inbox's recent
- -- signal threads (idx_email_threads_signal_view) instead of every
- -- Focus row the inbox ever had.
JOIN email_threads t ON t.id = f.thread_id AND t.link_id = f.link_id
+ -- Who wrote last comes from the mail itself: editing a draft moves
+ -- the thread's own timestamps. An indexed per-thread top-one probe.
+ CROSS JOIN LATERAL (
+ SELECT COALESCE(m.internal_date_ts, m.created_at) AS at, m.is_sent
+ FROM email_messages m
+ WHERE m.thread_id = t.id
+ AND NOT m.is_draft
+ AND EXISTS (
+ SELECT 1 FROM email_message_mailbox_facts facts
+ WHERE facts.id = m.id AND facts.is_present AND NOT facts.in_trash
+ )
+ ORDER BY COALESCE(m.internal_date_ts, m.created_at) DESC, m.id DESC
+ LIMIT 1
+ ) latest
WHERE f.link_id IN (
-- The owner's inboxes, as Soup lists them: their own and linked ones.
SELECT el.id FROM email_links el WHERE el.macro_id = $1
UNION
SELECT mul.link_id FROM macro_user_links mul WHERE mul.primary_macro_id = $1
)
- AND f.is_focus
+ -- Every way in, indexed by email_thread_focus_list_idx and
+ -- email_thread_focus_read_time_idx.
+ AND (f.is_focus OR f.awaiting_reply OR f.calendar_hold_until IS NOT NULL)
AND t.inbox_visible
- AND t.is_signal
- AND t.latest_inbound_message_ts >= $2
- -- Priority: importance, halved for every 2 days (172800 s) since the
- -- latest inbound mail. As a sum of logs the key doesn't depend on the
- -- current time, so the order only changes when new mail arrives.
- ORDER BY log(2.0, GREATEST(f.importance, 1))
- + extract(epoch FROM t.latest_inbound_message_ts) / 172800 DESC,
- t.latest_inbound_message_ts DESC,
- f.thread_id
- LIMIT $3
+ -- Never earlier than the latest mail, so this spares most probes.
+ AND GREATEST(t.latest_inbound_message_ts, t.latest_outbound_message_ts) >= $2
+ AND latest.at >= $2
+ AND (
+ (f.is_focus AND (t.is_signal OR f.pinned))
+ OR (
+ -- Nothing reclassifies a thread as it ages, so the wait is
+ -- measured here, and a newer reply ends it before the worker
+ -- gets to the thread.
+ f.awaiting_reply
+ AND latest.is_sent
+ AND latest.at <= $3
+ )
+ -- A calendar change holds a thread until its event is over, and
+ -- a newer message ends that before the worker gets to the thread.
+ OR (f.calendar_hold_until > $4 AND latest.at <= f.classified_message_ts)
+ )
+ -- Newest first, so the limit keeps the newest threads.
+ ORDER BY latest.at DESC, f.thread_id
+ LIMIT $5
"#,
owner.as_ref(),
since,
+ waiting_before,
+ now,
limit,
)
.fetch_all(&self.0)
- .await?)
+ .await?;
+ tracing::Span::current().record("threads", thread_ids.len());
+ Ok(thread_ids)
}
- #[tracing::instrument(skip(self, thread_ids), fields(threads = thread_ids.len()), err)]
+ #[tracing::instrument(
+ skip(self, thread_ids),
+ fields(
+ threads = thread_ids.len(),
+ waiting = tracing::field::Empty,
+ calendar_held = tracing::field::Empty,
+ ),
+ err
+ )]
async fn focus_for_threads(
&self,
owner: &MacroUserIdStr<'static>,
thread_ids: &[Uuid],
+ waiting_before: DateTime<Utc>,
+ now: DateTime<Utc>,
) -> Result<HashMap<Uuid, ThreadFocus>, Report> {
+ // The labels are settled here rather than trusted from the row: who
+ // wrote last can move before the worker reaches the thread, rows from an
+ // older rule can carry a follow-up on someone else's message, which reads
+ // as a reply needed, and a held thread's event can end.
let rows = sqlx::query!(
r#"
- SELECT f.thread_id, f.is_focus, f.category, f.importance, f.needs_reply,
- f.needs_follow_up, f.classified_at
+ SELECT f.thread_id, f.category, f.importance, f.classified_at,
+ now_state.earned OR now_state.waiting OR now_state.calendar_held
+ AS "is_focus!",
+ (now_state.earned OR now_state.calendar_held)
+ AND (f.needs_reply OR f.needs_follow_up)
+ AND NOT now_state.owner_latest
+ AS "needs_reply!",
+ ((now_state.earned OR now_state.calendar_held)
+ AND f.needs_follow_up
+ AND now_state.owner_latest)
+ OR now_state.waiting
+ AS "needs_follow_up!",
+ now_state.waiting AS "waiting!",
+ now_state.calendar_held AS "calendar_held!"
FROM email_thread_focus f
+ JOIN email_threads t ON t.id = f.thread_id AND t.link_id = f.link_id
+ LEFT JOIN LATERAL (
+ SELECT COALESCE(m.internal_date_ts, m.created_at) AS at, m.is_sent
+ FROM email_messages m
+ WHERE m.thread_id = t.id
+ AND NOT m.is_draft
+ AND EXISTS (
+ SELECT 1 FROM email_message_mailbox_facts facts
+ WHERE facts.id = m.id AND facts.is_present AND NOT facts.in_trash
+ )
+ ORDER BY COALESCE(m.internal_date_ts, m.created_at) DESC, m.id DESC
+ LIMIT 1
+ ) latest ON TRUE
+ CROSS JOIN LATERAL (
+ SELECT COALESCE(latest.is_sent, false) AS owner_latest,
+ f.is_focus AND (t.is_signal OR f.pinned) AS earned,
+ f.awaiting_reply
+ AND t.inbox_visible
+ AND COALESCE(latest.is_sent AND latest.at <= $3, false) AS waiting,
+ t.inbox_visible
+ AND COALESCE(
+ f.calendar_hold_until > $4
+ AND latest.at <= f.classified_message_ts,
+ false
+ ) AS calendar_held
+ ) now_state
WHERE f.thread_id = ANY($1)
AND f.link_id IN (
SELECT el.id FROM email_links el WHERE el.macro_id = $2
@@ -347,17 +554,30 @@ impl FocusStore for FocusPgRepository {
"#,
thread_ids,
owner.as_ref(),
+ waiting_before,
+ now,
)
.fetch_all(&self.0)
.await?;
+ let span = tracing::Span::current();
+ span.record("waiting", rows.iter().filter(|row| row.waiting).count());
+ span.record(
+ "calendar_held",
+ rows.iter().filter(|row| row.calendar_held).count(),
+ );
Ok(rows
.into_iter()
.filter_map(|row| {
- let category = FocusCategory::from_str(&row.category)
- .inspect_err(|_| {
- tracing::warn!(thread_id = %row.thread_id, "unknown focus category stored");
- })
- .ok()?;
+ // Calendar in Focus only ever means a change still ahead of its event.
+ let category = if row.calendar_held {
+ FocusCategory::Calendar
+ } else {
+ FocusCategory::from_str(&row.category)
+ .inspect_err(|_| {
+ tracing::warn!(thread_id = %row.thread_id, "unknown focus category stored");
+ })
+ .ok()?
+ };
Some((
row.thread_id,
ThreadFocus {
diff --git a/crates/email/src/outbound/focus_pg/test.rs b/crates/email/src/outbound/focus_pg/test.rs
index 3a919c31c4..f53a9001b0 100644
--- a/crates/email/src/outbound/focus_pg/test.rs
+++ b/crates/email/src/outbound/focus_pg/test.rs
@@ -1,9 +1,16 @@
use chrono::Duration;
use macro_db_migrator::MACRO_DB_MIGRATIONS;
+use serde_json::Value;
use sqlx::{Pool, Postgres};
use super::*;
-use crate::domain::focus::{FocusAnswers, FocusQuestion, FocusThread, FocusVerdict};
+use crate::domain::{
+ focus::{
+ ClassifyOutcome, FocusAnswers, FocusClassifier, FocusClassifierError, FocusQueries,
+ FocusQuestion, FocusService, FocusThread, FocusVerdict, ProfileSource,
+ },
+ models::calendar_invitation::{CalendarInvitation, InvitationDateTime, InvitationMethod},
+};
const OWNER_LINK: Uuid = uuid::uuid!("00000000-0000-0000-0000-000000000f01");
const OTHER_LINK: Uuid = uuid::uuid!("00000000-0000-0000-0000-000000000f02");
@@ -13,6 +20,133 @@ const ARCHIVED: Uuid = uuid::uuid!("00000000-0000-0000-0000-00000000f202");
const BULK: Uuid = uuid::uuid!("00000000-0000-0000-0000-00000000f204");
const FIRST_MESSAGE: Uuid = uuid::uuid!("00000000-0000-0000-0000-00000000f501");
const LATEST_MESSAGE: Uuid = uuid::uuid!("00000000-0000-0000-0000-00000000f503");
+// Threads in the email_focus_waiting fixture.
+const REPLIED_LAST: Uuid = uuid::uuid!("00000000-0000-0000-0000-00000000f301");
+const REPLIED_YESTERDAY: Uuid = uuid::uuid!("00000000-0000-0000-0000-00000000f302");
+const REPLIED_THEN_DONE: Uuid = uuid::uuid!("00000000-0000-0000-0000-00000000f303");
+const CLOSED_WITH_THANKS: Uuid = uuid::uuid!("00000000-0000-0000-0000-00000000f304");
+const ONLY_SENT: Uuid = uuid::uuid!("00000000-0000-0000-0000-00000000f305");
+const RESCHEDULE: Uuid = uuid::uuid!("00000000-0000-0000-0000-00000000f306");
+const RSVP: Uuid = uuid::uuid!("00000000-0000-0000-0000-00000000f307");
+const NOISE: Uuid = uuid::uuid!("00000000-0000-0000-0000-00000000f308");
+const CALENDAR_FILE: Uuid = uuid::uuid!("00000000-0000-0000-0000-00000000f309");
+const RECENT_CONTACT: Uuid = uuid::uuid!("00000000-0000-0000-0000-00000000f30a");
+/// In the email_focus_waiting fixture: someone the owner never wrote to, and labels.
+const STRANGER: Uuid = uuid::uuid!("00000000-0000-0000-0000-0000000cf006");
+const INBOX_LABEL: Uuid = uuid::uuid!("00000000-0000-0000-0000-0000000bf002");
+const UPDATES_LABEL: Uuid = uuid::uuid!("00000000-0000-0000-0000-0000000bf004");
+/// Answers that earn nothing: only key mail and the wait bring a thread in.
+const LOW_ANSWERS: [f32; 8] = [0.05, 0.0, 0.9, 0.0, 0.0, 0.0, 0.0, 0.0];
+/// Answers that earn Focus on their own.
+const HIGH_ANSWERS: [f32; 8] = [0.9, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0];
+
+struct NoProfile;
+
+impl ProfileSource for NoProfile {
+ async fn profile(&self, _owner: &MacroUserIdStr<'static>) -> Result<Option<String>, Report> {
+ Ok(None)
+ }
+}
+
+/// Answers every thread the same; fails the test's expectation by being called
+/// when the caller asserted it wouldn't be.
+#[derive(Clone)]
+struct FixedClassifier {
+ answers: Result<Vec<f32>, FocusClassifierError>,
+}
+
+impl FocusClassifier for FixedClassifier {
+ async fn answer(
+ &self,
+ _owner: &MacroUserIdStr<'static>,
+ _thread_id: Uuid,
+ _input: &Value,
+ _questions: &[&'static str],
+ ) -> Result<Vec<f32>, FocusClassifierError> {
+ self.answers.clone()
+ }
+
+ fn model(&self) -> &'static str {
+ "jev-test"
+ }
+}
+
+fn worker(
+ repo: &FocusPgRepository,
+ answers: Result<Vec<f32>, FocusClassifierError>,
+) -> FocusService<FocusPgRepository, NoProfile, FixedClassifier> {
+ FocusService::new(
+ repo.clone(),
+ NoProfile,
+ FixedClassifier { answers },
+ vec!["acme.example".to_owned()],
+ )
+}
+
+/// A message in a fixture thread, from Casey or the owner.
+async fn insert_message(
+ pool: &Pool<Postgres>,
+ thread_id: Uuid,
+ is_sent: bool,
+ at: DateTime<Utc>,
+) -> Result<(), Report> {
+ let from_contact_id = if is_sent {
+ uuid::uuid!("00000000-0000-0000-0000-0000000cf002")
+ } else {
+ uuid::uuid!("00000000-0000-0000-0000-0000000cf001")
+ };
+ insert_message_from(pool, thread_id, from_contact_id, is_sent, at).await
+}
+
+async fn insert_message_from(
+ pool: &Pool<Postgres>,
+ thread_id: Uuid,
+ from_contact_id: Uuid,
+ is_sent: bool,
+ at: DateTime<Utc>,
+) -> Result<(), Report> {
+ let id = macro_uuid::generate_uuid_v7();
+ sqlx::query!(
+ r#"
+ INSERT INTO email_messages (
+ id, thread_id, link_id, provider_id, is_sent, from_contact_id, internal_date_ts,
+ has_attachments, is_read, is_starred, is_draft, created_at, updated_at
+ )
+ VALUES ($1, $2, $3, $4, $5, $6, $7, false, true, false, false, NOW(), NOW())
+ "#,
+ id,
+ thread_id,
+ OWNER_LINK,
+ id.to_string(),
+ is_sent,
+ from_contact_id,
+ at,
+ )
+ .execute(pool)
+ .await?;
+ Ok(())
+}
+
+/// Project every fixture thread's labels onto it, as ingestion does.
+async fn project_threads(pool: &Pool<Postgres>) -> Result<(), Report> {
+ let mut conn = pool.acquire().await?;
+ for thread in [
+ REPLIED_LAST,
+ REPLIED_YESTERDAY,
+ REPLIED_THEN_DONE,
+ CLOSED_WITH_THANKS,
+ ONLY_SENT,
+ RESCHEDULE,
+ RSVP,
+ NOISE,
+ CALENDAR_FILE,
+ RECENT_CONTACT,
+ ] {
+ email_db_client::threads::update::recompute_thread_metadata(&mut conn, thread, OWNER_LINK)
+ .await?;
+ }
+ Ok(())
+}
async fn load_thread(
repo: &FocusPgRepository,
@@ -37,9 +171,12 @@ fn record(thread_id: Uuid, message_id: Uuid, at: DateTime<Utc>, importance: u8)
classified_message_ts: at,
verdict: FocusVerdict {
is_focus: true,
+ pinned: false,
+ calendar_hold_until: None,
category: FocusCategory::Customer,
needs_reply: true,
needs_follow_up: false,
+ awaiting_reply: false,
importance,
},
answers: FocusAnswers::from_probabilities(&[0.5; FocusQuestion::ALL.len()]).unwrap(),
@@ -59,7 +196,7 @@ async fn reads_a_thread_with_its_relationship(pool: Pool<Postgres>) -> Result<()
assert_eq!(thread.owner, owner());
assert_eq!(thread.owner_email, "[email protected]");
assert!(thread.is_signal && thread.inbox_visible);
- assert_eq!(thread.classified_message_id, None);
+ assert_eq!(thread.classified, None);
// Oldest first; the trashed follow-up is left out.
let ids = thread.messages.iter().map(|m| m.id).collect::<Vec<_>>();
assert_eq!(ids.first(), Some(&FIRST_MESSAGE));
@@ -73,13 +210,15 @@ async fn reads_a_thread_with_its_relationship(pool: Pool<Postgres>) -> Result<()
assert!(first.has_attachments && !first.bulk);
assert_eq!(thread.messages[2].from_name.as_deref(), Some("Casey C."));
assert!(thread.messages[1].is_sent);
+ let [note] = thread.sent_notes.as_slice() else {
+ panic!("expected one sent note, got {:?}", thread.sent_notes);
+ };
+ assert_eq!(note.recipient, "[email protected]");
assert_eq!(
- thread.sent_notes,
- vec![SentNote {
- recipient: "[email protected]".to_owned(),
- body: Some("Thanks, I will take a look today.".to_owned()),
- }]
+ note.body.as_deref(),
+ Some("Thanks, I will take a look today.")
);
+ assert_eq!(note.at, thread.messages[1].at);
assert!(load_thread(&repo, Uuid::new_v4()).await?.is_none());
Ok(())
}
@@ -92,13 +231,11 @@ async fn marks_bulk_mail_and_finds_brush_offs(pool: Pool<Postgres>) -> Result<()
let repo = FocusPgRepository(pool);
let thread = load_thread(&repo, BULK).await?.expect("thread exists");
assert!(thread.messages[0].bulk);
- assert_eq!(
- thread.sent_notes,
- vec![SentNote {
- recipient: "[email protected]".to_owned(),
- body: Some("No thanks".to_owned()),
- }]
- );
+ let [note] = thread.sent_notes.as_slice() else {
+ panic!("expected one sent note, got {:?}", thread.sent_notes);
+ };
+ assert_eq!(note.recipient, "[email protected]");
+ assert_eq!(note.body.as_deref(), Some("No thanks"));
Ok(())
}
@@ -114,7 +251,7 @@ async fn stale_threads_cover_enabled_inbox_signal_mail_in_the_window(
let enabled = vec!["acme.example".to_owned()];
let stale = async |limit| -> Result<Vec<Uuid>, Report> {
Ok(repo
- .stale_threads(since, &enabled, limit)
+ .stale_threads(since, &enabled, None, limit)
.await?
.into_iter()
.map(|stale| stale.thread_id)
@@ -123,8 +260,24 @@ async fn stale_threads_cover_enabled_inbox_signal_mail_in_the_window(
assert_eq!(stale(10).await?, vec![INVOICE, BULK]);
assert_eq!(stale(1).await?, vec![INVOICE]);
- assert!(repo.stale_threads(since, &[], 10).await?.is_empty());
+ assert!(repo.stale_threads(since, &[], None, 10).await?.is_empty());
+ // Each page starts past the last thread of the one before.
+ let first = repo.stale_threads(since, &enabled, None, 1).await?;
+ let second = repo
+ .stale_threads(since, &enabled, first.last().copied(), 1)
+ .await?;
+ assert_eq!(
+ second
+ .iter()
+ .map(|stale| stale.thread_id)
+ .collect::<Vec<_>>(),
+ vec![BULK]
+ );
+ let third = repo
+ .stale_threads(since, &enabled, second.last().copied(), 1)
+ .await?;
+ assert!(third.is_empty());
// Classified at an older message: still stale. At the latest: done.
let thread = load_thread(&repo, INVOICE).await?.unwrap();
repo.save(&record(INVOICE, FIRST_MESSAGE, thread.messages[0].at, 50))
@@ -134,13 +287,43 @@ async fn stale_threads_cover_enabled_inbox_signal_mail_in_the_window(
repo.save(&record(INVOICE, latest.id, latest.at, 50))
.await?;
assert_eq!(stale(10).await?, vec![BULK]);
- assert_eq!(
- load_thread(&repo, INVOICE)
- .await?
- .unwrap()
- .classified_message_id,
- Some(LATEST_MESSAGE)
- );
+ let stored = load_thread(&repo, INVOICE).await?.unwrap().classified;
+ assert_eq!(stored.map(|stored| stored.message_id), Some(LATEST_MESSAGE));
+ Ok(())
+}
+
+#[sqlx::test(
+ migrator = "MACRO_DB_MIGRATIONS",
+ fixtures(path = "../../../fixtures", scripts("email_focus"))
+)]
+async fn threads_with_the_same_latest_mail_do_not_fall_between_pages(
+ pool: Pool<Postgres>,
+) -> Result<(), Report> {
+ let repo = FocusPgRepository(pool.clone());
+ let since = Utc::now() - Duration::days(30);
+ let enabled = vec!["acme.example".to_owned()];
+ sqlx::query!(
+ r#"
+ UPDATE email_threads
+ SET latest_inbound_message_ts = $1, latest_outbound_message_ts = NULL
+ WHERE id = ANY($2)
+ "#,
+ Utc::now(),
+ &[INVOICE, BULK],
+ )
+ .execute(&pool)
+ .await?;
+ let first = repo.stale_threads(since, &enabled, None, 1).await?;
+ let second = repo
+ .stale_threads(since, &enabled, first.last().copied(), 1)
+ .await?;
+ let mut both = first
+ .iter()
+ .chain(&second)
+ .map(|stale| stale.thread_id)
+ .collect::<Vec<_>>();
+ both.sort_unstable();
+ assert_eq!(both, vec![INVOICE, BULK]);
Ok(())
}
@@ -164,16 +347,16 @@ async fn an_older_classification_never_replaces_a_newer_one(
.await?
);
- let stored = repo.focus_for_threads(&owner(), &[INVOICE]).await?;
+ let stored = repo
+ .focus_for_threads(&owner(), &[INVOICE], Utc::now(), Utc::now())
+ .await?;
let focus = stored.get(&INVOICE).expect("stored");
assert_eq!(focus.importance, 80);
assert_eq!(focus.category, FocusCategory::Customer);
assert!(focus.needs_reply && !focus.needs_follow_up && focus.is_focus);
+ let classified = load_thread(&repo, INVOICE).await?.unwrap().classified;
assert_eq!(
- load_thread(&repo, INVOICE)
- .await?
- .unwrap()
- .classified_message_id,
+ classified.map(|stored| stored.message_id),
Some(LATEST_MESSAGE)
);
@@ -182,7 +365,9 @@ async fn an_older_classification_never_replaces_a_newer_one(
repo.save(&record(INVOICE, latest.id, latest.at, 70))
.await?
);
- let stored = repo.focus_for_threads(&owner(), &[INVOICE]).await?;
+ let stored = repo
+ .focus_for_threads(&owner(), &[INVOICE], Utc::now(), Utc::now())
+ .await?;
assert_eq!(stored.get(&INVOICE).expect("stored").importance, 70);
Ok(())
}
@@ -191,10 +376,11 @@ async fn an_older_classification_never_replaces_a_newer_one(
migrator = "MACRO_DB_MIGRATIONS",
fixtures(path = "../../../fixtures", scripts("email_focus"))
)]
-async fn focus_list_is_the_owners_inbox_by_priority(pool: Pool<Postgres>) -> Result<(), Report> {
- let repo = FocusPgRepository(pool);
+async fn focus_list_is_the_owners_inbox_newest_first(pool: Pool<Postgres>) -> Result<(), Report> {
+ let repo = FocusPgRepository(pool.clone());
let now = Utc::now();
- repo.save(&record(INVOICE, LATEST_MESSAGE, now, 60)).await?;
+ let since = now - Duration::days(30);
+ repo.save(&record(INVOICE, LATEST_MESSAGE, now, 10)).await?;
repo.save(&record(BULK, Uuid::new_v4(), now, 90)).await?;
// Archived threads leave Focus even with a stored verdict.
repo.save(&record(ARCHIVED, Uuid::new_v4(), now, 99))
@@ -206,48 +392,48 @@ async fn focus_list_is_the_owners_inbox_by_priority(pool: Pool<Postgres>) -> Res
})
.await?;
- // Mail from 20 hours ago at 60 outranks mail from 3 days ago at 90: the
- // older thread has lost a half-life more.
- let since = now - Duration::days(30);
+ // Importance plays no part: mail from 20 hours ago at 10 comes before mail
+ // from 3 days ago at 90, and the limit keeps the newer one.
assert_eq!(
- repo.focus_thread_ids(&owner(), since, 10).await?,
+ repo.focus_thread_ids(&owner(), since, now, now, 10).await?,
vec![INVOICE, BULK]
);
assert_eq!(
- repo.focus_thread_ids(&owner(), since, 1).await?,
+ repo.focus_thread_ids(&owner(), since, now, now, 1).await?,
vec![INVOICE]
);
- // A wide enough importance gap still wins.
- repo.save(&record(INVOICE, LATEST_MESSAGE, now, 30)).await?;
assert_eq!(
- repo.focus_thread_ids(&owner(), since, 10).await?,
- vec![BULK, INVOICE]
+ repo.focus_thread_ids(&owner(), now - Duration::days(1), now, now, 10)
+ .await?,
+ vec![INVOICE]
);
- // Importance 0 ranks as 1 instead of failing on log(0).
- repo.save(&record(INVOICE, LATEST_MESSAGE, now, 0)).await?;
+
+ // A newer message in either direction moves a thread up: the owner's reply
+ // an hour ago brings BULK ahead of INVOICE's newer inbound mail.
+ insert_message(&pool, BULK, true, now - Duration::hours(1)).await?;
assert_eq!(
- repo.focus_thread_ids(&owner(), since, 10).await?,
+ repo.focus_thread_ids(&owner(), since, now, now, 10).await?,
vec![BULK, INVOICE]
);
assert_eq!(
- repo.focus_thread_ids(&owner(), now - Duration::days(1), 10)
- .await?,
- vec![INVOICE]
+ repo.focus_thread_ids(&owner(), since, now, now, 1).await?,
+ vec![BULK]
);
// The other inbox's owner sees only their own thread.
let stranger = MacroUserIdStr::try_from_email("[email protected]").unwrap();
assert_eq!(
- repo.focus_thread_ids(&stranger, since, 10).await?,
+ repo.focus_thread_ids(&stranger, since, now, now, 10)
+ .await?,
vec![OTHER_THREAD]
);
assert!(
- repo.focus_for_threads(&stranger, &[INVOICE, BULK])
+ repo.focus_for_threads(&stranger, &[INVOICE, BULK], now, now)
.await?
.is_empty()
);
assert_eq!(
- repo.focus_for_threads(&owner(), &[INVOICE, BULK, ARCHIVED])
+ repo.focus_for_threads(&owner(), &[INVOICE, BULK, ARCHIVED], now, now)
.await?
.len(),
3
@@ -273,14 +459,1281 @@ async fn focus_covers_inboxes_linked_to_the_owner(pool: Pool<Postgres>) -> Resul
// The other inbox is linked to the owner's account, so its Focus is theirs too.
assert_eq!(
- repo.focus_thread_ids(&owner(), now - Duration::days(30), 10)
+ repo.focus_thread_ids(&owner(), now - Duration::days(30), now, now, 10)
.await?,
vec![OTHER_THREAD]
);
assert!(
- repo.focus_for_threads(&owner(), &[OTHER_THREAD])
+ repo.focus_for_threads(&owner(), &[OTHER_THREAD], now, now)
.await?
.contains_key(&OTHER_THREAD)
);
Ok(())
}
+
+#[sqlx::test(
+ migrator = "MACRO_DB_MIGRATIONS",
+ fixtures(
+ path = "../../../fixtures",
+ scripts("email_focus", "email_focus_waiting")
+ )
+)]
+async fn a_thread_the_owner_answered_last_reaches_the_list_after_the_wait(
+ pool: Pool<Postgres>,
+) -> Result<(), Report> {
+ project_threads(&pool).await?;
+ let repo = FocusPgRepository(pool.clone());
+ let worker = worker(&repo, Ok(LOW_ANSWERS.to_vec()));
+ let queries = FocusQueries::new(repo.clone());
+
+ // In the inbox, so the client shows it with no change: `done` is
+ // `!inboxVisible`, and this thread's incoming mail keeps its INBOX label.
+ let inbox = repo.thread_inbox(REPLIED_LAST).await?.expect("thread");
+ assert!(inbox.inbox_visible);
+
+ for thread in [REPLIED_LAST, REPLIED_YESTERDAY] {
+ let ClassifyOutcome::Classified(verdict) = worker.classify_thread(thread).await? else {
+ panic!("expected {thread} to be classified");
+ };
+ assert!(verdict.awaiting_reply);
+ }
+ let listed = queries.focus_thread_ids(&owner(), 30).await?;
+ assert!(listed.contains(&REPLIED_LAST));
+ assert!(!listed.contains(&REPLIED_YESTERDAY));
+
+ let focus = queries
+ .focus_for_threads(&owner(), &[REPLIED_LAST, REPLIED_YESTERDAY])
+ .await?;
+ let waiting = focus.get(&REPLIED_LAST).expect("stored");
+ assert!(waiting.is_focus && waiting.needs_follow_up && !waiting.needs_reply);
+ assert_eq!(waiting.importance, 55);
+ let not_yet = focus.get(&REPLIED_YESTERDAY).expect("stored");
+ assert!(!not_yet.is_focus && !not_yet.needs_follow_up);
+
+ // Its signal score plays no part.
+ sqlx::query!(
+ "UPDATE email_threads SET is_signal = false WHERE id = $1",
+ REPLIED_LAST
+ )
+ .execute(&pool)
+ .await?;
+ assert!(
+ queries
+ .focus_thread_ids(&owner(), 30)
+ .await?
+ .contains(&REPLIED_LAST)
+ );
+
+ // Editing a draft moves the thread's inbound time, as recompute_thread_metadata
+ // does, but nobody wrote: it still waits and still reads as a follow-up.
+ sqlx::query!(
+ "UPDATE email_threads SET latest_inbound_message_ts = $1 WHERE id = $2",
+ Utc::now(),
+ REPLIED_LAST,
+ )
+ .execute(&pool)
+ .await?;
+ assert!(
+ queries
+ .focus_thread_ids(&owner(), 30)
+ .await?
+ .contains(&REPLIED_LAST)
+ );
+ let focus = queries.focus_for_threads(&owner(), &[REPLIED_LAST]).await?;
+ let drafting = focus.get(&REPLIED_LAST).expect("stored");
+ assert!(drafting.needs_follow_up && !drafting.needs_reply);
+
+ // An answer ends the wait at once, ahead of the worker. Until the worker
+ // reads it the thread keeps no label it no longer has.
+ insert_message(&pool, REPLIED_LAST, false, Utc::now()).await?;
+ assert!(
+ !queries
+ .focus_thread_ids(&owner(), 30)
+ .await?
+ .contains(&REPLIED_LAST)
+ );
+ let focus = queries.focus_for_threads(&owner(), &[REPLIED_LAST]).await?;
+ let answered = focus.get(&REPLIED_LAST).expect("stored");
+ assert!(!answered.is_focus && !answered.needs_follow_up);
+ Ok(())
+}
+
+#[sqlx::test(
+ migrator = "MACRO_DB_MIGRATIONS",
+ fixtures(
+ path = "../../../fixtures",
+ scripts("email_focus", "email_focus_waiting")
+ )
+)]
+async fn the_owner_writing_again_restarts_the_wait(pool: Pool<Postgres>) -> Result<(), Report> {
+ project_threads(&pool).await?;
+ let repo = FocusPgRepository(pool.clone());
+ let worker = worker(&repo, Ok(LOW_ANSWERS.to_vec()));
+ let queries = FocusQueries::new(repo);
+ worker.classify_thread(REPLIED_LAST).await?;
+ assert!(
+ queries
+ .focus_thread_ids(&owner(), 30)
+ .await?
+ .contains(&REPLIED_LAST)
+ );
+
+ // Ahead of the worker, the stored row still describes the older message.
+ insert_message(&pool, REPLIED_LAST, true, Utc::now()).await?;
+ assert!(
+ !queries
+ .focus_thread_ids(&owner(), 30)
+ .await?
+ .contains(&REPLIED_LAST)
+ );
+ let focus = queries.focus_for_threads(&owner(), &[REPLIED_LAST]).await?;
+ let restarted = focus.get(&REPLIED_LAST).expect("stored");
+ assert!(!restarted.is_focus && !restarted.needs_follow_up);
+ Ok(())
+}
+
+#[sqlx::test(
+ migrator = "MACRO_DB_MIGRATIONS",
+ fixtures(
+ path = "../../../fixtures",
+ scripts("email_focus", "email_focus_waiting")
+ )
+)]
+async fn an_answer_ends_the_wait_however_long_ago_it_came(
+ pool: Pool<Postgres>,
+) -> Result<(), Report> {
+ project_threads(&pool).await?;
+ let repo = FocusPgRepository(pool.clone());
+ let worker = worker(&repo, Ok(LOW_ANSWERS.to_vec()));
+ let queries = FocusQueries::new(repo);
+ worker.classify_thread(REPLIED_LAST).await?;
+
+ // Answered past the wait. Outside the signal inbox the worker can leave this
+ // row as it is, so only who wrote last says the wait is over.
+ insert_message(&pool, REPLIED_LAST, false, Utc::now() - Duration::days(4)).await?;
+ assert!(
+ !queries
+ .focus_thread_ids(&owner(), 30)
+ .await?
+ .contains(&REPLIED_LAST)
+ );
+ let focus = queries.focus_for_threads(&owner(), &[REPLIED_LAST]).await?;
+ let answered = focus.get(&REPLIED_LAST).expect("stored");
+ assert!(!answered.is_focus && !answered.needs_follow_up);
+ Ok(())
+}
+
+#[sqlx::test(
+ migrator = "MACRO_DB_MIGRATIONS",
+ fixtures(
+ path = "../../../fixtures",
+ scripts("email_focus", "email_focus_waiting")
+ )
+)]
+async fn a_thread_of_only_sent_mail_stays_out(pool: Pool<Postgres>) -> Result<(), Report> {
+ project_threads(&pool).await?;
+ let repo = FocusPgRepository(pool);
+ let now = Utc::now();
+ let since = now - Duration::days(30);
+
+ let inbox = repo.thread_inbox(ONLY_SENT).await?.expect("thread");
+ assert!(!inbox.inbox_visible);
+ let stale = repo
+ .stale_threads(since, &["acme.example".to_owned()], None, 100)
+ .await?;
+ assert!(!stale.iter().any(|stale| stale.thread_id == ONLY_SENT));
+ // Failing the classifier proves it was never asked.
+ let worker = worker(&repo, Err(FocusClassifierError::Rejected));
+ assert_eq!(
+ worker.classify_thread(ONLY_SENT).await?,
+ ClassifyOutcome::NotSignal
+ );
+
+ // Nothing classifies one, and a row could not bring it in anyway.
+ let at = now - Duration::days(5);
+ let mut waiting = record(ONLY_SENT, Uuid::new_v4(), at, 55);
+ waiting.verdict.awaiting_reply = true;
+ repo.save(&waiting).await?;
+ assert!(
+ !repo
+ .focus_thread_ids(&owner(), since, now, now, 100)
+ .await?
+ .contains(&ONLY_SENT)
+ );
+ Ok(())
+}
+
+#[sqlx::test(
+ migrator = "MACRO_DB_MIGRATIONS",
+ fixtures(
+ path = "../../../fixtures",
+ scripts("email_focus", "email_focus_waiting")
+ )
+)]
+async fn done_threads_and_closed_exchanges_are_not_waiting(
+ pool: Pool<Postgres>,
+) -> Result<(), Report> {
+ project_threads(&pool).await?;
+ let repo = FocusPgRepository(pool);
+ let now = Utc::now();
+ let since = now - Duration::days(30);
+ let worker = worker(&repo, Ok(LOW_ANSWERS.to_vec()));
+
+ // Marked Done: out of the inbox, so never classified or listed.
+ assert_eq!(
+ worker.classify_thread(REPLIED_THEN_DONE).await?,
+ ClassifyOutcome::NotSignal
+ );
+ let at = now - Duration::days(5);
+ let mut waiting = record(REPLIED_THEN_DONE, Uuid::new_v4(), at, 55);
+ waiting.verdict.awaiting_reply = true;
+ repo.save(&waiting).await?;
+ assert!(
+ !repo
+ .focus_thread_ids(&owner(), since, now, now, 100)
+ .await?
+ .contains(&REPLIED_THEN_DONE)
+ );
+
+ // "Thanks!" leaves nobody owing a reply.
+ let ClassifyOutcome::Classified(verdict) = worker.classify_thread(CLOSED_WITH_THANKS).await?
+ else {
+ panic!("expected a classification");
+ };
+ assert!(!verdict.awaiting_reply);
+ assert!(
+ !repo
+ .focus_thread_ids(&owner(), since, now, now, 100)
+ .await?
+ .contains(&CLOSED_WITH_THANKS)
+ );
+ Ok(())
+}
+
+#[sqlx::test(
+ migrator = "MACRO_DB_MIGRATIONS",
+ fixtures(
+ path = "../../../fixtures",
+ scripts("email_focus", "email_focus_waiting")
+ )
+)]
+async fn a_reschedule_reaches_focus_from_outside_the_signal_inbox(
+ pool: Pool<Postgres>,
+) -> Result<(), Report> {
+ project_threads(&pool).await?;
+ let repo = FocusPgRepository(pool);
+ let now = Utc::now();
+ let since = now - Duration::days(30);
+ let worker = worker(&repo, Ok(LOW_ANSWERS.to_vec()));
+
+ // Gmail filed these under Updates, so they are not signal threads.
+ let reschedule = load_thread(&repo, RESCHEDULE).await?.expect("thread");
+ assert!(!reschedule.is_signal);
+ assert!(matches!(
+ reschedule.messages[0].calendar_part,
+ Some(CalendarPart::Change { .. })
+ ));
+ let rsvp = load_thread(&repo, RSVP).await?.expect("thread");
+ assert_eq!(rsvp.messages[0].calendar_part, Some(CalendarPart::Rsvp));
+ let file = load_thread(&repo, CALENDAR_FILE).await?.expect("thread");
+ assert_eq!(file.messages[0].calendar_part, Some(CalendarPart::Unparsed));
+
+ // The sweep finds only the change; the others have no reason to be read.
+ let stale = repo
+ .stale_threads(since, &["acme.example".to_owned()], None, 100)
+ .await?
+ .into_iter()
+ .map(|stale| stale.thread_id)
+ .collect::<Vec<_>>();
+ assert!(stale.contains(&RESCHEDULE));
+ for other in [RSVP, CALENDAR_FILE, NOISE] {
+ assert!(!stale.contains(&other), "{other}");
+ }
+
+ let ClassifyOutcome::Classified(verdict) = worker.classify_thread(RESCHEDULE).await? else {
+ panic!("expected the reschedule to be classified");
+ };
+ assert!(verdict.calendar_hold_until.is_some());
+ assert!(!verdict.is_focus);
+ // None of these is key mail, so none reaches the classifier.
+ for other in [RSVP, CALENDAR_FILE, NOISE] {
+ assert_eq!(
+ worker.classify_thread(other).await?,
+ ClassifyOutcome::NotSignal,
+ "{other}"
+ );
+ }
+
+ let listed = repo
+ .focus_thread_ids(&owner(), since, now, now, 100)
+ .await?;
+ assert_eq!(listed, vec![RESCHEDULE]);
+ let focus = repo
+ .focus_for_threads(&owner(), &[RESCHEDULE], now, now)
+ .await?;
+ let held = focus.get(&RESCHEDULE).expect("stored");
+ assert!(held.is_focus);
+ assert_eq!(held.category, FocusCategory::Calendar);
+ assert_eq!(held.importance, 50);
+ Ok(())
+}
+
+fn invite(method: InvitationMethod, sequence: u32, end: DateTime<Utc>) -> CalendarInvitation {
+ CalendarInvitation {
+ id: macro_uuid::generate_uuid_v7().to_string(),
+ uid: "[email protected]".to_owned(),
+ method,
+ sequence,
+ dtstamp: None,
+ last_modified: None,
+ status: None,
+ recurrence_id: None,
+ recurrence_id_raw: None,
+ title: None,
+ organizer: None,
+ attendees: Vec::new(),
+ comment: None,
+ location: None,
+ description: None,
+ start: None,
+ end: Some(InvitationDateTime::Zoned {
+ value: end.to_rfc3339(),
+ time_zone: "UTC".to_owned(),
+ }),
+ recurrence: Some(Vec::new()),
+ conference_url: None,
+ }
+}
+
+/// A new thread holding one message from a stranger, received `at`, with what
+/// ingestion would store for it. Filed under Updates it is outside the signal
+/// inbox, so only what the message is can bring it in.
+async fn notice_thread(
+ pool: &Pool<Postgres>,
+ subject: &str,
+ invite: Option<CalendarInvitation>,
+ at: DateTime<Utc>,
+ filed_under_updates: bool,
+) -> Result<Uuid, Report> {
+ let thread_id = macro_uuid::generate_uuid_v7();
+ let message_id = macro_uuid::generate_uuid_v7();
+ sqlx::query!(
+ r#"
+ INSERT INTO email_threads (id, link_id, inbox_visible, is_read, is_signal, created_at, updated_at)
+ VALUES ($1, $2, false, true, false, NOW(), NOW())
+ "#,
+ thread_id,
+ OWNER_LINK,
+ )
+ .execute(pool)
+ .await?;
+ sqlx::query!(
+ r#"
+ INSERT INTO email_messages (
+ id, thread_id, link_id, provider_id, is_sent, from_contact_id, internal_date_ts,
+ subject, body_text, has_attachments, is_read, is_starred, is_draft, created_at, updated_at
+ )
+ VALUES ($1, $2, $3, $4, false, $5, $6, $7, '', false, true, false, false, NOW(), NOW())
+ "#,
+ message_id,
+ thread_id,
+ OWNER_LINK,
+ message_id.to_string(),
+ STRANGER,
+ at,
+ subject,
+ )
+ .execute(pool)
+ .await?;
+ let mut labels = vec![INBOX_LABEL];
+ labels.extend(filed_under_updates.then_some(UPDATES_LABEL));
+ for label in labels {
+ sqlx::query!(
+ "INSERT INTO email_message_labels (message_id, label_id) VALUES ($1, $2)",
+ message_id,
+ label,
+ )
+ .execute(pool)
+ .await?;
+ }
+ if let Some(invite) = invite {
+ sqlx::query!(
+ r#"
+ INSERT INTO email_message_calendar_invites (message_id, component_id, snapshot)
+ VALUES ($1, $2, $3)
+ "#,
+ message_id,
+ invite.id,
+ serde_json::to_value(&invite)?,
+ )
+ .execute(pool)
+ .await?;
+ }
+ let mut conn = pool.acquire().await?;
+ email_db_client::threads::update::recompute_thread_metadata(&mut conn, thread_id, OWNER_LINK)
+ .await?;
+ Ok(thread_id)
+}
+
+/// Whether the list holds `thread` at `now`, and how it reads then.
+async fn read_at(
+ repo: &FocusPgRepository,
+ thread: Uuid,
+ now: DateTime<Utc>,
+) -> Result<(bool, ThreadFocus), Report> {
+ let since = now - Duration::days(90);
+ let waiting_before = now - Duration::days(3);
+ let listed = repo
+ .focus_thread_ids(&owner(), since, waiting_before, now, 100)
+ .await?
+ .contains(&thread);
+ let focus = repo
+ .focus_for_threads(&owner(), &[thread], waiting_before, now)
+ .await?;
+ Ok((listed, focus.get(&thread).expect("stored").clone()))
+}
+
+async fn is_stale(repo: &FocusPgRepository, thread: Uuid) -> Result<bool, Report> {
+ let since = Utc::now() - Duration::days(30);
+ let enabled = vec!["acme.example".to_owned()];
+ let stale = repo.stale_threads(since, &enabled, None, 100).await?;
+ Ok(stale.iter().any(|stale| stale.thread_id == thread))
+}
+
+/// A change for an event that isn't over: found, classified, listed as Calendar.
+async fn assert_held(repo: &FocusPgRepository, thread: Uuid) -> Result<(), Report> {
+ assert!(is_stale(repo, thread).await?);
+ let verdict = worker(repo, Ok(LOW_ANSWERS.to_vec()))
+ .classify_thread(thread)
+ .await?;
+ assert!(matches!(verdict, ClassifyOutcome::Classified(_)));
+ let (listed, focus) = read_at(repo, thread, Utc::now()).await?;
+ assert!(listed && focus.is_focus);
+ assert_eq!(focus.category, FocusCategory::Calendar);
+ Ok(())
+}
+
+#[sqlx::test(
+ migrator = "MACRO_DB_MIGRATIONS",
+ fixtures(
+ path = "../../../fixtures",
+ scripts("email_focus", "email_focus_waiting")
+ )
+)]
+async fn an_upcoming_reschedule_is_in_until_the_event_is_over(
+ pool: Pool<Postgres>,
+) -> Result<(), Report> {
+ let repo = FocusPgRepository(pool.clone());
+ let now = Utc::now();
+ let end = now + Duration::days(2);
+ let thread = notice_thread(
+ &pool,
+ "Moved: Demo",
+ Some(invite(InvitationMethod::Request, 2, end)),
+ now - Duration::hours(1),
+ true,
+ )
+ .await?;
+ assert_held(&repo, thread).await?;
+
+ let (listed, focus) = read_at(&repo, thread, end - Duration::minutes(1)).await?;
+ assert!(listed && focus.is_focus);
+ assert_eq!(focus.category, FocusCategory::Calendar);
+
+ // Over: both reads agree it is out, and it no longer reads as Calendar.
+ let (listed, focus) = read_at(&repo, thread, end).await?;
+ assert!(!listed && !focus.is_focus);
+ assert!(!focus.needs_reply && !focus.needs_follow_up);
+ assert_ne!(focus.category, FocusCategory::Calendar);
+ Ok(())
+}
+
+#[sqlx::test(
+ migrator = "MACRO_DB_MIGRATIONS",
+ fixtures(
+ path = "../../../fixtures",
+ scripts("email_focus", "email_focus_waiting")
+ )
+)]
+async fn a_held_thread_that_needs_a_reply_loses_the_flag_with_the_hold(
+ pool: Pool<Postgres>,
+) -> Result<(), Report> {
+ let repo = FocusPgRepository(pool.clone());
+ let now = Utc::now();
+ let end = now + Duration::days(2);
+ let thread = notice_thread(
+ &pool,
+ "Moved: Demo",
+ Some(invite(InvitationMethod::Request, 2, end)),
+ now - Duration::hours(1),
+ true,
+ )
+ .await?;
+ // Reads as automated, yet Jev says it asks the owner something.
+ let asks = worker(&repo, Ok(vec![0.05, 0.0, 0.9, 0.0, 0.0, 0.0, 0.9, 0.0]));
+ assert!(matches!(
+ asks.classify_thread(thread).await?,
+ ClassifyOutcome::Classified(_)
+ ));
+ let (_, focus) = read_at(&repo, thread, now).await?;
+ assert!(focus.is_focus && focus.needs_reply);
+
+ let (_, focus) = read_at(&repo, thread, end).await?;
+ assert!(!focus.is_focus && !focus.needs_reply);
+ Ok(())
+}
+
+#[sqlx::test(
+ migrator = "MACRO_DB_MIGRATIONS",
+ fixtures(
+ path = "../../../fixtures",
+ scripts("email_focus", "email_focus_waiting")
+ )
+)]
+async fn a_new_invite_stays_out(pool: Pool<Postgres>) -> Result<(), Report> {
+ let repo = FocusPgRepository(pool.clone());
+ let now = Utc::now();
+ let at = now - Duration::hours(1);
+ let upcoming = now + Duration::days(2);
+ let refuses = worker(&repo, Err(FocusClassifierError::Rejected));
+ let earns = worker(&repo, Ok(HIGH_ANSWERS.to_vec()));
+
+ // Outside the signal inbox: never found, never read by the classifier.
+ for thread in [
+ notice_thread(
+ &pool,
+ "Demo",
+ Some(invite(InvitationMethod::Request, 0, upcoming)),
+ at,
+ true,
+ )
+ .await?,
+ notice_thread(&pool, "Invitation: Demo @ Thu", None, at, true).await?,
+ notice_thread(&pool, "New event: Demo", None, at, true).await?,
+ ] {
+ assert!(!is_stale(&repo, thread).await?);
+ assert_eq!(
+ refuses.classify_thread(thread).await?,
+ ClassifyOutcome::NotSignal
+ );
+ }
+
+ // In the signal inbox: out however much Jev likes it. Calendar names why.
+ let thread = notice_thread(
+ &pool,
+ "Invitation: Demo @ Thu",
+ Some(invite(InvitationMethod::Request, 0, upcoming)),
+ at,
+ false,
+ )
+ .await?;
+ let ClassifyOutcome::Classified(verdict) = earns.classify_thread(thread).await? else {
+ panic!("expected a classification");
+ };
+ assert!(!verdict.is_focus && verdict.calendar_hold_until.is_none());
+ let (listed, focus) = read_at(&repo, thread, now).await?;
+ assert!(!listed && !focus.is_focus);
+ assert_eq!(focus.category, FocusCategory::Calendar);
+ Ok(())
+}
+
+#[sqlx::test(
+ migrator = "MACRO_DB_MIGRATIONS",
+ fixtures(
+ path = "../../../fixtures",
+ scripts("email_focus", "email_focus_waiting")
+ )
+)]
+async fn an_update_to_an_event_already_over_stays_out(pool: Pool<Postgres>) -> Result<(), Report> {
+ let repo = FocusPgRepository(pool.clone());
+ let now = Utc::now();
+ let at = now - Duration::hours(3);
+ let over = now - Duration::hours(1);
+
+ // Outside the signal inbox the sweep reads it, and the classifier never does.
+ let outside = notice_thread(
+ &pool,
+ "Updated invitation: Demo",
+ Some(invite(InvitationMethod::Request, 1, over)),
+ at,
+ true,
+ )
+ .await?;
+ assert_eq!(
+ worker(&repo, Err(FocusClassifierError::Rejected))
+ .classify_thread(outside)
+ .await?,
+ ClassifyOutcome::NotSignal
+ );
+
+ // In the signal inbox with nothing else going for it: out.
+ let signal = notice_thread(
+ &pool,
+ "Updated invitation: Demo",
+ Some(invite(InvitationMethod::Request, 1, over)),
+ at,
+ false,
+ )
+ .await?;
+ let low = worker(&repo, Ok(LOW_ANSWERS.to_vec()));
+ assert!(matches!(
+ low.classify_thread(signal).await?,
+ ClassifyOutcome::Classified(_)
+ ));
+ let (listed, focus) = read_at(&repo, signal, now).await?;
+ assert!(!listed && !focus.is_focus);
+ assert_ne!(focus.category, FocusCategory::Calendar);
+ Ok(())
+}
+
+#[sqlx::test(
+ migrator = "MACRO_DB_MIGRATIONS",
+ fixtures(
+ path = "../../../fixtures",
+ scripts("email_focus", "email_focus_waiting")
+ )
+)]
+async fn a_thread_in_focus_for_more_than_the_hold_stays_after_the_event(
+ pool: Pool<Postgres>,
+) -> Result<(), Report> {
+ let repo = FocusPgRepository(pool.clone());
+ let now = Utc::now();
+ let end = now + Duration::days(2);
+ let high = worker(&repo, Ok(HIGH_ANSWERS.to_vec()));
+
+ // Jev says it is worth reading: in for that reason once the event is over,
+ // when it reads as its sender's category, since Calendar means a live change.
+ let signal = notice_thread(
+ &pool,
+ "Rescheduled: Demo",
+ Some(invite(InvitationMethod::Request, 1, end)),
+ now - Duration::hours(1),
+ false,
+ )
+ .await?;
+ high.classify_thread(signal).await?;
+ let (listed, focus) = read_at(&repo, signal, now).await?;
+ assert!(listed && focus.is_focus);
+ assert_eq!(focus.category, FocusCategory::Calendar);
+ let (listed, focus) = read_at(&repo, signal, end).await?;
+ assert!(listed && focus.is_focus);
+ assert_eq!(focus.category, FocusCategory::Other);
+
+ // The same answers outside the signal inbox earn nothing: only the hold had it in.
+ let outside = notice_thread(
+ &pool,
+ "Rescheduled: Demo",
+ Some(invite(InvitationMethod::Request, 1, end)),
+ now - Duration::hours(1),
+ true,
+ )
+ .await?;
+ high.classify_thread(outside).await?;
+ let (listed, focus) = read_at(&repo, outside, now).await?;
+ assert!(listed && focus.is_focus);
+ let (listed, focus) = read_at(&repo, outside, end).await?;
+ assert!(!listed && !focus.is_focus);
+ Ok(())
+}
+
+#[sqlx::test(
+ migrator = "MACRO_DB_MIGRATIONS",
+ fixtures(
+ path = "../../../fixtures",
+ scripts("email_focus", "email_focus_waiting")
+ )
+)]
+async fn a_change_subject_on_a_first_revision_holds_by_the_events_own_time(
+ pool: Pool<Postgres>,
+) -> Result<(), Report> {
+ let repo = FocusPgRepository(pool.clone());
+ let now = Utc::now();
+ let at = now - Duration::hours(1);
+
+ // Further ahead than the fallback: still held when the fallback would have ended.
+ let end = now + Duration::days(20);
+ let ahead = notice_thread(
+ &pool,
+ "Updated invitation: Demo",
+ Some(invite(InvitationMethod::Request, 0, end)),
+ at,
+ true,
+ )
+ .await?;
+ assert_held(&repo, ahead).await?;
+ let (listed, _) = read_at(&repo, ahead, now + Duration::days(10)).await?;
+ assert!(listed);
+ let (listed, _) = read_at(&repo, ahead, end).await?;
+ assert!(!listed);
+
+ // Already over: not held at all, and the classifier is never asked.
+ let over = notice_thread(
+ &pool,
+ "Updated invitation: Demo",
+ Some(invite(
+ InvitationMethod::Request,
+ 0,
+ now - Duration::minutes(5),
+ )),
+ at,
+ true,
+ )
+ .await?;
+ assert_eq!(
+ worker(&repo, Err(FocusClassifierError::Rejected))
+ .classify_thread(over)
+ .await?,
+ ClassifyOutcome::NotSignal
+ );
+ Ok(())
+}
+
+#[sqlx::test(
+ migrator = "MACRO_DB_MIGRATIONS",
+ fixtures(
+ path = "../../../fixtures",
+ scripts("email_focus", "email_focus_waiting")
+ )
+)]
+async fn a_held_thread_marked_done_leaves_focus(pool: Pool<Postgres>) -> Result<(), Report> {
+ let repo = FocusPgRepository(pool.clone());
+ let now = Utc::now();
+ let thread = notice_thread(
+ &pool,
+ "Rescheduled: Demo",
+ Some(invite(
+ InvitationMethod::Request,
+ 1,
+ now + Duration::days(2),
+ )),
+ now - Duration::hours(1),
+ true,
+ )
+ .await?;
+ assert_held(&repo, thread).await?;
+ sqlx::query!(
+ "UPDATE email_threads SET inbox_visible = false WHERE id = $1",
+ thread
+ )
+ .execute(&pool)
+ .await?;
+ let (listed, focus) = read_at(&repo, thread, now).await?;
+ assert!(!listed && !focus.is_focus);
+ assert_ne!(focus.category, FocusCategory::Calendar);
+ Ok(())
+}
+
+#[sqlx::test(
+ migrator = "MACRO_DB_MIGRATIONS",
+ fixtures(
+ path = "../../../fixtures",
+ scripts("email_focus", "email_focus_waiting")
+ )
+)]
+async fn the_sweep_ignores_a_change_only_in_the_owners_mail_or_a_draft(
+ pool: Pool<Postgres>,
+) -> Result<(), Report> {
+ let repo = FocusPgRepository(pool.clone());
+ let now = Utc::now();
+ for (is_sent, is_draft) in [(false, true), (true, false)] {
+ let thread = notice_thread(&pool, "Lunch?", None, now - Duration::hours(2), true).await?;
+ let id = macro_uuid::generate_uuid_v7();
+ sqlx::query!(
+ r#"
+ INSERT INTO email_messages (
+ id, thread_id, link_id, provider_id, is_sent, from_contact_id, internal_date_ts,
+ subject, body_text, has_attachments, is_read, is_starred, is_draft, created_at, updated_at
+ )
+ VALUES ($1, $2, $3, $4, $5, $6, $7, 'Rescheduled: Demo', '', false, true, false, $8, NOW(), NOW())
+ "#,
+ id,
+ thread,
+ OWNER_LINK,
+ id.to_string(),
+ is_sent,
+ STRANGER,
+ now - Duration::hours(1),
+ is_draft,
+ )
+ .execute(&pool)
+ .await?;
+ assert!(!is_stale(&repo, thread).await?, "{is_sent} {is_draft}");
+ }
+ Ok(())
+}
+
+#[sqlx::test(
+ migrator = "MACRO_DB_MIGRATIONS",
+ fixtures(
+ path = "../../../fixtures",
+ scripts("email_focus", "email_focus_waiting")
+ )
+)]
+async fn a_cancellation_is_in(pool: Pool<Postgres>) -> Result<(), Report> {
+ let repo = FocusPgRepository(pool.clone());
+ let now = Utc::now();
+ let end = now + Duration::days(2);
+ let thread = notice_thread(
+ &pool,
+ "Demo",
+ Some(invite(InvitationMethod::Cancel, 0, end)),
+ now - Duration::hours(1),
+ true,
+ )
+ .await?;
+ assert_held(&repo, thread).await?;
+ let (listed, _) = read_at(&repo, thread, end).await?;
+ assert!(!listed);
+ Ok(())
+}
+
+#[sqlx::test(
+ migrator = "MACRO_DB_MIGRATIONS",
+ fixtures(
+ path = "../../../fixtures",
+ scripts("email_focus", "email_focus_waiting")
+ )
+)]
+async fn a_counter_proposal_is_in(pool: Pool<Postgres>) -> Result<(), Report> {
+ let repo = FocusPgRepository(pool.clone());
+ let now = Utc::now();
+ let end = now + Duration::days(2);
+ let thread = notice_thread(
+ &pool,
+ "Demo",
+ Some(invite(InvitationMethod::Counter, 0, end)),
+ now - Duration::hours(1),
+ true,
+ )
+ .await?;
+ assert_held(&repo, thread).await?;
+ let (listed, _) = read_at(&repo, thread, end).await?;
+ assert!(!listed);
+ Ok(())
+}
+
+#[sqlx::test(
+ migrator = "MACRO_DB_MIGRATIONS",
+ fixtures(
+ path = "../../../fixtures",
+ scripts("email_focus", "email_focus_waiting")
+ )
+)]
+async fn a_series_or_a_subject_alone_holds_for_a_week_after_the_notice(
+ pool: Pool<Postgres>,
+) -> Result<(), Report> {
+ let repo = FocusPgRepository(pool.clone());
+ let now = Utc::now();
+ let at = now - Duration::hours(1);
+ // The series began long ago, so its own dates say nothing about what's ahead.
+ let mut series = invite(InvitationMethod::Request, 3, now - Duration::days(200));
+ series.recurrence = Some(vec!["RRULE:FREQ=WEEKLY".to_owned()]);
+ let mut wall_time = invite(InvitationMethod::Cancel, 0, now);
+ wall_time.end = Some(InvitationDateTime::Unresolved {
+ value: "2026-11-05T11:00:00".to_owned(),
+ time_zone: None,
+ });
+ for thread in [
+ notice_thread(&pool, "Standup", Some(series), at, true).await?,
+ notice_thread(&pool, "Standup", Some(wall_time), at, true).await?,
+ notice_thread(&pool, "Rescheduled: Demo", None, at, true).await?,
+ notice_thread(&pool, "Canceled event: Demo", None, at, true).await?,
+ ] {
+ assert_held(&repo, thread).await?;
+ let last = at + Duration::days(7);
+ let (listed, _) = read_at(&repo, thread, last - Duration::minutes(1)).await?;
+ assert!(listed);
+ let (listed, focus) = read_at(&repo, thread, last).await?;
+ assert!(!listed && !focus.is_focus);
+ }
+ Ok(())
+}
+
+#[sqlx::test(
+ migrator = "MACRO_DB_MIGRATIONS",
+ fixtures(
+ path = "../../../fixtures",
+ scripts("email_focus", "email_focus_waiting")
+ )
+)]
+async fn a_person_writing_after_a_notice_makes_it_a_conversation(
+ pool: Pool<Postgres>,
+) -> Result<(), Report> {
+ let repo = FocusPgRepository(pool.clone());
+ let now = Utc::now();
+ let at = now - Duration::hours(2);
+ let end = now + Duration::days(2);
+ let low = worker(&repo, Ok(LOW_ANSWERS.to_vec()));
+ let high = worker(&repo, Ok(HIGH_ANSWERS.to_vec()));
+
+ // A change: the reply ends the hold right away, ahead of the worker.
+ let outside = notice_thread(
+ &pool,
+ "Rescheduled: Demo",
+ Some(invite(InvitationMethod::Request, 1, end)),
+ at,
+ true,
+ )
+ .await?;
+ assert_held(&repo, outside).await?;
+ insert_message_from(&pool, outside, STRANGER, false, now - Duration::hours(1)).await?;
+ let (listed, focus) = read_at(&repo, outside, now).await?;
+ assert!(!listed && !focus.is_focus);
+ assert_ne!(focus.category, FocusCategory::Calendar);
+ // Filed outside the signal inbox, the conversation is no key mail either.
+ assert_eq!(
+ low.classify_thread(outside).await?,
+ ClassifyOutcome::NotSignal
+ );
+
+ // In the signal inbox it takes the normal rule, whichever way that goes.
+ let signal = notice_thread(
+ &pool,
+ "Rescheduled: Demo",
+ Some(invite(InvitationMethod::Request, 1, end)),
+ at,
+ false,
+ )
+ .await?;
+ insert_message_from(&pool, signal, STRANGER, false, now - Duration::hours(1)).await?;
+ let ClassifyOutcome::Classified(verdict) = low.classify_thread(signal).await? else {
+ panic!("expected a classification");
+ };
+ assert!(!verdict.is_focus && verdict.calendar_hold_until.is_none());
+ let (listed, _) = read_at(&repo, signal, now).await?;
+ assert!(!listed);
+
+ // A hold already stored is gone with the reclassification, not just hidden.
+ let held = notice_thread(
+ &pool,
+ "Rescheduled: Demo",
+ Some(invite(InvitationMethod::Request, 1, end)),
+ at,
+ false,
+ )
+ .await?;
+ assert_held(&repo, held).await?;
+ insert_message_from(&pool, held, STRANGER, false, now - Duration::hours(1)).await?;
+ assert!(matches!(
+ low.classify_thread(held).await?,
+ ClassifyOutcome::Classified(_)
+ ));
+ let (listed, focus) = read_at(&repo, held, now).await?;
+ assert!(!listed && !focus.is_focus);
+
+ // New invites and RSVPs are no veto once someone has written after them.
+ for (subject, invite) in [
+ (
+ "Invitation: Demo",
+ Some(invite(InvitationMethod::Request, 0, end)),
+ ),
+ (
+ "Accepted: Demo",
+ Some(invite(InvitationMethod::Reply, 0, end)),
+ ),
+ ] {
+ let thread = notice_thread(&pool, subject, invite, at, false).await?;
+ insert_message_from(&pool, thread, STRANGER, false, now - Duration::hours(1)).await?;
+ let ClassifyOutcome::Classified(verdict) = high.classify_thread(thread).await? else {
+ panic!("expected a classification");
+ };
+ assert!(verdict.is_focus, "{subject}");
+ let (listed, focus) = read_at(&repo, thread, now).await?;
+ assert!(listed, "{subject}");
+ assert_eq!(focus.category, FocusCategory::Other, "{subject}");
+ }
+ Ok(())
+}
+
+#[sqlx::test(
+ migrator = "MACRO_DB_MIGRATIONS",
+ fixtures(
+ path = "../../../fixtures",
+ scripts("email_focus", "email_focus_waiting")
+ )
+)]
+async fn the_owner_answering_a_notice_ends_the_hold(pool: Pool<Postgres>) -> Result<(), Report> {
+ let repo = FocusPgRepository(pool.clone());
+ let now = Utc::now();
+ let end = now + Duration::days(20);
+ let thread = notice_thread(
+ &pool,
+ "Rescheduled: Demo",
+ Some(invite(InvitationMethod::Request, 1, end)),
+ now - Duration::days(6),
+ true,
+ )
+ .await?;
+ assert_held(&repo, thread).await?;
+
+ // Ahead of the worker, and whatever it later makes of the answer.
+ insert_message(&pool, thread, true, now - Duration::days(5)).await?;
+ let (listed, focus) = read_at(&repo, thread, now).await?;
+ assert!(!listed && !focus.is_focus);
+ Ok(())
+}
+
+#[sqlx::test(
+ migrator = "MACRO_DB_MIGRATIONS",
+ fixtures(
+ path = "../../../fixtures",
+ scripts("email_focus", "email_focus_waiting")
+ )
+)]
+async fn mail_from_a_recent_contact_is_found_and_pinned_outside_the_signal_inbox(
+ pool: Pool<Postgres>,
+) -> Result<(), Report> {
+ project_threads(&pool).await?;
+ let repo = FocusPgRepository(pool);
+ let now = Utc::now();
+ let since = now - Duration::days(30);
+
+ // No event covers mail from before an inbox was enabled: the sweep finds it.
+ let thread = load_thread(&repo, RECENT_CONTACT).await?.expect("thread");
+ assert!(!thread.is_signal);
+ let stale = repo
+ .stale_threads(since, &["acme.example".to_owned()], None, 100)
+ .await?;
+ assert!(stale.iter().any(|stale| stale.thread_id == RECENT_CONTACT));
+
+ let worker = worker(&repo, Ok(vec![0.05, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]));
+ let ClassifyOutcome::Classified(verdict) = worker.classify_thread(RECENT_CONTACT).await? else {
+ panic!("expected the recent contact's mail to be classified");
+ };
+ assert!(verdict.is_focus && verdict.pinned);
+ assert!(
+ repo.focus_thread_ids(&owner(), since, now, now, 100)
+ .await?
+ .contains(&RECENT_CONTACT)
+ );
+ Ok(())
+}
+
+#[sqlx::test(
+ migrator = "MACRO_DB_MIGRATIONS",
+ fixtures(path = "../../../fixtures", scripts("email_focus"))
+)]
+async fn a_thread_that_lost_signal_leaves_focus_unless_pinned(
+ pool: Pool<Postgres>,
+) -> Result<(), Report> {
+ let repo = FocusPgRepository(pool.clone());
+ let now = Utc::now();
+ let since = now - Duration::days(30);
+ repo.save(&record(INVOICE, LATEST_MESSAGE, now, 60)).await?;
+ sqlx::query!(
+ "UPDATE email_threads SET is_signal = false WHERE id = $1",
+ INVOICE
+ )
+ .execute(&pool)
+ .await?;
+ assert!(
+ !repo
+ .focus_thread_ids(&owner(), since, now, now, 10)
+ .await?
+ .contains(&INVOICE)
+ );
+
+ // The labels read the same as the list, so nothing reads as owed.
+ let focus = repo
+ .focus_for_threads(&owner(), &[INVOICE], now, now)
+ .await?;
+ let out = focus.get(&INVOICE).expect("stored");
+ assert!(!out.is_focus && !out.needs_reply && !out.needs_follow_up);
+
+ let mut pinned = record(INVOICE, LATEST_MESSAGE, now, 60);
+ pinned.verdict.pinned = true;
+ repo.save(&pinned).await?;
+ assert!(
+ repo.focus_thread_ids(&owner(), since, now, now, 10)
+ .await?
+ .contains(&INVOICE)
+ );
+ Ok(())
+}
+
+#[sqlx::test(
+ migrator = "MACRO_DB_MIGRATIONS",
+ fixtures(
+ path = "../../../fixtures",
+ scripts("email_focus", "email_focus_waiting")
+ )
+)]
+async fn a_follow_up_that_lost_signal_reads_as_out(pool: Pool<Postgres>) -> Result<(), Report> {
+ project_threads(&pool).await?;
+ let repo = FocusPgRepository(pool.clone());
+ let now = Utc::now();
+ let thread = load_thread(&repo, REPLIED_YESTERDAY)
+ .await?
+ .expect("thread");
+ let latest = thread.messages.last().expect("message");
+ let mut flagged = record(REPLIED_YESTERDAY, latest.id, latest.at, 60);
+ flagged.verdict.needs_reply = false;
+ flagged.verdict.needs_follow_up = true;
+ repo.save(&flagged).await?;
+ sqlx::query!(
+ "UPDATE email_threads SET is_signal = false WHERE id = $1",
+ REPLIED_YESTERDAY
+ )
+ .execute(&pool)
+ .await?;
+ let focus = repo
+ .focus_for_threads(&owner(), &[REPLIED_YESTERDAY], now - Duration::days(3), now)
+ .await?;
+ let out = focus.get(&REPLIED_YESTERDAY).expect("stored");
+ assert!(!out.is_focus && !out.needs_follow_up);
+ Ok(())
+}
+
+#[sqlx::test(
+ migrator = "MACRO_DB_MIGRATIONS",
+ fixtures(path = "../../../fixtures", scripts("email_focus"))
+)]
+async fn rows_from_an_older_rule_read_right_and_are_decided_again_without_the_classifier(
+ pool: Pool<Postgres>,
+) -> Result<(), Report> {
+ let repo = FocusPgRepository(pool.clone());
+ let now = Utc::now();
+ let since = now - Duration::days(30);
+ let enabled = vec!["acme.example".to_owned()];
+
+ // As the previous rule stored it: a follow-up on Casey's message, on answers
+ // that Jev also said need a reply.
+ let thread = load_thread(&repo, INVOICE).await?.expect("thread");
+ let latest = thread.messages.last().expect("message");
+ let scores = serde_json::to_value(FocusAnswers::from_probabilities(&[
+ 0.1, 0.0, 0.0, 0.0, 0.0, 0.0, 0.9, 0.9,
+ ]))?;
+ sqlx::query!(
+ r#"
+ INSERT INTO email_thread_focus (
+ thread_id, link_id, classified_message_id, classified_message_ts, is_focus,
+ category, importance, needs_reply, needs_follow_up, scores, model, classified_at
+ )
+ VALUES ($1, $2, $3, $4, true, 'known', 6, false, true, $5, 'jev-earlier', $6)
+ "#,
+ INVOICE,
+ OWNER_LINK,
+ latest.id,
+ latest.at,
+ scores,
+ now,
+ )
+ .execute(&pool)
+ .await?;
+
+ // Right away, with no worker: someone else wrote last, so it reads as a reply.
+ let focus = repo
+ .focus_for_threads(&owner(), &[INVOICE], now, now)
+ .await?;
+ let stored = focus.get(&INVOICE).expect("stored");
+ assert!(stored.needs_reply && !stored.needs_follow_up);
+
+ // The sweep finds it even though its latest message was classified.
+ let stale = repo.stale_threads(since, &enabled, None, 10).await?;
+ assert!(stale.iter().any(|stale| stale.thread_id == INVOICE));
+
+ // Failing the classifier proves it was never asked.
+ let worker = worker(&repo, Err(FocusClassifierError::Rejected));
+ let ClassifyOutcome::Redecided(verdict) = worker.classify_thread(INVOICE).await? else {
+ panic!("expected the row to be decided again");
+ };
+ assert!(verdict.is_focus && verdict.needs_reply && !verdict.needs_follow_up);
+ assert_eq!(verdict.importance, 70);
+
+ assert!(
+ repo.focus_thread_ids(&owner(), since, now, now, 10)
+ .await?
+ .contains(&INVOICE)
+ );
+ let classified = load_thread(&repo, INVOICE).await?.unwrap().classified;
+ let classified = classified.expect("stored");
+ assert_eq!(classified.rule_version, FOCUS_RULE_VERSION);
+ assert_eq!(classified.model, "jev-earlier");
+ let stale = repo.stale_threads(since, &enabled, None, 10).await?;
+ assert!(!stale.iter().any(|stale| stale.thread_id == INVOICE));
+ assert_eq!(
+ worker.classify_thread(INVOICE).await?,
+ ClassifyOutcome::UpToDate
+ );
+ Ok(())
+}
+
+#[sqlx::test(
+ migrator = "MACRO_DB_MIGRATIONS",
+ fixtures(
+ path = "../../../fixtures",
+ scripts("email_focus", "email_focus_waiting")
+ )
+)]
+async fn a_row_from_an_older_rule_is_swept_even_outside_the_signal_inbox(
+ pool: Pool<Postgres>,
+) -> Result<(), Report> {
+ project_threads(&pool).await?;
+ let repo = FocusPgRepository(pool.clone());
+ let enabled = vec!["acme.example".to_owned()];
+ let since = Utc::now() - Duration::days(30);
+ let thread = load_thread(&repo, NOISE).await?.expect("thread");
+ assert!(!thread.is_signal);
+ let latest = thread.messages.last().expect("message");
+ repo.save(&record(NOISE, latest.id, latest.at, 40)).await?;
+
+ // Under the current rule there is nothing to do for it.
+ let stale = repo.stale_threads(since, &enabled, None, 100).await?;
+ assert!(!stale.iter().any(|stale| stale.thread_id == NOISE));
+
+ sqlx::query!(
+ "UPDATE email_thread_focus SET rule_version = 0 WHERE thread_id = $1",
+ NOISE
+ )
+ .execute(&pool)
+ .await?;
+ let stale = repo.stale_threads(since, &enabled, None, 100).await?;
+ assert!(stale.iter().any(|stale| stale.thread_id == NOISE));
+ Ok(())
+}
+
+#[sqlx::test(
+ migrator = "MACRO_DB_MIGRATIONS",
+ fixtures(
+ path = "../../../fixtures",
+ scripts("email_focus", "email_focus_waiting")
+ )
+)]
+async fn editing_a_draft_never_changes_who_wrote_last(pool: Pool<Postgres>) -> Result<(), Report> {
+ project_threads(&pool).await?;
+ let repo = FocusPgRepository(pool.clone());
+ let now = Utc::now();
+ // Jev flagged an open loop on the owner's own message, inside the wait.
+ let thread = load_thread(&repo, REPLIED_YESTERDAY)
+ .await?
+ .expect("thread");
+ let latest = thread.messages.last().expect("message");
+ let mut flagged = record(REPLIED_YESTERDAY, latest.id, latest.at, 60);
+ flagged.verdict.needs_reply = false;
+ flagged.verdict.needs_follow_up = true;
+ repo.save(&flagged).await?;
+
+ // Editing a draft moves the thread's inbound time, as recompute_thread_metadata does.
+ sqlx::query!(
+ "UPDATE email_threads SET latest_inbound_message_ts = $1 WHERE id = $2",
+ now,
+ REPLIED_YESTERDAY,
+ )
+ .execute(&pool)
+ .await?;
+ let waiting_before = now - Duration::days(3);
+ let focus = repo
+ .focus_for_threads(&owner(), &[REPLIED_YESTERDAY], waiting_before, now)
+ .await?;
+ let drafting = focus.get(&REPLIED_YESTERDAY).expect("stored");
+ assert!(drafting.needs_follow_up && !drafting.needs_reply);
+
+ // Mail from someone else does change it.
+ insert_message(&pool, REPLIED_YESTERDAY, false, now).await?;
+ let focus = repo
+ .focus_for_threads(&owner(), &[REPLIED_YESTERDAY], waiting_before, now)
+ .await?;
+ let answered = focus.get(&REPLIED_YESTERDAY).expect("stored");
+ assert!(answered.needs_reply && !answered.needs_follow_up);
+ Ok(())
+}
diff --git a/crates/graphql_email/src/focus.rs b/crates/graphql_email/src/focus.rs
index 016739c807..51e5357048 100644
--- a/crates/graphql_email/src/focus.rs
+++ b/crates/graphql_email/src/focus.rs
@@ -17,7 +17,7 @@ const MAX_FOCUS_KEYS: usize = 500;
/// Focus reads on behalf of a viewer, limited to the viewer's own inboxes.
pub trait EmailFocusReader: Send + Sync + 'static {
/// The viewer's Focus threads with mail in the last `window_days`,
- /// highest priority first.
+ /// newest first by latest message time, in either direction.
fn focus_thread_ids<'a>(
&'a self,
user: &'a MacroUserIdStr<'static>,
@@ -66,7 +66,8 @@ pub enum GraphqlEmailFocusCategory {
Known,
/// Relevant mail that fits no narrower category.
Other,
- /// A calendar invite, update or RSVP.
+ /// In Focus, a reschedule, update, cancellation or proposed new time for an
+ /// event that isn't over. Left out, a new invite or an RSVP notice.
Calendar,
/// Unsolicited outreach: a pitch, recruiting or a job application.
Outreach,
@@ -99,11 +100,14 @@ pub struct GraphqlEmailThreadFocus {
is_focus: bool,
/// The Focus category, or why the thread was left out.
category: GraphqlEmailFocusCategory,
- /// Sort key for the Focus list, 0 to 100.
+ /// How much the thread matters, 0 to 100.
importance: i32,
- /// The latest message waits on the owner's answer.
+ /// Someone else sent the latest message and it waits on the owner's answer.
+ /// Never true together with `needs_follow_up`.
needs_reply: bool,
- /// An open loop the owner should come back to.
+ /// The owner sent the latest message and should come back to the thread: it
+ /// has an open loop or has waited for an answer. Never true together with
+ /// `needs_reply`.
needs_follow_up: bool,
/// When the classification was made, in RFC 3339 format.
classified_at: String,
@@ -172,8 +176,8 @@ pub async fn load_email_thread_focus(
.map_err(|_| async_graphql::Error::new("email thread focus is unavailable"))
}
-/// The viewer's Focus thread ids, highest priority first; empty when Focus is
-/// not served here.
+/// The viewer's Focus thread ids, newest first; empty when Focus is not served
+/// here.
pub async fn load_email_focus_thread_ids(
ctx: &Context<'_>,
user_id: &MacroUserIdStr<'static>,
diff --git a/crates/macro_db_client/migrations/20261104031220_email_thread_focus_read_time_columns.sql b/crates/macro_db_client/migrations/20261104031220_email_thread_focus_read_time_columns.sql
new file mode 100644
index 0000000000..8dd5709ffe
--- /dev/null
+++ b/crates/macro_db_client/migrations/20261104031220_email_thread_focus_read_time_columns.sql
@@ -0,0 +1,16 @@
+-- Focus rule facts the list reads, and the rule version that wrote them.
+-- rule_version: the worker re-decides rows from an older version out of their
+-- stored scores, without calling the classifier. Existing rows read 0.
+-- pinned: key mail that stays in Focus even when the thread is not signal.
+-- awaiting_reply: the owner sent the classified message and expects an
+-- answer. The list applies the age threshold when it reads.
+-- calendar_hold_until: the classified message is a change to an event, which
+-- holds the thread in Focus until this time. The list applies it when it reads.
+-- Constant defaults only change the catalog, so old writers keep working.
+SET LOCAL lock_timeout = '5s';
+
+ALTER TABLE email_thread_focus
+ ADD COLUMN rule_version SMALLINT NOT NULL DEFAULT 0,
+ ADD COLUMN pinned BOOLEAN NOT NULL DEFAULT FALSE,
+ ADD COLUMN awaiting_reply BOOLEAN NOT NULL DEFAULT FALSE,
+ ADD COLUMN calendar_hold_until TIMESTAMPTZ;
diff --git a/crates/macro_db_client/migrations/20261104031221_email_thread_focus_read_time_index.sql b/crates/macro_db_client/migrations/20261104031221_email_thread_focus_read_time_index.sql
new file mode 100644
index 0000000000..f7a3bcd81c
--- /dev/null
+++ b/crates/macro_db_client/migrations/20261104031221_email_thread_focus_read_time_index.sql
@@ -0,0 +1,6 @@
+-- no-transaction
+-- One inbox's threads that join or leave Focus when it is read. With
+-- email_thread_focus_list_idx this covers every way into the Focus list.
+CREATE INDEX CONCURRENTLY IF NOT EXISTS email_thread_focus_read_time_idx
+ ON email_thread_focus (link_id)
+ WHERE awaiting_reply OR calendar_hold_until IS NOT NULL;
diff --git a/docs/agent-guide/email.md b/docs/agent-guide/email.md
index 994fdad10a..4c26e9c3f5 100644
--- a/docs/agent-guide/email.md
+++ b/docs/agent-guide/email.md
@@ -161,11 +161,23 @@ changing the list query. Text search still resolves favorite IDs for the search
service. An empty favorites list shows `No favorite emails`.
With `enable-email-focus` and `enable-graphql-soup` on, a `Focus` tab leads the
-desktop Inbox tabs and the mobile pills. It lists signal
+desktop Inbox tabs and the mobile pills. It lists inbox
threads from the last 30 days that the email focus worker
(`services/email_focus_worker`) classified as worth attention, including inboxes
-linked to the account, by priority: the importance score halved for every 2 days
-since the thread's latest inbound mail. Rows at least 32rem wide show the 0-100
+linked to the account, newest first by latest message in either direction. Key
+mail stays in whatever its score: mail waiting on the owner's reply, and mail from
+someone the owner wrote to in the last 90 days, unless it reads as outreach or
+automated. A calendar change (a reschedule, update, cancellation or proposed new
+time) stays in until its event is over, or for 7 days after the notice when it is a
+recurring event or no time can be read, and is the only mail with the Calendar
+category in Focus. New invites and RSVP notices stay out. A calendar notice counts
+only as the thread's latest message: once anyone writes after it, the thread takes
+the same rules as any other. A thread the owner answered last joins 3 days after
+their message, unless that message only closes the exchange ("Thanks!"). `Follow up`
+marks only threads whose
+latest message is the owner's, and `Reply needed` only threads whose latest message
+is someone else's. A thread holding only the owner's sent mail is not listed: it
+has no inbox state, so Mark Done could not remove it. Rows at least 32rem wide show the 0-100
importance score with a five-bar meter just before the date, and from 56rem also
`Reply needed` and `Follow up` tag pills ahead of it. Narrower rows drop both;
without a touch screen the score moves before the subject, and touch screens
diff --git a/services/email_focus_worker/src/inbound/sweep.rs b/services/email_focus_worker/src/inbound/sweep.rs
index 1c664bdf0a..f5d115b618 100644
--- a/services/email_focus_worker/src/inbound/sweep.rs
+++ b/services/email_focus_worker/src/inbound/sweep.rs
@@ -1,10 +1,11 @@
-//! The hourly sweep: classify signal threads the event stream missed.
+//! The hourly sweep: classify threads the event stream missed.
//!
//! Events can be lost, a classification can fail, initial sync emits no
//! events, and a thread can become signal without one. The sweep covers all
//! of these. The first sweep runs at startup, which backfills a newly
-//! allowlisted inbox. Overlapping sweeps (during a deploy) are harmless: a
-//! stored result for a thread's latest message is never classified again.
+//! allowlisted inbox and decides rows from an older rule again, from their
+//! stored answers. Overlapping sweeps (during a deploy) are harmless: a stored
+//! result for a thread's latest message is never sent to the classifier again.
use std::{sync::Arc, time::Duration};
@@ -12,7 +13,7 @@ use email::domain::focus::{FocusClassifier, FocusService, FocusStore, ProfileSou
use rootcause::Report;
use tokio_util::sync::CancellationToken;
-/// Threads one sweep classifies at most; the next sweep continues.
+/// Threads one sweep checks at most; the next sweep continues past them.
const SWEEP_LIMIT: i64 = 2_000;
/// Sweep every `interval` over the last `window_days` until `shutdown`.
@@ -45,6 +46,7 @@ where
Ok(report) => tracing::info!(
candidates = report.candidates,
classified = report.classified,
+ redecided = report.redecided,
skipped = report.skipped,
failed = report.failed,
stopped_early = report.stopped_early,
diff --git a/services/email_focus_worker/src/main.rs b/services/email_focus_worker/src/main.rs
index 40acb130e2..6320041784 100644
--- a/services/email_focus_worker/src/main.rs
+++ b/services/email_focus_worker/src/main.rs
@@ -1,10 +1,10 @@
#![recursion_limit = "256"]
-//! Classifies signal email threads for the Focus view.
+//! Classifies email threads for the Focus view.
//!
-//! Reads `macro.email` for new and sent messages, asks Jev about each thread
-//! in an allowlisted inbox with the owner's stored memory as context, and
-//! stores the verdict in `email_thread_focus`. An hourly sweep catches what
-//! the event stream missed.
+//! Reads `macro.email` for new and sent messages, asks Jev about each signal
+//! thread and key mail in an allowlisted inbox with the owner's stored memory
+//! as context, and stores the verdict in `email_thread_focus`. An hourly sweep
+//! catches what the event stream missed.
mod config;
mod inbound {
diff --git a/static_assets/schema.graphql b/static_assets/schema.graphql
index 1eb502c326..ccc4a379d6 100644
--- a/static_assets/schema.graphql
+++ b/static_assets/schema.graphql
@@ -4423,7 +4423,8 @@ enum GraphqlEmailFocusCategory {
"""
OTHER
"""
- A calendar invite, update or RSVP.
+ In Focus, a reschedule, update, cancellation or proposed new time for an
+ event that isn't over. Left out, a new invite or an RSVP notice.
"""
CALENDAR
"""
@@ -4667,15 +4668,18 @@ type GraphqlEmailThreadFocus {
"""
category: GraphqlEmailFocusCategory!
"""
- Sort key for the Focus list, 0 to 100.
+ How much the thread matters, 0 to 100.
"""
importance: Int!
"""
- The latest message waits on the owner's answer.
+ Someone else sent the latest message and it waits on the owner's answer.
+ Never true together with `needs_follow_up`.
"""
needsReply: Boolean!
"""
- An open loop the owner should come back to.
+ The owner sent the latest message and should come back to the thread: it
+ has an open loop or has waited for an answer. Never true together with
+ `needs_reply`.
"""
needsFollowUp: Boolean!
"""
@@ -10237,10 +10241,10 @@ type GraphqlUser {
"""
emailThread(input: EmailThreadInput!): GraphqlSoupEmailThread
"""
- The viewer's Focus: signal threads in their own inboxes worth their
- attention, highest priority first (importance, halved for every 2 days
- since the latest inbound mail), with mail in the last `days` days
- (default 30, at most 90). Each thread's `focus` field explains why.
+ The viewer's Focus: threads in their own inboxes worth their attention,
+ newest first by latest message time in either direction, with mail in
+ the last `days` days (default 30, at most 90). Each thread carries a
+ `focus` field that explains why.
"""
emailFocus(days: Int): [GraphqlSoupEmailThread!]!
"""