Skip to content
Merged
5 changes: 5 additions & 0 deletions .changeset/show-outside-workspace-approval-reason.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"kilo-code": patch
---

Show the permission approval reason for reads and writes outside the workspace, matching other tools
7 changes: 5 additions & 2 deletions packages/kilo-ui/src/components/message-part.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ import { checksum } from "@opencode-ai/core/util/encode"
import { Tooltip } from "./tooltip"
import { IconButton } from "./icon-button"
import { TextShimmer } from "@opencode-ai/ui/text-shimmer"
import { ToolApprovalProvider, resolveToolApproval } from "./tool-approval"
import { ToolApprovalProvider, resolveToolApproval, useToolApproval } from "./tool-approval"
export { ToolApprovalProvider, resolveToolApproval, ToolApprovalVisibilityProvider } from "./tool-approval"
import { GrowBox } from "./grow-box"
import { COLLAPSIBLE_SPRING } from "./motion"
Expand Down Expand Up @@ -1888,10 +1888,13 @@ ToolRegistry.register({
const pending = createMemo(() => busy(props.status))
const images = createMemo(() => (props.attachments ?? []).filter((f) => f.mime.startsWith("image/") && f.url))
const preview = (url: string, alt?: string) => dialog.show(() => <ImagePreview src={url} alt={alt} />)
// Read is high-frequency and low-risk, so details stay hidden unless the target was outside
// the workspace, in which case the approval reason explains what looks like an "agent escape".
const approval = useToolApproval()
return (
<>
<BasicTool
hideDetails
hideDetails={!approval()?.approval.outsideWorkspace}
{...props}
icon="glasses"
onSubtitleClick={
Expand Down
24 changes: 24 additions & 0 deletions packages/kilo-ui/src/components/tool-approval.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,28 @@ describe("resolveToolApproval", () => {
expect(out?.source).toBe("ui.approval.source.agent(agent=code)")
expect(out?.rule).toBeUndefined()
})

test("adds the outsideWorkspace text with just the filename when a path is known", () => {
const approval = {
source: "agent" as const,
agent: "code",
outsideWorkspace: true,
outsideWorkspacePath: "/etc/secrets/hello.txt",
}
const out = resolveToolApproval({ approval }, t)
expect(out?.outsideWorkspace).toBe("ui.approval.outsideWorkspace(file=hello.txt)")
})

test("omits the outsideWorkspace text for an ordinary in-workspace approval", () => {
const approval = { source: "agent" as const, agent: "code" }
const out = resolveToolApproval({ approval }, t)
expect(out?.outsideWorkspace).toBeUndefined()
})

test("omits the outsideWorkspace text when outsideWorkspace is set but no path is known", () => {
// e.g. a bash command scanning multiple external directories has no single filepath to show.
const approval = { source: "agent" as const, agent: "code", outsideWorkspace: true }
const out = resolveToolApproval({ approval }, t)
expect(out?.outsideWorkspace).toBeUndefined()
})
})
14 changes: 14 additions & 0 deletions packages/kilo-ui/src/components/tool-approval.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createContext, useContext, Show, type Accessor, type ParentProps } from "solid-js"
import { getFilename } from "@opencode-ai/core/util/path"
import { Icon } from "./icon"

/**
Expand All @@ -12,6 +13,10 @@ export type ToolApproval = {
source: "agent" | "global" | "project" | "yolo" | "session" | "manual" | "default"
agent?: string
rule?: { permission: string; pattern: string; action: string }
/** True when the tool call's target path was outside the workspace/worktree. */
outsideWorkspace?: boolean
/** The target file path, when known, for display as a filename next to the note above. */
outsideWorkspacePath?: string
}

/** Pre-resolved, localized text plus the raw approval, supplied by the caller. */
Expand All @@ -20,6 +25,7 @@ export type ToolApprovalDisplay = {
decision: string
source?: string
rule?: string
outsideWorkspace?: string
}

