Skip to content

fix(opencode): use most-specific-pattern-wins instead of last-match-wins#37936

Open
mgajda wants to merge 3 commits into
anomalyco:devfrom
mgajda:fix/permission-specificity-clean
Open

fix(opencode): use most-specific-pattern-wins instead of last-match-wins#37936
mgajda wants to merge 3 commits into
anomalyco:devfrom
mgajda:fix/permission-specificity-clean

Conversation

@mgajda

@mgajda mgajda commented Jul 20, 2026

Copy link
Copy Markdown

Issue for this PR

Closes #37935, #24335, #31854, #36765

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Replaces findLast() in both V1 and V2 permission evaluate() functions with a most-specific-pattern-wins algorithm. The specificity() 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, because findLast() 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?

  • Updated all unit tests in packages/opencode/test/permission/next.test.ts and packages/opencode/test/permission-task.test.ts to match the new specificity-based semantics
  • All existing tests pass with updated expectations
  • Cherry-picked onto clean origin/dev base with zero unrelated changes

Screenshots / recordings

N/A (logic change, not UI)

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

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
@github-actions

Copy link
Copy Markdown
Contributor

Hey! Your PR title permission: use most-specific-pattern-wins instead of last-match-wins doesn't follow conventional commit format.

Please update it to start with one of:

  • feat: or feat(scope): new feature
  • fix: or fix(scope): bug fix
  • docs: or docs(scope): documentation changes
  • chore: or chore(scope): maintenance tasks
  • refactor: or refactor(scope): code refactoring
  • test: or test(scope): adding or updating tests

Where scope is the package name (e.g., app, desktop, opencode).

See CONTRIBUTING.md for details.

@mgajda mgajda changed the title permission: use most-specific-pattern-wins instead of last-match-wins fix(opencode): use most-specific-pattern-wins instead of last-match-wins Jul 21, 2026
mgajda added 2 commits July 21, 2026 17:53
…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.
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.

permission: broad deny overrides specific allow via findLast() semantics

1 participant