@celljs/ta-exec 4.6.0 → 4.7.0

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 (135) hide show
  1. package/README.en.md +43 -0
  2. package/README.md +5 -1
  3. package/esm/node/exec-client.d.ts +28 -11
  4. package/esm/node/exec-client.js +89 -33
  5. package/esm/node/exec-client.js.map +1 -1
  6. package/esm/node/exec-peer.d.ts +40 -0
  7. package/esm/node/exec-peer.js +167 -0
  8. package/esm/node/exec-peer.js.map +1 -0
  9. package/esm/node/exec-protocol.d.ts +14 -0
  10. package/esm/node/exec-remote-source.d.ts +5 -0
  11. package/esm/node/exec-remote-source.js +9 -0
  12. package/esm/node/exec-remote-source.js.map +1 -0
  13. package/esm/node/exec-rpc.d.ts +21 -3
  14. package/esm/node/exec-rpc.js +14 -3
  15. package/esm/node/exec-rpc.js.map +1 -1
  16. package/esm/node/exec-server.d.ts +2 -26
  17. package/esm/node/exec-server.js +3 -132
  18. package/esm/node/exec-server.js.map +1 -1
  19. package/esm/node/exec-stdio.d.ts +27 -0
  20. package/esm/node/exec-stdio.js +60 -0
  21. package/esm/node/exec-stdio.js.map +1 -0
  22. package/esm/node/index.d.ts +8 -0
  23. package/esm/node/index.js +8 -0
  24. package/esm/node/index.js.map +1 -1
  25. package/esm/node/local-exec-environment.js +8 -8
  26. package/esm/node/local-exec-environment.js.map +1 -1
  27. package/esm/node/remote-exec-environment.d.ts +2 -1
  28. package/esm/node/remote-exec-environment.js +39 -0
  29. package/esm/node/remote-exec-environment.js.map +1 -1
  30. package/esm/node/ssh/node-dist.d.ts +20 -0
  31. package/esm/node/ssh/node-dist.js +77 -0
  32. package/esm/node/ssh/node-dist.js.map +1 -0
  33. package/esm/node/ssh/ssh-askpass.d.ts +24 -0
  34. package/esm/node/ssh/ssh-askpass.js +112 -0
  35. package/esm/node/ssh/ssh-askpass.js.map +1 -0
  36. package/esm/node/ssh/ssh-config.d.ts +20 -0
  37. package/esm/node/ssh/ssh-config.js +127 -0
  38. package/esm/node/ssh/ssh-config.js.map +1 -0
  39. package/esm/node/ssh/ssh-host.d.ts +73 -0
  40. package/esm/node/ssh/ssh-host.js +258 -0
  41. package/esm/node/ssh/ssh-host.js.map +1 -0
  42. package/esm/node/ssh/ssh-util.d.ts +45 -0
  43. package/esm/node/ssh/ssh-util.js +110 -0
  44. package/esm/node/ssh/ssh-util.js.map +1 -0
  45. package/lib/node/exec-client.d.ts +28 -11
  46. package/lib/node/exec-client.d.ts.map +1 -1
  47. package/lib/node/exec-client.js +89 -33
  48. package/lib/node/exec-client.js.map +1 -1
  49. package/lib/node/exec-peer.d.ts +41 -0
  50. package/lib/node/exec-peer.d.ts.map +1 -0
  51. package/lib/node/exec-peer.js +171 -0
  52. package/lib/node/exec-peer.js.map +1 -0
  53. package/lib/node/exec-protocol.d.ts +14 -0
  54. package/lib/node/exec-protocol.d.ts.map +1 -1
  55. package/lib/node/exec-remote-source.d.ts +6 -0
  56. package/lib/node/exec-remote-source.d.ts.map +1 -0
  57. package/lib/node/exec-remote-source.js +10 -0
  58. package/lib/node/exec-remote-source.js.map +1 -0
  59. package/lib/node/exec-rpc.d.ts +21 -3
  60. package/lib/node/exec-rpc.d.ts.map +1 -1
  61. package/lib/node/exec-rpc.js +14 -3
  62. package/lib/node/exec-rpc.js.map +1 -1
  63. package/lib/node/exec-server.d.ts +2 -26
  64. package/lib/node/exec-server.d.ts.map +1 -1
  65. package/lib/node/exec-server.js +4 -134
  66. package/lib/node/exec-server.js.map +1 -1
  67. package/lib/node/exec-server.spec.js +1 -1
  68. package/lib/node/exec-server.spec.js.map +1 -1
  69. package/lib/node/exec-stdio.d.ts +28 -0
  70. package/lib/node/exec-stdio.d.ts.map +1 -0
  71. package/lib/node/exec-stdio.js +64 -0
  72. package/lib/node/exec-stdio.js.map +1 -0
  73. package/lib/node/exec-stdio.spec.d.ts +2 -0
  74. package/lib/node/exec-stdio.spec.d.ts.map +1 -0
  75. package/lib/node/exec-stdio.spec.js +105 -0
  76. package/lib/node/exec-stdio.spec.js.map +1 -0
  77. package/lib/node/index.d.ts +8 -0
  78. package/lib/node/index.d.ts.map +1 -1
  79. package/lib/node/index.js +8 -0
  80. package/lib/node/index.js.map +1 -1
  81. package/lib/node/local-exec-environment.d.ts.map +1 -1
  82. package/lib/node/local-exec-environment.js +8 -8
  83. package/lib/node/local-exec-environment.js.map +1 -1
  84. package/lib/node/remote-exec-environment.d.ts +2 -1
  85. package/lib/node/remote-exec-environment.d.ts.map +1 -1
  86. package/lib/node/remote-exec-environment.js +39 -0
  87. package/lib/node/remote-exec-environment.js.map +1 -1
  88. package/lib/node/ssh/node-dist.d.ts +21 -0
  89. package/lib/node/ssh/node-dist.d.ts.map +1 -0
  90. package/lib/node/ssh/node-dist.js +80 -0
  91. package/lib/node/ssh/node-dist.js.map +1 -0
  92. package/lib/node/ssh/ssh-askpass.d.ts +25 -0
  93. package/lib/node/ssh/ssh-askpass.d.ts.map +1 -0
  94. package/lib/node/ssh/ssh-askpass.js +114 -0
  95. package/lib/node/ssh/ssh-askpass.js.map +1 -0
  96. package/lib/node/ssh/ssh-config.d.ts +21 -0
  97. package/lib/node/ssh/ssh-config.d.ts.map +1 -0
  98. package/lib/node/ssh/ssh-config.js +130 -0
  99. package/lib/node/ssh/ssh-config.js.map +1 -0
  100. package/lib/node/ssh/ssh-host.d.ts +74 -0
  101. package/lib/node/ssh/ssh-host.d.ts.map +1 -0
  102. package/lib/node/ssh/ssh-host.js +262 -0
  103. package/lib/node/ssh/ssh-host.js.map +1 -0
  104. package/lib/node/ssh/ssh-host.spec.d.ts +2 -0
  105. package/lib/node/ssh/ssh-host.spec.d.ts.map +1 -0
  106. package/lib/node/ssh/ssh-host.spec.js +182 -0
  107. package/lib/node/ssh/ssh-host.spec.js.map +1 -0
  108. package/lib/node/ssh/ssh-util.d.ts +46 -0
  109. package/lib/node/ssh/ssh-util.d.ts.map +1 -0
  110. package/lib/node/ssh/ssh-util.js +113 -0
  111. package/lib/node/ssh/ssh-util.js.map +1 -0
  112. package/lib/node/ssh/ssh-util.spec.d.ts +2 -0
  113. package/lib/node/ssh/ssh-util.spec.d.ts.map +1 -0
  114. package/lib/node/ssh/ssh-util.spec.js +87 -0
  115. package/lib/node/ssh/ssh-util.spec.js.map +1 -0
  116. package/package.json +3 -3
  117. package/src/node/exec-client.ts +108 -39
  118. package/src/node/exec-peer.ts +186 -0
  119. package/src/node/exec-protocol.ts +15 -0
  120. package/src/node/exec-remote-source.ts +7 -0
  121. package/src/node/exec-rpc.ts +23 -3
  122. package/src/node/exec-server.spec.ts +1 -1
  123. package/src/node/exec-server.ts +4 -140
  124. package/src/node/exec-stdio.spec.ts +107 -0
  125. package/src/node/exec-stdio.ts +75 -0
  126. package/src/node/index.ts +8 -0
  127. package/src/node/local-exec-environment.ts +7 -6
  128. package/src/node/remote-exec-environment.ts +39 -1
  129. package/src/node/ssh/node-dist.ts +83 -0
  130. package/src/node/ssh/ssh-askpass.ts +117 -0
  131. package/src/node/ssh/ssh-config.ts +128 -0
  132. package/src/node/ssh/ssh-host.spec.ts +189 -0
  133. package/src/node/ssh/ssh-host.ts +300 -0
  134. package/src/node/ssh/ssh-util.spec.ts +97 -0
  135. package/src/node/ssh/ssh-util.ts +127 -0
