@celljs/ta-host 4.6.1 → 4.8.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 (245) hide show
  1. package/README.en.md +70 -0
  2. package/README.md +16 -3
  3. package/cell.schema.json +21 -0
  4. package/cell.yml +7 -0
  5. package/esm/node/auth-server-impl.d.ts +2 -0
  6. package/esm/node/auth-server-impl.js +7 -0
  7. package/esm/node/auth-server-impl.js.map +1 -1
  8. package/esm/node/exec-environment-factory.d.ts +8 -4
  9. package/esm/node/exec-environment-factory.js +28 -6
  10. package/esm/node/exec-environment-factory.js.map +1 -1
  11. package/esm/node/extension-providers.d.ts +22 -0
  12. package/esm/node/extension-providers.js +52 -0
  13. package/esm/node/extension-providers.js.map +1 -0
  14. package/esm/node/index.d.ts +3 -0
  15. package/esm/node/index.js +3 -0
  16. package/esm/node/index.js.map +1 -1
  17. package/esm/node/package-host-extension.d.ts +4 -2
  18. package/esm/node/package-host-extension.js +4 -3
  19. package/esm/node/package-host-extension.js.map +1 -1
  20. package/esm/node/package-hosts.d.ts +6 -2
  21. package/esm/node/package-hosts.js +32 -0
  22. package/esm/node/package-hosts.js.map +1 -1
  23. package/esm/node/remote/index.d.ts +5 -0
  24. package/esm/node/remote/index.js +6 -0
  25. package/esm/node/remote/index.js.map +1 -0
  26. package/esm/node/remote/pi-directories.d.ts +14 -0
  27. package/esm/node/remote/pi-directories.js +64 -0
  28. package/esm/node/remote/pi-directories.js.map +1 -0
  29. package/esm/node/remote/port-forwards.d.ts +35 -0
  30. package/esm/node/remote/port-forwards.js +150 -0
  31. package/esm/node/remote/port-forwards.js.map +1 -0
  32. package/esm/node/remote/remote-hosts.d.ts +40 -0
  33. package/esm/node/remote/remote-hosts.js +170 -0
  34. package/esm/node/remote/remote-hosts.js.map +1 -0
  35. package/esm/node/remote/remote-protocol.d.ts +51 -0
  36. package/esm/node/remote/remote-protocol.js +5 -0
  37. package/esm/node/remote/remote-protocol.js.map +1 -0
  38. package/esm/node/remote/remote-server-impl.d.ts +19 -0
  39. package/esm/node/remote/remote-server-impl.js +66 -0
  40. package/esm/node/remote/remote-server-impl.js.map +1 -0
  41. package/esm/node/remote-ui-context.d.ts +6 -1
  42. package/esm/node/remote-ui-context.js +31 -3
  43. package/esm/node/remote-ui-context.js.map +1 -1
  44. package/esm/node/session-files.d.ts +18 -0
  45. package/esm/node/session-files.js +32 -0
  46. package/esm/node/session-files.js.map +1 -0
  47. package/esm/node/settings-server-impl.d.ts +1 -0
  48. package/esm/node/settings-server-impl.js +8 -0
  49. package/esm/node/settings-server-impl.js.map +1 -1
  50. package/esm/node/settings-store.d.ts +9 -2
  51. package/esm/node/settings-store.js +26 -5
  52. package/esm/node/settings-store.js.map +1 -1
  53. package/esm/node/ta-server-impl.d.ts +2 -0
  54. package/esm/node/ta-server-impl.js +16 -4
  55. package/esm/node/ta-server-impl.js.map +1 -1
  56. package/esm/node/ta-session-factory.d.ts +19 -2
  57. package/esm/node/ta-session-factory.js +54 -11
  58. package/esm/node/ta-session-factory.js.map +1 -1
  59. package/esm/node/ta-session.d.ts +4 -0
  60. package/esm/node/ta-session.js +6 -1
  61. package/esm/node/ta-session.js.map +1 -1
  62. package/esm/node/terminal-service.d.ts +18 -2
  63. package/esm/node/terminal-service.js +46 -10
  64. package/esm/node/terminal-service.js.map +1 -1
  65. package/esm/node/terminal-util.d.ts +14 -0
  66. package/esm/node/terminal-util.js +30 -1
  67. package/esm/node/terminal-util.js.map +1 -1
  68. package/esm/node/web-extension-catalog.d.ts +3 -1
  69. package/esm/node/web-extension-catalog.js +8 -2
  70. package/esm/node/web-extension-catalog.js.map +1 -1
  71. package/esm/node/working-directory.d.ts +12 -1
  72. package/esm/node/working-directory.js +22 -1
  73. package/esm/node/working-directory.js.map +1 -1
  74. package/esm/node/workspace-files.d.ts +1 -1
  75. package/esm/node/workspace-files.js +3 -3
  76. package/esm/node/workspace-files.js.map +1 -1
  77. package/esm/node/workspace-protocol.d.ts +2 -0
  78. package/esm/node/workspace-server-impl.d.ts +2 -1
  79. package/esm/node/workspace-server-impl.js +13 -1
  80. package/esm/node/workspace-server-impl.js.map +1 -1
  81. package/esm/node/workspace-service.d.ts +1 -0
  82. package/esm/node/workspace-service.js +15 -3
  83. package/esm/node/workspace-service.js.map +1 -1
  84. package/lib/node/auth-server-impl.d.ts +2 -0
  85. package/lib/node/auth-server-impl.d.ts.map +1 -1
  86. package/lib/node/auth-server-impl.js +7 -0
  87. package/lib/node/auth-server-impl.js.map +1 -1
  88. package/lib/node/exec-environment-factory.d.ts +8 -4
  89. package/lib/node/exec-environment-factory.d.ts.map +1 -1
  90. package/lib/node/exec-environment-factory.js +28 -6
  91. package/lib/node/exec-environment-factory.js.map +1 -1
  92. package/lib/node/extension-providers.d.ts +23 -0
  93. package/lib/node/extension-providers.d.ts.map +1 -0
  94. package/lib/node/extension-providers.js +55 -0
  95. package/lib/node/extension-providers.js.map +1 -0
  96. package/lib/node/index.d.ts +3 -0
  97. package/lib/node/index.d.ts.map +1 -1
  98. package/lib/node/index.js +3 -0
  99. package/lib/node/index.js.map +1 -1
  100. package/lib/node/package-host-extension.d.ts +4 -2
  101. package/lib/node/package-host-extension.d.ts.map +1 -1
  102. package/lib/node/package-host-extension.js +4 -3
  103. package/lib/node/package-host-extension.js.map +1 -1
  104. package/lib/node/package-hosts.d.ts +6 -2
  105. package/lib/node/package-hosts.d.ts.map +1 -1
  106. package/lib/node/package-hosts.js +32 -0
  107. package/lib/node/package-hosts.js.map +1 -1
  108. package/lib/node/remote/index.d.ts +6 -0
  109. package/lib/node/remote/index.d.ts.map +1 -0
  110. package/lib/node/remote/index.js +22 -0
  111. package/lib/node/remote/index.js.map +1 -0
  112. package/lib/node/remote/pi-directories.d.ts +15 -0
  113. package/lib/node/remote/pi-directories.d.ts.map +1 -0
  114. package/lib/node/remote/pi-directories.js +67 -0
  115. package/lib/node/remote/pi-directories.js.map +1 -0
  116. package/lib/node/remote/port-forwards.d.ts +36 -0
  117. package/lib/node/remote/port-forwards.d.ts.map +1 -0
  118. package/lib/node/remote/port-forwards.js +153 -0
  119. package/lib/node/remote/port-forwards.js.map +1 -0
  120. package/lib/node/remote/remote-hosts.d.ts +41 -0
  121. package/lib/node/remote/remote-hosts.d.ts.map +1 -0
  122. package/lib/node/remote/remote-hosts.js +173 -0
  123. package/lib/node/remote/remote-hosts.js.map +1 -0
  124. package/lib/node/remote/remote-protocol.d.ts +52 -0
  125. package/lib/node/remote/remote-protocol.d.ts.map +1 -0
  126. package/lib/node/remote/remote-protocol.js +8 -0
  127. package/lib/node/remote/remote-protocol.js.map +1 -0
  128. package/lib/node/remote/remote-server-impl.d.ts +20 -0
  129. package/lib/node/remote/remote-server-impl.d.ts.map +1 -0
  130. package/lib/node/remote/remote-server-impl.js +69 -0
  131. package/lib/node/remote/remote-server-impl.js.map +1 -0
  132. package/lib/node/remote/remote.spec.d.ts +2 -0
  133. package/lib/node/remote/remote.spec.d.ts.map +1 -0
  134. package/lib/node/remote/remote.spec.js +130 -0
  135. package/lib/node/remote/remote.spec.js.map +1 -0
  136. package/lib/node/remote-ui-context.d.ts +6 -1
  137. package/lib/node/remote-ui-context.d.ts.map +1 -1
  138. package/lib/node/remote-ui-context.js +31 -3
  139. package/lib/node/remote-ui-context.js.map +1 -1
  140. package/lib/node/remote-ui-context.spec.js +23 -0
  141. package/lib/node/remote-ui-context.spec.js.map +1 -1
  142. package/lib/node/session-files.d.ts +19 -0
  143. package/lib/node/session-files.d.ts.map +1 -0
  144. package/lib/node/session-files.js +36 -0
  145. package/lib/node/session-files.js.map +1 -0
  146. package/lib/node/session-files.spec.d.ts +2 -0
  147. package/lib/node/session-files.spec.d.ts.map +1 -0
  148. package/lib/node/session-files.spec.js +51 -0
  149. package/lib/node/session-files.spec.js.map +1 -0
  150. package/lib/node/settings-server-impl.d.ts +1 -0
  151. package/lib/node/settings-server-impl.d.ts.map +1 -1
  152. package/lib/node/settings-server-impl.js +8 -0
  153. package/lib/node/settings-server-impl.js.map +1 -1
  154. package/lib/node/settings-store.d.ts +9 -2
  155. package/lib/node/settings-store.d.ts.map +1 -1
  156. package/lib/node/settings-store.js +26 -5
  157. package/lib/node/settings-store.js.map +1 -1
  158. package/lib/node/settings-store.spec.js +5 -2
  159. package/lib/node/settings-store.spec.js.map +1 -1
  160. package/lib/node/ta-server-impl.d.ts +2 -0
  161. package/lib/node/ta-server-impl.d.ts.map +1 -1
  162. package/lib/node/ta-server-impl.js +16 -4
  163. package/lib/node/ta-server-impl.js.map +1 -1
  164. package/lib/node/ta-session-factory.d.ts +19 -2
  165. package/lib/node/ta-session-factory.d.ts.map +1 -1
  166. package/lib/node/ta-session-factory.js +53 -10
  167. package/lib/node/ta-session-factory.js.map +1 -1
  168. package/lib/node/ta-session.d.ts +4 -0
  169. package/lib/node/ta-session.d.ts.map +1 -1
  170. package/lib/node/ta-session.js +6 -1
  171. package/lib/node/ta-session.js.map +1 -1
  172. package/lib/node/terminal-service.d.ts +18 -2
  173. package/lib/node/terminal-service.d.ts.map +1 -1
  174. package/lib/node/terminal-service.js +45 -9
  175. package/lib/node/terminal-service.js.map +1 -1
  176. package/lib/node/terminal-service.spec.js +31 -0
  177. package/lib/node/terminal-service.spec.js.map +1 -1
  178. package/lib/node/terminal-util.d.ts +14 -0
  179. package/lib/node/terminal-util.d.ts.map +1 -1
  180. package/lib/node/terminal-util.js +30 -1
  181. package/lib/node/terminal-util.js.map +1 -1
  182. package/lib/node/terminal-util.spec.js +24 -0
  183. package/lib/node/terminal-util.spec.js.map +1 -1
  184. package/lib/node/web-extension-catalog.d.ts +3 -1
  185. package/lib/node/web-extension-catalog.d.ts.map +1 -1
  186. package/lib/node/web-extension-catalog.js +7 -1
  187. package/lib/node/web-extension-catalog.js.map +1 -1
  188. package/lib/node/web-extension-catalog.spec.js +1 -1
  189. package/lib/node/web-extension-catalog.spec.js.map +1 -1
  190. package/lib/node/working-directory.d.ts +12 -1
  191. package/lib/node/working-directory.d.ts.map +1 -1
  192. package/lib/node/working-directory.js +22 -1
  193. package/lib/node/working-directory.js.map +1 -1
  194. package/lib/node/workspace-files.d.ts +1 -1
  195. package/lib/node/workspace-files.d.ts.map +1 -1
  196. package/lib/node/workspace-files.js +3 -3
  197. package/lib/node/workspace-files.js.map +1 -1
  198. package/lib/node/workspace-protocol.d.ts +2 -0
  199. package/lib/node/workspace-protocol.d.ts.map +1 -1
  200. package/lib/node/workspace-server-impl.d.ts +2 -1
  201. package/lib/node/workspace-server-impl.d.ts.map +1 -1
  202. package/lib/node/workspace-server-impl.js +12 -0
  203. package/lib/node/workspace-server-impl.js.map +1 -1
  204. package/lib/node/workspace-service.d.ts +1 -0
  205. package/lib/node/workspace-service.d.ts.map +1 -1
  206. package/lib/node/workspace-service.js +15 -3
  207. package/lib/node/workspace-service.js.map +1 -1
  208. package/lib/node/workspace-service.spec.js +9 -0
  209. package/lib/node/workspace-service.spec.js.map +1 -1
  210. package/package.json +14 -10
  211. package/src/node/auth-server-impl.ts +6 -0
  212. package/src/node/exec-environment-factory.ts +26 -6
  213. package/src/node/extension-providers.ts +49 -0
  214. package/src/node/index.ts +3 -0
  215. package/src/node/package-host-extension.ts +7 -4
  216. package/src/node/package-hosts.ts +32 -2
  217. package/src/node/remote/index.ts +5 -0
  218. package/src/node/remote/pi-directories.ts +49 -0
  219. package/src/node/remote/port-forwards.ts +148 -0
  220. package/src/node/remote/remote-hosts.ts +170 -0
  221. package/src/node/remote/remote-protocol.ts +57 -0
  222. package/src/node/remote/remote-server-impl.ts +60 -0
  223. package/src/node/remote/remote.spec.ts +142 -0
  224. package/src/node/remote-ui-context.spec.ts +25 -0
  225. package/src/node/remote-ui-context.ts +34 -5
  226. package/src/node/session-files.spec.ts +52 -0
  227. package/src/node/session-files.ts +41 -0
  228. package/src/node/settings-server-impl.ts +9 -0
  229. package/src/node/settings-store.spec.ts +5 -2
  230. package/src/node/settings-store.ts +25 -6
  231. package/src/node/ta-server-impl.ts +18 -4
  232. package/src/node/ta-session-factory.ts +56 -10
  233. package/src/node/ta-session.ts +9 -1
  234. package/src/node/terminal-service.spec.ts +35 -0
  235. package/src/node/terminal-service.ts +47 -15
  236. package/src/node/terminal-util.spec.ts +31 -0
  237. package/src/node/terminal-util.ts +32 -1
  238. package/src/node/web-extension-catalog.spec.ts +1 -1
  239. package/src/node/web-extension-catalog.ts +7 -2
  240. package/src/node/working-directory.ts +35 -2
  241. package/src/node/workspace-files.ts +3 -3
  242. package/src/node/workspace-protocol.ts +2 -0
  243. package/src/node/workspace-server-impl.ts +18 -0
  244. package/src/node/workspace-service.spec.ts +11 -1
  245. package/src/node/workspace-service.ts +15 -3
