#!/usr/bin/env bash # mempalace-incremental-mine.sh # Re-mines only files changed since the last run, keeping the palace current # without full re-ingestion. # # Usage: # ./scripts/mempalace-incremental-mine.sh [workspace_dir] [palace_dir] # # Defaults: # workspace_dir — $WIZARD_HOME or /root/wizards/bezalel # palace_dir — $MEMPALACE_DIR or /.mempalace # # Dependencies: mempalace (vendored), find # # Refs: #1072 (Bezalel MemPalace Field Report) set -euo pipefail WORKSPACE="${1:-${WIZARD_HOME:-/root/wizards/bezalel}}" PALACE_DIR="${2:-${MEMPALACE_DIR:-$WORKSPACE/.mempalace}}" STAMP_FILE="$PALACE_DIR/.last_mine_ts" PALACE_PATH="$PALACE_DIR/palace" if [[ ! -d "$WORKSPACE" ]]; then echo "[mempalace-incremental-mine] ERROR: workspace not found: $WORKSPACE" >&2 exit 1 fi # Resolve mempalace binary — check vendored location first MEMPALACE_BIN="" for candidate in \ "$WORKSPACE/.vendor/mempalace/mempalace" \ "$WORKSPACE/.vendor/mempalace/bin/mempalace" \ "$(command -v mempalace 2>/dev/null || true)"; do if [[ -x "$candidate" ]]; then MEMPALACE_BIN="$candidate" break fi done if [[ -z "$MEMPALACE_BIN" ]]; then echo "[mempalace-incremental-mine] ERROR: mempalace binary not found." >&2 echo " Vendor it at $WORKSPACE/.vendor/mempalace/ or install it globally." >&2 exit 1 fi mkdir -p "$PALACE_DIR" # Determine changed files since last run if [[ -f "$STAMP_FILE" ]]; then SINCE=$(cat "$STAMP_FILE") echo "[mempalace-incremental-mine] Mining files changed since $SINCE" # Find files newer than the stamp file itself CHANGED_FILES=$(find "$WORKSPACE" \ -newer "$STAMP_FILE" \ -type f \ ! -path "*/.mempalace/*" \ ! -path "*/.git/*" \ ! -path "*/node_modules/*" \ ! -path "*/__pycache__/*" \ ! -name "*.pyc" \ 2>/dev/null || true) else echo "[mempalace-incremental-mine] No prior stamp found — running full mine." CHANGED_FILES="" fi if [[ -z "$CHANGED_FILES" && -f "$STAMP_FILE" ]]; then echo "[mempalace-incremental-mine] No changed files detected. Palace is current." exit 0 fi YAML_CONFIG="$WORKSPACE/mempalace.yaml" if [[ ! -f "$YAML_CONFIG" ]]; then echo "[mempalace-incremental-mine] WARNING: $YAML_CONFIG not found." >&2 echo " Room taxonomy will not be applied. Create mempalace.yaml to enable routing." >&2 YAML_ARGS=() else YAML_ARGS=(--config "$YAML_CONFIG") fi if [[ -n "$CHANGED_FILES" ]]; then # Mine only the changed files FILE_COUNT=$(echo "$CHANGED_FILES" | wc -l | tr -d ' ') echo "[mempalace-incremental-mine] Mining $FILE_COUNT changed file(s)..." echo "$CHANGED_FILES" | xargs -I{} "$MEMPALACE_BIN" mine "${YAML_ARGS[@]}" \ --palace "$PALACE_PATH" {} 2>&1 else # Full mine (first run) echo "[mempalace-incremental-mine] Running full mine of $WORKSPACE ..." "$MEMPALACE_BIN" mine "${YAML_ARGS[@]}" --palace "$PALACE_PATH" "$WORKSPACE" 2>&1 fi # Update stamp date -u +"%Y-%m-%dT%H:%M:%SZ" > "$STAMP_FILE" echo "[mempalace-incremental-mine] Done. Stamp updated: $(cat "$STAMP_FILE")"