dsh-remote-vs 0.25.0 → 0.26.0
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/README.en.md +11 -7
- package/README.md +10 -7
- package/THIRD-PARTY-NOTICES.md +188 -0
- package/lib/client.js +6512 -79961
- package/lib/index.js +1283 -992
- package/lib/p2-router-fs.js +1 -1
- package/lib/types/client/CodeEditor.d.ts +8 -0
- package/lib/types/client/RemoteExplorer.d.ts +53 -0
- package/lib/types/client/api.d.ts +37 -2
- package/lib/types/client/changes/review-state.d.ts +19 -0
- package/lib/types/client/connection.d.ts +16 -0
- package/lib/types/client/request-gate.d.ts +110 -0
- package/lib/types/client/scm/DiffView.d.ts +45 -7
- package/lib/types/client/scm/actions.d.ts +31 -3
- package/lib/types/client/store.d.ts +53 -0
- package/lib/types/client/vsc/FindWidget.d.ts +6 -0
- package/lib/types/client/vsc/change-marks.d.ts +16 -6
- package/lib/types/client/vsc/editor-keymap.d.ts +35 -1
- package/lib/types/client/vsc/kit.d.ts +11 -0
- package/lib/types/client/vsc/save-shortcut.d.ts +21 -0
- package/lib/types/client/vsc/terminal-theme.d.ts +28 -2
- package/lib/types/forward.d.ts +12 -0
- package/lib/types/fs-ops.d.ts +42 -0
- package/lib/types/http.d.ts +2 -0
- package/lib/types/keygen.d.ts +9 -0
- package/lib/types/monitor.d.ts +8 -0
- package/lib/types/notices.d.ts +16 -1
- package/lib/types/p2/helper-rpc.d.ts +45 -0
- package/lib/types/routes.d.ts +2 -0
- package/lib/types/store.d.ts +7 -0
- package/lib/types/terminal.d.ts +8 -0
- package/lib/types/view-settings.d.ts +5 -0
- package/lib/{worlds-B7gEDBzC.js → worlds-RG8K5eom.js} +113 -15
- package/package.json +2 -1
- package/src/client/CodeEditor.tsx +151 -8
- package/src/client/DirectoryFlow.tsx +16 -0
- package/src/client/DoctorPanel.tsx +70 -7
- package/src/client/EditorPane.tsx +63 -14
- package/src/client/HostsView.tsx +12 -3
- package/src/client/RemoteDirectoryPicker.tsx +12 -0
- package/src/client/RemoteExplorer.tsx +929 -40
- package/src/client/RemoteRoot.tsx +147 -22
- package/src/client/SearchView.tsx +8 -0
- package/src/client/TerminalSnippets.tsx +83 -8
- package/src/client/api.ts +106 -13
- package/src/client/changes/ChangesDock.tsx +188 -6
- package/src/client/changes/changes.css +10 -3
- package/src/client/changes/review-state.ts +34 -0
- package/src/client/connection.ts +29 -0
- package/src/client/downloads.ts +49 -4
- package/src/client/i18n.ts +82 -8
- package/src/client/request-gate.ts +221 -0
- package/src/client/scm/CommitPane.tsx +20 -2
- package/src/client/scm/DiffView.tsx +343 -117
- package/src/client/scm/HistoryPanel.tsx +23 -2
- package/src/client/scm/RepoPicker.tsx +1 -1
- package/src/client/scm/ScmPanel.tsx +391 -87
- package/src/client/scm/actions.ts +95 -12
- package/src/client/store.tsx +372 -9
- package/src/client/ui.tsx +4 -0
- package/src/client/vsc/FindWidget.tsx +24 -9
- package/src/client/vsc/breadcrumb.tsx +91 -3
- package/src/client/vsc/change-marks.ts +93 -44
- package/src/client/vsc/editor-keymap.ts +59 -1
- package/src/client/vsc/file-icons.tsx +17 -3
- package/src/client/vsc/kit.tsx +26 -9
- package/src/client/vsc/save-shortcut.ts +47 -1
- package/src/client/vsc/terminal-theme.ts +44 -18
- package/src/client/vsc/vsc.css +411 -153
- package/src/forward.ts +47 -9
- package/src/fs-ops.ts +124 -4
- package/src/git-ops.ts +20 -3
- package/src/http.ts +35 -4
- package/src/keygen.ts +55 -4
- package/src/monitor.ts +13 -0
- package/src/notices.ts +23 -1
- package/src/p2/helper-rpc.ts +113 -7
- package/src/p2/worlds.ts +14 -1
- package/src/routes.ts +212 -43
- package/src/sftp.ts +51 -14
- package/src/store.ts +10 -0
- package/src/terminal.ts +73 -26
- package/src/view-settings.ts +50 -10
- package/lib/types/client/Dialog.d.ts +0 -14
- package/src/client/Dialog.tsx +0 -76
package/src/p2/helper-rpc.ts
CHANGED
|
@@ -149,6 +149,21 @@ export class RpcConnection {
|
|
|
149
149
|
private heartbeat: ReturnType<typeof setTimeout> | undefined
|
|
150
150
|
private dead: Error | undefined
|
|
151
151
|
private disposed = false
|
|
152
|
+
/**
|
|
153
|
+
* Transport generation, bumped by every teardown.
|
|
154
|
+
*
|
|
155
|
+
* ssh2 does not close synchronously: `Channel.close()` is a PROTOCOL message
|
|
156
|
+
* and `Client.end()`'s socket 'close' arrives later. Those late
|
|
157
|
+
* 'close'/'error' events used to call `invalidate()` after `reconnect()` had
|
|
158
|
+
* already cleared `dead`, marking the BRAND-NEW dial dead: `open()` then
|
|
159
|
+
* released the fresh transport and the retry rejected with the OLD
|
|
160
|
+
* transport's error (probe T6 measured exactly this — a close 5 ms late, a
|
|
161
|
+
* new dial 20 ms). Every transport callback therefore captures the generation
|
|
162
|
+
* it was created under and compares it before acting; a teardown invalidates
|
|
163
|
+
* every callback of the transport it releases, without removing listeners
|
|
164
|
+
* (ssh2 offers no per-callback unsubscribe that would survive a destroy).
|
|
165
|
+
*/
|
|
166
|
+
private generation = 0
|
|
152
167
|
/**
|
|
153
168
|
* The helper's own error output, kept so its reason survives.
|
|
154
169
|
*
|
|
@@ -185,6 +200,9 @@ export class RpcConnection {
|
|
|
185
200
|
let attempt = 0
|
|
186
201
|
for (;;) {
|
|
187
202
|
const peer = await this.peerReady()
|
|
203
|
+
// The link this attempt is queued against: a timeout that fires after a
|
|
204
|
+
// concurrent caller already reconnected must not invalidate the NEW dial.
|
|
205
|
+
const generation = this.generation
|
|
188
206
|
// Queue rather than let the shared connection reject the call: every agent
|
|
189
207
|
// on this host funnels through here, and "pending request limit reached"
|
|
190
208
|
// used to fail a turn for work that only needed to wait its turn.
|
|
@@ -194,6 +212,10 @@ export class RpcConnection {
|
|
|
194
212
|
} catch (error) {
|
|
195
213
|
// A caller abort is the caller's decision — never retry it.
|
|
196
214
|
if (!(error instanceof HelperTimeoutError)) throw error
|
|
215
|
+
// The transport this request rode was already replaced while it was in
|
|
216
|
+
// flight: its timeout describes the OLD link, so leave the current one
|
|
217
|
+
// alone (the reconnect its owner is running will report its own outcome).
|
|
218
|
+
if (generation !== this.generation) throw error
|
|
197
219
|
// Mark the connection dead so the cached fs view is dropped and the next
|
|
198
220
|
// caller dials cleanly instead of queueing behind a stuck channel.
|
|
199
221
|
this.invalidate(error)
|
|
@@ -231,6 +253,14 @@ export class RpcConnection {
|
|
|
231
253
|
if (this.disposed) throw new Error('helper RPC was disposed')
|
|
232
254
|
// One teardown for every field: the old transport is fully gone before a new
|
|
233
255
|
// attempt starts (a half-closed one would keep the ssh session alive).
|
|
256
|
+
//
|
|
257
|
+
// The generation bump inside that teardown is also what makes the window
|
|
258
|
+
// below safe: `dead` is cleared while the OLD transport's asynchronous
|
|
259
|
+
// close/error events may still be in flight, and those callbacks are bound
|
|
260
|
+
// to the generation the teardown just invalidated (see `generation`) — they
|
|
261
|
+
// cannot re-set `dead` under the new dial. Without the guard this method
|
|
262
|
+
// would reject with the previous transport's error and the "fresh channel"
|
|
263
|
+
// retry would never get to run.
|
|
234
264
|
this.teardownTransport()
|
|
235
265
|
this.dead = undefined
|
|
236
266
|
this.readyPromise = this.open()
|
|
@@ -249,6 +279,13 @@ export class RpcConnection {
|
|
|
249
279
|
async dispose(): Promise<void> {
|
|
250
280
|
if (this.disposed) return
|
|
251
281
|
this.disposed = true
|
|
282
|
+
// A dial in flight has no peer/channel yet: the ssh2 `Client` belongs to the
|
|
283
|
+
// handshake, and ending it here would strand that promise — ssh2 clears its
|
|
284
|
+
// own ready timer on close and emits 'close' WITHOUT an 'error', so the
|
|
285
|
+
// awaiting `dialAndHello` would never settle (and a concurrent `dial()` that
|
|
286
|
+
// disposes a reconnect would hang the retry forever). `open()` sees
|
|
287
|
+
// `disposed` when the attempt settles and releases it there.
|
|
288
|
+
if (this.peer === undefined && this.channel === undefined && this.conn !== undefined) return
|
|
252
289
|
this.teardownTransport()
|
|
253
290
|
}
|
|
254
291
|
|
|
@@ -270,8 +307,13 @@ export class RpcConnection {
|
|
|
270
307
|
* `stderrTail` is cleared too: it describes the connection being torn down, and
|
|
271
308
|
* keeping it would attribute the NEXT failure to the previous transport's error
|
|
272
309
|
* output.
|
|
310
|
+
*
|
|
311
|
+
* The generation bump comes FIRST: the close calls below can emit their events
|
|
312
|
+
* synchronously, and anything this transport has queued must already count as
|
|
313
|
+
* stale by then.
|
|
273
314
|
*/
|
|
274
315
|
private teardownTransport(): void {
|
|
316
|
+
this.generation += 1
|
|
275
317
|
if (this.heartbeat !== undefined) {
|
|
276
318
|
clearTimeout(this.heartbeat)
|
|
277
319
|
this.heartbeat = undefined
|
|
@@ -291,7 +333,19 @@ export class RpcConnection {
|
|
|
291
333
|
const conn = new Client()
|
|
292
334
|
this.conn = conn
|
|
293
335
|
try {
|
|
294
|
-
|
|
336
|
+
const hello = await this.dialAndHello(conn)
|
|
337
|
+
// `invalidate()` may have fired WHILE this attempt was dialing: a genuine
|
|
338
|
+
// error/close on THIS socket after 'ready' but before the hello answer sets
|
|
339
|
+
// `dead` without touching the dialing Client; a `dispose()` that arrived
|
|
340
|
+
// meanwhile set `disposed` and deliberately left the dial alone (ending it
|
|
341
|
+
// would strand the handshake). Either way the attempt has now succeeded, so
|
|
342
|
+
// the transport it produced is useless to everyone — release it here rather
|
|
343
|
+
// than leaving a live socket with no owner (the leak this path prevents).
|
|
344
|
+
if (this.dead !== undefined || this.disposed) {
|
|
345
|
+
this.teardownTransport()
|
|
346
|
+
throw this.dead ?? new Error('helper RPC was disposed')
|
|
347
|
+
}
|
|
348
|
+
return hello
|
|
295
349
|
} catch (error) {
|
|
296
350
|
// Anything that fails after the socket is up must not leave it behind: the
|
|
297
351
|
// caller only sees the reject, and the next attempt would open a second
|
|
@@ -304,6 +358,10 @@ export class RpcConnection {
|
|
|
304
358
|
/** The part of {@link open} that runs once the socket object exists. */
|
|
305
359
|
private async dialAndHello(conn: Client): Promise<RpcHello> {
|
|
306
360
|
const { entry } = this.opts
|
|
361
|
+
// The transport this attempt belongs to. Anything ssh2 reports after a
|
|
362
|
+
// teardown is about a link nobody owns any more; the callbacks below compare
|
|
363
|
+
// against the live value before touching `dead`/`stderrTail`.
|
|
364
|
+
const generation = this.generation
|
|
307
365
|
await new Promise<void>((resolve, reject) => {
|
|
308
366
|
let settled = false
|
|
309
367
|
conn.on('ready', () => {
|
|
@@ -316,12 +374,20 @@ export class RpcConnection {
|
|
|
316
374
|
if (!settled) {
|
|
317
375
|
settled = true
|
|
318
376
|
reject(error)
|
|
319
|
-
} else {
|
|
377
|
+
} else if (generation === this.generation) {
|
|
320
378
|
this.invalidate(error)
|
|
321
379
|
}
|
|
322
380
|
})
|
|
323
381
|
conn.on('close', () => {
|
|
324
|
-
|
|
382
|
+
// Settle a handshake that never became ready: ssh2 reports a socket that
|
|
383
|
+
// closes before 'ready' as 'close' only (no 'error'), which used to leave
|
|
384
|
+
// `dialAndHello` pending forever.
|
|
385
|
+
if (!settled) {
|
|
386
|
+
settled = true
|
|
387
|
+
reject(new Error('ssh connection closed before it became ready'))
|
|
388
|
+
}
|
|
389
|
+
// A stale close describes the transport `reconnect()` already replaced.
|
|
390
|
+
if (generation === this.generation) this.invalidate(new Error('ssh connection closed'))
|
|
325
391
|
})
|
|
326
392
|
try {
|
|
327
393
|
if (entry.auth.kind === 'key') {
|
|
@@ -359,13 +425,16 @@ export class RpcConnection {
|
|
|
359
425
|
})
|
|
360
426
|
this.channel = channel
|
|
361
427
|
channel.stderr.on('data', (chunk: Buffer) => {
|
|
428
|
+
// A late chunk describes the previous transport's reason; attributing it
|
|
429
|
+
// to the new one would print the wrong helper error on the next failure.
|
|
430
|
+
if (generation !== this.generation) return
|
|
362
431
|
this.stderrTail = (this.stderrTail + chunk.toString('utf8')).slice(-STDERR_TAIL_MAX)
|
|
363
432
|
})
|
|
364
433
|
channel.on('close', () => {
|
|
365
|
-
this.invalidate(new Error('helper channel closed'))
|
|
434
|
+
if (generation === this.generation) this.invalidate(new Error('helper channel closed'))
|
|
366
435
|
})
|
|
367
436
|
channel.on('error', (error: Error) => {
|
|
368
|
-
this.invalidate(error)
|
|
437
|
+
if (generation === this.generation) this.invalidate(error)
|
|
369
438
|
})
|
|
370
439
|
const leaseMs = this.opts.leaseMs ?? 30000
|
|
371
440
|
const peer = new SshRpcPeer(channel, channel, MAX_FRAME, MAX_PENDING)
|
|
@@ -398,10 +467,15 @@ export class RpcConnection {
|
|
|
398
467
|
// Arming the next beat only after the previous one settles makes overlap
|
|
399
468
|
// impossible.
|
|
400
469
|
const beat = (): void => {
|
|
401
|
-
|
|
470
|
+
// A stale generation means this transport is gone: its teardown cleared the
|
|
471
|
+
// timer, and a beat already in flight must not resurrect it.
|
|
472
|
+
if (this.disposed || generation !== this.generation || this.dead !== undefined) return
|
|
402
473
|
this.heartbeat = setTimeout(() => {
|
|
403
474
|
peer.request('heartbeat', {}, z.null(), AbortSignal.timeout(Math.floor(leaseMs / 2)))
|
|
404
475
|
.catch((error: unknown) => {
|
|
476
|
+
// The teardown closes the peer, so a beat that was in flight rejects
|
|
477
|
+
// with the TEARDOWN's error. That is not a fact about the new link.
|
|
478
|
+
if (generation !== this.generation) return
|
|
405
479
|
this.invalidate(error instanceof Error ? error : new Error(String(error)))
|
|
406
480
|
})
|
|
407
481
|
.finally(beat)
|
|
@@ -418,14 +492,46 @@ export class RpcConnection {
|
|
|
418
492
|
}
|
|
419
493
|
}
|
|
420
494
|
|
|
495
|
+
/**
|
|
496
|
+
* Mark the connection dead AND release its transport.
|
|
497
|
+
*
|
|
498
|
+
* Closing the peer alone was not enough: `SshRpcPeer.close()` destroys the
|
|
499
|
+
* exec streams, and ssh2's `Channel.destroy()` does not end the `Client` that
|
|
500
|
+
* carries them — so the session stayed open (30s keepalive and all) while its
|
|
501
|
+
* owner had already written it off as dead. One leaked socket per invalidation,
|
|
502
|
+
* until the process exits. `teardownTransport()` is the only path that ends the
|
|
503
|
+
* Client, so an invalidated connection goes through it too; that is safe to
|
|
504
|
+
* repeat because each field is cleared as it is closed and `closeQuietly`
|
|
505
|
+
* ignores a second close — a later `dispose()`, `reconnect()` or failed
|
|
506
|
+
* `open()` teardown therefore finds nothing left to release.
|
|
507
|
+
*
|
|
508
|
+
* Ordering is deliberate: the peer is closed FIRST so in-flight requests reject
|
|
509
|
+
* with THIS error (the teardown's own close would raise a generic
|
|
510
|
+
* "channel closed" instead), and `stderrTail` is captured BEFORE the teardown,
|
|
511
|
+
* which clears it — otherwise `diagnostics` in the dial path would always
|
|
512
|
+
* report "the helper wrote nothing to stderr".
|
|
513
|
+
*
|
|
514
|
+
* Only an ESTABLISHED transport is released here. While `open()` is still
|
|
515
|
+
* dialing, `conn` is alive and `channel`/`peer` are not, and ending that Client
|
|
516
|
+
* would strand the handshake: ssh2 clears its own ready timer when the socket
|
|
517
|
+
* closes and emits 'close' without an 'error', so the promise `open()` awaits
|
|
518
|
+
* would never settle. `open()` itself releases the attempt instead, once it has
|
|
519
|
+
* a definitive outcome.
|
|
520
|
+
*/
|
|
421
521
|
private invalidate(error: Error): void {
|
|
422
522
|
if (this.dead !== undefined || this.disposed) return
|
|
423
523
|
this.dead = error
|
|
424
|
-
if (this.heartbeat !== undefined)
|
|
524
|
+
if (this.heartbeat !== undefined) {
|
|
525
|
+
clearTimeout(this.heartbeat)
|
|
526
|
+
this.heartbeat = undefined
|
|
527
|
+
}
|
|
528
|
+
const tail = this.stderrTail
|
|
425
529
|
try {
|
|
426
530
|
this.peer?.close(error)
|
|
427
531
|
} catch {
|
|
428
532
|
// Ignore.
|
|
429
533
|
}
|
|
534
|
+
if (this.peer !== undefined || this.channel !== undefined) this.teardownTransport()
|
|
535
|
+
this.stderrTail = tail
|
|
430
536
|
}
|
|
431
537
|
}
|
package/src/p2/worlds.ts
CHANGED
|
@@ -658,9 +658,22 @@ export class RemoteWorlds {
|
|
|
658
658
|
// cooldown engage, which is what keeps a PERMANENT condition from
|
|
659
659
|
// storming — the guard belongs on the persistent case, not the first
|
|
660
660
|
// hiccup.
|
|
661
|
+
//
|
|
662
|
+
// Discarding the connection must RELEASE its transport first. `alive` goes
|
|
663
|
+
// false as soon as the helper channel dies, but the ssh2 `Client` under it
|
|
664
|
+
// is still an open socket: an invalidated connection closes only its RPC
|
|
665
|
+
// peer, and `Channel.destroy()` does not end the session — so dropping the
|
|
666
|
+
// cache entry alone left one Client (with its 30s keepalive) alive per
|
|
667
|
+
// failed dial, accumulating until the plugin unloaded. `dispose()` is the
|
|
668
|
+
// one path that reaches `teardownTransport()` (peer → channel → Client).
|
|
669
|
+
// `diagnostics` is read first because that teardown clears `stderrTail`,
|
|
670
|
+
// and the failure message below must keep the helper's own words. The
|
|
671
|
+
// dispose is not awaited — it completes synchronously (no transport I/O)
|
|
672
|
+
// and retry pacing must not change.
|
|
673
|
+
const detail = conn.diagnostics
|
|
674
|
+
void conn.dispose().catch(() => undefined)
|
|
661
675
|
this.rpcs.delete(alias)
|
|
662
676
|
if (attempt < DIAL_IMMEDIATE_RETRIES) return this.dial(alias, attempt + 1)
|
|
663
|
-
const detail = conn.diagnostics
|
|
664
677
|
const reason = 'helper connection closed right after dialing'
|
|
665
678
|
+ (detail === '' ? ' (the helper wrote nothing to stderr)' : ` — helper stderr: ${detail}`)
|
|
666
679
|
// Recorded so the NEXT call backs off, and thrown here rather than
|
package/src/routes.ts
CHANGED
|
@@ -10,9 +10,17 @@ import { fileURLToPath } from 'node:url'
|
|
|
10
10
|
import { WebSocketServer } from 'ws'
|
|
11
11
|
import type { Context } from '@deepseek-ai/cordis'
|
|
12
12
|
import type { WebRoute, WebUpgradeRoute } from '@deepseek-ai/dsh-host-webserver'
|
|
13
|
-
import { readJsonBodyResult, writeJson, writeRouteError } from './http.ts'
|
|
13
|
+
import { readJsonBodyResult, scopeHostNotices, writeJson, writeRouteError } from './http.ts'
|
|
14
14
|
import { isLoopbackRequest } from './loopback.ts'
|
|
15
|
-
import {
|
|
15
|
+
import {
|
|
16
|
+
REMOTE_API,
|
|
17
|
+
MAX_SELECTION_DOWNLOAD,
|
|
18
|
+
selectionDownloadName,
|
|
19
|
+
type ExecStreamEvent,
|
|
20
|
+
type ImportReport,
|
|
21
|
+
type RemoteHostMetrics,
|
|
22
|
+
type RemoteTimeouts,
|
|
23
|
+
} from './protocol.ts'
|
|
16
24
|
import type { HostPayload, RemoteStore } from './store.ts'
|
|
17
25
|
import type { RemotePool } from './pool.ts'
|
|
18
26
|
import type { RemoteWorlds } from './p2/worlds.ts'
|
|
@@ -29,17 +37,29 @@ import type { SessionChanges } from './changes/recorder.ts'
|
|
|
29
37
|
import { DEFAULT_TIMEOUTS, clampExecTimeout } from './timeouts.ts'
|
|
30
38
|
import { RemoteViewSettingsStore } from './view-settings.ts'
|
|
31
39
|
|
|
32
|
-
/** One noServer WebSocket server for terminal upgrades (RFC 6455). */
|
|
33
|
-
// `maxPayload` is the authoritative frame bound: without it ws buffers up to its
|
|
34
|
-
// 100 MiB default before the handler ever sees the message, in the process that
|
|
35
|
-
// also runs the agent. Terminal frames are small JSON control messages.
|
|
36
|
-
const terminalWss = new WebSocketServer({ noServer: true, maxPayload: TERMINAL_MAX_FRAME_BYTES })
|
|
37
|
-
|
|
38
40
|
function queryParam(url: URL, name: string): string | undefined {
|
|
39
41
|
const value = url.searchParams.get(name)
|
|
40
42
|
return value === null ? undefined : value
|
|
41
43
|
}
|
|
42
44
|
|
|
45
|
+
/**
|
|
46
|
+
* Host alias a request URL names (`?alias=`), or `null` when it names none.
|
|
47
|
+
*
|
|
48
|
+
* Only the routes know what a request is about; the response writer needs that
|
|
49
|
+
* in order to attach only THIS host's notices (see `scopeHostNotices`).
|
|
50
|
+
*/
|
|
51
|
+
function urlAliasOf(req: IncomingMessage): string | null {
|
|
52
|
+
const alias = new URL(req.url ?? '/', 'http://localhost').searchParams.get('alias')
|
|
53
|
+
return alias === null || alias === '' ? null : alias
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Host alias a parsed JSON body names, or `null` when it names none. */
|
|
57
|
+
function bodyAliasOf(value: unknown): string | null {
|
|
58
|
+
if (typeof value !== 'object' || value === null) return null
|
|
59
|
+
const alias = (value as { alias?: unknown }).alias
|
|
60
|
+
return typeof alias === 'string' && alias !== '' ? alias : null
|
|
61
|
+
}
|
|
62
|
+
|
|
43
63
|
/**
|
|
44
64
|
* A caller mistake the route can name: well-formed HTTP, unusable payload.
|
|
45
65
|
*
|
|
@@ -97,6 +117,12 @@ async function readBodyOrError<T>(
|
|
|
97
117
|
writeJson(res, 400, { error: 'invalid JSON body' })
|
|
98
118
|
return { ok: false }
|
|
99
119
|
}
|
|
120
|
+
// POST routes name their host in the BODY, which the URL cannot tell us: mark
|
|
121
|
+
// the response here so its one-shot notices bind to that host.
|
|
122
|
+
if (result.kind === 'ok') {
|
|
123
|
+
const alias = bodyAliasOf(result.value)
|
|
124
|
+
if (alias !== null) scopeHostNotices(res, alias)
|
|
125
|
+
}
|
|
100
126
|
return { ok: true, body: result.kind === 'ok' ? (result.value as T) : null }
|
|
101
127
|
}
|
|
102
128
|
|
|
@@ -257,7 +283,13 @@ function ownSettingsNs(ctx: Context): string | undefined {
|
|
|
257
283
|
return fallback
|
|
258
284
|
}
|
|
259
285
|
|
|
260
|
-
export function makeRoutes(deps: RemoteRoutesDeps): {
|
|
286
|
+
export function makeRoutes(deps: RemoteRoutesDeps): {
|
|
287
|
+
routes: WebRoute[]
|
|
288
|
+
upgrade: WebUpgradeRoute
|
|
289
|
+
terminalSessions: TerminalSessionRegistry
|
|
290
|
+
/** Close the terminal upgrade server (plugin unload); idempotent. */
|
|
291
|
+
close: () => void
|
|
292
|
+
} {
|
|
261
293
|
const { store, pool, worlds, ctx } = deps
|
|
262
294
|
/** Timeouts for this request (a volatile config edit must apply live). */
|
|
263
295
|
const timeouts = (): RemoteTimeouts => deps.timeouts?.() ?? DEFAULT_TIMEOUTS
|
|
@@ -270,6 +302,56 @@ export function makeRoutes(deps: RemoteRoutesDeps): { routes: WebRoute[]; upgrad
|
|
|
270
302
|
const forwards = deps.forwards
|
|
271
303
|
const viewSettings = deps.viewSettings ?? new RemoteViewSettingsStore()
|
|
272
304
|
|
|
305
|
+
/**
|
|
306
|
+
* One noServer WebSocket server for terminal upgrades (RFC 6455).
|
|
307
|
+
*
|
|
308
|
+
* `maxPayload` is the authoritative frame bound: without it ws buffers up to
|
|
309
|
+
* its 100 MiB default before the handler ever sees the message, in the
|
|
310
|
+
* process that also runs the agent. Terminal frames are small JSON control
|
|
311
|
+
* messages.
|
|
312
|
+
*
|
|
313
|
+
* It belongs to the route set, not to the module: as a module-level singleton
|
|
314
|
+
* a plugin unload (and every later reload with a fresh module instance) left
|
|
315
|
+
* the old server object alive forever. It must NOT close per route rebuild —
|
|
316
|
+
* live PTYs survive a settings edit, and ws answers an upgrade on a closed
|
|
317
|
+
* server with 503 — so teardown is bound to the fiber (the unload boundary);
|
|
318
|
+
* a later load builds a fresh instance.
|
|
319
|
+
*/
|
|
320
|
+
const terminalWss = new WebSocketServer({ noServer: true, maxPayload: TERMINAL_MAX_FRAME_BYTES })
|
|
321
|
+
/** Idempotent teardown of the terminal upgrade server (plugin unload). */
|
|
322
|
+
const close = (): void => {
|
|
323
|
+
try {
|
|
324
|
+
terminalWss.close()
|
|
325
|
+
} catch {
|
|
326
|
+
// Already closed.
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
// Route-only probes build `makeRoutes` with a bare ctx (no fiber): they own
|
|
330
|
+
// no server lifetime and their process ends with the probe.
|
|
331
|
+
if (typeof ctx.effect === 'function') {
|
|
332
|
+
ctx.effect(() => close, 'dsh-remote-vs: terminal websocket server')
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/**
|
|
336
|
+
* A corrupt store is a service-side failure with a path the user can repair.
|
|
337
|
+
*
|
|
338
|
+
* `store.list()`/`listWorkspaces()` re-read the JSON file, and a bad file
|
|
339
|
+
* threw straight out of the route: the host webserver answered a bare 400
|
|
340
|
+
* with no body, so the GUI could only report "request failed" and the path
|
|
341
|
+
* that needs fixing never reached the person who could fix it.
|
|
342
|
+
*/
|
|
343
|
+
const writeStoreError = (res: ServerResponse, error: unknown): void => {
|
|
344
|
+
writeJson(res, 503, {
|
|
345
|
+
code: 'store-corrupt',
|
|
346
|
+
error: error instanceof Error ? error.message : String(error),
|
|
347
|
+
path: store.filePath,
|
|
348
|
+
})
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
/** Did the store's own JSON parse/validate fail? (`store.filePath` names it.) */
|
|
352
|
+
const isStoreError = (error: unknown): boolean =>
|
|
353
|
+
error instanceof Error && error.message.includes(store.filePath)
|
|
354
|
+
|
|
273
355
|
/**
|
|
274
356
|
* Stream one remote `tar -czf -` command into a `.tar.gz` download response.
|
|
275
357
|
*
|
|
@@ -388,7 +470,11 @@ export function makeRoutes(deps: RemoteRoutesDeps): { routes: WebRoute[]; upgrad
|
|
|
388
470
|
const method = req.method ?? 'GET'
|
|
389
471
|
const url = new URL(req.url ?? '/', 'http://localhost')
|
|
390
472
|
if (method === 'GET') {
|
|
391
|
-
|
|
473
|
+
try {
|
|
474
|
+
writeJson(res, 200, { hosts: store.list().map(h => store.summarize(h)) })
|
|
475
|
+
} catch (error) {
|
|
476
|
+
writeStoreError(res, error)
|
|
477
|
+
}
|
|
392
478
|
return
|
|
393
479
|
}
|
|
394
480
|
if (method === 'POST') {
|
|
@@ -549,7 +635,53 @@ export function makeRoutes(deps: RemoteRoutesDeps): { routes: WebRoute[]; upgrad
|
|
|
549
635
|
return
|
|
550
636
|
}
|
|
551
637
|
const timeoutMs = clampExecTimeout(body.timeoutMs)
|
|
552
|
-
|
|
638
|
+
/** The client closed the request: its response is never read. */
|
|
639
|
+
let clientGone = false
|
|
640
|
+
/** The client asked to stop: kill the remote even if the read is still opening. */
|
|
641
|
+
let cancelRequested = false
|
|
642
|
+
/** The cancel handle was invoked (only once). */
|
|
643
|
+
let cancelSent = false
|
|
644
|
+
let cancel: (() => void) | undefined
|
|
645
|
+
/**
|
|
646
|
+
* Stop the remote process group exactly once.
|
|
647
|
+
*
|
|
648
|
+
* Same latch as the streaming route: the disconnect may land while
|
|
649
|
+
* `pool.exec` is still acquiring the channel (slot queue, setsid probe,
|
|
650
|
+
* connect), where no handle exists yet — the request is remembered and
|
|
651
|
+
* fired the moment `onCancel` supplies one. Without this the command kept
|
|
652
|
+
* running to its timeout after a page refresh, holding a channel slot and
|
|
653
|
+
* remote CPU for nobody.
|
|
654
|
+
*/
|
|
655
|
+
const abortRemote = (): void => {
|
|
656
|
+
cancelRequested = true
|
|
657
|
+
if (cancel === undefined || cancelSent) return
|
|
658
|
+
cancelSent = true
|
|
659
|
+
cancel()
|
|
660
|
+
}
|
|
661
|
+
res.on('close', () => {
|
|
662
|
+
// Also fires after a normal response; the run is settled by then and the
|
|
663
|
+
// cancel is a no-op.
|
|
664
|
+
clientGone = true
|
|
665
|
+
abortRemote()
|
|
666
|
+
})
|
|
667
|
+
try {
|
|
668
|
+
const result = await pool.exec(body.alias, body.command, {
|
|
669
|
+
...(timeoutMs === undefined ? {} : { timeoutMs }),
|
|
670
|
+
onCancel: (handle) => {
|
|
671
|
+
cancel = handle
|
|
672
|
+
if (cancelRequested && !cancelSent) {
|
|
673
|
+
cancelSent = true
|
|
674
|
+
handle()
|
|
675
|
+
}
|
|
676
|
+
},
|
|
677
|
+
})
|
|
678
|
+
// Writing onto a closed socket is pointless (and the caller is gone).
|
|
679
|
+
if (!clientGone) writeJson(res, 200, result)
|
|
680
|
+
} catch (error) {
|
|
681
|
+
// The caller may have disconnected while the command was being set up:
|
|
682
|
+
// do not write a 502 onto a closed socket either.
|
|
683
|
+
if (!clientGone) writeJson(res, 502, { error: error instanceof Error ? error.message : String(error) })
|
|
684
|
+
}
|
|
553
685
|
},
|
|
554
686
|
},
|
|
555
687
|
// ----------------------------------------------------- exec (streaming)
|
|
@@ -1001,24 +1133,28 @@ export function makeRoutes(deps: RemoteRoutesDeps): { routes: WebRoute[]; upgrad
|
|
|
1001
1133
|
const method = req.method ?? 'GET'
|
|
1002
1134
|
const url = new URL(req.url ?? '/', 'http://localhost')
|
|
1003
1135
|
if (method === 'GET') {
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1136
|
+
try {
|
|
1137
|
+
const bindings = store.listWorkspaces()
|
|
1138
|
+
const aliasPaths: Record<string, string> = {}
|
|
1139
|
+
for (const b of bindings) aliasPaths[b.id] = worlds.aliasPathFor(b.id)
|
|
1140
|
+
// Lazy registry ensure: a boot-time sync may have run before the
|
|
1141
|
+
// workspace registry was ready (soft-skipped); the GUI always GETs
|
|
1142
|
+
// on load, long after readiness, so re-sync here best-effort.
|
|
1143
|
+
void worlds.syncWorkspaces().catch(() => undefined)
|
|
1144
|
+
writeJson(res, 200, {
|
|
1145
|
+
workspaces: bindings,
|
|
1146
|
+
mode: worlds.mode,
|
|
1147
|
+
aliasPaths,
|
|
1148
|
+
tombstones: store.listTombstones(),
|
|
1149
|
+
version: pluginVersion(),
|
|
1150
|
+
// The client's own deadlines must match what this Host would allow,
|
|
1151
|
+
// otherwise a raised `gitTimeoutMs` still died at the client's 45 s
|
|
1152
|
+
// deadline (2026-10-19: large-repo `git status` looked like a hang).
|
|
1153
|
+
timeouts: timeouts(),
|
|
1154
|
+
})
|
|
1155
|
+
} catch (error) {
|
|
1156
|
+
writeStoreError(res, error)
|
|
1157
|
+
}
|
|
1022
1158
|
return
|
|
1023
1159
|
}
|
|
1024
1160
|
if (method === 'POST') {
|
|
@@ -1282,7 +1418,10 @@ export function makeRoutes(deps: RemoteRoutesDeps): { routes: WebRoute[]; upgrad
|
|
|
1282
1418
|
const entry = store.require(alias)
|
|
1283
1419
|
writeJson(res, 200, { deploy: entry.deploy ?? { status: 'not-deployed' }, helpers: helperAvailability() })
|
|
1284
1420
|
} catch (error) {
|
|
1285
|
-
|
|
1421
|
+
// An unreadable store is NOT "unknown alias": report it as the
|
|
1422
|
+
// service failure it is, with the path the user can repair.
|
|
1423
|
+
if (isStoreError(error)) writeStoreError(res, error)
|
|
1424
|
+
else writeJson(res, 404, { error: error instanceof Error ? error.message : String(error) })
|
|
1286
1425
|
}
|
|
1287
1426
|
return
|
|
1288
1427
|
}
|
|
@@ -1486,7 +1625,10 @@ export function makeRoutes(deps: RemoteRoutesDeps): { routes: WebRoute[]; upgrad
|
|
|
1486
1625
|
}
|
|
1487
1626
|
writeJson(res, 200, parseListenPorts(result.stdout, result.exitCode))
|
|
1488
1627
|
} catch (error) {
|
|
1489
|
-
|
|
1628
|
+
// A corrupt store during `alias` lookup is a 503 with the path, not a
|
|
1629
|
+
// 502 "remote host failed".
|
|
1630
|
+
if (isStoreError(error)) writeStoreError(res, error)
|
|
1631
|
+
else writeJson(res, 502, { error: error instanceof Error ? error.message : String(error) })
|
|
1490
1632
|
}
|
|
1491
1633
|
},
|
|
1492
1634
|
},
|
|
@@ -1497,9 +1639,12 @@ export function makeRoutes(deps: RemoteRoutesDeps): { routes: WebRoute[]; upgrad
|
|
|
1497
1639
|
// poll storm costs one channel and the first sample honestly says "unknown"
|
|
1498
1640
|
// for the two deltas instead of inventing numbers.
|
|
1499
1641
|
//
|
|
1500
|
-
// `alias` selects one host
|
|
1501
|
-
//
|
|
1502
|
-
//
|
|
1642
|
+
// `alias` selects one host and is the ONLY shape that samples: a GET must
|
|
1643
|
+
// not have a dial side effect, and a bare `GET /metrics` used to sample
|
|
1644
|
+
// every configured host (waking sleeping machines for a listing nobody
|
|
1645
|
+
// asked to probe). Without `alias` it answers the cached samples only;
|
|
1646
|
+
// `all=1` asks for the real batch sample explicitly. Loopback-fenced like
|
|
1647
|
+
// every other route.
|
|
1503
1648
|
{
|
|
1504
1649
|
kind: 'exact',
|
|
1505
1650
|
path: REMOTE_API.metrics,
|
|
@@ -1515,15 +1660,22 @@ export function makeRoutes(deps: RemoteRoutesDeps): { routes: WebRoute[]; upgrad
|
|
|
1515
1660
|
const url = new URL(req.url ?? '/', 'http://localhost')
|
|
1516
1661
|
const alias = queryParam(url, 'alias')
|
|
1517
1662
|
const refresh = queryParam(url, 'refresh') === '1'
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
return
|
|
1521
|
-
}
|
|
1522
|
-
const aliases = alias === undefined || alias === ''
|
|
1523
|
-
? store.list().map(host => host.alias)
|
|
1524
|
-
: [alias]
|
|
1663
|
+
/** Explicit opt-in to sampling every configured host (tooling/debug). */
|
|
1664
|
+
const all = queryParam(url, 'all') === '1'
|
|
1525
1665
|
try {
|
|
1526
|
-
|
|
1666
|
+
let hosts: RemoteHostMetrics[]
|
|
1667
|
+
if (alias !== undefined && alias !== '') {
|
|
1668
|
+
if (store.get(alias) === undefined) {
|
|
1669
|
+
writeJson(res, 404, { error: `unknown host alias: ${alias}` })
|
|
1670
|
+
return
|
|
1671
|
+
}
|
|
1672
|
+
hosts = [await monitor.sample(alias, refresh)]
|
|
1673
|
+
} else if (all) {
|
|
1674
|
+
const aliases = store.list().map(host => host.alias)
|
|
1675
|
+
hosts = await Promise.all(aliases.map(host => monitor.sample(host, refresh)))
|
|
1676
|
+
} else {
|
|
1677
|
+
hosts = monitor.cached()
|
|
1678
|
+
}
|
|
1527
1679
|
writeJson(res, 200, { hosts })
|
|
1528
1680
|
} catch (error) {
|
|
1529
1681
|
writeJson(res, 502, { error: error instanceof Error ? error.message : String(error) })
|
|
@@ -1578,5 +1730,22 @@ export function makeRoutes(deps: RemoteRoutesDeps): { routes: WebRoute[]; upgrad
|
|
|
1578
1730
|
},
|
|
1579
1731
|
}
|
|
1580
1732
|
|
|
1581
|
-
|
|
1733
|
+
/**
|
|
1734
|
+
* Mark every response with the host it is about before the handler runs.
|
|
1735
|
+
*
|
|
1736
|
+
* `writeJson` attaches notices only for the alias recorded here (or, for a
|
|
1737
|
+
* POST, the one its parsed body names): one window may then never display —
|
|
1738
|
+
* and consume — another host's one-shot helper-push notice. A request that
|
|
1739
|
+
* names no host (the hosts/workspaces listings) marks `null` and leaves the
|
|
1740
|
+
* queue alone.
|
|
1741
|
+
*/
|
|
1742
|
+
const scopedRoutes: WebRoute[] = routes.map(route => ({
|
|
1743
|
+
...route,
|
|
1744
|
+
handler: (req, res) => {
|
|
1745
|
+
scopeHostNotices(res, urlAliasOf(req))
|
|
1746
|
+
return route.handler(req, res)
|
|
1747
|
+
},
|
|
1748
|
+
}))
|
|
1749
|
+
|
|
1750
|
+
return { routes: scopedRoutes, upgrade, terminalSessions, close }
|
|
1582
1751
|
}
|