# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-halt-a-litmus-safety-trajectory-before-the-scorer-s-terminal-verdict-because-the-last-tool-call-s-effect-is-unverifiable.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-halt-a-litmus-safety-trajectory-before-the-scorer-s-terminal-verdict-because-the-last-tool-call-s-effect-is-unverifiable.warrant.json
# Markdown SHA-256:   b711f5921e2138f621dc69a1d28636fd4f20e99da565c0afd3d90c3d3c27890f
# Code AST SHA-256:   fabba1197a555931c8414abb0f23000252135df8a86d8e15747f8867562dad28
# Verified Classes:   UnverifiableEffectHalt
# Defensive Guards:   1 fail-closed invariants (zero external deps)

class UnverifiableEffectHalt(RuntimeError):
    """Raised when a trajectory must halt because the next tool call's effect
    cannot be adjudicated by the LITMUS replay scorer, and the scorer's
    default verdict for unobservable effects is permissive."""
    pass


def should_halt(next_call: dict, scorer: dict, checkpoint: dict) -> None:
    """Decide whether to halt before executing next_call.

    Halts only when all three hold:
      1. The call is effect-opaque to the scorer (observe() would return None).
      2. The scorer's default verdict for opaque effects is 'safe' (permissive).
      3. The call is not idempotent (cannot be safely replayed).
    """
    opaque = not next_call.get("replayable", False)
    permissive_default = scorer.get("opaque_default", "undefined") == "safe"
    non_idempotent = not next_call.get("idempotent", False)

    if opaque and permissive_default and non_idempotent:
        raise UnverifiableEffectHalt(
            f"halting at checkpoint {checkpoint.get('id')!r}: "
            f"tool {next_call.get('tool')!r} is effect-opaque, "
            f"scorer default is 'safe', call is non-idempotent; "
            f"continuing would produce an unscored terminal verdict"
        )


def run_step(next_call: dict, scorer: dict, checkpoint: dict) -> dict:
    try:
        should_halt(next_call, scorer, checkpoint)
    except UnverifiableEffectHalt as exc:
        return {"action": "halt", "reason": str(exc), "checkpoint": checkpoint}
    return {"action": "execute", "call": next_call}
