Skip to content

fix(gaia-agent): stream answers on the /query sidecar route - #5567

Merged
kovtcharov-amd merged 9 commits into
amd:mainfrom
kovtcharov:kalin/stream-relayed-answers
Oct 23, 2026
Merged

kovtcharov-amd merged 9 commits into
amd:mainfrom
kovtcharov:kalin/stream-relayed-answers

Conversation

@kovtcharov

@kovtcharov kovtcharov commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

In the Agent UI on a desktop install, flagship answers didn't stream. A 400-word story sat on "Waiting on the model" for about 12 s and then appeared all at once. Pressing Stop during that wait left an empty "Stopped" bubble. The flagship runs in the daemon's sidecar there, and the sidecar built its agent with streaming off, so the model call was unstreamed and no answer text reached the relay until the turn ended. Now every relayed turn streams its answer token by token, and Stop keeps the text that has already arrived. This supersedes the draft #5006.

Test plan

  • pytest hub/agents/gaia/python/tests/test_relayed_answer_streams.py: drives the real agent loop, a real sidecar socket and the UI's relay with a model that parks mid-reply. It asserts answer text reaches the UI before the model finishes, and that Stop keeps it. All 3 cases fail without the fix.
  • pytest hub/agents/gaia/python/tests/test_server_query.py: 81 passed, including the new streaming, per-turn and stopped-run cases
  • pytest tests/unit/chat -n 4: 2164 passed
  • pytest hub/agents/gaia/python/tests -n 4: 769 passed. 3 fail in test_full_tool_bundles.py / test_manifest_tools_count_matches_real_registry because the optional browser tools aren't installed locally. They don't touch this change.
  • python util/lint.py --black --isort
  • Live: a Fireworks model (deepseek-v4p1-flash) in the Agent UI on a desktop install shows the answer growing (681 → 1,429 → 1,902 characters, 1 s apart); Stop kept the partial text (tested on local Qwen3.6, whose answers run long enough to stop mid-way)
  • Flagship eval on the next RC. Categories to watch: gaia_core, gaia_tool_selection, gaia_honesty, tool_selection, error_recovery
🔍 Technical details
  • Root cause: build_query_agent and build_session_agent construct GaiaAgent with GaiaAgentConfig's default streaming=False, and the /query route never turned it on. The base loop therefore took the send_messages branch and its "Waiting on the model" progress label, and the only text the route ever saw was the final answer. The stdio transport sets streaming=True, and so does the in-process Agent UI path, so neither had this problem. The relay, the daemon proxy and SidecarProxy.iter_lines already forward each event as it arrives.
  • Fix: agent.streaming = True on every /query turn, set after agent.console = handler so a retained session agent can't drift back.
  • Stop can now land mid-generation, and the base loop then returns status: "cancelled" with no text. _terminal_from_run_result maps that to an empty final, the same as stdio, instead of the "finished without producing an answer… This is a bug" error. token events are dropped once the run is cancelled, so a half-written JSON reply flushed at teardown can't leak.
  • The Agent UI already saves the accumulated chunk text when a relayed turn is stopped without an answer. test_relayed_stop_keeps_text.py pins that behaviour, since the fix depends on it.
  • Gateway and Fireworks models answer in the {"thought", "answer"} envelope. SSEOutputHandler has streamed the answer field since fix: stream JSON answers again and repair main CI #5147, so those answers stream too, and the new end-to-end test uses that envelope.
  • The /query SSE contract is unchanged: token was already part of it.

A relayed flagship turn in the Agent UI showed "Waiting on the model" until
the whole answer landed at once, and Stop left an empty bubble. The sidecar
built its agent with streaming off, so every model call was unstreamed and no
token events reached the relay. Every /query turn now streams, the way the
stdio transport and the in-process Agent UI path already do.

A turn stopped mid-answer ends with an empty final, as stdio does, instead of
the "finished without producing an answer" error, and no token follows the
cancel. The Agent UI keeps the text already streamed in.
@github-actions github-actions Bot added the tests Test changes label Oct 22, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Verdict: Approve with suggestions

This fixes the main complaint: on a desktop install, flagship answers in the Agent UI arrived all at once after a long "Waiting on the model", and Stop left an empty bubble. The sidecar now streams every relayed turn, and a stopped turn ends cleanly and keeps the text already shown. The fix is small and well aimed, and the new end-to-end test runs the real agent loop, socket and relay, so it would catch this regression again. Nothing blocks merge.

