dsh-remote-vs 0.24.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.
Files changed (125) hide show
  1. package/README.en.md +50 -16
  2. package/README.md +40 -15
  3. package/THIRD-PARTY-NOTICES.md +188 -0
  4. package/lib/client.js +6512 -77887
  5. package/lib/index.js +1461 -1024
  6. package/lib/p2-router-fs.js +1 -1
  7. package/lib/types/client/CodeEditor.d.ts +25 -0
  8. package/lib/types/client/EditorPane.d.ts +16 -5
  9. package/lib/types/client/FloatingPlaceholder.d.ts +16 -5
  10. package/lib/types/client/RemoteEditorView.d.ts +13 -10
  11. package/lib/types/client/RemoteExplorer.d.ts +53 -0
  12. package/lib/types/client/RemoteRoot.d.ts +5 -5
  13. package/lib/types/client/TerminalSnippets.d.ts +36 -0
  14. package/lib/types/client/TerminalView.d.ts +1 -1
  15. package/lib/types/client/XTerm.d.ts +33 -0
  16. package/lib/types/client/api.d.ts +37 -2
  17. package/lib/types/client/changes/review-state.d.ts +19 -0
  18. package/lib/types/client/connection.d.ts +16 -0
  19. package/lib/types/client/editor-capability.d.ts +27 -0
  20. package/lib/types/client/i18n.d.ts +1 -1
  21. package/lib/types/client/request-gate.d.ts +110 -0
  22. package/lib/types/client/scm/DiffView.d.ts +45 -7
  23. package/lib/types/client/scm/actions.d.ts +31 -3
  24. package/lib/types/client/store.d.ts +112 -4
  25. package/lib/types/client/terminal-input.d.ts +23 -0
  26. package/lib/types/client/terminal-snippets.d.ts +86 -0
  27. package/lib/types/client/vsc/FindWidget.d.ts +6 -0
  28. package/lib/types/client/vsc/breadcrumb.d.ts +30 -0
  29. package/lib/types/client/vsc/change-marks.d.ts +16 -6
  30. package/lib/types/client/vsc/editor-caret.d.ts +32 -0
  31. package/lib/types/client/vsc/editor-keymap.d.ts +35 -1
  32. package/lib/types/client/vsc/editor-langs.d.ts +9 -0
  33. package/lib/types/client/vsc/editor-settings.d.ts +55 -0
  34. package/lib/types/client/vsc/image-preview.d.ts +36 -0
  35. package/lib/types/client/vsc/kit.d.ts +13 -0
  36. package/lib/types/client/vsc/save-shortcut.d.ts +21 -0
  37. package/lib/types/client/vsc/terminal-clipboard.d.ts +15 -0
  38. package/lib/types/client/vsc/terminal-line.d.ts +25 -0
  39. package/lib/types/client/vsc/terminal-links.d.ts +62 -0
  40. package/lib/types/client/vsc/terminal-theme.d.ts +48 -0
  41. package/lib/types/client/vsc/terminal-writer.d.ts +52 -0
  42. package/lib/types/forward.d.ts +12 -0
  43. package/lib/types/fs-ops.d.ts +42 -0
  44. package/lib/types/http.d.ts +2 -0
  45. package/lib/types/keygen.d.ts +9 -0
  46. package/lib/types/monitor.d.ts +8 -0
  47. package/lib/types/notices.d.ts +16 -1
  48. package/lib/types/p2/helper-rpc.d.ts +45 -0
  49. package/lib/types/pool.d.ts +25 -0
  50. package/lib/types/routes.d.ts +2 -0
  51. package/lib/types/store.d.ts +7 -0
  52. package/lib/types/terminal.d.ts +33 -1
  53. package/lib/types/view-settings.d.ts +5 -0
  54. package/lib/{worlds-B7gEDBzC.js → worlds-RG8K5eom.js} +113 -15
  55. package/package.json +2 -1
  56. package/src/client/CodeEditor.tsx +205 -10
  57. package/src/client/DirectoryFlow.tsx +16 -0
  58. package/src/client/DoctorPanel.tsx +70 -7
  59. package/src/client/EditorPane.tsx +545 -118
  60. package/src/client/FloatingPlaceholder.tsx +34 -5
  61. package/src/client/HostsView.tsx +12 -3
  62. package/src/client/RemoteDirectoryPicker.tsx +12 -0
  63. package/src/client/RemoteEditorView.tsx +14 -11
  64. package/src/client/RemoteExplorer.tsx +929 -40
  65. package/src/client/RemoteRoot.tsx +167 -147
  66. package/src/client/SearchView.tsx +8 -0
  67. package/src/client/TerminalSnippets.tsx +432 -0
  68. package/src/client/TerminalView.tsx +50 -1
  69. package/src/client/XTerm.tsx +228 -58
  70. package/src/client/api.ts +106 -13
  71. package/src/client/auto-open-remote.ts +11 -9
  72. package/src/client/changes/ChangesDock.tsx +188 -6
  73. package/src/client/changes/changes.css +10 -3
  74. package/src/client/changes/review-state.ts +34 -0
  75. package/src/client/connection.ts +29 -0
  76. package/src/client/downloads.ts +49 -4
  77. package/src/client/editor-capability.ts +42 -0
  78. package/src/client/i18n.ts +228 -12
  79. package/src/client/index.tsx +22 -6
  80. package/src/client/request-gate.ts +221 -0
  81. package/src/client/scm/CommitPane.tsx +20 -2
  82. package/src/client/scm/DiffView.tsx +343 -117
  83. package/src/client/scm/HistoryPanel.tsx +23 -2
  84. package/src/client/scm/RepoPicker.tsx +1 -1
  85. package/src/client/scm/ScmPanel.tsx +391 -87
  86. package/src/client/scm/actions.ts +95 -12
  87. package/src/client/store.tsx +537 -22
  88. package/src/client/terminal-input.ts +44 -0
  89. package/src/client/terminal-snippets.ts +216 -0
  90. package/src/client/ui.tsx +4 -0
  91. package/src/client/vsc/FindWidget.tsx +24 -9
  92. package/src/client/vsc/breadcrumb.tsx +339 -0
  93. package/src/client/vsc/change-marks.ts +93 -44
  94. package/src/client/vsc/editor-caret.ts +30 -0
  95. package/src/client/vsc/editor-keymap.ts +59 -1
  96. package/src/client/vsc/editor-langs.ts +20 -0
  97. package/src/client/vsc/editor-settings.ts +146 -0
  98. package/src/client/vsc/editor-theme.ts +4 -5
  99. package/src/client/vsc/file-icons.tsx +17 -3
  100. package/src/client/vsc/image-preview.tsx +110 -0
  101. package/src/client/vsc/kit.tsx +46 -10
  102. package/src/client/vsc/save-shortcut.ts +47 -1
  103. package/src/client/vsc/terminal-clipboard.ts +19 -0
  104. package/src/client/vsc/terminal-line.ts +101 -0
  105. package/src/client/vsc/terminal-links.ts +166 -0
  106. package/src/client/vsc/terminal-theme.ts +97 -0
  107. package/src/client/vsc/terminal-writer.ts +112 -0
  108. package/src/client/vsc/vsc.css +1078 -181
  109. package/src/forward.ts +47 -9
  110. package/src/fs-ops.ts +124 -4
  111. package/src/git-ops.ts +20 -3
  112. package/src/http.ts +35 -4
  113. package/src/keygen.ts +55 -4
  114. package/src/monitor.ts +13 -0
  115. package/src/notices.ts +23 -1
  116. package/src/p2/helper-rpc.ts +113 -7
  117. package/src/p2/worlds.ts +14 -1
  118. package/src/pool.ts +99 -27
  119. package/src/routes.ts +212 -43
  120. package/src/sftp.ts +51 -14
  121. package/src/store.ts +10 -0
  122. package/src/terminal.ts +225 -24
  123. package/src/view-settings.ts +50 -10
  124. package/lib/types/client/Dialog.d.ts +0 -14
  125. package/src/client/Dialog.tsx +0 -76
