test(daemon): observe restart through the worker status RPC

The bounded CLI status observation may omit worker details while an authenticated connection remains healthy. Exercise the app-style restart through its full RPC and retain the replacement observation, with a real slow provider probe covering the distinction.
This commit is contained in:
Mohamed Boudra
2026-09-09 15:56:36 +02:00
parent 01c2b7f66e
commit 448c592c6a
@@ -7,15 +7,19 @@
import assert from "node:assert";
import { spawn, type ChildProcess } from "node:child_process";
import { mkdtemp, readFile, rm } from "node:fs/promises";
import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { $ } from "zx";
import { tryConnectToDaemon } from "../src/utils/client.ts";
import { WebSocket } from "ws";
import {
DaemonClient,
DaemonConnectionError,
type WebSocketLike,
} from "@getpaseo/client/internal/daemon-client";
import { readDaemonInstance, isSameDaemonInstance } from "@getpaseo/server";
import { runLocalPaseo } from "./helpers/local-cli.ts";
import { getAvailablePort } from "./helpers/network.ts";
$.verbose = false;
const pollIntervalMs = 100;
const testEnv = {
PASEO_LOCAL_SPEECH_AUTO_DOWNLOAD: process.env.PASEO_LOCAL_SPEECH_AUTO_DOWNLOAD ?? "0",
@@ -40,36 +44,6 @@ function isProcessRunning(pid: number): boolean {
}
}
interface DaemonStatus {
localDaemon: string | null;
pid: number | null;
workerPid: number | null;
}
async function readDaemonStatus(paseoHome: string): Promise<DaemonStatus> {
const result =
await $`PASEO_HOME=${paseoHome} PASEO_LOCAL_SPEECH_AUTO_DOWNLOAD=${testEnv.PASEO_LOCAL_SPEECH_AUTO_DOWNLOAD} PASEO_DICTATION_ENABLED=${testEnv.PASEO_DICTATION_ENABLED} PASEO_VOICE_MODE_ENABLED=${testEnv.PASEO_VOICE_MODE_ENABLED} npx paseo daemon status --home ${paseoHome} --json`.nothrow();
if (result.exitCode !== 0) {
return { localDaemon: null, pid: null, workerPid: null };
}
try {
const parsed = JSON.parse(result.stdout) as {
localDaemon?: unknown;
pid?: unknown;
workerPid?: number;
};
const localDaemon = typeof parsed.localDaemon === "string" ? parsed.localDaemon : null;
const pid =
typeof parsed.pid === "number" && Number.isInteger(parsed.pid) && parsed.pid > 0
? parsed.pid
: null;
return { localDaemon, pid, workerPid: parsed.workerPid ?? null };
} catch {
return { localDaemon: null, pid: null, workerPid: null };
}
}
async function readCapturedSupervisorLogs(paseoHome: string, recentLogs: string): Promise<string> {
const durableLogs = await readFile(join(paseoHome, "daemon.log"), "utf8").catch(() => "");
return `${recentLogs}\n${durableLogs}`;
@@ -101,8 +75,30 @@ const host = `127.0.0.1:${port}`;
let supervisorProcess: ChildProcess | null = null;
let recentSupervisorLogs = "";
let client: DaemonClient | undefined;
const availabilityLog = join(paseoHome, "availability.log");
try {
if (process.platform !== "win32") {
const provider = join(paseoHome, "slow-provider");
await writeFile(
provider,
`#!${process.execPath}
import('node:fs').then(({appendFileSync}) => {
appendFileSync(${JSON.stringify(availabilityLog)}, JSON.stringify(process.argv.slice(2)) + "\\n");
setTimeout(() => console.log('provider 1.0.0'), 1800);
});
`,
{ mode: 0o700 },
);
await writeFile(
join(paseoHome, "config.json"),
JSON.stringify({
version: 1,
agents: { providers: { claude: { command: { mode: "replace", argv: [provider] } } } },
}),
);
}
console.log("Test 1: start supervisor-entrypoint in dev mode with isolated PASEO_HOME");
supervisorProcess = spawn(
@@ -111,7 +107,11 @@ try {
{
cwd: cliRoot,
env: {
...process.env,
...Object.fromEntries(
Object.entries(process.env).filter(([key]) => !key.startsWith("PASEO_")),
),
HOME: paseoHome,
USERPROFILE: paseoHome,
...testEnv,
PASEO_HOME: paseoHome,
PASEO_LISTEN: host,
@@ -129,80 +129,90 @@ try {
recentSupervisorLogs = (recentSupervisorLogs + chunk.toString()).slice(-8000);
});
let supervisor = await readDaemonInstance(paseoHome);
await waitFor(
async () => {
const status = await readDaemonStatus(paseoHome);
return (
status.localDaemon === "running" && status.pid !== null && isProcessRunning(status.pid)
);
supervisor = await readDaemonInstance(paseoHome);
return supervisor?.pid === supervisorProcess?.pid && Boolean(supervisor?.listen);
},
120000,
"daemon did not become running in time",
"daemon did not publish its bound endpoint in time",
);
const statusBeforeRestart = await readDaemonStatus(paseoHome);
const supervisorPid = statusBeforeRestart.pid;
assert.strictEqual(
statusBeforeRestart.localDaemon,
"running",
"daemon should be running before restart",
);
assert(supervisorPid !== null, "supervisor pid should exist once daemon starts");
assert(supervisor?.listen, "owned supervisor should publish its bound endpoint");
const supervisorPid = supervisor.pid;
assert.strictEqual(supervisorPid, supervisorProcess.pid, "PID lock should identify this launch");
assert(isProcessRunning(supervisorPid), "supervisor process should be running");
const workerPidBeforeRestart = statusBeforeRestart.workerPid;
assert(workerPidBeforeRestart !== null, "supervisor should have a worker process before restart");
client = new DaemonClient({
url: `ws://${supervisor.listen}/ws`,
clientId: "supervisor-restart-test",
connectTimeoutMs: 5000,
webSocketFactory: (url) => new WebSocket(url) as unknown as WebSocketLike,
});
await client.connect();
// The app reads worker identity through the full RPC, not the bounded CLI observation.
const statusBeforeRestart = await client.getDaemonStatus();
const workerPidBeforeRestart = statusBeforeRestart.pid;
assert(
isProcessRunning(workerPidBeforeRestart),
"worker process should be running before restart",
);
if (process.platform !== "win32") {
assert.match(
await readFile(availabilityLog, "utf8"),
/\["--version"\]/,
"the configured slow provider was actually probed",
);
}
console.log(
`✓ daemon running with supervisor ${supervisorPid} and worker ${workerPidBeforeRestart}\n`,
);
console.log("Test 2: app-style restart request should restart worker and keep daemon healthy");
const client = await tryConnectToDaemon({ target: { kind: "endpoint", host }, timeout: 5000 });
assert(client, "daemon client should connect");
try {
const restartAck = await client.restartServer("settings_update");
assert.strictEqual(
restartAck.status,
"restart_requested",
"restart request should be acknowledged",
);
} finally {
await client?.close().catch(() => undefined);
}
const restartAck = await client.restartServer("settings_update");
assert.strictEqual(
restartAck.status,
"restart_requested",
"restart request should be acknowledged",
);
const deadline = Date.now() + 20000;
let statusAfterRestart = statusBeforeRestart;
await waitFor(
async () => {
const workerPid = (await readDaemonStatus(paseoHome)).workerPid;
return (
workerPid !== null && workerPid !== workerPidBeforeRestart && isProcessRunning(workerPid)
);
try {
const observed = await client!.getDaemonStatus({
timeout: Math.max(1, deadline - Date.now()),
});
if (observed.pid === workerPidBeforeRestart || !isProcessRunning(observed.pid))
return false;
statusAfterRestart = observed;
return true;
} catch (error) {
if (error instanceof DaemonConnectionError) return false;
throw error;
}
},
20000,
"worker pid did not change after restart request",
);
const workerPidAfterRestart = (await readDaemonStatus(paseoHome)).workerPid;
assert(workerPidAfterRestart !== null, "worker process should exist after restart");
assert.notStrictEqual(
workerPidAfterRestart,
statusAfterRestart.pid,
workerPidBeforeRestart,
"worker pid should change after restart",
);
const statusAfterRestart = await readDaemonStatus(paseoHome);
assert(isProcessRunning(statusAfterRestart.pid), "replacement worker should remain running");
const current = await readDaemonInstance(paseoHome);
assert(current?.listen, "daemon should remain bound after restart");
assert.strictEqual(
statusAfterRestart.localDaemon,
"running",
"daemon should stay running after restart",
);
assert.strictEqual(
statusAfterRestart.pid,
current.pid,
supervisorPid,
"supervisor pid should remain stable across restart",
);
assert(
isSameDaemonInstance(supervisor, current),
"supervisor start time should remain stable across restart",
);
const capturedSupervisorLogs = await readCapturedSupervisorLogs(paseoHome, recentSupervisorLogs);
assert(
capturedSupervisorLogs.includes('"msg":"Worker requested restart"') &&
@@ -219,6 +229,7 @@ try {
);
console.log("✓ app-style restart keeps daemon healthy and restarts worker\n");
} finally {
await client?.close();
if (supervisorProcess?.pid && isProcessRunning(supervisorProcess.pid)) {
supervisorProcess.kill("SIGTERM");
await waitFor(
@@ -230,7 +241,7 @@ try {
});
}
await $`PASEO_HOME=${paseoHome} PASEO_LOCAL_SPEECH_AUTO_DOWNLOAD=${testEnv.PASEO_LOCAL_SPEECH_AUTO_DOWNLOAD} PASEO_DICTATION_ENABLED=${testEnv.PASEO_DICTATION_ENABLED} PASEO_VOICE_MODE_ENABLED=${testEnv.PASEO_VOICE_MODE_ENABLED} npx paseo daemon stop --home ${paseoHome} --force`.nothrow();
await runLocalPaseo(["daemon", "stop", "--home", paseoHome, "--force"]);
await rm(paseoHome, { recursive: true, force: true });
}