Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions agents/s08_background_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import os
import subprocess
import threading
import time
import uuid
from pathlib import Path

Expand All @@ -44,6 +45,8 @@

SYSTEM = f"You are a coding agent at {WORKDIR}. Use background_run for long-running commands."

MAX_BG_WAIT = 30 # seconds to wait for still-running tasks before exiting


# -- BackgroundManager: threaded execution + notification queue --
class BackgroundManager:
Expand Down Expand Up @@ -200,6 +203,25 @@ def agent_loop(messages: list):
)
messages.append({"role": "assistant", "content": response.content})
if response.stop_reason != "tool_use":
# Flush results that completed while the LLM was responding.
# If tasks are still running, wait up to MAX_BG_WAIT for them.
pending = BG.drain_notifications()
if not pending:
still_running = [t for t in BG.tasks.values() if t["status"] == "running"]
if still_running:
deadline = time.monotonic() + MAX_BG_WAIT
while time.monotonic() < deadline:
if not any(t["status"] == "running" for t in BG.tasks.values()):
break
time.sleep(0.5)
pending = BG.drain_notifications()
if pending:
notif_text = "\n".join(
f"[bg:{n['task_id']}] {n['status']}: {n['result']}" for n in pending
)
messages.append({"role": "user", "content": f"<background-results>\n{notif_text}\n</background-results>"})
messages.append({"role": "assistant", "content": "Noted background results."})
continue # let the LLM see the late-arriving results
return
results = []
for block in response.content:
Expand Down
18 changes: 18 additions & 0 deletions agents/s_full.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
TOKEN_THRESHOLD = 100000
POLL_INTERVAL = 5
IDLE_TIMEOUT = 60
MAX_BG_WAIT = 30 # seconds to wait for still-running tasks before exiting

VALID_MSG_TYPES = {"message", "broadcast", "shutdown_request",
"shutdown_response", "plan_approval_response"}
Expand Down Expand Up @@ -677,6 +678,23 @@ def agent_loop(messages: list):
)
messages.append({"role": "assistant", "content": response.content})
if response.stop_reason != "tool_use":
# Flush results that completed while the LLM was responding.
# If tasks are still running, wait up to MAX_BG_WAIT for them.
pending = BG.drain()
if not pending:
still_running = [t for t in BG.tasks.values() if t["status"] == "running"]
if still_running:
deadline = time.monotonic() + MAX_BG_WAIT
while time.monotonic() < deadline:
if not any(t["status"] == "running" for t in BG.tasks.values()):
break
time.sleep(0.5)
pending = BG.drain()
if pending:
txt = "\n".join(f"[bg:{n['task_id']}] {n['status']}: {n['result']}" for n in pending)
messages.append({"role": "user", "content": f"<background-results>\n{txt}\n</background-results>"})
messages.append({"role": "assistant", "content": "Noted background results."})
continue # let the LLM see the late-arriving results
return
# Tool execution
results = []
Expand Down