Skip to content

fix(gateway): distinguish reachable-but-errored from unreachable in probe diagnostics - #93948

Merged
steipete merged 5 commits into
openclaw:mainfrom
xialonglee:fix/issue-79099-gateway-probe-tristate
Jun 19, 2026
Merged

steipete merged 5 commits into
openclaw:mainfrom
xialonglee:fix/issue-79099-gateway-probe-tristate

Conversation

@xialonglee

Copy link
Copy Markdown
Contributor

Root Cause

Two independent gaps produced the same symptom — doctor reporting "Gateway not running" while the gateway listener was actually alive:

  1. probe.ts — connectLatencyMs never set for pre-hello close: probeGateway only sets connectLatencyMs after onHelloOk. When the WebSocket connects but the handshake fails (protocol mismatch, auth rejection, version skew), onClose fires before onHelloOk, leaving connectLatencyMs === null. Downstream, isProbeReachable checks connectLatencyMs != null and returns false, making the probe indistinguishable from a TCP-level unreachable.

  2. doctor-gateway-health.ts / doctor-gateway-daemon-flow.ts — "gateway closed" check always renders "Gateway not running": Both health check functions catch errors from callGateway and test message.includes("gateway closed"). When true, they unconditionally print "Gateway not running", even when the WebSocket was connected and the close was a server-side rejection (protocol mismatch, etc.). The port-scan panel correctly shows the gateway PID, creating a contradiction.

Summary

  • Gateway probe diagnostics could not distinguish "reachable but handshake failed" from "unreachable", producing misleading "Gateway not running" messages in doctor output even when the gateway listener was alive.
  • probe.ts: Before settleProbe({ ok: false, ... }), check whether the transport ever connected (connectError == null). If the WebSocket was accepted but closed before the hello completed, set connectLatencyMs so downstream code can tell the transport was reachable (tri-state: unreachable / reachable-but-errored / healthy).
  • doctor-gateway-health.ts: Use the existing isGatewayTransportError(err) && err.kind === "closed" guard to distinguish a connected-then-closed WebSocket from a never-connected transport. When connected, render the specific close code and reason instead of "Gateway not running."
  • doctor-gateway-daemon-flow.ts: Same pattern — use isGatewayTransportError with kind === "closed" so the daemon flow shows the close reason when the listener is alive.
  • doctor-gateway-health.test.ts: Add isGatewayTransportError: vi.fn(() => false) mock to keep existing tests passing.

Fixes #79099

Verification

  • pnpm test src/gateway/probe.test.ts src/commands/doctor-gateway-health.test.ts src/commands/doctor-gateway-daemon-flow.test.ts — 145 tests passed across 4 test files and 2 Vitest shards
  • pnpm check:changed — passing
  • Autoreview: findings addressed (P2 test-coverage gap and P3 code-duplication noted but accepted as non-blocking downstream)
  • CI: pending

Real behavior proof

Behavior addressed: doctor no longer falsely reports "Gateway not running" when the gateway listener is alive but the probe/health RPC fails due to handshake error (protocol mismatch, auth rejection, version skew). Probe diagnostics now properly distinguish three states: unreachable / reachable-but-errored / healthy.

Real environment tested: Linux x86_64, Node 24, branch fix/issue-79099-gateway-probe-tristate, commit d553f04279, OpenClaw 2026.6.2 built from source

Exact steps or command run after this patch:

# 1. Build binary
pnpm openclaw --version

# 2. Start local gateway with auth=none (healthy path)
pnpm openclaw gateway run --allow-unconfigured --auth none --port 19999 &

# 3. Probe healthy gateway
pnpm openclaw gateway probe --url ws://localhost:19999 --timeout 5000

# 4. Start gateway with token auth, connect with wrong token (reachable-but-rejected)
pnpm openclaw gateway run --allow-unconfigured --auth token --token secrettoken --port 19998 &
pnpm openclaw gateway call health --url ws://localhost:19998 --token wrongtoken --timeout 5000

# 5. Probe unreachable port (no gateway)
pnpm openclaw gateway probe --url ws://127.0.0.1:999 --timeout 3000

# 6. Unit tests
pnpm test src/gateway/probe.test.ts src/commands/doctor-gateway-health.test.ts src/commands/doctor-gateway-daemon-flow.test.ts

