From 1182ba0cf2e82cebb3a351235c6fc3b8508b6684 Mon Sep 17 00:00:00 2001 From: Hermes Agent Date: Mon, 30 Nov 2026 06:44:26 +0000 Subject: [PATCH] feat(db,mcp,deploy): implement SQLite + FTS5 metadata/telemetry DB, automatic MCP/REST population, and quiescent logrotate.d compression (v2.0.19) --- deploy/hyperstition-mcp.service | 2 + deploy/logrotate.d/hyperstition-sqlite-meta | 43 + deploy/sync-vps-repos.sh | 17 +- deploy/test-first-deployment.sh | 21 +- package.json | 2 +- packages-js/ciphers-aq/package.json | 2 +- pyproject.toml | 3 +- scripts/build_gallery_manifest.py | 4 +- site/gallery/index.html | 2 +- site/index.html | 4 +- site/learn/index.html | 2 +- site/learn/learn-app.js | 2 +- site/quant/index.html | 4 +- site/quant/quant-app.js | 2 +- site/quant/quant-timeseries-data.js | 2 +- site/quant/quant-timeseries.json | 8 +- src/hyperstitionstud_io/__init__.py | 4 +- src/hyperstitionstud_io/ci/doctor.py | 20 + src/hyperstitionstud_io/db/__init__.py | 41 + src/hyperstitionstud_io/db/sqlite_meta.py | 1195 +++++++++++++++++++ src/hyperstitionstud_io/mcp/rest.py | 60 +- src/hyperstitionstud_io/mcp/server.py | 26 +- src/hyperstitionstud_io/quant/collector.py | 2 +- tests/test_core_suite.py | 129 ++ workspace.example.toml | 2 +- 25 files changed, 1562 insertions(+), 37 deletions(-) create mode 100644 deploy/logrotate.d/hyperstition-sqlite-meta create mode 100644 src/hyperstitionstud_io/db/__init__.py create mode 100644 src/hyperstitionstud_io/db/sqlite_meta.py diff --git a/deploy/hyperstition-mcp.service b/deploy/hyperstition-mcp.service index 5334801..5ede551 100644 --- a/deploy/hyperstition-mcp.service +++ b/deploy/hyperstition-mcp.service @@ -9,6 +9,8 @@ Type=simple WorkingDirectory=/srv/hyperstitionstud.io/repos Environment="HYPERSTITIONSTUDIO_CONFIG=/srv/hyperstitionstud.io/repos/workspace.toml" Environment="PYTHONPATH=/srv/hyperstitionstud.io/repos/hyperstitionstudio-core/src" +Environment="HYPERSTITION_TELEMETRY_DB_PATH=/tmp/hyperstition-api-telemetry.db" +Environment="HYPERSTITION_SQLITE_MAX_MB=100" ExecStart=/srv/hyperstitionstud.io/repos/.venv/bin/python3 /srv/hyperstitionstud.io/repos/hyperstitionstudio-core/src/hyperstitionstud_io/mcp/server.py --transport http --host 127.0.0.1 --port 8095 Restart=always RestartSec=2 diff --git a/deploy/logrotate.d/hyperstition-sqlite-meta b/deploy/logrotate.d/hyperstition-sqlite-meta new file mode 100644 index 0000000..b329634 --- /dev/null +++ b/deploy/logrotate.d/hyperstition-sqlite-meta @@ -0,0 +1,43 @@ +# /etc/logrotate.d/hyperstition-sqlite-meta +# Quiescent size-based compression & truncation for the hyperstitionstud.io SQLite REST/MCP API metadata DB. +# +# Why quiescent snapshot compression + in-place SQLite truncation is used: +# Raw byte truncation (`copytruncate`) on a live SQLite `.db` or `-wal` file while readers or +# writers hold locks corrupts the SQLite B-tree page header and WAL frame index. +# Instead, `prerotate` invokes `hyperstitionstud_io.db.sqlite_meta --truncate-quiescent --compress-archive`, which: +# 1. Sets `PRAGMA busy_timeout = 30000` (waits up to 30s for in-flight requests to finish), +# 2. Acquires `BEGIN EXCLUSIVE` to guarantee 100% SQLite reader/writer quiescence, +# 3. Writes a consistent point-in-time SQLite snapshot and compresses it with `gzip -9` +# to `.1.gz` (rotating `.1.gz` .. `.12.gz` rather than removing history), +# 4. Purges the live telemetry tables and commits cleanly, +# 5. Executes `PRAGMA wal_checkpoint(TRUNCATE)` + `VACUUM` + `PRAGMA optimize` on the live DB, and +# 6. Verifies `PRAGMA integrity_check == 'ok'` on the live DB. +# +# Configurable threshold: +# - Default logrotate trigger: `size 100M` (100 MB) +# - Runtime override: export `HYPERSTITION_SQLITE_MAX_MB` (default `100`) or `HYPERSTITION_SQLITE_MAX_BYTES`. + +/tmp/hyperstition-api-telemetry.db /var/lib/hyperstitionstud.io/api-telemetry.db { + size 100M + rotate 12 + compress + delaycompress + missingok + notifempty + nocreate + sharedscripts + prerotate + TARGET_DB="${1:-${HYPERSTITION_TELEMETRY_DB_PATH:-/tmp/hyperstition-api-telemetry.db}}" + MAX_MB="${HYPERSTITION_SQLITE_MAX_MB:-100}" + MAX_BYTES="${HYPERSTITION_SQLITE_MAX_BYTES:-$((MAX_MB * 1024 * 1024))}" + ROTATE_KEEP="${HYPERSTITION_SQLITE_ROTATE_KEEP:-12}" + PYTHON_BIN="${HYPERSTITION_PYTHON_BIN:-python3}" + PYTHONPATH="${PYTHONPATH:-/srv/hyperstitionstud.io/repos/hyperstitionstudio-core/src:/workspace/hyperstitionstudio-core/src}" \ + "${PYTHON_BIN}" -m hyperstitionstud_io.db.sqlite_meta \ + --truncate-quiescent \ + --compress-archive \ + --rotate-keep "${ROTATE_KEEP}" \ + --db "${TARGET_DB}" \ + --max-bytes "${MAX_BYTES}" + endscript +} diff --git a/deploy/sync-vps-repos.sh b/deploy/sync-vps-repos.sh index f0a56c1..ba1b162 100755 --- a/deploy/sync-vps-repos.sh +++ b/deploy/sync-vps-repos.sh @@ -3,7 +3,8 @@ # Hydrates per-repository install directories from REPOS_ROOT into LIVE_REPOS (tmpfs RAM mount), # supplies untracked haiperdohp-jemaytreeah/.gitignore files (config.js, db.json, db_lite.json), # creates top-level route symlinks, pre-compresses (.br / .gz) static assets, and idempotently -# syncs/validates/reloads /etc/caddy/Caddyfile and systemd units by default (negated via --no-caddy). +# syncs/validates/reloads /etc/caddy/Caddyfile, systemd units, and /etc/logrotate.d/hyperstition-sqlite-meta +# by default (negated via --no-caddy). # # Usage: # ./sync-vps-repos.sh [all|] [--no-pull] [--no-caddy|--no-services] @@ -181,9 +182,21 @@ if [[ -n "$(type -P brotli || true)" ]]; then -exec brotli -q 11 -k -f {} \; fi -# Step 5: Idempotent systemd unit & /etc/caddy/Caddyfile synchronization (enabled by default; skip with --no-caddy) +# Step 5: Idempotent systemd unit, logrotate.d & /etc/caddy/Caddyfile synchronization (enabled by default; skip with --no-caddy) if [[ "${DO_CADDY}" -eq 1 ]]; then DEPLOY_DIR="${REPOS_ROOT}/hyperstitionstudio-core/deploy" + SRC_LOGROTATE="${DEPLOY_DIR}/logrotate.d/hyperstition-sqlite-meta" + DST_LOGROTATE="/etc/logrotate.d/hyperstition-sqlite-meta" + if [[ -f "${SRC_LOGROTATE}" ]] && [[ -d "/etc/logrotate.d" ]] && { [[ -w "/etc/logrotate.d" ]] || [[ -n "${SUDO}" ]]; }; then + if [[ -f "${DST_LOGROTATE}" ]] && cmp -s "${SRC_LOGROTATE}" "${DST_LOGROTATE}"; then + echo "[5/5] ${DST_LOGROTATE} is already identical to deploy/logrotate.d/hyperstition-sqlite-meta." + else + echo "[5/5] Installing quiescent SQLite logrotate rule to ${DST_LOGROTATE}..." + run_checked "copy hyperstition-sqlite-meta logrotate rule" ${SUDO} cp "${SRC_LOGROTATE}" "${DST_LOGROTATE}" + run_checked "chmod 0644 ${DST_LOGROTATE}" ${SUDO} chmod 0644 "${DST_LOGROTATE}" + fi + fi + if [[ -n "$(type -P systemctl || true)" ]] && [[ -d "/run/systemd/system" ]]; then UNITS_CHANGED=0 for unit_name in "hyperstition-tmpfs-sync.service" "hyperstition-mcp.service"; do diff --git a/deploy/test-first-deployment.sh b/deploy/test-first-deployment.sh index ebe51cc..b29e413 100755 --- a/deploy/test-first-deployment.sh +++ b/deploy/test-first-deployment.sh @@ -28,7 +28,8 @@ trap cleanup EXIT if [[ -z "${TARGET_BASE_URL}" ]]; then export LIVE_REPOS="${LIVE_REPOS:-/tmp/hyperstition-live-repos}" - rm -rf "${LIVE_REPOS}" + export HYPERSTITION_TELEMETRY_DB_PATH="${HYPERSTITION_TELEMETRY_DB_PATH:-/tmp/hyperstition-deploy-test-telemetry.db}" + rm -rf "${LIVE_REPOS}" "${HYPERSTITION_TELEMETRY_DB_PATH}" "${HYPERSTITION_TELEMETRY_DB_PATH}-wal" "${HYPERSTITION_TELEMETRY_DB_PATH}-shm" echo "[1/4] Hydrating per-repository webroot at ${LIVE_REPOS} via sync-vps-repos.sh --no-pull..." "${SCRIPT_DIR}/sync-vps-repos.sh" --no-pull @@ -129,4 +130,22 @@ for api_sub in "/status" "/openapi.json" "/quant/instruments/NVDA" "/zoology/pok echo " [200 OK] GET ${API_V1_URL}${api_sub}" done +if [[ -n "${HYPERSTITION_TELEMETRY_DB_PATH:-}" ]] && [[ -f "${LIVE_REPOS}/hyperstitionstudio-core/deploy/logrotate.d/hyperstition-sqlite-meta" ]]; then + echo "[4c/4] Verifying SQLite REST/MCP metadata DB population & quiescent logrotate.d compression + truncation..." + PYTHONPATH="${SCRIPT_DIR}/../src" python3 - <<'PY' +import os +from pathlib import Path +from hyperstitionstud_io.db.sqlite_meta import get_api_telemetry_summary, run_logrotate_directive + +tel_db = Path(os.environ["HYPERSTITION_TELEMETRY_DB_PATH"]) +conf_p = Path(os.environ["LIVE_REPOS"]) / "hyperstitionstudio-core" / "deploy" / "logrotate.d" / "hyperstition-sqlite-meta" +summary = get_api_telemetry_summary(tel_db) +assert summary["total_requests"] >= 5, f"Expected >= 5 recorded MCP/REST calls, got {summary['total_requests']}" +lr_out = run_logrotate_directive(conf_path=conf_p, telemetry_db_path=tel_db, max_bytes_override=1024) +assert lr_out["truncated"] is True and lr_out["quiescent"] is True and lr_out["integrity_check"] == "ok", lr_out +assert lr_out["compress_enabled"] is True and lr_out["compressed_archive_bytes"] > 0 and Path(lr_out["compressed_archive_path"]).is_file(), lr_out +print(f" [OK] SQLite metadata DB populated ({summary['total_requests']} calls), compressed to {Path(lr_out['compressed_archive_path']).name} ({lr_out['compressed_archive_bytes']} B) & quiescently truncated via {conf_p.name} (default {lr_out['configured_size_directive']}).") +PY +fi + echo "[OK] All ${PASS_COUNT}/${#ROUTES[@]} static routes + 2 MCP HTTP endpoints + 4 REST API v1 endpoints verified (HTTP 200 OK)." diff --git a/package.json b/package.json index e39d162..3e45903 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@hyperstitionstud.io/workspace-core", - "version": "2.0.18", + "version": "2.0.19", "private": true, "description": "Offline-first ESM & browser global (@hyperstitionstud.io/* / window.HyperstitionStudio.*) packages for Base-36 AQ ciphers and interactive knowledge graph workbenches.", "license": "MIT", diff --git a/packages-js/ciphers-aq/package.json b/packages-js/ciphers-aq/package.json index 71ac3d3..1a309c9 100644 --- a/packages-js/ciphers-aq/package.json +++ b/packages-js/ciphers-aq/package.json @@ -1,6 +1,6 @@ { "name": "@hyperstitionstud.io/ciphers-aq", - "version": "2.0.18", + "version": "2.0.19", "type": "module", "main": "index.js", "license": "MIT" diff --git a/pyproject.toml b/pyproject.toml index 50884e1..6fa52c7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "hyperstitionstudio-core" -version = "2.0.18" +version = "2.0.19" description = "Shared PEP 420 common library (hyperstitionstud_io.*) for Base-36 AQ ciphers, offline CMUdict Ingglish phonetics, ReportLab/Pillow publishing, SHA-256 glossary isolation guards, local-config citation verification, and multi-format knowledge graphs." readme = "README.md" requires-python = ">=3.10" @@ -27,6 +27,7 @@ hs-kg-build = "hyperstitionstud_io.graph.cli:main" hs-quant-harvest = "hyperstitionstud_io.quant.collector:main" hs-mcp = "hyperstitionstud_io.mcp.server:main" hs-sync-version = "hyperstitionstud_io.version_sync:main" +hs-db-build = "hyperstitionstud_io.db.sqlite_meta:main" [tool.setuptools.packages.find] where = ["src"] diff --git a/scripts/build_gallery_manifest.py b/scripts/build_gallery_manifest.py index 78f6234..14e666a 100755 --- a/scripts/build_gallery_manifest.py +++ b/scripts/build_gallery_manifest.py @@ -144,8 +144,8 @@ def main() -> int: }) manifest = { - "version": "2.0.18", - "namespace_urn": "urn:io:hyperstitionstud:gallery:2.0.18", + "version": "2.0.19", + "namespace_urn": "urn:io:hyperstitionstud:gallery:2.0.19", "total_items": len(items), "items": items, } diff --git a/site/gallery/index.html b/site/gallery/index.html index 0bf542e..7acc1e8 100644 --- a/site/gallery/index.html +++ b/site/gallery/index.html @@ -217,7 +217,7 @@
-
hyperstitionstud.io/gallery • Powered by hyperstitionstudio-core (urn:io:hyperstitionstud:gallery:2.0.18)
+
hyperstitionstud.io/gallery • Powered by hyperstitionstudio-core (urn:io:hyperstitionstud:gallery:2.0.19)
Made with ♥ by Lemurs.
diff --git a/site/index.html b/site/index.html index 594007e..68235a7 100644 --- a/site/index.html +++ b/site/index.html @@ -131,7 +131,7 @@

