Skip to content

[UPDATE PRIMITIVE] sarif_diff_by_commits — refRange validation, file path improvement, test mock fix#242

Merged
data-douser merged 3 commits intocopilot/add-sarif-to-git-diff-toolfrom
copilot/fix-code-for-review-comments
Apr 12, 2026
Merged

[UPDATE PRIMITIVE] sarif_diff_by_commits — refRange validation, file path improvement, test mock fix#242
data-douser merged 3 commits intocopilot/add-sarif-to-git-diff-toolfrom
copilot/fix-code-for-review-comments

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 12, 2026

📝 Update Information

Primitive Details

⚠️ CRITICAL: PR SCOPE VALIDATION

ALLOWED FILES:

  • Server implementation files (server/src/**/*.ts)
  • Modified supporting library files (server/src/lib/*.ts)
  • Updated test files (server/test/**/*.ts)

🚫 FORBIDDEN FILES: None included.

🛑 MANDATORY PR VALIDATION CHECKLIST

  • ONLY server implementation files are included
  • NO temporary or output files are included
  • NO unrelated configuration files are included
  • ALL existing tests continue to pass
  • NEW functionality is properly tested

  • Impact Scope: Localized
  • Breaking Changes: No
  • API Compatibility: Enhanced (new validation, improved output)
  • Performance Impact: Neutral

🎯 Changes Description

Addresses three review comments from PR #236 review.

Current Behavior

  1. refRange passed directly to git diff without validation — a value like --option is interpreted as a git flag
  2. ClassifiedResult.file always uses normalizeUri(uri), producing home/user/project/src/db.js for file:/// URIs even when a diff match provides the repo-relative path
  3. Tests use vi.doMock per-test for cli-executor, but the dynamic import() in the handler caches the module after the first call — later tests may silently reuse stale mocks

Updated Behavior

  1. refRange is validated before use: rejects values starting with - or containing whitespace
  2. ClassifiedResult.file uses matchingDiff.path when matched (repo-relative), falls back to normalizeUri(uri) only for unmatched results
  3. Single module-scope vi.mock with shared mockExecuteCLICommand configured per-test

Motivation

Review feedback on PR #236.

🔄 Before vs. After Comparison

Functionality Changes

// BEFORE: refRange passed unchecked
const gitArgs = ['diff', '--unified=0', '--diff-filter=ACMR', '--no-color', refRange];

// AFTER: validated first
if (/^\s*-/.test(refRange) || /\s/.test(refRange)) {
  return { content: [{ type: 'text', text: 'Invalid refRange: must not start with "-" or contain whitespace.' }] };
}
// BEFORE: always normalizeUri
file: normalizeUri(uri),  // "home/user/project/src/db.js" for file:/// URIs

// AFTER: prefer diff path when matched
file: matchingDiff ? matchingDiff.path : normalizeUri(uri),  // "src/db.js"

🧪 Testing & Validation

Test Coverage Updates

  • Existing Tests: All 1380 tests continue to pass
  • New Test Cases: 3 new tests (refRange dash, refRange whitespace, unmatched URI fallback)
  • Regression Tests: Enhanced file:// URI test to assert diff path is used
  • Edge Case Tests: Covers option injection and whitespace in refRange

Test Results

  • Unit Tests: All pass (110/110 SARIF tests)
  • Integration Tests: N/A (no changes to integration fixtures)

📋 Implementation Details

Files Modified

  • Core Implementation: server/src/tools/sarif-tools.ts — refRange validation
  • Supporting Libraries: server/src/lib/sarif-utils.tsClassifiedResult.file path logic
  • Tests: server/test/src/tools/sarif-tools.test.ts — mock pattern + validation tests
  • Tests: server/test/src/lib/sarif-utils.test.ts — file path assertions

Dependencies

  • No New Dependencies

🔗 References

Related Issues/PRs

🚀 Compatibility & Migration

Backward Compatibility

  • Fully Compatible: No breaking changes

ClassifiedResult.file now returns shorter repo-relative paths when a diff match exists. Consumers comparing against normalizeUri() output for matched results may see different values — but the new values are strictly more useful (and what the PR description already documented as the expected format).

API Evolution

  • Better Error Messages: Clear rejection of invalid refRange
  • Improved Responses: Repo-relative file paths in classified results
  • Maintained Contracts: Core API contracts preserved

👥 Review Guidelines

For Reviewers

  • ⚠️ SCOPE COMPLIANCE: Only 4 server files changed
  • ⚠️ NO UNRELATED FILES: Clean diff
  • ⚠️ BACKWARD COMPATIBILITY: Existing functionality preserved

Testing Instructions

npm run build-and-test

# Targeted tests
npx vitest run server/test/src/tools/sarif-tools.test.ts server/test/src/lib/sarif-utils.test.ts

📊 Impact Assessment

Server Impact

  • Startup Time: No impact
  • Runtime Stability: Improved (rejects malformed input early)
  • Resource Usage: No change
  • Concurrent Usage: Safe

