From 7a9490176ec7484f46bce0a806243d7f9d678db7 Mon Sep 17 00:00:00 2001 From: Keith Kowal <3362598+keithpk@users.noreply.github.com> Date: Mon, 16 Nov 2026 20:32:09 -0800 Subject: [PATCH 1/3] feat(shared-browser): choose agents by label in the session-open hook Add a labels list to listed mode. An entry is a label key (team) or key=value (team=blue). Labels are not on the agent.create request but are on the session-open request, so the decision is made there: a server returned from that hook lasts for that session only, so an agent with a matching label gets the adapter, with a fresh ticket bound to it and its workspace, in every interactive session it opens. Helpers, history sessions and sessions without a workspace are skipped, a failure opens the session without the tool instead of failing it, and nothing changes in the all mode or on releases whose request has no labels. --- paseo-shared-browser/README.md | 16 +- .../client/agent-access-settings.tsx | 20 +- paseo-shared-browser/index.server.ts | 97 ++++++--- paseo-shared-browser/shared/agent-access.ts | 32 ++- .../tests/agent-filter.test.ts | 47 +++++ paseo-shared-browser/tests/contracts.test.ts | 190 +++++++++++++++++- 6 files changed, 365 insertions(+), 37 deletions(-) diff --git a/paseo-shared-browser/README.md b/paseo-shared-browser/README.md index d79120e5..1d3f76c0 100644 --- a/paseo-shared-browser/README.md +++ b/paseo-shared-browser/README.md @@ -163,8 +163,20 @@ By default every new agent gets the adapter. Settings, under Shared Browser, can - **Every new agent** (the default) injects the adapter into every new agent, as above. - **Only the agents listed** injects it only into a new agent whose provider id is listed, or whose - workspace id or project id is listed. Separate entries with commas or new lines. An empty - configuration means no agent. + workspace id or project id is listed, and into any agent that carries a listed label (below). + Separate entries with commas or new lines. An empty configuration means no agent. + +**Labels.** An entry is a label key (`team`: any agent with that label) or `key=value` (`team=blue`: +that label with exactly that value). Matching is exact and case-sensitive. Labels are not on the +`agent.create` request, only on the session-open request, so this is decided there, and it works +differently from the other lists: a server returned from that hook lasts for that session only, while one +added in `agent.create` is stored on the agent. So an agent with a matching label gets the adapter in +every interactive session it opens (a new agent, a resume, a refresh, an import), each time with a fresh +ticket bound to that agent and its workspace; the tickets are revoked together when the agent is +archived. Helper (background) agents, history sessions and sessions without a workspace are skipped. If +the ticket cannot be issued or bound the session opens without the tool and the log gets one line. In +"Every new agent" mode this does nothing, because every new agent already has the tool; an agent that +the create hook already gave a ticket is not given a second one. `agent.create` carries the provider and the working directory but no labels and no workspace id, so a workspace or project is matched by directory: the workspace whose directory is the agent's directory or diff --git a/paseo-shared-browser/client/agent-access-settings.tsx b/paseo-shared-browser/client/agent-access-settings.tsx index e8a38e45..ff25aabb 100644 --- a/paseo-shared-browser/client/agent-access-settings.tsx +++ b/paseo-shared-browser/client/agent-access-settings.tsx @@ -16,11 +16,12 @@ const MODE_OPTIONS: readonly { label: string; value: AgentAccessMode }[] = [ { label: "Only the agents listed below", value: "listed" }, ]; -type ListName = "providers" | "workspaces" | "projects"; +type ListName = "providers" | "workspaces" | "projects" | "labels"; const NO_ERRORS: Record = { providers: null, workspaces: null, projects: null, + labels: null, }; /** Settings screen: which new agents get the browser tool. */ @@ -29,6 +30,7 @@ export function AgentAccessSettings({ theme }: PluginSurfaceProps) { const providers = useRef(null); const workspaces = useRef(null); const projects = useRef(null); + const labels = useRef(null); // Errors belong to the revision they were found on; a newer revision (another client saved) clears them. const [found, setFound] = useState<{ revision: string | null; @@ -71,18 +73,21 @@ export function AgentAccessSettings({ theme }: PluginSurfaceProps) { providers: parseEntries(providers.current?.getText() ?? ""), workspaces: parseEntries(workspaces.current?.getText() ?? ""), projects: parseEntries(projects.current?.getText() ?? ""), + labels: parseEntries(labels.current?.getText() ?? ""), }; const errors = { providers: parsed.providers.error, workspaces: parsed.workspaces.error, projects: parsed.projects.error, + labels: parsed.labels.error, }; setFound({ revision: settings.revision, errors }); - if (errors.providers || errors.workspaces || errors.projects) return null; + if (errors.providers || errors.workspaces || errors.projects || errors.labels) return null; return { providers: parsed.providers.entries, workspaces: parsed.workspaces.entries, projects: parsed.projects.entries, + labels: parsed.labels.entries, }; }; return ( @@ -91,7 +96,7 @@ export function AgentAccessSettings({ theme }: PluginSurfaceProps) { {}} disabled={settings.saving} /> + {}} + disabled={settings.saving} + /> >; + background?: boolean; + mcpServers?: Record; +} + +/** The stdio server entry that runs the browser adapter for one ticket. */ +function browserMcpServer(ticket: string) { + return { + type: "stdio" as const, + command: process.execPath, + args: [mcpBundlePath()], + env: { PASEO_HOME: paseoHome(), [TICKET_ENV]: ticket, ELECTRON_RUN_AS_NODE: "1" }, + }; +} + export default function contribute(server: PluginServerContext) { server.registerSettings(browserDisplayPreferences); const agentAccess = server.registerSettings(agentAccessSettings); // The deadline covers the settings read alone: a setting that cannot be read in time is an unreadable // setting, and that falls back to the default (every new agent). - const readAccess = async () => { + // `quiet` is for the session-open path, which runs on every open: the fallback is still taken, without a log line each time. + const readAccess = async (quiet = false) => { try { const state = await withinDeadline( Promise.resolve(agentAccess.read()), @@ -122,16 +145,18 @@ export default function contribute(server: PluginServerContext) { "the read timed out", ); const access = accessFromState(state); - if (access === DEFAULT_AGENT_ACCESS) { + if (access === DEFAULT_AGENT_ACCESS && !quiet) { console.warn( `shared-browser: the agent-access setting is ${state?.status ?? "missing"}; every new agent gets the browser tool`, ); } return access; } catch (error) { - console.warn( - `shared-browser: the agent-access setting could not be read (${error instanceof Error ? error.message : "error"}); every new agent gets the browser tool`, - ); + if (!quiet) { + console.warn( + `shared-browser: the agent-access setting could not be read (${error instanceof Error ? error.message : "error"}); every new agent gets the browser tool`, + ); + } return DEFAULT_AGENT_ACCESS; } }; @@ -187,32 +212,56 @@ export default function contribute(server: PluginServerContext) { env: { ...request.env, [TICKET_ENV]: ticket }, config: { ...request.config, - mcpServers: { - ...request.config.mcpServers, - [MCP_SERVER_ID]: { - type: "stdio", - command: process.execPath, - args: [mcpBundlePath()], - env: { - PASEO_HOME: paseoHome(), - [TICKET_ENV]: ticket, - ELECTRON_RUN_AS_NODE: "1", - }, - }, - }, + mcpServers: { ...request.config.mcpServers, [MCP_SERVER_ID]: browserMcpServer(ticket) }, }, }; }); server.before("agent.session_open", async ({ request }) => { - if (request.reason !== "create") return request; const ticket = request.env[TICKET_ENV]; - if (!ticket || request.purpose !== "interactive" || !request.workspaceId) return request; - await bindAgentTicket(ticket, request.agentId, request.workspaceId); - const { [TICKET_ENV]: _ticket, ...environment } = request.env; - return { ...request, env: environment }; + if (request.reason === "create" && ticket) { + if (request.purpose !== "interactive" || !request.workspaceId) return request; + await bindAgentTicket(ticket, request.agentId, request.workspaceId); + const { [TICKET_ENV]: _ticket, ...environment } = request.env; + return { ...request, env: environment }; + } + if (ticket) return request; + return injectByLabel(request); }); + // Labels are on the session-open request but not on agent.create, so an agent chosen by label is handled here, in every + // interactive session of an agent that carries a matching label: a server returned from this hook lasts for that session + // only (unlike one added in agent.create, which is stored on the agent), so each open issues and binds a fresh ticket. + // Agents that gave no label, helpers, and sessions without a workspace are returned at once. Nothing here throws: a + // failure leaves the session without the tool instead of failing it. + const injectByLabel = async (request: PluginSessionOpenRequest) => { + // Older Paseo releases (0.13) do not send labels, a background flag or MCP servers on this request, so this does nothing there. + const { labels, background, mcpServers } = request as PluginSessionOpenRequest & + OptionalOpenFields; + if (request.purpose !== "interactive" || background === true || !request.workspaceId) + return request; + if (!labels || Object.keys(labels).length === 0) return request; + if (!agentGetsBrowserByLabel(await readAccess(true), labels)) return request; + const ticket = randomBytes(32).toString("base64url"); + if (!(await issueTicketWithinDeadline(ticket))) return request; + try { + await withinDeadline( + bindAgentTicket(ticket, request.agentId, request.workspaceId), + TICKET_ISSUE_TIMEOUT_MS, + "Shared Browser ticket binding timed out", + ); + } catch (error) { + console.warn( + `shared-browser: could not bind the browser ticket for a labelled agent (${error instanceof Error ? error.message : "error"}); the session opens without it`, + ); + return request; + } + return { + ...request, + mcpServers: { ...mcpServers, [MCP_SERVER_ID]: browserMcpServer(ticket) }, + } as PluginSessionOpenRequest; + }; + server.on("agent.archived", ({ agent }) => revokeAgentBrowserAccess(agent.id)); server.on("workspace.archived", ({ workspace }) => handleWorkspaceArchived(workspace.id)); diff --git a/paseo-shared-browser/shared/agent-access.ts b/paseo-shared-browser/shared/agent-access.ts index 09ff32aa..31790e3f 100644 --- a/paseo-shared-browser/shared/agent-access.ts +++ b/paseo-shared-browser/shared/agent-access.ts @@ -3,7 +3,7 @@ * * "all" injects the adapter into every new agent (the behaviour before this setting existed, and the * default). "listed" injects it only when the new agent matches an entry of one of three lists: its - * provider id, the id of its workspace, or the id of its workspace's project. An empty configuration + * provider id, the id of its workspace, the id of its workspace's project, or one of its labels. An empty configuration * means no agent. Agents that already exist are never changed. * * `agent.create` carries the provider and the working directory but no labels and no workspace id, so @@ -24,6 +24,7 @@ export const agentAccessSchema = z.object({ providers: entries, workspaces: entries, projects: entries, + labels: entries, }); export type AgentAccess = z.output; @@ -41,6 +42,7 @@ export const DEFAULT_AGENT_ACCESS: AgentAccess = { providers: [], workspaces: [], projects: [], + labels: [], }; export function accessFromState( @@ -135,6 +137,34 @@ export async function agentGetsBrowser( } } +/** + * True when one entry of `entries` matches the agent's labels. An entry is a label key (`team`: the agent has that + * label, whatever its value) or `key=value` (the agent has that label with exactly that value). Matching is exact and + * case-sensitive. An entry with an empty key never matches. + */ +export function labelMatches( + entries: readonly string[], + labels: Readonly>, +): boolean { + return entries.some((entry) => { + const separator = entry.indexOf("="); + const key = separator < 0 ? entry : entry.slice(0, separator); + if (key === "" || !Object.hasOwn(labels, key)) return false; + return separator < 0 || labels[key] === entry.slice(separator + 1); + }); +} + +/** + * True when an agent gets the browser tool because of its labels. Labels are only on the session-open request, not on + * agent.create, so this runs there. It applies to the "listed" mode only; in "all" every new agent already has the tool. + */ +export function agentGetsBrowserByLabel( + access: AgentAccess, + labels: Readonly>, +): boolean { + return access.mode === "listed" && labelMatches(access.labels, labels); +} + export const MAX_LIST_ENTRIES = MAX_ENTRIES; export const MAX_LIST_ENTRY_LENGTH = MAX_ENTRY_LENGTH; diff --git a/paseo-shared-browser/tests/agent-filter.test.ts b/paseo-shared-browser/tests/agent-filter.test.ts index 0c362e9f..bc7933b1 100644 --- a/paseo-shared-browser/tests/agent-filter.test.ts +++ b/paseo-shared-browser/tests/agent-filter.test.ts @@ -4,7 +4,9 @@ import { accessFromState, agentAccessSchema, agentGetsBrowser, + agentGetsBrowserByLabel, DEFAULT_AGENT_ACCESS, + labelMatches, normalizeDirectory, parseEntries, type WorkspaceRef, @@ -25,6 +27,7 @@ const listed = (over: Partial): AgentAccess => ({ providers: [], workspaces: [], projects: [], + labels: [], ...over, }); @@ -268,3 +271,47 @@ describe("dot segments cannot reach a workspace the agent is not in", () => { ]); }); }); + +describe("labels", () => { + const labels = { team: "blue", empty: "", "a=b": "c", "": "bare", pair: "a=b" }; + it.each([ + [["team"], true], + [["team=blue"], true], + [["team=red"], false], + [["team="], false], + [["squad"], false], + [["empty"], true], + [["empty="], true], + [["a=b"], false], + [["a"], false], + [["=blue"], false], + [["=", ""], false], + [["=bare"], false], + [["pair=a=b"], true], + [["pair=a"], false], + [["Team"], false], + [["team=Blue"], false], + [["squad", "team=blue"], true], + [[], false], + [["toString"], false], + [["constructor=x"], false], + ])("%j matches %s", (entries, expected) => { + expect(labelMatches(entries, labels)).toBe(expected); + }); + + it("gives the tool by label in listed mode only", () => { + expect(agentGetsBrowserByLabel(listed({ labels: ["team"] }), labels)).toBe(true); + expect(agentGetsBrowserByLabel({ ...listed({ labels: ["team"] }), mode: "all" }, labels)).toBe( + false, + ); + expect(agentGetsBrowserByLabel(listed({}), labels)).toBe(false); + }); + + it("is a list of the setting, empty by default, with the same limits as the others", () => { + expect(agentAccessSchema.parse({}).labels).toEqual([]); + expect(agentAccessSchema.safeParse({ labels: ["team=blue"] }).success).toBe(true); + expect(agentAccessSchema.safeParse({ labels: [""] }).success).toBe(false); + expect(agentAccessSchema.safeParse({ labels: ["x".repeat(257)] }).success).toBe(false); + expect(agentAccessSchema.safeParse({ labels: Array(201).fill("l") }).success).toBe(false); + }); +}); diff --git a/paseo-shared-browser/tests/contracts.test.ts b/paseo-shared-browser/tests/contracts.test.ts index eaf5ccf0..a77d1a8d 100644 --- a/paseo-shared-browser/tests/contracts.test.ts +++ b/paseo-shared-browser/tests/contracts.test.ts @@ -65,21 +65,43 @@ type AgentCreateHook = ( context?: unknown, ) => Promise; -function captureAgentCreateHook(readAccess?: () => Promise): AgentCreateHook { - let hook: AgentCreateHook | undefined; +type SessionOpenRequest = { + agentId: string; + workspaceId: string | null; + reason: "create" | "resume" | "refresh" | "import"; + purpose: "interactive" | "history"; + background: boolean; + labels: Record; + env: Record; + mcpServers: Record; +}; + +type SessionOpenHook = (input: { request: SessionOpenRequest }) => Promise; + +function captureHooks(readAccess?: () => Promise): { + create: AgentCreateHook; + open: SessionOpenHook; +} { + let create: AgentCreateHook | undefined; + let open: SessionOpenHook | undefined; contribute({ registerSettings: vi.fn((definition: { id: string }) => definition.id === "agent-access" && readAccess ? { read: readAccess } : undefined, ), handle: vi.fn(), before: vi.fn((name: string, handler: unknown) => { - if (name === "agent.create") hook = handler as AgentCreateHook; + if (name === "agent.create") create = handler as AgentCreateHook; + if (name === "agent.session_open") open = handler as SessionOpenHook; return () => {}; }), on: vi.fn(), } as never); - if (!hook) throw new Error("agent.create hook was not registered"); - return hook; + if (!create || !open) throw new Error("the agent hooks were not registered"); + return { create, open }; +} + +function captureAgentCreateHook(readAccess?: () => Promise): AgentCreateHook { + return captureHooks(readAccess).create; } const BASE_STATE: BrowserState = { @@ -286,7 +308,7 @@ describe("agent access", () => { const ready = (values: Record) => async () => ({ status: "ready", - values: { mode: "listed", providers: [], workspaces: [], projects: [], ...values }, + values: { mode: "listed", providers: [], workspaces: [], projects: [], labels: [], ...values }, }); const request = (provider: string, cwd = "/repos/one"): AgentCreateRequest => ({ config: { provider, cwd }, @@ -471,13 +493,167 @@ describe("agent access", () => { it("injects every agent in the default mode", async () => { const hook = captureAgentCreateHook(async () => ({ status: "ready", - values: { mode: "all", providers: [], workspaces: [], projects: [] }, + values: { mode: "all", providers: [], workspaces: [], projects: [], labels: [] }, })); const transformed = await hook({ request: request("anything") }); expect(transformed.config.mcpServers).toHaveProperty("shared-browser"); }); }); +describe("agent access by label", () => { + beforeEach(() => { + browserMocks.issueAgentTicket.mockReset(); + browserMocks.bindAgentTicket.mockReset(); + }); + + const ready = (values: Record) => async () => ({ + status: "ready", + values: { mode: "listed", providers: [], workspaces: [], projects: [], labels: [], ...values }, + }); + const open = (over: Partial = {}): SessionOpenRequest => ({ + agentId: "agent-1", + workspaceId: "ws_a", + reason: "create", + purpose: "interactive", + background: false, + labels: { team: "blue" }, + env: { EXISTING: "value" }, + mcpServers: { other: { type: "stdio" } }, + ...over, + }); + + it("injects the adapter and binds a fresh ticket for an agent with a matching label", async () => { + const { open: hook } = captureHooks(ready({ labels: ["team=blue"] })); + const result = await hook({ request: open() }); + expect(result.mcpServers).toMatchObject({ + other: { type: "stdio" }, + "shared-browser": { type: "stdio", env: { ELECTRON_RUN_AS_NODE: "1" } }, + }); + const ticket = (result.mcpServers["shared-browser"] as { env: Record }).env + .PASEO_SHARED_BROWSER_TICKET; + expect(ticket).toEqual(expect.any(String)); + expect(browserMocks.issueAgentTicket).toHaveBeenCalledWith(ticket); + expect(browserMocks.bindAgentTicket).toHaveBeenCalledWith(ticket, "agent-1", "ws_a"); + expect(result.env).toEqual({ EXISTING: "value" }); + }); + + it.each(["create", "resume", "refresh", "import"] as const)( + "does it again, with a new ticket, when the session reopens (%s)", + async (reason) => { + const { open: hook } = captureHooks(ready({ labels: ["team"] })); + const first = await hook({ request: open({ reason: "create" }) }); + const second = await hook({ request: open({ reason }) }); + const ticketOf = (r: SessionOpenRequest) => + (r.mcpServers["shared-browser"] as { env: Record }).env + .PASEO_SHARED_BROWSER_TICKET; + expect(ticketOf(second)).toEqual(expect.any(String)); + expect(ticketOf(second)).not.toBe(ticketOf(first)); + expect(browserMocks.bindAgentTicket).toHaveBeenCalledTimes(2); + }, + ); + + it.each([ + ["a different value", { team: "red" }, ["team=blue"]], + ["a different key", { squad: "blue" }, ["team"]], + ["no labels", {}, ["team"]], + ["a key only inherited from Object", { toString: "x" }, ["toString=y"]], + ])("leaves an agent with %s alone", async (_name, labels, entries) => { + const { open: hook } = captureHooks(ready({ labels: entries })); + const input = open({ labels }); + await expect(hook({ request: input })).resolves.toBe(input); + expect(browserMocks.issueAgentTicket).not.toHaveBeenCalled(); + expect(browserMocks.bindAgentTicket).not.toHaveBeenCalled(); + }); + + it("does not read the setting for an agent with no labels", async () => { + const read = vi.fn(ready({ labels: ["team"] })); + const { open: hook } = captureHooks(read); + const input = open({ labels: {} }); + await expect(hook({ request: input })).resolves.toBe(input); + expect(read).not.toHaveBeenCalled(); + }); + + it.each([ + ["a helper agent", { background: true }], + ["a history session", { purpose: "history" as const }], + ["a session without a workspace", { workspaceId: null }], + ])("leaves %s alone even with a matching label", async (_name, over) => { + const { open: hook } = captureHooks(ready({ labels: ["team"] })); + const input = open(over); + await expect(hook({ request: input })).resolves.toBe(input); + expect(browserMocks.issueAgentTicket).not.toHaveBeenCalled(); + }); + + it.each([ + [ + "all mode", + async () => ({ + status: "ready", + values: { mode: "all", providers: [], workspaces: [], projects: [], labels: ["team"] }, + }), + ], + ["an invalid setting", async () => ({ status: "invalid", revision: "1", error: "bad" })], + ["an unreadable setting", async () => Promise.reject(new Error("down"))], + ])( + "does nothing in %s (every new agent already has the tool, or the default applies)", + async (_name, read) => { + const warn = vi.spyOn(console, "warn").mockImplementation(() => {}); + try { + const { open: hook } = captureHooks(read as () => Promise); + const input = open(); + await expect(hook({ request: input })).resolves.toBe(input); + expect(browserMocks.issueAgentTicket).not.toHaveBeenCalled(); + expect(warn).not.toHaveBeenCalled(); + } finally { + warn.mockRestore(); + } + }, + ); + + it("does nothing on a Paseo release whose session-open request has no labels, background flag or MCP servers", async () => { + const { open: hook } = captureHooks(ready({ labels: ["team"] })); + const { labels: _labels, background: _background, mcpServers: _mcpServers, ...older } = open(); + const input = older as unknown as SessionOpenRequest; + await expect(hook({ request: input })).resolves.toBe(input); + expect(browserMocks.issueAgentTicket).not.toHaveBeenCalled(); + }); + + it("does not add a second ticket to an agent the create hook already gave one", async () => { + const { open: hook } = captureHooks(ready({ labels: ["team"], providers: ["codex"] })); + const input = open({ env: { PASEO_SHARED_BROWSER_TICKET: "t".repeat(32) } }); + const result = await hook({ request: input }); + expect(browserMocks.bindAgentTicket).toHaveBeenCalledTimes(1); + expect(browserMocks.bindAgentTicket).toHaveBeenCalledWith("t".repeat(32), "agent-1", "ws_a"); + expect(result.mcpServers).not.toHaveProperty("shared-browser"); + expect(result.env).not.toHaveProperty("PASEO_SHARED_BROWSER_TICKET"); + }); + + it("leaves a resumed agent that carries a create-hook ticket in its env untouched", async () => { + const { open: hook } = captureHooks(ready({ labels: ["team"] })); + const input = open({ reason: "resume", env: { PASEO_SHARED_BROWSER_TICKET: "t".repeat(32) } }); + await expect(hook({ request: input })).resolves.toBe(input); + expect(browserMocks.bindAgentTicket).not.toHaveBeenCalled(); + }); + + it("opens the session without the tool, and does not throw, when the ticket cannot be issued or bound", async () => { + const warn = vi.spyOn(console, "warn").mockImplementation(() => {}); + try { + const { open: hook } = captureHooks(ready({ labels: ["team"] })); + browserMocks.issueAgentTicket.mockRejectedValueOnce(new Error("supervisor down")); + const first = open(); + await expect(hook({ request: first })).resolves.toBe(first); + expect(browserMocks.bindAgentTicket).not.toHaveBeenCalled(); + browserMocks.bindAgentTicket.mockRejectedValueOnce(new Error("Unknown agent ticket")); + const second = open(); + await expect(hook({ request: second })).resolves.toBe(second); + expect(warn).toHaveBeenCalledTimes(1); + expect(String(warn.mock.calls[0]?.[0])).toMatch(/could not bind/); + } finally { + warn.mockRestore(); + } + }); +}); + describe("runtime installation compatibility", () => { it("requires at least the Node major required by agent-browser", async () => { const pluginPackage = JSON.parse( From dd81030a88c6580725d5b7f5d94de95591f46f3d Mon Sep 17 00:00:00 2001 From: Keith Kowal <3362598+keithpk@users.noreply.github.com> Date: Mon, 16 Nov 2026 20:40:54 -0800 Subject: [PATCH 2/3] fix(shared-browser): skip label injection when the server is already stored, warn when issuance fails - A resume of an agent whose server was stored at creation (chosen by provider, workspace or project) no longer gets a second ticket from the label path. - A failed or timed out ticket issuance on a labelled agent now logs one warning, like a failed bind. - The label tests run with PASEO_HOME pointed at a scratch path and unstub it afterwards. --- paseo-shared-browser/index.server.ts | 9 ++++++++- paseo-shared-browser/tests/contracts.test.ts | 21 +++++++++++++++++--- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/paseo-shared-browser/index.server.ts b/paseo-shared-browser/index.server.ts index f8bfede8..daeaeb7f 100644 --- a/paseo-shared-browser/index.server.ts +++ b/paseo-shared-browser/index.server.ts @@ -241,9 +241,16 @@ export default function contribute(server: PluginServerContext) { if (request.purpose !== "interactive" || background === true || !request.workspaceId) return request; if (!labels || Object.keys(labels).length === 0) return request; + // A server stored on the agent at creation (chosen by provider, workspace or project) is already there on every resume. + if (mcpServers && Object.hasOwn(mcpServers, MCP_SERVER_ID)) return request; if (!agentGetsBrowserByLabel(await readAccess(true), labels)) return request; const ticket = randomBytes(32).toString("base64url"); - if (!(await issueTicketWithinDeadline(ticket))) return request; + if (!(await issueTicketWithinDeadline(ticket))) { + console.warn( + "shared-browser: could not issue a browser ticket for a labelled agent; the session opens without it", + ); + return request; + } try { await withinDeadline( bindAgentTicket(ticket, request.agentId, request.workspaceId), diff --git a/paseo-shared-browser/tests/contracts.test.ts b/paseo-shared-browser/tests/contracts.test.ts index a77d1a8d..b5b1b22f 100644 --- a/paseo-shared-browser/tests/contracts.test.ts +++ b/paseo-shared-browser/tests/contracts.test.ts @@ -1,5 +1,6 @@ import { readFile } from "node:fs/promises"; -import { dirname, resolve } from "node:path"; +import { tmpdir } from "node:os"; +import { dirname, join, resolve } from "node:path"; import { fileURLToPath } from "node:url"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import contribute from "../index.server"; @@ -502,9 +503,14 @@ describe("agent access", () => { describe("agent access by label", () => { beforeEach(() => { + vi.stubEnv("PASEO_HOME", join(tmpdir(), "shared-browser-label-tests-no-home")); browserMocks.issueAgentTicket.mockReset(); browserMocks.bindAgentTicket.mockReset(); }); + afterEach(() => { + vi.unstubAllEnvs(); + vi.restoreAllMocks(); + }); const ready = (values: Record) => async () => ({ status: "ready", @@ -537,6 +543,14 @@ describe("agent access by label", () => { expect(result.env).toEqual({ EXISTING: "value" }); }); + it("leaves a session alone when the server was already stored on the agent at creation", async () => { + const { open: hook } = captureHooks(ready({ labels: ["team"] })); + const stored = { other: { type: "stdio" }, "shared-browser": { type: "stdio" } }; + const request = open({ reason: "resume", mcpServers: stored }); + expect(await hook({ request })).toBe(request); + expect(browserMocks.issueAgentTicket).not.toHaveBeenCalled(); + }); + it.each(["create", "resume", "refresh", "import"] as const)( "does it again, with a new ticket, when the session reopens (%s)", async (reason) => { @@ -646,8 +660,9 @@ describe("agent access by label", () => { browserMocks.bindAgentTicket.mockRejectedValueOnce(new Error("Unknown agent ticket")); const second = open(); await expect(hook({ request: second })).resolves.toBe(second); - expect(warn).toHaveBeenCalledTimes(1); - expect(String(warn.mock.calls[0]?.[0])).toMatch(/could not bind/); + expect(warn).toHaveBeenCalledTimes(2); + expect(String(warn.mock.calls[0]?.[0])).toMatch(/could not issue/); + expect(String(warn.mock.calls[1]?.[0])).toMatch(/could not bind/); } finally { warn.mockRestore(); } From 87e65df5dc8db0e4bb22709a6603a0b5e15a1e9f Mon Sep 17 00:00:00 2001 From: Keith Kowal <3362598+keithpk@users.noreply.github.com> Date: Mon, 16 Nov 2026 21:00:29 -0800 Subject: [PATCH 3/3] docs(shared-browser): name the labels exception, and test the inherited label key properly - The agent-access doc comment now says four lists and notes that an agent chosen by label gets the tool in every interactive session, resumed ones included. - The README paragraph that says resumed sessions are not modified names the labels exception. - The inherited-key label test uses a label object with no own toString, so only the inherited property could match; swapping Object.hasOwn for the in operator now fails it. --- paseo-shared-browser/README.md | 3 ++- paseo-shared-browser/shared/agent-access.ts | 5 +++-- paseo-shared-browser/tests/contracts.test.ts | 2 +- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/paseo-shared-browser/README.md b/paseo-shared-browser/README.md index 1d3f76c0..9aaffa01 100644 --- a/paseo-shared-browser/README.md +++ b/paseo-shared-browser/README.md @@ -135,7 +135,8 @@ jumps to the chosen workspace's browser panel. The plugin automatically injects its stdio MCP adapter only when a new, non-internal agent is created with a provider that accepts external MCP servers. Agents that already exist, resumed -sessions, imported sessions, and Paseo's internal agents are not modified. Paseo's built-in OMP +sessions, imported sessions, and Paseo's internal agents are not modified (the one exception is the labels list under +"listed" mode, which is applied at every interactive session open, resumed sessions included). Paseo's built-in OMP provider accepts session MCP servers, so new OMP agents receive the adapter. Pi agents also receive the adapter, but require Pi's optional MCP support to launch it. diff --git a/paseo-shared-browser/shared/agent-access.ts b/paseo-shared-browser/shared/agent-access.ts index 31790e3f..83497414 100644 --- a/paseo-shared-browser/shared/agent-access.ts +++ b/paseo-shared-browser/shared/agent-access.ts @@ -2,9 +2,10 @@ * Which new agents get the Shared Browser tool. * * "all" injects the adapter into every new agent (the behaviour before this setting existed, and the - * default). "listed" injects it only when the new agent matches an entry of one of three lists: its + * default). "listed" injects it only when the new agent matches an entry of one of four lists: its * provider id, the id of its workspace, the id of its workspace's project, or one of its labels. An empty configuration - * means no agent. Agents that already exist are never changed. + * means no agent. Agents that already exist are never changed, except that an agent chosen by label gets the tool in + * every interactive session it opens, resumed ones included. * * `agent.create` carries the provider and the working directory but no labels and no workspace id, so * the workspace is found by matching the directory against the daemon's workspaces. diff --git a/paseo-shared-browser/tests/contracts.test.ts b/paseo-shared-browser/tests/contracts.test.ts index b5b1b22f..00c9561e 100644 --- a/paseo-shared-browser/tests/contracts.test.ts +++ b/paseo-shared-browser/tests/contracts.test.ts @@ -570,7 +570,7 @@ describe("agent access by label", () => { ["a different value", { team: "red" }, ["team=blue"]], ["a different key", { squad: "blue" }, ["team"]], ["no labels", {}, ["team"]], - ["a key only inherited from Object", { toString: "x" }, ["toString=y"]], + ["a key only inherited from Object", { other: "x" }, ["toString"]], ])("leaves an agent with %s alone", async (_name, labels, entries) => { const { open: hook } = captureHooks(ready({ labels: entries })); const input = open({ labels });