Skip to content

feat(frontend): reconnect dropped agent websockets and say how to recover - #9809

Merged
bobbai00 merged 8 commits into
apache:mainfrom
zyratlo:fix/agent-connection-recovery
Dec 4, 2026
Merged

bobbai00 merged 8 commits into
apache:mainfrom
zyratlo:fix/agent-connection-recovery

Conversation

@zyratlo

@zyratlo zyratlo commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

What changes were proposed in this PR?

When an agent's WebSocket closed unexpectedly, the AI Agents panel marked the agent unavailable and never tried again. The chat showed "Agent is disconnected. Please check your connection." and sending showed "WebSocket connection not available", with nothing logged. The only way back was to reopen the tab or reload the page.

Automatic reconnect
When the socket closes with a code other than 1000 while the agent's tab is open, AgentService reopens it after 1, 2, 4, 8 and 16 seconds and logs each attempt with console.warn. The agent service already sends a snapshot when a socket opens, so the agent's state and steps come back on their own. A successful reconnect starts the count over. After the fifth failed retry it logs an error and stops. Leaving the agent's tab or removing the agent cancels a pending retry.

Connection state
A new AgentConnectionState (Connecting, Connected, Lost) is tracked per agent, separate from AgentState, which only says what the agent is doing. getConnectionStateObservable exposes it, and reconnectAgent reconnects an open agent right away with a fresh count.

Chat banner
The banner still shows while the agent is unavailable, but now says what is happening:

Connection Banner
Connecting, including the first connect when an existing agent is opened Connecting to the agent...
Lost Could not reconnect to the agent service. Check that it is running, then try again. With a Reconnect button.

Sending while disconnected. sendMessage logs the agent id and socket state, then:

Case Result
The agent's tab is not open "Agent" is not connected. Open its tab in the agent panel to connect.
The agent's tab is open Starts a reconnect and shows: Reconnecting to "Agent". Send the message again once it is connected.

Both callers check the connection first, so these messages are a fallback. The chat box is disabled while the agent is unavailable, and the operator chat popover shows its own "Agent is not connected. Please open the agent chat panel first." The fallback covers a socket that closed without changing the agent's state, such as a normal close or the moment before onclose runs.

Workflow and auto-save after a reconnect
The snapshot sent on reconnect has no workflow, so an unexpected close now clears wsWorkflowActive. The canvas then follows the copy the agent saves to the database until a live step with workflow content takes over again. Auto-save, which the chat turns off while a run is in progress, now comes back on any AVAILABLE after it was turned off, so a run that ends during an outage no longer leaves it off.

Demo video

agent-connection-demo.mp4

Any related issues, documentation, discussions?

Closes #9748
The agent naming part of #9748 is in #9808
Reported on the 1.2.1-incubating RC2 vote thread: https://lists.apache.org/thread/521g2csg8g8j27zbx1mw20375hwwn0tz

How was this PR tested?

  • agent.service.spec.ts: a new "reconnecting" block using fake timers. It covers the state moving from Connecting to Connected, an open event from a replaced socket being ignored, each retry waiting its exact delay, giving up after five, the count starting over after a reconnect succeeds, deactivating or removing an agent cancelling a pending retry, and the guards and fresh count of reconnectAgent. Three sendMessage tests replace the old "not available" one: tab not open, tab open after a drop, and a socket that is still opening. A "workflow polling" test covers the database poll resuming after an unexpected close.
  • agent-chat.component.spec.ts: the connecting banner, the lost banner with its Reconnect button calling reconnectAgent, auto-save coming back after a run ends during a drop, and an idle reconnect leaving auto-save alone.
  • agent-panel.component.spec.ts: the panel's mock service gains getConnectionStateObservable.

Checked against a local stack in the browser. Drops were simulated by closing the agent's socket, and an agent service outage by pointing new agent sockets at a closed port. A drop reconnected after about one second, and chat worked afterwards. During the outage the retries ran at about 1, 3, 7, 15 and 31 seconds, then the banner showed Reconnect. Reconnect during the outage started the count over, and once the outage ended the next retry connected. Reconnect after giving up connected on the first try. Switching tabs left only the open agent's socket open. Deleting an agent with a retry pending stopped further retries. After a drop the workflow poll ran again until a live step took over, and a run that ended during a drop had auto-save back on after the reconnect.

Was this PR authored or co-authored using generative AI tooling?

Generated-by: Claude Code (Claude Opus 5.5)

@zyratlo zyratlo changed the title fix(agent-service, frontend): reconnect dropped agent websockets and say how to recover fix(frontend): reconnect dropped agent websockets and say how to recover Dec 1, 2026
@github-actions github-actions Bot added fix frontend Changes related to the frontend GUI labels Dec 1, 2026
@github-actions

