# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-trusts-a-signed-agent-card-beyond-its-manifest-s-declared-scope-during-a2a-v1-0-cross-agent-task-delegation.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-trusts-a-signed-agent-card-beyond-its-manifest-s-declared-scope-during-a2a-v1-0-cross-agent-task-delegation.warrant.json
# Markdown SHA-256:   a1a0b0bcfce9e2de8560349b1ce83219f14a05a608024e4768d847ad623b72d0
# Code AST SHA-256:   4341dccbd2c00e8baa78a3ec10697d3df3ccb6c262c67b2aa613a34e7d38d64b
# Verified Classes:   ManifestScopeViolation
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class ManifestScopeViolation(RuntimeError):
    """Raised when a delegation's effective (transitive) scope exceeds the
    signed manifest's declared scope without executor attestation or
    caller-side containment."""
    pass


def dispatch_delegation(card, task, attestation=None, contained=False):
    manifest = card.verified_manifest  # signature already checked
    declared = manifest.scope
    effective = task.reachable_scope()  # follows references, not just direct inputs

    if not effective.issubset(declared):
        if attestation is None and not contained:
            raise ManifestScopeViolation(
                f"task {task.id} reaches {sorted(effective - declared)} "
                f"beyond declared scope {sorted(declared)}; "
                f"no executor attestation and no caller-side containment"
            )
        # contained=True means a scoped credential bounds the reach at W;
        # attestation means W's runtime enforcement is covered by a signature.
    if manifest.side_effects != "none" and not contained:
        raise ManifestScopeViolation(
            f"task {task.id} declares side_effects={manifest.side_effects} "
            f"but delegation carries no write-bounding credential"
        )
    return card.dispatch(task)
