Skip to content

fix(control-ui): Control UI drops its pending connection when the tab wakes during a busy Gateway - #172864

Merged
steipete merged 2 commits into
mainfrom
fix/control-ui-keep-pending-upgrade
Oct 15, 2026
Merged

steipete merged 2 commits into
mainfrom
fix/control-ui-keep-pending-upgrade

Conversation

@steipete

Copy link
Copy Markdown
Contributor

Related: #172507

What Problem This Solves

Fixes: the Control UI drops its own pending WebSocket upgrade, and shows "disconnected", when the tab regains focus or the network comes back while a busy Gateway is still answering the upgrade.

User Impact

User impact: a Control UI tab that is reconnecting to a stalled Gateway keeps its in-flight connection instead of abandoning it, and reconnecting to the same Gateway no longer re-reads the full 250 KB logs window.

Why This Change Was Made

Tab wake recovery (visibilitychange, online) asked needsWakeReconnect, which treated any socket that was not yet open as needing replacement. A socket waiting for the Gateway's 101 response is not stale: replacing it made the browser drop the TCP connection, and once the stalled main thread caught up the Gateway logged closed before connect code=1006 phase=ws_upgrade_started within milliseconds. A fresh upgrade then queued behind the same stall. The protocol client now exposes opening (socket created, not yet open), and wake recovery leaves that socket alone. The existing 30 s browser opening timeout still bounds a stuck upgrade, reconnect backoff is still skipped on wake, and Safari page-cache restores still force a fresh client.

The logs page also reset logs.tail to a full window on every same-Gateway reconnect, costing the Gateway one log read plus a synchronous redaction pass per reconnect. It now resumes from its cursor; identity changes and manual refresh still reset. Poll ownership was already single (one interval, no overlapping reads), so reconnects do not multiply poll loops.

Version diff note: the Gateway WS handshake path, keepalive, gateway-client reconnect, and Control UI socket code are identical between v2026.10.1 and v2026.10.3, so 10.3 introduced no new deadline. The report's increase lines up with longer Gateway stalls; one measured contributor is egress redaction in logs.tail, about 3x slower since the 10.3 redaction rewrite (real 250 KB tail: ~31 ms on 10.1 vs ~93-104 ms on main), plus a whole-file backward PEM-context scan on every poll. That redaction cost is tracked as a separate follow-up because it touches the secret-redaction boundary.

Evidence

  • New keeps an in-flight upgrade through tab wake while the Gateway is stalled (ui/src/api/gateway-heartbeat.node.test.ts) fails on the original code (expected true to be false) and passes with the fix; it also asserts backoff still counts as a wake reason.
  • New resumes the loaded cursor after a same-gateway reconnect (ui/src/pages/logs/logs-page.test.tsx) fails on the original code and passes with the fix.
  • The shared MockWebSocket now starts CONNECTING like a real browser socket; seven chat-delivery tests that sent requests on a never-opened socket now open it first.
  • Focused runs: the 11 UI files using the socket mock plus page-activation and logs-page suites (159 tests), and gateway-client handshake/socket-factory tests (22), all pass.
  • pnpm check:changed: all ratchets pass except the inherited env-var budget (main already has 455 OPENCLAW_* vars against a 454 budget; this diff adds none). Codex autoreview: scoped-clean at P0-P2.
  • Gap: no browser trace from the reporter's incident; other paths that can close a pending upgrade (terminal liveness reconnect, manual Retry) are unchanged.

Tab wake recovery replaced the client whenever the socket was not yet
open, dropping an in-flight upgrade while a stalled Gateway was still
answering it (Gateway logged closed before connect code=1006
phase=ws_upgrade_started). The protocol client now reports an opening
socket and wake recovery leaves it alone; the browser opening timeout
still bounds a stuck upgrade.

Same-Gateway reconnects on the logs page now resume logs.tail from the
loaded cursor instead of re-reading and re-redacting the full window.

Related: #172507
@clawsweeper

