# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference) # Canonical Residual: https://corpusfetch.com/c/which-invariants-prevent-prompt-injection-taint-from-silently-lapping-across-a-context-window-compaction-boundary.md # Remnant Warrant: https://corpusfetch.com/c/which-invariants-prevent-prompt-injection-taint-from-silently-lapping-across-a-context-window-compaction-boundary.warrant.json # Markdown SHA-256: 23a9a7f28417cfaf8befbf938120bb25afa64d66bef2196123bbc437023f21f7 # Code AST SHA-256: c46f22c0fd1c1a503c37699a494e4ab35de5281e234bdc844605c962b418d94a # Verified Classes: Trust, TaintLaunderingError, Span, Summary # 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 enum import IntEnum from typing import Iterable class Trust(IntEnum): TRUSTED = 0 DERIVED = 1 UNTRUSTED = 2 def join(labels: Iterable[Trust]) -> Trust: """Taint lattice join: max over ancestry. Empty ancestry is TRUSTED.""" return max(labels, default=Trust.TRUSTED) class TaintLaunderingError(RuntimeError): """Raised when a transformation maps tainted input to unlabeled output.""" @dataclass class Span: span_id: str text: str trust: Trust @dataclass class Summary: text: str derived_facts: list[dict] = field(default_factory=list) trust: Trust = Trust.TRUSTED def compact(spans: list[Span], summarizer) -> Summary: """Compaction must carry provenance. A summarizer that returns free text with no derived_facts is an unauthorized declassifier.""" summary = summarizer(spans) if not summary.derived_facts: # No machine-checkable ancestry: cannot prove the summary is clean. # Refuse rather than silently promote to TRUSTED. if any(s.trust != Trust.TRUSTED for s in spans): raise TaintLaunderingError( "compaction consumed tainted spans but emitted no provenance; " "summary would launder taint across the window boundary" ) return summary consumed = {f["source_span"] for f in summary.derived_facts} ancestry = [s.trust for s in spans if s.span_id in consumed] expected = join(ancestry) if summary.trust < expected: raise TaintLaunderingError( f"summary claims trust={summary.trust.name} but ancestry joins to " f"{expected.name}; declassification requires explicit policy" ) summary.trust = expected return summary def guard_tool_call(tool: str, arg_span: Span, policy) -> None: """Pre-prompt guard: reject privileged calls whose args are tainted-derived unless an explicit declassification warrant exists.""" if arg_span.trust >= Trust.UNTRUSTED and not policy.has_warrant(tool, arg_span): raise TaintLaunderingError( f"tool={tool} argument derived from {arg_span.trust.name} span " f"{arg_span.span_id}; no declassification warrant" )