Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
test(mcp-host): drive the real entrypoint for the persist-failure exit
Alfredo Lopez71cbb5b9bbac
diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.jsonindex d8597a464..9f70ddbb5 100644--- a/mcp-host/package-lock.json+++ b/mcp-host/package-lock.json@@ -1,12 +1,12 @@{"name": "clerum-mcp-host",- "version": "0.1.579",+ "version": "0.1.580","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "clerum-mcp-host",- "version": "0.1.579",+ "version": "0.1.580","license": "SEE LICENSE IN LICENSE","dependencies": {"@anthropic-ai/sdk": "^0.32.0",diff --git a/mcp-host/package.json b/mcp-host/package.jsonindex 9ec275eab..bb06940fb 100644--- a/mcp-host/package.json+++ b/mcp-host/package.json@@ -1,6 +1,6 @@{"name": "clerum-mcp-host",- "version": "0.1.579",+ "version": "0.1.580","description": "MCP Host - reads Host CRD and provides LLM access via OpenAI/Claude","main": "dist/main.js","engines": {diff --git a/mcp-host/src/__tests__/fixtures/dbWorkerControl.cjs b/mcp-host/src/__tests__/fixtures/dbWorkerControl.cjsnew file mode 100644index 000000000..07d7de3ac--- /dev/null+++ b/mcp-host/src/__tests__/fixtures/dbWorkerControl.cjs@@ -0,0 +1,41 @@+// Test-side preload for main.persistFailureExit.test.ts. Loaded with+// `node --require` before the compiled main.js, it replaces the Worker class+// on the worker_threads module so the conversation DB worker is still the+// real compiled dbWorker.js, and controls only how it receives one message:+//+// - DB_WORKER_CONTROL=withhold-shutdown: the `shutdown` op is never delivered,+// as if the worker were wedged in its close checkpoint.+// - DB_WORKER_CONTROL=terminate-on-sigusr2: SIGUSR2 terminates the DB worker,+// an unexpected worker exit while the Host is serving.+//+// Every other message reaches the real worker unchanged.+const workerThreads = require('node:worker_threads')++const RealWorker = workerThreads.Worker+const mode = process.env.DB_WORKER_CONTROL+if (mode !== 'withhold-shutdown' && mode !== 'terminate-on-sigusr2') {+ throw new Error(`dbWorkerControl: unknown DB_WORKER_CONTROL '${mode}'`)+}++class ControlledWorker extends RealWorker {+ constructor(filename, options) {+ super(filename, options)+ this.isDbWorker = String(filename).endsWith('/db/worker/dbWorker.js')+ if (this.isDbWorker && mode === 'terminate-on-sigusr2') {+ process.once('SIGUSR2', () => {+ process.stdout.write('PRELOAD_TERMINATING_DB_WORKER\n')+ void this.terminate()+ })+ }+ }++ postMessage(msg, transfer) {+ if (this.isDbWorker && mode === 'withhold-shutdown' && msg?.op?.kind === 'shutdown') {+ process.stdout.write('PRELOAD_WITHHELD_SHUTDOWN\n')+ return+ }+ return super.postMessage(msg, transfer)+ }+}++workerThreads.Worker = ControlledWorkerdiff --git a/mcp-host/src/__tests__/main.persistFailureExit.test.ts b/mcp-host/src/__tests__/main.persistFailureExit.test.tsnew file mode 100644index 000000000..cc500ce71--- /dev/null+++ b/mcp-host/src/__tests__/main.persistFailureExit.test.ts@@ -0,0 +1,117 @@+import { afterAll, beforeAll, describe, expect, it } from 'vitest'+import { spawn } from 'node:child_process'+import fs from 'node:fs'+import os from 'node:os'+import path from 'node:path'+import { MCP_HOST_ROOT, compileSources } from '../db/worker/__tests__/compileSources'++// Drives the real compiled main.js (dev mode, SQLite store, real DB worker+// thread) and reads its exit code. The only test-side change is a preload+// that withholds the worker's `shutdown` message or terminates the worker on+// SIGUSR2; main.ts's shutdown flag, persist-failure handler and final+// process.exit run as in production.++const PRELOAD = path.join(__dirname, 'fixtures', 'dbWorkerControl.cjs')+const STALL_TIMEOUT_MS = 1000+const CHILD_DEADLINE_MS = 15_000++let tmpRoot: string+let mainScript: string++interface ChildRun {+ code: number | null+ signal: NodeJS.Signals | null+ output: string+}++function runHost(+ control: 'withhold-shutdown' | 'terminate-on-sigusr2',+ signal: 'SIGTERM' | 'SIGUSR2'+): Promise<ChildRun> {+ const home = fs.mkdtempSync(path.join(tmpRoot, 'host-'))+ const child = spawn(process.execPath, ['--require', PRELOAD, mainScript], {+ cwd: home,+ env: {+ PATH: process.env.PATH,+ HOME: home,+ NODE_ENV: 'test',+ CLERUM_DEV_MODE: 'true',+ CLERUM_ENABLE_AUTH: 'false',+ OPENAI_API_KEY: 'sk-test-not-used',+ CLERUM_SESSION_STORE: 'sqlite',+ CLERUM_SESSION_DB_PATH: path.join(home, 'state.db'),+ CLERUM_SERVER_PORT: '0',+ CLERUM_MEMORY_WORKSPACE_PATH: path.join(home, 'workspace'),+ CLERUM_DB_PERSIST_SYNC_TIMEOUT_MS: '500',+ CLERUM_DB_WORKER_STALL_TIMEOUT_MS: String(STALL_TIMEOUT_MS),+ DB_WORKER_CONTROL: control,+ },+ })+ return new Promise(resolve => {+ let output = ''+ let signalled = false+ const onData = (chunk: Buffer) => {+ output += chunk.toString()+ if (+ !signalled &&+ output.includes('[ConversationStore] opened') &&+ output.includes('MCP Host running in dev mode')+ ) {+ signalled = true+ child.kill(signal)+ }+ }+ child.stdout.on('data', onData)+ child.stderr.on('data', onData)+ const deadline = setTimeout(() => {+ output += `\nTEST_DEADLINE_KILLED after ${CHILD_DEADLINE_MS}ms (signalled=${signalled})\n`+ child.kill('SIGKILL')+ }, CHILD_DEADLINE_MS)+ child.on('exit', (code, exitSignal) => {+ clearTimeout(deadline)+ resolve({ code, signal: exitSignal, output })+ })+ })+}++beforeAll(() => {+ tmpRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'main-persist-exit-'))+ fs.symlinkSync(path.join(MCP_HOST_ROOT, 'node_modules'), path.join(tmpRoot, 'node_modules'))+ ;[mainScript] = compileSources(['main.ts', 'db/worker/dbWorker.ts'], path.join(tmpRoot, 'dist'))+}, 120_000)++afterAll(() => {+ if (tmpRoot) fs.rmSync(tmpRoot, { recursive: true, force: true })+})++describe('main.ts persist-failure exit wiring (real entrypoint)', () => {+ it('exits 1 when the store cannot close during a graceful shutdown', async () => {+ const run = await runHost('withhold-shutdown', 'SIGTERM')++ // Witnesses: the real store opened, the preload engaged, and the+ // progress bound fired inside the shutdown window.+ expect(run.output, run.output).toContain('[ConversationStore] opened')+ expect(run.output, run.output).toContain('PRELOAD_WITHHELD_SHUTDOWN')+ expect(run.output, run.output).toContain('db worker made no progress')+ expect(run.output, run.output).toContain(+ 'ConversationStore shutdown failed; exiting with code 1'+ )+ // The in-service handler stays silent while shutdown owns the exit.+ expect(run.output, run.output).not.toContain('Conversation store can no longer persist writes')+ expect({ code: run.code, signal: run.signal }).toEqual({ code: 1, signal: null })+ }, 30_000)++ it('exits 1 when the DB worker exits while the Host is serving', async () => {+ const run = await runHost('terminate-on-sigusr2', 'SIGUSR2')++ expect(run.output, run.output).toContain('[ConversationStore] opened')+ expect(run.output, run.output).toContain('PRELOAD_TERMINATING_DB_WORKER')+ expect(run.output, run.output).toContain(+ '[ConversationStore] worker exited; writes are rejected'+ )+ expect(run.output, run.output).toContain(+ '[Main] Conversation store can no longer persist writes; exiting with code 1'+ )+ expect({ code: run.code, signal: run.signal }).toEqual({ code: 1, signal: null })+ }, 30_000)+})