From 81701e85f46a96e1bc20df9833a34207db99701c Mon Sep 17 00:00:00 2001 From: OoO Date: Fri, 29 May 2026 11:17:37 +0800 Subject: [PATCH] V10.488 add market intel MCP fetch receipt gate --- config.py | 2 +- ...s-platform-market-campaign-intelligence.md | 1 + routes/market_intel_mcp_run_routes.py | 39 + services/market_intel/deployment_readiness.py | 32 + .../market_intel/mcp_fetch_run_receipt.py | 744 ++++++++++++++++++ services/market_intel/phase.py | 2 +- templates/market_intel/disabled.html | 176 ++++- tests/test_market_intel_skeleton.py | 464 +++++++---- 8 files changed, 1312 insertions(+), 148 deletions(-) create mode 100644 services/market_intel/mcp_fetch_run_receipt.py diff --git a/config.py b/config.py index 7848836..61c5d1e 100644 --- a/config.py +++ b/config.py @@ -350,7 +350,7 @@ YOUTUBE_API_KEY = os.getenv('YOUTUBE_API_KEY', '') # ========================================== # 系統版本與路徑 # ========================================== -SYSTEM_VERSION = "V10.486" +SYSTEM_VERSION = "V10.488" LOG_FILE_PATH = os.path.join(BASE_DIR, 'logs/system.log') public_url = PUBLIC_URL # 用於模板顯示 diff --git a/docs/adr/ADR-035-cross-platform-market-campaign-intelligence.md b/docs/adr/ADR-035-cross-platform-market-campaign-intelligence.md index e307829..db7e5d3 100644 --- a/docs/adr/ADR-035-cross-platform-market-campaign-intelligence.md +++ b/docs/adr/ADR-035-cross-platform-market-campaign-intelligence.md @@ -162,6 +162,7 @@ EwoooC 目前已有 MOMO EDM / 節慶活動資料、`promo_products`、PChome - 2026-05-24 追加 MCP fetch target review gate:`services.market_intel.mcp_fetch_target_review` 與 `/api/market_intel/mcp_fetch_target_review` 在 manual fetch handoff 後審核 adapter registry 公開入口白名單、平台來源、每平台 delay / timeout / max pages / sample_limit、rollback plan 與操作員確認;API/UI 不保存 payload、不發外部 request、不開 DB、不寫入、不掛 scheduler,也不會自動打開 manual fetch,只放行到後續人工 fetch run package review。 - 2026-05-24 追加 MCP fetch run package gate:`services.market_intel.mcp_fetch_run_package` 與 `/api/market_intel/mcp_fetch_run_package` 將已通過的 target review 轉成 command argv preview、receipt path 與操作員 run controls 審核包;路由拆至 `routes.market_intel_mcp_run_routes`,主 Blueprint 僅掛 extension import。此階段不執行 CLI、不發外部 request、不寫 artifact、不開 DB、不掛 scheduler,只放行到後續 run readiness review。 - 2026-05-24 追加 MCP fetch run readiness gate:`services.market_intel.mcp_fetch_run_readiness` 與 `/api/market_intel/mcp_fetch_run_readiness` 在 run package 後檢查 command preview、receipt path、artifact path、節流/timeout/dry-run-first、secret payload 與操作員 shell-only 邊界;此階段不執行 CLI、不發外部 request、不寫 artifact、不開 DB、不掛 scheduler,只放行到人工 shell dry-run 與後續 receipt review。 +- 2026-05-29 追加 MCP fetch run receipt gate:`services.market_intel.mcp_fetch_run_receipt` 與 `/api/market_intel/mcp_fetch_run_receipt` 在操作員 shell 完成 dry-run fetch 後審核 receipt id、artifact path、platform/source/receipt path 對帳、公開 URL、request/error budget、secret 外洩與 API/DB/scheduler 副作用旗標;API/UI 不執行 CLI、不發外部 request、不保存 receipt、不開 DB、不寫入、不掛 scheduler,只放行到後續 result parser review。 - 2026-05-18 追加 scheduler attach plan preview:`services.market_intel.scheduler_plan` 與 `/api/market_intel/scheduler_plan` 描述未來 `campaign_discovery_daily`、`campaign_product_probe`、`product_match_review_seed` 三個 job 的 cadence、gate、fallback 與安全邊界。此階段不註冊 scheduler job、不啟動 crawler、不連外、不寫 DB;排程掛載必須等 migration、seed、MCP fetch gate、manual sample 與人工批准全過。 - 2026-05-18 追加 match review plan preview:`services.market_intel.match_review_plan` 與 `/api/market_intel/match_review_plan` 定義商品比對訊號、分數門檻、`needs_review → confirmed/rejected` HITL 流程與安全邊界。此階段不建立 review queue、不自動 confirmed、不寫 `market_product_matches`、不呼叫 MCP;價格只能作為輔助訊號,不能單獨決定同品比對。 - 2026-05-18 追加 opportunity plan preview:`services.market_intel.opportunity_plan` 與 `/api/market_intel/opportunity_plan` 定義競品低價威脅、促銷缺口、深折重疊、活動即將結束四類規則與分級策略。此階段不建立 opportunity queue、不派送 Telegram、不產生 AI 摘要、不寫 DB;高風險項必須先有 confirmed match 與 DB evidence 才能升級。 diff --git a/routes/market_intel_mcp_run_routes.py b/routes/market_intel_mcp_run_routes.py index b800ae5..4cd2707 100644 --- a/routes/market_intel_mcp_run_routes.py +++ b/routes/market_intel_mcp_run_routes.py @@ -13,6 +13,9 @@ from services.market_intel.mcp_fetch_run_package import ( from services.market_intel.mcp_fetch_run_readiness import ( build_mcp_fetch_run_readiness_preview, ) +from services.market_intel.mcp_fetch_run_receipt import ( + build_mcp_fetch_run_receipt_preview, +) @market_intel_bp.route("/api/market_intel/mcp_fetch_run_package", methods=["GET", "POST"]) @@ -73,3 +76,39 @@ def market_intel_mcp_fetch_run_readiness(): phase=service.phase, ) ) + + +@market_intel_bp.route("/api/market_intel/mcp_fetch_run_receipt", methods=["GET", "POST"]) +@login_required +def market_intel_mcp_fetch_run_receipt(): + run_readiness_package = {} + run_readiness_result = None + manual_fetch_receipt = None + if request.method == "POST": + payload = request.get_json(silent=True) or {} + package = payload.get("run_receipt") or payload.get("receipt_review") or payload + run_readiness_package = ( + package.get("run_readiness_package") + or package.get("run_readiness") + or package.get("readiness_package") + or {} + ) + run_readiness_result = ( + package.get("run_readiness_result") + or package.get("mcp_fetch_run_readiness") + ) + manual_fetch_receipt = ( + package.get("manual_fetch_receipt") + or package.get("fetch_receipt") + or package.get("receipt") + ) + + service = MarketIntelService() + return jsonify( + build_mcp_fetch_run_receipt_preview( + run_readiness_package=run_readiness_package, + run_readiness_result=run_readiness_result, + manual_fetch_receipt=manual_fetch_receipt, + phase=service.phase, + ) + ) diff --git a/services/market_intel/deployment_readiness.py b/services/market_intel/deployment_readiness.py index 0ca7ed6..62cb961 100644 --- a/services/market_intel/deployment_readiness.py +++ b/services/market_intel/deployment_readiness.py @@ -69,6 +69,9 @@ from services.market_intel.mcp_fetch_run_package import ( from services.market_intel.mcp_fetch_run_readiness import ( build_mcp_fetch_run_readiness_preview, ) +from services.market_intel.mcp_fetch_run_receipt import ( + build_mcp_fetch_run_receipt_preview, +) from services.market_intel.mcp_manual_fetch_handoff import ( build_mcp_manual_fetch_handoff_preview, ) @@ -181,6 +184,11 @@ PRODUCTION_SMOKE_TARGETS = ( + ("/api/market_intel/mcp_fetch_run_readiness",) + PRODUCTION_SMOKE_TARGETS[-1:] ) +PRODUCTION_SMOKE_TARGETS = ( + PRODUCTION_SMOKE_TARGETS[:-1] + + ("/api/market_intel/mcp_fetch_run_receipt",) + + 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): @@ -214,6 +222,9 @@ def build_deployment_readiness_preview(*, service, market_intel_tables, schema_s mcp_fetch_run_readiness = build_mcp_fetch_run_readiness_preview( phase=service.phase, ) + mcp_fetch_run_receipt = build_mcp_fetch_run_receipt_preview( + phase=service.phase, + ) scheduler_plan = service.build_scheduler_plan() manual_sample_plan = service.build_manual_sample_plan() manual_sample_acceptance = service.build_manual_sample_acceptance() @@ -525,6 +536,26 @@ def build_deployment_readiness_preview(*, service, market_intel_tables, schema_s and not mcp_fetch_run_readiness["file_written"] and not mcp_fetch_run_readiness["scheduler_attached"] ), + "mcp_fetch_run_receipt_preview_safe": bool( + mcp_fetch_run_receipt["mode"] == "mcp_fetch_run_receipt_preview" + and not mcp_fetch_run_receipt["payload_persisted"] + and not mcp_fetch_run_receipt["run_receipt_persisted"] + and not mcp_fetch_run_receipt["receipt_persisted"] + and not mcp_fetch_run_receipt["run_receipt_file_written"] + and not mcp_fetch_run_receipt["receipt_file_written"] + and not mcp_fetch_run_receipt["manual_fetch_gate_opened_by_api"] + and not mcp_fetch_run_receipt["network_request_allowed"] + and not mcp_fetch_run_receipt["api_executes_cli"] + and not mcp_fetch_run_receipt["api_opens_database_connection"] + and not mcp_fetch_run_receipt["api_writes_database"] + and not mcp_fetch_run_receipt["api_uses_external_network"] + and not mcp_fetch_run_receipt["database_write_executed"] + and not mcp_fetch_run_receipt["fetch_executed"] + and not mcp_fetch_run_receipt["fetch_executed_by_api"] + and not mcp_fetch_run_receipt["cli_executed"] + and not mcp_fetch_run_receipt["file_written"] + and not mcp_fetch_run_receipt["scheduler_attached"] + ), "scheduler_plan_preview_safe": bool( scheduler_plan["mode"] == "scheduler_attach_plan_preview" and not scheduler_plan["scheduler_registration_executed"] @@ -963,6 +994,7 @@ def build_deployment_readiness_preview(*, service, market_intel_tables, schema_s "mcp_fetch_target_review": mcp_fetch_target_review, "mcp_fetch_run_package": mcp_fetch_run_package, "mcp_fetch_run_readiness": mcp_fetch_run_readiness, + "mcp_fetch_run_receipt": mcp_fetch_run_receipt, "scheduler_plan": scheduler_plan, "manual_sample_plan": manual_sample_plan, "manual_sample_acceptance": manual_sample_acceptance, diff --git a/services/market_intel/mcp_fetch_run_receipt.py b/services/market_intel/mcp_fetch_run_receipt.py new file mode 100644 index 0000000..a044287 --- /dev/null +++ b/services/market_intel/mcp_fetch_run_receipt.py @@ -0,0 +1,744 @@ +"""市場情報 MCP manual fetch run receipt review preview。 + +本模組只審核操作員 shell 手動 dry-run fetch 後貼回的 receipt; +不執行 CLI、不發 HTTP request、不寫檔、不開 DB、不掛 scheduler。 +""" + +import ipaddress +from urllib.parse import urlparse + +from services.market_intel.mcp_fetch_run_package import ( + MAX_TOTAL_REQUESTS, + RUN_ARTIFACT_DIR_PREFIX, +) +from services.market_intel.mcp_fetch_run_readiness import ( + build_mcp_fetch_run_readiness_preview, +) + + +FORBIDDEN_SECRET_KEYS = ( + "approval_token", + "approval-token", + "api_key", + "authorization", + "bearer", + "client_secret", + "cookie", + "password", + "secret", + "session_cookie", + "token", +) + +SAFE_SECRET_METADATA_KEYS = { + "operator_confirmed_no_secret_payload", +} + +_BLOCKED_SIDE_EFFECT_KEYS = ( + "allow_api_execution", + "allow_database_write", + "allow_external_network_in_api", + "allow_scheduler_attach", + "api_executed_cli", + "api_executes_cli", + "api_executes_docker", + "api_executes_health_check", + "api_executes_ssh", + "api_opens_database_connection", + "api_uses_external_network", + "api_writes_database", + "attach_scheduler", + "cli_executed", + "command_executed", + "database_commit_executed", + "database_session_created", + "database_write_executed", + "external_network_executed", + "external_network_executed_by_api", + "fetch_executed", + "fetch_executed_by_api", + "file_written", + "manual_fetch_gate_opened_by_api", + "network_request_allowed", + "receipt_file_written_by_api", + "receipt_persisted_by_api", + "run_receipt_file_written", + "run_receipt_persisted", + "scheduler_attached", + "write_database", + "writes_executed", + "would_write_database", +) + + +def _as_dict(value): + return value if isinstance(value, dict) else {} + + +def _as_list(value): + if value is None: + return [] + if isinstance(value, (list, tuple, set)): + return list(value) + return [value] + + +def _safe_int(value): + try: + return int(value or 0) + except (TypeError, ValueError): + return 0 + + +def _has_text(value): + return bool(isinstance(value, str) and value.strip()) + + +def _safe_text(value, limit=500): + if value is None: + return None + text = str(value).strip() + return text[:limit] if text else None + + +def _safe_artifact_path(value, *, require_json=False): + if not isinstance(value, str): + return False + normalized = value.strip().replace("\\", "/") + if not normalized or normalized.startswith("/"): + return False + parts = [part for part in normalized.split("/") if part] + if any(part in (".", "..") for part in parts): + return False + if not normalized.startswith(RUN_ARTIFACT_DIR_PREFIX): + return False + return not require_json or normalized.endswith(".json") + + +def _contains_forbidden_secret_key(value): + if isinstance(value, dict): + for key, nested in value.items(): + normalized_key = str(key).lower() + if normalized_key in SAFE_SECRET_METADATA_KEYS and isinstance(nested, bool): + continue + if any(secret_key in normalized_key for secret_key in FORBIDDEN_SECRET_KEYS): + return True + if _contains_forbidden_secret_key(nested): + return True + elif isinstance(value, list): + return any(_contains_forbidden_secret_key(item) for item in value) + return False + + +def _blocked_side_effects(payload): + found = [] + + def visit(value, path): + if isinstance(value, dict): + for key, item in value.items(): + key_path = f"{path}.{key}" if path else key + if key in _BLOCKED_SIDE_EFFECT_KEYS and bool(item): + found.append(key_path) + visit(item, key_path) + elif isinstance(value, list): + for index, item in enumerate(value): + visit(item, f"{path}[{index}]") + + visit(payload, "") + return found + + +def _is_public_http_url(value): + if not isinstance(value, str): + return False + parsed = urlparse(value.strip()) + if parsed.scheme not in {"http", "https"} or not parsed.netloc: + return False + if parsed.username or parsed.password: + return False + host = (parsed.hostname or "").lower() + if host in {"localhost", "127.0.0.1", "::1"}: + return False + try: + ip = ipaddress.ip_address(host) + except ValueError: + return bool(host and "." in host) + return not ( + ip.is_private + or ip.is_loopback + or ip.is_link_local + or ip.is_reserved + or ip.is_multicast + ) + + +def _run_readiness_from_inputs(run_readiness_package, run_readiness_result, phase): + if isinstance(run_readiness_result, dict) and run_readiness_result: + return run_readiness_result + + run_readiness_package = _as_dict(run_readiness_package) + return build_mcp_fetch_run_readiness_preview( + run_package=( + run_readiness_package.get("run_package") + or run_readiness_package.get("fetch_run_package") + or run_readiness_package.get("run_package_payload") + ), + run_package_result=( + run_readiness_package.get("run_package_result") + or run_readiness_package.get("mcp_fetch_run_package") + ), + operator_readiness=( + run_readiness_package.get("operator_readiness") + or run_readiness_package.get("operator_run_readiness") + ), + phase=phase, + ) + + +def _command_index(run_readiness, run_readiness_package): + run_readiness = _as_dict(run_readiness) + run_readiness_package = _as_dict(run_readiness_package) + commands = {} + package_result = _as_dict( + run_readiness_package.get("run_package_result") + or run_readiness_package.get("mcp_fetch_run_package") + ) + for item in _as_list(package_result.get("command_previews")): + item = _as_dict(item) + key = (item.get("platform_code"), item.get("source_key")) + if key[0] and key[1]: + commands[key] = { + "platform_code": item.get("platform_code"), + "source_key": item.get("source_key"), + "source_url": item.get("source_url"), + "receipt_path": item.get("receipt_path"), + "sample_limit": item.get("sample_limit"), + "delay_seconds": item.get("delay_seconds"), + "timeout_seconds": item.get("timeout_seconds"), + "argv_preview_count": len(_as_list(item.get("argv_preview"))), + } + + for item in _as_list(run_readiness.get("command_readiness")): + item = _as_dict(item) + key = (item.get("platform_code"), item.get("source_key")) + if not key[0] or not key[1]: + continue + existing = commands.get(key, {}) + commands[key] = { + "platform_code": item.get("platform_code"), + "source_key": item.get("source_key"), + "source_url": existing.get("source_url") or item.get("source_url"), + "receipt_path": item.get("receipt_path") or existing.get("receipt_path"), + "sample_limit": existing.get("sample_limit") or item.get("sample_limit"), + "delay_seconds": existing.get("delay_seconds") or item.get("delay_seconds"), + "timeout_seconds": existing.get("timeout_seconds") or item.get("timeout_seconds"), + "argv_preview_count": len(_as_list(item.get("argv_preview"))) + or existing.get("argv_preview_count", 0), + } + + return commands + + +def _sample_run_receipt_package(): + readiness_preview = build_mcp_fetch_run_readiness_preview() + run_readiness_package = readiness_preview["sample_run_readiness_package"] + run_readiness_result = build_mcp_fetch_run_readiness_preview( + run_package=run_readiness_package["run_package"], + run_package_result=run_readiness_package["run_package_result"], + operator_readiness=run_readiness_package["operator_readiness"], + ) + commands = _command_index(run_readiness_result, run_readiness_package) + receipt_sources = [] + for command in commands.values(): + receipt_sources.append( + { + "platform_code": command["platform_code"], + "source_key": command["source_key"], + "source_url": command["source_url"], + "receipt_path": command["receipt_path"], + "status": "dry_run_completed", + "executed_by": "operator_shell", + "dry_run_first": True, + "request_count": 1, + "success_count": 1, + "error_count": 0, + "http_status_codes": [200], + "campaign_candidate_count": 1, + "product_candidate_count": 0, + "api_executed_cli": False, + "external_network_executed_by_api": False, + "database_write_executed": False, + "scheduler_attached": False, + } + ) + + artifact_path = run_readiness_package["operator_readiness"]["dry_run_receipt_path"] + return { + "run_readiness_package": run_readiness_package, + "run_readiness_result": run_readiness_result, + "manual_fetch_receipt": { + "receipt_id": "market-intel-manual-fetch-sample-receipt", + "run_label": "market-intel-manual-fetch-sample", + "receipt_artifact_path": artifact_path, + "executed_by": "operator_shell", + "dry_run_first": True, + "operator_shell_external_network_executed": True, + "operator_confirmed_public_pages_only": True, + "operator_confirmed_no_login_no_antibot": True, + "operator_confirmed_no_secret_payload": True, + "operator_confirmed_no_api_execution": True, + "operator_confirmed_no_database_write": True, + "operator_confirmed_no_scheduler_attach": True, + "summary": { + "source_count": len(receipt_sources), + "request_count": len(receipt_sources), + "success_count": len(receipt_sources), + "error_count": 0, + }, + "sources": receipt_sources, + }, + } + + +def _readiness_summary(run_readiness): + run_readiness = _as_dict(run_readiness) + package = _as_dict(run_readiness.get("run_package_summary")) + side_effects_clear = bool( + not run_readiness.get("manual_fetch_gate_opened_by_api") + and not run_readiness.get("network_request_allowed") + and not run_readiness.get("api_executes_cli") + and not run_readiness.get("api_opens_database_connection") + and not run_readiness.get("api_writes_database") + and not run_readiness.get("api_uses_external_network") + and not run_readiness.get("database_write_executed") + and not run_readiness.get("fetch_executed") + and not run_readiness.get("cli_executed") + and not run_readiness.get("file_written") + and not run_readiness.get("scheduler_attached") + ) + return { + "mode": run_readiness.get("mode"), + "accepted": bool(run_readiness.get("mcp_fetch_run_readiness_accepted")), + "ready_for_manual_fetch_operator_run": bool( + run_readiness.get("ready_for_manual_fetch_operator_run") + ), + "ready_for_manual_fetch_run_receipt_gate": bool( + run_readiness.get("ready_for_manual_fetch_run_receipt_gate") + ), + "operator_shell_external_network_required": bool( + run_readiness.get("operator_shell_external_network_required") + ), + "command_count": _safe_int(run_readiness.get("command_readiness_count")), + "max_total_requests": _safe_int(package.get("max_total_requests")), + "stop_after_error_count": _safe_int(package.get("stop_after_error_count")), + "side_effects_clear": side_effects_clear, + "blocked_reasons": run_readiness.get("blocked_reasons", []), + } + + +def _receipt_summary(manual_fetch_receipt): + receipt = _as_dict(manual_fetch_receipt) + sources = [_as_dict(item) for item in _as_list(receipt.get("sources"))] + summary = _as_dict(receipt.get("summary")) + source_summaries = [] + for source in sources: + request_count = _safe_int(source.get("request_count")) + error_count = _safe_int(source.get("error_count")) + success_count = _safe_int(source.get("success_count")) + source_summaries.append( + { + "platform_code": _safe_text(source.get("platform_code"), 80), + "source_key": _safe_text(source.get("source_key"), 160), + "source_url": _safe_text(source.get("source_url"), 500), + "receipt_path": _safe_text(source.get("receipt_path"), 500), + "receipt_path_safe": _safe_artifact_path( + source.get("receipt_path"), + require_json=True, + ), + "status": _safe_text(source.get("status"), 80), + "executed_by": _safe_text(source.get("executed_by"), 80), + "dry_run_first": bool(source.get("dry_run_first")), + "request_count": request_count, + "success_count": success_count, + "error_count": error_count, + "source_url_public": _is_public_http_url(source.get("source_url")), + "api_executed_cli": bool(source.get("api_executed_cli")), + "external_network_executed_by_api": bool( + source.get("external_network_executed_by_api") + ), + "database_write_executed": bool( + source.get("database_write_executed") + ), + "scheduler_attached": bool(source.get("scheduler_attached")), + } + ) + + request_count = _safe_int(summary.get("request_count")) or sum( + item["request_count"] for item in source_summaries + ) + success_count = _safe_int(summary.get("success_count")) or sum( + item["success_count"] for item in source_summaries + ) + error_count = _safe_int(summary.get("error_count")) or sum( + item["error_count"] for item in source_summaries + ) + artifact_path = receipt.get("receipt_artifact_path") + return { + "provided_keys": sorted(receipt.keys()), + "receipt_id": _safe_text(receipt.get("receipt_id"), 160), + "run_label": _safe_text(receipt.get("run_label"), 160), + "receipt_artifact_path": _safe_text(artifact_path, 500), + "receipt_artifact_path_safe": _safe_artifact_path( + artifact_path, + require_json=True, + ), + "executed_by": _safe_text(receipt.get("executed_by"), 80), + "dry_run_first": bool(receipt.get("dry_run_first")), + "operator_shell_external_network_executed": bool( + receipt.get("operator_shell_external_network_executed") + ), + "operator_confirmed_public_pages_only": bool( + receipt.get("operator_confirmed_public_pages_only") + ), + "operator_confirmed_no_login_no_antibot": bool( + receipt.get("operator_confirmed_no_login_no_antibot") + ), + "operator_confirmed_no_secret_payload": bool( + receipt.get("operator_confirmed_no_secret_payload") + ), + "operator_confirmed_no_api_execution": bool( + receipt.get("operator_confirmed_no_api_execution") + ), + "operator_confirmed_no_database_write": bool( + receipt.get("operator_confirmed_no_database_write") + ), + "operator_confirmed_no_scheduler_attach": bool( + receipt.get("operator_confirmed_no_scheduler_attach") + ), + "source_count": len(source_summaries), + "summary_source_count": _safe_int(summary.get("source_count")), + "request_count": request_count, + "success_count": success_count, + "error_count": error_count, + "sources": source_summaries, + "source_keys": sorted( + ( + item["platform_code"], + item["source_key"], + ) + for item in source_summaries + if item["platform_code"] and item["source_key"] + ), + "receipt_paths": sorted( + item["receipt_path"] + for item in source_summaries + if item["receipt_path"] + ), + "all_receipt_paths_safe": bool( + source_summaries + and all(item["receipt_path_safe"] for item in source_summaries) + ), + "all_urls_public": bool( + source_summaries + and all(item["source_url_public"] for item in source_summaries) + ), + "all_status_dry_run_completed": bool( + source_summaries + and all(item["status"] == "dry_run_completed" for item in source_summaries) + ), + "all_executed_by_operator_shell": bool( + receipt.get("executed_by") == "operator_shell" + and source_summaries + and all(item["executed_by"] == "operator_shell" for item in source_summaries) + ), + "all_dry_run_first": bool( + receipt.get("dry_run_first") is True + and source_summaries + and all(item["dry_run_first"] for item in source_summaries) + ), + "secret_or_token_submitted_to_api": _contains_forbidden_secret_key(receipt), + "blocked_side_effects": _blocked_side_effects(receipt), + } + + +def _expected_summary(command_index): + expected_source_keys = sorted(command_index) + expected_receipt_paths = sorted( + command.get("receipt_path") + for command in command_index.values() + if command.get("receipt_path") + ) + return { + "command_count": len(command_index), + "expected_source_keys": expected_source_keys, + "expected_receipt_paths": expected_receipt_paths, + "all_expected_receipt_paths_safe": bool( + expected_receipt_paths + and all( + _safe_artifact_path(path, require_json=True) + for path in expected_receipt_paths + ) + ), + "commands": list(command_index.values()), + } + + +def _receipt_gates( + *, + run_readiness_received, + receipt_received, + receipt_valid_object, + readiness, + expected, + receipt, +): + expected_source_keys = set(expected["expected_source_keys"]) + receipt_source_keys = set(tuple(item) for item in receipt["source_keys"]) + expected_receipt_paths = set(expected["expected_receipt_paths"]) + receipt_paths = set(receipt["receipt_paths"]) + max_requests = readiness["max_total_requests"] or MAX_TOTAL_REQUESTS + stop_after_error_count = readiness["stop_after_error_count"] or 0 + operator_boundaries_confirmed = bool( + receipt["operator_confirmed_public_pages_only"] + and receipt["operator_confirmed_no_login_no_antibot"] + and receipt["operator_confirmed_no_secret_payload"] + and receipt["operator_confirmed_no_api_execution"] + and receipt["operator_confirmed_no_database_write"] + and receipt["operator_confirmed_no_scheduler_attach"] + ) + return [ + { + "key": "run_readiness_payload_or_result_received", + "label": "已提供 run readiness package 或已審核結果", + "passed": run_readiness_received, + }, + { + "key": "run_readiness_accepted", + "label": "run readiness gate 必須已通過", + "passed": readiness["accepted"], + }, + { + "key": "run_readiness_ready_for_receipt_gate", + "label": "run readiness 只放行到 receipt review gate", + "passed": readiness["ready_for_manual_fetch_run_receipt_gate"], + }, + { + "key": "run_readiness_side_effect_free", + "label": "readiness 結果未顯示 API 執行、連外、寫檔、寫 DB 或掛 scheduler", + "passed": readiness["side_effects_clear"], + }, + { + "key": "manual_fetch_receipt_received", + "label": "已提供操作員 dry-run fetch receipt", + "passed": receipt_received, + }, + { + "key": "manual_fetch_receipt_valid_object", + "label": "receipt payload 必須是 JSON object", + "passed": receipt_valid_object, + }, + { + "key": "receipt_identity_recorded", + "label": "receipt 必須記錄 receipt_id、run_label 與 artifact path", + "passed": bool( + receipt["receipt_id"] + and receipt["run_label"] + and receipt["receipt_artifact_path_safe"] + ), + }, + { + "key": "expected_command_manifest_present", + "label": "readiness 必須有 command manifest 可供對帳", + "passed": bool( + expected["command_count"] > 0 + and expected["all_expected_receipt_paths_safe"] + ), + }, + { + "key": "receipt_source_count_matches_command_count", + "label": "receipt source 數必須等於 readiness command 數", + "passed": bool( + receipt["source_count"] > 0 + and receipt["source_count"] == expected["command_count"] + ), + }, + { + "key": "receipt_sources_match_expected", + "label": "receipt 的 platform/source 必須完全對齊 command manifest", + "passed": bool( + expected_source_keys + and receipt_source_keys == expected_source_keys + ), + }, + { + "key": "receipt_paths_match_expected", + "label": "receipt path 必須完全對齊 command preview 且位於安全目錄", + "passed": bool( + expected_receipt_paths + and receipt_paths == expected_receipt_paths + and receipt["all_receipt_paths_safe"] + ), + }, + { + "key": "receipt_status_dry_run_completed", + "label": "每個來源都必須回報 dry_run_completed", + "passed": receipt["all_status_dry_run_completed"], + }, + { + "key": "receipt_executed_by_operator_shell", + "label": "receipt 必須標明由操作員 shell 執行,不是 API", + "passed": receipt["all_executed_by_operator_shell"], + }, + { + "key": "receipt_dry_run_first_confirmed", + "label": "receipt 必須確認 dry-run-first", + "passed": receipt["all_dry_run_first"], + }, + { + "key": "receipt_operator_external_network_only", + "label": "外部網路只能發生在操作員 shell,不在 API", + "passed": bool( + receipt["operator_shell_external_network_executed"] + and readiness["operator_shell_external_network_required"] + ), + }, + { + "key": "receipt_request_budget_within_limit", + "label": "request count 不得超過 run package 上限", + "passed": bool(0 <= receipt["request_count"] <= max_requests), + }, + { + "key": "receipt_error_count_within_stop_limit", + "label": "error count 不得超過 stop-after-error 門檻", + "passed": bool(receipt["error_count"] <= stop_after_error_count), + }, + { + "key": "receipt_urls_public_http", + "label": "receipt source_url 必須是公開 http(s) URL,且不含帳密", + "passed": receipt["all_urls_public"], + }, + { + "key": "receipt_operator_boundaries_confirmed", + "label": "操作員確認公開頁、無登入/反爬、無 secret、無 API/DB/scheduler 副作用", + "passed": operator_boundaries_confirmed, + }, + { + "key": "receipt_no_secret_or_token_key", + "label": "receipt payload 不得包含 secret、cookie、password 或 token key", + "passed": not receipt["secret_or_token_submitted_to_api"], + }, + { + "key": "receipt_side_effect_free", + "label": "receipt payload 不得要求 API 執行、連外、寫檔、寫 DB 或掛 scheduler", + "passed": not receipt["blocked_side_effects"], + }, + ] + + +def build_mcp_fetch_run_receipt_preview( + *, + run_readiness_package=None, + run_readiness_result=None, + manual_fetch_receipt=None, + phase=None, +): + """建立 manual fetch run receipt review;不執行任何抓取或寫入。""" + run_readiness_package = _as_dict(run_readiness_package) + run_readiness_result_received = bool( + isinstance(run_readiness_result, dict) and run_readiness_result + ) + receipt_valid_object = isinstance(manual_fetch_receipt, dict) if ( + manual_fetch_receipt is not None + ) else True + receipt_payload = _as_dict(manual_fetch_receipt) + run_readiness = _run_readiness_from_inputs( + run_readiness_package, + run_readiness_result, + phase, + ) + run_readiness_received = bool( + run_readiness_package or run_readiness_result_received + ) + run_receipt_payload_received = bool( + run_readiness_received or receipt_payload or manual_fetch_receipt is not None + ) + receipt_received = bool(receipt_payload) + commands = _command_index(run_readiness, run_readiness_package) + readiness = _readiness_summary(run_readiness) + expected = _expected_summary(commands) + receipt = _receipt_summary(receipt_payload) + gates = _receipt_gates( + run_readiness_received=run_readiness_received, + receipt_received=receipt_received, + receipt_valid_object=receipt_valid_object, + readiness=readiness, + expected=expected, + receipt=receipt, + ) + blocked_reasons = [gate["key"] for gate in gates if not gate["passed"]] + accepted = bool(run_receipt_payload_received and not blocked_reasons) + + return { + "mode": ( + "mcp_fetch_run_receipt_review" + if run_receipt_payload_received + else "mcp_fetch_run_receipt_preview" + ), + "phase": phase, + "run_receipt_payload_received": run_receipt_payload_received, + "run_readiness_received": run_readiness_received, + "manual_fetch_receipt_received": receipt_received, + "manual_fetch_receipt_valid_object": receipt_valid_object, + "run_readiness_accepted": readiness["accepted"], + "mcp_fetch_run_receipt_accepted": accepted, + "run_receipt_ready": accepted, + "operator_shell_fetch_receipt_received": accepted, + "ready_for_manual_fetch_result_parser_review": accepted, + "ready_for_api_database_write": False, + "ready_for_scheduler_attach": False, + "manual_fetch_gate_opened_by_api": False, + "network_request_allowed": False, + "operator_shell_external_network_observed": bool( + receipt["operator_shell_external_network_executed"] + ), + "fetch_executed_by_api": False, + "fetch_executed": False, + "cli_executed": False, + "database_write_executed": False, + "scheduler_attached": False, + "expected_command_count": expected["command_count"], + "receipt_source_count": receipt["source_count"], + "request_count": receipt["request_count"], + "error_count": receipt["error_count"], + "gate_count": len(gates), + "passed_gate_count": sum(1 for gate in gates if gate["passed"]), + "blocked_reasons": blocked_reasons, + "gates": gates, + "run_readiness_summary": readiness, + "expected_command_manifest": expected, + "manual_fetch_receipt_summary": receipt, + "sample_run_receipt_package": _sample_run_receipt_package(), + "next_operator_steps": [ + "receipt 通過後,只代表可進結果 parser review,不代表可寫 market_*", + "parser、DB import、scheduler attach、AI/Telegram 摘要都必須另開獨立 gate", + "API/UI 仍不得執行 CLI、不得抓外站、不得寫檔、不得開 DB、不得掛 scheduler", + ], + "payload_persisted": False, + "run_receipt_persisted": False, + "receipt_persisted": False, + "run_receipt_file_written": False, + "receipt_file_written": False, + "api_executes_health_check": False, + "api_executes_docker": False, + "api_executes_ssh": False, + "api_executes_cli": False, + "api_opens_database_connection": False, + "api_writes_database": False, + "api_uses_external_network": False, + "database_session_created": False, + "database_commit_executed": False, + "external_network_executed": False, + "file_written": False, + "writes_executed": False, + "would_write_database": False, + } diff --git a/services/market_intel/phase.py b/services/market_intel/phase.py index 2126a83..276d32f 100644 --- a/services/market_intel/phase.py +++ b/services/market_intel/phase.py @@ -1,3 +1,3 @@ """市場情報 rollout phase 單一來源。""" -MARKET_INTEL_PHASE = "phase_123_market_intel_mcp_fetch_run_readiness" +MARKET_INTEL_PHASE = "phase_124_market_intel_mcp_fetch_run_receipt" diff --git a/templates/market_intel/disabled.html b/templates/market_intel/disabled.html index 098cc36..3331654 100644 --- a/templates/market_intel/disabled.html +++ b/templates/market_intel/disabled.html @@ -734,6 +734,32 @@ +
+
+
+

