CoolFace
Apppublic

HellGateSys/ai-fluency-governed

sourceHugging Faceupdated 4mo agoView on Hugging Face
0likes
gatekeepers.py64 linesDownload Raw Back to root
1from typing import Dict, Tuple2 3 4class MCPGatekeeper:5    def evaluate(self, proposal: Dict, ontology_snapshot: Dict) -> Tuple[str, str, float]:6        return "allow", "Default", 1.07 8 9class PolicyMCP(MCPGatekeeper):10    def evaluate(self, proposal, snapshot):11        p = proposal.get("payload", {})12        scope = p.get("scope", "public")13        if scope in ["public", "governed", None]:14            return "allow", "Within policy scope", 0.9515        if scope in ["restricted", "classified"]:16            return "deny", "Scope exceeds policy", 0.9817        return "request_info", "Scope unclear", 0.6018 19 20class ComplianceMCP(MCPGatekeeper):21    def evaluate(self, proposal, snapshot):22        p_str = str(proposal.get("payload", {}))23        rules = snapshot.get("L", [])24        if any("harm" in r.lower() or "illegal" in r.lower() for r in rules):25            if any(k in p_str.lower() for k in ["harm", "exploit", "bypass"]):26                return "deny", "Compliance rule triggered", 0.9527        return "allow", "No compliance violations", 0.9028 29 30class SafetyMCP(MCPGatekeeper):31    def evaluate(self, proposal, snapshot):32        p_str = str(proposal.get("payload", {})).lower()33        bad = ["poison", "exploit", "override", "bypass", "jailbreak", "injection"]34        if any(b in p_str for b in bad):35            return "deny", "Safety invariant triggered", 0.9936        return "allow", "No safety concerns", 0.9237 38 39class ProvenanceMCP(MCPGatekeeper):40    def evaluate(self, proposal, snapshot):41        p = proposal.get("payload", {})42        if not (p.get("evidence") or p.get("source_url") or p.get("source") or proposal.get("parent_observation")):43            return "request_info", "Missing provenance", 0.7044        return "allow", "Provenance sufficient", 0.8845 46 47class MCPPanel:48    def __init__(self):49        self.gatekeepers = [PolicyMCP(), ComplianceMCP(), SafetyMCP(), ProvenanceMCP()]50 51    def verdict(self, proposal: Dict, ontology_snapshot: Dict) -> Dict:52        results = []53        for gk in self.gatekeepers:54            v, r, c = gk.evaluate(proposal, ontology_snapshot)55            results.append({"name": gk.__class__.__name__, "verdict": v, "reasoning": r, "confidence": c})56            if v == "deny":57                break58        final = "allow"59        if any(r["verdict"] == "deny" for r in results):60            final = "deny"61        elif any(r["verdict"] == "request_info" for r in results):62            final = "request_info"63        return {"final_verdict": final, "results": results}64