Skip to content

fix(s08): flush background task results before agent loop exits#49

Open
sirhappy wants to merge 1 commit intoshareAI-lab:mainfrom
sirhappy:fix/s08-background-task-results-lost
Open

fix(s08): flush background task results before agent loop exits#49
sirhappy wants to merge 1 commit intoshareAI-lab:mainfrom
sirhappy:fix/s08-background-task-results-lost

Conversation

@sirhappy
Copy link

@sirhappy sirhappy commented Mar 3, 2026

Problem

Closes #46.

In s08_background_tasks.py (and s_full.py), the agent_loop exits immediately when stop_reason != "tool_use". Any background task results that arrive during the final LLM call, or from tasks that are still running at that point, are silently dropped — the notification queue is never drained again.

Minimal reproduction:

  1. Ask the agent to run a slow command in the background (background_run("sleep 5 && echo done")).
  2. The LLM responds with "OK, running in the background" and returns end_turn.
  3. The loop exits. When the task finishes 5 seconds later its result sits in the queue forever.

Fix

At the exit branch (stop_reason != "tool_use") in agent_loop, before returning:

  1. Immediate drain — flush any results that completed while the LLM was generating its response.
  2. Wait loop — if tasks are still running, poll up to MAX_BG_WAIT (30 s) for them to finish, then drain again.
  3. Re-enter loop — if there are pending results, inject them as a <background-results> message and continue so the LLM can acknowledge them before the session ends.
if response.stop_reason != "tool_use":
    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(...)
        messages.append(...)
        continue  # let the LLM see the late-arriving results
    return

The same fix is applied to s_full.py where BackgroundManager is also used.

Files changed

File Change
agents/s08_background_tasks.py Add import time, MAX_BG_WAIT constant, final-drain logic in agent_loop
agents/s_full.py Add MAX_BG_WAIT constant, same final-drain logic in agent_loop

When the LLM decides it has no more work to do (stop_reason != "tool_use"),
the agent loop exited immediately, silently dropping any background task
results that arrived during or after the final LLM call.

The fix adds a final drain step at the exit point of agent_loop in both
s08_background_tasks.py and s_full.py:

1. Drain any notifications that completed while the LLM was responding.
2. If tasks are still running, wait up to MAX_BG_WAIT (30s) for them to
   finish, then drain again.
3. If there are pending results, inject them and continue the loop so the
   LLM can acknowledge them before the session ends.

Fixes shareAI-lab#46
@vercel
Copy link

vercel bot commented Mar 3, 2026

@sirhappy is attempting to deploy a commit to the crazyboym's projects Team on Vercel.

A member of the Team first needs to authorize it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Background task results may be lost when agent loop exits early

1 participant