diff --git a/TODO_NEXT_STEPS.txt b/TODO_NEXT_STEPS.txt index ee51a85..d3ea833 100644 --- a/TODO_NEXT_STEPS.txt +++ b/TODO_NEXT_STEPS.txt @@ -4,6 +4,7 @@ ================================================================================ 【已完成】 + - V10.356 補市場情報 candidate queue review AI summary Telegram dispatch report catalog record final closeout gate:新增 read-only report catalog record final closeout builder、POST endpoint、UI 按鈕與 deployment readiness smoke target,在 archive summary gate 後覆核 catalog record identity、artifact traceability、sections、DB commit/post-write smoke、pipeline complete 與無後續 follow-up;API/UI 不讀 approval/Telegram token、不呼叫 LLM、不產報表、不派送 Telegram、不開 DB、不寫檔、不執行 CLI、不寫 catalog record、不 commit、不更新 review_state、不掛 scheduler。 - V10.355 補市場情報 candidate queue review AI summary Telegram dispatch report catalog record archive summary gate:新增 read-only report catalog record archive summary builder、POST endpoint、UI 按鈕與 deployment readiness smoke target,在 archive gate 後整理 catalog record identity、artifact traceability、DB commit/post-write smoke、archive manifest/retention policy 與後續 final closeout separate gate;API/UI 不讀 approval/Telegram token、不呼叫 LLM、不產報表、不派送 Telegram、不開 DB、不寫檔、不執行 CLI、不寫 catalog record、不 commit、不更新 review_state、不掛 scheduler。 - V10.354 補市場情報 candidate queue review AI summary Telegram dispatch report catalog record archive gate:新增 read-only report catalog record archive builder、POST endpoint、UI 按鈕與 deployment readiness smoke target,在 closeout gate 後審核 closeout/commit/run receipt/writer output/post-write smoke/backup 封存證據、archive manifest/retention policy 與後續 archive summary separate gate;API/UI 不讀 approval/Telegram token、不呼叫 LLM、不產報表、不派送 Telegram、不開 DB、不寫檔、不執行 CLI、不寫 catalog record、不 commit、不更新 review_state、不掛 scheduler。 - V10.353 補市場情報 candidate queue review AI summary Telegram dispatch report catalog record closeout gate:新增 read-only report catalog record closeout builder、POST endpoint、UI 按鈕與 deployment readiness smoke target,在 commit gate 後審核 catalog record identity、DB commit/post-write smoke 證據、操作員 closeout 確認與後續 archive separate gate;API/UI 不讀 approval/Telegram token、不呼叫 LLM、不補產報表、不派送 Telegram、不開 DB、不寫檔、不執行 CLI、不寫 catalog record、不 commit、不更新 review_state、不掛 scheduler。 diff --git a/config.py b/config.py index b87e7b7..62edb28 100644 --- a/config.py +++ b/config.py @@ -323,7 +323,7 @@ YOUTUBE_API_KEY = os.getenv('YOUTUBE_API_KEY', '') # ========================================== # 系統版本與路徑 # ========================================== -SYSTEM_VERSION = "V10.355" +SYSTEM_VERSION = "V10.356" LOG_FILE_PATH = os.path.join(BASE_DIR, 'logs/system.log') public_url = PUBLIC_URL # 用於模板顯示 diff --git a/docs/memory/history_logs.md b/docs/memory/history_logs.md index fa9b3bf..026b4a1 100644 --- a/docs/memory/history_logs.md +++ b/docs/memory/history_logs.md @@ -12,6 +12,10 @@ ## 📅 詳細更新日誌 (考古存檔) +### 2026-05-21:市場情報 Telegram dispatch report catalog record final closeout gate +- **V10.356 report catalog record final closeout gate**: 新增 `candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout` service、POST endpoint、UI 按鈕與 deployment readiness smoke target,在 archive summary gate 後覆核 catalog record identity、artifact traceability、sections、DB commit/post-write smoke、pipeline complete 與無後續 follow-up。 +- **只讀安全邊界**: 本階段是 catalog record pipeline terminal preview;API/UI 不讀 approval/Telegram token、不呼叫 LLM、不產報表、不派送 Telegram、不開 DB、不寫檔、不執行 CLI、不寫 catalog record、不 commit、不更新 `review_state`、不掛 scheduler。 + ### 2026-05-21:市場情報 Telegram dispatch report catalog record archive summary gate - **V10.355 report catalog record archive summary gate**: 新增 `candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary` service、POST endpoint、UI 按鈕與 deployment readiness smoke target,在 archive gate 後整理 catalog record identity、artifact traceability、DB commit/post-write smoke、archive manifest/retention policy 與後續 final closeout separate gate。 - **只讀安全邊界**: 本階段只放行到後續 report catalog record final closeout gate;API/UI 不讀 approval/Telegram token、不呼叫 LLM、不產報表、不派送 Telegram、不開 DB、不寫檔、不執行 CLI、不寫 catalog record、不 commit、不更新 `review_state`、不掛 scheduler。 diff --git a/routes/market_intel_review_report_routes.py b/routes/market_intel_review_report_routes.py index 0c9f7ff..399e88c 100644 --- a/routes/market_intel_review_report_routes.py +++ b/routes/market_intel_review_report_routes.py @@ -55,6 +55,9 @@ from services.market_intel.candidate_queue_review_ai_summary_persistence_telegra from services.market_intel.candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary import ( build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary, ) +from services.market_intel.candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout import ( + build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout, +) from services.market_intel.candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_closeout import ( build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_closeout, ) @@ -803,6 +806,61 @@ def _build_ai_summary_persistence_telegram_dispatch_report_catalog_record_archiv ) +def _extract_report_catalog_record_final_closeout_payload( + sample_result, + operator_evidence, +): + operator_evidence = _as_dict(operator_evidence) + sample_result = _as_dict(sample_result) + return _as_dict( + operator_evidence.get("market_intel_report_catalog_record_final_closeout") + or operator_evidence.get( + "telegram_dispatch_report_catalog_record_final_closeout" + ) + or operator_evidence.get("report_catalog_record_final_closeout") + or operator_evidence.get("catalog_record_final_closeout") + or sample_result.get("market_intel_report_catalog_record_final_closeout") + or sample_result.get("telegram_dispatch_report_catalog_record_final_closeout") + or sample_result.get("report_catalog_record_final_closeout") + or sample_result.get("catalog_record_final_closeout") + ) + + +def _build_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout( + *, + service, + sample_result, + operator_evidence, + writer_output, + smoke_result, + payload_error, + limit, + execute_requested, + apply_real_write, +): + report_catalog_record_archive_summary = _build_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary( + service=service, + sample_result=sample_result, + operator_evidence=operator_evidence, + writer_output=writer_output, + smoke_result=smoke_result, + payload_error=payload_error, + limit=limit, + execute_requested=execute_requested, + apply_real_write=apply_real_write, + ) + return build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout( + telegram_dispatch_report_catalog_record_archive_summary=report_catalog_record_archive_summary, + report_catalog_record_final_closeout=_extract_report_catalog_record_final_closeout_payload( + sample_result, + operator_evidence, + ), + operator_evidence=operator_evidence, + execute_requested=execute_requested, + apply_real_write=apply_real_write, + ) + + @market_intel_review_bp.route( "/api/market_intel/manual_sample_review/" "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_input", @@ -1317,3 +1375,31 @@ def market_intel_manual_sample_candidate_queue_review_ai_summary_persistence_tel ) data["phase"] = service.phase return jsonify(data), 400 if payload_error else 200 + + +@market_intel_review_bp.route( + "/api/market_intel/manual_sample_review/" + "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout", + methods=["POST"], +) +@login_required +def market_intel_manual_sample_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout(): + service = MarketIntelService() + execute_requested = request.args.get("execute", "false").lower() == "true" + apply_real_write = request.args.get("apply_real_write", "false").lower() == "true" + sample_result, operator_evidence, writer_output, smoke_result, payload_error, limit = ( + _extract_run_payload() + ) + data = _build_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout( + service=service, + sample_result=sample_result, + operator_evidence=operator_evidence, + writer_output=writer_output, + smoke_result=smoke_result, + payload_error=payload_error, + limit=limit, + execute_requested=execute_requested, + apply_real_write=apply_real_write, + ) + data["phase"] = service.phase + return jsonify(data), 400 if payload_error else 200 diff --git a/services/market_intel/candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout.py b/services/market_intel/candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout.py new file mode 100644 index 0000000..2271819 --- /dev/null +++ b/services/market_intel/candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout.py @@ -0,0 +1,683 @@ +"""候選審核 queue AI summary Telegram dispatch report catalog record final closeout gate。 + +本模組只在 catalog record archive summary 通過後整理終局收尾證據;不讀 +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_catalog_record_archive_summary import ( + FALSE_RESPONSE_KEYS as REPORT_CATALOG_RECORD_ARCHIVE_SUMMARY_FALSE_RESPONSE_KEYS, + 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, +) + + +REQUIRED_ARCHIVE_SUMMARY_BOUNDARIES = { + "do_not_read_approval_token_from_report_catalog_record_archive_summary_api", + "do_not_read_telegram_token_from_report_catalog_record_archive_summary_api", + "do_not_call_llm_from_report_catalog_record_archive_summary", + "do_not_generate_report_from_report_catalog_record_archive_summary_api", + "do_not_write_report_catalog_record_archive_summary_artifact_from_api", + "do_not_execute_catalog_record_cli_from_report_catalog_record_archive_summary_api", + "do_not_write_report_catalog_record_from_report_catalog_record_archive_summary_api", + "do_not_commit_catalog_record_from_report_catalog_record_archive_summary_api", + "do_not_dispatch_telegram_from_report_catalog_record_archive_summary_api", + "do_not_open_database_connection_from_report_catalog_record_archive_summary", + "do_not_update_review_state_from_report_catalog_record_archive_summary", + "do_not_attach_scheduler_from_report_catalog_record_archive_summary", + "future_market_intel_report_catalog_record_final_closeout_must_use_separate_gate", +} +FALSE_RESPONSE_KEYS = tuple( + dict.fromkeys( + REPORT_CATALOG_RECORD_ARCHIVE_SUMMARY_FALSE_RESPONSE_KEYS + + ( + "telegram_dispatch_report_catalog_record_final_closeout_gate_file_written", + "report_catalog_record_final_closeout_gate_file_written", + "catalog_record_final_closeout_gate_file_written", + "telegram_dispatch_report_catalog_record_final_closeout_file_written", + "report_catalog_record_final_closeout_file_written", + "catalog_record_final_closeout_file_written", + "catalog_record_final_closeout_record_written", + "catalog_record_final_closeout_executed", + "catalog_record_pipeline_completion_record_written", + "market_intel_catalog_record_pipeline_completed_by_api", + ) + ) +) + + +def _archive_summary_snapshot(telegram_dispatch_report_catalog_record_archive_summary): + summary = _as_dict(telegram_dispatch_report_catalog_record_archive_summary) + archive = _as_dict(summary.get("telegram_dispatch_report_catalog_record_archive")) + operator = _as_dict( + summary.get("operator_telegram_dispatch_report_catalog_record_archive_summary") + ) + promotion = _as_dict(summary.get("promotion_gate")) + sections = _as_list( + summary.get("telegram_dispatch_report_catalog_record_archive_summary_sections") + ) + section_keys = { + str(section.get("key")) + for section in sections + if isinstance(section, dict) and section.get("key") + } + safe_boundaries = set(str(item) for item in _as_list(summary.get("safe_boundaries"))) + missing_boundaries = sorted(REQUIRED_ARCHIVE_SUMMARY_BOUNDARIES - safe_boundaries) + blocked_keys = [key for key in FALSE_RESPONSE_KEYS if bool(summary.get(key))] + + return { + "provided": bool(summary), + "mode": summary.get("mode"), + "target_operation": summary.get("target_operation"), + "archive_summary_passed": bool( + summary.get("telegram_dispatch_report_catalog_record_archive_summary_passed") + or summary.get( + "summary_persistence_telegram_dispatch_report_catalog_record_archive_summary_passed" + ) + or summary.get("report_catalog_record_archive_summary_passed") + ), + "ready_for_next_manual_phase": bool(summary.get("ready_for_next_manual_phase")), + "ready_for_market_intel_report_catalog_record_final_closeout": bool( + summary.get("ready_for_market_intel_report_catalog_record_final_closeout") + ), + "ready_for_market_intel_report_catalog_record_archive_summary": bool( + summary.get("ready_for_market_intel_report_catalog_record_archive_summary") + ), + "statement_count": _safe_int(summary.get("statement_count")), + "expected_summary_payload_hash": _safe_text( + summary.get("expected_summary_payload_hash") + or archive.get("expected_summary_payload_hash"), + 80, + ), + "writer_record_family": _safe_text(archive.get("writer_record_family"), 80), + "writer_catalog_record_key": _safe_text( + archive.get("writer_catalog_record_key"), 160 + ), + "writer_target_table": archive.get("writer_target_table"), + "writer_target_column": archive.get("writer_target_column"), + "writer_statement_count": _safe_int(archive.get("writer_statement_count")), + "writer_database_commit_executed": bool( + archive.get("writer_database_commit_executed") + ), + "writer_hash_match": bool(archive.get("writer_hash_match")), + "smoke_read_only_query_executed": bool( + archive.get("smoke_read_only_query_executed") + ), + "smoke_postwrite_smoke_passed": bool( + archive.get("smoke_postwrite_smoke_passed") + ), + "smoke_hash_match": bool(archive.get("smoke_hash_match")), + "archive_passed": bool(archive.get("archive_passed")), + "archive_artifact_path": archive.get("archive_artifact_path"), + "archive_closeout_artifact_path_recorded": bool( + archive.get("archive_closeout_artifact_path_recorded") + ), + "archive_commit_artifact_path_recorded": bool( + archive.get("archive_commit_artifact_path_recorded") + ), + "archive_run_receipt_artifact_path_recorded": bool( + archive.get("archive_run_receipt_artifact_path_recorded") + ), + "archive_writer_output_artifact_path_recorded": bool( + archive.get("archive_writer_output_artifact_path_recorded") + ), + "archive_postwrite_smoke_artifact_path_recorded": bool( + archive.get("archive_postwrite_smoke_artifact_path_recorded") + ), + "archive_backup_artifact_path_recorded": bool( + archive.get("archive_backup_artifact_path_recorded") + ), + "operator_summary_artifact_path": operator.get( + "report_catalog_record_archive_summary_artifact_path" + ), + "operator_archive_artifact_path_recorded": bool( + operator.get("report_catalog_record_archive_artifact_path_recorded") + ), + "operator_archive_summary_gate": bool( + operator.get("operator_confirmed_report_catalog_record_archive_summary_gate") + ), + "operator_archive_reviewed": bool( + operator.get("operator_confirmed_report_catalog_record_archive_reviewed") + ), + "operator_traceability_reviewed": bool( + operator.get("operator_confirmed_catalog_record_traceability_reviewed") + ), + "operator_archive_summary_read_only": bool( + operator.get("operator_confirmed_catalog_record_archive_summary_read_only") + ), + "operator_final_closeout_requires_separate_gate": bool( + operator.get( + "operator_confirmed_catalog_record_final_closeout_requires_separate_gate" + ) + ), + "section_keys": sorted(section_keys), + "summary_sections_complete": { + "catalog_record_archive_summary_identity", + "catalog_record_archive_summary_traceability", + "catalog_record_archive_summary_integrity", + "catalog_record_archive_summary_safety", + }.issubset(section_keys), + "promotion_allowed": bool(promotion.get("allowed")), + "promotion_next_manual_phase": promotion.get("next_manual_phase"), + "promotion_final_closeout_separate_gate": bool( + promotion.get("report_catalog_record_final_closeout_requires_separate_gate") + ), + "promotion_api_must_not_generate_report": bool( + promotion.get("api_must_not_generate_report") + ), + "promotion_api_must_not_write_file": bool( + promotion.get("api_must_not_write_file") + ), + "promotion_api_must_not_execute_cli": bool( + promotion.get("api_must_not_execute_cli") + ), + "promotion_api_must_not_write_catalog_record": bool( + promotion.get("api_must_not_write_catalog_record") + ), + "promotion_api_must_not_write_database": bool( + promotion.get("api_must_not_write_database") + ), + "promotion_api_must_not_commit_catalog_record": bool( + promotion.get("api_must_not_commit_catalog_record") + ), + "promotion_api_must_not_dispatch_telegram": bool( + promotion.get("api_must_not_dispatch_telegram") + ), + "safe_boundaries_complete": not missing_boundaries, + "missing_safe_boundaries": missing_boundaries, + "forbidden_token_key_detected": _contains_forbidden_token_key( + _strip_safe_token_boolean_keys(summary) + ), + "blocked_response_keys_true": blocked_keys, + **{key: bool(summary.get(key)) for key in FALSE_RESPONSE_KEYS}, + } + + +def _operator_summary(operator_evidence, report_catalog_record_final_closeout=None): + operator_evidence = _as_dict(operator_evidence) + closeout_payload = _as_dict(report_catalog_record_final_closeout) + return { + "provided_keys": sorted(operator_evidence.keys()), + "report_catalog_record_final_closeout_artifact_path": _safe_text( + operator_evidence.get("report_catalog_record_final_closeout_artifact_path") + or operator_evidence.get( + "telegram_dispatch_report_catalog_record_final_closeout_artifact_path" + ) + or operator_evidence.get( + "market_intel_report_catalog_record_final_closeout_artifact_path" + ) + or closeout_payload.get("report_catalog_record_final_closeout_artifact_path"), + 300, + ), + "report_catalog_record_archive_summary_artifact_path_recorded": _has_text( + operator_evidence.get("report_catalog_record_archive_summary_artifact_path") + or closeout_payload.get("report_catalog_record_archive_summary_artifact_path") + ), + "report_catalog_record_archive_artifact_path_recorded": _has_text( + operator_evidence.get("report_catalog_record_archive_artifact_path") + or closeout_payload.get("report_catalog_record_archive_artifact_path") + ), + "report_catalog_record_closeout_artifact_path_recorded": _has_text( + operator_evidence.get("report_catalog_record_closeout_artifact_path") + or closeout_payload.get("report_catalog_record_closeout_artifact_path") + ), + "report_catalog_record_commit_artifact_path_recorded": _has_text( + operator_evidence.get("report_catalog_record_commit_artifact_path") + or closeout_payload.get("report_catalog_record_commit_artifact_path") + ), + "report_catalog_record_run_receipt_artifact_path_recorded": _has_text( + operator_evidence.get("report_catalog_record_run_receipt_artifact_path") + or closeout_payload.get("report_catalog_record_run_receipt_artifact_path") + ), + "catalog_record_writer_output_artifact_path_recorded": _has_text( + operator_evidence.get("catalog_record_writer_output_artifact_path") + or operator_evidence.get("writer_output_json_path") + or closeout_payload.get("catalog_record_writer_output_artifact_path") + ), + "catalog_record_postwrite_smoke_artifact_path_recorded": _has_text( + operator_evidence.get("catalog_record_postwrite_smoke_artifact_path") + or operator_evidence.get("postwrite_smoke_json_path") + or closeout_payload.get("catalog_record_postwrite_smoke_artifact_path") + ), + "catalog_record_backup_artifact_path_recorded": _has_text( + operator_evidence.get("catalog_record_backup_artifact_path") + or operator_evidence.get("backup_artifact_path") + or closeout_payload.get("catalog_record_backup_artifact_path") + ), + "operator_confirmed_report_catalog_record_final_closeout_gate": bool( + operator_evidence.get( + "operator_confirmed_report_catalog_record_final_closeout_gate" + ) + or operator_evidence.get( + "operator_confirmed_market_intel_report_catalog_record_final_closeout" + ) + or closeout_payload.get( + "operator_confirmed_report_catalog_record_final_closeout_gate" + ) + ), + "operator_confirmed_report_catalog_record_archive_summary_reviewed": bool( + operator_evidence.get( + "operator_confirmed_report_catalog_record_archive_summary_reviewed" + ) + or operator_evidence.get( + "operator_confirmed_catalog_record_archive_summary_reviewed" + ) + or closeout_payload.get( + "operator_confirmed_report_catalog_record_archive_summary_reviewed" + ) + ), + "operator_confirmed_catalog_record_traceability_reviewed": bool( + operator_evidence.get( + "operator_confirmed_catalog_record_traceability_reviewed" + ) + or closeout_payload.get("operator_confirmed_catalog_record_traceability_reviewed") + ), + "operator_confirmed_catalog_record_pipeline_complete": bool( + operator_evidence.get("operator_confirmed_catalog_record_pipeline_complete") + or operator_evidence.get( + "operator_confirmed_market_intel_catalog_record_pipeline_complete" + ) + or closeout_payload.get("operator_confirmed_catalog_record_pipeline_complete") + ), + "operator_confirmed_no_pending_catalog_record_followup": bool( + operator_evidence.get( + "operator_confirmed_no_pending_catalog_record_followup" + ) + or operator_evidence.get("operator_confirmed_no_followup_required") + or closeout_payload.get( + "operator_confirmed_no_pending_catalog_record_followup" + ) + ), + "operator_confirmed_catalog_record_final_closeout_read_only": bool( + operator_evidence.get( + "operator_confirmed_catalog_record_final_closeout_read_only" + ) + or operator_evidence.get("operator_confirmed_final_closeout_is_read_only") + or closeout_payload.get( + "operator_confirmed_catalog_record_final_closeout_read_only" + ) + ), + "operator_confirmed_no_token_in_report_catalog_record_final_closeout": bool( + operator_evidence.get( + "operator_confirmed_no_token_in_report_catalog_record_final_closeout" + ) + or operator_evidence.get("operator_confirmed_no_token_in_artifacts") + or closeout_payload.get( + "operator_confirmed_no_token_in_report_catalog_record_final_closeout" + ) + ), + "operator_confirmed_no_api_report_generation": bool( + operator_evidence.get("operator_confirmed_no_api_report_generation") + or operator_evidence.get("operator_confirmed_no_api_report_write") + or closeout_payload.get("operator_confirmed_no_api_report_generation") + ), + "operator_confirmed_no_api_file_write": bool( + operator_evidence.get("operator_confirmed_no_api_file_write") + or closeout_payload.get("operator_confirmed_no_api_file_write") + ), + "operator_confirmed_no_api_cli_execution": bool( + operator_evidence.get("operator_confirmed_no_api_cli_execution") + or operator_evidence.get("operator_confirmed_no_api_execute_cli") + or closeout_payload.get("operator_confirmed_no_api_cli_execution") + ), + "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") + or closeout_payload.get("operator_confirmed_no_api_catalog_record_write") + ), + "operator_confirmed_no_api_telegram_dispatch": bool( + operator_evidence.get("operator_confirmed_no_api_telegram_dispatch") + or closeout_payload.get("operator_confirmed_no_api_telegram_dispatch") + ), + "operator_confirmed_no_api_db_write": bool( + operator_evidence.get("operator_confirmed_no_api_db_write") + or closeout_payload.get("operator_confirmed_no_api_db_write") + ), + "operator_confirmed_no_llm_call": bool( + operator_evidence.get("operator_confirmed_no_llm_call") + or closeout_payload.get("operator_confirmed_no_llm_call") + ), + "operator_confirmed_no_scheduler_attach": bool( + operator_evidence.get("operator_confirmed_no_scheduler_attach") + or closeout_payload.get("operator_confirmed_no_scheduler_attach") + ), + "final_closeout_notes_recorded": _has_text( + operator_evidence.get("report_catalog_record_final_closeout_notes") + or operator_evidence.get("catalog_record_final_closeout_notes") + or closeout_payload.get("notes") + ), + "safe_token_metadata_only": all( + key in SAFE_TOKEN_METADATA_KEYS or key == "approval_env_var" + for key in _strip_safe_token_boolean_keys(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 + ) + or _contains_forbidden_token_key(closeout_payload), + } + + +def _closeout_sections(summary, operator): + return [ + { + "key": "catalog_record_final_closeout_identity", + "title": "Catalog record final closeout identity", + "facts": [ + f"family={summary['writer_record_family'] or 'missing'}", + f"record_key={summary['writer_catalog_record_key'] or 'missing'}", + f"statement_count={summary['writer_statement_count']}", + ], + }, + { + "key": "catalog_record_final_closeout_traceability", + "title": "Catalog record final closeout traceability", + "facts": [ + f"summary_path={operator['report_catalog_record_final_closeout_artifact_path'] or 'missing'}", + f"archive_summary_passed={summary['archive_summary_passed']}", + f"sections_complete={summary['summary_sections_complete']}", + ], + }, + { + "key": "catalog_record_final_closeout_integrity", + "title": "Catalog record final closeout integrity", + "facts": [ + f"db_commit={summary['writer_database_commit_executed']}", + f"smoke_passed={summary['smoke_postwrite_smoke_passed']}", + f"pipeline_complete={operator['operator_confirmed_catalog_record_pipeline_complete']}", + ], + }, + { + "key": "catalog_record_final_closeout_safety", + "title": "Catalog record final closeout safety", + "facts": [ + "report_generated=false", + "api_cli_executed=false", + "api_database_write_executed=false", + "catalog_record_final_closeout_executed=false", + "scheduler_attached=false", + ], + }, + ] + + +def _closeout_gates(summary, operator, apply_real_write): + return [ + { + "key": "report_catalog_record_archive_summary_passed", + "label": "上一關 report catalog record archive summary gate 已通過", + "passed": bool( + summary["provided"] + and summary["mode"] + == "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary_preview" + and summary["archive_summary_passed"] + and summary["ready_for_next_manual_phase"] + and summary["ready_for_market_intel_report_catalog_record_final_closeout"] + and not summary["ready_for_market_intel_report_catalog_record_archive_summary"] + ), + }, + { + "key": "report_catalog_record_archive_summary_catalog_record_evidence_retained", + "label": "archive summary payload 保留 catalog record identity、DB commit 與 post-write smoke 證據", + "passed": bool( + summary["writer_catalog_record_key"] + and summary["writer_target_table"] == TARGET_TABLE + and summary["writer_target_column"] == TARGET_COLUMN + and summary["writer_statement_count"] > 0 + and summary["writer_database_commit_executed"] + and summary["writer_hash_match"] + and summary["smoke_read_only_query_executed"] + and summary["smoke_postwrite_smoke_passed"] + and summary["smoke_hash_match"] + ), + }, + { + "key": "report_catalog_record_archive_summary_artifacts_verified", + "label": "archive summary payload 保留 archive、closeout、commit、run receipt、writer output、post-write smoke 與 backup trace", + "passed": bool( + summary["archive_passed"] + and summary["archive_artifact_path"] + and summary["archive_closeout_artifact_path_recorded"] + and summary["archive_commit_artifact_path_recorded"] + and summary["archive_run_receipt_artifact_path_recorded"] + and summary["archive_writer_output_artifact_path_recorded"] + and summary["archive_postwrite_smoke_artifact_path_recorded"] + and summary["archive_backup_artifact_path_recorded"] + and summary["operator_summary_artifact_path"] + and summary["operator_archive_artifact_path_recorded"] + ), + }, + { + "key": "report_catalog_record_archive_summary_sections_complete", + "label": "archive summary sections 已整理 identity、traceability、integrity 與 runtime safety", + "passed": bool(summary["summary_sections_complete"]), + }, + { + "key": "report_catalog_record_archive_summary_operator_evidence_retained", + "label": "archive summary payload 保留操作員 archive summary、traceability、read-only 與 final closeout separate gate 確認", + "passed": bool( + summary["operator_archive_summary_gate"] + and summary["operator_archive_reviewed"] + and summary["operator_traceability_reviewed"] + and summary["operator_archive_summary_read_only"] + and summary["operator_final_closeout_requires_separate_gate"] + ), + }, + { + "key": "report_catalog_record_archive_summary_promotion_gate", + "label": "archive summary promotion gate 僅放行到 final closeout 且要求獨立 gate", + "passed": bool( + summary["promotion_allowed"] + and summary["promotion_next_manual_phase"] + == "market_intel_report_catalog_record_final_closeout" + and summary["promotion_final_closeout_separate_gate"] + and summary["promotion_api_must_not_generate_report"] + and summary["promotion_api_must_not_write_file"] + and summary["promotion_api_must_not_execute_cli"] + and summary["promotion_api_must_not_write_catalog_record"] + and summary["promotion_api_must_not_write_database"] + and summary["promotion_api_must_not_commit_catalog_record"] + and summary["promotion_api_must_not_dispatch_telegram"] + ), + }, + { + "key": "report_catalog_record_archive_summary_api_side_effects_blocked", + "label": "上一關 archive summary top-level API 沒有執行 CLI、寫檔、寫 DB、commit、派 Telegram 或掛 scheduler", + "passed": bool( + not summary["api_executes_cli"] + and not summary["api_writes_file"] + and not summary["api_writes_database"] + and not summary["api_dispatches_telegram"] + and not summary["database_write_executed"] + and not summary["catalog_record_archive_summary_executed"] + and not summary["scheduler_attached"] + and not summary["blocked_response_keys_true"] + ), + }, + { + "key": "report_catalog_record_archive_summary_safe_boundaries_complete", + "label": "上一關 archive summary safe boundaries 完整且未夾帶 token key", + "passed": bool( + summary["safe_boundaries_complete"] + and not summary["forbidden_token_key_detected"] + ), + }, + { + "key": "report_catalog_record_final_closeout_operator_confirmed", + "label": "操作員確認 final closeout gate、完整 traceability、pipeline complete 與無後續 DB follow-up", + "passed": bool( + operator["report_catalog_record_final_closeout_artifact_path"] + and operator[ + "report_catalog_record_archive_summary_artifact_path_recorded" + ] + and operator["report_catalog_record_archive_artifact_path_recorded"] + and operator["report_catalog_record_closeout_artifact_path_recorded"] + and operator["report_catalog_record_commit_artifact_path_recorded"] + and operator["report_catalog_record_run_receipt_artifact_path_recorded"] + and operator["catalog_record_writer_output_artifact_path_recorded"] + and operator["catalog_record_postwrite_smoke_artifact_path_recorded"] + and operator["catalog_record_backup_artifact_path_recorded"] + and operator[ + "operator_confirmed_report_catalog_record_final_closeout_gate" + ] + and operator[ + "operator_confirmed_report_catalog_record_archive_summary_reviewed" + ] + and operator["operator_confirmed_catalog_record_traceability_reviewed"] + and operator["operator_confirmed_catalog_record_pipeline_complete"] + and operator[ + "operator_confirmed_no_pending_catalog_record_followup" + ] + and operator[ + "operator_confirmed_catalog_record_final_closeout_read_only" + ] + ), + }, + { + "key": "report_catalog_record_final_closeout_runtime_boundaries_confirmed", + "label": "操作員確認本 API 不產報表、不寫檔、不執行 CLI、不寫 DB、不派 Telegram、不呼叫 LLM、不掛 scheduler", + "passed": bool( + operator[ + "operator_confirmed_no_token_in_report_catalog_record_final_closeout" + ] + and operator["operator_confirmed_no_api_report_generation"] + and operator["operator_confirmed_no_api_file_write"] + and operator["operator_confirmed_no_api_cli_execution"] + 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_final_closeout_no_token_submitted_to_api", + "label": "final closeout gate payload 不得包含 approval 或 Telegram token key", + "passed": not operator["forbidden_token_submitted_to_api"], + }, + { + "key": "report_catalog_record_final_closeout_apply_real_write_not_requested_from_api", + "label": "API/UI report catalog record final closeout gate 不接受 apply_real_write", + "passed": not apply_real_write, + }, + ] + + +def build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout( + *, + telegram_dispatch_report_catalog_record_archive_summary, + report_catalog_record_final_closeout=None, + operator_evidence=None, + execute_requested=False, + apply_real_write=False, +): + """建立 Telegram dispatch report catalog record final closeout gate;不執行副作用。""" + summary = _archive_summary_snapshot( + telegram_dispatch_report_catalog_record_archive_summary + ) + operator = _operator_summary(operator_evidence, report_catalog_record_final_closeout) + sections = _closeout_sections(summary, operator) + gates = _closeout_gates(summary, operator, bool(apply_real_write)) + blocked_reasons = [gate["key"] for gate in gates if not gate["passed"]] + closeout_passed = bool(not blocked_reasons) + + return { + "mode": "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout_preview", + "target_table": TARGET_TABLE, + "target_column": TARGET_COLUMN, + "target_json_path": [ + "market_intel_ai_summary", + "telegram_dispatch_report_catalog_record_final_closeout", + ], + "target_operation": "close_manual_market_intel_report_catalog_record_pipeline", + "execute_requested": bool(execute_requested), + "apply_real_write_requested": bool(apply_real_write), + "report_catalog_record_final_closeout_reviewed": True, + "telegram_dispatch_report_catalog_record_final_closeout_passed": closeout_passed, + "summary_persistence_telegram_dispatch_report_catalog_record_final_closeout_passed": closeout_passed, + "report_catalog_record_final_closeout_passed": closeout_passed, + "market_intel_report_catalog_record_pipeline_complete": closeout_passed, + "ready_for_next_manual_phase": False, + "ready_for_market_intel_report_catalog_record_final_closeout": False, + "ready_for_market_intel_report_catalog_record_archive_summary": False, + "ready_for_market_intel_report_catalog_record_archive": False, + "ready_for_market_intel_report_catalog_record_closeout": False, + "ready_for_market_intel_report_catalog_record_commit": False, + "ready_for_market_intel_report_catalog_record_cli_run": False, + "ready_for_cli_operator_run": 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["writer_statement_count"] + or summary["statement_count"], + "expected_summary_payload_hash": summary["expected_summary_payload_hash"], + "blocked_reasons": blocked_reasons, + "gates": gates, + "telegram_dispatch_report_catalog_record_archive_summary": summary, + "telegram_dispatch_report_catalog_record_final_closeout_sections": sections, + "operator_telegram_dispatch_report_catalog_record_final_closeout": operator, + "promotion_gate": { + "allowed": closeout_passed, + "next_manual_phase": "market_intel_report_catalog_record_pipeline_complete", + "terminal_gate": True, + "requires_real_db_write": False, + "requires_cli_run": False, + "requires_scheduler_attach": False, + "requires_operator_approval": True, + "requires_postwrite_smoke": False, + "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, + "api_must_not_commit_catalog_record": True, + "report_catalog_record_pipeline_complete_no_separate_api_gate": True, + }, + "next_operator_steps": [ + "保存 report catalog record final closeout artifact path", + "確認 catalog record pipeline 已完成且沒有待補 DB write、Telegram dispatch 或 report generation", + "將後續工作轉為人工營運檢視,不從 API/UI 觸發任何寫入或派送", + "若未來需要重開流程,必須從新的獨立 gate 與人工核准開始", + ], + "safe_boundaries": [ + "do_not_read_approval_token_from_report_catalog_record_final_closeout_api", + "do_not_read_telegram_token_from_report_catalog_record_final_closeout_api", + "do_not_call_llm_from_report_catalog_record_final_closeout", + "do_not_generate_report_from_report_catalog_record_final_closeout_api", + "do_not_write_report_catalog_record_final_closeout_artifact_from_api", + "do_not_execute_catalog_record_cli_from_report_catalog_record_final_closeout_api", + "do_not_write_report_catalog_record_from_report_catalog_record_final_closeout_api", + "do_not_commit_catalog_record_from_report_catalog_record_final_closeout_api", + "do_not_dispatch_telegram_from_report_catalog_record_final_closeout_api", + "do_not_open_database_connection_from_report_catalog_record_final_closeout", + "do_not_update_review_state_from_report_catalog_record_final_closeout", + "do_not_attach_scheduler_from_report_catalog_record_final_closeout", + "market_intel_report_catalog_record_pipeline_terminal_preview_only", + "no_remove_orphans", + "no_momo_db_lifecycle_change", + ], + } diff --git a/services/market_intel/deployment_readiness.py b/services/market_intel/deployment_readiness.py index 60f2a7a..5b15815 100644 --- a/services/market_intel/deployment_readiness.py +++ b/services/market_intel/deployment_readiness.py @@ -55,6 +55,7 @@ from services.market_intel.candidate_queue_review_ai_summary_persistence_telegra from services.market_intel.candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_closeout import build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_closeout from services.market_intel.candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive import build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive from services.market_intel.candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary import build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary +from services.market_intel.candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout import build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout from services.market_intel.candidate_queue_review_ai_summary_persistence_transaction import build_candidate_queue_review_ai_summary_persistence_transaction from services.market_intel.candidate_queue_review_ai_summary_persistence_writer_preflight import build_candidate_queue_review_ai_summary_persistence_writer_preflight from services.market_intel.candidate_queue_review_ai_summary_run_package import build_candidate_queue_review_ai_summary_run_package @@ -86,7 +87,7 @@ BLOCKED_RUN_REVIEW_KEYS = ( "telegram_dispatch_report_closeout_file_written", "report_closeout_file_written", "telegram_dispatch_report_archive_file_written", "report_archive_file_written", "report_archive_record_written", "report_archive_manifest_written", "telegram_dispatch_report_archive_summary_file_written", "report_archive_summary_file_written", "report_summary_input_file_written", "telegram_dispatch_report_catalog_handoff_file_written", "report_catalog_handoff_file_written", "catalog_handoff_file_written", "telegram_dispatch_report_catalog_index_file_written", "report_catalog_index_file_written", "catalog_index_file_written", "report_catalog_index_manifest_written", "catalog_index_manifest_written", "catalog_index_written", "catalog_manifest_written", "catalog_record_written", "report_catalog_record_written", "report_archive_summary_catalog_handoff_file_written", "telegram_dispatch_report_catalog_write_preflight_file_written", "report_catalog_write_preflight_file_written", "catalog_write_preflight_file_written", "catalog_record_write_preflight_file_written", "telegram_dispatch_report_catalog_record_write_file_written", "report_catalog_record_write_file_written", "catalog_record_write_file_written", "catalog_record_write_executed", "report_catalog_record_write_executed", "telegram_dispatch_report_catalog_record_write_gate_file_written", "report_catalog_record_write_gate_file_written", "catalog_record_write_gate_file_written", "telegram_dispatch_report_catalog_record_run_package_file_written", "report_catalog_record_run_package_file_written", "catalog_record_run_package_file_written", "catalog_record_write_receipt_file_written", "catalog_record_postwrite_smoke_executed", "catalog_record_commit_executed", "telegram_dispatch_report_catalog_record_run_readiness_file_written", "report_catalog_record_run_readiness_file_written", "catalog_record_run_readiness_file_written", "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", "telegram_dispatch_report_catalog_record_run_receipt_gate_file_written", "report_catalog_record_run_receipt_gate_file_written", "catalog_record_run_receipt_gate_file_written", "catalog_record_run_receipt_executed", "catalog_record_commit_gate_file_written", "telegram_dispatch_report_catalog_record_run_receipt_file_written", "report_catalog_record_run_receipt_file_written", "catalog_record_run_receipt_file_written", "catalog_record_cli_executed", "catalog_record_writer_cli_executed", "catalog_record_postwrite_smoke_file_written", - "telegram_dispatch_report_catalog_record_commit_gate_file_written", "report_catalog_record_commit_gate_file_written", "telegram_dispatch_report_catalog_record_commit_file_written", "report_catalog_record_commit_file_written", "catalog_record_commit_file_written", "catalog_record_closeout_gate_file_written", "telegram_dispatch_report_catalog_record_closeout_gate_file_written", "report_catalog_record_closeout_gate_file_written", "telegram_dispatch_report_catalog_record_closeout_file_written", "report_catalog_record_closeout_file_written", "catalog_record_closeout_file_written", "catalog_record_closeout_executed", "catalog_record_archive_gate_file_written", "telegram_dispatch_report_catalog_record_archive_gate_file_written", "report_catalog_record_archive_gate_file_written", "telegram_dispatch_report_catalog_record_archive_file_written", "report_catalog_record_archive_file_written", "catalog_record_archive_file_written", "catalog_record_archive_record_written", "catalog_record_archive_manifest_written", "catalog_record_archive_executed", "catalog_record_archive_summary_gate_file_written", "telegram_dispatch_report_catalog_record_archive_summary_gate_file_written", "report_catalog_record_archive_summary_gate_file_written", "telegram_dispatch_report_catalog_record_archive_summary_file_written", "report_catalog_record_archive_summary_file_written", "catalog_record_archive_summary_file_written", "catalog_record_archive_summary_record_written", "catalog_record_archive_summary_executed", "catalog_record_final_closeout_gate_file_written", + "telegram_dispatch_report_catalog_record_commit_gate_file_written", "report_catalog_record_commit_gate_file_written", "telegram_dispatch_report_catalog_record_commit_file_written", "report_catalog_record_commit_file_written", "catalog_record_commit_file_written", "catalog_record_closeout_gate_file_written", "telegram_dispatch_report_catalog_record_closeout_gate_file_written", "report_catalog_record_closeout_gate_file_written", "telegram_dispatch_report_catalog_record_closeout_file_written", "report_catalog_record_closeout_file_written", "catalog_record_closeout_file_written", "catalog_record_closeout_executed", "catalog_record_archive_gate_file_written", "telegram_dispatch_report_catalog_record_archive_gate_file_written", "report_catalog_record_archive_gate_file_written", "telegram_dispatch_report_catalog_record_archive_file_written", "report_catalog_record_archive_file_written", "catalog_record_archive_file_written", "catalog_record_archive_record_written", "catalog_record_archive_manifest_written", "catalog_record_archive_executed", "catalog_record_archive_summary_gate_file_written", "telegram_dispatch_report_catalog_record_archive_summary_gate_file_written", "report_catalog_record_archive_summary_gate_file_written", "telegram_dispatch_report_catalog_record_archive_summary_file_written", "report_catalog_record_archive_summary_file_written", "catalog_record_archive_summary_file_written", "catalog_record_archive_summary_record_written", "catalog_record_archive_summary_executed", "catalog_record_final_closeout_gate_file_written", "telegram_dispatch_report_catalog_record_final_closeout_gate_file_written", "report_catalog_record_final_closeout_gate_file_written", "telegram_dispatch_report_catalog_record_final_closeout_file_written", "report_catalog_record_final_closeout_file_written", "catalog_record_final_closeout_file_written", "catalog_record_final_closeout_record_written", "catalog_record_final_closeout_executed", "catalog_record_pipeline_completion_record_written", "market_intel_catalog_record_pipeline_completed_by_api", "summary_persistence_record_written", "metadata_patch_written", "transaction_file_written", @@ -127,6 +128,14 @@ PRODUCTION_SMOKE_TARGETS = ( ) + PRODUCTION_SMOKE_TARGETS[-1:] ) +PRODUCTION_SMOKE_TARGETS = ( + PRODUCTION_SMOKE_TARGETS[:-1] + + ( + "/api/market_intel/manual_sample_review/" + "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout", + ) + + PRODUCTION_SMOKE_TARGETS[-1:] +) def _run_review_preview_safe(payload, mode): return bool(payload["mode"] == mode and all(not payload.get(key) for key in BLOCKED_RUN_REVIEW_KEYS)) def build_deployment_readiness_preview(*, service, market_intel_tables, schema_smoke_builder): @@ -307,6 +316,7 @@ def build_deployment_readiness_preview(*, service, market_intel_tables, schema_s candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_closeout = build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_closeout(telegram_dispatch_report_catalog_record_commit=candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_commit) candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive = build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive(telegram_dispatch_report_catalog_record_closeout=candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_closeout) candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary = build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary(telegram_dispatch_report_catalog_record_archive=candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive) + candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout = build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout(telegram_dispatch_report_catalog_record_archive_summary=candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary) checks = { "schema_smoke_passed": bool(schema_smoke["passed"]), "feature_flags_default_safe": bool( @@ -569,6 +579,7 @@ def build_deployment_readiness_preview(*, service, market_intel_tables, schema_s "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_closeout_preview_safe": _run_review_preview_safe(candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_closeout, "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_closeout_preview"), "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_preview_safe": _run_review_preview_safe(candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive, "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_preview"), "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary_preview_safe": _run_review_preview_safe(candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary, "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary_preview"), + "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout_preview_safe": _run_review_preview_safe(candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout, "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout_preview"), "candidate_queue_review_decision_writer_cli_status_safe": _run_review_preview_safe(candidate_queue_review_decision_writer_status, "candidate_queue_review_decision_writer_cli_blocked"), "match_review_plan_preview_safe": bool( match_review_plan["mode"] == "match_review_plan_preview" @@ -847,6 +858,7 @@ def build_deployment_readiness_preview(*, service, market_intel_tables, schema_s "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_closeout": candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_closeout, "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive": candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive, "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary": candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary, + "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout": candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout, "candidate_queue_review_decision_writer_status": candidate_queue_review_decision_writer_status, "match_review_plan": match_review_plan, "opportunity_plan": opportunity_plan, diff --git a/services/market_intel/phase.py b/services/market_intel/phase.py index ce7d72d..504ebe5 100644 --- a/services/market_intel/phase.py +++ b/services/market_intel/phase.py @@ -1,3 +1,3 @@ """市場情報 rollout phase 單一來源。""" -MARKET_INTEL_PHASE = "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" +MARKET_INTEL_PHASE = "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" diff --git a/templates/market_intel/disabled.html b/templates/market_intel/disabled.html index ff9d18a..f17199a 100644 --- a/templates/market_intel/disabled.html +++ b/templates/market_intel/disabled.html @@ -781,6 +781,9 @@ + @@ -1169,6 +1172,7 @@ const sampleCandidateQueueReviewAiSummaryPersistenceTelegramDispatchReportCatalogRecordCloseout = sampleReviewRoot ? sampleReviewRoot.querySelector('[data-market-intel-sample-candidate-queue-review-ai-summary-persistence-telegram-dispatch-report-catalog-record-closeout]') : null; const sampleCandidateQueueReviewAiSummaryPersistenceTelegramDispatchReportCatalogRecordArchive = sampleReviewRoot ? sampleReviewRoot.querySelector('[data-market-intel-sample-candidate-queue-review-ai-summary-persistence-telegram-dispatch-report-catalog-record-archive]') : null; const sampleCandidateQueueReviewAiSummaryPersistenceTelegramDispatchReportCatalogRecordArchiveSummary = sampleReviewRoot ? sampleReviewRoot.querySelector('[data-market-intel-sample-candidate-queue-review-ai-summary-persistence-telegram-dispatch-report-catalog-record-archive-summary]') : null; + const sampleCandidateQueueReviewAiSummaryPersistenceTelegramDispatchReportCatalogRecordFinalCloseout = sampleReviewRoot ? sampleReviewRoot.querySelector('[data-market-intel-sample-candidate-queue-review-ai-summary-persistence-telegram-dispatch-report-catalog-record-final-closeout]') : null; const sampleReviewEndpoint = "{{ url_for('market_intel.market_intel_manual_sample_review') }}"; const sampleReviewEvaluateEndpoint = "{{ url_for('market_intel.market_intel_manual_sample_review_evaluate') }}"; const sampleCandidateHandoffEndpoint = "{{ url_for('market_intel.market_intel_manual_sample_candidate_handoff') }}"; @@ -1234,6 +1238,7 @@ const sampleCandidateQueueReviewAiSummaryPersistenceTelegramDispatchReportCatalogRecordCloseoutEndpoint = "{{ url_for('market_intel_review.market_intel_manual_sample_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_closeout') }}"; const sampleCandidateQueueReviewAiSummaryPersistenceTelegramDispatchReportCatalogRecordArchiveEndpoint = "{{ url_for('market_intel_review.market_intel_manual_sample_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive') }}"; const sampleCandidateQueueReviewAiSummaryPersistenceTelegramDispatchReportCatalogRecordArchiveSummaryEndpoint = "{{ url_for('market_intel_review.market_intel_manual_sample_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary') }}"; + const sampleCandidateQueueReviewAiSummaryPersistenceTelegramDispatchReportCatalogRecordFinalCloseoutEndpoint = "{{ url_for('market_intel_review.market_intel_manual_sample_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout') }}"; const schedulerMeta = schedulerRoot ? schedulerRoot.querySelector('[data-market-intel-scheduler-meta]') : null; const schedulerBody = schedulerRoot ? schedulerRoot.querySelector('[data-market-intel-scheduler-body]') : null; const schedulerRefresh = schedulerRoot ? schedulerRoot.querySelector('[data-market-intel-scheduler-refresh]') : null; @@ -9636,6 +9641,154 @@ } }; + const renderCandidateQueueReviewAiSummaryPersistenceTelegramDispatchReportCatalogRecordFinalCloseout = data => { + const blockers = (data.blocked_reasons || []).join(' / '); + const gates = data.gates || []; + const summary = data.telegram_dispatch_report_catalog_record_archive_summary || {}; + const operator = data.operator_telegram_dispatch_report_catalog_record_final_closeout || {}; + const promotion = data.promotion_gate || {}; + const sections = data.telegram_dispatch_report_catalog_record_final_closeout_sections || []; + sampleReviewMeta.innerHTML = [ + `mode=${data.mode || 'unknown'}`, + `closeout=${data.telegram_dispatch_report_catalog_record_final_closeout_passed ? 'pass' : 'blocked'}`, + `summary=${summary.archive_summary_passed ? 'pass' : 'blocked'}`, + `complete=${data.market_intel_report_catalog_record_pipeline_complete ? 'yes' : 'blocked'}`, + `file=${data.catalog_record_final_closeout_file_written ? 'written' : 'blocked'}` + ].map(item => `${escapeHtml(item)}`).join(''); + sampleReviewBody.innerHTML = ` +
此卡只審核 Telegram dispatch report catalog record final closeout gate;API/UI 不讀 token、不產報表、不寫檔、不執行 CLI、不開 DB、不寫 catalog record、不執行 commit、不派 Telegram、不掛 scheduler。${blockers ? `阻擋:${escapeHtml(blockers)}` : ''}
+
+
+

