#!/usr/bin/env bash # /workspace/hyperstitionstudio-core/deploy/run-vps-data-production.sh # Autonomous VPS-Native Periodic Data Production Orchestrator for hyperstitionstud.io. # Executes Stage 1 (Harvest) -> Stage 2 (Compilers) -> Stage 3 (Transactional Test & Isolation Gate # with Automatic Rollback on Failure) -> Stage 4 (tmpfs RAM Hydration via sync-vps-repos.sh --no-pull) # -> Optional Automated Git Push to git.libre.audio. set -euo pipefail on_error() { local exit_code=$? local line_no="${1:-unknown}" echo "[ERROR] run-vps-data-production.sh failed at line ${line_no} with exit code ${exit_code} while running: ${BASH_COMMAND}" >&2 exit "${exit_code}" } trap 'on_error ${LINENO}' ERR run_checked() { local label="$1" shift local out="" local rc=0 out="$("$@" 2>&1)" || rc=$? if [[ "${rc}" -ne 0 ]]; then echo "[ERROR] ${label} failed (exit code ${rc}): $*" >&2 if [[ -n "${out}" ]]; then echo "${out}" >&2 fi return "${rc}" fi } SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")" && pwd)" DEFAULT_REPOS_ROOT="$(cd "${SCRIPT_DIR}/../.." && pwd)" export REPOS_ROOT="${REPOS_ROOT:-${DEFAULT_REPOS_ROOT}}" export LIVE_REPOS="${LIVE_REPOS:-/var/www/hyperstitionstud.io/repos}" export PYTHONPATH="${REPOS_ROOT}/hyperstitionstudio-core/src${PYTHONPATH:+:${PYTHONPATH}}" if [[ -f "${REPOS_ROOT}/workspace.toml" ]]; then export HYPERSTITIONSTUDIO_CONFIG="${REPOS_ROOT}/workspace.toml" fi PYTHON_BIN="python3" if [[ -x "${REPOS_ROOT}/.venv/bin/python3" ]]; then PYTHON_BIN="${REPOS_ROOT}/.venv/bin/python3" fi WINDOW_MODE="us-closing-bell" OFFLINE_MODE=0 DO_PUSH=1 SKIP_HYDRATE=0 PUSH_ONLY=0 for arg in "$@"; do case "${arg}" in --window=*) WINDOW_MODE="${arg#*=}" ;; --offline|--no-network) OFFLINE_MODE=1 ;; --no-push) DO_PUSH=0 ;; --skip-hydrate) SKIP_HYDRATE=1 ;; --push-only) PUSH_ONLY=1 ;; esac done LOCK_FILE="${HYPERSTITION_HARVEST_LOCK:-/tmp/hyperstition-vps-harvest.lock}" if [[ -n "$(type -P flock || true)" ]]; then exec 9>"${LOCK_FILE}" if ! flock -n 9; then echo "[info] Another harvest/sync is currently holding ${LOCK_FILE}; exiting cleanly." exit 0 fi fi unset OPENROUTER_API_KEY OPENAI_API_KEY ANTHROPIC_API_KEY if [[ "${PUSH_ONLY}" -eq 0 ]]; then echo "[1/5] Running Stage 1 & Stage 2 periodic data production (window=${WINDOW_MODE}, offline=${OFFLINE_MODE})..." if [[ "${OFFLINE_MODE}" -eq 1 ]]; then run_checked "hs-quant-harvest (offline)" "${PYTHON_BIN}" -m hyperstitionstud_io.quant.collector --mode "${WINDOW_MODE}" run_checked "hs-macro-harvest (offline)" "${PYTHON_BIN}" -m hyperstitionstud_io.quant.macro --offline else run_checked "hs-quant-harvest (--append-history --live)" "${PYTHON_BIN}" -m hyperstitionstud_io.quant.collector --mode "${WINDOW_MODE}" --append-history --live run_checked "hs-macro-harvest (live FRED CSV)" "${PYTHON_BIN}" -m hyperstitionstud_io.quant.macro fi if [[ "${WINDOW_MODE}" == "utc-rollover" ]] && [[ -f "${REPOS_ROOT}/ingglish-hyperdope-gematria/ingglish-aq-archive/scripts/ci_provision_ingglish.py" ]]; then CI_OUT_DIR="${REPOS_ROOT}/ingglish-hyperdope-ci" if [[ ! -d "${CI_OUT_DIR}" ]] && [[ -d "${REPOS_ROOT}/hyperdope-ci-build" ]]; then CI_OUT_DIR="${REPOS_ROOT}/hyperdope-ci-build" fi if [[ -d "${CI_OUT_DIR}" ]]; then run_checked "ci_provision_ingglish.py (--tier 2)" "${PYTHON_BIN}" "${REPOS_ROOT}/ingglish-hyperdope-gematria/ingglish-aq-archive/scripts/ci_provision_ingglish.py" \ --archive-dir "${REPOS_ROOT}/ingglish-hyperdope-gematria/ingglish-aq-archive" \ --out-dir "${CI_OUT_DIR}" \ --tier 2 fi fi run_checked "quant.timeseries" "${PYTHON_BIN}" -m hyperstitionstud_io.quant.timeseries run_checked "hs-daily-brief" "${PYTHON_BIN}" -m hyperstitionstud_io.quant.daily_brief run_checked "build_encyclopedia_knowledge_graph.py" "${PYTHON_BIN}" "${REPOS_ROOT}/encyclopedia-knowledge-graph/scripts/build_encyclopedia_knowledge_graph.py" --verify-only echo "[2/5] Running Stage 3 transactional verification gates (hs-doctor, ReadOnlyIsolationGuard, test_core_suite)..." if ! "${PYTHON_BIN}" -m hyperstitionstud_io.ci.doctor || ! "${PYTHON_BIN}" "${REPOS_ROOT}/hyperstitionstudio-core/tests/test_core_suite.py"; then echo "[ROLLBACK] Verification gate failed! Rolling back uncommitted changes in ${REPOS_ROOT} without touching tmpfs..." >&2 for r in hyperstitionstudio-core ingglish-hyperdope-gematria encyclopedia-knowledge-graph; do if [[ -d "${REPOS_ROOT}/${r}/.git" ]]; then git -C "${REPOS_ROOT}/${r}" checkout -- . fi done exit 1 fi if [[ "${SKIP_HYDRATE}" -eq 0 ]]; then echo "[3/5] Hydrating tmpfs RAM webroot (${LIVE_REPOS}) via sync-vps-repos.sh --no-pull --no-caddy..." run_checked "sync-vps-repos.sh --no-pull --no-caddy" "${SCRIPT_DIR}/sync-vps-repos.sh" --no-pull --no-caddy else echo "[3/5] Skipping tmpfs hydration (--skip-hydrate)." fi else echo "[info] --push-only requested: skipping harvesting, compilers, verification gates, and RAM hydration." fi if [[ "${DO_PUSH}" -eq 1 ]] && [[ -f "/etc/hyperstitionstud.io/id_ed25519_git_libre_audio" ]]; then echo "[4/5] Committing and pushing automated VPS telemetry updates to git.libre.audio..." run_checked "hs-readme-sync --hooks-only" "${PYTHON_BIN}" -m hyperstitionstud_io.publishing.readme_sync --hooks-only DEPLOY_KEY="/etc/hyperstitionstud.io/id_ed25519_git_libre_audio" GIT_SSH_CMD="ssh -i ${DEPLOY_KEY} -p 2222 -o IdentitiesOnly=yes -o IdentityAgent=none -o BatchMode=yes -o StrictHostKeyChecking=accept-new" export GIT_SSH_COMMAND="${GIT_SSH_CMD}" unset SSH_AUTH_SOCK || true GIT_SSH_FLAGS=(-c "core.sshCommand=${GIT_SSH_CMD}") STAMP_UTC="$(date -u +%Y-%m-%dT%H:%M:%SZ)" for r in hyperstitionstudio-core ingglish-hyperdope-gematria encyclopedia-knowledge-graph; do if [[ -d "${REPOS_ROOT}/${r}/.git" ]]; then if [[ "${PUSH_ONLY}" -eq 1 ]]; then echo " -> Inspecting ${r} for telemetry updates..." fi if [[ -z "$(git "${GIT_SSH_FLAGS[@]}" -C "${REPOS_ROOT}/${r}" config user.name || true)" ]]; then git "${GIT_SSH_FLAGS[@]}" -C "${REPOS_ROOT}/${r}" config user.name "Hyperstition VPS Bot" git "${GIT_SSH_FLAGS[@]}" -C "${REPOS_ROOT}/${r}" config user.email "vps-bot@hyperstitionstud.io" fi forge_ssh="ssh://git@git.libre.audio:2222/seasharp" git "${GIT_SSH_FLAGS[@]}" -C "${REPOS_ROOT}/${r}" config core.sshCommand "${GIT_SSH_CMD}" || true git "${GIT_SSH_FLAGS[@]}" -C "${REPOS_ROOT}/${r}" remote set-url origin "${forge_ssh}/${r}.git" || true if [[ "${r}" == "hyperstitionstudio-core" ]]; then TELEMETRY_SNAP_DIR="/tmp/hyperstition-vps-prod-telemetry-snap" rm -rf "${TELEMETRY_SNAP_DIR}" run_checked "snapshot telemetry (${r})" "${PYTHON_BIN}" -m hyperstitionstud_io.quant.collector --snapshot-telemetry "${TELEMETRY_SNAP_DIR}" run_checked "git fetch (${r})" git "${GIT_SSH_FLAGS[@]}" -C "${REPOS_ROOT}/${r}" fetch --force origin main --tags local_head="$(git -C "${REPOS_ROOT}/${r}" rev-parse HEAD)" remote_head="$(git -C "${REPOS_ROOT}/${r}" rev-parse origin/main)" if [[ "${local_head}" != "${remote_head}" ]]; then if [[ "${PUSH_ONLY}" -eq 1 ]]; then echo " [sync] Advancing ${r} base to origin/main and reconciling telemetry snapshot..." fi run_checked "git reset --hard origin/main (${r})" git -C "${REPOS_ROOT}/${r}" reset --hard origin/main git -C "${REPOS_ROOT}/${r}" stash clear || true run_checked "reconcile telemetry (${r})" "${PYTHON_BIN}" -m hyperstitionstud_io.quant.collector --reconcile-telemetry "${TELEMETRY_SNAP_DIR}" fi rm -rf "${TELEMETRY_SNAP_DIR}" else run_checked "git fetch (${r})" git "${GIT_SSH_FLAGS[@]}" -C "${REPOS_ROOT}/${r}" fetch --force origin main --tags run_checked "git pull --rebase --autostash (${r})" git "${GIT_SSH_FLAGS[@]}" -C "${REPOS_ROOT}/${r}" pull --rebase -X theirs --autostash origin main fi git "${GIT_SSH_FLAGS[@]}" -C "${REPOS_ROOT}/${r}" add -A if ! git "${GIT_SSH_FLAGS[@]}" -C "${REPOS_ROOT}/${r}" diff --cached --quiet; then if [[ "${PUSH_ONLY}" -eq 1 ]]; then echo " [commit] Committing new telemetry changes in ${r}..." fi run_checked "git commit (${r})" git "${GIT_SSH_FLAGS[@]}" -C "${REPOS_ROOT}/${r}" commit -m "chore(telemetry): automated VPS ${WINDOW_MODE} harvest (${STAMP_UTC})" if [[ "${PUSH_ONLY}" -eq 1 ]]; then echo " [push] Pushing ${r} to git.libre.audio..." fi run_checked "git push (${r})" git "${GIT_SSH_FLAGS[@]}" -C "${REPOS_ROOT}/${r}" push origin main if [[ "${PUSH_ONLY}" -eq 1 ]]; then echo " [ok] Repository ${r} successfully pushed to git.libre.audio ($(git -C "${REPOS_ROOT}/${r}" rev-parse --short HEAD))." fi else if [[ "${PUSH_ONLY}" -eq 1 ]]; then echo " [clean] Repository ${r} working tree is clean and in sync with origin/main ($(git -C "${REPOS_ROOT}/${r}" rev-parse --short HEAD))." fi fi fi done else echo "[4/5] Skipping automated git.libre.audio push (no-push or deploy key absent)." fi echo "[5/5] VPS periodic data production (${WINDOW_MODE}) completed and verified."