cli-setu 0.3.7 → 0.3.8
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.
- package/package.json +1 -1
- package/src/pair.js +7 -3
- package/src/supervisor.js +32 -0
- package/src/unpairedDetector.js +37 -0
package/package.json
CHANGED
package/src/pair.js
CHANGED
|
@@ -92,8 +92,12 @@ async function pair(authServer, authServerPublic) {
|
|
|
92
92
|
|
|
93
93
|
console.log('');
|
|
94
94
|
qrcode.generate(scanUrl, { small: true });
|
|
95
|
-
|
|
96
|
-
|
|
95
|
+
// 2026-11-16: this used to say "Scan this with your phone camera". A QR scanned with the phone's CAMERA app opens
|
|
96
|
+
// a web page, which registers an encryption key that lives in the browser: the app never gets the matching key,
|
|
97
|
+
// shows "The encryption key for this laptop does not match" and a blank terminal (found on a Linux test). The
|
|
98
|
+
// reliable ways are the app's own Pair button (scan or typed code).
|
|
99
|
+
console.log('[cli-setu] Scan this QR code with the Pair button INSIDE the CLI Setu app (not the phone\'s camera app).');
|
|
100
|
+
console.log(`[cli-setu] (Testing on one PC without a phone? Paste this URL into a browser tab instead: ${scanUrl})`);
|
|
97
101
|
|
|
98
102
|
const codeRes = await fetch(`${authServer}/device/pair/start`, {
|
|
99
103
|
method: 'POST',
|
|
@@ -101,7 +105,7 @@ async function pair(authServer, authServerPublic) {
|
|
|
101
105
|
body: JSON.stringify({ deviceName: config.deviceName, devicePublicKey: config.devicePublicKey }),
|
|
102
106
|
});
|
|
103
107
|
const { code: typedCode, pairId: typedPairId } = await codeRes.json();
|
|
104
|
-
console.log(`[cli-setu] Or
|
|
108
|
+
console.log(`[cli-setu] Or, most reliable: in the CLI Setu app tap Pair, choose "Enter code" and type this code: ${typedCode}`);
|
|
105
109
|
console.log('[cli-setu] Waiting for scan or code entry...');
|
|
106
110
|
|
|
107
111
|
let confirmed = false;
|
package/src/supervisor.js
CHANGED
|
@@ -48,6 +48,7 @@ const { attachStdinForwarder } = require('./stdinForwarder');
|
|
|
48
48
|
const { adoptPhoneKey } = require('./sharedKey');
|
|
49
49
|
const { approvalFingerprint, createApprovalDeduper, relayWithRetry } = require('./approvalRelay');
|
|
50
50
|
const { newSessionId, sessionPrefix, endSessionOnBackend, isSessionEndedResponse } = require('./sessionLifecycle');
|
|
51
|
+
const { createUnpairedDetector } = require('./unpairedDetector');
|
|
51
52
|
|
|
52
53
|
// Real bug found by testing: this script's own console.log/console.error
|
|
53
54
|
// calls write to the SAME process.stdout the PTY's raw output is echoed
|
|
@@ -373,7 +374,36 @@ async function pollFocus() {
|
|
|
373
374
|
}
|
|
374
375
|
}
|
|
375
376
|
|
|
377
|
+
// 2026-11-16 (real bug, found on a Linux/WSL test): a session that was already running when its device was unpaired
|
|
378
|
+
// (or paired again) kept sending output, got HTTP 401 for every piece and only logged it: the phone showed a blank
|
|
379
|
+
// terminal and the laptop never said why. Three 401s in a row from the output relay now stop the session with a
|
|
380
|
+
// clear message, the same way /device/connect already does for a 401. See unpairedDetector.js.
|
|
381
|
+
function stopBecauseUnpaired() {
|
|
382
|
+
diag.say('[supervisor] This device is no longer paired (it was unpaired, or paired again, from the app or another terminal), so your phone cannot see this session.', { error: true, showAtExit: true });
|
|
383
|
+
diag.say('[supervisor] This session is stopping. Pair again with `cli-setu pair`, then start a new session with `cli-setu session`.', { error: true, showAtExit: true });
|
|
384
|
+
log('[supervisor] The relay rejected this device token (HTTP 401) several times in a row: the device is no longer paired. Stopping this session.');
|
|
385
|
+
process.exitCode = 1;
|
|
386
|
+
if (ptyProcess) { ptyProcess.kill(); } else { process.exit(1); return; }
|
|
387
|
+
setTimeout(() => process.exit(1), 1500).unref();
|
|
388
|
+
}
|
|
389
|
+
const unpairedDetector = createUnpairedDetector({ onUnpaired: stopBecauseUnpaired });
|
|
390
|
+
|
|
391
|
+
// 2026-11-16 (real bug, found on a Linux/WSL test): when the very first /device/connect call could not even reach
|
|
392
|
+
// the server (connection timed out, DNS failure, refused), fetch() THROWS instead of returning a failed response.
|
|
393
|
+
// Only a failed RESPONSE was handled (retry with backoff), so a thrown network error became an unhandled promise
|
|
394
|
+
// rejection and crashed the whole session with a raw stack trace ("TypeError: fetch failed"). Every call now goes
|
|
395
|
+
// through this wrapper: any failure is logged and retried with the same backoff as a failed response.
|
|
376
396
|
async function connectToAzure() {
|
|
397
|
+
try {
|
|
398
|
+
await connectToAzureOnce();
|
|
399
|
+
} catch (err) {
|
|
400
|
+
const why = (err && err.cause && (err.cause.code || err.cause.message)) || (err && err.message) || String(err);
|
|
401
|
+
log(`[supervisor] /device/connect could not complete (${why}); will retry.`);
|
|
402
|
+
reconnectManager.scheduleRetry();
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
async function connectToAzureOnce() {
|
|
377
407
|
// cliType lets Devices/Profile show which CLI this paired laptop is
|
|
378
408
|
// actually running (see auth-server.js's /device/connect handler and
|
|
379
409
|
// db.js's devices.cli_type column) - sent on every connect (not just
|
|
@@ -743,6 +773,7 @@ async function sendSnapshot() {
|
|
|
743
773
|
});
|
|
744
774
|
if (!res.ok) {
|
|
745
775
|
log(`[supervisor] Snapshot part ${i + 1}/${parts.length} was refused by the backend (HTTP ${res.status}); stopping.`);
|
|
776
|
+
unpairedDetector.note(res.status);
|
|
746
777
|
return;
|
|
747
778
|
}
|
|
748
779
|
}
|
|
@@ -865,6 +896,7 @@ function flushOutputBuffer() {
|
|
|
865
896
|
body: JSON.stringify({ sessionId: SESSION_ID, seq: ++outputSeq, data, encrypted: true }),
|
|
866
897
|
});
|
|
867
898
|
if (!res.ok) log(`[supervisor] Output piece seq=${outputSeq} was refused by the backend (HTTP ${res.status}, ${data.length} chars).`);
|
|
899
|
+
unpairedDetector.note(res.status);
|
|
868
900
|
} catch (err) {
|
|
869
901
|
// A dropped terminal.output batch isn't fatal the way a dropped
|
|
870
902
|
// confirmation would be - the next batch (or a fresh session.attach
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// Notices that this device is no longer paired (found 2026-11-16 on a Linux test).
|
|
2
|
+
// After `cli-setu unpair`, or after the same device is paired again, the server rejects the old device token with
|
|
3
|
+
// HTTP 401. /device/connect already stopped the session on a 401, but a session that was ALREADY running kept sending
|
|
4
|
+
// output, got HTTP 401 for every piece, logged it and carried on: the phone showed a blank terminal and the laptop
|
|
5
|
+
// never said why. The output sender now reports every response status here; a few 401s in a row mean "no longer
|
|
6
|
+
// paired", and the session stops with a clear message.
|
|
7
|
+
//
|
|
8
|
+
// "In a row" matters: one odd 401 must not end a session. Any successful response resets the count. Pure and tested
|
|
9
|
+
// (test/unpairedDetector.test.js).
|
|
10
|
+
'use strict';
|
|
11
|
+
|
|
12
|
+
const DEFAULT_THRESHOLD = 3;
|
|
13
|
+
|
|
14
|
+
function createUnpairedDetector({ threshold = DEFAULT_THRESHOLD, onUnpaired }) {
|
|
15
|
+
let consecutive = 0;
|
|
16
|
+
let fired = false;
|
|
17
|
+
return {
|
|
18
|
+
// Call with the HTTP status of every authenticated relay response.
|
|
19
|
+
note(status) {
|
|
20
|
+
if (fired) return;
|
|
21
|
+
if (status === 401) {
|
|
22
|
+
consecutive += 1;
|
|
23
|
+
if (consecutive >= threshold) {
|
|
24
|
+
fired = true;
|
|
25
|
+
if (typeof onUnpaired === 'function') onUnpaired();
|
|
26
|
+
}
|
|
27
|
+
} else if (status >= 200 && status < 300) {
|
|
28
|
+
consecutive = 0;
|
|
29
|
+
}
|
|
30
|
+
// Other statuses (403, 410, 429, 5xx) neither count nor reset: they mean something else.
|
|
31
|
+
},
|
|
32
|
+
get consecutive() { return consecutive; },
|
|
33
|
+
get fired() { return fired; },
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
module.exports = { createUnpairedDetector, DEFAULT_THRESHOLD };
|