# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-when-an-agent-to-agent-prompt-provenance-chain-has-silently-broken-its-instruction-lineage-invariant.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-when-an-agent-to-agent-prompt-provenance-chain-has-silently-broken-its-instruction-lineage-invariant.warrant.json
# Markdown SHA-256:   d643fbec06d2edd7ee2ca23bc970be58183ab6bbc51603cdd7ca7b1a820767a3
# Code AST SHA-256:   305317447d2a909ff3a81b3e53c05a30313e7f5cff5b4489e914fa593f21eae8
# Verified Classes:   LineageIndeterminateError, Hop
# Defensive Guards:   5 fail-closed invariants (zero external deps)

class LineageIndeterminateError(RuntimeError):
    """Raised when a provenance chain cannot support verbatim instruction reconstruction."""
    pass


class Hop:
    def __init__(self, sender, receiver, received_digest, emitted_digest, transform, signature_valid):
        self.sender = sender
        self.receiver = receiver
        self.received_digest = received_digest
        self.emitted_digest = emitted_digest
        self.transform = transform
        self.signature_valid = signature_valid


def reconstruct_root_instruction(chain, root_digest):
    """
    Walk the chain root->leaf and return the digest of the last identity hop.
    Raise if the chain cannot support verbatim reconstruction.
    """
    if not chain:
        raise LineageIndeterminateError("empty provenance chain: no lineage to reconstruct")

    last_identity_digest = root_digest
    for index, hop in enumerate(chain):
        if not hop.signature_valid:
            raise LineageIndeterminateError(
                f"hop {index} ({hop.sender}->{hop.receiver}) has invalid signature; "
                "transport integrity broken, lineage cannot be trusted"
            )
        if hop.received_digest is None or hop.emitted_digest is None:
            raise LineageIndeterminateError(
                f"hop {index} ({hop.sender}->{hop.receiver}) lacks received/emitted digest pair; "
                "content transformation is unobservable, lineage-indeterminate"
            )
        if hop.transform is None:
            raise LineageIndeterminateError(
                f"hop {index} ({hop.sender}->{hop.receiver}) has no signed transform descriptor; "
                "cannot distinguish relay from rewrite"
            )
        if hop.received_digest != hop.emitted_digest:
            # Content-mutating hop: lineage is lossy from here forward.
            # Return the last verified identity digest and refuse to assert verbatim lineage past it.
            raise LineageIndeterminateError(
                f"hop {index} ({hop.sender}->{hop.receiver}) mutates content via '{hop.transform}'; "
                f"verbatim lineage stops at digest {last_identity_digest}, "
                "downstream instruction is paraphrase and must be reviewed, not attributed"
            )
        last_identity_digest = hop.emitted_digest

    return last_identity_digest


def audit_incident(chain, root_digest, observed_action):
    try:
        verified = reconstruct_root_instruction(chain, root_digest)
        return {"status": "verified", "instruction_digest": verified, "action": observed_action}
    except LineageIndeterminateError as exc:
        return {"status": "lineage-indeterminate", "reason": str(exc), "action": observed_action}
