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

address review (claude): isolate test log state

[._.]/ Adam Eivyd8d7abcf8a83
diff --git a/server/services/mtplxServerManager.js b/server/services/mtplxServerManager.js
index 5ac62bf0b4..42a6b630ba 100644
--- a/server/services/mtplxServerManager.js
+++ b/server/services/mtplxServerManager.js
@@ -125,14 +125,16 @@ const MTPLX_RUNTIME_BOOTSTRAP_ERROR = 'MTPLX\'s own Python runtime failed to boo
* launch. PM2 appends to its configured files across process deletion, so
* stable, app-specific paths are truncated before every start.
*/
-const MTPLX_LOG_FILES = {
+const DEFAULT_MTPLX_LOG_FILES = {
stdout: join(tmpdir(), 'portos-mtplx-out.log'),
stderr: join(tmpdir(), 'portos-mtplx-error.log'),
};
+let mtplxLogFiles = DEFAULT_MTPLX_LOG_FILES;
+
const resetMtplxLogs = async () => {
const results = await Promise.all(
- Object.entries(MTPLX_LOG_FILES).map(([stream, path]) => (
+ Object.entries(mtplxLogFiles).map(([stream, path]) => (
writeFile(path, '')
.then(() => true)
.catch((error) => {
@@ -491,8 +493,8 @@ export async function startMtplxServer(options = {}) {
'--name', MTPLX_APP,
'--interpreter', 'none',
'--no-autorestart',
- '--output', MTPLX_LOG_FILES.stdout,
- '--error', MTPLX_LOG_FILES.stderr,
+ '--output', mtplxLogFiles.stdout,
+ '--error', mtplxLogFiles.stderr,
'--',
...args,
]);
@@ -515,10 +517,10 @@ export async function startMtplxServer(options = {}) {
const lines = `${pm2Logs?.stderr || pm2Logs?.stdout || ''}`.split('\n').map((l) => l.trimEnd()).filter(Boolean);
const currentLines = logsReset ? lines : [];
for (const line of currentLines) appendLog(line);
- const tail = (currentLines.length ? currentLines : (logsReset ? daemon.snapshotLogs() : [])).slice(-4).join(' | ');
+ const tail = (currentLines.length ? currentLines : daemon.snapshotLogs()).slice(-4).join(' | ');
lastExitError = `PM2 status: ${currentProc.status}`;
const message = logsReset && isMtplxRuntimeBootstrapFailure(currentLines.join('\n'))
- ? MTPLX_RUNTIME_BOOTSTRAP_ERROR
+ ? `${MTPLX_RUNTIME_BOOTSTRAP_ERROR}${tail ? ` Last output: ${tail}` : ''}`
: `MTPLX exited immediately (${lastExitError}).${tail ? ` Last output: ${tail}` : ''}`;
await execPm2(['delete', MTPLX_APP]).catch(() => {});
@@ -752,8 +754,13 @@ export function _resetMtplxServerStateForTests({
relaunchReadyTimeout,
relaunchPoll,
idleMinutes = 0,
+ logFiles,
} = {}) {
idleMinutesOverride = idleMinutes;
+ mtplxLogFiles = logFiles ? {
+ stdout: logFiles.stdout || DEFAULT_MTPLX_LOG_FILES.stdout,
+ stderr: logFiles.stderr || DEFAULT_MTPLX_LOG_FILES.stderr,
+ } : DEFAULT_MTPLX_LOG_FILES;
currentConfig = null;
daemon.resetLogs();
lastExitError = null;
diff --git a/server/services/mtplxServerManager.test.js b/server/services/mtplxServerManager.test.js
index 7ab6552cba..f99a82e617 100644
--- a/server/services/mtplxServerManager.test.js
+++ b/server/services/mtplxServerManager.test.js
@@ -1,4 +1,6 @@
-import { readFile, writeFile } from 'fs/promises';
+import { mkdtemp, readFile, rm, writeFile } from 'fs/promises';
+import { tmpdir } from 'os';
+import { join } from 'path';
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
import {
getMtplxServerStatus,
@@ -36,9 +38,16 @@ const resetForTest = (overrides = {}) => {
describe('mtplxServerManager', () => {
let pm2State = null;
let execPm2Calls = [];
+ let testLogDir = null;
- beforeEach(() => {
- resetForTest();
+ beforeEach(async () => {
+ testLogDir = await mkdtemp(join(tmpdir(), 'portos-mtplx-test-'));
+ resetForTest({
+ logFiles: {
+ stdout: join(testLogDir, 'portos-mtplx-out.log'),
+ stderr: join(testLogDir, 'portos-mtplx-error.log'),
+ },
+ });
vi.restoreAllMocks();
pm2State = null;
execPm2Calls = [];
@@ -75,9 +84,11 @@ describe('mtplxServerManager', () => {
vi.spyOn(pm2Module, 'clearJlistCache').mockImplementation(() => {});
});
- afterEach(() => {
+ afterEach(async () => {
_resetMtplxServerStateForTests();
vi.restoreAllMocks();
+ await rm(testLogDir, { recursive: true, force: true });
+ testLogDir = null;
});
describe('getMtplxServerStatus', () => {
@@ -251,7 +262,7 @@ describe('mtplxServerManager', () => {
const start = execPm2Calls.find((args) => args[0] === 'start');
expect(start[start.indexOf('--output') + 1]).toMatch(/portos-mtplx-out\.log$/);
expect(start[start.indexOf('--error') + 1]).toMatch(/portos-mtplx-error\.log$/);
- expect(err.message).not.toContain('returned non-zero exit status 1');
+ expect(err.message).toContain('returned non-zero exit status 1');
expect(pm2State).toBeNull();
});
@@ -297,6 +308,31 @@ describe('mtplxServerManager', () => {
expect(pm2State).toBeNull();
});
+ it('keeps the in-memory launch context when log reset is unavailable', async () => {
+ resetForTest({
+ logFiles: {
+ stdout: join(testLogDir, 'missing', 'portos-mtplx-out.log'),
+ stderr: join(testLogDir, 'missing', 'portos-mtplx-error.log'),
+ },
+ });
+ vi.spyOn(console, 'error').mockImplementation(() => {});
+ vi.spyOn(pm2Module, 'execPm2').mockImplementation(async (args) => {
+ execPm2Calls.push(args);
+ if (args[0] === 'start') {
+ pm2State = { name: MTPLX_APP, status: 'errored', pid: null, args: [] };
+ }
+ if (args[0] === 'delete') pm2State = null;
+ return { stdout: '', stderr: '' };
+ });
+
+ const err = await startMtplxServer().catch((error) => error);
+
+ expect(err).toMatchObject({ code: 'MTPLX_EXITED' });
+ expect(err.message).toContain('Starting: mtplx serve');
+ expect(err.message).not.toMatch(/Homebrew/);
+ expect(pm2State).toBeNull();
+ });
+
it('reports the endpoint it actually bound, with no host it never passes', async () => {
// MTPLX is a loopback daemon and no `--host` ever reaches its launch line,
// so accepting a host would record an endpoint the server is not bound to.