feat(permission): contextual approval with token-aware prefix matching#3076
Open
GroovyCarrot wants to merge 1 commit into
Open
feat(permission): contextual approval with token-aware prefix matching#3076GroovyCarrot wants to merge 1 commit into
GroovyCarrot wants to merge 1 commit into
Conversation
Implements Phases 1–3 of the contextual permission redesign:
Phase 1 — Permission service (token-aware prefix matching):
- Add Contexts []string to CreatePermissionRequest / PermissionRequest
- Add PermissionKey.Context for per-token session grants
- GrantPersistent records one key per context token (Path omitted from
contextual keys; location semantics live in path: tokens)
- Request() step 5: all contexts must be satisfied for auto-approval;
step 6: legacy path-based key fallback for context-less tools
- Replace regex-based contextSatisfied with clean iteration over stored
grants using tokenSatisfies helper
Phase 2 — context.go: tokenSatisfies helper:
- command:<A> satisfies command:<B> iff B == A or B starts with A+space
(word boundary: command:go satisfies command:go test, not command:golang;
command:py does not satisfy command:python3)
- path:<A> satisfies path:<B> iff B == A, or B starts with A+separator,
or A is / (root satisfies all absolute paths)
Phase 3 — Bash tool produces contexts (bashctx.go):
- AnalyzeCommand parses command strings via mvdan.cc/sh AST walker
- Emits command:<name> and command:<name> <subcommand> tokens for every
SimpleCommand in chains (&&, ||, ;) and pipelines (|)
- Emits path:<abs> tokens for every path argument, resolved against
working dir (abs, relative, .., ~, quoted paths all handled)
- Fail-closed: command substitution, backticks, redirects, sh -c, eval,
loops, conditionals, grouping, process substitution → single opaque
- Wired as Contexts: AnalyzeCommand(params.Command, execWorkingDir) in
the permissions.Request call in bash.go
Phase 4 — config allowed_commands/allowed_paths to context tokens
- Add AllowedCommands []string and AllowedPaths []string to
config.Permissions (json: allowed_commands / allowed_paths)
allowed_commands: shell command names/subcommands (e.g. "go test",
"git diff") — auto-approve requests for matching command: tokens;
prefix matching applies ("go" also approves "go test", "go build")
allowed_paths: filesystem paths (e.g. "/tmp", "/home/user/projects")
— auto-approve requests for matching path: tokens; subpaths are also
approved (path:/tmp approves path:/tmp/subdir). Relative paths are
resolved against the working directory at startup.
- Add MakeCommandToken / MakePathToken to internal/agent/tools/bashctx.go;
these are the canonical token constructors for the bash tool domain —
both the AST walker in AnalyzeCommand and app.go config translation use
them to ensure token format can never diverge
- Add allowedContexts []string to permissionService; contextSatisfied
checks configured tokens via tokenSatisfies before session grants
- Update NewPermissionService to accept allowedContexts; all call sites
updated (app.go, testing.go, agent and permission test files)
- app.go imports internal/agent/tools and constructs context tokens from
AllowedCommands (MakeCommandToken) and AllowedPaths (MakePathToken +
filepath resolution for relative paths)
- Add TestMakeCommandToken / TestMakePathToken to bashctx_test.go; add
TestPermissionService_AllowedContexts to permission_test.go
Phase 5 — UI transparency — show only pending context tokens in dialog
When the permissions dialog is displayed for a bash tool request, context
tokens that are already approved by config or session grants are now omitted.
Only the truly new (pending) tokens are shown. A faint note shows how many
tokens were already approved when at least one is being omitted.
- Add PendingContexts []string to permission.PermissionRequest and
proto.PermissionRequest (serialised for client-server mode)
- Populate PendingContexts in Request() by reusing the contextSatisfied
check that already runs to decide auto-approval
- Add renderPendingContexts / parseContextToken helpers to the permissions
dialog; update renderBashContent and renderDefaultContent to use them
- Update client_workspace.go to map PendingContexts across the wire boundary
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
|
All contributors have signed the CLA ✍️ ✅ |
Author
|
I have read the Contributor License Agreement (CLA) and hereby sign the CLA. |
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.
#3077