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.
Files changed (85) hide show
  1. package/README.en.md +11 -7
  2. package/README.md +10 -7
  3. package/THIRD-PARTY-NOTICES.md +188 -0
  4. package/lib/client.js +6512 -79961
  5. package/lib/index.js +1283 -992
  6. package/lib/p2-router-fs.js +1 -1
  7. package/lib/types/client/CodeEditor.d.ts +8 -0
  8. package/lib/types/client/RemoteExplorer.d.ts +53 -0
  9. package/lib/types/client/api.d.ts +37 -2
  10. package/lib/types/client/changes/review-state.d.ts +19 -0
  11. package/lib/types/client/connection.d.ts +16 -0
  12. package/lib/types/client/request-gate.d.ts +110 -0
  13. package/lib/types/client/scm/DiffView.d.ts +45 -7
  14. package/lib/types/client/scm/actions.d.ts +31 -3
  15. package/lib/types/client/store.d.ts +53 -0
  16. package/lib/types/client/vsc/FindWidget.d.ts +6 -0
  17. package/lib/types/client/vsc/change-marks.d.ts +16 -6
  18. package/lib/types/client/vsc/editor-keymap.d.ts +35 -1
  19. package/lib/types/client/vsc/kit.d.ts +11 -0
  20. package/lib/types/client/vsc/save-shortcut.d.ts +21 -0
  21. package/lib/types/client/vsc/terminal-theme.d.ts +28 -2
  22. package/lib/types/forward.d.ts +12 -0
  23. package/lib/types/fs-ops.d.ts +42 -0
  24. package/lib/types/http.d.ts +2 -0
  25. package/lib/types/keygen.d.ts +9 -0
  26. package/lib/types/monitor.d.ts +8 -0
  27. package/lib/types/notices.d.ts +16 -1
  28. package/lib/types/p2/helper-rpc.d.ts +45 -0
  29. package/lib/types/routes.d.ts +2 -0
  30. package/lib/types/store.d.ts +7 -0
  31. package/lib/types/terminal.d.ts +8 -0
  32. package/lib/types/view-settings.d.ts +5 -0
  33. package/lib/{worlds-B7gEDBzC.js → worlds-RG8K5eom.js} +113 -15
  34. package/package.json +2 -1
  35. package/src/client/CodeEditor.tsx +151 -8
  36. package/src/client/DirectoryFlow.tsx +16 -0
  37. package/src/client/DoctorPanel.tsx +70 -7
  38. package/src/client/EditorPane.tsx +63 -14
  39. package/src/client/HostsView.tsx +12 -3
  40. package/src/client/RemoteDirectoryPicker.tsx +12 -0
  41. package/src/client/RemoteExplorer.tsx +929 -40
  42. package/src/client/RemoteRoot.tsx +147 -22
  43. package/src/client/SearchView.tsx +8 -0
  44. package/src/client/TerminalSnippets.tsx +83 -8
  45. package/src/client/api.ts +106 -13
  46. package/src/client/changes/ChangesDock.tsx +188 -6
  47. package/src/client/changes/changes.css +10 -3
  48. package/src/client/changes/review-state.ts +34 -0
  49. package/src/client/connection.ts +29 -0
  50. package/src/client/downloads.ts +49 -4
  51. package/src/client/i18n.ts +82 -8
  52. package/src/client/request-gate.ts +221 -0
  53. package/src/client/scm/CommitPane.tsx +20 -2
  54. package/src/client/scm/DiffView.tsx +343 -117
  55. package/src/client/scm/HistoryPanel.tsx +23 -2
  56. package/src/client/scm/RepoPicker.tsx +1 -1
  57. package/src/client/scm/ScmPanel.tsx +391 -87
  58. package/src/client/scm/actions.ts +95 -12
  59. package/src/client/store.tsx +372 -9
  60. package/src/client/ui.tsx +4 -0
  61. package/src/client/vsc/FindWidget.tsx +24 -9
  62. package/src/client/vsc/breadcrumb.tsx +91 -3
  63. package/src/client/vsc/change-marks.ts +93 -44
  64. package/src/client/vsc/editor-keymap.ts +59 -1
  65. package/src/client/vsc/file-icons.tsx +17 -3
  66. package/src/client/vsc/kit.tsx +26 -9
  67. package/src/client/vsc/save-shortcut.ts +47 -1
  68. package/src/client/vsc/terminal-theme.ts +44 -18
  69. package/src/client/vsc/vsc.css +411 -153
  70. package/src/forward.ts +47 -9
  71. package/src/fs-ops.ts +124 -4
  72. package/src/git-ops.ts +20 -3
  73. package/src/http.ts +35 -4
  74. package/src/keygen.ts +55 -4
  75. package/src/monitor.ts +13 -0
  76. package/src/notices.ts +23 -1
  77. package/src/p2/helper-rpc.ts +113 -7
  78. package/src/p2/worlds.ts +14 -1
  79. package/src/routes.ts +212 -43
  80. package/src/sftp.ts +51 -14
  81. package/src/store.ts +10 -0
  82. package/src/terminal.ts +73 -26
  83. package/src/view-settings.ts +50 -10
  84. package/lib/types/client/Dialog.d.ts +0 -14
  85. package/src/client/Dialog.tsx +0 -76
