11import { createHash , randomUUID } from "node:crypto" ;
2+ import { gatewayLog } from "./gateway-logger.js" ;
23import { io , type Socket } from "socket.io-client" ;
34import {
45 PROTOCOL_VERSION ,
@@ -94,6 +95,7 @@ export class CloudSocketService {
9495 state : DesktopPresenceEvent [ "state" ] ;
9596 }
9697 ) : void {
98+ gatewayLog . debug ( "cloud-socket" , `Sending presence: state=${ event . state } ` ) ;
9799 this . emit ( "desktop.presence" , event ) ;
98100 }
99101
@@ -129,6 +131,7 @@ export class CloudSocketService {
129131 if ( this . stopped ) {
130132 return ;
131133 }
134+ gatewayLog . info ( "cloud-socket" , "Connected to relay, sending hello handshake" ) ;
132135 this . awaitingHelloAck = true ;
133136 this . emitHello ( ) ;
134137 this . scheduleHelloAckTimeout ( ) ;
@@ -142,11 +145,13 @@ export class CloudSocketService {
142145 this . clearHelloAckTimer ( ) ;
143146 const message = error instanceof Error ? error . message : "connection failed" ;
144147 if ( looksLikeAuthError ( error ) ) {
148+ gatewayLog . error ( "cloud-socket" , "Authentication failed on connect" ) ;
145149 this . notifyStatus ( {
146150 state : "degraded" ,
147151 error : "Authentication failed — verify your API key in Settings"
148152 } ) ;
149153 } else {
154+ gatewayLog . error ( "cloud-socket" , `Connection error: ${ message } ` ) ;
150155 this . notifyStatus ( { state : "degraded" , error : `Cloud socket connection failed: ${ message } ` } ) ;
151156 }
152157 } ) ;
@@ -155,6 +160,7 @@ export class CloudSocketService {
155160 if ( this . stopped ) {
156161 return ;
157162 }
163+ gatewayLog . warn ( "cloud-socket" , `Disconnected: ${ reason } ` ) ;
158164 this . awaitingHelloAck = false ;
159165 this . clearHelloAckTimer ( ) ;
160166 this . notifyStatus ( { state : "degraded" , error : `Cloud socket disconnected: ${ reason } ` } ) ;
@@ -164,12 +170,14 @@ export class CloudSocketService {
164170 const event = asObject ( payload ) ;
165171 const computeTargetId = asNonEmptyString ( event . computeTargetId ) ;
166172 if ( ! computeTargetId ) {
173+ gatewayLog . warn ( "cloud-socket" , "hello.ack missing computeTargetId, ignoring" ) ;
167174 return ;
168175 }
169176
170177 this . targetId = computeTargetId ;
171178 this . awaitingHelloAck = false ;
172179 this . clearHelloAckTimer ( ) ;
180+ gatewayLog . info ( "cloud-socket" , `Hello ack received, targetId=${ computeTargetId } ` ) ;
173181 const ackEvent : DesktopHelloAckEvent = {
174182 ...createEnvelope ( ) ,
175183 computeTargetId,
@@ -190,8 +198,10 @@ export class CloudSocketService {
190198 socket . on ( "desktop.command" , ( payload : unknown ) => {
191199 const parsed = parseDesktopCommand ( payload ) ;
192200 if ( ! parsed ) {
201+ gatewayLog . warn ( "cloud-socket" , "Received unparseable desktop.command, ignoring" ) ;
193202 return ;
194203 }
204+ gatewayLog . debug ( "cloud-socket" , `Command received: ${ parsed . operationId } ${ parsed . method } ${ parsed . path } (commandId=${ parsed . commandId } )` ) ;
195205 this . options . onCommand ?.( parsed ) ;
196206 } ) ;
197207
@@ -267,6 +277,7 @@ export class CloudSocketService {
267277 if ( this . stopped || ! this . awaitingHelloAck ) {
268278 return ;
269279 }
280+ gatewayLog . warn ( "cloud-socket" , "Hello ack timeout -- retrying handshake" ) ;
270281 this . notifyStatus ( {
271282 state : "degraded" ,
272283 error : "Connected to cloud socket but did not receive desktop.hello.ack"
0 commit comments