fix(webui): triage review leftovers, the Stop hook records the last prompt it checked, fence and host P3s, a steadier snapshot read (#1873) #1987
No files matched your search
@@ -265,8 +265,12 @@ export returns:
|
||||
`harnessEnv` name only when it is listed here and is not one the host
|
||||
always refuses: `MOSAIC_*`, `NODE_*`, `LD_*`, `DYLD_*`, `PYTHON*`, `BASH_*`,
|
||||
`GIT_*`, `MALLOC_*`, `NPM_CONFIG_*`, `XDG_*`, any name ending in `_DIR`,
|
||||
`_CONF` or `_CONFIG`, PATH, HOME, TMPDIR, ENV, the founder's and the
|
||||
provider's names, and named shell, proxy, libc, OpenSSL and Java hooks. `installRoots` lists the directories
|
||||
`_CONF`, `_CONFIG`, `_ASKPASS`, `_CA_BUNDLE` or `_BASE_URL`, PATH, HOME,
|
||||
TMPDIR, ENV, the founder's and the provider's names, and named shell,
|
||||
proxy, resolver, libc, OpenSSL and Java hooks, Perl and Ruby start
|
||||
options and library paths, less's input filters, Lua's start code,
|
||||
curl's config home and a TLS key log. The full list is
|
||||
`REFUSED_NAMES` in `packages/webui/src/triage-host.mjs`. `installRoots` lists the directories
|
||||
where the adapter and the mounts live. A hosted adapter or mount source
|
||||
must resolve inside one of them, and never in or around the data root,
|
||||
the config directory, HOME, `/srv/mosaic` or a path with a `secret`
|
||||
|
||||
@@ -232,25 +232,32 @@ const secretPath = (p) => p.split("/").some((seg) => /secret/i.test(seg));
|
||||
const realOr = (p) => { try { return realpathSync(p); } catch { return p; } };
|
||||
const plainObject = (o) => o !== null && typeof o === "object" && !Array.isArray(o) && [Object.prototype, null].includes(Object.getPrototypeOf(o));
|
||||
|
||||
export function checkHostedRoute({ adapter, harnessMounts, installRoots, protectedPaths, codeRoot = null, codeHolder = null, runDir, node = process.execPath }) {
|
||||
// `alsoFixed` is a test hook: it adds stand-in fixed paths and never removes a shipped one.
|
||||
export function checkHostedRoute({ adapter, harnessMounts, installRoots, protectedPaths, codeRoot = null, codeHolder = null, runDir, node = process.execPath, alsoFixed = [] }) {
|
||||
if (!cleanPath(runDir)) refuseRoute("protected", "the run directory is not a clean absolute path");
|
||||
if (!Array.isArray(protectedPaths)) refuseRoute("protected", "no protected paths were given");
|
||||
if (!Array.isArray(alsoFixed)) refuseRoute("protected", "the added fixed paths are not a list");
|
||||
// Lists are copied once, so a Proxy cannot answer one length to a check and another to the map.
|
||||
protectedPaths = Array.from(protectedPaths);
|
||||
// Each guarded entry: its spellings, and whether a source in the code root is clear of it. The
|
||||
// fixed paths and the run dir never hold the code. Their spellings are looked up once, here, and
|
||||
// serve both the code-root check and their guards, so the two always see the same real path.
|
||||
const fixed = [...ALWAYS_PROTECTED, ...Array.from(alsoFixed), runDir].map((p) => {
|
||||
if (!cleanPath(p)) refuseRoute("protected", "a protected path is not a clean absolute path");
|
||||
return { spellings: [p, realOr(p)], holdsCode: false };
|
||||
});
|
||||
let code = null;
|
||||
if (codeRoot !== null) {
|
||||
if (!cleanPath(codeRoot)) refuseRoute("protected", "the code root is not a clean absolute path");
|
||||
if (!protectedPaths.includes(codeHolder)) refuseRoute("protected", "the code root's holder is not a protected path");
|
||||
code = [codeRoot, realOr(codeRoot)];
|
||||
const fixed = [runDir, ...ALWAYS_PROTECTED].flatMap((g) => [g, realOr(g)]);
|
||||
if (code.some((c) => fixed.some((g) => overlaps(c, g)))) refuseRoute("protected", "the code root is in or around a path that is never set aside");
|
||||
if (code.some((c) => fixed.some((g) => g.spellings.some((s) => overlaps(c, s))))) refuseRoute("protected", "the code root is in or around a path that is never set aside");
|
||||
}
|
||||
// Each guarded entry: its spellings, and whether a source in the code root is clear of it.
|
||||
const guarded = [];
|
||||
for (const [p, mayHoldCode] of [...ALWAYS_PROTECTED.map((g) => [g, false]), ...protectedPaths.map((g) => [g, g === codeHolder]), [runDir, false]]) {
|
||||
const guarded = [...fixed];
|
||||
for (const p of protectedPaths) {
|
||||
if (!cleanPath(p)) refuseRoute("protected", "a protected path is not a clean absolute path");
|
||||
const spellings = [p, realOr(p)];
|
||||
const holdsCode = mayHoldCode && code !== null && spellings.some((g) => code.some((c) => c !== g && within(c, g)));
|
||||
const holdsCode = p === codeHolder && code !== null && spellings.some((g) => code.some((c) => c !== g && within(c, g)));
|
||||
guarded.push({ spellings, holdsCode });
|
||||
}
|
||||
if (!Array.isArray(installRoots)) refuseRoute("roots", "the harness module declares no usable install roots");
|
||||
|
||||
@@ -177,6 +177,34 @@ test("a protected path that holds the code root (HOME over a copy or a worktree)
|
||||
assert.throws(() => checkHostedRoute({ adapter: join(realRun, "code", "inst", "a.sh"), harnessMounts: [], installRoots: [join(realRun, "code", "inst")], protectedPaths: [p.dataRoot, home], codeRoot: join(realRun, "code"), codeHolder: home, runDir: join(p.root, "runlink") }), (e) => e.fence === "protected", "code root in the run dir's real path");
|
||||
});
|
||||
|
||||
test("a fixed path named through a link: a code root in its real path is refused before any guard, and a source there is refused by its guard", async (t) => {
|
||||
const p = await layout(t);
|
||||
// None of the shipped fixed paths is a link on a test host, so the test hook adds a stand-in.
|
||||
const home = join(p.root, "home");
|
||||
// The real path lies outside HOME and the data root, so only the stand-in's own guard can refuse
|
||||
// a source there.
|
||||
const real = join(p.root, "fixedreal");
|
||||
const inst = join(real, "code", "inst");
|
||||
mkdirSync(inst, { recursive: true });
|
||||
writeFileSync(join(inst, "a.sh"), "x");
|
||||
const link = join(p.root, "fixedlink");
|
||||
symlinkSync(real, link);
|
||||
const base = { adapter: join(inst, "a.sh"), harnessMounts: [], installRoots: [inst], protectedPaths: [p.dataRoot, home], codeHolder: home, runDir: p.runDir, alsoFixed: [link] };
|
||||
const refused = (opts, fence, message, label) => assert.throws(() => checkHostedRoute({ ...base, ...opts }), (e) => e.code === "hosted-binds-refused" && e.fence === fence && message.test(e.message), label);
|
||||
// The code root lies in the stand-in's real path, not under the name the list gives.
|
||||
refused({ codeRoot: join(real, "code") }, "protected", /never set aside/, "code root in a fixed path's real path");
|
||||
// With the code root in HOME, the source in the stand-in's real path is refused by the stand-in's
|
||||
// guard, which holds the same real spelling. No other guard holds that path.
|
||||
mkdirSync(join(home, "code"), { recursive: true });
|
||||
refused({ codeRoot: join(home, "code") }, "adapter", /protected path/, "source in a fixed path's real path");
|
||||
// The same source passes without the stand-in: only the stand-in refused it.
|
||||
assert.equal(checkHostedRoute({ ...base, alsoFixed: [], codeRoot: join(home, "code") }).adapter.src, join(inst, "a.sh"));
|
||||
// The hook adds fixed paths and never removes one, and takes only a list of clean absolute paths.
|
||||
refused({ alsoFixed: [], codeRoot: "/srv/mosaic/x" }, "protected", /never set aside/, "shipped fixed path with an empty hook");
|
||||
refused({ alsoFixed: ["relative"], codeRoot: join(real, "code") }, "protected", /clean absolute/, "relative stand-in");
|
||||
refused({ alsoFixed: "/x", codeRoot: join(real, "code") }, "protected", /clean absolute|list/, "a hook that is not a list");
|
||||
});
|
||||
|
||||
test("a list that answers one length to the check and another later is read once", async (t) => {
|
||||
const p = await layout(t);
|
||||
const items = [];
|
||||
|
||||
@@ -75,14 +75,15 @@ const ENV_VALUE_MAX = 4096;
|
||||
const HARNESS_ENV_MAX = 64;
|
||||
// Refused in harnessEnv even when the harness module declares them: the runner's own MOSAIC_*
|
||||
// values, loader, libc and interpreter hooks, the shell's start files, a tool's config directory
|
||||
// and the founder's, an askpass program, a CA bundle or a base URL. A provider name (every ANTHROPIC_* among them) goes through route env or nowhere.
|
||||
// and the founder's, an askpass program, a CA bundle or a base URL, Lua's start code, curl's config
|
||||
// home and a TLS key log. A provider name (every ANTHROPIC_* among them) goes through route env or nowhere.
|
||||
const REFUSED_PREFIX = /^(MOSAIC_|NODE_|LD_|DYLD_|PYTHON|BASH_|GIT_|MALLOC_|NPM_CONFIG_|XDG_)/;
|
||||
const REFUSED_SUFFIX = /_(DIR|CONF|CONFIG|ASKPASS|CA_BUNDLE|BASE_URL)$/;
|
||||
const REFUSED_NAMES = new Set([
|
||||
"PATH", "HOME", "TMPDIR", "BASH_ENV", "ENV", "SSH_AUTH_SOCK", "SHELL", "SHELLOPTS", "BASHOPTS", "PS4", "IFS", "CDPATH",
|
||||
"GLOBIGNORE", "PROMPT_COMMAND", "ZDOTDIR", "PERL5OPT", "PERL5LIB", "RUBYOPT", "RUBYLIB", "PERLLIB", "LESSOPEN", "LESSCLOSE", "HOSTALIASES", "HTTP_PROXY", "HTTPS_PROXY", "ALL_PROXY", "NO_PROXY",
|
||||
"GCONV_PATH", "GLIBC_TUNABLES", "LOCPATH", "OPENSSL_CONF", "OPENSSL_ENGINES", "OPENSSL_MODULES", "SSL_CERT_FILE", "SSL_CERT_DIR",
|
||||
"JAVA_TOOL_OPTIONS", "_JAVA_OPTIONS", "JDK_JAVA_OPTIONS",
|
||||
"JAVA_TOOL_OPTIONS", "_JAVA_OPTIONS", "JDK_JAVA_OPTIONS", "LUA_INIT", "CURL_HOME", "SSLKEYLOGFILE",
|
||||
...FOUNDER_ENV,
|
||||
]);
|
||||
export const refusedHarnessName = (k) => typeof k !== "string" || !ENV_NAME.test(k) || REFUSED_PREFIX.test(k) || REFUSED_SUFFIX.test(k) || REFUSED_NAMES.has(k) || isProviderVariable(k);
|
||||
@@ -175,8 +176,9 @@ function sameCap(a, b) {
|
||||
return timingSafeEqual(Buffer.from(a), Buffer.from(b));
|
||||
}
|
||||
|
||||
// The relay server. `onSend(body)` keeps a human-bound send; `take()` returns the queued owner
|
||||
// messages; `read(id)` drops one that was answered.
|
||||
// The relay server. `onSend({ body, in_reply_to, result })` keeps a human-bound send, `result` true
|
||||
// only for class RESULT, the class the runner gives its answer to a turn; `take()` returns the
|
||||
// queued owner messages; `read(id)` drops one that was answered.
|
||||
export function createRelay({ socket, cap, take, read, onSend, log = () => {} }) {
|
||||
const answer = (conn, value) => { try { conn.end(JSON.stringify(value) + "\n"); } catch {} };
|
||||
const refuse = (conn) => answer(conn, { ok: false, error: RELAY_REFUSED, reason: "the Triage host answers only a message to the person" });
|
||||
@@ -203,7 +205,7 @@ export function createRelay({ socket, cap, take, read, onSend, log = () => {} })
|
||||
if (verb === "message.read") { read(args.id); return answer(conn, { ok: true, result: {} }); }
|
||||
if (verb === "message.send" && args.to === "human" && typeof args.body === "string" && args.body.length <= 32768) {
|
||||
// Only an id the host issued is kept. The model writes this field, and the transcript serves it.
|
||||
onSend({ body: args.body, in_reply_to: typeof args.in_reply_to === "string" && OWNER_ID.test(args.in_reply_to) ? args.in_reply_to : null });
|
||||
onSend({ body: args.body, in_reply_to: typeof args.in_reply_to === "string" && OWNER_ID.test(args.in_reply_to) ? args.in_reply_to : null, result: args.class === "RESULT" });
|
||||
return answer(conn, { ok: true, result: { id: `triage-${randomBytes(6).toString("hex")}` } });
|
||||
}
|
||||
} catch (e) {
|
||||
@@ -380,12 +382,15 @@ export function createTriageHost({ dataRoot, route = null, readSnapshot = async
|
||||
const i = queue.findIndex((m) => m.id === id);
|
||||
if (i >= 0) queue.splice(i, 1);
|
||||
},
|
||||
// A reply that names a queued message answers it: the message leaves the queue here, so a
|
||||
// session that ends before its message.read leaves no "unanswered" line under an answer.
|
||||
onSend: ({ body, in_reply_to }) => {
|
||||
// An answer (class RESULT) that names a queued message answers it: the message leaves the queue
|
||||
// here, so a session that ends before its message.read leaves no "unanswered" line under an
|
||||
// answer. A note in any other class stays in the transcript but leaves the message queued, so
|
||||
// a session that ends after a progress note still gets the "unanswered" line. The model's tool
|
||||
// can also pick class RESULT; such a note settles the message as the runner's answer would.
|
||||
onSend: ({ body, in_reply_to, result }) => {
|
||||
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 } : {}) });
|
||||
const i = in_reply_to ? queue.findIndex((m) => m.id === in_reply_to) : -1;
|
||||
const i = in_reply_to && result === true ? queue.findIndex((m) => m.id === in_reply_to) : -1;
|
||||
if (i >= 0) queue.splice(i, 1);
|
||||
},
|
||||
log,
|
||||
|
||||
@@ -39,7 +39,7 @@ 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.
|
||||
// At most this many organizations' config checks run at once; each starts a node child.
|
||||
export const ORG_READS_AT_ONCE = 4;
|
||||
|
||||
// What a failed system config check may say. Each refusal code the validator prints as its first
|
||||
@@ -190,8 +190,11 @@ async function mapLimit(items, limit, fn) {
|
||||
}
|
||||
|
||||
// The organization stage runs under one deadline, `timeoutMs` from its start, however many
|
||||
// organizations there are: an organization whose reads have not finished by then is "not read", and
|
||||
// one not yet begun is not read at all. A whole snapshot takes at most about two timeouts.
|
||||
// organizations there are: a read that has not finished by then is "not read", and one not yet
|
||||
// begun is not read at all. The bus and processing reads are socket reads and begin for every
|
||||
// organization at once. Only the config checks, a node child each, wait for one of
|
||||
// ORG_READS_AT_ONCE slots, so a hung check holds back other checks but never another
|
||||
// organization's socket reads. A whole snapshot takes at most about two timeouts.
|
||||
export async function composeSnapshot(readers = {}, { now = () => new Date(), timeoutMs = READ_TIMEOUT_MS, clock = Date.now } = {}) {
|
||||
const at = now().toISOString();
|
||||
const read = (name, ...args) => (typeof readers[name] === "function" ? bounded(() => readers[name](...args), timeoutMs) : Promise.resolve(NOT_READ));
|
||||
@@ -200,21 +203,26 @@ export async function composeSnapshot(readers = {}, { now = () => new Date(), ti
|
||||
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 deadline = clock() + timeoutMs;
|
||||
const list = await mapLimit(active, ORG_READS_AT_ONCE, async ({ id }) => {
|
||||
// fn bounded by what is left of the deadline, or NOT_READ once nothing is.
|
||||
const inTime = (fn) => {
|
||||
const left = deadline - clock();
|
||||
if (left <= 0) return { id, bus: NOT_READ, processing: NOT_READ, config: NOT_READ };
|
||||
const readOrg = (name) => (typeof readers[name] === "function" ? bounded(() => readers[name](id), left) : NOT_READ);
|
||||
const [bus, processing, config] = await Promise.all([
|
||||
typeof readers.busStatus === "function"
|
||||
? bounded(async () => {
|
||||
try { return busFields(await readers.busStatus(id)); }
|
||||
catch (e) { return e?.name === "CliError" || e?.dead === true ? busFields(null, true) : NOT_READ; }
|
||||
}, left)
|
||||
: NOT_READ,
|
||||
readOrg("processing"),
|
||||
readOrg("orgConfig"),
|
||||
]);
|
||||
return { id, bus: bus === NOT_READ ? NOT_READ : bus, processing: processingFields(processing), config: configFields(config, ORG_CONFIG_CODES) };
|
||||
return left > 0 ? bounded(fn, left) : Promise.resolve(NOT_READ);
|
||||
};
|
||||
const readOrg = (name, id) => (typeof readers[name] === "function" ? inTime(() => readers[name](id)) : Promise.resolve(NOT_READ));
|
||||
const sockets = Promise.all(active.map(({ id }) => Promise.all([
|
||||
typeof readers.busStatus === "function"
|
||||
? inTime(async () => {
|
||||
try { return busFields(await readers.busStatus(id)); }
|
||||
catch (e) { return e?.name === "CliError" || e?.dead === true ? busFields(null, true) : NOT_READ; }
|
||||
})
|
||||
: NOT_READ,
|
||||
readOrg("processing", id),
|
||||
])));
|
||||
const configs = await mapLimit(active, ORG_READS_AT_ONCE, ({ id }) => readOrg("orgConfig", id));
|
||||
const socketReads = await sockets;
|
||||
const list = active.map(({ id }, i) => {
|
||||
const [bus, processing] = socketReads[i];
|
||||
return { id, bus: bus === NOT_READ ? NOT_READ : bus, processing: processingFields(processing), config: configFields(configs[i], ORG_CONFIG_CODES) };
|
||||
});
|
||||
organizations = { count: list.length, list };
|
||||
}
|
||||
|
||||
+7
-2
@@ -6,7 +6,8 @@
|
||||
// CARDS answer a reply with a cards fence: one valid card, one unknown step
|
||||
// TOOL <to> call send_message to <to> through the tool socket and answer the raw reply
|
||||
// SLEEP hang until killed
|
||||
// REPLY-SLEEP send the person an answer to this message through the tool socket, then hang until killed
|
||||
// REPLY-SLEEP send the person an answer (class RESULT) to this message through the tool socket, then hang until killed
|
||||
// NOTE-SLEEP send the person a progress note (class INFO) on this message through the tool socket, then hang until killed
|
||||
// anything else answer "echo: <body>"
|
||||
import { existsSync, readFileSync, writeFileSync } from "node:fs";
|
||||
import { connect } from "node:net";
|
||||
@@ -57,7 +58,11 @@ if (body === "PROBE") {
|
||||
process.stdout.write(await toolCall("send_message", { to: body.slice(5), body: "from the tool", class: "INFO" }));
|
||||
} else if (body === "REPLY-SLEEP") {
|
||||
const id = request.match(/^Message (\S+) from /m)?.[1];
|
||||
await toolCall("send_message", { to: "human", body: "answered early", class: "INFO", in_reply_to: id });
|
||||
await toolCall("send_message", { to: "human", body: "answered early", class: "RESULT", in_reply_to: id });
|
||||
setInterval(() => {}, 1000);
|
||||
} else if (body === "NOTE-SLEEP") {
|
||||
const id = request.match(/^Message (\S+) from /m)?.[1];
|
||||
await toolCall("send_message", { to: "human", body: "still looking", class: "INFO", in_reply_to: id });
|
||||
setInterval(() => {}, 1000);
|
||||
} else if (body === "SLEEP") {
|
||||
setInterval(() => {}, 1000);
|
||||
|
||||
@@ -65,13 +65,20 @@ test("the relay refuses every verb but a message to the person, and any call wit
|
||||
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(sent, [{ body: "answer", in_reply_to: "owner-0123456789ab", result: true }]);
|
||||
assert.deepEqual(read, ["owner-0123456789ab"]);
|
||||
// Only the class RESULT send, the runner's answer to its turn, says the message is answered. A note
|
||||
// in any other class, or with no class, is kept but does not.
|
||||
for (const cls of ["INFO", "REQUEST", "result", undefined, 7]) {
|
||||
sent.length = 0;
|
||||
await client.call("message.send", { to: "human", body: "note", class: cls, in_reply_to: "owner-0123456789ab" });
|
||||
assert.deepEqual(sent, [{ body: "note", in_reply_to: "owner-0123456789ab", result: false }], String(cls));
|
||||
}
|
||||
// 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));
|
||||
assert.deepEqual(sent, [{ body: "b", in_reply_to: null, result: false }], bad.slice(0, 24));
|
||||
}
|
||||
const refused = [
|
||||
["decision.raise", { kind: "hire", body: "x" }], ["event.emit", { kind: "action.allowed", body: {} }],
|
||||
@@ -222,6 +229,7 @@ test("fence 1: a harnessEnv name the host refuses is refused even when the harne
|
||||
"LOCPATH", "MALLOC_CHECK_", "OPENSSL_CONF", "SSL_CERT_FILE", "ZDOTDIR", "PROMPT_COMMAND", "JAVA_TOOL_OPTIONS", "_JAVA_OPTIONS", "NPM_CONFIG_PREFIX",
|
||||
"CLAUDE_CONFIG_DIR", "XDG_DATA_HOME", "ANY_CONF", "ANY_CONFIG", "PERLLIB", "LESSOPEN", "LESSCLOSE", "HOSTALIASES",
|
||||
"SSH_ASKPASS", "SUDO_ASKPASS", "CURL_CA_BUNDLE", "REQUESTS_CA_BUNDLE", "OPENAI_BASE_URL", "ANY_BASE_URL",
|
||||
"LUA_INIT", "CURL_HOME", "SSLKEYLOGFILE",
|
||||
]) assert.equal(refusedHarnessName(name), true, name);
|
||||
for (const name of ["FAKE_OK", "PI_MODEL", "TRIAGE_PROBE"]) assert.equal(refusedHarnessName(name), false, name);
|
||||
});
|
||||
@@ -635,6 +643,17 @@ test("a reply that names its message answers it: a session killed after replying
|
||||
assert.deepEqual((await h.state()).transcript.filter((e) => e.from === "host"), []);
|
||||
});
|
||||
|
||||
test("a progress note that names its message does not answer it: a session killed after the note leaves an unanswered line", { skip, timeout: 120000 }, async (t) => {
|
||||
const root = scratch();
|
||||
const { h } = host(t, root, { route: stubRoute(root) });
|
||||
const sent = await h.message("NOTE-SLEEP");
|
||||
const [noted] = await repliesOf(h, 1);
|
||||
assert.deepEqual([noted.text.replace(/\n\n.*$/s, ""), noted.in_reply_to], ["still looking", sent.id]);
|
||||
process.kill(-h.pid, "SIGKILL");
|
||||
const [note] = await repliesOf(h, 1, "host");
|
||||
assert.deepEqual([note.in_reply_to, note.text], [sent.id, UNANSWERED]);
|
||||
});
|
||||
|
||||
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) });
|
||||
|
||||
@@ -156,7 +156,7 @@ test("the snapshot file is one 0600 file, replaced whole", () => {
|
||||
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 () => {
|
||||
test("config checks run at most ORG_READS_AT_ONCE at a time and organizations are 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 }; };
|
||||
@@ -167,15 +167,19 @@ test("organizations are read at most ORG_READS_AT_ONCE at a time and listed in r
|
||||
assert.equal(snap.organizations.count, 10);
|
||||
});
|
||||
|
||||
// The timing tests below assert what was read, which no load changes, and a time bound far
|
||||
// above the one deadline they prove; the "begun late" test moves the deadline's clock by hand.
|
||||
test("the organization reads share one deadline: forty hung organizations take one timeout, not ten", async () => {
|
||||
let calls = 0;
|
||||
const calls = { busStatus: 0, processing: 0, orgConfig: 0 };
|
||||
const ids = Array.from({ length: 40 }, (_, i) => `org${i}`);
|
||||
const hang = () => { calls++; return new Promise(() => {}); };
|
||||
const hang = (name) => () => { calls[name]++; return new Promise(() => {}); };
|
||||
const began = Date.now();
|
||||
const snap = await composeSnapshot({ orgs: async () => ids.map((id) => ({ id, state: "active" })), busStatus: hang, processing: hang, orgConfig: hang }, { timeoutMs: 200 });
|
||||
const snap = await composeSnapshot({ orgs: async () => ids.map((id) => ({ id, state: "active" })), busStatus: hang("busStatus"), processing: hang("processing"), orgConfig: hang("orgConfig") }, { timeoutMs: 200 });
|
||||
const took = Date.now() - began;
|
||||
assert.ok(took < 800, `took ${took} ms`);
|
||||
assert.ok(calls < ids.length, `${calls} reads began`);
|
||||
// Ten timeouts would be 2000 ms; one is 200.
|
||||
assert.ok(took < 1500, `took ${took} ms`);
|
||||
// The config checks behind the first four hang to the deadline, and no later one begins.
|
||||
assert.ok(calls.orgConfig < ids.length, `${calls.orgConfig} config checks began`);
|
||||
assert.equal(snap.organizations.count, 40);
|
||||
assert.deepEqual(snap.organizations.list.map((o) => o.id), ids);
|
||||
for (const o of snap.organizations.list) assert.deepEqual([o.bus, o.processing, o.config], [NOT_READ, NOT_READ, NOT_READ], o.id);
|
||||
@@ -184,11 +188,35 @@ test("the organization reads share one deadline: forty hung organizations take o
|
||||
test("an organization begun late gets only what is left of the deadline", async () => {
|
||||
const ids = ["a0", "a1", "a2", "a3", "a4"];
|
||||
const hang = () => new Promise(() => {});
|
||||
// Three readers hang; a3 answers after 300 ms, and the worker that read it starts a4 with 100 ms left.
|
||||
const orgConfig = (id) => (id === "a3" ? new Promise((r) => setTimeout(() => r({ valid: true, exit: 0, code: null }), 300)) : hang());
|
||||
// The deadline's clock moves only by hand. Three readers hang; a3 answers at once and moves the
|
||||
// clock 300 ms, so the worker that read it starts a4 with 100 ms left. a4 answers after 250 ms:
|
||||
// inside a whole timeout, outside what is left.
|
||||
let t = 0;
|
||||
const answer = { valid: true, exit: 0, code: null };
|
||||
const orgConfig = (id) => {
|
||||
if (id === "a3") { t += 300; return answer; }
|
||||
if (id === "a4") return new Promise((r) => setTimeout(() => r(answer), 250));
|
||||
return hang();
|
||||
};
|
||||
const began = Date.now();
|
||||
const snap = await composeSnapshot({ orgs: async () => ids.map((id) => ({ id, state: "active" })), orgConfig }, { timeoutMs: 400 });
|
||||
const snap = await composeSnapshot({ orgs: async () => ids.map((id) => ({ id, state: "active" })), orgConfig }, { timeoutMs: 400, clock: () => t });
|
||||
const took = Date.now() - began;
|
||||
assert.ok(took < 600, `took ${took} ms`);
|
||||
assert.ok(took < 1500, `took ${took} ms`);
|
||||
assert.deepEqual(snap.organizations.list.map((o) => [o.id, o.config === NOT_READ ? "not read" : "read"]), [["a0", "not read"], ["a1", "not read"], ["a2", "not read"], ["a3", "read"], ["a4", "not read"]]);
|
||||
});
|
||||
|
||||
test("the bus and processing reads are not held behind the config checks: every organization's sockets are read", async () => {
|
||||
const ids = Array.from({ length: 10 }, (_, i) => `org${i}`);
|
||||
const hang = () => new Promise(() => {});
|
||||
// The first four config checks hang to the deadline and hold every config slot; the socket reads answer at once.
|
||||
const orgConfig = (id) => (ids.indexOf(id) < ORG_READS_AT_ONCE ? hang() : { valid: true, exit: 0, code: null });
|
||||
const snap = await composeSnapshot({
|
||||
orgs: async () => ids.map((id) => ({ id, state: "active" })),
|
||||
busStatus: async () => ({ healthy: true, pid: 1 }),
|
||||
processing: async () => ({ halted: false, invalid: false }),
|
||||
orgConfig,
|
||||
}, { timeoutMs: 300 });
|
||||
assert.deepEqual(snap.organizations.list.map((o) => o.id), ids);
|
||||
for (const o of snap.organizations.list) assert.deepEqual([o.bus.state, o.processing], ["running", { halted: false, invalid: false }], o.id);
|
||||
for (const o of snap.organizations.list.slice(0, ORG_READS_AT_ONCE)) assert.equal(o.config, NOT_READ, o.id);
|
||||
});
|
||||
@@ -114,13 +114,25 @@ lists.
|
||||
- The extension's per-reply dedupe is in memory. After a restart the same
|
||||
reply can get a second notice.
|
||||
- Claude Code can run the Stop hook before it writes the turn to the
|
||||
transcript (seen on 2.1.319 to 2.1.325, on any turn). The hook reads the
|
||||
transcript again until its last reply matches `last_assistant_message`,
|
||||
for up to 2 s. If it never does, the hook checks `last_assistant_message`
|
||||
transcript (seen on 2.1.319 to 2.1.325, on any turn). The Stop hook keeps,
|
||||
per session id, the uuid of the last prompt whose turn it checked, in
|
||||
`<log>.stop.json` next to its log. A transcript is stale while its last
|
||||
prompt has that uuid, or while its last reply does not match
|
||||
`last_assistant_message`. The hook reads it again until it is not, for up
|
||||
to 2 s. If it never catches up, the hook checks `last_assistant_message`
|
||||
alone, logs an empty `entryId` and `late: true`, and the turn's earlier
|
||||
replies go unchecked. Two stale cases remain: a hook input with no
|
||||
`last_assistant_message`, and a reply whose text equals the last turn's
|
||||
last reply. Either can log the last turn's replies again.
|
||||
replies go unchecked; with no `last_assistant_message` it checks nothing.
|
||||
Two cases are left. First, a late fallback records no prompt, because
|
||||
the hook never saw the turn. When that turn lands in the transcript, a
|
||||
later Stop can take it for its own: one with no
|
||||
`last_assistant_message`, or one whose reply has the same text as the
|
||||
late turn's last reply. That Stop checks the late turn's replies again.
|
||||
Second, a Stop hook that blocks makes Claude go on with no new prompt,
|
||||
so a second Stop in that turn finds the transcript stale by its prompt.
|
||||
It waits 2 s and checks `last_assistant_message` alone, late. Managed
|
||||
sessions do not reach this case: their only Stop hook never blocks. The
|
||||
state file sits in the run directory with the log, so it is the
|
||||
session's own record too.
|
||||
- The word/phrase lists are the mechanical subset of ms-unslop only, keyed to
|
||||
pattern ids in lists.json. Style judgments (voice, rhythm, structure) stay in
|
||||
the skill, not the gate.
|
||||
|
||||
@@ -77,38 +77,77 @@ function check(text, at) {
|
||||
}
|
||||
}
|
||||
|
||||
// The assistant messages with text since the transcript's last prompt.
|
||||
function turnReplies(path) {
|
||||
// The transcript's last prompt (its uuid, or null) and the assistant messages with text since it.
|
||||
function lastTurn(path) {
|
||||
const list = entries(path);
|
||||
let start = -1;
|
||||
list.forEach((e, i) => { if (isPrompt(e)) start = i; });
|
||||
return replies(list.slice(start + 1));
|
||||
const uuid = start >= 0 && typeof list[start].uuid === "string" && list[start].uuid ? list[start].uuid : null;
|
||||
return { prompt: uuid, replies: replies(list.slice(start + 1)) };
|
||||
}
|
||||
|
||||
// The Stop hook's own state, next to its log: per session id, the uuid of the last prompt whose
|
||||
// turn it checked. At most STATE_MAX sessions are kept, the latest last.
|
||||
const STATE = LOG ? `${LOG}.stop.json` : null;
|
||||
const STATE_MAX = 64;
|
||||
function stopState() {
|
||||
if (!STATE) return {};
|
||||
try {
|
||||
const value = JSON.parse(fs.readFileSync(STATE, "utf8"));
|
||||
return value !== null && typeof value === "object" && !Array.isArray(value) ? value : {};
|
||||
} catch { return {}; }
|
||||
}
|
||||
function recordStop(session, prompt) {
|
||||
if (!STATE) return;
|
||||
const state = stopState();
|
||||
delete state[session];
|
||||
state[session] = prompt;
|
||||
const keys = Object.keys(state);
|
||||
for (const k of keys.slice(0, Math.max(0, keys.length - STATE_MAX))) delete state[k];
|
||||
const tmp = `${STATE}.${process.pid}`;
|
||||
try {
|
||||
fs.writeFileSync(tmp, JSON.stringify(state) + "\n");
|
||||
fs.renameSync(tmp, STATE);
|
||||
} catch {
|
||||
try { fs.rmSync(tmp, { force: true }); } catch {}
|
||||
}
|
||||
}
|
||||
|
||||
// Claude Code can run the Stop hook before it writes the turn to the transcript, so the
|
||||
// transcript may still end with the last turn. Read it again until its last reply is the one
|
||||
// the hook input names, for up to STOP_WAIT_MS (the hook's own limit is 10 s). A transcript that
|
||||
// never catches up gives last_assistant_message alone, logged with late: true so a release where
|
||||
// it never matches shows in the log.
|
||||
// Two stale cases remain: an input with no last_assistant_message is not waited for, and a reply
|
||||
// whose text equals the last turn's last reply matches a stale transcript at once. Either can log
|
||||
// the last turn's replies again.
|
||||
// transcript may still end with the last turn. It is stale while its last prompt is the one the
|
||||
// hook last checked for this session, or while its last reply is not the one the hook input
|
||||
// names in last_assistant_message. Read it again until it is not, for up to STOP_WAIT_MS (the
|
||||
// hook's own limit is 10 s). A transcript that never catches up gives last_assistant_message
|
||||
// alone, logged with late: true so a release where it never matches shows in the log; with no
|
||||
// last_assistant_message there is no reply to check, and nothing is logged.
|
||||
// Two cases are left. A late fallback records no prompt uuid, since the hook never saw the turn.
|
||||
// Once that turn lands, a later Stop with no last_assistant_message, or with a reply whose text
|
||||
// equals the late turn's last reply, takes it for its own and logs its replies again. And a Stop
|
||||
// hook that blocks makes Claude go on with no new prompt, so a second Stop in that turn finds the
|
||||
// transcript stale by the uuid rule, waits, and logs only last_assistant_message, late. Managed
|
||||
// sessions never reach the second case: their only Stop hook never blocks.
|
||||
const STOP_WAIT_MS = 2000;
|
||||
const flat = (s) => s.replace(/\s+/g, "");
|
||||
const pause = (ms) => Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
|
||||
|
||||
function stop(input) {
|
||||
const told = typeof input.last_assistant_message === "string" && input.last_assistant_message.trim() ? input.last_assistant_message : null;
|
||||
const session = typeof input.session_id === "string" ? input.session_id : "";
|
||||
const state = stopState();
|
||||
const seen = Object.hasOwn(state, session) && typeof state[session] === "string" ? state[session] : null;
|
||||
const stale = (t) => (t.prompt !== null && t.prompt === seen) || (told !== null && !(t.replies.length && flat(t.replies.at(-1).text) === flat(told)));
|
||||
const deadline = Date.now() + STOP_WAIT_MS;
|
||||
let turn = turnReplies(input.transcript_path), late = false;
|
||||
while (told !== null && !(turn.length && flat(turn.at(-1).text) === flat(told))) {
|
||||
let t = lastTurn(input.transcript_path);
|
||||
let turn = t.replies, late = false;
|
||||
while (stale(t)) {
|
||||
if (Date.now() >= deadline) {
|
||||
turn = [{ id: "", text: told }];
|
||||
turn = told === null ? [] : [{ id: "", text: told }];
|
||||
late = true;
|
||||
break;
|
||||
}
|
||||
pause(25);
|
||||
turn = turnReplies(input.transcript_path);
|
||||
t = lastTurn(input.transcript_path);
|
||||
turn = t.replies;
|
||||
}
|
||||
for (const r of turn) {
|
||||
const result = check(r.text, "stop");
|
||||
@@ -117,6 +156,7 @@ function stop(input) {
|
||||
if (result.clean) log({ ev: "checked", clean: true, entryId: r.id, charsChecked: result.charsChecked, ...extra });
|
||||
else log({ ev: "flagged", clean: false, entryId: r.id, charsChecked: result.charsChecked, findings: result.findings, ...extra });
|
||||
}
|
||||
if (!late && t.prompt !== null) recordStop(session, t.prompt);
|
||||
}
|
||||
|
||||
// The entry ids the log already holds a notice for.
|
||||
|
||||
@@ -340,6 +340,54 @@ t("claude-hook: Stop with a transcript that never catches up checks last_assista
|
||||
assert.deepStrictEqual(events(now).map((e) => [e.ev, e.entryId, e.late]), [["checked", "a1", undefined]]);
|
||||
});
|
||||
|
||||
// The Stop hook keeps, per session and next to its log, the last prompt whose turn it checked. A
|
||||
// transcript that still ends with that prompt is stale, even with no last_assistant_message or
|
||||
// with a reply whose text equals the last turn's.
|
||||
const appendLater = (from, to) => require("node:child_process").spawn("sh", ["-c", 'sleep 0.3; cat "$1" >> "$2"', "sh", from, to], { stdio: "ignore" });
|
||||
|
||||
t("claude-hook: Stop with no last_assistant_message does not check a turn it checked again, and waits for the late tail", () => {
|
||||
const transcript = writeTmp("transcript-untold.jsonl", jsonl(lastTurn));
|
||||
const log = tmp("hook-untold.jsonl");
|
||||
const env = { MOSAIC_UNSLOP_LOG: log };
|
||||
assert.strictEqual(hook("stop", { session_id: "s1", transcript_path: transcript }, env).status, 0);
|
||||
assert.deepStrictEqual(events(log).map((e) => [e.ev, e.entryId]), [["checked", "a1"]]);
|
||||
// The next turn's Stop runs before its turn reaches the transcript.
|
||||
const writer = appendLater(writeTmp("transcript-untold-tail.jsonl", jsonl([
|
||||
{ type: "user", uuid: "p2", message: { role: "user", content: "and now?" } },
|
||||
line("assistant", "m2", "a2", [{ type: "text", text: TELL_REPLY }]),
|
||||
])), transcript);
|
||||
const r = hook("stop", { session_id: "s1", transcript_path: transcript }, env);
|
||||
writer.kill();
|
||||
assert.strictEqual(r.status, 0, r.stderr);
|
||||
assert.deepStrictEqual(events(log).map((e) => [e.ev, e.entryId]), [["checked", "a1"], ["flagged", "a2"]]);
|
||||
// A transcript that never catches up gives nothing to check: nothing is logged.
|
||||
assert.strictEqual(hook("stop", { session_id: "s1", transcript_path: transcript }, env).status, 0);
|
||||
assert.strictEqual(events(log).length, 2);
|
||||
// The state is per session: another session on the same log has checked nothing yet.
|
||||
hook("stop", { session_id: "s2", transcript_path: transcript }, env);
|
||||
assert.deepStrictEqual(events(log).slice(2).map((e) => [e.ev, e.entryId]), [["flagged", "a2"]]);
|
||||
});
|
||||
|
||||
t("claude-hook: Stop whose reply has the last turn's text does not take the stale transcript for this turn", () => {
|
||||
const transcript = writeTmp("transcript-same.jsonl", jsonl(lastTurn));
|
||||
const log = tmp("hook-same.jsonl");
|
||||
const env = { MOSAIC_UNSLOP_LOG: log };
|
||||
const told = { session_id: "s1", transcript_path: transcript, last_assistant_message: "The build is green." };
|
||||
assert.strictEqual(hook("stop", told, env).status, 0);
|
||||
assert.deepStrictEqual(events(log).map((e) => [e.ev, e.entryId]), [["checked", "a1"]]);
|
||||
const writer = appendLater(writeTmp("transcript-same-tail.jsonl", jsonl([
|
||||
{ type: "user", uuid: "p2", message: { role: "user", content: "still?" } },
|
||||
line("assistant", "m2", "a2", [{ type: "text", text: "The build is green." }]),
|
||||
])), transcript);
|
||||
const r = hook("stop", told, env);
|
||||
writer.kill();
|
||||
assert.strictEqual(r.status, 0, r.stderr);
|
||||
assert.deepStrictEqual(events(log).map((e) => [e.ev, e.entryId]), [["checked", "a1"], ["checked", "a2"]]);
|
||||
// A transcript that never catches up gives last_assistant_message alone, late.
|
||||
hook("stop", told, env);
|
||||
assert.deepStrictEqual(events(log).slice(2).map((e) => [e.ev, e.entryId, e.late]), [["checked", "", true]]);
|
||||
});
|
||||
|
||||
let failed = 0;
|
||||
for (const [name, ok] of results) { console.log(`${ok ? "PASS" : "FAIL"} ${name}`); if (!ok) failed++; }
|
||||
console.log(`${results.length - failed}/${results.length} passed`);
|
||||
|
||||
Reference in new issue
Block a user