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(installer): bind native sign-in sessions to their runtime

Afloat63c4b5556fc9
diff --git a/apps/linger-installer/AGENTS.md b/apps/linger-installer/AGENTS.md
index e11c7b94..4acc670e 100644
--- a/apps/linger-installer/AGENTS.md
+++ b/apps/linger-installer/AGENTS.md
@@ -25,6 +25,9 @@ An idle desktop-owned manager steps aside when the desktop relaunches the same
instance with another checkout. Every installation runs natively from the
source checkout `--source-repo` names. Start failures print their real cause
on stderr for the desktop to show.
+Native sign-in sessions belong to an instance and its selected runtime. A changed
+setup choice must not reuse another runtime's URL or accept a code for its process;
+starting sign-in for the new runtime stops the previous runtime's pending login.
`--resume` (native source with `--instance`) starts an installed instance's
stopped services headless, reusing its saved choices; the desktop client runs it
diff --git a/apps/linger-installer/src/server/runtime-login-provider.test.ts b/apps/linger-installer/src/server/runtime-login-provider.test.ts
new file mode 100644
index 00000000..e521d709
--- /dev/null
+++ b/apps/linger-installer/src/server/runtime-login-provider.test.ts
@@ -0,0 +1,182 @@
+import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
+import { tmpdir } from "node:os";
+import path from "node:path";
+import { expect, it, vi } from "vitest";
+import { RuntimeLoginManager } from "./runtime-login.js";
+
+vi.mock("@linger/local-installation/managed-codex", () => ({
+ managedRuntimeEnvironment: async () => ({}),
+}));
+
+const urls = {
+ codex: "https://auth.openai.com/codex/device",
+ claude: "https://claude.com/cai/oauth/authorize?state=fixture",
+};
+
+async function setup() {
+ const home = await mkdtemp(path.join(tmpdir(), "runtime-provider-"));
+ const program = path.join(home, "login.mjs");
+ const starts = path.join(home, "starts.txt");
+ const codes = path.join(home, "codes.txt");
+ const ready = new Set<string>();
+ await writeFile(
+ program,
+ `
+import fs from 'node:fs';
+const runtime = process.argv[2];
+fs.appendFileSync(${JSON.stringify(starts)}, runtime + '\\n');
+fs.writeFileSync(${JSON.stringify(home)} + '/' + runtime + '.pid', String(process.pid));
+console.log(${JSON.stringify(urls)}[runtime]);
+process.stdin.on('data', (chunk) => fs.appendFileSync(${JSON.stringify(codes)}, chunk));
+setTimeout(() => process.exit(0), 30000);
+`,
+ );
+ const manager = new RuntimeLoginManager(async (runtime) => {
+ const command = {
+ executable: process.execPath,
+ args: [program, runtime],
+ env: {},
+ };
+ return {
+ runtime,
+ executable: process.execPath,
+ installed: true,
+ ready: ready.has(runtime),
+ loginCommand: "unused",
+ descriptor: {
+ runtime,
+ executable: process.execPath,
+ argvPrefix: [],
+ installed: true,
+ authenticated: ready.has(runtime),
+ authMode: null,
+ loginCommand: command,
+ deviceLoginCommand: command,
+ },
+ };
+ }, "linux");
+ const input = (runtime: "codex" | "claude") => ({
+ instance: "default",
+ home,
+ root: home,
+ runtime,
+ });
+ const wait = async (runtime: "codex" | "claude") => {
+ await expect
+ .poll(async () => (await manager.status(input(runtime))).url, {
+ timeout: 5000,
+ })
+ .toBe(urls[runtime]);
+ };
+ return {
+ manager,
+ input,
+ wait,
+ starts,
+ codes,
+ ready,
+ pid: async (runtime: "codex" | "claude") =>
+ Number(await readFile(path.join(home, `${runtime}.pid`), "utf8")),
+ cleanup: async () => {
+ manager.close();
+ await rm(home, { recursive: true, force: true });
+ },
+ };
+}
+
+function alive(pid: number) {
+ try {
+ process.kill(pid, 0);
+ return true;
+ } catch (error) {
+ if ((error as NodeJS.ErrnoException).code === "ESRCH") return false;
+ throw error;
+ }
+}
+
+it(
+ "does not report another runtime's pending sign-in after the setup choice changes",
+ { timeout: 15000 },
+ async () => {
+ const f = await setup();
+ try {
+ await f.manager.start(f.input("claude"));
+ await f.wait("claude");
+ expect(await f.manager.status(f.input("codex"))).toEqual({
+ state: "idle",
+ });
+ expect((await f.manager.status(f.input("claude"))).url).toBe(urls.claude);
+ } finally {
+ await f.cleanup();
+ }
+ },
+);
+
+it(
+ "replaces the previous runtime's sign-in while deduplicating the selected runtime",
+ { timeout: 15000 },
+ async () => {
+ const f = await setup();
+ try {
+ await f.manager.start(f.input("codex"));
+ await f.wait("codex");
+ const previous = await f.pid("codex");
+ expect(alive(previous)).toBe(true);
+ expect((await f.manager.start(f.input("claude"))).state).toBe("starting");
+ await f.wait("claude");
+ await expect.poll(() => alive(previous), { timeout: 5000 }).toBe(false);
+ expect((await f.manager.start(f.input("claude"))).url).toBe(urls.claude);
+ expect((await readFile(f.starts, "utf8")).trim().split("\n")).toEqual([
+ "codex",
+ "claude",
+ ]);
+ } finally {
+ await f.cleanup();
+ }
+ },
+);
+
+it(
+ "stops the previous login when the selected runtime is already ready",
+ { timeout: 15000 },
+ async () => {
+ const f = await setup();
+ try {
+ await f.manager.start(f.input("codex"));
+ await f.wait("codex");
+ const previous = await f.pid("codex");
+ expect(alive(previous)).toBe(true);
+ f.ready.add("claude");
+ expect(await f.manager.start(f.input("claude"))).toEqual({
+ state: "ready",
+ });
+ await expect.poll(() => alive(previous), { timeout: 5000 }).toBe(false);
+ expect((await readFile(f.starts, "utf8")).trim()).toBe("codex");
+ } finally {
+ await f.cleanup();
+ }
+ },
+);
+
+it(
+ "accepts a pasted code only for the runtime that owns the pending sign-in",
+ { timeout: 15000 },
+ async () => {
+ const f = await setup();
+ try {
+ await f.manager.start(f.input("claude"));
+ await f.wait("claude");
+ await expect(
+ f.manager.start(f.input("codex"), "fixture-code"),
+ ).rejects.toThrow("Invalid authorization code or inactive login");
+ await f.manager.start(f.input("claude"), "fixture-code");
+ await expect
+ .poll(() => readFile(f.codes, "utf8").catch(() => ""), {
+ timeout: 5000,
+ })
+ .toBe("fixture-code\n");
+ } finally {
+ await f.cleanup();
+ }
+ },
+);
diff --git a/apps/linger-installer/src/server/runtime-login.ts b/apps/linger-installer/src/server/runtime-login.ts
index fcd786df..b2aea3f0 100644
--- a/apps/linger-installer/src/server/runtime-login.ts
+++ b/apps/linger-installer/src/server/runtime-login.ts
@@ -15,6 +15,7 @@ interface Input {
runtime: NativeRuntime;
}
interface Session {
+ runtime: NativeRuntime;
snapshot: Snapshot;
child: ChildProcess;
timer: ReturnType<typeof setTimeout>;
@@ -70,15 +71,17 @@ export class RuntimeLoginManager {
// callback. Let it exit naturally; killing it here races that response.
return { state: "ready" };
}
+ const session = this.sessions.get(input.instance);
return structuredClone(
- this.sessions.get(input.instance)?.snapshot ?? { state: "idle" },
+ session?.runtime === input.runtime ? session.snapshot : { state: "idle" },
);
}
async start(input: Input, code?: string): Promise<Snapshot> {
if (code !== undefined) {
const session = this.sessions.get(input.instance);
if (
- !session?.snapshot.acceptsCode ||
+ session?.runtime !== input.runtime ||
+ !session.snapshot.acceptsCode ||
!session.child.stdin?.writable ||
!/^[A-Za-z0-9._~+/#=-]{1,4096}$/.test(code)
)
@@ -103,10 +106,13 @@ export class RuntimeLoginManager {
return { state: "idle" };
this.starting.delete(input.instance);
const { env, state } = result;
- if (state.ready) return { state: "ready" };
const existing = this.sessions.get(input.instance);
+ if (existing && existing.runtime !== input.runtime)
+ this.stop(input.instance);
+ if (state.ready) return { state: "ready" };
if (
existing &&
+ existing.runtime === input.runtime &&
["starting", "waiting_user"].includes(existing.snapshot.state)
)
return structuredClone(existing.snapshot);
@@ -136,6 +142,7 @@ export class RuntimeLoginManager {
stdio: ["pipe", "pipe", "pipe"],
});
const session: Session = {
+ runtime: input.runtime,
child,
snapshot: { state: "starting" },
timer: setTimeout(() => {