373 lines
23 KiB
JavaScript
373 lines
23 KiB
JavaScript
// #1873 triage-host: the Triage session's host and its relay. The relay answers only a message to
|
|
// the person; the session sees its own run directory and the health snapshots and nothing else of
|
|
// the data root; its environment is a fixed list; nothing launches without a model route or over
|
|
// the cap. The session runs the stub adapter (tests/fixtures/triage-adapter.mjs); no model is called.
|
|
import { test } from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import { connect } from "node:net";
|
|
import { existsSync, lstatSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, symlinkSync, writeFileSync } from "node:fs";
|
|
import { join } from "node:path";
|
|
import { Client } from "../../bus/src/client.mjs";
|
|
import { sandboxProblem } from "../../seat/src/sandbox.mjs";
|
|
import {
|
|
NOT_STARTED, NOT_TAKEN, 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;
|
|
const skip = sandboxProblem() ?? false;
|
|
const scratch = () => mkdtempSync(join(process.env.TMPDIR, "th-"));
|
|
const CAP = "a".repeat(64);
|
|
const SNAPSHOT = {
|
|
snapshotVersion: 1, at: "2026-11-16T12:00:00.000Z", console: { state: "answering", at: "2026-11-16T12:00:00.000Z" },
|
|
proxy: "not read", systemConfig: { valid: true, exit: 0, code: null },
|
|
organizations: { count: 1, list: [{ id: "alpha", bus: { state: "stopped", lastStart: null }, processing: "not read", config: { valid: true, exit: 0, code: null } }] },
|
|
};
|
|
// What the Console's own environment might hold: none of it but the allowed names may reach the session.
|
|
const CONSOLE_ENV = {
|
|
PATH: "/should/not/pass", LANG: "C.UTF-8", TZ: "UTC", HOME: "/home/someone", MOSAIC_CONFIG: "/x/config.json",
|
|
MOSAIC_BUS_CONSOLE_SOCKET: "/x/bus.sock", MOSAIC_GITEA_CREDENTIAL_FILE: "/x/cred", GITEA_TOKEN: "t", SSH_AUTH_SOCK: "/x/agent",
|
|
ANTHROPIC_API_KEY: "from-the-console", XDG_CONFIG_HOME: "/x", MOSAIC_DATA_ROOT: "/x",
|
|
};
|
|
|
|
const raw = (socket, line) => new Promise((resolve, reject) => {
|
|
const c = connect(socket);
|
|
let input = "";
|
|
c.on("connect", () => c.write(line + "\n"));
|
|
c.on("data", (b) => { input += b; });
|
|
c.on("end", () => resolve(JSON.parse(input)));
|
|
c.on("error", reject);
|
|
});
|
|
|
|
test("triageEnv keeps the fixed names and the route's proxy variables, and nothing else", () => {
|
|
const env = triageEnv(CONSOLE_ENV, { ANTHROPIC_BASE_URL: "http://127.0.0.1:1", ANTHROPIC_AUTH_TOKEN: "k", MOSAIC_CONFIG: "/y", OTHER: "z" }, { node: "/opt/node/bin/node" });
|
|
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"]);
|
|
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");
|
|
});
|
|
|
|
test("the relay refuses every verb but a message to the person, and any call without the capability", async (t) => {
|
|
const dir = scratch();
|
|
const socket = join(dir, "relay.sock");
|
|
const sent = [];
|
|
const queue = [{ id: "owner-0123456789ab", from_role: "human", class: "REQUEST", body: "hello", at: "2026-11-16T12:00:00.000Z" }];
|
|
const read = [];
|
|
const server = await createRelay({ socket, cap: CAP, take: () => queue.splice(0), read: (id) => read.push(id), onSend: (m) => sent.push(m) });
|
|
t.after(() => server.close());
|
|
const client = new Client({ path: socket, cap: CAP });
|
|
assert.deepEqual(await client.call("role.claim"), { library: false });
|
|
assert.deepEqual(await client.call("processing.status"), { halted: false, invalid: false });
|
|
assert.deepEqual((await client.call("state")).tasks, { open: [], total: 0, cap: 0 });
|
|
assert.deepEqual(await client.call("message.receive"), [{ id: "owner-0123456789ab", from_role: "human", class: "REQUEST", body: "hello", at: "2026-11-16T12:00:00.000Z" }]);
|
|
assert.match((await client.call("message.send", { to: "human", body: "answer", class: "RESULT", in_reply_to: "owner-0123456789ab" })).id, /^triage-/);
|
|
await client.call("message.read", { id: "owner-0123456789ab" });
|
|
assert.deepEqual(sent, [{ body: "answer", in_reply_to: "owner-0123456789ab" }]);
|
|
assert.deepEqual(read, ["owner-0123456789ab"]);
|
|
// The model writes in_reply_to. Anything but an id the host issued is dropped, never stored.
|
|
for (const bad of ["<img src=x onerror=1>", "owner-1", "owner-0123456789ab\u202e", "owner-0123456789abc", "x".repeat(60000)]) {
|
|
sent.length = 0;
|
|
await client.call("message.send", { to: "human", body: "b", in_reply_to: bad });
|
|
assert.deepEqual(sent, [{ body: "b", in_reply_to: null }], bad.slice(0, 24));
|
|
}
|
|
const refused = [
|
|
["decision.raise", { kind: "hire", body: "x" }], ["event.emit", { kind: "action.allowed", body: {} }],
|
|
["message.send", { to: "chief", body: "x", class: "INFO" }], ["message.send", { to: "pm", body: "x" }],
|
|
["message.send", { to: "human" }], ["task.create", { title: "x" }], ["launch", { instance: "chief" }],
|
|
["plugin.tool", {}], ["toString", {}], ["__proto__", {}], ["constructor", {}],
|
|
];
|
|
for (const [verb, args] of refused) {
|
|
await assert.rejects(client.call(verb, args), (e) => e.code === RELAY_REFUSED, verb);
|
|
}
|
|
await assert.rejects(new Client({ path: socket, cap: "b".repeat(64) }).call("message.send", { to: "human", body: "x" }), (e) => e.code === RELAY_REFUSED);
|
|
await assert.rejects(new Client({ path: socket, human: "jason" }).call("message.receive"), (e) => e.code === RELAY_REFUSED);
|
|
assert.deepEqual(await raw(socket, "not json"), { ok: false, error: RELAY_REFUSED, reason: "the Triage host answers only a message to the person" });
|
|
assert.equal((await raw(socket, JSON.stringify({ cap: CAP.toUpperCase(), verb: "role.claim" }))).error, RELAY_REFUSED);
|
|
assert.equal(sent.length, 1, "no refused call reached the transcript");
|
|
assert.equal(lstatSync(socket).mode & 0o777, 0o600);
|
|
});
|
|
|
|
test("the prompt names the snapshot inside the session and the card form, after the role's contract", () => {
|
|
const text = hostPrompt("# Triage\n\nContract.\n", { snapshotPath: "/r/work/health/snapshot.json", steps: ["bus-start"] });
|
|
assert.match(text, /^# Triage\n\nContract\.\n\n## This host/);
|
|
assert.match(text, /\/r\/work\/health\/snapshot\.json/);
|
|
assert.match(text, /```cards\n\[\{"step": "bus-start", "org": "alpha"\}\]\n```/);
|
|
});
|
|
|
|
function host(t, root, opts = {}) {
|
|
const dataRoot = join(root, "data");
|
|
const h = createTriageHost({ dataRoot, readSnapshot: async () => SNAPSHOT, env: CONSOLE_ENV, readOnly: [FIXTURE], pollInterval: 100, ...opts });
|
|
t.after(() => h.stop());
|
|
return { h, dataRoot };
|
|
}
|
|
|
|
test("no model route: nothing launches, the message is refused, and the state says the route is not set", async (t) => {
|
|
const { h, dataRoot } = host(t, scratch());
|
|
assert.equal((await h.state()).route, "not set");
|
|
await assert.rejects(h.message("hello"), (e) => e.code === "triage-no-route" && /no model route is set/.test(e.message));
|
|
assert.equal(h.running, false);
|
|
assert.equal(existsSync(join(dataRoot, "triage", "agent", "session.json")), false);
|
|
assert.deepEqual((await h.state()).transcript, []);
|
|
});
|
|
|
|
test("over the cap: the message is refused until the next day and nothing launches", async (t) => {
|
|
let clock = Date.parse("2026-11-16T15:00:00.000Z"), asked = 0, answer = { ok: false, code: "triage-over-cap", until: "2026-11-17T00:00:00.000Z" };
|
|
const route = { admit: async () => { asked++; return answer; } };
|
|
const { h, dataRoot } = host(t, scratch(), { route, now: () => new Date(clock) });
|
|
assert.equal((await h.state()).route, "set");
|
|
assert.equal((await h.state()).overCapUntil, null);
|
|
await assert.rejects(h.message("hello"), (e) => e.code === "triage-over-cap" && e.until === "2026-11-17T00:00:00.000Z");
|
|
assert.equal(h.running, false);
|
|
assert.equal(existsSync(join(dataRoot, "triage", "agent", "session.json")), false);
|
|
assert.equal((await h.state()).overCapUntil, "2026-11-17T00:00:00.000Z");
|
|
// Held: a later route answer cannot reopen the day; the route is not asked again before the time.
|
|
answer = { ok: false, code: "triage-no-route" };
|
|
clock = Date.parse("2026-11-16T23:59:59.000Z");
|
|
await assert.rejects(h.message("hello"), (e) => e.code === "triage-over-cap" && e.until === "2026-11-17T00:00:00.000Z");
|
|
assert.equal(asked, 1);
|
|
// At the time, the hold ends and the route is asked again.
|
|
clock = Date.parse("2026-11-17T00:00:00.000Z");
|
|
assert.equal((await h.state()).overCapUntil, null);
|
|
await assert.rejects(h.message("hello"), (e) => e.code === "triage-no-route");
|
|
assert.equal(asked, 2);
|
|
assert.deepEqual((await h.state()).transcript, [], "a refused message is never written");
|
|
// An over-cap answer whose time is not usable (past, too far, not an ISO time) refuses without a hold.
|
|
for (const until of ["2026-11-17T00:00:00.000Z", "2026-11-20T00:00:00.000Z", "Tue, 17 Nov 2026 12:00:00 GMT", "tomorrow", 5]) {
|
|
let n = 0;
|
|
const r = { admit: async () => { n++; return { ok: false, code: "triage-over-cap", until }; } };
|
|
const later = host(t, scratch(), { route: r, now: () => new Date("2026-11-17T01:00:00.000Z") }).h;
|
|
await assert.rejects(later.message("a"), (e) => e.code === "triage-over-cap" && e.until === undefined, String(until));
|
|
await assert.rejects(later.message("b"), (e) => e.code === "triage-over-cap");
|
|
assert.equal(n, 2, `${until}: not held`);
|
|
assert.equal((await later.state()).overCapUntil, null);
|
|
}
|
|
const odd = { admit: async () => ({ ok: false, code: "rm -rf", until: 5 }) };
|
|
const other = host(t, scratch(), { route: odd }).h;
|
|
await assert.rejects(other.message("hello"), (e) => e.code === "triage-route-refused" && e.until === undefined);
|
|
});
|
|
|
|
test("a route whose answer carries a config, bus or founder variable is refused before anything starts", async (t) => {
|
|
for (const name of ["MOSAIC_CONFIG", "MOSAIC_BUS_CONSOLE_SOCKET", "GITEA_TOKEN", "SSH_AUTH_SOCK", "lower"]) {
|
|
const root = scratch();
|
|
const adapter = join(root, "adapter.sh");
|
|
writeFileSync(adapter, "exit 0\n");
|
|
const route = { admit: async () => ({ ok: true, adapter, harness: "fake", harnessEnv: { [name]: "x" }, harnessMounts: [], turnEnd: "exit", model: "m", provider: "p", env: {} }) };
|
|
const { h, dataRoot } = host(t, root, { route });
|
|
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");
|
|
mkdirSync(join(root, "elsewhere"), { recursive: true });
|
|
mkdirSync(dataRoot);
|
|
symlinkSync(join(root, "elsewhere"), join(dataRoot, "triage"));
|
|
const route = { admit: async () => ({ ok: true }) };
|
|
const { h } = host(t, root, { route });
|
|
await assert.rejects(h.message("hello"), (e) => e.code === "triage-dir-unsafe");
|
|
assert.deepEqual(readdirSync(join(root, "elsewhere")), []);
|
|
});
|
|
|
|
async function waitFor(fn, ms = 30000) {
|
|
const end = Date.now() + ms;
|
|
while (Date.now() < end) {
|
|
const v = await fn();
|
|
if (v) return v;
|
|
await new Promise((r) => setTimeout(r, 50));
|
|
}
|
|
throw new Error("timed out");
|
|
}
|
|
|
|
test("end to end under the sandbox: the first message starts the session, which sees only its two directories and answers into the transcript", { skip, timeout: 120000 }, async (t) => {
|
|
const root = scratch();
|
|
const dataRoot = join(root, "data");
|
|
// What the session must not see: an org root, a config, a secrets file, the host's transcript.
|
|
mkdirSync(join(dataRoot, "orgs", "alpha"), { recursive: true });
|
|
writeFileSync(join(dataRoot, "orgs", "alpha", "org.json"), "{}");
|
|
mkdirSync(join(root, "config"));
|
|
writeFileSync(join(root, "config", "config.json"), "{}");
|
|
mkdirSync(join(root, "secrets"));
|
|
writeFileSync(join(root, "secrets", "token"), "secret");
|
|
// A link left where the host makes its work directory must not steer the bind.
|
|
mkdirSync(join(dataRoot, "triage", "agent"), { recursive: true, mode: 0o700 });
|
|
symlinkSync(join(dataRoot, "orgs"), join(dataRoot, "triage", "agent", "work"));
|
|
const adapter = join(root, "adapter.sh");
|
|
writeFileSync(adapter, `exec '${process.execPath}' '${FIXTURE}'\n`, { mode: 0o644 });
|
|
const probe = [
|
|
join(dataRoot, "orgs", "alpha", "org.json"), join(root, "config", "config.json"), join(root, "secrets", "token"),
|
|
join(dataRoot, "triage", "host", "transcript.jsonl"), join(dataRoot, "triage", "host"),
|
|
join(dataRoot, "triage", "health", "snapshot.json"), join(dataRoot, "triage", "agent", "work", "health", "snapshot.json"),
|
|
];
|
|
let admits = 0;
|
|
const route = { admit: async () => { admits += 1; return { ok: true, adapter, harness: "fake", harnessEnv: { TRIAGE_PROBE: JSON.stringify(probe) }, harnessMounts: [], turnEnd: "exit", model: "stub", provider: "stub", env: { ANTHROPIC_BASE_URL: "http://127.0.0.1:9", OTHER: "x" } }; } };
|
|
const { h } = host(t, root, { route });
|
|
assert.equal(h.running, false, "nothing starts before the owner's first message");
|
|
assert.equal(existsSync(join(dataRoot, "triage", "agent", "session.json")), false);
|
|
|
|
const replies = async (n) => waitFor(async () => {
|
|
const list = (await h.state()).transcript.filter((e) => e.from === "triage");
|
|
return list.length >= n ? list : null;
|
|
});
|
|
|
|
await h.message("PROBE");
|
|
assert.equal(h.running, true);
|
|
const probed = JSON.parse((await replies(1))[0].text.replace(/\n\n.*$/s, ""));
|
|
const allowed = new Set([
|
|
"PATH", "LANG", "TZ", "HOME", "TMPDIR", "ANTHROPIC_BASE_URL", "TRIAGE_PROBE", "PWD", "SHLVL", "_", "OLDPWD",
|
|
"MOSAIC_REQUEST", "MOSAIC_SYSTEM_PROMPT_FILE", "MOSAIC_WORKSPACE", "MOSAIC_SESSION_DIR", "MOSAIC_POLICY_FILE", "MOSAIC_TOOLS_FILE",
|
|
"MOSAIC_TOOL_SOCKET", "MOSAIC_TURN_MARKER", "MOSAIC_PROVIDER", "MOSAIC_MODEL", "MOSAIC_MODEL_ACTIVITY_DIR", "MOSAIC_UNSLOP_LOG",
|
|
]);
|
|
for (const name of probed.env) assert.ok(allowed.has(name), `the session's environment holds ${name}`);
|
|
for (const name of ["MOSAIC_CONFIG", "MOSAIC_BUS_CONSOLE_SOCKET", "GITEA_TOKEN", "SSH_AUTH_SOCK", "ANTHROPIC_API_KEY", "OTHER", "MOSAIC_DATA_ROOT"]) assert.ok(!probed.env.includes(name), name);
|
|
assert.deepEqual(probed.seen, Object.fromEntries(probe.map((p) => [p, p.endsWith(join("work", "health", "snapshot.json"))])));
|
|
assert.equal(probed.snapshot, 1);
|
|
assert.equal(probed.writeHealth, "EROFS");
|
|
assert.equal(probed.writeBundle, "EROFS");
|
|
assert.equal(probed.writeWork, "written");
|
|
assert.deepEqual(readdirSync(join(dataRoot, "orgs")), ["alpha"], "the planted link's target is untouched");
|
|
assert.equal(lstatSync(join(dataRoot, "triage", "agent", "work")).isDirectory(), true);
|
|
assert.deepEqual(readdirSync(join(dataRoot, "triage", "health")), ["snapshot.json"]);
|
|
|
|
await h.message("CARDS");
|
|
const cards = (await replies(2))[1];
|
|
assert.deepEqual(cards.cards, [{ step: "bus-start", org: "alpha", command: "mosaic bus start alpha --detach" }]);
|
|
assert.equal(cards.dropped, 1);
|
|
assert.match(cards.text, /^The alpha bus is stopped\./);
|
|
|
|
await h.message("TOOL chief");
|
|
assert.match((await replies(3))[2].text, new RegExp(RELAY_REFUSED));
|
|
await h.message("TOOL human");
|
|
const last = await replies(5);
|
|
const texts = last.slice(3).map((e) => e.text);
|
|
assert.ok(texts.includes("from the tool"), "the session's own send to the person reaches the transcript");
|
|
assert.ok(texts.some((x) => /"ok":true/.test(x)), "and the tool call was answered");
|
|
assert.equal(admits, 4, "each message is admitted by the route");
|
|
|
|
const tr = join(dataRoot, "triage", "host", "transcript.jsonl");
|
|
assert.equal(lstatSync(tr).mode & 0o777, 0o600);
|
|
for (const d of ["", "health", "agent", "host"]) assert.equal(lstatSync(join(dataRoot, "triage", d)).mode & 0o777, 0o700, d);
|
|
const owner = readFileSync(tr, "utf8").trim().split("\n").map((l) => JSON.parse(l)).filter((e) => e.from === "owner").map((e) => e.text);
|
|
assert.deepEqual(owner, ["PROBE", "CARDS", "TOOL chief", "TOOL human"]);
|
|
const tools = JSON.parse(readFileSync(join(dataRoot, "triage", "agent", "bundle", "tools.json"), "utf8"));
|
|
assert.deepEqual(tools.map((x) => x.name), ["send_message"]);
|
|
const policy = JSON.parse(readFileSync(join(dataRoot, "triage", "agent", "bundle", "policy.json"), "utf8"));
|
|
assert.deepEqual([policy.tools, policy.typed, policy.network, policy.needs, policy.credentials], [["read", "grep", "find", "ls"], ["send_message"], "none", [], []]);
|
|
|
|
await h.stop();
|
|
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");
|
|
});
|
|
|
|
test("a launcher that cannot run is a failed start: the message is not delivered, nothing reports running, and the next message is refused the same way", async (t) => {
|
|
const root = scratch();
|
|
const adapter = join(root, "adapter.sh");
|
|
writeFileSync(adapter, "exit 0\n");
|
|
const route = { admit: async () => ({ ok: true, adapter, harness: "fake", harnessEnv: {}, harnessMounts: [], turnEnd: "exit", model: "m", provider: "p", env: {} }) };
|
|
const { h, dataRoot } = host(t, root, { route, bwrap: join(root, "no-such-launcher") });
|
|
for (const text of ["one", "two"]) {
|
|
await assert.rejects(h.message(text), (e) => e.code === "triage-start-failed", text);
|
|
assert.equal(h.running, false, text);
|
|
}
|
|
const lines = (await h.state()).transcript.map((e) => [e.from, e.text]);
|
|
assert.deepEqual(lines, [["owner", "one"], ["host", NOT_STARTED], ["owner", "two"], ["host", NOT_STARTED]]);
|
|
assert.equal(existsSync(join(dataRoot, "triage", "agent", "relay.sock")), false);
|
|
});
|
|
|
|
test("a reply that names its message answers it: a session killed after replying leaves no unanswered line", { skip, timeout: 120000 }, async (t) => {
|
|
const root = scratch();
|
|
const { h } = host(t, root, { route: stubRoute(root) });
|
|
const sent = await h.message("REPLY-SLEEP");
|
|
const [reply] = await repliesOf(h, 1);
|
|
assert.deepEqual([reply.text.replace(/\n\n.*$/s, ""), reply.in_reply_to], ["answered early", sent.id]);
|
|
process.kill(-h.pid, "SIGKILL");
|
|
await waitFor(() => !h.running);
|
|
assert.deepEqual((await h.state()).transcript.filter((e) => e.from === "host"), []);
|
|
});
|
|
|
|
test("a message the session never read gets a host line when it ends and leaves the queue", { skip, timeout: 120000 }, async (t) => {
|
|
const root = scratch();
|
|
const { h, dataRoot } = host(t, root, { route: stubRoute(root) });
|
|
const slept = await h.message("SLEEP");
|
|
await waitFor(() => { try { return readdirSync(join(dataRoot, "triage", "agent", "turns")).length > 0; } catch { return false; } });
|
|
// The runner takes one message per turn and is held in SLEEP, so this one is queued and never taken.
|
|
const waiting = await h.message("waiting");
|
|
process.kill(-h.pid, "SIGKILL");
|
|
await waitFor(() => !h.running);
|
|
const notes = (await repliesOf(h, 2, "host")).map((e) => [e.in_reply_to, e.text]).sort((a, b) => a[1].localeCompare(b[1]));
|
|
assert.deepEqual(notes, [[waiting.id, NOT_TAKEN], [slept.id, UNANSWERED]].sort((a, b) => a[1].localeCompare(b[1])));
|
|
});
|
|
|
|
test("with devOwner set, a card's bus start carries the dev-owner flag", { skip, timeout: 120000 }, async (t) => {
|
|
const root = scratch();
|
|
const { h } = host(t, root, { route: stubRoute(root), devOwner: true });
|
|
await h.message("CARDS");
|
|
const [reply] = await repliesOf(h, 1);
|
|
assert.deepEqual(reply.cards, [{ step: "bus-start", org: "alpha", command: "mosaic bus start alpha --dev-local-owner --detach" }]);
|
|
});
|