tmux-ide 2.9.26 → 2.9.28

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (89) hide show
  1. package/README.md +1 -1
  2. package/bin/cli.js +2046 -623
  3. package/package.json +2 -2
  4. package/packages/contracts/src/pane-stream.ts +45 -0
  5. package/packages/contracts/src/terminal-delivery.ts +114 -3
  6. package/packages/core/src/index.ts +1 -0
  7. package/packages/core/src/terminal-delivery-binary.ts +8 -7
  8. package/packages/core/src/terminal-delivery.ts +174 -3
  9. package/packages/core/src/terminal-partial-history.ts +152 -0
  10. package/packages/core/src/terminal-replica.ts +70 -1
  11. package/packages/daemon/dist/command-center/owner-keep-alive.js +30 -0
  12. package/packages/daemon/dist/command-center/resources/terminal-runtime-inventory.js +17 -3
  13. package/packages/daemon/dist/command-center/server.js +3 -0
  14. package/packages/daemon/dist/command-center/tmux-server-attach.js +51 -0
  15. package/packages/daemon/dist/command-center/tmux-servers.js +80 -1
  16. package/packages/daemon/dist/doctor.js +4 -4
  17. package/packages/daemon/dist/lib/app-config.js +7 -0
  18. package/packages/daemon/dist/lib/node-requirement.js +20 -0
  19. package/packages/daemon/dist/lib/require-supported-node.js +8 -0
  20. package/packages/daemon/dist/lib/tmux-server-session-open.js +38 -12
  21. package/packages/daemon/dist/lib/workspace-promotion.js +4 -4
  22. package/packages/daemon/dist/native/TmuxIdeNotifier.app/Contents/Info.plist +2 -2
  23. package/packages/daemon/dist/native/TmuxIdeNotifier.app/Contents/MacOS/tmux-ide-notifier +0 -0
  24. package/packages/daemon/dist/native/TmuxIdeNotifier.app/Contents/Resources/Assets.car +0 -0
  25. package/packages/daemon/dist/native/TmuxIdeNotifier.app/Contents/_CodeSignature/CodeResources +2 -2
  26. package/packages/daemon/dist/server/pane-stream-upgrade.js +39 -1
  27. package/packages/daemon/dist/terminal/attachments/native-runtime.js +12 -8
  28. package/packages/daemon/dist/terminal/mirror/mirror-service.js +13 -0
  29. package/packages/daemon/dist/terminal/mirror/native-atomic-snapshot.js +17 -7
  30. package/packages/daemon/dist/terminal/mirror/native-grid-capture.js +129 -5
  31. package/packages/daemon/dist/terminal/mirror/pane-feed.js +4 -0
  32. package/packages/daemon/dist/terminal/mirror/pane-hold.js +13 -0
  33. package/packages/daemon/dist/terminal/mirror/session-channel.js +198 -27
  34. package/packages/daemon/dist/terminal/pane-stream/lease-manager.js +1 -0
  35. package/packages/daemon/dist/terminal/pane-stream/pane-stream-websocket.js +198 -12
  36. package/packages/daemon/dist/terminal/protocol/layout-parse.js +99 -10
  37. package/packages/daemon/dist/terminal/protocol/session-descriptor-discovery.js +5 -2
  38. package/packages/daemon/dist/terminal/session-runtime/registry.js +1 -0
  39. package/packages/daemon/dist/terminal/session-runtime/terminal-delivery-hub.js +465 -42
  40. package/packages/daemon/dist/terminal/session-runtime/terminal-interpreter-backend.js +2 -1
  41. package/packages/daemon/dist/terminal/session-runtime/terminal-replica-interpreter.js +46 -1
  42. package/packages/daemon/dist/terminal/session-runtime/terminal-replica-owner.js +128 -3
  43. package/packages/daemon/dist/terminal/session-runtime/xterm-terminal-interpreter-backend.js +115 -6
  44. package/packages/daemon/dist/tui/mirror/open-tui-workspace-runtime-port.js +94 -2
  45. package/packages/daemon/dist/tui/mirror/runtime/open-tui-generation-host.js +9 -1
  46. package/packages/daemon/dist/tui/mirror/theme.js +8 -1
  47. package/packages/daemon/dist/tui/mirror/workspace/connection-feedback.js +8 -0
  48. package/packages/daemon/src/command-center/attach-window-rule.cases.json +84 -0
  49. package/packages/daemon/src/command-center/owner-keep-alive.ts +38 -0
  50. package/packages/daemon/src/command-center/resources/terminal-runtime-inventory.ts +20 -2
  51. package/packages/daemon/src/command-center/server.ts +3 -0
  52. package/packages/daemon/src/command-center/tmux-server-attach.ts +75 -0
  53. package/packages/daemon/src/command-center/tmux-servers.ts +88 -1
  54. package/packages/daemon/src/doctor.ts +4 -4
  55. package/packages/daemon/src/lib/app-config.ts +15 -0
  56. package/packages/daemon/src/lib/node-requirement.ts +22 -0
  57. package/packages/daemon/src/lib/require-supported-node.ts +9 -0
  58. package/packages/daemon/src/lib/tmux-server-session-open.ts +53 -15
  59. package/packages/daemon/src/lib/workspace-promotion.ts +4 -4
  60. package/packages/daemon/src/server/pane-stream-upgrade.ts +41 -1
  61. package/packages/daemon/src/terminal/attachments/native-runtime.ts +15 -1
  62. package/packages/daemon/src/terminal/mirror/events.ts +31 -1
  63. package/packages/daemon/src/terminal/mirror/mirror-service.ts +19 -0
  64. package/packages/daemon/src/terminal/mirror/native-atomic-snapshot.ts +16 -5
  65. package/packages/daemon/src/terminal/mirror/native-grid-capture.ts +164 -5
  66. package/packages/daemon/src/terminal/mirror/pane-feed.ts +5 -0
  67. package/packages/daemon/src/terminal/mirror/pane-hold.ts +16 -0
  68. package/packages/daemon/src/terminal/mirror/session-channel.ts +253 -38
  69. package/packages/daemon/src/terminal/mirror/trusted-inventory.ts +2 -0
  70. package/packages/daemon/src/terminal/pane-stream/lease-manager.ts +3 -0
  71. package/packages/daemon/src/terminal/pane-stream/pane-stream-websocket.ts +240 -12
  72. package/packages/daemon/src/terminal/protocol/layout-parse.ts +109 -10
  73. package/packages/daemon/src/terminal/protocol/session-descriptor-discovery.ts +10 -1
  74. package/packages/daemon/src/terminal/session-runtime/registry.ts +1 -0
  75. package/packages/daemon/src/terminal/session-runtime/runtime-observability.ts +2 -1
  76. package/packages/daemon/src/terminal/session-runtime/terminal-delivery-hub.ts +575 -39
  77. package/packages/daemon/src/terminal/session-runtime/terminal-interpreter-backend.ts +24 -1
  78. package/packages/daemon/src/terminal/session-runtime/terminal-replica-interpreter.ts +61 -5
  79. package/packages/daemon/src/terminal/session-runtime/terminal-replica-owner.ts +159 -32
  80. package/packages/daemon/src/terminal/session-runtime/xterm-terminal-interpreter-backend.ts +134 -11
  81. package/packages/daemon/src/tui/mirror/open-tui-workspace-runtime-port.ts +101 -2
  82. package/packages/daemon/src/tui/mirror/runtime/open-tui-generation-host.ts +13 -0
  83. package/packages/daemon/src/tui/mirror/theme.ts +13 -1
  84. package/packages/daemon/src/tui/mirror/workspace/connection-feedback.ts +13 -1
  85. package/packages/daemon-client/src/direct-application-shell-transport.ts +6 -2
  86. package/packages/daemon-client/src/pane-stream-client.ts +24 -6
  87. package/packages/daemon-client/src/workspace-client-types.ts +6 -0
  88. package/packages/daemon-client/src/workspace-client.ts +15 -9
  89. package/packages/daemon-client/src/workspace-event-supervisor.ts +3 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tmux-ide",