package/README.en.md ADDED
@@ -0,0 +1,43 @@
1
+ # Cell - Ta Exec
2
+
3
+ Where the agent runs commands and reads/writes files—similar to Codex's exec-server: the agent loop stays in the host, and command and file operations go to the execution environment. Independent of pi.
4
+
5
+ ```ts
6
+ interface ExecEnvironment {
7
+ readonly cwd: string; // path in the execution environment
8
+ readonly local: boolean; // whether it is this machine
9
+ exec(command, options?): Promise<ExecResult>; // one-off execution; streams output; kills the process group on timeout/abort
10
+ spawn(command, args, options?): ExecProcess; // long-lived process; stdio communication (MCP, LSP)
11
+ readFile / writeFile / stat / readdir / mkdir / remove
12
+ connect?(port, host?): Promise<ExecSocket>; // TCP connection on the execution environment's machine (only for non-local environments); used for port forwarding
13
+ }
14
+ ```
15
+
16
+ All paths are paths in the execution environment. Never pass them to `node:fs`, and give the model only these paths.
17
+
18
+ ## Implementation
19
+
20
+ | | |
21
+ | --- | --- |
22
+ | `LocalExecEnvironment` | This machine |
23
+ | `RemoteExecEnvironment` + `ExecClient` | `ta exec-server` on another machine (container, sandbox) |
24
+ | `ExecServer` | `ta exec-server` itself: an HTTP service that doesn't load Cell (~60 MB when empty) |
25
+ | `ExecStdio` | The same server runs on stdin/stdout (one message per line), serving only one host. Ends when input closes or 60 seconds pass without messages (host pings every 15 seconds), terminating everything it started. |
26
+ | `SshHost` | Connects to another machine via system `ssh`: uses its own ControlMaster for one login, other commands share it. Ssh prompts for the user go through `SSH_ASKPASS` to `ask`. Detects the host; if it lacks Node.js 18+, downloads it locally (verified by SHASUMS256) and installs to `~/.ta/server/node` over ssh. Places `EXEC_REMOTE_SOURCE` (the `ExecStdio` bundled in `scripts/remote-server.ts`, ~7 KB, named by hash) in `~/.ta/server/<hash>/` and starts it in the user's login shell; `client` is its `ExecClient`. |
27
+ | `SshConfig` | Hosts in `~/.ssh/config` (including `Include`) that don't use wildcards. |
28
+
29
+ ```bash
30
+ # execution machine (container)
31
+ TA_EXEC_TOKEN=... ta exec-server --host 0.0.0.0 --port 4200 --cwd /workspace
32
+ # host
33
+ TA_EXEC_TOKEN=... ta serve --exec ws://exec-host:4200/exec
34
+ ```
35
+
36
+ Protocol (`ExecRpc`) mirrors Codex exec-server: JSON-RPC 2.0 over WebSocket (or stdin/stdout, one message per line). Methods include `process/exec`, `process/spawn`, `process/write`, `process/kill`, `fs/readFile`, `fs/writeFile`, `fs/stat`, `fs/readdir`, `fs/mkdir`, `fs/remove`, `net/connect`, `net/write`, `net/close`. Process output is pushed as `process/output`, `process/stderr`, `process/exit` notifications; connection data is pushed as `net/data`, `net/closed` notifications. Bytes are base64-encoded. `ExecClient` doesn't care about transport: `ExecClient.webSocket(url, token)` or an `SshHost` provides the connection.
37
+
38
+ - **Auth**: Token (via `Authorization: Bearer` or `?token=`). The security boundary is the execution machine itself (like pi: no approval before each tool call, just OS/container isolation).
39
+ - **Environment variables**: Only new or changed variables on top of the host's environment are sent (`RemoteExecEnvironment.delta`); host variables (API keys, etc.) never reach the execution machine.
40
+ - **Epoch isolation**: The host connects with an epoch (default: host startup time). If a larger epoch connects, the execution machine closes connections and kills processes from smaller epochs; their subsequent calls fail with `CELL_TA_EXEC_STALE_EPOCH`. After a session moves to a new host instance, the old one cannot run tools.
41
+ - **Process lifecycle**: Processes belong to their connection. Disconnecting ends processes started by that connection; `ExecClient` reconnects on the next call.
42
+
43
+ Host-side pi built-in tools (read, bash, edit, write, ls, find, and `!` commands) and stdio MCP servers all run through the execution environment (see `ExecExtension` and `ExecMcpTransport` in @celljs/ta-harness). The ripgrep version of grep works only locally—remote sessions drop it, and the model uses bash `rg` instead.
package/README.md CHANGED
@@ -9,6 +9,7 @@ interface ExecEnvironment {
9
9
  exec(command, options?): Promise<ExecResult>; // 一次性执行,流式输出,超时/中止时杀掉整个进程组
10
10
  spawn(command, args, options?): ExecProcess; // 长驻进程,stdin/stdout 通信(MCP、LSP)
11
11
  readFile / writeFile / stat / readdir / mkdir / remove
12
+ connect?(port, host?): Promise<ExecSocket>; // 环境所在机器上的 TCP 连接(只有非本机环境有),用于端口转发
12
13
  }
