openclaw - ✅(Solved) Fix [Bug]: [TUI] WebSocket session not cleaned up on /agent switch causes seq gap and missing responses [1 pull requests, 1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
openclaw/openclaw#63342Fetched 2026-04-09 07:55:07
View on GitHub
Comments
0
Participants
1
Timeline
2
Reactions
0
Author
Participants
Timeline (top)
labeled ×2

Error Message

  1. Sequence number collisionseq gap error on every response 19:22:58 [ws] → event agent seq=141 … stream=error reason=seq gap Note: agent start (seq 136) to agent end (seq 139) spans 19 seconds with no token events in between. The seq gap error confirms events were lost. Gateway logs show stream=error reason=seq gap on every response. Zero token stream events are sent between agent start and agent end. TUI requests are incorrectly tagged as messageChannel=webchat.

Root Cause

Switching agents in TUI with /agent <name> does not clean up the old WebSocket session. Instead, it stacks a new connection on top of the old one, causing:

  1. Duplicate sessions appear in WebUI — After /agent sales, WebUI dropdown shows two "clawdbot-tui" entries (one for main, one for sales)
  2. Sequence number collisionseq gap error on every response
  3. Token stream events never sent — Between agent start and agent end, zero token events reach the WebSocket
  4. Wrong messageChannel — TUI requests are tagged as messageChannel=webchat instead of tui

Fix Action

Workaround

Use WebUI instead of TUI. TUI is unusable in v0.2.0 due to this issue.

PR fix notes

PR #63630: fix: clean up WebSocket session on TUI agent switch

Description (problem / solution / changelog)

Summary

  • Reset the WebSocket sequence counter when switching agents/sessions in TUI, preventing seq gap errors and missing token-stream responses
  • Add resetSeq() method to GatewayClient and GatewayChatClient to allow clearing the stale sequence state
  • Call resetSeq() in setSession() before loading the new session's history

Problem

When switching agents in TUI with /agent <name>, the old WebSocket session's sequence counter persisted across the switch. Events from the new session were compared against the stale sequence of the previous session, producing seq gap errors on every response and silently dropping all token-stream events between agent start and agent end lifecycle phases. This made TUI completely unusable after any agent switch.

Changes

  • src/gateway/client.ts: Add resetSeq() method to GatewayClient that nulls the lastSeq counter
  • src/tui/gateway-chat.ts: Expose resetSeq() on GatewayChatClient wrapper
  • src/tui/tui-session-actions.ts: Call client.resetSeq() in setSession() before switching to the new session
  • src/tui/tui-session-actions.test.ts: Add resetSeq mock to test fixture

Testing

  • All existing TUI tests pass (30/30)
  • Manual: /agent <name> no longer produces seq gap errors in gateway logs
  • Manual: Token stream events are delivered correctly after agent switch

Closes #63342


This PR was generated with AI assistance (Claude).

Changed files

RAW_BUFFERClick to expand / collapse

Bug type

Behavior bug (incorrect output/state without crash)

Beta release blocker

No

Summary

Bug Description

TUI shows no response after sending a message. Tokens remain at 0. WebUI works perfectly with the same gateway and model.

Root Cause Analysis

Switching agents in TUI with /agent <name> does not clean up the old WebSocket session. Instead, it stacks a new connection on top of the old one, causing:

  1. Duplicate sessions appear in WebUI — After /agent sales, WebUI dropdown shows two "clawdbot-tui" entries (one for main, one for sales)
  2. Sequence number collisionseq gap error on every response
  3. Token stream events never sent — Between agent start and agent end, zero token events reach the WebSocket
  4. Wrong messageChannel — TUI requests are tagged as messageChannel=webchat instead of tui

Reproduction Steps

  1. openclaw gateway --port 18789 --verbose
  2. openclaw tui
  3. Send any message → no response in TUI
  4. Type /agent sales
  5. Open WebUI at http://127.0.0.1:18789
  6. Check session dropdown → two "clawdbot-tui" entries appear

Environment

  • OpenClaw-CN version: 0.2.0 (1b9f164)
  • OS: Windows 11
  • Model: ollama/qwen3.5:9b (local)
  • Node.js: via E:\Program Files\nodejs\node.exe

Verbose Log (key excerpt)

19:22:39 [agent/embedded] embedded run start: runId=07b6fda1… provider=ollama model=qwen3.5:9b thinking=off messageChannel=webchat 19:22:39 [ws] → event agent seq=136 … stream=lifecycle aseq=3 phase=start ← 19 seconds, ZERO token events → 19:22:58 [ws] → event agent seq=139 … stream=lifecycle aseq=4 phase=end 19:22:58 [ws] → event agent seq=141 … stream=error reason=seq gap 19:22:58 [agent/embedded] embedded run prompt end: durationMs=19192

Note: agent start (seq 136) to agent end (seq 139) spans 19 seconds with no token events in between. The seq gap error confirms events were lost.

Workaround

Use WebUI instead of TUI. TUI is unusable in v0.2.0 due to this issue.

Steps to reproduce

  1. openclaw gateway --port 18789 --verbose
  2. openclaw tui
  3. Send any message → no response in TUI
  4. Type /agent sales
  5. Open WebUI at http://127.0.0.1:18789
  6. Check session dropdown → two "clawdbot-tui" entries appear

Expected behavior

TUI should display the model's response normally.

When switching agents with /agent sales, the old session should be closed, and only one "clawdbot-tui" session should exist in the WebUI dropdown. No "seq gap" errors should occur.

Actual behavior

TUI shows no response and tokens stay at 0.

Gateway logs show stream=error reason=seq gap on every response. Zero token stream events are sent between agent start and agent end. TUI requests are incorrectly tagged as messageChannel=webchat.

Additionally, switching agents with /agent sales creates a duplicate "clawdbot-tui" session in the WebUI dropdown instead of replacing the old one.

OpenClaw version

0.2.0 (1b9f164)

Operating system

Windows 11

Install method

npm global

Model

ollama/qwen3.5:9b

Provider / routing chain

ollama (local) → embedded (gateway)

Additional provider/model setup details

No response

Logs, screenshots, and evidence

Impact and severity

No response

Additional information

No response

extent analysis

TL;DR

The most likely fix is to properly clean up the old WebSocket session when switching agents in TUI to prevent duplicate sessions and sequence number collisions.

Guidance

  • Verify that the issue is indeed caused by the duplicate WebSocket sessions by checking the WebUI dropdown for multiple "clawdbot-tui" entries after switching agents.
  • Investigate the TUI code to ensure that the old WebSocket session is properly closed when switching agents with the /agent command.
  • Check the gateway logs for any errors or warnings related to WebSocket connections and sequence number gaps.
  • Consider implementing a mechanism to handle sequence number gaps and prevent them from causing errors in the TUI.

Example

No code example is provided as the issue is related to the internal implementation of the TUI and gateway.

Notes

The provided workaround of using the WebUI instead of the TUI is a temporary solution, and the root cause of the issue should be addressed to ensure proper functionality of the TUI.

Recommendation

Apply a workaround by using the WebUI instead of the TUI until the issue is fixed, as the TUI is currently unusable in version 0.2.0 due to this bug.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

FAQ

Expected behavior

TUI should display the model's response normally.

When switching agents with /agent sales, the old session should be closed, and only one "clawdbot-tui" session should exist in the WebUI dropdown. No "seq gap" errors should occur.

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING