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): adopt macOS services 0.1.2 recorded with a localized start time

Dongdong Chen74ce0a23c70f
diff --git a/packages/local-installation/AGENTS.md b/packages/local-installation/AGENTS.md
index 1935e102..6e3c2bde 100644
--- a/packages/local-installation/AGENTS.md
+++ b/packages/local-installation/AGENTS.md
@@ -137,7 +137,12 @@ program fails the start after 15 seconds instead of at the deadline.
On POSIX, a start that finds the recorded supervisor gone but processes
still carrying its lease (the app was killed with it) ends them as a stop
does and starts the service again. A live recorded PID or process group
-without the lease proves nothing and is refused, never signalled. A start
+without the lease proves nothing and is refused, never signalled.
+On macOS every identity check reads `ps` start times in the C locale.
+Linger 0.1.2 recorded them in the locale it ran in; such a record is
+converted once to the C-locale birth when `ps` in the installer's own
+locale still prints the recorded birth for the live leader holding the
+lease, and is otherwise left as it is. A start
that fails before its record is kept ends what it launched: by the lease,
or, when the lease cannot be proved, by the group of the supervisor it has
not reaped yet, whose PID cannot belong to another program. Before the
diff --git a/packages/local-installation/src/native-process-darwin-legacy.test.ts b/packages/local-installation/src/native-process-darwin-legacy.test.ts
new file mode 100644
index 00000000..2b1f805c
--- /dev/null
+++ b/packages/local-installation/src/native-process-darwin-legacy.test.ts
@@ -0,0 +1,90 @@
+import { mkdtemp, readFile, rm } from "node:fs/promises";
+import { tmpdir } from "node:os";
+import path from "node:path";
+import { afterEach, beforeEach, expect, it, vi } from "vitest";
+
+// ps as a Mac in a German region format prints it: lstart in the caller's
+// locale, C only when the inspector asks for it.
+const ps = vi.hoisted(() => {
+ const uid = process.getuid!();
+ return {
+ uid,
+ c: `Wed Nov 18 06:04:14 2026 ${uid} 73285 Ss\n`,
+ localized: `Mi 18 Nov 06:04:14 2026 ${uid} 73285 Ss\n`,
+ environment: "",
+ };
+});
+vi.mock("node:child_process", async (original) => ({
+ ...(await original<typeof import("node:child_process")>()),
+ execFile: (
+ _executable: string,
+ args: string[],
+ options: { env: NodeJS.ProcessEnv },
+ callback: (error: null, stdout: string) => void,
+ ) => {
+ if (args[0] === "eww") callback(null, ps.environment);
+ else callback(null, options.env.LC_ALL === "C" ? ps.c : ps.localized);
+ },
+}));
+
+const { atomicPrivate, privateDirectory } = await import("./instance.js");
+const { inspectNativeProcess } = await import("./native-process.js");
+
+const lease = "a".repeat(64);
+// What Linger 0.1.2 recorded for that process: the localized start time.
+const legacyBirth = `darwin:Mi:18:Nov:06:04:14:2026:${ps.uid}:73285`;
+const platform = process.platform;
+let root: string;
+const recordFile = () =>
+ path.join(root, "native", "processes", "postgres.json");
+const birth = async () =>
+ (JSON.parse(await readFile(recordFile(), "utf8")) as { birth: string }).birth;
+
+beforeEach(async () => {
+ root = await mkdtemp(path.join(tmpdir(), "linger-darwin-legacy-"));
+ await privateDirectory(path.join(root, "native", "processes"));
+ await atomicPrivate(
+ recordFile(),
+ JSON.stringify({
+ schemaVersion: 1,
+ name: "postgres",
+ pid: 73285,
+ birth: legacyBirth,
+ lease,
+ configuration: "b".repeat(64),
+ }),
+ );
+ Object.defineProperty(process, "platform", { value: "darwin" });
+ vi.stubEnv("LANG", "de_DE.UTF-8");
+ ps.localized = `Mi 18 Nov 06:04:14 2026 ${ps.uid} 73285 Ss\n`;
+ ps.environment = `node worker.mjs LINGER_NATIVE_PROCESS_LEASE=${lease}\n`;
+});
+afterEach(async () => {
+ Object.defineProperty(process, "platform", { value: platform });
+ vi.unstubAllEnvs();
+ await rm(root, { recursive: true, force: true });
+});
+
+it("adopts a service 0.1.2 recorded with a localized start time, once", async () => {
+ await expect(inspectNativeProcess(root, "postgres")).resolves.toEqual({
+ running: true,
+ pid: 73285,
+ });
+ expect(await birth()).toBe(`darwin:Wed:Nov:18:06:04:14:2026:${ps.uid}:73285`);
+});
+
+it("keeps the record when the localized start time names another process", async () => {
+ ps.localized = `Mi 18 Nov 07:00:00 2026 ${ps.uid} 73285 Ss\n`;
+ await expect(inspectNativeProcess(root, "postgres")).resolves.toMatchObject({
+ running: false,
+ });
+ expect(await birth()).toBe(legacyBirth);
+});
+
+it("keeps the record of a process without the lease", async () => {
+ ps.environment = "node worker.mjs\n";
+ await expect(inspectNativeProcess(root, "postgres")).resolves.toMatchObject({
+ running: false,
+ });
+ expect(await birth()).toBe(legacyBirth);
+});
diff --git a/packages/local-installation/src/native-process-identity.ts b/packages/local-installation/src/native-process-identity.ts
index d5c62344..8adfff99 100644
--- a/packages/local-installation/src/native-process-identity.ts
+++ b/packages/local-installation/src/native-process-identity.ts
@@ -5,13 +5,16 @@ const leaseKey = "LINGER_NATIVE_PROCESS_LEASE";
function runProcess(
executable: string,
args: string[],
- options: { timeoutMs: number },
+ options: { timeoutMs: number; locale?: "c" | "inherited" },
) {
// Inspectors must not inherit the lease and appear among the owned processes.
// 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" };
+ const env: NodeJS.ProcessEnv =
+ options.locale === "inherited"
+ ? { ...process.env }
+ : { ...process.env, LC_ALL: "C", LANG: "C" };
delete env[leaseKey];
return new Promise<{ code: number; stdout: string }>((resolve, reject) => {
execFile(
@@ -183,10 +186,29 @@ export function darwinProcessIdentity(
state,
};
}
+/**
+ * The birth of a macOS process as Linger 0.1.2 recorded it: lstart in the
+ * locale the installer ran in, not the C locale every check now uses. Only
+ * for converting such a record; undefined elsewhere.
+ */
+export function legacyDarwinIdentity(
+ pid: number,
+ lease: string,
+): Promise<string | undefined> {
+ if (process.platform !== "darwin") return Promise.resolve(undefined);
+ return nativeProcessIdentity(pid, lease, true, "inherited");
+}
+/** Whether a recorded macOS birth reads lstart in the C locale. */
+export function darwinBirthInCLocale(birth: string): boolean {
+ return /^darwin:(?:Sun|Mon|Tue|Wed|Thu|Fri|Sat):(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec):\d{1,2}:\d{2}:\d{2}:\d{2}:\d{4}:\d+:\d+$/.test(
+ birth,
+ );
+}
export async function nativeProcessIdentity(
pid: number,
lease: string,
requireLeader = true,
+ locale: "c" | "inherited" = "c",
): Promise<string | undefined> {
try {
if (process.platform === "win32") {
@@ -214,7 +236,7 @@ export async function nativeProcessIdentity(
runProcess(
"/bin/ps",
["-p", String(pid), "-o", "lstart=,uid=,pgid=,stat="],
- { timeoutMs: 5_000 },
+ { timeoutMs: 5_000, locale },
),
// Read only this candidate. Never print the output: it contains
// environment values. The unguessable launch lease survives execve.
diff --git a/packages/local-installation/src/native-process.ts b/packages/local-installation/src/native-process.ts
index 489afffd..c68507e1 100644
--- a/packages/local-installation/src/native-process.ts
+++ b/packages/local-installation/src/native-process.ts
@@ -8,6 +8,8 @@ import { setTimeout as delay } from "node:timers/promises";
import { atomicPrivate, privateDirectory, readPrivate } from "./instance.js";
import { runProcess, windowsTreeTermination } from "./process.js";
import {
+ darwinBirthInCLocale,
+ legacyDarwinIdentity,
linuxProcessStates,
nativeProcessIdentity as observed,
nativeLeaseProcesses,
@@ -68,8 +70,33 @@ async function stored(
!/^[a-f0-9]{64}$/.test(record.configuration)
)
throw new Error("Invalid native process identity record");
+ if (process.platform === "darwin" && !darwinBirthInCLocale(record.birth))
+ return adoptLegacyDarwinBirth(root, name, record);
return record;
}
+/**
+ * Linger 0.1.2 recorded a macOS birth with lstart in the locale it ran in;
+ * every check now reads it in the C locale, so a service it started in, for
+ * example, a German region format never matched again. When ps in this
+ * process's own locale still prints the recorded birth for the live leader
+ * holding the lease, it is that service: its record takes the C-locale birth,
+ * once. Otherwise the record stays as it is and proves nothing.
+ */
+async function adoptLegacyDarwinBirth(
+ root: string,
+ name: NativeService,
+ record: Lease,
+): Promise<Lease> {
+ const current = await observed(record.pid, record.lease);
+ if (
+ !current ||
+ (await legacyDarwinIdentity(record.pid, record.lease)) !== record.birth
+ )
+ return record;
+ const converted = { ...record, birth: current };
+ await atomicPrivate(paths(root, name).record, JSON.stringify(converted));
+ return converted;
+}
async function stillLive(pid: number): Promise<boolean> {
try {
if (process.platform === "win32") {