13
14
  ```
14
15
 
@@ -21,6 +22,9 @@ interface ExecEnvironment {
21
22
  | `LocalExecEnvironment` | 本机 |
22
23
  | `RemoteExecEnvironment` + `ExecClient` | 另一台机器(容器、沙箱)上的 `ta exec-server` |
23
24
  | `ExecServer` | `ta exec-server` 本身:一个不加载 Cell 的 HTTP 服务(空载约 60 MB) |
25
+ | `ExecStdio` | 同一个服务器跑在标准输入输出上(一行一条消息),只服务一个宿主;输入结束或 60 秒收不到消息(宿主每 15 秒 ping)时结束它启动的一切 |
26
+ | `SshHost` | 经系统 `ssh` 连到另一台机器:自己的 ControlMaster 登录一次,其余命令共享;ssh 要问用户的经 `SSH_ASKPASS` 交给 `ask`;探测主机,没有 Node.js 18+ 时在本机下载(按 SHASUMS256 校验)后经 ssh 装到 `~/.ta/server/node`;把 `EXEC_REMOTE_SOURCE`(`scripts/remote-server.ts` 打包的 `ExecStdio`,约 7 KB,按哈希命名)放到 `~/.ta/server/<哈希>/`,在用户的登录 shell 里启动,`client` 就是它的 `ExecClient` |
27
+ | `SshConfig` | `~/.ssh/config`(含 `Include`)里没有通配符的 Host |
24
28
 
25
29
  ```bash
26
30
  # 执行机(容器里)
@@ -29,7 +33,7 @@ TA_EXEC_TOKEN=... ta exec-server --host 0.0.0.0 --port 4200 --cwd /workspace
29
33
  TA_EXEC_TOKEN=... ta serve --exec ws://exec-host:4200/exec
