diff --git a/AGENTS.md b/AGENTS.md index c4e623b..2a440d5 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -562,6 +562,9 @@ This file defines how coding agents should work in this repository. - Direct JSON-RPC and MCP tool `start_keep` requests with explicit `gpu_ids` must also reject IDs missing from a non-empty validated `list_gpus()` response as public invalid-parameter/tool errors before controller startup. +- MCP/REST tests that assert exact `start_keep` GPU-selection errors must stub + `KeepGPUServer.list_gpus()` explicitly. Do not let those assertions depend on + whether the host running pytest has a real visible GPU. - Keep GPU listing platform precedence aligned with controller platform detection: HIP/ROCm torch builds must prefer ROCm listing over NVML CUDA listing, and must not fall back to NVML CUDA records when torch's active diff --git a/docs/contributing.md b/docs/contributing.md index a5c1cc3..047a83c 100644 --- a/docs/contributing.md +++ b/docs/contributing.md @@ -39,6 +39,10 @@ expectations so you can get productive quickly and avoid surprises in CI. pytest tests/mcp tests/utilities/test_gpu_info.py ``` - Avoid enabling `large_memory` in CI. +- MCP/REST tests that assert exact `start_keep` GPU-selection errors should + stub `KeepGPUServer.list_gpus()` instead of relying on the host's visible GPU + state; real CUDA/ROCm machines can otherwise take the listed-GPU validation + path while CPU-only machines take the controller fallback path. - Keep broad validation matrices with the utility or controller that owns the contract; interface tests should use representative smoke cases plus side-effect guards instead of repeating every edge case. diff --git a/tests/mcp/test_server.py b/tests/mcp/test_server.py index 80874c6..73e0f60 100644 --- a/tests/mcp/test_server.py +++ b/tests/mcp/test_server.py @@ -50,6 +50,22 @@ def make_server() -> KeepGPUServer: return KeepGPUServer(controller_factory=cast(Any, dummy_factory)) +def _gpu_record(gpu_id: int) -> dict[str, Any]: + return { + "id": gpu_id, + "visible_id": gpu_id, + "platform": "cuda", + "name": f"visible {gpu_id}", + "memory_total": None, + "memory_used": None, + "utilization": None, + } + + +def _listed_gpus(*gpu_ids: int) -> dict[str, list[dict[str, Any]]]: + return {"gpus": [_gpu_record(gpu_id) for gpu_id in gpu_ids]} + + def _failing_after_work_factory(message, controllers=None): class FailsAfterWorkController(DummyController): def keep(self): @@ -922,6 +938,7 @@ def test_jsonrpc_start_keep_out_of_range_gpu_ids_returns_invalid_params(monkeypa monkeypatch.setattr(pm, "_cached_platform", pm.ComputingPlatform.CUDA) monkeypatch.setattr(torch.cuda, "device_count", lambda: 1) server = KeepGPUServer() + monkeypatch.setattr(server, "list_gpus", lambda: _listed_gpus()) req = { "id": 1, "method": "start_keep", @@ -950,7 +967,7 @@ def test_jsonrpc_start_keep_explicit_gpu_ids_with_zero_visible_devices_is_startu monkeypatch.setattr(pm, "_cached_platform", pm.ComputingPlatform.CUDA) monkeypatch.setattr(torch.cuda, "device_count", lambda: 0) server = KeepGPUServer() - monkeypatch.setattr(server, "list_gpus", lambda: {"gpus": []}) + monkeypatch.setattr(server, "list_gpus", lambda: _listed_gpus()) req = { "id": 1, "method": "start_keep", @@ -973,23 +990,7 @@ def fail_factory(**_kwargs): raise AssertionError("controller should not start for unlisted gpu_ids") server = KeepGPUServer(controller_factory=cast(Any, fail_factory)) - monkeypatch.setattr( - server, - "list_gpus", - lambda: { - "gpus": [ - { - "id": 0, - "visible_id": 0, - "platform": "cuda", - "name": "visible 0", - "memory_total": None, - "memory_used": None, - "utilization": None, - } - ] - }, - ) + monkeypatch.setattr(server, "list_gpus", lambda: _listed_gpus(0)) req = { "id": 1, "method": "start_keep", @@ -1769,6 +1770,7 @@ def test_mcp_tools_call_out_of_range_gpu_ids_returns_tool_error(monkeypatch): monkeypatch.setattr(pm, "_cached_platform", pm.ComputingPlatform.CUDA) monkeypatch.setattr(torch.cuda, "device_count", lambda: 1) server = KeepGPUServer() + monkeypatch.setattr(server, "list_gpus", lambda: _listed_gpus()) req = { "jsonrpc": "2.0", "id": 5, @@ -1801,23 +1803,7 @@ def fail_factory(**_kwargs): raise AssertionError("controller should not start for unlisted gpu_ids") server = KeepGPUServer(controller_factory=cast(Any, fail_factory)) - monkeypatch.setattr( - server, - "list_gpus", - lambda: { - "gpus": [ - { - "id": 0, - "visible_id": 0, - "platform": "cuda", - "name": "visible 0", - "memory_total": None, - "memory_used": None, - "utilization": None, - } - ] - }, - ) + monkeypatch.setattr(server, "list_gpus", lambda: _listed_gpus(0)) req = { "jsonrpc": "2.0", "id": 5,