clawsweeper Bot commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

ClawSweeper review complete

ClawSweeper finished reviewing this revision. The review result is being finalized.

View the workflow run.

@steipete
steipete marked this pull request as ready for review October 15, 2026 06:49
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-15T06:54:06.958579Z 4de4506 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@clawsweeper clawsweeper Bot added P2 Normal backlog priority with limited blast radius. rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. labels Oct 15, 2026
@clawsweeper

clawsweeper Bot commented Oct 15, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs changes before merge.

What this changes

This PR preserves pending Control UI connections during tab wake and resumes log reads from the existing cursor after reconnecting.

Example: A tab becomes visible 5 seconds into a pending Gateway WebSocket upgrade.

  • Before: Wake recovery replaces the client and abandons the pending upgrade.
  • After: Wake recovery preserves the pending upgrade while the existing opening timeout bounds its lifetime.

Review scores

Measure Result What it means
Overall readiness 🦪 silver shellfish (2/6) The production fix is focused and source-backed, but the incomplete shared-fixture migration causes a confirmed CI failure.
Proof confidence 🦐 gold shrimp (3/6) The supplied host context identifies this as maintainer-authored, outside the ordinary contributor proof gate; available evidence consists of unit regressions and CI rather than a shipped browser scenario.
Patch quality 🦪 silver shellfish (2/6) 1 actionable review finding remain.

Product

Kind: Bug fix · Worth it: Yes · Fix scope: Partial
User problem: Returning to a reconnecting tab can abandon a connection that a busy Gateway has not finished opening.
Reason: The source-backed repair has clear value and maintainer-authored direction; it addresses one disconnect mechanism rather than claiming to resolve the broader incident.

Merge readiness

⛔ Needs changes before merge - 1 item remains

Keep this PR open: the connection repair is useful and remains absent from current main, but the shared mock change introduces a confirmed CI failure.

Priority: P2
Reviewed head: 4de450691902423dbdaa3e261a3096c26a56e9ff

Before merge

  • Open the shared socket in the Gateway broadcast test (P2) - Changing the mock's initial state to CONNECTING also affects src/gateway/server-broadcast.live-text.test.ts, which imports this fixture and immediately sends subscription requests without calling emitOpen(). GatewayProtocolClient.request now rejects with gateway not connected, leaving ws.sent empty and causing request.params.key to throw. The exact-head Node shard reports both failures. Add ws.emitOpen() after obtaining the socket in that test, retaining the realistic CONNECTING default.

Findings

  • [P2] Open the shared socket in the Gateway broadcast test — ui/src/api/gateway-socket.test-support.ts:28-29

Tests

  • Missing end-to-end proof: The supplied evidence does not exercise browser tab activation through a delayed real WebSocket upgrade or logs cursor recovery through a running Gateway.
Agent review details

How this fits together

The Control UI adapts the shared Gateway protocol client to browser WebSockets; page activation consumes connection state, while the logs page requests redacted log windows through logs.tail.

flowchart TD
  A[Tab activation] --> B[Browser Gateway client]
  B --> C[Shared protocol lifecycle]
  C --> D[Browser WebSocket]
  D --> E[Gateway]
  F[Logs page] --> B
  E --> G[Redacted log tail]
Loading

Technical review

Best possible solution:

Keep the focused production repair and update the Gateway broadcast fixture to open its socket before sending requests.

Do we have a high-confidence way to reproduce the issue?

Source establishes that current-main wake recovery replaces a socket whose upgrade is pending; supplied regression results support that path, but the reporter's complete outage was not reproduced.

Is this the best way to solve the issue?

The production approach fixes client-side abandonment at the existing lifecycle owner and reduces redundant log reads without adding another retry mechanism.

