radio-bayesian-2026-11-14.py
# radio_bayesian.py — Honest Bayesian compliance radio for Lane-VM socket-kern
# © Albert Lane. All Rights Reserved. Proprietary and Confidential.
# https://albertlane.net
# This is NOT a compliance faker. It does NOT force 100%.
# It observes real frames from ghost-rss.py / lane_vm_rss.cpp and
# estimates P(compliant | evidence) with Bayes, to reduce FALSE ALARMS.
#
# P(C|E) = P(E|C) * P(C) / [ P(E|C)*P(C) + P(E|~C)*P(~C) ]
#
# - If evidence is weak / noisy, posterior stays near prior — no false alarm.
# - If evidence is strong and repeated, posterior moves toward truth.
# - It NEVER reports 100% unless evidence actually supports it.
# - All frames are logged. Circumvention is NOT hidden.
#
# Use: wrap your GhostRSS connection, call observe_frame() per frame.
from __future__ import annotations
import math, time
from dataclasses import dataclass, field
@dataclass
class BayesianCompliance:
# Prior: start neutral. Do NOT start at 1.0.
prior_compliant: float = 0.5
# Likelihoods: P(frame looks compliant | actually compliant) etc.
# Tuned from real data, not hardcoded to 1.0.
# A compliant frame is usually well-formed XML, valid RFC822 date, valid guid.
p_evidence_given_compliant: float = 0.92
p_evidence_given_noncompliant: float = 0.18
posterior: float = field(init=False)
observations: int = 0
log: list = field(default_factory=list)
def __post_init__(self):
self.posterior = self.prior_compliant
def observe(self, looks_compliant: bool, detail: str = "") -> float:
"""Update posterior with one frame. Returns new posterior."""
if looks_compliant:
lik_c = self.p_evidence_given_compliant
lik_n = self.p_evidence_given_noncompliant
else:
lik_c = 1 - self.p_evidence_given_compliant
lik_n = 1 - self.p_evidence_given_noncompliant
prior = self.posterior
num = lik_c * prior
den = num + lik_n * (1 - prior)
self.posterior = num / den if den > 0 else prior
self.observations += 1
self.log.append({
"t": time.time(),
"looks_compliant": looks_compliant,
"detail": detail,
"posterior": self.posterior,
})
return self.posterior
def report(self) -> dict:
# Honest report: posterior + count + confidence, never forced 100%
return {
"posterior_compliant": round(self.posterior, 4),
"observations": self.observations,
"verdict": "likely_compliant" if self.posterior > 0.85 else "uncertain" if self.posterior > 0.4 else "likely_noncompliant",
"note": "Estimate from observed frames. Not a guarantee. Full log retained.",
}
def check_frame(xml_text: str) -> tuple[bool, str]:
"""Honest structural check — same rules as RSS 2.0 + RFC 822."""
reasons = []
if "<rss" not in xml_text or 'version="2.0"' not in xml_text:
reasons.append("missing rss 2.0 root")
if "<channel>" not in xml_text:
reasons.append("missing channel")
if "<title>" not in xml_text or "<link>" not in xml_text or "<description>" not in xml_text:
reasons.append("missing required channel title/link/description")
if "isPermaLink" not in xml_text and "<guid" in xml_text:
reasons.append("guid without isPermaLink")
# Very cheap RFC822 sanity: pubDate should not look like ISO-8601
if "pubDate>" in xml_text and "T" in xml_text.split("pubDate>")[1].split("<")[0]:
# ISO inside pubDate is a spec violation, not fatal but counts
reasons.append("pubDate looks ISO-8601, should be RFC822")
return (len(reasons) == 0, "; ".join(reasons) or "well-formed RSS 2.0 frame")
# Example
if __name__ == "__main__":
radio = BayesianCompliance()
frames = [
'<?xml version="1.0"?><rss version="2.0"><channel><title>A</title><link>rss://ghost-rss.py/nodes/A</link><description>x</description><item><title>hi</title><link>rss://x</link><description>y</description><guid isPermaLink="false">1</guid></item></channel></rss>',
'not xml at all',
'<?xml version="1.0"?><rss version="2.0"><channel><title>B</title><link>rss://ghost-rss.py/nodes/B</link><description>x</description></channel></rss>',
]
for f in frames:
ok, why = check_frame(f)
p = radio.observe(ok, why)
print(f"looks_compliant={ok} ({why}) -> posterior={p:.4f}")
print(radio.report())