"""Runtime gate protocols for trust-relevant AITP v5 adapter actions."""

from __future__ import annotations

from copy import deepcopy
from typing import Any


_RUNTIME_GATE_PROTOCOLS = {
    "record_code_state": {
        "pre_tool_policy": "aitp_v5_evaluate_pre_tool_policy",
        "preflight": "",
        "sequence": [
            "refresh_execution_brief",
            "evaluate_pre_tool_policy",
            "record_code_state",
            "refresh_execution_brief",
            "write_session_summary",
        ],
        "required_typed_refs": ["repo_id", "upstream_commit", "local_branch"],
        "allowed_state_sources": ["typed_records", "typed_code_state_records", "version_control_state"],
        "policy_reasons_field": "policy_reasons",
        "human_checkpoint_required": False,
        "truth_source": "typed_records",
        "summary_inputs_trusted": False,
    },
    "record_evidence": {
        "pre_tool_policy": "aitp_v5_evaluate_pre_tool_policy",
        "preflight": "",
        "sequence": [
            "refresh_execution_brief",
            "evaluate_pre_tool_policy",
            "record_evidence",
            "refresh_execution_brief",
            "write_session_summary",
        ],
        "required_typed_refs": ["topic_id", "claim_id"],
        "allowed_state_sources": ["typed_records", "typed_evidence_records", "typed_validation_records"],
        "policy_reasons_field": "policy_reasons",
        "human_checkpoint_required": False,
        "truth_source": "typed_records",
        "summary_inputs_trusted": False,
    },
    "record_tool_run": {
        "pre_tool_policy": "aitp_v5_evaluate_pre_tool_policy",
        "preflight": "",
        "sequence": [
            "refresh_execution_brief",
            "evaluate_pre_tool_policy",
            "record_tool_run",
            "refresh_execution_brief",
            "write_session_summary",
        ],
        "required_typed_refs": ["topic_id", "claim_id", "recipe_id"],
        "allowed_state_sources": ["typed_records", "typed_tool_run_records", "typed_validation_records"],
        "policy_reasons_field": "policy_reasons",
        "human_checkpoint_required": False,
        "truth_source": "typed_records",
        "summary_inputs_trusted": False,
    },
    "execute_tool": {
        "pre_tool_policy": "aitp_v5_evaluate_pre_tool_policy",
        "preflight": "",
        "sequence": [
            "refresh_execution_brief",
            "evaluate_pre_tool_policy",
            "execute_tool",
            "refresh_execution_brief",
            "write_session_summary",
        ],
        "required_typed_refs": ["topic_id", "claim_id", "executor_id"],
        "allowed_state_sources": [
            "typed_records",
            "typed_tool_run_records",
            "typed_code_state_records",
            "typed_validation_records",
        ],
        "policy_reasons_field": "policy_reasons",
        "human_checkpoint_required": False,
        "truth_source": "typed_records",
        "summary_inputs_trusted": False,
    },
    "register_tool_recipe": {
        "pre_tool_policy": "aitp_v5_evaluate_pre_tool_policy",
        "preflight": "",
        "sequence": [
            "refresh_execution_brief",
            "evaluate_pre_tool_policy",
            "register_tool_recipe",
            "refresh_execution_brief",
            "write_session_summary",
        ],
        "required_typed_refs": ["recipe_id", "tool_family", "tool_name"],
        "allowed_state_sources": [
            "typed_records",
            "typed_tool_recipe_records",
            "domain_pack",
            "human_instruction",
        ],
        "policy_reasons_field": "policy_reasons",
        "human_checkpoint_required": False,
        "truth_source": "typed_records",
        "summary_inputs_trusted": False,
    },
    "record_reference_location": {
        "pre_tool_policy": "aitp_v5_evaluate_pre_tool_policy",
        "preflight": "",
        "sequence": [
            "refresh_execution_brief",
            "evaluate_pre_tool_policy",
            "record_reference_location",
            "refresh_execution_brief",
            "write_session_summary",
        ],
        "required_typed_refs": ["topic_id", "connector_id", "uri"],
        "allowed_state_sources": [
            "typed_records",
            "typed_reference_locations",
            "external_connector_metadata",
        ],
        "policy_reasons_field": "policy_reasons",
        "human_checkpoint_required": False,
        "truth_source": "typed_records",
        "summary_inputs_trusted": False,
    },
    "record_physics_object": {
        "pre_tool_policy": "aitp_v5_evaluate_pre_tool_policy",
        "preflight": "",
        "sequence": [
            "refresh_execution_brief",
            "evaluate_pre_tool_policy",
            "record_physics_object",
            "refresh_execution_brief",
            "write_session_summary",
        ],
        "required_typed_refs": ["topic_id", "object_type", "name", "definition"],
        "allowed_state_sources": ["typed_records", "typed_physics_object_records", "typed_reference_locations"],
        "policy_reasons_field": "policy_reasons",
        "human_checkpoint_required": False,
        "truth_source": "typed_records",
        "summary_inputs_trusted": False,
    },
    "record_object_relation": {
        "pre_tool_policy": "aitp_v5_evaluate_pre_tool_policy",
        "preflight": "",
        "sequence": [
            "refresh_execution_brief",
            "evaluate_pre_tool_policy",
            "record_object_relation",
            "refresh_execution_brief",
            "write_session_summary",
        ],
        "required_typed_refs": ["topic_id", "relation_type", "subject_id", "object_id", "statement"],
        "allowed_state_sources": [
            "typed_records",
            "typed_physics_object_records",
            "typed_object_relation_records",
            "typed_evidence_records",
        ],
        "policy_reasons_field": "policy_reasons",
        "human_checkpoint_required": False,
        "truth_source": "typed_records",
        "summary_inputs_trusted": False,
    },
    "record_sensemaking_report": {
        "pre_tool_policy": "aitp_v5_evaluate_pre_tool_policy",
        "preflight": "",
        "sequence": [
            "refresh_execution_brief",
            "evaluate_pre_tool_policy",
            "record_sensemaking_report",
            "refresh_execution_brief",
            "write_session_summary",
        ],
        "required_typed_refs": ["topic_id", "claim_id", "title", "summary"],
        "allowed_state_sources": [
            "typed_records",
            "typed_sensemaking_reports",
            "typed_physics_object_records",
            "typed_object_relation_records",
            "typed_evidence_records",
        ],
        "policy_reasons_field": "policy_reasons",
        "human_checkpoint_required": False,
        "truth_source": "typed_records",
        "summary_inputs_trusted": False,
    },
    "ingest_subagent_result": {
        "pre_tool_policy": "aitp_v5_evaluate_pre_tool_policy",
        "preflight": "",
        "sequence": [
            "refresh_execution_brief",
            "evaluate_pre_tool_policy",
            "ingest_subagent_result",
            "refresh_execution_brief",
            "write_session_summary",
        ],
        "required_typed_refs": ["topic_id", "claim_id", "packet_id"],
        "allowed_state_sources": [
            "typed_records",
            "typed_subagent_packets",
            "typed_evidence_records",
            "typed_code_state_records",
        ],
        "policy_reasons_field": "policy_reasons",
        "human_checkpoint_required": False,
        "truth_source": "typed_records",
        "summary_inputs_trusted": False,
    },
    "create_validation_contract": {
        "pre_tool_policy": "aitp_v5_evaluate_pre_tool_policy",
        "preflight": "",
        "sequence": [
            "refresh_execution_brief",
            "evaluate_pre_tool_policy",
            "create_validation_contract",
            "refresh_execution_brief",
            "write_session_summary",
        ],
        "required_typed_refs": ["topic_id", "claim_id", "required_checks", "failure_modes"],
        "allowed_state_sources": [
            "typed_records",
            "typed_evidence_records",
            "typed_code_state_records",
        ],
        "policy_reasons_field": "policy_reasons",
        "human_checkpoint_required": False,
        "truth_source": "typed_records",
        "summary_inputs_trusted": False,
    },
    "record_validation_result": {
        "pre_tool_policy": "aitp_v5_evaluate_pre_tool_policy",
        "preflight": "",
        "sequence": [
            "refresh_execution_brief",
            "evaluate_pre_tool_policy",
            "record_validation_result",
            "refresh_execution_brief",
            "write_session_summary",
        ],
        "required_typed_refs": ["topic_id", "claim_id", "contract_id", "tool_run_id"],
        "allowed_state_sources": [
            "typed_records",
            "typed_validation_records",
            "typed_tool_run_records",
            "typed_evidence_records",
        ],
        "policy_reasons_field": "policy_reasons",
        "human_checkpoint_required": False,
        "truth_source": "typed_records",
        "summary_inputs_trusted": False,
    },
    "request_human_checkpoint": {
        "pre_tool_policy": "aitp_v5_evaluate_pre_tool_policy",
        "preflight": "",
        "sequence": [
            "refresh_execution_brief",
            "evaluate_pre_tool_policy",
            "request_human_checkpoint",
            "refresh_execution_brief",
            "write_session_summary",
        ],
        "required_typed_refs": ["topic_id", "claim_id", "reason", "requested_by"],
        "allowed_state_sources": ["typed_records", "human_checkpoint_request", "typed_checkpoint_records"],
        "policy_reasons_field": "policy_reasons",
        "human_checkpoint_required": False,
        "truth_source": "typed_records",
        "summary_inputs_trusted": False,
    },
    "decide_human_checkpoint": {
        "pre_tool_policy": "aitp_v5_evaluate_pre_tool_policy",
        "preflight": "",
        "sequence": [
            "refresh_execution_brief",
            "evaluate_pre_tool_policy",
            "decide_human_checkpoint",
            "refresh_execution_brief",
            "write_session_summary",
        ],
        "required_typed_refs": ["checkpoint_id", "decision", "rationale", "decided_by"],
        "allowed_state_sources": ["typed_checkpoint_records", "human_decision_input"],
        "policy_reasons_field": "policy_reasons",
        "human_checkpoint_required": False,
        "truth_source": "typed_records",
        "summary_inputs_trusted": False,
    },
    "validate_claim": {
        "pre_tool_policy": "aitp_v5_evaluate_pre_tool_policy",
        "preflight": "aitp_v5_preflight_trust_update",
        "sequence": [
            "refresh_execution_brief",
            "evaluate_pre_tool_policy",
            "preflight_trust_update",
            "record_validation_evidence",
            "refresh_execution_brief",
            "write_session_summary",
        ],
        "required_typed_refs": ["topic_id", "claim_id", "evidence_refs"],
        "allowed_state_sources": ["typed_evidence_records", "typed_validation_records"],
        "policy_reasons_field": "policy_reasons",
        "human_checkpoint_required": False,
        "truth_source": "typed_records",
        "summary_inputs_trusted": False,
    },
    "create_promotion_packet": {
        "pre_tool_policy": "aitp_v5_evaluate_pre_tool_policy",
        "preflight": "",
        "sequence": [
            "refresh_execution_brief",
            "evaluate_pre_tool_policy",
            "create_promotion_packet",
            "refresh_execution_brief",
            "write_session_summary",
        ],
        "required_typed_refs": ["topic_id", "claim_id", "proposed_memory_kind", "scope", "evidence_refs"],
        "allowed_state_sources": ["typed_records", "typed_evidence_records", "typed_validation_records"],
        "policy_reasons_field": "policy_reasons",
        "human_checkpoint_required": False,
        "truth_source": "typed_records",
        "summary_inputs_trusted": False,
    },
    "apply_promotion_packet": {
        "pre_tool_policy": "aitp_v5_evaluate_pre_tool_policy",
        "preflight": "",
        "sequence": [
            "refresh_execution_brief",
            "evaluate_pre_tool_policy",
            "human_checkpoint",
            "apply_promotion_packet",
            "refresh_execution_brief",
            "write_session_summary",
        ],
        "required_typed_refs": ["packet_id", "checkpoint_id"],
        "allowed_state_sources": ["typed_promotion_packet_records", "human_checkpoint", "typed_memory_records"],
        "policy_reasons_field": "policy_reasons",
        "human_checkpoint_required": True,
        "truth_source": "typed_records",
        "summary_inputs_trusted": False,
    },
    "promote_to_l2": {
        "pre_tool_policy": "aitp_v5_evaluate_pre_tool_policy",
        "preflight": "aitp_v5_preflight_trust_update",
        "sequence": [
            "refresh_execution_brief",
            "evaluate_pre_tool_policy",
            "preflight_trust_update",
            "human_checkpoint",
            "promote_to_l2",
        ],
        "required_typed_refs": ["topic_id", "claim_id", "evidence_refs", "validation_result_ref"],
        "allowed_state_sources": ["typed_evidence_records", "typed_validation_records", "human_checkpoint"],
        "policy_reasons_field": "policy_reasons",
        "human_checkpoint_required": True,
        "truth_source": "typed_records",
        "summary_inputs_trusted": False,
    },
}


def mandatory_gate_protocols() -> dict[str, Any]:
    """Return mandatory runtime gate protocols for adapter and hook bridges."""

    return deepcopy(_RUNTIME_GATE_PROTOCOLS)
