# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference) # Canonical Residual: https://corpusfetch.com/c/which-invariants-prevent-skill-injection-drift-when-signed-tool-manifests-disagree-with-runtime-behavioral-attestation-at-the-tool-call-boundary.md # Remnant Warrant: https://corpusfetch.com/c/which-invariants-prevent-skill-injection-drift-when-signed-tool-manifests-disagree-with-runtime-behavioral-attestation-at-the-tool-call-boundary.warrant.json # Markdown SHA-256: f54aa14c305e54b9ecddc59fb35ca182d215df30fb5950d7b327925fbb86dd04 # Code AST SHA-256: 67e5cd10541f5177e10ecb74692634427f98bc6ee83eb8d799a9064b27567705 # Verified Classes: SideEffect, ToolTrustError, SignedManifest, AttestationWindow, SessionTrust # Defensive Guards: 6 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 enum import Enum from typing import FrozenSet class SideEffect(str, Enum): READ_ONLY = "read_only" NETWORK_EGRESS = "network_egress" FILESYSTEM_WRITE = "filesystem_write" PROCESS_SPAWN = "process_spawn" class ToolTrustError(RuntimeError): """Raised when manifest trust and runtime attestation cannot be reconciled.""" @dataclass(frozen=True) class SignedManifest: tool_id: str declared_effects: FrozenSet[SideEffect] valid_until_epoch: int signature_ok: bool @dataclass class AttestationWindow: tool_id: str observed_effects: FrozenSet[SideEffect] window_close_epoch: int attributed_trace_ids: FrozenSet[str] @dataclass class SessionTrust: demoted: set = field(default_factory=set) def admit_tool_call( manifest: SignedManifest, attestation: AttestationWindow, session: SessionTrust, now_epoch: int, trace_id: str, attestation_max_age: int = 3600, ) -> bool: if manifest.tool_id != attestation.tool_id: raise ToolTrustError( f"attestation for {attestation.tool_id} cannot gate manifest {manifest.tool_id}" ) if not manifest.signature_ok: raise ToolTrustError(f"manifest signature invalid for {manifest.tool_id}") if now_epoch > manifest.valid_until_epoch: raise ToolTrustError(f"manifest for {manifest.tool_id} expired at {manifest.valid_until_epoch}") if now_epoch - attestation.window_close_epoch > attestation_max_age: raise ToolTrustError( f"attestation for {manifest.tool_id} stale: " f"age {now_epoch - attestation.window_close_epoch}s > {attestation_max_age}s" ) if trace_id not in attestation.attributed_trace_ids: # Attribution by call-frame lineage is a precondition; timestamp # proximity is not sufficient and must not be substituted. raise ToolTrustError( f"trace {trace_id} not attributable to {manifest.tool_id} attestation" ) divergence = attestation.observed_effects - manifest.declared_effects if divergence: # Sticky demotion: a later clean window does NOT re-admit within session. session.demoted.add(manifest.tool_id) raise ToolTrustError( f"skill-injection divergence on {manifest.tool_id}: " f"observed {sorted(e.value for e in divergence)} " f"not in declared {sorted(e.value for e in manifest.declared_effects)}; " f"tool demoted to APPROVAL_GATED for session" ) if manifest.tool_id in session.demoted: # Clean attestation does not lift a prior demotion in the same session. return False return True