# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-treat-a-manifest-diff-as-adversarial-rather-than-a-benign-version-bump-the-canary-probe-blind-spot.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-treat-a-manifest-diff-as-adversarial-rather-than-a-benign-version-bump-the-canary-probe-blind-spot.warrant.json
# Markdown SHA-256:   695ca20026c0906fd6ffbc4b648209b99377d26e52400beac7f562b94180b8b4
# Code AST SHA-256:   0ad2d9b809e959f3ea86685c3ba24c87eef3459ede1931d1dcd8cdce021074da
# Verified Classes:   UndeclaredControlChannelError
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class UndeclaredControlChannelError(RuntimeError):
    """Raised when a tool output typed as data is promoted to control."""
    pass


DATA_TYPED_CHANNELS = {"string", "text", "object", "json"}


def admit_tool_result(declared_output_type: str, raw_result: str,
                      promoted_control: bool, probe_record: dict) -> str:
    """
    Enforce the output-channel invariant: data-typed outputs must never be
    promoted to the control plane, regardless of manifest diff or canary pass.
    """
    # A clean manifest diff and a passing canary set are NOT sufficient.
    if probe_record.get("pass") is True and probe_record.get("n_probes", 0) < 64:
        # Weak evidence: do not let it relax the channel guard.
        pass

    if declared_output_type in DATA_TYPED_CHANNELS and promoted_control:
        raise UndeclaredControlChannelError(
            f"tool output declared as {declared_output_type!r} was promoted to "
            f"control plane; probe record={probe_record!r}. Manifest diff and "
            f"canary pass do not authorize control-channel use."
        )

    return raw_result


def admit_manifest(diff: dict) -> None:
    """
    A clean schema diff is insufficient if any extension/free-text field
    changed in the same release — that is a diff-smuggling signal.
    """
    schema_clean = diff.get("schema_nodes_changed", 0) == 0
    extension_changed = diff.get("extension_fields_changed", 0) > 0
    if schema_clean and extension_changed:
        raise UndeclaredControlChannelError(
            "clean schema diff with changed x-*/free-text field: escalate to "
            "human review; do not admit on diff alone."
        )
