feat: Athena OS — launchd status=127 guard в health-check

chk_launchd: для каждого user LaunchAgent проверяет, что бинари, вызванные
голой командой (claude/chezmoi/node/yt-dlp/ffmpeg/whisper/python3), резолвятся
под эффективным PATH агента (EnvironmentVariables.PATH ∪ login-shell -lc ∪
inline `export PATH=`). Ловит рекуррентный баг «бинарь не на PATH launchd»
ДО того как агент молча сдохнет с exit 127 (3-й такой урок в lessons-log).

Детектор: PlistBuddy (невэкранированный вывод вместо plutil-json, иначе
\" ломает извлечение inline-PATH); тул = голая команда (граница пробел/;/&/|),
абсолютные пути и подстроки в путях (.claude/, claude-telegram-bot) не флагуются.

Живой аудит машины: 8/8 user-агентов ok (0 ложных, mnemosyne-health
подтверждён после Ф3-фикса PATH).
This commit is contained in:
Зарубин Филипп
2026-06-15 20:33:28 +03:00
parent f7d0485919
commit d2f2c382c2
@@ -6,6 +6,33 @@ LOG="$HOME/.claude/health.log"
ts="$(date '+%Y-%m-%d %H:%M:%S')" ts="$(date '+%Y-%m-%d %H:%M:%S')"
issues=0 issues=0
chk() { if eval "$2" >/dev/null 2>&1; then echo " ok $1"; else echo " !! $1"; issues=$((issues + 1)); fi; } chk() { if eval "$2" >/dev/null 2>&1; then echo " ok $1"; else echo " !! $1"; issues=$((issues + 1)); fi; }
# Guard каждого user LaunchAgent против status=127 (бинарь не на PATH launchd).
# Эфф. PATH = EnvironmentVariables.PATH ∪ login-shell(-lc) ∪ inline `export PATH=` в команде.
chk_launchd() {
local dir="$HOME/Library/LaunchAgents" plist label args effpath inlinepath tool miss
[ -d "$dir" ] || { echo " ·· launchd: нет агентов (skip)"; return; }
for plist in "$dir"/*.plist; do
[ -e "$plist" ] || continue
label="$(basename "$plist" .plist)"
args="$(/usr/libexec/PlistBuddy -c 'Print :ProgramArguments' "$plist" 2>/dev/null)" || continue
[ -n "$args" ] || continue
effpath="$(plutil -extract EnvironmentVariables.PATH raw -o - "$plist" 2>/dev/null)"
if [ -z "$effpath" ] && printf '%s' "$args" | grep -q -- '-lc'; then
effpath="$(env -i HOME="$HOME" /bin/zsh -lc 'printf %s "$PATH"' 2>/dev/null)"
fi
inlinepath="$(printf '%s' "$args" | grep -oE 'export PATH="[^"]*"' | head -1 | sed -E 's/^export PATH="//; s/"$//')"
[ -n "$inlinepath" ] && effpath="$inlinepath:$effpath"
effpath="${effpath//\$HOME/$HOME}"
[ -n "$effpath" ] || effpath="/usr/bin:/bin"
miss=""
for tool in claude chezmoi node yt-dlp ffmpeg whisper python3; do
printf '%s' "$args" | grep -qE '(^|[[:space:];&|(])'"$tool"'([[:space:]]|$)' || continue
PATH="$effpath" command -v "$tool" >/dev/null 2>&1 || miss="$miss $tool"
done
if [ -n "$miss" ]; then echo " !! launchd $label:$miss не на PATH (status=127)"; issues=$((issues + 1))
else echo " ok launchd $label"; fi
done
}
{ {
echo "── health $ts ──" echo "── health $ts ──"
chk "claude CLI" "command -v claude" chk "claude CLI" "command -v claude"
@@ -15,6 +42,7 @@ chk() { if eval "$2" >/dev/null 2>&1; then echo " ok $1"; else echo " !! $1"
chk "security-guard" "test -f \"$HOME/.claude/hooks/security-guard.sh\"" chk "security-guard" "test -f \"$HOME/.claude/hooks/security-guard.sh\""
chk "rules/structure" "test -f \"$HOME/.claude/rules/structure.md\"" chk "rules/structure" "test -f \"$HOME/.claude/rules/structure.md\""
chk "registry" "test -d \"$HOME/.agents/registry\"" chk "registry" "test -d \"$HOME/.agents/registry\""
chk_launchd
echo " итог: $issues проблем(ы)" echo " итог: $issues проблем(ы)"
} >"$LOG" 2>&1 } >"$LOG" 2>&1
exit 0 exit 0