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: bound PM2 jlist parsing to the first complete array (#8170)

[._.]/ Adam Eivy320c00304929
diff --git a/server/lib/pm2Jlist.js b/server/lib/pm2Jlist.js
index bad10866eb..6f49196963 100644
--- a/server/lib/pm2Jlist.js
+++ b/server/lib/pm2Jlist.js
@@ -26,10 +26,39 @@
*/
function extractJsonArrayText(stdout) {
const objectStart = stdout.indexOf('[{');
- if (objectStart >= 0) return stdout.slice(objectStart);
const emptyMatch = stdout.match(/\[\](?![0-9])/);
- if (emptyMatch) return stdout.slice(stdout.indexOf(emptyMatch[0]));
- return '[]';
+ const start = objectStart >= 0
+ ? objectStart
+ : emptyMatch
+ ? emptyMatch.index
+ : -1;
+ if (start < 0) return '[]';
+
+ let depth = 0;
+ let inString = false;
+ let escaped = false;
+ for (let index = start; index < stdout.length; index += 1) {
+ const char = stdout[index];
+ if (inString) {
+ if (escaped) {
+ escaped = false;
+ } else if (char === '\\') {
+ escaped = true;
+ } else if (char === '"') {
+ inString = false;
+ }
+ continue;
+ }
+ if (char === '"') {
+ inString = true;
+ } else if (char === '[') {
+ depth += 1;
+ } else if (char === ']') {
+ depth -= 1;
+ if (depth === 0) return stdout.slice(start, index + 1);
+ }
+ }
+ return stdout.slice(start);
}
/**
diff --git a/server/services/pm2.parseJlist.test.js b/server/services/pm2.parseJlist.test.js
index a4ffe5b687..6f5f236363 100644
--- a/server/services/pm2.parseJlist.test.js
+++ b/server/services/pm2.parseJlist.test.js
@@ -16,6 +16,14 @@ describe('parseJlistStdout (issue #968 — custom PM2_HOME absent-vs-empty)', ()
expect(list[0].name).toBe('svc-a');
});
+ it('parses the first complete array when trailing output follows it', () => {
+ const entry = { name: 'svc-a', pm2_env: { status: 'online', args: ['literal ] and [', 'other'] } };
+ expect(parseJlistStdout(`${JSON.stringify([entry])}\n[2K`)).toEqual([entry]);
+ expect(parseJlistStdout(`${JSON.stringify([entry])}\n${JSON.stringify([{ name: 'svc-b' }])}`)).toEqual([entry]);
+ expect(parseJlistStdout('[]\n[2K')).toEqual([]);
+ expect(parseJlistStdout('[{"name":"svc-a"}\n[2K')).toBeNull();
+ });
+
it('parses a genuine empty array as [] (read OK, no processes)', () => {
// The literal '[]' is a successful "nothing running" — NOT a failure.
expect(parseJlistStdout('[]')).toEqual([]);