Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
refactor(github_pull_requests): own pull request diff reading (#7113)
dev-rb4f625871a3c3
diff --git a/.github/workspace-dep-closures.json b/.github/workspace-dep-closures.jsonindex a03a3fa936..f158f6f558 100644--- a/.github/workspace-dep-closures.json+++ b/.github/workspace-dep-closures.json@@ -66,7 +66,6 @@"crates/call","crates/channel_sender","crates/channels",- "crates/comms_db_client","crates/connection","crates/connection_gateway_client","crates/connection_gateway_models",@@ -75,10 +74,8 @@"crates/crm","crates/database_env_vars","crates/document_sub_type",- "crates/documents","crates/email","crates/email_utils",- "crates/embedding","crates/entity_access","crates/entity_access_db_utils","crates/entity_access_management",@@ -89,10 +86,9 @@"crates/filter_ast","crates/foreign_entity","crates/frecency",- "crates/fusionauth","crates/genai_telemetry","crates/generic_email_domains",- "crates/github",+ "crates/git_patch","crates/github_pull_requests","crates/google_token","crates/harness_id",@@ -107,14 +103,12 @@"crates/macro_auth","crates/macro_authorization","crates/macro_aws_config",- "crates/macro_db_client","crates/macro_entrypoint","crates/macro_env","crates/macro_env_var","crates/macro_event_broker","crates/macro_event_topics","crates/macro_service_urls",- "crates/macro_sync_service_jwt","crates/macro_tower_layers","crates/macro_user_id","crates/macro_uuid",@@ -129,15 +123,12 @@"crates/model_user","crates/models_ai_projection","crates/models_bulk_upload",- "crates/models_dcs","crates/models_email","crates/models_entity_access_management",- "crates/models_opensearch","crates/models_pagination","crates/models_permissions","crates/models_properties","crates/models_sfs",- "crates/models_team","crates/non_empty","crates/notification","crates/notification_state",@@ -147,19 +138,13 @@"crates/remote_env_var","crates/roles_and_permissions","crates/s3_key",- "crates/secretsmanager_client","crates/share_permission_db_utils","crates/sqs_client","crates/sqs_worker","crates/static_file_service_client",- "crates/sync_service",- "crates/sync_service_client","crates/system_properties",- "crates/system_skills",- "crates/task_dedup","crates/teams","crates/unfurl",- "crates/user_quota","crates/workspace-hack"],"agent_egress": [@@ -209,6 +194,7 @@"crates/fusionauth","crates/genai_telemetry","crates/generic_email_domains",+ "crates/git_patch","crates/github","crates/github_pull_requests","crates/google_token",@@ -344,6 +330,7 @@"crates/fusionauth","crates/genai_telemetry","crates/generic_email_domains",+ "crates/git_patch","crates/github","crates/github_pull_requests","crates/google_token",@@ -483,6 +470,7 @@"crates/fusionauth","crates/genai_telemetry","crates/generic_email_domains",+ "crates/git_patch","crates/github","crates/github_pull_requests","crates/google_token",@@ -642,6 +630,7 @@"crates/fusionauth","crates/genai_telemetry","crates/generic_email_domains",+ "crates/git_patch","crates/github","crates/github_pull_requests","crates/google_token",@@ -1231,10 +1220,9 @@"crates/filter_ast","crates/foreign_entity","crates/frecency",- "crates/fusionauth","crates/genai_telemetry","crates/generic_email_domains",- "crates/github",+ "crates/git_patch","crates/github_pull_requests","crates/google_token","crates/harness_id",@@ -1474,10 +1462,9 @@"crates/filter_ast","crates/foreign_entity","crates/frecency",- "crates/fusionauth","crates/genai_telemetry","crates/generic_email_domains",- "crates/github",+ "crates/git_patch","crates/github_pull_requests","crates/google_token","crates/harness_id",@@ -1760,6 +1747,7 @@"crates/fusionauth","crates/genai_telemetry","crates/generic_email_domains",+ "crates/git_patch","crates/github","crates/github_pull_requests","crates/google_token",@@ -2001,7 +1989,6 @@"crates/client/cache-turso","crates/client/cache-wasm","crates/client/turso-opfs",- "crates/comms_db_client","crates/connection","crates/connection_gateway_client","crates/connection_gateway_models",@@ -2010,10 +1997,8 @@"crates/crm","crates/database_env_vars","crates/document_sub_type",- "crates/documents","crates/email","crates/email_utils",- "crates/embedding","crates/entity_access","crates/entity_access_db_utils","crates/entity_access_management",@@ -2025,10 +2010,9 @@"crates/filter_ast","crates/foreign_entity","crates/frecency",- "crates/fusionauth","crates/genai_telemetry","crates/generic_email_domains",- "crates/github",+ "crates/git_patch","crates/github_pull_requests","crates/google_token","crates/graphql_common",@@ -2046,14 +2030,12 @@"crates/macro_auth","crates/macro_authorization","crates/macro_aws_config",- "crates/macro_db_client","crates/macro_entrypoint","crates/macro_env","crates/macro_env_var","crates/macro_event_broker","crates/macro_event_topics","crates/macro_service_urls",- "crates/macro_sync_service_jwt","crates/macro_tower_layers","crates/macro_user_id","crates/macro_uuid",@@ -2069,17 +2051,14 @@"crates/model_user","crates/models_ai_projection","crates/models_bulk_upload",- "crates/models_dcs","crates/models_email","crates/models_entity_access_management","crates/models_grouping",- "crates/models_opensearch","crates/models_pagination","crates/models_permissions","crates/models_properties","crates/models_sfs","crates/models_soup",- "crates/models_team","crates/non_empty","crates/notification","crates/notification_state",@@ -2092,7 +2071,6 @@"crates/remote_env_var","crates/roles_and_permissions","crates/s3_key",- "crates/secretsmanager_client","crates/share_permission_db_utils","crates/soup","crates/soup_filter_cache_adapter",@@ -2100,14 +2078,9 @@"crates/sqs_client","crates/sqs_worker","crates/static_file_service_client",- "crates/sync_service",- "crates/sync_service_client","crates/system_properties",- "crates/system_skills",- "crates/task_dedup","crates/teams","crates/unfurl",- "crates/user_quota","crates/workspace-hack"],"cal": [@@ -2482,10 +2455,9 @@"crates/filter_ast","crates/foreign_entity","crates/frecency",- "crates/fusionauth","crates/genai_telemetry","crates/generic_email_domains",- "crates/github",+ "crates/git_patch","crates/github_pull_requests","crates/google_token","crates/harness_id",@@ -3012,6 +2984,7 @@"crates/frecency","crates/genai_telemetry","crates/generic_email_domains",+ "crates/git_patch","crates/github_pull_requests","crates/google_token","crates/harness_id",@@ -3277,10 +3250,9 @@"crates/filter_ast","crates/foreign_entity","crates/frecency",- "crates/fusionauth","crates/genai_telemetry","crates/generic_email_domains",- "crates/github",+ "crates/git_patch","crates/github_pull_requests","crates/google_token","crates/graphql_activity",@@ -3951,7 +3923,6 @@"crates/call","crates/channel_sender","crates/channels",- "crates/comms_db_client","crates/connection","crates/connection_gateway_client","crates/connection_gateway_models",@@ -3963,10 +3934,8 @@"crates/databases","crates/databases_sql","crates/document_sub_type",- "crates/documents","crates/email","crates/email_utils",- "crates/embedding","crates/entity_access","crates/entity_access_db_utils","crates/entity_access_management",@@ -3978,10 +3947,9 @@"crates/filter_ast","crates/foreign_entity","crates/frecency",- "crates/fusionauth","crates/genai_telemetry","crates/generic_email_domains",- "crates/github",+ "crates/git_patch","crates/github_pull_requests","crates/google_token","crates/harness_id",@@ -3996,14 +3964,12 @@"crates/macro_auth","crates/macro_authorization","crates/macro_aws_config",- "crates/macro_db_client","crates/macro_entrypoint","crates/macro_env","crates/macro_env_var","crates/macro_event_broker","crates/macro_event_topics","crates/macro_service_urls",- "crates/macro_sync_service_jwt","crates/macro_tower_layers","crates/macro_user_id","crates/macro_uuid",@@ -4020,17 +3986,14 @@"crates/models_ai_projection","crates/models_bulk_upload","crates/models_databases",- "crates/models_dcs","crates/models_email","crates/models_entity_access_management","crates/models_grouping",- "crates/models_opensearch","crates/models_pagination","crates/models_permissions","crates/models_properties","crates/models_sfs","crates/models_soup",- "crates/models_team","crates/non_empty","crates/notification","crates/notification_state",@@ -4042,20 +4005,14 @@"crates/remote_env_var","crates/roles_and_permissions","crates/s3_key",- "crates/secretsmanager_client","crates/share_permission_db_utils","crates/soup","crates/sqs_client","crates/sqs_worker","crates/static_file_service_client",- "crates/sync_service",- "crates/sync_service_client","crates/system_properties",- "crates/system_skills",- "crates/task_dedup","crates/teams","crates/unfurl",- "crates/user_quota","crates/workspace-hack"],"dataloss_prevention_handler": [@@ -4153,6 +4110,7 @@"crates/frecency","crates/genai_telemetry","crates/generic_email_domains",+ "crates/git_patch","crates/github_pull_requests","crates/google_token","crates/harness_id",@@ -4302,10 +4260,9 @@"crates/filter_ast","crates/foreign_entity","crates/frecency",- "crates/fusionauth","crates/genai_telemetry","crates/generic_email_domains",- "crates/github",+ "crates/git_patch","crates/github_pull_requests","crates/google_token","crates/harness_id",@@ -4475,6 +4432,7 @@"crates/fusionauth","crates/genai_telemetry","crates/generic_email_domains",+ "crates/git_patch","crates/github","crates/github_pull_requests","crates/google_token",@@ -4720,6 +4678,7 @@"crates/frecency","crates/genai_telemetry","crates/generic_email_domains",+ "crates/git_patch","crates/github_pull_requests","crates/google_token","crates/harness_id",@@ -4833,6 +4792,7 @@"crates/frecency","crates/genai_telemetry","crates/generic_email_domains",+ "crates/git_patch","crates/github_pull_requests","crates/google_token","crates/harness_id",@@ -5960,6 +5920,10 @@"crates/generic_email_domains","crates/workspace-hack"],+ "git_patch": [+ "crates/git_patch",+ "crates/workspace-hack"+ ],"github": ["crates/activity","crates/agent",@@ -6004,6 +5968,7 @@"crates/fusionauth","crates/genai_telemetry","crates/generic_email_domains",+ "crates/git_patch","crates/github","crates/github_pull_requests","crates/google_token",@@ -6088,6 +6053,7 @@"crates/filter_ast","crates/foreign_entity","crates/genai_telemetry",+ "crates/git_patch","crates/github_pull_requests","crates/harness_id","crates/harness_token",@@ -6439,10 +6405,9 @@"crates/filter_ast","crates/foreign_entity","crates/frecency",- "crates/fusionauth","crates/genai_telemetry","crates/generic_email_domains",- "crates/github",+ "crates/git_patch","crates/github_pull_requests","crates/google_token","crates/graphql_common",@@ -6576,10 +6541,9 @@"crates/filter_ast","crates/foreign_entity","crates/frecency",- "crates/fusionauth","crates/genai_telemetry","crates/generic_email_domains",- "crates/github",+ "crates/git_patch","crates/github_pull_requests","crates/google_token","crates/graphql_common",@@ -6714,10 +6678,9 @@"crates/filter_ast","crates/foreign_entity","crates/frecency",- "crates/fusionauth","crates/genai_telemetry","crates/generic_email_domains",- "crates/github",+ "crates/git_patch","crates/github_pull_requests","crates/google_token","crates/graphql_common",@@ -6996,10 +6959,9 @@"crates/filter_ast","crates/foreign_entity","crates/frecency",- "crates/fusionauth","crates/genai_telemetry","crates/generic_email_domains",- "crates/github",+ "crates/git_patch","crates/github_pull_requests","crates/google_token","crates/graphql_scheduled_action",@@ -7147,10 +7109,9 @@"crates/filter_ast","crates/foreign_entity","crates/frecency",- "crates/fusionauth","crates/genai_telemetry","crates/generic_email_domains",- "crates/github",+ "crates/git_patch","crates/github_pull_requests","crates/google_token","crates/graphql_common",@@ -7656,6 +7617,7 @@"crates/frecency","crates/genai_telemetry","crates/generic_email_domains",+ "crates/git_patch","crates/github_pull_requests","crates/google_token","crates/harness_id",@@ -8373,7 +8335,7 @@"crates/fusionauth","crates/genai_telemetry","crates/generic_email_domains",- "crates/github",+ "crates/git_patch","crates/github_pull_requests","crates/google_token","crates/harness_id",@@ -8534,10 +8496,9 @@"crates/filter_ast","crates/foreign_entity","crates/frecency",- "crates/fusionauth","crates/genai_telemetry","crates/generic_email_domains",- "crates/github",+ "crates/git_patch","crates/github_pull_requests","crates/google_token","crates/harness_id",@@ -9683,6 +9644,7 @@"crates/fusionauth","crates/genai_telemetry","crates/generic_email_domains",+ "crates/git_patch","crates/github","crates/github_pull_requests","crates/google_token",@@ -9803,6 +9765,7 @@"crates/frecency","crates/genai_telemetry","crates/generic_email_domains",+ "crates/git_patch","crates/github_pull_requests","crates/google_token","crates/harness_id",@@ -10196,10 +10159,9 @@"crates/filter_ast","crates/foreign_entity","crates/frecency",- "crates/fusionauth","crates/genai_telemetry","crates/generic_email_domains",- "crates/github",+ "crates/git_patch","crates/github_pull_requests","crates/google_token","crates/harness_id",@@ -10348,6 +10310,7 @@"crates/frecency","crates/genai_telemetry","crates/generic_email_domains",+ "crates/git_patch","crates/github_pull_requests","crates/google_token","crates/harness_id",@@ -10859,10 +10822,9 @@"crates/filter_ast","crates/foreign_entity","crates/frecency",- "crates/fusionauth","crates/genai_telemetry","crates/generic_email_domains",- "crates/github",+ "crates/git_patch","crates/github_pull_requests","crates/google_token","crates/harness_id",@@ -11171,7 +11133,6 @@"crates/call","crates/channel_sender","crates/channels",- "crates/comms_db_client","crates/connection","crates/connection_gateway_client","crates/connection_gateway_models",@@ -11180,10 +11141,8 @@"crates/crm","crates/database_env_vars","crates/document_sub_type",- "crates/documents","crates/email","crates/email_utils",- "crates/embedding","crates/entity_access","crates/entity_access_db_utils","crates/entity_access_management",@@ -11195,10 +11154,9 @@"crates/filter_ast","crates/foreign_entity","crates/frecency",- "crates/fusionauth","crates/genai_telemetry","crates/generic_email_domains",- "crates/github",+ "crates/git_patch","crates/github_pull_requests","crates/google_token","crates/harness_id",@@ -11213,14 +11171,12 @@"crates/macro_auth","crates/macro_authorization","crates/macro_aws_config",- "crates/macro_db_client","crates/macro_entrypoint","crates/macro_env","crates/macro_env_var","crates/macro_event_broker","crates/macro_event_topics","crates/macro_service_urls",- "crates/macro_sync_service_jwt","crates/macro_tower_layers","crates/macro_user_id","crates/macro_uuid",@@ -11235,17 +11191,14 @@"crates/model_user","crates/models_ai_projection","crates/models_bulk_upload",- "crates/models_dcs","crates/models_email","crates/models_entity_access_management","crates/models_grouping",- "crates/models_opensearch","crates/models_pagination","crates/models_permissions","crates/models_properties","crates/models_sfs","crates/models_soup",- "crates/models_team","crates/non_empty","crates/notification","crates/notification_state",@@ -11257,20 +11210,14 @@"crates/remote_env_var","crates/roles_and_permissions","crates/s3_key",- "crates/secretsmanager_client","crates/share_permission_db_utils","crates/soup","crates/sqs_client","crates/sqs_worker","crates/static_file_service_client",- "crates/sync_service",- "crates/sync_service_client","crates/system_properties",- "crates/system_skills",- "crates/task_dedup","crates/teams","crates/unfurl",- "crates/user_quota","crates/workspace-hack"],"soup-filter-cache-adapter": [@@ -11295,7 +11242,6 @@"crates/channel_sender","crates/channels","crates/client/cache-core",- "crates/comms_db_client","crates/connection","crates/connection_gateway_client","crates/connection_gateway_models",@@ -11304,10 +11250,8 @@"crates/crm","crates/database_env_vars","crates/document_sub_type",- "crates/documents","crates/email","crates/email_utils",- "crates/embedding","crates/entity_access","crates/entity_access_db_utils","crates/entity_access_management",@@ -11319,10 +11263,9 @@"crates/filter_ast","crates/foreign_entity","crates/frecency",- "crates/fusionauth","crates/genai_telemetry","crates/generic_email_domains",- "crates/github",+ "crates/git_patch","crates/github_pull_requests","crates/google_token","crates/graphql_common",@@ -11340,14 +11283,12 @@"crates/macro_auth","crates/macro_authorization","crates/macro_aws_config",- "crates/macro_db_client","crates/macro_entrypoint","crates/macro_env","crates/macro_env_var","crates/macro_event_broker","crates/macro_event_topics","crates/macro_service_urls",- "crates/macro_sync_service_jwt","crates/macro_tower_layers","crates/macro_user_id","crates/macro_uuid",@@ -11363,17 +11304,14 @@"crates/model_user","crates/models_ai_projection","crates/models_bulk_upload",- "crates/models_dcs","crates/models_email","crates/models_entity_access_management","crates/models_grouping",- "crates/models_opensearch","crates/models_pagination","crates/models_permissions","crates/models_properties","crates/models_sfs","crates/models_soup",- "crates/models_team","crates/non_empty","crates/notification","crates/notification_state",@@ -11386,7 +11324,6 @@"crates/remote_env_var","crates/roles_and_permissions","crates/s3_key",- "crates/secretsmanager_client","crates/share_permission_db_utils","crates/soup","crates/soup_filter_cache_adapter",@@ -11394,14 +11331,9 @@"crates/sqs_client","crates/sqs_worker","crates/static_file_service_client",- "crates/sync_service",- "crates/sync_service_client","crates/system_properties",- "crates/system_skills",- "crates/task_dedup","crates/teams","crates/unfurl",- "crates/user_quota","crates/workspace-hack"],"soup-filter-projection": [@@ -11425,7 +11357,6 @@"crates/call","crates/channel_sender","crates/channels",- "crates/comms_db_client","crates/connection","crates/connection_gateway_client","crates/connection_gateway_models",@@ -11434,10 +11365,8 @@"crates/crm","crates/database_env_vars","crates/document_sub_type",- "crates/documents","crates/email","crates/email_utils",- "crates/embedding","crates/entity_access","crates/entity_access_db_utils","crates/entity_access_management",@@ -11449,10 +11378,9 @@"crates/filter_ast","crates/foreign_entity","crates/frecency",- "crates/fusionauth","crates/genai_telemetry","crates/generic_email_domains",- "crates/github",+ "crates/git_patch","crates/github_pull_requests","crates/google_token","crates/harness_id",@@ -11468,14 +11396,12 @@"crates/macro_auth","crates/macro_authorization","crates/macro_aws_config",- "crates/macro_db_client","crates/macro_entrypoint","crates/macro_env","crates/macro_env_var","crates/macro_event_broker","crates/macro_event_topics","crates/macro_service_urls",- "crates/macro_sync_service_jwt","crates/macro_tower_layers","crates/macro_user_id","crates/macro_uuid",@@ -11490,17 +11416,14 @@"crates/model_user","crates/models_ai_projection","crates/models_bulk_upload",- "crates/models_dcs","crates/models_email","crates/models_entity_access_management","crates/models_grouping",- "crates/models_opensearch","crates/models_pagination","crates/models_permissions","crates/models_properties","crates/models_sfs","crates/models_soup",- "crates/models_team","crates/non_empty","crates/notification","crates/notification_state",@@ -11513,21 +11436,15 @@"crates/remote_env_var","crates/roles_and_permissions","crates/s3_key",- "crates/secretsmanager_client","crates/share_permission_db_utils","crates/soup","crates/soup_filter_projection","crates/sqs_client","crates/sqs_worker","crates/static_file_service_client",- "crates/sync_service",- "crates/sync_service_client","crates/system_properties",- "crates/system_skills",- "crates/task_dedup","crates/teams","crates/unfurl",- "crates/user_quota","crates/workspace-hack"],"soup_realtime": [@@ -11575,6 +11492,7 @@"crates/frecency","crates/genai_telemetry","crates/generic_email_domains",+ "crates/git_patch","crates/github_pull_requests","crates/google_token","crates/harness_id",@@ -12376,6 +12294,7 @@"crates/frecency","crates/genai_telemetry","crates/generic_email_domains",+ "crates/git_patch","crates/github_pull_requests","crates/google_token","crates/harness_id",diff --git a/Cargo.lock b/Cargo.lockindex ad2cf4c77e..ad0740a768 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -233,12 +233,12 @@ dependencies = ["chrono","dashmap","entity_access",- "github",+ "git_patch",+ "github_pull_requests","macro_authorization","macro_db_migrator","macro_user_id","macro_uuid",- "reqwest 0.13.4","rootcause","serde","serde_json",@@ -247,7 +247,6 @@ dependencies = ["thiserror 2.0.18","tokio","tracing",- "url","utoipa","uuid","workspace-hack",@@ -8081,6 +8080,18 @@ dependencies = ["weezl",]+[[package]]+name = "git_patch"+version = "0.1.0"+dependencies = [+ "chrono",+ "serde",+ "strum 0.27.2",+ "utoipa",+ "uuid",+ "workspace-hack",+]+[[package]]name = "github"version = "0.1.0"@@ -8150,6 +8161,7 @@ dependencies = ["entity_access","filter_ast","foreign_entity",+ "git_patch","item_filters","macro_authorization","macro_db_migrator",@@ -8162,6 +8174,7 @@ dependencies = ["thiserror 2.0.18","tokio","tracing",+ "url","utoipa","uuid","workspace-hack",diff --git a/Cargo.toml b/Cargo.tomlindex 41db948cf1..36d2b74e40 100644--- a/Cargo.toml+++ b/Cargo.toml@@ -50,6 +50,7 @@ members = ["crates/foreign_entity","crates/genai_telemetry","crates/generic_email_domains",+ "crates/git_patch","crates/gmail_client","crates/google_token","crates/graphql_activity",diff --git a/apps/web/src/lib/service-clients/service-agent-harness/generated/schemas/changesetDto.ts b/apps/web/src/lib/service-clients/service-agent-harness/generated/schemas/changesetDto.tsindex f3584b6fd0..06fdc9fa48 100644--- a/apps/web/src/lib/service-clients/service-agent-harness/generated/schemas/changesetDto.ts+++ b/apps/web/src/lib/service-clients/service-agent-harness/generated/schemas/changesetDto.ts@@ -11,7 +11,7 @@ import type { ChangesetSourceDto } from './changesetSourceDto';import type { GitRefDto } from './gitRefDto';/**- * One capture of a session's changes.+ * One changeset: the files a patch touches and what happened to each.Clients deserialize this, so both derives are used.*/@@ -34,10 +34,10 @@ export interface ChangesetDto {files: ChangedFileDto[];/** The side carrying the work. */head: GitRefDto;- /** The capture's id; changes with every capture. */+ /** The changeset's id; a different id means different changes. */id: string;/**- * Size of the patch `GET .../changes/patch` serves; zero when nothing+ * Size of the patch the matching patch route serves; zero when nothingchanged.* @minimum 0*/diff --git a/apps/web/src/lib/service-clients/service-agent-harness/generated/schemas/changesetSourceDto.ts b/apps/web/src/lib/service-clients/service-agent-harness/generated/schemas/changesetSourceDto.tsindex ba46ac932d..cb8c0bd4e0 100644--- a/apps/web/src/lib/service-clients/service-agent-harness/generated/schemas/changesetSourceDto.ts+++ b/apps/web/src/lib/service-clients/service-agent-harness/generated/schemas/changesetSourceDto.ts@@ -6,7 +6,7 @@*//**- * The source of the captured diff, on the wire.+ * The source of a changeset's diff, on the wire.*/export type ChangesetSourceDto =(typeof ChangesetSourceDto)[keyof typeof ChangesetSourceDto];diff --git a/apps/web/src/lib/service-clients/service-agent-harness/openapi.json b/apps/web/src/lib/service-clients/service-agent-harness/openapi.jsonindex 30957b96f7..83756cb3b0 100644--- a/apps/web/src/lib/service-clients/service-agent-harness/openapi.json+++ b/apps/web/src/lib/service-clients/service-agent-harness/openapi.json@@ -2724,7 +2724,7 @@},"ChangesetDto": {"type": "object",- "description": "One capture of a session's changes.\n\nClients deserialize this, so both derives are used.",+ "description": "One changeset: the files a patch touches and what happened to each.\n\nClients deserialize this, so both derives are used.","required": ["id","source",@@ -2773,12 +2773,12 @@"id": {"type": "string","format": "uuid",- "description": "The capture's id; changes with every capture."+ "description": "The changeset's id; a different id means different changes."},"patchBytes": {"type": "integer","format": "int64",- "description": "Size of the patch `GET .../changes/patch` serves; zero when nothing\nchanged.",+ "description": "Size of the patch the matching patch route serves; zero when nothing\nchanged.","minimum": 0},"repository": {@@ -2797,7 +2797,7 @@},"ChangesetSourceDto": {"type": "string",- "description": "The source of the captured diff, on the wire.",+ "description": "The source of a changeset's diff, on the wire.","enum": ["github_pull_request"]},"ChannelSharePermission": {diff --git a/crates/agent_changes/Cargo.toml b/crates/agent_changes/Cargo.tomlindex fb2abc7294..a2bf0649f9 100644--- a/crates/agent_changes/Cargo.toml+++ b/crates/agent_changes/Cargo.toml@@ -14,16 +14,10 @@ inbound = ["macro_authorization/axum","schema",]-# Postgres, S3, the GitHub pull request client.-outbound = [- "dep:aws-sdk-s3",- "dep:github",- "dep:reqwest",- "dep:sqlx",- "macro_user_id/sqlx",-]+# Postgres and S3.+outbound = ["dep:aws-sdk-s3", "dep:sqlx", "macro_user_id/sqlx"]# OpenAPI schemas for the router's DTOs.-schema = ["dep:utoipa"]+schema = ["dep:utoipa", "git_patch/schema"]# In-memory port implementations, for crates that test against this one.test-utils = []@@ -38,16 +32,13 @@ dashmap = { workspace = true }entity_access = { path = "../entity_access", default-features = false, features = ["ports",] }-# `sync` alone does not compile without `outbound` today; mirror agent_harness.-github = { path = "../github", default-features = false, features = [- "outbound",+git_patch = { path = "../git_patch" }+github_pull_requests = { path = "../github_pull_requests", default-features = false, features = ["ports",- "sync",-], optional = true }+] }macro_authorization = { path = "../macro_authorization", default-features = false, optional = true }macro_user_id = { path = "../macro_user_id" }macro_uuid = { path = "../macro_uuid" }-reqwest = { workspace = true, optional = true }rootcause = { workspace = true }serde = { workspace = true }serde_json = { workspace = true }@@ -56,7 +47,6 @@ strum = { workspace = true }thiserror = { workspace = true }tokio = { workspace = true, features = ["rt", "sync"] }tracing = { workspace = true }-url = { workspace = true }utoipa = { workspace = true, optional = true }uuid = { workspace = true }workspace-hack = { version = "0.1", path = "../workspace-hack" }diff --git a/crates/agent_changes/src/domain/error.rs b/crates/agent_changes/src/domain/error.rsindex e29ebfc7c3..b59ff768a6 100644--- a/crates/agent_changes/src/domain/error.rs+++ b/crates/agent_changes/src/domain/error.rs@@ -35,20 +35,3 @@ pub enum ExtractError {#[error("{0}")]Failed(rootcause::Report),}--/// Why a GitHub pull request diff could not be read.-#[derive(Debug, thiserror::Error)]-pub enum CompareError {- /// GitHub cannot find the linked pull request.- #[error("the linked pull request does not exist on GitHub")]- NotFound,- /// The provider refuses to render a diff this large.- #[error("the diff is too large for the repository provider to compare")]- TooLarge,- /// The caller may not reach the repository through the provider.- #[error("the repository is not reachable through the configured GitHub App")]- Unavailable,- /// Anything else.- #[error("{0}")]- Other(rootcause::Report),-}diff --git a/crates/agent_changes/src/domain/model.rs b/crates/agent_changes/src/domain/model.rsindex 9a803280a5..cdb6337f16 100644--- a/crates/agent_changes/src/domain/model.rs+++ b/crates/agent_changes/src/domain/model.rs@@ -11,6 +11,8 @@ use macro_uuid::Uuid;use serde::{Deserialize, Serialize};pub use agent_session::domain::model::AgentSessionId;+pub use git_patch::{ChangedFile, ChangesetRange, FileChangeKind, GitRef};+pub use github_pull_requests::domain::models::{PullRequestRef, RepositorySlug};/// A captured branch together with the repository it belongs to.#[derive(Debug, Clone, PartialEq, Eq)]@@ -66,84 +68,6 @@ impl std::fmt::Display for ChangesetId {}}-/// What happened to one file, as a diff reports it.-#[derive(- Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, strum::Display, strum::EnumString,-)]-#[serde(rename_all = "snake_case")]-#[strum(serialize_all = "snake_case")]-pub enum FileChangeKind {- /// The file did not exist before.- Added,- /// The file exists on both sides with different contents.- Modified,- /// The file no longer exists.- Deleted,- /// The file moved; `previous_path` names where from. Contents may also- /// have changed.- Renamed,-}--/// One file in a changeset, as much as the pane's tree and headers need.-#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]-#[serde(rename_all = "camelCase")]-pub struct ChangedFile {- /// The file's path after the change - or before it, for a deletion.- pub path: String,- /// Where a renamed file came from.- #[serde(default, skip_serializing_if = "Option::is_none")]- pub previous_path: Option<String>,- /// What happened to the file.- pub kind: FileChangeKind,- /// Lines added.- pub additions: u32,- /// Lines removed.- pub deletions: u32,- /// The diff carries no text for this file (an image, an archive, ...).- #[serde(default)]- pub binary: bool,- /// This file's hunks were left out of the stored patch because the- /// changeset exceeded the size budget; the summary is still complete.- #[serde(default)]- pub patch_omitted: bool,-}--/// One end of the compared range, as much of it as the provider told us.-#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]-#[serde(rename_all = "camelCase")]-pub struct GitRef {- /// The branch name, when the provider names one.- #[serde(default, skip_serializing_if = "Option::is_none")]- pub name: Option<String>,- /// The commit the diff was taken at, when known.- #[serde(default, skip_serializing_if = "Option::is_none")]- pub sha: Option<String>,-}--impl GitRef {- /// A ref known by name only.- #[must_use]- pub fn named(name: impl Into<String>) -> Self {- Self {- name: Some(name.into()),- sha: None,- }- }-}--/// What was compared with what.-#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]-#[serde(rename_all = "camelCase")]-pub struct ChangesetRange {- /// The linked pull request's repository, as `https://github.com/owner/name`.- #[serde(default, skip_serializing_if = "Option::is_none")]- pub repository: Option<String>,- /// The side the work started from.- pub base: GitRef,- /// The side carrying the work.- pub head: GitRef,-}-/// The source of the captured diff.#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, strum::Display, strum::EnumString,@@ -250,105 +174,5 @@ pub struct SessionChanges {pub attempt: Option<CaptureAttempt>,}-/// An `owner/name` GitHub repository, parsed from any of the spellings a-/// provider uses (`https://github.com/o/n`, `github.com/o/n`, `o/n`, with or-/// without a trailing `.git`).-#[derive(Debug, Clone, PartialEq, Eq, Hash)]-pub struct RepositorySlug {- /// The account the repository lives under.- pub owner: String,- /// The repository's name.- pub name: String,-}--impl RepositorySlug {- /// Parse a repository reference. `None` when the text does not name a- /// GitHub repository.- #[must_use]- pub fn parse(text: &str) -> Option<Self> {- let trimmed = text.trim().trim_end_matches('/');- let without_scheme = trimmed- .strip_prefix("https://")- .or_else(|| trimmed.strip_prefix("http://"))- .or_else(|| trimmed.strip_prefix("git@"))- .unwrap_or(trimmed);- let path = without_scheme- .strip_prefix("github.com/")- .or_else(|| without_scheme.strip_prefix("github.com:"))- .unwrap_or(without_scheme);- let path = path.strip_suffix(".git").unwrap_or(path);- let mut parts = path.split('/');- let owner = parts.next()?;- let name = parts.next()?;- if parts.next().is_some() || owner.is_empty() || name.is_empty() {- return None;- }- let valid = |part: &str| {- part.chars()- .all(|c| c.is_ascii_alphanumeric() || matches!(c, '-' | '_' | '.'))- };- if !valid(owner) || !valid(name) {- return None;- }- Some(Self {- owner: owner.to_owned(),- name: name.to_owned(),- })- }-- /// The canonical `https://github.com/{owner}/{name}` address.- #[must_use]- pub fn https_url(&self) -> String {- format!("https://github.com/{}/{}", self.owner, self.name)- }-}--impl std::fmt::Display for RepositorySlug {- fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {- write!(formatter, "{}/{}", self.owner, self.name)- }-}-#[cfg(test)]mod test;--/// A validated GitHub pull request URL, used to build a fixed-origin API request.-#[derive(Debug, Clone, PartialEq, Eq)]-pub struct PullRequestRef {- /// The repository containing the PR, including when its head is a fork.- pub repository: RepositorySlug,- /// The positive GitHub pull request number.- pub number: std::num::NonZeroU64,-}--impl PullRequestRef {- /// Accept GitHub PR links, optionally with a files/commits suffix or fragment.- pub fn parse(value: &str) -> Option<Self> {- let url = url::Url::parse(value).ok()?;- if url.scheme() != "https"- || url.host_str() != Some("github.com")- || !url.username().is_empty()- || url.password().is_some()- || url.port().is_some()- {- return None;- }- let mut segments = url.path_segments()?;- let owner = segments.next()?;- let repo = segments.next()?;- if segments.next()? != "pull" {- return None;- }- let number = segments.next()?.parse().ok()?;- match segments.next() {- None => {}- Some("") if segments.next().is_none() => {}- Some("files" | "commits" | "checks") if segments.next().is_none() => {}- _ => return None,- }- Some(Self {- repository: RepositorySlug::parse(&format!("{owner}/{repo}"))?,- number,- })- }-}diff --git a/crates/agent_changes/src/domain/model/test.rs b/crates/agent_changes/src/domain/model/test.rsindex b38a6ed136..1a9e5cc341 100644--- a/crates/agent_changes/src/domain/model/test.rs+++ b/crates/agent_changes/src/domain/model/test.rs@@ -20,40 +20,6 @@ fn captured_branch_requires_the_same_repository() {}}-#[test]-fn repository_slugs_parse_every_provider_spelling() {- for text in [- "https://github.com/macro-inc/macro",- "https://github.com/macro-inc/macro.git",- "http://github.com/macro-inc/macro/",- "github.com/macro-inc/macro",- "[email protected]:macro-inc/macro.git",- "macro-inc/macro",- ] {- let slug = RepositorySlug::parse(text).unwrap_or_else(|| panic!("{text} should parse"));- assert_eq!(slug.owner, "macro-inc", "{text}");- assert_eq!(slug.name, "macro", "{text}");- assert_eq!(slug.https_url(), "https://github.com/macro-inc/macro");- }-}--#[test]-fn repository_slugs_reject_anything_that_is_not_owner_and_name() {- for text in [- "",- "macro",- "https://github.com/macro-inc",- "https://github.com/macro-inc/macro/pull/1",- "https://gitlab.com/macro-inc/macro",- "owner/na me",- ] {- assert!(- RepositorySlug::parse(text).is_none(),- "{text:?} should not parse"- );- }-}-#[test]fn enums_round_trip_through_their_wire_strings() {assert_eq!(FileChangeKind::Renamed.to_string(), "renamed");@@ -71,35 +37,3 @@ fn enums_round_trip_through_their_wire_strings() {);assert_eq!(AttemptOutcome::NotReady.to_string(), "not_ready");}--#[test]-fn pull_request_urls_accept_github_pr_links_only() {- for suffix in [- "",- "/",- "/files",- "/commits",- "/checks",- "#discussion_r1",- "?diff=split",- ] {- let pr = PullRequestRef::parse(&format!("https://github.com/owner/repo/pull/123{suffix}"))- .unwrap();- assert_eq!(pr.repository.to_string(), "owner/repo");- assert_eq!(pr.number.get(), 123);- }- for url in [- "https://github.com/owner/repo/tree/work",- "https://github.com/owner/repo/pull/0",- "https://github.com/owner/repo/pull/-1",- "https://github.com/owner/repo/pull/1/anything",- "https://github.com/owner/repo/pull/1//anything",- "https://github.com.evil.example/owner/repo/pull/1",- "https://evil.example/owner/repo/pull/1",- "https://user:[email protected]/owner/repo/pull/1",- "http://github.com/owner/repo/pull/1",- "https://github.com/owner/repo/pull/nope",- ] {- assert!(PullRequestRef::parse(url).is_none(), "accepted {url}");- }-}diff --git a/crates/agent_changes/src/domain/patch.rs b/crates/agent_changes/src/domain/patch.rsindex b23dd8c9f0..c6d211f268 100644--- a/crates/agent_changes/src/domain/patch.rs+++ b/crates/agent_changes/src/domain/patch.rs@@ -1,347 +1,7 @@-//! Reading a git-style unified diff into per-file facts.-//!-//! Every extractor hands the service a raw patch - `git diff` output, or the-//! diff GitHub renders for a compare - and this is the one place that reads-//! it: which files it touches, what happened to each, how many lines moved,-//! and which files carry no text at all. Doing it here, once, is what keeps-//! extractors trivial and the summary identical whichever harness produced it.-//!-//! The reader is deliberately forgiving. A patch is a stream of `diff --git`-//! blocks; anything before the first block is ignored, an unknown extended-//! header line is skipped, and a block with no hunks is a rename, a mode-//! change, or a binary file rather than an error.+//! Reading a git-style unified diff into per-file facts, shared through+//! [`git_patch`] with the GitHub pull request changes.-use super::model::{ChangedFile, FileChangeKind};--#[cfg(test)]-mod test;--/// The most patch text stored for one changeset. Past this, whole files are-/// left out (largest last-in-order first) and the summary says so.-pub const MAX_PATCH_BYTES: usize = 6 * 1024 * 1024;--/// The most patch text stored for one file. A single file past this is a-/// generated artifact or a vendored tree, not something anyone reviews line-/// by line; its summary row stays, its hunks go.-pub const MAX_FILE_PATCH_BYTES: usize = 1024 * 1024;--/// One file's slice of a patch: the facts, and the text they were read from.-#[derive(Debug, Clone, PartialEq, Eq)]-pub struct ParsedFile {- /// What the diff says about the file.- pub file: ChangedFile,- /// This file's complete block, `diff --git` header through its last- /// hunk line, without a trailing newline.- pub text: String,-}--/// Split `patch` into its files, in order.-#[must_use]-pub fn parse_git_patch(patch: &str) -> Vec<ParsedFile> {- let mut files = Vec::new();- let mut current: Option<BlockReader> = None;- for line in patch.split_inclusive('\n') {- let content = line.strip_suffix('\n').unwrap_or(line);- let content = content.strip_suffix('\r').unwrap_or(content);- if let Some(rest) = content.strip_prefix("diff --git ") {- if let Some(block) = current.take() {- files.push(block.finish());- }- current = Some(BlockReader::start(rest, line));- continue;- }- if let Some(block) = current.as_mut() {- block.push(content, line);- }- }- if let Some(block) = current.take() {- files.push(block.finish());- }- files-}--/// A patch cut down to the size budget: the text to store, the files with-/// `patch_omitted` set where their hunks were dropped, and whether anything-/// was.-#[derive(Debug, Clone, PartialEq, Eq)]-pub struct BudgetedPatch {- /// The concatenated blocks that fit.- pub patch: String,- /// Every file, in patch order.- pub files: Vec<ChangedFile>,- /// Whether any file's hunks were left out.- pub truncated: bool,-}--/// Keep as much of `files` as the budget allows.-///-/// A file larger than `max_file_bytes` is always omitted; the rest are kept-/// in patch order while the running total stays within `max_total_bytes`.-/// Order is preserved rather than packing by size so the stored patch reads-/// like the diff it came from.-#[must_use]-pub fn budget_patch(- files: Vec<ParsedFile>,- max_total_bytes: usize,- max_file_bytes: usize,-) -> BudgetedPatch {- let mut patch = String::new();- let mut kept = Vec::with_capacity(files.len());- let mut truncated = false;- for parsed in files {- let ParsedFile { mut file, text } = parsed;- let block_bytes = text.len() + 1;- let fits = text.len() <= max_file_bytes && patch.len() + block_bytes <= max_total_bytes;- if fits && !file.binary {- patch.push_str(&text);- patch.push('\n');- } else if !file.binary {- file.patch_omitted = true;- truncated = true;- }- kept.push(file);- }- BudgetedPatch {- patch,- files: kept,- truncated,- }-}--/// Totals across a changeset's files.-#[must_use]-pub fn totals(files: &[ChangedFile]) -> (u32, u32) {- files.iter().fold((0, 0), |(adds, dels), file| {- (- adds.saturating_add(file.additions),- dels.saturating_add(file.deletions),- )- })-}--/// Reads one `diff --git` block line by line.-struct BlockReader {- header_old: String,- header_new: String,- old_path: Option<String>,- new_path: Option<String>,- rename_from: Option<String>,- rename_to: Option<String>,- added: bool,- deleted: bool,- binary: bool,- in_hunks: bool,- additions: u32,- deletions: u32,- text: String,-}--impl BlockReader {- fn start(header_rest: &str, raw_line: &str) -> Self {- let (header_old, header_new) = split_header_paths(header_rest);- Self {- header_old,- header_new,- old_path: None,- new_path: None,- rename_from: None,- rename_to: None,- added: false,- deleted: false,- binary: false,- in_hunks: false,- additions: 0,- deletions: 0,- text: raw_line.to_owned(),- }- }-- fn push(&mut self, content: &str, raw_line: &str) {- self.text.push_str(raw_line);- if self.in_hunks {- self.count(content);- return;- }- if content.starts_with("@@") {- self.in_hunks = true;- return;- }- if let Some(path) = content.strip_prefix("--- ") {- self.old_path = Some(strip_side(path, "a/"));- } else if let Some(path) = content.strip_prefix("+++ ") {- self.new_path = Some(strip_side(path, "b/"));- } else if content.starts_with("new file mode") {- self.added = true;- } else if content.starts_with("deleted file mode") {- self.deleted = true;- } else if let Some(from) = content.strip_prefix("rename from ") {- self.rename_from = Some(unquote(from));- } else if let Some(to) = content.strip_prefix("rename to ") {- self.rename_to = Some(unquote(to));- } else if content.starts_with("Binary files ") || content.starts_with("GIT binary patch") {- self.binary = true;- }- }-- fn count(&mut self, content: &str) {- // `--- `/`+++ ` cannot be headers here: a header only precedes the- // first hunk, and inside one a leading `-`/`+` is a changed line- // whatever follows it.- if content.starts_with('+') {- self.additions = self.additions.saturating_add(1);- } else if content.starts_with('-') {- self.deletions = self.deletions.saturating_add(1);- }- }-- fn finish(mut self) -> ParsedFile {- while self.text.ends_with('\n') || self.text.ends_with('\r') {- self.text.pop();- }- let dev_null = |path: &Option<String>| path.as_deref() == Some("/dev/null");- let added = self.added || dev_null(&self.old_path);- let deleted = self.deleted || dev_null(&self.new_path);- let renamed = self.rename_from.is_some() || self.rename_to.is_some();-- let new_name = self- .new_path- .clone()- .filter(|path| path != "/dev/null")- .or_else(|| self.rename_to.clone())- .unwrap_or_else(|| self.header_new.clone());- let old_name = self- .old_path- .clone()- .filter(|path| path != "/dev/null")- .or_else(|| self.rename_from.clone())- .unwrap_or_else(|| self.header_old.clone());-- let (kind, path, previous_path) = if deleted && !added {- (FileChangeKind::Deleted, old_name, None)- } else if added && !deleted {- (FileChangeKind::Added, new_name, None)- } else if renamed || old_name != new_name {- (FileChangeKind::Renamed, new_name, Some(old_name))- } else {- (FileChangeKind::Modified, new_name, None)- };-- ParsedFile {- file: ChangedFile {- path,- previous_path,- kind,- additions: self.additions,- deletions: self.deletions,- binary: self.binary,- patch_omitted: false,- },- text: self.text,- }- }-}--/// `a/OLD b/NEW` into `(OLD, NEW)`.-///-/// Paths can contain spaces, so the split point is the ` b/` after which-/// the two sides read the same - the common case of an unrenamed file - and-/// failing that the first ` b/`. Quoted paths are split on the closing quote.-fn split_header_paths(rest: &str) -> (String, String) {- if rest.starts_with('"')- && let Some((old, new)) = split_quoted_pair(rest)- {- return (strip_side(&old, "a/"), strip_side(&new, "b/"));- }- let candidates: Vec<usize> = rest.match_indices(" b/").map(|(index, _)| index).collect();- for index in &candidates {- let old = &rest[..*index];- let new = &rest[index + 1..];- if old.strip_prefix("a/") == new.strip_prefix("b/") {- return (strip_side(old, "a/"), strip_side(new, "b/"));- }- }- match candidates.first() {- Some(index) => (- strip_side(&rest[..*index], "a/"),- strip_side(&rest[index + 1..], "b/"),- ),- None => (strip_side(rest, "a/"), strip_side(rest, "b/")),- }-}--/// Two C-quoted strings separated by a space.-fn split_quoted_pair(rest: &str) -> Option<(String, String)> {- let mut escaped = false;- for (index, character) in rest.char_indices().skip(1) {- match character {- '\\' if !escaped => escaped = true,- '"' if !escaped => {- let first = &rest[..=index];- let second = rest.get(index + 1..)?.trim_start();- return Some((unquote(first), unquote(second)));- }- _ => escaped = false,- }- }- None-}--/// Drop git's `a/`/`b/` prefix and any `\t<timestamp>` suffix, unquoting a-/// C-quoted path on the way.-fn strip_side(path: &str, prefix: &str) -> String {- let path = path.split('\t').next().unwrap_or(path);- let unquoted = unquote(path);- if unquoted == "/dev/null" {- return unquoted;- }- unquoted- .strip_prefix(prefix)- .map(str::to_owned)- .unwrap_or(unquoted)-}--/// Undo git's C-style quoting of paths with unusual characters.-fn unquote(path: &str) -> String {- let path = path.trim();- let Some(inner) = path- .strip_prefix('"')- .and_then(|rest| rest.strip_suffix('"'))- else {- return path.to_owned();- };- let mut bytes = Vec::with_capacity(inner.len());- let mut chars = inner.chars().peekable();- while let Some(character) = chars.next() {- if character != '\\' {- let mut buffer = [0u8; 4];- bytes.extend_from_slice(character.encode_utf8(&mut buffer).as_bytes());- continue;- }- match chars.next() {- Some('n') => bytes.push(b'\n'),- Some('t') => bytes.push(b'\t'),- Some('r') => bytes.push(b'\r'),- Some('\\') => bytes.push(b'\\'),- Some('"') => bytes.push(b'"'),- Some(digit @ '0'..='7') => {- let mut value = digit.to_digit(8).unwrap_or(0);- for _ in 0..2 {- match chars.peek().and_then(|next| next.to_digit(8)) {- Some(next) => {- value = value * 8 + next;- chars.next();- }- None => break,- }- }- bytes.push(value.min(255) as u8);- }- Some(other) => {- let mut buffer = [0u8; 4];- bytes.extend_from_slice(other.encode_utf8(&mut buffer).as_bytes());- }- None => bytes.push(b'\\'),- }- }- String::from_utf8_lossy(&bytes).into_owned()-}+pub use git_patch::patch::{+ BudgetedPatch, MAX_FILE_PATCH_BYTES, MAX_PATCH_BYTES, ParsedFile, budget_patch,+ parse_git_patch, totals,+};diff --git a/crates/agent_changes/src/domain/ports.rs b/crates/agent_changes/src/domain/ports.rsindex 8dd4dd3522..0914adf327 100644--- a/crates/agent_changes/src/domain/ports.rs+++ b/crates/agent_changes/src/domain/ports.rs@@ -141,22 +141,3 @@ pub trait ChangesetBlobStore: Send + Sync + 'static {key: &PatchBlobKey,) -> impl Future<Output = Result<(), rootcause::Report>> + Send;}--/// The patch and actual base/head refs of a GitHub pull request.-#[derive(Debug, Clone, PartialEq, Eq)]-pub struct PullRequestDiff {- /// Git-style unified diff returned by GitHub.- pub patch: String,- /// The PR's target and source, including forks and non-default bases.- pub range: super::model::ChangesetRange,-}--/// Reads a pull request using the session owner's GitHub repository access.-pub trait PullRequestDiffReader: Send + Sync + 'static {- /// Read the PR itself; never fall back to a branch or workspace diff.- fn read(- &self,- user: ¯o_user_id::user_id::MacroUserIdStr<'static>,- pull_request: &super::model::PullRequestRef,- ) -> impl Future<Output = Result<PullRequestDiff, super::error::CompareError>> + Send;-}diff --git a/crates/agent_changes/src/domain/pull_request.rs b/crates/agent_changes/src/domain/pull_request.rsindex e334e021c5..9ab3fbf10d 100644--- a/crates/agent_changes/src/domain/pull_request.rs+++ b/crates/agent_changes/src/domain/pull_request.rs@@ -2,9 +2,12 @@use agent_session::domain::model::AgentSession;-use super::error::{CompareError, ExtractError};+use github_pull_requests::domain::models::GithubPullRequestDiffError;+use github_pull_requests::domain::ports::GithubPullRequestDiffReader;++use super::error::ExtractError;use super::model::{ChangesetSource, ExtractedChangeset, PullRequestRef};-use super::ports::{ChangesetExtractor, PullRequestDiffReader};+use super::ports::ChangesetExtractor;#[cfg(test)]mod test;@@ -21,7 +24,7 @@ impl<Reader> PullRequestChanges<Reader> {}}-impl<Reader: PullRequestDiffReader> ChangesetExtractor for PullRequestChanges<Reader> {+impl<Reader: GithubPullRequestDiffReader> ChangesetExtractor for PullRequestChanges<Reader> {async fn extract(&self, session: &AgentSession) -> Result<ExtractedChangeset, ExtractError> {let url = session.pull_request_url.as_deref().ok_or_else(|| {ExtractError::NotReady(@@ -37,10 +40,10 @@ impl<Reader: PullRequestDiffReader> ChangesetExtractor for PullRequestChanges<Re.owner_user().map_err(|error| ExtractError::Failed(rootcause::report!(error).into()))?;let diff = self.reader.read(owner, &pull_request).await.map_err(|error| match error {- CompareError::NotFound => ExtractError::NotReady("The linked pull request is not available on GitHub.".to_owned()),- CompareError::TooLarge => ExtractError::NotReady("This pull request is too large to load here. Review it on GitHub.".to_owned()),- CompareError::Unavailable => ExtractError::NotReady("Macro's GitHub App cannot read this pull request. Check its repository access.".to_owned()),- CompareError::Other(report) => ExtractError::Failed(report),+ GithubPullRequestDiffError::NotFound => ExtractError::NotReady("The linked pull request is not available on GitHub.".to_owned()),+ GithubPullRequestDiffError::TooLarge => ExtractError::NotReady("This pull request is too large to load here. Review it on GitHub.".to_owned()),+ GithubPullRequestDiffError::Unavailable => ExtractError::NotReady("Macro's GitHub App cannot read this pull request. Check its repository access.".to_owned()),+ GithubPullRequestDiffError::Other(error) => ExtractError::Failed(rootcause::report!("{error:#}")),})?;Ok(ExtractedChangeset {source: ChangesetSource::GithubPullRequest,diff --git a/crates/agent_changes/src/domain/pull_request/test.rs b/crates/agent_changes/src/domain/pull_request/test.rsindex 3df2225cac..d200fe852f 100644--- a/crates/agent_changes/src/domain/pull_request/test.rs+++ b/crates/agent_changes/src/domain/pull_request/test.rs@@ -1,23 +1,23 @@use super::*;use crate::domain::model::{ChangesetRange, GitRef};-use crate::domain::ports::PullRequestDiff;use agent_session::domain::model::AgentSessionId;use agent_session::testing::test_agent_session;+use github_pull_requests::domain::models::GithubPullRequestDiff;use macro_user_id::user_id::MacroUserIdStr;use std::sync::Mutex;#[derive(Default)]struct Reader {calls: Mutex<Vec<(String, PullRequestRef)>>,- error: Option<fn() -> CompareError>,+ error: Option<fn() -> GithubPullRequestDiffError>,}-impl PullRequestDiffReader for Reader {+impl GithubPullRequestDiffReader for Reader {async fn read(&self,user: &MacroUserIdStr<'static>,pr: &PullRequestRef,- ) -> Result<PullRequestDiff, CompareError> {+ ) -> Result<GithubPullRequestDiff, GithubPullRequestDiffError> {self.calls.lock().unwrap()@@ -25,7 +25,7 @@ impl PullRequestDiffReader for Reader {if let Some(error) = self.error {return Err(error());}- Ok(PullRequestDiff {+ Ok(GithubPullRequestDiff {patch: "PR diff".to_owned(),range: ChangesetRange {repository: Some(pr.repository.https_url()),@@ -87,9 +87,9 @@ async fn missing_or_invalid_pr_never_falls_back_to_a_branch_or_runtime() {#[tokio::test]async fn inaccessible_and_oversized_prs_remain_unavailable_without_a_fallback() {for error in [- || CompareError::NotFound,- || CompareError::Unavailable,- || CompareError::TooLarge,+ || GithubPullRequestDiffError::NotFound,+ || GithubPullRequestDiffError::Unavailable,+ || GithubPullRequestDiffError::TooLarge,] {let extractor = PullRequestChanges::new(Reader {error: Some(error),diff --git a/crates/agent_changes/src/inbound/axum_router.rs b/crates/agent_changes/src/inbound/axum_router.rsindex 96c8abeca4..1eafc76002 100644--- a/crates/agent_changes/src/inbound/axum_router.rs+++ b/crates/agent_changes/src/inbound/axum_router.rs@@ -22,17 +22,19 @@ use entity_access::domain::models::{EditAccessLevel, ViewAccessLevel};use entity_access::domain::ports::EntityAccessService;use entity_access::inbound::axum_extractors::AgentSessionAccessLevelExtractor;use macro_authorization::{MacroAuthorizationService, MacroAuthorizationState};-use macro_uuid::Uuid;use serde::{Deserialize, Serialize};use utoipa::ToSchema;use crate::domain::error::ChangesError;use crate::domain::model::{- AttemptOutcome, CaptureAttempt, ChangedFile, Changeset, ChangesetSource, FileChangeKind,- GitRef, SessionChanges,+ AttemptOutcome, CaptureAttempt, Changeset, ChangesetSource, SessionChanges,};use crate::domain::service::AgentChanges;+pub use git_patch::wire::{+ ChangedFileDto, ChangesetDto, ChangesetSourceDto, FileChangeKindDto, GitRefDto,+};+#[cfg(test)]mod test;@@ -114,39 +116,6 @@ where.with_state(state)}-/// What happened to a file, on the wire.-#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, ToSchema)]-#[serde(rename_all = "snake_case")]-pub enum FileChangeKindDto {- /// The file did not exist before.- Added,- /// The file exists on both sides with different contents.- Modified,- /// The file no longer exists.- Deleted,- /// The file moved; `previousPath` says from where.- Renamed,-}--impl From<FileChangeKind> for FileChangeKindDto {- fn from(kind: FileChangeKind) -> Self {- match kind {- FileChangeKind::Added => Self::Added,- FileChangeKind::Modified => Self::Modified,- FileChangeKind::Deleted => Self::Deleted,- FileChangeKind::Renamed => Self::Renamed,- }- }-}--/// The source of the captured diff, on the wire.-#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, ToSchema)]-#[serde(rename_all = "snake_case")]-pub enum ChangesetSourceDto {- /// The diff of the session's linked GitHub pull request.- GithubPullRequest,-}-impl From<ChangesetSource> for ChangesetSourceDto {fn from(source: ChangesetSource) -> Self {match source {@@ -177,96 +146,6 @@ impl From<AttemptOutcome> for CaptureOutcomeDto {}}-/// One changed file.-///-/// Clients deserialize this, so both derives are used.-#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, ToSchema)]-#[serde(rename_all = "camelCase")]-pub struct ChangedFileDto {- /// The file's path after the change, or before it for a deletion.- pub path: String,- /// Where a renamed file came from.- #[serde(default, skip_serializing_if = "Option::is_none")]- pub previous_path: Option<String>,- /// What happened to the file.- pub kind: FileChangeKindDto,- /// Lines added.- pub additions: u32,- /// Lines removed.- pub deletions: u32,- /// The diff carries no text for this file.- pub binary: bool,- /// The file's hunks were left out of the patch to fit the size budget.- pub patch_omitted: bool,-}--impl From<ChangedFile> for ChangedFileDto {- fn from(file: ChangedFile) -> Self {- Self {- path: file.path,- previous_path: file.previous_path,- kind: file.kind.into(),- additions: file.additions,- deletions: file.deletions,- binary: file.binary,- patch_omitted: file.patch_omitted,- }- }-}--/// One end of the compared range.-#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, ToSchema)]-#[serde(rename_all = "camelCase")]-pub struct GitRefDto {- /// The branch name, when known.- #[serde(default, skip_serializing_if = "Option::is_none")]- pub name: Option<String>,- /// The commit, when known.- #[serde(default, skip_serializing_if = "Option::is_none")]- pub sha: Option<String>,-}--impl From<GitRef> for GitRefDto {- fn from(git_ref: GitRef) -> Self {- Self {- name: git_ref.name,- sha: git_ref.sha,- }- }-}--/// One capture of a session's changes.-///-/// Clients deserialize this, so both derives are used.-#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, ToSchema)]-#[serde(rename_all = "camelCase")]-pub struct ChangesetDto {- /// The capture's id; changes with every capture.- pub id: Uuid,- /// Where the diff was read from.- pub source: ChangesetSourceDto,- /// `https://github.com/owner/name`, when known.- #[serde(default, skip_serializing_if = "Option::is_none")]- pub repository: Option<String>,- /// The side the work started from.- pub base: GitRefDto,- /// The side carrying the work.- pub head: GitRefDto,- /// Every changed file, in patch order.- pub files: Vec<ChangedFileDto>,- /// Lines added across all files.- pub additions: u32,- /// Lines removed across all files.- pub deletions: u32,- /// Size of the patch `GET .../changes/patch` serves; zero when nothing- /// changed.- pub patch_bytes: u64,- /// Some files' hunks were left out of the patch.- pub truncated: bool,- /// When the diff was taken.- pub captured_at: DateTime<Utc>,-}-impl From<Changeset> for ChangesetDto {fn from(changeset: Changeset) -> Self {Self {diff --git a/crates/agent_changes/src/inbound/axum_router/test.rs b/crates/agent_changes/src/inbound/axum_router/test.rsindex 554cd9a724..b69cfe1d24 100644--- a/crates/agent_changes/src/inbound/axum_router/test.rs+++ b/crates/agent_changes/src/inbound/axum_router/test.rs@@ -1,5 +1,5 @@use super::*;-use crate::domain::model::{ChangesetId, ChangesetRange};+use crate::domain::model::{ChangedFile, ChangesetId, ChangesetRange, FileChangeKind};#[test]fn the_changes_response_says_when_a_capture_is_running() {diff --git a/crates/agent_changes/src/outbound/mod.rs b/crates/agent_changes/src/outbound/mod.rsindex 6a38f5b7a3..923bf46c7f 100644--- a/crates/agent_changes/src/outbound/mod.rs+++ b/crates/agent_changes/src/outbound/mod.rs@@ -1,7 +1,5 @@//! Adapters implementing the changes ports for external systems.-/// Reading a linked pull request on GitHub.-pub mod github_pull_request;/// The summary row in Postgres.pub mod postgres;/// Patch blobs in S3.diff --git a/crates/git_patch/Cargo.toml b/crates/git_patch/Cargo.tomlnew file mode 100644index 0000000000..0f1ed615af--- /dev/null+++ b/crates/git_patch/Cargo.toml@@ -0,0 +1,17 @@+[package]+edition = "2024"+name = "git_patch"+publish = false+version = "0.1.0"++[features]+# OpenAPI schemas for the wire types.+schema = ["dep:utoipa"]++[dependencies]+chrono = { workspace = true }+serde = { workspace = true }+strum = { workspace = true }+utoipa = { workspace = true, optional = true }+uuid = { workspace = true }+workspace-hack = { version = "0.1", path = "../workspace-hack" }diff --git a/crates/git_patch/src/lib.rs b/crates/git_patch/src/lib.rsnew file mode 100644index 0000000000..0e242ddd12--- /dev/null+++ b/crates/git_patch/src/lib.rs@@ -0,0 +1,17 @@+#![deny(missing_docs)]+//! Reading git-style unified diffs into per-file facts, cutting them down to a+//! size budget, and the changeset summary types built from them.+//!+//! Agent session changes and GitHub pull request changes both read patches+//! through this crate, so a file's status and line counts, and the summary a+//! client decodes, are the same whichever produced the patch.++pub mod model;+pub mod patch;+pub mod wire;++pub use model::{ChangedFile, ChangesetRange, FileChangeKind, GitRef};+pub use patch::{+ BudgetedPatch, MAX_FILE_PATCH_BYTES, MAX_PATCH_BYTES, ParsedFile, budget_patch,+ parse_git_patch, totals,+};diff --git a/crates/git_patch/src/model.rs b/crates/git_patch/src/model.rsnew file mode 100644index 0000000000..e15a6ae1e4--- /dev/null+++ b/crates/git_patch/src/model.rs@@ -0,0 +1,81 @@+//! What a patch says about each file, and the range it compares.++use serde::{Deserialize, Serialize};++/// What happened to one file, as a diff reports it.+#[derive(+ Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, strum::Display, strum::EnumString,+)]+#[serde(rename_all = "snake_case")]+#[strum(serialize_all = "snake_case")]+pub enum FileChangeKind {+ /// The file did not exist before.+ Added,+ /// The file exists on both sides with different contents.+ Modified,+ /// The file no longer exists.+ Deleted,+ /// The file moved; `previous_path` names where from. Contents may also+ /// have changed.+ Renamed,+}++/// One file in a changeset, as much as a review's tree and headers need.+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]+#[serde(rename_all = "camelCase")]+pub struct ChangedFile {+ /// The file's path after the change - or before it, for a deletion.+ pub path: String,+ /// Where a renamed file came from.+ #[serde(default, skip_serializing_if = "Option::is_none")]+ pub previous_path: Option<String>,+ /// What happened to the file.+ pub kind: FileChangeKind,+ /// Lines added.+ pub additions: u32,+ /// Lines removed.+ pub deletions: u32,+ /// The diff carries no text for this file (an image, an archive, ...).+ #[serde(default)]+ pub binary: bool,+ /// This file's hunks were left out of the stored patch because the+ /// changeset exceeded the size budget; the summary is still complete.+ #[serde(default)]+ pub patch_omitted: bool,+}++/// One end of the compared range, as much of it as the provider told us.+#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]+#[serde(rename_all = "camelCase")]+pub struct GitRef {+ /// The branch name, when the provider names one.+ #[serde(default, skip_serializing_if = "Option::is_none")]+ pub name: Option<String>,+ /// The commit the diff was taken at, when known.+ #[serde(default, skip_serializing_if = "Option::is_none")]+ pub sha: Option<String>,+}++impl GitRef {+ /// A ref known by name only.+ #[must_use]+ pub fn named(name: impl Into<String>) -> Self {+ Self {+ name: Some(name.into()),+ sha: None,+ }+ }+}++/// What was compared with what.+#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]+#[serde(rename_all = "camelCase")]+pub struct ChangesetRange {+ /// The repository compared, as `https://github.com/owner/name`.+ #[serde(default, skip_serializing_if = "Option::is_none")]+ pub repository: Option<String>,+ /// The side the work started from.+ pub base: GitRef,+ /// The side carrying the work.+ pub head: GitRef,+}diff --git a/crates/git_patch/src/patch.rs b/crates/git_patch/src/patch.rsnew file mode 100644index 0000000000..c5ce98cc2a--- /dev/null+++ b/crates/git_patch/src/patch.rs@@ -0,0 +1,347 @@+//! Reading a git-style unified diff into per-file facts.+//!+//! Every source hands over a raw patch - `git diff` output, or the diff+//! GitHub renders for a pull request - and this is the one place that reads+//! it: which files it touches, what happened to each, how many lines moved,+//! and which files carry no text at all. Doing it here, once, is what keeps+//! the summary identical whichever source produced the patch.+//!+//! The reader is deliberately forgiving. A patch is a stream of `diff --git`+//! blocks; anything before the first block is ignored, an unknown extended+//! header line is skipped, and a block with no hunks is a rename, a mode+//! change, or a binary file rather than an error.++use crate::model::{ChangedFile, FileChangeKind};++#[cfg(test)]+mod test;++/// The most patch text stored for one changeset. Past this, whole files are+/// left out (largest last-in-order first) and the summary says so.+pub const MAX_PATCH_BYTES: usize = 6 * 1024 * 1024;++/// The most patch text stored for one file. A single file past this is a+/// generated artifact or a vendored tree, not something anyone reviews line+/// by line; its summary row stays, its hunks go.+pub const MAX_FILE_PATCH_BYTES: usize = 1024 * 1024;++/// One file's slice of a patch: the facts, and the text they were read from.+#[derive(Debug, Clone, PartialEq, Eq)]+pub struct ParsedFile {+ /// What the diff says about the file.+ pub file: ChangedFile,+ /// This file's complete block, `diff --git` header through its last+ /// hunk line, without a trailing newline.+ pub text: String,+}++/// Split `patch` into its files, in order.+#[must_use]+pub fn parse_git_patch(patch: &str) -> Vec<ParsedFile> {+ let mut files = Vec::new();+ let mut current: Option<BlockReader> = None;+ for line in patch.split_inclusive('\n') {+ let content = line.strip_suffix('\n').unwrap_or(line);+ let content = content.strip_suffix('\r').unwrap_or(content);+ if let Some(rest) = content.strip_prefix("diff --git ") {+ if let Some(block) = current.take() {+ files.push(block.finish());+ }+ current = Some(BlockReader::start(rest, line));+ continue;+ }+ if let Some(block) = current.as_mut() {+ block.push(content, line);+ }+ }+ if let Some(block) = current.take() {+ files.push(block.finish());+ }+ files+}++/// A patch cut down to the size budget: the text to store, the files with+/// `patch_omitted` set where their hunks were dropped, and whether anything+/// was.+#[derive(Debug, Clone, PartialEq, Eq)]+pub struct BudgetedPatch {+ /// The concatenated blocks that fit.+ pub patch: String,+ /// Every file, in patch order.+ pub files: Vec<ChangedFile>,+ /// Whether any file's hunks were left out.+ pub truncated: bool,+}++/// Keep as much of `files` as the budget allows.+///+/// A file larger than `max_file_bytes` is always omitted; the rest are kept+/// in patch order while the running total stays within `max_total_bytes`.+/// Order is preserved rather than packing by size so the stored patch reads+/// like the diff it came from.+#[must_use]+pub fn budget_patch(+ files: Vec<ParsedFile>,+ max_total_bytes: usize,+ max_file_bytes: usize,+) -> BudgetedPatch {+ let mut patch = String::new();+ let mut kept = Vec::with_capacity(files.len());+ let mut truncated = false;+ for parsed in files {+ let ParsedFile { mut file, text } = parsed;+ let block_bytes = text.len() + 1;+ let fits = text.len() <= max_file_bytes && patch.len() + block_bytes <= max_total_bytes;+ if fits && !file.binary {+ patch.push_str(&text);+ patch.push('\n');+ } else if !file.binary {+ file.patch_omitted = true;+ truncated = true;+ }+ kept.push(file);+ }+ BudgetedPatch {+ patch,+ files: kept,+ truncated,+ }+}++/// Totals across a changeset's files.+#[must_use]+pub fn totals(files: &[ChangedFile]) -> (u32, u32) {+ files.iter().fold((0, 0), |(adds, dels), file| {+ (+ adds.saturating_add(file.additions),+ dels.saturating_add(file.deletions),+ )+ })+}++/// Reads one `diff --git` block line by line.+struct BlockReader {+ header_old: String,+ header_new: String,+ old_path: Option<String>,+ new_path: Option<String>,+ rename_from: Option<String>,+ rename_to: Option<String>,+ added: bool,+ deleted: bool,+ binary: bool,+ in_hunks: bool,+ additions: u32,+ deletions: u32,+ text: String,+}++impl BlockReader {+ fn start(header_rest: &str, raw_line: &str) -> Self {+ let (header_old, header_new) = split_header_paths(header_rest);+ Self {+ header_old,+ header_new,+ old_path: None,+ new_path: None,+ rename_from: None,+ rename_to: None,+ added: false,+ deleted: false,+ binary: false,+ in_hunks: false,+ additions: 0,+ deletions: 0,+ text: raw_line.to_owned(),+ }+ }++ fn push(&mut self, content: &str, raw_line: &str) {+ self.text.push_str(raw_line);+ if self.in_hunks {+ self.count(content);+ return;+ }+ if content.starts_with("@@") {+ self.in_hunks = true;+ return;+ }+ if let Some(path) = content.strip_prefix("--- ") {+ self.old_path = Some(strip_side(path, "a/"));+ } else if let Some(path) = content.strip_prefix("+++ ") {+ self.new_path = Some(strip_side(path, "b/"));+ } else if content.starts_with("new file mode") {+ self.added = true;+ } else if content.starts_with("deleted file mode") {+ self.deleted = true;+ } else if let Some(from) = content.strip_prefix("rename from ") {+ self.rename_from = Some(unquote(from));+ } else if let Some(to) = content.strip_prefix("rename to ") {+ self.rename_to = Some(unquote(to));+ } else if content.starts_with("Binary files ") || content.starts_with("GIT binary patch") {+ self.binary = true;+ }+ }++ fn count(&mut self, content: &str) {+ // `--- `/`+++ ` cannot be headers here: a header only precedes the+ // first hunk, and inside one a leading `-`/`+` is a changed line+ // whatever follows it.+ if content.starts_with('+') {+ self.additions = self.additions.saturating_add(1);+ } else if content.starts_with('-') {+ self.deletions = self.deletions.saturating_add(1);+ }+ }++ fn finish(mut self) -> ParsedFile {+ while self.text.ends_with('\n') || self.text.ends_with('\r') {+ self.text.pop();+ }+ let dev_null = |path: &Option<String>| path.as_deref() == Some("/dev/null");+ let added = self.added || dev_null(&self.old_path);+ let deleted = self.deleted || dev_null(&self.new_path);+ let renamed = self.rename_from.is_some() || self.rename_to.is_some();++ let new_name = self+ .new_path+ .clone()+ .filter(|path| path != "/dev/null")+ .or_else(|| self.rename_to.clone())+ .unwrap_or_else(|| self.header_new.clone());+ let old_name = self+ .old_path+ .clone()+ .filter(|path| path != "/dev/null")+ .or_else(|| self.rename_from.clone())+ .unwrap_or_else(|| self.header_old.clone());++ let (kind, path, previous_path) = if deleted && !added {+ (FileChangeKind::Deleted, old_name, None)+ } else if added && !deleted {+ (FileChangeKind::Added, new_name, None)+ } else if renamed || old_name != new_name {+ (FileChangeKind::Renamed, new_name, Some(old_name))+ } else {+ (FileChangeKind::Modified, new_name, None)+ };++ ParsedFile {+ file: ChangedFile {+ path,+ previous_path,+ kind,+ additions: self.additions,+ deletions: self.deletions,+ binary: self.binary,+ patch_omitted: false,+ },+ text: self.text,+ }+ }+}++/// `a/OLD b/NEW` into `(OLD, NEW)`.+///+/// Paths can contain spaces, so the split point is the ` b/` after which+/// the two sides read the same - the common case of an unrenamed file - and+/// failing that the first ` b/`. Quoted paths are split on the closing quote.+fn split_header_paths(rest: &str) -> (String, String) {+ if rest.starts_with('"')+ && let Some((old, new)) = split_quoted_pair(rest)+ {+ return (strip_side(&old, "a/"), strip_side(&new, "b/"));+ }+ let candidates: Vec<usize> = rest.match_indices(" b/").map(|(index, _)| index).collect();+ for index in &candidates {+ let old = &rest[..*index];+ let new = &rest[index + 1..];+ if old.strip_prefix("a/") == new.strip_prefix("b/") {+ return (strip_side(old, "a/"), strip_side(new, "b/"));+ }+ }+ match candidates.first() {+ Some(index) => (+ strip_side(&rest[..*index], "a/"),+ strip_side(&rest[index + 1..], "b/"),+ ),+ None => (strip_side(rest, "a/"), strip_side(rest, "b/")),+ }+}++/// Two C-quoted strings separated by a space.+fn split_quoted_pair(rest: &str) -> Option<(String, String)> {+ let mut escaped = false;+ for (index, character) in rest.char_indices().skip(1) {+ match character {+ '\\' if !escaped => escaped = true,+ '"' if !escaped => {+ let first = &rest[..=index];+ let second = rest.get(index + 1..)?.trim_start();+ return Some((unquote(first), unquote(second)));+ }+ _ => escaped = false,+ }+ }+ None+}++/// Drop git's `a/`/`b/` prefix and any `\t<timestamp>` suffix, unquoting a+/// C-quoted path on the way.+fn strip_side(path: &str, prefix: &str) -> String {+ let path = path.split('\t').next().unwrap_or(path);+ let unquoted = unquote(path);+ if unquoted == "/dev/null" {+ return unquoted;+ }+ unquoted+ .strip_prefix(prefix)+ .map(str::to_owned)+ .unwrap_or(unquoted)+}++/// Undo git's C-style quoting of paths with unusual characters.+fn unquote(path: &str) -> String {+ let path = path.trim();+ let Some(inner) = path+ .strip_prefix('"')+ .and_then(|rest| rest.strip_suffix('"'))+ else {+ return path.to_owned();+ };+ let mut bytes = Vec::with_capacity(inner.len());+ let mut chars = inner.chars().peekable();+ while let Some(character) = chars.next() {+ if character != '\\' {+ let mut buffer = [0u8; 4];+ bytes.extend_from_slice(character.encode_utf8(&mut buffer).as_bytes());+ continue;+ }+ match chars.next() {+ Some('n') => bytes.push(b'\n'),+ Some('t') => bytes.push(b'\t'),+ Some('r') => bytes.push(b'\r'),+ Some('\\') => bytes.push(b'\\'),+ Some('"') => bytes.push(b'"'),+ Some(digit @ '0'..='7') => {+ let mut value = digit.to_digit(8).unwrap_or(0);+ for _ in 0..2 {+ match chars.peek().and_then(|next| next.to_digit(8)) {+ Some(next) => {+ value = value * 8 + next;+ chars.next();+ }+ None => break,+ }+ }+ bytes.push(value.min(255) as u8);+ }+ Some(other) => {+ let mut buffer = [0u8; 4];+ bytes.extend_from_slice(other.encode_utf8(&mut buffer).as_bytes());+ }+ None => bytes.push(b'\\'),+ }+ }+ String::from_utf8_lossy(&bytes).into_owned()+}diff --git a/crates/agent_changes/src/domain/patch/test.rs b/crates/git_patch/src/patch/test.rssimilarity index 100%rename from crates/agent_changes/src/domain/patch/test.rsrename to crates/git_patch/src/patch/test.rsdiff --git a/crates/git_patch/src/wire.rs b/crates/git_patch/src/wire.rsnew file mode 100644index 0000000000..20bea0d442--- /dev/null+++ b/crates/git_patch/src/wire.rs@@ -0,0 +1,136 @@+//! The changeset summary on the wire, shared by every route that serves one so+//! clients decode a single shape.++use chrono::{DateTime, Utc};+use serde::{Deserialize, Serialize};+use uuid::Uuid;++use crate::model::{ChangedFile, FileChangeKind, GitRef};++/// What happened to a file, on the wire.+#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]+#[cfg_attr(feature = "schema", derive(utoipa::ToSchema))]+#[serde(rename_all = "snake_case")]+pub enum FileChangeKindDto {+ /// The file did not exist before.+ Added,+ /// The file exists on both sides with different contents.+ Modified,+ /// The file no longer exists.+ Deleted,+ /// The file moved; `previousPath` says from where.+ Renamed,+}++impl From<FileChangeKind> for FileChangeKindDto {+ fn from(kind: FileChangeKind) -> Self {+ match kind {+ FileChangeKind::Added => Self::Added,+ FileChangeKind::Modified => Self::Modified,+ FileChangeKind::Deleted => Self::Deleted,+ FileChangeKind::Renamed => Self::Renamed,+ }+ }+}++/// The source of a changeset's diff, on the wire.+#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]+#[cfg_attr(feature = "schema", derive(utoipa::ToSchema))]+#[serde(rename_all = "snake_case")]+pub enum ChangesetSourceDto {+ /// The diff of a GitHub pull request.+ GithubPullRequest,+}++/// One changed file.+///+/// Clients deserialize this, so both derives are used.+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]+#[cfg_attr(feature = "schema", derive(utoipa::ToSchema))]+#[serde(rename_all = "camelCase")]+pub struct ChangedFileDto {+ /// The file's path after the change, or before it for a deletion.+ pub path: String,+ /// Where a renamed file came from.+ #[serde(default, skip_serializing_if = "Option::is_none")]+ pub previous_path: Option<String>,+ /// What happened to the file.+ pub kind: FileChangeKindDto,+ /// Lines added.+ pub additions: u32,+ /// Lines removed.+ pub deletions: u32,+ /// The diff carries no text for this file.+ pub binary: bool,+ /// The file's hunks were left out of the patch to fit the size budget.+ pub patch_omitted: bool,+}++impl From<ChangedFile> for ChangedFileDto {+ fn from(file: ChangedFile) -> Self {+ Self {+ path: file.path,+ previous_path: file.previous_path,+ kind: file.kind.into(),+ additions: file.additions,+ deletions: file.deletions,+ binary: file.binary,+ patch_omitted: file.patch_omitted,+ }+ }+}++/// One end of the compared range.+#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]+#[cfg_attr(feature = "schema", derive(utoipa::ToSchema))]+#[serde(rename_all = "camelCase")]+pub struct GitRefDto {+ /// The branch name, when known.+ #[serde(default, skip_serializing_if = "Option::is_none")]+ pub name: Option<String>,+ /// The commit, when known.+ #[serde(default, skip_serializing_if = "Option::is_none")]+ pub sha: Option<String>,+}++impl From<GitRef> for GitRefDto {+ fn from(git_ref: GitRef) -> Self {+ Self {+ name: git_ref.name,+ sha: git_ref.sha,+ }+ }+}++/// One changeset: the files a patch touches and what happened to each.+///+/// Clients deserialize this, so both derives are used.+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]+#[cfg_attr(feature = "schema", derive(utoipa::ToSchema))]+#[serde(rename_all = "camelCase")]+pub struct ChangesetDto {+ /// The changeset's id; a different id means different changes.+ pub id: Uuid,+ /// Where the diff was read from.+ pub source: ChangesetSourceDto,+ /// `https://github.com/owner/name`, when known.+ #[serde(default, skip_serializing_if = "Option::is_none")]+ pub repository: Option<String>,+ /// The side the work started from.+ pub base: GitRefDto,+ /// The side carrying the work.+ pub head: GitRefDto,+ /// Every changed file, in patch order.+ pub files: Vec<ChangedFileDto>,+ /// Lines added across all files.+ pub additions: u32,+ /// Lines removed across all files.+ pub deletions: u32,+ /// Size of the patch the matching patch route serves; zero when nothing+ /// changed.+ pub patch_bytes: u64,+ /// Some files' hunks were left out of the patch.+ pub truncated: bool,+ /// When the diff was taken.+ pub captured_at: DateTime<Utc>,+}diff --git a/crates/github/src/outbound.rs b/crates/github/src/outbound.rsindex 1371f7978c..fffc471acc 100644--- a/crates/github/src/outbound.rs+++ b/crates/github/src/outbound.rs@@ -10,6 +10,8 @@ pub mod github_sync_client;pub mod pg_github_repo;#[cfg(feature = "sync")]pub mod pg_github_sync_repo;+#[cfg(feature = "sync")]+pub mod pull_request_diff;#[cfg(any(feature = "link", feature = "sync"))]pub(crate) mod pull_request_metadata;diff --git a/crates/agent_changes/src/outbound/github_pull_request.rs b/crates/github/src/outbound/pull_request_diff.rssimilarity index 62%rename from crates/agent_changes/src/outbound/github_pull_request.rsrename to crates/github/src/outbound/pull_request_diff.rsindex ddaa004905..cd1beecdc7 100644--- a/crates/agent_changes/src/outbound/github_pull_request.rs+++ b/crates/github/src/outbound/pull_request_diff.rs@@ -1,17 +1,18 @@-//! Read GitHub pull request metadata and its diff with a repository-scoped App token.+//! Read a GitHub pull request's metadata and its diff with a repository-scoped App token.use std::time::Duration;-use github::domain::models::GithubError;-use github::domain::ports::{GithubSyncClient, GithubSyncRepo};-use github::domain::service::InstallationTokenService;+use github_pull_requests::domain::models::{+ ChangesetRange, GitRef, GithubPullRequestDiff, GithubPullRequestDiffError, PullRequestRef,+};+use github_pull_requests::domain::ports::GithubPullRequestDiffReader;use macro_user_id::user_id::MacroUserIdStr;use reqwest::StatusCode;use serde::Deserialize;-use crate::domain::error::CompareError;-use crate::domain::model::{ChangesetRange, GitRef, PullRequestRef};-use crate::domain::ports::{PullRequestDiff, PullRequestDiffReader};+use crate::domain::models::GithubError;+use crate::domain::ports::{GithubSyncClient, GithubSyncRepo};+use crate::domain::service::InstallationTokenService;#[cfg(test)]mod test;@@ -20,17 +21,17 @@ const GITHUB_API_BASE_URL: &str = "https://api.github.com";const READ_PERMISSIONS: &[(&str, &str)] = &[("pull_requests", "read")];const REQUEST_TIMEOUT: Duration = Duration::from_secs(60);-/// GitHub REST adapter for a linked pull request's diff.-pub struct GithubPullRequestDiff<Installations, Client> {+/// GitHub REST adapter for a pull request's diff.+pub struct GithubPullRequestDiffClient<Installations, Client> {tokens: InstallationTokenService<Installations, Client>,http: reqwest::Client,api_base_url: String,}impl<Installations: GithubSyncRepo, Client: GithubSyncClient>- GithubPullRequestDiff<Installations, Client>+ GithubPullRequestDiffClient<Installations, Client>{- /// Mint repository-scoped tokens through the owning GitHub domain service.+ /// Mint repository-scoped tokens through `tokens`.pub fn new(tokens: InstallationTokenService<Installations, Client>) -> Self {Self::with_api_base_url(tokens, GITHUB_API_BASE_URL.to_owned())}@@ -50,7 +51,12 @@ impl<Installations: GithubSyncRepo, Client: GithubSyncClient>}}- async fn get(&self, url: &str, token: &str, accept: &str) -> Result<String, CompareError> {+ async fn get(+ &self,+ url: &str,+ token: &str,+ accept: &str,+ ) -> Result<String, GithubPullRequestDiffError> {let response = self.http.get(url)@@ -60,12 +66,12 @@ impl<Installations: GithubSyncRepo, Client: GithubSyncClient>.header("X-GitHub-Api-Version", "2022-11-28").send().await- .map_err(|error| CompareError::Other(rootcause::report!(error).into()))?;+ .map_err(|error| GithubPullRequestDiffError::Other(error.into()))?;let status = response.status();let body = response.text().await- .map_err(|error| CompareError::Other(rootcause::report!(error).into()))?;+ .map_err(|error| GithubPullRequestDiffError::Other(error.into()))?;if !status.is_success() {return Err(status_error(status, &body));}@@ -95,23 +101,25 @@ impl From<PullRequestBranch> for GitRef {}}-impl<Installations: GithubSyncRepo, Client: GithubSyncClient> PullRequestDiffReader- for GithubPullRequestDiff<Installations, Client>+impl<Installations: GithubSyncRepo, Client: GithubSyncClient> GithubPullRequestDiffReader+ for GithubPullRequestDiffClient<Installations, Client>{#[tracing::instrument(skip_all, err)]async fn read(&self,user: &MacroUserIdStr<'static>,pull_request: &PullRequestRef,- ) -> Result<PullRequestDiff, CompareError> {+ ) -> Result<GithubPullRequestDiff, GithubPullRequestDiffError> {let repository = &pull_request.repository;let token = self.tokens.for_repository(user, &repository.owner, &repository.name, READ_PERMISSIONS).await.map_err(|error| match error {- GithubError::RepositoryUnavailable => CompareError::Unavailable,- other => CompareError::Other(rootcause::report!("github token: {other}")),+ GithubError::RepositoryUnavailable => GithubPullRequestDiffError::Unavailable,+ other => {+ GithubPullRequestDiffError::Other(anyhow::anyhow!("github token: {other}"))+ }})?;let url = format!("{}/repos/{}/{}/pulls/{}",@@ -121,12 +129,12 @@ impl<Installations: GithubSyncRepo, Client: GithubSyncClient> PullRequestDiffRea.get(&url, &token.token, "application/vnd.github+json").await?;let metadata: PullRequestMetadata = serde_json::from_str(&metadata).map_err(|error| {- CompareError::Other(rootcause::report!("pull request metadata: {error}"))+ GithubPullRequestDiffError::Other(anyhow::anyhow!("pull request metadata: {error}"))})?;let patch = self.get(&url, &token.token, "application/vnd.github.diff").await?;- Ok(PullRequestDiff {+ Ok(GithubPullRequestDiff {patch,range: ChangesetRange {repository: Some(repository.https_url()),@@ -137,12 +145,12 @@ impl<Installations: GithubSyncRepo, Client: GithubSyncClient> PullRequestDiffRea}}-fn status_error(status: StatusCode, body: &str) -> CompareError {+fn status_error(status: StatusCode, body: &str) -> GithubPullRequestDiffError {match status {- StatusCode::NOT_FOUND => CompareError::NotFound,- StatusCode::NOT_ACCEPTABLE => CompareError::TooLarge,- StatusCode::UNAUTHORIZED | StatusCode::FORBIDDEN => CompareError::Unavailable,- other => CompareError::Other(rootcause::report!(+ StatusCode::NOT_FOUND => GithubPullRequestDiffError::NotFound,+ StatusCode::NOT_ACCEPTABLE => GithubPullRequestDiffError::TooLarge,+ StatusCode::UNAUTHORIZED | StatusCode::FORBIDDEN => GithubPullRequestDiffError::Unavailable,+ other => GithubPullRequestDiffError::Other(anyhow::anyhow!("github answered {other}: {}",body.chars().take(300).collect::<String>())),diff --git a/crates/agent_changes/src/outbound/github_pull_request/test.rs b/crates/github/src/outbound/pull_request_diff/test.rssimilarity index 83%rename from crates/agent_changes/src/outbound/github_pull_request/test.rsrename to crates/github/src/outbound/pull_request_diff/test.rsindex cc3dc5a73e..7dbb4f7732 100644--- a/crates/agent_changes/src/outbound/github_pull_request/test.rs+++ b/crates/github/src/outbound/pull_request_diff/test.rs@@ -18,22 +18,22 @@ fn metadata_uses_the_prs_actual_base_and_head_including_a_fork() {fn inaccessible_or_oversized_diffs_map_to_unavailable_states() {assert!(matches!(status_error(StatusCode::NOT_FOUND, ""),- CompareError::NotFound+ GithubPullRequestDiffError::NotFound));assert!(matches!(status_error(StatusCode::FORBIDDEN, ""),- CompareError::Unavailable+ GithubPullRequestDiffError::Unavailable));assert!(matches!(status_error(StatusCode::UNAUTHORIZED, ""),- CompareError::Unavailable+ GithubPullRequestDiffError::Unavailable));assert!(matches!(status_error(StatusCode::NOT_ACCEPTABLE, ""),- CompareError::TooLarge+ GithubPullRequestDiffError::TooLarge));assert!(matches!(status_error(StatusCode::BAD_GATEWAY, ""),- CompareError::Other(_)+ GithubPullRequestDiffError::Other(_)));}diff --git a/crates/github_pull_requests/Cargo.toml b/crates/github_pull_requests/Cargo.tomlindex cad30b8adc..05d30157ec 100644--- a/crates/github_pull_requests/Cargo.toml+++ b/crates/github_pull_requests/Cargo.toml@@ -31,6 +31,7 @@ chrono = { workspace = true }entity_access = { path = "../entity_access", optional = true, default-features = false, features = ["ports"] }filter_ast = { path = "../filter_ast", optional = true }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_user_id = { path = "../macro_user_id" }@@ -41,6 +42,7 @@ serde_json = { workspace = true }sqlx = { workspace = true, optional = true }thiserror = { workspace = true }tracing = { workspace = true }+url = { workspace = true }utoipa = { workspace = true, optional = true }uuid = { workspace = true }workspace-hack = { version = "0.1", path = "../workspace-hack" }diff --git a/crates/github_pull_requests/src/domain/models.rs b/crates/github_pull_requests/src/domain/models.rsindex 208da45603..fbd83f2db0 100644--- a/crates/github_pull_requests/src/domain/models.rs+++ b/crates/github_pull_requests/src/domain/models.rs@@ -3,12 +3,17 @@#[cfg(test)]mod test;+mod diff;mod key;mod pull_request;use chrono::{DateTime, Utc};use foreign_entity::domain::models::{ForeignEntity, ForeignEntityError, SourceId};+pub use diff::{+ ChangesetRange, GitRef, GithubPullRequestDiff, GithubPullRequestDiffError, PullRequestRef,+ RepositorySlug,+};pub use key::GithubKey;pub use pull_request::{EnrichGithubPullRequestsProxyRequest, EnrichGithubPullRequestsResponse,diff --git a/crates/github_pull_requests/src/domain/models/diff.rs b/crates/github_pull_requests/src/domain/models/diff.rsnew file mode 100644index 0000000000..965fef11ea--- /dev/null+++ b/crates/github_pull_requests/src/domain/models/diff.rs@@ -0,0 +1,132 @@+//! Reading a pull request's diff: which pull request, and what comes back.++pub use git_patch::{ChangesetRange, GitRef};++#[cfg(test)]+mod test;++/// An `owner/name` GitHub repository, parsed from any of the spellings a+/// provider uses (`https://github.com/o/n`, `github.com/o/n`, `o/n`, with or+/// without a trailing `.git`).+#[derive(Debug, Clone, PartialEq, Eq, Hash)]+pub struct RepositorySlug {+ /// The account the repository lives under.+ pub owner: String,+ /// The repository's name.+ pub name: String,+}++impl RepositorySlug {+ /// Parse a repository reference. `None` when the text does not name a+ /// GitHub repository.+ #[must_use]+ pub fn parse(text: &str) -> Option<Self> {+ let trimmed = text.trim().trim_end_matches('/');+ let without_scheme = trimmed+ .strip_prefix("https://")+ .or_else(|| trimmed.strip_prefix("http://"))+ .or_else(|| trimmed.strip_prefix("git@"))+ .unwrap_or(trimmed);+ let path = without_scheme+ .strip_prefix("github.com/")+ .or_else(|| without_scheme.strip_prefix("github.com:"))+ .unwrap_or(without_scheme);+ let path = path.strip_suffix(".git").unwrap_or(path);+ let mut parts = path.split('/');+ let owner = parts.next()?;+ let name = parts.next()?;+ if parts.next().is_some() || owner.is_empty() || name.is_empty() {+ return None;+ }+ let valid = |part: &str| {+ part.chars()+ .all(|c| c.is_ascii_alphanumeric() || matches!(c, '-' | '_' | '.'))+ };+ if !valid(owner) || !valid(name) {+ return None;+ }+ Some(Self {+ owner: owner.to_owned(),+ name: name.to_owned(),+ })+ }++ /// The canonical `https://github.com/{owner}/{name}` address.+ #[must_use]+ pub fn https_url(&self) -> String {+ format!("https://github.com/{}/{}", self.owner, self.name)+ }+}++impl std::fmt::Display for RepositorySlug {+ fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {+ write!(formatter, "{}/{}", self.owner, self.name)+ }+}++/// A validated GitHub pull request URL, used to build a fixed-origin API request.+#[derive(Debug, Clone, PartialEq, Eq)]+pub struct PullRequestRef {+ /// The repository containing the PR, including when its head is a fork.+ pub repository: RepositorySlug,+ /// The positive GitHub pull request number.+ pub number: std::num::NonZeroU64,+}++impl PullRequestRef {+ /// Accept GitHub PR links, optionally with a files/commits suffix or fragment.+ pub fn parse(value: &str) -> Option<Self> {+ let url = url::Url::parse(value).ok()?;+ if url.scheme() != "https"+ || url.host_str() != Some("github.com")+ || !url.username().is_empty()+ || url.password().is_some()+ || url.port().is_some()+ {+ return None;+ }+ let mut segments = url.path_segments()?;+ let owner = segments.next()?;+ let repo = segments.next()?;+ if segments.next()? != "pull" {+ return None;+ }+ let number = segments.next()?.parse().ok()?;+ match segments.next() {+ None => {}+ Some("") if segments.next().is_none() => {}+ Some("files" | "commits" | "checks") if segments.next().is_none() => {}+ _ => return None,+ }+ Some(Self {+ repository: RepositorySlug::parse(&format!("{owner}/{repo}"))?,+ number,+ })+ }+}++/// A pull request's patch and the base and head it actually compares.+#[derive(Debug, Clone, PartialEq, Eq)]+pub struct GithubPullRequestDiff {+ /// Git-style unified diff returned by GitHub.+ pub patch: String,+ /// The pull request's target and source, including forks and non-default bases.+ pub range: ChangesetRange,+}++/// Why a pull request's diff could not be read.+#[derive(Debug, thiserror::Error)]+pub enum GithubPullRequestDiffError {+ /// GitHub cannot find the pull request.+ #[error("the pull request does not exist on GitHub")]+ NotFound,+ /// GitHub refuses to render a diff this large.+ #[error("the diff is too large for GitHub to render")]+ TooLarge,+ /// The caller may not reach the repository through the GitHub App.+ #[error("the repository is not reachable through the configured GitHub App")]+ Unavailable,+ /// Anything else.+ #[error(transparent)]+ Other(anyhow::Error),+}diff --git a/crates/github_pull_requests/src/domain/models/diff/test.rs b/crates/github_pull_requests/src/domain/models/diff/test.rsnew file mode 100644index 0000000000..d9ab8f4b45--- /dev/null+++ b/crates/github_pull_requests/src/domain/models/diff/test.rs@@ -0,0 +1,67 @@+use super::*;++#[test]+fn repository_slugs_parse_every_provider_spelling() {+ for text in [+ "https://github.com/macro-inc/macro",+ "https://github.com/macro-inc/macro.git",+ "http://github.com/macro-inc/macro/",+ "github.com/macro-inc/macro",+ "[email protected]:macro-inc/macro.git",+ "macro-inc/macro",+ ] {+ let slug = RepositorySlug::parse(text).unwrap_or_else(|| panic!("{text} should parse"));+ assert_eq!(slug.owner, "macro-inc", "{text}");+ assert_eq!(slug.name, "macro", "{text}");+ assert_eq!(slug.https_url(), "https://github.com/macro-inc/macro");+ }+}++#[test]+fn repository_slugs_reject_anything_that_is_not_owner_and_name() {+ for text in [+ "",+ "macro",+ "https://github.com/macro-inc",+ "https://github.com/macro-inc/macro/pull/1",+ "https://gitlab.com/macro-inc/macro",+ "owner/na me",+ ] {+ assert!(+ RepositorySlug::parse(text).is_none(),+ "{text:?} should not parse"+ );+ }+}++#[test]+fn pull_request_urls_accept_github_pr_links_only() {+ for suffix in [+ "",+ "/",+ "/files",+ "/commits",+ "/checks",+ "#discussion_r1",+ "?diff=split",+ ] {+ let pr = PullRequestRef::parse(&format!("https://github.com/owner/repo/pull/123{suffix}"))+ .unwrap();+ assert_eq!(pr.repository.to_string(), "owner/repo");+ assert_eq!(pr.number.get(), 123);+ }+ for url in [+ "https://github.com/owner/repo/tree/work",+ "https://github.com/owner/repo/pull/0",+ "https://github.com/owner/repo/pull/-1",+ "https://github.com/owner/repo/pull/1/anything",+ "https://github.com/owner/repo/pull/1//anything",+ "https://github.com.evil.example/owner/repo/pull/1",+ "https://evil.example/owner/repo/pull/1",+ "https://user:[email protected]/owner/repo/pull/1",+ "http://github.com/owner/repo/pull/1",+ "https://github.com/owner/repo/pull/nope",+ ] {+ assert!(PullRequestRef::parse(url).is_none(), "accepted {url}");+ }+}diff --git a/crates/github_pull_requests/src/domain/ports.rs b/crates/github_pull_requests/src/domain/ports.rsindex 976015c367..7d34032d7f 100644--- a/crates/github_pull_requests/src/domain/ports.rs+++ b/crates/github_pull_requests/src/domain/ports.rs@@ -11,10 +11,10 @@ use item_filters::ast::{LiteralTree, github_pull_request::GithubPullRequestLiteruse macro_user_id::user_id::MacroUserIdStr;use super::models::{- EnrichedGithubPullRequest, GithubPullRequestError, GithubPullRequestFacets,- GithubPullRequestRow, GithubPullRequestSortDirection, GithubPullRequestWrite,- GithubRepositoryIdentity, StoredGithubPullRequest, UpsertGithubPullRequest,- UpsertedGithubPullRequest,+ EnrichedGithubPullRequest, GithubPullRequestDiff, GithubPullRequestDiffError,+ GithubPullRequestError, GithubPullRequestFacets, GithubPullRequestRow,+ GithubPullRequestSortDirection, GithubPullRequestWrite, GithubRepositoryIdentity,+ PullRequestRef, StoredGithubPullRequest, UpsertGithubPullRequest, UpsertedGithubPullRequest,};/// Stores GitHub pull requests as foreign entity records, one record per user or team the pull@@ -144,6 +144,17 @@ pub trait GithubPullRequestListingRepository: Send + Sync + 'static {) -> impl Future<Output = Result<Vec<ForeignEntity>, Self::Err>> + Send;}+/// Reads a pull request's diff from GitHub with a user's repository access.+pub trait GithubPullRequestDiffReader: Send + Sync + 'static {+ /// The pull request's patch and actual base and head, read through the GitHub App on+ /// `user`'s behalf. Never falls back to a branch or workspace diff.+ fn read(+ &self,+ user: &MacroUserIdStr<'static>,+ pull_request: &PullRequestRef,+ ) -> impl Future<Output = Result<GithubPullRequestDiff, GithubPullRequestDiffError>> + Send;+}+/// Aggregate views over the GitHub pull requests a caller can see.pub trait GithubPullRequestFacetService: Send + Sync + 'static {/// Repositories and authors among the pull requests visible to `user`, plus those visiblediff --git a/packages/sdk/generated/agent-harness/types.gen.ts b/packages/sdk/generated/agent-harness/types.gen.tsindex 78e19072c5..9042979cb8 100644--- a/packages/sdk/generated/agent-harness/types.gen.ts+++ b/packages/sdk/generated/agent-harness/types.gen.ts@@ -581,7 +581,7 @@ export type ChangedFileDto = {};/**- * One capture of a session's changes.+ * One changeset: the files a patch touches and what happened to each.** Clients deserialize this, so both derives are used.*/@@ -611,11 +611,11 @@ export type ChangesetDto = {*/head: GitRefDto;/**- * The capture's id; changes with every capture.+ * The changeset's id; a different id means different changes.*/id: string;/**- * Size of the patch `GET .../changes/patch` serves; zero when nothing+ * Size of the patch the matching patch route serves; zero when nothing* changed.*/patchBytes: number;@@ -634,7 +634,7 @@ export type ChangesetDto = {};/**- * The source of the captured diff, on the wire.+ * The source of a changeset's diff, on the wire.*/export type ChangesetSourceDto = 'github_pull_request';diff --git a/packages/sdk/specs/agent-harness.json b/packages/sdk/specs/agent-harness.jsonindex 30957b96f7..83756cb3b0 100644--- a/packages/sdk/specs/agent-harness.json+++ b/packages/sdk/specs/agent-harness.json@@ -2724,7 +2724,7 @@},"ChangesetDto": {"type": "object",- "description": "One capture of a session's changes.\n\nClients deserialize this, so both derives are used.",+ "description": "One changeset: the files a patch touches and what happened to each.\n\nClients deserialize this, so both derives are used.","required": ["id","source",@@ -2773,12 +2773,12 @@"id": {"type": "string","format": "uuid",- "description": "The capture's id; changes with every capture."+ "description": "The changeset's id; a different id means different changes."},"patchBytes": {"type": "integer","format": "int64",- "description": "Size of the patch `GET .../changes/patch` serves; zero when nothing\nchanged.",+ "description": "Size of the patch the matching patch route serves; zero when nothing\nchanged.","minimum": 0},"repository": {@@ -2797,7 +2797,7 @@},"ChangesetSourceDto": {"type": "string",- "description": "The source of the captured diff, on the wire.",+ "description": "The source of a changeset's diff, on the wire.","enum": ["github_pull_request"]},"ChannelSharePermission": {diff --git a/services/agent_harness_service/src/main.rs b/services/agent_harness_service/src/main.rsindex 4877ca0fba..48d2dd5adb 100644--- a/services/agent_harness_service/src/main.rs+++ b/services/agent_harness_service/src/main.rs@@ -30,7 +30,6 @@ use std::{future::Future, pin::Pin, sync::Arc};use agent_changes::domain::pull_request::PullRequestChanges;use agent_changes::domain::service::{AgentChangesService, CaptureOnTurnEnd};use agent_changes::inbound::axum_router::AgentChangesRouterState;-use agent_changes::outbound::github_pull_request::GithubPullRequestDiff;use agent_changes::outbound::postgres::PgChangesetRepo;use agent_changes::outbound::s3::S3ChangesetBlobStore;use agent_egress::domain::service::EgressServiceImpl;@@ -114,6 +113,7 @@ use github::domain::service::{};use github::outbound::github_sync_client::GithubSyncClientImpl;use github::outbound::pg_github_sync_repo::PgGithubSyncRepo;+use github::outbound::pull_request_diff::GithubPullRequestDiffClient;use harness_bindings::{PgHarnessBindings, PgHarnessPresence};use harnesses::outbound::pg_harness_repo::PgHarnessRepo;use kafka_util::{GroupName, KafkaEventConsumer, consumer_span, record_span_error};@@ -951,15 +951,16 @@ async fn run() -> anyhow::Result<()> {MacrodModels::new(Arc::clone(&runtimes), redis.clone(), model_probe_timeout);// Capture only the session's linked GitHub pull request, for every harness.- let changes_extractor =- PullRequestChanges::new(GithubPullRequestDiff::new(InstallationTokenService::new(+ let changes_extractor = PullRequestChanges::new(GithubPullRequestDiffClient::new(+ InstallationTokenService::new(InstallationTokenConfig {client_id: config.github_sync_app_client_id.clone(),private_key_pem: config.github_sync_app_pem_secret_key.as_ref().to_owned(),},PgGithubSyncRepo::new(pool.clone()),GithubSyncClientImpl::default(),- )));+ ),+ ));let changes = AgentChangesService::new(session_repo.clone(),changes_extractor,