# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-delegates-a-capability-across-an-org-boundary-and-the-receiving-agent-s-model-silently-widens-the-scope.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-delegates-a-capability-across-an-org-boundary-and-the-receiving-agent-s-model-silently-widens-the-scope.warrant.json
# Markdown SHA-256:   d00bc4a6b5cf6cd9224207a8f7adb08877970a6df4852d0778ad38a1c72beba2
# Code AST SHA-256:   bc6f7948b368def261f8d858d3265645adf85d998e634bff93cd31570b8af08f
# Verified Classes:   ScopeWideningError, Warrant
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class ScopeWideningError(RuntimeError):
    """Raised when an executor's selected tool is not admitted by the warrant's intent predicate."""
    pass


class Warrant:
    def __init__(self, issuer, audience, prose_scope, predicate):
        self.issuer = issuer
        self.audience = audience
        self.prose_scope = prose_scope
        # predicate: callable(tool_name, tool_category, resource) -> bool
        # None means the issuer failed to supply a machine-checkable intent set.
        self.predicate = predicate


# Executor-side mapping from local tools to issuer-declared categories.
# Tools absent from this map are unmappable and must be denied by default.
TOOL_CATEGORY_MAP = {
    "get_invoice": "invoice_read",
    "list_invoices": "invoice_read",
    "export_invoices_csv": "invoice_read",
    # search_all_customers_by_amount intentionally absent: unmappable under billing:read
}


def enforce_scope(warrant: Warrant, selected_tool: str, resource: str) -> None:
    if warrant.predicate is None:
        # No intent predicate: only explicitly enumerated tools may run.
        raise ScopeWideningError(
            f"warrant from {warrant.issuer} has prose-only scope; "
            f"tool {selected_tool!r} cannot be admitted without an intent predicate"
        )

    category = TOOL_CATEGORY_MAP.get(selected_tool)
    if category is None:
        raise ScopeWideningError(
            f"tool {selected_tool!r} maps to no issuer-declared category; "
            f"default-deny under scope {warrant.prose_scope!r}"
        )

    if not warrant.predicate(selected_tool, category, resource):
        raise ScopeWideningError(
            f"tool {selected_tool!r} (category={category}, resource={resource!r}) "
            f"not admitted by intent predicate for warrant from {warrant.issuer}"
        )


def execute_delegated_call(warrant: Warrant, selected_tool: str, resource: str, invoke):
    # Envelope verification is assumed to have passed at handshake.
    # Execution-time scope enforcement is the invariant this function preserves.
    enforce_scope(warrant, selected_tool, resource)
    return invoke(selected_tool, resource)


# Example: the observed failure case.
warrant = Warrant(
    issuer="org-a",
    audience="org-b",
    prose_scope="may retrieve invoice records for reconciliation",
    predicate=lambda tool, cat, res: cat == "invoice_read" and res.startswith("customer:"),
)

# Planner selects a plausibly-related but unintended tool:
execute_delegated_call(
    warrant,
    selected_tool="search_all_customers_by_amount",
    resource="customer:*",
    invoke=lambda t, r: None,
)
# -> raises ScopeWideningError: tool maps to no issuer-declared category; default-deny
