| 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; |
| 3 | |
| 4 | import { ensureBuiltin } from "./builtin.ts"; |
| 5 | import { applyChanges } from "./definition.ts"; |
| 6 | import { |
| 7 | BUILTIN_NO_MODEL, |
| 8 | type Specialist, |
| 9 | builtinChanges, |
| 10 | builtinDefinition, |
| 11 | orchestratorInstructions, |
| 12 | orchestratorTier, |
| 13 | rosterLines, |
| 14 | } from "./orchestrator.ts"; |
| 15 | import { replyModel } from "./routing.ts"; |
| 16 | import { 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. */ |
| 19 | function 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 | |
| 38 | test("@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 | |
| 56 | test("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 | |
| 62 | test("@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 | team: "qa", |
| 73 | department: "x", |
| 74 | responsibilities: [], |
| 75 | subagents: [], |
| 76 | template: "qa", |
| 77 | faces: "customers", |
| 78 | personality_preset: "terse", |
| 79 | budget: { monthly_micros: 5_000_000 }, |
| 80 | instructions: "Be brief.", |
| 81 | }); |
| 82 | assert.ok(form.ok); |
| 83 | if (form.ok) assert.deepEqual(form.value, { personality_preset: "terse", budget: { monthly_micros: 5_000_000 }, instructions: "Be brief." }); |
| 84 | if (form.ok) { |
| 85 | const saved = applyChanges(current, form.value, [], { builtin: true }); |
| 86 | assert.ok(saved.ok); |
| 87 | if (saved.ok) { |
| 88 | assert.equal(saved.value.handle, "g1t"); |
| 89 | assert.equal(saved.value.title, "Orchestrator"); |
| 90 | assert.equal(saved.value.personality_preset, "terse"); |
| 91 | assert.equal(saved.value.budget.monthly_micros, 5_000_000); |
| 92 | } |
| 93 | } |
| 94 | // Its instructions are additions, and may be cleared. |
| 95 | const cleared = applyChanges({ ...current, instructions: "Be brief." }, { instructions: "" }, [], { builtin: true }); |
| 96 | assert.ok(cleared.ok); |
| 97 | if (cleared.ok) assert.equal(cleared.value.instructions, ""); |
| 98 | const limited = applyChanges(current, { budget: { monthly_micros: 10_000_000 }, routing: { ceiling: "large" }, capacity: 2 }, [], { builtin: true }); |
| 99 | assert.ok(limited.ok); |
| 100 | assert.equal(applyChanges(current, { instructions: "" }, []).ok, false, "any other agent needs instructions"); |
| 101 | }); |
| 102 | |
| 103 | const ship: Specialist = { handle: "ship", display_name: "Shipwright", role: "Release manager.", status: "idle", spent_month_micros: 1_200_000, monthly_micros: 20_000_000 }; |
| 104 | const 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 }; |
| 105 | const scribe: Specialist = { handle: "scribe", display_name: "Scribe", role: "Keeps docs true", status: "working", spent_month_micros: 0, monthly_micros: null }; |
| 106 | |
| 107 | test("the roster names each specialist, its role, status and spend against its cap", () => { |
| 108 | assert.equal( |
| 109 | rosterLines([ship, triage, scribe]), |
| 110 | [ |
| 111 | "- @ship (Shipwright): Release manager. idle; $1.20 of $20.00 this month.", |
| 112 | "- @triage: Sorts issues. out of budget; $5.00 of $5.00 this month.", |
| 113 | "- @scribe: Keeps docs true. working; $0.00, no cap this month.", |
| 114 | ].join("\n"), |
| 115 | ); |
| 116 | // A display name that only differs from the handle in case is not repeated. |
| 117 | assert.equal(rosterLines([]), "There are no specialists in this workspace yet."); |
| 118 | }); |
| 119 | |
| 120 | test("the orchestrator's job: decide, delegate with hand_off and a brief, at most two, no loops", () => { |
| 121 | const job = orchestratorInstructions([ship, triage], "Always copy #releases."); |
| 122 | assert.match(job, /@ship \(Shipwright\)/); |
| 123 | assert.match(job, /Answer directly/); |
| 124 | assert.match(job, /call hand_off with their handle and a crisp brief/); |
| 125 | assert.match(job, /a group message opens with the person who asked, you and them/); |
| 126 | assert.match(job, /Only hand_off delegates\. An @mention in your message wakes no agent/); |
| 127 | assert.match(job, /name specialists without @/); |
| 128 | assert.match(job, /Want me to set up a release manager/); |
| 129 | assert.match(job, /at most 2 specialists for one message/); |
| 130 | assert.match(job, /Never delegate in a loop/); |
| 131 | assert.match(job, /out of budget or paused/); |
| 132 | assert.doesNotMatch(job, /@mention them in this thread/, "a mention is no longer how work is handed over"); |
| 133 | assert.ok(job.endsWith("### Added by this workspace\n\nAlways copy #releases."), "the workspace's additions come after the fixed job"); |
| 134 | assert.match(orchestratorInstructions([triage], ""), /No specialist is available right now/); |
| 135 | assert.match(orchestratorInstructions([], ""), /no specialists in this workspace yet/); |
| 136 | }); |
| 137 | |
| 138 | test("@g1t routes a delegation call in a long thread to the large tier, held to its limits", () => { |
| 139 | assert.equal(orchestratorTier(6, 3), "small"); |
| 140 | assert.equal(orchestratorTier(7, 3), "large"); |
| 141 | assert.equal(orchestratorTier(30, 0), "small", "nobody to delegate to: no decision to make"); |
| 142 | const limits = { floor: null, ceiling: null, pinned: null }; |
| 143 | assert.equal(replyModel(POLICY, limits, { start: orchestratorTier(7, 1) }).tier, "large"); |
| 144 | assert.equal(replyModel(POLICY, { ...limits, ceiling: "small" }, { start: "large" }).tier, "small", "its ceiling holds"); |
| 145 | assert.equal(replyModel(POLICY, { ...limits, floor: "frontier" }, { start: "small" }).tier, "frontier", "and its floor"); |
| 146 | }); |
| 147 | |
| 148 | test("@g1t's notice without a model points to AI credit and Integrations", () => { |
| 149 | assert.match(BUILTIN_NO_MODEL, /AI credit under Billing/); |
| 150 | assert.match(BUILTIN_NO_MODEL, /Integrations/); |
| 151 | }); |
| 152 | |
| 153 | test("the roster shows title, team and duties, so 'QA should look' reaches Margo", () => { |
| 154 | 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 }; |
| 155 | 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 }; |
| 156 | assert.equal( |
| 157 | rosterLines([margo, david]), |
| 158 | [ |
| 159 | "- @margo: QA Engineer on the qa team. Does: Review pull requests; Chase flaky checks. idle; $0.00 of $10.00 this month.", |
| 160 | "- @david: Sales Operations, Sales. working; $0.00, no cap this month.", |
| 161 | ].join("\n"), |
| 162 | ); |
| 163 | }); |