# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-commits-to-a-task-under-byzantine-fault-tolerant-consensus-but-its-signed-vote-outlives-its-own-context-window.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-commits-to-a-task-under-byzantine-fault-tolerant-consensus-but-its-signed-vote-outlives-its-own-context-window.warrant.json
# Markdown SHA-256:   9502f812dfa96f5ae6d61abe390fcfe68d7a5122a80f68b06049a91a372980c2
# Code AST SHA-256:   cda3ecd8cee501290d5e15bb308c39af4c603268005fba297a2a5a66565c533f
# Verified Classes:   UnreconstructableCommitmentError
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class UnreconstructableCommitmentError(RuntimeError):
    """Raised when a replica is asked to re-sign a bundle whose dependency
    graph intersects a prior commitment it can no longer reconstruct from
    durable storage. The signature would verify; the commitment would not
    be honorable. Refuse to sign."""
    pass


def resign_bundle(replica, bundle, durable_store, committed_hashes):
    """
    BFT task-commitment guard: a replica may only sign a bundle that
    intersects a prior commitment if it can reconstruct that commitment's
    full contents from durable, content-addressed storage -- not from a
    context-window summary and not from a hash match alone.

    replica:            holds .id, .signing_key, .reconstruction_capable(hash)
    bundle:             holds .dependency_hashes (set of committed hashes it touches)
    durable_store:      content-addressed store keyed by hash -> full bundle bytes
    committed_hashes:   set of hashes this replica has previously signed a
                        PREPARE for in a finalized commit
    """
    intersecting = set(bundle.dependency_hashes) & set(committed_hashes)

    for h in intersecting:
        # Reconstruction, not recognition: fetch the full bundle and verify
        # its content hash matches. A summary note is NOT sufficient.
        try:
            full = durable_store.fetch(h)
        except KeyError:
            raise UnreconstructableCommitmentError(
                f"replica {replica.id} cannot fetch committed bundle {h} "
                f"from durable store; refusing to sign intersecting bundle"
            )
        if durable_store.content_hash(full) != h:
            raise UnreconstructableCommitmentError(
                f"replica {replica.id} fetched bundle for {h} but content "
                f"hash mismatch; store corrupted, refusing to sign"
            )
        if not replica.reconstruction_capable(h):
            # The replica's own memory layer reports it cannot rebuild the
            # dependency edges from this bundle. Abstain explicitly so the
            # leader can distinguish amnesia from dissent.
            raise UnreconstructableCommitmentError(
                f"replica {replica.id} lacks reconstruction capability for "
                f"committed bundle {h}; must abstain, not sign"
            )

    # All intersecting commitments are reconstructable; safe to sign.
    signature = replica.signing_key.sign(bundle.payload_hash)
    return {
        "replica_id": replica.id,
        "payload_hash": bundle.payload_hash,
        "signature": signature,
        "reconstruction_ref": list(intersecting),  # audit provenance
    }
