Skip to content

Linger is An Open-Source Personal Agent That Runs Fully on Your PC. We aim to break Big Tech’s grip on AI by bringing powerful local AI to everyday computers through efficient model–harness co-design, so everyone can run their own.

fix(local-installation): read macOS process identity in the C locale

Dongdong Chen4f096e203fd8
diff --git a/packages/local-installation/src/native-process-darwin.test.ts b/packages/local-installation/src/native-process-darwin.test.ts
new file mode 100644
index 00000000..dd4217f5
--- /dev/null
+++ b/packages/local-installation/src/native-process-darwin.test.ts
@@ -0,0 +1,90 @@
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+
+const calls = vi.hoisted(
+ () => [] as Array<{ args: string[]; env: NodeJS.ProcessEnv }>,
+);
+const output = vi.hoisted(() => ({ identity: "", environment: "" }));
+vi.mock("node:child_process", () => ({
+ execFile: (
+ _executable: string,
+ args: string[],
+ options: { env: NodeJS.ProcessEnv },
+ callback: (error: null, stdout: string) => void,
+ ) => {
+ calls.push({ args, env: options.env });
+ callback(null, args[0] === "eww" ? output.environment : output.identity);
+ },
+}));
+
+const { darwinProcessIdentity, nativeProcessIdentity } =
+ await import("./native-process-identity.js");
+
+const lease = "a".repeat(64);
+// What ps prints for lstart on a zh-CN Mac: four fields, not five.
+const zhStart = `${String.fromCodePoint(0x4e09)} 11${String.fromCodePoint(0x6708)}/18 06:04:14 2026`;
+
+describe("darwinProcessIdentity", () => {
+ it("keeps the C-locale birth every recorded identity uses", () => {
+ expect(
+ darwinProcessIdentity("Wed Nov 18 06:04:14 2026 501 73285 Ss\n"),
+ ).toEqual({
+ birth: "darwin:Wed:Nov:18:06:04:14:2026:501:73285",
+ uid: 501,
+ group: 73285,
+ state: "Ss",
+ });
+ });
+
+ it("reads uid, group and state after a localized start time", () => {
+ expect(darwinProcessIdentity(`${zhStart} 501 73285 Ss`)).toMatchObject({
+ uid: 501,
+ group: 73285,
+ state: "Ss",
+ });
+ });
+
+ it("rejects a line without numeric uid and group", () => {
+ expect(darwinProcessIdentity("Wed Nov 18 06:04:14 2026")).toBeUndefined();
+ });
+});
+
+describe("nativeProcessIdentity on macOS", () => {
+ const platform = process.platform;
+ const getuid = process.getuid;
+ beforeEach(() => {
+ calls.length = 0;
+ Object.defineProperty(process, "platform", { value: "darwin" });
+ process.getuid = () => 501;
+ process.env.LC_ALL = "zh_CN.UTF-8";
+ process.env.LANG = "zh_CN.UTF-8";
+ process.env.LINGER_NATIVE_PROCESS_LEASE = lease;
+ });
+ afterEach(() => {
+ Object.defineProperty(process, "platform", { value: platform });
+ process.getuid = getuid;
+ delete process.env.LC_ALL;
+ delete process.env.LANG;
+ delete process.env.LINGER_NATIVE_PROCESS_LEASE;
+ });
+
+ it("runs every ps in the C locale without the lease", async () => {
+ output.identity = "Wed Nov 18 06:04:14 2026 501 73285 Ss\n";
+ output.environment = `node worker.mjs LINGER_NATIVE_PROCESS_LEASE=${lease}\n`;
+ await expect(nativeProcessIdentity(73285, lease)).resolves.toBe(
+ "darwin:Wed:Nov:18:06:04:14:2026:501:73285",
+ );
+ expect(calls).toHaveLength(2);
+ for (const { env } of calls) {
+ expect(env).toMatchObject({ LC_ALL: "C", LANG: "C" });
+ expect(env.LINGER_NATIVE_PROCESS_LEASE).toBeUndefined();
+ }
+ });
+
+ it("verifies a process whose start time still prints localized", async () => {
+ output.identity = `${zhStart} 501 73285 Ss\n`;
+ output.environment = `node worker.mjs LINGER_NATIVE_PROCESS_LEASE=${lease}\n`;
+ await expect(nativeProcessIdentity(73285, lease)).resolves.toBe(
+ `darwin:${zhStart.split(" ").join(":")}:501:73285`,
+ );
+ });
+});
diff --git a/packages/local-installation/src/native-process-identity.ts b/packages/local-installation/src/native-process-identity.ts
index 9fba2b0a..987c53cf 100644
--- a/packages/local-installation/src/native-process-identity.ts
+++ b/packages/local-installation/src/native-process-identity.ts
@@ -8,7 +8,10 @@ function runProcess(
options: { timeoutMs: number },
) {
// Inspectors must not inherit the lease and appear among the owned processes.
- const env = { ...process.env };
+ // ps prints lstart in the caller's locale (a zh-CN Mac prints a different
+ // number of fields), and a recorded birth must read the same from every
+ // process that checks it, so inspectors run in the C locale.
+ const env: NodeJS.ProcessEnv = { ...process.env, LC_ALL: "C", LANG: "C" };
delete env[leaseKey];
return new Promise<{ code: number; stdout: string }>((resolve, reject) => {
execFile(
@@ -113,6 +116,24 @@ export async function linuxProcessStates(
);
return states.filter((state) => state !== undefined);
}
+/**
+ * Reads one `ps -o lstart=,uid=,pgid=,stat=` line. The start time is every
+ * field before the last three, so its word count never shifts the others.
+ */
+export function darwinProcessIdentity(
+ line: string,
+): { birth: string; uid: number; group: number; state: string } | undefined {
+ const parts = line.trim().split(/\s+/);
+ if (parts.length < 4) return;
+ const [uid, group, state] = parts.slice(-3) as [string, string, string];
+ if (!/^\d+$/.test(uid) || !/^\d+$/.test(group)) return;
+ return {
+ birth: `darwin:${parts.slice(0, -1).join(":")}`,
+ uid: Number(uid),
+ group: Number(group),
+ state,
+ };
+}
export async function nativeProcessIdentity(
pid: number,
lease: string,
@@ -158,14 +179,15 @@ export async function nativeProcessIdentity(
!environment.stdout.includes(`${leaseKey}=${lease}`)
)
return;
- const parts = identity.stdout.trim().split(/\s+/);
+ const parsed = darwinProcessIdentity(identity.stdout);
if (
- Number(parts[5]) !== process.getuid?.() ||
- (requireLeader && Number(parts[6]) !== pid) ||
- parts[7]?.startsWith("Z")
+ !parsed ||
+ parsed.uid !== process.getuid?.() ||
+ (requireLeader && parsed.group !== pid) ||
+ parsed.state.startsWith("Z")
)
return;
- return `darwin:${parts.slice(0, 7).join(":")}`;
+ return parsed.birth;
}
} catch {
return;