File size: 6,695 Bytes
5729d24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
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()