Files
adaptiveos/root/shell-project/contrib/fx/sources.d/30-selfdiag.sh
T
Daveswo 77856f0a09 Initial commit: apkovl deployment source, synced from shell-project main
This directory is what gets packaged into localhost.apkovl.tar.gz and
baked into adaptiveos-vN.iso. It had drifted out of sync with the real
project (root/shell-project/ had gone missing entirely at some point
after being written earlier this session -- cause unconfirmed, Desktop
is a plausible sync-related suspect but not verified) and separately
carried a stale, pre-fix copy of fx.sh.

Current contents:
  etc/profile.d/adaptive-loop.sh   -- disk-persists p AND p.trace
                                       (fx()'s fix-provenance record)
                                       across reboot, two separate 1MB
                                       blocks on the attached state disk
  etc/profile.d/adaptive-setup.sh  -- unchanged, network/git/fzf bring-up
  root/shell-project/              -- synced from space.seodr.ovh/
                                       Daveswo/self-healing-autopoietic-
                                       shell @ 3c4947e (main), plus three
                                       deployment-only files not in that
                                       repo: contrib/fx/builtin-fixes.txt,
                                       contrib/fx/sources.d/15-builtin-
                                       fixes.sh, contrib/heal/heal.sh

Verified byte-identical to what's actually embedded in the currently
shipping adaptiveos-v6.iso before this commit (full recursive diff),
and every .sh file passes `sh -n`.

.gitattributes (text=auto eol=lf) added to prevent the CRLF corruption
that hit the canonical repo earlier this session from recurring here.
2026-08-22 02:55:40 -04:00

40 lines
2.1 KiB
Bash

#!/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
# Literal prefix comparison (substr/index), never a regex built from
# $prog: proven broken live for program names containing ERE
# metacharacters -- e.g. "g++ foo.cpp" produced the pattern
# "^[[:space:]]*g++[[:space:]]", a malformed stacked-quantifier regex,
# and this source silently found nothing even when $FX_OUTPUT clearly
# started a line with "g++ ". Same fix applied to the emitted function
# below, since it runs the identical check on every future call.
printf '%s\n' "$FX_OUTPUT" | awk -v p="$prog" '
{ line = $0; sub(/^[ \t]*/, "", line); if (substr(line, 1, length(p) + 1) == p " ") { found = 1; exit } }
END { exit !found }
' || exit 0
cat <<FIX
${prog}(){ out=\$(command ${prog} "\$@" 2>&1); ec=\$?; if [ \$ec -ne 0 ]; then sug=\$(printf '%s\\n' "\$out" | awk -v p="${prog}" '{ line=\$0; sub(/^[ \\t]*/,"",line); if (substr(line,1,length(p)+1)==p" ") s=line } END{print s}'); if [ -n "\$sug" ]; then eval "command \$sug"; return \$?; fi; fi; printf '%s\\n' "\$out"; return \$ec; }
FIX