Harden the Hermes skin hand-over

Hermes' YAML reader breaks lines on carriage return, NEL and the Unicode line and paragraph separators, and stops at NUL, none of which grep treats as a line end, so a comment line carrying one could put a root-level key such as banner_logo past the validator and into Rich markup on Hermes' terminal surfaces. The lines grep accepted also did not add up to the YAML Hermes needs: a colour before colors:, a second colors:, or a key over YAML's simple-key limit all passed and loaded as no palette at all, which Hermes shows as its default. The validator now counts every byte outside printable ASCII first, then walks the file in order: the name, at most one plain description, colors:, and only #rrggbb colour lines after it.

omarchy-theme-set releases its lock before the hooks run, so the rendered skin can change under this one between the check and the copy. The check is made on a private copy and that copy is what gets published, both on the first pass and on the republish a minute after activation, which used to copy whatever the theme had become by then, unchecked.

A theme switch reads the config of the profile named in active_profile, which is the one Hermes reads, and a profile exists to Hermes once its directory does, with or without a config; it ends early only for a config plainly naming another skin, since only the default is ever replaced, and leaves anything Hermes might read as the default for Hermes to answer. Hermes is run by the path the readiness probe vets, ~/.local/bin/hermes, bounded the way the probe bounds it; an answer that did not come is not taken for the default, and a write Hermes refuses is reported rather than failed, being cosmetic.

A profile that cannot take the skin no longer costs the others or the activation; a directory at the skin's path is an error rather than a place mv puts the temp file; a temp file the copy could not fill is removed. Remove stops the unit the installer left waiting, so a removal within the waiter's half hour does not hand the theme to a Hermes installed some other way or recreate the skin under a home the user asked to delete. The migration no longer swallows the hook's exit: what is not ready or refused is reported and done with inside the hook, so only Omarchy's own failures return, and those keep the migration pending as the guide requires.

Comments are cut to what the code cannot say; the reasoning is here.

Co-Authored-By: Codex XHigh <noreply@openai.com>
This commit is contained in:
Spencer BullandCodex XHigh committed 2026-09-05 23:28:46 -05:00
1 parent 6baae0f556
commit 8569d1cadc
7 files changed
+280 -102

No files matched your search

