Skip to content

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

Closed
mgajda wants to merge 4 commits into
anomalyco:devfrom
mgajda:fix/permission-specificity-clean
Closed

mgajda wants to merge 4 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 3 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.
Policy.evaluate() had the same specificity-blind bug as permission
evaluate(): findLast picked the last matching statement regardless
of specificity. Changed to the same action+resource specificity
algorithm used in permission evaluate().

Add test for specific-before-wildcard ordering that would have
failed with findLast.
@github-actions

Copy link
Copy Markdown
Contributor

Automated PR Cleanup

Thank you for contributing to opencode.

Due to the high volume of PRs from users and AI agents, we periodically close older PRs using automated criteria so maintainers can focus review time on the most active and community-supported contributions.

This PR was closed because it matched the following cleanup criteria:

  • The PR was created more than 1 month ago
  • The PR had fewer than 2 positive reactions
  • Positive reactions are counted as thumbs-up, heart, celebration, or rocket reactions on the PR

PRs created within the last month are not affected by this cleanup.

If you believe this PR was closed incorrectly, or if you are still actively working on it, please leave a comment explaining why it should be reopened. A maintainer can review and reopen it if appropriate.

Thanks again for taking the time to contribute.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant