Skip to content

fix(ui): never re-run a failed chat turn behind the user's back - #5338

Merged
kovtcharov-amd merged 5 commits into
amd:mainfrom
kovtcharov:claude/rel-ui-no-hidden-retry
Oct 19, 2026
Merged

kovtcharov-amd merged 5 commits into
amd:mainfrom
kovtcharov:claude/rel-ui-no-hidden-retry

Conversation

@kovtcharov

@kovtcharov kovtcharov commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

Before: when an Agent UI turn hit a retryable model error (the model got evicted, a network blip), GAIA reloaded the model and ran the whole query again. Every tool the turn had already run (a file write, a shell command, a sent email) ran a second time, and all the user saw was "Model reloaded — retrying...". After: the turn ends with the error, which says whether sending it again can help, and the user decides. The next turn's pre-flight reloads an evicted model.

Reproduced by tests/unit/chat/ui/test_no_hidden_retry.py: on main the tool runs twice, here once. Covers both the streamed and the non-streamed paths.

Overlaps #5241 and #5275 (they also edit _chat_helpers.py, in different functions) and #5336 (its pre-flight docstring touches the same note; whichever lands second keeps one sentence).

Test plan

  • pytest tests/unit/chat/ui tests/unit/test_chat_preflight.py tests/unit/test_ctx_overflow_device_profile_2884.py tests/unit/test_lemonade_error_classification.py: all pass
  • python util/lint.py --black --isort --pylint --flake8 --test-lanes
🔍 Technical details
  • Removed the except → _maybe_load_expected_model → process_query replay in _run_agent (streamed) and _query (non-streamed). Errors still go through _classify_chat_exception, so the user sees the classified user_message.
  • Resuming from the failed model call would need the agent loop to checkpoint mid-turn (agents/base), which is outside the UI backend. Failing visibly is the safe half; resuming can follow there.
  • LemonadeModelNotLoadedError.user_message now says the model was unloaded and to send the message again, since nothing reloads it mid-turn any more.
  • LLM-affecting (error path): watch gaia_core and rag_quality on the next RC. A turn that used to recover silently from an eviction now fails once.

@github-actions github-actions Bot added tests Test changes llm LLM backend changes performance Performance-critical changes labels Oct 18, 2026
@kovtcharov
kovtcharov marked this pull request as ready for review October 18, 2026 17:10
@github-actions

Copy link
Copy Markdown
Contributor

Verdict: Approve with suggestions

When a model error hit mid-turn, the Agent UI used to reload the model and quietly run the whole query again, so any tool the turn had already run (a file write, a shell command, a sent email) ran a second time. This PR removes that replay from both the streamed and non-streamed chat paths. The turn now ends with an error that tells the user whether sending it again will help. That is the right trade: a visible failure beats silently doing a side effect twice, and it fits the project's fail-loudly rule.

Nothing blocks the merge. I traced the recovery path: both chat paths still run the model pre-flight at the start of every turn, so "send your message again" really does reload an evicted or too-small model first. The new tests reproduce the double run and pin it to a single run on both paths.

One thing to fix in the PR description. The technical notes say the "model not loaded" message was left alone, but the diff rewrites it. Update that bullet so reviewers aren't confused.

The behaviour change is expected: a turn that used to recover silently from an eviction now fails once and asks the user to resend. Watch gaia_core and rag_quality on the next RC, as the PR already notes.

Real-world evidence

N/A: no evidence bundle was produced for this run. The change sits on the Agent UI error path, and real model eviction needs live inference, so it's deferred to the strix-halo lane. The verdict rests on static review and the PR's unit tests. pytest isn't installed on this runner, so I couldn't re-run them here.

🔍 Technical details

Issues

  • 🟢 Stale PR description. The third bullet says LemonadeModelNotLoadedError.user_message "still reads 'Reloading the model…' … left alone here". Commit ca2baf2a changes it to "The model was unloaded before it could answer. Send your message again: GAIA loads it first." (src/gaia/llm/providers/lemonade.py:146). Drop or reword that bullet.
  • 🟢 Module-wide pytestmark = pytest.mark.asyncio also marks the sync test test_a_too_small_window_says_to_send_it_again (tests/unit/chat/ui/test_no_hidden_retry.py:24). asyncio_mode = "auto" is already set in pyproject.toml, so the mark isn't needed. Depending on the pytest-asyncio version, putting it on a non-coroutine raises a warning. Either remove the module mark or move the sync test out of its scope.
  • 🟢 Stale reference in another test's docstring. tests/unit/test_ctx_overflow_device_profile_2884.py:12 still describes the chat layer announcing "Model reloaded — retrying..." and replaying the turn as current behaviour. It's harmless, but now it's history. Consider rewording it to the past tense or trimming it.

