dsh-remote-vs 0.28.0 → 0.30.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 (46) hide show
  1. package/README.en.md +63 -7
  2. package/README.md +51 -7
  3. package/lib/client.js +393 -112
  4. package/lib/index.js +2271 -160
  5. package/lib/p2-router-fs.js +1 -1
  6. package/lib/types/changes/recorder.d.ts +65 -10
  7. package/lib/types/changes/status.d.ts +48 -3
  8. package/lib/types/client/ConnectionsSection.d.ts +73 -0
  9. package/lib/types/client/HostsView.d.ts +139 -2
  10. package/lib/types/client/api.d.ts +65 -2
  11. package/lib/types/client/connection.d.ts +28 -1
  12. package/lib/types/client/scm/actions.d.ts +25 -1
  13. package/lib/types/forward.d.ts +11 -1
  14. package/lib/types/p2/helper-rpc.d.ts +48 -0
  15. package/lib/types/p2/worlds.d.ts +28 -0
  16. package/lib/types/pool.d.ts +337 -8
  17. package/lib/types/protocol.d.ts +218 -0
  18. package/lib/types/proxy.d.ts +136 -0
  19. package/lib/types/store.d.ts +81 -1
  20. package/lib/types/tools.d.ts +3 -0
  21. package/lib/{worlds-RG8K5eom.js → worlds-CRRZMZhg.js} +85 -6
  22. package/package.json +1 -1
  23. package/src/changes/recorder.ts +151 -15
  24. package/src/changes/status.ts +59 -4
  25. package/src/client/ConnectionStatus.tsx +19 -5
  26. package/src/client/ConnectionsSection.tsx +359 -0
  27. package/src/client/HostsView.tsx +552 -11
  28. package/src/client/MonitorView.tsx +14 -0
  29. package/src/client/PortsView.tsx +16 -0
  30. package/src/client/SettingsHosts.tsx +132 -6
  31. package/src/client/api.ts +102 -4
  32. package/src/client/connection.ts +76 -2
  33. package/src/client/i18n.ts +152 -2
  34. package/src/client/scm/actions.ts +63 -12
  35. package/src/client/vsc/vsc.css +281 -0
  36. package/src/forward.ts +38 -2
  37. package/src/importers.ts +125 -13
  38. package/src/keygen.ts +8 -1
  39. package/src/p2/helper-rpc.ts +73 -5
  40. package/src/p2/worlds.ts +48 -0
  41. package/src/pool.ts +1120 -100
  42. package/src/protocol.ts +228 -0
  43. package/src/proxy.ts +673 -0
  44. package/src/routes.ts +139 -5
  45. package/src/store.ts +335 -0
  46. package/src/tools.ts +47 -2
