From 165fb5a72238ad444481f24ddd18d93e9f2a649a Mon Sep 17 00:00:00 2001 From: Nam Rodriguez Date: Sat, 25 Jul 2026 12:57:30 -0400 Subject: [PATCH] feat(repos): human-assigned repo roles to disambiguate Jira routing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit CPM-7881 (a Tailwind token change) mis-routed because two repos both use Tailwind — code evidence tied, and the LLM broke the tie on stale auto-generated descriptions (speedboat's omitted Tailwind though it has a tailwind.config). Descriptions are unreliable; a human-set signal isn't. Teams can now assign each repo a ROLE — web, backend, mobile, cms, infra, data — and the Jira repo-selector treats it as AUTHORITATIVE: it classifies the ticket's domain and picks the repo whose role matches, overriding descriptions and incidental code matches. Unset repos fall back to the code-evidence + LLM path unchanged. - worker: plan::fetch_team_repo_roles + select_repo feeds roles into the prompt as the dominant, override-descriptions signal. - gateway: GET /api/repos returns `role`; POST /api/repos/:owner/:name/role sets/clears it (admin+, validated against the 6 roles, must be a real repo). --- services/gateway/src/main.rs | 1 + services/gateway/src/routes/api.rs | 49 ++++++++++++++++++++++++++++ services/worker/src/passes/plan.rs | 43 ++++++++++++++++++++++++ services/worker/src/passes/triage.rs | 33 +++++++++++++++++-- 4 files changed, 123 insertions(+), 3 deletions(-) diff --git a/services/gateway/src/main.rs b/services/gateway/src/main.rs index 8e9ee0b6b..90bc46371 100644 --- a/services/gateway/src/main.rs +++ b/services/gateway/src/main.rs @@ -124,6 +124,7 @@ async fn main() -> Result<(), Error> { "/repos/:owner/:name/regenerate", post(routes::api::regenerate_repo), ) + .route("/repos/:owner/:name/role", post(routes::api::set_repo_role)) .route( "/integrations/jira/check", get(routes::api::get_jira_integration_check) diff --git a/services/gateway/src/routes/api.rs b/services/gateway/src/routes/api.rs index b3a03149f..2bedcd0c5 100644 --- a/services/gateway/src/routes/api.rs +++ b/services/gateway/src/routes/api.rs @@ -1450,6 +1450,7 @@ pub async fn list_repos( "ticket_source": item.get("ticket_source").and_then(|v| v.as_s().ok()), "onboard_status": item.get("onboard_status").and_then(|v| v.as_s().ok()), "onboard_error": item.get("onboard_error").and_then(|v| v.as_s().ok()), + "role": item.get("role").and_then(|v| v.as_s().ok()), })) }) .collect(); @@ -2491,6 +2492,54 @@ pub async fn validate_jira_integration_payload( }))) } +/// Valid repo role designations. A repo's role tells the Jira repo-selector which +/// repo owns a domain, so an ambiguous ticket (e.g. a Tailwind change matching two +/// frontends) resolves to the team's designated repo instead of an LLM guess. +pub const VALID_REPO_ROLES: &[&str] = &["web", "backend", "mobile", "cms", "infra", "data"]; + +/// POST /api/repos/:owner/:name/role — set (or clear, with "") a repo's role. +pub async fn set_repo_role( + State(state): State>, + Extension(claims): Extension, + axum::extract::Path((owner, name)): axum::extract::Path<(String, String)>, + Json(body): Json, +) -> Result { + claims.require_role(3)?; // admin+ + let repo = format!("{owner}/{name}"); + validate_repo_name(&repo)?; + let role = body["role"].as_str().unwrap_or("").trim().to_lowercase(); + let now = chrono::Utc::now().to_rfc3339(); + + let base = state + .dynamo + .update_item() + .table_name(&state.config.repos_table_name) + .key("pk", attr_s(&claims.team_id)) + .key("sk", attr_s(&format!("REPO#{repo}"))) + // Only designate a repo that exists (was synced) — never mint a stub. + .condition_expression("attribute_exists(sk)") + .expression_attribute_names("#role", "role") + .expression_attribute_values(":ua", attr_s(&now)); + + let update = if role.is_empty() { + base.update_expression("SET updated_at = :ua REMOVE #role") + } else { + if !VALID_REPO_ROLES.contains(&role.as_str()) { + return Err(StatusCode::BAD_REQUEST); + } + base.update_expression("SET #role = :role, updated_at = :ua") + .expression_attribute_values(":role", attr_s(&role)) + }; + + update.send().await.map_err(|e| { + error!("Failed to set repo role for {repo}: {e}"); + // ConditionalCheckFailed (unknown repo) is a client error. + StatusCode::BAD_REQUEST + })?; + + Ok(StatusCode::OK) +} + /// POST /api/repos/:owner/:name — update repo config. pub async fn update_repo( State(state): State>, diff --git a/services/worker/src/passes/plan.rs b/services/worker/src/passes/plan.rs index e94200068..1337f7608 100644 --- a/services/worker/src/passes/plan.rs +++ b/services/worker/src/passes/plan.rs @@ -650,6 +650,49 @@ pub async fn fetch_team_repos(state: &WorkerState, team_id: &str) -> Vec } } +/// Fetch human-assigned repo roles (repo_name → role) for a team. Only repos +/// that have a role set are returned. Used by Jira repo selection to resolve +/// ambiguous tickets to the team's designated repo for that domain. +pub async fn fetch_team_repo_roles( + state: &WorkerState, + team_id: &str, +) -> std::collections::HashMap { + let result = state + .dynamo + .query() + .table_name(&state.config.repos_table_name) + .key_condition_expression("pk = :pk AND begins_with(sk, :prefix)") + .expression_attribute_values( + ":pk", + aws_sdk_dynamodb::types::AttributeValue::S(team_id.to_string()), + ) + .expression_attribute_values( + ":prefix", + aws_sdk_dynamodb::types::AttributeValue::S("REPO#".to_string()), + ) + .send() + .await; + + match result { + Ok(output) => output + .items() + .iter() + .filter_map(|item| { + let name = item.get("repo_name").and_then(|v| v.as_s().ok())?; + let role = item + .get("role") + .and_then(|v| v.as_s().ok()) + .filter(|s| !s.is_empty())?; + Some((name.clone(), role.clone())) + }) + .collect(), + Err(e) => { + tracing::warn!(error = %e, "Failed to fetch repo roles"); + std::collections::HashMap::new() + } + } +} + // ─── Combined tool executor (read-only + MCP) ────────────── struct CombinedToolExecutor<'a> { diff --git a/services/worker/src/passes/triage.rs b/services/worker/src/passes/triage.rs index 4e724acd5..d89242bbb 100644 --- a/services/worker/src/passes/triage.rs +++ b/services/worker/src/passes/triage.rs @@ -396,6 +396,33 @@ pub async fn select_repo( ) }; + // Human-assigned repo roles are the AUTHORITATIVE tiebreak for ambiguous + // tickets (e.g. two frontends that both use Tailwind): the team designates + // which repo owns each domain, so selection follows that instead of guessing + // from auto-generated descriptions. + let roles = crate::passes::plan::fetch_team_repo_roles(state, &msg.team_id).await; + let roles_section = { + let mut lines: Vec = repos + .iter() + .filter_map(|r| roles.get(r).map(|role| format!("- {r} → {role}"))) + .collect(); + lines.sort(); + if lines.is_empty() { + String::new() + } else { + format!( + "\n## Repo roles (human-assigned — AUTHORITATIVE)\n{}\n\ + Meanings: web=primary web frontend, backend=primary API/backend, \ + mobile=mobile app, cms=content/CMS, infra=infrastructure, data=data/ETL.\n\ + These roles OVERRIDE repo descriptions and incidental code matches — a human set \ + them to resolve exactly the cases where several repos could match. Classify the \ + ticket's domain and pick the repo whose role matches it, unless the ticket \ + explicitly names a different repo.\n", + lines.join("\n") + ) + } + }; + let global_agents = super::load_content(state, &msg.team_id, "AGENTS#GLOBAL").await; let context_section = if global_agents.is_empty() { @@ -416,10 +443,10 @@ Description: {context_section} ## Available repositories {repo_list} -{evidence_section} +{evidence_section}{roles_section} Think step by step: -1. What is this ticket about? (one sentence) -2. Which repo owns that feature area? Weigh the CODE EVIDENCE above (which repo actually contains the ticket's identifiers) ABOVE the repo descriptions. +1. What is this ticket about, and what DOMAIN is it — web/frontend, backend/API, mobile, cms, infra, or data? (one sentence) +2. If a repo has a ROLE matching that domain (see Repo roles), pick it — roles are authoritative and override descriptions. Otherwise weigh the CODE EVIDENCE (which repo actually contains the ticket's identifiers) ABOVE the repo descriptions. 3. Why is it NOT any of the other repos? Then on the LAST line, return ONLY the repository in `owner/name` format."#,