Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(mcp-host): bound dual-store catalog pagination
Josuéb5769233c854
diff --git a/mcp-host/src/core/conversation/persistence/__tests__/dualConversationStore.test.ts b/mcp-host/src/core/conversation/persistence/__tests__/dualConversationStore.test.tsindex 8df199555..f30125728 100644--- a/mcp-host/src/core/conversation/persistence/__tests__/dualConversationStore.test.ts+++ b/mcp-host/src/core/conversation/persistence/__tests__/dualConversationStore.test.ts@@ -1,4 +1,4 @@-import { describe, expect, it } from 'vitest'+import { describe, expect, it, vi } from 'vitest'import { ConversationManager } from '../../conversation'import { InMemoryConversationStore } from '../../conversationStore'import { DualConversationStore } from '../dualConversationStore'@@ -173,6 +173,37 @@ describe('DualConversationStore — dual-write parity', () => {}})+ it('bounds durable catalog probes when memory already has the requested page', async () => {+ const sqlite = makeSqliteStore({ cacheSize: 128 })+ try {+ const memory = new InMemoryConversationStore()+ const dual = new DualConversationStore(memory, sqlite.store)+ const manager = new ConversationManager(dual)+ for (let index = 0; index < 100; index += 1) {+ await manager.getOrCreate(`u-1:rpc:a:chat-${index.toString().padStart(3, '0')}`, {+ userId: 'u-1',+ })+ }++ const expected = await memory.listSessionSummariesByPrefix('u-1:rpc:', { limit: 2 })+ const sqliteList = vi.spyOn(sqlite.store, 'listSessionSummariesByPrefix')+ const firstPage = await dual.listSessionSummariesByPrefix('u-1:rpc:', { limit: 1 })++ expect(firstPage.map(summary => summary.key)).toEqual([expected[0]!.key])+ expect(sqliteList).toHaveBeenCalledTimes(1)++ const [first] = firstPage+ const secondPage = await dual.listSessionSummariesByPrefix('u-1:rpc:', {+ limit: 1,+ cursor: { updatedAt: first!.lastActivityAt, key: first!.key },+ })+ expect(secondPage.map(summary => summary.key)).toEqual([expected[1]!.key])+ expect(sqliteList).toHaveBeenCalledTimes(2)+ } finally {+ await sqlite.shutdown()+ }+ })+it('does not repeat a memory-owned key when sqlite has an older copy', async () => {const sqlite = makeSqliteStore({ cacheSize: 4 })try {@@ -199,7 +230,10 @@ describe('DualConversationStore — dual-write parity', () => {.prepare('UPDATE sessions SET started_at = ?, last_activity_at = ? WHERE id = ?').run(100, 200, sqliteC.id)- const dual = new DualConversationStore(memory, sqlite.store)+ const parity: Array<{ op: string; match: boolean }> = []+ const dual = new DualConversationStore(memory, sqlite.store, {+ recordParity: (op, match) => parity.push({ op, match }),+ })const keys: string[] = []let cursor: { updatedAt: Date; key: string } | undefinedfor (let pageNumber = 0; pageNumber < 5; pageNumber += 1) {@@ -213,6 +247,7 @@ describe('DualConversationStore — dual-write parity', () => {}expect(keys).toEqual([keyA, keyB, keyC])+ expect(parity).toContainEqual({ op: 'listSessionSummariesByPrefix', match: false })} finally {await sqlite.shutdown()}diff --git a/mcp-host/src/core/conversation/persistence/dualConversationStore.ts b/mcp-host/src/core/conversation/persistence/dualConversationStore.tsindex a328803b0..3300cdeb3 100644--- a/mcp-host/src/core/conversation/persistence/dualConversationStore.ts+++ b/mcp-host/src/core/conversation/persistence/dualConversationStore.ts@@ -47,6 +47,8 @@ const TOKEN_COUNTER_KEYS = new Set(['cache_write_tokens',])+const DUAL_CATALOG_FALLBACK_OVERFETCH = 2+function normalizeParityValue(value: unknown, key?: string): unknown {if (value instanceof Date) return value.toISOString()if (Array.isArray(value)) return value.map(entry => normalizeParityValue(entry))@@ -173,41 +175,27 @@ export class DualConversationStore implements ConversationStore {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+ const memoryFillsPage = query.limit !== undefined && memList.length >= query.limit+ // A full memory page is already canonical, so SQLite is sampled only for+ // parity. A partial page gets one bounded over-fetch for cold durable rows;+ // never drain cursor pages in proportion to the total mirrored catalog.+ const sqliteQuery =+ query.limit === undefined || memoryFillsPage+ ? query+ : { ...query, limit: query.limit * DUAL_CATALOG_FALLBACK_OVERFETCH }+ sqlList = await this.sqlite.listSessionSummariesByPrefix(prefix, sqliteQuery)+ for (const summary of sqlList) {+ if (!this.memory.has(summary.key)) durableByKey.set(summary.key, summary)}} catch {this.recordParity('listSessionSummariesByPrefix', false)return memList}- this.recordParity('listSessionSummariesByPrefix', pagesMatch(memList, sqlList))+ const sqlParityPage =+ query.limit === undefined ? sqlList : sqlList.slice(0, query.limit)+ this.recordParity('listSessionSummariesByPrefix', pagesMatch(memList, sqlParityPage))+ if (query.limit !== undefined && memList.length >= query.limit) return memList// 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.