echoclaw-relay-agent 0.22.3 → 0.22.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -157,6 +157,8 @@ export class RelayAgent extends EventEmitter {
157
157
  this.transport = null;
158
158
  }
159
159
  this.frameCrypto = null;
160
+ this._dataHandler = null;
161
+ this._dataHandlerInner = null;
160
162
  try {
161
163
  if (pairingCode) {
162
164
  // New pairing — overwrite any existing session
@@ -173,6 +175,21 @@ export class RelayAgent extends EventEmitter {
173
175
  }
174
176
  }
175
177
  }
178
+ catch (err) {
179
+ // Clean up zombie transport on failure (prevents auto-reconnect loop)
180
+ const t = this.transport;
181
+ if (t) {
182
+ t.removeAllListeners();
183
+ t.disconnect();
184
+ this.transport = null;
185
+ }
186
+ this.frameCrypto = null;
187
+ this.sessionKey = null;
188
+ this._dataHandler = null;
189
+ this._dataHandlerInner = null;
190
+ this.setStatus('disconnected');
191
+ throw err;
192
+ }
176
193
  finally {
177
194
  this._starting = false;
178
195
  }
@@ -236,7 +253,7 @@ export class RelayAgent extends EventEmitter {
236
253
  async freshPairing(code) {
237
254
  this.setStatus('connecting');
238
255
  // Connect to relay server as agent
239
- const agentUrl = `${this.config.relayServer}/agent/connect?code=${code}`;
256
+ const agentUrl = `${this.config.relayServer}/agent/connect?code=${code}&protocol=2`;
240
257
  this.transport = new RelayTransport({
241
258
  url: agentUrl,
242
259
  reconnect: this.config.reconnect,
@@ -281,7 +298,7 @@ export class RelayAgent extends EventEmitter {
281
298
  this.frameCrypto = new FrameCrypto(this.sessionKey);
282
299
  // _paired stays false until HELLO confirmed — prevents on('open') from
283
300
  // emitting 'connected' before server confirms the session exists.
284
- const resumeUrl = `${this.config.relayServer}/agent/connect?resume=${session.relaySessionId}`;
301
+ const resumeUrl = `${this.config.relayServer}/agent/connect?resume=${session.relaySessionId}&protocol=2`;
285
302
  this.transport = new RelayTransport({
286
303
  url: resumeUrl,
287
304
  reconnect: this.config.reconnect,
@@ -318,7 +335,7 @@ export class RelayAgent extends EventEmitter {
318
335
  }
319
336
  else if (msg.type === 'CLOSE' && msg.sender_role === 'server') {
320
337
  const payload = msg.payload || 'unknown';
321
- if (['SESSION_NOT_FOUND', 'SESSION_EXPIRED', 'INVALID_SESSION', 'DEVICE_TOKEN_MISMATCH'].includes(payload)) {
338
+ if (['SESSION_NOT_FOUND', 'SESSION_EXPIRED', 'INVALID_SESSION', 'DEVICE_TOKEN_MISMATCH', 'SESSION_PROTOCOL_MISMATCH'].includes(payload)) {
322
339
  this.sessionStore.clear().catch(() => { });
323
340
  }
324
341
  settle(() => reject(new Error(`Relay server closed: ${payload}`)));
@@ -356,7 +373,7 @@ export class RelayAgent extends EventEmitter {
356
373
  this._paired = true;
357
374
  // Update transport URL so auto-reconnect uses ?resume=sessionId
358
375
  if (this.transport) {
359
- this.transport.setUrl(`${this.config.relayServer}/agent/connect?resume=${result.sessionId}`);
376
+ this.transport.setUrl(`${this.config.relayServer}/agent/connect?resume=${result.sessionId}&protocol=2`);
360
377
  }
361
378
  // Build session data with sessionKey for persistence
362
379
  const sessionData = {
@@ -452,6 +469,28 @@ export class RelayAgent extends EventEmitter {
452
469
  this.transport.on('connect_timeout', (timeoutMs) => {
453
470
  this.emit('error', Object.assign(new Error(`Connection timed out after ${timeoutMs / 1000}s`), { code: 'CONNECT_TIMEOUT' }));
454
471
  });
472
+ // Handle server fatal CLOSE messages (SESSION_NOT_FOUND, SESSION_PROTOCOL_MISMATCH, etc.)
473
+ const SESSION_FATAL = new Set(['SESSION_NOT_FOUND', 'INVALID_SESSION', 'SESSION_PROTOCOL_MISMATCH']);
474
+ this.transport.on('message', (msg) => {
475
+ if ((msg.type === 'CLOSE' || msg.type === 'STATUS') && msg.sender_role === 'server') {
476
+ const payload = typeof msg.payload === 'string' ? msg.payload : '';
477
+ if (payload === 'UNPAIRED' || SESSION_FATAL.has(payload)) {
478
+ this.sessionStore.clear().catch(() => { });
479
+ this._paired = false;
480
+ this._stopped = true;
481
+ this.gatewayManager?.stop();
482
+ this.transport?.disconnect();
483
+ this.transport = null;
484
+ this.sessionKey = null;
485
+ this.frameCrypto = null;
486
+ this._dataHandler = null;
487
+ this._dataHandlerInner = null;
488
+ this.setStatus('disconnected');
489
+ this.emit(payload === 'UNPAIRED' ? 'unpaired' : 'session_fatal', { reason: payload });
490
+ return;
491
+ }
492
+ }
493
+ });
455
494
  this.transport.on('open', () => {
456
495
  if (!this.transport || !this._paired || !this.sessionKey)
457
496
  return;
@@ -126,6 +126,7 @@ export class RelayClient extends EventEmitter {
126
126
  this._stopped = false;
127
127
  // Teardown previous transport before starting new
128
128
  if (this.transport) {
129
+ this.transport.removeAllListeners();
129
130
  this.transport.disconnect();
130
131
  this.transport = null;
131
132
  }
@@ -139,6 +140,19 @@ export class RelayClient extends EventEmitter {
139
140
  await this.freshPairing();
140
141
  }
141
142
  }
143
+ catch (err) {
144
+ // Clean up zombie transport on failure (prevents auto-reconnect loop)
145
+ const t = this.transport;
146
+ if (t) {
147
+ t.removeAllListeners();
148
+ t.disconnect();
149
+ this.transport = null;
150
+ }
151
+ this.frameCrypto = null;
152
+ this.sessionKey = null;
153
+ this.setStatus('disconnected');
154
+ throw err;
155
+ }
142
156
  finally {
143
157
  this._connecting = false;
144
158
  }
@@ -200,7 +214,7 @@ export class RelayClient extends EventEmitter {
200
214
  }
201
215
  async freshPairing() {
202
216
  this.setStatus('connecting');
203
- const clientUrl = `${this.config.relayServer}/client/connect`;
217
+ const clientUrl = `${this.config.relayServer}/client/connect?protocol=2`;
204
218
  this.transport = new RelayTransport({
205
219
  url: clientUrl,
206
220
  reconnect: this.config.reconnect,
@@ -244,7 +258,7 @@ export class RelayClient extends EventEmitter {
244
258
  this.frameCrypto = new FrameCrypto(this.sessionKey);
245
259
  // _paired stays false until HELLO confirmed — prevents on('open') from
246
260
  // emitting 'connected' before server confirms the session exists.
247
- const resumeUrl = `${this.config.relayServer}/client/connect?resume=${session.relaySessionId}`;
261
+ const resumeUrl = `${this.config.relayServer}/client/connect?resume=${session.relaySessionId}&protocol=2`;
248
262
  this.transport = new RelayTransport({
249
263
  url: resumeUrl,
250
264
  reconnect: this.config.reconnect,
@@ -281,7 +295,7 @@ export class RelayClient extends EventEmitter {
281
295
  }
282
296
  else if (msg.type === 'CLOSE' && msg.sender_role === 'server') {
283
297
  const payload = msg.payload || 'unknown';
284
- if (['SESSION_NOT_FOUND', 'SESSION_EXPIRED', 'INVALID_SESSION', 'DEVICE_TOKEN_MISMATCH'].includes(payload)) {
298
+ if (['SESSION_NOT_FOUND', 'SESSION_EXPIRED', 'INVALID_SESSION', 'DEVICE_TOKEN_MISMATCH', 'SESSION_PROTOCOL_MISMATCH'].includes(payload)) {
285
299
  this.sessionStore.clear().catch(() => { });
286
300
  }
287
301
  settle(() => reject(new Error(`Relay server closed: ${payload}`)));
@@ -308,7 +322,7 @@ export class RelayClient extends EventEmitter {
308
322
  this._paired = true;
309
323
  // Update transport URL so auto-reconnect uses ?resume=sessionId
310
324
  if (this.transport) {
311
- this.transport.setUrl(`${this.config.relayServer}/client/connect?resume=${result.sessionId}`);
325
+ this.transport.setUrl(`${this.config.relayServer}/client/connect?resume=${result.sessionId}&protocol=2`);
312
326
  }
313
327
  // Persist session with sessionKey for future resume
314
328
  const sessionData = {
@@ -381,7 +395,7 @@ export class RelayClient extends EventEmitter {
381
395
  });
382
396
  // Handle server messages (CLOSE/STATUS)
383
397
  const PEER_STATUS = new Set(['AGENT_WARNING', 'AGENT_ONLINE', 'DESKTOP_DISCONNECTED']);
384
- const SESSION_FATAL = new Set(['SESSION_NOT_FOUND', 'INVALID_SESSION']);
398
+ const SESSION_FATAL = new Set(['SESSION_NOT_FOUND', 'INVALID_SESSION', 'SESSION_PROTOCOL_MISMATCH']);
385
399
  this.transport.on('message', (msg) => {
386
400
  if ((msg.type === 'CLOSE' || msg.type === 'STATUS') && msg.sender_role === 'server') {
387
401
  const payload = typeof msg.payload === 'string' ? msg.payload : '';
@@ -394,12 +408,26 @@ export class RelayClient extends EventEmitter {
394
408
  }
395
409
  if (payload === 'UNPAIRED') {
396
410
  this.sessionStore.clear().catch(() => { });
397
- this.emit('unpaired');
411
+ this._paired = false;
412
+ this._stopped = true;
398
413
  this.transport?.disconnect();
414
+ this.transport = null;
415
+ this.sessionKey = null;
416
+ this.frameCrypto = null;
417
+ this.setStatus('disconnected');
418
+ this.emit('unpaired');
399
419
  return;
400
420
  }
401
421
  if (SESSION_FATAL.has(payload)) {
402
422
  this.sessionStore.clear().catch(() => { });
423
+ this._paired = false;
424
+ this._stopped = true;
425
+ this.transport?.disconnect();
426
+ this.transport = null;
427
+ this.sessionKey = null;
428
+ this.frameCrypto = null;
429
+ this.setStatus('disconnected');
430
+ this.emit('session_fatal', { reason: payload });
403
431
  return;
404
432
  }
405
433
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "echoclaw-relay-agent",
3
- "version": "0.22.3",
3
+ "version": "0.22.4",
4
4
  "description": "EchoClaw Relay Connection — E2E encrypted relay transport, pairing, and session management",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",