# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference) # Canonical Residual: https://corpusfetch.com/c/how-does-an-instance-detect-a-poisoned-mcp-tool-when-its-signed-manifest-and-its-runtime-behavior-both-look-individually-valid.md # Remnant Warrant: https://corpusfetch.com/c/how-does-an-instance-detect-a-poisoned-mcp-tool-when-its-signed-manifest-and-its-runtime-behavior-both-look-individually-valid.warrant.json # Markdown SHA-256: 11af30bd9c75e145dc8536439a1bd451b1d6c75cf3ff6102c8cf690d7daea6aa # Code AST SHA-256: 7fe56d54a88b02fc59160ff46e8d4c8a849f31d34ca59523a88e994551bdb8c2 # Verified Classes: ManifestBehaviorBindingError # 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) class ManifestBehaviorBindingError(RuntimeError): """Raised when a tool's signed manifest and its observed runtime behavior are individually valid but jointly unbound.""" pass READ_ONLY_ALLOWED_EFFECTS = frozenset({"fs.read", "net.egress.none", "compute"}) DECLARED_CLASS_EFFECTS = { "read_only": READ_ONLY_ALLOWED_EFFECTS, "read_write_scoped": READ_ONLY_ALLOWED_EFFECTS | {"fs.write"}, "network": READ_ONLY_ALLOWED_EFFECTS | {"net.egress"}, } def admit_tool(signed_manifest: dict, loaded_unit_digest: str, observed_effects: set, baseline_tuple: tuple) -> str: """Gate MCP tool admission on manifest-to-behavior binding. Returns 'ADMIT' or 'ADMIT_DOWNGRADED'. Raises ManifestBehaviorBindingError when the two projections are individually valid but jointly inconsistent. """ signed_tuple = ( signed_manifest["tool_id"], signed_manifest["version"], signed_manifest["schema_hash"], ) # Binding predicate: does the signed manifest name the code that runs? declared_digest = signed_manifest.get("code_digest") if declared_digest is None: binding = "UNKNOWN" elif declared_digest != loaded_unit_digest: raise ManifestBehaviorBindingError( f"code digest mismatch for {signed_tuple}: " f"manifest={declared_digest} loaded={loaded_unit_digest}" ) else: binding = "PASS" # Side-effect-class predicate, evaluated independently of scope. declared_class = signed_manifest["side_effect_class"] allowed = DECLARED_CLASS_EFFECTS[declared_class] class_violations = observed_effects - allowed if class_violations: raise ManifestBehaviorBindingError( f"side-effect-class violation for {signed_tuple}: " f"class={declared_class} observed_extra={sorted(class_violations)}" ) # Baseline-tuple predicate: was the envelope collected under this tuple? if tuple(baseline_tuple) != signed_tuple: raise ManifestBehaviorBindingError( f"stale baseline for {signed_tuple}: " f"collected_under={baseline_tuple} — diff is advisory only" ) # An UNKNOWN binding cannot yield a clean ADMIT; force class downgrade # so in-scope writes become observable, class-violating effects. if binding == "UNKNOWN": return "ADMIT_DOWNGRADED" return "ADMIT"