mxguru1 commited on
Commit
d156365
Β·
verified Β·
1 Parent(s): b413d99

Update vault smoke tests: T2 default for inserts, T1 denied test, audit log severity verification

Browse files
Files changed (1) hide show
  1. smoke_test_vault.py +98 -72
smoke_test_vault.py CHANGED
@@ -1,14 +1,16 @@
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
@@ -19,33 +21,31 @@ 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
 
@@ -65,112 +65,138 @@ 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()
 
1
  """
2
+ Smoke test for vault_adapter + permission_gate integration.
3
 
4
  Coverage:
5
+ 1. Open Vault on a tempdir; schema_migrations bootstraps.
6
  2. Apply migration 003 β€” idempotent (re-apply is a no-op).
7
+ 3. Migration requires VAULT_SCHEMA: T2 agent denied, T6 succeeds.
8
+ 4. Insert KV profile rows as a T2 agent (default I/O worker tier).
9
+ 5. Re-insert the same rows β€” duplicates silently dropped (idempotent).
10
+ 6. Fetch by invalidation key returns the inserted rows.
11
+ 7. has_kv_profile() works on hit and miss.
12
+ 8. T1 (read-only) agent denied at insert with capability-correct error.
13
+ 9. Audit log records every gate check with full severity metadata.
14
  """
15
 
16
  import sys
 
21
 
22
  import vault_adapter as va
23
  import kv_profiler as kvp
24
+ import permission_gate as pg
25
 
26
 
27
  def hr(title: str) -> None:
28
  print(f"\n{'=' * 6} {title} {'=' * 6}")
29
 
30
 
31
+ def make_row(layer_idx, k, v, quantizer, drift,
32
+ model_hash="testhash",
33
+ agent_id="smoke-test",
34
+ agent_tier=2):
35
  return kvp.ProfileRow(
36
  model_hash=model_hash,
37
  calibration_hash="calhash0",
38
  pipeline_version="1.0.0",
39
  layer_idx=layer_idx,
40
+ k_bits=k, v_bits=v, quantizer=quantizer,
 
 
41
  drift_attn_output=drift,
42
  drift_metric="mse_normalised",
43
  bytes_per_kv_token=128.0,
44
  max_seq_len_observed=1024,
45
+ num_kv_heads=8, head_dim=128,
 
46
  profiled_at="2026-05-18T12:00:00+00:00",
47
+ profiled_by_agent_id=agent_id,
48
+ profiled_by_agent_tier=agent_tier,
49
  )
50
 
51
 
 
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
69
  print(f" schema_migrations exists βœ“")
70
 
71
 
72
  # ===========================================================================
73
+ # 2. Apply migration 003 β€” idempotent (default agent_tier=6)
74
  # ===========================================================================
75
+ hr("2. apply migration 003 (idempotent) as T6")
76
  applied = vault.apply_migration(migration_path)
 
77
  assert applied, "first apply should have run"
78
+ print(f" first apply: YES βœ“")
79
 
80
  applied2 = vault.apply_migration(migration_path)
 
81
  assert not applied2, "second apply should have been skipped"
82
+ print(f" second apply: NO (skipped) βœ“")
 
83
  assert "003" in vault.applied_migrations()
84
 
85
 
86
  # ===========================================================================
87
+ # 3. Migration requires VAULT_SCHEMA: T2 denied, T6 succeeds
88
+ # ===========================================================================
89
+ hr("3. apply_migration as T2 must be denied")
90
+ # Reset for a fresh migration attempt (drop the version row)
91
+ vault2_path = tmp / "vault2.db"
92
+ vault2 = va.VaultAdapter(vault2_path)
93
+ try:
94
+ vault2.apply_migration(migration_path, agent_id="bad-agent", agent_tier=2)
95
+ print(f" FAIL β€” T2 should not have been able to apply migration")
96
+ sys.exit(1)
97
+ except pg.PermissionDenied as e:
98
+ print(f" caught PermissionDenied: {e} βœ“")
99
+
100
+ # Now apply as T6 β€” works
101
+ applied_t6 = vault2.apply_migration(
102
+ migration_path, agent_id="operator", agent_tier=6
103
+ )
104
+ assert applied_t6
105
+ print(f" T6 apply succeeded βœ“")
106
+ vault2.close()
107
+
108
+
109
+ # ===========================================================================
110
+ # 4. Insert KV profile rows as T2 (default working tier)
111
  # ===========================================================================
