Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

fix(mcp-host): log the store identity once the database opens, however long it takes

Alfredo Lopezcc674ff2cf6a
diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.json
index 7afc4621c..0eb4da2c9 100644
--- a/mcp-host/package-lock.json
+++ b/mcp-host/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.567",
+ "version": "0.1.568",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-mcp-host",
- "version": "0.1.567",
+ "version": "0.1.568",
"license": "SEE LICENSE IN LICENSE",
"dependencies": {
"@anthropic-ai/sdk": "^0.32.0",
diff --git a/mcp-host/package.json b/mcp-host/package.json
index 8856784a3..9f86d9703 100644
--- a/mcp-host/package.json
+++ b/mcp-host/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.567",
+ "version": "0.1.568",
"description": "MCP Host - reads Host CRD and provides LLM access via OpenAI/Claude",
"main": "dist/main.js",
"engines": {
diff --git a/mcp-host/src/core/conversation/persistence/__tests__/conversationStoreFactory.test.ts b/mcp-host/src/core/conversation/persistence/__tests__/conversationStoreFactory.test.ts
index 6d0e263be..df236a599 100644
--- a/mcp-host/src/core/conversation/persistence/__tests__/conversationStoreFactory.test.ts
+++ b/mcp-host/src/core/conversation/persistence/__tests__/conversationStoreFactory.test.ts
@@ -206,6 +206,55 @@ describe('createConversationStore store identity', () => {
}
})
+ // Opening can outlast a write budget (worker spawn, migrations, recovery of
+ // a large WAL), so the identity line must not depend on `syncTimeoutMs`.
+ it('logs the database path when opening takes longer than the sync write timeout', async () => {
+ const script = workerScript(`
+ const { parentPort } = require('node:worker_threads')
+ parentPort.on('message', msg => {
+ if (msg.op.kind === 'ping') {
+ setTimeout(() => parentPort.postMessage({ id: msg.id, ok: true, result: 'pong' }), 400)
+ }
+ if (msg.op.kind === 'shutdown') {
+ parentPort.postMessage({ id: msg.id, ok: true, result: { ok: true } })
+ process.exit(0)
+ }
+ })
+ `)
+ const dbPath = path.join(path.dirname(script), 'state.db')
+ const info = vi.spyOn(logger, 'info')
+ const errors = vi.spyOn(logger, 'error')
+ const opened = () => info.mock.calls.filter(([, msg]) => msg === '[ConversationStore] opened')
+ try {
+ const handle = createConversationStore({
+ mode: 'sqlite',
+ dbPath,
+ cacheSize: 10,
+ syncTimeoutMs: 100,
+ asyncTimeoutMs: 100,
+ checkpointEveryWrites: 100,
+ heartbeatMs: 60_000,
+ workerScriptPath: script,
+ })
+ await waitFor<void>(resolve => {
+ const poll = setInterval(() => {
+ if (opened().length > 0) {
+ clearInterval(poll)
+ resolve()
+ }
+ }, 5)
+ }, 3000)
+ expect(opened()).toEqual([[{ dbPath, mode: 'sqlite' }, '[ConversationStore] opened']])
+ expect(
+ errors.mock.calls.filter(([, msg]) => msg === '[ConversationStore] database did not open')
+ ).toEqual([])
+ await handle.shutdown()
+ } finally {
+ info.mockRestore()
+ errors.mockRestore()
+ }
+ })
+
it('logs no database path when the worker exits before opening the database', async () => {
const script = workerScript(`process.exit(1)`)
const info = vi.spyOn(logger, 'info')
diff --git a/mcp-host/src/core/conversation/persistence/__tests__/persistQueue.test.ts b/mcp-host/src/core/conversation/persistence/__tests__/persistQueue.test.ts
index e10b34a45..c0cf8dd4c 100644
--- a/mcp-host/src/core/conversation/persistence/__tests__/persistQueue.test.ts
+++ b/mcp-host/src/core/conversation/persistence/__tests__/persistQueue.test.ts
@@ -49,6 +49,34 @@ describe('PersistQueue', () => {
await expect(q.enqueueSync({ kind: 'ping' })).rejects.toThrow(/timeout/)
})
+ it('awaitOpen outlasts the sync timeout and resolves on the worker reply', async () => {
+ const worker = new FakeWorker()
+ const q = new PersistQueue(worker, { syncTimeoutMs: 30, asyncTimeoutMs: 30 })
+ let settled = false
+ const open = q.awaitOpen().finally(() => {
+ settled = true
+ })
+ // Liveness witness: the ping was posted to the worker.
+ expect(worker.sent.map(m => m.op.kind)).toEqual(['ping'])
+ await new Promise(r => setTimeout(r, 90))
+ expect(settled).toBe(false)
+ worker.emit('message', {
+ id: worker.sent[0].id,
+ ok: true,
+ result: 'pong',
+ } satisfies WorkerReply)
+ await expect(open).resolves.toBeUndefined()
+ })
+
+ it('awaitOpen rejects when the worker exits before answering', async () => {
+ const worker = new FakeWorker()
+ const q = new PersistQueue(worker, { syncTimeoutMs: 30, asyncTimeoutMs: 30 })
+ const open = q.awaitOpen()
+ expect(worker.sent.map(m => m.op.kind)).toEqual(['ping'])
+ worker.emit('exit', 1)
+ await expect(open).rejects.toThrow(/exited/)
+ })
+
it('enqueueAsync preserves order per sessionKey', async () => {
const worker = new FakeWorker()
const q = new PersistQueue(worker, { syncTimeoutMs: 200, asyncTimeoutMs: 500 })
diff --git a/mcp-host/src/core/conversation/persistence/conversationStoreFactory.ts b/mcp-host/src/core/conversation/persistence/conversationStoreFactory.ts
index e7b17ffa8..d44914bf0 100644
--- a/mcp-host/src/core/conversation/persistence/conversationStoreFactory.ts
+++ b/mcp-host/src/core/conversation/persistence/conversationStoreFactory.ts
@@ -106,9 +106,10 @@ export function createConversationStore(
// The worker answers only after it has opened and migrated the database, so
// the path is logged once that is true. One line per process names the
// database this Host reads and writes, so a Pod replacement that changes the
- // store is visible in the Pod logs. A worker exit also ends the process
- // through `onFailure`; this line only names the cause in the log.
- persistQueue.enqueueSync({ kind: 'ping' }).then(
+ // store is visible in the Pod logs. On a worker exit, `onFailure` ends the
+ // process in production before this rejection handler runs; the worker's
+ // fatal log and the exit log name the cause there.
+ persistQueue.awaitOpen().then(
() => logger.info({ dbPath: opts.dbPath, mode: opts.mode }, '[ConversationStore] opened'),
(err: unknown) => logger.error({ err }, '[ConversationStore] database did not open')
)
diff --git a/mcp-host/src/core/conversation/persistence/persistQueue.ts b/mcp-host/src/core/conversation/persistence/persistQueue.ts
index 133db702e..fd8e4d88e 100644
--- a/mcp-host/src/core/conversation/persistence/persistQueue.ts
+++ b/mcp-host/src/core/conversation/persistence/persistQueue.ts
@@ -57,7 +57,8 @@ export interface PersistQueueOptions {
interface PendingEntry {
resolve: (v: unknown) => void
reject: (e: Error) => void
- timeoutHandle: ReturnType<typeof setTimeout>
+ /** Absent for `awaitOpen`, which has no timeout. */
+ timeoutHandle?: ReturnType<typeof setTimeout>
op: WorkerOp
}
@@ -175,6 +176,22 @@ export class PersistQueue {
return this.send<T>(op, this.opts.syncTimeoutMs)
}
+ /**
+ * Resolves when the worker answers a `ping`. The worker registers its
+ * message handler only after it has opened and migrated the database, so
+ * this resolves once the database is open. No timeout: opening (spawn,
+ * migrations, recovery of a large WAL) can outlast any write budget.
+ * Rejects on a worker error reply, a worker exit, or `close()`.
+ */
+ awaitOpen(): Promise<void> {
+ try {
+ this.assertAdmissible()
+ } catch (err) {
+ return Promise.reject(err as Error)
+ }
+ return this.send<unknown>({ kind: 'ping' }, null).then(() => undefined)
+ }
+
/**
* Wait until every pending async write for `sessionKey` settles. Used by
* `SqliteConversationStore` before sync write-through operations that
@@ -272,14 +289,17 @@ export class PersistQueue {
return this.send<T>(op, timeoutMs)
}
- private send<T>(op: WorkerOp, timeoutMs: number): Promise<T> {
+ private send<T>(op: WorkerOp, timeoutMs: number | null): Promise<T> {
const id = randomUUID()
return new Promise<T>((resolve, reject) => {
- const timeoutHandle = setTimeout(() => {
- if (this.pending.delete(id)) {
- reject(new Error(`db worker timeout (${timeoutMs}ms) for op=${op.kind}`))
- }
- }, timeoutMs)
+ const timeoutHandle =
+ timeoutMs === null
+ ? undefined
+ : setTimeout(() => {
+ if (this.pending.delete(id)) {
+ reject(new Error(`db worker timeout (${timeoutMs}ms) for op=${op.kind}`))
+ }
+ }, timeoutMs)
const entry: PendingEntry = {
resolve: resolve as PendingEntry['resolve'],
reject,