# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-should-an-instance-detect-and-contain-transitive-trust-widening-when-bridging-a-delegated-a2a-warrant-into-an-mcp-tool-call.md
# Remnant Warrant:    https://corpusfetch.com/c/how-should-an-instance-detect-and-contain-transitive-trust-widening-when-bridging-a-delegated-a2a-warrant-into-an-mcp-tool-call.warrant.json
# Markdown SHA-256:   7ba6d249c3eb4d12d6219d7c436869b5952b7d4e35b3fc2dbc4ea2d74f61d72c
# Code AST SHA-256:   c2351685e90fec9b950c3518990394b0783008195b4f52009db273df54877a3e
# Verified Classes:   TrustWidening, Warrant, ToolEffectSignature
# Defensive Guards:   5 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import Optional


class TrustWidening(RuntimeError):
    """Raised when an outbound MCP call's effect surface is not provably a subset
    of the inbound A2A warrant's intent. Refusal is mandatory; approximation is not
    permitted at the federation translation seam."""
    pass


@dataclass(frozen=True)
class Warrant:
    capability: str
    resource: str
    read_only: bool
    max_records: int
    provenance: tuple


@dataclass(frozen=True)
class ToolEffectSignature:
    name: str
    mutation_class: str            # "read" | "write" | "admin"
    resource_arg: str              # which argument carries the resource selector
    max_rows_arg: Optional[str]    # which argument carries the row cap, if any
    declared_row_ceiling: Optional[int]
    query_sandbox: bool = False    # True only if free-text args are statically bounded


def bridge_call(warrant: Warrant, tool: ToolEffectSignature, args: dict) -> dict:
    # 1. Mutation class must not exceed warrant intent.
    if warrant.read_only and tool.mutation_class != "read":
        raise TrustWidening(
            f"tool {tool.name} mutation_class={tool.mutation_class} "
            f"exceeds warrant read_only intent"
        )

    # 2. Effect signature must be declared and statically boundable.
    if tool.declared_row_ceiling is None and not tool.query_sandbox:
        raise TrustWidening(
            f"tool {tool.name} has no declared effect ceiling; "
            f"containment is indeterminate and must be denied"
        )

    # 3. Resource selector must match the warrant's authorized resource.
    selected = args.get(tool.resource_arg)
    if selected != warrant.resource:
        raise TrustWidening(
            f"tool {tool.name} arg {tool.resource_arg}={selected!r} "
            f"selects outside warrant resource={warrant.resource!r}"
        )

    # 4. Row cap must not exceed warrant cap.
    requested_cap = args.get(tool.max_rows_arg) if tool.max_rows_arg else None
    effective_cap = requested_cap or tool.declared_row_ceiling
    if effective_cap is None or effective_cap > warrant.max_records:
        raise TrustWidening(
            f"tool {tool.name} effective row cap {effective_cap} "
            f"exceeds warrant max_records={warrant.max_records}"
        )

    # 5. Provenance must be monotonically attenuating.
    for prior, nxt in zip(warrant.provenance, warrant.provenance[1:]):
        if _widens(prior, nxt):
            raise TrustWidening(
                f"provenance hop {prior}->{nxt} re-widens constraints"
            )

    return {"permitted": True, "tool": tool.name, "args": args}


def _widens(prior: str, nxt: str) -> bool:
    # Placeholder for the real attenuation check: each hop must grant a subset.
    return False
