Skip to content

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

TypeScript

fix(mcp-host): create a session once under concurrent requests

Alfredo Lopez4de7a5f2db09
diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.json
index 9b4763764..2c4557074 100644
--- a/mcp-host/package-lock.json
+++ b/mcp-host/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.560",
+ "version": "0.1.561",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-mcp-host",
- "version": "0.1.560",
+ "version": "0.1.561",
"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 d9e813667..cd50b32bf 100644
--- a/mcp-host/package.json
+++ b/mcp-host/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.560",
+ "version": "0.1.561",
"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/conversation.ts b/mcp-host/src/core/conversation/conversation.ts
index 5c9ec5755..838e61f61 100644
--- a/mcp-host/src/core/conversation/conversation.ts
+++ b/mcp-host/src/core/conversation/conversation.ts
@@ -49,6 +49,12 @@ import { userIdFromRpcPrefix } from './sessionKeyParts'
*/
export class ConversationManager {
private store: ConversationStore
+ /**
+ * Load-or-create in flight per session key. Concurrent callers on a new
+ * chat share one creation; two would insert two rows with the same
+ * `session_key`, and the second insert fails on its unique index.
+ */
+ private readonly pendingCreates = new Map<string, Promise<Conversation>>()
constructor(store: ConversationStore = new InMemoryConversationStore()) {
this.store = store
@@ -88,11 +94,21 @@ export class ConversationManager {
return cached
}
- const loaded = await this.store.getOrLoad(sessionKey)
- if (loaded) {
- this.assertSessionOwner(loaded, opts?.userId)
- return loaded
+ let pending = this.pendingCreates.get(sessionKey)
+ if (!pending) {
+ pending = this.loadOrCreate(sessionKey, opts).finally(() => {
+ this.pendingCreates.delete(sessionKey)
+ })
+ this.pendingCreates.set(sessionKey, pending)
}
+ const conv = await pending
+ this.assertSessionOwner(conv, opts?.userId)
+ return conv
+ }
+
+ private async loadOrCreate(sessionKey: string, opts?: GetOrCreateOptions): Promise<Conversation> {
+ const loaded = await this.store.getOrLoad(sessionKey)
+ if (loaded) return loaded
const parsed = parseSessionKey(sessionKey)
diff --git a/mcp-host/src/core/conversation/persistence/__tests__/concurrentSessionCreate.test.ts b/mcp-host/src/core/conversation/persistence/__tests__/concurrentSessionCreate.test.ts
new file mode 100644
index 000000000..a133b5388
--- /dev/null
+++ b/mcp-host/src/core/conversation/persistence/__tests__/concurrentSessionCreate.test.ts
@@ -0,0 +1,79 @@
+/**
+ * Two requests on a new chat can reach `getOrCreate` before either has
+ * cached the conversation, for example two parallel model selections or a
+ * selection racing the first message. Both must receive one conversation
+ * backed by one session row. Two creations insert two rows with the same
+ * `session_key`; the second insert fails on that unique index, and a failed
+ * async write makes the persist queue unusable.
+ */
+import { describe, expect, it, vi } from 'vitest'
+import { ConversationError } from '../../../errors'
+import { ConversationManager } from '../../conversation'
+import { PersistQueue, type WorkerLike } from '../persistQueue'
+import { SqliteConversationStore } from '../sqliteConversationStore'
+import { createInProcessWorker } from './testHelpers'
+
+function setup() {
+ const inner = createInProcessWorker(':memory:')
+ // 2 ms per message stands in for a worker thread's round trip. Without it
+ // the in-process worker answers within the same millisecond and both
+ // creations would mint the same `conv-<key>-<ms>` id, hiding the race.
+ const worker: WorkerLike = {
+ ...inner.worker,
+ postMessage: m => void setTimeout(() => inner.worker.postMessage(m), 2),
+ }
+ const onFailure = vi.fn()
+ const queue = new PersistQueue(worker, {
+ syncTimeoutMs: 2000,
+ asyncTimeoutMs: 5000,
+ onFailure,
+ })
+ const manager = new ConversationManager(new SqliteConversationStore(queue, { cacheSize: 8 }))
+ const rows = (key: string) =>
+ inner.db.prepare('SELECT id FROM sessions WHERE session_key = ?').all(key) as Array<{
+ id: string
+ }>
+ return { manager, queue, onFailure, rows }
+}
+
+describe('concurrent getOrCreate on a new session key', () => {
+ it('creates one conversation and one session row', async () => {
+ const { manager, queue, onFailure, rows } = setup()
+ const key = 'u-1:rpc:host-a:fresh-chat'
+
+ const [a, b, c] = await Promise.all([
+ manager.getOrCreate(key, { userId: 'u-1' }),
+ manager.getOrCreate(key, { userId: 'u-1' }),
+ manager.getOrCreate(key, { userId: 'u-1' }),
+ ])
+ await queue.drain()
+
+ expect(b).toBe(a)
+ expect(c).toBe(a)
+ // Witness: the row was written, under the id every caller received.
+ expect(rows(key)).toEqual([{ id: a.id }])
+ expect(onFailure).not.toHaveBeenCalled()
+ await queue.close()
+ })
+
+ it('still refuses a concurrent caller that does not own the session', async () => {
+ const { manager, queue, onFailure, rows } = setup()
+ const key = 'u-1:rpc:host-a:fresh-chat'
+
+ const [owner, other] = await Promise.allSettled([
+ manager.getOrCreate(key, { userId: 'u-1' }),
+ manager.getOrCreate(key, { userId: 'u-2' }),
+ ])
+ await queue.drain()
+
+ expect(owner.status).toBe('fulfilled')
+ expect(other.status).toBe('rejected')
+ expect((other as PromiseRejectedResult).reason).toBeInstanceOf(ConversationError)
+ // Witness: the owner's creation was persisted.
+ const created = (owner as PromiseFulfilledResult<{ id: string; user_id: string }>).value
+ expect(created.user_id).toBe('u-1')
+ expect(rows(key)).toEqual([{ id: created.id }])
+ expect(onFailure).not.toHaveBeenCalled()
+ await queue.close()
+ })
+})