fix(hooks): digest bash 3.2 safe and time-bounded
This commit is contained in:
1 parent
177502139d
commit
d0d119fe30
2 files changed
+42
-22
No files matched your search
+24
-22
@@ -10,6 +10,8 @@
|
|||||||
# Digest on startup | clear | compact, and on an empty source from a manual
|
# Digest on startup | clear | compact, and on an empty source from a manual
|
||||||
# terminal run; never on resume. Without timeout or jq the source is
|
# terminal run; never on resume. Without timeout or jq the source is
|
||||||
# unknowable: digest only when stdin is a terminal, never from a hook.
|
# unknowable: digest only when stdin is a terminal, never from a hook.
|
||||||
|
# A non-JSON stdin makes jq print nothing: it reads as an empty source and
|
||||||
|
# the digest prints.
|
||||||
_ss_digest=0
|
_ss_digest=0
|
||||||
if command -v timeout >/dev/null 2>&1 && command -v jq >/dev/null 2>&1; then
|
if command -v timeout >/dev/null 2>&1 && command -v jq >/dev/null 2>&1; then
|
||||||
_ss_source=""
|
_ss_source=""
|
||||||
@@ -350,7 +352,8 @@ _digest_cut() {
|
|||||||
[ "${#v}" -le 160 ] && { printf '%s' "$v"; return; }
|
[ "${#v}" -le 160 ] && { printf '%s' "$v"; return; }
|
||||||
v="${v:0:159}"
|
v="${v:0:159}"
|
||||||
if command -v iconv >/dev/null 2>&1; then
|
if command -v iconv >/dev/null 2>&1; then
|
||||||
while [ -n "$v" ] && ! printf '%s' "$v" | iconv -f UTF-8 -t UTF-8 >/dev/null 2>&1; do
|
while [ -n "$v" ] \
|
||||||
|
&& ! printf '%s' "$v" | iconv -f UTF-8 -t UTF-8 >/dev/null 2>&1; do
|
||||||
v="${v%?}"
|
v="${v%?}"
|
||||||
done
|
done
|
||||||
fi
|
fi
|
||||||
@@ -359,7 +362,9 @@ _digest_cut() {
|
|||||||
|
|
||||||
# Open BLK (≤ 3 listed, the rest counted) and upstream count, as digest lines.
|
# Open BLK (≤ 3 listed, the rest counted) and upstream count, as digest lines.
|
||||||
_digest_blockers() { # <root> <memory.sh>
|
_digest_blockers() { # <root> <memory.sh>
|
||||||
local row line rest status title open=0 upstream=0 more=""
|
local row line rest status title open=0 upstream=0 more="" tmo=()
|
||||||
|
# a hung memory.sh must not stall every session start
|
||||||
|
command -v timeout >/dev/null 2>&1 && tmo=(timeout 2)
|
||||||
while IFS= read -r row; do
|
while IFS= read -r row; do
|
||||||
[[ "$row" == "| BLK-"* ]] || continue
|
[[ "$row" == "| BLK-"* ]] || continue
|
||||||
line="${row#| }"; line="${line% |}"
|
line="${row#| }"; line="${line% |}"
|
||||||
@@ -371,7 +376,7 @@ _digest_blockers() { # <root> <memory.sh>
|
|||||||
[ "$open" -le 3 ] && echo "- open: ${line%% | *} $title" ;;
|
[ "$open" -le 3 ] && echo "- open: ${line%% | *} $title" ;;
|
||||||
upstream) upstream=$((upstream + 1)) ;;
|
upstream) upstream=$((upstream + 1)) ;;
|
||||||
esac
|
esac
|
||||||
done < <(bash "$2" -C "$1" index blocker 2>/dev/null)
|
done < <("${tmo[@]}" bash "$2" -C "$1" index blocker 2>/dev/null)
|
||||||
[ "$open" -gt 3 ] && more="+$((open - 3)) more"
|
[ "$open" -gt 3 ] && more="+$((open - 3)) more"
|
||||||
[ "$upstream" -gt 0 ] && more="${more:+$more, }+${upstream} upstream"
|
[ "$upstream" -gt 0 ] && more="${more:+$more, }+${upstream} upstream"
|
||||||
[ -n "$more" ] && echo "- open: $more"
|
[ -n "$more" ] && echo "- open: $more"
|
||||||
@@ -390,26 +395,21 @@ _digest_journal() { # <root> → last journal bullet
|
|||||||
}
|
}
|
||||||
|
|
||||||
# The block: header + open BLK lines + count line + todo lines + journal.
|
# The block: header + open BLK lines + count line + todo lines + journal.
|
||||||
# Over 800 chars: todo lines go (last to first); header, open BLK lines
|
# Todo lines are added in order while the block stays <= 800 chars, so the
|
||||||
# (3 x 160 max), count line and journal stay, which always fits in 800.
|
# last ones drop first; header, open BLK lines (3 x 160 max), count line and
|
||||||
|
# journal always stay and always fit. The only loop is bounded by the todo
|
||||||
|
# count (3). Stays bash 3.2 safe (checked by SD11).
|
||||||
# Built in a variable and printed once, so a failure mid-way prints nothing.
|
# Built in a variable and printed once, so a failure mid-way prints nothing.
|
||||||
_digest_assemble() { # reads the arrays blk, cnt, todo, jr of the caller
|
_digest_assemble() { # reads the arrays blk, cnt, todo, jr of the caller
|
||||||
local block i
|
local head="memory digest:" tail="" body="" line size
|
||||||
while :; do
|
for line in "${blk[@]}" "${cnt[@]}"; do head+=$'\n'"$line"; done
|
||||||
block="memory digest:"
|
for line in "${jr[@]}"; do tail=$'\n'"$line"; done
|
||||||
for i in "${blk[@]}" "${cnt[@]}" "${todo[@]}" "${jr[@]}"; do
|
size=$((${#head} + ${#tail}))
|
||||||
block+=$'\n'"$i"
|
for line in "${todo[@]}"; do
|
||||||
done
|
[ $((size + ${#line} + 1)) -le 800 ] || break
|
||||||
[ "${#block}" -le 800 ] && break
|
body+=$'\n'"$line"; size=$((size + ${#line} + 1))
|
||||||
if [ "${#todo[@]}" -gt 0 ]; then unset 'todo[-1]'
|
|
||||||
else break; fi
|
|
||||||
done
|
done
|
||||||
printf '%s\n' "$block"
|
printf '%s%s%s\n' "$head" "$body" "$tail"
|
||||||
}
|
|
||||||
|
|
||||||
_digest_collect() { # <array name> <line source output on stdin>
|
|
||||||
local -n _dc=$1; local line
|
|
||||||
while IFS= read -r line; do _dc+=("$(_digest_cut "$line")"); done
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_memory_digest() { # <project-root>
|
_memory_digest() { # <project-root>
|
||||||
@@ -424,8 +424,10 @@ _memory_digest() { # <project-root>
|
|||||||
esac
|
esac
|
||||||
done < <(_digest_blockers "$root" "$lib")
|
done < <(_digest_blockers "$root" "$lib")
|
||||||
fi
|
fi
|
||||||
_digest_collect todo < <(_digest_todo "$root")
|
while IFS= read -r line; do todo+=("$(_digest_cut "$line")"); done \
|
||||||
_digest_collect jr < <(_digest_journal "$root")
|
< <(_digest_todo "$root")
|
||||||
|
while IFS= read -r line; do jr+=("$(_digest_cut "$line")"); done \
|
||||||
|
< <(_digest_journal "$root")
|
||||||
[ $((${#blk[@]} + ${#cnt[@]} + ${#todo[@]} + ${#jr[@]})) -gt 0 ] || return 0
|
[ $((${#blk[@]} + ${#cnt[@]} + ${#todo[@]} + ${#jr[@]})) -gt 0 ] || return 0
|
||||||
_digest_assemble
|
_digest_assemble
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -159,5 +159,23 @@ check SD10-no-jq-no-digest "$(printf '%s' "$out" | grep -c 'memory digest:')" 0
|
|||||||
check SD10-no-jq-banner "$(printf '%s' "$out" | grep -c 'make doctor')" 1
|
check SD10-no-jq-banner "$(printf '%s' "$out" | grep -c 'make doctor')" 1
|
||||||
check SD10-jq-really-absent "$(PATH="$bin" command -v jq | wc -l | tr -d ' ')" 0
|
check SD10-jq-really-absent "$(PATH="$bin" command -v jq | wc -l | tr -d ' ')" 0
|
||||||
|
|
||||||
|
# SD11: the digest section stays bash 3.2 safe (macOS)
|
||||||
|
sec=$(sed -n '/^# ── memory digest ──/,/^unset _ss_digest/p' "$SS")
|
||||||
|
check SD11-section-found "$([ -n "$sec" ] && echo yes || echo no)" yes
|
||||||
|
check SD11-bash32-safe "$(printf '%s\n' "$sec" \
|
||||||
|
| grep -c -e 'local -n' -e '\[-1\]' -e 'mapfile' -e 'declare -A' -e 'readarray')" 0
|
||||||
|
|
||||||
|
# SD12: a memory.sh that hangs costs under 3 s, no digest, exit 0
|
||||||
|
printf '#!/usr/bin/env bash\ncase "$*" in *index*) sleep 5 ;; esac\n' \
|
||||||
|
> "$WORK/sd9/lib/memory.sh"
|
||||||
|
p=$(mkproj sd12); blk "$p" 1 open "Slow"
|
||||||
|
t0=$(date +%s)
|
||||||
|
out=$(run "$p" - "$S9"); rc=$?
|
||||||
|
t1=$(date +%s)
|
||||||
|
check SD12-hang-bounded "$([ $((t1 - t0)) -lt 3 ] && echo yes || echo no)" yes
|
||||||
|
check SD12-rc "$rc" 0
|
||||||
|
check SD12-banner "$(printf '%s' "$out" | grep -c 'make doctor')" 1
|
||||||
|
check SD12-no-digest "$(printf '%s' "$out" | grep -c 'memory digest:')" 0
|
||||||
|
|
||||||
printf 'session-digest: %d passed, %d failed\n' "$pass" "$fail"
|
printf 'session-digest: %d passed, %d failed\n' "$pass" "$fail"
|
||||||
[ "$fail" -eq 0 ]
|
[ "$fail" -eq 0 ]
|
||||||
Reference in new issue
Block a user