Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions home/common/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,16 @@ let
# flake's own packages.<system>.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
Expand Down Expand Up @@ -1103,6 +1113,9 @@ in
# inherits them — the mcpx daemon receives them the same way.)
};


services.opencode-watchdog.expectedVersion = watchdogExpectedVersion;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Set the expected version in the NixOS module scope

home/common is merged into home-manager.users.user, .root, and .backup by home/{user,root,backup}/default.nix, but services.opencode-watchdog.expectedVersion is declared in the NixOS module at nixos/services/opencode-watchdog/default.nix:156–174, not in Home Manager. This assignment therefore defines an unknown Home Manager option and prevents configurations importing this shared file from evaluating. It also does not populate the NixOS watchdog's cfg.expectedVersion, whose empty default disables the new identity check. Move the assignment to the NixOS configuration scope while retaining the same package-derived version.

Useful? React with 👍 / 👎.


services.opencode = {
# enable comes from the cascade (mkDefault at each layer).
#
Expand Down
76 changes: 74 additions & 2 deletions nixos/services/opencode-watchdog/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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-<channel>.json) the build-identity check reads.";
};

graceSeconds = lib.mkOption {
type = lib.types.ints.positive;
default = 120;
Expand All @@ -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;
Expand Down
Loading