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.
Route on what a coworker can reach, not only what it is for (#144)
David McKaye905640288c7
diff --git a/server/src/app.ts b/server/src/app.tsindex 327430d3..23595c94 100644--- a/server/src/app.ts+++ b/server/src/app.ts@@ -659,6 +659,25 @@ export function createApp(intentRouter,requireUser,auditStore,+ /*+ * Which vendors each coworker holds tools for, so the router weighs what a coworker can+ * reach and not only what somebody wrote it was for. Only when there is a plugin store to+ * ask: a deployment with no connectors routes exactly as it did.+ */+ pluginStore+ ? async (agentId) => {+ const granted = await pluginStore.listForAgent(agentId);+ return [+ ...new Set(+ granted.tools.map(+ (tool) =>+ tool.toolName.replace(/^mcp__/, "").split("__")[0] ??+ tool.toolName,+ ),+ ),+ ];+ }+ : undefined,),);}diff --git a/server/src/routing/classify.ts b/server/src/routing/classify.tsindex 84377337..65905c21 100644--- a/server/src/routing/classify.ts+++ b/server/src/routing/classify.ts@@ -18,6 +18,20 @@ export type RoutingCandidate = {name: string;/** What this coworker is for. The one line an operator wrote to say when to reach them. */roleDescription: string;+ /**+ * The systems this coworker can actually reach, by name.+ *+ * Routing on the role description alone routes on what somebody wrote a coworker was for, which is+ * not the same as what it can do. A question about a document in Google Drive went to the coworker+ * whose description says "company knowledge" and which held no Drive grants at all, so it browsed+ * to the vendor, met a sign-in wall and asked the person to sign in to an account they had already+ * connected. The coworker that could have answered was one line further down the roster.+ *+ * Empty for a coworker holding nothing, which is most of them in most deployments. It is a hint+ * rather than a filter: a specialist with no connectors is still the right answer to a question+ * about its specialism.+ */+ reaches?: readonly string[];};export type RoutingDecision = {@@ -37,7 +51,16 @@ export function routingPrompt(candidates: readonly RoutingCandidate[],): string {const roster = candidates- .map((c) => `- id: ${c.id}\n name: ${c.name}\n for: ${c.roleDescription}`)+ .map((c) =>+ [+ `- id: ${c.id}`,+ ` name: ${c.name}`,+ ` for: ${c.roleDescription}`,+ ...(c.reaches && c.reaches.length > 0+ ? [` can reach: ${c.reaches.join(", ")}`]+ : []),+ ].join("\n"),+ ).join("\n");return ["You route a person's message to the one coworker best suited to it.",@@ -46,6 +69,18 @@ export function routingPrompt("",'Reply with only JSON: {"agentId": "<one id from the list>", "reason": "<short, names the fit>", "confidence": <0..1>}.',"Pick the specialist whose purpose matches the message. If none clearly fits, use the most general coworker and give it a low confidence.",+ /*+ * Only when somebody on the roster can actually reach something.+ *+ * A deployment with no connectors would otherwise carry a rule about systems none of its+ * coworkers have, in every routing prompt it ever sends. Same principle as the guidance a Bot+ * gets about its own grants: say nothing about what is not there.+ */+ ...(candidates.some((c) => c.reaches && c.reaches.length > 0)+ ? [+ "When the message names a system a coworker can reach, prefer that coworker: one that cannot reach it has no way to answer and will fall back to a browser that is signed in as nobody. Purpose still comes first — a specialist with no systems listed is right for a question about its specialism.",+ ]+ : []),"",`Message: ${text}`,].join("\n");diff --git a/server/src/routing/routes.ts b/server/src/routing/routes.tsindex c884d4d1..f951ac45 100644--- a/server/src/routing/routes.ts+++ b/server/src/routing/routes.ts@@ -21,6 +21,14 @@ export function createRoutingRoutes(router: IntentRouter,requireUser: MiddlewareHandler<{ Variables: AppVariables }>,auditStore?: AuditStore,+ /**+ * Which systems a coworker can reach, for the router to weigh alongside what it is for.+ *+ * Optional, and absent leaves routing exactly as it was: a deployment with no connectors has+ * nothing to add here, and one that cannot answer the question should not have routing fail over+ * it. Asked per request rather than held, because a grant added a minute ago has to count.+ */+ reachableSystems?: (agentId: string) => Promise<readonly string[]>,) {const routes = new Hono<{ Variables: AppVariables }>();@@ -39,11 +47,25 @@ export function createRoutingRoutes(if (!preferred) {return context.json({ error: "No coworker is available." }, 409);}- const candidates: RoutingCandidate[] = roster.map((a) => ({- id: a.id,- name: a.name,- roleDescription: a.roleDescription,- }));+ const candidates: RoutingCandidate[] = await Promise.all(+ roster.map(async (a) => ({+ id: a.id,+ name: a.name,+ roleDescription: a.roleDescription,+ /*+ * Never allowed to break routing. A connector store that is slow or unhappy must not turn+ * "who is this for" into an error, so a failure here is the same as holding nothing: the+ * router falls back to matching on purpose alone, which is what it did before.+ */+ ...(reachableSystems+ ? {+ reaches: await reachableSystems(a.id).catch(+ () => [] as readonly string[],+ ),+ }+ : {}),+ })),+ );const decision = await router.route(text, candidates, preferred.id);diff --git a/server/tests/routing-classify.test.ts b/server/tests/routing-classify.test.tsindex ef93da24..75a1271f 100644--- a/server/tests/routing-classify.test.ts+++ b/server/tests/routing-classify.test.ts@@ -1,6 +1,7 @@import { describe, expect, test } from "bun:test";import {createIntentRouter,+ routingPrompt,type RoutingCandidate,} from "../src/routing/classify";@@ -100,3 +101,62 @@ describe("routing a message with no @mention", () => {expect(r.fallback).toBe(true);});});++/**+ * The router is told what each coworker can reach, not only what it is for.+ *+ * Routing on the role description alone routes on what somebody wrote a coworker was for, which is+ * not the same as what it can do. A question about a document in Google Drive went to the coworker+ * whose description says "company knowledge" and which held no Drive grants at all. It browsed to+ * the vendor, met a sign-in wall, and asked the person to sign in to an account they had already+ * connected. The coworker that could have answered was one line further down the roster.+ */+describe("routing on what a coworker can reach", () => {+ const withReach: RoutingCandidate[] = [+ {+ id: "knowledge",+ name: "Knowledge",+ roleDescription: "company knowledge questions",+ },+ {+ id: "risk-analyst",+ name: "Risk Analyst",+ roleDescription: "risk and compliance",+ reaches: ["google-drive"],+ },+ ];++ test("names the systems in the roster the model is given", () => {+ const prompt = routingPrompt("what is in my Drive doc?", withReach);+ expect(prompt).toContain("can reach: google-drive");+ });++ test("says nothing about reach for a coworker that holds nothing", () => {+ // Most coworkers in most deployments. An empty line here would be noise in every prompt.+ const prompt = routingPrompt("anything", withReach);+ const knowledgeBlock = prompt.slice(+ prompt.indexOf("id: knowledge"),+ prompt.indexOf("id: risk-analyst"),+ );+ expect(knowledgeBlock).not.toContain("can reach");+ });++ test("tells the model to prefer reach without letting it override purpose", () => {+ /*+ * Both halves matter. Preferring a coworker that can reach the system is the fix; letting that+ * outrank purpose would send every question to whichever coworker happens to hold a connector,+ * which is a different bug with the same shape.+ */+ const prompt = routingPrompt("anything", withReach);+ expect(prompt).toContain("prefer that coworker");+ expect(prompt).toContain("Purpose still comes first");+ });++ test("a roster with no reach at all reads exactly as it did before", () => {+ // A deployment with no connectors must not have its routing prompt changed by this.+ const plain = routingPrompt("anything", [+ { id: "a", name: "A", roleDescription: "alpha" },+ ]);+ expect(plain).not.toContain("can reach");+ });+});diff --git a/server/tests/routing.test.ts b/server/tests/routing.test.tsnew file mode 100644index 00000000..e69de29b