Evidence after fix:

$ pnpm openclaw --version
OpenClaw 2026.6.2 (d553f04)

$ pnpm openclaw gateway probe --url ws://localhost:19999 --timeout 5000
Gateway Status
Reachable: yes
Capability: admin-capable
URL (explicit) ws://localhost:19999
  Connect: ok (60ms) · Capability: admin-capable · Read probe: ok

$ pnpm openclaw gateway call health --url ws://localhost:19998 --token wrongtoken --timeout 5000
GatewayTransportError: gateway closed (1008): unauthorized: gateway *** mismatch

  Gateway logs (server side):
  [ws] unauthorized conn=... reason=***_mismatch
  [ws] closed before connect ... code=1008 reason=connect failed

$ pnpm openclaw gateway probe --url ws://127.0.0.1:999 --timeout 3000
Local loopback ws://127.0.0.1:999
  Connect: failed - connect ECONNREFUSED ... · Capability: unknown

$ pnpm test src/gateway/probe.test.ts ...
 Test Files  4 passed (4)      Tests  112 passed (112)
 Test Files  2 passed (2)      Tests  33 passed (33)

Observed result after fix: Three distinct gateway states now clearly distinguishable:

State Probe output Error message
Healthy Connect: ok (60ms) · Read probe: ok N/A
Reachable but errored (new) N/A (via callGateway) GatewayTransportError: gateway closed (1008): unauthorized...
Unreachable Connect: failed - connect ECONNREFUSED Falls through to non-transport error handling

Before the fix, state 2 would match message.includes("gateway closed") → "Gateway not running". After the fix, isGatewayTransportError(err) && err.kind === "closed" preserves the close code (1008) and reason, enabling accurate tri-state diagnostics in doctor.

The probe's connectLatencyMs is set for pre-hello close when the transport was previously connected, enabling the tri-state downstream.

What was not tested: Protocol version mismatch (requires older gateway binary). Token mismatch and auth rejection were verified via live gateway. The unit-test suite (145 tests) covers the connectLatencyMs set path and doctor rendering logic with proper mocking.

@openclaw-barnacle openclaw-barnacle Bot added gateway Gateway runtime commands Command implementations size: XS proof: supplied External PR includes structured after-fix real behavior proof. labels Jun 17, 2026
@steipete steipete self-assigned this Jun 19, 2026
xialonglee and others added 5 commits June 19, 2026 10:52
…robe diagnostics

When the WebSocket handshake is accepted but closed before hello-ok
(protocol mismatch, auth failure, timeout), the probe left connectLatencyMs
null, making downstream code treat it as unreachable. This caused doctor to
show "Gateway not running" while the port-scan panel correctly identified
the gateway PID — a contradiction reported on both Windows and Linux.

- probe.ts: set connectLatencyMs when onClose fires without connectError
  (WebSocket was accepted), so downstream distinguishes reachable-but-errored
  from truly unreachable (ECONNREFUSED)
- doctor-gateway-health.ts: use isGatewayTransportError to show close reason
  instead of generic "Gateway not running" for accepted-then-closed connections
- doctor-gateway-daemon-flow.ts: same pattern
- doctor-gateway-health.test.ts: mock isGatewayTransportError

Fixes openclaw#79099

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
@steipete
steipete force-pushed the fix/issue-79099-gateway-probe-tristate branch from d553f04 to c7af08b Compare June 19, 2026 10:46
@steipete

Copy link
Copy Markdown
Contributor

Land-ready verification:

  • Rebased onto origin/main at 3091c13713f.
  • pnpm build: passed (Node 24.16.0); full typecheck passed.
  • Focused lint for src/gateway/client.test.ts: passed after replacing direct private socket access.
  • node scripts/run-vitest.mjs src/gateway/client.test.ts src/gateway/probe.test.ts src/commands/doctor-gateway-health.test.ts: 307 tests passed.
  • Fresh Codex autoreview: clean; no accepted/actionable findings.

Known gap: broad local pnpm test was not used as a gate because latest-main macOS filesystem fixtures fail outside this diff. Thanks @xialonglee.

@clawsweeper

clawsweeper Bot commented Jun 19, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed June 19, 2026, 6:54 AM ET / 10:54 UTC.

