refactor(difc): extract EvaluateCoarseAccess to eliminate duplicated Phase 2 logic#6639
Merged
Conversation
Closed
5 tasks
…icated Phase 2 logic
Copilot
AI
changed the title
[WIP] Fix duplicate code pattern in DIFC access check logic
refactor(difc): extract EvaluateCoarseAccess to eliminate duplicated Phase 2 logic
May 28, 2026
Collaborator
Contributor
There was a problem hiding this comment.
Pull request overview
Extracts the duplicated DIFC Phase 2 coarse-grained access check logic from internal/server/unified.go and internal/proxy/handler.go into a single shared helper EvaluateCoarseAccess in internal/difc/pipeline_decisions.go, returning a typed CoarseCheckOutcome so each caller still formats its own denial response (MCP error vs HTTP 403). This reduces drift between the server and proxy pipelines (the proxy version was already missing FormatViolationError / span error recording).
Changes:
- Add
CoarseCheckOutcomeenum andEvaluateCoarseAccesshelper centralizing Evaluate +ShouldBypassCoarseDeny. - Replace duplicated Phase 2 if/else blocks in
unified.goandproxy/handler.gowith a typed switch on the new outcome. - Add
TestEvaluateCoarseAccesscovering allowed/bypass-for-read/denied-write/denied-readwrite outcomes.
Show a summary per file
| File | Description |
|---|---|
| internal/difc/pipeline_decisions.go | Introduces CoarseCheckOutcome and EvaluateCoarseAccess helper. |
| internal/difc/pipeline_decisions_test.go | Adds table-driven test for the new helper across the three outcomes. |
| internal/server/unified.go | Switches Phase 2 to use the shared helper; inadvertently swaps tracing.RecordSpanError for inline calls referencing an unimported codes package. |
| internal/proxy/handler.go | Switches Phase 2 to the shared helper via a switch on the typed outcome. |
Copilot's findings
Tip
Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Files reviewed: 4/4 changed files
- Comments generated: 1
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot stopped work on behalf of
lpcox due to an error
May 28, 2026 16:27
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The DIFC Phase 2 coarse-grained check block (
Evaluate→IsAllowed→ShouldBypassCoarseDeny→ handle denial) was duplicated acrossinternal/server/unified.goandinternal/proxy/handler.go, with the proxy version already diverging (missingFormatViolationErrorandRecordError).Changes
internal/difc/pipeline_decisions.go— NewCoarseCheckOutcometype (CoarseAllowed/CoarseBypassForRead/CoarseDenied) andEvaluateCoarseAccesshelper that encapsulates the full Phase 2 decision; returns the typed outcome plus the rawEvaluationResultso each caller can format its own denial response (MCP error vs HTTP 403)internal/server/unified.goandinternal/proxy/handler.go— Replace their localEvaluate+ nested-if blocks with aswitchonEvaluateCoarseAccessinternal/difc/pipeline_decisions_test.go—TestEvaluateCoarseAccesscovering all three outcomes