#!/usr/bin/env bash
# memory-index-guard — PostToolUse hook on Write/Edit that ensures
# memory-directory writes get indexed in MEMORY.md.
#
# Triggers: PostToolUse on Write/Edit when the target path is under
# ~/.claude/projects/*/memory/*.md (Claude Code's auto-memory dir).
#
# Behavior: runs scripts/validate-memory.mjs in --check-file mode against
# the file just written. If THAT file is not reachable from MEMORY.md
# (no index line and no region pointer covers it — i.e. it was written
# straight to disk and isn't in the working set), emits a system reminder
# pointing Claude at /cc-remember. Keys on the written file's reachability,
# NOT on the whole-dir validation — so an unrelated pre-existing violation
# (e.g. a stale region pointer) never makes the guard cry wolf on every
# write, and the relaxed enumeration rule doesn't silently retire it.
#
# This is the post-omega-winddown replacement for omega-memory-guard.sh.
# Where the omega guard BLOCKED markdown writes (redirecting them to
# omega_store), this guard ALLOWS the write but enforces indexing.
#
# Exit codes are advisory — never block the user's tool. Always exit 0.
# The output (if any) goes back to Claude as additional context.

set -u

# Read JSON tool-input from stdin (Claude Code passes hook payload).
PAYLOAD=$(cat)

# Extract tool_input.file_path. Falls back to empty.
FILE_PATH=$(echo "$PAYLOAD" | sed -n 's/.*"file_path"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' | head -1)

# Bail if no file_path or path doesn't match memory dir pattern.
if [ -z "$FILE_PATH" ]; then
  exit 0
fi
case "$FILE_PATH" in
  */.claude/projects/*/memory/*.md) ;;
  *) exit 0 ;;
esac

# Find the memory dir (strip the filename component).
MEMORY_DIR=$(dirname "$FILE_PATH")
PROJECT_ROOT=$(cd "$(dirname "$0")/../.." && pwd)
VALIDATOR="$PROJECT_ROOT/scripts/validate-memory.mjs"

# If the validator isn't installed in this project, no-op silently.
# (Hook might be installed in a project that hasn't received the
# Phase 3a scripts yet — graceful degradation.)
if [ ! -f "$VALIDATOR" ]; then
  exit 0
fi

# Check only THIS file's reachability. Exits non-zero with a one-line
# message when the just-written file isn't reachable from MEMORY.md.
OUTPUT=$(node "$VALIDATOR" --memory-dir "$MEMORY_DIR" --check-file "$FILE_PATH" --quiet 2>&1)
STATUS=$?

if [ $STATUS -eq 0 ]; then
  exit 0
fi

# Print the failure to stdout so it surfaces back to Claude as
# additional context. Frame as a suggestion, not a block.
cat <<REMINDER
[memory-index-guard] $(basename "$FILE_PATH") was written to a memory directory
but is not reachable from MEMORY.md:

$OUTPUT

The file is on disk but nothing makes it findable next session — it has no
working-set index line and no region pointer covers it. Use /cc-remember
(it indexes into the working set automatically), or add a covering region
pointer in MEMORY.md, or add a "## Curated entries (hand-authored)" line.
REMINDER

exit 0