@@ -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,
@@ -78,8 +78,10 @@ describe('TaSettingsStoreImpl', () => {
78
78
  store = new TaSettingsStoreImpl();
79
79
  Object.assign(store, {
80
80
  codingAgentOptions: { agentDir },
81
- modelRuntimeProvider: { provide: async () => ({ getAvailable: async () => [{ provider: 'fake', id: 'fake-1' }] }) },
81
+ modelRuntimeProvider: { provide: async () => ({ getAvailable: async () => [{ provider: 'fake', id: 'fake-1', name: 'Fake One' }] }) },
82
+ extensionProviders: { ensure: async () => undefined },
82
83
  sessions: { list: () => [session(cwd), session(join(root, 'other'))] },
84
+ directories: { toPi: (location: string) => location, fromPi: (dir: string) => dir },
83
85
  });
84
86
  });
85
87
  afterEach(() => rmSync(root, { recursive: true, force: true }));
@@ -89,6 +91,7 @@ describe('TaSettingsStoreImpl', () => {
89
91
  const snapshot = await store.update('user', { 'compaction.enabled': false, model: 'fake/fake-1' }, cwd);
90
92
  expect(snapshot.user).toEqual({ model: 'fake/fake-1', 'compaction.enabled': false });
91
93
  expect(snapshot.models).toEqual(['fake/fake-1']);
94
+ expect(snapshot.modelNames).toEqual({ 'fake/fake-1': 'Fake One' });
92
95
  expect(JSON.parse(readFileSync(join(agentDir, 'settings.json'), 'utf8'))).toEqual({
93
96
  theme: 'dark',
94
97
  packages: ['npm:x'],
@@ -3,7 +3,9 @@ import { dirname, join } from 'node:path';
3
3
  import { CODING_AGENT_OPTIONS, type CodingAgentOptions, ModelRuntimeProvider, PiSdkUtil } from '@celljs/ai-coding-agent';
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
+ import { ExtensionProviders } from './extension-providers';
6
7
  import { TaSessionRegistry } from './host-protocol';
8
+ import { PiDirectories } from './remote/remote-protocol';
7
9
  import { TaErrors } from './ta-errors';
8
10
 
9
11
  export const TaSettingsStore = Symbol('TaSettingsStore');
@@ -210,12 +212,18 @@ export class TaSettingsStoreImpl implements TaSettingsStore {
210
212
  @Autowired(TaSessionRegistry)
211
213
  protected readonly sessions: TaSessionRegistry;
212
214
 
215
+ @Autowired(ExtensionProviders)
216
+ protected readonly extensionProviders: ExtensionProviders;
217
+
218
+ @Autowired(PiDirectories)
219
+ protected readonly directories: PiDirectories;
220
+
213
221
  /** One write at a time, so that two changes of one file both stay. */
214
222
  protected writing: Promise<unknown> = Promise.resolve();
215
223
 
216
224
  async get(cwd: string): Promise<TaSettingsSnapshot> {
217
225
  const { userFile, projectFile, trusted } = await this.files(cwd);
218
- const [user, project, models] = await Promise.all([this.read(userFile), this.read(projectFile), this.models()]);
226
+ const [user, project, { models, modelNames }] = await Promise.all([this.read(userFile), this.read(projectFile), this.models()]);
219
227
  return {
220
228
  user: TaSettingsUtil.read(user),
221
229
  project: TaSettingsUtil.read(project),
@@ -224,6 +232,7 @@ export class TaSettingsStoreImpl implements TaSettingsStore {
224
232
  projectFile,
225
233
  projectTrusted: trusted !== false,
226
234
  models,
235
+ modelNames,
227
236
  };
228
237
  }
229
238
 
@@ -262,7 +271,9 @@ export class TaSettingsStoreImpl implements TaSettingsStore {
262
271
  * `trusted`: pi's decision for the project, or false when it has resources pi asks about first;
263
272
  * undefined when nobody decided and there is nothing to ask about, as pi trusts it then.
264
273
  */
265
- protected async files(cwd: string): Promise<{ userFile: string; projectFile: string; trusted?: boolean; trust(): void }> {
274
+ protected async files(location: string): Promise<{ userFile: string; projectFile: string; trusted?: boolean; trust(): void }> {
275
+ // The project of a directory of another machine is the directory here that pi works in for it.
276
+ const cwd = this.directories.toPi(location);
266
277
  const sdk = await PiSdkUtil.load();
267
278
  const agentDir = this.codingAgentOptions?.agentDir ?? sdk.getAgentDir();
268
279
  const store = new sdk.ProjectTrustStore(agentDir);
@@ -285,18 +296,26 @@ export class TaSettingsStoreImpl implements TaSettingsStore {
285
296
  return TaSettingsUtil.parse(text, file);
286
297
  }
287
298
 
288
- protected async models(): Promise<string[]> {
299
+ protected async models(): Promise<{ models: string[]; modelNames: Record<string, string> }> {
289
300
  try {
290
301
  const runtime = await this.modelRuntimeProvider.provide();
291
- return (await runtime.getAvailable()).map(model => `${model.provider}/${model.id}`);
302
+ await this.extensionProviders.ensure();
303
+ const available = await runtime.getAvailable();
304
+ const modelNames: Record<string, string> = {};
305
+ for (const model of available) {
306
+ if (model.name && model.name !== model.id) {
307
+ modelNames[`${model.provider}/${model.id}`] = model.name;
308
+ }
309
+ }
310
+ return { models: available.map(model => `${model.provider}/${model.id}`), modelNames };
292
311
  } catch {
293
- return [];
312
+ return { models: [], modelNames: {} };
294
313
  }
295
314
  }
296
315
 
297
316
  protected async reloadSessions(scope: TaSettingsScope, cwd: string): Promise<void> {
298
317
  for (const session of this.sessions.list()) {
299
- if (scope === 'user' || session.runtime.cwd === cwd) {
318
+ if (scope === 'user' || session.cwd === cwd) {
300
319
  const manager = session.pi.settingsManager;
301
320
  if (scope === 'project' && !manager.isProjectTrusted()) {
302
321
  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,
@@ -23,12 +26,16 @@ import {
23
26
  } from '@celljs/ta-harness';
24
27
  import type { TaOpenSessionOptions } from '@celljs/ta-protocol';
25
28
  import { ClientToolBroker } from './client-tool-broker';
29
+ import { ExtensionProviders } from './extension-providers';
26
30
  import { TA_HOST_OPTIONS, type TaHostOptions, TaSessionFactory } from './host-protocol';
27
31
  import { PackageHostExtension } from './package-host-extension';
28
32
  import { PackageHosts } from './package-hosts';
33
+ import { PiDirectories } from './remote/remote-protocol';
34
+ import { ExecSessionFiles } from './session-files';
29
35
  import { TaErrors } from './ta-errors';
30
36
  import { TaSession } from './ta-session';
31
37
 
38
+ type AgentsFilesOverride = (base: { agentsFiles: ContextFile[] }) => { agentsFiles: ContextFile[] };
32
39
  type ModelRuntime = import('@earendil-works/pi-coding-agent', { with: { 'resolution-mode': 'import' }}).ModelRuntime;
33
40
  type SessionManager = import('@earendil-works/pi-coding-agent', { with: { 'resolution-mode': 'import' }}).SessionManager;
34
41
  type Model = NonNullable<import('@earendil-works/pi-coding-agent', { with: { 'resolution-mode': 'import' }}).AgentSession['model']>;
@@ -59,21 +66,35 @@ export class DefaultTaSessionFactory implements TaSessionFactory {
59
66
  @Autowired(PackageHosts)
60
67
  protected readonly packageHosts: PackageHosts;
61
68
 
69
+ @Autowired(PiDirectories)
70
+ protected readonly directories: PiDirectories;
71
+
72
+ @Autowired(ExtensionProviders)
73
+ protected readonly extensionProviders: ExtensionProviders;
74
+
62
75
  async create(options: TaOpenSessionOptions, requestClose: (session: TaSession) => void): Promise<TaSession> {
63
76
  const clientTools = new ClientToolBroker(options.clientTools);
64
77
  const sdk = await PiSdkUtil.load();
65
78
  const agentDir = this.codingAgentOptions?.agentDir ?? sdk.getAgentDir();
66
79
  const modelRuntime = await this.modelRuntimeProvider.provide();
67
- const sessionManager = this.openSessionManager(sdk, options);
80
+ // `options.cwd` is a location; pi works in a directory of this machine (PiDirectories). A session
81
+ // file works where its header says, as pi's `--session` does, whatever `cwd` the caller gave.
82
+ const sessionManager = this.openSessionManager(sdk, { ...options, cwd: this.directories.toPi(options.cwd) });
83
+ const piCwd = options.sessionFile ? sessionManager.getCwd() : this.directories.toPi(options.cwd);
68
84
  const lock = new SessionLock();
69
85
  const sessionFile = sessionManager.getSessionFile();
70
86
  if (sessionFile && !lock.acquire(sessionFile)) {
71
87
  throw TaErrors.sessionLocked(sessionFile);
72
88
  }
73
89
  try {
90
+ if (options.model && !this.findModel(modelRuntime, options.model)) {
91
+ // A model of a pi package, whose provider is there once the package loaded.
92
+ await this.extensionProviders.ensure();
93
+ }
74
94
  const model = options.model ? this.resolveModel(modelRuntime, options.model) : undefined;
75
95
  const runtime = await sdk.createAgentSessionRuntime(
76
96
  async ({ cwd, agentDir: dir, sessionManager: manager, sessionStartEvent }) => {
97
+ const environment = this.environments.create(this.directories.fromPi(cwd));
77
98
  const settingsManager = sdk.SettingsManager.create(cwd, dir, { projectTrusted: this.isTrusted(sdk, cwd, dir) });
78
99
  const services = await sdk.createAgentSessionServices({
79
100
  cwd,
@@ -82,7 +103,8 @@ export class DefaultTaSessionFactory implements TaSessionFactory {
82
103
  modelRuntime,
83
104
  resourceLoaderOptions: {
84
105
  ...TaPackages.loaderOptions(dir),
85
- extensionFactories: this.extensionFactories(sdk, cwd, lock),
106
+ ...(await this.contextFiles(environment, dir)),
107
+ extensionFactories: this.extensionFactories(sdk, environment, cwd, lock),
86
108
  appendSystemPrompt: options.appendSystemPrompt ? [options.appendSystemPrompt] : undefined,
87
109
  },
88
110
  });
@@ -96,9 +118,17 @@ export class DefaultTaSessionFactory implements TaSessionFactory {
96
118
  });
97
119
  return { ...created, services, diagnostics: services.diagnostics };
98
120
  },
99
- { cwd: options.cwd, agentDir, sessionManager },
121
+ { cwd: piCwd, agentDir, sessionManager },
100
122
  );
101
- const session = new TaSession({ runtime, lock, clientTools, options: this.options, requestClose, group: options.group });
123
+ const session = new TaSession({
124
+ runtime,
125
+ lock,
126
+ clientTools,
127
+ options: this.options,
128
+ requestClose,
129
+ group: options.group,
130
+ locate: cwd => this.directories.fromPi(cwd),
131
+ });
102
132
  await session.start();
103
133
  return session;
104
134
  } catch (error) {
@@ -121,13 +151,25 @@ export class DefaultTaSessionFactory implements TaSessionFactory {
121
151
  }
122
152
  }
123
153
 
124
- protected extensionFactories(sdk: PiSdk, cwd: string, lock: SessionLock): InlineExtension[] {
125
- const environment = this.environments.create(cwd);
154
+ /**
155
+ * For another machine, its context files (AGENTS.md...) instead of those pi finds above the directory
156
+ * of this machine it works in; the user's own, in the agent directory, stay.
157
+ */
158
+ protected async contextFiles(environment: ExecEnvironment, agentDir: string): Promise<{ agentsFilesOverride?: AgentsFilesOverride }> {
159
+ if (environment.local) {
160
+ return {};
161
+ }
162
+ const remote = await RemoteContextFiles.load(environment).catch(() => []);
163
+ const own = resolve(agentDir) + sep;
164
+ return { agentsFilesOverride: ({ agentsFiles }) => ({ agentsFiles: [...agentsFiles.filter(file => resolve(file.path).startsWith(own)), ...remote] }) };
165
+ }
166
+
167
+ protected extensionFactories(sdk: PiSdk, environment: ExecEnvironment, piCwd: string, lock: SessionLock): InlineExtension[] {
126
168
  const builtIn = [
127
169
  new SessionLockExtension(lock),
128
- new PackageHostExtension(this.packageHosts),
170
+ new PackageHostExtension(this.packageHosts, new ExecSessionFiles(environment, piCwd)),
129
171
  new BrandExtension(sdk),
130
- ...(environment.local ? [] : [new ExecExtension(environment, sdk)]),
172
+ ...(environment.local ? [] : [new ExecExtension(environment, sdk, piCwd)]),
131
173
  ];
132
174
  return [
133
175
  ...this.piExtensions(sdk, environment),
@@ -159,9 +201,13 @@ export class DefaultTaSessionFactory implements TaSessionFactory {
159
201
  return new sdk.ProjectTrustStore(agentDir).get(cwd) ?? !sdk.hasTrustRequiringProjectResources(cwd);
160
202
  }
161
203
 
162
- protected resolveModel(modelRuntime: ModelRuntime, ref: string): Model {
204
+ protected findModel(modelRuntime: ModelRuntime, ref: string): Model | undefined {
163
205
  const index = ref.indexOf('/');
164
- const model = index > 0 ? modelRuntime.getModel(ref.slice(0, index), ref.slice(index + 1)) : undefined;
206
+ return index > 0 ? modelRuntime.getModel(ref.slice(0, index), ref.slice(index + 1)) : undefined;
207
+ }
208
+
209
+ protected resolveModel(modelRuntime: ModelRuntime, ref: string): Model {
210
+ const model = this.findModel(modelRuntime, ref);
165
211
  if (!model) {
166
212
  throw TaErrors.modelNotFound(ref);
167
213
  }
@@ -33,6 +33,8 @@ export interface TaSessionContext {
33
33
  requestClose(session: TaSession): void;
34
34
  /** TaOpenSessionOptions.group. */
35
35
  group?: string;
36
+ /** The location of the directory pi works in (PiDirectories.fromPi); default: that directory. */
37
+ locate?(cwd: string): string;
36
38
  }
37
39
 
38
40
  /**
@@ -62,6 +64,11 @@ export class TaSession {
62
64
  return this.runtime.session;
63
65
  }
64
66
 
67
+ /** Where the session works, as clients name it: a path, or an `ssh://` location. */
68
+ get cwd(): string {
69
+ return this.context.locate ? this.context.locate(this.runtime.cwd) : this.runtime.cwd;
70
+ }
71
+
65
72
  async start(): Promise<void> {
66
73
  this.runtime.setRebindSession(() => this.bind());
67
74
  await this.bind();
@@ -76,7 +83,7 @@ export class TaSession {
76
83
  return {
77
84
  sessionId: this.id,
78
85
  piSessionId: pi.sessionId,
79
- cwd: this.runtime.cwd,
86
+ cwd: this.cwd,
80
87
  sessionFile: pi.sessionFile,
81
88
  sessionName: pi.sessionName,
82
89
  firstMessage: SessionInfoUtil.firstMessage(pi.messages),
@@ -98,6 +105,7 @@ export class TaSession {
98
105
  info: this.info(),
99
106
  events,
100
107
  pendingUi: this.ui.pendingRequests,
108
+ shownUi: this.ui.shownRequests,
101
109
  pendingClientTools: this.context.clientTools.pendingRequests,
102
110
  lastSeq: this.log.lastSeq,
103
111
  gap,
@@ -67,15 +67,50 @@ describe('DefaultTaTerminals', () => {
67
67
  expect(terminals.list(as(connection))).toEqual([]);
68
68
  });
69
69
 
70
+ it('sizes the shell as its view asked, even before the shell named its terminal', async () => {
71
+ const connection = new FakeConnection();
72
+ const info = await terminals.open(as(connection), { cwd, cols: 80, rows: 24 });
73
+ if (!info.pty) {
74
+ return;
75
+ }
76
+ await terminals.resize(as(connection), info.terminalId, 101, 31);
77
+ await Bun.sleep(300);
78
+ terminals.write(as(connection), info.terminalId, 'stty size\r');
79
+ await until(() => connection.output.includes('31 101'), 'the shell has the size of its view');
80
+ });
81
+
82
+ it('asks each machine for its own shell and system', async () => {
83
+ const asked: string[] = [];
84
+ const service = new DefaultTaTerminals();
85
+ const detect = (service as unknown as { detect(env: { exec: unknown; local: boolean }, machine: string): Promise<{ system: string }> }).detect.bind(
86
+ service,
87
+ );
88
+ const env = (system: string) => ({
89
+ local: false,
90
+ exec: async (_command: string, options: { onData(data: Uint8Array): void }) => {
91
+ asked.push(system);
92
+ options.onData(new TextEncoder().encode(`${system}\n/usr/bin/script\n/bin/zsh`));
93
+ return { exitCode: 0 };
94
+ },
95
+ });
96
+ expect((await detect(env('Linux'), 'box')).system).toBe('Linux');
97
+ expect((await detect(env('Darwin'), '')).system).toBe('Darwin');
98
+ expect((await detect(env('Darwin'), 'box')).system).toBe('Linux');
99
+ expect(asked).toEqual(['Linux', 'Darwin']);
100
+ });
101
+
70
102
  it('keeps a shell for another connection after its own dropped', async () => {
71
103
  const first = new FakeConnection();
72
104
  const info = await terminals.open(as(first), { cwd, cols: 80, rows: 24 });
73
105
  const stranger = new FakeConnection();
74
106
  expect(terminals.list(as(stranger))).toEqual([]);
75
107
  expect(() => terminals.write(as(stranger), info.terminalId, 'x')).toThrow('No terminal');
108
+ terminals.write(as(first), info.terminalId, 'echo first-$((1+2))\r');
109
+ await until(() => first.output.includes('first-3'), 'the first connection got the output');
76
110
  first.close();
77
111
  const second = new FakeConnection();
78
112
  expect(terminals.list(as(second))).toEqual([info]);
113
+ await until(() => second.output.startsWith('\x1bc') && second.output.includes('first-3'), 'the second connection got what the shell showed before');
79
114
  terminals.write(as(second), info.terminalId, 'echo back-$((1+1))\r');
80
115
  await until(() => second.output.includes('back-2'), 'the second connection got the output');
81
116
  await terminals.close(as(second), info.terminalId);
@@ -1,7 +1,7 @@
1
1
  import { type Application, ApplicationLifecycle, Autowired, Component } from '@celljs/core';
2
2
  import type { RpcConnection } from '@celljs/rpc/lib/node';
3
3
  import { type ExecEnvironment, ExecEnvironmentFactory, type ExecProcess } from '@celljs/ta-exec';
4
- import { TaClient, type TaOpenTerminalOptions, type TaTerminalInfo } from '@celljs/ta-protocol';
4
+ import { TaClient, TaLocation, type TaOpenTerminalOptions, type TaTerminalInfo } from '@celljs/ta-protocol';
5
5
  import { TaErrors } from './ta-errors';
6
6
  import { TaTerminals } from './terminal-protocol';
7
7
  import { PipeDiscipline, type TerminalPlatform, TerminalUtil, TtyMarker } from './terminal-util';
@@ -19,12 +19,16 @@ class Terminal {
19
19
  owner?: RpcConnection;
20
20
  orphanTimer?: ReturnType<typeof setTimeout>;
21
21
  pending = '';
22
+ /** The last output, for a page that takes the shell again after its own went (a reload). */
23
+ history = '';
22
24
  flushTimer?: ReturnType<typeof setTimeout>;
23
25
  sending = false;
24
26
  /** The code the shell ended with, as it said, or as its process did. */
25
27
  exitCode?: number;
26
28
  /** Its process ended. */
27
29
  exited = false;
30
+ /** The size its view asked for last; it waits for the name of the terminal when that is not known yet. */
31
+ size?: { cols: number; rows: number };
28
32
  /** Its client knows it ended: nothing more goes. */
29
33
  ended = false;
30
34
  readonly decoder = new TextDecoder();
@@ -51,16 +55,17 @@ export class DefaultTaTerminals implements TaTerminals, ApplicationLifecycle<App
51
55
 
52
56
  protected readonly terminals = new Map<string, Terminal>();
53
57
  protected readonly watched = new WeakSet<RpcConnection>();
54
- protected platform?: Promise<TerminalPlatform>;
58
+ /** By machine: `''` for this one (or the exec server), else the host of `ssh://`. */
59
+ protected readonly platforms = new Map<string, Promise<TerminalPlatform>>();
55
60
  protected nextId = 0;
56
61
 
57
62
  async open(connection: RpcConnection, options: TaOpenTerminalOptions & { cwd: string }): Promise<TaTerminalInfo> {
58
63
  const env = this.environments.create(options.cwd);
59
- const stat = await env.stat(options.cwd);
64
+ const stat = await env.stat(env.cwd);
60
65
  if (!stat?.isDirectory) {
61
66
  throw TaErrors.invalidArgument(`${options.cwd} is not a directory.`, 'Open the terminal in a directory that exists.');
62
67
  }
63
- const platform = await this.detect(env);
68
+ const platform = await this.detect(env, this.machine(options.cwd));
64
69
  const launch = TerminalUtil.launch(platform, TerminalUtil.size(options.cols, DEFAULT_COLS), TerminalUtil.size(options.rows, DEFAULT_ROWS));
65
70
  // `pty`: false when the shell runs on pipes, so that the view can say why some programs behave otherwise.
66
71
  const info: TaTerminalInfo & { pty: boolean } = { terminalId: `t${++this.nextId}`, cwd: options.cwd, shell: platform.shell, pty: launch.pty };
@@ -89,13 +94,14 @@ export class DefaultTaTerminals implements TaTerminals, ApplicationLifecycle<App
89
94
 
90
95
  async resize(connection: RpcConnection, terminalId: string, cols: number, rows: number): Promise<void> {
91
96
  const terminal = this.require(connection, terminalId);
92
- if (terminal.marker.tty) {
93
- const command = TerminalUtil.resizeCommand(
94
- terminal.platform,
95
- terminal.marker.tty,
96
- TerminalUtil.size(cols, DEFAULT_COLS),
97
- TerminalUtil.size(rows, DEFAULT_ROWS),
98
- );
97
+ terminal.size = { cols: TerminalUtil.size(cols, DEFAULT_COLS), rows: TerminalUtil.size(rows, DEFAULT_ROWS) };
98
+ await this.applySize(terminal);
99
+ }
100
+
101
+ /** A view sizes its shell as soon as it shows, often before the shell named its terminal: the size waits for it. */
102
+ protected async applySize(terminal: Terminal): Promise<void> {
103
+ if (terminal.marker.tty && terminal.size) {
104
+ const command = TerminalUtil.resizeCommand(terminal.platform, terminal.marker.tty, terminal.size.cols, terminal.size.rows);
99
105
  await terminal.env.exec(command, { timeout: 5000 });
100
106
  }
101
107
  }
@@ -140,10 +146,15 @@ export class DefaultTaTerminals implements TaTerminals, ApplicationLifecycle<App
140
146
  return terminal;
141
147
  }
142
148
 
149
+ /**
150
+ * A connection takes a shell nobody has. What its page shows went with the page: it gets the last
151
+ * output again, after a reset, so that a page that kept its screen does not show it twice.
152
+ */
143
153
  protected adopt(terminal: Terminal, connection: RpcConnection): void {
144
154
  clearTimeout(terminal.orphanTimer);
145
155
  terminal.orphanTimer = undefined;
146
156
  terminal.owner = connection;
157
+ terminal.pending = terminal.history ? `${TerminalUtil.RESET}${terminal.history}` : terminal.pending;
147
158
  this.watch(connection);
148
159
  this.schedule(terminal);
149
160
  }
@@ -166,7 +177,11 @@ export class DefaultTaTerminals implements TaTerminals, ApplicationLifecycle<App
166
177
  protected output(terminal: Terminal, text: string): void {
167
178
  const { text: rest, exitCode } = TerminalUtil.takeExitCode(text);
168
179
  terminal.exitCode ??= exitCode;
180
+ const named = terminal.marker.tty !== undefined;
169
181
  const shown = terminal.marker.feed(rest);
182
+ if (!named && terminal.marker.tty !== undefined) {
183
+ void this.applySize(terminal).catch(() => undefined);
184
+ }
170
185
  this.queue(terminal, terminal.discipline ? terminal.discipline.output(shown) : shown);
171
186
  }
172
187
 
@@ -176,8 +191,9 @@ export class DefaultTaTerminals implements TaTerminals, ApplicationLifecycle<App
176
191
  }
177
192
  terminal.pending += text;
178
193
  if (terminal.pending.length > MAX_PENDING_CHARS) {
179
- terminal.pending = terminal.pending.slice(-MAX_PENDING_CHARS);
194
+ terminal.pending = TerminalUtil.tail(terminal.pending, MAX_PENDING_CHARS);
180
195
  }
196
+ terminal.history = TerminalUtil.tail(terminal.history + text, MAX_PENDING_CHARS);
181
197
  this.schedule(terminal);
182
198
  }
183
199
 
@@ -243,8 +259,21 @@ export class DefaultTaTerminals implements TaTerminals, ApplicationLifecycle<App
243
259
  terminal.process.kill();
244
260
  }
245
261
 
246
- protected detect(env: ExecEnvironment): Promise<TerminalPlatform> {
247
- this.platform ??= (async () => {
262
+ /** The machine of a location: its shell, `script` and system are those of that machine. */
263
+ protected machine(location: string): string {
264
+ try {
265
+ return TaLocation.parse(location)?.host ?? '';
266
+ } catch {
267
+ return '';
268
+ }
269
+ }
270
+
271
+ protected detect(env: ExecEnvironment, machine: string): Promise<TerminalPlatform> {
272
+ let platform = this.platforms.get(machine);
273
+ if (platform) {
274
+ return platform;
275
+ }
276
+ platform = (async () => {
248
277
  let output = '';
249
278
  const decoder = new TextDecoder();
250
279
  const { exitCode } = await env.exec(`${TerminalUtil.PROBE} 2>/dev/null`, {
@@ -254,6 +283,9 @@ export class DefaultTaTerminals implements TaTerminals, ApplicationLifecycle<App
254
283
  const fallback = env.local && process.platform === 'win32' ? (process.env.ComSpec ?? 'cmd.exe') : '/bin/sh';
255
284
  return exitCode === 0 ? TerminalUtil.parsePlatform(output, fallback) : { system: '', script: false, shell: fallback };
256
285
  })();
257
- return this.platform;
286
+ // A machine that could not answer is asked again next time.
287
+ platform.catch(() => this.platforms.delete(machine));
288
+ this.platforms.set(machine, platform);
289
+ return platform;
258
290
  }
259
291
  }
@@ -76,3 +76,34 @@ describe('PipeDiscipline', () => {
76
76
  expect(new PipeDiscipline().output('a\nb\r\n')).toBe('a\r\nb\r\n');
77
77
  });
78
78
  });
79
+
80
+ describe('TerminalUtil.tail', () => {
81
+ it('keeps the end, from the start of a line', () => {
82
+ expect(TerminalUtil.tail('abc', 10)).toBe('abc');
83
+ expect(TerminalUtil.tail('one\r\n\x1b[31mtwo\r\nthree', 12)).toBe('three');
84
+ expect(TerminalUtil.tail('abcdefgh', 3)).toBe('fgh');
85
+ });
86
+ });
87
+
88
+ describe('TerminalUtil.UTF8_LOCALE', () => {
89
+ const ctype = (env: Record<string, string>) =>
90
+ new TextDecoder()
91
+ .decode(
92
+ Bun.spawnSync(['/bin/sh', '-c', `${TerminalUtil.UTF8_LOCALE}; printf '%s|%s' "$LC_CTYPE" "$LC_ALL"`], {
93
+ env: { PATH: process.env.PATH ?? '/usr/bin:/bin', ...env },
94
+ }).stdout,
95
+ )
96
+ .split('|');
97
+
98
+ it('gives a shell without a UTF-8 locale one for its characters', () => {
99
+ const [lcCtype, lcAll] = ctype({ LANG: 'C' });
100
+ expect(lcCtype).toMatch(/utf-?8$/i);
101
+ expect(lcAll).toBe('');
102
+ expect(ctype({ LC_ALL: 'POSIX' }).every(value => /utf-?8$/i.test(value))).toBe(true);
103
+ });
104
+
105
+ it('leaves a UTF-8 locale as it is', () => {
106
+ expect(ctype({ LANG: 'zh_CN.UTF-8' })).toEqual(['', '']);
107
+ expect(ctype({ LC_CTYPE: 'en_US.utf8' })).toEqual(['en_US.utf8', '']);
108
+ });
109
+ });
@@ -29,6 +29,35 @@ const TTY_SEARCH_CHARS = 4096;
29
29
  * shell runs on pipes, and {@link PipeDiscipline} does what the terminal would.
30
30
  */
31
31
  export namespace TerminalUtil {
32
+ /** A full reset of the terminal (RIS): its screen, scrollback and modes. */
33
+ export const RESET = '\x1bc';
34
+
35
+ /**
36
+ * At most `max` characters of the end of `text`, from the start of a line when it is cut, so that it
37
+ * does not start inside a control sequence or a character.
38
+ */
39
+ export function tail(text: string, max: number): string {
40
+ if (text.length <= max) {
41
+ return text;
42
+ }
43
+ const cut = text.slice(-max);
44
+ const line = cut.indexOf('\n');
45
+ return line < 0 ? cut : cut.slice(line + 1);
46
+ }
47
+
48
+ /**
49
+ * A shell started by ssh or by an app of the desktop often has no locale (`C`): it then reads
50
+ * Chinese, typed or pasted, as bytes it cannot show (`<ffffffff>`), and programs print `?`. When
51
+ * the locale is not UTF-8, the shell gets one for its characters (LC_CTYPE, and LC_ALL when set):
52
+ * C.UTF-8 where the system has it, else en_US.UTF-8; its messages and formats stay as they were.
53
+ */
54
+ export const UTF8_LOCALE = [
55
+ 'case "${LC_ALL:-${LC_CTYPE:-$LANG}}" in *[Uu][Tt][Ff]-8*|*[Uu][Tt][Ff]8*) ;; *)',
56
+ "ta_utf8=$(locale -a 2>/dev/null | grep -i -x -m 1 -E 'c\\.utf-?8|en_us\\.utf-?8'); ta_utf8=${ta_utf8:-en_US.UTF-8};",
57
+ '[ -n "$LC_ALL" ] && export LC_ALL="$ta_utf8"; export LC_CTYPE="$ta_utf8"; unset ta_utf8;;',
58
+ 'esac',
59
+ ].join(' ');
60
+
32
61
  /** The command that tells the platform: three lines. */
33
62
  export const PROBE = 'uname -s; command -v script; printf %s "$SHELL"';
34
63
 
@@ -47,8 +76,10 @@ export namespace TerminalUtil {
47
76
  const boot = [
48
77
  // macOS's bash 3.2 otherwise asks to switch to zsh on each start.
49
78
  'export TERM=xterm-256color BASH_SILENCE_DEPRECATION_WARNING=1',
50
- `printf '\\033]1337;ta-tty=%s\\007' "$(tty)"`,
79
+ UTF8_LOCALE,
80
+ // Sized before it says its name: a resize that waited for the name comes after this size.
51
81
  `stty cols ${cols} rows ${rows} 2>/dev/null`,
82
+ `printf '\\033]1337;ta-tty=%s\\007' "$(tty)"`,
52
83
  `exec ${quote(platform.shell)} -i`,
53
84
  ].join('; ');
54
85
  let script: string | undefined;
@@ -69,7 +69,7 @@ describe('WebExtensionCatalogImpl', () => {
69
69
  mkdirSync(cwd);
70
70
  const agentDir = join(home, 'agent');
71
71
  catalog = new WebExtensionCatalogImpl();
72
- Object.assign(catalog, { codingAgentOptions: { agentDir } });
72
+ Object.assign(catalog, { codingAgentOptions: { agentDir }, directories: { toPi: (location: string) => location, fromPi: (dir: string) => dir } });
73
73
  // A directory in extensions/, and a package of the packages setting elsewhere.
74
74
  write(join(agentDir, 'extensions/notes/package.json'), JSON.stringify({ name: 'notes', pi: { web: ['web.js'] } }));
75
75
  write(join(agentDir, 'extensions/notes/web.js'), WEB);