github-actions Bot commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Automated Reviewer Suggestions

Based on the git blame history of the changed files, we recommend the following reviewers:

  • Contributors with relevant context: @bobbai00, @eugenegujing, @yangzhang75
    You can notify them by mentioning @bobbai00, @eugenegujing, @yangzhang75 in a comment.

@Yicong-Huang Yicong-Huang added release/v1.3 back porting to release/v1.3 release/v1.2 back porting to release/v1.2 labels Dec 1, 2026
@github-actions
github-actions Bot requested review from mengw15 and xuang7 December 1, 2026 21:32
@github-actions

github-actions Bot commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Backport auto-label report

This fix: PR was checked against each actively-supported release branch. A release/* label nominates a backport target; the branch's release manager approving this PR is what sends the fix there, and the labels left on a merged PR are exactly the branches it reached.

Blocked by a red Backport Approvals check? That required check stays red until every release/* label below is either approved by its branch's release manager or removed. To unblock a backport you do not want:

  • Remove that branch's release/* label to decline just it (the removal is remembered, so it is not added again).
  • Add the no-backport-needed label to decline every branch at once.
  • If this PR is not really a fix, change its title away from fix: and all of these labels are removed automatically; retitling back to fix: re-adds them.
Release branch Analysis
✅ release/v1.3 Change detected on this branch — label added; this fix is queued to backport here. @mengw15 decides: approving sends the fix here, removing this label declines it. The merge waits on one or the other. Review requested.
✅ release/v1.2 Change detected on this branch — label added; this fix is queued to backport here. @xuang7 decides: approving sends the fix here, removing this label declines it. The merge waits on one or the other. Review requested.

Auto-label run.

@bobbai00
bobbai00 self-requested a review December 1, 2026 21:55

@bobbai00 bobbai00 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔴 2 must-fix · 2 advisory · 1 polish: the retry loop itself holds up; a reconnect after a mid-run drop restores the chat but not the canvas or auto-save.

Correctness (2)

  • agent.service.ts:496: canvas keeps the pre-drop workflow after a reconnect (must-fix, see inline)
  • agent-chat.component.ts:184: auto-save stays off when the run ends during the outage (must-fix, see inline)

Conventions (2)

  • agent.service.spec.ts:56: title type, fix vs feat, which also decides the backport labels (advisory, see inline)
  • agent.service.ts:892: the description's popover example never shows this message (advisory, see inline)

Polish: 1 quick touch-up (see inline comments).

Verification trace

Traced the retry lifecycle: each timer is cleared on deactivate, on removal and before any new socket, and onopen/onclose act only for the current socket. The only 1000 close in agent-service follows "Agent not found" (server.ts:468-469, Bun's default code), which already removes the agent, so skipping 1000 loses nothing. Both must-fix items come from the reconnect snapshot (server.ts:475), which restores state, steps and HEAD but not the workflow or the run end.

@zyratlo zyratlo changed the title fix(frontend): reconnect dropped agent websockets and say how to recover feat(frontend): reconnect dropped agent websockets and say how to recover Dec 3, 2026
@zyratlo

zyratlo commented Dec 3, 2026

Copy link
Copy Markdown
Contributor Author

The retitle to feat did not clear the backport labels, since Backport Auto Label is hitting startup_failure repo-wide today. No backport is intended, so fix, release/v1.2 and release/v1.3 should be removed before this PR is merged.

@zyratlo
zyratlo requested a review from bobbai00 December 3, 2026 18:24
@codecov-commenter

codecov-commenter commented Dec 3, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.86%. Comparing base (bbe15c8) to head (1f087c6).
⚠️ Report is 4 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##               main    #9809   +/-   ##
=========================================
  Coverage     91.86%   91.86%           
  Complexity     5158     5158           
=========================================
  Files          1310     1310           
  Lines         58404    58468   +64     
  Branches       7617     7625    +8     
=========================================
+ Hits          53652    53714   +62     
  Misses         2859     2859           
- Partials       1893     1895    +2     
Flag Coverage Δ *Carryforward flag
access-control-service 77.44% <ø> (ø) Carriedforward from 373a72b
agent-service 99.57% <ø> (ø) Carriedforward from 373a72b
amber 86.07% <ø> (ø) Carriedforward from 373a72b
computing-unit-managing-service 63.96% <ø> (ø) Carriedforward from 373a72b
config-service 87.50% <ø> (ø) Carriedforward from 373a72b
file-service 82.18% <ø> (ø) Carriedforward from 373a72b
frontend 96.51% <100.00%> (+<0.01%) ⬆️
notebook-migration-service 83.84% <ø> (ø) Carriedforward from 373a72b
pyamber 98.72% <ø> (ø) Carriedforward from 373a72b
result-service 86.48% <ø> (ø) Carriedforward from 373a72b
workflow-compiling-service 80.90% <ø> (ø) Carriedforward from 373a72b

*This pull request uses carry forward flags. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@bobbai00 bobbai00 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM overall — all previous review items are addressed cleanly. Left a couple of non-blocking nits inline. Also note that fix, release/v1.2, and release/v1.3 labels still need to be removed to clear Backport Approval Check.

@bobbai00 bobbai00 removed fix release/v1.2 back porting to release/v1.2 release/v1.3 back porting to release/v1.3 labels Dec 3, 2026
@bobbai00
bobbai00 removed request for mengw15 and xuang7 December 3, 2026 21:28
@bobbai00
bobbai00 enabled auto-merge December 3, 2026 21:29
@bobbai00
bobbai00 added this pull request to the merge queue Dec 4, 2026
renovate-bot pushed a commit to renovate-bot/apache-_-texera that referenced this pull request Dec 4, 2026
…he#9808)

### What changes were proposed in this PR?

This PR fixes the naming half of apache#9748. An agent created without a name
was called "Bob", and two agents could have the same name with nothing
to tell them apart.

**Default name**
An agent created without a name, or with only spaces, is now named
"Agent N", where N is the lowest number none of the user's agents uses,
so deleting Agent 2 makes Agent 2 the next default again. The
registration form used to start with "Texera Agent" but cleared the box
after the first agent, so every later agent created without typing a
name became "Bob". The form now starts empty, shows the default name as
its placeholder, and says "Leave empty to name it "Agent 2"." under the
box.

**Duplicate names**
Names are trimmed, and a name the user already has gets the first free
suffix: Analyst, Analyst (2), Analyst (3). Names are compared only
within one user's agents, so two users can both have an Analyst. When a
name is changed, the form says "An agent named "Analyst" already exists,
so this one is named "Analyst (2)"." Deleting an agent frees its name.

**Where the name is decided**
The agent service picks the name in `createAgentInstance`, since its
agent store is the source of truth across browser tabs. A create waits
on `initialize()` and the workflow load before the agent reaches the
store, so the chosen name is reserved for that owner in the meantime.
Two creates at the same moment get Analyst and Analyst (2), and a create
that fails releases its name. The frontend's `nextDefaultAgentName()`
computes the same default from the agents it knows, only to show it in
the form.

#### Demo video


https://github.com/user-attachments/assets/a1758a1b-91ce-431d-8529-39fb3b9e10a0

### Any related issues, documentation, discussions?

Part of apache#9748, covers request 1 and 2
The agent connection part of apache#9748 is in apache#9809
Reported on the 1.2.1-incubating RC2 vote thread:
https://lists.apache.org/thread/521g2csg8g8j27zbx1mw20375hwwn0tz

### How was this PR tested?

* `server.spec.ts`: a table of cases for `resolveAgentName` (empty,
spaces only, gaps, trimming, suffixes), and route tests for numbering,
suffixes, names compared per owner, a deleted agent freeing its name,
two concurrent creates, and a create that throws releasing the reserved
name.
* `agent-registration.component.spec.ts`: the empty name box, the
placeholder and hint, trimming, a blank name sent as no name, and the
rename notice.
* `agent.service.spec.ts`: `nextDefaultAgentName` fills the first gap.
* `agent-panel.component.spec.ts`: the panel's mock service gains
`nextDefaultAgentName`.

Tested manually on a local stack in the AI Agents panel, as recorded in
the demo video:

1. With no agents, the name box is empty, the placeholder is "Agent 1",
and the hint says to leave it empty for that name.
2. Creating three agents with an empty name, an empty name, and only
spaces gives Agent 1, Agent 2 and Agent 3.
3. After deleting Agent 2, the form offers Agent 2, and creating with an
empty name gives Agent 2.
4. Creating "Analyst", "Analyst" and " Analyst " gives Analyst, Analyst
(2) and Analyst (3), with the rename notice on the last two.
5. After deleting Analyst, creating "Analyst" gives Analyst with no
notice.

### Was this PR authored or co-authored using generative AI tooling?

Generated-by: Claude Code (Claude Opus 5.5)

---------

Co-authored-by: Jiadong Bai <43344272+bobbai00@users.noreply.github.com>
Merged via the queue into apache:main with commit e70a133 Dec 4, 2026
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

frontend Changes related to the frontend GUI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Agent naming and connection error handling in the AI Agents panel

4 participants