Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix: refuse a teammate's browser profile change while any of its work runs

Plateau Nguyencbc054604213
diff --git a/server/bot-browser-profile.ts b/server/bot-browser-profile.ts
index 4a2bb315dc..d8d5c37152 100644
--- a/server/bot-browser-profile.ts
+++ b/server/bot-browser-profile.ts
@@ -14,6 +14,8 @@ export interface BrowserProfileGuards {
profiles(): readonly Pick<BrowserProfile, "id" | "name">[];
heldBy(session: string): unknown;
currentBrowserSession(botId: string, profile: string | undefined): string;
+ /** Work the bot runs beyond its own busy flag: a room turn, a routine run, a turn just handed to it. */
+ turnRunning?(botId: string): boolean;
}
type GuardedBot = { id: string; busy?: boolean; browserProfile?: string };
@@ -21,7 +23,7 @@ type GuardedBot = { id: string; busy?: boolean; browserProfile?: string };
/** The one gate on a bot's browserProfile: null or "" selects the bot's own browser. */
export function checkBrowserProfileChange(bot: GuardedBot | null | undefined, requested: unknown, guards: BrowserProfileGuards): BrowserProfileChange {
const profile = requested === null || requested === "" ? undefined : requested;
- if (bot?.busy && profile !== bot.browserProfile) {
+ if (bot && (bot.busy || guards.turnRunning?.(bot.id)) && profile !== bot.browserProfile) {
return { ok: false, status: 409, error: "stop this bot's turn before changing its browser profile" };
}
if (bot && profile !== bot.browserProfile && guards.heldBy(guards.currentBrowserSession(bot.id, bot.browserProfile))) {
diff --git a/server/drivers/agents-catalog-wire.test.ts b/server/drivers/agents-catalog-wire.test.ts
index b5fb14feeb..345d654843 100644
--- a/server/drivers/agents-catalog-wire.test.ts
+++ b/server/drivers/agents-catalog-wire.test.ts
@@ -85,36 +85,36 @@ const FULL = "skills+shared+voice+x+team+images+hosting";
/** Bytes when the budget was last set; a profile may move at most 2% either way (lower it to lock in a win). */
const BUDGET_BASELINE: Record<string, number> = {
- "default": 63515,
- "voice": 64296,
- "shared": 65732,
- "shared+voice": 66513,
- "skills": 66543,
- "skills+voice": 67324,
- "skills+shared": 68761,
- "skills+shared+voice": 69494,
- "non-chief": 54155,
- "voice+non-chief": 54936,
- "shared+non-chief": 56420,
- "shared+voice+non-chief": 57201,
- "skills+non-chief": 57231,
- "skills+voice+non-chief": 58012,
- "skills+shared+non-chief": 59496,
- "skills+shared+voice+non-chief": 60277,
- "skills+shared+voice+cloud-home": 69260,
- "skills+shared+voice+non-chief+cloud-home": 60043,
- "default+legacy-switches": 63515,
- "default+mausbot": 64166,
- "skills+shared+voice+mausbot": 70145,
- "skills+shared+voice+x": 73318,
- "skills+shared+voice+x+team": 73318,
- "default+no-computer-pick": 62433,
- "default+room": 59950,
- "non-chief+room": 50594,
- "skills+shared+voice+x+team+images": 74091,
- "default+images": 64288,
- "skills+shared+voice+x+team+images+hosting": 75930,
- "default+hosting": 65475,
+ "default": 64748,
+ "voice": 65529,
+ "shared": 67013,
+ "shared+voice": 67794,
+ "skills": 67824,
+ "skills+voice": 68605,
+ "skills+shared": 70089,
+ "skills+shared+voice": 70870,
+ "non-chief": 55049,
+ "voice+non-chief": 55830,
+ "shared+non-chief": 57314,
+ "shared+voice+non-chief": 58095,
+ "skills+non-chief": 58125,
+ "skills+voice+non-chief": 58906,
+ "skills+shared+non-chief": 60390,
+ "skills+shared+voice+non-chief": 61171,
+ "skills+shared+voice+cloud-home": 70636,
+ "skills+shared+voice+non-chief+cloud-home": 60937,
+ "default+legacy-switches": 64748,
+ "default+mausbot": 65399,
+ "skills+shared+voice+mausbot": 71521,
+ "skills+shared+voice+x": 74709,
+ "skills+shared+voice+x+team": 74709,
+ "default+no-computer-pick": 63536,
+ "default+room": 61038,
+ "non-chief+room": 51339,
+ "skills+shared+voice+x+team+images": 75482,
+ "default+images": 65521,
+ "skills+shared+voice+x+team+images+hosting": 77345,
+ "default+hosting": 66611,
};
const RPC_PREFIX = '{"jsonrpc":"2.0","id":1,"result":';
diff --git a/server/index.test.ts b/server/index.test.ts
index d8cbb0aab8..9b2b9c3bb2 100644
--- a/server/index.test.ts
+++ b/server/index.test.ts
@@ -10297,6 +10297,33 @@ describe("harness HTTP API", () => {
}
});
+ it("tells a bot its current browser profile and who can change it when it asks select_computer", async () => {
+ const bot = (await api("POST", "/api/bots")).body.bot;
+ try {
+ expect((await api("PATCH", "/api/config", { features: { browser: true }, browserProfiles: [{ id: "work", name: "Work" }] })).status).toBe(200);
+ expect((await api("PATCH", `/api/bots/${bot.id}`, {
+ browserProfile: "work",
+ modelSelection: { instanceId: "claude", model: "claude-sonnet-5" },
+ })).status).toBe(200);
+ rmSync(fakeClaudeDump, { force: true });
+ expect((await api("POST", `/api/bots/${bot.id}/messages`, { text: "stay active" })).status).toBe(202);
+ const dump = await readJsonFileWhenReady<{ mcpConfig: { mcpServers: { agents: { env: { OMB_COMMS_TOKEN: string } } } } }>(fakeClaudeDump);
+ const asked = await fetch(`${BASE}/api/internal/computer/select`, {
+ headers: { authorization: `Bearer ${dump.mcpConfig.mcpServers.agents.env.OMB_COMMS_TOKEN}` },
+ });
+ expect(asked.status).toBe(200);
+ expect(((await asked.json()) as { browserProfile: unknown }).browserProfile).toEqual({
+ id: "work",
+ name: '"Work"',
+ changedBy: expect.stringMatching(/the person, in this bot's Browser tab; a Chief of Staff can set it for an idle teammate/),
+ });
+ } finally {
+ await api("POST", `/api/bots/${bot.id}/interrupt`, {}).catch(() => undefined);
+ await api("PATCH", "/api/config", { features: { browser: false }, browserProfiles: [] }).catch(() => undefined);
+ await api("DELETE", `/api/bots/${bot.id}`).catch(() => undefined);
+ }
+ }, 60_000);
+
it.each(["direct", "room"] as const)("mounts the browser engine's MCP server and the sign-in policy in %s turns and preview", async (target) => {
const bot = (await api("POST", "/api/bots")).body.bot;
let room: any;
diff --git a/server/index.ts b/server/index.ts
index 518c61c48d..fdcb6d114f 100644
--- a/server/index.ts
+++ b/server/index.ts
@@ -9209,7 +9209,7 @@ const profileRequests = new ProfileRequestService({
store,
autoApply: appliesDirectly,
canPersist: proposalPersistence,
- validateTarget: chiefPeerTargetRule("profile"), browserProfile: { guards: { profiles: () => cfg.browserProfiles ?? [], heldBy: (session) => browserRuntime.heldBy(session), currentBrowserSession: (botId, profile) => currentBrowserSession(botId, profile) }, cleanup: { closeForBot: (botId) => browserLive.closeForBot(botId), forgetTemporaryBrowser } },
+ validateTarget: chiefPeerTargetRule("profile"), browserProfile: { guards: { profiles: () => cfg.browserProfiles ?? [], heldBy: (session) => browserRuntime.heldBy(session), currentBrowserSession: (botId, profile) => currentBrowserSession(botId, profile), turnRunning: (botId) => Boolean(hasDirectDispatch(botId) || activeGroupTurnForBot(botId) || routines?.activeRunForBot(botId)) }, cleanup: { closeForBot: (botId) => browserLive.closeForBot(botId), forgetTemporaryBrowser } },
});
const modelRequests = new ModelRequestService({
store,
diff --git a/server/profile-requests-browser-profile.test.ts b/server/profile-requests-browser-profile.test.ts
index a0d7a484e6..c36672be25 100644
--- a/server/profile-requests-browser-profile.test.ts
+++ b/server/profile-requests-browser-profile.test.ts
@@ -48,6 +48,7 @@ const fullAccess: DirectApplyCheck = () => appliesNow({ fullAccess: true, ownerM
function harness(options: { autoApply?: DirectApplyCheck; chiefs?: string[] } = {}) {
const store = new MemoryStore();
const held = new Set<string>();
+ const running = new Set<string>();
const closeForBot = vi.fn<(botId: string) => void>();
const forgetTemporaryBrowser = vi.fn(async (_botId: string) => {});
const addBot = (name: string, fields: Partial<BotRecord> = {}): BotRecord => {
@@ -66,11 +67,11 @@ function harness(options: { autoApply?: DirectApplyCheck; chiefs?: string[] } =
autoApply: options.autoApply,
validateTarget: (proposer) => (chiefs.has(proposer) ? null : "Only a Chief of Staff can change another bot's profile."),
browserProfile: {
- guards: { profiles: () => PROFILES, heldBy: (session) => held.has(session), currentBrowserSession: (botId, profile) => `${botId}:${profile ?? "own"}` },
+ guards: { profiles: () => PROFILES, heldBy: (session) => held.has(session), currentBrowserSession: (botId, profile) => `${botId}:${profile ?? "own"}`, turnRunning: (botId) => running.has(botId) },
cleanup: { closeForBot, forgetTemporaryBrowser },
},
});
- return { store, service, chief, addBot, held, closeForBot, forgetTemporaryBrowser };
+ return { store, service, chief, addBot, held, running, closeForBot, forgetTemporaryBrowser };
}
describe("propose_profile browserProfile", () => {
@@ -127,6 +128,33 @@ describe("propose_profile browserProfile", () => {
expect(later.browserProfile).toBe("work");
});
+ it.each(["a room turn", "a routine run", "a turn just handed to it"])("refuses a teammate that is not marked busy but runs %s, at proposal, at confirm and at Undo", () => {
+ const { service, chief, addBot, running } = harness();
+ const peer = addBot("Peer");
+ running.add(peer.id);
+ expect(() => service.propose({ botId: chief.id, threadId: chief.threadId, targetBotId: peer.id, changes: { browserProfile: "work" }, reason: "asked" }))
+ .toThrow("@Peer: stop this bot's turn before changing its browser profile");
+
+ running.delete(peer.id);
+ const card = service.propose({ botId: chief.id, threadId: chief.threadId, targetBotId: peer.id, changes: { browserProfile: "work" }, reason: "asked" });
+ running.add(peer.id);
+ expect(service.resolve({ botId: chief.id, threadId: chief.threadId, requestId: card.requestId, behavior: "allow" }))
+ .toMatchObject({ state: "invalid", status: 409 });
+ expect(peer.browserProfile).toBeUndefined();
+
+
+ const applying = harness({ autoApply: fullAccess });
+ const other = applying.addBot("Other");
+ const applied = applying.service.submit({ botId: applying.chief.id, threadId: applying.chief.threadId, targetBotId: other.id, changes: { browserProfile: "work" }, reason: "asked" });
+ applying.running.add(other.id);
+ expect(applying.service.undo({ botId: applying.chief.id, threadId: applying.chief.threadId, requestId: applied.requestId }))
+ .toMatchObject({ state: "invalid", status: 409, error: "stop this bot's turn before changing its browser profile" });
+ expect(other.browserProfile).toBe("work");
+ applying.running.delete(other.id);
+ expect(applying.service.undo({ botId: applying.chief.id, threadId: applying.chief.threadId, requestId: applied.requestId })).toMatchObject({ state: "undone" });
+ expect(other.browserProfile).toBeUndefined();
+ });
+
it("refuses a profile that does not exist and never creates one", () => {
const { service, chief, addBot, closeForBot } = harness({ autoApply: fullAccess });
const peer = addBot("Peer");