Repository navigation
Description
Summary
On upstream main b9b7e587 (after #381), Conversations.dispatch() in apps/api/src/opendirector_api/agent.py starts a project Agent session before checking whether there is any queued message to dispatch.
Around lines 198–220:
if agent_session is not None:
self.projects.local.start_agent_session(db, project_id, directory, agent_session)
...
pending = next((item for item in value['messages'] if item.get('status') == 'queued'), None)
if pending is None:
return Nonerun_chain() calls dispatch(..., run_session) after a successful completed Turn (around lines 773–779), including when the queue is empty. If pending is None, that fresh Agent session is not associated with an executing Turn and the normal produce_one() release path is never entered for it.
A targeted isolated-main reproduction reported an extra agent: session row after a successful Turn, which persists until lease expiry. The source ordering explains the suspected cause. Please validate the exact persistence/expiry behavior with a narrow test rather than inferring broader customer impact.
Expected
- Check/claim an actual queued message before starting the Agent write session for its execution, within the existing transaction and admission-lock guarantees.
- Empty dispatch should not allocate an orphan Agent session.
- If dispatch persists the selected queued message but fails, session ownership and release should remain correct.
- Preserve the race safety introduced by [feat] agent: run a message sent while a turn is running #381: a concurrent message must not slip between queue exhaustion and release of the active project chain.
Suggested acceptance tests
- A single completed Turn with no queued follow-up leaves no extra Agent session lease.
- One or more genuinely queued messages dispatch in order with valid Agent write authorization.
- Stop, rejected dispatch, failed persistence, and concurrent enqueue do not leak or prematurely revoke another Turn's session.
- Existing chained SSE/queue semantics and tests remain unchanged.
Scope / ownership
Small lifecycle/lease cleanup, separately tracked from #502 (finish-write error reporting), #476 (timing), and any original-trace export work.
Tracking only; unassigned, unscheduled, no implementation PR.
Activity