# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-attenuates-a-delegated-aaif-credential-below-the-minimum-viable-scope-for-a-resumed-task.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-attenuates-a-delegated-aaif-credential-below-the-minimum-viable-scope-for-a-resumed-task.warrant.json
# Markdown SHA-256:   b2b71fdf5f9302ba6dab20d09528e2e6632192792c6e243c0b112a4255194085
# Code AST SHA-256:   aadb4a49748bb2bf4bfa6d75940a65063f27486c3eb2fdbcd9da16c16b9f5cf2
# Verified Classes:   ScopeAttenuationDeadEnd, Credential, Checkpoint
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import FrozenSet


class ScopeAttenuationDeadEnd(RuntimeError):
    """Raised when a resumed credential's scope cannot satisfy the remaining plan.

    Fires at resume time, not at first denial, so the agent can re-attenuate
    from the parent handle instead of dead-ending mid-task.
    """


@dataclass(frozen=True)
class Credential:
    handle: str
    parent_handle: str | None
    scope: FrozenSet[str]


@dataclass
class Checkpoint:
    credential_handle: str
    parent_handle: str | None
    remaining_plan_manifest: FrozenSet[str]  # capabilities the rest of the plan needs
    observed_history_scope: FrozenSet[str]   # capabilities exercised so far


def resume_task(
    checkpoint: Checkpoint,
    store: dict[str, Credential],
    attenuate,
) -> Credential:
    parent = store.get(checkpoint.credential_handle)
    if parent is None:
        raise ScopeAttenuationDeadEnd(
            f"credential handle {checkpoint.credential_handle!r} not found in store; "
            "cannot resume without re-delegation"
        )

    # Derive scope from the remaining plan, never from observed history.
    target = checkpoint.remaining_plan_manifest

    # A plan-scoped attenuation must be a subset of the parent's scope.
    missing_from_parent = target - parent.scope
    if missing_from_parent:
        raise ScopeAttenuationDeadEnd(
            f"remaining plan requires {sorted(missing_from_parent)} "
            f"not present in parent scope {sorted(parent.scope)}; "
            "re-delegation to orchestrator required"
        )

    # Guard against the history-derived bug: if the plan needs something the
    # history-derived scope would have dropped, and we have no parent handle
    # to re-attenuate from, we are about to dead-end.
    history_scope = checkpoint.observed_history_scope
    would_drop = target - history_scope
    if would_drop and checkpoint.parent_handle is None:
        raise ScopeAttenuationDeadEnd(
            f"history-derived attenuation would drop {sorted(would_drop)} "
            "and no parent handle is available to re-attenuate; "
            "refusing to mint a dead-end credential"
        )

    # Safe: attenuate to the plan manifest from the parent handle.
    return attenuate(parent, target)
