shellway 0.6.3 → 0.6.5

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 +23 -2
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "shellway",
3
- "version": "0.6.3",
3
+ "version": "0.6.5",
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.3'; // keep equal to agent/package.json
10
+ const AGENT_VERSION = '0.6.5'; // 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);
@@ -882,9 +882,30 @@ function start() {
882
882
  setInterval(keepAwakeTick, 60_000);
883
883
  const wsUrl = conf.server.replace(/^http/, 'ws') + '/agent';
884
884
  let delay = 1000;
885
+ // 0.6.4 (owner PC 15 Nov 09:13): after sleep or a network change the socket can stay "open" on this side while the
886
+ // server has already dropped it (heartbeat timeout) — the agent then never reconnects. Watchdog: a tiny keepalive
887
+ // every 15 s (the relay ignores messages without an id) makes a dead connection fail fast, a clock jump means the
888
+ // computer slept, and data stuck in the send buffer for ~1 minute means the link is gone. Any of these: reconnect now.
889
+ let current = null, lastTick = Date.now();
890
+ const reconnectNow = (why) => {
891
+ const w = current; if (!w) return;
892
+ current = null; w.onclose = null; w.onmessage = null;
893
+ try { w.close(); } catch {}
894
+ console.log(`${why} — reconnecting…`);
895
+ delay = 1000; setTimeout(connect, 500);
896
+ };
897
+ setInterval(() => {
898
+ const t = Date.now(), slept = t - lastTick > 45_000; lastTick = t;
899
+ const w = current; if (!w || w.readyState !== 1) return;
900
+ if (slept) return reconnectNow('Computer woke up');
901
+ // 0.6.5: check the buffer BEFORE sending (right after send() it always holds the new frame, so 0.6.4 reconnected every minute)
902
+ if (w.bufferedAmount > 0) { w._stuck = (w._stuck || 0) + 1; if (w._stuck >= 4) return reconnectNow('Connection stuck'); } else w._stuck = 0;
903
+ try { w.send('{"type":"keepalive"}'); } catch { return reconnectNow('Connection lost'); }
904
+ }, 15_000);
885
905
  const connect = async () => {
886
906
  if (!(await tokenValid(conf))) { console.error('This device was removed or its token is invalid. Pair again.'); process.exit(78); }
887
907
  const ws = new WebSocket(wsUrl, { headers: { authorization: `Bearer ${conf.device_token}`, 'x-shellway-version': AGENT_VERSION } });
908
+ current = ws;
888
909
  ws.onopen = () => { delay = 1000; console.log(`Connected as "${conf.device_name}"${readonly ? ' (read-only)' : ''}. Leave this running.`); };
889
910
  ws.onmessage = async (ev) => {
890
911
  let m; try { m = JSON.parse(String(ev.data)); } catch { return; }
@@ -895,7 +916,7 @@ function start() {
895
916
  ws.send(JSON.stringify({ id: m.id, error: e.message }));
896
917
  }
897
918
  };
898
- ws.onclose = () => { console.log(`Disconnected. Retrying in ${Math.round(delay / 1000)}s…`); setTimeout(connect, delay); delay = Math.min(delay * 2, 30_000); };
919
+ ws.onclose = () => { if (current === ws) current = null; console.log(`Disconnected. Retrying in ${Math.round(delay / 1000)}s…`); setTimeout(connect, delay); delay = Math.min(delay * 2, 30_000); };
899
920
  ws.onerror = () => {};
900
921
  };
901
922
  connect();