@@ -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
- return await this.dialAndHello(conn)
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
- this.invalidate(new Error('ssh connection closed'))
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
- if (this.disposed || this.dead !== undefined) return
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) clearTimeout(this.heartbeat)
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/pool.ts CHANGED
@@ -245,6 +245,24 @@ interface Slots {
245
245
 
246
246
  export class RemotePool {
247
247
  private readonly conns = new Map<string, Pooled>()
248
+ /**
249
+ * Dedicated connections for PTY shells (one per alias).
250
+ *
251
+ * Why a SECOND connection instead of a channel on the shared one: ssh2
252
+ * multiplexes channels over ONE TCP connection, and SSH has no per-channel
253
+ * priority or flow control. A large SFTP transfer / git output on that
254
+ * connection therefore delays everything else on it — including the echo of a
255
+ * keystroke — which is exactly the "typing stutters when something else is
256
+ * moving data" report. A terminal gets its own connection, so its bytes never
257
+ * queue behind browse/exec traffic, and shells stop consuming the shared
258
+ * connection's channel budget as well.
259
+ *
260
+ * Cost: one extra SSH handshake per alias the first time a terminal opens,
261
+ * plus one held TCP connection per host with a terminal. `shell()` falls back
262
+ * to the shared connection if this one cannot be established at all, so a host
263
+ * that refuses extra connections still gets a working terminal.
264
+ */
265
+ private readonly shellConns = new Map<string, Pooled>()
248
266
  /** TOFU fingerprints observed this process (alias -> sha256). */
249
267
  private readonly hostKeys = new Map<string, string>()
250
268
  /** Per-alias `setsid --wait` capability, probed once (see supportsSetsid). */
@@ -263,23 +281,19 @@ export class RemotePool {
263
281
  // Order matters: the open browse channel must give its slot and hold back
264
282
  // while the pooled entry still exists, or the release would be a no-op.
265
283
  this.evictBrowse(alias)
266
- const pooled = this.conns.get(alias)
267
284
  // The host entry can change under an alias, so the capability answer goes
268
285
  // with the connection; the next exec probes again.
269
286
  this.setsidProbes.delete(alias)
270
- if (pooled === undefined) return
271
- this.conns.delete(alias)
272
- if (pooled.timer !== undefined) clearTimeout(pooled.timer)
273
- try {
274
- pooled.conn.end()
275
- } catch {
276
- // Already gone.
277
- }
287
+ // Both connections belong to the host: an edit/removal must end both.
288
+ // (A transient error on ONE of them uses `evictFrom` for that map only, so
289
+ // a shared-connection blip does not take live terminals down with it.)
290
+ this.evictFrom(this.conns, alias)
291
+ this.evictFrom(this.shellConns, alias)
278
292
  }
279
293
 
280
294
  /** Close every pooled connection. */
281
295
  dispose(): void {
282
- for (const alias of [...this.conns.keys()]) this.drop(alias)
296
+ for (const alias of new Set([...this.conns.keys(), ...this.shellConns.keys()])) this.drop(alias)
283
297
  }
284
298
 
285
299
  /**
@@ -316,7 +330,8 @@ export class RemotePool {
316
330
  state: entry?.state ?? 'idle',
317
331
  // connecting means a DIAL is in flight (the pooled entry exists before
318
332
  // the Client does), so that is not an open connection yet.
319
- open: pooled !== undefined && pooled.connecting === undefined,
333
+ open: (pooled !== undefined && pooled.connecting === undefined)
334
+ || (this.shellConns.get(alias)?.connecting === undefined && this.shellConns.has(alias)),
320
335
  ...(entry?.okAt === undefined ? {} : { okAt: entry.okAt }),
321
336
  ...(entry?.errorAt === undefined ? {} : { errorAt: entry.errorAt }),
322
337
  ...(entry?.error === undefined ? {} : { error: entry.error }),
@@ -646,8 +661,29 @@ export class RemotePool {
646
661
 
647
662
  /** Open an interactive PTY shell. The caller owns the channel lifecycle. */
648
663
  async shell(alias: string, opts: ShellOptions = {}): Promise<ClientChannel> {
649
- const conn = await this.acquire(alias)
650
- this.hold(alias)
664
+ /**
665
+ * Prefer the DEDICATED PTY connection so terminal bytes never queue behind
666
+ * SFTP/git/exec traffic on the shared one (SSH has no channel priority).
667
+ * A host that refuses the extra connection still gets a terminal: the fallback
668
+ * is the shared connection, exactly the old behaviour.
669
+ */
670
+ let conn: Client
671
+ let dedicated = true
672
+ try {
673
+ conn = await this.acquireShell(alias)
674
+ this.holdIn(this.shellConns, alias)
675
+ } catch (error) {
676
+ dedicated = false
677
+ // The shared path reports its own failures; a dedicated-dial failure that
678
+ // the shared one survives is not the link's problem (e.g. a MaxStartups
679
+ // refusal of just this connection).
680
+ conn = await this.acquire(alias)
681
+ this.hold(alias)
682
+ if (!(error instanceof Error) || !isConnectionFailure(error)) {
683
+ // eslint-disable-next-line no-console
684
+ console.warn('[dsh-remote-vs] dedicated PTY connection failed; using the shared one:', error)
685
+ }
686
+ }
651
687
  try {
652
688
  const channel = await new Promise<ClientChannel>((resolve, reject) => {
653
689
  conn.shell({ cols: opts.cols ?? 80, rows: opts.rows ?? 24, term: 'xterm-256color' }, (error, stream) => {
@@ -655,11 +691,15 @@ export class RemotePool {
655
691
  else resolve(stream)
656
692
  })
657
693
  })
658
- channel.once('close', () => this.release(alias))
694
+ channel.once('close', () => {
695
+ if (dedicated) this.releaseIn(this.shellConns, alias)
696
+ else this.release(alias)
697
+ })
659
698
  this.markOk(alias)
660
699
  return channel
661
700
  } catch (error) {
662
- this.release(alias)
701
+ if (dedicated) this.releaseIn(this.shellConns, alias)
702
+ else this.release(alias)
663
703
  if (isConnectionFailure(error)) this.markError(alias, error)
664
704
  throw error
665
705
  }
@@ -1299,7 +1339,11 @@ export class RemotePool {
1299
1339
 
1300
1340
  /** Hold the connection against idle eviction (balanced by release). */
1301
1341
  private hold(alias: string): void {
1302
- const pooled = this.conns.get(alias)
1342
+ this.holdIn(this.conns, alias)
1343
+ }
1344
+
1345
+ private holdIn(map: Map<string, Pooled>, alias: string): void {
1346
+ const pooled = map.get(alias)
1303
1347
  if (pooled === undefined) return
1304
1348
  pooled.held += 1
1305
1349
  pooled.lastUse = Date.now()
@@ -1310,25 +1354,40 @@ export class RemotePool {
1310
1354
  }
1311
1355
 
1312
1356
  private release(alias: string): void {
1313
- const pooled = this.conns.get(alias)
1357
+ this.releaseIn(this.conns, alias)
1358
+ }
1359
+
1360
+ private releaseIn(map: Map<string, Pooled>, alias: string): void {
1361
+ const pooled = map.get(alias)
1314
1362
  if (pooled === undefined) return
1315
1363
  if (pooled.held > 0) pooled.held -= 1
1316
1364
  pooled.lastUse = Date.now()
1317
1365
  if (pooled.held > 0) return
1318
1366
  if (pooled.timer !== undefined) clearTimeout(pooled.timer)
1319
- pooled.timer = setTimeout(() => this.drop(alias), IDLE_MS)
1367
+ // Evict ONLY this map: the idle timer of the shell connection must not close
1368
+ // the shared connection (and vice versa) — they serve different consumers.
1369
+ pooled.timer = setTimeout(() => this.evictFrom(map, alias), IDLE_MS)
1320
1370
  if (typeof pooled.timer === 'object' && 'unref' in pooled.timer) pooled.timer.unref()
1321
1371
  }
1322
1372
 
1323
1373
  private async acquire(alias: string): Promise<Client> {
1324
- const pooled = this.conns.get(alias)
1374
+ return this.acquireIn(this.conns, alias)
1375
+ }
1376
+
1377
+ /** `acquire` for the dedicated PTY connection map. */
1378
+ private async acquireShell(alias: string): Promise<Client> {
1379
+ return this.acquireIn(this.shellConns, alias)
1380
+ }
1381
+
1382
+ private async acquireIn(map: Map<string, Pooled>, alias: string): Promise<Client> {
1383
+ const pooled = map.get(alias)
1325
1384
  if (pooled !== undefined && pooled.connecting !== undefined) return pooled.connecting
1326
1385
  if (pooled !== undefined) {
1327
1386
  pooled.lastUse = Date.now()
1328
1387
  return pooled.conn
1329
1388
  }
1330
1389
  const entry: Pooled = { conn: undefined as unknown as Client, timer: undefined, lastUse: Date.now(), connecting: undefined, held: 0 }
1331
- this.conns.set(alias, entry)
1390
+ map.set(alias, entry)
1332
1391
  /**
1333
1392
  * The entry object IS the ownership token: `drop()` deletes it and a later
1334
1393
  * `acquire()` creates a NEW one, so comparing identity answers "does this
@@ -1342,7 +1401,7 @@ export class RemotePool {
1342
1401
  */
1343
1402
  entry.connecting = this.dial(alias).then(
1344
1403
  (conn) => {
1345
- if (this.conns.get(alias) !== entry) {
1404
+ if (map.get(alias) !== entry) {
1346
1405
  try {
1347
1406
  conn.end()
1348
1407
  } catch {
@@ -1357,20 +1416,20 @@ export class RemotePool {
1357
1416
  // The server went away: record it for the status chip, then evict so
1358
1417
  // the next op redials. Our OWN drop() deleted the entry first, so a
1359
1418
  // normal close (idle eviction, host removal) never lands here.
1360
- if (this.conns.get(alias) !== entry) return
1419
+ if (map.get(alias) !== entry) return
1361
1420
  this.markError(alias, new Error('connection closed'))
1362
- this.drop(alias)
1421
+ this.evictFrom(map, alias)
1363
1422
  })
1364
1423
  conn.on('error', (error: Error) => {
1365
- if (this.conns.get(alias) !== entry) return
1424
+ if (map.get(alias) !== entry) return
1366
1425
  this.markError(alias, error)
1367
- this.drop(alias)
1426
+ this.evictFrom(map, alias)
1368
1427
  })
1369
- this.release(alias)
1428
+ this.releaseIn(map, alias)
1370
1429
  return conn
1371
1430
  },
1372
1431
  (error) => {
1373
- if (this.conns.get(alias) === entry) this.conns.delete(alias)
1432
+ if (map.get(alias) === entry) map.delete(alias)
1374
1433
  this.markError(alias, error)
1375
1434
  throw error
1376
1435
  },
@@ -1378,6 +1437,19 @@ export class RemotePool {
1378
1437
  return entry.connecting
1379
1438
  }
1380
1439
 
1440
+ /** Evict ONE alias from ONE map (host removal drops both maps explicitly). */
1441
+ private evictFrom(map: Map<string, Pooled>, alias: string): void {
1442
+ const pooled = map.get(alias)
1443
+ if (pooled === undefined) return
1444
+ map.delete(alias)
1445
+ if (pooled.timer !== undefined) clearTimeout(pooled.timer)
1446
+ try {
1447
+ pooled.conn.end()
1448
+ } catch {
1449
+ // Already gone.
1450
+ }
1451
+ }
1452
+
1381
1453
  /**
1382
1454
  * The ssh2 client factory.
1383
1455
  *