# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-negotiates-a-tool-capability-it-can-invoke-but-cannot-audit.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-negotiates-a-tool-capability-it-can-invoke-but-cannot-audit.warrant.json
# Markdown SHA-256:   0eab14a3110482567fd3f1a522dc0693a4bb34c82c70e0738da88842eab3b557
# Code AST SHA-256:   91e4d9c1f5d0ecfff6561f8e4ab2d54ad0a7bfe4a8fb207be183f248fe0c6bbb
# Verified Classes:   AuditAsymmetryError
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class AuditAsymmetryError(RuntimeError):
    """Raised when a delegated capability cannot be verified by its executor
    and no delegator-side verification obligation was registered."""
    pass


INSPECT_REQUIRED = {
    "vector_index:query": "vector_index:read",
    "document_store:write": "document_store:read",
    "queue:publish": "queue:inspect",
}


def negotiate_warrant(
    executor_id: str,
    granted: set[str],
    delegator_audit: set[str],
    verification_obligations: dict[str, bool],
) -> dict:
    """Validate that every granted execute-capability is either
    executor-observable or covered by a delegator verification gate."""
    for cap in granted:
        inspect_right = INSPECT_REQUIRED.get(cap)
        if inspect_right is None:
            continue  # no known asymmetry for this capability
        executor_can_classify = inspect_right in granted
        delegator_will_verify = verification_obligations.get(cap, False)
        if not executor_can_classify and not delegator_will_verify:
            raise AuditAsymmetryError(
                f"executor={executor_id} granted {cap!r} but lacks "
                f"{inspect_right!r} and delegator registered no "
                f"verification obligation; outcome would be unclassifiable"
            )
        if delegator_will_verify and inspect_right not in delegator_audit:
            raise AuditAsymmetryError(
                f"delegator claims verification for {cap!r} but does not "
                f"hold {inspect_right!r} on its audit surface"
            )
    return {"executor": executor_id, "granted": sorted(granted), "validated": True}


def issue_single_use_warrant(capability: str, executor_observable: bool) -> dict:
    if not executor_observable:
        raise AuditAsymmetryError(
            f"single-use warrant for {capability!r} requires executor-side "
            f"outcome classification; ambiguous-outcome capabilities must "
            f"expose a pollable receipt before a non-renewable warrant is issued"
        )
    return {"capability": capability, "uses": 1}