MCP / FETCH RUN RECEIPT

+

MCP Fetch Run Receipt 審核

+
+ +
+
+ loading +
+
+
讀取 MCP Fetch Run Receipt 審核中...
+
+
+ +
+ +
+
+
+
@@ -1245,6 +1271,7 @@ const mcpFetchTargetReviewRoot = document.querySelector('[data-market-intel-mcp-fetch-target-review]'); const mcpFetchRunPackageRoot = document.querySelector('[data-market-intel-mcp-fetch-run-package]'); const mcpFetchRunReadinessRoot = document.querySelector('[data-market-intel-mcp-fetch-run-readiness]'); + const mcpFetchRunReceiptRoot = document.querySelector('[data-market-intel-mcp-fetch-run-receipt]'); const manualSampleRoot = document.querySelector('[data-market-intel-manual-sample]'); const sampleAcceptanceRoot = document.querySelector('[data-market-intel-sample-acceptance]'); const sampleReviewRoot = document.querySelector('[data-market-intel-sample-review]'); @@ -1261,7 +1288,7 @@ const liveInventoryRoot = document.querySelector('[data-market-intel-live-inventory]'); const approvalRoot = document.querySelector('[data-market-intel-approval]'); const deployRoot = document.querySelector('[data-market-intel-deploy]'); - if (!root && !writerRoot && !cliRoot && !dbProbeRoot && !seedDiffRoot && !legacyBridgeRoot && !mcpReadinessRoot && !mcpPreflightRoot && !mcpActivationRoot && !mcpFetchGateRoot && !mcpCompletionRoot && !mcpActivationEvidenceRoot && !mcpRuntimeSmokeRoot && !mcpRuntimePromotionRoot && !mcpManualFetchHandoffRoot && !mcpFetchTargetReviewRoot && !mcpFetchRunPackageRoot && !mcpFetchRunReadinessRoot && !manualSampleRoot && !sampleAcceptanceRoot && !sampleReviewRoot && !schedulerRoot && !matchReviewRoot && !opportunityRoot && !opportunityScoringRoot && !opportunityEvidenceRoot && !opportunityAlertRoot && !migrationRoot && !migrationDrillRoot && !catalogReviewRoot && !liveSmokeRoot && !liveInventoryRoot && !approvalRoot && !deployRoot) return; + if (!root && !writerRoot && !cliRoot && !dbProbeRoot && !seedDiffRoot && !legacyBridgeRoot && !mcpReadinessRoot && !mcpPreflightRoot && !mcpActivationRoot && !mcpFetchGateRoot && !mcpCompletionRoot && !mcpActivationEvidenceRoot && !mcpRuntimeSmokeRoot && !mcpRuntimePromotionRoot && !mcpManualFetchHandoffRoot && !mcpFetchTargetReviewRoot && !mcpFetchRunPackageRoot && !mcpFetchRunReadinessRoot && !mcpFetchRunReceiptRoot && !manualSampleRoot && !sampleAcceptanceRoot && !sampleReviewRoot && !schedulerRoot && !matchReviewRoot && !opportunityRoot && !opportunityScoringRoot && !opportunityEvidenceRoot && !opportunityAlertRoot && !migrationRoot && !migrationDrillRoot && !catalogReviewRoot && !liveSmokeRoot && !liveInventoryRoot && !approvalRoot && !deployRoot) return; const meta = root ? root.querySelector('[data-market-intel-preview-meta]') : null; const body = root ? root.querySelector('[data-market-intel-preview-body]') : null; @@ -1350,6 +1377,12 @@ const mcpFetchRunReadinessReview = mcpFetchRunReadinessRoot ? mcpFetchRunReadinessRoot.querySelector('[data-market-intel-mcp-fetch-run-readiness-review]') : null; const mcpFetchRunReadinessRefresh = mcpFetchRunReadinessRoot ? mcpFetchRunReadinessRoot.querySelector('[data-market-intel-mcp-fetch-run-readiness-refresh]') : null; const mcpFetchRunReadinessEndpoint = "{{ url_for('market_intel.market_intel_mcp_fetch_run_readiness') }}"; + const mcpFetchRunReceiptMeta = mcpFetchRunReceiptRoot ? mcpFetchRunReceiptRoot.querySelector('[data-market-intel-mcp-fetch-run-receipt-meta]') : null; + const mcpFetchRunReceiptBody = mcpFetchRunReceiptRoot ? mcpFetchRunReceiptRoot.querySelector('[data-market-intel-mcp-fetch-run-receipt-body]') : null; + const mcpFetchRunReceiptInput = mcpFetchRunReceiptRoot ? mcpFetchRunReceiptRoot.querySelector('[data-market-intel-mcp-fetch-run-receipt-input]') : null; + const mcpFetchRunReceiptReview = mcpFetchRunReceiptRoot ? mcpFetchRunReceiptRoot.querySelector('[data-market-intel-mcp-fetch-run-receipt-review]') : null; + const mcpFetchRunReceiptRefresh = mcpFetchRunReceiptRoot ? mcpFetchRunReceiptRoot.querySelector('[data-market-intel-mcp-fetch-run-receipt-refresh]') : null; + const mcpFetchRunReceiptEndpoint = "{{ url_for('market_intel.market_intel_mcp_fetch_run_receipt') }}"; const manualSampleMeta = manualSampleRoot ? manualSampleRoot.querySelector('[data-market-intel-manual-sample-meta]') : null; const manualSampleBody = manualSampleRoot ? manualSampleRoot.querySelector('[data-market-intel-manual-sample-body]') : null; const manualSampleRefresh = manualSampleRoot ? manualSampleRoot.querySelector('[data-market-intel-manual-sample-refresh]') : null; @@ -3153,6 +3186,140 @@ } }; + const renderMcpFetchRunReceiptMeta = data => { + mcpFetchRunReceiptMeta.innerHTML = [ + `mode=${data.mode || 'unknown'}`, + `accepted=${data.mcp_fetch_run_receipt_accepted ? 'yes' : 'no'}`, + `gates=${data.passed_gate_count || 0}/${data.gate_count || 0}`, + `commands=${data.expected_command_count || 0}`, + `sources=${data.receipt_source_count || 0}`, + `api_network=${data.network_request_allowed ? 'open' : 'closed'}` + ].map(item => `${escapeHtml(item)}`).join(''); + }; + + const renderMcpFetchRunReceiptBody = data => { + const blockers = (data.blocked_reasons || []).join(' / '); + const gates = data.gates || []; + const receipt = data.manual_fetch_receipt_summary || {}; + const expected = data.expected_command_manifest || {}; + const commands = expected.commands || []; + const steps = data.next_operator_steps || []; + const renderCheck = (key, label, status) => ` +
+
+ ${escapeHtml(key)} + ${escapeHtml(label || '')} +
+ ${escapeHtml(status)} +
+ `; + mcpFetchRunReceiptBody.innerHTML = ` +
此 receipt 只審核操作員 shell dry-run fetch 後貼回的安全摘要;API 不執行 CLI、不抓外站、不保存 receipt、不寫 DB、不掛 scheduler。${blockers ? `阻擋:${escapeHtml(blockers)}` : ''}
+
+
+

RECEIPT GATES

+
${ + gates.length + ? gates.map(item => renderCheck(item.key, item.label, item.passed ? 'PASS' : 'BLOCK')).join('') + : '
尚未提供 receipt gates。
' + }
+
+
+

RECEIPT SUMMARY

+
+ ${renderCheck('receipt_id', receipt.receipt_id || 'missing', receipt.receipt_id ? 'RECORDED' : 'MISSING')} + ${renderCheck('artifact_path', receipt.receipt_artifact_path || 'missing', receipt.receipt_artifact_path_safe ? 'SAFE' : 'BLOCK')} + ${renderCheck('operator_shell', `requests=${receipt.request_count || 0} / errors=${receipt.error_count || 0}`, receipt.operator_shell_external_network_executed ? 'OBSERVED' : 'PENDING')} + ${renderCheck('secret_payload', 'no secret/cookie/password/token key submitted to API', receipt.secret_or_token_submitted_to_api ? 'BLOCK' : 'CLEAR')} +
+
+
+

SOURCE RECEIPTS

+
${ + (receipt.sources || []).length + ? receipt.sources.map(item => renderCheck( + `${item.platform_code || 'unknown'}:${item.source_key || 'source'}`, + `${item.receipt_path || 'no receipt'} / ${item.source_url || 'no url'}`, + item.status === 'dry_run_completed' ? 'DRY-RUN' : 'BLOCK' + )).join('') + : commands.length + ? commands.map(item => renderCheck( + `${item.platform_code || 'unknown'}:${item.source_key || 'source'}`, + `${item.receipt_path || 'no receipt'} / ${item.source_url || 'no url'}`, + 'EXPECTED' + )).join('') + : '
尚未提供 source receipt。
' + }
+
+
+

BOUNDARY / NEXT

+
+ ${renderCheck( + 'readiness', + `${data.run_readiness_accepted ? 'accepted' : 'pending'} / expected=${expected.command_count || 0}`, + data.run_readiness_accepted ? 'ACCEPTED' : 'PENDING' + )} + ${renderCheck( + 'api_boundary', + 'no CLI / no external request / no file / no DB / no scheduler', + data.api_executes_cli ? 'BLOCK' : 'CLOSED' + )} + ${steps.map((item, index) => renderCheck(`step_${index + 1}`, item, 'NEXT')).join('')} +
+
+
+ `; + if (mcpFetchRunReceiptInput && !mcpFetchRunReceiptInput.value.trim() && data.sample_run_receipt_package) { + mcpFetchRunReceiptInput.value = JSON.stringify(data.sample_run_receipt_package, null, 2); + } + }; + + const loadMcpFetchRunReceipt = async () => { + if (!mcpFetchRunReceiptMeta || !mcpFetchRunReceiptBody) return; + mcpFetchRunReceiptBody.innerHTML = '
讀取 MCP Fetch Run Receipt 審核中...
'; + try { + const response = await fetch(mcpFetchRunReceiptEndpoint, { credentials: 'same-origin' }); + if (!response.ok) throw new Error(`HTTP ${response.status}`); + const data = await response.json(); + renderMcpFetchRunReceiptMeta(data); + renderMcpFetchRunReceiptBody(data); + } catch (error) { + mcpFetchRunReceiptMeta.innerHTML = 'error'; + mcpFetchRunReceiptBody.innerHTML = `
MCP Fetch Run Receipt 審核讀取失敗:${escapeHtml(error.message)}
`; + } + }; + + const reviewMcpFetchRunReceipt = async () => { + if (!mcpFetchRunReceiptMeta || !mcpFetchRunReceiptBody || !mcpFetchRunReceiptInput) return; + let parsed; + try { + parsed = JSON.parse(mcpFetchRunReceiptInput.value || '{}'); + } catch (error) { + mcpFetchRunReceiptMeta.innerHTML = 'json_error'; + mcpFetchRunReceiptBody.innerHTML = `
JSON 格式錯誤:${escapeHtml(error.message)}
`; + return; + } + mcpFetchRunReceiptBody.innerHTML = '
審核 MCP Fetch Run Receipt 中...
'; + try { + const response = await fetch(mcpFetchRunReceiptEndpoint, { + method: 'POST', + credentials: 'same-origin', + headers: { + 'Content-Type': 'application/json', + 'X-CSRFToken': csrfToken + }, + body: JSON.stringify({ run_receipt: parsed }) + }); + const data = await response.json(); + if (!response.ok && !data.mode) throw new Error(`HTTP ${response.status}`); + renderMcpFetchRunReceiptMeta(data); + renderMcpFetchRunReceiptBody(data); + } catch (error) { + mcpFetchRunReceiptMeta.innerHTML = 'error'; + mcpFetchRunReceiptBody.innerHTML = `
MCP Fetch Run Receipt 審核失敗:${escapeHtml(error.message)}
`; + } + }; + const renderManualSampleMeta = data => { manualSampleMeta.innerHTML = [ `mode=${data.mode || 'unknown'}`, @@ -12598,6 +12765,12 @@ if (mcpFetchRunReadinessReview) { mcpFetchRunReadinessReview.addEventListener('click', reviewMcpFetchRunReadiness); } + if (mcpFetchRunReceiptRefresh) { + mcpFetchRunReceiptRefresh.addEventListener('click', loadMcpFetchRunReceipt); + } + if (mcpFetchRunReceiptReview) { + mcpFetchRunReceiptReview.addEventListener('click', reviewMcpFetchRunReceipt); + } if (manualSampleRefresh) { manualSampleRefresh.addEventListener('click', loadManualSample); } @@ -12859,6 +13032,7 @@ loadMcpFetchTargetReview(); loadMcpFetchRunPackage(); loadMcpFetchRunReadiness(); + loadMcpFetchRunReceipt(); loadManualSample(); loadSampleAcceptance(); loadSampleReview(); diff --git a/tests/test_market_intel_skeleton.py b/tests/test_market_intel_skeleton.py index acd0f17..b95debc 100644 --- a/tests/test_market_intel_skeleton.py +++ b/tests/test_market_intel_skeleton.py @@ -26,6 +26,9 @@ from services.market_intel.mcp_fetch_run_package import ( from services.market_intel.mcp_fetch_run_readiness import ( build_mcp_fetch_run_readiness_preview, ) +from services.market_intel.mcp_fetch_run_receipt import ( + build_mcp_fetch_run_receipt_preview, +) from services.market_intel.mcp_fetch_target_review import ( build_mcp_fetch_target_review_preview, ) @@ -982,6 +985,12 @@ def test_market_intel_preview_template_uses_safe_fetch_false_endpoint(): assert "data-market-intel-mcp-fetch-run-readiness-operator" in template assert "data-market-intel-mcp-fetch-run-readiness-commands" in template assert "data-market-intel-mcp-fetch-run-readiness-next" in template + assert "market_intel.market_intel_mcp_fetch_run_receipt" in template + assert "data-market-intel-mcp-fetch-run-receipt" in template + assert "data-market-intel-mcp-fetch-run-receipt-gates" in template + assert "data-market-intel-mcp-fetch-run-receipt-receipt" in template + assert "data-market-intel-mcp-fetch-run-receipt-sources" in template + assert "data-market-intel-mcp-fetch-run-receipt-next" in template assert "market_intel.market_intel_manual_sample_plan" in template assert "market_intel.market_intel_manual_sample_acceptance" in template assert "market_intel.market_intel_manual_sample_review" in template @@ -1452,7 +1461,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_123_market_intel_mcp_fetch_run_readiness" + assert bridge["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert bridge["execute_requested"] is False assert bridge["read_only_query_executed"] is False assert bridge["database_connection_opened"] is False @@ -1610,7 +1619,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_123_market_intel_mcp_fetch_run_readiness" + assert contract["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert contract["caller"] == "market_intel" assert contract["contract_ready"] is True assert contract["blocked_reasons"] == [] @@ -1743,7 +1752,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_123_market_intel_mcp_fetch_run_readiness" + assert data["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert data["deployment_actions_executed"] is False assert data["docker_command_executed"] is False assert data["ssh_command_executed"] is False @@ -1756,7 +1765,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_123_market_intel_mcp_fetch_run_readiness" + assert gate["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert gate["fetch_requested"] is True assert gate["manual_fetch_gate_open"] is False assert gate["network_request_allowed"] is False @@ -1826,7 +1835,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_123_market_intel_mcp_fetch_run_readiness" + assert data["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert data["fetch_requested"] is False assert data["network_request_allowed"] is False assert data["external_network_executed"] is False @@ -1840,7 +1849,7 @@ def test_mcp_completion_audit_summarizes_external_and_internal_state(monkeypatch audit = MarketIntelService().build_mcp_completion_audit() assert audit["mode"] == "mcp_completion_audit_preview" - assert audit["phase"] == "phase_123_market_intel_mcp_fetch_run_readiness" + assert audit["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert audit["audit_ready_for_operator_review"] is True assert audit["audit_preview_safe"] is True assert audit["external_mcp_runtime_complete"] is False @@ -1914,7 +1923,7 @@ def test_mcp_completion_audit_route_is_preview_only(): assert response.status_code == 200 assert data["mode"] == "mcp_completion_audit_preview" - assert data["phase"] == "phase_123_market_intel_mcp_fetch_run_readiness" + assert data["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert data["audit_preview_safe"] is True assert data["external_mcp_runtime_complete"] is False assert data["internal_mcp_contract_complete"] is True @@ -1931,11 +1940,11 @@ def test_mcp_completion_audit_route_is_preview_only(): def test_mcp_activation_evidence_preview_is_safe_without_payload(): evidence = build_mcp_activation_evidence_preview( - phase="phase_123_market_intel_mcp_fetch_run_readiness" + phase="phase_124_market_intel_mcp_fetch_run_receipt" ) assert evidence["mode"] == "mcp_activation_evidence_preview" - assert evidence["phase"] == "phase_123_market_intel_mcp_fetch_run_readiness" + assert evidence["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert evidence["evidence_payload_received"] is False assert evidence["activation_evidence_accepted"] is False assert evidence["ready_for_runtime_promotion"] is False @@ -1960,7 +1969,7 @@ def test_mcp_activation_evidence_accepts_redacted_runtime_evidence(): ] evidence = build_mcp_activation_evidence_preview( evidence=sample, - phase="phase_123_market_intel_mcp_fetch_run_readiness", + phase="phase_124_market_intel_mcp_fetch_run_receipt", ) assert evidence["mode"] == "mcp_activation_evidence_review" @@ -2018,12 +2027,12 @@ def test_mcp_activation_evidence_route_get_and_post_are_preview_only(): assert get_response.status_code == 200 assert get_data["mode"] == "mcp_activation_evidence_preview" - assert get_data["phase"] == "phase_123_market_intel_mcp_fetch_run_readiness" + assert get_data["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert get_data["api_executes_health_check"] is False assert get_data["api_writes_database"] is False assert post_response.status_code == 200 assert post_data["mode"] == "mcp_activation_evidence_review" - assert post_data["phase"] == "phase_123_market_intel_mcp_fetch_run_readiness" + assert post_data["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert post_data["activation_evidence_accepted"] is True assert post_data["payload_persisted"] is False assert post_data["api_opens_database_connection"] is False @@ -2032,11 +2041,11 @@ def test_mcp_activation_evidence_route_get_and_post_are_preview_only(): def test_mcp_runtime_smoke_receipt_preview_is_safe_without_payload(): receipt = build_mcp_runtime_smoke_receipt_preview( - phase="phase_123_market_intel_mcp_fetch_run_readiness" + phase="phase_124_market_intel_mcp_fetch_run_receipt" ) assert receipt["mode"] == "mcp_runtime_smoke_receipt_preview" - assert receipt["phase"] == "phase_123_market_intel_mcp_fetch_run_readiness" + assert receipt["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert receipt["receipt_payload_received"] is False assert receipt["runtime_smoke_receipt_accepted"] is False assert receipt["ready_for_completion_runtime_promotion"] is False @@ -2064,7 +2073,7 @@ def test_mcp_runtime_smoke_receipt_accepts_complete_readiness_receipt(): ] receipt = build_mcp_runtime_smoke_receipt_preview( receipt=sample, - phase="phase_123_market_intel_mcp_fetch_run_readiness", + phase="phase_124_market_intel_mcp_fetch_run_receipt", ) assert receipt["mode"] == "mcp_runtime_smoke_receipt_review" @@ -2129,12 +2138,12 @@ def test_mcp_runtime_smoke_receipt_route_get_and_post_are_preview_only(): assert get_response.status_code == 200 assert get_data["mode"] == "mcp_runtime_smoke_receipt_preview" - assert get_data["phase"] == "phase_123_market_intel_mcp_fetch_run_readiness" + assert get_data["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert get_data["api_executes_health_check"] is False assert get_data["api_writes_database"] is False assert post_response.status_code == 200 assert post_data["mode"] == "mcp_runtime_smoke_receipt_review" - assert post_data["phase"] == "phase_123_market_intel_mcp_fetch_run_readiness" + assert post_data["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert post_data["runtime_smoke_receipt_accepted"] is True assert post_data["receipt_persisted"] is False assert post_data["api_opens_database_connection"] is False @@ -2143,11 +2152,11 @@ def test_mcp_runtime_smoke_receipt_route_get_and_post_are_preview_only(): def test_mcp_runtime_promotion_preview_is_safe_without_payload(): promotion = build_mcp_runtime_promotion_preview( - phase="phase_123_market_intel_mcp_fetch_run_readiness" + phase="phase_124_market_intel_mcp_fetch_run_receipt" ) assert promotion["mode"] == "mcp_runtime_promotion_preview" - assert promotion["phase"] == "phase_123_market_intel_mcp_fetch_run_readiness" + assert promotion["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert promotion["promotion_payload_received"] is False assert promotion["runtime_promotion_accepted"] is False assert promotion["ready_for_completion_runtime_promotion"] is False @@ -2175,7 +2184,7 @@ def test_mcp_runtime_promotion_accepts_evidence_and_receipt_package(): promotion = build_mcp_runtime_promotion_preview( activation_evidence=sample["activation_evidence"], runtime_receipt=sample["runtime_receipt"], - phase="phase_123_market_intel_mcp_fetch_run_readiness", + phase="phase_124_market_intel_mcp_fetch_run_receipt", ) assert promotion["mode"] == "mcp_runtime_promotion_review" @@ -2235,12 +2244,12 @@ def test_mcp_runtime_promotion_route_get_and_post_are_preview_only(): assert get_response.status_code == 200 assert get_data["mode"] == "mcp_runtime_promotion_preview" - assert get_data["phase"] == "phase_123_market_intel_mcp_fetch_run_readiness" + assert get_data["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert get_data["api_executes_health_check"] is False assert get_data["api_writes_database"] is False assert post_response.status_code == 200 assert post_data["mode"] == "mcp_runtime_promotion_review" - assert post_data["phase"] == "phase_123_market_intel_mcp_fetch_run_readiness" + assert post_data["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert post_data["runtime_promotion_accepted"] is True assert post_data["promotion_persisted"] is False assert post_data["api_opens_database_connection"] is False @@ -2249,11 +2258,11 @@ def test_mcp_runtime_promotion_route_get_and_post_are_preview_only(): def test_mcp_manual_fetch_handoff_preview_is_safe_without_payload(): handoff = build_mcp_manual_fetch_handoff_preview( - phase="phase_123_market_intel_mcp_fetch_run_readiness", + phase="phase_124_market_intel_mcp_fetch_run_receipt", ) assert handoff["mode"] == "mcp_manual_fetch_handoff_preview" - assert handoff["phase"] == "phase_123_market_intel_mcp_fetch_run_readiness" + assert handoff["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert handoff["handoff_payload_received"] is False assert handoff["manual_fetch_handoff_accepted"] is False assert handoff["ready_for_manual_fetch_gate_operator_review"] is False @@ -2281,7 +2290,7 @@ def test_mcp_manual_fetch_handoff_accepts_promotion_and_acknowledgements(): handoff = build_mcp_manual_fetch_handoff_preview( promotion_package=sample["promotion_package"], operator_acknowledgements=sample["operator_acknowledgements"], - phase="phase_123_market_intel_mcp_fetch_run_readiness", + phase="phase_124_market_intel_mcp_fetch_run_receipt", ) assert handoff["mode"] == "mcp_manual_fetch_handoff_review" @@ -2343,12 +2352,12 @@ def test_mcp_manual_fetch_handoff_route_get_and_post_are_preview_only(): assert get_response.status_code == 200 assert get_data["mode"] == "mcp_manual_fetch_handoff_preview" - assert get_data["phase"] == "phase_123_market_intel_mcp_fetch_run_readiness" + assert get_data["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert get_data["api_executes_health_check"] is False assert get_data["api_writes_database"] is False assert post_response.status_code == 200 assert post_data["mode"] == "mcp_manual_fetch_handoff_review" - assert post_data["phase"] == "phase_123_market_intel_mcp_fetch_run_readiness" + assert post_data["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert post_data["manual_fetch_handoff_accepted"] is True assert post_data["manual_fetch_gate_opened_by_api"] is False assert post_data["network_request_allowed"] is False @@ -2359,11 +2368,11 @@ def test_mcp_manual_fetch_handoff_route_get_and_post_are_preview_only(): def test_mcp_fetch_target_review_preview_is_safe_without_payload(): review = build_mcp_fetch_target_review_preview( - phase="phase_123_market_intel_mcp_fetch_run_readiness", + phase="phase_124_market_intel_mcp_fetch_run_receipt", ) assert review["mode"] == "mcp_fetch_target_review_preview" - assert review["phase"] == "phase_123_market_intel_mcp_fetch_run_readiness" + assert review["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert review["target_payload_received"] is False assert review["mcp_fetch_target_review_accepted"] is False assert review["ready_for_manual_fetch_run_package_review"] is False @@ -2393,7 +2402,7 @@ def test_mcp_fetch_target_review_accepts_sample_targets(): review = build_mcp_fetch_target_review_preview( handoff_package=sample["handoff_package"], target_review=sample["target_review"], - phase="phase_123_market_intel_mcp_fetch_run_readiness", + phase="phase_124_market_intel_mcp_fetch_run_receipt", ) assert review["mode"] == "mcp_fetch_target_review" @@ -2471,13 +2480,13 @@ def test_mcp_fetch_target_review_route_get_and_post_are_preview_only(): assert get_response.status_code == 200 assert get_data["mode"] == "mcp_fetch_target_review_preview" - assert get_data["phase"] == "phase_123_market_intel_mcp_fetch_run_readiness" + assert get_data["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert get_data["api_executes_health_check"] is False assert get_data["api_writes_database"] is False assert get_data["api_uses_external_network"] is False assert post_response.status_code == 200 assert post_data["mode"] == "mcp_fetch_target_review" - assert post_data["phase"] == "phase_123_market_intel_mcp_fetch_run_readiness" + assert post_data["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert post_data["mcp_fetch_target_review_accepted"] is True assert post_data["manual_fetch_gate_opened_by_api"] is False assert post_data["network_request_allowed"] is False @@ -2489,11 +2498,11 @@ def test_mcp_fetch_target_review_route_get_and_post_are_preview_only(): def test_mcp_fetch_run_package_preview_is_safe_without_payload(): package = build_mcp_fetch_run_package_preview( - phase="phase_123_market_intel_mcp_fetch_run_readiness", + phase="phase_124_market_intel_mcp_fetch_run_receipt", ) assert package["mode"] == "mcp_fetch_run_package_preview" - assert package["phase"] == "phase_123_market_intel_mcp_fetch_run_readiness" + assert package["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert package["run_payload_received"] is False assert package["mcp_fetch_run_package_accepted"] is False assert package["ready_for_manual_fetch_run_readiness_review"] is False @@ -2525,7 +2534,7 @@ def test_mcp_fetch_run_package_accepts_sample_package(): package = build_mcp_fetch_run_package_preview( target_review_package=sample["target_review_package"], operator_run_controls=sample["operator_run_controls"], - phase="phase_123_market_intel_mcp_fetch_run_readiness", + phase="phase_124_market_intel_mcp_fetch_run_receipt", ) assert package["mode"] == "mcp_fetch_run_package_review" @@ -2600,13 +2609,13 @@ def test_mcp_fetch_run_package_route_get_and_post_are_preview_only(): assert get_response.status_code == 200 assert get_data["mode"] == "mcp_fetch_run_package_preview" - assert get_data["phase"] == "phase_123_market_intel_mcp_fetch_run_readiness" + assert get_data["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert get_data["api_executes_cli"] is False assert get_data["api_writes_database"] is False assert get_data["api_uses_external_network"] is False assert post_response.status_code == 200 assert post_data["mode"] == "mcp_fetch_run_package_review" - assert post_data["phase"] == "phase_123_market_intel_mcp_fetch_run_readiness" + assert post_data["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert post_data["mcp_fetch_run_package_accepted"] is True assert post_data["ready_for_manual_fetch_operator_run"] is False assert post_data["manual_fetch_gate_opened_by_api"] is False @@ -2620,11 +2629,11 @@ def test_mcp_fetch_run_package_route_get_and_post_are_preview_only(): def test_mcp_fetch_run_readiness_preview_is_safe_without_payload(): readiness = build_mcp_fetch_run_readiness_preview( - phase="phase_123_market_intel_mcp_fetch_run_readiness", + phase="phase_124_market_intel_mcp_fetch_run_receipt", ) assert readiness["mode"] == "mcp_fetch_run_readiness_preview" - assert readiness["phase"] == "phase_123_market_intel_mcp_fetch_run_readiness" + assert readiness["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert readiness["run_readiness_payload_received"] is False assert readiness["mcp_fetch_run_readiness_accepted"] is False assert readiness["run_readiness_ready"] is False @@ -2659,11 +2668,11 @@ def test_mcp_fetch_run_readiness_accepts_sample_package(): run_package=sample["run_package"], run_package_result=sample["run_package_result"], operator_readiness=sample["operator_readiness"], - phase="phase_123_market_intel_mcp_fetch_run_readiness", + phase="phase_124_market_intel_mcp_fetch_run_receipt", ) assert readiness["mode"] == "mcp_fetch_run_readiness_review" - assert readiness["phase"] == "phase_123_market_intel_mcp_fetch_run_readiness" + assert readiness["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert readiness["mcp_fetch_run_readiness_accepted"] is True assert readiness["run_readiness_ready"] is True assert readiness["ready_for_manual_fetch_operator_run"] is True @@ -2739,13 +2748,13 @@ def test_mcp_fetch_run_readiness_route_get_and_post_are_preview_only(): assert get_response.status_code == 200 assert get_data["mode"] == "mcp_fetch_run_readiness_preview" - assert get_data["phase"] == "phase_123_market_intel_mcp_fetch_run_readiness" + assert get_data["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert get_data["api_executes_cli"] is False assert get_data["api_writes_database"] is False assert get_data["api_uses_external_network"] is False assert post_response.status_code == 200 assert post_data["mode"] == "mcp_fetch_run_readiness_review" - assert post_data["phase"] == "phase_123_market_intel_mcp_fetch_run_readiness" + assert post_data["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert post_data["mcp_fetch_run_readiness_accepted"] is True assert post_data["ready_for_manual_fetch_operator_run"] is True assert post_data["manual_fetch_gate_opened_by_api"] is False @@ -2757,11 +2766,157 @@ def test_mcp_fetch_run_readiness_route_get_and_post_are_preview_only(): assert post_data["api_uses_external_network"] is False +def test_mcp_fetch_run_receipt_preview_is_safe_without_payload(): + receipt = build_mcp_fetch_run_receipt_preview( + phase="phase_124_market_intel_mcp_fetch_run_receipt", + ) + + assert receipt["mode"] == "mcp_fetch_run_receipt_preview" + assert receipt["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" + assert receipt["run_receipt_payload_received"] is False + assert receipt["mcp_fetch_run_receipt_accepted"] is False + assert receipt["run_receipt_ready"] is False + assert receipt["ready_for_manual_fetch_result_parser_review"] is False + assert receipt["ready_for_api_database_write"] is False + assert receipt["ready_for_scheduler_attach"] is False + assert receipt["manual_fetch_gate_opened_by_api"] is False + assert receipt["network_request_allowed"] is False + assert receipt["fetch_executed"] is False + assert receipt["fetch_executed_by_api"] is False + assert receipt["cli_executed"] is False + assert receipt["payload_persisted"] is False + assert receipt["run_receipt_persisted"] is False + assert receipt["receipt_persisted"] is False + assert receipt["run_receipt_file_written"] is False + assert receipt["receipt_file_written"] is False + assert receipt["api_executes_cli"] is False + assert receipt["api_opens_database_connection"] is False + assert receipt["api_writes_database"] is False + assert receipt["api_uses_external_network"] is False + assert receipt["database_write_executed"] is False + assert receipt["external_network_executed"] is False + assert receipt["file_written"] is False + assert receipt["scheduler_attached"] is False + assert "run_readiness_payload_or_result_received" in receipt["blocked_reasons"] + assert "manual_fetch_receipt_received" in receipt["blocked_reasons"] + assert "run_readiness_package" in receipt["sample_run_receipt_package"] + assert "manual_fetch_receipt" in receipt["sample_run_receipt_package"] + + +def test_mcp_fetch_run_receipt_accepts_sample_receipt(): + sample = build_mcp_fetch_run_receipt_preview()["sample_run_receipt_package"] + receipt = build_mcp_fetch_run_receipt_preview( + run_readiness_package=sample["run_readiness_package"], + run_readiness_result=sample["run_readiness_result"], + manual_fetch_receipt=sample["manual_fetch_receipt"], + phase="phase_124_market_intel_mcp_fetch_run_receipt", + ) + + assert receipt["mode"] == "mcp_fetch_run_receipt_review" + assert receipt["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" + assert receipt["mcp_fetch_run_receipt_accepted"] is True + assert receipt["run_receipt_ready"] is True + assert receipt["operator_shell_fetch_receipt_received"] is True + assert receipt["ready_for_manual_fetch_result_parser_review"] is True + assert receipt["ready_for_api_database_write"] is False + assert receipt["blocked_reasons"] == [] + assert receipt["passed_gate_count"] == receipt["gate_count"] + assert receipt["run_readiness_accepted"] is True + assert receipt["expected_command_count"] >= 8 + assert receipt["receipt_source_count"] == receipt["expected_command_count"] + assert receipt["manual_fetch_receipt_summary"]["all_urls_public"] is True + assert receipt["manual_fetch_receipt_summary"]["all_receipt_paths_safe"] is True + assert receipt["manual_fetch_receipt_summary"]["blocked_side_effects"] == [] + assert receipt["manual_fetch_gate_opened_by_api"] is False + assert receipt["network_request_allowed"] is False + assert receipt["fetch_executed"] is False + assert receipt["fetch_executed_by_api"] is False + assert receipt["cli_executed"] is False + assert receipt["run_receipt_file_written"] is False + assert receipt["file_written"] is False + assert receipt["database_write_executed"] is False + assert receipt["api_uses_external_network"] is False + assert receipt["scheduler_attached"] is False + + +def test_mcp_fetch_run_receipt_blocks_unsafe_receipt_payload(): + sample = json.loads( + json.dumps(build_mcp_fetch_run_receipt_preview()["sample_run_receipt_package"]) + ) + manual_fetch_receipt = sample["manual_fetch_receipt"] + manual_fetch_receipt["approval_token"] = "do-not-submit" + manual_fetch_receipt["api_writes_database"] = True + manual_fetch_receipt["summary"]["request_count"] = 99 + manual_fetch_receipt["operator_confirmed_no_api_execution"] = False + first_source = manual_fetch_receipt["sources"][0] + first_source["source_url"] = "http://127.0.0.1/private" + first_source["receipt_path"] = "../unsafe.json" + + receipt = build_mcp_fetch_run_receipt_preview( + run_readiness_package=sample["run_readiness_package"], + run_readiness_result=sample["run_readiness_result"], + manual_fetch_receipt=manual_fetch_receipt, + ) + + assert receipt["mcp_fetch_run_receipt_accepted"] is False + assert receipt["run_readiness_accepted"] is True + assert "receipt_paths_match_expected" in receipt["blocked_reasons"] + assert "receipt_request_budget_within_limit" in receipt["blocked_reasons"] + assert "receipt_urls_public_http" in receipt["blocked_reasons"] + assert "receipt_operator_boundaries_confirmed" in receipt["blocked_reasons"] + assert "receipt_no_secret_or_token_key" in receipt["blocked_reasons"] + assert "receipt_side_effect_free" in receipt["blocked_reasons"] + assert receipt["network_request_allowed"] is False + assert receipt["api_writes_database"] is False + assert receipt["scheduler_attached"] is False + + +def test_mcp_fetch_run_receipt_route_get_and_post_are_preview_only(): + from routes.market_intel_routes import market_intel_bp + + app = Flask(__name__) + app.secret_key = "test-secret" + app.register_blueprint(market_intel_bp) + client = app.test_client() + with client.session_transaction() as session: + session["logged_in"] = True + + get_response = client.get("/api/market_intel/mcp_fetch_run_receipt") + get_data = get_response.get_json() + sample = get_data["sample_run_receipt_package"] + post_response = client.post( + "/api/market_intel/mcp_fetch_run_receipt", + json={"run_receipt": sample}, + ) + post_data = post_response.get_json() + + assert get_response.status_code == 200 + assert get_data["mode"] == "mcp_fetch_run_receipt_preview" + assert get_data["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" + assert get_data["api_executes_cli"] is False + assert get_data["api_writes_database"] is False + assert get_data["api_uses_external_network"] is False + assert post_response.status_code == 200 + assert post_data["mode"] == "mcp_fetch_run_receipt_review" + assert post_data["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" + assert post_data["mcp_fetch_run_receipt_accepted"] is True + assert post_data["ready_for_manual_fetch_result_parser_review"] is True + assert post_data["ready_for_api_database_write"] is False + assert post_data["manual_fetch_gate_opened_by_api"] is False + assert post_data["network_request_allowed"] is False + assert post_data["fetch_executed"] is False + assert post_data["fetch_executed_by_api"] is False + assert post_data["cli_executed"] is False + assert post_data["run_receipt_persisted"] is False + assert post_data["api_opens_database_connection"] is False + assert post_data["api_uses_external_network"] is False + + 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_123_market_intel_mcp_fetch_run_readiness" + assert plan["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert plan["ready_for_manual_sample_fetch"] is False assert plan["sample_fetch_executed"] is False assert plan["external_network_executed"] is False @@ -2809,7 +2964,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_123_market_intel_mcp_fetch_run_readiness" + assert data["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert data["sample_fetch_executed"] is False assert data["external_network_executed"] is False assert data["database_write_executed"] is False @@ -2820,7 +2975,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_123_market_intel_mcp_fetch_run_readiness" + assert acceptance["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert acceptance["contract_ready"] is True assert acceptance["sample_result_loaded"] is False assert acceptance["sample_result_accepted"] is False @@ -2862,7 +3017,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_123_market_intel_mcp_fetch_run_readiness" + assert data["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert data["sample_result_loaded"] is False assert data["candidate_import_allowed"] is False assert data["external_network_executed"] is False @@ -2874,7 +3029,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_123_market_intel_mcp_fetch_run_readiness" + assert review["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert review["contract_ready"] is True assert review["sample_result_loaded"] is False assert review["sample_result_reviewed"] is False @@ -2985,7 +3140,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_123_market_intel_mcp_fetch_run_readiness" + assert data["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert data["sample_result_loaded"] is False assert data["sample_result_reviewed"] is False assert data["candidate_import_allowed"] is False @@ -3024,7 +3179,7 @@ def test_manual_sample_review_evaluation_preview_accepts_payload_without_persist ) assert review["mode"] == "manual_sample_review_evaluation_preview" - assert review["phase"] == "phase_123_market_intel_mcp_fetch_run_readiness" + assert review["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert review["review_request_type"] == "operator_posted_json" assert review["payload_received"] is True assert review["payload_valid_json_object"] is True @@ -3086,7 +3241,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_123_market_intel_mcp_fetch_run_readiness" + assert data["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert data["payload_received"] is True assert data["payload_valid_json_object"] is True assert data["payload_persisted"] is False @@ -3166,7 +3321,7 @@ def test_manual_sample_candidate_handoff_preview_creates_candidates_without_pers ) assert handoff["mode"] == "manual_sample_candidate_handoff_preview" - assert handoff["phase"] == "phase_123_market_intel_mcp_fetch_run_readiness" + assert handoff["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert handoff["payload_received"] is True assert handoff["payload_valid_json_object"] is True assert handoff["payload_persisted"] is False @@ -3230,7 +3385,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_123_market_intel_mcp_fetch_run_readiness" + assert data["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert data["payload_received"] is True assert data["handoff_ready"] is True assert data["candidate_handoff_created"] is True @@ -3289,7 +3444,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_123_market_intel_mcp_fetch_run_readiness" + assert queue_draft["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert queue_draft["payload_received"] is True assert queue_draft["payload_valid_json_object"] is True assert queue_draft["payload_persisted"] is False @@ -3363,7 +3518,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_123_market_intel_mcp_fetch_run_readiness" + assert data["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert data["payload_received"] is True assert data["handoff_ready"] is True assert data["queue_draft_ready"] is True @@ -3426,7 +3581,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_123_market_intel_mcp_fetch_run_readiness" + assert approval["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert approval["payload_received"] is True assert approval["payload_valid_json_object"] is True assert approval["payload_persisted"] is False @@ -3504,7 +3659,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_123_market_intel_mcp_fetch_run_readiness" + assert data["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert data["payload_received"] is True assert data["approval_preview_created"] is True assert data["approval_request_created"] is False @@ -3567,7 +3722,7 @@ def test_manual_sample_candidate_queue_transaction_preview_blocks_execution(): ) assert transaction["mode"] == "manual_sample_candidate_queue_transaction_preview" - assert transaction["phase"] == "phase_123_market_intel_mcp_fetch_run_readiness" + assert transaction["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert transaction["payload_received"] is True assert transaction["payload_valid_json_object"] is True assert transaction["payload_persisted"] is False @@ -3647,7 +3802,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_123_market_intel_mcp_fetch_run_readiness" + assert data["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert data["payload_received"] is True assert data["transaction_preview_created"] is True assert data["transaction_ready"] is False @@ -9358,7 +9513,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_123_market_intel_mcp_fetch_run_readiness" + "phase_124_market_intel_mcp_fetch_run_receipt" ) assert data["telegram_dispatch_report_input_ready"] is False assert data["summary_persistence_telegram_dispatch_report_input_ready"] is False @@ -9433,7 +9588,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_123_market_intel_mcp_fetch_run_readiness" + "phase_124_market_intel_mcp_fetch_run_receipt" ) assert data["telegram_dispatch_archive_summary_ready"] is False assert ( @@ -9705,7 +9860,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_123_market_intel_mcp_fetch_run_readiness" + "phase_124_market_intel_mcp_fetch_run_receipt" ) assert data["telegram_dispatch_report_input_ready"] is False assert data["summary_persistence_telegram_dispatch_report_input_ready"] is False @@ -9993,7 +10148,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_123_market_intel_mcp_fetch_run_readiness" + "phase_124_market_intel_mcp_fetch_run_receipt" ) assert data["telegram_dispatch_report_run_package_ready"] is False assert ( @@ -10303,7 +10458,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_123_market_intel_mcp_fetch_run_readiness" + "phase_124_market_intel_mcp_fetch_run_receipt" ) assert data["telegram_dispatch_report_run_readiness_ready"] is False assert ( @@ -10606,7 +10761,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_123_market_intel_mcp_fetch_run_readiness" + "phase_124_market_intel_mcp_fetch_run_receipt" ) assert data["telegram_dispatch_report_run_receipt_passed"] is False assert ( @@ -10865,7 +11020,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_123_market_intel_mcp_fetch_run_readiness" + "phase_124_market_intel_mcp_fetch_run_receipt" ) assert data["telegram_dispatch_report_closeout_passed"] is False assert ( @@ -11138,7 +11293,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_123_market_intel_mcp_fetch_run_readiness" + "phase_124_market_intel_mcp_fetch_run_receipt" ) assert data["telegram_dispatch_report_archive_passed"] is False assert ( @@ -11386,7 +11541,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_123_market_intel_mcp_fetch_run_readiness" + "phase_124_market_intel_mcp_fetch_run_receipt" ) assert data["telegram_dispatch_report_archive_summary_passed"] is False assert ( @@ -11616,7 +11771,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_123_market_intel_mcp_fetch_run_readiness" + "phase_124_market_intel_mcp_fetch_run_receipt" ) assert data["telegram_dispatch_report_catalog_handoff_passed"] is False assert data["summary_persistence_telegram_dispatch_report_catalog_handoff_passed"] is False @@ -11853,7 +12008,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_123_market_intel_mcp_fetch_run_readiness" + "phase_124_market_intel_mcp_fetch_run_receipt" ) assert data["telegram_dispatch_report_catalog_index_passed"] is False assert data["summary_persistence_telegram_dispatch_report_catalog_index_passed"] is False @@ -12095,7 +12250,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_123_market_intel_mcp_fetch_run_readiness" + "phase_124_market_intel_mcp_fetch_run_receipt" ) assert data["telegram_dispatch_report_catalog_write_preflight_passed"] is False assert ( @@ -12371,7 +12526,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_123_market_intel_mcp_fetch_run_readiness" + "phase_124_market_intel_mcp_fetch_run_receipt" ) assert data["telegram_dispatch_report_catalog_record_write_passed"] is False assert ( @@ -12648,7 +12803,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_123_market_intel_mcp_fetch_run_readiness" + "phase_124_market_intel_mcp_fetch_run_receipt" ) assert data["telegram_dispatch_report_catalog_record_run_package_passed"] is False assert ( @@ -12927,7 +13082,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_123_market_intel_mcp_fetch_run_readiness" + "phase_124_market_intel_mcp_fetch_run_receipt" ) assert data["telegram_dispatch_report_catalog_record_run_readiness_passed"] is False assert ( @@ -13256,7 +13411,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_123_market_intel_mcp_fetch_run_readiness" + "phase_124_market_intel_mcp_fetch_run_receipt" ) assert data["telegram_dispatch_report_catalog_record_run_receipt_passed"] is False assert ( @@ -13518,7 +13673,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_123_market_intel_mcp_fetch_run_readiness" + "phase_124_market_intel_mcp_fetch_run_receipt" ) assert data["telegram_dispatch_report_catalog_record_commit_passed"] is False assert ( @@ -13782,7 +13937,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_123_market_intel_mcp_fetch_run_readiness" + "phase_124_market_intel_mcp_fetch_run_receipt" ) assert data["telegram_dispatch_report_catalog_record_closeout_passed"] is False assert ( @@ -14052,7 +14207,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_123_market_intel_mcp_fetch_run_readiness" + "phase_124_market_intel_mcp_fetch_run_receipt" ) assert data["telegram_dispatch_report_catalog_record_archive_passed"] is False assert ( @@ -14340,7 +14495,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_123_market_intel_mcp_fetch_run_readiness" + "phase_124_market_intel_mcp_fetch_run_receipt" ) assert data["telegram_dispatch_report_catalog_record_archive_summary_passed"] is False assert ( @@ -14627,7 +14782,7 @@ def test_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_ "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout_preview" ) assert data["phase"] == ( - "phase_123_market_intel_mcp_fetch_run_readiness" + "phase_124_market_intel_mcp_fetch_run_receipt" ) assert data["telegram_dispatch_report_catalog_record_final_closeout_passed"] is False assert ( @@ -14713,7 +14868,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_123_market_intel_mcp_fetch_run_readiness" + assert data["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert data["execute_requested"] is False assert data["read_only_query_executed"] is False assert data["database_connection_opened"] is False @@ -14770,7 +14925,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_123_market_intel_mcp_fetch_run_readiness" + assert data["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert data["execute_requested"] is True assert data["apply_real_write_requested"] is True assert data["approval_token_present"] is False @@ -14859,7 +15014,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_123_market_intel_mcp_fetch_run_readiness" + assert data["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert data["execute_requested"] is False assert data["read_only_query_executed"] is False assert data["database_connection_opened"] is False @@ -14913,7 +15068,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_123_market_intel_mcp_fetch_run_readiness" + assert data["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert data["operator_drill_ready"] is True assert data["api_executes_cli"] is False assert data["api_reads_approval_token"] is False @@ -14969,7 +15124,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_123_market_intel_mcp_fetch_run_readiness" + assert data["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert data["package_ready"] is True assert data["package_artifact_created"] is False assert data["api_writes_file"] is False @@ -15035,7 +15190,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_123_market_intel_mcp_fetch_run_readiness" + assert data["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" 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 @@ -15337,7 +15492,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_123_market_intel_mcp_fetch_run_readiness" + assert data["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert data["receipt_passed"] is True assert data["ready_for_api_database_write"] is False assert data["ready_for_scheduler_attach"] is False @@ -15385,7 +15540,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_123_market_intel_mcp_fetch_run_readiness" + assert data["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert data["closeout_passed"] is True assert data["ready_for_next_manual_phase"] is True assert data["ready_for_api_database_write"] is False @@ -15434,7 +15589,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_123_market_intel_mcp_fetch_run_readiness" + assert data["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert data["handoff_ready"] is True assert data["ready_for_manual_queue_review"] is True assert data["ready_for_api_database_write"] is False @@ -15492,7 +15647,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_123_market_intel_mcp_fetch_run_readiness" + assert data["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert data["execute_requested"] is False assert data["review_inventory_ready"] is False assert data["ready_for_human_decision_review"] is False @@ -15558,7 +15713,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_123_market_intel_mcp_fetch_run_readiness" + assert data["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" 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 @@ -15629,7 +15784,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_123_market_intel_mcp_fetch_run_readiness" + assert data["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" 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 @@ -15705,7 +15860,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_123_market_intel_mcp_fetch_run_readiness" + assert data["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert data["transaction_preview_created"] is False assert data["transaction_ready"] is False assert data["ready_for_manual_shell_update_window"] is False @@ -15787,7 +15942,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_123_market_intel_mcp_fetch_run_readiness" + assert data["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert data["execute_requested"] is True assert data["apply_real_write_requested"] is True assert data["approval_token_present"] is False @@ -15873,7 +16028,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_123_market_intel_mcp_fetch_run_readiness" + assert data["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert data["execute_requested"] is True assert data["apply_real_write_requested"] is True assert data["read_only_query_executed"] is False @@ -15956,7 +16111,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_123_market_intel_mcp_fetch_run_readiness" + assert data["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert data["execute_requested"] is False assert data["read_only_query_executed"] is False assert data["database_connection_opened"] is False @@ -16039,7 +16194,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_123_market_intel_mcp_fetch_run_readiness" + assert data["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" 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 @@ -16125,7 +16280,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_123_market_intel_mcp_fetch_run_readiness" + assert data["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert data["package_ready"] is False assert data["package_artifact_created"] is False assert data["ready_for_api_review_state_update"] is False @@ -16216,7 +16371,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_123_market_intel_mcp_fetch_run_readiness" + "phase_124_market_intel_mcp_fetch_run_receipt" ) assert data["ready_for_cli_operator_run"] is False assert data["ready_for_api_review_state_update"] is False @@ -16326,7 +16481,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_123_market_intel_mcp_fetch_run_readiness" + "phase_124_market_intel_mcp_fetch_run_receipt" ) assert data["receipt_passed"] is False assert data["ready_for_api_review_state_update"] is False @@ -16412,7 +16567,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_123_market_intel_mcp_fetch_run_readiness" + "phase_124_market_intel_mcp_fetch_run_receipt" ) assert data["closeout_passed"] is False assert data["ready_for_api_review_state_update"] is False @@ -16469,7 +16624,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_123_market_intel_mcp_fetch_run_readiness" + "phase_124_market_intel_mcp_fetch_run_receipt" ) assert data["post_closeout_inventory_ready"] is False assert data["ready_for_api_review_state_update"] is False @@ -16522,7 +16677,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_123_market_intel_mcp_fetch_run_readiness" + assert data["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" 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 @@ -16575,7 +16730,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_123_market_intel_mcp_fetch_run_readiness" + assert data["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert data["archive_summary_ready"] is False assert data["summary_input_ready"] is False assert data["ready_for_ai_summary_generation"] is False @@ -16636,7 +16791,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_123_market_intel_mcp_fetch_run_readiness" + assert data["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" 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 @@ -16705,7 +16860,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_123_market_intel_mcp_fetch_run_readiness" + assert data["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" 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 @@ -16777,7 +16932,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_123_market_intel_mcp_fetch_run_readiness" + assert data["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" 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 @@ -16850,7 +17005,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_123_market_intel_mcp_fetch_run_readiness" + "phase_124_market_intel_mcp_fetch_run_receipt" ) assert data["summary_persistence_preflight_ready"] is False assert data["ready_for_summary_transaction_preview"] is False @@ -16921,7 +17076,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_123_market_intel_mcp_fetch_run_readiness" + "phase_124_market_intel_mcp_fetch_run_receipt" ) assert data["summary_persistence_transaction_ready"] is False assert data["ready_for_summary_persistence_writer_gate"] is False @@ -16986,7 +17141,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_123_market_intel_mcp_fetch_run_readiness" + "phase_124_market_intel_mcp_fetch_run_receipt" ) assert data["summary_persistence_writer_preflight_ready"] is False assert data["ready_for_summary_persistence_run_package"] is False @@ -17057,7 +17212,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_123_market_intel_mcp_fetch_run_readiness" + "phase_124_market_intel_mcp_fetch_run_receipt" ) assert data["package_ready"] is False assert data["ready_for_summary_persistence_run_readiness"] is False @@ -17130,7 +17285,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_123_market_intel_mcp_fetch_run_readiness" + "phase_124_market_intel_mcp_fetch_run_receipt" ) assert data["run_readiness_ready"] is False assert data["summary_persistence_run_readiness_ready"] is False @@ -17207,7 +17362,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_123_market_intel_mcp_fetch_run_readiness" + "phase_124_market_intel_mcp_fetch_run_receipt" ) assert data["run_receipt_passed"] is False assert data["summary_persistence_run_receipt_passed"] is False @@ -17284,7 +17439,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_123_market_intel_mcp_fetch_run_readiness" + "phase_124_market_intel_mcp_fetch_run_receipt" ) assert data["closeout_passed"] is False assert data["summary_persistence_closeout_passed"] is False @@ -17361,7 +17516,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_123_market_intel_mcp_fetch_run_readiness" + "phase_124_market_intel_mcp_fetch_run_receipt" ) assert data["telegram_dispatch_gate_passed"] is False assert data["summary_persistence_telegram_dispatch_gate_passed"] is False @@ -17435,7 +17590,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_123_market_intel_mcp_fetch_run_readiness" + "phase_124_market_intel_mcp_fetch_run_receipt" ) assert data["telegram_dispatch_run_package_ready"] is False assert data["summary_persistence_telegram_dispatch_run_package_ready"] is False @@ -17514,7 +17669,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_123_market_intel_mcp_fetch_run_readiness" + "phase_124_market_intel_mcp_fetch_run_receipt" ) assert data["telegram_dispatch_run_readiness_ready"] is False assert ( @@ -17601,7 +17756,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_123_market_intel_mcp_fetch_run_readiness" + "phase_124_market_intel_mcp_fetch_run_receipt" ) assert data["telegram_dispatch_run_receipt_passed"] is False assert data["summary_persistence_telegram_dispatch_run_receipt_passed"] is False @@ -17682,7 +17837,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_123_market_intel_mcp_fetch_run_readiness" + "phase_124_market_intel_mcp_fetch_run_receipt" ) assert data["telegram_dispatch_closeout_passed"] is False assert data["summary_persistence_telegram_dispatch_closeout_passed"] is False @@ -17764,7 +17919,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_123_market_intel_mcp_fetch_run_readiness" + "phase_124_market_intel_mcp_fetch_run_receipt" ) assert data["telegram_dispatch_archive_ready"] is False assert data["summary_persistence_telegram_dispatch_archive_ready"] is False @@ -17847,7 +18002,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_123_market_intel_mcp_fetch_run_readiness" + assert data["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert data["receipt_passed"] is True assert data["ready_for_next_manual_review"] is True assert data["ready_for_api_database_write"] is False @@ -17872,7 +18027,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_123_market_intel_mcp_fetch_run_readiness" + assert plan["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert plan["ready_to_attach_scheduler"] is False assert plan["scheduler_attached"] is False assert plan["scheduler_registration_executed"] is False @@ -17910,7 +18065,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_123_market_intel_mcp_fetch_run_readiness" + assert data["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert data["scheduler_registration_executed"] is False assert data["crawler_job_started"] is False assert data["external_network_executed"] is False @@ -17921,7 +18076,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_123_market_intel_mcp_fetch_run_readiness" + assert plan["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert plan["ready_for_review_queue"] is False assert plan["review_queue_created"] is False assert plan["auto_match_executed"] is False @@ -17957,7 +18112,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_123_market_intel_mcp_fetch_run_readiness" + assert data["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert data["review_queue_created"] is False assert data["auto_confirm_executed"] is False assert data["external_network_executed"] is False @@ -17968,7 +18123,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_123_market_intel_mcp_fetch_run_readiness" + assert plan["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert plan["ready_for_opportunity_queue"] is False assert plan["opportunity_queue_created"] is False assert plan["threat_alert_dispatched"] is False @@ -18009,7 +18164,7 @@ def test_opportunity_plan_route_is_preview_only(): assert response.status_code == 200 assert data["mode"] == "opportunity_plan_preview" - assert data["phase"] == "phase_123_market_intel_mcp_fetch_run_readiness" + assert data["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert data["opportunity_queue_created"] is False assert data["threat_alert_dispatched"] is False assert data["ai_summary_generated"] is False @@ -18020,7 +18175,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_123_market_intel_mcp_fetch_run_readiness" + assert plan["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert plan["ready_for_scoring_job"] is False assert plan["scoring_job_created"] is False assert plan["score_calculation_executed"] is False @@ -18068,7 +18223,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_123_market_intel_mcp_fetch_run_readiness" + assert data["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert data["scoring_job_created"] is False assert data["score_calculation_executed"] is False assert data["sample_scores_generated"] is False @@ -18080,7 +18235,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_123_market_intel_mcp_fetch_run_readiness" + assert plan["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert plan["ready_for_evidence_bundle"] is False assert plan["evidence_bundle_created"] is False assert plan["evidence_query_executed"] is False @@ -18126,7 +18281,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_123_market_intel_mcp_fetch_run_readiness" + assert data["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert data["evidence_bundle_created"] is False assert data["evidence_query_executed"] is False assert data["sample_evidence_generated"] is False @@ -18139,7 +18294,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_123_market_intel_mcp_fetch_run_readiness" + assert plan["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert plan["ready_for_alert_candidates"] is False assert plan["alert_candidate_created"] is False assert plan["alert_queue_created"] is False @@ -18224,7 +18379,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_123_market_intel_mcp_fetch_run_readiness" + assert data["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert data["alert_candidate_created"] is False assert data["alert_queue_created"] is False assert data["review_queue_created"] is False @@ -18302,7 +18457,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_123_market_intel_mcp_fetch_run_readiness" + assert data["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert data["deployment_actions_executed"] is False assert data["docker_command_executed"] is False assert data["ssh_command_executed"] is False @@ -18317,7 +18472,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_123_market_intel_mcp_fetch_run_readiness" + assert readiness["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert readiness["execute_requested"] is False assert readiness["router_enabled"] is False assert readiness["external_mcp_complete"] is False @@ -18712,6 +18867,7 @@ def test_deployment_readiness_reports_app_only_release_gate(): assert readiness["checks"]["mcp_fetch_target_review_preview_safe"] is True assert readiness["checks"]["mcp_fetch_run_package_preview_safe"] is True assert readiness["checks"]["mcp_fetch_run_readiness_preview_safe"] is True + assert readiness["checks"]["mcp_fetch_run_receipt_preview_safe"] is True assert readiness["checks"]["scheduler_plan_preview_safe"] is True assert readiness["checks"]["manual_sample_plan_preview_safe"] is True assert readiness["checks"]["manual_sample_acceptance_preview_safe"] is True @@ -19090,6 +19246,7 @@ def test_deployment_readiness_reports_app_only_release_gate(): assert "/api/market_intel/mcp_fetch_target_review" in readiness["production_smoke_targets"] assert "/api/market_intel/mcp_fetch_run_package" in readiness["production_smoke_targets"] assert "/api/market_intel/mcp_fetch_run_readiness" in readiness["production_smoke_targets"] + assert "/api/market_intel/mcp_fetch_run_receipt" in readiness["production_smoke_targets"] assert "/api/market_intel/scheduler_plan" in readiness["production_smoke_targets"] assert "/api/market_intel/manual_sample_plan" in readiness["production_smoke_targets"] assert "/api/market_intel/manual_sample_acceptance" in readiness["production_smoke_targets"] @@ -19495,6 +19652,23 @@ def test_deployment_readiness_reports_app_only_release_gate(): assert readiness["mcp_fetch_run_readiness"]["api_uses_external_network"] is False assert readiness["mcp_fetch_run_readiness"]["file_written"] is False assert readiness["mcp_fetch_run_readiness"]["scheduler_attached"] is False + assert readiness["mcp_fetch_run_receipt"]["mode"] == "mcp_fetch_run_receipt_preview" + assert readiness["mcp_fetch_run_receipt"]["run_receipt_payload_received"] is False + assert readiness["mcp_fetch_run_receipt"]["payload_persisted"] is False + assert readiness["mcp_fetch_run_receipt"]["run_receipt_persisted"] is False + assert readiness["mcp_fetch_run_receipt"]["receipt_persisted"] is False + assert readiness["mcp_fetch_run_receipt"]["run_receipt_file_written"] is False + assert readiness["mcp_fetch_run_receipt"]["receipt_file_written"] is False + assert readiness["mcp_fetch_run_receipt"]["manual_fetch_gate_opened_by_api"] is False + assert readiness["mcp_fetch_run_receipt"]["network_request_allowed"] is False + assert readiness["mcp_fetch_run_receipt"]["fetch_executed"] is False + assert readiness["mcp_fetch_run_receipt"]["fetch_executed_by_api"] is False + assert readiness["mcp_fetch_run_receipt"]["cli_executed"] is False + assert readiness["mcp_fetch_run_receipt"]["api_executes_cli"] is False + assert readiness["mcp_fetch_run_receipt"]["api_writes_database"] is False + assert readiness["mcp_fetch_run_receipt"]["api_uses_external_network"] is False + assert readiness["mcp_fetch_run_receipt"]["file_written"] is False + assert readiness["mcp_fetch_run_receipt"]["scheduler_attached"] is False assert readiness["manual_sample_plan"]["mode"] == "manual_sample_fetch_plan_preview" assert readiness["manual_sample_plan"]["sample_fetch_executed"] is False assert readiness["manual_sample_plan"]["external_network_executed"] is False @@ -23114,7 +23288,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_123_market_intel_mcp_fetch_run_readiness" + assert drill["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert drill["execute_requested"] is False assert drill["schema_state"] == "planned_no_db_probe" assert drill["drill_ready_for_operator_review"] is True @@ -23229,7 +23403,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_123_market_intel_mcp_fetch_run_readiness" + assert data["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert data["execute_requested"] is False assert data["migration_executed"] is False assert data["rollback_executed"] is False @@ -23241,7 +23415,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_123_market_intel_mcp_fetch_run_readiness" + assert review["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert review["execute_requested"] is False assert review["catalog_state"] == "planned_no_probe" assert review["seed_state"] == "planned_no_probe" @@ -23356,7 +23530,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_123_market_intel_mcp_fetch_run_readiness" + assert data["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert data["execute_requested"] is False assert data["catalog_state"] == "planned_no_probe" assert data["migration_executed"] is False @@ -23369,7 +23543,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_123_market_intel_mcp_fetch_run_readiness" + assert smoke["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert smoke["execute_requested"] is False assert smoke["smoke_result"] == "planned_no_execution" assert smoke["live_smoke_passed"] is False @@ -23431,7 +23605,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_123_market_intel_mcp_fetch_run_readiness" + assert data["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert data["execute_requested"] is False assert data["smoke_result"] == "planned_no_execution" assert data["migration_executed"] is False @@ -23444,7 +23618,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_123_market_intel_mcp_fetch_run_readiness" + assert inventory["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert inventory["execute_requested"] is False assert inventory["read_only_query_executed"] is False assert inventory["database_connection_opened"] is False @@ -23588,7 +23762,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_123_market_intel_mcp_fetch_run_readiness" + assert data["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert data["execute_requested"] is False assert data["read_only_query_executed"] is False assert data["database_write_executed"] is False @@ -23824,7 +23998,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_123_market_intel_mcp_fetch_run_readiness" + assert data["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert data["execute_requested"] is False assert data["apply_real_write_requested"] is False assert data["writes_executed"] is False @@ -23853,7 +24027,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_123_market_intel_mcp_fetch_run_readiness" + assert data["phase"] == "phase_124_market_intel_mcp_fetch_run_receipt" assert data["execute_requested"] is False assert data["apply_real_write_requested"] is False assert data["approval_token_present"] is False