Skip to content

Commit 6997453

Browse files
authored
fix: guard in-band macOS launchd stop
Summary: - guard macOS launchd stop/restart against in-band service relaunch loops - centralize current-service detection for launchd stop and restart handoff - preserve external launchd label stop overrides while fixing inherited XPC restart handoff Verification: - node scripts/run-vitest.mjs src/daemon/launchd.test.ts src/daemon/launchd-current-service.test.ts src/daemon/launchd-restart-handoff.test.ts - .agents/skills/autoreview/scripts/autoreview --mode local - pnpm check:changed via Blacksmith Testbox through Crabbox: tbx_01kt3xkmfqhnzghfxdn62fa8qm Closes #89174 Co-authored-by: Bek <[email protected]>
1 parent c35fda3 commit 6997453

5 files changed

Lines changed: 284 additions & 39 deletions

File tree

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { describe, expect, it } from "vitest";
2+
import { isCurrentProcessLaunchdServiceLabel } from "./launchd-current-service.js";
3+
4+
describe("isCurrentProcessLaunchdServiceLabel", () => {
5+
it("matches launchd-provided service labels", () => {
6+
expect(
7+
isCurrentProcessLaunchdServiceLabel("ai.openclaw.gateway", {
8+
LAUNCH_JOB_LABEL: "ai.openclaw.gateway",
9+
}),
10+
).toBe(true);
11+
});
12+
13+
it("falls back to OpenClaw service markers when XPC_SERVICE_NAME is inherited", () => {
14+
expect(
15+
isCurrentProcessLaunchdServiceLabel("ai.openclaw.gateway", {
16+
XPC_SERVICE_NAME: "0",
17+
OPENCLAW_SERVICE_MARKER: "openclaw",
18+
OPENCLAW_SERVICE_KIND: "gateway",
19+
OPENCLAW_LAUNCHD_LABEL: "ai.openclaw.gateway",
20+
}),
21+
).toBe(true);
22+
});
23+
24+
it("preserves label-only fallback when launchd exposes no label variables", () => {
25+
expect(
26+
isCurrentProcessLaunchdServiceLabel("ai.openclaw.gateway", {
27+
OPENCLAW_LAUNCHD_LABEL: "ai.openclaw.gateway",
28+
}),
29+
).toBe(true);
30+
});
31+
32+
it("can require service markers for label-only fallback", () => {
33+
expect(
34+
isCurrentProcessLaunchdServiceLabel(
35+
"ai.openclaw.gateway",
36+
{
37+
OPENCLAW_LAUNCHD_LABEL: "ai.openclaw.gateway",
38+
},
39+
{ allowConfiguredLabelFallback: false },
40+
),
41+
).toBe(false);
42+
});
43+
44+
it("does not treat unrelated inherited launchd labels as current services", () => {
45+
expect(
46+
isCurrentProcessLaunchdServiceLabel("ai.openclaw.gateway", {
47+
XPC_SERVICE_NAME: "0",
48+
OPENCLAW_LAUNCHD_LABEL: "ai.openclaw.gateway",
49+
}),
50+
).toBe(false);
51+
});
52+
});
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { normalizeOptionalString } from "@openclaw/normalization-core/string-coerce";
2+
3+
export type CurrentProcessLaunchdServiceLabelOptions = {
4+
allowConfiguredLabelFallback?: boolean;
5+
};
6+
7+
export function isCurrentProcessLaunchdServiceLabel(
8+
label: string,
9+
env: NodeJS.ProcessEnv = process.env,
10+
options: CurrentProcessLaunchdServiceLabelOptions = {},
11+
): boolean {
12+
const currentLabels = [env.LAUNCH_JOB_LABEL, env.LAUNCH_JOB_NAME, env.XPC_SERVICE_NAME].flatMap(
13+
(value) => {
14+
const normalized = normalizeOptionalString(value);
15+
return normalized ? [normalized] : [];
16+
},
17+
);
18+
19+
for (const currentLabel of currentLabels) {
20+
if (currentLabel === label) {
21+
return true;
22+
}
23+
}
24+
25+
const configuredLabel = normalizeOptionalString(env.OPENCLAW_LAUNCHD_LABEL);
26+
if (!configuredLabel || configuredLabel !== label) {
27+
return false;
28+
}
29+
if (
30+
normalizeOptionalString(env.OPENCLAW_SERVICE_MARKER) === "openclaw" &&
31+
Boolean(normalizeOptionalString(env.OPENCLAW_SERVICE_KIND))
32+
) {
33+
return true;
34+
}
35+
return options.allowConfiguredLabelFallback !== false && currentLabels.length === 0;
36+
}

‎src/daemon/launchd-restart-handoff.ts‎

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { sanitizeForLog } from "../../packages/terminal-core/src/ansi.js";
66
import { formatErrorMessage } from "../infra/errors.js";
77
import { sanitizeHostExecEnv } from "../infra/host-env-security.js";
88
import { resolveGatewayLaunchAgentLabel } from "./constants.js";
9+
export { isCurrentProcessLaunchdServiceLabel } from "./launchd-current-service.js";
910
import { renderPosixRestartLogSetup } from "./restart-logs.js";
1011

1112
type LaunchdRestartHandoffMode = "kickstart" | "reload" | "start-after-exit";
@@ -92,21 +93,6 @@ function resolveLaunchdRestartTarget(
9293
};
9394
}
9495

95-
export function isCurrentProcessLaunchdServiceLabel(
96-
label: string,
97-
env: NodeJS.ProcessEnv = process.env,
98-
): boolean {
99-
const launchdLabel =
100-
normalizeOptionalString(env.LAUNCH_JOB_LABEL) ||
101-
normalizeOptionalString(env.LAUNCH_JOB_NAME) ||
102-
normalizeOptionalString(env.XPC_SERVICE_NAME);
103-
if (launchdLabel) {
104-
return launchdLabel === label;
105-
}
106-
const configuredLabel = normalizeOptionalString(env.OPENCLAW_LAUNCHD_LABEL);
107-
return Boolean(configuredLabel && configuredLabel === label);
108-
}
109-
11096
function buildLaunchdRestartScript(
11197
mode: LaunchdRestartHandoffMode,
11298
restartLogEnv: LaunchdRestartLogEnv,

0 commit comments

Comments
 (0)