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