#!/bin/sh
# Drift guard for the RECHARGE vocabulary we teach. `commune across` was renamed to `recharge` on
# 2026-09-09 as a STAGED retirement: the old spelling stays understood everywhere and refused
# nowhere, because peers carry it in durable context that no release can reach.
#
# BOTH halves of that ruling are guarded, because each fails in the opposite direction:
#   - drop the new name and the rename never happened;
#   - teach the old name as CURRENT usage and the staging silently becomes a no-op, leaving two
#     live spellings with no signal about which one an agent should write.
# Shape-aware on the same model as tests/knock-vocabulary.sh: naming the old spelling is CORRECT
# and must stay legal — what is refused is naming it without marking it as the older/accepted one.
# [unit->REQ-RECHARGE-VOCABULARY]
set -u
ROOT=$(CDPATH= cd "$(dirname "$0")/.." && pwd)
cd "$ROOT" || exit 2
rc=0
ok()   { printf 'ok   %s\n' "$1"; }
fail() { printf 'FAIL: %s\n' "$1"; rc=1; }

MANIFEST="adapter/claude-spt.toml"
# Every surface that TEACHES the reset to an agent or a user. History (CHANGELOG, docs/adr,
# docs/plans, the root JIT plans) is deliberately absent: it describes what shipped under the name
# it shipped under, and rewriting it would make the release notes lie about their own releases.
TAUGHT="adapter/strings/briefs/live-ops.md
adapter/strings/briefs/work-discipline.md
adapter/strings/skills/live-normal.md
plugin/sptc/skills/commune/SKILL.md
plugin/sptc/skills/live/SKILL.md
CONTEXT.md"

for f in $TAUGHT; do
  [ -f "$f" ] || { fail "taught surface missing: $f"; continue; }

  # 1. The current name is present. A surface that teaches the reset without ever saying
  #    "recharge" is one an agent finishes reading still calling it something else.
  if grep -qi 'recharge' "$f"; then
    ok "$f names recharge"
  else
    fail "$f teaches the reset but never names recharge"
  fi

  # 2. Every occurrence of the old spelling sits on a line that MARKS it as the older/accepted one.
  #    A bare "commune across" on an instruction line is the staging quietly turning into a no-op.
  bad=$(grep -n -iE 'commune[ -]across|across[ -]commune|--across' "$f" \
        | grep -viE 'older|previous|legacy|still accepted|also accepted|not yet retired|accepted and means|spelling')
  if [ -n "$bad" ]; then
    fail "$f teaches the old spelling as current usage:"
    printf '%s\n' "$bad"
  else
    ok "$f names the old spelling only as accepted-legacy"
  fi
done

# 3. The flag. `--recharge` is the primary spelling the skill advertises, and its argument-hint —
#    often the only thing an agent sees before invoking — must carry it.
SKILL="plugin/sptc/skills/commune/SKILL.md"
grep -q -- '--recharge' "$SKILL" && ok "commune skill teaches --recharge" \
                                 || fail "commune skill does not teach --recharge"
grep -qE '^argument-hint:.*--recharge' "$SKILL" \
  && ok "argument-hint advertises --recharge" \
  || fail "argument-hint does not advertise --recharge"

# 4. THE STAGING, ON THE HINT. The keywords must KEEP the old spellings: an agent whose durable
#    context still says "commune across" is exactly the agent this tip needs to reach, and dropping
#    those keywords would silence the hint for the only population that still needs it. The tip
#    TEXT, meanwhile, must teach the new name.
hint=$(grep -n '^text = "Tip: /sptc:commune' "$MANIFEST")
[ -n "$hint" ] || { fail "no /sptc:commune hint line found in $MANIFEST"; exit 1; }
printf '%s\n' "$hint" | grep -q -- '--recharge' \
  && ok "commune hint teaches --recharge" || fail "commune hint does not teach --recharge"
grep -qE '^keywords = \[.*"commune across"' "$MANIFEST" \
  && ok "hint keywords still catch the old spelling" \
  || fail "hint keywords dropped \"commune across\" — the agents who most need this tip are the ones who only know that phrase"
grep -qE '^keywords = \[.*"recharge"' "$MANIFEST" \
  && ok "hint keywords catch recharge" || fail "hint keywords do not catch \"recharge\""

# 5. IDS ARE NOT RENAMED. An id is identity — renaming one is a delete plus an add that orphans
#    every tag pointing at it. If a REQ-RECHARGE-* id ever appears for one of the COMMUNE-named
#    requirements, someone renamed rather than superseded.
if grep -qE '^id = "REQ-(RECHARGE|ACROSS)-COMMUNE' traceable-reqs.toml; then
  fail "a COMMUNE requirement id was renamed — ids are identity; supersede, never rename"
else
  ok "commune requirement ids untouched"
fi

exit $rc
