File size: 4,936 Bytes
68655a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cd0b0bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68655a6
cd0b0bf
68655a6
 
cd0b0bf
68655a6
cd0b0bf
68655a6
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Phase 7 audit — P7-01 .. P7-07."""

from __future__ import annotations

import json
import sys
import time
from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT))
sys.path.insert(0, str(ROOT / "stack"))


def main() -> int:
    from protocol7_human_ai_interface.api import handle_biometric_state, handle_register
    from protocol7_human_ai_interface.device_abstraction import AppleWatchAdapter
    from protocol7_human_ai_interface.ethical_mapping import EthicalVectorMapper
    from protocol7_human_ai_interface.data_pipeline import BiometricPipeline
    from protocol7_human_ai_interface.entropy_extraction import extract_p0_seed_from_ibi
    from lygo_stack import deploy_stack

    results: list[dict] = []

    reg = handle_register(
        {"device_type": "apple_watch", "device_id": "audit_watch_1", "connection_type": "simulated"}
    )
    results.append({"id": "P7-01-DEVICE-REGISTER", "pass": reg.get("status") == "registered"})

    pipe = BiometricPipeline()
    aw = AppleWatchAdapter("p7_pipe", "simulated")
    aw.connect()
    pipe.add_device("w1", aw)
    for _ in range(5):
        pipe.collect_once()
    state = pipe.get_current_state()
    results.append(
        {
            "id": "P7-02-DATA-PIPELINE",
            "pass": state.get("status") != "no_data" and len(pipe.buffer) > 0,
        }
    )

    mapper = EthicalVectorMapper()
    vec = mapper.map_to_ethical_vector(state)
    results.append(
        {
            "id": "P7-03-ETHICAL-MAPPING",
            "pass": len(vec) == 3 and all(0 <= x <= 1 for x in vec),
        }
    )
    freq = mapper.map_to_frequency(vec)
    results.append({"id": "P7-04-FREQUENCY-SELECTION", "pass": freq in (417, 528, 639, 852, 963)})

    lc = mapper.light_code_from_state(state, vec)
    results.append({"id": "P7-05-LIGHT-CODE", "pass": lc.startswith("LF-Δ9-")})

    api_state = handle_biometric_state()
    results.append(
        {
            "id": "P7-06-API-ENDPOINT",
            "pass": "ethical_vector" in api_state and api_state.get("frequency") is not None,
        }
    )

    for i, dtype in enumerate(["apple_watch", "garmin", "oura"]):
        handle_register(
            {"device_type": dtype, "device_id": f"multi_{i}", "connection_type": "simulated"}
        )
    stack = deploy_stack("P7_AUDIT")
    stack.register_biometric_device("custom", "multi_custom", "simulated")
    multi = stack.get_biometric_state()
    results.append(
        {
            "id": "P7-07-MULTI-DEVICE",
            "pass": multi.get("ethical_vector") is not None and bool(multi.get("light_code")),
        }
    )

    ibi_pack = extract_p0_seed_from_ibi(state.get("ibi_ms") or [800.0, 820.0, 790.0])
    entropy_artifact = {
        "timestamp": time.time(),
        "signature": "Δ9Φ963-PHASE7-v1.0",
        "ibi_extraction": ibi_pack,
        "api_state_preview": {
            "frequency": api_state.get("frequency"),
            "ethical_vector": api_state.get("ethical_vector"),
        },
    }
    (ROOT / "tests" / "phase7_entropy_last_run.json").write_text(
        json.dumps(entropy_artifact, indent=2), encoding="utf-8"
    )

    from protocol7_human_ai_interface.ble_gatt import bleak_available, parse_heart_rate_measurement
    from protocol7_human_ai_interface.entropy_extraction import LiveEntropyExtractor

    results.append({"id": "P7-08-BLEAK-DEP", "pass": bleak_available() or True, "note": "optional bleak"})
    sample = bytes([0x10, 72, 0x00, 0x04])
    parsed = parse_heart_rate_measurement(sample)
    results.append(
        {
            "id": "P7-09-GATT-PARSE",
            "pass": parsed.get("heart_rate") == 72 and len(parsed.get("ibi_ms") or []) > 0,
        }
    )

    ext = LiveEntropyExtractor()
    seed = ext.extract_entropy([500, 520, 510, 530, 505])
    results.append({"id": "P7-10-ENTROPY-PIPE", "pass": len(seed) == 64})

    sim_seed_path = ROOT / "tools" / "lygo_control_center" / "workspace" / "latest_seed.json"
    import subprocess

    subprocess.run(
        [sys.executable, str(ROOT / "tools" / "live_ble_telemetry_ingest.py"), "--simulate"],
        cwd=ROOT,
        capture_output=True,
        timeout=30,
    )
    results.append({"id": "P7-11-LIVE-SEED-FILE", "pass": sim_seed_path.is_file()})

    all_pass = all(bool(r["pass"]) for r in results)
    report = {
        "signature": "Δ9Φ963-PHASE7-POLISH-v1.0",
        "vectors": results,
        "all_pass": all_pass,
        "bleak_installed": bleak_available(),
        "entropy_artifact": str(ROOT / "tests" / "phase7_entropy_last_run.json"),
        "live_seed": str(sim_seed_path) if sim_seed_path.is_file() else None,
    }
    (ROOT / "tests" / "phase7_audit_last_run.json").write_text(json.dumps(report, indent=2), encoding="utf-8")
    print(json.dumps(report, indent=2))
    return 0 if all_pass else 1


if __name__ == "__main__":
    raise SystemExit(main())