protocol_one_env / scripts /verify_phase1.py
suhaniawasthi's picture
Upload folder using huggingface_hub
6ff09d1 verified
Raw
History Blame Contribute Delete
2.72 kB
#!/usr/bin/env python
"""Phase 1 verification. Run this to prove the spec/matcher/server triad is consistent.
Expected output (all green):
βœ“ Spec validates
βœ“ Server boots
βœ“ Empty belief graph : 0.000
βœ“ Perfect belief graph : 0.9xx
βœ“ Half belief graph : 0.2xx - 0.5xx
βœ“ Garbage gets penalised: false_claims >= 15
Phase 1 verified.
"""
from __future__ import annotations
import os
import sys
# Allow running as a bare script from anywhere
HERE = os.path.dirname(os.path.abspath(__file__))
ROOT = os.path.dirname(HERE)
if ROOT not in sys.path:
sys.path.insert(0, ROOT)
from fastapi.testclient import TestClient # noqa: E402
from server.matcher import score # noqa: E402
from server.protocol_server import create_server # noqa: E402
from server.spec import SPEC # noqa: E402
from server.spec_schema import load_and_validate_spec # noqa: E402
from tests.helpers import belief_graph_from_spec, build_half_belief_graph # noqa: E402
def main() -> int:
# 1. Spec validates
load_and_validate_spec()
print("βœ“ Spec validates")
# 2. Server boots
srv = create_server()
c = TestClient(srv.app)
r = c.get("/_/health")
assert r.status_code == 200, r.text
r = c.get("/users", headers={"Authorization": "Bearer token_full"})
assert r.status_code == 200, r.text
print("βœ“ Server boots and serves authed traffic")
# 3. Empty belief graph near zero
r_empty = score({}, SPEC)
assert r_empty.total < 0.05, f"empty scored {r_empty.total}"
print(f"βœ“ Empty belief graph : {r_empty.total:.3f}")
# 4. Perfect belief graph near one
r_perfect = score(belief_graph_from_spec(SPEC), SPEC)
assert r_perfect.total > 0.9, f"perfect scored {r_perfect.total}"
print(f"βœ“ Perfect belief graph : {r_perfect.total:.3f}")
# 5. Half-ish belief graph gets partial credit
r_half = score(build_half_belief_graph(SPEC), SPEC)
assert 0.20 < r_half.total < 0.55, f"half scored {r_half.total}"
print(f"βœ“ Half belief graph : {r_half.total:.3f}")
# 6. Garbage is penalised
garbage = {"endpoints": [{"method": "GET", "path": f"/fake_{i}"} for i in range(20)]}
r_g = score(garbage, SPEC)
assert r_g.false_claims >= 15, f"only {r_g.false_claims} false claims detected"
assert r_g.total < 0.05, f"garbage scored {r_g.total}"
print(f"βœ“ Garbage penalised : {r_g.false_claims} false claims, total {r_g.total:.3f}")
# 7. Spec has exactly 18 endpoints
assert len(SPEC["endpoints"]) == 18, f"spec has {len(SPEC['endpoints'])} endpoints, not 18"
print(f"βœ“ 18 endpoints in spec")
print("\nPhase 1 verified.")
return 0
if __name__ == "__main__":
sys.exit(main())