#!/bin/sh
# Placement guard for `spt ring` guidance: ring is the PERCHLESS-ONLY reach. An agent that holds a
# perch — spt-hosted or harness-hosted — always uses `spt send`, because the reply lands on its
# perch; ring exists for the session that has no listener for a reply to arrive on. So the ring
# recommendation may appear ONLY in the no-perch brief, never in the perch brief or the identity
# brief a perched session reads (operator ruling 2026-07-30).
# Also guards the version floor: the pre-v0.46.0 hazard (ring adopting and deleting a perch
# directory whose ready marker was merely down) must stay stated wherever ring is recommended, so a
# reader on an older core is never told the safe-version story alone.
# Run: sh tests/ring-perchless-only.sh (exit 0 = pass).
ROOT=$(CDPATH= cd "$(dirname "$0")/.." && pwd)
BRIEFS="$ROOT/adapter/strings/briefs"
fail=0

# 1. The no-perch brief RECOMMENDS ring (it is the one surface that should).
# [unit->REQ-DIST-SESSIONSTART-BRIEF]
if grep -q 'spt ring' "$BRIEFS/messaging-no-perch.md"; then
  echo "ok   no-perch brief carries the ring reach"
else
  echo "FAIL no-perch brief lost the ring reach (perchless sessions have no other way to get a reply)"; fail=1
fi

# 2. NO perched surface mentions ring. A perched agent reading "ring" at all is the drift we are
#    guarding: it invites ring where `spt send` is correct, and ring's own guard then refuses it.
for f in "$BRIEFS/messaging-perch.md" "$BRIEFS/identity.md" "$BRIEFS/live-ops.md" "$BRIEFS/work-discipline.md"; do
  [ -f "$f" ] || continue
  if grep -q 'spt ring' "$f"; then
    echo "FAIL $(basename "$f") mentions spt ring — perched agents always use spt send"; fail=1
  else
    echo "ok   $(basename "$f") keeps ring out of a perched session's brief"
  fi
done

# 3. Wherever ring IS recommended, the pre-0.46.0 hazard stays stated with its version floor.
if grep -q '0\.46\.0' "$BRIEFS/messaging-no-perch.md" && grep -qi 'delet' "$BRIEFS/messaging-no-perch.md"; then
  echo "ok   ring guidance names the v0.46.0 floor and the perch-deletion hazard below it"
else
  echo "FAIL ring guidance must name the v0.46.0 safety floor AND what older cores did"; fail=1
fi

[ "$fail" -eq 0 ] && echo "PASS ring-perchless-only" || echo "FAIL ring-perchless-only"
exit "$fail"
