# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-taint-quarantine-deadlock-in-injection-gated-tool-sandboxes.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-taint-quarantine-deadlock-in-injection-gated-tool-sandboxes.warrant.json
# Markdown SHA-256:   97f0a16bea436c93d31d109fb92463ba3562720fbcdbeb9dfe212c551f786619
# Code AST SHA-256:   abba7e109626876e8b51af90175b91bc1616c25148f69bccc1cc15e574fd2d87
# Verified Classes:   UnreachableQuarantineRelease
# Defensive Guards:   1 fail-closed invariants (zero external deps)

class UnreachableQuarantineRelease(RuntimeError):
    """Raised when a quarantine record's release principal is also the blocked consumer."""
    pass


def admit_tool_output(artifact, policy_graph, blocked_consumer):
    """Decide the disposition of a detector-tainted tool output.

    Returns a terminal status string. Never returns an open 'quarantined'
    status to a principal that cannot release the record.
    """
    if not artifact.tainted:
        return "CLEAN"

    releasers = set(policy_graph.releasers_for(artifact.quarantine_id))
    if blocked_consumer in releasers:
        # Release-principal disjointness violated: the only principal that
        # may release this artifact is the one whose context is gated on it.
        if not policy_graph.has_fallback(artifact.quarantine_id):
            raise UnreachableQuarantineRelease(
                f"quarantine {artifact.quarantine_id}: release principal "
                f"{blocked_consumer!r} is the blocked consumer and no "
                f"fallback releaser is configured; refusing to advertise "
                f"an open state"
            )
        # Route to fallback; return a terminal status, not an open one.
        policy_graph.escalate(artifact.quarantine_id, to="fallback")
        return "ESCALATED_TERMINAL"

    return "QUARANTINED_OPEN"
