Skip to content

rust-guard: dedupe unsafe output write-back and unify search scope labeling arms#7629

Merged
lpcox merged 2 commits into
mainfrom
copilot/rust-guard-deduplicate-unsafe-write-back
Jun 16, 2026
Merged

rust-guard: dedupe unsafe output write-back and unify search scope labeling arms#7629
lpcox merged 2 commits into
mainfrom
copilot/rust-guard-deduplicate-unsafe-write-back

Conversation

Copilot AI commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

This PR removes duplicated unsafe buffer write-back code in Rust guard FFI entrypoints and consolidates duplicated search_code / search_commits labeling logic. The result keeps behavior the same while reducing unsafe surface area and eliminating redundant branching.

  • Unsafe output write-back deduplication (src/lib.rs)

    • Added a private helper:
      • unsafe fn write_bytes_to_output(output_ptr: u32, bytes: &[u8])
    • Replaced four identical raw-pointer copy blocks in:
      • label_agent
      • label_resource
      • label_response (path-based return)
      • label_response (legacy return)
  • Search tool arm consolidation (src/labels/tool_rules.rs)

    • Merged search_code and search_commits into a single match arm:
      • "search_code" | "search_commits" => { ... }
    • Unified scoped description generation via format!("{}:{}", tool_name, s_repo_id).
    • Removed the redundant fallback reassignment of baseline_scope in the prior search_commits branch.
// Before: duplicated unsafe write-back at multiple call sites
unsafe {
    let dest = slice::from_raw_parts_mut(output_ptr as *mut u8, output_bytes.len());
    dest.copy_from_slice(output_bytes);
}

// After: single documented unsafe helper
unsafe fn write_bytes_to_output(output_ptr: u32, bytes: &[u8]) {
    let dest = slice::from_raw_parts_mut(output_ptr as *mut u8, bytes.len());
    dest.copy_from_slice(bytes);
}

// Unified search tool labeling
"search_code" | "search_commits" => {
    let (s_owner, s_repo, s_repo_id) = resolve_search_scope(tool_args, &owner, &repo);
    if !s_repo_id.is_empty() {
        desc = format!("{}:{}", tool_name, s_repo_id);
        // ...
    } else {
        // ...
    }
}

GitHub Advanced Security started work on behalf of lpcox June 16, 2026 17:03 View session
GitHub Advanced Security finished work on behalf of lpcox June 16, 2026 17:05
GitHub Advanced Security started work on behalf of lpcox June 16, 2026 17:09 View session
Copilot AI changed the title [WIP] Deduplicate unsafe write-back pattern in Rust Guard rust-guard: dedupe unsafe output write-back and unify search scope labeling arms Jun 16, 2026
Copilot finished work on behalf of lpcox June 16, 2026 17:11
Copilot AI requested a review from lpcox June 16, 2026 17:11
GitHub Advanced Security finished work on behalf of lpcox June 16, 2026 17:11
@lpcox lpcox marked this pull request as ready for review June 16, 2026 17:12
Copilot AI review requested due to automatic review settings June 16, 2026 17:12

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors the Rust GitHub guard to reduce duplicated unsafe pointer write-back code in its WASM FFI entrypoints and to consolidate duplicated labeling logic for repo-scoped search tools, while preserving existing behavior.

Changes:

  • Introduced a single unsafe fn write_bytes_to_output(output_ptr, bytes) helper and replaced repeated raw-pointer slice copy blocks in label_agent, label_resource, and both label_response return paths.
  • Consolidated search_code and search_commits into a single match arm in tool labeling, including unified scoped description formatting and removal of redundant baseline_scope reassignment.
Show a summary per file
File Description
guards/github-guard/rust-guard/src/lib.rs Deduplicates unsafe output-buffer write-back logic into a single helper used by multiple FFI entrypoints.
guards/github-guard/rust-guard/src/labels/tool_rules.rs Unifies search tool labeling logic for search_code and search_commits into one consistent match arm.

Copilot's findings

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

  • Files reviewed: 2/2 changed files
  • Comments generated: 0

@lpcox lpcox merged commit 622dbb8 into main Jun 16, 2026
46 checks passed
@lpcox lpcox deleted the copilot/rust-guard-deduplicate-unsafe-write-back branch June 16, 2026 17:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants