Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix([issue-749]): use tasklist for Windows process-liveness check

[._.]/ Adam Eivy24f6c6dc4369
diff --git a/server/cos-runner/processStats.js b/server/cos-runner/processStats.js
index 5b290ba8df..3cc2c4ea03 100644
--- a/server/cos-runner/processStats.js
+++ b/server/cos-runner/processStats.js
@@ -52,6 +52,8 @@ export async function getProcessStats(pid) {
/**
* Check if a process is running by PID.
+ * Uses `tasklist` on Windows and `ps` elsewhere so orphan cleanup doesn't
+ * report live agents as dead on Windows installs.
*/
export async function checkProcessRunning(pid) {
// Security: Ensure PID is a valid integer to prevent command injection
@@ -60,6 +62,13 @@ export async function checkProcessRunning(pid) {
return false;
}
+ if (process.platform === 'win32') {
+ const result = await execAsync(`tasklist /FI "PID eq ${safePid}" /FO CSV /NH`, { windowsHide: true }).catch(() => ({ stdout: '' }));
+ // tasklist prints a CSV row containing the PID when the process exists;
+ // otherwise it emits an "INFO: No tasks…" line that won't contain the PID.
+ return result.stdout.includes(`"${safePid}"`);
+ }
+
const result = await execAsync(`ps -p ${safePid} -o pid=`, { windowsHide: true }).catch(() => ({ stdout: '' }));
return result.stdout.trim() !== '';
}
diff --git a/server/cos-runner/processStats.test.js b/server/cos-runner/processStats.test.js
index 7466d0dfb9..c428def2ec 100644
--- a/server/cos-runner/processStats.test.js
+++ b/server/cos-runner/processStats.test.js
@@ -1,4 +1,4 @@
-import { describe, it, expect } from 'vitest';
+import { describe, it, expect, vi, afterEach } from 'vitest';
import { getProcessStats, checkProcessRunning } from './processStats.js';
describe('cos-runner processStats', () => {
@@ -33,4 +33,40 @@ describe('cos-runner processStats', () => {
expect(await checkProcessRunning(process.pid)).toBe(true);
});
});
+
+ describe('checkProcessRunning Windows branch', () => {
+ afterEach(() => {
+ vi.unstubAllGlobals();
+ vi.resetModules();
+ });
+
+ // execAsync = promisify(exec), so the mocked exec must honor the callback
+ // convention: exec(cmd, opts, cb) → cb(err, { stdout, stderr }).
+ const loadWithMockedExec = async (stdout) => {
+ const calls = [];
+ vi.resetModules();
+ vi.doMock('child_process', () => ({
+ exec: (cmd, _opts, cb) => { calls.push(cmd); cb(null, { stdout, stderr: '' }); },
+ }));
+ vi.stubGlobal('process', { ...process, platform: 'win32' });
+ const mod = await import('./processStats.js');
+ return { checkProcessRunning: mod.checkProcessRunning, calls };
+ };
+
+ it('uses tasklist and treats a matching PID row as running', async () => {
+ const { checkProcessRunning: check, calls } = await loadWithMockedExec(
+ '"node.exe","1234","Console","1","50,000 K"\r\n'
+ );
+ expect(await check(1234)).toBe(true);
+ expect(calls[0]).toContain('tasklist');
+ expect(calls[0]).toContain('PID eq 1234');
+ });
+
+ it('treats an "INFO: No tasks" response as not running', async () => {
+ const { checkProcessRunning: check } = await loadWithMockedExec(
+ 'INFO: No tasks are running which match the specified criteria.\r\n'
+ );
+ expect(await check(1234)).toBe(false);
+ });
+ });
});