Skip to content

Commit 36ea849

Browse files
committed
🤖 refactor: simplify SSHRuntime tilde resolution to single cached method
1 parent 8e3de5d commit 36ea849

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

src/node/runtime/SSHRuntime.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ export interface SSHRuntimeConfig {
8080
export class SSHRuntime implements Runtime {
8181
private readonly config: SSHRuntimeConfig;
8282
private readonly controlPath: string;
83+
/** Cached resolved bgOutputDir (tilde expanded to absolute path) */
84+
private resolvedBgOutputDir: string | null = null;
8385

8486
constructor(config: SSHRuntimeConfig) {
8587
// Note: srcBaseDir may contain tildes - they will be resolved via resolvePath() before use
@@ -91,6 +93,28 @@ export class SSHRuntime implements Runtime {
9193
this.controlPath = getControlPath(config);
9294
}
9395

96+
/**
97+
* Get resolved background output directory (tilde expanded), caching the result.
98+
* This ensures all background process paths are absolute from the start.
99+
*/
100+
private async getBgOutputDir(): Promise<string> {
101+
if (this.resolvedBgOutputDir !== null) {
102+
return this.resolvedBgOutputDir;
103+
}
104+
105+
let dir = this.config.bgOutputDir ?? "/tmp/mux-bashes";
106+
107+
if (dir === "~" || dir.startsWith("~/")) {
108+
const result = await execBuffered(this, 'echo "$HOME"', { cwd: "/", timeout: 10 });
109+
const home =
110+
result.exitCode === 0 && result.stdout.trim() ? result.stdout.trim() : "/tmp";
111+
dir = dir === "~" ? home : `${home}/${dir.slice(2)}`;
112+
}
113+
114+
this.resolvedBgOutputDir = dir;
115+
return this.resolvedBgOutputDir;
116+
}
117+
94118
/**
95119
* Get SSH configuration (for PTY terminal spawning)
96120
*/
@@ -294,7 +318,7 @@ export class SSHRuntime implements Runtime {
294318
// Generate unique process ID and compute output directory
295319
// /tmp is cleaned by OS, so no explicit cleanup needed
296320
const processId = `bg-${randomBytes(4).toString("hex")}`;
297-
const bgOutputDir = this.config.bgOutputDir ?? "/tmp/mux-bashes";
321+
const bgOutputDir = await this.getBgOutputDir();
298322
const outputDir = `${bgOutputDir}/${options.workspaceId}/${processId}`;
299323
const stdoutPath = `${outputDir}/stdout.log`;
300324
const stderrPath = `${outputDir}/stderr.log`;
@@ -378,6 +402,7 @@ export class SSHRuntime implements Runtime {
378402

379403
log.debug(`SSHRuntime.spawnBackground: Spawned with PID ${pid}`);
380404

405+
// outputDir is already absolute (getBgOutputDir resolves tildes upfront)
381406
const handle = new SSHBackgroundHandle(this, pid, outputDir);
382407
return { success: true, handle, pid };
383408
} catch (error) {

0 commit comments

Comments
 (0)