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: handle ANSI codes in pm2 jlist across all usages
Adam Eivy44c2eac7a704
diff --git a/server/lib/fileUtils.js b/server/lib/fileUtils.jsindex b7262f6c52..2cb46e3396 100644--- a/server/lib/fileUtils.js+++ b/server/lib/fileUtils.js@@ -117,9 +117,29 @@ export function isValidJSON(str, { allowArray = true } = {}) {return true;}+/**+ * Extract JSON array from string that may contain ANSI codes or other noise.+ * Useful for parsing pm2 jlist output which may include warnings before the JSON.+ *+ * @param {string} str - String potentially containing JSON array+ * @returns {string} Extracted JSON or '[]' if not found+ */+export function extractJSONArray(str) {+ if (!str) return '[]';+ // Look for '[{' (array with objects) first+ let jsonStart = str.indexOf('[{');+ if (jsonStart < 0) {+ // Check for empty array - find '[]' that's not part of ANSI codes like [31m+ const emptyMatch = str.match(/\[\](?![0-9])/);+ jsonStart = emptyMatch ? str.indexOf(emptyMatch[0]) : -1;+ }+ return jsonStart >= 0 ? str.slice(jsonStart) : '[]';+}+/*** Safely parse JSON with validation and fallback.* Avoids "Unexpected end of JSON input" errors from empty/corrupted files.+ * For arrays, automatically extracts JSON from strings with ANSI codes/noise (e.g., pm2 output).** @param {string} str - JSON string to parse* @param {*} defaultValue - Default value if parsing fails (default: null)@@ -136,6 +156,11 @@ export function isValidJSON(str, { allowArray = true } = {}) {* safeJSONParse(null, { default: true }) // { default: true }*/export function safeJSONParse(str, defaultValue = null, { allowArray = true, logError = false, context = '' } = {}) {+ // For arrays, try to extract JSON from noisy output (e.g., pm2 with ANSI codes)+ if (allowArray && Array.isArray(defaultValue) && str && !str.trim().startsWith('[')) {+ str = extractJSONArray(str);+ }+if (!isValidJSON(str, { allowArray })) {if (logError && str) {console.warn(`Invalid JSON${context ? ` in ${context}` : ''}: empty or malformed content`);diff --git a/server/services/pm2.js b/server/services/pm2.jsindex 576ecb5cdc..c27f68119e 100644--- a/server/services/pm2.js+++ b/server/services/pm2.js@@ -152,7 +152,15 @@ export async function listProcesses() {});child.on('close', () => {- const list = JSON.parse(stdout || '[]');+ // pm2 jlist may output ANSI codes and warnings before JSON+ // Look for '[{' (array with objects) or '[]' (empty array) to avoid matching ANSI codes like [31m+ let jsonStart = stdout.indexOf('[{');+ if (jsonStart < 0) {+ const emptyMatch = stdout.match(/\[\](?![0-9])/);+ jsonStart = emptyMatch ? stdout.indexOf(emptyMatch[0]) : -1;+ }+ const pm2Json = jsonStart >= 0 ? stdout.slice(jsonStart) : '[]';+ const list = JSON.parse(pm2Json);const processes = list.map(proc => ({name: proc.name,status: proc.pm2_env?.status || 'unknown',