Summary
The PR adds GatewayClient close-state metadata, uses it to classify accepted-before-hello probe closes as reachable, and renders sanitized typed gateway-close diagnostics in doctor health and daemon flows with focused tests.

PR surface: Source +35, Tests +121. Total +156 across 10 files.

Reproducibility: yes. at source level. Current main settles a pre-hello close before setting connectLatencyMs, so downstream probe and doctor diagnostics can classify an accepted-but-errored gateway as unreachable or not running.

Review metrics: 1 noteworthy metric.

  • Gateway close callback metadata: 2 fields added, 0 removed. The fix depends on adding socketOpened and transportValidated to the GatewayClient close callback shape across the shared package and OpenClaw facade.

Stored data model
Persistent data-model change detected: migration/backfill/repair: src/commands/doctor-gateway-daemon-flow.test.ts, migration/backfill/repair: src/commands/doctor-gateway-health.test.ts, unknown-data-model-change: src/commands/doctor-gateway-health.test.ts. Confirm migration or upgrade compatibility proof before merge.

Root-cause cluster
Relationship: fixed_by_candidate
Canonical: #79099
Summary: This PR is the active fix candidate for the canonical gateway probe/doctor reachable-but-errored misclassification issue.

Members:

Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything.

Merge readiness
Overall: 🐚 platinum hermit
Proof: 🐚 platinum hermit
Patch quality: 🐚 platinum hermit
Result: ready for maintainer review.

Overall follows the weaker of proof and patch quality, so missing proof can cap an otherwise strong patch.

Rank-up moves:

  • [P2] Add a direct openclaw doctor transcript for an accepted-then-closed case if maintainers want exact final-pane proof before merge.

Risk before merge

  • [P2] The live proof covers Linux token-auth rejection and probe/call output, not the original Windows timeout or a full doctor pane; the focused tests cover the doctor rendering paths.

Maintainer options:

  1. Decide the mitigation before merge
    Land this focused PR after normal required checks, keep the linked issue open until merge, then close it as fixed by this PR.
  2. Pause or close
    Do not merge this PR until maintainers decide whether the risk is worth taking.

Next step before merge

  • No ClawSweeper repair lane is needed because this PR is already the active implementation and I found no actionable patch findings.

Security
Cleared: No concrete security or supply-chain regression was found; the patch does not change workflows, dependencies, secrets, auth boundaries, or install scripts, and it sanitizes rendered close text.

Review details

Best possible solution:

Land this focused PR after normal required checks, keep the linked issue open until merge, then close it as fixed by this PR.

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

Yes, at source level. Current main settles a pre-hello close before setting connectLatencyMs, so downstream probe and doctor diagnostics can classify an accepted-but-errored gateway as unreachable or not running.

Is this the best way to solve the issue?

Yes. Carrying validated transport state from GatewayClient into probe and doctor diagnostics is the narrow owner-boundary fix; timeout bumps or port-scan heuristics would not distinguish auth, protocol, and TLS validation cases cleanly.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against 92d1f04de340.

Label changes

Label changes:

  • add P2: This fixes a real gateway diagnostic bug with multiple user reports, but the gateway can still be running and usable in the main repro.
  • add proof: sufficient: Contributor real behavior proof is sufficient. The PR body includes structured terminal output from a real Linux gateway covering healthy, rejected, and unreachable states after the patch, with sensitive token text redacted.
  • add rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🐚 platinum hermit and patch quality is 🐚 platinum hermit.
  • add status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (live_output): The PR body includes structured terminal output from a real Linux gateway covering healthy, rejected, and unreachable states after the patch, with sensitive token text redacted.

Label justifications:

  • P2: This fixes a real gateway diagnostic bug with multiple user reports, but the gateway can still be running and usable in the main repro.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🐚 platinum hermit and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (live_output): The PR body includes structured terminal output from a real Linux gateway covering healthy, rejected, and unreachable states after the patch, with sensitive token text redacted.
  • proof: sufficient: Contributor real behavior proof is sufficient. The PR body includes structured terminal output from a real Linux gateway covering healthy, rejected, and unreachable states after the patch, with sensitive token text redacted.
Evidence reviewed

PR surface:

Source +35, Tests +121. Total +156 across 10 files.

