Skip to content

[Bug]: CLI write commands (message send/broadcast/probe) crash gateway handshake with TypeError reading 'trim' -> closed before connect (1000) #90654

Closed

Description

@EmilioNicolas

Bug type: Bug (CLI write path fails; read path works)

Summary

On 2026.6.1, all CLI write commands fail. openclaw message send, broadcast, and probe crash the gateway's WS message handler during the connect handshake. Read commands (openclaw channels status) work fine. The gateway daemon itself is healthy (Telegram/WhatsApp/webchat all deliver normally).

Steps to reproduce

openclaw message send --channel telegram --account <acct> --target <id> --message "test"

Actual behavior

CLI output:

Bind: loopback); resolved command secrets locally.
gateway connect failed: Error: gateway closed (1000):
GatewayTransportError: gateway closed (1000 normal closure): no close reason
Gateway target: ws://127.0.0.1:18789
Source: local loopback
Bind: loopback

With --json/debug the CLI also prints:

[secrets] message: gateway secrets.resolve unavailable (gateway closed (1000 normal closure): no close reason ...); resolved command secrets locally.

Gateway logs (reproducible 100%, crash happens within ~milliseconds, NOT a timeout):

[gateway] parse/handle error: TypeError: Cannot read properties of undefined (reading 'trim')
[ws] ✗ parse-error error=TypeError: Cannot read properties of undefined (reading 'trim') conn=<id>
[ws] closed before connect conn=<id> peer=127.0.0.1:...->127.0.0.1:18789 ... code=1000 reason=n/a

Root cause (analysis)

  • Write commands resolve SecretRefs via a secrets.resolve call against the gateway before sending (see dist/command-secret-gateway-*.js). Read commands do not, so they never hit the failing path.
  • The gateway's connect/message handler that prints parse/handle error lives at dist/message-handler-Du1uvc4A.js:1815. A .trim() is invoked on a field that arrives undefined while parsing the connect frame from the write path. Sibling fields (nonce, instanceId) are guarded with typeof === "string" but this one is not, so the handler throws and the socket is closed with code 1000 before connect completes.
  • The CLI then falls back to resolved command secrets locally, but without resolved secrets the send cannot complete.

Not the same as #47103

#47103 (gateway closed (1000)) had the same visible symptom but root cause = handshake timeout (fix = restore 10s timeout). This is different. Verified empirically: raising OPENCLAW_HANDSHAKE_TIMEOUT_MS=30000 and OPENCLAW_CONNECT_CHALLENGE_TIMEOUT_MS=30000 does NOT help — the crash still occurs in milliseconds with the same .trim() TypeError. This is an unguarded .trim() on an undefined connect-frame field, not a timing issue.

Environment

  • OpenClaw version: 2026.6.1 (latest on npm at time of report)
  • OS: Debian (GCP VM), Node v24.15.0
  • Install: npm global
  • Gateway bind: loopback, gateway.auth.mode: token
  • Reproduces with token present in env (OPENCLAW_GATEWAY_TOKEN, 64 chars) — not a missing-token issue.

Impact

All CLI-driven message sending is broken. Workaround: use native gateway delivery (session replies, cron delivery: announce) instead of the CLI. Read-only CLI commands are unaffected.

Suggested fix

Guard the .trim() at message-handler-Du1uvc4A.js:1815 (and the secrets.resolve connect path) with typeof === "string" like the sibling nonce/instanceId reads, and/or validate the connect frame field before trimming so a malformed/absent field returns a proper handshake error response instead of throwing inside the parse handler.

