chenhg5/cc-connect · error

wps-agentspace: init: %w

Error message

wps-agentspace: init: %w

What it means

The wps-agentspace platform wraps any failure to send the WebSocket "init" handshake frame after a successful dial as "wps-agentspace: init: %w". sendInit writes a JSON init frame carrying the device UUID/name and timestamp; if that write fails (connection already closed, send deadline exceeded, marshal error), connect aborts and the connectLoop retries with backoff. It means the TCP/TLS handshake succeeded but the application-level init exchange did not.

Source

Thrown at platform/wps-agentspace/wpsagentspace.go:390

	})

	p.mu.Lock()
	p.conn = conn
	p.mu.Unlock()

	// Reset backoff on successful connection
	defer func() {
		p.mu.Lock()
		if p.conn == conn {
			p.conn = nil
		}
		p.mu.Unlock()
		_ = conn.Close()
	}()

	// Send init
	if err := p.sendInit(); err != nil {
		return fmt.Errorf("wps-agentspace: init: %w", err)
	}

	// Start heartbeat
	hbCtx, hbCancel := context.WithCancel(ctx)
	defer hbCancel()
	go p.heartbeatLoop(hbCtx)

	// Start write loop
	writeDone := make(chan struct{})
	go func() {
		defer close(writeDone)
		p.writeLoop(conn)
	}()

	// Read loop
	return p.readLoop(conn, ctx)
}

View on GitHub (pinned to 4000b2338a)

Solutions

  1. Check the wrapped cause (%w) in logs — if it is 'use of closed connection' or a write timeout, the server/proxy dropped the socket; retry and inspect server-side access logs
  2. Verify the configured appID/baseURL so you are dialing the correct AgentSpace endpoint that accepts init frames
  3. Confirm outbound WebSocket (wss) connectivity to agentspace.wps.cn from the host, including proxies and firewalls
  4. If init failures persist, check device registration/permissions for the configured device UUID on the WPS side

Example fix

// before
if err := p.sendInit(); err != nil {
	return fmt.Errorf("wps-agentspace: init: %w", err)
}
// after
if err := p.sendInit(); err != nil {
	slog.Warn("wps-agentspace: init send failed, will retry", "error", err)
	return fmt.Errorf("wps-agentspace: init: %w", err) // connectLoop backoff retries
}
Defensive patterns

Strategy: retry

Validate before calling

// before starting: verify endpoint reachability
resp, err := http.Head("https://agentspace.wps.cn")
if err != nil || resp.StatusCode >= 500 { log.Printf("endpoint unreachable: %v", err) }

Try / catch

if err := p.sendInit(); err != nil {
	var ne net.Error
	if errors.As(err, &ne) && ne.Timeout() {
		// transient: rely on connectLoop backoff
	}
	return fmt.Errorf("wps-agentspace: init: %w", err)
}

Prevention

When it happens

Trigger: The server closes the connection immediately after dialing (rejection, auth wall, proxy interference); the underlying gorilla/websocket write fails due to a broken connection; or writeJSON's json.Marshal of initData fails (not possible with current fields, but wrapped the same way).

Common situations: WPS AgentSpace rejecting a device before init completes; corporate proxy/VPN dropping freshly established WebSocket connections; server restart racing with connectLoop so the conn is closed between dial and init; network flaps right after connect.

Related errors


AI-assisted analysis of chenhg5/cc-connect@4000b2338a (2026-09-06). Data as JSON: /api/errors/c667dc3bf8503f3d. Report an issue: GitHub.