#!/usr/bin/env bash
# spool session_end hook for Codex CLI (managed by `spool mcp install`).
#
# Codex sends this hook when the session ends. We run the distill
# pipeline (R3 self-tag heuristics -> ledger).
#
# Failure-silent: any non-zero exit from spool is swallowed (D7).
set -e

SPOOL_BIN="@@SPOOL_BIN@@"
SPOOL_CONFIG="@@SPOOL_CONFIG@@"
CWD="${CODEX_PROJECT_DIR:-$PWD}"

if [ ! -x "$SPOOL_BIN" ]; then
  exit 0
fi

"$SPOOL_BIN" hook stop \
  --config "$SPOOL_CONFIG" \
  --cwd "$CWD" \
  >/dev/null 2>&1 || true
exit 0
