Run tool in a daemon thread. Returns background task ID.
(block)
| 285 | |
| 286 | |
| 287 | def start_background_task(block) -> str: |
| 288 | """Run tool in a daemon thread. Returns background task ID.""" |
| 289 | global _bg_counter |
| 290 | _bg_counter += 1 |
| 291 | bg_id = f"bg_{_bg_counter:04d}" |
| 292 | cmd = block.input.get("command", block.name) |
| 293 | |
| 294 | def worker(): |
| 295 | result = execute_tool(block) |
| 296 | with background_lock: |
| 297 | background_tasks[bg_id]["status"] = "completed" |
| 298 | background_results[bg_id] = result |
| 299 | |
| 300 | with background_lock: |
| 301 | background_tasks[bg_id] = { |
| 302 | "tool_use_id": block.id, |
| 303 | "command": cmd, |
| 304 | "status": "running", |
| 305 | } |
| 306 | threading.Thread(target=worker, daemon=True).start() |
| 307 | print(f" \033[33m[background] dispatched {bg_id}: {cmd[:40]}\033[0m") |
| 308 | return bg_id |
| 309 | |
| 310 | |
| 311 | def collect_background_results() -> list[str]: |