Collect completed background results as task_notification messages.
()
| 367 | |
| 368 | |
| 369 | def collect_background_results() -> list[str]: |
| 370 | """Collect completed background results as task_notification messages.""" |
| 371 | with background_lock: |
| 372 | ready_ids = [bid for bid, task in background_tasks.items() |
| 373 | if task["status"] == "completed"] |
| 374 | notifications = [] |
| 375 | for bg_id in ready_ids: |
| 376 | with background_lock: |
| 377 | task = background_tasks.pop(bg_id) |
| 378 | output = background_results.pop(bg_id, "") |
| 379 | summary = output[:200] if len(output) > 200 else output |
| 380 | notifications.append( |
| 381 | f"<task_notification>\n" |
| 382 | f" <task_id>{bg_id}</task_id>\n" |
| 383 | f" <status>completed</status>\n" |
| 384 | f" <command>{task['command']}</command>\n" |
| 385 | f" <summary>{summary}</summary>\n" |
| 386 | f"</task_notification>") |
| 387 | print(f" \033[32m[background done] {bg_id}: " |
| 388 | f"{task['command'][:40]} ({len(output)} chars)\033[0m") |
| 389 | return notifications |
| 390 | |
| 391 | |
| 392 | # ── Context ── |