diff --git a/bin/omarchy-install-ai-hermes b/bin/omarchy-install-ai-hermes index 6b3764fd17b2..2721aca14737 100755 --- a/bin/omarchy-install-ai-hermes +++ b/bin/omarchy-install-ai-hermes @@ -35,11 +35,18 @@ export HERMES_HOME runtime="$HERMES_HOME/hermes-agent" native_app="$runtime/apps/desktop/release/linux-unpacked" -runtime_ready() { - [[ -f $runtime/.hermes-bootstrap-complete && -f $runtime/venv/bin/hermes && -x $runtime/venv/bin/hermes && -f $runtime/venv/bin/python && -x $runtime/venv/bin/python ]] && +# The venv commands the desktop and the readiness probe actually run. The +# bootstrap marker is separate: the packaged hermes-desktop install path +# Omarchy ships may leave it missing even when these binaries are usable. +runtime_commands_ready() { + [[ -f $runtime/venv/bin/hermes && -x $runtime/venv/bin/hermes && -f $runtime/venv/bin/python && -x $runtime/venv/bin/python ]] && timeout 15 "$runtime/venv/bin/hermes" --version >/dev/null 2>&1 } +runtime_ready() { + [[ -f $runtime/.hermes-bootstrap-complete ]] && runtime_commands_ready +} + check_main() { local main_commit main_commit=$(git -C "$runtime" rev-parse --verify refs/heads/main 2>/dev/null || true) @@ -49,6 +56,11 @@ check_main() { fi } +# Stamp a usable runtime that is only missing the marker the packaged +# hermes-desktop path may omit. Do not hand a dirty tree back to install.sh +# just to create that file. +HERMES_BOOTSTRAP_PINNED_COMMIT=$release_commit omarchy-stamp-hermes-bootstrap + if ! runtime_ready; then # The upstream installer can reset an existing checkout. Do not pin a newer # or modified runtime back to the package release while repairing setup. @@ -81,6 +93,7 @@ if ! runtime_ready; then echo "Setting up the Hermes runtime..." bash /usr/share/hermes-desktop/install.sh --skip-setup --branch main --commit "$release_commit" --force-commit --dir "$runtime" --hermes-home "$HERMES_HOME" + HERMES_BOOTSTRAP_PINNED_COMMIT=$release_commit omarchy-stamp-hermes-bootstrap if ! runtime_ready; then echo "Hermes runtime setup did not complete. Re-run this command after resolving the installer error." >&2 exit 1 diff --git a/bin/omarchy-stamp-hermes-bootstrap b/bin/omarchy-stamp-hermes-bootstrap new file mode 100755 index 000000000000..2504c9b862c5 --- /dev/null +++ b/bin/omarchy-stamp-hermes-bootstrap @@ -0,0 +1,62 @@ +#!/bin/bash + +# omarchy:summary=Stamp the Hermes Desktop bootstrap marker on a usable runtime +# omarchy:hidden=true +# omarchy:group=install + +# The packaged hermes-desktop path Omarchy ships +# (/usr/share/hermes-desktop/install.sh --skip-setup) does not write +# .hermes-bootstrap-complete. Upstream install.sh may stamp it at the end of a +# full install; Windows install.ps1 does (Write-BootstrapMarker). Write that +# same schema so omarchy-install-hermes-cli --check and theme activation see a +# finished desktop runtime. desktopVersion is omitted on purpose: only the +# Electron app knows it, and the validator does not require the field. + +set -euo pipefail + +if (( EUID == 0 )); then + echo "Run this command as your desktop user, without sudo." >&2 + exit 1 +fi + +HERMES_HOME=$(realpath -ms -- "${HERMES_HOME:-$HOME/.hermes}") +home_parent=$(dirname -- "$HERMES_HOME") +if [[ ${home_parent##*/} == [Pp][Rr][Oo][Ff][Ii][Ll][Ee][Ss] ]]; then + HERMES_HOME=$(dirname -- "$home_parent") +fi + +runtime=$HERMES_HOME/hermes-agent +marker=$runtime/.hermes-bootstrap-complete + +[[ -f $marker ]] && exit 0 + +if [[ ! -f $runtime/venv/bin/hermes || ! -x $runtime/venv/bin/hermes || ! -f $runtime/venv/bin/python || ! -x $runtime/venv/bin/python ]] || + ! timeout 15 "$runtime/venv/bin/hermes" --version >/dev/null 2>&1; then + exit 0 +fi + +pinned_commit=${HERMES_BOOTSTRAP_PINNED_COMMIT:-} +if [[ -z $pinned_commit ]]; then + pinned_commit=$(git -C "$runtime" rev-parse HEAD 2>/dev/null || true) +fi +if [[ -z $pinned_commit ]]; then + pinned_commit=$(jq -er 'select(.branch == "main") | .commit | select(test("^[0-9a-f]{40}$"))' /opt/hermes-desktop/resources/install-stamp.json 2>/dev/null || true) +fi +if [[ ! $pinned_commit =~ ^[0-9a-f]{7,}$ ]]; then + echo "Cannot stamp Hermes bootstrap marker: no pinned commit at $runtime" >&2 + exit 1 +fi + +pinned_branch=${HERMES_BOOTSTRAP_PINNED_BRANCH:-main} +completed_at=$(date -u +%Y-%m-%dT%H:%M:%S.000Z) +tmp=$(mktemp -p "$runtime" .hermes-bootstrap-complete.XXXXXX) +if ! jq -n --indent 2 \ + --arg pinnedCommit "$pinned_commit" \ + --arg pinnedBranch "$pinned_branch" \ + --arg completedAt "$completed_at" \ + '{schemaVersion: 1, pinnedCommit: $pinnedCommit, pinnedBranch: $pinnedBranch, completedAt: $completedAt}' >"$tmp"; then + rm -f -- "$tmp" + echo "Cannot stamp Hermes bootstrap marker: failed to write $marker" >&2 + exit 1 +fi +mv -f -- "$tmp" "$marker" diff --git a/migrations/1789746834.sh b/migrations/1789746834.sh new file mode 100644 index 000000000000..f2faa79e8a64 --- /dev/null +++ b/migrations/1789746834.sh @@ -0,0 +1,7 @@ +echo "Stamp the Hermes Desktop bootstrap marker on already-usable installs" + +# The packaged hermes-desktop path Omarchy ships may leave a working runtime +# without .hermes-bootstrap-complete, so --check and theme activation stay +# stuck. Only repair installs Omarchy owns; do not re-run install.sh. +omarchy-pkg-present hermes-desktop || exit 0 +omarchy-stamp-hermes-bootstrap diff --git a/test/shell.d/hermes-bootstrap-migration-test.sh b/test/shell.d/hermes-bootstrap-migration-test.sh new file mode 100644 index 000000000000..afb5cb38dd1f --- /dev/null +++ b/test/shell.d/hermes-bootstrap-migration-test.sh @@ -0,0 +1,113 @@ +#!/bin/bash + +set -euo pipefail + +# Backfill .hermes-bootstrap-complete on a usable Hermes Desktop runtime that +# the packaged install path left unmarked. Stub the package probe and a venv; +# never invoke install.sh or a desktop session. + +source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/base-test.sh" + +for command in git jq; do require_command "$command"; done + +migration="$ROOT/migrations/1789746834.sh" +[[ -f $migration ]] || fail "Hermes bootstrap marker migration exists" +[[ $(stat -c %a "$migration") == "644" ]] || fail "migration is a plain 0644 file" +! grep -q '^#!' "$migration" || fail "migration has no shebang" + +test_tmp=$(mktemp -d) +trap 'rm -rf -- "$test_tmp"' EXIT + +mock_bin="$test_tmp/bin" +mkdir -p "$mock_bin" + +cat >"$mock_bin/omarchy-pkg-present" <<'SH' +#!/bin/bash +[[ $1 == hermes-desktop && ${OMARCHY_TEST_DESKTOP_INSTALLED:-0} == 1 ]] +SH +cat >"$mock_bin/install.sh" <<'SH' +#!/bin/bash +printf 'install.sh\n' >>"$OMARCHY_TEST_CALLS" +exit 1 +SH +chmod +x "$mock_bin"/* + +setup_runtime() { + local home=$1 + local hermes_home=$home/.hermes + local runtime=$hermes_home/hermes-agent + + mkdir -p "$runtime/venv/bin" "$home/.local/bin" + git -C "$runtime" init -q -b main + printf 'runtime\n' >"$runtime/README" + git -C "$runtime" add README + git -C "$runtime" -c user.name=Test -c user.email=test@example.invalid commit -qm fixture + cat >"$runtime/venv/bin/hermes" <<'SH' +#!/bin/bash +if [[ ${1:-} == chat && ${2:-} == --help ]]; then + echo "[-q QUERY, --query QUERY] [--tui]" + exit 0 +fi +echo "hermes-agent 0.0.0-test" +SH + chmod +x "$runtime/venv/bin/hermes" + printf '#!/bin/bash\nexec /usr/bin/python3 "$@"\n' >"$runtime/venv/bin/python" + chmod +x "$runtime/venv/bin/python" + printf '#!/bin/bash\nexec "%s/venv/bin/hermes" "$@"\n' "$runtime" >"$home/.local/bin/hermes" + chmod +x "$home/.local/bin/hermes" +} + +run_migration() { + local home=$1 + : >"$test_tmp/calls" + OMARCHY_TEST_DESKTOP_INSTALLED="${OMARCHY_TEST_DESKTOP_INSTALLED:-1}" \ + OMARCHY_TEST_CALLS="$test_tmp/calls" \ + HOME="$home" \ + PATH="$mock_bin:$ROOT/bin:$PATH" \ + OMARCHY_PATH="$ROOT" \ + bash -euo pipefail "$migration" +} + +assert_marker() { + local runtime=$1 + local commit + commit=$(git -C "$runtime" rev-parse HEAD) + [[ -f $runtime/.hermes-bootstrap-complete ]] || fail "$2: bootstrap marker is missing" + jq -e --arg commit "$commit" ' + .schemaVersion == 1 + and .pinnedCommit == $commit + and .pinnedBranch == "main" + and (.completedAt | test("^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}(\\.[0-9]+)?Z$")) + ' "$runtime/.hermes-bootstrap-complete" >/dev/null || + fail "$2: bootstrap marker schema" "$(cat "$runtime/.hermes-bootstrap-complete")" +} + +home=$test_tmp/no-desktop +setup_runtime "$home" +OMARCHY_TEST_DESKTOP_INSTALLED=0 run_migration "$home" >/dev/null || fail "migration exits clean without Hermes Desktop" +[[ ! -e $home/.hermes/hermes-agent/.hermes-bootstrap-complete ]] || fail "a machine without Hermes Desktop is left unmarked" +[[ ! -s $test_tmp/calls ]] || fail "migration never reaches install.sh" +pass "migration only applies where Omarchy installed Hermes Desktop" + +home=$test_tmp/unusable +mkdir -p "$home/.hermes/hermes-agent" +run_migration "$home" >/dev/null || fail "migration exits clean without a usable runtime" +[[ ! -e $home/.hermes/hermes-agent/.hermes-bootstrap-complete ]] || fail "an unusable runtime is not stamped" +[[ ! -s $test_tmp/calls ]] || fail "an unusable runtime does not run install.sh" +pass "migration leaves an unusable runtime unmarked" + +home=$test_tmp/usable +setup_runtime "$home" +runtime=$home/.hermes/hermes-agent +run_migration "$home" >/dev/null || fail "migration stamps a usable unmarked runtime" +assert_marker "$runtime" "usable unmarked runtime" +[[ ! -s $test_tmp/calls ]] || fail "stamping does not re-run install.sh" +OMARCHY_TEST_DESKTOP_INSTALLED=1 HOME="$home" PATH="$home/.local/bin:$mock_bin:$ROOT/bin:$PATH" \ + bash "$ROOT/bin/omarchy-install-hermes-cli" --check || + fail "omarchy-install-hermes-cli --check after migration" +pass "migration writes the desktop bootstrap marker without reinstalling" + +before=$(cat "$runtime/.hermes-bootstrap-complete") +run_migration "$home" >/dev/null || fail "rerunning the migration succeeds" +[[ $(cat "$runtime/.hermes-bootstrap-complete") == "$before" ]] || fail "rerunning the migration leaves the same marker" +pass "migration is idempotent once the marker exists" diff --git a/test/shell.d/hermes-desktop-install-test.sh b/test/shell.d/hermes-desktop-install-test.sh index 8a9833feba15..c8eb9be75ab1 100644 --- a/test/shell.d/hermes-desktop-install-test.sh +++ b/test/shell.d/hermes-desktop-install-test.sh @@ -80,15 +80,25 @@ if [[ $force == true ]] || ! git -C "$runtime" merge-base --is-ancestor "$commit fi mkdir -p "$runtime/venv/bin" git -C "$runtime" rev-parse HEAD >"$runtime/venv/dependency-commit" -printf '#!/bin/bash\nexit 0\n' >"$runtime/venv/bin/hermes" +cat >"$runtime/venv/bin/hermes" <<'SH' +#!/bin/bash +if [[ ${1:-} == chat && ${2:-} == --help ]]; then + echo "[-q QUERY, --query QUERY] [--tui]" + exit 0 +fi +echo "hermes-agent 0.0.0-test" +SH chmod +x "$runtime/venv/bin/hermes" printf '#!/bin/bash\nexec /usr/bin/python3 "$@"\n' >"$runtime/venv/bin/python" chmod +x "$runtime/venv/bin/python" -[[ ${OMARCHY_TEST_NO_MARKER:-0} == 1 ]] || touch "$runtime/.hermes-bootstrap-complete" +# Deliberately no .hermes-bootstrap-complete: the packaged hermes-desktop +# install.sh --skip-setup that Omarchy ships does not write it. Upstream +# install.sh may stamp after a full install; this mock matches the package. mkdir -p "$HOME/.local/bin" for command in hermes hermes-agent hermes-acp; do rm -f "$HOME/.local/bin/$command" - printf 'native runtime shim\n' >"$HOME/.local/bin/$command" + printf '#!/bin/bash\n# native runtime shim for %s\nexec "%s/venv/bin/hermes" "$@"\n' "$command" "$runtime" >"$HOME/.local/bin/$command" + chmod +x "$HOME/.local/bin/$command" done MOCK @@ -97,6 +107,10 @@ cat >"$test_tmp/bin/omarchy-pkg-add" <<'MOCK' printf 'package %s\n' "$*" >>"$OMARCHY_TEST_ROOT/events" [[ ${OMARCHY_TEST_PACKAGE_FAIL:-0} != 1 ]] MOCK +cat >"$test_tmp/bin/omarchy-pkg-present" <<'MOCK' +#!/bin/bash +[[ $1 == hermes-desktop ]] +MOCK cat >"$test_tmp/bin/git" <<'MOCK' #!/bin/bash if [[ ${OMARCHY_TEST_FETCH_FAIL:-0} == 1 && " $* " == *" --unshallow "* ]]; then exit 8; fi @@ -182,12 +196,27 @@ new_home() { : >"$test_tmp/events" } run_installer() { - HOME="$test_home" HERMES_HOME="${OMARCHY_TEST_HOME:-$hermes_home}" PATH="$test_tmp/bin:$PATH" \ + HOME="$test_home" HERMES_HOME="${OMARCHY_TEST_HOME:-$hermes_home}" PATH="$test_tmp/bin:$ROOT/bin:$PATH" \ bash "$test_tmp/installer" >"$test_tmp/output" 2>&1 } assert_stopped() { if grep -Eq '^(launch|theme-|build-stamp)' "$test_tmp/events"; then fail "$1"; fi } +assert_bootstrap_marker() { + local marker=$runtime/.hermes-bootstrap-complete + [[ -f $marker ]] || fail "$1: bootstrap marker is missing" + jq -e --arg commit "$release_commit" ' + .schemaVersion == 1 + and .pinnedCommit == $commit + and .pinnedBranch == "main" + and (.completedAt | test("^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}(\\.[0-9]+)?Z$")) + ' "$marker" >/dev/null || fail "$1: bootstrap marker schema" "$(cat "$marker")" +} +assert_hermes_cli_ready() { + HOME="$test_home" PATH="$test_home/.local/bin:$test_tmp/bin:$PATH" \ + bash "$ROOT/bin/omarchy-install-hermes-cli" --check \ + || fail "$1: omarchy-install-hermes-cli --check" "$(cat "$runtime/.hermes-bootstrap-complete")" +} new_home fresh run_installer || fail "fresh setup succeeds" "$(cat "$test_tmp/output")" @@ -203,7 +232,8 @@ grep -qx launch "$test_tmp/events" || fail "native app is copied before launch" [[ $(git -C "$runtime" symbolic-ref --short HEAD) == main && $(git -C "$runtime" rev-parse main) == "$release_commit" ]] || fail "main starts at the release rather than the clone tip" [[ $(cat "$runtime/venv/dependency-commit") == "$release_commit" ]] || fail "dependencies are installed for the release" [[ $(git -C "$runtime" rev-parse --is-shallow-repository) == false ]] || fail "first update has connected history" -# Reproduce the updater's checkout/count/pull sequence while origin stays put. +assert_bootstrap_marker "fresh setup" +assert_hermes_cli_ready "fresh setup" git clone -q "$runtime" "$test_tmp/first-update" git -C "$test_tmp/first-update" remote set-url origin "file://$test_tmp/seed" git -C "$test_tmp/first-update" fetch -q origin main @@ -249,17 +279,35 @@ grep -q 'hermes desktop --build-only' "$test_tmp/output" || fail "modified deskt assert_stopped "modified desktop sources prevent stamping, launch and theme setup" pass "a matching commit with modified desktop sources is preserved without seeding or stamping" -for failure in package install marker; do +for failure in package install; do new_home "$failure-failure" case "$failure" in package) OMARCHY_TEST_PACKAGE_FAIL=1 run_installer && fail "package failure stops setup" ;; install) OMARCHY_TEST_INSTALL_FAIL=1 run_installer && fail "installer failure stops setup" ;; - marker) OMARCHY_TEST_NO_MARKER=1 run_installer && fail "missing marker stops setup" ;; esac [[ ! -e $native ]] || fail "failed setup does not seed the app" + [[ ! -e $runtime/.hermes-bootstrap-complete ]] || fail "failed setup does not stamp a bootstrap marker" assert_stopped "failed setup prevents launch and theme setup" done -pass "package, upstream installer and readiness failures stop before launch" +pass "package and upstream installer failures stop before launch" + +new_home headless-marker +run_installer || fail "headless skip-setup install succeeds without an upstream marker" "$(cat "$test_tmp/output")" +assert_bootstrap_marker "headless skip-setup" +assert_hermes_cli_ready "headless skip-setup" +pass "headless skip-setup install writes the desktop bootstrap marker" + +new_home marker-backfill +HOME="$test_home" HERMES_HOME="$hermes_home" bash "$test_tmp/share/install.sh" \ + --skip-setup --branch main --commit "$release_commit" --force-commit --dir "$runtime" --hermes-home "$hermes_home" +[[ ! -e $runtime/.hermes-bootstrap-complete ]] || fail "fixture runtime has no bootstrap marker" +: >"$test_tmp/events" +run_installer || fail "usable markerless runtime is accepted" "$(cat "$test_tmp/output")" +! grep -qx bootstrap "$test_tmp/events" || fail "usable runtime is not handed back to install.sh just to stamp the marker" +assert_bootstrap_marker "marker backfill" +assert_hermes_cli_ready "marker backfill" +grep -qx launch "$test_tmp/events" || fail "marker backfill still copies and launches the app" +pass "a usable runtime missing only the marker is stamped without reinstalling" for failure in copy race; do new_home "$failure-failure"