Repository navigation
feat(tui): explain tool auto approval #12728
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
63d35d0
feat(cli): record why a tool call was denied on its metadata
brunoagatao 8e515dd
feat(tui): show why a tool call was auto-approved or denied
brunoagatao 770176d
Merge remote-tracking branch 'origin/main' into feat/explain-tool-aut…
brunoagatao 6b27a26
docs: add changeset for TUI auto-approval/denial explanation
brunoagatao abe17f1
fix(tui): move the tool approval/denial note back onto the header line
brunoagatao 5f3b57b
fix(cli): attribute tool call denials to the rule that actually decid…
brunoagatao a07661b
Merge remote-tracking branch 'origin/main' into feat/explain-tool-aut…
brunoagatao c56aad9
refactor(cli): shrink the denial-provenance shared-file diff in permi…
brunoagatao fbc5f2a
Merge remote-tracking branch 'origin/main' into feat/explain-tool-aut…
brunoagatao 68f11a8
Merge branch 'main' into feat/explain-tool-auto-approval-tui
brunoagatao File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@kilocode/cli": minor | ||
| --- | ||
|
|
||
| Show why a tool call was auto-approved or denied in the TUI, and record the denial reason on the tool call metadata (visible in `kilo export`) alongside the existing auto-approval reason. |
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
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
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
92 changes: 92 additions & 0 deletions
92
packages/opencode/test/kilocode/permission/deny-provenance.test.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| import { describe, expect, test } from "bun:test" | ||
| import { Cause, Effect, Exit, Layer } from "effect" | ||
| import { Bus } from "../../../src/bus" | ||
| import { Permission } from "../../../src/permission" | ||
| import { PermissionProvenance } from "../../../src/kilocode/permission/provenance" | ||
| import { EventV2Bridge } from "../../../src/event-v2-bridge" | ||
| import { Database } from "@opencode-ai/core/database/database" | ||
| import { SessionID } from "../../../src/session/schema" | ||
| import * as Config from "../../../src/config/config" | ||
| import * as CrossSpawnSpawner from "@opencode-ai/core/cross-spawn-spawner" | ||
| import { provideTmpdirInstance } from "../../fixture/fixture" | ||
| import { testEffect } from "../../lib/effect" | ||
|
|
||
| const env = Layer.mergeAll( | ||
| Permission.layer.pipe( | ||
| Layer.provide(EventV2Bridge.defaultLayer), | ||
| Layer.provide(Config.defaultLayer), | ||
| Layer.provide(Database.defaultLayer), | ||
| ), | ||
| Config.defaultLayer, | ||
| Bus.layer, | ||
| CrossSpawnSpawner.defaultLayer, | ||
| ) | ||
| const it = testEffect(env) | ||
|
|
||
| const ask = (input: Parameters<Permission.Interface["ask"]>[0]) => | ||
| Effect.gen(function* () { | ||
| const permission = yield* Permission.Service | ||
| return yield* permission.ask(input) | ||
| }) | ||
|
|
||
| function withDir(options: { git?: boolean } | undefined, self: (dir: string) => Effect.Effect<any, any, any>) { | ||
|
brunoagatao marked this conversation as resolved.
Resolved
|
||
| return provideTmpdirInstance(self, options) | ||
| } | ||
|
|
||
| describe("Permission.ask denial provenance", () => { | ||
| it.live( | ||
| "attributes a denial to the rule that matched the request's pattern, not just the textually-last deny rule for the permission", | ||
| () => | ||
| withDir({ git: true }, () => | ||
| Effect.gen(function* () { | ||
| // Two deny rules under the same permission for different patterns. Matching by | ||
| // permission alone (e.g. findLast over rules with action "deny") would pick | ||
| // "rm -rf *" here since it sorts last, even though "git push *" is the one that | ||
| // actually matched the request. | ||
| const ruleset = [ | ||
| { permission: "bash", pattern: "git push *", action: "deny" as const }, | ||
| { permission: "bash", pattern: "rm -rf *", action: "deny" as const }, | ||
| ] | ||
| const exit = yield* ask({ | ||
| sessionID: SessionID.make("session_test"), | ||
| permission: "bash", | ||
| patterns: ["git push origin main"], | ||
| metadata: {}, | ||
| always: [], | ||
| ruleset, | ||
| }).pipe(Effect.exit) | ||
|
|
||
| expect(Exit.isFailure(exit)).toBe(true) | ||
| const err = Exit.isFailure(exit) ? Cause.squash(exit.cause) : undefined | ||
| expect(err).toBeInstanceOf(Permission.DeniedError) | ||
|
|
||
| const approval = PermissionProvenance.classifyDenial({ | ||
| ruleset: (err as Permission.DeniedError).ruleset, | ||
| permission: "bash", | ||
| patterns: ["git push origin main"], | ||
| agent: "build", | ||
| origins: undefined, | ||
| }) | ||
| expect(approval.rule).toEqual({ permission: "bash", pattern: "git push *", action: "deny" }) | ||
| }), | ||
| ), | ||
| ) | ||
|
|
||
| test("a denial with no specific rule (e.g. a headless-subagent policy denial) is still reported as denied, not as an ambiguous default approval", () => { | ||
| // Some denial paths don't carry a specific rule -- Permission.ask's headless-subagent policy | ||
| // denial, for instance, still sets `ruleset` to the plain deny-permission subset (an array, | ||
| // with no `.action`/`.pattern` of its own). classify({ rule: undefined }) reports | ||
| // { source: "default" } -- the same shape the *approval* fallback produces for "no rule | ||
| // matched" -- so without a synthesized deny rule, a refusal would render (and export) as an | ||
| // auto-approval. | ||
| const approval = PermissionProvenance.classifyDenial({ | ||
| ruleset: [{ permission: "bash", pattern: "*", action: "ask" as const }], | ||
| permission: "bash", | ||
| patterns: ["rm -rf /"], | ||
| agent: "build", | ||
| origins: undefined, | ||
| }) | ||
| expect(approval.rule?.action).toBe("deny") | ||
| expect(approval.rule).toEqual({ permission: "bash", pattern: "rm -rf /", action: "deny" }) | ||
| }) | ||
| }) | ||
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| import type { RGBA } from "@opentui/core" | ||
| import { Show } from "solid-js" | ||
| import type { PermissionProvenance } from "@/kilocode/permission/provenance" | ||
| import type { ToolState } from "@kilocode/sdk/v2" | ||
|
|
||
| /** `state.metadata` off any tool state, including the pending variant that lacks the field. */ | ||
| export function stateMetadata(state: ToolState | undefined) { | ||
| return state && "metadata" in state ? state.metadata : undefined | ||
| } | ||
|
|
||
| const SOURCES = ["agent", "global", "project", "yolo", "session", "manual", "default"] as const | ||
|
|
||
| /** Read the approval/denial provenance off a tool part's metadata, if present. */ | ||
| export function toolApprovalFrom(metadata: Record<string, unknown> | undefined) { | ||
| const value = metadata?.approval | ||
| if (!value || typeof value !== "object") return undefined | ||
| const approval = value as PermissionProvenance.Approval | ||
| return (SOURCES as readonly string[]).includes(approval.source) ? approval : undefined | ||
| } | ||
|
|
||
| function sourceLabel(approval: PermissionProvenance.Approval): string | undefined { | ||
| switch (approval.source) { | ||
| case "agent": | ||
| return approval.agent ? `by the ${approval.agent} agent` : "by the agent" | ||
| case "global": | ||
| return "by your global config" | ||
| case "project": | ||
| return "by the project config" | ||
| case "yolo": | ||
| return "by auto-approve (YOLO) mode" | ||
| case "session": | ||
| return "by a session auto-approve rule" | ||
| case "default": | ||
| return "by default" | ||
| default: | ||
| return undefined | ||
| } | ||
| } | ||
|
|
||
| /** A short "why" line describing an auto-approval or denial, for the TUI's plain-text rows. */ | ||
| export function describeApproval(metadata: Record<string, unknown> | undefined): string | undefined { | ||
| const approval = toolApprovalFrom(metadata) | ||
| if (!approval) return undefined | ||
| const manual = approval.source === "manual" | ||
| const decision = manual ? "approved by you" : approval.rule?.action === "deny" ? "denied" : "auto-approved" | ||
| if (manual) return decision | ||
| const source = sourceLabel(approval) | ||
| const rule = approval.rule | ||
| // The catch-all "*"/"*" rule carries no useful detail; let the source alone explain it. | ||
| const ruleText = | ||
| rule && !(rule.permission === "*" && rule.pattern === "*") ? ` (matched ${rule.permission} \`${rule.pattern}\`)` : "" | ||
| return source ? `${decision} ${source}${ruleText}` : decision | ||
| } | ||
|
|
||
| /** | ||
| * The muted "why" annotation appended inline after a tool's title/summary text, matching the | ||
| * `RoutedModelMeta.Badge` convention. Rendered on the header line (not after the tool's own | ||
| * output) so it reads as metadata about the call rather than part of the output itself. | ||
| */ | ||
| export function ApprovalBadge(props: { note: string | undefined; color?: RGBA }) { | ||
| return ( | ||
| <Show when={props.note}> | ||
| <span style={{ fg: props.color }}> · {props.note}</span> | ||
| </Show> | ||
| ) | ||
| } |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.