tmux-ide 2.9.26 → 2.9.28

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 (89) hide show
  1. package/README.md +1 -1
  2. package/bin/cli.js +2046 -623
  3. package/package.json +2 -2
  4. package/packages/contracts/src/pane-stream.ts +45 -0
  5. package/packages/contracts/src/terminal-delivery.ts +114 -3
  6. package/packages/core/src/index.ts +1 -0
  7. package/packages/core/src/terminal-delivery-binary.ts +8 -7
  8. package/packages/core/src/terminal-delivery.ts +174 -3
  9. package/packages/core/src/terminal-partial-history.ts +152 -0
  10. package/packages/core/src/terminal-replica.ts +70 -1
  11. package/packages/daemon/dist/command-center/owner-keep-alive.js +30 -0
  12. package/packages/daemon/dist/command-center/resources/terminal-runtime-inventory.js +17 -3
  13. package/packages/daemon/dist/command-center/server.js +3 -0
  14. package/packages/daemon/dist/command-center/tmux-server-attach.js +51 -0
  15. package/packages/daemon/dist/command-center/tmux-servers.js +80 -1
  16. package/packages/daemon/dist/doctor.js +4 -4
  17. package/packages/daemon/dist/lib/app-config.js +7 -0
  18. package/packages/daemon/dist/lib/node-requirement.js +20 -0
  19. package/packages/daemon/dist/lib/require-supported-node.js +8 -0
  20. package/packages/daemon/dist/lib/tmux-server-session-open.js +38 -12
  21. package/packages/daemon/dist/lib/workspace-promotion.js +4 -4
  22. package/packages/daemon/dist/native/TmuxIdeNotifier.app/Contents/Info.plist +2 -2
  23. package/packages/daemon/dist/native/TmuxIdeNotifier.app/Contents/MacOS/tmux-ide-notifier +0 -0
  24. package/packages/daemon/dist/native/TmuxIdeNotifier.app/Contents/Resources/Assets.car +0 -0
  25. package/packages/daemon/dist/native/TmuxIdeNotifier.app/Contents/_CodeSignature/CodeResources +2 -2
  26. package/packages/daemon/dist/server/pane-stream-upgrade.js +39 -1
  27. package/packages/daemon/dist/terminal/attachments/native-runtime.js +12 -8
  28. package/packages/daemon/dist/terminal/mirror/mirror-service.js +13 -0
  29. package/packages/daemon/dist/terminal/mirror/native-atomic-snapshot.js +17 -7
  30. package/packages/daemon/dist/terminal/mirror/native-grid-capture.js +129 -5
  31. package/packages/daemon/dist/terminal/mirror/pane-feed.js +4 -0
  32. package/packages/daemon/dist/terminal/mirror/pane-hold.js +13 -0
  33. package/packages/daemon/dist/terminal/mirror/session-channel.js +198 -27
  34. package/packages/daemon/dist/terminal/pane-stream/lease-manager.js +1 -0
  35. package/packages/daemon/dist/terminal/pane-stream/pane-stream-websocket.js +198 -12
  36. package/packages/daemon/dist/terminal/protocol/layout-parse.js +99 -10
  37. package/packages/daemon/dist/terminal/protocol/session-descriptor-discovery.js +5 -2
  38. package/packages/daemon/dist/terminal/session-runtime/registry.js +1 -0
  39. package/packages/daemon/dist/terminal/session-runtime/terminal-delivery-hub.js +465 -42
  40. package/packages/daemon/dist/terminal/session-runtime/terminal-interpreter-backend.js +2 -1
  41. package/packages/daemon/dist/terminal/session-runtime/terminal-replica-interpreter.js +46 -1
  42. package/packages/daemon/dist/terminal/session-runtime/terminal-replica-owner.js +128 -3
  43. package/packages/daemon/dist/terminal/session-runtime/xterm-terminal-interpreter-backend.js +115 -6
  44. package/packages/daemon/dist/tui/mirror/open-tui-workspace-runtime-port.js +94 -2
  45. package/packages/daemon/dist/tui/mirror/runtime/open-tui-generation-host.js +9 -1
  46. package/packages/daemon/dist/tui/mirror/theme.js +8 -1
  47. package/packages/daemon/dist/tui/mirror/workspace/connection-feedback.js +8 -0
  48. package/packages/daemon/src/command-center/attach-window-rule.cases.json +84 -0
  49. package/packages/daemon/src/command-center/owner-keep-alive.ts +38 -0
  50. package/packages/daemon/src/command-center/resources/terminal-runtime-inventory.ts +20 -2
  51. package/packages/daemon/src/command-center/server.ts +3 -0
  52. package/packages/daemon/src/command-center/tmux-server-attach.ts +75 -0
  53. package/packages/daemon/src/command-center/tmux-servers.ts +88 -1
  54. package/packages/daemon/src/doctor.ts +4 -4
  55. package/packages/daemon/src/lib/app-config.ts +15 -0
  56. package/packages/daemon/src/lib/node-requirement.ts +22 -0
  57. package/packages/daemon/src/lib/require-supported-node.ts +9 -0
  58. package/packages/daemon/src/lib/tmux-server-session-open.ts +53 -15
  59. package/packages/daemon/src/lib/workspace-promotion.ts +4 -4
  60. package/packages/daemon/src/server/pane-stream-upgrade.ts +41 -1
  61. package/packages/daemon/src/terminal/attachments/native-runtime.ts +15 -1
  62. package/packages/daemon/src/terminal/mirror/events.ts +31 -1
  63. package/packages/daemon/src/terminal/mirror/mirror-service.ts +19 -0
  64. package/packages/daemon/src/terminal/mirror/native-atomic-snapshot.ts +16 -5
  65. package/packages/daemon/src/terminal/mirror/native-grid-capture.ts +164 -5
  66. package/packages/daemon/src/terminal/mirror/pane-feed.ts +5 -0
  67. package/packages/daemon/src/terminal/mirror/pane-hold.ts +16 -0
  68. package/packages/daemon/src/terminal/mirror/session-channel.ts +253 -38
  69. package/packages/daemon/src/terminal/mirror/trusted-inventory.ts +2 -0
  70. package/packages/daemon/src/terminal/pane-stream/lease-manager.ts +3 -0
  71. package/packages/daemon/src/terminal/pane-stream/pane-stream-websocket.ts +240 -12
  72. package/packages/daemon/src/terminal/protocol/layout-parse.ts +109 -10
  73. package/packages/daemon/src/terminal/protocol/session-descriptor-discovery.ts +10 -1
  74. package/packages/daemon/src/terminal/session-runtime/registry.ts +1 -0
  75. package/packages/daemon/src/terminal/session-runtime/runtime-observability.ts +2 -1
  76. package/packages/daemon/src/terminal/session-runtime/terminal-delivery-hub.ts +575 -39
  77. package/packages/daemon/src/terminal/session-runtime/terminal-interpreter-backend.ts +24 -1
  78. package/packages/daemon/src/terminal/session-runtime/terminal-replica-interpreter.ts +61 -5
  79. package/packages/daemon/src/terminal/session-runtime/terminal-replica-owner.ts +159 -32
  80. package/packages/daemon/src/terminal/session-runtime/xterm-terminal-interpreter-backend.ts +134 -11
  81. package/packages/daemon/src/tui/mirror/open-tui-workspace-runtime-port.ts +101 -2
  82. package/packages/daemon/src/tui/mirror/runtime/open-tui-generation-host.ts +13 -0
  83. package/packages/daemon/src/tui/mirror/theme.ts +13 -1
  84. package/packages/daemon/src/tui/mirror/workspace/connection-feedback.ts +13 -1
  85. package/packages/daemon-client/src/direct-application-shell-transport.ts +6 -2
  86. package/packages/daemon-client/src/pane-stream-client.ts +24 -6
  87. package/packages/daemon-client/src/workspace-client-types.ts +6 -0
  88. package/packages/daemon-client/src/workspace-client.ts +15 -9
  89. package/packages/daemon-client/src/workspace-event-supervisor.ts +3 -1
