Repository navigation
fix(ui): don't evict a session's agent while a turn is running on it - #4277
Conversation
After one streaming turn, every later non-streaming turn and background goal tick on the same Agent UI session stopped at step 0 with "The request was stopped because it exceeded the allowed time", and tool confirmations were denied instantly. The streaming cleanup fires the agent's cancel event and SSE console, and both stayed attached to the cached agent. Non-streaming cache hits now start with no cancel event and a fresh silent console; goal ticks clear the cancel event (they already install their own console). Fixes amd#4196
Background goal ticks used a session's cached agent without the session lock or chat semaphore, and looked it up as a "chat" agent with the raw model id. On a gaia session that evicted the user's agent and disconnected its MCP tools; on a chat session the tick could run alongside the user's next turn on the same agent. Ticks now take the session lock and chat semaphore, skip the session while it is busy (never queueing on the lock, so they can't stall the user's next message), and resolve agent type, model, and device the way the chat path does. A user-message followup waits up to 10 s for the turn that enqueued it to release the session, as docs/spec/autonomous-agent-mode.md section 17 describes. Fixes amd#4197
When an Agent UI turn timed out, the session lock was released while the old worker thread was still running tools on the same cached agent, so the next turn could run concurrently with it. Non-streaming turns and goal ticks now signal the agent's cancel event on timeout and wait for the worker thread before returning, so the session lock and chat semaphore stay held until it is gone. The streaming cleanup waits for the producer thread the same way (off the event loop, replacing a 5 s join that blocked it), so the run stays registered and the lock held until the producer exits. Fixes amd#4198
Deleting a session, switching its agent, device, or mail provider, or detaching a document mid-turn disconnected the running agent's MCP tools, and the turn's final save then failed against the deleted session row with the error swallowed. Delete now stops the running turn and waits for it to finish (on the session lock every turn and goal tick holds) before deleting the row and evicting the agent. Agent/device/mail-provider changes and document detach return 409 while a turn is running, the same answer /api/chat/send gives for a busy session; renames are still accepted. Fixes amd#4199
|
Verdict: Approve with suggestions Deleting a chat session, switching its agent, or detaching a document mid-turn used to pull the agent out from under the running turn and quietly lose its answer. This PR makes the delete wait for the turn and makes the other two return a "busy, try again" error, which is the right split — the sidebar already deletes optimistically, so the wait is invisible, and the frontend does surface the busy error to the user. Two things to tighten before merge:
Note this PR is stacked on #4274–#4276, so the diff shows those commits too; the review above is about the top commit. Real-world evidenceNo evidence bundle was produced for this run, and the PR description shows unit tests only — the manual "delete a session mid-stream in 🔍 Technical details🟡 DELETE only stops streaming turns, but the docs claim it stops any turn (
|
Fixes a duplicate device= keyword argument the auto-merge produced: this branch and main both added a device= line to ChatAgentConfig(...) at slightly different positions, so git's 3-way merge kept both instead of flagging a conflict -- a SyntaxError that broke collection for every test importing gaia.ui.agent_loop. Also folds in main's cancel-lock handling alongside this branch's own worker-exit fix.
Same duplicate device= keyword the sibling branch's merge produced (this PR and amd#4276 both independently added a device= line to ChatAgentConfig, at different diff positions, so git kept both instead of flagging a conflict). Also combines this branch's cancel_event assignment with main's result_holder capture.
… of the API (amd#4340) ## Summary Following the Agent UI API docs to manage MCP servers got a client a 404: five documented `/api/mcp/*` endpoints (register/enable/disable/delete a server, the catalog) moved to `/api/connectors` when MCP-server lifecycle folded into the connectors model, and the doc never caught up. About three-quarters of the real backend was also simply undocumented — all of connectors, goals, onboarding, schedules, and most of hub and memory — and the required `X-Gaia-UI` CSRF header was never mentioned, so a doc-following client's first mutating call got a silent 403. Added a route-coverage test that builds the real FastAPI app and diffs its route table against every documented endpoint in both directions, so a route can't drift out of sync with the doc again the way this one did. Overlaps with amd#4277 (also edits this file, in the Sessions accordion group) in file only — rebased on latest `origin/main`, no shared lines. ## Test plan - [x] `pytest tests/unit/chat/ui/test_route_doc_coverage.py -q` — 3 passed; confirmed it fails against the pre-fix doc (72 undocumented routes, 4 fictional `/api/mcp/*` ones) - [x] `pytest tests/unit/chat/ui/ -q` — 1038 passed, no regressions - [x] `python util/check_doc_links.py --internal-only` — 1015 links checked, 0 broken Fixes amd#4255 Co-authored-by: Kalin Ovtcharov <[email protected]>
Stacked on #4276 — merge that first (stack: #4274 → #4275 → #4276). Until then this diff also shows those commits; the change here is the top commit.
Deleting a session, switching its agent, device, or mail provider, or detaching a document mid-turn disconnected the running agent's MCP tools. The turn's final save then failed against the deleted session row, and the error was swallowed. Now a delete stops the running turn and waits for it to finish before removing anything. The other changes get a 409 while a turn is running.
/api/chat/sendgives for a busy session. Renames are still accepted, because auto-titling renames sessions mid-run.docs/sdk/sdks/agent-ui.mdxnow describe the 409 and the wait.Test plan
python -m pytest tests/unit/chat/ui/test_session_mutation_during_run.py -q: 7 failed before the fix (the rename test passed, as it should); all 8 pass afterpython -m pytest tests/unit/chat/ui -q: 1041 passedpython -m pytest tests/unit/test_multi_device_wiring.py tests/unit/test_chat_inline_web_ssrf.py tests/unit/eval/test_scenario_agent_types.py tests/unit/eval/test_scenario_agent_provenance.py tests/integration/test_folder_indexing.py tests/integration/test_chat_ui_integration.py -q(plus the other session-endpoint neighbours): 330 passed, 2 skippedpython util/lint.py --black --isort+ pylint (errors-only config) on changed files: cleangaia chat --ui, delete a session while a turn is streaming. It disappears and doesn't come back, and the server log has no "Chat streaming error"Fixes #4199