@celljs/ta-host 4.6.1 → 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 (154) hide show
  1. package/README.en.md +68 -0
  2. package/README.md +13 -2
  3. package/cell.schema.json +21 -0
  4. package/cell.yml +7 -0
  5. package/esm/node/exec-environment-factory.d.ts +8 -4
  6. package/esm/node/exec-environment-factory.js +28 -6
  7. package/esm/node/exec-environment-factory.js.map +1 -1
  8. package/esm/node/index.d.ts +1 -0
  9. package/esm/node/index.js +1 -0
  10. package/esm/node/index.js.map +1 -1
  11. package/esm/node/package-hosts.d.ts +4 -0
  12. package/esm/node/package-hosts.js +30 -0
  13. package/esm/node/package-hosts.js.map +1 -1
  14. package/esm/node/remote/index.d.ts +5 -0
  15. package/esm/node/remote/index.js +6 -0
  16. package/esm/node/remote/index.js.map +1 -0
  17. package/esm/node/remote/pi-directories.d.ts +14 -0
  18. package/esm/node/remote/pi-directories.js +64 -0
  19. package/esm/node/remote/pi-directories.js.map +1 -0
  20. package/esm/node/remote/port-forwards.d.ts +35 -0
  21. package/esm/node/remote/port-forwards.js +150 -0
  22. package/esm/node/remote/port-forwards.js.map +1 -0
  23. package/esm/node/remote/remote-hosts.d.ts +40 -0
  24. package/esm/node/remote/remote-hosts.js +170 -0
  25. package/esm/node/remote/remote-hosts.js.map +1 -0
  26. package/esm/node/remote/remote-protocol.d.ts +51 -0
  27. package/esm/node/remote/remote-protocol.js +5 -0
  28. package/esm/node/remote/remote-protocol.js.map +1 -0
  29. package/esm/node/remote/remote-server-impl.d.ts +19 -0
  30. package/esm/node/remote/remote-server-impl.js +66 -0
  31. package/esm/node/remote/remote-server-impl.js.map +1 -0
  32. package/esm/node/settings-store.d.ts +3 -1
  33. package/esm/node/settings-store.js +9 -2
  34. package/esm/node/settings-store.js.map +1 -1
  35. package/esm/node/ta-server-impl.d.ts +2 -0
  36. package/esm/node/ta-server-impl.js +16 -4
  37. package/esm/node/ta-server-impl.js.map +1 -1
  38. package/esm/node/ta-session-factory.d.ts +16 -2
  39. package/esm/node/ta-session-factory.js +38 -8
  40. package/esm/node/ta-session-factory.js.map +1 -1
  41. package/esm/node/ta-session.d.ts +4 -0
  42. package/esm/node/ta-session.js +5 -1
  43. package/esm/node/ta-session.js.map +1 -1
  44. package/esm/node/terminal-service.js +1 -1
  45. package/esm/node/terminal-service.js.map +1 -1
  46. package/esm/node/web-extension-catalog.d.ts +3 -1
  47. package/esm/node/web-extension-catalog.js +8 -2
  48. package/esm/node/web-extension-catalog.js.map +1 -1
  49. package/esm/node/working-directory.d.ts +12 -1
  50. package/esm/node/working-directory.js +22 -1
  51. package/esm/node/working-directory.js.map +1 -1
  52. package/esm/node/workspace-files.d.ts +1 -1
  53. package/esm/node/workspace-files.js +3 -3
  54. package/esm/node/workspace-files.js.map +1 -1
  55. package/esm/node/workspace-service.js +3 -3
  56. package/esm/node/workspace-service.js.map +1 -1
  57. package/lib/node/exec-environment-factory.d.ts +8 -4
  58. package/lib/node/exec-environment-factory.d.ts.map +1 -1
  59. package/lib/node/exec-environment-factory.js +28 -6
  60. package/lib/node/exec-environment-factory.js.map +1 -1
  61. package/lib/node/index.d.ts +1 -0
  62. package/lib/node/index.d.ts.map +1 -1
  63. package/lib/node/index.js +1 -0
  64. package/lib/node/index.js.map +1 -1
  65. package/lib/node/package-hosts.d.ts +4 -0
  66. package/lib/node/package-hosts.d.ts.map +1 -1
  67. package/lib/node/package-hosts.js +30 -0
  68. package/lib/node/package-hosts.js.map +1 -1
  69. package/lib/node/remote/index.d.ts +6 -0
  70. package/lib/node/remote/index.d.ts.map +1 -0
  71. package/lib/node/remote/index.js +22 -0
  72. package/lib/node/remote/index.js.map +1 -0
  73. package/lib/node/remote/pi-directories.d.ts +15 -0
  74. package/lib/node/remote/pi-directories.d.ts.map +1 -0
  75. package/lib/node/remote/pi-directories.js +67 -0
  76. package/lib/node/remote/pi-directories.js.map +1 -0
  77. package/lib/node/remote/port-forwards.d.ts +36 -0
  78. package/lib/node/remote/port-forwards.d.ts.map +1 -0
  79. package/lib/node/remote/port-forwards.js +153 -0
  80. package/lib/node/remote/port-forwards.js.map +1 -0
  81. package/lib/node/remote/remote-hosts.d.ts +41 -0
  82. package/lib/node/remote/remote-hosts.d.ts.map +1 -0
  83. package/lib/node/remote/remote-hosts.js +173 -0
  84. package/lib/node/remote/remote-hosts.js.map +1 -0
  85. package/lib/node/remote/remote-protocol.d.ts +52 -0
  86. package/lib/node/remote/remote-protocol.d.ts.map +1 -0
  87. package/lib/node/remote/remote-protocol.js +8 -0
  88. package/lib/node/remote/remote-protocol.js.map +1 -0
  89. package/lib/node/remote/remote-server-impl.d.ts +20 -0
  90. package/lib/node/remote/remote-server-impl.d.ts.map +1 -0
  91. package/lib/node/remote/remote-server-impl.js +69 -0
  92. package/lib/node/remote/remote-server-impl.js.map +1 -0
  93. package/lib/node/remote/remote.spec.d.ts +2 -0
  94. package/lib/node/remote/remote.spec.d.ts.map +1 -0
  95. package/lib/node/remote/remote.spec.js +130 -0
  96. package/lib/node/remote/remote.spec.js.map +1 -0
  97. package/lib/node/settings-store.d.ts +3 -1
  98. package/lib/node/settings-store.d.ts.map +1 -1
  99. package/lib/node/settings-store.js +9 -2
  100. package/lib/node/settings-store.js.map +1 -1
  101. package/lib/node/settings-store.spec.js +2 -1
  102. package/lib/node/settings-store.spec.js.map +1 -1
  103. package/lib/node/ta-server-impl.d.ts +2 -0
  104. package/lib/node/ta-server-impl.d.ts.map +1 -1
  105. package/lib/node/ta-server-impl.js +16 -4
  106. package/lib/node/ta-server-impl.js.map +1 -1
  107. package/lib/node/ta-session-factory.d.ts +16 -2
  108. package/lib/node/ta-session-factory.d.ts.map +1 -1
  109. package/lib/node/ta-session-factory.js +37 -7
  110. package/lib/node/ta-session-factory.js.map +1 -1
  111. package/lib/node/ta-session.d.ts +4 -0
  112. package/lib/node/ta-session.d.ts.map +1 -1
  113. package/lib/node/ta-session.js +5 -1
  114. package/lib/node/ta-session.js.map +1 -1
  115. package/lib/node/terminal-service.js +1 -1
  116. package/lib/node/terminal-service.js.map +1 -1
  117. package/lib/node/web-extension-catalog.d.ts +3 -1
  118. package/lib/node/web-extension-catalog.d.ts.map +1 -1
  119. package/lib/node/web-extension-catalog.js +7 -1
  120. package/lib/node/web-extension-catalog.js.map +1 -1
  121. package/lib/node/web-extension-catalog.spec.js +1 -1
  122. package/lib/node/web-extension-catalog.spec.js.map +1 -1
  123. package/lib/node/working-directory.d.ts +12 -1
  124. package/lib/node/working-directory.d.ts.map +1 -1
  125. package/lib/node/working-directory.js +22 -1
  126. package/lib/node/working-directory.js.map +1 -1
  127. package/lib/node/workspace-files.d.ts +1 -1
  128. package/lib/node/workspace-files.d.ts.map +1 -1
  129. package/lib/node/workspace-files.js +3 -3
  130. package/lib/node/workspace-files.js.map +1 -1
  131. package/lib/node/workspace-service.js +3 -3
  132. package/lib/node/workspace-service.js.map +1 -1
  133. package/package.json +14 -10
  134. package/src/node/exec-environment-factory.ts +26 -6
  135. package/src/node/index.ts +1 -0
  136. package/src/node/package-hosts.ts +28 -0
  137. package/src/node/remote/index.ts +5 -0
  138. package/src/node/remote/pi-directories.ts +49 -0
  139. package/src/node/remote/port-forwards.ts +148 -0
  140. package/src/node/remote/remote-hosts.ts +170 -0
  141. package/src/node/remote/remote-protocol.ts +57 -0
  142. package/src/node/remote/remote-server-impl.ts +60 -0
  143. package/src/node/remote/remote.spec.ts +142 -0
  144. package/src/node/settings-store.spec.ts +2 -1
  145. package/src/node/settings-store.ts +8 -2
  146. package/src/node/ta-server-impl.ts +18 -4
  147. package/src/node/ta-session-factory.ts +40 -7
  148. package/src/node/ta-session.ts +8 -1
  149. package/src/node/terminal-service.ts +1 -1
  150. package/src/node/web-extension-catalog.spec.ts +1 -1
  151. package/src/node/web-extension-catalog.ts +7 -2
  152. package/src/node/working-directory.ts +35 -2
  153. package/src/node/workspace-files.ts +3 -3
  154. package/src/node/workspace-service.ts +3 -3
