#!/usr/bin/env bash # /workspace/hyperstitionstudio-core/deploy/run-vps-archive-rotation.sh # Daily 00:20 UTC (19:20 EST) Cold-Archive Rotation & Quiescent SQLite Logrotate Runner for hyperstitionstud.io # Enforces 0 LLM tokens, 0 API keys, full stdout/stderr visibility, flock concurrency protection, and Gematria-Pipeline Immunity. set -euo pipefail REPOS_ROOT="/srv/hyperstitionstud.io/repos" LIVE_REPOS="/var/www/hyperstitionstud.io/repos" LOCK_FILE="/tmp/hyperstition-archive-rotation.lock" DRY_RUN=0 while [[ $# -gt 0 ]]; do case "$1" in --repos-root=*) REPOS_ROOT="${1#*=}" shift ;; --repos-root) REPOS_ROOT="${2:-}" shift 2 ;; --live-repos=*) LIVE_REPOS="${1#*=}" shift ;; --live-repos) LIVE_REPOS="${2:-}" shift 2 ;; --dry-run) DRY_RUN=1 shift ;; *) echo "[ERROR] Unknown argument: $1" >&2 exit 2 ;; esac done exec 9>"${LOCK_FILE}" if ! flock -n 9; then echo "[SKIP] Another hyperstition-archive-rotation run holds ${LOCK_FILE}; exiting cleanly." exit 0 fi export TZ="UTC" export PYTHONPATH="${REPOS_ROOT}/hyperstitionstudio-core/src" unset OPENROUTER_API_KEY OPENAI_API_KEY ANTHROPIC_API_KEY echo "[1/3] Running Cold-Storage Plan Archive Rotation (hs-archive-rotate) against ${REPOS_ROOT}..." if [[ "${DRY_RUN}" -eq 1 ]]; then ROTATE_JSON="$(python3 -m hyperstitionstud_io.publishing.archive_rotator --workspace-root "${REPOS_ROOT}" --dry-run)" else ROTATE_JSON="$(python3 -m hyperstitionstud_io.publishing.archive_rotator --workspace-root "${REPOS_ROOT}")" fi echo "${ROTATE_JSON}" echo "[2/3] Running Quiescent SQLite Telemetry Logrotate Check (100M threshold)..." if [[ "${DRY_RUN}" -eq 0 ]]; then python3 -m hyperstitionstud_io.db.sqlite_meta --compress-archive --truncate-quiescent --max-mb 100 fi echo "[3/3] Checking whether rotated docs/archive/ bundles require tmpfs webroot hydration..." ROTATED_COUNT="$(python3 -c 'import json,sys; d=json.loads(sys.argv[1]); print(int(d.get("rotated_loose_files", 0)))' "${ROTATE_JSON}")" if [[ "${DRY_RUN}" -eq 0 ]] && [[ "${ROTATED_COUNT}" -gt 0 ]] && [[ -d "${LIVE_REPOS}" ]]; then echo "[SYNC] Rotated ${ROTATED_COUNT} loose archive file(s); refreshing ${LIVE_REPOS}..." "${REPOS_ROOT}/hyperstitionstudio-core/deploy/sync-vps-repos.sh" \ --repos-root="${REPOS_ROOT}" \ --live-repos="${LIVE_REPOS}" \ --no-pull \ --no-caddy else echo "[OK] No loose archive files required rotation (rotated_loose_files=${ROTATED_COUNT}); tmpfs hydration skipped." fi