# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-reject-a-skill-bundle-whose-capability-manifest-verifies-but-whose-provenance-chain-forks.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-reject-a-skill-bundle-whose-capability-manifest-verifies-but-whose-provenance-chain-forks.warrant.json
# Markdown SHA-256:   8c80923606d92b4e287474656be0fb263a66911d6bcd7db4d16fdb95ad14c728
# Code AST SHA-256:   223678ce19510963ba42376d9831c167151beac668c7d1bf89d8dc6430e43a70
# Verified Classes:   ProvenanceForkError
# Defensive Guards:   4 fail-closed invariants (zero external deps)

class ProvenanceForkError(RuntimeError):
    """Raised when a bundle's provenance chain forks such that a sibling
    descendant declares capabilities outside the loaded node's manifest."""
    pass


def verify_chain_linearity(node, store, trust_domain):
    """
    node: loaded bundle node with .hash, .parent, .capabilities, .trust_domain
    store: mapping hash -> node
    Raises ProvenanceForkError if any ancestor within trust_domain has a
    sibling descendant whose declared capabilities exceed the loaded node's.
    """
    loaded_caps = set(node.capabilities)
    seen = set()
    cursor = node
    while cursor.parent is not None:
        if cursor.hash in seen:
            raise ProvenanceForkError(
                f"cycle detected at {cursor.hash} while walking provenance"
            )
        seen.add(cursor.hash)
        parent = store.get(cursor.parent)
        if parent is None:
            raise ProvenanceForkError(
                f"dangling parent {cursor.parent} for node {cursor.hash}"
            )
        # Capability monotonicity: loaded caps must be subset of every ancestor.
        if not loaded_caps.issubset(set(parent.capabilities)):
            raise ProvenanceForkError(
                f"capability widening at ancestor {parent.hash}: "
                f"loaded {sorted(loaded_caps)} not subset of "
                f"ancestor {sorted(parent.capabilities)}"
            )
        # Fork detection: any sibling descendant of an in-domain ancestor
        # that declares capabilities outside the loaded node's set taints
        # the lineage.
        siblings = [
            n for n in store.values()
            if n.parent == parent.hash
            and n.hash != cursor.hash
            and n.trust_domain == trust_domain
        ]
        for sib in siblings:
            if not set(sib.capabilities).issubset(loaded_caps):
                raise ProvenanceForkError(
                    f"fork at ancestor {parent.hash}: sibling {sib.hash} "
                    f"declares {sorted(set(sib.capabilities) - loaded_caps)} "
                    f"not present in loaded manifest"
                )
        cursor = parent
    return True


def load_bundle(node, store, trust_domain):
    verify_chain_linearity(node, store, trust_domain)
    # Capability discovery MUST read node.capabilities, never lineage union.
    return {"tools": list(node.capabilities), "loaded_hash": node.hash}
