fix(linux): start Hopper demo sessions headlessly

This commit is contained in:
morluto
2026-07-14 04:58:45 +08:00
parent 08d5b913b0
commit 75818d119e
18 changed files with 816 additions and 169 deletions
+69 -21
View File
@@ -44,6 +44,7 @@ export interface HopperApplicationLauncherOptions {
readonly targetKind: "executable" | "database";
readonly loaderArgs: readonly string[];
readonly bridgeScriptPath: string;
readonly demoHelperPath?: string;
}
/**
@@ -101,26 +102,16 @@ export class HopperApplicationLauncher implements BridgeLauncher {
options.signal,
);
if (!prepared) return err(new HopperCancelledError());
const child = spawn(this.options.launcherPath, args, {
stdio: ["ignore", "ignore", "pipe"],
detached: process.platform !== "win32",
env: { ...process.env, REA_PROCESS_RUN_ID: session.runId },
});
const started = await new Promise<Result<undefined, HopperStartError>>(
(resolve) => {
const onSpawn = (): void => {
child.off("error", onError);
resolve(ok(undefined));
};
const onError = (cause: Error): void => {
child.off("spawn", onSpawn);
resolve(err(new HopperStartError({ cause })));
};
child.once("spawn", onSpawn);
child.once("error", onError);
},
const linuxDemo = linuxDemoLaunch(this.options, session, args);
const ownershipCommand =
linuxDemo?.ownershipCommand ?? this.options.launcherPath;
const started = await spawnLauncher(
linuxDemo?.command ?? this.options.launcherPath,
linuxDemo?.args ?? args,
session.runId,
);
if (!started.ok) return started;
const child = started.value;
const pid = child.pid;
if (pid === undefined)
return err(
@@ -132,7 +123,7 @@ export class HopperApplicationLauncher implements BridgeLauncher {
pid,
process_group_id: pid,
parent_pid: process.pid,
launcher: this.options.launcherPath,
launcher: linuxDemo?.command ?? this.options.launcherPath,
created_at: new Date().toISOString(),
};
try {
@@ -143,7 +134,7 @@ export class HopperApplicationLauncher implements BridgeLauncher {
);
} catch (cause: unknown) {
await cleanupOwnedProcessGroup(
ownedProcessGroup(session, pid, this.options.launcherPath),
ownedProcessGroup(session, pid, ownershipCommand),
);
return err(new HopperStartError({ cause }));
}
@@ -152,7 +143,7 @@ export class HopperApplicationLauncher implements BridgeLauncher {
ownsProcessLifetime: true,
cleanup: () =>
cleanupOwnedProcessGroup(
ownedProcessGroup(session, pid, this.options.launcherPath),
ownedProcessGroup(session, pid, ownershipCommand),
),
});
} catch (cause: unknown) {
@@ -161,6 +152,63 @@ export class HopperApplicationLauncher implements BridgeLauncher {
}
}
const linuxDemoLaunch = (
options: HopperApplicationLauncherOptions,
session: BridgeSession,
hopperArgs: readonly string[],
):
| {
readonly command: string;
readonly args: readonly string[];
readonly ownershipCommand: string;
}
| undefined => {
if (
process.platform !== "linux" ||
basename(options.launcherPath) !== "Hopper" ||
options.demoHelperPath === undefined
)
return undefined;
return {
command: "/usr/bin/python3",
ownershipCommand: "/usr/bin/python3",
args: [
options.demoHelperPath,
"--hopper",
options.launcherPath,
"--socket",
session.socketPath,
"--",
options.launcherPath,
...hopperArgs,
],
};
};
const spawnLauncher = async (
command: string,
args: readonly string[],
runId: string,
): Promise<Result<ChildProcess, HopperStartError>> => {
const child = spawn(command, args, {
stdio: ["ignore", "ignore", "pipe"],
detached: process.platform !== "win32",
env: { ...process.env, REA_PROCESS_RUN_ID: runId },
});
return new Promise((resolve) => {
const onSpawn = (): void => {
child.off("error", onError);
resolve(ok(child));
};
const onError = (cause: Error): void => {
child.off("spawn", onSpawn);
resolve(err(new HopperStartError({ cause })));
};
child.once("spawn", onSpawn);
child.once("error", onError);
});
};
const ownedProcessGroup = (
session: BridgeSession,
pid: number,
+5
View File
@@ -70,6 +70,7 @@ export class HopperClient {
#nextId = 1;
#buffer = "";
#closing = false;
#launcherExitCode: number | null | undefined;
#startPromise: Promise<Result<HopperServerInfo, HopperError>> | undefined;
#closePromise: Promise<void> | undefined;
@@ -102,6 +103,7 @@ export class HopperClient {
}
const socketPath = join(this.#directory, "bridge.sock");
this.#token = randomBytes(32).toString("hex");
this.#launcherExitCode = undefined;
const runId = randomUUID();
const launched = await this.#options.launcher.launch(
{
@@ -210,6 +212,8 @@ export class HopperClient {
while (Date.now() < deadline) {
if (signal?.aborted === true) return err(new HopperCancelledError());
if (this.#closing) return err(new HopperProcessError(null));
if (this.#launcherExitCode === 78)
return err(new HopperProcessError(this.#launcherExitCode));
try {
await access(socketPath);
} catch {
@@ -305,6 +309,7 @@ export class HopperClient {
});
});
launch.process.on("exit", (code) => {
this.#launcherExitCode = code;
this.#options.onDiagnostic?.({ type: "launcher-exit", code });
if (launch.ownsProcessLifetime && !this.#closing) {
this.#failAll(new HopperProcessError(code));
+3
View File
@@ -114,6 +114,9 @@ export class HopperProvider implements AnalysisProvider {
bridgeScriptPath: fileURLToPath(
new URL("../../bridge/hopper_bridge.py", import.meta.url),
),
demoHelperPath: fileURLToPath(
new URL("../../scripts/hopper-demo-x11.py", import.meta.url),
),
}),
logger: this.logger.child({ layer: "bridge" }),
});