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 +1,2 @@
1
- export {};
1
+ /** #873, owner decision of 25 Nov 2026: the daemon holds at most this many history rows per pane. */
2
+ export const DAEMON_HISTORY_ROW_CAP = 10_000;
@@ -5,6 +5,7 @@ import { detectWidgetMarkerFromReplicaRows, } from "@tmux-ide/contracts";
5
5
  import { applyTerminalReplicaPatch, assembleTerminalReplicaSnapshot, blankTerminalReplicaSnapshot, hashTerminalReplicaSnapshot, hashTerminalReplicaTombstone, hashTerminalWidgetContent, terminalReplicaRowsEqual, } from "@tmux-ide/core";
6
6
  import { SYSTEM_SESSION_RUNTIME_SCHEDULER, } from "./runtime-scheduler.js";
7
7
  import { DISABLED_SESSION_RUNTIME_OBSERVABILITY, } from "./runtime-observability.js";
8
+ import { DAEMON_HISTORY_ROW_CAP, } from "./terminal-interpreter-backend.js";
8
9
  import { createXtermTerminalInterpreterBackend } from "./xterm-terminal-interpreter-backend.js";
9
10
  import { CAUSAL_CELL_OSC, CausalCellLedger, cellsEqual, } from "./causal-cell-ledger.js";
10
11
  import { KITTY_PLACEMENT_OSC, KittyApcScanner, KittyImageStore, } from "../graphics/kitty-graphics.js";
