#!/usr/bin/env bash # Watch Lenormand unify → on Gemini limit switch to Grok → then Tarot unify. set -u ROOT="$(cd "$(dirname "$0")/.." && pwd)" ENR="$ROOT/oracle_enricher" LEN_UNI="$ENR/out/lenormand_unify/unified.json" LEN_LOG="$ENR/out/lenormand_unify/unify.log" TAR_UNI="$ENR/out/tarot_unify/unified.json" TAR_LOG="$ENR/out/tarot_unify/unify.log" CHAIN_LOG="$ENR/out/enrich_chain.log" ENGINE_FILE="$ENR/out/enrich_engine" mkdir -p "$ENR/out" "$(dirname "$LEN_LOG")" "$(dirname "$TAR_LOG")" log() { echo "[$(date -u '+%Y-%m-%d %H:%M:%S UTC')] $*" | tee -a "$CHAIN_LOG"; } engine() { if [[ -f "$ENGINE_FILE" ]]; then cat "$ENGINE_FILE"; else echo gemini; fi } set_engine() { echo "$1" > "$ENGINE_FILE" log "ENGINE=$1" } looks_like_limit() { local f="$1" [[ -f "$f" ]] || return 1 grep -qiE 'quota|429|resource.exhausted|eligibility|rate limit|too many requests|limit exceeded' "$f" } lenormand_ok() { python3 - <<'PY' import json from pathlib import Path p=Path("/mnt/data/StudioProjects/999/oracle_enricher/out/lenormand_unify/unified.json") if not p.exists(): raise SystemExit(1) d=json.loads(p.read_text()) c=len(d.get("cards") or {}) p=len(d.get("pairs") or {}) t=len(d.get("triads") or []) print(f"cards={c} pairs={p} triads={t}") raise SystemExit(0 if c>=36 and p>=630 else 1) PY } tarot_ok() { python3 - <<'PY' import json from pathlib import Path p=Path("/mnt/data/StudioProjects/999/oracle_enricher/out/tarot_unify/unified.json") src=Path("/mnt/data/StudioProjects/999/oracle_enricher/data/sources/tarot_majors_corpus.json") if not p.exists(): raise SystemExit(1) d=json.loads(p.read_text()) need_pairs=231 if src.exists(): s=json.loads(src.read_text()) need_pairs=len(s.get("pairs") or {}) c=len(d.get("cards") or {}) pr=len(d.get("pairs") or {}) print(f"cards={c} pairs={pr}/{need_pairs}") raise SystemExit(0 if c>=22 and pr>=need_pairs else 1) PY } wait_pid() { local pid="$1" log "wait pid=$pid" while kill -0 "$pid" 2>/dev/null; do sleep 20; done log "pid $pid exited" } run_unify() { local which="$1" local eng eng="$(engine)" log "start $which ENGINE=$eng" cd "$ENR" || exit 1 if [[ "$eng" == grok ]]; then ENRICHER_ENGINE=grok python3 -u -m "scripts.unify_${which}" --mode all else ENRICHER_ENGINE=gemini python3 -u -m "scripts.unify_${which}" --mode all fi } # --- Lenormand --- LIVE=$(pgrep -f 'scripts.unify_lenormand' | head -1 || true) if [[ -n "${LIVE:-}" ]]; then wait_pid "$LIVE" fi TRIES=0 while ! lenormand_ok; do TRIES=$((TRIES+1)) if [[ "$TRIES" -gt 8 ]]; then log "FAILED lenormand after $TRIES tries" exit 1 fi if looks_like_limit "$LEN_LOG"; then set_engine grok fi run_unify lenormand || { if looks_like_limit "$LEN_LOG"; then set_engine grok; fi log "unify_lenormand exit non-zero, retry" sleep 8 } done log "LENORMAND DONE" # --- Tarot --- TRIES=0 while ! tarot_ok; do TRIES=$((TRIES+1)) if [[ "$TRIES" -gt 8 ]]; then log "FAILED tarot after $TRIES tries" exit 1 fi if looks_like_limit "$TAR_LOG"; then set_engine grok fi run_unify tarot || { if looks_like_limit "$TAR_LOG"; then set_engine grok; fi log "unify_tarot exit non-zero, retry" sleep 8 } done log "TAROT DONE" log "CHAIN DONE" exit 0