Sunstead trust scoring project
1"""Smoke checks: run the module self-checks under pytest (PRD names pytest)."""
2
3import pytest
4
5from trust import eigentrust, fusion, review
6
7
8def test_eigentrust_starves_sybils():
9 eigentrust.demo() # asserts trusted chain > sybil cluster + correct path
10
11
12def test_gate_never_lifts_untrusted():
13 fusion.demo() # asserts constraint 2: clean content can't fast-lane low trust
14
15
16def test_review_schema_shape():
17 review.demo() # parses schema, or no-ops cleanly without an API key
18
19
20def test_static_scan_flags_added_secrets():
21 from trust import vouchsafe
22 vouchsafe.demo() # flags an added secret, redacts the value, ignores removals
23
24
25def test_learned_ranks_trusted_above_sybil():
26 pytest.importorskip("lightgbm") # M5 is the .[learned] extra; skip if not installed
27 from trust import learned
28 learned.demo() # trains, asserts calibrated P(clean): trusted > sybil
29
30
31def test_gnn_trains_and_is_stable():
32 pytest.importorskip("torch_geometric") # M6 is the .[gnn] extra; skip if not installed
33 from trust import gnn
34 gnn.demo() # trains GraphSAGE, asserts finite outputs + a written verdict
35
36
37def test_atproto_record_shape():
38 from trust import atproto
39 atproto.demo() # builds a valid sh.tangled.trust.score record (no network)
40
41
42def test_voice_brief_text():
43 from trust import voice
44 voice.demo() # composes a speakable brief; synth no-ops without a key
45
46
47def test_embed_slop_ranking():
48 from trust import embed
49 embed.demo() # cosine identities + slop-corpus ranking; live embed only with a key
50
51
52def test_diffs_gunzip_roundtrip():
53 from trust import diffs
54 diffs.demo() # patchBlob gunzip round-trip + CID extraction (no network)
55
56
57def test_content_head_outranks_bad_diff():
58 pytest.importorskip("sklearn") # Tower B head is the .[learned] extra (scikit-learn)
59 from trust import content
60 content.demo() # frozen-embedding linear probe: a bad diff out-risks a clean one
61
62
63def test_pull_status_drives_clean_merge():
64 """sh.tangled.repo.pull.status (public) -> merged/closed -> clean_merge label."""
65 import json
66 import duckdb
67 from trust import db, ingest
68
69 con = duckdb.connect(":memory:")
70 con.execute(db.SCHEMA); con.execute(db.FEATURES_VIEW); con.execute(db.PR_LABELS_VIEW)
71 did = "did:plc:x"
72 old = "2020-01-01T00:00:00Z" # well past the N-day window, so not NULL'd
73 def ev(coll, rkey, rec): return (did, 0, "create", coll, rkey, json.dumps(rec))
74 ingest.derive(con, [
75 ev("sh.tangled.repo.pull", "r1", {"createdAt": old, "target": {"repo": "x", "branch": "main"}}),
76 ev("sh.tangled.repo.pull.status", "s1",
77 {"pull": f"at://{did}/sh.tangled.repo.pull/r1", "status": "sh.tangled.repo.pull.status.merged"}),
78 ev("sh.tangled.repo.pull", "r2", {"createdAt": old}),
79 ev("sh.tangled.repo.pull.status", "s2",
80 {"pull": f"at://{did}/sh.tangled.repo.pull/r2", "status": "sh.tangled.repo.pull.status.closed"}),
81 ])
82 lbl = dict(con.execute("SELECT pr_id, clean_merge FROM pr_labels").fetchall())
83 assert lbl[f"{did}/sh.tangled.repo.pull/r1"] == 1, "merged + old + no revert -> clean_merge=1"
84 assert lbl[f"{did}/sh.tangled.repo.pull/r2"] == 0, "closed unmerged -> clean_merge=0"
85
86
87def test_stars_trust_weights_by_starrer_trust():
88 """A star from a trusted DID must outweigh a star from an untrusted/sybil DID."""
89 import json
90 import duckdb
91 from trust import db, ingest, eigentrust
92
93 con = duckdb.connect(":memory:")
94 con.execute(db.SCHEMA); con.execute(db.FEATURES_VIEW); con.execute(db.PR_LABELS_VIEW)
95 # trusted chain seed -> alice; sybil is isolated (no incoming trust).
96 con.execute("INSERT INTO contributors (did) VALUES ('seed'),('alice'),('sybil')")
97 con.execute("INSERT INTO vouches (voucher_did, subject_did) VALUES ('seed','alice')")
98 con.execute("INSERT INTO seeds VALUES ('seed')")
99
100 def star(starrer, owner): # derive() inserts the starrer as a contributor too
101 ingest.derive(con, [(starrer, 0, "create", "sh.tangled.feed.star", f"{starrer}-{owner}",
102 json.dumps({"subject": {"did": owner, "$type": "sh.tangled.feed.star#repo"}}))])
103 star("alice", "did:plc:ownerA") # endorsed by a trusted DID
104 star("sybil", "did:plc:ownerB") # endorsed by an untrusted DID
105
106 er = eigentrust.compute(con)
107 a = er.stars_trust.get("did:plc:ownerA", 0.0)
108 b = er.stars_trust.get("did:plc:ownerB", 0.0)
109 assert a > b, f"trusted-starred owner ({a}) must outrank sybil-starred owner ({b})"
110 # raw count is blind to starrer trust: both owners show 1 star received.
111 raw = dict(con.execute("SELECT owner_did, COUNT(*) FROM stars GROUP BY owner_did").fetchall())
112 assert raw["did:plc:ownerA"] == raw["did:plc:ownerB"] == 1