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(github): push PR updates through Soup realtime (#7151)

Eric Hayesa6a87d693299
diff --git a/.github/kafka-cluster-topics.json b/.github/kafka-cluster-topics.json
index 4c5772b904..6167bb95fa 100644
--- a/.github/kafka-cluster-topics.json
+++ b/.github/kafka-cluster-topics.json
@@ -13,6 +13,7 @@
"macro.email",
"macro.example",
"macro.forms",
+ "macro.github_pull_requests",
"macro.initiatives",
"macro.mentions",
"macro.messages",
diff --git a/.github/workspace-dep-closures.json b/.github/workspace-dep-closures.json
index 18af6d4298..04ba532df0 100644
--- a/.github/workspace-dep-closures.json
+++ b/.github/workspace-dep-closures.json
@@ -6541,11 +6541,14 @@
"crates/harness_id",
"crates/harness_token",
"crates/item_filters",
+ "crates/kafka_util",
"crates/macro_auth",
"crates/macro_authorization",
"crates/macro_entrypoint",
"crates/macro_env",
"crates/macro_env_var",
+ "crates/macro_event_broker",
+ "crates/macro_event_topics",
"crates/macro_user_id",
"crates/macro_uuid",
"crates/model",
@@ -12074,6 +12077,7 @@
"crates/activity",
"crates/agent",
"crates/agent_runtime_protocol",
+ "crates/agent_session",
"crates/ai_billing",
"crates/ai_engine",
"crates/ai_toolset",
@@ -12083,6 +12087,7 @@
"crates/authentication_service_client",
"crates/bot_id",
"crates/bot_token",
+ "crates/bots",
"crates/broadcast",
"crates/calendar_events",
"crates/call",
diff --git a/.sqlx/query-154a7cdea2cba555530eaee8ffcd5d73916d500e67cb3afc048072d1b76a6951.json b/.sqlx/query-154a7cdea2cba555530eaee8ffcd5d73916d500e67cb3afc048072d1b76a6951.json
new file mode 100644
index 0000000000..74eceb78c7
--- /dev/null
+++ b/.sqlx/query-154a7cdea2cba555530eaee8ffcd5d73916d500e67cb3afc048072d1b76a6951.json
@@ -0,0 +1,15 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "INSERT INTO team_user (team_id, user_id, team_role) VALUES ($1, $2, 'member')",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Text"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "154a7cdea2cba555530eaee8ffcd5d73916d500e67cb3afc048072d1b76a6951"
+}
diff --git a/.sqlx/query-25fc7e8600d28e94dd5cc2a1422863f16730f8c494b8467d1c78a04ddf57d209.json b/.sqlx/query-25fc7e8600d28e94dd5cc2a1422863f16730f8c494b8467d1c78a04ddf57d209.json
new file mode 100644
index 0000000000..5c5f0a7eb8
--- /dev/null
+++ b/.sqlx/query-25fc7e8600d28e94dd5cc2a1422863f16730f8c494b8467d1c78a04ddf57d209.json
@@ -0,0 +1,76 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n WITH source_ids AS (\n SELECT DISTINCT stored_for_id, stored_for_auth_entity\n FROM UNNEST($1::text[], $2::text[])\n AS source_rows(stored_for_id, stored_for_auth_entity)\n ),\n deduped AS (\n SELECT DISTINCT ON (fe.foreign_entity_source, fe.foreign_entity_id,\n CASE WHEN $13::uuid[] IS NOT NULL THEN fe.id END)\n fe.id,\n fe.foreign_entity_id,\n fe.foreign_entity_source,\n fe.metadata,\n fe.stored_for_id,\n fe.stored_for_auth_entity,\n fe.created_at,\n COALESCE(gpr.github_updated_at, fe.updated_at) AS updated_at,\n CASE $3::text\n WHEN 'created_at' THEN fe.created_at\n ELSE COALESCE(gpr.github_updated_at, fe.updated_at)\n END AS sort_at\n FROM foreign_entity fe\n LEFT JOIN github_pull_request gpr ON gpr.github_key = fe.foreign_entity_id\n WHERE fe.foreign_entity_source = $10::text\n AND ($13::uuid[] IS NULL OR fe.id = ANY($13))\n AND EXISTS (\n SELECT 1\n FROM source_ids s\n WHERE s.stored_for_id = fe.stored_for_id\n AND s.stored_for_auth_entity = fe.stored_for_auth_entity\n )\n AND (\n $4::text IS NULL\n OR jsonb_path_match(\n jsonb_build_object(\n 'id', fe.id::text,\n 'foreignEntityId', fe.foreign_entity_id,\n 'foreignEntitySource', fe.foreign_entity_source\n ),\n ($4::text)::jsonpath\n )\n )\n AND (\n $11::text IS NULL\n OR (gpr.github_key IS NOT NULL AND jsonb_path_match(\n jsonb_build_object(\n 'repositoryId', gpr.repository_id,\n 'authorId', gpr.author_github_user_id,\n 'status', gpr.status,\n 'draft', gpr.draft,\n 'requestedReviewers', to_jsonb(gpr.requested_reviewer_github_user_ids),\n 'participants', to_jsonb(gpr.participant_github_user_ids),\n 'assignees', jsonb_path_query_array(gpr.assignees, '$[*].githubUserId'),\n 'labels', jsonb_path_query_array(gpr.labels, '$[*].name'),\n 'reviewers', jsonb_path_query_array(gpr.reviews, '$[*].reviewerGithubUserId'),\n 'reviewDecision', gpr.review_decision,\n 'reviewCount', jsonb_array_length(gpr.reviews)\n ),\n ($11::text)::jsonpath\n ))\n )\n AND (\n $8::int[] IS NULL\n OR ($9::text IS NOT NULL AND (\n SELECT COALESCE(bit_or(CASE un.state\n WHEN 'unseen' THEN 1 WHEN 'seen' THEN 2 WHEN 'done' THEN 4 END), 0)\n FROM notification n\n JOIN user_notification un ON un.notification_id = n.id\n WHERE un.user_id = $9::text\n AND un.deleted_at IS NULL\n AND n.event_item_type = 'foreign_entity'\n AND n.event_item_id = fe.id::text\n ) = ANY($8::int[]))\n )\n ORDER BY\n fe.foreign_entity_source,\n fe.foreign_entity_id,\n CASE WHEN $13::uuid[] IS NOT NULL THEN fe.id END,\n sort_at DESC,\n fe.updated_at DESC,\n fe.id DESC\n )\n SELECT\n id as \"id!: Uuid\",\n foreign_entity_id as \"foreign_entity_id!: String\",\n foreign_entity_source as \"foreign_entity_source!: String\",\n metadata as \"metadata!: serde_json::Value\",\n stored_for_id as \"stored_for_id!: String\",\n stored_for_auth_entity as \"stored_for_auth_entity!: String\",\n created_at as \"created_at!: DateTime<Utc>\",\n updated_at as \"updated_at!: DateTime<Utc>\"\n FROM deduped\n WHERE $5::timestamptz IS NULL\n OR ($12::bool AND (sort_at, id) > ($5::timestamptz, $6::uuid))\n OR (NOT $12::bool AND (sort_at, id) < ($5::timestamptz, $6::uuid))\n ORDER BY\n CASE WHEN $12::bool THEN sort_at END ASC,\n CASE WHEN $12::bool THEN id END ASC,\n sort_at DESC,\n id DESC\n LIMIT $7\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "id!: Uuid",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 1,
+ "name": "foreign_entity_id!: String",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 2,
+ "name": "foreign_entity_source!: String",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 3,
+ "name": "metadata!: serde_json::Value",
+ "type_info": "Jsonb"
+ },
+ {
+ "ordinal": 4,
+ "name": "stored_for_id!: String",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 5,
+ "name": "stored_for_auth_entity!: String",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 6,
+ "name": "created_at!: DateTime<Utc>",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 7,
+ "name": "updated_at!: DateTime<Utc>",
+ "type_info": "Timestamptz"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "TextArray",
+ "TextArray",
+ "Text",
+ "Text",
+ "Timestamptz",
+ "Uuid",
+ "Int8",
+ "Int4Array",
+ "Text",
+ "Text",
+ "Text",
+ "Bool",
+ "UuidArray"
+ ]
+ },
+ "nullable": [
+ false,
+ false,
+ false,
+ false,
+ false,
+ false,
+ false,
+ null
+ ]
+ },
+ "hash": "25fc7e8600d28e94dd5cc2a1422863f16730f8c494b8467d1c78a04ddf57d209"
+}
diff --git a/.sqlx/query-353955d82b1efe81b5b907607f1fcc440c1fc4d0e5de148cfbc2e9e9b0a5a495.json b/.sqlx/query-353955d82b1efe81b5b907607f1fcc440c1fc4d0e5de148cfbc2e9e9b0a5a495.json
deleted file mode 100644
index 02e045f122..0000000000
--- a/.sqlx/query-353955d82b1efe81b5b907607f1fcc440c1fc4d0e5de148cfbc2e9e9b0a5a495.json
+++ /dev/null
@@ -1,75 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n WITH source_ids AS (\n SELECT DISTINCT stored_for_id, stored_for_auth_entity\n FROM UNNEST($1::text[], $2::text[])\n AS source_rows(stored_for_id, stored_for_auth_entity)\n ),\n deduped AS (\n SELECT DISTINCT ON (fe.foreign_entity_source, fe.foreign_entity_id)\n fe.id,\n fe.foreign_entity_id,\n fe.foreign_entity_source,\n fe.metadata,\n fe.stored_for_id,\n fe.stored_for_auth_entity,\n fe.created_at,\n COALESCE(gpr.github_updated_at, fe.updated_at) AS updated_at,\n CASE $3::text\n WHEN 'created_at' THEN fe.created_at\n ELSE COALESCE(gpr.github_updated_at, fe.updated_at)\n END AS sort_at\n FROM foreign_entity fe\n LEFT JOIN github_pull_request gpr ON gpr.github_key = fe.foreign_entity_id\n WHERE fe.foreign_entity_source = $10::text\n AND EXISTS (\n SELECT 1\n FROM source_ids s\n WHERE s.stored_for_id = fe.stored_for_id\n AND s.stored_for_auth_entity = fe.stored_for_auth_entity\n )\n AND (\n $4::text IS NULL\n OR jsonb_path_match(\n jsonb_build_object(\n 'id', fe.id::text,\n 'foreignEntityId', fe.foreign_entity_id,\n 'foreignEntitySource', fe.foreign_entity_source\n ),\n ($4::text)::jsonpath\n )\n )\n AND (\n $11::text IS NULL\n OR (gpr.github_key IS NOT NULL AND jsonb_path_match(\n jsonb_build_object(\n 'repositoryId', gpr.repository_id,\n 'authorId', gpr.author_github_user_id,\n 'status', gpr.status,\n 'draft', gpr.draft,\n 'requestedReviewers', to_jsonb(gpr.requested_reviewer_github_user_ids),\n 'participants', to_jsonb(gpr.participant_github_user_ids),\n 'assignees', jsonb_path_query_array(gpr.assignees, '$[*].githubUserId'),\n 'labels', jsonb_path_query_array(gpr.labels, '$[*].name'),\n 'reviewers', jsonb_path_query_array(gpr.reviews, '$[*].reviewerGithubUserId'),\n 'reviewDecision', gpr.review_decision,\n 'reviewCount', jsonb_array_length(gpr.reviews)\n ),\n ($11::text)::jsonpath\n ))\n )\n AND (\n $8::int[] IS NULL\n OR ($9::text IS NOT NULL AND (\n SELECT COALESCE(bit_or(CASE un.state\n WHEN 'unseen' THEN 1 WHEN 'seen' THEN 2 WHEN 'done' THEN 4 END), 0)\n FROM notification n\n JOIN user_notification un ON un.notification_id = n.id\n WHERE un.user_id = $9::text\n AND un.deleted_at IS NULL\n AND n.event_item_type = 'foreign_entity'\n AND n.event_item_id = fe.id::text\n ) = ANY($8::int[]))\n )\n ORDER BY\n fe.foreign_entity_source,\n fe.foreign_entity_id,\n sort_at DESC,\n fe.updated_at DESC,\n fe.id DESC\n )\n SELECT\n id as \"id!: Uuid\",\n foreign_entity_id as \"foreign_entity_id!: String\",\n foreign_entity_source as \"foreign_entity_source!: String\",\n metadata as \"metadata!: serde_json::Value\",\n stored_for_id as \"stored_for_id!: String\",\n stored_for_auth_entity as \"stored_for_auth_entity!: String\",\n created_at as \"created_at!: DateTime<Utc>\",\n updated_at as \"updated_at!: DateTime<Utc>\"\n FROM deduped\n WHERE $5::timestamptz IS NULL\n OR ($12::bool AND (sort_at, id) > ($5::timestamptz, $6::uuid))\n OR (NOT $12::bool AND (sort_at, id) < ($5::timestamptz, $6::uuid))\n ORDER BY\n CASE WHEN $12::bool THEN sort_at END ASC,\n CASE WHEN $12::bool THEN id END ASC,\n sort_at DESC,\n id DESC\n LIMIT $7\n ",
- "describe": {
- "columns": [
- {
- "ordinal": 0,
- "name": "id!: Uuid",
- "type_info": "Uuid"
- },
- {
- "ordinal": 1,
- "name": "foreign_entity_id!: String",
- "type_info": "Text"
- },
- {
- "ordinal": 2,
- "name": "foreign_entity_source!: String",
- "type_info": "Text"
- },
- {
- "ordinal": 3,
- "name": "metadata!: serde_json::Value",
- "type_info": "Jsonb"
- },
- {
- "ordinal": 4,
- "name": "stored_for_id!: String",
- "type_info": "Text"
- },
- {
- "ordinal": 5,
- "name": "stored_for_auth_entity!: String",
- "type_info": "Text"
- },
- {
- "ordinal": 6,
- "name": "created_at!: DateTime<Utc>",
- "type_info": "Timestamptz"
- },
- {
- "ordinal": 7,
- "name": "updated_at!: DateTime<Utc>",
- "type_info": "Timestamptz"
- }
- ],
- "parameters": {
- "Left": [
- "TextArray",
- "TextArray",
- "Text",
- "Text",
- "Timestamptz",
- "Uuid",
- "Int8",
- "Int4Array",
- "Text",
- "Text",
- "Text",
- "Bool"
- ]
- },
- "nullable": [
- false,
- false,
- false,
- false,
- false,
- false,
- false,
- null
- ]
- },
- "hash": "353955d82b1efe81b5b907607f1fcc440c1fc4d0e5de148cfbc2e9e9b0a5a495"
-}
diff --git a/.sqlx/query-87c9c6d9ebaa0590c40c110c7d453c0faa191a341d516b008c794f3179849cff.json b/.sqlx/query-87c9c6d9ebaa0590c40c110c7d453c0faa191a341d516b008c794f3179849cff.json
new file mode 100644
index 0000000000..c17f81e267
--- /dev/null
+++ b/.sqlx/query-87c9c6d9ebaa0590c40c110c7d453c0faa191a341d516b008c794f3179849cff.json
@@ -0,0 +1,22 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n SELECT stored_for_id AS \"user_id!\" FROM foreign_entity\n WHERE id = $1 AND stored_for_auth_entity = 'user'\n UNION\n SELECT tu.user_id AS \"user_id!\"\n FROM foreign_entity fe\n JOIN team_user tu ON tu.team_id = CASE\n WHEN fe.stored_for_auth_entity = 'team' THEN fe.stored_for_id::uuid END\n WHERE fe.id = $1 AND fe.stored_for_auth_entity = 'team'\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "user_id!",
+ "type_info": "Text"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ null
+ ]
+ },
+ "hash": "87c9c6d9ebaa0590c40c110c7d453c0faa191a341d516b008c794f3179849cff"
+}
diff --git a/.sqlx/query-a47ae61afdc43746d37172689c2d7ba6c5a02d90dde6f19aef371ab1d6ef997c.json b/.sqlx/query-a47ae61afdc43746d37172689c2d7ba6c5a02d90dde6f19aef371ab1d6ef997c.json
new file mode 100644
index 0000000000..1a5cfeb467
--- /dev/null
+++ b/.sqlx/query-a47ae61afdc43746d37172689c2d7ba6c5a02d90dde6f19aef371ab1d6ef997c.json
@@ -0,0 +1,15 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "DELETE FROM team_user WHERE team_id = $1 AND user_id = $2",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Text"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "a47ae61afdc43746d37172689c2d7ba6c5a02d90dde6f19aef371ab1d6ef997c"
+}
diff --git a/.sqlx/query-bb0a0eadde401cbb1f34fa45b1806392cbbfd0c6cfd2ee247d7748059171d7ee.json b/.sqlx/query-bb0a0eadde401cbb1f34fa45b1806392cbbfd0c6cfd2ee247d7748059171d7ee.json
new file mode 100644
index 0000000000..2d270ad421
--- /dev/null
+++ b/.sqlx/query-bb0a0eadde401cbb1f34fa45b1806392cbbfd0c6cfd2ee247d7748059171d7ee.json
@@ -0,0 +1,16 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "INSERT INTO foreign_entity (id, foreign_entity_id, foreign_entity_source, metadata, stored_for_id, stored_for_auth_entity) VALUES ($1, 'macro/app/pull/7', 'github_pull_request', '{}', $2, $3)",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Text",
+ "Text"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "bb0a0eadde401cbb1f34fa45b1806392cbbfd0c6cfd2ee247d7748059171d7ee"
+}
diff --git a/Cargo.lock b/Cargo.lock
index 7682584bad..b91a11d032 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -8439,9 +8439,12 @@ dependencies = [
"item_filters",
"macro_authorization",
"macro_db_migrator",
+ "macro_event_broker",
+ "macro_event_topics",
"macro_user_id",
"model-error-response",
"models_pagination",
+ "rootcause",
"serde",
"serde_json",
"sqlx",
@@ -17994,6 +17997,7 @@ dependencies = [
name = "soup_realtime"
version = "0.1.0"
dependencies = [
+ "agent_session",
"broadcast",
"channels",
"chat",
@@ -18002,6 +18006,7 @@ dependencies = [
"email",
"entity_access",
"futures",
+ "github_pull_requests",
"initiative",
"kafka_util",
"macro_event_broker",
diff --git a/crates/complete_graph/src/schema/test/soup_patches.rs b/crates/complete_graph/src/schema/test/soup_patches.rs
index 6a7e965cd3..cbc0b729c4 100644
--- a/crates/complete_graph/src/schema/test/soup_patches.rs
+++ b/crates/complete_graph/src/schema/test/soup_patches.rs
@@ -9,7 +9,7 @@ use tracing::{
const PATCH_SELECTION: &str = r#"
__typename
- ... on SoupUpdated { item { __typename id displayName cacheProjection ... on GraphqlSoupChat { model } } }
+ ... on SoupUpdated { item { __typename id displayName cacheProjection ... on GraphqlSoupChat { model } ... on GraphqlSoupForeignEntity { sourceMetadata } ... on GraphqlSoupAgentSession { pullRequestState pullRequestId } } }
... on GraphqlCacheDeletion { graphqlTypeName entityId }
"#;
@@ -382,3 +382,77 @@ async fn initiative_patches_hydrate_and_delete_the_soup_record() {
assert_eq!(patches[1]["graphqlTypeName"], "GraphqlSoupInitiative");
assert_eq!(patches[1]["entityId"], removed_id.to_string());
}
+
+#[tokio::test]
+async fn pr_and_linked_session_updates_serialize_current_metadata() {
+ use models_soup::{
+ agent_session::{AgentPullRequestState, SoupAgentSession},
+ foreign_entity::SoupForeignEntity,
+ };
+ let pr_id = Uuid::now_v7();
+ let session_id = Uuid::now_v7();
+ let now = chrono::Utc::now();
+ let soup = CountingSoupService::default();
+ soup.set_raw_response(vec![
+ SoupItem::ForeignEntity(SoupForeignEntity {
+ id: pr_id,
+ foreign_entity_id: "macro/app/pull/7".into(),
+ foreign_entity_source: "github_pull_request".into(),
+ metadata: async_graphql::value!({"title": "Updated PR", "state": "merged"})
+ .into_json()
+ .unwrap(),
+ stored_for_id: VALID_USER_ID.into(),
+ stored_for_auth_entity: "user".into(),
+ created_at: now,
+ updated_at: now,
+ }),
+ SoupItem::AgentSession(SoupAgentSession {
+ id: session_id,
+ name: "Linked session".into(),
+ is_archived: false,
+ owner_id: model_owner::Owner::from_principal_str(VALID_USER_ID).unwrap(),
+ bot_id: Uuid::now_v7(),
+ harness: "claude".into(),
+ repo_url: None,
+ repo_branch: None,
+ pull_request_url: Some("https://github.com/macro/app/pull/7".into()),
+ working_branch: None,
+ pull_request_state: Some(AgentPullRequestState::Merged),
+ pull_request_id: Some(pr_id),
+ turn_state: None,
+ thread_id: None,
+ status: "disconnected".into(),
+ created_at: now,
+ updated_at: now,
+ viewed_at: None,
+ extra: (),
+ }),
+ ]);
+ let calls = soup.raw_calls.clone();
+ let responses = subscription_responses(
+ soup,
+ vec![
+ Patch::Updated(ModelEntityType::ForeignEntity.with_entity_string(pr_id.to_string())),
+ Patch::Updated(
+ ModelEntityType::AgentSession.with_entity_string(session_id.to_string()),
+ ),
+ ],
+ )
+ .await;
+ assert_eq!(responses.len(), 1);
+ assert!(responses[0].errors.is_empty(), "{:?}", responses[0].errors);
+ let data = responses[0].data.clone().into_json().unwrap();
+ let patches = data["soupUpdates"].as_array().unwrap();
+ let pr = patches
+ .iter()
+ .find(|p| p["item"]["id"] == pr_id.to_string())
+ .unwrap();
+ assert_eq!(pr["item"]["sourceMetadata"]["state"], "merged");
+ let session = patches
+ .iter()
+ .find(|p| p["item"]["id"] == session_id.to_string())
+ .unwrap();
+ assert_eq!(session["item"]["pullRequestState"], "MERGED");
+ assert_eq!(session["item"]["pullRequestId"], pr_id.to_string());
+ assert_eq!(calls.load(Ordering::SeqCst), 1);
+}
diff --git a/crates/entity_access/src/domain/service.rs b/crates/entity_access/src/domain/service.rs
index 2e4ea1ecff..448b62e84e 100644
--- a/crates/entity_access/src/domain/service.rs
+++ b/crates/entity_access/src/domain/service.rs
@@ -754,7 +754,8 @@ where
// A database's audience is exactly its `entity_access` rows, so it
// resolves the same way a document's does. So is a form's: a public
// audience is anyone with the link, which no list can name.
- EntityType::Document
+ EntityType::ForeignEntity
+ | EntityType::Document
| EntityType::Chat
| EntityType::Project
| EntityType::EmailThread
diff --git a/crates/entity_access/src/outbound/pg_access_repo/queries.rs b/crates/entity_access/src/outbound/pg_access_repo/queries.rs
index 8d749a2063..367b0d1bf4 100644
--- a/crates/entity_access/src/outbound/pg_access_repo/queries.rs
+++ b/crates/entity_access/src/outbound/pg_access_repo/queries.rs
@@ -161,6 +161,9 @@ pub(in crate::outbound::pg_access_repo) async fn get_entity_users(
entity_id: &uuid::Uuid,
entity_type: EntityType,
) -> anyhow::Result<Vec<MacroUserIdStr<'static>>> {
+ if entity_type == EntityType::ForeignEntity {
+ return foreign_entity_access::get_foreign_entity_users(pool, entity_id).await;
+ }
// because we don't store entity_access per email we need to also grab the owner
// of the email to append to the list, plus any primary that delegates the inbox
// via macro_user_links (shared inbox)
diff --git a/crates/entity_access/src/outbound/pg_access_repo/queries/foreign_entity_access.rs b/crates/entity_access/src/outbound/pg_access_repo/queries/foreign_entity_access.rs
index 0aa046cfd8..c29682da98 100644
--- a/crates/entity_access/src/outbound/pg_access_repo/queries/foreign_entity_access.rs
+++ b/crates/entity_access/src/outbound/pg_access_repo/queries/foreign_entity_access.rs
@@ -98,3 +98,35 @@ pub async fn list_foreign_entity_grants(
})
.collect())
}
+
+/// Current personal owner or team members of a foreign record.
+#[tracing::instrument(err, skip(pool))]
+pub async fn get_foreign_entity_users(
+ pool: &PgPool,
+ foreign_entity_id: &Uuid,
+) -> anyhow::Result<Vec<macro_user_id::user_id::MacroUserIdStr<'static>>> {
+ use macro_user_id::{cowlike::CowLike, user_id::MacroUserIdStr};
+ let users = sqlx::query_scalar!(
+ r#"
+ SELECT stored_for_id AS "user_id!" FROM foreign_entity
+ WHERE id = $1 AND stored_for_auth_entity = 'user'
+ UNION
+ SELECT tu.user_id AS "user_id!"
+ FROM foreign_entity fe
+ JOIN team_user tu ON tu.team_id = CASE
+ WHEN fe.stored_for_auth_entity = 'team' THEN fe.stored_for_id::uuid END
+ WHERE fe.id = $1 AND fe.stored_for_auth_entity = 'team'
+ "#,
+ foreign_entity_id,
+ )
+ .fetch_all(pool)
+ .await?;
+ users
+ .into_iter()
+ .map(|user| {
+ MacroUserIdStr::parse_from_str(&user)
+ .map(|id| id.into_owned())
+ .map_err(anyhow::Error::from)
+ })
+ .collect()
+}
diff --git a/crates/entity_access/src/outbound/pg_access_repo/queries/test.rs b/crates/entity_access/src/outbound/pg_access_repo/queries/test.rs
index 694bc018e5..dea73c1ec2 100644
--- a/crates/entity_access/src/outbound/pg_access_repo/queries/test.rs
+++ b/crates/entity_access/src/outbound/pg_access_repo/queries/test.rs
@@ -299,3 +299,57 @@ async fn get_entity_users_excludes_delegate_scoped_to_other_link(
);
Ok(())
}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn foreign_recipients_follow_exact_source_and_current_team_membership(
+ pool: PgPool,
+) -> anyhow::Result<()> {
+ insert_user(&pool, OWNER, "[email protected]").await;
+ insert_user(&pool, DELEGATE, "[email protected]").await;
+ let team = Uuid::now_v7();
+ insert_team(&pool, team).await;
+ sqlx::query!(
+ "INSERT INTO team_user (team_id, user_id, team_role) VALUES ($1, $2, 'member')",
+ team,
+ DELEGATE
+ )
+ .execute(&pool)
+ .await?;
+ let personal = Uuid::now_v7();
+ let shared = Uuid::now_v7();
+ for (id, source, kind) in [
+ (personal, OWNER.to_owned(), "user"),
+ (shared, team.to_string(), "team"),
+ ] {
+ sqlx::query!(
+ "INSERT INTO foreign_entity (id, foreign_entity_id, foreign_entity_source, metadata, stored_for_id, stored_for_auth_entity) VALUES ($1, 'macro/app/pull/7', 'github_pull_request', '{}', $2, $3)",
+ id, source, kind,
+ ).execute(&pool).await?;
+ }
+ assert_eq!(
+ get_entity_users(&pool, &personal, EntityType::ForeignEntity).await?,
+ vec![MacroUserIdStr::parse_from_str(OWNER)?.into_owned()]
+ );
+ assert_eq!(
+ get_entity_users(&pool, &shared, EntityType::ForeignEntity).await?,
+ vec![MacroUserIdStr::parse_from_str(DELEGATE)?.into_owned()]
+ );
+ sqlx::query!(
+ "DELETE FROM team_user WHERE team_id = $1 AND user_id = $2",
+ team,
+ DELEGATE
+ )
+ .execute(&pool)
+ .await?;
+ assert!(
+ get_entity_users(&pool, &shared, EntityType::ForeignEntity)
+ .await?
+ .is_empty()
+ );
+ assert!(
+ get_entity_users(&pool, &Uuid::now_v7(), EntityType::ForeignEntity)
+ .await?
+ .is_empty()
+ );
+ Ok(())
+}
diff --git a/crates/github_pull_requests/Cargo.toml b/crates/github_pull_requests/Cargo.toml
index 8455040853..6d859a5a58 100644
--- a/crates/github_pull_requests/Cargo.toml
+++ b/crates/github_pull_requests/Cargo.toml
@@ -37,9 +37,12 @@ foreign_entity = { path = "../foreign_entity", default-features = false }
git_patch = { path = "../git_patch" }
item_filters = { path = "../item_filters", optional = true }
macro_authorization = { path = "../macro_authorization", optional = true, default-features = false, features = ["axum"] }
+macro_event_broker = { path = "../macro_event_broker", default-features = false }
+macro_event_topics = { path = "../macro_event_topics" }
macro_user_id = { path = "../macro_user_id" }
model-error-response = { path = "../model-error-response", optional = true }
models_pagination = { path = "../models_pagination", optional = true }
+rootcause = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
sqlx = { workspace = true, optional = true }
diff --git a/crates/github_pull_requests/src/broker.rs b/crates/github_pull_requests/src/broker.rs
new file mode 100644
index 0000000000..35aaa2bbb1
--- /dev/null
+++ b/crates/github_pull_requests/src/broker.rs
@@ -0,0 +1,69 @@
+//! Broker envelopes and publication for persisted pull request updates.
+use crate::domain::events::GithubPullRequestUpdated;
+use macro_event_broker::{Event, MacroEvent, TopicEvent};
+use macro_event_topics::MacroGithubPullRequestsTopic;
+use serde::{Deserialize, Serialize};
+
+/// Facts published to the pull request topic.
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+#[serde(tag = "event_type", content = "metadata")]
+pub enum GithubPullRequestTopicEvent {
+ /// Stored PR metadata changed.
+ #[serde(rename = "github_pull_request.updated")]
+ Updated(GithubPullRequestUpdated),
+}
+impl TopicEvent for GithubPullRequestTopicEvent {
+ type Topic = MacroGithubPullRequestsTopic;
+ const SCHEMA_VERSION: u8 = 1;
+}
+/// An event keyed by PR so updates retain per-PR ordering.
+#[derive(Debug, Clone)]
+pub struct GithubPullRequestMacroEvent {
+ key: String,
+ event: Event<GithubPullRequestTopicEvent>,
+}
+impl GithubPullRequestMacroEvent {
+ /// Wrap a committed PR change.
+ pub fn updated(update: GithubPullRequestUpdated) -> Self {
+ Self {
+ key: update.github_key.to_lowercase(),
+ event: Event::new(GithubPullRequestTopicEvent::Updated(update)),
+ }
+ }
+}
+impl MacroEvent for GithubPullRequestMacroEvent {
+ type EventPayload = GithubPullRequestTopicEvent;
+ fn key(&self) -> &str {
+ &self.key
+ }
+ fn event(&self) -> &Event<Self::EventPayload> {
+ &self.event
+ }
+ fn from_event(key: String, event: Event<Self::EventPayload>) -> Self {
+ Self { key, event }
+ }
+}
+/// Publishes PR facts through the shared event broker.
+pub struct BrokerGithubPullRequestPublisher<B>(pub B);
+#[cfg(feature = "ports")]
+impl<B: macro_event_broker::MacroEventBroker> crate::domain::ports::GithubPullRequestEventPublisher
+ for BrokerGithubPullRequestPublisher<B>
+{
+ fn publish_updated(
+ &self,
+ update: GithubPullRequestUpdated,
+ ) -> std::pin::Pin<
+ Box<dyn std::future::Future<Output = Result<(), rootcause::Report>> + Send + '_>,
+ > {
+ Box::pin(async move {
+ use rootcause::prelude::ResultExt as _;
+ self.0
+ .send_event(&GithubPullRequestMacroEvent::updated(update))
+ .context("enqueueing PR update")?
+ .await
+ .context("joining PR publication")?
+ .context("publishing PR update")?;
+ Ok(())
+ })
+ }
+}
diff --git a/crates/github_pull_requests/src/domain.rs b/crates/github_pull_requests/src/domain.rs
index 6e34618b64..94aa6d459d 100644
--- a/crates/github_pull_requests/src/domain.rs
+++ b/crates/github_pull_requests/src/domain.rs
@@ -2,6 +2,9 @@
pub mod models;
+/// Committed pull request facts.
+pub mod events;
+
#[cfg(feature = "ports")]
pub mod ports;
diff --git a/crates/github_pull_requests/src/domain/events.rs b/crates/github_pull_requests/src/domain/events.rs
new file mode 100644
index 0000000000..2865fb745b
--- /dev/null
+++ b/crates/github_pull_requests/src/domain/events.rs
@@ -0,0 +1,12 @@
+//! Facts emitted after a pull request has been stored.
+use serde::{Deserialize, Serialize};
+use uuid::Uuid;
+
+/// A persisted update to a pull request and its source-scoped entity records.
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+pub struct GithubPullRequestUpdated {
+ /// The canonical `owner/repo/pull/number` key used by session links.
+ pub github_key: String,
+ /// Exact foreign entity identities affected by this write.
+ pub foreign_entity_ids: Vec<Uuid>,
+}
diff --git a/crates/github_pull_requests/src/domain/ports.rs b/crates/github_pull_requests/src/domain/ports.rs
index 3fa10eda85..c5961e06da 100644
--- a/crates/github_pull_requests/src/domain/ports.rs
+++ b/crates/github_pull_requests/src/domain/ports.rs
@@ -278,3 +278,12 @@ pub trait GithubPullRequestFacetRepository: Send + Sync + 'static {
source_ids: Vec<SourceId>,
) -> impl Future<Output = Result<GithubPullRequestFacets, Self::Err>> + Send;
}
+
+/// Publishes committed PR facts independently of the webhook transport.
+pub trait GithubPullRequestEventPublisher: Send + Sync + 'static {
+ /// Await publication; an error must not be mistaken for a failed database write.
+ fn publish_updated(
+ &self,
+ update: super::events::GithubPullRequestUpdated,
+ ) -> std::pin::Pin<Box<dyn Future<Output = Result<(), rootcause::Report>> + Send + '_>>;
+}
diff --git a/crates/github_pull_requests/src/domain/service.rs b/crates/github_pull_requests/src/domain/service.rs
index 7cc48a59d5..8c383459a4 100644
--- a/crates/github_pull_requests/src/domain/service.rs
+++ b/crates/github_pull_requests/src/domain/service.rs
@@ -8,6 +8,9 @@ mod index;
pub use changes::{GithubPullRequestChangesServiceImpl, GithubPullRequestChangesetStore};
+use std::sync::Arc;
+use uuid::Uuid;
+
use entity_access::domain::models::{
EntityAccessReceipt, EntityType, MemberTeamRole, ViewAccessLevel,
};
@@ -19,6 +22,7 @@ use item_filters::ast::{LiteralTree, github_pull_request::GithubPullRequestLiter
use macro_user_id::user_id::MacroUserIdStr;
use super::{
+ events::GithubPullRequestUpdated,
models::{
EnrichedGithubPullRequest, GITHUB_PULL_REQUEST_FOREIGN_ENTITY_SOURCE,
GithubPullRequestError, GithubPullRequestFacets, GithubPullRequestRow,
@@ -26,7 +30,8 @@ use super::{
StoredGithubPullRequest, UpsertGithubPullRequest, UpsertedGithubPullRequest,
},
ports::{
- GithubPullRequestFacetRepository, GithubPullRequestFacetService, GithubPullRequestListing,
+ GithubPullRequestEventPublisher, GithubPullRequestFacetRepository,
+ GithubPullRequestFacetService, GithubPullRequestListing,
GithubPullRequestListingRepository, GithubPullRequestRepository, GithubPullRequestService,
},
};
@@ -35,6 +40,7 @@ use super::{
pub struct GithubPullRequestServiceImpl<F, R> {
foreign_entity_service: F,
repo: R,
+ events: Option<Arc<dyn GithubPullRequestEventPublisher>>,
}
impl<F: ForeignEntityService, R: GithubPullRequestRepository> GithubPullRequestServiceImpl<F, R> {
@@ -44,6 +50,28 @@ impl<F: ForeignEntityService, R: GithubPullRequestRepository> GithubPullRequestS
Self {
foreign_entity_service,
repo,
+ events: None,
+ }
+ }
+
+ /// Enable committed change publication. Read-only service instances need no publisher.
+ pub fn with_event_publisher(mut self, publisher: impl GithubPullRequestEventPublisher) -> Self {
+ self.events = Some(Arc::new(publisher));
+ self
+ }
+
+ async fn publish_updated(&self, github_key: &str, foreign_entity_ids: Vec<Uuid>) {
+ if foreign_entity_ids.is_empty() {
+ return;
+ }
+ if let Some(events) = &self.events {
+ let update = GithubPullRequestUpdated {
+ github_key: github_key.to_owned(),
+ foreign_entity_ids,
+ };
+ let _ = events.publish_updated(update).await.inspect_err(|error| {
+ tracing::error!(error=?error, github_key, "failed to publish committed PR update");
+ });
}
}
@@ -176,6 +204,7 @@ impl<F: ForeignEntityService, R: GithubPullRequestRepository> GithubPullRequestS
.or_else(|| records.first())
.map(|record| &record.metadata);
let metadata = pull_request.foreign_entity_metadata(existing_metadata)?;
+ let changed = existing.is_none_or(|record| record.metadata != metadata);
let participant_github_user_ids = participant_github_user_ids(&metadata);
let foreign_entity = match existing {
@@ -205,6 +234,10 @@ impl<F: ForeignEntityService, R: GithubPullRequestRepository> GithubPullRequestS
};
records.push(foreign_entity.clone());
self.store_row(&pull_request, &records).await;
+ if changed {
+ self.publish_updated(&pull_request.github_key, vec![foreign_entity.id])
+ .await;
+ }
Ok(UpsertedGithubPullRequest {
foreign_entity,
@@ -223,11 +256,15 @@ impl<F: ForeignEntityService, R: GithubPullRequestRepository> GithubPullRequestS
pull_request: &EnrichedGithubPullRequest,
) -> Result<Vec<ForeignEntity>, GithubPullRequestError> {
let mut refreshed = Vec::new();
+ let mut changed = false;
let mut first_error = None;
let mut records = self.stored_records(&pull_request.github_key).await?;
for record in &records {
match self.refresh_record(pull_request, record).await {
- Ok(record) => refreshed.push(record),
+ Ok(refreshed_record) => {
+ changed |= refreshed_record.metadata != record.metadata;
+ refreshed.push(refreshed_record);
+ }
Err(error) => {
tracing::error!(
error=?error,
@@ -249,6 +286,15 @@ impl<F: ForeignEntityService, R: GithubPullRequestRepository> GithubPullRequestS
records.extend(refreshed.iter().cloned());
self.store_row(pull_request, &records).await;
}
+ if changed {
+ // The typed row is shared by every source. Include records already refreshed by
+ // an earlier partial attempt, since that attempt could not publish the shared row.
+ self.publish_updated(
+ &pull_request.github_key,
+ refreshed.iter().map(|record| record.id).collect(),
+ )
+ .await;
+ }
Ok(refreshed)
}
diff --git a/crates/github_pull_requests/src/domain/service/test.rs b/crates/github_pull_requests/src/domain/service/test.rs
index 917afae144..a352ee0ff7 100644
--- a/crates/github_pull_requests/src/domain/service/test.rs
+++ b/crates/github_pull_requests/src/domain/service/test.rs
@@ -1,3 +1,4 @@
+mod events;
mod index;
mod initialization;
mod refresh;
diff --git a/crates/github_pull_requests/src/domain/service/test/events.rs b/crates/github_pull_requests/src/domain/service/test/events.rs
new file mode 100644
index 0000000000..07c02d7be5
--- /dev/null
+++ b/crates/github_pull_requests/src/domain/service/test/events.rs
@@ -0,0 +1,158 @@
+use super::*;
+
+#[derive(Clone)]
+struct RecordingPublisher {
+ events: Arc<Mutex<Vec<crate::domain::events::GithubPullRequestUpdated>>>,
+ rows: StubPullRequestRows,
+ fail: bool,
+}
+impl crate::domain::ports::GithubPullRequestEventPublisher for RecordingPublisher {
+ fn publish_updated(
+ &self,
+ update: crate::domain::events::GithubPullRequestUpdated,
+ ) -> std::pin::Pin<Box<dyn Future<Output = Result<(), rootcause::Report>> + Send + '_>> {
+ Box::pin(async move {
+ assert!(
+ self.rows
+ .rows()
+ .iter()
+ .any(|row| row.github_key == update.github_key),
+ "publish only after typed row persistence"
+ );
+ self.events.lock().unwrap().push(update);
+ if self.fail {
+ Err(rootcause::report!("broker unavailable"))
+ } else {
+ Ok(())
+ }
+ })
+ }
+}
+
+#[tokio::test]
+async fn publishes_only_changed_records_after_storage_and_coalesces_refresh() {
+ let foreign = StubForeignEntityService::default();
+ let rows = StubPullRequestRows::default();
+ let events = Arc::new(Mutex::new(Vec::new()));
+ let service = service(&foreign, &rows).with_event_publisher(RecordingPublisher {
+ events: events.clone(),
+ rows: rows.clone(),
+ fail: false,
+ });
+ let open = pull_request(GithubPullRequestStatus::Open);
+ let personal = service
+ .upsert_pull_request(UpsertGithubPullRequest {
+ pull_request: open.clone(),
+ stored_for: user(),
+ })
+ .await
+ .unwrap();
+ let shared = service
+ .upsert_pull_request(UpsertGithubPullRequest {
+ pull_request: open.clone(),
+ stored_for: team(),
+ })
+ .await
+ .unwrap();
+ assert_eq!(events.lock().unwrap().len(), 2);
+ service
+ .upsert_pull_request(UpsertGithubPullRequest {
+ pull_request: open.clone(),
+ stored_for: user(),
+ })
+ .await
+ .unwrap();
+ service.refresh_pull_request(&open).await.unwrap();
+ assert_eq!(
+ events.lock().unwrap().len(),
+ 2,
+ "unchanged enrichments must not generate traffic"
+ );
+
+ service
+ .refresh_pull_request(&pull_request(GithubPullRequestStatus::Merged))
+ .await
+ .unwrap();
+ let events = events.lock().unwrap();
+ assert_eq!(events.len(), 3, "one fact per refresh, not per source");
+ assert_eq!(events[2].github_key, GITHUB_KEY);
+ assert_eq!(
+ events[2].foreign_entity_ids,
+ vec![personal.foreign_entity.id, shared.foreign_entity.id]
+ );
+}
+
+#[tokio::test]
+async fn successful_retry_publishes_records_committed_by_a_partial_refresh() {
+ for failed_record_first in [true, false] {
+ let open = pull_request(GithubPullRequestStatus::Open)
+ .foreign_entity_metadata(None)
+ .unwrap();
+ let failed = stored_record(&team(), open.clone());
+ let successful = stored_record(&user(), open.clone());
+ let records = if failed_record_first {
+ vec![failed.clone(), successful.clone()]
+ } else {
+ vec![successful.clone(), failed.clone()]
+ };
+ let expected_ids = records.iter().map(|record| record.id).collect::<Vec<_>>();
+ let foreign = StubForeignEntityService::with_records(records);
+ foreign.state.lock().unwrap().failing_patches = vec![failed.id];
+ let rows = StubPullRequestRows::default();
+ let original_row = GithubPullRequestRow::from_metadata(&open).unwrap();
+ rows.rows.lock().unwrap().push(original_row.clone());
+ let events = Arc::new(Mutex::new(Vec::new()));
+ let service = service(&foreign, &rows).with_event_publisher(RecordingPublisher {
+ events: events.clone(),
+ rows: rows.clone(),
+ fail: false,
+ });
+ let closed = pull_request(GithubPullRequestStatus::Closed);
+
+ assert!(service.refresh_pull_request(&closed).await.is_err());
+ assert_eq!(foreign.patches().len(), 2);
+ assert_eq!(rows.rows(), vec![original_row]);
+ assert!(events.lock().unwrap().is_empty());
+ assert_eq!(
+ foreign
+ .records()
+ .iter()
+ .find(|record| record.id == successful.id)
+ .unwrap()
+ .metadata["status"],
+ "closed"
+ );
+
+ foreign.state.lock().unwrap().failing_patches.clear();
+ service.refresh_pull_request(&closed).await.unwrap();
+ assert_eq!(rows.rows()[0].status, Some(GithubPullRequestStatus::Closed));
+ {
+ let events = events.lock().unwrap();
+ assert_eq!(events.len(), 1);
+ assert_eq!(events[0].github_key, GITHUB_KEY);
+ assert_eq!(events[0].foreign_entity_ids, expected_ids);
+ }
+
+ service.refresh_pull_request(&closed).await.unwrap();
+ assert_eq!(events.lock().unwrap().len(), 1);
+ }
+}
+
+#[tokio::test]
+async fn failed_publication_does_not_report_a_committed_write_as_failed() {
+ let foreign = StubForeignEntityService::default();
+ let rows = StubPullRequestRows::default();
+ let service = service(&foreign, &rows).with_event_publisher(RecordingPublisher {
+ events: Arc::default(),
+ rows: rows.clone(),
+ fail: true,
+ });
+ let saved = service
+ .upsert_pull_request(UpsertGithubPullRequest {
+ pull_request: pull_request(GithubPullRequestStatus::Open),
+ stored_for: user(),
+ })
+ .await
+ .unwrap();
+ assert_eq!(foreign.records()[0].id, saved.foreign_entity.id);
+}
diff --git a/crates/github_pull_requests/src/lib.rs b/crates/github_pull_requests/src/lib.rs
index 68f976853a..62235f28d8 100644
--- a/crates/github_pull_requests/src/lib.rs
+++ b/crates/github_pull_requests/src/lib.rs
@@ -12,6 +12,7 @@
#![deny(missing_docs)]
+pub mod broker;
pub mod domain;
#[cfg(feature = "inbound")]
diff --git a/crates/github_pull_requests/src/outbound/pg_github_pull_request_repo/listing.rs b/crates/github_pull_requests/src/outbound/pg_github_pull_request_repo/listing.rs
index 9cd07e6a20..cca3e2114f 100644
--- a/crates/github_pull_requests/src/outbound/pg_github_pull_request_repo/listing.rs
+++ b/crates/github_pull_requests/src/outbound/pg_github_pull_request_repo/listing.rs
@@ -46,6 +46,22 @@ struct ListingQuery<'a> {
github_pull_request_jsonpath: Option<&'a str>,
/// Smallest sort value first, with the cursor paging toward larger values.
ascending: bool,
+ /// Exact hydration preserves source-scoped IDs instead of deduplicating by GitHub key.
+ entity_ids: Option<&'a [Uuid]>,
+}
+
+/// Only positive ID disjunctions denote an exact hydration request. Broader filters
+/// retain the normal one-row-per-PR listing semantics.
+fn exact_entity_ids(expr: &Expr<ForeignEntityLiteral>) -> Option<Vec<Uuid>> {
+ match expr {
+ Expr::Literal(ForeignEntityLiteral::Id(id)) => Some(vec![*id]),
+ Expr::Or(left, right) => {
+ let mut ids = exact_entity_ids(left)?;
+ ids.extend(exact_entity_ids(right)?);
+ Some(ids)
+ }
+ _ => None,
+ }
}
fn source_id_parts(source_ids: &[SourceId]) -> (Vec<String>, Vec<String>) {
@@ -285,6 +301,7 @@ impl PgGithubPullRequestRepo {
limit,
github_pull_request_jsonpath,
ascending,
+ entity_ids,
} = query;
let sort_method = sort_method.to_string();
@@ -297,7 +314,8 @@ impl PgGithubPullRequestRepo {
AS source_rows(stored_for_id, stored_for_auth_entity)
),
deduped AS (
- SELECT DISTINCT ON (fe.foreign_entity_source, fe.foreign_entity_id)
+ SELECT DISTINCT ON (fe.foreign_entity_source, fe.foreign_entity_id,
+ CASE WHEN $13::uuid[] IS NOT NULL THEN fe.id END)
fe.id,
fe.foreign_entity_id,
fe.foreign_entity_source,
@@ -313,6 +331,7 @@ impl PgGithubPullRequestRepo {
FROM foreign_entity fe
LEFT JOIN github_pull_request gpr ON gpr.github_key = fe.foreign_entity_id
WHERE fe.foreign_entity_source = $10::text
+ AND ($13::uuid[] IS NULL OR fe.id = ANY($13))
AND EXISTS (
SELECT 1
FROM source_ids s
@@ -365,6 +384,7 @@ impl PgGithubPullRequestRepo {
ORDER BY
fe.foreign_entity_source,
fe.foreign_entity_id,
+ CASE WHEN $13::uuid[] IS NOT NULL THEN fe.id END,
sort_at DESC,
fe.updated_at DESC,
fe.id DESC
@@ -401,6 +421,7 @@ impl PgGithubPullRequestRepo {
GITHUB_PULL_REQUEST_FOREIGN_ENTITY_SOURCE,
github_pull_request_jsonpath,
ascending,
+ entity_ids,
)
.fetch_all(&self.pool)
.await
@@ -440,6 +461,7 @@ impl GithubPullRequestListingRepository for PgGithubPullRequestRepo {
return Ok(Vec::new());
}
+ let entity_ids = query.filter().as_deref().and_then(exact_entity_ids);
let HoistedForeignEntityFilters {
includes_me,
notification_matches,
@@ -509,7 +531,11 @@ impl GithubPullRequestListingRepository for PgGithubPullRequestRepo {
source_ids: &source_ids,
source_auth_entities: &source_auth_entities,
sort_method: *query.sort_method(),
- filter_jsonpath: filter_jsonpath.as_deref(),
+ filter_jsonpath: if entity_ids.is_some() {
+ None
+ } else {
+ filter_jsonpath.as_deref()
+ },
notification_user_id,
notification_sets: notification_sets.as_deref(),
cursor_id: cursor_id.copied(),
@@ -517,6 +543,7 @@ impl GithubPullRequestListingRepository for PgGithubPullRequestRepo {
limit: limit as i64,
github_pull_request_jsonpath: github_pull_request_jsonpath.as_deref(),
ascending: sort_direction == GithubPullRequestSortDirection::Asc,
+ entity_ids: entity_ids.as_deref(),
})
.await
}
diff --git a/crates/github_pull_requests/src/outbound/pg_github_pull_request_repo/listing/test.rs b/crates/github_pull_requests/src/outbound/pg_github_pull_request_repo/listing/test.rs
index 1144eceb21..e4abb7181f 100644
--- a/crates/github_pull_requests/src/outbound/pg_github_pull_request_repo/listing/test.rs
+++ b/crates/github_pull_requests/src/outbound/pg_github_pull_request_repo/listing/test.rs
@@ -1553,3 +1553,59 @@ async fn list_updated_sort_uses_githubs_updated_at_when_known(pool: PgPool) {
"the pull request reports GitHub's updated time"
);
}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn exact_ids_preserve_both_pr_sources_and_enforce_visibility(pool: PgPool) {
+ let records = PgForeignEntityRepo::new(pool.clone());
+ let listing = PgGithubPullRequestRepo::new(pool);
+ let team_id = Uuid::now_v7();
+ let personal = insert_foreign_entity_for_source(
+ &records,
+ "macro/app/pull/7",
+ "github_pull_request",
+ "user",
+ )
+ .await;
+ let team = insert_foreign_entity_for_source(
+ &records,
+ "macro/app/pull/7",
+ "github_pull_request",
+ &team_id.to_string(),
+ "team",
+ )
+ .await;
+ let filter = Some(Arc::new(Expr::or(
+ Expr::val(ForeignEntityLiteral::Id(personal.id)),
+ Expr::val(ForeignEntityLiteral::Id(team.id)),
+ )));
+ let both = listing
+ .list_pull_requests(
+ None,
+ vec![
+ SourceId::user("macro|[email protected]"),
+ SourceId::team(team_id),
+ ],
+ 10,
+ filter_query(filter.clone()),
+ None,
+ GithubPullRequestSortDirection::Desc,
+ )
+ .await
+ .unwrap();
+ assert_eq!(both.len(), 2);
+ assert!(ids(&both).contains(&personal.id));
+ assert!(ids(&both).contains(&team.id));
+ let personal_only = listing
+ .list_pull_requests(
+ None,
+ vec![SourceId::user("macro|[email protected]")],
+ 10,
+ filter_query(filter),
+ None,
+ GithubPullRequestSortDirection::Desc,
+ )
+ .await
+ .unwrap();
+ assert_eq!(ids(&personal_only), vec![personal.id]);
+}
diff --git a/crates/graphql_soup/src/lib.rs b/crates/graphql_soup/src/lib.rs
index 872c874675..b5a75571ad 100644
--- a/crates/graphql_soup/src/lib.rs
+++ b/crates/graphql_soup/src/lib.rs
@@ -22,6 +22,7 @@ pub use inputs::{GraphqlSimpleSortMethod, GroupedSoupInput, SoupInput};
pub use loaders::{
AgentSessionEntityLoader, EmailServiceInboxReader, SoupInboxReader, SoupItemDataLoader,
SoupItemLoader, SoupItemLoaderError, SoupItemLoaderKey, soup_item_loader,
+ soup_item_loader_with_team_access,
};
pub use mutation_thread::{
EmailMutationThreadLoader, EmailMutationThreadReader, email_mutation_thread_loader,
diff --git a/crates/graphql_soup/src/loaders.rs b/crates/graphql_soup/src/loaders.rs
index 5c87e359e8..ea56bd0e2e 100644
--- a/crates/graphql_soup/src/loaders.rs
+++ b/crates/graphql_soup/src/loaders.rs
@@ -14,6 +14,10 @@ use email::domain::{
models::{PreviewView, PreviewViewStandardLabel},
ports::EmailService,
};
+use entity_access::domain::{
+ models::{EntityAccessReceipt, MemberTeamRole},
+ ports::EntityAccessService,
+};
use filter_ast::Expr;
use futures::{future::BoxFuture, future::try_join_all};
use item_filters::{
@@ -95,12 +99,54 @@ where
}
}
+/// Resolves a viewer's current team capability for source-scoped PR hydration.
+pub trait SoupTeamReader: Send + Sync + 'static {
+ /// Recheck membership on each batch, so a long-lived socket cannot retain removed access.
+ fn team_receipt(
+ &self,
+ user: MacroUserIdStr<'static>,
+ ) -> BoxFuture<'_, Result<Option<EntityAccessReceipt<MemberTeamRole>>, SoupItemLoaderError>>;
+}
+
+/// Reads current team capabilities through the entity access service.
+struct EntityAccessTeamReader<A>(A);
+impl<A: EntityAccessService> SoupTeamReader for EntityAccessTeamReader<A> {
+ fn team_receipt(
+ &self,
+ user: MacroUserIdStr<'static>,
+ ) -> BoxFuture<'_, Result<Option<EntityAccessReceipt<MemberTeamRole>>, SoupItemLoaderError>>
+ {
+ Box::pin(async move {
+ let team = self
+ .0
+ .get_user_team(&user)
+ .await
+ .map_err(|error| rootcause::report!(error).into_dynamic().into_cloneable())?;
+ let Some(team) = team else {
+ return Ok(None);
+ };
+ self.0
+ .generate_entity_access_receipt::<MemberTeamRole>(
+ &user,
+ None,
+ &team.team_id.to_string(),
+ EntityType::Team,
+ )
+ .await
+ .map(Some)
+ .map_err(|error| rootcause::report!(error).into_dynamic().into_cloneable())
+ })
+ }
+}
+
/// Batches lightweight entity patches into one filtered Soup request per user.
pub struct SoupItemLoader<S, I> {
/// Existing Soup query service.
soup_service: S,
/// Reader used only when a batch requests email threads.
inbox_reader: I,
+ /// Optional reader for current team access during PR and session hydration.
+ team_reader: Option<Arc<dyn SoupTeamReader>>,
}
impl<S, I> SoupItemLoader<S, I> {
@@ -109,6 +155,7 @@ impl<S, I> SoupItemLoader<S, I> {
Self {
soup_service,
inbox_reader,
+ team_reader: None,
}
}
}
@@ -133,6 +180,16 @@ where
} else {
Vec::new()
};
+ let needs_team = entities.iter().any(|entity| {
+ matches!(
+ entity.entity_type,
+ EntityType::ForeignEntity | EntityType::AgentSession
+ )
+ });
+ let team = match (&self.team_reader, needs_team) {
+ (Some(reader), true) => reader.team_receipt(user_id.clone()).await?,
+ _ => None,
+ };
let filter = entity_filter_ast(&entities)?;
let limit = u16::try_from(entities.len())
.unwrap_or(MAX_BATCH_SIZE as u16)
@@ -150,7 +207,7 @@ where
let items = self
.soup_service
- .get_user_soup_with_projection(request, None)
+ .get_user_soup_with_projection(request, team)
.await
.map_err(|error| rootcause::report!(error).into_dynamic().into_cloneable())?
.into_items();
@@ -329,6 +386,22 @@ where
))
}
+/// Build a loader that rechecks team access for PRs and linked session metadata.
+pub fn soup_item_loader_with_team_access<S, E, A>(
+ soup_service: S,
+ email_service: Arc<E>,
+ access: A,
+) -> SoupItemDataLoader
+where
+ S: SoupService,
+ E: EmailService,
+ A: EntityAccessService,
+{
+ let mut loader = SoupItemLoader::new(soup_service, EmailServiceInboxReader::new(email_service));
+ loader.team_reader = Some(Arc::new(EntityAccessTeamReader(access)));
+ SoupItemDataLoader::new(loader)
+}
+
/// Build an OR tree from literals, falling back to an impossible literal when empty.
fn literal_tree<T>(literals: Vec<T>, impossible: T) -> Arc<Expr<T>> {
Arc::new(
diff --git a/crates/graphql_soup/src/loaders/test.rs b/crates/graphql_soup/src/loaders/test.rs
index 33d6e8194b..856ab31011 100644
--- a/crates/graphql_soup/src/loaders/test.rs
+++ b/crates/graphql_soup/src/loaders/test.rs
@@ -41,6 +41,7 @@ struct RecordedRequest {
limit: u16,
/// Inbox IDs attached to the request.
link_ids: Vec<Uuid>,
+ team_id: Option<String>,
}
/// Soup service that records raw requests and returns configured items by user.
@@ -73,7 +74,6 @@ impl SoupService for RecordingSoupService {
SoupRequest<T>: IntoSoupReqAst,
T: Clone + serde::Serialize + Send,
{
- assert!(team_receipt.is_none());
let items = self
.responses
.lock()
@@ -88,6 +88,7 @@ impl SoupService for RecordingSoupService {
user_id: req.user,
limit: req.limit,
link_ids: req.link_ids,
+ team_id: team_receipt.map(|receipt| receipt.entity().entity_id.to_string()),
});
let page: PaginatedCursor<SoupItem<()>, String, SimpleSortMethod, T> =
Paginated::from_parts(items, None);
@@ -562,3 +563,55 @@ fn database_row_hydration_targets_exact_ids_and_rules_rows_out_otherwise() {
Some(&Expr::val(DatabaseRowLiteral::Id(Uuid::nil())))
);
}
+
+struct CurrentTeam(Mutex<Option<EntityAccessReceipt<MemberTeamRole>>>);
+impl SoupTeamReader for CurrentTeam {
+ fn team_receipt(
+ &self,
+ _: MacroUserIdStr<'static>,
+ ) -> BoxFuture<'_, Result<Option<EntityAccessReceipt<MemberTeamRole>>, SoupItemLoaderError>>
+ {
+ Box::pin(async { Ok(self.0.lock().unwrap().clone()) })
+ }
+}
+
+#[tokio::test]
+async fn pr_and_session_hydration_recheck_team_on_each_batch() {
+ use entity_access::domain::models::{EntityPermission, TeamRole};
+ let viewer = user("macro|[email protected]");
+ let team_id = Uuid::now_v7();
+ let team = Arc::new(CurrentTeam(Mutex::new(Some(
+ EntityAccessReceipt::try_new_authenticated_user(
+ viewer.clone(),
+ entity_access::domain::models::Entity {
+ entity_type: EntityType::Team,
+ entity_id: team_id.to_string(),
+ },
+ EntityPermission::TeamRole {
+ role: TeamRole::Member,
+ },
+ )
+ .unwrap(),
+ ))));
+ let soup = RecordingSoupService::default();
+ let mut loader = SoupItemLoader::new(soup.clone(), RecordingInboxReader::default());
+ loader.team_reader = Some(team.clone());
+ let keys = vec![
+ (
+ viewer.clone(),
+ EntityType::ForeignEntity.with_entity_string(Uuid::now_v7().to_string()),
+ ),
+ (
+ viewer.clone(),
+ EntityType::AgentSession.with_entity_string(Uuid::now_v7().to_string()),
+ ),
+ ];
+ loader.load(&keys).await.unwrap();
+ assert_eq!(
+ soup.calls.lock().unwrap()[0].team_id,
+ Some(team_id.to_string())
+ );
+ *team.0.lock().unwrap() = None;
+ loader.load(&keys).await.unwrap();
+ assert_eq!(soup.calls.lock().unwrap()[1].team_id, None);
+}
diff --git a/crates/macro_event_topics/src/lib.rs b/crates/macro_event_topics/src/lib.rs
index 21c8dffbf8..74a01b9545 100644
--- a/crates/macro_event_topics/src/lib.rs
+++ b/crates/macro_event_topics/src/lib.rs
@@ -53,6 +53,8 @@ topics! {
MacroCallsTopic => "macro.calls",
/// Document lifecycle events (created / updated / deleted / copied).
MacroDocumentsTopic => "macro.documents",
+ /// Committed GitHub pull request updates.
+ MacroGithubPullRequestsTopic => "macro.github_pull_requests",
/// User-scoped full Soup items produced from entity updates.
MacroSoupRealtimeTopic => "macro.soup",
/// Project lifecycle events (created, updated, deleted, restored, permanently deleted, and uploaded).
diff --git a/crates/soup_realtime/Cargo.toml b/crates/soup_realtime/Cargo.toml
index f8a35d94c3..5e45b9ef6b 100644
--- a/crates/soup_realtime/Cargo.toml
+++ b/crates/soup_realtime/Cargo.toml
@@ -10,6 +10,7 @@ consumer = [
"dep:chat",
"dep:documents",
"dep:email",
+ "dep:github_pull_requests",
"dep:initiative",
"dep:kafka_util",
"dep:models_properties",
@@ -23,6 +24,7 @@ consumer = [
]
default = ["consumer", "outbound", "ports"]
outbound = [
+ "dep:agent_session",
"dep:entity_access",
"ports",
]
@@ -36,6 +38,7 @@ ports = [
]
[dependencies]
+agent_session = { path = "../agent_session", default-features = false, optional = true }
broadcast = { path = "../broadcast", optional = true }
channels = { path = "../channels", default-features = false, optional = true }
messages = { path = "../messages", features = ["broker"] }
@@ -44,6 +47,7 @@ documents = { path = "../documents", default-features = false, features = ["serv
email = { path = "../email", default-features = false, optional = true }
entity_access = { path = "../entity_access", default-features = false, features = ["ports"], optional = true }
futures = { workspace = true, optional = true }
+github_pull_requests = { path = "../github_pull_requests", default-features = false, optional = true }
initiative = { path = "../initiative", default-features = false, optional = true }
kafka_util = { path = "../kafka_util", optional = true }
macro_event_broker = { path = "../macro_event_broker", default-features = false }
diff --git a/crates/soup_realtime/src/domain/ports.rs b/crates/soup_realtime/src/domain/ports.rs
index ec86034ac0..04cf20b91f 100644
--- a/crates/soup_realtime/src/domain/ports.rs
+++ b/crates/soup_realtime/src/domain/ports.rs
@@ -81,3 +81,12 @@ pub trait SoupRealtimePublisher: Send + Sync + 'static {
message: SoupRealtimeMessage,
) -> impl Future<Output = Result<(), Report>> + Send;
}
+
+/// Finds session entities whose displayed PR metadata depends on a pull request.
+pub trait PullRequestSessions: Send + Sync + 'static {
+ /// Return linked sessions; recipients must still be resolved using each session's access.
+ fn linked_sessions(
+ &self,
+ github_key: &str,
+ ) -> impl Future<Output = Result<Vec<Entity<'static>>, Report>> + Send;
+}
diff --git a/crates/soup_realtime/src/domain/service.rs b/crates/soup_realtime/src/domain/service.rs
index d2fa886038..77c7526804 100644
--- a/crates/soup_realtime/src/domain/service.rs
+++ b/crates/soup_realtime/src/domain/service.rs
@@ -224,3 +224,20 @@ impl SoupRealtimeService for SoupRealtimeServiceImpl {
.context("realtime Soup worker stopped before completing patch")?
}
}
+
+/// Expand a PR change to source-scoped records and linked sessions. Each item
+/// keeps its own access source; PR access never grants access to a session.
+pub async fn pull_request_patches(
+ sessions: &impl super::ports::PullRequestSessions,
+ github_key: &str,
+ records: impl IntoIterator<Item = Entity<'static>>,
+) -> Result<Vec<SoupRealtimePatch>, Report> {
+ let linked = sessions.linked_sessions(github_key).await?;
+ let mut seen = HashSet::new();
+ Ok(records
+ .into_iter()
+ .chain(linked)
+ .filter(|entity| seen.insert(entity.clone()))
+ .map(|entity| SoupRealtimePatch::for_entity(Patch::Updated(entity)))
+ .collect())
+}
diff --git a/crates/soup_realtime/src/inbound/kafka_consumer.rs b/crates/soup_realtime/src/inbound/kafka_consumer.rs
index be92bc71fb..96a35f4b8a 100644
--- a/crates/soup_realtime/src/inbound/kafka_consumer.rs
+++ b/crates/soup_realtime/src/inbound/kafka_consumer.rs
@@ -1,8 +1,9 @@
//! Kafka consumer for entity events that change realtime Soup items.
//!
//! Delivery is best effort with bounded retries: offsets are committed after
-//! successful domain processing or a logged terminal failure. Exhausted retries
-//! discard the rest of that event so it cannot wedge a partition. Malformed and
+//! successful domain processing or a logged terminal failure. Exhausted publication
+//! retries discard the rest of that event so it cannot wedge a partition. Failed
+//! linked-session lookups still allow direct PR record updates. Malformed and
//! recognized-but-irrelevant events are also committed. Cancellation leaves the
//! in-flight event uncommitted; partial delivery and replay can produce duplicates.
@@ -13,7 +14,7 @@ use std::{future::Future, time::Duration};
use crate::domain::{
models::{Patch, SoupRealtimePatch},
- ports::SoupRealtimeService,
+ ports::{PullRequestSessions, SoupRealtimeService},
service::SoupRealtimeServiceImpl,
};
use channels::domain::{
@@ -23,6 +24,7 @@ use channels::domain::{
use chat::domain::events::{ChatMacroEvent, ChatTopicEvent};
use documents::domain::events::{DocumentMacroEvent, DocumentTopicEvent, InteractionReason};
use email::domain::events::{EmailMacroEvent, EmailTopicEvent};
+use github_pull_requests::broker::{GithubPullRequestMacroEvent, GithubPullRequestTopicEvent};
use initiative::domain::events::{InitiativeMacroEvent, InitiativeTopicEvent};
use kafka_util::{GroupName, InitialOffset, KafkaEventConsumer};
use macro_event_broker::{
@@ -58,6 +60,7 @@ type SoupRealtimeKafkaConsumer =
macro_event_broker::declare_topics!(
DeclaredMacroEvent:
+ GithubPullRequestMacroEvent,
DocumentMacroEvent,
ProjectMacroEvent,
ChatMacroEvent,
@@ -552,6 +555,14 @@ fn patches_from_property_event(event: &PropertyTopicEvent) -> Vec<SoupRealtimePa
fn patches_from_event(event: &DeclaredMacroEvent) -> Vec<SoupRealtimePatch> {
match event {
+ DeclaredMacroEvent::GithubPullRequestMacroEvent(event) => {
+ let GithubPullRequestTopicEvent::Updated(change) = &event.event().event;
+ change
+ .foreign_entity_ids
+ .iter()
+ .map(|id| update(EntityType::ForeignEntity, id))
+ .collect()
+ }
DeclaredMacroEvent::DocumentMacroEvent(event) => {
patches_from_document_event(&event.event().event)
}
@@ -597,13 +608,39 @@ enum EventOutcome {
Dropped,
}
-#[tracing::instrument(skip(service, event, commit))]
-async fn process_event<S: SoupRealtimeService>(
+#[tracing::instrument(skip(service, sessions, event, commit))]
+async fn process_event<S: SoupRealtimeService, L: PullRequestSessions>(
service: &S,
+ sessions: &L,
event: &DeclaredMacroEvent,
commit: impl FnOnce(),
) -> EventOutcome {
- let patches = patches_from_event(event);
+ let mut patches = patches_from_event(event);
+ if let DeclaredMacroEvent::GithubPullRequestMacroEvent(event) = event {
+ let GithubPullRequestTopicEvent::Updated(update) = &event.event().event;
+ match Retry::start(notify_retry_strategy(), || {
+ crate::domain::service::pull_request_patches(
+ sessions,
+ &update.github_key,
+ update
+ .foreign_entity_ids
+ .iter()
+ .map(|id| entity(EntityType::ForeignEntity, id)),
+ )
+ })
+ .await
+ {
+ Ok(expanded) => patches = expanded,
+ Err(error) => {
+ tracing::error!(
+ error=?error,
+ github_key=%update.github_key,
+ attempts=MAX_NOTIFY_ATTEMPTS,
+ "skipping linked session updates after lookup retries; publishing PR records"
+ );
+ }
+ }
+ }
let mut outcome = if patches.is_empty() {
tracing::trace!("ignoring event without a Soup patch");
EventOutcome::Ignored
@@ -662,10 +699,11 @@ impl SoupRealtimeServiceImpl {
/// intentionally preferring consumer progress over delivery during persistent
/// failures. Shutdown during processing or retry backoff leaves the event
/// uncommitted for replay.
- #[tracing::instrument(skip(self, shutdown), fields(brokers), err)]
+ #[tracing::instrument(skip(self, sessions, shutdown), fields(brokers), err)]
pub async fn run_entity_update_consumer(
&self,
brokers: &str,
+ sessions: &impl PullRequestSessions,
shutdown: impl Future<Output = ()> + Send,
) -> Result<(), Report> {
let consumer = KafkaEventConsumer::<SoupRealtimeConsumerGroup>::from_env(brokers)?;
@@ -712,7 +750,7 @@ impl SoupRealtimeServiceImpl {
tokio::select! {
biased;
_ = &mut shutdown => break,
- _ = process_event(self, &event, || {
+ _ = process_event(self, sessions, &event, || {
commit_logged(&consumer, kafka_message);
}).instrument(span) => {}
}
diff --git a/crates/soup_realtime/src/inbound/kafka_consumer/test.rs b/crates/soup_realtime/src/inbound/kafka_consumer/test.rs
index 91b580427a..9adbf6c5f3 100644
--- a/crates/soup_realtime/src/inbound/kafka_consumer/test.rs
+++ b/crates/soup_realtime/src/inbound/kafka_consumer/test.rs
@@ -1,3 +1,5 @@
+mod pull_requests;
+
use std::collections::HashSet;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};
@@ -113,6 +115,7 @@ fn subscribes_to_all_existing_soup_source_topics() {
assert_eq!(
DeclaredMacroEvent::topics(),
[
+ "macro.github_pull_requests",
"macro.documents",
"macro.projects",
"macro.chats",
@@ -741,7 +744,7 @@ async fn updated_payload_maps_to_document_patch_and_commits() {
let commits = AtomicUsize::new(0);
assert!(matches!(
- process_event(&service, &event, || {
+ process_event(&service, &NoPullRequestSessions, &event, || {
commits.fetch_add(1, Ordering::SeqCst);
})
.await,
@@ -779,7 +782,7 @@ async fn transient_service_failures_are_retried_before_committing() {
let commits = AtomicUsize::new(0);
let started = tokio::time::Instant::now();
- let outcome = process_event(&service, &event, || {
+ let outcome = process_event(&service, &NoPullRequestSessions, &event, || {
assert_eq!(service.attempts.load(Ordering::SeqCst), MAX_NOTIFY_ATTEMPTS);
commits.fetch_add(1, Ordering::SeqCst);
})
@@ -800,7 +803,7 @@ async fn exhausted_service_retries_drop_and_commit_the_event() {
let commits = AtomicUsize::new(0);
let started = tokio::time::Instant::now();
- let outcome = process_event(&service, &event, || {
+ let outcome = process_event(&service, &NoPullRequestSessions, &event, || {
assert_eq!(service.attempts.load(Ordering::SeqCst), MAX_NOTIFY_ATTEMPTS);
assert_eq!(started.elapsed(), Duration::from_secs(15));
commits.fetch_add(1, Ordering::SeqCst);
@@ -818,14 +821,14 @@ async fn dropped_event_does_not_prevent_later_events_from_being_processed() {
let service = flaky_service(MAX_NOTIFY_ATTEMPTS);
let committed_offsets = Mutex::new(Vec::new());
- let first_outcome = process_event(&service, &first_event, || {
+ let first_outcome = process_event(&service, &NoPullRequestSessions, &first_event, || {
assert_eq!(service.attempts.load(Ordering::SeqCst), MAX_NOTIFY_ATTEMPTS);
committed_offsets.lock().expect("offsets lock").push(0);
})
.await;
assert!(matches!(first_outcome, EventOutcome::Dropped));
- let next_outcome = process_event(&service, &next_event, || {
+ let next_outcome = process_event(&service, &NoPullRequestSessions, &next_event, || {
assert_eq!(
service.attempts.load(Ordering::SeqCst),
MAX_NOTIFY_ATTEMPTS + 1
@@ -856,7 +859,7 @@ async fn multi_patch_event_retries_only_the_failed_patch_before_committing() {
service.failures.insert(2);
let commits = AtomicUsize::new(0);
- let outcome = process_event(&service, &event, || {
+ let outcome = process_event(&service, &NoPullRequestSessions, &event, || {
assert_eq!(service.attempts.load(Ordering::SeqCst), 4);
commits.fetch_add(1, Ordering::SeqCst);
})
@@ -884,7 +887,7 @@ async fn partial_event_failure_discards_remaining_patches_and_commits() {
service.failures = (2..=MAX_NOTIFY_ATTEMPTS + 1).collect();
let commits = AtomicUsize::new(0);
- let outcome = process_event(&service, &event, || {
+ let outcome = process_event(&service, &NoPullRequestSessions, &event, || {
assert_eq!(
service.attempts.load(Ordering::SeqCst),
MAX_NOTIFY_ATTEMPTS + 1
@@ -913,7 +916,7 @@ async fn ignored_event_commits_without_notifying() {
let service = flaky_service(0);
let commits = AtomicUsize::new(0);
- let outcome = process_event(&service, &event, || {
+ let outcome = process_event(&service, &NoPullRequestSessions, &event, || {
commits.fetch_add(1, Ordering::SeqCst);
})
.await;
@@ -937,7 +940,7 @@ async fn offset_is_committed_only_after_every_patch_finishes() {
let event = reindex_event(vec![Uuid::now_v7(), Uuid::now_v7()]);
let service = GatedService(tokio::sync::Semaphore::new(0));
let commits = AtomicUsize::new(0);
- let process = process_event(&service, &event, || {
+ let process = process_event(&service, &NoPullRequestSessions, &event, || {
commits.fetch_add(1, Ordering::SeqCst);
});
tokio::pin!(process);
@@ -958,7 +961,7 @@ async fn cancellation_during_publication_leaves_the_event_uncommitted() {
let service = GatedService(tokio::sync::Semaphore::new(0));
let commits = AtomicUsize::new(0);
{
- let process = process_event(&service, &event, || {
+ let process = process_event(&service, &NoPullRequestSessions, &event, || {
commits.fetch_add(1, Ordering::SeqCst);
});
tokio::pin!(process);
@@ -974,7 +977,7 @@ async fn cancellation_during_retry_backoff_leaves_the_event_uncommitted() {
let service = flaky_service(MAX_NOTIFY_ATTEMPTS);
let commits = AtomicUsize::new(0);
{
- let process = process_event(&service, &event, || {
+ let process = process_event(&service, &NoPullRequestSessions, &event, || {
commits.fetch_add(1, Ordering::SeqCst);
});
tokio::pin!(process);
@@ -1063,3 +1066,10 @@ fn moving_a_task_between_projects_refreshes_the_task_and_both_projects() {
vec![update(EntityType::Document, DOCUMENT_ID)]
);
}
+
+struct NoPullRequestSessions;
+impl PullRequestSessions for NoPullRequestSessions {
+ async fn linked_sessions(&self, _: &str) -> Result<Vec<Entity<'static>>, Report> {
+ Ok(Vec::new())
+ }
+}
diff --git a/crates/soup_realtime/src/inbound/kafka_consumer/test/pull_requests.rs b/crates/soup_realtime/src/inbound/kafka_consumer/test/pull_requests.rs
new file mode 100644
index 0000000000..f7eab2b153
--- /dev/null
+++ b/crates/soup_realtime/src/inbound/kafka_consumer/test/pull_requests.rs
@@ -0,0 +1,134 @@
+use super::*;
+
+struct LinkedSessions {
+ keys: Mutex<Vec<String>>,
+ session: Entity<'static>,
+ failures: usize,
+}
+impl PullRequestSessions for LinkedSessions {
+ async fn linked_sessions(&self, key: &str) -> Result<Vec<Entity<'static>>, Report> {
+ let mut keys = self.keys.lock().unwrap();
+ keys.push(key.to_owned());
+ if keys.len() <= self.failures {
+ return Err(rootcause::report!("temporary session lookup failure"));
+ }
+ Ok(vec![self.session.clone(), self.session.clone()])
+ }
+}
+
+fn pr_updated_event(foreign_entity_ids: Vec<Uuid>) -> DeclaredMacroEvent {
+ DeclaredMacroEvent::GithubPullRequestMacroEvent(GithubPullRequestMacroEvent::updated(
+ github_pull_requests::domain::events::GithubPullRequestUpdated {
+ github_key: "macro/app/pull/7".into(),
+ foreign_entity_ids,
+ },
+ ))
+}
+
+#[tokio::test]
+async fn pr_update_targets_exact_records_and_deduplicated_linked_sessions() {
+ let foreign = Uuid::now_v7();
+ let sessions = LinkedSessions {
+ keys: Mutex::default(),
+ session: entity(EntityType::AgentSession, Uuid::now_v7()),
+ failures: 0,
+ };
+ let event = pr_updated_event(vec![foreign]);
+ let service = flaky_service(0);
+ let commits = AtomicUsize::new(0);
+ assert!(matches!(
+ process_event(&service, &sessions, &event, || {
+ commits.fetch_add(1, Ordering::SeqCst);
+ })
+ .await,
+ EventOutcome::Notified
+ ));
+ assert_eq!(*sessions.keys.lock().unwrap(), vec!["macro/app/pull/7"]);
+ let patches = service.patches.lock().unwrap();
+ assert_eq!(patches.len(), 2);
+ assert_eq!(patches[0], update(EntityType::ForeignEntity, foreign));
+ assert_eq!(
+ patches[1],
+ SoupRealtimePatch::for_entity(Patch::Updated(sessions.session.clone()))
+ );
+ assert_eq!(commits.load(Ordering::SeqCst), 1);
+}
+
+#[tokio::test(start_paused = true)]
+async fn exhausted_session_lookup_retries_still_publish_pr_records_before_committing() {
+ let foreign_ids = vec![Uuid::now_v7(), Uuid::now_v7()];
+ let event = pr_updated_event(foreign_ids.clone());
+ let sessions = LinkedSessions {
+ keys: Mutex::default(),
+ session: entity(EntityType::AgentSession, Uuid::now_v7()),
+ failures: MAX_NOTIFY_ATTEMPTS,
+ };
+ let service = flaky_service(0);
+ let commits = AtomicUsize::new(0);
+ let started = tokio::time::Instant::now();
+ let expected = foreign_ids
+ .into_iter()
+ .map(|id| update(EntityType::ForeignEntity, id))
+ .collect::<Vec<_>>();
+
+ let outcome = process_event(&service, &sessions, &event, || {
+ assert_eq!(sessions.keys.lock().unwrap().len(), MAX_NOTIFY_ATTEMPTS);
+ assert_eq!(*service.patches.lock().unwrap(), expected);
+ commits.fetch_add(1, Ordering::SeqCst);
+ })
+ .await;
+
+ assert!(matches!(outcome, EventOutcome::Notified));
+ assert_eq!(commits.load(Ordering::SeqCst), 1);
+ assert_eq!(started.elapsed(), Duration::from_secs(15));
+}
+
+#[tokio::test(start_paused = true)]
+async fn transient_session_lookup_failures_publish_records_and_sessions_once() {
+ let foreign = Uuid::now_v7();
+ let event = pr_updated_event(vec![foreign]);
+ let sessions = LinkedSessions {
+ keys: Mutex::default(),
+ session: entity(EntityType::AgentSession, Uuid::now_v7()),
+ failures: MAX_NOTIFY_ATTEMPTS - 1,
+ };
+ let service = flaky_service(0);
+ let commits = AtomicUsize::new(0);
+ let expected = vec![
+ update(EntityType::ForeignEntity, foreign),
+ SoupRealtimePatch::for_entity(Patch::Updated(sessions.session.clone())),
+ ];
+
+ let outcome = process_event(&service, &sessions, &event, || {
+ assert_eq!(*service.patches.lock().unwrap(), expected);
+ commits.fetch_add(1, Ordering::SeqCst);
+ })
+ .await;
+
+ assert!(matches!(outcome, EventOutcome::Notified));
+ assert_eq!(sessions.keys.lock().unwrap().len(), MAX_NOTIFY_ATTEMPTS);
+ assert_eq!(commits.load(Ordering::SeqCst), 1);
+}
+
+#[tokio::test(start_paused = true)]
+async fn cancellation_during_session_lookup_backoff_leaves_event_uncommitted() {
+ let event = pr_updated_event(vec![Uuid::now_v7()]);
+ let sessions = LinkedSessions {
+ keys: Mutex::default(),
+ session: entity(EntityType::AgentSession, Uuid::now_v7()),
+ failures: MAX_NOTIFY_ATTEMPTS,
+ };
+ let service = flaky_service(0);
+ let commits = AtomicUsize::new(0);
+ {
+ let process = process_event(&service, &sessions, &event, || {
+ commits.fetch_add(1, Ordering::SeqCst);
+ });
+ tokio::pin!(process);
+ assert!(futures::poll!(&mut process).is_pending());
+ assert_eq!(sessions.keys.lock().unwrap().len(), 1);
+ }
+
+ assert!(service.patches.lock().unwrap().is_empty());
+ assert_eq!(commits.load(Ordering::SeqCst), 0);
+}
diff --git a/crates/soup_realtime/src/outbound/agent_sessions.rs b/crates/soup_realtime/src/outbound/agent_sessions.rs
new file mode 100644
index 0000000000..591f4fd2aa
--- /dev/null
+++ b/crates/soup_realtime/src/outbound/agent_sessions.rs
@@ -0,0 +1,20 @@
+//! Resolve PR dependencies through the agent-session domain's existing link port.
+use crate::domain::ports::PullRequestSessions;
+use agent_session::domain::pull_request_links::SessionPullRequestLinkRepo;
+use model_entity::{Entity, EntityType};
+use rootcause::prelude::{Report, ResultExt as _};
+
+/// Uses the owning session repository to look up links without duplicating its SQL.
+pub struct AgentSessionPullRequestLookup<R>(pub R);
+impl<R: SessionPullRequestLinkRepo> PullRequestSessions for AgentSessionPullRequestLookup<R> {
+ async fn linked_sessions(&self, github_key: &str) -> Result<Vec<Entity<'static>>, Report> {
+ Ok(self
+ .0
+ .sessions_for_pull_request(github_key)
+ .await
+ .context("finding sessions linked to PR")?
+ .into_iter()
+ .map(|id| EntityType::AgentSession.with_entity_string(id.to_string()))
+ .collect())
+ }
+}
diff --git a/crates/soup_realtime/src/outbound/mod.rs b/crates/soup_realtime/src/outbound/mod.rs
index be94d4b9b2..fda07ff27a 100644
--- a/crates/soup_realtime/src/outbound/mod.rs
+++ b/crates/soup_realtime/src/outbound/mod.rs
@@ -9,3 +9,7 @@ pub mod kafka_publisher;
/// Independent Kafka consumer for recipient-targeted Soup messages.
#[cfg(feature = "consumer")]
pub mod soup_consumer;
+
+/// Session dependencies for PR updates.
+#[cfg(feature = "outbound")]
+pub mod agent_sessions;
diff --git a/docs/AGENT_GUIDE/surfaces.md b/docs/AGENT_GUIDE/surfaces.md
index 19a9857ecf..f85e7585f1 100644
--- a/docs/AGENT_GUIDE/surfaces.md
+++ b/docs/AGENT_GUIDE/surfaces.md
@@ -1707,6 +1707,15 @@ a PR opened from a list or agent session, a second split, breadcrumb return,
side-panel toggle, and phone layout. If no GitHub data loads, the detail shows
an error banner with a Retry button; pressing it refetches the PR in place.
+PR metadata changes should update an already loaded Reviews list row and the
+linked agent session's PR status without reloading the page. To verify, keep
+both rows visible and change the PR in GitHub; check the status after the
+webhook arrives. Include a team-owned PR and a PR also stored for your personal
+account. In the network panel, confirm the update arrives on the Soup GraphQL
+subscription without triggering a refetch of every Soup list. Replaying an
+unchanged webhook should not produce another Soup update. The PR detail's
+separate diff and discussion requests are outside this subscription behavior.
+
## Calls — `/app/component/calls`
Tabs `All` / `Missed` / `Unattended`; `New call` offers `Call a channel or contact`
diff --git a/services/authentication_service/src/server.rs b/services/authentication_service/src/server.rs
index 131a0c7631..775ac79e18 100644
--- a/services/authentication_service/src/server.rs
+++ b/services/authentication_service/src/server.rs
@@ -462,6 +462,11 @@ pub async fn run() -> anyhow::Result<()> {
GithubPullRequestServiceImpl::new(
foreign_entity_service,
PgGithubPullRequestRepo::new(db.clone()),
+ )
+ .with_event_publisher(
+ github_pull_requests::broker::BrokerGithubPullRequestPublisher(
+ macro_event_broker.clone(),
+ ),
),
GithubLinkConfig {
client_id: config.github_client_id.to_string(),
diff --git a/services/document_storage_service/src/api/graphql_soup.rs b/services/document_storage_service/src/api/graphql_soup.rs
index 6ff0ae62c1..ebd513c480 100644
--- a/services/document_storage_service/src/api/graphql_soup.rs
+++ b/services/document_storage_service/src/api/graphql_soup.rs
@@ -17,7 +17,7 @@ use bots::outbound::pg_bots_repo::PgBotsRepo;
use complete_graph::GraphqlRequestParts;
use entity_registry::OwnerGrantPolicy;
use entity_registry_db_utils::OwnedEntityRegistrar;
-use graphql_soup::{email_mutation_thread_loader, soup_item_loader};
+use graphql_soup::{email_mutation_thread_loader, soup_item_loader_with_team_access};
use macro_authorization::{
OptionalMacroAuthorizationExtractor, UserOrInternalService, UserOrInternalServiceAuthorization,
};
@@ -152,9 +152,10 @@ fn insert_graphql_context_data(
);
// Ordinary queries and subscription hydration use the replica-backed
// Soup reader. The mutation-only loader below retains the primary reader.
- let soup_item_loader = soup_item_loader(
+ let soup_item_loader = soup_item_loader_with_team_access(
state.soup_router_state.service(),
state.soup_router_state.email_service(),
+ state.entity_access_service.as_ref().clone(),
);
data.insert(macro_user_id.clone());
data.insert(entity_mutation::EntityMutationActor {
diff --git a/services/document_storage_service/src/server.rs b/services/document_storage_service/src/server.rs
index 6ebca4e57a..cd6b677bb2 100644
--- a/services/document_storage_service/src/server.rs
+++ b/services/document_storage_service/src/server.rs
@@ -581,10 +581,17 @@ pub async fn run() -> anyhow::Result<()> {
installation_state_secret: config.github_installation_state_secret.to_string(),
},
document_service.clone(),
- Arc::new(GithubPullRequestServiceImpl::new(
- ForeignEntityServiceImpl::new(PgForeignEntityRepo::new(db.clone())),
- PgGithubPullRequestRepo::new(db.clone()),
- )),
+ Arc::new(
+ GithubPullRequestServiceImpl::new(
+ ForeignEntityServiceImpl::new(PgForeignEntityRepo::new(db.clone())),
+ PgGithubPullRequestRepo::new(db.clone()),
+ )
+ .with_event_publisher(
+ github_pull_requests::broker::BrokerGithubPullRequestPublisher(
+ macro_event_broker.clone(),
+ ),
+ ),
+ ),
(*notification_ingress_service).clone(),
PgGithubSyncRepo::new(db.clone()),
GithubSyncClientImpl::default(),
@@ -1699,6 +1706,10 @@ pub async fn run() -> anyhow::Result<()> {
});
consumer_tracker.spawn({
+ let session_repo = agent_session::outbound::postgres::PgAgentSessionRepo::new(
+ db.clone(),
+ owned_entity_registrar.clone(),
+ );
let brokers = config.kafka_brokers.as_ref().to_string();
let entity_access_service = entity_access_service.as_ref().clone();
let macro_event_broker = macro_event_broker.clone();
@@ -1716,7 +1727,13 @@ pub async fn run() -> anyhow::Result<()> {
);
tracing::info!("starting realtime Soup entity consumer");
let result = fanout_service
- .run_entity_update_consumer(&brokers, cancellation_token.cancelled())
+ .run_entity_update_consumer(
+ &brokers,
+ &soup_realtime::outbound::agent_sessions::AgentSessionPullRequestLookup(
+ session_repo.clone(),
+ ),
+ cancellation_token.cancelled(),
+ )
.await;
if cancellation_token.is_cancelled() {