Files
paseo/packages/cli/tests/24-daemon-stop-ownership.test.ts
Mohamed Boudra 8d0a9f9224 fix(server): fix daemon startup race and reduce log bloat
The desktop app could time out connecting to the daemon on first launch
because the PID file advertised a listen address before the HTTP server
was actually listening. The supervisor now writes the PID lock with
listen: null and updates it only after the worker sends a paseo:ready
IPC message confirming the server is listening.

- Rename daemon-runner.ts to supervisor-entrypoint.ts
- PID lock acquired with listen: null, updated via paseo:ready IPC
- Worker sends paseo:ready after httpServer.listen() resolves
- Desktop polls until listen is non-null before returning to the app
- Remove PASEO_PID_LOCK_MODE and external lock mode
- Remove unnecessary env overrides (PASEO_HOME, PASEO_CORS_ORIGINS) from
  desktop daemon spawn
- Reduce trace log bloat: inbound/outbound WebSocket messages now log
  only message type and payload size instead of full payloads
- Supervisor restarts worker on SIGKILL (covers OOM)
2026-03-30 12:37:07 +07:00

120 lines
3.6 KiB
TypeScript

#!/usr/bin/env npx tsx
/**
* Regression: `paseo daemon stop` must only act on daemon ownership state and
* must not discover/kill processes via home-scoped `ps` command heuristics.
*/
import assert from "node:assert";
import { spawn, type ChildProcess } from "node:child_process";
import { mkdtemp, rm } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { $ } from "zx";
$.verbose = false;
const testEnv = {
PASEO_LOCAL_SPEECH_AUTO_DOWNLOAD: process.env.PASEO_LOCAL_SPEECH_AUTO_DOWNLOAD ?? "0",
PASEO_DICTATION_ENABLED: process.env.PASEO_DICTATION_ENABLED ?? "0",
PASEO_VOICE_MODE_ENABLED: process.env.PASEO_VOICE_MODE_ENABLED ?? "0",
};
function sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}
function isProcessRunning(pid: number): boolean {
if (!Number.isInteger(pid) || pid <= 0) {
return false;
}
try {
process.kill(pid, 0);
return true;
} catch {
return false;
}
}
async function waitForRunning(pid: number, timeoutMs: number): Promise<void> {
const deadline = Date.now() + timeoutMs;
while (Date.now() < deadline) {
if (isProcessRunning(pid)) {
return;
}
await sleep(50);
}
throw new Error(`Process ${pid} did not become running in time`);
}
console.log("=== Daemon Stop Ownership Regression ===\n");
const paseoHome = await mkdtemp(join(tmpdir(), "paseo-stop-ownership-"));
let decoyProcess: ChildProcess | null = null;
try {
console.log("Test 1: start decoy process with daemon-like command markers");
decoyProcess = spawn(
process.execPath,
[
"-e",
// Keep the process alive long enough for stop command assertions.
"setInterval(() => {}, 1000)",
"supervisor-entrypoint.ts",
],
{
env: {
...process.env,
PASEO_HOME: paseoHome,
},
stdio: "ignore",
detached: process.platform !== "win32",
},
);
decoyProcess.unref();
const decoyPid = decoyProcess.pid;
assert(Number.isInteger(decoyPid) && (decoyPid ?? 0) > 0, "decoy pid should exist");
await waitForRunning(decoyPid!, 5000);
console.log(`✓ decoy process started (${decoyPid})\n`);
console.log("Test 2: daemon stop should report not_running and leave decoy untouched");
const stopResult =
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} --json`.nothrow();
assert.strictEqual(stopResult.exitCode, 0, `stop should succeed: ${stopResult.stderr}`);
const parsed = JSON.parse(stopResult.stdout) as { action?: unknown };
assert.strictEqual(
parsed.action,
"not_running",
`stop should not target decoy process: ${stopResult.stdout}`,
);
assert(isProcessRunning(decoyPid!), "decoy process must remain alive after stop");
console.log("✓ stop is ownership-driven and does not kill decoy process\n");
} finally {
if (decoyProcess?.pid && isProcessRunning(decoyProcess.pid)) {
try {
process.kill(decoyProcess.pid, "SIGTERM");
} catch {
// ignore
}
await sleep(100);
if (isProcessRunning(decoyProcess.pid)) {
try {
process.kill(decoyProcess.pid, "SIGKILL");
} catch {
// ignore
}
}
}
await $`PASEO_HOME=${paseoHome} npx paseo daemon stop --home ${paseoHome} --force`.nothrow();
await rm(paseoHome, { recursive: true, force: true });
}
console.log("=== Daemon stop ownership regression test passed ===");