feat(db,mcp,deploy): implement SQLite + FTS5 metadata/telemetry DB, automatic MCP/REST population, and quiescent logrotate.d compression (v2.0.19)
This commit is contained in:
1 parent
ee5f458c76
commit
1182ba0cf2
25 files changed
+1562
-37
No files matched your search
@@ -275,6 +275,135 @@ def test_ipld_quant_and_mcp_server() -> None:
|
||||
err_code, _, err_body = dispatch_rest_v1("GET", "/api/v1/ciphers/evaluate")
|
||||
assert err_code == 422 and err_body["ok"] is False
|
||||
|
||||
# 5. SQLite + FTS5 Catalog, Automatic REST/MCP Telemetry Population, Inactive Plan Isolation & Quiescent Logrotate Truncation
|
||||
import os
|
||||
import sqlite3
|
||||
import tempfile
|
||||
|
||||
from hyperstitionstud_io.db.sqlite_meta import (
|
||||
DEFAULT_MAX_TELEMETRY_BYTES,
|
||||
build_catalog_sqlite_db,
|
||||
get_api_telemetry_summary,
|
||||
parse_size_spec_bytes,
|
||||
query_coordinate_join,
|
||||
query_fts_catalog,
|
||||
query_registry_manifests,
|
||||
query_timeseries_bars,
|
||||
run_logrotate_directive,
|
||||
truncate_telemetry_db_quiescent,
|
||||
)
|
||||
|
||||
assert DEFAULT_MAX_TELEMETRY_BYTES == 100 * 1024 * 1024
|
||||
assert parse_size_spec_bytes("100M") == 100 * 1024 * 1024
|
||||
assert parse_size_spec_bytes("100MB") == 100 * 1024 * 1024
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmp_dir:
|
||||
tmp_path = Path(tmp_dir)
|
||||
cat_db = tmp_path / "test-catalog.db"
|
||||
tel_db = tmp_path / "test-telemetry.db"
|
||||
|
||||
cat_res = build_catalog_sqlite_db(cat_db)
|
||||
assert cat_res["isolation_receipt"]["all_unchanged"] is True
|
||||
assert cat_res["counts"]["registry_manifests"] >= 7
|
||||
assert cat_res["counts"]["glossary_entries"] >= 282
|
||||
assert cat_res["counts"]["kg_nodes"] == 388
|
||||
assert cat_res["counts"]["kg_edges"] == 1282
|
||||
assert cat_res["counts"]["dossiers"] >= 71
|
||||
assert cat_res["counts"]["quant_timeseries_bars"] == 720
|
||||
|
||||
manifests = query_registry_manifests(cat_db)
|
||||
assert any(m["registry_slug"] == "encyclopedia-graph" and m["dag_json_cidv1"].startswith("baguqeera") for m in manifests)
|
||||
|
||||
fts_res = query_fts_catalog("envideea", db_path=cat_db)
|
||||
assert fts_res["match_count"] >= 1
|
||||
|
||||
coord_137 = query_coordinate_join(137, db_path=cat_db)
|
||||
assert coord_137["coordinate_aq"] == 137
|
||||
assert len(coord_137["kg_nodes"]) >= 1
|
||||
assert len(coord_137["quant_timeseries_bars"]) >= 1
|
||||
|
||||
nvda_bars = query_timeseries_bars(symbol="NVDA", min_points=1.0, db_path=cat_db)
|
||||
assert len(nvda_bars) >= 1 and all(b["symbol"] == "NVDA" for b in nvda_bars)
|
||||
|
||||
# Verify MCP and REST API calls automatically populate the SQLite REST API metadata database
|
||||
prev_tel_env = os.environ.get("HYPERSTITION_TELEMETRY_DB_PATH")
|
||||
os.environ["HYPERSTITION_TELEMETRY_DB_PATH"] = str(tel_db)
|
||||
try:
|
||||
s1, _, _ = dispatch_rest_v1("GET", "/api/v1/status")
|
||||
s2, _, _ = dispatch_rest_v1("GET", "/api/v1/quant/instruments/NVDA")
|
||||
m1 = dispatch_jsonrpc({"jsonrpc": "2.0", "id": 99, "method": "tools/list"})
|
||||
assert s1 == 200 and s2 == 200 and m1 is not None
|
||||
|
||||
# Verify inactive plan routes (/api/v1/meta/*, hs_query_sqlite_catalog) remain unexposed (404)
|
||||
inact_meta_code, _, _ = dispatch_rest_v1("GET", "/api/v1/meta/manifests")
|
||||
inact_tool_code, _, _ = dispatch_rest_v1("GET", "/api/v1/tools/hs_query_sqlite_catalog")
|
||||
assert inact_meta_code == 404
|
||||
assert inact_tool_code == 404
|
||||
|
||||
summary_before = get_api_telemetry_summary(tel_db)
|
||||
assert summary_before["total_requests"] == 5
|
||||
assert summary_before["ringbuffer_count"] == 5
|
||||
protocols_seen = {ep["protocol"] for ep in summary_before["endpoints"]}
|
||||
assert protocols_seen == {"rest_v1", "mcp_jsonrpc"}
|
||||
patterns_seen = {ep["endpoint_pattern"] for ep in summary_before["endpoints"]}
|
||||
assert "/api/v1/status" in patterns_seen
|
||||
assert "/api/v1/quant/instruments/{symbol}" in patterns_seen
|
||||
assert "tools/list" in patterns_seen
|
||||
assert all(ev["isolation_ok"] == 1 for ev in summary_before["recent_audit_events"])
|
||||
|
||||
# Verify logrotate.d file configuration (default 100M = 100MB, compress enabled, rotate >= 12) & no-op when below limit
|
||||
import gzip
|
||||
|
||||
lr_noop = run_logrotate_directive(telemetry_db_path=tel_db)
|
||||
assert lr_noop["configured_size_directive"].upper() == "100M"
|
||||
assert lr_noop["configured_default_bytes"] == 100 * 1024 * 1024
|
||||
assert lr_noop["compress_enabled"] is True
|
||||
assert lr_noop["rotate_count"] >= 12
|
||||
assert lr_noop["truncated"] is False
|
||||
assert get_api_telemetry_summary(tel_db)["total_requests"] == 5
|
||||
|
||||
# Verify logrotate.d quiescent compression + truncation when DB size >= configured limit
|
||||
lr_trunc = run_logrotate_directive(telemetry_db_path=tel_db, max_bytes_override=1024)
|
||||
assert lr_trunc["truncated"] is True
|
||||
assert lr_trunc["quiescent"] is True
|
||||
assert lr_trunc["integrity_check"] == "ok"
|
||||
assert lr_trunc["compressed_archive_path"] == f"{tel_db}.1.gz"
|
||||
assert lr_trunc["compressed_archive_bytes"] > 0
|
||||
assert Path(lr_trunc["compressed_archive_path"]).is_file()
|
||||
|
||||
# Verify the compressed .1.gz archive preserved all 5 pre-truncation requests & passes SQLite integrity_check
|
||||
restored_db = tmp_path / "restored-from-gz.db"
|
||||
with gzip.open(lr_trunc["compressed_archive_path"], "rb") as f_gz:
|
||||
restored_db.write_bytes(f_gz.read())
|
||||
with sqlite3.connect(str(restored_db)) as r_conn:
|
||||
assert r_conn.execute("PRAGMA integrity_check;").fetchone()[0] == "ok"
|
||||
assert get_api_telemetry_summary(restored_db)["total_requests"] == 5
|
||||
|
||||
summary_after = get_api_telemetry_summary(tel_db)
|
||||
assert summary_after["total_requests"] == 0
|
||||
assert summary_after["ringbuffer_count"] == 0
|
||||
|
||||
# Verify SQLite integrity_check == 'ok' and continued automatic population after quiescent truncation
|
||||
with sqlite3.connect(str(tel_db)) as check_conn:
|
||||
assert check_conn.execute("PRAGMA integrity_check;").fetchone()[0] == "ok"
|
||||
|
||||
s_post, _, _ = dispatch_rest_v1("GET", "/api/v1/status")
|
||||
assert s_post == 200
|
||||
assert get_api_telemetry_summary(tel_db)["total_requests"] == 1
|
||||
|
||||
# Also verify second rotation shifts .1.gz -> .2.gz and writes new .1.gz
|
||||
direct_trunc = truncate_telemetry_db_quiescent(tel_db, max_bytes=512, force=False)
|
||||
assert direct_trunc["truncated"] is True
|
||||
assert direct_trunc["quiescent"] is True
|
||||
assert direct_trunc["integrity_check"] == "ok"
|
||||
assert Path(f"{tel_db}.1.gz").is_file()
|
||||
assert Path(f"{tel_db}.2.gz").is_file()
|
||||
finally:
|
||||
if prev_tel_env is None:
|
||||
os.environ.pop("HYPERSTITION_TELEMETRY_DB_PATH", None)
|
||||
else:
|
||||
os.environ["HYPERSTITION_TELEMETRY_DB_PATH"] = prev_tel_env
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_offline_urn_namespaces_and_no_external_https_fonts()
|
||||
|
||||
Reference in new issue
Block a user