Skip to content

Commit ffcf529

Browse files
committed
fix(infra): bound macOS metadata probes
1 parent b6e90cb commit ffcf529

4 files changed

Lines changed: 74 additions & 11 deletions

File tree

‎src/infra/os-summary.test.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@ describe("resolveRuntimeOsLabel", () => {
118118
});
119119

120120
expect(resolveRuntimeOsLabel()).toBe("macOS 26.6.0");
121+
expect(spawnSyncMock).toHaveBeenCalledWith("sw_vers", ["-productVersion"], {
122+
encoding: "utf-8",
123+
timeout: 5_000,
124+
killSignal: "SIGKILL",
125+
});
121126
});
122127

123128
it("falls back to the Darwin release when sw_vers output is blank", () => {

‎src/infra/os-summary.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ type OsSummary = {
1313
const cachedOsSummaryByKey = new Map<string, OsSummary>();
1414
const cachedRuntimeOsLabelByKey = new Map<string, string>();
1515

16+
// These synchronous probes run during both gateway startup and first-turn prompt
17+
// preparation, so keep one hard bound for every macOS system metadata lookup.
18+
export const DARWIN_SYSTEM_PROBE_TIMEOUT_MS = 5_000;
19+
1620
/**
1721
* Resolve Darwin product version via sw_vers.
1822
*
@@ -21,8 +25,12 @@ const cachedRuntimeOsLabelByKey = new Map<string, string>();
2125
* historical Darwin N → macOS N+9 formula. Prefer sw_vers over os.release() on
2226
* macOS to avoid stale mappings.
2327
*/
24-
function resolveDarwinProductVersion(): string {
25-
const res = spawnSync("sw_vers", ["-productVersion"], { encoding: "utf-8" });
28+
export function resolveDarwinProductVersion(): string {
29+
const res = spawnSync("sw_vers", ["-productVersion"], {
30+
encoding: "utf-8",
31+
timeout: DARWIN_SYSTEM_PROBE_TIMEOUT_MS,
32+
killSignal: "SIGKILL",
33+
});
2634
const out = normalizeOptionalString(res.stdout) ?? "";
2735
return out || os.release();
2836
}

‎src/infra/system-presence.ts‎

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
import { truncateUtf16Safe } from "@openclaw/normalization-core/utf16-slice";
1111
import { resolveRuntimeServiceVersion } from "../version.js";
1212
import { pickBestEffortPrimaryLanIPv4 } from "./network-discovery-display.js";
13+
import { DARWIN_SYSTEM_PROBE_TIMEOUT_MS, resolveDarwinProductVersion } from "./os-summary.js";
1314

1415
export type SystemPresence = {
1516
host?: string;
@@ -59,24 +60,19 @@ function initSelfPresence() {
5960
if (p === "darwin") {
6061
const res = spawnSync("sysctl", ["-n", "hw.model"], {
6162
encoding: "utf-8",
63+
timeout: DARWIN_SYSTEM_PROBE_TIMEOUT_MS,
64+
killSignal: "SIGKILL",
6265
});
6366
const out = normalizeOptionalString(res.stdout) ?? "";
6467
return out.length > 0 ? out : undefined;
6568
}
6669
return os.arch();
6770
})();
68-
const macOSVersion = () => {
69-
const res = spawnSync("sw_vers", ["-productVersion"], {
70-
encoding: "utf-8",
71-
});
72-
const out = normalizeOptionalString(res.stdout) ?? "";
73-
return out.length > 0 ? out : os.release();
74-
};
7571
const platform = (() => {
7672
const p = os.platform();
7773
const rel = os.release();
7874
if (p === "darwin") {
79-
return `macos ${macOSVersion()}`;
75+
return `macos ${resolveDarwinProductVersion()}`;
8076
}
8177
if (p === "win32") {
8278
return `windows ${rel}`;

‎src/infra/system-presence.version.test.ts‎

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
// Tests system command version probing for presence checks.
22
import os from "node:os";
33
import { importFreshModule } from "openclaw/plugin-sdk/test-fixtures";
4-
import { afterEach, describe, expect, it, vi } from "vitest";
4+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
55
import { withEnvAsync } from "../test-utils/env.js";
66
import { VERSION as runtimeVersion } from "../version.js";
77

88
vi.unmock("../version.js");
99

10+
const spawnSyncMock = vi.hoisted(() => vi.fn());
11+
12+
vi.mock("node:child_process", async () => {
13+
const { mockNodeChildProcessSpawnSync } = await import("openclaw/plugin-sdk/test-node-mocks");
14+
return mockNodeChildProcessSpawnSync(spawnSyncMock, () =>
15+
vi.importActual<typeof import("node:child_process")>("node:child_process"),
16+
);
17+
});
18+
1019
async function withPresenceModule<T>(
1120
env: Record<string, string | undefined>,
1221
run: (module: typeof import("./system-presence.js")) => Promise<T> | T,
@@ -29,8 +38,20 @@ async function withPresenceModule<T>(
2938
}
3039

3140
describe("system-presence version fallback", () => {
41+
beforeEach(() => {
42+
spawnSyncMock.mockReturnValue({
43+
stdout: "",
44+
stderr: "",
45+
pid: 1,
46+
output: [],
47+
status: 0,
48+
signal: null,
49+
});
50+
});
51+
3252
afterEach(() => {
3353
vi.restoreAllMocks();
54+
spawnSyncMock.mockReset();
3455
});
3556

3657
async function expectSelfVersion(
@@ -114,4 +135,37 @@ describe("system-presence version fallback", () => {
114135
expect(selfEntry?.ip).toBe("test-host");
115136
});
116137
});
138+
139+
it("bounds macOS self-presence metadata probes", async () => {
140+
vi.spyOn(os, "platform").mockReturnValue("darwin");
141+
vi.spyOn(os, "release").mockReturnValue("25.0.0");
142+
vi.spyOn(os, "arch").mockReturnValue("arm64");
143+
vi.spyOn(os, "hostname").mockReturnValue("test-mac");
144+
vi.spyOn(os, "networkInterfaces").mockReturnValue({});
145+
spawnSyncMock.mockImplementation((command: string) => ({
146+
stdout: command === "sysctl" ? "Mac16,1\n" : "26.0\n",
147+
stderr: "",
148+
pid: 1,
149+
output: [],
150+
status: 0,
151+
signal: null,
152+
}));
153+
154+
await withPresenceModule({}, ({ listSystemPresence }) => {
155+
const selfEntry = listSystemPresence().find((entry) => entry.reason === "self");
156+
expect(selfEntry?.modelIdentifier).toBe("Mac16,1");
157+
expect(selfEntry?.platform).toBe("macos 26.0");
158+
});
159+
160+
expect(spawnSyncMock).toHaveBeenCalledWith("sysctl", ["-n", "hw.model"], {
161+
encoding: "utf-8",
162+
timeout: 5_000,
163+
killSignal: "SIGKILL",
164+
});
165+
expect(spawnSyncMock).toHaveBeenCalledWith("sw_vers", ["-productVersion"], {
166+
encoding: "utf-8",
167+
timeout: 5_000,
168+
killSignal: "SIGKILL",
169+
});
170+
});
117171
});

0 commit comments

Comments
 (0)