HYPERSTITIONSTUD.IO // TECHNOCAPITAL SINGULARITY ZOOLOGY WORKBENCH

-

100% Offline-Portable & HTTP/3 Edge-Ready Research Corpus • Namespace Authority: urn:io:hyperstitionstud (v2.0.18)

+

100% Offline-Portable & HTTP/3 Edge-Ready Research Corpus • Namespace Authority: urn:io:hyperstitionstud (v2.0.19)

Start Learning Track (/learn/) @@ -254,7 +254,7 @@
-
hyperstitionstud.io • Powered by hyperstitionstudio-core (hyperstitionstud_io.* / urn:io:hyperstitionstud v2.0.18) • Zero External Network Dependencies
+
hyperstitionstud.io • Powered by hyperstitionstudio-core (hyperstitionstud_io.* / urn:io:hyperstitionstud v2.0.19) • Zero External Network Dependencies
Made with ♥ by Lemurs.
diff --git a/site/learn/index.html b/site/learn/index.html index 1ae9882..5be1634 100644 --- a/site/learn/index.html +++ b/site/learn/index.html @@ -413,7 +413,7 @@
-
hyperstitionstud.io/learn • Powered by hyperstitionstudio-core (urn:io:hyperstitionstud:schema:v1:learn-progress-state v2.0.18)
+
hyperstitionstud.io/learn • Powered by hyperstitionstudio-core (urn:io:hyperstitionstud:schema:v1:learn-progress-state v2.0.19)
Made with ♥ by Lemurs.
diff --git a/site/learn/learn-app.js b/site/learn/learn-app.js index 0391e9b..63078e6 100644 --- a/site/learn/learn-app.js +++ b/site/learn/learn-app.js @@ -265,7 +265,7 @@ const base = { $schema: "../schemas/v1/learn-progress-state.schema.json", $id: SCHEMA_ID, - version: "2.0.18", + version: "2.0.19", updated_at_utc: new Date().toISOString(), preferred_register: "fund_manager", completed_modules: [], diff --git a/site/quant/index.html b/site/quant/index.html index 86a7c02..7c05d4c 100644 --- a/site/quant/index.html +++ b/site/quant/index.html @@ -222,7 +222,7 @@

hyperstitionstud.io/quant — Quantitative Finance & Memetic Gematria Time-Series Workbench

-

Strictly Periodic Telemetry (21:30 UTC US Closing Bell × 00:00 UTC News-Wire Dossiers) • Lens-Neutral Schema (urn:io:hyperstitionstud:quant-timeseries:v2.0.18)

+

Strictly Periodic Telemetry (21:30 UTC US Closing Bell × 00:00 UTC News-Wire Dossiers) • Lens-Neutral Schema (urn:io:hyperstitionstud:quant-timeseries:v2.0.19)