FINAL GATES

+
${ + gates.map(item => ` +
+
+ ${escapeHtml(item.key)} + ${escapeHtml(item.label)} +
+ ${item.passed ? 'PASS' : 'BLOCK'} +
+ `).join('') || '
尚未提供 final closeout gate。
' + }
+
+
+

ARCHIVE SUMMARY

+
+ ${[ + ['provided', summary.provided], + ['mode', summary.mode || 'missing'], + ['passed', summary.archive_summary_passed], + ['record_key', summary.writer_catalog_record_key || 'missing'], + ['sections', summary.summary_sections_complete], + ['safe_boundaries', summary.safe_boundaries_complete] + ].map(([key, value]) => ` +
+
${escapeHtml(key)}
+ ${escapeHtml(String(value))} +
+ `).join('')} +
+
+
+

SECTIONS

+
${ + sections.map(section => ` +
+ ${escapeHtml(section.key || 'section')} + ${escapeHtml((section.facts || []).join(' / '))} +
+ `).join('') || '
尚未提供 final closeout sections。
' + }
+
+
+

OPERATOR

+
+ ${[ + ['final_path', operator.report_catalog_record_final_closeout_artifact_path || 'missing'], + ['summary_path', operator.report_catalog_record_archive_summary_artifact_path_recorded], + ['traceability', operator.operator_confirmed_catalog_record_traceability_reviewed], + ['pipeline', operator.operator_confirmed_catalog_record_pipeline_complete], + ['no_followup', operator.operator_confirmed_no_pending_catalog_record_followup], + ['read_only', operator.operator_confirmed_catalog_record_final_closeout_read_only] + ].map(([key, value]) => ` +
+
${escapeHtml(key)}
+ ${escapeHtml(String(value))} +
+ `).join('')} +
+
+
+

RUNTIME

+
+ ${[ + ['api_report', data.ready_for_report_generation], + ['api_file', data.api_writes_file], + ['api_cli', data.api_executes_cli], + ['api_db', data.api_writes_database], + ['final_exec', data.catalog_record_final_closeout_executed], + ['scheduler', data.scheduler_attached] + ].map(([key, value]) => ` +
+
${escapeHtml(key)}
+ ${escapeHtml(String(value))} +
+ `).join('')} +
+
+
+

PROMOTION

+
+ ${[ + ['next_phase', promotion.next_manual_phase || 'missing'], + ['terminal', promotion.terminal_gate], + ['api_commit', promotion.api_must_not_commit_catalog_record], + ['api_write', promotion.api_must_not_write_database], + ['api_telegram', promotion.api_must_not_dispatch_telegram] + ].map(([key, value]) => ` +
+
${escapeHtml(key)}
+ ${escapeHtml(String(value))} +
+ `).join('')} +
+
+
+ `; + }; + + const loadCandidateQueueReviewAiSummaryPersistenceTelegramDispatchReportCatalogRecordFinalCloseout = async () => { + if (!sampleReviewMeta || !sampleReviewBody || !sampleReviewInput) return; + let parsed; + try { + parsed = JSON.parse(sampleReviewInput.value || '{}'); + } catch (error) { + sampleReviewMeta.innerHTML = 'json_error'; + sampleReviewBody.innerHTML = `
JSON 格式錯誤:${escapeHtml(error.message)}
`; + return; + } + const body = parsed && parsed.sample_result ? parsed : { sample_result: parsed }; + sampleReviewBody.innerHTML = '
審核 queue review AI summary Telegram dispatch report catalog record final closeout gate 中...
'; + try { + const response = await fetch(`${sampleCandidateQueueReviewAiSummaryPersistenceTelegramDispatchReportCatalogRecordFinalCloseoutEndpoint}?execute=false&apply_real_write=false`, { + method: 'POST', + credentials: 'same-origin', + headers: { + 'Content-Type': 'application/json', + 'X-CSRFToken': csrfToken + }, + body: JSON.stringify(body) + }); + const data = await response.json(); + if (!response.ok && !data.mode) throw new Error(`HTTP ${response.status}`); + renderCandidateQueueReviewAiSummaryPersistenceTelegramDispatchReportCatalogRecordFinalCloseout(data); + } catch (error) { + sampleReviewMeta.innerHTML = 'error'; + sampleReviewBody.innerHTML = `
queue review AI summary Telegram dispatch report catalog record final closeout gate 失敗:${escapeHtml(error.message)}
`; + } + }; + const renderCandidateQueueReviewDecisionWriter = data => { const blockers = (data.blocked_reasons || []).join(' / '); const summary = data.statement_summary || {}; @@ -11432,6 +11585,9 @@ if (sampleCandidateQueueReviewAiSummaryPersistenceTelegramDispatchReportCatalogRecordArchiveSummary) { sampleCandidateQueueReviewAiSummaryPersistenceTelegramDispatchReportCatalogRecordArchiveSummary.addEventListener('click', loadCandidateQueueReviewAiSummaryPersistenceTelegramDispatchReportCatalogRecordArchiveSummary); } + if (sampleCandidateQueueReviewAiSummaryPersistenceTelegramDispatchReportCatalogRecordFinalCloseout) { + sampleCandidateQueueReviewAiSummaryPersistenceTelegramDispatchReportCatalogRecordFinalCloseout.addEventListener('click', loadCandidateQueueReviewAiSummaryPersistenceTelegramDispatchReportCatalogRecordFinalCloseout); + } if (schedulerRefresh) { schedulerRefresh.addEventListener('click', loadScheduler); } diff --git a/tests/test_market_intel_skeleton.py b/tests/test_market_intel_skeleton.py index 1724a4e..2c62328 100644 --- a/tests/test_market_intel_skeleton.py +++ b/tests/test_market_intel_skeleton.py @@ -1363,6 +1363,14 @@ def test_market_intel_preview_template_uses_safe_fetch_false_endpoint(): "data-market-intel-sample-candidate-queue-review-ai-summary-persistence-telegram-dispatch-report-catalog-record-archive-summary" in template ) + assert ( + "market_intel_review.market_intel_manual_sample_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" + in template + ) + assert ( + "data-market-intel-sample-candidate-queue-review-ai-summary-persistence-telegram-dispatch-report-catalog-record-final-closeout" + in template + ) assert "X-CSRFToken" in template assert "market_intel.market_intel_scheduler_plan" in template assert "market_intel.market_intel_match_review_plan" in template @@ -1399,7 +1407,7 @@ def test_legacy_source_bridge_default_is_planned_only(): bridge = MarketIntelService().build_legacy_source_bridge() assert bridge["mode"] == "legacy_source_bridge_planned" - assert bridge["phase"] == "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + assert bridge["phase"] == "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" assert bridge["execute_requested"] is False assert bridge["read_only_query_executed"] is False assert bridge["database_connection_opened"] is False @@ -1557,7 +1565,7 @@ def test_mcp_tool_contract_preview_is_read_only_and_whitelisted(): contract = MarketIntelService().build_mcp_tool_contract() assert contract["mode"] == "mcp_tool_contract_preview" - assert contract["phase"] == "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + assert contract["phase"] == "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" assert contract["caller"] == "market_intel" assert contract["contract_ready"] is True assert contract["blocked_reasons"] == [] @@ -1690,7 +1698,7 @@ def test_mcp_activation_runbook_route_is_preview_only(): assert response.status_code == 200 assert data["mode"] == "mcp_activation_runbook_preview" - assert data["phase"] == "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + assert data["phase"] == "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" assert data["deployment_actions_executed"] is False assert data["docker_command_executed"] is False assert data["ssh_command_executed"] is False @@ -1703,7 +1711,7 @@ def test_mcp_fetch_gate_default_blocks_external_fetch(): gate = MarketIntelService().build_mcp_fetch_gate(fetch_requested=True) assert gate["mode"] == "mcp_fetch_gate_planned" - assert gate["phase"] == "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + assert gate["phase"] == "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" assert gate["fetch_requested"] is True assert gate["manual_fetch_gate_open"] is False assert gate["network_request_allowed"] is False @@ -1773,7 +1781,7 @@ def test_mcp_fetch_gate_route_is_preview_only(): assert response.status_code == 200 assert data["mode"] == "mcp_fetch_gate_planned" - assert data["phase"] == "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + assert data["phase"] == "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" assert data["fetch_requested"] is False assert data["network_request_allowed"] is False assert data["external_network_executed"] is False @@ -1785,7 +1793,7 @@ def test_manual_sample_plan_preview_blocks_fetch_and_write(): plan = MarketIntelService().build_manual_sample_plan() assert plan["mode"] == "manual_sample_fetch_plan_preview" - assert plan["phase"] == "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + assert plan["phase"] == "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" assert plan["ready_for_manual_sample_fetch"] is False assert plan["sample_fetch_executed"] is False assert plan["external_network_executed"] is False @@ -1833,7 +1841,7 @@ def test_manual_sample_plan_route_is_preview_only(): assert response.status_code == 200 assert data["mode"] == "manual_sample_fetch_plan_preview" - assert data["phase"] == "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + assert data["phase"] == "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" assert data["sample_fetch_executed"] is False assert data["external_network_executed"] is False assert data["database_write_executed"] is False @@ -1844,7 +1852,7 @@ def test_manual_sample_acceptance_preview_blocks_candidate_import(): acceptance = MarketIntelService().build_manual_sample_acceptance() assert acceptance["mode"] == "manual_sample_acceptance_preview" - assert acceptance["phase"] == "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + assert acceptance["phase"] == "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" assert acceptance["contract_ready"] is True assert acceptance["sample_result_loaded"] is False assert acceptance["sample_result_accepted"] is False @@ -1886,7 +1894,7 @@ def test_manual_sample_acceptance_route_is_preview_only(): assert response.status_code == 200 assert data["mode"] == "manual_sample_acceptance_preview" - assert data["phase"] == "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + assert data["phase"] == "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" assert data["sample_result_loaded"] is False assert data["candidate_import_allowed"] is False assert data["external_network_executed"] is False @@ -1898,7 +1906,7 @@ def test_manual_sample_review_preview_is_planned_until_result_loaded(): review = MarketIntelService().build_manual_sample_review() assert review["mode"] == "manual_sample_review_preview" - assert review["phase"] == "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + assert review["phase"] == "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" assert review["contract_ready"] is True assert review["sample_result_loaded"] is False assert review["sample_result_reviewed"] is False @@ -2009,7 +2017,7 @@ def test_manual_sample_review_route_is_preview_only(): assert response.status_code == 200 assert data["mode"] == "manual_sample_review_preview" - assert data["phase"] == "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + assert data["phase"] == "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" assert data["sample_result_loaded"] is False assert data["sample_result_reviewed"] is False assert data["candidate_import_allowed"] is False @@ -2048,7 +2056,7 @@ def test_manual_sample_review_evaluation_preview_accepts_payload_without_persist ) assert review["mode"] == "manual_sample_review_evaluation_preview" - assert review["phase"] == "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + assert review["phase"] == "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" assert review["review_request_type"] == "operator_posted_json" assert review["payload_received"] is True assert review["payload_valid_json_object"] is True @@ -2110,7 +2118,7 @@ def test_manual_sample_review_evaluate_route_is_post_only_and_no_write(): assert response.status_code == 200 assert data["mode"] == "manual_sample_review_evaluation_preview" - assert data["phase"] == "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + assert data["phase"] == "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" assert data["payload_received"] is True assert data["payload_valid_json_object"] is True assert data["payload_persisted"] is False @@ -2190,7 +2198,7 @@ def test_manual_sample_candidate_handoff_preview_creates_candidates_without_pers ) assert handoff["mode"] == "manual_sample_candidate_handoff_preview" - assert handoff["phase"] == "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + assert handoff["phase"] == "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" assert handoff["payload_received"] is True assert handoff["payload_valid_json_object"] is True assert handoff["payload_persisted"] is False @@ -2254,7 +2262,7 @@ def test_manual_sample_candidate_handoff_route_is_post_only_and_no_write(): assert response.status_code == 200 assert data["mode"] == "manual_sample_candidate_handoff_preview" - assert data["phase"] == "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + assert data["phase"] == "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" assert data["payload_received"] is True assert data["handoff_ready"] is True assert data["candidate_handoff_created"] is True @@ -2313,7 +2321,7 @@ def test_manual_sample_candidate_queue_draft_preview_builds_review_items_without ) assert queue_draft["mode"] == "manual_sample_candidate_queue_draft_preview" - assert queue_draft["phase"] == "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + assert queue_draft["phase"] == "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" assert queue_draft["payload_received"] is True assert queue_draft["payload_valid_json_object"] is True assert queue_draft["payload_persisted"] is False @@ -2387,7 +2395,7 @@ def test_manual_sample_candidate_queue_draft_route_is_post_only_and_no_write(): assert response.status_code == 200 assert data["mode"] == "manual_sample_candidate_queue_draft_preview" - assert data["phase"] == "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + assert data["phase"] == "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" assert data["payload_received"] is True assert data["handoff_ready"] is True assert data["queue_draft_ready"] is True @@ -2450,7 +2458,7 @@ def test_manual_sample_candidate_queue_approval_preview_blocks_write_and_maps_ro ) assert approval["mode"] == "manual_sample_candidate_queue_approval_preview" - assert approval["phase"] == "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + assert approval["phase"] == "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" assert approval["payload_received"] is True assert approval["payload_valid_json_object"] is True assert approval["payload_persisted"] is False @@ -2528,7 +2536,7 @@ def test_manual_sample_candidate_queue_approval_route_is_post_only_and_no_write( assert response.status_code == 200 assert data["mode"] == "manual_sample_candidate_queue_approval_preview" - assert data["phase"] == "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + assert data["phase"] == "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" assert data["payload_received"] is True assert data["approval_preview_created"] is True assert data["approval_request_created"] is False @@ -2591,7 +2599,7 @@ def test_manual_sample_candidate_queue_transaction_preview_blocks_execution(): ) assert transaction["mode"] == "manual_sample_candidate_queue_transaction_preview" - assert transaction["phase"] == "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + assert transaction["phase"] == "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" assert transaction["payload_received"] is True assert transaction["payload_valid_json_object"] is True assert transaction["payload_persisted"] is False @@ -2671,7 +2679,7 @@ def test_manual_sample_candidate_queue_transaction_route_is_post_only_and_no_wri assert response.status_code == 200 assert data["mode"] == "manual_sample_candidate_queue_transaction_preview" - assert data["phase"] == "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + assert data["phase"] == "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" assert data["payload_received"] is True assert data["transaction_preview_created"] is True assert data["transaction_ready"] is False @@ -8382,7 +8390,7 @@ def test_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_ "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_input_preview" ) assert data["phase"] == ( - "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" ) assert data["telegram_dispatch_report_input_ready"] is False assert data["summary_persistence_telegram_dispatch_report_input_ready"] is False @@ -8457,7 +8465,7 @@ def test_candidate_queue_review_ai_summary_persistence_telegram_dispatch_archive "candidate_queue_review_ai_summary_persistence_telegram_dispatch_archive_summary_preview" ) assert data["phase"] == ( - "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" ) assert data["telegram_dispatch_archive_summary_ready"] is False assert ( @@ -8729,7 +8737,7 @@ def test_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_ "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_input_preview" ) assert data["phase"] == ( - "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" ) assert data["telegram_dispatch_report_input_ready"] is False assert data["summary_persistence_telegram_dispatch_report_input_ready"] is False @@ -9017,7 +9025,7 @@ def test_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_ "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_run_package_preview" ) assert data["phase"] == ( - "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" ) assert data["telegram_dispatch_report_run_package_ready"] is False assert ( @@ -9327,7 +9335,7 @@ def test_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_ "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_run_readiness_preview" ) assert data["phase"] == ( - "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" ) assert data["telegram_dispatch_report_run_readiness_ready"] is False assert ( @@ -9630,7 +9638,7 @@ def test_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_ "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_run_receipt_preview" ) assert data["phase"] == ( - "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" ) assert data["telegram_dispatch_report_run_receipt_passed"] is False assert ( @@ -9889,7 +9897,7 @@ def test_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_ "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_closeout_preview" ) assert data["phase"] == ( - "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" ) assert data["telegram_dispatch_report_closeout_passed"] is False assert ( @@ -10162,7 +10170,7 @@ def test_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_ "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_archive_preview" ) assert data["phase"] == ( - "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" ) assert data["telegram_dispatch_report_archive_passed"] is False assert ( @@ -10410,7 +10418,7 @@ def test_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_ "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_archive_summary_preview" ) assert data["phase"] == ( - "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" ) assert data["telegram_dispatch_report_archive_summary_passed"] is False assert ( @@ -10640,7 +10648,7 @@ def test_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_ "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_handoff_preview" ) assert data["phase"] == ( - "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" ) assert data["telegram_dispatch_report_catalog_handoff_passed"] is False assert data["summary_persistence_telegram_dispatch_report_catalog_handoff_passed"] is False @@ -10877,7 +10885,7 @@ def test_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_ "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_index_preview" ) assert data["phase"] == ( - "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" ) assert data["telegram_dispatch_report_catalog_index_passed"] is False assert data["summary_persistence_telegram_dispatch_report_catalog_index_passed"] is False @@ -11119,7 +11127,7 @@ def test_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_ "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_write_preflight_preview" ) assert data["phase"] == ( - "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" ) assert data["telegram_dispatch_report_catalog_write_preflight_passed"] is False assert ( @@ -11395,7 +11403,7 @@ def test_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_ "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_write_preview" ) assert data["phase"] == ( - "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" ) assert data["telegram_dispatch_report_catalog_record_write_passed"] is False assert ( @@ -11672,7 +11680,7 @@ def test_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_ "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_run_package_preview" ) assert data["phase"] == ( - "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" ) assert data["telegram_dispatch_report_catalog_record_run_package_passed"] is False assert ( @@ -11951,7 +11959,7 @@ def test_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_ "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_run_readiness_preview" ) assert data["phase"] == ( - "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" ) assert data["telegram_dispatch_report_catalog_record_run_readiness_passed"] is False assert ( @@ -12280,7 +12288,7 @@ def test_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_ "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_run_receipt_preview" ) assert data["phase"] == ( - "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" ) assert data["telegram_dispatch_report_catalog_record_run_receipt_passed"] is False assert ( @@ -12542,7 +12550,7 @@ def test_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_ "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_commit_preview" ) assert data["phase"] == ( - "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" ) assert data["telegram_dispatch_report_catalog_record_commit_passed"] is False assert ( @@ -12806,7 +12814,7 @@ def test_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_ "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_closeout_preview" ) assert data["phase"] == ( - "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" ) assert data["telegram_dispatch_report_catalog_record_closeout_passed"] is False assert ( @@ -13076,7 +13084,7 @@ def test_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_ "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_preview" ) assert data["phase"] == ( - "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" ) assert data["telegram_dispatch_report_catalog_record_archive_passed"] is False assert ( @@ -13364,7 +13372,7 @@ def test_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_ "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary_preview" ) assert data["phase"] == ( - "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" ) assert data["telegram_dispatch_report_catalog_record_archive_summary_passed"] is False assert ( @@ -13407,6 +13415,294 @@ def test_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_ assert TEST_APPROVAL_TOKEN not in payload +def test_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout_contract_only(): + from services.market_intel.candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout import ( + build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout, + ) + + archive_summary = { + "mode": "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary_preview", + "target_operation": "summarize_manual_market_intel_report_catalog_record_archive", + "telegram_dispatch_report_catalog_record_archive_summary_passed": True, + "summary_persistence_telegram_dispatch_report_catalog_record_archive_summary_passed": True, + "report_catalog_record_archive_summary_passed": True, + "ready_for_next_manual_phase": True, + "ready_for_market_intel_report_catalog_record_final_closeout": True, + "ready_for_market_intel_report_catalog_record_archive_summary": False, + "statement_count": 1, + "expected_summary_payload_hash": "a" * 64, + "api_executes_cli": False, + "api_writes_file": False, + "api_writes_database": False, + "api_dispatches_telegram": False, + "database_write_executed": False, + "catalog_record_archive_summary_executed": False, + "scheduler_attached": False, + "telegram_dispatch_report_catalog_record_archive": { + "writer_record_family": "market_intel_telegram_dispatch_audit_report", + "writer_catalog_record_key": "market-intel-report-20260520", + "writer_target_table": "market_alert_review_queue", + "writer_target_column": "metadata_json", + "writer_statement_count": 1, + "writer_database_commit_executed": True, + "writer_hash_match": True, + "smoke_read_only_query_executed": True, + "smoke_postwrite_smoke_passed": True, + "smoke_hash_match": True, + "archive_passed": True, + "archive_artifact_path": ( + "artifacts/market_intel/catalog-record-archive-gate.json" + ), + "archive_closeout_artifact_path_recorded": True, + "archive_commit_artifact_path_recorded": True, + "archive_run_receipt_artifact_path_recorded": True, + "archive_writer_output_artifact_path_recorded": True, + "archive_postwrite_smoke_artifact_path_recorded": True, + "archive_backup_artifact_path_recorded": True, + }, + "operator_telegram_dispatch_report_catalog_record_archive_summary": { + "report_catalog_record_archive_summary_artifact_path": ( + "artifacts/market_intel/catalog-record-archive-summary-gate.json" + ), + "report_catalog_record_archive_artifact_path_recorded": True, + "operator_confirmed_report_catalog_record_archive_summary_gate": True, + "operator_confirmed_report_catalog_record_archive_reviewed": True, + "operator_confirmed_catalog_record_traceability_reviewed": True, + "operator_confirmed_catalog_record_archive_summary_read_only": True, + "operator_confirmed_catalog_record_final_closeout_requires_separate_gate": True, + }, + "telegram_dispatch_report_catalog_record_archive_summary_sections": [ + {"key": "catalog_record_archive_summary_identity", "facts": []}, + {"key": "catalog_record_archive_summary_traceability", "facts": []}, + {"key": "catalog_record_archive_summary_integrity", "facts": []}, + {"key": "catalog_record_archive_summary_safety", "facts": []}, + ], + "promotion_gate": { + "allowed": True, + "next_manual_phase": "market_intel_report_catalog_record_final_closeout", + "report_catalog_record_final_closeout_requires_separate_gate": 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_commit_catalog_record": True, + "api_must_not_dispatch_telegram": True, + }, + "safe_boundaries": [ + "do_not_read_approval_token_from_report_catalog_record_archive_summary_api", + "do_not_read_telegram_token_from_report_catalog_record_archive_summary_api", + "do_not_call_llm_from_report_catalog_record_archive_summary", + "do_not_generate_report_from_report_catalog_record_archive_summary_api", + "do_not_write_report_catalog_record_archive_summary_artifact_from_api", + "do_not_execute_catalog_record_cli_from_report_catalog_record_archive_summary_api", + "do_not_write_report_catalog_record_from_report_catalog_record_archive_summary_api", + "do_not_commit_catalog_record_from_report_catalog_record_archive_summary_api", + "do_not_dispatch_telegram_from_report_catalog_record_archive_summary_api", + "do_not_open_database_connection_from_report_catalog_record_archive_summary", + "do_not_update_review_state_from_report_catalog_record_archive_summary", + "do_not_attach_scheduler_from_report_catalog_record_archive_summary", + "future_market_intel_report_catalog_record_final_closeout_must_use_separate_gate", + ], + } + operator_evidence = { + "report_catalog_record_final_closeout_artifact_path": ( + "artifacts/market_intel/catalog-record-final-closeout-gate.json" + ), + "report_catalog_record_archive_summary_artifact_path": ( + "artifacts/market_intel/catalog-record-archive-summary-gate.json" + ), + "report_catalog_record_archive_artifact_path": ( + "artifacts/market_intel/catalog-record-archive-gate.json" + ), + "report_catalog_record_closeout_artifact_path": ( + "artifacts/market_intel/catalog-record-closeout-gate.json" + ), + "report_catalog_record_commit_artifact_path": ( + "artifacts/market_intel/catalog-record-commit-gate.json" + ), + "report_catalog_record_run_receipt_artifact_path": ( + "artifacts/market_intel/catalog-record-run-receipt.json" + ), + "catalog_record_writer_output_artifact_path": ( + "artifacts/market_intel/catalog-record-writer-output.json" + ), + "catalog_record_postwrite_smoke_artifact_path": ( + "artifacts/market_intel/catalog-record-postwrite-smoke.json" + ), + "catalog_record_backup_artifact_path": ( + "artifacts/market_intel/catalog-record-backup.json" + ), + "operator_confirmed_report_catalog_record_final_closeout_gate": True, + "operator_confirmed_report_catalog_record_archive_summary_reviewed": True, + "operator_confirmed_catalog_record_traceability_reviewed": True, + "operator_confirmed_catalog_record_pipeline_complete": True, + "operator_confirmed_no_pending_catalog_record_followup": True, + "operator_confirmed_catalog_record_final_closeout_read_only": True, + "operator_confirmed_no_token_in_report_catalog_record_final_closeout": True, + "operator_confirmed_no_api_report_generation": True, + "operator_confirmed_no_api_file_write": True, + "operator_confirmed_no_api_cli_execution": True, + "operator_confirmed_no_api_catalog_record_write": True, + "operator_confirmed_no_api_telegram_dispatch": True, + "operator_confirmed_no_api_db_write": True, + "operator_confirmed_no_llm_call": True, + "operator_confirmed_no_scheduler_attach": True, + "catalog_record_final_closeout_notes": "manual final closeout reviewed", + } + + closeout = build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout( + telegram_dispatch_report_catalog_record_archive_summary=archive_summary, + operator_evidence=operator_evidence, + execute_requested=True, + ) + token_leak = build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout( + telegram_dispatch_report_catalog_record_archive_summary=archive_summary, + operator_evidence={**operator_evidence, "telegram_token": TEST_APPROVAL_TOKEN}, + ) + apply_write = build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout( + telegram_dispatch_report_catalog_record_archive_summary=archive_summary, + operator_evidence=operator_evidence, + apply_real_write=True, + ) + + assert closeout["mode"] == ( + "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout_preview" + ) + assert closeout["target_operation"] == ( + "close_manual_market_intel_report_catalog_record_pipeline" + ) + assert closeout["telegram_dispatch_report_catalog_record_final_closeout_passed"] is True + assert ( + closeout[ + "summary_persistence_telegram_dispatch_report_catalog_record_final_closeout_passed" + ] + is True + ) + assert closeout["report_catalog_record_final_closeout_passed"] is True + assert closeout["market_intel_report_catalog_record_pipeline_complete"] is True + assert closeout["ready_for_next_manual_phase"] is False + assert closeout["ready_for_market_intel_report_catalog_record_final_closeout"] is False + assert closeout["ready_for_real_write"] is False + assert closeout["ready_for_report_generation"] is False + assert closeout["ready_for_api_database_write"] is False + assert closeout["api_executes_cli"] is False + assert closeout["api_writes_file"] is False + assert closeout["api_writes_database"] is False + assert closeout["catalog_record_written"] is False + assert closeout["catalog_record_final_closeout_gate_file_written"] is False + assert closeout["catalog_record_final_closeout_executed"] is False + assert closeout["catalog_record_pipeline_completion_record_written"] is False + assert closeout["market_intel_catalog_record_pipeline_completed_by_api"] is False + assert closeout["database_connection_opened"] is False + assert closeout["database_write_executed"] is False + assert closeout["scheduler_attached"] is False + assert closeout["blocked_reasons"] == [] + assert closeout["telegram_dispatch_report_catalog_record_archive_summary"][ + "writer_database_commit_executed" + ] is True + assert closeout["promotion_gate"]["next_manual_phase"] == ( + "market_intel_report_catalog_record_pipeline_complete" + ) + assert closeout["promotion_gate"]["terminal_gate"] is True + assert ( + token_leak["telegram_dispatch_report_catalog_record_final_closeout_passed"] + is False + ) + assert ( + "report_catalog_record_final_closeout_no_token_submitted_to_api" + in token_leak["blocked_reasons"] + ) + assert ( + "report_catalog_record_final_closeout_apply_real_write_not_requested_from_api" + in apply_write["blocked_reasons"] + ) + assert ( + "do_not_commit_catalog_record_from_report_catalog_record_final_closeout_api" + in closeout["safe_boundaries"] + ) + assert TEST_APPROVAL_TOKEN not in json.dumps( + token_leak, + ensure_ascii=False, + sort_keys=True, + ) + + +def test_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout_route_is_post_only_and_no_write(): + from routes.market_intel_routes import market_intel_bp + from routes.market_intel_review_routes import market_intel_review_bp + + app = Flask(__name__) + app.secret_key = "test-secret" + app.register_blueprint(market_intel_bp) + app.register_blueprint(market_intel_review_bp) + client = app.test_client() + with client.session_transaction() as session: + session["logged_in"] = True + + get_response = client.get( + "/api/market_intel/manual_sample_review/" + "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" + ) + response = client.post( + "/api/market_intel/manual_sample_review/" + "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" + "?execute=true&apply_real_write=true", + json={"sample_result": {}}, + ) + data = response.get_json() + payload = json.dumps(data, ensure_ascii=False, sort_keys=True) + + assert get_response.status_code == 405 + assert response.status_code == 200 + assert data["mode"] == ( + "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout_preview" + ) + assert data["phase"] == ( + "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" + ) + assert data["telegram_dispatch_report_catalog_record_final_closeout_passed"] is False + assert ( + data[ + "summary_persistence_telegram_dispatch_report_catalog_record_final_closeout_passed" + ] + is False + ) + assert data["report_catalog_record_final_closeout_passed"] is False + assert data["market_intel_report_catalog_record_pipeline_complete"] is False + assert data["ready_for_market_intel_report_catalog_record_final_closeout"] is False + assert data["ready_for_real_write"] is False + assert data["ready_for_report_generation"] is False + assert data["ready_for_api_database_write"] is False + assert data["api_executes_cli"] is False + assert data["api_writes_file"] is False + assert data["api_writes_database"] is False + assert data["catalog_record_written"] is False + assert data["catalog_record_final_closeout_gate_file_written"] is False + assert data["catalog_record_final_closeout_executed"] is False + assert data["catalog_record_pipeline_completion_record_written"] is False + assert data["market_intel_catalog_record_pipeline_completed_by_api"] is False + assert data["database_connection_opened"] is False + assert data["database_write_executed"] is False + assert data["scheduler_attached"] is False + assert data["telegram_dispatch_report_catalog_record_archive_summary"]["mode"] == ( + "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary_preview" + ) + assert "report_catalog_record_archive_summary_passed" in data["blocked_reasons"] + assert "report_catalog_record_final_closeout_operator_confirmed" in data[ + "blocked_reasons" + ] + assert ( + "report_catalog_record_final_closeout_apply_real_write_not_requested_from_api" + in data["blocked_reasons"] + ) + assert ( + "do_not_commit_catalog_record_from_report_catalog_record_final_closeout_api" + in data["safe_boundaries"] + ) + assert TEST_APPROVAL_TOKEN not in payload + + def test_candidate_queue_writer_preflight_route_is_post_only_and_no_write(): from routes.market_intel_routes import market_intel_bp @@ -13449,7 +13745,7 @@ def test_candidate_queue_writer_preflight_route_is_post_only_and_no_write(): assert response.status_code == 200 assert data["mode"] == "candidate_queue_writer_preflight_planned" - assert data["phase"] == "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + assert data["phase"] == "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" assert data["execute_requested"] is False assert data["read_only_query_executed"] is False assert data["database_connection_opened"] is False @@ -13506,7 +13802,7 @@ def test_candidate_queue_writer_status_route_never_leaks_approval_token(monkeypa assert response.status_code == 200 assert data["mode"] == "candidate_queue_writer_cli_blocked" - assert data["phase"] == "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + assert data["phase"] == "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" assert data["execute_requested"] is True assert data["apply_real_write_requested"] is True assert data["approval_token_present"] is False @@ -13595,7 +13891,7 @@ def test_candidate_queue_writer_postwrite_smoke_route_is_post_only_and_no_write( assert response.status_code == 200 assert data["mode"] == "candidate_queue_writer_postwrite_smoke_planned" - assert data["phase"] == "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + assert data["phase"] == "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" assert data["execute_requested"] is False assert data["read_only_query_executed"] is False assert data["database_connection_opened"] is False @@ -13649,7 +13945,7 @@ def test_candidate_queue_writer_operator_drill_route_is_post_only_and_no_write() assert response.status_code == 200 assert data["mode"] == "candidate_queue_writer_operator_drill_preview" - assert data["phase"] == "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + assert data["phase"] == "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" assert data["operator_drill_ready"] is True assert data["api_executes_cli"] is False assert data["api_reads_approval_token"] is False @@ -13705,7 +14001,7 @@ def test_candidate_queue_writer_run_package_route_is_post_only_and_no_write(): assert response.status_code == 200 assert data["mode"] == "candidate_queue_writer_run_package_preview" - assert data["phase"] == "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + assert data["phase"] == "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" assert data["package_ready"] is True assert data["package_artifact_created"] is False assert data["api_writes_file"] is False @@ -13771,7 +14067,7 @@ def test_candidate_queue_writer_run_readiness_route_is_post_only_and_no_write(): assert response.status_code == 200 assert data["mode"] == "candidate_queue_writer_run_readiness_preview" - assert data["phase"] == "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + assert data["phase"] == "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" assert data["ready_for_cli_operator_run"] is True assert data["ready_for_api_database_write"] is False assert data["api_executes_cli"] is False @@ -14073,7 +14369,7 @@ def test_candidate_queue_writer_run_receipt_route_accepts_inline_payload_no_writ assert response.status_code == 200 assert data["mode"] == "candidate_queue_writer_run_receipt_preview" - assert data["phase"] == "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + assert data["phase"] == "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" assert data["receipt_passed"] is True assert data["ready_for_api_database_write"] is False assert data["ready_for_scheduler_attach"] is False @@ -14121,7 +14417,7 @@ def test_candidate_queue_writer_run_closeout_route_is_post_only_and_no_write(): assert response.status_code == 200 assert data["mode"] == "candidate_queue_writer_run_closeout_preview" - assert data["phase"] == "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + assert data["phase"] == "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" assert data["closeout_passed"] is True assert data["ready_for_next_manual_phase"] is True assert data["ready_for_api_database_write"] is False @@ -14170,7 +14466,7 @@ def test_candidate_queue_review_handoff_route_is_post_only_and_no_write(): assert response.status_code == 200 assert data["mode"] == "candidate_queue_review_handoff_preview" - assert data["phase"] == "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + assert data["phase"] == "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" assert data["handoff_ready"] is True assert data["ready_for_manual_queue_review"] is True assert data["ready_for_api_database_write"] is False @@ -14228,7 +14524,7 @@ def test_candidate_queue_review_inventory_route_is_post_only_and_no_write(): assert get_response.status_code == 405 assert response.status_code == 200 assert data["mode"] == "candidate_queue_review_inventory_preview" - assert data["phase"] == "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + assert data["phase"] == "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" assert data["execute_requested"] is False assert data["review_inventory_ready"] is False assert data["ready_for_human_decision_review"] is False @@ -14294,7 +14590,7 @@ def test_candidate_queue_review_decision_route_is_post_only_and_no_write(): assert get_response.status_code == 405 assert response.status_code == 200 assert data["mode"] == "candidate_queue_review_decision_preview" - assert data["phase"] == "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + assert data["phase"] == "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" assert data["decision_ready"] is False assert data["ready_for_human_decision_record"] is False assert data["ready_for_api_review_state_update"] is False @@ -14365,7 +14661,7 @@ def test_candidate_queue_review_decision_approval_route_is_post_only_and_no_writ assert get_response.status_code == 405 assert response.status_code == 200 assert data["mode"] == "candidate_queue_review_decision_approval_preview" - assert data["phase"] == "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + assert data["phase"] == "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" assert data["approval_ready"] is False assert data["ready_for_review_state_transaction_preview"] is False assert data["ready_for_cli_decision_writer"] is False @@ -14441,7 +14737,7 @@ def test_candidate_queue_review_decision_transaction_route_is_post_only_and_no_w assert get_response.status_code == 405 assert response.status_code == 200 assert data["mode"] == "candidate_queue_review_decision_transaction_preview" - assert data["phase"] == "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + assert data["phase"] == "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" assert data["transaction_preview_created"] is False assert data["transaction_ready"] is False assert data["ready_for_manual_shell_update_window"] is False @@ -14523,7 +14819,7 @@ def test_candidate_queue_review_decision_writer_status_route_is_post_only_and_no assert get_response.status_code == 405 assert response.status_code == 200 assert data["mode"] == "candidate_queue_review_decision_writer_cli_blocked" - assert data["phase"] == "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + assert data["phase"] == "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" assert data["execute_requested"] is True assert data["apply_real_write_requested"] is True assert data["approval_token_present"] is False @@ -14609,7 +14905,7 @@ def test_candidate_queue_review_decision_writer_preflight_route_is_post_only_and assert data["mode"] == ( "candidate_queue_review_decision_writer_preflight_preview" ) - assert data["phase"] == "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + assert data["phase"] == "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" assert data["execute_requested"] is True assert data["apply_real_write_requested"] is True assert data["read_only_query_executed"] is False @@ -14692,7 +14988,7 @@ def test_candidate_queue_review_decision_writer_postwrite_smoke_route_is_post_on assert data["mode"] == ( "candidate_queue_review_decision_writer_postwrite_smoke_planned" ) - assert data["phase"] == "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + assert data["phase"] == "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" assert data["execute_requested"] is False assert data["read_only_query_executed"] is False assert data["database_connection_opened"] is False @@ -14775,7 +15071,7 @@ def test_candidate_queue_review_decision_writer_operator_drill_route_is_post_onl assert data["mode"] == ( "candidate_queue_review_decision_writer_operator_drill_preview" ) - assert data["phase"] == "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + assert data["phase"] == "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" assert data["operator_drill_ready"] is False assert data["ready_for_api_review_state_update"] is False assert data["ready_for_api_database_write"] is False @@ -14861,7 +15157,7 @@ def test_candidate_queue_review_decision_writer_run_package_route_is_post_only_a assert data["mode"] == ( "candidate_queue_review_decision_writer_run_package_preview" ) - assert data["phase"] == "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + assert data["phase"] == "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" assert data["package_ready"] is False assert data["package_artifact_created"] is False assert data["ready_for_api_review_state_update"] is False @@ -14952,7 +15248,7 @@ def test_candidate_queue_review_decision_writer_run_readiness_route_is_post_only "candidate_queue_review_decision_writer_run_readiness_preview" ) assert data["phase"] == ( - "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" ) assert data["ready_for_cli_operator_run"] is False assert data["ready_for_api_review_state_update"] is False @@ -15062,7 +15358,7 @@ def test_candidate_queue_review_decision_writer_run_receipt_route_is_post_only_a "candidate_queue_review_decision_writer_run_receipt_preview" ) assert data["phase"] == ( - "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" ) assert data["receipt_passed"] is False assert data["ready_for_api_review_state_update"] is False @@ -15148,7 +15444,7 @@ def test_candidate_queue_review_decision_writer_run_closeout_route_is_post_only_ "candidate_queue_review_decision_writer_run_closeout_preview" ) assert data["phase"] == ( - "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" ) assert data["closeout_passed"] is False assert data["ready_for_api_review_state_update"] is False @@ -15205,7 +15501,7 @@ def test_candidate_queue_review_decision_post_closeout_inventory_route_is_post_o "candidate_queue_review_decision_post_closeout_inventory_preview" ) assert data["phase"] == ( - "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" ) assert data["post_closeout_inventory_ready"] is False assert data["ready_for_api_review_state_update"] is False @@ -15258,7 +15554,7 @@ def test_candidate_queue_review_completion_archive_route_is_post_only_and_no_wri assert get_response.status_code == 405 assert response.status_code == 200 assert data["mode"] == "candidate_queue_review_completion_archive_preview" - assert data["phase"] == "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + assert data["phase"] == "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" assert data["review_completion_archive_ready"] is False assert data["archive_manifest_ready"] is False assert data["ready_for_api_review_state_update"] is False @@ -15311,7 +15607,7 @@ def test_candidate_queue_review_archive_summary_route_is_post_only_and_no_write( assert get_response.status_code == 405 assert response.status_code == 200 assert data["mode"] == "candidate_queue_review_archive_summary_preview" - assert data["phase"] == "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + assert data["phase"] == "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" assert data["archive_summary_ready"] is False assert data["summary_input_ready"] is False assert data["ready_for_ai_summary_generation"] is False @@ -15372,7 +15668,7 @@ def test_candidate_queue_review_ai_summary_preflight_route_is_post_only_and_no_w assert get_response.status_code == 405 assert response.status_code == 200 assert data["mode"] == "candidate_queue_review_ai_summary_preflight_preview" - assert data["phase"] == "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + assert data["phase"] == "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" assert data["ai_summary_preflight_ready"] is False assert data["ready_for_manual_ollama_summary_run"] is False assert data["ready_for_ai_summary_generation"] is False @@ -15441,7 +15737,7 @@ def test_candidate_queue_review_ai_summary_run_package_route_is_post_only_and_no assert get_response.status_code == 405 assert response.status_code == 200 assert data["mode"] == "candidate_queue_review_ai_summary_run_package_preview" - assert data["phase"] == "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + assert data["phase"] == "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" assert data["ai_summary_run_package_ready"] is False assert data["ready_for_manual_ollama_summary_run"] is False assert data["ready_for_ai_summary_generation"] is False @@ -15513,7 +15809,7 @@ def test_candidate_queue_review_ai_summary_output_receipt_route_is_post_only_and assert get_response.status_code == 405 assert response.status_code == 200 assert data["mode"] == "candidate_queue_review_ai_summary_output_receipt_preview" - assert data["phase"] == "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + assert data["phase"] == "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" assert data["ai_summary_output_receipt_ready"] is False assert data["ready_for_summary_persistence_review"] is False assert data["manual_ai_summary_output_provided"] is False @@ -15586,7 +15882,7 @@ def test_candidate_queue_review_ai_summary_persistence_preflight_route_is_post_o "candidate_queue_review_ai_summary_persistence_preflight_preview" ) assert data["phase"] == ( - "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" ) assert data["summary_persistence_preflight_ready"] is False assert data["ready_for_summary_transaction_preview"] is False @@ -15657,7 +15953,7 @@ def test_candidate_queue_review_ai_summary_persistence_transaction_route_is_post "candidate_queue_review_ai_summary_persistence_transaction_preview" ) assert data["phase"] == ( - "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" ) assert data["summary_persistence_transaction_ready"] is False assert data["ready_for_summary_persistence_writer_gate"] is False @@ -15722,7 +16018,7 @@ def test_candidate_queue_review_ai_summary_persistence_writer_preflight_route_is "candidate_queue_review_ai_summary_persistence_writer_preflight_preview" ) assert data["phase"] == ( - "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" ) assert data["summary_persistence_writer_preflight_ready"] is False assert data["ready_for_summary_persistence_run_package"] is False @@ -15793,7 +16089,7 @@ def test_candidate_queue_review_ai_summary_persistence_run_package_route_is_post "candidate_queue_review_ai_summary_persistence_run_package_preview" ) assert data["phase"] == ( - "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" ) assert data["package_ready"] is False assert data["ready_for_summary_persistence_run_readiness"] is False @@ -15866,7 +16162,7 @@ def test_candidate_queue_review_ai_summary_persistence_run_readiness_route_is_po "candidate_queue_review_ai_summary_persistence_run_readiness_preview" ) assert data["phase"] == ( - "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" ) assert data["run_readiness_ready"] is False assert data["summary_persistence_run_readiness_ready"] is False @@ -15943,7 +16239,7 @@ def test_candidate_queue_review_ai_summary_persistence_run_receipt_route_is_post "candidate_queue_review_ai_summary_persistence_run_receipt_preview" ) assert data["phase"] == ( - "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" ) assert data["run_receipt_passed"] is False assert data["summary_persistence_run_receipt_passed"] is False @@ -16020,7 +16316,7 @@ def test_candidate_queue_review_ai_summary_persistence_run_closeout_route_is_pos "candidate_queue_review_ai_summary_persistence_run_closeout_preview" ) assert data["phase"] == ( - "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" ) assert data["closeout_passed"] is False assert data["summary_persistence_closeout_passed"] is False @@ -16097,7 +16393,7 @@ def test_candidate_queue_review_ai_summary_persistence_telegram_dispatch_gate_ro "candidate_queue_review_ai_summary_persistence_telegram_dispatch_gate_preview" ) assert data["phase"] == ( - "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" ) assert data["telegram_dispatch_gate_passed"] is False assert data["summary_persistence_telegram_dispatch_gate_passed"] is False @@ -16171,7 +16467,7 @@ def test_candidate_queue_review_ai_summary_persistence_telegram_dispatch_run_pac "candidate_queue_review_ai_summary_persistence_telegram_dispatch_run_package_preview" ) assert data["phase"] == ( - "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" ) assert data["telegram_dispatch_run_package_ready"] is False assert data["summary_persistence_telegram_dispatch_run_package_ready"] is False @@ -16250,7 +16546,7 @@ def test_candidate_queue_review_ai_summary_persistence_telegram_dispatch_run_rea "candidate_queue_review_ai_summary_persistence_telegram_dispatch_run_readiness_preview" ) assert data["phase"] == ( - "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" ) assert data["telegram_dispatch_run_readiness_ready"] is False assert ( @@ -16337,7 +16633,7 @@ def test_candidate_queue_review_ai_summary_persistence_telegram_dispatch_run_rec "candidate_queue_review_ai_summary_persistence_telegram_dispatch_run_receipt_preview" ) assert data["phase"] == ( - "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" ) assert data["telegram_dispatch_run_receipt_passed"] is False assert data["summary_persistence_telegram_dispatch_run_receipt_passed"] is False @@ -16418,7 +16714,7 @@ def test_candidate_queue_review_ai_summary_persistence_telegram_dispatch_closeou "candidate_queue_review_ai_summary_persistence_telegram_dispatch_closeout_preview" ) assert data["phase"] == ( - "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" ) assert data["telegram_dispatch_closeout_passed"] is False assert data["summary_persistence_telegram_dispatch_closeout_passed"] is False @@ -16500,7 +16796,7 @@ def test_candidate_queue_review_ai_summary_persistence_telegram_dispatch_archive "candidate_queue_review_ai_summary_persistence_telegram_dispatch_archive_preview" ) assert data["phase"] == ( - "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" ) assert data["telegram_dispatch_archive_ready"] is False assert data["summary_persistence_telegram_dispatch_archive_ready"] is False @@ -16583,7 +16879,7 @@ def test_candidate_queue_writer_run_receipt_route_is_post_only_and_no_write(): assert response.status_code == 200 assert data["mode"] == "candidate_queue_writer_run_receipt_preview" - assert data["phase"] == "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + assert data["phase"] == "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" assert data["receipt_passed"] is True assert data["ready_for_next_manual_review"] is True assert data["ready_for_api_database_write"] is False @@ -16608,7 +16904,7 @@ def test_scheduler_plan_preview_blocks_job_attachment(): plan = MarketIntelService().build_scheduler_plan() assert plan["mode"] == "scheduler_attach_plan_preview" - assert plan["phase"] == "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + assert plan["phase"] == "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" assert plan["ready_to_attach_scheduler"] is False assert plan["scheduler_attached"] is False assert plan["scheduler_registration_executed"] is False @@ -16646,7 +16942,7 @@ def test_scheduler_plan_route_is_preview_only(): assert response.status_code == 200 assert data["mode"] == "scheduler_attach_plan_preview" - assert data["phase"] == "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + assert data["phase"] == "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" assert data["scheduler_registration_executed"] is False assert data["crawler_job_started"] is False assert data["external_network_executed"] is False @@ -16657,7 +16953,7 @@ def test_match_review_plan_preview_blocks_auto_confirm(): plan = MarketIntelService().build_match_review_plan() assert plan["mode"] == "match_review_plan_preview" - assert plan["phase"] == "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + assert plan["phase"] == "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" assert plan["ready_for_review_queue"] is False assert plan["review_queue_created"] is False assert plan["auto_match_executed"] is False @@ -16693,7 +16989,7 @@ def test_match_review_plan_route_is_preview_only(): assert response.status_code == 200 assert data["mode"] == "match_review_plan_preview" - assert data["phase"] == "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + assert data["phase"] == "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" assert data["review_queue_created"] is False assert data["auto_confirm_executed"] is False assert data["external_network_executed"] is False @@ -16704,7 +17000,7 @@ def test_opportunity_plan_preview_blocks_alerts_and_ai_summary(): plan = MarketIntelService().build_opportunity_plan() assert plan["mode"] == "opportunity_plan_preview" - assert plan["phase"] == "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + assert plan["phase"] == "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" assert plan["ready_for_opportunity_queue"] is False assert plan["opportunity_queue_created"] is False assert plan["threat_alert_dispatched"] is False @@ -16745,7 +17041,7 @@ def test_opportunity_plan_route_is_preview_only(): assert response.status_code == 200 assert data["mode"] == "opportunity_plan_preview" - assert data["phase"] == "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + assert data["phase"] == "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" assert data["opportunity_queue_created"] is False assert data["threat_alert_dispatched"] is False assert data["ai_summary_generated"] is False @@ -16756,7 +17052,7 @@ def test_opportunity_scoring_plan_preview_blocks_scoring_and_alerts(): plan = MarketIntelService().build_opportunity_scoring_plan() assert plan["mode"] == "opportunity_scoring_plan_preview" - assert plan["phase"] == "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + assert plan["phase"] == "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" assert plan["ready_for_scoring_job"] is False assert plan["scoring_job_created"] is False assert plan["score_calculation_executed"] is False @@ -16804,7 +17100,7 @@ def test_opportunity_scoring_plan_route_is_preview_only(): assert response.status_code == 200 assert data["mode"] == "opportunity_scoring_plan_preview" - assert data["phase"] == "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + assert data["phase"] == "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" assert data["scoring_job_created"] is False assert data["score_calculation_executed"] is False assert data["sample_scores_generated"] is False @@ -16816,7 +17112,7 @@ def test_opportunity_evidence_plan_preview_blocks_queries_and_alerts(): plan = MarketIntelService().build_opportunity_evidence_plan() assert plan["mode"] == "opportunity_evidence_plan_preview" - assert plan["phase"] == "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + assert plan["phase"] == "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" assert plan["ready_for_evidence_bundle"] is False assert plan["evidence_bundle_created"] is False assert plan["evidence_query_executed"] is False @@ -16862,7 +17158,7 @@ def test_opportunity_evidence_plan_route_is_preview_only(): assert response.status_code == 200 assert data["mode"] == "opportunity_evidence_plan_preview" - assert data["phase"] == "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + assert data["phase"] == "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" assert data["evidence_bundle_created"] is False assert data["evidence_query_executed"] is False assert data["sample_evidence_generated"] is False @@ -16875,7 +17171,7 @@ def test_opportunity_alert_plan_preview_blocks_dispatch_and_llm_calls(): plan = MarketIntelService().build_opportunity_alert_plan() assert plan["mode"] == "opportunity_alert_plan_preview" - assert plan["phase"] == "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + assert plan["phase"] == "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" assert plan["ready_for_alert_candidates"] is False assert plan["alert_candidate_created"] is False assert plan["alert_queue_created"] is False @@ -16960,7 +17256,7 @@ def test_opportunity_alert_plan_route_is_preview_only(): assert response.status_code == 200 assert data["mode"] == "opportunity_alert_plan_preview" - assert data["phase"] == "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + assert data["phase"] == "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" assert data["alert_candidate_created"] is False assert data["alert_queue_created"] is False assert data["review_queue_created"] is False @@ -17038,7 +17334,7 @@ def test_mcp_deploy_preflight_route_is_preview_only(): assert response.status_code == 200 assert data["mode"] == "mcp_external_deploy_preflight_preview" - assert data["phase"] == "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + assert data["phase"] == "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" assert data["deployment_actions_executed"] is False assert data["docker_command_executed"] is False assert data["ssh_command_executed"] is False @@ -17053,7 +17349,7 @@ def test_mcp_readiness_default_is_planned_only(monkeypatch): readiness = MarketIntelService().build_mcp_readiness() assert readiness["mode"] == "mcp_readiness_planned" - assert readiness["phase"] == "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + assert readiness["phase"] == "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" assert readiness["execute_requested"] is False assert readiness["router_enabled"] is False assert readiness["external_mcp_complete"] is False @@ -17769,6 +18065,12 @@ def test_deployment_readiness_reports_app_only_release_gate(): ] is True ) + assert ( + readiness["checks"][ + "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout_preview_safe" + ] + is True + ) assert ( readiness["checks"][ "candidate_queue_review_decision_writer_cli_status_safe" @@ -18088,6 +18390,11 @@ def test_deployment_readiness_reports_app_only_release_gate(): "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" in readiness["production_smoke_targets"] ) + assert ( + "/api/market_intel/manual_sample_review/" + "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" + in readiness["production_smoke_targets"] + ) assert ( "/api/market_intel/manual_sample_review/" "candidate_queue_review_decision_writer_status" @@ -21557,6 +21864,48 @@ def test_deployment_readiness_reports_app_only_release_gate(): ]["scheduler_attached"] is False ) + assert ( + readiness[ + "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" + ]["mode"] + == "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout_preview" + ) + assert ( + readiness[ + "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" + ]["telegram_dispatch_report_catalog_record_final_closeout_passed"] + is False + ) + assert ( + readiness[ + "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" + ]["market_intel_report_catalog_record_pipeline_complete"] + is False + ) + assert ( + readiness[ + "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" + ]["catalog_record_final_closeout_gate_file_written"] + is False + ) + assert ( + readiness[ + "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" + ]["catalog_record_final_closeout_executed"] + is False + ) + assert ( + readiness[ + "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" + ]["database_write_executed"] + is False + ) + assert ( + readiness[ + "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" + ]["scheduler_attached"] + is False + ) assert ( readiness["candidate_queue_review_decision_writer_status"]["mode"] == "candidate_queue_review_decision_writer_cli_blocked" @@ -21707,7 +22056,7 @@ def test_migration_apply_drill_planned_is_safe_and_manual_only(): drill = MarketIntelService().build_migration_apply_drill() assert drill["mode"] == "migration_apply_drill_preview" - assert drill["phase"] == "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + assert drill["phase"] == "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" assert drill["execute_requested"] is False assert drill["schema_state"] == "planned_no_db_probe" assert drill["drill_ready_for_operator_review"] is True @@ -21822,7 +22171,7 @@ def test_migration_apply_drill_route_is_preview_only(): assert response.status_code == 200 assert data["mode"] == "migration_apply_drill_preview" - assert data["phase"] == "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + assert data["phase"] == "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" assert data["execute_requested"] is False assert data["migration_executed"] is False assert data["rollback_executed"] is False @@ -21834,7 +22183,7 @@ def test_migration_catalog_review_planned_is_safe_and_diagnostic(): review = MarketIntelService().build_migration_catalog_review() assert review["mode"] == "migration_catalog_review_preview" - assert review["phase"] == "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + assert review["phase"] == "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" assert review["execute_requested"] is False assert review["catalog_state"] == "planned_no_probe" assert review["seed_state"] == "planned_no_probe" @@ -21949,7 +22298,7 @@ def test_migration_catalog_review_route_is_preview_only(): assert response.status_code == 200 assert data["mode"] == "migration_catalog_review_preview" - assert data["phase"] == "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + assert data["phase"] == "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" assert data["execute_requested"] is False assert data["catalog_state"] == "planned_no_probe" assert data["migration_executed"] is False @@ -21962,7 +22311,7 @@ def test_migration_live_smoke_planned_is_preview_only(): smoke = MarketIntelService().build_migration_live_smoke() assert smoke["mode"] == "migration_live_smoke_preview" - assert smoke["phase"] == "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + assert smoke["phase"] == "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" assert smoke["execute_requested"] is False assert smoke["smoke_result"] == "planned_no_execution" assert smoke["live_smoke_passed"] is False @@ -22024,7 +22373,7 @@ def test_migration_live_smoke_route_is_preview_only(): assert response.status_code == 200 assert data["mode"] == "migration_live_smoke_preview" - assert data["phase"] == "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + assert data["phase"] == "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" assert data["execute_requested"] is False assert data["smoke_result"] == "planned_no_execution" assert data["migration_executed"] is False @@ -22037,7 +22386,7 @@ def test_live_db_inventory_planned_is_preview_only(): inventory = MarketIntelService().build_live_db_inventory() assert inventory["mode"] == "live_db_inventory_planned" - assert inventory["phase"] == "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + assert inventory["phase"] == "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" assert inventory["execute_requested"] is False assert inventory["read_only_query_executed"] is False assert inventory["database_connection_opened"] is False @@ -22181,7 +22530,7 @@ def test_live_db_inventory_route_is_preview_only(): assert response.status_code == 200 assert data["mode"] == "live_db_inventory_planned" - assert data["phase"] == "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + assert data["phase"] == "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" assert data["execute_requested"] is False assert data["read_only_query_executed"] is False assert data["database_write_executed"] is False @@ -22417,7 +22766,7 @@ def test_candidate_queue_writer_cli_script_outputs_blocked_gate(tmp_path): assert result.returncode == 0 assert data["mode"] == "candidate_queue_writer_cli_blocked" - assert data["phase"] == "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + assert data["phase"] == "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" assert data["execute_requested"] is False assert data["apply_real_write_requested"] is False assert data["writes_executed"] is False @@ -22446,7 +22795,7 @@ def test_review_decision_writer_cli_script_outputs_blocked_gate_without_login_en assert result.returncode == 0 assert data["mode"] == "candidate_queue_review_decision_writer_cli_blocked" - assert data["phase"] == "phase_114_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary" + assert data["phase"] == "phase_115_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout" assert data["execute_requested"] is False assert data["apply_real_write_requested"] is False assert data["approval_token_present"] is False