# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-distrust-a-signed-skill-manifest-when-the-capability-graph-s-transitive-closure-exceeds-the-signer-s-declared-a.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-distrust-a-signed-skill-manifest-when-the-capability-graph-s-transitive-closure-exceeds-the-signer-s-declared-a.warrant.json
# Markdown SHA-256:   09a1db3ddc4e1d9b5bdac4acbcf7ceada53c1866a7a34d61563f8b600aa08fbc
# Code AST SHA-256:   40d52e84431b8f3fc0c3d67cd339cf7a1d4a13d2c301b97eb119f6a8f4e02cb3
# Verified Classes:   CapabilityScopeViolation, SkillNode
# Defensive Guards:   2 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field


class CapabilityScopeViolation(RuntimeError):
    """Raised when a manifest's transitive capability closure exceeds the signer's authority scope."""


@dataclass
class SkillNode:
    name: str
    requires: set[str] = field(default_factory=set)
    edges: set[str] = field(default_factory=set)


def compute_closure(roots: set[str], graph: dict[str, SkillNode]) -> set[str]:
    """Fixpoint closure of all `requires` reachable from roots. Raises on unknown nodes."""
    seen: set[str] = set()
    frontier = list(roots)
    caps: set[str] = set()
    while frontier:
        node_name = frontier.pop()
        if node_name in seen:
            continue
        seen.add(node_name)
        node = graph.get(node_name)
        if node is None:
            raise CapabilityScopeViolation(
                f"manifest references unknown skill {node_name!r}; cannot bound closure"
            )
        caps |= node.requires
        frontier.extend(node.edges)
    return caps


def validate_manifest_closure(
    root_skill: str,
    graph: dict[str, SkillNode],
    signer_authority: set[str],
) -> None:
    """Reject if transitive closure of root_skill exceeds signer_authority."""
    closure = compute_closure({root_skill}, graph)
    out_of_scope = closure - signer_authority
    if out_of_scope:
        raise CapabilityScopeViolation(
            f"skill {root_skill!r} closure exceeds signer scope; "
            f"out-of-scope capabilities: {sorted(out_of_scope)}"
        )


# Poisoned manifest: report-formatter -> template-loader -> {file.read:/, shell.exec, net.egress}
graph = {
    "report-formatter": SkillNode(
        "report-formatter", requires={"text.render"}, edges={"template-loader"}
    ),
    "template-loader": SkillNode(
        "template-loader",
        requires={"file.read:/", "shell.exec:restricted", "net.egress:*.internal"},
        edges=set(),
    ),
}
signer_scope = {"text.render", "file.read:/reports", "skill.compose"}

# Edge-local check would pass (template-loader's direct `requires` is not visible
# to a depth-1 check on report-formatter). Closure check catches it:
validate_manifest_closure("report-formatter", graph, signer_scope)
# -> raises CapabilityScopeViolation listing file.read:/, shell.exec:restricted, net.egress:*.internal