@@ -0,0 +1,170 @@
1
+ import { randomUUID } from 'node:crypto';
2
+ import { CODING_AGENT_OPTIONS, type CodingAgentOptions } from '@celljs/ai-coding-agent';
3
+ import { type Application, ApplicationLifecycle, Autowired, Component, Value } from '@celljs/core';
4
+ import type { RpcConnection } from '@celljs/rpc/lib/node';
5
+ import { type ExecClient, SshConfig, SshHost, type SshQuestion } from '@celljs/ta-exec';
6
+ import { TaBrand } from '@celljs/ta-harness/lib/node/ta-brand';
7
+ import { TaClient, TaLocation, type TaRemoteEvent, type TaRemoteHost, type TaRemotePrompt } from '@celljs/ta-protocol';
8
+ import { TaErrors } from '../ta-errors';
9
+ import { TaWorkingDirectory } from '../working-directory';
10
+ import { RemoteHosts, TA_REMOTE_OPTIONS, type TaRemoteOptions } from './remote-protocol';
11
+
12
+ /** A prompt nobody answers is cancelled after this: ssh gives up. */
13
+ const PROMPT_TIMEOUT_MS = 5 * 60 * 1000;
14
+
15
+ interface Waiting {
16
+ prompt: TaRemotePrompt;
17
+ settle(value: string | undefined): void;
18
+ }
19
+
20
+ /**
21
+ * One SshHost per host, which connects on the first call there and again after a loss. What ssh asks
22
+ * goes to every client that watches, and the first answer counts, as with the dialogs of sessions.
23
+ */
24
+ @Component([RemoteHosts, ApplicationLifecycle])
25
+ export class DefaultRemoteHosts implements RemoteHosts, ApplicationLifecycle<Application> {
26
+ @Value(TA_REMOTE_OPTIONS)
27
+ protected readonly options?: TaRemoteOptions;
28
+
29
+ @Value(CODING_AGENT_OPTIONS)
30
+ protected readonly codingAgentOptions?: CodingAgentOptions;
31
+
32
+ @Autowired(TaWorkingDirectory)
33
+ protected readonly workingDirectory: TaWorkingDirectory;
34
+
35
+ protected readonly hosts = new Map<string, SshHost>();
36
+ protected readonly waiting = new Map<string, Waiting>();
37
+ protected readonly watchers = new Set<RpcConnection>();
38
+ protected readonly disconnectListeners = new Set<(host: string) => void>();
39
+
40
+ client(host: string): ExecClient {
41
+ this.check(host);
42
+ let ssh = this.hosts.get(host);
43
+ if (!ssh) {
44
+ ssh = new SshHost({
45
+ host,
46
+ stateDir: this.codingAgentOptions?.agentDir ?? TaBrand.agentDir(),
47
+ ssh: this.options?.ssh || undefined,
48
+ sshArgs: this.options?.sshArgs,
49
+ nodeMirror: this.options?.nodeMirror || undefined,
50
+ ask: question => this.ask(host, question),
51
+ onStep: (step, detail) => this.broadcast({ type: 'progress', host, step, detail }),
52
+ });
53
+ ssh.client.onState((connected, error) => {
54
+ if (connected) {
55
+ this.workingDirectory.addRecentHost(host);
56
+ }
57
+ this.broadcast(error ? { type: 'connection', host, connected, error } : { type: 'connection', host, connected });
58
+ });
59
+ this.hosts.set(host, ssh);
60
+ }
61
+ return ssh.client;
62
+ }
63
+
64
+ list(): TaRemoteHost[] {
65
+ const recent = this.workingDirectory.recentHosts();
66
+ const config = SshConfig.hosts().filter(host => TaLocation.isHost(host) && !recent.includes(host));
67
+ return [
68
+ ...recent.map(host => ({ host, source: 'recent' as const, connected: this.connected(host) })),
69
+ ...config.map(host => ({ host, source: 'config' as const, connected: this.connected(host) })),
70
+ ];
71
+ }
72
+
73
+ add(host: string): void {
74
+ this.workingDirectory.addRecentHost(this.check(host));
75
+ }
76
+
77
+ async remove(host: string): Promise<void> {
78
+ await this.disconnect(host);
79
+ this.workingDirectory.removeRecentHost(host);
80
+ }
81
+
82
+ async resolve(location: string): Promise<string> {
83
+ const remote = this.parse(location);
84
+ if (!remote || remote.path) {
85
+ return location;
86
+ }
87
+ return TaLocation.ssh(remote.host, (await this.client(remote.host).info()).cwd);
88
+ }
89
+
90
+ async disconnect(host: string): Promise<void> {
91
+ const ssh = this.hosts.get(host);
92
+ this.hosts.delete(host);
93
+ for (const listener of this.disconnectListeners) {
94
+ listener(host);
95
+ }
96
+ await ssh?.dispose();
97
+ }
98
+
99
+ onDisconnect(listener: (host: string) => void): () => void {
100
+ this.disconnectListeners.add(listener);
101
+ return () => this.disconnectListeners.delete(listener);
102
+ }
103
+
104
+ watch(connection: RpcConnection): TaRemotePrompt[] {
105
+ if (!this.watchers.has(connection)) {
106
+ this.watchers.add(connection);
107
+ connection.onClose(() => this.watchers.delete(connection));
108
+ }
109
+ return [...this.waiting.values()].map(({ prompt }) => prompt);
110
+ }
111
+
112
+ answer(promptId: string, value: string | null): boolean {
113
+ const waiting = this.waiting.get(promptId);
114
+ waiting?.settle(typeof value === 'string' ? value : undefined);
115
+ return waiting !== undefined;
116
+ }
117
+
118
+ async onStop(): Promise<void> {
119
+ for (const waiting of [...this.waiting.values()]) {
120
+ waiting.settle(undefined);
121
+ }
122
+ await Promise.all([...this.hosts.keys()].map(host => this.disconnect(host)));
123
+ }
124
+
125
+ protected ask(host: string, question: SshQuestion): Promise<string | undefined> {
126
+ const prompt: TaRemotePrompt = { promptId: randomUUID(), host, ...question };
127
+ return new Promise(resolve => {
128
+ const timer = setTimeout(() => settle(undefined), PROMPT_TIMEOUT_MS);
129
+ const settle = (value: string | undefined) => {
130
+ clearTimeout(timer);
131
+ if (this.waiting.delete(prompt.promptId)) {
132
+ this.broadcast({ type: 'prompt_closed', promptId: prompt.promptId });
133
+ resolve(value);
134
+ }
135
+ };
136
+ this.waiting.set(prompt.promptId, { prompt, settle });
137
+ this.broadcast({ type: 'prompt', prompt });
138
+ });
139
+ }
140
+
141
+ protected broadcast(event: TaRemoteEvent): void {
142
+ for (const connection of this.watchers) {
143
+ if (!connection.closed) {
144
+ connection
145
+ .client<TaClient>(TaClient)
146
+ .onRemoteEvent(event)
147
+ .catch(() => undefined);
148
+ }
149
+ }
150
+ }
151
+
152
+ protected check(host: string): string {
153
+ if (typeof host !== 'string' || !TaLocation.isHost(host)) {
154
+ throw TaErrors.invalidArgument(`${JSON.stringify(host)} is not a host.`, 'Name a Host of ~/.ssh/config, or [user@]host[:port].');
155
+ }
156
+ return host;
157
+ }
158
+
159
+ protected connected(host: string): boolean {
160
+ return this.hosts.get(host)?.client.connected ?? false;
161
+ }
162
+
163
+ protected parse(location: string): TaLocation.Remote | undefined {
164
+ try {
165
+ return TaLocation.parse(location);
166
+ } catch (error) {
167
+ throw TaErrors.invalidArgument((error as Error).message, 'Write ssh://<host>/<absolute path>.');
168
+ }
169
+ }
170
+ }
@@ -0,0 +1,57 @@
1
+ import type { RpcConnection } from '@celljs/rpc/lib/node';
2
+ import type { ExecClient } from '@celljs/ta-exec';
3
+ import type { TaForwardedPort, TaRemoteHost, TaRemotePrompt } from '@celljs/ta-protocol';
4
+
5
+ export const TA_REMOTE_OPTIONS = 'ta.remote';
6
+
7
+ /** `ta.remote`: how ta serve reaches other machines. */
8
+ export interface TaRemoteOptions {
9
+ /** The ssh command; default `ssh`. */
10
+ ssh?: string;
11
+ /** Before each host, as `-F <file>`. */
12
+ sshArgs?: string[];
13
+ /** Where Node.js comes from for hosts without one; empty: nodejs.org. */
14
+ nodeMirror?: string;
15
+ }
16
+
17
+ export const RemoteHosts = Symbol('RemoteHosts');
18
+ export const PiDirectories = Symbol('PiDirectories');
19
+ export const PortForwards = Symbol('PortForwards');
20
+
21
+ /** The hosts of `ssh://` locations, one connection each, and what their ssh asks the users. */
22
+ export interface RemoteHosts {
23
+ /** The connection to a host, made on its first call. */
24
+ client(host: string): ExecClient;
25
+ list(): TaRemoteHost[];
26
+ /** Keeps a host in the list without connecting; throws for what is not a host. */
27
+ add(host: string): void;
28
+ /** Disconnects and drops it from the list of those opened before. */
29
+ remove(host: string): Promise<void>;
30
+ /** The location with the home directory as its path when it has none. */
31
+ resolve(location: string): Promise<string>;
32
+ disconnect(host: string): Promise<void>;
33
+ /** Runs once a host is disconnected on purpose. */
34
+ onDisconnect(listener: (host: string) => void): () => void;
35
+ /** The connection hears TaClient.onRemoteEvent; the prompts that wait now. */
36
+ watch(connection: RpcConnection): TaRemotePrompt[];
37
+ answer(promptId: string, value: string | null): boolean;
38
+ }
39
+
40
+ /**
41
+ * The directory pi works in for a location. pi reads and writes this machine's files in it (its
42
+ * sessions are named after it, the project's settings are there), so a directory of another machine
43
+ * gets one here, empty, under `<agent directory>/remote/`; the agent's tools work there.
44
+ */
45
+ export interface PiDirectories {
46
+ /** Creates it when it is not there. */
47
+ toPi(location: string): string;
48
+ /** The location of a directory that `toPi` gave; any other is a directory of this machine. */
49
+ fromPi(cwd: string): string;
50
+ }
51
+
52
+ /** Ports of other machines, opened on this one. */
53
+ export interface PortForwards {
54
+ forward(cwd: string, port: number): Promise<TaForwardedPort>;
55
+ list(): TaForwardedPort[];
56
+ close(host: string, remotePort: number): Promise<void>;
57
+ }
@@ -0,0 +1,60 @@
1
+ import { Autowired } from '@celljs/core';
2
+ import { Rpc } from '@celljs/rpc';
3
+ import { RpcContext } from '@celljs/rpc/lib/node';
4
+ import { ExecEnvironmentFactory } from '@celljs/ta-exec';
5
+ import { type TaForwardedPort, type TaRemoteHost, type TaRemotePrompt, TaRemoteServer } from '@celljs/ta-protocol';
6
+ import { TaErrors } from '../ta-errors';
7
+ import { PortForwards, RemoteHosts } from './remote-protocol';
8
+
9
+ /** The other machines that conversations work on, for the clients. */
10
+ @Rpc(TaRemoteServer)
11
+ export class TaRemoteServerImpl implements TaRemoteServer {
12
+ @Autowired(RemoteHosts)
13
+ protected readonly hosts: RemoteHosts;
14
+
15
+ @Autowired(PortForwards)
16
+ protected readonly ports: PortForwards;
17
+
18
+ @Autowired(ExecEnvironmentFactory)
19
+ protected readonly environments: ExecEnvironmentFactory;
20
+
21
+ async listHosts(): Promise<TaRemoteHost[]> {
22
+ return TaErrors.guard(() => this.hosts.list());
23
+ }
24
+
25
+ addHost(host: string): Promise<void> {
26
+ return TaErrors.guard(() => this.hosts.add(host));
27
+ }
28
+
29
+ removeHost(host: string): Promise<void> {
30
+ return TaErrors.guard(() => this.hosts.remove(host));
31
+ }
32
+
33
+ resolveLocation(location: string): Promise<string> {
34
+ return TaErrors.guard(() => this.hosts.resolve(location));
35
+ }
36
+
37
+ forwardPort(port: number, cwd?: string): Promise<TaForwardedPort> {
38
+ return TaErrors.guard(async () => this.ports.forward(cwd || (await this.environments.defaultCwd()), port));
39
+ }
40
+
41
+ async listForwardedPorts(): Promise<TaForwardedPort[]> {
42
+ return this.ports.list();
43
+ }
44
+
45
+ closeForwardedPort(host: string, remotePort: number): Promise<void> {
46
+ return TaErrors.guard(() => this.ports.close(host, remotePort));
47
+ }
48
+
49
+ async watch(): Promise<TaRemotePrompt[]> {
50
+ return this.hosts.watch(RpcContext.requireConnection());
51
+ }
52
+
53
+ async answer(promptId: string, value: string | null): Promise<boolean> {
54
+ return this.hosts.answer(promptId, value);
55
+ }
56
+
57
+ disconnect(host: string): Promise<void> {
58
+ return TaErrors.guard(() => this.hosts.disconnect(host));
59
+ }
60
+ }
@@ -0,0 +1,142 @@
1
+ import { afterAll, describe, expect, it } from 'bun:test';
2
+ import { existsSync, mkdtempSync } from 'node:fs';
3
+ import { createServer } from 'node:net';
4
+ import { tmpdir } from 'node:os';
5
+ import { join } from 'node:path';
6
+ import type { RpcConnection } from '@celljs/rpc/lib/node';
7
+ import { type ExecEnvironment, type ExecSocket, LocalExecEnvironment } from '@celljs/ta-exec';
8
+ import type { TaRemoteEvent } from '@celljs/ta-protocol';
9
+ import { DefaultPiDirectories } from './pi-directories';
10
+ import { DefaultPortForwards } from './port-forwards';
11
+ import { DefaultRemoteHosts } from './remote-hosts';
12
+
13
+ const root = mkdtempSync(join(tmpdir(), 'ta-remote-'));
14
+
15
+ function directories(execUrl = ''): DefaultPiDirectories {
16
+ return Object.assign(new DefaultPiDirectories(), { codingAgentOptions: { agentDir: root }, execOptions: { url: execUrl } });
17
+ }
18
+
19
+ describe('DefaultPiDirectories', () => {
20
+ it('keeps a directory of this machine', () => {
21
+ expect(directories().toPi('/work/app')).toBe('/work/app');
22
+ expect(directories().fromPi('/work/app')).toBe('/work/app');
23
+ });
24
+
25
+ it('gives a directory of another machine one here, and back', () => {
26
+ const dirs = directories();
27
+ const cwd = dirs.toPi('ssh://me@dev:2222/srv/app');
28
+ expect(cwd).toBe(join(root, 'remote', 'ssh', 'me@dev:2222', 'srv', 'app'));
29
+ expect(existsSync(cwd)).toBe(true);
30
+ expect(dirs.fromPi(cwd)).toBe('ssh://me@dev:2222/srv/app');
31
+ expect(dirs.fromPi(dirs.toPi('ssh://dev/'))).toBe('ssh://dev/');
32
+ });
33
+
34
+ it('gives every directory one here with an exec server', () => {
35
+ const dirs = directories('ws://exec');
36
+ const cwd = dirs.toPi('/srv/app');
37
+ expect(cwd).toBe(join(root, 'remote', 'exec', 'srv', 'app'));
38
+ expect(dirs.fromPi(cwd)).toBe('/srv/app');
39
+ });
40
+ });
41
+
42
+ describe('DefaultRemoteHosts', () => {
43
+ it('asks every client that watches, and the first answer counts', async () => {
44
+ const events: TaRemoteEvent[][] = [[], []];
45
+ const connections = events.map(
46
+ received =>
47
+ ({
48
+ closed: false,
49
+ onClose: () => undefined,
50
+ client: () => ({ onRemoteEvent: async (event: TaRemoteEvent) => void received.push(event) }),
51
+ }) as unknown as RpcConnection,
52
+ );
53
+ const hosts = new DefaultRemoteHosts();
54
+ expect(hosts.watch(connections[0])).toEqual([]);
55
+ const asked = (hosts as unknown as { ask: DefaultRemoteHosts['ask'] }).ask.call(hosts, 'dev', { kind: 'secret', message: 'Password:' });
56
+ const [prompt] = hosts.watch(connections[1]);
57
+ expect(prompt).toMatchObject({ host: 'dev', kind: 'secret', message: 'Password:' });
58
+ expect(events[0]).toEqual([{ type: 'prompt', prompt }]);
59
+ expect(hosts.answer(prompt.promptId, 's3cret')).toBe(true);
60
+ expect(hosts.answer(prompt.promptId, 'again')).toBe(false);
61
+ expect(await asked).toBe('s3cret');
62
+ expect(events[1]).toEqual([{ type: 'prompt_closed', promptId: prompt.promptId }]);
63
+ });
64
+
65
+ it('refuses a host that ssh would read as an option', () => {
66
+ expect(() => new DefaultRemoteHosts().client('-oProxyCommand=x')).toThrow();
67
+ expect(() => new DefaultRemoteHosts().add('-oProxyCommand=x')).toThrow();
68
+ });
69
+
70
+ it('keeps the hosts added, the latest first, and forgets one removed', async () => {
71
+ let recent: string[] = [];
72
+ const hosts = Object.assign(new DefaultRemoteHosts(), {
73
+ workingDirectory: {
74
+ recentHosts: () => recent,
75
+ addRecentHost: (host: string) => (recent = [host, ...recent.filter(other => other !== host)]),
76
+ removeRecentHost: (host: string) => (recent = recent.filter(other => other !== host)),
77
+ },
78
+ });
79
+ hosts.add('dev');
80
+ hosts.add('me@prod:2222');
81
+ expect(hosts.list().filter(host => host.source === 'recent')).toEqual([
82
+ { host: 'me@prod:2222', source: 'recent', connected: false },
83
+ { host: 'dev', source: 'recent', connected: false },
84
+ ]);
85
+ await hosts.remove('dev');
86
+ expect(recent).toEqual(['me@prod:2222']);
87
+ });
88
+ });
89
+
90
+ describe('DefaultPortForwards', () => {
91
+ /** A machine whose connections are those of this one, as the server of Ta makes them there. */
92
+ class Remote extends LocalExecEnvironment {
93
+ async connect(port: number): Promise<ExecSocket> {
94
+ const { connect } = await import('node:net');
95
+ const socket = connect(port, '127.0.0.1');
96
+ await new Promise((resolve, reject) => socket.once('connect', resolve).once('error', reject));
97
+ return {
98
+ write: data => socket.write(data),
99
+ end: () => socket.end(),
100
+ onData: listener => socket.on('data', listener),
101
+ onClose: listener => socket.once('close', listener),
102
+ };
103
+ }
104
+ }
105
+ const forwards = new DefaultPortForwards();
106
+ const local = new LocalExecEnvironment(root);
107
+ const remote = new Remote(root);
108
+ Object.assign(forwards, {
109
+ environments: { create: (cwd: string): ExecEnvironment => (cwd.startsWith('ssh://') ? remote : local) },
110
+ hosts: { onDisconnect: () => () => undefined },
111
+ });
112
+ afterAll(() => forwards.onStop());
113
+
114
+ it('keeps a port of this machine as it is', async () => {
115
+ expect(await forwards.forward('/work', 3000)).toEqual({ host: '', remotePort: 3000, localPort: 3000 });
116
+ await expect(forwards.forward('/work', 70000)).rejects.toThrow();
117
+ });
118
+
119
+ it('opens a port of another machine here, and carries data both ways', async () => {
120
+ const server = createServer(socket => socket.on('data', data => socket.end(`echo:${data}`)));
121
+ await new Promise<void>(resolve => server.listen(0, '127.0.0.1', resolve));
122
+ const port = (server.address() as { port: number }).port;
123
+ try {
124
+ const forwarded = await forwards.forward('ssh://dev/srv', port);
125
+ expect(forwarded.host).toBe('dev');
126
+ expect(forwarded.localPort).not.toBe(port);
127
+ expect(await forwards.forward('ssh://dev/srv', port)).toEqual(forwarded);
128
+ expect(forwards.list()).toEqual([forwarded]);
129
+ const { connect } = await import('node:net');
130
+ const client = connect(forwarded.localPort, '127.0.0.1');
131
+ const received: Buffer[] = [];
132
+ client.on('data', data => received.push(data));
133
+ client.write('hi');
134
+ await new Promise(resolve => client.once('close', resolve));
135
+ expect(Buffer.concat(received).toString()).toBe('echo:hi');
136
+ await forwards.close('dev', port);
137
+ expect(forwards.list()).toEqual([]);
138
+ } finally {
139
+ server.close();
140
+ }
141
+ });
142
+ });
@@ -52,7 +52,7 @@ describe('TaSettingsStoreImpl', () => {
52
52
  function session(dir: string) {
53
53
  let trusted = false;
54
54
  return {
55
- runtime: { cwd: dir },
55
+ cwd: dir,
56
56
  pi: {
57
57
  settingsManager: {
58
58
  isProjectTrusted: () => trusted,
@@ -80,6 +80,7 @@ describe('TaSettingsStoreImpl', () => {
80
80
  codingAgentOptions: { agentDir },
81
81
  modelRuntimeProvider: { provide: async () => ({ getAvailable: async () => [{ provider: 'fake', id: 'fake-1' }] }) },
82
82
  sessions: { list: () => [session(cwd), session(join(root, 'other'))] },
83
+ directories: { toPi: (location: string) => location, fromPi: (dir: string) => dir },
83
84
  });
84
85
  });
85
86
  afterEach(() => rmSync(root, { recursive: true, force: true }));
@@ -4,6 +4,7 @@ import { CODING_AGENT_OPTIONS, type CodingAgentOptions, ModelRuntimeProvider, Pi
4
4
  import { Autowired, Component, Value } from '@celljs/core';
5
5
  import { TA_SETTINGS, type TaSettingSpec, type TaSettingsScope, type TaSettingsSnapshot } from '@celljs/ta-protocol';
6
6
  import { TaSessionRegistry } from './host-protocol';
7
+ import { PiDirectories } from './remote/remote-protocol';
7
8
  import { TaErrors } from './ta-errors';
8
9
 
9
10
  export const TaSettingsStore = Symbol('TaSettingsStore');
@@ -210,6 +211,9 @@ export class TaSettingsStoreImpl implements TaSettingsStore {
210
211
  @Autowired(TaSessionRegistry)
211
212
  protected readonly sessions: TaSessionRegistry;
212
213
 
214
+ @Autowired(PiDirectories)
215
+ protected readonly directories: PiDirectories;
216
+
213
217
  /** One write at a time, so that two changes of one file both stay. */
214
218
  protected writing: Promise<unknown> = Promise.resolve();
215
219
 
@@ -262,7 +266,9 @@ export class TaSettingsStoreImpl implements TaSettingsStore {
262
266
  * `trusted`: pi's decision for the project, or false when it has resources pi asks about first;
263
267
  * undefined when nobody decided and there is nothing to ask about, as pi trusts it then.
264
268
  */
265
- protected async files(cwd: string): Promise<{ userFile: string; projectFile: string; trusted?: boolean; trust(): void }> {
269
+ protected async files(location: string): Promise<{ userFile: string; projectFile: string; trusted?: boolean; trust(): void }> {
270
+ // The project of a directory of another machine is the directory here that pi works in for it.
271
+ const cwd = this.directories.toPi(location);
266
272
  const sdk = await PiSdkUtil.load();
267
273
  const agentDir = this.codingAgentOptions?.agentDir ?? sdk.getAgentDir();
268
274
  const store = new sdk.ProjectTrustStore(agentDir);
@@ -296,7 +302,7 @@ export class TaSettingsStoreImpl implements TaSettingsStore {
296
302
 
297
303
  protected async reloadSessions(scope: TaSettingsScope, cwd: string): Promise<void> {
298
304
  for (const session of this.sessions.list()) {
299
- if (scope === 'user' || session.runtime.cwd === cwd) {
305
+ if (scope === 'user' || session.cwd === cwd) {
300
306
  const manager = session.pi.settingsManager;
301
307
  if (scope === 'project' && !manager.isProjectTrusted()) {
302
308
  manager.setProjectTrusted(true);
@@ -1,4 +1,5 @@
1
1
  import { existsSync } from 'node:fs';
2
+ import { hostname, userInfo } from 'node:os';
2
3
  import { PiSdkUtil } from '@celljs/ai-coding-agent';
3
4
  import { Autowired } from '@celljs/core';
4
5
  import { Rpc } from '@celljs/rpc';
@@ -21,6 +22,7 @@ import {
21
22
  type ThinkingLevel,
22
23
  } from '@celljs/ta-protocol';
23
24
  import { TaSessionRegistry } from './host-protocol';
25
+ import { RemoteHosts } from './remote/remote-protocol';
24
26
  import { SessionInfoUtil } from './session-info-util';
25
27
  import { TaErrors } from './ta-errors';
26
28
  import type { TaSession } from './ta-session';
@@ -42,15 +44,27 @@ export class TaServerImpl implements TaServer {
42
44
  @Autowired(TaWorkingDirectory)
43
45
  protected readonly workingDirectory: TaWorkingDirectory;
44
46
 
47
+ @Autowired(RemoteHosts)
48
+ protected readonly remoteHosts: RemoteHosts;
49
+
45
50
  async getServerInfo(): Promise<TaServerInfo> {
46
- return this.guard(async () => ({ version: TaBrand.version(), cwd: await this.environments.defaultCwd() }));
51
+ return this.guard(async () => ({
52
+ version: TaBrand.version(),
53
+ cwd: await this.environments.defaultCwd(),
54
+ user: userInfo().username,
55
+ host: hostname().replace(/\.local$/, ''),
56
+ }));
47
57
  }
48
58
 
49
59
  async setWorkingDirectory(cwd: string): Promise<TaServerInfo> {
50
60
  return this.guard(async () => {
51
- const path = typeof cwd === 'string' ? cwd.trim() : '';
52
- if (!path || !(await this.environments.create(path).stat(path))?.isDirectory) {
53
- throw TaErrors.invalidArgument(`${cwd} is not a directory.`, 'Pass the absolute path of a directory that exists on the server.');
61
+ const path = typeof cwd === 'string' && cwd.trim() ? await this.remoteHosts.resolve(cwd.trim()) : '';
62
+ const env = path ? this.environments.create(path) : undefined;
63
+ if (!env || !(await env.stat(env.cwd))?.isDirectory) {
64
+ throw TaErrors.invalidArgument(
65
+ `${cwd} is not a directory.`,
66
+ 'Pass the absolute path of a directory that exists on the server, or an ssh:// location.',
67
+ );
54
68
  }
55
69
  this.workingDirectory.set(path);
56
70
  return this.getServerInfo();
@@ -1,3 +1,4 @@
1
+ import { resolve, sep } from 'node:path';
1
2
  import {
2
3
  AgentExtension,
3
4
  AgentTool,
@@ -12,10 +13,12 @@ import { Autowired, Component, Optional, Value } from '@celljs/core';
12
13
  import { type ExecEnvironment, ExecEnvironmentFactory } from '@celljs/ta-exec';
13
14
  import {
14
15
  BrandExtension,
16
+ type ContextFile,
15
17
  ExecExtension,
16
18
  ExecMcpTransport,
17
19
  type InlineExtension,
18
20
  type McpServerEntry,
21
+ RemoteContextFiles,
19
22
  SessionLock,
20
23
  SessionLockExtension,
21
24
  TaExtensionUtil,
@@ -26,9 +29,11 @@ import { ClientToolBroker } from './client-tool-broker';
26
29
  import { TA_HOST_OPTIONS, type TaHostOptions, TaSessionFactory } from './host-protocol';
27
30
  import { PackageHostExtension } from './package-host-extension';
28
31
  import { PackageHosts } from './package-hosts';
32
+ import { PiDirectories } from './remote/remote-protocol';
29
33
  import { TaErrors } from './ta-errors';
30
34
  import { TaSession } from './ta-session';
31
35
 
36
+ type AgentsFilesOverride = (base: { agentsFiles: ContextFile[] }) => { agentsFiles: ContextFile[] };
32
37
  type ModelRuntime = import('@earendil-works/pi-coding-agent', { with: { 'resolution-mode': 'import' }}).ModelRuntime;
33
38
  type SessionManager = import('@earendil-works/pi-coding-agent', { with: { 'resolution-mode': 'import' }}).SessionManager;
34
39
  type Model = NonNullable<import('@earendil-works/pi-coding-agent', { with: { 'resolution-mode': 'import' }}).AgentSession['model']>;
@@ -59,12 +64,18 @@ export class DefaultTaSessionFactory implements TaSessionFactory {
59
64
  @Autowired(PackageHosts)
60
65
  protected readonly packageHosts: PackageHosts;
61
66
 
67
+ @Autowired(PiDirectories)
68
+ protected readonly directories: PiDirectories;
69
+
62
70
  async create(options: TaOpenSessionOptions, requestClose: (session: TaSession) => void): Promise<TaSession> {
63
71
  const clientTools = new ClientToolBroker(options.clientTools);
64
72
  const sdk = await PiSdkUtil.load();
65
73
  const agentDir = this.codingAgentOptions?.agentDir ?? sdk.getAgentDir();
66
74
  const modelRuntime = await this.modelRuntimeProvider.provide();
67
- const sessionManager = this.openSessionManager(sdk, options);
75
+ // `options.cwd` is a location; pi works in a directory of this machine (PiDirectories). A session
76
+ // file works where its header says, as pi's `--session` does, whatever `cwd` the caller gave.
77
+ const sessionManager = this.openSessionManager(sdk, { ...options, cwd: this.directories.toPi(options.cwd) });
78
+ const piCwd = options.sessionFile ? sessionManager.getCwd() : this.directories.toPi(options.cwd);
68
79
  const lock = new SessionLock();
69
80
  const sessionFile = sessionManager.getSessionFile();
70
81
  if (sessionFile && !lock.acquire(sessionFile)) {
@@ -74,6 +85,7 @@ export class DefaultTaSessionFactory implements TaSessionFactory {
74
85
  const model = options.model ? this.resolveModel(modelRuntime, options.model) : undefined;
75
86
  const runtime = await sdk.createAgentSessionRuntime(
76
87
  async ({ cwd, agentDir: dir, sessionManager: manager, sessionStartEvent }) => {
88
+ const environment = this.environments.create(this.directories.fromPi(cwd));
77
89
  const settingsManager = sdk.SettingsManager.create(cwd, dir, { projectTrusted: this.isTrusted(sdk, cwd, dir) });
78
90
  const services = await sdk.createAgentSessionServices({
79
91
  cwd,
@@ -82,7 +94,8 @@ export class DefaultTaSessionFactory implements TaSessionFactory {
82
94
  modelRuntime,
83
95
  resourceLoaderOptions: {
84
96
  ...TaPackages.loaderOptions(dir),
85
- extensionFactories: this.extensionFactories(sdk, cwd, lock),
97
+ ...(await this.contextFiles(environment, dir)),
98
+ extensionFactories: this.extensionFactories(sdk, environment, cwd, lock),
86
99
  appendSystemPrompt: options.appendSystemPrompt ? [options.appendSystemPrompt] : undefined,
87
100
  },
88
101
  });
@@ -96,9 +109,17 @@ export class DefaultTaSessionFactory implements TaSessionFactory {
96
109
  });
97
110
  return { ...created, services, diagnostics: services.diagnostics };
98
111
  },
99
- { cwd: options.cwd, agentDir, sessionManager },
112
+ { cwd: piCwd, agentDir, sessionManager },
100
113
  );
101
- const session = new TaSession({ runtime, lock, clientTools, options: this.options, requestClose, group: options.group });
114
+ const session = new TaSession({
115
+ runtime,
116
+ lock,
117
+ clientTools,
118
+ options: this.options,
119
+ requestClose,
120
+ group: options.group,
121
+ locate: cwd => this.directories.fromPi(cwd),
122
+ });
102
123
  await session.start();
103
124
  return session;
104
125
  } catch (error) {
@@ -121,13 +142,25 @@ export class DefaultTaSessionFactory implements TaSessionFactory {
121
142
  }
122
143
  }
123
144
 
124
- protected extensionFactories(sdk: PiSdk, cwd: string, lock: SessionLock): InlineExtension[] {
125
- const environment = this.environments.create(cwd);
145
+ /**
146
+ * For another machine, its context files (AGENTS.md...) instead of those pi finds above the directory
147
+ * of this machine it works in; the user's own, in the agent directory, stay.
148
+ */
149
+ protected async contextFiles(environment: ExecEnvironment, agentDir: string): Promise<{ agentsFilesOverride?: AgentsFilesOverride }> {
150
+ if (environment.local) {
151
+ return {};
152
+ }
153
+ const remote = await RemoteContextFiles.load(environment).catch(() => []);
154
+ const own = resolve(agentDir) + sep;
155
+ return { agentsFilesOverride: ({ agentsFiles }) => ({ agentsFiles: [...agentsFiles.filter(file => resolve(file.path).startsWith(own)), ...remote] }) };
156
+ }
157
+
158
+ protected extensionFactories(sdk: PiSdk, environment: ExecEnvironment, piCwd: string, lock: SessionLock): InlineExtension[] {
126
159
  const builtIn = [
127
160
  new SessionLockExtension(lock),
128
161
  new PackageHostExtension(this.packageHosts),
129
162
  new BrandExtension(sdk),
130
- ...(environment.local ? [] : [new ExecExtension(environment, sdk)]),
163
+ ...(environment.local ? [] : [new ExecExtension(environment, sdk, piCwd)]),
131
164
  ];
132
165
  return [
133
166
  ...this.piExtensions(sdk, environment),