From 27d19d35a3ee7d6816666ecf77137965ba167e3e Mon Sep 17 00:00:00 2001 From: repo-worker-code-k Date: Tue, 10 Nov 2026 15:27:48 -0600 Subject: [PATCH] fix(watchdog): alert when a wrong build holds the opencode port (code#8255) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The dark-port watchdog (code#7780) treats any serving port as healthy, so on 2026-11-10 it stayed silent while a two-generations-old build held port 13420 for ~2 days and generation switches reported success. Add a build-identity assertion: when opencode.service is active, the watchdog reads the registered service version from /api/info (credentials from the channel registration file) and compares it with services.opencode-watchdog.expectedVersion, which home/common derives from the name of the same cascade package the unit runs — expectation and ExecStart resolve from one derivation. A mismatch held past the grace window alerts on the same surface as the dark case (the watchdog unit exits 1) but never restarts a serving unit: replacing a wrong build is the switch procedure's deliberate act, and the watchdog must never race a handover. An unreadable registration or probe is not evidence; only a positively different version counts. Test hooks: OPENCODE_WATCHDOG_EXPECTED_VERSION / OPENCODE_WATCHDOG_REGISTRATION_FILE. --- home/common/default.nix | 13 ++++ nixos/services/opencode-watchdog/default.nix | 76 +++++++++++++++++++- 2 files changed, 87 insertions(+), 2 deletions(-) diff --git a/home/common/default.nix b/home/common/default.nix index 8158606a33..e17043d7d1 100644 --- a/home/common/default.nix +++ b/home/common/default.nix @@ -129,6 +129,16 @@ let # flake's own packages..opencode). Named once here: the # wrapper below execs it, and the wrapped package joins over it. opencodeCascade = inputs.openchamber.inputs.opencode.packages.${system}.opencode; + + # The build the watchdog's build-identity check (code#8255) expects + # on the port — read from the name of the very package derivation + # the unit runs (opencodeCascade), so expectation and ExecStart + # resolve from one derivation and cannot drift apart. + watchdogExpectedVersion = + let + m = builtins.match "opencode-(.*)" inputs.openchamber.inputs.opencode.packages.${system}.opencode.name; + in + if m == null then "" else builtins.head m; # Drew's opencode wrapper -- formerly a standalone home.packages # entry (writeShellScriptBin "opencode"), logic unchanged: # Cloudflare env top-up and the stamp-gated sync-models @@ -1103,6 +1113,9 @@ in # inherits them — the mcpx daemon receives them the same way.) }; + + services.opencode-watchdog.expectedVersion = watchdogExpectedVersion; + services.opencode = { # enable comes from the cascade (mkDefault at each layer). # diff --git a/nixos/services/opencode-watchdog/default.nix b/nixos/services/opencode-watchdog/default.nix index fc5db88e58..501ec841d6 100644 --- a/nixos/services/opencode-watchdog/default.nix +++ b/nixos/services/opencode-watchdog/default.nix @@ -20,6 +20,21 @@ # dark for less than graceSeconds -> wait (restart/handover # windows are legitimately # dark for a few seconds) +# +# Build identity (code#8255): the dark-port probe cannot see a WRONG +# build holding the port — on 2026-11-10 the port served a +# two-generations-old build for ~2 days while generation switches +# reported success, because a service-mode starter silently defers to +# any registered incumbent. So when the unit is active, the watchdog +# also compares the build the registered service reports (/api/info) +# against the build baked into this watchdog's own generation (its +# sibling opencode package). A mismatch is NOT auto-repaired — the +# watchdog never restarts a serving unit (the handover semantics +# above); it alerts on the same surface as the dark case (this unit +# exits 1 until a healthy run) once the mismatch has held for the +# grace window, and the switch procedure performs the replacement. +# Test hooks: OPENCODE_WATCHDOG_EXPECTED_VERSION / +# OPENCODE_WATCHDOG_REGISTRATION_FILE override the baked values. # dark continuously >= graceSeconds -> alert (journal, and this # unit exits 1 so it shows # as failed until the next @@ -46,15 +61,60 @@ }: let cfg = config.services.opencode-watchdog; + registrationFile = "\${XDG_STATE_HOME:-\$HOME/.local/state}/opencode/service-${cfg.channel}.json"; watchdog = pkgs.writeShellApplication { name = "opencode-watchdog"; - runtimeInputs = [ pkgs.systemd ]; + runtimeInputs = [ + pkgs.systemd + pkgs.curl + pkgs.jq + ]; text = '' unit="''${OPENCODE_WATCHDOG_UNIT:-opencode.service}" port="''${OPENCODE_WATCHDOG_PORT:-${toString cfg.port}}" grace="''${OPENCODE_WATCHDOG_GRACE_SECONDS:-${toString cfg.graceSeconds}}" state_dir="''${OPENCODE_WATCHDOG_STATE_DIR:-''${XDG_STATE_HOME:-$HOME/.local/state}/opencode-watchdog}" stamp="$state_dir/first-dark-epoch" + expected="''${OPENCODE_WATCHDOG_EXPECTED_VERSION:-${cfg.expectedVersion}}" + registration="''${OPENCODE_WATCHDOG_REGISTRATION_FILE:-${registrationFile}}" + mismatch_stamp="$state_dir/first-mismatch-epoch" + + # Build identity (code#8255): when the unit is active, the + # registered service must report the unit's build. A mismatch + # held past the grace window alerts (exit 1) but never restarts: + # replacement is the switch procedure's deliberate act. An + # unreadable registration or probe is not evidence — only a + # positively different version counts. + if [ -n "$expected" ] && systemctl --user is-active --quiet "$unit"; then + password="$(jq -r '.password // empty' "$registration" 2>/dev/null || true)" + reported="" + if [ -n "$password" ]; then + reported="$(curl -fsS -m 5 -u "opencode:$password" "http://127.0.0.1:$port/api/info" 2>/dev/null | jq -r '.version // empty' 2>/dev/null || true)" + fi + if [ -n "$reported" ] && [ "$reported" != "$expected" ]; then + mkdir -p "$state_dir" + now="$(date +%s)" + first="" + if [ -f "$mismatch_stamp" ]; then + first="$(cat "$mismatch_stamp")" + fi + case "$first" in + "" | *[!0-9]*) + printf '%s\n' "$now" >"$mismatch_stamp" + echo "opencode-watchdog: registered service reports build '$reported' but $unit runs '$expected'; grace window (${toString cfg.graceSeconds}s default) started (code#8255)" >&2 + ;; + *) + held_for=$((now - first)) + if [ "$held_for" -ge "$grace" ]; then + echo "opencode-watchdog: ALERT: port $port is held by build '$reported' while $unit runs '$expected' for ''${held_for}s (>= ''${grace}s grace) — run the switch procedure to hand the port over (code#8255)" >&2 + exit 1 + fi + ;; + esac + else + rm -f "$mismatch_stamp" + fi + fi serving() { (exec 3<>"/dev/tcp/127.0.0.1/$port") 2>/dev/null @@ -108,6 +168,18 @@ in description = "Port the standing OpenCode server must serve on 127.0.0.1."; }; + expectedVersion = lib.mkOption { + type = lib.types.str; + default = ""; + description = "The build version the registered service must report (code#8255). Set from the same package derivation as the unit's ExecStart in home/common. Empty disables the build-identity check."; + }; + + channel = lib.mkOption { + type = lib.types.str; + default = "prod"; + description = "Service channel whose registration file (service-.json) the build-identity check reads."; + }; + graceSeconds = lib.mkOption { type = lib.types.ints.positive; default = 120; @@ -124,7 +196,7 @@ in config = lib.mkIf cfg.enable { home-manager.users.${cfg.user} = { systemd.user.services.opencode-watchdog = { - Unit.Description = "OpenCode watchdog — opencode.service active or port ${toString cfg.port} serving, else alert + restart after ${toString cfg.graceSeconds}s dark (code#7780)"; + Unit.Description = "OpenCode watchdog — opencode.service active or port ${toString cfg.port} serving, else alert + restart after ${toString cfg.graceSeconds}s dark (code#7780); alert if a wrong build holds the port (code#8255)"; Service = { Type = "oneshot"; TimeoutStartSec = 60;