#!/bin/bash
# UserPromptSubmit hook — the recall cue (Simplification Project 1, plan
# .claude/plans/simplification-1-recall-cue.md, act:e46ce733).
#
# THE CONTRACT. On every prompt, ask the project's recall index whether the
# record already holds a strong, distinct answer and — only when the project
# is switched `on` in watchtower's config — hand the session ONE line naming
# that file by title and path. A POINTER, NEVER CONTENT: pasted record text is
# an old fact sitting in the conversation looking current. SHADOW BY DEFAULT:
# every project logs what the cue would have said and injects nothing until
# `watchtower-recall-cue.mjs mode --project <name> --set on`. ITS OWN LOG:
# state/recall-cue-log.jsonl, never recall-log.jsonl, so "a session chose to
# ask" is never inflated by cue fires. NEVER BLOCKS: exit 0 in every path,
# never exit 2, never `decision: block`; the registration carries timeout 2.
#
# FAIL-OPEN. A UserPromptSubmit hook that errors is a spurious failure on
# every single turn. Missing runtime script, the wrong node, a locked index,
# malformed stdin, broken pipe — all pass through silently; the script logs
# its own outcome (`busy`, `no-index`, `unregistered`, `error`) so a quietly
# broken cue is visible at review rather than mistaken for "found nothing".
#
# WHICH NODE. The runtime's better-sqlite3 is built for one node ABI (the one
# /watchtower install recorded), and a hook runs under the SESSION's PATH,
# whose first `node` may be the wrong one. So this wrapper selects node the
# way the ring runners do — WATCHTOWER_NODE_PATH / NODE_PATH from
# ~/.claude-cabinet/watchtower/env — but it must NOT `source` that file: the
# file also holds ANTHROPIC_API_KEY, and a per-prompt hook has no business
# exporting it into a child process. Only the two lines it needs are parsed.

# Ignore SIGPIPE: a reader that hung up before we finished writing must not
# turn into exit 141 ("failed hook") on a turn where nothing went wrong.
trap '' PIPE

# Consume stdin unconditionally (a hook that doesn't read its payload can
# SIGPIPE the caller).
PAYLOAD=$(cat 2>/dev/null)

RUNTIME="${HOME}/.claude-cabinet/watchtower"
SCRIPT="${RUNTIME}/scripts/watchtower-recall-cue.mjs"
[ -f "${SCRIPT}" ] || exit 0

# Parse ONLY WATCHTOWER_NODE_PATH= and NODE_PATH= from the env file —
# line-anchored, an optional `export ` prefix, surrounding quotes stripped,
# last occurrence wins. Never `source`, never `set -a`.
NODE_BIN=""
CHILD_NODE_PATH=""
ENV_FILE="${RUNTIME}/env"
if [ -f "${ENV_FILE}" ]; then
  while IFS= read -r line || [ -n "$line" ]; do
    case "$line" in
      WATCHTOWER_NODE_PATH=*|"export WATCHTOWER_NODE_PATH="*)
        v="${line#*WATCHTOWER_NODE_PATH=}" ;;
      NODE_PATH=*|"export NODE_PATH="*)
        v="${line#*NODE_PATH=}" ;;
      *) continue ;;
    esac
    v="${v%\"}"; v="${v#\"}"; v="${v%\'}"; v="${v#\'}"
    case "$line" in
      *WATCHTOWER_NODE_PATH=*) NODE_BIN="$v" ;;
      *) CHILD_NODE_PATH="$v" ;;
    esac
  done < "${ENV_FILE}" 2>/dev/null
fi

if [ -z "${NODE_BIN}" ] || [ ! -x "${NODE_BIN}" ]; then
  NODE_BIN="$(command -v node 2>/dev/null || true)"
fi
[ -n "${NODE_BIN}" ] || exit 0

if [ -n "${CHILD_NODE_PATH}" ]; then
  OUT=$(printf '%s' "$PAYLOAD" | NODE_PATH="${CHILD_NODE_PATH}" "${NODE_BIN}" "${SCRIPT}" hook 2>/dev/null)
else
  OUT=$(printf '%s' "$PAYLOAD" | "${NODE_BIN}" "${SCRIPT}" hook 2>/dev/null)
fi

# Emit only a non-empty payload (an injection in `on`). The write may fail
# (see the PIPE trap); a reader that hung up is not our problem.
if [ -n "${OUT}" ]; then
  printf '%s' "${OUT}" 2>/dev/null || true
fi

exit 0