@@ -47,6 +48,7 @@ export class TerminalReplicaInterpreter {
47
48
  #semanticPaneId;
48
49
  #incarnation;
49
50
  #scrollback;
51
+ #historyRowCap;
50
52
  #listeners = new Set();
51
53
  #onRawCommit;
52
54
  #scheduler;
@@ -113,6 +115,7 @@ export class TerminalReplicaInterpreter {
113
115
  this.#incarnation = options.incarnation;
114
116
  this.#revision = options.initialRevision ?? 0;
115
117
  this.#scrollback = options.scrollback ?? 5000;
118
+ this.#historyRowCap = options.historyRowCap ?? DAEMON_HISTORY_ROW_CAP;
116
119
  this.#onRawCommit = options.onRawCommit;
117
120
  this.#onNativeReseedRequired = options.onNativeReseedRequired;
118
121
  this.#scheduler = options.scheduler ?? SYSTEM_SESSION_RUNTIME_SCHEDULER;
@@ -122,6 +125,7 @@ export class TerminalReplicaInterpreter {
122
125
  cols: options.cols,
123
126
  rows: options.rows,
124
127
  scrollback: this.#scrollback,
128
+ historyRowCap: this.#historyRowCap,
125
129
  });
126
130
  this.#backend.startHistoryCollection?.();
127
131
  this.#kitty = new KittyImageStore(options.kittyImageBudget ? { budget: options.kittyImageBudget } : {});
@@ -177,6 +181,30 @@ export class TerminalReplicaInterpreter {
177
181
  : operation;
178
182
  return this.#append(admitted);
179
183
  }
184
+ /**
185
+ * #873: prepend the older history rows of a piece read from tmux, in order
186
+ * with the writes queued before it. True when the rows were prepended and a
187
+ * new revision published; false when the piece does not fit the replica as
188
+ * it is at that point of the stream (see the backend's prependNativeHistory).
189
+ */
190
+ prependNativeHistory(piece) {
191
+ this.#flushWrites();
192
+ const run = this.#tail.then(() => this.#applyHistoryPiece(piece));
193
+ this.#tail = run.then(() => undefined, () => undefined);
194
+ return run;
195
+ }
196
+ /**
197
+ * #873: tmux history rows older than the replica's oldest history row; tmux's
198
+ * history size is this plus the replica's history rows. Set by a native seed
199
+ * that left them out, zero after any other seed.
200
+ */
201
+ omittedHistory() {
202
+ return this.#backend.omittedHistory?.() ?? 0;
203
+ }
204
+ /** #873: the most history rows the replica holds; the backfill stops there. */
205
+ historyRowCap() {
206
+ return this.#historyRowCap;
207
+ }
180
208
  whenIdle() {
181
209
  this.#flushWrites();
182
210
  return this.#tail;
@@ -272,12 +300,13 @@ export class TerminalReplicaInterpreter {
272
300
  this.#causalCell?.fail("reseeded");
273
301
  const nativeCols = operation.nativeCols ?? operation.cols;
274
302
  const nativeRows = operation.nativeRows ?? operation.rows;
275
- const scrollback = Math.max(operation.historyLimit ?? this.#scrollback, operation.historySize ?? 0, operation.native?.history ?? 0);
303
+ const scrollback = Math.max(operation.historyLimit ?? this.#scrollback, operation.historySize ?? 0, (operation.native?.history ?? 0) + (operation.native?.omittedHistory ?? 0));
276
304
  const replacement = this.#backendFactory({
277
305
  cols: nativeCols,
278
306
  rows: nativeRows,
279
307
  scrollback,
280
308
  historyLimit: operation.historyLimit ?? scrollback,
309
+ historyRowCap: this.#historyRowCap,
281
310
  });
282
311
  try {
283
312
  if (operation.native && !replacement.importNativeGrid?.(operation.native))
@@ -416,6 +445,22 @@ export class TerminalReplicaInterpreter {
416
445
  this.#backend.dispose();
417
446
  this.#emit(update);
418
447
  }
448
+ #applyHistoryPiece(piece) {
449
+ if (this.#closed ||
450
+ this.#needsSeed ||
451
+ this.#nativeReseedRequested ||
452
+ this.#pendingResize !== null ||
453
+ this.#backend.requiresNativeReseed() ||
454
+ // A synchronized update publishes nothing until it ends.
455
+ this.#backend.modes().synchronizedOutput ||
456
+ !this.#backend.prependNativeHistory?.(piece))
457
+ return false;
458
+ // Image anchors are absolute buffer lines; prepended rows moved them all.
459
+ this.#kitty.dropCursorPlacements();
460
+ this.#commit(false);
461
+ this.#publishGraphics();
462
+ return true;
463
+ }
419
464
  async #write(data, continuedTrace, hasBacklog = () => false) {
420
465
  if (this.#closed || this.#nativeReseedRequested)
421
466
  return;
@@ -1,5 +1,7 @@
1
+ import { MirrorPaneHeldError } from "../mirror/pane-hold.js";
1
2
  import { HistoryAnchorOwner, } from "./history-anchor-owner.js";
2
3
  import { layoutContentRows } from "../mirror/layout-content-rows.js";
4
+ import { nativeHistoryPieceRows } from "../mirror/native-grid-capture.js";
3
5
  import { terminalReplicaRowsEqual } from "@tmux-ide/core";
4
6
  import { projectNativeGridRow } from "../mirror/native-grid-projection.js";
5
7
  import { TerminalReplicaInterpreter, } from "./terminal-replica-interpreter.js";
@@ -10,6 +12,20 @@ import { DISABLED_SESSION_RUNTIME_OBSERVABILITY } from "./runtime-observability.
10
12
  * enough that switching away and back stays warm.
11
13
  */
12
14
  export const PARK_AFTER_MS = 10_000;
15
+ /**
16
+ * #873 backfill: a native seed can leave out the oldest history rows; the
17
+ * owner then asks tmux for them piece by piece. A null answer (output crossed
18
+ * the read) or a piece that no longer fits is asked again after
19
+ * BACKFILL_FIRST_RETRY_MS, doubling up to BACKFILL_MAX_RETRY_MS. After
20
+ * BACKFILL_MAX_FAILURES in a row (about 48 s of a pane that never pauses) the
21
+ * owner stops until the next seed; the replica stays correct without the
22
+ * older rows, it only lacks them. While the alternate screen is active it
23
+ * checks again every BACKFILL_ALTERNATE_WAIT_MS without counting a failure.
24
+ */
25
+ export const BACKFILL_FIRST_RETRY_MS = 250;
26
+ export const BACKFILL_MAX_RETRY_MS = 4_000;
27
+ export const BACKFILL_MAX_FAILURES = 16;
28
+ export const BACKFILL_ALTERNATE_WAIT_MS = 1_000;
13
29
  /** One parser/replica owner for one semantic pane inside one SessionRuntime. */
14
30
  export class SessionRuntimeTerminalReplicaOwner {
15
31
  generation;
@@ -46,6 +62,14 @@ export class SessionRuntimeTerminalReplicaOwner {
46
62
  #historyChecking = false;
47
63
  #historyDirty = false;
48
64
  #reconstructedHistoryGap = null;
65
+ // #873 backfill. A new seed, resize, park or disposal starts a new token;
66
+ // anything from an older token is ignored.
67
+ #backfillToken = 0;
68
+ // The token of each history request sent and not yet answered, oldest first.
69
+ // Answers arrive in request order, so each one belongs to the first entry.
70
+ #backfillAsked = [];
71
+ #backfillFailures = 0;
72
+ #backfillTimer = null;
49
73
  constructor(generation, session, semanticPaneId, mirror, options) {
50
74
  this.generation = generation;
51
75
  this.session = session;
@@ -65,11 +89,13 @@ export class SessionRuntimeTerminalReplicaOwner {
65
89
  rows: this.#rows,
66
90
  scheduler: options.scheduler,
67
91
  observability: options.observability,
92
+ ...(options.historyRowCap !== undefined ? { historyRowCap: options.historyRowCap } : {}),
68
93
  onNativeReseedRequired: () => {
69
94
  if (this.#disposed || this.#reseed || this.#waitingForGeometryCapture)
70
95
  return;
71
96
  this.#waitingForGeometryCapture = true;
72
97
  this.#historyAnchors.invalidate();
98
+ this.#cancelBackfill();
73
99
  this.#supervise(this.#start.then(() => {
74
100
  if (!this.#disposed && this.#waitingForGeometryCapture)
75
101
  this.#upstream?.reseed();
@@ -196,6 +222,9 @@ export class SessionRuntimeTerminalReplicaOwner {
196
222
  const initial = this.#interpreter.currentSeed();
197
223
  if (!initial || this.#stale || this.#reseed || this.#waitingForGeometryCapture)
198
224
  return { status: "changed" };
225
+ // #873: tmux holds older rows than the replica; its rows cannot line up.
226
+ if (this.#interpreter.omittedHistory() > 0)
227
+ return { status: "mismatch" };
199
228
  const epoch = this.#subscriptionEpoch;
200
229
  const captured = await upstream.captureNativeBacking();
201
230
  if (captured.status !== "captured")
@@ -304,6 +333,7 @@ export class SessionRuntimeTerminalReplicaOwner {
304
333
  return;
305
334
  this.#parked = true;
306
335
  this.#stale = true;
336
+ this.#cancelBackfill();
307
337
  this.#upstream.freeze();
308
338
  }, PARK_AFTER_MS);
309
339
  }
@@ -359,6 +389,7 @@ export class SessionRuntimeTerminalReplicaOwner {
359
389
  this.#historyTimer = null;
360
390
  this.#parkTimer?.cancel();
361
391
  this.#parkTimer = null;
392
+ this.#cancelBackfill();
362
393
  this.#subscriptionEpoch += 1;
363
394
  await this.#interpreter.enqueue({ type: "close", reason });
364
395
  await this.#start.catch(() => undefined);
@@ -401,6 +432,8 @@ export class SessionRuntimeTerminalReplicaOwner {
401
432
  const seed = this.#interpreter.currentSeed();
402
433
  if (!seed)
403
434
  return;
435
+ // A native seed can leave out tmux's oldest rows (#873): tmux then holds
436
+ // that many more than the replica, until the backfill prepends them.
404
437
  const gap = size - seed.snapshot.history.length;
405
438
  // A seed with reconstructed tabs can differ from tmux in row count, and
406
439
  // a new capture would paint the same rows again. Accept the difference
@@ -415,7 +448,7 @@ export class SessionRuntimeTerminalReplicaOwner {
415
448
  if (this.#reconstructedHistoryGap.gap === gap)
416
449
  return;
417
450
  }
418
- else if (gap === 0)
451
+ else if (gap === this.#interpreter.omittedHistory())
419
452
  return;
420
453
  upstream.reseed();
421
454
  }
@@ -429,6 +462,10 @@ export class SessionRuntimeTerminalReplicaOwner {
429
462
  }
430
463
  }
431
464
  #observePane(event) {
465
+ if (event.type === "history") {
466
+ this.#observeHistoryPiece(event.piece);
467
+ return;
468
+ }
432
469
  if (event.type === "delta") {
433
470
  this.#historyDirty = true;
434
471
  this.#scheduleHistoryCheck();
@@ -438,6 +475,7 @@ export class SessionRuntimeTerminalReplicaOwner {
438
475
  // armed after reset belongs to the next post-capture delta, never to this
439
476
  // already-started reseed.
440
477
  this.#historyAnchors.invalidate();
478
+ this.#cancelBackfill();
441
479
  this.#reseed = {
442
480
  nativeCols: event.cols,
443
481
  nativeRows: event.rows,
@@ -479,7 +517,8 @@ export class SessionRuntimeTerminalReplicaOwner {
479
517
  this.#stale = false;
480
518
  const lease = reseed ? this.#qualifyReseed(reseed, event.x, event.y) : null;
481
519
  this.#supervise(reseed && lease
482
- ? this.#interpreter.enqueue({
520
+ ? this.#interpreter
521
+ .enqueue({
483
522
  type: "reseed",
484
523
  nativeCols: reseed.nativeCols,
485
524
  nativeRows: reseed.nativeRows,
@@ -502,6 +541,7 @@ export class SessionRuntimeTerminalReplicaOwner {
502
541
  validateBeforeCommit: () => this.#leaseIsCurrent(lease, reseed.subscriptionEpoch),
503
542
  onInvalidated: () => this.#retryReseedOrFault("terminal reseed layout lease crossed", reseed, "before-commit", "lease-crossed"),
504
543
  })
544
+ .then(() => this.#startBackfill())
505
545
  : reseed
506
546
  ? (this.#retryReseedOrFault(`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}`, reseed, "capture-qualification", this.#reseedRejection(reseed, event.x, event.y)),
507
547
  Promise.resolve())
@@ -515,7 +555,9 @@ export class SessionRuntimeTerminalReplicaOwner {
515
555
  }
516
556
  else if (event.type === "fault") {
517
557
  this.#noteLifecycle("upstream-fault");
518
- const error = new Error("Native terminal recovery failed");
558
+ const error = event.hold
559
+ ? new MirrorPaneHeldError(event.hold)
560
+ : new Error("Native terminal recovery failed");
519
561
  this.#interpreter.abort(error);
520
562
  this.#onFault?.(error);
521
563
  }
@@ -529,6 +571,87 @@ export class SessionRuntimeTerminalReplicaOwner {
529
571
  }, () => undefined);
530
572
  }
531
573
  }
574
+ #cancelBackfill() {
575
+ this.#backfillToken += 1;
576
+ this.#backfillFailures = 0;
577
+ this.#backfillTimer?.cancel();
578
+ this.#backfillTimer = null;
579
+ }
580
+ /** After a seed commits: ask for the rows it left out, if any. */
581
+ #startBackfill() {
582
+ this.#cancelBackfill();
583
+ void this.#askForHistory(this.#backfillToken);
584
+ }
585
+ async #askForHistory(token) {
586
+ await this.#interpreter.whenIdle();
587
+ if (token !== this.#backfillToken ||
588
+ this.#disposed ||
589
+ this.#parked ||
590
+ this.#stale ||
591
+ this.#reseed ||
592
+ this.#waitingForGeometryCapture)
593
+ return;
594
+ const omitted = this.#interpreter.omittedHistory();
595
+ const upstream = this.#upstream;
596
+ if (omitted <= 0 || !upstream?.requestNativeHistory)
597
+ return;
598
+ const snapshot = this.#interpreter.currentSnapshot();
599
+ // tmux reads the normal screen's history; the piece is prepended there.
600
+ if (snapshot.modes.alternateScreen) {
601
+ this.#retryBackfill(token, false);
602
+ return;
603
+ }
604
+ // #873: the daemon holds at most the cap; rows past it stay in tmux, left
605
+ // out for good, and are never asked for.
606
+ const room = this.#interpreter.historyRowCap() - snapshot.history.length;
607
+ if (room <= 0)
608
+ return;
609
+ const sent = upstream.requestNativeHistory({
610
+ endRow: omitted,
611
+ rows: Math.min(room, nativeHistoryPieceRows(snapshot.cols)),
612
+ historySize: omitted + snapshot.history.length,
613
+ });
614
+ if (sent)
615
+ this.#backfillAsked.push(token);
616
+ else
617
+ this.#retryBackfill(token, true);
618
+ }
619
+ #observeHistoryPiece(piece) {
620
+ const token = this.#backfillAsked.shift();
621
+ if (token === undefined || token !== this.#backfillToken || this.#disposed)
622
+ return;
623
+ if (!piece) {
624
+ this.#retryBackfill(token, true);
625
+ return;
626
+ }
627
+ // Queued now, behind every delta tmux sent before it read the piece.
628
+ this.#supervise(this.#interpreter.prependNativeHistory(piece).then((applied) => {
629
+ if (token !== this.#backfillToken)
630
+ return;
631
+ if (applied) {
632
+ this.#backfillFailures = 0;
633
+ void this.#askForHistory(token);
634
+ }
635
+ else
636
+ this.#retryBackfill(token, true);
637
+ }));
638
+ }
639
+ #retryBackfill(token, failed) {
640
+ if (token !== this.#backfillToken || this.#disposed)
641
+ return;
642
+ let delay = BACKFILL_ALTERNATE_WAIT_MS;
643
+ if (failed) {
644
+ this.#backfillFailures += 1;
645
+ if (this.#backfillFailures >= BACKFILL_MAX_FAILURES)
646
+ return;
647
+ delay = Math.min(BACKFILL_MAX_RETRY_MS, BACKFILL_FIRST_RETRY_MS * 2 ** (this.#backfillFailures - 1));
648
+ }
649
+ this.#backfillTimer?.cancel();
650
+ this.#backfillTimer = this.#scheduler.timer(() => {
651
+ this.#backfillTimer = null;
652
+ void this.#askForHistory(token);
653
+ }, delay);
654
+ }
532
655
  #observeLayout(event) {
533
656
  const observed = this.#readLayoutLease(event);
534
657
  if (observed.kind === "irrelevant")
@@ -555,6 +678,7 @@ export class SessionRuntimeTerminalReplicaOwner {
555
678
  // One outstanding capture also coalesces geometry bursts; crossed leases
556
679
  // request the latest capture through the normal qualification retry path.
557
680
  this.#reseedRetryCount = 0;
681
+ this.#cancelBackfill();
558
682
  if (!this.#bootstrapped || this.#reseed || this.#waitingForGeometryCapture)
559
683
  return;
560
684
  this.#waitingForGeometryCapture = true;
@@ -669,6 +793,7 @@ export class SessionRuntimeTerminalReplicaOwner {
669
793
  this.#noteLifecycle("reseed-retry", diagnostic);
670
794
  this.#reseedRetryCount += 1;
671
795
  this.#waitingForGeometryCapture = true;
796
+ this.#cancelBackfill();
672
797
  this.#historyAnchors.invalidate();
673
798
  void this.#start.then(() => {
674
799
  if (!this.#disposed)
@@ -1,8 +1,9 @@
1
- import { isNativeBootstrapCapture } from "../mirror/native-grid-capture.js";
1
+ import { isNativeBootstrapCapture, } from "../mirror/native-grid-capture.js";
2
2
  import { projectNativeGridRow } from "../mirror/native-grid-projection.js";
3
3
  import { Terminal } from "@tmux-ide/xterm-headless";
4
4
  import { Unicode11Addon } from "@xterm/addon-unicode11";
5
5
  import { createProjectedTerminalReplicaRowBuilder } from "@tmux-ide/core";
6
+ import { DAEMON_HISTORY_ROW_CAP, } from "./terminal-interpreter-backend.js";
6
7
  /** Pinned xterm 6 adapter and the rollback oracle for native backends. */
7
8
  export class XtermTerminalInterpreterBackend {
8
9
  kind = "xterm";
@@ -23,9 +24,14 @@ export class XtermTerminalInterpreterBackend {
23
24
  #nativeModesObserved = false;
24
25
  #historyProjectionInvalidated = false;
25
26
  #scrollOnClear;
27
+ /** #873: tmux history rows older than the oldest row held (see omittedHistory). */
28
+ #omittedHistory = 0;
29
+ /** #873: the most history rows held; rows pushed past it count as left out. */
30
+ #historyRowCap;
26
31
  constructor(options) {
27
32
  const historyLimit = options.historyLimit ?? options.scrollback;
28
33
  this.#historyLimit = historyLimit;
34
+ this.#historyRowCap = options.historyRowCap ?? DAEMON_HISTORY_ROW_CAP;
29
35
  this.#terminal = new Terminal({
30
36
  cols: options.cols,
31
37
  rows: options.rows,
@@ -96,31 +102,78 @@ export class XtermTerminalInterpreterBackend {
96
102
  */
97
103
  startHistoryCollection() {
98
104
  this.#collecting = true;
105
+ this.#enforceRowCap();
99
106
  this.#lastYbase = this.#normalBuffer()?.ybase ?? null;
100
107
  }
108
+ historyRowCap() {
109
+ return this.#historyRowCap;
110
+ }
111
+ /**
112
+ * #873: hold at most #historyRowCap history rows. The oldest rows past it
113
+ * are dropped here and counted as left out, so tmux's history size is still
114
+ * the rows held plus the rows left out. tmux itself keeps them.
115
+ */
116
+ #enforceRowCap() {
117
+ const buffer = this.#normalBuffer();
118
+ if (!buffer || buffer.ybase <= this.#historyRowCap)
119
+ return;
120
+ const extra = buffer.ybase - this.#historyRowCap;
121
+ buffer.lines.trimStart(extra);
122
+ buffer.ybase -= extra;
123
+ buffer.ydisp = Math.max(0, buffer.ydisp - extra);
124
+ this.#setOmittedHistory(this.#omittedHistory + extra);
125
+ // No projection invalidation: the incremental projection reads a trim at
126
+ // the old end like tmux's own collection, so a scroll at the cap stays a
127
+ // one-row delta. Seeds and prepends invalidate on their own.
128
+ }
101
129
  #normalBuffer() {
102
130
  return this.#terminal._core?.buffers?.normal;
103
131
  }
132
+ omittedHistory() {
133
+ return this.#omittedHistory;
134
+ }
135
+ /**
136
+ * The parser's own erase-in-display collection counts only the rows held.
137
+ * While tmux holds older rows than these, turn it off: #collectHistory then
138
+ * collects after each of the erase's scrolls instead, which leaves the same
139
+ * rows as tmux's collection before it.
140
+ */
141
+ #setOmittedHistory(count) {
142
+ this.#omittedHistory = count;
143
+ this.#terminal.options.tmuxHistoryLimit =
144
+ count > 0 ? Number.MAX_SAFE_INTEGER : this.#historyLimit;
145
+ }
104
146
  #collectHistory() {
105
147
  const buffer = this.#normalBuffer();
106
148
  if (!buffer)
107
149
  return;
108
150
  const before = this.#lastYbase;
109
151
  const limit = this.#historyLimit;
152
+ const omitted = this.#omittedHistory;
110
153
  // Only a plain one-row scroll from at or above the limit is collected
111
154
  // here. ED2 collects before its own scrolls (one-column.patch), and any
112
155
  // other change (reset, resize, a capped ring) is left alone: a history
113
156
  // that still differs from tmux is caught by the owner's size check.
157
+ // tmux's history size counts the rows a native seed left out, and its
158
+ // collection drops the oldest rows: those first, then rows held.
114
159
  if (this.#collecting &&
115
160
  limit > 0 &&
116
161
  before !== null &&
117
- before >= limit &&
162
+ omitted + before >= limit &&
118
163
  buffer.ybase === before + 1) {
119
- const trim = Math.min(buffer.ybase, Math.max(1, Math.floor(limit / 10)));
120
- buffer.lines.trimStart(trim);
121
- buffer.ybase -= trim;
122
- buffer.ydisp = Math.max(0, buffer.ydisp - trim);
164
+ const collected = Math.min(omitted + buffer.ybase, Math.max(1, Math.floor(limit / 10)));
165
+ const fromOmitted = Math.min(omitted, collected);
166
+ if (fromOmitted > 0)
167
+ this.#setOmittedHistory(omitted - fromOmitted);
168
+ const trim = collected - fromOmitted;
169
+ if (trim > 0) {
170
+ buffer.lines.trimStart(trim);
171
+ buffer.ybase -= trim;
172
+ buffer.ydisp = Math.max(0, buffer.ydisp - trim);
173
+ }
123
174
  }
175
+ if (this.#collecting)
176
+ this.#enforceRowCap();
124
177
  this.#lastYbase = buffer.ybase;
125
178
  }
126
179
  get cols() {
@@ -180,6 +233,9 @@ export class XtermTerminalInterpreterBackend {
180
233
  const cell = this.#terminal.buffer.active.getNullCell();
181
234
  if (typeof cell.setFromCharData !== "function")
182
235
  throw new Error("Unsupported @tmux-ide/xterm-headless 6 native cell shape");
236
+ const omitted = snapshot.omittedHistory ?? 0;
237
+ if (!Number.isSafeInteger(omitted) || omitted < 0)
238
+ return false;
183
239
  if (buffer.lines.maxLength < snapshot.grid.length)
184
240
  return false;
185
241
  buffer.lines.length = 0;
@@ -220,9 +276,62 @@ export class XtermTerminalInterpreterBackend {
220
276
  ],
221
277
  }, 1).cells[0];
222
278
  [handler._curAttrData.fg, handler._curAttrData.bg] = nativeImportAttributes(current);
279
+ this.#setOmittedHistory(omitted);
223
280
  this.#historyProjectionInvalidated = true;
224
281
  return true;
225
282
  }
283
+ prependNativeHistory(piece) {
284
+ const omitted = this.#omittedHistory;
285
+ const buffer = this.#normalBuffer();
286
+ if (!buffer ||
287
+ omitted <= 0 ||
288
+ this.#terminal.buffer.active.type !== "normal" ||
289
+ piece.cols !== this.cols ||
290
+ piece.historySize !== omitted + buffer.ybase ||
291
+ !Number.isSafeInteger(piece.firstRow) ||
292
+ piece.firstRow < 0 ||
293
+ piece.firstRow >= omitted ||
294
+ piece.firstRow + piece.rows.length < omitted ||
295
+ !this.canImportNativeGrid())
296
+ return false;
297
+ // Rows numbered below the oldest row held; a piece can overlap rows held.
298
+ const count = omitted - piece.firstRow;
299
+ if (buffer.lines.length + count > buffer.lines.maxLength)
300
+ return false;
301
+ const lines = piece.rows
302
+ .slice(0, count)
303
+ .map((row, index) => this.#nativeLine(row, index > 0 && (piece.rows[index - 1].flags & 1) !== 0));
304
+ // In slices: one call with every row as an argument can exceed the stack.
305
+ for (let offset = 0; offset < lines.length; offset += 4096)
306
+ buffer.lines.splice(offset, 0, ...lines.slice(offset, offset + 4096));
307
+ // The import marked the row that was oldest as unwrapped: it had no row before it.
308
+ const boundary = buffer.lines.get(count);
309
+ if (boundary)
310
+ boundary.isWrapped = (piece.rows[count - 1].flags & 1) !== 0;
311
+ buffer.ybase += count;
312
+ buffer.ydisp += count;
313
+ if (this.#lastYbase !== null)
314
+ this.#lastYbase += count;
315
+ this.#setOmittedHistory(piece.firstRow);
316
+ this.#enforceRowCap();
317
+ this.#historyProjectionInvalidated = true;
318
+ return true;
319
+ }
320
+ /** One native row as a parser line, as importNativeGrid writes it. */
321
+ #nativeLine(source, wrapped) {
322
+ const buffer = this.#terminal.buffer.active
323
+ ._buffer;
324
+ const cell = this.#terminal.buffer.active.getNullCell();
325
+ const row = projectNativeGridRow(source, this.cols, 0, wrapped);
326
+ const line = buffer.getBlankLine(undefined, row.wrapped);
327
+ for (let column = 0; column < row.cells.length; column++) {
328
+ const projected = row.cells[column];
329
+ cell.setFromCharData([0, projected.grapheme, projected.width, 0]);
330
+ [cell.fg, cell.bg] = nativeImportAttributes(projected);
331
+ line.setCell(column, cell);
332
+ }
333
+ return line;
334
+ }
226
335
  prioritizeNextWrite() {
227
336
  this.#terminal.prioritizeNextWrite();
228
337
  }