Files
self-healing-autopoietic-shell/contrib/fx/sources.d/30-selfdiag.sh
T

24 lines
1.3 KiB
Bash
Executable File

#!/bin/sh
# Self-diagnosis source: many CLIs print their own corrected invocation
# in their error output (git's --set-upstream hint, apt's "did you
# mean", etc.).
#
# IMPORTANT: this must NOT cache the literal suggested line -- that
# reintroduces the exact-key staleness problem (e.g. "git push" learned
# once on branch A silently misapplies its captured --set-upstream on
# branch B, and B never actually gets pushed). Instead it emits a
# function that shadows the whole program and re-derives the suggestion
# from *live* output on every call, the same trick proven to generalize
# correctly across branches/arguments in practice. One taught fix this
# way covers every future self-diagnosing failure from that program, not
# just the one instance that happened to trigger the teaching prompt.
cmd="$1"
prog=$(printf '%s' "$cmd" | awk '{print $1}')
[ -n "$prog" ] || exit 0
[ -n "$FX_OUTPUT" ] || exit 0
printf '%s\n' "$FX_OUTPUT" | grep -qE "^[[:space:]]*${prog}[[:space:]]" || exit 0
cat <<FIX
${prog}(){ out=\$(command ${prog} "\$@" 2>&1); ec=\$?; if [ \$ec -ne 0 ]; then sug=\$(printf '%s\\n' "\$out" | grep -E "^[[:space:]]*${prog}[[:space:]]" | sed 's/^[[:space:]]*//' | tail -1); if [ -n "\$sug" ]; then eval "command \$sug"; return \$?; fi; fi; printf '%s\\n' "\$out"; return \$ec; }
FIX