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
127 changes: 93 additions & 34 deletions pm-stack/ready-for-agent/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
# Install/refresh ready-for-agent (pm-stack Phase 2, dispatch layer)
# on this host.
#
# ./install.sh install pinned package + unit, (re)start,
# wait for the UI, print status
# ./install.sh install pinned package + unit, restart on
# change (or start when inactive), wait for the UI
# ./install.sh clone additionally clone (or fetch) the
# repositories the harness serves into
# ./repos/
Expand All @@ -27,74 +27,133 @@ set -euo pipefail
PINNED_VERSION="1.1.0"

cd "$(dirname "$0")"
DEPLOY_DIR="$PWD"
DEPLOY_DIR="${PWD}"

# Capture the serving process before changing its executable or unit.
service_was_active=false
previous_main_pid=""
if systemctl --user is-active --quiet ready-for-agent.service; then
service_was_active=true
previous_main_pid="$(systemctl --user show ready-for-agent.service \
--property=MainPID --value 2>/dev/null || true)"
fi
previous_start="$(systemctl --user show ready-for-agent.service \
--property=ExecMainStartTimestampMonotonic --value 2>/dev/null || true)"

# 1. Pinned npm package into the user prefix (~/.local). The system
# npm prefix on this host is the read-only Nix store.
current=""
if [[ -x $HOME/.local/bin/ready-for-agent ]]; then
current="$("$HOME/.local/bin/ready-for-agent" --version 2>/dev/null | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | head -1 || true)"
if [[ -x ${HOME}/.local/bin/ready-for-agent ]]; then
current="$("${HOME}/.local/bin/ready-for-agent" --version 2>/dev/null | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | head -1 || true)"
fi
if [[ $current != "$PINNED_VERSION" ]]; then
echo "installing ready-for-agent@$PINNED_VERSION (found: ${current:-none})"
npm install -g --prefix "$HOME/.local" "ready-for-agent@$PINNED_VERSION"
package_changed=false
if [[ ${current} != "${PINNED_VERSION}" ]]; then
package_changed=true
echo "installing ready-for-agent@${PINNED_VERSION} (found: ${current:-none})"
npm install -g --prefix "${HOME}/.local" "ready-for-agent@${PINNED_VERSION}"
else
echo "ready-for-agent@$PINNED_VERSION already installed"
echo "ready-for-agent@${PINNED_VERSION} already installed"
fi

# 2. Repository clones the harness works from (full clones — the shape
# the first deployment verified; see the registration note above).
# Only with the explicit `clone` argument — cloning is a one-time,
# network-heavy step.
if [[ ${1:-} == clone ]]; then
mkdir -p "$DEPLOY_DIR/repos"
if [[ -d $DEPLOY_DIR/repos/code ]]; then
git -C "$DEPLOY_DIR/repos/code" fetch --all --prune
mkdir -p "${DEPLOY_DIR}/repos"
if [[ -d ${DEPLOY_DIR}/repos/code ]]; then
git -C "${DEPLOY_DIR}/repos/code" fetch --all --prune
echo "fetched code"
else
GIT_TERMINAL_PROMPT=0 git clone \
https://github.com/developing-today/code.git "$DEPLOY_DIR/repos/code"
https://github.com/developing-today/code.git "${DEPLOY_DIR}/repos/code"
echo "cloned code"
fi
fi

# 3. systemd user unit.
installed_version="$("$HOME/.local/bin/ready-for-agent" --version 2>/dev/null | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | head -1 || true)"
if [[ $installed_version != "$PINNED_VERSION" ]]; then
echo "ERROR: installed ready-for-agent version is '${installed_version:-unknown}', expected '$PINNED_VERSION'" >&2
installed_version="$("${HOME}/.local/bin/ready-for-agent" --version 2>/dev/null | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | head -1 || true)"
if [[ ${installed_version} != "${PINNED_VERSION}" ]]; then
echo "ERROR: installed ready-for-agent version is '${installed_version:-unknown}', expected '${PINNED_VERSION}'" >&2
exit 1
fi

