Skip to content

Fix: Restore Stop Generation Button During Agent Session Execution Loop#5970

Open
angelplusultra wants to merge 6 commits into
masterfrom
5969-bug-stopgenerationbutton-disappears-on-the-first-prompt-that-initiates-an-agent-session
Open

Fix: Restore Stop Generation Button During Agent Session Execution Loop#5970
angelplusultra wants to merge 6 commits into
masterfrom
5969-bug-stopgenerationbutton-disappears-on-the-first-prompt-that-initiates-an-agent-session

Conversation

@angelplusultra

Copy link
Copy Markdown
Contributor

Pull Request Type

  • ✨ feat (New feature)
  • 🐛 fix (Bug fix)
  • ♻️ refactor (Code refactoring without changing behavior)
  • 💄 style (UI style changes)
  • 🔨 chore (Build, CI, maintenance)
  • 📝 docs (Documentation updates)

Relevant Issues

resolves #5969

Description

<StopGenerationButton /> never appeared during the first execution loop of a new agent session. Two causes:

  1. The HTTP handoff that starts an agent session ends with a statusResponse ("Swapping over to agent chat"), and handleChat treats every statusResponse as end-of-exchange, clearing loadingResponse right as the agent starts working.
  2. The websocket message listener called setLoadingResponse(true) then setLoadingResponse(false) in the same synchronous handler, which React 18 batches into a net false on every incoming agent event — so the loading state could never recover.

Fix (frontend only):

  • loadingResponse is set on when the agent websocket is established, and the closing handoff statusResponse is no longer allowed to clear it once the session is active (guards against either ordering of the handoff chunks).
  • Each socket event now maps to a loading state via agentEventLoadingState() in utils/chat/agent.js: work events (status updates, text chunks, tool calls) keep it on; awaiting-user events (WAITING_ON_INPUT — previously ignored by the frontend entirely — plus toolApprovalRequest/clarificationRequest) turn it off; passive bookkeeping events (rename_thread, chatId, usageMetrics, citations, etc.) leave it untouched. The passive bucket matters because rename_thread arrives after the async chat save + thread auto-rename complete, i.e. after WAITING_ON_INPUT, and would otherwise re-show the stop button indefinitely.

The stop button now stays visible for the entire execution loop and hands back to the send button when the agent is waiting on the user.

Visuals (if applicable)

Additional Information

Developer Validations

  • I ran yarn lint from the root of the repo & committed changes
  • Relevant documentation has been updated (if applicable)
  • I have tested my code functionality
  • Docker build succeeds locally

@angelplusultra angelplusultra changed the title fix: restore stop generation button during agent session execution loops Fix: Restore Stop Generation Button During Agent Session Execution Loop Jul 7, 2026
fred-terzi added a commit to fred-terzi/totem-llm that referenced this pull request Jul 8, 2026
…op (port of PR Mintplex-Labs#5970)

Restore the stop generation button during agent WebSocket sessions by using
event-based loading state management instead of unconditional true/false sandwhich.

Changes:
- Added agentEventLoadingState() dispatcher in utils/chat/agent.js to classify
  incoming socket events into three buckets: active work (keep stop button),
  awaiting user (show send button), and passive bookkeeping (leave unchanged)
- Rewrote attachSocketListeners() message handler to call setLoadingResponse
  only when agentEventLoadingState returns a non-null opinion
- Added setLoadingResponse(true) after AGENT_SESSION_START to restore loading
  state that the HTTP handoff statusResponse cleared
- Guard statusResponse clearing in handleChat() so it doesn't hide the stop
  button once an agent WebSocket session is live
- Reverted broken shouldShowStopButton reference back to isStreaming, fixing
  ReferenceError crash caused by incomplete revert in commit 733da21

Resolves: stop button never appearing during first agent loop, and stop button
getting permanently stuck due to agentSessionActive always being true with
persistent sockets.
…n-the-first-prompt-that-initiates-an-agent-session

@shatfield4 shatfield4 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just 1 small nit here, other than that LGTM. It works and solves the bug in a simple way.

Comment thread frontend/src/utils/chat/agent.js Outdated
…n-the-first-prompt-that-initiates-an-agent-session
@timothycarambat

Copy link
Copy Markdown
Member

@angelplusultra can we get this moving?

@timothycarambat timothycarambat left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noting this because i was testing this prior to review:

With LMStudio, if I kill the response when generating a response - the UI properly shows "Agent session ended" and the button returns to a sendable state. However on the backend the message continues.

The intention for this PR is JUST to fix the button state, yeah? Because on reload of the page the completed message is present.

If this PR is JUST for button state, we need to stack PRs as merging just this to mater results in still a misleading state and we should merge this fix + actual stopping generation in a single commit.

@angelplusultra

Copy link
Copy Markdown
Contributor Author

Noting this because i was testing this prior to review:

With LMStudio, if I kill the response when generating a response - the UI properly shows "Agent session ended" and the button returns to a sendable state. However on the backend the message continues.

The intention for this PR is JUST to fix the button state, yeah? Because on reload of the page the completed message is present.

If this PR is JUST for button state, we need to stack PRs as merging just this to mater results in still a misleading state and we should merge this fix + actual stopping generation in a single commit.

Yes, the intention of this PR is just to restore the functionality of the abort feature as is. The state of the button was lost somehow in a v2 migration. We are splitting the true abort feature into 2 steps. First fix it as is. Then refactor to be a full end-to-end abort.

…rompt-that-initiates-an-agent-session' of github.com:Mintplex-Labs/anything-llm into 5969-bug-stopgenerationbutton-disappears-on-the-first-prompt-that-initiates-an-agent-session
…tionbutton-disappears-on-the-first-prompt-that-initiates-an-agent-session
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.

[BUG]: <StopGenerationButton /> Disappears on The First Prompt That Initiates an Agent Session

3 participants