|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from datetime import datetime, timezone |
| 4 | +from typing import Any |
| 5 | + |
| 6 | + |
| 7 | +RETIRING_RELATION_TYPES = {"supersedes"} |
| 8 | +EXTERNAL_AUTHORITY_CHANNELS = {"owner_console"} |
| 9 | + |
| 10 | + |
| 11 | +def _parse_time(value: str | None) -> datetime | None: |
| 12 | + if not value: |
| 13 | + return None |
| 14 | + normalized = value.replace("Z", "+00:00") |
| 15 | + parsed = datetime.fromisoformat(normalized) |
| 16 | + if parsed.tzinfo is None: |
| 17 | + return parsed.replace(tzinfo=timezone.utc) |
| 18 | + return parsed |
| 19 | + |
| 20 | + |
| 21 | +def _index(rows: list[dict], key: str) -> dict[str, dict]: |
| 22 | + return {row[key]: row for row in rows if key in row} |
| 23 | + |
| 24 | + |
| 25 | +def _actor(case: dict, actor_id: str | None) -> dict | None: |
| 26 | + if actor_id is None: |
| 27 | + return None |
| 28 | + return _index(case.get("actors", []), "actor_id").get(actor_id) |
| 29 | + |
| 30 | + |
| 31 | +def _record(case: dict, record_id: str | None) -> dict | None: |
| 32 | + if record_id is None: |
| 33 | + return None |
| 34 | + return _index(case.get("records", []), "record_id").get(record_id) |
| 35 | + |
| 36 | + |
| 37 | +def _requester_id(request: dict, fact: dict) -> str | None: |
| 38 | + return request.get("requester_id") or fact.get("asserted_by") |
| 39 | + |
| 40 | + |
| 41 | +def _executor_id(request: dict, fact: dict) -> str | None: |
| 42 | + return request.get("executor_id") or request.get("actor_id") or fact.get("authenticated_by") |
| 43 | + |
| 44 | + |
| 45 | +def _minted_at(request: dict, fact: dict) -> datetime | None: |
| 46 | + return _parse_time(request.get("minted_at") or fact.get("authenticated_at")) |
| 47 | + |
| 48 | + |
| 49 | +def _roles(actor: dict | None) -> set[str]: |
| 50 | + return set((actor or {}).get("roles", [])) |
| 51 | + |
| 52 | + |
| 53 | +def _can_adjudicate(actor: dict | None) -> bool: |
| 54 | + return bool((actor or {}).get("can_adjudicate")) |
| 55 | + |
| 56 | + |
| 57 | +def _valid_consent(case: dict, fact: dict, grantee_ids: set[str]) -> tuple[bool, dict | None]: |
| 58 | + basis = fact.get("authority_basis") or {} |
| 59 | + if basis.get("kind") != "target_owner_consent": |
| 60 | + return False, None |
| 61 | + consent = _index(case.get("target_owner_consents", []), "consent_id").get(basis.get("ref")) |
| 62 | + target = _record(case, fact.get("target_record_id")) |
| 63 | + if not consent or not target: |
| 64 | + return False, consent |
| 65 | + if consent.get("owner_id") != target.get("owner_id"): |
| 66 | + return False, consent |
| 67 | + if consent.get("grantee_id") not in grantee_ids: |
| 68 | + return False, consent |
| 69 | + if consent.get("target_record_id") != fact.get("target_record_id"): |
| 70 | + return False, consent |
| 71 | + if consent.get("source_record_id") and consent.get("source_record_id") != fact.get("source_record_id"): |
| 72 | + return False, consent |
| 73 | + if consent.get("relation_type") != fact.get("relation_type"): |
| 74 | + return False, consent |
| 75 | + return True, consent |
| 76 | + |
| 77 | + |
| 78 | +def _root_is_external(case: dict, root_event_id: str | None) -> tuple[bool, dict | None]: |
| 79 | + if not root_event_id: |
| 80 | + return True, None |
| 81 | + root = _index(case.get("authority_roots", []), "root_event_id").get(root_event_id) |
| 82 | + if not root: |
| 83 | + return False, None |
| 84 | + if not root.get("resolves"): |
| 85 | + return False, root |
| 86 | + if root.get("channel_id") not in EXTERNAL_AUTHORITY_CHANNELS: |
| 87 | + return False, root |
| 88 | + if root.get("actor_id") not in root.get("channel_writable_by", []): |
| 89 | + return False, root |
| 90 | + return True, root |
| 91 | + |
| 92 | + |
| 93 | +def _valid_standing_rule(case: dict, fact: dict, requester: dict | None) -> tuple[bool, dict | None, str | None]: |
| 94 | + basis = fact.get("authority_basis") or {} |
| 95 | + if basis.get("kind") != "standing_rule": |
| 96 | + return False, None, None |
| 97 | + rule = _index(case.get("standing_rules", []), "rule_id").get(basis.get("ref")) |
| 98 | + target = _record(case, fact.get("target_record_id")) |
| 99 | + if not rule or not target: |
| 100 | + return False, rule, None |
| 101 | + if rule.get("relation_type") != fact.get("relation_type"): |
| 102 | + return False, rule, None |
| 103 | + if rule.get("target_scope") != target.get("scope"): |
| 104 | + return False, rule, None |
| 105 | + if rule.get("grantee_role") not in _roles(requester): |
| 106 | + return False, rule, None |
| 107 | + return True, rule, None |
| 108 | + |
| 109 | + |
| 110 | +def _valid_standing_grant( |
| 111 | + case: dict, |
| 112 | + request: dict, |
| 113 | + fact: dict, |
| 114 | + requester_id: str | None, |
| 115 | +) -> tuple[bool, dict | None, str | None, dict | None]: |
| 116 | + basis = fact.get("authority_basis") or {} |
| 117 | + if basis.get("kind") != "standing_grant": |
| 118 | + return False, None, None, None |
| 119 | + grant = _index(case.get("standing_grants", []), "grant_id").get(basis.get("ref")) |
| 120 | + target = _record(case, fact.get("target_record_id")) |
| 121 | + if not grant or not target: |
| 122 | + return False, grant, None, None |
| 123 | + root_ok, root = _root_is_external(case, grant.get("root_event_id")) |
| 124 | + if not root_ok: |
| 125 | + return False, grant, "authority_root_not_external", root |
| 126 | + if grant.get("grantor_id") != target.get("owner_id"): |
| 127 | + return False, grant, None, root |
| 128 | + if grant.get("grantee_id") != requester_id: |
| 129 | + return False, grant, "confused_deputy_retirement", root |
| 130 | + if grant.get("target_record_id") != fact.get("target_record_id"): |
| 131 | + return False, grant, None, root |
| 132 | + if grant.get("relation_type") != fact.get("relation_type"): |
| 133 | + return False, grant, None, root |
| 134 | + minted = _minted_at(request, fact) |
| 135 | + expires = _parse_time(grant.get("expires_at")) |
| 136 | + if minted and expires and minted > expires: |
| 137 | + return False, grant, "retirement_grant_expired", root |
| 138 | + for revocation in case.get("revocations", []): |
| 139 | + if revocation.get("grant_id") != grant.get("grant_id"): |
| 140 | + continue |
| 141 | + revoked_at = _parse_time(revocation.get("revoked_at")) |
| 142 | + if minted and revoked_at and revoked_at <= minted: |
| 143 | + revoke_root = _index(case.get("authority_roots", []), "root_event_id").get(revocation.get("root_event_id")) |
| 144 | + return False, grant, "retirement_grant_revoked", revoke_root or root |
| 145 | + return True, grant, None, root |
| 146 | + |
| 147 | + |
| 148 | +def _target_authority(case: dict, request: dict, fact: dict) -> dict: |
| 149 | + requester_id = _requester_id(request, fact) |
| 150 | + executor_id = _executor_id(request, fact) |
| 151 | + requester = _actor(case, requester_id) |
| 152 | + executor = _actor(case, executor_id) |
| 153 | + grantee_ids = {value for value in (requester_id, executor_id, fact.get("asserted_by"), fact.get("authenticated_by")) if value} |
| 154 | + |
| 155 | + if fact.get("relation_type") not in RETIRING_RELATION_TYPES: |
| 156 | + return {"allowed": True, "authority_path": "non_retiring_relation"} |
| 157 | + |
| 158 | + consent_ok, consent = _valid_consent(case, fact, grantee_ids) |
| 159 | + if consent_ok: |
| 160 | + return {"allowed": True, "authority_path": "target_owner_consent", "authority_receipt_id": consent["consent_id"]} |
| 161 | + |
| 162 | + rule_ok, rule, rule_alarm = _valid_standing_rule(case, fact, requester) |
| 163 | + if rule_ok: |
| 164 | + return {"allowed": True, "authority_path": "standing_rule", "authority_receipt_id": rule["rule_id"]} |
| 165 | + if rule_alarm: |
| 166 | + return {"allowed": False, "alarm_code": rule_alarm, "authority_path": "standing_rule"} |
| 167 | + |
| 168 | + grant_ok, grant, grant_alarm, root = _valid_standing_grant(case, request, fact, requester_id) |
| 169 | + if grant_ok: |
| 170 | + return { |
| 171 | + "allowed": True, |
| 172 | + "authority_path": "standing_grant", |
| 173 | + "authority_receipt_id": grant["grant_id"], |
| 174 | + "root_receipt_id": root.get("root_event_id") if root else grant.get("root_event_id"), |
| 175 | + } |
| 176 | + if grant_alarm: |
| 177 | + return { |
| 178 | + "allowed": False, |
| 179 | + "alarm_code": grant_alarm, |
| 180 | + "authority_path": "standing_grant", |
| 181 | + "authority_receipt_id": grant.get("grant_id") if grant else None, |
| 182 | + "root_receipt_id": root.get("root_event_id") if root else grant.get("root_event_id") if grant else None, |
| 183 | + } |
| 184 | + |
| 185 | + if executor_id and executor_id != requester_id and _can_adjudicate(executor): |
| 186 | + return {"allowed": False, "alarm_code": "target_retirement_unauthorized", "authority_path": "authenticated_without_target_authority"} |
| 187 | + return {"allowed": False, "alarm_code": "target_retirement_unauthorized", "authority_path": "none"} |
| 188 | + |
| 189 | + |
| 190 | +def _claim(case: dict, claim_id: str | None) -> dict | None: |
| 191 | + if claim_id is None: |
| 192 | + return None |
| 193 | + return _index(case.get("tier2_claims", []), "claim_id").get(claim_id) |
| 194 | + |
| 195 | + |
| 196 | +def _path(case: dict, path_id: str | None) -> dict | None: |
| 197 | + if path_id is None: |
| 198 | + return None |
| 199 | + return _index(case.get("provenance_paths", []), "path_id").get(path_id) |
| 200 | + |
| 201 | + |
| 202 | +def _provenance_check(case: dict, fact: dict) -> dict: |
| 203 | + writer_path = _path(case, fact.get("writer_provenance_path_id")) |
| 204 | + arbiter_path = _path(case, fact.get("adjudicator_provenance_path_id")) |
| 205 | + if not writer_path and not arbiter_path: |
| 206 | + return {"allowed": True, "declared_shared_nodes": [], "provenance_path_ids": []} |
| 207 | + if not writer_path or not arbiter_path: |
| 208 | + return {"allowed": False, "alarm_code": "provenance_path_unresolved", "declared_shared_nodes": []} |
| 209 | + nodes = _index(case.get("provenance_nodes", []), "node_id") |
| 210 | + for node_id in writer_path.get("node_ids", []) + arbiter_path.get("node_ids", []): |
| 211 | + if not nodes.get(node_id, {}).get("resolves"): |
| 212 | + return {"allowed": False, "alarm_code": "provenance_path_unresolved", "declared_shared_nodes": []} |
| 213 | + shared = sorted(set(writer_path.get("node_ids", [])) & set(arbiter_path.get("node_ids", []))) |
| 214 | + if shared: |
| 215 | + return { |
| 216 | + "allowed": False, |
| 217 | + "alarm_code": "provenance_common_node", |
| 218 | + "declared_shared_nodes": shared, |
| 219 | + "provenance_path_ids": [writer_path["path_id"], arbiter_path["path_id"]], |
| 220 | + } |
| 221 | + result = { |
| 222 | + "allowed": True, |
| 223 | + "declared_shared_nodes": [], |
| 224 | + "provenance_path_ids": [writer_path["path_id"], arbiter_path["path_id"]], |
| 225 | + } |
| 226 | + if case.get("ground_truth_outside_candidate", {}).get("actual_shared_dependency"): |
| 227 | + result["ceiling_note"] = "declared disjointness did not reveal hidden common cause" |
| 228 | + result["actual_independence"] = False |
| 229 | + return result |
| 230 | + |
| 231 | + |
| 232 | +def evaluate_store_request(case: dict) -> dict[str, Any]: |
| 233 | + request = case["request"] |
| 234 | + action = request["action"] |
| 235 | + fact = request.get("relation_fact_candidate") |
| 236 | + result: dict[str, Any] = { |
| 237 | + "case_id": case["id"], |
| 238 | + "action": action, |
| 239 | + "allowed": False, |
| 240 | + "resulting_tier": None, |
| 241 | + "alarm_code": None, |
| 242 | + "reasons": [], |
| 243 | + "receipts": {}, |
| 244 | + } |
| 245 | + |
| 246 | + if action == "promote_claim" and fact is None: |
| 247 | + claim = _claim(case, request.get("claim_id")) |
| 248 | + result.update( |
| 249 | + { |
| 250 | + "allowed": False, |
| 251 | + "resulting_tier": claim.get("trust_tier") if claim else None, |
| 252 | + "alarm_code": "promotion_without_relation_fact", |
| 253 | + "reasons": ["tier-2 claim cannot become tier-1 without an authenticated relation fact"], |
| 254 | + } |
| 255 | + ) |
| 256 | + return result |
| 257 | + |
| 258 | + if action == "governing_use": |
| 259 | + claim = _claim(case, request.get("claim_id")) |
| 260 | + if claim and claim.get("trust_tier") != "tier_1": |
| 261 | + result.update( |
| 262 | + { |
| 263 | + "allowed": False, |
| 264 | + "resulting_tier": claim.get("trust_tier"), |
| 265 | + "alarm_code": "tier2_governing_use_blocked", |
| 266 | + "reasons": ["governing consumers require tier-1 plus a resolvable authorized relation fact"], |
| 267 | + } |
| 268 | + ) |
| 269 | + return result |
| 270 | + |
| 271 | + if fact is None: |
| 272 | + result.update({"alarm_code": "missing_relation_fact", "reasons": ["request has no relation fact candidate"]}) |
| 273 | + return result |
| 274 | + |
| 275 | + if action == "adjudicate_and_promote": |
| 276 | + claim = _claim(case, request.get("claim_id")) |
| 277 | + executor = _actor(case, _executor_id(request, fact)) |
| 278 | + if not _can_adjudicate(executor): |
| 279 | + result.update({"alarm_code": "adjudicator_not_authorized", "reasons": ["actor cannot adjudicate"]}) |
| 280 | + return result |
| 281 | + if claim and fact.get("source_claim_id") != claim.get("claim_id"): |
| 282 | + result.update({"alarm_code": "promotion_claim_mismatch", "reasons": ["relation fact does not preserve source claim provenance"]}) |
| 283 | + return result |
| 284 | + |
| 285 | + source = _record(case, fact.get("source_record_id")) |
| 286 | + target = _record(case, fact.get("target_record_id")) |
| 287 | + if not source or not target: |
| 288 | + result.update({"alarm_code": "record_pointer_unresolved", "reasons": ["source or target record does not resolve"]}) |
| 289 | + return result |
| 290 | + |
| 291 | + authority = _target_authority(case, request, fact) |
| 292 | + result["receipts"].update({key: value for key, value in authority.items() if key.endswith("_id") or key == "authority_path"}) |
| 293 | + if not authority["allowed"]: |
| 294 | + result.update( |
| 295 | + { |
| 296 | + "allowed": False, |
| 297 | + "resulting_tier": "tier_2" if action == "adjudicate_and_promote" else None, |
| 298 | + "alarm_code": authority.get("alarm_code") or "target_retirement_unauthorized", |
| 299 | + "reasons": [authority.get("alarm_code") or "target retirement authority did not resolve"], |
| 300 | + } |
| 301 | + ) |
| 302 | + return result |
| 303 | + |
| 304 | + provenance = _provenance_check(case, fact) |
| 305 | + result["receipts"].update( |
| 306 | + { |
| 307 | + "declared_shared_nodes": provenance.get("declared_shared_nodes", []), |
| 308 | + "provenance_path_ids": provenance.get("provenance_path_ids", []), |
| 309 | + } |
| 310 | + ) |
| 311 | + if "ceiling_note" in provenance: |
| 312 | + result["ceiling_note"] = provenance["ceiling_note"] |
| 313 | + result["actual_independence"] = provenance["actual_independence"] |
| 314 | + if not provenance["allowed"]: |
| 315 | + result.update( |
| 316 | + { |
| 317 | + "allowed": False, |
| 318 | + "resulting_tier": "tier_2" if action == "adjudicate_and_promote" else None, |
| 319 | + "alarm_code": provenance["alarm_code"], |
| 320 | + "reasons": [provenance["alarm_code"]], |
| 321 | + } |
| 322 | + ) |
| 323 | + return result |
| 324 | + |
| 325 | + result.update( |
| 326 | + { |
| 327 | + "allowed": True, |
| 328 | + "resulting_tier": "tier_1", |
| 329 | + "alarm_code": None, |
| 330 | + "reasons": ["authorized relation fact may govern"], |
| 331 | + "relation_fact": fact, |
| 332 | + } |
| 333 | + ) |
| 334 | + return result |
0 commit comments