fix(hopper): report launcher failures before bridge timeout (#1105)
* fix(hopper): report launcher failures before bridge timeout * fix(hopper): separate helper ownership from GUI lifetime
This commit is contained in:
@@ -45,6 +45,7 @@ class OwnedFixtureLauncher implements BridgeLauncher {
|
||||
return ok({
|
||||
process: started.process,
|
||||
ownsProcessLifetime: true as const,
|
||||
providerLifetime: "launcher-process" as const,
|
||||
ownership: started.ownership,
|
||||
shutdownMode: "process-cleanup" as const,
|
||||
cleanup: () => cleanupOwnedProcessGroup(started.ownership),
|
||||
@@ -68,6 +69,7 @@ class UnconfirmedFixtureLauncher implements BridgeLauncher {
|
||||
{ stdio: ["ignore", "ignore", "pipe"] },
|
||||
),
|
||||
ownsProcessLifetime: false,
|
||||
providerLifetime: "external-application" as const,
|
||||
shutdownMode: "bridge-request" as const,
|
||||
}),
|
||||
);
|
||||
@@ -90,6 +92,7 @@ class UnconfirmedOwnedFixtureLauncher implements BridgeLauncher {
|
||||
{ stdio: ["ignore", "ignore", "pipe"] },
|
||||
),
|
||||
ownsProcessLifetime: true as const,
|
||||
providerLifetime: "launcher-process" as const,
|
||||
shutdownMode: "bridge-request" as const,
|
||||
}),
|
||||
);
|
||||
|
||||
@@ -55,6 +55,7 @@ export class HopperFixtureLauncher implements BridgeLauncher {
|
||||
ok({
|
||||
process: child,
|
||||
ownsProcessLifetime: true as const,
|
||||
providerLifetime: "launcher-process" as const,
|
||||
shutdownMode: "bridge-request" as const,
|
||||
}),
|
||||
);
|
||||
|
||||
@@ -40,7 +40,10 @@ class LifecycleLauncher implements BridgeLauncher {
|
||||
await this.fixture.launch(session);
|
||||
return ok({
|
||||
process: this.observed,
|
||||
ownsProcessLifetime: this.owned,
|
||||
ownsProcessLifetime: true as const,
|
||||
providerLifetime: this.owned
|
||||
? ("launcher-process" as const)
|
||||
: ("external-application" as const),
|
||||
shutdownMode: "bridge-request" as const,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -0,0 +1,136 @@
|
||||
import { spawn } from "node:child_process";
|
||||
|
||||
import { expect, it, onTestFinished } from "vitest";
|
||||
|
||||
import { projectAnalysisError } from "../../../../src/domain/analysisErrorProjection.js";
|
||||
import { ok } from "../../../../src/domain/result.js";
|
||||
import type { BridgeLauncher } from "../../../../src/hopper/BridgeLauncher.js";
|
||||
import { HopperApplicationLauncher } from "../../../../src/hopper/BridgeLauncher.js";
|
||||
import { HopperClient } from "../../../../src/hopper/HopperClient.js";
|
||||
|
||||
it("does not equate an owned native helper exit with an external GUI exit", async () => {
|
||||
const launcher = new HopperApplicationLauncher({
|
||||
launcherPath: process.execPath,
|
||||
targetPath: "/target",
|
||||
targetKind: "executable",
|
||||
loaderArgs: [
|
||||
"-e",
|
||||
'process.stderr.write("Hopper has not been found.\\n", () => process.exit(3))',
|
||||
"--",
|
||||
],
|
||||
bridgeScriptPath: "/rea/hopper_bridge.py",
|
||||
launchMode: "native",
|
||||
});
|
||||
const client = new HopperClient({ launcher, startupTimeoutMs: 10_000 });
|
||||
onTestFinished(() => client.close());
|
||||
const result = await client.start();
|
||||
expect(result).toMatchObject({
|
||||
ok: false,
|
||||
error: { _tag: "HopperStartError" },
|
||||
});
|
||||
if (result.ok) throw new Error("Expected the native helper to fail");
|
||||
expect(projectAnalysisError(result.error)).toMatchObject({
|
||||
details: {
|
||||
provider_state: "unknown",
|
||||
launcher: { exit_code: 3 },
|
||||
},
|
||||
});
|
||||
expect(client.operationHealth()).toMatchObject({
|
||||
state: "unknown",
|
||||
exitCode: null,
|
||||
});
|
||||
});
|
||||
|
||||
it.each(["exit", "signal", "exit75"] as const)(
|
||||
"reports a failed non-owning launcher %s without waiting for bridge timeout or inventing a GUI exit",
|
||||
async (termination) => {
|
||||
let token = "";
|
||||
const launcher: BridgeLauncher = {
|
||||
launch(session) {
|
||||
token = session.token;
|
||||
const stdout = `API_KEY=local-evidence-kept ${token}\n`;
|
||||
const stderr = `Hopper has not been found. Spotlight is unavailable. ${token}\n`;
|
||||
const stop =
|
||||
termination === "signal"
|
||||
? 'process.kill(process.pid, "SIGTERM")'
|
||||
: `process.exit(${termination === "exit75" ? 75 : 1})`;
|
||||
return Promise.resolve(
|
||||
ok({
|
||||
process: spawn(
|
||||
process.execPath,
|
||||
[
|
||||
"-e",
|
||||
`process.stdout.write(${JSON.stringify(stdout)}, () => process.stderr.write(${JSON.stringify(stderr)}, () => ${stop}));`,
|
||||
],
|
||||
{ stdio: ["ignore", "pipe", "pipe"] },
|
||||
),
|
||||
ownsProcessLifetime: false as const,
|
||||
providerLifetime: "external-application" as const,
|
||||
shutdownMode: "bridge-request" as const,
|
||||
}),
|
||||
);
|
||||
},
|
||||
};
|
||||
const client = new HopperClient({ launcher, startupTimeoutMs: 10_000 });
|
||||
onTestFinished(() => client.close());
|
||||
const result = await client.start();
|
||||
expect(result).toMatchObject({
|
||||
ok: false,
|
||||
error: { _tag: "HopperStartError" },
|
||||
});
|
||||
if (result.ok) throw new Error("Expected the launcher to fail");
|
||||
const projected = projectAnalysisError(result.error);
|
||||
expect(projected).toMatchObject({
|
||||
code: "provider_unavailable",
|
||||
details: {
|
||||
stage: "launch",
|
||||
launcher: {
|
||||
exit_code:
|
||||
termination === "signal" ? null : termination === "exit75" ? 75 : 1,
|
||||
signal: termination === "signal" ? "SIGTERM" : null,
|
||||
stdout: {
|
||||
text: expect.stringContaining("API_KEY=local-evidence-kept"),
|
||||
},
|
||||
stderr: { text: expect.stringContaining("Spotlight is unavailable") },
|
||||
},
|
||||
},
|
||||
});
|
||||
expect(JSON.stringify(projected).includes(token)).toBe(false);
|
||||
expect(client.operationHealth()).toMatchObject({
|
||||
state: "unknown",
|
||||
stage: "launch",
|
||||
exitCode: null,
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
it.each([0, 1])(
|
||||
"reports owned process exit %s before bridge readiness as an observed exit",
|
||||
async (code) => {
|
||||
const launcher: BridgeLauncher = {
|
||||
launch() {
|
||||
return Promise.resolve(
|
||||
ok({
|
||||
process: spawn(process.execPath, ["-e", `process.exit(${code})`], {
|
||||
stdio: ["ignore", "pipe", "pipe"],
|
||||
}),
|
||||
ownsProcessLifetime: true as const,
|
||||
providerLifetime: "launcher-process" as const,
|
||||
shutdownMode: "bridge-request" as const,
|
||||
}),
|
||||
);
|
||||
},
|
||||
};
|
||||
const client = new HopperClient({ launcher, startupTimeoutMs: 10_000 });
|
||||
onTestFinished(() => client.close());
|
||||
const result = await client.start();
|
||||
expect(result).toMatchObject({
|
||||
ok: false,
|
||||
error: { _tag: "HopperProcessError", exitCode: code, stage: "launch" },
|
||||
});
|
||||
expect(client.operationHealth()).toMatchObject({
|
||||
state: "exited",
|
||||
exitCode: code,
|
||||
});
|
||||
},
|
||||
);
|
||||
@@ -27,6 +27,7 @@ class SilentLauncher implements BridgeLauncher {
|
||||
},
|
||||
),
|
||||
ownsProcessLifetime: true as const,
|
||||
providerLifetime: "launcher-process" as const,
|
||||
shutdownMode: "bridge-request" as const,
|
||||
}),
|
||||
);
|
||||
@@ -52,7 +53,10 @@ class CancelThenFixtureLauncher implements BridgeLauncher {
|
||||
}
|
||||
|
||||
class ExitingLauncher implements BridgeLauncher {
|
||||
constructor(readonly code: number) {}
|
||||
constructor(
|
||||
readonly code: number,
|
||||
readonly ownsProcessLifetime = true,
|
||||
) {}
|
||||
|
||||
launch() {
|
||||
return Promise.resolve(
|
||||
@@ -60,7 +64,10 @@ class ExitingLauncher implements BridgeLauncher {
|
||||
process: spawn(process.execPath, ["-e", `process.exit(${this.code})`], {
|
||||
stdio: ["ignore", "ignore", "pipe"],
|
||||
}),
|
||||
ownsProcessLifetime: true as const,
|
||||
ownsProcessLifetime: this.ownsProcessLifetime,
|
||||
providerLifetime: this.ownsProcessLifetime
|
||||
? ("launcher-process" as const)
|
||||
: ("external-application" as const),
|
||||
shutdownMode: "bridge-request" as const,
|
||||
}),
|
||||
);
|
||||
@@ -97,6 +104,7 @@ class DiagnosticExitingLauncher implements BridgeLauncher {
|
||||
{ stdio: ["ignore", "ignore", "pipe"] },
|
||||
),
|
||||
ownsProcessLifetime: true as const,
|
||||
providerLifetime: "launcher-process" as const,
|
||||
shutdownMode: "bridge-request" as const,
|
||||
}),
|
||||
);
|
||||
@@ -281,7 +289,7 @@ describe("HopperClient startup failures", () => {
|
||||
|
||||
it("allows a short-lived launcher to hand off bridge startup", async () => {
|
||||
const client = new HopperClient({
|
||||
launcher: new ExitingLauncher(0),
|
||||
launcher: new ExitingLauncher(0, false),
|
||||
startupTimeoutMs: 100,
|
||||
});
|
||||
clients.push(client);
|
||||
|
||||
Reference in New Issue
Block a user