#!/usr/bin/env -S uv run --script --quiet # /// script # requires-python = ">=3.12" # dependencies = ["prefect>=3.0"] # /// """ Validates end-to-end flow execution against the zig prefect-server: 1. Deploy a flow using `.serve()` — a Prefect 3.x Runner that polls /api/deployments/get_scheduled_flow_runs in-process and executes flows in the same Python process. This is the "user-friendly" deployment pattern; it doesn't require a separate worker pool / worker daemon. 2. The schedule (`interval=2`) drives the SERVER's scheduler service to create SCHEDULED flow runs every 2 seconds. 3. The in-process Runner picks up SCHEDULED runs, transitions them SCHEDULED → PENDING → RUNNING → COMPLETED via POST /flow_runs/{id}/set_state. 4. We poll the API for a COMPLETED run with non-null start_time + end_time + run_count >= 1 — that proves every global-policy state-bookkeeping rule fired against the zig server (`test_global_policy.py` parity). Run: ./scripts/test-flow-execution """ import os import sys import threading import time from pathlib import Path from uuid import uuid4 os.environ.setdefault("PREFECT_API_URL", "http://localhost:4200/api") from prefect import flow # noqa: E402 from prefect.client.orchestration import get_client # noqa: E402 from prefect.client.schemas.objects import StateType # noqa: E402 def main() -> int: suffix = uuid4().hex[:6] deployment_name = f"smoke-{suffix}" marker = Path(f"/tmp/prefect-smoke-{suffix}.marker") @flow(log_prints=True) def hello(marker_file: str = str(marker)) -> str: Path(marker_file).write_text("ran") msg = "hello from the zig server" print(msg) return msg serve_started = threading.Event() serve_error: list[BaseException] = [] def run_serve() -> None: try: serve_started.set() hello.serve(name=deployment_name, interval=2) except BaseException as e: # noqa: BLE001 serve_error.append(e) t = threading.Thread(target=run_serve, daemon=True) t.start() serve_started.wait(timeout=10) try: # 1. Wait for serve() to register the deployment. with get_client(sync_client=True) as client: deadline = time.monotonic() + 15 deployment_id = None while time.monotonic() < deadline: try: dep = client.read_deployment_by_name(f"hello/{deployment_name}") deployment_id = dep.id break except Exception: # noqa: BLE001 time.sleep(0.5) if deployment_id is None: if serve_error: raise RuntimeError(f"serve() crashed: {serve_error[0]!r}") raise RuntimeError("deployment never registered") print(f"deployment registered: {deployment_id}") # 2. Wait for the marker — proof the flow body actually executed. deadline = time.monotonic() + 30 while time.monotonic() < deadline: if marker.exists(): print(f"✅ flow body executed (marker {marker} present)") break time.sleep(0.5) else: raise RuntimeError(f"flow body never ran (marker {marker} missing after 30s)") # 3. Verify the COMPLETED run is recorded against the server with # proper state-bookkeeping: start_time, end_time, run_count >= 1. # ALSO assert that no other runs against this deployment landed # in a CRASHED state — a single crashed run is enough to indicate # the orchestration response wire format is broken (regression # for parity-bug B1: float delay_seconds crashing pydantic decode). deadline = time.monotonic() + 10 completed_run = None while time.monotonic() < deadline: runs = client.read_flow_runs() for r in runs: if r.deployment_id == deployment_id and r.state and r.state.is_completed(): completed_run = r break if completed_run: break time.sleep(0.5) if completed_run is None: raise RuntimeError("no COMPLETED run recorded against deployment") print(f"✅ run COMPLETED: {completed_run.id}") print(f" state.type = {completed_run.state.type}") print(f" state.name = {completed_run.state.name}") print(f" start_time = {completed_run.start_time}") print(f" end_time = {completed_run.end_time}") print(f" run_count = {completed_run.run_count}") failures: list[str] = [] if completed_run.start_time is None: failures.append("start_time should be non-null after Running (global policy)") if completed_run.end_time is None: failures.append("end_time should be non-null after terminal (global policy)") if (completed_run.run_count or 0) < 1: failures.append(f"run_count should be >= 1, got {completed_run.run_count}") # Look at ALL runs against this deployment — any CRASHED run is a # red flag (B1 regression check). all_runs = client.read_flow_runs() crashed_runs = [ r for r in all_runs if r.deployment_id == deployment_id and r.state and r.state.type == StateType.CRASHED ] if crashed_runs: failures.append( f"{len(crashed_runs)} flow run(s) reached CRASHED while running this deployment —" f" indicates the orchestration response wire format is broken (likely B1:" f" non-int delay_seconds, but verify in the runner's stderr/log)." f" Crashed IDs: {[str(r.id) for r in crashed_runs[:3]]}" ) if failures: for msg in failures: print(f"❌ {msg}") return 1 print("✅ global-policy state-bookkeeping intact + no crashed runs") return 0 finally: marker.unlink(missing_ok=True) if __name__ == "__main__": sys.exit(main())