Skip to content
167 linesCodeBlameRaw
1import assert from "node:assert/strict";
2import { test } from "node:test";
3
4import { ensureBuiltin } from "./builtin.ts";
5import { applyChanges } from "./definition.ts";
6import {
7 BUILTIN_NO_MODEL,
8 type Specialist,
9 builtinChanges,
10 builtinDefinition,
11 capMentions,
12 orchestratorInstructions,
13 orchestratorTier,
14 rosterLines,
15} from "./orchestrator.ts";
16import { replyModel } from "./routing.ts";
17import { DEFAULT_ROUTING as POLICY } from "../../runner/src/model-env.ts";
18
19/** A stand-in D1 that records statements, and says whether an insert changed anything. */
20function fakeDb(insertChanges: number) {
21 const ran: { sql: string; args: unknown[] }[] = [];
22 const db = {
23 prepare(sql: string) {
24 return {
25 bind(...args: unknown[]) {
26 return {
27 async run() {
28 ran.push({ sql, args });
29 return { meta: { changes: sql.includes("INSERT OR IGNORE INTO agents") ? insertChanges : 1 } };
30 },
31 };
32 },
33 };
34 },
35 };
36 return { db: db as unknown as D1Database, ran };
37}
38
39test("@g1t is made once per workspace, marked builtin, with its first version", async () => {
40 const memo = new Set<string>();
41 const { db, ran } = fakeDb(1);
42 await ensureBuiltin(db, "wsp_1", memo);
43 assert.equal(ran.length, 2);
44 assert.match(ran[0].sql, /INSERT OR IGNORE INTO agents/);
45 const column = (name: string) => ran[0].args[ran[0].sql.slice(ran[0].sql.indexOf("(") + 1, ran[0].sql.indexOf(")")).split(", ").indexOf(name)];
46 assert.equal(column("workspace_id"), "wsp_1");
47 assert.equal(column("handle"), "g1t");
48 assert.equal(column("template"), "orchestrator");
49 assert.equal(column("builtin"), 1);
50 assert.equal(column("version"), 1);
51 assert.equal(column("created_by"), "g1t");
52 assert.match(ran[1].sql, /INSERT INTO agent_versions/);
53 await ensureBuiltin(db, "wsp_1", memo);
54 assert.equal(ran.length, 2, "an isolate that has seen it made does not write again");
55});
56
57test("a workspace that has @g1t already gets no second one, and no version row", async () => {
58 const { db, ran } = fakeDb(0);
59 await ensureBuiltin(db, "wsp_2", new Set());
60 assert.equal(ran.length, 1);
61});
62
63test("@g1t's handle, name, role and job are fixed; the rest is the workspace's", () => {
64 const current = builtinDefinition();
65 assert.equal(current.handle, "g1t");
66 assert.equal(current.template, "orchestrator");
67 // A profile form sends every field: the fixed ones are ignored, the editable ones apply.
68 const form = builtinChanges(current, {
69 handle: "boss",
70 display_name: "Boss",
71 role: "Does whatever",
72 title: "",
73 team: "qa",
74 department: "x",
75 responsibilities: [],
76 subagents: [],
77 template: "qa",
78 faces: "customers",
79 personality_preset: "terse",
80 budget: { monthly_micros: 5_000_000 },
81 instructions: "Be brief.",
82 });
83 assert.ok(form.ok);
84 if (form.ok) assert.deepEqual(form.value, { personality_preset: "terse", budget: { monthly_micros: 5_000_000 }, instructions: "Be brief." });
85 if (form.ok) {
86 const saved = applyChanges(current, form.value, [], { builtin: true });
87 assert.ok(saved.ok);
88 if (saved.ok) {
89 assert.equal(saved.value.handle, "g1t");
90 assert.equal(saved.value.title, "Orchestrator");
91 assert.equal(saved.value.personality_preset, "terse");
92 assert.equal(saved.value.budget.monthly_micros, 5_000_000);
93 }
94 }
95 // Its instructions are additions, and may be cleared.
96 const cleared = applyChanges({ ...current, instructions: "Be brief." }, { instructions: "" }, [], { builtin: true });
97 assert.ok(cleared.ok);
98 if (cleared.ok) assert.equal(cleared.value.instructions, "");
99 const limited = applyChanges(current, { budget: { monthly_micros: 10_000_000 }, routing: { ceiling: "large" }, capacity: 2 }, [], { builtin: true });
100 assert.ok(limited.ok);
101 assert.equal(applyChanges(current, { instructions: "" }, []).ok, false, "any other agent needs instructions");
102});
103
104const ship: Specialist = { handle: "ship", display_name: "Shipwright", role: "Release manager.", status: "idle", spent_month_micros: 1_200_000, monthly_micros: 20_000_000 };
105const triage: Specialist = { handle: "triage", display_name: "triage", role: "Sorts issues", status: "out_of_budget", spent_month_micros: 5_000_000, monthly_micros: 5_000_000 };
106const scribe: Specialist = { handle: "scribe", display_name: "Scribe", role: "Keeps docs true", status: "working", spent_month_micros: 0, monthly_micros: null };
107
108test("the roster names each specialist, its role, status and spend against its cap", () => {
109 assert.equal(
110 rosterLines([ship, triage, scribe]),
111 [
112 "- @ship (Shipwright): Release manager. idle; $1.20 of $20.00 this month.",
113 "- @triage: Sorts issues. out of budget; $5.00 of $5.00 this month.",
114 "- @scribe: Keeps docs true. working; $0.00, no cap this month.",
115 ].join("\n"),
116 );
117 // A display name that only differs from the handle in case is not repeated.
118 assert.equal(rosterLines([]), "There are no specialists in this workspace yet.");
119});
120
121test("the orchestrator's job: decide, delegate by mention with a brief, at most two, no loops", () => {
122 const job = orchestratorInstructions([ship, triage], "Always copy #releases.");
123 assert.match(job, /@ship \(Shipwright\)/);
124 assert.match(job, /Answer directly/);
125 assert.match(job, /@mention them in this thread with a crisp brief/);
126 assert.match(job, /Want me to set up a release manager/);
127 assert.match(job, /at most 2 specialists in one message/);
128 assert.match(job, /Never delegate in a loop/);
129 assert.match(job, /out of budget or paused/);
130 assert.match(job, /you only speak again if someone mentions you/);
131 assert.ok(job.endsWith("### Added by this workspace\n\nAlways copy #releases."), "the workspace's additions come after the fixed job");
132 assert.match(orchestratorInstructions([triage], ""), /No specialist is available right now/);
133 assert.match(orchestratorInstructions([], ""), /no specialists in this workspace yet/);
134});
135
136test("past two specialists, a reply's mentions wake nobody", () => {
137 const text = "@ship cut it, @triage group these, @scribe write it up, and @ship again. Ask @dana.";
138 assert.equal(capMentions(text, ["ship", "triage", "scribe"]), "@ship cut it, @triage group these, scribe write it up, and @ship again. Ask @dana.");
139 assert.equal(capMentions("mail me@ship.io", ["ship"]), "mail me@ship.io", "an address is not a mention");
140});
141
142test("@g1t routes a delegation call in a long thread to the large tier, held to its limits", () => {
143 assert.equal(orchestratorTier(6, 3), "small");
144 assert.equal(orchestratorTier(7, 3), "large");
145 assert.equal(orchestratorTier(30, 0), "small", "nobody to delegate to: no decision to make");
146 const limits = { floor: null, ceiling: null, pinned: null };
147 assert.equal(replyModel(POLICY, limits, { start: orchestratorTier(7, 1) }).tier, "large");
148 assert.equal(replyModel(POLICY, { ...limits, ceiling: "small" }, { start: "large" }).tier, "small", "its ceiling holds");
149 assert.equal(replyModel(POLICY, { ...limits, floor: "frontier" }, { start: "small" }).tier, "frontier", "and its floor");
150});
151
152test("@g1t's notice without a model points to AI credit and Integrations", () => {
153 assert.match(BUILTIN_NO_MODEL, /AI credit under Billing/);
154 assert.match(BUILTIN_NO_MODEL, /Integrations/);
155});
156
157test("the roster shows title, team and duties, so 'QA should look' reaches Margo", () => {
158 const margo: Specialist = { handle: "margo", display_name: "Margo", role: "QA", title: "QA Engineer", team: "qa", responsibilities: ["Review pull requests.", "Chase flaky checks"], status: "idle", spent_month_micros: 0, monthly_micros: 10_000_000 };
159 const david: Specialist = { handle: "david", display_name: "David", role: "Sales Operations, Sales", title: "Sales Operations", department: "Sales", status: "working", spent_month_micros: 0, monthly_micros: null };
160 assert.equal(
161 rosterLines([margo, david]),
162 [
163 "- @margo: QA Engineer on the qa team. Does: Review pull requests; Chase flaky checks. idle; $0.00 of $10.00 this month.",
164 "- @david: Sales Operations, Sales. working; $0.00, no cap this month.",
165 ].join("\n"),
166 );
167});