@@ -45,8 +45,10 @@ import type { RemoteChangedFile, RemoteChangesSummary } from '../../protocol.ts'
45
45
  import { EMPTY_CHANGE_SET, changeSetFromHunks, type ChangeSet } from './model.ts'
46
46
  import {
47
47
  isReviewed,
48
+ nextUnreviewed,
48
49
  parseReviewed,
49
50
  pruneReviewed,
51
+ reviewKeyOf,
50
52
  reviewedCount,
51
53
  serializeReviewed,
52
54
  setAllReviewed,
@@ -60,6 +62,41 @@ import './changes.css'
60
62
  /** How often an open panel re-reads the ledger (the host refreshes it per turn). */
61
63
  const POLL_MS = 3000
62
64
 
65
+ /**
66
+ * While the list is FOLDED, read the ledger only every Nth poll (15 s).
67
+ *
68
+ * The folded panel still shows its header (file count, +/− totals, the
69
+ * "pending" badge), so the poll cannot simply stop: the header would then keep
70
+ * describing an older turn. A fifth of the cadence keeps it honest without
71
+ * paying a loopback POST (and, through the host's ledger `touch`, a tracked
72
+ * git scan every 30 s) four times a second-old counter does not need.
73
+ *
74
+ * A HIDDEN page is different and skips the tick entirely: nothing on screen can
75
+ * change, and `visibilitychange` catches up at once when it is shown again.
76
+ * Manual ⟳ (`load(true)`) and the first read are never gated.
77
+ */
78
+ const FOLDED_POLL_SKIP = 5
79
+
80
+ /**
81
+ * While the panel renders NOTHING (`!visible` below and no error — no summary
82
+ * yet, or none of files/pending/unavailable), patrol the ledger only every Nth
83
+ * poll (60 s).
84
+ *
85
+ * It cannot simply stop: the panel's own visibility is DERIVED from the answer it
86
+ * polls for, so a stopped poll would never discover the first change of the next
87
+ * turn and the dock would never come back. The cadence is cut because a parked
88
+ * dock (another view in the sidebar, a session with no changes) must not pay a
89
+ * loopback POST plus the host's ledger touch every 15 s, while 60 s is still fast
90
+ * enough to surface new work without a manual refresh. The first read, the manual
91
+ * ⟳ and the fold/unfold re-read are not gated at all.
92
+ */
93
+ /**
94
+ * The dock is not rendered at all when there is nothing to show, but it must
95
+ * still notice the next change: one read every 30 s is the compromise between
96
+ * "a new change surfaces promptly" and "a hidden panel costs nothing".
97
+ */
98
+ const INVISIBLE_POLL_SKIP = 10
99
+
63
100
  /**
64
101
  * Where the review marks are remembered, per session.
65
102
  *
@@ -144,7 +181,23 @@ export function ChangesDock(props: ChangesDockProps): React.ReactNode {
144
181
  const [busy, setBusy] = useState<string | undefined>(undefined)
145
182
  /** This session's local review marks (see `review-state.ts`). */
146
183
  const [reviewed, setReviewed] = useState<Record<string, 1>>(() => loadReviewed(sessionId))
184
+ /**
185
+ * 「只看未审阅」的过滤开关(纯组件状态)。
186
+ *
187
+ * It is a reading posture for THIS pass ("I only care about what is left"),
188
+ * not a fact about the remote tree, so it is neither persisted nor sent: a
189
+ * freshly mounted panel starts by showing the whole list.
190
+ */
191
+ const [onlyUnreviewed, setOnlyUnreviewed] = useState(false)
192
+ /** 审阅游标:最近一次打开/跳到的行(`reviewKeyOf` 的键)。 */
193
+ const [cursorKey, setCursorKey] = useState<string | undefined>(undefined)
194
+ /** 行元素(键同游标),供「跳到下一个未审阅」把目标滚进视野。 */
195
+ const rowRefs = useRef(new Map<string, HTMLDivElement>())
147
196
  const alive = useRef(true)
197
+ // Mirrors "the panel renders something" for the poll tick: adding that as an
198
+ // effect dependency would rebuild the interval (and pay a re-read) every time
199
+ // the panel appears or disappears, which is not what the cadence is about.
200
+ const visibleRef = useRef(false)
148
201
  const currentSession = useRef(sessionId)
149
202
  currentSession.current = sessionId
150
203
  const rowMenu = useContextMenu()
@@ -184,18 +237,43 @@ export function ChangesDock(props: ChangesDockProps): React.ReactNode {
184
237
 
185
238
  useEffect(() => {
186
239
  alive.current = true
240
+ // The first read (and the manual ⟳, which calls `load(true)` directly) is
241
+ // deliberately NOT gated: it is what paints the panel after a session
242
+ // switch or a cold page.
187
243
  void load()
188
- const timer = window.setInterval(() => { void load() }, POLL_MS)
244
+ let ticks = 0
245
+ const timer = window.setInterval(() => {
246
+ // A background tab costs a loopback POST every 3 s without any possible
247
+ // visible change; skip the tick outright. The listener below is what makes
248
+ // the panel catch up the moment it is shown again.
249
+ if (document.hidden) return
250
+ ticks += 1
251
+ // An unrendered panel is slower than a folded one: it still has to look for
252
+ // the next turn's changes (see INVISIBLE_POLL_SKIP), just not at the folded
253
+ // cadence. The first read, the fold re-read and the manual ⟳ stay outside
254
+ // this tick.
255
+ const skip = visibleRef.current ? (collapsed ? FOLDED_POLL_SKIP : 1) : INVISIBLE_POLL_SKIP
256
+ if (skip > 1 && ticks % skip !== 0) return
257
+ void load()
258
+ }, POLL_MS)
259
+ // Shown again, or unfolded: read at once instead of waiting for the next
260
+ // tick (a folded panel may be up to 15 s stale by design). `collapsed` is a
261
+ // dependency, so folding/unfolding also re-runs this effect and reloads.
262
+ const wake = (): void => { if (!document.hidden) void load() }
263
+ document.addEventListener('visibilitychange', wake)
189
264
  return () => {
190
265
  alive.current = false
191
266
  window.clearInterval(timer)
267
+ document.removeEventListener('visibilitychange', wake)
192
268
  }
193
- }, [load])
269
+ }, [load, collapsed])
194
270
 
