#!/usr/bin/env bash
# memory-flush-prompt.sh — surface memory-flush instructions on PreCompact / SessionEnd.
# Stdout is injected into the conversation context so Claude reviews and writes
# memory files before the session boundary.
#
# Schema and write rules: ~/.claude/CLAUDE.md

set -e

DEV_DIR="${AILI_DEV_DIR:-$HOME/Development}"
DEV_SLUG="$(printf '%s' "$DEV_DIR" | sed 's|/|-|g')"
MEM_DIR="$HOME/.claude/projects/${DEV_SLUG}/memory"

case "${1:-}" in
  precompact)
    cat <<EOF
[memory flush — pre-compact]

Context is about to be compacted. Before it proceeds, review this session for
memory-worthy observations and write them now. Categories:
  - user        role / preferences / knowledge details
  - feedback    corrections OR validated wins about working style
  - project     new state, decisions, or non-obvious facts about active work
  - reference   external systems / locations / endpoints to remember

For anything worth saving:
  1. Write or update file(s) in ${MEM_DIR}/
     using the schema in ~/.claude/CLAUDE.md (name, description, type, created,
     event_date if project, optional updates/extends/derives/related).
  2. If a new memory updates an older one: append \`superseded_by: <new>.md\` to
     the older file's frontmatter, \`mv\` it to memory/archive/, remove its line
     from MEMORY.md.
  3. Append entries under a \`## YYYY-MM-DD\` heading to memory/log.md describing
     added / updated / superseded files.
  4. Update MEMORY.md if the cluster index changed.

If nothing in this session is worth saving, say so in one line and skip.
EOF
    ;;
  sessionend)
    cat <<EOF
[memory flush — session end]

Session is ending. Same flush procedure as pre-compact: review for
memory-worthy observations and write files now per ~/.claude/CLAUDE.md.
Target dir: ${MEM_DIR}/
Skip with a one-line note if nothing is worth saving.
EOF
    ;;
  *)
    echo "usage: memory-flush-prompt.sh {precompact|sessionend}" >&2
    exit 2
    ;;
esac

exit 0
