(messages: list)
| 235 | rounds_since_todo = 0 |
| 236 | |
| 237 | def agent_loop(messages: list): |
| 238 | global rounds_since_todo |
| 239 | while True: |
| 240 | # s05: nag reminder — inject if model hasn't updated todos for 3 rounds |
| 241 | if rounds_since_todo >= 3 and messages: |
| 242 | messages.append({"role": "user", |
| 243 | "content": "<reminder>Update your todos.</reminder>"}) |
| 244 | rounds_since_todo = 0 |
| 245 | |
| 246 | response = client.messages.create( |
| 247 | model=MODEL, system=SYSTEM, messages=messages, |
| 248 | tools=TOOLS, max_tokens=8000, |
| 249 | ) |
| 250 | messages.append({"role": "assistant", "content": response.content}) |
| 251 | |
| 252 | if response.stop_reason != "tool_use": |
| 253 | force = trigger_hooks("Stop", messages) |
| 254 | if force: |
| 255 | messages.append({"role": "user", "content": force}) |
| 256 | continue |
| 257 | return |
| 258 | |
| 259 | rounds_since_todo += 1 |
| 260 | results = [] |
| 261 | for block in response.content: |
| 262 | if block.type != "tool_use": |
| 263 | continue |
| 264 | |
| 265 | blocked = trigger_hooks("PreToolUse", block) |
| 266 | if blocked: |
| 267 | results.append({"type": "tool_result", "tool_use_id": block.id, |
| 268 | "content": str(blocked)}) |
| 269 | continue |
| 270 | |
| 271 | handler = TOOL_HANDLERS.get(block.name) |
| 272 | output = handler(**block.input) if handler else f"Unknown: {block.name}" |
| 273 | |
| 274 | trigger_hooks("PostToolUse", block, output) |
| 275 | |
| 276 | # s05: reset nag counter when todo_write is called |
| 277 | if block.name == "todo_write": |
| 278 | rounds_since_todo = 0 |
| 279 | |
| 280 | results.append({"type": "tool_result", "tool_use_id": block.id, |
| 281 | "content": output}) |
| 282 | |
| 283 | messages.append({"role": "user", "content": results}) |
| 284 | |
| 285 | |
| 286 | if __name__ == "__main__": |
no test coverage detected