|
| 1 | +import os |
| 2 | +import sys |
| 3 | +import types |
| 4 | +import unittest |
| 5 | +from pathlib import Path |
| 6 | +from types import SimpleNamespace |
| 7 | + |
| 8 | + |
| 9 | +REPO_ROOT = Path(__file__).resolve().parents[1] |
| 10 | +if str(REPO_ROOT) not in sys.path: |
| 11 | + sys.path.insert(0, str(REPO_ROOT)) |
| 12 | + |
| 13 | +os.environ.setdefault("MODEL_ID", "test-model") |
| 14 | + |
| 15 | +fake_anthropic = types.ModuleType("anthropic") |
| 16 | + |
| 17 | + |
| 18 | +class FakeAnthropic: |
| 19 | + def __init__(self, *args, **kwargs): |
| 20 | + self.messages = SimpleNamespace(create=None) |
| 21 | + |
| 22 | + |
| 23 | +setattr(fake_anthropic, "Anthropic", FakeAnthropic) |
| 24 | +sys.modules.setdefault("anthropic", fake_anthropic) |
| 25 | + |
| 26 | +fake_dotenv = types.ModuleType("dotenv") |
| 27 | +setattr(fake_dotenv, "load_dotenv", lambda *args, **kwargs: None) |
| 28 | +sys.modules.setdefault("dotenv", fake_dotenv) |
| 29 | + |
| 30 | +import agents.s13_background_tasks as s13_background_tasks |
| 31 | +import agents.s_full as s_full |
| 32 | + |
| 33 | + |
| 34 | +class FakeMessagesAPI: |
| 35 | + def __init__(self, responses): |
| 36 | + self._responses = iter(responses) |
| 37 | + self.call_count = 0 |
| 38 | + |
| 39 | + def create(self, **kwargs): |
| 40 | + self.call_count += 1 |
| 41 | + return next(self._responses) |
| 42 | + |
| 43 | + |
| 44 | +class FakeS13BackgroundManager: |
| 45 | + def __init__(self): |
| 46 | + self._running = True |
| 47 | + self.wait_called = False |
| 48 | + |
| 49 | + def drain_notifications(self): |
| 50 | + return [] |
| 51 | + |
| 52 | + def has_running_tasks(self): |
| 53 | + return self._running |
| 54 | + |
| 55 | + def wait_for_notifications(self): |
| 56 | + self.wait_called = True |
| 57 | + self._running = False |
| 58 | + return [ |
| 59 | + { |
| 60 | + "task_id": "bg-1", |
| 61 | + "status": "completed", |
| 62 | + "preview": "done", |
| 63 | + "output_file": ".runtime-tasks/bg-1.log", |
| 64 | + } |
| 65 | + ] |
| 66 | + |
| 67 | + |
| 68 | +class FakeSFullBackgroundManager: |
| 69 | + def __init__(self): |
| 70 | + self._running = True |
| 71 | + self.wait_called = False |
| 72 | + |
| 73 | + def drain(self): |
| 74 | + return [] |
| 75 | + |
| 76 | + def has_running_tasks(self): |
| 77 | + return self._running |
| 78 | + |
| 79 | + def wait_for_notifications(self): |
| 80 | + self.wait_called = True |
| 81 | + self._running = False |
| 82 | + return [{"task_id": "bg-1", "status": "completed", "result": "done"}] |
| 83 | + |
| 84 | + |
| 85 | +class BackgroundNotificationTests(unittest.TestCase): |
| 86 | + def test_s13_agent_loop_waits_for_background_results_after_end_turn(self): |
| 87 | + messages = [{"role": "user", "content": "Run tests in the background"}] |
| 88 | + fake_bg = FakeS13BackgroundManager() |
| 89 | + fake_api = FakeMessagesAPI( |
| 90 | + [ |
| 91 | + SimpleNamespace( |
| 92 | + stop_reason="end_turn", content="Started background work." |
| 93 | + ), |
| 94 | + SimpleNamespace( |
| 95 | + stop_reason="end_turn", content="Background work completed." |
| 96 | + ), |
| 97 | + ] |
| 98 | + ) |
| 99 | + original_bg = s13_background_tasks.BG |
| 100 | + original_client = s13_background_tasks.client |
| 101 | + try: |
| 102 | + s13_background_tasks.BG = fake_bg |
| 103 | + s13_background_tasks.client = SimpleNamespace(messages=fake_api) |
| 104 | + s13_background_tasks.agent_loop(messages) |
| 105 | + finally: |
| 106 | + s13_background_tasks.BG = original_bg |
| 107 | + s13_background_tasks.client = original_client |
| 108 | + |
| 109 | + self.assertTrue(fake_bg.wait_called) |
| 110 | + self.assertEqual(fake_api.call_count, 2) |
| 111 | + self.assertTrue( |
| 112 | + any( |
| 113 | + message["role"] == "user" |
| 114 | + and isinstance(message["content"], str) |
| 115 | + and "<background-results>" in message["content"] |
| 116 | + for message in messages |
| 117 | + ) |
| 118 | + ) |
| 119 | + |
| 120 | + def test_s_full_agent_loop_waits_for_background_results_after_end_turn(self): |
| 121 | + messages = [{"role": "user", "content": "Run tests in the background"}] |
| 122 | + fake_bg = FakeSFullBackgroundManager() |
| 123 | + fake_api = FakeMessagesAPI( |
| 124 | + [ |
| 125 | + SimpleNamespace( |
| 126 | + stop_reason="end_turn", content="Started background work." |
| 127 | + ), |
| 128 | + SimpleNamespace( |
| 129 | + stop_reason="end_turn", content="Background work completed." |
| 130 | + ), |
| 131 | + ] |
| 132 | + ) |
| 133 | + original_bg = s_full.BG |
| 134 | + original_client = s_full.client |
| 135 | + try: |
| 136 | + s_full.BG = fake_bg |
| 137 | + s_full.client = SimpleNamespace(messages=fake_api) |
| 138 | + s_full.agent_loop(messages) |
| 139 | + finally: |
| 140 | + s_full.BG = original_bg |
| 141 | + s_full.client = original_client |
| 142 | + |
| 143 | + self.assertTrue(fake_bg.wait_called) |
| 144 | + self.assertEqual(fake_api.call_count, 2) |
| 145 | + self.assertTrue( |
| 146 | + any( |
| 147 | + message["role"] == "user" |
| 148 | + and isinstance(message["content"], str) |
| 149 | + and "<background-results>" in message["content"] |
| 150 | + for message in messages |
| 151 | + ) |
| 152 | + ) |
| 153 | + |
| 154 | + |
| 155 | +if __name__ == "__main__": |
| 156 | + unittest.main() |
0 commit comments