# This upstream service has no version-bearing health endpoint. Record its
# current start marker, then restart unconditionally after installing the
# package and unit. A new systemd process start plus the pinned launcher
# version proves the UI is served by the just-installed package, not an old
# process that happens to answer on the port.
previous_start="$(systemctl --user show ready-for-agent.service \
--property=ExecMainStartTimestampMonotonic --value 2>/dev/null || true)"
install -Dm644 systemd/ready-for-agent.service \
"$HOME/.config/systemd/user/ready-for-agent.service"
systemctl --user daemon-reload
# This upstream service has no version-bearing health endpoint. Only replace
# the unit when its content changed, so an unchanged install is a true
# no-restart path.
source_unit="${DEPLOY_DIR}/systemd/ready-for-agent.service"
installed_unit="${HOME}/.config/systemd/user/ready-for-agent.service"
unit_changed=false
if ! cmp -s "${source_unit}" "${installed_unit}"; then
unit_changed=true
install -Dm644 "${source_unit}" "${installed_unit}"
fi

service_changed=false
if [[ ${package_changed} == true || ${unit_changed} == true ]]; then
service_changed=true
fi

if [[ ${unit_changed} == true ]]; then
systemctl --user daemon-reload
fi
systemctl --user enable ready-for-agent.service
systemctl --user restart ready-for-agent.service
if [[ ${service_was_active} == true ]]; then
if [[ ${service_changed} == true ]]; then
systemctl --user restart ready-for-agent.service
service_action="restarted"
else
echo "no package or unit changes detected; leaving ready-for-agent.service running"
service_action="unchanged"
Comment on lines +105 to +107

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve pending restarts across failed installation attempts

If an active service is upgraded by install.sh clone but the subsequent Git clone/fetch fails, npm has already installed the pinned version while the old process remains running. On the next invocation, both on-disk comparisons match, so this branch skips the restart and reports the pinned version as running even though the old process still serves requests. This is reproducible with the stubbed harness by failing Git after npm succeeds; the retry succeeds without changing the service PID. Preserve a pending-restart marker until deployment completes, or otherwise verify that the running process corresponds to the installed package before taking the no-op path.

Useful? React with 👍 / 👎.

fi
else
systemctl --user start ready-for-agent.service
service_action="started"
fi

echo "waiting for the harness UI..."
for i in $(seq 1 30); do
for ((i = 1; i <= 30; i++)); do
current_start="$(systemctl --user show ready-for-agent.service \
--property=ExecMainStartTimestampMonotonic --value 2>/dev/null || true)"
if [[ -n $current_start && $current_start != 0 && $current_start != "$previous_start" ]] \
current_main_pid="$(systemctl --user show ready-for-agent.service \
--property=MainPID --value 2>/dev/null || true)"
process_verified=false
if [[ -n ${current_start} && ${current_start} != 0 && -n ${current_main_pid} && ${current_main_pid} != 0 ]]; then
if [[ ${service_was_active} == true && ${service_changed} == true ]]; then
# A required restart must replace the serving process, not merely make
# the unchanged UI endpoint answer successfully.
if [[ ${current_start} != "${previous_start}" && ${current_main_pid} != "${previous_main_pid}" ]]; then
process_verified=true
fi
elif [[ ${service_was_active} == false ]]; then
# Starting an inactive unit is itself the process transition. If
# systemd retained an older start marker, require it to advance too.
if [[ -z ${previous_start} || ${current_start} != "${previous_start}" ]]; then
process_verified=true
fi
else
# No changes were detected, so an existing active process is expected.
process_verified=true
fi
fi
if [[ ${process_verified} == true ]] \
&& systemctl --user is-active --quiet ready-for-agent.service \
&& curl --fail --silent http://127.0.0.1:6056/ >/dev/null; then
running_version="$("$HOME/.local/bin/ready-for-agent" --version 2>/dev/null | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | head -1 || true)"
if [[ $running_version != "$PINNED_VERSION" ]]; then
echo "ERROR: service restarted, but installed ready-for-agent version is '${running_version:-unknown}', expected '$PINNED_VERSION'" >&2
running_version="$("${HOME}/.local/bin/ready-for-agent" --version 2>/dev/null | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | head -1 || true)"
if [[ ${running_version} != "${PINNED_VERSION}" ]]; then
echo "ERROR: service restarted, but installed ready-for-agent version is '${running_version:-unknown}', expected '${PINNED_VERSION}'" >&2
exit 1
fi
echo "ready-for-agent@$running_version is running after restart: http://127.0.0.1:6056"
case "${service_action}" in
restarted) echo "ready-for-agent@${running_version} is running after restart: http://127.0.0.1:6056" ;;
started) echo "ready-for-agent@${running_version} is running after start: http://127.0.0.1:6056" ;;
unchanged) echo "ready-for-agent@${running_version} is already running (no restart): http://127.0.0.1:6056" ;;
*) echo "ERROR: unknown service action '${service_action}'" >&2; exit 1 ;;
esac
systemctl --user --no-pager status ready-for-agent.service | head -5
exit 0
fi
sleep 5
done
echo "ready-for-agent did not restart on the installed version and serve the UI; check: journalctl --user -u ready-for-agent" >&2
echo "ready-for-agent did not reach the expected process state (${service_action}) and serve the UI; check: journalctl --user -u ready-for-agent" >&2
exit 1
154 changes: 154 additions & 0 deletions pm-stack/ready-for-agent/test-install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
#!/usr/bin/env bash
# Exercise install.sh with isolated HOME and stubbed systemctl/npm/curl.
set -euo pipefail

REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
ORIGINAL_PATH="${PATH}"
TEST_ROOT="$(mktemp -d)"
trap 'rm -rf "${TEST_ROOT}"' EXIT

fail() {
echo "FAIL: $*" >&2
exit 1
}

run_case() {
local name="$1" initial_version="$2" active="$3" unit_matches="$4"
local case_root="${TEST_ROOT}/${name}"
local home="${case_root}/home" deploy="${case_root}/deploy"
local stub_bin="${case_root}/bin" state="${case_root}/state"
local log="${case_root}/systemctl.log" output

mkdir -p "${home}/.local/bin" "${home}/.config/systemd/user" "${deploy}/systemd" "${stub_bin}" "${state}"
cp "${REPO_ROOT}/pm-stack/ready-for-agent/install.sh" "${deploy}/install.sh"
cp "${REPO_ROOT}/pm-stack/ready-for-agent/systemd/ready-for-agent.service" \
"${deploy}/systemd/ready-for-agent.service"
if [[ "${unit_matches}" == true ]]; then
cp "${deploy}/systemd/ready-for-agent.service" "${home}/.config/systemd/user/ready-for-agent.service"
elif [[ "${unit_matches}" == false ]]; then
echo 'stale unit' > "${home}/.config/systemd/user/ready-for-agent.service"
fi

if [[ "${initial_version}" != missing ]]; then
printf '%s\n' "${initial_version}" > "${home}/.ready-agent-version"
cat > "${home}/.local/bin/ready-for-agent" <<'LAUNCHER'
#!/usr/bin/env bash
printf 'ready-for-agent %s\n' "$(cat "${HOME}/.ready-agent-version")"
LAUNCHER
chmod +x "${home}/.local/bin/ready-for-agent"
fi

if [[ "${active}" == active ]]; then
printf 'active\n' > "${state}/active"
printf '100\n' > "${state}/pid"
printf '1000\n' > "${state}/start"
else
printf 'inactive\n' > "${state}/active"
printf '0\n' > "${state}/pid"
printf '0\n' > "${state}/start"
fi
: > "${log}"

cat > "${stub_bin}/systemctl" <<'SYSTEMCTL'
#!/usr/bin/env bash
set -euo pipefail
[[ ${1:-} == --user ]] || exit 2
shift
while [[ ${1:-} == --no-pager ]]; do shift; done
command="${1:-}"
shift || true
case "${command}" in
is-active)
[[ $(cat "${STUB_STATE}/active") == active ]]
;;
show)
property=""
for arg in "$@"; do
case "${arg}" in
--property=*) property="${arg#--property=}" ;;
esac
done
case "${property}" in
MainPID) cat "${STUB_STATE}/pid" ;;
ExecMainStartTimestampMonotonic) cat "${STUB_STATE}/start" ;;
*) exit 2 ;;
esac
;;
daemon-reload|enable)
printf '%s\n' "${command}" >> "${STUB_LOG}"
;;
restart|start)
printf '%s\n' "${command}" >> "${STUB_LOG}"
printf 'active\n' > "${STUB_STATE}/active"
printf '%s\n' "$(( $(cat "${STUB_STATE}/pid") + 1 ))" > "${STUB_STATE}/pid"
printf '%s\n' "$(( $(cat "${STUB_STATE}/start") + 1000 ))" > "${STUB_STATE}/start"
;;
status)
printf 'Active: active (running)\nMain PID: %s\n' "$(cat "${STUB_STATE}/pid")"
;;
*) echo "unexpected systemctl command: ${command}" >&2; exit 2 ;;
esac
SYSTEMCTL

