"""候選審核 queue AI summary Telegram dispatch report catalog record run readiness。

本模組只檢查 catalog record run package 後的 CLI 寫入準備度；不讀 approval
或 Telegram token、不呼叫 LLM、不派送 Telegram、不開 DB、不寫檔、不執行 CLI、
不寫 catalog record、不 commit、不更新 review_state、不掛 scheduler。
"""

from services.market_intel.candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_archive_summary import (
    SAFE_APPROVAL_ENV_VAR,
    SAFE_TOKEN_METADATA_KEYS,
    TARGET_COLUMN,
    TARGET_TABLE,
    _as_dict,
    _as_list,
    _contains_forbidden_token_key,
    _has_text,
    _safe_int,
    _safe_text,
    _strip_safe_token_boolean_keys,
)
from services.market_intel.candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_run_package import (
    FALSE_RESPONSE_KEYS as REPORT_CATALOG_RECORD_RUN_PACKAGE_FALSE_RESPONSE_KEYS,
)


REQUIRED_CATALOG_RECORD_RUN_PACKAGE_BOUNDARIES = {
    "do_not_read_approval_token_from_report_catalog_record_run_package_api",
    "do_not_read_telegram_token_from_report_catalog_record_run_package_api",
    "do_not_call_llm_from_report_catalog_record_run_package",
    "do_not_generate_report_from_report_catalog_record_run_package_api",
    "do_not_write_report_catalog_record_run_package_artifact_from_api",
    "do_not_execute_catalog_record_cli_from_api",
    "do_not_write_report_catalog_record_from_api",
    "do_not_dispatch_telegram_from_report_catalog_record_run_package_api",
    "do_not_open_database_connection_from_report_catalog_record_run_package",
    "do_not_update_review_state_from_report_catalog_record_run_package",
    "do_not_attach_scheduler_from_report_catalog_record_run_package",
    "future_market_intel_report_catalog_record_run_readiness_must_use_separate_gate",
}
FALSE_RESPONSE_KEYS = tuple(
    dict.fromkeys(
        REPORT_CATALOG_RECORD_RUN_PACKAGE_FALSE_RESPONSE_KEYS
        + (
            "telegram_dispatch_report_catalog_record_run_readiness_gate_file_written",
            "report_catalog_record_run_readiness_gate_file_written",
            "catalog_record_run_readiness_gate_file_written",
            "catalog_record_run_readiness_executed",
            "catalog_record_run_receipt_written",
            "catalog_record_postwrite_smoke_written",
        )
    )
)