Activity

  1. clawsweeper commented on Jun 5, 2026

    @clawsweeper
    Contributor

    Codex review: keeping this open for maintainer follow-up; there is still a little grit to resolve. Reviewed June 15, 2026, 8:39 AM ET / 12:39 UTC.

    Summary
    Current main still contains the unguarded gateway handshake trim path, and the issue is already paired with an open, cleanly mergeable, proof-positive closing PR, so it should remain open until that PR merges or is rejected.

    Reproducibility: yes. Current-main source still contains the unguarded trim paths, and the linked PR documents a real gateway WebSocket reproduction using malformed persisted pairing state.

    Next step
    Do not queue duplicate repair work because #92178 is already open, cleanly mergeable, and proof-positive.

    Review details

    Best possible solution:

    Land #92178 after maintainer review, then close this issue with merge and release provenance.

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

    Yes. Current-main source still contains the unguarded trim paths, and the linked PR documents a real gateway WebSocket reproduction using malformed persisted pairing state.

    Is this the best way to solve the issue?

    Yes. The open linked PR fixes the runtime boundary in both the audit formatter and pairing merge helpers; earlier secrets.resolve-only attempts targeted the wrong layer.

    AGENTS.md: found and applied where relevant.

    Remaining risk / open question:

    • Closing now would detach a high-confidence CLI message-delivery regression from its still-unmerged canonical fix PR.

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

    Label changes

    Label changes:

    • add issue-rating: 🦞 diamond lobster: Current issue advisory state selects this label.
    • add clawsweeper:source-repro: Current issue advisory state selects this label.
    • remove clawsweeper:current-main-repro: Current issue advisory state no longer selects this label.
    • remove issue-rating: 🦀 challenger crab: Current issue advisory state no longer selects this label.

    Label justifications:

    • P1: The issue blocks established CLI send, broadcast, and probe workflows for affected users.
    • impact:message-loss: The gateway closes during connection setup before requested CLI outbound messages can be delivered.
    Evidence reviewed

    What I checked:

    Likely related people:

    • steipete: History shows Peter Steinberger is the dominant contributor in the gateway/pairing files, introduced explicit device access-upgrade approval behavior, and later hardened pairing scope approval. (role: feature owner; confidence: high; commits: 0bda0202fd12, 9292ec9880fd, b660493e5466; files: src/gateway/server/ws-connection/message-handler.ts, src/infra/device-pairing.ts)
    • joshavant: Josh Avant previously touched the same WebSocket upgrade/pairing compatibility area and was explicitly routed on the issue timeline. (role: adjacent owner; confidence: medium; commits: 29ad0736f445; files: src/gateway/server/ws-connection/message-handler.ts)
    • wangmiao0668000666: This contributor authored the open proof-positive fix PR that currently owns the implementation path for this issue. (role: active fix contributor; confidence: medium; commits: 20ca32e10cf9, 3c0adde3f2ec, 609c116cdfdd; files: src/gateway/server/ws-connection/message-handler.ts, src/infra/device-pairing.ts)
    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.
  2. added
    P1High-priority user-facing bug, regression, or broken workflow.
    impact:crash-loopCrash, hang, restart loop, or process-level availability failure.
    impact:message-lossChannel message delivery can be lost, duplicated, or misrouted.
    issue-rating: 🐚 platinum hermitGood issue quality with a plausible reproduction path needing some confirmation.
    clawsweeper:needs-live-reproClawSweeper needs live local, crabbox, or manual validation to confirm this issue.
    on Jun 5, 2026
  3. added a commit that references this issue on Jun 9, 2026
    f9fb5a9
  4. wangmiao0668000666 commented on Jun 11, 2026

    @wangmiao0668000666
    Contributor

    I'd like to work on this. The fix is straightforward: add a typeof item === "string" guard before calling .trim() in the gateway handshake audit formatter, matching the pattern already used for sibling fields like nonce/instanceId. I'll also add a regression test.

  5. added
    issue-rating: 🦀 challenger crabExceptional issue quality: high-confidence current-main reproduction and actionable evidence.
    clawsweeper:current-main-reproClawSweeper found a high-confidence current-main issue reproduction.
    and removed
    clawsweeper:needs-live-reproClawSweeper needs live local, crabbox, or manual validation to confirm this issue.
    issue-rating: 🐚 platinum hermitGood issue quality with a plausible reproduction path needing some confirmation.
    impact:crash-loopCrash, hang, restart loop, or process-level availability failure.
    on Jun 12, 2026
  6. 11 remaining items

  7. locked as resolved and limited conversation to collaborators on Jun 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P1High-priority user-facing bug, regression, or broken workflow.clawsweeper:linked-pr-openClawSweeper found an open linked pull request for this issue.clawsweeper:no-new-fix-prClawSweeper does not recommend queueing a new automated fix PR for this issue.clawsweeper:source-reproClawSweeper found a high-confidence source-level issue reproduction.impact:message-lossChannel message delivery can be lost, duplicated, or misrouted.issue-rating: 🦞 diamond lobsterVery strong issue quality with high-confidence source-level or clear reproduction.

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions