|
| 1 | +"""Tests for clawteam.spawn.respawn module.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from unittest.mock import MagicMock, patch |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +from clawteam.spawn.registry import AgentHealth, HealthState |
| 10 | +from clawteam.spawn.respawn import MAX_RESPAWN_ATTEMPTS, respawn_agent |
| 11 | + |
| 12 | +# Patch targets: since respawn_agent uses lazy imports (from X import Y inside |
| 13 | +# the function body), we must patch the *source* modules so the fresh local |
| 14 | +# bindings pick up the mocks. |
| 15 | +_PATCH_RECORD = "clawteam.spawn.registry.record_outcome" |
| 16 | +_PATCH_GET_INFO = "clawteam.spawn.registry.get_agent_info" |
| 17 | +_PATCH_TM = "clawteam.team.manager.TeamManager" |
| 18 | +_PATCH_BACKEND = "clawteam.spawn.get_backend" |
| 19 | +_PATCH_SWR = "clawteam.spawn.spawn_with_retry" |
| 20 | + |
| 21 | + |
| 22 | +@pytest.fixture |
| 23 | +def mock_team(): |
| 24 | + return {"team": "test-team", "agent": "worker-1"} |
| 25 | + |
| 26 | + |
| 27 | +class TestRespawnAgent: |
| 28 | + |
| 29 | + def test_respawn_succeeds_on_first_crash(self, mock_team): |
| 30 | + health = AgentHealth( |
| 31 | + agent_name=mock_team["agent"], |
| 32 | + consecutive_failures=1, |
| 33 | + state=HealthState.degraded, |
| 34 | + ) |
| 35 | + member = MagicMock() |
| 36 | + member.agent_id = "abc123" |
| 37 | + member.agent_type = "researcher" |
| 38 | + member.model_name = "" |
| 39 | + spawn_info = { |
| 40 | + "backend": "tmux", |
| 41 | + "command": ["claude", "--dangerously-skip-permissions"], |
| 42 | + "tmux_target": "clawteam-test:0", |
| 43 | + "pid": 0, |
| 44 | + } |
| 45 | + |
| 46 | + with ( |
| 47 | + patch(_PATCH_RECORD, return_value=health), |
| 48 | + patch(_PATCH_GET_INFO, return_value=spawn_info), |
| 49 | + patch(_PATCH_TM) as mock_tm, |
| 50 | + patch(_PATCH_BACKEND), |
| 51 | + patch(_PATCH_SWR, return_value="ok") as mock_swr, |
| 52 | + ): |
| 53 | + mock_tm.get_member.return_value = member |
| 54 | + result = respawn_agent(mock_team["team"], mock_team["agent"], spawn_info=spawn_info) |
| 55 | + |
| 56 | + assert result.startswith("ok:") |
| 57 | + mock_swr.assert_called_once() |
| 58 | + |
| 59 | + def test_respawn_blocked_after_max_attempts(self, mock_team): |
| 60 | + health = AgentHealth( |
| 61 | + agent_name=mock_team["agent"], |
| 62 | + consecutive_failures=MAX_RESPAWN_ATTEMPTS + 1, |
| 63 | + state=HealthState.open, |
| 64 | + ) |
| 65 | + |
| 66 | + with patch(_PATCH_RECORD, return_value=health): |
| 67 | + result = respawn_agent(mock_team["team"], mock_team["agent"]) |
| 68 | + |
| 69 | + assert result.startswith("Error:") |
| 70 | + assert "not respawning" in result |
| 71 | + |
| 72 | + def test_respawn_fails_no_spawn_info(self, mock_team): |
| 73 | + health = AgentHealth( |
| 74 | + agent_name=mock_team["agent"], |
| 75 | + consecutive_failures=1, |
| 76 | + ) |
| 77 | + |
| 78 | + with ( |
| 79 | + patch(_PATCH_RECORD, return_value=health), |
| 80 | + patch(_PATCH_GET_INFO, return_value=None), |
| 81 | + ): |
| 82 | + result = respawn_agent(mock_team["team"], mock_team["agent"]) |
| 83 | + |
| 84 | + assert result.startswith("Error:") |
| 85 | + assert "no spawn info" in result |
| 86 | + |
| 87 | + def test_respawn_fails_no_team_member(self, mock_team): |
| 88 | + health = AgentHealth( |
| 89 | + agent_name=mock_team["agent"], |
| 90 | + consecutive_failures=1, |
| 91 | + ) |
| 92 | + spawn_info = {"backend": "tmux", "command": ["claude"]} |
| 93 | + |
| 94 | + with ( |
| 95 | + patch(_PATCH_RECORD, return_value=health), |
| 96 | + patch(_PATCH_GET_INFO, return_value=spawn_info), |
| 97 | + patch(_PATCH_TM) as mock_tm, |
| 98 | + ): |
| 99 | + mock_tm.get_member.return_value = None |
| 100 | + result = respawn_agent(mock_team["team"], mock_team["agent"], spawn_info=spawn_info) |
| 101 | + |
| 102 | + assert result.startswith("Error:") |
| 103 | + assert "not found in team config" in result |
| 104 | + |
| 105 | + def test_respawn_fails_spawn_error(self, mock_team): |
| 106 | + health = AgentHealth( |
| 107 | + agent_name=mock_team["agent"], |
| 108 | + consecutive_failures=1, |
| 109 | + ) |
| 110 | + member = MagicMock() |
| 111 | + member.agent_id = "abc" |
| 112 | + member.agent_type = "worker" |
| 113 | + member.model_name = "" |
| 114 | + spawn_info = {"backend": "tmux", "command": ["claude"]} |
| 115 | + |
| 116 | + with ( |
| 117 | + patch(_PATCH_RECORD, return_value=health), |
| 118 | + patch(_PATCH_GET_INFO, return_value=spawn_info), |
| 119 | + patch(_PATCH_TM) as mock_tm, |
| 120 | + patch(_PATCH_BACKEND), |
| 121 | + patch(_PATCH_SWR, return_value="Error: tmux not found"), |
| 122 | + ): |
| 123 | + mock_tm.get_member.return_value = member |
| 124 | + result = respawn_agent(mock_team["team"], mock_team["agent"], spawn_info=spawn_info) |
| 125 | + |
| 126 | + assert result.startswith("Error:") |
| 127 | + |
| 128 | + def test_respawn_second_attempt_still_allowed(self, mock_team): |
| 129 | + """Second crash (consecutive_failures=2) should still allow respawn.""" |
| 130 | + health = AgentHealth( |
| 131 | + agent_name=mock_team["agent"], |
| 132 | + consecutive_failures=2, |
| 133 | + state=HealthState.degraded, |
| 134 | + ) |
| 135 | + member = MagicMock() |
| 136 | + member.agent_id = "abc" |
| 137 | + member.agent_type = "worker" |
| 138 | + member.model_name = "" |
| 139 | + spawn_info = {"backend": "tmux", "command": ["claude"]} |
| 140 | + |
| 141 | + with ( |
| 142 | + patch(_PATCH_RECORD, return_value=health), |
| 143 | + patch(_PATCH_GET_INFO, return_value=spawn_info), |
| 144 | + patch(_PATCH_TM) as mock_tm, |
| 145 | + patch(_PATCH_BACKEND), |
| 146 | + patch(_PATCH_SWR, return_value="ok"), |
| 147 | + ): |
| 148 | + mock_tm.get_member.return_value = member |
| 149 | + result = respawn_agent(mock_team["team"], mock_team["agent"], spawn_info=spawn_info) |
| 150 | + |
| 151 | + assert result.startswith("ok:") |
0 commit comments