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-968]): custom PM2_HOME jlist parse failure must yield null, not [] (review: codex)
Adam Eivya06d68992953
diff --git a/server/services/pm2.js b/server/services/pm2.jsindex ad52327565..aac599e491 100644--- a/server/services/pm2.js+++ b/server/services/pm2.js@@ -279,6 +279,28 @@ function shapeProcStatus(proc) {};}+/**+ * Parse the stdout of a `pm2 jlist` (custom PM2_HOME CLI path) into a raw+ * process array, or `null` when the read effectively failed.+ *+ * PM2 jlist always emits a JSON array (`[]` when there are no processes), so an+ * exit-0 with empty or garbage stdout (no array literal at all) is a FAILED read+ * — not a successful "no processes." Returning `[]` there would reintroduce the+ * absent-vs-empty footgun (issue #968) for custom PM2_HOME reads. `extractJSONArray`+ * itself falls back to `'[]'` on garbage, so we must detect a real array literal+ * before trusting it (mirroring extractJSONArray's own detection) and return null+ * otherwise — matching the default-home path, which resolves null for a non-array.+ *+ * @param {string} stdout Raw `pm2 jlist` stdout (may carry ANSI noise).+ * @returns {Array|null} The parsed process array (incl. `[]`), or `null` on failure.+ */+export function parseJlistStdout(stdout) {+ const hasArrayLiteral = typeof stdout === 'string' && (stdout.includes('[{') || /\[\](?![0-9])/.test(stdout));+ if (!hasArrayLiteral) return null;+ const list = safeJSONParse(extractJSONArray(stdout), null);+ return Array.isArray(list) ? list : null;+}+/*** Fetch PM2 process list with TTL caching.* Uses the PM2 Node.js API for the default PM2_HOME (no subprocess spawning — avoids@@ -335,7 +357,11 @@ function fetchJlist(pm2Home = null) {resolve(null);return;}- const list = safeJSONParse(extractJSONArray(stdout), []);+ const list = parseJlistStdout(stdout);+ if (list === null) {+ resolve(null);+ return;+ }jlistCache.set(key, { data: list, ts: Date.now() });resolve(list);});diff --git a/server/services/pm2.parseJlist.test.js b/server/services/pm2.parseJlist.test.jsnew file mode 100644index 0000000000..a4ffe5b687--- /dev/null+++ b/server/services/pm2.parseJlist.test.js@@ -0,0 +1,44 @@+import { describe, it, expect, vi } from 'vitest';++// pm2.js imports the `pm2` package at module load (no daemon connection until a+// call is made), so importing it for a pure-helper test is safe. We still mock+// the package to keep the import side-effect-free in CI.+vi.mock('pm2', () => ({ default: { connect: vi.fn(), list: vi.fn(), disconnect: vi.fn() } }));++import { parseJlistStdout } from './pm2.js';++describe('parseJlistStdout (issue #968 — custom PM2_HOME absent-vs-empty)', () => {+ it('parses a populated jlist array', () => {+ const out = '[{"name":"svc-a","pm2_env":{"status":"online"}}]';+ const list = parseJlistStdout(out);+ expect(Array.isArray(list)).toBe(true);+ expect(list).toHaveLength(1);+ expect(list[0].name).toBe('svc-a');+ });++ it('parses a genuine empty array as [] (read OK, no processes)', () => {+ // The literal '[]' is a successful "nothing running" — NOT a failure.+ expect(parseJlistStdout('[]')).toEqual([]);+ // Tolerates ANSI/log noise around the array literal.+ expect(parseJlistStdout('[2K[]\n')).toEqual([]);+ });++ it('returns null for empty stdout (failed read, not "no processes")', () => {+ // exit-0 with no output is a failed read — must NOT collapse to [].+ expect(parseJlistStdout('')).toBeNull();+ expect(parseJlistStdout(' \n')).toBeNull();+ });++ it('returns null for garbage / non-array stdout', () => {+ expect(parseJlistStdout('pm2 daemon not running')).toBeNull();+ expect(parseJlistStdout('{"not":"an array"}')).toBeNull();+ expect(parseJlistStdout(undefined)).toBeNull();+ expect(parseJlistStdout(null)).toBeNull();+ });++ it('does not mistake an ANSI color code like [31m for an empty array', () => {+ // The /\[\](?![0-9])/ guard means '[]' inside '[31m' style noise without a+ // real array still resolves to null, not a bogus empty success.+ expect(parseJlistStdout('[31mERROR[0m no pm2')).toBeNull();+ });+});