Things to know before merging:

  • Turning streaming on changes how every sidecar turn talks to the model, for the TUI's later sessions as well as the Agent UI. The PR already names the eval categories to watch on the next RC. Treat a drop in those as caused by this change.
  • Text the model wrote just before Stop but that hadn't been sent yet is now discarded, not sent. That is a small loss, and it's the price of keeping the half-written tail off the wire. It's acceptable, but the docs should say so.

Real-world evidence

N/A: no evidence bundle was produced for this run. The two live checks (an Agent UI answer that visibly grows, and Stop keeping partial text) are still unchecked in the test plan, so this verdict rests on static review plus the author's reported test runs. The /query route evidence and the eventual Agent UI screenshot are still owed. The screenshot can wait for the strix-halo lane.

🔍 Technical details

Issues

🟢 SPEC.md doesn't state the new cancel terminal (hub/agents/gaia/npm/SPEC.md:271): SKILL.md now promises that a stopped run ends with an empty final and that no token follows the cancel. SPEC.md, the full technical reference, says nothing about what a cancelled /query ends with. Per the CLAUDE.md doc-sync rule, add one line next to the cancel route, or in the SSE terminal-events section, so integrators reading SPEC.md see the same contract.

🟢 Tokens queued before the cancel are dropped as well (hub/agents/gaia/python/gaia_agent/server.py:1335): the filter checks run.cancel_event.is_set() at drain time, not at emit time. Real answer text that was produced but not yet drained (the loop polls every 30 ms) gets dropped along with the flushed tail. It's harmless in practice, and the client keeps what it displayed. Still, the SKILL.md wording "keep the text the token events already delivered" is the accurate framing, so don't strengthen it to "all text generated before Stop".

🟢 Timing-based tests (test_relayed_answer_streams.py): _FIRST_CHUNK_S = 8.0 on a real uvicorn and thread hop is generous. If this flakes on a loaded CI runner, raise the wait before blaming the code.

Strengths

  • agent.streaming = True is set per turn, after agent.console = handler, and test_a_retained_session_streams_even_if_its_agent_was_turned_off pins that a retained session agent can't drift back.
  • The status == "cancelled" mapping is checked before the answer keys, which matches the base loop's contract (it returns result: "" by design). A stopped run no longer surfaces the "This is a bug" error.
  • test_relayed_stop_keeps_text.py pins the UI-side behaviour this fix depends on (stopped relayed turn saves its accumulated chunks and sends done.cancelled). It covers both sides of the hop, not just the changed one.

@kovtcharov

Copy link
Copy Markdown
Contributor Author

Checked live in the Agent UI on a desktop-style install (relayed sidecar built from this branch, fireworks.deepseek-v4p1-flash): answers now stream, and Stop keeps the text written so far.

Stopped mid-answer, partial text kept

🔍 Technical details

Sampling the reply bubble once a second during "Write a 400-word story": 112 → 551 → 1,649 → 2,193 characters (before this branch: 24 characters for ~10 s, then all 2,174 at once). Stopping a 1,500-word answer at ~1,666 characters left that text in the bubble followed by "Stopped" (before: an empty "Stopped" bubble). Build: the integration branch plus #5553/#5561/#5562/#5568, app/backend/agent binary from one commit.

SPEC.md now carries the cancel contract SKILL.md already promised: a
stopped run ends with an empty final and no token after the cancel. Both
docs and the changelog say text not yet sent when the cancel lands is
dropped.
@kovtcharov

kovtcharov commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor Author

SPEC.md now states what a stopped /query run ends with, and all three docs say plainly that text not yet sent when Stop lands is dropped. The live Agent UI evidence (an answer that visibly streams, and Stop keeping the text already shown) is in this comment.

🔍 Technical details
  • SPEC.md: new paragraph in §5.1: /query streams token events before final; a cancelled run ends with an empty final, no token follows the cancel, and undrained answer text is discarded with the half-written tail.
  • Drain-time filter: kept as is. SKILL.md keeps the "keep the text the token events already delivered" wording, plus one sentence that unsent text is dropped, not delivered late. CHANGELOG says the same.
  • Streaming for every sidecar turn: intended; the eval categories to watch are already listed in the PR body for the next RC.
  • _FIRST_CHUNK_S timing: left at 8 s; will raise it if a loaded runner flakes.
  • Checks: the PR's own tests plus test_server_query.py and test_turn_idle_limit.py: 112 passed. tests/unit/chat: 2164 passed. black/isort clean. A local run of the full hub/agents/gaia/python/tests has setup errors from the local daemon guard (no daemon running here). Those errors are the same on origin/main (81 errors on both).

