rezabarkhordary commited on
Commit
720b97a
·
verified ·
1 Parent(s): f4a448a

Create sovereign_operational_resilience_report.py

Browse files
sovereign_operational_resilience_report.py ADDED
@@ -0,0 +1,291 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ # sovereign_operational_resilience_report.py
3
+ from __future__ import annotations
4
+
5
+ import json
6
+ import os
7
+ from datetime import datetime, timezone
8
+ from typing import Any, Dict, List, Optional
9
+
10
+ from sovereign_operational_resilience_profile import generate_operational_resilience_profile
11
+
12
+
13
+ OPERATIONAL_RESILIENCE_OUTPUT_DIR = "sovereign_operational_resilience_outputs"
14
+
15
+
16
+ def _utc_now() -> str:
17
+ return datetime.now(timezone.utc).isoformat()
18
+
19
+
20
+ def _ensure_dir(path: str) -> None:
21
+ os.makedirs(path, exist_ok=True)
22
+
23
+
24
+ def _safe(v: Any, default: Any = "") -> Any:
25
+ return v if v is not None else default
26
+
27
+
28
+ def _bool_text(v: Any) -> str:
29
+ return "True" if bool(v) else "False"
30
+
31
+
32
+ def _render_header(bundle: Dict[str, Any]) -> str:
33
+ profile = bundle.get("operational_resilience_profile", {}) if isinstance(bundle, dict) else {}
34
+
35
+ return f"""# Sovereign Operational Resilience Report
36
+
37
+ Generated at: {_safe((profile or {}).get("generated_at"), _utc_now())}
38
+
39
+ ## 1. Profile Identity
40
+ - Profile Type: {_safe(profile.get("profile_type"), "Sovereign Operational Resilience Profile")}
41
+ - Domain: {_safe(profile.get("domain"), "banking_and_financial_institutions")}
42
+ """
43
+
44
+
45
+ def _render_summary(bundle: Dict[str, Any]) -> str:
46
+ profile = bundle.get("operational_resilience_profile", {}) if isinstance(bundle, dict) else {}
47
+ summary = profile.get("summary", {}) if isinstance(profile, dict) else {}
48
+
49
+ return f"""## 2. Executive Summary
50
+ - Total Degradation Classes: {_safe(summary.get("total_degradation_classes"), 0)}
51
+ - Freeze or Freeze-Like Classes: {_safe(summary.get("freeze_or_freeze_like_classes"), 0)}
52
+ - Block-Default Classes: {_safe(summary.get("block_default_classes"), 0)}
53
+ - Human-Escalation Classes: {_safe(summary.get("human_escalation_classes"), 0)}
54
+
55
+ ### Practical Interpretation
56
+ - Sovereign treats resilience as controlled continuity, not unrestricted continuation under degraded conditions.
57
+ - Degraded runtime posture is expected to narrow authority rather than silently preserve normal authority.
58
+ - Critical degradation can drive the system into minimal-safe or no-normal-runtime modes.
59
+ - Recovery and degraded operation are expected to remain auditable and evidence-backed.
60
+ """
61
+
62
+
63
+ def _render_degradation_classes(bundle: Dict[str, Any]) -> str:
64
+ profile = bundle.get("operational_resilience_profile", {}) if isinstance(bundle, dict) else {}
65
+ classes = profile.get("degradation_classes", []) if isinstance(profile, dict) else []
66
+
67
+ lines: List[str] = ["## 3. Degradation Class Inventory"]
68
+
69
+ if not classes:
70
+ lines.append("- No degradation classes recorded.")
71
+ return "\n".join(lines)
72
+
73
+ for item in classes:
74
+ lines.append(f"### {item.get('degradation_class', 'unknown_degradation_class')}")
75
+ lines.append(f"- Severity: {_safe(item.get('severity'))}")
76
+ lines.append(f"- Default Mode: {_safe(item.get('default_mode'))}")
77
+ lines.append(f"- Default Action: {_safe(item.get('default_action'))}")
78
+ lines.append(f"- Human Escalation Required: {_bool_text(item.get('human_escalation_required'))}")
79
+ lines.append(f"- Evidence Required: {_bool_text(item.get('evidence_required'))}")
80
+ lines.append(f"- Description: {_safe(item.get('description'))}")
81
+ lines.append("")
82
+
83
+ return "\n".join(lines).rstrip() + "\n"
84
+
85
+
86
+ def _render_continuity_principles(bundle: Dict[str, Any]) -> str:
87
+ profile = bundle.get("operational_resilience_profile", {}) if isinstance(bundle, dict) else {}
88
+ principles = profile.get("continuity_principles", []) if isinstance(profile, dict) else []
89
+
90
+ lines: List[str] = ["## 4. Continuity Principles"]
91
+
92
+ if not principles:
93
+ lines.append("- No continuity principles recorded.")
94
+ return "\n".join(lines)
95
+
96
+ for item in principles:
97
+ lines.append(f"### {item.get('principle', 'unknown_principle')}")
98
+ lines.append(f"- Status: {_safe(item.get('status'))}")
99
+ lines.append(f"- Description: {_safe(item.get('description'))}")
100
+ lines.append("")
101
+
102
+ return "\n".join(lines).rstrip() + "\n"
103
+
104
+
105
+ def _render_fallback_posture(bundle: Dict[str, Any]) -> str:
106
+ profile = bundle.get("operational_resilience_profile", {}) if isinstance(bundle, dict) else {}
107
+ fallback = profile.get("fallback_posture", {}) if isinstance(profile, dict) else {}
108
+
109
+ modes = fallback.get("fallback_modes", []) if isinstance(fallback, dict) else []
110
+ forbidden = fallback.get("forbidden_fallback_patterns", []) if isinstance(fallback, dict) else []
111
+
112
+ lines = ["## 5. Fallback Posture"]
113
+
114
+ if modes:
115
+ lines.append("- Fallback Modes:")
116
+ for item in modes:
117
+ lines.append(f" - {item.get('mode')}: {item.get('meaning')}")
118
+
119
+ if forbidden:
120
+ lines.append("- Forbidden Fallback Patterns:")
121
+ for item in forbidden:
122
+ lines.append(f" - {item}")
123
+
124
+ return "\n".join(lines)
125
+
126
+
127
+ def _render_evidence_expectations(bundle: Dict[str, Any]) -> str:
128
+ profile = bundle.get("operational_resilience_profile", {}) if isinstance(bundle, dict) else {}
129
+ evidence = profile.get("evidence_expectations", {}) if isinstance(profile, dict) else {}
130
+
131
+ required_artifacts = evidence.get("required_artifacts", []) if isinstance(evidence, dict) else []
132
+ expectations = evidence.get("expectations", []) if isinstance(evidence, dict) else []
133
+
134
+ lines = ["## 6. Evidence Expectations"]
135
+
136
+ if required_artifacts:
137
+ lines.append("- Required Artifacts:")
138
+ for item in required_artifacts:
139
+ lines.append(f" - {item}")
140
+
141
+ if expectations:
142
+ lines.append("- Expectations:")
143
+ for item in expectations:
144
+ lines.append(f" - {item}")
145
+
146
+ return "\n".join(lines)
147
+
148
+
149
+ def _render_governance_interpretation(bundle: Dict[str, Any]) -> str:
150
+ profile = bundle.get("operational_resilience_profile", {}) if isinstance(bundle, dict) else {}
151
+ classes = profile.get("degradation_classes", []) if isinstance(profile, dict) else []
152
+
153
+ freeze_like = [
154
+ x.get("degradation_class")
155
+ for x in classes
156
+ if str(x.get("default_action", "")).upper() in {
157
+ "FREEZE",
158
+ "FREEZE_FOR_HIGH_RISK_ONLY",
159
+ "FREEZE_FOR_SENSITIVE_PATHS",
160
+ "REVIEW_OR_FREEZE",
161
+ "BLOCK_OR_FREEZE",
162
+ }
163
+ ]
164
+ block_default = [
165
+ x.get("degradation_class")
166
+ for x in classes
167
+ if str(x.get("default_action", "")).upper() == "BLOCK"
168
+ ]
169
+
170
+ lines = ["## 7. Governance Interpretation"]
171
+
172
+ if freeze_like:
173
+ lines.append("- Freeze-Oriented Degradation Classes:")
174
+ for item in freeze_like:
175
+ lines.append(f" - {item}")
176
+
177
+ if block_default:
178
+ lines.append("- Block-Oriented Degradation Classes:")
179
+ for item in block_default:
180
+ lines.append(f" - {item}")
181
+
182
+ lines.append("- Degraded continuity should remain narrower than normal continuity when trust is reduced.")
183
+ lines.append("- High-risk paths should not continue as normal under material uncertainty.")
184
+ lines.append("- Degraded runtime entry, fallback, and recovery should remain reviewable and evidence-backed.")
185
+
186
+ return "\n".join(lines)
187
+
188
+
189
+ def _render_artifact_status(bundle: Dict[str, Any]) -> str:
190
+ seal = bundle.get("crypto_seal", {}) if isinstance(bundle, dict) else {}
191
+
192
+ return f"""## 8. Artifact Status
193
+ - Sealed: {bool(bundle.get("sealed", False))}
194
+ - Seal Digest: {_safe(seal.get("seal_digest"), "")}
195
+ - Seal Scheme: {_safe(seal.get("scheme"), "")}
196
+ - Seal Algorithm: {_safe(seal.get("algorithm"), "")}
197
+ - Signer Hint: {_safe(seal.get("signer_hint"), "")}
198
+ """
199
+
200
+
201
+ def _render_verdict(bundle: Dict[str, Any]) -> str:
202
+ profile = bundle.get("operational_resilience_profile", {}) if isinstance(bundle, dict) else {}
203
+ notes = profile.get("notes", []) if isinstance(profile, dict) else []
204
+
205
+ verdict = (
206
+ "Sovereign currently expresses a bank-grade operational resilience posture: "
207
+ "degraded operation is explicitly classified, fallback modes are bounded, "
208
+ "and continuity under impairment is treated as controlled and auditable rather than permissive."
209
+ )
210
+
211
+ lines = [
212
+ "## 9. Preliminary Bank-Facing Verdict",
213
+ verdict,
214
+ "",
215
+ "## 10. Notes",
216
+ ]
217
+
218
+ if notes:
219
+ for item in notes:
220
+ lines.append(f"- {item}")
221
+ else:
222
+ lines.append("- No additional notes provided.")
223
+
224
+ return "\n".join(lines)
225
+
226
+
227
+ def build_operational_resilience_markdown(bundle: Dict[str, Any]) -> str:
228
+ parts = [
229
+ _render_header(bundle),
230
+ _render_summary(bundle),
231
+ _render_degradation_classes(bundle),
232
+ _render_continuity_principles(bundle),
233
+ _render_fallback_posture(bundle),
234
+ _render_evidence_expectations(bundle),
235
+ _render_governance_interpretation(bundle),
236
+ _render_artifact_status(bundle),
237
+ _render_verdict(bundle),
238
+ ]
239
+ return "\n\n".join(parts).strip() + "\n"
240
+
241
+
242
+ def save_operational_resilience_report(
243
+ bundle: Dict[str, Any],
244
+ output_dir: str = OPERATIONAL_RESILIENCE_OUTPUT_DIR,
245
+ base_name: Optional[str] = None,
246
+ ) -> Dict[str, str]:
247
+ _ensure_dir(output_dir)
248
+
249
+ ts = datetime.now(timezone.utc).strftime("%Y%m%dT%H%M%SZ")
250
+ base = base_name or f"sovereign_operational_resilience_report_{ts}"
251
+
252
+ json_path = os.path.join(output_dir, f"{base}.json")
253
+ md_path = os.path.join(output_dir, f"{base}.md")
254
+
255
+ with open(json_path, "w", encoding="utf-8") as f:
256
+ json.dump(bundle, f, ensure_ascii=False, indent=2)
257
+
258
+ markdown = build_operational_resilience_markdown(bundle)
259
+ with open(md_path, "w", encoding="utf-8") as f:
260
+ f.write(markdown)
261
+
262
+ return {
263
+ "json_report": json_path,
264
+ "markdown_report": md_path,
265
+ }
266
+
267
+
268
+ def generate_operational_resilience_report(
269
+ output_dir: str = OPERATIONAL_RESILIENCE_OUTPUT_DIR,
270
+ base_name: Optional[str] = None,
271
+ ) -> Dict[str, Any]:
272
+ bundle = generate_operational_resilience_profile()
273
+
274
+ files = save_operational_resilience_report(
275
+ bundle=bundle,
276
+ output_dir=output_dir,
277
+ base_name=base_name,
278
+ )
279
+
280
+ return {
281
+ "ok": True,
282
+ "generated_at": _utc_now(),
283
+ "bundle": bundle,
284
+ "files": files,
285
+ }
286
+
287
+
288
+ if __name__ == "__main__":
289
+ out = generate_operational_resilience_report()
290
+ print(json.dumps(out, indent=2, ensure_ascii=False))
291
+