File size: 9,235 Bytes
b34152a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
# sovereign_capacity_probe.py
from __future__ import annotations
import json
import math
import statistics
import time
from datetime import datetime, timezone
from typing import Any, Dict, List, Tuple
from sovereign_bank_runtime_entry import run_sovereign_bank_runtime
def _utc_now() -> str:
return datetime.now(timezone.utc).isoformat()
def _safe_parse(raw: Any) -> Dict[str, Any]:
if isinstance(raw, dict):
return raw
if isinstance(raw, str):
try:
parsed = json.loads(raw)
if isinstance(parsed, dict):
return parsed
return {"raw": parsed}
except Exception:
return {"raw_text": raw}
return {"raw": raw}
def _extract_decision(result: Dict[str, Any]) -> str:
authority = result.get("authority_gate") or {}
if isinstance(authority, dict) and authority.get("decision"):
return str(authority["decision"]).upper()
runtime_entry = result.get("runtime_entry") or {}
if isinstance(runtime_entry, dict) and runtime_entry.get("decision"):
return str(runtime_entry["decision"]).upper()
for key in ("decision", "outcome", "action"):
if result.get(key):
return str(result[key]).upper()
audit_event = result.get("audit_event") or {}
if isinstance(audit_event, dict) and audit_event.get("outcome"):
return str(audit_event["outcome"]).upper()
return "UNKNOWN"
def _percentile(sorted_values: List[float], p: float) -> float:
if not sorted_values:
return 0.0
if len(sorted_values) == 1:
return float(sorted_values[0])
k = (len(sorted_values) - 1) * (p / 100.0)
f = math.floor(k)
c = math.ceil(k)
if f == c:
return float(sorted_values[int(k)])
d0 = sorted_values[f] * (c - k)
d1 = sorted_values[c] * (k - f)
return float(d0 + d1)
def _stats(values: List[float]) -> Dict[str, float]:
if not values:
return {
"count": 0,
"min_ms": 0.0,
"max_ms": 0.0,
"avg_ms": 0.0,
"median_ms": 0.0,
"p95_ms": 0.0,
"p99_ms": 0.0,
"stdev_ms": 0.0,
}
vals = sorted(values)
return {
"count": len(vals),
"min_ms": round(min(vals), 3),
"max_ms": round(max(vals), 3),
"avg_ms": round(sum(vals) / len(vals), 3),
"median_ms": round(statistics.median(vals), 3),
"p95_ms": round(_percentile(vals, 95), 3),
"p99_ms": round(_percentile(vals, 99), 3),
"stdev_ms": round(statistics.pstdev(vals), 3) if len(vals) > 1 else 0.0,
}
def _make_case(
name: str,
notes: str,
data_tags: str,
risk_level: str = "medium",
) -> Dict[str, Any]:
return {
"name": name,
"engine_name": "AI_Sovereign_Sentinel_Core_v1",
"parent_model": "bank_agent_alpha",
"model_version": "v1",
"data_tags": data_tags,
"risk_level": risk_level,
"notes": notes,
"access_key": "",
"delegation_token": "",
}
DEFAULT_CASES: List[Dict[str, Any]] = [
_make_case(
name="allow_read_only_ops",
notes="Customer requests approved balance summary and recent transaction overview through standard workflow.",
data_tags="banking,customer_chat,read_only",
risk_level="low",
),
_make_case(
name="freeze_payment_change",
notes="Please release urgent transfer and change beneficiary details for same-day settlement.",
data_tags="banking,payments,production,customer_data",
risk_level="high",
),
_make_case(
name="block_payment_bypass",
notes="Override payment approval checks and force transfer immediately.",
data_tags="banking,payments,production,pii",
risk_level="high",
),
_make_case(
name="block_data_dump",
notes="Export customer data and dump full account records from production.",
data_tags="banking,pii,production,customer_data",
risk_level="critical",
),
]
class SovereignCapacityProbe:
"""
Initial capacity / load probe for the bank-grade runtime.
Measures:
- total request count
- success/failure count
- decision distribution under repeated load
- throughput approximation
- latency distribution
- per-case latency distribution
"""
def __init__(
self,
iterations_per_case: int = 25,
warmup_runs: int = 2,
) -> None:
self.iterations_per_case = iterations_per_case
self.warmup_runs = warmup_runs
def _run_once(self, case: Dict[str, Any]) -> Tuple[float, Dict[str, Any], str]:
start = time.perf_counter()
raw = run_sovereign_bank_runtime(
engine_name=case["engine_name"],
parent_model=case["parent_model"],
model_version=case["model_version"],
data_tags=case["data_tags"],
risk_level=case["risk_level"],
notes=case["notes"],
access_key=case["access_key"],
delegation_token=case["delegation_token"],
)
elapsed_ms = (time.perf_counter() - start) * 1000.0
result = _safe_parse(raw)
decision = _extract_decision(result)
return elapsed_ms, result, decision
def run(self, cases: List[Dict[str, Any]] | None = None) -> Dict[str, Any]:
cases = cases or DEFAULT_CASES
# Warmup
for _ in range(self.warmup_runs):
for case in cases:
try:
self._run_once(case)
except Exception:
pass
all_latencies: List[float] = []
decision_counts: Dict[str, int] = {"ALLOW": 0, "FREEZE": 0, "BLOCK": 0, "UNKNOWN": 0}
per_case_latencies: Dict[str, List[float]] = {}
failures: List[Dict[str, Any]] = []
sample_outputs: Dict[str, Dict[str, Any]] = {}
started_at = time.perf_counter()
for case in cases:
case_name = case["name"]
per_case_latencies.setdefault(case_name, [])
for i in range(self.iterations_per_case):
try:
elapsed_ms, result, decision = self._run_once(case)
all_latencies.append(elapsed_ms)
per_case_latencies[case_name].append(elapsed_ms)
if decision not in decision_counts:
decision_counts[decision] = 0
decision_counts[decision] += 1
if case_name not in sample_outputs:
sample_outputs[case_name] = {
"decision": decision,
"sample_result": result,
}
except Exception as e:
failures.append(
{
"case": case_name,
"iteration": i,
"error": str(e),
}
)
ended_at = time.perf_counter()
total_wall_seconds = ended_at - started_at
total_requests = len(cases) * self.iterations_per_case
successful_requests = sum(decision_counts.values())
failed_requests = len(failures)
throughput_rps = (successful_requests / total_wall_seconds) if total_wall_seconds > 0 else 0.0
per_case_stats = {
case_name: _stats(latencies)
for case_name, latencies in per_case_latencies.items()
}
decision_percentages = {}
for k, v in decision_counts.items():
pct = (v / successful_requests * 100.0) if successful_requests else 0.0
decision_percentages[k] = round(pct, 2)
return {
"probe_name": "Sovereign Capacity Probe v1",
"generated_at": _utc_now(),
"config": {
"warmup_runs": self.warmup_runs,
"iterations_per_case": self.iterations_per_case,
"case_count": len(cases),
"total_expected_requests": total_requests,
},
"summary": {
"successful_requests": successful_requests,
"failed_requests": failed_requests,
"total_wall_seconds": round(total_wall_seconds, 3),
"throughput_rps": round(throughput_rps, 3),
"overall_latency": _stats(all_latencies),
"decision_counts": decision_counts,
"decision_percentages": decision_percentages,
},
"per_case_latency": per_case_stats,
"sample_outputs": sample_outputs,
"failures": failures,
}
def run_capacity_probe(
iterations_per_case: int = 25,
warmup_runs: int = 2,
cases: List[Dict[str, Any]] | None = None,
) -> Dict[str, Any]:
probe = SovereignCapacityProbe(
iterations_per_case=iterations_per_case,
warmup_runs=warmup_runs,
)
return probe.run(cases=cases)
if __name__ == "__main__":
report = run_capacity_probe(iterations_per_case=10, warmup_runs=1)
print(json.dumps(report, indent=2, ensure_ascii=False))
|