cat > "${stub_bin}/npm" <<'NPM'
#!/usr/bin/env bash
set -euo pipefail
printf 'npm install\n' >> "${STUB_LOG}"
printf '1.1.0\n' > "${HOME}/.ready-agent-version"
cat > "${HOME}/.local/bin/ready-for-agent" <<'LAUNCHER'
#!/usr/bin/env bash
printf 'ready-for-agent %s\n' "$(cat "${HOME}/.ready-agent-version")"
LAUNCHER
chmod +x "${HOME}/.local/bin/ready-for-agent"
NPM
cat > "${stub_bin}/curl" <<'CURL'
#!/usr/bin/env bash
exit 0
CURL
cat > "${stub_bin}/sleep" <<'SLEEP'
#!/usr/bin/env bash
exit 0
SLEEP
chmod +x "${stub_bin}/systemctl" "${stub_bin}/npm" "${stub_bin}/curl" "${stub_bin}/sleep"

if ! output="$(HOME="${home}" PATH="${stub_bin}:${ORIGINAL_PATH}" STUB_STATE="${state}" STUB_LOG="${log}" \
bash "${deploy}/install.sh" 2>&1)"; then
printf '%s\n' "${output}" >&2
fail "${name}: installer exited unsuccessfully"
fi

printf '%s\n' "${output}" | grep -Fq 'ready-for-agent@1.1.0'
case "${name}" in
package-change-active)
grep -Fxq restart "${log}" || fail "${name}: expected restart"
grep -Fxq 'npm install' "${log}" || fail "${name}: expected package install"
;;
unit-change-active)
grep -Fxq restart "${log}" || fail "${name}: expected restart"
grep -Fxq daemon-reload "${log}" || fail "${name}: expected daemon-reload"
;;
unchanged-active)
! grep -Fxq restart "${log}" || fail "${name}: unexpectedly restarted"
! grep -Fxq start "${log}" || fail "${name}: unexpectedly started"
printf '%s\n' "${output}" | grep -Fq 'no restart' || fail "${name}: missing no-op report"
;;
inactive)
grep -Fxq start "${log}" || fail "${name}: expected start"
! grep -Fxq restart "${log}" || fail "${name}: unexpectedly restarted"
;;
first-install)
grep -Fxq start "${log}" || fail "${name}: expected start"
grep -Fxq daemon-reload "${log}" || fail "${name}: expected daemon-reload"
grep -Fxq 'npm install' "${log}" || fail "${name}: expected package install"
! grep -Fxq restart "${log}" || fail "${name}: unexpectedly restarted"
;;
*) fail "unknown test case: ${name}" ;;
esac
echo "PASS: ${name}"
}

run_case package-change-active 1.0.0 active true
run_case unit-change-active 1.1.0 active false
run_case unchanged-active 1.1.0 active true
run_case inactive 1.1.0 inactive true
run_case first-install missing inactive missing
Loading