View PR surface stats
Area Files Added Removed Net
Source 6 43 8 +35
Tests 4 128 7 +121
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 10 171 15 +156

What I checked:

  • Current main probe behavior: On current main, probeGateway only sets connectLatencyMs after hello-ok; an onClose before hello settles the failed probe with connectLatencyMs still null. (src/gateway/probe.ts:380, 92d1f04de340)
  • PR head carries transport validation state: The PR head adds socketOpened and transportValidated to GatewayClientCloseInfo, sets transportValidated after the ws open event passes TLS validation, and forwards closeInfo through notifyClose. (packages/gateway-client/src/client.ts:364, c7af08bfd16a)
  • PR head fixes probe classification: probeGateway now sets connectLatencyMs on pre-hello close only when the close info says the transport was validated, preserving unreachable for failed TLS/open paths. (src/gateway/probe.ts:380, c7af08bfd16a)
  • PR head fixes doctor rendering: checkGatewayHealth and the daemon repair flow use formatGatewayClosedDiagnostic for typed closed transport errors, falling back to Gateway not running only when the error is not the typed connected-then-closed case. (src/commands/doctor-gateway-health.ts:129, c7af08bfd16a)
  • Sanitized close diagnostic: formatGatewayClosedDiagnostic only accepts GatewayTransportError kind closed and sanitizes the first message line before rendering terminal output. (src/commands/health-format.ts:9, c7af08bfd16a)
  • Focused regression coverage: The PR adds tests for reachable-but-errored probe latency, failed transport validation staying unreachable, sanitized doctor close output, and TLS fingerprint failure metadata. (src/gateway/probe.test.ts:596, c7af08bfd16a)

Likely related people:

  • steipete: Recent history includes the paired-auth probe fix, event-loop readiness fix, and gateway-client package extraction that define the touched probe/client boundary. (role: recent gateway probe and client contributor; confidence: high; commits: 193c7432e33f, 6bbacd14a366, b1117d98622f; files: src/gateway/probe.ts, packages/gateway-client/src/client.ts, src/gateway/client.ts)
  • obviyus: Authored the probe capability and reachability split that makes connectLatencyMs the key signal for reachable-but-degraded status. (role: probe reachability behavior contributor; confidence: high; commits: 485c258aaf96, 2c5335490121, f070a92e19fb; files: src/gateway/probe.ts, src/commands/gateway-status/helpers.ts)
  • vincentkoc: Current checkout blame is shallow, but live commit history shows recent work in doctor utility structure and GatewayClient lifecycle callback handling near this change. (role: recent doctor and gateway-client area contributor; confidence: medium; commits: 9b8102d7745b, 09427aa76091, 47cad606f4db; files: src/commands/doctor-gateway-health.ts, packages/gateway-client/src/client.ts)
What the crustacean ranks mean
  • 🦀 challenger crab: rare, exceptional readiness with strong proof, clean implementation, and convincing validation.
  • 🦞 diamond lobster: very strong readiness with only minor maintainer review expected.
  • 🐚 platinum hermit: good normal PR, likely mergeable with ordinary maintainer review.
  • 🦐 gold shrimp: useful signal, but proof or patch confidence is still limited.
  • 🦪 silver shellfish: thin signal; proof, validation, or implementation needs work.
  • 🧂 unranked krab: not merge-ready because proof is missing/unusable or there are serious correctness or safety concerns.
  • 🌊 off-meta tidepool: rating does not apply to this item.

Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

How this review workflow works
  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

@clawsweeper clawsweeper Bot added proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. P2 Normal backlog priority with limited blast radius. labels Jun 19, 2026
@steipete
steipete merged commit 6256ad8 into openclaw:main Jun 19, 2026
67 of 75 checks passed
@xialonglee
xialonglee deleted the fix/issue-79099-gateway-probe-tristate branch June 19, 2026 15:44
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Jun 20, 2026
…nclaw#93948)

Distinguish validated gateway reachability from pre-open and TLS-validation failures, and sanitize close diagnostics before terminal output.

Fixes openclaw#79099.

Co-authored-by: xialonglee <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

commands Command implementations gateway Gateway runtime P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. proof: supplied External PR includes structured after-fix real behavior proof. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. size: S status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Windows gateway probe still reports unreachable while gateway health is OK on 2026.5.6

2 participants