cybersec-audit-trail-stack / runtime_examples_run_audit_chain_demo.py
HirModel's picture
Upload 21 files
5729d24 verified
Raw
History Blame Contribute Delete
6.7 kB
"""
Primordial OS Runtime — Audit Chain Demo (Phase 10)
Multi-event JSONL audit chain demonstration using synthetic runtime evaluations.
Writes synthetic audit events to audit_logs/demo_audit_chain.jsonl, reads the
file back, verifies the hash chain, and prints a chain summary.
Synthetic demo events only. No real user data, PHI, PII, or clinical content.
Pre-validation architecture. Not clinical software. Not a medical device.
Does not diagnose, treat, cure, or make medical decisions.
"""
from __future__ import annotations
from pathlib import Path
from primordial_os.audit_log import append_to_audit_chain
from primordial_os.audit_store import append_audit_event_jsonl, verify_audit_jsonl, read_audit_events_jsonl
from primordial_os.hir_kernel import HIRInput
from primordial_os.oam_detector import OAMSignals
from primordial_os.runtime import run_evaluation
# ---------------------------------------------------------------------------
# Synthetic demo scenarios (no real user data)
# ---------------------------------------------------------------------------
_DEMO_SCENARIOS: list[tuple[str, HIRInput, OAMSignals]] = [
(
"demo_green_baseline",
HIRInput(honesty=0.92, integrity=0.90, respect=0.88, accountability=0.91, pressure=0.05),
OAMSignals(
certainty_level=0.55, decision_made_for_user=False,
context_preservation=0.93, dignity_preserved=0.95,
identity_assigned=False, session_load_ratio=0.18,
uncertainty_disclosed=True, autonomy_scope=0.15,
consent_present=True, clinical_language_present=False,
memory_accessed_without_consent=False,
end_of_life_decision_attempted=False, human_override_available=True,
),
),
(
"demo_yellow_elevated_pressure",
HIRInput(honesty=0.68, integrity=0.66, respect=0.67, accountability=0.65, pressure=0.22),
OAMSignals(
certainty_level=0.50, decision_made_for_user=False,
context_preservation=0.84, dignity_preserved=0.87,
identity_assigned=False, session_load_ratio=0.26,
uncertainty_disclosed=True, autonomy_scope=0.20,
consent_present=True, clinical_language_present=False,
memory_accessed_without_consent=False,
end_of_life_decision_attempted=False, human_override_available=True,
),
),
(
"demo_red_agency_outsourcing",
HIRInput(honesty=0.91, integrity=0.89, respect=0.90, accountability=0.92, pressure=0.04),
OAMSignals(
certainty_level=0.55, decision_made_for_user=True,
context_preservation=0.90, dignity_preserved=0.93,
identity_assigned=False, session_load_ratio=0.20,
uncertainty_disclosed=True, autonomy_scope=0.20,
consent_present=True, clinical_language_present=False,
memory_accessed_without_consent=False,
end_of_life_decision_attempted=False, human_override_available=True,
),
),
(
"demo_green_recovery",
HIRInput(honesty=0.88, integrity=0.86, respect=0.87, accountability=0.89, pressure=0.08),
OAMSignals(
certainty_level=0.52, decision_made_for_user=False,
context_preservation=0.91, dignity_preserved=0.92,
identity_assigned=False, session_load_ratio=0.21,
uncertainty_disclosed=True, autonomy_scope=0.18,
consent_present=True, clinical_language_present=False,
memory_accessed_without_consent=False,
end_of_life_decision_attempted=False, human_override_available=True,
),
),
(
"demo_yellow_session_load",
HIRInput(honesty=0.75, integrity=0.73, respect=0.74, accountability=0.72, pressure=0.15),
OAMSignals(
certainty_level=0.53, decision_made_for_user=False,
context_preservation=0.80, dignity_preserved=0.82,
identity_assigned=False, session_load_ratio=0.42,
uncertainty_disclosed=True, autonomy_scope=0.22,
consent_present=True, clinical_language_present=False,
memory_accessed_without_consent=False,
end_of_life_decision_attempted=False, human_override_available=True,
),
),
]
# ---------------------------------------------------------------------------
# Main
# ---------------------------------------------------------------------------
def main() -> None:
project_root = Path(__file__).parent.parent
audit_dir = project_root / "audit_logs"
output_path = audit_dir / "demo_audit_chain.jsonl"
print("=== Primordial OS Runtime Prototype — Audit Chain Demo ===")
print()
print("PRE-VALIDATION DISCLAIMER")
print(" This is pre-validation architecture only. Not clinical software.")
print(" Not a medical device. Does not diagnose, treat, cure, or make")
print(" medical decisions. All events are synthetic demonstrations.")
print()
if not audit_dir.exists():
print(f"[ERROR] Audit log directory does not exist: {audit_dir}")
print(" Create the directory and re-run.")
return
# Start fresh each run so the demo chain is self-contained.
if output_path.exists():
output_path.unlink()
# Build the in-memory chain, then write each event to JSONL.
chain: tuple = ()
for label, hir_input, oam_signals in _DEMO_SCENARIOS:
decision = run_evaluation(hir_input, oam_signals)
chain = append_to_audit_chain(chain, decision, event_type=label)
append_audit_event_jsonl(output_path, chain[-1])
# Read back from disk and verify.
events = read_audit_events_jsonl(output_path)
chain_valid = verify_audit_jsonl(output_path)
# Summary output.
print(f"Output file: {output_path}")
print(f"Events written: {len(events)}")
print(f"Chain verified: {chain_valid}")
print()
if events:
first_hash = events[0].current_hash[:12]
last_hash = events[-1].current_hash[:12]
print(f"First hash: {first_hash}...")
print(f"Last hash: {last_hash}...")
print()
print("--- Event Summary ---")
for i, event in enumerate(events, start=1):
gate = event.final_gate_state
resonance = event.resonance
hard_stop = event.hard_stop
label = event.event_type
print(
f" [{i}] {label:<32} Gate={gate:<6} "
f"Resonance={resonance:.3f} HardStop={hard_stop}"
)
print()
print("PRE-VALIDATION ARCHITECTURE. NOT CLINICAL SOFTWARE.")
print("NOT A MEDICAL DEVICE. SYNTHETIC DEMO EVENTS ONLY.")
if __name__ == "__main__":
main()