# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-reconcile-capability-scoped-syscall-grants-against-egress-policy-when-a-tool-call-sandbox-rehydrates-after-a-mid-execution-chec.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-reconcile-capability-scoped-syscall-grants-against-egress-policy-when-a-tool-call-sandbox-rehydrates-after-a-mid-execution-chec.warrant.json
# Markdown SHA-256:   0224d76b2a92931b1f2d7b29434833c11b06adf2606fef41dad7ec065d655d7c
# Code AST SHA-256:   43cb0d6bad4e275fb1cc46948e895852901f5be9fd920fe32db074b93dc8a9a5
# Verified Classes:   CapabilityRehydrationError, Grant, Sandbox
# Defensive Guards:   4 fail-closed invariants (zero external deps)

class CapabilityRehydrationError(RuntimeError):
    """Raised when a checkpointed capability cannot be safely re-derived at resume."""
    pass


class Grant:
    def __init__(self, name, object_identity, egress_intent, policy_version):
        self.name = name                      # integer fd or token name
        self.object_identity = object_identity  # e.g. (socket_cookie,) or (inode, gen)
        self.egress_intent = egress_intent    # recorded destination, not live peer
        self.policy_version = policy_version


class Sandbox:
    def __init__(self, live_policy, live_policy_version):
        self.live_policy = live_policy
        self.live_policy_version = live_policy_version
        self.grants = {}

    def live_object_identity(self, name):
        # Resolve the *current* object behind a name; may differ from checkpoint.
        return self._fd_table.get(name)

    def egress_allows(self, intent):
        return self.live_policy.allows(intent)

    def rehydrate(self, checkpoint_grants):
        for g in checkpoint_grants:
            current = self.live_object_identity(g.name)
            if current is None:
                raise CapabilityRehydrationError(
                    f"grant {g.name}: no live object at restore"
                )
            if current != g.object_identity:
                raise CapabilityRehydrationError(
                    f"grant {g.name}: object identity mismatch "
                    f"(checkpoint={g.object_identity}, live={current}) — fd reuse suspected"
                )
            if self.live_policy_version > g.policy_version and self.egress_allows(g.egress_intent):
                # Policy widened across the checkpoint window; intent may be laundered.
                raise CapabilityRehydrationError(
                    f"grant {g.name}: egress policy drifted across checkpoint "
                    f"(checkpoint v{g.policy_version} -> live v{self.live_policy_version}); "
                    f"refusing to re-issue, revoke and re-acquire"
                )
            if not self.egress_allows(g.egress_intent):
                raise CapabilityRehydrationError(
                    f"grant {g.name}: egress intent {g.egress_intent} no longer allowed"
                )
            self.grants[g.name] = g  # only reached when both proofs hold
        return self.grants