3
- "version": "2.9.26",
3
+ "version": "2.9.28",
4
4
  "description": "The open-source workspace for coding agents: run Claude Code, Codex and other agents in tmux with live status",
5
5
  "type": "module",
6
6
  "bin": {
@@ -124,7 +124,7 @@
124
124
  "developer-tools"
125
125
  ],
126
126
  "engines": {
127
- "node": ">=20"
127
+ "node": ">=24"
128
128
  },
129
129
  "repository": {
130
130
  "type": "git",
@@ -46,6 +46,7 @@ import {
46
46
  TerminalDeliveryEnvelopeSchemaZ,
47
47
  TerminalDeliveryFaultSchemaZ,
48
48
  TerminalDeliveryNackSchemaZ,
49
+ TerminalDeliveryHistoryRequestSchemaZ,
49
50
  TerminalDeliveryNegotiationResultSchemaZ,
50
51
  TerminalDeliveryOfferSchemaZ,
51
52
  TerminalDeliveryVisibilitySchemaZ,
@@ -144,10 +145,46 @@ export const PaneStreamLeaseRequestSchemaZ = z
144
145
  * Released daemons refuse the field: send it only to a daemon that has it.
145
146
  */
146
147
  semanticWindowId: TerminalAttachmentSemanticWindowIdSchemaZ.optional(),
148
+ /**
149
+ * #872: a person asked to retry now. The daemon asks tmux for the
150
+ * stream's panes even inside a hold after failed recoveries. Released
151
+ * daemons refuse the field: send it only to 2.9.28 or newer.
152
+ */
153
+ retryAskedByPerson: z.literal(true).optional(),
147
154
  })
148
155
  .strict();
149
156
  export type PaneStreamLeaseRequest = z.infer<typeof PaneStreamLeaseRequestSchemaZ>;
150
157
 
158
+ /**
159
+ * #872: the close of a stream whose pane the daemon holds back after its
160
+ * recoveries failed in a row: "try again later", with the reason
161
+ * `pane-held:<ms>:<why>`. `<ms>` is the whole milliseconds until the daemon
162
+ * asks tmux again; `<why>` is `too-large` when the last try's capture was over
163
+ * its size cap, else `failed`. A client that does not know the reason treats
164
+ * it as any other close and reconnects.
165
+ */
166
+ export const PANE_STREAM_PANE_HELD_CLOSE_CODE = 1013;
167
+ export type PaneStreamPaneHoldWhy = "too-large" | "failed";
168
+ export interface PaneStreamPaneHold {
169
+ readonly retryInMs: number;
170
+ readonly why: PaneStreamPaneHoldWhy;
171
+ }
172
+ const PANE_HELD_CLOSE_REASON = /^pane-held:([1-9][0-9]{0,5}):([a-z-]+)$/u;
173
+ /** The longest wait a reader takes; the daemon's hold is at most 30 s. */
174
+ const PANE_HELD_MAX_READ_MS = 600_000;
175
+
176
+ export function paneHeldCloseReason(hold: PaneStreamPaneHold): string {
177
+ return `pane-held:${Math.max(1, Math.ceil(hold.retryInMs))}:${hold.why}`;
178
+ }
179
+
180
+ export function readPaneHeldCloseReason(reason: string): PaneStreamPaneHold | null {
181
+ const match = PANE_HELD_CLOSE_REASON.exec(reason);
182
+ if (!match) return null;
183
+ const retryInMs = Number(match[1]);
184
+ if (retryInMs > PANE_HELD_MAX_READ_MS) return null;
185
+ return { retryInMs, why: match[2] === "too-large" ? "too-large" : "failed" };
186
+ }
187
+
151
188
  export const PaneStreamRedemptionTicketSchemaZ = z.string().regex(/^ps2_[A-Za-z0-9_-]{43}$/u);
