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

autofixer: fail cleanly when the PM2 binary is missing

[._.]/ Adam Eivycebd7c24c406
diff --git a/autofixer/shared.js b/autofixer/shared.js
index 7cac628b46..d38aed4b3e 100644
--- a/autofixer/shared.js
+++ b/autofixer/shared.js
@@ -5,6 +5,7 @@
// server dependency graph into a package whose package.json declares only
// express.
import { spawn } from 'child_process';
+import { existsSync } from 'fs';
import { readFile } from 'fs/promises';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';
@@ -19,6 +20,15 @@ export const PM2_BIN = join(dirname(require.resolve('pm2/package.json')), 'bin',
/** Execute a PM2 CLI command via node (bypasses pm2.cmd) */
export function execPm2(pm2Args) {
return new Promise((resolve, reject) => {
+ // node_modules/pm2 can go stale (e.g. a dangling symlink left by a reaped
+ // worktree install) without require.resolve('pm2/package.json') noticing,
+ // since that only resolves the package dir, not this bin file. Catch it
+ // here with a one-line message instead of spawning node against a missing
+ // path, which surfaces as an opaque multi-line MODULE_NOT_FOUND dump.
+ if (!existsSync(PM2_BIN)) {
+ reject(new Error(`PM2 binary not found at ${PM2_BIN} — run "npm install" in the repo root`));
+ return;
+ }
const child = spawn(process.execPath, [PM2_BIN, ...pm2Args], { windowsHide: true });
let stdout = '';
let stderr = '';
diff --git a/autofixer/shared.test.js b/autofixer/shared.test.js
index a5191af274..31e16eaa3d 100644
--- a/autofixer/shared.test.js
+++ b/autofixer/shared.test.js
@@ -12,6 +12,14 @@ vi.mock('fs/promises', async (importOriginal) => ({
readFile: (...args) => readFileMock(...args),
}));
+// execPm2's missing-binary guard is tested by faking existsSync rather than
+// actually deleting PM2_BIN, which would break every other test in this file.
+const existsSyncMock = vi.hoisted(() => vi.fn());
+vi.mock('fs', async (importOriginal) => {
+ const actual = await importOriginal();
+ return { ...actual, existsSync: (...args) => existsSyncMock(...args) };
+});
+
// shared.js resolves the PM2 binary at import time, and Node walks node_modules
// upward from `autofixer/` — so it needs the ROOT install. CI installs only
// `server/node_modules` (`npm ci --prefix server`), which is never on that path,
@@ -29,6 +37,11 @@ const pm2Installed = (() => {
const describeShared = pm2Installed ? describe : describe.skip;
const shared = pm2Installed ? await import('./shared.js') : {};
+// Default existsSync to the real filesystem; individual tests override with
+// mockReturnValueOnce to simulate a missing PM2_BIN.
+const actualFs = await vi.importActual('fs');
+existsSyncMock.mockImplementation((...args) => actualFs.existsSync(...args));
+
const AUTOFIXER_SRC_DIR = dirname(fileURLToPath(import.meta.url));
describeShared('autofixer/shared — PM2 binary resolution', () => {
@@ -41,6 +54,19 @@ describeShared('autofixer/shared — PM2 binary resolution', () => {
});
});
+describeShared('autofixer/shared — execPm2', () => {
+ // A dangling symlink (e.g. left by a reaped worktree install) makes PM2_BIN
+ // not exist on disk without require.resolve('pm2/package.json') ever
+ // noticing, since that only resolves the package dir. Simulating that with
+ // existsSync proves execPm2 rejects with a clean one-line message instead
+ // of spawning `node <missing-file>` and surfacing an opaque multi-line
+ // MODULE_NOT_FOUND dump.
+ it('rejects with a one-line message when PM2_BIN is missing, without spawning', async () => {
+ existsSyncMock.mockReturnValueOnce(false);
+ await expect(shared.execPm2(['jlist'])).rejects.toThrow(`PM2 binary not found at ${shared.PM2_BIN} — run "npm install" in the repo root`);
+ });
+});
+
describeShared('autofixer/shared — data paths', () => {
// Both PM2 processes must agree on one data/ directory; resolving from this
// module's own location is what guarantees that. Spelled as a dirname climb