195
- // A different conversation has different marks: reload them instead of carrying
196
- // one session's ✓ onto another's files.
271
+ // A different conversation has different marks (and its own place in the
272
+ // list): reload instead of carrying one session's ✓ / cursor onto another's
273
+ // files.
197
274
  useEffect(() => {
198
275
  setReviewed(loadReviewed(sessionId))
276
+ setCursorKey(undefined)
199
277
  }, [sessionId])
200
278
 
201
279
  /**
@@ -303,6 +381,10 @@ export function ChangesDock(props: ChangesDockProps): React.ReactNode {
303
381
  /** Open one listed file in the remote editor with its change marks. */
304
382
  const openChanged = useCallback(async (file: RemoteChangedFile): Promise<void> => {
305
383
  if (sessionId === undefined || summary === null) return
384
+ // Opening a row also moves the review cursor, so 「跳到下一个未审阅」continues
385
+ // from whatever the user just looked at (a click, a menu entry or a jump)
386
+ // instead of from wherever the last jump happened to stop.
387
+ setCursorKey(reviewKeyOf(file))
306
388
  setBusy(file.path)
307
389
  try {
308
390
  const diff = await api.changes.diff(sessionId, file.path, repoSubdirOf())
@@ -392,14 +474,51 @@ export function ChangesDock(props: ChangesDockProps): React.ReactNode {
392
474
  // Drop marks for rows that are gone (or whose counts changed). Without this a
393
475
  // path that leaves and comes back would show a ✓ nobody gave it.
394
476
  useEffect(() => {
477
+ // `files` is EMPTY until the first summary lands — that is "not loaded yet",
478
+ // not "the session changed nothing". Pruning on that frame wiped the marks
479
+ // just restored from localStorage (and wrote the empty map back).
480
+ if (summary === null) return
395
481
  setReviewed(previous => {
396
482
  const next = pruneReviewed(files, previous)
397
483
  // A prune only ever removes entries, so equal length means "identical".
398
484
  return Object.keys(next).length === Object.keys(previous).length ? previous : next
399
485
  })
400
- }, [files])
486
+ }, [files, summary])
487
+
488
+ /**
489
+ * Bring the cursor's row into view.
490
+ *
491
+ * The jump can be pressed while the list is FOLDED, where the row does not
492
+ * exist yet in that commit: the effect runs after the unfold has landed, so
493
+ * the ref map has the row by then. `block: 'nearest'` means an already visible
494
+ * row does not move at all, which is what makes a plain row click (which also
495
+ * moves the cursor) a no-op here. Optional call: a DOM without `scrollIntoView`
496
+ * (tests) must cost only the scroll.
497
+ */
498
+ useEffect(() => {
499
+ if (cursorKey === undefined || collapsed) return
500
+ rowRefs.current.get(cursorKey)?.scrollIntoView?.({ block: 'nearest' })
501
+ }, [cursorKey, collapsed])
401
502
 
402
503
  const done = reviewedCount(files, reviewed)
504
+ /**
505
+ * Rows the body renders: the whole list, or only the rows that are still
506
+ * unreviewed. The header's counters keep describing the whole list — the
507
+ * filter is a view, not a recount.
508
+ */
509
+ const listed = useMemo(
510
+ () => (onlyUnreviewed ? files.filter(file => !isReviewed(file, reviewed)) : files),
511
+ [files, onlyUnreviewed, reviewed],
512
+ )
513
+ /**
514
+ * What 「跳到下一个未审阅」would open; `undefined` both when the list is empty
515
+ * and when every row is reviewed, which is exactly the button's disabled
516
+ * condition. `fromKey` is the cursor, so a second press continues the walk.
517
+ */
518
+ const nextFile = useMemo(
519
+ () => nextUnreviewed(files, reviewed, cursorKey),
520
+ [files, reviewed, cursorKey],
521
+ )
403
522
 
404
523
  const openRowMenu = useCallback((event: React.MouseEvent, file: RemoteChangedFile): void => {
405
524
  event.preventDefault()
@@ -429,11 +548,30 @@ export function ChangesDock(props: ChangesDockProps): React.ReactNode {
429
548
  ])
430
549
  }, [files, reviewMenu, t])
431
550
 
551
+ /**
552
+ * 「跳到下一个未审阅」:把下一行滚进视野并打开它。
553
+ *
554
+ * 这一跳不标记任何东西:打开就是它的全部动作,看过没有由用户按 ✓ 决定。折叠着
555
+ * 的列表先展开,否则只是换了个看不见的游标。打开走 `openChanged`(改动内容、
556
+ * 绑定解析与撤销来源都在那一条路上),它同时把游标移到目标行,所以连按两次是
557
+ * 继续往下走,而不是原地打转。
558
+ */
559
+ const jumpToNextUnreviewed = useCallback((): void => {
560
+ if (nextFile === undefined) return
561
+ setCollapsed(false)
562
+ void openChanged(nextFile)
563
+ }, [nextFile, openChanged])
564
+
432
565
  const activeTab = explorer.tabs.find(candidate => candidate.id === explorer.activeTabId)
433
566
  const visible = useMemo(() => {
434
567
  if (summary === null) return false
435
568
  return files.length > 0 || summary.pending === true || summary.unavailable !== undefined
436
569
  }, [files.length, summary])
570
+ // Published for the tick (see `visibleRef`): read at tick time, not captured at
571
+ // effect time, so the cadence follows the panel without restarting the timer.
572
+ // "Rendered" mirrors the early return below: an error is shown even without a
573
+ // summary, so a failed panel keeps the near cadence (its ⟳ is on screen).
574
+ visibleRef.current = visible || error !== undefined
437
575
 
438
576
  if (sessionId === undefined || sessionId === '') return null
439
577
  if (!visible && error === undefined) return null
@@ -470,6 +608,38 @@ export function ChangesDock(props: ChangesDockProps): React.ReactNode {
470
608
  {t('changes.reviewCount').replace('{done}', String(done)).replace('{total}', String(files.length))}
471
609
  </button>
472
610
  )}
