tmux-ide 2.9.21 → 2.9.23

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 (131) hide show
  1. package/bin/cli.js +6235 -8473
  2. package/package.json +3 -3
  3. package/packages/contracts/src/index.ts +2 -0
  4. package/packages/contracts/src/pane-reader.ts +121 -0
  5. package/packages/contracts/src/pane-stream.ts +18 -0
  6. package/packages/contracts/src/visual-theme-presets.ts +58 -2
  7. package/packages/contracts/src/websocket-heartbeat.ts +41 -0
  8. package/packages/daemon/dist/command-center/automation.js +1 -1
  9. package/packages/daemon/dist/command-center/daemon-fleet-facts-observer.js +37 -5
  10. package/packages/daemon/dist/command-center/discovery.js +28 -2
  11. package/packages/daemon/dist/command-center/server.js +8 -2
  12. package/packages/daemon/dist/command-center/tmux-server-reader.js +41 -0
  13. package/packages/daemon/dist/command-center/tmux-servers.js +7 -1
  14. package/packages/daemon/dist/doctor.js +147 -43
  15. package/packages/daemon/dist/lib/bounded-tmux-read.js +34 -12
  16. package/packages/daemon/dist/lib/cli-command.js +13 -0
  17. package/packages/daemon/dist/lib/daemon-embed.js +126 -55
  18. package/packages/daemon/dist/lib/daemon-stop-progress.js +58 -0
  19. package/packages/daemon/dist/lib/embedded-daemon-lifecycle.js +11 -0
  20. package/packages/daemon/dist/lib/embedded-tmux-server-owners.js +88 -12
  21. package/packages/daemon/dist/lib/headless-daemon.js +177 -10
  22. package/packages/daemon/dist/lib/log.js +18 -1
  23. package/packages/daemon/dist/lib/pane-reader-files.js +226 -0
  24. package/packages/daemon/dist/lib/remote-tmux-command.js +8 -5
  25. package/packages/daemon/dist/lib/tmux-client-execution.js +73 -7
  26. package/packages/daemon/dist/lib/tmux-external-interaction-observer.js +140 -19
  27. package/packages/daemon/dist/lib/tmux-native-browser.js +4 -2
  28. package/packages/daemon/dist/lib/tmux-pane-reader.js +107 -0
  29. package/packages/daemon/dist/lib/tmux-server-agent-admission.js +6 -1
  30. package/packages/daemon/dist/lib/tmux-server-features.js +38 -0
  31. package/packages/daemon/dist/lib/tmux-server-owner-teardown.js +33 -0
  32. package/packages/daemon/dist/lib/tmux-server-owner.js +38 -37
  33. package/packages/daemon/dist/lib/tmux-server-owners.js +310 -20
  34. package/packages/daemon/dist/lib/tmux-server-registration.js +128 -9
  35. package/packages/daemon/dist/lib/unix-socket-authority.js +58 -3
  36. package/packages/daemon/dist/lib/workspace-pane-creation.js +10 -1
  37. package/packages/daemon/dist/native/TmuxIdeNotifier.app/Contents/Info.plist +2 -2
  38. package/packages/daemon/dist/native/TmuxIdeNotifier.app/Contents/MacOS/tmux-ide-notifier +0 -0
  39. package/packages/daemon/dist/native/TmuxIdeNotifier.app/Contents/Resources/Assets.car +0 -0
  40. package/packages/daemon/dist/native/TmuxIdeNotifier.app/Contents/_CodeSignature/CodeResources +2 -2
  41. package/packages/daemon/dist/open-browser.js +26 -12
  42. package/packages/daemon/dist/open-reader.js +84 -0
  43. package/packages/daemon/dist/plugins/cli.js +1 -2
  44. package/packages/daemon/dist/plugins/daemon-plugins.js +2 -10
  45. package/packages/daemon/dist/restore.js +57 -40
  46. package/packages/daemon/dist/server/pane-stream-upgrade.js +5 -1
  47. package/packages/daemon/dist/server/websocket-heartbeat.js +103 -0
  48. package/packages/daemon/dist/terminal/attachments/native-runtime.js +32 -14
  49. package/packages/daemon/dist/terminal/mirror/layout-content-rows.js +11 -0
  50. package/packages/daemon/dist/terminal/mirror/mirror-service.js +61 -3
  51. package/packages/daemon/dist/terminal/mirror/session-channel.js +13 -12
  52. package/packages/daemon/dist/terminal/pane-stream/pane-stream-websocket.js +123 -25
  53. package/packages/daemon/dist/terminal/pane-stream/runtime.js +3 -2
  54. package/packages/daemon/dist/terminal/protocol/reader-option.js +66 -0
  55. package/packages/daemon/dist/terminal/protocol/session-descriptor-discovery.js +30 -2
  56. package/packages/daemon/dist/terminal/protocol/stock-split-resize.js +4 -1
  57. package/packages/daemon/dist/terminal/session-runtime/registry.js +17 -0
  58. package/packages/daemon/dist/terminal/session-runtime/terminal-replica-interpreter.js +81 -3
  59. package/packages/daemon/dist/terminal/session-runtime/terminal-replica-owner.js +60 -3
  60. package/packages/daemon/dist/tmux-upgrade/cli.js +106 -0
  61. package/packages/daemon/dist/tmux-upgrade/io.js +540 -0
  62. package/packages/daemon/dist/tmux-upgrade/plan.js +238 -0
  63. package/packages/daemon/dist/tmux-upgrade/run.js +199 -0
  64. package/packages/daemon/dist/tui/chrome/snapshot.js +3 -4
  65. package/packages/daemon/dist/tui/mirror/runtime/application-home-agent-navigation.js +64 -4
  66. package/packages/daemon/dist/tui/mirror/runtime/application-home-agents-owner.js +35 -5
  67. package/packages/daemon/dist/tui/mirror/runtime/application-machine-agent-navigation.js +12 -8
  68. package/packages/daemon/dist/tui/mirror/runtime/application-terminal-interaction-controller.js +125 -111
  69. package/packages/daemon/src/command-center/automation.ts +1 -1
  70. package/packages/daemon/src/command-center/daemon-fleet-facts-observer.ts +49 -8
  71. package/packages/daemon/src/command-center/discovery.ts +35 -2
  72. package/packages/daemon/src/command-center/server.ts +22 -8
  73. package/packages/daemon/src/command-center/tmux-server-reader.ts +62 -0
  74. package/packages/daemon/src/command-center/tmux-servers.ts +7 -1
  75. package/packages/daemon/src/doctor.ts +164 -45
  76. package/packages/daemon/src/lib/bounded-tmux-read.ts +29 -9
  77. package/packages/daemon/src/lib/cli-command.ts +16 -0
  78. package/packages/daemon/src/lib/daemon-embed.ts +176 -64
  79. package/packages/daemon/src/lib/daemon-stop-progress.ts +78 -0
  80. package/packages/daemon/src/lib/embedded-daemon-lifecycle.ts +12 -0
  81. package/packages/daemon/src/lib/embedded-tmux-server-owners.ts +115 -11
  82. package/packages/daemon/src/lib/headless-daemon.ts +205 -12
  83. package/packages/daemon/src/lib/log.ts +18 -1
  84. package/packages/daemon/src/lib/pane-reader-files.ts +294 -0
  85. package/packages/daemon/src/lib/remote-tmux-command.ts +13 -6
  86. package/packages/daemon/src/lib/tmux-client-execution.ts +79 -7
  87. package/packages/daemon/src/lib/tmux-external-interaction-observer.ts +163 -25
  88. package/packages/daemon/src/lib/tmux-native-browser.ts +4 -3
  89. package/packages/daemon/src/lib/tmux-pane-reader.ts +145 -0
  90. package/packages/daemon/src/lib/tmux-server-agent-admission.ts +6 -1
  91. package/packages/daemon/src/lib/tmux-server-features.ts +43 -0
  92. package/packages/daemon/src/lib/tmux-server-owner-teardown.ts +54 -0
  93. package/packages/daemon/src/lib/tmux-server-owner.ts +41 -35
  94. package/packages/daemon/src/lib/tmux-server-owners.ts +427 -22
  95. package/packages/daemon/src/lib/tmux-server-registration.ts +155 -7
  96. package/packages/daemon/src/lib/unix-socket-authority.ts +72 -3
  97. package/packages/daemon/src/lib/workspace-pane-creation.ts +11 -1
  98. package/packages/daemon/src/open-browser.ts +59 -18
  99. package/packages/daemon/src/open-reader.ts +148 -0
  100. package/packages/daemon/src/plugins/cli.ts +1 -2
  101. package/packages/daemon/src/plugins/daemon-plugins.ts +2 -12
  102. package/packages/daemon/src/restore.ts +80 -44
  103. package/packages/daemon/src/server/pane-stream-upgrade.ts +6 -0
  104. package/packages/daemon/src/server/websocket-heartbeat.ts +135 -0
  105. package/packages/daemon/src/terminal/attachments/native-runtime.ts +58 -14
  106. package/packages/daemon/src/terminal/mirror/events.ts +2 -0
  107. package/packages/daemon/src/terminal/mirror/layout-content-rows.ts +11 -0
  108. package/packages/daemon/src/terminal/mirror/mirror-service.ts +61 -2
  109. package/packages/daemon/src/terminal/mirror/session-channel.ts +15 -14
  110. package/packages/daemon/src/terminal/pane-stream/pane-stream-websocket.ts +140 -23
  111. package/packages/daemon/src/terminal/pane-stream/runtime.ts +4 -2
  112. package/packages/daemon/src/terminal/protocol/reader-option.ts +77 -0
  113. package/packages/daemon/src/terminal/protocol/session-descriptor-discovery.ts +41 -1
  114. package/packages/daemon/src/terminal/protocol/stock-split-resize.ts +6 -1
  115. package/packages/daemon/src/terminal/session-runtime/registry.ts +23 -0
  116. package/packages/daemon/src/terminal/session-runtime/runtime-observability.ts +2 -0
  117. package/packages/daemon/src/terminal/session-runtime/terminal-replica-interpreter.ts +100 -3
  118. package/packages/daemon/src/terminal/session-runtime/terminal-replica-owner.ts +60 -3
  119. package/packages/daemon/src/tmux-upgrade/cli.ts +118 -0
  120. package/packages/daemon/src/tmux-upgrade/io.ts +596 -0
  121. package/packages/daemon/src/tmux-upgrade/plan.ts +353 -0
  122. package/packages/daemon/src/tmux-upgrade/run.ts +325 -0
  123. package/packages/daemon/src/tui/chrome/snapshot.ts +3 -4
  124. package/packages/daemon/src/tui/mirror/runtime/application-home-agent-navigation.ts +110 -10
  125. package/packages/daemon/src/tui/mirror/runtime/application-home-agents-owner.ts +58 -6
  126. package/packages/daemon/src/tui/mirror/runtime/application-machine-agent-navigation.ts +20 -9
  127. package/packages/daemon/src/tui/mirror/runtime/application-root-v2.tsx +2 -2
  128. package/packages/daemon/src/tui/mirror/runtime/application-terminal-interaction-controller.ts +149 -128
  129. package/packages/daemon-client/src/direct-application-shell-transport.ts +21 -3
  130. package/packages/daemon-client/src/workspace-event-supervisor.ts +63 -2
  131. package/packages/tmux-bridge/src/runner.ts +3 -1
