Skip to content

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

TypeScript

fix(mcp-host): stabilize dual-store catalog paging

Josué892e11009457
diff --git a/mcp-host/src/core/conversation/persistence/__tests__/dualConversationStore.test.ts b/mcp-host/src/core/conversation/persistence/__tests__/dualConversationStore.test.ts
index ec495589d..8df199555 100644
--- a/mcp-host/src/core/conversation/persistence/__tests__/dualConversationStore.test.ts
+++ b/mcp-host/src/core/conversation/persistence/__tests__/dualConversationStore.test.ts
@@ -173,6 +173,51 @@ describe('DualConversationStore — dual-write parity', () => {
}
})
+ it('does not repeat a memory-owned key when sqlite has an older copy', async () => {
+ const sqlite = makeSqliteStore({ cacheSize: 4 })
+ try {
+ const memory = new InMemoryConversationStore()
+ const memoryManager = new ConversationManager(memory)
+ const sqliteManager = new ConversationManager(sqlite.store)
+ const keyA = 'u-1:rpc:a:chat-a'
+ const keyB = 'u-1:rpc:a:chat-b'
+ const keyC = 'u-1:rpc:a:chat-c'
+
+ const memoryA = await memoryManager.getOrCreate(keyA, { userId: 'u-1' })
+ const memoryB = await memoryManager.getOrCreate(keyB, { userId: 'u-1' })
+ memoryA.updated_at = new Date(400_000)
+ memoryB.updated_at = new Date(300_000)
+
+ const sqliteA = await sqliteManager.getOrCreate(keyA, { userId: 'u-1' })
+ const sqliteC = await sqliteManager.getOrCreate(keyC, { userId: 'u-1' })
+ await sqlite.persistQueue.drainSessionKey(keyA)
+ await sqlite.persistQueue.drainSessionKey(keyC)
+ sqlite.worker.db
+ .prepare('UPDATE sessions SET started_at = ?, last_activity_at = ? WHERE id = ?')
+ .run(100, 250, sqliteA.id)
+ sqlite.worker.db
+ .prepare('UPDATE sessions SET started_at = ?, last_activity_at = ? WHERE id = ?')
+ .run(100, 200, sqliteC.id)
+
+ const dual = new DualConversationStore(memory, sqlite.store)
+ const keys: string[] = []
+ let cursor: { updatedAt: Date; key: string } | undefined
+ for (let pageNumber = 0; pageNumber < 5; pageNumber += 1) {
+ const page = await dual.listSessionSummariesByPrefix('u-1:rpc:', {
+ limit: 1,
+ cursor,
+ })
+ if (page.length === 0) break
+ keys.push(page[0]!.key)
+ cursor = { updatedAt: page[0]!.lastActivityAt, key: page[0]!.key }
+ }
+
+ expect(keys).toEqual([keyA, keyB, keyC])
+ } finally {
+ await sqlite.shutdown()
+ }
+ })
+
it('keeps canonical memory reads available when sqlite parity probes fail', async () => {
const sqlite = makeSqliteStore({ cacheSize: 4 })
try {
diff --git a/mcp-host/src/core/conversation/persistence/dualConversationStore.ts b/mcp-host/src/core/conversation/persistence/dualConversationStore.ts
index 6ee688b5f..a328803b0 100644
--- a/mcp-host/src/core/conversation/persistence/dualConversationStore.ts
+++ b/mcp-host/src/core/conversation/persistence/dualConversationStore.ts
@@ -171,15 +171,47 @@ export class DualConversationStore implements ConversationStore {
): Promise<ConversationSessionSummary[]> {
const memList = await this.memory.listSessionSummariesByPrefix(prefix, query)
let sqlList: ConversationSessionSummary[]
+ const durableByKey = new Map<string, ConversationSessionSummary>()
try {
sqlList = await this.sqlite.listSessionSummariesByPrefix(prefix, query)
+ let sqlPage = sqlList
+ while (true) {
+ for (const summary of sqlPage) {
+ if (!this.memory.has(summary.key)) durableByKey.set(summary.key, summary)
+ }
+ if (
+ query.limit === undefined ||
+ durableByKey.size >= query.limit ||
+ sqlPage.length < query.limit
+ ) {
+ break
+ }
+ const last = sqlPage.at(-1)
+ if (!last) break
+ const nextPage = await this.sqlite.listSessionSummariesByPrefix(prefix, {
+ ...query,
+ cursor: { updatedAt: last.lastActivityAt, key: last.key },
+ })
+ const nextLast = nextPage.at(-1)
+ if (
+ nextLast &&
+ nextLast.key === last.key &&
+ nextLast.lastActivityAt.getTime() === last.lastActivityAt.getTime()
+ ) {
+ break
+ }
+ sqlPage = nextPage
+ }
} catch {
this.recordParity('listSessionSummariesByPrefix', false)
return memList
}
this.recordParity('listSessionSummariesByPrefix', pagesMatch(memList, sqlList))
- const byKey = new Map(sqlList.map(summary => [summary.key, summary]))
+ // A live memory copy is canonical even when its timestamp places it outside
+ // this page. Otherwise an older SQLite copy can cross a later cursor and
+ // repeat a key that an earlier page already emitted from memory.
+ const byKey = new Map(durableByKey)
for (const summary of memList) byKey.set(summary.key, summary)
const merged = Array.from(byKey.values()).sort(compareSummaries)
return query.limit === undefined ? merged : merged.slice(0, query.limit)