def _run_package_summary(telegram_dispatch_report_catalog_record_run_package):
    run_package = _as_dict(telegram_dispatch_report_catalog_record_run_package)
    payload_manifest = _as_dict(run_package.get("payload_manifest"))
    source_artifacts = _as_dict(payload_manifest.get("source_artifacts"))
    command_bundle = _as_dict(run_package.get("command_bundle"))
    promotion = _as_dict(run_package.get("promotion_gate"))
    sections = _as_list(
        run_package.get("telegram_dispatch_report_catalog_record_run_package_sections")
    )
    safe_boundaries = set(str(item) for item in _as_list(run_package.get("safe_boundaries")))
    missing_boundaries = sorted(REQUIRED_CATALOG_RECORD_RUN_PACKAGE_BOUNDARIES - safe_boundaries)
    section_keys = sorted(
        str(item.get("key"))
        for item in sections
        if isinstance(item, dict) and item.get("key")
    )

    return {
        "provided": bool(run_package),
        "mode": run_package.get("mode"),
        "target_operation": run_package.get("target_operation"),
        "catalog_record_run_package_passed": bool(
            run_package.get("telegram_dispatch_report_catalog_record_run_package_passed")
            or run_package.get(
                "summary_persistence_telegram_dispatch_report_catalog_record_run_package_passed"
            )
            or run_package.get("report_catalog_record_run_package_passed")
        ),
        "ready_for_next_manual_phase": bool(run_package.get("ready_for_next_manual_phase")),
        "ready_for_market_intel_report_catalog_record_run_package": bool(
            run_package.get("ready_for_market_intel_report_catalog_record_run_package")
        ),
        "ready_for_market_intel_report_catalog_record_run_readiness": bool(
            run_package.get("ready_for_market_intel_report_catalog_record_run_readiness")
        ),
        "ready_for_market_intel_report_catalog_record_cli_run": bool(
            run_package.get("ready_for_market_intel_report_catalog_record_cli_run")
        ),
        "ready_for_market_intel_report_catalog_record_commit": bool(
            run_package.get("ready_for_market_intel_report_catalog_record_commit")
        ),
        "statement_count": _safe_int(run_package.get("statement_count")),
        "expected_summary_payload_hash": _safe_text(
            run_package.get("expected_summary_payload_hash"),
            80,
        ),
        "payload_manifest": payload_manifest,
        "target_table": payload_manifest.get("target_table"),
        "target_column": payload_manifest.get("target_column"),
        "record_family": payload_manifest.get("record_family"),
        "record_key_recorded": bool(payload_manifest.get("record_key_recorded")),
        "record_schema_recorded": bool(payload_manifest.get("record_schema_recorded")),
        "report_output_hash": payload_manifest.get("report_output_hash"),
        "manifest_expected_hash": payload_manifest.get("expected_summary_payload_hash"),
        "manifest_statement_count": _safe_int(payload_manifest.get("statement_count")),
        "source_record_write_gate": bool(source_artifacts.get("record_write_gate")),
        "source_write_preflight": bool(source_artifacts.get("write_preflight")),
        "source_report_output": bool(source_artifacts.get("report_output")),
        "source_backup": bool(source_artifacts.get("backup")),
        "source_dry_run": bool(source_artifacts.get("dry_run")),
        "manifest_execute_in_api": bool(payload_manifest.get("execute_in_api")),
        "command_script_path": command_bundle.get("script_path"),
        "command_approval_env_var": command_bundle.get("approval_env_var"),
        "dry_run_command": command_bundle.get("dry_run_command"),
        "real_run_command_requires_separate_gate": bool(
            command_bundle.get("real_run_command_requires_separate_gate")
        ),
        "api_must_not_execute_command": bool(command_bundle.get("api_must_not_execute_command")),
        "cli_command_recorded_by_operator": bool(
            command_bundle.get("cli_command_recorded_by_operator")
        ),
        "promotion_allowed": bool(promotion.get("allowed")),
        "promotion_next_manual_phase": promotion.get("next_manual_phase"),
        "promotion_requires_real_db_write": bool(promotion.get("requires_real_db_write")),
        "promotion_requires_cli_run": bool(promotion.get("requires_cli_run")),
        "promotion_requires_postwrite_smoke": bool(promotion.get("requires_postwrite_smoke")),
        "promotion_run_readiness_separate_gate": bool(
            promotion.get("report_catalog_record_run_readiness_requires_separate_gate")
        ),
        "promotion_commit_separate_gate": bool(
            promotion.get("report_catalog_record_commit_requires_separate_gate")
        ),
        "safe_boundaries_complete": not missing_boundaries,
        "missing_safe_boundaries": missing_boundaries,
        "section_keys": section_keys,
        "section_count": len(section_keys),
        "forbidden_token_key_detected": _contains_forbidden_token_key(
            _strip_safe_token_boolean_keys(run_package)
        ),
        "blocked_count": len(_as_list(run_package.get("blocked_reasons"))),
        **{key: bool(run_package.get(key)) for key in FALSE_RESPONSE_KEYS},
    }


