Same contract as ygg-verify.sh wait (270 s cap, bounded tail, RUNNING exit 99, exit 98 for a thing gone with no completion evidence). Windows PIDs are found through tasklist, since kill -0 only knows Git Bash's own. Fixtures in tools/test-tools.sh; INDEX row added. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
74 lines
3.3 KiB
Shell
74 lines
3.3 KiB
Shell
#!/usr/bin/env bash
|
|
# One general waiter, so no agent hand-rolls a poll loop. Same contract as
|
|
# `ygg-verify.sh wait`: one call blocks at most 270 s (WAIT_MAX can only lower
|
|
# that), checks locally every 2 s (no model turn), and exits:
|
|
# 0 done; prints a bounded tail (at most 60 lines) of the log when one is given
|
|
# 99 still going; prints `RUNNING <n>s (...)`: call it again
|
|
# 98 the thing waited on is gone with no completion evidence
|
|
# Call it with a Bash tool `timeout` of at least 280000 ms.
|
|
#
|
|
# Forms:
|
|
# wait-on.sh pid <pid> [<log>] done when the process is gone (exit 0)
|
|
# wait-on.sh log <log> <done-regex> done when a log line matches the ERE
|
|
# wait-on.sh file <path> done when the file exists and is non-empty
|
|
# `log` accepts WAIT_PID=<pid>: if that process dies and the regex never matched
|
|
# (checked once more after the death), exit 98. Without WAIT_PID a log that does
|
|
# not exist after 30 s is exit 98 (never started). `pid` cannot tell a finished
|
|
# process from one that never existed: both are exit 0, so pass a log and read it.
|
|
#
|
|
# PIDs: on Git Bash a Windows PID (PowerShell Start-Process .Id, a task manager
|
|
# number) is seen by `tasklist`, but `kill -0` does not know it; Git Bash's own
|
|
# `$!` is known to `kill -0` and also to `tasklist`'s WINPID only by luck. Alive
|
|
# means either check says so. Linux uses `kill -0`.
|
|
set -uo pipefail
|
|
mode=${1:-}; cap=${WAIT_MAX:-270}; [ "$cap" -le 270 ] || cap=270
|
|
t0=$(date +%s)
|
|
usage() { echo "usage: wait-on.sh pid <pid> [<log>] | log <log> <done-regex> | file <path>" >&2; exit 2; }
|
|
|
|
alive() {
|
|
local p=$1
|
|
case "$p" in ''|*[!0-9]*) return 1 ;; esac
|
|
kill -0 "$p" 2>/dev/null && return 0
|
|
case "$(uname -s)" in
|
|
MINGW*|MSYS*|CYGWIN*)
|
|
tasklist //FI "PID eq $p" //NH 2>/dev/null | tr -d '\r' | grep -qE "[[:space:]]$p[[:space:]]" && return 0 ;;
|
|
esac
|
|
return 1
|
|
}
|
|
tail60() { [ -n "${1:-}" ] && [ -f "$1" ] && tail -n 60 "$1" | cut -c1-400; return 0; }
|
|
running() { echo "RUNNING $(( $(date +%s) - t0 ))s ($1)"; exit 99; }
|
|
elapsed() { echo $(( $(date +%s) - t0 )); }
|
|
|
|
case "$mode" in
|
|
pid)
|
|
pid=${2:-}; log=${3:-}; [ -n "$pid" ] || usage
|
|
while :; do
|
|
if ! alive "$pid"; then tail60 "$log"; exit 0; fi
|
|
[ "$(elapsed)" -lt "$cap" ] || running "pid $pid alive; last log line: $(tail -n 1 "$log" 2>/dev/null | cut -c1-160)"
|
|
sleep 2
|
|
done ;;
|
|
log)
|
|
log=${2:-}; re=${3:-}; [ -n "$log" ] && [ -n "$re" ] || usage
|
|
wp=${WAIT_PID:-}
|
|
while :; do
|
|
if [ -f "$log" ] && grep -Eq -- "$re" "$log" 2>/dev/null; then tail60 "$log"; exit 0; fi
|
|
if [ -n "$wp" ] && ! alive "$wp"; then
|
|
sleep 1
|
|
if [ -f "$log" ] && grep -Eq -- "$re" "$log" 2>/dev/null; then tail60 "$log"; exit 0; fi
|
|
echo "wait-on: DEAD - pid $wp is gone and /$re/ never matched in $log"; tail60 "$log"; exit 98
|
|
fi
|
|
if [ -z "$wp" ] && [ ! -f "$log" ] && [ "$(elapsed)" -ge 30 ]; then
|
|
echo "wait-on: NOT STARTED - no $log after 30 s"; exit 98
|
|
fi
|
|
[ "$(elapsed)" -lt "$cap" ] || running "no /$re/ yet; last line: $(tail -n 1 "$log" 2>/dev/null | cut -c1-160)"
|
|
sleep 2
|
|
done ;;
|
|
file)
|
|
path=${2:-}; [ -n "$path" ] || usage
|
|
while :; do
|
|
[ -s "$path" ] && exit 0
|
|
[ "$(elapsed)" -lt "$cap" ] || running "$path not there yet"
|
|
sleep 2
|
|
done ;;
|
|
*) usage ;;
|
|
esac
|