#!/usr/bin/env bash
# SessionStart hook: inject the ASUCB operating contract (AGENTS.md) as
# additionalContext so the always-on workflow (Action First, Setup Gate,
# Write Safety Guard, routing, HPI/ID standard) is present every session in
# both Claude Code and Codex, independent of cwd or which runtime loaded.
#
# Claude Code consumes the JSON `hookSpecificOutput.additionalContext`.
# Runtimes that inject raw hook stdout still get the contract text via the
# no-python fallback. Failure is silent and non-blocking: a missing contract
# or interpreter must never break session start.
set -u

ROOT="${ASUCB_PLUGIN_ROOT:-${CODEX_PLUGIN_ROOT:-${CLAUDE_PLUGIN_ROOT:-}}}"
if [ -z "$ROOT" ]; then
  ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
fi

CONTRACT="$ROOT/AGENTS.md"
[ -f "$CONTRACT" ] || exit 0

PY="$ROOT/bin/python3"
[ -x "$PY" ] || PY="$(command -v python3 || true)"

if [ -n "$PY" ]; then
  "$PY" - "$CONTRACT" <<'PYEOF'
import json, sys
try:
    text = open(sys.argv[1], encoding="utf-8").read()
except OSError:
    sys.exit(0)
print(json.dumps({
    "hookSpecificOutput": {
        "hookEventName": "SessionStart",
        "additionalContext": text,
    }
}))
PYEOF
else
  cat "$CONTRACT"
fi
exit 0
