Skip to content

Commit fccd228

Browse files
committed
fix(mcp): ignore a stray reopen on a hidden CLI process
A hidden Openscreen mcp process answered every reopen Apple Event by starting the GUI, so OpenScreen came to the front out of nowhere for each agent session still open. macOS only routes a real launch to a CLI process that is in the Dock (#1214); a hidden one now ignores activate. Refs #1488.
1 parent 0cd5b44 commit fccd228

3 files changed

Lines changed: 42 additions & 3 deletions

File tree

‎electron/cli/cliMain.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { registerSttIpc, shutdownStt } from "../stt";
1818
import { sendUsagePing, USAGE_PING_INTERVAL_MS } from "../usage-ping";
1919
import { createRunnerWindow } from "../windows";
2020
import { CLI_USAGE, type CliCommand } from "./args";
21-
import { openGuiFromCli } from "./openGui";
21+
import { onCliActivate } from "./openGui";
2222
import { runInfoCommand, runPackCommand } from "./projectCommands";
2323
import { createRunnerHost } from "./runnerHost";
2424
import { isolateCliSessionData } from "./sessionData";
@@ -282,7 +282,7 @@ export function runCli(command: CliCommand): void {
282282
// to this process on to the GUI (see openGui.ts).
283283
if (process.platform === "darwin") {
284284
app.dock?.hide();
285-
app.on("activate", () => openGuiFromCli(process.execPath, Date.now(), () => app.dock?.hide()));
285+
app.on("activate", () => onCliActivate(app.dock));
286286
// No application menu either. That menu shows whenever this hidden process comes to the
287287
// front, as it does while it hands a launch on to the GUI, and its Quit (Cmd+Q) would end
288288
// the agent's MCP server rather than anything the user knowingly started. On macOS

‎electron/cli/openGui.test.ts‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { beforeEach, describe, expect, it, vi } from "vitest";
33
const spawn = vi.hoisted(() => vi.fn());
44
vi.mock("node:child_process", () => ({ spawn }));
55

6-
import { appBundleOf, openGuiFromCli } from "./openGui";
6+
import { appBundleOf, onCliActivate, openGuiFromCli } from "./openGui";
77

88
describe("appBundleOf", () => {
99
it("climbs from Contents/MacOS to the .app bundle", () => {
@@ -77,3 +77,26 @@ describe("openGuiFromCli", () => {
7777
expect(spawn).toHaveBeenCalledTimes(2);
7878
});
7979
});
80+
81+
describe("onCliActivate", () => {
82+
const exe = "/Applications/Openscreen.app/Contents/MacOS/Openscreen";
83+
84+
beforeEach(() => {
85+
spawn.mockReset();
86+
spawn.mockImplementation(() => ({ on: vi.fn(), unref: vi.fn() }));
87+
});
88+
89+
it("ignores a reopen sent to a hidden CLI process", () => {
90+
onCliActivate({ isVisible: () => false, hide: vi.fn() }, exe, 4_000_000);
91+
onCliActivate(undefined, exe, 4_100_000);
92+
expect(spawn).not.toHaveBeenCalled();
93+
});
94+
95+
it("opens the GUI when the process is in the Dock, where macOS routes launches", () => {
96+
onCliActivate({ isVisible: () => true, hide: vi.fn() }, exe, 5_000_000);
97+
expect(spawn).toHaveBeenCalledWith("open", ["-n", "/Applications/Openscreen.app"], {
98+
detached: true,
99+
stdio: "ignore",
100+
});
101+
});
102+
});

‎electron/cli/openGui.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77
// CLI process as `activate`, so it starts the GUI itself, as a new instance. When a GUI is
88
// already running, that instance loses the single-instance lock and brings it forward through
99
// `second-instance`.
10+
//
11+
// LaunchServices only does that to a CLI process it counts as a regular app, one that shows in
12+
// the Dock, as a run that has recorded does. A hidden one (`UIElement`) is left alone: Finder,
13+
// the Dock and `open` start the GUI themselves. All it ever gets is a stray `reopen` Apple Event,
14+
// and answering that put OpenScreen in front, out of nowhere, once per agent session still open
15+
// (#1488, measured on macOS 27 with `tell application id … to reopen`).
1016

1117
import { spawn } from "node:child_process";
1218
import path from "node:path";
@@ -58,3 +64,13 @@ export function openGuiFromCli(
5864
if (afterLaunch) child.on("exit", afterLaunch);
5965
child.unref();
6066
}
67+
68+
/** `activate` on a CLI run: open the GUI only when it is a launch macOS handed to this process. */
69+
export function onCliActivate(
70+
dock: { isVisible(): boolean; hide(): void } | undefined,
71+
execPath: string = process.execPath,
72+
now: number = Date.now(),
73+
): void {
74+
if (!dock?.isVisible()) return;
75+
openGuiFromCli(execPath, now, () => dock.hide());
76+
}

0 commit comments

Comments
 (0)