Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(mcp-host): stop chained writes and concurrent close after a queue failure
Alfredo Lopezc911ad9c71a2
diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.jsonindex 8e131b7f2..8424c1378 100644--- a/mcp-host/package-lock.json+++ b/mcp-host/package-lock.json@@ -1,12 +1,12 @@{"name": "clerum-mcp-host",- "version": "0.1.562",+ "version": "0.1.563","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "clerum-mcp-host",- "version": "0.1.562",+ "version": "0.1.563","license": "SEE LICENSE IN LICENSE","dependencies": {"@anthropic-ai/sdk": "^0.32.0",diff --git a/mcp-host/package.json b/mcp-host/package.jsonindex 0226dcd6d..7dc5ae1aa 100644--- a/mcp-host/package.json+++ b/mcp-host/package.json@@ -1,6 +1,6 @@{"name": "clerum-mcp-host",- "version": "0.1.562",+ "version": "0.1.563","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__/persistQueue.test.ts b/mcp-host/src/core/conversation/persistence/__tests__/persistQueue.test.tsindex 0239e3b30..5ea1e61f9 100644--- a/mcp-host/src/core/conversation/persistence/__tests__/persistQueue.test.ts+++ b/mcp-host/src/core/conversation/persistence/__tests__/persistQueue.test.ts@@ -264,4 +264,68 @@ describe('PersistQueue', () => {worker.emit('message', { id: shutdown!.id, ok: true, result: null } satisfies WorkerReply)await expect(closing).rejects.toThrow(/disk I\/O error/)})++ it('never posts a write chained behind a failed async write on the same key', async () => {+ const worker = new FakeWorker()+ const q = new PersistQueue(worker, { syncTimeoutMs: 500, asyncTimeoutMs: 500 })+ q.enqueueAsync('s1', { kind: 'ping' })+ q.enqueueAsync('s1', { kind: 'ping' })+ const chainedSync = q.enqueueSync({ kind: 'ping' }, 's1')+ await Promise.resolve()+ // Liveness witness: the first write was posted; the others wait on it.+ expect(worker.sent.map(m => m.op.kind)).toEqual(['ping'])++ worker.emit('message', {+ id: worker.sent[0].id,+ ok: false,+ error: { code: 'SQLITE_FULL', message: 'database or disk is full' },+ } satisfies WorkerReply)+ await expect(chainedSync).rejects.toThrow(+ /async write failed \(sessionKey=s1\): database or disk is full/+ )+ await new Promise(r => setImmediate(r))+ expect(worker.sent).toHaveLength(1)+ })++ it('resolves a concurrent second close only after the shutdown ACK', async () => {+ const worker = new FakeWorker()+ const q = new PersistQueue(worker, { syncTimeoutMs: 500, asyncTimeoutMs: 500 })+ let secondSettled = false+ const first = q.close()+ const second = q.close().finally(() => {+ secondSettled = true+ })+ await new Promise(r => setImmediate(r))+ const shutdowns = worker.sent.filter(m => m.op.kind === 'shutdown')+ // Witness: exactly one shutdown was posted, and it is not ACKed yet.+ expect(shutdowns).toHaveLength(1)+ expect(secondSettled).toBe(false)++ worker.emit('message', { id: shutdowns[0].id, ok: true, result: null } satisfies WorkerReply)+ await Promise.all([first, second])+ expect(secondSettled).toBe(true)+ })++ it('rejects close when the worker reports a shutdown failure', async () => {+ const worker = new FakeWorker()+ const failures: Error[] = []+ const q = new PersistQueue(worker, {+ syncTimeoutMs: 500,+ asyncTimeoutMs: 500,+ onFailure: err => failures.push(err),+ })+ const closing = q.close()+ await new Promise(r => setImmediate(r))+ const shutdown = worker.sent.find(m => m.op.kind === 'shutdown')+ expect(shutdown).toBeDefined()+ worker.emit('message', {+ id: shutdown!.id,+ ok: false,+ error: { code: 'SQLITE_BUSY', message: 'database is locked' },+ } satisfies WorkerReply)+ await expect(closing).rejects.toThrow(/database is locked/)+ // The shutdown failure is the queue's failure; the exit that terminate()+ // causes afterwards is not reported as a second one.+ expect(failures.map(e => e.message)).toEqual(['database is locked'])+ })})diff --git a/mcp-host/src/core/conversation/persistence/persistQueue.ts b/mcp-host/src/core/conversation/persistence/persistQueue.tsindex 754311c84..2f9f7c97e 100644--- a/mcp-host/src/core/conversation/persistence/persistQueue.ts+++ b/mcp-host/src/core/conversation/persistence/persistQueue.ts@@ -70,6 +70,7 @@ export class PersistQueue {private shutdownAcked = false/** First failure that makes further writes unsafe; never cleared. */private failure: Error | null = null+ private closePromise: Promise<void> | null = nullconstructor(private readonly worker: WorkerLike,@@ -106,11 +107,13 @@ export class PersistQueue {this.assertAdmissible()const prev = this.writeChain.get(sessionKey) ?? Promise.resolve()const next = prev- .then(() => this.send(op, this.opts.asyncTimeoutMs))+ .then(() => this.sendUnlessFailed(op, this.opts.asyncTimeoutMs)).then(() => {/* discard the result — async path */}).catch((err: Error) => {+ // An earlier failure already recorded and logged.+ if (err === this.failure) returnconst failure = new Error(`async write failed (sessionKey=${sessionKey}): ${err.message ?? String(err)}`)@@ -148,7 +151,7 @@ export class PersistQueue {if (sessionKey) {const prev = this.writeChain.get(sessionKey) ?? Promise.resolve()const next = prev- .then(() => this.send<T>(op, this.opts.syncTimeoutMs))+ .then(() => this.sendUnlessFailed<T>(op, this.opts.syncTimeoutMs)).finally(() => {if (this.writeChain.get(sessionKey) === (next as Promise<unknown>)) {this.writeChain.delete(sessionKey)@@ -217,16 +220,22 @@ export class PersistQueue {* terminate it. Rejects with the first write failure, or with the shutdown* failure, so the caller sees that the durable trail is incomplete.*/- async close(): Promise<void> {- if (this.closed && !this.exited) return+ close(): Promise<void> {+ // A second caller waits for the same shutdown instead of returning early.+ this.closePromise ??= this.shutdown()+ return this.closePromise+ }++ private async shutdown(): Promise<void> {this.closed = trueawait this.drain()- let shutdownError: Error | null = nullif (!this.exited) {try {await this.send({ kind: 'shutdown' }, this.opts.syncTimeoutMs)} catch (err) {- shutdownError = err as Error+ // Recorded before terminate(), so the exit it causes is not reported+ // as a second failure.+ this.recordFailure(err as Error)}}const terminated = this.worker.terminate()@@ -239,7 +248,6 @@ export class PersistQueue {}this.pending.clear()if (this.failure) throw this.failure- if (shutdownError) throw shutdownError}private recordFailure(err: Error): void {@@ -253,6 +261,15 @@ export class PersistQueue {if (this.closed) throw new Error('PersistQueue is closed')}+ /**+ * Post a write that waited on its `sessionKey` chain. The queue may have+ * failed while it waited, and admission was checked before that.+ */+ private sendUnlessFailed<T>(op: WorkerOp, timeoutMs: number): Promise<T> {+ if (this.failure) return Promise.reject(this.failure)+ return this.send<T>(op, timeoutMs)+ }+private send<T>(op: WorkerOp, timeoutMs: number): Promise<T> {const id = randomUUID()return new Promise<T>((resolve, reject) => {