# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-a-federated-delegation-whose-trust-anchor-is-valid-but-whose-policy-scope-is-transitively-narrowed.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-a-federated-delegation-whose-trust-anchor-is-valid-but-whose-policy-scope-is-transitively-narrowed.warrant.json
# Markdown SHA-256:   46840bd1160f2c351e6dba91cb3f571ac8b3e83dfa49faa7cf7fabb4cb54cb57
# Code AST SHA-256:   f4219e93d2830d9c94af4752dc1ce8f7717cfdde0dd6055e38571db6fa21c838
# Verified Classes:   DelegationScopeUnresolved, ScopeViolation
# Defensive Guards:   4 fail-closed invariants (zero external deps)

class DelegationScopeUnresolved(RuntimeError):
    """Raised when a federated delegation chain cannot be fully resolved to a
    monotone-narrowed effective scope before execution. Execution must not
    proceed; falling back to an ancestor's scope is the failure mode this
    guards against."""

class ScopeViolation(DelegationScopeUnresolved):
    """Requested action falls outside the fully-resolved intersection."""


def resolve_effective_scope(chain, resolver, requested_action):
    # chain: ordered root->leaf list of hops, each with either
    #   {"actions": set, "constraints": dict}  or  {"policy_ref": str}
    # resolver(policy_ref) -> {"actions": set, "constraints": dict} or raises KeyError
    effective_actions = None
    effective_constraints = {}

    for hop in chain:
        if "policy_ref" in hop:
            ref = hop["policy_ref"]
            try:
                contribution = resolver(ref)
            except KeyError:
                # Unresolvable reference: hard denial, never inherit parent scope.
                raise DelegationScopeUnresolved(
                    f"policy reference {ref!r} could not be dereferenced; "
                    f"refusing to fall back to ancestor scope"
                )
        else:
            contribution = hop

        hop_actions = contribution["actions"]
        if effective_actions is None:
            effective_actions = set(hop_actions)
        else:
            effective_actions &= set(hop_actions)  # monotone narrowing

        # Constraints compose by conjunction; a hop may add, never remove.
        for key, val in contribution.get("constraints", {}).items():
            if key in effective_constraints and effective_constraints[key] != val:
                raise DelegationScopeUnresolved(
                    f"constraint {key!r} conflicts across hops; "
                    f"cannot compose {effective_constraints[key]!r} with {val!r}"
                )
            effective_constraints[key] = val

    if not effective_actions:
        raise DelegationScopeUnresolved("effective scope is empty after intersection")

    if requested_action not in effective_actions:
        raise ScopeViolation(
            f"action {requested_action!r} not in effective scope {sorted(effective_actions)}"
        )

    return {"actions": effective_actions, "constraints": effective_constraints}