112
+ hr("4. insert KV profile rows as T2")
113
  rows = [
114
+ make_row(i, 4, 4, "hqq_g64", 0.01 + i * 0.001) for i in range(5)
 
115
  ] + [
116
+ make_row(i, 8, 8, "hqq_g64", 0.001 + i * 0.0001) for i in range(5)
 
117
  ]
 
118
  result = vault.insert_kv_profile_rows(rows)
119
+ print(f" requested={result.requested} inserted={result.inserted}")
120
  assert result.requested == 10
121
  assert result.inserted == 10
 
 
122
 
123
 
124
  # ===========================================================================
125
+ # 5. Re-insert same rows β€” idempotent dedup
126
  # ===========================================================================
127
+ hr("5. re-insert same rows (idempotent)")
128
  result2 = vault.insert_kv_profile_rows(rows)
129
  print(f" requested={result2.requested} inserted={result2.inserted}")
130
  assert result2.requested == 10
131
+ assert result2.inserted == 0
132
 
133
 
134
  # ===========================================================================
135
+ # 6. Fetch by invalidation key
136
  # ===========================================================================
137
+ hr("6. fetch_kv_profile_rows")
138
  fetched = vault.fetch_kv_profile_rows("testhash", "calhash0", "1.0.0")
139
  print(f" fetched {len(fetched)} rows")
140
  assert len(fetched) == 10
 
 
 
 
141
  missing = vault.fetch_kv_profile_rows("testhash", "calhash0", "9.9.9")
 
142
  assert len(missing) == 0
143
 
144
 
145
  # ===========================================================================
146
+ # 7. has_kv_profile
147
  # ===========================================================================
148
+ hr("7. has_kv_profile hit/miss")
149
  assert vault.has_kv_profile("testhash", "calhash0", "1.0.0") is True
 
150
  assert vault.has_kv_profile("doesnotexist", "calhash0", "1.0.0") is False
151
+ print(f" hit/miss correctly distinguished βœ“")
152
 
153
 
154
  # ===========================================================================
155
+ # 8. T1 (read-only) agent denied insert with correct capability error
156
  # ===========================================================================
157
+ hr("8. T1 agent denied insert (vault.append requires T2+)")
158
+ t1_row = make_row(
159
+ 99, 2, 2, "hqq_g64", 0.5,
160
+ model_hash="willnotinsert",
161
+ agent_id="readonly-agent",
162
+ agent_tier=1,
163
+ )
 
 
 
 
 
 
 
 
164
  try:
165
+ vault.insert_kv_profile_rows([t1_row])
166
+ print(f" FAIL β€” T1 should not be able to insert")
167
  sys.exit(1)
168
+ except pg.PermissionDenied as e:
169
  print(f" caught PermissionDenied: {e} βœ“")
170
+ assert "vault.append" in str(e)
171
+ print(f" capability cited correctly βœ“")
172
 
173
+ # Confirm nothing got in
174
+ missing = vault.fetch_kv_profile_rows("willnotinsert", "calhash0", "1.0.0")
175
  assert len(missing) == 0
176
+
177
+
178
+ # ===========================================================================
179
+ # 9. Audit log: every gate check recorded with severity metadata
180
+ # ===========================================================================
181
+ hr("9. audit log captures every check + severity")
182
+ log = vault.gate.audit_log
183
+ print(f" total audit entries: {len(log)}")
184
+ # The migration apply checked VAULT_SCHEMA at HIGH severity
185
+ schema_checks = [r for r in log if r.capability == "vault.schema"]
186
+ assert len(schema_checks) > 0, "VAULT_SCHEMA never logged"
187
+ assert all(r.severity == "HIGH" for r in schema_checks)
188
+ print(f" VAULT_SCHEMA entries: {len(schema_checks)} (all severity=HIGH) βœ“")
189
+
190
+ # The inserts checked VAULT_APPEND at NORMAL severity
191
+ append_checks = [r for r in log if r.capability == "vault.append"]
192
+ assert len(append_checks) > 0
193
+ assert all(r.severity == "NORMAL" for r in append_checks)
194
+ print(f" VAULT_APPEND entries: {len(append_checks)} (all severity=NORMAL) βœ“")
195
+
196
+ # The T1 attempt was a denied VAULT_APPEND
197
+ denials = [r for r in log if not r.granted]
198
+ assert any(r.agent_id == "readonly-agent" for r in denials)
199
+ print(f" denial entries: {len(denials)} (incl. T1 agent) βœ“")
200
 
201
 
202
  vault.close()