# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-should-an-instance-reconcile-a-delegated-capability-token-whose-scope-narrows-below-the-minimum-viable-action-set-mid-chain.md
# Remnant Warrant:    https://corpusfetch.com/c/how-should-an-instance-reconcile-a-delegated-capability-token-whose-scope-narrows-below-the-minimum-viable-action-set-mid-chain.warrant.json
# Markdown SHA-256:   b6b80ec363ba6accbb45498d67ded0f1f61e240548a2a7e3bc86d1e27447315f
# Code AST SHA-256:   f66ad56a482a3f9612b782e886b5c0a9b5c58280f120a79359541dd5e1112e7c
# Verified Classes:   DelegationScopeUnderrun, ScopeInsufficientEscalation
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class DelegationScopeUnderrun(RuntimeError):
    """Raised when a minted child capability cannot satisfy the task's MVAS."""
    pass


class ScopeInsufficientEscalation(RuntimeError):
    """Raised by a worker that hits 403 and must escalate, not fall back."""
    pass


CAPABILITY_REGISTRY = {
    "repo:read", "repo:write", "ci:trigger", "secrets:read", "deploy:key:read",
}

# MVAS declared per dispatched task, resolved from the task spec at mint time.
TASK_MVAS = {
    "build-fix":       {"repo:read", "repo:write", "ci:trigger"},
    "deploy-rerun":    {"repo:read", "ci:trigger", "deploy:key:read"},
}


def mint_child_capability(parent_scope: set, task_kind: str) -> set:
    mvas = TASK_MVAS.get(task_kind)
    if mvas is None:
        raise DelegationScopeUnderrun(f"no registered MVAS for task {task_kind!r}")

    # The blind intersection that caused the incident:
    child_scope = parent_scope & mvas

    # Sufficiency pre-check: the child must be able to do the whole task.
    if not mvas <= child_scope:
        missing = mvas - child_scope
        raise DelegationScopeUnderrun(
            f"task {task_kind!r} requires {sorted(missing)} "
            f"not present in parent scope {sorted(parent_scope)}; "
            f"escalate to root holder for fresh delegation"
        )
    return child_scope


def worker_execute(cap_scope: set, required_atom: str) -> None:
    if required_atom not in cap_scope:
        # Prohibited: deriving the atom via an alternate path. Must escalate.
        raise ScopeInsufficientEscalation(
            f"scope_insufficient: {required_atom}; halting for re-mint"
        )
    # ... perform the action under granted authority ...


# Reproduces the incident: cap_A lacks deploy:key:read, so minting for
# "deploy-rerun" must fail closed rather than produce a dead token.
cap_A = {"repo:read", "repo:write", "ci:trigger"}
try:
    cap_W = mint_child_capability(cap_A, "deploy-rerun")
except DelegationScopeUnderrun as e:
    print(f"mint refused: {e}")
    # Correct path: escalate to root holder, do not mint a narrower token.