🔄 Deployment Strategy

  • Safe Deployment: Additive validation + improved output only
  • Rollback Plan: Revert single commit

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • update.code.visualstudio.com
    • Triggering command: /opt/hostedtoolcache/node/24.13.0/x64/bin/node node scripts/download-vscode.js -tests/primitive--unified=0 (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

@data-douser data-douser changed the base branch from next to copilot/add-sarif-to-git-diff-tool April 12, 2026 14:31
Copilot AI and others added 2 commits April 12, 2026 14:35
…ol' into copilot/fix-code-for-review-comments

Co-authored-by: data-douser <70299490+data-douser@users.noreply.github.com>
…le, test mocking

1. Validate refRange in sarif_diff_by_commits to reject strings starting
   with '-' or containing whitespace (prevents git option injection).

2. Use matchingDiff.path for ClassifiedResult.file when a diff match exists,
   falling back to normalizeUri(uri) only for unmatched results (produces
   repo-relative paths instead of long file:// URI paths).

3. Replace vi.doMock with module-scope vi.mock + shared mockExecuteCLICommand
   to prevent module-cache flakiness in sarif_diff_by_commits handler tests.

Agent-Logs-Url: https://github.com/advanced-security/codeql-development-mcp-server/sessions/1960960b-9658-44b5-87d8-bc29cc55a5ef

Co-authored-by: data-douser <70299490+data-douser@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix code according to review comments [UPDATE PRIMITIVE] sarif_diff_by_commits — refRange validation, file path improvement, test mock fix Apr 12, 2026
Copilot AI requested a review from data-douser April 12, 2026 15:01
@data-douser data-douser marked this pull request as ready for review April 12, 2026 15:26
@data-douser data-douser requested review from a team and enyil as code owners April 12, 2026 15:26
@data-douser data-douser merged commit 8f055f5 into copilot/add-sarif-to-git-diff-tool Apr 12, 2026
@data-douser data-douser deleted the copilot/fix-code-for-review-comments branch April 12, 2026 15:27
data-douser added a commit that referenced this pull request Apr 13, 2026
…e path improvement, test mock fix (#242)

* Initial plan

* Fix PR #236 review comments: refRange validation, ClassifiedResult.file, test mocking

1. Validate refRange in sarif_diff_by_commits to reject strings starting
   with '-' or containing whitespace (prevents git option injection).

2. Use matchingDiff.path for ClassifiedResult.file when a diff match exists,
   falling back to normalizeUri(uri) only for unmatched results (produces
   repo-relative paths instead of long file:// URI paths).

3. Replace vi.doMock with module-scope vi.mock + shared mockExecuteCLICommand
   to prevent module-cache flakiness in sarif_diff_by_commits handler tests.

Agent-Logs-Url: https://github.com/advanced-security/codeql-development-mcp-server/sessions/1960960b-9658-44b5-87d8-bc29cc55a5ef

Co-authored-by: data-douser <70299490+data-douser@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: data-douser <70299490+data-douser@users.noreply.github.com>
data-douser added a commit that referenced this pull request Apr 13, 2026
…on tool (#236)

* Initial plan

* feat: add sarif_diff_by_commits tool for SARIF-to-git-diff correlation

Implements a new MCP tool that accepts a SARIF file path and git ref range,
partitions SARIF results into "new" vs "pre-existing" based on file-level
or line-level overlap with the git diff, and returns structured output for
triage workflows.

- Add diffSarifByCommits() pure utility in sarif-utils.ts with types
- Register sarif_diff_by_commits tool in sarif-tools.ts
- Add parseGitDiffOutput() helper for unified diff parsing
- Add 14 unit tests for diffSarifByCommits() utility
- Add 5 unit tests for sarif_diff_by_commits tool handler
- Update server-tools.md documentation
- Update tool registration count from 7 to 8

Closes #209

Agent-Logs-Url: https://github.com/advanced-security/codeql-development-mcp-server/sessions/8abb21bb-8877-4628-90da-36ffc8eeb742

Co-authored-by: data-douser <70299490+data-douser@users.noreply.github.com>

* feat: add client integration test for sarif_diff_by_commits

- Add sarif_diff_by_commits case to Go params.go test runner
- Add Go unit test for param resolution (params_test.go)
- Create file_level_classification integration test fixture with:
  - SARIF with 3 results across 2 rules
  - HEAD..HEAD ref range (empty diff → all pre-existing)
  - Assertions validating totalNew=0, totalPreExisting=3
  - before/after directories with SARIF and monitoring state

Agent-Logs-Url: https://github.com/advanced-security/codeql-development-mcp-server/sessions/edb1fae4-1f49-44f9-af31-71483b674da7

Co-authored-by: data-douser <70299490+data-douser@users.noreply.github.com>

* Apply lint/format & rebuild server/dist/

* [UPDATE PRIMITIVE] `sarif_diff_by_commits` — refRange validation, file path improvement, test mock fix (#242)

* Initial plan

* Fix PR #236 review comments: refRange validation, ClassifiedResult.file, test mocking

1. Validate refRange in sarif_diff_by_commits to reject strings starting
   with '-' or containing whitespace (prevents git option injection).

2. Use matchingDiff.path for ClassifiedResult.file when a diff match exists,
   falling back to normalizeUri(uri) only for unmatched results (produces
   repo-relative paths instead of long file:// URI paths).

3. Replace vi.doMock with module-scope vi.mock + shared mockExecuteCLICommand
   to prevent module-cache flakiness in sarif_diff_by_commits handler tests.

Agent-Logs-Url: https://github.com/advanced-security/codeql-development-mcp-server/sessions/1960960b-9658-44b5-87d8-bc29cc55a5ef

Co-authored-by: data-douser <70299490+data-douser@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: data-douser <70299490+data-douser@users.noreply.github.com>

* Extend client tests -> sarif_diff_by_commits tool

* fix: address unresolved PR review comments

- Fix deletion-only hunk misclassification in line-level granularity by
  adding hunksParsed flag to DiffFileEntry; parseGitDiffOutput sets it
  when @@ headers are seen, and diffSarifByCommits uses it to distinguish
  "no hunk info" from "deletion-only" diffs
- Precompute normalized diff paths once before the results loop, removing
  the unused diffPathMatchesSarifUri wrapper
- Migrate all params_test.go from t.TempDir() to project-local .tmp/
- Add regression tests for deletion-only diffs in unit and handler tests

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: data-douser <70299490+data-douser@users.noreply.github.com>
Co-authored-by: Nathan Randall <data-douser@github.com>
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.

2 participants