From 16264824ba46bafecbdc238e130189b59736d3a4 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Sat, 21 Nov 2026 20:16:01 -0600 Subject: [PATCH 1/2] task-20671: avoid same-build opencode restart Disable automatic unit restarts for helper-only changes and deploy explicitly by the serving OpenCode store path. Seed the initial key from the active process command line. Add fake-systemctl regression tests. --- .github/workflows/nix-ci.yml | 2 + home/common/default.nix | 11 +++++ home/common/opencode-deploy.sh | 48 ++++++++++++++++++++ home/common/opencode-deploy.test.sh | 70 +++++++++++++++++++++++++++++ 4 files changed, 131 insertions(+) create mode 100755 home/common/opencode-deploy.sh create mode 100644 home/common/opencode-deploy.test.sh diff --git a/.github/workflows/nix-ci.yml b/.github/workflows/nix-ci.yml index 36acd5301d..fb5d7e589b 100644 --- a/.github/workflows/nix-ci.yml +++ b/.github/workflows/nix-ci.yml @@ -54,6 +54,8 @@ jobs: run: .github/scripts/check-openclaw-unit-wants.sh - name: Self-test the openclaw unit Wants guard run: .github/scripts/check-openclaw-unit-wants.test.sh + - name: Test OpenCode unchanged-build deployment + run: bash home/common/opencode-deploy.test.sh - name: Test the OpenClaw comment-loop pre-filter run: home/openclaw/comment-loop/pilot-loop-prefilter.test.sh diff --git a/home/common/default.nix b/home/common/default.nix index 474ba31c9f..80bdfafd91 100644 --- a/home/common/default.nix +++ b/home/common/default.nix @@ -1174,6 +1174,17 @@ in # EnvironmentFile at every server start — it is never stored in this # repo or the Nix store. The quota gatherer reads the same file (see # nixos/services/pm-stack) so its `opencode quota` refresh works too. + # Helper-only unit changes must not interrupt the server (#10639). + # Home Manager still reloads the unit; the activation below owns build + # changes explicitly, using only the serving package's store path as key. + systemd.user.services.opencode.Unit."X-RestartIfChanged" = false; + home.activation.opencodeDeploy = lib.hm.dag.entryAfter [ "reloadSystemd" ] '' + run ${pkgs.bash}/bin/bash ${./opencode-deploy.sh} \ + '${opencodeCascade}' \ + "''${XDG_STATE_HOME:-$HOME/.local/state}/opencode/deployed-build" \ + '${pkgs.systemd}/bin/systemctl' /proc + ''; + systemd.user.services.opencode.Service = { ExecStartPre = toString ( pkgs.writeShellScript "opencode-env-key-materialize" '' diff --git a/home/common/opencode-deploy.sh b/home/common/opencode-deploy.sh new file mode 100755 index 0000000000..66f4e7008d --- /dev/null +++ b/home/common/opencode-deploy.sh @@ -0,0 +1,48 @@ +#!/usr/bin/env bash +# Internal Home Manager activation helper. +# Usage: opencode-deploy.sh BUILD_KEY STATE_FILE SYSTEMCTL PROC_ROOT +# Parse the complete argument list before running systemctl or writing state. +set -euo pipefail + +if [[ $# -ne 4 ]]; then + echo "usage: $0 BUILD_KEY STATE_FILE SYSTEMCTL PROC_ROOT" >&2 + exit 2 +fi + +build_key=$1 +state_file=$2 +systemctl=$3 +proc_root=$4 +if [[ -z "$build_key" || -z "$state_file" || -z "$systemctl" || -z "$proc_root" ]]; then + echo "usage: $0 BUILD_KEY STATE_FILE SYSTEMCTL PROC_ROOT (arguments must be non-empty)" >&2 + exit 2 +fi + +previous_key="$(cat "$state_file" 2>/dev/null || true)" +if [[ "$previous_key" == "$build_key" ]]; then + exit 0 +fi + +# Do not record a key while the user manager is unavailable; a later activation +# must retry the deployment instead of treating an unperformed restart as done. +if "$systemctl" --user is-system-running --quiet; then + # On the first activation there is no saved deployment key. If the active + # process already names this exact store path, seed the key without restarting. + if [[ -z "$previous_key" ]]; then + pid="$("$systemctl" --user show --property=MainPID --value opencode.service)" + if [[ "$pid" =~ ^[1-9][0-9]*$ ]] && grep -zFq -- "$build_key" "$proc_root/$pid/cmdline"; then + mkdir -p "$(dirname "$state_file")" + printf '%s\n' "$build_key" >"$state_file" + exit 0 + fi + fi + + if "$systemctl" --user is-active --quiet opencode.service; then + "$systemctl" --user restart opencode.service + else + "$systemctl" --user start opencode.service + fi + + mkdir -p "$(dirname "$state_file")" + printf '%s\n' "$build_key" >"$state_file" +fi diff --git a/home/common/opencode-deploy.test.sh b/home/common/opencode-deploy.test.sh new file mode 100644 index 0000000000..3a8178350e --- /dev/null +++ b/home/common/opencode-deploy.test.sh @@ -0,0 +1,70 @@ +#!/usr/bin/env bash +# Regression test for same-build OpenCode deployment. Oracle: a fake +# systemctl invocation log must stay empty for an unchanged store-path key. +set -euo pipefail + +repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" +tmp="$(mktemp -d)" +trap 'rm -rf "$tmp"' EXIT + +systemctl="$tmp/systemctl" +log="$tmp/systemctl.log" +state="$tmp/deployed" +proc_root="$tmp/proc" +cat >"$systemctl" <<'EOF' +#!/usr/bin/env bash +printf '%s\n' "$*" >>"$SYSTEMCTL_LOG" +if [[ "$*" == '--user show --property=MainPID --value opencode.service' ]]; then + printf '4242\n' +fi +EOF +chmod +x "$systemctl" +export SYSTEMCTL_LOG="$log" + +printf '%s\n' '/nix/store/opencode-same-build' >"$state" + +"$repo_root/home/common/opencode-deploy.sh" \ + '/nix/store/opencode-same-build' "$state" "$systemctl" "$proc_root" + +if [[ -e "$log" ]]; then + echo "FAIL: unchanged OpenCode build called systemctl:" >&2 + cat "$log" >&2 + exit 1 +fi + +echo 'ok - unchanged store path does not invoke systemctl' + +# Bootstrap case: the marker does not exist yet, but the active process +# command line proves it already runs this store path. +rm -f "$state" +mkdir -p "$proc_root/4242" +printf '%s\0' '/nix/store/opencode-same-build/bin/opencode' 'serve' >"$proc_root/4242/cmdline" +: >"$log" +"$repo_root/home/common/opencode-deploy.sh" \ + '/nix/store/opencode-same-build' "$state" "$systemctl" "$proc_root" +if grep -Fxq -- '--user restart opencode.service' "$log"; then + echo 'FAIL: first activation restarted the already-matching build' >&2 + cat "$log" >&2 + exit 1 +fi +if [[ "$(cat "$state")" != '/nix/store/opencode-same-build' ]]; then + echo 'FAIL: first activation did not record the matching live build' >&2 + exit 1 +fi +echo 'ok - first activation recognizes the already-running build' + +: >"$log" +"$repo_root/home/common/opencode-deploy.sh" \ + '/nix/store/opencode-new-build' "$state" "$systemctl" "$proc_root" + +if ! grep -Fxq -- '--user restart opencode.service' "$log"; then + echo 'FAIL: changed OpenCode build did not restart the active service' >&2 + cat "$log" >&2 + exit 1 +fi +if [[ "$(cat "$state")" != '/nix/store/opencode-new-build' ]]; then + echo 'FAIL: changed OpenCode build key was not recorded' >&2 + exit 1 +fi + +echo 'ok - changed store path restarts and records the active service build' From a89901b5b433745bfdb7facc811aa41ec0cd8443 Mon Sep 17 00:00:00 2001 From: repo-worker-code Date: Sun, 22 Nov 2026 12:44:43 -0600 Subject: [PATCH 2/2] fix(opencode): deploy with degraded user manager --- home/common/opencode-deploy.sh | 8 ++++--- home/common/opencode-deploy.test.sh | 37 +++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/home/common/opencode-deploy.sh b/home/common/opencode-deploy.sh index 66f4e7008d..7e781085f8 100755 --- a/home/common/opencode-deploy.sh +++ b/home/common/opencode-deploy.sh @@ -23,9 +23,11 @@ if [[ "$previous_key" == "$build_key" ]]; then exit 0 fi -# Do not record a key while the user manager is unavailable; a later activation -# must retry the deployment instead of treating an unperformed restart as done. -if "$systemctl" --user is-system-running --quiet; then +# `is-system-running` exits nonzero for `degraded`, even though the manager can +# still control services. Do not record a key while it is unavailable; a later +# activation must retry instead of treating an unperformed restart as done. +manager_state="$("$systemctl" --user is-system-running 2>/dev/null || true)" +if [[ "$manager_state" == running || "$manager_state" == degraded ]]; then # On the first activation there is no saved deployment key. If the active # process already names this exact store path, seed the key without restarting. if [[ -z "$previous_key" ]]; then diff --git a/home/common/opencode-deploy.test.sh b/home/common/opencode-deploy.test.sh index 3a8178350e..00c631071d 100644 --- a/home/common/opencode-deploy.test.sh +++ b/home/common/opencode-deploy.test.sh @@ -16,6 +16,9 @@ cat >"$systemctl" <<'EOF' printf '%s\n' "$*" >>"$SYSTEMCTL_LOG" if [[ "$*" == '--user show --property=MainPID --value opencode.service' ]]; then printf '4242\n' +elif [[ "$*" == '--user is-system-running' ]]; then + printf '%s\n' "${SYSTEMD_STATE:-running}" + [[ "${SYSTEMD_STATE:-running}" == running ]] fi EOF chmod +x "$systemctl" @@ -68,3 +71,37 @@ if [[ "$(cat "$state")" != '/nix/store/opencode-new-build' ]]; then fi echo 'ok - changed store path restarts and records the active service build' + +# A degraded manager is still available for service control, even though +# systemctl reports a nonzero exit status for is-system-running. +: >"$log" +export SYSTEMD_STATE=degraded +"$repo_root/home/common/opencode-deploy.sh" \ + '/nix/store/opencode-degraded-manager-build' "$state" "$systemctl" "$proc_root" +if ! grep -Fxq -- '--user restart opencode.service' "$log"; then + echo 'FAIL: degraded manager did not deploy the changed build' >&2 + cat "$log" >&2 + exit 1 +fi +if [[ "$(cat "$state")" != '/nix/store/opencode-degraded-manager-build' ]]; then + echo 'FAIL: degraded-manager deployment key was not recorded' >&2 + exit 1 +fi +echo 'ok - degraded manager deploys and records the changed build' + +# An unavailable manager must not record the new key, so a later activation +# can retry the deployment. +: >"$log" +export SYSTEMD_STATE=offline +"$repo_root/home/common/opencode-deploy.sh" \ + '/nix/store/opencode-offline-manager-build' "$state" "$systemctl" "$proc_root" +if [[ "$(cat "$state")" != '/nix/store/opencode-degraded-manager-build' ]]; then + echo 'FAIL: unavailable manager recorded a deployment key' >&2 + exit 1 +fi +if grep -Fxq -- '--user restart opencode.service' "$log"; then + echo 'FAIL: unavailable manager attempted a service restart' >&2 + cat "$log" >&2 + exit 1 +fi +echo 'ok - unavailable manager skips without recording the build'