A personal assistant that lives on your desktop, remembers what matters, and can use your Mac for you.
TypeScript
fix(daemon): lsof -w, and an uncertain lookup refuses ownership, not startup
Marcelitoca0831ab10d6
diff --git a/packages/daemon/src/isolation.ts b/packages/daemon/src/isolation.tsindex 3bc4286..70708a8 100644--- a/packages/daemon/src/isolation.ts+++ b/packages/daemon/src/isolation.ts@@ -235,7 +235,9 @@ interface PrivateMemoryOptions {/*** Owns cleanup only after confirming an empty private port before startup.- * A failed or ambiguous lookup refuses ownership. Before termination, both the+ * A failed or ambiguous lookup refuses ownership, not startup: the daemon starts,+ * logs it, and leaves that memory server running when it closes. An invalid port+ * is a configuration error and does stop startup. Before termination, both the* private data identity and a repeated stable listener PID must match.* PID reuse after the final check remains possible; nothing is killed by name.*/@@ -259,7 +261,9 @@ export async function privateMemory(const before = await listeningPid(port, options);if (before.kind === "one") return undefined;if (before.kind !== "empty") {- throw new Error(`Cannot start private memory: ${before.kind} listener lookup on port ${port}`);+ // Not knowing is no reason to keep the assistant from starting, nor to claim the server.+ log(`cannot verify private memory listener on port ${port} (${before.kind} lookup); will not stop it`);+ return undefined;}return {async stop() {@@ -347,7 +351,7 @@ async function listeningPid(port: string, options: PrivateMemoryOptions): Promis"-Command",`$ErrorActionPreference='Stop'; $ProgressPreference='SilentlyContinue'; try { Get-NetTCPConnection -State Listen -ErrorAction Stop | Where-Object { $_.LocalPort -eq ${Number(port)} } | Select-Object -ExpandProperty OwningProcess } catch { [Console]::Error.WriteLine($_.Exception.Message); exit 1 }`,]- : ["-t", "-n", "-P", `-iTCP:${port}`, "-sTCP:LISTEN"];+ : ["-w", "-t", "-n", "-P", `-iTCP:${port}`, "-sTCP:LISTEN"];try {const result = await (options.runListenerCommand ?? runListenerCommand)(command, args);const text = result.stdout.trim();diff --git a/packages/daemon/test/private-memory.test.ts b/packages/daemon/test/private-memory.test.tsindex 0d203dd..469420d 100644--- a/packages/daemon/test/private-memory.test.ts+++ b/packages/daemon/test/private-memory.test.ts@@ -164,10 +164,12 @@ describe("private memory ownership safeguards", () => {{ code: 0, stdout: "0" },{ code: 0, stdout: "garbage" },{ code: 0, stdout: "12345\n12346" },- ])("refuses uncertain startup ownership: %j", async (output) => {+ ])("starts without claiming ownership when the startup lookup is uncertain: %j", async (output) => {const f = await fixture([output]);- await expect(privateMemory(f.base, f.dataDir, () => {}, undefined, f.options)).rejects.toThrow(- "private memory",+ const log = vi.fn();+ expect(await privateMemory(f.base, f.dataDir, log, undefined, f.options)).toBeUndefined();+ expect(log).toHaveBeenCalledWith(+ `cannot verify private memory listener on port ${f.port} (${output.code === 0 && output.stdout.includes("\n") ? "ambiguous" : "failed"} lookup); will not stop it`,);expect(f.kill).not.toHaveBeenCalled();});@@ -200,12 +202,12 @@ describe("private memory ownership safeguards", () => {expect(f.kill).not.toHaveBeenCalled();});- it("rejects a failed command instead of assuming an empty port", async () => {+ it("does not take a failed command for an empty port, and still starts", async () => {const f = await fixture([]);f.runListenerCommand.mockRejectedValueOnce(new Error("spawn failed"));- await expect(privateMemory(f.base, f.dataDir, () => {}, undefined, f.options)).rejects.toThrow(- "private memory",- );+ const log = vi.fn();+ expect(await privateMemory(f.base, f.dataDir, log, undefined, f.options)).toBeUndefined();+ expect(log).toHaveBeenCalledWith(expect.stringMatching(/cannot verify .* will not stop it/));expect(f.kill).not.toHaveBeenCalled();});@@ -218,6 +220,14 @@ describe("private memory ownership safeguards", () => {expect(f.runListenerCommand.mock.calls[0]?.[0]).toBe("lsof");});+ it("asks lsof for no warnings, so a successful lookup is not read as failed", async () => {+ // lsof warns on stderr even when the lookup works (gvfs, Docker overlay mounts), and any+ // stderr is a failed lookup: `-w` keeps those warnings out, real failures keep their exit code.+ const f = await fixture([{ code: 1, stdout: "" }]);+ await privateMemory(f.base, f.dataDir, () => {}, undefined, { ...f.options, platform: "linux" });+ expect(f.runListenerCommand.mock.calls[0]?.[1]).toContain("-w");+ });+it("does not terminate when health is unavailable or the stored instance id is empty", async () => {const f = await fixture([empty, one]);await (