const SOURCE_KEYS = ["agent", "global", "project", "yolo", "session", "manual", "default"] as const
Expand Down Expand Up @@ -82,11 +88,16 @@ export function resolveToolApproval(
rule && !(rule.permission === "*" && rule.pattern === "*")
? t("ui.approval.rule", { permission: rule.permission, pattern: rule.pattern })
: undefined
// Only worth calling out when we know which file it was; a bare "outside your workspace" note
// without a filename (e.g. a bash command touching several directories) isn't actionable.
const filename = approval.outsideWorkspacePath ? getFilename(approval.outsideWorkspacePath) : undefined
return {
approval,
decision: approval.source === "manual" ? t("ui.approval.manual") : t("ui.approval.auto"),
source: sourceText(),
rule: ruleText,
outsideWorkspace:
approval.outsideWorkspace && filename ? t("ui.approval.outsideWorkspace", { file: filename }) : undefined,
}
}

Expand All @@ -101,6 +112,9 @@ export function ToolApprovalLine(props: { display: ToolApprovalDisplay }) {
<Show when={props.display.source}>{(text) => <span data-slot="tool-approval-source">{text()}</span>}</Show>
<Show when={props.display.rule}>{(text) => <span data-slot="tool-approval-rule">{text()}</span>}</Show>
</Show>
<Show when={props.display.outsideWorkspace}>
{(text) => <span data-slot="tool-approval-outside-workspace">{text()}</span>}
</Show>
</div>
)
}
1 change: 1 addition & 0 deletions packages/kilo-vscode/webview-ui/src/i18n/ar.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/kilo-vscode/webview-ui/src/i18n/br.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/kilo-vscode/webview-ui/src/i18n/bs.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/kilo-vscode/webview-ui/src/i18n/da.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/kilo-vscode/webview-ui/src/i18n/de.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ export const dict = {
"ui.approval.source.yolo": "durch den Auto-Genehmigungsmodus (YOLO)",
"ui.approval.source.session": "durch eine Sitzungs-Auto-Genehmigungsregel",
"ui.approval.source.default": "standardmäßig",
"ui.approval.outsideWorkspace": "(außerhalb deines Arbeitsbereichs: {{file}})",

"session.tab.review": "Überprüfung",
"session.review.filesChanged": "{{count}} Dateien geändert",
Expand Down
1 change: 1 addition & 0 deletions packages/kilo-vscode/webview-ui/src/i18n/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ export const dict = {
"ui.approval.source.yolo": "by auto-approve (YOLO) mode",
"ui.approval.source.session": "by a session auto-approve rule",
"ui.approval.source.default": "by default",
"ui.approval.outsideWorkspace": "(outside your workspace: {{file}})",

"session.tab.review": "Review",
"session.review.filesChanged": "{{count}} Files Changed",
Expand Down
1 change: 1 addition & 0 deletions packages/kilo-vscode/webview-ui/src/i18n/es.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/kilo-vscode/webview-ui/src/i18n/fa.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/kilo-vscode/webview-ui/src/i18n/fr.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/kilo-vscode/webview-ui/src/i18n/it.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/kilo-vscode/webview-ui/src/i18n/ja.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/kilo-vscode/webview-ui/src/i18n/ko.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/kilo-vscode/webview-ui/src/i18n/nl.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/kilo-vscode/webview-ui/src/i18n/no.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/kilo-vscode/webview-ui/src/i18n/pl.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/kilo-vscode/webview-ui/src/i18n/ru.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/kilo-vscode/webview-ui/src/i18n/th.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/kilo-vscode/webview-ui/src/i18n/tr.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/kilo-vscode/webview-ui/src/i18n/uk.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/kilo-vscode/webview-ui/src/i18n/zh.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/kilo-vscode/webview-ui/src/i18n/zht.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 33 additions & 2 deletions packages/opencode/src/kilocode/permission/provenance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,21 @@ export namespace PermissionProvenance {
agent?: string
/** The winning rule, omitted for manual replies and the ask fallback. */
rule?: { permission: string; pattern: string; action: Permission.Action }
/** True when the ask's target path was outside the workspace/worktree (an `external_directory` ask). */
outsideWorkspace?: boolean
/** The target file path, when the `external_directory` ask carried one, for display as a filename. */
outsideWorkspacePath?: string
}

/** The `filepath` an `external_directory` ask's metadata carries, if any (see `Tool.assertExternalDirectory`). */
export function filepathOf(metadata: Record<string, unknown> | undefined): string | undefined {
return typeof metadata?.filepath === "string" ? metadata.filepath : undefined
}

/** Tag an approval as outside-workspace when it answers an `external_directory` ask. */
export function tagOutsideWorkspace(approval: Approval, permission: string, path?: string): Approval {
if (permission !== "external_directory") return approval
return { ...approval, outsideWorkspace: true, ...(path ? { outsideWorkspacePath: path } : {}) }
}

export type Scope = "global" | "local"
Expand Down Expand Up @@ -71,13 +86,29 @@ export namespace PermissionProvenance {
* The approval is written once during `ask()`, but tools freely overwrite `state.metadata`
* during execution and on completion. Carry the prior `approval` onto the replacement unless
* the replacement sets its own.
*
* A file tool that crosses the workspace boundary issues *two* asks for one call: the generic
* `external_directory` ask first, then its own `read`/`write`/`edit` ask. Both write `approval`
* metadata, so the second ask's `outsideWorkspace` marker would otherwise clobber the first's
* even though `"approval" in next` is true. Merge that marker forward so it survives.
*/
export function carryApproval(
prev: Record<string, unknown> | undefined,
next: Record<string, unknown> | undefined,
) {
if (!next || !prev?.approval || "approval" in next) return next
return { ...next, approval: prev.approval }
if (!next) return next
const prior = prev?.approval as Approval | undefined
if (!("approval" in next)) return prior ? { ...next, approval: prior } : next
const current = next.approval as Approval | undefined
if (!prior?.outsideWorkspace || !current || current.outsideWorkspace) return next
return {
...next,
approval: {
...current,
outsideWorkspace: true,
...(prior.outsideWorkspacePath ? { outsideWorkspacePath: prior.outsideWorkspacePath } : {}),
},
}
}

/**
Expand Down
30 changes: 22 additions & 8 deletions packages/opencode/src/session/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,18 +97,32 @@ export const resolve = Effect.fn("SessionTools.resolve")(function* (input: {
},
}).pipe(
// record why the call was allowed onto the tool part, then discard the outcome for the tool-facing ask
Effect.tap((approval) => input.processor.metadata(options.toolCallId, { metadata: { approval } })),
Effect.tap((approval) =>
input.processor.metadata(options.toolCallId, {
metadata: {
approval: PermissionProvenance.tagOutsideWorkspace(
approval,
req.permission,
PermissionProvenance.filepathOf(req.metadata),
),
},
}),
),
// record why the call was denied too, so JSON exports and clients can explain the denial
Effect.tapErrorTag("PermissionDeniedError", (err) =>
input.processor.metadata(options.toolCallId, {
metadata: {
approval: PermissionProvenance.classifyDenial({
ruleset: err.ruleset,
permission: req.permission,
patterns: req.patterns,
agent: input.agent.name,
origins: permissionOrigins,
}),
approval: PermissionProvenance.tagOutsideWorkspace(
PermissionProvenance.classifyDenial({
ruleset: err.ruleset,
permission: req.permission,
patterns: req.patterns,
agent: input.agent.name,
origins: permissionOrigins,
}),
req.permission,
PermissionProvenance.filepathOf(req.metadata),
),
},
}),
),
Expand Down
Loading
Loading