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.
- package/README.md +1 -1
- package/bin/cli.js +2046 -623
- package/package.json +2 -2
- package/packages/contracts/src/pane-stream.ts +45 -0
- package/packages/contracts/src/terminal-delivery.ts +114 -3
- 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 +17 -3
- 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 +80 -1
- 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-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/attachments/native-runtime.js +12 -8
- package/packages/daemon/dist/terminal/mirror/mirror-service.js +13 -0
- package/packages/daemon/dist/terminal/mirror/native-atomic-snapshot.js +17 -7
- package/packages/daemon/dist/terminal/mirror/native-grid-capture.js +129 -5
- package/packages/daemon/dist/terminal/mirror/pane-feed.js +4 -0
- package/packages/daemon/dist/terminal/mirror/pane-hold.js +13 -0
- package/packages/daemon/dist/terminal/mirror/session-channel.js +198 -27
- package/packages/daemon/dist/terminal/pane-stream/lease-manager.js +1 -0
- package/packages/daemon/dist/terminal/pane-stream/pane-stream-websocket.js +198 -12
- package/packages/daemon/dist/terminal/protocol/layout-parse.js +99 -10
- package/packages/daemon/dist/terminal/protocol/session-descriptor-discovery.js +5 -2
- package/packages/daemon/dist/terminal/session-runtime/registry.js +1 -0
- package/packages/daemon/dist/terminal/session-runtime/terminal-delivery-hub.js +465 -42
- package/packages/daemon/dist/terminal/session-runtime/terminal-interpreter-backend.js +2 -1
- package/packages/daemon/dist/terminal/session-runtime/terminal-replica-interpreter.js +46 -1
- package/packages/daemon/dist/terminal/session-runtime/terminal-replica-owner.js +128 -3
- package/packages/daemon/dist/terminal/session-runtime/xterm-terminal-interpreter-backend.js +115 -6
- package/packages/daemon/dist/tui/mirror/open-tui-workspace-runtime-port.js +94 -2
- package/packages/daemon/dist/tui/mirror/runtime/open-tui-generation-host.js +9 -1
- package/packages/daemon/dist/tui/mirror/theme.js +8 -1
- package/packages/daemon/dist/tui/mirror/workspace/connection-feedback.js +8 -0
- 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 +20 -2
- 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 +88 -1
- 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-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/attachments/native-runtime.ts +15 -1
- package/packages/daemon/src/terminal/mirror/events.ts +31 -1
- package/packages/daemon/src/terminal/mirror/mirror-service.ts +19 -0
- package/packages/daemon/src/terminal/mirror/native-atomic-snapshot.ts +16 -5
- package/packages/daemon/src/terminal/mirror/native-grid-capture.ts +164 -5
- package/packages/daemon/src/terminal/mirror/pane-feed.ts +5 -0
- package/packages/daemon/src/terminal/mirror/pane-hold.ts +16 -0
- package/packages/daemon/src/terminal/mirror/session-channel.ts +253 -38
- package/packages/daemon/src/terminal/mirror/trusted-inventory.ts +2 -0
- package/packages/daemon/src/terminal/pane-stream/lease-manager.ts +3 -0
- package/packages/daemon/src/terminal/pane-stream/pane-stream-websocket.ts +240 -12
- package/packages/daemon/src/terminal/protocol/layout-parse.ts +109 -10
- package/packages/daemon/src/terminal/protocol/session-descriptor-discovery.ts +10 -1
- package/packages/daemon/src/terminal/session-runtime/registry.ts +1 -0
- package/packages/daemon/src/terminal/session-runtime/runtime-observability.ts +2 -1
- package/packages/daemon/src/terminal/session-runtime/terminal-delivery-hub.ts +575 -39
- package/packages/daemon/src/terminal/session-runtime/terminal-interpreter-backend.ts +24 -1
- package/packages/daemon/src/terminal/session-runtime/terminal-replica-interpreter.ts +61 -5
- package/packages/daemon/src/terminal/session-runtime/terminal-replica-owner.ts +159 -32
- package/packages/daemon/src/terminal/session-runtime/xterm-terminal-interpreter-backend.ts +134 -11
- package/packages/daemon/src/tui/mirror/open-tui-workspace-runtime-port.ts +101 -2
- package/packages/daemon/src/tui/mirror/runtime/open-tui-generation-host.ts +13 -0
- package/packages/daemon/src/tui/mirror/theme.ts +13 -1
- package/packages/daemon/src/tui/mirror/workspace/connection-feedback.ts +13 -1
- package/packages/daemon-client/src/direct-application-shell-transport.ts +6 -2
- package/packages/daemon-client/src/pane-stream-client.ts +24 -6
- package/packages/daemon-client/src/workspace-client-types.ts +6 -0
- package/packages/daemon-client/src/workspace-client.ts +15 -9
- package/packages/daemon-client/src/workspace-event-supervisor.ts +3 -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,6 +95,11 @@ 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;
|
|
@@ -103,6 +119,14 @@ export const TERMINAL_DELIVERY_CONNECTION_HOLD_MS = 250;
|
|
|
103
119
|
* there only bunches panes up behind each other.
|
|
104
120
|
*/
|
|
105
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 };
|
|
106
130
|
|
|
107
131
|
export interface TerminalDeliveryMetrics {
|
|
108
132
|
/** Unique delivery subscribers, independent of their pane count. */
|
|
@@ -157,6 +181,8 @@ export interface TerminalDeliveryConnection {
|
|
|
157
181
|
projectionReceipt?(receipt: TerminalProjectionReceipt): void;
|
|
158
182
|
materialize?(request: TerminalProjectionMaterialize): void;
|
|
159
183
|
nack(nack: TerminalDeliveryNack): void;
|
|
184
|
+
/** Capability 122: older history rows for a client holding a partial history. */
|
|
185
|
+
historyRequest?(request: TerminalDeliveryHistoryRequest): void;
|
|
160
186
|
setVisibility(visibility: TerminalDeliveryVisibility): void;
|
|
161
187
|
/**
|
|
162
188
|
* The client typed into this pane: its deliveries go ahead of the
|
|
@@ -247,14 +273,47 @@ interface ClientState {
|
|
|
247
273
|
lastProjectionReceipt: TerminalProjectionReceipt | null;
|
|
248
274
|
lastMaterialize: TerminalProjectionMaterialize | null;
|
|
249
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. */
|
|
250
291
|
inFlight: {
|
|
251
292
|
envelope: DeliveryEnvelope;
|
|
252
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;
|
|
253
300
|
deliveryOrdinal: number;
|
|
254
301
|
bytes: Uint8Array;
|
|
255
302
|
nextChunk: number;
|
|
256
303
|
sentAt: number;
|
|
257
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>;
|
|
258
317
|
latestRevision: number | null;
|
|
259
318
|
encoding: PendingClientEncoding | null;
|
|
260
319
|
scheduled: boolean;
|
|
@@ -277,6 +336,7 @@ interface ClientState {
|
|
|
277
336
|
budgetTimer: SessionRuntimeTimer | null;
|
|
278
337
|
}
|
|
279
338
|
|
|
339
|
+
type DeliveryFlight = NonNullable<ClientState["inFlight"]>;
|
|
280
340
|
type SeedEncodingOutcome = { readonly bytes: Uint8Array } | { readonly error: unknown };
|
|
281
341
|
interface PendingSeedEncoding {
|
|
282
342
|
readonly key: string;
|
|
@@ -291,6 +351,7 @@ interface PendingClientEncoding {
|
|
|
291
351
|
readonly baselineRevision: number;
|
|
292
352
|
readonly baselineHash: string | null;
|
|
293
353
|
readonly reseedRequired: boolean;
|
|
354
|
+
readonly historyUnloadedRows: number;
|
|
294
355
|
readonly traceStarted: number;
|
|
295
356
|
}
|
|
296
357
|
class DeferredProjectionEncoding extends Error {
|
|
@@ -299,7 +360,11 @@ class DeferredProjectionEncoding extends Error {
|
|
|
299
360
|
}
|
|
300
361
|
}
|
|
301
362
|
class DeferredSeedEncoding extends Error {
|
|
302
|
-
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
|
+
) {
|
|
303
368
|
super("Deferred compact seed encoding");
|
|
304
369
|
}
|
|
305
370
|
}
|
|
@@ -313,9 +378,16 @@ interface CachedRepresentation {
|
|
|
313
378
|
| "alternate-projection-binary-v1";
|
|
314
379
|
readonly projection?: ProjectionBaseline;
|
|
315
380
|
readonly projectionBaseRevision?: number | null;
|
|
316
|
-
readonly frame: "seed" | "patch" | "tombstone";
|
|
381
|
+
readonly frame: "seed" | "patch" | "tombstone" | "history";
|
|
317
382
|
readonly canonicalEquivalent: boolean;
|
|
318
|
-
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
|
+
};
|
|
319
391
|
readonly representationHash?: string;
|
|
320
392
|
/** Internal reachability metadata; never serialized onto the wire. */
|
|
321
393
|
readonly cachePaneId?: string;
|
|
@@ -505,7 +577,18 @@ export class SessionRuntimeTerminalDeliveryHub {
|
|
|
505
577
|
lastProjectionReceipt: null,
|
|
506
578
|
lastMaterialize: null,
|
|
507
579
|
reseedRequired: false,
|
|
580
|
+
historyUnloadedRows: 0,
|
|
581
|
+
historyUnloadedDigest: TERMINAL_HISTORY_EMPTY_DIGEST,
|
|
582
|
+
historyLoadedRows: 0,
|
|
583
|
+
pendingHistoryRows: null,
|
|
584
|
+
historyBytesPerRow: null,
|
|
585
|
+
revisionSinceHistoryRequest: false,
|
|
508
586
|
inFlight: null,
|
|
587
|
+
pipelining: offer.protocolVersions.includes(
|
|
588
|
+
TERMINAL_DELIVERY_PIPELINING_CAPABILITY_VERSION,
|
|
589
|
+
),
|
|
590
|
+
pipelined: [],
|
|
591
|
+
droppedTransactions: new Set(),
|
|
509
592
|
latestRevision: pane.latest?.update.revision ?? null,
|
|
510
593
|
encoding: null,
|
|
511
594
|
scheduled: false,
|
|
@@ -534,6 +617,7 @@ export class SessionRuntimeTerminalDeliveryHub {
|
|
|
534
617
|
projectionReceipt: (receipt) => this.#projectionReceipt(client, receipt),
|
|
535
618
|
materialize: (request) => this.#materialize(client, request),
|
|
536
619
|
nack: (nack) => this.#nack(client, nack),
|
|
620
|
+
historyRequest: (request) => this.#historyRequest(client, request),
|
|
537
621
|
prioritize: () => {
|
|
538
622
|
if (client.closed) return;
|
|
539
623
|
this.#priority.set(client.clientId, client.paneId);
|
|
@@ -702,7 +786,7 @@ export class SessionRuntimeTerminalDeliveryHub {
|
|
|
702
786
|
),
|
|
703
787
|
maxQueueDepth: this.#maxQueueDepth,
|
|
704
788
|
inFlightBytes: [...this.#clients.values()].reduce(
|
|
705
|
-
(sum, client) => sum + (client
|
|
789
|
+
(sum, client) => sum + this.#unackedBytes(client),
|
|
706
790
|
0,
|
|
707
791
|
),
|
|
708
792
|
});
|
|
@@ -746,6 +830,7 @@ export class SessionRuntimeTerminalDeliveryHub {
|
|
|
746
830
|
this.#cancelEncoding(client);
|
|
747
831
|
client.outgoing.length = 0;
|
|
748
832
|
client.inFlight = null;
|
|
833
|
+
client.pipelined.length = 0;
|
|
749
834
|
client.budgetHeld = false;
|
|
750
835
|
logDeliveryFault(client, "source-closed", "source-restarted");
|
|
751
836
|
this.#enqueue(client, {
|
|
@@ -1125,7 +1210,7 @@ export class SessionRuntimeTerminalDeliveryHub {
|
|
|
1125
1210
|
let oldest = now;
|
|
1126
1211
|
for (const other of this.#clients.values())
|
|
1127
1212
|
if (other !== client && other.clientId === client.clientId && other.inFlight) {
|
|
1128
|
-
bytes += other
|
|
1213
|
+
bytes += this.#unackedBytes(other);
|
|
1129
1214
|
oldest = Math.min(oldest, other.inFlight.sentAt);
|
|
1130
1215
|
}
|
|
1131
1216
|
return bytes >= TERMINAL_DELIVERY_CONNECTION_UNACKED_BYTES && now - oldest >= this.#congestedMs;
|
|
@@ -1144,14 +1229,125 @@ export class SessionRuntimeTerminalDeliveryHub {
|
|
|
1144
1229
|
}
|
|
1145
1230
|
}
|
|
1146
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
|
+
|
|
1147
1343
|
#schedule(client: ClientState): void {
|
|
1148
1344
|
if (
|
|
1149
1345
|
client.closed ||
|
|
1150
1346
|
client.captureUnavailable ||
|
|
1151
|
-
client.inFlight ||
|
|
1347
|
+
(client.inFlight && !this.#canPipeline(client)) ||
|
|
1152
1348
|
client.encoding ||
|
|
1153
|
-
client.latestRevision === null ||
|
|
1154
|
-
|
|
1349
|
+
((client.latestRevision === null || client.latestRevision === this.#sentRevision(client)) &&
|
|
1350
|
+
client.pendingHistoryRows === null) ||
|
|
1155
1351
|
client.visibility === "hidden" ||
|
|
1156
1352
|
client.visibility === "frozen"
|
|
1157
1353
|
)
|
|
@@ -1160,7 +1356,11 @@ export class SessionRuntimeTerminalDeliveryHub {
|
|
|
1160
1356
|
if (client.backgroundTimer) return;
|
|
1161
1357
|
client.backgroundTimer = this.#scheduler.timer(() => {
|
|
1162
1358
|
client.backgroundTimer = null;
|
|
1163
|
-
if (
|
|
1359
|
+
if (
|
|
1360
|
+
!client.closed &&
|
|
1361
|
+
!client.inFlight &&
|
|
1362
|
+
(client.latestRevision !== null || client.pendingHistoryRows !== null)
|
|
1363
|
+
)
|
|
1164
1364
|
this.#deliver(client);
|
|
1165
1365
|
}, BACKGROUND_CADENCE_MS);
|
|
1166
1366
|
return;
|
|
@@ -1169,7 +1369,11 @@ export class SessionRuntimeTerminalDeliveryHub {
|
|
|
1169
1369
|
client.scheduled = true;
|
|
1170
1370
|
this.#scheduler.microtask(() => {
|
|
1171
1371
|
client.scheduled = false;
|
|
1172
|
-
if (
|
|
1372
|
+
if (
|
|
1373
|
+
!client.closed &&
|
|
1374
|
+
client.visibility === "visible" &&
|
|
1375
|
+
(!client.inFlight || this.#canPipeline(client))
|
|
1376
|
+
)
|
|
1173
1377
|
this.#deliver(client);
|
|
1174
1378
|
});
|
|
1175
1379
|
}
|
|
@@ -1183,33 +1387,28 @@ export class SessionRuntimeTerminalDeliveryHub {
|
|
|
1183
1387
|
},
|
|
1184
1388
|
): void {
|
|
1185
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
|
+
}
|
|
1186
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;
|
|
1187
1399
|
if (
|
|
1188
1400
|
!pane ||
|
|
1189
1401
|
!target ||
|
|
1190
1402
|
client.closed ||
|
|
1191
1403
|
client.captureUnavailable ||
|
|
1192
|
-
client.inFlight ||
|
|
1404
|
+
(client.inFlight && (completed || !this.#canPipeline(client))) ||
|
|
1193
1405
|
client.encoding ||
|
|
1194
1406
|
client.visibility === "hidden" ||
|
|
1195
1407
|
client.visibility === "frozen" ||
|
|
1196
1408
|
(!completed && target.update.revision !== client.latestRevision)
|
|
1197
1409
|
)
|
|
1198
1410
|
return;
|
|
1199
|
-
if (!completed && this.#
|
|
1200
|
-
client.budgetHeld = true;
|
|
1201
|
-
client.budgetHeldSince ??= this.#scheduler.nowMs();
|
|
1202
|
-
client.budgetTimer ??= this.#scheduler.timer(() => {
|
|
1203
|
-
client.budgetTimer = null;
|
|
1204
|
-
if (client.closed || !client.budgetHeld) return;
|
|
1205
|
-
client.budgetHeld = false;
|
|
1206
|
-
this.#schedule(client);
|
|
1207
|
-
}, this.#holdMs);
|
|
1208
|
-
return;
|
|
1209
|
-
}
|
|
1210
|
-
client.budgetHeldSince = null;
|
|
1211
|
-
client.budgetTimer?.cancel();
|
|
1212
|
-
client.budgetTimer = null;
|
|
1411
|
+
if (!completed && this.#holdForBudget(client)) return;
|
|
1213
1412
|
const traceStarted =
|
|
1214
1413
|
completed?.traceStarted ??
|
|
1215
1414
|
(this.#observability.enabled ? this.#observability.nowMicros() : 0);
|
|
@@ -1219,7 +1418,15 @@ export class SessionRuntimeTerminalDeliveryHub {
|
|
|
1219
1418
|
let deliveryEnvelope: DeliveryEnvelope | null = null;
|
|
1220
1419
|
let deliveryOrdinal: number | null = null;
|
|
1221
1420
|
try {
|
|
1222
|
-
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
|
+
}
|
|
1223
1430
|
encodedRepresentation = representation;
|
|
1224
1431
|
const transactionId = this.#scheduler.createId();
|
|
1225
1432
|
const chunkCount = Math.max(1, Math.ceil(representation.bytes.byteLength / (256 * 1024)));
|
|
@@ -1246,7 +1453,7 @@ export class SessionRuntimeTerminalDeliveryHub {
|
|
|
1246
1453
|
? representation.projectionBaseRevision
|
|
1247
1454
|
: representation.frame === "seed"
|
|
1248
1455
|
? null
|
|
1249
|
-
: Math.max(0,
|
|
1456
|
+
: Math.max(0, base.baselineRevision),
|
|
1250
1457
|
canonicalRevision: target.update.revision,
|
|
1251
1458
|
canonicalStateHash: target.update.stateHash,
|
|
1252
1459
|
representationHash:
|
|
@@ -1256,19 +1463,26 @@ export class SessionRuntimeTerminalDeliveryHub {
|
|
|
1256
1463
|
chunkCount,
|
|
1257
1464
|
canonicalEquivalent: representation.canonicalEquivalent,
|
|
1258
1465
|
history: representation.history,
|
|
1466
|
+
...(representation.historyUnloaded
|
|
1467
|
+
? { historyUnloaded: representation.historyUnloaded }
|
|
1468
|
+
: {}),
|
|
1259
1469
|
richPlacements: client.negotiated.richPlacements,
|
|
1260
1470
|
});
|
|
1261
1471
|
deliveryEnvelope = envelope;
|
|
1262
1472
|
deliveryOrdinal = ++this.#deliveryOrdinal;
|
|
1263
1473
|
registerTerminalDeliveryObservationOrdinal(envelope, deliveryOrdinal);
|
|
1264
|
-
client.
|
|
1474
|
+
if (client.pendingHistoryRows !== null) client.revisionSinceHistoryRequest = true;
|
|
1475
|
+
const flight: DeliveryFlight = {
|
|
1265
1476
|
envelope,
|
|
1266
1477
|
projection: representation.projection ?? null,
|
|
1478
|
+
historyRows: representation.historyRows ?? null,
|
|
1267
1479
|
deliveryOrdinal,
|
|
1268
1480
|
bytes: representation.bytes,
|
|
1269
1481
|
nextChunk: 0,
|
|
1270
1482
|
sentAt: this.#scheduler.nowMs(),
|
|
1271
1483
|
};
|
|
1484
|
+
if (pipelining) client.pipelined.push(flight);
|
|
1485
|
+
else client.inFlight = flight;
|
|
1272
1486
|
this.#enqueue(client, envelope);
|
|
1273
1487
|
this.#recordDeliveryStatus(client, pane);
|
|
1274
1488
|
if (target.trace?.traceId === pane.pendingDeliveryTrace?.traceId)
|
|
@@ -1283,6 +1497,7 @@ export class SessionRuntimeTerminalDeliveryHub {
|
|
|
1283
1497
|
error.payload,
|
|
1284
1498
|
traceStarted,
|
|
1285
1499
|
error instanceof DeferredProjectionEncoding,
|
|
1500
|
+
error instanceof DeferredSeedEncoding ? error.variant : "complete",
|
|
1286
1501
|
);
|
|
1287
1502
|
return;
|
|
1288
1503
|
}
|
|
@@ -1315,7 +1530,7 @@ export class SessionRuntimeTerminalDeliveryHub {
|
|
|
1315
1530
|
inFlightBytes: metrics.inFlightBytes,
|
|
1316
1531
|
...(encodedRepresentation
|
|
1317
1532
|
? {
|
|
1318
|
-
representation: encodedRepresentation.frame,
|
|
1533
|
+
representation: encodedRepresentation.frame as "seed" | "patch" | "tombstone",
|
|
1319
1534
|
representationBytes: encodedRepresentation.bytes.byteLength,
|
|
1320
1535
|
}
|
|
1321
1536
|
: {}),
|
|
@@ -1364,6 +1579,7 @@ export class SessionRuntimeTerminalDeliveryHub {
|
|
|
1364
1579
|
payload: TerminalSemanticDeliveryPayload | TerminalProjectionPayload,
|
|
1365
1580
|
traceStarted: number,
|
|
1366
1581
|
projection = false,
|
|
1582
|
+
variant = "complete",
|
|
1367
1583
|
): void {
|
|
1368
1584
|
const key = cacheKey([
|
|
1369
1585
|
target.update.workspaceName,
|
|
@@ -1376,7 +1592,7 @@ export class SessionRuntimeTerminalDeliveryHub {
|
|
|
1376
1592
|
client.negotiated.richPlacements ? "rich" : "plain",
|
|
1377
1593
|
projection
|
|
1378
1594
|
? `projection:${client.projectionBaseline?.revision ?? -1}:${client.projectionBaseline?.hash ?? "none"}`
|
|
1379
|
-
:
|
|
1595
|
+
: variant,
|
|
1380
1596
|
]);
|
|
1381
1597
|
let job = this.#pendingEncodings.get(key);
|
|
1382
1598
|
let created = false;
|
|
@@ -1406,6 +1622,7 @@ export class SessionRuntimeTerminalDeliveryHub {
|
|
|
1406
1622
|
baselineRevision: client.baselineRevision,
|
|
1407
1623
|
baselineHash: client.baselineHash,
|
|
1408
1624
|
reseedRequired: client.reseedRequired,
|
|
1625
|
+
historyUnloadedRows: client.historyUnloadedRows,
|
|
1409
1626
|
traceStarted,
|
|
1410
1627
|
};
|
|
1411
1628
|
owned.clients.add(client);
|
|
@@ -1462,6 +1679,7 @@ export class SessionRuntimeTerminalDeliveryHub {
|
|
|
1462
1679
|
client.baselineRevision !== encoding.baselineRevision ||
|
|
1463
1680
|
client.baselineHash !== encoding.baselineHash ||
|
|
1464
1681
|
client.reseedRequired !== encoding.reseedRequired ||
|
|
1682
|
+
client.historyUnloadedRows !== encoding.historyUnloadedRows ||
|
|
1465
1683
|
client.inFlight ||
|
|
1466
1684
|
client.visibility === "hidden" ||
|
|
1467
1685
|
client.visibility === "frozen" ||
|
|
@@ -1523,6 +1741,8 @@ export class SessionRuntimeTerminalDeliveryHub {
|
|
|
1523
1741
|
projectionBaseRevision: payload.frame === "seed" ? null : payload.baseRevision,
|
|
1524
1742
|
};
|
|
1525
1743
|
}
|
|
1744
|
+
if (client.negotiated.partialHistory === "tail-v1" && target.state.snapshot)
|
|
1745
|
+
return this.#partialRepresentation(client, pane, target, seedOutcome);
|
|
1526
1746
|
const key = cacheKey([
|
|
1527
1747
|
target.update.workspaceName,
|
|
1528
1748
|
target.update.semanticPaneId,
|
|
@@ -1878,6 +2098,7 @@ export class SessionRuntimeTerminalDeliveryHub {
|
|
|
1878
2098
|
#ack(client: ClientState, input: TerminalDeliveryAck): void {
|
|
1879
2099
|
const ack = TerminalDeliveryAckSchemaZ.parse(input);
|
|
1880
2100
|
const flight = client.inFlight;
|
|
2101
|
+
if (client.droppedTransactions.has(ack.transactionId)) return;
|
|
1881
2102
|
if (!flight) {
|
|
1882
2103
|
if (client.lastAck && JSON.stringify(client.lastAck) === JSON.stringify(ack)) return;
|
|
1883
2104
|
const closed = client.sourceClosedFlight;
|
|
@@ -1926,7 +2147,14 @@ export class SessionRuntimeTerminalDeliveryHub {
|
|
|
1926
2147
|
client.baselineRevision = ack.canonicalRevision;
|
|
1927
2148
|
client.baselineHash = ack.canonicalStateHash;
|
|
1928
2149
|
client.reseedRequired = false;
|
|
1929
|
-
|
|
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();
|
|
1930
2158
|
this.#releaseBudget(client.clientId);
|
|
1931
2159
|
client.lastAck = ack;
|
|
1932
2160
|
if (client.latestRevision === ack.canonicalRevision) client.latestRevision = null;
|
|
@@ -1981,6 +2209,301 @@ export class SessionRuntimeTerminalDeliveryHub {
|
|
|
1981
2209
|
this.#recordDeliveryStatus(client, this.#panes.get(client.paneId));
|
|
1982
2210
|
}
|
|
1983
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
|
+
|
|
1984
2507
|
#projectionReceipt(client: ClientState, input: TerminalProjectionReceipt): void {
|
|
1985
2508
|
const receipt = TerminalProjectionReceiptSchemaZ.parse(input);
|
|
1986
2509
|
if (
|
|
@@ -2079,6 +2602,7 @@ export class SessionRuntimeTerminalDeliveryHub {
|
|
|
2079
2602
|
nack.transactionId === closed.transactionId
|
|
2080
2603
|
)
|
|
2081
2604
|
return;
|
|
2605
|
+
if (nack.transactionId !== null && client.droppedTransactions.has(nack.transactionId)) return;
|
|
2082
2606
|
const envelope = client.inFlight?.envelope;
|
|
2083
2607
|
if (
|
|
2084
2608
|
!envelope ||
|
|
@@ -2095,6 +2619,7 @@ export class SessionRuntimeTerminalDeliveryHub {
|
|
|
2095
2619
|
this.#nacks += 1;
|
|
2096
2620
|
client.reseedRequired = true;
|
|
2097
2621
|
client.inFlight = null;
|
|
2622
|
+
this.#dropPipelined(client);
|
|
2098
2623
|
this.#releaseBudget(client.clientId);
|
|
2099
2624
|
this.#pruneCanonicalRevisions(client.paneId);
|
|
2100
2625
|
this.#schedule(client);
|
|
@@ -2150,6 +2675,7 @@ export class SessionRuntimeTerminalDeliveryHub {
|
|
|
2150
2675
|
client.outgoing.length = 0;
|
|
2151
2676
|
if (reason === "source-closed") client.sourceClosedFlight = client.inFlight?.envelope ?? null;
|
|
2152
2677
|
client.inFlight = null;
|
|
2678
|
+
this.#dropPipelined(client);
|
|
2153
2679
|
this.#releaseBudget(client.clientId);
|
|
2154
2680
|
const fault: TerminalDeliveryServerMessage = {
|
|
2155
2681
|
type: "terminal.delivery.fault",
|
|
@@ -2187,10 +2713,15 @@ export class SessionRuntimeTerminalDeliveryHub {
|
|
|
2187
2713
|
while (!client.closed) {
|
|
2188
2714
|
const queued = client.outgoing.shift();
|
|
2189
2715
|
let next = queued?.();
|
|
2190
|
-
|
|
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;
|
|
2191
2719
|
if (!next && flight && flight.nextChunk < flight.envelope.chunkCount) {
|
|
2192
2720
|
const index = flight.nextChunk;
|
|
2193
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);
|
|
2194
2725
|
next = {
|
|
2195
2726
|
type: "terminal.delivery.chunk",
|
|
2196
2727
|
transactionId: flight.envelope.transactionId,
|
|
@@ -2392,13 +2923,15 @@ export class SessionRuntimeTerminalDeliveryHub {
|
|
|
2392
2923
|
reachable.add(client.baselineRevision);
|
|
2393
2924
|
const encoding = client.encoding?.target.update;
|
|
2394
2925
|
if (encoding?.incarnation === latest.incarnation) reachable.add(encoding.revision);
|
|
2395
|
-
const flight
|
|
2396
|
-
|
|
2926
|
+
for (const flight of [client.inFlight, ...client.pipelined])
|
|
2927
|
+
if (flight?.envelope.incarnation === latest.incarnation)
|
|
2928
|
+
reachable.add(flight.envelope.canonicalRevision);
|
|
2397
2929
|
}
|
|
2398
|
-
// A hidden or slow reader owns at most one baseline and
|
|
2399
|
-
//
|
|
2400
|
-
//
|
|
2401
|
-
//
|
|
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.
|
|
2402
2935
|
for (const revision of pane.revisions.keys())
|
|
2403
2936
|
if (!reachable.has(revision)) {
|
|
2404
2937
|
this.#releaseNativeBacking(pane.revisions.get(revision)?.nativeBacking);
|
|
@@ -2417,7 +2950,10 @@ export class SessionRuntimeTerminalDeliveryHub {
|
|
|
2417
2950
|
cached.cacheBaseRevision === candidate.baselineRevision ||
|
|
2418
2951
|
(Number.isSafeInteger(cached.cacheTargetRevision) &&
|
|
2419
2952
|
cached.cacheTargetRevision! > candidate.baselineRevision) ||
|
|
2420
|
-
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
|
+
),
|
|
2421
2957
|
);
|
|
2422
2958
|
if (reachable) continue;
|
|
2423
2959
|
this.#cache.delete(key);
|