diff --git a/README.md b/README.md index b446885..8934600 100644 --- a/README.md +++ b/README.md @@ -59,7 +59,9 @@ Local package surfaces: - Local VoidBot keepalive: `scripts\health-voidbot.cmd` and `scripts\restart-voidbot.cmd` - Operator escalation: Bifrost-owned CultMesh crossing; current compatibility - delivery may call `voidbot.operator-dm` command `owner.dm.send` + delivery calls `scripts\notify-idunn-operator-alarm.ps1`, which asks Bifrost + to publish a typed `gamecult.operator_dm_request.v1` CultMesh command document + only after Idunn raises an alarm ## Authority Map diff --git a/crates/idunn-daemon/src/main.rs b/crates/idunn-daemon/src/main.rs index 76033e0..d4c61d3 100644 --- a/crates/idunn-daemon/src/main.rs +++ b/crates/idunn-daemon/src/main.rs @@ -19,6 +19,7 @@ struct Options { name: String, health_command: Option, restart_command: Option, + operator_alarm_command: Option, enabled: bool, execute: bool, interval_seconds: Option, @@ -89,6 +90,7 @@ fn run_cycle(options: &Options) -> Result<()> { "Idunn raised operator alarm for {} through {}: {}", alarm.daemon_id, alarm.escalation_target, alarm.reason ); + run_operator_alarm_command(&options, &alarm); } } else { println!( @@ -104,6 +106,7 @@ fn run_cycle(options: &Options) -> Result<()> { "Idunn raised operator alarm for {} through {}: {}", alarm.daemon_id, alarm.escalation_target, alarm.reason ); + run_operator_alarm_command(options, alarm); } println!( @@ -140,6 +143,7 @@ impl Options { name: String::new(), health_command: None, restart_command: None, + operator_alarm_command: None, enabled: true, execute: false, interval_seconds: None, @@ -158,6 +162,10 @@ impl Options { "--restart-command" => { options.restart_command = Some(take_value(&mut args, "--restart-command")?) } + "--operator-alarm-command" => { + options.operator_alarm_command = + Some(take_value(&mut args, "--operator-alarm-command")?) + } "--disabled" => options.enabled = false, "--execute" => options.execute = true, "--interval-seconds" => { @@ -269,6 +277,52 @@ fn run_shell(command: &str) -> Result { .with_context(|| format!("running command {command:?}")) } +fn run_operator_alarm_command(options: &Options, alarm: &IdunnOperatorAlarmRecord) { + let Some(command) = options.operator_alarm_command.as_deref() else { + return; + }; + if command.trim().is_empty() { + return; + } + + let mut process = if cfg!(windows) { + let mut process = Command::new("cmd"); + process.arg("/C").arg(command); + process + } else { + let mut process = Command::new("sh"); + process.arg("-c").arg(command); + process + }; + + let output = process + .env("IDUNN_ALARM_ID", &alarm.alarm_id) + .env("IDUNN_ALARM_DAEMON_ID", &alarm.daemon_id) + .env("IDUNN_ALARM_SEVERITY", &alarm.severity) + .env("IDUNN_ALARM_REASON", &alarm.reason) + .env("IDUNN_ALARM_ESCALATION_TARGET", &alarm.escalation_target) + .env("IDUNN_ALARM_RAISED_AT", &alarm.raised_at) + .output(); + + match output { + Ok(output) if output.status.success() => { + println!("Idunn operator alarm command completed for {}.", alarm.daemon_id); + } + Ok(output) => { + eprintln!( + "Idunn operator alarm command failed for {} with {}.", + alarm.daemon_id, output.status + ); + } + Err(error) => { + eprintln!( + "Idunn operator alarm command could not run for {}: {}", + alarm.daemon_id, error + ); + } + } +} + fn timestamp() -> Result { let seconds = SystemTime::now() .duration_since(UNIX_EPOCH) @@ -278,5 +332,5 @@ fn timestamp() -> Result { } fn help_text() -> &'static str { - "Usage: idunn --daemon [--name ] [--verse ] [--store ] [--health-command ] [--restart-command ] [--execute] [--interval-seconds ]\n\nIdunn probes one daemon, writes typed CultMesh records, and executes restart only when --execute is present." + "Usage: idunn --daemon [--name ] [--verse ] [--store ] [--health-command ] [--restart-command ] [--operator-alarm-command ] [--execute] [--interval-seconds ]\n\nIdunn probes one daemon, writes typed CultMesh records, executes restart only when --execute is present, and may invoke an operator alarm bridge command only after an alarm is raised." } diff --git a/docs/idunn.md b/docs/idunn.md index 81ac45b..7c8e569 100644 --- a/docs/idunn.md +++ b/docs/idunn.md @@ -50,6 +50,10 @@ Idunn now shares Odin's Rust body: operators, and daemon authors. - `scripts/start-idunn-local.ps1` starts resident local watchdogs, including VoidBot through `health-voidbot.cmd` and `restart-voidbot.cmd`. +- `scripts/notify-idunn-operator-alarm.ps1` is the local operator crossing: + Idunn invokes it only after raising an operator alarm, and it asks Bifrost to + publish a typed `gamecult.operator_dm_request.v1` CultMesh command document + instead of learning Discord delivery itself. - `npm run idunn:build` builds the Rust daemon. - `npm run idunn:start -- ...` probes a daemon, records the decision, and can execute the restart command when `--execute` is present. @@ -93,6 +97,11 @@ idunn.operator_alarm.v1 still-VoidBot delivery path must be invoked by Bifrost or documented as migration debt. Idunn must not learn Discord token handling, DM delivery internals, or VoidBot-specific transport. +- In the current local bridge, `--operator-alarm-command` invokes + `notify-idunn-operator-alarm.cmd`, which forwards `IDUNN_ALARM_*` + environment variables to `E:\Projects\Bifrost\tools\operator-notification.mjs + publish-idunn-alarm`. Bifrost publishes the typed CultMesh command document; + Idunn only decides that an alarm exists. ## First Runtime Direction diff --git a/scripts/notify-idunn-operator-alarm.cmd b/scripts/notify-idunn-operator-alarm.cmd new file mode 100644 index 0000000..33b17d2 --- /dev/null +++ b/scripts/notify-idunn-operator-alarm.cmd @@ -0,0 +1,2 @@ +@echo off +powershell.exe -NoProfile -ExecutionPolicy Bypass -File E:\Projects\Odin\scripts\notify-idunn-operator-alarm.ps1 diff --git a/scripts/notify-idunn-operator-alarm.ps1 b/scripts/notify-idunn-operator-alarm.ps1 new file mode 100644 index 0000000..76c51a3 --- /dev/null +++ b/scripts/notify-idunn-operator-alarm.ps1 @@ -0,0 +1,23 @@ +Set-StrictMode -Version Latest +$ErrorActionPreference = "Stop" + +$bifrostRoot = "E:\Projects\Bifrost" +$publisherScript = Join-Path $bifrostRoot "tools\operator-notification.mjs" + +if (-not (Test-Path -LiteralPath $publisherScript)) { + throw "Bifrost operator-notification publisher is missing at $publisherScript." +} + +Push-Location -LiteralPath $bifrostRoot +try { + $arguments = @($publisherScript, "publish-idunn-alarm") + if ($env:IDUNN_OPERATOR_ALARM_DRY_RUN -in @("1", "true", "yes", "on")) { + $arguments += "--dry-run" + } + & node @arguments + if ($LASTEXITCODE -ne 0) { + throw "Bifrost operator-notification publisher exited with code $LASTEXITCODE." + } +} finally { + Pop-Location +} diff --git a/scripts/start-idunn-local.ps1 b/scripts/start-idunn-local.ps1 index 23fdf27..fc0dfec 100644 --- a/scripts/start-idunn-local.ps1 +++ b/scripts/start-idunn-local.ps1 @@ -7,6 +7,7 @@ $ErrorActionPreference = "Stop" $repoRoot = Split-Path -Parent $PSScriptRoot $idunnExe = Join-Path $repoRoot "target\debug\idunn.exe" +$operatorAlarmCommand = Join-Path $repoRoot "scripts\notify-idunn-operator-alarm.cmd" $logDir = Join-Path $StateDir "logs" $pidDir = Join-Path $StateDir "pids" @@ -124,6 +125,9 @@ function Start-Watchdog { if (-not [string]::IsNullOrWhiteSpace($Watchdog.Restart)) { $arguments += @("--restart-command", $Watchdog.Restart, "--execute") } + if (Test-Path -LiteralPath $operatorAlarmCommand) { + $arguments += @("--operator-alarm-command", $operatorAlarmCommand) + } $startInfo.Arguments = Join-WindowsArguments -Arguments $arguments $process = [System.Diagnostics.Process]::new()