From a3fb476eed6a45f570ea30eb3ab35a86802c01cc Mon Sep 17 00:00:00 2001 From: OoO Date: Mon, 18 May 2026 15:48:59 +0800 Subject: [PATCH] feat(market-intel): add scheduler attach plan preview --- config.py | 2 +- ...s-platform-market-campaign-intelligence.md | 1 + routes/README.md | 2 +- routes/market_intel_routes.py | 6 + services/market_intel/scheduler_plan.py | 115 ++++++++++++++++ services/market_intel/service.py | 22 +++- templates/market_intel/disabled.html | 124 +++++++++++++++++- tests/test_market_intel_skeleton.py | 74 ++++++++++- 8 files changed, 335 insertions(+), 11 deletions(-) create mode 100644 services/market_intel/scheduler_plan.py diff --git a/config.py b/config.py index 7d48bf5..c7562df 100644 --- a/config.py +++ b/config.py @@ -320,7 +320,7 @@ YOUTUBE_API_KEY = os.getenv('YOUTUBE_API_KEY', '') # ========================================== # 系統版本與路徑 # ========================================== -SYSTEM_VERSION = "V10.190" +SYSTEM_VERSION = "V10.191" 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 f16205a..04a81f3 100644 --- a/docs/adr/ADR-035-cross-platform-market-campaign-intelligence.md +++ b/docs/adr/ADR-035-cross-platform-market-campaign-intelligence.md @@ -157,6 +157,7 @@ EwoooC 目前已有 MOMO EDM / 節慶活動資料、`promo_products`、PChome - 2026-05-18 追加 external MCP deploy preflight preview:`services.market_intel.mcp_deploy_preflight` 與 `/api/market_intel/mcp_deploy_preflight` 只讀檢查 `docker-compose.mcp.yml`、必要 env、localhost-only ports、read-only volume、Firecrawl resource guard 與 fallback plan。`docker-compose.mcp.yml` 需以 read-only bind mount 進 app 容器供 preflight 審核。此 preflight 不執行 docker/SSH、不建立 `mcp_readonly` role、不啟用 `MCP_ROUTER_ENABLED`、不寫 DB、不掛 scheduler;外部 MCP stack 須等 env 與 operator smoke 全過後另行批准。 - 2026-05-18 追加 MCP activation runbook preview:`services.market_intel.mcp_activation_runbook` 與 `/api/market_intel/mcp_activation_runbook` 只輸出人工啟用順序與 gate:補必要 env、人工建立/驗證 `mcp_readonly`、啟動外部 MCP stack、四個 health 全過、最後才允許 `MCP_ROUTER_ENABLED=true`。此 runbook 不執行 docker/SSH、不寫 env、不建立 DB role、不跑 health、不啟用 router、不寫 DB、不掛 scheduler。 - 2026-05-18 追加 MCP fetch gate preview:`services.market_intel.mcp_fetch_gate` 與 `/api/market_intel/mcp_fetch_gate` 將人工 discovery fetch 改成必須先通過市場情報 feature flags、MCP readiness、router、外部 MCP health 與 tool contract gate;`run_manual_discovery(fetch=true)` 即使 flags 開啟也會先被此 gate 阻擋,直到 MCP 條件全過。此 gate 不抓電商頁、不寫 DB、不掛 scheduler、不執行 deployment,UI 預設只呼叫 `fetch=false&execute=false`。 +- 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 與人工批准全過。 ### Phase 4:Coupang / Shopee Adapter diff --git a/routes/README.md b/routes/README.md index 7d34943..8707520 100644 --- a/routes/README.md +++ b/routes/README.md @@ -19,7 +19,7 @@ | `edm_routes.py` | EDM 與節慶儀表板 | `/edm`, `/festival` | | `monthly_routes.py` | 月結分析 | `/monthly_summary_analysis`, `/api/monthly_summary_data` | | `daily_sales_routes.py` | 當日業績 | `/daily_sales`, `/daily_sales/export*` | -| `market_intel_routes.py` | 市場情報 Phase 32 MCP fetch gate preview | `/market_intel`, `/market_intel/*`, `/api/market_intel/status`, `/api/market_intel/schema`, `/api/market_intel/schema_smoke`, `/api/market_intel/schema_db_probe`, `/api/market_intel/platform_seed_db_diff`, `/api/market_intel/legacy_source_bridge`, `/api/market_intel/mcp_readiness`, `/api/market_intel/mcp_tool_contract`, `/api/market_intel/mcp_deploy_preflight`, `/api/market_intel/mcp_activation_runbook`, `/api/market_intel/mcp_fetch_gate`, `/api/market_intel/adapters`, `/api/market_intel/dry_run_plan`, `/api/market_intel/discovery_plan`, `/api/market_intel/manual_discovery`, `/api/market_intel/candidate_preview`, `/api/market_intel/platform_seed_plan`, `/api/market_intel/platform_seed_write_guard`, `/api/market_intel/platform_seed_writer_plan`, `/api/market_intel/migration_blueprint`, `/api/market_intel/seed_writer_cli_status`, `/api/market_intel/write_approval_runbook`, `/api/market_intel/deployment_readiness` | +| `market_intel_routes.py` | 市場情報 Phase 33 scheduler attach plan preview | `/market_intel`, `/market_intel/*`, `/api/market_intel/status`, `/api/market_intel/schema`, `/api/market_intel/schema_smoke`, `/api/market_intel/schema_db_probe`, `/api/market_intel/platform_seed_db_diff`, `/api/market_intel/legacy_source_bridge`, `/api/market_intel/mcp_readiness`, `/api/market_intel/mcp_tool_contract`, `/api/market_intel/mcp_deploy_preflight`, `/api/market_intel/mcp_activation_runbook`, `/api/market_intel/mcp_fetch_gate`, `/api/market_intel/scheduler_plan`, `/api/market_intel/adapters`, `/api/market_intel/dry_run_plan`, `/api/market_intel/discovery_plan`, `/api/market_intel/manual_discovery`, `/api/market_intel/candidate_preview`, `/api/market_intel/platform_seed_plan`, `/api/market_intel/platform_seed_write_guard`, `/api/market_intel/platform_seed_writer_plan`, `/api/market_intel/migration_blueprint`, `/api/market_intel/seed_writer_cli_status`, `/api/market_intel/write_approval_runbook`, `/api/market_intel/deployment_readiness` | | `api_routes.py` | 通用任務與查詢 API | `/api/run_task`, `/api/history/*` | | `export_routes.py` | 匯出功能 | `/api/export/*` | | `import_routes.py` | 匯入功能 | `/api/import_excel`, `/api/import/monthly_summary` | diff --git a/routes/market_intel_routes.py b/routes/market_intel_routes.py index 2126fe7..122fef0 100644 --- a/routes/market_intel_routes.py +++ b/routes/market_intel_routes.py @@ -151,6 +151,12 @@ def market_intel_mcp_fetch_gate(): ) +@market_intel_bp.route("/api/market_intel/scheduler_plan") +@login_required +def market_intel_scheduler_plan(): + return jsonify(_service().build_scheduler_plan()) + + @market_intel_bp.route("/api/market_intel/adapters") @login_required def market_intel_adapters(): diff --git a/services/market_intel/scheduler_plan.py b/services/market_intel/scheduler_plan.py new file mode 100644 index 0000000..b167634 --- /dev/null +++ b/services/market_intel/scheduler_plan.py @@ -0,0 +1,115 @@ +"""市場情報排程掛載 preview。 + +只描述未來 scheduler job 的節奏、gate 與備援;不註冊 job、不啟動 crawler、 +不寫 DB、不連外。 +""" + + +SCHEDULER_JOBS = ( + { + "key": "campaign_discovery_daily", + "label": "跨平台活動入口探索", + "cadence": "daily 09:20 Asia/Taipei", + "platforms": ["momo", "pchome", "coupang", "shopee"], + "entrypoint": "services.market_intel.jobs.discover_campaigns", + "max_runtime_minutes": 20, + "requires_external_network": True, + "requires_database_write": False, + "write_target": "market_crawler_runs preview only", + }, + { + "key": "campaign_product_probe", + "label": "已核准活動頁商品探測", + "cadence": "manual-first, then every 4 hours for active campaigns", + "platforms": ["momo", "pchome", "coupang", "shopee"], + "entrypoint": "services.market_intel.jobs.probe_campaign_products", + "max_runtime_minutes": 30, + "requires_external_network": True, + "requires_database_write": True, + "write_target": "market_campaign_products / market_product_price_history", + }, + { + "key": "product_match_review_seed", + "label": "商品比對審核候選生成", + "cadence": "daily 11:10 Asia/Taipei after discovery", + "platforms": ["momo", "pchome", "coupang", "shopee"], + "entrypoint": "services.market_intel.jobs.seed_product_matches", + "max_runtime_minutes": 15, + "requires_external_network": False, + "requires_database_write": True, + "write_target": "market_product_matches", + }, +) + + +def build_scheduler_attach_plan( + *, + runtime_status, + mcp_fetch_gate, + schema_smoke, +): + """建立排程掛載計畫;所有輸出都是 preview,不會建立 scheduler job。""" + schema_passed = bool( + schema_smoke.get("passed") + or (schema_smoke.get("schema_smoke") or {}).get("passed") + ) + gate_checks = { + "market_intel_enabled": bool(runtime_status.enabled), + "market_intel_crawler_enabled": bool(runtime_status.crawler_enabled), + "market_intel_write_enabled": bool(runtime_status.write_enabled), + "database_write_allowed": bool(runtime_status.database_write_allowed), + "schema_smoke_passed": schema_passed, + "mcp_fetch_gate_open": bool(mcp_fetch_gate.get("manual_fetch_gate_open")), + "scheduler_currently_detached": not bool(runtime_status.scheduler_attached), + "manual_operator_approval": False, + } + blocked_reasons = [ + key for key, passed in gate_checks.items() + if not passed + ] + + return { + "mode": "scheduler_attach_plan_preview", + "ready_to_attach_scheduler": False, + "scheduler_attached": False, + "scheduler_registration_executed": False, + "crawler_job_started": False, + "external_network_executed": False, + "database_session_created": False, + "database_write_executed": False, + "database_commit_executed": False, + "writes_executed": False, + "would_write_database": False, + "job_count": len(SCHEDULER_JOBS), + "jobs": list(SCHEDULER_JOBS), + "gate_checks": gate_checks, + "blocked_reasons": blocked_reasons, + "attach_sequence": [ + "先完成 market_* migration 與 platform seed 寫入驗證", + "MCP deploy preflight、activation runbook、fetch gate 必須全過", + "先以 manual discovery fetch 產生小樣本,確認 parser diagnostics 正常", + "只掛 campaign_discovery_daily,觀察 24 小時後才評估 product probe", + "任何異常立即關閉 MARKET_INTEL_CRAWLER_ENABLED,不影響既有 MOMO 排程", + ], + "fallback_plan": [ + { + "key": "feature_flag_pause", + "label": "關閉 MARKET_INTEL_CRAWLER_ENABLED 即可阻止新排程執行", + }, + { + "key": "detach_market_jobs_only", + "label": "只移除 market_intel job,不動既有 scheduler 與 momo-db", + }, + { + "key": "manual_fetch_only_mode", + "label": "回到人工 fetch gate 模式,保留 UI/API preview", + }, + ], + "safe_boundaries": [ + "do_not_attach_scheduler_from_api", + "do_not_run_external_crawler_from_preview", + "do_not_write_market_tables_before_migration", + "do_not_touch_existing_momo_scheduler_jobs", + "do_not_touch_momo_db_lifecycle", + ], + } diff --git a/services/market_intel/service.py b/services/market_intel/service.py index 6f9d10f..e8a8f1d 100644 --- a/services/market_intel/service.py +++ b/services/market_intel/service.py @@ -28,6 +28,7 @@ from services.market_intel.mcp_readiness import build_mcp_readiness_plan from services.market_intel.migration_blueprint import build_migration_blueprint from services.market_intel.platform_seed import build_platform_seed_rows from services.market_intel.platform_seed_db_diff import build_platform_seed_db_diff_plan +from services.market_intel.scheduler_plan import build_scheduler_attach_plan from services.market_intel.platform_seed_writer import ( build_platform_seed_writer_plan, build_schema_smoke, @@ -68,7 +69,7 @@ class MarketIntelRuntimeStatus: class MarketIntelService: """市場情報入口服務,先集中 feature gate 與安全狀態。""" - phase = "phase_32_mcp_fetch_gate_preview" + phase = "phase_33_scheduler_plan_preview" def get_runtime_status(self) -> MarketIntelRuntimeStatus: return MarketIntelRuntimeStatus( @@ -366,6 +367,16 @@ class MarketIntelService: gate["phase"] = self.phase return gate + def build_scheduler_plan(self): + """回報市場情報排程掛載計畫;不註冊 job、不啟動 crawler。""" + plan = build_scheduler_attach_plan( + runtime_status=self.get_runtime_status(), + mcp_fetch_gate=self.build_mcp_fetch_gate(), + schema_smoke=build_schema_smoke(MARKET_INTEL_TABLES), + ) + plan["phase"] = self.phase + return plan + def build_platform_seed_writer_plan(self, platform_code="all"): """建立 platform seed writer dry-run plan,不建立 DB session。""" seed_plan = self.build_platform_seed_plan(platform_code=platform_code) @@ -445,6 +456,7 @@ class MarketIntelService: mcp_deploy_preflight = self.build_mcp_deploy_preflight() mcp_activation_runbook = self.build_mcp_activation_runbook() mcp_fetch_gate = self.build_mcp_fetch_gate() + scheduler_plan = self.build_scheduler_plan() checks = { "schema_smoke_passed": bool(schema_smoke["passed"]), "feature_flags_default_safe": bool( @@ -489,6 +501,12 @@ class MarketIntelService: and not mcp_fetch_gate["network_request_allowed"] and not mcp_fetch_gate["external_network_executed"] ), + "scheduler_plan_preview_safe": bool( + scheduler_plan["mode"] == "scheduler_attach_plan_preview" + and not scheduler_plan["scheduler_registration_executed"] + and not scheduler_plan["crawler_job_started"] + and not scheduler_plan["database_write_executed"] + ), } ready_for_production_deploy = all(checks.values()) blocked_reasons = [ @@ -617,6 +635,7 @@ class MarketIntelService: "/api/market_intel/mcp_deploy_preflight", "/api/market_intel/mcp_activation_runbook", "/api/market_intel/mcp_fetch_gate", + "/api/market_intel/scheduler_plan", ], "status": status.to_dict(), "schema_smoke": schema_smoke, @@ -636,4 +655,5 @@ class MarketIntelService: "mcp_deploy_preflight": mcp_deploy_preflight, "mcp_activation_runbook": mcp_activation_runbook, "mcp_fetch_gate": mcp_fetch_gate, + "scheduler_plan": scheduler_plan, } diff --git a/templates/market_intel/disabled.html b/templates/market_intel/disabled.html index 97ec76e..bdc45f4 100644 --- a/templates/market_intel/disabled.html +++ b/templates/market_intel/disabled.html @@ -486,6 +486,24 @@ +
+
+
+

