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(node-services): unmount memory routes when the server closes

Tsan1024dd2c11594fd9
diff --git a/apps/node-services/AGENTS.md b/apps/node-services/AGENTS.md
index d2988ec6..ccccaef9 100644
--- a/apps/node-services/AGENTS.md
+++ b/apps/node-services/AGENTS.md
@@ -14,6 +14,8 @@
not collide. The host alone answers `/healthz` (process alive), `/startupz`
(every selected role started), and `/readyz` (started and every mounted
role's readiness ok); roles must not register their own probes.
+ Stopping Memory unmounts only its own routes. A shared listener and other
+ roles remain available, and a replacement Memory server can mount again.
- The `document` role keeps its own port (4321 by default) on its own worker
thread (`src/document/main.ts`) because it installs jsdom globals and
replaces the global `Request`/`Response`. The `voice` role supervises separate ASR, recorder and Live child processes (ports 4324–4326); it does not run audio jobs on the main thread. Voice workers follow TeamHub's effective provider keys (`GET /internal/v1/voice/provider-keys` with the `voice-runtime` credential): a Soniox key runs ASR and recorder, an xAI key runs Live. The role reconciles at startup, on TeamHub's `POST /v1/voice/workers:reconcile` (`node-services-internal` bearer), and every 60 seconds, so workers start, drain, or restart with a new key without restarting node-services. The reconcile route answers `{ready}` only after every wanted worker's health port reports it registered with LiveKit, or after 20 seconds, so TeamHub answers a key save once a voice session can be dispatched. Keys reach a worker only through its environment; a failed key fetch leaves the running workers unchanged. Other roles run on the main thread
diff --git a/apps/node-services/src/memory/server.ts b/apps/node-services/src/memory/server.ts
index cdceb68e..0e1fb994 100644
--- a/apps/node-services/src/memory/server.ts
+++ b/apps/node-services/src/memory/server.ts
@@ -406,13 +406,14 @@ export async function startMemoryServer(options: {
port: options.port ?? 0,
});
const http = options.http ?? ownedHost!;
- http.mount({
+ const unmount = http.mount({
prefixes: ["/internal/v1/"],
fetch: (request) => app.fetch(request),
});
ownedHost?.finishStartup();
return {
close: async () => {
+ unmount();
await ownedHost?.close();
},
url: http.url,
diff --git a/apps/node-services/tests/memory/server.test.ts b/apps/node-services/tests/memory/server.test.ts
index 536e2f91..cdd7a331 100644
--- a/apps/node-services/tests/memory/server.test.ts
+++ b/apps/node-services/tests/memory/server.test.ts
@@ -7,6 +7,7 @@ import { afterEach, describe, expect, it, vi } from "vitest";
import type { MemoryScheduleConfig } from "../../src/memory/config.js";
import { PersonalKnowledgeGraphNotFoundError } from "../../src/memory/graph-visualization.js";
import { MemoryScheduler } from "../../src/memory/scheduler.js";
+import { startHttpHost } from "../../src/http-host.js";
import { startMemoryServer } from "../../src/memory/server.js";
const roots: string[] = [];
@@ -25,6 +26,44 @@ afterEach(async () => {
});
describe("Memory server", () => {
+ it("unmounts on close without stopping other roles and allows a replacement", async () => {
+ const dataRoot = await fs.mkdtemp(path.join(os.tmpdir(), "memory-server-"));
+ roots.push(dataRoot);
+ const http = await startHttpHost({ host: "127.0.0.1", port: 0 });
+ http.mount({ prefixes: ["/v1/"], fetch: () => new Response("agent") });
+ const options = {
+ dataRoot,
+ http,
+ scheduler: new MemoryScheduler(schedule, async () => undefined),
+ serviceToken: "memory-token",
+ };
+ try {
+ const server = await startMemoryServer(options);
+ http.finishStartup();
+ expect(
+ (await fetch(`${http.url}/internal/v1/memory/entries`)).status,
+ ).toBe(401);
+ await server.close();
+ await server.close();
+ expect(
+ (await fetch(`${http.url}/internal/v1/memory/entries`)).status,
+ ).toBe(404);
+ expect(await (await fetch(`${http.url}/v1/status`)).text()).toBe("agent");
+ const replacement = await startMemoryServer(options);
+ try {
+ // Closing the previous instance again must not remove its replacement.
+ await server.close();
+ expect(
+ (await fetch(`${http.url}/internal/v1/memory/entries`)).status,
+ ).toBe(401);
+ } finally {
+ await replacement.close();
+ }
+ } finally {
+ await http.close();
+ }
+ });
+
it("lists and reads only authenticated Memory entries through the internal API", async () => {
const dataRoot = await fs.mkdtemp(path.join(os.tmpdir(), "memory-server-"));
roots.push(dataRoot);