-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathshell.ts
More file actions
463 lines (403 loc) · 12.6 KB
/
Copy pathshell.ts
File metadata and controls
463 lines (403 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
import { exec } from "node:child_process";
import { existsSync } from "node:fs";
import { homedir, platform } from "node:os";
import {
ROOT_LOGGER,
type RootLogger,
type ScopedLogger,
} from "@posthog/di/logger";
import {
type IWorkspaceSettings,
WORKSPACE_SETTINGS_SERVICE,
} from "@posthog/platform/workspace-settings";
import { TypedEventEmitter } from "@posthog/shared";
import { POSTHOG_CODE_INTERNAL_CHILD_ENV } from "@posthog/shared/constants";
import { inject, injectable, preDestroy } from "inversify";
import * as pty from "node-pty";
import {
REPOSITORY_REPOSITORY,
WORKSPACE_REPOSITORY,
WORKTREE_REPOSITORY,
} from "../../db/identifiers";
import type { RepositoryRepository } from "../../db/repositories/repository-repository";
import type { WorkspaceRepository } from "../../db/repositories/workspace-repository";
import type { WorktreeRepository } from "../../db/repositories/worktree-repository";
import { buildWorkspaceEnv } from "../../workspace-env";
import { PROCESS_TRACKING_SERVICE } from "../process-tracking/identifiers";
import type { ProcessTrackingService } from "../process-tracking/process-tracking";
import { deriveWorktreePath as deriveWorktreePathFromBase } from "../worktree-path/worktree-path";
import { type ExecuteOutput, ShellEvent, type ShellEvents } from "./schemas";
// node-pty exposes destroy() at runtime but it's missing from type definitions
declare module "node-pty" {
interface IPty {
destroy(): void;
}
}
const PTY_ENCODING = "utf8";
const DESTROYED_EXIT_CODE = 130;
export interface ShellSession {
pty: pty.IPty;
exitPromise: Promise<{ exitCode: number }>;
command?: string;
disposables: pty.IDisposable[];
}
function getDefaultShell(): string {
if (platform() === "win32") {
return process.env.COMSPEC || "cmd.exe";
}
return process.env.SHELL || "/bin/bash";
}
function getShellArgs(shell: string): string[] {
if (platform() === "win32") {
const lower = shell.toLowerCase();
if (lower.includes("powershell") || lower.includes("pwsh")) {
return ["-NoLogo"];
}
return [];
}
return ["-l"];
}
function buildShellEnv(
additionalEnv?: Record<string, string>,
): Record<string, string> {
const env = { ...process.env } as Record<string, string>;
// User-facing shells must not inherit the workspace-server's internal
// markers: ELECTRON_RUN_AS_NODE makes any Electron-based CLI run as node,
// and the internal-child marker makes the packaged app refuse to launch.
delete env.ELECTRON_RUN_AS_NODE;
delete env[POSTHOG_CODE_INTERNAL_CHILD_ENV];
if (platform() === "darwin" && !process.env.LC_ALL) {
const locale = process.env.LC_CTYPE || "en_US.UTF-8";
Object.assign(env, {
LANG: locale,
LC_ALL: locale,
LC_MESSAGES: locale,
LC_NUMERIC: locale,
LC_COLLATE: locale,
LC_MONETARY: locale,
});
}
Object.assign(env, {
TERM_PROGRAM: "PostHog Code",
COLORTERM: "truecolor",
FORCE_COLOR: "3",
...additionalEnv,
});
return env;
}
export interface CreateSessionOptions {
sessionId: string;
cwd?: string;
taskId?: string;
initialCommand?: string;
additionalEnv?: Record<string, string>;
}
@injectable()
export class ShellService extends TypedEventEmitter<ShellEvents> {
private sessions = new Map<string, ShellSession>();
private processTracking: ProcessTrackingService;
private repositoryRepo: RepositoryRepository;
private workspaceRepo: WorkspaceRepository;
private worktreeRepo: WorktreeRepository;
private readonly log: ScopedLogger;
constructor(
@inject(PROCESS_TRACKING_SERVICE)
processTracking: ProcessTrackingService,
@inject(REPOSITORY_REPOSITORY)
repositoryRepo: RepositoryRepository,
@inject(WORKSPACE_REPOSITORY)
workspaceRepo: WorkspaceRepository,
@inject(WORKTREE_REPOSITORY)
worktreeRepo: WorktreeRepository,
@inject(WORKSPACE_SETTINGS_SERVICE)
private readonly workspaceSettings: IWorkspaceSettings,
@inject(ROOT_LOGGER)
logger: RootLogger,
) {
super();
this.processTracking = processTracking;
this.repositoryRepo = repositoryRepo;
this.workspaceRepo = workspaceRepo;
this.worktreeRepo = worktreeRepo;
this.log = logger.scope("shell");
}
private deriveWorktreePath(folderPath: string, worktreeName: string): string {
return deriveWorktreePathFromBase(
this.workspaceSettings.getWorktreeLocation(),
folderPath,
worktreeName,
);
}
async create(
sessionId: string,
cwd?: string,
taskId?: string,
): Promise<void> {
await this.createSession({ sessionId, cwd, taskId });
}
async createSession(options: CreateSessionOptions): Promise<ShellSession> {
const { sessionId, cwd, taskId, initialCommand, additionalEnv } = options;
const existing = this.sessions.get(sessionId);
if (existing) {
return existing;
}
const taskEnv = await this.getTaskEnv(taskId);
const mergedEnv = { ...taskEnv, ...additionalEnv };
const workingDir = this.resolveWorkingDir(sessionId, cwd);
const shell = getDefaultShell();
const ptyProcess = pty.spawn(shell, getShellArgs(shell), {
name: "xterm-256color",
cols: 80,
rows: 24,
cwd: workingDir,
env: buildShellEnv(mergedEnv),
encoding: PTY_ENCODING,
});
this.processTracking.register(
ptyProcess.pid,
"shell",
`shell:${sessionId}`,
{ sessionId, cwd: workingDir },
taskId,
);
let resolveExit: (result: { exitCode: number }) => void;
const exitPromise = new Promise<{ exitCode: number }>((resolve) => {
resolveExit = resolve;
});
const disposables: pty.IDisposable[] = [];
disposables.push(
ptyProcess.onData((data: string) => {
this.emit(ShellEvent.Data, { sessionId, data });
}),
);
disposables.push(
ptyProcess.onExit(({ exitCode }) => {
this.processTracking.unregister(ptyProcess.pid, "exited");
const session = this.sessions.get(sessionId);
if (session) {
for (const d of session.disposables) {
d.dispose();
}
session.pty.destroy();
this.sessions.delete(sessionId);
}
this.emit(ShellEvent.Exit, { sessionId, exitCode });
resolveExit({ exitCode });
}),
);
if (initialCommand) {
setTimeout(() => {
ptyProcess.write(`${initialCommand}\n`);
}, 100);
}
const session: ShellSession = {
pty: ptyProcess,
exitPromise,
command: initialCommand,
disposables,
};
this.sessions.set(sessionId, session);
return session;
}
async createCommandSession(options: {
sessionId: string;
command: string;
cwd: string;
taskId?: string;
}): Promise<void> {
const { sessionId, command, cwd, taskId } = options;
const existing = this.sessions.get(sessionId);
if (existing) {
return;
}
const taskEnv = await this.getTaskEnv(taskId);
const workingDir = this.resolveWorkingDir(sessionId, cwd);
const shell = getDefaultShell();
const ptyProcess = pty.spawn(shell, ["-c", command], {
name: "xterm-256color",
cols: 80,
rows: 24,
cwd: workingDir,
env: buildShellEnv(taskEnv),
encoding: PTY_ENCODING,
});
this.processTracking.register(
ptyProcess.pid,
"shell",
`shell:${sessionId}`,
{ sessionId, cwd: workingDir, command },
taskId,
);
let resolveExit: (result: { exitCode: number }) => void;
const exitPromise = new Promise<{ exitCode: number }>((resolve) => {
resolveExit = resolve;
});
const disposables: pty.IDisposable[] = [];
disposables.push(
ptyProcess.onData((data: string) => {
this.emit(ShellEvent.Data, { sessionId, data });
}),
);
disposables.push(
ptyProcess.onExit(({ exitCode }) => {
this.processTracking.unregister(ptyProcess.pid, "exited");
const session = this.sessions.get(sessionId);
if (session) {
for (const d of session.disposables) {
d.dispose();
}
session.pty.destroy();
this.sessions.delete(sessionId);
}
this.emit(ShellEvent.Exit, { sessionId, exitCode });
resolveExit({ exitCode });
}),
);
const session: ShellSession = {
pty: ptyProcess,
exitPromise,
command,
disposables,
};
this.sessions.set(sessionId, session);
}
write(sessionId: string, data: string): void {
this.getSessionOrThrow(sessionId).pty.write(data);
}
resize(sessionId: string, cols: number, rows: number): void {
this.getSessionOrThrow(sessionId).pty.resize(cols, rows);
}
check(sessionId: string): boolean {
return this.sessions.has(sessionId);
}
hasSession(sessionId: string): boolean {
return this.sessions.has(sessionId);
}
getSession(sessionId: string): ShellSession | undefined {
return this.sessions.get(sessionId);
}
getSessionsByPrefix(prefix: string): string[] {
const result: string[] = [];
for (const sessionId of this.sessions.keys()) {
if (sessionId.startsWith(prefix)) {
result.push(sessionId);
}
}
return result;
}
destroyByPrefix(prefix: string): void {
for (const sessionId of this.sessions.keys()) {
if (sessionId.startsWith(prefix)) {
this.destroy(sessionId);
}
}
}
destroy(sessionId: string): void {
const session = this.sessions.get(sessionId);
if (session) {
const pid = session.pty.pid;
this.processTracking.kill(pid);
for (const disposable of session.disposables) {
disposable.dispose();
}
session.pty.destroy();
this.sessions.delete(sessionId);
this.emit(ShellEvent.Exit, {
sessionId,
exitCode: DESTROYED_EXIT_CODE,
});
}
}
/**
* Destroy all active shell sessions.
* Used during application shutdown to ensure all child processes are cleaned up.
*/
@preDestroy()
destroyAll(): void {
for (const sessionId of this.sessions.keys()) {
this.destroy(sessionId);
}
}
/**
* Get the count of active sessions.
*/
getSessionCount(): number {
return this.sessions.size;
}
getProcess(sessionId: string): string | null {
return this.sessions.get(sessionId)?.pty.process ?? null;
}
execute(cwd: string, command: string): Promise<ExecuteOutput> {
return new Promise((resolve) => {
exec(
command,
{ cwd, timeout: 60000, env: buildShellEnv(), windowsHide: true },
(error, stdout, stderr) => {
resolve({
stdout: stdout || "",
stderr: stderr || "",
exitCode: error?.code ?? 0,
});
},
);
});
}
private getSessionOrThrow(sessionId: string): ShellSession {
const session = this.sessions.get(sessionId);
if (!session) {
throw new Error(`Shell session ${sessionId} not found`);
}
return session;
}
private resolveWorkingDir(sessionId: string, cwd?: string): string {
const home = homedir();
const workingDir = cwd || home;
if (!existsSync(workingDir)) {
this.log.warn(
`Shell session ${sessionId}: cwd "${workingDir}" does not exist, falling back to home`,
);
return home;
}
return workingDir;
}
private async getTaskEnv(
taskId?: string,
): Promise<Record<string, string> | undefined> {
if (!taskId) return undefined;
const workspace = this.workspaceRepo.findByTaskId(taskId);
if (!workspace || workspace.mode === "cloud" || !workspace.repositoryId) {
return undefined;
}
const repo = this.repositoryRepo.findById(workspace.repositoryId);
if (!repo) return undefined;
let worktreePath: string | null = null;
let worktreeName: string | null = null;
if (workspace.mode === "worktree") {
const worktree = this.worktreeRepo.findByWorkspaceId(workspace.id);
if (worktree) {
worktreeName = worktree.name;
// The stored path is authoritative — reused worktrees can live
// outside the managed worktree directory. Only derive when the
// stored path is gone (e.g. stale row after a location move).
worktreePath = existsSync(worktree.path)
? worktree.path
: this.deriveWorktreePath(repo.path, worktreeName);
}
}
try {
return await buildWorkspaceEnv({
taskId,
folderPath: repo.path,
worktreePath,
worktreeName,
mode: workspace.mode,
});
} catch (error) {
this.log.warn(
`Failed to build workspace env for task ${taskId}, starting shell without it`,
error,
);
return undefined;
}
}
}