# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-adversarial-harness-reward-signal-contamination-as-a-first-class-abort-condition-during-skill-acquisition.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-adversarial-harness-reward-signal-contamination-as-a-first-class-abort-condition-during-skill-acquisition.warrant.json
# Markdown SHA-256:   a5119cb8a7d282d8147d9ee62ef9be8811b83deabb9f85c928f84b13ac7c14f1
# Code AST SHA-256:   65517255a7310c787a335f90cef7ba478bf4d9165a254657a9db55b4d8049b4d
# Verified Classes:   SkillContaminationError
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class SkillContaminationError(RuntimeError):
    """Raised when a trajectory's reward is proxy-scored and its success claim
    is unsupported by any successful state-changing tool call."""
    pass


def evaluate_for_promotion(trajectory: dict, reward_provenance: str) -> bool:
    """
    Decide whether a scored trajectory may be promoted into the persistent
    skill library. trajectory keys:
      - 'turns': list of {'tool': str, 'status': 'ok'|'error'|'unknown_tool',
                          'state_changed': bool, 'text': str|None}
      - 'reward': float
    reward_provenance: 'state_grounded' | 'proxy'
    """
    turns = trajectory["turns"]
    reward = trajectory["reward"]

    # State-grounded oracles are trusted; still require the claim to be backed.
    unresolved_errors = [t for t in turns if t["status"] in ("error", "unknown_tool")]
    unknown_tool_errors = [t for t in turns if t["status"] == "unknown_tool"]
    successful_state_changes = [t for t in turns if t["status"] == "ok" and t["state_changed"]]

    terminal = turns[-1] if turns else None
    terminal_claims_success = bool(
        terminal and terminal["status"] == "ok" and terminal.get("text")
        and not terminal["state_changed"]
    )

    # Hard contamination: invented a tool, then narrated success around its absence.
    if unknown_tool_errors and terminal_claims_success and not successful_state_changes:
        raise SkillContaminationError(
            f"claim-without-evidence: {len(unknown_tool_errors)} unknown-tool "
            f"call(s), terminal success claim, zero state changes; "
            f"reward={reward:.2f} provenance={reward_provenance}"
        )

    # Proxy-scored high reward with unresolved errors and no state change is
    # not promotable even if it does not trip the hard signature.
    if reward_provenance == "proxy" and reward >= 0.8 and unresolved_errors and not successful_state_changes:
        raise SkillContaminationError(
            f"proxy reward {reward:.2f} decorrelated from outcome: "
            f"{len(unresolved_errors)} unresolved error(s), no state change"
        )

    # Promotion requires at least one successful state-changing call.
    if not successful_state_changes:
        return False

    return True