def _operator_summary(operator_evidence):
    operator_evidence = _as_dict(operator_evidence)
    return {
        "provided_keys": sorted(operator_evidence.keys()),
        "report_catalog_record_run_readiness_artifact_path": _safe_text(
            operator_evidence.get("report_catalog_record_run_readiness_artifact_path")
            or operator_evidence.get(
                "telegram_dispatch_report_catalog_record_run_readiness_artifact_path"
            )
            or operator_evidence.get(
                "market_intel_report_catalog_record_run_readiness_artifact_path"
            )
        ),
        "report_catalog_record_run_package_artifact_path_recorded": _has_text(
            operator_evidence.get("report_catalog_record_run_package_artifact_path")
            or operator_evidence.get(
                "telegram_dispatch_report_catalog_record_run_package_artifact_path"
            )
        ),
        "catalog_record_payload_manifest_path_recorded": _has_text(
            operator_evidence.get("catalog_record_payload_manifest_path")
            or operator_evidence.get("report_catalog_record_payload_manifest_path")
        ),
        "catalog_record_backup_artifact_path_recorded": _has_text(
            operator_evidence.get("catalog_record_backup_artifact_path")
            or operator_evidence.get("report_catalog_backup_artifact_path")
            or operator_evidence.get("metadata_json_backup_artifact_path")
        ),
        "catalog_record_write_dry_run_artifact_path_recorded": _has_text(
            operator_evidence.get("catalog_record_write_dry_run_artifact_path")
            or operator_evidence.get("report_catalog_record_dry_run_artifact_path")
        ),
        "catalog_record_cli_command_recorded": _has_text(
            operator_evidence.get("catalog_record_cli_command")
            or operator_evidence.get("report_catalog_record_cli_command")
        ),
        "catalog_record_run_receipt_artifact_path_recorded": _has_text(
            operator_evidence.get("catalog_record_run_receipt_artifact_path")
            or operator_evidence.get("report_catalog_record_run_receipt_artifact_path")
        ),
        "catalog_record_postwrite_smoke_artifact_path_recorded": _has_text(
            operator_evidence.get("catalog_record_postwrite_smoke_artifact_path")
            or operator_evidence.get("report_catalog_record_postwrite_smoke_artifact_path")
        ),
        "operator_confirmed_report_catalog_record_run_readiness": bool(
            operator_evidence.get("operator_confirmed_report_catalog_record_run_readiness")
            or operator_evidence.get(
                "operator_confirmed_market_intel_report_catalog_record_run_readiness"
            )
        ),
        "operator_confirmed_report_catalog_record_run_package_reviewed": bool(
            operator_evidence.get(
                "operator_confirmed_report_catalog_record_run_package_reviewed"
            )
            or operator_evidence.get(
                "operator_confirmed_catalog_record_run_package_reviewed"
            )
        ),
        "operator_confirmed_catalog_record_payload_manifest_reviewed": bool(
            operator_evidence.get(
                "operator_confirmed_catalog_record_payload_manifest_reviewed"
            )
        ),
        "operator_confirmed_catalog_record_cli_command_reviewed": bool(
            operator_evidence.get("operator_confirmed_catalog_record_cli_command_reviewed")
            or operator_evidence.get("operator_confirmed_cli_command_reviewed")
        ),
        "operator_confirmed_catalog_record_dry_run_reviewed": bool(
            operator_evidence.get("operator_confirmed_catalog_record_dry_run_reviewed")
            or operator_evidence.get(
                "operator_confirmed_catalog_record_write_dry_run_reviewed"
            )
        ),
        "operator_confirmed_catalog_record_backup_available": bool(
            operator_evidence.get("operator_confirmed_catalog_record_backup_available")
            or operator_evidence.get("operator_confirmed_catalog_record_backup_required")
        ),
        "operator_confirmed_catalog_record_run_is_cli_only": bool(
            operator_evidence.get("operator_confirmed_catalog_record_run_is_cli_only")
            or operator_evidence.get("operator_confirmed_catalog_record_write_is_cli_only")
        ),
        "operator_confirmed_one_time_token_shell_only": bool(
            operator_evidence.get("operator_confirmed_one_time_token_shell_only")
            or operator_evidence.get("operator_acknowledged_shell_only_token")
        ),
        "operator_confirmed_report_catalog_record_run_receipt_required": bool(
            operator_evidence.get(
                "operator_confirmed_report_catalog_record_run_receipt_required"
            )
        ),
        "operator_confirmed_report_catalog_record_commit_requires_separate_gate": bool(
            operator_evidence.get(
                "operator_confirmed_report_catalog_record_commit_requires_separate_gate"
            )
        ),
        "operator_confirmed_postwrite_smoke_required": bool(
            operator_evidence.get("operator_confirmed_postwrite_smoke_required")
            or operator_evidence.get(
                "operator_confirmed_catalog_record_postwrite_smoke_required"
            )
        ),
        "operator_confirmed_no_token_in_report_catalog_record_run_readiness": bool(
            operator_evidence.get(
                "operator_confirmed_no_token_in_report_catalog_record_run_readiness"
            )
            or operator_evidence.get("operator_confirmed_no_token_in_artifacts")
        ),
        "operator_confirmed_no_api_file_write": bool(
            operator_evidence.get("operator_confirmed_no_api_file_write")
        ),
        "operator_confirmed_no_api_catalog_record_write": bool(
            operator_evidence.get("operator_confirmed_no_api_catalog_record_write")
            or operator_evidence.get("operator_confirmed_no_api_db_write")
        ),
        "operator_confirmed_no_api_telegram_dispatch": bool(
            operator_evidence.get("operator_confirmed_no_api_telegram_dispatch")
        ),
        "operator_confirmed_no_api_db_write": bool(
            operator_evidence.get("operator_confirmed_no_api_db_write")
        ),
        "operator_confirmed_no_llm_call": bool(
            operator_evidence.get("operator_confirmed_no_llm_call")
        ),
        "operator_confirmed_no_scheduler_attach": bool(
            operator_evidence.get("operator_confirmed_no_scheduler_attach")
        ),
        "catalog_record_run_readiness_notes_recorded": _has_text(
            operator_evidence.get("report_catalog_record_run_readiness_notes")
            or operator_evidence.get("catalog_record_run_readiness_notes")
        ),
        "safe_token_metadata_only": all(
            key in SAFE_TOKEN_METADATA_KEYS or key == "approval_env_var"
            for key in operator_evidence
            if "token" in str(key).lower() or str(key).lower() == "approval_env_var"
        )
        and operator_evidence.get("approval_env_var", SAFE_APPROVAL_ENV_VAR)
        == SAFE_APPROVAL_ENV_VAR,
        "forbidden_token_submitted_to_api": _contains_forbidden_token_key(
            operator_evidence
        ),
    }