Verified

  • The pre-flight _maybe_load_expected_model still runs on every turn: non-streamed at _chat_helpers.py:2361, streamed at :2930. That makes the new "send it again" wording accurate for both LemonadeModelNotLoadedError and the too-small-window overflow.
  • mark_reloadable() is used by both classifiers, _classify_lemonade_response and classify_lemonade_exception. A context overflow from loading at too small a window therefore no longer tells the user "Start a fresh task", which would have been wrong.
  • No remaining code reads .retryable to drive a UI replay. The only other readers are in llm/retry.py, for StreamStalledError, and they aren't affected.

Strengths

  • The regression test models exactly the dangerous case: a side effect followed by a model error. It asserts the tool ran once, that no "retrying" status was emitted, and that the user sees the classified message.
  • Removing two near-duplicate retry blocks (about 70 lines, including a broad-except that swallowed reload failures) takes out a silent-fallback path rather than patching it.
  • The docstrings in relay.py and _chat_helpers.py were updated to match, so nothing still describes a retry that no longer exists.

kovtcharov and others added 4 commits October 18, 2026 23:15
When a turn hit a retryable model error (the model was evicted, a network
blip), the Agent UI reloaded the model and ran the whole query again. Any
tool the turn had already run (a file write, a shell command, a sent
email) ran a second time, and the user only saw "Model reloaded,
retrying...".

The turn now ends with the error, which says whether sending it again can
help, and the user chooses to resend. The next turn's pre-flight reloads
an evicted model. Applies to both the streamed and non-streamed paths.
Without the hidden retry, an overflow caused by a model loaded at too
small a window told the user to start a fresh task, though sending the
message again works: the next turn's pre-flight reloads it at the right
size. That case now says so, and comments that described the removed
retry are corrected.
Nothing reloads the model when a turn finds it unloaded any more; the
next turn's pre-flight does. The message now says so.
The new no-retry test marked its one sync test as asyncio, which the
auto mode already covers. An older overflow test still described the
chat layer replaying the turn as current behaviour.
@kovtcharov-amd
kovtcharov-amd force-pushed the claude/rel-ui-no-hidden-retry branch from ca2baf2 to 52fa734 Compare October 19, 2026 06:32
kovtcharov-amd added a commit to kovtcharov/gaia that referenced this pull request Oct 19, 2026
Before: two Agent UI chats on different models swapped models on every
turn, and starting a turn unloaded models that other apps sharing
Lemonade were answering with. When GAIA couldn't get the model (lease
timeout, not enough memory, server not answering), it showed a warning
and answered anyway, so Lemonade loaded the model itself and skipped the
free-memory check. Settings → Load and the auto-title went straight to
Lemonade with no lease or lock, so either could evict a model
mid-answer, and a title could load a second model. After: GAIA only ever
unloads models it loaded itself, and only to make room. A turn that
can't get its model fails and says why. Titles run on the side slot and
never load anything. Load asks before interrupting a chat that's
answering.

- **Shared server, shared rules.** Settings → Load, the post-download
auto-load and titles take the same lease and lock a chat turn does, in
the same order.
- **Stop is honoured before a load.** A turn stopped while waiting for
the model slot or another chat's load ends there: no load, no query. The
wait for another chat's load is shown.
- **A restart still knows GAIA's own models.** Chat models already on
GAIA's own Lemonade at startup count as its own, so they can be unloaded
to make room; on a server you run, nothing is.

Reproduced by `tests/unit/chat/ui/test_model_slot_discipline.py`; each
case fails on main and passes here, including a whole turn stopped
during the pre-flight.

Overlaps amd#5241 and amd#5275 (both edit `_chat_helpers.py`; different
functions). Builds on the amd#4772 discussion without fixing amd#4772 itself:
the title still uses the model it's given, but it can no longer load it.

**Merging with amd#5328:** whichever of the two lands second also carries,
as agreed with that PR's author:
- `except LeaseWaitCancelled` (quiet) in `_preflight_load`, ahead of its
catch-all;
- the chat title's wait for the model wrapped in `lease_wait_limit(30)`,
with the 504 logged at info and the title skipped that turn, plus a test
that it gives up.
- keep this PR's own raises of `InsufficientMemoryError` and
`ModelInUseError` ahead of that clause.

