Skip to content
Merged
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
20 changes: 20 additions & 0 deletions paseo-shared-browser/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,26 @@ The provider launches the stdio adapter on the Paseo daemon host, beside the dae
runtime. Web, desktop, and mobile clients never own or host the adapter or Chromium, and a client
disconnect does not close or reset either process.

### Choosing which agents get the tool

By default every new agent gets the adapter. Settings, under Shared Browser, can restrict that:

- **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.

`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
the deepest one containing it. Both directories are normalized first (separators, `.` and `..` segments),
so a path that climbs out of a workspace does not match it. The lookup includes background workspaces. If
the list is longer than 20 pages of 200, or the lookup fails, those lists match nothing. Workspaces that
share a directory cannot be told apart, so listing any of them matches them all. Matching is lexical: nothing on disk is consulted, so symlinks are not resolved and letter case is not folded. The settings read has a 2 second deadline, and a read that is too slow is treated like an unreadable setting, so every new agent gets the tool. The workspace lookup has its own 2 second deadline, and a timeout there counts as no match. Either timeout logs one line, and a slow lookup never holds up agent creation. The check runs before the adapter, its credential or the supervisor are set up, so
an agent that is not allowed costs nothing. It applies to agents created after the setting is saved. A
setting that cannot be read or is invalid falls back to every new agent, and the plugin log gets one line
saying so. The settings screen reports an entry over 256 characters or a list over 200 entries instead of
cutting it off.

## Runtime environment overrides

The plugin recognizes only these deployment overrides:
Expand Down
143 changes: 143 additions & 0 deletions paseo-shared-browser/client/agent-access-settings.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
import { type PluginSurfaceProps, useSettings } from "@getpaseo/plugin/client";
import {
SettingsAction,
SettingsCard,
SettingsInput,
type SettingsInputHandle,
SettingsSection,
SettingsSelect,
} from "@getpaseo/plugin/client/ui";
import { useRef, useState } from "react";
import { Text } from "react-native";
import { type AgentAccessMode, agentAccessSettings, parseEntries } from "../shared/agent-access";

const MODE_OPTIONS: readonly { label: string; value: AgentAccessMode }[] = [
{ label: "Every new agent", value: "all" },
{ label: "Only the agents listed below", value: "listed" },
];

type ListName = "providers" | "workspaces" | "projects";
const NO_ERRORS: Record<ListName, string | null> = {
providers: null,
workspaces: null,
projects: null,
};

/** Settings screen: which new agents get the browser tool. */
export function AgentAccessSettings({ theme }: PluginSurfaceProps) {
const settings = useSettings(agentAccessSettings);
const providers = useRef<SettingsInputHandle>(null);
const workspaces = useRef<SettingsInputHandle>(null);
const projects = useRef<SettingsInputHandle>(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;
errors: Record<ListName, string | null>;
}>({ revision: null, errors: NO_ERRORS });
const muted = { color: theme.colors.foregroundMuted, fontSize: 13, lineHeight: 19 };

if (settings.status === "loading") {
return (
<SettingsSection title="Shared Browser: agent access">
<Text style={muted}>Loading</Text>
</SettingsSection>
);
}
if (settings.status !== "ready") {
return (
<SettingsSection title="Shared Browser: agent access">
<Text style={muted}>{settings.error}</Text>
<SettingsCard>
<SettingsAction label="Try again" actionLabel="Reload" onPress={settings.reload} />
{settings.status === "invalid" ? (
<SettingsAction
label="Back to the default"
actionLabel="Reset"
onPress={settings.reset}
/>
) : null}
</SettingsCard>
</SettingsSection>
);
}

const { values } = settings;
const listErrors = found.revision === settings.revision ? found.errors : NO_ERRORS;
const save = (next: typeof values) => void settings.save(next, settings.revision);
// The lists as typed, parsed the same way for Save and for a mode change, so unsaved text is never
// lost to the revision remount. A list that does not parse shows its error and nothing is saved.
const typedLists = () => {
const parsed = {
providers: parseEntries(providers.current?.getText() ?? ""),
workspaces: parseEntries(workspaces.current?.getText() ?? ""),
projects: parseEntries(projects.current?.getText() ?? ""),
};
const errors = {
providers: parsed.providers.error,
workspaces: parsed.workspaces.error,
projects: parsed.projects.error,
};
setFound({ revision: settings.revision, errors });
if (errors.providers || errors.workspaces || errors.projects) return null;
return {
providers: parsed.providers.entries,
workspaces: parsed.workspaces.entries,
projects: parsed.projects.entries,
};
};
return (
<SettingsSection title="Shared Browser: agent access">
{/* Keyed on the revision, so the text inputs start over from the saved lists when another client changes them. */}
<SettingsCard key={settings.revision}>
Comment thread
keithpk marked this conversation as resolved.
Resolved
<SettingsSelect
label="Browser tool for"
hint="Applies to agents created from now on. With the lists empty, no agent gets the tool."
error={settings.saveError}
value={values.mode}
options={MODE_OPTIONS}
disabled={settings.saving}
onValueChange={(mode) => {
const lists = typedLists();
if (lists) save({ ...values, ...lists, mode });
}}
/>
Comment thread
keithpk marked this conversation as resolved.
Resolved
<SettingsInput
ref={providers}
label="Provider ids"
hint="Separated by commas or new lines. An agent whose provider is listed gets the tool."
initialValue={values.providers.join(", ")}
error={listErrors.providers}
onChangeText={() => {}}
disabled={settings.saving}
/>
<SettingsInput
ref={workspaces}
label="Workspace ids"
hint="Every new agent in a listed workspace gets the tool."
initialValue={values.workspaces.join(", ")}
error={listErrors.workspaces}
onChangeText={() => {}}
disabled={settings.saving}
/>
<SettingsInput
ref={projects}
label="Project ids"
hint="Every new agent in a workspace of a listed project gets the tool."
initialValue={values.projects.join(", ")}
error={listErrors.projects}
onChangeText={() => {}}
disabled={settings.saving}
/>
<SettingsAction
label="Save the lists"
actionLabel="Save"
disabled={settings.saving}
onPress={() => {
const lists = typedLists();
if (lists) save({ ...values, ...lists });
}}
/>
</SettingsCard>
</SettingsSection>
);
}
8 changes: 8 additions & 0 deletions paseo-shared-browser/index.client.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { PluginClientContext } from "@getpaseo/plugin/client";
import { AgentAccessSettings } from "./client/agent-access-settings";
import { contributeSharedBrowserClient, SharedBrowserPanel } from "./client/browser";
import { contributeSharedBrowserSidebar } from "./client/sidebar";

Expand All @@ -21,9 +22,16 @@ export default function contribute(client: PluginClientContext) {
},
});

const removeSettings = client.addSettingsScreen({
id: "agent-access",
title: "Shared Browser",
icon: "PanelsTopLeft",
Component: AgentAccessSettings,
});
const removeSidebar = contributeSharedBrowserSidebar(client);
const cleanup = contributeSharedBrowserClient(client);
return () => {
removeSettings();
removeSidebar();
return cleanup();
};
Expand Down
82 changes: 71 additions & 11 deletions paseo-shared-browser/index.server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { randomBytes } from "node:crypto";
import { homedir } from "node:os";
import { join } from "node:path";
import type { PluginServerContext } from "@getpaseo/plugin/server";
import type { PluginHookContext, PluginServerContext } from "@getpaseo/plugin/server";
import {
bindAgentTicket,
cleanupBrowserServer,
Expand Down Expand Up @@ -33,6 +33,13 @@ import {
revokeAgentBrowserAccess,
} from "./server/browser";
import { resolveBrowserRuntimeRoot } from "./server/runtime-path";
import { listAllWorkspaces } from "./server/workspace-list";
import {
accessFromState,
agentAccessSettings,
agentGetsBrowser,
DEFAULT_AGENT_ACCESS,
} from "./shared/agent-access";
import {
acquireControlRpc,
applyDevicePresetRpc,
Expand Down Expand Up @@ -63,25 +70,34 @@ import { readBrowserVideoRpc } from "./shared/browser-video";
const TICKET_ENV = "PASEO_SHARED_BROWSER_TICKET";
const MCP_SERVER_ID = "shared-browser";
const TICKET_ISSUE_TIMEOUT_MS = 2_000;
const ACCESS_DECISION_TIMEOUT_MS = 2_000;

async function issueTicketWithinDeadline(ticket: string): Promise<boolean> {
/** The result of `work`, or a rejection when it has not settled within `ms`. */
async function withinDeadline<T>(work: Promise<T>, ms: number, message: string): Promise<T> {
let timer: NodeJS.Timeout | undefined;
try {
await Promise.race([
issueAgentTicket(ticket),
return await Promise.race([
work,
new Promise<never>((_, reject) => {
timer = setTimeout(
() => reject(new Error("Shared Browser ticket issuance timed out")),
TICKET_ISSUE_TIMEOUT_MS,
);
timer = setTimeout(() => reject(new Error(message)), ms);
timer.unref();
}),
]);
} finally {
clearTimeout(timer);
}
}

async function issueTicketWithinDeadline(ticket: string): Promise<boolean> {
try {
await withinDeadline(
issueAgentTicket(ticket),
TICKET_ISSUE_TIMEOUT_MS,
"Shared Browser ticket issuance timed out",
);
return true;
} catch {
return false;
} finally {
clearTimeout(timer);
}
}

Expand All @@ -95,6 +111,45 @@ function mcpBundlePath(): string {

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 () => {
try {
const state = await withinDeadline(
Promise.resolve(agentAccess.read()),
ACCESS_DECISION_TIMEOUT_MS,
"the read timed out",
);
Comment thread
keithpk marked this conversation as resolved.
Resolved
const access = accessFromState(state);
if (access === DEFAULT_AGENT_ACCESS) {
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`,
);
return DEFAULT_AGENT_ACCESS;
}
};
// The workspace lookup has its own deadline; a timeout or any other failure there is no match.
const lookupWorkspaces = async (paseo: PluginHookContext["paseo"]) => {
try {
return await withinDeadline(
listAllWorkspaces(paseo),
ACCESS_DECISION_TIMEOUT_MS,
"the workspace lookup timed out",
);
} catch (error) {
console.warn(
`shared-browser: the workspace lookup failed (${error instanceof Error ? error.message : "error"}); the agent is not matched to a workspace or project`,
);
throw error;
}
};
server.handle(attachBrowserRpc, handleAttachBrowser);
server.handle(detachBrowserRpc, handleDetachBrowser);
server.handle(closeBrowserRpc, handleCloseBrowser);
Expand All @@ -118,8 +173,13 @@ export default function contribute(server: PluginServerContext) {
server.handle(resizeBrowserRpc, handleResizeBrowser);
server.handle(applyDevicePresetRpc, handleApplyDevicePreset);
server.handle(sendBrowserInputRpc, handleSendBrowserInput);
server.before("agent.create", async ({ request }) => {
server.before("agent.create", async ({ request }, context) => {
if (request.config.internal) return request;
// Decide before any ticket or supervisor work, so an agent that is not allowed costs nothing.
const allowed = await agentGetsBrowser(await readAccess(), request.config, () =>
lookupWorkspaces(context.paseo),
);
if (!allowed) return request;
const ticket = randomBytes(32).toString("base64url");
if (!(await issueTicketWithinDeadline(ticket))) return request;
return {
Expand Down
4 changes: 2 additions & 2 deletions paseo-shared-browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@
"check:write": "biome check --write .",
"typecheck": "tsc --noEmit",
"prepare:runtime": "node scripts/prepare-runtime.mjs",
"test": "vitest run tests/contracts.test.ts tests/lease.test.ts tests/runtime-lifecycle.test.ts tests/chromium-release.test.ts tests/prepare-runtime.test.ts tests/agent-access.test.ts tests/agent-input-safety.test.ts tests/tabs.test.ts tests/tabs-runtime-loss.test.ts tests/composer-pills.test.ts tests/sidebar.test.ts tests/pointer-mapping.test.ts tests/frame-lifecycle.test.ts tests/viewport-resync.test.ts client server shared",
"test:unit": "vitest run tests/contracts.test.ts tests/lease.test.ts tests/runtime-lifecycle.test.ts tests/chromium-release.test.ts tests/prepare-runtime.test.ts tests/agent-access.test.ts tests/agent-input-safety.test.ts tests/tabs.test.ts tests/tabs-runtime-loss.test.ts tests/composer-pills.test.ts tests/sidebar.test.ts tests/pointer-mapping.test.ts tests/frame-lifecycle.test.ts tests/viewport-resync.test.ts client server shared",
"test": "vitest run tests/contracts.test.ts tests/lease.test.ts tests/runtime-lifecycle.test.ts tests/chromium-release.test.ts tests/prepare-runtime.test.ts tests/agent-access.test.ts tests/agent-filter.test.ts tests/workspace-list.test.ts tests/agent-input-safety.test.ts tests/tabs.test.ts tests/tabs-runtime-loss.test.ts tests/composer-pills.test.ts tests/sidebar.test.ts tests/pointer-mapping.test.ts tests/frame-lifecycle.test.ts tests/viewport-resync.test.ts client server shared",
"test:unit": "vitest run tests/contracts.test.ts tests/lease.test.ts tests/runtime-lifecycle.test.ts tests/chromium-release.test.ts tests/prepare-runtime.test.ts tests/agent-access.test.ts tests/agent-filter.test.ts tests/workspace-list.test.ts tests/agent-input-safety.test.ts tests/tabs.test.ts tests/tabs-runtime-loss.test.ts tests/composer-pills.test.ts tests/sidebar.test.ts tests/pointer-mapping.test.ts tests/frame-lifecycle.test.ts tests/viewport-resync.test.ts client server shared",
"test:smoke": "vitest run tests/browser.smoke.test.ts tests/browser.drag-cancel.smoke.test.ts tests/xvfb.smoke.test.ts tests/browser.failed-open.smoke.test.ts tests/browser.background-tab.smoke.test.ts --testTimeout=30000",
"test:tabs": "vitest run tests/browser-tabs.smoke.test.ts --testTimeout=30000",
"test:video": "vitest run tests/video.smoke.test.ts --testTimeout=30000"
Expand Down
42 changes: 42 additions & 0 deletions paseo-shared-browser/server/workspace-list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import type { WorkspaceRef } from "../shared/agent-access";

export const WORKSPACE_PAGE_SIZE = 200;
export const WORKSPACE_PAGE_LIMIT = 20;

interface WorkspacePage {
entries: readonly WorkspaceRef[];
pageInfo: { nextCursor?: string | null; hasMore: boolean };
}

export interface WorkspaceLister {
workspaces: {
list(options: {
filter: { includeBackground: true };
page: { limit: number; cursor?: string };
}): Promise<WorkspacePage>;
};
}

/**
* Every workspace the daemon knows, background ones included. An access decision made from a partial
* list could match a shallower workspace than the agent is really in, so a list that is still
* incomplete after the page limit is an error, and the caller counts it as a failed lookup.
*/
export async function listAllWorkspaces(
paseo: WorkspaceLister,
pageLimit: number = WORKSPACE_PAGE_LIMIT,
): Promise<WorkspaceRef[]> {
const workspaces: WorkspaceRef[] = [];
let cursor: string | undefined;
for (let page = 0; page < pageLimit; page += 1) {
const result = await paseo.workspaces.list({
filter: { includeBackground: true },
page: { limit: WORKSPACE_PAGE_SIZE, ...(cursor ? { cursor } : {}) },
});
workspaces.push(...result.entries);
if (!result.pageInfo.hasMore) return workspaces;
cursor = result.pageInfo.nextCursor ?? undefined;
if (!cursor) throw new Error("Workspace list reports more pages but gave no cursor");
}
throw new Error(`Workspace list is longer than ${pageLimit} pages`);
}
Loading
Loading