diff --git a/src/panopticon/terminal/__main__.py b/src/panopticon/terminal/__main__.py index d399b2ba..4f09a807 100644 --- a/src/panopticon/terminal/__main__.py +++ b/src/panopticon/terminal/__main__.py @@ -30,6 +30,16 @@ DEFAULT_SERVICE_URL = "http://localhost:8000" +# The full `GET /tasks` read can outlast httpx's 5s default on a loaded host — and the service's own +# SQLite busy timeout is also 5s, so the default gives up just as the server is waiting on a lock. +# Read generously; connect stays short so a down service still fails fast. (`list_tasks_versioned` +# overrides this per-request with its long-poll headroom.) +SERVICE_TIMEOUT = httpx.Timeout(30.0, connect=5.0) + + +def _service_client(service_url: str) -> TaskServiceClient: + return TaskServiceClient(httpx.Client(base_url=service_url, timeout=SERVICE_TIMEOUT)) + def _run_migrate() -> None: import importlib.resources @@ -144,7 +154,7 @@ def main( _qs.wait_for_service(args.service_url) env_file = _qs.ensure_secrets_file() git_url = _qs.detect_git_url() - qs_client = TaskServiceClient(httpx.Client(base_url=args.service_url)) + qs_client = _service_client(args.service_url) repo_id, repo_name = _qs.setup_repo(qs_client, git_url, env_file) task_id = _qs.ensure_setup_repo_task(qs_client, repo_id, repo_name) from panopticon.terminal.console import run_console_local @@ -180,7 +190,7 @@ def main( ) return 0 - client = client or TaskServiceClient(httpx.Client(base_url=args.service_url)) + client = client or _service_client(args.service_url) if args.command == "tasks": for t in client.list_tasks(): print(f"{t['id']} {t['state']:<10} {t['turn']:<5} {t['slug'] or '-'}") diff --git a/tests/terminal/test_cli.py b/tests/terminal/test_cli.py index 9b717d6c..01fe5322 100644 --- a/tests/terminal/test_cli.py +++ b/tests/terminal/test_cli.py @@ -118,3 +118,44 @@ def test_restart_passes_named_targets_through_and_returns_its_code() -> None: def test_restart_rejects_an_unknown_target() -> None: with pytest.raises(SystemExit): # argparse rejects it before anything is bounced main(["restart", "containers"]) + + +def test_the_service_client_reads_generously_but_connects_fast() -> None: + from panopticon.terminal.__main__ import SERVICE_TIMEOUT + + assert SERVICE_TIMEOUT.read == 30.0 + assert SERVICE_TIMEOUT.connect == 5.0 + + +def test_the_dashboard_client_is_built_with_the_service_timeout() -> None: + from panopticon.terminal.__main__ import SERVICE_TIMEOUT + + with ( + patch("panopticon.terminal.__main__.httpx.Client") as mock_http, + patch("panopticon.terminal.dashboard.run"), + ): + main(["dashboard"]) + assert mock_http.call_args.kwargs["timeout"] is SERVICE_TIMEOUT + + +def test_the_long_poll_still_overrides_the_service_timeout() -> None: + import httpx + + from panopticon.client import TaskServiceClient + from panopticon.terminal.__main__ import SERVICE_TIMEOUT + + seen: list[dict[str, float | None]] = [] + + def handler(request: httpx.Request) -> httpx.Response: + seen.append(request.extensions["timeout"]) + return httpx.Response(200, json=[], headers={"X-Tasks-Version": "1"}) + + http = httpx.Client( + base_url="http://test", transport=httpx.MockTransport(handler), timeout=SERVICE_TIMEOUT + ) + client = TaskServiceClient(http) + client.list_tasks_versioned(since=0, wait=20.0) + client.list_tasks_versioned(since=0) + assert seen[0]["read"] == 30.0 # wait + 10 + assert seen[0]["connect"] == 30.0 # the per-request Timeout(wait + 10) sets every phase + assert seen[1] == SERVICE_TIMEOUT.as_dict()