From fe6c22e0f2443ca63e8b9aa8486b7050a8327a95 Mon Sep 17 00:00:00 2001 From: Jason Woltje Date: Mon, 16 Nov 2026 12:56:32 -0600 Subject: [PATCH] fix(webui): triage-host review, the owner reaches the routes on an organizations Console, a second start answers, an unanswered or undelivered message gets a host line and leaves the queue (#1873, lane triage-host) P1-1: the organizations branch lets the instance owner's own session through to the two Triage paths; everyone else still gets this Console's unknown /api/ answer. P1-2: each start clears turns/, so the runner's numbered turn files do not collide on restart. P2-1: on exit, a message the session took and never marked read leaves the queue with a host line. P2-2: member, signed-out, organizations-Console and org-selected route tests. P3-1: bus-start cards carry --dev-local-owner on a dev local owner Console. P3-2: a failed start writes a host line; the queue index is guarded. P3-3: one shared refresh, a unique temp name per write, four organizations read at a time. P3-4: the restart path waits for stop() inside its two-second exit. P3-5: ANTHROPIC_API_KEY no longer passes from the route. --- packages/webui/src/cli.mjs | 8 ++- packages/webui/src/serve.mjs | 5 +- packages/webui/src/triage-cards.mjs | 11 +-- packages/webui/src/triage-host.mjs | 35 +++++++--- packages/webui/src/triage-snapshot.mjs | 18 ++++- packages/webui/tests/triage-cards.test.mjs | 8 +++ packages/webui/tests/triage-host.test.mjs | 67 ++++++++++++++++++- packages/webui/tests/triage-http.test.mjs | 33 +++++++++ packages/webui/tests/triage-snapshot.test.mjs | 13 +++- 9 files changed, 173 insertions(+), 25 deletions(-) diff --git a/packages/webui/src/cli.mjs b/packages/webui/src/cli.mjs index 2c59adf98..3986214b1 100644 --- a/packages/webui/src/cli.mjs +++ b/packages/webui/src/cli.mjs @@ -233,7 +233,7 @@ try { // Nothing starts here; the owner's first message starts it. No model route store exists without a // schema change, so none is wired: the read says no route is set and nothing launches. const triage = system && (devOwner || ssoOn) ? createTriageHost({ - dataRoot: system.dataRoot, route: null, log: line => console.log(line), + dataRoot: system.dataRoot, route: null, devOwner: Boolean(devOwner), log: line => console.log(line), readSnapshot: () => composeSnapshot({ orgs: () => listOrgs({ configDir: configDir(), dataRoot: system.dataRoot, instance }), busStatus: id => orgBusStatus({ configDir: configDir(), dataRoot: system.dataRoot, instance }, id), @@ -251,9 +251,11 @@ try { // F3: the restart route closes every listener and connection, then exits RESTART_EXIT for the // supervisor to relaunch. Without --supervised the route answers not-supervised. const restart = supervised ? () => { - sso?.close(); testerServer?.close(); triage?.stop(); + sso?.close(); testerServer?.close(); + // The Triage session gets SIGTERM now and SIGKILL before the 2 s exit below. + const triageStopped = triage ? triage.stop({ graceMs: 1500 }) : Promise.resolve(); setTimeout(() => process.exit(RESTART_EXIT), 2000).unref(); - server.close(() => process.exit(RESTART_EXIT)); + server.close(() => triageStopped.finally(() => process.exit(RESTART_EXIT))); server.closeAllConnections(); } : null; const trail = system ? actionTrail(join(system.dataRoot, 'console')) : null; diff --git a/packages/webui/src/serve.mjs b/packages/webui/src/serve.mjs index 611e34c8f..aadf23ba3 100644 --- a/packages/webui/src/serve.mjs +++ b/packages/webui/src/serve.mjs @@ -936,7 +936,10 @@ export async function startServer({ host = '127.0.0.1', port = 7330, board = DEF if (match) { orgId = match[1]; path = `/api/${match[2]}`; - } else if (!orgList && path !== '/api/config' && !hook) { + } else if (!orgList && path !== '/api/config' && !hook + // #1873 triage-host: the Triage routes pass only for the instance owner's own session on the + // public name; every other caller gets this Console's unknown /api/ answer. The gate below checks again. + && !(triage && TRIAGE_PATHS.includes(path) && !tester && named && sso !== null && sso.session(req)?.rootOwner === true)) { return json(res, 404, { error: 'org-unavailable' }); } } diff --git a/packages/webui/src/triage-cards.mjs b/packages/webui/src/triage-cards.mjs index 03c29bc91..1233cf4af 100644 --- a/packages/webui/src/triage-cards.mjs +++ b/packages/webui/src/triage-cards.mjs @@ -8,7 +8,8 @@ import { ID_PATTERN } from "../../organization/src/vocabulary.mjs"; export const CARD_STEPS = Object.freeze({ - "bus-start": (org) => `mosaic bus start ${org} --detach`, + // On a --dev-local-owner Console the bus starts in that mode, as the Organization page's own step says. + "bus-start": (org, { devOwner = false } = {}) => `mosaic bus start ${org}${devOwner === true ? " --dev-local-owner" : ""} --detach`, "bus-restart": (org) => `mosaic bus restart ${org}`, "bus-recover": (org) => `mosaic bus recover ${org}`, "bus-stop": (org) => `mosaic bus stop ${org}`, @@ -26,18 +27,18 @@ export function plainText(value, max = MAX_TEXT) { } // One card, or null. orgIds is the set of registry ids the snapshot read. -export function validateCard(card, orgIds) { +export function validateCard(card, orgIds, opts = {}) { if (!card || typeof card !== "object" || Array.isArray(card) || Object.getPrototypeOf(card) !== Object.prototype) return null; const keys = Object.keys(card); if (keys.length !== 2 || !keys.includes("step") || !keys.includes("org")) return null; const { step, org } = card; if (typeof step !== "string" || !Object.hasOwn(CARD_STEPS, step)) return null; if (typeof org !== "string" || !ID_PATTERN.test(org) || !orgIds?.has?.(org)) return null; - return { step, org, command: CARD_STEPS[step](org) }; + return { step, org, command: CARD_STEPS[step](org, opts) }; } // The reply split into its plain text and the cards the server keeps. -export function extractCards(reply, orgIds) { +export function extractCards(reply, orgIds, opts = {}) { const source = typeof reply === "string" ? reply : ""; const cards = []; let dropped = 0; @@ -47,7 +48,7 @@ export function extractCards(reply, orgIds) { try { parsed = JSON.parse(body); } catch { dropped += 1; return ""; } const list = Array.isArray(parsed) ? parsed : [parsed]; for (const item of list) { - const card = validateCard(item, orgIds); + const card = validateCard(item, orgIds, opts); const key = card && `${card.step} ${card.org}`; if (!card || seen.has(key) || cards.length >= MAX_CARDS) { dropped += 1; continue; } seen.add(key); diff --git a/packages/webui/src/triage-host.mjs b/packages/webui/src/triage-host.mjs index 2f3edfff0..a6da8231c 100644 --- a/packages/webui/src/triage-host.mjs +++ b/packages/webui/src/triage-host.mjs @@ -40,6 +40,8 @@ export const RELAY_REFUSED = "triage-refused"; export const MESSAGE_MAX = 4000; export const TRANSCRIPT_KEEP = 200; export const QUEUE_MAX = 20; +export const UNANSWERED = "Triage stopped before it answered this message. Send it again."; +export const NOT_STARTED = "Triage did not start, so this message was not delivered."; // An over-cap answer is held for at most this long; a later time is not taken. export const CAP_HOLD_MAX_MS = 48 * 3600 * 1000; const ISO_TIME = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d{1,3})?Z$/; @@ -49,7 +51,7 @@ const CAP = /^[0-9a-f]{64}$/; // The spawn's environment: these names from the Console's own environment, PATH fixed, and the // route's proxy variables. bwrap sets HOME and TMPDIR inside. export const SPAWN_ENV_ALLOW = Object.freeze(["PATH", "LANG", "LC_ALL", "TZ", "TERM"]); -export const ROUTE_ENV_ALLOW = Object.freeze(["ANTHROPIC_BASE_URL", "ANTHROPIC_AUTH_TOKEN", "ANTHROPIC_API_KEY"]); +export const ROUTE_ENV_ALLOW = Object.freeze(["ANTHROPIC_BASE_URL", "ANTHROPIC_AUTH_TOKEN"]); const ENV_NAME = /^[A-Z][A-Z0-9_]{0,63}$/; const deniedHarnessName = (k) => !ENV_NAME.test(k) || k === "MOSAIC_CONFIG" || k.startsWith("MOSAIC_BUS_") || FOUNDER_ENV.includes(k) || k === "SSH_AUTH_SOCK"; @@ -174,12 +176,12 @@ export function createRelay({ socket, cap, take, read, onSend, log = () => {} }) }); } -// createTriageHost({ dataRoot, route, readSnapshot, env, node, bwrap, now, log }) +// createTriageHost({ dataRoot, route, readSnapshot, env, node, bwrap, now, devOwner, log }) // route { admit() -> { ok: true, adapter, harness, harnessEnv, harnessMounts, turnEnd, model, // provider, env } | { ok: false, code, until? } } or null for no route // readSnapshot async () -> the snapshot (triage-snapshot.mjs composeSnapshot), written to health/ // pollInterval the runner's receive interval in ms, for tests; the runner's default otherwise -export function createTriageHost({ dataRoot, route = null, readSnapshot = async () => null, env = process.env, node = process.execPath, bwrap = BWRAP, now = () => new Date(), log = () => {}, roleDir = join(REPO, "roles"), readOnly = [], pollInterval = null }) { +export function createTriageHost({ dataRoot, route = null, readSnapshot = async () => null, env = process.env, node = process.execPath, bwrap = BWRAP, now = () => new Date(), devOwner = false, log = () => {}, roleDir = join(REPO, "roles"), readOnly = [], pollInterval = null }) { if (typeof dataRoot !== "string" || !dataRoot.startsWith("/")) throw new TriageError("triage-no-data-root", "no data root"); const paths = triagePaths(dataRoot); const role = JSON.parse(readFileSync(join(roleDir, "triage.json"), "utf8")); @@ -223,7 +225,9 @@ export function createTriageHost({ dataRoot, route = null, readSnapshot = async const orgIds = () => new Set(Array.isArray(last?.organizations?.list) ? last.organizations.list.map((o) => o.id) : []); - const refresh = async () => { + // One refresh at a time: a read and a message that arrive together share it. + let refreshing = null; + const refresh = () => refreshing ??= (async () => { ensureDirs(); let snap = null; try { snap = await readSnapshot(); } catch { snap = null; } @@ -233,13 +237,15 @@ export function createTriageHost({ dataRoot, route = null, readSnapshot = async lastAt = now().getTime(); } return last; - }; + })().finally(() => { refreshing = null; }); const writeBundle = (admitted) => { const a = paths.agent; for (const d of ["bundle", "tmp", "work", "home"]) freshDir(join(a, d)); mkdirSync(join(a, "work", "health"), { mode: 0o700 }); - for (const f of ["session.json", "relay.sock", "tools.sock", "turn.done"]) rmSync(join(a, f), { recursive: true, force: true }); + // turns/ goes too: the runner numbers turns from 1 and opens each turn file exclusively, so a + // turns/ left by the last run stops the next one at its first turn. + for (const f of ["session.json", "relay.sock", "tools.sock", "turn.done", "turns"]) rmSync(join(a, f), { recursive: true, force: true }); // The session's history stays across starts, unless the session put something other than a // real directory there. try { realDir(join(a, "session")); } catch { freshDir(join(a, "session")); } @@ -288,7 +294,7 @@ export function createTriageHost({ dataRoot, route = null, readSnapshot = async if (i >= 0) queue.splice(i, 1); }, onSend: ({ body, in_reply_to }) => { - const { text, cards, dropped } = extractCards(body, orgIds()); + const { text, cards, dropped } = extractCards(body, orgIds(), { devOwner: devOwner === true }); append({ at: now().toISOString(), from: "triage", text, cards, dropped, ...(in_reply_to ? { in_reply_to } : {}) }); }, log, @@ -309,6 +315,13 @@ export function createTriageHost({ dataRoot, route = null, readSnapshot = async if (child !== mine) return; lastExit = { code: Number.isInteger(code) ? code : null, signal: signal ?? null, at: now().toISOString() }; child = null; + // A message the session took and never marked read was not answered. It leaves the queue, so + // it holds no slot, and the transcript says so; a message it never took waits for the next start. + for (let i = queue.length - 1; i >= 0; i--) { + if (!queue[i].delivered) continue; + const [m] = queue.splice(i, 1); + try { append({ at: now().toISOString(), from: "host", in_reply_to: m.id, text: UNANSWERED }); } catch { log("triage host: transcript write failed"); } + } relay?.close(); relay = null; rmSync(join(paths.agent, "relay.sock"), { force: true }); @@ -372,7 +385,9 @@ export function createTriageHost({ dataRoot, route = null, readSnapshot = async starting ??= start(admitted).finally(() => { starting = null; }); try { await starting; } catch (e) { - queue.splice(queue.findIndex((m) => m.id === id), 1); + const i = queue.findIndex((m) => m.id === id); + if (i >= 0) queue.splice(i, 1); + try { append({ at: now().toISOString(), from: "host", in_reply_to: id, text: NOT_STARTED }); } catch {} relay?.close(); relay = null; log(`triage host: start failed: ${e.code ?? "error"}`); @@ -381,12 +396,12 @@ export function createTriageHost({ dataRoot, route = null, readSnapshot = async } return { id, at }; }, - async stop() { + async stop({ graceMs = 5000 } = {}) { const c = child; if (c) { try { process.kill(-c.pid, "SIGTERM"); } catch {} await new Promise((resolve) => { - const t = setTimeout(() => { try { process.kill(-c.pid, "SIGKILL"); } catch {} resolve(); }, 5000); + const t = setTimeout(() => { try { process.kill(-c.pid, "SIGKILL"); } catch {} resolve(); }, graceMs); c.once("exit", () => { clearTimeout(t); resolve(); }); }); } diff --git a/packages/webui/src/triage-snapshot.mjs b/packages/webui/src/triage-snapshot.mjs index b44ccc099..585db192a 100644 --- a/packages/webui/src/triage-snapshot.mjs +++ b/packages/webui/src/triage-snapshot.mjs @@ -21,6 +21,7 @@ // names the config path and data-root facts, so only its first line's leading word is matched // against SYSTEM_CONFIG_CODES; the text itself never leaves this module. +import { randomBytes } from "node:crypto"; import { spawn } from "node:child_process"; import { chmodSync, renameSync, rmSync, writeFileSync } from "node:fs"; import { join, resolve, dirname } from "node:path"; @@ -38,6 +39,8 @@ export const BUS_STATES = Object.freeze(["running", "not-answering", "stopped", const UNIT_ACTIVE = Object.freeze(["active", "inactive", "failed", "activating", "deactivating", "reloading", "unknown"]); export const READ_TIMEOUT_MS = 5000; export const MAX_ORGS = 200; +// At most this many organizations are read at once; each read can start a node child for its config check. +export const ORG_READS_AT_ONCE = 4; // What a failed system config check may say. Each refusal code the validator prints as its first // word passes as itself; a known sentence maps to a fixed code; anything else is the exit's general @@ -177,6 +180,15 @@ function configFields(c, codes) { // readers: { orgs() -> [{ id, state }], busStatus(id) -> status (a CliError, or a throw with // `dead: true`, is a dead host), processing(id), orgConfig(id), proxy(), systemConfig() }. Each may be missing. +// fn over items, at most `limit` running at once; the results keep the items' order. +async function mapLimit(items, limit, fn) { + const out = new Array(items.length); + let next = 0; + const worker = async () => { while (next < items.length) { const i = next++; out[i] = await fn(items[i]); } }; + await Promise.all(Array.from({ length: Math.min(limit, items.length) }, worker)); + return out; +} + export async function composeSnapshot(readers = {}, { now = () => new Date(), timeoutMs = READ_TIMEOUT_MS } = {}) { const at = now().toISOString(); const read = (name, ...args) => (typeof readers[name] === "function" ? bounded(() => readers[name](...args), timeoutMs) : Promise.resolve(NOT_READ)); @@ -184,7 +196,7 @@ export async function composeSnapshot(readers = {}, { now = () => new Date(), ti let organizations = NOT_READ; if (Array.isArray(orgsRaw)) { const active = orgsRaw.filter((o) => o && typeof o === "object" && o.state === "active" && typeof o.id === "string" && ID_PATTERN.test(o.id)).slice(0, MAX_ORGS); - const list = await Promise.all(active.map(async ({ id }) => { + const list = await mapLimit(active, ORG_READS_AT_ONCE, async ({ id }) => { const [bus, processing, config] = await Promise.all([ typeof readers.busStatus === "function" ? bounded(async () => { @@ -196,7 +208,7 @@ export async function composeSnapshot(readers = {}, { now = () => new Date(), ti read("orgConfig", id), ]); return { id, bus: bus === NOT_READ ? NOT_READ : bus, processing: processingFields(processing), config: configFields(config, ORG_CONFIG_CODES) }; - })); + }); organizations = { count: list.length, list }; } return { @@ -212,7 +224,7 @@ export async function composeSnapshot(readers = {}, { now = () => new Date(), ti // One file, replaced whole: written beside itself at 0600, then renamed over the last one. export function writeSnapshot(healthDir, snapshot) { const file = join(healthDir, SNAPSHOT_FILE); - const temp = join(healthDir, `.${SNAPSHOT_FILE}.${process.pid}.tmp`); + const temp = join(healthDir, `.${SNAPSHOT_FILE}.${process.pid}.${randomBytes(6).toString("hex")}.tmp`); try { writeFileSync(temp, JSON.stringify(snapshot, null, 2) + "\n", { mode: 0o600 }); chmodSync(temp, 0o600); diff --git a/packages/webui/tests/triage-cards.test.mjs b/packages/webui/tests/triage-cards.test.mjs index e7e83d8ba..5209c795f 100644 --- a/packages/webui/tests/triage-cards.test.mjs +++ b/packages/webui/tests/triage-cards.test.mjs @@ -16,6 +16,14 @@ test("each known step renders its fixed command for a registered org", () => { assert.equal(validateCard({ step: "org-config-validate", org: "alpha" }, orgs).command, "mosaic org config validate alpha"); }); +test("a dev local owner Console renders bus-start with its flag; every other step and Console stays as it is", () => { + const orgs = new Set(["alpha"]); + const reply = '```cards\n[{"step": "bus-start", "org": "alpha"}, {"step": "bus-stop", "org": "alpha"}]\n```'; + assert.deepEqual(extractCards(reply, orgs, { devOwner: true }).cards.map((c) => c.command), ["mosaic bus start alpha --dev-local-owner --detach", CARD_STEPS["bus-stop"]("alpha")]); + assert.deepEqual(extractCards(reply, orgs).cards.map((c) => c.command), ["mosaic bus start alpha --detach", CARD_STEPS["bus-stop"]("alpha")]); + assert.equal(validateCard({ step: "bus-start", org: "alpha" }, orgs, { devOwner: "yes" }).command, "mosaic bus start alpha --detach", "only true turns it on"); +}); + test("a card with an unknown step, an unknown org or an extra field is dropped", () => { assert.equal(validateCard({ step: "bus-delete", org: "alpha" }, orgs), null); assert.equal(validateCard({ step: "toString", org: "alpha" }, orgs), null); diff --git a/packages/webui/tests/triage-host.test.mjs b/packages/webui/tests/triage-host.test.mjs index f938faa14..570913d66 100644 --- a/packages/webui/tests/triage-host.test.mjs +++ b/packages/webui/tests/triage-host.test.mjs @@ -10,7 +10,7 @@ import { join } from "node:path"; import { Client } from "../../bus/src/client.mjs"; import { sandboxProblem } from "../../seat/src/sandbox.mjs"; import { - RELAY_REFUSED, ROUTE_ENV_ALLOW, SPAWN_ENV_ALLOW, createRelay, createTriageHost, hostPrompt, triageEnv, + NOT_STARTED, RELAY_REFUSED, ROUTE_ENV_ALLOW, SPAWN_ENV_ALLOW, UNANSWERED, createRelay, createTriageHost, hostPrompt, triageEnv, } from "../src/triage-host.mjs"; const FIXTURE = new URL("./fixtures/triage-adapter.mjs", import.meta.url).pathname; @@ -43,7 +43,8 @@ test("triageEnv keeps the fixed names and the route's proxy variables, and nothi assert.deepEqual(Object.keys(env).sort(), ["ANTHROPIC_AUTH_TOKEN", "ANTHROPIC_BASE_URL", "LANG", "PATH", "TZ"]); assert.equal(env.PATH, "/opt/node/bin:/usr/bin:/bin"); assert.deepEqual(SPAWN_ENV_ALLOW, ["PATH", "LANG", "LC_ALL", "TZ", "TERM"]); - assert.deepEqual(ROUTE_ENV_ALLOW, ["ANTHROPIC_BASE_URL", "ANTHROPIC_AUTH_TOKEN", "ANTHROPIC_API_KEY"]); + assert.deepEqual(ROUTE_ENV_ALLOW, ["ANTHROPIC_BASE_URL", "ANTHROPIC_AUTH_TOKEN"]); + assert.equal(triageEnv(CONSOLE_ENV, { ANTHROPIC_API_KEY: "from-the-route" }).ANTHROPIC_API_KEY, undefined, "the session gets no key, only the proxy's token"); assert.equal(triageEnv(CONSOLE_ENV).ANTHROPIC_API_KEY, undefined, "a key in the Console's environment is not the route's"); }); @@ -150,9 +151,25 @@ test("a route whose answer carries a config, bus or founder variable is refused await assert.rejects(h.message("hello"), (e) => e.code === "triage-route-invalid", name); assert.equal(h.running, false); assert.equal(existsSync(join(dataRoot, "triage", "agent", "session.json")), false); + // The owner's line is written before the start; the transcript then says it was not delivered. + const [owner, note, ...rest] = (await h.state()).transcript; + assert.deepEqual([owner.from, owner.text, note.from, note.in_reply_to, note.text, rest.length], ["owner", "hello", "host", owner.id, NOT_STARTED, 0], name); } }); +test("a read and a message that arrive together share one snapshot read", async (t) => { + let reads = 0, release; + const gate = new Promise((r) => { release = r; }); + const { h } = host(t, scratch(), { readSnapshot: async () => { reads++; await gate; return SNAPSHOT; } }); + const both = Promise.all([h.refresh(), h.refresh()]); + release(); + const [a, b] = await both; + assert.equal(reads, 1); + assert.equal(a, b); + await h.refresh(); + assert.equal(reads, 2, "a later refresh reads again"); +}); + test("a Triage directory that is a link is refused", async (t) => { const root = scratch(); const dataRoot = join(root, "data"); @@ -254,3 +271,49 @@ test("end to end under the sandbox: the first message starts the session, which assert.equal(h.running, false); assert.equal(existsSync(join(dataRoot, "triage", "agent", "relay.sock")), false); }); + +// A route answer that runs the stub adapter under the sandbox. +function stubRoute(root) { + const adapter = join(root, "adapter.sh"); + writeFileSync(adapter, `exec '${process.execPath}' '${FIXTURE}'\n`, { mode: 0o644 }); + return { admit: async () => ({ ok: true, adapter, harness: "fake", harnessEnv: {}, harnessMounts: [], turnEnd: "exit", model: "stub", provider: "stub", env: {} }) }; +} +const repliesOf = (h, n, from = "triage") => waitFor(async () => { + const list = (await h.state()).transcript.filter((e) => e.from === from); + return list.length >= n ? list : null; +}); + +test("a second start on the same data root answers too: the last run's turns do not stop the next", { skip, timeout: 120000 }, async (t) => { + const root = scratch(); + const route = stubRoute(root); + const first = host(t, root, { route }).h; + const one = await first.message("one"); + assert.deepEqual((await repliesOf(first, 1)).map((e) => [e.text.replace(/\n\n.*$/s, ""), e.in_reply_to]), [["echo: one", one.id]]); + await first.stop(); + assert.equal(first.running, false); + const { h: second, dataRoot } = host(t, root, { route }); + const two = await second.message("two"); + const list = await repliesOf(second, 2); + assert.deepEqual(list.map((e) => [e.text.replace(/\n\n.*$/s, ""), e.in_reply_to]), [["echo: one", one.id], ["echo: two", two.id]]); + assert.equal((await second.state()).transcript.filter((e) => e.from === "host").length, 0); + assert.equal(existsSync(join(dataRoot, "triage", "agent", "turns")), true); +}); + +test("a session that ends mid-turn: the message it took is answered by the host, leaves the queue, and the next message is answered", { skip, timeout: 120000 }, async (t) => { + const root = scratch(); + const { h, dataRoot } = host(t, root, { route: stubRoute(root) }); + const slept = await h.message("SLEEP"); + // The turn has started once the runner made its turns directory. + await waitFor(() => { try { return readdirSync(join(dataRoot, "triage", "agent", "turns")).length > 0; } catch { return false; } }); + const pid = h.pid; + assert.ok(Number.isInteger(pid)); + process.kill(-pid, "SIGKILL"); + const [note] = await repliesOf(h, 1, "host"); + assert.deepEqual([note.in_reply_to, note.text], [slept.id, UNANSWERED]); + await waitFor(() => !h.running); + assert.equal((await h.state()).lastExit.signal, "SIGKILL"); + const next = await h.message("after"); + const [reply] = await repliesOf(h, 1); + assert.deepEqual([reply.text.replace(/\n\n.*$/s, ""), reply.in_reply_to], ["echo: after", next.id]); + assert.equal((await h.state()).transcript.filter((e) => e.from === "host").length, 1, "the killed message is answered once and not taken again"); +}); diff --git a/packages/webui/tests/triage-http.test.mjs b/packages/webui/tests/triage-http.test.mjs index afe9e9302..fff0bb4c8 100644 --- a/packages/webui/tests/triage-http.test.mjs +++ b/packages/webui/tests/triage-http.test.mjs @@ -138,6 +138,39 @@ test('a signed-out caller and a granted user get the unknown-route answer on bot assert.equal(triage.calls.length, before, 'the host saw nothing from them'); }); +test('on an organizations Console the instance owner reaches both routes; a member, a signed-out caller and an org-selected path get the unknown answer', async t => { + const triage = fakeTriage(); + const organizations = { list: async () => [], resolve: async id => (id === 'x' ? { busHealthy: true } : null) }; + const owner = await devServer(t, { triage, organizations }); + const read = await send(owner.port, '/api/triage', { headers: { host: owner.headers.host } }); + assert.equal(read.status, 200, 'the owner reads'); + assert.equal(read.json.route, 'set'); + assert.equal((await send(owner.port, '/api/triage/message', { method: 'POST', headers: owner.headers, body: { text: 'hi' } })).status, 202, 'and writes'); + assert.equal((await send(owner.port, '/api/config', { headers: { host: owner.headers.host } })).json.triage, true); + const seen = triage.calls.length; + // With an organization selected the paths are that organization's, and Triage is in none. + for (const [path, method] of EVERY) { + const orgPath = path.replace('/api/', '/api/orgs/x/'); + await sameAsUnknown(owner.port, orgPath, method, owner.headers, method === 'POST' ? { text: 'hi' } : undefined, 'org selected'); + } + const member = await devServer(t, { triage, organizations, sso: { ...createDevOwner(), session: () => ({ name: 'Bob', rootOwner: false, instanceRole: 'member' }) } }); + const signedOut = await devServer(t, { triage, organizations, sso: { ...createDevOwner(), session: () => null } }); + for (const [label, f] of [['member', member], ['signed out', signedOut]]) { + for (const [path, method] of EVERY) await sameAsUnknown(f.port, path, method, f.headers, method === 'POST' ? { text: 'hi' } : undefined, label); + } + assert.equal(Object.hasOwn((await send(member.port, '/api/config', { headers: { host: member.headers.host } })).json, 'triage'), false); + assert.equal(triage.calls.length, seen, 'the host saw nothing from them'); +}); + +test('without organizations a member session gets the unknown answer and no drawer in the config', async t => { + const triage = fakeTriage(); + const member = await devServer(t, { triage, sso: { ...createDevOwner(), session: () => ({ name: 'Bob', rootOwner: false, instanceRole: 'member' }) } }); + for (const [path, method] of EVERY) await sameAsUnknown(member.port, path, method, member.headers, method === 'POST' ? { text: 'hi' } : undefined, 'member'); + assert.equal((await send(member.port, '/api/triage', { headers: { host: member.headers.host } })).status, 404); + assert.equal(Object.hasOwn((await send(member.port, '/api/config', { headers: { host: member.headers.host } })).json, 'triage'), false); + assert.deepEqual(triage.calls, []); +}); + test('without a host, on a loopback Host under --sso, and on the tester listener the routes are unknown', async t => { const none = await devServer(t); for (const [path, method] of EVERY) await sameAsUnknown(none.port, path, method, none.headers, method === 'POST' ? { text: 'hi' } : undefined, 'no host'); diff --git a/packages/webui/tests/triage-snapshot.test.mjs b/packages/webui/tests/triage-snapshot.test.mjs index 4700cc827..fe3b49b6b 100644 --- a/packages/webui/tests/triage-snapshot.test.mjs +++ b/packages/webui/tests/triage-snapshot.test.mjs @@ -5,7 +5,7 @@ import assert from "node:assert/strict"; import { mkdtempSync, mkdirSync, writeFileSync, readFileSync, statSync, readdirSync } from "node:fs"; import { join } from "node:path"; import { - NOT_READ, SNAPSHOT_FIELDS, SYSTEM_CONFIG_CODES, ORG_CONFIG_CODES, composeSnapshot, readSystemConfig, readOrgConfig, + NOT_READ, SNAPSHOT_FIELDS, SYSTEM_CONFIG_CODES, ORG_CONFIG_CODES, ORG_READS_AT_ONCE, composeSnapshot, readSystemConfig, readOrgConfig, runExitOnly, systemConfigCode, writeSnapshot, busFields, proxyFields, } from "../src/triage-snapshot.mjs"; @@ -155,3 +155,14 @@ test("the snapshot file is one 0600 file, replaced whole", () => { assert.deepEqual(readdirSync(join(dir, "health")), ["snapshot.json"]); assert.equal(JSON.parse(readFileSync(file, "utf8")).n, 2); }); + +test("organizations are read at most ORG_READS_AT_ONCE at a time and listed in registry order", async () => { + let live = 0, most = 0; + const ids = Array.from({ length: 10 }, (_, i) => `org${i}`); + const slow = async () => { live++; most = Math.max(most, live); await new Promise((r) => setTimeout(r, 5)); live--; return { valid: true, exit: 0, code: null }; }; + const snap = await composeSnapshot({ orgs: async () => ids.map((id) => ({ id, state: "active" })), orgConfig: slow }); + assert.equal(ORG_READS_AT_ONCE, 4); + assert.equal(most, ORG_READS_AT_ONCE); + assert.deepEqual(snap.organizations.list.map((o) => o.id), ids); + assert.equal(snap.organizations.count, 10); +});