mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-26 10:53:56 +00:00
* feat(capabilities): add terminal_execute_bulk to run one command on many sessions
Add a terminal_execute_bulk MCP tool that runs the same short command on
2-20 open terminal sessions and groups identical results.
- All-or-nothing preflight: scope, closing, busy, connected, transport
and the per-shell command safety policy are checked for every target
before anything is sent. Any bad target cancels the whole call and all
bad targets are reported at once.
- One approval listing every host. Remembered grants and external-client
session approval never satisfy it (policy.requiresFreshApproval), and a
pending fan-out is not swept up by another request's session approval.
- Targets are claimed before the prompt and re-checked after it; all
targets then start in the same synchronous turn under a single per-chat
exec lock, each with its own wall-clock timeout.
- Results are grouped only when status, exit code, stdout, stderr and
error are identical on the full output, then truncated (tailLines and
size caps). JSON output matches the other tools.
- Loaded on demand through the terminal_bulk toolset.
* fix(capabilities): harden terminal_execute_bulk after review
- Only trust the metadata "connected" flag for worker-hosted sessions; a
live main-process session with a stale snapshot is no longer refused.
- Keep targets protected from idle auto-close from the claim through the
approval wait to completion, and release on every path.
- Classify blocklist refusals before status keywords so a pattern such as
"cancel-job" is reported as not_sent, not cancelled.
- List the terminal_bulk toolset in the MCP server instructions and the
netcatty-mcp skill so clients can discover it.
- Test that the bulk RPC gets the same timeout budget as terminal_execute.
* fix(capabilities): validate worker sessions up front and cancel them at the deadline
An independent review found that the default (terminal worker) sessions
could receive a partial send, and that the backstop deadline never
reached the worker.
- Add a no-send precheck mode to the worker exec handler. The bulk
preflight now runs every deterministic check for worker sessions
(transport, shell kind, shell-specific safety patterns, unsupported
local shell, busy job) so a refusal cancels the whole call before any
host receives the command.
- Add netcatty:ai:exec:cancel and call it at the deadline, so a stuck
worker command is cancelled before the per-chat lock is released
instead of overlapping the next command. Background jobs are left
alone.
- Make the deadline grace 3s and give the bulk RPC extra timeout budget
for its preflight, so the backstop fires before the client gives up.
- Do not group hosts whose exec layer already dropped different amounts
of output, and share one size budget between stdout and stderr.
- Restrict the capability to the sidebar agent; the global agent runs
delegated, which bulk refuses.
- Document that live terminal readiness is a send-time state.
* test(capabilities): cover terminal_execute_bulk over the real MCP wire and close mutation gaps
- Drive the tool through the real stdio MCP server: hidden until the
terminal_bulk toolset is loaded, schema shape, JSON result, Error text
with isError, schema rejection of a native array, and privacy masking
of the grouped result with token restoration in the command.
- Mutation testing (18 deliberate breakages of the safety-critical
lines) left three survivors; add tests that kill two of them: hosts
differing only in exit code, status, stderr or error never share a
group, and a session-scoped answer to a fan-out approval does not
approve later writes from that client.
- Assert that delegated agents cannot reach the fan-out at the gate.
* fix(capabilities): address review findings on terminal_execute_bulk
- Keep the tool MCP-only for now. The in-app agent has no completed-write
replay for it after a context-compaction retry, and its SDK approval
gate would stack a second prompt on the bridge's fresh approval. Both
can follow in a separate change.
- Re-run the transport preflight for every claimed target after the
approval and before the synchronous start, so a worker session that
changed while the user was deciding cancels the whole call instead of
being refused after other hosts have started.
- Never group hosts whose exec layer already dropped part of the output:
equal retained tails do not prove equal full results.
* fix(capabilities): only report not_sent on explicit evidence and flag capped output
- Classify a host as not_sent only when the exec layer says so (a known
pre-send code or refusal text). An error with no output and no exit
code, such as a terminal worker exiting after it typed the command,
is now reported as error, so a mutating command is never presented as
safe to retry blindly. SHELL_NOT_READY and EXECUTION_NOT_AUTHORIZED
are now recognised, so a call where every host refuses fails as
not sent instead of returning ok.
- Have execViaRawPty and execViaChannel set outputTruncated when they
stop collecting at their size cap, so hosts that differ only in the
discarded output are never grouped.
* test(capabilities): run terminal_execute_bulk against the real worker exec handler
Wire the bridge to the production terminal-worker exec handler with fake
shells that speak the wrapper protocol, and cover the grouped result,
Stop while every host is running, and real per-host timeouts.
* fix(capabilities): do not probe hosts before the bulk approval
In confirm mode the preflight ran the shell-kind probe, which opens an
SSH exec channel and runs a remote command, before the approval prompt.
Denying the call (or another target failing) still executed something on
the host, and on an account with a forced command that could trigger it.
- The preflight before the prompt is now read-only: no channel is opened,
and the safety check uses the shell kind only if it is already known,
otherwise the shell-independent patterns.
- The full check, including the probe and the shell-aware patterns, runs
right after approval and before any command is sent, so the call is
still all-or-nothing. Without a prompt the full check runs first.
- The worker precheck honours the same distinction.
* fix(capabilities): recheck local guards before probing hosts after approval
After the approval the bulk call ran the probing transport pass first and
rechecked cancellation, scope and the privacy policy afterwards. A call
that was no longer authorized (Stop pressed, session out of scope,
privacy settings changed while the prompt was open) was rejected only
after the hosts had already been probed.
Run the local, read-only guards first, then the probing pass, then the
guards again because the pass awaits, and only then take the chat lock
and start. A refusal by the privacy hook now happens before the lock is
taken.
* fix(capabilities): re-read the permission mode and classify exec refusals precisely
- Re-evaluate the live permission mode with the other local guards, both
before the probing pass and again before the start. Switching to
observer while a prompt is open (or while hosts are being checked) now
refuses the call, and a call that needed no approval is refused if the
mode turns into confirm before it starts.
- Keep the code of an error the exec layer throws, so SHELL_PROMPT_PENDING
(thrown before anything is written) is reported as not_sent and an
all-refused call fails as not sent.
- Recognise the executors' real closed-stream messages ("Stream closed
unexpectedly", "Serial port closed unexpectedly") as connection_lost.
787 lines
39 KiB
JavaScript
787 lines
39 KiB
JavaScript
"use strict";
|
|
|
|
const assert = require("node:assert/strict");
|
|
const test = require("node:test");
|
|
|
|
const {
|
|
BULK_RPC_METHOD,
|
|
CHAT_LOCK_HELD,
|
|
DEADLINE_GRACE_MS,
|
|
MAX_BULK_TARGETS,
|
|
MIN_BULK_TARGETS,
|
|
classifyHostResult,
|
|
createExecBulkHandler,
|
|
formatPreflightFailure,
|
|
groupBulkResults,
|
|
parseBulkExecParams,
|
|
tailText,
|
|
} = require("./execBulk.cjs");
|
|
|
|
const nextTick = () => new Promise((resolve) => setImmediate(resolve));
|
|
|
|
function deferred() {
|
|
let resolve;
|
|
const promise = new Promise((res) => { resolve = res; });
|
|
return { promise, resolve };
|
|
}
|
|
|
|
// ── parseBulkExecParams ──
|
|
|
|
test("parseBulkExecParams accepts a JSON string or an array and applies defaults", () => {
|
|
const fromString = parseBulkExecParams({ command: "uptime", sessionIds: '["a","b"]' });
|
|
assert.deepEqual(fromString, { ok: true, command: "uptime", sessionIds: ["a", "b"], tailLines: 100 });
|
|
const fromArray = parseBulkExecParams({ command: "uptime", sessionIds: [" a ", "b"], tailLines: 5 });
|
|
assert.deepEqual(fromArray, { ok: true, command: "uptime", sessionIds: ["a", "b"], tailLines: 5 });
|
|
});
|
|
|
|
test("parseBulkExecParams rejects malformed input without touching any session", () => {
|
|
const bad = [
|
|
[{ command: "", sessionIds: '["a","b"]' }, /Invalid command/],
|
|
[{ command: " ", sessionIds: '["a","b"]' }, /Invalid command/],
|
|
[{ command: "ls", sessionIds: "not json" }, /JSON array/],
|
|
[{ command: "ls", sessionIds: "{}" }, /JSON array/],
|
|
[{ command: "ls", sessionIds: "[]" }, /JSON array of 2-20/],
|
|
[{ command: "ls", sessionIds: '["a"]' }, /at least|Use terminal_execute/],
|
|
[{ command: "ls", sessionIds: '["a", 1]' }, /JSON array/],
|
|
[{ command: "ls", sessionIds: '["a", ""]' }, /JSON array/],
|
|
[{ command: "ls", sessionIds: JSON.stringify(Array.from({ length: MAX_BULK_TARGETS + 1 }, (_, i) => `s${i}`)) }, /JSON array/],
|
|
[{ command: "ls", sessionIds: '["a","b","a"]' }, /unique.*a/],
|
|
[{ command: "ls", sessionIds: '["a","b"]', tailLines: -1 }, /tailLines/],
|
|
[{ command: "ls", sessionIds: '["a","b"]', tailLines: "abc" }, /tailLines/],
|
|
];
|
|
for (const [params, pattern] of bad) {
|
|
const result = parseBulkExecParams(params);
|
|
assert.equal(result.ok, false, JSON.stringify(params));
|
|
assert.match(result.error, pattern);
|
|
}
|
|
});
|
|
|
|
test("parseBulkExecParams treats tailLines 0 as default and caps large values", () => {
|
|
assert.equal(parseBulkExecParams({ command: "ls", sessionIds: '["a","b"]', tailLines: 0 }).tailLines, 100);
|
|
assert.equal(parseBulkExecParams({ command: "ls", sessionIds: '["a","b"]', tailLines: 99999 }).tailLines, 2000);
|
|
assert.equal(parseBulkExecParams({ command: "ls", sessionIds: '["a","b"]', tailLines: 7.9 }).tailLines, 7);
|
|
});
|
|
|
|
// ── formatting ──
|
|
|
|
test("formatPreflightFailure lists every failing target and carries no Error prefix", () => {
|
|
const text = formatPreflightFailure([
|
|
{ sessionId: "s-9", label: "", error: 'Session "s-9" is not in the current scope.' },
|
|
{ sessionId: "s-3", label: "web3", error: "Session already has exec in progress." },
|
|
], 5);
|
|
assert.equal(text, [
|
|
"nothing was sent. 2 of 5 targets cannot run this command:",
|
|
'- s-9: Session "s-9" is not in the current scope.',
|
|
"- s-3 (web3): Session already has exec in progress.",
|
|
"Remove or fix these session ids and retry.",
|
|
].join("\n"));
|
|
});
|
|
|
|
test("classifyHostResult reports what happened without judging the exit code", () => {
|
|
assert.deepEqual(classifyHostResult({ ok: true, stdout: "a", stderr: "", exitCode: 0 }), {
|
|
status: "completed", exitCode: 0, stdout: "a", stderr: "", error: "", sourceTruncated: false,
|
|
});
|
|
assert.equal(classifyHostResult({ ok: false, stdout: "inactive", exitCode: 3 }).status, "completed");
|
|
assert.equal(classifyHostResult({ ok: false, stdout: "inactive", exitCode: 3 }).exitCode, 3);
|
|
// Raw serial / network-device runs have no exit code.
|
|
assert.deepEqual(
|
|
[classifyHostResult({ ok: true, stdout: "x", exitCode: null }).status, classifyHostResult({ ok: true, stdout: "x", exitCode: null }).exitCode],
|
|
["completed", null],
|
|
);
|
|
const timeout = classifyHostResult({ ok: false, stdout: "p", exitCode: -1, error: "Command timed out (60s)" });
|
|
assert.equal(timeout.status, "timeout");
|
|
assert.equal(timeout.exitCode, null, "the -1 sentinel never leaves the handler");
|
|
assert.equal(classifyHostResult({ ok: false, exitCode: -1, error: "Cancelled" }).status, "cancelled");
|
|
assert.equal(
|
|
classifyHostResult({ ok: false, exitCode: -1, error: "Command terminated unexpectedly (connection lost or signal)" }).status,
|
|
"connection_lost",
|
|
);
|
|
assert.equal(
|
|
classifyHostResult({ ok: false, error: "Command blocked by safety policy. Pattern: rm -rf" }).status,
|
|
"not_sent",
|
|
);
|
|
assert.equal(
|
|
classifyHostResult({ ok: false, code: "SHELL_READINESS_UNKNOWN", error: "no command was sent" }).status,
|
|
"not_sent",
|
|
);
|
|
assert.equal(classifyHostResult({ ok: false, stdout: "partial", exitCode: 1, error: "boom" }).status, "error");
|
|
assert.equal(classifyHostResult(null).status, "error");
|
|
});
|
|
|
|
test("tailText keeps the last lines and the last characters", () => {
|
|
assert.deepEqual(tailText("", 5, 100), { text: "", omittedLines: 0, truncated: false });
|
|
assert.deepEqual(tailText("a\nb\nc\n", 5, 100), { text: "a\nb\nc\n", omittedLines: 0, truncated: false });
|
|
assert.deepEqual(tailText("1\n2\n3\n4\n5\n", 2, 100), { text: "4\n5\n", omittedLines: 3, truncated: true });
|
|
const long = Array.from({ length: 50 }, (_, i) => `line-${i}`).join("\n");
|
|
const capped = tailText(long, 1000, 40);
|
|
assert.equal(capped.truncated, true);
|
|
assert.ok(capped.text.length <= 40);
|
|
assert.ok(capped.text.startsWith("line-"), "cut starts on a line boundary");
|
|
assert.ok(capped.omittedLines > 0);
|
|
const oneHugeLine = tailText("x".repeat(100), 10, 30);
|
|
assert.equal(oneHugeLine.truncated, true);
|
|
assert.equal(oneHugeLine.text.length, 30);
|
|
});
|
|
|
|
// ── groupBulkResults ──
|
|
|
|
test("groupBulkResults groups identical results on full raw output and keeps request order", () => {
|
|
const entries = [
|
|
{ sessionId: "s1", label: "web1", result: { ok: true, stdout: "active", exitCode: 0 } },
|
|
{ sessionId: "s2", label: "web2", result: { ok: false, stdout: "inactive", exitCode: 3 } },
|
|
{ sessionId: "s3", label: "web3", result: { ok: true, stdout: "active", exitCode: 0 } },
|
|
{ sessionId: "s4", label: "db1", result: { ok: false, stdout: "", stderr: "no route", exitCode: -1, error: "Command timed out (60s)" } },
|
|
];
|
|
const payload = groupBulkResults(entries, { tailLines: 100 });
|
|
assert.equal(payload.ok, true);
|
|
assert.equal(payload.hostCount, 4);
|
|
assert.deepEqual(payload.counts, { completed: 3, timeout: 1 });
|
|
assert.deepEqual(payload.groups, [
|
|
{
|
|
hosts: [{ sessionId: "s1", label: "web1" }, { sessionId: "s3", label: "web3" }],
|
|
status: "completed", exitCode: 0, stdout: "active",
|
|
},
|
|
{ hosts: [{ sessionId: "s2", label: "web2" }], status: "completed", exitCode: 3, stdout: "inactive" },
|
|
{
|
|
hosts: [{ sessionId: "s4", label: "db1" }],
|
|
status: "timeout", exitCode: null, stdout: "", stderr: "no route", error: "Command timed out (60s)",
|
|
},
|
|
]);
|
|
});
|
|
|
|
test("groupBulkResults never merges hosts that only differ in the truncated part", () => {
|
|
const head = (tag) => `${tag}\n${Array.from({ length: 10 }, (_, i) => `same-${i}`).join("\n")}`;
|
|
const payload = groupBulkResults([
|
|
{ sessionId: "s1", label: "a", result: { ok: true, stdout: head("first"), exitCode: 0 } },
|
|
{ sessionId: "s2", label: "b", result: { ok: true, stdout: head("second"), exitCode: 0 } },
|
|
], { tailLines: 3 });
|
|
assert.equal(payload.groups.length, 2);
|
|
for (const group of payload.groups) {
|
|
assert.equal(group.outputTruncated, true);
|
|
assert.equal(group.omittedLines, 8);
|
|
assert.equal(group.stdout, "same-7\nsame-8\nsame-9");
|
|
}
|
|
});
|
|
|
|
test("groupBulkResults omits empty stderr/error and truncation fields", () => {
|
|
const [group] = groupBulkResults([{ sessionId: "s1", label: "", result: { ok: true, stdout: "", exitCode: 0 } }]).groups;
|
|
assert.deepEqual(Object.keys(group).sort(), ["exitCode", "hosts", "status", "stdout"]);
|
|
assert.equal(group.stdout, "");
|
|
});
|
|
|
|
// ── handler ──
|
|
|
|
function createHarness(options = {}) {
|
|
const ids = options.ids || ["a", "b", "c"];
|
|
const inScope = new Set(options.inScope || ids);
|
|
const labels = options.labels || { a: "web1", b: "web2", c: "web3" };
|
|
const mainSessions = new Map((options.mainIds || ids).map((id) => [id, { label: labels[id] }]));
|
|
const busy = new Map();
|
|
const closing = new Map();
|
|
const pending = new Map();
|
|
const state = {
|
|
chatLock: null,
|
|
cancelled: false,
|
|
events: [],
|
|
execCalls: [],
|
|
approvals: [],
|
|
activity: { begun: [], ended: [] },
|
|
cancelCalls: [],
|
|
};
|
|
const transportErrors = options.transportErrors || {};
|
|
const deps = {
|
|
getSessions: () => mainSessions,
|
|
getSessionMeta: (id) => (options.noMeta?.includes(id) ? null : { label: labels[id] || "", connected: options.disconnected?.includes(id) ? false : true }),
|
|
validateSessionScope: (id) => (inScope.has(id) ? null : `Session "${id}" is not in the current scope.`),
|
|
closingTerminalSessions: closing,
|
|
getSessionBusyError: (id) => (busy.has(id) ? { ok: false, error: busy.get(id) } : null),
|
|
pendingSessionWriteApprovals: pending,
|
|
getActiveChatExecution: () => state.chatLock,
|
|
beginChatExecution: (chatSessionId, sessionId, command) => {
|
|
state.events.push(`lock:${sessionId}`);
|
|
if (options.lockBusy) return { ok: false, active: { command: "other", sessionId: "x" } };
|
|
state.chatLock = { sessionId, command };
|
|
return { ok: true, release: () => { state.events.push("unlock"); state.chatLock = null; } };
|
|
},
|
|
isChatSessionCancelled: () => state.cancelled,
|
|
preflightMainExec: async ({ sessionId }) => (transportErrors[sessionId] ? { ok: false, error: transportErrors[sessionId] } : { ok: true }),
|
|
preflightWorkerExec: ({ sessionId }) => (transportErrors[sessionId] ? { ok: false, error: transportErrors[sessionId] } : { ok: true }),
|
|
execOnSession: (params, executionContext) => {
|
|
state.execCalls.push({ params, executionContext });
|
|
if (options.exec) return options.exec(params, executionContext, state);
|
|
return Promise.resolve({ ok: true, stdout: `out-${params.sessionId}`, exitCode: 0 });
|
|
},
|
|
requestApproval: async (...args) => {
|
|
state.approvals.push(args);
|
|
state.events.push("approval");
|
|
if (options.onApproval) await options.onApproval({ busy, closing, state, pending, inScope });
|
|
return options.approve !== false;
|
|
},
|
|
sessionIdleManager: {
|
|
beginActivity: (chat, id) => { state.activity.begun.push(id); return true; },
|
|
endActivity: (chat, id) => { state.activity.ended.push(id); return true; },
|
|
},
|
|
getCommandTimeoutMs: () => 60000,
|
|
cancelExecsForChat: (chatSessionId) => state.cancelCalls.push(chatSessionId),
|
|
USER_DENIED_MESSAGE: "Operation denied by user.",
|
|
CHAT_SESSION_CANCELLED_MESSAGE: "Operation cancelled: the SDK agent session was stopped.",
|
|
setTimeoutFn: options.setTimeoutFn,
|
|
clearTimeoutFn: options.clearTimeoutFn,
|
|
};
|
|
Object.keys(deps).forEach((key) => deps[key] === undefined && delete deps[key]);
|
|
const handler = createExecBulkHandler(deps);
|
|
const baseParams = { command: "uptime", sessionIds: JSON.stringify(ids), chatSessionId: "chat-1" };
|
|
return { handler, state, busy, closing, pending, baseParams, deps };
|
|
}
|
|
|
|
test("run fans out to every target in the same turn and holds the chat lock once", async () => {
|
|
const gates = new Map();
|
|
const harness = createHarness({
|
|
exec: (params) => {
|
|
const gate = deferred();
|
|
gates.set(params.sessionId, gate);
|
|
return gate.promise;
|
|
},
|
|
});
|
|
const running = harness.handler.run(harness.baseParams, {}, { requiresApproval: false });
|
|
await nextTick();
|
|
// Every target started before any of them finished.
|
|
assert.deepEqual(harness.state.execCalls.map((call) => call.params.sessionId), ["a", "b", "c"]);
|
|
assert.equal(harness.state.events.filter((event) => event.startsWith("lock:")).length, 1);
|
|
assert.equal(harness.state.events.includes("unlock"), false, "lock is held while hosts run");
|
|
for (const call of harness.state.execCalls) {
|
|
assert.equal(call.executionContext[CHAT_LOCK_HELD], true);
|
|
assert.equal(call.params.chatSessionId, "chat-1");
|
|
}
|
|
gates.get("a").resolve({ ok: true, stdout: "same", exitCode: 0 });
|
|
gates.get("b").resolve({ ok: true, stdout: "same", exitCode: 0 });
|
|
gates.get("c").resolve({ ok: false, stdout: "different", exitCode: 2 });
|
|
const payload = await running;
|
|
assert.equal(payload.ok, true);
|
|
assert.equal(payload.hostCount, 3);
|
|
assert.equal(payload.groups.length, 2);
|
|
assert.deepEqual(payload.groups[0].hosts.map((host) => host.sessionId), ["a", "b"]);
|
|
assert.equal(harness.state.events.at(-1), "unlock");
|
|
assert.deepEqual(harness.state.activity.begun, ["a", "b", "c"]);
|
|
assert.deepEqual(harness.state.activity.ended.sort(), ["a", "b", "c"]);
|
|
assert.equal(harness.pending.size, 0, "write locks are released");
|
|
});
|
|
|
|
test("run sends nothing and lists every bad target when any preflight check fails", async () => {
|
|
const harness = createHarness({
|
|
ids: ["a", "b", "c", "d"],
|
|
labels: { a: "web1", b: "web2", c: "web3", d: "db1" },
|
|
inScope: ["a", "b", "c"],
|
|
transportErrors: { c: "Command blocked by safety policy. Pattern: rm -rf" },
|
|
});
|
|
harness.busy.set("b", "Session already has exec in progress.");
|
|
const result = await harness.handler.run(
|
|
{ ...harness.baseParams, sessionIds: '["a","b","c","d"]' },
|
|
{},
|
|
{ requiresApproval: true },
|
|
);
|
|
assert.equal(result.ok, false);
|
|
assert.equal(result.code, "BULK_PREFLIGHT_FAILED");
|
|
assert.match(result.error, /^nothing was sent\. 3 of 4 targets cannot run this command:/);
|
|
assert.match(result.error, /- b \(web2\): Session already has exec in progress\./);
|
|
assert.match(result.error, /- c \(web3\): Command blocked by safety policy\. Pattern: rm -rf/);
|
|
assert.match(result.error, /- d: Session "d" is not in the current scope\./);
|
|
assert.doesNotMatch(result.error, /db1/, "out-of-scope ids reveal no label");
|
|
assert.equal(harness.state.execCalls.length, 0);
|
|
assert.equal(harness.state.approvals.length, 0, "no approval prompt for an impossible run");
|
|
assert.equal(harness.pending.size, 0);
|
|
});
|
|
|
|
test("run reports closing, disconnected and unknown sessions", async () => {
|
|
const harness = createHarness({
|
|
ids: ["a", "b", "c"],
|
|
mainIds: ["a"],
|
|
disconnected: ["b"],
|
|
noMeta: ["c"],
|
|
});
|
|
harness.closing.set("a", 1);
|
|
const result = await harness.handler.run(harness.baseParams, {}, {});
|
|
assert.equal(result.ok, false);
|
|
assert.match(result.error, /- a \(web1\): Session "a" is closing\./);
|
|
assert.match(result.error, /- b \(web2\): Session "b" is not connected\./);
|
|
assert.match(result.error, /- c: Session not found/);
|
|
assert.equal(harness.state.execCalls.length, 0);
|
|
});
|
|
|
|
test("run refuses while another command holds the chat lock", async () => {
|
|
const harness = createHarness();
|
|
harness.state.chatLock = { command: "sleep 100", sessionId: "z" };
|
|
const result = await harness.handler.run(harness.baseParams, {}, {});
|
|
assert.equal(result.ok, false);
|
|
assert.equal(result.code, "COMMAND_ALREADY_RUNNING");
|
|
assert.equal(harness.state.execCalls.length, 0);
|
|
});
|
|
|
|
test("transport checks run again after approval and can still cancel the whole call", async () => {
|
|
let refuseNow = false;
|
|
let transportChecks = 0;
|
|
const harness = createHarness({ onApproval: () => { refuseNow = true; } });
|
|
harness.deps.preflightMainExec = async ({ sessionId }) => {
|
|
transportChecks += 1;
|
|
return refuseNow && sessionId === "b"
|
|
? { ok: false, error: "Session does not support command execution" }
|
|
: { ok: true };
|
|
};
|
|
const result = await createExecBulkHandler(harness.deps).run(harness.baseParams, {}, { requiresApproval: true });
|
|
assert.equal(result.ok, false);
|
|
assert.match(result.error, /^nothing was sent\. 1 of 3 targets/);
|
|
assert.match(result.error, /- b \(web2\): Session does not support command execution/);
|
|
assert.equal(transportChecks, 6, "3 before approval + 3 after");
|
|
assert.equal(harness.state.execCalls.length, 0, "no host received the command");
|
|
assert.equal(harness.pending.size, 0, "claims are released");
|
|
});
|
|
|
|
test("run asks once with a fresh, readable approval and runs only after it", async () => {
|
|
const harness = createHarness();
|
|
const payload = await harness.handler.run(harness.baseParams, { externalSocket: "sock" }, { requiresApproval: true });
|
|
assert.equal(payload.ok, true);
|
|
assert.equal(harness.state.approvals.length, 1);
|
|
const [method, args, chatSessionId, context] = harness.state.approvals[0];
|
|
assert.equal(method, BULK_RPC_METHOD);
|
|
assert.deepEqual(args, { command: "uptime", sessions: ["a (web1)", "b (web2)", "c (web3)"] });
|
|
assert.equal(chatSessionId, "chat-1");
|
|
assert.equal(context.requireFreshApproval, true);
|
|
assert.equal(context.externalSocket, "sock");
|
|
assert.ok(harness.state.events.indexOf("approval") < harness.state.events.findIndex((event) => event.startsWith("lock:")));
|
|
});
|
|
|
|
test("run does not ask for approval when the permission mode needs none", async () => {
|
|
const harness = createHarness();
|
|
const payload = await harness.handler.run(harness.baseParams, {}, { requiresApproval: false });
|
|
assert.equal(payload.ok, true);
|
|
assert.equal(harness.state.approvals.length, 0);
|
|
});
|
|
|
|
test("a denied approval runs nothing and releases every claim", async () => {
|
|
const harness = createHarness({ approve: false });
|
|
const result = await harness.handler.run(harness.baseParams, {}, { requiresApproval: true });
|
|
assert.deepEqual(result, { ok: false, error: "Operation denied by user." });
|
|
assert.equal(harness.state.execCalls.length, 0);
|
|
assert.equal(harness.state.events.some((event) => event.startsWith("lock:")), false);
|
|
assert.equal(harness.pending.size, 0);
|
|
});
|
|
|
|
test("targets stay claimed while approval is pending", async () => {
|
|
let claimedDuringApproval = null;
|
|
const harness = createHarness({
|
|
onApproval: ({ pending }) => { claimedDuringApproval = [...pending.keys()]; },
|
|
});
|
|
await harness.handler.run(harness.baseParams, {}, { requiresApproval: true });
|
|
assert.deepEqual(claimedDuringApproval, ["a", "b", "c"]);
|
|
});
|
|
|
|
test("a concurrent bulk call cannot claim targets that are awaiting approval", async () => {
|
|
const approvalGate = deferred();
|
|
const first = createHarness({ onApproval: () => approvalGate.promise });
|
|
const running = first.handler.run(first.baseParams, {}, { requiresApproval: true });
|
|
await nextTick();
|
|
const second = createHarness();
|
|
// Share the first call's claims, like two calls inside one bridge.
|
|
for (const id of first.pending.keys()) second.pending.set(id, BULK_RPC_METHOD);
|
|
const conflict = await second.handler.run(second.baseParams, {}, { requiresApproval: true });
|
|
assert.equal(conflict.ok, false);
|
|
assert.match(conflict.error, /awaiting approval or startup/);
|
|
assert.equal(second.state.execCalls.length, 0);
|
|
approvalGate.resolve();
|
|
assert.equal((await running).ok, true);
|
|
});
|
|
|
|
test("sessions that become busy during approval cancel the whole call before anything is sent", async () => {
|
|
const harness = createHarness({
|
|
onApproval: ({ busy }) => { busy.set("c", "Session already has exec in progress."); },
|
|
});
|
|
const result = await harness.handler.run(harness.baseParams, {}, { requiresApproval: true });
|
|
assert.equal(result.ok, false);
|
|
assert.match(result.error, /^nothing was sent\. 1 of 3 targets/);
|
|
assert.equal(harness.state.execCalls.length, 0);
|
|
assert.equal(harness.pending.size, 0);
|
|
});
|
|
|
|
test("a chat cancelled during approval sends nothing", async () => {
|
|
const harness = createHarness({ onApproval: ({ state }) => { state.cancelled = true; } });
|
|
const result = await harness.handler.run(harness.baseParams, {}, { requiresApproval: true });
|
|
assert.deepEqual(result, { ok: false, error: "Operation cancelled: the SDK agent session was stopped." });
|
|
assert.equal(harness.state.execCalls.length, 0);
|
|
assert.equal(harness.pending.size, 0);
|
|
});
|
|
|
|
test("the privacy hook runs before the first command and can stop the call", async () => {
|
|
const harness = createHarness();
|
|
const result = await harness.handler.run(
|
|
harness.baseParams,
|
|
{ beforeExecute: () => { throw new Error("External MCP privacy settings changed. Reconnect the MCP client before continuing."); } },
|
|
{},
|
|
);
|
|
assert.equal(result.ok, false);
|
|
assert.match(result.error, /privacy settings changed/);
|
|
assert.equal(harness.state.execCalls.length, 0);
|
|
assert.equal(harness.state.events.some((event) => event.startsWith("lock:")), false, "refused before the chat lock is taken");
|
|
});
|
|
|
|
test("per-host failures stay per host and the others still report", async () => {
|
|
const harness = createHarness({
|
|
exec: (params) => {
|
|
if (params.sessionId === "a") throw new Error("sync boom");
|
|
if (params.sessionId === "b") return Promise.reject(new Error("async boom"));
|
|
return Promise.resolve({ ok: true, stdout: "fine", exitCode: 0 });
|
|
},
|
|
});
|
|
const payload = await harness.handler.run(harness.baseParams, {}, {});
|
|
assert.equal(payload.ok, true);
|
|
assert.deepEqual(payload.counts, { error: 2, completed: 1 });
|
|
const errors = payload.groups.filter((group) => group.status === "error");
|
|
assert.deepEqual(errors.map((group) => group.error).sort(), ["async boom", "sync boom"]);
|
|
assert.equal(payload.groups.find((group) => group.status === "completed").stdout, "fine");
|
|
});
|
|
|
|
test("when no host received the command the call fails", async () => {
|
|
const harness = createHarness({
|
|
exec: () => Promise.resolve({ ok: false, error: "Command blocked by safety policy. Pattern: shutdown" }),
|
|
});
|
|
const result = await harness.handler.run(harness.baseParams, {}, {});
|
|
assert.equal(result.ok, false);
|
|
assert.equal(result.code, "BULK_NOT_SENT");
|
|
assert.match(result.error, /^No command was sent to any host\./);
|
|
assert.match(result.error, /- a \(web1\): Command blocked by safety policy/);
|
|
});
|
|
|
|
test("hosts proven not ready make the whole call fail, but an unexplained failure does not claim 'not sent'", async () => {
|
|
const refused = createHarness({
|
|
exec: () => Promise.resolve({ ok: false, code: "SHELL_NOT_READY", exitCode: -1, error: "Shell prompt is not ready; no command was sent" }),
|
|
});
|
|
const notSent = await refused.handler.run(refused.baseParams, {}, {});
|
|
assert.equal(notSent.ok, false);
|
|
assert.equal(notSent.code, "BULK_NOT_SENT");
|
|
|
|
const died = createHarness({
|
|
exec: () => Promise.resolve({ ok: false, error: "Terminal worker exited before replying" }),
|
|
});
|
|
const unknown = await died.handler.run(died.baseParams, {}, {});
|
|
assert.equal(unknown.ok, true, "the command may have run; do not invite a blind retry");
|
|
assert.deepEqual(unknown.counts, { error: 3 });
|
|
});
|
|
|
|
test("the deadline reports unfinished hosts as timeouts and cancels the chat's execs", async () => {
|
|
let fireDeadline;
|
|
let scheduledMs;
|
|
const harness = createHarness({
|
|
exec: (params) => (params.sessionId === "c" ? new Promise(() => {}) : Promise.resolve({ ok: true, stdout: "ok", exitCode: 0 })),
|
|
setTimeoutFn: (fn, ms) => { fireDeadline = fn; scheduledMs = ms; return 1; },
|
|
clearTimeoutFn: () => {},
|
|
});
|
|
const running = harness.handler.run(harness.baseParams, {}, {});
|
|
await nextTick();
|
|
assert.equal(scheduledMs, 60000 + DEADLINE_GRACE_MS);
|
|
fireDeadline();
|
|
const payload = await running;
|
|
assert.deepEqual(harness.state.cancelCalls, ["chat-1"]);
|
|
assert.deepEqual(payload.counts, { completed: 2, timeout: 1 });
|
|
const timeout = payload.groups.find((group) => group.status === "timeout");
|
|
assert.deepEqual(timeout.hosts, [{ sessionId: "c", label: "web3" }]);
|
|
assert.equal(timeout.error, "Command timed out (60s)");
|
|
assert.equal(timeout.exitCode, null);
|
|
assert.equal(harness.state.events.at(-1), "unlock");
|
|
});
|
|
|
|
test("invalid input is rejected before any session is inspected", async () => {
|
|
const harness = createHarness();
|
|
const result = await harness.handler.run({ ...harness.baseParams, sessionIds: '["a","a"]' }, {}, {});
|
|
assert.equal(result.ok, false);
|
|
assert.match(result.error, /unique/);
|
|
assert.equal(harness.state.execCalls.length, 0);
|
|
assert.equal(harness.pending.size, 0);
|
|
});
|
|
|
|
test("a single session is rejected and pointed at terminal_execute", () => {
|
|
assert.equal(MIN_BULK_TARGETS, 2);
|
|
const result = parseBulkExecParams({ command: "uptime", sessionIds: '["only-one"]' });
|
|
assert.equal(result.ok, false);
|
|
assert.match(result.error, /2-20/);
|
|
assert.match(result.error, /terminal_execute/);
|
|
assert.equal(parseBulkExecParams({ command: "uptime", sessionIds: '["a","b"]' }).ok, true);
|
|
});
|
|
|
|
test("sessions are protected from idle close during approval and released on every path", async () => {
|
|
const denied = createHarness({ approve: false });
|
|
await denied.handler.run(denied.baseParams, {}, { requiresApproval: true });
|
|
assert.deepEqual(denied.state.activity.begun, ["a", "b", "c"]);
|
|
assert.deepEqual(denied.state.activity.ended.sort(), ["a", "b", "c"]);
|
|
|
|
let begunDuringApproval = null;
|
|
const approved = createHarness({ onApproval: ({ state }) => { begunDuringApproval = [...state.activity.begun]; } });
|
|
await approved.handler.run(approved.baseParams, {}, { requiresApproval: true });
|
|
assert.deepEqual(begunDuringApproval, ["a", "b", "c"], "protected before the user answers");
|
|
assert.deepEqual(approved.state.activity.ended.sort(), ["a", "b", "c"]);
|
|
|
|
const failedPreflight = createHarness({ inScope: ["a", "b"] });
|
|
await failedPreflight.handler.run(failedPreflight.baseParams, {}, {});
|
|
assert.deepEqual(failedPreflight.state.activity.begun, [], "nothing is held when preflight fails");
|
|
});
|
|
|
|
test("a stale disconnected snapshot does not reject a live main-process session", async () => {
|
|
const harness = createHarness({ disconnected: ["a", "b", "c"], mainIds: ["a"] });
|
|
const result = await harness.handler.run(harness.baseParams, {}, {});
|
|
// a is a live main-process session: accepted. b and c exist only as stale metadata: refused.
|
|
assert.equal(result.ok, false);
|
|
assert.doesNotMatch(result.error, /- a /);
|
|
assert.match(result.error, /- b \(web2\): Session "b" is not connected\./);
|
|
assert.match(result.error, /- c \(web3\): Session "c" is not connected\./);
|
|
});
|
|
|
|
test("a refusal that quotes a pattern containing status words is still not_sent", () => {
|
|
for (const pattern of ["cancel-job", "timed out", "connection lost"]) {
|
|
const result = classifyHostResult({ ok: false, error: `Command blocked by safety policy. Pattern: ${pattern}` });
|
|
assert.equal(result.status, "not_sent", pattern);
|
|
}
|
|
});
|
|
|
|
test("hosts whose exec layer already dropped part of the output are never grouped", () => {
|
|
const same = { ok: true, stdout: "tail", exitCode: 0 };
|
|
const payload = groupBulkResults([
|
|
// Same retained tail and same amount dropped: still cannot prove the full results match.
|
|
{ sessionId: "s1", label: "a", result: { ...same, outputTruncated: true, outputBaseOffset: 1_100_000 } },
|
|
{ sessionId: "s2", label: "b", result: { ...same, outputTruncated: true, outputBaseOffset: 1_100_000 } },
|
|
{ sessionId: "s3", label: "c", result: { ...same } },
|
|
{ sessionId: "s4", label: "d", result: { ...same } },
|
|
]);
|
|
assert.equal(payload.groups.length, 3);
|
|
assert.deepEqual(payload.groups.map((group) => group.hosts.map((host) => host.sessionId)), [["s1"], ["s2"], ["s3", "s4"]]);
|
|
assert.deepEqual(payload.groups.map((group) => group.outputTruncated), [true, true, undefined]);
|
|
});
|
|
|
|
test("stdout and stderr share one size budget per group so the response stays capped", () => {
|
|
const big = "x".repeat(40_000);
|
|
const entries = Array.from({ length: 20 }, (_, i) => ({
|
|
sessionId: `s${i}`, label: `h${i}`,
|
|
result: { ok: true, stdout: `${i}${big}`, stderr: `${i}${big}`, exitCode: 0 },
|
|
}));
|
|
const payload = groupBulkResults(entries, { tailLines: 2000 });
|
|
assert.equal(payload.groups.length, 20);
|
|
const total = payload.groups.reduce((sum, group) => sum + group.stdout.length + (group.stderr || "").length, 0);
|
|
assert.ok(total <= 256 * 1024, `response ${total} exceeds the cap`);
|
|
assert.ok(payload.groups.every((group) => group.outputTruncated === true));
|
|
});
|
|
|
|
test("hosts that differ in only one field never share a group", () => {
|
|
const base = { ok: true, stdout: "same", stderr: "", exitCode: 0 };
|
|
const variants = [
|
|
base,
|
|
{ ...base, exitCode: 1, ok: false },
|
|
{ ...base, stderr: "warn" },
|
|
{ ...base, ok: false, exitCode: -1, error: "Command timed out (60s)" },
|
|
{ ...base, ok: false, exitCode: -1, error: "Cancelled" },
|
|
{ ...base, exitCode: null },
|
|
];
|
|
const payload = groupBulkResults(variants.map((result, i) => ({ sessionId: `s${i}`, label: "", result })));
|
|
assert.equal(payload.groups.length, variants.length, "one group per distinct result");
|
|
assert.deepEqual(
|
|
payload.groups.map((group) => [group.status, group.exitCode]),
|
|
[["completed", 0], ["completed", 1], ["completed", 0], ["timeout", null], ["cancelled", null], ["completed", null]],
|
|
);
|
|
// And identical results still merge.
|
|
assert.equal(groupBulkResults([0, 1, 2].map((i) => ({ sessionId: `s${i}`, label: "", result: { ...base } }))).groups.length, 1);
|
|
});
|
|
|
|
test("not_sent requires explicit evidence; an unexplained failure is never reported as not sent", () => {
|
|
// A worker that exits after typing the command returns an error with no output and no exit code.
|
|
const unknown = classifyHostResult({ ok: false, error: "Terminal worker exited before replying" });
|
|
assert.equal(unknown.status, "error");
|
|
assert.equal(classifyHostResult({ ok: false, error: "boom" }).status, "error");
|
|
|
|
for (const code of ["SHELL_NOT_READY", "SHELL_READINESS_UNKNOWN", "EXECUTION_NOT_AUTHORIZED", "COMMAND_ALREADY_RUNNING"]) {
|
|
assert.equal(classifyHostResult({ ok: false, code, exitCode: -1, error: "x" }).status, "not_sent", code);
|
|
}
|
|
for (const error of [
|
|
"Command blocked by safety policy. Pattern: rm -rf",
|
|
"Shell prompt is not ready; no command was sent",
|
|
"The command was NOT sent - never blind-type",
|
|
"AI privacy session is unavailable or its settings changed; start a new chat",
|
|
"Session already has another command in progress. Wait for it.",
|
|
"Session not found",
|
|
'Session "s1" is closing.',
|
|
"Session does not support command execution",
|
|
"No terminal stream or SSH client available for this session",
|
|
"Network device session has no writable PTY stream for command execution",
|
|
"Serial file transfer is already in progress",
|
|
"AI execution is not supported for this local shell executable.",
|
|
'Another Netcatty command is already running for chat session "c".',
|
|
]) {
|
|
assert.equal(classifyHostResult({ ok: false, error }).status, "not_sent", error);
|
|
}
|
|
});
|
|
|
|
test("source-capped outputs are never grouped", () => {
|
|
const capped = { ok: false, stdout: "prefix", exitCode: null, error: "Command output exceeded the 1048576 byte limit", outputTruncated: true };
|
|
const payload = groupBulkResults([
|
|
{ sessionId: "s1", label: "a", result: { ...capped } },
|
|
{ sessionId: "s2", label: "b", result: { ...capped } },
|
|
]);
|
|
assert.equal(payload.groups.length, 2);
|
|
assert.ok(payload.groups.every((group) => group.outputTruncated === true));
|
|
});
|
|
|
|
function recordProbes(harness, log, getPhase) {
|
|
const record = (kind) => async ({ sessionId }, options) => {
|
|
log.push({ kind, sessionId, probe: options?.probe, phase: getPhase() });
|
|
return { ok: true };
|
|
};
|
|
harness.deps.preflightMainExec = record("main");
|
|
harness.deps.preflightWorkerExec = record("worker");
|
|
}
|
|
|
|
test("no host is probed before the approval prompt; probes run only after the user approves", async () => {
|
|
for (const mainIds of [["a", "b", "c"], []]) {
|
|
let phase = "before";
|
|
const log = [];
|
|
const harness = createHarness({ mainIds, onApproval: () => { phase = "after"; } });
|
|
recordProbes(harness, log, () => phase);
|
|
const result = await createExecBulkHandler(harness.deps).run(harness.baseParams, {}, { requiresApproval: true });
|
|
assert.equal(result.ok, true);
|
|
const before = log.filter((entry) => entry.phase === "before");
|
|
const after = log.filter((entry) => entry.phase === "after");
|
|
assert.equal(before.length, 3);
|
|
assert.ok(before.every((entry) => entry.probe === false), "pre-approval checks must not touch the hosts");
|
|
assert.equal(after.length, 3);
|
|
assert.ok(after.every((entry) => entry.probe === true), "the full check runs after approval");
|
|
}
|
|
});
|
|
|
|
test("a denied approval never probes any host", async () => {
|
|
const log = [];
|
|
const harness = createHarness({ approve: false });
|
|
recordProbes(harness, log, () => "before");
|
|
const result = await createExecBulkHandler(harness.deps).run(harness.baseParams, {}, { requiresApproval: true });
|
|
assert.equal(result.ok, false);
|
|
assert.equal(log.length, 3);
|
|
assert.ok(log.every((entry) => entry.probe === false), "denying must leave the hosts untouched");
|
|
});
|
|
|
|
test("without an approval prompt the full check runs up front", async () => {
|
|
const log = [];
|
|
const harness = createHarness();
|
|
recordProbes(harness, log, () => "before");
|
|
await createExecBulkHandler(harness.deps).run(harness.baseParams, {}, { requiresApproval: false });
|
|
assert.ok(log.length >= 3);
|
|
assert.ok(log.every((entry) => entry.probe === true));
|
|
});
|
|
|
|
test("a call that is no longer authorized after approval never probes a host", async () => {
|
|
const scenarios = {
|
|
"chat cancelled while the prompt was open": { onApproval: ({ state }) => { state.cancelled = true; } },
|
|
"session left the scope while the prompt was open": { onApproval: ({ inScope }) => { inScope.delete("b"); } },
|
|
"session became busy while the prompt was open": { onApproval: ({ busy }) => { busy.set("c", "Session already has exec in progress."); } },
|
|
};
|
|
for (const [name, options] of Object.entries(scenarios)) {
|
|
let phase = "before";
|
|
const log = [];
|
|
const harness = createHarness({ onApproval: (ctx) => { phase = "after"; options.onApproval(ctx); } });
|
|
recordProbes(harness, log, () => phase);
|
|
const result = await createExecBulkHandler(harness.deps).run(harness.baseParams, {}, { requiresApproval: true });
|
|
assert.equal(result.ok, false, name);
|
|
assert.equal(log.filter((entry) => entry.probe === true).length, 0, `${name}: no remote probe`);
|
|
assert.equal(harness.state.execCalls.length, 0, name);
|
|
}
|
|
|
|
// The privacy policy changing while the prompt was open is checked before the probes too.
|
|
let phase = "before";
|
|
const log = [];
|
|
const harness = createHarness({ onApproval: () => { phase = "after"; } });
|
|
recordProbes(harness, log, () => phase);
|
|
const result = await createExecBulkHandler(harness.deps).run(
|
|
harness.baseParams,
|
|
{ beforeExecute: () => { if (phase === "after") throw new Error("privacy settings changed"); } },
|
|
{ requiresApproval: true },
|
|
);
|
|
assert.equal(result.ok, false);
|
|
assert.equal(log.filter((entry) => entry.probe === true).length, 0, "privacy change: no remote probe");
|
|
});
|
|
|
|
test("the local guards run again after the probes, which awaited, before anything starts", async () => {
|
|
let cancelDuringProbe = false;
|
|
const harness = createHarness();
|
|
harness.deps.preflightMainExec = async (_params, options) => {
|
|
if (options?.probe === true) cancelDuringProbe = true; // the chat is stopped while hosts are being probed
|
|
return { ok: true };
|
|
};
|
|
harness.deps.isChatSessionCancelled = () => cancelDuringProbe;
|
|
const result = await createExecBulkHandler(harness.deps).run(harness.baseParams, {}, { requiresApproval: true });
|
|
assert.deepEqual(result, { ok: false, error: "Operation cancelled: the SDK agent session was stopped." });
|
|
assert.equal(harness.state.execCalls.length, 0, "nothing starts after a Stop that landed during the probes");
|
|
});
|
|
|
|
const OBSERVER_DENIED = { allowed: false, error: 'Operation denied: permission mode is "observer" (read-only).' };
|
|
|
|
test("switching to observer while the prompt is open stops the call before any probe", async () => {
|
|
let mode = "confirm";
|
|
let phase = "before";
|
|
const log = [];
|
|
const harness = createHarness({ onApproval: () => { phase = "after"; mode = "observer"; } });
|
|
recordProbes(harness, log, () => phase);
|
|
harness.deps.getLivePermission = () => (mode === "observer" ? OBSERVER_DENIED : { allowed: true, requiresApproval: true });
|
|
const result = await createExecBulkHandler(harness.deps).run(harness.baseParams, {}, { requiresApproval: true });
|
|
assert.deepEqual(result, { ok: false, error: OBSERVER_DENIED.error });
|
|
assert.equal(log.filter((entry) => entry.probe === true).length, 0, "no probe after the mode changed");
|
|
assert.equal(harness.state.execCalls.length, 0);
|
|
assert.equal(harness.pending.size, 0);
|
|
});
|
|
|
|
test("switching to observer while the hosts are being probed stops the call before it starts", async () => {
|
|
let mode = "confirm";
|
|
const harness = createHarness();
|
|
harness.deps.preflightMainExec = async (_params, options) => {
|
|
if (options?.probe === true) mode = "observer";
|
|
return { ok: true };
|
|
};
|
|
harness.deps.getLivePermission = () => (mode === "observer" ? OBSERVER_DENIED : { allowed: true, requiresApproval: true });
|
|
const result = await createExecBulkHandler(harness.deps).run(harness.baseParams, {}, { requiresApproval: true });
|
|
assert.equal(result.ok, false);
|
|
assert.equal(result.error, OBSERVER_DENIED.error);
|
|
assert.equal(harness.state.execCalls.length, 0, "nothing starts once the mode is observer");
|
|
});
|
|
|
|
test("a call that needed no approval is refused if the mode turns into confirm before it starts", async () => {
|
|
let mode = "auto";
|
|
const harness = createHarness();
|
|
harness.deps.preflightMainExec = async () => { mode = "confirm"; return { ok: true }; };
|
|
harness.deps.getLivePermission = () => ({ allowed: true, requiresApproval: mode === "confirm" });
|
|
const result = await createExecBulkHandler(harness.deps).run(harness.baseParams, {}, { requiresApproval: false });
|
|
assert.equal(result.ok, false);
|
|
assert.match(result.error, /changed to confirm/);
|
|
assert.equal(harness.state.execCalls.length, 0);
|
|
|
|
// An approval the user did give still covers a run that stays in confirm mode.
|
|
const approved = createHarness();
|
|
approved.deps.getLivePermission = () => ({ allowed: true, requiresApproval: true });
|
|
const ok = await createExecBulkHandler(approved.deps).run(approved.baseParams, {}, { requiresApproval: true });
|
|
assert.equal(ok.ok, true);
|
|
});
|
|
|
|
test("explicit no-send codes thrown by the exec layer, and closed streams, are classified from the executors' real messages", async () => {
|
|
const pending = Object.assign(new Error("Terminal is still waiting for the shell prompt after the previous command"), { code: "SHELL_PROMPT_PENDING" });
|
|
const refused = createHarness({ exec: () => { throw pending; } });
|
|
const notSent = await refused.handler.run(refused.baseParams, {}, {});
|
|
assert.equal(notSent.ok, false, "every host refused before writing anything");
|
|
assert.equal(notSent.code, "BULK_NOT_SENT");
|
|
|
|
const mixed = createHarness({
|
|
exec: (params) => {
|
|
if (params.sessionId === "a") throw pending;
|
|
if (params.sessionId === "b") throw new Error("something unexpected");
|
|
return Promise.resolve({ ok: true, stdout: "ok", exitCode: 0 });
|
|
},
|
|
});
|
|
const payload = await mixed.handler.run(mixed.baseParams, {}, {});
|
|
assert.deepEqual(payload.counts, { not_sent: 1, error: 1, completed: 1 });
|
|
|
|
for (const error of ["Stream closed unexpectedly", "Serial port closed unexpectedly", "Command terminated unexpectedly (connection lost or signal)"]) {
|
|
assert.equal(classifyHostResult({ ok: false, error, stdout: "partial" }).status, "connection_lost", error);
|
|
}
|
|
assert.equal(classifyHostResult({ ok: false, thrown: true, error: "boom" }).status, "error");
|
|
assert.equal(classifyHostResult({ ok: false, thrown: true, code: "SHELL_PROMPT_PENDING", error: "x" }).status, "not_sent");
|
|
});
|