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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
# sovereign_chain_runtime_bridge.py
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
from typing import Any, Dict, Optional
|
| 6 |
+
|
| 7 |
+
from sovereign_audit_chain_integration import (
|
| 8 |
+
attach_chain_to_result,
|
| 9 |
+
chain_event_from_freeze_case,
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
try:
|
| 13 |
+
from sovereign_freeze_review import SovereignFreezeReview
|
| 14 |
+
except Exception:
|
| 15 |
+
SovereignFreezeReview = None # type: ignore
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
class SovereignChainRuntimeBridge:
|
| 19 |
+
"""
|
| 20 |
+
Bridges the audit-chain layer into:
|
| 21 |
+
1) runtime decision results
|
| 22 |
+
2) freeze lifecycle events
|
| 23 |
+
|
| 24 |
+
Goal:
|
| 25 |
+
Every bank-grade decision path gets a chained audit artifact.
|
| 26 |
+
"""
|
| 27 |
+
|
| 28 |
+
def __init__(self) -> None:
|
| 29 |
+
self.freeze_review = SovereignFreezeReview() if SovereignFreezeReview else None
|
| 30 |
+
|
| 31 |
+
# ------------------------------------------------------------------
|
| 32 |
+
# Runtime result chaining
|
| 33 |
+
# ------------------------------------------------------------------
|
| 34 |
+
def attach_to_runtime_result(
|
| 35 |
+
self,
|
| 36 |
+
result: Dict[str, Any],
|
| 37 |
+
*,
|
| 38 |
+
event_type: str = "sentinel_runtime_decision",
|
| 39 |
+
engine_name: Optional[str] = None,
|
| 40 |
+
parent_model: str = "",
|
| 41 |
+
model_version: str = "",
|
| 42 |
+
data_tags: Any = None,
|
| 43 |
+
risk_level: str = "",
|
| 44 |
+
notes: str = "",
|
| 45 |
+
policy_bundle: Optional[Dict[str, Any]] = None,
|
| 46 |
+
freeze_case: Optional[Dict[str, Any]] = None,
|
| 47 |
+
extra: Optional[Dict[str, Any]] = None,
|
| 48 |
+
) -> Dict[str, Any]:
|
| 49 |
+
return attach_chain_to_result(
|
| 50 |
+
result=result,
|
| 51 |
+
event_type=event_type,
|
| 52 |
+
engine_name=engine_name,
|
| 53 |
+
parent_model=parent_model,
|
| 54 |
+
model_version=model_version,
|
| 55 |
+
data_tags=data_tags,
|
| 56 |
+
risk_level=risk_level,
|
| 57 |
+
notes=notes,
|
| 58 |
+
policy_bundle=policy_bundle,
|
| 59 |
+
freeze_case=freeze_case,
|
| 60 |
+
extra=extra,
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
# ------------------------------------------------------------------
|
| 64 |
+
# Freeze event chaining helpers
|
| 65 |
+
# ------------------------------------------------------------------
|
| 66 |
+
def chain_freeze_created(
|
| 67 |
+
self,
|
| 68 |
+
freeze_case: Dict[str, Any],
|
| 69 |
+
*,
|
| 70 |
+
extra: Optional[Dict[str, Any]] = None,
|
| 71 |
+
) -> Dict[str, Any]:
|
| 72 |
+
return chain_event_from_freeze_case(
|
| 73 |
+
freeze_case,
|
| 74 |
+
event_type="freeze_created",
|
| 75 |
+
decision="FREEZE",
|
| 76 |
+
extra=extra or {},
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
def chain_freeze_released(
|
| 80 |
+
self,
|
| 81 |
+
freeze_case: Dict[str, Any],
|
| 82 |
+
*,
|
| 83 |
+
extra: Optional[Dict[str, Any]] = None,
|
| 84 |
+
) -> Dict[str, Any]:
|
| 85 |
+
return chain_event_from_freeze_case(
|
| 86 |
+
freeze_case,
|
| 87 |
+
event_type="freeze_released",
|
| 88 |
+
decision="ALLOW",
|
| 89 |
+
extra=extra or {},
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
def chain_freeze_escalated(
|
| 93 |
+
self,
|
| 94 |
+
freeze_case: Dict[str, Any],
|
| 95 |
+
*,
|
| 96 |
+
extra: Optional[Dict[str, Any]] = None,
|
| 97 |
+
) -> Dict[str, Any]:
|
| 98 |
+
return chain_event_from_freeze_case(
|
| 99 |
+
freeze_case,
|
| 100 |
+
event_type="freeze_escalated",
|
| 101 |
+
decision="BLOCK",
|
| 102 |
+
extra=extra or {},
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
# ------------------------------------------------------------------
|
| 106 |
+
# Freeze lifecycle wrappers
|
| 107 |
+
# ------------------------------------------------------------------
|
| 108 |
+
def release_case(
|
| 109 |
+
self,
|
| 110 |
+
case_id: str,
|
| 111 |
+
*,
|
| 112 |
+
reviewer: str,
|
| 113 |
+
release_notes: str = "",
|
| 114 |
+
extra: Optional[Dict[str, Any]] = None,
|
| 115 |
+
) -> Dict[str, Any]:
|
| 116 |
+
if not self.freeze_review:
|
| 117 |
+
raise RuntimeError("SovereignFreezeReview is not available")
|
| 118 |
+
|
| 119 |
+
result = self.freeze_review.release_case(
|
| 120 |
+
case_id=case_id,
|
| 121 |
+
reviewer=reviewer,
|
| 122 |
+
release_notes=release_notes,
|
| 123 |
+
)
|
| 124 |
+
|
| 125 |
+
freeze_case = result.get("case") or {}
|
| 126 |
+
chain = self.chain_freeze_released(
|
| 127 |
+
freeze_case,
|
| 128 |
+
extra={
|
| 129 |
+
"reviewer": reviewer,
|
| 130 |
+
"release_notes": release_notes,
|
| 131 |
+
**(extra or {}),
|
| 132 |
+
},
|
| 133 |
+
)
|
| 134 |
+
result["audit_chain"] = chain.get("chain")
|
| 135 |
+
return result
|
| 136 |
+
|
| 137 |
+
def escalate_case(
|
| 138 |
+
self,
|
| 139 |
+
case_id: str,
|
| 140 |
+
*,
|
| 141 |
+
reviewer: str,
|
| 142 |
+
escalation_reason: str = "",
|
| 143 |
+
extra: Optional[Dict[str, Any]] = None,
|
| 144 |
+
) -> Dict[str, Any]:
|
| 145 |
+
if not self.freeze_review:
|
| 146 |
+
raise RuntimeError("SovereignFreezeReview is not available")
|
| 147 |
+
|
| 148 |
+
result = self.freeze_review.escalate_case(
|
| 149 |
+
case_id=case_id,
|
| 150 |
+
reviewer=reviewer,
|
| 151 |
+
escalation_reason=escalation_reason,
|
| 152 |
+
)
|
| 153 |
+
|
| 154 |
+
freeze_case = result.get("case") or {}
|
| 155 |
+
chain = self.chain_freeze_escalated(
|
| 156 |
+
freeze_case,
|
| 157 |
+
extra={
|
| 158 |
+
"reviewer": reviewer,
|
| 159 |
+
"escalation_reason": escalation_reason,
|
| 160 |
+
**(extra or {}),
|
| 161 |
+
},
|
| 162 |
+
)
|
| 163 |
+
result["audit_chain"] = chain.get("chain")
|
| 164 |
+
return result
|
| 165 |
+
|
| 166 |
+
def get_case(self, case_id: str) -> Dict[str, Any]:
|
| 167 |
+
if not self.freeze_review:
|
| 168 |
+
raise RuntimeError("SovereignFreezeReview is not available")
|
| 169 |
+
return self.freeze_review.get_case(case_id)
|
| 170 |
+
|
| 171 |
+
|
| 172 |
+
BRIDGE = SovereignChainRuntimeBridge()
|
| 173 |
+
|