SCHEDULER / ATTACH PLAN

+

排程掛載計畫

+
+ +
+
+ loading +
+
+
讀取排程掛載計畫中...
+
+
+
@@ -555,10 +573,11 @@ const mcpPreflightRoot = document.querySelector('[data-market-intel-mcp-preflight]'); const mcpActivationRoot = document.querySelector('[data-market-intel-mcp-activation]'); const mcpFetchGateRoot = document.querySelector('[data-market-intel-mcp-fetch-gate]'); + const schedulerRoot = document.querySelector('[data-market-intel-scheduler]'); const migrationRoot = document.querySelector('[data-market-intel-migration]'); 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 && !migrationRoot && !approvalRoot && !deployRoot) return; + if (!root && !writerRoot && !cliRoot && !dbProbeRoot && !seedDiffRoot && !legacyBridgeRoot && !mcpReadinessRoot && !mcpPreflightRoot && !mcpActivationRoot && !mcpFetchGateRoot && !schedulerRoot && !migrationRoot && !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; @@ -600,6 +619,10 @@ const mcpFetchGateBody = mcpFetchGateRoot ? mcpFetchGateRoot.querySelector('[data-market-intel-mcp-fetch-gate-body]') : null; const mcpFetchGateRefresh = mcpFetchGateRoot ? mcpFetchGateRoot.querySelector('[data-market-intel-mcp-fetch-gate-refresh]') : null; const mcpFetchGateEndpoint = "{{ url_for('market_intel.market_intel_mcp_fetch_gate') }}?fetch=false&execute=false"; + const schedulerMeta = schedulerRoot ? schedulerRoot.querySelector('[data-market-intel-scheduler-meta]') : null; + const schedulerBody = schedulerRoot ? schedulerRoot.querySelector('[data-market-intel-scheduler-body]') : null; + const schedulerRefresh = schedulerRoot ? schedulerRoot.querySelector('[data-market-intel-scheduler-refresh]') : null; + const schedulerEndpoint = "{{ url_for('market_intel.market_intel_scheduler_plan') }}"; const migrationMeta = migrationRoot ? migrationRoot.querySelector('[data-market-intel-migration-meta]') : null; const migrationBody = migrationRoot ? migrationRoot.querySelector('[data-market-intel-migration-body]') : null; const migrationRefresh = migrationRoot ? migrationRoot.querySelector('[data-market-intel-migration-refresh]') : null; @@ -1305,6 +1328,101 @@ } }; + const renderSchedulerMeta = data => { + schedulerMeta.innerHTML = [ + `mode=${data.mode || 'unknown'}`, + `ready=${data.ready_to_attach_scheduler ? 'yes' : 'no'}`, + `jobs=${data.job_count || 0}`, + `scheduler=${data.scheduler_attached ? 'on' : 'off'}`, + `crawler=${data.crawler_job_started ? 'started' : 'blocked'}` + ].map(item => `${escapeHtml(item)}`).join(''); + }; + + const renderSchedulerBody = data => { + const blockers = (data.blocked_reasons || []).join(' / '); + const checks = Object.entries(data.gate_checks || {}); + const jobs = data.jobs || []; + const sequence = data.attach_sequence || []; + const fallback = data.fallback_plan || []; + const renderCheck = ([name, passed]) => ` +
+
+ ${escapeHtml(name)} +
+ ${passed ? 'PASS' : 'BLOCK'} +
+ `; + const renderJob = item => ` +
+ ${escapeHtml(item.key)} + ${escapeHtml(item.label)} / ${escapeHtml(item.cadence)} + ${escapeHtml(item.entrypoint)} / max=${escapeHtml(item.max_runtime_minutes)}m + network=${item.requires_external_network ? 'yes' : 'no'} / write=${item.requires_database_write ? 'yes' : 'no'} +
+ `; + const renderPlanItem = (item, status) => ` +
+
+ ${escapeHtml(item.key || status)} + ${escapeHtml(item.label || item)} +
+ ${escapeHtml(status).toUpperCase()} +
+ `; + schedulerBody.innerHTML = ` +
這是排程掛載計畫,不會從 API 註冊 job、不啟動 crawler、不連外、不寫 DB。${blockers ? `阻擋:${escapeHtml(blockers)}` : ''}
+
+
+

SCHEDULER GATES

+
${ + checks.length + ? checks.map(renderCheck).join('') + : '
尚未提供排程 gate。
' + }
+
+
+

PLANNED JOBS

+
${ + jobs.length + ? jobs.map(renderJob).join('') + : '
尚未提供排程 job。
' + }
+
+
+

ATTACH SEQUENCE

+
${ + sequence.length + ? sequence.map(item => renderPlanItem(item, 'required')).join('') + : '
尚未提供掛載順序。
' + }
+
+
+

FALLBACK

+
${ + fallback.length + ? fallback.map(item => renderPlanItem(item, 'ready')).join('') + : '
尚未提供備援方案。
' + }
+
+
+ `; + }; + + const loadScheduler = async () => { + if (!schedulerMeta || !schedulerBody) return; + schedulerBody.innerHTML = '
讀取排程掛載計畫中...
'; + try { + const response = await fetch(schedulerEndpoint, { credentials: 'same-origin' }); + if (!response.ok) throw new Error(`HTTP ${response.status}`); + const data = await response.json(); + renderSchedulerMeta(data); + renderSchedulerBody(data); + } catch (error) { + schedulerMeta.innerHTML = 'error'; + schedulerBody.innerHTML = `
排程掛載計畫讀取失敗:${escapeHtml(error.message)}
`; + } + }; + const renderMigrationMeta = data => { const seedWriter = data.command_plan && data.command_plan.seed_writer_command ? data.command_plan.seed_writer_command @@ -1569,6 +1687,9 @@ if (mcpFetchGateRefresh) { mcpFetchGateRefresh.addEventListener('click', loadMcpFetchGate); } + if (schedulerRefresh) { + schedulerRefresh.addEventListener('click', loadScheduler); + } if (migrationRefresh) { migrationRefresh.addEventListener('click', loadMigration); } @@ -1588,6 +1709,7 @@ loadMcpPreflight(); loadMcpActivation(); loadMcpFetchGate(); + loadScheduler(); loadMigration(); loadApproval(); loadDeploy(); diff --git a/tests/test_market_intel_skeleton.py b/tests/test_market_intel_skeleton.py index 6175774..abf8cb9 100644 --- a/tests/test_market_intel_skeleton.py +++ b/tests/test_market_intel_skeleton.py @@ -443,6 +443,11 @@ def test_market_intel_preview_template_uses_safe_fetch_false_endpoint(): assert "data-market-intel-mcp-fetch-gate-checks" in template assert "data-market-intel-mcp-fetch-gate-sequence" in template assert "data-market-intel-mcp-fetch-gate-readiness" in template + assert "data-market-intel-scheduler" in template + assert "data-market-intel-scheduler-checks" in template + assert "data-market-intel-scheduler-jobs" in template + assert "data-market-intel-scheduler-sequence" in template + assert "data-market-intel-scheduler-fallback" in template assert "data-market-intel-migration" in template assert "data-market-intel-migration-tables" in template assert "data-market-intel-approval" in template @@ -460,6 +465,7 @@ def test_market_intel_preview_template_uses_safe_fetch_false_endpoint(): assert "market_intel.market_intel_mcp_deploy_preflight" in template assert "market_intel.market_intel_mcp_activation_runbook" in template assert "market_intel.market_intel_mcp_fetch_gate" in template + assert "market_intel.market_intel_scheduler_plan" in template assert "market_intel.market_intel_migration_blueprint" in template assert "market_intel.market_intel_write_approval_runbook" in template assert "market_intel.market_intel_deployment_readiness" in template @@ -485,7 +491,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_32_mcp_fetch_gate_preview" + assert bridge["phase"] == "phase_33_scheduler_plan_preview" assert bridge["execute_requested"] is False assert bridge["read_only_query_executed"] is False assert bridge["database_connection_opened"] is False @@ -643,7 +649,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_32_mcp_fetch_gate_preview" + assert contract["phase"] == "phase_33_scheduler_plan_preview" assert contract["caller"] == "market_intel" assert contract["contract_ready"] is True assert contract["blocked_reasons"] == [] @@ -776,7 +782,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_32_mcp_fetch_gate_preview" + assert data["phase"] == "phase_33_scheduler_plan_preview" assert data["deployment_actions_executed"] is False assert data["docker_command_executed"] is False assert data["ssh_command_executed"] is False @@ -789,7 +795,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_32_mcp_fetch_gate_preview" + assert gate["phase"] == "phase_33_scheduler_plan_preview" assert gate["fetch_requested"] is True assert gate["manual_fetch_gate_open"] is False assert gate["network_request_allowed"] is False @@ -859,7 +865,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_32_mcp_fetch_gate_preview" + assert data["phase"] == "phase_33_scheduler_plan_preview" assert data["fetch_requested"] is False assert data["network_request_allowed"] is False assert data["external_network_executed"] is False @@ -867,6 +873,55 @@ def test_mcp_fetch_gate_route_is_preview_only(): assert data["scheduler_attached"] is False +def test_scheduler_plan_preview_blocks_job_attachment(): + plan = MarketIntelService().build_scheduler_plan() + + assert plan["mode"] == "scheduler_attach_plan_preview" + assert plan["phase"] == "phase_33_scheduler_plan_preview" + assert plan["ready_to_attach_scheduler"] is False + assert plan["scheduler_attached"] is False + assert plan["scheduler_registration_executed"] is False + assert plan["crawler_job_started"] is False + assert plan["external_network_executed"] is False + assert plan["database_session_created"] is False + assert plan["database_write_executed"] is False + assert plan["database_commit_executed"] is False + assert plan["writes_executed"] is False + assert plan["would_write_database"] is False + assert plan["job_count"] == 3 + assert {item["key"] for item in plan["jobs"]} == { + "campaign_discovery_daily", + "campaign_product_probe", + "product_match_review_seed", + } + assert "market_intel_enabled" in plan["blocked_reasons"] + assert "mcp_fetch_gate_open" in plan["blocked_reasons"] + assert "manual_operator_approval" in plan["blocked_reasons"] + assert "do_not_attach_scheduler_from_api" in plan["safe_boundaries"] + + +def test_scheduler_plan_route_is_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 + + response = client.get("/api/market_intel/scheduler_plan") + data = response.get_json() + + assert response.status_code == 200 + assert data["mode"] == "scheduler_attach_plan_preview" + assert data["phase"] == "phase_33_scheduler_plan_preview" + assert data["scheduler_registration_executed"] is False + assert data["crawler_job_started"] is False + assert data["external_network_executed"] is False + assert data["database_write_executed"] is False + + def test_mcp_deploy_preflight_blocks_without_required_env(): preflight = build_mcp_deploy_preflight_plan(env={}) @@ -930,7 +985,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_32_mcp_fetch_gate_preview" + assert data["phase"] == "phase_33_scheduler_plan_preview" assert data["deployment_actions_executed"] is False assert data["docker_command_executed"] is False assert data["ssh_command_executed"] is False @@ -945,7 +1000,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_32_mcp_fetch_gate_preview" + assert readiness["phase"] == "phase_33_scheduler_plan_preview" assert readiness["execute_requested"] is False assert readiness["router_enabled"] is False assert readiness["external_mcp_complete"] is False @@ -1332,6 +1387,7 @@ def test_deployment_readiness_reports_app_only_release_gate(): assert readiness["checks"]["mcp_deploy_preflight_preview_safe"] is True assert readiness["checks"]["mcp_activation_runbook_preview_safe"] is True assert readiness["checks"]["mcp_fetch_gate_preview_safe"] is True + assert readiness["checks"]["scheduler_plan_preview_safe"] is True assert readiness["checks"]["writer_plan_dry_run_only"] is True assert readiness["writer_plan_summary"]["writes_executed"] is False assert "readiness_checks_not_all_passed" not in readiness["blocked_reasons"] @@ -1352,6 +1408,7 @@ def test_deployment_readiness_reports_app_only_release_gate(): assert "/api/market_intel/mcp_deploy_preflight" in readiness["production_smoke_targets"] assert "/api/market_intel/mcp_activation_runbook" in readiness["production_smoke_targets"] assert "/api/market_intel/mcp_fetch_gate" in readiness["production_smoke_targets"] + assert "/api/market_intel/scheduler_plan" in readiness["production_smoke_targets"] assert readiness["write_approval_runbook"]["ready_for_real_write"] is False assert readiness["write_approval_runbook"]["writes_executed"] is False assert readiness["migration_blueprint"]["migration_executed"] is False @@ -1370,6 +1427,9 @@ def test_deployment_readiness_reports_app_only_release_gate(): assert readiness["mcp_activation_runbook"]["docker_command_executed"] is False assert readiness["mcp_fetch_gate"]["network_request_allowed"] is False assert readiness["mcp_fetch_gate"]["external_network_executed"] is False + assert readiness["scheduler_plan"]["scheduler_registration_executed"] is False + assert readiness["scheduler_plan"]["crawler_job_started"] is False + assert readiness["scheduler_plan"]["database_write_executed"] is False def test_write_approval_runbook_is_read_only_and_blocks_real_write():