tmux-ide 2.9.26 → 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.
Files changed (47) hide show
  1. package/README.md +1 -1
  2. package/bin/cli.js +1252 -528
  3. package/package.json +2 -2
  4. package/packages/contracts/src/pane-stream.ts +9 -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/server.js +3 -0
  13. package/packages/daemon/dist/command-center/tmux-server-attach.js +51 -0
  14. package/packages/daemon/dist/command-center/tmux-servers.js +78 -0
  15. package/packages/daemon/dist/doctor.js +4 -4
  16. package/packages/daemon/dist/lib/app-config.js +7 -0
  17. package/packages/daemon/dist/lib/node-requirement.js +20 -0
  18. package/packages/daemon/dist/lib/require-supported-node.js +8 -0
  19. package/packages/daemon/dist/lib/tmux-server-session-open.js +38 -12
  20. package/packages/daemon/dist/lib/workspace-promotion.js +4 -4
  21. package/packages/daemon/dist/native/TmuxIdeNotifier.app/Contents/Info.plist +2 -2
  22. package/packages/daemon/dist/native/TmuxIdeNotifier.app/Contents/MacOS/tmux-ide-notifier +0 -0
  23. package/packages/daemon/dist/native/TmuxIdeNotifier.app/Contents/Resources/Assets.car +0 -0
  24. package/packages/daemon/dist/native/TmuxIdeNotifier.app/Contents/_CodeSignature/CodeResources +2 -2
  25. package/packages/daemon/dist/server/pane-stream-upgrade.js +39 -1
  26. package/packages/daemon/dist/terminal/pane-stream/pane-stream-websocket.js +14 -0
  27. package/packages/daemon/dist/terminal/session-runtime/registry.js +1 -0
  28. package/packages/daemon/dist/terminal/session-runtime/terminal-delivery-hub.js +465 -42
  29. package/packages/daemon/dist/tui/mirror/open-tui-workspace-runtime-port.js +76 -2
  30. package/packages/daemon/dist/tui/mirror/theme.js +8 -1
  31. package/packages/daemon/src/command-center/attach-window-rule.cases.json +84 -0
  32. package/packages/daemon/src/command-center/owner-keep-alive.ts +38 -0
  33. package/packages/daemon/src/command-center/server.ts +3 -0
  34. package/packages/daemon/src/command-center/tmux-server-attach.ts +75 -0
  35. package/packages/daemon/src/command-center/tmux-servers.ts +84 -0
  36. package/packages/daemon/src/doctor.ts +4 -4
  37. package/packages/daemon/src/lib/app-config.ts +15 -0
  38. package/packages/daemon/src/lib/node-requirement.ts +22 -0
  39. package/packages/daemon/src/lib/require-supported-node.ts +9 -0
  40. package/packages/daemon/src/lib/tmux-server-session-open.ts +53 -15
  41. package/packages/daemon/src/lib/workspace-promotion.ts +4 -4
  42. package/packages/daemon/src/server/pane-stream-upgrade.ts +41 -1
  43. package/packages/daemon/src/terminal/pane-stream/pane-stream-websocket.ts +25 -0
  44. package/packages/daemon/src/terminal/session-runtime/registry.ts +1 -0
  45. package/packages/daemon/src/terminal/session-runtime/terminal-delivery-hub.ts +575 -39
  46. package/packages/daemon/src/tui/mirror/open-tui-workspace-runtime-port.ts +82 -2
  47. package/packages/daemon/src/tui/mirror/theme.ts +13 -1
@@ -8,7 +8,13 @@ import { WorkspaceCatalogLiveSessionIdSchemaZ } from "@tmux-ide/contracts";
8
8
  import { liveSessionIdForNativeIdentity } from "../terminal/protocol/live-session-identity.ts";
9
9
  import { fleetSessionIdForName } from "../command-center/resources/fleet-catalog.ts";
10
10
  import { isVisibleFleetSession } from "../command-center/discovery.ts";