def _run_readiness_manifest(summary):
    return {
        "manifest_version": "market_intel_report_catalog_record_run_readiness_v1",
        "source_mode": summary["mode"],
        "source_operation": summary["target_operation"],
        "target_table": TARGET_TABLE,
        "target_column": TARGET_COLUMN,
        "record_family": summary["record_family"],
        "report_output_hash": summary["report_output_hash"],
        "expected_summary_payload_hash": summary["expected_summary_payload_hash"],
        "statement_count": summary["statement_count"],
        "source_artifacts": {
            "record_write_gate": summary["source_record_write_gate"],
            "write_preflight": summary["source_write_preflight"],
            "report_output": summary["source_report_output"],
            "backup": summary["source_backup"],
            "dry_run": summary["source_dry_run"],
        },
        "manual_cli_run_command": {
            "script_path": summary["command_script_path"],
            "dry_run_command": summary["dry_run_command"],
            "real_run_requires_separate_gate": False,
            "executed_by_api": False,
            "writes_catalog_record": True,
            "requires_approval_env_var": SAFE_APPROVAL_ENV_VAR,
        },
        "execution_boundaries": {
            "api_writes_file": False,
            "api_executes_cli": False,
            "api_writes_catalog_record": False,
            "api_writes_database": False,
            "api_dispatches_telegram": False,
            "api_calls_llm": False,
            "api_attaches_scheduler": False,
        },
    }


def _run_readiness_sections(summary, operator, manifest):
    return [
        {
            "key": "catalog_record_run_readiness_identity",
            "title": "Catalog record run readiness identity",
            "facts": [
                f"family={summary['record_family'] or 'missing'}",
                f"manifest={manifest['manifest_version']}",
                f"statement_count={summary['statement_count']}",
            ],
        },
        {
            "key": "catalog_record_run_readiness_artifacts",
            "title": "Catalog record run readiness artifacts",
            "facts": [
                f"run_package={operator['report_catalog_record_run_package_artifact_path_recorded']}",
                f"readiness={bool(operator['report_catalog_record_run_readiness_artifact_path'])}",
                f"backup={operator['catalog_record_backup_artifact_path_recorded']}",
                f"dry_run={operator['catalog_record_write_dry_run_artifact_path_recorded']}",
            ],
        },
        {
            "key": "catalog_record_run_readiness_command",
            "title": "Catalog record run readiness command",
            "facts": [
                f"script={summary['command_script_path'] or 'missing'}",
                f"cli_command={operator['catalog_record_cli_command_recorded']}",
                "executed_by_api=false",
            ],
        },
        {
            "key": "catalog_record_run_readiness_safety",
            "title": "Catalog record run readiness safety",
            "facts": [
                "run_readiness_file_written=false",
                "cli_executed=false",
                "database_write_executed=false",
                "scheduler_attached=false",
            ],
        },
    ]