611
+ {files.length > 0 && (
612
+ <>
613
+ {/*
614
+ 「只看未审阅」: the header's small-chip style (`rvs-changes-reviewed`)
615
+ is the one text button this panel's palette already carries. Its
616
+ "on" state is painted from the same two variables it reads, since
617
+ that chip rule only describes the idle look.
618
+ */}
619
+ <button
620
+ type="button"
621
+ className="rvs-changes-reviewed"
622
+ aria-pressed={onlyUnreviewed}
623
+ title={t('changes.onlyUnreviewed')}
624
+ onClick={() => { setOnlyUnreviewed(value => !value) }}
625
+ style={onlyUnreviewed ? { backgroundColor: 'var(--rvs-chg-hover)', color: 'var(--rvs-chg-fg)' } : undefined}
626
+ >
627
+ {t('changes.onlyUnreviewed')}
628
+ </button>
629
+ {/* 跳到下一个未审阅: one glyph (the words live in the tooltip), disabled
630
+ when nothing is left. */}
631
+ <span className="rvs-changes-nav">
632
+ <button
633
+ type="button"
634
+ title={nextFile === undefined ? t('changes.allReviewed') : t('changes.nextUnreviewed')}
635
+ aria-label={t('changes.nextUnreviewed')}
636
+ disabled={nextFile === undefined}
637
+ onClick={jumpToNextUnreviewed}
638
+ style={nextFile === undefined ? { cursor: 'default', opacity: 0.4 } : undefined}
639
+ >↓</button>
640
+ </span>
641
+ </>
642
+ )}
473
643
  {summary?.pending === true && <span className="rvs-changes-pending">{t('changes.pending')}</span>}
