"""Materialized objects: evidence, claims, capabilities, inference records. These are projections of the signed operation log. They are never persisted independently — the log is the only durable artifact — so every field here is recomputable from the log on any conforming implementation. """ from __future__ import annotations from dataclasses import dataclass, field from typing import Any, Dict, List, Optional # Claim lifecycle statuses, in precedence order (later entries override # earlier ones when multiple conditions apply). STATUS_ACTIVE = "active" STATUS_INVALIDATED = "invalidated" # an upstream input was refuted/corrected STATUS_CORRECTED = "corrected" # user replaced it with a correction STATUS_REFUTED = "refuted" # user explicitly refuted it STATUS_ORDER = (STATUS_ACTIVE, STATUS_INVALIDATED, STATUS_CORRECTED, STATUS_REFUTED) @dataclass class Evidence: """A piece of raw, imported evidence. Evidence is immutable ground truth about *what was observed*, never about whether it is correct.""" id: str op_id: str source: str # adapter name, e.g. "calendar.ics" kind: str # e.g. "calendar_event", "note", "photo_meta" content: Dict[str, Any] observed_at: str tags: List[str] author: str ts: str def to_dict(self) -> Dict[str, Any]: return { "id": self.id, "op_id": self.op_id, "source": self.source, "kind": self.kind, "content": self.content, "observed_at": self.observed_at, "tags": list(self.tags), "author": self.author, "ts": self.ts, } def summary(self, width: int = 72) -> str: """One-line human summary of the evidence content.""" c = self.content if self.kind == "calendar_event": text = f"{c.get('start', '?')} {c.get('title', '(untitled event)')}" loc = c.get("location") if loc: text += f" @ {loc}" elif self.kind == "note": text = c.get("title") or (c.get("text", "") or "")[:width] elif self.kind == "photo_meta": text = ( f"photo {c.get('taken_at', '?')}" f" near {c.get('place_label') or c.get('lat')},{c.get('lon')}" ) else: text = str(c) text = " ".join(text.split()) return text if len(text) <= width else text[: width - 1] + "…" @dataclass class Claim: """A derived statement about the subject, with provenance and lifecycle.""" id: str op_id: str subject: str topic: str statement: str value: Any confidence: float method: str inputs: List[str] # ids of claims and/or evidence params: Dict[str, Any] author: str ts: str status: str = STATUS_ACTIVE refuted_by: Optional[str] = None # op_id of the claim.refute refute_reason: Optional[str] = None corrected_by: Optional[str] = None # claim id of the replacement invalidated_by: Optional[str] = None # op_id of the root-cause operation invalidated_via: Optional[str] = None # claim id of the direct upstream cause corrects: Optional[str] = None # claim id this claim replaces @property def is_active(self) -> bool: return self.status == STATUS_ACTIVE def to_dict(self) -> Dict[str, Any]: return { "id": self.id, "op_id": self.op_id, "subject": self.subject, "topic": self.topic, "statement": self.statement, "value": self.value, "confidence": self.confidence, "method": self.method, "inputs": list(self.inputs), "params": dict(self.params), "author": self.author, "ts": self.ts, "status": self.status, "refuted_by": self.refuted_by, "refute_reason": self.refute_reason, "corrected_by": self.corrected_by, "invalidated_by": self.invalidated_by, "invalidated_via": self.invalidated_via, "corrects": self.corrects, } @dataclass class Capability: """A capability grant: a named grantee may receive claims under the listed topic prefixes. Evidence is never shareable through a capability; only claims (and their explanations) are.""" id: str op_id: str grantee: str topics: List[str] expires_at: Optional[str] note: Optional[str] author: str ts: str status: str = "active" # active | revoked revoked_by: Optional[str] = None revoke_reason: Optional[str] = None def covers_topic(self, topic: str) -> bool: for prefix in self.topics: if topic == prefix or topic.startswith(prefix + "."): return True return False def to_dict(self) -> Dict[str, Any]: return { "id": self.id, "op_id": self.op_id, "grantee": self.grantee, "topics": list(self.topics), "expires_at": self.expires_at, "note": self.note, "author": self.author, "ts": self.ts, "status": self.status, "revoked_by": self.revoked_by, "revoke_reason": self.revoke_reason, } @dataclass class InferenceRecord: """An audit record of a model call made over the user's data.""" op_id: str model: str purpose: str input_ids: List[str] output_ids: List[str] = field(default_factory=list) prompt_hash: Optional[str] = None author: str = "" ts: str = "" def to_dict(self) -> Dict[str, Any]: return { "op_id": self.op_id, "model": self.model, "purpose": self.purpose, "input_ids": list(self.input_ids), "output_ids": list(self.output_ids), "prompt_hash": self.prompt_hash, "author": self.author, "ts": self.ts, }