def _run_readiness_gates(summary, operator, manifest, apply_real_write):
    required_sections = {
        "catalog_record_run_package_identity",
        "catalog_record_payload_manifest",
        "catalog_record_command_bundle",
        "catalog_record_run_package_safety",
    }
    section_keys = set(summary["section_keys"])
    return [
        {
            "key": "report_catalog_record_run_package_preview_provided",
            "label": "必須提供上一階段 report catalog record run package preview",
            "passed": bool(
                summary["provided"]
                and summary["mode"]
                == "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_run_package_preview"
            ),
        },
        {
            "key": "report_catalog_record_run_package_passed",
            "label": "report catalog record run package gate 必須已通過",
            "passed": summary["catalog_record_run_package_passed"],
        },
        {
            "key": "report_catalog_record_run_package_ready_for_run_readiness",
            "label": "run package 只能放行到 catalog record run readiness",
            "passed": bool(
                summary["ready_for_market_intel_report_catalog_record_run_package"]
                and summary["ready_for_market_intel_report_catalog_record_run_readiness"]
                and summary["ready_for_next_manual_phase"]
                and not summary["ready_for_market_intel_report_catalog_record_cli_run"]
                and not summary["ready_for_market_intel_report_catalog_record_commit"]
                and summary["promotion_allowed"]
                and summary["promotion_next_manual_phase"]
                == "market_intel_report_catalog_record_run_readiness"
                and summary["promotion_requires_real_db_write"]
                and summary["promotion_requires_cli_run"]
                and summary["promotion_requires_postwrite_smoke"]
                and summary["promotion_run_readiness_separate_gate"]
                and summary["promotion_commit_separate_gate"]
            ),
        },
        {
            "key": "catalog_record_run_package_payload_manifest_complete",
            "label": "payload manifest 必須保留 target、record family/key/schema、report hash 與 source artifacts",
            "passed": bool(
                summary["target_table"] == TARGET_TABLE
                and summary["target_column"] == TARGET_COLUMN
                and summary["record_family"]
                and summary["record_key_recorded"]
                and summary["record_schema_recorded"]
                and summary["report_output_hash"]
                and summary["expected_summary_payload_hash"]
                and summary["manifest_expected_hash"]
                and summary["statement_count"] > 0
                and summary["manifest_statement_count"] == summary["statement_count"]
                and summary["source_record_write_gate"]
                and summary["source_write_preflight"]
                and summary["source_report_output"]
                and summary["source_backup"]
                and summary["source_dry_run"]
                and not summary["manifest_execute_in_api"]
            ),
        },
        {
            "key": "catalog_record_run_package_command_bundle_ready",
            "label": "command bundle 必須記錄 script、dry-run、operator CLI command，且 API 不得執行",
            "passed": bool(
                summary["command_script_path"]
                == "scripts/market_intel/write_report_catalog_record.py"
                and summary["command_approval_env_var"] == SAFE_APPROVAL_ENV_VAR
                and summary["dry_run_command"]
                and summary["real_run_command_requires_separate_gate"]
                and summary["api_must_not_execute_command"]
                and summary["cli_command_recorded_by_operator"]
                and manifest["manual_cli_run_command"]["executed_by_api"] is False
            ),
        },
        {
            "key": "catalog_record_run_package_sections_complete",
            "label": "run package sections 與 readiness manifest 必須完整",
            "passed": bool(
                required_sections.issubset(section_keys)
                and manifest["manifest_version"]
                == "market_intel_report_catalog_record_run_readiness_v1"
            ),
        },
        {
            "key": "report_catalog_record_run_package_runtime_boundaries_clear",
            "label": "run package payload 不得顯示 API 寫檔、DB、Telegram、LLM、CLI 或 scheduler 副作用",
            "passed": all(not summary[key] for key in FALSE_RESPONSE_KEYS),
        },
        {
            "key": "report_catalog_record_run_package_safe_boundaries_complete",
            "label": "run package 必須保留 run readiness separate gate 與 runtime 安全邊界",
            "passed": summary["safe_boundaries_complete"],
        },
        {
            "key": "operator_confirmed_report_catalog_record_run_readiness",
            "label": "操作員確認 run readiness、run package、manifest、CLI command、backup、dry-run、receipt 與 smoke",
            "passed": bool(
                operator["report_catalog_record_run_readiness_artifact_path"]
                and operator["report_catalog_record_run_package_artifact_path_recorded"]
                and operator["catalog_record_payload_manifest_path_recorded"]
                and operator["catalog_record_backup_artifact_path_recorded"]
                and operator["catalog_record_write_dry_run_artifact_path_recorded"]
                and operator["catalog_record_cli_command_recorded"]
                and operator["catalog_record_run_receipt_artifact_path_recorded"]
                and operator["catalog_record_postwrite_smoke_artifact_path_recorded"]
                and operator["operator_confirmed_report_catalog_record_run_readiness"]
                and operator["operator_confirmed_report_catalog_record_run_package_reviewed"]
                and operator["operator_confirmed_catalog_record_payload_manifest_reviewed"]
                and operator["operator_confirmed_catalog_record_cli_command_reviewed"]
                and operator["operator_confirmed_catalog_record_dry_run_reviewed"]
                and operator["operator_confirmed_catalog_record_backup_available"]
                and operator["operator_confirmed_catalog_record_run_is_cli_only"]
                and operator["operator_confirmed_one_time_token_shell_only"]
                and operator["operator_confirmed_report_catalog_record_run_receipt_required"]
                and operator[
                    "operator_confirmed_report_catalog_record_commit_requires_separate_gate"
                ]
                and operator["operator_confirmed_postwrite_smoke_required"]
            ),
        },
        {
            "key": "operator_confirmed_report_catalog_record_run_readiness_runtime_boundaries",
            "label": "操作員確認本 API 不寫檔、不執行 CLI、不寫 DB、不派送 Telegram、不呼叫 LLM、不掛 scheduler",
            "passed": bool(
                operator[
                    "operator_confirmed_no_token_in_report_catalog_record_run_readiness"
                ]
                and operator["operator_confirmed_no_api_file_write"]
                and operator["operator_confirmed_no_api_catalog_record_write"]
                and operator["operator_confirmed_no_api_telegram_dispatch"]
                and operator["operator_confirmed_no_api_db_write"]
                and operator["operator_confirmed_no_llm_call"]
                and operator["operator_confirmed_no_scheduler_attach"]
            ),
        },
        {
            "key": "report_catalog_record_run_readiness_no_token_submitted_to_api",
            "label": "catalog record run readiness payload 不得包含 approval 或 Telegram token key",
            "passed": not operator["forbidden_token_submitted_to_api"]
            and not summary["forbidden_token_key_detected"],
        },
        {
            "key": "report_catalog_record_run_readiness_apply_real_write_not_requested_from_api",
            "label": "API/UI report catalog record run readiness 不接受 apply_real_write",
            "passed": not apply_real_write,
        },
    ]


