Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/plugin/src/tui/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -492,12 +492,18 @@ export interface Keymap {
}
}

export type PermissionMode = "prompt" | "autoaccept"

export interface UI {
readonly dialog: Dialog
readonly toast: Toast
readonly format: {
path(value: string): string
}
readonly permission: {
/** The active permission mode. Reactive when read in a Solid computation. */
mode(): PermissionMode
}
readonly router: {
register(page: Page): () => void
navigate(destination: Destination): void
Expand Down
9 changes: 9 additions & 0 deletions packages/tui/src/feature-plugins/system/notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ function hasRunningChild(context: Plugin.Context, sessionID: string) {
)
}

function willAutoApprove(context: Plugin.Context, sessionID: string) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This copies the auto-approve scope from routes/session/index.tsx (the descendantSessionIDs/permissions memos and the auto-accept createEffect). If one changes without the other, for example if auto-accept starts covering other tabs or stops skipping subagent routes, requests will either be silenced while left pending (the bad case) or alert again. Could both places use one shared helper for the scope, or could a comment on each side point to the other, so they stay in sync?

if (context.ui.permission.mode() !== "autoaccept") return false
const route = context.ui.router.current()
if (route.type !== "session") return false
if (context.data.session.get(route.sessionID)?.parentID !== undefined) return false
return context.data.session.family(route.sessionID).includes(sessionID)
}

export default Plugin.define({
id: "opencode.notifications",
setup(context) {
Expand Down Expand Up @@ -65,6 +73,7 @@ export default Plugin.define({
context.data.on("permission.asked", (event) => {
if (permissions.has(event.data.id)) return
permissions.add(event.data.id)
if (willAutoApprove(context, event.data.sessionID)) return
notify(context, event.data.sessionID, "Permission needs input", "permission")
}),
context.data.on("permission.replied", (event) => permissions.delete(event.data.requestID)),
Expand Down
3 changes: 3 additions & 0 deletions packages/tui/src/plugin/api.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,9 @@ export function createPluginContext(input: {
format: {
path: (value) => abbreviateHome(value, host.paths.home),
},
permission: {
mode: () => host.local.permission.mode,
},
router: {
register(page) {
if (input.registry.has("routes", page.name)) throw new Error(`Route already registered: ${page.name}`)
Expand Down
34 changes: 31 additions & 3 deletions packages/tui/test/cli/cmd/tui/notifications.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { describe, expect, test } from "bun:test"
import Notifications from "../../../../src/feature-plugins/system/notifications"
import type { OpenCodeEvent, PermissionAsked } from "@opencode/client"
import type { AttentionNotifyOptions, Context, Route, ToastOptions } from "@opencode/plugin/tui/context"
import type { AttentionNotifyOptions, Context, PermissionMode, Route, ToastOptions } from "@opencode/plugin/tui/context"

type Session = { id: string; title: string; parentID?: string }

async function setup(route: Route = { type: "session", sessionID: "session" }) {
async function setup(route: Route = { type: "session", sessionID: "session" }, mode: PermissionMode = "prompt") {
const notifications: AttentionNotifyOptions[] = []
const toasts: ToastOptions[] = []
const handlers = new Map<OpenCodeEvent["type"], ((event: OpenCodeEvent) => void)[]>()
Expand All @@ -27,6 +27,7 @@ async function setup(route: Route = { type: "session", sessionID: "session" }) {
await Notifications.setup({
ui: {
router: { current: () => route },
permission: { mode: () => mode },
toast: { show: (toast: ToastOptions) => toasts.push(toast) },
},
attention: {
Expand All @@ -53,7 +54,8 @@ async function setup(route: Route = { type: "session", sessionID: "session" }) {
},
session: {
get: (sessionID: string) => sessions[sessionID],
family: () => Object.keys(sessions),
family: (sessionID: string) =>
sessionID === "session" ? ["session", "subagent", "second", "nested"] : [sessionID],
status: (sessionID: string) => (running.has(sessionID) ? "running" : "idle"),
},
},
Expand Down Expand Up @@ -216,6 +218,32 @@ describe("internal notifications TUI plugin", () => {
expect(harness.notifications).toEqual([titledFormNotification, permissionNotification])
})

test("stays silent for permissions the client auto-approves on the open session and its subagents", async () => {
const harness = await setup(undefined, "autoaccept")

harness.emit({ id: "event-1", created: 0, type: "permission.asked", data: permission("permission-open") })
harness.emit({
id: "event-2",
created: 0,
type: "permission.asked",
data: permission("permission-subagent", "subagent"),
})

expect(harness.notifications).toEqual([])
})

test.each<Route>([
{ type: "home" },
{ type: "session", sessionID: "other" },
{ type: "session", sessionID: "subagent" },
])("still notifies for auto-accepted permissions outside the open session (%j)", async (route) => {
const harness = await setup(route, "autoaccept")

harness.emit({ id: "event-1", created: 0, type: "permission.asked", data: permission("permission-1") })

expect(harness.notifications).toEqual([permissionNotification])
})

test("notifies for global forms once the TUI can render them", async () => {
const harness = await setup()

Expand Down
Loading