tmux-ide 2.9.25 → 2.9.27
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.
- package/README.md +1 -1
- package/bin/cli.js +1577 -559
- package/package.json +3 -2
- package/packages/contracts/src/pane-stream.ts +28 -0
- package/packages/contracts/src/terminal-delivery.ts +122 -5
- package/packages/contracts/src/terminal-runtime-inventory.ts +61 -1
- package/packages/contracts/src/tmux-server-scope.ts +18 -0
- package/packages/core/src/index.ts +1 -0
- package/packages/core/src/terminal-delivery-binary.ts +8 -7
- package/packages/core/src/terminal-delivery.ts +174 -3
- package/packages/core/src/terminal-partial-history.ts +152 -0
- package/packages/core/src/terminal-replica.ts +70 -1
- package/packages/daemon/dist/command-center/owner-keep-alive.js +30 -0
- package/packages/daemon/dist/command-center/resources/terminal-runtime-inventory.js +42 -2
- package/packages/daemon/dist/command-center/server.js +3 -0
- package/packages/daemon/dist/command-center/tmux-server-attach.js +51 -0
- package/packages/daemon/dist/command-center/tmux-servers.js +95 -7
- package/packages/daemon/dist/doctor.js +4 -4
- package/packages/daemon/dist/lib/app-config.js +7 -0
- package/packages/daemon/dist/lib/node-requirement.js +20 -0
- package/packages/daemon/dist/lib/require-supported-node.js +8 -0
- package/packages/daemon/dist/lib/tmux-server-owner.js +4 -2
- package/packages/daemon/dist/lib/tmux-server-session-open.js +38 -12
- package/packages/daemon/dist/lib/workspace-promotion.js +4 -4
- package/packages/daemon/dist/native/TmuxIdeNotifier.app/Contents/Info.plist +2 -2
- package/packages/daemon/dist/native/TmuxIdeNotifier.app/Contents/MacOS/tmux-ide-notifier +0 -0
- package/packages/daemon/dist/native/TmuxIdeNotifier.app/Contents/Resources/Assets.car +0 -0
- package/packages/daemon/dist/native/TmuxIdeNotifier.app/Contents/_CodeSignature/CodeResources +2 -2
- package/packages/daemon/dist/server/pane-stream-upgrade.js +39 -1
- package/packages/daemon/dist/terminal/mirror/session-channel.js +6 -1
- package/packages/daemon/dist/terminal/pane-stream/lease-manager.js +1 -0
- package/packages/daemon/dist/terminal/pane-stream/pane-stream-websocket.js +112 -22
- package/packages/daemon/dist/terminal/protocol/browser-icon.js +1 -1
- package/packages/daemon/dist/terminal/protocol/session-descriptor-discovery.js +26 -3
- package/packages/daemon/dist/terminal/session-runtime/registry.js +2 -0
- package/packages/daemon/dist/terminal/session-runtime/terminal-delivery-hub.js +571 -33
- package/packages/daemon/dist/terminal/session-runtime/terminal-replica-interpreter.js +2 -0
- package/packages/daemon/dist/terminal/session-runtime/xterm-terminal-interpreter-backend.js +51 -4
- package/packages/daemon/dist/tui/mirror/open-tui-workspace-runtime-port.js +76 -2
- package/packages/daemon/dist/tui/mirror/theme.js +8 -1
- package/packages/daemon/src/command-center/attach-window-rule.cases.json +84 -0
- package/packages/daemon/src/command-center/owner-keep-alive.ts +38 -0
- package/packages/daemon/src/command-center/resources/terminal-runtime-inventory.ts +48 -1
- package/packages/daemon/src/command-center/server.ts +3 -0
- package/packages/daemon/src/command-center/tmux-server-attach.ts +75 -0
- package/packages/daemon/src/command-center/tmux-servers.ts +103 -6
- package/packages/daemon/src/doctor.ts +4 -4
- package/packages/daemon/src/lib/app-config.ts +15 -0
- package/packages/daemon/src/lib/node-requirement.ts +22 -0
- package/packages/daemon/src/lib/require-supported-node.ts +9 -0
- package/packages/daemon/src/lib/tmux-server-owner.ts +6 -3
- package/packages/daemon/src/lib/tmux-server-session-open.ts +53 -15
- package/packages/daemon/src/lib/workspace-promotion.ts +4 -4
- package/packages/daemon/src/server/pane-stream-upgrade.ts +41 -1
- package/packages/daemon/src/terminal/mirror/events.ts +2 -0
- package/packages/daemon/src/terminal/mirror/session-channel.ts +8 -1
- package/packages/daemon/src/terminal/pane-stream/lease-manager.ts +3 -0
- package/packages/daemon/src/terminal/pane-stream/pane-stream-websocket.ts +136 -20
- package/packages/daemon/src/terminal/protocol/browser-icon.ts +1 -1
- package/packages/daemon/src/terminal/protocol/session-descriptor-discovery.ts +32 -2
- package/packages/daemon/src/terminal/session-runtime/registry.ts +2 -0
- package/packages/daemon/src/terminal/session-runtime/terminal-delivery-hub.ts +696 -30
- package/packages/daemon/src/terminal/session-runtime/terminal-interpreter-backend.ts +6 -0
- package/packages/daemon/src/terminal/session-runtime/terminal-replica-interpreter.ts +2 -0
- package/packages/daemon/src/terminal/session-runtime/xterm-terminal-interpreter-backend.ts +67 -3
- package/packages/daemon/src/tui/mirror/open-tui-workspace-runtime-port.ts +82 -2
- package/packages/daemon/src/tui/mirror/theme.ts +13 -1
|
@@ -31,6 +31,13 @@ import {
|
|
|
31
31
|
TerminalDeliveryNackSchemaZ,
|
|
32
32
|
TerminalDeliveryOfferSchemaZ,
|
|
33
33
|
TerminalDeliveryVisibilitySchemaZ,
|
|
34
|
+
TerminalDeliveryHistoryRequestSchemaZ,
|
|
35
|
+
TERMINAL_DELIVERY_HISTORY_TAIL_ROWS,
|
|
36
|
+
TERMINAL_DELIVERY_MAX_PIPELINED,
|
|
37
|
+
TERMINAL_DELIVERY_PIPELINING_CAPABILITY_VERSION,
|
|
38
|
+
TERMINAL_HISTORY_EMPTY_DIGEST,
|
|
39
|
+
type TerminalDeliveryHistoryCommitment,
|
|
40
|
+
type TerminalDeliveryHistoryRequest,
|
|
34
41
|
type CanonicalTerminalReplicaUpdate,
|
|
35
42
|
type SessionRuntimeGeneration,
|
|
36
43
|
type TerminalDeliveryAck,
|
|
@@ -60,6 +67,10 @@ import {
|
|
|
60
67
|
hashTerminalDeliveryRepresentation,
|
|
61
68
|
negotiateTerminalDelivery,
|
|
62
69
|
preaccountSemanticTerminalUpdateBytes,
|
|
70
|
+
encodeSemanticTerminalHistory,
|
|
71
|
+
terminalHistoryPrefixCommitment,
|
|
72
|
+
dropTerminalHistoryCommitmentPrefix,
|
|
73
|
+
popTerminalHistoryCommitmentSuffix,
|
|
63
74
|
type TerminalReplicaState,
|
|
64
75
|
} from "@tmux-ide/core";
|
|
65
76
|
import type {
|
|
@@ -84,10 +95,38 @@ export const MAX_RAW_JOURNAL_BYTES = 4 * 1024 * 1024;
|
|
|
84
95
|
export const MAX_REPRESENTATION_CACHE_ENTRIES = 8;
|
|
85
96
|
export const MAX_REPRESENTATION_CACHE_BYTES = 16 * 1024 * 1024;
|
|
86
97
|
export const MAX_CLIENTS = 64;
|
|
98
|
+
/**
|
|
99
|
+
* Capability 122: one history frame's encoded bytes, well under the
|
|
100
|
+
* connection's unacknowledged budget, so history never holds live output.
|
|
101
|
+
*/
|
|
102
|
+
export const HISTORY_FRAME_MAX_BYTES = 128 * 1024;
|
|
87
103
|
const MAX_PANES = 32;
|
|
88
104
|
const MAX_PENDING_CANONICAL_CELLS = 1_000_000;
|
|
89
105
|
const MAX_CONNECTIONS = MAX_CLIENTS * MAX_PANES;
|
|
90
106
|
const BACKGROUND_CADENCE_MS = 100;
|
|
107
|
+
/**
|
|
108
|
+
* One connection's unacknowledged delivery bytes, across its panes, before a
|
|
109
|
+
* pane other than the one it types into waits. A remote link (SSH) queues
|
|
110
|
+
* every sent byte ahead of the next input-ack and echo; this bounds that queue
|
|
111
|
+
* to about this much plus one delivery. Daemon policy only: nothing on the wire.
|
|
112
|
+
*/
|
|
113
|
+
export const TERMINAL_DELIVERY_CONNECTION_UNACKED_BYTES = 256 * 1024;
|
|
114
|
+
/** No pane is held longer than this: it is then sent behind whatever is in flight. */
|
|
115
|
+
export const TERMINAL_DELIVERY_CONNECTION_HOLD_MS = 250;
|
|
116
|
+
/**
|
|
117
|
+
* A pane is held only once some delivery of its connection has gone this long
|
|
118
|
+
* unacknowledged: a fast link acks bulk within a few round trips, and holding
|
|
119
|
+
* there only bunches panes up behind each other.
|
|
120
|
+
*/
|
|
121
|
+
export const TERMINAL_DELIVERY_CONNECTION_CONGESTED_MS = 30;
|
|
122
|
+
/**
|
|
123
|
+
* Capability 124 (TERMINAL_DELIVERY_PIPELINING_CAPABILITY_VERSION): over a
|
|
124
|
+
* long link an echo commits while the previous echo's delivery is still
|
|
125
|
+
* unacknowledged, and waiting for that ACK costs up to one more round trip
|
|
126
|
+
* per key. A visible pane of a client that offers 124 sends up to this many
|
|
127
|
+
* patches before the first is acknowledged. Daemon policy over that capability.
|
|
128
|
+
*/
|
|
129
|
+
export { TERMINAL_DELIVERY_MAX_PIPELINED };
|
|
91
130
|
|
|
92
131
|
export interface TerminalDeliveryMetrics {
|
|
93
132
|
/** Unique delivery subscribers, independent of their pane count. */
|
|
@@ -142,7 +181,14 @@ export interface TerminalDeliveryConnection {
|
|
|
142
181
|
projectionReceipt?(receipt: TerminalProjectionReceipt): void;
|
|
143
182
|
materialize?(request: TerminalProjectionMaterialize): void;
|
|
144
183
|
nack(nack: TerminalDeliveryNack): void;
|
|
184
|
+
/** Capability 122: older history rows for a client holding a partial history. */
|
|
185
|
+
historyRequest?(request: TerminalDeliveryHistoryRequest): void;
|
|
145
186
|
setVisibility(visibility: TerminalDeliveryVisibility): void;
|
|
187
|
+
/**
|
|
188
|
+
* The client typed into this pane: its deliveries go ahead of the
|
|
189
|
+
* connection's other panes and never wait for their acknowledgements.
|
|
190
|
+
*/
|
|
191
|
+
prioritize?(): void;
|
|
146
192
|
close(): Promise<void>;
|
|
147
193
|
/**
|
|
148
194
|
* Kitty graphics side channel, present only when the client offered
|
|
@@ -227,14 +273,47 @@ interface ClientState {
|
|
|
227
273
|
lastProjectionReceipt: TerminalProjectionReceipt | null;
|
|
228
274
|
lastMaterialize: TerminalProjectionMaterialize | null;
|
|
229
275
|
reseedRequired: boolean;
|
|
276
|
+
/**
|
|
277
|
+
* Capability 122: history rows the client lacks at its baseline (oldest
|
|
278
|
+
* first), and the rows it holds. Both zero for a complete-history client.
|
|
279
|
+
*/
|
|
280
|
+
historyUnloadedRows: number;
|
|
281
|
+
/** The digest of those unloaded rows, kept so updates cost O(trim). */
|
|
282
|
+
historyUnloadedDigest: string;
|
|
283
|
+
historyLoadedRows: number;
|
|
284
|
+
/** Rows the client asked for and has not been sent; null when none. */
|
|
285
|
+
pendingHistoryRows: number | null;
|
|
286
|
+
/** Encoded bytes per row of the last history frame: sizes the next one. */
|
|
287
|
+
historyBytesPerRow: number | null;
|
|
288
|
+
/** New output went out since the request: the history frame goes next. */
|
|
289
|
+
revisionSinceHistoryRequest: boolean;
|
|
290
|
+
/** The oldest unacknowledged delivery; `pipelined` holds the ones sent after it. */
|
|
230
291
|
inFlight: {
|
|
231
292
|
envelope: DeliveryEnvelope;
|
|
232
293
|
projection: ProjectionBaseline | null;
|
|
294
|
+
/** Capability 122: the client's unloaded and loaded rows once this is acknowledged. */
|
|
295
|
+
historyRows: {
|
|
296
|
+
readonly unloaded: number;
|
|
297
|
+
readonly digest: string;
|
|
298
|
+
readonly loaded: number;
|
|
299
|
+
} | null;
|
|
233
300
|
deliveryOrdinal: number;
|
|
234
301
|
bytes: Uint8Array;
|
|
235
302
|
nextChunk: number;
|
|
236
303
|
sentAt: number;
|
|
237
304
|
} | null;
|
|
305
|
+
/** Capability 124: the client takes patches behind unacknowledged ones. */
|
|
306
|
+
readonly pipelining: boolean;
|
|
307
|
+
/**
|
|
308
|
+
* Patches sent after `inFlight`, oldest first, each based on the one before
|
|
309
|
+
* (TERMINAL_DELIVERY_MAX_PIPELINED). ACKs settle `inFlight` first.
|
|
310
|
+
*/
|
|
311
|
+
pipelined: DeliveryFlight[];
|
|
312
|
+
/**
|
|
313
|
+
* Transactions dropped unacknowledged (by a NACK of an earlier flight, or a
|
|
314
|
+
* fault). The client may still ACK or NACK them; that feedback is inert.
|
|
315
|
+
*/
|
|
316
|
+
droppedTransactions: Set<string>;
|
|
238
317
|
latestRevision: number | null;
|
|
239
318
|
encoding: PendingClientEncoding | null;
|
|
240
319
|
scheduled: boolean;
|
|
@@ -250,8 +329,14 @@ interface ClientState {
|
|
|
250
329
|
/** Delivery displaced by authoritative source close; its racing ACK is benign. */
|
|
251
330
|
sourceClosedFlight: DeliveryEnvelope | null;
|
|
252
331
|
backgroundTimer: SessionRuntimeTimer | null;
|
|
332
|
+
/** Waiting for its connection's other panes to be acknowledged. */
|
|
333
|
+
budgetHeld: boolean;
|
|
334
|
+
/** When the current hold began (it ends by TERMINAL_DELIVERY_CONNECTION_HOLD_MS). */
|
|
335
|
+
budgetHeldSince: number | null;
|
|
336
|
+
budgetTimer: SessionRuntimeTimer | null;
|
|
253
337
|
}
|
|
254
338
|
|
|
339
|
+
type DeliveryFlight = NonNullable<ClientState["inFlight"]>;
|
|
255
340
|
type SeedEncodingOutcome = { readonly bytes: Uint8Array } | { readonly error: unknown };
|
|
256
341
|
interface PendingSeedEncoding {
|
|
257
342
|
readonly key: string;
|
|
@@ -266,6 +351,7 @@ interface PendingClientEncoding {
|
|
|
266
351
|
readonly baselineRevision: number;
|
|
267
352
|
readonly baselineHash: string | null;
|
|
268
353
|
readonly reseedRequired: boolean;
|
|
354
|
+
readonly historyUnloadedRows: number;
|
|
269
355
|
readonly traceStarted: number;
|
|
270
356
|
}
|
|
271
357
|
class DeferredProjectionEncoding extends Error {
|
|
@@ -274,7 +360,11 @@ class DeferredProjectionEncoding extends Error {
|
|
|
274
360
|
}
|
|
275
361
|
}
|
|
276
362
|
class DeferredSeedEncoding extends Error {
|
|
277
|
-
constructor(
|
|
363
|
+
constructor(
|
|
364
|
+
readonly payload: TerminalSemanticDeliveryPayload,
|
|
365
|
+
/** Capability 122: the history tail this seed carries, part of its encoding key. */
|
|
366
|
+
readonly variant = "complete",
|
|
367
|
+
) {
|
|
278
368
|
super("Deferred compact seed encoding");
|
|
279
369
|
}
|
|
280
370
|
}
|
|
@@ -288,9 +378,16 @@ interface CachedRepresentation {
|
|
|
288
378
|
| "alternate-projection-binary-v1";
|
|
289
379
|
readonly projection?: ProjectionBaseline;
|
|
290
380
|
readonly projectionBaseRevision?: number | null;
|
|
291
|
-
readonly frame: "seed" | "patch" | "tombstone";
|
|
381
|
+
readonly frame: "seed" | "patch" | "tombstone" | "history";
|
|
292
382
|
readonly canonicalEquivalent: boolean;
|
|
293
|
-
readonly history: "complete" | "truncated" | "not-applicable" | "deferred";
|
|
383
|
+
readonly history: "complete" | "partial" | "truncated" | "not-applicable" | "deferred";
|
|
384
|
+
/** Capability 122: what the client will lack and hold once this is acknowledged. */
|
|
385
|
+
readonly historyUnloaded?: TerminalDeliveryHistoryCommitment;
|
|
386
|
+
readonly historyRows?: {
|
|
387
|
+
readonly unloaded: number;
|
|
388
|
+
readonly digest: string;
|
|
389
|
+
readonly loaded: number;
|
|
390
|
+
};
|
|
294
391
|
readonly representationHash?: string;
|
|
295
392
|
/** Internal reachability metadata; never serialized onto the wire. */
|
|
296
393
|
readonly cachePaneId?: string;
|
|
@@ -373,6 +470,8 @@ export class SessionRuntimeTerminalDeliveryHub {
|
|
|
373
470
|
readonly #observability: SessionRuntimeObservability;
|
|
374
471
|
readonly #panes = new Map<string, PaneState>();
|
|
375
472
|
readonly #clients = new Map<string, ClientState>();
|
|
473
|
+
/** Per connection: the pane it last typed into. */
|
|
474
|
+
readonly #priority = new Map<string, string>();
|
|
376
475
|
#sourceCloseFailures = 0;
|
|
377
476
|
#pendingSourceCloses = 0;
|
|
378
477
|
/** Synchronous reservations held while an async pane source is starting. */
|
|
@@ -390,6 +489,8 @@ export class SessionRuntimeTerminalDeliveryHub {
|
|
|
390
489
|
#deliveryLifecycleOrdinal = 0;
|
|
391
490
|
#deliveryStatusOrdinal = 0;
|
|
392
491
|
#closed = false;
|
|
492
|
+
readonly #holdMs: number;
|
|
493
|
+
readonly #congestedMs: number;
|
|
393
494
|
|
|
394
495
|
constructor(
|
|
395
496
|
readonly generation: SessionRuntimeGeneration,
|
|
@@ -398,9 +499,15 @@ export class SessionRuntimeTerminalDeliveryHub {
|
|
|
398
499
|
options: {
|
|
399
500
|
readonly scheduler?: SessionRuntimeScheduler;
|
|
400
501
|
readonly observability?: SessionRuntimeObservability;
|
|
502
|
+
/** Tests only: the longest a pane waits for its connection's bulk. */
|
|
503
|
+
readonly connectionHoldMs?: number;
|
|
504
|
+
/** Tests only: how long unacked bulk takes to count as congestion. */
|
|
505
|
+
readonly connectionCongestedMs?: number;
|
|
401
506
|
} = {},
|
|
402
507
|
) {
|
|
403
508
|
this.#ownerForPane = ownerForPane;
|
|
509
|
+
this.#holdMs = options.connectionHoldMs ?? TERMINAL_DELIVERY_CONNECTION_HOLD_MS;
|
|
510
|
+
this.#congestedMs = options.connectionCongestedMs ?? TERMINAL_DELIVERY_CONNECTION_CONGESTED_MS;
|
|
404
511
|
this.#scheduler = options.scheduler ?? SYSTEM_SESSION_RUNTIME_SCHEDULER;
|
|
405
512
|
this.#observability = options.observability ?? DISABLED_SESSION_RUNTIME_OBSERVABILITY;
|
|
406
513
|
}
|
|
@@ -470,7 +577,18 @@ export class SessionRuntimeTerminalDeliveryHub {
|
|
|
470
577
|
lastProjectionReceipt: null,
|
|
471
578
|
lastMaterialize: null,
|
|
472
579
|
reseedRequired: false,
|
|
580
|
+
historyUnloadedRows: 0,
|
|
581
|
+
historyUnloadedDigest: TERMINAL_HISTORY_EMPTY_DIGEST,
|
|
582
|
+
historyLoadedRows: 0,
|
|
583
|
+
pendingHistoryRows: null,
|
|
584
|
+
historyBytesPerRow: null,
|
|
585
|
+
revisionSinceHistoryRequest: false,
|
|
473
586
|
inFlight: null,
|
|
587
|
+
pipelining: offer.protocolVersions.includes(
|
|
588
|
+
TERMINAL_DELIVERY_PIPELINING_CAPABILITY_VERSION,
|
|
589
|
+
),
|
|
590
|
+
pipelined: [],
|
|
591
|
+
droppedTransactions: new Set(),
|
|
474
592
|
latestRevision: pane.latest?.update.revision ?? null,
|
|
475
593
|
encoding: null,
|
|
476
594
|
scheduled: false,
|
|
@@ -484,6 +602,9 @@ export class SessionRuntimeTerminalDeliveryHub {
|
|
|
484
602
|
lastAck: null,
|
|
485
603
|
sourceClosedFlight: null,
|
|
486
604
|
backgroundTimer: null,
|
|
605
|
+
budgetHeld: false,
|
|
606
|
+
budgetHeldSince: null,
|
|
607
|
+
budgetTimer: null,
|
|
487
608
|
};
|
|
488
609
|
this.#clients.set(key, client);
|
|
489
610
|
client.lifecycleOpenRecorded = this.#recordDeliveryLifecycle(client, pane, "open");
|
|
@@ -496,6 +617,12 @@ export class SessionRuntimeTerminalDeliveryHub {
|
|
|
496
617
|
projectionReceipt: (receipt) => this.#projectionReceipt(client, receipt),
|
|
497
618
|
materialize: (request) => this.#materialize(client, request),
|
|
498
619
|
nack: (nack) => this.#nack(client, nack),
|
|
620
|
+
historyRequest: (request) => this.#historyRequest(client, request),
|
|
621
|
+
prioritize: () => {
|
|
622
|
+
if (client.closed) return;
|
|
623
|
+
this.#priority.set(client.clientId, client.paneId);
|
|
624
|
+
this.#releaseBudget(client.clientId);
|
|
625
|
+
},
|
|
499
626
|
setVisibility: (visibilityInput) => {
|
|
500
627
|
if (client.closed) return;
|
|
501
628
|
client.visibility = TerminalDeliveryVisibilitySchemaZ.parse(visibilityInput);
|
|
@@ -659,7 +786,7 @@ export class SessionRuntimeTerminalDeliveryHub {
|
|
|
659
786
|
),
|
|
660
787
|
maxQueueDepth: this.#maxQueueDepth,
|
|
661
788
|
inFlightBytes: [...this.#clients.values()].reduce(
|
|
662
|
-
(sum, client) => sum + (client
|
|
789
|
+
(sum, client) => sum + this.#unackedBytes(client),
|
|
663
790
|
0,
|
|
664
791
|
),
|
|
665
792
|
});
|
|
@@ -703,6 +830,8 @@ export class SessionRuntimeTerminalDeliveryHub {
|
|
|
703
830
|
this.#cancelEncoding(client);
|
|
704
831
|
client.outgoing.length = 0;
|
|
705
832
|
client.inFlight = null;
|
|
833
|
+
client.pipelined.length = 0;
|
|
834
|
+
client.budgetHeld = false;
|
|
706
835
|
logDeliveryFault(client, "source-closed", "source-restarted");
|
|
707
836
|
this.#enqueue(client, {
|
|
708
837
|
type: "terminal.delivery.fault",
|
|
@@ -1069,14 +1198,156 @@ export class SessionRuntimeTerminalDeliveryHub {
|
|
|
1069
1198
|
}
|
|
1070
1199
|
}
|
|
1071
1200
|
|
|
1201
|
+
#overBudget(client: ClientState): boolean {
|
|
1202
|
+
if (this.#priority.get(client.clientId) === client.paneId) return false;
|
|
1203
|
+
if (
|
|
1204
|
+
client.budgetHeldSince !== null &&
|
|
1205
|
+
this.#scheduler.nowMs() - client.budgetHeldSince >= this.#holdMs
|
|
1206
|
+
)
|
|
1207
|
+
return false;
|
|
1208
|
+
const now = this.#scheduler.nowMs();
|
|
1209
|
+
let bytes = 0;
|
|
1210
|
+
let oldest = now;
|
|
1211
|
+
for (const other of this.#clients.values())
|
|
1212
|
+
if (other !== client && other.clientId === client.clientId && other.inFlight) {
|
|
1213
|
+
bytes += this.#unackedBytes(other);
|
|
1214
|
+
oldest = Math.min(oldest, other.inFlight.sentAt);
|
|
1215
|
+
}
|
|
1216
|
+
return bytes >= TERMINAL_DELIVERY_CONNECTION_UNACKED_BYTES && now - oldest >= this.#congestedMs;
|
|
1217
|
+
}
|
|
1218
|
+
|
|
1219
|
+
/** A pane's delivery settled or left: held panes of its connection try again, typed-into pane first. */
|
|
1220
|
+
#releaseBudget(clientId: string): void {
|
|
1221
|
+
const held = [...this.#clients.values()].filter(
|
|
1222
|
+
(other) => other.clientId === clientId && other.budgetHeld,
|
|
1223
|
+
);
|
|
1224
|
+
const priority = this.#priority.get(clientId);
|
|
1225
|
+
held.sort((a, b) => Number(b.paneId === priority) - Number(a.paneId === priority));
|
|
1226
|
+
for (const other of held) {
|
|
1227
|
+
other.budgetHeld = false;
|
|
1228
|
+
this.#schedule(other);
|
|
1229
|
+
}
|
|
1230
|
+
}
|
|
1231
|
+
|
|
1232
|
+
/** Bytes the client has not acknowledged: every flight, pipelined ones included. */
|
|
1233
|
+
#unackedBytes(client: ClientState): number {
|
|
1234
|
+
let bytes = client.inFlight?.envelope.representationBytes ?? 0;
|
|
1235
|
+
for (const flight of client.pipelined) bytes += flight.envelope.representationBytes;
|
|
1236
|
+
return bytes;
|
|
1237
|
+
}
|
|
1238
|
+
|
|
1239
|
+
/** The revision the client holds once every flight applies (its ACKed baseline if none). */
|
|
1240
|
+
#sentRevision(client: ClientState): number {
|
|
1241
|
+
const newest = client.pipelined.at(-1) ?? client.inFlight;
|
|
1242
|
+
return newest?.envelope.type === "terminal.delivery" && newest.envelope.frame !== "history"
|
|
1243
|
+
? newest.envelope.canonicalRevision
|
|
1244
|
+
: client.baselineRevision;
|
|
1245
|
+
}
|
|
1246
|
+
|
|
1247
|
+
/**
|
|
1248
|
+
* Whether another delivery may go out behind the unacknowledged ones. Only
|
|
1249
|
+
* for a client that offers 124, only a visible pane, only behind canonical patches whose chunks have all been
|
|
1250
|
+
* handed to the socket (a client takes one envelope's chunks at a time), and
|
|
1251
|
+
* within TERMINAL_DELIVERY_MAX_PIPELINED and the connection's unacknowledged
|
|
1252
|
+
* byte budget. Seeds, projections, history frames and history requests keep
|
|
1253
|
+
* one delivery in flight.
|
|
1254
|
+
*/
|
|
1255
|
+
#canPipeline(client: ClientState): boolean {
|
|
1256
|
+
if (
|
|
1257
|
+
!client.pipelining ||
|
|
1258
|
+
!client.inFlight ||
|
|
1259
|
+
client.pipelined.length + 1 >= TERMINAL_DELIVERY_MAX_PIPELINED ||
|
|
1260
|
+
client.visibility !== "visible" ||
|
|
1261
|
+
client.reseedRequired ||
|
|
1262
|
+
client.materializeRequested ||
|
|
1263
|
+
client.pendingHistoryRows !== null ||
|
|
1264
|
+
client.encoding ||
|
|
1265
|
+
(client.negotiated.encoding !== "semantic-v1" &&
|
|
1266
|
+
client.negotiated.encoding !== "semantic-compact-v1" &&
|
|
1267
|
+
client.negotiated.encoding !== "semantic-binary-v1")
|
|
1268
|
+
)
|
|
1269
|
+
return false;
|
|
1270
|
+
for (const flight of [client.inFlight, ...client.pipelined])
|
|
1271
|
+
if (
|
|
1272
|
+
flight.envelope.type !== "terminal.delivery" ||
|
|
1273
|
+
flight.projection ||
|
|
1274
|
+
flight.envelope.frame !== "patch" ||
|
|
1275
|
+
!flight.envelope.canonicalEquivalent ||
|
|
1276
|
+
flight.nextChunk < flight.envelope.chunkCount
|
|
1277
|
+
)
|
|
1278
|
+
return false;
|
|
1279
|
+
return this.#unackedBytes(client) < TERMINAL_DELIVERY_CONNECTION_UNACKED_BYTES;
|
|
1280
|
+
}
|
|
1281
|
+
|
|
1282
|
+
/**
|
|
1283
|
+
* The client as it will be once every flight applies: what its ACKs would
|
|
1284
|
+
* set (see #ack). Representations for a pipelined patch are based on it.
|
|
1285
|
+
*/
|
|
1286
|
+
#pipelineBase(client: ClientState): ClientState {
|
|
1287
|
+
const newest = client.pipelined.at(-1) ?? client.inFlight!;
|
|
1288
|
+
const rows = newest.historyRows;
|
|
1289
|
+
return {
|
|
1290
|
+
...client,
|
|
1291
|
+
baselineRevision: newest.envelope.canonicalRevision,
|
|
1292
|
+
baselineHash: newest.envelope.canonicalStateHash,
|
|
1293
|
+
projectionBaseline: null,
|
|
1294
|
+
lastProjectionReceipt: null,
|
|
1295
|
+
reseedRequired: false,
|
|
1296
|
+
...(rows
|
|
1297
|
+
? {
|
|
1298
|
+
historyUnloadedRows: rows.unloaded,
|
|
1299
|
+
historyUnloadedDigest: rows.digest,
|
|
1300
|
+
historyLoadedRows: rows.loaded,
|
|
1301
|
+
}
|
|
1302
|
+
: {}),
|
|
1303
|
+
};
|
|
1304
|
+
}
|
|
1305
|
+
|
|
1306
|
+
/** A patch on `base`'s projected state, or null when only a seed (or an encoding job) would do. */
|
|
1307
|
+
#pipelinedRepresentation(
|
|
1308
|
+
base: ClientState,
|
|
1309
|
+
pane: PaneState,
|
|
1310
|
+
target: RevisionRecord,
|
|
1311
|
+
): CachedRepresentation | null {
|
|
1312
|
+
if (
|
|
1313
|
+
target.update.type !== "terminal.patch" ||
|
|
1314
|
+
target.update.revision <= base.baselineRevision ||
|
|
1315
|
+
pane.revisions.get(base.baselineRevision)?.update.incarnation !== target.update.incarnation
|
|
1316
|
+
)
|
|
1317
|
+
return null;
|
|
1318
|
+
const reseeds = this.#reseeds;
|
|
1319
|
+
try {
|
|
1320
|
+
const representation = this.#representation(base, pane, target);
|
|
1321
|
+
if (
|
|
1322
|
+
representation.frame === "patch" &&
|
|
1323
|
+
representation.canonicalEquivalent &&
|
|
1324
|
+
!representation.projection
|
|
1325
|
+
)
|
|
1326
|
+
return representation;
|
|
1327
|
+
} catch (error) {
|
|
1328
|
+
if (!(error instanceof DeferredSeedEncoding || error instanceof DeferredProjectionEncoding))
|
|
1329
|
+
throw error;
|
|
1330
|
+
}
|
|
1331
|
+
// A seed waits for the ACKs and is then chosen again; it is counted then.
|
|
1332
|
+
this.#reseeds = reseeds;
|
|
1333
|
+
return null;
|
|
1334
|
+
}
|
|
1335
|
+
|
|
1336
|
+
/** Flights behind a NACKed or faulted one: the client gets a reseed; their feedback is inert. */
|
|
1337
|
+
#dropPipelined(client: ClientState): void {
|
|
1338
|
+
for (const flight of client.pipelined)
|
|
1339
|
+
client.droppedTransactions.add(flight.envelope.transactionId);
|
|
1340
|
+
client.pipelined.length = 0;
|
|
1341
|
+
}
|
|
1342
|
+
|
|
1072
1343
|
#schedule(client: ClientState): void {
|
|
1073
1344
|
if (
|
|
1074
1345
|
client.closed ||
|
|
1075
1346
|
client.captureUnavailable ||
|
|
1076
|
-
client.inFlight ||
|
|
1347
|
+
(client.inFlight && !this.#canPipeline(client)) ||
|
|
1077
1348
|
client.encoding ||
|
|
1078
|
-
client.latestRevision === null ||
|
|
1079
|
-
|
|
1349
|
+
((client.latestRevision === null || client.latestRevision === this.#sentRevision(client)) &&
|
|
1350
|
+
client.pendingHistoryRows === null) ||
|
|
1080
1351
|
client.visibility === "hidden" ||
|
|
1081
1352
|
client.visibility === "frozen"
|
|
1082
1353
|
)
|
|
@@ -1085,7 +1356,11 @@ export class SessionRuntimeTerminalDeliveryHub {
|
|
|
1085
1356
|
if (client.backgroundTimer) return;
|
|
1086
1357
|
client.backgroundTimer = this.#scheduler.timer(() => {
|
|
1087
1358
|
client.backgroundTimer = null;
|
|
1088
|
-
if (
|
|
1359
|
+
if (
|
|
1360
|
+
!client.closed &&
|
|
1361
|
+
!client.inFlight &&
|
|
1362
|
+
(client.latestRevision !== null || client.pendingHistoryRows !== null)
|
|
1363
|
+
)
|
|
1089
1364
|
this.#deliver(client);
|
|
1090
1365
|
}, BACKGROUND_CADENCE_MS);
|
|
1091
1366
|
return;
|
|
@@ -1094,7 +1369,11 @@ export class SessionRuntimeTerminalDeliveryHub {
|
|
|
1094
1369
|
client.scheduled = true;
|
|
1095
1370
|
this.#scheduler.microtask(() => {
|
|
1096
1371
|
client.scheduled = false;
|
|
1097
|
-
if (
|
|
1372
|
+
if (
|
|
1373
|
+
!client.closed &&
|
|
1374
|
+
client.visibility === "visible" &&
|
|
1375
|
+
(!client.inFlight || this.#canPipeline(client))
|
|
1376
|
+
)
|
|
1098
1377
|
this.#deliver(client);
|
|
1099
1378
|
});
|
|
1100
1379
|
}
|
|
@@ -1108,19 +1387,28 @@ export class SessionRuntimeTerminalDeliveryHub {
|
|
|
1108
1387
|
},
|
|
1109
1388
|
): void {
|
|
1110
1389
|
const pane = this.#panes.get(client.paneId);
|
|
1390
|
+
if (!completed && pane && this.#historyDue(client)) {
|
|
1391
|
+
// History counts against the connection's byte budget like any delivery.
|
|
1392
|
+
if (this.#holdForBudget(client)) return;
|
|
1393
|
+
this.#deliverHistory(client, pane);
|
|
1394
|
+
return;
|
|
1395
|
+
}
|
|
1111
1396
|
const target = completed?.target ?? pane?.latest;
|
|
1397
|
+
// Behind an unacknowledged delivery only a patch on the client's projected state goes out.
|
|
1398
|
+
const pipelining = !completed && client.inFlight !== null;
|
|
1112
1399
|
if (
|
|
1113
1400
|
!pane ||
|
|
1114
1401
|
!target ||
|
|
1115
1402
|
client.closed ||
|
|
1116
1403
|
client.captureUnavailable ||
|
|
1117
|
-
client.inFlight ||
|
|
1404
|
+
(client.inFlight && (completed || !this.#canPipeline(client))) ||
|
|
1118
1405
|
client.encoding ||
|
|
1119
1406
|
client.visibility === "hidden" ||
|
|
1120
1407
|
client.visibility === "frozen" ||
|
|
1121
1408
|
(!completed && target.update.revision !== client.latestRevision)
|
|
1122
1409
|
)
|
|
1123
1410
|
return;
|
|
1411
|
+
if (!completed && this.#holdForBudget(client)) return;
|
|
1124
1412
|
const traceStarted =
|
|
1125
1413
|
completed?.traceStarted ??
|
|
1126
1414
|
(this.#observability.enabled ? this.#observability.nowMicros() : 0);
|
|
@@ -1130,7 +1418,15 @@ export class SessionRuntimeTerminalDeliveryHub {
|
|
|
1130
1418
|
let deliveryEnvelope: DeliveryEnvelope | null = null;
|
|
1131
1419
|
let deliveryOrdinal: number | null = null;
|
|
1132
1420
|
try {
|
|
1133
|
-
const
|
|
1421
|
+
const base = pipelining ? this.#pipelineBase(client) : client;
|
|
1422
|
+
const representation = pipelining
|
|
1423
|
+
? this.#pipelinedRepresentation(base, pane, target)
|
|
1424
|
+
: this.#representation(client, pane, target, completed?.outcome);
|
|
1425
|
+
if (!representation) {
|
|
1426
|
+
// Not a patch: it waits for the outstanding ACKs, then goes out as usual.
|
|
1427
|
+
deferred = true;
|
|
1428
|
+
return;
|
|
1429
|
+
}
|
|
1134
1430
|
encodedRepresentation = representation;
|
|
1135
1431
|
const transactionId = this.#scheduler.createId();
|
|
1136
1432
|
const chunkCount = Math.max(1, Math.ceil(representation.bytes.byteLength / (256 * 1024)));
|
|
@@ -1157,7 +1453,7 @@ export class SessionRuntimeTerminalDeliveryHub {
|
|
|
1157
1453
|
? representation.projectionBaseRevision
|
|
1158
1454
|
: representation.frame === "seed"
|
|
1159
1455
|
? null
|
|
1160
|
-
: Math.max(0,
|
|
1456
|
+
: Math.max(0, base.baselineRevision),
|
|
1161
1457
|
canonicalRevision: target.update.revision,
|
|
1162
1458
|
canonicalStateHash: target.update.stateHash,
|
|
1163
1459
|
representationHash:
|
|
@@ -1167,19 +1463,26 @@ export class SessionRuntimeTerminalDeliveryHub {
|
|
|
1167
1463
|
chunkCount,
|
|
1168
1464
|
canonicalEquivalent: representation.canonicalEquivalent,
|
|
1169
1465
|
history: representation.history,
|
|
1466
|
+
...(representation.historyUnloaded
|
|
1467
|
+
? { historyUnloaded: representation.historyUnloaded }
|
|
1468
|
+
: {}),
|
|
1170
1469
|
richPlacements: client.negotiated.richPlacements,
|
|
1171
1470
|
});
|
|
1172
1471
|
deliveryEnvelope = envelope;
|
|
1173
1472
|
deliveryOrdinal = ++this.#deliveryOrdinal;
|
|
1174
1473
|
registerTerminalDeliveryObservationOrdinal(envelope, deliveryOrdinal);
|
|
1175
|
-
client.
|
|
1474
|
+
if (client.pendingHistoryRows !== null) client.revisionSinceHistoryRequest = true;
|
|
1475
|
+
const flight: DeliveryFlight = {
|
|
1176
1476
|
envelope,
|
|
1177
1477
|
projection: representation.projection ?? null,
|
|
1478
|
+
historyRows: representation.historyRows ?? null,
|
|
1178
1479
|
deliveryOrdinal,
|
|
1179
1480
|
bytes: representation.bytes,
|
|
1180
1481
|
nextChunk: 0,
|
|
1181
1482
|
sentAt: this.#scheduler.nowMs(),
|
|
1182
1483
|
};
|
|
1484
|
+
if (pipelining) client.pipelined.push(flight);
|
|
1485
|
+
else client.inFlight = flight;
|
|
1183
1486
|
this.#enqueue(client, envelope);
|
|
1184
1487
|
this.#recordDeliveryStatus(client, pane);
|
|
1185
1488
|
if (target.trace?.traceId === pane.pendingDeliveryTrace?.traceId)
|
|
@@ -1194,6 +1497,7 @@ export class SessionRuntimeTerminalDeliveryHub {
|
|
|
1194
1497
|
error.payload,
|
|
1195
1498
|
traceStarted,
|
|
1196
1499
|
error instanceof DeferredProjectionEncoding,
|
|
1500
|
+
error instanceof DeferredSeedEncoding ? error.variant : "complete",
|
|
1197
1501
|
);
|
|
1198
1502
|
return;
|
|
1199
1503
|
}
|
|
@@ -1226,7 +1530,7 @@ export class SessionRuntimeTerminalDeliveryHub {
|
|
|
1226
1530
|
inFlightBytes: metrics.inFlightBytes,
|
|
1227
1531
|
...(encodedRepresentation
|
|
1228
1532
|
? {
|
|
1229
|
-
representation: encodedRepresentation.frame,
|
|
1533
|
+
representation: encodedRepresentation.frame as "seed" | "patch" | "tombstone",
|
|
1230
1534
|
representationBytes: encodedRepresentation.bytes.byteLength,
|
|
1231
1535
|
}
|
|
1232
1536
|
: {}),
|
|
@@ -1275,6 +1579,7 @@ export class SessionRuntimeTerminalDeliveryHub {
|
|
|
1275
1579
|
payload: TerminalSemanticDeliveryPayload | TerminalProjectionPayload,
|
|
1276
1580
|
traceStarted: number,
|
|
1277
1581
|
projection = false,
|
|
1582
|
+
variant = "complete",
|
|
1278
1583
|
): void {
|
|
1279
1584
|
const key = cacheKey([
|
|
1280
1585
|
target.update.workspaceName,
|
|
@@ -1287,7 +1592,7 @@ export class SessionRuntimeTerminalDeliveryHub {
|
|
|
1287
1592
|
client.negotiated.richPlacements ? "rich" : "plain",
|
|
1288
1593
|
projection
|
|
1289
1594
|
? `projection:${client.projectionBaseline?.revision ?? -1}:${client.projectionBaseline?.hash ?? "none"}`
|
|
1290
|
-
:
|
|
1595
|
+
: variant,
|
|
1291
1596
|
]);
|
|
1292
1597
|
let job = this.#pendingEncodings.get(key);
|
|
1293
1598
|
let created = false;
|
|
@@ -1317,6 +1622,7 @@ export class SessionRuntimeTerminalDeliveryHub {
|
|
|
1317
1622
|
baselineRevision: client.baselineRevision,
|
|
1318
1623
|
baselineHash: client.baselineHash,
|
|
1319
1624
|
reseedRequired: client.reseedRequired,
|
|
1625
|
+
historyUnloadedRows: client.historyUnloadedRows,
|
|
1320
1626
|
traceStarted,
|
|
1321
1627
|
};
|
|
1322
1628
|
owned.clients.add(client);
|
|
@@ -1373,6 +1679,7 @@ export class SessionRuntimeTerminalDeliveryHub {
|
|
|
1373
1679
|
client.baselineRevision !== encoding.baselineRevision ||
|
|
1374
1680
|
client.baselineHash !== encoding.baselineHash ||
|
|
1375
1681
|
client.reseedRequired !== encoding.reseedRequired ||
|
|
1682
|
+
client.historyUnloadedRows !== encoding.historyUnloadedRows ||
|
|
1376
1683
|
client.inFlight ||
|
|
1377
1684
|
client.visibility === "hidden" ||
|
|
1378
1685
|
client.visibility === "frozen" ||
|
|
@@ -1434,6 +1741,8 @@ export class SessionRuntimeTerminalDeliveryHub {
|
|
|
1434
1741
|
projectionBaseRevision: payload.frame === "seed" ? null : payload.baseRevision,
|
|
1435
1742
|
};
|
|
1436
1743
|
}
|
|
1744
|
+
if (client.negotiated.partialHistory === "tail-v1" && target.state.snapshot)
|
|
1745
|
+
return this.#partialRepresentation(client, pane, target, seedOutcome);
|
|
1437
1746
|
const key = cacheKey([
|
|
1438
1747
|
target.update.workspaceName,
|
|
1439
1748
|
target.update.semanticPaneId,
|
|
@@ -1789,6 +2098,7 @@ export class SessionRuntimeTerminalDeliveryHub {
|
|
|
1789
2098
|
#ack(client: ClientState, input: TerminalDeliveryAck): void {
|
|
1790
2099
|
const ack = TerminalDeliveryAckSchemaZ.parse(input);
|
|
1791
2100
|
const flight = client.inFlight;
|
|
2101
|
+
if (client.droppedTransactions.has(ack.transactionId)) return;
|
|
1792
2102
|
if (!flight) {
|
|
1793
2103
|
if (client.lastAck && JSON.stringify(client.lastAck) === JSON.stringify(ack)) return;
|
|
1794
2104
|
const closed = client.sourceClosedFlight;
|
|
@@ -1837,7 +2147,15 @@ export class SessionRuntimeTerminalDeliveryHub {
|
|
|
1837
2147
|
client.baselineRevision = ack.canonicalRevision;
|
|
1838
2148
|
client.baselineHash = ack.canonicalStateHash;
|
|
1839
2149
|
client.reseedRequired = false;
|
|
1840
|
-
|
|
2150
|
+
if (flight.historyRows) {
|
|
2151
|
+
client.historyUnloadedRows = flight.historyRows.unloaded;
|
|
2152
|
+
client.historyUnloadedDigest = flight.historyRows.digest;
|
|
2153
|
+
client.historyLoadedRows = flight.historyRows.loaded;
|
|
2154
|
+
this.#pushHistoryTail(client);
|
|
2155
|
+
}
|
|
2156
|
+
client.inFlight = client.pipelined.shift() ?? null;
|
|
2157
|
+
client.droppedTransactions.clear();
|
|
2158
|
+
this.#releaseBudget(client.clientId);
|
|
1841
2159
|
client.lastAck = ack;
|
|
1842
2160
|
if (client.latestRevision === ack.canonicalRevision) client.latestRevision = null;
|
|
1843
2161
|
this.#pruneAckSupersededCache(client.paneId);
|
|
@@ -1891,6 +2209,301 @@ export class SessionRuntimeTerminalDeliveryHub {
|
|
|
1891
2209
|
this.#recordDeliveryStatus(client, this.#panes.get(client.paneId));
|
|
1892
2210
|
}
|
|
1893
2211
|
|
|
2212
|
+
/**
|
|
2213
|
+
* Capability 122: the client's partial view of `target`. Its unloaded rows
|
|
2214
|
+
* follow the baseline's (a trim takes unloaded rows first); a seed carries
|
|
2215
|
+
* the newest TERMINAL_DELIVERY_HISTORY_TAIL_ROWS rows (none on the alternate
|
|
2216
|
+
* screen) or, after a reseed, as many rows as the client held. The full
|
|
2217
|
+
* canonical hash still verifies: the commitment covers the rest.
|
|
2218
|
+
*/
|
|
2219
|
+
#partialRepresentation(
|
|
2220
|
+
client: ClientState,
|
|
2221
|
+
pane: PaneState,
|
|
2222
|
+
target: RevisionRecord,
|
|
2223
|
+
seedOutcome?: SeedEncodingOutcome,
|
|
2224
|
+
): CachedRepresentation {
|
|
2225
|
+
const full = target.state.snapshot!;
|
|
2226
|
+
const baselineRecord =
|
|
2227
|
+
client.reseedRequired || client.baselineRevision < 0
|
|
2228
|
+
? undefined
|
|
2229
|
+
: pane.revisions.get(client.baselineRevision);
|
|
2230
|
+
const baselineFull = baselineRecord?.state.snapshot ?? null;
|
|
2231
|
+
const tail = full.modes.alternateScreen ? 0 : TERMINAL_DELIVERY_HISTORY_TAIL_ROWS;
|
|
2232
|
+
const loadedDepth = () =>
|
|
2233
|
+
Math.max(
|
|
2234
|
+
0,
|
|
2235
|
+
full.history.length -
|
|
2236
|
+
Math.max(tail, client.baselineRevision >= 0 ? client.historyLoadedRows : 0),
|
|
2237
|
+
);
|
|
2238
|
+
let unloadedRows: number;
|
|
2239
|
+
let unloaded: TerminalDeliveryHistoryCommitment | null = null;
|
|
2240
|
+
let baseline: TerminalReplicaSnapshot | null = null;
|
|
2241
|
+
if (baselineFull && baselineRecord!.update.incarnation === target.update.incarnation) {
|
|
2242
|
+
baseline =
|
|
2243
|
+
client.historyUnloadedRows === 0
|
|
2244
|
+
? baselineFull
|
|
2245
|
+
: { ...baselineFull, history: baselineFull.history.slice(client.historyUnloadedRows) };
|
|
2246
|
+
// A trim takes unloaded rows first. The rows still unloaded must be the
|
|
2247
|
+
// same row objects, so their commitment updates in O(trim).
|
|
2248
|
+
const trim = historyTrim(baselineFull.history, full.history);
|
|
2249
|
+
const kept = Math.max(0, client.historyUnloadedRows - trim);
|
|
2250
|
+
let same = kept <= full.history.length;
|
|
2251
|
+
for (let index = 0; same && index < kept; index++)
|
|
2252
|
+
if (baselineFull.history[trim + index] !== full.history[index]) same = false;
|
|
2253
|
+
if (same) {
|
|
2254
|
+
unloadedRows = kept;
|
|
2255
|
+
const before = { rows: client.historyUnloadedRows, digest: client.historyUnloadedDigest };
|
|
2256
|
+
unloaded =
|
|
2257
|
+
trim === 0
|
|
2258
|
+
? before
|
|
2259
|
+
: dropTerminalHistoryCommitmentPrefix(
|
|
2260
|
+
before,
|
|
2261
|
+
baselineFull.history.slice(0, Math.min(trim, client.historyUnloadedRows)),
|
|
2262
|
+
);
|
|
2263
|
+
} else {
|
|
2264
|
+
// Not a proved trim (a reflow, a clear): a seed that keeps the depth
|
|
2265
|
+
// the client held. A patch could not grow the unloaded rows.
|
|
2266
|
+
unloadedRows = loadedDepth();
|
|
2267
|
+
baseline = null;
|
|
2268
|
+
}
|
|
2269
|
+
} else {
|
|
2270
|
+
unloadedRows = loadedDepth();
|
|
2271
|
+
}
|
|
2272
|
+
const partial =
|
|
2273
|
+
unloadedRows === 0 ? full : { ...full, history: full.history.slice(unloadedRows) };
|
|
2274
|
+
// A client that holds everything and stays complete keeps the canonical
|
|
2275
|
+
// patch; otherwise diff the partial views (never the full-history patch).
|
|
2276
|
+
const view =
|
|
2277
|
+
unloadedRows === 0 && client.historyUnloadedRows === 0
|
|
2278
|
+
? target
|
|
2279
|
+
: ({
|
|
2280
|
+
...target,
|
|
2281
|
+
state: { ...target.state, snapshot: partial },
|
|
2282
|
+
update: { ...target.update, type: "terminal.seed" },
|
|
2283
|
+
} as RevisionRecord);
|
|
2284
|
+
const payload = baseline
|
|
2285
|
+
? semanticPayload(client.baselineRevision, baseline, view, baselineRecord!.update.incarnation)
|
|
2286
|
+
: semanticSeed(view);
|
|
2287
|
+
const encode = (value: TerminalSemanticDeliveryPayload): Uint8Array => {
|
|
2288
|
+
if (
|
|
2289
|
+
value.frame === "seed" &&
|
|
2290
|
+
(client.negotiated.encoding === "semantic-binary-v1" ||
|
|
2291
|
+
terminalSemanticUpdateNeedsCooperativeEncoding(value))
|
|
2292
|
+
) {
|
|
2293
|
+
if (!seedOutcome) throw new DeferredSeedEncoding(value, `partial:${unloadedRows}`);
|
|
2294
|
+
if ("error" in seedOutcome) throw seedOutcome.error;
|
|
2295
|
+
return seedOutcome.bytes;
|
|
2296
|
+
}
|
|
2297
|
+
return client.negotiated.encoding === "semantic-binary-v1"
|
|
2298
|
+
? encodeBinarySemanticTerminalUpdate(value)
|
|
2299
|
+
: encodeCompactSemanticTerminalUpdate(value);
|
|
2300
|
+
};
|
|
2301
|
+
let frame = payload.frame;
|
|
2302
|
+
let bytes: Uint8Array;
|
|
2303
|
+
try {
|
|
2304
|
+
bytes = encode(payload);
|
|
2305
|
+
} catch (error) {
|
|
2306
|
+
if (!(error instanceof TerminalDeliveryStateTooLargeError) || payload.frame !== "patch")
|
|
2307
|
+
throw error;
|
|
2308
|
+
frame = "seed";
|
|
2309
|
+
bytes = encode(semanticSeed(view));
|
|
2310
|
+
}
|
|
2311
|
+
if (frame === "seed") this.#reseeds += client.reseedRequired ? 1 : 0;
|
|
2312
|
+
const commitment = unloaded ?? terminalHistoryPrefixCommitment(full.history, unloadedRows);
|
|
2313
|
+
return {
|
|
2314
|
+
bytes,
|
|
2315
|
+
encoding: client.negotiated.encoding as "semantic-compact-v1" | "semantic-binary-v1",
|
|
2316
|
+
frame,
|
|
2317
|
+
canonicalEquivalent: true,
|
|
2318
|
+
history: "partial",
|
|
2319
|
+
historyUnloaded: commitment,
|
|
2320
|
+
historyRows: {
|
|
2321
|
+
unloaded: unloadedRows,
|
|
2322
|
+
digest: commitment.digest,
|
|
2323
|
+
loaded: full.history.length - unloadedRows,
|
|
2324
|
+
},
|
|
2325
|
+
};
|
|
2326
|
+
}
|
|
2327
|
+
|
|
2328
|
+
/** Hold the client while its connection's other panes are congested; true if held. */
|
|
2329
|
+
#holdForBudget(client: ClientState): boolean {
|
|
2330
|
+
if (this.#overBudget(client)) {
|
|
2331
|
+
client.budgetHeld = true;
|
|
2332
|
+
client.budgetHeldSince ??= this.#scheduler.nowMs();
|
|
2333
|
+
client.budgetTimer ??= this.#scheduler.timer(() => {
|
|
2334
|
+
client.budgetTimer = null;
|
|
2335
|
+
if (client.closed || !client.budgetHeld) return;
|
|
2336
|
+
client.budgetHeld = false;
|
|
2337
|
+
this.#schedule(client);
|
|
2338
|
+
}, this.#holdMs);
|
|
2339
|
+
return true;
|
|
2340
|
+
}
|
|
2341
|
+
client.budgetHeldSince = null;
|
|
2342
|
+
client.budgetTimer?.cancel();
|
|
2343
|
+
client.budgetTimer = null;
|
|
2344
|
+
return false;
|
|
2345
|
+
}
|
|
2346
|
+
|
|
2347
|
+
/** Capability 122: a history frame goes next if asked for and no newer output waits (or one just went). */
|
|
2348
|
+
#historyDue(client: ClientState): boolean {
|
|
2349
|
+
return (
|
|
2350
|
+
client.pendingHistoryRows !== null &&
|
|
2351
|
+
!client.inFlight &&
|
|
2352
|
+
!client.encoding &&
|
|
2353
|
+
client.baselineRevision >= 0 &&
|
|
2354
|
+
!client.reseedRequired &&
|
|
2355
|
+
(client.latestRevision === null ||
|
|
2356
|
+
client.latestRevision === client.baselineRevision ||
|
|
2357
|
+
client.revisionSinceHistoryRequest) &&
|
|
2358
|
+
// Live output of another pane held by the connection's budget goes first.
|
|
2359
|
+
![...this.#clients.values()].some(
|
|
2360
|
+
(other) => other !== client && other.clientId === client.clientId && other.budgetHeld,
|
|
2361
|
+
)
|
|
2362
|
+
);
|
|
2363
|
+
}
|
|
2364
|
+
|
|
2365
|
+
#deliverHistory(client: ClientState, pane: PaneState): void {
|
|
2366
|
+
const asked = client.pendingHistoryRows!;
|
|
2367
|
+
client.pendingHistoryRows = null;
|
|
2368
|
+
client.revisionSinceHistoryRequest = false;
|
|
2369
|
+
const record = pane.revisions.get(client.baselineRevision);
|
|
2370
|
+
const full = record?.state.snapshot;
|
|
2371
|
+
if (!record || !full || client.historyUnloadedRows === 0) {
|
|
2372
|
+
this.#schedule(client);
|
|
2373
|
+
return;
|
|
2374
|
+
}
|
|
2375
|
+
const encoding = client.negotiated.encoding as "semantic-compact-v1" | "semantic-binary-v1";
|
|
2376
|
+
const unloadedRows = client.historyUnloadedRows;
|
|
2377
|
+
try {
|
|
2378
|
+
// Bound the frame by its encoded bytes. The event loop pays for every
|
|
2379
|
+
// row encoded, so encode about what is sent: size the frame from the
|
|
2380
|
+
// last frame's bytes per row (or a small sample of the rows next in
|
|
2381
|
+
// line), grow once if it came out well under, shrink until it fits.
|
|
2382
|
+
const limit = Math.max(1, Math.min(asked, unloadedRows));
|
|
2383
|
+
const encode = (count: number) => {
|
|
2384
|
+
const rows = full.history.slice(unloadedRows - count, unloadedRows);
|
|
2385
|
+
const bytes = encodeSemanticTerminalHistory(
|
|
2386
|
+
{ frame: "history", revision: record.update.revision, rows },
|
|
2387
|
+
encoding,
|
|
2388
|
+
);
|
|
2389
|
+
return { count, rows, bytes };
|
|
2390
|
+
};
|
|
2391
|
+
const fit = (bytesPerRow: number) =>
|
|
2392
|
+
Math.max(
|
|
2393
|
+
1,
|
|
2394
|
+
Math.min(limit, Math.floor((HISTORY_FRAME_MAX_BYTES * 0.95) / Math.max(1, bytesPerRow))),
|
|
2395
|
+
);
|
|
2396
|
+
let frame = client.historyBytesPerRow === null ? encode(Math.min(limit, 32)) : null;
|
|
2397
|
+
const estimate = client.historyBytesPerRow ?? frame!.bytes.byteLength / frame!.count;
|
|
2398
|
+
if (!frame || fit(estimate) !== frame.count) frame = encode(fit(estimate));
|
|
2399
|
+
if (frame.count < limit && frame.bytes.byteLength < HISTORY_FRAME_MAX_BYTES * 0.6) {
|
|
2400
|
+
const grown = fit(frame.bytes.byteLength / frame.count);
|
|
2401
|
+
if (grown > frame.count) frame = encode(grown);
|
|
2402
|
+
}
|
|
2403
|
+
let { count, rows, bytes } = frame;
|
|
2404
|
+
while (bytes.byteLength > HISTORY_FRAME_MAX_BYTES && count > 1) {
|
|
2405
|
+
count = Math.max(
|
|
2406
|
+
1,
|
|
2407
|
+
Math.min(
|
|
2408
|
+
count - 1,
|
|
2409
|
+
Math.floor((count * HISTORY_FRAME_MAX_BYTES * 0.9) / bytes.byteLength),
|
|
2410
|
+
),
|
|
2411
|
+
);
|
|
2412
|
+
({ rows, bytes } = encode(count));
|
|
2413
|
+
}
|
|
2414
|
+
client.historyBytesPerRow = bytes.byteLength / count;
|
|
2415
|
+
const chunk = {
|
|
2416
|
+
rows,
|
|
2417
|
+
unloaded: popTerminalHistoryCommitmentSuffix(
|
|
2418
|
+
{ rows: unloadedRows, digest: client.historyUnloadedDigest },
|
|
2419
|
+
rows,
|
|
2420
|
+
),
|
|
2421
|
+
};
|
|
2422
|
+
const envelope = TerminalDeliveryEnvelopeSchemaZ.parse({
|
|
2423
|
+
type: "terminal.delivery",
|
|
2424
|
+
workspaceName: client.deliveryWorkspaceName,
|
|
2425
|
+
semanticPaneId: record.update.semanticPaneId,
|
|
2426
|
+
generation: record.update.generation,
|
|
2427
|
+
incarnation: record.update.incarnation,
|
|
2428
|
+
deliveryNonce: client.negotiated.deliveryNonce,
|
|
2429
|
+
transactionId: this.#scheduler.createId(),
|
|
2430
|
+
protocolVersion: 1,
|
|
2431
|
+
encoding,
|
|
2432
|
+
frame: "history",
|
|
2433
|
+
baseRevision: record.update.revision,
|
|
2434
|
+
canonicalRevision: record.update.revision,
|
|
2435
|
+
canonicalStateHash: record.update.stateHash,
|
|
2436
|
+
representationHash: hashTerminalDeliveryRepresentation(bytes),
|
|
2437
|
+
representationBytes: bytes.byteLength,
|
|
2438
|
+
chunkCount: Math.max(1, Math.ceil(bytes.byteLength / (256 * 1024))),
|
|
2439
|
+
canonicalEquivalent: true,
|
|
2440
|
+
history: "partial",
|
|
2441
|
+
historyUnloaded: chunk.unloaded,
|
|
2442
|
+
richPlacements: client.negotiated.richPlacements,
|
|
2443
|
+
});
|
|
2444
|
+
const deliveryOrdinal = ++this.#deliveryOrdinal;
|
|
2445
|
+
registerTerminalDeliveryObservationOrdinal(envelope, deliveryOrdinal);
|
|
2446
|
+
client.inFlight = {
|
|
2447
|
+
envelope,
|
|
2448
|
+
projection: null,
|
|
2449
|
+
historyRows: {
|
|
2450
|
+
unloaded: chunk.unloaded.rows,
|
|
2451
|
+
digest: chunk.unloaded.digest,
|
|
2452
|
+
loaded: full.history.length - chunk.unloaded.rows,
|
|
2453
|
+
},
|
|
2454
|
+
deliveryOrdinal,
|
|
2455
|
+
bytes,
|
|
2456
|
+
nextChunk: 0,
|
|
2457
|
+
sentAt: this.#scheduler.nowMs(),
|
|
2458
|
+
};
|
|
2459
|
+
this.#enqueue(client, envelope);
|
|
2460
|
+
this.#recordDeliveryStatus(client, pane);
|
|
2461
|
+
} catch (error) {
|
|
2462
|
+
this.#fault(
|
|
2463
|
+
client,
|
|
2464
|
+
error instanceof TerminalDeliveryStateTooLargeError
|
|
2465
|
+
? "state-too-large"
|
|
2466
|
+
: "protocol-violation",
|
|
2467
|
+
error instanceof Error ? error.message : String(error),
|
|
2468
|
+
);
|
|
2469
|
+
}
|
|
2470
|
+
}
|
|
2471
|
+
|
|
2472
|
+
/** Back on the normal screen with less than the tail loaded: send the tail unasked. */
|
|
2473
|
+
#pushHistoryTail(client: ClientState): void {
|
|
2474
|
+
if (client.pendingHistoryRows !== null || client.historyUnloadedRows === 0) return;
|
|
2475
|
+
const full = this.#panes.get(client.paneId)?.revisions.get(client.baselineRevision)
|
|
2476
|
+
?.state.snapshot;
|
|
2477
|
+
if (!full || full.modes.alternateScreen) return;
|
|
2478
|
+
if (client.historyLoadedRows >= TERMINAL_DELIVERY_HISTORY_TAIL_ROWS) return;
|
|
2479
|
+
client.pendingHistoryRows = Math.min(
|
|
2480
|
+
client.historyUnloadedRows,
|
|
2481
|
+
TERMINAL_DELIVERY_HISTORY_TAIL_ROWS - client.historyLoadedRows,
|
|
2482
|
+
);
|
|
2483
|
+
client.revisionSinceHistoryRequest = false;
|
|
2484
|
+
}
|
|
2485
|
+
|
|
2486
|
+
#historyRequest(client: ClientState, input: TerminalDeliveryHistoryRequest): void {
|
|
2487
|
+
if (client.closed) return;
|
|
2488
|
+
const parsed = TerminalDeliveryHistoryRequestSchemaZ.safeParse(input);
|
|
2489
|
+
if (
|
|
2490
|
+
!parsed.success ||
|
|
2491
|
+
client.negotiated.partialHistory !== "tail-v1" ||
|
|
2492
|
+
parsed.data.workspaceName !== client.deliveryWorkspaceName ||
|
|
2493
|
+
parsed.data.semanticPaneId !== client.paneId ||
|
|
2494
|
+
parsed.data.generation !== client.negotiated.generation ||
|
|
2495
|
+
parsed.data.deliveryNonce !== client.negotiated.deliveryNonce
|
|
2496
|
+
) {
|
|
2497
|
+
this.#fault(client, "protocol-violation", "History request does not identify this delivery");
|
|
2498
|
+
return;
|
|
2499
|
+
}
|
|
2500
|
+
// The daemon knows what the client holds at its baseline; the newest
|
|
2501
|
+
// request replaces an unserved one and is served from that baseline.
|
|
2502
|
+
client.pendingHistoryRows = parsed.data.maxRows;
|
|
2503
|
+
client.revisionSinceHistoryRequest = false;
|
|
2504
|
+
this.#schedule(client);
|
|
2505
|
+
}
|
|
2506
|
+
|
|
1894
2507
|
#projectionReceipt(client: ClientState, input: TerminalProjectionReceipt): void {
|
|
1895
2508
|
const receipt = TerminalProjectionReceiptSchemaZ.parse(input);
|
|
1896
2509
|
if (
|
|
@@ -1927,6 +2540,7 @@ export class SessionRuntimeTerminalDeliveryHub {
|
|
|
1927
2540
|
client.lastProjectionReceipt = receipt;
|
|
1928
2541
|
client.reseedRequired = false;
|
|
1929
2542
|
client.inFlight = null;
|
|
2543
|
+
this.#releaseBudget(client.clientId);
|
|
1930
2544
|
if (client.latestRevision === receipt.canonicalRevision) client.latestRevision = null;
|
|
1931
2545
|
if (client.materializeRequested)
|
|
1932
2546
|
client.latestRevision = this.#panes.get(client.paneId)?.latest?.update.revision ?? null;
|
|
@@ -1988,6 +2602,7 @@ export class SessionRuntimeTerminalDeliveryHub {
|
|
|
1988
2602
|
nack.transactionId === closed.transactionId
|
|
1989
2603
|
)
|
|
1990
2604
|
return;
|
|
2605
|
+
if (nack.transactionId !== null && client.droppedTransactions.has(nack.transactionId)) return;
|
|
1991
2606
|
const envelope = client.inFlight?.envelope;
|
|
1992
2607
|
if (
|
|
1993
2608
|
!envelope ||
|
|
@@ -2004,6 +2619,8 @@ export class SessionRuntimeTerminalDeliveryHub {
|
|
|
2004
2619
|
this.#nacks += 1;
|
|
2005
2620
|
client.reseedRequired = true;
|
|
2006
2621
|
client.inFlight = null;
|
|
2622
|
+
this.#dropPipelined(client);
|
|
2623
|
+
this.#releaseBudget(client.clientId);
|
|
2007
2624
|
this.#pruneCanonicalRevisions(client.paneId);
|
|
2008
2625
|
this.#schedule(client);
|
|
2009
2626
|
this.#recordDeliveryStatus(client, this.#panes.get(client.paneId));
|
|
@@ -2058,6 +2675,8 @@ export class SessionRuntimeTerminalDeliveryHub {
|
|
|
2058
2675
|
client.outgoing.length = 0;
|
|
2059
2676
|
if (reason === "source-closed") client.sourceClosedFlight = client.inFlight?.envelope ?? null;
|
|
2060
2677
|
client.inFlight = null;
|
|
2678
|
+
this.#dropPipelined(client);
|
|
2679
|
+
this.#releaseBudget(client.clientId);
|
|
2061
2680
|
const fault: TerminalDeliveryServerMessage = {
|
|
2062
2681
|
type: "terminal.delivery.fault",
|
|
2063
2682
|
reason,
|
|
@@ -2094,10 +2713,15 @@ export class SessionRuntimeTerminalDeliveryHub {
|
|
|
2094
2713
|
while (!client.closed) {
|
|
2095
2714
|
const queued = client.outgoing.shift();
|
|
2096
2715
|
let next = queued?.();
|
|
2097
|
-
|
|
2716
|
+
// Only the newest flight can still have chunks: a pipelined
|
|
2717
|
+
// delivery is created only once every earlier one is fully out.
|
|
2718
|
+
const flight = client.pipelined.at(-1) ?? client.inFlight;
|
|
2098
2719
|
if (!next && flight && flight.nextChunk < flight.envelope.chunkCount) {
|
|
2099
2720
|
const index = flight.nextChunk;
|
|
2100
2721
|
flight.nextChunk += 1;
|
|
2722
|
+
// A newer revision may be waiting for this flight's last chunk.
|
|
2723
|
+
if (flight.nextChunk === flight.envelope.chunkCount && client.latestRevision !== null)
|
|
2724
|
+
this.#schedule(client);
|
|
2101
2725
|
next = {
|
|
2102
2726
|
type: "terminal.delivery.chunk",
|
|
2103
2727
|
transactionId: flight.envelope.transactionId,
|
|
@@ -2137,8 +2761,13 @@ export class SessionRuntimeTerminalDeliveryHub {
|
|
|
2137
2761
|
if (client.lifecycleOpenRecorded)
|
|
2138
2762
|
this.#recordDeliveryLifecycle(client, this.#panes.get(client.paneId), "close");
|
|
2139
2763
|
client.backgroundTimer?.cancel();
|
|
2764
|
+
client.budgetTimer?.cancel();
|
|
2765
|
+
client.budgetTimer = null;
|
|
2140
2766
|
client.outgoing.length = 0;
|
|
2141
2767
|
this.#clients.delete(client.key);
|
|
2768
|
+
if (![...this.#clients.values()].some((other) => other.clientId === client.clientId))
|
|
2769
|
+
this.#priority.delete(client.clientId);
|
|
2770
|
+
else this.#releaseBudget(client.clientId);
|
|
2142
2771
|
this.#pruneCanonicalRevisions(client.paneId);
|
|
2143
2772
|
if ([...this.#clients.values()].some((candidate) => candidate.paneId === client.paneId)) return;
|
|
2144
2773
|
const pane = this.#panes.get(client.paneId);
|
|
@@ -2294,13 +2923,15 @@ export class SessionRuntimeTerminalDeliveryHub {
|
|
|
2294
2923
|
reachable.add(client.baselineRevision);
|
|
2295
2924
|
const encoding = client.encoding?.target.update;
|
|
2296
2925
|
if (encoding?.incarnation === latest.incarnation) reachable.add(encoding.revision);
|
|
2297
|
-
const flight
|
|
2298
|
-
|
|
2926
|
+
for (const flight of [client.inFlight, ...client.pipelined])
|
|
2927
|
+
if (flight?.envelope.incarnation === latest.incarnation)
|
|
2928
|
+
reachable.add(flight.envelope.canonicalRevision);
|
|
2299
2929
|
}
|
|
2300
|
-
// A hidden or slow reader owns at most one baseline and
|
|
2301
|
-
//
|
|
2302
|
-
//
|
|
2303
|
-
//
|
|
2930
|
+
// A hidden or slow reader owns at most one baseline and its transactions
|
|
2931
|
+
// (one, or TERMINAL_DELIVERY_MAX_PIPELINED for a visible pane). Historical
|
|
2932
|
+
// redraws with no reader cannot help a future delivery: new readers and
|
|
2933
|
+
// skipped revisions already receive an atomic current seed. MAX_CLIENTS
|
|
2934
|
+
// bounds this set to (TERMINAL_DELIVERY_MAX_PIPELINED + 1) * MAX_CLIENTS + 1 per pane.
|
|
2304
2935
|
for (const revision of pane.revisions.keys())
|
|
2305
2936
|
if (!reachable.has(revision)) {
|
|
2306
2937
|
this.#releaseNativeBacking(pane.revisions.get(revision)?.nativeBacking);
|
|
@@ -2319,7 +2950,10 @@ export class SessionRuntimeTerminalDeliveryHub {
|
|
|
2319
2950
|
cached.cacheBaseRevision === candidate.baselineRevision ||
|
|
2320
2951
|
(Number.isSafeInteger(cached.cacheTargetRevision) &&
|
|
2321
2952
|
cached.cacheTargetRevision! > candidate.baselineRevision) ||
|
|
2322
|
-
cached.cacheTargetRevision === candidate.inFlight?.envelope.canonicalRevision
|
|
2953
|
+
cached.cacheTargetRevision === candidate.inFlight?.envelope.canonicalRevision ||
|
|
2954
|
+
candidate.pipelined.some(
|
|
2955
|
+
(flight) => cached.cacheTargetRevision === flight.envelope.canonicalRevision,
|
|
2956
|
+
),
|
|
2323
2957
|
);
|
|
2324
2958
|
if (reachable) continue;
|
|
2325
2959
|
this.#cache.delete(key);
|
|
@@ -2584,12 +3218,13 @@ function semanticPayload(
|
|
|
2584
3218
|
// append-only history is proved here; trim/reflow and changed dimensions
|
|
2585
3219
|
// retain the full seed boundary. Equality includes wrapped and all cells.
|
|
2586
3220
|
const snapshot = target.state.snapshot;
|
|
3221
|
+
const trim = baseline ? historyTrim(baseline.history, snapshot.history) : 0;
|
|
2587
3222
|
if (
|
|
2588
3223
|
baseline &&
|
|
2589
3224
|
baselineIncarnation === update.incarnation &&
|
|
2590
3225
|
baseline.cols === snapshot.cols &&
|
|
2591
3226
|
baseline.rows === snapshot.rows &&
|
|
2592
|
-
baseline.history.length <= snapshot.history.length
|
|
3227
|
+
baseline.history.length - trim <= snapshot.history.length
|
|
2593
3228
|
) {
|
|
2594
3229
|
// Identity reuse is cheap, but detached equal histories must not turn a
|
|
2595
3230
|
// cooperative seed into an unbounded synchronous cell comparison.
|
|
@@ -2604,8 +3239,8 @@ function semanticPayload(
|
|
|
2604
3239
|
? terminalReplicaRowsEqual(left, right)
|
|
2605
3240
|
: null;
|
|
2606
3241
|
};
|
|
2607
|
-
for (let index =
|
|
2608
|
-
if (equal(baseline.history[index]!, snapshot.history[index]!) !== true)
|
|
3242
|
+
for (let index = trim; index < baseline.history.length; index++)
|
|
3243
|
+
if (equal(baseline.history[index]!, snapshot.history[index - trim]!) !== true)
|
|
2609
3244
|
return semanticSeed(target);
|
|
2610
3245
|
const rows: { index: number; row: TerminalReplicaSnapshot["grid"][number] }[] = [];
|
|
2611
3246
|
for (let index = 0; index < snapshot.grid.length; index++) {
|
|
@@ -2613,19 +3248,20 @@ function semanticPayload(
|
|
|
2613
3248
|
if (same === null) return semanticSeed(target);
|
|
2614
3249
|
if (!same) rows.push({ index, row: snapshot.grid[index]! });
|
|
2615
3250
|
}
|
|
2616
|
-
const
|
|
3251
|
+
const retained = baseline.history.length - trim;
|
|
3252
|
+
const appendedRows = snapshot.history.length - retained;
|
|
2617
3253
|
// Compact patches serialize synchronously. Do not move a large appended
|
|
2618
3254
|
// suffix or repaint across the seed-only cooperative encoding boundary.
|
|
2619
3255
|
if ((rows.length + appendedRows) * snapshot.cols >= TERMINAL_COMPACT_COOPERATIVE_SEED_CELLS)
|
|
2620
3256
|
return semanticSeed(target);
|
|
2621
|
-
const append = snapshot.history.slice(
|
|
3257
|
+
const append = snapshot.history.slice(retained);
|
|
2622
3258
|
return {
|
|
2623
3259
|
frame: "patch",
|
|
2624
3260
|
baseRevision: baselineRevision,
|
|
2625
3261
|
revision: update.revision,
|
|
2626
3262
|
patch: {
|
|
2627
3263
|
rows,
|
|
2628
|
-
...(append.length ? { historyDelta: { trim
|
|
3264
|
+
...(append.length || trim ? { historyDelta: { trim, append } } : {}),
|
|
2629
3265
|
cursor: snapshot.cursor,
|
|
2630
3266
|
modes: snapshot.modes,
|
|
2631
3267
|
placements: snapshot.placements,
|
|
@@ -2636,6 +3272,36 @@ function semanticPayload(
|
|
|
2636
3272
|
return semanticSeed(target);
|
|
2637
3273
|
}
|
|
2638
3274
|
|
|
3275
|
+
/**
|
|
3276
|
+
* How many of the baseline's oldest history rows the target dropped, proved by
|
|
3277
|
+
* row identity only: the replica keeps each surviving row object when tmux
|
|
3278
|
+
* trims at its history-limit. Zero when nothing was trimmed or no shift is
|
|
3279
|
+
* proved; the caller still checks every retained row exactly.
|
|
3280
|
+
*/
|
|
3281
|
+
function historyTrim(
|
|
3282
|
+
baseline: TerminalReplicaSnapshot["history"],
|
|
3283
|
+
target: TerminalReplicaSnapshot["history"],
|
|
3284
|
+
): number {
|
|
3285
|
+
const first = target[0];
|
|
3286
|
+
if (first === undefined || baseline[0] === first) return 0;
|
|
3287
|
+
// A shared row object (a blank row) can recur; prove at most a few shifts.
|
|
3288
|
+
let candidates = 0;
|
|
3289
|
+
for (let trim = 1; trim < baseline.length; trim++) {
|
|
3290
|
+
if (baseline[trim] !== first) continue;
|
|
3291
|
+
if (++candidates > 4) return 0;
|
|
3292
|
+
const retained = baseline.length - trim;
|
|
3293
|
+
if (retained > target.length) continue;
|
|
3294
|
+
let same = true;
|
|
3295
|
+
for (let index = 0; index < retained; index++)
|
|
3296
|
+
if (baseline[trim + index] !== target[index]) {
|
|
3297
|
+
same = false;
|
|
3298
|
+
break;
|
|
3299
|
+
}
|
|
3300
|
+
if (same) return trim;
|
|
3301
|
+
}
|
|
3302
|
+
return 0;
|
|
3303
|
+
}
|
|
3304
|
+
|
|
2639
3305
|
/** Account represented cells without traversing/serializing the snapshot. */
|
|
2640
3306
|
function canonicalUpdateCellCost(update: CanonicalTerminalReplicaUpdate): number {
|
|
2641
3307
|
if (update.type === "terminal.tombstone") return 0;
|