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): stop native node-services only for a TeamHub restart
shoudi zheng0435a6d46ddc
diff --git a/packages/local-installation/AGENTS.md b/packages/local-installation/AGENTS.mdindex 69c9a77b..305150b3 100644--- a/packages/local-installation/AGENTS.md+++ b/packages/local-installation/AGENTS.md@@ -23,8 +23,8 @@ the key pair and media ports in the instance's `livekit.env`; the gatewayforwards `/rtc` to it. `compose up` recreates what a changed `user.env`touches; a native process restarts when its generated environment changes.Every update or recreate path stops node-services on its own, right before-the recreate and while TeamHub is still up, so its graceful shutdown requeues-in-flight Agent Runs instead of failing them.+TeamHub restarts (natively: for a changed configuration) and while TeamHub is+still up, so its graceful shutdown requeues in-flight Agent Runs.Move a freshly downloaded or unpacked file or folder into place with`renameRetrying` from `fs-retry.ts`: on Windows antivirus and indexing holddiff --git a/packages/local-installation/src/native-process.test.ts b/packages/local-installation/src/native-process.test.tsindex 924f29f0..e89e8335 100644--- a/packages/local-installation/src/native-process.test.ts+++ b/packages/local-installation/src/native-process.test.ts@@ -21,6 +21,7 @@ vi.mock("./instance.js", async (original) => {});import { afterEach, expect, test, vi } from "vitest";import {+ nativeConfigurationChanged,startNativeProcess,inspectNativeProcess,stopNativeProcess,@@ -401,3 +402,25 @@ test.each([false, true])(},25_000,);+test("reports a configuration change only for a running recorded process", async () => {+ const root = await fixture();+ const options = {+ root,+ name: "teamhub" as const,+ executable: process.execPath,+ args: ["-e", "setInterval(()=>{},1000)"],+ cwd: root,+ env: process.env,+ settings: "first",+ };+ expect(await nativeConfigurationChanged(options)).toBe(false);+ await startNativeProcess(options);+ expect(await nativeConfigurationChanged(options)).toBe(false);+ expect(+ await nativeConfigurationChanged({ ...options, settings: "second" }),+ ).toBe(true);+ await stopNativeProcess(root, "teamhub");+ expect(+ await nativeConfigurationChanged({ ...options, settings: "second" }),+ ).toBe(false);+});diff --git a/packages/local-installation/src/native-process.ts b/packages/local-installation/src/native-process.tsindex 1cf6134a..515bbaaa 100644--- a/packages/local-installation/src/native-process.ts+++ b/packages/local-installation/src/native-process.ts@@ -121,6 +121,36 @@ export async function inspectNativeProcess(pid: record.pid,};}+interface NativeConfiguration {+ executable: string;+ args: string[];+ cwd: string;+ settings?: string;+}+function configurationHash(options: NativeConfiguration): string {+ return createHash("sha256")+ .update(+ JSON.stringify({+ executable: options.executable,+ args: options.args,+ cwd: options.cwd,+ settings: options.settings,+ }),+ )+ .digest("hex");+}+/** True only when a recorded, running process would restart for this configuration. */+export async function nativeConfigurationChanged(+ options: NativeConfiguration & { root: string; name: NativeService },+): Promise<boolean> {+ const existing = await stored(options.root, options.name);+ if (+ !existing ||+ (await observed(existing.pid, existing.lease)) !== existing.birth+ )+ return false;+ return configurationHash(options) !== existing.configuration;+}export async function startNativeProcess(options: {root: string;name: NativeService;@@ -136,16 +166,7 @@ export async function startNativeProcess(options: {}): Promise<NativeProcessState> {options.signal?.throwIfAborted();const location = paths(options.root, options.name);- const configuration = createHash("sha256")- .update(- JSON.stringify({- executable: options.executable,- args: options.args,- cwd: options.cwd,- settings: options.settings,- }),- )- .digest("hex");+ const configuration = configurationHash(options);const existing = await stored(options.root, options.name);if (existing &&diff --git a/packages/local-installation/src/native-source-flow.test.ts b/packages/local-installation/src/native-source-flow.test.tsindex f1e4e044..8ba0e530 100644--- a/packages/local-installation/src/native-source-flow.test.ts+++ b/packages/local-installation/src/native-source-flow.test.ts@@ -41,6 +41,7 @@ import {import { prepareRuntime } from "./managed-runtimes.js";import {inspectNativeProcess,+ nativeConfigurationChanged,startNativeProcess,stopNativeProcess,} from "./native-process.js";@@ -71,6 +72,7 @@ vi.mock("./managed-codex.js", () => ({}));vi.mock("./native-process.js", () => ({inspectNativeProcess: vi.fn(),+ nativeConfigurationChanged: vi.fn(),startNativeProcess: vi.fn(),stopNativeProcess: vi.fn(),}));@@ -122,6 +124,7 @@ async function fixture() {running.add(options.name);return { running: true, pid: 100 };});+ vi.mocked(nativeConfigurationChanged).mockResolvedValue(false);vi.mocked(stopNativeProcess).mockImplementation(async (_root, name) => {running.delete(name);});@@ -316,21 +319,48 @@ test("failed preparation never stops services that were already running", asyncexpect(f.running.has("postgres")).toBe(true);});-test("a running node-services stops before TeamHub restarts", async () => {- const f = await fixture();- for (const name of ["teamhub", "node-services", "web"]) f.running.add(name);+async function runFlow(f: Awaited<ReturnType<typeof fixture>>) {vi.mocked(prepareRuntime).mockResolvedValue({code: 0,state: "ready",message: "ready",});- await installNativeSource({+ return installNativeSource({...f,name: "native-flow",setupChoice: "pi",signal: new AbortController().signal,onEvent: () => {},});+}++test("an unchanged TeamHub leaves a running node-services alone", async () => {+ const f = await fixture();+ for (const name of ["teamhub", "node-services", "web"]) f.running.add(name);+ await runFlow(f);+ expect(vi.mocked(stopNativeProcess)).not.toHaveBeenCalledWith(+ expect.anything(),+ "node-services",+ );+});++test("a stopped TeamHub never stops node-services up front", async () => {+ const f = await fixture();+ f.running.add("node-services");+ await runFlow(f);+ expect(vi.mocked(stopNativeProcess)).not.toHaveBeenCalledWith(+ expect.anything(),+ "node-services",+ );+});++test("a running node-services stops before TeamHub restarts", async () => {+ const f = await fixture();+ for (const name of ["teamhub", "node-services", "web"]) f.running.add(name);+ vi.mocked(nativeConfigurationChanged).mockImplementation(+ async (options) => options.name === "teamhub",+ );+ await runFlow(f);expect(vi.mocked(stopNativeProcess).mock.calls.map((call) => call[1]),).toEqual(["node-services"]);@@ -343,34 +373,27 @@ test("a running node-services stops before TeamHub restarts", async () => {expect(f.running.has("node-services")).toBe(true);});-test("a failure after the restart stops the node-services this run started", async () => {+test("a failure after the restart keeps the node-services that ran before", async () => {const f = await fixture();for (const name of ["postgres", "teamhub", "node-services"])f.running.add(name);- vi.mocked(prepareRuntime).mockResolvedValue({- code: 0,- state: "ready",- message: "ready",- });+ vi.mocked(nativeConfigurationChanged).mockImplementation(+ async (options) => options.name === "teamhub",+ );vi.mocked(startNativeProcess).mockImplementation(async (options) => {if (options.name === "web") throw new Error("web failed");f.running.add(options.name);return { running: true, pid: 100 };});- const result = await installNativeSource({- ...f,- name: "native-flow",- setupChoice: "pi",- signal: new AbortController().signal,- onEvent: () => {},- });+ const result = await runFlow(f);expect(result.code).toBe(1);- // The first stop hands in-flight Runs back; the rollback stops the- // processes this run started, and leaves the earlier TeamHub alone.+ // The pre-stop hands in-flight Runs back; the rollback stops only what this+ // run started, never the node-services or TeamHub that were already running.expect(vi.mocked(stopNativeProcess).mock.calls.map((call) => call[1]),- ).toEqual(["node-services", "web", "node-services"]);+ ).toEqual(["node-services", "web"]);expect(f.running.has("teamhub")).toBe(true);+ expect(f.running.has("node-services")).toBe(true);});test("stops native business services even if owned gateway shutdown reports a failure", async () => {diff --git a/packages/local-installation/src/native-source.ts b/packages/local-installation/src/native-source.tsindex e30e7285..1a8d8546 100644--- a/packages/local-installation/src/native-source.ts+++ b/packages/local-installation/src/native-source.ts@@ -29,6 +29,7 @@ import { npmRegistryEnvironment } from "./npm-registry.js";import { prepareNativeDatabase } from "./native-database.js";import {inspectNativeProcess,+ nativeConfigurationChanged,startNativeProcess,stopNativeProcess,type NativeService,@@ -789,31 +790,40 @@ async function runNativeSource((await inspectNativeProcess(current.root, "livekit")).running)await stopNativeProcess(current.root, "livekit");- // Stop the Agent Service while TeamHub is still up so its graceful- // shutdown can requeue in-flight Agent Runs before TeamHub restarts.- // The loop below then sees it stopped and records it as started here.- if ((await inspectNativeProcess(current.root, "node-services")).running)- await stopNativeProcess(current.root, "node-services");+ const serviceOptions = (name: "teamhub" | "node-services" | "web") => ({+ root: current.root,+ name,+ executable: process.execPath,+ args: [+ path.join(current.source!.repo, "scripts/source-native.mjs"),+ name,+ ],+ cwd: current.source!.repo,+ env,+ signal,+ redactions: secrets,+ // A changed user.env, port, credential or voice tool restarts it.+ settings,+ restartOnConfigurationChange: true,+ });+ // Stop the Agent Service only when TeamHub is about to restart for a+ // changed configuration, so its graceful shutdown can requeue+ // in-flight Runs while TeamHub is still up. When TeamHub is not+ // running or not changing, node-services is left alone so it can+ // reconnect. It was running before this run, so it is never recorded+ // as started: if a later step fails, its replacement stays up beside+ // the new TeamHub; if TeamHub's own restart fails, node-services+ // stays stopped until the next resume.+ const preStopped =+ (await nativeConfigurationChanged(serviceOptions("teamhub"))) &&+ (await inspectNativeProcess(current.root, "node-services")).running;+ if (preStopped) await stopNativeProcess(current.root, "node-services");for (const name of ["teamhub", "node-services", "web"] as const) {const running = (await inspectNativeProcess(current.root, name)).running;- if (!running) started.push(name);- await startNativeProcess({- root: current.root,- name,- executable: process.execPath,- args: [- path.join(current.source!.repo, "scripts/source-native.mjs"),- name,- ],- cwd: current.source!.repo,- env,- signal,- redactions: secrets,- // A changed user.env, port, credential or voice tool restarts it.- settings,- restartOnConfigurationChange: true,- });+ if (!running && !(preStopped && name === "node-services"))+ started.push(name);+ await startNativeProcess(serviceOptions(name));}const checks = await waitHealthy(current, signal, emit, ends);emit({