Files
stack/packages/cli/tests/verbs.test.mjs
T
jason.woltjeandClaude Opus 5.5 c9a5650ed0 bus, webui, cli: the Console shows the reply to a web send (#1575)
Row 81. The broker gains a `replies` read verb for human and reader
sessions: `{id}`, a message UUID, returns that business's messages whose
in_reply_to is the id, oldest first, at most 50, and writes no
deliveries row, so the reply still reaches the next `mosaic talk`. An
unknown id refuses message-not-found. The Console serves it through
BUS_VERBS with an id check, and the Sent receipt shows the replies to a
web send. The cli's talk timeout message and README now say the reply
may still come and the next `mosaic talk` prints it.

Dewey's round 2, candidate manifest 3ce7527f, base 1e5f5958, a rebase
after row 61. Approved by Darkwing (27783 round 1, 27821 round 2).
Sage's gate green 19:25:18Z to 19:30:39Z. Live at the next bus restart
(the broker verb) and Console restart (the view), bus first.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-12 14:33:11 -05:00

148 lines
7.8 KiB
JavaScript

// The S6 human verbs: talk, stop and launches. The human transport is the
// in-process broker from helpers.mjs, because the real one refuses to run
// under an agent (src/transport.mjs). A real stop of a real runner is in
// packages/seat/tests/session.test.mjs and tests/launcher.test.mjs.
import { test } from "node:test";
import assert from "node:assert/strict";
import { main } from "../src/cli.mjs";
import { CliError } from "../src/errors.mjs";
import { startTimeOf } from "../src/host.mjs";
import { Registry } from "../../seat/src/session.mjs";
import { broker, io, tmp } from "./helpers.mjs";
function setup(t, deps = {}) {
const bus = broker(t);
const dataRoot = tmp(t);
const run = async (argv, x = io(), more = {}) => {
try {
await main(argv, x, { system: { dataRoot }, transport: bus.transport, talkPollMs: 20, ...deps, ...more });
return { code: 0, io: x };
} catch (e) {
if (!(e instanceof CliError)) throw e;
return { code: e.exitCode, error: e.message, io: x };
}
};
return { ...bus, dataRoot, run };
}
const call = (b, cap, verb, args = {}) => b.request(cap, { verb, args });
test("talk sends a REQUEST, prints and reads everything that arrives, and stops at the reply", async (t) => {
const s = setup(t);
const cto = s.b.bindLaunch({ business: "demo", role: "cto", run: "cto-run", harness: "pi" });
call(s.b, cto, "role.claim");
// The coder answers on the second poll; a cto note arrives first and is printed too.
let polls = 0;
const transport = (business) => {
const inner = s.transport(business);
return async (verb, args) => {
if (verb === "message.receive" && ++polls === 2) {
const [m] = call(s.b, s.coder, "message.receive");
call(s.b, cto, "message.send", { to: m.from_role, body: "fyi", class: "INFO" });
call(s.b, s.coder, "message.send", { to: m.from_role, body: "done: 3 files", class: "RESULT", in_reply_to: m.id });
}
return inner(verb, args);
};
};
const r = await s.run(["talk", "coder", "fix the build", "--business", "demo"], io(), { transport });
assert.equal(r.code, 0, r.error);
const sent = s.calls.find((c) => c.verb === "message.send");
assert.deepEqual(sent.args, { to: "coder", body: "fix the build", class: "REQUEST" });
assert.match(r.io.out.text, /^sent [0-9a-f]{8} to coder\n/);
assert.match(r.io.out.text, /cto \(INFO\) [0-9a-f]{8}:\nfyi\n/);
assert.match(r.io.out.text, /coder \(RESULT, re [0-9a-f]{8}\) [0-9a-f]{8}:\ndone: 3 files\n$/);
assert.equal(s.calls.filter((c) => c.verb === "message.read").length, 2);
assert.deepEqual(await s.transport("demo")("message.receive"), [], "both were marked read");
});
test("talk --wait 0 only sends; no reply within --wait exits 1 and says where it will show", async (t) => {
const s = setup(t);
const quick = await s.run(["talk", "coder", "hello", "--wait", "0", "--business", "demo"]);
assert.equal(quick.code, 0);
assert.deepEqual(s.calls.map((c) => c.verb), ["message.send"]);
const slow = await s.run(["talk", "coder", "hello", "--wait", "1", "--business", "demo"]);
assert.equal(slow.code, 1);
// Row 81 (#1575): only mosaic talk shows a late reply; mosaic inbox lists decisions, never messages.
assert.equal(slow.error, "no reply from coder within 1 s; it may still come, and the next mosaic talk shows it");
});
test("a late reply the Console has listed still reaches the next talk", async (t) => {
const s = setup(t);
assert.equal((await s.run(["talk", "coder", "hello", "--wait", "1", "--business", "demo"])).code, 1);
const { id } = s.store.get("SELECT id FROM messages WHERE from_role='human' ORDER BY seq DESC LIMIT 1");
assert.equal(call(s.b, s.coder, "message.receive")[0].id, id);
const answer = call(s.b, s.coder, "message.send", { to: "human", body: "late answer", class: "RESULT", in_reply_to: id });
// What the Console's send receipt reads, through its reader session.
assert.deepEqual(s.read("replies", { id }).map((m) => m.id), [answer.id]);
const next = await s.run(["talk", "coder", "again", "--wait", "1", "--business", "demo"]);
assert.equal(next.code, 1, "no answer to the second one");
assert.match(next.io.out.text, new RegExp(`coder \\(RESULT, re ${id.slice(0, 8)}\\) ${answer.id.slice(0, 8)}:\nlate answer\n`));
assert.deepEqual(s.calls.filter((c) => c.verb === "message.read").map((c) => c.args.id), [answer.id]);
});
test("talk, stop and launches off/on refuse inside an agent run; bad arguments are usage errors", async (t) => {
const s = setup(t);
const agent = () => io({ ...io().env, CLAUDECODE: "1" });
for (const argv of [["talk", "coder", "hi", "--business", "demo"], ["stop", "r0123456789ab"], ["launches", "off", "--business", "demo"], ["launches", "on", "--business", "demo"]]) {
assert.equal((await s.run(argv, agent())).code, 3, argv.join(" "));
}
const usage = [
["talk", "coder", "--business", "demo"],
["talk", "coder", "a", "b", "--business", "demo"],
["talk", "coder", "hi", "--wait", "-1", "--business", "demo"],
["talk", "coder", "hi", "--wait", "1.5", "--business", "demo"],
["stop"],
["stop", "a", "b"],
["stop", "a", "--business", "demo"],
["launches"],
["launches", "maybe"],
["launches", "off", "x", "--business", "demo"],
["launches", "list", "x"],
];
for (const argv of usage) assert.equal((await s.run(argv)).code, 4, argv.join(" "));
assert.equal(s.calls.length, 0);
});
test("launches off and on go to the broker and change the business's launch state", async (t) => {
const s = setup(t);
const state = () => s.store.get("SELECT state FROM launch_state WHERE business=?", "demo")?.state;
const off = await s.run(["launches", "off", "--business", "demo"]);
assert.equal(off.code, 0, off.error);
assert.match(off.io.out.text, /^launches off for demo: .*running sessions keep running\n$/);
assert.equal(state(), "revoked");
const on = await s.run(["launches", "on", "--business", "demo"]);
assert.equal(on.io.out.text, "launches on for demo\n");
assert.notEqual(state(), "revoked");
assert.deepEqual(s.calls.map((c) => c.verb), ["launch.revoke", "launch.restore"]);
});
test("launches list reads sessions.json and marks a stale entry; stop reports it and refuses a non-runner", async (t) => {
const s = setup(t);
assert.equal((await s.run(["launches", "list"])).io.out.text, "no sessions\n");
const r = new Registry(s.dataRoot);
const base = { business: "demo", harness: "pi", model: "claude-sonnet-5-5", launchedBy: "pm", startedAt: "2026-10-09T00:00:00.000Z", runnerPid: process.pid };
r.add({ ...base, instance: "coder", run: "r000000000001", runnerStartTime: startTimeOf(process.pid) });
r.add({ ...base, instance: "reviewer", run: "r000000000002", runnerStartTime: "1" });
const list = await s.run(["launches", "list"]);
assert.equal(
list.io.out.text,
"r000000000001 demo/coder pi claude-sonnet-5-5 by pm since 2026-10-09T00:00:00.000Z running\n" +
"r000000000002 demo/reviewer pi claude-sonnet-5-5 by pm since 2026-10-09T00:00:00.000Z not running (stale)\n",
);
const json = JSON.parse((await s.run(["launches", "list", "--json"])).io.out.text);
assert.deepEqual(json.map((e) => [e.run, e.live]), [["r000000000001", true], ["r000000000002", false]]);
const stale = await s.run(["stop", "r000000000002"]);
assert.equal(stale.code, 0);
assert.equal(stale.io.out.text, "run r000000000002 is not running (stale entry)\n");
const unknown = await s.run(["stop", "nope"]);
assert.equal(unknown.code, 2);
assert.match(unknown.error, /no running session has run id nope/);
// This test process is live with the recorded start time but is not a runner.
const notRunner = await s.run(["stop", "r000000000001"]);
assert.equal(notRunner.code, 2);
assert.match(notRunner.error, /is not a session runner; refusing to signal it/);
assert.equal(s.calls.length, 0, "stop and list never touch the bus");
});