11
- import { WorkspacePromotionAuthority } from "./workspace-promotion.ts";
11
+ import {
12
+ ADOPTED_OPTION,
13
+ hasValidPaneStamp,
14
+ SEMANTIC_PANE_OPTION,
15
+ SEMANTIC_WINDOW_OPTION,
16
+ WorkspacePromotionAuthority,
17
+ } from "./workspace-promotion.ts";
12
18
  import type { WorkspaceRegistry } from "./workspace-registry.ts";
13
19
  import { shellEscape } from "./shell.ts";
14
20
 
@@ -28,30 +34,62 @@ export function createNativeTmuxSessionOpener(options: {
28
34
  };
29
35
  const open = async (liveSessionId: string, observed?: ObservedAgent) => {
30
36
  assertOpen();
37
+ // One row per pane: the session's identity, whether it is adopted, and
38
+ // each window's and pane's stamp. tmux rejects tabs in session names, so
39
+ // the name is the last field; a pane stamp with a tab is invalid anyway.
31
40
  const records = await options.run([
32
- "list-sessions",
41
+ "list-panes",
42
+ "-a",
33
43
  "-F",
34
- "#{pid}\t#{session_id}\t#{session_created}\t#{session_name}",
44
+ `#{pid}\t#{session_id}\t#{session_created}\t#{${ADOPTED_OPTION}}\t#{${SEMANTIC_WINDOW_OPTION}}\t#{${SEMANTIC_PANE_OPTION}}\t#{session_name}`,
35
45
  ]);
36
- // The final field is an exact session identity; spaces are valid name bytes.
37
- const matches = records
46
+ const rows = records
38
47
  .replace(/\n+$/u, "")
39
48
  .split("\n")
40
49
  .map((line) => line.split("\t"))
50
+ .filter((fields) => fields.length >= 7)
51
+ .map((fields) => ({
52
+ // A tab can only come from a window or pane stamp: such a row never
53
+ // takes the fast path below.
54
+ exact: fields.length === 7,
55
+ pid: fields[0]!,
56
+ id: fields[1]!,
57
+ created: fields[2]!,
58
+ adopted: fields[3]!,
59
+ window: fields.slice(4, -2).join("\t"),
60
+ pane: fields.at(-2)!,
61
+ name: fields.at(-1)!,
62
+ }))
41
63
  .filter(
42
- ([pid, id, created, name]) =>
43
- pid &&
64
+ ({ pid, id, created, name }) =>
44
65
  /^\d+$/.test(pid) &&
45
- id &&
46
66
  /^\$\d+$/.test(id) &&
47
- created &&
48
67
  /^\d+$/.test(created) &&
49
68
  name &&
50
69
  isVisibleFleetSession(name) &&
51
70
  liveSessionIdForNativeIdentity(pid, id, created) === liveSessionId,
52
71
  );
53
- if (matches.length !== 1) throw new Error("Selected live tmux session is no longer available");
54
- const [, nativeSessionId, created, name] = matches[0]!;
72
+ if (new Set(rows.map((row) => row.id)).size !== 1)
73
+ throw new Error("Selected live tmux session is no longer available");
74
+ const { id: nativeSessionId, created, name } = rows[0]!;
75
+ // Re-opening a session that an earlier open already finished changes
76
+ // nothing: it is adopted, has exactly one durable alias, and every window
77
+ // and pane already carries a stamp. Promotion would only read it again
78
+ // (seven tmux commands), so skip it. Anything else promotes as before,
79
+ // which also stamps panes split since the last open.
80
+ const aliases = options.registry.list().filter((entry) => entry.sessionName === name);
81
+ if (
82
+ !observed &&
83
+ aliases.length === 1 &&
84
+ !options.registry.isVolatile(aliases[0]!.name) &&
85
+ rows.every(
86
+ (row) =>
87
+ row.exact && row.adopted === "1" && row.window.length > 0 && hasValidPaneStamp(row.pane),
88
+ )
89
+ ) {
90
+ assertOpen();
91
+ return { workspaceName: aliases[0]!.name, liveSessionId };
92
+ }
55
93
  // Each command checks the exact native session on the server accepting it.
56
94
  // A name-only validation followed by stamping would cross a session replacement.
57
95
  const run = async (args: readonly string[]) => {
@@ -62,7 +100,7 @@ export function createNativeTmuxSessionOpener(options: {
62
100
  const output = await options.run([
63
101
  "if-shell",
64
102
  "-t",
65
- nativeSessionId!,
103
+ nativeSessionId,
66
104
  "-F",
67
105
  condition,
68
106
  command,
@@ -74,7 +112,7 @@ export function createNativeTmuxSessionOpener(options: {
74
112
  return output;
75
113
  };
76
114
  const agent = observed
77
- ? await captureAgentAdmission(run, { id: nativeSessionId!, name: name! }, observed)
115
+ ? await captureAgentAdmission(run, { id: nativeSessionId, name }, observed)
78
116
  : null;
79
117
  await agent?.verify(false);
80
118
  const promotion = new WorkspacePromotionAuthority({
@@ -86,9 +124,9 @@ export function createNativeTmuxSessionOpener(options: {
86
124
  const result = await promotion.promote({
87
125
  operationId: randomUUID(),
88
126
  expectedDaemonInstanceId: options.generation,
89
- intent: { sessionId: fleetSessionIdForName(name!) },
127
+ intent: { sessionId: fleetSessionIdForName(name) },
90
128
  });
91
- await run(["display-message", "-p", "-t", nativeSessionId!, "#{session_id}"]);
129
+ await run(["display-message", "-p", "-t", nativeSessionId, "#{session_id}"]);
92
130
  const semanticPaneId = await agent?.verify(true);
93
131
  return {
94
132
  workspaceName: result.resource.workspaceName,
@@ -69,12 +69,12 @@ const MAX_TMUX_OUTPUT_BYTES = 128 * 1024;
69
69
  // is proven, immediately before registry admission. The promotion marker is
70
70
  // DELIBERATELY distinct from the m32 open marker so a promoted session is a
71
71
  // separate provenance.
72
- const ADOPTED_OPTION = "@tmux_ide_adopted";
72
+ export const ADOPTED_OPTION = "@tmux_ide_adopted";
73
73
  const SESSION_PROMOTED_MARKER_OPTION = "@tmux_ide_workspace_promoted_v1";
74
74
  const SESSION_WORKSPACE_OPTION = "@tmux_ide_workspace_name";
75
75
  const SESSION_OPERATION_OPTION = "@tmux_ide_workspace_promote_operation";
76
- const SEMANTIC_PANE_OPTION = "@tmux_ide_pane_id";
77
- const SEMANTIC_WINDOW_OPTION = "@tmux_ide_window_id";
76
+ export const SEMANTIC_PANE_OPTION = "@tmux_ide_pane_id";
77
+ export const SEMANTIC_WINDOW_OPTION = "@tmux_ide_window_id";
78
78
 
79
79
  // Distinctive multi-char field/line delimiters — collision-resistant against
80
80
  // arbitrary pane paths, window names and user-controlled `@agent_*` values,
@@ -301,7 +301,7 @@ function isInternalSession(name: string): boolean {
301
301
  const VALID_SEMANTIC_PANE_ID = /^[A-Za-z0-9][A-Za-z0-9._-]*$/u;
302
302
  const RESERVED_DISCOVERED_PREFIX = "terminal.discovered.";
303
303
 
304
- function hasValidPaneStamp(value: string): boolean {
304
+ export function hasValidPaneStamp(value: string): boolean {
305
305
  return (
306
306
  value.length > 0 &&
307
307
  value.length <= 128 &&
@@ -28,6 +28,35 @@ function rawHeaderValues(
28
28
  return values;
29
29
  }
30
30
 
31
+ /**
32
+ * attach-rtt: the client may carry its redeem frame in the upgrade request
33
+ * (base64url of the frame's JSON text), so the daemon answers the 101 and the
34
+ * seed without waiting a round trip for the frame. The 101 says so with
35
+ * `X-Tmux-Ide-Redeem: accepted`; a client that sees no such header sends the
36
+ * frame as before. Absent, the upgrade is exactly as it was.
37
+ */
38
+ export const PANE_STREAM_REDEEM_HEADER = "x-tmux-ide-redeem";
39
+
40
+ /** The frame a redeem header carries, `null` for none, or "invalid". */
41
+ function upgradeRedeemFrame(
42
+ request: import("node:http").IncomingMessage,
43
+ ): Buffer | null | "invalid" {
44
+ const values = rawHeaderValues(request, PANE_STREAM_REDEEM_HEADER);
45
+ if (values.length === 0) return null;
46
+ const value = values[0]!;
47
+ if (
48
+ values.length > 1 ||
49
+ value.length === 0 ||
50
+ value.length > Math.ceil((PANE_STREAM_MAX_REDEMPTION_BYTES * 4) / 3) ||
51
+ !/^[A-Za-z0-9_-]+$/u.test(value)
52
+ )
53
+ return "invalid";
54
+ const frame = Buffer.from(value, "base64url");
55
+ return frame.byteLength === 0 || frame.byteLength > PANE_STREAM_MAX_REDEMPTION_BYTES
56
+ ? "invalid"
57
+ : frame;
58
+ }
59
+
31
60
  function rejectUpgrade(socket: Socket, status: number): void {
32
61
  const phrase =
33
62
  status === 403
@@ -77,7 +106,11 @@ export function attachPaneStreamWebSocket(
77
106
  });
78
107
 
79
108
  // Old clients ignore the header and answer the pings by protocol.
80
- wss.on("headers", (headers) => heartbeat.advertise(headers));
109
+ const redeemed = new WeakSet<import("node:http").IncomingMessage>();
110
+ wss.on("headers", (headers, request) => {
111
+ heartbeat.advertise(headers);
112
+ if (redeemed.has(request)) headers.push("X-Tmux-Ide-Redeem: accepted");
113
+ });
81
114
 
82
115
  const upgrade = (
83
116
  request: import("node:http").IncomingMessage,
@@ -123,6 +156,11 @@ export function attachPaneStreamWebSocket(
123
156
  rejectUpgrade(socket, 403);
124
157
  return;
125
158
  }
159
+ const redeemFrame = upgradeRedeemFrame(request);
160
+ if (redeemFrame === "invalid") {
161
+ rejectUpgrade(socket, 403);
162
+ return;
163
+ }
126
164
  if (!isCurrent()) {
127
165
  rejectUpgrade(socket, 410);
128
166
  return;
@@ -141,12 +179,14 @@ export function attachPaneStreamWebSocket(
141
179
  const cancelUnbound = (): void => decision.admission.cancelBeforeBind();
142
180
  socket.once("close", cancelUnbound);
143
181
  socket.once("error", cancelUnbound);
182
+ if (redeemFrame) redeemed.add(request);
144
183
  try {
145
184
  wss.handleUpgrade(request, socket, head, (ws) => {
146
185
  socket.off("close", cancelUnbound);
147
186
  socket.off("error", cancelUnbound);
148
187
  heartbeat.watch(ws, `pane-stream ${redemptionPath}`);
149
188
  decision.admission.bind(ws as unknown as DirectTerminalSocket);
189
+ if (redeemFrame) decision.admission.redeemFromUpgrade(redeemFrame);
150
190
  });
151
191
  } catch {
152
192
  socket.off("close", cancelUnbound);
@@ -17,6 +17,7 @@ import {
17
17
  TERMINAL_PROJECTION_LOCAL_CAPABILITY_VERSION,
18
18
  type TerminalProjectionReceipt,
19
19
  type TerminalProjectionMaterialize,
20
+ type TerminalDeliveryHistoryRequest,
20
21
  } from "@tmux-ide/contracts";
21
22
  import type { LocalImageOwner } from "../graphics/local-image-owner.ts";
22
23
  import {
@@ -314,6 +315,12 @@ export type PaneStreamUpgradeDecision =
314
315
  export interface PaneStreamPreAuthAdmission {
315
316
  bind(socket: DirectTerminalSocket): void;
316
317
  cancelBeforeBind(): void;
318
+ /**
319
+ * Redeem with the frame the upgrade request carried (attach-rtt), exactly
320
+ * as if it were the socket's first message. A frame sent afterwards is a
321
+ * second redemption and closes the socket.
322
+ */
323
+ redeemFromUpgrade(frame: Buffer): void;
317
324
  }
318
325
 
319
326
  export interface PaneStreamAdmissionSnapshot {
@@ -422,6 +429,8 @@ export interface SessionRuntimeTerminalDeliveryConnection {
422
429
  ack(ack: TerminalDeliveryAck): void;
423
430
  projectionReceipt?(receipt: TerminalProjectionReceipt): void;
424
431
  materialize?(request: TerminalProjectionMaterialize): void;
432
+ /** Capability 122: older history rows for a client holding a partial history. */
433
+ historyRequest?(request: TerminalDeliveryHistoryRequest): void;
425
434
  nack(nack: TerminalDeliveryNack): void;
426
435
  setVisibility(visibility: TerminalDeliveryVisibility): void;
427
436
  /** The client typed into this pane: send its deliveries first. */
@@ -1079,6 +1088,10 @@ class PreAuthAdmission implements PaneStreamPreAuthAdmission {
1079
1088
  this.close(1008, "upgrade-rejected");
1080
1089
  }
1081
1090
 
1091
+ redeemFromUpgrade(frame: Buffer): void {
1092
+ this.#onMessage(frame, false);
1093
+ }
1094
+
1082
1095
  promote(): void {
1083
1096
  if (!this.#open) return;
1084
1097
  this.#promoted = true;
@@ -3370,6 +3383,18 @@ export class PaneStreamLiveConnection {
3370
3383
  channel.delivery.projectionReceipt(frame.receipt);
3371
3384
  return;
3372
3385
  }
3386
+ if (frame.type === "terminal-delivery-history-request") {
3387
+ const channel = this.#deliveryChannel(frame.request);
3388
+ if (!channel) return;
3389
+ if (
3390
+ !channel.delivery?.historyRequest ||
3391
+ !channel.delivery.negotiation.accepted ||
3392
+ channel.delivery.negotiation.negotiated.partialHistory !== "tail-v1"
3393
+ )
3394
+ return this.#failProtocol("protocol-error");
3395
+ channel.delivery.historyRequest(frame.request);
3396
+ return;
3397
+ }
3373
3398
  if (frame.type === "terminal-projection-materialize") {
3374
3399
  const channel = this.#deliveryChannel(frame.request);
3375
3400
  if (!channel) return;
@@ -2530,6 +2530,7 @@ class SessionRuntimeConsumerImpl implements SessionRuntimeConsumer {
2530
2530
  ack: (ack: TerminalDeliveryAck) => upstream.ack(ack),
2531
2531
  ...(upstream.projectionReceipt ? { projectionReceipt: upstream.projectionReceipt } : {}),
2532
2532
  ...(upstream.materialize ? { materialize: upstream.materialize } : {}),
2533
+ ...(upstream.historyRequest ? { historyRequest: upstream.historyRequest } : {}),
2533
2534
  ...(upstream.prioritize ? { prioritize: upstream.prioritize } : {}),
2534
2535
  nack: (nack: TerminalDeliveryNack) => upstream.nack(nack),
2535
2536
  setVisibility: (visibility: TerminalDeliveryVisibility) => upstream.setVisibility(visibility),