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
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
fix([issue-796]): parse Windows tasklist CSV in getProcessStats
[._.]/ Adam Eivyc4e62b493d72
diff --git a/.changelog/NEXT.md b/.changelog/NEXT.mdindex 4a9d9fd1a9..3bf36ce82d 100644--- a/.changelog/NEXT.md+++ b/.changelog/NEXT.md@@ -70,5 +70,6 @@- **[issue-766] Episode Video renders cleanly when a remembered model is gone** — if the video model you previously pinned for an episode has since been removed, the render now quietly falls back to your default model instead of failing partway through.- **[issue-728] Imported stories no longer fake a "Ready" aesthetic or reader map** — when you start a Story Builder session from an import, only the steps the importer actually fills (idea, plot arc, characters, issues) open as "Ready" to review. The Universe Aesthetic and Reader Map steps now start "pending" instead of showing an empty step under a misleading "Ready" badge, so you generate them like you would in a from-scratch build.- **Self-update no longer crashes at the "Rebuilding esbuild" step** — the updater rebuilt esbuild via a hardcoded `node_modules/esbuild/install.js` path that assumed esbuild was hoisted to the top of each dependency tree. It no longer is (esbuild nests under Vite/Vitest, and the server dropped its esbuild dependency entirely with Vitest 4 → Vite 8), so the path 404'd and aborted the whole update with exit code 1. The updater now uses `npm rebuild esbuild`, which finds esbuild wherever it lives and skips cleanly when absent; the same fix lands in `npm run install:all` and the Windows updater.+- **[issue-796] Agent CPU/memory readouts work on Windows** — on Windows installs, the live CPU and memory stats for running agents always read as zero. The stats reader expected Unix `ps` output but was being handed Windows `tasklist`'s comma-quoted format, so it never found the numbers. It now reads the Windows format correctly, reporting real memory usage and marking a finished agent as done rather than leaving it in an unknown state.## Removeddiff --git a/server/cos-runner/processStats.js b/server/cos-runner/processStats.jsindex 3cc2c4ea03..3c8c5d954f 100644--- a/server/cos-runner/processStats.js+++ b/server/cos-runner/processStats.js@@ -12,6 +12,57 @@ import { promisify } from 'util';const execAsync = promisify(exec);+const toMemoryMb = (memoryKb) => Math.round(memoryKb / 1024 * 10) / 10;++/**+ * Parse a single `tasklist /FO CSV /NH` row into process stats.+ *+ * tasklist emits comma-delimited, double-quoted fields:+ * "node.exe","1234","Console","1","50,000 K"+ * image PID session # memUsage+ *+ * `/NH /FO CSV` does NOT report %CPU, so cpu is always 0 here (a different+ * source — WMIC/PowerShell — would be needed for CPU on Windows). The memory+ * column carries thousands separators and a " K" (KiB) unit, so strip every+ * non-digit before parsing. Returns null when the row isn't a parseable data+ * row (e.g. the "INFO: No tasks…" line).+ */+function parseTasklistRow(line, fallbackPid) {+ const fields = [...line.matchAll(/"([^"]*)"/g)].map((m) => m[1]);+ if (fields.length < 5) {+ return null;+ }+ const memoryKb = parseInt(fields[4].replace(/[^\d]/g, ''), 10) || 0;+ return {+ active: true,+ pid: parseInt(fields[1], 10) || fallbackPid,+ cpu: 0, // tasklist /NH does not report %CPU+ memoryKb,+ memoryMb: toMemoryMb(memoryKb),+ state: 'running' // tasklist row only appears for live PIDs; it carries no state column+ };+}++/**+ * Parse a single `ps -o pid=,pcpu=,rss=,state=` row into process stats.+ * Whitespace-delimited: `1234 0.5 50000 S`.+ */+function parsePsRow(line, fallbackPid) {+ const parts = line.split(/\s+/).filter(Boolean);+ if (parts.length < 3) {+ return null;+ }+ const memoryKb = parseInt(parts[2], 10) || 0;+ return {+ active: true,+ pid: parseInt(parts[0], 10) || fallbackPid,+ cpu: parseFloat(parts[1]) || 0,+ memoryKb,+ memoryMb: toMemoryMb(memoryKb),+ state: parts[3] || 'unknown'+ };+}+/*** Get process stats (CPU, memory) for a PID.* Returns { active, pid, cpu, memoryKb, memoryMb?, state }.@@ -23,9 +74,9 @@ export async function getProcessStats(pid) {return { active: false, pid, cpu: 0, memoryKb: 0, state: 'invalid' };}- // Get process stats using ps command+ const isWindows = process.platform === 'win32';// %cpu = CPU percentage, rss = resident set size in KB- const psCmd = process.platform === 'win32'+ const psCmd = isWindows? `tasklist /FI "PID eq ${safePid}" /FO CSV /NH`: `ps -p ${safePid} -o pid=,pcpu=,rss=,state=`;const result = await execAsync(psCmd, { windowsHide: true }).catch(() => ({ stdout: '' }));@@ -35,19 +86,19 @@ export async function getProcessStats(pid) {return { active: false, pid, cpu: 0, memoryKb: 0, state: 'dead' };}- const parts = line.split(/\s+/).filter(Boolean);- if (parts.length >= 3) {- return {- active: true,- pid: parseInt(parts[0], 10),- cpu: parseFloat(parts[1]) || 0,- memoryKb: parseInt(parts[2], 10) || 0,- memoryMb: Math.round((parseInt(parts[2], 10) || 0) / 1024 * 10) / 10,- state: parts[3] || 'unknown'- };+ const parseRow = isWindows ? parseTasklistRow : parsePsRow;+ const stats = parseRow(line, safePid);+ if (stats) {+ return stats;}- return { active: true, pid, cpu: 0, memoryKb: 0, state: 'unknown' };+ // No parseable data row. On Windows a non-empty-but-unparseable line is the+ // "INFO: No tasks are running…" message tasklist prints (with exit 0) when+ // the PID is gone, so report it dead — matching checkProcessRunning.+ if (isWindows) {+ return { active: false, pid: safePid, cpu: 0, memoryKb: 0, state: 'dead' };+ }+ return { active: true, pid: safePid, cpu: 0, memoryKb: 0, state: 'unknown' };}/**diff --git a/server/cos-runner/processStats.test.js b/server/cos-runner/processStats.test.jsindex c428def2ec..92f66eabeb 100644--- a/server/cos-runner/processStats.test.js+++ b/server/cos-runner/processStats.test.js@@ -34,6 +34,52 @@ describe('cos-runner processStats', () => {});});+ describe('getProcessStats Windows branch', () => {+ afterEach(() => {+ vi.unstubAllGlobals();+ vi.resetModules();+ });++ // execAsync = promisify(exec) → exec(cmd, opts, cb) with cb(err, { stdout }).+ 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 { getProcessStats: mod.getProcessStats, calls };+ };++ it('parses CPU/memory from the comma-quoted tasklist CSV row', async () => {+ const { getProcessStats: stats, calls } = await loadWithMockedExec(+ '"node.exe","1234","Console","1","50,000 K"\r\n'+ );+ const r = await stats(1234);+ expect(calls[0]).toContain('tasklist');+ expect(calls[0]).toContain('PID eq 1234');+ expect(r.active).toBe(true);+ expect(r.pid).toBe(1234);+ // "50,000 K" → 50000 KiB → ~48.8 MiB; the old whitespace split reported 0.+ expect(r.memoryKb).toBe(50000);+ expect(r.memoryMb).toBe(48.8);+ // tasklist /NH does not report %CPU.+ expect(r.cpu).toBe(0);+ expect(r.state).toBe('running');+ });++ it('reports a missing PID as dead from the "INFO: No tasks" line', async () => {+ const { getProcessStats: stats } = await loadWithMockedExec(+ 'INFO: No tasks are running which match the specified criteria.\r\n'+ );+ const r = await stats(1234);+ expect(r.active).toBe(false);+ expect(r.state).toBe('dead');+ expect(r.memoryKb).toBe(0);+ });+ });+describe('checkProcessRunning Windows branch', () => {afterEach(() => {vi.unstubAllGlobals();