…answers

# Conflicts:
#	hub/agents/gaia/python/gaia_agent/server.py
auto-merge was automatically disabled October 23, 2026 08:29

Head branch was pushed to by a user without write access

@kovtcharov

Copy link
Copy Markdown
Contributor Author

Merged current main (6cbb246) into this branch. There was one conflict, in the npm CHANGELOG, and both entries are kept. Tests pass.

🔍 Technical details
  • hub/agents/gaia/npm/CHANGELOG.md: this PR's "Answers stream over /query" entry stays at the top of Unreleased. Main's "For integrators" entry (tool result keeps its status) follows it.
  • server.py merged cleanly. The cancelled → empty final mapping, the per-turn agent.streaming = True and the post-cancel token filter are all still in place.
  • Tests: 277 passed. That covers test_relayed_answer_streams.py, test_server_query.py, test_relayed_stop_keeps_text.py and the relay suites (fidelity, profiles, email relay, turn slot). black/isort clean.

@kovtcharov

Copy link
Copy Markdown
Contributor Author

Checked live in the desktop app: fresh install, local Qwen3.6-35B-A3B, and a gaia-agent binary frozen from this branch merged with main 6cbb246. I sampled the answer's visible text every 2 s.

  • Head of main: nothing visible until the answer arrives all at once, at about 20 s. Stop keeps only "Cancelled.".
  • This branch: "Thinking…" with a step count while the model reasons, then the text grows as it is written (490 → 1,311 → 1,681 characters over the last few seconds).
  • Stop mid-answer: with about 830 characters on screen, the turn kept 1,061 characters, and they are still on screen.

@github-actions github-actions Bot added the devops DevOps/infrastructure changes label Oct 23, 2026
@kovtcharov-amd
kovtcharov-amd added this pull request to the merge queue Oct 23, 2026
Merged via the queue into amd:main with commit 43e478f Oct 23, 2026
26 checks passed
@kovtcharov-amd
kovtcharov-amd deleted the kalin/stream-relayed-answers branch October 23, 2026 22:59
kovtcharov pushed a commit to kovtcharov/gaia that referenced this pull request Oct 26, 2026
The email agent's `/v1/email/query` ran with streaming off, so a relayed
turn showed "Waiting on the model" until the whole answer landed, Stop
could not cut a model call short, and a stopped turn kept no text — the
symptoms amd#5567 removed from the flagship, still present in a package
published to npm.

## What changed

- `query_routes.py` now sets `agent.streaming = True` where it already
sets `agent.console`, which is where the flagship sets it. That covers
both agent paths in this route: the per-turn agent and the
session-scoped one.
- One test in `test_query_route.py` drives the real route with the fake
agent and asserts the flag reached it; the fake starts at the config
default (`False`).
- `hub/agents/email/npm/CHANGELOG.md` names the behaviour change under
`[Unreleased]`, per the review.

<details>
<summary>🔍 Technical details</summary>

`hub/agents/email/python/gaia_agent_email/query_routes.py`, in the block
that prepares the agent before the worker thread starts. The flagship
does this at `hub/agents/gaia/python/gaia_agent/server.py:1298`.
`streaming` is a base-agent attribute
(`src/gaia/agents/base/agent.py:2423`), default off in
`GaiaAgentConfig`, read at the step and streaming boundaries.

</details>

## Test plan

- `PYTHONPATH=src:hub/agents/email/python pytest
hub/agents/email/python/tests/test_query_route.py -q` → **42 passed**.
- With only the fix reverted and the test kept: `1 failed` — `assert
False is True` for the fake's `streaming`. With the fix: `1 passed`.
That is the red/green pair.
- `black --check` and `flake8 --select=E9,F63,F7,F82` clean on both
Python files.
- Not run: the rest of the email-agent suite, and a live Agent UI
session against a real mailbox (needs Gmail credentials and Lemonade).

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

Labels

devops DevOps/infrastructure changes tests Test changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants