1. Stale bus host lock refusals from bus status, start, stop and restart name mosaic bus recover ORG. 2. org definitions: unknown org, unknown or malformed name and bad id give one refusal or usage line, no stack trace, no host path. 3. Pack, board and workflow refusals print their hint as template save does; tested with the real CliError transport shape. 4. console start and stop clear a console/start.lock whose named supervisor (pid and start time) is gone; a live or unnamed holder refuses. 5. Template save keeps free text verbatim and writes no placeholders; the preview lists each field that mentions an org-specific name. 6. A request the shim never sent (no socket, connect refused) exits 5 and the CLI reports bus-not-running with the start and recover line. 7. autonomy-precondition carries the check that failed, including on the hire paths. 8. Saving under an existing template name refuses template-exists with a next step; refusal table entry added, channel-proof pins moved.
102 lines
4.0 KiB
JavaScript
102 lines
4.0 KiB
JavaScript
// The human transport (lead decision 70): run
|
|
// `packages/bus/src/human-cli.mjs <socket>` as a child, write
|
|
// `{business, verb, args}` on its stdin, read the JSON reply on its stdout,
|
|
// or one bus error code on its stderr. The bus does the proof: the child
|
|
// re-executes itself with a nonce and the broker checks the process and its
|
|
// ancestry for agent markers. Nothing here carries a capability.
|
|
|
|
import { spawnSync } from "node:child_process";
|
|
import { fileURLToPath } from "node:url";
|
|
import { CliError } from "./errors.mjs";
|
|
|
|
export const HUMAN_CLI = fileURLToPath(new URL("../../bus/src/human-cli.mjs", import.meta.url));
|
|
export const TESTER_CLI = fileURLToPath(new URL("../../bus/src/tester-cli.mjs", import.meta.url));
|
|
|
|
// The broker's markers (packages/bus/src/human.mjs). The broker checks the
|
|
// whole ancestry; this check is only the clear early message for the case
|
|
// it would refuse anyway.
|
|
export const AGENT_MARKERS = Object.freeze([
|
|
"MOSAIC_BUS_CAP",
|
|
"MOSAIC_RUN_ID",
|
|
"MOSAIC_AGENT_RUN",
|
|
"CLAUDECODE",
|
|
"CLAUDE_CODE_ENTRYPOINT",
|
|
"CODEX_THREAD_ID",
|
|
"PI_AGENT_DIR",
|
|
]);
|
|
|
|
export function refuseInsideAgent(env = process.env) {
|
|
const found = AGENT_MARKERS.filter((k) => env[k]);
|
|
if (found.length > 0) {
|
|
throw new CliError(`refused: this runs only from a human shell, outside any agent run (found ${found.join(", ")} in the environment)`, 3);
|
|
}
|
|
}
|
|
|
|
// Exit codes by bus error code; anything else is invalid input (2).
|
|
const EXIT = {
|
|
"human-required": 3,
|
|
unauthenticated: 3,
|
|
"unknown-business": 3,
|
|
"read-only": 3,
|
|
"tester-required": 3,
|
|
"tester-off": 3,
|
|
"tester-refused": 3,
|
|
"bus-not-running": 3,
|
|
"outcome-unknown": 1,
|
|
"outcome-unknown-revoked": 1,
|
|
"response-too-large": 1,
|
|
"invalid-response": 1,
|
|
};
|
|
|
|
export function busExit(code) {
|
|
return EXIT[code] ?? 2;
|
|
}
|
|
|
|
// E2E6 fix 6: the shim exits 5 (packages/bus/src/client.mjs NOT_SENT_EXIT) when its connect failed
|
|
// before the request was written: no socket, or the connect refused. That is a stopped bus, and
|
|
// nothing happened; outcome-unknown stays for a request that left and got no answer.
|
|
const NOT_SENT_EXIT = 5;
|
|
export function busNotRunning(business) {
|
|
const error = new CliError(`bus-not-running: the bus for ${business} is not running, so the request was not sent`, 3);
|
|
error.code = "bus-not-running";
|
|
error.next = `start it with mosaic bus start ${business}, or clear a host that died with mosaic bus recover ${business}`;
|
|
return error;
|
|
}
|
|
|
|
// Returns a call(verb, args) for one business. `cli` and `spawn` exist for
|
|
// the tests; the command line never sets them.
|
|
export function humanTransport({ socket, business, env = process.env, cli = HUMAN_CLI, spawn = spawnSync, timeoutMs = 30000 }) {
|
|
return async function call(verb, args = {}) {
|
|
const proc = spawn(process.execPath, [cli, socket], {
|
|
input: `${JSON.stringify({ business, verb, args })}\n`,
|
|
encoding: "utf8",
|
|
env,
|
|
timeout: timeoutMs,
|
|
maxBuffer: 8 * 1024 * 1024,
|
|
});
|
|
if (proc.error || proc.status === null) throw new CliError("outcome-unknown: the bus transport did not finish", 1);
|
|
if (proc.status !== 0) {
|
|
const code = String(proc.stderr ?? "").trim().split("\n").reverse().find((l) => /^[a-z-]{1,64}$/.test(l)) ?? "invalid-response";
|
|
if (proc.status === NOT_SENT_EXIT && code === "outcome-unknown") throw busNotRunning(business);
|
|
const error = new CliError(code, busExit(code));
|
|
error.code = code;
|
|
throw error;
|
|
}
|
|
try {
|
|
return JSON.parse(proc.stdout);
|
|
} catch {
|
|
const error = new CliError("invalid-response", 1);
|
|
error.code = "invalid-response";
|
|
throw error;
|
|
}
|
|
};
|
|
}
|
|
|
|
// Row 110 (#1604): the tester's transport is the same exchange through
|
|
// tester-cli.mjs. It makes no agent check of its own: a tester runs from
|
|
// inside Mos or a T3 seat, and the broker refuses one under a managed run or
|
|
// while the business's bus.tester is off.
|
|
export function testerTransport(options) {
|
|
return humanTransport({ cli: TESTER_CLI, ...options });
|
|
}
|