Self-healing autopoietic shell: f(), tests, docs, interactive demo, fx fix-source layer
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
#!/bin/sh
|
||||
# Self-healing shell loop: run a command, and on failure ask for a fix,
|
||||
# persist it keyed by the exact command line, then retry. On future
|
||||
# calls with the same command, the learned fix is applied proactively
|
||||
# before the command itself runs.
|
||||
#
|
||||
# State lives in ./p as "command=fix" lines, one per learned command.
|
||||
# Copy p to another machine to transfer everything this shell has learned.
|
||||
#
|
||||
# Usage:
|
||||
# . f.sh
|
||||
# f some-command-that-might-fail --with args
|
||||
|
||||
f() {
|
||||
FIX=""
|
||||
if [ -f p ]; then
|
||||
while IFS= read -r line; do
|
||||
case "$line" in
|
||||
"$*="*) FIX="${line#"$*="}" ;;
|
||||
esac
|
||||
done < p
|
||||
fi
|
||||
|
||||
until { eval "$FIX" 2>/dev/null; "$@"; }; do
|
||||
printf '\n[LEARN] "%s" failed. Enter a fix (blank to give up): ' "$*" >&2
|
||||
read -r n
|
||||
[ -n "$n" ] || return 1
|
||||
echo "$*=$n" >> p
|
||||
FIX="$n"
|
||||
done
|
||||
}
|
||||
Reference in New Issue
Block a user