#!/usr/bin/env bash
# Phase 83-07: Mid-session intent classifier (Tier 2).
#
# Thin bash wrapper around scripts/intent-classifier.cjs. Exists because
# hooks/run-hook.cmd unconditionally `exec bash`s the target file, so a
# pure `#!/usr/bin/env node` script would be mis-dispatched. Same pattern
# as scripts/write-scope-check from 83-06.
#
# Passes stdin through unchanged. Propagates exit code.
#
# ---------------------------------------------------------------------------
# Phase 88-05: background regen runner ("lazy commit" for MINTO regen).
#
# On every UserPromptSubmit the debouncer drains queue items older than 30s,
# appends them to .mindrian/pending-tier1-regen.json (atomic tmp+rename), and
# spawns tier-0 regens in the BACKGROUND via
#   node scripts/vault-section-minto-generator.cjs --write <roomDir> --section <name>
# (no --narrative flag = tier-0 fallback path from Phase 81).
#
# Quick 261030-a2s win 1: the drain now runs at the top of the classifier
# process (scripts/minto-drain-on-prompt.cjs, called before the classifier
# reads stdin), so each prompt starts node once instead of twice. This
# wrapper only finds the room and, when that room has a .mindrian folder,
# exports MOS_MINTO_DRAIN_ROOM for the classifier; otherwise it unsets it,
# so an inherited value never triggers a drain. The order of effects is
# unchanged: the drain first, then the classifier.
#
# Unchanged: the soft-fail contract (a broken dependency never breaks the
# user-facing prompt-submit path, and the drain writes nothing to stdout or
# stderr), the 500 ms drain timeout, and the regens still spawn detached and
# unref'd (fire and forget). This wrapper still never reads stdin; the
# classifier consumes it as before.
# ---------------------------------------------------------------------------
set -eu
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"

# --- Phase 88-05 drain room (the drain itself runs inside the classifier) --
{
  ROOM_DIR=$("${SCRIPT_DIR}/resolve-room" 2>/dev/null) || ROOM_DIR=""
  if [ -n "$ROOM_DIR" ] && [ -d "$ROOM_DIR" ] && [ -d "$ROOM_DIR/.mindrian" ]; then
    export MOS_MINTO_DRAIN_ROOM="$ROOM_DIR"
  else
    unset MOS_MINTO_DRAIN_ROOM
  fi
} 2>/dev/null || true
# --- end Phase 88-05 drain room --------------------------------------------

exec node "${SCRIPT_DIR}/intent-classifier.cjs" "$@"
