#!/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. # # The emitted function runs on every future call to that program for # the rest of the session, so its own internal extraction is one awk # pass, not a grep|sed|tail chain -- three fewer forks on every failure # it ever handles, not just this first one. 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 <&1); ec=\$?; if [ \$ec -ne 0 ]; then sug=\$(printf '%s\\n' "\$out" | awk '/^[ \\t]*${prog}[ \\t]/{sub(/^[ \\t]*/,""); s=\$0} END{print s}'); if [ -n "\$sug" ]; then eval "command \$sug"; return \$?; fi; fi; printf '%s\\n' "\$out"; return \$ec; } FIX