**Merging with amd#5337:** the two PRs conflict in one hunk of
`_chat_helpers.py` next to `model_load_lock`. Both sides only add code
there, so keep both. I ran a trial merge of the two locally: the UI
suite and amd#5337's non-streamed timeout test passed 8 times out of 8.
That test was racy with amd#5336's stop check, and amd#5337 now fixes it.

## Test plan
- [x] `pytest tests/unit/chat/ui tests/unit/test_chat_preflight.py
tests/unit/test_model_server_info.py
tests/unit/test_users_lemonade_untouched.py tests/unit/test_ui_warmup.py
tests/unit/test_broker_wiring.py`: all pass
- [x] `npx vitest run` in `src/gaia/apps/webui`: all pass, new
`api.loadModel.test.ts`
- [x] `python util/lint.py --black --isort --pylint --flake8`
- [ ] Live: two chats on two models that both fit, and confirm neither
is unloaded between turns

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

- `_maybe_load_expected_model` no longer unloads other resident chat
models. On `InsufficientMemoryError` it unloads only models in
`_models_loaded_here` (pre-flight and Settings loads record there, with
names normalized), says so in a status event, and loads once more.
Otherwise it raises.
- Exceptions now raised instead of a warning status:
`BrokerUnavailableError` (with the model and what to check),
`InsufficientMemoryError` (unchanged text), and `RuntimeError` for
anything else, including a non-200 `/health`. The streaming and
non-streamed paths both show the reason. The non-streamed path used to
substitute "make sure Lemonade is running" for every unclassified error.
- `_maybe_load_expected_model` wraps `_preflight_load`: any exception
raised while the turn's Stop flag or its own cancel event is set is read
as the Stop. That covers turns that pass those in, which the streamed
turn does. A non-streamed turn's Stop reaches the wait only through
amd#5328's `interactive_turn(cancel=...)`, so there only the `except
LeaseWaitCancelled` clause above keeps it quiet. amd#5328's
`test_stop_during_a_queued_preflight_stays_quiet_on_the_non_streaming_path`
guards that.
- A failed Settings or post-download load shows in `/api/system/status`
`probe_warnings` until a load succeeds.
- `model_load_lock` is taken with `_take_model_load_lock` (polls, shows
status, returns on Stop) after the lease. `_load_through_the_slot` in
`routers/system.py` uses the same lease-then-lock order.
- Title: `LemonadeClient.chat_completions` under one background-priority
`model_lease` spanning the residency check and the call;
`id_slot=SIDE_SLOT` when the model has two slots; `auto_download=False`.
- Ownership: the UI keeps a record of the models it loaded (`<GAIA
home>/ui/loaded_models.json`, written whole under a lock): pre-flight
and Settings loads, and any load its own agents make (a `LemonadeClient`
load listener). Models Lemonade no longer has loaded drop out of it. A
same-name copy at too small a window is reloaded only if the UI loaded
it or it is an auto-load (Lemonade's 4096 default or the supervisor's
profile window); otherwise `ModelInUseError` says another app has it.
- `/api/system/load-model` gains `force`. Without it the endpoint
returns 409 while any run is active. `loadModelAskingFirst` turns that
into a `confirm()` in Settings. The API schema and types are
regenerated.
- Updated tests that pinned the old behaviour: unload-before-load
(amd#1382's double-load case is now covered by the free-memory check),
swallowed pre-flight failures, and raw-HTTP titles.
- Not changed here: the retry-on-error path that re-runs the whole query
is amd#5338.
- LLM-affecting? Error surfacing only; prompts and tools are unchanged.
Categories to watch on the next RC: `rag_quality`, `gaia_core`.
</details>

---------

Co-authored-by: Kalin Ovtcharov <[email protected]>
# Conflicts:
#	src/gaia/ui/_chat_helpers.py
@kovtcharov-amd
kovtcharov-amd added this pull request to the merge queue Oct 19, 2026
Merged via the queue into amd:main with commit 8ddd6c1 Oct 19, 2026
38 checks passed
@kovtcharov-amd
kovtcharov-amd deleted the claude/rel-ui-no-hidden-retry branch October 19, 2026 13:29
kovtcharov-amd added a commit to kovtcharov/gaia that referenced this pull request Oct 19, 2026
…de/ui-composer-context-retry-779e65

main no longer re-runs a failed turn, so the follow-up inbox's re-offer for
that re-run (requeue_taken) has no caller and goes, with its test.
kovtcharov-amd added a commit to kovtcharov/gaia that referenced this pull request Oct 19, 2026
Brings in main's removal of the in-turn retry (amd#5338), so the panel's
retried-turn handling is dropped with it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

llm LLM backend changes performance Performance-critical changes tests Test changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants