Open-source AI coworkers that each get a computer of their own: a browser, files and tools, with every action decided before it happens and recorded after. Bring any AG-UI agent.
Fix three things a from-scratch install found (#128)
David McKay319dff2571b8
diff --git a/app/src/lib/computers/queries.ts b/app/src/lib/computers/queries.tsindex 03aba9fb..84cf9c09 100644--- a/app/src/lib/computers/queries.ts+++ b/app/src/lib/computers/queries.ts@@ -41,17 +41,20 @@ export const computerKeys = {};/**- * A placeholder id in the path. The endpoint answers with every computer regardless, so this is- * addressing a collection through a member's route rather than naming one.+ * The deployment-wide fleet route.+ *+ * Not a Bot id in a member route, which is what this used to be. That placeholder stopped working+ * when the server began checking whether the caller may act as the Bot in the path: a placeholder+ * is not a Bot, so the list 404d and this screen showed nothing at all.*/-const FLEET_ID = "openbot-computer";+const FLEET_PATH = "/api/computers/fleet";/** No envelope key: the body carries both the list and the isolation mode. */export function computerFleetQueryOptions() {return queryOptions({queryKey: computerKeys.fleet(),queryFn: async (): Promise<ComputerFleet> => {- const response = await client(`/api/computers/${FLEET_ID}/computers`, {+ const response = await client(FLEET_PATH, {fallback: "The computers could not be listed.",});return response.json();diff --git a/scripts/start.sh b/scripts/start.shindex 986727bd..3dbd9d5f 100755--- a/scripts/start.sh+++ b/scripts/start.sh@@ -141,13 +141,16 @@ for table in agent_profiles agent_preferences; dodonegreen " coworker tables migrated"-MANAGED_URL="$(grep -E '^MANAGED_AGENT_AG_UI_URL=' "$ROOT/.env" | tail -1 | cut -d= -f2-)"-if [ -z "$MANAGED_URL" ]; then- red " MANAGED_AGENT_AG_UI_URL is not set in .env."- red " See .env.example."- exit 1-fi-green " managed coworker endpoint: $MANAGED_URL"+# Resolved at the top, where the default that .env deliberately does not carry is applied. Reading+# .env again here would have demanded the line be present in a file the comment above that default+# says must not contain it, which is how this came to refuse every fresh clone: `cp .env.example+# .env` leaves it commented out, so the grep matched nothing.+#+# It failed silently, too. Under `set -o pipefail` a grep that matches nothing fails its whole+# pipeline, and `set -e` then aborts the assignment before the check below could say why. The same+# grep survives inside `setting()` only because `local v="$(...)"` takes `local`'s own exit status+# and masks it. So: report what was resolved, and do not re-read the file.+green " managed coworker endpoint: $MANAGED_AGENT_AG_UI_URL"info "2/4 Server"require_free_or_ours "$SERVER_PORT" serverdiff --git a/server/src/components/store.ts b/server/src/components/store.tsindex 31e9afea..33304230 100644--- a/server/src/components/store.ts+++ b/server/src/components/store.ts@@ -125,7 +125,7 @@ export function createComponentStore(database: Database): ComponentStore {const missing = entries.filter((entry) => !known.has(entry.name));if (missing.length === 0) return { added: [] };- await database+ const inserted = await database.insert(components).values(missing.map((entry) => ({@@ -142,9 +142,15 @@ export function createComponentStore(database: Database): ComponentStore {)// Two browsers announcing the same new component at once is ordinary, not a fault: whichever// arrives second must be a no-op rather than a duplicate-key error thrown at a page load.- .onConflictDoNothing();+ .onConflictDoNothing()+ // What was actually inserted, which after `onConflictDoNothing` is only the rows this call+ // won the race for. Returning `missing` instead would report the intent: the loser of that+ // ordinary race would name every component it had tried to insert, and the caller writes an+ // audit row per name, so a first start with two tabs open recorded all thirteen components+ // twice. The insert was already protected; the trail was not.+ .returning({ name: components.name });- return { added: missing.map((entry) => entry.name) };+ return { added: inserted.map((row) => row.name) };},async list() {diff --git a/server/src/computer/routes.ts b/server/src/computer/routes.tsindex d50c663f..23688664 100644--- a/server/src/computer/routes.ts+++ b/server/src/computer/routes.ts@@ -32,7 +32,7 @@ import { type PolicyStore, parseActionPolicy } from "./policy-store";* The bot-access middleware matches `/:botId/*`, and Hono's `/*` matches zero segments, so a* single-segment path like `/policy` reaches it as a Bot id. These have their own guards.*/-const DEPLOYMENT_ROUTES = new Set(["policy"]);+const DEPLOYMENT_ROUTES = new Set(["policy", "fleet"]);export function createComputerRoutes(gateway: ComputerGateway,@@ -229,6 +229,26 @@ export function createComputerRoutes(* it holds a list. `:botId` is still there because every route under this router has it. The* list itself is every computer, so a signed-in user is not enough; an administrator has to ask.*/+ /**+ * Every computer in the deployment.+ *+ * Its own route rather than `/:botId/computers`, which is what Admin used to call with a+ * placeholder id. That worked until the bot-access middleware arrived: the placeholder is not a+ * Bot, `canUseBot` said so, and the screen 404d for everybody including an administrator, showing+ * an empty page rather than the fleet. The list is deployment-wide, so it is addressed+ * deployment-wide and named in DEPLOYMENT_ROUTES beside `/policy`.+ */+ routes.get("/fleet", requireUser, async (context) => {+ const denied = requireAdmin(context);+ if (denied) return denied;++ try {+ return context.json(await gateway.computers());+ } catch (error) {+ return context.json({ error: describe(error) }, statusFor(error));+ }+ });+routes.get("/:botId/computers", async (context) => {// The session guard and the question of whether this person may act as the Bot in the path are// both applied by the middleware above. Neither is the question here: the answer is the wholediff --git a/server/tests/component-store.integration.test.ts b/server/tests/component-store.integration.test.tsindex 1acc2db1..a71e25ae 100644--- a/server/tests/component-store.integration.test.ts+++ b/server/tests/component-store.integration.test.ts@@ -285,6 +285,36 @@ describe("learning what a build ships", () => {expect(stored?.withheldFrom).toEqual([botA]);});+ test("two browsers announcing at once add it once, and record it once", async () => {+ /*+ * The insert has always been safe: `onConflictDoNothing` is there because two page loads racing+ * is ordinary. What was not safe was the answer. `added` reported what the call had *tried* to+ * insert, so the loser of that ordinary race named every component anyway, and the caller writes+ * one audit row per name. A first start with two tabs open recorded all thirteen shipped+ * components twice, in the trail whose whole value is that it says what happened once.+ *+ * Concurrent rather than sequential on purpose: the sequential case was always covered by+ * "announcing again adds nothing", and it passed throughout.+ */+ const racer = {+ name: `testRaced_${suite}`,+ title: "Raced",+ kind: "card",+ description: "Announced by two page loads at the same moment.",+ };++ const [first, second] = await Promise.all([+ store.syncCatalogue([racer]),+ store.syncCatalogue([racer]),+ ]);++ const named = [...first.added, ...second.added];+ expect(named).toEqual([racer.name]);++ const rows = (await store.list()).filter((one) => one.name === racer.name);+ expect(rows).toHaveLength(1);+ });+test("announcing nothing is not a request to forget everything", async () => {// A build with no components, or a failed manifest, must not empty the catalogue.const before = (await store.list()).length;diff --git a/server/tests/computer-fleet-route.test.ts b/server/tests/computer-fleet-route.test.tsnew file mode 100644index 00000000..7d798d4a--- /dev/null+++ b/server/tests/computer-fleet-route.test.ts@@ -0,0 +1,95 @@+import { describe, expect, test } from "bun:test";+import type { MiddlewareHandler } from "hono";+import { Hono } from "hono";+import type { AppVariables } from "../src/auth/guards";+import type { ComputerGateway } from "../src/computer/gateway";+import type { PolicyStore } from "../src/computer/policy-store";+import { createComputerRoutes } from "../src/computer/routes";++/**+ * The fleet is a fact about the deployment, not about a Bot. This is the same bug as the boundary+ * one in computer-policy-route.test.ts, a second time.+ *+ * Admin listed the fleet by calling `/:botId/computers` with a placeholder id, on the reasoning that+ * the endpoint answers with every computer whatever the path says. True when it was written. Then+ * the bot-access middleware arrived, the placeholder was not a Bot, `canUseBot` said so, and+ * `/admin/computers` answered 404 for everybody including an administrator. The screen renders+ * nothing at all while the list is null, so it did not even look broken: it looked like a+ * deployment with no computers, while two containers were running.+ *+ * The middleware's own comment asked for this: "a second deployment-wide route added later should+ * have to think about this line." It was added later and did not. So the fleet has a route of its+ * own now, and this holds it there.+ *+ * Found by driving the screen against a deployment that had computers, not by reading the diff.+ */++const ADMIN = { id: "u1", email: "[email protected]", role: "admin" } as const;++function app(role: "admin" | "user" = "admin") {+ const asActor: MiddlewareHandler<{ Variables: AppVariables }> = async (+ context,+ next,+ ) => {+ context.set("actor", { ...ADMIN, role });+ await next();+ };++ const gateway = {+ computers: async () => ({+ isolation: "per-bot",+ computers: [+ { botId: "risk-analyst", running: true, startedAt: null },+ { botId: "general-assistant", running: false, startedAt: null },+ ],+ }),+ } as unknown as ComputerGateway;++ const policyStore = {+ get: () => ({ mode: "enforce", deny: [], allow: ["true"] }),+ set: async () => undefined,+ } as unknown as PolicyStore;++ const routes = createComputerRoutes(+ gateway,+ policyStore,+ asActor,+ // No Bot is reachable, which is the deployment the bug showed up in: listing the fleet must not+ // depend on the caller having access to a Bot that happens to share the collection's name.+ async () => false,+ );++ return new Hono<{ Variables: AppVariables }>().route(+ "/api/computers",+ routes,+ );+}++describe("listing every computer in the deployment", () => {+ test("an administrator gets the fleet, whatever Bots they can reach", async () => {+ const response = await app().request("http://t/api/computers/fleet");++ expect(response.status).toBe(200);+ expect(await response.json()).toMatchObject({+ isolation: "per-bot",+ computers: [{ botId: "risk-analyst" }, { botId: "general-assistant" }],+ });+ });++ test("it is still administrator-only", async () => {+ // The exemption is from the bot-access check, not from the admin check. The fleet names every+ // Bot in the deployment, which is exactly what a plain user must not be handed.+ const response = await app("user").request("http://t/api/computers/fleet");++ expect(response.status).toBe(403);+ });++ test("a Bot route is still gated", async () => {+ // One named path, not a hole.+ const response = await app().request(+ "http://t/api/computers/some-bot/status",+ );++ expect(response.status).toBe(404);+ });+});