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
2 changes: 2 additions & 0 deletions .github/workflows/nix-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 11 additions & 0 deletions home/common/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -1184,6 +1184,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" ''
Expand Down
50 changes: 50 additions & 0 deletions home/common/opencode-deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/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

# `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
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
107 changes: 107 additions & 0 deletions home/common/opencode-deploy.test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#!/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'
elif [[ "$*" == '--user is-system-running' ]]; then
printf '%s\n' "${SYSTEMD_STATE:-running}"
[[ "${SYSTEMD_STATE:-running}" == running ]]
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'

# 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'