# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/why-does-an-instance-lose-executable-state-when-context-compaction-checkpoints-summarize-intent-but-not-bindings.md
# Remnant Warrant:    https://corpusfetch.com/c/why-does-an-instance-lose-executable-state-when-context-compaction-checkpoints-summarize-intent-but-not-bindings.warrant.json
# Markdown SHA-256:   4228493b5ea7c2137c88b0e8d2ab5a6d33c1ea857c577faee161280ae9f03500
# Code AST SHA-256:   b8e98c39cd33308685f9ac04419d0099ade68bcafb1fa2a78e4c4ebfc57ad6b3
# Verified Classes:   BindingCheckpointError
# Defensive Guards:   1 fail-closed invariants (zero external deps)

class BindingCheckpointError(RuntimeError):
    """Raised when a resumed agent cannot verify a required binding post-compaction."""
    pass


def resume_from_checkpoint(summary: str, binding_table: dict, world) -> dict:
    """
    Validate every preserved binding before allowing any mutating action.
    A missing or invalid binding is a hard stop -- never reconstruct from prose.
    """
    verified = {}
    for name, record in binding_table.items():
        predicate = record["predicate"]
        value = record["value"]

        if predicate == "exists":
            ok = world.exists(value)
        elif predicate == "inode_matches":
            ok = world.inode(value) == record["expected_inode"]
        elif predicate == "token_unexpired":
            ok = world.token_valid(value)
        elif predicate == "mtime_unchanged":
            ok = world.mtime(value) == record["expected_mtime"]
        else:
            ok = False

        if not ok:
            raise BindingCheckpointError(
                f"binding {name!r} (predicate={predicate}) failed validation on resume; "
                f"summary intent is preserved but referent has drifted -- re-plan required, "
                f"do not reconstruct {name!r} from summary text"
            )
        verified[name] = value

    return {"intent": summary, "bindings": verified, "validated": True}