@@ -117,6 +117,8 @@ export interface SessionRuntimeStageSpan {
117
117
  | "panes-closed"
118
118
  | "peer-closed"
119
119
  | "daemon-shutdown"
120
+ | "tmux-server-retired"
121
+ | "daemon-restarting"
120
122
  | "redemption-rejected"
121
123
  | "unknown";
122
124
  readonly deliveryVisibility?: "visible" | "background" | "hidden" | "frozen";
@@ -56,6 +56,31 @@ import type { TerminalGraphicsState } from "@tmux-ide/contracts";
56
56
  // Bound parser work per turn without changing byte-stream semantics. This is a
57
57
  // batch bound, not a producer queue cap: upstream pressure must preserve bytes.
58
58
  const MAX_INGEST_BATCH_BYTES = 256 * 1024;
59
+ /**
60
+ * Under a flood, publish at most one commit per display interval (daemon perf
61
+ * F1). Each commit projects, diffs and hashes the replica; committing after
62
+ * every parsed chunk kept the daemon off tmux's control pipe, so tmux itself
63
+ * ran about 4× slower while a pane was viewed. Rows that scroll through
64
+ * history between two commits are never projected or hashed. A write with no
65
+ * output queued behind it (a keystroke echo, the end of a burst) still
66
+ * commits at once.
67
+ */
68
+ export const COALESCED_COMMIT_INTERVAL_MS = 16;
69
+ /**
70
+ * A flood commit costs far more than the display interval (about 35 ms with a
71
+ * full 2,000-row history), so an interval counted from the commit's start
72
+ * alone never skips anything: the next chunk always arrives "late". After a
73
+ * commit the interpreter therefore also reads queued output for this many
74
+ * times the commit's own cost before it commits again, so at most one part in
75
+ * (1 + factor) of a flood goes to commits (half, at 1).
76
+ */
77
+ export const COALESCED_COMMIT_REST_FACTOR = 1;
78
+ /**
79
+ * Upper bound of that rest. A skipped frame can still wait longer when the
80
+ * queued write behind it opens a synchronized update: then it is published
81
+ * with that update, at its end or by the 250 ms sync recovery.
82
+ */
83
+ export const COALESCED_COMMIT_MAX_REST_MS = 100;
59
84
  type PendingWrite = {
60
85
  data: Uint8Array;
61
86
  offset: number;
@@ -184,6 +209,11 @@ export class TerminalReplicaInterpreter {
184
209
  #needsSeed = true;
185
210
  #closed = false;
186
211
  #walkCount = 0;
212
+ #lastCommitAtMs = Number.NEGATIVE_INFINITY;
213
+ #lastCommitEndMs = Number.NEGATIVE_INFINITY;
214
+ #lastCommitCostMs = 0;
215
+ // A coalesced write skipped its commit and counts on a queued write to publish.
216
+ #coalescedCommitOwed = false;
187
217
  #pendingWrites: WriteGroup | null = null;
188
218
  #widgetGate = false;
189
219
  // kitty graphics: images outlive reseeds; the scanner owns split APCs.
@@ -528,7 +558,11 @@ export class TerminalReplicaInterpreter {
528
558
  this.#emit(update);
529
559
  }
530
560
 
531
- async #write(data: Uint8Array, continuedTrace: SessionRuntimeTraceContext | null): Promise<void> {
561
+ async #write(
562
+ data: Uint8Array,
563
+ continuedTrace: SessionRuntimeTraceContext | null,
564
+ hasBacklog: () => boolean = () => false,
565
+ ): Promise<void> {
532
566
  if (this.#closed || this.#nativeReseedRequested) return;
533
567
  // This optional id is only a controlled next-output probe. It is not a
534
568
  // causal assertion: unrelated external tmux output arriving first may
@@ -547,7 +581,17 @@ export class TerminalReplicaInterpreter {
547
581
  // see. Skipping it matters: every parser write settles on a later timer
548
582
  // turn, so a frame split into dozens of %output chunks would otherwise
549
583
  // serialize dozens of turns and fall behind a 30 fps image stream.
550
- if (scanned.forward.byteLength === 0) return;
584
+ if (scanned.forward.byteLength === 0) {
585
+ // The queued write a coalesced commit was counting on turned out to be
586
+ // all image payload, and nothing else is queued: publish what it skipped.
587
+ // Not inside a synchronized update: its end, or the sync recovery,
588
+ // publishes the owed commit with the whole frame.
589
+ if (this.#coalescedCommitOwed && !hasBacklog() && !this.#backend.modes().synchronizedOutput) {
590
+ this.#commit(false, this.#pendingWriteDirty ?? undefined, trace);
591
+ this.#publishGraphics();
592
+ }
593
+ return;
594
+ }
551
595
  // Scanning can prepend an escape retained from the previous chunk. Bound
552
596
  // the actual parser calls too, and union damage across every fragment.
553
597
  for (let offset = 0; offset < scanned.forward.length; offset += MAX_INGEST_BATCH_BYTES) {
@@ -592,6 +636,20 @@ export class TerminalReplicaInterpreter {
592
636
  if (pendingResize) {
593
637
  this.#commit(false, undefined, trace);
594
638
  } else {
639
+ // Coalesce under a flood: more output is already queued, the last commit
640
+ // is recent, and nobody is timing this write. The dirty range stays in
641
+ // #pendingWriteDirty and the next commit (the queued write's, or any
642
+ // control operation's) merges it, so nothing is dropped. The bootstrap
643
+ // seed is never held: before it no commit has run, so one is always due.
644
+ if (
645
+ trace === null &&
646
+ this.#causalCell === null &&
647
+ hasBacklog() &&
648
+ !this.#coalescedCommitIsDue()
649
+ ) {
650
+ this.#coalescedCommitOwed = true;
651
+ return;
652
+ }
595
653
  this.#commit(false, this.#backend.dirtyRange(), trace);
596
654
  }
597
655
  this.#publishGraphics();
@@ -629,10 +687,36 @@ export class TerminalReplicaInterpreter {
629
687
  this.#syncRecovery = null;
630
688
  }
631
689
 
690
+ /** True once a queued flood may publish again: see the two constants above. */
691
+ #coalescedCommitIsDue(): boolean {
692
+ const now = this.#scheduler.nowMs();
693
+ if (now - this.#lastCommitAtMs < COALESCED_COMMIT_INTERVAL_MS) return false;
694
+ const rest = Math.min(
695
+ COALESCED_COMMIT_MAX_REST_MS,
696
+ COALESCED_COMMIT_REST_FACTOR * this.#lastCommitCostMs,
697
+ );
698
+ return now - this.#lastCommitEndMs >= rest;
699
+ }
700
+
632
701
  #commit(
633
702
  forceSeed: boolean,
634
703
  dirty?: { start: number; end: number },
635
704
  trace: SessionRuntimeTraceContext | null = null,
705
+ ): void {
706
+ this.#lastCommitAtMs = this.#scheduler.nowMs();
707
+ this.#coalescedCommitOwed = false;
708
+ try {
709
+ this.#commitNow(forceSeed, dirty, trace);
710
+ } finally {
711
+ this.#lastCommitEndMs = this.#scheduler.nowMs();
712
+ this.#lastCommitCostMs = this.#lastCommitEndMs - this.#lastCommitAtMs;
713
+ }
714
+ }
715
+
716
+ #commitNow(
717
+ forceSeed: boolean,
718
+ dirty: { start: number; end: number } | undefined,
719
+ trace: SessionRuntimeTraceContext | null,
636
720
  ): void {
637
721
  if (this.#backend.requiresNativeReseed()) {
638
722
  if (!this.#nativeReseedRequested) {
@@ -916,7 +1000,20 @@ export class TerminalReplicaInterpreter {
916
1000
  group.entries.splice(0, completed.length);
917
1001
  group.bytes -= length;
918
1002
  try {
919
- await this.#write(data, group.trace);
1003
+ // Backlog: bytes still in this group, or a newer untraced group
1004
+ // already waiting behind it (a group that has not run yet always
1005
+ // holds an entry). A traced group is a boundary: its commit must
1006
+ // not carry these external bytes. Checked after parsing, when it
1007
+ // is current.
1008
+ await this.#write(
1009
+ data,
1010
+ group.trace,
1011
+ () =>
1012
+ group.entries.length > 0 ||
1013
+ (this.#pendingWrites !== null &&
1014
+ this.#pendingWrites !== group &&
1015
+ this.#pendingWrites.trace === null),
1016
+ );
920
1017
  } catch (error) {
921
1018
  for (const entry of completed) entry.reject(error);
922
1019
  throw error;
@@ -42,6 +42,12 @@ export interface TerminalReplicaSubscription {
42
42
  }
43
43
 
44
44
  export type TerminalReplicaSourceSubscription = TerminalReplicaSubscription;
45
+ /**
46
+ * How long a pane may have no listener before it is parked (perf F2). Long
47
+ * enough that switching away and back stays warm.
48
+ */
49
+ export const PARK_AFTER_MS = 10_000;
50
+
45
51
  export interface TerminalReplicaCommittedRaw {
46
52
  readonly baseRevision: number;
47
53
  readonly revision: number;
@@ -103,6 +109,11 @@ export class SessionRuntimeTerminalReplicaOwner {
103
109
  readonly #start: Promise<void>;
104
110
  #upstream: MirrorSubscription | null = null;
105
111
  #disposed = false;
112
+ #parkTimer: SessionRuntimeTimer | null = null;
113
+ #parked = false;
114
+ // From the park until the next reseed lands, the replica is older than tmux.
115
+ // Native backing and history anchors must not pair it with current tmux state.
116
+ #stale = false;
106
117
  readonly #historyAnchors = new HistoryAnchorOwner();
107
118
  #cols = 80;
108
119
  #rows = 24;
@@ -243,13 +254,13 @@ export class SessionRuntimeTerminalReplicaOwner {
243
254
  }
244
255
 
245
256
  captureHistoryAnchor(proof: HistoryAnchorProof, historyIndex: number) {
246
- if (this.#disposed || this.#reseed || this.#waitingForGeometryCapture)
257
+ if (this.#disposed || this.#stale || this.#reseed || this.#waitingForGeometryCapture)
247
258
  return { status: "unavailable" as const };
248
259
  return this.#historyAnchors.capture(proof, historyIndex);
249
260
  }
250
261
 
251
262
  resolveHistoryAnchor(proof: HistoryAnchorIdentity, token: string) {
252
- if (this.#disposed || this.#reseed || this.#waitingForGeometryCapture)
263
+ if (this.#disposed || this.#stale || this.#reseed || this.#waitingForGeometryCapture)
253
264
  return { status: "unavailable" as const };
254
265
  return this.#historyAnchors.resolve(proof, token);
255
266
  }
@@ -287,7 +298,8 @@ export class SessionRuntimeTerminalReplicaOwner {
287
298
  const upstream = this.#upstream;
288
299
  if (!upstream?.captureNativeBacking) return { status: "unsupported" };
289
300
  const initial = this.#interpreter.currentSeed();
290
- if (!initial || this.#reseed || this.#waitingForGeometryCapture) return { status: "changed" };
301
+ if (!initial || this.#stale || this.#reseed || this.#waitingForGeometryCapture)
302
+ return { status: "changed" };
291
303
  const epoch = this.#subscriptionEpoch;
292
304
  const captured = await upstream.captureNativeBacking();
293
305
  if (captured.status !== "captured") return captured;
@@ -298,6 +310,7 @@ export class SessionRuntimeTerminalReplicaOwner {
298
310
  !this.#disposed &&
299
311
  this.#upstream === upstream &&
300
312
  this.#subscriptionEpoch === epoch &&
313
+ !this.#stale &&
301
314
  !this.#reseed &&
302
315
  !this.#waitingForGeometryCapture &&
303
316
  captured.isCurrent() &&
@@ -362,6 +375,8 @@ export class SessionRuntimeTerminalReplicaOwner {
362
375
  } catch {
363
376
  // Bootstrap delivery has the same client-isolation rule as live frames.
364
377
  }
378
+ // The last frame is out; now ask for the reseed that makes it current.
379
+ this.#unpark();
365
380
  } catch (error) {
366
381
  this.#listeners.delete(listener);
367
382
  throw error;
@@ -374,10 +389,47 @@ export class SessionRuntimeTerminalReplicaOwner {
374
389
  if (closed) return;
375
390
  closed = true;
376
391
  this.#listeners.delete(listener);
392
+ if (this.#listeners.size === 0) this.#schedulePark();
377
393
  },
378
394
  };
379
395
  }
380
396
 
397
+ /**
398
+ * Park this pane PARK_AFTER_MS after its last listener leaves (perf F2).
399
+ * The owner is the pane's only channel subscriber, so freezing it pauses the
400
+ * pane's output to the daemon's control client (the channel's all-frozen
401
+ * path). tmux keeps running the pane; nothing here is parsed or hashed. The
402
+ * last committed seed stays, so the next listener sees it at once, and
403
+ * thawing runs the channel's requested-pause recovery, which reseeds.
404
+ * Until that reseed lands the replica is stale: history anchors and native
405
+ * backing answer as they do during any reseed, and the reseed's reset then
406
+ * drops every anchor taken before the park.
407
+ */
408
+ #schedulePark(): void {
409
+ if (this.#disposed || this.#parked) return;
410
+ this.#parkTimer?.cancel();
411
+ this.#parkTimer = this.#scheduler.timer(() => {
412
+ this.#parkTimer = null;
413
+ if (this.#disposed || this.#parked || this.#listeners.size > 0 || !this.#upstream) return;
414
+ this.#parked = true;
415
+ this.#stale = true;
416
+ this.#upstream.freeze();
417
+ }, PARK_AFTER_MS);
418
+ }
419
+
420
+ #unpark(): void {
421
+ this.#parkTimer?.cancel();
422
+ this.#parkTimer = null;
423
+ if (!this.#parked) return;
424
+ this.#parked = false;
425
+ this.#upstream?.thaw();
426
+ }
427
+
428
+ /** True while the pane is parked (tests and qualification). */
429
+ get parked(): boolean {
430
+ return this.#parked;
431
+ }
432
+
381
433
  /** Kitty graphics side channel: current state now (if any), then every change. */
382
434
  subscribeGraphics(listener: (state: TerminalGraphicsState) => void): () => void {
383
435
  const current = this.#interpreter.currentGraphics();
@@ -420,6 +472,8 @@ export class SessionRuntimeTerminalReplicaOwner {
420
472
  this.#historyAnchors.invalidate();
421
473
  this.#historyTimer?.cancel();
422
474
  this.#historyTimer = null;
475
+ this.#parkTimer?.cancel();
476
+ this.#parkTimer = null;
423
477
  this.#subscriptionEpoch += 1;
424
478
  await this.#interpreter.enqueue({ type: "close", reason });
425
479
  await this.#start.catch(() => undefined);
@@ -454,6 +508,7 @@ export class SessionRuntimeTerminalReplicaOwner {
454
508
  // Output received during the probe makes its count incomparable with
455
509
  // the newer parser state. The dirty flag schedules another check.
456
510
  this.#historyDirty ||
511
+ this.#stale ||
457
512
  this.#reseed ||
458
513
  this.#waitingForGeometryCapture ||
459
514
  size === null
@@ -511,6 +566,8 @@ export class SessionRuntimeTerminalReplicaOwner {
511
566
  this.#waitingForGeometryCapture = false;
512
567
  const reseed = this.#reseed;
513
568
  this.#reseed = null;
569
+ // A completed capture after the park makes the replica current again.
570
+ if (reseed && !this.#parked) this.#stale = false;
514
571
  const lease = reseed ? this.#qualifyReseed(reseed, event.x, event.y) : null;
515
572
  this.#supervise(
516
573
  reseed && lease
@@ -0,0 +1,118 @@
1
+ /**
2
+ * `tmux-ide servers upgrade` (#766): flags, the `--ssh` hand-off, the detached
3
+ * worker's `--continue`, and the exit code. The steps are in ./run.ts.
4
+ */
5
+ import { spawnSync } from "node:child_process";
6
+ import { readFileSync } from "node:fs";
7
+ import { dirname, join } from "node:path";
8
+ import { SavedMachineSchema } from "@tmux-ide/contracts";
9
+ import { IdeError } from "../lib/errors.ts";
10
+ import { remoteTmuxIdeArgsCommand } from "../lib/remote-tmux-command.ts";
11
+ import { createUpgradeIo, parseWorkerState, writeWorkerResult, type UpgradeTarget } from "./io.ts";
12
+ import {
13
+ finishUpgrade,
14
+ retryCommand,
15
+ upgradeServer,
16
+ type UpgradeOptions,
17
+ type UpgradeResult,
18
+ } from "./run.ts";
19
+
20
+ export interface UpgradeCliOptions extends UpgradeOptions, UpgradeTarget {
21
+ ssh?: string;
22
+ /** The detached worker: finish the upgrade described in this file. */
23
+ continueFrom?: string;
24
+ cliEntry: string;
25
+ }
26
+
27
+ /** The flags a remote run gets: the same choices, never the local socket defaults. */
28
+ export function forwardedUpgradeArgs(options: UpgradeCliOptions): string[] {
29
+ const args: string[] = [];
30
+ if (options.yes) args.push("--yes");
31
+ if (options.endRunningAgents) args.push("--end-running-agents");
32
+ if (!options.resumeAgents) args.push("--no-resume-agents");
33
+ if (options.dryRun) args.push("--dry-run");
34
+ if (options.json) args.push("--json");
35
+ if (options.socketPath) args.push("--socket-path", options.socketPath);
36
+ if (options.socketName) args.push("--socket-name", options.socketName);
37
+ return args;
38
+ }
39
+
40
+ const EXIT: Record<UpgradeResult["status"], number> = {
41
+ "already-full": 0,
42
+ preview: 0,
43
+ declined: 0,
44
+ "handed-off": 0,
45
+ upgraded: 0,
46
+ refused: 1,
47
+ failed: 1,
48
+ };
49
+
50
+ function report(result: UpgradeResult, json: boolean): void {
51
+ if (json) {
52
+ process.stdout.write(`${JSON.stringify(result)}\n`);
53
+ return;
54
+ }
55
+ // The plan was already printed before the question; print it here only for a preview.
56
+ console.log(
57
+ result.status === "preview"
58
+ ? `${result.message}\n\nDry run: nothing was stopped.`
59
+ : result.message,
60
+ );
61
+ }
62
+
63
+ export async function runUpgradeCli(options: UpgradeCliOptions): Promise<void> {
64
+ if (options.socketPath && options.socketName)
65
+ throw new IdeError("Pass --socket-path or --socket-name, not both.", {
66
+ code: "USAGE",
67
+ exitCode: 2,
68
+ });
69
+ if (options.ssh !== undefined) {
70
+ SavedMachineSchema.shape.sshTarget.parse(options.ssh);
71
+ // The remote runs the same command against its own server, bundle and
72
+ // hostname; its prompt answers here through the terminal.
73
+ const child = spawnSync(
74
+ "ssh",
75
+ [
76
+ ...(options.json ? ["-T"] : ["-t"]),
77
+ "--",
78
+ options.ssh,
79
+ remoteTmuxIdeArgsCommand(["servers", "upgrade", ...forwardedUpgradeArgs(options)]),
80
+ ],
81
+ { stdio: "inherit" },
82
+ );
83
+ if (child.error) throw new IdeError("Could not start ssh.", { code: "SSH", exitCode: 1 });
84
+ if (child.status !== 0) process.exitCode = child.status ?? 1;
85
+ return;
86
+ }
87
+ if (options.continueFrom !== undefined) {
88
+ const work = parseWorkerState(readFileSync(options.continueFrom, "utf8"));
89
+ const io = createUpgradeIo({
90
+ target: { socketPath: work.server.socketPath },
91
+ cliEntry: options.cliEntry,
92
+ json: false,
93
+ });
94
+ // Nothing the foreground's terminal does may stop the worker mid-restore.
95
+ for (const signal of ["SIGHUP", "SIGINT", "SIGPIPE"] as const) process.on(signal, () => {});
96
+ let result: UpgradeResult;
97
+ try {
98
+ result = await finishUpgrade(io, work);
99
+ } catch (error) {
100
+ result = {
101
+ status: "failed",
102
+ message: `${(error as Error).message} If the sessions are missing, run: ${retryCommand(work.snapshotPath, work.resumeAgents)}`,
103
+ };
104
+ }
105
+ console.log(`${new Date().toISOString()} ${result.status}: ${result.message}`);
106
+ writeWorkerResult(join(dirname(options.continueFrom), "result.json"), result);
107
+ process.exitCode = EXIT[result.status];
108
+ return;
109
+ }
110
+ const io = createUpgradeIo({
111
+ target: { socketPath: options.socketPath, socketName: options.socketName },
112
+ cliEntry: options.cliEntry,
113
+ json: options.json,
114
+ });
115
+ const result = await upgradeServer(io, options);
116
+ report(result, options.json);
117
+ process.exitCode = EXIT[result.status];
118
+ }