# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-of-runtime-agent-supply-chain-attestation-fail-closed-the-transitive-tool-closure-gap-in-slsa-style-prompt-tool-weight-provenan.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-of-runtime-agent-supply-chain-attestation-fail-closed-the-transitive-tool-closure-gap-in-slsa-style-prompt-tool-weight-provenan.warrant.json
# Markdown SHA-256:   138f478be057ea6f2d2f501493c693338c15f8c718895756273dfb6f966be55e
# Code AST SHA-256:   b35b7fa5f732bedea5dbb4ba918a3b5370a492e7adf39d6ad2e789a1bc9eeeff
# Verified Classes:   UnattestedClosureError
# Defensive Guards:   5 fail-closed invariants (zero external deps)

class UnattestedClosureError(RuntimeError):
    """Raised when the runtime execution closure contains a node
    that cannot be resolved to an attested content digest."""
    pass


def verify_execution_closure(root_tool, digest_map, attested_subjects, max_depth=16):
    """
    Walk the transitive tool/dependency closure starting at root_tool.
    Fail closed on: unattested digest, name-only resolution, or depth overflow.
    """
    stack = [(root_tool, 0)]
    seen = set()

    while stack:
        node, depth = stack.pop()
        if depth > max_depth:
            raise UnattestedClosureError(
                f"closure depth exceeded at {node.name!r}; refusing to execute"
            )
        if node.digest is None:
            # Name-resolved node (PATH lookup, tag fetch, dynamic import)
            raise UnattestedClosureError(
                f"node {node.name!r} resolved without a content digest; "
                f"name-based resolution is not attestable"
            )
        if node.digest not in attested_subjects:
            raise UnattestedClosureError(
                f"node {node.name!r} digest {node.digest} has no provenance statement"
            )
        if digest_map.get(node.name) != node.digest:
            # Valid statement, wrong subject: substitution, not staleness.
            raise UnattestedClosureError(
                f"substitution detected for {node.name!r}: "
                f"executed {node.digest} != attested {digest_map.get(node.name)}"
            )
        if node.digest in seen:
            continue
        seen.add(node.digest)
        for ref in node.references:
            stack.append((ref, depth + 1))

    return True


def admit_agent(prompt_bundle, tool_registry, weight_manifest, digest_map, attested_subjects):
    # Admission verifies the flat registry...
    for artifact in (prompt_bundle, tool_registry, weight_manifest):
        if artifact.digest not in attested_subjects:
            raise UnattestedClosureError(
                f"artifact {artifact.name!r} has no provenance statement"
            )
    # ...but attestation status is decided by the runtime closure, not the registry.
    for tool in tool_registry.tools:
        verify_execution_closure(tool, digest_map, attested_subjects)
    return "agent-attested"
