shellway 0.6.6 → 0.6.7

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/shellway.js +26 -11
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "shellway",
3
- "version": "0.6.6",
3
+ "version": "0.6.7",
4
4
  "description": "Shellway agent: lets ChatGPT, Claude and Cursor work on this computer through the Shellway relay.",
5
5
  "type": "module",
6
6
  "bin": {
package/shellway.js CHANGED
@@ -7,7 +7,7 @@ import { fileURLToPath } from 'node:url';
7
7
  import { execFileSync, execFile } from 'node:child_process';
8
8
  import { readFile, writeFile, appendFile, readdir, stat, lstat, mkdir, rename, cp, rm } from 'node:fs/promises';
9
9
 
10
- const AGENT_VERSION = '0.6.6'; // keep equal to agent/package.json
10
+ const AGENT_VERSION = '0.6.7'; // keep equal to agent/package.json
11
11
  if (Number(process.versions.node.split('.')[0]) < 22) {
12
12
  console.error(`Shellway needs Node.js 22 or newer (you have ${process.versions.node}). Get it at https://nodejs.org`);
13
13
  process.exit(1);
@@ -890,45 +890,60 @@ function start() {
890
890
  // 0.6.6 (owner PC 16 Nov 21:56 and 23:03): a connect attempt sometimes neither opened nor closed (no socket, no
891
891
  // event), so the agent sat offline forever. Every attempt now has a generation and a deadline: no open within 30 s
892
892
  // -> drop it and start a fresh attempt. Events from an old attempt are ignored.
893
+ // 0.6.7 (17 Nov, customers reconnecting every 1-2 min on some Windows PCs): the link is judged by what the SERVER sends.
894
+ // The relay answers every keepalive ({"type":"ka"}); no message for KA_TIMEOUT means the link is gone. The old guesses
895
+ // (send-buffer size, timer gaps) are only a fallback for an old relay that never answers. After each new connection the
896
+ // agent reports its Node.js version, the reason, and the connection id it left, so the relay drops that one at once.
897
+ const TICK = Number(process.env.SHELLWAY_TICK_MS) || 15_000, KA_TIMEOUT = Number(process.env.SHELLWAY_KA_TIMEOUT_MS) || 75_000;
893
898
  let current = null, lastTick = Date.now(), gen = 0, connectingSince = 0;
894
- const reconnectNow = (why) => {
899
+ let lastMsg = Date.now(), kaSeen = false, cid = null, prevCid = null, why = 'start';
900
+ const reconnectNow = (reason) => {
895
901
  const w = current; if (!w) return;
896
902
  current = null; w.onclose = null; w.onmessage = null;
897
903
  try { w.close(); } catch {}
898
- console.log(`${why} — reconnecting…`);
904
+ console.log(`${reason} — reconnecting…`);
905
+ why = reason; prevCid = cid; cid = null;
899
906
  delay = 1000; setTimeout(connect, 500);
900
907
  };
901
908
  setInterval(() => {
902
- const t = Date.now(), slept = t - lastTick > 45_000; lastTick = t;
909
+ const t = Date.now(), slept = t - lastTick > Math.max(45_000, TICK * 3); lastTick = t;
903
910
  if (connectingSince && t - connectingSince > 30_000) {
904
911
  const w = current; current = null; connectingSince = 0;
905
912
  if (w) { w.onopen = null; w.onclose = null; w.onmessage = null; try { w.close(); } catch {} }
906
913
  console.log('Connecting took too long — retrying…');
914
+ why = 'Connecting took too long';
907
915
  delay = 1000; connect(); return;
908
916
  }
909
917
  const w = current; if (!w || w.readyState !== 1) return;
910
- if (slept) return reconnectNow('Computer woke up');
911
- // 0.6.5: check the buffer BEFORE sending (right after send() it always holds the new frame, so 0.6.4 reconnected every minute)
912
- if (w.bufferedAmount > 0) { w._stuck = (w._stuck || 0) + 1; if (w._stuck >= 4) return reconnectNow('Connection stuck'); } else w._stuck = 0;
918
+ const quiet = t - lastMsg;
919
+ if (slept && quiet > 30_000) return reconnectNow('Computer woke up');
920
+ if (kaSeen) { if (quiet > KA_TIMEOUT) return reconnectNow('No answer from Shellway'); }
921
+ // fallback for a relay without keepalive answers. 0.6.5: check the buffer BEFORE sending (right after send() it always holds the new frame)
922
+ else if (w.bufferedAmount > 0) { w._stuck = (w._stuck || 0) + 1; if (w._stuck >= 8) return reconnectNow('Connection stuck'); } else w._stuck = 0;
913
923
  try { w.send('{"type":"keepalive"}'); } catch { return reconnectNow('Connection lost'); }
914
- }, 15_000);
924
+ }, TICK);
915
925
  const connect = async () => {
916
926
  const my = ++gen; connectingSince = Date.now();
917
927
  if (!(await tokenValid(conf))) { console.error('This device was removed or its token is invalid. Pair again.'); process.exit(78); }
918
928
  if (my !== gen) return; // a newer attempt took over while the check was running
919
929
  const ws = new WebSocket(wsUrl, { headers: { authorization: `Bearer ${conf.device_token}`, 'x-shellway-version': AGENT_VERSION } });
920
930
  current = ws;
921
- ws.onopen = () => { if (my !== gen) { try { ws.close(); } catch {} return; } connectingSince = 0; delay = 1000; console.log(`Connected as "${conf.device_name}"${readonly ? ' (read-only)' : ''}. Leave this running.`); };
931
+ ws.onopen = () => { if (my !== gen) { try { ws.close(); } catch {} return; } connectingSince = 0; delay = 1000; lastMsg = Date.now();
932
+ try { ws.send(JSON.stringify({ type: 'info', v: AGENT_VERSION, node: process.versions.node, os: platform(), reason: why, ...(prevCid ? { replaced: prevCid } : {}) })); } catch {}
933
+ why = 'network'; prevCid = null; console.log(`Connected as "${conf.device_name}"${readonly ? ' (read-only)' : ''}. Leave this running.`); };
922
934
  ws.onmessage = async (ev) => {
935
+ lastMsg = Date.now();
923
936
  let m; try { m = JSON.parse(String(ev.data)); } catch { return; }
924
- if (m.type === 'hello' || !m.id) return;
937
+ if (m.type === 'ka') { kaSeen = true; return; }
938
+ if (m.type === 'hello') { if (typeof m.cid === 'string') cid = m.cid; return; }
939
+ if (!m.id) return;
925
940
  try {
926
941
  ws.send(JSON.stringify({ id: m.id, result: await dispatch(m.method, m.params) }));
927
942
  } catch (e) {
928
943
  ws.send(JSON.stringify({ id: m.id, error: e.message }));
929
944
  }
930
945
  };
931
- ws.onclose = () => { if (my !== gen) return; if (current === ws) current = null; connectingSince = 0; console.log(`Disconnected. Retrying in ${Math.round(delay / 1000)}s…`); setTimeout(connect, delay); delay = Math.min(delay * 2, 30_000); };
946
+ ws.onclose = () => { if (my !== gen) return; if (current === ws) current = null; connectingSince = 0; prevCid = cid; cid = null; console.log(`Disconnected. Retrying in ${Math.round(delay / 1000)}s…`); setTimeout(connect, delay); delay = Math.min(delay * 2, 30_000); };
932
947
  ws.onerror = () => {};
933
948
  };
934
949
  connect();