|
| 1 | +""" |
| 2 | +Tests for XPC error recovery in LocalSTCrossEncoder. |
| 3 | +
|
| 4 | +This tests the automatic reinitialization of the cross-encoder model when |
| 5 | +XPC connection errors occur on macOS (common in long-running daemon processes). |
| 6 | +""" |
| 7 | + |
| 8 | +import asyncio |
| 9 | +from unittest.mock import MagicMock, patch |
| 10 | + |
| 11 | +import pytest |
| 12 | + |
| 13 | +from hindsight_api.engine.cross_encoder import LocalSTCrossEncoder |
| 14 | + |
| 15 | + |
| 16 | +class TestCrossEncoderXPCErrorRecovery: |
| 17 | + """Tests for XPC error detection and recovery in LocalSTCrossEncoder.""" |
| 18 | + |
| 19 | + @pytest.fixture |
| 20 | + def cross_encoder(self): |
| 21 | + """Create a LocalSTCrossEncoder instance.""" |
| 22 | + return LocalSTCrossEncoder(model_name="cross-encoder/ms-marco-TinyBERT-L-2-v2") |
| 23 | + |
| 24 | + def test_is_xpc_error_detection(self, cross_encoder): |
| 25 | + """Test that XPC errors are correctly detected.""" |
| 26 | + # Test various XPC error message formats |
| 27 | + xpc_error = Exception("Compiler encountered XPC_ERROR_CONNECTION_INVALID (is the OS shutting down?)") |
| 28 | + assert cross_encoder._is_xpc_error(xpc_error) |
| 29 | + |
| 30 | + xpc_error2 = Exception("XPC error occurred") |
| 31 | + assert cross_encoder._is_xpc_error(xpc_error2) |
| 32 | + |
| 33 | + # Test that non-XPC errors are not detected |
| 34 | + normal_error = Exception("Some other error") |
| 35 | + assert not cross_encoder._is_xpc_error(normal_error) |
| 36 | + |
| 37 | + @pytest.mark.asyncio |
| 38 | + async def test_predict_with_xpc_recovery(self, cross_encoder): |
| 39 | + """Test that predict() recovers from XPC errors by reinitializing.""" |
| 40 | + # Initialize the cross-encoder |
| 41 | + await cross_encoder.initialize() |
| 42 | + |
| 43 | + # Track calls to reinitialize |
| 44 | + reinit_called = False |
| 45 | + original_reinit = cross_encoder._reinitialize_model_sync |
| 46 | + |
| 47 | + def track_reinit(): |
| 48 | + nonlocal reinit_called |
| 49 | + reinit_called = True |
| 50 | + original_reinit() |
| 51 | + |
| 52 | + # Track predict attempts |
| 53 | + predict_attempts = [] |
| 54 | + original_predict = cross_encoder._model.predict |
| 55 | + |
| 56 | + def mock_predict(*args, **kwargs): |
| 57 | + predict_attempts.append(1) |
| 58 | + # Only fail on first attempt |
| 59 | + if len(predict_attempts) == 1: |
| 60 | + raise RuntimeError("Compiler encountered XPC_ERROR_CONNECTION_INVALID (is the OS shutting down?)") |
| 61 | + else: |
| 62 | + # After reinit: succeed |
| 63 | + return original_predict(*args, **kwargs) |
| 64 | + |
| 65 | + # Mock the initial predict to fail, reinit happens, then new model succeeds |
| 66 | + with patch.object(cross_encoder, "_reinitialize_model_sync", side_effect=track_reinit): |
| 67 | + with patch.object(cross_encoder._model, "predict", side_effect=mock_predict): |
| 68 | + # This should trigger XPC error on first attempt, then recover and succeed |
| 69 | + result = await cross_encoder.predict([("query", "document")]) |
| 70 | + |
| 71 | + # Verify we got a result |
| 72 | + assert result is not None |
| 73 | + assert len(result) == 1 |
| 74 | + assert isinstance(result[0], float) |
| 75 | + assert reinit_called # Should have reinitialized |
| 76 | + assert len(predict_attempts) >= 1 # At least one attempt was made |
| 77 | + |
| 78 | + @pytest.mark.asyncio |
| 79 | + async def test_predict_fails_on_non_xpc_error(self, cross_encoder): |
| 80 | + """Test that predict() does not retry for non-XPC errors.""" |
| 81 | + # Initialize the cross-encoder |
| 82 | + await cross_encoder.initialize() |
| 83 | + |
| 84 | + # Create a mock that raises a non-XPC error |
| 85 | + def mock_predict(*args, **kwargs): |
| 86 | + raise RuntimeError("Some other error") |
| 87 | + |
| 88 | + # Patch the model's predict method |
| 89 | + with patch.object(cross_encoder._model, "predict", side_effect=mock_predict): |
| 90 | + # This should fail without retry |
| 91 | + with pytest.raises(RuntimeError) as exc_info: |
| 92 | + await cross_encoder.predict([("query", "document")]) |
| 93 | + |
| 94 | + assert "Some other error" in str(exc_info.value) |
| 95 | + |
| 96 | + @pytest.mark.asyncio |
| 97 | + async def test_reinitialize_clears_model(self, cross_encoder): |
| 98 | + """Test that _reinitialize_model_sync properly clears and reinits the model.""" |
| 99 | + # Initialize the cross-encoder |
| 100 | + await cross_encoder.initialize() |
| 101 | + |
| 102 | + original_model = cross_encoder._model |
| 103 | + assert original_model is not None |
| 104 | + |
| 105 | + # Reinitialize |
| 106 | + cross_encoder._reinitialize_model_sync() |
| 107 | + |
| 108 | + # Model should be reinitialized (new instance) |
| 109 | + assert cross_encoder._model is not None |
| 110 | + assert cross_encoder._model is not original_model |
| 111 | + |
| 112 | + # Should still work |
| 113 | + result = await cross_encoder.predict([("test query", "test document")]) |
| 114 | + assert len(result) == 1 |
| 115 | + assert isinstance(result[0], float) |
| 116 | + |
| 117 | + @pytest.mark.asyncio |
| 118 | + async def test_xpc_recovery_exhausts_retries(self, cross_encoder): |
| 119 | + """Test that XPC recovery gives up after max retries.""" |
| 120 | + # Initialize the cross-encoder |
| 121 | + await cross_encoder.initialize() |
| 122 | + |
| 123 | + # Track reinit calls |
| 124 | + reinit_count = 0 |
| 125 | + original_reinit = cross_encoder._reinitialize_model_sync |
| 126 | + |
| 127 | + def track_and_fail_reinit(): |
| 128 | + nonlocal reinit_count |
| 129 | + reinit_count += 1 |
| 130 | + # Call original reinit, but the new model will also be mocked to fail |
| 131 | + original_reinit() |
| 132 | + # After reinit, patch the new model too |
| 133 | + cross_encoder._model.predict = MagicMock( |
| 134 | + side_effect=RuntimeError("Compiler encountered XPC_ERROR_CONNECTION_INVALID") |
| 135 | + ) |
| 136 | + |
| 137 | + # Mock that always raises XPC error |
| 138 | + cross_encoder._model.predict = MagicMock( |
| 139 | + side_effect=RuntimeError("Compiler encountered XPC_ERROR_CONNECTION_INVALID") |
| 140 | + ) |
| 141 | + |
| 142 | + with patch.object(cross_encoder, "_reinitialize_model_sync", side_effect=track_and_fail_reinit): |
| 143 | + # Should try once, reinitialize, try again, and fail |
| 144 | + with pytest.raises(Exception) as exc_info: |
| 145 | + await cross_encoder.predict([("query", "document")]) |
| 146 | + |
| 147 | + assert "XPC_ERROR_CONNECTION_INVALID" in str(exc_info.value) or "Failed to recover" in str(exc_info.value) |
| 148 | + assert reinit_count == 1 # Should have tried to reinitialize once |
0 commit comments