Add Vault adapter smoke tests (7 assertions: bootstrap, migration idempotency, insert, dedup, fetch, hit/miss, gate rejection)
Browse files- smoke_test_vault.py +177 -0
smoke_test_vault.py
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Smoke test for vault_adapter.
|
| 3 |
+
|
| 4 |
+
Coverage:
|
| 5 |
+
1. Open Vault on a tempdir, schema_migrations bootstrap works.
|
| 6 |
+
2. Apply migration 003 — idempotent (re-apply is a no-op).
|
| 7 |
+
3. Insert KV profile rows, count matches, gate audit log populated.
|
| 8 |
+
4. Re-insert the same rows — duplicates silently dropped (idempotent).
|
| 9 |
+
5. Fetch by invalidation key returns the inserted rows.
|
| 10 |
+
6. has_kv_profile() works on hit and miss.
|
| 11 |
+
7. PermissionGate rejection actually blocks writes.
|
| 12 |
+
"""
|
| 13 |
+
|
| 14 |
+
import sys
|
| 15 |
+
import tempfile
|
| 16 |
+
from pathlib import Path
|
| 17 |
+
|
| 18 |
+
sys.path.insert(0, str(Path(__file__).resolve().parent))
|
| 19 |
+
|
| 20 |
+
import vault_adapter as va
|
| 21 |
+
import kv_profiler as kvp
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def hr(title: str) -> None:
|
| 25 |
+
print(f"\n{'=' * 6} {title} {'=' * 6}")
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
# Build a minimal ProfileRow-shaped object for testing without needing a real
|
| 29 |
+
# model. We use kvp.ProfileRow directly since it's the canonical shape.
|
| 30 |
+
def make_row(layer_idx: int, k: int, v: int, quantizer: str, drift: float,
|
| 31 |
+
model_hash: str = "testhash") -> kvp.ProfileRow:
|
| 32 |
+
return kvp.ProfileRow(
|
| 33 |
+
model_hash=model_hash,
|
| 34 |
+
calibration_hash="calhash0",
|
| 35 |
+
pipeline_version="1.0.0",
|
| 36 |
+
layer_idx=layer_idx,
|
| 37 |
+
k_bits=k,
|
| 38 |
+
v_bits=v,
|
| 39 |
+
quantizer=quantizer,
|
| 40 |
+
drift_attn_output=drift,
|
| 41 |
+
drift_metric="mse_normalised",
|
| 42 |
+
bytes_per_kv_token=128.0,
|
| 43 |
+
max_seq_len_observed=1024,
|
| 44 |
+
num_kv_heads=8,
|
| 45 |
+
head_dim=128,
|
| 46 |
+
profiled_at="2026-05-18T12:00:00+00:00",
|
| 47 |
+
profiled_by_agent_id="smoke-test",
|
| 48 |
+
profiled_by_agent_tier=1,
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
tmp = Path(tempfile.mkdtemp(prefix="vault_smoke_"))
|
| 53 |
+
db_path = tmp / "vault.db"
|
| 54 |
+
migration_path = Path(__file__).resolve().parent / "vault_migration_003_kv_sensitivity_profile.sql"
|
| 55 |
+
print(f" tmpdir: {tmp}")
|
| 56 |
+
print(f" migration: {migration_path}")
|
| 57 |
+
assert migration_path.exists(), f"migration file not found at {migration_path}"
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
# ===========================================================================
|
| 61 |
+
# 1. Open Vault — schema_migrations bootstrap
|
| 62 |
+
# ===========================================================================
|
| 63 |
+
hr("1. open Vault on tempdir, schema_migrations bootstraps")
|
| 64 |
+
vault = va.VaultAdapter(db_path)
|
| 65 |
+
cur = vault.conn.execute(
|
| 66 |
+
"SELECT name FROM sqlite_master WHERE type='table' AND name='schema_migrations'"
|
| 67 |
+
)
|
| 68 |
+
assert cur.fetchone() is not None, "schema_migrations not created"
|
| 69 |
+
print(f" schema_migrations exists ✓")
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
# ===========================================================================
|
| 73 |
+
# 2. Apply migration 003 — idempotent
|
| 74 |
+
# ===========================================================================
|
| 75 |
+
hr("2. apply migration 003 (idempotent)")
|
| 76 |
+
applied = vault.apply_migration(migration_path)
|
| 77 |
+
print(f" first apply: {'YES' if applied else 'NO'}")
|
| 78 |
+
assert applied, "first apply should have run"
|
| 79 |
+
|
| 80 |
+
applied2 = vault.apply_migration(migration_path)
|
| 81 |
+
print(f" second apply: {'YES' if applied2 else 'NO'}")
|
| 82 |
+
assert not applied2, "second apply should have been skipped"
|
| 83 |
+
|
| 84 |
+
print(f" applied migrations: {vault.applied_migrations()}")
|
| 85 |
+
assert "003" in vault.applied_migrations()
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
# ===========================================================================
|
| 89 |
+
# 3. Insert KV profile rows
|
| 90 |
+
# ===========================================================================
|
| 91 |
+
hr("3. insert KV profile rows")
|
| 92 |
+
rows = [
|
| 93 |
+
make_row(layer_idx=i, k=4, v=4, quantizer="hqq_g64", drift=0.01 + i * 0.001)
|
| 94 |
+
for i in range(5)
|
| 95 |
+
] + [
|
| 96 |
+
make_row(layer_idx=i, k=8, v=8, quantizer="hqq_g64", drift=0.001 + i * 0.0001)
|
| 97 |
+
for i in range(5)
|
| 98 |
+
]
|
| 99 |
+
print(f" inserting {len(rows)} rows")
|
| 100 |
+
result = vault.insert_kv_profile_rows(rows)
|
| 101 |
+
print(f" requested={result.requested} inserted={result.inserted} table={result.table}")
|
| 102 |
+
assert result.requested == 10
|
| 103 |
+
assert result.inserted == 10
|
| 104 |
+
assert len(vault.gate.audit_log) >= 1, "permission gate audit log not populated"
|
| 105 |
+
print(f" gate audit log: {vault.gate.audit_log}")
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
# ===========================================================================
|
| 109 |
+
# 4. Re-insert same rows — idempotent dedup
|
| 110 |
+
# ===========================================================================
|
| 111 |
+
hr("4. re-insert same rows (idempotent)")
|
| 112 |
+
result2 = vault.insert_kv_profile_rows(rows)
|
| 113 |
+
print(f" requested={result2.requested} inserted={result2.inserted}")
|
| 114 |
+
assert result2.requested == 10
|
| 115 |
+
assert result2.inserted == 0, "duplicate insert should have inserted 0 new rows"
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
# ===========================================================================
|
| 119 |
+
# 5. Fetch by invalidation key
|
| 120 |
+
# ===========================================================================
|
| 121 |
+
hr("5. fetch_kv_profile_rows")
|
| 122 |
+
fetched = vault.fetch_kv_profile_rows("testhash", "calhash0", "1.0.0")
|
| 123 |
+
print(f" fetched {len(fetched)} rows")
|
| 124 |
+
assert len(fetched) == 10
|
| 125 |
+
assert all("drift_attn_output" in r for r in fetched)
|
| 126 |
+
assert fetched[0]["model_hash"] == "testhash"
|
| 127 |
+
|
| 128 |
+
# Wrong invalidation key returns empty
|
| 129 |
+
missing = vault.fetch_kv_profile_rows("testhash", "calhash0", "9.9.9")
|
| 130 |
+
print(f" wrong pipeline_version: {len(missing)} rows")
|
| 131 |
+
assert len(missing) == 0
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
# ===========================================================================
|
| 135 |
+
# 6. has_kv_profile
|
| 136 |
+
# ===========================================================================
|
| 137 |
+
hr("6. has_kv_profile hit/miss")
|
| 138 |
+
assert vault.has_kv_profile("testhash", "calhash0", "1.0.0") is True
|
| 139 |
+
print(f" hit: True ✓")
|
| 140 |
+
assert vault.has_kv_profile("doesnotexist", "calhash0", "1.0.0") is False
|
| 141 |
+
print(f" miss: False ✓")
|
| 142 |
+
|
| 143 |
+
|
| 144 |
+
# ===========================================================================
|
| 145 |
+
# 7. PermissionGate rejection
|
| 146 |
+
# ===========================================================================
|
| 147 |
+
hr("7. PermissionGate rejection actually blocks writes")
|
| 148 |
+
|
| 149 |
+
|
| 150 |
+
class BlockingGate(va.PermissionGate):
|
| 151 |
+
def check_write(self, table: str, agent_id: str, agent_tier: int) -> None:
|
| 152 |
+
if agent_tier < 5:
|
| 153 |
+
raise va.PermissionDenied(
|
| 154 |
+
f"agent {agent_id} tier {agent_tier} below min tier 5 for {table}"
|
| 155 |
+
)
|
| 156 |
+
super().check_write(table, agent_id, agent_tier)
|
| 157 |
+
|
| 158 |
+
|
| 159 |
+
vault2 = va.VaultAdapter(db_path, permission_gate=BlockingGate())
|
| 160 |
+
new_row = make_row(layer_idx=99, k=2, v=2, quantizer="hqq_g64", drift=0.5,
|
| 161 |
+
model_hash="willnotinsert")
|
| 162 |
+
try:
|
| 163 |
+
vault2.insert_kv_profile_rows([new_row])
|
| 164 |
+
print(f" FAIL: should have raised PermissionDenied")
|
| 165 |
+
sys.exit(1)
|
| 166 |
+
except va.PermissionDenied as e:
|
| 167 |
+
print(f" caught PermissionDenied: {e} ✓")
|
| 168 |
+
|
| 169 |
+
# Verify nothing actually got in
|
| 170 |
+
missing = vault2.fetch_kv_profile_rows("willnotinsert", "calhash0", "1.0.0")
|
| 171 |
+
assert len(missing) == 0
|
| 172 |
+
print(f" row not in DB after rejection: ✓")
|
| 173 |
+
vault2.close()
|
| 174 |
+
|
| 175 |
+
|
| 176 |
+
vault.close()
|
| 177 |
+
print("\nAll assertions passed.")
|