152
189
 
153
190
  /** Same canonical loopback discipline as the terminal-attachment endpoint. */
@@ -346,6 +383,13 @@ export const PaneStreamTerminalProjectionMaterializeFrameSchemaZ = z
346
383
  request: TerminalProjectionMaterializeSchemaZ,
347
384
  })
348
385
  .strict();
386
+ /** Capability 122: ask for older history rows of one pane. */
387
+ export const PaneStreamTerminalDeliveryHistoryRequestFrameSchemaZ = z
388
+ .object({
389
+ type: z.literal("terminal-delivery-history-request"),
390
+ request: TerminalDeliveryHistoryRequestSchemaZ,
391
+ })
392
+ .strict();
349
393
  export const PaneStreamTerminalDeliveryNackFrameSchemaZ = z
350
394
  .object({ type: z.literal("terminal-delivery-nack"), nack: TerminalDeliveryNackSchemaZ })
351
395
  .strict();
@@ -471,6 +515,7 @@ export const PaneStreamClientFrameSchemaZ = z.union([
471
515
  PaneStreamTerminalProjectionReceiptFrameSchemaZ,
472
516
  PaneStreamTerminalProjectionMaterializeFrameSchemaZ,
473
517
  PaneStreamTerminalDeliveryNackFrameSchemaZ,
518
+ PaneStreamTerminalDeliveryHistoryRequestFrameSchemaZ,
474
519
  PaneStreamTerminalDeliveryVisibilityFrameSchemaZ,
475
520
  PaneStreamSemanticIntentFrameSchemaZ,
476
521
  PaneStreamViewportFrameSchemaZ,
@@ -5,6 +5,7 @@ import { TerminalAttachmentSemanticPaneIdSchemaZ } from "./semantic-identity.ts"
5
5
  import { SessionRuntimeGenerationSchemaZ } from "./session-runtime.ts";
6
6
  import { WorkspaceIdSchemaZ } from "./workspace-state.ts";
7
7
  import {
8
+ TerminalReplicaRowSchemaZ,
8
9
  TerminalReplicaPatchPayloadSchemaZ,
9
10
  TerminalReplicaSnapshotSchemaZ,
10
11
  TerminalReplicaTombstonePayloadSchemaZ,
@@ -20,6 +21,32 @@ export const TERMINAL_DELIVERY_BINARY_CAPABILITY_VERSION = 109;
20
21
  export const TERMINAL_DELIVERY_BINARY_LOCAL_CAPABILITY_VERSION = 110;
21
22
 
22
23
  export const TERMINAL_DELIVERY_PROTOCOL_VERSION = 1 as const;
24
+
25
+ /**
26
+ * History on demand (owner's decision 17 Nov, 2.9.26). With capability 122 a
27
+ * seed carries the screen plus the newest TERMINAL_DELIVERY_HISTORY_TAIL_ROWS
28
+ * history rows and a commitment `{rows, digest}` to the older, unloaded rows.
29
+ * The rows digest is a polynomial fold, so the loaded rows and the commitment
30
+ * still verify against the full canonical state hash. The client asks for
31
+ * older rows with a history request; they come newest first in a "history"
32
+ * frame at its current revision. It replaces deferred-history-v1 (111/112).
33
+ */
34
+ export const TERMINAL_DELIVERY_PARTIAL_HISTORY_CAPABILITY_VERSION = 122;
35
+ export const TERMINAL_DELIVERY_HISTORY_TAIL_ROWS = 200;
36
+ /**
37
+ * Capability 124: the client takes patches pipelined behind unacknowledged
38
+ * ones. It applies each in order against its last applied revision, ACKs each
39
+ * in order, and holds a delivery that arrives while an earlier one is still
40
+ * decoding or unsettled. Without it a pane keeps one delivery in flight.
41
+ */
42
+ export const TERMINAL_DELIVERY_PIPELINING_CAPABILITY_VERSION = 124;
43
+ /**
44
+ * Deliveries a pane of a 124 client may have unacknowledged. After a NACK a
45
+ * client NACKs each pipelined patch once; the app fails a stream on its
46
+ * fourth NACK in a row, so this stays at 3 or below.
47
+ */
48
+ export const TERMINAL_DELIVERY_MAX_PIPELINED = 3;
49
+ export const TERMINAL_DELIVERY_HISTORY_CHUNK_MAX_ROWS = 2000;
23
50
  /** Capability numbers one offer may carry (nine before 2.9.26). */
24
51
  export const TERMINAL_DELIVERY_MAX_OFFERED_VERSIONS = 16;
25
52
  export const TERMINAL_DELIVERY_CHUNK_BYTES = 256 * 1024;
@@ -61,6 +88,8 @@ export const TerminalDeliveryNegotiatedSchemaZ = z
61
88
  encoding: TerminalDeliveryEncodingSchemaZ,
62
89
  fallbackEncoding: z.literal("semantic-v1").nullable().optional(),
63
90
  alternateScreenProjection: z.literal("deferred-history-v1").optional(),
91
+ /** Capability 122: history on demand. */
92
+ partialHistory: z.literal("tail-v1").optional(),
64
93
  richPlacements: z.boolean(),
65
94
  generation: SessionRuntimeGenerationSchemaZ,
66
95
  deliveryNonce: z.uuid(),
@@ -69,6 +98,15 @@ export const TerminalDeliveryNegotiatedSchemaZ = z
69
98
  .superRefine((value, context) => {
70
99
  if (value.alternateScreenProjection && value.encoding !== "semantic-binary-v1")
71
100
  context.addIssue({ code: "custom", message: "projection requires binary semantic delivery" });
101
+ if (
102
+ value.partialHistory &&
103
+ ((value.encoding !== "semantic-compact-v1" && value.encoding !== "semantic-binary-v1") ||
104
+ value.alternateScreenProjection)
105
+ )
106
+ context.addIssue({
107
+ code: "custom",
108
+ message: "partial history requires compact or binary semantic delivery and no projection",
109
+ });
72
110
  if (value.fallbackEncoding && value.encoding !== "semantic-compact-v1")
73
111
  context.addIssue({ code: "custom", message: "fallback encoding requires compact semantic" });
74
112
  if (
@@ -98,6 +136,28 @@ export type TerminalDeliveryNegotiationResult = z.infer<
98
136
  typeof TerminalDeliveryNegotiationResultSchemaZ
99
137
  >;
100
138
 
139
+ /** The rows digest of no rows. */
140
+ export const TERMINAL_HISTORY_EMPTY_DIGEST = "0000000000000000";
141
+
142
+ /**
143
+ * Pinned at `rows: 0` to the digest of no rows: with that anchor, each history
144
+ * chunk's chain check is an induction proof (review of #435). Without it a
145
+ * forged chain of chunks could end in any digest and still match the hash.
146
+ */
147
+ export const TerminalDeliveryHistoryCommitmentSchemaZ = z
148
+ .object({
149
+ rows: z.number().int().nonnegative().max(1_000_000),
150
+ digest: z.string().regex(/^[0-9a-f]{16}$/u),
151
+ })
152
+ .strict()
153
+ .refine(
154
+ (value) => value.rows > 0 || value.digest === TERMINAL_HISTORY_EMPTY_DIGEST,
155
+ "an empty commitment has the empty digest",
156
+ );
157
+ export type TerminalDeliveryHistoryCommitment = z.infer<
158
+ typeof TerminalDeliveryHistoryCommitmentSchemaZ
159
+ >;
160
+
101
161
  const DeliveryAddressSchemaZ = z.object({
102
162
  workspaceName: WorkspaceIdSchemaZ,
103
163
  semanticPaneId: TerminalAttachmentSemanticPaneIdSchemaZ,
@@ -119,7 +179,7 @@ export const TerminalDeliveryEnvelopeSchemaZ = DeliveryAddressSchemaZ.extend({
119
179
  causalCellProof: CausalCellProofV1SchemaZ.optional(),
120
180
  protocolVersion: z.literal(TERMINAL_DELIVERY_PROTOCOL_VERSION),
121
181
  encoding: TerminalDeliveryEncodingSchemaZ,
122
- frame: z.enum(["seed", "patch", "tombstone"]),
182
+ frame: z.enum(["seed", "patch", "tombstone", "history"]),
123
183
  baseRevision: z.number().int().min(-1).nullable(),
124
184
  canonicalRevision: z.number().int().nonnegative(),
125
185
  canonicalStateHash: z.string().regex(/^[0-9a-f]{16}$/u),
@@ -131,7 +191,9 @@ export const TerminalDeliveryEnvelopeSchemaZ = DeliveryAddressSchemaZ.extend({
131
191
  .max(TERMINAL_DELIVERY_MAX_REPRESENTATION_BYTES),
132
192
  chunkCount: z.number().int().positive().max(256),
133
193
  canonicalEquivalent: z.boolean(),
134
- history: z.enum(["complete", "truncated", "not-applicable"]),
194
+ history: z.enum(["complete", "partial", "truncated", "not-applicable"]),
195
+ /** Capability 122: the history rows the client does not hold, oldest first. */
196
+ historyUnloaded: TerminalDeliveryHistoryCommitmentSchemaZ.optional(),
135
197
  richPlacements: z.boolean(),
136
198
  })
137
199
  .strict()
@@ -151,17 +213,39 @@ export const TerminalDeliveryEnvelopeSchemaZ = DeliveryAddressSchemaZ.extend({
151
213
  });
152
214
  if (value.frame === "seed" && value.baseRevision !== null)
153
215
  context.addIssue({ code: "custom", message: "seed baseRevision must be null" });
216
+ if (
217
+ value.frame === "history" &&
218
+ (value.baseRevision !== value.canonicalRevision || value.history !== "partial")
219
+ )
220
+ context.addIssue({
221
+ code: "custom",
222
+ message: "a history frame is partial and stays at the client's revision",
223
+ });
154
224
  if (
155
225
  value.frame !== "seed" &&
226
+ value.frame !== "history" &&
156
227
  (value.baseRevision === null || value.canonicalRevision <= value.baseRevision)
157
228
  )
158
229
  context.addIssue({
159
230
  code: "custom",
160
231
  message: "patch/tombstone requires an earlier baseRevision",
161
232
  });
233
+ if ((value.history === "partial") !== (value.historyUnloaded !== undefined))
234
+ context.addIssue({
235
+ code: "custom",
236
+ message: "a partial history carries its unloaded commitment, and only it does",
237
+ });
238
+ if (value.history === "partial" && (!semantic || value.frame === "tombstone"))
239
+ context.addIssue({
240
+ code: "custom",
241
+ message: "partial history is semantic and not a tombstone",
242
+ });
162
243
  if (
163
244
  semantic &&
164
- (!value.canonicalEquivalent || (value.frame !== "tombstone" && value.history !== "complete"))
245
+ (!value.canonicalEquivalent ||
246
+ (value.frame !== "tombstone" &&
247
+ value.history !== "complete" &&
248
+ value.history !== "partial"))
165
249
  )
166
250
  context.addIssue({
167
251
  code: "custom",
@@ -225,6 +309,19 @@ export const TerminalDeliveryNackSchemaZ = DeliveryAddressSchemaZ.extend({
225
309
  }).strict();
226
310
  export type TerminalDeliveryNack = z.infer<typeof TerminalDeliveryNackSchemaZ>;
227
311
 
312
+ /**
313
+ * Capability 122: the client asks for the newest `maxRows` of its unloaded
314
+ * history. It names its committed revision and unloaded row count; a request
315
+ * that no longer matches the daemon's view of that baseline is dropped.
316
+ */
317
+ export const TerminalDeliveryHistoryRequestSchemaZ = DeliveryAddressSchemaZ.extend({
318
+ type: z.literal("terminal.delivery.history-request"),
319
+ canonicalRevision: z.number().int().nonnegative(),
320
+ unloadedRows: z.number().int().nonnegative().max(1_000_000),
321
+ maxRows: z.number().int().min(1).max(TERMINAL_DELIVERY_HISTORY_CHUNK_MAX_ROWS),
322
+ }).strict();
323
+ export type TerminalDeliveryHistoryRequest = z.infer<typeof TerminalDeliveryHistoryRequestSchemaZ>;
324
+
228
325
  export type TerminalDeliveryServerMessage =
229
326
  | TerminalDeliveryEnvelope
230
327
  | TerminalProjectionEnvelope
@@ -269,3 +366,17 @@ export const TerminalSemanticDeliveryPayloadSchemaZ = z.discriminatedUnion("fram
269
366
  export type TerminalSemanticDeliveryPayload = z.infer<
270
367
  typeof TerminalSemanticDeliveryPayloadSchemaZ
271
368
  >;
369
+
370
+ /**
371
+ * Capability 122: a "history" frame's payload, the newest unloaded rows,
372
+ * oldest first, at the client's revision. Kept apart from the seed, patch and
373
+ * tombstone union: it changes no revision and no canonical state.
374
+ */
375
+ export const TerminalSemanticHistoryPayloadSchemaZ = z
376
+ .object({
377
+ frame: z.literal("history"),
378
+ revision: z.number().int().nonnegative(),
379
+ rows: z.array(TerminalReplicaRowSchemaZ).min(1).max(TERMINAL_DELIVERY_HISTORY_CHUNK_MAX_ROWS),
380
+ })
381
+ .strict();
382
+ export type TerminalSemanticHistoryPayload = z.infer<typeof TerminalSemanticHistoryPayloadSchemaZ>;
@@ -22,3 +22,4 @@ export * from "./resize-transaction.ts";
22
22
  export * from "./kitty-placeholders.ts";
23
23
 
24
24
  export * from "./terminal-projection.ts";
25
+ export * from "./terminal-partial-history.ts";
@@ -70,15 +70,15 @@ export class BinaryTerminalWriter {
70
70
  throw new TypeError("Invalid binary tuple value");
71
71
  }
72
72
  header(
73
- frame: "seed" | "patch" | "tombstone",
73
+ frame: "seed" | "patch" | "tombstone" | "history",
74
74
  revision: number,
75
75
  base?: number,
76
76
  magic: "TCB1" | "TCP1" = "TCB1",
77
77
  ) {
78
78
  for (const b of magic === "TCP1" ? [84, 67, 80, 49] : [84, 67, 66, 49]) this.byte(b);
79
- this.byte(frame === "seed" ? 0 : frame === "patch" ? 1 : 2);
79
+ this.byte(frame === "seed" ? 0 : frame === "patch" ? 1 : frame === "tombstone" ? 2 : 3);
80
80
  this.uint(revision);
81
- if (frame !== "seed") this.uint(base!);
81
+ if (frame !== "seed" && frame !== "history") this.uint(base!);
82
82
  }
83
83
  get byteLength(): number {
84
84
  return this.length;
@@ -91,7 +91,7 @@ export function decodeBinaryTerminalTuple(
91
91
  bytes: Uint8Array,
92
92
  magic: "TCB1" | "TCP1" = "TCB1",
93
93
  ): {
94
- frame: "seed" | "patch" | "tombstone";
94
+ frame: "seed" | "patch" | "tombstone" | "history";
95
95
  revision: number;
96
96
  baseRevision?: number;
97
97
  value: unknown;
@@ -150,10 +150,11 @@ export function decodeBinaryTerminalTuple(
150
150
  throw new TypeError("Unknown binary value tag");
151
151
  };
152
152
  const kind = byte();
153
- if (kind > 2) throw new TypeError("Unknown binary frame");
154
- const frame = kind === 0 ? "seed" : kind === 1 ? "patch" : "tombstone";
153
+ // Frame 3 is a capability-122 history chunk: a revision and no base.
154
+ if (kind > 3) throw new TypeError("Unknown binary frame");
155
+ const frame = kind === 0 ? "seed" : kind === 1 ? "patch" : kind === 2 ? "tombstone" : "history";
155
156
  const revision = uint();
156
- const baseRevision = kind === 0 ? undefined : uint();
157
+ const baseRevision = kind === 0 || kind === 3 ? undefined : uint();
157
158
  const value = read();
158
159
  if (offset !== bytes.length) throw new TypeError("Trailing binary data");
159
160
  return { frame, revision, ...(baseRevision === undefined ? {} : { baseRevision }), value };
@@ -3,6 +3,8 @@ import {
3
3
  TERMINAL_GRAPHICS_COMPRESSED_LOCAL_CAPABILITY_VERSION,
4
4
  TERMINAL_PROJECTION_CAPABILITY_VERSION,
5
5
  TERMINAL_PROJECTION_LOCAL_CAPABILITY_VERSION,
6
+ TERMINAL_DELIVERY_PARTIAL_HISTORY_CAPABILITY_VERSION,
7
+ type TerminalDeliveryHistoryCommitment,
6
8
  } from "@tmux-ide/contracts";
7
9
  import {
8
10
  BinaryTerminalWriter,
@@ -27,6 +29,9 @@ import {
27
29
  TerminalDeliveryNegotiatedSchemaZ,
28
30
  TerminalDeliveryNackSchemaZ,
29
31
  TerminalSemanticDeliveryPayloadSchemaZ,
32
+ TerminalSemanticHistoryPayloadSchemaZ,
33
+ TERMINAL_DELIVERY_HISTORY_CHUNK_MAX_ROWS,
34
+ type TerminalSemanticHistoryPayload,
30
35
  TerminalReplicaSnapshotSchemaZ,
31
36
  TerminalReplicaRowSchemaZ,
32
37
  TerminalReplicaPatchPayloadSchemaZ,
@@ -48,6 +53,7 @@ import {
48
53
  } from "@tmux-ide/contracts";
49
54
  import {
50
55
  applyTerminalReplicaPatch,
56
+ freezeTerminalReplicaRow,
51
57
  freezeTerminalReplicaSnapshot,
52
58
  hashTerminalReplicaSnapshot,
53
59
  hashTerminalReplicaTombstone,
@@ -56,6 +62,10 @@ import {
56
62
  terminalReplicaRowsEqual,
57
63
  } from "./terminal-replica.ts";
58
64
  import { grantCompactReplicaCapability } from "./terminal-compact-capability.ts";
65
+ import {
66
+ hashPartialTerminalReplicaSnapshot,
67
+ prependVerifiedTerminalHistory,
68
+ } from "./terminal-partial-history.ts";
59
69
  import {
60
70
  createDecodedTerminalReplicaRowBuilder,
61
71
  hashTerminalReplicaRowCooperatively,
@@ -107,12 +117,18 @@ export function negotiateTerminalDelivery(
107
117
  encoding !== "semantic-binary-v1"
108
118
  )
109
119
  return { accepted: false, reason: "unsupported-capability-combination" };
120
+ // Capability 122 (history on demand) replaces the alternate-screen projection.
121
+ const partialHistory =
122
+ offer.protocolVersions.includes(TERMINAL_DELIVERY_PARTIAL_HISTORY_CAPABILITY_VERSION) &&
123
+ (encoding === "semantic-compact-v1" || encoding === "semantic-binary-v1");
110
124
  return {
111
125
  accepted: true,
112
126
  negotiated: {
113
127
  protocolVersion: TERMINAL_DELIVERY_PROTOCOL_VERSION,
114
128
  encoding,
129
+ ...(partialHistory ? { partialHistory: "tail-v1" as const } : {}),
115
130
  ...(binary &&
131
+ !partialHistory &&
116
132
  offer.protocolVersions.some(
117
133
  (v) =>
118
134
  v === TERMINAL_PROJECTION_CAPABILITY_VERSION ||
@@ -458,6 +474,92 @@ function compactSemanticWire(input: TerminalSemanticDeliveryPayload) {
458
474
  return wire;
459
475
  }
460
476
 
477
+ // History rows the daemon owns are strictly validated once, like patch rows,
478
+ // with the strict schema's own issues; foreign rows are validated every time.
479
+ const HistoryEncodingPayloadSchema = TerminalSemanticHistoryPayloadSchemaZ.extend({
480
+ rows: z.array(CompactPatchEncodingRowSchema).min(1).max(TERMINAL_DELIVERY_HISTORY_CHUNK_MAX_ROWS),
481
+ });
482
+
483
+ /** Capability 122: encode a "history" frame's rows in the negotiated semantic encoding. */
484
+ export function encodeSemanticTerminalHistory(
485
+ input: TerminalSemanticHistoryPayload,
486
+ encoding: "semantic-v1" | "semantic-compact-v1" | "semantic-binary-v1",
487
+ ): Uint8Array {
488
+ const payload = HistoryEncodingPayloadSchema.parse(input);
489
+ if (encoding === "semantic-v1") {
490
+ const bytes = UTF8_ENCODER.encode(canonicalJson(payload));
491
+ assertRepresentationSize(bytes);
492
+ return bytes;
493
+ }
494
+ const rows = compactRows(payload.rows, null, {
495
+ rows: 0,
496
+ runs: 0,
497
+ cells: 0,
498
+ placements: 0,
499
+ maxCells: COMPACT_MAX_EXPANDED_CELLS,
500
+ reusedRows: 0,
501
+ allocatedCells: 0,
502
+ canonicalUtf8Allocations: 0,
503
+ canonicalUtf8Bytes: 0,
504
+ validatedCellAllocations: 0,
505
+ });
506
+ if (encoding === "semantic-compact-v1") {
507
+ const bytes = UTF8_ENCODER.encode(
508
+ JSON.stringify({ f: "h", h: rows, k: COMPACT_SEMANTIC_KIND, r: payload.revision, v: 1 }),
509
+ );
510
+ assertRepresentationSize(bytes);
511
+ return bytes;
512
+ }
513
+ const writer = new BinaryTerminalWriter();
514
+ try {
515
+ writer.header("history", payload.revision);
516
+ writer.value(rows);
517
+ return writer.finish();
518
+ } catch (error) {
519
+ if (error instanceof BinaryTerminalLimitError)
520
+ throw new TerminalDeliveryStateTooLargeError(TERMINAL_DELIVERY_MAX_REPRESENTATION_BYTES + 1);
521
+ throw error;
522
+ }
523
+ }
524
+
525
+ /** Capability 122: decode a "history" frame's rows. */
526
+ export function decodeSemanticTerminalHistory(
527
+ bytes: Uint8Array,
528
+ encoding: "semantic-v1" | "semantic-compact-v1" | "semantic-binary-v1",
529
+ ): TerminalSemanticHistoryPayload {
530
+ if (encoding === "semantic-v1")
531
+ return TerminalSemanticHistoryPayloadSchemaZ.parse(
532
+ JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(bytes)),
533
+ );
534
+ const budget = { ...compactDecodeBudget(bytes.length), maxCells: COMPACT_MAX_EXPANDED_CELLS };
535
+ let revision: number;
536
+ let value: unknown;
537
+ if (encoding === "semantic-compact-v1") {
538
+ const wire = JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(bytes)) as unknown;
539
+ if (
540
+ typeof wire !== "object" ||
541
+ wire === null ||
542
+ Object.keys(wire).join(",") !== "f,h,k,r,v" ||
543
+ (wire as { f: unknown }).f !== "h" ||
544
+ (wire as { k: unknown }).k !== COMPACT_SEMANTIC_KIND ||
545
+ (wire as { v: unknown }).v !== 1
546
+ )
547
+ throw new TypeError("Invalid compact history payload");
548
+ revision = (wire as { r: number }).r;
549
+ value = (wire as { h: unknown }).h;
550
+ } else {
551
+ const decoded = decodeBinaryTerminalTuple(bytes);
552
+ if (decoded.frame !== "history") throw new TypeError("Invalid binary history payload");
553
+ revision = decoded.revision;
554
+ value = decoded.value;
555
+ }
556
+ return TerminalSemanticHistoryPayloadSchemaZ.parse({
557
+ frame: "history",
558
+ revision,
559
+ rows: expandRows(value, budget, null, TERMINAL_DELIVERY_HISTORY_CHUNK_MAX_ROWS, "history"),
560
+ });
561
+ }
562
+
461
563
  export function encodeBinarySemanticTerminalUpdate(
462
564
  input: TerminalSemanticDeliveryPayload,
463
565
  ): Uint8Array {
@@ -1627,6 +1729,8 @@ export function decodeVerifiedCompactSemanticTerminalUpdate(
1627
1729
  options?: {
1628
1730
  readonly grantReducerAdoption?: boolean;
1629
1731
  readonly onComplete?: (profile: CompactSemanticCommitProfile) => void;
1732
+ /** Capability 122: the snapshot holds only loaded history; hash with this commitment. */
1733
+ readonly historyUnloaded?: TerminalDeliveryHistoryCommitment;
1630
1734
  },
1631
1735
  ): Readonly<{
1632
1736
  payload: TerminalSemanticDeliveryPayload;
@@ -1646,7 +1750,9 @@ export function decodeVerifiedCompactSemanticTerminalUpdate(
1646
1750
  })()
1647
1751
  : null;
1648
1752
  const hash = snapshot
1649
- ? hashTerminalReplicaSnapshot(snapshot)
1753
+ ? options?.historyUnloaded
1754
+ ? hashPartialTerminalReplicaSnapshot(snapshot, options.historyUnloaded)
1755
+ : hashTerminalReplicaSnapshot(snapshot)
1650
1756
  : hashTerminalReplicaTombstone(
1651
1757
  payload.frame === "tombstone" ? payload.tombstone.reason : "protocol",
1652
1758
  );
@@ -3184,6 +3290,11 @@ export interface TerminalDeliveryClientState {
3184
3290
  readonly appliedRevision: number;
3185
3291
  readonly appliedHash: string | null;
3186
3292
  readonly canonicalSnapshot: TerminalReplicaSnapshot | null;
3293
+ /**
3294
+ * Capability 122: the history rows `canonicalSnapshot` does not hold, oldest
3295
+ * first. Null when the stream has no partial history.
3296
+ */
3297
+ readonly historyUnloaded: TerminalDeliveryHistoryCommitment | null;
3187
3298
  readonly inFlight: TerminalDeliveryEnvelope | null;
3188
3299
  readonly nextChunk: number;
3189
3300
  readonly receivedBytes: number;
@@ -3205,6 +3316,7 @@ export function createTerminalDeliveryClientState(
3205
3316
  appliedRevision: -1,
3206
3317
  appliedHash: null,
3207
3318
  canonicalSnapshot: null,
3319
+ historyUnloaded: null,
3208
3320
  inFlight: null,
3209
3321
  nextChunk: 0,
3210
3322
  receivedBytes: 0,
@@ -3231,6 +3343,21 @@ export function admitTerminalDeliveryEnvelope(
3231
3343
  state.semanticPaneId !== envelope.semanticPaneId
3232
3344
  )
3233
3345
  return failClient(state);
3346
+ // Capability 122: a partial history only on a stream that negotiated it, and
3347
+ // then on every seed, patch and history frame.
3348
+ if (
3349
+ (envelope.history === "partial") !==
3350
+ (state.negotiated.partialHistory === "tail-v1" && envelope.frame !== "tombstone")
3351
+ )
3352
+ return failClient(state);
3353
+ if (
3354
+ envelope.frame === "history" &&
3355
+ (state.historyUnloaded === null ||
3356
+ state.tombstoned ||
3357
+ envelope.canonicalRevision !== state.appliedRevision ||
3358
+ envelope.canonicalStateHash !== state.appliedHash)
3359
+ )
3360
+ return failClient(state);
3234
3361
  if (envelope.frame === "seed") {
3235
3362
  if (state.tombstoned && state.incarnation === envelope.incarnation) return failClient(state);
3236
3363
  if (
@@ -3389,7 +3516,47 @@ export function commitTerminalDelivery(
3389
3516
  throw new TypeError("Terminal delivery representation hash mismatch");
3390
3517
  let canonicalSnapshot = state.canonicalSnapshot;
3391
3518
  let semanticUpdate: TerminalSemanticDeliveryPayload | null = null;
3392
- if (
3519
+ const historyUnloaded = envelope.historyUnloaded ?? null;
3520
+ if (envelope.frame === "history") {
3521
+ // Capability 122: prepend proven rows; the canonical state is unchanged.
3522
+ if (
3523
+ !canonicalSnapshot ||
3524
+ !state.historyUnloaded ||
3525
+ !historyUnloaded ||
3526
+ envelope.encoding === "ansi-diff-v1" ||
3527
+ envelope.encoding === "ansi-raw-v1"
3528
+ )
3529
+ throw new TypeError("History frame without a partial history");
3530
+ const payload = decodeSemanticTerminalHistory(staged.bytes, envelope.encoding);
3531
+ if (payload.revision !== envelope.canonicalRevision)
3532
+ throw new TypeError("History frame revision mismatch");
3533
+ // The chunk chains to the old commitment, so the whole history's digest,
3534
+ // and with it the canonical hash, is the one this state was verified
3535
+ // against. Verify and freeze only the new rows: O(chunk), not O(loaded).
3536
+ if (envelope.canonicalStateHash !== state.appliedHash)
3537
+ throw new TypeError("Canonical state hash mismatch");
3538
+ const cols = canonicalSnapshot.cols;
3539
+ const added = payload.rows.map((row) => {
3540
+ if (row.cells.length !== cols) throw new TypeError("Malformed terminal replica row");
3541
+ return freezeTerminalReplicaRow(row);
3542
+ });
3543
+ const prepended = prependVerifiedTerminalHistory(
3544
+ canonicalSnapshot,
3545
+ state.historyUnloaded,
3546
+ added,
3547
+ historyUnloaded,
3548
+ );
3549
+ canonicalSnapshot = Object.freeze({
3550
+ ...prepended,
3551
+ history: Object.freeze(prepended.history),
3552
+ }) as TerminalReplicaSnapshot;
3553
+ } else if (
3554
+ historyUnloaded &&
3555
+ envelope.frame === "patch" &&
3556
+ (!state.historyUnloaded || historyUnloaded.rows > state.historyUnloaded.rows)
3557
+ ) {
3558
+ throw new TypeError("A patch cannot add unloaded history rows");
3559
+ } else if (
3393
3560
  envelope.encoding === "semantic-v1" ||
3394
3561
  envelope.encoding === "semantic-compact-v1" ||
3395
3562
  envelope.encoding === "semantic-binary-v1"
@@ -3402,6 +3569,7 @@ export function commitTerminalDelivery(
3402
3569
  staged.bytes,
3403
3570
  canonicalSnapshot,
3404
3571
  envelope.canonicalStateHash,
3572
+ historyUnloaded ? { historyUnloaded } : undefined,
3405
3573
  ).payload
3406
3574
  : decodeSemanticTerminalUpdate(staged.bytes);
3407
3575
  semanticUpdate = payload;
@@ -3427,7 +3595,9 @@ export function commitTerminalDelivery(
3427
3595
  canonicalSnapshot = applyTerminalReplicaPatch(canonicalSnapshot, payload.patch);
3428
3596
  } else canonicalSnapshot = null;
3429
3597
  const hash = canonicalSnapshot
3430
- ? hashTerminalReplicaSnapshot(canonicalSnapshot)
3598
+ ? historyUnloaded
3599
+ ? hashPartialTerminalReplicaSnapshot(canonicalSnapshot, historyUnloaded)
3600
+ : hashTerminalReplicaSnapshot(canonicalSnapshot)
3431
3601
  : hashTerminalReplicaTombstone(
3432
3602
  payload.frame === "tombstone" ? payload.tombstone.reason : "protocol",
3433
3603
  );
@@ -3445,6 +3615,7 @@ export function commitTerminalDelivery(
3445
3615
  appliedRevision: envelope.canonicalRevision,
3446
3616
  appliedHash: envelope.canonicalStateHash,
3447
3617
  canonicalSnapshot,
3618
+ historyUnloaded: envelope.frame === "tombstone" ? null : historyUnloaded,
3448
3619
  tombstoned: envelope.frame === "tombstone",
3449
3620
  inFlight: null,
3450
3621
  nextChunk: 0,