From 26853a9dfe1859bcfba965c634be04b7fffc08ea Mon Sep 17 00:00:00 2001 From: Krzysztof kuhy Rudnicki Date: Thu, 19 Nov 2026 21:24:28 +0100 Subject: [PATCH] Skip phone deploy when the app is unchanged deploy.sh records the workout app's git tree after each phone install and skips the phone when the tree is the same, so a PC-only change deploys with the phone unplugged. No record means the phone is needed. Todo: 2556952d Co-Authored-By: Claude Opus 5.5 --- AGENTS.md | 2 +- screen_locker/tests/test_deploy_phone_skip.py | 114 ++++++++++++++++++ scripts/deploy.sh | 33 ++++- 3 files changed, 142 insertions(+), 7 deletions(-) create mode 100644 screen_locker/tests/test_deploy_phone_skip.py diff --git a/AGENTS.md b/AGENTS.md index bdf35d9..bad30a1 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -124,7 +124,7 @@ wait, and tests pin the retry to 0. - lint: `pre-commit run ruff --all-files` - coverage: `python -m pytest -n auto -p no:sugar -q` - coverage-gaps: `coverage-gaps coverage.lcov` -- deploy: `git -C ~/src/screen-locker pull --ff-only && bash ~/src/screen-locker/scripts/deploy.sh` (reinstalls, restarts web/poke/runnerup units, health-checks them, then release-deploys the workout app; exit 46 from the phone guard means kuhy reruns it with `PHONE_PROTECT_ALLOW=com.kuhy.workout_app`) +- deploy: `git -C ~/src/screen-locker pull --ff-only && bash ~/src/screen-locker/scripts/deploy.sh` (reinstalls, restarts web/poke/runnerup units, health-checks them, then release-deploys the workout app (skipped when its source tree matches the last phone install, `~/.local/state/screen-locker/phone_app_tree`); exit 46 from the phone guard means kuhy reruns it with `PHONE_PROTECT_ALLOW=com.kuhy.workout_app`) ## Pre-push gate (<=60 s) diff --git a/screen_locker/tests/test_deploy_phone_skip.py b/screen_locker/tests/test_deploy_phone_skip.py new file mode 100644 index 0000000..4331136 --- /dev/null +++ b/screen_locker/tests/test_deploy_phone_skip.py @@ -0,0 +1,114 @@ +"""scripts/deploy.sh --phone-only: skip the phone when its app is unchanged. + +A PC-only change (CI config, a Python pin) must deploy with the phone +unplugged: the workout app installed last time is byte-for-byte the same +source. The skip keys on the git tree of the app directory recorded after the +last successful phone install, so an app change is never skipped. +""" + +from __future__ import annotations + +import os +from pathlib import Path +import subprocess + +import pytest + +ROOT = Path(__file__).resolve().parents[2] +DEPLOY = ROOT / "scripts" / "deploy.sh" +APP_DIR = "stronglift_replacement/workout_app" +# conftest replaces subprocess.run process-wide for every test; this module +# needs the real one, captured at import time before any fixture runs. +_real_run = subprocess.run + + +def _git(repo: Path, *args: str) -> str: + """Run git in ``repo`` and return its stripped stdout.""" + env = {**os.environ, "GIT_CONFIG_GLOBAL": os.devnull} + env.pop("GIT_DIR", None) + out = _real_run( + ["git", "-C", str(repo), *args], + check=True, + capture_output=True, + text=True, + env=env, + ) + return out.stdout.strip() + + +@pytest.fixture(name="live") +def _live_repo(tmp_path: Path) -> Path: + """A throwaway 'live clone' with one committed app file.""" + repo = tmp_path / "live" + (repo / APP_DIR).mkdir(parents=True) + (repo / APP_DIR / "pubspec.yaml").write_text("name: workout_app\n") + _git(repo, "init", "-q") + _git(repo, "-c", "user.email=t@t", "-c", "user.name=t", "add", ".") + _git(repo, "-c", "user.email=t@t", "-c", "user.name=t", "commit", "-qm", "c") + return repo + + +def _fake_phone(tmp_path: Path, rc: int) -> tuple[Path, Path]: + """A stand-in phone_deploy.sh exiting ``rc``; returns (script, call log).""" + calls = tmp_path / "phone_calls" + script = tmp_path / "phone_deploy.sh" + script.write_text(f'#!/bin/bash\necho "$@" >> "{calls}"\nexit {rc}\n') + return script, calls + + +def _run(live: Path, phone: Path, marker: Path) -> subprocess.CompletedProcess[str]: + env = { + **os.environ, + "SCREEN_LOCKER_LIVE": str(live), + "PHONE_DEPLOY": str(phone), + "PHONE_APP_MARKER": str(marker), + } + env.pop("GIT_DIR", None) + return _real_run( + ["bash", str(DEPLOY), "--phone-only"], + check=False, + capture_output=True, + text=True, + env=env, + ) + + +def test_unchanged_app_skips_unreachable_phone(live: Path, tmp_path: Path) -> None: + """Recorded tree == current tree: exit 0 without touching the phone.""" + phone, calls = _fake_phone(tmp_path, 30) + marker = tmp_path / "state" / "phone_app_tree" + marker.parent.mkdir() + marker.write_text(_git(live, "rev-parse", f"HEAD:{APP_DIR}") + "\n") + result = _run(live, phone, marker) + assert not result.returncode, result.stderr + assert "unchanged" in result.stdout + assert not calls.exists() + + +def test_changed_app_still_needs_the_phone(live: Path, tmp_path: Path) -> None: + """A different recorded tree: phone_deploy runs and its exit 30 fails.""" + phone, calls = _fake_phone(tmp_path, 30) + marker = tmp_path / "phone_app_tree" + marker.write_text("0" * 40 + "\n") + result = _run(live, phone, marker) + assert result.returncode == 1 + assert calls.exists() + assert "exited 30" in result.stderr + + +def test_no_marker_fails_closed(live: Path, tmp_path: Path) -> None: + """No record of an install: the phone is required, not skipped.""" + phone, calls = _fake_phone(tmp_path, 30) + result = _run(live, phone, tmp_path / "missing" / "phone_app_tree") + assert result.returncode == 1 + assert calls.exists() + + +def test_install_records_the_tree(live: Path, tmp_path: Path) -> None: + """A successful install writes the app tree, so the next deploy can skip.""" + phone, calls = _fake_phone(tmp_path, 0) + marker = tmp_path / "state" / "phone_app_tree" + result = _run(live, phone, marker) + assert not result.returncode, result.stderr + assert calls.exists() + assert marker.read_text().strip() == _git(live, "rev-parse", f"HEAD:{APP_DIR}") diff --git a/scripts/deploy.sh b/scripts/deploy.sh index 896adec..afb3597 100755 --- a/scripts/deploy.sh +++ b/scripts/deploy.sh @@ -2,8 +2,6 @@ # Deploy screen-locker for real: update the live clone, reinstall, restart the # long-running user units, prove they are healthy, then ship the workout app. # -# Usage: bash scripts/deploy.sh [--health-only] -# # Every path is anchored on the LIVE clone (~/src/screen-locker), never on this # script's own directory: install.sh installs editable from wherever it sits, # so running a worktree copy of it would point the services at scratch code. @@ -16,7 +14,16 @@ # closed. This script never sets that variable; it passes the caller's # environment through and fails loudly when the guard refuses. # -# Seams: SCREEN_LOCKER_LIVE (live clone), DEPLOY_WEB_PORT (health port). +# The phone step is skipped when the workout app's source is unchanged since +# the last successful phone install: a PC-only change (CI config, a Python pin) +# then deploys with the phone unplugged. The key is the git tree of APP_DIR, +# written to PHONE_APP_MARKER after each install; no marker means the phone is +# required (fail closed). Any app change gives a new tree, so it is never skipped. +# +# Usage: bash scripts/deploy.sh [--health-only | --phone-only] +# +# Seams: SCREEN_LOCKER_LIVE (live clone), DEPLOY_WEB_PORT (health port), +# PHONE_DEPLOY (installer), PHONE_APP_MARKER (last installed app tree). # Exit: 0 healthy and installed everywhere | 1 any step or check failed. set -euo pipefail @@ -24,7 +31,8 @@ set -euo pipefail readonly LIVE="${SCREEN_LOCKER_LIVE:-$HOME/src/screen-locker}" readonly PY="/usr/bin/python3" readonly APP_DIR="stronglift_replacement/workout_app" -readonly PHONE_DEPLOY="$HOME/.claude/scripts/phone_deploy.sh" +readonly PHONE_DEPLOY="${PHONE_DEPLOY:-$HOME/.claude/scripts/phone_deploy.sh}" +readonly PHONE_APP_MARKER="${PHONE_APP_MARKER:-$HOME/.local/state/screen-locker/phone_app_tree}" readonly UNITS=(screen-locker-web.service workout-poke.service runnerup-watch.service) die() { echo "deploy: FAIL: $*" >&2; exit 1; } @@ -94,10 +102,19 @@ desktop() { } phone() { - local rc=0 + local rc=0 tree + tree="$(git -C "$LIVE" rev-parse "HEAD:$APP_DIR")" || die "no $APP_DIR tree at $LIVE HEAD" + if [[ "$(cat "$PHONE_APP_MARKER" 2>/dev/null || true)" == "$tree" ]]; then + ok "workout app unchanged since the last phone install (tree ${tree:0:10}); phone step skipped" + return 0 + fi bash "$PHONE_DEPLOY" "$LIVE/$APP_DIR" --release || rc=$? case "$rc" in - 0) ok "workout app release build installed on the phone" ;; + 0) + mkdir -p "$(dirname "$PHONE_APP_MARKER")" + printf '%s\n' "$tree" > "$PHONE_APP_MARKER" + ok "workout app release build installed on the phone (tree ${tree:0:10})" + ;; 46) die "phone_deploy protect guard (exit 46) refused com.kuhy.workout_app;" \ "kuhy runs: PHONE_PROTECT_ALLOW=com.kuhy.workout_app bash $LIVE/scripts/deploy.sh" \ "with the workout app closed" ;; @@ -105,6 +122,10 @@ phone() { esac } +if [[ "${1:-}" == "--phone-only" ]]; then + phone + exit 0 +fi if [[ "${1:-}" == "--health-only" ]]; then health exit 0