A self-improving RLM agent for coding workflows and long-running autonomous tasks.
Rust
fix(coding-agent): stabilize daemon process identity (fixes #879) (#1971)
Seth Karten0ba0423c5c18
diff --git a/packages/coding-agent/.changes/timezone-stable-process-identity.md b/packages/coding-agent/.changes/timezone-stable-process-identity.mdnew file mode 100644index 0000000000..28097a9f62--- /dev/null+++ b/packages/coding-agent/.changes/timezone-stable-process-identity.md@@ -0,0 +1 @@+- Fixed daemon session creation after macOS timezone changes ([#879](https://github.com/PrimeIntellect-ai/prime-agent/issues/879))diff --git a/packages/coding-agent/src/core/session-lease.ts b/packages/coding-agent/src/core/session-lease.tsindex 7f89449c67..af0f205dbd 100644--- a/packages/coding-agent/src/core/session-lease.ts+++ b/packages/coding-agent/src/core/session-lease.ts@@ -110,12 +110,17 @@ function isProcessAlive(pid: number): boolean {}}-type ProcessQuery = (command: string, args: string[]) => string;+interface ProcessQueryOptions {+ env?: NodeJS.ProcessEnv;+}++type ProcessQuery = (command: string, args: string[], options?: ProcessQueryOptions) => string;-function runProcessQuery(command: string, args: string[]): string {+function runProcessQuery(command: string, args: string[], options?: ProcessQueryOptions): string {return execFileSync(command, args, {encoding: "utf8",stdio: ["ignore", "pipe", "ignore"],+ env: options?.env,});}@@ -137,6 +142,21 @@ export function getWindowsProcessStartId(pid: number, query: ProcessQuery = runP}}+export function getPsProcessStartId(pid: number, query: ProcessQuery = runProcessQuery): string | undefined {+ if (!Number.isInteger(pid) || pid <= 0) {+ return undefined;+ }+ try {+ // `lstart` is rendered in the subprocess timezone and locale, so pin both for a durable identity.+ const startTime = query("ps", ["-p", String(pid), "-o", "lstart="], {+ env: { ...process.env, LC_ALL: "C", LC_TIME: "C", LANG: "C", TZ: "UTC" },+ }).trim();+ return startTime ? `ps:${startTime}` : undefined;+ } catch {+ return undefined;+ }+}+export function getProcessStartId(pid: number): string | undefined {if (!Number.isInteger(pid) || pid <= 0) {return undefined;@@ -155,12 +175,7 @@ export function getProcessStartId(pid: number): string | undefined {} catch {// Fall through to the portable process listing used on macOS and BSD.}- try {- const startTime = runProcessQuery("ps", ["-p", String(pid), "-o", "lstart="]).trim();- return startTime ? `ps:${startTime}` : undefined;- } catch {- return undefined;- }+ return getPsProcessStartId(pid);}let currentProcessStartId: string | undefined;diff --git a/packages/coding-agent/test/suite/regressions/879-timezone-stable-process-identity.test.ts b/packages/coding-agent/test/suite/regressions/879-timezone-stable-process-identity.test.tsnew file mode 100644index 0000000000..ab6a6c778c--- /dev/null+++ b/packages/coding-agent/test/suite/regressions/879-timezone-stable-process-identity.test.ts@@ -0,0 +1,98 @@+import { mkdtempSync, rmSync } from "node:fs";+import { tmpdir } from "node:os";+import { join } from "node:path";+import { afterEach, describe, expect, it } from "vitest";+import { getPsProcessStartId } from "../../../src/core/session-lease.js";+import {+ acquireDaemonSupervisorOwnership,+ assertDaemonSupervisorOwnerCurrent,+} from "../../../src/modes/daemon/daemon-supervisor-ownership.js";+import { createHarness, type Harness } from "../harness.js";++describe("issue #879 stable daemon process identity across timezone changes", () => {+ const harnesses: Harness[] = [];+ const tempDirs: string[] = [];++ afterEach(() => {+ for (const harness of harnesses.splice(0)) {+ harness.cleanup();+ }+ for (const directory of tempDirs.splice(0)) {+ rmSync(directory, { recursive: true, force: true });+ }+ });++ it("pins the portable process query to UTC across caller timezone changes", async () => {+ const harness = await createHarness();+ harnesses.push(harness);+ const calls: Array<{ command: string; args: string[]; env: NodeJS.ProcessEnv | undefined }> = [];+ const query = (command: string, args: string[], options?: { env?: NodeJS.ProcessEnv }) => {+ calls.push({ command, args, env: options?.env });+ return options?.env?.TZ === "UTC" ? "Sat Aug 29 20:55:18 2026\n" : "Sat Aug 29 16:55:18 2026\n";+ };+ const originalTimezone = process.env.TZ;+ let before: string | undefined;+ let after: string | undefined;+ try {+ process.env.TZ = "America/Los_Angeles";+ before = getPsProcessStartId(42, query);+ process.env.TZ = "America/New_York";+ after = getPsProcessStartId(42, query);+ } finally {+ if (originalTimezone === undefined) {+ delete process.env.TZ;+ } else {+ process.env.TZ = originalTimezone;+ }+ }++ expect(before).toBe("ps:Sat Aug 29 20:55:18 2026");+ expect(after).toBe(before);+ expect(calls).toHaveLength(2);+ for (const call of calls) {+ expect(call).toMatchObject({+ command: "ps",+ args: ["-p", "42", "-o", "lstart="],+ env: { LC_ALL: "C", LC_TIME: "C", LANG: "C", TZ: "UTC" },+ });+ }+ });++ it.skipIf(process.platform !== "darwin")("keeps supervisor ownership current after a timezone change", async () => {+ const root = mkdtempSync(join(tmpdir(), "prime-timezone-ownership-"));+ tempDirs.push(root);+ const registryDir = join(root, "registry");+ const socketPath = join(root, "daemon.sock");+ const originalTimezone = process.env.TZ;+ let ownership: Awaited<ReturnType<typeof acquireDaemonSupervisorOwnership>> | undefined;+ try {+ process.env.TZ = "America/Los_Angeles";+ ownership = await acquireDaemonSupervisorOwnership({+ agentDir: join(root, "agent"),+ appVersion: "test",+ descriptorDir: join(root, "workers"),+ generation: "timezone-regression",+ registryDir,+ socketPath,+ });+ const identity = {+ generation: ownership.record.generation,+ pid: ownership.record.pid,+ ...(ownership.record.processStartId ? { processStartId: ownership.record.processStartId } : {}),+ socketPath: ownership.record.socketPath,+ };++ process.env.TZ = "America/New_York";+ await expect(assertDaemonSupervisorOwnerCurrent(identity, undefined, registryDir, undefined)).resolves.toEqual(+ expect.any(String),+ );+ } finally {+ await ownership?.release();+ if (originalTimezone === undefined) {+ delete process.env.TZ;+ } else {+ process.env.TZ = originalTimezone;+ }+ }+ });+});