474
644
  <span className="rvs-changes-spacer" />
475
645
  {stale && <span className="rvs-changes-stale" title={t('changes.staleHint')}>{t('changes.stale')}</span>}
@@ -489,12 +659,24 @@ export function ChangesDock(props: ChangesDockProps): React.ReactNode {
489
659
  {files.length === 0 && summary?.unavailable === undefined && summary?.pending !== true && (
490
660
  <div className="rvs-changes-note">{t('changes.empty')}</div>
491
661
  )}
492
- {files.map(file => {
662
+ {/*
663
+ The filter can hide every row; an empty body must then say WHY instead
664
+ of looking like a broken list.
665
+ */}
666
+ {files.length > 0 && listed.length === 0 && (
667
+ <div className="rvs-changes-note">{t('changes.allReviewed')}</div>
668
+ )}
669
+ {listed.map(file => {
493
670
  const { dir, name } = splitDisplay(file.display)
494
671
  const marked = isReviewed(file, reviewed)
672
+ const key = reviewKeyOf(file)
495
673
  return (
496
674
  <div
497
675
  key={file.path}
676
+ ref={(node) => {
677
+ if (node === null) rowRefs.current.delete(key)
678
+ else rowRefs.current.set(key, node)
679
+ }}
498
680
  className={`rvs-chg-row${busy === file.path ? ' rvs-chg-loading' : ''}`}
499
681
  data-reviewed={marked ? '1' : '0'}
500
682
  >
@@ -16,7 +16,10 @@
16
16
  --rvs-chg-hover: var(--dsw-alias-interactive-bg-hover, rgba(127, 127, 127, .14));
17
17
  --rvs-chg-surface: var(--dsw-alias-bg-layer-2, rgba(127, 127, 127, .07));
18
18
  --rvs-chg-add: #4ec9b0;
19
- --rvs-chg-del: #f14c4c;
19
+ /* #f14c4c on the dark surface is 3.90:1 / 3.79:1 — below AA for 11px counts.
20
+ One step lighter in the same hue family (6.18 / 6.02) without touching the
21
+ light value, which is already a GitHub-Light red. */
22
+ --rvs-chg-del: #ff8b8b;
20
23
  --rvs-chg-mod: #e2b714;
21
24
  --rvs-chg-info: #58a6ff;
22
25
 
@@ -313,9 +316,13 @@ body:not([data-ds-dark-theme]) .rvs-changes {
313
316
  --rvs-chg-border: var(--dsw-alias-border-l1, rgba(31, 35, 40, .16));
314
317
  --rvs-chg-hover: var(--dsw-alias-interactive-bg-hover, rgba(31, 35, 40, .07));
315
318
  --rvs-chg-surface: var(--dsw-alias-bg-layer-2, rgba(31, 35, 40, .04));
316
- --rvs-chg-add: #1a7f37;
319
+ /* Deeper than GitHub's #1a7f37: the skin's cream surface eats ~0.8:1 off it
320
+ (4.50:1 there, 5.08 on white), so the same hue one step down (5.79 / 6.53).
321
+ The amber "M" has the same island problem (4.32 vs 4.87) and takes the same
322
+ step (6.05 / 6.82). */
323
+ --rvs-chg-add: #146c2e;
317
324
  --rvs-chg-del: #cf222e;
318
- --rvs-chg-mod: #9a6700;
325
+ --rvs-chg-mod: #7d5200;
319
326
  --rvs-chg-info: #0969da;
320
327
  }
321
328
 
@@ -57,6 +57,40 @@ export function reviewedCount(files: readonly ReviewSubject[], reviewed: Reviewe
57
57
  return count
58
58
  }
59
59
 
60
+ /**
61
+ * 从 `fromKey` 之后找第一个未审阅的行 —— 面板头部「跳到下一个未审阅」的取数。
62
+ *
63
+ * 顺序就是列表本身的顺序:从游标位置往**后**线性找,走到末尾回绕到开头,最多看
64
+ * 一圈,因此不会重复、也不会越界。跳过已审阅的行是它的职责;它本身不改任何状态,
65
+ * 也不负责打开文件(那是面板的事)。
66
+ *
67
+ * `fromKey` 缺失、或已经不在列表里(行被删了、文件又改了一次导致计数进键变化)时
68
+ * 从第一行开始:起点未知只会让这一跳从头枚举,绝不会漏掉任何一行。
69
+ *
70
+ * 返回值与入参同一行类型(`T extends ReviewSubject`),所以面板拿回的是它自己的
71
+ * `RemoteChangedFile`,不需要在调用处断言一次。
72
+ *
73
+ * @param files - 面板当前的行,数组顺序即导航顺序。
74
+ * @param reviewed - 已审阅集合(键见 {@link reviewKeyOf})。
75
+ * @param fromKey - 当前游标(`reviewKeyOf` 给出的键);空/未知表示从头开始。
76
+ * @returns 第一个未审阅的行;空列表或全部已审阅时 `undefined`。
77
+ */
78
+ export function nextUnreviewed<T extends ReviewSubject>(
79
+ files: readonly T[],
80
+ reviewed: ReviewedMap,
81
+ fromKey?: string,
82
+ ): T | undefined {
83
+ if (files.length === 0) return undefined
84
+ const current = fromKey === undefined || fromKey === '' ? -1 : files.findIndex(file => reviewKeyOf(file) === fromKey)
85
+ // One past the row the cursor is on; an unknown start means "from the top".
86
+ const start = current + 1
87
+ for (let offset = 0; offset < files.length; offset += 1) {
88
+ const file = files[(start + offset) % files.length]
89
+ if (!isReviewed(file, reviewed)) return file
90
+ }
91
+ return undefined
92
+ }
93
+
60
94
  /** 切换一行的标记,返回新表(输入不被修改)。 */
61
95
  export function toggleReviewed(files: readonly ReviewSubject[], reviewed: ReviewedMap, file: ReviewSubject): Record<string, 1> {
62
96
  const key = reviewKeyOf(file)
@@ -206,6 +206,35 @@ export function markConnectionProbeOk(alias: string): void {
206
206
  probes.set(alias, Date.now())
207
207
  }
208
208
 
209
+ /**
210
+ * Forget the cached record of every alias the hosts table no longer holds.
211
+ *
212
+ * `views`/`probes` are module-level maps keyed by alias and nothing ever removed
213
+ * an entry: deleting or renaming a host in Settings left its view (and its
214
+ * explicit-retry probe) in page memory for the life of the tab. Callers pass the
215
+ * aliases the hosts table currently holds; everything else is stale.
216
+ *
217
+ * Only cache entries are removed — no connection state is transitioned. When the
218
+ * ACTIVE alias is pruned its snapshot becomes `undefined` ("no evidence yet",
219
+ * which every reader already treats as "not proven down") and subscribers are
220
+ * woken. `activeAlias` itself is deliberately KEPT: `setActiveConnectionAlias`
221
+ * early-returns on an unchanged alias, so clearing it here would leave a
222
+ * re-added host's chip blank until the user switched workspaces.
223
+ */
224
+ export function pruneConnections(aliases: Iterable<string>): void {
225
+ const keep = new Set(aliases)
226
+ let dropped = false
227
+ for (const alias of [...views.keys()]) {
228
+ if (keep.has(alias)) continue
229
+ views.delete(alias)
230
+ dropped = true
231
+ }
232
+ for (const alias of [...probes.keys()]) {
233
+ if (!keep.has(alias)) probes.delete(alias)
234
+ }
235
+ if (dropped) refreshSnapshot()
236
+ }
237
+
209
238
  /** Where the last observed state came from (see `isConnectionRecovery`). */
210
239
  export interface ConnectionTrace {
211
240
  alias: string
@@ -88,13 +88,55 @@ const listeners = new Set<() => void>()
88
88
  const controllers = new Map<string, AbortController>()
89
89
  let sequence = 0
90
90
 
91
- const notify = (): void => {
91
+ /**
92
+ * Minimum gap between two PROGRESS notifications, in ms.
93
+ *
94
+ * The transfer callbacks fire once per fetched chunk (commonly 64 KiB), so a
95
+ * fast download notified the dock — and re-rendered every row it draws —
96
+ * hundreds of times per second, while the bar moves by less than a pixel.
97
+ * Coalescing on a ~10 Hz cadence keeps the bar visibly live and the task table
98
+ * synchronous: `tasks` is still patched on every chunk, only the "something
99
+ * changed" signal is rate-limited. A terminal status NEVER waits for this timer
100
+ * (see `notify`) — a finished task must appear finished at once.
101
+ */
102
+ const PROGRESS_NOTIFY_MS = 100
103
+
104
+ /** The pending coalesced notification, if one is scheduled. */
105
+ let progressTimer: ReturnType<typeof setTimeout> | undefined
106
+
107
+ const emit = (): void => {
92
108
  for (const listener of [...listeners]) listener()
93
109
  }
94
110
 
95
- const patch = (id: string, next: Partial<DownloadTask>): void => {
111
+ /** Immediate notification: drops any queued progress tick, so final states win. */
112
+ const notify = (): void => {
113
+ if (progressTimer !== undefined) {
114
+ clearTimeout(progressTimer)
115
+ progressTimer = undefined
116
+ }
117
+ emit()
118
+ }
119
+
120
+ /** At most one queued notification per `PROGRESS_NOTIFY_MS` window. */
121
+ const notifyProgress = (): void => {
122
+ if (progressTimer !== undefined) return
123
+ progressTimer = setTimeout(() => {
124
+ progressTimer = undefined
125
+ emit()
126
+ }, PROGRESS_NOTIFY_MS)
127
+ }
128
+
129
+ /**
130
+ * Patch one task and notify.
131
+ *
132
+ * `coalesce` is for the per-chunk progress path only: it updates the snapshot
133
+ * at once (readers/`waitTask` see it) but announces the change on the throttled
134
+ * cadence. Every status transition stays on the immediate path.
135
+ */
136
+ const patch = (id: string, next: Partial<DownloadTask>, coalesce = false): void => {
96
137
  tasks = tasks.map(task => (task.id === id ? { ...task, ...next } : task))
97
- notify()
138
+ if (coalesce) notifyProgress()
139
+ else notify()
98
140
  }
99
141
 
100
142
  /** Store surface: `useSyncExternalStore(subscribe, getSnapshot, getSnapshot)`. */
@@ -129,10 +171,13 @@ const launch = (id: string, controller: AbortController, run: (options: RunnerOp
129
171
  signal: controller.signal,
130
172
  onProgress: (progress) => {
131
173
  if (controller.signal.aborted) return
174
+ // The task table is patched on every chunk; only the dock's repaint is
175
+ // coalesced (see PROGRESS_NOTIFY_MS) — a dropped chunk would make the
176
+ // byte counter lag its own progress bar.
132
177
  patch(id, {
133
178
  loaded: progress.loaded,
134
179
  ...(progress.total === undefined ? {} : { total: progress.total }),
135
- })
180
+ }, true)
136
181
  },
137
182
  })
138
183
  if (outcome === 'handedOff') {