+2 -7
View File
@@ -21,13 +21,8 @@ omarchy-install-hermes-cli || true
echo "Opening Hermes Desktop..."
setsid uwsm-app -- /usr/bin/hermes-desktop >/dev/null 2>&1 &
# The app builds its runtime on first launch, which takes minutes, and only a
# running Hermes can be told which skin to show. So the theme is handed over
# after the launch above, by a helper that waits for that runtime and then
# names the Omarchy skin through Hermes itself; the running app repaints. Only
# Hermes' own default is replaced, so a skin chosen in Hermes is never undone.
# It runs as its own unit, so this terminal closing cannot take it along and
# what it did is in the journal: journalctl --user -u omarchy-hermes-theme
# Only a running Hermes can be told which skin to show, and the first launch
# takes minutes; a unit outlives this terminal and reports to the journal.
echo "Matching Hermes to the current theme once it is set up..."
systemctl --user stop omarchy-hermes-theme.service 2>/dev/null || true
systemd-run --user --quiet --collect --unit=omarchy-hermes-theme omarchy-theme-set-hermes --wait
+3
View File
@@ -8,6 +8,9 @@ set -euo pipefail
omarchy-pkg-drop hermes-desktop
# The installer leaves a unit waiting to hand the app the Omarchy theme.
systemctl --user stop omarchy-hermes-theme.service 2>/dev/null || true
# The mise CLI is the app's predecessor, not the app itself: Hermes Desktop takes
# it over on install and runs its own runtime instead, so a copy still here is one
# the app never superseded -- an interrupted install, or the terminal CLI from
+112 -78
View File
@@ -4,21 +4,8 @@
# omarchy:args=[--activate] [--wait]
# omarchy:hidden=true
# Hermes has one theme unit for all of its surfaces, the skin: a YAML file in
# ~/.hermes/skins/ that the desktop app, the TUI and the CLI each turn into
# their own palette. The active one is named by display.skin in Hermes' config,
# and the gateway watches that file's mtime, so a rewrite of the active skin
# repaints every connected surface within a second -- no Omarchy code in
# Hermes, and nothing here that Hermes did not already offer any other system.
#
# Every run publishes the skin file, and for the desktop app Omarchy installed
# names it in the config through Hermes' own config writer if Hermes is still
# on its default skin, so a theme switch also picks up an install whose
# hand-over was missed. --activate is the deliberate form, for the installer
# and the migration: it renders a skin the current theme does not have yet and
# says why nothing happened when it cannot finish. --wait holds the whole thing
# until the desktop app has finished building the runtime its first launch
# provisions.
# A skin is Hermes' one theme unit for the desktop app, the TUI and the CLI;
# its gateway watches the active skin file and repaints every surface on change.
set -euo pipefail
@@ -26,11 +13,10 @@ HERMES_SOURCE_PATH="$HOME/.local/state/omarchy/current/theme/hermes.yaml"
HERMES_THEME_NAME_PATH="$HOME/.local/state/omarchy/current/theme.name"
HERMES_HOME="${HERMES_HOME:-$HOME/.hermes}"
HERMES_CONFIG_PATH="$HERMES_HOME/config.yaml"
# The filename is the skin name Hermes shows and display.skin refers to, and it
# stays put while the colors change underneath on every theme switch.
HERMES_SKIN_NAME="omarchy"
# The app writes this when the runtime it provisions has landed; the CLI
# installer's readiness probe passes only once it has.
# The command the readiness probe vets, rather than whichever hermes is on PATH.
HERMES_COMMAND="$HOME/.local/bin/hermes"
# Written by the desktop app once the runtime its first launch provisions is in.
HERMES_BOOTSTRAP_MARKER="$HERMES_HOME/hermes-agent/.hermes-bootstrap-complete"
HERMES_ACTIVATE=0
HERMES_WAIT=0
@@ -60,17 +46,14 @@ for arg in "$@"; do
esac
done
# Only the deliberate form explains itself: the theme switch runs this beside
# a dozen other hooks, where a note about Hermes on every switch is noise.
# A theme switch runs this beside a dozen other hooks; only --activate explains.
note() {
if (( HERMES_ACTIVATE == 1 )); then
echo "$*" >&2
fi
}
# The skin is generated with the rest of the theme, so a theme applied before
# the template existed has none yet. Asked to activate, re-stage the current
# theme to render it; a theme switch simply has nothing to publish.
# A theme applied before the template existed has no skin rendered yet.
if [[ ! -f $HERMES_SOURCE_PATH ]]; then
(( HERMES_ACTIVATE == 1 )) || exit 0
@@ -88,9 +71,8 @@ if [[ ! -f $HERMES_SOURCE_PATH ]]; then
fi
fi
# The desktop app builds its runtime on first launch, which takes minutes and
# may be abandoned. Poll for the marker rather than the readiness probe, which
# starts Hermes to answer, and give up quietly on a launch that never came.
# The first launch takes minutes and may be abandoned; the readiness probe
# would start Hermes to answer, so poll for the marker instead.
if (( HERMES_WAIT == 1 )); then
waited=0
until [[ -f $HERMES_BOOTSTRAP_MARKER && -f $HERMES_CONFIG_PATH ]]; do
@@ -104,96 +86,148 @@ if (( HERMES_WAIT == 1 )); then
done
fi
# Only follow a Hermes that has been set up. Its config is what Hermes writes
# when it has; ~/.hermes itself proves nothing, because provisioning creates it
# on every machine to hold the Omarchy skill.
# Provisioning creates ~/.hermes on every machine for the Omarchy skill; the
# config is what Hermes writes once it has actually run.
if [[ ! -f $HERMES_CONFIG_PATH ]]; then
note "Hermes is not set up yet; launch it once, then run omarchy-theme-set-hermes --activate."
exit 0
fi
# Hermes parses the skin as YAML and hands the strings to every surface, so the
# generated file is held to the shape the template promises before it is
# published: the name Hermes loads it by, a plain description, and colors that
# are all #rrggbb. A theme missing a palette key leaves its placeholder
# unresolved, and a theme cloned from a git repo may ship its own hermes.yaml;
# neither gets to write anything else into a file another program reads. The
# previous skin stays in place instead.
# Hermes parses the skin as YAML and hands its strings to every surface, so only
# the name, a plain description and #rrggbb colours may reach it, in the order
# YAML needs them. YAML breaks lines on bytes grep does not, so the bytes are
# counted first, NUL included.
skin_is_well_formed() {
local skin="$1"
grep -qx "name: $HERMES_SKIN_NAME" "$skin" &&
grep -qx 'colors:' "$skin" &&
! grep -qvE "^(#.*|[[:space:]]*|name: $HERMES_SKIN_NAME|description: [A-Za-z0-9 ,.()-]*|colors:| [a-z_]+: \"#[0-9a-fA-F]{6}\")$" "$skin"
[[ -f $skin ]] &&
(( $(LC_ALL=C tr -d ' -~\n' <"$skin" | wc -c) == 0 )) &&
awk -v name="name: $HERMES_SKIN_NAME" '
bad { next }
/^#/ || /^[[:space:]]*$/ { next }
!seen_name { if ($0 == name) seen_name = 1; else bad = 1; next }
!seen_colors {
if ($0 == "colors:") seen_colors = 1
else if (!seen_description && $0 ~ /^description: [A-Za-z0-9 ,.()-]{0,200}$/) seen_description = 1
else bad = 1
next
}
/^ [a-z_]{1,64}: "#[0-9a-fA-F]{6}"$/ { colors++; next }
{ bad = 1 }
END { exit (bad || !seen_colors || colors == 0) }
' "$skin"
}
if ! skin_is_well_formed "$HERMES_SOURCE_PATH"; then
# The check has to cover the bytes that get published, and the theme can change
# underneath between the two, so a private copy is taken and that is checked.
snapshot_dir=$(mktemp -d)
trap 'rm -rf "$snapshot_dir"' EXIT
HERMES_SNAPSHOT="$snapshot_dir/$HERMES_SKIN_NAME.yaml"
take_snapshot() {
cp "$HERMES_SOURCE_PATH" "$HERMES_SNAPSHOT" 2>/dev/null && skin_is_well_formed "$HERMES_SNAPSHOT"
}
if ! take_snapshot; then
echo "Skipping Hermes skin: $(basename "$HERMES_SOURCE_PATH") is not a plain color palette." >&2
exit 0
fi
# The gateway watches the skin file's mtime and reads it whole on change, so
# the write has to be atomic: a half-written file would read as no skin at all.
# The gateway reads the file whole on an mtime change, so the write is atomic;
# -T so a directory at the skin's path is an error rather than a destination.
publish_skin() {
local skins_dir="$1"
local tmp
mkdir -p "$skins_dir"
tmp=$(mktemp "$skins_dir/$HERMES_SKIN_NAME.yaml.XXXXXX")
cp "$HERMES_SOURCE_PATH" "$tmp"
mv "$tmp" "$skins_dir/$HERMES_SKIN_NAME.yaml"
mkdir -p "$skins_dir" 2>/dev/null || return 1
tmp=$(mktemp "$skins_dir/$HERMES_SKIN_NAME.yaml.XXXXXX" 2>/dev/null) || return 1
if ! cp "$HERMES_SNAPSHOT" "$tmp" 2>/dev/null || ! mv -T "$tmp" "$skins_dir/$HERMES_SKIN_NAME.yaml" 2>/dev/null; then
rm -f "$tmp"
return 1
fi
}
publish_skin "$HERMES_HOME/skins"
# A profile is a Hermes home of its own; existing ones get the skin, none are
# made, and one that cannot take it does not cost the others.
publish_skin_everywhere() {
local profile
# A Hermes profile is a home of its own, with its own skins and config, and the
# gateway watches whichever profile is active. Existing profiles get the skin
# too; none are created here.
for profile in "$HERMES_HOME"/profiles/*/; do
[[ -d $profile ]] || continue
publish_skin "${profile%/}/skins"
done
publish_skin "$HERMES_HOME/skins" || {
echo "Could not publish the Hermes skin to $HERMES_HOME/skins." >&2
return 1
}
# A theme switch finishes the hand-over only for the app Omarchy installed
# under Install > AI; a Hermes set up some other way keeps whatever skin it
# has unless --activate asks for it by name. Hermes writes the active skin
# under display: at that indent, and once it names ours a theme switch is done
# here without starting Hermes; anything else is for Hermes itself to answer
# below, so the grep is only ever a shortcut.
for profile in "$HERMES_HOME"/profiles/*/; do
[[ -d $profile ]] || continue
publish_skin "${profile%/}/skins" || note "Could not publish the skin to the Hermes profile $(basename "$profile")."
done
}
publish_skin_everywhere
# Hermes reads the config of the profile named in active_profile; the profile
# exists once its directory does, with or without a config of its own.
active_config_path() {
local profile
profile=$(cat "$HERMES_HOME/active_profile" 2>/dev/null || true)
profile=${profile,,}
if [[ -n $profile && $profile != "default" && -d $HERMES_HOME/profiles/$profile ]]; then
echo "$HERMES_HOME/profiles/$profile/config.yaml"
else
echo "$HERMES_CONFIG_PATH"
fi
}
# A theme switch finishes the hand-over only for the app Omarchy installed, and
# only Hermes' default is ever replaced, so a config plainly naming another skin
# ends it here without starting Hermes. Anything less plain is for Hermes to read.
if (( HERMES_ACTIVATE == 0 )); then
omarchy-pkg-present hermes-desktop || exit 0
grep -qx " skin: $HERMES_SKIN_NAME" "$HERMES_CONFIG_PATH" && exit 0
skin_line=$(grep -m1 -x ' skin: .*' "$(active_config_path)" 2>/dev/null || true)
case "${skin_line# skin: }" in
"" | default | null | true | false | *[!A-Za-z0-9_-]*) ;;
*) exit 0 ;;
esac
fi
# Naming the skin in the config is Hermes' own job: `hermes config set` is what
# `hermes skin use` runs, it writes the active profile's config, and it touches
# the skin file so a running gateway broadcasts the change and every surface
# repaints at once. Asked through the installer's readiness probe rather than by
# running whatever `hermes` is on PATH, because Omarchy's cold stub installs
# Hermes when run, and that takes minutes.
# Omarchy's cold stub installs Hermes when run, so ask the probe before running it.
if ! omarchy-install-hermes-cli --check 2>/dev/null; then
note "Hermes is not ready, so the Omarchy skin is published but not active."
note "Once Hermes runs, activate it with: hermes config set display.skin $HERMES_SKIN_NAME"
exit 0
fi
# A skin the user picked in Hermes stays theirs. Only Hermes' own default is
# replaced, so a reinstall, a migration or a theme switch never undoes a choice.
current_skin=$(hermes config get display.skin 2>/dev/null || true)
# Only Hermes' own default is replaced, so a skin chosen in Hermes stays; an
# answer that did not come is not a default.
if ! current_skin=$(timeout 15 "$HERMES_COMMAND" config get display.skin 2>/dev/null); then
note "Hermes did not say which skin it is on, so the Omarchy skin is published but not active."
exit 0
fi
if [[ -n $current_skin && $current_skin != "default" && $current_skin != "$HERMES_SKIN_NAME" ]]; then
note "Hermes is set to the '$current_skin' skin; leaving it. Switch with: hermes config set display.skin $HERMES_SKIN_NAME"
exit 0
fi
hermes config set display.skin "$HERMES_SKIN_NAME" >/dev/null
# Hermes' own writer: it updates the active profile's config and touches the
# skin file so a running gateway broadcasts the change. A refusal is cosmetic.
if ! timeout 30 "$HERMES_COMMAND" config set display.skin "$HERMES_SKIN_NAME" >/dev/null 2>&1; then
note "Hermes refused to switch skins, so the Omarchy skin is published but not active."
exit 0
fi
note "Hermes is on the Omarchy skin."
# The desktop app adopts a skin from a change broadcast, not from the config it
# finds at connect time, and the gateway seeds its change watcher when it starts.
# A first launch brings the gateway up moments after the marker, so a config
# written in that gap goes unannounced; republishing once the gateway is surely
# up moves the file's mtime again, and that broadcast the desktop does apply.
# The desktop applies a skin only from a broadcast, and a config written before
# the gateway seeded its watcher goes unannounced; a later write is announced.
# The theme may have changed underneath in the meantime, so it is checked again.
if (( HERMES_WAIT == 1 )); then
sleep 60
publish_skin "$HERMES_HOME/skins"
if take_snapshot; then
publish_skin_everywhere
fi
fi