30
34
  ```
31
35
 
32
- 协议(`ExecRpc`)对标 Codex exec-server:WebSocket 上的 JSON-RPC 2.0,`process/exec`、`process/spawn`、`process/write`、`process/kill`、`fs/readFile`、`fs/writeFile`、`fs/stat`、`fs/readdir`、`fs/mkdir`、`fs/remove`;进程输出以 `process/output`、`process/stderr`、`process/exit` 通知推送,字节用 base64。
36
+ 协议(`ExecRpc`)对标 Codex exec-server:WebSocket 上(或标准输入输出上,一行一条)的 JSON-RPC 2.0,`process/exec`、`process/spawn`、`process/write`、`process/kill`、`fs/readFile`、`fs/writeFile`、`fs/stat`、`fs/readdir`、`fs/mkdir`、`fs/remove`;`net/connect`、`net/write`、`net/close`;进程输出以 `process/output`、`process/stderr`、`process/exit` 通知推送,连接的数据以 `net/data`、`net/closed` 推送,字节用 base64。`ExecClient` 不管传输:`ExecClient.webSocket(url, token)` 或 `SshHost` 给它连接。
33
37
 
34
38
  - **鉴权**:token(`Authorization: Bearer` 或 `?token=`)。安全边界是执行机本身(与 pi 一致:不在工具调用前审批,靠操作系统/容器隔离)。
35
39
  - **环境变量**:工具传入的环境只发送它在宿主环境之上新增或改动的变量(`RemoteExecEnvironment.delta`),宿主的变量(API key 等)不会到执行机。
@@ -1,10 +1,19 @@
1
1
  import { ExecRpc } from './exec-rpc.js';
2
+ /** One connection to `ta exec-server`, whatever carries it: a WebSocket, or ssh (SshHost). */
3
+ export interface ExecConnection {
4
+ send(text: string): void;
5
+ close(): void;
6
+ onMessage(listener: (text: string) => void): void;
7
+ /** Once, with what went wrong when it was not closed on purpose. */
8
+ onClose(listener: (reason?: string) => void): void;
9
+ }
2
10
  export interface ExecClientOptions {
3
- /** The WebSocket URL of `ta exec-server`. */
4
- url: string;
5
- token: string;
11
+ /** Opens a connection; rejects with an ExecError when it cannot. See {@link ExecClient.webSocket}. */
12
+ connect(): Promise<ExecConnection>;
6
13
  /** Larger than the epoch of any host that ran these sessions before; see {@link ExecRpc.InitializeParams}. */
7
14
  epoch: number;
15
+ /** Pings this often (milliseconds) while connected, for a server that ends without them (over stdio). */
16
+ pingMs?: number;
8
17
  }
9
18
  /** A failed call to `ta exec-server`. */
10
19
  export declare class ExecError extends Error {
@@ -26,27 +35,35 @@ interface PendingCall {
26
35
  */
27
36
  export declare class ExecClient {
28
37
  protected readonly options: ExecClientOptions;
29
- protected socket?: Promise<WebSocket>;
30
- protected current?: WebSocket;
38
+ protected connection?: Promise<ExecConnection>;
39
+ protected current?: ExecConnection;
31
40
  protected nextCall: number;
32
41
  protected nextProcess: number;
33
42
  protected fenced: boolean;
43
+ protected pinger?: ReturnType<typeof setInterval>;
34
44
  protected initialized?: ExecRpc.InitializeResult;
35
45
  protected readonly pending: Map<number, PendingCall>;
36
46
  protected readonly listeners: Map<number, NotificationListener>;
47
+ protected readonly stateListeners: Set<(connected: boolean, error?: string) => void>;
37
48
  constructor(options: ExecClientOptions);
49
+ /** Connects to a `ta exec-server` listening on a WebSocket. */
50
+ static webSocket(url: string, token: string): () => Promise<ExecConnection>;
51
+ get connected(): boolean;
38
52
  call<T>(method: string, params: object): Promise<T>;
39
53
  /** What the server said when this client connected. */
40
54
  info(): Promise<ExecRpc.InitializeResult>;
41
- /** An id for a process, which its notifications carry. */
55
+ /** An id for a process or a TCP connection, which its notifications carry. */
42
56
  processId(): number;
43
57
  listen(id: number, listener: NotificationListener): () => void;
58
+ /** Runs when a connection opens (true), is lost or closed (false), or cannot be opened (false, with why). */
59
+ onState(listener: (connected: boolean, error?: string) => void): () => void;
44
60
  close(): void;
45
- protected connect(): Promise<WebSocket>;
46
- protected open(): Promise<WebSocket>;
47
- protected send<T>(socket: WebSocket, method: string, params: object): Promise<T>;
61
+ protected connect(): Promise<ExecConnection>;
62
+ protected open(): Promise<ExecConnection>;
63
+ protected send<T>(connection: ExecConnection, method: string, params: object): Promise<T>;
64
+ /** Anything but JSON-RPC is left alone: a shell of the other machine may have printed it first. */
48
65
  protected receive(text: string): void;
49
- /** Calls fail, and processes are gone: the server ended them with the connection. */
50
- protected lost(socket: WebSocket): void;
66
+ /** Calls fail, and processes are gone: the server ended them with the connection. `reason`: it was not closed on purpose. */
67
+ protected lost(connection: ExecConnection, reason?: string): void;
51
68
  }
52
69
  export {};
@@ -20,17 +20,43 @@ export class ExecClient {
20
20
  this.fenced = false;
21
21
  this.pending = new Map();
22
22
  this.listeners = new Map();
23
+ this.stateListeners = new Set();
24
+ }
25
+ /** Connects to a `ta exec-server` listening on a WebSocket. */
26
+ static webSocket(url, token) {
27
+ return async () => {
28
+ const address = new URL(url);
29
+ address.searchParams.set('token', token);
30
+ const socket = new WebSocket(address);
31
+ await new Promise((resolve, reject) => {
32
+ socket.onopen = () => resolve();
33
+ socket.onerror = () => reject(new ExecError(`Cannot connect to ${url}.`, ExecRpc.ErrorCode.CONNECTION_LOST));
34
+ });
35
+ return {
36
+ send: text => socket.send(text),
37
+ close: () => socket.close(),
38
+ onMessage: listener => {
39
+ socket.onmessage = message => listener(String(message.data));
40
+ },
41
+ onClose: listener => {
42
+ socket.onclose = () => listener();
43
+ },
44
+ };
45
+ };
46
+ }
47
+ get connected() {
48
+ return this.current !== undefined;
23
49
  }
24
50
  async call(method, params) {
25
- const socket = await this.connect();
26
- return this.send(socket, method, params);
51
+ const connection = await this.connect();
52
+ return this.send(connection, method, params);
27
53
  }
28
54
  /** What the server said when this client connected. */
29
55
  async info() {
30
56
  await this.connect();
31
57
  return this.initialized;
32
58
  }
33
- /** An id for a process, which its notifications carry. */
59
+ /** An id for a process or a TCP connection, which its notifications carry. */
34
60
  processId() {
35
61
  return ++this.nextProcess;
36
62
  }
@@ -38,60 +64,83 @@ export class ExecClient {
38
64
  this.listeners.set(id, listener);
39
65
  return () => this.listeners.delete(id);
40
66
  }
67
+ /** Runs when a connection opens (true), is lost or closed (false), or cannot be opened (false, with why). */
68
+ onState(listener) {
69
+ this.stateListeners.add(listener);
70
+ return () => this.stateListeners.delete(listener);
71
+ }
41
72
  close() {
42
- var _a;
43
- (_a = this.current) === null || _a === void 0 ? void 0 : _a.close();
44
- this.current = undefined;
45
- this.socket = undefined;
73
+ const current = this.current;
74
+ this.connection = undefined;
75
+ current === null || current === void 0 ? void 0 : current.close();
76
+ if (current) {
77
+ this.lost(current);
78
+ }
46
79
  }
47
80
  connect() {
48
81
  var _a;
49
82
  if (this.fenced) {
50
83
  return Promise.reject(new ExecError('A host with a newer epoch took over the execution environment.', ExecRpc.ErrorCode.STALE_EPOCH));
51
84
  }
52
- (_a = this.socket) !== null && _a !== void 0 ? _a : (this.socket = this.open().catch(error => {
53
- this.socket = undefined;
85
+ (_a = this.connection) !== null && _a !== void 0 ? _a : (this.connection = this.open().catch(error => {
86
+ this.connection = undefined;
87
+ for (const listener of this.stateListeners) {
88
+ listener(false, error instanceof Error ? error.message : String(error));
89
+ }
54
90
  throw error;
55
91
  }));
56
- return this.socket;
92
+ return this.connection;
57
93
  }
58
94
  async open() {
59
- const url = new URL(this.options.url);
60
- url.searchParams.set('token', this.options.token);
61
- const socket = new WebSocket(url);
62
- await new Promise((resolve, reject) => {
63
- socket.onopen = () => resolve();
64
- socket.onerror = () => reject(new ExecError(`Cannot connect to ${this.options.url}.`, ExecRpc.ErrorCode.CONNECTION_LOST));
65
- });
66
- socket.onmessage = message => this.receive(String(message.data));
67
- this.current = socket;
68
- socket.onclose = () => this.lost(socket);
95
+ var _a, _b;
96
+ const connection = await this.options.connect();
97
+ connection.onMessage(text => this.receive(text));
98
+ connection.onClose(reason => this.lost(connection, reason));
99
+ this.current = connection;
69
100
  try {
70
- this.initialized = await this.send(socket, ExecRpc.Method.INITIALIZE, { epoch: this.options.epoch });
101
+ this.initialized = await this.send(connection, ExecRpc.Method.INITIALIZE, { epoch: this.options.epoch });
71
102
  }
72
103
  catch (error) {
73
104
  if (error.code === ExecRpc.ErrorCode.STALE_EPOCH) {
74
105
  this.fenced = true;
75
106
  }
76
- socket.close();
107
+ connection.close();
77
108
  throw error;
78
109
  }
79
- return socket;
110
+ if (this.options.pingMs) {
111
+ clearInterval(this.pinger);
112
+ this.pinger = setInterval(() => connection.send(JSON.stringify({ jsonrpc: '2.0', method: ExecRpc.PING })), this.options.pingMs);
113
+ (_b = (_a = this.pinger).unref) === null || _b === void 0 ? void 0 : _b.call(_a);
114
+ }
115
+ for (const listener of this.stateListeners) {
116
+ listener(true);
117
+ }
118
+ return connection;
80
119
  }
81
- send(socket, method, params) {
82
- if (socket.readyState !== WebSocket.OPEN) {
120
+ send(connection, method, params) {
121
+ if (this.current !== connection) {
83
122
  return Promise.reject(new ExecError('The connection to the execution environment was lost.', ExecRpc.ErrorCode.CONNECTION_LOST));
84
123
  }
85
124
  const id = ++this.nextCall;
86
125
  return new Promise((resolve, reject) => {
87
126
  this.pending.set(id, { resolve: resolve, reject });
88
- socket.send(JSON.stringify({ jsonrpc: '2.0', id, method, params }));
127
+ connection.send(JSON.stringify({ jsonrpc: '2.0', id, method, params }));
89
128
  });
90
129
  }
130
+ /** Anything but JSON-RPC is left alone: a shell of the other machine may have printed it first. */
91
131
  receive(text) {
92
132
  var _a, _b, _c;
93
133
  var _d;
94
- const message = JSON.parse(text);
134
+ let message;
135
+ try {
136
+ message = JSON.parse(text);
137
+ }
138
+ catch (_e) {
139
+ return;
140
+ }
141
+ if ((message === null || message === void 0 ? void 0 : message.jsonrpc) !== '2.0') {
142
+ return;
143
+ }
95
144
  if (message.method) {
96
145
  (_b = this.listeners.get((_a = message.params) === null || _a === void 0 ? void 0 : _a.id)) === null || _b === void 0 ? void 0 : _b(message.method, message.params);
97
146
  return;
@@ -109,20 +158,27 @@ export class ExecClient {
109
158
  call === null || call === void 0 ? void 0 : call.resolve(message.result);
110
159
  }
111
160
  }
112
- /** Calls fail, and processes are gone: the server ended them with the connection. */
113
- lost(socket) {
114
- if (this.current === socket) {
115
- this.current = undefined;
116
- this.socket = undefined;
161
+ /** Calls fail, and processes are gone: the server ended them with the connection. `reason`: it was not closed on purpose. */
162
+ lost(connection, reason) {
163
+ if (this.current !== connection) {
164
+ return;
117
165
  }
166
+ this.current = undefined;
167
+ this.connection = undefined;
168
+ clearInterval(this.pinger);
169
+ this.pinger = undefined;
170
+ const message = reason !== null && reason !== void 0 ? reason : 'The connection to the execution environment was lost.';
118
171
  for (const call of this.pending.values()) {
119
- call.reject(new ExecError('The connection to the execution environment was lost.', ExecRpc.ErrorCode.CONNECTION_LOST));
172
+ call.reject(new ExecError(message, ExecRpc.ErrorCode.CONNECTION_LOST));
120
173
  }
121
174
  this.pending.clear();
122
175
  for (const listener of [...this.listeners.values()]) {
123
176
  listener(ExecRpc.Notification.EXIT, { exitCode: null });
124
177
  }
125
178
  this.listeners.clear();
179
+ for (const listener of this.stateListeners) {
180
+ listener(false, reason);
181
+ }
126
182
  }
127
183
  }
128
184
  //# sourceMappingURL=exec-client.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"exec-client.js","sourceRoot":"","sources":["../../src/node/exec-client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAUrC,yCAAyC;AACzC,MAAM,OAAO,SAAU,SAAQ,KAAK;IAChC,YACI,OAAe,EACN,IAAuB;QAEhC,KAAK,CAAC,OAAO,CAAC,CAAC;oBAFN,IAAI;QAGb,IAAI,CAAC,IAAI,GAAG,WAAW,CAAC;IAC5B,CAAC;CACJ;AASD;;;;GAIG;AACH,MAAM,OAAO,UAAU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AAC;QAClF,KAAK,MAAM,IAAI,IAAI,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC;YACvC,IAAI,CAAC,MAAM,CAAC,IAAI,SAAS,CAAC,OAAO,EAAE,OAAO,CAAC,SAAS,CAAC,eAAe,CAAC,CAAC,CAAC;QAC3E,CAAC;QACD,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,CAAC;QACrB,KAAK,MAAM,QAAQ,IAAI,CAAC,GAAG,IAAI,CAAC,SAAS,CAAC,MAAM,EAAE,CAAC,EAAE,CAAC;YAClD,QAAQ,CAAC,OAAO,CAAC,YAAY,CAAC,IAAI,EAAE,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC,CAAC;QAC5D,CAAC;QACD,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE,CAAC;QACvB,KAAK,MAAM,QAAQ,IAAI,IAAI,CAAC,cAAc,EAAE,CAAC;YACzC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;QAC5B,CAAC;IACL,CAAC;CACJ"}
@@ -0,0 +1,40 @@
1
+ import { type Socket } from 'node:net';
2
+ import type { ExecProcess } from './exec-protocol.js';
3
+ import { ExecRpc } from './exec-rpc.js';
4
+ import { LocalExecEnvironment } from './local-exec-environment.js';
5
+ type Method = (typeof ExecRpc.Method)[keyof typeof ExecRpc.Method];
6
+ /** What a peer needs of its server: the default directory, and which host may run things now. */
7
+ export interface ExecPeerServer {
8
+ readonly cwd: string;
9
+ /** False when a newer host has taken over. */
10
+ admit(peer: ExecPeer, epoch: number): boolean;
11
+ isCurrent(epoch: number | undefined): boolean;
12
+ }
13
+ /** How a peer answers its host: a WebSocket, or the standard output. */
14
+ export interface ExecPeerChannel {
15
+ send(text: string): void;
16
+ close(code: number, reason: string): void;
17
+ }
18
+ /** One host connection: its calls, and the processes and TCP connections it started. */
19
+ export declare class ExecPeer {
20
+ protected readonly server: ExecPeerServer;
21
+ protected readonly channel: ExecPeerChannel;
22
+ epoch?: number;
23
+ protected readonly running: Map<number, AbortController>;
24
+ protected readonly spawned: Map<number, ExecProcess>;
25
+ protected readonly sockets: Map<number, Socket>;
26
+ constructor(server: ExecPeerServer, channel: ExecPeerChannel);
27
+ close(code: number, reason: string): void;
28
+ /** Ends what this host started. */
29
+ dispose(): void;
30
+ receive(text: string): Promise<void>;
31
+ protected initialize(id: number | undefined, params: ExecRpc.InitializeParams): ExecRpc.Message;
32
+ protected call(method: Method, params: any): Promise<unknown>;
33
+ protected exec(environment: LocalExecEnvironment, params: ExecRpc.ExecParams): Promise<unknown>;
34
+ protected spawn(environment: LocalExecEnvironment, params: ExecRpc.SpawnParams): void;
35
+ /** Resolves once connected; the data comes as notifications, the end as NET_CLOSED. */
36
+ protected connect(params: ExecRpc.ConnectParams): Promise<void>;
37
+ protected notify(method: string, params: unknown): void;
38
+ protected send(message: ExecRpc.Message): void;
39
+ }
40
+ export {};
@@ -0,0 +1,167 @@
1
+ import { connect } from 'node:net';
2
+ import { ExecRpc } from './exec-rpc.js';
3
+ import { LocalExecEnvironment } from './local-exec-environment.js';
4
+ /** One host connection: its calls, and the processes and TCP connections it started. */
5
+ export class ExecPeer {
6
+ constructor(server, channel) {
7
+ this.server = server;
8
+ this.channel = channel;
9
+ this.running = new Map();
10
+ this.spawned = new Map();
11
+ this.sockets = new Map();
12
+ }
13
+ close(code, reason) {
14
+ this.dispose();
15
+ this.channel.close(code, reason);
16
+ }
17
+ /** Ends what this host started. */
18
+ dispose() {
19
+ for (const controller of this.running.values()) {
20
+ controller.abort();
21
+ }
22
+ for (const child of this.spawned.values()) {
23
+ child.kill();
24
+ }
25
+ for (const socket of this.sockets.values()) {
26
+ socket.destroy();
27
+ }
28
+ this.running.clear();
29
+ this.spawned.clear();
30
+ this.sockets.clear();
31
+ }
32
+ async receive(text) {
33
+ var _a;
34
+ let message;
35
+ try {
36
+ message = JSON.parse(text);
37
+ }
38
+ catch (_b) {
39
+ this.close(1007, 'Not JSON.');
40
+ return;
41
+ }
42
+ const { id, method, params } = message;
43
+ if (method === ExecRpc.PING) {
44
+ return;
45
+ }
46
+ let reply;
47
+ if (method === ExecRpc.Method.INITIALIZE) {
48
+ reply = this.initialize(id, params);
49
+ }
50
+ else if (this.epoch === undefined) {
51
+ reply = ExecRpc.error(id, ExecRpc.ErrorCode.NOT_INITIALIZED, 'Call initialize first.');
52
+ }
53
+ else if (!this.server.isCurrent(this.epoch)) {
54
+ reply = ExecRpc.error(id, ExecRpc.ErrorCode.STALE_EPOCH, 'A host with a newer epoch took over.');
55
+ }
56
+ else {
57
+ try {
58
+ reply = { jsonrpc: '2.0', id, result: (_a = (await this.call(method, params))) !== null && _a !== void 0 ? _a : null };
59
+ }
60
+ catch (error) {
61
+ reply = ExecRpc.error(id, ExecRpc.ErrorCode.FAILED, error instanceof Error ? error.message : String(error));
62
+ }
63
+ }
64
+ if (id !== undefined) {
65
+ this.send(reply);
66
+ }
67
+ }
68
+ initialize(id, params) {
69
+ if (typeof (params === null || params === void 0 ? void 0 : params.epoch) !== 'number' || !this.server.admit(this, params.epoch)) {
70
+ return ExecRpc.error(id, ExecRpc.ErrorCode.STALE_EPOCH, 'A host with a newer epoch is connected.');
71
+ }
72
+ this.epoch = params.epoch;
73
+ const result = { platform: process.platform, cwd: this.server.cwd };
74
+ return { jsonrpc: '2.0', id, result };
75
+ }
76
+ async call(method, params) {
77
+ var _a, _b, _c, _d, _e;
78
+ var _f;
79
+ const environment = new LocalExecEnvironment((_f = params.root) !== null && _f !== void 0 ? _f : this.server.cwd);
80
+ switch (method) {
81
+ case ExecRpc.Method.EXEC:
82
+ return this.exec(environment, params);
83
+ case ExecRpc.Method.SPAWN:
84
+ return this.spawn(environment, params);
85
+ case ExecRpc.Method.WRITE:
86
+ (_a = this.spawned.get(params.id)) === null || _a === void 0 ? void 0 : _a.write(ExecRpc.fromBase64(params.data));
87
+ return;
88
+ case ExecRpc.Method.KILL:
89
+ (_b = this.running.get(params.id)) === null || _b === void 0 ? void 0 : _b.abort();
90
+ (_c = this.spawned.get(params.id)) === null || _c === void 0 ? void 0 : _c.kill();
91
+ return;
92
+ case ExecRpc.Method.READ_FILE:
93
+ return ExecRpc.toBase64(await environment.readFile(params.path));
94
+ case ExecRpc.Method.WRITE_FILE:
95
+ return environment.writeFile(params.path, ExecRpc.fromBase64(params.data));
96
+ case ExecRpc.Method.STAT:
97
+ return environment.stat(params.path);
98
+ case ExecRpc.Method.READDIR:
99
+ return environment.readdir(params.path);
100
+ case ExecRpc.Method.MKDIR:
101
+ return environment.mkdir(params.path);
102
+ case ExecRpc.Method.REMOVE:
103
+ return environment.remove(params.path, { recursive: params.recursive });
104
+ case ExecRpc.Method.NET_CONNECT:
105
+ return this.connect(params);
106
+ case ExecRpc.Method.NET_WRITE:
107
+ (_d = this.sockets.get(params.id)) === null || _d === void 0 ? void 0 : _d.write(ExecRpc.fromBase64(params.data));
108
+ return;
109
+ case ExecRpc.Method.NET_CLOSE:
110
+ (_e = this.sockets.get(params.id)) === null || _e === void 0 ? void 0 : _e.end();
111
+ return;
112
+ default:
113
+ throw new Error(`No method ${method}.`);
114
+ }
115
+ }
116
+ async exec(environment, params) {
117
+ const controller = new AbortController();
118
+ this.running.set(params.id, controller);
119
+ try {
120
+ return await environment.exec(params.command, {
121
+ cwd: params.cwd,
122
+ env: { ...process.env, ...params.env },
123
+ timeout: params.timeout,
124
+ signal: controller.signal,
125
+ onData: data => this.notify(ExecRpc.Notification.OUTPUT, { id: params.id, data: ExecRpc.toBase64(data) }),
126
+ });
127
+ }
128
+ finally {
129
+ this.running.delete(params.id);
130
+ }
131
+ }
132
+ spawn(environment, params) {
133
+ const child = environment.spawn(params.command, params.args, { cwd: params.cwd, env: { ...process.env, ...params.env } });
134
+ this.spawned.set(params.id, child);
135
+ child.onStdout(data => this.notify(ExecRpc.Notification.OUTPUT, { id: params.id, data: ExecRpc.toBase64(data) }));
136
+ child.onStderr(data => this.notify(ExecRpc.Notification.STDERR, { id: params.id, data: ExecRpc.toBase64(data) }));
137
+ child.onExit(exitCode => {
138
+ this.spawned.delete(params.id);
139
+ this.notify(ExecRpc.Notification.EXIT, { id: params.id, exitCode });
140
+ });
141
+ }
142
+ /** Resolves once connected; the data comes as notifications, the end as NET_CLOSED. */
143
+ connect(params) {
144
+ return new Promise((resolve, reject) => {
145
+ var _a;
146
+ const socket = connect({ port: params.port, host: (_a = params.host) !== null && _a !== void 0 ? _a : '127.0.0.1' });
147
+ socket.once('connect', () => {
148
+ this.sockets.set(params.id, socket);
149
+ resolve();
150
+ });
151
+ socket.on('data', data => this.notify(ExecRpc.Notification.NET_DATA, { id: params.id, data: ExecRpc.toBase64(data) }));
152
+ socket.once('error', reject);
153
+ socket.once('close', () => {
154
+ if (this.sockets.delete(params.id)) {
155
+ this.notify(ExecRpc.Notification.NET_CLOSED, { id: params.id });
156
+ }
157
+ });
158
+ });
159
+ }
160
+ notify(method, params) {
161
+ this.send({ jsonrpc: '2.0', method, params });
162
+ }
163
+ send(message) {
164
+ this.channel.send(JSON.stringify(message));
165
+ }
166
+ }
167
+ //# sourceMappingURL=exec-peer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"exec-peer.js","sourceRoot":"","sources":["../../src/node/exec-peer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAe,MAAM,UAAU,CAAC;AAEhD,OAAO,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AACrC,OAAO,EAAE,oBAAoB,EAAE,MAAM,0BAA0B,CAAC;AAkBhE,wFAAwF;AACxF,MAAM,OAAO,QAAQ;IAMjB,YACuB,MAAsB,EACtB,OAAwB;QADxB,WAAM,GAAN,MAAM,CAAgB;QACtB,YAAO,GAAP,OAAO,CAAiB;QAN5B,YAAO,GAAG,IAAI,GAAG,EAA2B,CAAC;QAC7C,YAAO,GAAG,IAAI,GAAG,EAAuB,CAAC;QACzC,YAAO,GAAG,IAAI,GAAG,EAAkB,CAAC;IAKpD,CAAC;IAEJ,KAAK,CAAC,IAAY,EAAE,MAAc;QAC9B,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,CAAC,EAAE,CAAC,MAAM,EAAE,IAAI,CAAC,EAAE,CAAC,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,YAAY,CAAC,QAAQ,EAAE,EAAE,EAAE,EAAE,MAAM,CAAC,EAAE,EAAE,IAAI,EAAE,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC;YACvH,MAAM,CAAC,IAAI,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;YAC7B,MAAM,CAAC,IAAI,CAAC,OAAO,EAAE,GAAG,EAAE;gBACtB,IAAI,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC,EAAE,CAAC;oBACjC,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,YAAY,CAAC,UAAU,EAAE,EAAE,EAAE,EAAE,MAAM,CAAC,EAAE,EAAE,CAAC,CAAC;gBACpE,CAAC;YACL,CAAC,CAAC,CAAC;QACP,CAAC,CAAC,CAAC;IACP,CAAC;IAES,MAAM,CAAC,MAAc,EAAE,MAAe;QAC5C,IAAI,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC,CAAC;IAClD,CAAC;IAES,IAAI,CAAC,OAAwB;QACnC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC,CAAC;IAC/C,CAAC;CACJ"}
@@ -25,6 +25,15 @@ export interface ExecProcess {
25
25
  onExit(listener: (exitCode: number | null) => void): void;
26
26
  kill(): void;
27
27
  }
28
+ /** A TCP connection made from inside an environment. */
29
+ export interface ExecSocket {
30
+ write(data: Uint8Array): void;
31
+ /** Ends the connection; `onClose` follows. */
32
+ end(): void;
33
+ onData(listener: (data: Uint8Array) => void): void;
34
+ /** Once, when either side ended it or the environment was lost. */
35
+ onClose(listener: () => void): void;
36
+ }
28
37
  export interface FileStat {
29
38
  isDirectory: boolean;
30
39
  size: number;
@@ -54,6 +63,11 @@ export interface ExecEnvironment {
54
63
  remove(path: string, options?: {
55
64
  recursive?: boolean;
56
65
  }): Promise<void>;
66
+ /**
67
+ * A TCP connection to a port of the environment's machine (default host 127.0.0.1), for one only
68
+ * reachable there; only environments on another machine have it. Rejects when nothing listens.
69
+ */
70
+ connect?(port: number, host?: string): Promise<ExecSocket>;
57
71
  }
58
72
  /** Bind another implementation with `rebind: true` to run sessions elsewhere. */
59
73
  export interface ExecEnvironmentFactory {
@@ -0,0 +1,5 @@
1
+ /** `ta exec-server` on stdio in one script for Node.js 18, and the hash that names its version. */
2
+ export declare const EXEC_REMOTE_SOURCE: {
3
+ hash: string;
4
+ script: string;
5
+ };