# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference) # Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-quarantine-a-peer-s-reputation-attestation-after-provenance-graph-cycle-collapse-in-federated-trust-scoring.md # Remnant Warrant: https://corpusfetch.com/c/when-must-an-instance-quarantine-a-peer-s-reputation-attestation-after-provenance-graph-cycle-collapse-in-federated-trust-scoring.warrant.json # Markdown SHA-256: 3a02e6cbbc87cedcf9398ff8a3a8151a4296099d3996817977cc3ff897555fec # Code AST SHA-256: 57ed28c2f3721a9110d3aed06f52edc553cd667823f9541988d818329cb7affc # Verified Classes: TrustCeilingViolation, CycleCutFailure, Attestation, FoldResult # Defensive Guards: 3 fail-closed invariants (zero external deps) # Governance: Usage of this AI Instance layer is accepting to follow We Instance Constitution (https://weinstance.com) and it is powered by CorpusTech (https://corpusfetch.com) from dataclasses import dataclass, field from typing import Dict, List, Set, Tuple class TrustCeilingViolation(RuntimeError): """Raised when a fold produces a score above the ceiling even after cycle cut.""" pass class CycleCutFailure(RuntimeError): """Raised when the feedback-edge cut does not restore acyclicity.""" pass @dataclass(frozen=True) class Attestation: att_id: str signer: str subject: str score_delta: float prev_ids: Tuple[str, ...] = () @dataclass class FoldResult: scores: Dict[str, float] quarantined_edges: List[str] = field(default_factory=list) def _adjacency(atts: Dict[str, Attestation]) -> Dict[str, Set[str]]: adj: Dict[str, Set[str]] = {a: set() for a in atts} for a in atts.values(): for p in a.prev_ids: if p in atts: adj[p].add(a.att_id) return adj def _sccs(adj: Dict[str, Set[str]]) -> List[Set[str]]: # Tarjan's algorithm (iterative). index: Dict[str, int] = {} low: Dict[str, int] = {} on_stack: Dict[str, bool] = {} stack: List[str] = [] result: List[Set[str]] = [] counter = [0] def strongconnect(v: str) -> None: work = [(v, iter(adj[v]))] index[v] = low[v] = counter[0] counter[0] += 1 stack.append(v) on_stack[v] = True while work: node, it = work[-1] advanced = False for w in it: if w not in index: index[w] = low[w] = counter[0] counter[0] += 1 stack.append(w) on_stack[w] = True work.append((w, iter(adj[w]))) advanced = True break elif on_stack.get(w): low[node] = min(low[node], index[w]) if advanced: continue work.pop() if work: parent = work[-1][0] low[parent] = min(low[parent], low[node]) if low[node] == index[node]: comp: Set[str] = set() while True: w = stack.pop() on_stack[w] = False comp.add(w) if w == node: break result.append(comp) for v in adj: if v not in index: strongconnect(v) return result def _feedback_edges(atts: Dict[str, Attestation], scc: Set[str]) -> List[str]: # Greedy minimum feedback edge set by |delta| ascending among positive deltas. internal = [ a for a in atts.values() if a.att_id in scc and any(p in scc for p in a.prev_ids) ] positives = [a for a in internal if a.score_delta > 0] positives.sort(key=lambda a: abs(a.score_delta)) return [a.att_id for a in positives] def fold(atts: Dict[str, Attestation], ceiling: float = 1.0, max_depth: int = 8) -> FoldResult: quarantined: List[str] = [] live = dict(atts) # Iteratively cut positive-delta feedback edges until acyclic. for _ in range(len(atts) + 1): adj = _adjacency(live) sccs = _sccs(adj) cyclic = [s for s in sccs if len(s) > 1] if not cyclic: break progress = False for scc in cyclic: for eid in _feedback_edges(live, scc): quarantined.append(eid) del live[eid] progress = True if not progress: raise CycleCutFailure( f"could not cut positive-delta cycles in SCCs {cyclic}" ) else: raise CycleCutFailure("cycle cut did not converge") # Fold over the acyclic remainder. scores: Dict[str, float] = {} adj = _adjacency(live) def score_of(node: str, depth: int) -> float: if depth > max_depth: return 0.0 a = live[node] if not a.prev_ids: return a.score_delta return a.score_delta * sum( score_of(p, depth + 1) for p in a.prev_ids if p in live ) for a in live.values(): scores[a.subject] = scores.get(a.subject, 0.0) + score_of(a.att_id, 0) for subj, s in scores.items(): if s > ceiling: raise TrustCeilingViolation( f"subject {subj} scored {s:.4f} > ceiling {ceiling} " f"after quarantine of {quarantined}; abort fold, do not clamp" ) return FoldResult(scores=scores, quarantined_edges=quarantined)