def build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_run_readiness(
    *,
    telegram_dispatch_report_catalog_record_run_package,
    operator_evidence=None,
    execute_requested=False,
    apply_real_write=False,
):
    """建立 Telegram dispatch report catalog record run readiness；不執行副作用。"""
    summary = _run_package_summary(telegram_dispatch_report_catalog_record_run_package)
    operator = _operator_summary(operator_evidence)
    manifest = _run_readiness_manifest(summary)
    sections = _run_readiness_sections(summary, operator, manifest)
    gates = _run_readiness_gates(summary, operator, manifest, bool(apply_real_write))
    blocked_reasons = [gate["key"] for gate in gates if not gate["passed"]]
    readiness_passed = bool(not blocked_reasons)

    return {
        "mode": "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_run_readiness_preview",
        "target_table": TARGET_TABLE,
        "target_column": TARGET_COLUMN,
        "target_json_path": [
            "market_intel_ai_summary",
            "telegram_dispatch_report_catalog_record_run_readiness",
        ],
        "target_operation": "manual_market_intel_report_catalog_record_run_readiness",
        "execute_requested": bool(execute_requested),
        "apply_real_write_requested": bool(apply_real_write),
        "report_catalog_record_run_readiness_reviewed": True,
        "telegram_dispatch_report_catalog_record_run_readiness_passed": readiness_passed,
        "summary_persistence_telegram_dispatch_report_catalog_record_run_readiness_passed": readiness_passed,
        "report_catalog_record_run_readiness_passed": readiness_passed,
        "ready_for_next_manual_phase": readiness_passed,
        "ready_for_cli_operator_run": readiness_passed,
        "ready_for_market_intel_report_catalog_record_cli_operator_run": readiness_passed,
        "ready_for_market_intel_report_catalog_record_run_receipt": readiness_passed,
        "ready_for_market_intel_report_catalog_record_cli_run": False,
        "ready_for_market_intel_report_catalog_record_commit": False,
        "ready_for_real_write": False,
        "ready_for_report_generation": False,
        "ready_for_telegram_dispatch": False,
        "ready_for_api_database_write": False,
        "ready_for_scheduler_attach": False,
        "ready_for_llm_call": False,
        "api_executes_cli": False,
        "api_executes_llm": False,
        "api_dispatches_telegram": False,
        "api_reads_approval_token": False,
        "api_writes_file": False,
        "api_writes_database": False,
        "api_updates_review_state": False,
        **{key: False for key in FALSE_RESPONSE_KEYS},
        "statement_count": summary["statement_count"],
        "expected_summary_payload_hash": summary["expected_summary_payload_hash"],
        "blocked_reasons": blocked_reasons,
        "gates": gates,
        "telegram_dispatch_report_catalog_record_run_package": summary,
        "telegram_dispatch_report_catalog_record_run_readiness_sections": sections,
        "operator_telegram_dispatch_report_catalog_record_run_readiness": operator,
        "catalog_record_run_readiness_manifest": manifest,
        "promotion_gate": {
            "allowed": readiness_passed,
            "next_manual_phase": "market_intel_report_catalog_record_run_receipt",
            "requires_real_db_write": True,
            "requires_cli_run": True,
            "requires_scheduler_attach": False,
            "requires_operator_approval": True,
            "requires_postwrite_smoke": True,
            "future_catalog_record_run_result_requires_receipt": True,
            "api_must_not_generate_report": True,
            "api_must_not_write_file": True,
            "api_must_not_execute_cli": True,
            "api_must_not_write_catalog_record": True,
            "api_must_not_write_database": True,
            "api_must_not_dispatch_telegram": True,
            "report_catalog_record_run_receipt_requires_separate_gate": True,
            "report_catalog_record_commit_requires_separate_gate": True,
        },
        "next_operator_steps": [
            "保存 report catalog record run readiness artifact path",
            "只在 CLI shell 注入一次性 approval token；API/UI 不讀 token",
            "由操作員或獨立 job 執行 catalog record CLI，並保存 run receipt",
            "CLI 後立即保存 postwrite smoke 結果；下一階段 run receipt 才審核真實寫入結果",
        ],
        "safe_boundaries": [
            "do_not_read_approval_token_from_report_catalog_record_run_readiness_api",
            "do_not_read_telegram_token_from_report_catalog_record_run_readiness_api",
            "do_not_call_llm_from_report_catalog_record_run_readiness",
            "do_not_generate_report_from_report_catalog_record_run_readiness_api",
            "do_not_write_report_catalog_record_run_readiness_artifact_from_api",
            "do_not_execute_catalog_record_cli_from_report_catalog_record_run_readiness_api",
            "do_not_write_report_catalog_record_from_report_catalog_record_run_readiness_api",
            "do_not_dispatch_telegram_from_report_catalog_record_run_readiness_api",
            "do_not_open_database_connection_from_report_catalog_record_run_readiness",
            "do_not_update_review_state_from_report_catalog_record_run_readiness",
            "do_not_attach_scheduler_from_report_catalog_record_run_readiness",
            "future_market_intel_report_catalog_record_run_receipt_must_use_separate_gate",
            "report_catalog_record_run_readiness_preview_only",
            "no_remove_orphans",
            "no_momo_db_lifecycle_change",
        ],
    }
