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): start Memory's scheduler after every role has started
shoudi zhengd3edb8906857
diff --git a/apps/node-services/AGENTS.md b/apps/node-services/AGENTS.mdindex a979fbb1..81368d81 100644--- a/apps/node-services/AGENTS.md+++ b/apps/node-services/AGENTS.md@@ -23,7 +23,11 @@and must not install process globals.- `NODE_SERVICES_ROLES` selects roles as a comma-separated list. An unset orempty value starts every role; an unknown role fails startup. Selected roles- always start in the order `memory`, `llm-wiki`, `agent`, `voice`, `document`. Shutdown starts media draining immediately while other roles stop in reverse. The shared listener starts only when an HTTP role is selected.+ always start in the order `memory`, `llm-wiki`, `agent`, `voice`, `document`. Shutdown starts media draining immediately while other roles stop in reverse.+ Background work that uses a later role's seam starts from the role's+ `started()`, which the host calls once every selected role has started, and+ stops from `stopping()`, called before the first role stops: Memory's+ scheduler runs only while the agent role's model call can be there. The shared listener starts only when an HTTP role is selected.- `src/main.ts` is the only process entrypoint. It owns SIGINT/SIGTERM through`src/process-lifecycle.ts`: the first signal aborts startup or startsordered shutdown under the fixed 95-second `SHUTDOWN_TIMEOUT_MS`, laterdiff --git a/apps/node-services/src/main.ts b/apps/node-services/src/main.tsindex 6500af21..1a4dcb66 100644--- a/apps/node-services/src/main.ts+++ b/apps/node-services/src/main.ts@@ -22,6 +22,12 @@ import {} from "./process-lifecycle.js";interface RunningRole {+ /** Called once every selected role has started, for background work that+ * uses a seam a later role fills, such as the agent role's model call. */+ started?(): void;+ /** Called on every role before the first role stops, so that background+ * work stops taking new items while the roles it calls drain. */+ stopping?(): void;stop(): Promise<void>;}@@ -34,7 +40,7 @@ interface RoleHost extends ProcessControl {readonly normalizer: DocumentNormalizer;/** What a role hands to the roles that start after it. The agent role fills* modelCall after Memory and LLM Wiki have started, so they read it when- * they call. */+ * they call, and Memory's scheduler starts only from started(). */readonly seams: { flashback?: Flashback; modelCall?: ModelCall };/** Replaces the Agent Service's Room request timeout in process tests. */readonly agentRoomRequestTimeoutMs?: number;@@ -165,6 +171,7 @@ export async function main(const stopRoles = async (): Promise<void> => {const failures: unknown[] = [];const stopping = running.splice(0).reverse();+ for (const { role } of stopping) role.stopping?.();const voice = stopping.find(({ name }) => name === "voice");const voiceStopped = voice?.role.stop().catch((error) => {failures.push(error);@@ -206,6 +213,7 @@ export async function main(});running.push({ name, role });}+ for (const { role } of running) role.started?.();http?.finishStartup();if (http) logger.log("info", "host.listening", { url: http.url });} catch (error) {diff --git a/apps/node-services/src/memory/main.ts b/apps/node-services/src/memory/main.tsindex 20e0727c..3d966799 100644--- a/apps/node-services/src/memory/main.ts+++ b/apps/node-services/src/memory/main.ts@@ -44,6 +44,10 @@ export async function startMemoryService({}): Promise<{/** Another Me flashback, which the host hands to the agent role. */flashback: Flashback;+ /** Starts the scheduler once every role in the process has started. */+ started(): void;+ /** Stops the scheduler's timer before the agent role stops. */+ stopping(): void;stop(): Promise<void>;}> {setMemoryLogger(logger);@@ -132,7 +136,6 @@ export async function startMemoryService({chunkIndex.indexHold = indexHold;wikiIndex.indexHold = indexHold;const flashback = new FlashbackService(config.dataRoot);- scheduler.start();const server = await startMemoryServer({dataRoot: config.dataRoot,http,@@ -198,5 +201,13 @@ export async function startMemoryService({await chunkIndex.close();await closeMemoryDatabase();};- return { flashback, stop };+ return {+ flashback,+ // The agent role, which provides the model call, starts after Memory and+ // stops before it; the scheduler runs only while the model call can be+ // there.+ started: () => scheduler.start(),+ stopping: () => scheduler.stop(),+ stop,+ };}diff --git a/apps/node-services/tests/main-role-lifecycle.test.ts b/apps/node-services/tests/main-role-lifecycle.test.tsnew file mode 100644index 00000000..267b57fa--- /dev/null+++ b/apps/node-services/tests/main-role-lifecycle.test.ts@@ -0,0 +1,82 @@+import { afterEach, describe, expect, it, vi } from "vitest";++const events: string[] = [];+let close: (() => Promise<void>) | undefined;++vi.mock("../src/process-lifecycle.js", () => ({+ SHUTDOWN_TIMEOUT_MS: 1_000,+ runServiceProcess: async (+ start: (control: unknown) => Promise<{ close(): Promise<void> }>,+ ) => {+ const running = await start({+ signal: new AbortController().signal,+ progress: () => undefined,+ });+ close = running.close;+ },+}));++vi.mock("../src/http-host.js", () => ({+ startHttpHost: async () => ({+ url: "http://127.0.0.1:0",+ finishStartup: () => events.push("http:finishStartup"),+ close: async () => undefined,+ }),+}));++vi.mock("../src/model-packages.js", () => ({+ pruneModelPackages: async () => [],+}));++vi.mock("../src/memory/main.js", () => ({+ startMemoryService: async () => {+ events.push("memory:start");+ return {+ flashback: {},+ started: () => events.push("memory:started"),+ stopping: () => events.push("memory:stopping"),+ stop: async () => {+ events.push("memory:stop");+ },+ };+ },+}));++vi.mock("../src/agent/main.js", () => ({+ startAgentService: async () => {+ events.push("agent:start");+ return {+ modelCall: {},+ stop: async () => {+ events.push("agent:stop");+ },+ };+ },+}));++afterEach(() => {+ events.length = 0;+ close = undefined;+ vi.unstubAllEnvs();+});++describe("node-services role lifecycle", () => {+ it("starts background work after every role and stops it before any role", async () => {+ vi.stubEnv("NODE_SERVICES_ROLES", "memory,agent");+ vi.stubEnv("NODE_ENV", "test");+ const { main } = await import("../src/main.js");+ await main();+ // Memory's scheduler needs the agent role's model call, which exists only+ // once the agent role has started.+ expect(events).toEqual([+ "memory:start",+ "agent:start",+ "memory:started",+ "http:finishStartup",+ ]);++ events.length = 0;+ await close?.();+ expect(events).toEqual(["memory:stopping", "agent:stop", "memory:stop"]);+ });+});