rezabarkhordary commited on
Commit
e80d32f
·
verified ·
1 Parent(s): d4bdc9d

Create sovereign_runtime_chain_integration.py

Browse files
sovereign_runtime_chain_integration.py ADDED
@@ -0,0 +1,128 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # sovereign_runtime_chain_integration.py
2
+ from __future__ import annotations
3
+
4
+ import json
5
+ from typing import Any, Dict, Optional
6
+
7
+ import app as sovereign_app
8
+
9
+ from sovereign_chain_runtime_bridge import BRIDGE
10
+
11
+
12
+ def _norm_result(raw: Any) -> Dict[str, Any]:
13
+ """
14
+ Normalize runtime output into a dict.
15
+ app.run_sentinel / integration layers may return either:
16
+ - dict
17
+ - JSON string
18
+ - other serializable object
19
+ """
20
+ if isinstance(raw, dict):
21
+ return raw
22
+
23
+ if isinstance(raw, str):
24
+ try:
25
+ parsed = json.loads(raw)
26
+ if isinstance(parsed, dict):
27
+ return parsed
28
+ return {"raw_result": parsed}
29
+ except Exception:
30
+ return {"raw_result_text": raw}
31
+
32
+ return {"raw_result": raw}
33
+
34
+
35
+ def _extract_primary_decision(result: Dict[str, Any]) -> str:
36
+ """
37
+ Conservative extraction of the effective decision.
38
+ """
39
+ # authority gate first
40
+ authority_gate = result.get("authority_gate") or {}
41
+ if isinstance(authority_gate, dict):
42
+ decision = authority_gate.get("decision")
43
+ if decision:
44
+ return str(decision).upper()
45
+
46
+ # top-level direct decision fields
47
+ for key in ("decision", "outcome", "action"):
48
+ if result.get(key):
49
+ return str(result.get(key)).upper()
50
+
51
+ # fallback from audit event outcome
52
+ audit_event = result.get("audit_event") or {}
53
+ if isinstance(audit_event, dict) and audit_event.get("outcome"):
54
+ return str(audit_event.get("outcome")).upper()
55
+
56
+ return "UNKNOWN"
57
+
58
+
59
+ def _extract_freeze_case(result: Dict[str, Any]) -> Optional[Dict[str, Any]]:
60
+ """
61
+ If earlier stages already produced a freeze_case / freeze object,
62
+ pass it into the chain layer.
63
+ """
64
+ for key in ("freeze_case", "freeze", "review_case"):
65
+ value = result.get(key)
66
+ if isinstance(value, dict) and value:
67
+ return value
68
+ return None
69
+
70
+
71
+ def run_sentinel_with_runtime_chain(
72
+ engine_name: str,
73
+ parent_model: str,
74
+ model_version: str,
75
+ data_tags: str,
76
+ risk_level: str,
77
+ notes: str,
78
+ access_key: str,
79
+ delegation_token: str = "",
80
+ ) -> str:
81
+ """
82
+ Bank-grade wrapper around sovereign_app.run_sentinel.
83
+
84
+ It:
85
+ 1) runs the real Sovereign flow
86
+ 2) normalizes result
87
+ 3) attaches chained audit artifact
88
+ 4) returns JSON string for UI/API compatibility
89
+ """
90
+ raw = sovereign_app.run_sentinel(
91
+ engine_name=engine_name,
92
+ parent_model=parent_model,
93
+ model_version=model_version,
94
+ data_tags=data_tags,
95
+ risk_level=risk_level,
96
+ notes=notes,
97
+ access_key=access_key,
98
+ delegation_token=delegation_token,
99
+ )
100
+
101
+ result = _norm_result(raw)
102
+ freeze_case = _extract_freeze_case(result)
103
+ primary_decision = _extract_primary_decision(result)
104
+
105
+ chained = BRIDGE.attach_to_runtime_result(
106
+ result=result,
107
+ event_type="bank_runtime_decision",
108
+ engine_name=engine_name,
109
+ parent_model=parent_model,
110
+ model_version=model_version,
111
+ data_tags=data_tags,
112
+ risk_level=risk_level,
113
+ notes=notes,
114
+ policy_bundle={
115
+ "domain": "banking",
116
+ "runtime_decision": primary_decision,
117
+ "freeze_present": bool(freeze_case),
118
+ },
119
+ freeze_case=freeze_case,
120
+ extra={
121
+ "integration_layer": "sovereign_runtime_chain_integration",
122
+ "bank_grade": True,
123
+ },
124
+ )
125
+
126
+ return json.dumps(chained, indent=2, ensure_ascii=False)
127
+
128
+