Full review comments:

  • [P2] Open the shared socket in the Gateway broadcast test — ui/src/api/gateway-socket.test-support.ts:28-29
    Changing the mock's initial state to CONNECTING also affects src/gateway/server-broadcast.live-text.test.ts, which imports this fixture and immediately sends subscription requests without calling emitOpen(). GatewayProtocolClient.request now rejects with gateway not connected, leaving ws.sent empty and causing request.params.key to throw. The exact-head Node shard reports both failures. Add ws.emitOpen() after obtaining the socket in that test, retaining the realistic CONNECTING default.
    Confidence: 1

Overall correctness: patch is incorrect
Overall confidence: 0.98

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against 148a19f69f2c.

Provenance checked

  • Shared protocol lifecycle keeps the original intent (7c9b8ba: Consolidate browser and Node protocol lifecycle while retaining adapter-specific policy.)
  • Wake recovery keeps the original intent (1fd8014: Preserve terminal connection recovery state instead of bypassing the reconnect owner during page activation.)
  • Logs reconnect refresh changes intended behavior with a stated reason (7dd5b52: Move Logs to Solid while preserving Gateway behavior and shared cancellation and reconnect lifetimes.)
  • Shared WebSocket fixture changes intended behavior with a stated reason (2f58ead: Support shared chat-stream delivery coverage; this PR deliberately makes the fixture begin in the browser's CONNECTING state.)

Testing

Proof path: unit tests only.

Security

None.

Evidence

Acceptance criteria:

  • pnpm test src/gateway/server-broadcast.live-text.test.ts --maxWorkers=1
  • pnpm --dir ui test src/api/gateway-heartbeat.node.test.ts src/api/gateway-chat-events.node.test.ts src/pages/logs/logs-page.test.tsx
  • pnpm check:changed
  • git diff --check

What I checked:

PR surface

Source +8, Tests +43. Total +51 across 7 files. Added test files: 0.

View PR surface stats
Area Files Added Removed Net
Source 3 10 2 +8
Tests 4 44 1 +43
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 7 54 3 +51

Review metrics

Metric Value Why it matters
Production versus test changes Production +10/-2 lines; tests and fixtures +44/-1 lines The small production increase exposes existing transport state and reuses cursor behavior; the fixture changes require one additional consumer migration.

Labels

Label changes:

  • add P2: This is a focused connection-recovery improvement with a narrow, confirmed test-fixture blocker.
  • add rating: 🦪 silver shellfish: Overall readiness is 🦪 silver shellfish; proof is 🦐 gold shrimp and patch quality is 🦪 silver shellfish.
  • add status: ⏳ waiting on author: ClawSweeper has contributor-facing work open and is waiting for author action.

Label justifications:

  • P2: This is a focused connection-recovery improvement with a narrow, confirmed test-fixture blocker.
  • rating: 🦪 silver shellfish: Overall readiness is 🦪 silver shellfish; proof is 🦐 gold shrimp and patch quality is 🦪 silver shellfish.
  • status: ⏳ waiting on author: ClawSweeper has contributor-facing work open and is waiting for author action.

Rating scale

6/6 🦀 challenger crab · 5/6 🦞 diamond lobster · 4/6 🐚 platinum hermit · 3/6 🦐 gold shrimp · 2/6 🦪 silver shellfish · 1/6 🧂 unranked krab. Overall follows the weaker of proof and patch quality; ✨ marks media proof (a screenshot, video, or linked artifact) that directly shows the changed behavior.

Workflow

ClawSweeper edits this one comment on every review. Comment @clawsweeper re-review for a fresh review only. Repair and merge execution lanes are retired.

Reviewed October 15, 2026, 3:05 AM ET / 07:05 UTC.

@steipete
steipete enabled auto-merge (squash) October 15, 2026 07:06
@openclaw-barnacle openclaw-barnacle Bot added the gateway Gateway runtime label Oct 15, 2026
@steipete
steipete merged commit 1f862d1 into main Oct 15, 2026
121 checks passed
@steipete
steipete deleted the fix/control-ui-keep-pending-upgrade branch October 15, 2026 07:21
RomneyDa pushed a commit that referenced this pull request Oct 16, 2026
)

