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): wait for the replacement voice worker after a key change

shoudi zheng1843eff165f7
diff --git a/apps/node-services/src/voice/service.ts b/apps/node-services/src/voice/service.ts
index d76b54ab..a8f5b4a8 100644
--- a/apps/node-services/src/voice/service.ts
+++ b/apps/node-services/src/voice/service.ts
@@ -161,6 +161,8 @@ export async function startVoiceService(options: {
digest?: string;
generation: number;
child?: ChildProcess;
+ /** The generation that started `child`; an older one is draining. */
+ childGeneration?: number;
exited?: Promise<void>;
retry?: NodeJS.Timeout;
failures: number;
@@ -252,6 +254,7 @@ export async function startVoiceService(options: {
return;
}
entry.child = child;
+ entry.childGeneration = generation;
entry.exited = new Promise<void>((resolve) => {
let settled = false;
const finish = (error?: unknown) => {
@@ -341,10 +344,16 @@ export async function startVoiceService(options: {
const workersReady = async () =>
(
await Promise.all(
- wantedWorkers().map(
- async (worker) =>
- Boolean(entries.get(worker)?.child) && (await probeWorker(worker)),
- ),
+ wantedWorkers().map(async (worker) => {
+ const entry = entries.get(worker)!;
+ // A replaced worker answers on the same port until it exits, so
+ // only the child of the current generation counts.
+ return (
+ Boolean(entry.child) &&
+ entry.childGeneration === entry.generation &&
+ (await probeWorker(worker))
+ );
+ }),
)
).every(Boolean);
/** Reconciles, then waits until every wanted worker can take a job, so the
diff --git a/apps/node-services/tests/voice-service.test.ts b/apps/node-services/tests/voice-service.test.ts
index 2ccb1d24..255e1439 100644
--- a/apps/node-services/tests/voice-service.test.ts
+++ b/apps/node-services/tests/voice-service.test.ts
@@ -306,6 +306,35 @@ describe("voice worker reconcile", () => {
await shutdown();
});
+ it("waits for the replacement worker after a key change", async () => {
+ const { route, teamHub, health, shutdown, of } = await harness({
+ keys: { xai_api_key: XAI },
+ });
+ expect(of("live")).toHaveLength(1);
+ teamHub.keys = { xai_api_key: "xai-key-2" };
+ let answer: unknown;
+ const answered = Promise.resolve(route.fetch(reconcileRequest())).then(
+ async (response) => {
+ answer = await response.json();
+ },
+ );
+ // The draining worker still answers healthy; it must not count.
+ await vi.advanceTimersByTimeAsync(1_000);
+ expect(of("live")[0]!.child.kill).toHaveBeenCalledWith("SIGTERM");
+ expect(of("live")).toHaveLength(1);
+ expect(answer).toBeUndefined();
+ health.healthy = false;
+ of("live")[0]!.child.emit("exit", 0, null);
+ await vi.advanceTimersByTimeAsync(250);
+ expect(of("live")).toHaveLength(2);
+ expect(answer).toBeUndefined();
+ health.healthy = true;
+ await vi.advanceTimersByTimeAsync(250);
+ await answered;
+ expect(answer).toEqual({ ready: true });
+ await shutdown();
+ });
+
it("answers not ready when the workers miss the wait", async () => {
const { route, teamHub, shutdown } = await harness({ healthy: false });
teamHub.keys = { soniox_api_key: SONIOX };