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): normalize updated dotenv assignments
Afloat7a2ae488ba59
diff --git a/packages/local-installation/AGENTS.md b/packages/local-installation/AGENTS.mdindex 94ac2867..4a8d7926 100644--- a/packages/local-installation/AGENTS.md+++ b/packages/local-installation/AGENTS.md@@ -30,7 +30,7 @@ Each instance directory holds `user.env` (0600), the only file a person edits:(`user-environment.ts`). Every mode merges it into the generated`compose.env` / `native.env` over a developer checkout's `.env` and under theinstaller's own values; other names are ignored with a warning, and the file-is created from a template once and never rewritten. The desktop source+is created from a template once; ordinary reads never rewrite it. The desktop sourcepackage's `.env` is not read. Voice keys are normally saved in the app(TeamHub); a key in `user.env` overrides it. Whenever the distribution hasvoice tools for this computer, Native prepares them and runs LiveKit with@@ -39,6 +39,14 @@ voice key; without them, or when they fail to prepare, TeamHub gets`TEAMHUB_VOICE_MEDIA=off` and the install continues. The services' `voice`setting tracks only the tools, so a key change never restarts them throughit. A native process restarts when its generated environment changes.++Adopting a legacy environment keeps nonempty values already set in `user.env`.+Adoption and saved key updates recognize plain, exported and whitespace-formatted+assignments through the same helper. Each changed key replaces its first assignment+in place and removes later duplicates, or is appended once when absent. This keeps+values beside their template comments without duplicate secret values. The legacy+file is removed only after the private atomic write completes.+Generated native values preserve quoted paths and literal backslashes bychecking their complete dotenv file with the launcher's parser. Multiline,NUL-containing or otherwise unrepresentable values fail before publishingdiff --git a/packages/local-installation/src/user-environment.test.ts b/packages/local-installation/src/user-environment.test.tsindex d1da5769..f3687cb1 100644--- a/packages/local-installation/src/user-environment.test.ts+++ b/packages/local-installation/src/user-environment.test.ts@@ -1,4 +1,11 @@-import { chmod, mkdtemp, readFile, stat, writeFile } from "node:fs/promises";+import {+ chmod,+ mkdir,+ mkdtemp,+ readFile,+ stat,+ writeFile,+} from "node:fs/promises";import { tmpdir } from "node:os";import path from "node:path";import { parseEnv } from "node:util";@@ -124,6 +131,14 @@ describe("adoptUserEnvironment", () => {const contents = await readFile(userEnvironmentFile(instanceRoot), "utf8");expect(contents).not.toContain("TEAMHUB_SECRET_ENCRYPTION_KEY");expect(contents).toContain("# Your own keys");+ expect(contents).toContain(+ "# Connected apps such as Gmail, GitHub and Slack (Composio).\nCOMPOSIO_API_KEY='composio'\n",+ );+ expect(contents).toContain(+ "# Recording, dictation and meeting transcripts (Soniox).\nSONIOX_API_KEY='old-soniox'\n",+ );+ expect(contents.match(/^COMPOSIO_API_KEY=/gm)).toHaveLength(1);+ expect(contents.match(/^SONIOX_API_KEY=/gm)).toHaveLength(1);await writeFile(legacy, "SONIOX_API_KEY=older\nXAI_API_KEY=xai\n");expect(await adoptUserEnvironment(instanceRoot, legacy)).toEqual([@@ -143,9 +158,106 @@ describe("adoptUserEnvironment", () => {).toEqual([]);await expect(stat(instanceRoot)).rejects.toMatchObject({ code: "ENOENT" });});++ it.each([+ "SONIOX_API_KEY=\nSONIOX_API_KEY=\n",+ 'SONIOX_API_KEY=\nexport SONIOX_API_KEY=""\n',+ 'SONIOX_API_KEY=\n SONIOX_API_KEY = ""\n',+ ])(+ "keeps adopted keys effective after empty assignments %j",+ async (empty) => {+ const directory = await root();+ const legacy = path.join(directory, "source.env");+ const file = userEnvironmentFile(directory);+ await writeFile(file, `${empty}TZ=UTC\n# Kept configuration\n`, {+ mode: 0o600,+ });+ await writeFile(legacy, "SONIOX_API_KEY=migrated-soniox\n", {+ mode: 0o600,+ });+ expect(await adoptUserEnvironment(directory, legacy)).toEqual([+ "SONIOX_API_KEY",+ ]);+ expect(await readUserEnvironment(directory)).toEqual({+ SONIOX_API_KEY: "migrated-soniox",+ TZ: "UTC",+ });+ expect(await readFile(file, "utf8")).toBe(+ "SONIOX_API_KEY='migrated-soniox'\nTZ=UTC\n# Kept configuration\n",+ );+ await expect(stat(legacy)).rejects.toMatchObject({ code: "ENOENT" });+ },+ );});describe("updateUserKeys", () => {+ it.each([+ "SONIOX_API_KEY=old\nSONIOX_API_KEY=later\n",+ "SONIOX_API_KEY=old\nexport SONIOX_API_KEY=later\n",+ "SONIOX_API_KEY=old\n SONIOX_API_KEY = 'later'\n",+ ])(+ "replaces and clears every effective assignment in %j",+ async (assignments) => {+ const directory = path.join(await root(), "instance");+ await mkdir(directory);+ const file = userEnvironmentFile(directory);+ await writeFile(file, `# Keep this comment\n${assignments}TZ=UTC\n`, {+ mode: 0o600,+ });+ await updateUserKeys(directory, { SONIOX_API_KEY: "replacement" });+ expect(await readUserEnvironment(directory)).toEqual({+ SONIOX_API_KEY: "replacement",+ TZ: "UTC",+ });+ expect(await readFile(file, "utf8")).not.toContain("later");+ expect(+ (await readFile(file, "utf8")).match(/^SONIOX_API_KEY=/gm),+ ).toHaveLength(1);+ expect(await updateUserKeys(directory, { SONIOX_API_KEY: "" })).toEqual(+ [],+ );+ expect(await readUserEnvironment(directory)).toEqual({ TZ: "UTC" });+ expect(await readFile(file, "utf8")).toContain("# Keep this comment");+ expect(+ (await readFile(file, "utf8")).match(/^SONIOX_API_KEY=/gm),+ ).toHaveLength(1);+ },+ );++ it("keeps comments and CRLF line endings while removing duplicate assignments", async () => {+ const directory = path.join(await root(), "instance");+ await mkdir(directory);+ const file = userEnvironmentFile(directory);+ await writeFile(+ file,+ '# API key\r\nSONIOX_API_KEY=old\r\n# Keep this comment\r\nexport SONIOX_API_KEY="later"\r\nTZ=UTC\r\n',+ { mode: 0o600 },+ );+ await updateUserKeys(directory, { SONIOX_API_KEY: "replacement" });+ expect(await readFile(file, "utf8")).toBe(+ "# API key\r\nSONIOX_API_KEY=replacement\r\n# Keep this comment\r\nTZ=UTC\r\n",+ );+ expect(await readUserEnvironment(directory)).toEqual({+ SONIOX_API_KEY: "replacement",+ TZ: "UTC",+ });+ });++ it("appends an absent key after existing settings without a final newline", async () => {+ const directory = path.join(await root(), "instance");+ await mkdir(directory);+ const file = userEnvironmentFile(directory);+ await writeFile(file, "# Keep this comment\nTZ=UTC", { mode: 0o600 });+ await updateUserKeys(directory, { SONIOX_API_KEY: "replacement" });+ expect(await readFile(file, "utf8")).toBe(+ "# Keep this comment\nTZ=UTC\nSONIOX_API_KEY=replacement\n",+ );+ expect(await readUserEnvironment(directory)).toEqual({+ SONIOX_API_KEY: "replacement",+ TZ: "UTC",+ });+ });+it("saves keys owner-only before the instance exists and keeps the rest of the file", async () => {const directory = path.join(await root(), "instances", "desktop");expect(diff --git a/packages/local-installation/src/user-environment.ts b/packages/local-installation/src/user-environment.tsindex 36db7d43..a042b036 100644--- a/packages/local-installation/src/user-environment.ts+++ b/packages/local-installation/src/user-environment.ts@@ -52,6 +52,23 @@ function isUserKey(name: string): name is UserEnvironmentKey {return (USER_ENVIRONMENT_KEYS as readonly string[]).includes(name);}+function setAssignment(text: string, name: string, value: string): string {+ const line = `${name}=${value}`;+ const pattern = new RegExp(+ `^[\\t ]*(?:export[\\t ]+)?${name}[\\t ]*=[^\\r\\n]*(\\r?\\n|$)`,+ "gm",+ );+ let replaced = false;+ const updated = text.replace(pattern, (_assignment, newline: string) => {+ if (replaced) return "";+ replaced = true;+ return `${line}${newline}`;+ });+ return replaced+ ? updated+ : `${text.endsWith("\n") || !text ? text : `${text}\n`}${line}\n`;+}+async function readUserFile(root: string): Promise<string | undefined> {const file = userEnvironmentFile(root);try {@@ -151,11 +168,7 @@ export async function adoptUserEnvironment(const existing = parseEnv(text);for (const [name, value] of found) {if (existing[name]?.trim()) continue;- const line = `${name}='${value}'`;- const pattern = new RegExp(`^${name}=.*$`, "m");- text = pattern.test(text)- ? text.replace(pattern, () => line)- : `${text.endsWith("\n") ? text : `${text}\n`}${line}\n`;+ text = setAssignment(text, name, `'${value}'`);moved.push(name);}await atomicPrivate(userEnvironmentFile(root), text);@@ -207,11 +220,7 @@ export async function updateUserKeys(await privateDirectory(root);let text = (await readUserFile(root)) ?? TEMPLATE;for (const [name, value] of Object.entries(changes)) {- const line = `${name}=${value}`;- const pattern = new RegExp(`^${name}=.*$`, "gm");- text = pattern.test(text)- ? text.replace(pattern, () => line)- : `${text.endsWith("\n") || !text ? text : `${text}\n`}${line}\n`;+ text = setAssignment(text, name, value);}await atomicPrivate(userEnvironmentFile(root), text);const set = parseEnv(text);