diff --git a/docs/watchdog-notice.md b/docs/watchdog-notice.md new file mode 100644 index 0000000..610f46c --- /dev/null +++ b/docs/watchdog-notice.md @@ -0,0 +1,52 @@ +# Watchdog notice reader + +`tools/watchdog-notice.mjs` is a one-shot reader. It turns Idunn's operator +incidents into Discord DMs to the owner. Run it from a timer. + +``` +node tools/watchdog-notice.mjs process --incident-store P --journal-store J --receipt-store R [--bridge-cli B] +node tools/watchdog-notice.mjs status --journal-store J +``` + +## Requirements + +- **CultLib at `8cb3b728` or later.** The reader loads `@gamecult/cultcache-ts`, + the name CultLib gave the package in `8cb3b728` (2026-09-04), from + `$VOIDBOT_CULTLIB_ROOT` or the sibling `../CultLib`. An older CultLib fails at + import with `MODULE_NOT_FOUND`; the reader reports `cultlib-unavailable`. The + same floor applies to every Bifrost tool that loads CultCache + (`tools/persona-feedback.mjs` and the others). A deployment pairs a Bifrost + release with a CultLib commit at or after this one. +- `DISCORD_OWNER_ID` in the environment: the only source of the recipient. The + reader exits non-zero before writing the journal if it is missing. +- `BIFROST_DISCORD_BOT_TOKEN` in the environment, read only by + `bifrost-bridge.mjs`, which the reader spawns. + +## Behaviour + +- P is Idunn's `idunn.operator_incident.v1` store. The reader reads it without a + lock and never writes it. An absent P is a clean no-op. +- One notice when an incident opens and one when it closes, each at most once. + The closing notice is sent only after the opening notice completed. +- J is Bifrost's journal, `bifrost.watchdog_notice_execution.v1`, keyed + `#opened` or `#closed`. A `running` entry found at + start becomes `unknown` and is never sent again. A `failed` entry is retried on + later runs with the same nonce, up to five attempts. The journal is never + pruned: what Bifrost has sent is Bifrost's memory, whatever P holds. +- The bridge (`bifrost-bridge.mjs discord-dm`) exits 75 for argument or token + errors, a failure opening the DM channel, or any non-2xx answer to the message + POST. A non-2xx is retried, not proven unsent: the retry carries the same nonce + and relies on Discord's `enforce_nonce` inside its window, so a 5xx that Discord + answered after creating the message can be delivered twice if the retry falls + outside that window. A 5xx stays retryable because parking it as `unknown` + would lose the notice on every routine Discord outage. The reader records + `failed` for exit 75 only. Any other non-success without a message id (another exit code, + a signal, the spawn timeout, unreadable output) is `unknown`: never sent again, + counted by the exit status. +- The exit status is non-zero while any entry is `unknown` or out of attempts, or + while P holds a record of this schema that breaks its contract. systemd shows + that; `status` lists the entries. +- Notice text is built from `condition`, `subject` and ISO UTC times only. The + subject follows Idunn's `require_id` (1-256 bytes of `[A-Za-z0-9-_.:/]`) and + is placed in inline code. A time outside the ECMAScript Date range refuses the + record. diff --git a/tests/fixtures/idunn.operator_incident.v1.cc b/tests/fixtures/idunn.operator_incident.v1.cc new file mode 100644 index 0000000..180c6f0 Binary files /dev/null and b/tests/fixtures/idunn.operator_incident.v1.cc differ diff --git a/tests/watchdog-notice.test.mjs b/tests/watchdog-notice.test.mjs new file mode 100644 index 0000000..ff7c6ae --- /dev/null +++ b/tests/watchdog-notice.test.mjs @@ -0,0 +1,460 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import { createHash } from "node:crypto"; +import { createRequire } from "node:module"; +import { spawnSync } from "node:child_process"; +import { mkdtemp, mkdir, readFile, readdir, stat, writeFile, copyFile } from "node:fs/promises"; +import { existsSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { resolve } from "node:path"; +import { fileURLToPath, pathToFileURL } from "node:url"; + +// The fixture is the store Idunn's incident cut wrote: GameCult/Idunn branch +// hands/watchdog-idunn-incident, commit 7f7528b33e8e5c607c19cc7f8f8a8da4def50fca +// ("Add the idunn.operator_incident.v1 wire fixture"), tests/fixtures/ there. +// It holds two records: closed-target (closed, recovered) and open-target. +const FIXTURE = resolve(import.meta.dirname, "fixtures", "idunn.operator_incident.v1.cc"); +const FIXTURE_SHA256 = "f446770b07da99204e9036ef521f3136be6251e5b0a68e4e9533a89f25f18cab"; +const CLOSED_KEY = "continuity-exhausted:closed-target:1700000000000"; +const OPEN_KEY = "continuity-exhausted:open-target:1700000100000"; +const TOOL = resolve(import.meta.dirname, "..", "tools", "watchdog-notice.mjs"); +const BRIDGE = resolve(import.meta.dirname, "..", "tools", "bifrost-bridge.mjs"); +const RECIPIENT = "424242RECIPIENTCANARY"; + +const OPENED_TEXT = (subject, at) => `Idunn incident opened: continuity-exhausted on \`${subject}\` at ${at}`; +const T_OPEN = "2023-11-14T22:13:20.000Z"; +const T_CLOSE = "2023-11-14T22:15:00.000Z"; +const CLOSED_TEXT = `Idunn incident closed (recovered): continuity-exhausted on \`closed-target\`, opened ${T_OPEN}, closed ${T_CLOSE}`; + +// CultLib is the sibling ../CultLib, like the other tests. +const cult = resolve(import.meta.dirname, "..", "..", "CultLib"); +const cache = createRequire(resolve(cult, "packages", "cultcache-ts", "package.json"))("@gamecult/cultcache-ts"); +const msgpack = createRequire(resolve(cult, "packages", "cultnet-ts", "package.json"))("@msgpack/msgpack"); + +// A bridge double that logs each call (with the content it was handed) and +// behaves as the `mode` file says: ok; fail (exit 75, retryable); die +// (exit 1 after "sending"); signal (killed after "sending"); hang (never answers after +// "sending"); flood (answers with more stdout than spawnSync buffers); or crash (kills its +// parent, the reader, after logging, as a power cut between send and journal would). +const FAKE_BRIDGE = ` +import { appendFileSync, readFileSync } from "node:fs"; +import { dirname, resolve } from "node:path"; +import { fileURLToPath } from "node:url"; +const here = dirname(fileURLToPath(import.meta.url)); +const args = process.argv.slice(2); +const opt = (name) => args[args.indexOf("--" + name) + 1]; +const calls = resolve(here, "calls.jsonl"); +const n = (() => { try { return readFileSync(calls, "utf8").trim().split("\\n").length; } catch { return 0; } })(); +appendFileSync(calls, JSON.stringify({ args, content: readFileSync(opt("content-file"), "utf8") }) + "\\n"); +const mode = readFileSync(resolve(here, "mode"), "utf8").trim(); +if (mode === "crash") { process.kill(process.ppid, "SIGKILL"); process.exit(0); } +if (mode === "fail" || mode === "die") { process.stderr.write("boom " + opt("recipient-id")); process.stdout.write("boom " + opt("recipient-id")); process.exit(mode === "fail" ? 75 : 1); } +if (mode === "signal") process.kill(process.pid, "SIGKILL"); +if (mode === "hang") setTimeout(() => {}, 30000); +else if (mode === "flood") process.stdout.write("x".repeat(2 * 1024 * 1024)); +else +process.stdout.write(JSON.stringify({ action: "discord-dm", ok: true, messageId: "m" + (n + 1) })); +`; + +async function world(prefix, { incidents = FIXTURE, mode = "ok" } = {}) { + const dir = await mkdtemp(resolve(tmpdir(), prefix)); + const w = { + dir, + incidents: resolve(dir, "idunn", "incidents.cc"), + journal: resolve(dir, "bifrost", "journal.cc"), + receipts: resolve(dir, "bifrost", "receipts.cc"), + bridge: resolve(dir, "fake-bridge.mjs"), + calls: resolve(dir, "calls.jsonl"), + }; + await mkdir(resolve(dir, "idunn")); + await writeFile(w.bridge, FAKE_BRIDGE); + await writeFile(resolve(dir, "mode"), mode); + if (incidents) await copyFile(incidents, w.incidents); + w.setMode = (value) => writeFile(resolve(dir, "mode"), value); + w.run = (extra = {}) => run(w, extra); + w.status = () => runStatus(w); + w.posts = async () => (existsSync(w.calls) ? (await readFile(w.calls, "utf8")).trim().split("\n").map((line) => JSON.parse(line)) : []); + return w; +} + +function run(w, { env = {}, bridge = w.bridge } = {}) { + return spawnSync(process.execPath, [TOOL, "process", "--incident-store", w.incidents, "--journal-store", w.journal, "--receipt-store", w.receipts, "--bridge-cli", bridge], { + encoding: "utf8", + env: { ...process.env, DISCORD_OWNER_ID: RECIPIENT, ...env }, + }); +} + +function runStatus(w) { + const result = spawnSync(process.execPath, [TOOL, "status", "--journal-store", w.journal], { encoding: "utf8", env: process.env }); + return { ...result, rows: result.status === null || !result.stdout ? [] : JSON.parse(result.stdout) }; +} + +const opt = (post, name) => post.args[post.args.indexOf(`--${name}`) + 1]; +const notice = (post) => opt(post, "cultmesh-command-id").split("#")[1]; + +// Builds an incident store from the fixture's own envelopes with the payload +// tuples rewritten by `edit`. +async function variantStore(path, edit) { + const source = await new cache.SingleFileMessagePackBackingStore(FIXTURE).pullAll(); + const tuples = edit(source.map((envelope) => msgpack.decode(envelope.payload))); + const envelopes = tuples.map((tuple, index) => ({ ...source[0], key: `variant-${index}-${tuple[1]}`, payload: msgpack.encode(tuple), schemaId: source[0].schemaId, catalogEntry: source[0].catalogEntry })); + await new cache.SingleFileMessagePackBackingStore(path).pushAll(envelopes); +} + +test("the Idunn fixture decodes as the incidents Idunn wrote", async () => { + const bytes = await readFile(FIXTURE); + assert.equal(createHash("sha256").update(bytes).digest("hex"), FIXTURE_SHA256); + const w = await world("wn-fixture-"); + const result = w.run(); + assert.equal(result.status, 0, result.stderr); + const posts = await w.posts(); + const byKey = Object.fromEntries(posts.map((post) => [opt(post, "cultmesh-command-id"), post.content])); + assert.deepEqual(byKey, { + [`${CLOSED_KEY}#opened`]: OPENED_TEXT("closed-target", T_OPEN), + [`${CLOSED_KEY}#closed`]: CLOSED_TEXT, + [`${OPEN_KEY}#opened`]: OPENED_TEXT("open-target", T_CLOSE), + }); +}); + +test("one incident yields one opening post across runs", async () => { + const w = await world("wn-once-"); + assert.equal(w.run().status, 0); + assert.equal(w.run().status, 0); + const posts = await w.posts(); + const openings = posts.filter((post) => notice(post) === "opened"); + assert.equal(openings.length, 2); + assert.deepEqual(openings.map((post) => opt(post, "source-id")).sort(), [CLOSED_KEY, OPEN_KEY]); + assert.equal(posts.length, 3, "no second run posts again"); + for (const post of posts) { + const key = opt(post, "cultmesh-command-id"); + assert.equal(opt(post, "recipient-id"), RECIPIENT); + assert.equal(opt(post, "nonce"), createHash("sha256").update(key).digest("hex").slice(0, 25)); + assert.equal(opt(post, "receipt-store"), w.receipts); + assert.equal(opt(post, "source-kind"), "idunn-operator-incident"); + } +}); + +test("closure is posted once and only after a completed opening", async () => { + const w = await world("wn-closure-"); + assert.equal(w.run().status, 0); + assert.equal(w.run().status, 0); + const closures = (await w.posts()).filter((post) => notice(post) === "closed"); + assert.equal(closures.length, 1); + assert.equal(opt(closures[0], "source-id"), CLOSED_KEY); + + // An opening that never completes blocks its closure, now and later. + const failing = await world("wn-closure-failed-", { mode: "fail" }); + for (let run = 0; run < 5; run += 1) failing.run(); + await failing.setMode("ok"); + failing.run(); + failing.run(); + assert.equal((await failing.posts()).filter((post) => notice(post) === "closed").length, 0); + const rows = (await failing.status()).rows; + assert.deepEqual(rows.map((row) => `${row.notice}:${row.status}:${row.attempts}`), ["opened:failed:5", "opened:failed:5"]); + + const crashed = await world("wn-closure-unknown-", { mode: "crash" }); + assert.equal(crashed.run().status, null, "the reader died mid-send"); + await crashed.setMode("ok"); + crashed.run(); + crashed.run(); + assert.equal((await crashed.posts()).filter((post) => notice(post) === "closed").length, 0); +}); + +test("a running entry becomes unknown and is never re-posted", async () => { + const w = await world("wn-running-", { mode: "crash" }); + assert.equal(w.run().status, null); + assert.equal((await w.status()).rows[0].status, "running"); + await w.setMode("ok"); + const recovered = w.run(); + assert.notEqual(recovered.status, 0, "an unknown entry is a delivery failure"); + assert.equal(JSON.parse(recovered.stdout).unknown, 1); + const posts = await w.posts(); + assert.equal(posts.filter((post) => opt(post, "cultmesh-command-id") === `${CLOSED_KEY}#opened`).length, 1, "the interrupted notice is not sent again"); + const rows = (await w.status()).rows; + assert.equal(rows.find((row) => row.incidentKey === CLOSED_KEY && row.notice === "opened").status, "unknown"); + assert.notEqual(w.run().status, 0, "still failing on the next run"); + assert.equal((await w.posts()).length, posts.length); +}); + +test("only a bridge exit 75 is retried, with the same nonce, up to five attempts", async () => { + const w = await world("wn-retry-", { mode: "fail" }); + const exits = []; + for (let run = 0; run < 5; run += 1) exits.push(w.run().status); + assert.deepEqual(exits, [0, 0, 0, 0, 1], "non-zero once attempts are exhausted"); + assert.deepEqual((await w.status()).rows.map((row) => row.lastError), ["bridge-not-sent", "bridge-not-sent"]); + const open = (await w.posts()).filter((post) => opt(post, "source-id") === OPEN_KEY); + assert.equal(open.length, 5); + assert.equal(new Set(open.map((post) => opt(post, "nonce"))).size, 1); + const before = (await w.posts()).length; + const again = w.run(); + assert.notEqual(again.status, 0); + assert.equal((await w.posts()).length, before, "no spawn after the fifth attempt"); + const status = await w.status(); + assert.notEqual(status.status, 0); + for (const text of [again.stdout, again.stderr, status.stdout, await readFile(w.journal, "latin1")]) { + assert.ok(!text.includes("RECIPIENTCANARY"), "bridge output and recipient never reach the reader's output or journal"); + } +}); + +test("the reader never writes or locks the incident store", async () => { + const w = await world("wn-readonly-"); + const before = { bytes: await readFile(w.incidents), mtime: (await stat(w.incidents)).mtimeMs }; + assert.equal(w.run().status, 0); + assert.equal(w.run().status, 0); + assert.deepEqual(await readFile(w.incidents), before.bytes); + assert.equal((await stat(w.incidents)).mtimeMs, before.mtime); + assert.deepEqual(await readdir(resolve(w.dir, "idunn")), ["incidents.cc"], "no lock or temp file beside the store"); +}); + +test("notice content carries no sensitive text", async () => { + const w = await world("wn-egress-", { incidents: null }); + await variantStore(w.incidents, (tuples) => { + const [closed, open] = tuples; + const longer = [...open, "extra-CANARYFIELD"]; + const pathSubject = ["idunn.operator_incident.v1", `continuity-exhausted:/etc/CANARYPATH x:${open[4]}`, "continuity-exhausted", "/etc/CANARYPATH x", open[4], null, null]; + const otherSchema = ["idunn.operator_incident.v0", "CANARYSCHEMA", "continuity-exhausted", "x", 1, null, null]; + return [closed, longer, pathSubject, otherSchema]; + }); + const result = w.run(); + assert.notEqual(result.status, 0, "refused records fail the run"); + assert.deepEqual(JSON.parse(result.stdout), { skipped: 1, refused: 2, unknown: 0, exhausted: 0 }); + const posts = await w.posts(); + assert.deepEqual(posts.map((post) => opt(post, "source-id")), [CLOSED_KEY, CLOSED_KEY]); + for (const post of posts) assert.match(post.content, /^Idunn incident (opened: continuity-exhausted on `closed-target` at \d{4}-[\d-]+T[\d:.]+Z|closed \(recovered\): continuity-exhausted on `closed-target`, opened [\dTZ:.-]+, closed [\dTZ:.-]+)$/); + for (const text of [result.stdout, result.stderr, (await w.status()).stdout, await readFile(w.journal, "latin1"), JSON.stringify(posts)]) { + for (const canary of ["CANARYFIELD", "CANARYPATH", "CANARYSCHEMA"]) assert.ok(!text.includes(canary), "no byte of a refused record is echoed"); + } +}); + +test("no recipient means no journal write and a non-zero exit", async () => { + for (const value of [undefined, "", " "]) { + const w = await world("wn-recipient-"); + const env = value === undefined ? { DISCORD_OWNER_ID: undefined } : { DISCORD_OWNER_ID: value }; + const result = spawnSync(process.execPath, [TOOL, "process", "--incident-store", w.incidents, "--journal-store", w.journal, "--receipt-store", w.receipts, "--bridge-cli", w.bridge], { encoding: "utf8", env: { ...process.env, ...env } }); + assert.notEqual(result.status, 0); + assert.ok(!existsSync(w.journal)); + assert.deepEqual(await w.posts(), []); + } +}); + +test("an absent incident store is a clean no-op", async () => { + const w = await world("wn-absent-", { incidents: null }); + const result = w.run(); + assert.equal(result.status, 0, result.stderr); + assert.ok(!existsSync(w.journal)); + assert.deepEqual(await w.posts(), []); +}); + +test("an emptied or restored incident store never re-sends", async () => { + const w = await world("wn-restore-"); + assert.equal(w.run().status, 0); + const keys = async () => (await w.status()).rows.map((row) => `${row.incidentKey}#${row.notice}`).sort(); + const all = [`${CLOSED_KEY}#closed`, `${CLOSED_KEY}#opened`, `${OPEN_KEY}#opened`]; + assert.deepEqual(await keys(), all); + await variantStore(w.incidents, (tuples) => [tuples[1]]); + w.run(); + await variantStore(w.incidents, () => []); + w.run(); + await writeFile(w.incidents, ""); + w.run(); + assert.deepEqual(await keys(), all, "absence from Idunn's store deletes nothing"); + await copyFile(FIXTURE, w.incidents); + assert.equal(w.run().status, 0); + assert.equal((await w.posts()).length, 3, "exactly one post per notice across all views of the store"); +}); + +test("a bridge that dies after sending is unknown and never re-posted", async () => { + for (const mode of ["die", "signal"]) { + const w = await world(`wn-${mode}-`, { mode }); + const first = w.run(); + assert.notEqual(first.status, 0); + assert.equal(JSON.parse(first.stdout).unknown, 2); + assert.notEqual((await w.status()).status, 0, "status exits non-zero while an entry is unknown"); + await w.setMode("ok"); + assert.notEqual(w.run().status, 0); + assert.notEqual(w.run().status, 0); + const posts = await w.posts(); + assert.equal(posts.length, 2, "one opening post per incident, no retry, no closure"); + assert.ok(posts.every((post) => notice(post) === "opened")); + assert.deepEqual((await w.status()).rows.map((row) => `${row.notice}:${row.status}:${row.attempts}:${row.lastError}`), ["opened:unknown:1:bridge-exit-unknown", "opened:unknown:1:bridge-exit-unknown"]); + assert.ok(!(await readFile(w.journal, "latin1")).includes("RECIPIENTCANARY")); + } +}); + +test("a spawn timeout and a spawn error are unknown, never failed, never re-posted", async () => { + for (const mode of ["hang", "flood"]) { + const w = await world(`wn-${mode}-`, { mode }); + const first = w.run({ env: { WATCHDOG_NOTICE_BRIDGE_TIMEOUT_MS: "1500" } }); + assert.notEqual(first.status, 0, mode); + assert.equal(JSON.parse(first.stdout).unknown, 2, mode); + await w.setMode("ok"); + assert.notEqual(w.run().status, 0); + assert.notEqual(w.run().status, 0); + const posts = await w.posts(); + assert.equal(posts.length, 2, `${mode}: one opening post per incident, no retry, no closure`); + assert.ok(posts.every((post) => notice(post) === "opened")); + assert.deepEqual((await w.status()).rows.map((row) => `${row.notice}:${row.status}:${row.attempts}:${row.lastError}`), ["opened:unknown:1:bridge-exit-unknown", "opened:unknown:1:bridge-exit-unknown"], mode); + } +}); + +test("errors name a code and never echo a path or an input", async () => { + const w = await world("wn-canary-", { incidents: null }); + await mkdir(resolve(w.dir, "CANARYDIR")); + const corrupt = resolve(w.dir, "CANARYDIR", "incidents.cc"); + await writeFile(corrupt, "not a cultcache store CANARYBYTES"); + const result = spawnSync(process.execPath, [TOOL, "process", "--incident-store", corrupt, "--journal-store", resolve(w.dir, "CANARYDIR", "j.cc"), "--receipt-store", w.receipts, "--bridge-cli", w.bridge], { encoding: "utf8", env: { ...process.env, DISCORD_OWNER_ID: RECIPIENT } }); + assert.notEqual(result.status, 0); + assert.match(result.stderr, /incident-store-unreadable/); + assert.ok(!(result.stdout + result.stderr).includes("CANARY")); +}); + +// The real bridge, reached through the real reader, with Discord's HTTP API +// stubbed at fetch (NODE_OPTIONS reaches the spawned bridge). +async function fetchStub(dir) { + const log = resolve(dir, "fetch.jsonl"); + const stub = resolve(dir, "fetch-stub.mjs"); + await writeFile(stub, ` +import { appendFileSync } from "node:fs"; +globalThis.fetch = async (url, init) => { + appendFileSync(${JSON.stringify(log)}, JSON.stringify({ url: String(url), body: JSON.parse(init.body) }) + "\\n"); + const channels = String(url).endsWith("/users/@me/channels"); + const behavior = process.env.STUB_BEHAVIOR; + if (channels && behavior === "dm-channel-500") return new Response("{}", { status: 500 }); + const rejected = /^message-(\\d{3})$/.exec(behavior ?? ""); + if (!channels && rejected) return new Response("{}", { status: Number(rejected[1]) }); + if (!channels && behavior === "message-network-error") throw new TypeError("fetch failed"); + return new Response(JSON.stringify(channels ? { id: "dm-channel" } : { id: "message-1" }), { status: 200 }); +}; +`); + return { log, options: `--import=${pathToFileURL(stub).href}` }; +} +const fetches = async (log) => (existsSync(log) ? (await readFile(log, "utf8")).trim().split("\n").map((line) => JSON.parse(line)) : []); + +test("discord-dm carries nonce and enforce_nonce when asked", async () => { + const dir = await mkdtemp(resolve(tmpdir(), "wn-bridge-")); + const stub = await fetchStub(dir); + const env = { ...process.env, NODE_OPTIONS: stub.options, BIFROST_DISCORD_BOT_TOKEN: "test-token-not-real" }; + const dm = (...extra) => spawnSync(process.execPath, [BRIDGE, "discord-dm", "--recipient-id", "1", "--content", "hi", "--cultmesh-command-id", "cmd", "--receipt-store", resolve(dir, "r.cc"), ...extra], { encoding: "utf8", env }); + + const with_nonce = dm("--nonce", "abc123"); + assert.equal(with_nonce.status, 0, with_nonce.stderr); + let posted = (await fetches(stub.log)).filter((entry) => entry.url.endsWith("/channels/dm-channel/messages")); + assert.equal(posted.at(-1).body.nonce, "abc123"); + assert.equal(posted.at(-1).body.enforce_nonce, true); + assert.deepEqual(posted.at(-1).body.allowed_mentions, { parse: [] }, "no mention is ever parsed"); + + const without = dm(); + assert.equal(without.status, 0, without.stderr); + posted = (await fetches(stub.log)).filter((entry) => entry.url.endsWith("/channels/dm-channel/messages")); + assert.ok(!("nonce" in posted.at(-1).body) && !("enforce_nonce" in posted.at(-1).body)); + + const before = (await fetches(stub.log)).length; + const tooLong = dm("--nonce", "x".repeat(26)); + assert.notEqual(tooLong.status, 0); + assert.equal((await fetches(stub.log)).length, before, "an over-long nonce posts nothing"); +}); + +test("discord-dm exits 75 only before delivery", async () => { + const dir = await mkdtemp(resolve(tmpdir(), "wn-exit75-")); + const stub = await fetchStub(dir); + const dm = (behavior, { token = "test-token-not-real", recipient = "1" } = {}) => { + const env = { ...process.env, NODE_OPTIONS: stub.options, BIFROST_DISCORD_BOT_TOKEN: token, DISCORD_BOT_TOKEN: token }; + if (behavior) env.STUB_BEHAVIOR = behavior; + if (!token) { delete env.BIFROST_DISCORD_BOT_TOKEN; delete env.DISCORD_BOT_TOKEN; } + const args = ["discord-dm", ...(recipient ? ["--recipient-id", recipient] : []), "--content", "hi", "--cultmesh-command-id", "cmd", "--receipt-store", resolve(dir, "r.cc")]; + return spawnSync(process.execPath, [BRIDGE, ...args], { encoding: "utf8", env }); + }; + const messagePosts = async () => (await fetches(stub.log)).filter((entry) => entry.url.endsWith("/messages")).length; + + assert.equal(dm(undefined, { token: "" }).status, 75, "missing token"); + assert.equal(dm(undefined, { recipient: "" }).status, 75, "missing argument"); + assert.equal(dm("dm-channel-500").status, 75, "DM channel failure"); + assert.equal(await messagePosts(), 0, "nothing was posted by any of those"); + assert.equal(dm("message-400").status, 75, "a 4xx answer to the message POST"); + assert.equal(await messagePosts(), 1); + for (const status of [500, 502, 503, 429]) { + const before = await messagePosts(); + assert.equal(dm(`message-${status}`).status, 75, `a ${status} answer is retryable, not unknown`); + assert.equal(await messagePosts(), before + 1); + } + const lost = dm("message-network-error"); + assert.equal(lost.status, 1, "a network error after the message POST was sent is not exit 75"); + assert.equal(await messagePosts(), 6); + assert.equal(dm().status, 0); +}); + +test("a 5xx or 429 on the message POST is retried with the same nonce", async () => { + for (const status of [502, 429]) { + const w = await world(`wn-${status}-`); + const stub = await fetchStub(w.dir); + const env = { NODE_OPTIONS: stub.options, BIFROST_DISCORD_BOT_TOKEN: "test-token-not-real" }; + const messages = async () => (await fetches(stub.log)).filter((entry) => entry.url.endsWith("/messages")); + for (let tick = 1; tick <= 2; tick += 1) { + const result = w.run({ bridge: BRIDGE, env: { ...env, STUB_BEHAVIOR: `message-${status}` } }); + assert.equal(result.status, 0, result.stderr); + assert.deepEqual((await w.status()).rows.map((row) => `${row.status}:${row.attempts}`), [`failed:${tick}`, `failed:${tick}`]); + } + const sent = await messages(); + assert.equal(sent.length, 4, "two openings, each tried twice"); + const nonces = Map.groupBy(sent, (entry) => entry.body.content); + assert.equal(nonces.size, 2); + for (const group of nonces.values()) assert.equal(new Set(group.map((entry) => entry.body.nonce)).size, 1, "a retry reuses its nonce"); + assert.equal(w.run({ bridge: BRIDGE, env }).status, 0); + assert.deepEqual((await w.status()).rows.map((row) => row.status), ["completed", "completed", "completed"]); + } +}); + +test("every subject Idunn accepts is delivered, inside backticks", async () => { + const good = ["gamecult/odin", "odin:store", "_odin", "a".repeat(256), "Odin.v2-x"]; + const bad = ["a".repeat(257), "has space", "back`tick", "new\nline", "", "ün"]; + const w = await world("wn-subject-", { incidents: null }); + await variantStore(w.incidents, (tuples) => { + return [...good, ...bad].map((subject, index) => { + const opened = 1_700_000_200_000 + index; + return ["idunn.operator_incident.v1", `continuity-exhausted:${subject}:${opened}`, "continuity-exhausted", subject, opened, null, null]; + }); + }); + const stub = await fetchStub(w.dir); + const result = w.run({ bridge: BRIDGE, env: { NODE_OPTIONS: stub.options, BIFROST_DISCORD_BOT_TOKEN: "test-token-not-real" } }); + assert.deepEqual(JSON.parse(result.stdout), { skipped: 0, refused: bad.length, unknown: 0, exhausted: 0 }); + const contents = (await fetches(stub.log)).filter((entry) => entry.url.endsWith("/messages")).map((entry) => entry.body.content).sort(); + const at = (index) => new Date(1_700_000_200_000 + index).toISOString(); + assert.deepEqual(contents, good.map((subject, index) => OPENED_TEXT(subject, at(index))).sort()); + for (const content of contents) assert.match(content, /on `[A-Za-z0-9_.:/-]{1,256}` at /); +}); + +test("an out-of-range time refuses the record without aborting the run", async () => { + const w = await world("wn-time-", { incidents: null }); + await variantStore(w.incidents, (tuples) => { + const [closed, open] = tuples; + const hugeClose = [...closed]; + hugeClose[5] = 8.7e15; + hugeClose[1] = closed[1]; + const hugeOpen = ["idunn.operator_incident.v1", "continuity-exhausted:huge-open:8700000000000000", "continuity-exhausted", "huge-open", 8.7e15, null, null]; + const edge = ["idunn.operator_incident.v1", "continuity-exhausted:edge:8640000000000000", "continuity-exhausted", "edge", 8.64e15, null, null]; + return [hugeClose, hugeOpen, edge, open]; + }); + const result = w.run(); + assert.equal(result.stderr, "", "no unexpected-error"); + assert.deepEqual(JSON.parse(result.stdout), { skipped: 0, refused: 2, unknown: 0, exhausted: 0 }); + assert.notEqual(result.status, 0, "refused records fail the run"); + assert.deepEqual((await w.posts()).map((post) => opt(post, "source-id")).sort(), ["continuity-exhausted:edge:8640000000000000", OPEN_KEY]); + assert.ok((await w.status()).rows.every((row) => row.status === "completed")); +}); + +test("the reader posts through the real bridge with its nonce", async () => { + const w = await world("wn-real-bridge-"); + const stub = await fetchStub(w.dir); + const result = w.run({ bridge: BRIDGE, env: { NODE_OPTIONS: stub.options, BIFROST_DISCORD_BOT_TOKEN: "test-token-not-real" } }); + assert.equal(result.status, 0, result.stderr); + const posted = (await fetches(stub.log)).filter((entry) => entry.url.endsWith("/messages")); + assert.deepEqual(posted.map((entry) => entry.body.content).sort(), [CLOSED_TEXT, OPENED_TEXT("closed-target", T_OPEN), OPENED_TEXT("open-target", T_CLOSE)].sort()); + for (const entry of posted) { + assert.equal(entry.body.nonce.length, 25); + assert.equal(entry.body.enforce_nonce, true); + } + assert.ok(existsSync(w.receipts), "the bridge wrote its crossing receipts to the reader's receipt store"); + assert.deepEqual((await w.status()).rows.map((row) => row.status), ["completed", "completed", "completed"]); + assert.equal((await w.status()).status, 0, "status exits zero when nothing is unknown or exhausted"); +}); + diff --git a/tools/bifrost-bridge.mjs b/tools/bifrost-bridge.mjs index 5b4179b..e51b2f0 100644 --- a/tools/bifrost-bridge.mjs +++ b/tools/bifrost-bridge.mjs @@ -470,11 +470,36 @@ async function postDiscordMessage(options) { }); } +// discord-dm exits NOT_SENT_EXIT only when it knows no message was delivered: +// anything before the message POST, or a non-2xx answer to it. A 5xx or 429 +// is not proof of non-delivery (Discord may have created the message before +// answering): the caller retries with the same nonce and relies on +// enforce_nonce inside Discord's window. A failure after the POST was sent +// (network error, receipt completion, output) keeps exit 1, so the caller +// treats the outcome as unknown. +const NOT_SENT_EXIT = 75; +class NotSentError extends Error {} +class DiscordRejection extends Error {} + async function sendDiscordDm(options) { + const state = { postStarted: false }; + try { + await deliverDiscordDm(options, state); + } catch (error) { + const message = error instanceof Error ? error.message : String(error); + throw state.postStarted && !(error instanceof DiscordRejection) ? error : new NotSentError(message); + } +} + +async function deliverDiscordDm(options, state) { ensureBridgeReceiptGate(options); const token = process.env.BIFROST_DISCORD_BOT_TOKEN ?? process.env.DISCORD_BOT_TOKEN; const recipientId = requireOption(options, "recipient-id"); const content = await readOptionText(options, "content", "content-file"); + const nonce = optionalString(options.nonce); + if (options.nonce !== undefined && (!nonce || nonce.length > 25)) { + throw new Error("--nonce must be 1 to 25 characters."); + } const dryRun = options["dry-run"] === "true"; if (dryRun) { @@ -506,7 +531,8 @@ async function sendDiscordDm(options) { throw new Error("Set BIFROST_DISCORD_BOT_TOKEN or DISCORD_BOT_TOKEN before sending a Discord DM."); } channelId = await openDiscordDmChannel(token, recipientId); - result = await postDiscordBotMessage(token, channelId, content, undefined); + state.postStarted = true; + result = await postDiscordBotMessage(token, channelId, content, undefined, nonce); await bridgeAction?.complete({ receiptUrl: `https://discord.com/channels/@me/${channelId}/${result.id}`, externalReceiptId: result.id, @@ -755,7 +781,7 @@ async function openDiscordDmChannel(token, recipientId) { return channel.id; } -async function postDiscordBotMessage(token, channelId, content, replyToMessageId) { +async function postDiscordBotMessage(token, channelId, content, replyToMessageId, nonce) { const response = await fetch(`https://discord.com/api/v10/channels/${channelId}/messages`, { method: "POST", headers: { @@ -773,12 +799,13 @@ async function postDiscordBotMessage(token, channelId, content, replyToMessageId allowed_mentions: { parse: [], }, + ...(nonce ? { nonce, enforce_nonce: true } : {}), }), }); const text = await response.text(); if (!response.ok) { - throw new Error(`Discord post failed with ${response.status}: ${text}`); + throw new DiscordRejection(`Discord post failed with ${response.status}: ${text}`); } const message = JSON.parse(text); @@ -1445,5 +1472,5 @@ Provenance note: main().catch((error) => { process.stderr.write(`${error instanceof Error ? error.message : String(error)}\n`); - process.exitCode = 1; + process.exitCode = error instanceof NotSentError ? NOT_SENT_EXIT : 1; }); diff --git a/tools/watchdog-notice.mjs b/tools/watchdog-notice.mjs new file mode 100644 index 0000000..485a59d --- /dev/null +++ b/tools/watchdog-notice.mjs @@ -0,0 +1,283 @@ +#!/usr/bin/env node +// One-shot reader: turns Idunn's operator incidents into owner DMs. +// +// process --incident-store P --journal-store J --receipt-store R [--bridge-cli B] +// status --journal-store J +// +// Owner of the decision to send, the journal (J), the recipient +// (DISCORD_OWNER_ID) and the token (BIFROST_DISCORD_BOT_TOKEN, read only by the +// bridge it spawns). Idunn's store P is read unlocked and never written: no +// CultMesh node, no lock, only SingleFileMessagePackBackingStore.pullAll(). +// +// Requires CultLib 8cb3b728 or later (package @gamecult/cultcache-ts); see +// docs/watchdog-notice.md. +// +// Nothing here echoes an input value. Errors are fixed codes, and the +// journal's lastError is a fixed code, never bridge output. +import { createHash } from "node:crypto"; +import { spawnSync } from "node:child_process"; +import { createRequire } from "node:module"; +import { mkdtemp, rm, stat, writeFile } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { resolve } from "node:path"; + +const root = resolve(import.meta.dirname, ".."); +const cult = resolve(process.env.VOIDBOT_CULTLIB_ROOT || resolve(root, "..", "CultLib")); + +const INCIDENT_SCHEMA = "idunn.operator_incident.v1"; +const JOURNAL_TYPE = "bifrost.watchdog_notice_execution"; +const JOURNAL_SCHEMA = "bifrost.watchdog_notice_execution.v1"; +const MAX_ATTEMPTS = 5; +// The override exists so a test can reach the timeout branch without a 60 s wait. +const BRIDGE_TIMEOUT_MS = Number(process.env.WATCHDOG_NOTICE_BRIDGE_TIMEOUT_MS) || 60_000; +const STATUSES = ["running", "completed", "failed", "unknown"]; +const NAME = /^[a-z][a-z-]{0,63}$/; +// Idunn's require_id (GameCult/Idunn src/control_plane.rs:9122-9131 at 7f7528b): +// 1-256 bytes of [A-Za-z0-9-_.:/]. The notice puts the subject in inline code. +const SUBJECT = /^[A-Za-z0-9_.:/-]{1,256}$/; +const MAX_DATE_MS = 8.64e15; +// bifrost-bridge.mjs discord-dm exits 75 (EX_TEMPFAIL) for failures before +// the message POST and for any non-2xx answer to it: retried with the same nonce. +// Every other non-success leaves the outcome unknown. +const BRIDGE_NOT_SENT = 75; + +class NoticeError extends Error {} +const fail = (code) => new NoticeError(code); + +async function main(args) { + const [verb, ...rest] = args; + const options = parseArgs(rest); + if (verb === "status") return status(options); + if (verb === "process") return processIncidents(options); + throw fail("unknown-verb"); +} + +async function processIncidents(options) { + const incidentStore = requireOption(options, "incident-store"); + const journalStore = requireOption(options, "journal-store"); + const receiptStore = requireOption(options, "receipt-store"); + const bridgeCli = resolve(options["bridge-cli"] || resolve(root, "tools", "bifrost-bridge.mjs")); + const recipientId = (process.env.DISCORD_OWNER_ID ?? "").trim(); + if (!recipientId) throw fail("recipient-missing"); + + const runtime = loadRuntime(); + const { records, skipped, refused } = await readIncidents(runtime, incidentStore); + if (records === null) return 0; + + const journal = await openJournal(runtime, journalStore); + for (const entry of journal.cache.getAll(journal.definition)) { + if (entry.status === "running") { + await journal.put({ ...entry, status: "unknown", lastError: "interrupted-before-outcome", updatedAt: now() }); + } + } + + const deliver = async (record, notice) => { + const key = `${record.incidentKey}#${notice}`; + const previous = journal.cache.get(journal.definition, key); + const attempts = (previous?.attempts ?? 0) + 1; + const nonce = createHash("sha256").update(key).digest("hex").slice(0, 25); + const content = noticeText(record, notice); + const base = { schemaVersion: JOURNAL_SCHEMA, incidentKey: record.incidentKey, notice, attempts, nonce }; + await journal.put({ ...base, status: "running", messageId: "", lastError: "", updatedAt: now() }); + const outcome = await sendNotice({ bridgeCli, recipientId, receiptStore, key, nonce, record, content }); + await journal.put({ ...base, messageId: outcome.messageId ?? "", status: outcome.status, lastError: outcome.error ?? "", updatedAt: now() }); + }; + const retryable = (entry) => !entry || (entry.status === "failed" && entry.attempts < MAX_ATTEMPTS); + const entryFor = (record, notice) => journal.cache.get(journal.definition, `${record.incidentKey}#${notice}`); + + for (const record of records) { + if (retryable(entryFor(record, "opened"))) await deliver(record, "opened"); + if (record.closedAt !== null && entryFor(record, "opened")?.status === "completed" && retryable(entryFor(record, "closed"))) { + await deliver(record, "closed"); + } + } + + const entries = journal.cache.getAll(journal.definition); + const unknown = entries.filter((entry) => entry.status === "unknown").length; + const exhausted = entries.filter(isExhausted).length; + process.stdout.write(`${JSON.stringify({ skipped, refused, unknown, exhausted })}\n`); + return unknown + exhausted + refused > 0 ? 1 : 0; +} + +async function status(options) { + const runtime = loadRuntime(); + const journal = await openJournal(runtime, requireOption(options, "journal-store")); + const entries = journal.cache.getAll(journal.definition); + const rows = entries + .map(({ incidentKey, notice, status, attempts, updatedAt, lastError }) => ({ incidentKey, notice, status, attempts, updatedAt, lastError })) + .sort((a, b) => (a.incidentKey + a.notice).localeCompare(b.incidentKey + b.notice)); + process.stdout.write(`${JSON.stringify(rows, null, 2)}\n`); + return entries.some((entry) => entry.status === "unknown" || isExhausted(entry)) ? 1 : 0; +} + +const isExhausted = (entry) => entry.status === "failed" && entry.attempts >= MAX_ATTEMPTS; +const now = () => new Date().toISOString(); + +// Reads P unlocked. An absent file is "nothing to do" (records: null). A +// record of another schema is counted as skipped; a record of this schema +// that breaks the contract is counted as refused. Neither is echoed. +async function readIncidents(runtime, path) { + try { + await stat(path); + } catch (error) { + if (error?.code === "ENOENT") return { records: null, skipped: 0, refused: 0 }; + throw fail("incident-store-unreadable"); + } + let envelopes; + try { + envelopes = await new runtime.SingleFileMessagePackBackingStore(path).pullAll(); + } catch { + throw fail("incident-store-unreadable"); + } + const records = []; + let skipped = 0; + let refused = 0; + for (const envelope of envelopes) { + let tuple; + try { + tuple = runtime.decode(envelope.payload); + } catch { + skipped += 1; + continue; + } + if (!Array.isArray(tuple) || tuple[0] !== INCIDENT_SCHEMA) { + skipped += 1; + continue; + } + const record = parseIncident(tuple); + if (record) records.push(record); + else refused += 1; + } + records.sort((a, b) => a.openedAt - b.openedAt || a.incidentKey.localeCompare(b.incidentKey)); + return { records, skipped, refused }; +} + +// [schema_version, incident_key, condition, subject, opened_at, closed_at, close_reason] +function parseIncident(tuple) { + if (tuple.length !== 7) return null; + const [, incidentKey, condition, subject, openedAt, closedAt, closeReason] = tuple; + if (typeof condition !== "string" || !NAME.test(condition)) return null; + if (typeof subject !== "string" || !SUBJECT.test(subject)) return null; + if (!Number.isSafeInteger(openedAt) || openedAt <= 0 || openedAt > MAX_DATE_MS) return null; + if (incidentKey !== `${condition}:${subject}:${openedAt}`) return null; + if (closedAt === null) { + if (closeReason !== null) return null; + } else { + if (!Number.isSafeInteger(closedAt) || closedAt < openedAt || closedAt > MAX_DATE_MS) return null; + if (typeof closeReason !== "string" || !NAME.test(closeReason)) return null; + } + return { incidentKey, condition, subject, openedAt, closedAt, closeReason }; +} + +function noticeText(record, notice) { + const opened = new Date(record.openedAt).toISOString(); + if (notice === "opened") return `Idunn incident opened: ${record.condition} on \`${record.subject}\` at ${opened}`; + const closed = new Date(record.closedAt).toISOString(); + return `Idunn incident closed (${record.closeReason}): ${record.condition} on \`${record.subject}\`, opened ${opened}, closed ${closed}`; +} + +// The one send primitive. The outcome is completed with a message id; failed +// only when the bridge reports exit 75 (retryable, same nonce); otherwise unknown. The +// error is a fixed code: bridge stdout and stderr never reach the journal. +async function sendNotice({ bridgeCli, recipientId, receiptStore, key, nonce, record, content }) { + const dir = await mkdtemp(resolve(tmpdir(), "watchdog-notice-")); + try { + const contentFile = resolve(dir, "content.txt"); + await writeFile(contentFile, content, "utf8"); + const result = spawnSync(process.execPath, [ + bridgeCli, "discord-dm", + "--recipient-id", recipientId, + "--content-file", contentFile, + "--nonce", nonce, + "--cultmesh-command-id", key, + "--receipt-store", receiptStore, + "--source-kind", "idunn-operator-incident", + "--source-id", record.incidentKey, + ], { encoding: "utf8", env: process.env, windowsHide: true, timeout: BRIDGE_TIMEOUT_MS, stdio: ["ignore", "pipe", "ignore"] }); + if (result.status === BRIDGE_NOT_SENT) return { status: "failed", error: "bridge-not-sent" }; + if (result.error || result.status !== 0) return { status: "unknown", error: "bridge-exit-unknown" }; + try { + const value = JSON.parse(result.stdout); + if (value.action === "discord-dm" && value.ok === true && typeof value.messageId === "string" && value.messageId) { + return { status: "completed", messageId: value.messageId }; + } + } catch { + // falls through to the fixed code + } + return { status: "unknown", error: "bridge-output-invalid" }; + } finally { + await rm(dir, { recursive: true, force: true }); + } +} + +async function openJournal(runtime, path) { + const definition = runtime.defineDocumentType({ + type: JOURNAL_TYPE, + schemaName: JOURNAL_TYPE, + schemaId: JOURNAL_SCHEMA, + schemaVersion: JOURNAL_SCHEMA, + contentHash: JOURNAL_SCHEMA, + global: false, + schema: { parse: parseEntry }, + members: ["schemaVersion", "incidentKey", "notice", "status", "attempts", "nonce", "messageId", "lastError", "updatedAt"] + .map((memberName, slot) => ({ slot, memberName, typeName: memberName === "attempts" ? "uint32" : "string" })), + }); + const cache = runtime.CultCache.builder() + .withDocumentType(definition) + .withGenericStore(new runtime.SingleFileMessagePackBackingStore(path)) + .build(); + try { + await cache.pullAllBackingStores(); + } catch { + throw fail("journal-unreadable"); + } + return { cache, definition, put: (entry) => cache.put(definition, `${entry.incidentKey}#${entry.notice}`, entry) }; +} + +function parseEntry(value) { + const ok = value && value.schemaVersion === JOURNAL_SCHEMA + && typeof value.incidentKey === "string" && value.incidentKey + && (value.notice === "opened" || value.notice === "closed") + && STATUSES.includes(value.status) + && Number.isSafeInteger(value.attempts) && value.attempts >= 0 + && ["nonce", "messageId", "lastError", "updatedAt"].every((field) => typeof value[field] === "string"); + if (!ok) throw fail("journal-entry-invalid"); + return value; +} + +function loadRuntime() { + try { + const cache = createRequire(resolve(cult, "packages", "cultcache-ts", "package.json")); + const net = createRequire(resolve(cult, "packages", "cultnet-ts", "package.json")); + const cc = cache("@gamecult/cultcache-ts"); + return { + CultCache: cc.CultCache, + SingleFileMessagePackBackingStore: cc.SingleFileMessagePackBackingStore, + defineDocumentType: cc.defineDocumentType, + decode: net("@msgpack/msgpack").decode, + }; + } catch { + throw fail("cultlib-unavailable"); + } +} + +function parseArgs(args) { + const options = {}; + for (let i = 0; i < args.length; i += 2) { + if (!args[i].startsWith("--") || args[i + 1] === undefined) throw fail("bad-arguments"); + options[args[i].slice(2)] = args[i + 1]; + } + return options; +} + +function requireOption(options, name) { + if (!options[name]) throw fail(`missing-option-${name}`); + return options[name]; +} + +try { + process.exitCode = await main(process.argv.slice(2)); +} catch (error) { + process.stderr.write(`watchdog-notice: ${error instanceof NoticeError ? error.message : "unexpected-error"}\n`); + process.exitCode = 1; +}