@@ -1,4 +1,4 @@
1
- import type { NativeGridCapture } from "../mirror/native-grid-capture.ts";
1
+ import type { NativeGridCapture, NativeHistoryPiece } from "../mirror/native-grid-capture.ts";
2
2
  import type { MirrorObservedTerminalModes } from "../mirror/events.ts";
3
3
  import type {
4
4
  TerminalReplicaCursor,
@@ -32,6 +32,21 @@ export interface TerminalInterpreterBackend {
32
32
  /** Exact native painted state; false means this parser cannot import it. */
33
33
  canImportNativeGrid?(): boolean;
34
34
  importNativeGrid?(snapshot: NativeGridCapture): boolean;
35
+ /**
36
+ * #873: tmux history rows older than the oldest row held, left out by the
37
+ * last native import and not yet prepended or collected. tmux's history
38
+ * size is this plus the history rows held. Zero after any other start.
39
+ */
40
+ omittedHistory?(): number;
41
+ /** #873: the most history rows this parser holds (DAEMON_HISTORY_ROW_CAP unless set). */
42
+ historyRowCap?(): number;
43
+ /**
44
+ * #873: prepend the piece's rows that are older than the oldest row held.
45
+ * Applies only on the normal screen, at the piece's width, when the piece
46
+ * was read at this history size and reaches the oldest row held; otherwise
47
+ * changes nothing and returns false.
48
+ */
49
+ prependNativeHistory?(piece: NativeHistoryPiece): boolean;
35
50
  resize(cols: number, rows: number): void;
36
51
  /** x === cols preserves native wrap-pending state; project() returns a cell position. */
37
52
  setAuthoritativeCursor(x: number, y: number): void;
@@ -81,8 +96,16 @@ export interface TerminalInterpreterBackendFactoryOptions {
81
96
  readonly scrollback: number;
82
97
  /** Native collection policy; retained capacity can be larger after tmux reflow. */
83
98
  readonly historyLimit?: number;
99
+ /**
100
+ * #873: the most history rows the daemon holds for a pane, whatever tmux's
101
+ * history-limit. Older rows stay in tmux and count as left out.
102
+ */
103
+ readonly historyRowCap?: number;
84
104
  }
85
105
 
106
+ /** #873, owner decision of 25 Nov 2026: the daemon holds at most this many history rows per pane. */
107
+ export const DAEMON_HISTORY_ROW_CAP = 10_000;
108
+
86
109
  export type TerminalInterpreterBackendFactory = (
87
110
  options: TerminalInterpreterBackendFactoryOptions,
88
111
  ) => TerminalInterpreterBackend;
@@ -1,7 +1,7 @@
1
1
  import { assertStockCaptureRepresentable } from "./stock-capture-fidelity.ts";
2
2
  import { logger } from "../../lib/log.ts";
3
3
  import { rememberNativeSeedBacking } from "./native-seed-backing.ts";
4
- import type { NativeGridCapture } from "../mirror/native-grid-capture.ts";
4
+ import type { NativeGridCapture, NativeHistoryPiece } from "../mirror/native-grid-capture.ts";
5
5
  import type { MirrorObservedTerminalModes } from "../mirror/events.ts";
6
6
  import {
7
7
  detectWidgetMarkerFromReplicaRows,
@@ -34,9 +34,10 @@ import {
34
34
  type SessionRuntimeObservability,
35
35
  type SessionRuntimeTraceContext,
36
36
  } from "./runtime-observability.ts";
37
- import type {
38
- TerminalInterpreterBackend,
39
- TerminalInterpreterBackendFactory,
37
+ import {
38
+ DAEMON_HISTORY_ROW_CAP,
39
+ type TerminalInterpreterBackend,
40
+ type TerminalInterpreterBackendFactory,
40
41
  } from "./terminal-interpreter-backend.ts";
41
42
  import { createXtermTerminalInterpreterBackend } from "./xterm-terminal-interpreter-backend.ts";
42
43
  import {
@@ -148,6 +149,8 @@ export interface TerminalReplicaInterpreterOptions {
148
149
  readonly cols: number;
149
150
  readonly rows: number;
150
151
  readonly scrollback?: number;
152
+ /** #873: the most history rows held per pane; DAEMON_HISTORY_ROW_CAP by default. */
153
+ readonly historyRowCap?: number;
151
154
  readonly onUpdate?: (
152
155
  update: CanonicalTerminalReplicaUpdate,
153
156
  trace: SessionRuntimeTraceContext | null,
@@ -191,6 +194,7 @@ export class TerminalReplicaInterpreter {
191
194
  readonly #semanticPaneId: string;
192
195
  readonly #incarnation: string;
193
196
  #scrollback: number;
197
+ readonly #historyRowCap: number;
194
198
  readonly #listeners = new Set<
195
199
  (update: CanonicalTerminalReplicaUpdate, trace: SessionRuntimeTraceContext | null) => void
196
200
  >();
@@ -260,6 +264,7 @@ export class TerminalReplicaInterpreter {
260
264
  this.#incarnation = options.incarnation;
261
265
  this.#revision = options.initialRevision ?? 0;
262
266
  this.#scrollback = options.scrollback ?? 5000;
267
+ this.#historyRowCap = options.historyRowCap ?? DAEMON_HISTORY_ROW_CAP;
263
268
  this.#onRawCommit = options.onRawCommit;
264
269
  this.#onNativeReseedRequired = options.onNativeReseedRequired;
265
270
  this.#scheduler = options.scheduler ?? SYSTEM_SESSION_RUNTIME_SCHEDULER;
@@ -269,6 +274,7 @@ export class TerminalReplicaInterpreter {
269
274
  cols: options.cols,
270
275
  rows: options.rows,
271
276
  scrollback: this.#scrollback,
277
+ historyRowCap: this.#historyRowCap,
272
278
  });
273
279
  this.#backend.startHistoryCollection?.();
274
280
  this.#kitty = new KittyImageStore(
@@ -336,6 +342,36 @@ export class TerminalReplicaInterpreter {
336
342
  return this.#append(admitted);
337
343
  }
338
344
 
345
+ /**
346
+ * #873: prepend the older history rows of a piece read from tmux, in order
347
+ * with the writes queued before it. True when the rows were prepended and a
348
+ * new revision published; false when the piece does not fit the replica as
349
+ * it is at that point of the stream (see the backend's prependNativeHistory).
350
+ */
351
+ prependNativeHistory(piece: NativeHistoryPiece): Promise<boolean> {
352
+ this.#flushWrites();
353
+ const run = this.#tail.then(() => this.#applyHistoryPiece(piece));
354
+ this.#tail = run.then(
355
+ () => undefined,
356
+ () => undefined,
357
+ );
358
+ return run;
359
+ }
360
+
361
+ /**
362
+ * #873: tmux history rows older than the replica's oldest history row; tmux's
363
+ * history size is this plus the replica's history rows. Set by a native seed
364
+ * that left them out, zero after any other seed.
365
+ */
366
+ omittedHistory(): number {
367
+ return this.#backend.omittedHistory?.() ?? 0;
368
+ }
369
+
370
+ /** #873: the most history rows the replica holds; the backfill stops there. */
371
+ historyRowCap(): number {
372
+ return this.#historyRowCap;
373
+ }
374
+
339
375
  whenIdle(): Promise<void> {
340
376
  this.#flushWrites();
341
377
  return this.#tail;
@@ -455,13 +491,14 @@ export class TerminalReplicaInterpreter {
455
491
  const scrollback = Math.max(
456
492
  operation.historyLimit ?? this.#scrollback,
457
493
  operation.historySize ?? 0,
458
- operation.native?.history ?? 0,
494
+ (operation.native?.history ?? 0) + (operation.native?.omittedHistory ?? 0),
459
495
  );
460
496
  const replacement = this.#backendFactory({
461
497
  cols: nativeCols,
462
498
  rows: nativeRows,
463
499
  scrollback,
464
500
  historyLimit: operation.historyLimit ?? scrollback,
501
+ historyRowCap: this.#historyRowCap,
465
502
  });
466
503
  try {
467
504
  if (operation.native && !replacement.importNativeGrid?.(operation.native))
@@ -594,6 +631,25 @@ export class TerminalReplicaInterpreter {
594
631
  this.#emit(update);
595
632
  }
596
633
 
634
+ #applyHistoryPiece(piece: NativeHistoryPiece): boolean {
635
+ if (
636
+ this.#closed ||
637
+ this.#needsSeed ||
638
+ this.#nativeReseedRequested ||
639
+ this.#pendingResize !== null ||
640
+ this.#backend.requiresNativeReseed() ||
641
+ // A synchronized update publishes nothing until it ends.
642
+ this.#backend.modes().synchronizedOutput ||
643
+ !this.#backend.prependNativeHistory?.(piece)
644
+ )
645
+ return false;
646
+ // Image anchors are absolute buffer lines; prepended rows moved them all.
647
+ this.#kitty.dropCursorPlacements();
648
+ this.#commit(false);
649
+ this.#publishGraphics();
650
+ return true;
651
+ }
652
+
597
653
  async #write(
598
654
  data: Uint8Array,
599
655
  continuedTrace: SessionRuntimeTraceContext | null,
@@ -1,10 +1,11 @@
1
+ import { MirrorPaneHeldError } from "../mirror/pane-hold.ts";
1
2
  import {
2
3
  HistoryAnchorOwner,
3
4
  type HistoryAnchorIdentity,
4
5
  type HistoryAnchorProof,
5
6
  } from "./history-anchor-owner.ts";
6
7
  import { layoutContentRows } from "../mirror/layout-content-rows.ts";
7
- import type { NativeGridCapture } from "../mirror/native-grid-capture.ts";
8
+ import { nativeHistoryPieceRows, type NativeGridCapture } from "../mirror/native-grid-capture.ts";
8
9
  import type {
9
10
  CanonicalTerminalReplicaUpdate,
10
11
  CausalCellFailureReasonV1,
@@ -47,6 +48,20 @@ export type TerminalReplicaSourceSubscription = TerminalReplicaSubscription;
47
48
  * enough that switching away and back stays warm.
48
49
  */
49
50
  export const PARK_AFTER_MS = 10_000;
51
+ /**
52
+ * #873 backfill: a native seed can leave out the oldest history rows; the
53
+ * owner then asks tmux for them piece by piece. A null answer (output crossed
54
+ * the read) or a piece that no longer fits is asked again after
55
+ * BACKFILL_FIRST_RETRY_MS, doubling up to BACKFILL_MAX_RETRY_MS. After
56
+ * BACKFILL_MAX_FAILURES in a row (about 48 s of a pane that never pauses) the
57
+ * owner stops until the next seed; the replica stays correct without the
58
+ * older rows, it only lacks them. While the alternate screen is active it
59
+ * checks again every BACKFILL_ALTERNATE_WAIT_MS without counting a failure.
60
+ */
61
+ export const BACKFILL_FIRST_RETRY_MS = 250;
62
+ export const BACKFILL_MAX_RETRY_MS = 4_000;
63
+ export const BACKFILL_MAX_FAILURES = 16;
64
+ export const BACKFILL_ALTERNATE_WAIT_MS = 1_000;
50
65
 
51
66
  export interface TerminalReplicaCommittedRaw {
52
67
  readonly baseRevision: number;
@@ -129,6 +144,14 @@ export class SessionRuntimeTerminalReplicaOwner {
129
144
  #historyChecking = false;
130
145
  #historyDirty = false;
131
146
  #reconstructedHistoryGap: { readonly seed: number; readonly gap: number } | null = null;
147
+ // #873 backfill. A new seed, resize, park or disposal starts a new token;
148
+ // anything from an older token is ignored.
149
+ #backfillToken = 0;
150
+ // The token of each history request sent and not yet answered, oldest first.
151
+ // Answers arrive in request order, so each one belongs to the first entry.
152
+ readonly #backfillAsked: number[] = [];
153
+ #backfillFailures = 0;
154
+ #backfillTimer: SessionRuntimeTimer | null = null;
132
155
 
133
156
  constructor(
134
157
  readonly generation: SessionRuntimeGeneration,
@@ -144,6 +167,8 @@ export class SessionRuntimeTerminalReplicaOwner {
144
167
  readonly scheduler?: SessionRuntimeScheduler;
145
168
  readonly observability?: SessionRuntimeObservability;
146
169
  readonly takeOutputTrace?: () => SessionRuntimeTraceContext | null;
170
+ /** #873: the most history rows held for this pane (DAEMON_HISTORY_ROW_CAP). */
171
+ readonly historyRowCap?: number;
147
172
  },
148
173
  ) {
149
174
  this.#onClosed = options.onClosed;
@@ -161,10 +186,12 @@ export class SessionRuntimeTerminalReplicaOwner {
161
186
  rows: this.#rows,
162
187
  scheduler: options.scheduler,
163
188
  observability: options.observability,
189
+ ...(options.historyRowCap !== undefined ? { historyRowCap: options.historyRowCap } : {}),
164
190
  onNativeReseedRequired: () => {
165
191
  if (this.#disposed || this.#reseed || this.#waitingForGeometryCapture) return;
166
192
  this.#waitingForGeometryCapture = true;
167
193
  this.#historyAnchors.invalidate();
194
+ this.#cancelBackfill();
168
195
  this.#supervise(
169
196
  this.#start.then(() => {
170
197
  if (!this.#disposed && this.#waitingForGeometryCapture) this.#upstream?.reseed();
@@ -301,6 +328,8 @@ export class SessionRuntimeTerminalReplicaOwner {
301
328
  const initial = this.#interpreter.currentSeed();
302
329
  if (!initial || this.#stale || this.#reseed || this.#waitingForGeometryCapture)
303
330
  return { status: "changed" };
331
+ // #873: tmux holds older rows than the replica; its rows cannot line up.
332
+ if (this.#interpreter.omittedHistory() > 0) return { status: "mismatch" };
304
333
  const epoch = this.#subscriptionEpoch;
305
334
  const captured = await upstream.captureNativeBacking();
306
335
  if (captured.status !== "captured") return captured;
@@ -414,6 +443,7 @@ export class SessionRuntimeTerminalReplicaOwner {
414
443
  if (this.#disposed || this.#parked || this.#listeners.size > 0 || !this.#upstream) return;
415
444
  this.#parked = true;
416
445
  this.#stale = true;
446
+ this.#cancelBackfill();
417
447
  this.#upstream.freeze();
418
448
  }, PARK_AFTER_MS);
419
449
  }
@@ -480,6 +510,7 @@ export class SessionRuntimeTerminalReplicaOwner {
480
510
  this.#historyTimer = null;
481
511
  this.#parkTimer?.cancel();
482
512
  this.#parkTimer = null;
513
+ this.#cancelBackfill();
483
514
  this.#subscriptionEpoch += 1;
484
515
  await this.#interpreter.enqueue({ type: "close", reason });
485
516
  await this.#start.catch(() => undefined);
@@ -522,6 +553,8 @@ export class SessionRuntimeTerminalReplicaOwner {
522
553
  return;
523
554
  const seed = this.#interpreter.currentSeed();
524
555
  if (!seed) return;
556
+ // A native seed can leave out tmux's oldest rows (#873): tmux then holds
557
+ // that many more than the replica, until the backfill prepends them.
525
558
  const gap = size - seed.snapshot.history.length;
526
559
  // A seed with reconstructed tabs can differ from tmux in row count, and
527
560
  // a new capture would paint the same rows again. Accept the difference
@@ -534,7 +567,7 @@ export class SessionRuntimeTerminalReplicaOwner {
534
567
  return;
535
568
  }
536
569
  if (this.#reconstructedHistoryGap.gap === gap) return;
537
- } else if (gap === 0) return;
570
+ } else if (gap === this.#interpreter.omittedHistory()) return;
538
571
  upstream.reseed();
539
572
  } catch {
540
573
  // Unavailable metadata is not evidence that history disappeared.
@@ -545,6 +578,10 @@ export class SessionRuntimeTerminalReplicaOwner {
545
578
  }
546
579
 
547
580
  #observePane(event: MirrorPaneEvent): void {
581
+ if (event.type === "history") {
582
+ this.#observeHistoryPiece(event.piece);
583
+ return;
584
+ }
548
585
  if (event.type === "delta") {
549
586
  this.#historyDirty = true;
550
587
  this.#scheduleHistoryCheck();
@@ -554,6 +591,7 @@ export class SessionRuntimeTerminalReplicaOwner {
554
591
  // armed after reset belongs to the next post-capture delta, never to this
555
592
  // already-started reseed.
556
593
  this.#historyAnchors.invalidate();
594
+ this.#cancelBackfill();
557
595
  this.#reseed = {
558
596
  nativeCols: event.cols,
559
597
  nativeRows: event.rows,
@@ -591,35 +629,37 @@ export class SessionRuntimeTerminalReplicaOwner {
591
629
  const lease = reseed ? this.#qualifyReseed(reseed, event.x, event.y) : null;
592
630
  this.#supervise(
593
631
  reseed && lease
594
- ? this.#interpreter.enqueue({
595
- type: "reseed",
596
- nativeCols: reseed.nativeCols,
597
- nativeRows: reseed.nativeRows,
598
- cols: reseed.nativeCols,
599
- rows: reseed.nativeRows,
600
- chunks: reseed.chunks,
601
- captureChunks: reseed.captureChunks,
602
- native: reseed.native,
603
- historyLimit: event.historyLimit,
604
- historySize: event.historySize,
605
- trace: reseed.trace,
606
- // tmux can retain an offscreen x after a non-reflow width shrink
607
- // (screen_resize_cursor), as well as x === cols for wrap pending.
608
- // Preserve wrap pending in the parser. The backend's canonical
609
- // projection, not its input cursor, clamps to the final cell.
610
- cursor: { x: event.x, y: event.y },
611
- wraparound: event.wraparound,
612
- observedModes: event.observedModes,
613
- bootstrap: "painted-capture",
614
- validateBeforeCommit: () => this.#leaseIsCurrent(lease, reseed.subscriptionEpoch),
615
- onInvalidated: () =>
616
- this.#retryReseedOrFault(
617
- "terminal reseed layout lease crossed",
618
- reseed,
619
- "before-commit",
620
- "lease-crossed",
621
- ),
622
- })
632
+ ? this.#interpreter
633
+ .enqueue({
634
+ type: "reseed",
635
+ nativeCols: reseed.nativeCols,
636
+ nativeRows: reseed.nativeRows,
637
+ cols: reseed.nativeCols,
638
+ rows: reseed.nativeRows,
639
+ chunks: reseed.chunks,
640
+ captureChunks: reseed.captureChunks,
641
+ native: reseed.native,
642
+ historyLimit: event.historyLimit,
643
+ historySize: event.historySize,
644
+ trace: reseed.trace,
645
+ // tmux can retain an offscreen x after a non-reflow width shrink
646
+ // (screen_resize_cursor), as well as x === cols for wrap pending.
647
+ // Preserve wrap pending in the parser. The backend's canonical
648
+ // projection, not its input cursor, clamps to the final cell.
649
+ cursor: { x: event.x, y: event.y },
650
+ wraparound: event.wraparound,
651
+ observedModes: event.observedModes,
652
+ bootstrap: "painted-capture",
653
+ validateBeforeCommit: () => this.#leaseIsCurrent(lease, reseed.subscriptionEpoch),
654
+ onInvalidated: () =>
655
+ this.#retryReseedOrFault(
656
+ "terminal reseed layout lease crossed",
657
+ reseed,
658
+ "before-commit",
659
+ "lease-crossed",
660
+ ),
661
+ })
662
+ .then(() => this.#startBackfill())
623
663
  : reseed
624
664
  ? (this.#retryReseedOrFault(
625
665
  `terminal reseed geometry is incompatible: capture ${reseed.nativeCols}x${reseed.nativeRows}, layout ${reseed.layoutLease?.pane.width}x${reseed.layoutLease?.pane.height}, border ${reseed.layoutLease?.paneBorderStatus}`,
@@ -638,7 +678,9 @@ export class SessionRuntimeTerminalReplicaOwner {
638
678
  );
639
679
  } else if (event.type === "fault") {
640
680
  this.#noteLifecycle("upstream-fault");
641
- const error = new Error("Native terminal recovery failed");
681
+ const error = event.hold
682
+ ? new MirrorPaneHeldError(event.hold)
683
+ : new Error("Native terminal recovery failed");
642
684
  this.#interpreter.abort(error);
643
685
  this.#onFault?.(error);
644
686
  } else if (event.type === "closed") {
@@ -655,6 +697,89 @@ export class SessionRuntimeTerminalReplicaOwner {
655
697
  }
656
698
  }
657
699
 
700
+ #cancelBackfill(): void {
701
+ this.#backfillToken += 1;
702
+ this.#backfillFailures = 0;
703
+ this.#backfillTimer?.cancel();
704
+ this.#backfillTimer = null;
705
+ }
706
+
707
+ /** After a seed commits: ask for the rows it left out, if any. */
708
+ #startBackfill(): void {
709
+ this.#cancelBackfill();
710
+ void this.#askForHistory(this.#backfillToken);
711
+ }
712
+
713
+ async #askForHistory(token: number): Promise<void> {
714
+ await this.#interpreter.whenIdle();
715
+ if (
716
+ token !== this.#backfillToken ||
717
+ this.#disposed ||
718
+ this.#parked ||
719
+ this.#stale ||
720
+ this.#reseed ||
721
+ this.#waitingForGeometryCapture
722
+ )
723
+ return;
724
+ const omitted = this.#interpreter.omittedHistory();
725
+ const upstream = this.#upstream;
726
+ if (omitted <= 0 || !upstream?.requestNativeHistory) return;
727
+ const snapshot = this.#interpreter.currentSnapshot();
728
+ // tmux reads the normal screen's history; the piece is prepended there.
729
+ if (snapshot.modes.alternateScreen) {
730
+ this.#retryBackfill(token, false);
731
+ return;
732
+ }
733
+ // #873: the daemon holds at most the cap; rows past it stay in tmux, left
734
+ // out for good, and are never asked for.
735
+ const room = this.#interpreter.historyRowCap() - snapshot.history.length;
736
+ if (room <= 0) return;
737
+ const sent = upstream.requestNativeHistory({
738
+ endRow: omitted,
739
+ rows: Math.min(room, nativeHistoryPieceRows(snapshot.cols)),
740
+ historySize: omitted + snapshot.history.length,
741
+ });
742
+ if (sent) this.#backfillAsked.push(token);
743
+ else this.#retryBackfill(token, true);
744
+ }
745
+
746
+ #observeHistoryPiece(piece: Extract<MirrorPaneEvent, { type: "history" }>["piece"]): void {
747
+ const token = this.#backfillAsked.shift();
748
+ if (token === undefined || token !== this.#backfillToken || this.#disposed) return;
749
+ if (!piece) {
750
+ this.#retryBackfill(token, true);
751
+ return;
752
+ }
753
+ // Queued now, behind every delta tmux sent before it read the piece.
754
+ this.#supervise(
755
+ this.#interpreter.prependNativeHistory(piece).then((applied) => {
756
+ if (token !== this.#backfillToken) return;
757
+ if (applied) {
758
+ this.#backfillFailures = 0;
759
+ void this.#askForHistory(token);
760
+ } else this.#retryBackfill(token, true);
761
+ }),
762
+ );
763
+ }
764
+
765
+ #retryBackfill(token: number, failed: boolean): void {
766
+ if (token !== this.#backfillToken || this.#disposed) return;
767
+ let delay = BACKFILL_ALTERNATE_WAIT_MS;
768
+ if (failed) {
769
+ this.#backfillFailures += 1;
770
+ if (this.#backfillFailures >= BACKFILL_MAX_FAILURES) return;
771
+ delay = Math.min(
772
+ BACKFILL_MAX_RETRY_MS,
773
+ BACKFILL_FIRST_RETRY_MS * 2 ** (this.#backfillFailures - 1),
774
+ );
775
+ }
776
+ this.#backfillTimer?.cancel();
777
+ this.#backfillTimer = this.#scheduler.timer(() => {
778
+ this.#backfillTimer = null;
779
+ void this.#askForHistory(token);
780
+ }, delay);
781
+ }
782
+
658
783
  #observeLayout(event: MirrorLayoutEvent): void {
659
784
  const observed = this.#readLayoutLease(event);
660
785
  if (observed.kind === "irrelevant") return;
@@ -678,6 +803,7 @@ export class SessionRuntimeTerminalReplicaOwner {
678
803
  // One outstanding capture also coalesces geometry bursts; crossed leases
679
804
  // request the latest capture through the normal qualification retry path.
680
805
  this.#reseedRetryCount = 0;
806
+ this.#cancelBackfill();
681
807
  if (!this.#bootstrapped || this.#reseed || this.#waitingForGeometryCapture) return;
682
808
  this.#waitingForGeometryCapture = true;
683
809
  this.#historyAnchors.invalidate();
@@ -810,6 +936,7 @@ export class SessionRuntimeTerminalReplicaOwner {
810
936
  this.#noteLifecycle("reseed-retry", diagnostic);
811
937
  this.#reseedRetryCount += 1;
812
938
  this.#waitingForGeometryCapture = true;
939
+ this.#cancelBackfill();
813
940
  this.#historyAnchors.invalidate();
814
941
  void this.#start.then(() => {
815
942
  if (!this.#disposed) this.#upstream?.reseed();