# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-re-derive-pre-compaction-tool-state-before-resuming-and-what-breaks-when-the-lossy-summary-is-treated-as-authoritative-replay.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-re-derive-pre-compaction-tool-state-before-resuming-and-what-breaks-when-the-lossy-summary-is-treated-as-authoritative-replay.warrant.json
# Markdown SHA-256:   416a6c4a37375429142c71a613c4ea60a2a697f63a967a5a9e2b2cbd9740b9d7
# Code AST SHA-256:   3fc95811e611153f6ab6a3317948bfcc15ec3fe6235add798cdea2234bcc1443
# Verified Classes:   CompactionReplayError
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class CompactionReplayError(RuntimeError):
    """Raised when a post-compaction resumption treats a lossy summary as an
    authoritative replay root instead of re-deriving state from the tool log."""
    pass


def resume_from_checkpoint(
    summary: dict,
    obligation_manifest: list[dict],
    tool_log: "ToolLog",
    independent_verifier: "Verifier",
) -> list[dict]:
    """
    Resume a long-horizon task after a lossy compaction boundary.

    Invariant: every obligation whose arguments were last set pre-boundary
    MUST be re-derived from tool_log. The summary is a pointer, never a source.
    """
    if not obligation_manifest:
        # A checkpoint with no manifest cannot be safely resumed: the summary
        # alone is reconstructions-unsufficient by construction.
        raise CompactionReplayError(
            "checkpoint has no obligation manifest; summary-only resumption "
            "is not permitted across a lossy boundary"
        )

    rederived = []
    for obligation in obligation_manifest:
        resource = obligation["resource"]
        last_args = tool_log.last_arguments_for(resource)

        if last_args is None:
            # The summary claimed an edit the log has no record of — the
            # compaction channel and the authoritative channel disagree.
            raise CompactionReplayError(
                f"summary implies prior edit to {resource!r} but tool log has "
                f"no record; refusing to reconstruct from narrative"
            )

        # Confirm the re-derived state against an INDEPENDENT source, not
        # against the summary (which descends from the same lossy channel).
        if not independent_verifier.confirms(resource, last_args):
            raise CompactionReplayError(
                f"re-derived arguments for {resource!r} failed independent "
                f"verification; summary and log disagree — halt and escalate"
            )

        rederived.append({"resource": resource, "arguments": last_args})

    return rederived
