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): remove temporary files after private write failures
Afloatad63af00f7b0
diff --git a/packages/local-installation/AGENTS.md b/packages/local-installation/AGENTS.mdindex 56e62919..802af587 100644--- a/packages/local-installation/AGENTS.md+++ b/packages/local-installation/AGENTS.md@@ -11,6 +11,10 @@ stop unrelated processes, pull mutable images, or delete volumes as recovery.Every installation runs natively from a Linger source checkout; Docker is notused.+`atomicPrivate` closes and removes its temporary file on failed writes or+syncs as well as failed publication. A failed replacement keeps the previous+private file available, without leaving partial configuration copies behind.+The account file records provisioning history, not current database readinessor a promise that its saved password is still current. On HTTP 401, the existingidempotent owner command establishes whether this email owns the deployment anddiff --git a/packages/local-installation/src/private-files.test.ts b/packages/local-installation/src/private-files.test.tsnew file mode 100644index 00000000..f22ced8d--- /dev/null+++ b/packages/local-installation/src/private-files.test.ts@@ -0,0 +1,60 @@+import { mkdtemp, readFile, readdir, rm, writeFile } from "node:fs/promises";+import { tmpdir } from "node:os";+import path from "node:path";+import { afterEach, expect, it, vi } from "vitest";+import { atomicPrivate } from "./private-files.js";++const failure = vi.hoisted(() => ({+ stage: undefined as "write" | "sync" | undefined,+ error: Object.assign(new Error("Private write failed"), { code: "ENOSPC" }),+}));+vi.mock("node:fs/promises", async (importOriginal) => {+ const actual = await importOriginal<typeof import("node:fs/promises")>();+ return {+ ...actual,+ open: async (...args: Parameters<typeof actual.open>) => {+ const handle = await actual.open(...args);+ if (args[1] === "wx" && failure.stage === "write") {+ const write = handle.writeFile.bind(handle);+ vi.spyOn(handle, "writeFile").mockImplementationOnce(async () => {+ await write("partial private contents");+ throw failure.error;+ });+ } else if (args[1] === "wx" && failure.stage === "sync") {+ vi.spyOn(handle, "sync").mockRejectedValueOnce(failure.error);+ }+ return handle;+ },+ };+});++const roots: string[] = [];+afterEach(async () => {+ failure.stage = undefined;+ vi.restoreAllMocks();+ await Promise.all(+ roots.splice(0).map((root) => rm(root, { recursive: true, force: true })),+ );+});++it.each(["write", "sync"] as const)(+ "removes a temporary private file after a %s failure and permits retry",+ async (stage) => {+ const root = await mkdtemp(path.join(tmpdir(), "linger-private-write-"));+ roots.push(root);+ const file = path.join(root, "settings.json");+ await writeFile(file, "previous private contents", { mode: 0o600 });++ failure.stage = stage;+ await expect(atomicPrivate(file, "new private contents")).rejects.toBe(+ failure.error,+ );+ expect(await readFile(file, "utf8")).toBe("previous private contents");+ expect(await readdir(root)).toEqual(["settings.json"]);++ failure.stage = undefined;+ await atomicPrivate(file, "new private contents");+ expect(await readFile(file, "utf8")).toBe("new private contents");+ expect(await readdir(root)).toEqual(["settings.json"]);+ },+);diff --git a/packages/local-installation/src/private-files.ts b/packages/local-installation/src/private-files.tsindex 5d36ea6a..bc05168f 100644--- a/packages/local-installation/src/private-files.ts+++ b/packages/local-installation/src/private-files.ts@@ -37,14 +37,14 @@ export async function atomicPrivate(const temporary = `${file}.${randomUUID()}.tmp`;const handle = await open(temporary, "wx", 0o600);try {- if (process.platform === "win32")- await windowsPrivatePath(temporary, "file", true);- await handle.writeFile(contents);- await handle.sync();- } finally {- await handle.close();- }- try {+ try {+ if (process.platform === "win32")+ await windowsPrivatePath(temporary, "file", true);+ await handle.writeFile(contents);+ await handle.sync();+ } finally {+ await handle.close();+ }await rename(temporary, file);} finally {await rm(temporary, { force: true });