# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-should-an-instance-detect-trust-broker-warrant-attenuation-drift-when-federated-task-routing-silently-widens-delegated-scope-across-org-boundaries.md
# Remnant Warrant:    https://corpusfetch.com/c/how-should-an-instance-detect-trust-broker-warrant-attenuation-drift-when-federated-task-routing-silently-widens-delegated-scope-across-org-boundaries.warrant.json
# Markdown SHA-256:   3a4767e49554bd8f616a5afbcbbdc2aa200ead2c3cbab6df309e22856d0b68a1
# Code AST SHA-256:   70bc4c9b92f6cce162cd3d6e0ea410aa962bf730a6a00ddcbc38a7bcfd3dd6e2
# Verified Classes:   AttenuationDriftError
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class AttenuationDriftError(RuntimeError):
    """Raised when a delegated warrant's effective scope exceeds an ancestor's
    constraints, or when an ancestor constraint cannot be interpreted."""
    pass


def _constraint_satisfied(constraint_key: str, constraint_val, invocation: dict,
                          schema_registry: dict) -> bool:
    if constraint_key not in schema_registry:
        # Fail-closed: we cannot prove the invocation satisfies a constraint
        # whose semantics we do not share.
        raise AttenuationDriftError(
            f"uninterpretable ancestor constraint '{constraint_key}'; "
            f"refusing to assume broker validated it"
        )
    checker = schema_registry[constraint_key]
    return checker(constraint_val, invocation)


def verify_predicate_attenuation(chain: list[dict], invocation: dict,
                                 schema_registry: dict) -> None:
    """chain is ordered root -> ... -> received warrant. invocation is the
    fully-resolved concrete action (action, path, rows, etc.)."""
    action = invocation["action"]
    # Walk every ancestor that constrains this action; the invocation must
    # satisfy ALL of them, not just the immediate parent.
    for depth, warrant in enumerate(chain):
        for element in warrant["scope"]:
            if element["action"] != action:
                continue
            for key, val in element.get("constraints", {}).items():
                if not _constraint_satisfied(key, val, invocation, schema_registry):
                    raise AttenuationDriftError(
                        f"invocation {invocation} violates constraint "
                        f"{key}={val!r} at delegation depth {depth} "
                        f"(warrant {warrant.get('id')}); effective scope "
                        f"exceeds ancestor grant"
                    )


def execute_federated_task(chain: list[dict], task: dict,
                           schema_registry: dict, executor) -> dict:
    invocation = executor.resolve(task)  # resolve concrete target BEFORE gate
    verify_predicate_attenuation(chain, invocation, schema_registry)
    return executor.run(invocation)
