Skip to content

Commit dad2a0a

Browse files
authored
[Repo Assist] perf(rust-guard): hoist invariant integrity/secrecy calls outside per-item loops (#6005)
🤖 *Repo Assist — automated AI assistant.* ## Summary Eliminates redundant `Vec` allocations in three `label_response_items` branches by hoisting constant integrity/secrecy calls outside their loops. ## Problem Three item-labeling loops were recomputing identical `Vec` values on **every iteration**: | Branch | Repeated call(s) | |--------|-----------------| | `list_gists` / `get_gist` | `reader_integrity(scope_names::USER, ctx)` × N | | `list_notifications` / `get_notification_details` | `private_user_label()` × N **and** `none_integrity("", ctx)` × N | | `list_releases` / `get_latest_release` / `get_release_by_tag` | `merged_integrity(&repo_full_name, ctx)` × N | For a 30-item batch (the default limit) this was ~90 redundant heap allocations per call. In WASM, every allocation is expensive since the linear memory allocator scans free lists on each call. ## Fix Hoist each invariant call before its loop; clone the pre-built `Vec` per item. Also extracts a `DEFAULT_BRANCH_NAMES` constant in `helpers.rs`, consistent with `WRITE_OPERATIONS`, `BLOCKED_TOOLS`, and similar named arrays. The `get_file_contents` branch already uses this pattern correctly — this change makes the rest of the code consistent. Closes #5997 ## Test Status - `cargo check` ✅ - `cargo test` ✅ — 411 tests passed, 0 failed > Generated by [Repo Assist](https://github.com/github/gh-aw-mcpg/actions/runs/26101078502/agentic_workflow) · ● 2.6M · [◷](https://github.com/search?q=repo%3Agithub%2Fgh-aw-mcpg+%22gh-aw-workflow-id%3A+repo-assist%22&type=pullrequests) > > To install this [agentic workflow](https://github.com/githubnext/agentics/blob/851905c06e905bf362a9f6cc54f912e3df747d55/workflows/repo-assist.md), run > ``` > gh aw add githubnext/agentics@851905c > ``` <!-- gh-aw-agentic-workflow: Repo Assist, engine: copilot, version: 1.0.40, model: claude-sonnet-4.6, id: 26101078502, workflow_id: repo-assist, run: https://github.com/github/gh-aw-mcpg/actions/runs/26101078502 --> <!-- gh-aw-workflow-id: repo-assist -->
2 parents e06554f + 36ff56c commit dad2a0a

4 files changed

Lines changed: 133 additions & 68 deletions

File tree

guards/github-guard/rust-guard/src/labels/helpers.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1522,12 +1522,15 @@ pub fn elevate_via_collaborator_permission(
15221522
}
15231523
}
15241524

1525+
/// Well-known default branch names treated as the "merged" branch context.
1526+
const DEFAULT_BRANCH_NAMES: &[&str] = &["main", "master", "HEAD"];
1527+
15251528
/// Check if a branch/ref should be treated as default branch context
15261529
pub fn is_default_branch_ref(branch_ref: &str) -> bool {
15271530
branch_ref.is_empty()
1528-
|| branch_ref.eq_ignore_ascii_case("main")
1529-
|| branch_ref.eq_ignore_ascii_case("master")
1530-
|| branch_ref.eq_ignore_ascii_case("HEAD")
1531+
|| DEFAULT_BRANCH_NAMES
1532+
.iter()
1533+
.any(|n| branch_ref.eq_ignore_ascii_case(n))
15311534
}
15321535

15331536
fn looks_like_commit_sha(reference: &str) -> bool {

guards/github-guard/rust-guard/src/labels/response_items.rs

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use super::constants::{field_names, label_constants, scope_names};
1414
use super::extract_mcp_response;
1515
use super::helpers::*;
16-
use crate::{LabeledItem, ResourceLabels};
16+
use crate::{LabeledItem, ResourceLabels, SharedLabels};
1717
use serde_json::Value;
1818

1919
/// Label individual items in a response (fine-grained labeling)
@@ -72,8 +72,8 @@ pub fn label_response_items(
7272
data: item.clone(),
7373
labels: ResourceLabels {
7474
description: format!("repo:{}", full_name),
75-
secrecy,
76-
integrity,
75+
secrecy: secrecy.into(),
76+
integrity: integrity.into(),
7777
},
7878
});
7979
} else {
@@ -82,8 +82,8 @@ pub fn label_response_items(
8282
data: item.clone(),
8383
labels: ResourceLabels {
8484
description: format!("repo:{}", full_name),
85-
secrecy: vec![],
86-
integrity,
85+
secrecy: vec![].into(),
86+
integrity: integrity.into(),
8787
},
8888
});
8989
}
@@ -204,8 +204,9 @@ pub fn label_response_items(
204204
repo_visibility_secrecy_for_repo_id(repo_full_name, ctx)
205205
} else {
206206
secrecy.clone()
207-
},
208-
integrity,
207+
}
208+
.into(),
209+
integrity: integrity.into(),
209210
},
210211
});
211212
}
@@ -287,8 +288,9 @@ pub fn label_response_items(
287288
repo_visibility_secrecy_for_repo_id(&repo_full_name, ctx)
288289
} else {
289290
secrecy.clone()
290-
},
291-
integrity,
291+
}
292+
.into(),
293+
integrity: integrity.into(),
292294
},
293295
});
294296
}
@@ -308,14 +310,16 @@ pub fn label_response_items(
308310
} else {
309311
writer_integrity(&repo_full_name, ctx)
310312
};
313+
let secrecy_shared: SharedLabels = secrecy.into();
314+
let file_integrity_shared: SharedLabels = file_integrity.into();
311315