@@ -0,0 +1,359 @@
1
+ /**
2
+ * SSH connection-pool section shown on the monitor page, right under the
3
+ * process table.
4
+ *
5
+ * The pool is where "why is this slow" has an actual answer: one shared TCP
6
+ * connection per host carries exec/git/monitor traffic, terminals and port
7
+ * forwards dial their own, file operations ride a third, and each lane has a
8
+ * channel budget. When a budget is full the work QUEUES — and that is invisible
9
+ * from the file tree, where a slow listing just looks like a slow remote. So
10
+ * the page prints the four lanes, their channel counts, the slot occupancy and
11
+ * the route the shared connection took.
12
+ *
13
+ * Cost rules, the same ones the process section above follows:
14
+ * - collapsed by default; a collapsed section issues ZERO requests, because a
15
+ * closed diagnostic panel must not become the plugin's traffic source;
16
+ * - sampling happens only while the section is EXPANDED, the monitor panel is
17
+ * the visible view AND the page is in the foreground. There is no second
18
+ * throttle: the panel's own interval (and its pause control) is the cadence.
19
+ *
20
+ * Degradation: a Host that predates the route answers 404. That is not an error
21
+ * — the section keeps its header and prints ONE muted line saying so, instead of
22
+ * an error banner or an empty pool that would read as "nothing is connected".
23
+ * After it has said that it stops asking: the answer cannot change without an
24
+ * upgrade and a page reload.
25
+ */
26
+ import { useCallback, useEffect, useMemo, useRef, useState, type ReactNode } from 'react'
27
+ import type {
28
+ RemoteHostPoolStats,
29
+ RemoteHostSummary,
30
+ RemotePoolLane,
31
+ RemotePoolStatsResult,
32
+ } from '../protocol.ts'
33
+ import { api, RemoteApiError } from './api.ts'
34
+ import { egressTitleLine } from './connection.ts'
35
+ import { MiniSpinner } from './vsc/kit.tsx'
36
+
37
+ /** Fallback cadence when the panel has no interval of its own. */
38
+ export const POOL_POLL_MS = 5_000
39
+
40
+ /**
41
+ * Rounds waiting for a channel on ANY budget of one host.
42
+ *
43
+ * All three budgets are queues the user can be stuck behind, so the badge, the
44
+ * summary and the "a queue sorts first" rank count them together; which budget
45
+ * is full is the per-line detail below the lanes.
46
+ */
47
+ export function queuedOf(host: RemoteHostPoolStats): number {
48
+ return host.slots.queued + (host.sftpSlots?.queued ?? 0) + (host.netSlots?.queued ?? 0) + (host.gitSlots?.queued ?? 0)
49
+ }
50
+
51
+ /**
52
+ * Channels riding that host's lanes (the header summary's "N connections").
53
+ *
54
+ * The helper counts too: it is its own SSH connection with one channel, and a
55
+ * summary that said "0 connections" while a helper session was up would be the
56
+ * one number on the page that lies. Its lane is absent when the Host cannot
57
+ * report it, and then it contributes nothing.
58
+ */
59
+ export function channelsOf(host: RemoteHostPoolStats): number {
60
+ const lanes = host.connections
61
+ return lanes.shared.channels + lanes.pty.channels + lanes.forward.channels + lanes.sftp.channels
62
+ + (lanes.helper?.channels ?? 0)
63
+ }
64
+
65
+ /**
66
+ * Host order: the CURRENT host first, then a queue, then alias.
67
+ *
68
+ * The current host is the one the workspace is bound to, so its numbers are the
69
+ * answer to "why is what I am doing slow" — it belongs at the top even when it
70
+ * has nothing queued. Everything below keeps the rules this list was built on:
71
+ * a queued host outranks an idle one, and the rest is alias order.
72
+ *
73
+ * The host with work waiting behind a full budget is the one the user is
74
+ * looking for, so "has a queue" is the only promotion. The rank is a BOOLEAN on
75
+ * purpose: ranking by the queue DEPTH made rows swap places between polls (a
76
+ * host draining from 3 queued to 1 slid under a host at 2 — the same two hosts,
77
+ * a different order, nothing the user could have come to read). Everything else
78
+ * keeps a fixed order; the number itself is on the row's badge and in the
79
+ * summary. Aliases compare by code unit, not `localeCompare`: the order must
80
+ * not depend on the browser's locale.
81
+ */
82
+ export function sortPoolHosts(
83
+ hosts: readonly RemoteHostPoolStats[],
84
+ currentAlias = '',
85
+ ): RemoteHostPoolStats[] {
86
+ return [...hosts].sort((left, right) => {
87
+ // `currentAlias` is '' when no workspace is bound: nothing matches, so no
88
+ // host is promoted by identity (an alias is never the empty string).
89
+ const current = Number(right.alias === currentAlias) - Number(left.alias === currentAlias)
90
+ if (current !== 0) return current
91
+ const rank = Number(queuedOf(right) > 0) - Number(queuedOf(left) > 0)
92
+ if (rank !== 0) return rank
93
+ return left.alias < right.alias ? -1 : left.alias > right.alias ? 1 : 0
94
+ })
95
+ }
96
+
97
+ /**
98
+ * One connection kind: its name, dialled-or-not, and how many channels ride it.
99
+ * `egress` is passed for the shared lane only — it is the route that connection
100
+ * took, already worded by `connection.ts` — `fallback` for the file-transfer
101
+ * lane, which says the files are riding the shared connection instead of their
102
+ * own, and `inFlight` for the helper lane, whose `held` counts RPC calls inside
103
+ * the connection rather than a queue (so it is a clause, not a budget line).
104
+ */
105
+ function PoolLane(props: {
106
+ t: (key: string) => string
107
+ label: string
108
+ lane: RemotePoolLane
109
+ egress?: string | undefined
110
+ fallback?: string | undefined
111
+ inFlight?: string | undefined
112
+ }): ReactNode {
113
+ const t = props.t
114
+ return (
115
+ <div className="rvs-pool-lane">
116
+ <span className="rvs-pool-lane-name">{props.label}</span>
117
+ <span className={props.lane.open ? 'rvs-pool-lane-state open' : 'rvs-pool-lane-state'}>
118
+ {props.lane.open ? t('pool.laneOpen') : t('pool.laneClosed')}
119
+ {' · '}
120
+ {t('pool.channels').replace('{channels}', String(props.lane.channels))}
121
+ {props.inFlight !== undefined && (<>{' · '}{props.inFlight}</>)}
122
+ </span>
123
+ {props.egress !== undefined && (
124
+ <span className="rvs-pool-egress" title={props.egress}>{props.egress}</span>
125
+ )}
126
+ {props.fallback !== undefined && (
127
+ <span className="rvs-pool-fallback" title={props.fallback}>{props.fallback}</span>
128
+ )}
129
+ </div>
130
+ )
131
+ }
132
+
133
+ /** One host's pool: the four lanes, then its channel budget(s). */
134
+ function PoolHost(props: { t: (key: string) => string; host: RemoteHostPoolStats }): ReactNode {
135
+ const t = props.t
136
+ const host = props.host
137
+ const queued = queuedOf(host)
138
+ // The live route of the held shared connection, worded once by `connection.ts`
139
+ // (an absent egress means the shared connection is not dialled — say nothing
140
+ // rather than guess a route).
141
+ const egress = host.egress === undefined ? undefined : egressTitleLine({ egress: host.egress }, t)
142
+ // A host that refused its own SFTP connection still serves files, over the
143
+ // shared connection. Without this line the "file transfer: closed, 0 channels"
144
+ // row would read as "files are down", which is the opposite of the truth.
145
+ const fallback = host.sftpFallback === true ? t('pool.sftpFallback') : undefined
146
+ /**
147
+ * The SSH helper (the p2 transport: the file/Git fast path).
148
+ *
149
+ * It dials from `p2/worlds.ts`, not from the pool, so the key is OPTIONAL — a
150
+ * Host that cannot report it omits it rather than claiming "closed" — and its
151
+ * `held` counts RPC calls IN FLIGHT, not work waiting for a channel. That
152
+ * difference is load-bearing in two places:
153
+ * - it is a clause on the row (`3 calls in flight`), never a budget line: the
154
+ * helper has no channel limit to compare `active/limit` against;
155
+ * - it is NOT part of `queuedOf`, so a busy helper never paints a queue badge,
156
+ * never promotes its host in the ordering and never inflates the summary's
157
+ * queue count. A saturated helper is busy, not blocked.
158
+ */
159
+ const helper = host.connections.helper
160
+ const helperInFlight = helper === undefined || helper.held <= 0
161
+ ? undefined
162
+ : t('pool.helperInFlight').replace('{count}', String(helper.held))
163
+ return (
164
+ <div className="rvs-pool-host" role="group" aria-label={host.alias}>
165
+ <div className="rvs-pool-host-head">
166
+ <span className="rvs-pool-host-alias" title={host.alias}>{host.alias}</span>
167
+ {queued > 0 && (
168
+ <span className="rvs-pool-queue">{t('pool.queued').replace('{queued}', String(queued))}</span>
169
+ )}
170
+ </div>
171
+ <PoolLane t={t} label={t('pool.shared')} lane={host.connections.shared} egress={egress} />
172
+ <PoolLane t={t} label={t('pool.pty')} lane={host.connections.pty} />
173
+ <PoolLane t={t} label={t('pool.forward')} lane={host.connections.forward} />
174
+ <PoolLane t={t} label={t('pool.sftp')} lane={host.connections.sftp} fallback={fallback} />
175
+ {/* Fifth, and after the four pool lanes on purpose: the pool owns those,
176
+ the p2 layer owns this one, and an absent key means "no helper to
177
+ report" rather than "closed". */}
178
+ {helper !== undefined && (
179
+ <PoolLane t={t} label={t('pool.helper')} lane={helper} inFlight={helperInFlight} />
180
+ )}
181
+ <div className="rvs-pool-slots">
182
+ {t('pool.slots')
183
+ .replace('{active}', String(host.slots.active))
184
+ .replace('{limit}', String(host.slots.limit))
185
+ .replace('{queued}', String(host.slots.queued))}
186
+ </div>
187
+ {host.sftpSlots !== undefined && (
188
+ <div className="rvs-pool-slots">
189
+ {t('pool.sftpSlots')
190
+ .replace('{active}', String(host.sftpSlots.active))
191
+ .replace('{limit}', String(host.sftpSlots.limit))
192
+ .replace('{queued}', String(host.sftpSlots.queued))}
193
+ </div>
194
+ )}
195
+ {/* Network verbs (git fetch/pull/push) hold a channel for the whole network
196
+ timeout, so they have their own small budget; the line only appears while
197
+ one is running, which is also when the shared budget is NOT the thing
198
+ holding them up. */}
199
+ {host.netSlots !== undefined && (
200
+ <div className="rvs-pool-slots">
201
+ {t('pool.netSlots')
202
+ .replace('{active}', String(host.netSlots.active))
203
+ .replace('{limit}', String(host.netSlots.limit))
204
+ .replace('{queued}', String(host.netSlots.queued))}
205
+ </div>
206
+ )}
207
+ {/* Git commands (the `git status`/`diff`/`log` reads, the ledger's own
208
+ scans, and the SCM panel's local writes) take seconds on a big tree, so
209
+ they have a budget of their own rather than eating the exec slots the
210
+ monitor and agent tools need. Printed only while one is running or
211
+ waiting, like the two budgets above. */}
212
+ {host.gitSlots !== undefined && (
213
+ <div className="rvs-pool-slots">
214
+ {t('pool.gitSlots')
215
+ .replace('{active}', String(host.gitSlots.active))
216
+ .replace('{limit}', String(host.gitSlots.limit))
217
+ .replace('{queued}', String(host.gitSlots.queued))}
218
+ </div>
219
+ )}
220
+ </div>
221
+ )
222
+ }
223
+
224
+ export function ConnectionsSection(props: {
225
+ t: (key: string) => string
226
+ hosts: RemoteHostSummary[]
227
+ visible: boolean
228
+ paused: boolean
229
+ intervalMs: number
230
+ /** Alias of the host this workspace is bound to (sorted first). */
231
+ currentAlias?: string
232
+ }): ReactNode {
233
+ const t = props.t
234
+ const [expanded, setExpanded] = useState(false)
235
+ const [result, setResult] = useState<RemotePoolStatsResult | undefined>(undefined)
236
+ /** The Host answered 404: the route does not exist in this version. */
237
+ const [unsupported, setUnsupported] = useState(false)
238
+ const [error, setError] = useState<string | undefined>(undefined)
239
+ /** One request at a time: an interval tick cannot open a second channel. */
240
+ const inFlight = useRef(false)
241
+ const alive = useRef(true)
242
+
243
+ useEffect(() => {
244
+ alive.current = true
245
+ return () => { alive.current = false }
246
+ }, [])
247
+
248
+ const load = useCallback(async (): Promise<void> => {
249
+ if (inFlight.current) return
250
+ inFlight.current = true
251
+ try {
252
+ const next = await api.getPoolStats()
253
+ if (!alive.current) return
254
+ // `undefined` is the Host saying "I do not know this route" (404). Keep
255
+ // the header, say it once, and stop sampling (see the module docstring).
256
+ if (next === undefined) {
257
+ setUnsupported(true)
258
+ return
259
+ }
260
+ setResult(next)
261
+ setError(undefined)
262
+ } catch (caught) {
263
+ if (!alive.current) return
264
+ setError(caught instanceof RemoteApiError ? caught.message : String(caught))
265
+ } finally {
266
+ inFlight.current = false
267
+ }
268
+ }, [])
269
+
270
+ // The ONLY sampling sites: the immediate first sample plus the interval. Same
271
+ // guards as the process section above — expanded, panel visible, page not
272
+ // paused — and the tick itself re-checks `document.hidden`, so a background
273
+ // tab stops asking immediately instead of after the next interval.
274
+ useEffect(() => {
275
+ if (!expanded || !props.visible || props.paused || unsupported) return
276
+ const tick = (): void => {
277
+ if (document.hidden) return
278
+ void load()
279
+ }
280
+ tick()
281
+ const timer = window.setInterval(tick, Math.max(2_000, props.intervalMs || POOL_POLL_MS))
282
+ return () => window.clearInterval(timer)
283
+ }, [expanded, props.visible, props.paused, unsupported, load, props.intervalMs])
284
+
285
+ useEffect(() => {
286
+ if (!expanded || !props.visible || props.paused || unsupported) return
287
+ const onVisible = (): void => {
288
+ if (!document.hidden) void load()
289
+ }
290
+ document.addEventListener('visibilitychange', onVisible)
291
+ return () => document.removeEventListener('visibilitychange', onVisible)
292
+ }, [expanded, props.visible, props.paused, unsupported, load])
293
+
294
+ const currentAlias = props.currentAlias ?? ''
295
+ const hosts = useMemo(
296
+ () => sortPoolHosts(result?.hosts ?? [], currentAlias),
297
+ [result, currentAlias],
298
+ )
299
+ const summary = useMemo(() => {
300
+ if (result === undefined) return undefined
301
+ return {
302
+ hosts: hosts.length,
303
+ connections: hosts.reduce((total, host) => total + channelsOf(host), 0),
304
+ queued: hosts.reduce((total, host) => total + queuedOf(host), 0),
305
+ }
306
+ }, [result, hosts])
307
+
308
+ // Nothing to diagnose before the first host exists; the panel's own "no hosts"
309
+ // banner already explains the empty page.
310
+ if (props.hosts.length === 0) return null
311
+
312
+ const loading = expanded && !unsupported && error === undefined && result === undefined
313
+
314
+ return (
315
+ <section className={expanded ? 'rvs-pool expanded' : 'rvs-pool'} aria-label={t('pool.title')}>
316
+ <div className="rvs-pool-head">
317
+ <button
318
+ type="button"
319
+ className="rvs-pool-toggle"
320
+ aria-expanded={expanded}
321
+ onClick={() => { setExpanded(value => !value) }}
322
+ title={expanded ? t('pool.collapse') : t('pool.expand')}
323
+ >
324
+ <span aria-hidden="true">{expanded ? '▾' : '▸'}</span>
325
+ </button>
326
+ <span className="rvs-pool-title">{t('pool.title')}</span>
327
+ {summary !== undefined && (
328
+ // `aria-live` lives HERE, not on the list: the numbers above are one
329
+ // short sentence whose change is worth announcing (a queue appeared,
330
+ // a channel was freed), while the table below re-renders every 5 s —
331
+ // a live region around it makes a screen reader re-read every lane on
332
+ // each poll.
333
+ <span className="rvs-pool-summary" aria-live="polite">
334
+ {t('pool.summary')
335
+ .replace('{hosts}', String(summary.hosts))
336
+ .replace('{connections}', String(summary.connections))
337
+ .replace('{queued}', String(summary.queued))}
338
+ </span>
339
+ )}
340
+ </div>
341
+ {!expanded && <div className="rvs-pool-collapsed">{t('pool.collapsedHint')}</div>}
342
+ {expanded && unsupported && <div className="rvs-pool-note">{t('pool.unsupported')}</div>}
343
+ {expanded && !unsupported && error !== undefined && <div className="rvs-pool-note error">{error}</div>}
344
+ {loading && (
345
+ <div className="rvs-pool-note">
346
+ <MiniSpinner /> {t('pool.loading')}
347
+ </div>
348
+ )}
349
+ {expanded && !unsupported && result !== undefined && hosts.length === 0 && (
350
+ <div className="rvs-pool-note">{t('pool.empty')}</div>
351
+ )}
352
+ {expanded && hosts.length > 0 && (
353
+ <div className="rvs-pool-list rvs-scroll">
354
+ {hosts.map(host => <PoolHost key={host.alias} t={t} host={host} />)}
355
+ </div>
356
+ )}
357
+ </section>
358
+ )
359
+ }