fix(opencode): use most-specific-pattern-wins instead of last-match-wins#37936
Open
mgajda wants to merge 3 commits into
Open
fix(opencode): use most-specific-pattern-wins instead of last-match-wins#37936mgajda wants to merge 3 commits into
mgajda wants to merge 3 commits into
Conversation
Replace findLast() in evaluate() with a specificity-based algorithm: most specific pattern (longest literal prefix before first wildcard) wins over less specific patterns, regardless of insertion order. Fixes the problem where a broad deny like /home/m/* at the end of a ruleset silently overrides more specific allows like /home/m/Projects/tools/paper-making-system/* that appear earlier. - Add specificity() helper: counts literal chars before first * or ? - V1 evaluate() and disabled(): use specificity-based selection - V2 evaluate(): same change for the core permission system - Tests: update names and expectations to match new semantics
Contributor
|
Hey! Your PR title Please update it to start with one of:
Where See CONTRIBUTING.md for details. |
…te specificity tests - disabled() now considers pattern specificity (not just permission specificity), so specific patterns keep a tool visible even when a wildcard deny exists for the same permission - Fix 2 tests that weren't updated to specificity semantics: - next.test.ts: wildcard permission no longer overrides specific - permission-task.test.ts: disabled() with specific patterns is false - Add 10 direct evaluate() specificity tests for V2 in core
whollyDisabled() in tool/registry.ts had the same findLast bug — a specific allow like question.private was ignored when *: deny appeared later. Now uses the same action+resource specificity algorithm as evaluate(). Also rename remaining tests/comments that still referenced 'last matching rule' semantics to reflect specificity-based behavior.
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.
Issue for this PR
Closes #37935, #24335, #31854, #36765
Type of change
What does this PR do?
Replaces
findLast()in both V1 and V2 permissionevaluate()functions with a most-specific-pattern-wins algorithm. Thespecificity()helper counts literal characters before the first*or?wildcard. When two rules both match, the one with higher specificity wins; ties are broken by position (later wins).Before (broken): a broad deny like
$HOME/*→ deny at the end of a ruleset silently overrides a more specific allow like$HOME/utilities/*→ allow that appears earlier, becausefindLast()picks the last matching rule regardless of specificity.After (fixed): the more specific pattern wins.
$HOME/utilities(specificity 48) beats$HOME/*(specificity 8). Same for bash command patterns:rm(specificity 2) beats*(specificity 0).Also updates
disabled()to use the same specificity logic when determining whether a tool is completely hidden.How did you verify your code works?
packages/opencode/test/permission/next.test.tsandpackages/opencode/test/permission-task.test.tsto match the new specificity-based semanticsorigin/devbase with zero unrelated changesScreenshots / recordings
N/A (logic change, not UI)
Checklist