#!/usr/bin/env bash
# Q118 gate: the pipe change-monitor retries a folder read before calling it a
# failure -- 3 attempts, 2 s apart, the last code named, and a wake rather than
# a fabricated deletion when every attempt fails.
#
# The artifact under repair is NOT in this repository -- the pipe's monitor lives
# at /root/.hermes/scripts/troupe_pipe_monitor.py because the cron job runs it by
# host path -- so like CHECK-q115 this gate reads a host path. That is stated,
# not hidden (the Q106 host-pin class); moving the monitor into the tree is the
# pipe's decision, not this item's. Everything the gate grades is the deployed
# retry loop itself: the fake `curl` sits at the module's real boundary, so no
# function under test is stubbed.
set -u
HERE="$(cd "$(dirname "$0")" && pwd)"
VERIFY="$HERE/q118_verify.py"

if [ ! -f "$VERIFY" ]; then
    echo "FAIL: verifier missing at $VERIFY" >&2
    exit 2
fi

out="$(python3 "$VERIFY" 2>&1)"
rc=$?
printf '%s\n' "$out" | grep -E '^FAIL:|^q118:' || true

if [ "$rc" -eq 2 ]; then
    echo "FAIL: could not grade (rc 2) -- see the line above" >&2
    exit 2
fi
if [ "$rc" -ne 0 ]; then
    echo "FAIL: rc=$rc -- a leg above is red" >&2
    exit 1
fi
if ! printf '%s\n' "$out" | grep -q '^q118: 8 leg(s), 8 passed, 0 fault(s)$'; then
    echo "FAIL: expected 8/8 legs, got: $(printf '%s\n' "$out" | tail -1)" >&2
    exit 1
fi
echo "q118: retry absorbs a transient read, names attempts= and last=, wakes on exhaustion -- OK"
exit 0
