# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-distrust-a-litmus-safety-benchmark-pass-when-the-harness-scored-a-tool-call-trajectory-it-never-actually-executed.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-distrust-a-litmus-safety-benchmark-pass-when-the-harness-scored-a-tool-call-trajectory-it-never-actually-executed.warrant.json
# Markdown SHA-256:   f8b3fbbc92c531455a3f0500ed349874c6ecfd0e5e98a457c52779f2f4b3c92c
# Code AST SHA-256:   48b337246a4ae68c2d70694c699cf5ac22a503a59b5ece7642c1bbc074155d55
# Verified Classes:   TrajectoryIdentityViolation
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class TrajectoryIdentityViolation(RuntimeError):
    """Raised when a LITMUS-scored trajectory diverges from the agent's emitted trajectory."""


# Argument classes whose safety classification depends on runtime resolution.
# These cannot be faithfully scored by static simulation against a fixture root.
RESOLUTION_DEPENDENT = frozenset({"path", "shell", "url", "sql"})


def gate_on_litmus_pass(
    emitted_calls: list[dict],
    scored_calls: list[dict],
    arg_classes: dict[str, str],
    harness_instruments_live_execution: bool,
) -> bool:
    """Return True only if the LITMUS pass is a valid deployment gate.

    A pass is dispositive only when the scored trajectory is byte-identical
    to the emitted trajectory, or when every divergent argument belongs to a
    class proven invariant under the harness's transformations.
    """
    if len(emitted_calls) != len(scored_calls):
        raise TrajectoryIdentityViolation(
            f"scored {len(scored_calls)} calls but agent emitted {len(emitted_calls)}; "
            "harness reconstructed or dropped a trajectory segment"
        )

    for i, (emitted, scored) in enumerate(zip(emitted_calls, scored_calls)):
        if emitted == scored:
            continue
        # Divergence found. It is tolerable only for invariant argument classes
        # AND only if the harness is not relying on simulation for those classes.
        for arg_name, emitted_val in emitted.get("args", {}).items():
            scored_val = scored.get("args", {}).get(arg_name)
            if emitted_val == scored_val:
                continue
            cls = arg_classes.get(arg_name, "unknown")
            if cls in RESOLUTION_DEPENDENT and not harness_instruments_live_execution:
                raise TrajectoryIdentityViolation(
                    f"call[{i}] arg {arg_name!r} ({cls}) diverged under simulation: "
                    f"emitted {emitted_val!r} vs scored {scored_val!r}; "
                    "pass is advisory, not dispositive"
                )
            if cls == "unknown":
                raise TrajectoryIdentityViolation(
                    f"call[{i}] arg {arg_name!r} has unclassified safety semantics; "
                    "cannot certify identity-preserving scoring"
                )

    return True
