"""Operator policy is hot-loaded; browser requests cannot override scheduling.""" from datetime import datetime import json from types import SimpleNamespace import pytest def write_policy(path, enabled, max_shots=120): path.write_text(json.dumps({"schema_version": 1, "batch_window_enabled": enabled, "workflow_max_shots": max_shots}), encoding="utf-8") def test_daytime_policy_switch_is_hot_loaded_without_changing_job_mode(tmp_path): from app.resource_policy import admits, execution_policy path = tmp_path / "runtime-policy.json" settings = SimpleNamespace(runtime_policy_file=path) job = SimpleNamespace(execution_mode="batch", desired_state="run", cancel_requested=False, hold_reasons=[]) noon = datetime.fromisoformat("2026-11-11T12:00:00+08:00") write_policy(path, False, 240) assert admits(job, now=noon, settings=settings) info = execution_policy(noon, settings=settings) assert info["batch_window_enabled"] is False and info["batch_open"] assert info["next_batch_at"] is None write_policy(path, True) assert not admits(job, now=noon, settings=settings) assert execution_policy(noon, settings=settings)["batch_window_enabled"] is True assert job.execution_mode == "batch" write_policy(path, False) job.hold_reasons = ["batch_pause"] assert not admits(job, now=noon, settings=settings) @pytest.mark.parametrize("value", [ {"schema_version": 1, "batch_window_enabled": "false"}, {"schema_version": True, "batch_window_enabled": False}, {"schema_version": 1, "batch_window_enabled": False, "workflow_max_shots": True}, {"schema_version": 1, "batch_window_enabled": False, "workflow_max_shots": 1001}, {"schema_version": 1, "batch_window_enabled": False, "unexpected": True}, [], None, ]) def test_bad_runtime_policy_only_blocks_batch(tmp_path, value): from app.runtime_policy import read_runtime_policy from app.resource_policy import admits, execution_policy path = tmp_path / "runtime-policy.json" settings = SimpleNamespace(runtime_policy_file=path) path.write_text(json.dumps(value), encoding="utf-8") result = read_runtime_policy(settings) assert result["batch_window_enabled"] is True assert result["configuration_error"] == "runtime_policy_invalid" job = SimpleNamespace(execution_mode="batch", desired_state="run", cancel_requested=False, hold_reasons=[]) night = datetime.fromisoformat("2026-11-11T22:00:00+08:00") assert not admits(job, now=night, settings=settings) assert not execution_policy(night, settings=settings)["batch_open"] job.execution_mode = "interactive" assert admits(job, now=night, settings=settings) def test_missing_file_is_production_default_and_environment_selects_file(tmp_path, monkeypatch): from app.config import Settings from app.runtime_policy import read_runtime_policy path = tmp_path / "missing.json" monkeypatch.setenv("CMVR_MODE", "test") monkeypatch.setenv("DATABASE_URL", "sqlite+aiosqlite:///:memory:") monkeypatch.setenv("WORKSPACE_ROOT", str(tmp_path)) monkeypatch.setenv("RUNTIME_POLICY_FILE", str(path)) settings = Settings.from_env() assert settings.runtime_policy_file == path assert read_runtime_policy(settings) == {"batch_window_enabled": True, "workflow_max_shots": 120, "configuration_error": None} def test_empty_environment_path_keeps_shared_default_and_unreadable_file_fails_closed(tmp_path, monkeypatch): from pathlib import Path from app.config import Settings from app.runtime_policy import read_runtime_policy monkeypatch.setenv("CMVR_MODE", "test") monkeypatch.setenv("DATABASE_URL", "sqlite+aiosqlite:///:memory:") monkeypatch.setenv("WORKSPACE_ROOT", str(tmp_path)) monkeypatch.setenv("RUNTIME_POLICY_FILE", "") assert Settings.from_env().runtime_policy_file == Path("/workspace/projects/llm-services/config/runtime-policy.json") policy = read_runtime_policy(SimpleNamespace(runtime_policy_file=tmp_path)) assert policy["batch_window_enabled"] is True assert policy["configuration_error"] == "runtime_policy_unavailable" @pytest.mark.asyncio async def test_disabled_window_drains_same_waiting_job_and_preserves_corrupt_clock(system, monkeypatch): from app import resource_policy from app.models import Job from app.admission_clock import AdmissionClock path = system.settings.state_root / "runtime-policy.json" system.settings.runtime_policy_file = path write_policy(path, True) monkeypatch.setattr(resource_policy, "execution_policy", lambda now=None: {"batch_open": False}) response = await system.client.post("/api/jobs", json={"task_type": "math.solve", "input": {"prompt": "2+3"}, "execution_mode": "batch"}) assert response.status_code == 202, response.text job_id = response.json()["jobs"][0]["id"] coordinator = system.app.state.coordinator clock_file = system.settings.state_root / "admission-clock.json" clock_file.write_text("{corrupt", encoding="utf-8") coordinator._admission_clock = AdmissionClock(clock_file) await coordinator.run_once(force=True) assert system.provider.cpu_runs == 0 write_policy(path, False) await coordinator.run_once(force=True) await coordinator._cpu[job_id] async with system.app.state.db.session() as session: job = await session.get(Job, job_id) assert job.state == "succeeded" and job.attempt_count == 1 assert job.execution_mode == "batch" assert clock_file.read_text(encoding="utf-8") == "{corrupt" write_policy(path, True) assert not coordinator._clock_allows(job) @pytest.mark.asyncio async def test_status_and_capabilities_share_live_policy_and_workflow_limits(system): path = system.settings.state_root / "runtime-policy.json" system.settings.runtime_policy_file = path write_policy(path, False, 240) for route in ["/api/capabilities", "/api/system/status"]: response = await system.client.get(route) assert response.status_code == 200, response.text body = response.json() assert body["execution_policy"]["batch_window_enabled"] is False assert body["execution_policy"]["configuration_error"] is None assert body["workflow_limits"] == {"max_shots": 240, "source_frames": 121, "max_clip_frames": 120, "fps": 24}