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
38 changes: 38 additions & 0 deletions packages/core/test/browser-attach.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@ import type { Permission } from "@opencode/schema/permission"
import { AppNodeBuilder } from "@opencode/core/effect/app-node-builder"
import { LayerNode } from "@opencode/util/effect/layer-node"
import { Global } from "@opencode/util/global"
import type { SessionHooks } from "@opencode/plugin/effect/session"
import { Bus } from "@opencode/core/bus"
import { Database } from "@opencode/core/database/database"
import { Agent } from "@opencode/core/agent"
import { Location } from "@opencode/core/location"
import { LocationServiceMap } from "@opencode/core/location-services"
import { Model } from "@opencode/core/model"
import { Plugin } from "@opencode/core/plugin"
import { PluginHooks } from "@opencode/core/plugin/hooks"
import { Provider } from "@opencode/core/provider"
import { Rpc } from "@opencode/core/rpc"
import { AbsolutePath } from "@opencode/core/schema"
import { Session } from "@opencode/core/session"
Expand Down Expand Up @@ -103,9 +108,29 @@ const setup = Effect.gen(function* () {
),
Effect.forkScoped({ startImmediately: true }),
)
const hooks = Context.get(context, PluginHooks.Service)
// The tools a request sends directly, after the browser plugin's context hook.
const directTools = (sessionID: Session.ID) =>
Effect.gen(function* () {
const tool = { description: "", input: { type: "object" } }
const event: SessionHooks["context"] = {
sessionID,
agent: Agent.ID.make("explore"),
model: Model.Ref.make({ providerID: Provider.ID.make("test"), id: Model.ID.make("test") }),
system: [],
messages: [],
tools: { browser_click: tool, browser_tabs_open: tool, read: tool },
options: {},
}
yield* hooks.trigger("session", "context", event)
return Object.keys(event.tools)
})
return {
ref,
locations,
session,
sessions,
directTools,
attach: (connectionID: string) =>
rpc.attach({ sessionID: session.id, connectionID, version: 4 }).pipe(Effect.forkScoped),
attached: (connectionID: string) => Deferred.await(handshake(connectionID)).pipe(Effect.timeout("10 seconds")),
Expand Down Expand Up @@ -148,3 +173,16 @@ it.live("disposing the plugin revokes the browser rules before the location fini
expect(yield* browser.permissions).toEqual([user])
}),
)

it.live("child sessions inherit the browser grant but are not sent browser tools", () =>
Effect.gen(function* () {
const browser = yield* setup
yield* browser.attach("conn_1")
yield* browser.attached("conn_1")
const child = yield* browser.sessions.create({ parentID: browser.session.id })
expect(child.permissions).toEqual([user, ...allows])

expect(yield* browser.directTools(browser.session.id)).toEqual(["browser_click", "browser_tabs_open", "read"])
expect(yield* browser.directTools(child.id)).toEqual(["read"])
}),
)
1 change: 1 addition & 0 deletions packages/plugin-browser/src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ export const make = Effect.fn("BrowserConnection.make")(function* (
})

return {
attached: (sessionID: Session.ID) => BrowserMachine.hasAttachment(machine.state, sessionID),
target: Effect.fn("BrowserConnection.target")(function* (sessionID: Session.ID, action: Browser.Action) {
const selected = BrowserMachine.selectTarget(machine.state, sessionID, action)
if (!selected.ok) return yield* selected.error
Expand Down
11 changes: 11 additions & 0 deletions packages/plugin-browser/src/machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,17 @@ export function isConnectionDisposed(state: ConnectionState) {
return state.status === "disposed"
}

/**
* Whether the session has its own attachment, connecting or connected. Child sessions inherit the parent's browser
* grant with their permissions but never an attachment, so their browser tools could only fail.
*/
export function hasAttachment(state: ConnectionState, sessionID: Session.ID) {
if (state.status === "disposed") return false
const entry = state.sessions.get(sessionID)

return entry !== undefined && entry.status !== "revoking"
}

/** The attempt the next `attach` gets; the driver keys its handle by it before dispatching `attach.started`. */
export function selectNextAttempt(state: ConnectionState) {
return state.nextAttempt
Expand Down
19 changes: 16 additions & 3 deletions packages/plugin-browser/src/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Browser } from "./rpc.js"
import { BrowserSkill } from "./skill.js"

export const register = Effect.fn("BrowserTools.register")(function* (
ctx: Pick<Context, "tool" | "skill" | "location">,
ctx: Pick<Context, "tool" | "skill" | "location" | "session">,
connection: BrowserConnection.Connection,
) {
const execute = Effect.fn("BrowserTools.execute")(function* (
Expand All @@ -33,6 +33,8 @@ export const register = Effect.fn("BrowserTools.register")(function* (
})

yield* ctx.skill.transform((editor) => editor.add(BrowserSkill.info))
// Names the model sees when a tool is sent directly rather than through Code Mode.
const direct = new Set<string>()
yield* ctx.tool
.transform((editor) => {
editor.namespace({
Expand All @@ -42,13 +44,16 @@ export const register = Effect.fn("BrowserTools.register")(function* (
})
Browser.Operations.forEach((operation) => {
const separator = operation.name.lastIndexOf(".")
const name = operation.name.slice(separator + 1)
const namespace = separator < 0 ? "browser" : `browser.${operation.name.slice(0, separator)}`
direct.add(`${namespace.replaceAll(".", "_")}_${name}`)
editor.add({
name: operation.name.slice(separator + 1),
name,
description: operation.description,
input: operation.input,
output: operation.output,
options: {
namespace: separator < 0 ? "browser" : `browser.${operation.name.slice(0, separator)}`,
namespace,
permission: "browser",
codemode: true,
},
Expand All @@ -58,6 +63,14 @@ export const register = Effect.fn("BrowserTools.register")(function* (
})
})
.pipe(Effect.orDie)
// A child session inherits the browser grant with its parent's permissions, but the attachment belongs to the
// parent, so every call from the child fails. Agents without Code Mode, such as explore, would otherwise receive
// all browser tools directly.
yield* ctx.session.hook("context", (event) => {
if (connection.attached(event.sessionID)) return Effect.void
for (const name of Object.keys(event.tools)) if (direct.has(name)) delete event.tools[name]
return Effect.void
})
})

function decodeResult(operation: Browser.Operation, result: Browser.Result) {
Expand Down
Loading