312316
for &item in items_limited.iter() {
313317
labeled_items.push(LabeledItem {
314318
data: item.clone(),
315319
labels: ResourceLabels {
316320
description: format!("file:{}", repo_full_name),
317-
secrecy: secrecy.clone(),
318-
integrity: file_integrity.clone(),
321+
secrecy: secrecy_shared.clone(),
322+
integrity: file_integrity_shared.clone(),
319323
},
320324
});
321325
}
@@ -345,6 +349,7 @@ pub fn label_response_items(
345349
// requests, which should preserve merged-floor consistency with
346350
// list_commits-derived SHAs.
347351
let is_default_branch = is_default_branch_commit_context(tool_name, arg_branch);
352+
let secrecy_shared: SharedLabels = secrecy.into();
348353

349354
for item in items_limited.iter().copied() {
350355
let sha = item.get("sha").and_then(|v| v.as_str()).unwrap_or("");
@@ -357,8 +362,8 @@ pub fn label_response_items(
357362
data: item.clone(),
358363
labels: ResourceLabels {
359364
description: format!("commit:{}@{}", repo_full_name, short_sha),
360-
secrecy: secrecy.clone(),
361-
integrity,
365+
secrecy: secrecy_shared.clone(),
366+
integrity: integrity.into(),
362367
},
363368
});
364369
}
@@ -371,6 +376,8 @@ pub fn label_response_items(
371376
// Limit items to prevent WASM memory exhaustion
372377
let items_limited = limit_items_with_log(all_items.as_slice(), "list_gists");
373378

379+
let gist_integrity = reader_integrity(scope_names::USER, ctx);
380+
let gist_integrity_shared: SharedLabels = gist_integrity.into();
374381
for item in items_limited.iter().copied() {
375382
let is_public = get_bool_or(item, "public", true);
376383
let id = get_str_or(item, "id", "unknown");
@@ -386,8 +393,8 @@ pub fn label_response_items(
386393
data: item.clone(),
387394
labels: ResourceLabels {
388395
description: format!("gist:{}", id),
389-
secrecy,
390-
integrity: reader_integrity(scope_names::USER, ctx),
396+
secrecy: secrecy.into(),
397+
integrity: gist_integrity_shared.clone(),
391398
},
392399
});
393400
}
@@ -398,14 +405,18 @@ pub fn label_response_items(
398405
let items = actual_response.as_array().or_else(|| response.as_array());
399406

400407
if let Some(items) = items {
408+
let notif_secrecy = private_user_label();
409+
let notif_integrity = none_integrity("", ctx);
410+
let notif_secrecy_shared: SharedLabels = notif_secrecy.into();
411+
let notif_integrity_shared: SharedLabels = notif_integrity.into();
401412
for item in items.iter() {
402413
let id = get_str_or(item, "id", "unknown");
403414
labeled_items.push(LabeledItem {
404415
data: item.clone(),
405416
labels: ResourceLabels {
406417
description: format!("notification:{}", id),
407-
secrecy: private_user_label(),
408-
integrity: none_integrity("", ctx),
418+
secrecy: notif_secrecy_shared.clone(),
419+
integrity: notif_integrity_shared.clone(),
409420
},
410421
});
411422
}
@@ -422,6 +433,9 @@ pub fn label_response_items(
422433
let (arg_owner, arg_repo, repo_full_name) = extract_repo_info(tool_args);
423434
let secrecy = repo_visibility_secrecy(&arg_owner, &arg_repo, &repo_full_name, ctx);
424435

436+
let release_integrity = merged_integrity(&repo_full_name, ctx);
437+
let secrecy_shared: SharedLabels = secrecy.into();
438+
let release_integrity_shared: SharedLabels = release_integrity.into();
425439
for item in items_limited.iter().copied() {
426440
let tag = get_str_or(item, "tag_name", "unknown");
427441

@@ -430,8 +444,8 @@ pub fn label_response_items(
430444
data: item.clone(),
431445
labels: ResourceLabels {
432446
description: format!("release:{}@{}", repo_full_name, tag),
433-
secrecy: secrecy.clone(),
434-
integrity: merged_integrity(&repo_full_name, ctx),
447+
secrecy: secrecy_shared.clone(),
448+
integrity: release_integrity_shared.clone(),
435449
},
436450
});
437451
}

0 commit comments

Comments
 (0)