Tab wake recovery replaced the client whenever the socket was not yet open, dropping an in-flight upgrade while a stalled Gateway was still answering it (Gateway logged closed before connect code=1006 phase=ws_upgrade_started). The protocol client now reports an opening socket and wake recovery leaves it alone; the browser opening timeout still bounds a stuck upgrade. Same-Gateway reconnects on the logs page now resume logs.tail from the loaded cursor instead of re-reading and re-redacting the full window.

Related: #172507
(cherry picked from commit 1f862d1)
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Oct 16, 2026
…claw#172864)

Tab wake recovery replaced the client whenever the socket was not yet open, dropping an in-flight upgrade while a stalled Gateway was still answering it (Gateway logged closed before connect code=1006 phase=ws_upgrade_started). The protocol client now reports an opening socket and wake recovery leaves it alone; the browser opening timeout still bounds a stuck upgrade. Same-Gateway reconnects on the logs page now resume logs.tail from the loaded cursor instead of re-reading and re-redacting the full window.

Related: openclaw#172507
RomneyDa added a commit that referenced this pull request Oct 16, 2026
The #172864 backport (1a5bdbd) made same-Gateway reconnects resume
logs.tail from the loaded cursor, but release/2026.10.5 lacks main's
boot-scoped cursor from #172828. After a Gateway restart that rewrote the
log file in place, the next poll resumed the old byte offset and appended
the new tail to retired rows (ui/src/e2e/logs-lifecycle.e2e.test.ts).

Record the hello server bootId with each cursor and read from the start
when it changes, retaining stale rows until the replacement succeeds. A
boot-change read counts as a reset so it supersedes the reconnect's
pending invalidation run instead of waiting for the next poll.
wheakerd pushed a commit to wheakerd/clawdbot that referenced this pull request Oct 22, 2026
The openclaw#172864 backport (1a5bdbd) made same-Gateway reconnects resume
logs.tail from the loaded cursor, but release/2026.10.5 lacks main's
boot-scoped cursor from openclaw#172828. After a Gateway restart that rewrote the
log file in place, the next poll resumed the old byte offset and appended
the new tail to retired rows (ui/src/e2e/logs-lifecycle.e2e.test.ts).

Record the hello server bootId with each cursor and read from the start
when it changes, retaining stale rows until the replacement succeeds. A
boot-change read counts as a reset so it supersedes the reconnect's
pending invalidation run instead of waiting for the next poll.

(cherry picked from commit ddd39ec)
Co-authored-by: RomneyDa <[email protected]>
RomneyDa pushed a commit that referenced this pull request Oct 27, 2026
)

Tab wake recovery replaced the client whenever the socket was not yet open, dropping an in-flight upgrade while a stalled Gateway was still answering it (Gateway logged closed before connect code=1006 phase=ws_upgrade_started). The protocol client now reports an opening socket and wake recovery leaves it alone; the browser opening timeout still bounds a stuck upgrade. Same-Gateway reconnects on the logs page now resume logs.tail from the loaded cursor instead of re-reading and re-redacting the full window.

Related: #172507
(cherry picked from commit 1f862d1)
RomneyDa added a commit that referenced this pull request Oct 27, 2026
The #172864 backport made same-Gateway reconnects resume logs.tail from
the loaded cursor, but 2026.10.10 lacked main's boot-scoped cursor. After a
Gateway restart that rewrote the log file in place, the next poll resumed
the old byte offset and appended the new tail to retired rows. Record the
hello server bootId with each cursor and read from the start when it
changes, retaining stale rows until the replacement succeeds.

Release adaptation: ports the logs-page hunk of main 65023f7 (#172828)
instead of the 2026.10.5 Lit adaptation ddd39ec, since 2026.10.10 carries
main's Solid logs page.

(cherry picked from commit ddd39ec)
(partial cherry pick from commit 65023f7)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

app: web-ui App: web-ui gateway Gateway runtime P2 Normal backlog priority with limited blast radius. rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. size: S status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant